blob: 77fe16967376612fb3e620208feff2dc235508fa [file] [log] [blame]
Ursula Braun5f083182017-01-09 16:55:22 +01001/*
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
12#include <linux/spinlock.h>
13
14#include "smc.h"
15#include "smc_wr.h"
16#include "smc_cdc.h"
Ursula Braune6727f32017-01-09 16:55:23 +010017#include "smc_tx.h"
Ursula Braun5f083182017-01-09 16:55:22 +010018
19/********************************** send *************************************/
20
21struct smc_cdc_tx_pend {
22 struct smc_connection *conn; /* socket connection */
23 union smc_host_cursor cursor; /* tx sndbuf cursor sent */
24 union smc_host_cursor p_cursor; /* rx RMBE cursor produced */
25 u16 ctrl_seq; /* conn. tx sequence # */
26};
27
28/* handler for send/transmission completion of a CDC msg */
29static void smc_cdc_tx_handler(struct smc_wr_tx_pend_priv *pnd_snd,
30 struct smc_link *link,
31 enum ib_wc_status wc_status)
32{
33 struct smc_cdc_tx_pend *cdcpend = (struct smc_cdc_tx_pend *)pnd_snd;
34 struct smc_sock *smc;
35 int diff;
36
37 if (!cdcpend->conn)
38 /* already dismissed */
39 return;
40
41 smc = container_of(cdcpend->conn, struct smc_sock, conn);
42 bh_lock_sock(&smc->sk);
43 if (!wc_status) {
44 diff = smc_curs_diff(cdcpend->conn->sndbuf_size,
45 &cdcpend->conn->tx_curs_fin,
46 &cdcpend->cursor);
47 /* sndbuf_space is decreased in smc_sendmsg */
48 smp_mb__before_atomic();
49 atomic_add(diff, &cdcpend->conn->sndbuf_space);
50 /* guarantee 0 <= sndbuf_space <= sndbuf_size */
51 smp_mb__after_atomic();
52 smc_curs_write(&cdcpend->conn->tx_curs_fin,
53 smc_curs_read(&cdcpend->cursor, cdcpend->conn),
54 cdcpend->conn);
55 }
Ursula Braune6727f32017-01-09 16:55:23 +010056 smc_tx_sndbuf_nonfull(smc);
Ursula Braun5f083182017-01-09 16:55:22 +010057 bh_unlock_sock(&smc->sk);
58}
59
60int smc_cdc_get_free_slot(struct smc_link *link,
61 struct smc_wr_buf **wr_buf,
62 struct smc_cdc_tx_pend **pend)
63{
64 return smc_wr_tx_get_free_slot(link, smc_cdc_tx_handler, wr_buf,
65 (struct smc_wr_tx_pend_priv **)pend);
66}
67
68static 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 offsetof(struct smc_cdc_msg, reserved) > 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
86int smc_cdc_msg_send(struct smc_connection *conn,
87 struct smc_wr_buf *wr_buf,
88 struct smc_cdc_tx_pend *pend)
89{
90 struct smc_link *link;
91 int rc;
92
93 link = &conn->lgr->lnk[SMC_SINGLE_LINK];
94
95 smc_cdc_add_pending_send(conn, pend);
96
97 conn->tx_cdc_seq++;
98 conn->local_tx_ctrl.seqno = conn->tx_cdc_seq;
99 smc_host_msg_to_cdc((struct smc_cdc_msg *)wr_buf,
100 &conn->local_tx_ctrl, conn);
101 rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
102 if (!rc)
103 smc_curs_write(&conn->rx_curs_confirmed,
104 smc_curs_read(&conn->local_tx_ctrl.cons, conn),
105 conn);
106
107 return rc;
108}
109
110int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn)
111{
112 struct smc_cdc_tx_pend *pend;
113 struct smc_wr_buf *wr_buf;
114 int rc;
115
116 rc = smc_cdc_get_free_slot(&conn->lgr->lnk[SMC_SINGLE_LINK], &wr_buf,
117 &pend);
118 if (rc)
119 return rc;
120
121 return smc_cdc_msg_send(conn, wr_buf, pend);
122}
123
124static 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
131 return cdc_pend->conn == conn;
132}
133
134static void smc_cdc_tx_dismisser(struct smc_wr_tx_pend_priv *tx_pend)
135{
136 struct smc_cdc_tx_pend *cdc_pend =
137 (struct smc_cdc_tx_pend *)tx_pend;
138
139 cdc_pend->conn = NULL;
140}
141
142void smc_cdc_tx_dismiss_slots(struct smc_connection *conn)
143{
144 struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
145
146 smc_wr_tx_dismiss_slots(link, SMC_CDC_MSG_TYPE,
147 smc_cdc_tx_filter, smc_cdc_tx_dismisser,
148 (unsigned long)conn);
149}
150
151/********************************* receive ***********************************/
152
153static inline bool smc_cdc_before(u16 seq1, u16 seq2)
154{
155 return (s16)(seq1 - seq2) < 0;
156}
157
158static void smc_cdc_msg_recv_action(struct smc_sock *smc,
159 struct smc_link *link,
160 struct smc_cdc_msg *cdc)
161{
162 union smc_host_cursor cons_old, prod_old;
163 struct smc_connection *conn = &smc->conn;
164 int diff_cons, diff_prod;
165
166 if (!cdc->prod_flags.failover_validation) {
167 if (smc_cdc_before(ntohs(cdc->seqno),
168 conn->local_rx_ctrl.seqno))
169 /* received seqno is old */
170 return;
171 }
172 smc_curs_write(&prod_old,
173 smc_curs_read(&conn->local_rx_ctrl.prod, conn),
174 conn);
175 smc_curs_write(&cons_old,
176 smc_curs_read(&conn->local_rx_ctrl.cons, conn),
177 conn);
178 smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn);
179
180 diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old,
181 &conn->local_rx_ctrl.cons);
182 if (diff_cons) {
183 /* peer_rmbe_space is decreased during data transfer with RDMA
184 * write
185 */
186 smp_mb__before_atomic();
187 atomic_add(diff_cons, &conn->peer_rmbe_space);
188 /* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
189 smp_mb__after_atomic();
190 }
191
192 diff_prod = smc_curs_diff(conn->rmbe_size, &prod_old,
193 &conn->local_rx_ctrl.prod);
194 if (diff_prod) {
195 /* bytes_to_rcv is decreased in smc_recvmsg */
196 smp_mb__before_atomic();
197 atomic_add(diff_prod, &conn->bytes_to_rcv);
198 /* guarantee 0 <= bytes_to_rcv <= rmbe_size */
199 smp_mb__after_atomic();
200 }
201
202 if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort)
203 smc->sk.sk_err = ECONNRESET;
204 if (smc_cdc_rxed_any_close_or_senddone(conn))
205 /* subsequent patch: terminate connection */
206
207 /* piggy backed tx info */
Ursula Braune6727f32017-01-09 16:55:23 +0100208 /* 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);
Ursula Braun5f083182017-01-09 16:55:22 +0100211
212 /* subsequent patch: trigger socket release if connection closed */
213
214 /* socket connected but not accepted */
215 if (!smc->sk.sk_socket)
216 return;
217
218 /* data available */
219 /* subsequent patch: send delayed ack, wake receivers */
220}
221
222/* called under tasklet context */
223static inline void smc_cdc_msg_recv(struct smc_cdc_msg *cdc,
224 struct smc_link *link, u64 wr_id)
225{
226 struct smc_link_group *lgr = container_of(link, struct smc_link_group,
227 lnk[SMC_SINGLE_LINK]);
228 struct smc_connection *connection;
229 struct smc_sock *smc;
230
231 /* lookup connection */
232 read_lock_bh(&lgr->conns_lock);
233 connection = smc_lgr_find_conn(ntohl(cdc->token), lgr);
234 if (!connection) {
235 read_unlock_bh(&lgr->conns_lock);
236 return;
237 }
238 smc = container_of(connection, struct smc_sock, conn);
239 sock_hold(&smc->sk);
240 read_unlock_bh(&lgr->conns_lock);
241 bh_lock_sock(&smc->sk);
242 smc_cdc_msg_recv_action(smc, link, cdc);
243 bh_unlock_sock(&smc->sk);
244 sock_put(&smc->sk); /* no free sk in softirq-context */
245}
246
247/***************************** init, exit, misc ******************************/
248
249static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf)
250{
251 struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
252 struct smc_cdc_msg *cdc = buf;
253
254 if (wc->byte_len < offsetof(struct smc_cdc_msg, reserved))
255 return; /* short message */
256 if (cdc->len != sizeof(*cdc))
257 return; /* invalid message */
258 smc_cdc_msg_recv(cdc, link, wc->wr_id);
259}
260
261static struct smc_wr_rx_handler smc_cdc_rx_handlers[] = {
262 {
263 .handler = smc_cdc_rx_handler,
264 .type = SMC_CDC_MSG_TYPE
265 },
266 {
267 .handler = NULL,
268 }
269};
270
271int __init smc_cdc_init(void)
272{
273 struct smc_wr_rx_handler *handler;
274 int rc = 0;
275
276 for (handler = smc_cdc_rx_handlers; handler->handler; handler++) {
277 INIT_HLIST_NODE(&handler->list);
278 rc = smc_wr_rx_register_handler(handler);
279 if (rc)
280 break;
281 }
282 return rc;
283}