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