2 * Shared Memory Communications over RDMA (SMC-R) and RoCE
4 * AF_SMC protocol family socket handler keeping the AF_INET sock address type
5 * applies to SOCK_STREAM sockets only
6 * offers an alternative communication option for TCP-protocol sockets
7 * applicable with RoCE-cards only
9 * Initial restrictions:
10 * - non-blocking connect postponed
11 * - IPv6 support postponed
12 * - support for alternate links postponed
13 * - partial support for non-blocking sockets only
14 * - support for urgent data postponed
16 * Copyright IBM Corp. 2016
18 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
19 * based on prototype from Frank Blaschka
22 #define KMSG_COMPONENT "smc"
23 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
25 #include <linux/module.h>
26 #include <linux/socket.h>
27 #include <linux/inetdevice.h>
28 #include <linux/workqueue.h>
30 #include <linux/sched/signal.h>
45 #include "smc_close.h"
47 static DEFINE_MUTEX(smc_create_lgr_pending); /* serialize link group
51 struct smc_lgr_list smc_lgr_list = { /* established link groups */
52 .lock = __SPIN_LOCK_UNLOCKED(smc_lgr_list.lock),
53 .list = LIST_HEAD_INIT(smc_lgr_list.list),
56 static void smc_tcp_listen_work(struct work_struct *);
58 static void smc_set_keepalive(struct sock *sk, int val)
60 struct smc_sock *smc = smc_sk(sk);
62 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
65 static struct smc_hashinfo smc_v4_hashinfo = {
66 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
69 int smc_hash_sk(struct sock *sk)
71 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
72 struct hlist_head *head;
76 write_lock_bh(&h->lock);
77 sk_add_node(sk, head);
78 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
79 write_unlock_bh(&h->lock);
83 EXPORT_SYMBOL_GPL(smc_hash_sk);
85 void smc_unhash_sk(struct sock *sk)
87 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
89 write_lock_bh(&h->lock);
90 if (sk_del_node_init(sk))
91 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
92 write_unlock_bh(&h->lock);
94 EXPORT_SYMBOL_GPL(smc_unhash_sk);
96 struct proto smc_proto = {
99 .keepalive = smc_set_keepalive,
101 .unhash = smc_unhash_sk,
102 .obj_size = sizeof(struct smc_sock),
103 .h.smc_hash = &smc_v4_hashinfo,
104 .slab_flags = SLAB_TYPESAFE_BY_RCU,
106 EXPORT_SYMBOL_GPL(smc_proto);
108 static int smc_release(struct socket *sock)
110 struct sock *sk = sock->sk;
111 struct smc_sock *smc;
118 if (sk->sk_state == SMC_LISTEN)
119 /* smc_close_non_accepted() is called and acquires
120 * sock lock for child sockets again
122 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
126 if (!smc->use_fallback) {
127 rc = smc_close_active(smc);
128 sock_set_flag(sk, SOCK_DEAD);
129 sk->sk_shutdown |= SHUTDOWN_MASK;
132 sock_release(smc->clcsock);
135 if (smc->use_fallback) {
136 sock_put(sk); /* passive closing */
137 sk->sk_state = SMC_CLOSED;
138 sk->sk_state_change(sk);
144 if (!smc->use_fallback && sk->sk_state == SMC_CLOSED)
145 smc_conn_free(&smc->conn);
148 sk->sk_prot->unhash(sk);
149 sock_put(sk); /* final sock_put */
154 static void smc_destruct(struct sock *sk)
156 if (sk->sk_state != SMC_CLOSED)
158 if (!sock_flag(sk, SOCK_DEAD))
161 sk_refcnt_debug_dec(sk);
164 static struct sock *smc_sock_alloc(struct net *net, struct socket *sock)
166 struct smc_sock *smc;
169 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, &smc_proto, 0);
173 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
174 sk->sk_state = SMC_INIT;
175 sk->sk_destruct = smc_destruct;
176 sk->sk_protocol = SMCPROTO_SMC;
178 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
179 INIT_LIST_HEAD(&smc->accept_q);
180 spin_lock_init(&smc->accept_q_lock);
181 sk->sk_prot->hash(sk);
182 sk_refcnt_debug_inc(sk);
187 static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
190 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
191 struct sock *sk = sock->sk;
192 struct smc_sock *smc;
197 /* replicate tests from inet_bind(), to be safe wrt. future changes */
199 if (addr_len < sizeof(struct sockaddr_in))
203 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
204 if ((addr->sin_family != AF_INET) &&
205 ((addr->sin_family != AF_UNSPEC) ||
206 (addr->sin_addr.s_addr != htonl(INADDR_ANY))))
211 /* Check if socket is already active */
213 if (sk->sk_state != SMC_INIT)
216 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
217 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
225 static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
228 /* options we don't get control via setsockopt for */
229 nsk->sk_type = osk->sk_type;
230 nsk->sk_sndbuf = osk->sk_sndbuf;
231 nsk->sk_rcvbuf = osk->sk_rcvbuf;
232 nsk->sk_sndtimeo = osk->sk_sndtimeo;
233 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
234 nsk->sk_mark = osk->sk_mark;
235 nsk->sk_priority = osk->sk_priority;
236 nsk->sk_rcvlowat = osk->sk_rcvlowat;
237 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
238 nsk->sk_err = osk->sk_err;
240 nsk->sk_flags &= ~mask;
241 nsk->sk_flags |= osk->sk_flags & mask;
244 #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
245 (1UL << SOCK_KEEPOPEN) | \
246 (1UL << SOCK_LINGER) | \
247 (1UL << SOCK_BROADCAST) | \
248 (1UL << SOCK_TIMESTAMP) | \
249 (1UL << SOCK_DBG) | \
250 (1UL << SOCK_RCVTSTAMP) | \
251 (1UL << SOCK_RCVTSTAMPNS) | \
252 (1UL << SOCK_LOCALROUTE) | \
253 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
254 (1UL << SOCK_RXQ_OVFL) | \
255 (1UL << SOCK_WIFI_STATUS) | \
256 (1UL << SOCK_NOFCS) | \
257 (1UL << SOCK_FILTER_LOCKED))
258 /* copy only relevant settings and flags of SOL_SOCKET level from smc to
259 * clc socket (since smc is not called for these options from net/core)
261 static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
263 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
266 #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
267 (1UL << SOCK_KEEPOPEN) | \
268 (1UL << SOCK_LINGER) | \
270 /* copy only settings and flags relevant for smc from clc to smc socket */
271 static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
273 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
276 /* determine subnet and mask of internal TCP socket */
277 int smc_netinfo_by_tcpsk(struct socket *clcsock,
278 __be32 *subnet, u8 *prefix_len)
280 struct dst_entry *dst = sk_dst_get(clcsock->sk);
281 struct in_device *in_dev;
282 struct sockaddr_in addr;
295 /* get address to which the internal TCP socket is bound */
296 kernel_getsockname(clcsock, (struct sockaddr *)&addr, &len);
297 /* analyze IPv4 specific data of net_device belonging to TCP socket */
299 in_dev = __in_dev_get_rcu(dst->dev);
301 if (!inet_ifa_match(addr.sin_addr.s_addr, ifa))
303 *prefix_len = inet_mask_len(ifa->ifa_mask);
304 *subnet = ifa->ifa_address & ifa->ifa_mask;
307 } endfor_ifa(in_dev);
316 static int smc_clnt_conf_first_link(struct smc_sock *smc, union ib_gid *gid)
318 struct smc_link_group *lgr = smc->conn.lgr;
319 struct smc_link *link;
323 link = &lgr->lnk[SMC_SINGLE_LINK];
324 /* receive CONFIRM LINK request from server over RoCE fabric */
325 rest = wait_for_completion_interruptible_timeout(
327 SMC_LLC_WAIT_FIRST_TIME);
329 struct smc_clc_msg_decline dclc;
331 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
336 rc = smc_ib_modify_qp_rts(link);
338 return SMC_CLC_DECL_INTERR;
340 smc_wr_remember_qp_attr(link);
342 rc = smc_wr_reg_send(link,
343 smc->conn.rmb_desc->mr_rx[SMC_SINGLE_LINK]);
345 return SMC_CLC_DECL_INTERR;
347 /* send CONFIRM LINK response over RoCE fabric */
348 rc = smc_llc_send_confirm_link(link,
349 link->smcibdev->mac[link->ibport - 1],
352 return SMC_CLC_DECL_TCL;
357 static void smc_conn_save_peer_info(struct smc_sock *smc,
358 struct smc_clc_msg_accept_confirm *clc)
360 smc->conn.peer_conn_idx = clc->conn_idx;
361 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
362 smc->conn.peer_rmbe_size = smc_uncompress_bufsize(clc->rmbe_size);
363 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
366 static void smc_link_save_peer_info(struct smc_link *link,
367 struct smc_clc_msg_accept_confirm *clc)
369 link->peer_qpn = ntoh24(clc->qpn);
370 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
371 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
372 link->peer_psn = ntoh24(clc->psn);
373 link->peer_mtu = clc->qp_mtu;
376 static void smc_lgr_forget(struct smc_link_group *lgr)
378 spin_lock_bh(&smc_lgr_list.lock);
379 /* do not use this link group for new connections */
380 if (!list_empty(&lgr->list))
381 list_del_init(&lgr->list);
382 spin_unlock_bh(&smc_lgr_list.lock);
385 /* setup for RDMA connection of client */
386 static int smc_connect_rdma(struct smc_sock *smc)
388 struct sockaddr_in *inaddr = (struct sockaddr_in *)smc->addr;
389 struct smc_clc_msg_accept_confirm aclc;
390 int local_contact = SMC_FIRST_CONTACT;
391 struct smc_ib_device *smcibdev;
392 struct smc_link *link;
393 u8 srv_first_contact;
398 sock_hold(&smc->sk); /* sock put in passive closing */
400 if (!tcp_sk(smc->clcsock->sk)->syn_smc) {
401 /* peer has not signalled SMC-capability */
402 smc->use_fallback = true;
406 /* IPSec connections opt out of SMC-R optimizations */
407 if (using_ipsec(smc)) {
408 reason_code = SMC_CLC_DECL_IPSEC;
412 /* PNET table look up: search active ib_device and port
413 * within same PNETID that also contains the ethernet device
414 * used for the internal TCP socket
416 smc_pnet_find_roce_resource(smc->clcsock->sk, &smcibdev, &ibport);
418 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
422 /* do inband token exchange */
423 reason_code = smc_clc_send_proposal(smc, smcibdev, ibport);
424 if (reason_code < 0) {
428 if (reason_code > 0) /* configuration error */
430 /* receive SMC Accept CLC message */
431 reason_code = smc_clc_wait_msg(smc, &aclc, sizeof(aclc),
433 if (reason_code < 0) {
440 srv_first_contact = aclc.hdr.flag;
441 mutex_lock(&smc_create_lgr_pending);
442 local_contact = smc_conn_create(smc, inaddr->sin_addr.s_addr, smcibdev,
443 ibport, &aclc.lcl, srv_first_contact);
444 if (local_contact < 0) {
447 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
448 else if (rc == -ENOLINK)
449 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
450 goto decline_rdma_unlock;
452 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
454 smc_conn_save_peer_info(smc, &aclc);
456 /* create send buffer and rmb */
457 rc = smc_buf_create(smc);
459 reason_code = SMC_CLC_DECL_MEM;
460 goto decline_rdma_unlock;
463 if (local_contact == SMC_FIRST_CONTACT)
464 smc_link_save_peer_info(link, &aclc);
466 rc = smc_rmb_rtoken_handling(&smc->conn, &aclc);
468 reason_code = SMC_CLC_DECL_INTERR;
469 goto decline_rdma_unlock;
475 if (local_contact == SMC_FIRST_CONTACT) {
476 rc = smc_ib_ready_link(link);
478 reason_code = SMC_CLC_DECL_INTERR;
479 goto decline_rdma_unlock;
482 struct smc_buf_desc *buf_desc = smc->conn.rmb_desc;
484 if (!buf_desc->reused) {
485 /* register memory region for new rmb */
486 rc = smc_wr_reg_send(link,
487 buf_desc->mr_rx[SMC_SINGLE_LINK]);
489 reason_code = SMC_CLC_DECL_INTERR;
490 goto decline_rdma_unlock;
494 smc_rmb_sync_sg_for_device(&smc->conn);
496 rc = smc_clc_send_confirm(smc);
500 if (local_contact == SMC_FIRST_CONTACT) {
501 /* QP confirmation over RoCE fabric */
502 reason_code = smc_clnt_conf_first_link(
503 smc, &smcibdev->gid[ibport - 1]);
504 if (reason_code < 0) {
509 goto decline_rdma_unlock;
512 mutex_unlock(&smc_create_lgr_pending);
516 smc_copy_sock_settings_to_clc(smc);
517 if (smc->sk.sk_state == SMC_INIT)
518 smc->sk.sk_state = SMC_ACTIVE;
520 return rc ? rc : local_contact;
523 if (local_contact == SMC_FIRST_CONTACT)
524 smc_lgr_forget(smc->conn.lgr);
525 mutex_unlock(&smc_create_lgr_pending);
526 smc_conn_free(&smc->conn);
528 /* RDMA setup failed, switch back to TCP */
529 smc->use_fallback = true;
530 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
531 rc = smc_clc_send_decline(smc, reason_code);
538 if (local_contact == SMC_FIRST_CONTACT)
539 smc_lgr_forget(smc->conn.lgr);
540 mutex_unlock(&smc_create_lgr_pending);
541 smc_conn_free(&smc->conn);
543 if (smc->sk.sk_state == SMC_INIT)
544 sock_put(&smc->sk); /* passive closing */
548 static int smc_connect(struct socket *sock, struct sockaddr *addr,
551 struct sock *sk = sock->sk;
552 struct smc_sock *smc;
557 /* separate smc parameter checking to be safe */
558 if (alen < sizeof(addr->sa_family))
560 if (addr->sa_family != AF_INET)
562 smc->addr = addr; /* needed for nonblocking connect */
565 switch (sk->sk_state) {
576 smc_copy_sock_settings_to_clc(smc);
577 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
578 rc = kernel_connect(smc->clcsock, addr, alen, flags);
582 /* setup RDMA connection */
583 rc = smc_connect_rdma(smc);
587 rc = 0; /* success cases including fallback */
595 static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
597 struct socket *new_clcsock = NULL;
598 struct sock *lsk = &lsmc->sk;
603 new_sk = smc_sock_alloc(sock_net(lsk), NULL);
606 lsk->sk_err = ENOMEM;
611 *new_smc = smc_sk(new_sk);
613 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
617 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
619 sock_release(new_clcsock);
620 new_sk->sk_state = SMC_CLOSED;
621 sock_set_flag(new_sk, SOCK_DEAD);
622 new_sk->sk_prot->unhash(new_sk);
623 sock_put(new_sk); /* final */
628 (*new_smc)->clcsock = new_clcsock;
633 /* add a just created sock to the accept queue of the listen sock as
634 * candidate for a following socket accept call from user space
636 static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
638 struct smc_sock *par = smc_sk(parent);
640 sock_hold(sk); /* sock_put in smc_accept_unlink () */
641 spin_lock(&par->accept_q_lock);
642 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
643 spin_unlock(&par->accept_q_lock);
644 sk_acceptq_added(parent);
647 /* remove a socket from the accept queue of its parental listening socket */
648 static void smc_accept_unlink(struct sock *sk)
650 struct smc_sock *par = smc_sk(sk)->listen_smc;
652 spin_lock(&par->accept_q_lock);
653 list_del_init(&smc_sk(sk)->accept_q);
654 spin_unlock(&par->accept_q_lock);
655 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
656 sock_put(sk); /* sock_hold in smc_accept_enqueue */
659 /* remove a sock from the accept queue to bind it to a new socket created
660 * for a socket accept call from user space
662 struct sock *smc_accept_dequeue(struct sock *parent,
663 struct socket *new_sock)
665 struct smc_sock *isk, *n;
668 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
669 new_sk = (struct sock *)isk;
671 smc_accept_unlink(new_sk);
672 if (new_sk->sk_state == SMC_CLOSED) {
674 sock_release(isk->clcsock);
677 new_sk->sk_prot->unhash(new_sk);
678 sock_put(new_sk); /* final */
682 sock_graft(new_sk, new_sock);
688 /* clean up for a created but never accepted sock */
689 void smc_close_non_accepted(struct sock *sk)
691 struct smc_sock *smc = smc_sk(sk);
694 if (!sk->sk_lingertime)
695 /* wait for peer closing */
696 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
697 if (!smc->use_fallback) {
698 smc_close_active(smc);
699 sock_set_flag(sk, SOCK_DEAD);
700 sk->sk_shutdown |= SHUTDOWN_MASK;
709 if (smc->use_fallback) {
710 sock_put(sk); /* passive closing */
711 sk->sk_state = SMC_CLOSED;
713 if (sk->sk_state == SMC_CLOSED)
714 smc_conn_free(&smc->conn);
717 sk->sk_prot->unhash(sk);
718 sock_put(sk); /* final sock_put */
721 static int smc_serv_conf_first_link(struct smc_sock *smc)
723 struct smc_link_group *lgr = smc->conn.lgr;
724 struct smc_link *link;
728 link = &lgr->lnk[SMC_SINGLE_LINK];
730 rc = smc_wr_reg_send(link,
731 smc->conn.rmb_desc->mr_rx[SMC_SINGLE_LINK]);
733 return SMC_CLC_DECL_INTERR;
735 /* send CONFIRM LINK request to client over the RoCE fabric */
736 rc = smc_llc_send_confirm_link(link,
737 link->smcibdev->mac[link->ibport - 1],
738 &link->smcibdev->gid[link->ibport - 1],
741 return SMC_CLC_DECL_TCL;
743 /* receive CONFIRM LINK response from client over the RoCE fabric */
744 rest = wait_for_completion_interruptible_timeout(
745 &link->llc_confirm_resp,
746 SMC_LLC_WAIT_FIRST_TIME);
748 struct smc_clc_msg_decline dclc;
750 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
757 /* setup for RDMA connection of server */
758 static void smc_listen_work(struct work_struct *work)
760 struct smc_sock *new_smc = container_of(work, struct smc_sock,
762 struct smc_clc_msg_proposal_prefix *pclc_prfx;
763 struct socket *newclcsock = new_smc->clcsock;
764 struct smc_sock *lsmc = new_smc->listen_smc;
765 struct smc_clc_msg_accept_confirm cclc;
766 int local_contact = SMC_REUSE_CONTACT;
767 struct sock *newsmcsk = &new_smc->sk;
768 struct smc_clc_msg_proposal *pclc;
769 struct smc_ib_device *smcibdev;
770 struct sockaddr_in peeraddr;
771 u8 buf[SMC_CLC_MAX_LEN];
772 struct smc_link *link;
779 /* check if peer is smc capable */
780 if (!tcp_sk(newclcsock->sk)->syn_smc) {
781 new_smc->use_fallback = true;
785 /* do inband token exchange -
786 *wait for and receive SMC Proposal CLC message
788 reason_code = smc_clc_wait_msg(new_smc, &buf, sizeof(buf),
795 /* IPSec connections opt out of SMC-R optimizations */
796 if (using_ipsec(new_smc)) {
797 reason_code = SMC_CLC_DECL_IPSEC;
801 /* PNET table look up: search active ib_device and port
802 * within same PNETID that also contains the ethernet device
803 * used for the internal TCP socket
805 smc_pnet_find_roce_resource(newclcsock->sk, &smcibdev, &ibport);
807 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
811 /* determine subnet and mask from internal TCP socket */
812 rc = smc_netinfo_by_tcpsk(newclcsock, &subnet, &prefix_len);
814 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
818 pclc = (struct smc_clc_msg_proposal *)&buf;
819 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
820 if (pclc_prfx->outgoing_subnet != subnet ||
821 pclc_prfx->prefix_len != prefix_len) {
822 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
826 /* get address of the peer connected to the internal TCP socket */
827 kernel_getpeername(newclcsock, (struct sockaddr *)&peeraddr, &len);
829 /* allocate connection / link group */
830 mutex_lock(&smc_create_lgr_pending);
831 local_contact = smc_conn_create(new_smc, peeraddr.sin_addr.s_addr,
832 smcibdev, ibport, &pclc->lcl, 0);
833 if (local_contact < 0) {
836 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
837 goto decline_rdma_unlock;
839 link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
841 /* create send buffer and rmb */
842 rc = smc_buf_create(new_smc);
844 reason_code = SMC_CLC_DECL_MEM;
845 goto decline_rdma_unlock;
848 smc_close_init(new_smc);
849 smc_rx_init(new_smc);
851 if (local_contact != SMC_FIRST_CONTACT) {
852 struct smc_buf_desc *buf_desc = new_smc->conn.rmb_desc;
854 if (!buf_desc->reused) {
855 /* register memory region for new rmb */
856 rc = smc_wr_reg_send(link,
857 buf_desc->mr_rx[SMC_SINGLE_LINK]);
859 reason_code = SMC_CLC_DECL_INTERR;
860 goto decline_rdma_unlock;
864 smc_rmb_sync_sg_for_device(&new_smc->conn);
866 rc = smc_clc_send_accept(new_smc, local_contact);
870 /* receive SMC Confirm CLC message */
871 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
876 goto decline_rdma_unlock;
877 smc_conn_save_peer_info(new_smc, &cclc);
878 if (local_contact == SMC_FIRST_CONTACT)
879 smc_link_save_peer_info(link, &cclc);
881 rc = smc_rmb_rtoken_handling(&new_smc->conn, &cclc);
883 reason_code = SMC_CLC_DECL_INTERR;
884 goto decline_rdma_unlock;
887 if (local_contact == SMC_FIRST_CONTACT) {
888 rc = smc_ib_ready_link(link);
890 reason_code = SMC_CLC_DECL_INTERR;
891 goto decline_rdma_unlock;
893 /* QP confirmation over RoCE fabric */
894 reason_code = smc_serv_conf_first_link(new_smc);
896 /* peer is not aware of a problem */
899 goto decline_rdma_unlock;
902 smc_tx_init(new_smc);
903 mutex_unlock(&smc_create_lgr_pending);
906 sk_refcnt_debug_inc(newsmcsk);
907 if (newsmcsk->sk_state == SMC_INIT)
908 newsmcsk->sk_state = SMC_ACTIVE;
910 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
911 if (lsmc->sk.sk_state == SMC_LISTEN) {
912 smc_accept_enqueue(&lsmc->sk, newsmcsk);
913 } else { /* no longer listening */
914 smc_close_non_accepted(newsmcsk);
916 release_sock(&lsmc->sk);
919 lsmc->sk.sk_data_ready(&lsmc->sk);
920 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
924 if (local_contact == SMC_FIRST_CONTACT)
925 smc_lgr_forget(new_smc->conn.lgr);
926 mutex_unlock(&smc_create_lgr_pending);
928 /* RDMA setup failed, switch back to TCP */
929 smc_conn_free(&new_smc->conn);
930 new_smc->use_fallback = true;
931 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
932 if (smc_clc_send_decline(new_smc, reason_code) < 0)
938 if (local_contact == SMC_FIRST_CONTACT)
939 smc_lgr_forget(new_smc->conn.lgr);
940 mutex_unlock(&smc_create_lgr_pending);
942 if (newsmcsk->sk_state == SMC_INIT)
943 sock_put(&new_smc->sk); /* passive closing */
944 newsmcsk->sk_state = SMC_CLOSED;
945 smc_conn_free(&new_smc->conn);
946 goto enqueue; /* queue new sock with sk_err set */
949 static void smc_tcp_listen_work(struct work_struct *work)
951 struct smc_sock *lsmc = container_of(work, struct smc_sock,
953 struct sock *lsk = &lsmc->sk;
954 struct smc_sock *new_smc;
958 while (lsk->sk_state == SMC_LISTEN) {
959 rc = smc_clcsock_accept(lsmc, &new_smc);
965 new_smc->listen_smc = lsmc;
966 new_smc->use_fallback = false; /* assume rdma capability first*/
967 sock_hold(lsk); /* sock_put in smc_listen_work */
968 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
969 smc_copy_sock_settings_to_smc(new_smc);
970 sock_hold(&new_smc->sk); /* sock_put in passive closing */
971 if (!schedule_work(&new_smc->smc_listen_work))
972 sock_put(&new_smc->sk);
977 sock_release(lsmc->clcsock);
978 lsmc->clcsock = NULL;
981 /* no more listening, wake up smc_close_wait_listen_clcsock and
984 lsk->sk_state_change(lsk);
985 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
988 static int smc_listen(struct socket *sock, int backlog)
990 struct sock *sk = sock->sk;
991 struct smc_sock *smc;
998 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
1002 if (sk->sk_state == SMC_LISTEN) {
1003 sk->sk_max_ack_backlog = backlog;
1006 /* some socket options are handled in core, so we could not apply
1007 * them to the clc socket -- copy smc socket options to clc socket
1009 smc_copy_sock_settings_to_clc(smc);
1010 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
1012 rc = kernel_listen(smc->clcsock, backlog);
1015 sk->sk_max_ack_backlog = backlog;
1016 sk->sk_ack_backlog = 0;
1017 sk->sk_state = SMC_LISTEN;
1018 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
1019 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1020 if (!schedule_work(&smc->tcp_listen_work))
1028 static int smc_accept(struct socket *sock, struct socket *new_sock,
1029 int flags, bool kern)
1031 struct sock *sk = sock->sk, *nsk;
1032 DECLARE_WAITQUEUE(wait, current);
1033 struct smc_sock *lsmc;
1038 sock_hold(sk); /* sock_put below */
1041 if (lsmc->sk.sk_state != SMC_LISTEN) {
1046 /* Wait for an incoming connection */
1047 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1048 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1049 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1050 set_current_state(TASK_INTERRUPTIBLE);
1056 timeo = schedule_timeout(timeo);
1057 /* wakeup by sk_data_ready in smc_listen_work() */
1058 sched_annotate_sleep();
1060 if (signal_pending(current)) {
1061 rc = sock_intr_errno(timeo);
1065 set_current_state(TASK_RUNNING);
1066 remove_wait_queue(sk_sleep(sk), &wait);
1069 rc = sock_error(nsk);
1073 sock_put(sk); /* sock_hold above */
1077 static int smc_getname(struct socket *sock, struct sockaddr *addr,
1080 struct smc_sock *smc;
1082 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1083 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
1086 smc = smc_sk(sock->sk);
1088 return smc->clcsock->ops->getname(smc->clcsock, addr, len, peer);
1091 static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1093 struct sock *sk = sock->sk;
1094 struct smc_sock *smc;
1099 if ((sk->sk_state != SMC_ACTIVE) &&
1100 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1101 (sk->sk_state != SMC_INIT))
1103 if (smc->use_fallback)
1104 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1106 rc = smc_tx_sendmsg(smc, msg, len);
1112 static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1115 struct sock *sk = sock->sk;
1116 struct smc_sock *smc;
1121 if ((sk->sk_state == SMC_INIT) ||
1122 (sk->sk_state == SMC_LISTEN) ||
1123 (sk->sk_state == SMC_CLOSED))
1126 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1131 if (smc->use_fallback)
1132 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
1134 rc = smc_rx_recvmsg(smc, msg, len, flags);
1141 static __poll_t smc_accept_poll(struct sock *parent)
1143 struct smc_sock *isk = smc_sk(parent);
1146 spin_lock(&isk->accept_q_lock);
1147 if (!list_empty(&isk->accept_q))
1148 mask = EPOLLIN | EPOLLRDNORM;
1149 spin_unlock(&isk->accept_q_lock);
1154 static __poll_t smc_poll(struct file *file, struct socket *sock,
1157 struct sock *sk = sock->sk;
1159 struct smc_sock *smc;
1165 smc = smc_sk(sock->sk);
1168 if ((sk->sk_state == SMC_INIT) || smc->use_fallback) {
1169 /* delegate to CLC child sock */
1171 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
1172 /* if non-blocking connect finished ... */
1174 if ((sk->sk_state == SMC_INIT) && (mask & EPOLLOUT)) {
1175 sk->sk_err = smc->clcsock->sk->sk_err;
1179 rc = smc_connect_rdma(smc);
1182 /* success cases including fallback */
1183 mask |= EPOLLOUT | EPOLLWRNORM;
1187 if (sk->sk_state != SMC_CLOSED) {
1189 sock_poll_wait(file, sk_sleep(sk), wait);
1194 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1195 (sk->sk_state == SMC_CLOSED))
1197 if (sk->sk_state == SMC_LISTEN) {
1198 /* woken up by sk_data_ready in smc_listen_work() */
1199 mask = smc_accept_poll(sk);
1201 if (atomic_read(&smc->conn.sndbuf_space) ||
1202 sk->sk_shutdown & SEND_SHUTDOWN) {
1203 mask |= EPOLLOUT | EPOLLWRNORM;
1205 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1206 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1208 if (atomic_read(&smc->conn.bytes_to_rcv))
1209 mask |= EPOLLIN | EPOLLRDNORM;
1210 if (sk->sk_shutdown & RCV_SHUTDOWN)
1211 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
1212 if (sk->sk_state == SMC_APPCLOSEWAIT1)
1223 static int smc_shutdown(struct socket *sock, int how)
1225 struct sock *sk = sock->sk;
1226 struct smc_sock *smc;
1232 if ((how < SHUT_RD) || (how > SHUT_RDWR))
1238 if ((sk->sk_state != SMC_LISTEN) &&
1239 (sk->sk_state != SMC_ACTIVE) &&
1240 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1241 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1242 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1243 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1244 (sk->sk_state != SMC_APPFINCLOSEWAIT))
1246 if (smc->use_fallback) {
1247 rc = kernel_sock_shutdown(smc->clcsock, how);
1248 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1249 if (sk->sk_shutdown == SHUTDOWN_MASK)
1250 sk->sk_state = SMC_CLOSED;
1254 case SHUT_RDWR: /* shutdown in both directions */
1255 rc = smc_close_active(smc);
1258 rc = smc_close_shutdown_write(smc);
1261 if (sk->sk_state == SMC_LISTEN)
1262 rc = smc_close_active(smc);
1265 /* nothing more to do because peer is not involved */
1268 rc1 = kernel_sock_shutdown(smc->clcsock, how);
1269 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1270 sk->sk_shutdown |= how + 1;
1274 return rc ? rc : rc1;
1277 static int smc_setsockopt(struct socket *sock, int level, int optname,
1278 char __user *optval, unsigned int optlen)
1280 struct sock *sk = sock->sk;
1281 struct smc_sock *smc;
1285 /* generic setsockopts reaching us here always apply to the
1288 return smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1292 static int smc_getsockopt(struct socket *sock, int level, int optname,
1293 char __user *optval, int __user *optlen)
1295 struct smc_sock *smc;
1297 smc = smc_sk(sock->sk);
1298 /* socket options apply to the CLC socket */
1299 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1303 static int smc_ioctl(struct socket *sock, unsigned int cmd,
1306 struct smc_sock *smc;
1308 smc = smc_sk(sock->sk);
1309 if (smc->use_fallback)
1310 return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1312 return sock_no_ioctl(sock, cmd, arg);
1315 static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1316 int offset, size_t size, int flags)
1318 struct sock *sk = sock->sk;
1319 struct smc_sock *smc;
1324 if (sk->sk_state != SMC_ACTIVE)
1326 if (smc->use_fallback)
1327 rc = kernel_sendpage(smc->clcsock, page, offset,
1330 rc = sock_no_sendpage(sock, page, offset, size, flags);
1337 static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1338 struct pipe_inode_info *pipe, size_t len,
1341 struct sock *sk = sock->sk;
1342 struct smc_sock *smc;
1347 if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED))
1349 if (smc->use_fallback) {
1350 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1360 /* must look like tcp */
1361 static const struct proto_ops smc_sock_ops = {
1363 .owner = THIS_MODULE,
1364 .release = smc_release,
1366 .connect = smc_connect,
1367 .socketpair = sock_no_socketpair,
1368 .accept = smc_accept,
1369 .getname = smc_getname,
1372 .listen = smc_listen,
1373 .shutdown = smc_shutdown,
1374 .setsockopt = smc_setsockopt,
1375 .getsockopt = smc_getsockopt,
1376 .sendmsg = smc_sendmsg,
1377 .recvmsg = smc_recvmsg,
1378 .mmap = sock_no_mmap,
1379 .sendpage = smc_sendpage,
1380 .splice_read = smc_splice_read,
1383 static int smc_create(struct net *net, struct socket *sock, int protocol,
1386 struct smc_sock *smc;
1390 rc = -ESOCKTNOSUPPORT;
1391 if (sock->type != SOCK_STREAM)
1394 rc = -EPROTONOSUPPORT;
1395 if ((protocol != IPPROTO_IP) && (protocol != IPPROTO_TCP))
1399 sock->ops = &smc_sock_ops;
1400 sk = smc_sock_alloc(net, sock);
1404 /* create internal TCP socket for CLC handshake and fallback */
1406 smc->use_fallback = false; /* assume rdma capability first */
1407 rc = sock_create_kern(net, PF_INET, SOCK_STREAM,
1408 IPPROTO_TCP, &smc->clcsock);
1410 sk_common_release(sk);
1413 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1414 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
1420 static const struct net_proto_family smc_sock_family_ops = {
1422 .owner = THIS_MODULE,
1423 .create = smc_create,
1426 static int __init smc_init(void)
1430 rc = smc_pnet_init();
1434 rc = smc_llc_init();
1436 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1440 rc = smc_cdc_init();
1442 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1446 rc = proto_register(&smc_proto, 1);
1448 pr_err("%s: proto_register fails with %d\n", __func__, rc);
1452 rc = sock_register(&smc_sock_family_ops);
1454 pr_err("%s: sock_register fails with %d\n", __func__, rc);
1457 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
1459 rc = smc_ib_register_client();
1461 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1465 static_branch_enable(&tcp_have_smc);
1469 sock_unregister(PF_SMC);
1471 proto_unregister(&smc_proto);
1477 static void __exit smc_exit(void)
1479 struct smc_link_group *lgr, *lg;
1480 LIST_HEAD(lgr_freeing_list);
1482 spin_lock_bh(&smc_lgr_list.lock);
1483 if (!list_empty(&smc_lgr_list.list))
1484 list_splice_init(&smc_lgr_list.list, &lgr_freeing_list);
1485 spin_unlock_bh(&smc_lgr_list.lock);
1486 list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) {
1487 list_del_init(&lgr->list);
1488 smc_lgr_free(lgr); /* free link group */
1490 static_branch_disable(&tcp_have_smc);
1491 smc_ib_unregister_client();
1492 sock_unregister(PF_SMC);
1493 proto_unregister(&smc_proto);
1497 module_init(smc_init);
1498 module_exit(smc_exit);
1500 MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
1501 MODULE_DESCRIPTION("smc socket address family");
1502 MODULE_LICENSE("GPL");
1503 MODULE_ALIAS_NETPROTO(PF_SMC);