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