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 #include <linux/splice.h>
31
32 #include <net/sock.h>
33 #include <net/tcp.h>
34 #include <net/smc.h>
35 #include <asm/ioctls.h>
36
37 #include <net/net_namespace.h>
38 #include <net/netns/generic.h>
39 #include "smc_netns.h"
40
41 #include "smc.h"
42 #include "smc_clc.h"
43 #include "smc_llc.h"
44 #include "smc_cdc.h"
45 #include "smc_core.h"
46 #include "smc_ib.h"
47 #include "smc_ism.h"
48 #include "smc_pnet.h"
49 #include "smc_netlink.h"
50 #include "smc_tx.h"
51 #include "smc_rx.h"
52 #include "smc_close.h"
53 #include "smc_stats.h"
54 #include "smc_tracepoint.h"
55 #include "smc_sysctl.h"
56
57 static DEFINE_MUTEX(smc_server_lgr_pending); /* serialize link group
58 * creation on server
59 */
60 static DEFINE_MUTEX(smc_client_lgr_pending); /* serialize link group
61 * creation on client
62 */
63
64 static struct workqueue_struct *smc_tcp_ls_wq; /* wq for tcp listen work */
65 struct workqueue_struct *smc_hs_wq; /* wq for handshake work */
66 struct workqueue_struct *smc_close_wq; /* wq for close work */
67
68 static void smc_tcp_listen_work(struct work_struct *);
69 static void smc_connect_work(struct work_struct *);
70
smc_nl_dump_hs_limitation(struct sk_buff * skb,struct netlink_callback * cb)71 int smc_nl_dump_hs_limitation(struct sk_buff *skb, struct netlink_callback *cb)
72 {
73 struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb);
74 void *hdr;
75
76 if (cb_ctx->pos[0])
77 goto out;
78
79 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
80 &smc_gen_nl_family, NLM_F_MULTI,
81 SMC_NETLINK_DUMP_HS_LIMITATION);
82 if (!hdr)
83 return -ENOMEM;
84
85 if (nla_put_u8(skb, SMC_NLA_HS_LIMITATION_ENABLED,
86 sock_net(skb->sk)->smc.limit_smc_hs))
87 goto err;
88
89 genlmsg_end(skb, hdr);
90 cb_ctx->pos[0] = 1;
91 out:
92 return skb->len;
93 err:
94 genlmsg_cancel(skb, hdr);
95 return -EMSGSIZE;
96 }
97
smc_nl_enable_hs_limitation(struct sk_buff * skb,struct genl_info * info)98 int smc_nl_enable_hs_limitation(struct sk_buff *skb, struct genl_info *info)
99 {
100 sock_net(skb->sk)->smc.limit_smc_hs = true;
101 return 0;
102 }
103
smc_nl_disable_hs_limitation(struct sk_buff * skb,struct genl_info * info)104 int smc_nl_disable_hs_limitation(struct sk_buff *skb, struct genl_info *info)
105 {
106 sock_net(skb->sk)->smc.limit_smc_hs = false;
107 return 0;
108 }
109
smc_set_keepalive(struct sock * sk,int val)110 static void smc_set_keepalive(struct sock *sk, int val)
111 {
112 struct smc_sock *smc = smc_sk(sk);
113
114 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
115 }
116
smc_tcp_syn_recv_sock(const struct sock * sk,struct sk_buff * skb,struct request_sock * req,struct dst_entry * dst,struct request_sock * req_unhash,bool * own_req)117 static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk,
118 struct sk_buff *skb,
119 struct request_sock *req,
120 struct dst_entry *dst,
121 struct request_sock *req_unhash,
122 bool *own_req)
123 {
124 struct smc_sock *smc;
125 struct sock *child;
126
127 smc = smc_clcsock_user_data(sk);
128
129 if (READ_ONCE(sk->sk_ack_backlog) + atomic_read(&smc->queued_smc_hs) >
130 sk->sk_max_ack_backlog)
131 goto drop;
132
133 if (sk_acceptq_is_full(&smc->sk)) {
134 NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
135 goto drop;
136 }
137
138 /* passthrough to original syn recv sock fct */
139 child = smc->ori_af_ops->syn_recv_sock(sk, skb, req, dst, req_unhash,
140 own_req);
141 /* child must not inherit smc or its ops */
142 if (child) {
143 rcu_assign_sk_user_data(child, NULL);
144
145 /* v4-mapped sockets don't inherit parent ops. Don't restore. */
146 if (inet_csk(child)->icsk_af_ops == inet_csk(sk)->icsk_af_ops)
147 inet_csk(child)->icsk_af_ops = smc->ori_af_ops;
148 }
149 return child;
150
151 drop:
152 dst_release(dst);
153 tcp_listendrop(sk);
154 return NULL;
155 }
156
smc_hs_congested(const struct sock * sk)157 static bool smc_hs_congested(const struct sock *sk)
158 {
159 const struct smc_sock *smc;
160
161 smc = smc_clcsock_user_data(sk);
162
163 if (!smc)
164 return true;
165
166 if (workqueue_congested(WORK_CPU_UNBOUND, smc_hs_wq))
167 return true;
168
169 return false;
170 }
171
172 static struct smc_hashinfo smc_v4_hashinfo = {
173 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
174 };
175
176 static struct smc_hashinfo smc_v6_hashinfo = {
177 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
178 };
179
smc_hash_sk(struct sock * sk)180 int smc_hash_sk(struct sock *sk)
181 {
182 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
183 struct hlist_head *head;
184
185 head = &h->ht;
186
187 write_lock_bh(&h->lock);
188 sk_add_node(sk, head);
189 write_unlock_bh(&h->lock);
190 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
191
192 return 0;
193 }
194 EXPORT_SYMBOL_GPL(smc_hash_sk);
195
smc_unhash_sk(struct sock * sk)196 void smc_unhash_sk(struct sock *sk)
197 {
198 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
199
200 write_lock_bh(&h->lock);
201 if (sk_del_node_init(sk))
202 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
203 write_unlock_bh(&h->lock);
204 }
205 EXPORT_SYMBOL_GPL(smc_unhash_sk);
206
207 /* This will be called before user really release sock_lock. So do the
208 * work which we didn't do because of user hold the sock_lock in the
209 * BH context
210 */
smc_release_cb(struct sock * sk)211 static void smc_release_cb(struct sock *sk)
212 {
213 struct smc_sock *smc = smc_sk(sk);
214
215 if (smc->conn.tx_in_release_sock) {
216 smc_tx_pending(&smc->conn);
217 smc->conn.tx_in_release_sock = false;
218 }
219 }
220
221 struct proto smc_proto = {
222 .name = "SMC",
223 .owner = THIS_MODULE,
224 .keepalive = smc_set_keepalive,
225 .hash = smc_hash_sk,
226 .unhash = smc_unhash_sk,
227 .release_cb = smc_release_cb,
228 .obj_size = sizeof(struct smc_sock),
229 .h.smc_hash = &smc_v4_hashinfo,
230 .slab_flags = SLAB_TYPESAFE_BY_RCU,
231 };
232 EXPORT_SYMBOL_GPL(smc_proto);
233
234 struct proto smc_proto6 = {
235 .name = "SMC6",
236 .owner = THIS_MODULE,
237 .keepalive = smc_set_keepalive,
238 .hash = smc_hash_sk,
239 .unhash = smc_unhash_sk,
240 .release_cb = smc_release_cb,
241 .obj_size = sizeof(struct smc_sock),
242 .h.smc_hash = &smc_v6_hashinfo,
243 .slab_flags = SLAB_TYPESAFE_BY_RCU,
244 };
245 EXPORT_SYMBOL_GPL(smc_proto6);
246
smc_fback_restore_callbacks(struct smc_sock * smc)247 static void smc_fback_restore_callbacks(struct smc_sock *smc)
248 {
249 struct sock *clcsk = smc->clcsock->sk;
250
251 write_lock_bh(&clcsk->sk_callback_lock);
252 clcsk->sk_user_data = NULL;
253
254 smc_clcsock_restore_cb(&clcsk->sk_state_change, &smc->clcsk_state_change);
255 smc_clcsock_restore_cb(&clcsk->sk_data_ready, &smc->clcsk_data_ready);
256 smc_clcsock_restore_cb(&clcsk->sk_write_space, &smc->clcsk_write_space);
257 smc_clcsock_restore_cb(&clcsk->sk_error_report, &smc->clcsk_error_report);
258
259 write_unlock_bh(&clcsk->sk_callback_lock);
260 }
261
smc_restore_fallback_changes(struct smc_sock * smc)262 static void smc_restore_fallback_changes(struct smc_sock *smc)
263 {
264 if (smc->clcsock->file) { /* non-accepted sockets have no file yet */
265 smc->clcsock->file->private_data = smc->sk.sk_socket;
266 smc->clcsock->file = NULL;
267 smc_fback_restore_callbacks(smc);
268 }
269 }
270
__smc_release(struct smc_sock * smc)271 static int __smc_release(struct smc_sock *smc)
272 {
273 struct sock *sk = &smc->sk;
274 int rc = 0;
275
276 if (!smc->use_fallback) {
277 rc = smc_close_active(smc);
278 smc_sock_set_flag(sk, SOCK_DEAD);
279 sk->sk_shutdown |= SHUTDOWN_MASK;
280 } else {
281 if (sk->sk_state != SMC_CLOSED) {
282 if (sk->sk_state != SMC_LISTEN &&
283 sk->sk_state != SMC_INIT)
284 sock_put(sk); /* passive closing */
285 if (sk->sk_state == SMC_LISTEN) {
286 /* wake up clcsock accept */
287 rc = kernel_sock_shutdown(smc->clcsock,
288 SHUT_RDWR);
289 }
290 sk->sk_state = SMC_CLOSED;
291 sk->sk_state_change(sk);
292 }
293 smc_restore_fallback_changes(smc);
294 }
295
296 sk->sk_prot->unhash(sk);
297
298 if (sk->sk_state == SMC_CLOSED) {
299 if (smc->clcsock) {
300 release_sock(sk);
301 smc_clcsock_release(smc);
302 lock_sock(sk);
303 }
304 if (!smc->use_fallback)
305 smc_conn_free(&smc->conn);
306 }
307
308 return rc;
309 }
310
smc_release(struct socket * sock)311 static int smc_release(struct socket *sock)
312 {
313 struct sock *sk = sock->sk;
314 struct smc_sock *smc;
315 int old_state, rc = 0;
316
317 if (!sk)
318 goto out;
319
320 sock_hold(sk); /* sock_put below */
321 smc = smc_sk(sk);
322
323 old_state = sk->sk_state;
324
325 /* cleanup for a dangling non-blocking connect */
326 if (smc->connect_nonblock && old_state == SMC_INIT)
327 tcp_abort(smc->clcsock->sk, ECONNABORTED);
328
329 if (cancel_work_sync(&smc->connect_work))
330 sock_put(&smc->sk); /* sock_hold in smc_connect for passive closing */
331
332 if (sk->sk_state == SMC_LISTEN)
333 /* smc_close_non_accepted() is called and acquires
334 * sock lock for child sockets again
335 */
336 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
337 else
338 lock_sock(sk);
339
340 if (old_state == SMC_INIT && sk->sk_state == SMC_ACTIVE &&
341 !smc->use_fallback)
342 smc_close_active_abort(smc);
343
344 rc = __smc_release(smc);
345
346 /* detach socket */
347 sock_orphan(sk);
348 sock->sk = NULL;
349 release_sock(sk);
350
351 sock_put(sk); /* sock_hold above */
352 sock_put(sk); /* final sock_put */
353 out:
354 return rc;
355 }
356
smc_destruct(struct sock * sk)357 static void smc_destruct(struct sock *sk)
358 {
359 if (sk->sk_state != SMC_CLOSED)
360 return;
361 if (!sock_flag(sk, SOCK_DEAD))
362 return;
363 }
364
smc_sock_alloc(struct net * net,struct socket * sock,int protocol)365 static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
366 int protocol)
367 {
368 struct smc_sock *smc;
369 struct proto *prot;
370 struct sock *sk;
371
372 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
373 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
374 if (!sk)
375 return NULL;
376
377 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
378 sk->sk_state = SMC_INIT;
379 sk->sk_destruct = smc_destruct;
380 sk->sk_protocol = protocol;
381 WRITE_ONCE(sk->sk_sndbuf, 2 * READ_ONCE(net->smc.sysctl_wmem));
382 WRITE_ONCE(sk->sk_rcvbuf, 2 * READ_ONCE(net->smc.sysctl_rmem));
383 smc = smc_sk(sk);
384 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
385 INIT_WORK(&smc->connect_work, smc_connect_work);
386 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
387 INIT_LIST_HEAD(&smc->accept_q);
388 spin_lock_init(&smc->accept_q_lock);
389 spin_lock_init(&smc->conn.send_lock);
390 sk->sk_prot->hash(sk);
391 mutex_init(&smc->clcsock_release_lock);
392 smc_init_saved_callbacks(smc);
393
394 return sk;
395 }
396
smc_bind(struct socket * sock,struct sockaddr * uaddr,int addr_len)397 static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
398 int addr_len)
399 {
400 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
401 struct sock *sk = sock->sk;
402 struct smc_sock *smc;
403 int rc;
404
405 smc = smc_sk(sk);
406
407 /* replicate tests from inet_bind(), to be safe wrt. future changes */
408 rc = -EINVAL;
409 if (addr_len < sizeof(struct sockaddr_in))
410 goto out;
411
412 rc = -EAFNOSUPPORT;
413 if (addr->sin_family != AF_INET &&
414 addr->sin_family != AF_INET6 &&
415 addr->sin_family != AF_UNSPEC)
416 goto out;
417 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
418 if (addr->sin_family == AF_UNSPEC &&
419 addr->sin_addr.s_addr != htonl(INADDR_ANY))
420 goto out;
421
422 lock_sock(sk);
423
424 /* Check if socket is already active */
425 rc = -EINVAL;
426 if (sk->sk_state != SMC_INIT || smc->connect_nonblock)
427 goto out_rel;
428
429 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
430 smc->clcsock->sk->sk_reuseport = sk->sk_reuseport;
431 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
432
433 out_rel:
434 release_sock(sk);
435 out:
436 return rc;
437 }
438
439 /* copy only relevant settings and flags of SOL_SOCKET level from smc to
440 * clc socket (since smc is not called for these options from net/core)
441 */
442
443 #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
444 (1UL << SOCK_KEEPOPEN) | \
445 (1UL << SOCK_LINGER) | \
446 (1UL << SOCK_BROADCAST) | \
447 (1UL << SOCK_TIMESTAMP) | \
448 (1UL << SOCK_DBG) | \
449 (1UL << SOCK_RCVTSTAMP) | \
450 (1UL << SOCK_RCVTSTAMPNS) | \
451 (1UL << SOCK_LOCALROUTE) | \
452 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
453 (1UL << SOCK_RXQ_OVFL) | \
454 (1UL << SOCK_WIFI_STATUS) | \
455 (1UL << SOCK_NOFCS) | \
456 (1UL << SOCK_FILTER_LOCKED) | \
457 (1UL << SOCK_TSTAMP_NEW))
458
459 /* if set, use value set by setsockopt() - else use IPv4 or SMC sysctl value */
smc_adjust_sock_bufsizes(struct sock * nsk,struct sock * osk,unsigned long mask)460 static void smc_adjust_sock_bufsizes(struct sock *nsk, struct sock *osk,
461 unsigned long mask)
462 {
463 nsk->sk_userlocks = osk->sk_userlocks;
464 if (osk->sk_userlocks & SOCK_SNDBUF_LOCK)
465 nsk->sk_sndbuf = osk->sk_sndbuf;
466 if (osk->sk_userlocks & SOCK_RCVBUF_LOCK)
467 nsk->sk_rcvbuf = osk->sk_rcvbuf;
468 }
469
smc_copy_sock_settings(struct sock * nsk,struct sock * osk,unsigned long mask)470 static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
471 unsigned long mask)
472 {
473 /* options we don't get control via setsockopt for */
474 nsk->sk_type = osk->sk_type;
475 nsk->sk_sndtimeo = osk->sk_sndtimeo;
476 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
477 nsk->sk_mark = READ_ONCE(osk->sk_mark);
478 nsk->sk_priority = osk->sk_priority;
479 nsk->sk_rcvlowat = osk->sk_rcvlowat;
480 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
481 nsk->sk_err = osk->sk_err;
482
483 nsk->sk_flags &= ~mask;
484 nsk->sk_flags |= osk->sk_flags & mask;
485
486 smc_adjust_sock_bufsizes(nsk, osk, mask);
487 }
488
smc_copy_sock_settings_to_clc(struct smc_sock * smc)489 static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
490 {
491 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
492 }
493
494 #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
495 (1UL << SOCK_KEEPOPEN) | \
496 (1UL << SOCK_LINGER) | \
497 (1UL << SOCK_DBG))
498 /* copy only settings and flags relevant for smc from clc to smc socket */
smc_copy_sock_settings_to_smc(struct smc_sock * smc)499 static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
500 {
501 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
502 }
503
504 /* register the new vzalloced sndbuf on all links */
smcr_lgr_reg_sndbufs(struct smc_link * link,struct smc_buf_desc * snd_desc)505 static int smcr_lgr_reg_sndbufs(struct smc_link *link,
506 struct smc_buf_desc *snd_desc)
507 {
508 struct smc_link_group *lgr = link->lgr;
509 int i, rc = 0;
510
511 if (!snd_desc->is_vm)
512 return -EINVAL;
513
514 /* protect against parallel smcr_link_reg_buf() */
515 down_write(&lgr->llc_conf_mutex);
516 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
517 if (!smc_link_active(&lgr->lnk[i]))
518 continue;
519 rc = smcr_link_reg_buf(&lgr->lnk[i], snd_desc);
520 if (rc)
521 break;
522 }
523 up_write(&lgr->llc_conf_mutex);
524 return rc;
525 }
526
527 /* register the new rmb on all links */
smcr_lgr_reg_rmbs(struct smc_link * link,struct smc_buf_desc * rmb_desc)528 static int smcr_lgr_reg_rmbs(struct smc_link *link,
529 struct smc_buf_desc *rmb_desc)
530 {
531 struct smc_link_group *lgr = link->lgr;
532 bool do_slow = false;
533 int i, rc = 0;
534
535 rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY);
536 if (rc)
537 return rc;
538
539 down_read(&lgr->llc_conf_mutex);
540 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
541 if (!smc_link_active(&lgr->lnk[i]))
542 continue;
543 if (!rmb_desc->is_reg_mr[link->link_idx]) {
544 up_read(&lgr->llc_conf_mutex);
545 goto slow_path;
546 }
547 }
548 /* mr register already */
549 goto fast_path;
550 slow_path:
551 do_slow = true;
552 /* protect against parallel smc_llc_cli_rkey_exchange() and
553 * parallel smcr_link_reg_buf()
554 */
555 down_write(&lgr->llc_conf_mutex);
556 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
557 if (!smc_link_active(&lgr->lnk[i]))
558 continue;
559 rc = smcr_link_reg_buf(&lgr->lnk[i], rmb_desc);
560 if (rc)
561 goto out;
562 }
563 fast_path:
564 /* exchange confirm_rkey msg with peer */
565 rc = smc_llc_do_confirm_rkey(link, rmb_desc);
566 if (rc) {
567 rc = -EFAULT;
568 goto out;
569 }
570 rmb_desc->is_conf_rkey = true;
571 out:
572 do_slow ? up_write(&lgr->llc_conf_mutex) : up_read(&lgr->llc_conf_mutex);
573 smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
574 return rc;
575 }
576
smcr_clnt_conf_first_link(struct smc_sock * smc)577 static int smcr_clnt_conf_first_link(struct smc_sock *smc)
578 {
579 struct smc_link *link = smc->conn.lnk;
580 struct smc_llc_qentry *qentry;
581 int rc;
582
583 /* Receive CONFIRM LINK request from server over RoCE fabric.
584 * Increasing the client's timeout by twice as much as the server's
585 * timeout by default can temporarily avoid decline messages of
586 * both sides crossing or colliding
587 */
588 qentry = smc_llc_wait(link->lgr, NULL, 2 * SMC_LLC_WAIT_TIME,
589 SMC_LLC_CONFIRM_LINK);
590 if (!qentry) {
591 struct smc_clc_msg_decline dclc;
592
593 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
594 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
595 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
596 }
597 smc_llc_save_peer_uid(qentry);
598 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_REQ);
599 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
600 if (rc)
601 return SMC_CLC_DECL_RMBE_EC;
602
603 rc = smc_ib_modify_qp_rts(link);
604 if (rc)
605 return SMC_CLC_DECL_ERR_RDYLNK;
606
607 smc_wr_remember_qp_attr(link);
608
609 /* reg the sndbuf if it was vzalloced */
610 if (smc->conn.sndbuf_desc->is_vm) {
611 if (smcr_link_reg_buf(link, smc->conn.sndbuf_desc))
612 return SMC_CLC_DECL_ERR_REGBUF;
613 }
614
615 /* reg the rmb */
616 if (smcr_link_reg_buf(link, smc->conn.rmb_desc))
617 return SMC_CLC_DECL_ERR_REGBUF;
618
619 /* confirm_rkey is implicit on 1st contact */
620 smc->conn.rmb_desc->is_conf_rkey = true;
621
622 /* send CONFIRM LINK response over RoCE fabric */
623 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
624 if (rc < 0)
625 return SMC_CLC_DECL_TIMEOUT_CL;
626
627 smc_llc_link_active(link);
628 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
629
630 if (link->lgr->max_links > 1) {
631 /* optional 2nd link, receive ADD LINK request from server */
632 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
633 SMC_LLC_ADD_LINK);
634 if (!qentry) {
635 struct smc_clc_msg_decline dclc;
636
637 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
638 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
639 if (rc == -EAGAIN)
640 rc = 0; /* no DECLINE received, go with one link */
641 return rc;
642 }
643 smc_llc_flow_qentry_clr(&link->lgr->llc_flow_lcl);
644 smc_llc_cli_add_link(link, qentry);
645 }
646 return 0;
647 }
648
smc_isascii(char * hostname)649 static bool smc_isascii(char *hostname)
650 {
651 int i;
652
653 for (i = 0; i < SMC_MAX_HOSTNAME_LEN; i++)
654 if (!isascii(hostname[i]))
655 return false;
656 return true;
657 }
658
smc_conn_save_peer_info_fce(struct smc_sock * smc,struct smc_clc_msg_accept_confirm * clc)659 static void smc_conn_save_peer_info_fce(struct smc_sock *smc,
660 struct smc_clc_msg_accept_confirm *clc)
661 {
662 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
663 (struct smc_clc_msg_accept_confirm_v2 *)clc;
664 struct smc_clc_first_contact_ext *fce;
665 int clc_v2_len;
666
667 if (clc->hdr.version == SMC_V1 ||
668 !(clc->hdr.typev2 & SMC_FIRST_CONTACT_MASK))
669 return;
670
671 if (smc->conn.lgr->is_smcd) {
672 memcpy(smc->conn.lgr->negotiated_eid, clc_v2->d1.eid,
673 SMC_MAX_EID_LEN);
674 clc_v2_len = offsetofend(struct smc_clc_msg_accept_confirm_v2,
675 d1);
676 } else {
677 memcpy(smc->conn.lgr->negotiated_eid, clc_v2->r1.eid,
678 SMC_MAX_EID_LEN);
679 clc_v2_len = offsetofend(struct smc_clc_msg_accept_confirm_v2,
680 r1);
681 }
682 fce = (struct smc_clc_first_contact_ext *)(((u8 *)clc_v2) + clc_v2_len);
683 smc->conn.lgr->peer_os = fce->os_type;
684 smc->conn.lgr->peer_smc_release = fce->release;
685 if (smc_isascii(fce->hostname))
686 memcpy(smc->conn.lgr->peer_hostname, fce->hostname,
687 SMC_MAX_HOSTNAME_LEN);
688 }
689
smcr_conn_save_peer_info(struct smc_sock * smc,struct smc_clc_msg_accept_confirm * clc)690 static void smcr_conn_save_peer_info(struct smc_sock *smc,
691 struct smc_clc_msg_accept_confirm *clc)
692 {
693 int bufsize = smc_uncompress_bufsize(clc->r0.rmbe_size);
694
695 smc->conn.peer_rmbe_idx = clc->r0.rmbe_idx;
696 smc->conn.local_tx_ctrl.token = ntohl(clc->r0.rmbe_alert_token);
697 smc->conn.peer_rmbe_size = bufsize;
698 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
699 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
700 }
701
smcd_conn_save_peer_info(struct smc_sock * smc,struct smc_clc_msg_accept_confirm * clc)702 static void smcd_conn_save_peer_info(struct smc_sock *smc,
703 struct smc_clc_msg_accept_confirm *clc)
704 {
705 int bufsize = smc_uncompress_bufsize(clc->d0.dmbe_size);
706
707 smc->conn.peer_rmbe_idx = clc->d0.dmbe_idx;
708 smc->conn.peer_token = ntohll(clc->d0.token);
709 /* msg header takes up space in the buffer */
710 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
711 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
712 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
713 }
714
smc_conn_save_peer_info(struct smc_sock * smc,struct smc_clc_msg_accept_confirm * clc)715 static void smc_conn_save_peer_info(struct smc_sock *smc,
716 struct smc_clc_msg_accept_confirm *clc)
717 {
718 if (smc->conn.lgr->is_smcd)
719 smcd_conn_save_peer_info(smc, clc);
720 else
721 smcr_conn_save_peer_info(smc, clc);
722 smc_conn_save_peer_info_fce(smc, clc);
723 }
724
smc_link_save_peer_info(struct smc_link * link,struct smc_clc_msg_accept_confirm * clc,struct smc_init_info * ini)725 static void smc_link_save_peer_info(struct smc_link *link,
726 struct smc_clc_msg_accept_confirm *clc,
727 struct smc_init_info *ini)
728 {
729 link->peer_qpn = ntoh24(clc->r0.qpn);
730 memcpy(link->peer_gid, ini->peer_gid, SMC_GID_SIZE);
731 memcpy(link->peer_mac, ini->peer_mac, sizeof(link->peer_mac));
732 link->peer_psn = ntoh24(clc->r0.psn);
733 link->peer_mtu = clc->r0.qp_mtu;
734 }
735
smc_stat_inc_fback_rsn_cnt(struct smc_sock * smc,struct smc_stats_fback * fback_arr)736 static void smc_stat_inc_fback_rsn_cnt(struct smc_sock *smc,
737 struct smc_stats_fback *fback_arr)
738 {
739 int cnt;
740
741 for (cnt = 0; cnt < SMC_MAX_FBACK_RSN_CNT; cnt++) {
742 if (fback_arr[cnt].fback_code == smc->fallback_rsn) {
743 fback_arr[cnt].count++;
744 break;
745 }
746 if (!fback_arr[cnt].fback_code) {
747 fback_arr[cnt].fback_code = smc->fallback_rsn;
748 fback_arr[cnt].count++;
749 break;
750 }
751 }
752 }
753
smc_stat_fallback(struct smc_sock * smc)754 static void smc_stat_fallback(struct smc_sock *smc)
755 {
756 struct net *net = sock_net(&smc->sk);
757
758 mutex_lock(&net->smc.mutex_fback_rsn);
759 if (smc->listen_smc) {
760 smc_stat_inc_fback_rsn_cnt(smc, net->smc.fback_rsn->srv);
761 net->smc.fback_rsn->srv_fback_cnt++;
762 } else {
763 smc_stat_inc_fback_rsn_cnt(smc, net->smc.fback_rsn->clnt);
764 net->smc.fback_rsn->clnt_fback_cnt++;
765 }
766 mutex_unlock(&net->smc.mutex_fback_rsn);
767 }
768
769 /* must be called under rcu read lock */
smc_fback_wakeup_waitqueue(struct smc_sock * smc,void * key)770 static void smc_fback_wakeup_waitqueue(struct smc_sock *smc, void *key)
771 {
772 struct socket_wq *wq;
773 __poll_t flags;
774
775 wq = rcu_dereference(smc->sk.sk_wq);
776 if (!skwq_has_sleeper(wq))
777 return;
778
779 /* wake up smc sk->sk_wq */
780 if (!key) {
781 /* sk_state_change */
782 wake_up_interruptible_all(&wq->wait);
783 } else {
784 flags = key_to_poll(key);
785 if (flags & (EPOLLIN | EPOLLOUT))
786 /* sk_data_ready or sk_write_space */
787 wake_up_interruptible_sync_poll(&wq->wait, flags);
788 else if (flags & EPOLLERR)
789 /* sk_error_report */
790 wake_up_interruptible_poll(&wq->wait, flags);
791 }
792 }
793
smc_fback_mark_woken(wait_queue_entry_t * wait,unsigned int mode,int sync,void * key)794 static int smc_fback_mark_woken(wait_queue_entry_t *wait,
795 unsigned int mode, int sync, void *key)
796 {
797 struct smc_mark_woken *mark =
798 container_of(wait, struct smc_mark_woken, wait_entry);
799
800 mark->woken = true;
801 mark->key = key;
802 return 0;
803 }
804
smc_fback_forward_wakeup(struct smc_sock * smc,struct sock * clcsk,void (* clcsock_callback)(struct sock * sk))805 static void smc_fback_forward_wakeup(struct smc_sock *smc, struct sock *clcsk,
806 void (*clcsock_callback)(struct sock *sk))
807 {
808 struct smc_mark_woken mark = { .woken = false };
809 struct socket_wq *wq;
810
811 init_waitqueue_func_entry(&mark.wait_entry,
812 smc_fback_mark_woken);
813 rcu_read_lock();
814 wq = rcu_dereference(clcsk->sk_wq);
815 if (!wq)
816 goto out;
817 add_wait_queue(sk_sleep(clcsk), &mark.wait_entry);
818 clcsock_callback(clcsk);
819 remove_wait_queue(sk_sleep(clcsk), &mark.wait_entry);
820
821 if (mark.woken)
822 smc_fback_wakeup_waitqueue(smc, mark.key);
823 out:
824 rcu_read_unlock();
825 }
826
smc_fback_state_change(struct sock * clcsk)827 static void smc_fback_state_change(struct sock *clcsk)
828 {
829 struct smc_sock *smc;
830
831 read_lock_bh(&clcsk->sk_callback_lock);
832 smc = smc_clcsock_user_data(clcsk);
833 if (smc)
834 smc_fback_forward_wakeup(smc, clcsk,
835 smc->clcsk_state_change);
836 read_unlock_bh(&clcsk->sk_callback_lock);
837 }
838
smc_fback_data_ready(struct sock * clcsk)839 static void smc_fback_data_ready(struct sock *clcsk)
840 {
841 struct smc_sock *smc;
842
843 read_lock_bh(&clcsk->sk_callback_lock);
844 smc = smc_clcsock_user_data(clcsk);
845 if (smc)
846 smc_fback_forward_wakeup(smc, clcsk,
847 smc->clcsk_data_ready);
848 read_unlock_bh(&clcsk->sk_callback_lock);
849 }
850
smc_fback_write_space(struct sock * clcsk)851 static void smc_fback_write_space(struct sock *clcsk)
852 {
853 struct smc_sock *smc;
854
855 read_lock_bh(&clcsk->sk_callback_lock);
856 smc = smc_clcsock_user_data(clcsk);
857 if (smc)
858 smc_fback_forward_wakeup(smc, clcsk,
859 smc->clcsk_write_space);
860 read_unlock_bh(&clcsk->sk_callback_lock);
861 }
862
smc_fback_error_report(struct sock * clcsk)863 static void smc_fback_error_report(struct sock *clcsk)
864 {
865 struct smc_sock *smc;
866
867 read_lock_bh(&clcsk->sk_callback_lock);
868 smc = smc_clcsock_user_data(clcsk);
869 if (smc)
870 smc_fback_forward_wakeup(smc, clcsk,
871 smc->clcsk_error_report);
872 read_unlock_bh(&clcsk->sk_callback_lock);
873 }
874
smc_fback_replace_callbacks(struct smc_sock * smc)875 static void smc_fback_replace_callbacks(struct smc_sock *smc)
876 {
877 struct sock *clcsk = smc->clcsock->sk;
878
879 write_lock_bh(&clcsk->sk_callback_lock);
880 clcsk->sk_user_data = (void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY);
881
882 smc_clcsock_replace_cb(&clcsk->sk_state_change, smc_fback_state_change,
883 &smc->clcsk_state_change);
884 smc_clcsock_replace_cb(&clcsk->sk_data_ready, smc_fback_data_ready,
885 &smc->clcsk_data_ready);
886 smc_clcsock_replace_cb(&clcsk->sk_write_space, smc_fback_write_space,
887 &smc->clcsk_write_space);
888 smc_clcsock_replace_cb(&clcsk->sk_error_report, smc_fback_error_report,
889 &smc->clcsk_error_report);
890
891 write_unlock_bh(&clcsk->sk_callback_lock);
892 }
893
smc_switch_to_fallback(struct smc_sock * smc,int reason_code)894 static int smc_switch_to_fallback(struct smc_sock *smc, int reason_code)
895 {
896 int rc = 0;
897
898 mutex_lock(&smc->clcsock_release_lock);
899 if (!smc->clcsock) {
900 rc = -EBADF;
901 goto out;
902 }
903
904 smc->use_fallback = true;
905 smc->fallback_rsn = reason_code;
906 smc_stat_fallback(smc);
907 trace_smc_switch_to_fallback(smc, reason_code);
908 if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
909 smc->clcsock->file = smc->sk.sk_socket->file;
910 smc->clcsock->file->private_data = smc->clcsock;
911 smc->clcsock->wq.fasync_list =
912 smc->sk.sk_socket->wq.fasync_list;
913
914 /* There might be some wait entries remaining
915 * in smc sk->sk_wq and they should be woken up
916 * as clcsock's wait queue is woken up.
917 */
918 smc_fback_replace_callbacks(smc);
919 }
920 out:
921 mutex_unlock(&smc->clcsock_release_lock);
922 return rc;
923 }
924
925 /* fall back during connect */
smc_connect_fallback(struct smc_sock * smc,int reason_code)926 static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
927 {
928 struct net *net = sock_net(&smc->sk);
929 int rc = 0;
930
931 rc = smc_switch_to_fallback(smc, reason_code);
932 if (rc) { /* fallback fails */
933 this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
934 if (smc->sk.sk_state == SMC_INIT)
935 sock_put(&smc->sk); /* passive closing */
936 return rc;
937 }
938 smc_copy_sock_settings_to_clc(smc);
939 smc->connect_nonblock = 0;
940 if (smc->sk.sk_state == SMC_INIT)
941 smc->sk.sk_state = SMC_ACTIVE;
942 return 0;
943 }
944
945 /* decline and fall back during connect */
smc_connect_decline_fallback(struct smc_sock * smc,int reason_code,u8 version)946 static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code,
947 u8 version)
948 {
949 struct net *net = sock_net(&smc->sk);
950 int rc;
951
952 if (reason_code < 0) { /* error, fallback is not possible */
953 this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
954 if (smc->sk.sk_state == SMC_INIT)
955 sock_put(&smc->sk); /* passive closing */
956 return reason_code;
957 }
958 if (reason_code != SMC_CLC_DECL_PEERDECL) {
959 rc = smc_clc_send_decline(smc, reason_code, version);
960 if (rc < 0) {
961 this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
962 if (smc->sk.sk_state == SMC_INIT)
963 sock_put(&smc->sk); /* passive closing */
964 return rc;
965 }
966 }
967 return smc_connect_fallback(smc, reason_code);
968 }
969
smc_conn_abort(struct smc_sock * smc,int local_first)970 static void smc_conn_abort(struct smc_sock *smc, int local_first)
971 {
972 struct smc_connection *conn = &smc->conn;
973 struct smc_link_group *lgr = conn->lgr;
974 bool lgr_valid = false;
975
976 if (smc_conn_lgr_valid(conn))
977 lgr_valid = true;
978
979 smc_conn_free(conn);
980 if (local_first && lgr_valid)
981 smc_lgr_cleanup_early(lgr);
982 }
983
984 /* check if there is a rdma device available for this connection. */
985 /* called for connect and listen */
smc_find_rdma_device(struct smc_sock * smc,struct smc_init_info * ini)986 static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
987 {
988 /* PNET table look up: search active ib_device and port
989 * within same PNETID that also contains the ethernet device
990 * used for the internal TCP socket
991 */
992 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
993 if (!ini->check_smcrv2 && !ini->ib_dev)
994 return SMC_CLC_DECL_NOSMCRDEV;
995 if (ini->check_smcrv2 && !ini->smcrv2.ib_dev_v2)
996 return SMC_CLC_DECL_NOSMCRDEV;
997 return 0;
998 }
999
1000 /* check if there is an ISM device available for this connection. */
1001 /* called for connect and listen */
smc_find_ism_device(struct smc_sock * smc,struct smc_init_info * ini)1002 static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
1003 {
1004 /* Find ISM device with same PNETID as connecting interface */
1005 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
1006 if (!ini->ism_dev[0])
1007 return SMC_CLC_DECL_NOSMCDDEV;
1008 else
1009 ini->ism_chid[0] = smc_ism_get_chid(ini->ism_dev[0]);
1010 return 0;
1011 }
1012
1013 /* 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)1014 static bool smc_find_ism_v2_is_unique_chid(u16 chid, struct smc_init_info *ini,
1015 int cnt)
1016 {
1017 int i = (!ini->ism_dev[0]) ? 1 : 0;
1018
1019 for (; i < cnt; i++)
1020 if (ini->ism_chid[i] == chid)
1021 return false;
1022 return true;
1023 }
1024
1025 /* determine possible V2 ISM devices (either without PNETID or with PNETID plus
1026 * PNETID matching net_device)
1027 */
smc_find_ism_v2_device_clnt(struct smc_sock * smc,struct smc_init_info * ini)1028 static int smc_find_ism_v2_device_clnt(struct smc_sock *smc,
1029 struct smc_init_info *ini)
1030 {
1031 int rc = SMC_CLC_DECL_NOSMCDDEV;
1032 struct smcd_dev *smcd;
1033 int i = 1;
1034 u16 chid;
1035
1036 if (smcd_indicated(ini->smc_type_v1))
1037 rc = 0; /* already initialized for V1 */
1038 mutex_lock(&smcd_dev_list.mutex);
1039 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
1040 if (smcd->going_away || smcd == ini->ism_dev[0])
1041 continue;
1042 chid = smc_ism_get_chid(smcd);
1043 if (!smc_find_ism_v2_is_unique_chid(chid, ini, i))
1044 continue;
1045 if (!smc_pnet_is_pnetid_set(smcd->pnetid) ||
1046 smc_pnet_is_ndev_pnetid(sock_net(&smc->sk), smcd->pnetid)) {
1047 ini->ism_dev[i] = smcd;
1048 ini->ism_chid[i] = chid;
1049 ini->is_smcd = true;
1050 rc = 0;
1051 i++;
1052 if (i > SMC_MAX_ISM_DEVS)
1053 break;
1054 }
1055 }
1056 mutex_unlock(&smcd_dev_list.mutex);
1057 ini->ism_offered_cnt = i - 1;
1058 if (!ini->ism_dev[0] && !ini->ism_dev[1])
1059 ini->smcd_version = 0;
1060
1061 return rc;
1062 }
1063
1064 /* 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)1065 static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
1066 struct smc_init_info *ini)
1067 {
1068 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev[0], ini->vlan_id))
1069 return SMC_CLC_DECL_ISMVLANERR;
1070 return 0;
1071 }
1072
smc_find_proposal_devices(struct smc_sock * smc,struct smc_init_info * ini)1073 static int smc_find_proposal_devices(struct smc_sock *smc,
1074 struct smc_init_info *ini)
1075 {
1076 int rc = 0;
1077
1078 /* check if there is an ism device available */
1079 if (!(ini->smcd_version & SMC_V1) ||
1080 smc_find_ism_device(smc, ini) ||
1081 smc_connect_ism_vlan_setup(smc, ini))
1082 ini->smcd_version &= ~SMC_V1;
1083 /* else ISM V1 is supported for this connection */
1084
1085 /* check if there is an rdma device available */
1086 if (!(ini->smcr_version & SMC_V1) ||
1087 smc_find_rdma_device(smc, ini))
1088 ini->smcr_version &= ~SMC_V1;
1089 /* else RDMA is supported for this connection */
1090
1091 ini->smc_type_v1 = smc_indicated_type(ini->smcd_version & SMC_V1,
1092 ini->smcr_version & SMC_V1);
1093
1094 /* check if there is an ism v2 device available */
1095 if (!(ini->smcd_version & SMC_V2) ||
1096 !smc_ism_is_v2_capable() ||
1097 smc_find_ism_v2_device_clnt(smc, ini))
1098 ini->smcd_version &= ~SMC_V2;
1099
1100 /* check if there is an rdma v2 device available */
1101 ini->check_smcrv2 = true;
1102 ini->smcrv2.saddr = smc->clcsock->sk->sk_rcv_saddr;
1103 if (!(ini->smcr_version & SMC_V2) ||
1104 smc->clcsock->sk->sk_family != AF_INET ||
1105 !smc_clc_ueid_count() ||
1106 smc_find_rdma_device(smc, ini))
1107 ini->smcr_version &= ~SMC_V2;
1108 ini->check_smcrv2 = false;
1109
1110 ini->smc_type_v2 = smc_indicated_type(ini->smcd_version & SMC_V2,
1111 ini->smcr_version & SMC_V2);
1112
1113 /* if neither ISM nor RDMA are supported, fallback */
1114 if (ini->smc_type_v1 == SMC_TYPE_N && ini->smc_type_v2 == SMC_TYPE_N)
1115 rc = SMC_CLC_DECL_NOSMCDEV;
1116
1117 return rc;
1118 }
1119
1120 /* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
1121 * used, the VLAN ID will be registered again during the connection setup.
1122 */
smc_connect_ism_vlan_cleanup(struct smc_sock * smc,struct smc_init_info * ini)1123 static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc,
1124 struct smc_init_info *ini)
1125 {
1126 if (!smcd_indicated(ini->smc_type_v1))
1127 return 0;
1128 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev[0], ini->vlan_id))
1129 return SMC_CLC_DECL_CNFERR;
1130 return 0;
1131 }
1132
1133 #define SMC_CLC_MAX_ACCEPT_LEN \
1134 (sizeof(struct smc_clc_msg_accept_confirm_v2) + \
1135 sizeof(struct smc_clc_first_contact_ext_v2x) + \
1136 sizeof(struct smc_clc_msg_trail))
1137
1138 /* CLC handshake during connect */
smc_connect_clc(struct smc_sock * smc,struct smc_clc_msg_accept_confirm_v2 * aclc2,struct smc_init_info * ini)1139 static int smc_connect_clc(struct smc_sock *smc,
1140 struct smc_clc_msg_accept_confirm_v2 *aclc2,
1141 struct smc_init_info *ini)
1142 {
1143 int rc = 0;
1144
1145 /* do inband token exchange */
1146 rc = smc_clc_send_proposal(smc, ini);
1147 if (rc)
1148 return rc;
1149 /* receive SMC Accept CLC message */
1150 return smc_clc_wait_msg(smc, aclc2, SMC_CLC_MAX_ACCEPT_LEN,
1151 SMC_CLC_ACCEPT, CLC_WAIT_TIME);
1152 }
1153
smc_fill_gid_list(struct smc_link_group * lgr,struct smc_gidlist * gidlist,struct smc_ib_device * known_dev,u8 * known_gid)1154 void smc_fill_gid_list(struct smc_link_group *lgr,
1155 struct smc_gidlist *gidlist,
1156 struct smc_ib_device *known_dev, u8 *known_gid)
1157 {
1158 struct smc_init_info *alt_ini = NULL;
1159
1160 memset(gidlist, 0, sizeof(*gidlist));
1161 memcpy(gidlist->list[gidlist->len++], known_gid, SMC_GID_SIZE);
1162
1163 alt_ini = kzalloc(sizeof(*alt_ini), GFP_KERNEL);
1164 if (!alt_ini)
1165 goto out;
1166
1167 alt_ini->vlan_id = lgr->vlan_id;
1168 alt_ini->check_smcrv2 = true;
1169 alt_ini->smcrv2.saddr = lgr->saddr;
1170 smc_pnet_find_alt_roce(lgr, alt_ini, known_dev);
1171
1172 if (!alt_ini->smcrv2.ib_dev_v2)
1173 goto out;
1174
1175 memcpy(gidlist->list[gidlist->len++], alt_ini->smcrv2.ib_gid_v2,
1176 SMC_GID_SIZE);
1177
1178 out:
1179 kfree(alt_ini);
1180 }
1181
smc_connect_rdma_v2_prepare(struct smc_sock * smc,struct smc_clc_msg_accept_confirm * aclc,struct smc_init_info * ini)1182 static int smc_connect_rdma_v2_prepare(struct smc_sock *smc,
1183 struct smc_clc_msg_accept_confirm *aclc,
1184 struct smc_init_info *ini)
1185 {
1186 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
1187 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
1188 struct smc_clc_first_contact_ext *fce =
1189 smc_get_clc_first_contact_ext(clc_v2, false);
1190 struct net *net = sock_net(&smc->sk);
1191 int rc;
1192
1193 if (!ini->first_contact_peer || aclc->hdr.version == SMC_V1)
1194 return 0;
1195
1196 if (fce->v2_direct) {
1197 memcpy(ini->smcrv2.nexthop_mac, &aclc->r0.lcl.mac, ETH_ALEN);
1198 ini->smcrv2.uses_gateway = false;
1199 } else {
1200 if (smc_ib_find_route(net, smc->clcsock->sk->sk_rcv_saddr,
1201 smc_ib_gid_to_ipv4(aclc->r0.lcl.gid),
1202 ini->smcrv2.nexthop_mac,
1203 &ini->smcrv2.uses_gateway))
1204 return SMC_CLC_DECL_NOROUTE;
1205 if (!ini->smcrv2.uses_gateway) {
1206 /* mismatch: peer claims indirect, but its direct */
1207 return SMC_CLC_DECL_NOINDIRECT;
1208 }
1209 }
1210
1211 ini->release_nr = fce->release;
1212 rc = smc_clc_clnt_v2x_features_validate(fce, ini);
1213 if (rc)
1214 return rc;
1215
1216 return 0;
1217 }
1218
1219 /* 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)1220 static int smc_connect_rdma(struct smc_sock *smc,
1221 struct smc_clc_msg_accept_confirm *aclc,
1222 struct smc_init_info *ini)
1223 {
1224 int i, reason_code = 0;
1225 struct smc_link *link;
1226 u8 *eid = NULL;
1227
1228 ini->is_smcd = false;
1229 ini->ib_clcqpn = ntoh24(aclc->r0.qpn);
1230 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
1231 memcpy(ini->peer_systemid, aclc->r0.lcl.id_for_peer, SMC_SYSTEMID_LEN);
1232 memcpy(ini->peer_gid, aclc->r0.lcl.gid, SMC_GID_SIZE);
1233 memcpy(ini->peer_mac, aclc->r0.lcl.mac, ETH_ALEN);
1234 ini->max_conns = SMC_CONN_PER_LGR_MAX;
1235 ini->max_links = SMC_LINKS_ADD_LNK_MAX;
1236
1237 reason_code = smc_connect_rdma_v2_prepare(smc, aclc, ini);
1238 if (reason_code)
1239 return reason_code;
1240
1241 mutex_lock(&smc_client_lgr_pending);
1242 reason_code = smc_conn_create(smc, ini);
1243 if (reason_code) {
1244 mutex_unlock(&smc_client_lgr_pending);
1245 return reason_code;
1246 }
1247
1248 smc_conn_save_peer_info(smc, aclc);
1249
1250 if (ini->first_contact_local) {
1251 link = smc->conn.lnk;
1252 } else {
1253 /* set link that was assigned by server */
1254 link = NULL;
1255 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1256 struct smc_link *l = &smc->conn.lgr->lnk[i];
1257
1258 if (l->peer_qpn == ntoh24(aclc->r0.qpn) &&
1259 !memcmp(l->peer_gid, &aclc->r0.lcl.gid,
1260 SMC_GID_SIZE) &&
1261 (aclc->hdr.version > SMC_V1 ||
1262 !memcmp(l->peer_mac, &aclc->r0.lcl.mac,
1263 sizeof(l->peer_mac)))) {
1264 link = l;
1265 break;
1266 }
1267 }
1268 if (!link) {
1269 reason_code = SMC_CLC_DECL_NOSRVLINK;
1270 goto connect_abort;
1271 }
1272 smc_switch_link_and_count(&smc->conn, link);
1273 }
1274
1275 /* create send buffer and rmb */
1276 if (smc_buf_create(smc, false)) {
1277 reason_code = SMC_CLC_DECL_MEM;
1278 goto connect_abort;
1279 }
1280
1281 if (ini->first_contact_local)
1282 smc_link_save_peer_info(link, aclc, ini);
1283
1284 if (smc_rmb_rtoken_handling(&smc->conn, link, aclc)) {
1285 reason_code = SMC_CLC_DECL_ERR_RTOK;
1286 goto connect_abort;
1287 }
1288
1289 smc_close_init(smc);
1290 smc_rx_init(smc);
1291
1292 if (ini->first_contact_local) {
1293 if (smc_ib_ready_link(link)) {
1294 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
1295 goto connect_abort;
1296 }
1297 } else {
1298 /* reg sendbufs if they were vzalloced */
1299 if (smc->conn.sndbuf_desc->is_vm) {
1300 if (smcr_lgr_reg_sndbufs(link, smc->conn.sndbuf_desc)) {
1301 reason_code = SMC_CLC_DECL_ERR_REGBUF;
1302 goto connect_abort;
1303 }
1304 }
1305 if (smcr_lgr_reg_rmbs(link, smc->conn.rmb_desc)) {
1306 reason_code = SMC_CLC_DECL_ERR_REGBUF;
1307 goto connect_abort;
1308 }
1309 }
1310
1311 if (aclc->hdr.version > SMC_V1) {
1312 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
1313 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
1314
1315 eid = clc_v2->r1.eid;
1316 if (ini->first_contact_local)
1317 smc_fill_gid_list(link->lgr, &ini->smcrv2.gidlist,
1318 link->smcibdev, link->gid);
1319 }
1320
1321 reason_code = smc_clc_send_confirm(smc, ini->first_contact_local,
1322 aclc->hdr.version, eid, ini);
1323 if (reason_code)
1324 goto connect_abort;
1325
1326 smc_tx_init(smc);
1327
1328 if (ini->first_contact_local) {
1329 /* QP confirmation over RoCE fabric */
1330 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
1331 reason_code = smcr_clnt_conf_first_link(smc);
1332 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
1333 if (reason_code)
1334 goto connect_abort;
1335 }
1336 mutex_unlock(&smc_client_lgr_pending);
1337
1338 smc_copy_sock_settings_to_clc(smc);
1339 smc->connect_nonblock = 0;
1340 if (smc->sk.sk_state == SMC_INIT)
1341 smc->sk.sk_state = SMC_ACTIVE;
1342
1343 return 0;
1344 connect_abort:
1345 smc_conn_abort(smc, ini->first_contact_local);
1346 mutex_unlock(&smc_client_lgr_pending);
1347 smc->connect_nonblock = 0;
1348
1349 return reason_code;
1350 }
1351
1352 /* The server has chosen one of the proposed ISM devices for the communication.
1353 * Determine from the CHID of the received CLC ACCEPT the ISM device chosen.
1354 */
1355 static int
smc_v2_determine_accepted_chid(struct smc_clc_msg_accept_confirm_v2 * aclc,struct smc_init_info * ini)1356 smc_v2_determine_accepted_chid(struct smc_clc_msg_accept_confirm_v2 *aclc,
1357 struct smc_init_info *ini)
1358 {
1359 int i;
1360
1361 for (i = 0; i < ini->ism_offered_cnt + 1; i++) {
1362 if (ini->ism_chid[i] == ntohs(aclc->d1.chid)) {
1363 ini->ism_selected = i;
1364 return 0;
1365 }
1366 }
1367
1368 return -EPROTO;
1369 }
1370
1371 /* 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)1372 static int smc_connect_ism(struct smc_sock *smc,
1373 struct smc_clc_msg_accept_confirm *aclc,
1374 struct smc_init_info *ini)
1375 {
1376 u8 *eid = NULL;
1377 int rc = 0;
1378
1379 ini->is_smcd = true;
1380 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
1381
1382 if (aclc->hdr.version == SMC_V2) {
1383 struct smc_clc_msg_accept_confirm_v2 *aclc_v2 =
1384 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
1385
1386 if (ini->first_contact_peer) {
1387 struct smc_clc_first_contact_ext *fce =
1388 smc_get_clc_first_contact_ext(aclc_v2, true);
1389
1390 ini->release_nr = fce->release;
1391 rc = smc_clc_clnt_v2x_features_validate(fce, ini);
1392 if (rc)
1393 return rc;
1394 }
1395
1396 rc = smc_v2_determine_accepted_chid(aclc_v2, ini);
1397 if (rc)
1398 return rc;
1399 }
1400 ini->ism_peer_gid[ini->ism_selected] = ntohll(aclc->d0.gid);
1401
1402 /* there is only one lgr role for SMC-D; use server lock */
1403 mutex_lock(&smc_server_lgr_pending);
1404 rc = smc_conn_create(smc, ini);
1405 if (rc) {
1406 mutex_unlock(&smc_server_lgr_pending);
1407 return rc;
1408 }
1409
1410 /* Create send and receive buffers */
1411 rc = smc_buf_create(smc, true);
1412 if (rc) {
1413 rc = (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB : SMC_CLC_DECL_MEM;
1414 goto connect_abort;
1415 }
1416
1417 smc_conn_save_peer_info(smc, aclc);
1418 smc_close_init(smc);
1419 smc_rx_init(smc);
1420 smc_tx_init(smc);
1421
1422 if (aclc->hdr.version > SMC_V1) {
1423 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
1424 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
1425
1426 eid = clc_v2->d1.eid;
1427 }
1428
1429 rc = smc_clc_send_confirm(smc, ini->first_contact_local,
1430 aclc->hdr.version, eid, ini);
1431 if (rc)
1432 goto connect_abort;
1433 mutex_unlock(&smc_server_lgr_pending);
1434
1435 smc_copy_sock_settings_to_clc(smc);
1436 smc->connect_nonblock = 0;
1437 if (smc->sk.sk_state == SMC_INIT)
1438 smc->sk.sk_state = SMC_ACTIVE;
1439
1440 return 0;
1441 connect_abort:
1442 smc_conn_abort(smc, ini->first_contact_local);
1443 mutex_unlock(&smc_server_lgr_pending);
1444 smc->connect_nonblock = 0;
1445
1446 return rc;
1447 }
1448
1449 /* 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)1450 static int smc_connect_check_aclc(struct smc_init_info *ini,
1451 struct smc_clc_msg_accept_confirm *aclc)
1452 {
1453 if (aclc->hdr.typev1 != SMC_TYPE_R &&
1454 aclc->hdr.typev1 != SMC_TYPE_D)
1455 return SMC_CLC_DECL_MODEUNSUPP;
1456
1457 if (aclc->hdr.version >= SMC_V2) {
1458 if ((aclc->hdr.typev1 == SMC_TYPE_R &&
1459 !smcr_indicated(ini->smc_type_v2)) ||
1460 (aclc->hdr.typev1 == SMC_TYPE_D &&
1461 !smcd_indicated(ini->smc_type_v2)))
1462 return SMC_CLC_DECL_MODEUNSUPP;
1463 } else {
1464 if ((aclc->hdr.typev1 == SMC_TYPE_R &&
1465 !smcr_indicated(ini->smc_type_v1)) ||
1466 (aclc->hdr.typev1 == SMC_TYPE_D &&
1467 !smcd_indicated(ini->smc_type_v1)))
1468 return SMC_CLC_DECL_MODEUNSUPP;
1469 }
1470
1471 return 0;
1472 }
1473
1474 /* perform steps before actually connecting */
__smc_connect(struct smc_sock * smc)1475 static int __smc_connect(struct smc_sock *smc)
1476 {
1477 u8 version = smc_ism_is_v2_capable() ? SMC_V2 : SMC_V1;
1478 struct smc_clc_msg_accept_confirm_v2 *aclc2;
1479 struct smc_clc_msg_accept_confirm *aclc;
1480 struct smc_init_info *ini = NULL;
1481 u8 *buf = NULL;
1482 int rc = 0;
1483
1484 if (smc->use_fallback)
1485 return smc_connect_fallback(smc, smc->fallback_rsn);
1486
1487 /* if peer has not signalled SMC-capability, fall back */
1488 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
1489 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
1490
1491 /* IPSec connections opt out of SMC optimizations */
1492 if (using_ipsec(smc))
1493 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC,
1494 version);
1495
1496 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
1497 if (!ini)
1498 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_MEM,
1499 version);
1500
1501 ini->smcd_version = SMC_V1 | SMC_V2;
1502 ini->smcr_version = SMC_V1 | SMC_V2;
1503 ini->smc_type_v1 = SMC_TYPE_B;
1504 ini->smc_type_v2 = SMC_TYPE_B;
1505
1506 /* get vlan id from IP device */
1507 if (smc_vlan_by_tcpsk(smc->clcsock, ini)) {
1508 ini->smcd_version &= ~SMC_V1;
1509 ini->smcr_version = 0;
1510 ini->smc_type_v1 = SMC_TYPE_N;
1511 if (!ini->smcd_version) {
1512 rc = SMC_CLC_DECL_GETVLANERR;
1513 goto fallback;
1514 }
1515 }
1516
1517 rc = smc_find_proposal_devices(smc, ini);
1518 if (rc)
1519 goto fallback;
1520
1521 buf = kzalloc(SMC_CLC_MAX_ACCEPT_LEN, GFP_KERNEL);
1522 if (!buf) {
1523 rc = SMC_CLC_DECL_MEM;
1524 goto fallback;
1525 }
1526 aclc2 = (struct smc_clc_msg_accept_confirm_v2 *)buf;
1527 aclc = (struct smc_clc_msg_accept_confirm *)aclc2;
1528
1529 /* perform CLC handshake */
1530 rc = smc_connect_clc(smc, aclc2, ini);
1531 if (rc) {
1532 /* -EAGAIN on timeout, see tcp_recvmsg() */
1533 if (rc == -EAGAIN) {
1534 rc = -ETIMEDOUT;
1535 smc->sk.sk_err = ETIMEDOUT;
1536 }
1537 goto vlan_cleanup;
1538 }
1539
1540 /* check if smc modes and versions of CLC proposal and accept match */
1541 rc = smc_connect_check_aclc(ini, aclc);
1542 version = aclc->hdr.version == SMC_V1 ? SMC_V1 : SMC_V2;
1543 if (rc)
1544 goto vlan_cleanup;
1545
1546 /* depending on previous steps, connect using rdma or ism */
1547 if (aclc->hdr.typev1 == SMC_TYPE_R) {
1548 ini->smcr_version = version;
1549 rc = smc_connect_rdma(smc, aclc, ini);
1550 } else if (aclc->hdr.typev1 == SMC_TYPE_D) {
1551 ini->smcd_version = version;
1552 rc = smc_connect_ism(smc, aclc, ini);
1553 }
1554 if (rc)
1555 goto vlan_cleanup;
1556
1557 SMC_STAT_CLNT_SUCC_INC(sock_net(smc->clcsock->sk), aclc);
1558 smc_connect_ism_vlan_cleanup(smc, ini);
1559 kfree(buf);
1560 kfree(ini);
1561 return 0;
1562
1563 vlan_cleanup:
1564 smc_connect_ism_vlan_cleanup(smc, ini);
1565 kfree(buf);
1566 fallback:
1567 kfree(ini);
1568 return smc_connect_decline_fallback(smc, rc, version);
1569 }
1570
smc_connect_work(struct work_struct * work)1571 static void smc_connect_work(struct work_struct *work)
1572 {
1573 struct smc_sock *smc = container_of(work, struct smc_sock,
1574 connect_work);
1575 long timeo = smc->sk.sk_sndtimeo;
1576 int rc = 0;
1577
1578 if (!timeo)
1579 timeo = MAX_SCHEDULE_TIMEOUT;
1580 lock_sock(smc->clcsock->sk);
1581 if (smc->clcsock->sk->sk_err) {
1582 smc->sk.sk_err = smc->clcsock->sk->sk_err;
1583 } else if ((1 << smc->clcsock->sk->sk_state) &
1584 (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
1585 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
1586 if ((rc == -EPIPE) &&
1587 ((1 << smc->clcsock->sk->sk_state) &
1588 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
1589 rc = 0;
1590 }
1591 release_sock(smc->clcsock->sk);
1592 lock_sock(&smc->sk);
1593 if (rc != 0 || smc->sk.sk_err) {
1594 smc->sk.sk_state = SMC_CLOSED;
1595 if (rc == -EPIPE || rc == -EAGAIN)
1596 smc->sk.sk_err = EPIPE;
1597 else if (rc == -ECONNREFUSED)
1598 smc->sk.sk_err = ECONNREFUSED;
1599 else if (signal_pending(current))
1600 smc->sk.sk_err = -sock_intr_errno(timeo);
1601 sock_put(&smc->sk); /* passive closing */
1602 goto out;
1603 }
1604
1605 rc = __smc_connect(smc);
1606 if (rc < 0)
1607 smc->sk.sk_err = -rc;
1608
1609 out:
1610 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
1611 if (smc->sk.sk_err) {
1612 smc->sk.sk_state_change(&smc->sk);
1613 } else { /* allow polling before and after fallback decision */
1614 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
1615 smc->sk.sk_write_space(&smc->sk);
1616 }
1617 }
1618 release_sock(&smc->sk);
1619 }
1620
smc_connect(struct socket * sock,struct sockaddr * addr,int alen,int flags)1621 static int smc_connect(struct socket *sock, struct sockaddr *addr,
1622 int alen, int flags)
1623 {
1624 struct sock *sk = sock->sk;
1625 struct smc_sock *smc;
1626 int rc = -EINVAL;
1627
1628 smc = smc_sk(sk);
1629
1630 /* separate smc parameter checking to be safe */
1631 if (alen < sizeof(addr->sa_family))
1632 goto out_err;
1633 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
1634 goto out_err;
1635
1636 lock_sock(sk);
1637 switch (sock->state) {
1638 default:
1639 rc = -EINVAL;
1640 goto out;
1641 case SS_CONNECTED:
1642 rc = sk->sk_state == SMC_ACTIVE ? -EISCONN : -EINVAL;
1643 goto out;
1644 case SS_CONNECTING:
1645 if (sk->sk_state == SMC_ACTIVE)
1646 goto connected;
1647 break;
1648 case SS_UNCONNECTED:
1649 sock->state = SS_CONNECTING;
1650 break;
1651 }
1652
1653 switch (sk->sk_state) {
1654 default:
1655 goto out;
1656 case SMC_CLOSED:
1657 rc = sock_error(sk) ? : -ECONNABORTED;
1658 sock->state = SS_UNCONNECTED;
1659 goto out;
1660 case SMC_ACTIVE:
1661 rc = -EISCONN;
1662 goto out;
1663 case SMC_INIT:
1664 break;
1665 }
1666
1667 smc_copy_sock_settings_to_clc(smc);
1668 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
1669 if (smc->connect_nonblock) {
1670 rc = -EALREADY;
1671 goto out;
1672 }
1673 rc = kernel_connect(smc->clcsock, addr, alen, flags);
1674 if (rc && rc != -EINPROGRESS)
1675 goto out;
1676
1677 if (smc->use_fallback) {
1678 sock->state = rc ? SS_CONNECTING : SS_CONNECTED;
1679 goto out;
1680 }
1681 sock_hold(&smc->sk); /* sock put in passive closing */
1682 if (flags & O_NONBLOCK) {
1683 if (queue_work(smc_hs_wq, &smc->connect_work))
1684 smc->connect_nonblock = 1;
1685 rc = -EINPROGRESS;
1686 goto out;
1687 } else {
1688 rc = __smc_connect(smc);
1689 if (rc < 0)
1690 goto out;
1691 }
1692
1693 connected:
1694 rc = 0;
1695 sock->state = SS_CONNECTED;
1696 out:
1697 release_sock(sk);
1698 out_err:
1699 return rc;
1700 }
1701
smc_clcsock_accept(struct smc_sock * lsmc,struct smc_sock ** new_smc)1702 static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
1703 {
1704 struct socket *new_clcsock = NULL;
1705 struct sock *lsk = &lsmc->sk;
1706 struct sock *new_sk;
1707 int rc = -EINVAL;
1708
1709 release_sock(lsk);
1710 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
1711 if (!new_sk) {
1712 rc = -ENOMEM;
1713 lsk->sk_err = ENOMEM;
1714 *new_smc = NULL;
1715 lock_sock(lsk);
1716 goto out;
1717 }
1718 *new_smc = smc_sk(new_sk);
1719
1720 mutex_lock(&lsmc->clcsock_release_lock);
1721 if (lsmc->clcsock)
1722 rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK);
1723 mutex_unlock(&lsmc->clcsock_release_lock);
1724 lock_sock(lsk);
1725 if (rc < 0 && rc != -EAGAIN)
1726 lsk->sk_err = -rc;
1727 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
1728 new_sk->sk_prot->unhash(new_sk);
1729 if (new_clcsock)
1730 sock_release(new_clcsock);
1731 new_sk->sk_state = SMC_CLOSED;
1732 smc_sock_set_flag(new_sk, SOCK_DEAD);
1733 sock_put(new_sk); /* final */
1734 *new_smc = NULL;
1735 goto out;
1736 }
1737
1738 /* new clcsock has inherited the smc listen-specific sk_data_ready
1739 * function; switch it back to the original sk_data_ready function
1740 */
1741 new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready;
1742
1743 /* if new clcsock has also inherited the fallback-specific callback
1744 * functions, switch them back to the original ones.
1745 */
1746 if (lsmc->use_fallback) {
1747 if (lsmc->clcsk_state_change)
1748 new_clcsock->sk->sk_state_change = lsmc->clcsk_state_change;
1749 if (lsmc->clcsk_write_space)
1750 new_clcsock->sk->sk_write_space = lsmc->clcsk_write_space;
1751 if (lsmc->clcsk_error_report)
1752 new_clcsock->sk->sk_error_report = lsmc->clcsk_error_report;
1753 }
1754
1755 (*new_smc)->clcsock = new_clcsock;
1756 out:
1757 return rc;
1758 }
1759
1760 /* add a just created sock to the accept queue of the listen sock as
1761 * candidate for a following socket accept call from user space
1762 */
smc_accept_enqueue(struct sock * parent,struct sock * sk)1763 static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
1764 {
1765 struct smc_sock *par = smc_sk(parent);
1766
1767 sock_hold(sk); /* sock_put in smc_accept_unlink () */
1768 spin_lock(&par->accept_q_lock);
1769 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
1770 spin_unlock(&par->accept_q_lock);
1771 sk_acceptq_added(parent);
1772 }
1773
1774 /* remove a socket from the accept queue of its parental listening socket */
smc_accept_unlink(struct sock * sk)1775 static void smc_accept_unlink(struct sock *sk)
1776 {
1777 struct smc_sock *par = smc_sk(sk)->listen_smc;
1778
1779 spin_lock(&par->accept_q_lock);
1780 list_del_init(&smc_sk(sk)->accept_q);
1781 spin_unlock(&par->accept_q_lock);
1782 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
1783 sock_put(sk); /* sock_hold in smc_accept_enqueue */
1784 }
1785
1786 /* remove a sock from the accept queue to bind it to a new socket created
1787 * for a socket accept call from user space
1788 */
smc_accept_dequeue(struct sock * parent,struct socket * new_sock)1789 struct sock *smc_accept_dequeue(struct sock *parent,
1790 struct socket *new_sock)
1791 {
1792 struct smc_sock *isk, *n;
1793 struct sock *new_sk;
1794
1795 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
1796 new_sk = (struct sock *)isk;
1797
1798 smc_accept_unlink(new_sk);
1799 if (new_sk->sk_state == SMC_CLOSED) {
1800 new_sk->sk_prot->unhash(new_sk);
1801 if (isk->clcsock) {
1802 sock_release(isk->clcsock);
1803 isk->clcsock = NULL;
1804 }
1805 sock_put(new_sk); /* final */
1806 continue;
1807 }
1808 if (new_sock) {
1809 sock_graft(new_sk, new_sock);
1810 new_sock->state = SS_CONNECTED;
1811 if (isk->use_fallback) {
1812 smc_sk(new_sk)->clcsock->file = new_sock->file;
1813 isk->clcsock->file->private_data = isk->clcsock;
1814 }
1815 }
1816 return new_sk;
1817 }
1818 return NULL;
1819 }
1820
1821 /* clean up for a created but never accepted sock */
smc_close_non_accepted(struct sock * sk)1822 void smc_close_non_accepted(struct sock *sk)
1823 {
1824 struct smc_sock *smc = smc_sk(sk);
1825
1826 sock_hold(sk); /* sock_put below */
1827 lock_sock(sk);
1828 if (!sk->sk_lingertime)
1829 /* wait for peer closing */
1830 WRITE_ONCE(sk->sk_lingertime, SMC_MAX_STREAM_WAIT_TIMEOUT);
1831 __smc_release(smc);
1832 release_sock(sk);
1833 sock_put(sk); /* sock_hold above */
1834 sock_put(sk); /* final sock_put */
1835 }
1836
smcr_serv_conf_first_link(struct smc_sock * smc)1837 static int smcr_serv_conf_first_link(struct smc_sock *smc)
1838 {
1839 struct smc_link *link = smc->conn.lnk;
1840 struct smc_llc_qentry *qentry;
1841 int rc;
1842
1843 /* reg the sndbuf if it was vzalloced*/
1844 if (smc->conn.sndbuf_desc->is_vm) {
1845 if (smcr_link_reg_buf(link, smc->conn.sndbuf_desc))
1846 return SMC_CLC_DECL_ERR_REGBUF;
1847 }
1848
1849 /* reg the rmb */
1850 if (smcr_link_reg_buf(link, smc->conn.rmb_desc))
1851 return SMC_CLC_DECL_ERR_REGBUF;
1852
1853 /* send CONFIRM LINK request to client over the RoCE fabric */
1854 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
1855 if (rc < 0)
1856 return SMC_CLC_DECL_TIMEOUT_CL;
1857
1858 /* receive CONFIRM LINK response from client over the RoCE fabric */
1859 qentry = smc_llc_wait(link->lgr, link, SMC_LLC_WAIT_TIME,
1860 SMC_LLC_CONFIRM_LINK);
1861 if (!qentry) {
1862 struct smc_clc_msg_decline dclc;
1863
1864 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
1865 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
1866 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
1867 }
1868 smc_llc_save_peer_uid(qentry);
1869 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_RESP);
1870 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
1871 if (rc)
1872 return SMC_CLC_DECL_RMBE_EC;
1873
1874 /* confirm_rkey is implicit on 1st contact */
1875 smc->conn.rmb_desc->is_conf_rkey = true;
1876
1877 smc_llc_link_active(link);
1878 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
1879
1880 if (link->lgr->max_links > 1) {
1881 down_write(&link->lgr->llc_conf_mutex);
1882 /* initial contact - try to establish second link */
1883 smc_llc_srv_add_link(link, NULL);
1884 up_write(&link->lgr->llc_conf_mutex);
1885 }
1886 return 0;
1887 }
1888
1889 /* listen worker: finish */
smc_listen_out(struct smc_sock * new_smc)1890 static void smc_listen_out(struct smc_sock *new_smc)
1891 {
1892 struct smc_sock *lsmc = new_smc->listen_smc;
1893 struct sock *newsmcsk = &new_smc->sk;
1894
1895 if (tcp_sk(new_smc->clcsock->sk)->syn_smc)
1896 atomic_dec(&lsmc->queued_smc_hs);
1897
1898 if (lsmc->sk.sk_state == SMC_LISTEN) {
1899 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
1900 smc_accept_enqueue(&lsmc->sk, newsmcsk);
1901 release_sock(&lsmc->sk);
1902 } else { /* no longer listening */
1903 smc_close_non_accepted(newsmcsk);
1904 }
1905
1906 /* Wake up accept */
1907 lsmc->sk.sk_data_ready(&lsmc->sk);
1908 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
1909 }
1910
1911 /* listen worker: finish in state connected */
smc_listen_out_connected(struct smc_sock * new_smc)1912 static void smc_listen_out_connected(struct smc_sock *new_smc)
1913 {
1914 struct sock *newsmcsk = &new_smc->sk;
1915
1916 if (newsmcsk->sk_state == SMC_INIT)
1917 newsmcsk->sk_state = SMC_ACTIVE;
1918
1919 smc_listen_out(new_smc);
1920 }
1921
1922 /* listen worker: finish in error state */
smc_listen_out_err(struct smc_sock * new_smc)1923 static void smc_listen_out_err(struct smc_sock *new_smc)
1924 {
1925 struct sock *newsmcsk = &new_smc->sk;
1926 struct net *net = sock_net(newsmcsk);
1927
1928 this_cpu_inc(net->smc.smc_stats->srv_hshake_err_cnt);
1929 if (newsmcsk->sk_state == SMC_INIT)
1930 sock_put(&new_smc->sk); /* passive closing */
1931 newsmcsk->sk_state = SMC_CLOSED;
1932
1933 smc_listen_out(new_smc);
1934 }
1935
1936 /* listen worker: decline and fall back if possible */
smc_listen_decline(struct smc_sock * new_smc,int reason_code,int local_first,u8 version)1937 static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1938 int local_first, u8 version)
1939 {
1940 /* RDMA setup failed, switch back to TCP */
1941 smc_conn_abort(new_smc, local_first);
1942 if (reason_code < 0 ||
1943 smc_switch_to_fallback(new_smc, reason_code)) {
1944 /* error, no fallback possible */
1945 smc_listen_out_err(new_smc);
1946 return;
1947 }
1948 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
1949 if (smc_clc_send_decline(new_smc, reason_code, version) < 0) {
1950 smc_listen_out_err(new_smc);
1951 return;
1952 }
1953 }
1954 smc_listen_out_connected(new_smc);
1955 }
1956
1957 /* listen worker: version checking */
smc_listen_v2_check(struct smc_sock * new_smc,struct smc_clc_msg_proposal * pclc,struct smc_init_info * ini)1958 static int smc_listen_v2_check(struct smc_sock *new_smc,
1959 struct smc_clc_msg_proposal *pclc,
1960 struct smc_init_info *ini)
1961 {
1962 struct smc_clc_smcd_v2_extension *pclc_smcd_v2_ext;
1963 struct smc_clc_v2_extension *pclc_v2_ext;
1964 int rc = SMC_CLC_DECL_PEERNOSMC;
1965
1966 ini->smc_type_v1 = pclc->hdr.typev1;
1967 ini->smc_type_v2 = pclc->hdr.typev2;
1968 ini->smcd_version = smcd_indicated(ini->smc_type_v1) ? SMC_V1 : 0;
1969 ini->smcr_version = smcr_indicated(ini->smc_type_v1) ? SMC_V1 : 0;
1970 if (pclc->hdr.version > SMC_V1) {
1971 if (smcd_indicated(ini->smc_type_v2))
1972 ini->smcd_version |= SMC_V2;
1973 if (smcr_indicated(ini->smc_type_v2))
1974 ini->smcr_version |= SMC_V2;
1975 }
1976 if (!(ini->smcd_version & SMC_V2) && !(ini->smcr_version & SMC_V2)) {
1977 rc = SMC_CLC_DECL_PEERNOSMC;
1978 goto out;
1979 }
1980 pclc_v2_ext = smc_get_clc_v2_ext(pclc);
1981 if (!pclc_v2_ext) {
1982 ini->smcd_version &= ~SMC_V2;
1983 ini->smcr_version &= ~SMC_V2;
1984 rc = SMC_CLC_DECL_NOV2EXT;
1985 goto out;
1986 }
1987 pclc_smcd_v2_ext = smc_get_clc_smcd_v2_ext(pclc_v2_ext);
1988 if (ini->smcd_version & SMC_V2) {
1989 if (!smc_ism_is_v2_capable()) {
1990 ini->smcd_version &= ~SMC_V2;
1991 rc = SMC_CLC_DECL_NOISM2SUPP;
1992 } else if (!pclc_smcd_v2_ext) {
1993 ini->smcd_version &= ~SMC_V2;
1994 rc = SMC_CLC_DECL_NOV2DEXT;
1995 } else if (!pclc_v2_ext->hdr.eid_cnt &&
1996 !pclc_v2_ext->hdr.flag.seid) {
1997 ini->smcd_version &= ~SMC_V2;
1998 rc = SMC_CLC_DECL_NOUEID;
1999 }
2000 }
2001 if (ini->smcr_version & SMC_V2) {
2002 if (!pclc_v2_ext->hdr.eid_cnt) {
2003 ini->smcr_version &= ~SMC_V2;
2004 rc = SMC_CLC_DECL_NOUEID;
2005 }
2006 }
2007
2008 ini->release_nr = pclc_v2_ext->hdr.flag.release;
2009 if (pclc_v2_ext->hdr.flag.release > SMC_RELEASE)
2010 ini->release_nr = SMC_RELEASE;
2011
2012 out:
2013 if (!ini->smcd_version && !ini->smcr_version)
2014 return rc;
2015
2016 return 0;
2017 }
2018
2019 /* listen worker: check prefixes */
smc_listen_prfx_check(struct smc_sock * new_smc,struct smc_clc_msg_proposal * pclc)2020 static int smc_listen_prfx_check(struct smc_sock *new_smc,
2021 struct smc_clc_msg_proposal *pclc)
2022 {
2023 struct smc_clc_msg_proposal_prefix *pclc_prfx;
2024 struct socket *newclcsock = new_smc->clcsock;
2025
2026 if (pclc->hdr.typev1 == SMC_TYPE_N)
2027 return 0;
2028 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
2029 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
2030 return SMC_CLC_DECL_DIFFPREFIX;
2031
2032 return 0;
2033 }
2034
2035 /* listen worker: initialize connection and buffers */
smc_listen_rdma_init(struct smc_sock * new_smc,struct smc_init_info * ini)2036 static int smc_listen_rdma_init(struct smc_sock *new_smc,
2037 struct smc_init_info *ini)
2038 {
2039 int rc;
2040
2041 /* allocate connection / link group */
2042 rc = smc_conn_create(new_smc, ini);
2043 if (rc)
2044 return rc;
2045
2046 /* create send buffer and rmb */
2047 if (smc_buf_create(new_smc, false)) {
2048 smc_conn_abort(new_smc, ini->first_contact_local);
2049 return SMC_CLC_DECL_MEM;
2050 }
2051
2052 return 0;
2053 }
2054
2055 /* listen worker: initialize connection and buffers for SMC-D */
smc_listen_ism_init(struct smc_sock * new_smc,struct smc_init_info * ini)2056 static int smc_listen_ism_init(struct smc_sock *new_smc,
2057 struct smc_init_info *ini)
2058 {
2059 int rc;
2060
2061 rc = smc_conn_create(new_smc, ini);
2062 if (rc)
2063 return rc;
2064
2065 /* Create send and receive buffers */
2066 rc = smc_buf_create(new_smc, true);
2067 if (rc) {
2068 smc_conn_abort(new_smc, ini->first_contact_local);
2069 return (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB :
2070 SMC_CLC_DECL_MEM;
2071 }
2072
2073 return 0;
2074 }
2075
smc_is_already_selected(struct smcd_dev * smcd,struct smc_init_info * ini,int matches)2076 static bool smc_is_already_selected(struct smcd_dev *smcd,
2077 struct smc_init_info *ini,
2078 int matches)
2079 {
2080 int i;
2081
2082 for (i = 0; i < matches; i++)
2083 if (smcd == ini->ism_dev[i])
2084 return true;
2085
2086 return false;
2087 }
2088
2089 /* 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)2090 static void smc_check_ism_v2_match(struct smc_init_info *ini,
2091 u16 proposed_chid, u64 proposed_gid,
2092 unsigned int *matches)
2093 {
2094 struct smcd_dev *smcd;
2095
2096 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
2097 if (smcd->going_away)
2098 continue;
2099 if (smc_is_already_selected(smcd, ini, *matches))
2100 continue;
2101 if (smc_ism_get_chid(smcd) == proposed_chid &&
2102 !smc_ism_cantalk(proposed_gid, ISM_RESERVED_VLANID, smcd)) {
2103 ini->ism_peer_gid[*matches] = proposed_gid;
2104 ini->ism_dev[*matches] = smcd;
2105 (*matches)++;
2106 break;
2107 }
2108 }
2109 }
2110
smc_find_ism_store_rc(u32 rc,struct smc_init_info * ini)2111 static void smc_find_ism_store_rc(u32 rc, struct smc_init_info *ini)
2112 {
2113 if (!ini->rc)
2114 ini->rc = rc;
2115 }
2116
smc_find_ism_v2_device_serv(struct smc_sock * new_smc,struct smc_clc_msg_proposal * pclc,struct smc_init_info * ini)2117 static void smc_find_ism_v2_device_serv(struct smc_sock *new_smc,
2118 struct smc_clc_msg_proposal *pclc,
2119 struct smc_init_info *ini)
2120 {
2121 struct smc_clc_smcd_v2_extension *smcd_v2_ext;
2122 struct smc_clc_v2_extension *smc_v2_ext;
2123 struct smc_clc_msg_smcd *pclc_smcd;
2124 unsigned int matches = 0;
2125 u8 smcd_version;
2126 u8 *eid = NULL;
2127 int i, rc;
2128
2129 if (!(ini->smcd_version & SMC_V2) || !smcd_indicated(ini->smc_type_v2))
2130 goto not_found;
2131
2132 pclc_smcd = smc_get_clc_msg_smcd(pclc);
2133 smc_v2_ext = smc_get_clc_v2_ext(pclc);
2134 smcd_v2_ext = smc_get_clc_smcd_v2_ext(smc_v2_ext);
2135
2136 mutex_lock(&smcd_dev_list.mutex);
2137 if (pclc_smcd->ism.chid)
2138 /* check for ISM device matching proposed native ISM device */
2139 smc_check_ism_v2_match(ini, ntohs(pclc_smcd->ism.chid),
2140 ntohll(pclc_smcd->ism.gid), &matches);
2141 for (i = 1; i <= smc_v2_ext->hdr.ism_gid_cnt; i++) {
2142 /* check for ISM devices matching proposed non-native ISM
2143 * devices
2144 */
2145 smc_check_ism_v2_match(ini,
2146 ntohs(smcd_v2_ext->gidchid[i - 1].chid),
2147 ntohll(smcd_v2_ext->gidchid[i - 1].gid),
2148 &matches);
2149 }
2150 mutex_unlock(&smcd_dev_list.mutex);
2151
2152 if (!ini->ism_dev[0]) {
2153 smc_find_ism_store_rc(SMC_CLC_DECL_NOSMCD2DEV, ini);
2154 goto not_found;
2155 }
2156
2157 smc_ism_get_system_eid(&eid);
2158 if (!smc_clc_match_eid(ini->negotiated_eid, smc_v2_ext,
2159 smcd_v2_ext->system_eid, eid))
2160 goto not_found;
2161
2162 /* separate - outside the smcd_dev_list.lock */
2163 smcd_version = ini->smcd_version;
2164 for (i = 0; i < matches; i++) {
2165 ini->smcd_version = SMC_V2;
2166 ini->is_smcd = true;
2167 ini->ism_selected = i;
2168 rc = smc_listen_ism_init(new_smc, ini);
2169 if (rc) {
2170 smc_find_ism_store_rc(rc, ini);
2171 /* try next active ISM device */
2172 continue;
2173 }
2174 return; /* matching and usable V2 ISM device found */
2175 }
2176 /* no V2 ISM device could be initialized */
2177 ini->smcd_version = smcd_version; /* restore original value */
2178 ini->negotiated_eid[0] = 0;
2179
2180 not_found:
2181 ini->smcd_version &= ~SMC_V2;
2182 ini->ism_dev[0] = NULL;
2183 ini->is_smcd = false;
2184 }
2185
smc_find_ism_v1_device_serv(struct smc_sock * new_smc,struct smc_clc_msg_proposal * pclc,struct smc_init_info * ini)2186 static void smc_find_ism_v1_device_serv(struct smc_sock *new_smc,
2187 struct smc_clc_msg_proposal *pclc,
2188 struct smc_init_info *ini)
2189 {
2190 struct smc_clc_msg_smcd *pclc_smcd = smc_get_clc_msg_smcd(pclc);
2191 int rc = 0;
2192
2193 /* check if ISM V1 is available */
2194 if (!(ini->smcd_version & SMC_V1) || !smcd_indicated(ini->smc_type_v1))
2195 goto not_found;
2196 ini->is_smcd = true; /* prepare ISM check */
2197 ini->ism_peer_gid[0] = ntohll(pclc_smcd->ism.gid);
2198 rc = smc_find_ism_device(new_smc, ini);
2199 if (rc)
2200 goto not_found;
2201 ini->ism_selected = 0;
2202 rc = smc_listen_ism_init(new_smc, ini);
2203 if (!rc)
2204 return; /* V1 ISM device found */
2205
2206 not_found:
2207 smc_find_ism_store_rc(rc, ini);
2208 ini->smcd_version &= ~SMC_V1;
2209 ini->ism_dev[0] = NULL;
2210 ini->is_smcd = false;
2211 }
2212
2213 /* listen worker: register buffers */
smc_listen_rdma_reg(struct smc_sock * new_smc,bool local_first)2214 static int smc_listen_rdma_reg(struct smc_sock *new_smc, bool local_first)
2215 {
2216 struct smc_connection *conn = &new_smc->conn;
2217
2218 if (!local_first) {
2219 /* reg sendbufs if they were vzalloced */
2220 if (conn->sndbuf_desc->is_vm) {
2221 if (smcr_lgr_reg_sndbufs(conn->lnk,
2222 conn->sndbuf_desc))
2223 return SMC_CLC_DECL_ERR_REGBUF;
2224 }
2225 if (smcr_lgr_reg_rmbs(conn->lnk, conn->rmb_desc))
2226 return SMC_CLC_DECL_ERR_REGBUF;
2227 }
2228
2229 return 0;
2230 }
2231
smc_find_rdma_v2_device_serv(struct smc_sock * new_smc,struct smc_clc_msg_proposal * pclc,struct smc_init_info * ini)2232 static void smc_find_rdma_v2_device_serv(struct smc_sock *new_smc,
2233 struct smc_clc_msg_proposal *pclc,
2234 struct smc_init_info *ini)
2235 {
2236 struct smc_clc_v2_extension *smc_v2_ext;
2237 u8 smcr_version;
2238 int rc;
2239
2240 if (!(ini->smcr_version & SMC_V2) || !smcr_indicated(ini->smc_type_v2))
2241 goto not_found;
2242
2243 smc_v2_ext = smc_get_clc_v2_ext(pclc);
2244 if (!smc_clc_match_eid(ini->negotiated_eid, smc_v2_ext, NULL, NULL))
2245 goto not_found;
2246
2247 /* prepare RDMA check */
2248 memcpy(ini->peer_systemid, pclc->lcl.id_for_peer, SMC_SYSTEMID_LEN);
2249 memcpy(ini->peer_gid, smc_v2_ext->roce, SMC_GID_SIZE);
2250 memcpy(ini->peer_mac, pclc->lcl.mac, ETH_ALEN);
2251 ini->check_smcrv2 = true;
2252 ini->smcrv2.clc_sk = new_smc->clcsock->sk;
2253 ini->smcrv2.saddr = new_smc->clcsock->sk->sk_rcv_saddr;
2254 ini->smcrv2.daddr = smc_ib_gid_to_ipv4(smc_v2_ext->roce);
2255 rc = smc_find_rdma_device(new_smc, ini);
2256 if (rc) {
2257 smc_find_ism_store_rc(rc, ini);
2258 goto not_found;
2259 }
2260 if (!ini->smcrv2.uses_gateway)
2261 memcpy(ini->smcrv2.nexthop_mac, pclc->lcl.mac, ETH_ALEN);
2262
2263 smcr_version = ini->smcr_version;
2264 ini->smcr_version = SMC_V2;
2265 rc = smc_listen_rdma_init(new_smc, ini);
2266 if (!rc) {
2267 rc = smc_listen_rdma_reg(new_smc, ini->first_contact_local);
2268 if (rc)
2269 smc_conn_abort(new_smc, ini->first_contact_local);
2270 }
2271 if (!rc)
2272 return;
2273 ini->smcr_version = smcr_version;
2274 smc_find_ism_store_rc(rc, ini);
2275
2276 not_found:
2277 ini->smcr_version &= ~SMC_V2;
2278 ini->smcrv2.ib_dev_v2 = NULL;
2279 ini->check_smcrv2 = false;
2280 }
2281
smc_find_rdma_v1_device_serv(struct smc_sock * new_smc,struct smc_clc_msg_proposal * pclc,struct smc_init_info * ini)2282 static int smc_find_rdma_v1_device_serv(struct smc_sock *new_smc,
2283 struct smc_clc_msg_proposal *pclc,
2284 struct smc_init_info *ini)
2285 {
2286 int rc;
2287
2288 if (!(ini->smcr_version & SMC_V1) || !smcr_indicated(ini->smc_type_v1))
2289 return SMC_CLC_DECL_NOSMCDEV;
2290
2291 /* prepare RDMA check */
2292 memcpy(ini->peer_systemid, pclc->lcl.id_for_peer, SMC_SYSTEMID_LEN);
2293 memcpy(ini->peer_gid, pclc->lcl.gid, SMC_GID_SIZE);
2294 memcpy(ini->peer_mac, pclc->lcl.mac, ETH_ALEN);
2295 rc = smc_find_rdma_device(new_smc, ini);
2296 if (rc) {
2297 /* no RDMA device found */
2298 return SMC_CLC_DECL_NOSMCDEV;
2299 }
2300 rc = smc_listen_rdma_init(new_smc, ini);
2301 if (rc)
2302 return rc;
2303 return smc_listen_rdma_reg(new_smc, ini->first_contact_local);
2304 }
2305
2306 /* 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)2307 static int smc_listen_find_device(struct smc_sock *new_smc,
2308 struct smc_clc_msg_proposal *pclc,
2309 struct smc_init_info *ini)
2310 {
2311 int prfx_rc;
2312
2313 /* check for ISM device matching V2 proposed device */
2314 smc_find_ism_v2_device_serv(new_smc, pclc, ini);
2315 if (ini->ism_dev[0])
2316 return 0;
2317
2318 /* check for matching IP prefix and subnet length (V1) */
2319 prfx_rc = smc_listen_prfx_check(new_smc, pclc);
2320 if (prfx_rc)
2321 smc_find_ism_store_rc(prfx_rc, ini);
2322
2323 /* get vlan id from IP device */
2324 if (smc_vlan_by_tcpsk(new_smc->clcsock, ini))
2325 return ini->rc ?: SMC_CLC_DECL_GETVLANERR;
2326
2327 /* check for ISM device matching V1 proposed device */
2328 if (!prfx_rc)
2329 smc_find_ism_v1_device_serv(new_smc, pclc, ini);
2330 if (ini->ism_dev[0])
2331 return 0;
2332
2333 if (!smcr_indicated(pclc->hdr.typev1) &&
2334 !smcr_indicated(pclc->hdr.typev2))
2335 /* skip RDMA and decline */
2336 return ini->rc ?: SMC_CLC_DECL_NOSMCDDEV;
2337
2338 /* check if RDMA V2 is available */
2339 smc_find_rdma_v2_device_serv(new_smc, pclc, ini);
2340 if (ini->smcrv2.ib_dev_v2)
2341 return 0;
2342
2343 /* check if RDMA V1 is available */
2344 if (!prfx_rc) {
2345 int rc;
2346
2347 rc = smc_find_rdma_v1_device_serv(new_smc, pclc, ini);
2348 smc_find_ism_store_rc(rc, ini);
2349 return (!rc) ? 0 : ini->rc;
2350 }
2351 return prfx_rc;
2352 }
2353
2354 /* listen worker: finish RDMA setup */
smc_listen_rdma_finish(struct smc_sock * new_smc,struct smc_clc_msg_accept_confirm * cclc,bool local_first,struct smc_init_info * ini)2355 static int smc_listen_rdma_finish(struct smc_sock *new_smc,
2356 struct smc_clc_msg_accept_confirm *cclc,
2357 bool local_first,
2358 struct smc_init_info *ini)
2359 {
2360 struct smc_link *link = new_smc->conn.lnk;
2361 int reason_code = 0;
2362
2363 if (local_first)
2364 smc_link_save_peer_info(link, cclc, ini);
2365
2366 if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc))
2367 return SMC_CLC_DECL_ERR_RTOK;
2368
2369 if (local_first) {
2370 if (smc_ib_ready_link(link))
2371 return SMC_CLC_DECL_ERR_RDYLNK;
2372 /* QP confirmation over RoCE fabric */
2373 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
2374 reason_code = smcr_serv_conf_first_link(new_smc);
2375 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
2376 }
2377 return reason_code;
2378 }
2379
2380 /* setup for connection of server */
smc_listen_work(struct work_struct * work)2381 static void smc_listen_work(struct work_struct *work)
2382 {
2383 struct smc_sock *new_smc = container_of(work, struct smc_sock,
2384 smc_listen_work);
2385 struct socket *newclcsock = new_smc->clcsock;
2386 struct smc_clc_msg_accept_confirm *cclc;
2387 struct smc_clc_msg_proposal_area *buf;
2388 struct smc_clc_msg_proposal *pclc;
2389 struct smc_init_info *ini = NULL;
2390 u8 proposal_version = SMC_V1;
2391 u8 accept_version;
2392 int rc = 0;
2393
2394 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
2395 return smc_listen_out_err(new_smc);
2396
2397 if (new_smc->use_fallback) {
2398 smc_listen_out_connected(new_smc);
2399 return;
2400 }
2401
2402 /* check if peer is smc capable */
2403 if (!tcp_sk(newclcsock->sk)->syn_smc) {
2404 rc = smc_switch_to_fallback(new_smc, SMC_CLC_DECL_PEERNOSMC);
2405 if (rc)
2406 smc_listen_out_err(new_smc);
2407 else
2408 smc_listen_out_connected(new_smc);
2409 return;
2410 }
2411
2412 /* do inband token exchange -
2413 * wait for and receive SMC Proposal CLC message
2414 */
2415 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
2416 if (!buf) {
2417 rc = SMC_CLC_DECL_MEM;
2418 goto out_decl;
2419 }
2420 pclc = (struct smc_clc_msg_proposal *)buf;
2421 rc = smc_clc_wait_msg(new_smc, pclc, sizeof(*buf),
2422 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
2423 if (rc)
2424 goto out_decl;
2425
2426 if (pclc->hdr.version > SMC_V1)
2427 proposal_version = SMC_V2;
2428
2429 /* IPSec connections opt out of SMC optimizations */
2430 if (using_ipsec(new_smc)) {
2431 rc = SMC_CLC_DECL_IPSEC;
2432 goto out_decl;
2433 }
2434
2435 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
2436 if (!ini) {
2437 rc = SMC_CLC_DECL_MEM;
2438 goto out_decl;
2439 }
2440
2441 /* initial version checking */
2442 rc = smc_listen_v2_check(new_smc, pclc, ini);
2443 if (rc)
2444 goto out_decl;
2445
2446 rc = smc_clc_srv_v2x_features_validate(pclc, ini);
2447 if (rc)
2448 goto out_decl;
2449
2450 mutex_lock(&smc_server_lgr_pending);
2451 smc_close_init(new_smc);
2452 smc_rx_init(new_smc);
2453 smc_tx_init(new_smc);
2454
2455 /* determine ISM or RoCE device used for connection */
2456 rc = smc_listen_find_device(new_smc, pclc, ini);
2457 if (rc)
2458 goto out_unlock;
2459
2460 /* send SMC Accept CLC message */
2461 accept_version = ini->is_smcd ? ini->smcd_version : ini->smcr_version;
2462 rc = smc_clc_send_accept(new_smc, ini->first_contact_local,
2463 accept_version, ini->negotiated_eid, ini);
2464 if (rc)
2465 goto out_unlock;
2466
2467 /* SMC-D does not need this lock any more */
2468 if (ini->is_smcd)
2469 mutex_unlock(&smc_server_lgr_pending);
2470
2471 /* receive SMC Confirm CLC message */
2472 memset(buf, 0, sizeof(*buf));
2473 cclc = (struct smc_clc_msg_accept_confirm *)buf;
2474 rc = smc_clc_wait_msg(new_smc, cclc, sizeof(*buf),
2475 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
2476 if (rc) {
2477 if (!ini->is_smcd)
2478 goto out_unlock;
2479 goto out_decl;
2480 }
2481
2482 rc = smc_clc_v2x_features_confirm_check(cclc, ini);
2483 if (rc) {
2484 if (!ini->is_smcd)
2485 goto out_unlock;
2486 goto out_decl;
2487 }
2488
2489 /* fce smc release version is needed in smc_listen_rdma_finish,
2490 * so save fce info here.
2491 */
2492 smc_conn_save_peer_info_fce(new_smc, cclc);
2493
2494 /* finish worker */
2495 if (!ini->is_smcd) {
2496 rc = smc_listen_rdma_finish(new_smc, cclc,
2497 ini->first_contact_local, ini);
2498 if (rc)
2499 goto out_unlock;
2500 mutex_unlock(&smc_server_lgr_pending);
2501 }
2502 smc_conn_save_peer_info(new_smc, cclc);
2503 smc_listen_out_connected(new_smc);
2504 SMC_STAT_SERV_SUCC_INC(sock_net(newclcsock->sk), ini);
2505 goto out_free;
2506
2507 out_unlock:
2508 mutex_unlock(&smc_server_lgr_pending);
2509 out_decl:
2510 smc_listen_decline(new_smc, rc, ini ? ini->first_contact_local : 0,
2511 proposal_version);
2512 out_free:
2513 kfree(ini);
2514 kfree(buf);
2515 }
2516
smc_tcp_listen_work(struct work_struct * work)2517 static void smc_tcp_listen_work(struct work_struct *work)
2518 {
2519 struct smc_sock *lsmc = container_of(work, struct smc_sock,
2520 tcp_listen_work);
2521 struct sock *lsk = &lsmc->sk;
2522 struct smc_sock *new_smc;
2523 int rc = 0;
2524
2525 lock_sock(lsk);
2526 while (lsk->sk_state == SMC_LISTEN) {
2527 rc = smc_clcsock_accept(lsmc, &new_smc);
2528 if (rc) /* clcsock accept queue empty or error */
2529 goto out;
2530 if (!new_smc)
2531 continue;
2532
2533 if (tcp_sk(new_smc->clcsock->sk)->syn_smc)
2534 atomic_inc(&lsmc->queued_smc_hs);
2535
2536 new_smc->listen_smc = lsmc;
2537 new_smc->use_fallback = lsmc->use_fallback;
2538 new_smc->fallback_rsn = lsmc->fallback_rsn;
2539 sock_hold(lsk); /* sock_put in smc_listen_work */
2540 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
2541 smc_copy_sock_settings_to_smc(new_smc);
2542 sock_hold(&new_smc->sk); /* sock_put in passive closing */
2543 if (!queue_work(smc_hs_wq, &new_smc->smc_listen_work))
2544 sock_put(&new_smc->sk);
2545 }
2546
2547 out:
2548 release_sock(lsk);
2549 sock_put(&lsmc->sk); /* sock_hold in smc_clcsock_data_ready() */
2550 }
2551
smc_clcsock_data_ready(struct sock * listen_clcsock)2552 static void smc_clcsock_data_ready(struct sock *listen_clcsock)
2553 {
2554 struct smc_sock *lsmc;
2555
2556 read_lock_bh(&listen_clcsock->sk_callback_lock);
2557 lsmc = smc_clcsock_user_data(listen_clcsock);
2558 if (!lsmc)
2559 goto out;
2560 lsmc->clcsk_data_ready(listen_clcsock);
2561 if (lsmc->sk.sk_state == SMC_LISTEN) {
2562 sock_hold(&lsmc->sk); /* sock_put in smc_tcp_listen_work() */
2563 if (!queue_work(smc_tcp_ls_wq, &lsmc->tcp_listen_work))
2564 sock_put(&lsmc->sk);
2565 }
2566 out:
2567 read_unlock_bh(&listen_clcsock->sk_callback_lock);
2568 }
2569
smc_listen(struct socket * sock,int backlog)2570 static int smc_listen(struct socket *sock, int backlog)
2571 {
2572 struct sock *sk = sock->sk;
2573 struct smc_sock *smc;
2574 int rc;
2575
2576 smc = smc_sk(sk);
2577 lock_sock(sk);
2578
2579 rc = -EINVAL;
2580 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
2581 smc->connect_nonblock || sock->state != SS_UNCONNECTED)
2582 goto out;
2583
2584 rc = 0;
2585 if (sk->sk_state == SMC_LISTEN) {
2586 sk->sk_max_ack_backlog = backlog;
2587 goto out;
2588 }
2589 /* some socket options are handled in core, so we could not apply
2590 * them to the clc socket -- copy smc socket options to clc socket
2591 */
2592 smc_copy_sock_settings_to_clc(smc);
2593 if (!smc->use_fallback)
2594 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
2595
2596 /* save original sk_data_ready function and establish
2597 * smc-specific sk_data_ready function
2598 */
2599 write_lock_bh(&smc->clcsock->sk->sk_callback_lock);
2600 smc->clcsock->sk->sk_user_data =
2601 (void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY);
2602 smc_clcsock_replace_cb(&smc->clcsock->sk->sk_data_ready,
2603 smc_clcsock_data_ready, &smc->clcsk_data_ready);
2604 write_unlock_bh(&smc->clcsock->sk->sk_callback_lock);
2605
2606 /* save original ops */
2607 smc->ori_af_ops = inet_csk(smc->clcsock->sk)->icsk_af_ops;
2608
2609 smc->af_ops = *smc->ori_af_ops;
2610 smc->af_ops.syn_recv_sock = smc_tcp_syn_recv_sock;
2611
2612 inet_csk(smc->clcsock->sk)->icsk_af_ops = &smc->af_ops;
2613
2614 if (smc->limit_smc_hs)
2615 tcp_sk(smc->clcsock->sk)->smc_hs_congested = smc_hs_congested;
2616
2617 rc = kernel_listen(smc->clcsock, backlog);
2618 if (rc) {
2619 write_lock_bh(&smc->clcsock->sk->sk_callback_lock);
2620 smc_clcsock_restore_cb(&smc->clcsock->sk->sk_data_ready,
2621 &smc->clcsk_data_ready);
2622 smc->clcsock->sk->sk_user_data = NULL;
2623 write_unlock_bh(&smc->clcsock->sk->sk_callback_lock);
2624 goto out;
2625 }
2626 sk->sk_max_ack_backlog = backlog;
2627 sk->sk_ack_backlog = 0;
2628 sk->sk_state = SMC_LISTEN;
2629
2630 out:
2631 release_sock(sk);
2632 return rc;
2633 }
2634
smc_accept(struct socket * sock,struct socket * new_sock,int flags,bool kern)2635 static int smc_accept(struct socket *sock, struct socket *new_sock,
2636 int flags, bool kern)
2637 {
2638 struct sock *sk = sock->sk, *nsk;
2639 DECLARE_WAITQUEUE(wait, current);
2640 struct smc_sock *lsmc;
2641 long timeo;
2642 int rc = 0;
2643
2644 lsmc = smc_sk(sk);
2645 sock_hold(sk); /* sock_put below */
2646 lock_sock(sk);
2647
2648 if (lsmc->sk.sk_state != SMC_LISTEN) {
2649 rc = -EINVAL;
2650 release_sock(sk);
2651 goto out;
2652 }
2653
2654 /* Wait for an incoming connection */
2655 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2656 add_wait_queue_exclusive(sk_sleep(sk), &wait);
2657 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
2658 set_current_state(TASK_INTERRUPTIBLE);
2659 if (!timeo) {
2660 rc = -EAGAIN;
2661 break;
2662 }
2663 release_sock(sk);
2664 timeo = schedule_timeout(timeo);
2665 /* wakeup by sk_data_ready in smc_listen_work() */
2666 sched_annotate_sleep();
2667 lock_sock(sk);
2668 if (signal_pending(current)) {
2669 rc = sock_intr_errno(timeo);
2670 break;
2671 }
2672 }
2673 set_current_state(TASK_RUNNING);
2674 remove_wait_queue(sk_sleep(sk), &wait);
2675
2676 if (!rc)
2677 rc = sock_error(nsk);
2678 release_sock(sk);
2679 if (rc)
2680 goto out;
2681
2682 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
2683 /* wait till data arrives on the socket */
2684 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
2685 MSEC_PER_SEC);
2686 if (smc_sk(nsk)->use_fallback) {
2687 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
2688
2689 lock_sock(clcsk);
2690 if (skb_queue_empty(&clcsk->sk_receive_queue))
2691 sk_wait_data(clcsk, &timeo, NULL);
2692 release_sock(clcsk);
2693 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
2694 lock_sock(nsk);
2695 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
2696 release_sock(nsk);
2697 }
2698 }
2699
2700 out:
2701 sock_put(sk); /* sock_hold above */
2702 return rc;
2703 }
2704
smc_getname(struct socket * sock,struct sockaddr * addr,int peer)2705 static int smc_getname(struct socket *sock, struct sockaddr *addr,
2706 int peer)
2707 {
2708 struct smc_sock *smc;
2709
2710 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
2711 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
2712 return -ENOTCONN;
2713
2714 smc = smc_sk(sock->sk);
2715
2716 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
2717 }
2718
smc_sendmsg(struct socket * sock,struct msghdr * msg,size_t len)2719 static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
2720 {
2721 struct sock *sk = sock->sk;
2722 struct smc_sock *smc;
2723 int rc;
2724
2725 smc = smc_sk(sk);
2726 lock_sock(sk);
2727
2728 /* SMC does not support connect with fastopen */
2729 if (msg->msg_flags & MSG_FASTOPEN) {
2730 /* not connected yet, fallback */
2731 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
2732 rc = smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP);
2733 if (rc)
2734 goto out;
2735 } else {
2736 rc = -EINVAL;
2737 goto out;
2738 }
2739 } else if ((sk->sk_state != SMC_ACTIVE) &&
2740 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2741 (sk->sk_state != SMC_INIT)) {
2742 rc = -EPIPE;
2743 goto out;
2744 }
2745
2746 if (smc->use_fallback) {
2747 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
2748 } else {
2749 rc = smc_tx_sendmsg(smc, msg, len);
2750 SMC_STAT_TX_PAYLOAD(smc, len, rc);
2751 }
2752 out:
2753 release_sock(sk);
2754 return rc;
2755 }
2756
smc_recvmsg(struct socket * sock,struct msghdr * msg,size_t len,int flags)2757 static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
2758 int flags)
2759 {
2760 struct sock *sk = sock->sk;
2761 struct smc_sock *smc;
2762 int rc = -ENOTCONN;
2763
2764 smc = smc_sk(sk);
2765 lock_sock(sk);
2766 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2767 /* socket was connected before, no more data to read */
2768 rc = 0;
2769 goto out;
2770 }
2771 if ((sk->sk_state == SMC_INIT) ||
2772 (sk->sk_state == SMC_LISTEN) ||
2773 (sk->sk_state == SMC_CLOSED))
2774 goto out;
2775
2776 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2777 rc = 0;
2778 goto out;
2779 }
2780
2781 if (smc->use_fallback) {
2782 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
2783 } else {
2784 msg->msg_namelen = 0;
2785 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
2786 SMC_STAT_RX_PAYLOAD(smc, rc, rc);
2787 }
2788
2789 out:
2790 release_sock(sk);
2791 return rc;
2792 }
2793
smc_accept_poll(struct sock * parent)2794 static __poll_t smc_accept_poll(struct sock *parent)
2795 {
2796 struct smc_sock *isk = smc_sk(parent);
2797 __poll_t mask = 0;
2798
2799 spin_lock(&isk->accept_q_lock);
2800 if (!list_empty(&isk->accept_q))
2801 mask = EPOLLIN | EPOLLRDNORM;
2802 spin_unlock(&isk->accept_q_lock);
2803
2804 return mask;
2805 }
2806
smc_poll(struct file * file,struct socket * sock,poll_table * wait)2807 static __poll_t smc_poll(struct file *file, struct socket *sock,
2808 poll_table *wait)
2809 {
2810 struct sock *sk = sock->sk;
2811 struct smc_sock *smc;
2812 __poll_t mask = 0;
2813
2814 if (!sk)
2815 return EPOLLNVAL;
2816
2817 smc = smc_sk(sock->sk);
2818 if (smc->use_fallback) {
2819 /* delegate to CLC child sock */
2820 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
2821 sk->sk_err = smc->clcsock->sk->sk_err;
2822 } else {
2823 if (sk->sk_state != SMC_CLOSED)
2824 sock_poll_wait(file, sock, wait);
2825 if (sk->sk_err)
2826 mask |= EPOLLERR;
2827 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
2828 (sk->sk_state == SMC_CLOSED))
2829 mask |= EPOLLHUP;
2830 if (sk->sk_state == SMC_LISTEN) {
2831 /* woken up by sk_data_ready in smc_listen_work() */
2832 mask |= smc_accept_poll(sk);
2833 } else if (smc->use_fallback) { /* as result of connect_work()*/
2834 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
2835 wait);
2836 sk->sk_err = smc->clcsock->sk->sk_err;
2837 } else {
2838 if ((sk->sk_state != SMC_INIT &&
2839 atomic_read(&smc->conn.sndbuf_space)) ||
2840 sk->sk_shutdown & SEND_SHUTDOWN) {
2841 mask |= EPOLLOUT | EPOLLWRNORM;
2842 } else {
2843 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
2844 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
2845 }
2846 if (atomic_read(&smc->conn.bytes_to_rcv))
2847 mask |= EPOLLIN | EPOLLRDNORM;
2848 if (sk->sk_shutdown & RCV_SHUTDOWN)
2849 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
2850 if (sk->sk_state == SMC_APPCLOSEWAIT1)
2851 mask |= EPOLLIN;
2852 if (smc->conn.urg_state == SMC_URG_VALID)
2853 mask |= EPOLLPRI;
2854 }
2855 }
2856
2857 return mask;
2858 }
2859
smc_shutdown(struct socket * sock,int how)2860 static int smc_shutdown(struct socket *sock, int how)
2861 {
2862 struct sock *sk = sock->sk;
2863 bool do_shutdown = true;
2864 struct smc_sock *smc;
2865 int rc = -EINVAL;
2866 int old_state;
2867 int rc1 = 0;
2868
2869 smc = smc_sk(sk);
2870
2871 if ((how < SHUT_RD) || (how > SHUT_RDWR))
2872 return rc;
2873
2874 lock_sock(sk);
2875
2876 if (sock->state == SS_CONNECTING) {
2877 if (sk->sk_state == SMC_ACTIVE)
2878 sock->state = SS_CONNECTED;
2879 else if (sk->sk_state == SMC_PEERCLOSEWAIT1 ||
2880 sk->sk_state == SMC_PEERCLOSEWAIT2 ||
2881 sk->sk_state == SMC_APPCLOSEWAIT1 ||
2882 sk->sk_state == SMC_APPCLOSEWAIT2 ||
2883 sk->sk_state == SMC_APPFINCLOSEWAIT)
2884 sock->state = SS_DISCONNECTING;
2885 }
2886
2887 rc = -ENOTCONN;
2888 if ((sk->sk_state != SMC_ACTIVE) &&
2889 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
2890 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
2891 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2892 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
2893 (sk->sk_state != SMC_APPFINCLOSEWAIT))
2894 goto out;
2895 if (smc->use_fallback) {
2896 rc = kernel_sock_shutdown(smc->clcsock, how);
2897 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
2898 if (sk->sk_shutdown == SHUTDOWN_MASK) {
2899 sk->sk_state = SMC_CLOSED;
2900 sk->sk_socket->state = SS_UNCONNECTED;
2901 sock_put(sk);
2902 }
2903 goto out;
2904 }
2905 switch (how) {
2906 case SHUT_RDWR: /* shutdown in both directions */
2907 old_state = sk->sk_state;
2908 rc = smc_close_active(smc);
2909 if (old_state == SMC_ACTIVE &&
2910 sk->sk_state == SMC_PEERCLOSEWAIT1)
2911 do_shutdown = false;
2912 break;
2913 case SHUT_WR:
2914 rc = smc_close_shutdown_write(smc);
2915 break;
2916 case SHUT_RD:
2917 rc = 0;
2918 /* nothing more to do because peer is not involved */
2919 break;
2920 }
2921 if (do_shutdown && smc->clcsock)
2922 rc1 = kernel_sock_shutdown(smc->clcsock, how);
2923 /* map sock_shutdown_cmd constants to sk_shutdown value range */
2924 sk->sk_shutdown |= how + 1;
2925
2926 if (sk->sk_state == SMC_CLOSED)
2927 sock->state = SS_UNCONNECTED;
2928 else
2929 sock->state = SS_DISCONNECTING;
2930 out:
2931 release_sock(sk);
2932 return rc ? rc : rc1;
2933 }
2934
__smc_getsockopt(struct socket * sock,int level,int optname,char __user * optval,int __user * optlen)2935 static int __smc_getsockopt(struct socket *sock, int level, int optname,
2936 char __user *optval, int __user *optlen)
2937 {
2938 struct smc_sock *smc;
2939 int val, len;
2940
2941 smc = smc_sk(sock->sk);
2942
2943 if (get_user(len, optlen))
2944 return -EFAULT;
2945
2946 len = min_t(int, len, sizeof(int));
2947
2948 if (len < 0)
2949 return -EINVAL;
2950
2951 switch (optname) {
2952 case SMC_LIMIT_HS:
2953 val = smc->limit_smc_hs;
2954 break;
2955 default:
2956 return -EOPNOTSUPP;
2957 }
2958
2959 if (put_user(len, optlen))
2960 return -EFAULT;
2961 if (copy_to_user(optval, &val, len))
2962 return -EFAULT;
2963
2964 return 0;
2965 }
2966
__smc_setsockopt(struct socket * sock,int level,int optname,sockptr_t optval,unsigned int optlen)2967 static int __smc_setsockopt(struct socket *sock, int level, int optname,
2968 sockptr_t optval, unsigned int optlen)
2969 {
2970 struct sock *sk = sock->sk;
2971 struct smc_sock *smc;
2972 int val, rc;
2973
2974 smc = smc_sk(sk);
2975
2976 lock_sock(sk);
2977 switch (optname) {
2978 case SMC_LIMIT_HS:
2979 if (optlen < sizeof(int)) {
2980 rc = -EINVAL;
2981 break;
2982 }
2983 if (copy_from_sockptr(&val, optval, sizeof(int))) {
2984 rc = -EFAULT;
2985 break;
2986 }
2987
2988 smc->limit_smc_hs = !!val;
2989 rc = 0;
2990 break;
2991 default:
2992 rc = -EOPNOTSUPP;
2993 break;
2994 }
2995 release_sock(sk);
2996
2997 return rc;
2998 }
2999
smc_setsockopt(struct socket * sock,int level,int optname,sockptr_t optval,unsigned int optlen)3000 static int smc_setsockopt(struct socket *sock, int level, int optname,
3001 sockptr_t optval, unsigned int optlen)
3002 {
3003 struct sock *sk = sock->sk;
3004 struct smc_sock *smc;
3005 int val, rc;
3006
3007 if (level == SOL_TCP && optname == TCP_ULP)
3008 return -EOPNOTSUPP;
3009 else if (level == SOL_SMC)
3010 return __smc_setsockopt(sock, level, optname, optval, optlen);
3011
3012 smc = smc_sk(sk);
3013
3014 /* generic setsockopts reaching us here always apply to the
3015 * CLC socket
3016 */
3017 mutex_lock(&smc->clcsock_release_lock);
3018 if (!smc->clcsock) {
3019 mutex_unlock(&smc->clcsock_release_lock);
3020 return -EBADF;
3021 }
3022 if (unlikely(!smc->clcsock->ops->setsockopt))
3023 rc = -EOPNOTSUPP;
3024 else
3025 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
3026 optval, optlen);
3027 if (smc->clcsock->sk->sk_err) {
3028 sk->sk_err = smc->clcsock->sk->sk_err;
3029 sk_error_report(sk);
3030 }
3031 mutex_unlock(&smc->clcsock_release_lock);
3032
3033 if (optlen < sizeof(int))
3034 return -EINVAL;
3035 if (copy_from_sockptr(&val, optval, sizeof(int)))
3036 return -EFAULT;
3037
3038 lock_sock(sk);
3039 if (rc || smc->use_fallback)
3040 goto out;
3041 switch (optname) {
3042 case TCP_FASTOPEN:
3043 case TCP_FASTOPEN_CONNECT:
3044 case TCP_FASTOPEN_KEY:
3045 case TCP_FASTOPEN_NO_COOKIE:
3046 /* option not supported by SMC */
3047 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
3048 rc = smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP);
3049 } else {
3050 rc = -EINVAL;
3051 }
3052 break;
3053 case TCP_NODELAY:
3054 if (sk->sk_state != SMC_INIT &&
3055 sk->sk_state != SMC_LISTEN &&
3056 sk->sk_state != SMC_CLOSED) {
3057 if (val) {
3058 SMC_STAT_INC(smc, ndly_cnt);
3059 smc_tx_pending(&smc->conn);
3060 cancel_delayed_work(&smc->conn.tx_work);
3061 }
3062 }
3063 break;
3064 case TCP_CORK:
3065 if (sk->sk_state != SMC_INIT &&
3066 sk->sk_state != SMC_LISTEN &&
3067 sk->sk_state != SMC_CLOSED) {
3068 if (!val) {
3069 SMC_STAT_INC(smc, cork_cnt);
3070 smc_tx_pending(&smc->conn);
3071 cancel_delayed_work(&smc->conn.tx_work);
3072 }
3073 }
3074 break;
3075 case TCP_DEFER_ACCEPT:
3076 smc->sockopt_defer_accept = val;
3077 break;
3078 default:
3079 break;
3080 }
3081 out:
3082 release_sock(sk);
3083
3084 return rc;
3085 }
3086
smc_getsockopt(struct socket * sock,int level,int optname,char __user * optval,int __user * optlen)3087 static int smc_getsockopt(struct socket *sock, int level, int optname,
3088 char __user *optval, int __user *optlen)
3089 {
3090 struct smc_sock *smc;
3091 int rc;
3092
3093 if (level == SOL_SMC)
3094 return __smc_getsockopt(sock, level, optname, optval, optlen);
3095
3096 smc = smc_sk(sock->sk);
3097 mutex_lock(&smc->clcsock_release_lock);
3098 if (!smc->clcsock) {
3099 mutex_unlock(&smc->clcsock_release_lock);
3100 return -EBADF;
3101 }
3102 /* socket options apply to the CLC socket */
3103 if (unlikely(!smc->clcsock->ops->getsockopt)) {
3104 mutex_unlock(&smc->clcsock_release_lock);
3105 return -EOPNOTSUPP;
3106 }
3107 rc = smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
3108 optval, optlen);
3109 mutex_unlock(&smc->clcsock_release_lock);
3110 return rc;
3111 }
3112
smc_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)3113 static int smc_ioctl(struct socket *sock, unsigned int cmd,
3114 unsigned long arg)
3115 {
3116 union smc_host_cursor cons, urg;
3117 struct smc_connection *conn;
3118 struct smc_sock *smc;
3119 int answ;
3120
3121 smc = smc_sk(sock->sk);
3122 conn = &smc->conn;
3123 lock_sock(&smc->sk);
3124 if (smc->use_fallback) {
3125 if (!smc->clcsock) {
3126 release_sock(&smc->sk);
3127 return -EBADF;
3128 }
3129 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
3130 release_sock(&smc->sk);
3131 return answ;
3132 }
3133 switch (cmd) {
3134 case SIOCINQ: /* same as FIONREAD */
3135 if (smc->sk.sk_state == SMC_LISTEN) {
3136 release_sock(&smc->sk);
3137 return -EINVAL;
3138 }
3139 if (smc->sk.sk_state == SMC_INIT ||
3140 smc->sk.sk_state == SMC_CLOSED)
3141 answ = 0;
3142 else
3143 answ = atomic_read(&smc->conn.bytes_to_rcv);
3144 break;
3145 case SIOCOUTQ:
3146 /* output queue size (not send + not acked) */
3147 if (smc->sk.sk_state == SMC_LISTEN) {
3148 release_sock(&smc->sk);
3149 return -EINVAL;
3150 }
3151 if (smc->sk.sk_state == SMC_INIT ||
3152 smc->sk.sk_state == SMC_CLOSED)
3153 answ = 0;
3154 else
3155 answ = smc->conn.sndbuf_desc->len -
3156 atomic_read(&smc->conn.sndbuf_space);
3157 break;
3158 case SIOCOUTQNSD:
3159 /* output queue size (not send only) */
3160 if (smc->sk.sk_state == SMC_LISTEN) {
3161 release_sock(&smc->sk);
3162 return -EINVAL;
3163 }
3164 if (smc->sk.sk_state == SMC_INIT ||
3165 smc->sk.sk_state == SMC_CLOSED)
3166 answ = 0;
3167 else
3168 answ = smc_tx_prepared_sends(&smc->conn);
3169 break;
3170 case SIOCATMARK:
3171 if (smc->sk.sk_state == SMC_LISTEN) {
3172 release_sock(&smc->sk);
3173 return -EINVAL;
3174 }
3175 if (smc->sk.sk_state == SMC_INIT ||
3176 smc->sk.sk_state == SMC_CLOSED) {
3177 answ = 0;
3178 } else {
3179 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
3180 smc_curs_copy(&urg, &conn->urg_curs, conn);
3181 answ = smc_curs_diff(conn->rmb_desc->len,
3182 &cons, &urg) == 1;
3183 }
3184 break;
3185 default:
3186 release_sock(&smc->sk);
3187 return -ENOIOCTLCMD;
3188 }
3189 release_sock(&smc->sk);
3190
3191 return put_user(answ, (int __user *)arg);
3192 }
3193
3194 /* Map the affected portions of the rmbe into an spd, note the number of bytes
3195 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
3196 * updates till whenever a respective page has been fully processed.
3197 * Note that subsequent recv() calls have to wait till all splice() processing
3198 * completed.
3199 */
smc_splice_read(struct socket * sock,loff_t * ppos,struct pipe_inode_info * pipe,size_t len,unsigned int flags)3200 static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
3201 struct pipe_inode_info *pipe, size_t len,
3202 unsigned int flags)
3203 {
3204 struct sock *sk = sock->sk;
3205 struct smc_sock *smc;
3206 int rc = -ENOTCONN;
3207
3208 smc = smc_sk(sk);
3209 lock_sock(sk);
3210 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
3211 /* socket was connected before, no more data to read */
3212 rc = 0;
3213 goto out;
3214 }
3215 if (sk->sk_state == SMC_INIT ||
3216 sk->sk_state == SMC_LISTEN ||
3217 sk->sk_state == SMC_CLOSED)
3218 goto out;
3219
3220 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
3221 rc = 0;
3222 goto out;
3223 }
3224
3225 if (smc->use_fallback) {
3226 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
3227 pipe, len, flags);
3228 } else {
3229 if (*ppos) {
3230 rc = -ESPIPE;
3231 goto out;
3232 }
3233 if (flags & SPLICE_F_NONBLOCK)
3234 flags = MSG_DONTWAIT;
3235 else
3236 flags = 0;
3237 SMC_STAT_INC(smc, splice_cnt);
3238 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
3239 }
3240 out:
3241 release_sock(sk);
3242
3243 return rc;
3244 }
3245
3246 /* must look like tcp */
3247 static const struct proto_ops smc_sock_ops = {
3248 .family = PF_SMC,
3249 .owner = THIS_MODULE,
3250 .release = smc_release,
3251 .bind = smc_bind,
3252 .connect = smc_connect,
3253 .socketpair = sock_no_socketpair,
3254 .accept = smc_accept,
3255 .getname = smc_getname,
3256 .poll = smc_poll,
3257 .ioctl = smc_ioctl,
3258 .listen = smc_listen,
3259 .shutdown = smc_shutdown,
3260 .setsockopt = smc_setsockopt,
3261 .getsockopt = smc_getsockopt,
3262 .sendmsg = smc_sendmsg,
3263 .recvmsg = smc_recvmsg,
3264 .mmap = sock_no_mmap,
3265 .splice_read = smc_splice_read,
3266 };
3267
__smc_create(struct net * net,struct socket * sock,int protocol,int kern,struct socket * clcsock)3268 static int __smc_create(struct net *net, struct socket *sock, int protocol,
3269 int kern, struct socket *clcsock)
3270 {
3271 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
3272 struct smc_sock *smc;
3273 struct sock *sk;
3274 int rc;
3275
3276 rc = -ESOCKTNOSUPPORT;
3277 if (sock->type != SOCK_STREAM)
3278 goto out;
3279
3280 rc = -EPROTONOSUPPORT;
3281 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
3282 goto out;
3283
3284 rc = -ENOBUFS;
3285 sock->ops = &smc_sock_ops;
3286 sock->state = SS_UNCONNECTED;
3287 sk = smc_sock_alloc(net, sock, protocol);
3288 if (!sk)
3289 goto out;
3290
3291 /* create internal TCP socket for CLC handshake and fallback */
3292 smc = smc_sk(sk);
3293 smc->use_fallback = false; /* assume rdma capability first */
3294 smc->fallback_rsn = 0;
3295
3296 /* default behavior from limit_smc_hs in every net namespace */
3297 smc->limit_smc_hs = net->smc.limit_smc_hs;
3298
3299 rc = 0;
3300 if (!clcsock) {
3301 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
3302 &smc->clcsock);
3303 if (rc) {
3304 sk_common_release(sk);
3305 goto out;
3306 }
3307
3308 /* smc_clcsock_release() does not wait smc->clcsock->sk's
3309 * destruction; its sk_state might not be TCP_CLOSE after
3310 * smc->sk is close()d, and TCP timers can be fired later,
3311 * which need net ref.
3312 */
3313 sk = smc->clcsock->sk;
3314 __netns_tracker_free(net, &sk->ns_tracker, false);
3315 sk->sk_net_refcnt = 1;
3316 get_net_track(net, &sk->ns_tracker, GFP_KERNEL);
3317 sock_inuse_add(net, 1);
3318 } else {
3319 smc->clcsock = clcsock;
3320 }
3321
3322 out:
3323 return rc;
3324 }
3325
smc_create(struct net * net,struct socket * sock,int protocol,int kern)3326 static int smc_create(struct net *net, struct socket *sock, int protocol,
3327 int kern)
3328 {
3329 return __smc_create(net, sock, protocol, kern, NULL);
3330 }
3331
3332 static const struct net_proto_family smc_sock_family_ops = {
3333 .family = PF_SMC,
3334 .owner = THIS_MODULE,
3335 .create = smc_create,
3336 };
3337
smc_ulp_init(struct sock * sk)3338 static int smc_ulp_init(struct sock *sk)
3339 {
3340 struct socket *tcp = sk->sk_socket;
3341 struct net *net = sock_net(sk);
3342 struct socket *smcsock;
3343 int protocol, ret;
3344
3345 /* only TCP can be replaced */
3346 if (tcp->type != SOCK_STREAM || sk->sk_protocol != IPPROTO_TCP ||
3347 (sk->sk_family != AF_INET && sk->sk_family != AF_INET6))
3348 return -ESOCKTNOSUPPORT;
3349 /* don't handle wq now */
3350 if (tcp->state != SS_UNCONNECTED || !tcp->file || tcp->wq.fasync_list)
3351 return -ENOTCONN;
3352
3353 if (sk->sk_family == AF_INET)
3354 protocol = SMCPROTO_SMC;
3355 else
3356 protocol = SMCPROTO_SMC6;
3357
3358 smcsock = sock_alloc();
3359 if (!smcsock)
3360 return -ENFILE;
3361
3362 smcsock->type = SOCK_STREAM;
3363 __module_get(THIS_MODULE); /* tried in __tcp_ulp_find_autoload */
3364 ret = __smc_create(net, smcsock, protocol, 1, tcp);
3365 if (ret) {
3366 sock_release(smcsock); /* module_put() which ops won't be NULL */
3367 return ret;
3368 }
3369
3370 /* replace tcp socket to smc */
3371 smcsock->file = tcp->file;
3372 smcsock->file->private_data = smcsock;
3373 smcsock->file->f_inode = SOCK_INODE(smcsock); /* replace inode when sock_close */
3374 smcsock->file->f_path.dentry->d_inode = SOCK_INODE(smcsock); /* dput() in __fput */
3375 tcp->file = NULL;
3376
3377 return ret;
3378 }
3379
smc_ulp_clone(const struct request_sock * req,struct sock * newsk,const gfp_t priority)3380 static void smc_ulp_clone(const struct request_sock *req, struct sock *newsk,
3381 const gfp_t priority)
3382 {
3383 struct inet_connection_sock *icsk = inet_csk(newsk);
3384
3385 /* don't inherit ulp ops to child when listen */
3386 icsk->icsk_ulp_ops = NULL;
3387 }
3388
3389 static struct tcp_ulp_ops smc_ulp_ops __read_mostly = {
3390 .name = "smc",
3391 .owner = THIS_MODULE,
3392 .init = smc_ulp_init,
3393 .clone = smc_ulp_clone,
3394 };
3395
3396 unsigned int smc_net_id;
3397
smc_net_init(struct net * net)3398 static __net_init int smc_net_init(struct net *net)
3399 {
3400 int rc;
3401
3402 rc = smc_sysctl_net_init(net);
3403 if (rc)
3404 return rc;
3405 return smc_pnet_net_init(net);
3406 }
3407
smc_net_exit(struct net * net)3408 static void __net_exit smc_net_exit(struct net *net)
3409 {
3410 smc_sysctl_net_exit(net);
3411 smc_pnet_net_exit(net);
3412 }
3413
smc_net_stat_init(struct net * net)3414 static __net_init int smc_net_stat_init(struct net *net)
3415 {
3416 return smc_stats_init(net);
3417 }
3418
smc_net_stat_exit(struct net * net)3419 static void __net_exit smc_net_stat_exit(struct net *net)
3420 {
3421 smc_stats_exit(net);
3422 }
3423
3424 static struct pernet_operations smc_net_ops = {
3425 .init = smc_net_init,
3426 .exit = smc_net_exit,
3427 .id = &smc_net_id,
3428 .size = sizeof(struct smc_net),
3429 };
3430
3431 static struct pernet_operations smc_net_stat_ops = {
3432 .init = smc_net_stat_init,
3433 .exit = smc_net_stat_exit,
3434 };
3435
smc_init(void)3436 static int __init smc_init(void)
3437 {
3438 int rc;
3439
3440 rc = register_pernet_subsys(&smc_net_ops);
3441 if (rc)
3442 return rc;
3443
3444 rc = register_pernet_subsys(&smc_net_stat_ops);
3445 if (rc)
3446 goto out_pernet_subsys;
3447
3448 rc = smc_ism_init();
3449 if (rc)
3450 goto out_pernet_subsys_stat;
3451 smc_clc_init();
3452
3453 rc = smc_nl_init();
3454 if (rc)
3455 goto out_ism;
3456
3457 rc = smc_pnet_init();
3458 if (rc)
3459 goto out_nl;
3460
3461 rc = -ENOMEM;
3462
3463 smc_tcp_ls_wq = alloc_workqueue("smc_tcp_ls_wq", 0, 0);
3464 if (!smc_tcp_ls_wq)
3465 goto out_pnet;
3466
3467 smc_hs_wq = alloc_workqueue("smc_hs_wq", 0, 0);
3468 if (!smc_hs_wq)
3469 goto out_alloc_tcp_ls_wq;
3470
3471 smc_close_wq = alloc_workqueue("smc_close_wq", 0, 0);
3472 if (!smc_close_wq)
3473 goto out_alloc_hs_wq;
3474
3475 rc = smc_core_init();
3476 if (rc) {
3477 pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
3478 goto out_alloc_wqs;
3479 }
3480
3481 rc = smc_llc_init();
3482 if (rc) {
3483 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
3484 goto out_core;
3485 }
3486
3487 rc = smc_cdc_init();
3488 if (rc) {
3489 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
3490 goto out_core;
3491 }
3492
3493 rc = proto_register(&smc_proto, 1);
3494 if (rc) {
3495 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
3496 goto out_core;
3497 }
3498
3499 rc = proto_register(&smc_proto6, 1);
3500 if (rc) {
3501 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
3502 goto out_proto;
3503 }
3504
3505 rc = sock_register(&smc_sock_family_ops);
3506 if (rc) {
3507 pr_err("%s: sock_register fails with %d\n", __func__, rc);
3508 goto out_proto6;
3509 }
3510 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
3511 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
3512
3513 rc = smc_ib_register_client();
3514 if (rc) {
3515 pr_err("%s: ib_register fails with %d\n", __func__, rc);
3516 goto out_sock;
3517 }
3518
3519 rc = tcp_register_ulp(&smc_ulp_ops);
3520 if (rc) {
3521 pr_err("%s: tcp_ulp_register fails with %d\n", __func__, rc);
3522 goto out_ib;
3523 }
3524
3525 static_branch_enable(&tcp_have_smc);
3526 return 0;
3527
3528 out_ib:
3529 smc_ib_unregister_client();
3530 out_sock:
3531 sock_unregister(PF_SMC);
3532 out_proto6:
3533 proto_unregister(&smc_proto6);
3534 out_proto:
3535 proto_unregister(&smc_proto);
3536 out_core:
3537 smc_core_exit();
3538 out_alloc_wqs:
3539 destroy_workqueue(smc_close_wq);
3540 out_alloc_hs_wq:
3541 destroy_workqueue(smc_hs_wq);
3542 out_alloc_tcp_ls_wq:
3543 destroy_workqueue(smc_tcp_ls_wq);
3544 out_pnet:
3545 smc_pnet_exit();
3546 out_nl:
3547 smc_nl_exit();
3548 out_ism:
3549 smc_clc_exit();
3550 smc_ism_exit();
3551 out_pernet_subsys_stat:
3552 unregister_pernet_subsys(&smc_net_stat_ops);
3553 out_pernet_subsys:
3554 unregister_pernet_subsys(&smc_net_ops);
3555
3556 return rc;
3557 }
3558
smc_exit(void)3559 static void __exit smc_exit(void)
3560 {
3561 static_branch_disable(&tcp_have_smc);
3562 tcp_unregister_ulp(&smc_ulp_ops);
3563 sock_unregister(PF_SMC);
3564 smc_core_exit();
3565 smc_ib_unregister_client();
3566 smc_ism_exit();
3567 destroy_workqueue(smc_close_wq);
3568 destroy_workqueue(smc_tcp_ls_wq);
3569 destroy_workqueue(smc_hs_wq);
3570 proto_unregister(&smc_proto6);
3571 proto_unregister(&smc_proto);
3572 smc_pnet_exit();
3573 smc_nl_exit();
3574 smc_clc_exit();
3575 unregister_pernet_subsys(&smc_net_stat_ops);
3576 unregister_pernet_subsys(&smc_net_ops);
3577 rcu_barrier();
3578 }
3579
3580 module_init(smc_init);
3581 module_exit(smc_exit);
3582
3583 MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
3584 MODULE_DESCRIPTION("smc socket address family");
3585 MODULE_LICENSE("GPL");
3586 MODULE_ALIAS_NETPROTO(PF_SMC);
3587 MODULE_ALIAS_TCP_ULP("smc");
3588 MODULE_ALIAS_GENL_FAMILY(SMC_GENL_FAMILY_NAME);
3589