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 | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 103 | sk->sk_err = ECONNABORTED; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 104 | if (smc->clcsock && smc->clcsock->sk) { |
| 105 | smc->clcsock->sk->sk_err = ECONNABORTED; |
| 106 | smc->clcsock->sk->sk_state_change(smc->clcsock->sk); |
| 107 | } |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 108 | switch (sk->sk_state) { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 109 | case SMC_INIT: |
Ursula Braun | 46c28db | 2017-04-10 14:58:01 +0200 | [diff] [blame] | 110 | case SMC_ACTIVE: |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 111 | sk->sk_state = SMC_PEERABORTWAIT; |
Ursula Braun | 611b63a | 2018-01-25 11:15:31 +0100 | [diff] [blame] | 112 | release_sock(sk); |
| 113 | cancel_delayed_work_sync(&smc->conn.tx_work); |
| 114 | lock_sock(sk); |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 115 | sock_put(sk); /* passive closing */ |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 116 | break; |
| 117 | case SMC_APPCLOSEWAIT1: |
| 118 | case SMC_APPCLOSEWAIT2: |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 119 | if (!smc_cdc_rxed_any_close(&smc->conn)) |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 120 | sk->sk_state = SMC_PEERABORTWAIT; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 121 | else |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 122 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | 611b63a | 2018-01-25 11:15:31 +0100 | [diff] [blame] | 123 | release_sock(sk); |
| 124 | cancel_delayed_work_sync(&smc->conn.tx_work); |
| 125 | lock_sock(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 126 | break; |
| 127 | case SMC_PEERCLOSEWAIT1: |
| 128 | case SMC_PEERCLOSEWAIT2: |
| 129 | if (!txflags->peer_conn_closed) { |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 130 | /* just SHUTDOWN_SEND done */ |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 131 | sk->sk_state = SMC_PEERABORTWAIT; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 132 | } else { |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 133 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 134 | } |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 135 | sock_put(sk); /* passive closing */ |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 136 | break; |
| 137 | case SMC_PROCESSABORT: |
| 138 | case SMC_APPFINCLOSEWAIT: |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 139 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 140 | break; |
| 141 | case SMC_PEERFINCLOSEWAIT: |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 142 | sock_put(sk); /* passive closing */ |
| 143 | break; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 144 | case SMC_PEERABORTWAIT: |
| 145 | case SMC_CLOSED: |
| 146 | break; |
| 147 | } |
| 148 | |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 149 | sock_set_flag(sk, SOCK_DEAD); |
| 150 | sk->sk_state_change(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Ursula Braun | a98bf8c0 | 2017-04-10 14:58:02 +0200 | [diff] [blame] | 153 | static inline bool smc_close_sent_any_close(struct smc_connection *conn) |
| 154 | { |
| 155 | return conn->local_tx_ctrl.conn_state_flags.peer_conn_abort || |
| 156 | conn->local_tx_ctrl.conn_state_flags.peer_conn_closed; |
| 157 | } |
| 158 | |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 159 | int smc_close_active(struct smc_sock *smc) |
| 160 | { |
| 161 | struct smc_cdc_conn_state_flags *txflags = |
| 162 | &smc->conn.local_tx_ctrl.conn_state_flags; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 163 | struct smc_connection *conn = &smc->conn; |
| 164 | struct sock *sk = &smc->sk; |
| 165 | int old_state; |
Ursula Braun | 8c96fee | 2017-09-21 09:16:34 +0200 | [diff] [blame] | 166 | long timeout; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 167 | int rc = 0; |
| 168 | |
Ursula Braun | 8c96fee | 2017-09-21 09:16:34 +0200 | [diff] [blame] | 169 | timeout = current->flags & PF_EXITING ? |
| 170 | 0 : sock_flag(sk, SOCK_LINGER) ? |
| 171 | sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 172 | |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 173 | old_state = sk->sk_state; |
Ursula Braun | bbb96bf | 2018-01-24 10:28:16 +0100 | [diff] [blame] | 174 | again: |
| 175 | switch (sk->sk_state) { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 176 | case SMC_INIT: |
| 177 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 178 | break; |
| 179 | case SMC_LISTEN: |
| 180 | sk->sk_state = SMC_CLOSED; |
| 181 | sk->sk_state_change(sk); /* wake up accept */ |
| 182 | if (smc->clcsock && smc->clcsock->sk) { |
| 183 | rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR); |
| 184 | /* wake up kernel_accept of smc_tcp_listen_worker */ |
| 185 | smc->clcsock->sk->sk_data_ready(smc->clcsock->sk); |
| 186 | } |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 187 | smc_close_cleanup_listen(sk); |
Ursula Braun | 3d50206 | 2018-03-13 10:41:54 +0100 | [diff] [blame^] | 188 | release_sock(sk); |
| 189 | flush_work(&smc->tcp_listen_work); |
| 190 | lock_sock(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 191 | break; |
| 192 | case SMC_ACTIVE: |
| 193 | smc_close_stream_wait(smc, timeout); |
| 194 | release_sock(sk); |
Ursula Braun | 18e537c | 2017-09-21 09:16:33 +0200 | [diff] [blame] | 195 | cancel_delayed_work_sync(&conn->tx_work); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 196 | lock_sock(sk); |
| 197 | if (sk->sk_state == SMC_ACTIVE) { |
| 198 | /* send close request */ |
| 199 | rc = smc_close_final(conn); |
Ursula Braun | bbb96bf | 2018-01-24 10:28:16 +0100 | [diff] [blame] | 200 | if (rc) |
| 201 | break; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 202 | sk->sk_state = SMC_PEERCLOSEWAIT1; |
| 203 | } else { |
| 204 | /* peer event has changed the state */ |
| 205 | goto again; |
| 206 | } |
| 207 | break; |
| 208 | case SMC_APPFINCLOSEWAIT: |
| 209 | /* socket already shutdown wr or both (active close) */ |
| 210 | if (txflags->peer_done_writing && |
Ursula Braun | a98bf8c0 | 2017-04-10 14:58:02 +0200 | [diff] [blame] | 211 | !smc_close_sent_any_close(conn)) { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 212 | /* just shutdown wr done, send close request */ |
| 213 | rc = smc_close_final(conn); |
Ursula Braun | bbb96bf | 2018-01-24 10:28:16 +0100 | [diff] [blame] | 214 | if (rc) |
| 215 | break; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 216 | } |
| 217 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 218 | break; |
| 219 | case SMC_APPCLOSEWAIT1: |
| 220 | case SMC_APPCLOSEWAIT2: |
| 221 | if (!smc_cdc_rxed_any_close(conn)) |
| 222 | smc_close_stream_wait(smc, timeout); |
| 223 | release_sock(sk); |
Ursula Braun | 18e537c | 2017-09-21 09:16:33 +0200 | [diff] [blame] | 224 | cancel_delayed_work_sync(&conn->tx_work); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 225 | lock_sock(sk); |
Ursula Braun | bbb96bf | 2018-01-24 10:28:16 +0100 | [diff] [blame] | 226 | if (sk->sk_state != SMC_APPCLOSEWAIT1 && |
| 227 | sk->sk_state != SMC_APPCLOSEWAIT2) |
| 228 | goto again; |
| 229 | /* confirm close from peer */ |
| 230 | rc = smc_close_final(conn); |
| 231 | if (rc) |
| 232 | break; |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 233 | if (smc_cdc_rxed_any_close(conn)) { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 234 | /* peer has closed the socket already */ |
| 235 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 236 | sock_put(sk); /* postponed passive closing */ |
| 237 | } else { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 238 | /* peer has just issued a shutdown write */ |
| 239 | sk->sk_state = SMC_PEERFINCLOSEWAIT; |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 240 | } |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 241 | break; |
| 242 | case SMC_PEERCLOSEWAIT1: |
| 243 | case SMC_PEERCLOSEWAIT2: |
Ursula Braun | a98bf8c0 | 2017-04-10 14:58:02 +0200 | [diff] [blame] | 244 | if (txflags->peer_done_writing && |
| 245 | !smc_close_sent_any_close(conn)) { |
| 246 | /* just shutdown wr done, send close request */ |
| 247 | rc = smc_close_final(conn); |
Ursula Braun | bbb96bf | 2018-01-24 10:28:16 +0100 | [diff] [blame] | 248 | if (rc) |
| 249 | break; |
Ursula Braun | a98bf8c0 | 2017-04-10 14:58:02 +0200 | [diff] [blame] | 250 | } |
| 251 | /* peer sending PeerConnectionClosed will cause transition */ |
| 252 | break; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 253 | case SMC_PEERFINCLOSEWAIT: |
| 254 | /* peer sending PeerConnectionClosed will cause transition */ |
| 255 | break; |
| 256 | case SMC_PROCESSABORT: |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 257 | smc_close_abort(conn); |
| 258 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 259 | break; |
| 260 | case SMC_PEERABORTWAIT: |
| 261 | case SMC_CLOSED: |
| 262 | /* nothing to do, add tracing in future patch */ |
| 263 | break; |
| 264 | } |
| 265 | |
| 266 | if (old_state != sk->sk_state) |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 267 | sk->sk_state_change(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 268 | return rc; |
| 269 | } |
| 270 | |
| 271 | static void smc_close_passive_abort_received(struct smc_sock *smc) |
| 272 | { |
| 273 | struct smc_cdc_conn_state_flags *txflags = |
| 274 | &smc->conn.local_tx_ctrl.conn_state_flags; |
| 275 | struct sock *sk = &smc->sk; |
| 276 | |
| 277 | switch (sk->sk_state) { |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 278 | case SMC_INIT: |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 279 | case SMC_ACTIVE: |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 280 | case SMC_APPCLOSEWAIT1: |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 281 | sk->sk_state = SMC_PROCESSABORT; |
| 282 | sock_put(sk); /* passive closing */ |
| 283 | break; |
| 284 | case SMC_APPFINCLOSEWAIT: |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 285 | sk->sk_state = SMC_PROCESSABORT; |
| 286 | break; |
| 287 | case SMC_PEERCLOSEWAIT1: |
| 288 | case SMC_PEERCLOSEWAIT2: |
| 289 | if (txflags->peer_done_writing && |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 290 | !smc_close_sent_any_close(&smc->conn)) |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 291 | /* just shutdown, but not yet closed locally */ |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 292 | sk->sk_state = SMC_PROCESSABORT; |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 293 | else |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 294 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 295 | sock_put(sk); /* passive closing */ |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 296 | break; |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 297 | case SMC_APPCLOSEWAIT2: |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 298 | case SMC_PEERFINCLOSEWAIT: |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 299 | sk->sk_state = SMC_CLOSED; |
| 300 | sock_put(sk); /* passive closing */ |
| 301 | break; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 302 | case SMC_PEERABORTWAIT: |
| 303 | sk->sk_state = SMC_CLOSED; |
| 304 | break; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 305 | case SMC_PROCESSABORT: |
| 306 | /* nothing to do, add tracing in future patch */ |
| 307 | break; |
| 308 | } |
| 309 | } |
| 310 | |
Ursula Braun | 732720f | 2018-01-25 11:15:35 +0100 | [diff] [blame] | 311 | /* Either some kind of closing has been received: peer_conn_closed, |
| 312 | * peer_conn_abort, or peer_done_writing |
| 313 | * or the link group of the connection terminates abnormally. |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 314 | */ |
Ursula Braun | 46c28db | 2017-04-10 14:58:01 +0200 | [diff] [blame] | 315 | static void smc_close_passive_work(struct work_struct *work) |
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 | struct smc_connection *conn = container_of(work, |
| 318 | struct smc_connection, |
| 319 | close_work); |
| 320 | struct smc_sock *smc = container_of(conn, struct smc_sock, conn); |
| 321 | struct smc_cdc_conn_state_flags *rxflags; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 322 | struct sock *sk = &smc->sk; |
| 323 | int old_state; |
| 324 | |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 325 | lock_sock(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 326 | old_state = sk->sk_state; |
| 327 | |
Ursula Braun | 46c28db | 2017-04-10 14:58:01 +0200 | [diff] [blame] | 328 | if (!conn->alert_token_local) { |
| 329 | /* abnormal termination */ |
| 330 | smc_close_active_abort(smc); |
| 331 | goto wakeup; |
| 332 | } |
| 333 | |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 334 | rxflags = &conn->local_rx_ctrl.conn_state_flags; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 335 | if (rxflags->peer_conn_abort) { |
Ursula Braun | 732720f | 2018-01-25 11:15:35 +0100 | [diff] [blame] | 336 | /* peer has not received all data */ |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 337 | smc_close_passive_abort_received(smc); |
Ursula Braun | 611b63a | 2018-01-25 11:15:31 +0100 | [diff] [blame] | 338 | release_sock(&smc->sk); |
| 339 | cancel_delayed_work_sync(&conn->tx_work); |
| 340 | lock_sock(&smc->sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 341 | goto wakeup; |
| 342 | } |
| 343 | |
| 344 | switch (sk->sk_state) { |
| 345 | case SMC_INIT: |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 346 | if (atomic_read(&conn->bytes_to_rcv) || |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 347 | (rxflags->peer_done_writing && |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 348 | !smc_cdc_rxed_any_close(conn))) { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 349 | sk->sk_state = SMC_APPCLOSEWAIT1; |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 350 | } else { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 351 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 352 | sock_put(sk); /* passive closing */ |
| 353 | } |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 354 | break; |
| 355 | case SMC_ACTIVE: |
| 356 | sk->sk_state = SMC_APPCLOSEWAIT1; |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 357 | /* postpone sock_put() for passive closing to cover |
| 358 | * received SEND_SHUTDOWN as well |
| 359 | */ |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 360 | break; |
| 361 | case SMC_PEERCLOSEWAIT1: |
| 362 | if (rxflags->peer_done_writing) |
| 363 | sk->sk_state = SMC_PEERCLOSEWAIT2; |
Gustavo A. R. Silva | 7f6b437 | 2017-10-21 20:35:30 -0500 | [diff] [blame] | 364 | /* fall through */ |
| 365 | /* to check for closing */ |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 366 | case SMC_PEERCLOSEWAIT2: |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 367 | if (!smc_cdc_rxed_any_close(conn)) |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 368 | break; |
| 369 | if (sock_flag(sk, SOCK_DEAD) && |
Ursula Braun | a98bf8c0 | 2017-04-10 14:58:02 +0200 | [diff] [blame] | 370 | smc_close_sent_any_close(conn)) { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 371 | /* smc_release has already been called locally */ |
| 372 | sk->sk_state = SMC_CLOSED; |
| 373 | } else { |
| 374 | /* just shutdown, but not yet closed locally */ |
| 375 | sk->sk_state = SMC_APPFINCLOSEWAIT; |
| 376 | } |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 377 | sock_put(sk); /* passive closing */ |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 378 | break; |
Ursula Braun | 5ac92a0 | 2018-01-25 11:15:32 +0100 | [diff] [blame] | 379 | case SMC_PEERFINCLOSEWAIT: |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 380 | if (smc_cdc_rxed_any_close(conn)) { |
Ursula Braun | 5ac92a0 | 2018-01-25 11:15:32 +0100 | [diff] [blame] | 381 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 382 | sock_put(sk); /* passive closing */ |
| 383 | } |
Ursula Braun | 5ac92a0 | 2018-01-25 11:15:32 +0100 | [diff] [blame] | 384 | break; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 385 | case SMC_APPCLOSEWAIT1: |
| 386 | case SMC_APPCLOSEWAIT2: |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 387 | /* postpone sock_put() for passive closing to cover |
| 388 | * received SEND_SHUTDOWN as well |
| 389 | */ |
| 390 | break; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 391 | case SMC_APPFINCLOSEWAIT: |
| 392 | case SMC_PEERABORTWAIT: |
| 393 | case SMC_PROCESSABORT: |
| 394 | case SMC_CLOSED: |
| 395 | /* nothing to do, add tracing in future patch */ |
| 396 | break; |
| 397 | } |
| 398 | |
| 399 | wakeup: |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 400 | sk->sk_data_ready(sk); /* wakeup blocked rcvbuf consumers */ |
| 401 | sk->sk_write_space(sk); /* wakeup blocked sndbuf producers */ |
| 402 | |
Ursula Braun | a98bf8c0 | 2017-04-10 14:58:02 +0200 | [diff] [blame] | 403 | if (old_state != sk->sk_state) { |
| 404 | sk->sk_state_change(sk); |
| 405 | if ((sk->sk_state == SMC_CLOSED) && |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 406 | (sock_flag(sk, SOCK_DEAD) || !sk->sk_socket)) |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 407 | smc_conn_free(conn); |
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 | } |