smc_cdc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Shared Memory Communications over RDMA (SMC-R) and RoCE
  4. *
  5. * Connection Data Control (CDC)
  6. * handles flow control
  7. *
  8. * Copyright IBM Corp. 2016
  9. *
  10. * Author(s): Ursula Braun <[email protected]>
  11. */
  12. #include <linux/spinlock.h>
  13. #include "smc.h"
  14. #include "smc_wr.h"
  15. #include "smc_cdc.h"
  16. #include "smc_tx.h"
  17. #include "smc_rx.h"
  18. #include "smc_close.h"
  19. /********************************** send *************************************/
  20. /* handler for send/transmission completion of a CDC msg */
  21. static void smc_cdc_tx_handler(struct smc_wr_tx_pend_priv *pnd_snd,
  22. struct smc_link *link,
  23. enum ib_wc_status wc_status)
  24. {
  25. struct smc_cdc_tx_pend *cdcpend = (struct smc_cdc_tx_pend *)pnd_snd;
  26. struct smc_connection *conn = cdcpend->conn;
  27. struct smc_buf_desc *sndbuf_desc;
  28. struct smc_sock *smc;
  29. int diff;
  30. sndbuf_desc = conn->sndbuf_desc;
  31. smc = container_of(conn, struct smc_sock, conn);
  32. bh_lock_sock(&smc->sk);
  33. if (!wc_status && sndbuf_desc) {
  34. diff = smc_curs_diff(sndbuf_desc->len,
  35. &cdcpend->conn->tx_curs_fin,
  36. &cdcpend->cursor);
  37. /* sndbuf_space is decreased in smc_sendmsg */
  38. smp_mb__before_atomic();
  39. atomic_add(diff, &cdcpend->conn->sndbuf_space);
  40. /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
  41. smp_mb__after_atomic();
  42. smc_curs_copy(&conn->tx_curs_fin, &cdcpend->cursor, conn);
  43. smc_curs_copy(&conn->local_tx_ctrl_fin, &cdcpend->p_cursor,
  44. conn);
  45. conn->tx_cdc_seq_fin = cdcpend->ctrl_seq;
  46. }
  47. if (atomic_dec_and_test(&conn->cdc_pend_tx_wr)) {
  48. /* If user owns the sock_lock, mark the connection need sending.
  49. * User context will later try to send when it release sock_lock
  50. * in smc_release_cb()
  51. */
  52. if (sock_owned_by_user(&smc->sk))
  53. conn->tx_in_release_sock = true;
  54. else
  55. smc_tx_pending(conn);
  56. if (unlikely(wq_has_sleeper(&conn->cdc_pend_tx_wq)))
  57. wake_up(&conn->cdc_pend_tx_wq);
  58. }
  59. WARN_ON(atomic_read(&conn->cdc_pend_tx_wr) < 0);
  60. smc_tx_sndbuf_nonfull(smc);
  61. bh_unlock_sock(&smc->sk);
  62. }
  63. int smc_cdc_get_free_slot(struct smc_connection *conn,
  64. struct smc_link *link,
  65. struct smc_wr_buf **wr_buf,
  66. struct smc_rdma_wr **wr_rdma_buf,
  67. struct smc_cdc_tx_pend **pend)
  68. {
  69. int rc;
  70. rc = smc_wr_tx_get_free_slot(link, smc_cdc_tx_handler, wr_buf,
  71. wr_rdma_buf,
  72. (struct smc_wr_tx_pend_priv **)pend);
  73. if (conn->killed) {
  74. /* abnormal termination */
  75. if (!rc)
  76. smc_wr_tx_put_slot(link,
  77. (struct smc_wr_tx_pend_priv *)(*pend));
  78. rc = -EPIPE;
  79. }
  80. return rc;
  81. }
  82. static inline void smc_cdc_add_pending_send(struct smc_connection *conn,
  83. struct smc_cdc_tx_pend *pend)
  84. {
  85. BUILD_BUG_ON_MSG(
  86. sizeof(struct smc_cdc_msg) > SMC_WR_BUF_SIZE,
  87. "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_cdc_msg)");
  88. BUILD_BUG_ON_MSG(
  89. offsetofend(struct smc_cdc_msg, reserved) > SMC_WR_TX_SIZE,
  90. "must adapt SMC_WR_TX_SIZE to sizeof(struct smc_cdc_msg); if not all smc_wr upper layer protocols use the same message size any more, must start to set link->wr_tx_sges[i].length on each individual smc_wr_tx_send()");
  91. BUILD_BUG_ON_MSG(
  92. sizeof(struct smc_cdc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE,
  93. "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_cdc_tx_pend)");
  94. pend->conn = conn;
  95. pend->cursor = conn->tx_curs_sent;
  96. pend->p_cursor = conn->local_tx_ctrl.prod;
  97. pend->ctrl_seq = conn->tx_cdc_seq;
  98. }
  99. int smc_cdc_msg_send(struct smc_connection *conn,
  100. struct smc_wr_buf *wr_buf,
  101. struct smc_cdc_tx_pend *pend)
  102. {
  103. struct smc_link *link = conn->lnk;
  104. union smc_host_cursor cfed;
  105. int rc;
  106. smc_cdc_add_pending_send(conn, pend);
  107. conn->tx_cdc_seq++;
  108. conn->local_tx_ctrl.seqno = conn->tx_cdc_seq;
  109. smc_host_msg_to_cdc((struct smc_cdc_msg *)wr_buf, conn, &cfed);
  110. atomic_inc(&conn->cdc_pend_tx_wr);
  111. smp_mb__after_atomic(); /* Make sure cdc_pend_tx_wr added before post */
  112. rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
  113. if (!rc) {
  114. smc_curs_copy(&conn->rx_curs_confirmed, &cfed, conn);
  115. conn->local_rx_ctrl.prod_flags.cons_curs_upd_req = 0;
  116. } else {
  117. conn->tx_cdc_seq--;
  118. conn->local_tx_ctrl.seqno = conn->tx_cdc_seq;
  119. atomic_dec(&conn->cdc_pend_tx_wr);
  120. }
  121. return rc;
  122. }
  123. /* send a validation msg indicating the move of a conn to an other QP link */
  124. int smcr_cdc_msg_send_validation(struct smc_connection *conn,
  125. struct smc_cdc_tx_pend *pend,
  126. struct smc_wr_buf *wr_buf)
  127. {
  128. struct smc_host_cdc_msg *local = &conn->local_tx_ctrl;
  129. struct smc_link *link = conn->lnk;
  130. struct smc_cdc_msg *peer;
  131. int rc;
  132. peer = (struct smc_cdc_msg *)wr_buf;
  133. peer->common.type = local->common.type;
  134. peer->len = local->len;
  135. peer->seqno = htons(conn->tx_cdc_seq_fin); /* seqno last compl. tx */
  136. peer->token = htonl(local->token);
  137. peer->prod_flags.failover_validation = 1;
  138. /* We need to set pend->conn here to make sure smc_cdc_tx_handler()
  139. * can handle properly
  140. */
  141. smc_cdc_add_pending_send(conn, pend);
  142. atomic_inc(&conn->cdc_pend_tx_wr);
  143. smp_mb__after_atomic(); /* Make sure cdc_pend_tx_wr added before post */
  144. rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
  145. if (unlikely(rc))
  146. atomic_dec(&conn->cdc_pend_tx_wr);
  147. return rc;
  148. }
  149. static int smcr_cdc_get_slot_and_msg_send(struct smc_connection *conn)
  150. {
  151. struct smc_cdc_tx_pend *pend;
  152. struct smc_wr_buf *wr_buf;
  153. struct smc_link *link;
  154. bool again = false;
  155. int rc;
  156. again:
  157. link = conn->lnk;
  158. if (!smc_wr_tx_link_hold(link))
  159. return -ENOLINK;
  160. rc = smc_cdc_get_free_slot(conn, link, &wr_buf, NULL, &pend);
  161. if (rc)
  162. goto put_out;
  163. spin_lock_bh(&conn->send_lock);
  164. if (link != conn->lnk) {
  165. /* link of connection changed, try again one time*/
  166. spin_unlock_bh(&conn->send_lock);
  167. smc_wr_tx_put_slot(link,
  168. (struct smc_wr_tx_pend_priv *)pend);
  169. smc_wr_tx_link_put(link);
  170. if (again)
  171. return -ENOLINK;
  172. again = true;
  173. goto again;
  174. }
  175. rc = smc_cdc_msg_send(conn, wr_buf, pend);
  176. spin_unlock_bh(&conn->send_lock);
  177. put_out:
  178. smc_wr_tx_link_put(link);
  179. return rc;
  180. }
  181. int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn)
  182. {
  183. int rc;
  184. if (!smc_conn_lgr_valid(conn) ||
  185. (conn->lgr->is_smcd && conn->lgr->peer_shutdown))
  186. return -EPIPE;
  187. if (conn->lgr->is_smcd) {
  188. spin_lock_bh(&conn->send_lock);
  189. rc = smcd_cdc_msg_send(conn);
  190. spin_unlock_bh(&conn->send_lock);
  191. } else {
  192. rc = smcr_cdc_get_slot_and_msg_send(conn);
  193. }
  194. return rc;
  195. }
  196. void smc_cdc_wait_pend_tx_wr(struct smc_connection *conn)
  197. {
  198. wait_event(conn->cdc_pend_tx_wq, !atomic_read(&conn->cdc_pend_tx_wr));
  199. }
  200. /* Send a SMC-D CDC header.
  201. * This increments the free space available in our send buffer.
  202. * Also update the confirmed receive buffer with what was sent to the peer.
  203. */
  204. int smcd_cdc_msg_send(struct smc_connection *conn)
  205. {
  206. struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
  207. union smc_host_cursor curs;
  208. struct smcd_cdc_msg cdc;
  209. int rc, diff;
  210. memset(&cdc, 0, sizeof(cdc));
  211. cdc.common.type = SMC_CDC_MSG_TYPE;
  212. curs.acurs.counter = atomic64_read(&conn->local_tx_ctrl.prod.acurs);
  213. cdc.prod.wrap = curs.wrap;
  214. cdc.prod.count = curs.count;
  215. curs.acurs.counter = atomic64_read(&conn->local_tx_ctrl.cons.acurs);
  216. cdc.cons.wrap = curs.wrap;
  217. cdc.cons.count = curs.count;
  218. cdc.cons.prod_flags = conn->local_tx_ctrl.prod_flags;
  219. cdc.cons.conn_state_flags = conn->local_tx_ctrl.conn_state_flags;
  220. rc = smcd_tx_ism_write(conn, &cdc, sizeof(cdc), 0, 1);
  221. if (rc)
  222. return rc;
  223. smc_curs_copy(&conn->rx_curs_confirmed, &curs, conn);
  224. conn->local_rx_ctrl.prod_flags.cons_curs_upd_req = 0;
  225. /* Calculate transmitted data and increment free send buffer space */
  226. diff = smc_curs_diff(conn->sndbuf_desc->len, &conn->tx_curs_fin,
  227. &conn->tx_curs_sent);
  228. /* increased by confirmed number of bytes */
  229. smp_mb__before_atomic();
  230. atomic_add(diff, &conn->sndbuf_space);
  231. /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
  232. smp_mb__after_atomic();
  233. smc_curs_copy(&conn->tx_curs_fin, &conn->tx_curs_sent, conn);
  234. smc_tx_sndbuf_nonfull(smc);
  235. return rc;
  236. }
  237. /********************************* receive ***********************************/
  238. static inline bool smc_cdc_before(u16 seq1, u16 seq2)
  239. {
  240. return (s16)(seq1 - seq2) < 0;
  241. }
  242. static void smc_cdc_handle_urg_data_arrival(struct smc_sock *smc,
  243. int *diff_prod)
  244. {
  245. struct smc_connection *conn = &smc->conn;
  246. char *base;
  247. /* new data included urgent business */
  248. smc_curs_copy(&conn->urg_curs, &conn->local_rx_ctrl.prod, conn);
  249. conn->urg_state = SMC_URG_VALID;
  250. if (!sock_flag(&smc->sk, SOCK_URGINLINE))
  251. /* we'll skip the urgent byte, so don't account for it */
  252. (*diff_prod)--;
  253. base = (char *)conn->rmb_desc->cpu_addr + conn->rx_off;
  254. if (conn->urg_curs.count)
  255. conn->urg_rx_byte = *(base + conn->urg_curs.count - 1);
  256. else
  257. conn->urg_rx_byte = *(base + conn->rmb_desc->len - 1);
  258. sk_send_sigurg(&smc->sk);
  259. }
  260. static void smc_cdc_msg_validate(struct smc_sock *smc, struct smc_cdc_msg *cdc,
  261. struct smc_link *link)
  262. {
  263. struct smc_connection *conn = &smc->conn;
  264. u16 recv_seq = ntohs(cdc->seqno);
  265. s16 diff;
  266. /* check that seqnum was seen before */
  267. diff = conn->local_rx_ctrl.seqno - recv_seq;
  268. if (diff < 0) { /* diff larger than 0x7fff */
  269. /* drop connection */
  270. conn->out_of_sync = 1; /* prevent any further receives */
  271. spin_lock_bh(&conn->send_lock);
  272. conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
  273. conn->lnk = link;
  274. spin_unlock_bh(&conn->send_lock);
  275. sock_hold(&smc->sk); /* sock_put in abort_work */
  276. if (!queue_work(smc_close_wq, &conn->abort_work))
  277. sock_put(&smc->sk);
  278. }
  279. }
  280. static void smc_cdc_msg_recv_action(struct smc_sock *smc,
  281. struct smc_cdc_msg *cdc)
  282. {
  283. union smc_host_cursor cons_old, prod_old;
  284. struct smc_connection *conn = &smc->conn;
  285. int diff_cons, diff_prod;
  286. smc_curs_copy(&prod_old, &conn->local_rx_ctrl.prod, conn);
  287. smc_curs_copy(&cons_old, &conn->local_rx_ctrl.cons, conn);
  288. smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn);
  289. diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old,
  290. &conn->local_rx_ctrl.cons);
  291. if (diff_cons) {
  292. /* peer_rmbe_space is decreased during data transfer with RDMA
  293. * write
  294. */
  295. smp_mb__before_atomic();
  296. atomic_add(diff_cons, &conn->peer_rmbe_space);
  297. /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
  298. smp_mb__after_atomic();
  299. }
  300. diff_prod = smc_curs_diff(conn->rmb_desc->len, &prod_old,
  301. &conn->local_rx_ctrl.prod);
  302. if (diff_prod) {
  303. if (conn->local_rx_ctrl.prod_flags.urg_data_present)
  304. smc_cdc_handle_urg_data_arrival(smc, &diff_prod);
  305. /* bytes_to_rcv is decreased in smc_recvmsg */
  306. smp_mb__before_atomic();
  307. atomic_add(diff_prod, &conn->bytes_to_rcv);
  308. /* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */
  309. smp_mb__after_atomic();
  310. smc->sk.sk_data_ready(&smc->sk);
  311. } else {
  312. if (conn->local_rx_ctrl.prod_flags.write_blocked)
  313. smc->sk.sk_data_ready(&smc->sk);
  314. if (conn->local_rx_ctrl.prod_flags.urg_data_pending)
  315. conn->urg_state = SMC_URG_NOTYET;
  316. }
  317. /* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */
  318. if ((diff_cons && smc_tx_prepared_sends(conn)) ||
  319. conn->local_rx_ctrl.prod_flags.cons_curs_upd_req ||
  320. conn->local_rx_ctrl.prod_flags.urg_data_pending) {
  321. if (!sock_owned_by_user(&smc->sk))
  322. smc_tx_pending(conn);
  323. else
  324. conn->tx_in_release_sock = true;
  325. }
  326. if (diff_cons && conn->urg_tx_pend &&
  327. atomic_read(&conn->peer_rmbe_space) == conn->peer_rmbe_size) {
  328. /* urg data confirmed by peer, indicate we're ready for more */
  329. conn->urg_tx_pend = false;
  330. smc->sk.sk_write_space(&smc->sk);
  331. }
  332. if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {
  333. smc->sk.sk_err = ECONNRESET;
  334. conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
  335. }
  336. if (smc_cdc_rxed_any_close_or_senddone(conn)) {
  337. smc->sk.sk_shutdown |= RCV_SHUTDOWN;
  338. if (smc->clcsock && smc->clcsock->sk)
  339. smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN;
  340. smc_sock_set_flag(&smc->sk, SOCK_DONE);
  341. sock_hold(&smc->sk); /* sock_put in close_work */
  342. if (!queue_work(smc_close_wq, &conn->close_work))
  343. sock_put(&smc->sk);
  344. }
  345. }
  346. /* called under tasklet context */
  347. static void smc_cdc_msg_recv(struct smc_sock *smc, struct smc_cdc_msg *cdc)
  348. {
  349. sock_hold(&smc->sk);
  350. bh_lock_sock(&smc->sk);
  351. smc_cdc_msg_recv_action(smc, cdc);
  352. bh_unlock_sock(&smc->sk);
  353. sock_put(&smc->sk); /* no free sk in softirq-context */
  354. }
  355. /* Schedule a tasklet for this connection. Triggered from the ISM device IRQ
  356. * handler to indicate update in the DMBE.
  357. *
  358. * Context:
  359. * - tasklet context
  360. */
  361. static void smcd_cdc_rx_tsklet(struct tasklet_struct *t)
  362. {
  363. struct smc_connection *conn = from_tasklet(conn, t, rx_tsklet);
  364. struct smcd_cdc_msg *data_cdc;
  365. struct smcd_cdc_msg cdc;
  366. struct smc_sock *smc;
  367. if (!conn || conn->killed)
  368. return;
  369. data_cdc = (struct smcd_cdc_msg *)conn->rmb_desc->cpu_addr;
  370. smcd_curs_copy(&cdc.prod, &data_cdc->prod, conn);
  371. smcd_curs_copy(&cdc.cons, &data_cdc->cons, conn);
  372. smc = container_of(conn, struct smc_sock, conn);
  373. smc_cdc_msg_recv(smc, (struct smc_cdc_msg *)&cdc);
  374. }
  375. /* Initialize receive tasklet. Called from ISM device IRQ handler to start
  376. * receiver side.
  377. */
  378. void smcd_cdc_rx_init(struct smc_connection *conn)
  379. {
  380. tasklet_setup(&conn->rx_tsklet, smcd_cdc_rx_tsklet);
  381. }
  382. /***************************** init, exit, misc ******************************/
  383. static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf)
  384. {
  385. struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
  386. struct smc_cdc_msg *cdc = buf;
  387. struct smc_connection *conn;
  388. struct smc_link_group *lgr;
  389. struct smc_sock *smc;
  390. if (wc->byte_len < offsetof(struct smc_cdc_msg, reserved))
  391. return; /* short message */
  392. if (cdc->len != SMC_WR_TX_SIZE)
  393. return; /* invalid message */
  394. /* lookup connection */
  395. lgr = smc_get_lgr(link);
  396. read_lock_bh(&lgr->conns_lock);
  397. conn = smc_lgr_find_conn(ntohl(cdc->token), lgr);
  398. read_unlock_bh(&lgr->conns_lock);
  399. if (!conn || conn->out_of_sync)
  400. return;
  401. smc = container_of(conn, struct smc_sock, conn);
  402. if (cdc->prod_flags.failover_validation) {
  403. smc_cdc_msg_validate(smc, cdc, link);
  404. return;
  405. }
  406. if (smc_cdc_before(ntohs(cdc->seqno),
  407. conn->local_rx_ctrl.seqno))
  408. /* received seqno is old */
  409. return;
  410. smc_cdc_msg_recv(smc, cdc);
  411. }
  412. static struct smc_wr_rx_handler smc_cdc_rx_handlers[] = {
  413. {
  414. .handler = smc_cdc_rx_handler,
  415. .type = SMC_CDC_MSG_TYPE
  416. },
  417. {
  418. .handler = NULL,
  419. }
  420. };
  421. int __init smc_cdc_init(void)
  422. {
  423. struct smc_wr_rx_handler *handler;
  424. int rc = 0;
  425. for (handler = smc_cdc_rx_handlers; handler->handler; handler++) {
  426. INIT_HLIST_NODE(&handler->list);
  427. rc = smc_wr_rx_register_handler(handler);
  428. if (rc)
  429. break;
  430. }
  431. return rc;
  432. }