2 * Shared Memory Communications over RDMA (SMC-R) and RoCE
4 * Connection Data Control (CDC)
7 * Copyright IBM Corp. 2016
9 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
12 #include <linux/spinlock.h>
19 #include "smc_close.h"
21 /********************************** send *************************************/
23 struct smc_cdc_tx_pend {
24 struct smc_connection *conn; /* socket connection */
25 union smc_host_cursor cursor; /* tx sndbuf cursor sent */
26 union smc_host_cursor p_cursor; /* rx RMBE cursor produced */
27 u16 ctrl_seq; /* conn. tx sequence # */
30 /* handler for send/transmission completion of a CDC msg */
31 static void smc_cdc_tx_handler(struct smc_wr_tx_pend_priv *pnd_snd,
32 struct smc_link *link,
33 enum ib_wc_status wc_status)
35 struct smc_cdc_tx_pend *cdcpend = (struct smc_cdc_tx_pend *)pnd_snd;
40 /* already dismissed */
43 smc = container_of(cdcpend->conn, struct smc_sock, conn);
44 bh_lock_sock(&smc->sk);
46 diff = smc_curs_diff(cdcpend->conn->sndbuf_size,
47 &cdcpend->conn->tx_curs_fin,
49 /* sndbuf_space is decreased in smc_sendmsg */
50 smp_mb__before_atomic();
51 atomic_add(diff, &cdcpend->conn->sndbuf_space);
52 /* guarantee 0 <= sndbuf_space <= sndbuf_size */
53 smp_mb__after_atomic();
54 smc_curs_write(&cdcpend->conn->tx_curs_fin,
55 smc_curs_read(&cdcpend->cursor, cdcpend->conn),
58 smc_tx_sndbuf_nonfull(smc);
59 if (smc->sk.sk_state != SMC_ACTIVE)
60 /* wake up smc_close_wait_tx_pends() */
61 smc->sk.sk_state_change(&smc->sk);
62 bh_unlock_sock(&smc->sk);
65 int smc_cdc_get_free_slot(struct smc_link *link,
66 struct smc_wr_buf **wr_buf,
67 struct smc_cdc_tx_pend **pend)
69 return smc_wr_tx_get_free_slot(link, smc_cdc_tx_handler, wr_buf,
70 (struct smc_wr_tx_pend_priv **)pend);
73 static inline void smc_cdc_add_pending_send(struct smc_connection *conn,
74 struct smc_cdc_tx_pend *pend)
77 sizeof(struct smc_cdc_msg) > SMC_WR_BUF_SIZE,
78 "must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_cdc_msg)");
80 offsetof(struct smc_cdc_msg, reserved) > SMC_WR_TX_SIZE,
81 "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()");
83 sizeof(struct smc_cdc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE,
84 "must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_cdc_tx_pend)");
86 pend->cursor = conn->tx_curs_sent;
87 pend->p_cursor = conn->local_tx_ctrl.prod;
88 pend->ctrl_seq = conn->tx_cdc_seq;
91 int smc_cdc_msg_send(struct smc_connection *conn,
92 struct smc_wr_buf *wr_buf,
93 struct smc_cdc_tx_pend *pend)
95 struct smc_link *link;
98 link = &conn->lgr->lnk[SMC_SINGLE_LINK];
100 smc_cdc_add_pending_send(conn, pend);
103 conn->local_tx_ctrl.seqno = conn->tx_cdc_seq;
104 smc_host_msg_to_cdc((struct smc_cdc_msg *)wr_buf,
105 &conn->local_tx_ctrl, conn);
106 rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
108 smc_curs_write(&conn->rx_curs_confirmed,
109 smc_curs_read(&conn->local_tx_ctrl.cons, conn),
115 int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn)
117 struct smc_cdc_tx_pend *pend;
118 struct smc_wr_buf *wr_buf;
121 rc = smc_cdc_get_free_slot(&conn->lgr->lnk[SMC_SINGLE_LINK], &wr_buf,
126 return smc_cdc_msg_send(conn, wr_buf, pend);
129 static bool smc_cdc_tx_filter(struct smc_wr_tx_pend_priv *tx_pend,
132 struct smc_connection *conn = (struct smc_connection *)data;
133 struct smc_cdc_tx_pend *cdc_pend =
134 (struct smc_cdc_tx_pend *)tx_pend;
136 return cdc_pend->conn == conn;
139 static void smc_cdc_tx_dismisser(struct smc_wr_tx_pend_priv *tx_pend)
141 struct smc_cdc_tx_pend *cdc_pend =
142 (struct smc_cdc_tx_pend *)tx_pend;
144 cdc_pend->conn = NULL;
147 void smc_cdc_tx_dismiss_slots(struct smc_connection *conn)
149 struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
151 smc_wr_tx_dismiss_slots(link, SMC_CDC_MSG_TYPE,
152 smc_cdc_tx_filter, smc_cdc_tx_dismisser,
153 (unsigned long)conn);
156 bool smc_cdc_tx_has_pending(struct smc_connection *conn)
158 struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
160 return smc_wr_tx_has_pending(link, SMC_CDC_MSG_TYPE,
161 smc_cdc_tx_filter, (unsigned long)conn);
164 /********************************* receive ***********************************/
166 static inline bool smc_cdc_before(u16 seq1, u16 seq2)
168 return (s16)(seq1 - seq2) < 0;
171 static void smc_cdc_msg_recv_action(struct smc_sock *smc,
172 struct smc_link *link,
173 struct smc_cdc_msg *cdc)
175 union smc_host_cursor cons_old, prod_old;
176 struct smc_connection *conn = &smc->conn;
177 int diff_cons, diff_prod;
179 if (!cdc->prod_flags.failover_validation) {
180 if (smc_cdc_before(ntohs(cdc->seqno),
181 conn->local_rx_ctrl.seqno))
182 /* received seqno is old */
185 smc_curs_write(&prod_old,
186 smc_curs_read(&conn->local_rx_ctrl.prod, conn),
188 smc_curs_write(&cons_old,
189 smc_curs_read(&conn->local_rx_ctrl.cons, conn),
191 smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn);
193 diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old,
194 &conn->local_rx_ctrl.cons);
196 /* peer_rmbe_space is decreased during data transfer with RDMA
199 smp_mb__before_atomic();
200 atomic_add(diff_cons, &conn->peer_rmbe_space);
201 /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
202 smp_mb__after_atomic();
205 diff_prod = smc_curs_diff(conn->rmbe_size, &prod_old,
206 &conn->local_rx_ctrl.prod);
208 /* bytes_to_rcv is decreased in smc_recvmsg */
209 smp_mb__before_atomic();
210 atomic_add(diff_prod, &conn->bytes_to_rcv);
211 /* guarantee 0 <= bytes_to_rcv <= rmbe_size */
212 smp_mb__after_atomic();
213 smc->sk.sk_data_ready(&smc->sk);
216 if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {
217 smc->sk.sk_err = ECONNRESET;
218 conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
220 if (smc_cdc_rxed_any_close_or_senddone(conn)) {
221 smc->sk.sk_shutdown |= RCV_SHUTDOWN;
222 if (smc->clcsock && smc->clcsock->sk)
223 smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN;
224 sock_set_flag(&smc->sk, SOCK_DONE);
225 schedule_work(&conn->close_work);
228 /* piggy backed tx info */
229 /* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */
230 if (diff_cons && smc_tx_prepared_sends(conn)) {
231 smc_tx_sndbuf_nonempty(conn);
232 /* trigger socket release if connection closed */
233 smc_close_wake_tx_prepared(smc);
236 /* socket connected but not accepted */
237 if (!smc->sk.sk_socket)
241 if ((conn->local_rx_ctrl.prod_flags.write_blocked) ||
242 (conn->local_rx_ctrl.prod_flags.cons_curs_upd_req))
243 smc_tx_consumer_update(conn);
246 /* called under tasklet context */
247 static inline void smc_cdc_msg_recv(struct smc_cdc_msg *cdc,
248 struct smc_link *link, u64 wr_id)
250 struct smc_link_group *lgr = container_of(link, struct smc_link_group,
251 lnk[SMC_SINGLE_LINK]);
252 struct smc_connection *connection;
253 struct smc_sock *smc;
255 /* lookup connection */
256 read_lock_bh(&lgr->conns_lock);
257 connection = smc_lgr_find_conn(ntohl(cdc->token), lgr);
259 read_unlock_bh(&lgr->conns_lock);
262 smc = container_of(connection, struct smc_sock, conn);
264 read_unlock_bh(&lgr->conns_lock);
265 bh_lock_sock(&smc->sk);
266 smc_cdc_msg_recv_action(smc, link, cdc);
267 bh_unlock_sock(&smc->sk);
268 sock_put(&smc->sk); /* no free sk in softirq-context */
271 /***************************** init, exit, misc ******************************/
273 static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf)
275 struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
276 struct smc_cdc_msg *cdc = buf;
278 if (wc->byte_len < offsetof(struct smc_cdc_msg, reserved))
279 return; /* short message */
280 if (cdc->len != sizeof(*cdc))
281 return; /* invalid message */
282 smc_cdc_msg_recv(cdc, link, wc->wr_id);
285 static struct smc_wr_rx_handler smc_cdc_rx_handlers[] = {
287 .handler = smc_cdc_rx_handler,
288 .type = SMC_CDC_MSG_TYPE
295 int __init smc_cdc_init(void)
297 struct smc_wr_rx_handler *handler;
300 for (handler = smc_cdc_rx_handlers; handler->handler; handler++) {
301 INIT_HLIST_NODE(&handler->list);
302 rc = smc_wr_rx_register_handler(handler);