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>
43 static DEFINE_MUTEX(smc_create_lgr_pending); /* serialize link group
47 struct smc_lgr_list smc_lgr_list = { /* established link groups */
48 .lock = __SPIN_LOCK_UNLOCKED(smc_lgr_list.lock),
49 .list = LIST_HEAD_INIT(smc_lgr_list.list),
52 static void smc_tcp_listen_work(struct work_struct *);
54 static void smc_set_keepalive(struct sock *sk, int val)
56 struct smc_sock *smc = smc_sk(sk);
58 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
61 static struct proto smc_proto = {
64 .keepalive = smc_set_keepalive,
65 .obj_size = sizeof(struct smc_sock),
66 .slab_flags = SLAB_DESTROY_BY_RCU,
69 static int smc_release(struct socket *sock)
71 struct sock *sk = sock->sk;
80 sk->sk_state = SMC_CLOSED;
82 sock_release(smc->clcsock);
96 static void smc_destruct(struct sock *sk)
98 if (sk->sk_state != SMC_CLOSED)
100 if (!sock_flag(sk, SOCK_DEAD))
103 sk_refcnt_debug_dec(sk);
106 static struct sock *smc_sock_alloc(struct net *net, struct socket *sock)
108 struct smc_sock *smc;
111 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, &smc_proto, 0);
115 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
116 sk->sk_state = SMC_INIT;
117 sk->sk_destruct = smc_destruct;
118 sk->sk_protocol = SMCPROTO_SMC;
120 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
121 INIT_LIST_HEAD(&smc->accept_q);
122 spin_lock_init(&smc->accept_q_lock);
123 sk_refcnt_debug_inc(sk);
128 static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
131 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
132 struct sock *sk = sock->sk;
133 struct smc_sock *smc;
138 /* replicate tests from inet_bind(), to be safe wrt. future changes */
140 if (addr_len < sizeof(struct sockaddr_in))
144 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
145 if ((addr->sin_family != AF_INET) &&
146 ((addr->sin_family != AF_UNSPEC) ||
147 (addr->sin_addr.s_addr != htonl(INADDR_ANY))))
152 /* Check if socket is already active */
154 if (sk->sk_state != SMC_INIT)
157 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
158 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
166 static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
169 /* options we don't get control via setsockopt for */
170 nsk->sk_type = osk->sk_type;
171 nsk->sk_sndbuf = osk->sk_sndbuf;
172 nsk->sk_rcvbuf = osk->sk_rcvbuf;
173 nsk->sk_sndtimeo = osk->sk_sndtimeo;
174 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
175 nsk->sk_mark = osk->sk_mark;
176 nsk->sk_priority = osk->sk_priority;
177 nsk->sk_rcvlowat = osk->sk_rcvlowat;
178 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
179 nsk->sk_err = osk->sk_err;
181 nsk->sk_flags &= ~mask;
182 nsk->sk_flags |= osk->sk_flags & mask;
185 #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
186 (1UL << SOCK_KEEPOPEN) | \
187 (1UL << SOCK_LINGER) | \
188 (1UL << SOCK_BROADCAST) | \
189 (1UL << SOCK_TIMESTAMP) | \
190 (1UL << SOCK_DBG) | \
191 (1UL << SOCK_RCVTSTAMP) | \
192 (1UL << SOCK_RCVTSTAMPNS) | \
193 (1UL << SOCK_LOCALROUTE) | \
194 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
195 (1UL << SOCK_RXQ_OVFL) | \
196 (1UL << SOCK_WIFI_STATUS) | \
197 (1UL << SOCK_NOFCS) | \
198 (1UL << SOCK_FILTER_LOCKED))
199 /* copy only relevant settings and flags of SOL_SOCKET level from smc to
200 * clc socket (since smc is not called for these options from net/core)
202 static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
204 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
207 #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
208 (1UL << SOCK_KEEPOPEN) | \
209 (1UL << SOCK_LINGER) | \
211 /* copy only settings and flags relevant for smc from clc to smc socket */
212 static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
214 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
217 /* determine subnet and mask of internal TCP socket */
218 int smc_netinfo_by_tcpsk(struct socket *clcsock,
219 __be32 *subnet, u8 *prefix_len)
221 struct dst_entry *dst = sk_dst_get(clcsock->sk);
222 struct sockaddr_in addr;
235 /* get address to which the internal TCP socket is bound */
236 kernel_getsockname(clcsock, (struct sockaddr *)&addr, &len);
237 /* analyze IPv4 specific data of net_device belonging to TCP socket */
238 for_ifa(dst->dev->ip_ptr) {
239 if (ifa->ifa_address != addr.sin_addr.s_addr)
241 *prefix_len = inet_mask_len(ifa->ifa_mask);
242 *subnet = ifa->ifa_address & ifa->ifa_mask;
245 } endfor_ifa(dst->dev->ip_ptr);
253 static int smc_clnt_conf_first_link(struct smc_sock *smc, union ib_gid *gid)
255 struct smc_link_group *lgr = smc->conn.lgr;
256 struct smc_link *link;
260 link = &lgr->lnk[SMC_SINGLE_LINK];
261 /* receive CONFIRM LINK request from server over RoCE fabric */
262 rest = wait_for_completion_interruptible_timeout(
264 SMC_LLC_WAIT_FIRST_TIME);
266 struct smc_clc_msg_decline dclc;
268 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
273 rc = smc_ib_modify_qp_rts(link);
275 return SMC_CLC_DECL_INTERR;
277 smc_wr_remember_qp_attr(link);
278 /* send CONFIRM LINK response over RoCE fabric */
279 rc = smc_llc_send_confirm_link(link,
280 link->smcibdev->mac[link->ibport - 1],
283 return SMC_CLC_DECL_TCL;
288 static void smc_conn_save_peer_info(struct smc_sock *smc,
289 struct smc_clc_msg_accept_confirm *clc)
291 smc->conn.peer_conn_idx = clc->conn_idx;
292 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
293 smc->conn.peer_rmbe_size = smc_uncompress_bufsize(clc->rmbe_size);
294 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
297 static void smc_link_save_peer_info(struct smc_link *link,
298 struct smc_clc_msg_accept_confirm *clc)
300 link->peer_qpn = ntoh24(clc->qpn);
301 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
302 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
303 link->peer_psn = ntoh24(clc->psn);
304 link->peer_mtu = clc->qp_mtu;
307 /* setup for RDMA connection of client */
308 static int smc_connect_rdma(struct smc_sock *smc)
310 struct sockaddr_in *inaddr = (struct sockaddr_in *)smc->addr;
311 struct smc_clc_msg_accept_confirm aclc;
312 int local_contact = SMC_FIRST_CONTACT;
313 struct smc_ib_device *smcibdev;
314 struct smc_link *link;
315 u8 srv_first_contact;
320 /* IPSec connections opt out of SMC-R optimizations */
321 if (using_ipsec(smc)) {
322 reason_code = SMC_CLC_DECL_IPSEC;
326 /* PNET table look up: search active ib_device and port
327 * within same PNETID that also contains the ethernet device
328 * used for the internal TCP socket
330 smc_pnet_find_roce_resource(smc->clcsock->sk, &smcibdev, &ibport);
332 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
336 /* do inband token exchange */
337 reason_code = smc_clc_send_proposal(smc, smcibdev, ibport);
338 if (reason_code < 0) {
342 if (reason_code > 0) /* configuration error */
344 /* receive SMC Accept CLC message */
345 reason_code = smc_clc_wait_msg(smc, &aclc, sizeof(aclc),
347 if (reason_code < 0) {
354 srv_first_contact = aclc.hdr.flag;
355 mutex_lock(&smc_create_lgr_pending);
356 local_contact = smc_conn_create(smc, inaddr->sin_addr.s_addr, smcibdev,
357 ibport, &aclc.lcl, srv_first_contact);
358 if (local_contact < 0) {
361 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
362 else if (rc == -ENOLINK)
363 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
364 goto decline_rdma_unlock;
366 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
368 smc_conn_save_peer_info(smc, &aclc);
370 rc = smc_sndbuf_create(smc);
372 reason_code = SMC_CLC_DECL_MEM;
373 goto decline_rdma_unlock;
375 rc = smc_rmb_create(smc);
377 reason_code = SMC_CLC_DECL_MEM;
378 goto decline_rdma_unlock;
381 if (local_contact == SMC_FIRST_CONTACT)
382 smc_link_save_peer_info(link, &aclc);
384 rc = smc_rmb_rtoken_handling(&smc->conn, &aclc);
386 reason_code = SMC_CLC_DECL_INTERR;
387 goto decline_rdma_unlock;
390 if (local_contact == SMC_FIRST_CONTACT) {
391 rc = smc_ib_ready_link(link);
393 reason_code = SMC_CLC_DECL_INTERR;
394 goto decline_rdma_unlock;
398 rc = smc_clc_send_confirm(smc);
402 if (local_contact == SMC_FIRST_CONTACT) {
403 /* QP confirmation over RoCE fabric */
404 reason_code = smc_clnt_conf_first_link(
405 smc, &smcibdev->gid[ibport - 1]);
406 if (reason_code < 0) {
411 goto decline_rdma_unlock;
414 mutex_unlock(&smc_create_lgr_pending);
419 smc_copy_sock_settings_to_clc(smc);
420 smc->sk.sk_state = SMC_ACTIVE;
422 return rc ? rc : local_contact;
425 mutex_unlock(&smc_create_lgr_pending);
426 smc_conn_free(&smc->conn);
428 /* RDMA setup failed, switch back to TCP */
429 smc->use_fallback = true;
430 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
431 rc = smc_clc_send_decline(smc, reason_code, 0);
432 if (rc < sizeof(struct smc_clc_msg_decline))
438 mutex_unlock(&smc_create_lgr_pending);
439 smc_conn_free(&smc->conn);
444 static int smc_connect(struct socket *sock, struct sockaddr *addr,
447 struct sock *sk = sock->sk;
448 struct smc_sock *smc;
453 /* separate smc parameter checking to be safe */
454 if (alen < sizeof(addr->sa_family))
456 if (addr->sa_family != AF_INET)
458 smc->addr = addr; /* needed for nonblocking connect */
461 switch (sk->sk_state) {
472 smc_copy_sock_settings_to_clc(smc);
473 rc = kernel_connect(smc->clcsock, addr, alen, flags);
477 /* setup RDMA connection */
478 rc = smc_connect_rdma(smc);
482 rc = 0; /* success cases including fallback */
490 static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
492 struct sock *sk = &lsmc->sk;
493 struct socket *new_clcsock;
497 release_sock(&lsmc->sk);
498 new_sk = smc_sock_alloc(sock_net(sk), NULL);
501 lsmc->sk.sk_err = ENOMEM;
503 lock_sock(&lsmc->sk);
506 *new_smc = smc_sk(new_sk);
508 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
509 lock_sock(&lsmc->sk);
511 lsmc->sk.sk_err = -rc;
512 new_sk->sk_state = SMC_CLOSED;
513 sock_set_flag(new_sk, SOCK_DEAD);
518 if (lsmc->sk.sk_state == SMC_CLOSED) {
520 sock_release(new_clcsock);
521 new_sk->sk_state = SMC_CLOSED;
522 sock_set_flag(new_sk, SOCK_DEAD);
528 (*new_smc)->clcsock = new_clcsock;
533 /* add a just created sock to the accept queue of the listen sock as
534 * candidate for a following socket accept call from user space
536 static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
538 struct smc_sock *par = smc_sk(parent);
541 spin_lock(&par->accept_q_lock);
542 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
543 spin_unlock(&par->accept_q_lock);
544 sk_acceptq_added(parent);
547 /* remove a socket from the accept queue of its parental listening socket */
548 static void smc_accept_unlink(struct sock *sk)
550 struct smc_sock *par = smc_sk(sk)->listen_smc;
552 spin_lock(&par->accept_q_lock);
553 list_del_init(&smc_sk(sk)->accept_q);
554 spin_unlock(&par->accept_q_lock);
555 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
559 /* remove a sock from the accept queue to bind it to a new socket created
560 * for a socket accept call from user space
562 static struct sock *smc_accept_dequeue(struct sock *parent,
563 struct socket *new_sock)
565 struct smc_sock *isk, *n;
568 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
569 new_sk = (struct sock *)isk;
571 smc_accept_unlink(new_sk);
572 if (new_sk->sk_state == SMC_CLOSED) {
573 /* tbd in follow-on patch: close this sock */
577 sock_graft(new_sk, new_sock);
583 /* clean up for a created but never accepted sock */
584 static void smc_close_non_accepted(struct sock *sk)
586 struct smc_sock *smc = smc_sk(sk);
596 /* more closing stuff to be added with socket closing patch */
600 static int smc_serv_conf_first_link(struct smc_sock *smc)
602 struct smc_link_group *lgr = smc->conn.lgr;
603 struct smc_link *link;
607 link = &lgr->lnk[SMC_SINGLE_LINK];
608 /* send CONFIRM LINK request to client over the RoCE fabric */
609 rc = smc_llc_send_confirm_link(link,
610 link->smcibdev->mac[link->ibport - 1],
611 &link->smcibdev->gid[link->ibport - 1],
614 return SMC_CLC_DECL_TCL;
616 /* receive CONFIRM LINK response from client over the RoCE fabric */
617 rest = wait_for_completion_interruptible_timeout(
618 &link->llc_confirm_resp,
619 SMC_LLC_WAIT_FIRST_TIME);
621 struct smc_clc_msg_decline dclc;
623 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
630 /* setup for RDMA connection of server */
631 static void smc_listen_work(struct work_struct *work)
633 struct smc_sock *new_smc = container_of(work, struct smc_sock,
635 struct socket *newclcsock = new_smc->clcsock;
636 struct smc_sock *lsmc = new_smc->listen_smc;
637 struct smc_clc_msg_accept_confirm cclc;
638 int local_contact = SMC_REUSE_CONTACT;
639 struct sock *newsmcsk = &new_smc->sk;
640 struct smc_clc_msg_proposal pclc;
641 struct smc_ib_device *smcibdev;
642 struct sockaddr_in peeraddr;
643 struct smc_link *link;
650 /* do inband token exchange -
651 *wait for and receive SMC Proposal CLC message
653 reason_code = smc_clc_wait_msg(new_smc, &pclc, sizeof(pclc),
660 /* IPSec connections opt out of SMC-R optimizations */
661 if (using_ipsec(new_smc)) {
662 reason_code = SMC_CLC_DECL_IPSEC;
666 /* PNET table look up: search active ib_device and port
667 * within same PNETID that also contains the ethernet device
668 * used for the internal TCP socket
670 smc_pnet_find_roce_resource(newclcsock->sk, &smcibdev, &ibport);
672 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
676 /* determine subnet and mask from internal TCP socket */
677 rc = smc_netinfo_by_tcpsk(newclcsock, &subnet, &prefix_len);
679 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
682 if ((pclc.outgoing_subnet != subnet) ||
683 (pclc.prefix_len != prefix_len)) {
684 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
688 /* get address of the peer connected to the internal TCP socket */
689 kernel_getpeername(newclcsock, (struct sockaddr *)&peeraddr, &len);
691 /* allocate connection / link group */
692 mutex_lock(&smc_create_lgr_pending);
693 local_contact = smc_conn_create(new_smc, peeraddr.sin_addr.s_addr,
694 smcibdev, ibport, &pclc.lcl, 0);
695 if (local_contact == SMC_REUSE_CONTACT)
696 /* lock no longer needed, free it due to following
697 * smc_clc_wait_msg() call
699 mutex_unlock(&smc_create_lgr_pending);
700 if (local_contact < 0) {
703 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
704 else if (rc == -ENOLINK)
705 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
708 link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
710 rc = smc_sndbuf_create(new_smc);
712 reason_code = SMC_CLC_DECL_MEM;
715 rc = smc_rmb_create(new_smc);
717 reason_code = SMC_CLC_DECL_MEM;
721 rc = smc_clc_send_accept(new_smc, local_contact);
725 /* receive SMC Confirm CLC message */
726 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
732 smc_conn_save_peer_info(new_smc, &cclc);
733 if (local_contact == SMC_FIRST_CONTACT)
734 smc_link_save_peer_info(link, &cclc);
736 rc = smc_rmb_rtoken_handling(&new_smc->conn, &cclc);
738 reason_code = SMC_CLC_DECL_INTERR;
742 if (local_contact == SMC_FIRST_CONTACT) {
743 rc = smc_ib_ready_link(link);
745 reason_code = SMC_CLC_DECL_INTERR;
748 /* QP confirmation over RoCE fabric */
749 reason_code = smc_serv_conf_first_link(new_smc);
750 if (reason_code < 0) {
751 /* peer is not aware of a problem */
759 smc_tx_init(new_smc);
760 smc_rx_init(new_smc);
763 sk_refcnt_debug_inc(newsmcsk);
764 newsmcsk->sk_state = SMC_ACTIVE;
766 if (local_contact == SMC_FIRST_CONTACT)
767 mutex_unlock(&smc_create_lgr_pending);
768 lock_sock(&lsmc->sk);
769 if (lsmc->sk.sk_state == SMC_LISTEN) {
770 smc_accept_enqueue(&lsmc->sk, newsmcsk);
771 } else { /* no longer listening */
772 smc_close_non_accepted(newsmcsk);
774 release_sock(&lsmc->sk);
777 lsmc->sk.sk_data_ready(&lsmc->sk);
778 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
782 /* RDMA setup failed, switch back to TCP */
783 smc_conn_free(&new_smc->conn);
784 new_smc->use_fallback = true;
785 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
786 rc = smc_clc_send_decline(new_smc, reason_code, 0);
787 if (rc < sizeof(struct smc_clc_msg_decline))
793 newsmcsk->sk_state = SMC_CLOSED;
794 goto enqueue; /* queue new sock with sk_err set */
797 static void smc_tcp_listen_work(struct work_struct *work)
799 struct smc_sock *lsmc = container_of(work, struct smc_sock,
801 struct smc_sock *new_smc;
804 lock_sock(&lsmc->sk);
805 while (lsmc->sk.sk_state == SMC_LISTEN) {
806 rc = smc_clcsock_accept(lsmc, &new_smc);
812 new_smc->listen_smc = lsmc;
813 new_smc->use_fallback = false; /* assume rdma capability first*/
814 sock_hold(&lsmc->sk); /* sock_put in smc_listen_work */
815 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
816 smc_copy_sock_settings_to_smc(new_smc);
817 schedule_work(&new_smc->smc_listen_work);
821 release_sock(&lsmc->sk);
822 lsmc->sk.sk_data_ready(&lsmc->sk); /* no more listening, wake accept */
825 static int smc_listen(struct socket *sock, int backlog)
827 struct sock *sk = sock->sk;
828 struct smc_sock *smc;
835 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
839 if (sk->sk_state == SMC_LISTEN) {
840 sk->sk_max_ack_backlog = backlog;
843 /* some socket options are handled in core, so we could not apply
844 * them to the clc socket -- copy smc socket options to clc socket
846 smc_copy_sock_settings_to_clc(smc);
848 rc = kernel_listen(smc->clcsock, backlog);
851 sk->sk_max_ack_backlog = backlog;
852 sk->sk_ack_backlog = 0;
853 sk->sk_state = SMC_LISTEN;
854 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
855 schedule_work(&smc->tcp_listen_work);
862 static int smc_accept(struct socket *sock, struct socket *new_sock,
865 struct sock *sk = sock->sk, *nsk;
866 DECLARE_WAITQUEUE(wait, current);
867 struct smc_sock *lsmc;
874 if (lsmc->sk.sk_state != SMC_LISTEN) {
879 /* Wait for an incoming connection */
880 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
881 add_wait_queue_exclusive(sk_sleep(sk), &wait);
882 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
883 set_current_state(TASK_INTERRUPTIBLE);
889 timeo = schedule_timeout(timeo);
890 /* wakeup by sk_data_ready in smc_listen_work() */
891 sched_annotate_sleep();
893 if (signal_pending(current)) {
894 rc = sock_intr_errno(timeo);
898 set_current_state(TASK_RUNNING);
899 remove_wait_queue(sk_sleep(sk), &wait);
902 rc = sock_error(nsk);
909 static int smc_getname(struct socket *sock, struct sockaddr *addr,
912 struct smc_sock *smc;
914 if (peer && (sock->sk->sk_state != SMC_ACTIVE))
917 smc = smc_sk(sock->sk);
919 return smc->clcsock->ops->getname(smc->clcsock, addr, len, peer);
922 static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
924 struct sock *sk = sock->sk;
925 struct smc_sock *smc;
930 if (sk->sk_state != SMC_ACTIVE)
932 if (smc->use_fallback)
933 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
935 rc = smc_tx_sendmsg(smc, msg, len);
941 static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
944 struct sock *sk = sock->sk;
945 struct smc_sock *smc;
950 if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED))
953 if (smc->use_fallback)
954 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
956 rc = smc_rx_recvmsg(smc, msg, len, flags);
962 static unsigned int smc_accept_poll(struct sock *parent)
964 struct smc_sock *isk;
968 list_for_each_entry(isk, &smc_sk(parent)->accept_q, accept_q) {
969 sk = (struct sock *)isk;
971 if (sk->sk_state == SMC_ACTIVE) {
972 release_sock(parent);
973 return POLLIN | POLLRDNORM;
976 release_sock(parent);
981 static unsigned int smc_poll(struct file *file, struct socket *sock,
984 struct sock *sk = sock->sk;
985 unsigned int mask = 0;
986 struct smc_sock *smc;
989 smc = smc_sk(sock->sk);
990 if ((sk->sk_state == SMC_INIT) || smc->use_fallback) {
991 /* delegate to CLC child sock */
992 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
993 /* if non-blocking connect finished ... */
995 if ((sk->sk_state == SMC_INIT) && (mask & POLLOUT)) {
996 sk->sk_err = smc->clcsock->sk->sk_err;
1000 rc = smc_connect_rdma(smc);
1004 /* success cases including fallback */
1005 mask |= POLLOUT | POLLWRNORM;
1010 sock_poll_wait(file, sk_sleep(sk), wait);
1011 if (sk->sk_state == SMC_LISTEN)
1012 /* woken up by sk_data_ready in smc_listen_work() */
1013 mask |= smc_accept_poll(sk);
1016 if (atomic_read(&smc->conn.sndbuf_space)) {
1017 mask |= POLLOUT | POLLWRNORM;
1019 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1020 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1022 if (atomic_read(&smc->conn.bytes_to_rcv))
1023 mask |= POLLIN | POLLRDNORM;
1024 /* for now - to be enhanced in follow-on patch */
1030 static int smc_shutdown(struct socket *sock, int how)
1032 struct sock *sk = sock->sk;
1033 struct smc_sock *smc;
1038 if ((how < SHUT_RD) || (how > SHUT_RDWR))
1044 if (sk->sk_state == SMC_CLOSED)
1046 if (smc->use_fallback) {
1047 rc = kernel_sock_shutdown(smc->clcsock, how);
1048 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1049 if (sk->sk_shutdown == SHUTDOWN_MASK)
1050 sk->sk_state = SMC_CLOSED;
1052 rc = sock_no_shutdown(sock, how);
1062 static int smc_setsockopt(struct socket *sock, int level, int optname,
1063 char __user *optval, unsigned int optlen)
1065 struct sock *sk = sock->sk;
1066 struct smc_sock *smc;
1070 /* generic setsockopts reaching us here always apply to the
1073 return smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1077 static int smc_getsockopt(struct socket *sock, int level, int optname,
1078 char __user *optval, int __user *optlen)
1080 struct smc_sock *smc;
1082 smc = smc_sk(sock->sk);
1083 /* socket options apply to the CLC socket */
1084 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1088 static int smc_ioctl(struct socket *sock, unsigned int cmd,
1091 struct smc_sock *smc;
1093 smc = smc_sk(sock->sk);
1094 if (smc->use_fallback)
1095 return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1097 return sock_no_ioctl(sock, cmd, arg);
1100 static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1101 int offset, size_t size, int flags)
1103 struct sock *sk = sock->sk;
1104 struct smc_sock *smc;
1109 if (sk->sk_state != SMC_ACTIVE)
1111 if (smc->use_fallback)
1112 rc = kernel_sendpage(smc->clcsock, page, offset,
1115 rc = sock_no_sendpage(sock, page, offset, size, flags);
1122 static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1123 struct pipe_inode_info *pipe, size_t len,
1126 struct sock *sk = sock->sk;
1127 struct smc_sock *smc;
1132 if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED))
1134 if (smc->use_fallback) {
1135 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1145 /* must look like tcp */
1146 static const struct proto_ops smc_sock_ops = {
1148 .owner = THIS_MODULE,
1149 .release = smc_release,
1151 .connect = smc_connect,
1152 .socketpair = sock_no_socketpair,
1153 .accept = smc_accept,
1154 .getname = smc_getname,
1157 .listen = smc_listen,
1158 .shutdown = smc_shutdown,
1159 .setsockopt = smc_setsockopt,
1160 .getsockopt = smc_getsockopt,
1161 .sendmsg = smc_sendmsg,
1162 .recvmsg = smc_recvmsg,
1163 .mmap = sock_no_mmap,
1164 .sendpage = smc_sendpage,
1165 .splice_read = smc_splice_read,
1168 static int smc_create(struct net *net, struct socket *sock, int protocol,
1171 struct smc_sock *smc;
1175 rc = -ESOCKTNOSUPPORT;
1176 if (sock->type != SOCK_STREAM)
1179 rc = -EPROTONOSUPPORT;
1180 if ((protocol != IPPROTO_IP) && (protocol != IPPROTO_TCP))
1184 sock->ops = &smc_sock_ops;
1185 sk = smc_sock_alloc(net, sock);
1189 /* create internal TCP socket for CLC handshake and fallback */
1191 smc->use_fallback = false; /* assume rdma capability first */
1192 rc = sock_create_kern(net, PF_INET, SOCK_STREAM,
1193 IPPROTO_TCP, &smc->clcsock);
1195 sk_common_release(sk);
1196 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1197 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
1203 static const struct net_proto_family smc_sock_family_ops = {
1205 .owner = THIS_MODULE,
1206 .create = smc_create,
1209 static int __init smc_init(void)
1213 rc = smc_pnet_init();
1217 rc = smc_llc_init();
1219 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1223 rc = smc_cdc_init();
1225 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1229 rc = proto_register(&smc_proto, 1);
1231 pr_err("%s: proto_register fails with %d\n", __func__, rc);
1235 rc = sock_register(&smc_sock_family_ops);
1237 pr_err("%s: sock_register fails with %d\n", __func__, rc);
1241 rc = smc_ib_register_client();
1243 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1250 sock_unregister(PF_SMC);
1252 proto_unregister(&smc_proto);
1258 static void __exit smc_exit(void)
1260 struct smc_link_group *lgr, *lg;
1261 LIST_HEAD(lgr_freeing_list);
1263 spin_lock_bh(&smc_lgr_list.lock);
1264 if (!list_empty(&smc_lgr_list.list))
1265 list_splice_init(&smc_lgr_list.list, &lgr_freeing_list);
1266 spin_unlock_bh(&smc_lgr_list.lock);
1267 list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) {
1268 list_del_init(&lgr->list);
1269 smc_lgr_free(lgr); /* free link group */
1271 smc_ib_unregister_client();
1272 sock_unregister(PF_SMC);
1273 proto_unregister(&smc_proto);
1277 module_init(smc_init);
1278 module_exit(smc_exit);
1280 MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
1281 MODULE_DESCRIPTION("smc socket address family");
1282 MODULE_LICENSE("GPL");
1283 MODULE_ALIAS_NETPROTO(PF_SMC);