smc_close.c 11.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
3
4
5
6
7
8
9
10
11
12
/*
 *  Shared Memory Communications over RDMA (SMC-R) and RoCE
 *
 *  Socket Closing - normal and abnormal
 *
 *  Copyright IBM Corp. 2016
 *
 *  Author(s):  Ursula Braun <ubraun@linux.vnet.ibm.com>
 */

#include <linux/workqueue.h>
13
14
#include <linux/sched/signal.h>

15
16
17
18
19
20
21
#include <net/sock.h>

#include "smc.h"
#include "smc_tx.h"
#include "smc_cdc.h"
#include "smc_close.h"

22
23
#define SMC_CLOSE_WAIT_LISTEN_CLCSOCK_TIME	(5 * HZ)

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
static void smc_close_cleanup_listen(struct sock *parent)
{
	struct sock *sk;

	/* Close non-accepted connections */
	while ((sk = smc_accept_dequeue(parent, NULL)))
		smc_close_non_accepted(sk);
}

/* wait for sndbuf data being transmitted */
static void smc_close_stream_wait(struct smc_sock *smc, long timeout)
{
	DEFINE_WAIT_FUNC(wait, woken_wake_function);
	struct sock *sk = &smc->sk;

	if (!timeout)
		return;

	if (!smc_tx_prepared_sends(&smc->conn))
		return;

	smc->wait_close_tx_prepared = 1;
	add_wait_queue(sk_sleep(sk), &wait);
	while (!signal_pending(current) && timeout) {
		int rc;

		rc = sk_wait_event(sk, &timeout,
				   !smc_tx_prepared_sends(&smc->conn) ||
				   (sk->sk_err == ECONNABORTED) ||
				   (sk->sk_err == ECONNRESET),
				   &wait);
		if (rc)
			break;
	}
	remove_wait_queue(sk_sleep(sk), &wait);
	smc->wait_close_tx_prepared = 0;
}

void smc_close_wake_tx_prepared(struct smc_sock *smc)
{
	if (smc->wait_close_tx_prepared)
		/* wake up socket closing */
		smc->sk.sk_state_change(&smc->sk);
}

static int smc_close_wr(struct smc_connection *conn)
{
	conn->local_tx_ctrl.conn_state_flags.peer_done_writing = 1;

	return smc_cdc_get_slot_and_msg_send(conn);
}

static int smc_close_final(struct smc_connection *conn)
{
	if (atomic_read(&conn->bytes_to_rcv))
		conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
	else
		conn->local_tx_ctrl.conn_state_flags.peer_conn_closed = 1;

	return smc_cdc_get_slot_and_msg_send(conn);
}

static int smc_close_abort(struct smc_connection *conn)
{
	conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;

	return smc_cdc_get_slot_and_msg_send(conn);
}

/* terminate smc socket abnormally - active abort
94
 * link group is terminated, i.e. RDMA communication no longer possible
95
 */
96
static void smc_close_active_abort(struct smc_sock *smc)
97
{
98
99
	struct sock *sk = &smc->sk;

100
101
102
	struct smc_cdc_conn_state_flags *txflags =
		&smc->conn.local_tx_ctrl.conn_state_flags;

103
	sk->sk_err = ECONNABORTED;
104
105
106
107
	if (smc->clcsock && smc->clcsock->sk) {
		smc->clcsock->sk->sk_err = ECONNABORTED;
		smc->clcsock->sk->sk_state_change(smc->clcsock->sk);
	}
108
	switch (sk->sk_state) {
109
	case SMC_INIT:
110
111
		sk->sk_state = SMC_PEERABORTWAIT;
		break;
112
	case SMC_ACTIVE:
113
		sk->sk_state = SMC_PEERABORTWAIT;
114
115
116
		release_sock(sk);
		cancel_delayed_work_sync(&smc->conn.tx_work);
		lock_sock(sk);
117
		sock_put(sk); /* passive closing */
118
119
120
121
		break;
	case SMC_APPCLOSEWAIT1:
	case SMC_APPCLOSEWAIT2:
		if (!smc_cdc_rxed_any_close(&smc->conn))
122
			sk->sk_state = SMC_PEERABORTWAIT;
123
		else
124
			sk->sk_state = SMC_CLOSED;
125
126
127
		release_sock(sk);
		cancel_delayed_work_sync(&smc->conn.tx_work);
		lock_sock(sk);
128
129
130
131
		break;
	case SMC_PEERCLOSEWAIT1:
	case SMC_PEERCLOSEWAIT2:
		if (!txflags->peer_conn_closed) {
132
			/* just SHUTDOWN_SEND done */
133
			sk->sk_state = SMC_PEERABORTWAIT;
134
		} else {
135
			sk->sk_state = SMC_CLOSED;
136
		}
137
		sock_put(sk); /* passive closing */
138
139
140
		break;
	case SMC_PROCESSABORT:
	case SMC_APPFINCLOSEWAIT:
141
		sk->sk_state = SMC_CLOSED;
142
143
		break;
	case SMC_PEERFINCLOSEWAIT:
144
145
		sock_put(sk); /* passive closing */
		break;
146
147
148
149
150
	case SMC_PEERABORTWAIT:
	case SMC_CLOSED:
		break;
	}

151
152
	sock_set_flag(sk, SOCK_DEAD);
	sk->sk_state_change(sk);
153
154
}

155
156
157
158
159
160
static inline bool smc_close_sent_any_close(struct smc_connection *conn)
{
	return conn->local_tx_ctrl.conn_state_flags.peer_conn_abort ||
	       conn->local_tx_ctrl.conn_state_flags.peer_conn_closed;
}

161
162
163
164
165
166
167
int smc_close_active(struct smc_sock *smc)
{
	struct smc_cdc_conn_state_flags *txflags =
		&smc->conn.local_tx_ctrl.conn_state_flags;
	struct smc_connection *conn = &smc->conn;
	struct sock *sk = &smc->sk;
	int old_state;
168
	long timeout;
169
170
	int rc = 0;

171
172
173
	timeout = current->flags & PF_EXITING ?
		  0 : sock_flag(sk, SOCK_LINGER) ?
		      sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
174
175

	old_state = sk->sk_state;
176
177
again:
	switch (sk->sk_state) {
178
179
180
181
182
183
184
185
186
187
188
189
	case SMC_INIT:
		sk->sk_state = SMC_CLOSED;
		break;
	case SMC_LISTEN:
		sk->sk_state = SMC_CLOSED;
		sk->sk_state_change(sk); /* wake up accept */
		if (smc->clcsock && smc->clcsock->sk) {
			rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
			/* wake up kernel_accept of smc_tcp_listen_worker */
			smc->clcsock->sk->sk_data_ready(smc->clcsock->sk);
		}
		smc_close_cleanup_listen(sk);
190
191
192
		release_sock(sk);
		flush_work(&smc->tcp_listen_work);
		lock_sock(sk);
193
194
195
196
		break;
	case SMC_ACTIVE:
		smc_close_stream_wait(smc, timeout);
		release_sock(sk);
Ursula Braun's avatar
Ursula Braun committed
197
		cancel_delayed_work_sync(&conn->tx_work);
198
199
200
201
		lock_sock(sk);
		if (sk->sk_state == SMC_ACTIVE) {
			/* send close request */
			rc = smc_close_final(conn);
202
203
			if (rc)
				break;
204
205
206
207
208
209
210
211
212
			sk->sk_state = SMC_PEERCLOSEWAIT1;
		} else {
			/* peer event has changed the state */
			goto again;
		}
		break;
	case SMC_APPFINCLOSEWAIT:
		/* socket already shutdown wr or both (active close) */
		if (txflags->peer_done_writing &&
213
		    !smc_close_sent_any_close(conn)) {
214
215
			/* just shutdown wr done, send close request */
			rc = smc_close_final(conn);
216
217
			if (rc)
				break;
218
219
220
221
222
223
224
225
		}
		sk->sk_state = SMC_CLOSED;
		break;
	case SMC_APPCLOSEWAIT1:
	case SMC_APPCLOSEWAIT2:
		if (!smc_cdc_rxed_any_close(conn))
			smc_close_stream_wait(smc, timeout);
		release_sock(sk);
Ursula Braun's avatar
Ursula Braun committed
226
		cancel_delayed_work_sync(&conn->tx_work);
227
		lock_sock(sk);
228
229
230
231
232
233
234
		if (sk->sk_state != SMC_APPCLOSEWAIT1 &&
		    sk->sk_state != SMC_APPCLOSEWAIT2)
			goto again;
		/* confirm close from peer */
		rc = smc_close_final(conn);
		if (rc)
			break;
235
		if (smc_cdc_rxed_any_close(conn)) {
236
237
			/* peer has closed the socket already */
			sk->sk_state = SMC_CLOSED;
238
239
			sock_put(sk); /* postponed passive closing */
		} else {
240
241
			/* peer has just issued a shutdown write */
			sk->sk_state = SMC_PEERFINCLOSEWAIT;
242
		}
243
244
245
		break;
	case SMC_PEERCLOSEWAIT1:
	case SMC_PEERCLOSEWAIT2:
246
247
248
249
		if (txflags->peer_done_writing &&
		    !smc_close_sent_any_close(conn)) {
			/* just shutdown wr done, send close request */
			rc = smc_close_final(conn);
250
251
			if (rc)
				break;
252
253
254
		}
		/* peer sending PeerConnectionClosed will cause transition */
		break;
255
256
257
258
259
260
261
262
263
264
265
266
267
268
	case SMC_PEERFINCLOSEWAIT:
		/* peer sending PeerConnectionClosed will cause transition */
		break;
	case SMC_PROCESSABORT:
		smc_close_abort(conn);
		sk->sk_state = SMC_CLOSED;
		break;
	case SMC_PEERABORTWAIT:
	case SMC_CLOSED:
		/* nothing to do, add tracing in future patch */
		break;
	}

	if (old_state != sk->sk_state)
269
		sk->sk_state_change(sk);
270
271
272
273
274
275
276
277
278
279
	return rc;
}

static void smc_close_passive_abort_received(struct smc_sock *smc)
{
	struct smc_cdc_conn_state_flags *txflags =
		&smc->conn.local_tx_ctrl.conn_state_flags;
	struct sock *sk = &smc->sk;

	switch (sk->sk_state) {
280
	case SMC_INIT:
281
282
	case SMC_ACTIVE:
	case SMC_APPCLOSEWAIT1:
283
284
285
286
		sk->sk_state = SMC_PROCESSABORT;
		sock_put(sk); /* passive closing */
		break;
	case SMC_APPFINCLOSEWAIT:
287
288
289
290
291
		sk->sk_state = SMC_PROCESSABORT;
		break;
	case SMC_PEERCLOSEWAIT1:
	case SMC_PEERCLOSEWAIT2:
		if (txflags->peer_done_writing &&
292
		    !smc_close_sent_any_close(&smc->conn))
293
294
			/* just shutdown, but not yet closed locally */
			sk->sk_state = SMC_PROCESSABORT;
295
		else
296
			sk->sk_state = SMC_CLOSED;
297
		sock_put(sk); /* passive closing */
298
		break;
299
	case SMC_APPCLOSEWAIT2:
300
	case SMC_PEERFINCLOSEWAIT:
301
302
303
		sk->sk_state = SMC_CLOSED;
		sock_put(sk); /* passive closing */
		break;
304
305
306
307
308
309
310
311
312
	case SMC_PEERABORTWAIT:
		sk->sk_state = SMC_CLOSED;
		break;
	case SMC_PROCESSABORT:
	/* nothing to do, add tracing in future patch */
		break;
	}
}

313
314
315
/* Either some kind of closing has been received: peer_conn_closed,
 * peer_conn_abort, or peer_done_writing
 * or the link group of the connection terminates abnormally.
316
 */
317
static void smc_close_passive_work(struct work_struct *work)
318
{
319
320
321
322
323
	struct smc_connection *conn = container_of(work,
						   struct smc_connection,
						   close_work);
	struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
	struct smc_cdc_conn_state_flags *rxflags;
324
325
326
	struct sock *sk = &smc->sk;
	int old_state;

327
	lock_sock(sk);
328
329
	old_state = sk->sk_state;

330
331
332
333
334
335
	if (!conn->alert_token_local) {
		/* abnormal termination */
		smc_close_active_abort(smc);
		goto wakeup;
	}

336
	rxflags = &conn->local_rx_ctrl.conn_state_flags;
337
	if (rxflags->peer_conn_abort) {
338
		/* peer has not received all data */
339
		smc_close_passive_abort_received(smc);
340
341
342
		release_sock(&smc->sk);
		cancel_delayed_work_sync(&conn->tx_work);
		lock_sock(&smc->sk);
343
344
345
346
347
		goto wakeup;
	}

	switch (sk->sk_state) {
	case SMC_INIT:
348
		if (atomic_read(&conn->bytes_to_rcv) ||
349
		    (rxflags->peer_done_writing &&
350
		     !smc_cdc_rxed_any_close(conn))) {
351
			sk->sk_state = SMC_APPCLOSEWAIT1;
352
		} else {
353
			sk->sk_state = SMC_CLOSED;
354
355
			sock_put(sk); /* passive closing */
		}
356
357
358
		break;
	case SMC_ACTIVE:
		sk->sk_state = SMC_APPCLOSEWAIT1;
359
360
361
		/* postpone sock_put() for passive closing to cover
		 * received SEND_SHUTDOWN as well
		 */
362
363
364
365
		break;
	case SMC_PEERCLOSEWAIT1:
		if (rxflags->peer_done_writing)
			sk->sk_state = SMC_PEERCLOSEWAIT2;
366
367
		/* fall through */
		/* to check for closing */
368
	case SMC_PEERCLOSEWAIT2:
369
		if (!smc_cdc_rxed_any_close(conn))
370
371
			break;
		if (sock_flag(sk, SOCK_DEAD) &&
372
		    smc_close_sent_any_close(conn)) {
373
374
375
376
377
378
			/* smc_release has already been called locally */
			sk->sk_state = SMC_CLOSED;
		} else {
			/* just shutdown, but not yet closed locally */
			sk->sk_state = SMC_APPFINCLOSEWAIT;
		}
379
		sock_put(sk); /* passive closing */
380
		break;
381
	case SMC_PEERFINCLOSEWAIT:
382
		if (smc_cdc_rxed_any_close(conn)) {
383
			sk->sk_state = SMC_CLOSED;
384
385
			sock_put(sk); /* passive closing */
		}
386
		break;
387
388
	case SMC_APPCLOSEWAIT1:
	case SMC_APPCLOSEWAIT2:
389
390
391
392
		/* postpone sock_put() for passive closing to cover
		 * received SEND_SHUTDOWN as well
		 */
		break;
393
394
395
396
397
398
399
400
401
402
403
404
	case SMC_APPFINCLOSEWAIT:
	case SMC_PEERABORTWAIT:
	case SMC_PROCESSABORT:
	case SMC_CLOSED:
		/* nothing to do, add tracing in future patch */
		break;
	}

wakeup:
	sk->sk_data_ready(sk); /* wakeup blocked rcvbuf consumers */
	sk->sk_write_space(sk); /* wakeup blocked sndbuf producers */

405
406
407
	if (old_state != sk->sk_state) {
		sk->sk_state_change(sk);
		if ((sk->sk_state == SMC_CLOSED) &&
408
		    (sock_flag(sk, SOCK_DEAD) || !sk->sk_socket))
409
			smc_conn_free(conn);
410
	}
411
	release_sock(sk);
412
	sock_put(sk); /* sock_hold done by schedulers of close_work */
413
414
415
416
417
418
419
}

int smc_close_shutdown_write(struct smc_sock *smc)
{
	struct smc_connection *conn = &smc->conn;
	struct sock *sk = &smc->sk;
	int old_state;
420
	long timeout;
421
422
	int rc = 0;

423
424
425
	timeout = current->flags & PF_EXITING ?
		  0 : sock_flag(sk, SOCK_LINGER) ?
		      sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
426
427

	old_state = sk->sk_state;
428
429
again:
	switch (sk->sk_state) {
430
431
432
	case SMC_ACTIVE:
		smc_close_stream_wait(smc, timeout);
		release_sock(sk);
Ursula Braun's avatar
Ursula Braun committed
433
		cancel_delayed_work_sync(&conn->tx_work);
434
		lock_sock(sk);
435
436
		if (sk->sk_state != SMC_ACTIVE)
			goto again;
437
438
		/* send close wr request */
		rc = smc_close_wr(conn);
439
440
441
		if (rc)
			break;
		sk->sk_state = SMC_PEERCLOSEWAIT1;
442
443
444
445
446
447
		break;
	case SMC_APPCLOSEWAIT1:
		/* passive close */
		if (!smc_cdc_rxed_any_close(conn))
			smc_close_stream_wait(smc, timeout);
		release_sock(sk);
Ursula Braun's avatar
Ursula Braun committed
448
		cancel_delayed_work_sync(&conn->tx_work);
449
		lock_sock(sk);
450
451
		if (sk->sk_state != SMC_APPCLOSEWAIT1)
			goto again;
452
453
		/* confirm close from peer */
		rc = smc_close_wr(conn);
454
455
		if (rc)
			break;
456
457
458
459
460
461
462
463
464
465
466
467
468
469
		sk->sk_state = SMC_APPCLOSEWAIT2;
		break;
	case SMC_APPCLOSEWAIT2:
	case SMC_PEERFINCLOSEWAIT:
	case SMC_PEERCLOSEWAIT1:
	case SMC_PEERCLOSEWAIT2:
	case SMC_APPFINCLOSEWAIT:
	case SMC_PROCESSABORT:
	case SMC_PEERABORTWAIT:
		/* nothing to do, add tracing in future patch */
		break;
	}

	if (old_state != sk->sk_state)
470
		sk->sk_state_change(sk);
471
472
	return rc;
}
473
474
475
476
477
478

/* Initialize close properties on connection establishment. */
void smc_close_init(struct smc_sock *smc)
{
	INIT_WORK(&smc->conn.close_work, smc_close_passive_work);
}