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