smc_cdc.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Shared Memory Communications over RDMA (SMC-R) and RoCE
  3. *
  4. * Connection Data Control (CDC)
  5. * handles flow control
  6. *
  7. * Copyright IBM Corp. 2016
  8. *
  9. * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
  10. */
  11. #include <linux/spinlock.h>
  12. #include "smc.h"
  13. #include "smc_wr.h"
  14. #include "smc_cdc.h"
  15. #include "smc_tx.h"
  16. #include "smc_rx.h"
  17. #include "smc_close.h"
  18. /********************************** send *************************************/
  19. struct smc_cdc_tx_pend {
  20. struct smc_connection *conn; /* socket connection */
  21. union smc_host_cursor cursor; /* tx sndbuf cursor sent */
  22. union smc_host_cursor p_cursor; /* rx RMBE cursor produced */
  23. u16 ctrl_seq; /* conn. tx sequence # */
  24. };
  25. /* handler for send/transmission completion of a CDC msg */
  26. static void smc_cdc_tx_handler(struct smc_wr_tx_pend_priv *pnd_snd,
  27. struct smc_link *link,
  28. enum ib_wc_status wc_status)
  29. {
  30. struct smc_cdc_tx_pend *cdcpend = (struct smc_cdc_tx_pend *)pnd_snd;
  31. struct smc_sock *smc;
  32. int diff;
  33. if (!cdcpend->conn)
  34. /* already dismissed */
  35. return;
  36. smc = container_of(cdcpend->conn, struct smc_sock, conn);
  37. bh_lock_sock(&smc->sk);
  38. if (!wc_status) {
  39. diff = smc_curs_diff(cdcpend->conn->sndbuf_size,
  40. &cdcpend->conn->tx_curs_fin,
  41. &cdcpend->cursor);
  42. /* sndbuf_space is decreased in smc_sendmsg */
  43. smp_mb__before_atomic();
  44. atomic_add(diff, &cdcpend->conn->sndbuf_space);
  45. /* guarantee 0 <= sndbuf_space <= sndbuf_size */
  46. smp_mb__after_atomic();
  47. smc_curs_write(&cdcpend->conn->tx_curs_fin,
  48. smc_curs_read(&cdcpend->cursor, cdcpend->conn),
  49. cdcpend->conn);
  50. }
  51. smc_tx_sndbuf_nonfull(smc);
  52. if (smc->sk.sk_state != SMC_ACTIVE)
  53. /* wake up smc_close_wait_tx_pends() */
  54. smc->sk.sk_state_change(&smc->sk);
  55. bh_unlock_sock(&smc->sk);
  56. }
  57. int smc_cdc_get_free_slot(struct smc_link *link,
  58. struct smc_wr_buf **wr_buf,
  59. struct smc_cdc_tx_pend **pend)
  60. {
  61. return smc_wr_tx_get_free_slot(link, smc_cdc_tx_handler, wr_buf,
  62. (struct smc_wr_tx_pend_priv **)pend);
  63. }
  64. static inline void smc_cdc_add_pending_send(struct smc_connection *conn,
  65. struct smc_cdc_tx_pend *pend)
  66. {
  67. BUILD_BUG_ON_MSG(
  68. sizeof(struct smc_cdc_msg) > SMC_WR_BUF_SIZE,
  69. "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_cdc_msg)");
  70. BUILD_BUG_ON_MSG(
  71. offsetof(struct smc_cdc_msg, reserved) > SMC_WR_TX_SIZE,
  72. "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()");
  73. BUILD_BUG_ON_MSG(
  74. sizeof(struct smc_cdc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE,
  75. "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_cdc_tx_pend)");
  76. pend->conn = conn;
  77. pend->cursor = conn->tx_curs_sent;
  78. pend->p_cursor = conn->local_tx_ctrl.prod;
  79. pend->ctrl_seq = conn->tx_cdc_seq;
  80. }
  81. int smc_cdc_msg_send(struct smc_connection *conn,
  82. struct smc_wr_buf *wr_buf,
  83. struct smc_cdc_tx_pend *pend)
  84. {
  85. struct smc_link *link;
  86. int rc;
  87. link = &conn->lgr->lnk[SMC_SINGLE_LINK];
  88. smc_cdc_add_pending_send(conn, pend);
  89. conn->tx_cdc_seq++;
  90. conn->local_tx_ctrl.seqno = conn->tx_cdc_seq;
  91. smc_host_msg_to_cdc((struct smc_cdc_msg *)wr_buf,
  92. &conn->local_tx_ctrl, conn);
  93. rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
  94. if (!rc)
  95. smc_curs_write(&conn->rx_curs_confirmed,
  96. smc_curs_read(&conn->local_tx_ctrl.cons, conn),
  97. conn);
  98. return rc;
  99. }
  100. int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn)
  101. {
  102. struct smc_cdc_tx_pend *pend;
  103. struct smc_wr_buf *wr_buf;
  104. int rc;
  105. rc = smc_cdc_get_free_slot(&conn->lgr->lnk[SMC_SINGLE_LINK], &wr_buf,
  106. &pend);
  107. if (rc)
  108. return rc;
  109. return smc_cdc_msg_send(conn, wr_buf, pend);
  110. }
  111. static bool smc_cdc_tx_filter(struct smc_wr_tx_pend_priv *tx_pend,
  112. unsigned long data)
  113. {
  114. struct smc_connection *conn = (struct smc_connection *)data;
  115. struct smc_cdc_tx_pend *cdc_pend =
  116. (struct smc_cdc_tx_pend *)tx_pend;
  117. return cdc_pend->conn == conn;
  118. }
  119. static void smc_cdc_tx_dismisser(struct smc_wr_tx_pend_priv *tx_pend)
  120. {
  121. struct smc_cdc_tx_pend *cdc_pend =
  122. (struct smc_cdc_tx_pend *)tx_pend;
  123. cdc_pend->conn = NULL;
  124. }
  125. void smc_cdc_tx_dismiss_slots(struct smc_connection *conn)
  126. {
  127. struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
  128. smc_wr_tx_dismiss_slots(link, SMC_CDC_MSG_TYPE,
  129. smc_cdc_tx_filter, smc_cdc_tx_dismisser,
  130. (unsigned long)conn);
  131. }
  132. bool smc_cdc_tx_has_pending(struct smc_connection *conn)
  133. {
  134. struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
  135. return smc_wr_tx_has_pending(link, SMC_CDC_MSG_TYPE,
  136. smc_cdc_tx_filter, (unsigned long)conn);
  137. }
  138. /********************************* receive ***********************************/
  139. static inline bool smc_cdc_before(u16 seq1, u16 seq2)
  140. {
  141. return (s16)(seq1 - seq2) < 0;
  142. }
  143. static void smc_cdc_msg_recv_action(struct smc_sock *smc,
  144. struct smc_link *link,
  145. struct smc_cdc_msg *cdc)
  146. {
  147. union smc_host_cursor cons_old, prod_old;
  148. struct smc_connection *conn = &smc->conn;
  149. int diff_cons, diff_prod;
  150. if (!cdc->prod_flags.failover_validation) {
  151. if (smc_cdc_before(ntohs(cdc->seqno),
  152. conn->local_rx_ctrl.seqno))
  153. /* received seqno is old */
  154. return;
  155. }
  156. smc_curs_write(&prod_old,
  157. smc_curs_read(&conn->local_rx_ctrl.prod, conn),
  158. conn);
  159. smc_curs_write(&cons_old,
  160. smc_curs_read(&conn->local_rx_ctrl.cons, conn),
  161. conn);
  162. smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn);
  163. diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old,
  164. &conn->local_rx_ctrl.cons);
  165. if (diff_cons) {
  166. /* peer_rmbe_space is decreased during data transfer with RDMA
  167. * write
  168. */
  169. smp_mb__before_atomic();
  170. atomic_add(diff_cons, &conn->peer_rmbe_space);
  171. /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
  172. smp_mb__after_atomic();
  173. }
  174. diff_prod = smc_curs_diff(conn->rmbe_size, &prod_old,
  175. &conn->local_rx_ctrl.prod);
  176. if (diff_prod) {
  177. /* bytes_to_rcv is decreased in smc_recvmsg */
  178. smp_mb__before_atomic();
  179. atomic_add(diff_prod, &conn->bytes_to_rcv);
  180. /* guarantee 0 <= bytes_to_rcv <= rmbe_size */
  181. smp_mb__after_atomic();
  182. smc->sk.sk_data_ready(&smc->sk);
  183. }
  184. if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {
  185. smc->sk.sk_err = ECONNRESET;
  186. conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
  187. }
  188. if (smc_cdc_rxed_any_close_or_senddone(conn)) {
  189. smc->sk.sk_shutdown |= RCV_SHUTDOWN;
  190. if (smc->clcsock && smc->clcsock->sk)
  191. smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN;
  192. sock_set_flag(&smc->sk, SOCK_DONE);
  193. schedule_work(&conn->close_work);
  194. }
  195. /* piggy backed tx info */
  196. /* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */
  197. if (diff_cons && smc_tx_prepared_sends(conn)) {
  198. smc_tx_sndbuf_nonempty(conn);
  199. /* trigger socket release if connection closed */
  200. smc_close_wake_tx_prepared(smc);
  201. }
  202. /* socket connected but not accepted */
  203. if (!smc->sk.sk_socket)
  204. return;
  205. /* data available */
  206. if ((conn->local_rx_ctrl.prod_flags.write_blocked) ||
  207. (conn->local_rx_ctrl.prod_flags.cons_curs_upd_req))
  208. smc_tx_consumer_update(conn);
  209. }
  210. /* called under tasklet context */
  211. static inline void smc_cdc_msg_recv(struct smc_cdc_msg *cdc,
  212. struct smc_link *link, u64 wr_id)
  213. {
  214. struct smc_link_group *lgr = container_of(link, struct smc_link_group,
  215. lnk[SMC_SINGLE_LINK]);
  216. struct smc_connection *connection;
  217. struct smc_sock *smc;
  218. /* lookup connection */
  219. read_lock_bh(&lgr->conns_lock);
  220. connection = smc_lgr_find_conn(ntohl(cdc->token), lgr);
  221. if (!connection) {
  222. read_unlock_bh(&lgr->conns_lock);
  223. return;
  224. }
  225. smc = container_of(connection, struct smc_sock, conn);
  226. sock_hold(&smc->sk);
  227. read_unlock_bh(&lgr->conns_lock);
  228. bh_lock_sock(&smc->sk);
  229. smc_cdc_msg_recv_action(smc, link, cdc);
  230. bh_unlock_sock(&smc->sk);
  231. sock_put(&smc->sk); /* no free sk in softirq-context */
  232. }
  233. /***************************** init, exit, misc ******************************/
  234. static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf)
  235. {
  236. struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
  237. struct smc_cdc_msg *cdc = buf;
  238. if (wc->byte_len < offsetof(struct smc_cdc_msg, reserved))
  239. return; /* short message */
  240. if (cdc->len != sizeof(*cdc))
  241. return; /* invalid message */
  242. smc_cdc_msg_recv(cdc, link, wc->wr_id);
  243. }
  244. static struct smc_wr_rx_handler smc_cdc_rx_handlers[] = {
  245. {
  246. .handler = smc_cdc_rx_handler,
  247. .type = SMC_CDC_MSG_TYPE
  248. },
  249. {
  250. .handler = NULL,
  251. }
  252. };
  253. int __init smc_cdc_init(void)
  254. {
  255. struct smc_wr_rx_handler *handler;
  256. int rc = 0;
  257. for (handler = smc_cdc_rx_handlers; handler->handler; handler++) {
  258. INIT_HLIST_NODE(&handler->list);
  259. rc = smc_wr_rx_register_handler(handler);
  260. if (rc)
  261. break;
  262. }
  263. return rc;
  264. }