blob: 1468a2a3cdf402262f1cd77fcfaad26e1b7f281d [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 Braunb38d7322017-01-09 16:55:25 +010022static void smc_close_cleanup_listen(struct sock *parent)
23{
24 struct sock *sk;
25
26 /* Close non-accepted connections */
27 while ((sk = smc_accept_dequeue(parent, NULL)))
28 smc_close_non_accepted(sk);
29}
30
Ursula Braunb38d7322017-01-09 16:55:25 +010031/* wait for sndbuf data being transmitted */
32static void smc_close_stream_wait(struct smc_sock *smc, long timeout)
33{
34 DEFINE_WAIT_FUNC(wait, woken_wake_function);
35 struct sock *sk = &smc->sk;
36
37 if (!timeout)
38 return;
39
40 if (!smc_tx_prepared_sends(&smc->conn))
41 return;
42
43 smc->wait_close_tx_prepared = 1;
44 add_wait_queue(sk_sleep(sk), &wait);
45 while (!signal_pending(current) && timeout) {
46 int rc;
47
48 rc = sk_wait_event(sk, &timeout,
49 !smc_tx_prepared_sends(&smc->conn) ||
50 (sk->sk_err == ECONNABORTED) ||
51 (sk->sk_err == ECONNRESET),
52 &wait);
53 if (rc)
54 break;
55 }
56 remove_wait_queue(sk_sleep(sk), &wait);
57 smc->wait_close_tx_prepared = 0;
58}
59
60void smc_close_wake_tx_prepared(struct smc_sock *smc)
61{
62 if (smc->wait_close_tx_prepared)
63 /* wake up socket closing */
64 smc->sk.sk_state_change(&smc->sk);
65}
66
67static int smc_close_wr(struct smc_connection *conn)
68{
69 conn->local_tx_ctrl.conn_state_flags.peer_done_writing = 1;
70
71 return smc_cdc_get_slot_and_msg_send(conn);
72}
73
74static int smc_close_final(struct smc_connection *conn)
75{
76 if (atomic_read(&conn->bytes_to_rcv))
77 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
78 else
79 conn->local_tx_ctrl.conn_state_flags.peer_conn_closed = 1;
80
81 return smc_cdc_get_slot_and_msg_send(conn);
82}
83
84static int smc_close_abort(struct smc_connection *conn)
85{
86 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
87
88 return smc_cdc_get_slot_and_msg_send(conn);
89}
90
91/* terminate smc socket abnormally - active abort
92 * RDMA communication no longer possible
93 */
Ursula Brauna8ae8902017-12-07 13:38:44 +010094static void smc_close_active_abort(struct smc_sock *smc)
Ursula Braunb38d7322017-01-09 16:55:25 +010095{
Ursula Braun3163c502018-01-24 10:28:12 +010096 struct sock *sk = &smc->sk;
97
Ursula Braunb38d7322017-01-09 16:55:25 +010098 struct smc_cdc_conn_state_flags *txflags =
99 &smc->conn.local_tx_ctrl.conn_state_flags;
100
Ursula Braun3163c502018-01-24 10:28:12 +0100101 sk->sk_err = ECONNABORTED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100102 if (smc->clcsock && smc->clcsock->sk) {
103 smc->clcsock->sk->sk_err = ECONNABORTED;
104 smc->clcsock->sk->sk_state_change(smc->clcsock->sk);
105 }
Ursula Braun3163c502018-01-24 10:28:12 +0100106 switch (sk->sk_state) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100107 case SMC_INIT:
Ursula Braun46c28db2017-04-10 14:58:01 +0200108 case SMC_ACTIVE:
Ursula Braun3163c502018-01-24 10:28:12 +0100109 sk->sk_state = SMC_PEERABORTWAIT;
Ursula Braunb38d7322017-01-09 16:55:25 +0100110 break;
111 case SMC_APPCLOSEWAIT1:
112 case SMC_APPCLOSEWAIT2:
113 txflags->peer_conn_abort = 1;
114 sock_release(smc->clcsock);
115 if (!smc_cdc_rxed_any_close(&smc->conn))
Ursula Braun3163c502018-01-24 10:28:12 +0100116 sk->sk_state = SMC_PEERABORTWAIT;
Ursula Braunb38d7322017-01-09 16:55:25 +0100117 else
Ursula Braun3163c502018-01-24 10:28:12 +0100118 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100119 break;
120 case SMC_PEERCLOSEWAIT1:
121 case SMC_PEERCLOSEWAIT2:
122 if (!txflags->peer_conn_closed) {
Ursula Braun3163c502018-01-24 10:28:12 +0100123 sk->sk_state = SMC_PEERABORTWAIT;
Ursula Braunb38d7322017-01-09 16:55:25 +0100124 txflags->peer_conn_abort = 1;
125 sock_release(smc->clcsock);
126 } else {
Ursula Braun3163c502018-01-24 10:28:12 +0100127 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100128 }
129 break;
130 case SMC_PROCESSABORT:
131 case SMC_APPFINCLOSEWAIT:
132 if (!txflags->peer_conn_closed) {
133 txflags->peer_conn_abort = 1;
134 sock_release(smc->clcsock);
135 }
Ursula Braun3163c502018-01-24 10:28:12 +0100136 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100137 break;
138 case SMC_PEERFINCLOSEWAIT:
139 case SMC_PEERABORTWAIT:
140 case SMC_CLOSED:
141 break;
142 }
143
Ursula Braun3163c502018-01-24 10:28:12 +0100144 sock_set_flag(sk, SOCK_DEAD);
145 sk->sk_state_change(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100146}
147
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200148static inline bool smc_close_sent_any_close(struct smc_connection *conn)
149{
150 return conn->local_tx_ctrl.conn_state_flags.peer_conn_abort ||
151 conn->local_tx_ctrl.conn_state_flags.peer_conn_closed;
152}
153
Ursula Braunb38d7322017-01-09 16:55:25 +0100154int smc_close_active(struct smc_sock *smc)
155{
156 struct smc_cdc_conn_state_flags *txflags =
157 &smc->conn.local_tx_ctrl.conn_state_flags;
Ursula Braunb38d7322017-01-09 16:55:25 +0100158 struct smc_connection *conn = &smc->conn;
159 struct sock *sk = &smc->sk;
160 int old_state;
Ursula Braun8c96fee2017-09-21 09:16:34 +0200161 long timeout;
Ursula Braunb38d7322017-01-09 16:55:25 +0100162 int rc = 0;
163
Ursula Braun8c96fee2017-09-21 09:16:34 +0200164 timeout = current->flags & PF_EXITING ?
165 0 : sock_flag(sk, SOCK_LINGER) ?
166 sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braunb38d7322017-01-09 16:55:25 +0100167
Ursula Braunb38d7322017-01-09 16:55:25 +0100168 old_state = sk->sk_state;
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100169again:
170 switch (sk->sk_state) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100171 case SMC_INIT:
172 sk->sk_state = SMC_CLOSED;
173 if (smc->smc_listen_work.func)
Ursula Braun46c28db2017-04-10 14:58:01 +0200174 cancel_work_sync(&smc->smc_listen_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100175 break;
176 case SMC_LISTEN:
177 sk->sk_state = SMC_CLOSED;
178 sk->sk_state_change(sk); /* wake up accept */
179 if (smc->clcsock && smc->clcsock->sk) {
180 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
181 /* wake up kernel_accept of smc_tcp_listen_worker */
182 smc->clcsock->sk->sk_data_ready(smc->clcsock->sk);
183 }
184 release_sock(sk);
185 smc_close_cleanup_listen(sk);
Ursula Braun46c28db2017-04-10 14:58:01 +0200186 cancel_work_sync(&smc->smc_listen_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100187 lock_sock(sk);
188 break;
189 case SMC_ACTIVE:
190 smc_close_stream_wait(smc, timeout);
191 release_sock(sk);
Ursula Braun18e537c2017-09-21 09:16:33 +0200192 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100193 lock_sock(sk);
194 if (sk->sk_state == SMC_ACTIVE) {
195 /* send close request */
196 rc = smc_close_final(conn);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100197 if (rc)
198 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100199 sk->sk_state = SMC_PEERCLOSEWAIT1;
200 } else {
201 /* peer event has changed the state */
202 goto again;
203 }
204 break;
205 case SMC_APPFINCLOSEWAIT:
206 /* socket already shutdown wr or both (active close) */
207 if (txflags->peer_done_writing &&
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200208 !smc_close_sent_any_close(conn)) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100209 /* just shutdown wr done, send close request */
210 rc = smc_close_final(conn);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100211 if (rc)
212 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100213 }
214 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100215 break;
216 case SMC_APPCLOSEWAIT1:
217 case SMC_APPCLOSEWAIT2:
218 if (!smc_cdc_rxed_any_close(conn))
219 smc_close_stream_wait(smc, timeout);
220 release_sock(sk);
Ursula Braun18e537c2017-09-21 09:16:33 +0200221 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100222 lock_sock(sk);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100223 if (sk->sk_state != SMC_APPCLOSEWAIT1 &&
224 sk->sk_state != SMC_APPCLOSEWAIT2)
225 goto again;
226 /* confirm close from peer */
227 rc = smc_close_final(conn);
228 if (rc)
229 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100230 if (smc_cdc_rxed_any_close(conn))
231 /* peer has closed the socket already */
232 sk->sk_state = SMC_CLOSED;
233 else
234 /* peer has just issued a shutdown write */
235 sk->sk_state = SMC_PEERFINCLOSEWAIT;
Ursula Braunb38d7322017-01-09 16:55:25 +0100236 break;
237 case SMC_PEERCLOSEWAIT1:
238 case SMC_PEERCLOSEWAIT2:
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200239 if (txflags->peer_done_writing &&
240 !smc_close_sent_any_close(conn)) {
241 /* just shutdown wr done, send close request */
242 rc = smc_close_final(conn);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100243 if (rc)
244 break;
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200245 }
246 /* peer sending PeerConnectionClosed will cause transition */
247 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100248 case SMC_PEERFINCLOSEWAIT:
249 /* peer sending PeerConnectionClosed will cause transition */
250 break;
251 case SMC_PROCESSABORT:
Ursula Braun18e537c2017-09-21 09:16:33 +0200252 release_sock(sk);
253 cancel_delayed_work_sync(&conn->tx_work);
254 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100255 smc_close_abort(conn);
256 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100257 break;
258 case SMC_PEERABORTWAIT:
259 case SMC_CLOSED:
260 /* nothing to do, add tracing in future patch */
261 break;
262 }
263
264 if (old_state != sk->sk_state)
Ursula Braun3163c502018-01-24 10:28:12 +0100265 sk->sk_state_change(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100266 return rc;
267}
268
269static void smc_close_passive_abort_received(struct smc_sock *smc)
270{
271 struct smc_cdc_conn_state_flags *txflags =
272 &smc->conn.local_tx_ctrl.conn_state_flags;
273 struct sock *sk = &smc->sk;
274
275 switch (sk->sk_state) {
276 case SMC_ACTIVE:
277 case SMC_APPFINCLOSEWAIT:
278 case SMC_APPCLOSEWAIT1:
279 case SMC_APPCLOSEWAIT2:
280 smc_close_abort(&smc->conn);
281 sk->sk_state = SMC_PROCESSABORT;
282 break;
283 case SMC_PEERCLOSEWAIT1:
284 case SMC_PEERCLOSEWAIT2:
285 if (txflags->peer_done_writing &&
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200286 !smc_close_sent_any_close(&smc->conn)) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100287 /* just shutdown, but not yet closed locally */
288 smc_close_abort(&smc->conn);
289 sk->sk_state = SMC_PROCESSABORT;
290 } else {
291 sk->sk_state = SMC_CLOSED;
292 }
293 break;
294 case SMC_PEERFINCLOSEWAIT:
295 case SMC_PEERABORTWAIT:
296 sk->sk_state = SMC_CLOSED;
297 break;
298 case SMC_INIT:
299 case SMC_PROCESSABORT:
300 /* nothing to do, add tracing in future patch */
301 break;
302 }
303}
304
305/* Some kind of closing has been received: peer_conn_closed, peer_conn_abort,
306 * or peer_done_writing.
Ursula Braunb38d7322017-01-09 16:55:25 +0100307 */
Ursula Braun46c28db2017-04-10 14:58:01 +0200308static void smc_close_passive_work(struct work_struct *work)
Ursula Braunb38d7322017-01-09 16:55:25 +0100309{
Ursula Braun46c28db2017-04-10 14:58:01 +0200310 struct smc_connection *conn = container_of(work,
311 struct smc_connection,
312 close_work);
313 struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
314 struct smc_cdc_conn_state_flags *rxflags;
Ursula Braunb38d7322017-01-09 16:55:25 +0100315 struct sock *sk = &smc->sk;
316 int old_state;
317
Ursula Braun3163c502018-01-24 10:28:12 +0100318 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100319 old_state = sk->sk_state;
320
Ursula Braun46c28db2017-04-10 14:58:01 +0200321 if (!conn->alert_token_local) {
322 /* abnormal termination */
323 smc_close_active_abort(smc);
324 goto wakeup;
325 }
326
Ursula Braun3163c502018-01-24 10:28:12 +0100327 rxflags = &conn->local_rx_ctrl.conn_state_flags;
Ursula Braunb38d7322017-01-09 16:55:25 +0100328 if (rxflags->peer_conn_abort) {
329 smc_close_passive_abort_received(smc);
330 goto wakeup;
331 }
332
333 switch (sk->sk_state) {
334 case SMC_INIT:
Ursula Braun3163c502018-01-24 10:28:12 +0100335 if (atomic_read(&conn->bytes_to_rcv) ||
Ursula Braunb38d7322017-01-09 16:55:25 +0100336 (rxflags->peer_done_writing &&
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200337 !smc_cdc_rxed_any_close(conn)))
Ursula Braunb38d7322017-01-09 16:55:25 +0100338 sk->sk_state = SMC_APPCLOSEWAIT1;
339 else
340 sk->sk_state = SMC_CLOSED;
341 break;
342 case SMC_ACTIVE:
343 sk->sk_state = SMC_APPCLOSEWAIT1;
344 break;
345 case SMC_PEERCLOSEWAIT1:
346 if (rxflags->peer_done_writing)
347 sk->sk_state = SMC_PEERCLOSEWAIT2;
Gustavo A. R. Silva7f6b4372017-10-21 20:35:30 -0500348 /* fall through */
349 /* to check for closing */
Ursula Braunb38d7322017-01-09 16:55:25 +0100350 case SMC_PEERCLOSEWAIT2:
351 case SMC_PEERFINCLOSEWAIT:
Ursula Braun3163c502018-01-24 10:28:12 +0100352 if (!smc_cdc_rxed_any_close(conn))
Ursula Braunb38d7322017-01-09 16:55:25 +0100353 break;
354 if (sock_flag(sk, SOCK_DEAD) &&
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200355 smc_close_sent_any_close(conn)) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100356 /* smc_release has already been called locally */
357 sk->sk_state = SMC_CLOSED;
358 } else {
359 /* just shutdown, but not yet closed locally */
360 sk->sk_state = SMC_APPFINCLOSEWAIT;
361 }
362 break;
363 case SMC_APPCLOSEWAIT1:
364 case SMC_APPCLOSEWAIT2:
365 case SMC_APPFINCLOSEWAIT:
366 case SMC_PEERABORTWAIT:
367 case SMC_PROCESSABORT:
368 case SMC_CLOSED:
369 /* nothing to do, add tracing in future patch */
370 break;
371 }
372
373wakeup:
Ursula Braunb38d7322017-01-09 16:55:25 +0100374 sk->sk_data_ready(sk); /* wakeup blocked rcvbuf consumers */
375 sk->sk_write_space(sk); /* wakeup blocked sndbuf producers */
376
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200377 if (old_state != sk->sk_state) {
378 sk->sk_state_change(sk);
379 if ((sk->sk_state == SMC_CLOSED) &&
380 (sock_flag(sk, SOCK_DEAD) || !sk->sk_socket)) {
Ursula Braun3163c502018-01-24 10:28:12 +0100381 smc_conn_free(conn);
Ursula Brauna98bf8c02017-04-10 14:58:02 +0200382 schedule_delayed_work(&smc->sock_put_work,
383 SMC_CLOSE_SOCK_PUT_DELAY);
384 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100385 }
Ursula Braun3163c502018-01-24 10:28:12 +0100386 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100387}
388
389void smc_close_sock_put_work(struct work_struct *work)
390{
391 struct smc_sock *smc = container_of(to_delayed_work(work),
392 struct smc_sock,
393 sock_put_work);
394
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100395 smc->sk.sk_prot->unhash(&smc->sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100396 sock_put(&smc->sk);
397}
398
399int smc_close_shutdown_write(struct smc_sock *smc)
400{
401 struct smc_connection *conn = &smc->conn;
Ursula Braunb38d7322017-01-09 16:55:25 +0100402 struct sock *sk = &smc->sk;
403 int old_state;
Ursula Braun8c96fee2017-09-21 09:16:34 +0200404 long timeout;
Ursula Braunb38d7322017-01-09 16:55:25 +0100405 int rc = 0;
406
Ursula Braun8c96fee2017-09-21 09:16:34 +0200407 timeout = current->flags & PF_EXITING ?
408 0 : sock_flag(sk, SOCK_LINGER) ?
409 sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braunb38d7322017-01-09 16:55:25 +0100410
Ursula Braunb38d7322017-01-09 16:55:25 +0100411 old_state = sk->sk_state;
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100412again:
413 switch (sk->sk_state) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100414 case SMC_ACTIVE:
415 smc_close_stream_wait(smc, timeout);
416 release_sock(sk);
Ursula Braun18e537c2017-09-21 09:16:33 +0200417 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100418 lock_sock(sk);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100419 if (sk->sk_state != SMC_ACTIVE)
420 goto again;
Ursula Braunb38d7322017-01-09 16:55:25 +0100421 /* send close wr request */
422 rc = smc_close_wr(conn);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100423 if (rc)
424 break;
425 sk->sk_state = SMC_PEERCLOSEWAIT1;
Ursula Braunb38d7322017-01-09 16:55:25 +0100426 break;
427 case SMC_APPCLOSEWAIT1:
428 /* passive close */
429 if (!smc_cdc_rxed_any_close(conn))
430 smc_close_stream_wait(smc, timeout);
431 release_sock(sk);
Ursula Braun18e537c2017-09-21 09:16:33 +0200432 cancel_delayed_work_sync(&conn->tx_work);
Ursula Braunb38d7322017-01-09 16:55:25 +0100433 lock_sock(sk);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100434 if (sk->sk_state != SMC_APPCLOSEWAIT1)
435 goto again;
Ursula Braunb38d7322017-01-09 16:55:25 +0100436 /* confirm close from peer */
437 rc = smc_close_wr(conn);
Ursula Braunbbb96bf2018-01-24 10:28:16 +0100438 if (rc)
439 break;
Ursula Braunb38d7322017-01-09 16:55:25 +0100440 sk->sk_state = SMC_APPCLOSEWAIT2;
441 break;
442 case SMC_APPCLOSEWAIT2:
443 case SMC_PEERFINCLOSEWAIT:
444 case SMC_PEERCLOSEWAIT1:
445 case SMC_PEERCLOSEWAIT2:
446 case SMC_APPFINCLOSEWAIT:
447 case SMC_PROCESSABORT:
448 case SMC_PEERABORTWAIT:
449 /* nothing to do, add tracing in future patch */
450 break;
451 }
452
453 if (old_state != sk->sk_state)
Ursula Braun3163c502018-01-24 10:28:12 +0100454 sk->sk_state_change(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100455 return rc;
456}
Ursula Braun46c28db2017-04-10 14:58:01 +0200457
458/* Initialize close properties on connection establishment. */
459void smc_close_init(struct smc_sock *smc)
460{
461 INIT_WORK(&smc->conn.close_work, smc_close_passive_work);
462}