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 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
984 static int smc_listen(struct socket *sock, int backlog)
986 struct sock *sk = sock->sk;
987 struct smc_sock *smc;
994 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
998 if (sk->sk_state == SMC_LISTEN) {
999 sk->sk_max_ack_backlog = backlog;
1002 /* some socket options are handled in core, so we could not apply
1003 * them to the clc socket -- copy smc socket options to clc socket
1005 smc_copy_sock_settings_to_clc(smc);
1006 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
1008 rc = kernel_listen(smc->clcsock, backlog);
1011 sk->sk_max_ack_backlog = backlog;
1012 sk->sk_ack_backlog = 0;
1013 sk->sk_state = SMC_LISTEN;
1014 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
1015 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1016 if (!schedule_work(&smc->tcp_listen_work))
1024 static int smc_accept(struct socket *sock, struct socket *new_sock,
1025 int flags, bool kern)
1027 struct sock *sk = sock->sk, *nsk;
1028 DECLARE_WAITQUEUE(wait, current);
1029 struct smc_sock *lsmc;
1034 sock_hold(sk); /* sock_put below */
1037 if (lsmc->sk.sk_state != SMC_LISTEN) {
1042 /* Wait for an incoming connection */
1043 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1044 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1045 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1046 set_current_state(TASK_INTERRUPTIBLE);
1052 timeo = schedule_timeout(timeo);
1053 /* wakeup by sk_data_ready in smc_listen_work() */
1054 sched_annotate_sleep();
1056 if (signal_pending(current)) {
1057 rc = sock_intr_errno(timeo);
1061 set_current_state(TASK_RUNNING);
1062 remove_wait_queue(sk_sleep(sk), &wait);
1065 rc = sock_error(nsk);
1069 sock_put(sk); /* sock_hold above */
1073 static int smc_getname(struct socket *sock, struct sockaddr *addr,
1076 struct smc_sock *smc;
1078 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1079 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
1082 smc = smc_sk(sock->sk);
1084 return smc->clcsock->ops->getname(smc->clcsock, addr, len, peer);
1087 static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1089 struct sock *sk = sock->sk;
1090 struct smc_sock *smc;
1095 if ((sk->sk_state != SMC_ACTIVE) &&
1096 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1097 (sk->sk_state != SMC_INIT))
1099 if (smc->use_fallback)
1100 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1102 rc = smc_tx_sendmsg(smc, msg, len);
1108 static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1111 struct sock *sk = sock->sk;
1112 struct smc_sock *smc;
1117 if ((sk->sk_state == SMC_INIT) ||
1118 (sk->sk_state == SMC_LISTEN) ||
1119 (sk->sk_state == SMC_CLOSED))
1122 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1127 if (smc->use_fallback)
1128 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
1130 rc = smc_rx_recvmsg(smc, msg, len, flags);
1137 static __poll_t smc_accept_poll(struct sock *parent)
1139 struct smc_sock *isk = smc_sk(parent);
1142 spin_lock(&isk->accept_q_lock);
1143 if (!list_empty(&isk->accept_q))
1144 mask = EPOLLIN | EPOLLRDNORM;
1145 spin_unlock(&isk->accept_q_lock);
1150 static __poll_t smc_poll(struct file *file, struct socket *sock,
1153 struct sock *sk = sock->sk;
1155 struct smc_sock *smc;
1161 smc = smc_sk(sock->sk);
1164 if ((sk->sk_state == SMC_INIT) || smc->use_fallback) {
1165 /* delegate to CLC child sock */
1167 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
1168 /* if non-blocking connect finished ... */
1170 if ((sk->sk_state == SMC_INIT) && (mask & EPOLLOUT)) {
1171 sk->sk_err = smc->clcsock->sk->sk_err;
1175 rc = smc_connect_rdma(smc);
1178 /* success cases including fallback */
1179 mask |= EPOLLOUT | EPOLLWRNORM;
1183 if (sk->sk_state != SMC_CLOSED) {
1185 sock_poll_wait(file, sk_sleep(sk), wait);
1190 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1191 (sk->sk_state == SMC_CLOSED))
1193 if (sk->sk_state == SMC_LISTEN) {
1194 /* woken up by sk_data_ready in smc_listen_work() */
1195 mask = smc_accept_poll(sk);
1197 if (atomic_read(&smc->conn.sndbuf_space) ||
1198 sk->sk_shutdown & SEND_SHUTDOWN) {
1199 mask |= EPOLLOUT | EPOLLWRNORM;
1201 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1202 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1204 if (atomic_read(&smc->conn.bytes_to_rcv))
1205 mask |= EPOLLIN | EPOLLRDNORM;
1206 if (sk->sk_shutdown & RCV_SHUTDOWN)
1207 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
1208 if (sk->sk_state == SMC_APPCLOSEWAIT1)
1219 static int smc_shutdown(struct socket *sock, int how)
1221 struct sock *sk = sock->sk;
1222 struct smc_sock *smc;
1228 if ((how < SHUT_RD) || (how > SHUT_RDWR))
1234 if ((sk->sk_state != SMC_LISTEN) &&
1235 (sk->sk_state != SMC_ACTIVE) &&
1236 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1237 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1238 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1239 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1240 (sk->sk_state != SMC_APPFINCLOSEWAIT))
1242 if (smc->use_fallback) {
1243 rc = kernel_sock_shutdown(smc->clcsock, how);
1244 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1245 if (sk->sk_shutdown == SHUTDOWN_MASK)
1246 sk->sk_state = SMC_CLOSED;
1250 case SHUT_RDWR: /* shutdown in both directions */
1251 rc = smc_close_active(smc);
1254 rc = smc_close_shutdown_write(smc);
1257 if (sk->sk_state == SMC_LISTEN)
1258 rc = smc_close_active(smc);
1261 /* nothing more to do because peer is not involved */
1264 rc1 = kernel_sock_shutdown(smc->clcsock, how);
1265 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1266 sk->sk_shutdown |= how + 1;
1270 return rc ? rc : rc1;
1273 static int smc_setsockopt(struct socket *sock, int level, int optname,
1274 char __user *optval, unsigned int optlen)
1276 struct sock *sk = sock->sk;
1277 struct smc_sock *smc;
1281 /* generic setsockopts reaching us here always apply to the
1284 return smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1288 static int smc_getsockopt(struct socket *sock, int level, int optname,
1289 char __user *optval, int __user *optlen)
1291 struct smc_sock *smc;
1293 smc = smc_sk(sock->sk);
1294 /* socket options apply to the CLC socket */
1295 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1299 static int smc_ioctl(struct socket *sock, unsigned int cmd,
1302 struct smc_sock *smc;
1304 smc = smc_sk(sock->sk);
1305 if (smc->use_fallback)
1306 return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1308 return sock_no_ioctl(sock, cmd, arg);
1311 static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1312 int offset, size_t size, int flags)
1314 struct sock *sk = sock->sk;
1315 struct smc_sock *smc;
1320 if (sk->sk_state != SMC_ACTIVE)
1322 if (smc->use_fallback)
1323 rc = kernel_sendpage(smc->clcsock, page, offset,
1326 rc = sock_no_sendpage(sock, page, offset, size, flags);
1333 static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1334 struct pipe_inode_info *pipe, size_t len,
1337 struct sock *sk = sock->sk;
1338 struct smc_sock *smc;
1343 if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED))
1345 if (smc->use_fallback) {
1346 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1356 /* must look like tcp */
1357 static const struct proto_ops smc_sock_ops = {
1359 .owner = THIS_MODULE,
1360 .release = smc_release,
1362 .connect = smc_connect,
1363 .socketpair = sock_no_socketpair,
1364 .accept = smc_accept,
1365 .getname = smc_getname,
1368 .listen = smc_listen,
1369 .shutdown = smc_shutdown,
1370 .setsockopt = smc_setsockopt,
1371 .getsockopt = smc_getsockopt,
1372 .sendmsg = smc_sendmsg,
1373 .recvmsg = smc_recvmsg,
1374 .mmap = sock_no_mmap,
1375 .sendpage = smc_sendpage,
1376 .splice_read = smc_splice_read,
1379 static int smc_create(struct net *net, struct socket *sock, int protocol,
1382 struct smc_sock *smc;
1386 rc = -ESOCKTNOSUPPORT;
1387 if (sock->type != SOCK_STREAM)
1390 rc = -EPROTONOSUPPORT;
1391 if ((protocol != IPPROTO_IP) && (protocol != IPPROTO_TCP))
1395 sock->ops = &smc_sock_ops;
1396 sk = smc_sock_alloc(net, sock);
1400 /* create internal TCP socket for CLC handshake and fallback */
1402 smc->use_fallback = false; /* assume rdma capability first */
1403 rc = sock_create_kern(net, PF_INET, SOCK_STREAM,
1404 IPPROTO_TCP, &smc->clcsock);
1406 sk_common_release(sk);
1409 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1410 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
1416 static const struct net_proto_family smc_sock_family_ops = {
1418 .owner = THIS_MODULE,
1419 .create = smc_create,
1422 static int __init smc_init(void)
1426 rc = smc_pnet_init();
1430 rc = smc_llc_init();
1432 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1436 rc = smc_cdc_init();
1438 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1442 rc = proto_register(&smc_proto, 1);
1444 pr_err("%s: proto_register fails with %d\n", __func__, rc);
1448 rc = sock_register(&smc_sock_family_ops);
1450 pr_err("%s: sock_register fails with %d\n", __func__, rc);
1453 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
1455 rc = smc_ib_register_client();
1457 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1461 static_branch_enable(&tcp_have_smc);
1465 sock_unregister(PF_SMC);
1467 proto_unregister(&smc_proto);
1473 static void __exit smc_exit(void)
1475 struct smc_link_group *lgr, *lg;
1476 LIST_HEAD(lgr_freeing_list);
1478 spin_lock_bh(&smc_lgr_list.lock);
1479 if (!list_empty(&smc_lgr_list.list))
1480 list_splice_init(&smc_lgr_list.list, &lgr_freeing_list);
1481 spin_unlock_bh(&smc_lgr_list.lock);
1482 list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) {
1483 list_del_init(&lgr->list);
1484 smc_lgr_free(lgr); /* free link group */
1486 static_branch_disable(&tcp_have_smc);
1487 smc_ib_unregister_client();
1488 sock_unregister(PF_SMC);
1489 proto_unregister(&smc_proto);
1493 module_init(smc_init);
1494 module_exit(smc_exit);
1496 MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
1497 MODULE_DESCRIPTION("smc socket address family");
1498 MODULE_LICENSE("GPL");
1499 MODULE_ALIAS_NETPROTO(PF_SMC);