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>
42 static DEFINE_MUTEX(smc_create_lgr_pending); /* serialize link group
46 struct smc_lgr_list smc_lgr_list = { /* established link groups */
47 .lock = __SPIN_LOCK_UNLOCKED(smc_lgr_list.lock),
48 .list = LIST_HEAD_INIT(smc_lgr_list.list),
51 static void smc_tcp_listen_work(struct work_struct *);
53 static void smc_set_keepalive(struct sock *sk, int val)
55 struct smc_sock *smc = smc_sk(sk);
57 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
60 static struct proto smc_proto = {
63 .keepalive = smc_set_keepalive,
64 .obj_size = sizeof(struct smc_sock),
65 .slab_flags = SLAB_DESTROY_BY_RCU,
68 static int smc_release(struct socket *sock)
70 struct sock *sk = sock->sk;
79 sk->sk_state = SMC_CLOSED;
81 sock_release(smc->clcsock);
95 static void smc_destruct(struct sock *sk)
97 if (sk->sk_state != SMC_CLOSED)
99 if (!sock_flag(sk, SOCK_DEAD))
102 sk_refcnt_debug_dec(sk);
105 static struct sock *smc_sock_alloc(struct net *net, struct socket *sock)
107 struct smc_sock *smc;
110 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, &smc_proto, 0);
114 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
115 sk->sk_state = SMC_INIT;
116 sk->sk_destruct = smc_destruct;
117 sk->sk_protocol = SMCPROTO_SMC;
119 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
120 INIT_LIST_HEAD(&smc->accept_q);
121 spin_lock_init(&smc->accept_q_lock);
122 sk_refcnt_debug_inc(sk);
127 static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
130 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
131 struct sock *sk = sock->sk;
132 struct smc_sock *smc;
137 /* replicate tests from inet_bind(), to be safe wrt. future changes */
139 if (addr_len < sizeof(struct sockaddr_in))
143 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
144 if ((addr->sin_family != AF_INET) &&
145 ((addr->sin_family != AF_UNSPEC) ||
146 (addr->sin_addr.s_addr != htonl(INADDR_ANY))))
151 /* Check if socket is already active */
153 if (sk->sk_state != SMC_INIT)
156 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
157 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
165 static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
168 /* options we don't get control via setsockopt for */
169 nsk->sk_type = osk->sk_type;
170 nsk->sk_sndbuf = osk->sk_sndbuf;
171 nsk->sk_rcvbuf = osk->sk_rcvbuf;
172 nsk->sk_sndtimeo = osk->sk_sndtimeo;
173 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
174 nsk->sk_mark = osk->sk_mark;
175 nsk->sk_priority = osk->sk_priority;
176 nsk->sk_rcvlowat = osk->sk_rcvlowat;
177 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
178 nsk->sk_err = osk->sk_err;
180 nsk->sk_flags &= ~mask;
181 nsk->sk_flags |= osk->sk_flags & mask;
184 #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
185 (1UL << SOCK_KEEPOPEN) | \
186 (1UL << SOCK_LINGER) | \
187 (1UL << SOCK_BROADCAST) | \
188 (1UL << SOCK_TIMESTAMP) | \
189 (1UL << SOCK_DBG) | \
190 (1UL << SOCK_RCVTSTAMP) | \
191 (1UL << SOCK_RCVTSTAMPNS) | \
192 (1UL << SOCK_LOCALROUTE) | \
193 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
194 (1UL << SOCK_RXQ_OVFL) | \
195 (1UL << SOCK_WIFI_STATUS) | \
196 (1UL << SOCK_NOFCS) | \
197 (1UL << SOCK_FILTER_LOCKED))
198 /* copy only relevant settings and flags of SOL_SOCKET level from smc to
199 * clc socket (since smc is not called for these options from net/core)
201 static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
203 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
206 #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
207 (1UL << SOCK_KEEPOPEN) | \
208 (1UL << SOCK_LINGER) | \
210 /* copy only settings and flags relevant for smc from clc to smc socket */
211 static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
213 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
216 /* determine subnet and mask of internal TCP socket */
217 int smc_netinfo_by_tcpsk(struct socket *clcsock,
218 __be32 *subnet, u8 *prefix_len)
220 struct dst_entry *dst = sk_dst_get(clcsock->sk);
221 struct sockaddr_in addr;
234 /* get address to which the internal TCP socket is bound */
235 kernel_getsockname(clcsock, (struct sockaddr *)&addr, &len);
236 /* analyze IPv4 specific data of net_device belonging to TCP socket */
237 for_ifa(dst->dev->ip_ptr) {
238 if (ifa->ifa_address != addr.sin_addr.s_addr)
240 *prefix_len = inet_mask_len(ifa->ifa_mask);
241 *subnet = ifa->ifa_address & ifa->ifa_mask;
244 } endfor_ifa(dst->dev->ip_ptr);
252 static int smc_clnt_conf_first_link(struct smc_sock *smc, union ib_gid *gid)
254 struct smc_link_group *lgr = smc->conn.lgr;
255 struct smc_link *link;
259 link = &lgr->lnk[SMC_SINGLE_LINK];
260 /* receive CONFIRM LINK request from server over RoCE fabric */
261 rest = wait_for_completion_interruptible_timeout(
263 SMC_LLC_WAIT_FIRST_TIME);
265 struct smc_clc_msg_decline dclc;
267 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
272 rc = smc_ib_modify_qp_rts(link);
274 return SMC_CLC_DECL_INTERR;
276 smc_wr_remember_qp_attr(link);
277 /* send CONFIRM LINK response over RoCE fabric */
278 rc = smc_llc_send_confirm_link(link,
279 link->smcibdev->mac[link->ibport - 1],
282 return SMC_CLC_DECL_TCL;
287 static void smc_conn_save_peer_info(struct smc_sock *smc,
288 struct smc_clc_msg_accept_confirm *clc)
290 smc->conn.peer_conn_idx = clc->conn_idx;
291 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
292 smc->conn.peer_rmbe_size = smc_uncompress_bufsize(clc->rmbe_size);
293 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
296 static void smc_link_save_peer_info(struct smc_link *link,
297 struct smc_clc_msg_accept_confirm *clc)
299 link->peer_qpn = ntoh24(clc->qpn);
300 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
301 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
302 link->peer_psn = ntoh24(clc->psn);
303 link->peer_mtu = clc->qp_mtu;
306 /* setup for RDMA connection of client */
307 static int smc_connect_rdma(struct smc_sock *smc)
309 struct sockaddr_in *inaddr = (struct sockaddr_in *)smc->addr;
310 struct smc_clc_msg_accept_confirm aclc;
311 int local_contact = SMC_FIRST_CONTACT;
312 struct smc_ib_device *smcibdev;
313 struct smc_link *link;
314 u8 srv_first_contact;
319 /* IPSec connections opt out of SMC-R optimizations */
320 if (using_ipsec(smc)) {
321 reason_code = SMC_CLC_DECL_IPSEC;
325 /* PNET table look up: search active ib_device and port
326 * within same PNETID that also contains the ethernet device
327 * used for the internal TCP socket
329 smc_pnet_find_roce_resource(smc->clcsock->sk, &smcibdev, &ibport);
331 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
335 /* do inband token exchange */
336 reason_code = smc_clc_send_proposal(smc, smcibdev, ibport);
337 if (reason_code < 0) {
341 if (reason_code > 0) /* configuration error */
343 /* receive SMC Accept CLC message */
344 reason_code = smc_clc_wait_msg(smc, &aclc, sizeof(aclc),
346 if (reason_code < 0) {
353 srv_first_contact = aclc.hdr.flag;
354 mutex_lock(&smc_create_lgr_pending);
355 local_contact = smc_conn_create(smc, inaddr->sin_addr.s_addr, smcibdev,
356 ibport, &aclc.lcl, srv_first_contact);
357 if (local_contact < 0) {
360 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
361 else if (rc == -ENOLINK)
362 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
363 goto decline_rdma_unlock;
365 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
367 smc_conn_save_peer_info(smc, &aclc);
369 rc = smc_sndbuf_create(smc);
371 reason_code = SMC_CLC_DECL_MEM;
372 goto decline_rdma_unlock;
374 rc = smc_rmb_create(smc);
376 reason_code = SMC_CLC_DECL_MEM;
377 goto decline_rdma_unlock;
380 if (local_contact == SMC_FIRST_CONTACT)
381 smc_link_save_peer_info(link, &aclc);
383 rc = smc_rmb_rtoken_handling(&smc->conn, &aclc);
385 reason_code = SMC_CLC_DECL_INTERR;
386 goto decline_rdma_unlock;
389 if (local_contact == SMC_FIRST_CONTACT) {
390 rc = smc_ib_ready_link(link);
392 reason_code = SMC_CLC_DECL_INTERR;
393 goto decline_rdma_unlock;
397 rc = smc_clc_send_confirm(smc);
401 if (local_contact == SMC_FIRST_CONTACT) {
402 /* QP confirmation over RoCE fabric */
403 reason_code = smc_clnt_conf_first_link(
404 smc, &smcibdev->gid[ibport - 1]);
405 if (reason_code < 0) {
410 goto decline_rdma_unlock;
413 mutex_unlock(&smc_create_lgr_pending);
417 smc_copy_sock_settings_to_clc(smc);
418 smc->sk.sk_state = SMC_ACTIVE;
420 return rc ? rc : local_contact;
423 mutex_unlock(&smc_create_lgr_pending);
424 smc_conn_free(&smc->conn);
426 /* RDMA setup failed, switch back to TCP */
427 smc->use_fallback = true;
428 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
429 rc = smc_clc_send_decline(smc, reason_code, 0);
430 if (rc < sizeof(struct smc_clc_msg_decline))
436 mutex_unlock(&smc_create_lgr_pending);
437 smc_conn_free(&smc->conn);
442 static int smc_connect(struct socket *sock, struct sockaddr *addr,
445 struct sock *sk = sock->sk;
446 struct smc_sock *smc;
451 /* separate smc parameter checking to be safe */
452 if (alen < sizeof(addr->sa_family))
454 if (addr->sa_family != AF_INET)
456 smc->addr = addr; /* needed for nonblocking connect */
459 switch (sk->sk_state) {
470 smc_copy_sock_settings_to_clc(smc);
471 rc = kernel_connect(smc->clcsock, addr, alen, flags);
475 /* setup RDMA connection */
476 rc = smc_connect_rdma(smc);
480 rc = 0; /* success cases including fallback */
488 static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
490 struct sock *sk = &lsmc->sk;
491 struct socket *new_clcsock;
495 release_sock(&lsmc->sk);
496 new_sk = smc_sock_alloc(sock_net(sk), NULL);
499 lsmc->sk.sk_err = ENOMEM;
501 lock_sock(&lsmc->sk);
504 *new_smc = smc_sk(new_sk);
506 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
507 lock_sock(&lsmc->sk);
509 lsmc->sk.sk_err = -rc;
510 new_sk->sk_state = SMC_CLOSED;
511 sock_set_flag(new_sk, SOCK_DEAD);
516 if (lsmc->sk.sk_state == SMC_CLOSED) {
518 sock_release(new_clcsock);
519 new_sk->sk_state = SMC_CLOSED;
520 sock_set_flag(new_sk, SOCK_DEAD);
526 (*new_smc)->clcsock = new_clcsock;
531 /* add a just created sock to the accept queue of the listen sock as
532 * candidate for a following socket accept call from user space
534 static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
536 struct smc_sock *par = smc_sk(parent);
539 spin_lock(&par->accept_q_lock);
540 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
541 spin_unlock(&par->accept_q_lock);
542 sk_acceptq_added(parent);
545 /* remove a socket from the accept queue of its parental listening socket */
546 static void smc_accept_unlink(struct sock *sk)
548 struct smc_sock *par = smc_sk(sk)->listen_smc;
550 spin_lock(&par->accept_q_lock);
551 list_del_init(&smc_sk(sk)->accept_q);
552 spin_unlock(&par->accept_q_lock);
553 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
557 /* remove a sock from the accept queue to bind it to a new socket created
558 * for a socket accept call from user space
560 static struct sock *smc_accept_dequeue(struct sock *parent,
561 struct socket *new_sock)
563 struct smc_sock *isk, *n;
566 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
567 new_sk = (struct sock *)isk;
569 smc_accept_unlink(new_sk);
570 if (new_sk->sk_state == SMC_CLOSED) {
571 /* tbd in follow-on patch: close this sock */
575 sock_graft(new_sk, new_sock);
581 /* clean up for a created but never accepted sock */
582 static void smc_close_non_accepted(struct sock *sk)
584 struct smc_sock *smc = smc_sk(sk);
594 /* more closing stuff to be added with socket closing patch */
598 static int smc_serv_conf_first_link(struct smc_sock *smc)
600 struct smc_link_group *lgr = smc->conn.lgr;
601 struct smc_link *link;
605 link = &lgr->lnk[SMC_SINGLE_LINK];
606 /* send CONFIRM LINK request to client over the RoCE fabric */
607 rc = smc_llc_send_confirm_link(link,
608 link->smcibdev->mac[link->ibport - 1],
609 &link->smcibdev->gid[link->ibport - 1],
612 return SMC_CLC_DECL_TCL;
614 /* receive CONFIRM LINK response from client over the RoCE fabric */
615 rest = wait_for_completion_interruptible_timeout(
616 &link->llc_confirm_resp,
617 SMC_LLC_WAIT_FIRST_TIME);
619 struct smc_clc_msg_decline dclc;
621 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
628 /* setup for RDMA connection of server */
629 static void smc_listen_work(struct work_struct *work)
631 struct smc_sock *new_smc = container_of(work, struct smc_sock,
633 struct socket *newclcsock = new_smc->clcsock;
634 struct smc_sock *lsmc = new_smc->listen_smc;
635 struct smc_clc_msg_accept_confirm cclc;
636 int local_contact = SMC_REUSE_CONTACT;
637 struct sock *newsmcsk = &new_smc->sk;
638 struct smc_clc_msg_proposal pclc;
639 struct smc_ib_device *smcibdev;
640 struct sockaddr_in peeraddr;
641 struct smc_link *link;
648 /* do inband token exchange -
649 *wait for and receive SMC Proposal CLC message
651 reason_code = smc_clc_wait_msg(new_smc, &pclc, sizeof(pclc),
658 /* IPSec connections opt out of SMC-R optimizations */
659 if (using_ipsec(new_smc)) {
660 reason_code = SMC_CLC_DECL_IPSEC;
664 /* PNET table look up: search active ib_device and port
665 * within same PNETID that also contains the ethernet device
666 * used for the internal TCP socket
668 smc_pnet_find_roce_resource(newclcsock->sk, &smcibdev, &ibport);
670 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
674 /* determine subnet and mask from internal TCP socket */
675 rc = smc_netinfo_by_tcpsk(newclcsock, &subnet, &prefix_len);
677 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
680 if ((pclc.outgoing_subnet != subnet) ||
681 (pclc.prefix_len != prefix_len)) {
682 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
686 /* get address of the peer connected to the internal TCP socket */
687 kernel_getpeername(newclcsock, (struct sockaddr *)&peeraddr, &len);
689 /* allocate connection / link group */
690 mutex_lock(&smc_create_lgr_pending);
691 local_contact = smc_conn_create(new_smc, peeraddr.sin_addr.s_addr,
692 smcibdev, ibport, &pclc.lcl, 0);
693 if (local_contact == SMC_REUSE_CONTACT)
694 /* lock no longer needed, free it due to following
695 * smc_clc_wait_msg() call
697 mutex_unlock(&smc_create_lgr_pending);
698 if (local_contact < 0) {
701 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
702 else if (rc == -ENOLINK)
703 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
706 link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
708 rc = smc_sndbuf_create(new_smc);
710 reason_code = SMC_CLC_DECL_MEM;
713 rc = smc_rmb_create(new_smc);
715 reason_code = SMC_CLC_DECL_MEM;
719 rc = smc_clc_send_accept(new_smc, local_contact);
723 /* receive SMC Confirm CLC message */
724 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
730 smc_conn_save_peer_info(new_smc, &cclc);
731 if (local_contact == SMC_FIRST_CONTACT)
732 smc_link_save_peer_info(link, &cclc);
734 rc = smc_rmb_rtoken_handling(&new_smc->conn, &cclc);
736 reason_code = SMC_CLC_DECL_INTERR;
740 if (local_contact == SMC_FIRST_CONTACT) {
741 rc = smc_ib_ready_link(link);
743 reason_code = SMC_CLC_DECL_INTERR;
746 /* QP confirmation over RoCE fabric */
747 reason_code = smc_serv_conf_first_link(new_smc);
748 if (reason_code < 0) {
749 /* peer is not aware of a problem */
757 smc_tx_init(new_smc);
760 sk_refcnt_debug_inc(newsmcsk);
761 newsmcsk->sk_state = SMC_ACTIVE;
763 if (local_contact == SMC_FIRST_CONTACT)
764 mutex_unlock(&smc_create_lgr_pending);
765 lock_sock(&lsmc->sk);
766 if (lsmc->sk.sk_state == SMC_LISTEN) {
767 smc_accept_enqueue(&lsmc->sk, newsmcsk);
768 } else { /* no longer listening */
769 smc_close_non_accepted(newsmcsk);
771 release_sock(&lsmc->sk);
774 lsmc->sk.sk_data_ready(&lsmc->sk);
775 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
779 /* RDMA setup failed, switch back to TCP */
780 smc_conn_free(&new_smc->conn);
781 new_smc->use_fallback = true;
782 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
783 rc = smc_clc_send_decline(new_smc, reason_code, 0);
784 if (rc < sizeof(struct smc_clc_msg_decline))
790 newsmcsk->sk_state = SMC_CLOSED;
791 goto enqueue; /* queue new sock with sk_err set */
794 static void smc_tcp_listen_work(struct work_struct *work)
796 struct smc_sock *lsmc = container_of(work, struct smc_sock,
798 struct smc_sock *new_smc;
801 lock_sock(&lsmc->sk);
802 while (lsmc->sk.sk_state == SMC_LISTEN) {
803 rc = smc_clcsock_accept(lsmc, &new_smc);
809 new_smc->listen_smc = lsmc;
810 new_smc->use_fallback = false; /* assume rdma capability first*/
811 sock_hold(&lsmc->sk); /* sock_put in smc_listen_work */
812 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
813 smc_copy_sock_settings_to_smc(new_smc);
814 schedule_work(&new_smc->smc_listen_work);
818 release_sock(&lsmc->sk);
819 lsmc->sk.sk_data_ready(&lsmc->sk); /* no more listening, wake accept */
822 static int smc_listen(struct socket *sock, int backlog)
824 struct sock *sk = sock->sk;
825 struct smc_sock *smc;
832 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
836 if (sk->sk_state == SMC_LISTEN) {
837 sk->sk_max_ack_backlog = backlog;
840 /* some socket options are handled in core, so we could not apply
841 * them to the clc socket -- copy smc socket options to clc socket
843 smc_copy_sock_settings_to_clc(smc);
845 rc = kernel_listen(smc->clcsock, backlog);
848 sk->sk_max_ack_backlog = backlog;
849 sk->sk_ack_backlog = 0;
850 sk->sk_state = SMC_LISTEN;
851 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
852 schedule_work(&smc->tcp_listen_work);
859 static int smc_accept(struct socket *sock, struct socket *new_sock,
862 struct sock *sk = sock->sk, *nsk;
863 DECLARE_WAITQUEUE(wait, current);
864 struct smc_sock *lsmc;
871 if (lsmc->sk.sk_state != SMC_LISTEN) {
876 /* Wait for an incoming connection */
877 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
878 add_wait_queue_exclusive(sk_sleep(sk), &wait);
879 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
880 set_current_state(TASK_INTERRUPTIBLE);
886 timeo = schedule_timeout(timeo);
887 /* wakeup by sk_data_ready in smc_listen_work() */
888 sched_annotate_sleep();
890 if (signal_pending(current)) {
891 rc = sock_intr_errno(timeo);
895 set_current_state(TASK_RUNNING);
896 remove_wait_queue(sk_sleep(sk), &wait);
899 rc = sock_error(nsk);
906 static int smc_getname(struct socket *sock, struct sockaddr *addr,
909 struct smc_sock *smc;
911 if (peer && (sock->sk->sk_state != SMC_ACTIVE))
914 smc = smc_sk(sock->sk);
916 return smc->clcsock->ops->getname(smc->clcsock, addr, len, peer);
919 static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
921 struct sock *sk = sock->sk;
922 struct smc_sock *smc;
927 if (sk->sk_state != SMC_ACTIVE)
929 if (smc->use_fallback)
930 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
932 rc = smc_tx_sendmsg(smc, msg, len);
938 static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
941 struct sock *sk = sock->sk;
942 struct smc_sock *smc;
947 if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED))
950 if (smc->use_fallback)
951 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
953 rc = sock_no_recvmsg(sock, msg, len, flags);
959 static unsigned int smc_accept_poll(struct sock *parent)
961 struct smc_sock *isk;
965 list_for_each_entry(isk, &smc_sk(parent)->accept_q, accept_q) {
966 sk = (struct sock *)isk;
968 if (sk->sk_state == SMC_ACTIVE) {
969 release_sock(parent);
970 return POLLIN | POLLRDNORM;
973 release_sock(parent);
978 static unsigned int smc_poll(struct file *file, struct socket *sock,
981 struct sock *sk = sock->sk;
982 unsigned int mask = 0;
983 struct smc_sock *smc;
986 smc = smc_sk(sock->sk);
987 if ((sk->sk_state == SMC_INIT) || smc->use_fallback) {
988 /* delegate to CLC child sock */
989 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
990 /* if non-blocking connect finished ... */
992 if ((sk->sk_state == SMC_INIT) && (mask & POLLOUT)) {
993 sk->sk_err = smc->clcsock->sk->sk_err;
997 rc = smc_connect_rdma(smc);
1001 /* success cases including fallback */
1002 mask |= POLLOUT | POLLWRNORM;
1007 sock_poll_wait(file, sk_sleep(sk), wait);
1008 if (sk->sk_state == SMC_LISTEN)
1009 /* woken up by sk_data_ready in smc_listen_work() */
1010 mask |= smc_accept_poll(sk);
1013 if (atomic_read(&smc->conn.sndbuf_space)) {
1014 mask |= POLLOUT | POLLWRNORM;
1016 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1017 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1019 /* for now - to be enhanced in follow-on patch */
1025 static int smc_shutdown(struct socket *sock, int how)
1027 struct sock *sk = sock->sk;
1028 struct smc_sock *smc;
1033 if ((how < SHUT_RD) || (how > SHUT_RDWR))
1039 if (sk->sk_state == SMC_CLOSED)
1041 if (smc->use_fallback) {
1042 rc = kernel_sock_shutdown(smc->clcsock, how);
1043 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1044 if (sk->sk_shutdown == SHUTDOWN_MASK)
1045 sk->sk_state = SMC_CLOSED;
1047 rc = sock_no_shutdown(sock, how);
1057 static int smc_setsockopt(struct socket *sock, int level, int optname,
1058 char __user *optval, unsigned int optlen)
1060 struct sock *sk = sock->sk;
1061 struct smc_sock *smc;
1065 /* generic setsockopts reaching us here always apply to the
1068 return smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1072 static int smc_getsockopt(struct socket *sock, int level, int optname,
1073 char __user *optval, int __user *optlen)
1075 struct smc_sock *smc;
1077 smc = smc_sk(sock->sk);
1078 /* socket options apply to the CLC socket */
1079 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1083 static int smc_ioctl(struct socket *sock, unsigned int cmd,
1086 struct smc_sock *smc;
1088 smc = smc_sk(sock->sk);
1089 if (smc->use_fallback)
1090 return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1092 return sock_no_ioctl(sock, cmd, arg);
1095 static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1096 int offset, size_t size, int flags)
1098 struct sock *sk = sock->sk;
1099 struct smc_sock *smc;
1104 if (sk->sk_state != SMC_ACTIVE)
1106 if (smc->use_fallback)
1107 rc = kernel_sendpage(smc->clcsock, page, offset,
1110 rc = sock_no_sendpage(sock, page, offset, size, flags);
1117 static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1118 struct pipe_inode_info *pipe, size_t len,
1121 struct sock *sk = sock->sk;
1122 struct smc_sock *smc;
1127 if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED))
1129 if (smc->use_fallback) {
1130 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1140 /* must look like tcp */
1141 static const struct proto_ops smc_sock_ops = {
1143 .owner = THIS_MODULE,
1144 .release = smc_release,
1146 .connect = smc_connect,
1147 .socketpair = sock_no_socketpair,
1148 .accept = smc_accept,
1149 .getname = smc_getname,
1152 .listen = smc_listen,
1153 .shutdown = smc_shutdown,
1154 .setsockopt = smc_setsockopt,
1155 .getsockopt = smc_getsockopt,
1156 .sendmsg = smc_sendmsg,
1157 .recvmsg = smc_recvmsg,
1158 .mmap = sock_no_mmap,
1159 .sendpage = smc_sendpage,
1160 .splice_read = smc_splice_read,
1163 static int smc_create(struct net *net, struct socket *sock, int protocol,
1166 struct smc_sock *smc;
1170 rc = -ESOCKTNOSUPPORT;
1171 if (sock->type != SOCK_STREAM)
1174 rc = -EPROTONOSUPPORT;
1175 if ((protocol != IPPROTO_IP) && (protocol != IPPROTO_TCP))
1179 sock->ops = &smc_sock_ops;
1180 sk = smc_sock_alloc(net, sock);
1184 /* create internal TCP socket for CLC handshake and fallback */
1186 smc->use_fallback = false; /* assume rdma capability first */
1187 rc = sock_create_kern(net, PF_INET, SOCK_STREAM,
1188 IPPROTO_TCP, &smc->clcsock);
1190 sk_common_release(sk);
1191 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1192 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
1198 static const struct net_proto_family smc_sock_family_ops = {
1200 .owner = THIS_MODULE,
1201 .create = smc_create,
1204 static int __init smc_init(void)
1208 rc = smc_pnet_init();
1212 rc = smc_llc_init();
1214 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1218 rc = smc_cdc_init();
1220 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1224 rc = proto_register(&smc_proto, 1);
1226 pr_err("%s: proto_register fails with %d\n", __func__, rc);
1230 rc = sock_register(&smc_sock_family_ops);
1232 pr_err("%s: sock_register fails with %d\n", __func__, rc);
1236 rc = smc_ib_register_client();
1238 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1245 sock_unregister(PF_SMC);
1247 proto_unregister(&smc_proto);
1253 static void __exit smc_exit(void)
1255 struct smc_link_group *lgr, *lg;
1256 LIST_HEAD(lgr_freeing_list);
1258 spin_lock_bh(&smc_lgr_list.lock);
1259 if (!list_empty(&smc_lgr_list.list))
1260 list_splice_init(&smc_lgr_list.list, &lgr_freeing_list);
1261 spin_unlock_bh(&smc_lgr_list.lock);
1262 list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) {
1263 list_del_init(&lgr->list);
1264 smc_lgr_free(lgr); /* free link group */
1266 smc_ib_unregister_client();
1267 sock_unregister(PF_SMC);
1268 proto_unregister(&smc_proto);
1272 module_init(smc_init);
1273 module_exit(smc_exit);
1275 MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
1276 MODULE_DESCRIPTION("smc socket address family");
1277 MODULE_LICENSE("GPL");
1278 MODULE_ALIAS_NETPROTO(PF_SMC);