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