1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
4 *
5 * AF_SMC protocol family socket handler keeping the AF_INET sock address type
6 * applies to SOCK_STREAM sockets only
7 * offers an alternative communication option for TCP-protocol sockets
8 * applicable with RoCE-cards only
9 *
10 * Initial restrictions:
11 * - support for alternate links postponed
12 *
13 * Copyright IBM Corp. 2016, 2018
14 *
15 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
16 * based on prototype from Frank Blaschka
17 */
18
19 #define KMSG_COMPONENT "smc"
20 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
21
22 #include <linux/module.h>
23 #include <linux/socket.h>
24 #include <linux/workqueue.h>
25 #include <linux/in.h>
26 #include <linux/sched/signal.h>
27 #include <linux/if_vlan.h>
28 #include <linux/rcupdate_wait.h>
29 #include <linux/ctype.h>
30
31 #include <net/sock.h>
32 #include <net/tcp.h>
33 #include <net/smc.h>
34 #include <asm/ioctls.h>
35
36 #include <net/net_namespace.h>
37 #include <net/netns/generic.h>
38 #include "smc_netns.h"
39
40 #include "smc.h"
41 #include "smc_clc.h"
42 #include "smc_llc.h"
43 #include "smc_cdc.h"
44 #include "smc_core.h"
45 #include "smc_ib.h"
46 #include "smc_ism.h"
47 #include "smc_pnet.h"
48 #include "smc_tx.h"
49 #include "smc_rx.h"
50 #include "smc_close.h"
51
52 static DEFINE_MUTEX(smc_server_lgr_pending); /* serialize link group
53 * creation on server
54 */
55 static DEFINE_MUTEX(smc_client_lgr_pending); /* serialize link group
56 * creation on client
57 */
58
59 struct workqueue_struct *smc_hs_wq; /* wq for handshake work */
60 struct workqueue_struct *smc_close_wq; /* wq for close work */
61
62 static void smc_tcp_listen_work(struct work_struct *);
63 static void smc_connect_work(struct work_struct *);
64
smc_set_keepalive(struct sock * sk,int val)65 static void smc_set_keepalive(struct sock *sk, int val)
66 {
67 struct smc_sock *smc = smc_sk(sk);
68
69 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
70 }
71
72 static struct smc_hashinfo smc_v4_hashinfo = {
73 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
74 };
75
76 static struct smc_hashinfo smc_v6_hashinfo = {
77 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
78 };
79
smc_hash_sk(struct sock * sk)80 int smc_hash_sk(struct sock *sk)
81 {
82 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
83 struct hlist_head *head;
84
85 head = &h->ht;
86
87 write_lock_bh(&h->lock);
88 sk_add_node(sk, head);
89 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
90 write_unlock_bh(&h->lock);
91
92 return 0;
93 }
94 EXPORT_SYMBOL_GPL(smc_hash_sk);
95
smc_unhash_sk(struct sock * sk)96 void smc_unhash_sk(struct sock *sk)
97 {
98 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
99
100 write_lock_bh(&h->lock);
101 if (sk_del_node_init(sk))
102 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
103 write_unlock_bh(&h->lock);
104 }
105 EXPORT_SYMBOL_GPL(smc_unhash_sk);
106
107 struct proto smc_proto = {
108 .name = "SMC",
109 .owner = THIS_MODULE,
110 .keepalive = smc_set_keepalive,
111 .hash = smc_hash_sk,
112 .unhash = smc_unhash_sk,
113 .obj_size = sizeof(struct smc_sock),
114 .h.smc_hash = &smc_v4_hashinfo,
115 .slab_flags = SLAB_TYPESAFE_BY_RCU,
116 };
117 EXPORT_SYMBOL_GPL(smc_proto);
118
119 struct proto smc_proto6 = {
120 .name = "SMC6",
121 .owner = THIS_MODULE,
122 .keepalive = smc_set_keepalive,
123 .hash = smc_hash_sk,
124 .unhash = smc_unhash_sk,
125 .obj_size = sizeof(struct smc_sock),
126 .h.smc_hash = &smc_v6_hashinfo,
127 .slab_flags = SLAB_TYPESAFE_BY_RCU,
128 };
129 EXPORT_SYMBOL_GPL(smc_proto6);
130
smc_restore_fallback_changes(struct smc_sock * smc)131 static void smc_restore_fallback_changes(struct smc_sock *smc)
132 {
133 if (smc->clcsock->file) { /* non-accepted sockets have no file yet */
134 smc->clcsock->file->private_data = smc->sk.sk_socket;
135 smc->clcsock->file = NULL;
136 }
137 }
138
__smc_release(struct smc_sock * smc)139 static int __smc_release(struct smc_sock *smc)
140 {
141 struct sock *sk = &smc->sk;
142 int rc = 0;
143
144 if (!smc->use_fallback) {
145 rc = smc_close_active(smc);
146 sock_set_flag(sk, SOCK_DEAD);
147 sk->sk_shutdown |= SHUTDOWN_MASK;
148 } else {
149 if (sk->sk_state != SMC_CLOSED) {
150 if (sk->sk_state != SMC_LISTEN &&
151 sk->sk_state != SMC_INIT)
152 sock_put(sk); /* passive closing */
153 if (sk->sk_state == SMC_LISTEN) {
154 /* wake up clcsock accept */
155 rc = kernel_sock_shutdown(smc->clcsock,
156 SHUT_RDWR);
157 }
158 sk->sk_state = SMC_CLOSED;
159 sk->sk_state_change(sk);
160 }
161 smc_restore_fallback_changes(smc);
162 }
163
164 sk->sk_prot->unhash(sk);
165
166 if (sk->sk_state == SMC_CLOSED) {
167 if (smc->clcsock) {
168 release_sock(sk);
169 smc_clcsock_release(smc);
170 lock_sock(sk);
171 }
172 if (!smc->use_fallback)
173 smc_conn_free(&smc->conn);
174 }
175
176 return rc;
177 }
178
smc_release(struct socket * sock)179 static int smc_release(struct socket *sock)
180 {
181 struct sock *sk = sock->sk;
182 struct smc_sock *smc;
183 int rc = 0;
184
185 if (!sk)
186 goto out;
187
188 sock_hold(sk); /* sock_put below */
189 smc = smc_sk(sk);
190
191 /* cleanup for a dangling non-blocking connect */
192 if (smc->connect_nonblock && sk->sk_state == SMC_INIT)
193 tcp_abort(smc->clcsock->sk, ECONNABORTED);
194
195 if (cancel_work_sync(&smc->connect_work))
196 sock_put(&smc->sk); /* sock_hold in smc_connect for passive closing */
197
198 if (sk->sk_state == SMC_LISTEN)
199 /* smc_close_non_accepted() is called and acquires
200 * sock lock for child sockets again
201 */
202 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
203 else
204 lock_sock(sk);
205
206 rc = __smc_release(smc);
207
208 /* detach socket */
209 sock_orphan(sk);
210 sock->sk = NULL;
211 release_sock(sk);
212
213 sock_put(sk); /* sock_hold above */
214 sock_put(sk); /* final sock_put */
215 out:
216 return rc;
217 }
218
smc_destruct(struct sock * sk)219 static void smc_destruct(struct sock *sk)
220 {
221 if (sk->sk_state != SMC_CLOSED)
222 return;
223 if (!sock_flag(sk, SOCK_DEAD))
224 return;
225
226 sk_refcnt_debug_dec(sk);
227 }
228
smc_sock_alloc(struct net * net,struct socket * sock,int protocol)229 static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
230 int protocol)
231 {
232 struct smc_sock *smc;
233 struct proto *prot;
234 struct sock *sk;
235
236 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
237 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
238 if (!sk)
239 return NULL;
240
241 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
242 sk->sk_state = SMC_INIT;
243 sk->sk_destruct = smc_destruct;
244 sk->sk_protocol = protocol;
245 smc = smc_sk(sk);
246 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
247 INIT_WORK(&smc->connect_work, smc_connect_work);
248 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
249 INIT_LIST_HEAD(&smc->accept_q);
250 spin_lock_init(&smc->accept_q_lock);
251 spin_lock_init(&smc->conn.send_lock);
252 sk->sk_prot->hash(sk);
253 sk_refcnt_debug_inc(sk);
254 mutex_init(&smc->clcsock_release_lock);
255
256 return sk;
257 }
258
smc_bind(struct socket * sock,struct sockaddr * uaddr,int addr_len)259 static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
260 int addr_len)
261 {
262 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
263 struct sock *sk = sock->sk;
264 struct smc_sock *smc;
265 int rc;
266
267 smc = smc_sk(sk);
268
269 /* replicate tests from inet_bind(), to be safe wrt. future changes */
270 rc = -EINVAL;
271 if (addr_len < sizeof(struct sockaddr_in))
272 goto out;
273
274 rc = -EAFNOSUPPORT;
275 if (addr->sin_family != AF_INET &&
276 addr->sin_family != AF_INET6 &&
277 addr->sin_family != AF_UNSPEC)
278 goto out;
279 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
280 if (addr->sin_family == AF_UNSPEC &&
281 addr->sin_addr.s_addr != htonl(INADDR_ANY))
282 goto out;
283
284 lock_sock(sk);
285
286 /* Check if socket is already active */
287 rc = -EINVAL;
288 if (sk->sk_state != SMC_INIT || smc->connect_nonblock)
289 goto out_rel;
290
291 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
292 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
293
294 out_rel:
295 release_sock(sk);
296 out:
297 return rc;
298 }
299
smc_copy_sock_settings(struct sock * nsk,struct sock * osk,unsigned long mask)300 static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
301 unsigned long mask)
302 {
303 /* options we don't get control via setsockopt for */
304 nsk->sk_type = osk->sk_type;
305 nsk->sk_sndbuf = osk->sk_sndbuf;
306 nsk->sk_rcvbuf = osk->sk_rcvbuf;
307 nsk->sk_sndtimeo = osk->sk_sndtimeo;
308 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
309 nsk->sk_mark = osk->sk_mark;
310 nsk->sk_priority = osk->sk_priority;
311 nsk->sk_rcvlowat = osk->sk_rcvlowat;
312 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
313 nsk->sk_err = osk->sk_err;
314
315 nsk->sk_flags &= ~mask;
316 nsk->sk_flags |= osk->sk_flags & mask;
317 }
318
319 #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
320 (1UL << SOCK_KEEPOPEN) | \
321 (1UL << SOCK_LINGER) | \
322 (1UL << SOCK_BROADCAST) | \
323 (1UL << SOCK_TIMESTAMP) | \
324 (1UL << SOCK_DBG) | \
325 (1UL << SOCK_RCVTSTAMP) | \
326 (1UL << SOCK_RCVTSTAMPNS) | \
327 (1UL << SOCK_LOCALROUTE) | \
328 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
329 (1UL << SOCK_RXQ_OVFL) | \
330 (1UL << SOCK_WIFI_STATUS) | \
331 (1UL << SOCK_NOFCS) | \
332 (1UL << SOCK_FILTER_LOCKED) | \
333 (1UL << SOCK_TSTAMP_NEW))
334 /* copy only relevant settings and flags of SOL_SOCKET level from smc to
335 * clc socket (since smc is not called for these options from net/core)
336 */
smc_copy_sock_settings_to_clc(struct smc_sock * smc)337 static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
338 {
339 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
340 }
341
342 #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
343 (1UL << SOCK_KEEPOPEN) | \
344 (1UL << SOCK_LINGER) | \
345 (1UL << SOCK_DBG))
346 /* copy only settings and flags relevant for smc from clc to smc socket */
smc_copy_sock_settings_to_smc(struct smc_sock * smc)347 static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
348 {
349 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
350 }
351
352 /* register the new rmb on all links */
smcr_lgr_reg_rmbs(struct smc_link * link,struct smc_buf_desc * rmb_desc)353 static int smcr_lgr_reg_rmbs(struct smc_link *link,
354 struct smc_buf_desc *rmb_desc)
355 {
356 struct smc_link_group *lgr = link->lgr;
357 int i, rc = 0;
358
359 rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY);
360 if (rc)
361 return rc;
362 /* protect against parallel smc_llc_cli_rkey_exchange() and
363 * parallel smcr_link_reg_rmb()
364 */
365 mutex_lock(&lgr->llc_conf_mutex);
366 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
367 if (!smc_link_active(&lgr->lnk[i]))
368 continue;
369 rc = smcr_link_reg_rmb(&lgr->lnk[i], rmb_desc);
370 if (rc)
371 goto out;
372 }
373
374 /* exchange confirm_rkey msg with peer */
375 rc = smc_llc_do_confirm_rkey(link, rmb_desc);
376 if (rc) {
377 rc = -EFAULT;
378 goto out;
379 }
380 rmb_desc->is_conf_rkey = true;
381 out:
382 mutex_unlock(&lgr->llc_conf_mutex);
383 smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
384 return rc;
385 }
386
smcr_clnt_conf_first_link(struct smc_sock * smc)387 static int smcr_clnt_conf_first_link(struct smc_sock *smc)
388 {
389 struct smc_link *link = smc->conn.lnk;
390 struct smc_llc_qentry *qentry;
391 int rc;
392
393 /* receive CONFIRM LINK request from server over RoCE fabric */
394 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
395 SMC_LLC_CONFIRM_LINK);
396 if (!qentry) {
397 struct smc_clc_msg_decline dclc;
398
399 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
400 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
401 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
402 }
403 smc_llc_save_peer_uid(qentry);
404 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_REQ);
405 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
406 if (rc)
407 return SMC_CLC_DECL_RMBE_EC;
408
409 rc = smc_ib_modify_qp_rts(link);
410 if (rc)
411 return SMC_CLC_DECL_ERR_RDYLNK;
412
413 smc_wr_remember_qp_attr(link);
414
415 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
416 return SMC_CLC_DECL_ERR_REGRMB;
417
418 /* confirm_rkey is implicit on 1st contact */
419 smc->conn.rmb_desc->is_conf_rkey = true;
420
421 /* send CONFIRM LINK response over RoCE fabric */
422 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
423 if (rc < 0)
424 return SMC_CLC_DECL_TIMEOUT_CL;
425
426 smc_llc_link_active(link);
427 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
428
429 /* optional 2nd link, receive ADD LINK request from server */
430 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
431 SMC_LLC_ADD_LINK);
432 if (!qentry) {
433 struct smc_clc_msg_decline dclc;
434
435 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
436 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
437 if (rc == -EAGAIN)
438 rc = 0; /* no DECLINE received, go with one link */
439 return rc;
440 }
441 smc_llc_flow_qentry_clr(&link->lgr->llc_flow_lcl);
442 smc_llc_cli_add_link(link, qentry);
443 return 0;
444 }
445
smcr_conn_save_peer_info(struct smc_sock * smc,struct smc_clc_msg_accept_confirm * clc)446 static void smcr_conn_save_peer_info(struct smc_sock *smc,
447 struct smc_clc_msg_accept_confirm *clc)
448 {
449 int bufsize = smc_uncompress_bufsize(clc->r0.rmbe_size);
450
451 smc->conn.peer_rmbe_idx = clc->r0.rmbe_idx;
452 smc->conn.local_tx_ctrl.token = ntohl(clc->r0.rmbe_alert_token);
453 smc->conn.peer_rmbe_size = bufsize;
454 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
455 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
456 }
457
smc_isascii(char * hostname)458 static bool smc_isascii(char *hostname)
459 {
460 int i;
461
462 for (i = 0; i < SMC_MAX_HOSTNAME_LEN; i++)
463 if (!isascii(hostname[i]))
464 return false;
465 return true;
466 }
467
smcd_conn_save_peer_info(struct smc_sock * smc,struct smc_clc_msg_accept_confirm * clc)468 static void smcd_conn_save_peer_info(struct smc_sock *smc,
469 struct smc_clc_msg_accept_confirm *clc)
470 {
471 int bufsize = smc_uncompress_bufsize(clc->d0.dmbe_size);
472
473 smc->conn.peer_rmbe_idx = clc->d0.dmbe_idx;
474 smc->conn.peer_token = clc->d0.token;
475 /* msg header takes up space in the buffer */
476 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
477 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
478 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
479 if (clc->hdr.version > SMC_V1 &&
480 (clc->hdr.typev2 & SMC_FIRST_CONTACT_MASK)) {
481 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
482 (struct smc_clc_msg_accept_confirm_v2 *)clc;
483 struct smc_clc_first_contact_ext *fce =
484 (struct smc_clc_first_contact_ext *)
485 (((u8 *)clc_v2) + sizeof(*clc_v2));
486
487 memcpy(smc->conn.lgr->negotiated_eid, clc_v2->eid,
488 SMC_MAX_EID_LEN);
489 smc->conn.lgr->peer_os = fce->os_type;
490 smc->conn.lgr->peer_smc_release = fce->release;
491 if (smc_isascii(fce->hostname))
492 memcpy(smc->conn.lgr->peer_hostname, fce->hostname,
493 SMC_MAX_HOSTNAME_LEN);
494 }
495 }
496
smc_conn_save_peer_info(struct smc_sock * smc,struct smc_clc_msg_accept_confirm * clc)497 static void smc_conn_save_peer_info(struct smc_sock *smc,
498 struct smc_clc_msg_accept_confirm *clc)
499 {
500 if (smc->conn.lgr->is_smcd)
501 smcd_conn_save_peer_info(smc, clc);
502 else
503 smcr_conn_save_peer_info(smc, clc);
504 }
505
smc_link_save_peer_info(struct smc_link * link,struct smc_clc_msg_accept_confirm * clc)506 static void smc_link_save_peer_info(struct smc_link *link,
507 struct smc_clc_msg_accept_confirm *clc)
508 {
509 link->peer_qpn = ntoh24(clc->r0.qpn);
510 memcpy(link->peer_gid, clc->r0.lcl.gid, SMC_GID_SIZE);
511 memcpy(link->peer_mac, clc->r0.lcl.mac, sizeof(link->peer_mac));
512 link->peer_psn = ntoh24(clc->r0.psn);
513 link->peer_mtu = clc->r0.qp_mtu;
514 }
515
smc_switch_to_fallback(struct smc_sock * smc)516 static void smc_switch_to_fallback(struct smc_sock *smc)
517 {
518 wait_queue_head_t *smc_wait = sk_sleep(&smc->sk);
519 wait_queue_head_t *clc_wait = sk_sleep(smc->clcsock->sk);
520 unsigned long flags;
521
522 smc->use_fallback = true;
523 if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
524 smc->clcsock->file = smc->sk.sk_socket->file;
525 smc->clcsock->file->private_data = smc->clcsock;
526 smc->clcsock->wq.fasync_list =
527 smc->sk.sk_socket->wq.fasync_list;
528
529 /* There may be some entries remaining in
530 * smc socket->wq, which should be removed
531 * to clcsocket->wq during the fallback.
532 */
533 spin_lock_irqsave(&smc_wait->lock, flags);
534 spin_lock_nested(&clc_wait->lock, SINGLE_DEPTH_NESTING);
535 list_splice_init(&smc_wait->head, &clc_wait->head);
536 spin_unlock(&clc_wait->lock);
537 spin_unlock_irqrestore(&smc_wait->lock, flags);
538 }
539 }
540
541 /* fall back during connect */
smc_connect_fallback(struct smc_sock * smc,int reason_code)542 static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
543 {
544 smc_switch_to_fallback(smc);
545 smc->fallback_rsn = reason_code;
546 smc_copy_sock_settings_to_clc(smc);
547 smc->connect_nonblock = 0;
548 if (smc->sk.sk_state == SMC_INIT)
549 smc->sk.sk_state = SMC_ACTIVE;
550 return 0;
551 }
552
553 /* decline and fall back during connect */
smc_connect_decline_fallback(struct smc_sock * smc,int reason_code,u8 version)554 static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code,
555 u8 version)
556 {
557 int rc;
558
559 if (reason_code < 0) { /* error, fallback is not possible */
560 if (smc->sk.sk_state == SMC_INIT)
561 sock_put(&smc->sk); /* passive closing */
562 return reason_code;
563 }
564 if (reason_code != SMC_CLC_DECL_PEERDECL) {
565 rc = smc_clc_send_decline(smc, reason_code, version);
566 if (rc < 0) {
567 if (smc->sk.sk_state == SMC_INIT)
568 sock_put(&smc->sk); /* passive closing */
569 return rc;
570 }
571 }
572 return smc_connect_fallback(smc, reason_code);
573 }
574
575 /* abort connecting */
smc_connect_abort(struct smc_sock * smc,int local_first)576 static void smc_connect_abort(struct smc_sock *smc, int local_first)
577 {
578 if (local_first)
579 smc_lgr_cleanup_early(&smc->conn);
580 else
581 smc_conn_free(&smc->conn);
582 }
583
584 /* check if there is a rdma device available for this connection. */
585 /* called for connect and listen */
smc_find_rdma_device(struct smc_sock * smc,struct smc_init_info * ini)586 static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
587 {
588 /* PNET table look up: search active ib_device and port
589 * within same PNETID that also contains the ethernet device
590 * used for the internal TCP socket
591 */
592 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
593 if (!ini->ib_dev)
594 return SMC_CLC_DECL_NOSMCRDEV;
595 return 0;
596 }
597
598 /* check if there is an ISM device available for this connection. */
599 /* called for connect and listen */
smc_find_ism_device(struct smc_sock * smc,struct smc_init_info * ini)600 static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
601 {
602 /* Find ISM device with same PNETID as connecting interface */
603 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
604 if (!ini->ism_dev[0])
605 return SMC_CLC_DECL_NOSMCDDEV;
606 else
607 ini->ism_chid[0] = smc_ism_get_chid(ini->ism_dev[0]);
608 return 0;
609 }
610
611 /* is chid unique for the ism devices that are already determined? */
smc_find_ism_v2_is_unique_chid(u16 chid,struct smc_init_info * ini,int cnt)612 static bool smc_find_ism_v2_is_unique_chid(u16 chid, struct smc_init_info *ini,
613 int cnt)
614 {
615 int i = (!ini->ism_dev[0]) ? 1 : 0;
616
617 for (; i < cnt; i++)
618 if (ini->ism_chid[i] == chid)
619 return false;
620 return true;
621 }
622
623 /* determine possible V2 ISM devices (either without PNETID or with PNETID plus
624 * PNETID matching net_device)
625 */
smc_find_ism_v2_device_clnt(struct smc_sock * smc,struct smc_init_info * ini)626 static int smc_find_ism_v2_device_clnt(struct smc_sock *smc,
627 struct smc_init_info *ini)
628 {
629 int rc = SMC_CLC_DECL_NOSMCDDEV;
630 struct smcd_dev *smcd;
631 int i = 1;
632 u16 chid;
633
634 if (smcd_indicated(ini->smc_type_v1))
635 rc = 0; /* already initialized for V1 */
636 mutex_lock(&smcd_dev_list.mutex);
637 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
638 if (smcd->going_away || smcd == ini->ism_dev[0])
639 continue;
640 chid = smc_ism_get_chid(smcd);
641 if (!smc_find_ism_v2_is_unique_chid(chid, ini, i))
642 continue;
643 if (!smc_pnet_is_pnetid_set(smcd->pnetid) ||
644 smc_pnet_is_ndev_pnetid(sock_net(&smc->sk), smcd->pnetid)) {
645 ini->ism_dev[i] = smcd;
646 ini->ism_chid[i] = chid;
647 ini->is_smcd = true;
648 rc = 0;
649 i++;
650 if (i > SMC_MAX_ISM_DEVS)
651 break;
652 }
653 }
654 mutex_unlock(&smcd_dev_list.mutex);
655 ini->ism_offered_cnt = i - 1;
656 if (!ini->ism_dev[0] && !ini->ism_dev[1])
657 ini->smcd_version = 0;
658
659 return rc;
660 }
661
662 /* Check for VLAN ID and register it on ISM device just for CLC handshake */
smc_connect_ism_vlan_setup(struct smc_sock * smc,struct smc_init_info * ini)663 static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
664 struct smc_init_info *ini)
665 {
666 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev[0], ini->vlan_id))
667 return SMC_CLC_DECL_ISMVLANERR;
668 return 0;
669 }
670
smc_find_proposal_devices(struct smc_sock * smc,struct smc_init_info * ini)671 static int smc_find_proposal_devices(struct smc_sock *smc,
672 struct smc_init_info *ini)
673 {
674 int rc = 0;
675
676 /* check if there is an ism device available */
677 if (ini->smcd_version & SMC_V1) {
678 if (smc_find_ism_device(smc, ini) ||
679 smc_connect_ism_vlan_setup(smc, ini)) {
680 if (ini->smc_type_v1 == SMC_TYPE_B)
681 ini->smc_type_v1 = SMC_TYPE_R;
682 else
683 ini->smc_type_v1 = SMC_TYPE_N;
684 } /* else ISM V1 is supported for this connection */
685 if (smc_find_rdma_device(smc, ini)) {
686 if (ini->smc_type_v1 == SMC_TYPE_B)
687 ini->smc_type_v1 = SMC_TYPE_D;
688 else
689 ini->smc_type_v1 = SMC_TYPE_N;
690 } /* else RDMA is supported for this connection */
691 }
692 if (smc_ism_v2_capable && smc_find_ism_v2_device_clnt(smc, ini))
693 ini->smc_type_v2 = SMC_TYPE_N;
694
695 /* if neither ISM nor RDMA are supported, fallback */
696 if (!smcr_indicated(ini->smc_type_v1) &&
697 ini->smc_type_v1 == SMC_TYPE_N && ini->smc_type_v2 == SMC_TYPE_N)
698 rc = SMC_CLC_DECL_NOSMCDEV;
699
700 return rc;
701 }
702
703 /* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
704 * used, the VLAN ID will be registered again during the connection setup.
705 */
smc_connect_ism_vlan_cleanup(struct smc_sock * smc,struct smc_init_info * ini)706 static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc,
707 struct smc_init_info *ini)
708 {
709 if (!smcd_indicated(ini->smc_type_v1))
710 return 0;
711 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev[0], ini->vlan_id))
712 return SMC_CLC_DECL_CNFERR;
713 return 0;
714 }
715
716 #define SMC_CLC_MAX_ACCEPT_LEN \
717 (sizeof(struct smc_clc_msg_accept_confirm_v2) + \
718 sizeof(struct smc_clc_first_contact_ext) + \
719 sizeof(struct smc_clc_msg_trail))
720
721 /* CLC handshake during connect */
smc_connect_clc(struct smc_sock * smc,struct smc_clc_msg_accept_confirm_v2 * aclc2,struct smc_init_info * ini)722 static int smc_connect_clc(struct smc_sock *smc,
723 struct smc_clc_msg_accept_confirm_v2 *aclc2,
724 struct smc_init_info *ini)
725 {
726 int rc = 0;
727
728 /* do inband token exchange */
729 rc = smc_clc_send_proposal(smc, ini);
730 if (rc)
731 return rc;
732 /* receive SMC Accept CLC message */
733 return smc_clc_wait_msg(smc, aclc2, SMC_CLC_MAX_ACCEPT_LEN,
734 SMC_CLC_ACCEPT, CLC_WAIT_TIME);
735 }
736
737 /* setup for RDMA connection of client */
smc_connect_rdma(struct smc_sock * smc,struct smc_clc_msg_accept_confirm * aclc,struct smc_init_info * ini)738 static int smc_connect_rdma(struct smc_sock *smc,
739 struct smc_clc_msg_accept_confirm *aclc,
740 struct smc_init_info *ini)
741 {
742 int i, reason_code = 0;
743 struct smc_link *link;
744
745 ini->is_smcd = false;
746 ini->ib_lcl = &aclc->r0.lcl;
747 ini->ib_clcqpn = ntoh24(aclc->r0.qpn);
748 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
749
750 mutex_lock(&smc_client_lgr_pending);
751 reason_code = smc_conn_create(smc, ini);
752 if (reason_code) {
753 mutex_unlock(&smc_client_lgr_pending);
754 return reason_code;
755 }
756
757 smc_conn_save_peer_info(smc, aclc);
758
759 if (ini->first_contact_local) {
760 link = smc->conn.lnk;
761 } else {
762 /* set link that was assigned by server */
763 link = NULL;
764 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
765 struct smc_link *l = &smc->conn.lgr->lnk[i];
766
767 if (l->peer_qpn == ntoh24(aclc->r0.qpn) &&
768 !memcmp(l->peer_gid, &aclc->r0.lcl.gid,
769 SMC_GID_SIZE) &&
770 !memcmp(l->peer_mac, &aclc->r0.lcl.mac,
771 sizeof(l->peer_mac))) {
772 link = l;
773 break;
774 }
775 }
776 if (!link) {
777 reason_code = SMC_CLC_DECL_NOSRVLINK;
778 goto connect_abort;
779 }
780 smc->conn.lnk = link;
781 }
782
783 /* create send buffer and rmb */
784 if (smc_buf_create(smc, false)) {
785 reason_code = SMC_CLC_DECL_MEM;
786 goto connect_abort;
787 }
788
789 if (ini->first_contact_local)
790 smc_link_save_peer_info(link, aclc);
791
792 if (smc_rmb_rtoken_handling(&smc->conn, link, aclc)) {
793 reason_code = SMC_CLC_DECL_ERR_RTOK;
794 goto connect_abort;
795 }
796
797 smc_close_init(smc);
798 smc_rx_init(smc);
799
800 if (ini->first_contact_local) {
801 if (smc_ib_ready_link(link)) {
802 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
803 goto connect_abort;
804 }
805 } else {
806 if (smcr_lgr_reg_rmbs(link, smc->conn.rmb_desc)) {
807 reason_code = SMC_CLC_DECL_ERR_REGRMB;
808 goto connect_abort;
809 }
810 }
811 smc_rmb_sync_sg_for_device(&smc->conn);
812
813 reason_code = smc_clc_send_confirm(smc, ini->first_contact_local,
814 SMC_V1);
815 if (reason_code)
816 goto connect_abort;
817
818 smc_tx_init(smc);
819
820 if (ini->first_contact_local) {
821 /* QP confirmation over RoCE fabric */
822 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
823 reason_code = smcr_clnt_conf_first_link(smc);
824 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
825 if (reason_code)
826 goto connect_abort;
827 }
828 mutex_unlock(&smc_client_lgr_pending);
829
830 smc_copy_sock_settings_to_clc(smc);
831 smc->connect_nonblock = 0;
832 if (smc->sk.sk_state == SMC_INIT)
833 smc->sk.sk_state = SMC_ACTIVE;
834
835 return 0;
836 connect_abort:
837 smc_connect_abort(smc, ini->first_contact_local);
838 mutex_unlock(&smc_client_lgr_pending);
839 smc->connect_nonblock = 0;
840
841 return reason_code;
842 }
843
844 /* The server has chosen one of the proposed ISM devices for the communication.
845 * Determine from the CHID of the received CLC ACCEPT the ISM device chosen.
846 */
847 static int
smc_v2_determine_accepted_chid(struct smc_clc_msg_accept_confirm_v2 * aclc,struct smc_init_info * ini)848 smc_v2_determine_accepted_chid(struct smc_clc_msg_accept_confirm_v2 *aclc,
849 struct smc_init_info *ini)
850 {
851 int i;
852
853 for (i = 0; i < ini->ism_offered_cnt + 1; i++) {
854 if (ini->ism_chid[i] == ntohs(aclc->chid)) {
855 ini->ism_selected = i;
856 return 0;
857 }
858 }
859
860 return -EPROTO;
861 }
862
863 /* setup for ISM connection of client */
smc_connect_ism(struct smc_sock * smc,struct smc_clc_msg_accept_confirm * aclc,struct smc_init_info * ini)864 static int smc_connect_ism(struct smc_sock *smc,
865 struct smc_clc_msg_accept_confirm *aclc,
866 struct smc_init_info *ini)
867 {
868 int rc = 0;
869
870 ini->is_smcd = true;
871 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
872
873 if (aclc->hdr.version == SMC_V2) {
874 struct smc_clc_msg_accept_confirm_v2 *aclc_v2 =
875 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
876
877 rc = smc_v2_determine_accepted_chid(aclc_v2, ini);
878 if (rc)
879 return rc;
880 }
881 ini->ism_peer_gid[ini->ism_selected] = aclc->d0.gid;
882
883 /* there is only one lgr role for SMC-D; use server lock */
884 mutex_lock(&smc_server_lgr_pending);
885 rc = smc_conn_create(smc, ini);
886 if (rc) {
887 mutex_unlock(&smc_server_lgr_pending);
888 return rc;
889 }
890
891 /* Create send and receive buffers */
892 rc = smc_buf_create(smc, true);
893 if (rc) {
894 rc = (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB : SMC_CLC_DECL_MEM;
895 goto connect_abort;
896 }
897
898 smc_conn_save_peer_info(smc, aclc);
899 smc_close_init(smc);
900 smc_rx_init(smc);
901 smc_tx_init(smc);
902
903 rc = smc_clc_send_confirm(smc, ini->first_contact_local,
904 aclc->hdr.version);
905 if (rc)
906 goto connect_abort;
907 mutex_unlock(&smc_server_lgr_pending);
908
909 smc_copy_sock_settings_to_clc(smc);
910 smc->connect_nonblock = 0;
911 if (smc->sk.sk_state == SMC_INIT)
912 smc->sk.sk_state = SMC_ACTIVE;
913
914 return 0;
915 connect_abort:
916 smc_connect_abort(smc, ini->first_contact_local);
917 mutex_unlock(&smc_server_lgr_pending);
918 smc->connect_nonblock = 0;
919
920 return rc;
921 }
922
923 /* check if received accept type and version matches a proposed one */
smc_connect_check_aclc(struct smc_init_info * ini,struct smc_clc_msg_accept_confirm * aclc)924 static int smc_connect_check_aclc(struct smc_init_info *ini,
925 struct smc_clc_msg_accept_confirm *aclc)
926 {
927 if ((aclc->hdr.typev1 == SMC_TYPE_R &&
928 !smcr_indicated(ini->smc_type_v1)) ||
929 (aclc->hdr.typev1 == SMC_TYPE_D &&
930 ((!smcd_indicated(ini->smc_type_v1) &&
931 !smcd_indicated(ini->smc_type_v2)) ||
932 (aclc->hdr.version == SMC_V1 &&
933 !smcd_indicated(ini->smc_type_v1)) ||
934 (aclc->hdr.version == SMC_V2 &&
935 !smcd_indicated(ini->smc_type_v2)))))
936 return SMC_CLC_DECL_MODEUNSUPP;
937
938 return 0;
939 }
940
941 /* perform steps before actually connecting */
__smc_connect(struct smc_sock * smc)942 static int __smc_connect(struct smc_sock *smc)
943 {
944 u8 version = smc_ism_v2_capable ? SMC_V2 : SMC_V1;
945 struct smc_clc_msg_accept_confirm_v2 *aclc2;
946 struct smc_clc_msg_accept_confirm *aclc;
947 struct smc_init_info *ini = NULL;
948 u8 *buf = NULL;
949 int rc = 0;
950
951 if (smc->use_fallback)
952 return smc_connect_fallback(smc, smc->fallback_rsn);
953
954 /* if peer has not signalled SMC-capability, fall back */
955 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
956 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
957
958 /* IPSec connections opt out of SMC optimizations */
959 if (using_ipsec(smc))
960 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC,
961 version);
962
963 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
964 if (!ini)
965 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_MEM,
966 version);
967
968 ini->smcd_version = SMC_V1;
969 ini->smcd_version |= smc_ism_v2_capable ? SMC_V2 : 0;
970 ini->smc_type_v1 = SMC_TYPE_B;
971 ini->smc_type_v2 = smc_ism_v2_capable ? SMC_TYPE_D : SMC_TYPE_N;
972
973 /* get vlan id from IP device */
974 if (smc_vlan_by_tcpsk(smc->clcsock, ini)) {
975 ini->smcd_version &= ~SMC_V1;
976 ini->smc_type_v1 = SMC_TYPE_N;
977 if (!ini->smcd_version) {
978 rc = SMC_CLC_DECL_GETVLANERR;
979 goto fallback;
980 }
981 }
982
983 rc = smc_find_proposal_devices(smc, ini);
984 if (rc)
985 goto fallback;
986
987 buf = kzalloc(SMC_CLC_MAX_ACCEPT_LEN, GFP_KERNEL);
988 if (!buf) {
989 rc = SMC_CLC_DECL_MEM;
990 goto fallback;
991 }
992 aclc2 = (struct smc_clc_msg_accept_confirm_v2 *)buf;
993 aclc = (struct smc_clc_msg_accept_confirm *)aclc2;
994
995 /* perform CLC handshake */
996 rc = smc_connect_clc(smc, aclc2, ini);
997 if (rc)
998 goto vlan_cleanup;
999
1000 /* check if smc modes and versions of CLC proposal and accept match */
1001 rc = smc_connect_check_aclc(ini, aclc);
1002 version = aclc->hdr.version == SMC_V1 ? SMC_V1 : SMC_V2;
1003 ini->smcd_version = version;
1004 if (rc)
1005 goto vlan_cleanup;
1006
1007 /* depending on previous steps, connect using rdma or ism */
1008 if (aclc->hdr.typev1 == SMC_TYPE_R)
1009 rc = smc_connect_rdma(smc, aclc, ini);
1010 else if (aclc->hdr.typev1 == SMC_TYPE_D)
1011 rc = smc_connect_ism(smc, aclc, ini);
1012 if (rc)
1013 goto vlan_cleanup;
1014
1015 smc_connect_ism_vlan_cleanup(smc, ini);
1016 kfree(buf);
1017 kfree(ini);
1018 return 0;
1019
1020 vlan_cleanup:
1021 smc_connect_ism_vlan_cleanup(smc, ini);
1022 kfree(buf);
1023 fallback:
1024 kfree(ini);
1025 return smc_connect_decline_fallback(smc, rc, version);
1026 }
1027
smc_connect_work(struct work_struct * work)1028 static void smc_connect_work(struct work_struct *work)
1029 {
1030 struct smc_sock *smc = container_of(work, struct smc_sock,
1031 connect_work);
1032 long timeo = smc->sk.sk_sndtimeo;
1033 int rc = 0;
1034
1035 if (!timeo)
1036 timeo = MAX_SCHEDULE_TIMEOUT;
1037 lock_sock(smc->clcsock->sk);
1038 if (smc->clcsock->sk->sk_err) {
1039 smc->sk.sk_err = smc->clcsock->sk->sk_err;
1040 } else if ((1 << smc->clcsock->sk->sk_state) &
1041 (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
1042 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
1043 if ((rc == -EPIPE) &&
1044 ((1 << smc->clcsock->sk->sk_state) &
1045 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
1046 rc = 0;
1047 }
1048 release_sock(smc->clcsock->sk);
1049 lock_sock(&smc->sk);
1050 if (rc != 0 || smc->sk.sk_err) {
1051 smc->sk.sk_state = SMC_CLOSED;
1052 if (rc == -EPIPE || rc == -EAGAIN)
1053 smc->sk.sk_err = EPIPE;
1054 else if (signal_pending(current))
1055 smc->sk.sk_err = -sock_intr_errno(timeo);
1056 sock_put(&smc->sk); /* passive closing */
1057 goto out;
1058 }
1059
1060 rc = __smc_connect(smc);
1061 if (rc < 0)
1062 smc->sk.sk_err = -rc;
1063
1064 out:
1065 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
1066 if (smc->sk.sk_err) {
1067 smc->sk.sk_state_change(&smc->sk);
1068 } else { /* allow polling before and after fallback decision */
1069 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
1070 smc->sk.sk_write_space(&smc->sk);
1071 }
1072 }
1073 release_sock(&smc->sk);
1074 }
1075
smc_connect(struct socket * sock,struct sockaddr * addr,int alen,int flags)1076 static int smc_connect(struct socket *sock, struct sockaddr *addr,
1077 int alen, int flags)
1078 {
1079 struct sock *sk = sock->sk;
1080 struct smc_sock *smc;
1081 int rc = -EINVAL;
1082
1083 smc = smc_sk(sk);
1084
1085 /* separate smc parameter checking to be safe */
1086 if (alen < sizeof(addr->sa_family))
1087 goto out_err;
1088 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
1089 goto out_err;
1090
1091 lock_sock(sk);
1092 switch (sk->sk_state) {
1093 default:
1094 goto out;
1095 case SMC_ACTIVE:
1096 rc = -EISCONN;
1097 goto out;
1098 case SMC_INIT:
1099 rc = 0;
1100 break;
1101 }
1102
1103 smc_copy_sock_settings_to_clc(smc);
1104 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
1105 if (smc->connect_nonblock) {
1106 rc = -EALREADY;
1107 goto out;
1108 }
1109 rc = kernel_connect(smc->clcsock, addr, alen, flags);
1110 if (rc && rc != -EINPROGRESS)
1111 goto out;
1112
1113 sock_hold(&smc->sk); /* sock put in passive closing */
1114 if (smc->use_fallback)
1115 goto out;
1116 if (flags & O_NONBLOCK) {
1117 if (queue_work(smc_hs_wq, &smc->connect_work))
1118 smc->connect_nonblock = 1;
1119 rc = -EINPROGRESS;
1120 } else {
1121 rc = __smc_connect(smc);
1122 if (rc < 0)
1123 goto out;
1124 else
1125 rc = 0; /* success cases including fallback */
1126 }
1127
1128 out:
1129 release_sock(sk);
1130 out_err:
1131 return rc;
1132 }
1133
smc_clcsock_accept(struct smc_sock * lsmc,struct smc_sock ** new_smc)1134 static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
1135 {
1136 struct socket *new_clcsock = NULL;
1137 struct sock *lsk = &lsmc->sk;
1138 struct sock *new_sk;
1139 int rc = -EINVAL;
1140
1141 release_sock(lsk);
1142 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
1143 if (!new_sk) {
1144 rc = -ENOMEM;
1145 lsk->sk_err = ENOMEM;
1146 *new_smc = NULL;
1147 lock_sock(lsk);
1148 goto out;
1149 }
1150 *new_smc = smc_sk(new_sk);
1151
1152 mutex_lock(&lsmc->clcsock_release_lock);
1153 if (lsmc->clcsock)
1154 rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK);
1155 mutex_unlock(&lsmc->clcsock_release_lock);
1156 lock_sock(lsk);
1157 if (rc < 0 && rc != -EAGAIN)
1158 lsk->sk_err = -rc;
1159 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
1160 new_sk->sk_prot->unhash(new_sk);
1161 if (new_clcsock)
1162 sock_release(new_clcsock);
1163 new_sk->sk_state = SMC_CLOSED;
1164 sock_set_flag(new_sk, SOCK_DEAD);
1165 sock_put(new_sk); /* final */
1166 *new_smc = NULL;
1167 goto out;
1168 }
1169
1170 /* new clcsock has inherited the smc listen-specific sk_data_ready
1171 * function; switch it back to the original sk_data_ready function
1172 */
1173 new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready;
1174 (*new_smc)->clcsock = new_clcsock;
1175 out:
1176 return rc;
1177 }
1178
1179 /* add a just created sock to the accept queue of the listen sock as
1180 * candidate for a following socket accept call from user space
1181 */
smc_accept_enqueue(struct sock * parent,struct sock * sk)1182 static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
1183 {
1184 struct smc_sock *par = smc_sk(parent);
1185
1186 sock_hold(sk); /* sock_put in smc_accept_unlink () */
1187 spin_lock(&par->accept_q_lock);
1188 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
1189 spin_unlock(&par->accept_q_lock);
1190 sk_acceptq_added(parent);
1191 }
1192
1193 /* remove a socket from the accept queue of its parental listening socket */
smc_accept_unlink(struct sock * sk)1194 static void smc_accept_unlink(struct sock *sk)
1195 {
1196 struct smc_sock *par = smc_sk(sk)->listen_smc;
1197
1198 spin_lock(&par->accept_q_lock);
1199 list_del_init(&smc_sk(sk)->accept_q);
1200 spin_unlock(&par->accept_q_lock);
1201 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
1202 sock_put(sk); /* sock_hold in smc_accept_enqueue */
1203 }
1204
1205 /* remove a sock from the accept queue to bind it to a new socket created
1206 * for a socket accept call from user space
1207 */
smc_accept_dequeue(struct sock * parent,struct socket * new_sock)1208 struct sock *smc_accept_dequeue(struct sock *parent,
1209 struct socket *new_sock)
1210 {
1211 struct smc_sock *isk, *n;
1212 struct sock *new_sk;
1213
1214 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
1215 new_sk = (struct sock *)isk;
1216
1217 smc_accept_unlink(new_sk);
1218 if (new_sk->sk_state == SMC_CLOSED) {
1219 new_sk->sk_prot->unhash(new_sk);
1220 if (isk->clcsock) {
1221 sock_release(isk->clcsock);
1222 isk->clcsock = NULL;
1223 }
1224 sock_put(new_sk); /* final */
1225 continue;
1226 }
1227 if (new_sock) {
1228 sock_graft(new_sk, new_sock);
1229 if (isk->use_fallback) {
1230 smc_sk(new_sk)->clcsock->file = new_sock->file;
1231 isk->clcsock->file->private_data = isk->clcsock;
1232 }
1233 }
1234 return new_sk;
1235 }
1236 return NULL;
1237 }
1238
1239 /* clean up for a created but never accepted sock */
smc_close_non_accepted(struct sock * sk)1240 void smc_close_non_accepted(struct sock *sk)
1241 {
1242 struct smc_sock *smc = smc_sk(sk);
1243
1244 sock_hold(sk); /* sock_put below */
1245 lock_sock(sk);
1246 if (!sk->sk_lingertime)
1247 /* wait for peer closing */
1248 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
1249 __smc_release(smc);
1250 release_sock(sk);
1251 sock_put(sk); /* sock_hold above */
1252 sock_put(sk); /* final sock_put */
1253 }
1254
smcr_serv_conf_first_link(struct smc_sock * smc)1255 static int smcr_serv_conf_first_link(struct smc_sock *smc)
1256 {
1257 struct smc_link *link = smc->conn.lnk;
1258 struct smc_llc_qentry *qentry;
1259 int rc;
1260
1261 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
1262 return SMC_CLC_DECL_ERR_REGRMB;
1263
1264 /* send CONFIRM LINK request to client over the RoCE fabric */
1265 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
1266 if (rc < 0)
1267 return SMC_CLC_DECL_TIMEOUT_CL;
1268
1269 /* receive CONFIRM LINK response from client over the RoCE fabric */
1270 qentry = smc_llc_wait(link->lgr, link, SMC_LLC_WAIT_TIME,
1271 SMC_LLC_CONFIRM_LINK);
1272 if (!qentry) {
1273 struct smc_clc_msg_decline dclc;
1274
1275 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
1276 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
1277 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
1278 }
1279 smc_llc_save_peer_uid(qentry);
1280 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_RESP);
1281 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
1282 if (rc)
1283 return SMC_CLC_DECL_RMBE_EC;
1284
1285 /* confirm_rkey is implicit on 1st contact */
1286 smc->conn.rmb_desc->is_conf_rkey = true;
1287
1288 smc_llc_link_active(link);
1289 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
1290
1291 /* initial contact - try to establish second link */
1292 smc_llc_srv_add_link(link);
1293 return 0;
1294 }
1295
1296 /* listen worker: finish */
smc_listen_out(struct smc_sock * new_smc)1297 static void smc_listen_out(struct smc_sock *new_smc)
1298 {
1299 struct smc_sock *lsmc = new_smc->listen_smc;
1300 struct sock *newsmcsk = &new_smc->sk;
1301
1302 if (lsmc->sk.sk_state == SMC_LISTEN) {
1303 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
1304 smc_accept_enqueue(&lsmc->sk, newsmcsk);
1305 release_sock(&lsmc->sk);
1306 } else { /* no longer listening */
1307 smc_close_non_accepted(newsmcsk);
1308 }
1309
1310 /* Wake up accept */
1311 lsmc->sk.sk_data_ready(&lsmc->sk);
1312 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
1313 }
1314
1315 /* listen worker: finish in state connected */
smc_listen_out_connected(struct smc_sock * new_smc)1316 static void smc_listen_out_connected(struct smc_sock *new_smc)
1317 {
1318 struct sock *newsmcsk = &new_smc->sk;
1319
1320 sk_refcnt_debug_inc(newsmcsk);
1321 if (newsmcsk->sk_state == SMC_INIT)
1322 newsmcsk->sk_state = SMC_ACTIVE;
1323
1324 smc_listen_out(new_smc);
1325 }
1326
1327 /* listen worker: finish in error state */
smc_listen_out_err(struct smc_sock * new_smc)1328 static void smc_listen_out_err(struct smc_sock *new_smc)
1329 {
1330 struct sock *newsmcsk = &new_smc->sk;
1331
1332 if (newsmcsk->sk_state == SMC_INIT)
1333 sock_put(&new_smc->sk); /* passive closing */
1334 newsmcsk->sk_state = SMC_CLOSED;
1335
1336 smc_listen_out(new_smc);
1337 }
1338
1339 /* listen worker: decline and fall back if possible */
smc_listen_decline(struct smc_sock * new_smc,int reason_code,int local_first,u8 version)1340 static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1341 int local_first, u8 version)
1342 {
1343 /* RDMA setup failed, switch back to TCP */
1344 if (local_first)
1345 smc_lgr_cleanup_early(&new_smc->conn);
1346 else
1347 smc_conn_free(&new_smc->conn);
1348 if (reason_code < 0) { /* error, no fallback possible */
1349 smc_listen_out_err(new_smc);
1350 return;
1351 }
1352 smc_switch_to_fallback(new_smc);
1353 new_smc->fallback_rsn = reason_code;
1354 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
1355 if (smc_clc_send_decline(new_smc, reason_code, version) < 0) {
1356 smc_listen_out_err(new_smc);
1357 return;
1358 }
1359 }
1360 smc_listen_out_connected(new_smc);
1361 }
1362
1363 /* listen worker: version checking */
smc_listen_v2_check(struct smc_sock * new_smc,struct smc_clc_msg_proposal * pclc,struct smc_init_info * ini)1364 static int smc_listen_v2_check(struct smc_sock *new_smc,
1365 struct smc_clc_msg_proposal *pclc,
1366 struct smc_init_info *ini)
1367 {
1368 struct smc_clc_smcd_v2_extension *pclc_smcd_v2_ext;
1369 struct smc_clc_v2_extension *pclc_v2_ext;
1370
1371 ini->smc_type_v1 = pclc->hdr.typev1;
1372 ini->smc_type_v2 = pclc->hdr.typev2;
1373 ini->smcd_version = ini->smc_type_v1 != SMC_TYPE_N ? SMC_V1 : 0;
1374 if (pclc->hdr.version > SMC_V1)
1375 ini->smcd_version |=
1376 ini->smc_type_v2 != SMC_TYPE_N ? SMC_V2 : 0;
1377 if (!smc_ism_v2_capable) {
1378 ini->smcd_version &= ~SMC_V2;
1379 goto out;
1380 }
1381 pclc_v2_ext = smc_get_clc_v2_ext(pclc);
1382 if (!pclc_v2_ext) {
1383 ini->smcd_version &= ~SMC_V2;
1384 goto out;
1385 }
1386 pclc_smcd_v2_ext = smc_get_clc_smcd_v2_ext(pclc_v2_ext);
1387 if (!pclc_smcd_v2_ext)
1388 ini->smcd_version &= ~SMC_V2;
1389
1390 out:
1391 if (!ini->smcd_version) {
1392 if (pclc->hdr.typev1 == SMC_TYPE_B ||
1393 pclc->hdr.typev2 == SMC_TYPE_B)
1394 return SMC_CLC_DECL_NOSMCDEV;
1395 if (pclc->hdr.typev1 == SMC_TYPE_D ||
1396 pclc->hdr.typev2 == SMC_TYPE_D)
1397 return SMC_CLC_DECL_NOSMCDDEV;
1398 return SMC_CLC_DECL_NOSMCRDEV;
1399 }
1400
1401 return 0;
1402 }
1403
1404 /* listen worker: check prefixes */
smc_listen_prfx_check(struct smc_sock * new_smc,struct smc_clc_msg_proposal * pclc)1405 static int smc_listen_prfx_check(struct smc_sock *new_smc,
1406 struct smc_clc_msg_proposal *pclc)
1407 {
1408 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1409 struct socket *newclcsock = new_smc->clcsock;
1410
1411 if (pclc->hdr.typev1 == SMC_TYPE_N)
1412 return 0;
1413 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1414 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
1415 return SMC_CLC_DECL_DIFFPREFIX;
1416
1417 return 0;
1418 }
1419
1420 /* listen worker: initialize connection and buffers */
smc_listen_rdma_init(struct smc_sock * new_smc,struct smc_init_info * ini)1421 static int smc_listen_rdma_init(struct smc_sock *new_smc,
1422 struct smc_init_info *ini)
1423 {
1424 int rc;
1425
1426 /* allocate connection / link group */
1427 rc = smc_conn_create(new_smc, ini);
1428 if (rc)
1429 return rc;
1430
1431 /* create send buffer and rmb */
1432 if (smc_buf_create(new_smc, false))
1433 return SMC_CLC_DECL_MEM;
1434
1435 return 0;
1436 }
1437
1438 /* listen worker: initialize connection and buffers for SMC-D */
smc_listen_ism_init(struct smc_sock * new_smc,struct smc_init_info * ini)1439 static int smc_listen_ism_init(struct smc_sock *new_smc,
1440 struct smc_init_info *ini)
1441 {
1442 int rc;
1443
1444 rc = smc_conn_create(new_smc, ini);
1445 if (rc)
1446 return rc;
1447
1448 /* Create send and receive buffers */
1449 rc = smc_buf_create(new_smc, true);
1450 if (rc) {
1451 if (ini->first_contact_local)
1452 smc_lgr_cleanup_early(&new_smc->conn);
1453 else
1454 smc_conn_free(&new_smc->conn);
1455 return (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB :
1456 SMC_CLC_DECL_MEM;
1457 }
1458
1459 return 0;
1460 }
1461
smc_is_already_selected(struct smcd_dev * smcd,struct smc_init_info * ini,int matches)1462 static bool smc_is_already_selected(struct smcd_dev *smcd,
1463 struct smc_init_info *ini,
1464 int matches)
1465 {
1466 int i;
1467
1468 for (i = 0; i < matches; i++)
1469 if (smcd == ini->ism_dev[i])
1470 return true;
1471
1472 return false;
1473 }
1474
1475 /* check for ISM devices matching proposed ISM devices */
smc_check_ism_v2_match(struct smc_init_info * ini,u16 proposed_chid,u64 proposed_gid,unsigned int * matches)1476 static void smc_check_ism_v2_match(struct smc_init_info *ini,
1477 u16 proposed_chid, u64 proposed_gid,
1478 unsigned int *matches)
1479 {
1480 struct smcd_dev *smcd;
1481
1482 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
1483 if (smcd->going_away)
1484 continue;
1485 if (smc_is_already_selected(smcd, ini, *matches))
1486 continue;
1487 if (smc_ism_get_chid(smcd) == proposed_chid &&
1488 !smc_ism_cantalk(proposed_gid, ISM_RESERVED_VLANID, smcd)) {
1489 ini->ism_peer_gid[*matches] = proposed_gid;
1490 ini->ism_dev[*matches] = smcd;
1491 (*matches)++;
1492 break;
1493 }
1494 }
1495 }
1496
smc_find_ism_v2_device_serv(struct smc_sock * new_smc,struct smc_clc_msg_proposal * pclc,struct smc_init_info * ini)1497 static void smc_find_ism_v2_device_serv(struct smc_sock *new_smc,
1498 struct smc_clc_msg_proposal *pclc,
1499 struct smc_init_info *ini)
1500 {
1501 struct smc_clc_smcd_v2_extension *smcd_v2_ext;
1502 struct smc_clc_v2_extension *smc_v2_ext;
1503 struct smc_clc_msg_smcd *pclc_smcd;
1504 unsigned int matches = 0;
1505 u8 smcd_version;
1506 u8 *eid = NULL;
1507 int i;
1508
1509 if (!(ini->smcd_version & SMC_V2) || !smcd_indicated(ini->smc_type_v2))
1510 goto not_found;
1511
1512 pclc_smcd = smc_get_clc_msg_smcd(pclc);
1513 smc_v2_ext = smc_get_clc_v2_ext(pclc);
1514 smcd_v2_ext = smc_get_clc_smcd_v2_ext(smc_v2_ext);
1515 if (!smcd_v2_ext ||
1516 !smc_v2_ext->hdr.flag.seid) /* no system EID support for SMCD */
1517 goto not_found;
1518
1519 mutex_lock(&smcd_dev_list.mutex);
1520 if (pclc_smcd->ism.chid)
1521 /* check for ISM device matching proposed native ISM device */
1522 smc_check_ism_v2_match(ini, ntohs(pclc_smcd->ism.chid),
1523 ntohll(pclc_smcd->ism.gid), &matches);
1524 for (i = 1; i <= smc_v2_ext->hdr.ism_gid_cnt; i++) {
1525 /* check for ISM devices matching proposed non-native ISM
1526 * devices
1527 */
1528 smc_check_ism_v2_match(ini,
1529 ntohs(smcd_v2_ext->gidchid[i - 1].chid),
1530 ntohll(smcd_v2_ext->gidchid[i - 1].gid),
1531 &matches);
1532 }
1533 mutex_unlock(&smcd_dev_list.mutex);
1534
1535 if (ini->ism_dev[0]) {
1536 smc_ism_get_system_eid(ini->ism_dev[0], &eid);
1537 if (memcmp(eid, smcd_v2_ext->system_eid, SMC_MAX_EID_LEN))
1538 goto not_found;
1539 } else {
1540 goto not_found;
1541 }
1542
1543 /* separate - outside the smcd_dev_list.lock */
1544 smcd_version = ini->smcd_version;
1545 for (i = 0; i < matches; i++) {
1546 ini->smcd_version = SMC_V2;
1547 ini->is_smcd = true;
1548 ini->ism_selected = i;
1549 if (smc_listen_ism_init(new_smc, ini))
1550 /* try next active ISM device */
1551 continue;
1552 return; /* matching and usable V2 ISM device found */
1553 }
1554 /* no V2 ISM device could be initialized */
1555 ini->smcd_version = smcd_version; /* restore original value */
1556
1557 not_found:
1558 ini->smcd_version &= ~SMC_V2;
1559 ini->ism_dev[0] = NULL;
1560 ini->is_smcd = false;
1561 }
1562
smc_find_ism_v1_device_serv(struct smc_sock * new_smc,struct smc_clc_msg_proposal * pclc,struct smc_init_info * ini)1563 static void smc_find_ism_v1_device_serv(struct smc_sock *new_smc,
1564 struct smc_clc_msg_proposal *pclc,
1565 struct smc_init_info *ini)
1566 {
1567 struct smc_clc_msg_smcd *pclc_smcd = smc_get_clc_msg_smcd(pclc);
1568
1569 /* check if ISM V1 is available */
1570 if (!(ini->smcd_version & SMC_V1) || !smcd_indicated(ini->smc_type_v1))
1571 goto not_found;
1572 ini->is_smcd = true; /* prepare ISM check */
1573 ini->ism_peer_gid[0] = ntohll(pclc_smcd->ism.gid);
1574 if (smc_find_ism_device(new_smc, ini))
1575 goto not_found;
1576 ini->ism_selected = 0;
1577 if (!smc_listen_ism_init(new_smc, ini))
1578 return; /* V1 ISM device found */
1579
1580 not_found:
1581 ini->ism_dev[0] = NULL;
1582 ini->is_smcd = false;
1583 }
1584
1585 /* listen worker: register buffers */
smc_listen_rdma_reg(struct smc_sock * new_smc,bool local_first)1586 static int smc_listen_rdma_reg(struct smc_sock *new_smc, bool local_first)
1587 {
1588 struct smc_connection *conn = &new_smc->conn;
1589
1590 if (!local_first) {
1591 if (smcr_lgr_reg_rmbs(conn->lnk, conn->rmb_desc))
1592 return SMC_CLC_DECL_ERR_REGRMB;
1593 }
1594 smc_rmb_sync_sg_for_device(&new_smc->conn);
1595
1596 return 0;
1597 }
1598
smc_find_rdma_v1_device_serv(struct smc_sock * new_smc,struct smc_clc_msg_proposal * pclc,struct smc_init_info * ini)1599 static int smc_find_rdma_v1_device_serv(struct smc_sock *new_smc,
1600 struct smc_clc_msg_proposal *pclc,
1601 struct smc_init_info *ini)
1602 {
1603 int rc;
1604
1605 if (!smcr_indicated(ini->smc_type_v1))
1606 return SMC_CLC_DECL_NOSMCDEV;
1607
1608 /* prepare RDMA check */
1609 ini->ib_lcl = &pclc->lcl;
1610 rc = smc_find_rdma_device(new_smc, ini);
1611 if (rc) {
1612 /* no RDMA device found */
1613 if (ini->smc_type_v1 == SMC_TYPE_B)
1614 /* neither ISM nor RDMA device found */
1615 rc = SMC_CLC_DECL_NOSMCDEV;
1616 return rc;
1617 }
1618 rc = smc_listen_rdma_init(new_smc, ini);
1619 if (rc)
1620 return rc;
1621 return smc_listen_rdma_reg(new_smc, ini->first_contact_local);
1622 }
1623
1624 /* determine the local device matching to proposal */
smc_listen_find_device(struct smc_sock * new_smc,struct smc_clc_msg_proposal * pclc,struct smc_init_info * ini)1625 static int smc_listen_find_device(struct smc_sock *new_smc,
1626 struct smc_clc_msg_proposal *pclc,
1627 struct smc_init_info *ini)
1628 {
1629 int rc;
1630
1631 /* check for ISM device matching V2 proposed device */
1632 smc_find_ism_v2_device_serv(new_smc, pclc, ini);
1633 if (ini->ism_dev[0])
1634 return 0;
1635
1636 if (!(ini->smcd_version & SMC_V1))
1637 return SMC_CLC_DECL_NOSMCDEV;
1638
1639 /* check for matching IP prefix and subnet length */
1640 rc = smc_listen_prfx_check(new_smc, pclc);
1641 if (rc)
1642 return rc;
1643
1644 /* get vlan id from IP device */
1645 if (smc_vlan_by_tcpsk(new_smc->clcsock, ini))
1646 return SMC_CLC_DECL_GETVLANERR;
1647
1648 /* check for ISM device matching V1 proposed device */
1649 smc_find_ism_v1_device_serv(new_smc, pclc, ini);
1650 if (ini->ism_dev[0])
1651 return 0;
1652
1653 if (pclc->hdr.typev1 == SMC_TYPE_D)
1654 return SMC_CLC_DECL_NOSMCDDEV; /* skip RDMA and decline */
1655
1656 /* check if RDMA is available */
1657 return smc_find_rdma_v1_device_serv(new_smc, pclc, ini);
1658 }
1659
1660 /* listen worker: finish RDMA setup */
smc_listen_rdma_finish(struct smc_sock * new_smc,struct smc_clc_msg_accept_confirm * cclc,bool local_first)1661 static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1662 struct smc_clc_msg_accept_confirm *cclc,
1663 bool local_first)
1664 {
1665 struct smc_link *link = new_smc->conn.lnk;
1666 int reason_code = 0;
1667
1668 if (local_first)
1669 smc_link_save_peer_info(link, cclc);
1670
1671 if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc))
1672 return SMC_CLC_DECL_ERR_RTOK;
1673
1674 if (local_first) {
1675 if (smc_ib_ready_link(link))
1676 return SMC_CLC_DECL_ERR_RDYLNK;
1677 /* QP confirmation over RoCE fabric */
1678 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
1679 reason_code = smcr_serv_conf_first_link(new_smc);
1680 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
1681 }
1682 return reason_code;
1683 }
1684
1685 /* setup for connection of server */
smc_listen_work(struct work_struct * work)1686 static void smc_listen_work(struct work_struct *work)
1687 {
1688 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1689 smc_listen_work);
1690 u8 version = smc_ism_v2_capable ? SMC_V2 : SMC_V1;
1691 struct socket *newclcsock = new_smc->clcsock;
1692 struct smc_clc_msg_accept_confirm *cclc;
1693 struct smc_clc_msg_proposal_area *buf;
1694 struct smc_clc_msg_proposal *pclc;
1695 struct smc_init_info *ini = NULL;
1696 int rc = 0;
1697
1698 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1699 return smc_listen_out_err(new_smc);
1700
1701 if (new_smc->use_fallback) {
1702 smc_listen_out_connected(new_smc);
1703 return;
1704 }
1705
1706 /* check if peer is smc capable */
1707 if (!tcp_sk(newclcsock->sk)->syn_smc) {
1708 smc_switch_to_fallback(new_smc);
1709 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
1710 smc_listen_out_connected(new_smc);
1711 return;
1712 }
1713
1714 /* do inband token exchange -
1715 * wait for and receive SMC Proposal CLC message
1716 */
1717 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
1718 if (!buf) {
1719 rc = SMC_CLC_DECL_MEM;
1720 goto out_decl;
1721 }
1722 pclc = (struct smc_clc_msg_proposal *)buf;
1723 rc = smc_clc_wait_msg(new_smc, pclc, sizeof(*buf),
1724 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
1725 if (rc)
1726 goto out_decl;
1727 version = pclc->hdr.version == SMC_V1 ? SMC_V1 : version;
1728
1729 /* IPSec connections opt out of SMC optimizations */
1730 if (using_ipsec(new_smc)) {
1731 rc = SMC_CLC_DECL_IPSEC;
1732 goto out_decl;
1733 }
1734
1735 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
1736 if (!ini) {
1737 rc = SMC_CLC_DECL_MEM;
1738 goto out_decl;
1739 }
1740
1741 /* initial version checking */
1742 rc = smc_listen_v2_check(new_smc, pclc, ini);
1743 if (rc)
1744 goto out_decl;
1745
1746 mutex_lock(&smc_server_lgr_pending);
1747 smc_close_init(new_smc);
1748 smc_rx_init(new_smc);
1749 smc_tx_init(new_smc);
1750
1751 /* determine ISM or RoCE device used for connection */
1752 rc = smc_listen_find_device(new_smc, pclc, ini);
1753 if (rc)
1754 goto out_unlock;
1755
1756 /* send SMC Accept CLC message */
1757 rc = smc_clc_send_accept(new_smc, ini->first_contact_local,
1758 ini->smcd_version == SMC_V2 ? SMC_V2 : SMC_V1);
1759 if (rc)
1760 goto out_unlock;
1761
1762 /* SMC-D does not need this lock any more */
1763 if (ini->is_smcd)
1764 mutex_unlock(&smc_server_lgr_pending);
1765
1766 /* receive SMC Confirm CLC message */
1767 memset(buf, 0, sizeof(*buf));
1768 cclc = (struct smc_clc_msg_accept_confirm *)buf;
1769 rc = smc_clc_wait_msg(new_smc, cclc, sizeof(*buf),
1770 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
1771 if (rc) {
1772 if (!ini->is_smcd)
1773 goto out_unlock;
1774 goto out_decl;
1775 }
1776
1777 /* finish worker */
1778 if (!ini->is_smcd) {
1779 rc = smc_listen_rdma_finish(new_smc, cclc,
1780 ini->first_contact_local);
1781 if (rc)
1782 goto out_unlock;
1783 mutex_unlock(&smc_server_lgr_pending);
1784 }
1785 smc_conn_save_peer_info(new_smc, cclc);
1786 smc_listen_out_connected(new_smc);
1787 goto out_free;
1788
1789 out_unlock:
1790 mutex_unlock(&smc_server_lgr_pending);
1791 out_decl:
1792 smc_listen_decline(new_smc, rc, ini ? ini->first_contact_local : 0,
1793 version);
1794 out_free:
1795 kfree(ini);
1796 kfree(buf);
1797 }
1798
smc_tcp_listen_work(struct work_struct * work)1799 static void smc_tcp_listen_work(struct work_struct *work)
1800 {
1801 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1802 tcp_listen_work);
1803 struct sock *lsk = &lsmc->sk;
1804 struct smc_sock *new_smc;
1805 int rc = 0;
1806
1807 lock_sock(lsk);
1808 while (lsk->sk_state == SMC_LISTEN) {
1809 rc = smc_clcsock_accept(lsmc, &new_smc);
1810 if (rc) /* clcsock accept queue empty or error */
1811 goto out;
1812 if (!new_smc)
1813 continue;
1814
1815 new_smc->listen_smc = lsmc;
1816 new_smc->use_fallback = lsmc->use_fallback;
1817 new_smc->fallback_rsn = lsmc->fallback_rsn;
1818 sock_hold(lsk); /* sock_put in smc_listen_work */
1819 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1820 smc_copy_sock_settings_to_smc(new_smc);
1821 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1822 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
1823 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1824 if (!queue_work(smc_hs_wq, &new_smc->smc_listen_work))
1825 sock_put(&new_smc->sk);
1826 }
1827
1828 out:
1829 release_sock(lsk);
1830 sock_put(&lsmc->sk); /* sock_hold in smc_clcsock_data_ready() */
1831 }
1832
smc_clcsock_data_ready(struct sock * listen_clcsock)1833 static void smc_clcsock_data_ready(struct sock *listen_clcsock)
1834 {
1835 struct smc_sock *lsmc;
1836
1837 lsmc = (struct smc_sock *)
1838 ((uintptr_t)listen_clcsock->sk_user_data & ~SK_USER_DATA_NOCOPY);
1839 if (!lsmc)
1840 return;
1841 lsmc->clcsk_data_ready(listen_clcsock);
1842 if (lsmc->sk.sk_state == SMC_LISTEN) {
1843 sock_hold(&lsmc->sk); /* sock_put in smc_tcp_listen_work() */
1844 if (!queue_work(smc_hs_wq, &lsmc->tcp_listen_work))
1845 sock_put(&lsmc->sk);
1846 }
1847 }
1848
smc_listen(struct socket * sock,int backlog)1849 static int smc_listen(struct socket *sock, int backlog)
1850 {
1851 struct sock *sk = sock->sk;
1852 struct smc_sock *smc;
1853 int rc;
1854
1855 smc = smc_sk(sk);
1856 lock_sock(sk);
1857
1858 rc = -EINVAL;
1859 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
1860 smc->connect_nonblock)
1861 goto out;
1862
1863 rc = 0;
1864 if (sk->sk_state == SMC_LISTEN) {
1865 sk->sk_max_ack_backlog = backlog;
1866 goto out;
1867 }
1868 /* some socket options are handled in core, so we could not apply
1869 * them to the clc socket -- copy smc socket options to clc socket
1870 */
1871 smc_copy_sock_settings_to_clc(smc);
1872 if (!smc->use_fallback)
1873 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
1874
1875 /* save original sk_data_ready function and establish
1876 * smc-specific sk_data_ready function
1877 */
1878 smc->clcsk_data_ready = smc->clcsock->sk->sk_data_ready;
1879 smc->clcsock->sk->sk_data_ready = smc_clcsock_data_ready;
1880 smc->clcsock->sk->sk_user_data =
1881 (void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY);
1882 rc = kernel_listen(smc->clcsock, backlog);
1883 if (rc) {
1884 smc->clcsock->sk->sk_data_ready = smc->clcsk_data_ready;
1885 goto out;
1886 }
1887 sk->sk_max_ack_backlog = backlog;
1888 sk->sk_ack_backlog = 0;
1889 sk->sk_state = SMC_LISTEN;
1890
1891 out:
1892 release_sock(sk);
1893 return rc;
1894 }
1895
smc_accept(struct socket * sock,struct socket * new_sock,int flags,bool kern)1896 static int smc_accept(struct socket *sock, struct socket *new_sock,
1897 int flags, bool kern)
1898 {
1899 struct sock *sk = sock->sk, *nsk;
1900 DECLARE_WAITQUEUE(wait, current);
1901 struct smc_sock *lsmc;
1902 long timeo;
1903 int rc = 0;
1904
1905 lsmc = smc_sk(sk);
1906 sock_hold(sk); /* sock_put below */
1907 lock_sock(sk);
1908
1909 if (lsmc->sk.sk_state != SMC_LISTEN) {
1910 rc = -EINVAL;
1911 release_sock(sk);
1912 goto out;
1913 }
1914
1915 /* Wait for an incoming connection */
1916 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1917 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1918 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1919 set_current_state(TASK_INTERRUPTIBLE);
1920 if (!timeo) {
1921 rc = -EAGAIN;
1922 break;
1923 }
1924 release_sock(sk);
1925 timeo = schedule_timeout(timeo);
1926 /* wakeup by sk_data_ready in smc_listen_work() */
1927 sched_annotate_sleep();
1928 lock_sock(sk);
1929 if (signal_pending(current)) {
1930 rc = sock_intr_errno(timeo);
1931 break;
1932 }
1933 }
1934 set_current_state(TASK_RUNNING);
1935 remove_wait_queue(sk_sleep(sk), &wait);
1936
1937 if (!rc)
1938 rc = sock_error(nsk);
1939 release_sock(sk);
1940 if (rc)
1941 goto out;
1942
1943 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1944 /* wait till data arrives on the socket */
1945 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1946 MSEC_PER_SEC);
1947 if (smc_sk(nsk)->use_fallback) {
1948 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1949
1950 lock_sock(clcsk);
1951 if (skb_queue_empty(&clcsk->sk_receive_queue))
1952 sk_wait_data(clcsk, &timeo, NULL);
1953 release_sock(clcsk);
1954 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1955 lock_sock(nsk);
1956 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
1957 release_sock(nsk);
1958 }
1959 }
1960
1961 out:
1962 sock_put(sk); /* sock_hold above */
1963 return rc;
1964 }
1965
smc_getname(struct socket * sock,struct sockaddr * addr,int peer)1966 static int smc_getname(struct socket *sock, struct sockaddr *addr,
1967 int peer)
1968 {
1969 struct smc_sock *smc;
1970
1971 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1972 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
1973 return -ENOTCONN;
1974
1975 smc = smc_sk(sock->sk);
1976
1977 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
1978 }
1979
smc_sendmsg(struct socket * sock,struct msghdr * msg,size_t len)1980 static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1981 {
1982 struct sock *sk = sock->sk;
1983 struct smc_sock *smc;
1984 int rc = -EPIPE;
1985
1986 smc = smc_sk(sk);
1987 lock_sock(sk);
1988 if ((sk->sk_state != SMC_ACTIVE) &&
1989 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1990 (sk->sk_state != SMC_INIT))
1991 goto out;
1992
1993 if (msg->msg_flags & MSG_FASTOPEN) {
1994 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
1995 smc_switch_to_fallback(smc);
1996 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
1997 } else {
1998 rc = -EINVAL;
1999 goto out;
2000 }
2001 }
2002
2003 if (smc->use_fallback)
2004 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
2005 else
2006 rc = smc_tx_sendmsg(smc, msg, len);
2007 out:
2008 release_sock(sk);
2009 return rc;
2010 }
2011
smc_recvmsg(struct socket * sock,struct msghdr * msg,size_t len,int flags)2012 static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
2013 int flags)
2014 {
2015 struct sock *sk = sock->sk;
2016 struct smc_sock *smc;
2017 int rc = -ENOTCONN;
2018
2019 smc = smc_sk(sk);
2020 lock_sock(sk);
2021 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2022 /* socket was connected before, no more data to read */
2023 rc = 0;
2024 goto out;
2025 }
2026 if ((sk->sk_state == SMC_INIT) ||
2027 (sk->sk_state == SMC_LISTEN) ||
2028 (sk->sk_state == SMC_CLOSED))
2029 goto out;
2030
2031 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2032 rc = 0;
2033 goto out;
2034 }
2035
2036 if (smc->use_fallback) {
2037 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
2038 } else {
2039 msg->msg_namelen = 0;
2040 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
2041 }
2042
2043 out:
2044 release_sock(sk);
2045 return rc;
2046 }
2047
smc_accept_poll(struct sock * parent)2048 static __poll_t smc_accept_poll(struct sock *parent)
2049 {
2050 struct smc_sock *isk = smc_sk(parent);
2051 __poll_t mask = 0;
2052
2053 spin_lock(&isk->accept_q_lock);
2054 if (!list_empty(&isk->accept_q))
2055 mask = EPOLLIN | EPOLLRDNORM;
2056 spin_unlock(&isk->accept_q_lock);
2057
2058 return mask;
2059 }
2060
smc_poll(struct file * file,struct socket * sock,poll_table * wait)2061 static __poll_t smc_poll(struct file *file, struct socket *sock,
2062 poll_table *wait)
2063 {
2064 struct sock *sk = sock->sk;
2065 struct smc_sock *smc;
2066 __poll_t mask = 0;
2067
2068 if (!sk)
2069 return EPOLLNVAL;
2070
2071 smc = smc_sk(sock->sk);
2072 if (smc->use_fallback) {
2073 /* delegate to CLC child sock */
2074 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
2075 sk->sk_err = smc->clcsock->sk->sk_err;
2076 } else {
2077 if (sk->sk_state != SMC_CLOSED)
2078 sock_poll_wait(file, sock, wait);
2079 if (sk->sk_err)
2080 mask |= EPOLLERR;
2081 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
2082 (sk->sk_state == SMC_CLOSED))
2083 mask |= EPOLLHUP;
2084 if (sk->sk_state == SMC_LISTEN) {
2085 /* woken up by sk_data_ready in smc_listen_work() */
2086 mask |= smc_accept_poll(sk);
2087 } else if (smc->use_fallback) { /* as result of connect_work()*/
2088 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
2089 wait);
2090 sk->sk_err = smc->clcsock->sk->sk_err;
2091 } else {
2092 if ((sk->sk_state != SMC_INIT &&
2093 atomic_read(&smc->conn.sndbuf_space)) ||
2094 sk->sk_shutdown & SEND_SHUTDOWN) {
2095 mask |= EPOLLOUT | EPOLLWRNORM;
2096 } else {
2097 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
2098 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
2099 }
2100 if (atomic_read(&smc->conn.bytes_to_rcv))
2101 mask |= EPOLLIN | EPOLLRDNORM;
2102 if (sk->sk_shutdown & RCV_SHUTDOWN)
2103 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
2104 if (sk->sk_state == SMC_APPCLOSEWAIT1)
2105 mask |= EPOLLIN;
2106 if (smc->conn.urg_state == SMC_URG_VALID)
2107 mask |= EPOLLPRI;
2108 }
2109 }
2110
2111 return mask;
2112 }
2113
smc_shutdown(struct socket * sock,int how)2114 static int smc_shutdown(struct socket *sock, int how)
2115 {
2116 struct sock *sk = sock->sk;
2117 bool do_shutdown = true;
2118 struct smc_sock *smc;
2119 int rc = -EINVAL;
2120 int old_state;
2121 int rc1 = 0;
2122
2123 smc = smc_sk(sk);
2124
2125 if ((how < SHUT_RD) || (how > SHUT_RDWR))
2126 return rc;
2127
2128 lock_sock(sk);
2129
2130 rc = -ENOTCONN;
2131 if ((sk->sk_state != SMC_ACTIVE) &&
2132 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
2133 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
2134 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2135 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
2136 (sk->sk_state != SMC_APPFINCLOSEWAIT))
2137 goto out;
2138 if (smc->use_fallback) {
2139 rc = kernel_sock_shutdown(smc->clcsock, how);
2140 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
2141 if (sk->sk_shutdown == SHUTDOWN_MASK)
2142 sk->sk_state = SMC_CLOSED;
2143 goto out;
2144 }
2145 switch (how) {
2146 case SHUT_RDWR: /* shutdown in both directions */
2147 old_state = sk->sk_state;
2148 rc = smc_close_active(smc);
2149 if (old_state == SMC_ACTIVE &&
2150 sk->sk_state == SMC_PEERCLOSEWAIT1)
2151 do_shutdown = false;
2152 break;
2153 case SHUT_WR:
2154 rc = smc_close_shutdown_write(smc);
2155 break;
2156 case SHUT_RD:
2157 rc = 0;
2158 /* nothing more to do because peer is not involved */
2159 break;
2160 }
2161 if (do_shutdown && smc->clcsock)
2162 rc1 = kernel_sock_shutdown(smc->clcsock, how);
2163 /* map sock_shutdown_cmd constants to sk_shutdown value range */
2164 sk->sk_shutdown |= how + 1;
2165
2166 out:
2167 release_sock(sk);
2168 return rc ? rc : rc1;
2169 }
2170
smc_setsockopt(struct socket * sock,int level,int optname,sockptr_t optval,unsigned int optlen)2171 static int smc_setsockopt(struct socket *sock, int level, int optname,
2172 sockptr_t optval, unsigned int optlen)
2173 {
2174 struct sock *sk = sock->sk;
2175 struct smc_sock *smc;
2176 int val, rc;
2177
2178 if (level == SOL_TCP && optname == TCP_ULP)
2179 return -EOPNOTSUPP;
2180
2181 smc = smc_sk(sk);
2182
2183 /* generic setsockopts reaching us here always apply to the
2184 * CLC socket
2185 */
2186 if (unlikely(!smc->clcsock->ops->setsockopt))
2187 rc = -EOPNOTSUPP;
2188 else
2189 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
2190 optval, optlen);
2191 if (smc->clcsock->sk->sk_err) {
2192 sk->sk_err = smc->clcsock->sk->sk_err;
2193 sk->sk_error_report(sk);
2194 }
2195
2196 if (optlen < sizeof(int))
2197 return -EINVAL;
2198 if (copy_from_sockptr(&val, optval, sizeof(int)))
2199 return -EFAULT;
2200
2201 lock_sock(sk);
2202 if (rc || smc->use_fallback)
2203 goto out;
2204 switch (optname) {
2205 case TCP_FASTOPEN:
2206 case TCP_FASTOPEN_CONNECT:
2207 case TCP_FASTOPEN_KEY:
2208 case TCP_FASTOPEN_NO_COOKIE:
2209 /* option not supported by SMC */
2210 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
2211 smc_switch_to_fallback(smc);
2212 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
2213 } else {
2214 rc = -EINVAL;
2215 }
2216 break;
2217 case TCP_NODELAY:
2218 if (sk->sk_state != SMC_INIT &&
2219 sk->sk_state != SMC_LISTEN &&
2220 sk->sk_state != SMC_CLOSED) {
2221 if (val)
2222 mod_delayed_work(smc->conn.lgr->tx_wq,
2223 &smc->conn.tx_work, 0);
2224 }
2225 break;
2226 case TCP_CORK:
2227 if (sk->sk_state != SMC_INIT &&
2228 sk->sk_state != SMC_LISTEN &&
2229 sk->sk_state != SMC_CLOSED) {
2230 if (!val)
2231 mod_delayed_work(smc->conn.lgr->tx_wq,
2232 &smc->conn.tx_work, 0);
2233 }
2234 break;
2235 case TCP_DEFER_ACCEPT:
2236 smc->sockopt_defer_accept = val;
2237 break;
2238 default:
2239 break;
2240 }
2241 out:
2242 release_sock(sk);
2243
2244 return rc;
2245 }
2246
smc_getsockopt(struct socket * sock,int level,int optname,char __user * optval,int __user * optlen)2247 static int smc_getsockopt(struct socket *sock, int level, int optname,
2248 char __user *optval, int __user *optlen)
2249 {
2250 struct smc_sock *smc;
2251
2252 smc = smc_sk(sock->sk);
2253 /* socket options apply to the CLC socket */
2254 if (unlikely(!smc->clcsock->ops->getsockopt))
2255 return -EOPNOTSUPP;
2256 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
2257 optval, optlen);
2258 }
2259
smc_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)2260 static int smc_ioctl(struct socket *sock, unsigned int cmd,
2261 unsigned long arg)
2262 {
2263 union smc_host_cursor cons, urg;
2264 struct smc_connection *conn;
2265 struct smc_sock *smc;
2266 int answ;
2267
2268 smc = smc_sk(sock->sk);
2269 conn = &smc->conn;
2270 lock_sock(&smc->sk);
2271 if (smc->use_fallback) {
2272 if (!smc->clcsock) {
2273 release_sock(&smc->sk);
2274 return -EBADF;
2275 }
2276 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
2277 release_sock(&smc->sk);
2278 return answ;
2279 }
2280 switch (cmd) {
2281 case SIOCINQ: /* same as FIONREAD */
2282 if (smc->sk.sk_state == SMC_LISTEN) {
2283 release_sock(&smc->sk);
2284 return -EINVAL;
2285 }
2286 if (smc->sk.sk_state == SMC_INIT ||
2287 smc->sk.sk_state == SMC_CLOSED)
2288 answ = 0;
2289 else
2290 answ = atomic_read(&smc->conn.bytes_to_rcv);
2291 break;
2292 case SIOCOUTQ:
2293 /* output queue size (not send + not acked) */
2294 if (smc->sk.sk_state == SMC_LISTEN) {
2295 release_sock(&smc->sk);
2296 return -EINVAL;
2297 }
2298 if (smc->sk.sk_state == SMC_INIT ||
2299 smc->sk.sk_state == SMC_CLOSED)
2300 answ = 0;
2301 else
2302 answ = smc->conn.sndbuf_desc->len -
2303 atomic_read(&smc->conn.sndbuf_space);
2304 break;
2305 case SIOCOUTQNSD:
2306 /* output queue size (not send only) */
2307 if (smc->sk.sk_state == SMC_LISTEN) {
2308 release_sock(&smc->sk);
2309 return -EINVAL;
2310 }
2311 if (smc->sk.sk_state == SMC_INIT ||
2312 smc->sk.sk_state == SMC_CLOSED)
2313 answ = 0;
2314 else
2315 answ = smc_tx_prepared_sends(&smc->conn);
2316 break;
2317 case SIOCATMARK:
2318 if (smc->sk.sk_state == SMC_LISTEN) {
2319 release_sock(&smc->sk);
2320 return -EINVAL;
2321 }
2322 if (smc->sk.sk_state == SMC_INIT ||
2323 smc->sk.sk_state == SMC_CLOSED) {
2324 answ = 0;
2325 } else {
2326 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
2327 smc_curs_copy(&urg, &conn->urg_curs, conn);
2328 answ = smc_curs_diff(conn->rmb_desc->len,
2329 &cons, &urg) == 1;
2330 }
2331 break;
2332 default:
2333 release_sock(&smc->sk);
2334 return -ENOIOCTLCMD;
2335 }
2336 release_sock(&smc->sk);
2337
2338 return put_user(answ, (int __user *)arg);
2339 }
2340
smc_sendpage(struct socket * sock,struct page * page,int offset,size_t size,int flags)2341 static ssize_t smc_sendpage(struct socket *sock, struct page *page,
2342 int offset, size_t size, int flags)
2343 {
2344 struct sock *sk = sock->sk;
2345 struct smc_sock *smc;
2346 int rc = -EPIPE;
2347
2348 smc = smc_sk(sk);
2349 lock_sock(sk);
2350 if (sk->sk_state != SMC_ACTIVE) {
2351 release_sock(sk);
2352 goto out;
2353 }
2354 release_sock(sk);
2355 if (smc->use_fallback)
2356 rc = kernel_sendpage(smc->clcsock, page, offset,
2357 size, flags);
2358 else
2359 rc = sock_no_sendpage(sock, page, offset, size, flags);
2360
2361 out:
2362 return rc;
2363 }
2364
2365 /* Map the affected portions of the rmbe into an spd, note the number of bytes
2366 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
2367 * updates till whenever a respective page has been fully processed.
2368 * Note that subsequent recv() calls have to wait till all splice() processing
2369 * completed.
2370 */
smc_splice_read(struct socket * sock,loff_t * ppos,struct pipe_inode_info * pipe,size_t len,unsigned int flags)2371 static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
2372 struct pipe_inode_info *pipe, size_t len,
2373 unsigned int flags)
2374 {
2375 struct sock *sk = sock->sk;
2376 struct smc_sock *smc;
2377 int rc = -ENOTCONN;
2378
2379 smc = smc_sk(sk);
2380 lock_sock(sk);
2381 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2382 /* socket was connected before, no more data to read */
2383 rc = 0;
2384 goto out;
2385 }
2386 if (sk->sk_state == SMC_INIT ||
2387 sk->sk_state == SMC_LISTEN ||
2388 sk->sk_state == SMC_CLOSED)
2389 goto out;
2390
2391 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2392 rc = 0;
2393 goto out;
2394 }
2395
2396 if (smc->use_fallback) {
2397 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
2398 pipe, len, flags);
2399 } else {
2400 if (*ppos) {
2401 rc = -ESPIPE;
2402 goto out;
2403 }
2404 if (flags & SPLICE_F_NONBLOCK)
2405 flags = MSG_DONTWAIT;
2406 else
2407 flags = 0;
2408 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
2409 }
2410 out:
2411 release_sock(sk);
2412
2413 return rc;
2414 }
2415
2416 /* must look like tcp */
2417 static const struct proto_ops smc_sock_ops = {
2418 .family = PF_SMC,
2419 .owner = THIS_MODULE,
2420 .release = smc_release,
2421 .bind = smc_bind,
2422 .connect = smc_connect,
2423 .socketpair = sock_no_socketpair,
2424 .accept = smc_accept,
2425 .getname = smc_getname,
2426 .poll = smc_poll,
2427 .ioctl = smc_ioctl,
2428 .listen = smc_listen,
2429 .shutdown = smc_shutdown,
2430 .setsockopt = smc_setsockopt,
2431 .getsockopt = smc_getsockopt,
2432 .sendmsg = smc_sendmsg,
2433 .recvmsg = smc_recvmsg,
2434 .mmap = sock_no_mmap,
2435 .sendpage = smc_sendpage,
2436 .splice_read = smc_splice_read,
2437 };
2438
smc_create(struct net * net,struct socket * sock,int protocol,int kern)2439 static int smc_create(struct net *net, struct socket *sock, int protocol,
2440 int kern)
2441 {
2442 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
2443 struct smc_sock *smc;
2444 struct sock *sk;
2445 int rc;
2446
2447 rc = -ESOCKTNOSUPPORT;
2448 if (sock->type != SOCK_STREAM)
2449 goto out;
2450
2451 rc = -EPROTONOSUPPORT;
2452 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
2453 goto out;
2454
2455 rc = -ENOBUFS;
2456 sock->ops = &smc_sock_ops;
2457 sk = smc_sock_alloc(net, sock, protocol);
2458 if (!sk)
2459 goto out;
2460
2461 /* create internal TCP socket for CLC handshake and fallback */
2462 smc = smc_sk(sk);
2463 smc->use_fallback = false; /* assume rdma capability first */
2464 smc->fallback_rsn = 0;
2465 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
2466 &smc->clcsock);
2467 if (rc) {
2468 sk_common_release(sk);
2469 goto out;
2470 }
2471 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
2472 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
2473
2474 out:
2475 return rc;
2476 }
2477
2478 static const struct net_proto_family smc_sock_family_ops = {
2479 .family = PF_SMC,
2480 .owner = THIS_MODULE,
2481 .create = smc_create,
2482 };
2483
2484 unsigned int smc_net_id;
2485
smc_net_init(struct net * net)2486 static __net_init int smc_net_init(struct net *net)
2487 {
2488 return smc_pnet_net_init(net);
2489 }
2490
smc_net_exit(struct net * net)2491 static void __net_exit smc_net_exit(struct net *net)
2492 {
2493 smc_pnet_net_exit(net);
2494 }
2495
2496 static struct pernet_operations smc_net_ops = {
2497 .init = smc_net_init,
2498 .exit = smc_net_exit,
2499 .id = &smc_net_id,
2500 .size = sizeof(struct smc_net),
2501 };
2502
smc_init(void)2503 static int __init smc_init(void)
2504 {
2505 int rc;
2506
2507 rc = register_pernet_subsys(&smc_net_ops);
2508 if (rc)
2509 return rc;
2510
2511 smc_ism_init();
2512 smc_clc_init();
2513
2514 rc = smc_pnet_init();
2515 if (rc)
2516 goto out_pernet_subsys;
2517
2518 rc = -ENOMEM;
2519 smc_hs_wq = alloc_workqueue("smc_hs_wq", 0, 0);
2520 if (!smc_hs_wq)
2521 goto out_pnet;
2522
2523 smc_close_wq = alloc_workqueue("smc_close_wq", 0, 0);
2524 if (!smc_close_wq)
2525 goto out_alloc_hs_wq;
2526
2527 rc = smc_core_init();
2528 if (rc) {
2529 pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
2530 goto out_alloc_wqs;
2531 }
2532
2533 rc = smc_llc_init();
2534 if (rc) {
2535 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
2536 goto out_core;
2537 }
2538
2539 rc = smc_cdc_init();
2540 if (rc) {
2541 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
2542 goto out_core;
2543 }
2544
2545 rc = proto_register(&smc_proto, 1);
2546 if (rc) {
2547 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
2548 goto out_core;
2549 }
2550
2551 rc = proto_register(&smc_proto6, 1);
2552 if (rc) {
2553 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2554 goto out_proto;
2555 }
2556
2557 rc = sock_register(&smc_sock_family_ops);
2558 if (rc) {
2559 pr_err("%s: sock_register fails with %d\n", __func__, rc);
2560 goto out_proto6;
2561 }
2562 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
2563 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
2564
2565 rc = smc_ib_register_client();
2566 if (rc) {
2567 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2568 goto out_sock;
2569 }
2570
2571 static_branch_enable(&tcp_have_smc);
2572 return 0;
2573
2574 out_sock:
2575 sock_unregister(PF_SMC);
2576 out_proto6:
2577 proto_unregister(&smc_proto6);
2578 out_proto:
2579 proto_unregister(&smc_proto);
2580 out_core:
2581 smc_core_exit();
2582 out_alloc_wqs:
2583 destroy_workqueue(smc_close_wq);
2584 out_alloc_hs_wq:
2585 destroy_workqueue(smc_hs_wq);
2586 out_pnet:
2587 smc_pnet_exit();
2588 out_pernet_subsys:
2589 unregister_pernet_subsys(&smc_net_ops);
2590
2591 return rc;
2592 }
2593
smc_exit(void)2594 static void __exit smc_exit(void)
2595 {
2596 static_branch_disable(&tcp_have_smc);
2597 sock_unregister(PF_SMC);
2598 smc_core_exit();
2599 smc_ib_unregister_client();
2600 destroy_workqueue(smc_close_wq);
2601 destroy_workqueue(smc_hs_wq);
2602 proto_unregister(&smc_proto6);
2603 proto_unregister(&smc_proto);
2604 smc_pnet_exit();
2605 unregister_pernet_subsys(&smc_net_ops);
2606 rcu_barrier();
2607 }
2608
2609 module_init(smc_init);
2610 module_exit(smc_exit);
2611
2612 MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2613 MODULE_DESCRIPTION("smc socket address family");
2614 MODULE_LICENSE("GPL");
2615 MODULE_ALIAS_NETPROTO(PF_SMC);
2616