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