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>
38 static DEFINE_MUTEX(smc_create_lgr_pending); /* serialize link group
42 struct smc_lgr_list smc_lgr_list = { /* established link groups */
43 .lock = __SPIN_LOCK_UNLOCKED(smc_lgr_list.lock),
44 .list = LIST_HEAD_INIT(smc_lgr_list.list),
47 static void smc_tcp_listen_work(struct work_struct *);
49 static void smc_set_keepalive(struct sock *sk, int val)
51 struct smc_sock *smc = smc_sk(sk);
53 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
56 static struct proto smc_proto = {
59 .keepalive = smc_set_keepalive,
60 .obj_size = sizeof(struct smc_sock),
61 .slab_flags = SLAB_DESTROY_BY_RCU,
64 static int smc_release(struct socket *sock)
66 struct sock *sk = sock->sk;
75 sk->sk_state = SMC_CLOSED;
77 sock_release(smc->clcsock);
91 static void smc_destruct(struct sock *sk)
93 if (sk->sk_state != SMC_CLOSED)
95 if (!sock_flag(sk, SOCK_DEAD))
98 sk_refcnt_debug_dec(sk);
101 static struct sock *smc_sock_alloc(struct net *net, struct socket *sock)
103 struct smc_sock *smc;
106 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, &smc_proto, 0);
110 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
111 sk->sk_state = SMC_INIT;
112 sk->sk_destruct = smc_destruct;
113 sk->sk_protocol = SMCPROTO_SMC;
115 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
116 INIT_LIST_HEAD(&smc->accept_q);
117 spin_lock_init(&smc->accept_q_lock);
118 sk_refcnt_debug_inc(sk);
123 static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
126 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
127 struct sock *sk = sock->sk;
128 struct smc_sock *smc;
133 /* replicate tests from inet_bind(), to be safe wrt. future changes */
135 if (addr_len < sizeof(struct sockaddr_in))
139 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
140 if ((addr->sin_family != AF_INET) &&
141 ((addr->sin_family != AF_UNSPEC) ||
142 (addr->sin_addr.s_addr != htonl(INADDR_ANY))))
147 /* Check if socket is already active */
149 if (sk->sk_state != SMC_INIT)
152 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
153 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
161 static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
164 /* options we don't get control via setsockopt for */
165 nsk->sk_type = osk->sk_type;
166 nsk->sk_sndbuf = osk->sk_sndbuf;
167 nsk->sk_rcvbuf = osk->sk_rcvbuf;
168 nsk->sk_sndtimeo = osk->sk_sndtimeo;
169 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
170 nsk->sk_mark = osk->sk_mark;
171 nsk->sk_priority = osk->sk_priority;
172 nsk->sk_rcvlowat = osk->sk_rcvlowat;
173 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
174 nsk->sk_err = osk->sk_err;
176 nsk->sk_flags &= ~mask;
177 nsk->sk_flags |= osk->sk_flags & mask;
180 #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
181 (1UL << SOCK_KEEPOPEN) | \
182 (1UL << SOCK_LINGER) | \
183 (1UL << SOCK_BROADCAST) | \
184 (1UL << SOCK_TIMESTAMP) | \
185 (1UL << SOCK_DBG) | \
186 (1UL << SOCK_RCVTSTAMP) | \
187 (1UL << SOCK_RCVTSTAMPNS) | \
188 (1UL << SOCK_LOCALROUTE) | \
189 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
190 (1UL << SOCK_RXQ_OVFL) | \
191 (1UL << SOCK_WIFI_STATUS) | \
192 (1UL << SOCK_NOFCS) | \
193 (1UL << SOCK_FILTER_LOCKED))
194 /* copy only relevant settings and flags of SOL_SOCKET level from smc to
195 * clc socket (since smc is not called for these options from net/core)
197 static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
199 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
202 #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
203 (1UL << SOCK_KEEPOPEN) | \
204 (1UL << SOCK_LINGER) | \
206 /* copy only settings and flags relevant for smc from clc to smc socket */
207 static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
209 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
212 /* determine subnet and mask of internal TCP socket */
213 int smc_netinfo_by_tcpsk(struct socket *clcsock,
214 __be32 *subnet, u8 *prefix_len)
216 struct dst_entry *dst = sk_dst_get(clcsock->sk);
217 struct sockaddr_in addr;
230 /* get address to which the internal TCP socket is bound */
231 kernel_getsockname(clcsock, (struct sockaddr *)&addr, &len);
232 /* analyze IPv4 specific data of net_device belonging to TCP socket */
233 for_ifa(dst->dev->ip_ptr) {
234 if (ifa->ifa_address != addr.sin_addr.s_addr)
236 *prefix_len = inet_mask_len(ifa->ifa_mask);
237 *subnet = ifa->ifa_address & ifa->ifa_mask;
240 } endfor_ifa(dst->dev->ip_ptr);
248 static void smc_conn_save_peer_info(struct smc_sock *smc,
249 struct smc_clc_msg_accept_confirm *clc)
251 smc->conn.peer_conn_idx = clc->conn_idx;
254 static void smc_link_save_peer_info(struct smc_link *link,
255 struct smc_clc_msg_accept_confirm *clc)
257 link->peer_qpn = ntoh24(clc->qpn);
258 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
259 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
260 link->peer_psn = ntoh24(clc->psn);
261 link->peer_mtu = clc->qp_mtu;
264 /* setup for RDMA connection of client */
265 static int smc_connect_rdma(struct smc_sock *smc)
267 struct sockaddr_in *inaddr = (struct sockaddr_in *)smc->addr;
268 struct smc_clc_msg_accept_confirm aclc;
269 int local_contact = SMC_FIRST_CONTACT;
270 struct smc_ib_device *smcibdev;
271 struct smc_link *link;
272 u8 srv_first_contact;
277 /* IPSec connections opt out of SMC-R optimizations */
278 if (using_ipsec(smc)) {
279 reason_code = SMC_CLC_DECL_IPSEC;
283 /* PNET table look up: search active ib_device and port
284 * within same PNETID that also contains the ethernet device
285 * used for the internal TCP socket
287 smc_pnet_find_roce_resource(smc->clcsock->sk, &smcibdev, &ibport);
289 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
293 /* do inband token exchange */
294 reason_code = smc_clc_send_proposal(smc, smcibdev, ibport);
295 if (reason_code < 0) {
299 if (reason_code > 0) /* configuration error */
301 /* receive SMC Accept CLC message */
302 reason_code = smc_clc_wait_msg(smc, &aclc, sizeof(aclc),
304 if (reason_code < 0) {
311 srv_first_contact = aclc.hdr.flag;
312 mutex_lock(&smc_create_lgr_pending);
313 local_contact = smc_conn_create(smc, inaddr->sin_addr.s_addr, smcibdev,
314 ibport, &aclc.lcl, srv_first_contact);
315 if (local_contact < 0) {
318 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
319 else if (rc == -ENOLINK)
320 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
321 goto decline_rdma_unlock;
323 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
325 smc_conn_save_peer_info(smc, &aclc);
326 if (local_contact == SMC_FIRST_CONTACT)
327 smc_link_save_peer_info(link, &aclc);
328 /* tbd in follow-on patch: more steps to setup RDMA communcication,
329 * create rmbs, map rmbs, rtoken_handling, modify_qp
332 rc = smc_clc_send_confirm(smc);
336 /* tbd in follow-on patch: llc_confirm */
338 mutex_unlock(&smc_create_lgr_pending);
340 smc_copy_sock_settings_to_clc(smc);
341 smc->sk.sk_state = SMC_ACTIVE;
343 return rc ? rc : local_contact;
346 mutex_unlock(&smc_create_lgr_pending);
347 smc_conn_free(&smc->conn);
349 /* RDMA setup failed, switch back to TCP */
350 smc->use_fallback = true;
351 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
352 rc = smc_clc_send_decline(smc, reason_code, 0);
353 if (rc < sizeof(struct smc_clc_msg_decline))
359 mutex_unlock(&smc_create_lgr_pending);
360 smc_conn_free(&smc->conn);
365 static int smc_connect(struct socket *sock, struct sockaddr *addr,
368 struct sock *sk = sock->sk;
369 struct smc_sock *smc;
374 /* separate smc parameter checking to be safe */
375 if (alen < sizeof(addr->sa_family))
377 if (addr->sa_family != AF_INET)
379 smc->addr = addr; /* needed for nonblocking connect */
382 switch (sk->sk_state) {
393 smc_copy_sock_settings_to_clc(smc);
394 rc = kernel_connect(smc->clcsock, addr, alen, flags);
398 /* setup RDMA connection */
399 rc = smc_connect_rdma(smc);
403 rc = 0; /* success cases including fallback */
411 static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
413 struct sock *sk = &lsmc->sk;
414 struct socket *new_clcsock;
418 release_sock(&lsmc->sk);
419 new_sk = smc_sock_alloc(sock_net(sk), NULL);
422 lsmc->sk.sk_err = ENOMEM;
424 lock_sock(&lsmc->sk);
427 *new_smc = smc_sk(new_sk);
429 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
430 lock_sock(&lsmc->sk);
432 lsmc->sk.sk_err = -rc;
433 new_sk->sk_state = SMC_CLOSED;
434 sock_set_flag(new_sk, SOCK_DEAD);
439 if (lsmc->sk.sk_state == SMC_CLOSED) {
441 sock_release(new_clcsock);
442 new_sk->sk_state = SMC_CLOSED;
443 sock_set_flag(new_sk, SOCK_DEAD);
449 (*new_smc)->clcsock = new_clcsock;
454 /* add a just created sock to the accept queue of the listen sock as
455 * candidate for a following socket accept call from user space
457 static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
459 struct smc_sock *par = smc_sk(parent);
462 spin_lock(&par->accept_q_lock);
463 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
464 spin_unlock(&par->accept_q_lock);
465 sk_acceptq_added(parent);
468 /* remove a socket from the accept queue of its parental listening socket */
469 static void smc_accept_unlink(struct sock *sk)
471 struct smc_sock *par = smc_sk(sk)->listen_smc;
473 spin_lock(&par->accept_q_lock);
474 list_del_init(&smc_sk(sk)->accept_q);
475 spin_unlock(&par->accept_q_lock);
476 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
480 /* remove a sock from the accept queue to bind it to a new socket created
481 * for a socket accept call from user space
483 static struct sock *smc_accept_dequeue(struct sock *parent,
484 struct socket *new_sock)
486 struct smc_sock *isk, *n;
489 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
490 new_sk = (struct sock *)isk;
492 smc_accept_unlink(new_sk);
493 if (new_sk->sk_state == SMC_CLOSED) {
494 /* tbd in follow-on patch: close this sock */
498 sock_graft(new_sk, new_sock);
504 /* clean up for a created but never accepted sock */
505 static void smc_close_non_accepted(struct sock *sk)
507 struct smc_sock *smc = smc_sk(sk);
517 /* more closing stuff to be added with socket closing patch */
521 /* setup for RDMA connection of server */
522 static void smc_listen_work(struct work_struct *work)
524 struct smc_sock *new_smc = container_of(work, struct smc_sock,
526 struct socket *newclcsock = new_smc->clcsock;
527 struct smc_sock *lsmc = new_smc->listen_smc;
528 struct smc_clc_msg_accept_confirm cclc;
529 int local_contact = SMC_REUSE_CONTACT;
530 struct sock *newsmcsk = &new_smc->sk;
531 struct smc_clc_msg_proposal pclc;
532 struct smc_ib_device *smcibdev;
533 struct sockaddr_in peeraddr;
534 struct smc_link *link;
541 /* do inband token exchange -
542 *wait for and receive SMC Proposal CLC message
544 reason_code = smc_clc_wait_msg(new_smc, &pclc, sizeof(pclc),
551 /* IPSec connections opt out of SMC-R optimizations */
552 if (using_ipsec(new_smc)) {
553 reason_code = SMC_CLC_DECL_IPSEC;
557 /* PNET table look up: search active ib_device and port
558 * within same PNETID that also contains the ethernet device
559 * used for the internal TCP socket
561 smc_pnet_find_roce_resource(newclcsock->sk, &smcibdev, &ibport);
563 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
567 /* determine subnet and mask from internal TCP socket */
568 rc = smc_netinfo_by_tcpsk(newclcsock, &subnet, &prefix_len);
570 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
573 if ((pclc.outgoing_subnet != subnet) ||
574 (pclc.prefix_len != prefix_len)) {
575 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
579 /* get address of the peer connected to the internal TCP socket */
580 kernel_getpeername(newclcsock, (struct sockaddr *)&peeraddr, &len);
582 /* allocate connection / link group */
583 mutex_lock(&smc_create_lgr_pending);
584 local_contact = smc_conn_create(new_smc, peeraddr.sin_addr.s_addr,
585 smcibdev, ibport, &pclc.lcl, 0);
586 if (local_contact == SMC_REUSE_CONTACT)
587 /* lock no longer needed, free it due to following
588 * smc_clc_wait_msg() call
590 mutex_unlock(&smc_create_lgr_pending);
591 if (local_contact < 0) {
594 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
595 else if (rc == -ENOLINK)
596 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
599 link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
601 /* tbd in follow-on patch: more steps to setup RDMA communcication,
602 * create rmbs, map rmbs
605 rc = smc_clc_send_accept(new_smc, local_contact);
609 /* receive SMC Confirm CLC message */
610 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
616 smc_conn_save_peer_info(new_smc, &cclc);
617 if (local_contact == SMC_FIRST_CONTACT)
618 smc_link_save_peer_info(link, &cclc);
620 /* tbd in follow-on patch: more steps to setup RDMA communcication,
621 * rtoken_handling, modify_qp
625 sk_refcnt_debug_inc(newsmcsk);
626 newsmcsk->sk_state = SMC_ACTIVE;
628 if (local_contact == SMC_FIRST_CONTACT)
629 mutex_unlock(&smc_create_lgr_pending);
630 lock_sock(&lsmc->sk);
631 if (lsmc->sk.sk_state == SMC_LISTEN) {
632 smc_accept_enqueue(&lsmc->sk, newsmcsk);
633 } else { /* no longer listening */
634 smc_close_non_accepted(newsmcsk);
636 release_sock(&lsmc->sk);
639 lsmc->sk.sk_data_ready(&lsmc->sk);
640 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
644 /* RDMA setup failed, switch back to TCP */
645 smc_conn_free(&new_smc->conn);
646 new_smc->use_fallback = true;
647 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
648 rc = smc_clc_send_decline(new_smc, reason_code, 0);
649 if (rc < sizeof(struct smc_clc_msg_decline))
655 newsmcsk->sk_state = SMC_CLOSED;
656 goto enqueue; /* queue new sock with sk_err set */
659 static void smc_tcp_listen_work(struct work_struct *work)
661 struct smc_sock *lsmc = container_of(work, struct smc_sock,
663 struct smc_sock *new_smc;
666 lock_sock(&lsmc->sk);
667 while (lsmc->sk.sk_state == SMC_LISTEN) {
668 rc = smc_clcsock_accept(lsmc, &new_smc);
674 new_smc->listen_smc = lsmc;
675 new_smc->use_fallback = false; /* assume rdma capability first*/
676 sock_hold(&lsmc->sk); /* sock_put in smc_listen_work */
677 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
678 smc_copy_sock_settings_to_smc(new_smc);
679 schedule_work(&new_smc->smc_listen_work);
683 release_sock(&lsmc->sk);
684 lsmc->sk.sk_data_ready(&lsmc->sk); /* no more listening, wake accept */
687 static int smc_listen(struct socket *sock, int backlog)
689 struct sock *sk = sock->sk;
690 struct smc_sock *smc;
697 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
701 if (sk->sk_state == SMC_LISTEN) {
702 sk->sk_max_ack_backlog = backlog;
705 /* some socket options are handled in core, so we could not apply
706 * them to the clc socket -- copy smc socket options to clc socket
708 smc_copy_sock_settings_to_clc(smc);
710 rc = kernel_listen(smc->clcsock, backlog);
713 sk->sk_max_ack_backlog = backlog;
714 sk->sk_ack_backlog = 0;
715 sk->sk_state = SMC_LISTEN;
716 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
717 schedule_work(&smc->tcp_listen_work);
724 static int smc_accept(struct socket *sock, struct socket *new_sock,
727 struct sock *sk = sock->sk, *nsk;
728 DECLARE_WAITQUEUE(wait, current);
729 struct smc_sock *lsmc;
736 if (lsmc->sk.sk_state != SMC_LISTEN) {
741 /* Wait for an incoming connection */
742 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
743 add_wait_queue_exclusive(sk_sleep(sk), &wait);
744 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
745 set_current_state(TASK_INTERRUPTIBLE);
751 timeo = schedule_timeout(timeo);
752 /* wakeup by sk_data_ready in smc_listen_work() */
753 sched_annotate_sleep();
755 if (signal_pending(current)) {
756 rc = sock_intr_errno(timeo);
760 set_current_state(TASK_RUNNING);
761 remove_wait_queue(sk_sleep(sk), &wait);
764 rc = sock_error(nsk);
771 static int smc_getname(struct socket *sock, struct sockaddr *addr,
774 struct smc_sock *smc;
776 if (peer && (sock->sk->sk_state != SMC_ACTIVE))
779 smc = smc_sk(sock->sk);
781 return smc->clcsock->ops->getname(smc->clcsock, addr, len, peer);
784 static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
786 struct sock *sk = sock->sk;
787 struct smc_sock *smc;
792 if (sk->sk_state != SMC_ACTIVE)
794 if (smc->use_fallback)
795 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
797 rc = sock_no_sendmsg(sock, msg, len);
803 static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
806 struct sock *sk = sock->sk;
807 struct smc_sock *smc;
812 if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED))
815 if (smc->use_fallback)
816 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
818 rc = sock_no_recvmsg(sock, msg, len, flags);
824 static unsigned int smc_accept_poll(struct sock *parent)
826 struct smc_sock *isk;
830 list_for_each_entry(isk, &smc_sk(parent)->accept_q, accept_q) {
831 sk = (struct sock *)isk;
833 if (sk->sk_state == SMC_ACTIVE) {
834 release_sock(parent);
835 return POLLIN | POLLRDNORM;
838 release_sock(parent);
843 static unsigned int smc_poll(struct file *file, struct socket *sock,
846 struct sock *sk = sock->sk;
847 unsigned int mask = 0;
848 struct smc_sock *smc;
851 smc = smc_sk(sock->sk);
852 if ((sk->sk_state == SMC_INIT) || smc->use_fallback) {
853 /* delegate to CLC child sock */
854 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
855 /* if non-blocking connect finished ... */
857 if ((sk->sk_state == SMC_INIT) && (mask & POLLOUT)) {
858 sk->sk_err = smc->clcsock->sk->sk_err;
862 rc = smc_connect_rdma(smc);
866 /* success cases including fallback */
867 mask |= POLLOUT | POLLWRNORM;
872 sock_poll_wait(file, sk_sleep(sk), wait);
873 if (sk->sk_state == SMC_LISTEN)
874 /* woken up by sk_data_ready in smc_listen_work() */
875 mask |= smc_accept_poll(sk);
878 /* for now - to be enhanced in follow-on patch */
884 static int smc_shutdown(struct socket *sock, int how)
886 struct sock *sk = sock->sk;
887 struct smc_sock *smc;
892 if ((how < SHUT_RD) || (how > SHUT_RDWR))
898 if (sk->sk_state == SMC_CLOSED)
900 if (smc->use_fallback) {
901 rc = kernel_sock_shutdown(smc->clcsock, how);
902 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
903 if (sk->sk_shutdown == SHUTDOWN_MASK)
904 sk->sk_state = SMC_CLOSED;
906 rc = sock_no_shutdown(sock, how);
916 static int smc_setsockopt(struct socket *sock, int level, int optname,
917 char __user *optval, unsigned int optlen)
919 struct sock *sk = sock->sk;
920 struct smc_sock *smc;
924 /* generic setsockopts reaching us here always apply to the
927 return smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
931 static int smc_getsockopt(struct socket *sock, int level, int optname,
932 char __user *optval, int __user *optlen)
934 struct smc_sock *smc;
936 smc = smc_sk(sock->sk);
937 /* socket options apply to the CLC socket */
938 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
942 static int smc_ioctl(struct socket *sock, unsigned int cmd,
945 struct smc_sock *smc;
947 smc = smc_sk(sock->sk);
948 if (smc->use_fallback)
949 return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
951 return sock_no_ioctl(sock, cmd, arg);
954 static ssize_t smc_sendpage(struct socket *sock, struct page *page,
955 int offset, size_t size, int flags)
957 struct sock *sk = sock->sk;
958 struct smc_sock *smc;
963 if (sk->sk_state != SMC_ACTIVE)
965 if (smc->use_fallback)
966 rc = kernel_sendpage(smc->clcsock, page, offset,
969 rc = sock_no_sendpage(sock, page, offset, size, flags);
976 static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
977 struct pipe_inode_info *pipe, size_t len,
980 struct sock *sk = sock->sk;
981 struct smc_sock *smc;
986 if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED))
988 if (smc->use_fallback) {
989 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
999 /* must look like tcp */
1000 static const struct proto_ops smc_sock_ops = {
1002 .owner = THIS_MODULE,
1003 .release = smc_release,
1005 .connect = smc_connect,
1006 .socketpair = sock_no_socketpair,
1007 .accept = smc_accept,
1008 .getname = smc_getname,
1011 .listen = smc_listen,
1012 .shutdown = smc_shutdown,
1013 .setsockopt = smc_setsockopt,
1014 .getsockopt = smc_getsockopt,
1015 .sendmsg = smc_sendmsg,
1016 .recvmsg = smc_recvmsg,
1017 .mmap = sock_no_mmap,
1018 .sendpage = smc_sendpage,
1019 .splice_read = smc_splice_read,
1022 static int smc_create(struct net *net, struct socket *sock, int protocol,
1025 struct smc_sock *smc;
1029 rc = -ESOCKTNOSUPPORT;
1030 if (sock->type != SOCK_STREAM)
1033 rc = -EPROTONOSUPPORT;
1034 if ((protocol != IPPROTO_IP) && (protocol != IPPROTO_TCP))
1038 sock->ops = &smc_sock_ops;
1039 sk = smc_sock_alloc(net, sock);
1043 /* create internal TCP socket for CLC handshake and fallback */
1045 smc->use_fallback = false; /* assume rdma capability first */
1046 rc = sock_create_kern(net, PF_INET, SOCK_STREAM,
1047 IPPROTO_TCP, &smc->clcsock);
1049 sk_common_release(sk);
1055 static const struct net_proto_family smc_sock_family_ops = {
1057 .owner = THIS_MODULE,
1058 .create = smc_create,
1061 static int __init smc_init(void)
1065 rc = smc_pnet_init();
1069 rc = proto_register(&smc_proto, 1);
1071 pr_err("%s: proto_register fails with %d\n", __func__, rc);
1075 rc = sock_register(&smc_sock_family_ops);
1077 pr_err("%s: sock_register fails with %d\n", __func__, rc);
1081 rc = smc_ib_register_client();
1083 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1090 sock_unregister(PF_SMC);
1092 proto_unregister(&smc_proto);
1098 static void __exit smc_exit(void)
1100 struct smc_link_group *lgr, *lg;
1101 LIST_HEAD(lgr_freeing_list);
1103 spin_lock_bh(&smc_lgr_list.lock);
1104 if (!list_empty(&smc_lgr_list.list))
1105 list_splice_init(&smc_lgr_list.list, &lgr_freeing_list);
1106 spin_unlock_bh(&smc_lgr_list.lock);
1107 list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) {
1108 list_del_init(&lgr->list);
1109 smc_lgr_free(lgr); /* free link group */
1111 smc_ib_unregister_client();
1112 sock_unregister(PF_SMC);
1113 proto_unregister(&smc_proto);
1117 module_init(smc_init);
1118 module_exit(smc_exit);
1120 MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
1121 MODULE_DESCRIPTION("smc socket address family");
1122 MODULE_LICENSE("GPL");
1123 MODULE_ALIAS_NETPROTO(PF_SMC);