smc_close.c 12 KB

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