1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 /* 2 /* 3 * Shared Memory Communications over RDMA (SM 3 * Shared Memory Communications over RDMA (SMC-R) and RoCE 4 * 4 * 5 * Socket Closing - normal and abnormal 5 * Socket Closing - normal and abnormal 6 * 6 * 7 * Copyright IBM Corp. 2016 7 * Copyright IBM Corp. 2016 8 * 8 * 9 * Author(s): Ursula Braun <ubraun@linux.vne 9 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com> 10 */ 10 */ 11 11 12 #include <linux/workqueue.h> 12 #include <linux/workqueue.h> 13 #include <linux/sched/signal.h> 13 #include <linux/sched/signal.h> 14 14 15 #include <net/sock.h> 15 #include <net/sock.h> 16 #include <net/tcp.h> << 17 16 18 #include "smc.h" 17 #include "smc.h" 19 #include "smc_tx.h" 18 #include "smc_tx.h" 20 #include "smc_cdc.h" 19 #include "smc_cdc.h" 21 #include "smc_close.h" 20 #include "smc_close.h" 22 21 >> 22 #define SMC_CLOSE_WAIT_LISTEN_CLCSOCK_TIME (5 * HZ) >> 23 23 /* release the clcsock that is assigned to the 24 /* release the clcsock that is assigned to the smc_sock */ 24 void smc_clcsock_release(struct smc_sock *smc) 25 void smc_clcsock_release(struct smc_sock *smc) 25 { 26 { 26 struct socket *tcp; 27 struct socket *tcp; 27 28 28 if (smc->listen_smc && current_work() 29 if (smc->listen_smc && current_work() != &smc->smc_listen_work) 29 cancel_work_sync(&smc->smc_lis 30 cancel_work_sync(&smc->smc_listen_work); 30 mutex_lock(&smc->clcsock_release_lock) 31 mutex_lock(&smc->clcsock_release_lock); 31 if (smc->clcsock) { 32 if (smc->clcsock) { 32 tcp = smc->clcsock; 33 tcp = smc->clcsock; 33 smc->clcsock = NULL; 34 smc->clcsock = NULL; 34 sock_release(tcp); 35 sock_release(tcp); 35 } 36 } 36 mutex_unlock(&smc->clcsock_release_loc 37 mutex_unlock(&smc->clcsock_release_lock); 37 } 38 } 38 39 39 static void smc_close_cleanup_listen(struct so 40 static void smc_close_cleanup_listen(struct sock *parent) 40 { 41 { 41 struct sock *sk; 42 struct sock *sk; 42 43 43 /* Close non-accepted connections */ 44 /* Close non-accepted connections */ 44 while ((sk = smc_accept_dequeue(parent 45 while ((sk = smc_accept_dequeue(parent, NULL))) 45 smc_close_non_accepted(sk); 46 smc_close_non_accepted(sk); 46 } 47 } 47 48 48 /* wait for sndbuf data being transmitted */ 49 /* wait for sndbuf data being transmitted */ 49 static void smc_close_stream_wait(struct smc_s 50 static void smc_close_stream_wait(struct smc_sock *smc, long timeout) 50 { 51 { 51 DEFINE_WAIT_FUNC(wait, woken_wake_func 52 DEFINE_WAIT_FUNC(wait, woken_wake_function); 52 struct sock *sk = &smc->sk; 53 struct sock *sk = &smc->sk; 53 54 54 if (!timeout) 55 if (!timeout) 55 return; 56 return; 56 57 57 if (!smc_tx_prepared_sends(&smc->conn) 58 if (!smc_tx_prepared_sends(&smc->conn)) 58 return; 59 return; 59 60 60 /* Send out corked data remaining in s << 61 smc_tx_pending(&smc->conn); << 62 << 63 smc->wait_close_tx_prepared = 1; 61 smc->wait_close_tx_prepared = 1; 64 add_wait_queue(sk_sleep(sk), &wait); 62 add_wait_queue(sk_sleep(sk), &wait); 65 while (!signal_pending(current) && tim 63 while (!signal_pending(current) && timeout) { 66 int rc; 64 int rc; 67 65 68 rc = sk_wait_event(sk, &timeou 66 rc = sk_wait_event(sk, &timeout, 69 !smc_tx_pre 67 !smc_tx_prepared_sends(&smc->conn) || 70 READ_ONCE(s !! 68 (sk->sk_err == ECONNABORTED) || 71 READ_ONCE(s !! 69 (sk->sk_err == ECONNRESET), 72 smc->conn.k << 73 &wait); 70 &wait); 74 if (rc) 71 if (rc) 75 break; 72 break; 76 } 73 } 77 remove_wait_queue(sk_sleep(sk), &wait) 74 remove_wait_queue(sk_sleep(sk), &wait); 78 smc->wait_close_tx_prepared = 0; 75 smc->wait_close_tx_prepared = 0; 79 } 76 } 80 77 81 void smc_close_wake_tx_prepared(struct smc_soc 78 void smc_close_wake_tx_prepared(struct smc_sock *smc) 82 { 79 { 83 if (smc->wait_close_tx_prepared) 80 if (smc->wait_close_tx_prepared) 84 /* wake up socket closing */ 81 /* wake up socket closing */ 85 smc->sk.sk_state_change(&smc-> 82 smc->sk.sk_state_change(&smc->sk); 86 } 83 } 87 84 88 static int smc_close_wr(struct smc_connection 85 static int smc_close_wr(struct smc_connection *conn) 89 { 86 { 90 conn->local_tx_ctrl.conn_state_flags.p 87 conn->local_tx_ctrl.conn_state_flags.peer_done_writing = 1; 91 88 92 return smc_cdc_get_slot_and_msg_send(c 89 return smc_cdc_get_slot_and_msg_send(conn); 93 } 90 } 94 91 95 static int smc_close_final(struct smc_connecti 92 static int smc_close_final(struct smc_connection *conn) 96 { 93 { 97 if (atomic_read(&conn->bytes_to_rcv)) 94 if (atomic_read(&conn->bytes_to_rcv)) 98 conn->local_tx_ctrl.conn_state 95 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1; 99 else 96 else 100 conn->local_tx_ctrl.conn_state 97 conn->local_tx_ctrl.conn_state_flags.peer_conn_closed = 1; 101 if (conn->killed) << 102 return -EPIPE; << 103 98 104 return smc_cdc_get_slot_and_msg_send(c 99 return smc_cdc_get_slot_and_msg_send(conn); 105 } 100 } 106 101 107 int smc_close_abort(struct smc_connection *con !! 102 static int smc_close_abort(struct smc_connection *conn) 108 { 103 { 109 conn->local_tx_ctrl.conn_state_flags.p 104 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1; 110 105 111 return smc_cdc_get_slot_and_msg_send(c 106 return smc_cdc_get_slot_and_msg_send(conn); 112 } 107 } 113 108 114 static void smc_close_cancel_work(struct smc_s << 115 { << 116 struct sock *sk = &smc->sk; << 117 << 118 release_sock(sk); << 119 if (cancel_work_sync(&smc->conn.close_ << 120 sock_put(sk); << 121 cancel_delayed_work_sync(&smc->conn.tx << 122 lock_sock(sk); << 123 } << 124 << 125 /* terminate smc socket abnormally - active ab 109 /* terminate smc socket abnormally - active abort 126 * link group is terminated, i.e. RDMA communi 110 * link group is terminated, i.e. RDMA communication no longer possible 127 */ 111 */ 128 void smc_close_active_abort(struct smc_sock *s !! 112 static void smc_close_active_abort(struct smc_sock *smc) 129 { 113 { 130 struct sock *sk = &smc->sk; 114 struct sock *sk = &smc->sk; 131 bool release_clcsock = false; !! 115 >> 116 struct smc_cdc_conn_state_flags *txflags = >> 117 &smc->conn.local_tx_ctrl.conn_state_flags; 132 118 133 if (sk->sk_state != SMC_INIT && smc->c 119 if (sk->sk_state != SMC_INIT && smc->clcsock && smc->clcsock->sk) { 134 sk->sk_err = ECONNABORTED; 120 sk->sk_err = ECONNABORTED; 135 if (smc->clcsock && smc->clcso !! 121 if (smc->clcsock && smc->clcsock->sk) { 136 tcp_abort(smc->clcsock !! 122 smc->clcsock->sk->sk_err = ECONNABORTED; >> 123 smc->clcsock->sk->sk_state_change(smc->clcsock->sk); >> 124 } 137 } 125 } 138 switch (sk->sk_state) { 126 switch (sk->sk_state) { 139 case SMC_ACTIVE: 127 case SMC_ACTIVE: >> 128 sk->sk_state = SMC_PEERABORTWAIT; >> 129 release_sock(sk); >> 130 cancel_delayed_work_sync(&smc->conn.tx_work); >> 131 lock_sock(sk); >> 132 sock_put(sk); /* passive closing */ >> 133 break; 140 case SMC_APPCLOSEWAIT1: 134 case SMC_APPCLOSEWAIT1: 141 case SMC_APPCLOSEWAIT2: 135 case SMC_APPCLOSEWAIT2: 142 sk->sk_state = SMC_PEERABORTWA !! 136 if (!smc_cdc_rxed_any_close(&smc->conn)) 143 smc_close_cancel_work(smc); !! 137 sk->sk_state = SMC_PEERABORTWAIT; 144 if (sk->sk_state != SMC_PEERAB !! 138 else 145 break; !! 139 sk->sk_state = SMC_CLOSED; 146 sk->sk_state = SMC_CLOSED; !! 140 release_sock(sk); 147 sock_put(sk); /* (postponed) p !! 141 cancel_delayed_work_sync(&smc->conn.tx_work); >> 142 lock_sock(sk); 148 break; 143 break; 149 case SMC_PEERCLOSEWAIT1: 144 case SMC_PEERCLOSEWAIT1: 150 case SMC_PEERCLOSEWAIT2: 145 case SMC_PEERCLOSEWAIT2: 151 case SMC_PEERFINCLOSEWAIT: !! 146 if (!txflags->peer_conn_closed) { 152 sk->sk_state = SMC_PEERABORTWA !! 147 /* just SHUTDOWN_SEND done */ 153 smc_close_cancel_work(smc); !! 148 sk->sk_state = SMC_PEERABORTWAIT; 154 if (sk->sk_state != SMC_PEERAB !! 149 } else { 155 break; !! 150 sk->sk_state = SMC_CLOSED; 156 sk->sk_state = SMC_CLOSED; !! 151 } 157 smc_conn_free(&smc->conn); << 158 release_clcsock = true; << 159 sock_put(sk); /* passive closi 152 sock_put(sk); /* passive closing */ 160 break; 153 break; 161 case SMC_PROCESSABORT: 154 case SMC_PROCESSABORT: 162 case SMC_APPFINCLOSEWAIT: 155 case SMC_APPFINCLOSEWAIT: 163 sk->sk_state = SMC_PEERABORTWA << 164 smc_close_cancel_work(smc); << 165 if (sk->sk_state != SMC_PEERAB << 166 break; << 167 sk->sk_state = SMC_CLOSED; 156 sk->sk_state = SMC_CLOSED; 168 smc_conn_free(&smc->conn); !! 157 break; 169 release_clcsock = true; !! 158 case SMC_PEERFINCLOSEWAIT: >> 159 sock_put(sk); /* passive closing */ 170 break; 160 break; 171 case SMC_INIT: 161 case SMC_INIT: 172 case SMC_PEERABORTWAIT: 162 case SMC_PEERABORTWAIT: 173 case SMC_CLOSED: 163 case SMC_CLOSED: 174 break; 164 break; 175 } 165 } 176 166 177 smc_sock_set_flag(sk, SOCK_DEAD); 167 smc_sock_set_flag(sk, SOCK_DEAD); 178 sk->sk_state_change(sk); 168 sk->sk_state_change(sk); 179 << 180 if (release_clcsock) { << 181 release_sock(sk); << 182 smc_clcsock_release(smc); << 183 lock_sock(sk); << 184 } << 185 } 169 } 186 170 187 static inline bool smc_close_sent_any_close(st 171 static inline bool smc_close_sent_any_close(struct smc_connection *conn) 188 { 172 { 189 return conn->local_tx_ctrl.conn_state_ 173 return conn->local_tx_ctrl.conn_state_flags.peer_conn_abort || 190 conn->local_tx_ctrl.conn_state_ 174 conn->local_tx_ctrl.conn_state_flags.peer_conn_closed; 191 } 175 } 192 176 193 int smc_close_active(struct smc_sock *smc) 177 int smc_close_active(struct smc_sock *smc) 194 { 178 { 195 struct smc_cdc_conn_state_flags *txfla 179 struct smc_cdc_conn_state_flags *txflags = 196 &smc->conn.local_tx_ctrl.conn_ 180 &smc->conn.local_tx_ctrl.conn_state_flags; 197 struct smc_connection *conn = &smc->co 181 struct smc_connection *conn = &smc->conn; 198 struct sock *sk = &smc->sk; 182 struct sock *sk = &smc->sk; 199 int old_state; 183 int old_state; 200 long timeout; 184 long timeout; 201 int rc = 0; 185 int rc = 0; 202 int rc1 = 0; 186 int rc1 = 0; 203 187 204 timeout = current->flags & PF_EXITING 188 timeout = current->flags & PF_EXITING ? 205 0 : sock_flag(sk, SOCK_LINGE 189 0 : sock_flag(sk, SOCK_LINGER) ? 206 sk->sk_lingertime : SMC_ 190 sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT; 207 191 208 old_state = sk->sk_state; 192 old_state = sk->sk_state; 209 again: 193 again: 210 switch (sk->sk_state) { 194 switch (sk->sk_state) { 211 case SMC_INIT: 195 case SMC_INIT: 212 sk->sk_state = SMC_CLOSED; 196 sk->sk_state = SMC_CLOSED; 213 break; 197 break; 214 case SMC_LISTEN: 198 case SMC_LISTEN: 215 sk->sk_state = SMC_CLOSED; 199 sk->sk_state = SMC_CLOSED; 216 sk->sk_state_change(sk); /* wa 200 sk->sk_state_change(sk); /* wake up accept */ 217 if (smc->clcsock && smc->clcso 201 if (smc->clcsock && smc->clcsock->sk) { 218 write_lock_bh(&smc->cl << 219 smc_clcsock_restore_cb << 220 << 221 smc->clcsock->sk->sk_u << 222 write_unlock_bh(&smc-> << 223 rc = kernel_sock_shutd 202 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR); >> 203 /* wake up kernel_accept of smc_tcp_listen_worker */ >> 204 smc->clcsock->sk->sk_data_ready(smc->clcsock->sk); 224 } 205 } 225 smc_close_cleanup_listen(sk); 206 smc_close_cleanup_listen(sk); 226 release_sock(sk); 207 release_sock(sk); 227 flush_work(&smc->tcp_listen_wo 208 flush_work(&smc->tcp_listen_work); 228 lock_sock(sk); 209 lock_sock(sk); 229 break; 210 break; 230 case SMC_ACTIVE: 211 case SMC_ACTIVE: 231 smc_close_stream_wait(smc, tim 212 smc_close_stream_wait(smc, timeout); 232 release_sock(sk); 213 release_sock(sk); 233 cancel_delayed_work_sync(&conn 214 cancel_delayed_work_sync(&conn->tx_work); 234 lock_sock(sk); 215 lock_sock(sk); 235 if (sk->sk_state == SMC_ACTIVE 216 if (sk->sk_state == SMC_ACTIVE) { 236 /* send close request 217 /* send close request */ 237 rc = smc_close_final(c 218 rc = smc_close_final(conn); >> 219 if (rc) >> 220 break; 238 sk->sk_state = SMC_PEE 221 sk->sk_state = SMC_PEERCLOSEWAIT1; 239 222 240 /* actively shutdown c 223 /* actively shutdown clcsock before peer close it, 241 * prevent peer from e 224 * prevent peer from entering TIME_WAIT state. 242 */ 225 */ 243 if (smc->clcsock && sm 226 if (smc->clcsock && smc->clcsock->sk) { 244 rc1 = kernel_s 227 rc1 = kernel_sock_shutdown(smc->clcsock, 245 228 SHUT_RDWR); 246 rc = rc ? rc : 229 rc = rc ? rc : rc1; 247 } 230 } 248 } else { 231 } else { 249 /* peer event has chan 232 /* peer event has changed the state */ 250 goto again; 233 goto again; 251 } 234 } 252 break; 235 break; 253 case SMC_APPFINCLOSEWAIT: 236 case SMC_APPFINCLOSEWAIT: 254 /* socket already shutdown wr 237 /* socket already shutdown wr or both (active close) */ 255 if (txflags->peer_done_writing 238 if (txflags->peer_done_writing && 256 !smc_close_sent_any_close( 239 !smc_close_sent_any_close(conn)) { 257 /* just shutdown wr do 240 /* just shutdown wr done, send close request */ 258 rc = smc_close_final(c 241 rc = smc_close_final(conn); >> 242 if (rc) >> 243 break; 259 } 244 } 260 sk->sk_state = SMC_CLOSED; 245 sk->sk_state = SMC_CLOSED; 261 break; 246 break; 262 case SMC_APPCLOSEWAIT1: 247 case SMC_APPCLOSEWAIT1: 263 case SMC_APPCLOSEWAIT2: 248 case SMC_APPCLOSEWAIT2: 264 if (!smc_cdc_rxed_any_close(co 249 if (!smc_cdc_rxed_any_close(conn)) 265 smc_close_stream_wait( 250 smc_close_stream_wait(smc, timeout); 266 release_sock(sk); 251 release_sock(sk); 267 cancel_delayed_work_sync(&conn 252 cancel_delayed_work_sync(&conn->tx_work); 268 lock_sock(sk); 253 lock_sock(sk); 269 if (sk->sk_state != SMC_APPCLO 254 if (sk->sk_state != SMC_APPCLOSEWAIT1 && 270 sk->sk_state != SMC_APPCLO 255 sk->sk_state != SMC_APPCLOSEWAIT2) 271 goto again; 256 goto again; 272 /* confirm close from peer */ 257 /* confirm close from peer */ 273 rc = smc_close_final(conn); 258 rc = smc_close_final(conn); >> 259 if (rc) >> 260 break; 274 if (smc_cdc_rxed_any_close(con 261 if (smc_cdc_rxed_any_close(conn)) { 275 /* peer has closed the 262 /* peer has closed the socket already */ 276 sk->sk_state = SMC_CLO 263 sk->sk_state = SMC_CLOSED; 277 sock_put(sk); /* postp 264 sock_put(sk); /* postponed passive closing */ 278 } else { 265 } else { 279 /* peer has just issue 266 /* peer has just issued a shutdown write */ 280 sk->sk_state = SMC_PEE 267 sk->sk_state = SMC_PEERFINCLOSEWAIT; 281 } 268 } 282 break; 269 break; 283 case SMC_PEERCLOSEWAIT1: 270 case SMC_PEERCLOSEWAIT1: 284 case SMC_PEERCLOSEWAIT2: 271 case SMC_PEERCLOSEWAIT2: 285 if (txflags->peer_done_writing 272 if (txflags->peer_done_writing && 286 !smc_close_sent_any_close( 273 !smc_close_sent_any_close(conn)) { 287 /* just shutdown wr do 274 /* just shutdown wr done, send close request */ 288 rc = smc_close_final(c 275 rc = smc_close_final(conn); >> 276 if (rc) >> 277 break; 289 } 278 } 290 /* peer sending PeerConnection 279 /* peer sending PeerConnectionClosed will cause transition */ 291 break; 280 break; 292 case SMC_PEERFINCLOSEWAIT: 281 case SMC_PEERFINCLOSEWAIT: 293 /* peer sending PeerConnection 282 /* peer sending PeerConnectionClosed will cause transition */ 294 break; 283 break; 295 case SMC_PROCESSABORT: 284 case SMC_PROCESSABORT: 296 rc = smc_close_abort(conn); !! 285 smc_close_abort(conn); 297 sk->sk_state = SMC_CLOSED; 286 sk->sk_state = SMC_CLOSED; 298 break; 287 break; 299 case SMC_PEERABORTWAIT: 288 case SMC_PEERABORTWAIT: 300 sk->sk_state = SMC_CLOSED; << 301 break; << 302 case SMC_CLOSED: 289 case SMC_CLOSED: 303 /* nothing to do, add tracing 290 /* nothing to do, add tracing in future patch */ 304 break; 291 break; 305 } 292 } 306 293 307 if (old_state != sk->sk_state) 294 if (old_state != sk->sk_state) 308 sk->sk_state_change(sk); 295 sk->sk_state_change(sk); 309 return rc; 296 return rc; 310 } 297 } 311 298 312 static void smc_close_passive_abort_received(s 299 static void smc_close_passive_abort_received(struct smc_sock *smc) 313 { 300 { 314 struct smc_cdc_conn_state_flags *txfla 301 struct smc_cdc_conn_state_flags *txflags = 315 &smc->conn.local_tx_ctrl.conn_ 302 &smc->conn.local_tx_ctrl.conn_state_flags; 316 struct sock *sk = &smc->sk; 303 struct sock *sk = &smc->sk; 317 304 318 switch (sk->sk_state) { 305 switch (sk->sk_state) { 319 case SMC_INIT: 306 case SMC_INIT: 320 case SMC_ACTIVE: 307 case SMC_ACTIVE: 321 case SMC_APPCLOSEWAIT1: 308 case SMC_APPCLOSEWAIT1: 322 sk->sk_state = SMC_PROCESSABOR 309 sk->sk_state = SMC_PROCESSABORT; 323 sock_put(sk); /* passive closi 310 sock_put(sk); /* passive closing */ 324 break; 311 break; 325 case SMC_APPFINCLOSEWAIT: 312 case SMC_APPFINCLOSEWAIT: 326 sk->sk_state = SMC_PROCESSABOR 313 sk->sk_state = SMC_PROCESSABORT; 327 break; 314 break; 328 case SMC_PEERCLOSEWAIT1: 315 case SMC_PEERCLOSEWAIT1: 329 case SMC_PEERCLOSEWAIT2: 316 case SMC_PEERCLOSEWAIT2: 330 if (txflags->peer_done_writing 317 if (txflags->peer_done_writing && 331 !smc_close_sent_any_close( 318 !smc_close_sent_any_close(&smc->conn)) 332 /* just shutdown, but 319 /* just shutdown, but not yet closed locally */ 333 sk->sk_state = SMC_PRO 320 sk->sk_state = SMC_PROCESSABORT; 334 else 321 else 335 sk->sk_state = SMC_CLO 322 sk->sk_state = SMC_CLOSED; 336 sock_put(sk); /* passive closi 323 sock_put(sk); /* passive closing */ 337 break; 324 break; 338 case SMC_APPCLOSEWAIT2: 325 case SMC_APPCLOSEWAIT2: 339 case SMC_PEERFINCLOSEWAIT: 326 case SMC_PEERFINCLOSEWAIT: 340 sk->sk_state = SMC_CLOSED; 327 sk->sk_state = SMC_CLOSED; 341 sock_put(sk); /* passive closi 328 sock_put(sk); /* passive closing */ 342 break; 329 break; 343 case SMC_PEERABORTWAIT: 330 case SMC_PEERABORTWAIT: 344 sk->sk_state = SMC_CLOSED; 331 sk->sk_state = SMC_CLOSED; 345 break; 332 break; 346 case SMC_PROCESSABORT: 333 case SMC_PROCESSABORT: 347 /* nothing to do, add tracing in futur 334 /* nothing to do, add tracing in future patch */ 348 break; 335 break; 349 } 336 } 350 } 337 } 351 338 352 /* Either some kind of closing has been receiv 339 /* Either some kind of closing has been received: peer_conn_closed, 353 * peer_conn_abort, or peer_done_writing 340 * peer_conn_abort, or peer_done_writing 354 * or the link group of the connection termina 341 * or the link group of the connection terminates abnormally. 355 */ 342 */ 356 static void smc_close_passive_work(struct work 343 static void smc_close_passive_work(struct work_struct *work) 357 { 344 { 358 struct smc_connection *conn = containe 345 struct smc_connection *conn = container_of(work, 359 346 struct smc_connection, 360 347 close_work); 361 struct smc_sock *smc = container_of(co 348 struct smc_sock *smc = container_of(conn, struct smc_sock, conn); 362 struct smc_cdc_conn_state_flags *rxfla 349 struct smc_cdc_conn_state_flags *rxflags; 363 bool release_clcsock = false; 350 bool release_clcsock = false; 364 struct sock *sk = &smc->sk; 351 struct sock *sk = &smc->sk; 365 int old_state; 352 int old_state; 366 353 367 lock_sock(sk); 354 lock_sock(sk); 368 old_state = sk->sk_state; 355 old_state = sk->sk_state; 369 356 >> 357 if (!conn->alert_token_local) { >> 358 /* abnormal termination */ >> 359 smc_close_active_abort(smc); >> 360 goto wakeup; >> 361 } >> 362 370 rxflags = &conn->local_rx_ctrl.conn_st 363 rxflags = &conn->local_rx_ctrl.conn_state_flags; 371 if (rxflags->peer_conn_abort) { 364 if (rxflags->peer_conn_abort) { 372 /* peer has not received all d 365 /* peer has not received all data */ 373 smc_close_passive_abort_receiv 366 smc_close_passive_abort_received(smc); 374 release_sock(sk); !! 367 release_sock(&smc->sk); 375 cancel_delayed_work_sync(&conn 368 cancel_delayed_work_sync(&conn->tx_work); 376 lock_sock(sk); !! 369 lock_sock(&smc->sk); 377 goto wakeup; 370 goto wakeup; 378 } 371 } 379 372 380 switch (sk->sk_state) { 373 switch (sk->sk_state) { 381 case SMC_INIT: 374 case SMC_INIT: 382 sk->sk_state = SMC_APPCLOSEWAI 375 sk->sk_state = SMC_APPCLOSEWAIT1; 383 break; 376 break; 384 case SMC_ACTIVE: 377 case SMC_ACTIVE: 385 sk->sk_state = SMC_APPCLOSEWAI 378 sk->sk_state = SMC_APPCLOSEWAIT1; 386 /* postpone sock_put() for pas 379 /* postpone sock_put() for passive closing to cover 387 * received SEND_SHUTDOWN as w 380 * received SEND_SHUTDOWN as well 388 */ 381 */ 389 break; 382 break; 390 case SMC_PEERCLOSEWAIT1: 383 case SMC_PEERCLOSEWAIT1: 391 if (rxflags->peer_done_writing 384 if (rxflags->peer_done_writing) 392 sk->sk_state = SMC_PEE 385 sk->sk_state = SMC_PEERCLOSEWAIT2; 393 fallthrough; !! 386 /* fall through */ 394 /* to check for closing */ 387 /* to check for closing */ 395 case SMC_PEERCLOSEWAIT2: 388 case SMC_PEERCLOSEWAIT2: 396 if (!smc_cdc_rxed_any_close(co 389 if (!smc_cdc_rxed_any_close(conn)) 397 break; 390 break; 398 if (sock_flag(sk, SOCK_DEAD) & 391 if (sock_flag(sk, SOCK_DEAD) && 399 smc_close_sent_any_close(c 392 smc_close_sent_any_close(conn)) { 400 /* smc_release has alr 393 /* smc_release has already been called locally */ 401 sk->sk_state = SMC_CLO 394 sk->sk_state = SMC_CLOSED; 402 } else { 395 } else { 403 /* just shutdown, but 396 /* just shutdown, but not yet closed locally */ 404 sk->sk_state = SMC_APP 397 sk->sk_state = SMC_APPFINCLOSEWAIT; 405 } 398 } 406 sock_put(sk); /* passive closi 399 sock_put(sk); /* passive closing */ 407 break; 400 break; 408 case SMC_PEERFINCLOSEWAIT: 401 case SMC_PEERFINCLOSEWAIT: 409 if (smc_cdc_rxed_any_close(con 402 if (smc_cdc_rxed_any_close(conn)) { 410 sk->sk_state = SMC_CLO 403 sk->sk_state = SMC_CLOSED; 411 sock_put(sk); /* passi 404 sock_put(sk); /* passive closing */ 412 } 405 } 413 break; 406 break; 414 case SMC_APPCLOSEWAIT1: 407 case SMC_APPCLOSEWAIT1: 415 case SMC_APPCLOSEWAIT2: 408 case SMC_APPCLOSEWAIT2: 416 /* postpone sock_put() for pas 409 /* postpone sock_put() for passive closing to cover 417 * received SEND_SHUTDOWN as w 410 * received SEND_SHUTDOWN as well 418 */ 411 */ 419 break; 412 break; 420 case SMC_APPFINCLOSEWAIT: 413 case SMC_APPFINCLOSEWAIT: 421 case SMC_PEERABORTWAIT: 414 case SMC_PEERABORTWAIT: 422 case SMC_PROCESSABORT: 415 case SMC_PROCESSABORT: 423 case SMC_CLOSED: 416 case SMC_CLOSED: 424 /* nothing to do, add tracing 417 /* nothing to do, add tracing in future patch */ 425 break; 418 break; 426 } 419 } 427 420 428 wakeup: 421 wakeup: 429 sk->sk_data_ready(sk); /* wakeup block 422 sk->sk_data_ready(sk); /* wakeup blocked rcvbuf consumers */ 430 sk->sk_write_space(sk); /* wakeup bloc 423 sk->sk_write_space(sk); /* wakeup blocked sndbuf producers */ 431 424 432 if (old_state != sk->sk_state) { 425 if (old_state != sk->sk_state) { 433 sk->sk_state_change(sk); 426 sk->sk_state_change(sk); 434 if ((sk->sk_state == SMC_CLOSE 427 if ((sk->sk_state == SMC_CLOSED) && 435 (sock_flag(sk, SOCK_DEAD) 428 (sock_flag(sk, SOCK_DEAD) || !sk->sk_socket)) { 436 smc_conn_free(conn); 429 smc_conn_free(conn); 437 if (smc->clcsock) 430 if (smc->clcsock) 438 release_clcsoc 431 release_clcsock = true; 439 } 432 } 440 } 433 } 441 release_sock(sk); 434 release_sock(sk); 442 if (release_clcsock) 435 if (release_clcsock) 443 smc_clcsock_release(smc); 436 smc_clcsock_release(smc); 444 sock_put(sk); /* sock_hold done by sch 437 sock_put(sk); /* sock_hold done by schedulers of close_work */ 445 } 438 } 446 439 447 int smc_close_shutdown_write(struct smc_sock * 440 int smc_close_shutdown_write(struct smc_sock *smc) 448 { 441 { 449 struct smc_connection *conn = &smc->co 442 struct smc_connection *conn = &smc->conn; 450 struct sock *sk = &smc->sk; 443 struct sock *sk = &smc->sk; 451 int old_state; 444 int old_state; 452 long timeout; 445 long timeout; 453 int rc = 0; 446 int rc = 0; 454 447 455 timeout = current->flags & PF_EXITING 448 timeout = current->flags & PF_EXITING ? 456 0 : sock_flag(sk, SOCK_LINGE 449 0 : sock_flag(sk, SOCK_LINGER) ? 457 sk->sk_lingertime : SMC_ 450 sk->sk_lingertime : SMC_MAX_STREAM_WAIT_TIMEOUT; 458 451 459 old_state = sk->sk_state; 452 old_state = sk->sk_state; 460 again: 453 again: 461 switch (sk->sk_state) { 454 switch (sk->sk_state) { 462 case SMC_ACTIVE: 455 case SMC_ACTIVE: 463 smc_close_stream_wait(smc, tim 456 smc_close_stream_wait(smc, timeout); 464 release_sock(sk); 457 release_sock(sk); 465 cancel_delayed_work_sync(&conn 458 cancel_delayed_work_sync(&conn->tx_work); 466 lock_sock(sk); 459 lock_sock(sk); 467 if (sk->sk_state != SMC_ACTIVE 460 if (sk->sk_state != SMC_ACTIVE) 468 goto again; 461 goto again; 469 /* send close wr request */ 462 /* send close wr request */ 470 rc = smc_close_wr(conn); 463 rc = smc_close_wr(conn); >> 464 if (rc) >> 465 break; 471 sk->sk_state = SMC_PEERCLOSEWA 466 sk->sk_state = SMC_PEERCLOSEWAIT1; 472 break; 467 break; 473 case SMC_APPCLOSEWAIT1: 468 case SMC_APPCLOSEWAIT1: 474 /* passive close */ 469 /* passive close */ 475 if (!smc_cdc_rxed_any_close(co 470 if (!smc_cdc_rxed_any_close(conn)) 476 smc_close_stream_wait( 471 smc_close_stream_wait(smc, timeout); 477 release_sock(sk); 472 release_sock(sk); 478 cancel_delayed_work_sync(&conn 473 cancel_delayed_work_sync(&conn->tx_work); 479 lock_sock(sk); 474 lock_sock(sk); 480 if (sk->sk_state != SMC_APPCLO 475 if (sk->sk_state != SMC_APPCLOSEWAIT1) 481 goto again; 476 goto again; 482 /* confirm close from peer */ 477 /* confirm close from peer */ 483 rc = smc_close_wr(conn); 478 rc = smc_close_wr(conn); >> 479 if (rc) >> 480 break; 484 sk->sk_state = SMC_APPCLOSEWAI 481 sk->sk_state = SMC_APPCLOSEWAIT2; 485 break; 482 break; 486 case SMC_APPCLOSEWAIT2: 483 case SMC_APPCLOSEWAIT2: 487 case SMC_PEERFINCLOSEWAIT: 484 case SMC_PEERFINCLOSEWAIT: 488 case SMC_PEERCLOSEWAIT1: 485 case SMC_PEERCLOSEWAIT1: 489 case SMC_PEERCLOSEWAIT2: 486 case SMC_PEERCLOSEWAIT2: 490 case SMC_APPFINCLOSEWAIT: 487 case SMC_APPFINCLOSEWAIT: 491 case SMC_PROCESSABORT: 488 case SMC_PROCESSABORT: 492 case SMC_PEERABORTWAIT: 489 case SMC_PEERABORTWAIT: 493 /* nothing to do, add tracing 490 /* nothing to do, add tracing in future patch */ 494 break; 491 break; 495 } 492 } 496 493 497 if (old_state != sk->sk_state) 494 if (old_state != sk->sk_state) 498 sk->sk_state_change(sk); 495 sk->sk_state_change(sk); 499 return rc; 496 return rc; 500 } 497 } 501 498 502 /* Initialize close properties on connection e 499 /* Initialize close properties on connection establishment. */ 503 void smc_close_init(struct smc_sock *smc) 500 void smc_close_init(struct smc_sock *smc) 504 { 501 { 505 INIT_WORK(&smc->conn.close_work, smc_c 502 INIT_WORK(&smc->conn.close_work, smc_close_passive_work); 506 } 503 } 507 504
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.