xref: /linux/net/smc/smc_cdc.c (revision 83a37b3292f4aca799b355179ad6fbdd78a08e10)
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 
12 #include <linux/spinlock.h>
13 
14 #include "smc.h"
15 #include "smc_wr.h"
16 #include "smc_cdc.h"
17 #include "smc_tx.h"
18 #include "smc_rx.h"
19 #include "smc_close.h"
20 
21 /********************************** send *************************************/
22 
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 # */
28 };
29 
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)
34 {
35 	struct smc_cdc_tx_pend *cdcpend = (struct smc_cdc_tx_pend *)pnd_snd;
36 	struct smc_sock *smc;
37 	int diff;
38 
39 	if (!cdcpend->conn)
40 		/* already dismissed */
41 		return;
42 
43 	smc = container_of(cdcpend->conn, struct smc_sock, conn);
44 	bh_lock_sock(&smc->sk);
45 	if (!wc_status) {
46 		diff = smc_curs_diff(cdcpend->conn->sndbuf_size,
47 				     &cdcpend->conn->tx_curs_fin,
48 				     &cdcpend->cursor);
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),
56 			       cdcpend->conn);
57 	}
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);
63 }
64 
65 int smc_cdc_get_free_slot(struct smc_connection *conn,
66 			  struct smc_wr_buf **wr_buf,
67 			  struct smc_cdc_tx_pend **pend)
68 {
69 	struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
70 
71 	return smc_wr_tx_get_free_slot(link, smc_cdc_tx_handler, wr_buf,
72 				       (struct smc_wr_tx_pend_priv **)pend);
73 }
74 
75 static inline void smc_cdc_add_pending_send(struct smc_connection *conn,
76 					    struct smc_cdc_tx_pend *pend)
77 {
78 	BUILD_BUG_ON_MSG(
79 		sizeof(struct smc_cdc_msg) > SMC_WR_BUF_SIZE,
80 		"must increase SMC_WR_BUF_SIZE to at least sizeof(struct smc_cdc_msg)");
81 	BUILD_BUG_ON_MSG(
82 		offsetof(struct smc_cdc_msg, reserved) > SMC_WR_TX_SIZE,
83 		"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()");
84 	BUILD_BUG_ON_MSG(
85 		sizeof(struct smc_cdc_tx_pend) > SMC_WR_TX_PEND_PRIV_SIZE,
86 		"must increase SMC_WR_TX_PEND_PRIV_SIZE to at least sizeof(struct smc_cdc_tx_pend)");
87 	pend->conn = conn;
88 	pend->cursor = conn->tx_curs_sent;
89 	pend->p_cursor = conn->local_tx_ctrl.prod;
90 	pend->ctrl_seq = conn->tx_cdc_seq;
91 }
92 
93 int smc_cdc_msg_send(struct smc_connection *conn,
94 		     struct smc_wr_buf *wr_buf,
95 		     struct smc_cdc_tx_pend *pend)
96 {
97 	struct smc_link *link;
98 	int rc;
99 
100 	link = &conn->lgr->lnk[SMC_SINGLE_LINK];
101 
102 	smc_cdc_add_pending_send(conn, pend);
103 
104 	conn->tx_cdc_seq++;
105 	conn->local_tx_ctrl.seqno = conn->tx_cdc_seq;
106 	smc_host_msg_to_cdc((struct smc_cdc_msg *)wr_buf,
107 			    &conn->local_tx_ctrl, conn);
108 	rc = smc_wr_tx_send(link, (struct smc_wr_tx_pend_priv *)pend);
109 	if (!rc)
110 		smc_curs_write(&conn->rx_curs_confirmed,
111 			       smc_curs_read(&conn->local_tx_ctrl.cons, conn),
112 			       conn);
113 
114 	return rc;
115 }
116 
117 int smc_cdc_get_slot_and_msg_send(struct smc_connection *conn)
118 {
119 	struct smc_cdc_tx_pend *pend;
120 	struct smc_wr_buf *wr_buf;
121 	int rc;
122 
123 	rc = smc_cdc_get_free_slot(conn, &wr_buf, &pend);
124 	if (rc)
125 		return rc;
126 
127 	return smc_cdc_msg_send(conn, wr_buf, pend);
128 }
129 
130 static bool smc_cdc_tx_filter(struct smc_wr_tx_pend_priv *tx_pend,
131 			      unsigned long data)
132 {
133 	struct smc_connection *conn = (struct smc_connection *)data;
134 	struct smc_cdc_tx_pend *cdc_pend =
135 		(struct smc_cdc_tx_pend *)tx_pend;
136 
137 	return cdc_pend->conn == conn;
138 }
139 
140 static void smc_cdc_tx_dismisser(struct smc_wr_tx_pend_priv *tx_pend)
141 {
142 	struct smc_cdc_tx_pend *cdc_pend =
143 		(struct smc_cdc_tx_pend *)tx_pend;
144 
145 	cdc_pend->conn = NULL;
146 }
147 
148 void smc_cdc_tx_dismiss_slots(struct smc_connection *conn)
149 {
150 	struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
151 
152 	smc_wr_tx_dismiss_slots(link, SMC_CDC_MSG_TYPE,
153 				smc_cdc_tx_filter, smc_cdc_tx_dismisser,
154 				(unsigned long)conn);
155 }
156 
157 bool smc_cdc_tx_has_pending(struct smc_connection *conn)
158 {
159 	struct smc_link *link = &conn->lgr->lnk[SMC_SINGLE_LINK];
160 
161 	return smc_wr_tx_has_pending(link, SMC_CDC_MSG_TYPE,
162 				     smc_cdc_tx_filter, (unsigned long)conn);
163 }
164 
165 /********************************* receive ***********************************/
166 
167 static inline bool smc_cdc_before(u16 seq1, u16 seq2)
168 {
169 	return (s16)(seq1 - seq2) < 0;
170 }
171 
172 static void smc_cdc_msg_recv_action(struct smc_sock *smc,
173 				    struct smc_link *link,
174 				    struct smc_cdc_msg *cdc)
175 {
176 	union smc_host_cursor cons_old, prod_old;
177 	struct smc_connection *conn = &smc->conn;
178 	int diff_cons, diff_prod;
179 
180 	if (!cdc->prod_flags.failover_validation) {
181 		if (smc_cdc_before(ntohs(cdc->seqno),
182 				   conn->local_rx_ctrl.seqno))
183 			/* received seqno is old */
184 			return;
185 	}
186 	smc_curs_write(&prod_old,
187 		       smc_curs_read(&conn->local_rx_ctrl.prod, conn),
188 		       conn);
189 	smc_curs_write(&cons_old,
190 		       smc_curs_read(&conn->local_rx_ctrl.cons, conn),
191 		       conn);
192 	smc_cdc_msg_to_host(&conn->local_rx_ctrl, cdc, conn);
193 
194 	diff_cons = smc_curs_diff(conn->peer_rmbe_size, &cons_old,
195 				  &conn->local_rx_ctrl.cons);
196 	if (diff_cons) {
197 		/* peer_rmbe_space is decreased during data transfer with RDMA
198 		 * write
199 		 */
200 		smp_mb__before_atomic();
201 		atomic_add(diff_cons, &conn->peer_rmbe_space);
202 		/* guarantee 0 <= peer_rmbe_space <= peer_rmbe_size */
203 		smp_mb__after_atomic();
204 	}
205 
206 	diff_prod = smc_curs_diff(conn->rmbe_size, &prod_old,
207 				  &conn->local_rx_ctrl.prod);
208 	if (diff_prod) {
209 		/* bytes_to_rcv is decreased in smc_recvmsg */
210 		smp_mb__before_atomic();
211 		atomic_add(diff_prod, &conn->bytes_to_rcv);
212 		/* guarantee 0 <= bytes_to_rcv <= rmbe_size */
213 		smp_mb__after_atomic();
214 		smc->sk.sk_data_ready(&smc->sk);
215 	}
216 
217 	if (conn->local_rx_ctrl.conn_state_flags.peer_conn_abort) {
218 		smc->sk.sk_err = ECONNRESET;
219 		conn->local_tx_ctrl.conn_state_flags.peer_conn_abort = 1;
220 	}
221 	if (smc_cdc_rxed_any_close_or_senddone(conn)) {
222 		smc->sk.sk_shutdown |= RCV_SHUTDOWN;
223 		if (smc->clcsock && smc->clcsock->sk)
224 			smc->clcsock->sk->sk_shutdown |= RCV_SHUTDOWN;
225 		sock_set_flag(&smc->sk, SOCK_DONE);
226 		schedule_work(&conn->close_work);
227 	}
228 
229 	/* piggy backed tx info */
230 	/* trigger sndbuf consumer: RDMA write into peer RMBE and CDC */
231 	if (diff_cons && smc_tx_prepared_sends(conn)) {
232 		smc_tx_sndbuf_nonempty(conn);
233 		/* trigger socket release if connection closed */
234 		smc_close_wake_tx_prepared(smc);
235 	}
236 
237 	/* socket connected but not accepted */
238 	if (!smc->sk.sk_socket)
239 		return;
240 
241 	/* data available */
242 	if ((conn->local_rx_ctrl.prod_flags.write_blocked) ||
243 	    (conn->local_rx_ctrl.prod_flags.cons_curs_upd_req))
244 		smc_tx_consumer_update(conn);
245 }
246 
247 /* called under tasklet context */
248 static inline void smc_cdc_msg_recv(struct smc_cdc_msg *cdc,
249 				    struct smc_link *link, u64 wr_id)
250 {
251 	struct smc_link_group *lgr = container_of(link, struct smc_link_group,
252 						  lnk[SMC_SINGLE_LINK]);
253 	struct smc_connection *connection;
254 	struct smc_sock *smc;
255 
256 	/* lookup connection */
257 	read_lock_bh(&lgr->conns_lock);
258 	connection = smc_lgr_find_conn(ntohl(cdc->token), lgr);
259 	if (!connection) {
260 		read_unlock_bh(&lgr->conns_lock);
261 		return;
262 	}
263 	smc = container_of(connection, struct smc_sock, conn);
264 	sock_hold(&smc->sk);
265 	read_unlock_bh(&lgr->conns_lock);
266 	bh_lock_sock(&smc->sk);
267 	smc_cdc_msg_recv_action(smc, link, cdc);
268 	bh_unlock_sock(&smc->sk);
269 	sock_put(&smc->sk); /* no free sk in softirq-context */
270 }
271 
272 /***************************** init, exit, misc ******************************/
273 
274 static void smc_cdc_rx_handler(struct ib_wc *wc, void *buf)
275 {
276 	struct smc_link *link = (struct smc_link *)wc->qp->qp_context;
277 	struct smc_cdc_msg *cdc = buf;
278 
279 	if (wc->byte_len < offsetof(struct smc_cdc_msg, reserved))
280 		return; /* short message */
281 	if (cdc->len != sizeof(*cdc))
282 		return; /* invalid message */
283 	smc_cdc_msg_recv(cdc, link, wc->wr_id);
284 }
285 
286 static struct smc_wr_rx_handler smc_cdc_rx_handlers[] = {
287 	{
288 		.handler	= smc_cdc_rx_handler,
289 		.type		= SMC_CDC_MSG_TYPE
290 	},
291 	{
292 		.handler	= NULL,
293 	}
294 };
295 
296 int __init smc_cdc_init(void)
297 {
298 	struct smc_wr_rx_handler *handler;
299 	int rc = 0;
300 
301 	for (handler = smc_cdc_rx_handlers; handler->handler; handler++) {
302 		INIT_HLIST_NODE(&handler->list);
303 		rc = smc_wr_rx_register_handler(handler);
304 		if (rc)
305 			break;
306 	}
307 	return rc;
308 }
309