smc_cdc.c 15 KB

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