blob: e339c0186dcfc2990d50c0b733e050a10f24b661 [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 Braun127f4972018-01-26 09:28:49 +010033static void smc_close_wait_listen_clcsock(struct smc_sock *smc)
34{
35 DEFINE_WAIT_FUNC(wait, woken_wake_function);
36 struct sock *sk = &smc->sk;
37 signed long timeout;
38
39 timeout = SMC_CLOSE_WAIT_LISTEN_CLCSOCK_TIME;
40 add_wait_queue(sk_sleep(sk), &wait);
41 do {
42 release_sock(sk);
43 if (smc->clcsock)
44 timeout = wait_woken(&wait, TASK_UNINTERRUPTIBLE,
45 timeout);
46 sched_annotate_sleep();
47 lock_sock(sk);
48 if (!smc->clcsock)
49 break;
50 } while (timeout);
51 remove_wait_queue(sk_sleep(sk), &wait);
52}
53
Ursula Braunb38d7322017-01-09 16:55:25 +010054/* wait for sndbuf data being transmitted */
55static void smc_close_stream_wait(struct smc_sock *smc, long timeout)
56{
57 DEFINE_WAIT_FUNC(wait, woken_wake_function);
58 struct sock *sk = &smc->sk;
59
60 if (!timeout)
61 return;
62
63 if (!smc_tx_prepared_sends(&smc->conn))
64 return;
65
66 smc->wait_close_tx_prepared = 1;
67 add_wait_queue(sk_sleep(sk), &wait);
68 while (!signal_pending(current) && timeout) {
69 int rc;
70
71 rc = sk_wait_event(sk, &timeout,
72 !smc_tx_prepared_sends(&smc->conn) ||
73 (sk->sk_err == ECONNABORTED) ||
74 (sk->sk_err == ECONNRESET),
75 &wait);
76 if (rc)
77 break;
78 }
79 remove_wait_queue(sk_sleep(sk), &wait);
80 smc->wait_close_tx_prepared = 0;
81}
82
83void smc_close_wake_tx_prepared(struct smc_sock *smc)
84{
85 if (smc->wait_close_tx_prepared)
86 /* wake up socket closing */
87 smc->sk.sk_state_change(&smc->sk);
88}
89
90static int smc_close_wr(struct smc_connection *conn)
91{
92 conn->local_tx_ctrl.conn_state_flags.peer_done_writing = 1;
93
94 return smc_cdc_get_slot_and_msg_send(conn);
95}
96
97static int smc_close_final(struct smc_connection *conn)
98{
99 if (atomic_read(&conn->bytes_to_rcv))
100 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
101 else
102 conn->local_tx_ctrl.conn_state_flags.peer_conn_closed = 1;
103
104 return smc_cdc_get_slot_and_msg_send(conn);
105}
106
107static int smc_close_abort(struct smc_connection *conn)
108{
109 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
110
111 return smc_cdc_get_slot_and_msg_send(conn);
112}
113
114/* terminate smc socket abnormally - active abort
Ursula Braun732720f2018-01-25 11:15:35 +0100115 * link group is terminated, i.e. RDMA communication no longer possible
Ursula Braunb38d7322017-01-09 16:55:25 +0100116 */
Ursula Brauna8ae8902017-12-07 13:38:44 +0100117static void smc_close_active_abort(struct smc_sock *smc)
Ursula Braunb38d7322017-01-09 16:55:25 +0100118{
Ursula Braun3163c502018-01-24 10:28:12 +0100119 struct sock *sk = &smc->sk;
120
Ursula Braunb38d7322017-01-09 16:55:25 +0100121 struct smc_cdc_conn_state_flags *txflags =
122 &smc->conn.local_tx_ctrl.conn_state_flags;
123
Ursula Braun3163c502018-01-24 10:28:12 +0100124 sk->sk_err = ECONNABORTED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100125 if (smc->clcsock && smc->clcsock->sk) {
126 smc->clcsock->sk->sk_err = ECONNABORTED;
127 smc->clcsock->sk->sk_state_change(smc->clcsock->sk);
128 }
Ursula Braun3163c502018-01-24 10:28:12 +0100129 switch (sk->sk_state) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100130 case SMC_INIT:
Ursula Braun46c28db2017-04-10 14:58:01 +0200131 case SMC_ACTIVE:
Ursula Braun3163c502018-01-24 10:28:12 +0100132 sk->sk_state = SMC_PEERABORTWAIT;
Ursula Braun611b63a2018-01-25 11:15:31 +0100133 release_sock(sk);
134 cancel_delayed_work_sync(&smc->conn.tx_work);
135 lock_sock(sk);
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_APPCLOSEWAIT1:
139 case SMC_APPCLOSEWAIT2:
Ursula Braunb38d7322017-01-09 16:55:25 +0100140 if (!smc_cdc_rxed_any_close(&smc->conn))
Ursula Braun3163c502018-01-24 10:28:12 +0100141 sk->sk_state = SMC_PEERABORTWAIT;
Ursula Braunb38d7322017-01-09 16:55:25 +0100142 else
Ursula Braun3163c502018-01-24 10:28:12 +0100143 sk->sk_state = SMC_CLOSED;
Ursula Braun611b63a2018-01-25 11:15:31 +0100144 release_sock(sk);
145 cancel_delayed_work_sync(&smc->conn.tx_work);
146 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100147 break;
148 case SMC_PEERCLOSEWAIT1:
149 case SMC_PEERCLOSEWAIT2:
150 if (!txflags->peer_conn_closed) {
Ursula Braun51f1de72018-01-26 09:28:48 +0100151 /* just SHUTDOWN_SEND done */
Ursula Braun3163c502018-01-24 10:28:12 +0100152 sk->sk_state = SMC_PEERABORTWAIT;
Ursula Braunb38d7322017-01-09 16:55:25 +0100153 } else {
Ursula Braun3163c502018-01-24 10:28:12 +0100154 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100155 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100156 sock_put(sk); /* passive closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100157 break;
158 case SMC_PROCESSABORT:
159 case SMC_APPFINCLOSEWAIT:
Ursula Braun3163c502018-01-24 10:28:12 +0100160 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100161 break;
162 case SMC_PEERFINCLOSEWAIT:
Ursula Braun51f1de72018-01-26 09:28:48 +0100163 sock_put(sk); /* passive closing */
164 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100165 case SMC_PEERABORTWAIT:
166 case SMC_CLOSED:
167 break;
168 }
169
Ursula Braun3163c502018-01-24 10:28:12 +0100170 sock_set_flag(sk, SOCK_DEAD);
171 sk->sk_state_change(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100172}
173
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200174static inline bool smc_close_sent_any_close(struct smc_connection *conn)
175{
176 return conn->local_tx_ctrl.conn_state_flags.peer_conn_abort ||
177 conn->local_tx_ctrl.conn_state_flags.peer_conn_closed;
178}
179
Ursula Braunb38d7322017-01-09 16:55:25 +0100180int smc_close_active(struct smc_sock *smc)
181{
182 struct smc_cdc_conn_state_flags *txflags =
183 &smc->conn.local_tx_ctrl.conn_state_flags;
Ursula Braunb38d7322017-01-09 16:55:25 +0100184 struct smc_connection *conn = &smc->conn;
185 struct sock *sk = &smc->sk;
186 int old_state;
Ursula Braun8c96fee2017-09-21 09:16:34 +0200187 long timeout;
Ursula Braunb38d7322017-01-09 16:55:25 +0100188 int rc = 0;
189
Ursula Braun8c96fee2017-09-21 09:16:34 +0200190 timeout = current->flags & PF_EXITING ?
191 0 : sock_flag(sk, SOCK_LINGER) ?
192 sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braunb38d7322017-01-09 16:55:25 +0100193
Ursula Braunb38d7322017-01-09 16:55:25 +0100194 old_state = sk->sk_state;
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100195again:
196 switch (sk->sk_state) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100197 case SMC_INIT:
198 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100199 break;
200 case SMC_LISTEN:
201 sk->sk_state = SMC_CLOSED;
202 sk->sk_state_change(sk); /* wake up accept */
203 if (smc->clcsock && smc->clcsock->sk) {
204 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
205 /* wake up kernel_accept of smc_tcp_listen_worker */
206 smc->clcsock->sk->sk_data_ready(smc->clcsock->sk);
Ursula Braun127f4972018-01-26 09:28:49 +0100207 smc_close_wait_listen_clcsock(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +0100208 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100209 smc_close_cleanup_listen(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100210 break;
211 case SMC_ACTIVE:
212 smc_close_stream_wait(smc, timeout);
213 release_sock(sk);
Ursula Braun18e537c2017-09-21 09:16:33 +0200214 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100215 lock_sock(sk);
216 if (sk->sk_state == SMC_ACTIVE) {
217 /* send close request */
218 rc = smc_close_final(conn);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100219 if (rc)
220 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100221 sk->sk_state = SMC_PEERCLOSEWAIT1;
222 } else {
223 /* peer event has changed the state */
224 goto again;
225 }
226 break;
227 case SMC_APPFINCLOSEWAIT:
228 /* socket already shutdown wr or both (active close) */
229 if (txflags->peer_done_writing &&
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200230 !smc_close_sent_any_close(conn)) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100231 /* just shutdown wr done, send close request */
232 rc = smc_close_final(conn);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100233 if (rc)
234 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100235 }
236 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100237 break;
238 case SMC_APPCLOSEWAIT1:
239 case SMC_APPCLOSEWAIT2:
240 if (!smc_cdc_rxed_any_close(conn))
241 smc_close_stream_wait(smc, timeout);
242 release_sock(sk);
Ursula Braun18e537c2017-09-21 09:16:33 +0200243 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100244 lock_sock(sk);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100245 if (sk->sk_state != SMC_APPCLOSEWAIT1 &&
246 sk->sk_state != SMC_APPCLOSEWAIT2)
247 goto again;
248 /* confirm close from peer */
249 rc = smc_close_final(conn);
250 if (rc)
251 break;
Ursula Braun51f1de72018-01-26 09:28:48 +0100252 if (smc_cdc_rxed_any_close(conn)) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100253 /* peer has closed the socket already */
254 sk->sk_state = SMC_CLOSED;
Ursula Braun51f1de72018-01-26 09:28:48 +0100255 sock_put(sk); /* postponed passive closing */
256 } else {
Ursula Braunb38d7322017-01-09 16:55:25 +0100257 /* peer has just issued a shutdown write */
258 sk->sk_state = SMC_PEERFINCLOSEWAIT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100259 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100260 break;
261 case SMC_PEERCLOSEWAIT1:
262 case SMC_PEERCLOSEWAIT2:
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200263 if (txflags->peer_done_writing &&
264 !smc_close_sent_any_close(conn)) {
265 /* just shutdown wr done, send close request */
266 rc = smc_close_final(conn);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100267 if (rc)
268 break;
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200269 }
270 /* peer sending PeerConnectionClosed will cause transition */
271 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100272 case SMC_PEERFINCLOSEWAIT:
273 /* peer sending PeerConnectionClosed will cause transition */
274 break;
275 case SMC_PROCESSABORT:
Ursula Braunb38d7322017-01-09 16:55:25 +0100276 smc_close_abort(conn);
277 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100278 break;
279 case SMC_PEERABORTWAIT:
280 case SMC_CLOSED:
281 /* nothing to do, add tracing in future patch */
282 break;
283 }
284
285 if (old_state != sk->sk_state)
Ursula Braun3163c502018-01-24 10:28:12 +0100286 sk->sk_state_change(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100287 return rc;
288}
289
290static void smc_close_passive_abort_received(struct smc_sock *smc)
291{
292 struct smc_cdc_conn_state_flags *txflags =
293 &smc->conn.local_tx_ctrl.conn_state_flags;
294 struct sock *sk = &smc->sk;
295
296 switch (sk->sk_state) {
Ursula Braun51f1de72018-01-26 09:28:48 +0100297 case SMC_INIT:
Ursula Braunb38d7322017-01-09 16:55:25 +0100298 case SMC_ACTIVE:
Ursula Braunb38d7322017-01-09 16:55:25 +0100299 case SMC_APPCLOSEWAIT1:
Ursula Braun51f1de72018-01-26 09:28:48 +0100300 sk->sk_state = SMC_PROCESSABORT;
301 sock_put(sk); /* passive closing */
302 break;
303 case SMC_APPFINCLOSEWAIT:
Ursula Braunb38d7322017-01-09 16:55:25 +0100304 sk->sk_state = SMC_PROCESSABORT;
305 break;
306 case SMC_PEERCLOSEWAIT1:
307 case SMC_PEERCLOSEWAIT2:
308 if (txflags->peer_done_writing &&
Ursula Braun51f1de72018-01-26 09:28:48 +0100309 !smc_close_sent_any_close(&smc->conn))
Ursula Braunb38d7322017-01-09 16:55:25 +0100310 /* just shutdown, but not yet closed locally */
Ursula Braunb38d7322017-01-09 16:55:25 +0100311 sk->sk_state = SMC_PROCESSABORT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100312 else
Ursula Braunb38d7322017-01-09 16:55:25 +0100313 sk->sk_state = SMC_CLOSED;
Ursula Braun51f1de72018-01-26 09:28:48 +0100314 sock_put(sk); /* passive closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100315 break;
Ursula Braun51f1de72018-01-26 09:28:48 +0100316 case SMC_APPCLOSEWAIT2:
Ursula Braunb38d7322017-01-09 16:55:25 +0100317 case SMC_PEERFINCLOSEWAIT:
Ursula Braun51f1de72018-01-26 09:28:48 +0100318 sk->sk_state = SMC_CLOSED;
319 sock_put(sk); /* passive closing */
320 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100321 case SMC_PEERABORTWAIT:
322 sk->sk_state = SMC_CLOSED;
323 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100324 case SMC_PROCESSABORT:
325 /* nothing to do, add tracing in future patch */
326 break;
327 }
328}
329
Ursula Braun732720f2018-01-25 11:15:35 +0100330/* Either some kind of closing has been received: peer_conn_closed,
331 * peer_conn_abort, or peer_done_writing
332 * or the link group of the connection terminates abnormally.
Ursula Braunb38d7322017-01-09 16:55:25 +0100333 */
Ursula Braun46c28db2017-04-10 14:58:01 +0200334static void smc_close_passive_work(struct work_struct *work)
Ursula Braunb38d7322017-01-09 16:55:25 +0100335{
Ursula Braun46c28db2017-04-10 14:58:01 +0200336 struct smc_connection *conn = container_of(work,
337 struct smc_connection,
338 close_work);
339 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
340 struct smc_cdc_conn_state_flags *rxflags;
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 Braun3163c502018-01-24 10:28:12 +0100365 if (atomic_read(&conn->bytes_to_rcv) ||
Ursula Braunb38d7322017-01-09 16:55:25 +0100366 (rxflags->peer_done_writing &&
Ursula Braun51f1de72018-01-26 09:28:48 +0100367 !smc_cdc_rxed_any_close(conn))) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100368 sk->sk_state = SMC_APPCLOSEWAIT1;
Ursula Braun51f1de72018-01-26 09:28:48 +0100369 } else {
Ursula Braunb38d7322017-01-09 16:55:25 +0100370 sk->sk_state = SMC_CLOSED;
Ursula Braun51f1de72018-01-26 09:28:48 +0100371 sock_put(sk); /* passive closing */
372 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100373 break;
374 case SMC_ACTIVE:
375 sk->sk_state = SMC_APPCLOSEWAIT1;
Ursula Braun51f1de72018-01-26 09:28:48 +0100376 /* postpone sock_put() for passive closing to cover
377 * received SEND_SHUTDOWN as well
378 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100379 break;
380 case SMC_PEERCLOSEWAIT1:
381 if (rxflags->peer_done_writing)
382 sk->sk_state = SMC_PEERCLOSEWAIT2;
Gustavo A. R. Silva7f6b4372017-10-21 20:35:30 -0500383 /* fall through */
384 /* to check for closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100385 case SMC_PEERCLOSEWAIT2:
Ursula Braun3163c502018-01-24 10:28:12 +0100386 if (!smc_cdc_rxed_any_close(conn))
Ursula Braunb38d7322017-01-09 16:55:25 +0100387 break;
388 if (sock_flag(sk, SOCK_DEAD) &&
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200389 smc_close_sent_any_close(conn)) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100390 /* smc_release has already been called locally */
391 sk->sk_state = SMC_CLOSED;
392 } else {
393 /* just shutdown, but not yet closed locally */
394 sk->sk_state = SMC_APPFINCLOSEWAIT;
395 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100396 sock_put(sk); /* passive closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100397 break;
Ursula Braun5ac92a02018-01-25 11:15:32 +0100398 case SMC_PEERFINCLOSEWAIT:
Ursula Braun51f1de72018-01-26 09:28:48 +0100399 if (smc_cdc_rxed_any_close(conn)) {
Ursula Braun5ac92a02018-01-25 11:15:32 +0100400 sk->sk_state = SMC_CLOSED;
Ursula Braun51f1de72018-01-26 09:28:48 +0100401 sock_put(sk); /* passive closing */
402 }
Ursula Braun5ac92a02018-01-25 11:15:32 +0100403 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100404 case SMC_APPCLOSEWAIT1:
405 case SMC_APPCLOSEWAIT2:
Ursula Braun51f1de72018-01-26 09:28:48 +0100406 /* postpone sock_put() for passive closing to cover
407 * received SEND_SHUTDOWN as well
408 */
409 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100410 case SMC_APPFINCLOSEWAIT:
411 case SMC_PEERABORTWAIT:
412 case SMC_PROCESSABORT:
413 case SMC_CLOSED:
414 /* nothing to do, add tracing in future patch */
415 break;
416 }
417
418wakeup:
Ursula Braunb38d7322017-01-09 16:55:25 +0100419 sk->sk_data_ready(sk); /* wakeup blocked rcvbuf consumers */
420 sk->sk_write_space(sk); /* wakeup blocked sndbuf producers */
421
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200422 if (old_state != sk->sk_state) {
423 sk->sk_state_change(sk);
424 if ((sk->sk_state == SMC_CLOSED) &&
Ursula Braun51f1de72018-01-26 09:28:48 +0100425 (sock_flag(sk, SOCK_DEAD) || !sk->sk_socket))
Ursula Braun3163c502018-01-24 10:28:12 +0100426 smc_conn_free(conn);
Ursula Braunb38d7322017-01-09 16:55:25 +0100427 }
Ursula Braun3163c502018-01-24 10:28:12 +0100428 release_sock(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100429 sock_put(sk); /* sock_hold done by schedulers of close_work */
Ursula Braunb38d7322017-01-09 16:55:25 +0100430}
431
432int smc_close_shutdown_write(struct smc_sock *smc)
433{
434 struct smc_connection *conn = &smc->conn;
Ursula Braunb38d7322017-01-09 16:55:25 +0100435 struct sock *sk = &smc->sk;
436 int old_state;
Ursula Braun8c96fee2017-09-21 09:16:34 +0200437 long timeout;
Ursula Braunb38d7322017-01-09 16:55:25 +0100438 int rc = 0;
439
Ursula Braun8c96fee2017-09-21 09:16:34 +0200440 timeout = current->flags & PF_EXITING ?
441 0 : sock_flag(sk, SOCK_LINGER) ?
442 sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braunb38d7322017-01-09 16:55:25 +0100443
Ursula Braunb38d7322017-01-09 16:55:25 +0100444 old_state = sk->sk_state;
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100445again:
446 switch (sk->sk_state) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100447 case SMC_ACTIVE:
448 smc_close_stream_wait(smc, timeout);
449 release_sock(sk);
Ursula Braun18e537c2017-09-21 09:16:33 +0200450 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100451 lock_sock(sk);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100452 if (sk->sk_state != SMC_ACTIVE)
453 goto again;
Ursula Braunb38d7322017-01-09 16:55:25 +0100454 /* send close wr request */
455 rc = smc_close_wr(conn);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100456 if (rc)
457 break;
458 sk->sk_state = SMC_PEERCLOSEWAIT1;
Ursula Braunb38d7322017-01-09 16:55:25 +0100459 break;
460 case SMC_APPCLOSEWAIT1:
461 /* passive close */
462 if (!smc_cdc_rxed_any_close(conn))
463 smc_close_stream_wait(smc, timeout);
464 release_sock(sk);
Ursula Braun18e537c2017-09-21 09:16:33 +0200465 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100466 lock_sock(sk);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100467 if (sk->sk_state != SMC_APPCLOSEWAIT1)
468 goto again;
Ursula Braunb38d7322017-01-09 16:55:25 +0100469 /* confirm close from peer */
470 rc = smc_close_wr(conn);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100471 if (rc)
472 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100473 sk->sk_state = SMC_APPCLOSEWAIT2;
474 break;
475 case SMC_APPCLOSEWAIT2:
476 case SMC_PEERFINCLOSEWAIT:
477 case SMC_PEERCLOSEWAIT1:
478 case SMC_PEERCLOSEWAIT2:
479 case SMC_APPFINCLOSEWAIT:
480 case SMC_PROCESSABORT:
481 case SMC_PEERABORTWAIT:
482 /* nothing to do, add tracing in future patch */
483 break;
484 }
485
486 if (old_state != sk->sk_state)
Ursula Braun3163c502018-01-24 10:28:12 +0100487 sk->sk_state_change(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100488 return rc;
489}
Ursula Braun46c28db2017-04-10 14:58:01 +0200490
491/* Initialize close properties on connection establishment. */
492void smc_close_init(struct smc_sock *smc)
493{
494 INIT_WORK(&smc->conn.close_work, smc_close_passive_work);
495}