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;
252 smc->conn.peer_rmbe_size = smc_uncompress_bufsize(clc->rmbe_size);
253 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
256 static void smc_link_save_peer_info(struct smc_link *link,
257 struct smc_clc_msg_accept_confirm *clc)
259 link->peer_qpn = ntoh24(clc->qpn);
260 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
261 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
262 link->peer_psn = ntoh24(clc->psn);
263 link->peer_mtu = clc->qp_mtu;
266 /* setup for RDMA connection of client */
267 static int smc_connect_rdma(struct smc_sock *smc)
269 struct sockaddr_in *inaddr = (struct sockaddr_in *)smc->addr;
270 struct smc_clc_msg_accept_confirm aclc;
271 int local_contact = SMC_FIRST_CONTACT;
272 struct smc_ib_device *smcibdev;
273 struct smc_link *link;
274 u8 srv_first_contact;
279 /* IPSec connections opt out of SMC-R optimizations */
280 if (using_ipsec(smc)) {
281 reason_code = SMC_CLC_DECL_IPSEC;
285 /* PNET table look up: search active ib_device and port
286 * within same PNETID that also contains the ethernet device
287 * used for the internal TCP socket
289 smc_pnet_find_roce_resource(smc->clcsock->sk, &smcibdev, &ibport);
291 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
295 /* do inband token exchange */
296 reason_code = smc_clc_send_proposal(smc, smcibdev, ibport);
297 if (reason_code < 0) {
301 if (reason_code > 0) /* configuration error */
303 /* receive SMC Accept CLC message */
304 reason_code = smc_clc_wait_msg(smc, &aclc, sizeof(aclc),
306 if (reason_code < 0) {
313 srv_first_contact = aclc.hdr.flag;
314 mutex_lock(&smc_create_lgr_pending);
315 local_contact = smc_conn_create(smc, inaddr->sin_addr.s_addr, smcibdev,
316 ibport, &aclc.lcl, srv_first_contact);
317 if (local_contact < 0) {
320 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
321 else if (rc == -ENOLINK)
322 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
323 goto decline_rdma_unlock;
325 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
327 smc_conn_save_peer_info(smc, &aclc);
329 rc = smc_sndbuf_create(smc);
331 reason_code = SMC_CLC_DECL_MEM;
332 goto decline_rdma_unlock;
334 rc = smc_rmb_create(smc);
336 reason_code = SMC_CLC_DECL_MEM;
337 goto decline_rdma_unlock;
340 if (local_contact == SMC_FIRST_CONTACT)
341 smc_link_save_peer_info(link, &aclc);
342 /* tbd in follow-on patch: more steps to setup RDMA communcication,
343 * create rmbs, map rmbs, rtoken_handling, modify_qp
346 rc = smc_clc_send_confirm(smc);
350 /* tbd in follow-on patch: llc_confirm */
352 mutex_unlock(&smc_create_lgr_pending);
354 smc_copy_sock_settings_to_clc(smc);
355 smc->sk.sk_state = SMC_ACTIVE;
357 return rc ? rc : local_contact;
360 mutex_unlock(&smc_create_lgr_pending);
361 smc_conn_free(&smc->conn);
363 /* RDMA setup failed, switch back to TCP */
364 smc->use_fallback = true;
365 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
366 rc = smc_clc_send_decline(smc, reason_code, 0);
367 if (rc < sizeof(struct smc_clc_msg_decline))
373 mutex_unlock(&smc_create_lgr_pending);
374 smc_conn_free(&smc->conn);
379 static int smc_connect(struct socket *sock, struct sockaddr *addr,
382 struct sock *sk = sock->sk;
383 struct smc_sock *smc;
388 /* separate smc parameter checking to be safe */
389 if (alen < sizeof(addr->sa_family))
391 if (addr->sa_family != AF_INET)
393 smc->addr = addr; /* needed for nonblocking connect */
396 switch (sk->sk_state) {
407 smc_copy_sock_settings_to_clc(smc);
408 rc = kernel_connect(smc->clcsock, addr, alen, flags);
412 /* setup RDMA connection */
413 rc = smc_connect_rdma(smc);
417 rc = 0; /* success cases including fallback */
425 static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
427 struct sock *sk = &lsmc->sk;
428 struct socket *new_clcsock;
432 release_sock(&lsmc->sk);
433 new_sk = smc_sock_alloc(sock_net(sk), NULL);
436 lsmc->sk.sk_err = ENOMEM;
438 lock_sock(&lsmc->sk);
441 *new_smc = smc_sk(new_sk);
443 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
444 lock_sock(&lsmc->sk);
446 lsmc->sk.sk_err = -rc;
447 new_sk->sk_state = SMC_CLOSED;
448 sock_set_flag(new_sk, SOCK_DEAD);
453 if (lsmc->sk.sk_state == SMC_CLOSED) {
455 sock_release(new_clcsock);
456 new_sk->sk_state = SMC_CLOSED;
457 sock_set_flag(new_sk, SOCK_DEAD);
463 (*new_smc)->clcsock = new_clcsock;
468 /* add a just created sock to the accept queue of the listen sock as
469 * candidate for a following socket accept call from user space
471 static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
473 struct smc_sock *par = smc_sk(parent);
476 spin_lock(&par->accept_q_lock);
477 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
478 spin_unlock(&par->accept_q_lock);
479 sk_acceptq_added(parent);
482 /* remove a socket from the accept queue of its parental listening socket */
483 static void smc_accept_unlink(struct sock *sk)
485 struct smc_sock *par = smc_sk(sk)->listen_smc;
487 spin_lock(&par->accept_q_lock);
488 list_del_init(&smc_sk(sk)->accept_q);
489 spin_unlock(&par->accept_q_lock);
490 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
494 /* remove a sock from the accept queue to bind it to a new socket created
495 * for a socket accept call from user space
497 static struct sock *smc_accept_dequeue(struct sock *parent,
498 struct socket *new_sock)
500 struct smc_sock *isk, *n;
503 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
504 new_sk = (struct sock *)isk;
506 smc_accept_unlink(new_sk);
507 if (new_sk->sk_state == SMC_CLOSED) {
508 /* tbd in follow-on patch: close this sock */
512 sock_graft(new_sk, new_sock);
518 /* clean up for a created but never accepted sock */
519 static void smc_close_non_accepted(struct sock *sk)
521 struct smc_sock *smc = smc_sk(sk);
531 /* more closing stuff to be added with socket closing patch */
535 /* setup for RDMA connection of server */
536 static void smc_listen_work(struct work_struct *work)
538 struct smc_sock *new_smc = container_of(work, struct smc_sock,
540 struct socket *newclcsock = new_smc->clcsock;
541 struct smc_sock *lsmc = new_smc->listen_smc;
542 struct smc_clc_msg_accept_confirm cclc;
543 int local_contact = SMC_REUSE_CONTACT;
544 struct sock *newsmcsk = &new_smc->sk;
545 struct smc_clc_msg_proposal pclc;
546 struct smc_ib_device *smcibdev;
547 struct sockaddr_in peeraddr;
548 struct smc_link *link;
555 /* do inband token exchange -
556 *wait for and receive SMC Proposal CLC message
558 reason_code = smc_clc_wait_msg(new_smc, &pclc, sizeof(pclc),
565 /* IPSec connections opt out of SMC-R optimizations */
566 if (using_ipsec(new_smc)) {
567 reason_code = SMC_CLC_DECL_IPSEC;
571 /* PNET table look up: search active ib_device and port
572 * within same PNETID that also contains the ethernet device
573 * used for the internal TCP socket
575 smc_pnet_find_roce_resource(newclcsock->sk, &smcibdev, &ibport);
577 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
581 /* determine subnet and mask from internal TCP socket */
582 rc = smc_netinfo_by_tcpsk(newclcsock, &subnet, &prefix_len);
584 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
587 if ((pclc.outgoing_subnet != subnet) ||
588 (pclc.prefix_len != prefix_len)) {
589 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
593 /* get address of the peer connected to the internal TCP socket */
594 kernel_getpeername(newclcsock, (struct sockaddr *)&peeraddr, &len);
596 /* allocate connection / link group */
597 mutex_lock(&smc_create_lgr_pending);
598 local_contact = smc_conn_create(new_smc, peeraddr.sin_addr.s_addr,
599 smcibdev, ibport, &pclc.lcl, 0);
600 if (local_contact == SMC_REUSE_CONTACT)
601 /* lock no longer needed, free it due to following
602 * smc_clc_wait_msg() call
604 mutex_unlock(&smc_create_lgr_pending);
605 if (local_contact < 0) {
608 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
609 else if (rc == -ENOLINK)
610 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
613 link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
615 rc = smc_sndbuf_create(new_smc);
617 reason_code = SMC_CLC_DECL_MEM;
620 rc = smc_rmb_create(new_smc);
622 reason_code = SMC_CLC_DECL_MEM;
626 rc = smc_clc_send_accept(new_smc, local_contact);
630 /* receive SMC Confirm CLC message */
631 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
637 smc_conn_save_peer_info(new_smc, &cclc);
638 if (local_contact == SMC_FIRST_CONTACT)
639 smc_link_save_peer_info(link, &cclc);
641 /* tbd in follow-on patch: more steps to setup RDMA communcication,
642 * rtoken_handling, modify_qp
646 sk_refcnt_debug_inc(newsmcsk);
647 newsmcsk->sk_state = SMC_ACTIVE;
649 if (local_contact == SMC_FIRST_CONTACT)
650 mutex_unlock(&smc_create_lgr_pending);
651 lock_sock(&lsmc->sk);
652 if (lsmc->sk.sk_state == SMC_LISTEN) {
653 smc_accept_enqueue(&lsmc->sk, newsmcsk);
654 } else { /* no longer listening */
655 smc_close_non_accepted(newsmcsk);
657 release_sock(&lsmc->sk);
660 lsmc->sk.sk_data_ready(&lsmc->sk);
661 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
665 /* RDMA setup failed, switch back to TCP */
666 smc_conn_free(&new_smc->conn);
667 new_smc->use_fallback = true;
668 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
669 rc = smc_clc_send_decline(new_smc, reason_code, 0);
670 if (rc < sizeof(struct smc_clc_msg_decline))
676 newsmcsk->sk_state = SMC_CLOSED;
677 goto enqueue; /* queue new sock with sk_err set */
680 static void smc_tcp_listen_work(struct work_struct *work)
682 struct smc_sock *lsmc = container_of(work, struct smc_sock,
684 struct smc_sock *new_smc;
687 lock_sock(&lsmc->sk);
688 while (lsmc->sk.sk_state == SMC_LISTEN) {
689 rc = smc_clcsock_accept(lsmc, &new_smc);
695 new_smc->listen_smc = lsmc;
696 new_smc->use_fallback = false; /* assume rdma capability first*/
697 sock_hold(&lsmc->sk); /* sock_put in smc_listen_work */
698 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
699 smc_copy_sock_settings_to_smc(new_smc);
700 schedule_work(&new_smc->smc_listen_work);
704 release_sock(&lsmc->sk);
705 lsmc->sk.sk_data_ready(&lsmc->sk); /* no more listening, wake accept */
708 static int smc_listen(struct socket *sock, int backlog)
710 struct sock *sk = sock->sk;
711 struct smc_sock *smc;
718 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
722 if (sk->sk_state == SMC_LISTEN) {
723 sk->sk_max_ack_backlog = backlog;
726 /* some socket options are handled in core, so we could not apply
727 * them to the clc socket -- copy smc socket options to clc socket
729 smc_copy_sock_settings_to_clc(smc);
731 rc = kernel_listen(smc->clcsock, backlog);
734 sk->sk_max_ack_backlog = backlog;
735 sk->sk_ack_backlog = 0;
736 sk->sk_state = SMC_LISTEN;
737 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
738 schedule_work(&smc->tcp_listen_work);
745 static int smc_accept(struct socket *sock, struct socket *new_sock,
748 struct sock *sk = sock->sk, *nsk;
749 DECLARE_WAITQUEUE(wait, current);
750 struct smc_sock *lsmc;
757 if (lsmc->sk.sk_state != SMC_LISTEN) {
762 /* Wait for an incoming connection */
763 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
764 add_wait_queue_exclusive(sk_sleep(sk), &wait);
765 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
766 set_current_state(TASK_INTERRUPTIBLE);
772 timeo = schedule_timeout(timeo);
773 /* wakeup by sk_data_ready in smc_listen_work() */
774 sched_annotate_sleep();
776 if (signal_pending(current)) {
777 rc = sock_intr_errno(timeo);
781 set_current_state(TASK_RUNNING);
782 remove_wait_queue(sk_sleep(sk), &wait);
785 rc = sock_error(nsk);
792 static int smc_getname(struct socket *sock, struct sockaddr *addr,
795 struct smc_sock *smc;
797 if (peer && (sock->sk->sk_state != SMC_ACTIVE))
800 smc = smc_sk(sock->sk);
802 return smc->clcsock->ops->getname(smc->clcsock, addr, len, peer);
805 static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
807 struct sock *sk = sock->sk;
808 struct smc_sock *smc;
813 if (sk->sk_state != SMC_ACTIVE)
815 if (smc->use_fallback)
816 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
818 rc = sock_no_sendmsg(sock, msg, len);
824 static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
827 struct sock *sk = sock->sk;
828 struct smc_sock *smc;
833 if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED))
836 if (smc->use_fallback)
837 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
839 rc = sock_no_recvmsg(sock, msg, len, flags);
845 static unsigned int smc_accept_poll(struct sock *parent)
847 struct smc_sock *isk;
851 list_for_each_entry(isk, &smc_sk(parent)->accept_q, accept_q) {
852 sk = (struct sock *)isk;
854 if (sk->sk_state == SMC_ACTIVE) {
855 release_sock(parent);
856 return POLLIN | POLLRDNORM;
859 release_sock(parent);
864 static unsigned int smc_poll(struct file *file, struct socket *sock,
867 struct sock *sk = sock->sk;
868 unsigned int mask = 0;
869 struct smc_sock *smc;
872 smc = smc_sk(sock->sk);
873 if ((sk->sk_state == SMC_INIT) || smc->use_fallback) {
874 /* delegate to CLC child sock */
875 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
876 /* if non-blocking connect finished ... */
878 if ((sk->sk_state == SMC_INIT) && (mask & POLLOUT)) {
879 sk->sk_err = smc->clcsock->sk->sk_err;
883 rc = smc_connect_rdma(smc);
887 /* success cases including fallback */
888 mask |= POLLOUT | POLLWRNORM;
893 sock_poll_wait(file, sk_sleep(sk), wait);
894 if (sk->sk_state == SMC_LISTEN)
895 /* woken up by sk_data_ready in smc_listen_work() */
896 mask |= smc_accept_poll(sk);
899 /* for now - to be enhanced in follow-on patch */
905 static int smc_shutdown(struct socket *sock, int how)
907 struct sock *sk = sock->sk;
908 struct smc_sock *smc;
913 if ((how < SHUT_RD) || (how > SHUT_RDWR))
919 if (sk->sk_state == SMC_CLOSED)
921 if (smc->use_fallback) {
922 rc = kernel_sock_shutdown(smc->clcsock, how);
923 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
924 if (sk->sk_shutdown == SHUTDOWN_MASK)
925 sk->sk_state = SMC_CLOSED;
927 rc = sock_no_shutdown(sock, how);
937 static int smc_setsockopt(struct socket *sock, int level, int optname,
938 char __user *optval, unsigned int optlen)
940 struct sock *sk = sock->sk;
941 struct smc_sock *smc;
945 /* generic setsockopts reaching us here always apply to the
948 return smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
952 static int smc_getsockopt(struct socket *sock, int level, int optname,
953 char __user *optval, int __user *optlen)
955 struct smc_sock *smc;
957 smc = smc_sk(sock->sk);
958 /* socket options apply to the CLC socket */
959 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
963 static int smc_ioctl(struct socket *sock, unsigned int cmd,
966 struct smc_sock *smc;
968 smc = smc_sk(sock->sk);
969 if (smc->use_fallback)
970 return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
972 return sock_no_ioctl(sock, cmd, arg);
975 static ssize_t smc_sendpage(struct socket *sock, struct page *page,
976 int offset, size_t size, int flags)
978 struct sock *sk = sock->sk;
979 struct smc_sock *smc;
984 if (sk->sk_state != SMC_ACTIVE)
986 if (smc->use_fallback)
987 rc = kernel_sendpage(smc->clcsock, page, offset,
990 rc = sock_no_sendpage(sock, page, offset, size, flags);
997 static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
998 struct pipe_inode_info *pipe, size_t len,
1001 struct sock *sk = sock->sk;
1002 struct smc_sock *smc;
1007 if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED))
1009 if (smc->use_fallback) {
1010 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1020 /* must look like tcp */
1021 static const struct proto_ops smc_sock_ops = {
1023 .owner = THIS_MODULE,
1024 .release = smc_release,
1026 .connect = smc_connect,
1027 .socketpair = sock_no_socketpair,
1028 .accept = smc_accept,
1029 .getname = smc_getname,
1032 .listen = smc_listen,
1033 .shutdown = smc_shutdown,
1034 .setsockopt = smc_setsockopt,
1035 .getsockopt = smc_getsockopt,
1036 .sendmsg = smc_sendmsg,
1037 .recvmsg = smc_recvmsg,
1038 .mmap = sock_no_mmap,
1039 .sendpage = smc_sendpage,
1040 .splice_read = smc_splice_read,
1043 static int smc_create(struct net *net, struct socket *sock, int protocol,
1046 struct smc_sock *smc;
1050 rc = -ESOCKTNOSUPPORT;
1051 if (sock->type != SOCK_STREAM)
1054 rc = -EPROTONOSUPPORT;
1055 if ((protocol != IPPROTO_IP) && (protocol != IPPROTO_TCP))
1059 sock->ops = &smc_sock_ops;
1060 sk = smc_sock_alloc(net, sock);
1064 /* create internal TCP socket for CLC handshake and fallback */
1066 smc->use_fallback = false; /* assume rdma capability first */
1067 rc = sock_create_kern(net, PF_INET, SOCK_STREAM,
1068 IPPROTO_TCP, &smc->clcsock);
1070 sk_common_release(sk);
1071 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1072 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
1078 static const struct net_proto_family smc_sock_family_ops = {
1080 .owner = THIS_MODULE,
1081 .create = smc_create,
1084 static int __init smc_init(void)
1088 rc = smc_pnet_init();
1092 rc = proto_register(&smc_proto, 1);
1094 pr_err("%s: proto_register fails with %d\n", __func__, rc);
1098 rc = sock_register(&smc_sock_family_ops);
1100 pr_err("%s: sock_register fails with %d\n", __func__, rc);
1104 rc = smc_ib_register_client();
1106 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1113 sock_unregister(PF_SMC);
1115 proto_unregister(&smc_proto);
1121 static void __exit smc_exit(void)
1123 struct smc_link_group *lgr, *lg;
1124 LIST_HEAD(lgr_freeing_list);
1126 spin_lock_bh(&smc_lgr_list.lock);
1127 if (!list_empty(&smc_lgr_list.list))
1128 list_splice_init(&smc_lgr_list.list, &lgr_freeing_list);
1129 spin_unlock_bh(&smc_lgr_list.lock);
1130 list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) {
1131 list_del_init(&lgr->list);
1132 smc_lgr_free(lgr); /* free link group */
1134 smc_ib_unregister_client();
1135 sock_unregister(PF_SMC);
1136 proto_unregister(&smc_proto);
1140 module_init(smc_init);
1141 module_exit(smc_exit);
1143 MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
1144 MODULE_DESCRIPTION("smc socket address family");
1145 MODULE_LICENSE("GPL");
1146 MODULE_ALIAS_NETPROTO(PF_SMC);