KVM: PPC: Book3S HV: Don't rely on host's page size information
[linux/fpc-iii.git] / net / smc / smc_close.c
blobf0d16fb825f7a545104232baacca32520cb14e08
1 /*
2 * Shared Memory Communications over RDMA (SMC-R) and RoCE
4 * Socket Closing - normal and abnormal
6 * Copyright IBM Corp. 2016
8 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
9 */
11 #include <linux/workqueue.h>
12 #include <linux/sched/signal.h>
14 #include <net/sock.h>
16 #include "smc.h"
17 #include "smc_tx.h"
18 #include "smc_cdc.h"
19 #include "smc_close.h"
21 #define SMC_CLOSE_WAIT_TX_PENDS_TIME (5 * HZ)
23 static void smc_close_cleanup_listen(struct sock *parent)
25 struct sock *sk;
27 /* Close non-accepted connections */
28 while ((sk = smc_accept_dequeue(parent, NULL)))
29 smc_close_non_accepted(sk);
32 static void smc_close_wait_tx_pends(struct smc_sock *smc)
34 DEFINE_WAIT_FUNC(wait, woken_wake_function);
35 struct sock *sk = &smc->sk;
36 signed long timeout;
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;
43 rc = sk_wait_event(sk, &timeout,
44 !smc_cdc_tx_has_pending(&smc->conn),
45 &wait);
46 if (rc)
47 break;
49 remove_wait_queue(sk_sleep(sk), &wait);
52 /* wait for sndbuf data being transmitted */
53 static void smc_close_stream_wait(struct smc_sock *smc, long timeout)
55 DEFINE_WAIT_FUNC(wait, woken_wake_function);
56 struct sock *sk = &smc->sk;
58 if (!timeout)
59 return;
61 if (!smc_tx_prepared_sends(&smc->conn))
62 return;
64 smc->wait_close_tx_prepared = 1;
65 add_wait_queue(sk_sleep(sk), &wait);
66 while (!signal_pending(current) && timeout) {
67 int rc;
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;
77 remove_wait_queue(sk_sleep(sk), &wait);
78 smc->wait_close_tx_prepared = 0;
81 void smc_close_wake_tx_prepared(struct smc_sock *smc)
83 if (smc->wait_close_tx_prepared)
84 /* wake up socket closing */
85 smc->sk.sk_state_change(&smc->sk);
88 static int smc_close_wr(struct smc_connection *conn)
90 conn->local_tx_ctrl.conn_state_flags.peer_done_writing = 1;
92 return smc_cdc_get_slot_and_msg_send(conn);
95 static int smc_close_final(struct smc_connection *conn)
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;
102 return smc_cdc_get_slot_and_msg_send(conn);
105 static int smc_close_abort(struct smc_connection *conn)
107 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
109 return smc_cdc_get_slot_and_msg_send(conn);
112 /* terminate smc socket abnormally - active abort
113 * RDMA communication no longer possible
115 void smc_close_active_abort(struct smc_sock *smc)
117 struct smc_cdc_conn_state_flags *txflags =
118 &smc->conn.local_tx_ctrl.conn_state_flags;
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);
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;
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);
155 smc->sk.sk_state = SMC_CLOSED;
156 break;
157 case SMC_PEERFINCLOSEWAIT:
158 case SMC_PEERABORTWAIT:
159 case SMC_CLOSED:
160 break;
163 sock_set_flag(&smc->sk, SOCK_DEAD);
164 smc->sk.sk_state_change(&smc->sk);
167 static inline bool smc_close_sent_any_close(struct smc_connection *conn)
169 return conn->local_tx_ctrl.conn_state_flags.peer_conn_abort ||
170 conn->local_tx_ctrl.conn_state_flags.peer_conn_closed;
173 int smc_close_active(struct smc_sock *smc)
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;
183 timeout = current->flags & PF_EXITING ?
184 0 : sock_flag(sk, SOCK_LINGER) ?
185 sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
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);
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;
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);
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;
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);
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;
279 if (old_state != sk->sk_state)
280 sk->sk_state_change(&smc->sk);
281 return rc;
284 static void smc_close_passive_abort_received(struct smc_sock *smc)
286 struct smc_cdc_conn_state_flags *txflags =
287 &smc->conn.local_tx_ctrl.conn_state_flags;
288 struct sock *sk = &smc->sk;
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;
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;
320 /* Some kind of closing has been received: peer_conn_closed, peer_conn_abort,
321 * or peer_done_writing.
323 static void smc_close_passive_work(struct work_struct *work)
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;
333 lock_sock(&smc->sk);
334 old_state = sk->sk_state;
336 if (!conn->alert_token_local) {
337 /* abnormal termination */
338 smc_close_active_abort(smc);
339 goto wakeup;
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;
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;
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;
387 wakeup:
388 sk->sk_data_ready(sk); /* wakeup blocked rcvbuf consumers */
389 sk->sk_write_space(sk); /* wakeup blocked sndbuf producers */
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);
400 release_sock(&smc->sk);
403 void smc_close_sock_put_work(struct work_struct *work)
405 struct smc_sock *smc = container_of(to_delayed_work(work),
406 struct smc_sock,
407 sock_put_work);
409 smc->sk.sk_prot->unhash(&smc->sk);
410 sock_put(&smc->sk);
413 int smc_close_shutdown_write(struct smc_sock *smc)
415 struct smc_connection *conn = &smc->conn;
416 struct sock *sk = &smc->sk;
417 int old_state;
418 long timeout;
419 int rc = 0;
421 timeout = current->flags & PF_EXITING ?
422 0 : sock_flag(sk, SOCK_LINGER) ?
423 sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT;
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;
462 if (old_state != sk->sk_state)
463 sk->sk_state_change(&smc->sk);
464 return rc;
467 /* Initialize close properties on connection establishment. */
468 void smc_close_init(struct smc_sock *smc)
470 INIT_WORK(&smc->conn.close_work, smc_close_passive_work);