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
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_DESTROY_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;
119 if (sk->sk_state == SMC_LISTEN)
120 /* smc_close_non_accepted() is called and acquires
121 * sock lock for child sockets again
123 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
127 if (smc->use_fallback) {
128 sk->sk_state = SMC_CLOSED;
129 sk->sk_state_change(sk);
131 rc = smc_close_active(smc);
132 sock_set_flag(sk, SOCK_DEAD);
133 sk->sk_shutdown |= SHUTDOWN_MASK;
136 sock_release(smc->clcsock);
143 if (smc->use_fallback) {
144 schedule_delayed_work(&smc->sock_put_work, TCP_TIMEWAIT_LEN);
145 } else if (sk->sk_state == SMC_CLOSED) {
146 smc_conn_free(&smc->conn);
147 schedule_delayed_work(&smc->sock_put_work,
148 SMC_CLOSE_SOCK_PUT_DELAY);
150 sk->sk_prot->unhash(sk);
158 static void smc_destruct(struct sock *sk)
160 if (sk->sk_state != SMC_CLOSED)
162 if (!sock_flag(sk, SOCK_DEAD))
165 sk_refcnt_debug_dec(sk);
168 static struct sock *smc_sock_alloc(struct net *net, struct socket *sock)
170 struct smc_sock *smc;
173 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, &smc_proto, 0);
177 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
178 sk->sk_state = SMC_INIT;
179 sk->sk_destruct = smc_destruct;
180 sk->sk_protocol = SMCPROTO_SMC;
182 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
183 INIT_LIST_HEAD(&smc->accept_q);
184 spin_lock_init(&smc->accept_q_lock);
185 INIT_DELAYED_WORK(&smc->sock_put_work, smc_close_sock_put_work);
186 sk->sk_prot->hash(sk);
187 sk_refcnt_debug_inc(sk);
192 static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
195 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
196 struct sock *sk = sock->sk;
197 struct smc_sock *smc;
202 /* replicate tests from inet_bind(), to be safe wrt. future changes */
204 if (addr_len < sizeof(struct sockaddr_in))
208 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
209 if ((addr->sin_family != AF_INET) &&
210 ((addr->sin_family != AF_UNSPEC) ||
211 (addr->sin_addr.s_addr != htonl(INADDR_ANY))))
216 /* Check if socket is already active */
218 if (sk->sk_state != SMC_INIT)
221 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
222 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
230 static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
233 /* options we don't get control via setsockopt for */
234 nsk->sk_type = osk->sk_type;
235 nsk->sk_sndbuf = osk->sk_sndbuf;
236 nsk->sk_rcvbuf = osk->sk_rcvbuf;
237 nsk->sk_sndtimeo = osk->sk_sndtimeo;
238 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
239 nsk->sk_mark = osk->sk_mark;
240 nsk->sk_priority = osk->sk_priority;
241 nsk->sk_rcvlowat = osk->sk_rcvlowat;
242 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
243 nsk->sk_err = osk->sk_err;
245 nsk->sk_flags &= ~mask;
246 nsk->sk_flags |= osk->sk_flags & mask;
249 #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
250 (1UL << SOCK_KEEPOPEN) | \
251 (1UL << SOCK_LINGER) | \
252 (1UL << SOCK_BROADCAST) | \
253 (1UL << SOCK_TIMESTAMP) | \
254 (1UL << SOCK_DBG) | \
255 (1UL << SOCK_RCVTSTAMP) | \
256 (1UL << SOCK_RCVTSTAMPNS) | \
257 (1UL << SOCK_LOCALROUTE) | \
258 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
259 (1UL << SOCK_RXQ_OVFL) | \
260 (1UL << SOCK_WIFI_STATUS) | \
261 (1UL << SOCK_NOFCS) | \
262 (1UL << SOCK_FILTER_LOCKED))
263 /* copy only relevant settings and flags of SOL_SOCKET level from smc to
264 * clc socket (since smc is not called for these options from net/core)
266 static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
268 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
271 #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
272 (1UL << SOCK_KEEPOPEN) | \
273 (1UL << SOCK_LINGER) | \
275 /* copy only settings and flags relevant for smc from clc to smc socket */
276 static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
278 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
281 /* determine subnet and mask of internal TCP socket */
282 int smc_netinfo_by_tcpsk(struct socket *clcsock,
283 __be32 *subnet, u8 *prefix_len)
285 struct dst_entry *dst = sk_dst_get(clcsock->sk);
286 struct sockaddr_in addr;
299 /* get address to which the internal TCP socket is bound */
300 kernel_getsockname(clcsock, (struct sockaddr *)&addr, &len);
301 /* analyze IPv4 specific data of net_device belonging to TCP socket */
302 for_ifa(dst->dev->ip_ptr) {
303 if (ifa->ifa_address != addr.sin_addr.s_addr)
305 *prefix_len = inet_mask_len(ifa->ifa_mask);
306 *subnet = ifa->ifa_address & ifa->ifa_mask;
309 } endfor_ifa(dst->dev->ip_ptr);
317 static int smc_clnt_conf_first_link(struct smc_sock *smc, union ib_gid *gid)
319 struct smc_link_group *lgr = smc->conn.lgr;
320 struct smc_link *link;
324 link = &lgr->lnk[SMC_SINGLE_LINK];
325 /* receive CONFIRM LINK request from server over RoCE fabric */
326 rest = wait_for_completion_interruptible_timeout(
328 SMC_LLC_WAIT_FIRST_TIME);
330 struct smc_clc_msg_decline dclc;
332 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
337 rc = smc_ib_modify_qp_rts(link);
339 return SMC_CLC_DECL_INTERR;
341 smc_wr_remember_qp_attr(link);
342 /* send CONFIRM LINK response over RoCE fabric */
343 rc = smc_llc_send_confirm_link(link,
344 link->smcibdev->mac[link->ibport - 1],
347 return SMC_CLC_DECL_TCL;
352 static void smc_conn_save_peer_info(struct smc_sock *smc,
353 struct smc_clc_msg_accept_confirm *clc)
355 smc->conn.peer_conn_idx = clc->conn_idx;
356 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
357 smc->conn.peer_rmbe_size = smc_uncompress_bufsize(clc->rmbe_size);
358 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
361 static void smc_link_save_peer_info(struct smc_link *link,
362 struct smc_clc_msg_accept_confirm *clc)
364 link->peer_qpn = ntoh24(clc->qpn);
365 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
366 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
367 link->peer_psn = ntoh24(clc->psn);
368 link->peer_mtu = clc->qp_mtu;
371 /* setup for RDMA connection of client */
372 static int smc_connect_rdma(struct smc_sock *smc)
374 struct sockaddr_in *inaddr = (struct sockaddr_in *)smc->addr;
375 struct smc_clc_msg_accept_confirm aclc;
376 int local_contact = SMC_FIRST_CONTACT;
377 struct smc_ib_device *smcibdev;
378 struct smc_link *link;
379 u8 srv_first_contact;
384 /* IPSec connections opt out of SMC-R optimizations */
385 if (using_ipsec(smc)) {
386 reason_code = SMC_CLC_DECL_IPSEC;
390 /* PNET table look up: search active ib_device and port
391 * within same PNETID that also contains the ethernet device
392 * used for the internal TCP socket
394 smc_pnet_find_roce_resource(smc->clcsock->sk, &smcibdev, &ibport);
396 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
400 /* do inband token exchange */
401 reason_code = smc_clc_send_proposal(smc, smcibdev, ibport);
402 if (reason_code < 0) {
406 if (reason_code > 0) /* configuration error */
408 /* receive SMC Accept CLC message */
409 reason_code = smc_clc_wait_msg(smc, &aclc, sizeof(aclc),
411 if (reason_code < 0) {
418 srv_first_contact = aclc.hdr.flag;
419 mutex_lock(&smc_create_lgr_pending);
420 local_contact = smc_conn_create(smc, inaddr->sin_addr.s_addr, smcibdev,
421 ibport, &aclc.lcl, srv_first_contact);
422 if (local_contact < 0) {
425 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
426 else if (rc == -ENOLINK)
427 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
428 goto decline_rdma_unlock;
430 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
432 smc_conn_save_peer_info(smc, &aclc);
434 rc = smc_sndbuf_create(smc);
436 reason_code = SMC_CLC_DECL_MEM;
437 goto decline_rdma_unlock;
439 rc = smc_rmb_create(smc);
441 reason_code = SMC_CLC_DECL_MEM;
442 goto decline_rdma_unlock;
445 if (local_contact == SMC_FIRST_CONTACT)
446 smc_link_save_peer_info(link, &aclc);
448 rc = smc_rmb_rtoken_handling(&smc->conn, &aclc);
450 reason_code = SMC_CLC_DECL_INTERR;
451 goto decline_rdma_unlock;
454 if (local_contact == SMC_FIRST_CONTACT) {
455 rc = smc_ib_ready_link(link);
457 reason_code = SMC_CLC_DECL_INTERR;
458 goto decline_rdma_unlock;
462 rc = smc_clc_send_confirm(smc);
466 if (local_contact == SMC_FIRST_CONTACT) {
467 /* QP confirmation over RoCE fabric */
468 reason_code = smc_clnt_conf_first_link(
469 smc, &smcibdev->gid[ibport - 1]);
470 if (reason_code < 0) {
475 goto decline_rdma_unlock;
478 mutex_unlock(&smc_create_lgr_pending);
483 smc_copy_sock_settings_to_clc(smc);
484 if (smc->sk.sk_state == SMC_INIT)
485 smc->sk.sk_state = SMC_ACTIVE;
487 return rc ? rc : local_contact;
490 mutex_unlock(&smc_create_lgr_pending);
491 smc_conn_free(&smc->conn);
493 /* RDMA setup failed, switch back to TCP */
494 smc->use_fallback = true;
495 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
496 rc = smc_clc_send_decline(smc, reason_code, 0);
497 if (rc < sizeof(struct smc_clc_msg_decline))
503 mutex_unlock(&smc_create_lgr_pending);
504 smc_conn_free(&smc->conn);
509 static int smc_connect(struct socket *sock, struct sockaddr *addr,
512 struct sock *sk = sock->sk;
513 struct smc_sock *smc;
518 /* separate smc parameter checking to be safe */
519 if (alen < sizeof(addr->sa_family))
521 if (addr->sa_family != AF_INET)
523 smc->addr = addr; /* needed for nonblocking connect */
526 switch (sk->sk_state) {
537 smc_copy_sock_settings_to_clc(smc);
538 rc = kernel_connect(smc->clcsock, addr, alen, flags);
542 /* setup RDMA connection */
543 rc = smc_connect_rdma(smc);
547 rc = 0; /* success cases including fallback */
555 static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
557 struct sock *sk = &lsmc->sk;
558 struct socket *new_clcsock;
562 release_sock(&lsmc->sk);
563 new_sk = smc_sock_alloc(sock_net(sk), NULL);
566 lsmc->sk.sk_err = ENOMEM;
568 lock_sock(&lsmc->sk);
571 *new_smc = smc_sk(new_sk);
573 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
574 lock_sock(&lsmc->sk);
576 lsmc->sk.sk_err = -rc;
577 new_sk->sk_state = SMC_CLOSED;
578 sock_set_flag(new_sk, SOCK_DEAD);
579 sk->sk_prot->unhash(new_sk);
584 if (lsmc->sk.sk_state == SMC_CLOSED) {
586 sock_release(new_clcsock);
587 new_sk->sk_state = SMC_CLOSED;
588 sock_set_flag(new_sk, SOCK_DEAD);
589 sk->sk_prot->unhash(new_sk);
595 (*new_smc)->clcsock = new_clcsock;
600 /* add a just created sock to the accept queue of the listen sock as
601 * candidate for a following socket accept call from user space
603 static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
605 struct smc_sock *par = smc_sk(parent);
608 spin_lock(&par->accept_q_lock);
609 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
610 spin_unlock(&par->accept_q_lock);
611 sk_acceptq_added(parent);
614 /* remove a socket from the accept queue of its parental listening socket */
615 static void smc_accept_unlink(struct sock *sk)
617 struct smc_sock *par = smc_sk(sk)->listen_smc;
619 spin_lock(&par->accept_q_lock);
620 list_del_init(&smc_sk(sk)->accept_q);
621 spin_unlock(&par->accept_q_lock);
622 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
626 /* remove a sock from the accept queue to bind it to a new socket created
627 * for a socket accept call from user space
629 struct sock *smc_accept_dequeue(struct sock *parent,
630 struct socket *new_sock)
632 struct smc_sock *isk, *n;
635 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
636 new_sk = (struct sock *)isk;
638 smc_accept_unlink(new_sk);
639 if (new_sk->sk_state == SMC_CLOSED) {
640 /* tbd in follow-on patch: close this sock */
644 sock_graft(new_sk, new_sock);
650 /* clean up for a created but never accepted sock */
651 void smc_close_non_accepted(struct sock *sk)
653 struct smc_sock *smc = smc_sk(sk);
657 if (!sk->sk_lingertime)
658 /* wait for peer closing */
659 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
660 if (!smc->use_fallback)
661 smc_close_active(smc);
669 sock_set_flag(sk, SOCK_DEAD);
670 sk->sk_shutdown |= SHUTDOWN_MASK;
671 if (smc->use_fallback) {
672 schedule_delayed_work(&smc->sock_put_work, TCP_TIMEWAIT_LEN);
674 smc_conn_free(&smc->conn);
675 schedule_delayed_work(&smc->sock_put_work,
676 SMC_CLOSE_SOCK_PUT_DELAY);
682 static int smc_serv_conf_first_link(struct smc_sock *smc)
684 struct smc_link_group *lgr = smc->conn.lgr;
685 struct smc_link *link;
689 link = &lgr->lnk[SMC_SINGLE_LINK];
690 /* send CONFIRM LINK request to client over the RoCE fabric */
691 rc = smc_llc_send_confirm_link(link,
692 link->smcibdev->mac[link->ibport - 1],
693 &link->smcibdev->gid[link->ibport - 1],
696 return SMC_CLC_DECL_TCL;
698 /* receive CONFIRM LINK response from client over the RoCE fabric */
699 rest = wait_for_completion_interruptible_timeout(
700 &link->llc_confirm_resp,
701 SMC_LLC_WAIT_FIRST_TIME);
703 struct smc_clc_msg_decline dclc;
705 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
712 /* setup for RDMA connection of server */
713 static void smc_listen_work(struct work_struct *work)
715 struct smc_sock *new_smc = container_of(work, struct smc_sock,
717 struct socket *newclcsock = new_smc->clcsock;
718 struct smc_sock *lsmc = new_smc->listen_smc;
719 struct smc_clc_msg_accept_confirm cclc;
720 int local_contact = SMC_REUSE_CONTACT;
721 struct sock *newsmcsk = &new_smc->sk;
722 struct smc_clc_msg_proposal pclc;
723 struct smc_ib_device *smcibdev;
724 struct sockaddr_in peeraddr;
725 struct smc_link *link;
732 /* do inband token exchange -
733 *wait for and receive SMC Proposal CLC message
735 reason_code = smc_clc_wait_msg(new_smc, &pclc, sizeof(pclc),
742 /* IPSec connections opt out of SMC-R optimizations */
743 if (using_ipsec(new_smc)) {
744 reason_code = SMC_CLC_DECL_IPSEC;
748 /* PNET table look up: search active ib_device and port
749 * within same PNETID that also contains the ethernet device
750 * used for the internal TCP socket
752 smc_pnet_find_roce_resource(newclcsock->sk, &smcibdev, &ibport);
754 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
758 /* determine subnet and mask from internal TCP socket */
759 rc = smc_netinfo_by_tcpsk(newclcsock, &subnet, &prefix_len);
761 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
764 if ((pclc.outgoing_subnet != subnet) ||
765 (pclc.prefix_len != prefix_len)) {
766 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
770 /* get address of the peer connected to the internal TCP socket */
771 kernel_getpeername(newclcsock, (struct sockaddr *)&peeraddr, &len);
773 /* allocate connection / link group */
774 mutex_lock(&smc_create_lgr_pending);
775 local_contact = smc_conn_create(new_smc, peeraddr.sin_addr.s_addr,
776 smcibdev, ibport, &pclc.lcl, 0);
777 if (local_contact == SMC_REUSE_CONTACT)
778 /* lock no longer needed, free it due to following
779 * smc_clc_wait_msg() call
781 mutex_unlock(&smc_create_lgr_pending);
782 if (local_contact < 0) {
785 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
786 else if (rc == -ENOLINK)
787 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
790 link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
792 rc = smc_sndbuf_create(new_smc);
794 reason_code = SMC_CLC_DECL_MEM;
797 rc = smc_rmb_create(new_smc);
799 reason_code = SMC_CLC_DECL_MEM;
803 rc = smc_clc_send_accept(new_smc, local_contact);
807 /* receive SMC Confirm CLC message */
808 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
814 smc_conn_save_peer_info(new_smc, &cclc);
815 if (local_contact == SMC_FIRST_CONTACT)
816 smc_link_save_peer_info(link, &cclc);
818 rc = smc_rmb_rtoken_handling(&new_smc->conn, &cclc);
820 reason_code = SMC_CLC_DECL_INTERR;
824 if (local_contact == SMC_FIRST_CONTACT) {
825 rc = smc_ib_ready_link(link);
827 reason_code = SMC_CLC_DECL_INTERR;
830 /* QP confirmation over RoCE fabric */
831 reason_code = smc_serv_conf_first_link(new_smc);
832 if (reason_code < 0) {
833 /* peer is not aware of a problem */
841 smc_tx_init(new_smc);
842 smc_rx_init(new_smc);
845 sk_refcnt_debug_inc(newsmcsk);
846 if (newsmcsk->sk_state == SMC_INIT)
847 newsmcsk->sk_state = SMC_ACTIVE;
849 if (local_contact == SMC_FIRST_CONTACT)
850 mutex_unlock(&smc_create_lgr_pending);
851 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
852 if (lsmc->sk.sk_state == SMC_LISTEN) {
853 smc_accept_enqueue(&lsmc->sk, newsmcsk);
854 } else { /* no longer listening */
855 smc_close_non_accepted(newsmcsk);
857 release_sock(&lsmc->sk);
860 lsmc->sk.sk_data_ready(&lsmc->sk);
861 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
865 /* RDMA setup failed, switch back to TCP */
866 smc_conn_free(&new_smc->conn);
867 new_smc->use_fallback = true;
868 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
869 rc = smc_clc_send_decline(new_smc, reason_code, 0);
870 if (rc < sizeof(struct smc_clc_msg_decline))
876 newsmcsk->sk_state = SMC_CLOSED;
877 smc_conn_free(&new_smc->conn);
878 goto enqueue; /* queue new sock with sk_err set */
881 static void smc_tcp_listen_work(struct work_struct *work)
883 struct smc_sock *lsmc = container_of(work, struct smc_sock,
885 struct smc_sock *new_smc;
888 lock_sock(&lsmc->sk);
889 while (lsmc->sk.sk_state == SMC_LISTEN) {
890 rc = smc_clcsock_accept(lsmc, &new_smc);
896 new_smc->listen_smc = lsmc;
897 new_smc->use_fallback = false; /* assume rdma capability first*/
898 sock_hold(&lsmc->sk); /* sock_put in smc_listen_work */
899 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
900 smc_copy_sock_settings_to_smc(new_smc);
901 schedule_work(&new_smc->smc_listen_work);
905 release_sock(&lsmc->sk);
906 lsmc->sk.sk_data_ready(&lsmc->sk); /* no more listening, wake accept */
909 static int smc_listen(struct socket *sock, int backlog)
911 struct sock *sk = sock->sk;
912 struct smc_sock *smc;
919 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
923 if (sk->sk_state == SMC_LISTEN) {
924 sk->sk_max_ack_backlog = backlog;
927 /* some socket options are handled in core, so we could not apply
928 * them to the clc socket -- copy smc socket options to clc socket
930 smc_copy_sock_settings_to_clc(smc);
932 rc = kernel_listen(smc->clcsock, backlog);
935 sk->sk_max_ack_backlog = backlog;
936 sk->sk_ack_backlog = 0;
937 sk->sk_state = SMC_LISTEN;
938 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
939 schedule_work(&smc->tcp_listen_work);
946 static int smc_accept(struct socket *sock, struct socket *new_sock,
949 struct sock *sk = sock->sk, *nsk;
950 DECLARE_WAITQUEUE(wait, current);
951 struct smc_sock *lsmc;
958 if (lsmc->sk.sk_state != SMC_LISTEN) {
963 /* Wait for an incoming connection */
964 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
965 add_wait_queue_exclusive(sk_sleep(sk), &wait);
966 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
967 set_current_state(TASK_INTERRUPTIBLE);
973 timeo = schedule_timeout(timeo);
974 /* wakeup by sk_data_ready in smc_listen_work() */
975 sched_annotate_sleep();
977 if (signal_pending(current)) {
978 rc = sock_intr_errno(timeo);
982 set_current_state(TASK_RUNNING);
983 remove_wait_queue(sk_sleep(sk), &wait);
986 rc = sock_error(nsk);
993 static int smc_getname(struct socket *sock, struct sockaddr *addr,
996 struct smc_sock *smc;
998 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
999 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
1002 smc = smc_sk(sock->sk);
1004 return smc->clcsock->ops->getname(smc->clcsock, addr, len, peer);
1007 static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1009 struct sock *sk = sock->sk;
1010 struct smc_sock *smc;
1015 if ((sk->sk_state != SMC_ACTIVE) &&
1016 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1017 (sk->sk_state != SMC_INIT))
1019 if (smc->use_fallback)
1020 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1022 rc = smc_tx_sendmsg(smc, msg, len);
1028 static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1031 struct sock *sk = sock->sk;
1032 struct smc_sock *smc;
1037 if ((sk->sk_state == SMC_INIT) ||
1038 (sk->sk_state == SMC_LISTEN) ||
1039 (sk->sk_state == SMC_CLOSED))
1042 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1047 if (smc->use_fallback)
1048 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
1050 rc = smc_rx_recvmsg(smc, msg, len, flags);
1057 static unsigned int smc_accept_poll(struct sock *parent)
1059 struct smc_sock *isk;
1063 list_for_each_entry(isk, &smc_sk(parent)->accept_q, accept_q) {
1064 sk = (struct sock *)isk;
1066 if (sk->sk_state == SMC_ACTIVE) {
1067 release_sock(parent);
1068 return POLLIN | POLLRDNORM;
1071 release_sock(parent);
1076 static unsigned int smc_poll(struct file *file, struct socket *sock,
1079 struct sock *sk = sock->sk;
1080 unsigned int mask = 0;
1081 struct smc_sock *smc;
1084 smc = smc_sk(sock->sk);
1085 if ((sk->sk_state == SMC_INIT) || smc->use_fallback) {
1086 /* delegate to CLC child sock */
1087 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
1088 /* if non-blocking connect finished ... */
1090 if ((sk->sk_state == SMC_INIT) && (mask & POLLOUT)) {
1091 sk->sk_err = smc->clcsock->sk->sk_err;
1095 rc = smc_connect_rdma(smc);
1099 /* success cases including fallback */
1100 mask |= POLLOUT | POLLWRNORM;
1105 sock_poll_wait(file, sk_sleep(sk), wait);
1106 if (sk->sk_state == SMC_LISTEN)
1107 /* woken up by sk_data_ready in smc_listen_work() */
1108 mask |= smc_accept_poll(sk);
1111 if (atomic_read(&smc->conn.sndbuf_space) ||
1112 (sk->sk_shutdown & SEND_SHUTDOWN)) {
1113 mask |= POLLOUT | POLLWRNORM;
1115 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1116 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1118 if (atomic_read(&smc->conn.bytes_to_rcv))
1119 mask |= POLLIN | POLLRDNORM;
1120 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1121 (sk->sk_state == SMC_CLOSED))
1123 if (sk->sk_shutdown & RCV_SHUTDOWN)
1124 mask |= POLLIN | POLLRDNORM | POLLRDHUP;
1125 if (sk->sk_state == SMC_APPCLOSEWAIT1)
1133 static int smc_shutdown(struct socket *sock, int how)
1135 struct sock *sk = sock->sk;
1136 struct smc_sock *smc;
1142 if ((how < SHUT_RD) || (how > SHUT_RDWR))
1148 if ((sk->sk_state != SMC_LISTEN) &&
1149 (sk->sk_state != SMC_ACTIVE) &&
1150 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1151 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1152 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1153 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1154 (sk->sk_state != SMC_APPFINCLOSEWAIT))
1156 if (smc->use_fallback) {
1157 rc = kernel_sock_shutdown(smc->clcsock, how);
1158 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1159 if (sk->sk_shutdown == SHUTDOWN_MASK)
1160 sk->sk_state = SMC_CLOSED;
1164 case SHUT_RDWR: /* shutdown in both directions */
1165 rc = smc_close_active(smc);
1168 rc = smc_close_shutdown_write(smc);
1171 if (sk->sk_state == SMC_LISTEN)
1172 rc = smc_close_active(smc);
1175 /* nothing more to do because peer is not involved */
1178 rc1 = kernel_sock_shutdown(smc->clcsock, how);
1179 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1180 sk->sk_shutdown |= how + 1;
1184 return rc ? rc : rc1;
1187 static int smc_setsockopt(struct socket *sock, int level, int optname,
1188 char __user *optval, unsigned int optlen)
1190 struct sock *sk = sock->sk;
1191 struct smc_sock *smc;
1195 /* generic setsockopts reaching us here always apply to the
1198 return smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1202 static int smc_getsockopt(struct socket *sock, int level, int optname,
1203 char __user *optval, int __user *optlen)
1205 struct smc_sock *smc;
1207 smc = smc_sk(sock->sk);
1208 /* socket options apply to the CLC socket */
1209 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1213 static int smc_ioctl(struct socket *sock, unsigned int cmd,
1216 struct smc_sock *smc;
1218 smc = smc_sk(sock->sk);
1219 if (smc->use_fallback)
1220 return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1222 return sock_no_ioctl(sock, cmd, arg);
1225 static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1226 int offset, size_t size, int flags)
1228 struct sock *sk = sock->sk;
1229 struct smc_sock *smc;
1234 if (sk->sk_state != SMC_ACTIVE)
1236 if (smc->use_fallback)
1237 rc = kernel_sendpage(smc->clcsock, page, offset,
1240 rc = sock_no_sendpage(sock, page, offset, size, flags);
1247 static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1248 struct pipe_inode_info *pipe, size_t len,
1251 struct sock *sk = sock->sk;
1252 struct smc_sock *smc;
1257 if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED))
1259 if (smc->use_fallback) {
1260 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1270 /* must look like tcp */
1271 static const struct proto_ops smc_sock_ops = {
1273 .owner = THIS_MODULE,
1274 .release = smc_release,
1276 .connect = smc_connect,
1277 .socketpair = sock_no_socketpair,
1278 .accept = smc_accept,
1279 .getname = smc_getname,
1282 .listen = smc_listen,
1283 .shutdown = smc_shutdown,
1284 .setsockopt = smc_setsockopt,
1285 .getsockopt = smc_getsockopt,
1286 .sendmsg = smc_sendmsg,
1287 .recvmsg = smc_recvmsg,
1288 .mmap = sock_no_mmap,
1289 .sendpage = smc_sendpage,
1290 .splice_read = smc_splice_read,
1293 static int smc_create(struct net *net, struct socket *sock, int protocol,
1296 struct smc_sock *smc;
1300 rc = -ESOCKTNOSUPPORT;
1301 if (sock->type != SOCK_STREAM)
1304 rc = -EPROTONOSUPPORT;
1305 if ((protocol != IPPROTO_IP) && (protocol != IPPROTO_TCP))
1309 sock->ops = &smc_sock_ops;
1310 sk = smc_sock_alloc(net, sock);
1314 /* create internal TCP socket for CLC handshake and fallback */
1316 smc->use_fallback = false; /* assume rdma capability first */
1317 rc = sock_create_kern(net, PF_INET, SOCK_STREAM,
1318 IPPROTO_TCP, &smc->clcsock);
1320 sk_common_release(sk);
1321 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1322 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
1328 static const struct net_proto_family smc_sock_family_ops = {
1330 .owner = THIS_MODULE,
1331 .create = smc_create,
1334 static int __init smc_init(void)
1338 rc = smc_pnet_init();
1342 rc = smc_llc_init();
1344 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1348 rc = smc_cdc_init();
1350 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1354 rc = proto_register(&smc_proto, 1);
1356 pr_err("%s: proto_register fails with %d\n", __func__, rc);
1360 rc = sock_register(&smc_sock_family_ops);
1362 pr_err("%s: sock_register fails with %d\n", __func__, rc);
1365 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
1367 rc = smc_ib_register_client();
1369 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1376 sock_unregister(PF_SMC);
1378 proto_unregister(&smc_proto);
1384 static void __exit smc_exit(void)
1386 struct smc_link_group *lgr, *lg;
1387 LIST_HEAD(lgr_freeing_list);
1389 spin_lock_bh(&smc_lgr_list.lock);
1390 if (!list_empty(&smc_lgr_list.list))
1391 list_splice_init(&smc_lgr_list.list, &lgr_freeing_list);
1392 spin_unlock_bh(&smc_lgr_list.lock);
1393 list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) {
1394 list_del_init(&lgr->list);
1395 smc_lgr_free(lgr); /* free link group */
1397 smc_ib_unregister_client();
1398 sock_unregister(PF_SMC);
1399 proto_unregister(&smc_proto);
1403 module_init(smc_init);
1404 module_exit(smc_exit);
1407 MODULE_DESCRIPTION("smc socket address family");
1408 MODULE_LICENSE("GPL");
1409 MODULE_ALIAS_NETPROTO(PF_SMC);