smc_cdc.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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_sock *smc;
  33. int diff;
  34. if (!cdcpend->conn)
  35. /* already dismissed */
  36. return;
  37. smc = container_of(cdcpend->conn, struct smc_sock, conn);
  38. bh_lock_sock(&smc->sk);
  39. if (!wc_status) {
  40. diff = smc_curs_diff(cdcpend->conn->sndbuf_desc->len,
  41. &cdcpend->conn->tx_curs_fin,
  42. &cdcpend->cursor);
  43. /* sndbuf_space is decreased in smc_sendmsg */
  44. smp_mb__before_atomic();
  45. atomic_add(diff, &cdcpend->conn->sndbuf_space);
  46. /* guarantee 0 <= sndbuf_space <= sndbuf_desc->len */
  47. smp_mb__after_atomic();
  48. smc_curs_write(&cdcpend->conn->tx_curs_fin,
  49. smc_curs_read(&cdcpend->cursor, cdcpend->conn),
  50. cdcpend->conn);
  51. }
  52. smc_tx_sndbuf_nonfull(smc);
  53. bh_unlock_sock(&smc->sk);
  54. }
  55. int smc_cdc_get_free_slot(struct smc_connection *conn,
  56. struct smc_wr_buf **wr_buf,
  57. struct smc_cdc_tx_pend **pend)
  58. {
  59. struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
  60. int rc;
  61. rc = smc_wr_tx_get_free_slot(link, smc_cdc_tx_handler, wr_buf,
  62. (struct smc_wr_tx_pend_priv **)pend);
  63. if (!conn->alert_token_local)
  64. /* abnormal termination */
  65. rc = -EPIPE;
  66. return rc;
  67. }
  68. static inline void smc_cdc_add_pending_send(struct smc_connection *conn,
  69. struct smc_cdc_tx_pend *pend)
  70. {
  71. BUILD_BUG_ON_MSG(
  72. sizeof(struct smc_cdc_msg) > SMC_WR_BUF_SIZE,
  73. "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_cdc_msg)");
  74. BUILD_BUG_ON_MSG(
  75. sizeof(struct smc_cdc_msg) != SMC_WR_TX_SIZE,
  76. "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()");
  77. BUILD_BUG_ON_MSG(
  78. sizeof(struct smc_cdc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE,
  79. "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_cdc_tx_pend)");
  80. pend->conn = conn;
  81. pend->cursor = conn->tx_curs_sent;
  82. pend->p_cursor = conn->local_tx_ctrl.prod;
  83. pend->ctrl_seq = conn->tx_cdc_seq;
  84. }
  85. int smc_cdc_msg_send(struct smc_connection *conn,
  86. struct smc_wr_buf *wr_buf,
  87. struct smc_cdc_tx_pend *pend)
  88. {
  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,
  96. &conn->local_tx_ctrl, conn);
  97. rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
  98. if (!rc)
  99. smc_curs_write(&conn->rx_curs_confirmed,
  100. smc_curs_read(&conn->local_tx_ctrl.cons, conn),
  101. conn);
  102. return rc;
  103. }
  104. int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn)
  105. {
  106. struct smc_cdc_tx_pend *pend;
  107. struct smc_wr_buf *wr_buf;
  108. int rc;
  109. rc = smc_cdc_get_free_slot(conn, &wr_buf, &pend);
  110. if (rc)
  111. return rc;
  112. return smc_cdc_msg_send(conn, wr_buf, pend);
  113. }
  114. static bool smc_cdc_tx_filter(struct smc_wr_tx_pend_priv *tx_pend,
  115. unsigned long data)
  116. {
  117. struct smc_connection *conn = (struct smc_connection *)data;
  118. struct smc_cdc_tx_pend *cdc_pend =
  119. (struct smc_cdc_tx_pend *)tx_pend;
  120. return cdc_pend->conn == conn;
  121. }
  122. static void smc_cdc_tx_dismisser(struct smc_wr_tx_pend_priv *tx_pend)
  123. {
  124. struct smc_cdc_tx_pend *cdc_pend =
  125. (struct smc_cdc_tx_pend *)tx_pend;
  126. cdc_pend->conn = NULL;
  127. }
  128. void smc_cdc_tx_dismiss_slots(struct smc_connection *conn)
  129. {
  130. struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
  131. smc_wr_tx_dismiss_slots(link, SMC_CDC_MSG_TYPE,
  132. smc_cdc_tx_filter, smc_cdc_tx_dismisser,
  133. (unsigned long)conn);
  134. }
  135. /********************************* receive ***********************************/
  136. static inline bool smc_cdc_before(u16 seq1, u16 seq2)
  137. {
  138. return (s16)(seq1 - seq2) < 0;
  139. }
  140. static void smc_cdc_handle_urg_data_arrival(struct smc_sock *smc,
  141. int *diff_prod)
  142. {
  143. struct smc_connection *conn = &smc->conn;
  144. char *base;
  145. /* new data included urgent business */
  146. smc_curs_write(&conn->urg_curs,
  147. smc_curs_read(&conn->local_rx_ctrl.prod, conn),
  148. conn);
  149. conn->urg_state = SMC_URG_VALID;
  150. if (!sock_flag(&smc->sk, SOCK_URGINLINE))
  151. /* we'll skip the urgent byte, so don't account for it */
  152. (*diff_prod)--;
  153. base = (char *)conn->rmb_desc->cpu_addr;
  154. if (conn->urg_curs.count)
  155. conn->urg_rx_byte = *(base + conn->urg_curs.count - 1);
  156. else
  157. conn->urg_rx_byte = *(base + conn->rmb_desc->len - 1);
  158. sk_send_sigurg(&smc->sk);
  159. }
  160. static void smc_cdc_msg_recv_action(struct smc_sock *smc,
  161. struct smc_cdc_msg *cdc)
  162. {
  163. union smc_host_cursor cons_old, prod_old;
  164. struct smc_connection *conn = &smc->conn;
  165. int diff_cons, diff_prod;
  166. smc_curs_write(&prod_old,
  167. smc_curs_read(&conn->local_rx_ctrl.prod, conn),
  168. conn);
  169. smc_curs_write(&cons_old,
  170. smc_curs_read(&conn->local_rx_ctrl.cons, conn),
  171. conn);
  172. smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn);
  173. diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old,
  174. &conn->local_rx_ctrl.cons);
  175. if (diff_cons) {
  176. /* peer_rmbe_space is decreased during data transfer with RDMA
  177. * write
  178. */
  179. smp_mb__before_atomic();
  180. atomic_add(diff_cons, &conn->peer_rmbe_space);
  181. /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
  182. smp_mb__after_atomic();
  183. }
  184. diff_prod = smc_curs_diff(conn->rmb_desc->len, &prod_old,
  185. &conn->local_rx_ctrl.prod);
  186. if (diff_prod) {
  187. if (conn->local_rx_ctrl.prod_flags.urg_data_present)
  188. smc_cdc_handle_urg_data_arrival(smc, &diff_prod);
  189. /* bytes_to_rcv is decreased in smc_recvmsg */
  190. smp_mb__before_atomic();
  191. atomic_add(diff_prod, &conn->bytes_to_rcv);
  192. /* guarantee 0 <= bytes_to_rcv <= rmb_desc->len */
  193. smp_mb__after_atomic();
  194. smc->sk.sk_data_ready(&smc->sk);
  195. } else {
  196. if (conn->local_rx_ctrl.prod_flags.write_blocked ||
  197. conn->local_rx_ctrl.prod_flags.cons_curs_upd_req ||
  198. conn->local_rx_ctrl.prod_flags.urg_data_pending) {
  199. if (conn->local_rx_ctrl.prod_flags.urg_data_pending)
  200. conn->urg_state = SMC_URG_NOTYET;
  201. /* force immediate tx of current consumer cursor, but
  202. * under send_lock to guarantee arrival in seqno-order
  203. */
  204. smc_tx_sndbuf_nonempty(conn);
  205. }
  206. }
  207. /* piggy backed tx info */
  208. /* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */
  209. if (diff_cons && smc_tx_prepared_sends(conn)) {
  210. smc_tx_sndbuf_nonempty(conn);
  211. /* trigger socket release if connection closed */
  212. smc_close_wake_tx_prepared(smc);
  213. }
  214. if (diff_cons && conn->urg_tx_pend &&
  215. atomic_read(&conn->peer_rmbe_space) == conn->peer_rmbe_size) {
  216. /* urg data confirmed by peer, indicate we're ready for more */
  217. conn->urg_tx_pend = false;
  218. smc->sk.sk_write_space(&smc->sk);
  219. }
  220. if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {
  221. smc->sk.sk_err = ECONNRESET;
  222. conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
  223. }
  224. if (smc_cdc_rxed_any_close_or_senddone(conn)) {
  225. smc->sk.sk_shutdown |= RCV_SHUTDOWN;
  226. if (smc->clcsock && smc->clcsock->sk)
  227. smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN;
  228. sock_set_flag(&smc->sk, SOCK_DONE);
  229. sock_hold(&smc->sk); /* sock_put in close_work */
  230. if (!schedule_work(&conn->close_work))
  231. sock_put(&smc->sk);
  232. }
  233. }
  234. /* called under tasklet context */
  235. static void smc_cdc_msg_recv(struct smc_sock *smc, struct smc_cdc_msg *cdc)
  236. {
  237. sock_hold(&smc->sk);
  238. bh_lock_sock(&smc->sk);
  239. smc_cdc_msg_recv_action(smc, cdc);
  240. bh_unlock_sock(&smc->sk);
  241. sock_put(&smc->sk); /* no free sk in softirq-context */
  242. }
  243. /***************************** init, exit, misc ******************************/
  244. static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf)
  245. {
  246. struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
  247. struct smc_cdc_msg *cdc = buf;
  248. struct smc_connection *conn;
  249. struct smc_link_group *lgr;
  250. struct smc_sock *smc;
  251. if (wc->byte_len < offsetof(struct smc_cdc_msg, reserved))
  252. return; /* short message */
  253. if (cdc->len != SMC_WR_TX_SIZE)
  254. return; /* invalid message */
  255. /* lookup connection */
  256. lgr = container_of(link, struct smc_link_group, lnk[SMC_SINGLE_LINK]);
  257. read_lock_bh(&lgr->conns_lock);
  258. conn = smc_lgr_find_conn(ntohl(cdc->token), lgr);
  259. read_unlock_bh(&lgr->conns_lock);
  260. if (!conn)
  261. return;
  262. smc = container_of(conn, struct smc_sock, conn);
  263. if (!cdc->prod_flags.failover_validation) {
  264. if (smc_cdc_before(ntohs(cdc->seqno),
  265. conn->local_rx_ctrl.seqno))
  266. /* received seqno is old */
  267. return;
  268. }
  269. smc_cdc_msg_recv(smc, cdc);
  270. }
  271. static struct smc_wr_rx_handler smc_cdc_rx_handlers[] = {
  272. {
  273. .handler = smc_cdc_rx_handler,
  274. .type = SMC_CDC_MSG_TYPE
  275. },
  276. {
  277. .handler = NULL,
  278. }
  279. };
  280. int __init smc_cdc_init(void)
  281. {
  282. struct smc_wr_rx_handler *handler;
  283. int rc = 0;
  284. for (handler = smc_cdc_rx_handlers; handler->handler; handler++) {
  285. INIT_HLIST_NODE(&handler->list);
  286. rc = smc_wr_rx_register_handler(handler);
  287. if (rc)
  288. break;
  289. }
  290. return rc;
  291. }