blob: fc06720b53c1442a8dd3222ed7be482a8993ab92 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ursula Braunb38d7322017-01-09 16:55:25 +01002/*
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
4 *
5 * Socket Closing - normal and abnormal
6 *
7 * Copyright IBM Corp. 2016
8 *
9 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
10 */
11
12#include <linux/workqueue.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010013#include <linux/sched/signal.h>
14
Ursula Braunb38d7322017-01-09 16:55:25 +010015#include <net/sock.h>
16
17#include "smc.h"
18#include "smc_tx.h"
19#include "smc_cdc.h"
20#include "smc_close.h"
21
Ursula Braun127f4972018-01-26 09:28:49 +010022#define SMC_CLOSE_WAIT_LISTEN_CLCSOCK_TIME (5 * HZ)
23
Karsten Graulfd577702019-04-11 11:17:30 +020024/* release the clcsock that is assigned to the smc_sock */
25void smc_clcsock_release(struct smc_sock *smc)
26{
27 struct socket *tcp;
28
29 if (smc->listen_smc && current_work() != &smc->smc_listen_work)
30 cancel_work_sync(&smc->smc_listen_work);
31 mutex_lock(&smc->clcsock_release_lock);
32 if (smc->clcsock) {
33 tcp = smc->clcsock;
34 smc->clcsock = NULL;
35 sock_release(tcp);
36 }
37 mutex_unlock(&smc->clcsock_release_lock);
38}
39
Ursula Braunb38d7322017-01-09 16:55:25 +010040static void smc_close_cleanup_listen(struct sock *parent)
41{
42 struct sock *sk;
43
44 /* Close non-accepted connections */
45 while ((sk = smc_accept_dequeue(parent, NULL)))
46 smc_close_non_accepted(sk);
47}
48
Ursula Braunb38d7322017-01-09 16:55:25 +010049/* wait for sndbuf data being transmitted */
50static void smc_close_stream_wait(struct smc_sock *smc, long timeout)
51{
52 DEFINE_WAIT_FUNC(wait, woken_wake_function);
53 struct sock *sk = &smc->sk;
54
55 if (!timeout)
56 return;
57
58 if (!smc_tx_prepared_sends(&smc->conn))
59 return;
60
61 smc->wait_close_tx_prepared = 1;
62 add_wait_queue(sk_sleep(sk), &wait);
63 while (!signal_pending(current) && timeout) {
64 int rc;
65
66 rc = sk_wait_event(sk, &timeout,
67 !smc_tx_prepared_sends(&smc->conn) ||
68 (sk->sk_err == ECONNABORTED) ||
69 (sk->sk_err == ECONNRESET),
70 &wait);
71 if (rc)
72 break;
73 }
74 remove_wait_queue(sk_sleep(sk), &wait);
75 smc->wait_close_tx_prepared = 0;
76}
77
78void smc_close_wake_tx_prepared(struct smc_sock *smc)
79{
80 if (smc->wait_close_tx_prepared)
81 /* wake up socket closing */
82 smc->sk.sk_state_change(&smc->sk);
83}
84
85static int smc_close_wr(struct smc_connection *conn)
86{
87 conn->local_tx_ctrl.conn_state_flags.peer_done_writing = 1;
88
89 return smc_cdc_get_slot_and_msg_send(conn);
90}
91
92static int smc_close_final(struct smc_connection *conn)
93{
94 if (atomic_read(&conn->bytes_to_rcv))
95 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
96 else
97 conn->local_tx_ctrl.conn_state_flags.peer_conn_closed = 1;
98
99 return smc_cdc_get_slot_and_msg_send(conn);
100}
101
102static int smc_close_abort(struct smc_connection *conn)
103{
104 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
105
106 return smc_cdc_get_slot_and_msg_send(conn);
107}
108
109/* terminate smc socket abnormally - active abort
Ursula Braun732720f2018-01-25 11:15:35 +0100110 * link group is terminated, i.e. RDMA communication no longer possible
Ursula Braunb38d7322017-01-09 16:55:25 +0100111 */
Ursula Brauna8ae8902017-12-07 13:38:44 +0100112static void smc_close_active_abort(struct smc_sock *smc)
Ursula Braunb38d7322017-01-09 16:55:25 +0100113{
Ursula Braun3163c502018-01-24 10:28:12 +0100114 struct sock *sk = &smc->sk;
115
Ursula Braunb38d7322017-01-09 16:55:25 +0100116 struct smc_cdc_conn_state_flags *txflags =
117 &smc->conn.local_tx_ctrl.conn_state_flags;
118
Ursula Braundd65d872018-09-18 15:46:36 +0200119 if (sk->sk_state != SMC_INIT && smc->clcsock && smc->clcsock->sk) {
120 sk->sk_err = ECONNABORTED;
121 if (smc->clcsock && smc->clcsock->sk) {
122 smc->clcsock->sk->sk_err = ECONNABORTED;
123 smc->clcsock->sk->sk_state_change(smc->clcsock->sk);
124 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100125 }
Ursula Braun3163c502018-01-24 10:28:12 +0100126 switch (sk->sk_state) {
Ursula Braun46c28db2017-04-10 14:58:01 +0200127 case SMC_ACTIVE:
Ursula Braun3163c502018-01-24 10:28:12 +0100128 sk->sk_state = SMC_PEERABORTWAIT;
Ursula Braun611b63a2018-01-25 11:15:31 +0100129 release_sock(sk);
130 cancel_delayed_work_sync(&smc->conn.tx_work);
131 lock_sock(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100132 sock_put(sk); /* passive closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100133 break;
134 case SMC_APPCLOSEWAIT1:
135 case SMC_APPCLOSEWAIT2:
Ursula Braunb38d7322017-01-09 16:55:25 +0100136 if (!smc_cdc_rxed_any_close(&smc->conn))
Ursula Braun3163c502018-01-24 10:28:12 +0100137 sk->sk_state = SMC_PEERABORTWAIT;
Ursula Braunb38d7322017-01-09 16:55:25 +0100138 else
Ursula Braun3163c502018-01-24 10:28:12 +0100139 sk->sk_state = SMC_CLOSED;
Ursula Braun611b63a2018-01-25 11:15:31 +0100140 release_sock(sk);
141 cancel_delayed_work_sync(&smc->conn.tx_work);
142 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100143 break;
144 case SMC_PEERCLOSEWAIT1:
145 case SMC_PEERCLOSEWAIT2:
146 if (!txflags->peer_conn_closed) {
Ursula Braun51f1de72018-01-26 09:28:48 +0100147 /* just SHUTDOWN_SEND done */
Ursula Braun3163c502018-01-24 10:28:12 +0100148 sk->sk_state = SMC_PEERABORTWAIT;
Ursula Braunb38d7322017-01-09 16:55:25 +0100149 } else {
Ursula Braun3163c502018-01-24 10:28:12 +0100150 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100151 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100152 sock_put(sk); /* passive closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100153 break;
154 case SMC_PROCESSABORT:
155 case SMC_APPFINCLOSEWAIT:
Ursula Braun3163c502018-01-24 10:28:12 +0100156 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100157 break;
158 case SMC_PEERFINCLOSEWAIT:
Ursula Braun51f1de72018-01-26 09:28:48 +0100159 sock_put(sk); /* passive closing */
160 break;
Ursula Braundd65d872018-09-18 15:46:36 +0200161 case SMC_INIT:
Ursula Braunb38d7322017-01-09 16:55:25 +0100162 case SMC_PEERABORTWAIT:
163 case SMC_CLOSED:
164 break;
165 }
166
Ursula Braun3163c502018-01-24 10:28:12 +0100167 sock_set_flag(sk, SOCK_DEAD);
168 sk->sk_state_change(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100169}
170
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200171static inline bool smc_close_sent_any_close(struct smc_connection *conn)
172{
173 return conn->local_tx_ctrl.conn_state_flags.peer_conn_abort ||
174 conn->local_tx_ctrl.conn_state_flags.peer_conn_closed;
175}
176
Ursula Braunb38d7322017-01-09 16:55:25 +0100177int smc_close_active(struct smc_sock *smc)
178{
179 struct smc_cdc_conn_state_flags *txflags =
180 &smc->conn.local_tx_ctrl.conn_state_flags;
Ursula Braunb38d7322017-01-09 16:55:25 +0100181 struct smc_connection *conn = &smc->conn;
182 struct sock *sk = &smc->sk;
183 int old_state;
Ursula Braun8c96fee2017-09-21 09:16:34 +0200184 long timeout;
Ursula Braunb38d7322017-01-09 16:55:25 +0100185 int rc = 0;
186
Ursula Braun8c96fee2017-09-21 09:16:34 +0200187 timeout = current->flags & PF_EXITING ?
188 0 : sock_flag(sk, SOCK_LINGER) ?
189 sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braunb38d7322017-01-09 16:55:25 +0100190
Ursula Braunb38d7322017-01-09 16:55:25 +0100191 old_state = sk->sk_state;
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100192again:
193 switch (sk->sk_state) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100194 case SMC_INIT:
195 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100196 break;
197 case SMC_LISTEN:
198 sk->sk_state = SMC_CLOSED;
199 sk->sk_state_change(sk); /* wake up accept */
200 if (smc->clcsock && smc->clcsock->sk) {
201 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
202 /* wake up kernel_accept of smc_tcp_listen_worker */
203 smc->clcsock->sk->sk_data_ready(smc->clcsock->sk);
204 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100205 smc_close_cleanup_listen(sk);
Ursula Braun3d502062018-03-13 10:41:54 +0100206 release_sock(sk);
207 flush_work(&smc->tcp_listen_work);
208 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100209 break;
210 case SMC_ACTIVE:
211 smc_close_stream_wait(smc, timeout);
212 release_sock(sk);
Ursula Braun18e537c2017-09-21 09:16:33 +0200213 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100214 lock_sock(sk);
215 if (sk->sk_state == SMC_ACTIVE) {
216 /* send close request */
217 rc = smc_close_final(conn);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100218 if (rc)
219 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100220 sk->sk_state = SMC_PEERCLOSEWAIT1;
221 } else {
222 /* peer event has changed the state */
223 goto again;
224 }
225 break;
226 case SMC_APPFINCLOSEWAIT:
227 /* socket already shutdown wr or both (active close) */
228 if (txflags->peer_done_writing &&
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200229 !smc_close_sent_any_close(conn)) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100230 /* just shutdown wr done, send close request */
231 rc = smc_close_final(conn);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100232 if (rc)
233 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100234 }
235 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100236 break;
237 case SMC_APPCLOSEWAIT1:
238 case SMC_APPCLOSEWAIT2:
239 if (!smc_cdc_rxed_any_close(conn))
240 smc_close_stream_wait(smc, timeout);
241 release_sock(sk);
Ursula Braun18e537c2017-09-21 09:16:33 +0200242 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100243 lock_sock(sk);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100244 if (sk->sk_state != SMC_APPCLOSEWAIT1 &&
245 sk->sk_state != SMC_APPCLOSEWAIT2)
246 goto again;
247 /* confirm close from peer */
248 rc = smc_close_final(conn);
249 if (rc)
250 break;
Ursula Braun51f1de72018-01-26 09:28:48 +0100251 if (smc_cdc_rxed_any_close(conn)) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100252 /* peer has closed the socket already */
253 sk->sk_state = SMC_CLOSED;
Ursula Braun51f1de72018-01-26 09:28:48 +0100254 sock_put(sk); /* postponed passive closing */
255 } else {
Ursula Braunb38d7322017-01-09 16:55:25 +0100256 /* peer has just issued a shutdown write */
257 sk->sk_state = SMC_PEERFINCLOSEWAIT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100258 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100259 break;
260 case SMC_PEERCLOSEWAIT1:
261 case SMC_PEERCLOSEWAIT2:
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200262 if (txflags->peer_done_writing &&
263 !smc_close_sent_any_close(conn)) {
264 /* just shutdown wr done, send close request */
265 rc = smc_close_final(conn);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100266 if (rc)
267 break;
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200268 }
269 /* peer sending PeerConnectionClosed will cause transition */
270 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100271 case SMC_PEERFINCLOSEWAIT:
272 /* peer sending PeerConnectionClosed will cause transition */
273 break;
274 case SMC_PROCESSABORT:
Ursula Braunb38d7322017-01-09 16:55:25 +0100275 smc_close_abort(conn);
276 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100277 break;
278 case SMC_PEERABORTWAIT:
279 case SMC_CLOSED:
280 /* nothing to do, add tracing in future patch */
281 break;
282 }
283
284 if (old_state != sk->sk_state)
Ursula Braun3163c502018-01-24 10:28:12 +0100285 sk->sk_state_change(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100286 return rc;
287}
288
289static void smc_close_passive_abort_received(struct smc_sock *smc)
290{
291 struct smc_cdc_conn_state_flags *txflags =
292 &smc->conn.local_tx_ctrl.conn_state_flags;
293 struct sock *sk = &smc->sk;
294
295 switch (sk->sk_state) {
Ursula Braun51f1de72018-01-26 09:28:48 +0100296 case SMC_INIT:
Ursula Braunb38d7322017-01-09 16:55:25 +0100297 case SMC_ACTIVE:
Ursula Braunb38d7322017-01-09 16:55:25 +0100298 case SMC_APPCLOSEWAIT1:
Ursula Braun51f1de72018-01-26 09:28:48 +0100299 sk->sk_state = SMC_PROCESSABORT;
300 sock_put(sk); /* passive closing */
301 break;
302 case SMC_APPFINCLOSEWAIT:
Ursula Braunb38d7322017-01-09 16:55:25 +0100303 sk->sk_state = SMC_PROCESSABORT;
304 break;
305 case SMC_PEERCLOSEWAIT1:
306 case SMC_PEERCLOSEWAIT2:
307 if (txflags->peer_done_writing &&
Ursula Braun51f1de72018-01-26 09:28:48 +0100308 !smc_close_sent_any_close(&smc->conn))
Ursula Braunb38d7322017-01-09 16:55:25 +0100309 /* just shutdown, but not yet closed locally */
Ursula Braunb38d7322017-01-09 16:55:25 +0100310 sk->sk_state = SMC_PROCESSABORT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100311 else
Ursula Braunb38d7322017-01-09 16:55:25 +0100312 sk->sk_state = SMC_CLOSED;
Ursula Braun51f1de72018-01-26 09:28:48 +0100313 sock_put(sk); /* passive closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100314 break;
Ursula Braun51f1de72018-01-26 09:28:48 +0100315 case SMC_APPCLOSEWAIT2:
Ursula Braunb38d7322017-01-09 16:55:25 +0100316 case SMC_PEERFINCLOSEWAIT:
Ursula Braun51f1de72018-01-26 09:28:48 +0100317 sk->sk_state = SMC_CLOSED;
318 sock_put(sk); /* passive closing */
319 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100320 case SMC_PEERABORTWAIT:
321 sk->sk_state = SMC_CLOSED;
322 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100323 case SMC_PROCESSABORT:
324 /* nothing to do, add tracing in future patch */
325 break;
326 }
327}
328
Ursula Braun732720f2018-01-25 11:15:35 +0100329/* Either some kind of closing has been received: peer_conn_closed,
330 * peer_conn_abort, or peer_done_writing
331 * or the link group of the connection terminates abnormally.
Ursula Braunb38d7322017-01-09 16:55:25 +0100332 */
Ursula Braun46c28db2017-04-10 14:58:01 +0200333static void smc_close_passive_work(struct work_struct *work)
Ursula Braunb38d7322017-01-09 16:55:25 +0100334{
Ursula Braun46c28db2017-04-10 14:58:01 +0200335 struct smc_connection *conn = container_of(work,
336 struct smc_connection,
337 close_work);
338 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
339 struct smc_cdc_conn_state_flags *rxflags;
Karsten Graulfd577702019-04-11 11:17:30 +0200340 bool release_clcsock = false;
Ursula Braunb38d7322017-01-09 16:55:25 +0100341 struct sock *sk = &smc->sk;
342 int old_state;
343
Ursula Braun3163c502018-01-24 10:28:12 +0100344 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100345 old_state = sk->sk_state;
346
Ursula Braun46c28db2017-04-10 14:58:01 +0200347 if (!conn->alert_token_local) {
348 /* abnormal termination */
349 smc_close_active_abort(smc);
350 goto wakeup;
351 }
352
Ursula Braun3163c502018-01-24 10:28:12 +0100353 rxflags = &conn->local_rx_ctrl.conn_state_flags;
Ursula Braunb38d7322017-01-09 16:55:25 +0100354 if (rxflags->peer_conn_abort) {
Ursula Braun732720f2018-01-25 11:15:35 +0100355 /* peer has not received all data */
Ursula Braunb38d7322017-01-09 16:55:25 +0100356 smc_close_passive_abort_received(smc);
Ursula Braun611b63a2018-01-25 11:15:31 +0100357 release_sock(&smc->sk);
358 cancel_delayed_work_sync(&conn->tx_work);
359 lock_sock(&smc->sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100360 goto wakeup;
361 }
362
363 switch (sk->sk_state) {
364 case SMC_INIT:
Ursula Braun84b799a2019-02-04 13:44:47 +0100365 sk->sk_state = SMC_APPCLOSEWAIT1;
Ursula Braunb38d7322017-01-09 16:55:25 +0100366 break;
367 case SMC_ACTIVE:
368 sk->sk_state = SMC_APPCLOSEWAIT1;
Ursula Braun51f1de72018-01-26 09:28:48 +0100369 /* postpone sock_put() for passive closing to cover
370 * received SEND_SHUTDOWN as well
371 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100372 break;
373 case SMC_PEERCLOSEWAIT1:
374 if (rxflags->peer_done_writing)
375 sk->sk_state = SMC_PEERCLOSEWAIT2;
Gustavo A. R. Silva7f6b4372017-10-21 20:35:30 -0500376 /* fall through */
377 /* to check for closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100378 case SMC_PEERCLOSEWAIT2:
Ursula Braun3163c502018-01-24 10:28:12 +0100379 if (!smc_cdc_rxed_any_close(conn))
Ursula Braunb38d7322017-01-09 16:55:25 +0100380 break;
381 if (sock_flag(sk, SOCK_DEAD) &&
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200382 smc_close_sent_any_close(conn)) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100383 /* smc_release has already been called locally */
384 sk->sk_state = SMC_CLOSED;
385 } else {
386 /* just shutdown, but not yet closed locally */
387 sk->sk_state = SMC_APPFINCLOSEWAIT;
388 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100389 sock_put(sk); /* passive closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100390 break;
Ursula Braun5ac92a02018-01-25 11:15:32 +0100391 case SMC_PEERFINCLOSEWAIT:
Ursula Braun51f1de72018-01-26 09:28:48 +0100392 if (smc_cdc_rxed_any_close(conn)) {
Ursula Braun5ac92a02018-01-25 11:15:32 +0100393 sk->sk_state = SMC_CLOSED;
Ursula Braun51f1de72018-01-26 09:28:48 +0100394 sock_put(sk); /* passive closing */
395 }
Ursula Braun5ac92a02018-01-25 11:15:32 +0100396 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100397 case SMC_APPCLOSEWAIT1:
398 case SMC_APPCLOSEWAIT2:
Ursula Braun51f1de72018-01-26 09:28:48 +0100399 /* postpone sock_put() for passive closing to cover
400 * received SEND_SHUTDOWN as well
401 */
402 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100403 case SMC_APPFINCLOSEWAIT:
404 case SMC_PEERABORTWAIT:
405 case SMC_PROCESSABORT:
406 case SMC_CLOSED:
407 /* nothing to do, add tracing in future patch */
408 break;
409 }
410
411wakeup:
Ursula Braunb38d7322017-01-09 16:55:25 +0100412 sk->sk_data_ready(sk); /* wakeup blocked rcvbuf consumers */
413 sk->sk_write_space(sk); /* wakeup blocked sndbuf producers */
414
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200415 if (old_state != sk->sk_state) {
416 sk->sk_state_change(sk);
417 if ((sk->sk_state == SMC_CLOSED) &&
Ursula Braunb03faa12019-02-07 15:56:15 +0100418 (sock_flag(sk, SOCK_DEAD) || !sk->sk_socket)) {
Ursula Braun3163c502018-01-24 10:28:12 +0100419 smc_conn_free(conn);
Karsten Graulfd577702019-04-11 11:17:30 +0200420 if (smc->clcsock)
421 release_clcsock = true;
Ursula Braunb03faa12019-02-07 15:56:15 +0100422 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100423 }
Ursula Braun3163c502018-01-24 10:28:12 +0100424 release_sock(sk);
Karsten Graulfd577702019-04-11 11:17:30 +0200425 if (release_clcsock)
426 smc_clcsock_release(smc);
Ursula Braun51f1de72018-01-26 09:28:48 +0100427 sock_put(sk); /* sock_hold done by schedulers of close_work */
Ursula Braunb38d7322017-01-09 16:55:25 +0100428}
429
430int smc_close_shutdown_write(struct smc_sock *smc)
431{
432 struct smc_connection *conn = &smc->conn;
Ursula Braunb38d7322017-01-09 16:55:25 +0100433 struct sock *sk = &smc->sk;
434 int old_state;
Ursula Braun8c96fee2017-09-21 09:16:34 +0200435 long timeout;
Ursula Braunb38d7322017-01-09 16:55:25 +0100436 int rc = 0;
437
Ursula Braun8c96fee2017-09-21 09:16:34 +0200438 timeout = current->flags & PF_EXITING ?
439 0 : sock_flag(sk, SOCK_LINGER) ?
440 sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braunb38d7322017-01-09 16:55:25 +0100441
Ursula Braunb38d7322017-01-09 16:55:25 +0100442 old_state = sk->sk_state;
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100443again:
444 switch (sk->sk_state) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100445 case SMC_ACTIVE:
446 smc_close_stream_wait(smc, timeout);
447 release_sock(sk);
Ursula Braun18e537c2017-09-21 09:16:33 +0200448 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100449 lock_sock(sk);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100450 if (sk->sk_state != SMC_ACTIVE)
451 goto again;
Ursula Braunb38d7322017-01-09 16:55:25 +0100452 /* send close wr request */
453 rc = smc_close_wr(conn);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100454 if (rc)
455 break;
456 sk->sk_state = SMC_PEERCLOSEWAIT1;
Ursula Braunb38d7322017-01-09 16:55:25 +0100457 break;
458 case SMC_APPCLOSEWAIT1:
459 /* passive close */
460 if (!smc_cdc_rxed_any_close(conn))
461 smc_close_stream_wait(smc, timeout);
462 release_sock(sk);
Ursula Braun18e537c2017-09-21 09:16:33 +0200463 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100464 lock_sock(sk);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100465 if (sk->sk_state != SMC_APPCLOSEWAIT1)
466 goto again;
Ursula Braunb38d7322017-01-09 16:55:25 +0100467 /* confirm close from peer */
468 rc = smc_close_wr(conn);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100469 if (rc)
470 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100471 sk->sk_state = SMC_APPCLOSEWAIT2;
472 break;
473 case SMC_APPCLOSEWAIT2:
474 case SMC_PEERFINCLOSEWAIT:
475 case SMC_PEERCLOSEWAIT1:
476 case SMC_PEERCLOSEWAIT2:
477 case SMC_APPFINCLOSEWAIT:
478 case SMC_PROCESSABORT:
479 case SMC_PEERABORTWAIT:
480 /* nothing to do, add tracing in future patch */
481 break;
482 }
483
484 if (old_state != sk->sk_state)
Ursula Braun3163c502018-01-24 10:28:12 +0100485 sk->sk_state_change(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100486 return rc;
487}
Ursula Braun46c28db2017-04-10 14:58:01 +0200488
489/* Initialize close properties on connection establishment. */
490void smc_close_init(struct smc_sock *smc)
491{
492 INIT_WORK(&smc->conn.close_work, smc_close_passive_work);
493}