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 | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 22 | static 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 Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 31 | /* wait for sndbuf data being transmitted */ |
| 32 | static 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 | |
| 60 | void 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 | |
| 67 | static 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 | |
| 74 | static 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 | |
| 84 | static 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 Braun | a8ae890 | 2017-12-07 13:38:44 +0100 | [diff] [blame] | 94 | static void smc_close_active_abort(struct smc_sock *smc) |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 95 | { |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 96 | struct sock *sk = &smc->sk; |
| 97 | |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 98 | struct smc_cdc_conn_state_flags *txflags = |
| 99 | &smc->conn.local_tx_ctrl.conn_state_flags; |
| 100 | |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 101 | sk->sk_err = ECONNABORTED; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 102 | 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 Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 106 | switch (sk->sk_state) { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 107 | case SMC_INIT: |
Ursula Braun | 46c28db | 2017-04-10 14:58:01 +0200 | [diff] [blame] | 108 | case SMC_ACTIVE: |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 109 | sk->sk_state = SMC_PEERABORTWAIT; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 110 | 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 Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 116 | sk->sk_state = SMC_PEERABORTWAIT; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 117 | else |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 118 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 119 | break; |
| 120 | case SMC_PEERCLOSEWAIT1: |
| 121 | case SMC_PEERCLOSEWAIT2: |
| 122 | if (!txflags->peer_conn_closed) { |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 123 | sk->sk_state = SMC_PEERABORTWAIT; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 124 | txflags->peer_conn_abort = 1; |
| 125 | sock_release(smc->clcsock); |
| 126 | } else { |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 127 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 128 | } |
| 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 Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 136 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 137 | break; |
| 138 | case SMC_PEERFINCLOSEWAIT: |
| 139 | case SMC_PEERABORTWAIT: |
| 140 | case SMC_CLOSED: |
| 141 | break; |
| 142 | } |
| 143 | |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 144 | sock_set_flag(sk, SOCK_DEAD); |
| 145 | sk->sk_state_change(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 146 | } |
| 147 | |
Ursula Braun | a98bf8c0 | 2017-04-10 14:58:02 +0200 | [diff] [blame] | 148 | static 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 Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 154 | int 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 Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 158 | struct smc_connection *conn = &smc->conn; |
| 159 | struct sock *sk = &smc->sk; |
| 160 | int old_state; |
Ursula Braun | 8c96fee | 2017-09-21 09:16:34 +0200 | [diff] [blame] | 161 | long timeout; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 162 | int rc = 0; |
| 163 | |
Ursula Braun | 8c96fee | 2017-09-21 09:16:34 +0200 | [diff] [blame] | 164 | timeout = current->flags & PF_EXITING ? |
| 165 | 0 : sock_flag(sk, SOCK_LINGER) ? |
| 166 | sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 167 | |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 168 | old_state = sk->sk_state; |
Ursula Braun | bbb96bf | 2018-01-24 10:28:16 +0100 | [diff] [blame^] | 169 | again: |
| 170 | switch (sk->sk_state) { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 171 | case SMC_INIT: |
| 172 | sk->sk_state = SMC_CLOSED; |
| 173 | if (smc->smc_listen_work.func) |
Ursula Braun | 46c28db | 2017-04-10 14:58:01 +0200 | [diff] [blame] | 174 | cancel_work_sync(&smc->smc_listen_work); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 175 | 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 Braun | 46c28db | 2017-04-10 14:58:01 +0200 | [diff] [blame] | 186 | cancel_work_sync(&smc->smc_listen_work); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 187 | lock_sock(sk); |
| 188 | break; |
| 189 | case SMC_ACTIVE: |
| 190 | smc_close_stream_wait(smc, timeout); |
| 191 | release_sock(sk); |
Ursula Braun | 18e537c | 2017-09-21 09:16:33 +0200 | [diff] [blame] | 192 | cancel_delayed_work_sync(&conn->tx_work); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 193 | lock_sock(sk); |
| 194 | if (sk->sk_state == SMC_ACTIVE) { |
| 195 | /* send close request */ |
| 196 | rc = smc_close_final(conn); |
Ursula Braun | bbb96bf | 2018-01-24 10:28:16 +0100 | [diff] [blame^] | 197 | if (rc) |
| 198 | break; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 199 | 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 Braun | a98bf8c0 | 2017-04-10 14:58:02 +0200 | [diff] [blame] | 208 | !smc_close_sent_any_close(conn)) { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 209 | /* just shutdown wr done, send close request */ |
| 210 | rc = smc_close_final(conn); |
Ursula Braun | bbb96bf | 2018-01-24 10:28:16 +0100 | [diff] [blame^] | 211 | if (rc) |
| 212 | break; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 213 | } |
| 214 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 215 | 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 Braun | 18e537c | 2017-09-21 09:16:33 +0200 | [diff] [blame] | 221 | cancel_delayed_work_sync(&conn->tx_work); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 222 | lock_sock(sk); |
Ursula Braun | bbb96bf | 2018-01-24 10:28:16 +0100 | [diff] [blame^] | 223 | 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 Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 230 | 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 Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 236 | break; |
| 237 | case SMC_PEERCLOSEWAIT1: |
| 238 | case SMC_PEERCLOSEWAIT2: |
Ursula Braun | a98bf8c0 | 2017-04-10 14:58:02 +0200 | [diff] [blame] | 239 | 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 Braun | bbb96bf | 2018-01-24 10:28:16 +0100 | [diff] [blame^] | 243 | if (rc) |
| 244 | break; |
Ursula Braun | a98bf8c0 | 2017-04-10 14:58:02 +0200 | [diff] [blame] | 245 | } |
| 246 | /* peer sending PeerConnectionClosed will cause transition */ |
| 247 | break; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 248 | case SMC_PEERFINCLOSEWAIT: |
| 249 | /* peer sending PeerConnectionClosed will cause transition */ |
| 250 | break; |
| 251 | case SMC_PROCESSABORT: |
Ursula Braun | 18e537c | 2017-09-21 09:16:33 +0200 | [diff] [blame] | 252 | release_sock(sk); |
| 253 | cancel_delayed_work_sync(&conn->tx_work); |
| 254 | lock_sock(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 255 | smc_close_abort(conn); |
| 256 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 257 | 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 Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 265 | sk->sk_state_change(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 266 | return rc; |
| 267 | } |
| 268 | |
| 269 | static 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 Braun | a98bf8c0 | 2017-04-10 14:58:02 +0200 | [diff] [blame] | 286 | !smc_close_sent_any_close(&smc->conn)) { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 287 | /* 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 Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 307 | */ |
Ursula Braun | 46c28db | 2017-04-10 14:58:01 +0200 | [diff] [blame] | 308 | static void smc_close_passive_work(struct work_struct *work) |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 309 | { |
Ursula Braun | 46c28db | 2017-04-10 14:58:01 +0200 | [diff] [blame] | 310 | 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 Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 315 | struct sock *sk = &smc->sk; |
| 316 | int old_state; |
| 317 | |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 318 | lock_sock(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 319 | old_state = sk->sk_state; |
| 320 | |
Ursula Braun | 46c28db | 2017-04-10 14:58:01 +0200 | [diff] [blame] | 321 | if (!conn->alert_token_local) { |
| 322 | /* abnormal termination */ |
| 323 | smc_close_active_abort(smc); |
| 324 | goto wakeup; |
| 325 | } |
| 326 | |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 327 | rxflags = &conn->local_rx_ctrl.conn_state_flags; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 328 | 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 Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 335 | if (atomic_read(&conn->bytes_to_rcv) || |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 336 | (rxflags->peer_done_writing && |
Ursula Braun | a98bf8c0 | 2017-04-10 14:58:02 +0200 | [diff] [blame] | 337 | !smc_cdc_rxed_any_close(conn))) |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 338 | 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. Silva | 7f6b437 | 2017-10-21 20:35:30 -0500 | [diff] [blame] | 348 | /* fall through */ |
| 349 | /* to check for closing */ |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 350 | case SMC_PEERCLOSEWAIT2: |
| 351 | case SMC_PEERFINCLOSEWAIT: |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 352 | if (!smc_cdc_rxed_any_close(conn)) |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 353 | break; |
| 354 | if (sock_flag(sk, SOCK_DEAD) && |
Ursula Braun | a98bf8c0 | 2017-04-10 14:58:02 +0200 | [diff] [blame] | 355 | smc_close_sent_any_close(conn)) { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 356 | /* 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 | |
| 373 | wakeup: |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 374 | sk->sk_data_ready(sk); /* wakeup blocked rcvbuf consumers */ |
| 375 | sk->sk_write_space(sk); /* wakeup blocked sndbuf producers */ |
| 376 | |
Ursula Braun | a98bf8c0 | 2017-04-10 14:58:02 +0200 | [diff] [blame] | 377 | 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 Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 381 | smc_conn_free(conn); |
Ursula Braun | a98bf8c0 | 2017-04-10 14:58:02 +0200 | [diff] [blame] | 382 | schedule_delayed_work(&smc->sock_put_work, |
| 383 | SMC_CLOSE_SOCK_PUT_DELAY); |
| 384 | } |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 385 | } |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 386 | release_sock(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | void 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 Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 395 | smc->sk.sk_prot->unhash(&smc->sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 396 | sock_put(&smc->sk); |
| 397 | } |
| 398 | |
| 399 | int smc_close_shutdown_write(struct smc_sock *smc) |
| 400 | { |
| 401 | struct smc_connection *conn = &smc->conn; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 402 | struct sock *sk = &smc->sk; |
| 403 | int old_state; |
Ursula Braun | 8c96fee | 2017-09-21 09:16:34 +0200 | [diff] [blame] | 404 | long timeout; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 405 | int rc = 0; |
| 406 | |
Ursula Braun | 8c96fee | 2017-09-21 09:16:34 +0200 | [diff] [blame] | 407 | timeout = current->flags & PF_EXITING ? |
| 408 | 0 : sock_flag(sk, SOCK_LINGER) ? |
| 409 | sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 410 | |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 411 | old_state = sk->sk_state; |
Ursula Braun | bbb96bf | 2018-01-24 10:28:16 +0100 | [diff] [blame^] | 412 | again: |
| 413 | switch (sk->sk_state) { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 414 | case SMC_ACTIVE: |
| 415 | smc_close_stream_wait(smc, timeout); |
| 416 | release_sock(sk); |
Ursula Braun | 18e537c | 2017-09-21 09:16:33 +0200 | [diff] [blame] | 417 | cancel_delayed_work_sync(&conn->tx_work); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 418 | lock_sock(sk); |
Ursula Braun | bbb96bf | 2018-01-24 10:28:16 +0100 | [diff] [blame^] | 419 | if (sk->sk_state != SMC_ACTIVE) |
| 420 | goto again; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 421 | /* send close wr request */ |
| 422 | rc = smc_close_wr(conn); |
Ursula Braun | bbb96bf | 2018-01-24 10:28:16 +0100 | [diff] [blame^] | 423 | if (rc) |
| 424 | break; |
| 425 | sk->sk_state = SMC_PEERCLOSEWAIT1; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 426 | 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 Braun | 18e537c | 2017-09-21 09:16:33 +0200 | [diff] [blame] | 432 | cancel_delayed_work_sync(&conn->tx_work); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 433 | lock_sock(sk); |
Ursula Braun | bbb96bf | 2018-01-24 10:28:16 +0100 | [diff] [blame^] | 434 | if (sk->sk_state != SMC_APPCLOSEWAIT1) |
| 435 | goto again; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 436 | /* confirm close from peer */ |
| 437 | rc = smc_close_wr(conn); |
Ursula Braun | bbb96bf | 2018-01-24 10:28:16 +0100 | [diff] [blame^] | 438 | if (rc) |
| 439 | break; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 440 | 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 Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 454 | sk->sk_state_change(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 455 | return rc; |
| 456 | } |
Ursula Braun | 46c28db | 2017-04-10 14:58:01 +0200 | [diff] [blame] | 457 | |
| 458 | /* Initialize close properties on connection establishment. */ |
| 459 | void smc_close_init(struct smc_sock *smc) |
| 460 | { |
| 461 | INIT_WORK(&smc->conn.close_work, smc_close_passive_work); |
| 462 | } |