smc_cdc.c 12 KB

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