1 // SPDX-License-Identifier: GPL-2.0-only
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
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
10 * Initial restrictions:
11 * - support for alternate links postponed
13 * Copyright IBM Corp. 2016, 2018
15 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
16 * based on prototype from Frank Blaschka
19 #define KMSG_COMPONENT "smc"
20 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
22 #include <linux/module.h>
23 #include <linux/socket.h>
24 #include <linux/workqueue.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>
35 #include <asm/ioctls.h>
37 #include <net/net_namespace.h>
38 #include <net/netns/generic.h>
39 #include "smc_netns.h"
49 #include "smc_netlink.h"
52 #include "smc_close.h"
53 #include "smc_stats.h"
54 #include "smc_tracepoint.h"
55 #include "smc_sysctl.h"
57 static DEFINE_MUTEX(smc_server_lgr_pending); /* serialize link group
60 static DEFINE_MUTEX(smc_client_lgr_pending); /* serialize link group
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 */
68 static void smc_tcp_listen_work(struct work_struct *);
69 static void smc_connect_work(struct work_struct *);
71 int smc_nl_dump_hs_limitation(struct sk_buff *skb, struct netlink_callback *cb)
73 struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb);
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);
85 if (nla_put_u8(skb, SMC_NLA_HS_LIMITATION_ENABLED,
86 sock_net(skb->sk)->smc.limit_smc_hs))
89 genlmsg_end(skb, hdr);
94 genlmsg_cancel(skb, hdr);
98 int smc_nl_enable_hs_limitation(struct sk_buff *skb, struct genl_info *info)
100 sock_net(skb->sk)->smc.limit_smc_hs = true;
104 int smc_nl_disable_hs_limitation(struct sk_buff *skb, struct genl_info *info)
106 sock_net(skb->sk)->smc.limit_smc_hs = false;
110 static void smc_set_keepalive(struct sock *sk, int val)
112 struct smc_sock *smc = smc_sk(sk);
114 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
117 static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk,
119 struct request_sock *req,
120 struct dst_entry *dst,
121 struct request_sock *req_unhash,
124 struct smc_sock *smc;
127 smc = smc_clcsock_user_data(sk);
129 if (READ_ONCE(sk->sk_ack_backlog) + atomic_read(&smc->queued_smc_hs) >
130 sk->sk_max_ack_backlog)
133 if (sk_acceptq_is_full(&smc->sk)) {
134 NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
138 /* passthrough to original syn recv sock fct */
139 child = smc->ori_af_ops->syn_recv_sock(sk, skb, req, dst, req_unhash,
141 /* child must not inherit smc or its ops */
143 rcu_assign_sk_user_data(child, NULL);
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;
157 static bool smc_hs_congested(const struct sock *sk)
159 const struct smc_sock *smc;
161 smc = smc_clcsock_user_data(sk);
166 if (workqueue_congested(WORK_CPU_UNBOUND, smc_hs_wq))
172 static struct smc_hashinfo smc_v4_hashinfo = {
173 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
176 static struct smc_hashinfo smc_v6_hashinfo = {
177 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
180 int smc_hash_sk(struct sock *sk)
182 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
183 struct hlist_head *head;
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);
194 EXPORT_SYMBOL_GPL(smc_hash_sk);
196 void smc_unhash_sk(struct sock *sk)
198 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
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);
205 EXPORT_SYMBOL_GPL(smc_unhash_sk);
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
211 static void smc_release_cb(struct sock *sk)
213 struct smc_sock *smc = smc_sk(sk);
215 if (smc->conn.tx_in_release_sock) {
216 smc_tx_pending(&smc->conn);
217 smc->conn.tx_in_release_sock = false;
221 struct proto smc_proto = {
223 .owner = THIS_MODULE,
224 .keepalive = smc_set_keepalive,
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,
232 EXPORT_SYMBOL_GPL(smc_proto);
234 struct proto smc_proto6 = {
236 .owner = THIS_MODULE,
237 .keepalive = smc_set_keepalive,
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,
245 EXPORT_SYMBOL_GPL(smc_proto6);
247 static void smc_fback_restore_callbacks(struct smc_sock *smc)
249 struct sock *clcsk = smc->clcsock->sk;
251 write_lock_bh(&clcsk->sk_callback_lock);
252 clcsk->sk_user_data = NULL;
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);
259 write_unlock_bh(&clcsk->sk_callback_lock);
262 static void smc_restore_fallback_changes(struct smc_sock *smc)
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);
271 static int __smc_release(struct smc_sock *smc)
273 struct sock *sk = &smc->sk;
276 if (!smc->use_fallback) {
277 rc = smc_close_active(smc);
278 sock_set_flag(sk, SOCK_DEAD);
279 sk->sk_shutdown |= SHUTDOWN_MASK;
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,
290 sk->sk_state = SMC_CLOSED;
291 sk->sk_state_change(sk);
293 smc_restore_fallback_changes(smc);
296 sk->sk_prot->unhash(sk);
298 if (sk->sk_state == SMC_CLOSED) {
301 smc_clcsock_release(smc);
304 if (!smc->use_fallback)
305 smc_conn_free(&smc->conn);
311 static int smc_release(struct socket *sock)
313 struct sock *sk = sock->sk;
314 struct smc_sock *smc;
315 int old_state, rc = 0;
320 sock_hold(sk); /* sock_put below */
323 old_state = sk->sk_state;
325 /* cleanup for a dangling non-blocking connect */
326 if (smc->connect_nonblock && old_state == SMC_INIT)
327 tcp_abort(smc->clcsock->sk, ECONNABORTED);
329 if (cancel_work_sync(&smc->connect_work))
330 sock_put(&smc->sk); /* sock_hold in smc_connect for passive closing */
332 if (sk->sk_state == SMC_LISTEN)
333 /* smc_close_non_accepted() is called and acquires
334 * sock lock for child sockets again
336 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
340 if (old_state == SMC_INIT && sk->sk_state == SMC_ACTIVE &&
342 smc_close_active_abort(smc);
344 rc = __smc_release(smc);
351 sock_put(sk); /* sock_hold above */
352 sock_put(sk); /* final sock_put */
357 static void smc_destruct(struct sock *sk)
359 if (sk->sk_state != SMC_CLOSED)
361 if (!sock_flag(sk, SOCK_DEAD))
365 static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
368 struct smc_sock *smc;
372 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
373 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
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, READ_ONCE(net->smc.sysctl_wmem));
382 WRITE_ONCE(sk->sk_rcvbuf, READ_ONCE(net->smc.sysctl_rmem));
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);
397 static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
400 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
401 struct sock *sk = sock->sk;
402 struct smc_sock *smc;
407 /* replicate tests from inet_bind(), to be safe wrt. future changes */
409 if (addr_len < sizeof(struct sockaddr_in))
413 if (addr->sin_family != AF_INET &&
414 addr->sin_family != AF_INET6 &&
415 addr->sin_family != AF_UNSPEC)
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))
424 /* Check if socket is already active */
426 if (sk->sk_state != SMC_INIT || smc->connect_nonblock)
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);
439 static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
442 /* options we don't get control via setsockopt for */
443 nsk->sk_type = osk->sk_type;
444 nsk->sk_sndbuf = osk->sk_sndbuf;
445 nsk->sk_rcvbuf = osk->sk_rcvbuf;
446 nsk->sk_sndtimeo = osk->sk_sndtimeo;
447 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
448 nsk->sk_mark = osk->sk_mark;
449 nsk->sk_priority = osk->sk_priority;
450 nsk->sk_rcvlowat = osk->sk_rcvlowat;
451 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
452 nsk->sk_err = osk->sk_err;
454 nsk->sk_flags &= ~mask;
455 nsk->sk_flags |= osk->sk_flags & mask;
458 #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
459 (1UL << SOCK_KEEPOPEN) | \
460 (1UL << SOCK_LINGER) | \
461 (1UL << SOCK_BROADCAST) | \
462 (1UL << SOCK_TIMESTAMP) | \
463 (1UL << SOCK_DBG) | \
464 (1UL << SOCK_RCVTSTAMP) | \
465 (1UL << SOCK_RCVTSTAMPNS) | \
466 (1UL << SOCK_LOCALROUTE) | \
467 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
468 (1UL << SOCK_RXQ_OVFL) | \
469 (1UL << SOCK_WIFI_STATUS) | \
470 (1UL << SOCK_NOFCS) | \
471 (1UL << SOCK_FILTER_LOCKED) | \
472 (1UL << SOCK_TSTAMP_NEW))
473 /* copy only relevant settings and flags of SOL_SOCKET level from smc to
474 * clc socket (since smc is not called for these options from net/core)
476 static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
478 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
481 #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
482 (1UL << SOCK_KEEPOPEN) | \
483 (1UL << SOCK_LINGER) | \
485 /* copy only settings and flags relevant for smc from clc to smc socket */
486 static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
488 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
491 /* register the new vzalloced sndbuf on all links */
492 static int smcr_lgr_reg_sndbufs(struct smc_link *link,
493 struct smc_buf_desc *snd_desc)
495 struct smc_link_group *lgr = link->lgr;
498 if (!snd_desc->is_vm)
501 /* protect against parallel smcr_link_reg_buf() */
502 down_write(&lgr->llc_conf_mutex);
503 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
504 if (!smc_link_active(&lgr->lnk[i]))
506 rc = smcr_link_reg_buf(&lgr->lnk[i], snd_desc);
510 up_write(&lgr->llc_conf_mutex);
514 /* register the new rmb on all links */
515 static int smcr_lgr_reg_rmbs(struct smc_link *link,
516 struct smc_buf_desc *rmb_desc)
518 struct smc_link_group *lgr = link->lgr;
519 bool do_slow = false;
522 rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY);
526 down_read(&lgr->llc_conf_mutex);
527 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
528 if (!smc_link_active(&lgr->lnk[i]))
530 if (!rmb_desc->is_reg_mr[link->link_idx]) {
531 up_read(&lgr->llc_conf_mutex);
535 /* mr register already */
539 /* protect against parallel smc_llc_cli_rkey_exchange() and
540 * parallel smcr_link_reg_buf()
542 down_write(&lgr->llc_conf_mutex);
543 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
544 if (!smc_link_active(&lgr->lnk[i]))
546 rc = smcr_link_reg_buf(&lgr->lnk[i], rmb_desc);
551 /* exchange confirm_rkey msg with peer */
552 rc = smc_llc_do_confirm_rkey(link, rmb_desc);
557 rmb_desc->is_conf_rkey = true;
559 do_slow ? up_write(&lgr->llc_conf_mutex) : up_read(&lgr->llc_conf_mutex);
560 smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
564 static int smcr_clnt_conf_first_link(struct smc_sock *smc)
566 struct smc_link *link = smc->conn.lnk;
567 struct smc_llc_qentry *qentry;
570 /* receive CONFIRM LINK request from server over RoCE fabric */
571 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
572 SMC_LLC_CONFIRM_LINK);
574 struct smc_clc_msg_decline dclc;
576 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
577 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
578 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
580 smc_llc_save_peer_uid(qentry);
581 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_REQ);
582 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
584 return SMC_CLC_DECL_RMBE_EC;
586 rc = smc_ib_modify_qp_rts(link);
588 return SMC_CLC_DECL_ERR_RDYLNK;
590 smc_wr_remember_qp_attr(link);
592 /* reg the sndbuf if it was vzalloced */
593 if (smc->conn.sndbuf_desc->is_vm) {
594 if (smcr_link_reg_buf(link, smc->conn.sndbuf_desc))
595 return SMC_CLC_DECL_ERR_REGBUF;
599 if (smcr_link_reg_buf(link, smc->conn.rmb_desc))
600 return SMC_CLC_DECL_ERR_REGBUF;
602 /* confirm_rkey is implicit on 1st contact */
603 smc->conn.rmb_desc->is_conf_rkey = true;
605 /* send CONFIRM LINK response over RoCE fabric */
606 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
608 return SMC_CLC_DECL_TIMEOUT_CL;
610 smc_llc_link_active(link);
611 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
613 /* optional 2nd link, receive ADD LINK request from server */
614 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
617 struct smc_clc_msg_decline dclc;
619 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
620 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
622 rc = 0; /* no DECLINE received, go with one link */
625 smc_llc_flow_qentry_clr(&link->lgr->llc_flow_lcl);
626 smc_llc_cli_add_link(link, qentry);
630 static bool smc_isascii(char *hostname)
634 for (i = 0; i < SMC_MAX_HOSTNAME_LEN; i++)
635 if (!isascii(hostname[i]))
640 static void smc_conn_save_peer_info_fce(struct smc_sock *smc,
641 struct smc_clc_msg_accept_confirm *clc)
643 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
644 (struct smc_clc_msg_accept_confirm_v2 *)clc;
645 struct smc_clc_first_contact_ext *fce;
648 if (clc->hdr.version == SMC_V1 ||
649 !(clc->hdr.typev2 & SMC_FIRST_CONTACT_MASK))
652 if (smc->conn.lgr->is_smcd) {
653 memcpy(smc->conn.lgr->negotiated_eid, clc_v2->d1.eid,
655 clc_v2_len = offsetofend(struct smc_clc_msg_accept_confirm_v2,
658 memcpy(smc->conn.lgr->negotiated_eid, clc_v2->r1.eid,
660 clc_v2_len = offsetofend(struct smc_clc_msg_accept_confirm_v2,
663 fce = (struct smc_clc_first_contact_ext *)(((u8 *)clc_v2) + clc_v2_len);
664 smc->conn.lgr->peer_os = fce->os_type;
665 smc->conn.lgr->peer_smc_release = fce->release;
666 if (smc_isascii(fce->hostname))
667 memcpy(smc->conn.lgr->peer_hostname, fce->hostname,
668 SMC_MAX_HOSTNAME_LEN);
671 static void smcr_conn_save_peer_info(struct smc_sock *smc,
672 struct smc_clc_msg_accept_confirm *clc)
674 int bufsize = smc_uncompress_bufsize(clc->r0.rmbe_size);
676 smc->conn.peer_rmbe_idx = clc->r0.rmbe_idx;
677 smc->conn.local_tx_ctrl.token = ntohl(clc->r0.rmbe_alert_token);
678 smc->conn.peer_rmbe_size = bufsize;
679 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
680 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
683 static void smcd_conn_save_peer_info(struct smc_sock *smc,
684 struct smc_clc_msg_accept_confirm *clc)
686 int bufsize = smc_uncompress_bufsize(clc->d0.dmbe_size);
688 smc->conn.peer_rmbe_idx = clc->d0.dmbe_idx;
689 smc->conn.peer_token = clc->d0.token;
690 /* msg header takes up space in the buffer */
691 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
692 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
693 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
696 static void smc_conn_save_peer_info(struct smc_sock *smc,
697 struct smc_clc_msg_accept_confirm *clc)
699 if (smc->conn.lgr->is_smcd)
700 smcd_conn_save_peer_info(smc, clc);
702 smcr_conn_save_peer_info(smc, clc);
703 smc_conn_save_peer_info_fce(smc, clc);
706 static void smc_link_save_peer_info(struct smc_link *link,
707 struct smc_clc_msg_accept_confirm *clc,
708 struct smc_init_info *ini)
710 link->peer_qpn = ntoh24(clc->r0.qpn);
711 memcpy(link->peer_gid, ini->peer_gid, SMC_GID_SIZE);
712 memcpy(link->peer_mac, ini->peer_mac, sizeof(link->peer_mac));
713 link->peer_psn = ntoh24(clc->r0.psn);
714 link->peer_mtu = clc->r0.qp_mtu;
717 static void smc_stat_inc_fback_rsn_cnt(struct smc_sock *smc,
718 struct smc_stats_fback *fback_arr)
722 for (cnt = 0; cnt < SMC_MAX_FBACK_RSN_CNT; cnt++) {
723 if (fback_arr[cnt].fback_code == smc->fallback_rsn) {
724 fback_arr[cnt].count++;
727 if (!fback_arr[cnt].fback_code) {
728 fback_arr[cnt].fback_code = smc->fallback_rsn;
729 fback_arr[cnt].count++;
735 static void smc_stat_fallback(struct smc_sock *smc)
737 struct net *net = sock_net(&smc->sk);
739 mutex_lock(&net->smc.mutex_fback_rsn);
740 if (smc->listen_smc) {
741 smc_stat_inc_fback_rsn_cnt(smc, net->smc.fback_rsn->srv);
742 net->smc.fback_rsn->srv_fback_cnt++;
744 smc_stat_inc_fback_rsn_cnt(smc, net->smc.fback_rsn->clnt);
745 net->smc.fback_rsn->clnt_fback_cnt++;
747 mutex_unlock(&net->smc.mutex_fback_rsn);
750 /* must be called under rcu read lock */
751 static void smc_fback_wakeup_waitqueue(struct smc_sock *smc, void *key)
753 struct socket_wq *wq;
756 wq = rcu_dereference(smc->sk.sk_wq);
757 if (!skwq_has_sleeper(wq))
760 /* wake up smc sk->sk_wq */
762 /* sk_state_change */
763 wake_up_interruptible_all(&wq->wait);
765 flags = key_to_poll(key);
766 if (flags & (EPOLLIN | EPOLLOUT))
767 /* sk_data_ready or sk_write_space */
768 wake_up_interruptible_sync_poll(&wq->wait, flags);
769 else if (flags & EPOLLERR)
770 /* sk_error_report */
771 wake_up_interruptible_poll(&wq->wait, flags);
775 static int smc_fback_mark_woken(wait_queue_entry_t *wait,
776 unsigned int mode, int sync, void *key)
778 struct smc_mark_woken *mark =
779 container_of(wait, struct smc_mark_woken, wait_entry);
786 static void smc_fback_forward_wakeup(struct smc_sock *smc, struct sock *clcsk,
787 void (*clcsock_callback)(struct sock *sk))
789 struct smc_mark_woken mark = { .woken = false };
790 struct socket_wq *wq;
792 init_waitqueue_func_entry(&mark.wait_entry,
793 smc_fback_mark_woken);
795 wq = rcu_dereference(clcsk->sk_wq);
798 add_wait_queue(sk_sleep(clcsk), &mark.wait_entry);
799 clcsock_callback(clcsk);
800 remove_wait_queue(sk_sleep(clcsk), &mark.wait_entry);
803 smc_fback_wakeup_waitqueue(smc, mark.key);
808 static void smc_fback_state_change(struct sock *clcsk)
810 struct smc_sock *smc;
812 read_lock_bh(&clcsk->sk_callback_lock);
813 smc = smc_clcsock_user_data(clcsk);
815 smc_fback_forward_wakeup(smc, clcsk,
816 smc->clcsk_state_change);
817 read_unlock_bh(&clcsk->sk_callback_lock);
820 static void smc_fback_data_ready(struct sock *clcsk)
822 struct smc_sock *smc;
824 read_lock_bh(&clcsk->sk_callback_lock);
825 smc = smc_clcsock_user_data(clcsk);
827 smc_fback_forward_wakeup(smc, clcsk,
828 smc->clcsk_data_ready);
829 read_unlock_bh(&clcsk->sk_callback_lock);
832 static void smc_fback_write_space(struct sock *clcsk)
834 struct smc_sock *smc;
836 read_lock_bh(&clcsk->sk_callback_lock);
837 smc = smc_clcsock_user_data(clcsk);
839 smc_fback_forward_wakeup(smc, clcsk,
840 smc->clcsk_write_space);
841 read_unlock_bh(&clcsk->sk_callback_lock);
844 static void smc_fback_error_report(struct sock *clcsk)
846 struct smc_sock *smc;
848 read_lock_bh(&clcsk->sk_callback_lock);
849 smc = smc_clcsock_user_data(clcsk);
851 smc_fback_forward_wakeup(smc, clcsk,
852 smc->clcsk_error_report);
853 read_unlock_bh(&clcsk->sk_callback_lock);
856 static void smc_fback_replace_callbacks(struct smc_sock *smc)
858 struct sock *clcsk = smc->clcsock->sk;
860 write_lock_bh(&clcsk->sk_callback_lock);
861 clcsk->sk_user_data = (void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY);
863 smc_clcsock_replace_cb(&clcsk->sk_state_change, smc_fback_state_change,
864 &smc->clcsk_state_change);
865 smc_clcsock_replace_cb(&clcsk->sk_data_ready, smc_fback_data_ready,
866 &smc->clcsk_data_ready);
867 smc_clcsock_replace_cb(&clcsk->sk_write_space, smc_fback_write_space,
868 &smc->clcsk_write_space);
869 smc_clcsock_replace_cb(&clcsk->sk_error_report, smc_fback_error_report,
870 &smc->clcsk_error_report);
872 write_unlock_bh(&clcsk->sk_callback_lock);
875 static int smc_switch_to_fallback(struct smc_sock *smc, int reason_code)
879 mutex_lock(&smc->clcsock_release_lock);
885 smc->use_fallback = true;
886 smc->fallback_rsn = reason_code;
887 smc_stat_fallback(smc);
888 trace_smc_switch_to_fallback(smc, reason_code);
889 if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
890 smc->clcsock->file = smc->sk.sk_socket->file;
891 smc->clcsock->file->private_data = smc->clcsock;
892 smc->clcsock->wq.fasync_list =
893 smc->sk.sk_socket->wq.fasync_list;
895 /* There might be some wait entries remaining
896 * in smc sk->sk_wq and they should be woken up
897 * as clcsock's wait queue is woken up.
899 smc_fback_replace_callbacks(smc);
902 mutex_unlock(&smc->clcsock_release_lock);
906 /* fall back during connect */
907 static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
909 struct net *net = sock_net(&smc->sk);
912 rc = smc_switch_to_fallback(smc, reason_code);
913 if (rc) { /* fallback fails */
914 this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
915 if (smc->sk.sk_state == SMC_INIT)
916 sock_put(&smc->sk); /* passive closing */
919 smc_copy_sock_settings_to_clc(smc);
920 smc->connect_nonblock = 0;
921 if (smc->sk.sk_state == SMC_INIT)
922 smc->sk.sk_state = SMC_ACTIVE;
926 /* decline and fall back during connect */
927 static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code,
930 struct net *net = sock_net(&smc->sk);
933 if (reason_code < 0) { /* error, fallback is not possible */
934 this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
935 if (smc->sk.sk_state == SMC_INIT)
936 sock_put(&smc->sk); /* passive closing */
939 if (reason_code != SMC_CLC_DECL_PEERDECL) {
940 rc = smc_clc_send_decline(smc, reason_code, version);
942 this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
943 if (smc->sk.sk_state == SMC_INIT)
944 sock_put(&smc->sk); /* passive closing */
948 return smc_connect_fallback(smc, reason_code);
951 static void smc_conn_abort(struct smc_sock *smc, int local_first)
953 struct smc_connection *conn = &smc->conn;
954 struct smc_link_group *lgr = conn->lgr;
955 bool lgr_valid = false;
957 if (smc_conn_lgr_valid(conn))
961 if (local_first && lgr_valid)
962 smc_lgr_cleanup_early(lgr);
965 /* check if there is a rdma device available for this connection. */
966 /* called for connect and listen */
967 static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
969 /* PNET table look up: search active ib_device and port
970 * within same PNETID that also contains the ethernet device
971 * used for the internal TCP socket
973 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
974 if (!ini->check_smcrv2 && !ini->ib_dev)
975 return SMC_CLC_DECL_NOSMCRDEV;
976 if (ini->check_smcrv2 && !ini->smcrv2.ib_dev_v2)
977 return SMC_CLC_DECL_NOSMCRDEV;
981 /* check if there is an ISM device available for this connection. */
982 /* called for connect and listen */
983 static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
985 /* Find ISM device with same PNETID as connecting interface */
986 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
987 if (!ini->ism_dev[0])
988 return SMC_CLC_DECL_NOSMCDDEV;
990 ini->ism_chid[0] = smc_ism_get_chid(ini->ism_dev[0]);
994 /* is chid unique for the ism devices that are already determined? */
995 static bool smc_find_ism_v2_is_unique_chid(u16 chid, struct smc_init_info *ini,
998 int i = (!ini->ism_dev[0]) ? 1 : 0;
1000 for (; i < cnt; i++)
1001 if (ini->ism_chid[i] == chid)
1006 /* determine possible V2 ISM devices (either without PNETID or with PNETID plus
1007 * PNETID matching net_device)
1009 static int smc_find_ism_v2_device_clnt(struct smc_sock *smc,
1010 struct smc_init_info *ini)
1012 int rc = SMC_CLC_DECL_NOSMCDDEV;
1013 struct smcd_dev *smcd;
1017 if (smcd_indicated(ini->smc_type_v1))
1018 rc = 0; /* already initialized for V1 */
1019 mutex_lock(&smcd_dev_list.mutex);
1020 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
1021 if (smcd->going_away || smcd == ini->ism_dev[0])
1023 chid = smc_ism_get_chid(smcd);
1024 if (!smc_find_ism_v2_is_unique_chid(chid, ini, i))
1026 if (!smc_pnet_is_pnetid_set(smcd->pnetid) ||
1027 smc_pnet_is_ndev_pnetid(sock_net(&smc->sk), smcd->pnetid)) {
1028 ini->ism_dev[i] = smcd;
1029 ini->ism_chid[i] = chid;
1030 ini->is_smcd = true;
1033 if (i > SMC_MAX_ISM_DEVS)
1037 mutex_unlock(&smcd_dev_list.mutex);
1038 ini->ism_offered_cnt = i - 1;
1039 if (!ini->ism_dev[0] && !ini->ism_dev[1])
1040 ini->smcd_version = 0;
1045 /* Check for VLAN ID and register it on ISM device just for CLC handshake */
1046 static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
1047 struct smc_init_info *ini)
1049 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev[0], ini->vlan_id))
1050 return SMC_CLC_DECL_ISMVLANERR;
1054 static int smc_find_proposal_devices(struct smc_sock *smc,
1055 struct smc_init_info *ini)
1059 /* check if there is an ism device available */
1060 if (!(ini->smcd_version & SMC_V1) ||
1061 smc_find_ism_device(smc, ini) ||
1062 smc_connect_ism_vlan_setup(smc, ini))
1063 ini->smcd_version &= ~SMC_V1;
1064 /* else ISM V1 is supported for this connection */
1066 /* check if there is an rdma device available */
1067 if (!(ini->smcr_version & SMC_V1) ||
1068 smc_find_rdma_device(smc, ini))
1069 ini->smcr_version &= ~SMC_V1;
1070 /* else RDMA is supported for this connection */
1072 ini->smc_type_v1 = smc_indicated_type(ini->smcd_version & SMC_V1,
1073 ini->smcr_version & SMC_V1);
1075 /* check if there is an ism v2 device available */
1076 if (!(ini->smcd_version & SMC_V2) ||
1077 !smc_ism_is_v2_capable() ||
1078 smc_find_ism_v2_device_clnt(smc, ini))
1079 ini->smcd_version &= ~SMC_V2;
1081 /* check if there is an rdma v2 device available */
1082 ini->check_smcrv2 = true;
1083 ini->smcrv2.saddr = smc->clcsock->sk->sk_rcv_saddr;
1084 if (!(ini->smcr_version & SMC_V2) ||
1085 smc->clcsock->sk->sk_family != AF_INET ||
1086 !smc_clc_ueid_count() ||
1087 smc_find_rdma_device(smc, ini))
1088 ini->smcr_version &= ~SMC_V2;
1089 ini->check_smcrv2 = false;
1091 ini->smc_type_v2 = smc_indicated_type(ini->smcd_version & SMC_V2,
1092 ini->smcr_version & SMC_V2);
1094 /* if neither ISM nor RDMA are supported, fallback */
1095 if (ini->smc_type_v1 == SMC_TYPE_N && ini->smc_type_v2 == SMC_TYPE_N)
1096 rc = SMC_CLC_DECL_NOSMCDEV;
1101 /* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
1102 * used, the VLAN ID will be registered again during the connection setup.
1104 static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc,
1105 struct smc_init_info *ini)
1107 if (!smcd_indicated(ini->smc_type_v1))
1109 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev[0], ini->vlan_id))
1110 return SMC_CLC_DECL_CNFERR;
1114 #define SMC_CLC_MAX_ACCEPT_LEN \
1115 (sizeof(struct smc_clc_msg_accept_confirm_v2) + \
1116 sizeof(struct smc_clc_first_contact_ext) + \
1117 sizeof(struct smc_clc_msg_trail))
1119 /* CLC handshake during connect */
1120 static int smc_connect_clc(struct smc_sock *smc,
1121 struct smc_clc_msg_accept_confirm_v2 *aclc2,
1122 struct smc_init_info *ini)
1126 /* do inband token exchange */
1127 rc = smc_clc_send_proposal(smc, ini);
1130 /* receive SMC Accept CLC message */
1131 return smc_clc_wait_msg(smc, aclc2, SMC_CLC_MAX_ACCEPT_LEN,
1132 SMC_CLC_ACCEPT, CLC_WAIT_TIME);
1135 void smc_fill_gid_list(struct smc_link_group *lgr,
1136 struct smc_gidlist *gidlist,
1137 struct smc_ib_device *known_dev, u8 *known_gid)
1139 struct smc_init_info *alt_ini = NULL;
1141 memset(gidlist, 0, sizeof(*gidlist));
1142 memcpy(gidlist->list[gidlist->len++], known_gid, SMC_GID_SIZE);
1144 alt_ini = kzalloc(sizeof(*alt_ini), GFP_KERNEL);
1148 alt_ini->vlan_id = lgr->vlan_id;
1149 alt_ini->check_smcrv2 = true;
1150 alt_ini->smcrv2.saddr = lgr->saddr;
1151 smc_pnet_find_alt_roce(lgr, alt_ini, known_dev);
1153 if (!alt_ini->smcrv2.ib_dev_v2)
1156 memcpy(gidlist->list[gidlist->len++], alt_ini->smcrv2.ib_gid_v2,
1163 static int smc_connect_rdma_v2_prepare(struct smc_sock *smc,
1164 struct smc_clc_msg_accept_confirm *aclc,
1165 struct smc_init_info *ini)
1167 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
1168 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
1169 struct smc_clc_first_contact_ext *fce =
1170 (struct smc_clc_first_contact_ext *)
1171 (((u8 *)clc_v2) + sizeof(*clc_v2));
1173 if (!ini->first_contact_peer || aclc->hdr.version == SMC_V1)
1176 if (fce->v2_direct) {
1177 memcpy(ini->smcrv2.nexthop_mac, &aclc->r0.lcl.mac, ETH_ALEN);
1178 ini->smcrv2.uses_gateway = false;
1180 if (smc_ib_find_route(smc->clcsock->sk->sk_rcv_saddr,
1181 smc_ib_gid_to_ipv4(aclc->r0.lcl.gid),
1182 ini->smcrv2.nexthop_mac,
1183 &ini->smcrv2.uses_gateway))
1184 return SMC_CLC_DECL_NOROUTE;
1185 if (!ini->smcrv2.uses_gateway) {
1186 /* mismatch: peer claims indirect, but its direct */
1187 return SMC_CLC_DECL_NOINDIRECT;
1193 /* setup for RDMA connection of client */
1194 static int smc_connect_rdma(struct smc_sock *smc,
1195 struct smc_clc_msg_accept_confirm *aclc,
1196 struct smc_init_info *ini)
1198 int i, reason_code = 0;
1199 struct smc_link *link;
1202 ini->is_smcd = false;
1203 ini->ib_clcqpn = ntoh24(aclc->r0.qpn);
1204 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
1205 memcpy(ini->peer_systemid, aclc->r0.lcl.id_for_peer, SMC_SYSTEMID_LEN);
1206 memcpy(ini->peer_gid, aclc->r0.lcl.gid, SMC_GID_SIZE);
1207 memcpy(ini->peer_mac, aclc->r0.lcl.mac, ETH_ALEN);
1209 reason_code = smc_connect_rdma_v2_prepare(smc, aclc, ini);
1213 mutex_lock(&smc_client_lgr_pending);
1214 reason_code = smc_conn_create(smc, ini);
1216 mutex_unlock(&smc_client_lgr_pending);
1220 smc_conn_save_peer_info(smc, aclc);
1222 if (ini->first_contact_local) {
1223 link = smc->conn.lnk;
1225 /* set link that was assigned by server */
1227 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1228 struct smc_link *l = &smc->conn.lgr->lnk[i];
1230 if (l->peer_qpn == ntoh24(aclc->r0.qpn) &&
1231 !memcmp(l->peer_gid, &aclc->r0.lcl.gid,
1233 (aclc->hdr.version > SMC_V1 ||
1234 !memcmp(l->peer_mac, &aclc->r0.lcl.mac,
1235 sizeof(l->peer_mac)))) {
1241 reason_code = SMC_CLC_DECL_NOSRVLINK;
1244 smc_switch_link_and_count(&smc->conn, link);
1247 /* create send buffer and rmb */
1248 if (smc_buf_create(smc, false)) {
1249 reason_code = SMC_CLC_DECL_MEM;
1253 if (ini->first_contact_local)
1254 smc_link_save_peer_info(link, aclc, ini);
1256 if (smc_rmb_rtoken_handling(&smc->conn, link, aclc)) {
1257 reason_code = SMC_CLC_DECL_ERR_RTOK;
1261 smc_close_init(smc);
1264 if (ini->first_contact_local) {
1265 if (smc_ib_ready_link(link)) {
1266 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
1270 /* reg sendbufs if they were vzalloced */
1271 if (smc->conn.sndbuf_desc->is_vm) {
1272 if (smcr_lgr_reg_sndbufs(link, smc->conn.sndbuf_desc)) {
1273 reason_code = SMC_CLC_DECL_ERR_REGBUF;
1277 if (smcr_lgr_reg_rmbs(link, smc->conn.rmb_desc)) {
1278 reason_code = SMC_CLC_DECL_ERR_REGBUF;
1283 if (aclc->hdr.version > SMC_V1) {
1284 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
1285 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
1287 eid = clc_v2->r1.eid;
1288 if (ini->first_contact_local)
1289 smc_fill_gid_list(link->lgr, &ini->smcrv2.gidlist,
1290 link->smcibdev, link->gid);
1293 reason_code = smc_clc_send_confirm(smc, ini->first_contact_local,
1294 aclc->hdr.version, eid, ini);
1300 if (ini->first_contact_local) {
1301 /* QP confirmation over RoCE fabric */
1302 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
1303 reason_code = smcr_clnt_conf_first_link(smc);
1304 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
1308 mutex_unlock(&smc_client_lgr_pending);
1310 smc_copy_sock_settings_to_clc(smc);
1311 smc->connect_nonblock = 0;
1312 if (smc->sk.sk_state == SMC_INIT)
1313 smc->sk.sk_state = SMC_ACTIVE;
1317 smc_conn_abort(smc, ini->first_contact_local);
1318 mutex_unlock(&smc_client_lgr_pending);
1319 smc->connect_nonblock = 0;
1324 /* The server has chosen one of the proposed ISM devices for the communication.
1325 * Determine from the CHID of the received CLC ACCEPT the ISM device chosen.
1328 smc_v2_determine_accepted_chid(struct smc_clc_msg_accept_confirm_v2 *aclc,
1329 struct smc_init_info *ini)
1333 for (i = 0; i < ini->ism_offered_cnt + 1; i++) {
1334 if (ini->ism_chid[i] == ntohs(aclc->d1.chid)) {
1335 ini->ism_selected = i;
1343 /* setup for ISM connection of client */
1344 static int smc_connect_ism(struct smc_sock *smc,
1345 struct smc_clc_msg_accept_confirm *aclc,
1346 struct smc_init_info *ini)
1351 ini->is_smcd = true;
1352 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
1354 if (aclc->hdr.version == SMC_V2) {
1355 struct smc_clc_msg_accept_confirm_v2 *aclc_v2 =
1356 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
1358 rc = smc_v2_determine_accepted_chid(aclc_v2, ini);
1362 ini->ism_peer_gid[ini->ism_selected] = aclc->d0.gid;
1364 /* there is only one lgr role for SMC-D; use server lock */
1365 mutex_lock(&smc_server_lgr_pending);
1366 rc = smc_conn_create(smc, ini);
1368 mutex_unlock(&smc_server_lgr_pending);
1372 /* Create send and receive buffers */
1373 rc = smc_buf_create(smc, true);
1375 rc = (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB : SMC_CLC_DECL_MEM;
1379 smc_conn_save_peer_info(smc, aclc);
1380 smc_close_init(smc);
1384 if (aclc->hdr.version > SMC_V1) {
1385 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
1386 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
1388 eid = clc_v2->d1.eid;
1391 rc = smc_clc_send_confirm(smc, ini->first_contact_local,
1392 aclc->hdr.version, eid, NULL);
1395 mutex_unlock(&smc_server_lgr_pending);
1397 smc_copy_sock_settings_to_clc(smc);
1398 smc->connect_nonblock = 0;
1399 if (smc->sk.sk_state == SMC_INIT)
1400 smc->sk.sk_state = SMC_ACTIVE;
1404 smc_conn_abort(smc, ini->first_contact_local);
1405 mutex_unlock(&smc_server_lgr_pending);
1406 smc->connect_nonblock = 0;
1411 /* check if received accept type and version matches a proposed one */
1412 static int smc_connect_check_aclc(struct smc_init_info *ini,
1413 struct smc_clc_msg_accept_confirm *aclc)
1415 if (aclc->hdr.typev1 != SMC_TYPE_R &&
1416 aclc->hdr.typev1 != SMC_TYPE_D)
1417 return SMC_CLC_DECL_MODEUNSUPP;
1419 if (aclc->hdr.version >= SMC_V2) {
1420 if ((aclc->hdr.typev1 == SMC_TYPE_R &&
1421 !smcr_indicated(ini->smc_type_v2)) ||
1422 (aclc->hdr.typev1 == SMC_TYPE_D &&
1423 !smcd_indicated(ini->smc_type_v2)))
1424 return SMC_CLC_DECL_MODEUNSUPP;
1426 if ((aclc->hdr.typev1 == SMC_TYPE_R &&
1427 !smcr_indicated(ini->smc_type_v1)) ||
1428 (aclc->hdr.typev1 == SMC_TYPE_D &&
1429 !smcd_indicated(ini->smc_type_v1)))
1430 return SMC_CLC_DECL_MODEUNSUPP;
1436 /* perform steps before actually connecting */
1437 static int __smc_connect(struct smc_sock *smc)
1439 u8 version = smc_ism_is_v2_capable() ? SMC_V2 : SMC_V1;
1440 struct smc_clc_msg_accept_confirm_v2 *aclc2;
1441 struct smc_clc_msg_accept_confirm *aclc;
1442 struct smc_init_info *ini = NULL;
1446 if (smc->use_fallback)
1447 return smc_connect_fallback(smc, smc->fallback_rsn);
1449 /* if peer has not signalled SMC-capability, fall back */
1450 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
1451 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
1453 /* IPSec connections opt out of SMC optimizations */
1454 if (using_ipsec(smc))
1455 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC,
1458 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
1460 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_MEM,
1463 ini->smcd_version = SMC_V1 | SMC_V2;
1464 ini->smcr_version = SMC_V1 | SMC_V2;
1465 ini->smc_type_v1 = SMC_TYPE_B;
1466 ini->smc_type_v2 = SMC_TYPE_B;
1468 /* get vlan id from IP device */
1469 if (smc_vlan_by_tcpsk(smc->clcsock, ini)) {
1470 ini->smcd_version &= ~SMC_V1;
1471 ini->smcr_version = 0;
1472 ini->smc_type_v1 = SMC_TYPE_N;
1473 if (!ini->smcd_version) {
1474 rc = SMC_CLC_DECL_GETVLANERR;
1479 rc = smc_find_proposal_devices(smc, ini);
1483 buf = kzalloc(SMC_CLC_MAX_ACCEPT_LEN, GFP_KERNEL);
1485 rc = SMC_CLC_DECL_MEM;
1488 aclc2 = (struct smc_clc_msg_accept_confirm_v2 *)buf;
1489 aclc = (struct smc_clc_msg_accept_confirm *)aclc2;
1491 /* perform CLC handshake */
1492 rc = smc_connect_clc(smc, aclc2, ini);
1494 /* -EAGAIN on timeout, see tcp_recvmsg() */
1495 if (rc == -EAGAIN) {
1497 smc->sk.sk_err = ETIMEDOUT;
1502 /* check if smc modes and versions of CLC proposal and accept match */
1503 rc = smc_connect_check_aclc(ini, aclc);
1504 version = aclc->hdr.version == SMC_V1 ? SMC_V1 : SMC_V2;
1508 /* depending on previous steps, connect using rdma or ism */
1509 if (aclc->hdr.typev1 == SMC_TYPE_R) {
1510 ini->smcr_version = version;
1511 rc = smc_connect_rdma(smc, aclc, ini);
1512 } else if (aclc->hdr.typev1 == SMC_TYPE_D) {
1513 ini->smcd_version = version;
1514 rc = smc_connect_ism(smc, aclc, ini);
1519 SMC_STAT_CLNT_SUCC_INC(sock_net(smc->clcsock->sk), aclc);
1520 smc_connect_ism_vlan_cleanup(smc, ini);
1526 smc_connect_ism_vlan_cleanup(smc, ini);
1530 return smc_connect_decline_fallback(smc, rc, version);
1533 static void smc_connect_work(struct work_struct *work)
1535 struct smc_sock *smc = container_of(work, struct smc_sock,
1537 long timeo = smc->sk.sk_sndtimeo;
1541 timeo = MAX_SCHEDULE_TIMEOUT;
1542 lock_sock(smc->clcsock->sk);
1543 if (smc->clcsock->sk->sk_err) {
1544 smc->sk.sk_err = smc->clcsock->sk->sk_err;
1545 } else if ((1 << smc->clcsock->sk->sk_state) &
1546 (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
1547 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
1548 if ((rc == -EPIPE) &&
1549 ((1 << smc->clcsock->sk->sk_state) &
1550 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
1553 release_sock(smc->clcsock->sk);
1554 lock_sock(&smc->sk);
1555 if (rc != 0 || smc->sk.sk_err) {
1556 smc->sk.sk_state = SMC_CLOSED;
1557 if (rc == -EPIPE || rc == -EAGAIN)
1558 smc->sk.sk_err = EPIPE;
1559 else if (rc == -ECONNREFUSED)
1560 smc->sk.sk_err = ECONNREFUSED;
1561 else if (signal_pending(current))
1562 smc->sk.sk_err = -sock_intr_errno(timeo);
1563 sock_put(&smc->sk); /* passive closing */
1567 rc = __smc_connect(smc);
1569 smc->sk.sk_err = -rc;
1572 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
1573 if (smc->sk.sk_err) {
1574 smc->sk.sk_state_change(&smc->sk);
1575 } else { /* allow polling before and after fallback decision */
1576 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
1577 smc->sk.sk_write_space(&smc->sk);
1580 release_sock(&smc->sk);
1583 static int smc_connect(struct socket *sock, struct sockaddr *addr,
1584 int alen, int flags)
1586 struct sock *sk = sock->sk;
1587 struct smc_sock *smc;
1592 /* separate smc parameter checking to be safe */
1593 if (alen < sizeof(addr->sa_family))
1595 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
1599 switch (sock->state) {
1604 rc = sk->sk_state == SMC_ACTIVE ? -EISCONN : -EINVAL;
1607 if (sk->sk_state == SMC_ACTIVE)
1610 case SS_UNCONNECTED:
1611 sock->state = SS_CONNECTING;
1615 switch (sk->sk_state) {
1619 rc = sock_error(sk) ? : -ECONNABORTED;
1620 sock->state = SS_UNCONNECTED;
1629 smc_copy_sock_settings_to_clc(smc);
1630 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
1631 if (smc->connect_nonblock) {
1635 rc = kernel_connect(smc->clcsock, addr, alen, flags);
1636 if (rc && rc != -EINPROGRESS)
1639 if (smc->use_fallback) {
1640 sock->state = rc ? SS_CONNECTING : SS_CONNECTED;
1643 sock_hold(&smc->sk); /* sock put in passive closing */
1644 if (flags & O_NONBLOCK) {
1645 if (queue_work(smc_hs_wq, &smc->connect_work))
1646 smc->connect_nonblock = 1;
1650 rc = __smc_connect(smc);
1657 sock->state = SS_CONNECTED;
1664 static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
1666 struct socket *new_clcsock = NULL;
1667 struct sock *lsk = &lsmc->sk;
1668 struct sock *new_sk;
1672 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
1675 lsk->sk_err = ENOMEM;
1680 *new_smc = smc_sk(new_sk);
1682 mutex_lock(&lsmc->clcsock_release_lock);
1684 rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK);
1685 mutex_unlock(&lsmc->clcsock_release_lock);
1687 if (rc < 0 && rc != -EAGAIN)
1689 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
1690 new_sk->sk_prot->unhash(new_sk);
1692 sock_release(new_clcsock);
1693 new_sk->sk_state = SMC_CLOSED;
1694 sock_set_flag(new_sk, SOCK_DEAD);
1695 sock_put(new_sk); /* final */
1700 /* new clcsock has inherited the smc listen-specific sk_data_ready
1701 * function; switch it back to the original sk_data_ready function
1703 new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready;
1705 /* if new clcsock has also inherited the fallback-specific callback
1706 * functions, switch them back to the original ones.
1708 if (lsmc->use_fallback) {
1709 if (lsmc->clcsk_state_change)
1710 new_clcsock->sk->sk_state_change = lsmc->clcsk_state_change;
1711 if (lsmc->clcsk_write_space)
1712 new_clcsock->sk->sk_write_space = lsmc->clcsk_write_space;
1713 if (lsmc->clcsk_error_report)
1714 new_clcsock->sk->sk_error_report = lsmc->clcsk_error_report;
1717 (*new_smc)->clcsock = new_clcsock;
1722 /* add a just created sock to the accept queue of the listen sock as
1723 * candidate for a following socket accept call from user space
1725 static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
1727 struct smc_sock *par = smc_sk(parent);
1729 sock_hold(sk); /* sock_put in smc_accept_unlink () */
1730 spin_lock(&par->accept_q_lock);
1731 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
1732 spin_unlock(&par->accept_q_lock);
1733 sk_acceptq_added(parent);
1736 /* remove a socket from the accept queue of its parental listening socket */
1737 static void smc_accept_unlink(struct sock *sk)
1739 struct smc_sock *par = smc_sk(sk)->listen_smc;
1741 spin_lock(&par->accept_q_lock);
1742 list_del_init(&smc_sk(sk)->accept_q);
1743 spin_unlock(&par->accept_q_lock);
1744 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
1745 sock_put(sk); /* sock_hold in smc_accept_enqueue */
1748 /* remove a sock from the accept queue to bind it to a new socket created
1749 * for a socket accept call from user space
1751 struct sock *smc_accept_dequeue(struct sock *parent,
1752 struct socket *new_sock)
1754 struct smc_sock *isk, *n;
1755 struct sock *new_sk;
1757 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
1758 new_sk = (struct sock *)isk;
1760 smc_accept_unlink(new_sk);
1761 if (new_sk->sk_state == SMC_CLOSED) {
1762 new_sk->sk_prot->unhash(new_sk);
1764 sock_release(isk->clcsock);
1765 isk->clcsock = NULL;
1767 sock_put(new_sk); /* final */
1771 sock_graft(new_sk, new_sock);
1772 new_sock->state = SS_CONNECTED;
1773 if (isk->use_fallback) {
1774 smc_sk(new_sk)->clcsock->file = new_sock->file;
1775 isk->clcsock->file->private_data = isk->clcsock;
1783 /* clean up for a created but never accepted sock */
1784 void smc_close_non_accepted(struct sock *sk)
1786 struct smc_sock *smc = smc_sk(sk);
1788 sock_hold(sk); /* sock_put below */
1790 if (!sk->sk_lingertime)
1791 /* wait for peer closing */
1792 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
1795 sock_put(sk); /* sock_hold above */
1796 sock_put(sk); /* final sock_put */
1799 static int smcr_serv_conf_first_link(struct smc_sock *smc)
1801 struct smc_link *link = smc->conn.lnk;
1802 struct smc_llc_qentry *qentry;
1805 /* reg the sndbuf if it was vzalloced*/
1806 if (smc->conn.sndbuf_desc->is_vm) {
1807 if (smcr_link_reg_buf(link, smc->conn.sndbuf_desc))
1808 return SMC_CLC_DECL_ERR_REGBUF;
1812 if (smcr_link_reg_buf(link, smc->conn.rmb_desc))
1813 return SMC_CLC_DECL_ERR_REGBUF;
1815 /* send CONFIRM LINK request to client over the RoCE fabric */
1816 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
1818 return SMC_CLC_DECL_TIMEOUT_CL;
1820 /* receive CONFIRM LINK response from client over the RoCE fabric */
1821 qentry = smc_llc_wait(link->lgr, link, SMC_LLC_WAIT_TIME,
1822 SMC_LLC_CONFIRM_LINK);
1824 struct smc_clc_msg_decline dclc;
1826 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
1827 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
1828 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
1830 smc_llc_save_peer_uid(qentry);
1831 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_RESP);
1832 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
1834 return SMC_CLC_DECL_RMBE_EC;
1836 /* confirm_rkey is implicit on 1st contact */
1837 smc->conn.rmb_desc->is_conf_rkey = true;
1839 smc_llc_link_active(link);
1840 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
1842 down_write(&link->lgr->llc_conf_mutex);
1843 /* initial contact - try to establish second link */
1844 smc_llc_srv_add_link(link, NULL);
1845 up_write(&link->lgr->llc_conf_mutex);
1849 /* listen worker: finish */
1850 static void smc_listen_out(struct smc_sock *new_smc)
1852 struct smc_sock *lsmc = new_smc->listen_smc;
1853 struct sock *newsmcsk = &new_smc->sk;
1855 if (tcp_sk(new_smc->clcsock->sk)->syn_smc)
1856 atomic_dec(&lsmc->queued_smc_hs);
1858 if (lsmc->sk.sk_state == SMC_LISTEN) {
1859 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
1860 smc_accept_enqueue(&lsmc->sk, newsmcsk);
1861 release_sock(&lsmc->sk);
1862 } else { /* no longer listening */
1863 smc_close_non_accepted(newsmcsk);
1866 /* Wake up accept */
1867 lsmc->sk.sk_data_ready(&lsmc->sk);
1868 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
1871 /* listen worker: finish in state connected */
1872 static void smc_listen_out_connected(struct smc_sock *new_smc)
1874 struct sock *newsmcsk = &new_smc->sk;
1876 if (newsmcsk->sk_state == SMC_INIT)
1877 newsmcsk->sk_state = SMC_ACTIVE;
1879 smc_listen_out(new_smc);
1882 /* listen worker: finish in error state */
1883 static void smc_listen_out_err(struct smc_sock *new_smc)
1885 struct sock *newsmcsk = &new_smc->sk;
1886 struct net *net = sock_net(newsmcsk);
1888 this_cpu_inc(net->smc.smc_stats->srv_hshake_err_cnt);
1889 if (newsmcsk->sk_state == SMC_INIT)
1890 sock_put(&new_smc->sk); /* passive closing */
1891 newsmcsk->sk_state = SMC_CLOSED;
1893 smc_listen_out(new_smc);
1896 /* listen worker: decline and fall back if possible */
1897 static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1898 int local_first, u8 version)
1900 /* RDMA setup failed, switch back to TCP */
1901 smc_conn_abort(new_smc, local_first);
1902 if (reason_code < 0 ||
1903 smc_switch_to_fallback(new_smc, reason_code)) {
1904 /* error, no fallback possible */
1905 smc_listen_out_err(new_smc);
1908 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
1909 if (smc_clc_send_decline(new_smc, reason_code, version) < 0) {
1910 smc_listen_out_err(new_smc);
1914 smc_listen_out_connected(new_smc);
1917 /* listen worker: version checking */
1918 static int smc_listen_v2_check(struct smc_sock *new_smc,
1919 struct smc_clc_msg_proposal *pclc,
1920 struct smc_init_info *ini)
1922 struct smc_clc_smcd_v2_extension *pclc_smcd_v2_ext;
1923 struct smc_clc_v2_extension *pclc_v2_ext;
1924 int rc = SMC_CLC_DECL_PEERNOSMC;
1926 ini->smc_type_v1 = pclc->hdr.typev1;
1927 ini->smc_type_v2 = pclc->hdr.typev2;
1928 ini->smcd_version = smcd_indicated(ini->smc_type_v1) ? SMC_V1 : 0;
1929 ini->smcr_version = smcr_indicated(ini->smc_type_v1) ? SMC_V1 : 0;
1930 if (pclc->hdr.version > SMC_V1) {
1931 if (smcd_indicated(ini->smc_type_v2))
1932 ini->smcd_version |= SMC_V2;
1933 if (smcr_indicated(ini->smc_type_v2))
1934 ini->smcr_version |= SMC_V2;
1936 if (!(ini->smcd_version & SMC_V2) && !(ini->smcr_version & SMC_V2)) {
1937 rc = SMC_CLC_DECL_PEERNOSMC;
1940 pclc_v2_ext = smc_get_clc_v2_ext(pclc);
1942 ini->smcd_version &= ~SMC_V2;
1943 ini->smcr_version &= ~SMC_V2;
1944 rc = SMC_CLC_DECL_NOV2EXT;
1947 pclc_smcd_v2_ext = smc_get_clc_smcd_v2_ext(pclc_v2_ext);
1948 if (ini->smcd_version & SMC_V2) {
1949 if (!smc_ism_is_v2_capable()) {
1950 ini->smcd_version &= ~SMC_V2;
1951 rc = SMC_CLC_DECL_NOISM2SUPP;
1952 } else if (!pclc_smcd_v2_ext) {
1953 ini->smcd_version &= ~SMC_V2;
1954 rc = SMC_CLC_DECL_NOV2DEXT;
1955 } else if (!pclc_v2_ext->hdr.eid_cnt &&
1956 !pclc_v2_ext->hdr.flag.seid) {
1957 ini->smcd_version &= ~SMC_V2;
1958 rc = SMC_CLC_DECL_NOUEID;
1961 if (ini->smcr_version & SMC_V2) {
1962 if (!pclc_v2_ext->hdr.eid_cnt) {
1963 ini->smcr_version &= ~SMC_V2;
1964 rc = SMC_CLC_DECL_NOUEID;
1969 if (!ini->smcd_version && !ini->smcr_version)
1975 /* listen worker: check prefixes */
1976 static int smc_listen_prfx_check(struct smc_sock *new_smc,
1977 struct smc_clc_msg_proposal *pclc)
1979 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1980 struct socket *newclcsock = new_smc->clcsock;
1982 if (pclc->hdr.typev1 == SMC_TYPE_N)
1984 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1985 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
1986 return SMC_CLC_DECL_DIFFPREFIX;
1991 /* listen worker: initialize connection and buffers */
1992 static int smc_listen_rdma_init(struct smc_sock *new_smc,
1993 struct smc_init_info *ini)
1997 /* allocate connection / link group */
1998 rc = smc_conn_create(new_smc, ini);
2002 /* create send buffer and rmb */
2003 if (smc_buf_create(new_smc, false))
2004 return SMC_CLC_DECL_MEM;
2009 /* listen worker: initialize connection and buffers for SMC-D */
2010 static int smc_listen_ism_init(struct smc_sock *new_smc,
2011 struct smc_init_info *ini)
2015 rc = smc_conn_create(new_smc, ini);
2019 /* Create send and receive buffers */
2020 rc = smc_buf_create(new_smc, true);
2022 smc_conn_abort(new_smc, ini->first_contact_local);
2023 return (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB :
2030 static bool smc_is_already_selected(struct smcd_dev *smcd,
2031 struct smc_init_info *ini,
2036 for (i = 0; i < matches; i++)
2037 if (smcd == ini->ism_dev[i])
2043 /* check for ISM devices matching proposed ISM devices */
2044 static void smc_check_ism_v2_match(struct smc_init_info *ini,
2045 u16 proposed_chid, u64 proposed_gid,
2046 unsigned int *matches)
2048 struct smcd_dev *smcd;
2050 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
2051 if (smcd->going_away)
2053 if (smc_is_already_selected(smcd, ini, *matches))
2055 if (smc_ism_get_chid(smcd) == proposed_chid &&
2056 !smc_ism_cantalk(proposed_gid, ISM_RESERVED_VLANID, smcd)) {
2057 ini->ism_peer_gid[*matches] = proposed_gid;
2058 ini->ism_dev[*matches] = smcd;
2065 static void smc_find_ism_store_rc(u32 rc, struct smc_init_info *ini)
2071 static void smc_find_ism_v2_device_serv(struct smc_sock *new_smc,
2072 struct smc_clc_msg_proposal *pclc,
2073 struct smc_init_info *ini)
2075 struct smc_clc_smcd_v2_extension *smcd_v2_ext;
2076 struct smc_clc_v2_extension *smc_v2_ext;
2077 struct smc_clc_msg_smcd *pclc_smcd;
2078 unsigned int matches = 0;
2083 if (!(ini->smcd_version & SMC_V2) || !smcd_indicated(ini->smc_type_v2))
2086 pclc_smcd = smc_get_clc_msg_smcd(pclc);
2087 smc_v2_ext = smc_get_clc_v2_ext(pclc);
2088 smcd_v2_ext = smc_get_clc_smcd_v2_ext(smc_v2_ext);
2090 mutex_lock(&smcd_dev_list.mutex);
2091 if (pclc_smcd->ism.chid)
2092 /* check for ISM device matching proposed native ISM device */
2093 smc_check_ism_v2_match(ini, ntohs(pclc_smcd->ism.chid),
2094 ntohll(pclc_smcd->ism.gid), &matches);
2095 for (i = 1; i <= smc_v2_ext->hdr.ism_gid_cnt; i++) {
2096 /* check for ISM devices matching proposed non-native ISM
2099 smc_check_ism_v2_match(ini,
2100 ntohs(smcd_v2_ext->gidchid[i - 1].chid),
2101 ntohll(smcd_v2_ext->gidchid[i - 1].gid),
2104 mutex_unlock(&smcd_dev_list.mutex);
2106 if (!ini->ism_dev[0]) {
2107 smc_find_ism_store_rc(SMC_CLC_DECL_NOSMCD2DEV, ini);
2111 smc_ism_get_system_eid(&eid);
2112 if (!smc_clc_match_eid(ini->negotiated_eid, smc_v2_ext,
2113 smcd_v2_ext->system_eid, eid))
2116 /* separate - outside the smcd_dev_list.lock */
2117 smcd_version = ini->smcd_version;
2118 for (i = 0; i < matches; i++) {
2119 ini->smcd_version = SMC_V2;
2120 ini->is_smcd = true;
2121 ini->ism_selected = i;
2122 rc = smc_listen_ism_init(new_smc, ini);
2124 smc_find_ism_store_rc(rc, ini);
2125 /* try next active ISM device */
2128 return; /* matching and usable V2 ISM device found */
2130 /* no V2 ISM device could be initialized */
2131 ini->smcd_version = smcd_version; /* restore original value */
2132 ini->negotiated_eid[0] = 0;
2135 ini->smcd_version &= ~SMC_V2;
2136 ini->ism_dev[0] = NULL;
2137 ini->is_smcd = false;
2140 static void smc_find_ism_v1_device_serv(struct smc_sock *new_smc,
2141 struct smc_clc_msg_proposal *pclc,
2142 struct smc_init_info *ini)
2144 struct smc_clc_msg_smcd *pclc_smcd = smc_get_clc_msg_smcd(pclc);
2147 /* check if ISM V1 is available */
2148 if (!(ini->smcd_version & SMC_V1) || !smcd_indicated(ini->smc_type_v1))
2150 ini->is_smcd = true; /* prepare ISM check */
2151 ini->ism_peer_gid[0] = ntohll(pclc_smcd->ism.gid);
2152 rc = smc_find_ism_device(new_smc, ini);
2155 ini->ism_selected = 0;
2156 rc = smc_listen_ism_init(new_smc, ini);
2158 return; /* V1 ISM device found */
2161 smc_find_ism_store_rc(rc, ini);
2162 ini->smcd_version &= ~SMC_V1;
2163 ini->ism_dev[0] = NULL;
2164 ini->is_smcd = false;
2167 /* listen worker: register buffers */
2168 static int smc_listen_rdma_reg(struct smc_sock *new_smc, bool local_first)
2170 struct smc_connection *conn = &new_smc->conn;
2173 /* reg sendbufs if they were vzalloced */
2174 if (conn->sndbuf_desc->is_vm) {
2175 if (smcr_lgr_reg_sndbufs(conn->lnk,
2177 return SMC_CLC_DECL_ERR_REGBUF;
2179 if (smcr_lgr_reg_rmbs(conn->lnk, conn->rmb_desc))
2180 return SMC_CLC_DECL_ERR_REGBUF;
2186 static void smc_find_rdma_v2_device_serv(struct smc_sock *new_smc,
2187 struct smc_clc_msg_proposal *pclc,
2188 struct smc_init_info *ini)
2190 struct smc_clc_v2_extension *smc_v2_ext;
2194 if (!(ini->smcr_version & SMC_V2) || !smcr_indicated(ini->smc_type_v2))
2197 smc_v2_ext = smc_get_clc_v2_ext(pclc);
2198 if (!smc_clc_match_eid(ini->negotiated_eid, smc_v2_ext, NULL, NULL))
2201 /* prepare RDMA check */
2202 memcpy(ini->peer_systemid, pclc->lcl.id_for_peer, SMC_SYSTEMID_LEN);
2203 memcpy(ini->peer_gid, smc_v2_ext->roce, SMC_GID_SIZE);
2204 memcpy(ini->peer_mac, pclc->lcl.mac, ETH_ALEN);
2205 ini->check_smcrv2 = true;
2206 ini->smcrv2.clc_sk = new_smc->clcsock->sk;
2207 ini->smcrv2.saddr = new_smc->clcsock->sk->sk_rcv_saddr;
2208 ini->smcrv2.daddr = smc_ib_gid_to_ipv4(smc_v2_ext->roce);
2209 rc = smc_find_rdma_device(new_smc, ini);
2211 smc_find_ism_store_rc(rc, ini);
2214 if (!ini->smcrv2.uses_gateway)
2215 memcpy(ini->smcrv2.nexthop_mac, pclc->lcl.mac, ETH_ALEN);
2217 smcr_version = ini->smcr_version;
2218 ini->smcr_version = SMC_V2;
2219 rc = smc_listen_rdma_init(new_smc, ini);
2221 rc = smc_listen_rdma_reg(new_smc, ini->first_contact_local);
2224 ini->smcr_version = smcr_version;
2225 smc_find_ism_store_rc(rc, ini);
2228 ini->smcr_version &= ~SMC_V2;
2229 ini->smcrv2.ib_dev_v2 = NULL;
2230 ini->check_smcrv2 = false;
2233 static int smc_find_rdma_v1_device_serv(struct smc_sock *new_smc,
2234 struct smc_clc_msg_proposal *pclc,
2235 struct smc_init_info *ini)
2239 if (!(ini->smcr_version & SMC_V1) || !smcr_indicated(ini->smc_type_v1))
2240 return SMC_CLC_DECL_NOSMCDEV;
2242 /* prepare RDMA check */
2243 memcpy(ini->peer_systemid, pclc->lcl.id_for_peer, SMC_SYSTEMID_LEN);
2244 memcpy(ini->peer_gid, pclc->lcl.gid, SMC_GID_SIZE);
2245 memcpy(ini->peer_mac, pclc->lcl.mac, ETH_ALEN);
2246 rc = smc_find_rdma_device(new_smc, ini);
2248 /* no RDMA device found */
2249 return SMC_CLC_DECL_NOSMCDEV;
2251 rc = smc_listen_rdma_init(new_smc, ini);
2254 return smc_listen_rdma_reg(new_smc, ini->first_contact_local);
2257 /* determine the local device matching to proposal */
2258 static int smc_listen_find_device(struct smc_sock *new_smc,
2259 struct smc_clc_msg_proposal *pclc,
2260 struct smc_init_info *ini)
2264 /* check for ISM device matching V2 proposed device */
2265 smc_find_ism_v2_device_serv(new_smc, pclc, ini);
2266 if (ini->ism_dev[0])
2269 /* check for matching IP prefix and subnet length (V1) */
2270 prfx_rc = smc_listen_prfx_check(new_smc, pclc);
2272 smc_find_ism_store_rc(prfx_rc, ini);
2274 /* get vlan id from IP device */
2275 if (smc_vlan_by_tcpsk(new_smc->clcsock, ini))
2276 return ini->rc ?: SMC_CLC_DECL_GETVLANERR;
2278 /* check for ISM device matching V1 proposed device */
2280 smc_find_ism_v1_device_serv(new_smc, pclc, ini);
2281 if (ini->ism_dev[0])
2284 if (!smcr_indicated(pclc->hdr.typev1) &&
2285 !smcr_indicated(pclc->hdr.typev2))
2286 /* skip RDMA and decline */
2287 return ini->rc ?: SMC_CLC_DECL_NOSMCDDEV;
2289 /* check if RDMA V2 is available */
2290 smc_find_rdma_v2_device_serv(new_smc, pclc, ini);
2291 if (ini->smcrv2.ib_dev_v2)
2294 /* check if RDMA V1 is available */
2298 rc = smc_find_rdma_v1_device_serv(new_smc, pclc, ini);
2299 smc_find_ism_store_rc(rc, ini);
2300 return (!rc) ? 0 : ini->rc;
2302 return SMC_CLC_DECL_NOSMCDEV;
2305 /* listen worker: finish RDMA setup */
2306 static int smc_listen_rdma_finish(struct smc_sock *new_smc,
2307 struct smc_clc_msg_accept_confirm *cclc,
2309 struct smc_init_info *ini)
2311 struct smc_link *link = new_smc->conn.lnk;
2312 int reason_code = 0;
2315 smc_link_save_peer_info(link, cclc, ini);
2317 if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc))
2318 return SMC_CLC_DECL_ERR_RTOK;
2321 if (smc_ib_ready_link(link))
2322 return SMC_CLC_DECL_ERR_RDYLNK;
2323 /* QP confirmation over RoCE fabric */
2324 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
2325 reason_code = smcr_serv_conf_first_link(new_smc);
2326 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
2331 /* setup for connection of server */
2332 static void smc_listen_work(struct work_struct *work)
2334 struct smc_sock *new_smc = container_of(work, struct smc_sock,
2336 struct socket *newclcsock = new_smc->clcsock;
2337 struct smc_clc_msg_accept_confirm *cclc;
2338 struct smc_clc_msg_proposal_area *buf;
2339 struct smc_clc_msg_proposal *pclc;
2340 struct smc_init_info *ini = NULL;
2341 u8 proposal_version = SMC_V1;
2345 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
2346 return smc_listen_out_err(new_smc);
2348 if (new_smc->use_fallback) {
2349 smc_listen_out_connected(new_smc);
2353 /* check if peer is smc capable */
2354 if (!tcp_sk(newclcsock->sk)->syn_smc) {
2355 rc = smc_switch_to_fallback(new_smc, SMC_CLC_DECL_PEERNOSMC);
2357 smc_listen_out_err(new_smc);
2359 smc_listen_out_connected(new_smc);
2363 /* do inband token exchange -
2364 * wait for and receive SMC Proposal CLC message
2366 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
2368 rc = SMC_CLC_DECL_MEM;
2371 pclc = (struct smc_clc_msg_proposal *)buf;
2372 rc = smc_clc_wait_msg(new_smc, pclc, sizeof(*buf),
2373 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
2377 if (pclc->hdr.version > SMC_V1)
2378 proposal_version = SMC_V2;
2380 /* IPSec connections opt out of SMC optimizations */
2381 if (using_ipsec(new_smc)) {
2382 rc = SMC_CLC_DECL_IPSEC;
2386 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
2388 rc = SMC_CLC_DECL_MEM;
2392 /* initial version checking */
2393 rc = smc_listen_v2_check(new_smc, pclc, ini);
2397 mutex_lock(&smc_server_lgr_pending);
2398 smc_close_init(new_smc);
2399 smc_rx_init(new_smc);
2400 smc_tx_init(new_smc);
2402 /* determine ISM or RoCE device used for connection */
2403 rc = smc_listen_find_device(new_smc, pclc, ini);
2407 /* send SMC Accept CLC message */
2408 accept_version = ini->is_smcd ? ini->smcd_version : ini->smcr_version;
2409 rc = smc_clc_send_accept(new_smc, ini->first_contact_local,
2410 accept_version, ini->negotiated_eid);
2414 /* SMC-D does not need this lock any more */
2416 mutex_unlock(&smc_server_lgr_pending);
2418 /* receive SMC Confirm CLC message */
2419 memset(buf, 0, sizeof(*buf));
2420 cclc = (struct smc_clc_msg_accept_confirm *)buf;
2421 rc = smc_clc_wait_msg(new_smc, cclc, sizeof(*buf),
2422 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
2430 if (!ini->is_smcd) {
2431 rc = smc_listen_rdma_finish(new_smc, cclc,
2432 ini->first_contact_local, ini);
2435 mutex_unlock(&smc_server_lgr_pending);
2437 smc_conn_save_peer_info(new_smc, cclc);
2438 smc_listen_out_connected(new_smc);
2439 SMC_STAT_SERV_SUCC_INC(sock_net(newclcsock->sk), ini);
2443 mutex_unlock(&smc_server_lgr_pending);
2445 smc_listen_decline(new_smc, rc, ini ? ini->first_contact_local : 0,
2452 static void smc_tcp_listen_work(struct work_struct *work)
2454 struct smc_sock *lsmc = container_of(work, struct smc_sock,
2456 struct sock *lsk = &lsmc->sk;
2457 struct smc_sock *new_smc;
2461 while (lsk->sk_state == SMC_LISTEN) {
2462 rc = smc_clcsock_accept(lsmc, &new_smc);
2463 if (rc) /* clcsock accept queue empty or error */
2468 if (tcp_sk(new_smc->clcsock->sk)->syn_smc)
2469 atomic_inc(&lsmc->queued_smc_hs);
2471 new_smc->listen_smc = lsmc;
2472 new_smc->use_fallback = lsmc->use_fallback;
2473 new_smc->fallback_rsn = lsmc->fallback_rsn;
2474 sock_hold(lsk); /* sock_put in smc_listen_work */
2475 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
2476 smc_copy_sock_settings_to_smc(new_smc);
2477 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
2478 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
2479 sock_hold(&new_smc->sk); /* sock_put in passive closing */
2480 if (!queue_work(smc_hs_wq, &new_smc->smc_listen_work))
2481 sock_put(&new_smc->sk);
2486 sock_put(&lsmc->sk); /* sock_hold in smc_clcsock_data_ready() */
2489 static void smc_clcsock_data_ready(struct sock *listen_clcsock)
2491 struct smc_sock *lsmc;
2493 read_lock_bh(&listen_clcsock->sk_callback_lock);
2494 lsmc = smc_clcsock_user_data(listen_clcsock);
2497 lsmc->clcsk_data_ready(listen_clcsock);
2498 if (lsmc->sk.sk_state == SMC_LISTEN) {
2499 sock_hold(&lsmc->sk); /* sock_put in smc_tcp_listen_work() */
2500 if (!queue_work(smc_tcp_ls_wq, &lsmc->tcp_listen_work))
2501 sock_put(&lsmc->sk);
2504 read_unlock_bh(&listen_clcsock->sk_callback_lock);
2507 static int smc_listen(struct socket *sock, int backlog)
2509 struct sock *sk = sock->sk;
2510 struct smc_sock *smc;
2517 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
2518 smc->connect_nonblock || sock->state != SS_UNCONNECTED)
2522 if (sk->sk_state == SMC_LISTEN) {
2523 sk->sk_max_ack_backlog = backlog;
2526 /* some socket options are handled in core, so we could not apply
2527 * them to the clc socket -- copy smc socket options to clc socket
2529 smc_copy_sock_settings_to_clc(smc);
2530 if (!smc->use_fallback)
2531 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
2533 /* save original sk_data_ready function and establish
2534 * smc-specific sk_data_ready function
2536 write_lock_bh(&smc->clcsock->sk->sk_callback_lock);
2537 smc->clcsock->sk->sk_user_data =
2538 (void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY);
2539 smc_clcsock_replace_cb(&smc->clcsock->sk->sk_data_ready,
2540 smc_clcsock_data_ready, &smc->clcsk_data_ready);
2541 write_unlock_bh(&smc->clcsock->sk->sk_callback_lock);
2543 /* save original ops */
2544 smc->ori_af_ops = inet_csk(smc->clcsock->sk)->icsk_af_ops;
2546 smc->af_ops = *smc->ori_af_ops;
2547 smc->af_ops.syn_recv_sock = smc_tcp_syn_recv_sock;
2549 inet_csk(smc->clcsock->sk)->icsk_af_ops = &smc->af_ops;
2551 if (smc->limit_smc_hs)
2552 tcp_sk(smc->clcsock->sk)->smc_hs_congested = smc_hs_congested;
2554 rc = kernel_listen(smc->clcsock, backlog);
2556 write_lock_bh(&smc->clcsock->sk->sk_callback_lock);
2557 smc_clcsock_restore_cb(&smc->clcsock->sk->sk_data_ready,
2558 &smc->clcsk_data_ready);
2559 smc->clcsock->sk->sk_user_data = NULL;
2560 write_unlock_bh(&smc->clcsock->sk->sk_callback_lock);
2563 sk->sk_max_ack_backlog = backlog;
2564 sk->sk_ack_backlog = 0;
2565 sk->sk_state = SMC_LISTEN;
2572 static int smc_accept(struct socket *sock, struct socket *new_sock,
2573 int flags, bool kern)
2575 struct sock *sk = sock->sk, *nsk;
2576 DECLARE_WAITQUEUE(wait, current);
2577 struct smc_sock *lsmc;
2582 sock_hold(sk); /* sock_put below */
2585 if (lsmc->sk.sk_state != SMC_LISTEN) {
2591 /* Wait for an incoming connection */
2592 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2593 add_wait_queue_exclusive(sk_sleep(sk), &wait);
2594 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
2595 set_current_state(TASK_INTERRUPTIBLE);
2601 timeo = schedule_timeout(timeo);
2602 /* wakeup by sk_data_ready in smc_listen_work() */
2603 sched_annotate_sleep();
2605 if (signal_pending(current)) {
2606 rc = sock_intr_errno(timeo);
2610 set_current_state(TASK_RUNNING);
2611 remove_wait_queue(sk_sleep(sk), &wait);
2614 rc = sock_error(nsk);
2619 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
2620 /* wait till data arrives on the socket */
2621 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
2623 if (smc_sk(nsk)->use_fallback) {
2624 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
2627 if (skb_queue_empty(&clcsk->sk_receive_queue))
2628 sk_wait_data(clcsk, &timeo, NULL);
2629 release_sock(clcsk);
2630 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
2632 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
2638 sock_put(sk); /* sock_hold above */
2642 static int smc_getname(struct socket *sock, struct sockaddr *addr,
2645 struct smc_sock *smc;
2647 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
2648 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
2651 smc = smc_sk(sock->sk);
2653 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
2656 static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
2658 struct sock *sk = sock->sk;
2659 struct smc_sock *smc;
2664 if ((sk->sk_state != SMC_ACTIVE) &&
2665 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2666 (sk->sk_state != SMC_INIT))
2669 if (msg->msg_flags & MSG_FASTOPEN) {
2670 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
2671 rc = smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP);
2680 if (smc->use_fallback) {
2681 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
2683 rc = smc_tx_sendmsg(smc, msg, len);
2684 SMC_STAT_TX_PAYLOAD(smc, len, rc);
2691 static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
2694 struct sock *sk = sock->sk;
2695 struct smc_sock *smc;
2700 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2701 /* socket was connected before, no more data to read */
2705 if ((sk->sk_state == SMC_INIT) ||
2706 (sk->sk_state == SMC_LISTEN) ||
2707 (sk->sk_state == SMC_CLOSED))
2710 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2715 if (smc->use_fallback) {
2716 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
2718 msg->msg_namelen = 0;
2719 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
2720 SMC_STAT_RX_PAYLOAD(smc, rc, rc);
2728 static __poll_t smc_accept_poll(struct sock *parent)
2730 struct smc_sock *isk = smc_sk(parent);
2733 spin_lock(&isk->accept_q_lock);
2734 if (!list_empty(&isk->accept_q))
2735 mask = EPOLLIN | EPOLLRDNORM;
2736 spin_unlock(&isk->accept_q_lock);
2741 static __poll_t smc_poll(struct file *file, struct socket *sock,
2744 struct sock *sk = sock->sk;
2745 struct smc_sock *smc;
2751 smc = smc_sk(sock->sk);
2752 if (smc->use_fallback) {
2753 /* delegate to CLC child sock */
2754 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
2755 sk->sk_err = smc->clcsock->sk->sk_err;
2757 if (sk->sk_state != SMC_CLOSED)
2758 sock_poll_wait(file, sock, wait);
2761 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
2762 (sk->sk_state == SMC_CLOSED))
2764 if (sk->sk_state == SMC_LISTEN) {
2765 /* woken up by sk_data_ready in smc_listen_work() */
2766 mask |= smc_accept_poll(sk);
2767 } else if (smc->use_fallback) { /* as result of connect_work()*/
2768 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
2770 sk->sk_err = smc->clcsock->sk->sk_err;
2772 if ((sk->sk_state != SMC_INIT &&
2773 atomic_read(&smc->conn.sndbuf_space)) ||
2774 sk->sk_shutdown & SEND_SHUTDOWN) {
2775 mask |= EPOLLOUT | EPOLLWRNORM;
2777 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
2778 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
2780 if (atomic_read(&smc->conn.bytes_to_rcv))
2781 mask |= EPOLLIN | EPOLLRDNORM;
2782 if (sk->sk_shutdown & RCV_SHUTDOWN)
2783 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
2784 if (sk->sk_state == SMC_APPCLOSEWAIT1)
2786 if (smc->conn.urg_state == SMC_URG_VALID)
2794 static int smc_shutdown(struct socket *sock, int how)
2796 struct sock *sk = sock->sk;
2797 bool do_shutdown = true;
2798 struct smc_sock *smc;
2805 if ((how < SHUT_RD) || (how > SHUT_RDWR))
2810 if (sock->state == SS_CONNECTING) {
2811 if (sk->sk_state == SMC_ACTIVE)
2812 sock->state = SS_CONNECTED;
2813 else if (sk->sk_state == SMC_PEERCLOSEWAIT1 ||
2814 sk->sk_state == SMC_PEERCLOSEWAIT2 ||
2815 sk->sk_state == SMC_APPCLOSEWAIT1 ||
2816 sk->sk_state == SMC_APPCLOSEWAIT2 ||
2817 sk->sk_state == SMC_APPFINCLOSEWAIT)
2818 sock->state = SS_DISCONNECTING;
2822 if ((sk->sk_state != SMC_ACTIVE) &&
2823 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
2824 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
2825 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2826 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
2827 (sk->sk_state != SMC_APPFINCLOSEWAIT))
2829 if (smc->use_fallback) {
2830 rc = kernel_sock_shutdown(smc->clcsock, how);
2831 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
2832 if (sk->sk_shutdown == SHUTDOWN_MASK) {
2833 sk->sk_state = SMC_CLOSED;
2834 sk->sk_socket->state = SS_UNCONNECTED;
2840 case SHUT_RDWR: /* shutdown in both directions */
2841 old_state = sk->sk_state;
2842 rc = smc_close_active(smc);
2843 if (old_state == SMC_ACTIVE &&
2844 sk->sk_state == SMC_PEERCLOSEWAIT1)
2845 do_shutdown = false;
2848 rc = smc_close_shutdown_write(smc);
2852 /* nothing more to do because peer is not involved */
2855 if (do_shutdown && smc->clcsock)
2856 rc1 = kernel_sock_shutdown(smc->clcsock, how);
2857 /* map sock_shutdown_cmd constants to sk_shutdown value range */
2858 sk->sk_shutdown |= how + 1;
2860 if (sk->sk_state == SMC_CLOSED)
2861 sock->state = SS_UNCONNECTED;
2863 sock->state = SS_DISCONNECTING;
2866 return rc ? rc : rc1;
2869 static int __smc_getsockopt(struct socket *sock, int level, int optname,
2870 char __user *optval, int __user *optlen)
2872 struct smc_sock *smc;
2875 smc = smc_sk(sock->sk);
2877 if (get_user(len, optlen))
2880 len = min_t(int, len, sizeof(int));
2887 val = smc->limit_smc_hs;
2893 if (put_user(len, optlen))
2895 if (copy_to_user(optval, &val, len))
2901 static int __smc_setsockopt(struct socket *sock, int level, int optname,
2902 sockptr_t optval, unsigned int optlen)
2904 struct sock *sk = sock->sk;
2905 struct smc_sock *smc;
2913 if (optlen < sizeof(int)) {
2917 if (copy_from_sockptr(&val, optval, sizeof(int))) {
2922 smc->limit_smc_hs = !!val;
2934 static int smc_setsockopt(struct socket *sock, int level, int optname,
2935 sockptr_t optval, unsigned int optlen)
2937 struct sock *sk = sock->sk;
2938 struct smc_sock *smc;
2941 if (level == SOL_TCP && optname == TCP_ULP)
2943 else if (level == SOL_SMC)
2944 return __smc_setsockopt(sock, level, optname, optval, optlen);
2948 /* generic setsockopts reaching us here always apply to the
2951 mutex_lock(&smc->clcsock_release_lock);
2952 if (!smc->clcsock) {
2953 mutex_unlock(&smc->clcsock_release_lock);
2956 if (unlikely(!smc->clcsock->ops->setsockopt))
2959 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
2961 if (smc->clcsock->sk->sk_err) {
2962 sk->sk_err = smc->clcsock->sk->sk_err;
2963 sk_error_report(sk);
2965 mutex_unlock(&smc->clcsock_release_lock);
2967 if (optlen < sizeof(int))
2969 if (copy_from_sockptr(&val, optval, sizeof(int)))
2973 if (rc || smc->use_fallback)
2977 case TCP_FASTOPEN_CONNECT:
2978 case TCP_FASTOPEN_KEY:
2979 case TCP_FASTOPEN_NO_COOKIE:
2980 /* option not supported by SMC */
2981 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
2982 rc = smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP);
2988 if (sk->sk_state != SMC_INIT &&
2989 sk->sk_state != SMC_LISTEN &&
2990 sk->sk_state != SMC_CLOSED) {
2992 SMC_STAT_INC(smc, ndly_cnt);
2993 smc_tx_pending(&smc->conn);
2994 cancel_delayed_work(&smc->conn.tx_work);
2999 if (sk->sk_state != SMC_INIT &&
3000 sk->sk_state != SMC_LISTEN &&
3001 sk->sk_state != SMC_CLOSED) {
3003 SMC_STAT_INC(smc, cork_cnt);
3004 smc_tx_pending(&smc->conn);
3005 cancel_delayed_work(&smc->conn.tx_work);
3009 case TCP_DEFER_ACCEPT:
3010 smc->sockopt_defer_accept = val;
3021 static int smc_getsockopt(struct socket *sock, int level, int optname,
3022 char __user *optval, int __user *optlen)
3024 struct smc_sock *smc;
3027 if (level == SOL_SMC)
3028 return __smc_getsockopt(sock, level, optname, optval, optlen);
3030 smc = smc_sk(sock->sk);
3031 mutex_lock(&smc->clcsock_release_lock);
3032 if (!smc->clcsock) {
3033 mutex_unlock(&smc->clcsock_release_lock);
3036 /* socket options apply to the CLC socket */
3037 if (unlikely(!smc->clcsock->ops->getsockopt)) {
3038 mutex_unlock(&smc->clcsock_release_lock);
3041 rc = smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
3043 mutex_unlock(&smc->clcsock_release_lock);
3047 static int smc_ioctl(struct socket *sock, unsigned int cmd,
3050 union smc_host_cursor cons, urg;
3051 struct smc_connection *conn;
3052 struct smc_sock *smc;
3055 smc = smc_sk(sock->sk);
3057 lock_sock(&smc->sk);
3058 if (smc->use_fallback) {
3059 if (!smc->clcsock) {
3060 release_sock(&smc->sk);
3063 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
3064 release_sock(&smc->sk);
3068 case SIOCINQ: /* same as FIONREAD */
3069 if (smc->sk.sk_state == SMC_LISTEN) {
3070 release_sock(&smc->sk);
3073 if (smc->sk.sk_state == SMC_INIT ||
3074 smc->sk.sk_state == SMC_CLOSED)
3077 answ = atomic_read(&smc->conn.bytes_to_rcv);
3080 /* output queue size (not send + not acked) */
3081 if (smc->sk.sk_state == SMC_LISTEN) {
3082 release_sock(&smc->sk);
3085 if (smc->sk.sk_state == SMC_INIT ||
3086 smc->sk.sk_state == SMC_CLOSED)
3089 answ = smc->conn.sndbuf_desc->len -
3090 atomic_read(&smc->conn.sndbuf_space);
3093 /* output queue size (not send only) */
3094 if (smc->sk.sk_state == SMC_LISTEN) {
3095 release_sock(&smc->sk);
3098 if (smc->sk.sk_state == SMC_INIT ||
3099 smc->sk.sk_state == SMC_CLOSED)
3102 answ = smc_tx_prepared_sends(&smc->conn);
3105 if (smc->sk.sk_state == SMC_LISTEN) {
3106 release_sock(&smc->sk);
3109 if (smc->sk.sk_state == SMC_INIT ||
3110 smc->sk.sk_state == SMC_CLOSED) {
3113 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
3114 smc_curs_copy(&urg, &conn->urg_curs, conn);
3115 answ = smc_curs_diff(conn->rmb_desc->len,
3120 release_sock(&smc->sk);
3121 return -ENOIOCTLCMD;
3123 release_sock(&smc->sk);
3125 return put_user(answ, (int __user *)arg);
3128 static ssize_t smc_sendpage(struct socket *sock, struct page *page,
3129 int offset, size_t size, int flags)
3131 struct sock *sk = sock->sk;
3132 struct smc_sock *smc;
3137 if (sk->sk_state != SMC_ACTIVE) {
3142 if (smc->use_fallback) {
3143 rc = kernel_sendpage(smc->clcsock, page, offset,
3147 rc = smc_tx_sendpage(smc, page, offset, size, flags);
3149 SMC_STAT_INC(smc, sendpage_cnt);
3156 /* Map the affected portions of the rmbe into an spd, note the number of bytes
3157 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
3158 * updates till whenever a respective page has been fully processed.
3159 * Note that subsequent recv() calls have to wait till all splice() processing
3162 static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
3163 struct pipe_inode_info *pipe, size_t len,
3166 struct sock *sk = sock->sk;
3167 struct smc_sock *smc;
3172 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
3173 /* socket was connected before, no more data to read */
3177 if (sk->sk_state == SMC_INIT ||
3178 sk->sk_state == SMC_LISTEN ||
3179 sk->sk_state == SMC_CLOSED)
3182 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
3187 if (smc->use_fallback) {
3188 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
3195 if (flags & SPLICE_F_NONBLOCK)
3196 flags = MSG_DONTWAIT;
3199 SMC_STAT_INC(smc, splice_cnt);
3200 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
3208 /* must look like tcp */
3209 static const struct proto_ops smc_sock_ops = {
3211 .owner = THIS_MODULE,
3212 .release = smc_release,
3214 .connect = smc_connect,
3215 .socketpair = sock_no_socketpair,
3216 .accept = smc_accept,
3217 .getname = smc_getname,
3220 .listen = smc_listen,
3221 .shutdown = smc_shutdown,
3222 .setsockopt = smc_setsockopt,
3223 .getsockopt = smc_getsockopt,
3224 .sendmsg = smc_sendmsg,
3225 .recvmsg = smc_recvmsg,
3226 .mmap = sock_no_mmap,
3227 .sendpage = smc_sendpage,
3228 .splice_read = smc_splice_read,
3231 static int __smc_create(struct net *net, struct socket *sock, int protocol,
3232 int kern, struct socket *clcsock)
3234 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
3235 struct smc_sock *smc;
3239 rc = -ESOCKTNOSUPPORT;
3240 if (sock->type != SOCK_STREAM)
3243 rc = -EPROTONOSUPPORT;
3244 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
3248 sock->ops = &smc_sock_ops;
3249 sock->state = SS_UNCONNECTED;
3250 sk = smc_sock_alloc(net, sock, protocol);
3254 /* create internal TCP socket for CLC handshake and fallback */
3256 smc->use_fallback = false; /* assume rdma capability first */
3257 smc->fallback_rsn = 0;
3259 /* default behavior from limit_smc_hs in every net namespace */
3260 smc->limit_smc_hs = net->smc.limit_smc_hs;
3264 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
3267 sk_common_release(sk);
3271 smc->clcsock = clcsock;
3278 static int smc_create(struct net *net, struct socket *sock, int protocol,
3281 return __smc_create(net, sock, protocol, kern, NULL);
3284 static const struct net_proto_family smc_sock_family_ops = {
3286 .owner = THIS_MODULE,
3287 .create = smc_create,
3290 static int smc_ulp_init(struct sock *sk)
3292 struct socket *tcp = sk->sk_socket;
3293 struct net *net = sock_net(sk);
3294 struct socket *smcsock;
3297 /* only TCP can be replaced */
3298 if (tcp->type != SOCK_STREAM || sk->sk_protocol != IPPROTO_TCP ||
3299 (sk->sk_family != AF_INET && sk->sk_family != AF_INET6))
3300 return -ESOCKTNOSUPPORT;
3301 /* don't handle wq now */
3302 if (tcp->state != SS_UNCONNECTED || !tcp->file || tcp->wq.fasync_list)
3305 if (sk->sk_family == AF_INET)
3306 protocol = SMCPROTO_SMC;
3308 protocol = SMCPROTO_SMC6;
3310 smcsock = sock_alloc();
3314 smcsock->type = SOCK_STREAM;
3315 __module_get(THIS_MODULE); /* tried in __tcp_ulp_find_autoload */
3316 ret = __smc_create(net, smcsock, protocol, 1, tcp);
3318 sock_release(smcsock); /* module_put() which ops won't be NULL */
3322 /* replace tcp socket to smc */
3323 smcsock->file = tcp->file;
3324 smcsock->file->private_data = smcsock;
3325 smcsock->file->f_inode = SOCK_INODE(smcsock); /* replace inode when sock_close */
3326 smcsock->file->f_path.dentry->d_inode = SOCK_INODE(smcsock); /* dput() in __fput */
3332 static void smc_ulp_clone(const struct request_sock *req, struct sock *newsk,
3333 const gfp_t priority)
3335 struct inet_connection_sock *icsk = inet_csk(newsk);
3337 /* don't inherit ulp ops to child when listen */
3338 icsk->icsk_ulp_ops = NULL;
3341 static struct tcp_ulp_ops smc_ulp_ops __read_mostly = {
3343 .owner = THIS_MODULE,
3344 .init = smc_ulp_init,
3345 .clone = smc_ulp_clone,
3348 unsigned int smc_net_id;
3350 static __net_init int smc_net_init(struct net *net)
3354 rc = smc_sysctl_net_init(net);
3357 return smc_pnet_net_init(net);
3360 static void __net_exit smc_net_exit(struct net *net)
3362 smc_sysctl_net_exit(net);
3363 smc_pnet_net_exit(net);
3366 static __net_init int smc_net_stat_init(struct net *net)
3368 return smc_stats_init(net);
3371 static void __net_exit smc_net_stat_exit(struct net *net)
3373 smc_stats_exit(net);
3376 static struct pernet_operations smc_net_ops = {
3377 .init = smc_net_init,
3378 .exit = smc_net_exit,
3380 .size = sizeof(struct smc_net),
3383 static struct pernet_operations smc_net_stat_ops = {
3384 .init = smc_net_stat_init,
3385 .exit = smc_net_stat_exit,
3388 static int __init smc_init(void)
3392 rc = register_pernet_subsys(&smc_net_ops);
3396 rc = register_pernet_subsys(&smc_net_stat_ops);
3398 goto out_pernet_subsys;
3400 rc = smc_ism_init();
3402 goto out_pernet_subsys_stat;
3409 rc = smc_pnet_init();
3415 smc_tcp_ls_wq = alloc_workqueue("smc_tcp_ls_wq", 0, 0);
3419 smc_hs_wq = alloc_workqueue("smc_hs_wq", 0, 0);
3421 goto out_alloc_tcp_ls_wq;
3423 smc_close_wq = alloc_workqueue("smc_close_wq", 0, 0);
3425 goto out_alloc_hs_wq;
3427 rc = smc_core_init();
3429 pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
3433 rc = smc_llc_init();
3435 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
3439 rc = smc_cdc_init();
3441 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
3445 rc = proto_register(&smc_proto, 1);
3447 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
3451 rc = proto_register(&smc_proto6, 1);
3453 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
3457 rc = sock_register(&smc_sock_family_ops);
3459 pr_err("%s: sock_register fails with %d\n", __func__, rc);
3462 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
3463 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
3465 rc = smc_ib_register_client();
3467 pr_err("%s: ib_register fails with %d\n", __func__, rc);
3471 rc = tcp_register_ulp(&smc_ulp_ops);
3473 pr_err("%s: tcp_ulp_register fails with %d\n", __func__, rc);
3477 static_branch_enable(&tcp_have_smc);
3481 smc_ib_unregister_client();
3483 sock_unregister(PF_SMC);
3485 proto_unregister(&smc_proto6);
3487 proto_unregister(&smc_proto);
3491 destroy_workqueue(smc_close_wq);
3493 destroy_workqueue(smc_hs_wq);
3494 out_alloc_tcp_ls_wq:
3495 destroy_workqueue(smc_tcp_ls_wq);
3502 out_pernet_subsys_stat:
3503 unregister_pernet_subsys(&smc_net_stat_ops);
3505 unregister_pernet_subsys(&smc_net_ops);
3510 static void __exit smc_exit(void)
3512 static_branch_disable(&tcp_have_smc);
3513 tcp_unregister_ulp(&smc_ulp_ops);
3514 sock_unregister(PF_SMC);
3516 smc_ib_unregister_client();
3518 destroy_workqueue(smc_close_wq);
3519 destroy_workqueue(smc_tcp_ls_wq);
3520 destroy_workqueue(smc_hs_wq);
3521 proto_unregister(&smc_proto6);
3522 proto_unregister(&smc_proto);
3526 unregister_pernet_subsys(&smc_net_stat_ops);
3527 unregister_pernet_subsys(&smc_net_ops);
3531 module_init(smc_init);
3532 module_exit(smc_exit);
3534 MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
3535 MODULE_DESCRIPTION("smc socket address family");
3536 MODULE_LICENSE("GPL");
3537 MODULE_ALIAS_NETPROTO(PF_SMC);
3538 MODULE_ALIAS_TCP_ULP("smc");
3539 MODULE_ALIAS_GENL_FAMILY(SMC_GENL_FAMILY_NAME);