smc_cdc.c 14 KB

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