smc_cdc.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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_size,
  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_size */
  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. if (smc->sk.sk_state != SMC_ACTIVE)
  54. /* wake up smc_close_wait_tx_pends() */
  55. smc->sk.sk_state_change(&smc->sk);
  56. bh_unlock_sock(&smc->sk);
  57. }
  58. int smc_cdc_get_free_slot(struct smc_link *link,
  59. struct smc_wr_buf **wr_buf,
  60. struct smc_cdc_tx_pend **pend)
  61. {
  62. return smc_wr_tx_get_free_slot(link, smc_cdc_tx_handler, wr_buf,
  63. (struct smc_wr_tx_pend_priv **)pend);
  64. }
  65. static inline void smc_cdc_add_pending_send(struct smc_connection *conn,
  66. struct smc_cdc_tx_pend *pend)
  67. {
  68. BUILD_BUG_ON_MSG(
  69. sizeof(struct smc_cdc_msg) > SMC_WR_BUF_SIZE,
  70. "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_cdc_msg)");
  71. BUILD_BUG_ON_MSG(
  72. offsetof(struct smc_cdc_msg, reserved) > SMC_WR_TX_SIZE,
  73. "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()");
  74. BUILD_BUG_ON_MSG(
  75. sizeof(struct smc_cdc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE,
  76. "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_cdc_tx_pend)");
  77. pend->conn = conn;
  78. pend->cursor = conn->tx_curs_sent;
  79. pend->p_cursor = conn->local_tx_ctrl.prod;
  80. pend->ctrl_seq = conn->tx_cdc_seq;
  81. }
  82. int smc_cdc_msg_send(struct smc_connection *conn,
  83. struct smc_wr_buf *wr_buf,
  84. struct smc_cdc_tx_pend *pend)
  85. {
  86. struct smc_link *link;
  87. int rc;
  88. link = &conn->lgr->lnk[SMC_SINGLE_LINK];
  89. smc_cdc_add_pending_send(conn, pend);
  90. conn->tx_cdc_seq++;
  91. conn->local_tx_ctrl.seqno = conn->tx_cdc_seq;
  92. smc_host_msg_to_cdc((struct smc_cdc_msg *)wr_buf,
  93. &conn->local_tx_ctrl, conn);
  94. rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
  95. if (!rc)
  96. smc_curs_write(&conn->rx_curs_confirmed,
  97. smc_curs_read(&conn->local_tx_ctrl.cons, conn),
  98. conn);
  99. return rc;
  100. }
  101. int smc_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->lgr->lnk[SMC_SINGLE_LINK], &wr_buf,
  107. &pend);
  108. if (rc)
  109. return rc;
  110. return smc_cdc_msg_send(conn, wr_buf, pend);
  111. }
  112. static bool smc_cdc_tx_filter(struct smc_wr_tx_pend_priv *tx_pend,
  113. unsigned long data)
  114. {
  115. struct smc_connection *conn = (struct smc_connection *)data;
  116. struct smc_cdc_tx_pend *cdc_pend =
  117. (struct smc_cdc_tx_pend *)tx_pend;
  118. return cdc_pend->conn == conn;
  119. }
  120. static void smc_cdc_tx_dismisser(struct smc_wr_tx_pend_priv *tx_pend)
  121. {
  122. struct smc_cdc_tx_pend *cdc_pend =
  123. (struct smc_cdc_tx_pend *)tx_pend;
  124. cdc_pend->conn = NULL;
  125. }
  126. void smc_cdc_tx_dismiss_slots(struct smc_connection *conn)
  127. {
  128. struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
  129. smc_wr_tx_dismiss_slots(link, SMC_CDC_MSG_TYPE,
  130. smc_cdc_tx_filter, smc_cdc_tx_dismisser,
  131. (unsigned long)conn);
  132. }
  133. bool smc_cdc_tx_has_pending(struct smc_connection *conn)
  134. {
  135. struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
  136. return smc_wr_tx_has_pending(link, SMC_CDC_MSG_TYPE,
  137. smc_cdc_tx_filter, (unsigned long)conn);
  138. }
  139. /********************************* receive ***********************************/
  140. static inline bool smc_cdc_before(u16 seq1, u16 seq2)
  141. {
  142. return (s16)(seq1 - seq2) < 0;
  143. }
  144. static void smc_cdc_msg_recv_action(struct smc_sock *smc,
  145. struct smc_link *link,
  146. struct smc_cdc_msg *cdc)
  147. {
  148. union smc_host_cursor cons_old, prod_old;
  149. struct smc_connection *conn = &smc->conn;
  150. int diff_cons, diff_prod;
  151. if (!cdc->prod_flags.failover_validation) {
  152. if (smc_cdc_before(ntohs(cdc->seqno),
  153. conn->local_rx_ctrl.seqno))
  154. /* received seqno is old */
  155. return;
  156. }
  157. smc_curs_write(&prod_old,
  158. smc_curs_read(&conn->local_rx_ctrl.prod, conn),
  159. conn);
  160. smc_curs_write(&cons_old,
  161. smc_curs_read(&conn->local_rx_ctrl.cons, conn),
  162. conn);
  163. smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn);
  164. diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old,
  165. &conn->local_rx_ctrl.cons);
  166. if (diff_cons) {
  167. /* peer_rmbe_space is decreased during data transfer with RDMA
  168. * write
  169. */
  170. smp_mb__before_atomic();
  171. atomic_add(diff_cons, &conn->peer_rmbe_space);
  172. /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
  173. smp_mb__after_atomic();
  174. }
  175. diff_prod = smc_curs_diff(conn->rmbe_size, &prod_old,
  176. &conn->local_rx_ctrl.prod);
  177. if (diff_prod) {
  178. /* bytes_to_rcv is decreased in smc_recvmsg */
  179. smp_mb__before_atomic();
  180. atomic_add(diff_prod, &conn->bytes_to_rcv);
  181. /* guarantee 0 <= bytes_to_rcv <= rmbe_size */
  182. smp_mb__after_atomic();
  183. smc->sk.sk_data_ready(&smc->sk);
  184. }
  185. if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {
  186. smc->sk.sk_err = ECONNRESET;
  187. conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
  188. }
  189. if (smc_cdc_rxed_any_close_or_senddone(conn)) {
  190. smc->sk.sk_shutdown |= RCV_SHUTDOWN;
  191. if (smc->clcsock && smc->clcsock->sk)
  192. smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN;
  193. sock_set_flag(&smc->sk, SOCK_DONE);
  194. schedule_work(&conn->close_work);
  195. }
  196. /* piggy backed tx info */
  197. /* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */
  198. if (diff_cons && smc_tx_prepared_sends(conn)) {
  199. smc_tx_sndbuf_nonempty(conn);
  200. /* trigger socket release if connection closed */
  201. smc_close_wake_tx_prepared(smc);
  202. }
  203. /* socket connected but not accepted */
  204. if (!smc->sk.sk_socket)
  205. return;
  206. /* data available */
  207. if ((conn->local_rx_ctrl.prod_flags.write_blocked) ||
  208. (conn->local_rx_ctrl.prod_flags.cons_curs_upd_req))
  209. smc_tx_consumer_update(conn);
  210. }
  211. /* called under tasklet context */
  212. static inline void smc_cdc_msg_recv(struct smc_cdc_msg *cdc,
  213. struct smc_link *link, u64 wr_id)
  214. {
  215. struct smc_link_group *lgr = container_of(link, struct smc_link_group,
  216. lnk[SMC_SINGLE_LINK]);
  217. struct smc_connection *connection;
  218. struct smc_sock *smc;
  219. /* lookup connection */
  220. read_lock_bh(&lgr->conns_lock);
  221. connection = smc_lgr_find_conn(ntohl(cdc->token), lgr);
  222. if (!connection) {
  223. read_unlock_bh(&lgr->conns_lock);
  224. return;
  225. }
  226. smc = container_of(connection, struct smc_sock, conn);
  227. sock_hold(&smc->sk);
  228. read_unlock_bh(&lgr->conns_lock);
  229. bh_lock_sock(&smc->sk);
  230. smc_cdc_msg_recv_action(smc, link, cdc);
  231. bh_unlock_sock(&smc->sk);
  232. sock_put(&smc->sk); /* no free sk in softirq-context */
  233. }
  234. /***************************** init, exit, misc ******************************/
  235. static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf)
  236. {
  237. struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
  238. struct smc_cdc_msg *cdc = buf;
  239. if (wc->byte_len < offsetof(struct smc_cdc_msg, reserved))
  240. return; /* short message */
  241. if (cdc->len != sizeof(*cdc))
  242. return; /* invalid message */
  243. smc_cdc_msg_recv(cdc, link, wc->wr_id);
  244. }
  245. static struct smc_wr_rx_handler smc_cdc_rx_handlers[] = {
  246. {
  247. .handler = smc_cdc_rx_handler,
  248. .type = SMC_CDC_MSG_TYPE
  249. },
  250. {
  251. .handler = NULL,
  252. }
  253. };
  254. int __init smc_cdc_init(void)
  255. {
  256. struct smc_wr_rx_handler *handler;
  257. int rc = 0;
  258. for (handler = smc_cdc_rx_handlers; handler->handler; handler++) {
  259. INIT_HLIST_NODE(&handler->list);
  260. rc = smc_wr_rx_register_handler(handler);
  261. if (rc)
  262. break;
  263. }
  264. return rc;
  265. }