smc_cdc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. struct smc_cdc_tx_pend {
  21. struct smc_connection *conn; /* socket connection */
  22. union smc_host_cursor cursor; /* tx sndbuf cursor sent */
  23. union smc_host_cursor p_cursor; /* rx RMBE cursor produced */
  24. u16 ctrl_seq; /* conn. tx sequence # */
  25. };
  26. /* handler for send/transmission completion of a CDC msg */
  27. static void smc_cdc_tx_handler(struct smc_wr_tx_pend_priv *pnd_snd,
  28. struct smc_link *link,
  29. enum ib_wc_status wc_status)
  30. {
  31. struct smc_cdc_tx_pend *cdcpend = (struct smc_cdc_tx_pend *)pnd_snd;
  32. struct smc_connection *conn = cdcpend->conn;
  33. struct smc_sock *smc;
  34. int diff;
  35. if (!conn)
  36. /* already dismissed */
  37. return;
  38. smc = container_of(conn, struct smc_sock, conn);
  39. bh_lock_sock(&smc->sk);
  40. if (!wc_status) {
  41. diff = smc_curs_diff(cdcpend->conn->sndbuf_desc->len,
  42. &cdcpend->conn->tx_curs_fin,
  43. &cdcpend->cursor);
  44. /* sndbuf_space is decreased in smc_sendmsg */
  45. smp_mb__before_atomic();
  46. atomic_add(diff, &cdcpend->conn->sndbuf_space);
  47. /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
  48. smp_mb__after_atomic();
  49. smc_curs_copy(&conn->tx_curs_fin, &cdcpend->cursor, conn);
  50. }
  51. smc_tx_sndbuf_nonfull(smc);
  52. bh_unlock_sock(&smc->sk);
  53. }
  54. int smc_cdc_get_free_slot(struct smc_connection *conn,
  55. struct smc_wr_buf **wr_buf,
  56. struct smc_cdc_tx_pend **pend)
  57. {
  58. struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
  59. int rc;
  60. rc = smc_wr_tx_get_free_slot(link, smc_cdc_tx_handler, wr_buf,
  61. (struct smc_wr_tx_pend_priv **)pend);
  62. if (!conn->alert_token_local)
  63. /* abnormal termination */
  64. rc = -EPIPE;
  65. return rc;
  66. }
  67. static inline void smc_cdc_add_pending_send(struct smc_connection *conn,
  68. struct smc_cdc_tx_pend *pend)
  69. {
  70. BUILD_BUG_ON_MSG(
  71. sizeof(struct smc_cdc_msg) > SMC_WR_BUF_SIZE,
  72. "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_cdc_msg)");
  73. BUILD_BUG_ON_MSG(
  74. offsetofend(struct smc_cdc_msg, reserved) > SMC_WR_TX_SIZE,
  75. "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()");
  76. BUILD_BUG_ON_MSG(
  77. sizeof(struct smc_cdc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE,
  78. "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_cdc_tx_pend)");
  79. pend->conn = conn;
  80. pend->cursor = conn->tx_curs_sent;
  81. pend->p_cursor = conn->local_tx_ctrl.prod;
  82. pend->ctrl_seq = conn->tx_cdc_seq;
  83. }
  84. int smc_cdc_msg_send(struct smc_connection *conn,
  85. struct smc_wr_buf *wr_buf,
  86. struct smc_cdc_tx_pend *pend)
  87. {
  88. struct smc_link *link;
  89. int rc;
  90. link = &conn->lgr->lnk[SMC_SINGLE_LINK];
  91. smc_cdc_add_pending_send(conn, pend);
  92. conn->tx_cdc_seq++;
  93. conn->local_tx_ctrl.seqno = conn->tx_cdc_seq;
  94. smc_host_msg_to_cdc((struct smc_cdc_msg *)wr_buf,
  95. &conn->local_tx_ctrl, conn);
  96. rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
  97. if (!rc)
  98. smc_curs_copy(&conn->rx_curs_confirmed,
  99. &conn->local_tx_ctrl.cons, conn);
  100. return rc;
  101. }
  102. static int smcr_cdc_get_slot_and_msg_send(struct smc_connection *conn)
  103. {
  104. struct smc_cdc_tx_pend *pend;
  105. struct smc_wr_buf *wr_buf;
  106. int rc;
  107. rc = smc_cdc_get_free_slot(conn, &wr_buf, &pend);
  108. if (rc)
  109. return rc;
  110. return smc_cdc_msg_send(conn, wr_buf, pend);
  111. }
  112. int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn)
  113. {
  114. int rc;
  115. if (conn->lgr->is_smcd) {
  116. spin_lock_bh(&conn->send_lock);
  117. rc = smcd_cdc_msg_send(conn);
  118. spin_unlock_bh(&conn->send_lock);
  119. } else {
  120. rc = smcr_cdc_get_slot_and_msg_send(conn);
  121. }
  122. return rc;
  123. }
  124. static bool smc_cdc_tx_filter(struct smc_wr_tx_pend_priv *tx_pend,
  125. unsigned long data)
  126. {
  127. struct smc_connection *conn = (struct smc_connection *)data;
  128. struct smc_cdc_tx_pend *cdc_pend =
  129. (struct smc_cdc_tx_pend *)tx_pend;
  130. return cdc_pend->conn == conn;
  131. }
  132. static void smc_cdc_tx_dismisser(struct smc_wr_tx_pend_priv *tx_pend)
  133. {
  134. struct smc_cdc_tx_pend *cdc_pend =
  135. (struct smc_cdc_tx_pend *)tx_pend;
  136. cdc_pend->conn = NULL;
  137. }
  138. void smc_cdc_tx_dismiss_slots(struct smc_connection *conn)
  139. {
  140. struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
  141. smc_wr_tx_dismiss_slots(link, SMC_CDC_MSG_TYPE,
  142. smc_cdc_tx_filter, smc_cdc_tx_dismisser,
  143. (unsigned long)conn);
  144. }
  145. /* Send a SMC-D CDC header.
  146. * This increments the free space available in our send buffer.
  147. * Also update the confirmed receive buffer with what was sent to the peer.
  148. */
  149. int smcd_cdc_msg_send(struct smc_connection *conn)
  150. {
  151. struct smc_sock *smc = container_of(conn, struct smc_sock, conn);
  152. union smc_host_cursor curs;
  153. struct smcd_cdc_msg cdc;
  154. int rc, diff;
  155. memset(&cdc, 0, sizeof(cdc));
  156. cdc.common.type = SMC_CDC_MSG_TYPE;
  157. curs.acurs.counter = atomic64_read(&conn->local_tx_ctrl.prod.acurs);
  158. cdc.prod.wrap = curs.wrap;
  159. cdc.prod.count = curs.count;
  160. curs.acurs.counter = atomic64_read(&conn->local_tx_ctrl.cons.acurs);
  161. cdc.cons.wrap = curs.wrap;
  162. cdc.cons.count = curs.count;
  163. cdc.cons.prod_flags = conn->local_tx_ctrl.prod_flags;
  164. cdc.cons.conn_state_flags = conn->local_tx_ctrl.conn_state_flags;
  165. rc = smcd_tx_ism_write(conn, &cdc, sizeof(cdc), 0, 1);
  166. if (rc)
  167. return rc;
  168. smc_curs_copy(&conn->rx_curs_confirmed, &curs, conn);
  169. /* Calculate transmitted data and increment free send buffer space */
  170. diff = smc_curs_diff(conn->sndbuf_desc->len, &conn->tx_curs_fin,
  171. &conn->tx_curs_sent);
  172. /* increased by confirmed number of bytes */
  173. smp_mb__before_atomic();
  174. atomic_add(diff, &conn->sndbuf_space);
  175. /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
  176. smp_mb__after_atomic();
  177. smc_curs_copy(&conn->tx_curs_fin, &conn->tx_curs_sent, conn);
  178. smc_tx_sndbuf_nonfull(smc);
  179. return rc;
  180. }
  181. /********************************* receive ***********************************/
  182. static inline bool smc_cdc_before(u16 seq1, u16 seq2)
  183. {
  184. return (s16)(seq1 - seq2) < 0;
  185. }
  186. static void smc_cdc_handle_urg_data_arrival(struct smc_sock *smc,
  187. int *diff_prod)
  188. {
  189. struct smc_connection *conn = &smc->conn;
  190. char *base;
  191. /* new data included urgent business */
  192. smc_curs_copy(&conn->urg_curs, &conn->local_rx_ctrl.prod, conn);
  193. conn->urg_state = SMC_URG_VALID;
  194. if (!sock_flag(&smc->sk, SOCK_URGINLINE))
  195. /* we'll skip the urgent byte, so don't account for it */
  196. (*diff_prod)--;
  197. base = (char *)conn->rmb_desc->cpu_addr + conn->rx_off;
  198. if (conn->urg_curs.count)
  199. conn->urg_rx_byte = *(base + conn->urg_curs.count - 1);
  200. else
  201. conn->urg_rx_byte = *(base + conn->rmb_desc->len - 1);
  202. sk_send_sigurg(&smc->sk);
  203. }
  204. static void smc_cdc_msg_recv_action(struct smc_sock *smc,
  205. struct smc_cdc_msg *cdc)
  206. {
  207. union smc_host_cursor cons_old, prod_old;
  208. struct smc_connection *conn = &smc->conn;
  209. int diff_cons, diff_prod;
  210. smc_curs_copy(&prod_old, &conn->local_rx_ctrl.prod, conn);
  211. smc_curs_copy(&cons_old, &conn->local_rx_ctrl.cons, conn);
  212. smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn);
  213. diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old,
  214. &conn->local_rx_ctrl.cons);
  215. if (diff_cons) {
  216. /* peer_rmbe_space is decreased during data transfer with RDMA
  217. * write
  218. */
  219. smp_mb__before_atomic();
  220. atomic_add(diff_cons, &conn->peer_rmbe_space);
  221. /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
  222. smp_mb__after_atomic();
  223. }
  224. diff_prod = smc_curs_diff(conn->rmb_desc->len, &prod_old,
  225. &conn->local_rx_ctrl.prod);
  226. if (diff_prod) {
  227. if (conn->local_rx_ctrl.prod_flags.urg_data_present)
  228. smc_cdc_handle_urg_data_arrival(smc, &diff_prod);
  229. /* bytes_to_rcv is decreased in smc_recvmsg */
  230. smp_mb__before_atomic();
  231. atomic_add(diff_prod, &conn->bytes_to_rcv);
  232. /* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */
  233. smp_mb__after_atomic();
  234. smc->sk.sk_data_ready(&smc->sk);
  235. } else {
  236. if (conn->local_rx_ctrl.prod_flags.write_blocked ||
  237. conn->local_rx_ctrl.prod_flags.cons_curs_upd_req ||
  238. conn->local_rx_ctrl.prod_flags.urg_data_pending) {
  239. if (conn->local_rx_ctrl.prod_flags.urg_data_pending)
  240. conn->urg_state = SMC_URG_NOTYET;
  241. /* force immediate tx of current consumer cursor, but
  242. * under send_lock to guarantee arrival in seqno-order
  243. */
  244. if (smc->sk.sk_state != SMC_INIT)
  245. smc_tx_sndbuf_nonempty(conn);
  246. }
  247. }
  248. /* piggy backed tx info */
  249. /* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */
  250. if (diff_cons && smc_tx_prepared_sends(conn)) {
  251. smc_tx_sndbuf_nonempty(conn);
  252. /* trigger socket release if connection closed */
  253. smc_close_wake_tx_prepared(smc);
  254. }
  255. if (diff_cons && conn->urg_tx_pend &&
  256. atomic_read(&conn->peer_rmbe_space) == conn->peer_rmbe_size) {
  257. /* urg data confirmed by peer, indicate we're ready for more */
  258. conn->urg_tx_pend = false;
  259. smc->sk.sk_write_space(&smc->sk);
  260. }
  261. if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {
  262. smc->sk.sk_err = ECONNRESET;
  263. conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
  264. }
  265. if (smc_cdc_rxed_any_close_or_senddone(conn)) {
  266. smc->sk.sk_shutdown |= RCV_SHUTDOWN;
  267. if (smc->clcsock && smc->clcsock->sk)
  268. smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN;
  269. sock_set_flag(&smc->sk, SOCK_DONE);
  270. sock_hold(&smc->sk); /* sock_put in close_work */
  271. if (!schedule_work(&conn->close_work))
  272. sock_put(&smc->sk);
  273. }
  274. }
  275. /* called under tasklet context */
  276. static void smc_cdc_msg_recv(struct smc_sock *smc, struct smc_cdc_msg *cdc)
  277. {
  278. sock_hold(&smc->sk);
  279. bh_lock_sock(&smc->sk);
  280. smc_cdc_msg_recv_action(smc, cdc);
  281. bh_unlock_sock(&smc->sk);
  282. sock_put(&smc->sk); /* no free sk in softirq-context */
  283. }
  284. /* Schedule a tasklet for this connection. Triggered from the ISM device IRQ
  285. * handler to indicate update in the DMBE.
  286. *
  287. * Context:
  288. * - tasklet context
  289. */
  290. static void smcd_cdc_rx_tsklet(unsigned long data)
  291. {
  292. struct smc_connection *conn = (struct smc_connection *)data;
  293. struct smcd_cdc_msg *data_cdc;
  294. struct smcd_cdc_msg cdc;
  295. struct smc_sock *smc;
  296. if (!conn)
  297. return;
  298. data_cdc = (struct smcd_cdc_msg *)conn->rmb_desc->cpu_addr;
  299. smcd_curs_copy(&cdc.prod, &data_cdc->prod, conn);
  300. smcd_curs_copy(&cdc.cons, &data_cdc->cons, conn);
  301. smc = container_of(conn, struct smc_sock, conn);
  302. smc_cdc_msg_recv(smc, (struct smc_cdc_msg *)&cdc);
  303. }
  304. /* Initialize receive tasklet. Called from ISM device IRQ handler to start
  305. * receiver side.
  306. */
  307. void smcd_cdc_rx_init(struct smc_connection *conn)
  308. {
  309. tasklet_init(&conn->rx_tsklet, smcd_cdc_rx_tsklet, (unsigned long)conn);
  310. }
  311. /***************************** init, exit, misc ******************************/
  312. static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf)
  313. {
  314. struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
  315. struct smc_cdc_msg *cdc = buf;
  316. struct smc_connection *conn;
  317. struct smc_link_group *lgr;
  318. struct smc_sock *smc;
  319. if (wc->byte_len < offsetof(struct smc_cdc_msg, reserved))
  320. return; /* short message */
  321. if (cdc->len != SMC_WR_TX_SIZE)
  322. return; /* invalid message */
  323. /* lookup connection */
  324. lgr = smc_get_lgr(link);
  325. read_lock_bh(&lgr->conns_lock);
  326. conn = smc_lgr_find_conn(ntohl(cdc->token), lgr);
  327. read_unlock_bh(&lgr->conns_lock);
  328. if (!conn)
  329. return;
  330. smc = container_of(conn, struct smc_sock, conn);
  331. if (!cdc->prod_flags.failover_validation) {
  332. if (smc_cdc_before(ntohs(cdc->seqno),
  333. conn->local_rx_ctrl.seqno))
  334. /* received seqno is old */
  335. return;
  336. }
  337. smc_cdc_msg_recv(smc, cdc);
  338. }
  339. static struct smc_wr_rx_handler smc_cdc_rx_handlers[] = {
  340. {
  341. .handler = smc_cdc_rx_handler,
  342. .type = SMC_CDC_MSG_TYPE
  343. },
  344. {
  345. .handler = NULL,
  346. }
  347. };
  348. int __init smc_cdc_init(void)
  349. {
  350. struct smc_wr_rx_handler *handler;
  351. int rc = 0;
  352. for (handler = smc_cdc_rx_handlers; handler->handler; handler++) {
  353. INIT_HLIST_NODE(&handler->list);
  354. rc = smc_wr_rx_register_handler(handler);
  355. if (rc)
  356. break;
  357. }
  358. return rc;
  359. }