xref: /openbmc/linux/net/smc/smc_close.c (revision 5a244f48)
1 /*
2  *  Shared Memory Communications over RDMA (SMC-R) and RoCE
3  *
4  *  Socket Closing - normal and abnormal
5  *
6  *  Copyright IBM Corp. 2016
7  *
8  *  Author(s):  Ursula Braun <ubraun@linux.vnet.ibm.com>
9  */
10 
11 #include <linux/workqueue.h>
12 #include <linux/sched/signal.h>
13 
14 #include <net/sock.h>
15 
16 #include "smc.h"
17 #include "smc_tx.h"
18 #include "smc_cdc.h"
19 #include "smc_close.h"
20 
21 #define SMC_CLOSE_WAIT_TX_PENDS_TIME		(5 * HZ)
22 
23 static void smc_close_cleanup_listen(struct sock *parent)
24 {
25 	struct sock *sk;
26 
27 	/* Close non-accepted connections */
28 	while ((sk = smc_accept_dequeue(parent, NULL)))
29 		smc_close_non_accepted(sk);
30 }
31 
32 static void smc_close_wait_tx_pends(struct smc_sock *smc)
33 {
34 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
35 	struct sock *sk = &smc->sk;
36 	signed long timeout;
37 
38 	timeout = SMC_CLOSE_WAIT_TX_PENDS_TIME;
39 	add_wait_queue(sk_sleep(sk), &wait);
40 	while (!signal_pending(current) && timeout) {
41 		int rc;
42 
43 		rc = sk_wait_event(sk, &timeout,
44 				   !smc_cdc_tx_has_pending(&smc->conn),
45 				   &wait);
46 		if (rc)
47 			break;
48 	}
49 	remove_wait_queue(sk_sleep(sk), &wait);
50 }
51 
52 /* wait for sndbuf data being transmitted */
53 static void smc_close_stream_wait(struct smc_sock *smc, long timeout)
54 {
55 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
56 	struct sock *sk = &smc->sk;
57 
58 	if (!timeout)
59 		return;
60 
61 	if (!smc_tx_prepared_sends(&smc->conn))
62 		return;
63 
64 	smc->wait_close_tx_prepared = 1;
65 	add_wait_queue(sk_sleep(sk), &wait);
66 	while (!signal_pending(current) && timeout) {
67 		int rc;
68 
69 		rc = sk_wait_event(sk, &timeout,
70 				   !smc_tx_prepared_sends(&smc->conn) ||
71 				   (sk->sk_err == ECONNABORTED) ||
72 				   (sk->sk_err == ECONNRESET),
73 				   &wait);
74 		if (rc)
75 			break;
76 	}
77 	remove_wait_queue(sk_sleep(sk), &wait);
78 	smc->wait_close_tx_prepared = 0;
79 }
80 
81 void smc_close_wake_tx_prepared(struct smc_sock *smc)
82 {
83 	if (smc->wait_close_tx_prepared)
84 		/* wake up socket closing */
85 		smc->sk.sk_state_change(&smc->sk);
86 }
87 
88 static int smc_close_wr(struct smc_connection *conn)
89 {
90 	conn->local_tx_ctrl.conn_state_flags.peer_done_writing = 1;
91 
92 	return smc_cdc_get_slot_and_msg_send(conn);
93 }
94 
95 static int smc_close_final(struct smc_connection *conn)
96 {
97 	if (atomic_read(&conn->bytes_to_rcv))
98 		conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
99 	else
100 		conn->local_tx_ctrl.conn_state_flags.peer_conn_closed = 1;
101 
102 	return smc_cdc_get_slot_and_msg_send(conn);
103 }
104 
105 static int smc_close_abort(struct smc_connection *conn)
106 {
107 	conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
108 
109 	return smc_cdc_get_slot_and_msg_send(conn);
110 }
111 
112 /* terminate smc socket abnormally - active abort
113  * RDMA communication no longer possible
114  */
115 void smc_close_active_abort(struct smc_sock *smc)
116 {
117 	struct smc_cdc_conn_state_flags *txflags =
118 		&smc->conn.local_tx_ctrl.conn_state_flags;
119 
120 	smc->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 	}
125 	switch (smc->sk.sk_state) {
126 	case SMC_INIT:
127 	case SMC_ACTIVE:
128 		smc->sk.sk_state = SMC_PEERABORTWAIT;
129 		break;
130 	case SMC_APPCLOSEWAIT1:
131 	case SMC_APPCLOSEWAIT2:
132 		txflags->peer_conn_abort = 1;
133 		sock_release(smc->clcsock);
134 		if (!smc_cdc_rxed_any_close(&smc->conn))
135 			smc->sk.sk_state = SMC_PEERABORTWAIT;
136 		else
137 			smc->sk.sk_state = SMC_CLOSED;
138 		break;
139 	case SMC_PEERCLOSEWAIT1:
140 	case SMC_PEERCLOSEWAIT2:
141 		if (!txflags->peer_conn_closed) {
142 			smc->sk.sk_state = SMC_PEERABORTWAIT;
143 			txflags->peer_conn_abort = 1;
144 			sock_release(smc->clcsock);
145 		} else {
146 			smc->sk.sk_state = SMC_CLOSED;
147 		}
148 		break;
149 	case SMC_PROCESSABORT:
150 	case SMC_APPFINCLOSEWAIT:
151 		if (!txflags->peer_conn_closed) {
152 			txflags->peer_conn_abort = 1;
153 			sock_release(smc->clcsock);
154 		}
155 		smc->sk.sk_state = SMC_CLOSED;
156 		break;
157 	case SMC_PEERFINCLOSEWAIT:
158 	case SMC_PEERABORTWAIT:
159 	case SMC_CLOSED:
160 		break;
161 	}
162 
163 	sock_set_flag(&smc->sk, SOCK_DEAD);
164 	smc->sk.sk_state_change(&smc->sk);
165 }
166 
167 static inline bool smc_close_sent_any_close(struct smc_connection *conn)
168 {
169 	return conn->local_tx_ctrl.conn_state_flags.peer_conn_abort ||
170 	       conn->local_tx_ctrl.conn_state_flags.peer_conn_closed;
171 }
172 
173 int smc_close_active(struct smc_sock *smc)
174 {
175 	struct smc_cdc_conn_state_flags *txflags =
176 		&smc->conn.local_tx_ctrl.conn_state_flags;
177 	struct smc_connection *conn = &smc->conn;
178 	struct sock *sk = &smc->sk;
179 	int old_state;
180 	long timeout;
181 	int rc = 0;
182 
183 	timeout = current->flags & PF_EXITING ?
184 		  0 : sock_flag(sk, SOCK_LINGER) ?
185 		      sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
186 
187 again:
188 	old_state = sk->sk_state;
189 	switch (old_state) {
190 	case SMC_INIT:
191 		sk->sk_state = SMC_CLOSED;
192 		if (smc->smc_listen_work.func)
193 			cancel_work_sync(&smc->smc_listen_work);
194 		break;
195 	case SMC_LISTEN:
196 		sk->sk_state = SMC_CLOSED;
197 		sk->sk_state_change(sk); /* wake up accept */
198 		if (smc->clcsock && smc->clcsock->sk) {
199 			rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
200 			/* wake up kernel_accept of smc_tcp_listen_worker */
201 			smc->clcsock->sk->sk_data_ready(smc->clcsock->sk);
202 		}
203 		release_sock(sk);
204 		smc_close_cleanup_listen(sk);
205 		cancel_work_sync(&smc->smc_listen_work);
206 		lock_sock(sk);
207 		break;
208 	case SMC_ACTIVE:
209 		smc_close_stream_wait(smc, timeout);
210 		release_sock(sk);
211 		cancel_delayed_work_sync(&conn->tx_work);
212 		lock_sock(sk);
213 		if (sk->sk_state == SMC_ACTIVE) {
214 			/* send close request */
215 			rc = smc_close_final(conn);
216 			sk->sk_state = SMC_PEERCLOSEWAIT1;
217 		} else {
218 			/* peer event has changed the state */
219 			goto again;
220 		}
221 		break;
222 	case SMC_APPFINCLOSEWAIT:
223 		/* socket already shutdown wr or both (active close) */
224 		if (txflags->peer_done_writing &&
225 		    !smc_close_sent_any_close(conn)) {
226 			/* just shutdown wr done, send close request */
227 			rc = smc_close_final(conn);
228 		}
229 		sk->sk_state = SMC_CLOSED;
230 		smc_close_wait_tx_pends(smc);
231 		break;
232 	case SMC_APPCLOSEWAIT1:
233 	case SMC_APPCLOSEWAIT2:
234 		if (!smc_cdc_rxed_any_close(conn))
235 			smc_close_stream_wait(smc, timeout);
236 		release_sock(sk);
237 		cancel_delayed_work_sync(&conn->tx_work);
238 		lock_sock(sk);
239 		if (sk->sk_err != ECONNABORTED) {
240 			/* confirm close from peer */
241 			rc = smc_close_final(conn);
242 			if (rc)
243 				break;
244 		}
245 		if (smc_cdc_rxed_any_close(conn))
246 			/* peer has closed the socket already */
247 			sk->sk_state = SMC_CLOSED;
248 		else
249 			/* peer has just issued a shutdown write */
250 			sk->sk_state = SMC_PEERFINCLOSEWAIT;
251 		smc_close_wait_tx_pends(smc);
252 		break;
253 	case SMC_PEERCLOSEWAIT1:
254 	case SMC_PEERCLOSEWAIT2:
255 		if (txflags->peer_done_writing &&
256 		    !smc_close_sent_any_close(conn)) {
257 			/* just shutdown wr done, send close request */
258 			rc = smc_close_final(conn);
259 		}
260 		/* peer sending PeerConnectionClosed will cause transition */
261 		break;
262 	case SMC_PEERFINCLOSEWAIT:
263 		/* peer sending PeerConnectionClosed will cause transition */
264 		break;
265 	case SMC_PROCESSABORT:
266 		release_sock(sk);
267 		cancel_delayed_work_sync(&conn->tx_work);
268 		lock_sock(sk);
269 		smc_close_abort(conn);
270 		sk->sk_state = SMC_CLOSED;
271 		smc_close_wait_tx_pends(smc);
272 		break;
273 	case SMC_PEERABORTWAIT:
274 	case SMC_CLOSED:
275 		/* nothing to do, add tracing in future patch */
276 		break;
277 	}
278 
279 	if (old_state != sk->sk_state)
280 		sk->sk_state_change(&smc->sk);
281 	return rc;
282 }
283 
284 static void smc_close_passive_abort_received(struct smc_sock *smc)
285 {
286 	struct smc_cdc_conn_state_flags *txflags =
287 		&smc->conn.local_tx_ctrl.conn_state_flags;
288 	struct sock *sk = &smc->sk;
289 
290 	switch (sk->sk_state) {
291 	case SMC_ACTIVE:
292 	case SMC_APPFINCLOSEWAIT:
293 	case SMC_APPCLOSEWAIT1:
294 	case SMC_APPCLOSEWAIT2:
295 		smc_close_abort(&smc->conn);
296 		sk->sk_state = SMC_PROCESSABORT;
297 		break;
298 	case SMC_PEERCLOSEWAIT1:
299 	case SMC_PEERCLOSEWAIT2:
300 		if (txflags->peer_done_writing &&
301 		    !smc_close_sent_any_close(&smc->conn)) {
302 			/* just shutdown, but not yet closed locally */
303 			smc_close_abort(&smc->conn);
304 			sk->sk_state = SMC_PROCESSABORT;
305 		} else {
306 			sk->sk_state = SMC_CLOSED;
307 		}
308 		break;
309 	case SMC_PEERFINCLOSEWAIT:
310 	case SMC_PEERABORTWAIT:
311 		sk->sk_state = SMC_CLOSED;
312 		break;
313 	case SMC_INIT:
314 	case SMC_PROCESSABORT:
315 	/* nothing to do, add tracing in future patch */
316 		break;
317 	}
318 }
319 
320 /* Some kind of closing has been received: peer_conn_closed, peer_conn_abort,
321  * or peer_done_writing.
322  */
323 static void smc_close_passive_work(struct work_struct *work)
324 {
325 	struct smc_connection *conn = container_of(work,
326 						   struct smc_connection,
327 						   close_work);
328 	struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
329 	struct smc_cdc_conn_state_flags *rxflags;
330 	struct sock *sk = &smc->sk;
331 	int old_state;
332 
333 	lock_sock(&smc->sk);
334 	old_state = sk->sk_state;
335 
336 	if (!conn->alert_token_local) {
337 		/* abnormal termination */
338 		smc_close_active_abort(smc);
339 		goto wakeup;
340 	}
341 
342 	rxflags = &smc->conn.local_rx_ctrl.conn_state_flags;
343 	if (rxflags->peer_conn_abort) {
344 		smc_close_passive_abort_received(smc);
345 		goto wakeup;
346 	}
347 
348 	switch (sk->sk_state) {
349 	case SMC_INIT:
350 		if (atomic_read(&smc->conn.bytes_to_rcv) ||
351 		    (rxflags->peer_done_writing &&
352 		     !smc_cdc_rxed_any_close(conn)))
353 			sk->sk_state = SMC_APPCLOSEWAIT1;
354 		else
355 			sk->sk_state = SMC_CLOSED;
356 		break;
357 	case SMC_ACTIVE:
358 		sk->sk_state = SMC_APPCLOSEWAIT1;
359 		break;
360 	case SMC_PEERCLOSEWAIT1:
361 		if (rxflags->peer_done_writing)
362 			sk->sk_state = SMC_PEERCLOSEWAIT2;
363 		/* fall through to check for closing */
364 	case SMC_PEERCLOSEWAIT2:
365 	case SMC_PEERFINCLOSEWAIT:
366 		if (!smc_cdc_rxed_any_close(&smc->conn))
367 			break;
368 		if (sock_flag(sk, SOCK_DEAD) &&
369 		    smc_close_sent_any_close(conn)) {
370 			/* smc_release has already been called locally */
371 			sk->sk_state = SMC_CLOSED;
372 		} else {
373 			/* just shutdown, but not yet closed locally */
374 			sk->sk_state = SMC_APPFINCLOSEWAIT;
375 		}
376 		break;
377 	case SMC_APPCLOSEWAIT1:
378 	case SMC_APPCLOSEWAIT2:
379 	case SMC_APPFINCLOSEWAIT:
380 	case SMC_PEERABORTWAIT:
381 	case SMC_PROCESSABORT:
382 	case SMC_CLOSED:
383 		/* nothing to do, add tracing in future patch */
384 		break;
385 	}
386 
387 wakeup:
388 	sk->sk_data_ready(sk); /* wakeup blocked rcvbuf consumers */
389 	sk->sk_write_space(sk); /* wakeup blocked sndbuf producers */
390 
391 	if (old_state != sk->sk_state) {
392 		sk->sk_state_change(sk);
393 		if ((sk->sk_state == SMC_CLOSED) &&
394 		    (sock_flag(sk, SOCK_DEAD) || !sk->sk_socket)) {
395 			smc_conn_free(&smc->conn);
396 			schedule_delayed_work(&smc->sock_put_work,
397 					      SMC_CLOSE_SOCK_PUT_DELAY);
398 		}
399 	}
400 	release_sock(&smc->sk);
401 }
402 
403 void smc_close_sock_put_work(struct work_struct *work)
404 {
405 	struct smc_sock *smc = container_of(to_delayed_work(work),
406 					    struct smc_sock,
407 					    sock_put_work);
408 
409 	smc->sk.sk_prot->unhash(&smc->sk);
410 	sock_put(&smc->sk);
411 }
412 
413 int smc_close_shutdown_write(struct smc_sock *smc)
414 {
415 	struct smc_connection *conn = &smc->conn;
416 	struct sock *sk = &smc->sk;
417 	int old_state;
418 	long timeout;
419 	int rc = 0;
420 
421 	timeout = current->flags & PF_EXITING ?
422 		  0 : sock_flag(sk, SOCK_LINGER) ?
423 		      sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
424 
425 again:
426 	old_state = sk->sk_state;
427 	switch (old_state) {
428 	case SMC_ACTIVE:
429 		smc_close_stream_wait(smc, timeout);
430 		release_sock(sk);
431 		cancel_delayed_work_sync(&conn->tx_work);
432 		lock_sock(sk);
433 		/* send close wr request */
434 		rc = smc_close_wr(conn);
435 		if (sk->sk_state == SMC_ACTIVE)
436 			sk->sk_state = SMC_PEERCLOSEWAIT1;
437 		else
438 			goto again;
439 		break;
440 	case SMC_APPCLOSEWAIT1:
441 		/* passive close */
442 		if (!smc_cdc_rxed_any_close(conn))
443 			smc_close_stream_wait(smc, timeout);
444 		release_sock(sk);
445 		cancel_delayed_work_sync(&conn->tx_work);
446 		lock_sock(sk);
447 		/* confirm close from peer */
448 		rc = smc_close_wr(conn);
449 		sk->sk_state = SMC_APPCLOSEWAIT2;
450 		break;
451 	case SMC_APPCLOSEWAIT2:
452 	case SMC_PEERFINCLOSEWAIT:
453 	case SMC_PEERCLOSEWAIT1:
454 	case SMC_PEERCLOSEWAIT2:
455 	case SMC_APPFINCLOSEWAIT:
456 	case SMC_PROCESSABORT:
457 	case SMC_PEERABORTWAIT:
458 		/* nothing to do, add tracing in future patch */
459 		break;
460 	}
461 
462 	if (old_state != sk->sk_state)
463 		sk->sk_state_change(&smc->sk);
464 	return rc;
465 }
466 
467 /* Initialize close properties on connection establishment. */
468 void smc_close_init(struct smc_sock *smc)
469 {
470 	INIT_WORK(&smc->conn.close_work, smc_close_passive_work);
471 }
472