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; |
Tony Lu | 00e158f | 2021-12-01 14:42:16 +0800 | [diff] [blame] | 198 | int rc1 = 0; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 199 | |
Ursula Braun | 8c96fee | 2017-09-21 09:16:34 +0200 | [diff] [blame] | 200 | timeout = current->flags & PF_EXITING ? |
| 201 | 0 : sock_flag(sk, SOCK_LINGER) ? |
| 202 | sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 203 | |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 204 | old_state = sk->sk_state; |
Ursula Braun | bbb96bf | 2018-01-24 10:28:16 +0100 | [diff] [blame] | 205 | again: |
| 206 | switch (sk->sk_state) { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 207 | case SMC_INIT: |
| 208 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 209 | break; |
| 210 | case SMC_LISTEN: |
| 211 | sk->sk_state = SMC_CLOSED; |
| 212 | sk->sk_state_change(sk); /* wake up accept */ |
Karsten Graul | ddcc9b7 | 2020-09-15 22:57:09 +0200 | [diff] [blame] | 213 | if (smc->clcsock && smc->clcsock->sk) { |
| 214 | smc->clcsock->sk->sk_data_ready = smc->clcsk_data_ready; |
| 215 | smc->clcsock->sk->sk_user_data = NULL; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 216 | rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR); |
Karsten Graul | ddcc9b7 | 2020-09-15 22:57:09 +0200 | [diff] [blame] | 217 | } |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 218 | smc_close_cleanup_listen(sk); |
Ursula Braun | 3d50206 | 2018-03-13 10:41:54 +0100 | [diff] [blame] | 219 | release_sock(sk); |
| 220 | flush_work(&smc->tcp_listen_work); |
| 221 | lock_sock(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 222 | break; |
| 223 | case SMC_ACTIVE: |
| 224 | smc_close_stream_wait(smc, timeout); |
| 225 | release_sock(sk); |
Ursula Braun | 18e537c | 2017-09-21 09:16:33 +0200 | [diff] [blame] | 226 | cancel_delayed_work_sync(&conn->tx_work); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 227 | lock_sock(sk); |
| 228 | if (sk->sk_state == SMC_ACTIVE) { |
| 229 | /* send close request */ |
| 230 | rc = smc_close_final(conn); |
| 231 | sk->sk_state = SMC_PEERCLOSEWAIT1; |
Tony Lu | 606a63c | 2021-11-23 16:25:18 +0800 | [diff] [blame] | 232 | |
| 233 | /* actively shutdown clcsock before peer close it, |
| 234 | * prevent peer from entering TIME_WAIT state. |
| 235 | */ |
Tony Lu | 00e158f | 2021-12-01 14:42:16 +0800 | [diff] [blame] | 236 | if (smc->clcsock && smc->clcsock->sk) { |
| 237 | rc1 = kernel_sock_shutdown(smc->clcsock, |
| 238 | SHUT_RDWR); |
| 239 | rc = rc ? rc : rc1; |
| 240 | } |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 241 | } else { |
| 242 | /* peer event has changed the state */ |
| 243 | goto again; |
| 244 | } |
| 245 | break; |
| 246 | case SMC_APPFINCLOSEWAIT: |
| 247 | /* socket already shutdown wr or both (active close) */ |
| 248 | if (txflags->peer_done_writing && |
Ursula Braun | a98bf8c0 | 2017-04-10 14:58:02 +0200 | [diff] [blame] | 249 | !smc_close_sent_any_close(conn)) { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 250 | /* just shutdown wr done, send close request */ |
| 251 | rc = smc_close_final(conn); |
| 252 | } |
| 253 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 254 | break; |
| 255 | case SMC_APPCLOSEWAIT1: |
| 256 | case SMC_APPCLOSEWAIT2: |
| 257 | if (!smc_cdc_rxed_any_close(conn)) |
| 258 | smc_close_stream_wait(smc, timeout); |
| 259 | release_sock(sk); |
Ursula Braun | 18e537c | 2017-09-21 09:16:33 +0200 | [diff] [blame] | 260 | cancel_delayed_work_sync(&conn->tx_work); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 261 | lock_sock(sk); |
Ursula Braun | bbb96bf | 2018-01-24 10:28:16 +0100 | [diff] [blame] | 262 | if (sk->sk_state != SMC_APPCLOSEWAIT1 && |
| 263 | sk->sk_state != SMC_APPCLOSEWAIT2) |
| 264 | goto again; |
| 265 | /* confirm close from peer */ |
| 266 | rc = smc_close_final(conn); |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 267 | if (smc_cdc_rxed_any_close(conn)) { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 268 | /* peer has closed the socket already */ |
| 269 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 270 | sock_put(sk); /* postponed passive closing */ |
| 271 | } else { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 272 | /* peer has just issued a shutdown write */ |
| 273 | sk->sk_state = SMC_PEERFINCLOSEWAIT; |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 274 | } |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 275 | break; |
| 276 | case SMC_PEERCLOSEWAIT1: |
| 277 | case SMC_PEERCLOSEWAIT2: |
Ursula Braun | a98bf8c0 | 2017-04-10 14:58:02 +0200 | [diff] [blame] | 278 | if (txflags->peer_done_writing && |
| 279 | !smc_close_sent_any_close(conn)) { |
| 280 | /* just shutdown wr done, send close request */ |
| 281 | rc = smc_close_final(conn); |
| 282 | } |
| 283 | /* peer sending PeerConnectionClosed will cause transition */ |
| 284 | break; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 285 | case SMC_PEERFINCLOSEWAIT: |
| 286 | /* peer sending PeerConnectionClosed will cause transition */ |
| 287 | break; |
| 288 | case SMC_PROCESSABORT: |
Ursula Braun | d18963c | 2019-10-09 10:07:47 +0200 | [diff] [blame] | 289 | rc = smc_close_abort(conn); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 290 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 291 | break; |
| 292 | case SMC_PEERABORTWAIT: |
Ursula Braun | d18963c | 2019-10-09 10:07:47 +0200 | [diff] [blame] | 293 | sk->sk_state = SMC_CLOSED; |
| 294 | break; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 295 | case SMC_CLOSED: |
| 296 | /* nothing to do, add tracing in future patch */ |
| 297 | break; |
| 298 | } |
| 299 | |
| 300 | if (old_state != sk->sk_state) |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 301 | sk->sk_state_change(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 302 | return rc; |
| 303 | } |
| 304 | |
| 305 | static void smc_close_passive_abort_received(struct smc_sock *smc) |
| 306 | { |
| 307 | struct smc_cdc_conn_state_flags *txflags = |
| 308 | &smc->conn.local_tx_ctrl.conn_state_flags; |
| 309 | struct sock *sk = &smc->sk; |
| 310 | |
| 311 | switch (sk->sk_state) { |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 312 | case SMC_INIT: |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 313 | case SMC_ACTIVE: |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 314 | case SMC_APPCLOSEWAIT1: |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 315 | sk->sk_state = SMC_PROCESSABORT; |
| 316 | sock_put(sk); /* passive closing */ |
| 317 | break; |
| 318 | case SMC_APPFINCLOSEWAIT: |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 319 | sk->sk_state = SMC_PROCESSABORT; |
| 320 | break; |
| 321 | case SMC_PEERCLOSEWAIT1: |
| 322 | case SMC_PEERCLOSEWAIT2: |
| 323 | if (txflags->peer_done_writing && |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 324 | !smc_close_sent_any_close(&smc->conn)) |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 325 | /* just shutdown, but not yet closed locally */ |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 326 | sk->sk_state = SMC_PROCESSABORT; |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 327 | else |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 328 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 329 | sock_put(sk); /* passive closing */ |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 330 | break; |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 331 | case SMC_APPCLOSEWAIT2: |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 332 | case SMC_PEERFINCLOSEWAIT: |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 333 | sk->sk_state = SMC_CLOSED; |
| 334 | sock_put(sk); /* passive closing */ |
| 335 | break; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 336 | case SMC_PEERABORTWAIT: |
| 337 | sk->sk_state = SMC_CLOSED; |
| 338 | break; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 339 | case SMC_PROCESSABORT: |
| 340 | /* nothing to do, add tracing in future patch */ |
| 341 | break; |
| 342 | } |
| 343 | } |
| 344 | |
Ursula Braun | 732720f | 2018-01-25 11:15:35 +0100 | [diff] [blame] | 345 | /* Either some kind of closing has been received: peer_conn_closed, |
| 346 | * peer_conn_abort, or peer_done_writing |
| 347 | * or the link group of the connection terminates abnormally. |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 348 | */ |
Ursula Braun | 46c28db | 2017-04-10 14:58:01 +0200 | [diff] [blame] | 349 | static void smc_close_passive_work(struct work_struct *work) |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 350 | { |
Ursula Braun | 46c28db | 2017-04-10 14:58:01 +0200 | [diff] [blame] | 351 | struct smc_connection *conn = container_of(work, |
| 352 | struct smc_connection, |
| 353 | close_work); |
| 354 | struct smc_sock *smc = container_of(conn, struct smc_sock, conn); |
| 355 | struct smc_cdc_conn_state_flags *rxflags; |
Karsten Graul | fd57770 | 2019-04-11 11:17:30 +0200 | [diff] [blame] | 356 | bool release_clcsock = false; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 357 | struct sock *sk = &smc->sk; |
| 358 | int old_state; |
| 359 | |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 360 | lock_sock(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 361 | old_state = sk->sk_state; |
| 362 | |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 363 | rxflags = &conn->local_rx_ctrl.conn_state_flags; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 364 | if (rxflags->peer_conn_abort) { |
Ursula Braun | 732720f | 2018-01-25 11:15:35 +0100 | [diff] [blame] | 365 | /* peer has not received all data */ |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 366 | smc_close_passive_abort_received(smc); |
Tony Lu | 45c3ff7 | 2021-11-23 16:25:16 +0800 | [diff] [blame] | 367 | release_sock(sk); |
Ursula Braun | 611b63a | 2018-01-25 11:15:31 +0100 | [diff] [blame] | 368 | cancel_delayed_work_sync(&conn->tx_work); |
Tony Lu | 45c3ff7 | 2021-11-23 16:25:16 +0800 | [diff] [blame] | 369 | lock_sock(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 370 | goto wakeup; |
| 371 | } |
| 372 | |
| 373 | switch (sk->sk_state) { |
| 374 | case SMC_INIT: |
Ursula Braun | 84b799a | 2019-02-04 13:44:47 +0100 | [diff] [blame] | 375 | sk->sk_state = SMC_APPCLOSEWAIT1; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 376 | break; |
| 377 | case SMC_ACTIVE: |
| 378 | sk->sk_state = SMC_APPCLOSEWAIT1; |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 379 | /* postpone sock_put() for passive closing to cover |
| 380 | * received SEND_SHUTDOWN as well |
| 381 | */ |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 382 | break; |
| 383 | case SMC_PEERCLOSEWAIT1: |
| 384 | if (rxflags->peer_done_writing) |
| 385 | sk->sk_state = SMC_PEERCLOSEWAIT2; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 386 | fallthrough; |
Gustavo A. R. Silva | 7f6b437 | 2017-10-21 20:35:30 -0500 | [diff] [blame] | 387 | /* to check for closing */ |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 388 | case SMC_PEERCLOSEWAIT2: |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 389 | if (!smc_cdc_rxed_any_close(conn)) |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 390 | break; |
| 391 | if (sock_flag(sk, SOCK_DEAD) && |
Ursula Braun | a98bf8c0 | 2017-04-10 14:58:02 +0200 | [diff] [blame] | 392 | smc_close_sent_any_close(conn)) { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 393 | /* smc_release has already been called locally */ |
| 394 | sk->sk_state = SMC_CLOSED; |
| 395 | } else { |
| 396 | /* just shutdown, but not yet closed locally */ |
| 397 | sk->sk_state = SMC_APPFINCLOSEWAIT; |
| 398 | } |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 399 | sock_put(sk); /* passive closing */ |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 400 | break; |
Ursula Braun | 5ac92a0 | 2018-01-25 11:15:32 +0100 | [diff] [blame] | 401 | case SMC_PEERFINCLOSEWAIT: |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 402 | if (smc_cdc_rxed_any_close(conn)) { |
Ursula Braun | 5ac92a0 | 2018-01-25 11:15:32 +0100 | [diff] [blame] | 403 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 404 | sock_put(sk); /* passive closing */ |
| 405 | } |
Ursula Braun | 5ac92a0 | 2018-01-25 11:15:32 +0100 | [diff] [blame] | 406 | break; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 407 | case SMC_APPCLOSEWAIT1: |
| 408 | case SMC_APPCLOSEWAIT2: |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 409 | /* postpone sock_put() for passive closing to cover |
| 410 | * received SEND_SHUTDOWN as well |
| 411 | */ |
| 412 | break; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 413 | case SMC_APPFINCLOSEWAIT: |
| 414 | case SMC_PEERABORTWAIT: |
| 415 | case SMC_PROCESSABORT: |
| 416 | case SMC_CLOSED: |
| 417 | /* nothing to do, add tracing in future patch */ |
| 418 | break; |
| 419 | } |
| 420 | |
| 421 | wakeup: |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 422 | sk->sk_data_ready(sk); /* wakeup blocked rcvbuf consumers */ |
| 423 | sk->sk_write_space(sk); /* wakeup blocked sndbuf producers */ |
| 424 | |
Ursula Braun | a98bf8c0 | 2017-04-10 14:58:02 +0200 | [diff] [blame] | 425 | if (old_state != sk->sk_state) { |
| 426 | sk->sk_state_change(sk); |
| 427 | if ((sk->sk_state == SMC_CLOSED) && |
Ursula Braun | b03faa1 | 2019-02-07 15:56:15 +0100 | [diff] [blame] | 428 | (sock_flag(sk, SOCK_DEAD) || !sk->sk_socket)) { |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 429 | smc_conn_free(conn); |
Karsten Graul | fd57770 | 2019-04-11 11:17:30 +0200 | [diff] [blame] | 430 | if (smc->clcsock) |
| 431 | release_clcsock = true; |
Ursula Braun | b03faa1 | 2019-02-07 15:56:15 +0100 | [diff] [blame] | 432 | } |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 433 | } |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 434 | release_sock(sk); |
Karsten Graul | fd57770 | 2019-04-11 11:17:30 +0200 | [diff] [blame] | 435 | if (release_clcsock) |
| 436 | smc_clcsock_release(smc); |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 437 | sock_put(sk); /* sock_hold done by schedulers of close_work */ |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | int smc_close_shutdown_write(struct smc_sock *smc) |
| 441 | { |
| 442 | struct smc_connection *conn = &smc->conn; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 443 | struct sock *sk = &smc->sk; |
| 444 | int old_state; |
Ursula Braun | 8c96fee | 2017-09-21 09:16:34 +0200 | [diff] [blame] | 445 | long timeout; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 446 | int rc = 0; |
| 447 | |
Ursula Braun | 8c96fee | 2017-09-21 09:16:34 +0200 | [diff] [blame] | 448 | timeout = current->flags & PF_EXITING ? |
| 449 | 0 : sock_flag(sk, SOCK_LINGER) ? |
| 450 | sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 451 | |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 452 | old_state = sk->sk_state; |
Ursula Braun | bbb96bf | 2018-01-24 10:28:16 +0100 | [diff] [blame] | 453 | again: |
| 454 | switch (sk->sk_state) { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 455 | case SMC_ACTIVE: |
| 456 | smc_close_stream_wait(smc, timeout); |
| 457 | release_sock(sk); |
Ursula Braun | 18e537c | 2017-09-21 09:16:33 +0200 | [diff] [blame] | 458 | cancel_delayed_work_sync(&conn->tx_work); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 459 | lock_sock(sk); |
Ursula Braun | bbb96bf | 2018-01-24 10:28:16 +0100 | [diff] [blame] | 460 | if (sk->sk_state != SMC_ACTIVE) |
| 461 | goto again; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 462 | /* send close wr request */ |
| 463 | rc = smc_close_wr(conn); |
Ursula Braun | bbb96bf | 2018-01-24 10:28:16 +0100 | [diff] [blame] | 464 | sk->sk_state = SMC_PEERCLOSEWAIT1; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 465 | break; |
| 466 | case SMC_APPCLOSEWAIT1: |
| 467 | /* passive close */ |
| 468 | if (!smc_cdc_rxed_any_close(conn)) |
| 469 | smc_close_stream_wait(smc, timeout); |
| 470 | release_sock(sk); |
Ursula Braun | 18e537c | 2017-09-21 09:16:33 +0200 | [diff] [blame] | 471 | cancel_delayed_work_sync(&conn->tx_work); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 472 | lock_sock(sk); |
Ursula Braun | bbb96bf | 2018-01-24 10:28:16 +0100 | [diff] [blame] | 473 | if (sk->sk_state != SMC_APPCLOSEWAIT1) |
| 474 | goto again; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 475 | /* confirm close from peer */ |
| 476 | rc = smc_close_wr(conn); |
| 477 | sk->sk_state = SMC_APPCLOSEWAIT2; |
| 478 | break; |
| 479 | case SMC_APPCLOSEWAIT2: |
| 480 | case SMC_PEERFINCLOSEWAIT: |
| 481 | case SMC_PEERCLOSEWAIT1: |
| 482 | case SMC_PEERCLOSEWAIT2: |
| 483 | case SMC_APPFINCLOSEWAIT: |
| 484 | case SMC_PROCESSABORT: |
| 485 | case SMC_PEERABORTWAIT: |
| 486 | /* nothing to do, add tracing in future patch */ |
| 487 | break; |
| 488 | } |
| 489 | |
| 490 | if (old_state != sk->sk_state) |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 491 | sk->sk_state_change(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 492 | return rc; |
| 493 | } |
Ursula Braun | 46c28db | 2017-04-10 14:58:01 +0200 | [diff] [blame] | 494 | |
| 495 | /* Initialize close properties on connection establishment. */ |
| 496 | void smc_close_init(struct smc_sock *smc) |
| 497 | { |
| 498 | INIT_WORK(&smc->conn.close_work, smc_close_passive_work); |
| 499 | } |