blob: 2ad37e998509310f210f4e3654cc054487731e87 [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
Ursula Braunb38d7322017-01-09 16:55:25 +010024static void smc_close_cleanup_listen(struct sock *parent)
25{
26 struct sock *sk;
27
28 /* Close non-accepted connections */
29 while ((sk = smc_accept_dequeue(parent, NULL)))
30 smc_close_non_accepted(sk);
31}
32
Ursula Braunb38d7322017-01-09 16:55:25 +010033/* wait for sndbuf data being transmitted */
34static void smc_close_stream_wait(struct smc_sock *smc, long timeout)
35{
36 DEFINE_WAIT_FUNC(wait, woken_wake_function);
37 struct sock *sk = &smc->sk;
38
39 if (!timeout)
40 return;
41
42 if (!smc_tx_prepared_sends(&smc->conn))
43 return;
44
45 smc->wait_close_tx_prepared = 1;
46 add_wait_queue(sk_sleep(sk), &wait);
47 while (!signal_pending(current) && timeout) {
48 int rc;
49
50 rc = sk_wait_event(sk, &timeout,
51 !smc_tx_prepared_sends(&smc->conn) ||
52 (sk->sk_err == ECONNABORTED) ||
53 (sk->sk_err == ECONNRESET),
54 &wait);
55 if (rc)
56 break;
57 }
58 remove_wait_queue(sk_sleep(sk), &wait);
59 smc->wait_close_tx_prepared = 0;
60}
61
62void smc_close_wake_tx_prepared(struct smc_sock *smc)
63{
64 if (smc->wait_close_tx_prepared)
65 /* wake up socket closing */
66 smc->sk.sk_state_change(&smc->sk);
67}
68
69static int smc_close_wr(struct smc_connection *conn)
70{
71 conn->local_tx_ctrl.conn_state_flags.peer_done_writing = 1;
72
73 return smc_cdc_get_slot_and_msg_send(conn);
74}
75
76static int smc_close_final(struct smc_connection *conn)
77{
78 if (atomic_read(&conn->bytes_to_rcv))
79 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
80 else
81 conn->local_tx_ctrl.conn_state_flags.peer_conn_closed = 1;
82
83 return smc_cdc_get_slot_and_msg_send(conn);
84}
85
86static int smc_close_abort(struct smc_connection *conn)
87{
88 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
89
90 return smc_cdc_get_slot_and_msg_send(conn);
91}
92
93/* terminate smc socket abnormally - active abort
Ursula Braun732720f2018-01-25 11:15:35 +010094 * link group is terminated, i.e. RDMA communication no longer possible
Ursula Braunb38d7322017-01-09 16:55:25 +010095 */
Ursula Brauna8ae8902017-12-07 13:38:44 +010096static void smc_close_active_abort(struct smc_sock *smc)
Ursula Braunb38d7322017-01-09 16:55:25 +010097{
Ursula Braun3163c502018-01-24 10:28:12 +010098 struct sock *sk = &smc->sk;
99
Ursula Braunb38d7322017-01-09 16:55:25 +0100100 struct smc_cdc_conn_state_flags *txflags =
101 &smc->conn.local_tx_ctrl.conn_state_flags;
102
Ursula Braundd65d872018-09-18 15:46:36 +0200103 if (sk->sk_state != SMC_INIT && smc->clcsock && smc->clcsock->sk) {
104 sk->sk_err = ECONNABORTED;
105 if (smc->clcsock && smc->clcsock->sk) {
106 smc->clcsock->sk->sk_err = ECONNABORTED;
107 smc->clcsock->sk->sk_state_change(smc->clcsock->sk);
108 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100109 }
Ursula Braun3163c502018-01-24 10:28:12 +0100110 switch (sk->sk_state) {
Ursula Braun46c28db2017-04-10 14:58:01 +0200111 case SMC_ACTIVE:
Ursula Braun3163c502018-01-24 10:28:12 +0100112 sk->sk_state = SMC_PEERABORTWAIT;
Ursula Braun611b63a2018-01-25 11:15:31 +0100113 release_sock(sk);
114 cancel_delayed_work_sync(&smc->conn.tx_work);
115 lock_sock(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100116 sock_put(sk); /* passive closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100117 break;
118 case SMC_APPCLOSEWAIT1:
119 case SMC_APPCLOSEWAIT2:
Ursula Braunb38d7322017-01-09 16:55:25 +0100120 if (!smc_cdc_rxed_any_close(&smc->conn))
Ursula Braun3163c502018-01-24 10:28:12 +0100121 sk->sk_state = SMC_PEERABORTWAIT;
Ursula Braunb38d7322017-01-09 16:55:25 +0100122 else
Ursula Braun3163c502018-01-24 10:28:12 +0100123 sk->sk_state = SMC_CLOSED;
Ursula Braun611b63a2018-01-25 11:15:31 +0100124 release_sock(sk);
125 cancel_delayed_work_sync(&smc->conn.tx_work);
126 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100127 break;
128 case SMC_PEERCLOSEWAIT1:
129 case SMC_PEERCLOSEWAIT2:
130 if (!txflags->peer_conn_closed) {
Ursula Braun51f1de72018-01-26 09:28:48 +0100131 /* just SHUTDOWN_SEND done */
Ursula Braun3163c502018-01-24 10:28:12 +0100132 sk->sk_state = SMC_PEERABORTWAIT;
Ursula Braunb38d7322017-01-09 16:55:25 +0100133 } else {
Ursula Braun3163c502018-01-24 10:28:12 +0100134 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100135 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100136 sock_put(sk); /* passive closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100137 break;
138 case SMC_PROCESSABORT:
139 case SMC_APPFINCLOSEWAIT:
Ursula Braun3163c502018-01-24 10:28:12 +0100140 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100141 break;
142 case SMC_PEERFINCLOSEWAIT:
Ursula Braun51f1de72018-01-26 09:28:48 +0100143 sock_put(sk); /* passive closing */
144 break;
Ursula Braundd65d872018-09-18 15:46:36 +0200145 case SMC_INIT:
Ursula Braunb38d7322017-01-09 16:55:25 +0100146 case SMC_PEERABORTWAIT:
147 case SMC_CLOSED:
148 break;
149 }
150
Ursula Braun3163c502018-01-24 10:28:12 +0100151 sock_set_flag(sk, SOCK_DEAD);
152 sk->sk_state_change(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100153}
154
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200155static inline bool smc_close_sent_any_close(struct smc_connection *conn)
156{
157 return conn->local_tx_ctrl.conn_state_flags.peer_conn_abort ||
158 conn->local_tx_ctrl.conn_state_flags.peer_conn_closed;
159}
160
Ursula Braunb38d7322017-01-09 16:55:25 +0100161int smc_close_active(struct smc_sock *smc)
162{
163 struct smc_cdc_conn_state_flags *txflags =
164 &smc->conn.local_tx_ctrl.conn_state_flags;
Ursula Braunb38d7322017-01-09 16:55:25 +0100165 struct smc_connection *conn = &smc->conn;
166 struct sock *sk = &smc->sk;
167 int old_state;
Ursula Braun8c96fee2017-09-21 09:16:34 +0200168 long timeout;
Ursula Braunb38d7322017-01-09 16:55:25 +0100169 int rc = 0;
170
Ursula Braun8c96fee2017-09-21 09:16:34 +0200171 timeout = current->flags & PF_EXITING ?
172 0 : sock_flag(sk, SOCK_LINGER) ?
173 sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braunb38d7322017-01-09 16:55:25 +0100174
Ursula Braunb38d7322017-01-09 16:55:25 +0100175 old_state = sk->sk_state;
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100176again:
177 switch (sk->sk_state) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100178 case SMC_INIT:
179 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100180 break;
181 case SMC_LISTEN:
182 sk->sk_state = SMC_CLOSED;
183 sk->sk_state_change(sk); /* wake up accept */
184 if (smc->clcsock && smc->clcsock->sk) {
185 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
186 /* wake up kernel_accept of smc_tcp_listen_worker */
187 smc->clcsock->sk->sk_data_ready(smc->clcsock->sk);
188 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100189 smc_close_cleanup_listen(sk);
Ursula Braun3d502062018-03-13 10:41:54 +0100190 release_sock(sk);
191 flush_work(&smc->tcp_listen_work);
192 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100193 break;
194 case SMC_ACTIVE:
195 smc_close_stream_wait(smc, timeout);
196 release_sock(sk);
Ursula Braun18e537c2017-09-21 09:16:33 +0200197 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100198 lock_sock(sk);
199 if (sk->sk_state == SMC_ACTIVE) {
200 /* send close request */
201 rc = smc_close_final(conn);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100202 if (rc)
203 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100204 sk->sk_state = SMC_PEERCLOSEWAIT1;
205 } else {
206 /* peer event has changed the state */
207 goto again;
208 }
209 break;
210 case SMC_APPFINCLOSEWAIT:
211 /* socket already shutdown wr or both (active close) */
212 if (txflags->peer_done_writing &&
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200213 !smc_close_sent_any_close(conn)) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100214 /* just shutdown wr done, send close request */
215 rc = smc_close_final(conn);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100216 if (rc)
217 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100218 }
219 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100220 break;
221 case SMC_APPCLOSEWAIT1:
222 case SMC_APPCLOSEWAIT2:
223 if (!smc_cdc_rxed_any_close(conn))
224 smc_close_stream_wait(smc, timeout);
225 release_sock(sk);
Ursula Braun18e537c2017-09-21 09:16:33 +0200226 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100227 lock_sock(sk);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100228 if (sk->sk_state != SMC_APPCLOSEWAIT1 &&
229 sk->sk_state != SMC_APPCLOSEWAIT2)
230 goto again;
231 /* confirm close from peer */
232 rc = smc_close_final(conn);
233 if (rc)
234 break;
Ursula Braun51f1de72018-01-26 09:28:48 +0100235 if (smc_cdc_rxed_any_close(conn)) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100236 /* peer has closed the socket already */
237 sk->sk_state = SMC_CLOSED;
Ursula Braun51f1de72018-01-26 09:28:48 +0100238 sock_put(sk); /* postponed passive closing */
239 } else {
Ursula Braunb38d7322017-01-09 16:55:25 +0100240 /* peer has just issued a shutdown write */
241 sk->sk_state = SMC_PEERFINCLOSEWAIT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100242 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100243 break;
244 case SMC_PEERCLOSEWAIT1:
245 case SMC_PEERCLOSEWAIT2:
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200246 if (txflags->peer_done_writing &&
247 !smc_close_sent_any_close(conn)) {
248 /* just shutdown wr done, send close request */
249 rc = smc_close_final(conn);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100250 if (rc)
251 break;
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200252 }
253 /* peer sending PeerConnectionClosed will cause transition */
254 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100255 case SMC_PEERFINCLOSEWAIT:
256 /* peer sending PeerConnectionClosed will cause transition */
257 break;
258 case SMC_PROCESSABORT:
Ursula Braunb38d7322017-01-09 16:55:25 +0100259 smc_close_abort(conn);
260 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100261 break;
262 case SMC_PEERABORTWAIT:
263 case SMC_CLOSED:
264 /* nothing to do, add tracing in future patch */
265 break;
266 }
267
268 if (old_state != sk->sk_state)
Ursula Braun3163c502018-01-24 10:28:12 +0100269 sk->sk_state_change(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100270 return rc;
271}
272
273static void smc_close_passive_abort_received(struct smc_sock *smc)
274{
275 struct smc_cdc_conn_state_flags *txflags =
276 &smc->conn.local_tx_ctrl.conn_state_flags;
277 struct sock *sk = &smc->sk;
278
279 switch (sk->sk_state) {
Ursula Braun51f1de72018-01-26 09:28:48 +0100280 case SMC_INIT:
Ursula Braunb38d7322017-01-09 16:55:25 +0100281 case SMC_ACTIVE:
Ursula Braunb38d7322017-01-09 16:55:25 +0100282 case SMC_APPCLOSEWAIT1:
Ursula Braun51f1de72018-01-26 09:28:48 +0100283 sk->sk_state = SMC_PROCESSABORT;
284 sock_put(sk); /* passive closing */
285 break;
286 case SMC_APPFINCLOSEWAIT:
Ursula Braunb38d7322017-01-09 16:55:25 +0100287 sk->sk_state = SMC_PROCESSABORT;
288 break;
289 case SMC_PEERCLOSEWAIT1:
290 case SMC_PEERCLOSEWAIT2:
291 if (txflags->peer_done_writing &&
Ursula Braun51f1de72018-01-26 09:28:48 +0100292 !smc_close_sent_any_close(&smc->conn))
Ursula Braunb38d7322017-01-09 16:55:25 +0100293 /* just shutdown, but not yet closed locally */
Ursula Braunb38d7322017-01-09 16:55:25 +0100294 sk->sk_state = SMC_PROCESSABORT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100295 else
Ursula Braunb38d7322017-01-09 16:55:25 +0100296 sk->sk_state = SMC_CLOSED;
Ursula Braun51f1de72018-01-26 09:28:48 +0100297 sock_put(sk); /* passive closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100298 break;
Ursula Braun51f1de72018-01-26 09:28:48 +0100299 case SMC_APPCLOSEWAIT2:
Ursula Braunb38d7322017-01-09 16:55:25 +0100300 case SMC_PEERFINCLOSEWAIT:
Ursula Braun51f1de72018-01-26 09:28:48 +0100301 sk->sk_state = SMC_CLOSED;
302 sock_put(sk); /* passive closing */
303 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100304 case SMC_PEERABORTWAIT:
305 sk->sk_state = SMC_CLOSED;
306 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100307 case SMC_PROCESSABORT:
308 /* nothing to do, add tracing in future patch */
309 break;
310 }
311}
312
Ursula Braun732720f2018-01-25 11:15:35 +0100313/* Either some kind of closing has been received: peer_conn_closed,
314 * peer_conn_abort, or peer_done_writing
315 * or the link group of the connection terminates abnormally.
Ursula Braunb38d7322017-01-09 16:55:25 +0100316 */
Ursula Braun46c28db2017-04-10 14:58:01 +0200317static void smc_close_passive_work(struct work_struct *work)
Ursula Braunb38d7322017-01-09 16:55:25 +0100318{
Ursula Braun46c28db2017-04-10 14:58:01 +0200319 struct smc_connection *conn = container_of(work,
320 struct smc_connection,
321 close_work);
322 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
323 struct smc_cdc_conn_state_flags *rxflags;
Ursula Braunb38d7322017-01-09 16:55:25 +0100324 struct sock *sk = &smc->sk;
325 int old_state;
326
Ursula Braun3163c502018-01-24 10:28:12 +0100327 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100328 old_state = sk->sk_state;
329
Ursula Braun46c28db2017-04-10 14:58:01 +0200330 if (!conn->alert_token_local) {
331 /* abnormal termination */
332 smc_close_active_abort(smc);
333 goto wakeup;
334 }
335
Ursula Braun3163c502018-01-24 10:28:12 +0100336 rxflags = &conn->local_rx_ctrl.conn_state_flags;
Ursula Braunb38d7322017-01-09 16:55:25 +0100337 if (rxflags->peer_conn_abort) {
Ursula Braun732720f2018-01-25 11:15:35 +0100338 /* peer has not received all data */
Ursula Braunb38d7322017-01-09 16:55:25 +0100339 smc_close_passive_abort_received(smc);
Ursula Braun611b63a2018-01-25 11:15:31 +0100340 release_sock(&smc->sk);
341 cancel_delayed_work_sync(&conn->tx_work);
342 lock_sock(&smc->sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100343 goto wakeup;
344 }
345
346 switch (sk->sk_state) {
347 case SMC_INIT:
Ursula Braun84b799a2019-02-04 13:44:47 +0100348 sk->sk_state = SMC_APPCLOSEWAIT1;
Ursula Braunb38d7322017-01-09 16:55:25 +0100349 break;
350 case SMC_ACTIVE:
351 sk->sk_state = SMC_APPCLOSEWAIT1;
Ursula Braun51f1de72018-01-26 09:28:48 +0100352 /* postpone sock_put() for passive closing to cover
353 * received SEND_SHUTDOWN as well
354 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100355 break;
356 case SMC_PEERCLOSEWAIT1:
357 if (rxflags->peer_done_writing)
358 sk->sk_state = SMC_PEERCLOSEWAIT2;
Gustavo A. R. Silva7f6b4372017-10-21 20:35:30 -0500359 /* fall through */
360 /* to check for closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100361 case SMC_PEERCLOSEWAIT2:
Ursula Braun3163c502018-01-24 10:28:12 +0100362 if (!smc_cdc_rxed_any_close(conn))
Ursula Braunb38d7322017-01-09 16:55:25 +0100363 break;
364 if (sock_flag(sk, SOCK_DEAD) &&
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200365 smc_close_sent_any_close(conn)) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100366 /* smc_release has already been called locally */
367 sk->sk_state = SMC_CLOSED;
368 } else {
369 /* just shutdown, but not yet closed locally */
370 sk->sk_state = SMC_APPFINCLOSEWAIT;
371 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100372 sock_put(sk); /* passive closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100373 break;
Ursula Braun5ac92a02018-01-25 11:15:32 +0100374 case SMC_PEERFINCLOSEWAIT:
Ursula Braun51f1de72018-01-26 09:28:48 +0100375 if (smc_cdc_rxed_any_close(conn)) {
Ursula Braun5ac92a02018-01-25 11:15:32 +0100376 sk->sk_state = SMC_CLOSED;
Ursula Braun51f1de72018-01-26 09:28:48 +0100377 sock_put(sk); /* passive closing */
378 }
Ursula Braun5ac92a02018-01-25 11:15:32 +0100379 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100380 case SMC_APPCLOSEWAIT1:
381 case SMC_APPCLOSEWAIT2:
Ursula Braun51f1de72018-01-26 09:28:48 +0100382 /* postpone sock_put() for passive closing to cover
383 * received SEND_SHUTDOWN as well
384 */
385 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100386 case SMC_APPFINCLOSEWAIT:
387 case SMC_PEERABORTWAIT:
388 case SMC_PROCESSABORT:
389 case SMC_CLOSED:
390 /* nothing to do, add tracing in future patch */
391 break;
392 }
393
394wakeup:
Ursula Braunb38d7322017-01-09 16:55:25 +0100395 sk->sk_data_ready(sk); /* wakeup blocked rcvbuf consumers */
396 sk->sk_write_space(sk); /* wakeup blocked sndbuf producers */
397
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200398 if (old_state != sk->sk_state) {
399 sk->sk_state_change(sk);
400 if ((sk->sk_state == SMC_CLOSED) &&
Ursula Braunb03faa12019-02-07 15:56:15 +0100401 (sock_flag(sk, SOCK_DEAD) || !sk->sk_socket)) {
Ursula Braun3163c502018-01-24 10:28:12 +0100402 smc_conn_free(conn);
Ursula Braunb03faa12019-02-07 15:56:15 +0100403 if (smc->clcsock) {
404 sock_release(smc->clcsock);
405 smc->clcsock = NULL;
406 }
407 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100408 }
Ursula Braun3163c502018-01-24 10:28:12 +0100409 release_sock(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100410 sock_put(sk); /* sock_hold done by schedulers of close_work */
Ursula Braunb38d7322017-01-09 16:55:25 +0100411}
412
413int smc_close_shutdown_write(struct smc_sock *smc)
414{
415 struct smc_connection *conn = &smc->conn;
Ursula Braunb38d7322017-01-09 16:55:25 +0100416 struct sock *sk = &smc->sk;
417 int old_state;
Ursula Braun8c96fee2017-09-21 09:16:34 +0200418 long timeout;
Ursula Braunb38d7322017-01-09 16:55:25 +0100419 int rc = 0;
420
Ursula Braun8c96fee2017-09-21 09:16:34 +0200421 timeout = current->flags & PF_EXITING ?
422 0 : sock_flag(sk, SOCK_LINGER) ?
423 sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braunb38d7322017-01-09 16:55:25 +0100424
Ursula Braunb38d7322017-01-09 16:55:25 +0100425 old_state = sk->sk_state;
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100426again:
427 switch (sk->sk_state) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100428 case SMC_ACTIVE:
429 smc_close_stream_wait(smc, timeout);
430 release_sock(sk);
Ursula Braun18e537c2017-09-21 09:16:33 +0200431 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100432 lock_sock(sk);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100433 if (sk->sk_state != SMC_ACTIVE)
434 goto again;
Ursula Braunb38d7322017-01-09 16:55:25 +0100435 /* send close wr request */
436 rc = smc_close_wr(conn);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100437 if (rc)
438 break;
439 sk->sk_state = SMC_PEERCLOSEWAIT1;
Ursula Braunb38d7322017-01-09 16:55:25 +0100440 break;
441 case SMC_APPCLOSEWAIT1:
442 /* passive close */
443 if (!smc_cdc_rxed_any_close(conn))
444 smc_close_stream_wait(smc, timeout);
445 release_sock(sk);
Ursula Braun18e537c2017-09-21 09:16:33 +0200446 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100447 lock_sock(sk);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100448 if (sk->sk_state != SMC_APPCLOSEWAIT1)
449 goto again;
Ursula Braunb38d7322017-01-09 16:55:25 +0100450 /* confirm close from peer */
451 rc = smc_close_wr(conn);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100452 if (rc)
453 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100454 sk->sk_state = SMC_APPCLOSEWAIT2;
455 break;
456 case SMC_APPCLOSEWAIT2:
457 case SMC_PEERFINCLOSEWAIT:
458 case SMC_PEERCLOSEWAIT1:
459 case SMC_PEERCLOSEWAIT2:
460 case SMC_APPFINCLOSEWAIT:
461 case SMC_PROCESSABORT:
462 case SMC_PEERABORTWAIT:
463 /* nothing to do, add tracing in future patch */
464 break;
465 }
466
467 if (old_state != sk->sk_state)
Ursula Braun3163c502018-01-24 10:28:12 +0100468 sk->sk_state_change(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100469 return rc;
470}
Ursula Braun46c28db2017-04-10 14:58:01 +0200471
472/* Initialize close properties on connection establishment. */
473void smc_close_init(struct smc_sock *smc)
474{
475 INIT_WORK(&smc->conn.close_work, smc_close_passive_work);
476}