1 // SPDX-License-Identifier: GPL-2.0-only
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
5 * AF_SMC protocol family socket handler keeping the AF_INET sock address type
6 * applies to SOCK_STREAM sockets only
7 * offers an alternative communication option for TCP-protocol sockets
8 * applicable with RoCE-cards only
10 * Initial restrictions:
11 * - support for alternate links postponed
13 * Copyright IBM Corp. 2016, 2018
15 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
16 * based on prototype from Frank Blaschka
19 #define KMSG_COMPONENT "smc"
20 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
22 #include <linux/module.h>
23 #include <linux/socket.h>
24 #include <linux/workqueue.h>
26 #include <linux/sched/signal.h>
27 #include <linux/if_vlan.h>
28 #include <linux/rcupdate_wait.h>
29 #include <linux/ctype.h>
34 #include <asm/ioctls.h>
36 #include <net/net_namespace.h>
37 #include <net/netns/generic.h>
38 #include "smc_netns.h"
50 #include "smc_close.h"
52 static DEFINE_MUTEX(smc_server_lgr_pending); /* serialize link group
55 static DEFINE_MUTEX(smc_client_lgr_pending); /* serialize link group
59 struct workqueue_struct *smc_hs_wq; /* wq for handshake work */
60 struct workqueue_struct *smc_close_wq; /* wq for close work */
62 static void smc_tcp_listen_work(struct work_struct *);
63 static void smc_connect_work(struct work_struct *);
65 static void smc_set_keepalive(struct sock *sk, int val)
67 struct smc_sock *smc = smc_sk(sk);
69 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
72 static struct smc_hashinfo smc_v4_hashinfo = {
73 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
76 static struct smc_hashinfo smc_v6_hashinfo = {
77 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
80 int smc_hash_sk(struct sock *sk)
82 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
83 struct hlist_head *head;
87 write_lock_bh(&h->lock);
88 sk_add_node(sk, head);
89 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
90 write_unlock_bh(&h->lock);
94 EXPORT_SYMBOL_GPL(smc_hash_sk);
96 void smc_unhash_sk(struct sock *sk)
98 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
100 write_lock_bh(&h->lock);
101 if (sk_del_node_init(sk))
102 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
103 write_unlock_bh(&h->lock);
105 EXPORT_SYMBOL_GPL(smc_unhash_sk);
107 struct proto smc_proto = {
109 .owner = THIS_MODULE,
110 .keepalive = smc_set_keepalive,
112 .unhash = smc_unhash_sk,
113 .obj_size = sizeof(struct smc_sock),
114 .h.smc_hash = &smc_v4_hashinfo,
115 .slab_flags = SLAB_TYPESAFE_BY_RCU,
117 EXPORT_SYMBOL_GPL(smc_proto);
119 struct proto smc_proto6 = {
121 .owner = THIS_MODULE,
122 .keepalive = smc_set_keepalive,
124 .unhash = smc_unhash_sk,
125 .obj_size = sizeof(struct smc_sock),
126 .h.smc_hash = &smc_v6_hashinfo,
127 .slab_flags = SLAB_TYPESAFE_BY_RCU,
129 EXPORT_SYMBOL_GPL(smc_proto6);
131 static void smc_restore_fallback_changes(struct smc_sock *smc)
133 if (smc->clcsock->file) { /* non-accepted sockets have no file yet */
134 smc->clcsock->file->private_data = smc->sk.sk_socket;
135 smc->clcsock->file = NULL;
139 static int __smc_release(struct smc_sock *smc)
141 struct sock *sk = &smc->sk;
144 if (!smc->use_fallback) {
145 rc = smc_close_active(smc);
146 sock_set_flag(sk, SOCK_DEAD);
147 sk->sk_shutdown |= SHUTDOWN_MASK;
149 if (sk->sk_state != SMC_LISTEN && sk->sk_state != SMC_INIT)
150 sock_put(sk); /* passive closing */
151 if (sk->sk_state == SMC_LISTEN) {
152 /* wake up clcsock accept */
153 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
155 sk->sk_state = SMC_CLOSED;
156 sk->sk_state_change(sk);
157 smc_restore_fallback_changes(smc);
160 sk->sk_prot->unhash(sk);
162 if (sk->sk_state == SMC_CLOSED) {
165 smc_clcsock_release(smc);
168 if (!smc->use_fallback)
169 smc_conn_free(&smc->conn);
175 static int smc_release(struct socket *sock)
177 struct sock *sk = sock->sk;
178 struct smc_sock *smc;
184 sock_hold(sk); /* sock_put below */
187 /* cleanup for a dangling non-blocking connect */
188 if (smc->connect_nonblock && sk->sk_state == SMC_INIT)
189 tcp_abort(smc->clcsock->sk, ECONNABORTED);
190 flush_work(&smc->connect_work);
192 if (sk->sk_state == SMC_LISTEN)
193 /* smc_close_non_accepted() is called and acquires
194 * sock lock for child sockets again
196 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
200 rc = __smc_release(smc);
207 sock_put(sk); /* sock_hold above */
208 sock_put(sk); /* final sock_put */
213 static void smc_destruct(struct sock *sk)
215 if (sk->sk_state != SMC_CLOSED)
217 if (!sock_flag(sk, SOCK_DEAD))
220 sk_refcnt_debug_dec(sk);
223 static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
226 struct smc_sock *smc;
230 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
231 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
235 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
236 sk->sk_state = SMC_INIT;
237 sk->sk_destruct = smc_destruct;
238 sk->sk_protocol = protocol;
240 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
241 INIT_WORK(&smc->connect_work, smc_connect_work);
242 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
243 INIT_LIST_HEAD(&smc->accept_q);
244 spin_lock_init(&smc->accept_q_lock);
245 spin_lock_init(&smc->conn.send_lock);
246 sk->sk_prot->hash(sk);
247 sk_refcnt_debug_inc(sk);
248 mutex_init(&smc->clcsock_release_lock);
253 static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
256 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
257 struct sock *sk = sock->sk;
258 struct smc_sock *smc;
263 /* replicate tests from inet_bind(), to be safe wrt. future changes */
265 if (addr_len < sizeof(struct sockaddr_in))
269 if (addr->sin_family != AF_INET &&
270 addr->sin_family != AF_INET6 &&
271 addr->sin_family != AF_UNSPEC)
273 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
274 if (addr->sin_family == AF_UNSPEC &&
275 addr->sin_addr.s_addr != htonl(INADDR_ANY))
280 /* Check if socket is already active */
282 if (sk->sk_state != SMC_INIT || smc->connect_nonblock)
285 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
286 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
294 static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
297 /* options we don't get control via setsockopt for */
298 nsk->sk_type = osk->sk_type;
299 nsk->sk_sndbuf = osk->sk_sndbuf;
300 nsk->sk_rcvbuf = osk->sk_rcvbuf;
301 nsk->sk_sndtimeo = osk->sk_sndtimeo;
302 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
303 nsk->sk_mark = osk->sk_mark;
304 nsk->sk_priority = osk->sk_priority;
305 nsk->sk_rcvlowat = osk->sk_rcvlowat;
306 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
307 nsk->sk_err = osk->sk_err;
309 nsk->sk_flags &= ~mask;
310 nsk->sk_flags |= osk->sk_flags & mask;
313 #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
314 (1UL << SOCK_KEEPOPEN) | \
315 (1UL << SOCK_LINGER) | \
316 (1UL << SOCK_BROADCAST) | \
317 (1UL << SOCK_TIMESTAMP) | \
318 (1UL << SOCK_DBG) | \
319 (1UL << SOCK_RCVTSTAMP) | \
320 (1UL << SOCK_RCVTSTAMPNS) | \
321 (1UL << SOCK_LOCALROUTE) | \
322 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
323 (1UL << SOCK_RXQ_OVFL) | \
324 (1UL << SOCK_WIFI_STATUS) | \
325 (1UL << SOCK_NOFCS) | \
326 (1UL << SOCK_FILTER_LOCKED) | \
327 (1UL << SOCK_TSTAMP_NEW))
328 /* copy only relevant settings and flags of SOL_SOCKET level from smc to
329 * clc socket (since smc is not called for these options from net/core)
331 static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
333 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
336 #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
337 (1UL << SOCK_KEEPOPEN) | \
338 (1UL << SOCK_LINGER) | \
340 /* copy only settings and flags relevant for smc from clc to smc socket */
341 static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
343 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
346 /* register the new rmb on all links */
347 static int smcr_lgr_reg_rmbs(struct smc_link *link,
348 struct smc_buf_desc *rmb_desc)
350 struct smc_link_group *lgr = link->lgr;
353 rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY);
356 /* protect against parallel smc_llc_cli_rkey_exchange() and
357 * parallel smcr_link_reg_rmb()
359 mutex_lock(&lgr->llc_conf_mutex);
360 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
361 if (!smc_link_active(&lgr->lnk[i]))
363 rc = smcr_link_reg_rmb(&lgr->lnk[i], rmb_desc);
368 /* exchange confirm_rkey msg with peer */
369 rc = smc_llc_do_confirm_rkey(link, rmb_desc);
374 rmb_desc->is_conf_rkey = true;
376 mutex_unlock(&lgr->llc_conf_mutex);
377 smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
381 static int smcr_clnt_conf_first_link(struct smc_sock *smc)
383 struct smc_link *link = smc->conn.lnk;
384 struct smc_llc_qentry *qentry;
387 /* receive CONFIRM LINK request from server over RoCE fabric */
388 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
389 SMC_LLC_CONFIRM_LINK);
391 struct smc_clc_msg_decline dclc;
393 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
394 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
395 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
397 smc_llc_save_peer_uid(qentry);
398 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_REQ);
399 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
401 return SMC_CLC_DECL_RMBE_EC;
403 rc = smc_ib_modify_qp_rts(link);
405 return SMC_CLC_DECL_ERR_RDYLNK;
407 smc_wr_remember_qp_attr(link);
409 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
410 return SMC_CLC_DECL_ERR_REGRMB;
412 /* confirm_rkey is implicit on 1st contact */
413 smc->conn.rmb_desc->is_conf_rkey = true;
415 /* send CONFIRM LINK response over RoCE fabric */
416 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
418 return SMC_CLC_DECL_TIMEOUT_CL;
420 smc_llc_link_active(link);
421 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
423 /* optional 2nd link, receive ADD LINK request from server */
424 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
427 struct smc_clc_msg_decline dclc;
429 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
430 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
432 rc = 0; /* no DECLINE received, go with one link */
435 smc_llc_flow_qentry_clr(&link->lgr->llc_flow_lcl);
436 smc_llc_cli_add_link(link, qentry);
440 static void smcr_conn_save_peer_info(struct smc_sock *smc,
441 struct smc_clc_msg_accept_confirm *clc)
443 int bufsize = smc_uncompress_bufsize(clc->r0.rmbe_size);
445 smc->conn.peer_rmbe_idx = clc->r0.rmbe_idx;
446 smc->conn.local_tx_ctrl.token = ntohl(clc->r0.rmbe_alert_token);
447 smc->conn.peer_rmbe_size = bufsize;
448 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
449 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
452 static bool smc_isascii(char *hostname)
456 for (i = 0; i < SMC_MAX_HOSTNAME_LEN; i++)
457 if (!isascii(hostname[i]))
462 static void smcd_conn_save_peer_info(struct smc_sock *smc,
463 struct smc_clc_msg_accept_confirm *clc)
465 int bufsize = smc_uncompress_bufsize(clc->d0.dmbe_size);
467 smc->conn.peer_rmbe_idx = clc->d0.dmbe_idx;
468 smc->conn.peer_token = clc->d0.token;
469 /* msg header takes up space in the buffer */
470 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
471 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
472 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
473 if (clc->hdr.version > SMC_V1 &&
474 (clc->hdr.typev2 & SMC_FIRST_CONTACT_MASK)) {
475 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
476 (struct smc_clc_msg_accept_confirm_v2 *)clc;
477 struct smc_clc_first_contact_ext *fce =
478 (struct smc_clc_first_contact_ext *)
479 (((u8 *)clc_v2) + sizeof(*clc_v2));
481 memcpy(smc->conn.lgr->negotiated_eid, clc_v2->eid,
483 smc->conn.lgr->peer_os = fce->os_type;
484 smc->conn.lgr->peer_smc_release = fce->release;
485 if (smc_isascii(fce->hostname))
486 memcpy(smc->conn.lgr->peer_hostname, fce->hostname,
487 SMC_MAX_HOSTNAME_LEN);
491 static void smc_conn_save_peer_info(struct smc_sock *smc,
492 struct smc_clc_msg_accept_confirm *clc)
494 if (smc->conn.lgr->is_smcd)
495 smcd_conn_save_peer_info(smc, clc);
497 smcr_conn_save_peer_info(smc, clc);
500 static void smc_link_save_peer_info(struct smc_link *link,
501 struct smc_clc_msg_accept_confirm *clc)
503 link->peer_qpn = ntoh24(clc->r0.qpn);
504 memcpy(link->peer_gid, clc->r0.lcl.gid, SMC_GID_SIZE);
505 memcpy(link->peer_mac, clc->r0.lcl.mac, sizeof(link->peer_mac));
506 link->peer_psn = ntoh24(clc->r0.psn);
507 link->peer_mtu = clc->r0.qp_mtu;
510 static void smc_switch_to_fallback(struct smc_sock *smc)
512 smc->use_fallback = true;
513 if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
514 smc->clcsock->file = smc->sk.sk_socket->file;
515 smc->clcsock->file->private_data = smc->clcsock;
516 smc->clcsock->wq.fasync_list =
517 smc->sk.sk_socket->wq.fasync_list;
521 /* fall back during connect */
522 static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
524 smc_switch_to_fallback(smc);
525 smc->fallback_rsn = reason_code;
526 smc_copy_sock_settings_to_clc(smc);
527 smc->connect_nonblock = 0;
528 if (smc->sk.sk_state == SMC_INIT)
529 smc->sk.sk_state = SMC_ACTIVE;
533 /* decline and fall back during connect */
534 static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code,
539 if (reason_code < 0) { /* error, fallback is not possible */
540 if (smc->sk.sk_state == SMC_INIT)
541 sock_put(&smc->sk); /* passive closing */
544 if (reason_code != SMC_CLC_DECL_PEERDECL) {
545 rc = smc_clc_send_decline(smc, reason_code, version);
547 if (smc->sk.sk_state == SMC_INIT)
548 sock_put(&smc->sk); /* passive closing */
552 return smc_connect_fallback(smc, reason_code);
555 /* abort connecting */
556 static void smc_connect_abort(struct smc_sock *smc, int local_first)
559 smc_lgr_cleanup_early(&smc->conn);
561 smc_conn_free(&smc->conn);
564 /* check if there is a rdma device available for this connection. */
565 /* called for connect and listen */
566 static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
568 /* PNET table look up: search active ib_device and port
569 * within same PNETID that also contains the ethernet device
570 * used for the internal TCP socket
572 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
574 return SMC_CLC_DECL_NOSMCRDEV;
578 /* check if there is an ISM device available for this connection. */
579 /* called for connect and listen */
580 static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
582 /* Find ISM device with same PNETID as connecting interface */
583 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
584 if (!ini->ism_dev[0])
585 return SMC_CLC_DECL_NOSMCDDEV;
587 ini->ism_chid[0] = smc_ism_get_chid(ini->ism_dev[0]);
591 /* is chid unique for the ism devices that are already determined? */
592 static bool smc_find_ism_v2_is_unique_chid(u16 chid, struct smc_init_info *ini,
595 int i = (!ini->ism_dev[0]) ? 1 : 0;
598 if (ini->ism_chid[i] == chid)
603 /* determine possible V2 ISM devices (either without PNETID or with PNETID plus
604 * PNETID matching net_device)
606 static int smc_find_ism_v2_device_clnt(struct smc_sock *smc,
607 struct smc_init_info *ini)
609 int rc = SMC_CLC_DECL_NOSMCDDEV;
610 struct smcd_dev *smcd;
614 if (smcd_indicated(ini->smc_type_v1))
615 rc = 0; /* already initialized for V1 */
616 mutex_lock(&smcd_dev_list.mutex);
617 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
618 if (smcd->going_away || smcd == ini->ism_dev[0])
620 chid = smc_ism_get_chid(smcd);
621 if (!smc_find_ism_v2_is_unique_chid(chid, ini, i))
623 if (!smc_pnet_is_pnetid_set(smcd->pnetid) ||
624 smc_pnet_is_ndev_pnetid(sock_net(&smc->sk), smcd->pnetid)) {
625 ini->ism_dev[i] = smcd;
626 ini->ism_chid[i] = chid;
630 if (i > SMC_MAX_ISM_DEVS)
634 mutex_unlock(&smcd_dev_list.mutex);
635 ini->ism_offered_cnt = i - 1;
636 if (!ini->ism_dev[0] && !ini->ism_dev[1])
637 ini->smcd_version = 0;
642 /* Check for VLAN ID and register it on ISM device just for CLC handshake */
643 static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
644 struct smc_init_info *ini)
646 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev[0], ini->vlan_id))
647 return SMC_CLC_DECL_ISMVLANERR;
651 static int smc_find_proposal_devices(struct smc_sock *smc,
652 struct smc_init_info *ini)
656 /* check if there is an ism device available */
657 if (ini->smcd_version & SMC_V1) {
658 if (smc_find_ism_device(smc, ini) ||
659 smc_connect_ism_vlan_setup(smc, ini)) {
660 if (ini->smc_type_v1 == SMC_TYPE_B)
661 ini->smc_type_v1 = SMC_TYPE_R;
663 ini->smc_type_v1 = SMC_TYPE_N;
664 } /* else ISM V1 is supported for this connection */
665 if (smc_find_rdma_device(smc, ini)) {
666 if (ini->smc_type_v1 == SMC_TYPE_B)
667 ini->smc_type_v1 = SMC_TYPE_D;
669 ini->smc_type_v1 = SMC_TYPE_N;
670 } /* else RDMA is supported for this connection */
672 if (smc_ism_v2_capable && smc_find_ism_v2_device_clnt(smc, ini))
673 ini->smc_type_v2 = SMC_TYPE_N;
675 /* if neither ISM nor RDMA are supported, fallback */
676 if (!smcr_indicated(ini->smc_type_v1) &&
677 ini->smc_type_v1 == SMC_TYPE_N && ini->smc_type_v2 == SMC_TYPE_N)
678 rc = SMC_CLC_DECL_NOSMCDEV;
683 /* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
684 * used, the VLAN ID will be registered again during the connection setup.
686 static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc,
687 struct smc_init_info *ini)
689 if (!smcd_indicated(ini->smc_type_v1))
691 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev[0], ini->vlan_id))
692 return SMC_CLC_DECL_CNFERR;
696 #define SMC_CLC_MAX_ACCEPT_LEN \
697 (sizeof(struct smc_clc_msg_accept_confirm_v2) + \
698 sizeof(struct smc_clc_first_contact_ext) + \
699 sizeof(struct smc_clc_msg_trail))
701 /* CLC handshake during connect */
702 static int smc_connect_clc(struct smc_sock *smc,
703 struct smc_clc_msg_accept_confirm_v2 *aclc2,
704 struct smc_init_info *ini)
708 /* do inband token exchange */
709 rc = smc_clc_send_proposal(smc, ini);
712 /* receive SMC Accept CLC message */
713 return smc_clc_wait_msg(smc, aclc2, SMC_CLC_MAX_ACCEPT_LEN,
714 SMC_CLC_ACCEPT, CLC_WAIT_TIME);
717 /* setup for RDMA connection of client */
718 static int smc_connect_rdma(struct smc_sock *smc,
719 struct smc_clc_msg_accept_confirm *aclc,
720 struct smc_init_info *ini)
722 int i, reason_code = 0;
723 struct smc_link *link;
725 ini->is_smcd = false;
726 ini->ib_lcl = &aclc->r0.lcl;
727 ini->ib_clcqpn = ntoh24(aclc->r0.qpn);
728 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
730 mutex_lock(&smc_client_lgr_pending);
731 reason_code = smc_conn_create(smc, ini);
733 mutex_unlock(&smc_client_lgr_pending);
737 smc_conn_save_peer_info(smc, aclc);
739 if (ini->first_contact_local) {
740 link = smc->conn.lnk;
742 /* set link that was assigned by server */
744 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
745 struct smc_link *l = &smc->conn.lgr->lnk[i];
747 if (l->peer_qpn == ntoh24(aclc->r0.qpn) &&
748 !memcmp(l->peer_gid, &aclc->r0.lcl.gid,
750 !memcmp(l->peer_mac, &aclc->r0.lcl.mac,
751 sizeof(l->peer_mac))) {
757 reason_code = SMC_CLC_DECL_NOSRVLINK;
760 smc->conn.lnk = link;
763 /* create send buffer and rmb */
764 if (smc_buf_create(smc, false)) {
765 reason_code = SMC_CLC_DECL_MEM;
769 if (ini->first_contact_local)
770 smc_link_save_peer_info(link, aclc);
772 if (smc_rmb_rtoken_handling(&smc->conn, link, aclc)) {
773 reason_code = SMC_CLC_DECL_ERR_RTOK;
780 if (ini->first_contact_local) {
781 if (smc_ib_ready_link(link)) {
782 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
786 if (smcr_lgr_reg_rmbs(link, smc->conn.rmb_desc)) {
787 reason_code = SMC_CLC_DECL_ERR_REGRMB;
791 smc_rmb_sync_sg_for_device(&smc->conn);
793 reason_code = smc_clc_send_confirm(smc, ini->first_contact_local,
800 if (ini->first_contact_local) {
801 /* QP confirmation over RoCE fabric */
802 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
803 reason_code = smcr_clnt_conf_first_link(smc);
804 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
808 mutex_unlock(&smc_client_lgr_pending);
810 smc_copy_sock_settings_to_clc(smc);
811 smc->connect_nonblock = 0;
812 if (smc->sk.sk_state == SMC_INIT)
813 smc->sk.sk_state = SMC_ACTIVE;
817 smc_connect_abort(smc, ini->first_contact_local);
818 mutex_unlock(&smc_client_lgr_pending);
819 smc->connect_nonblock = 0;
824 /* The server has chosen one of the proposed ISM devices for the communication.
825 * Determine from the CHID of the received CLC ACCEPT the ISM device chosen.
828 smc_v2_determine_accepted_chid(struct smc_clc_msg_accept_confirm_v2 *aclc,
829 struct smc_init_info *ini)
833 for (i = 0; i < ini->ism_offered_cnt + 1; i++) {
834 if (ini->ism_chid[i] == ntohs(aclc->chid)) {
835 ini->ism_selected = i;
843 /* setup for ISM connection of client */
844 static int smc_connect_ism(struct smc_sock *smc,
845 struct smc_clc_msg_accept_confirm *aclc,
846 struct smc_init_info *ini)
851 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
853 if (aclc->hdr.version == SMC_V2) {
854 struct smc_clc_msg_accept_confirm_v2 *aclc_v2 =
855 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
857 rc = smc_v2_determine_accepted_chid(aclc_v2, ini);
861 ini->ism_peer_gid[ini->ism_selected] = aclc->d0.gid;
863 /* there is only one lgr role for SMC-D; use server lock */
864 mutex_lock(&smc_server_lgr_pending);
865 rc = smc_conn_create(smc, ini);
867 mutex_unlock(&smc_server_lgr_pending);
871 /* Create send and receive buffers */
872 rc = smc_buf_create(smc, true);
874 rc = (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB : SMC_CLC_DECL_MEM;
878 smc_conn_save_peer_info(smc, aclc);
883 rc = smc_clc_send_confirm(smc, ini->first_contact_local,
887 mutex_unlock(&smc_server_lgr_pending);
889 smc_copy_sock_settings_to_clc(smc);
890 smc->connect_nonblock = 0;
891 if (smc->sk.sk_state == SMC_INIT)
892 smc->sk.sk_state = SMC_ACTIVE;
896 smc_connect_abort(smc, ini->first_contact_local);
897 mutex_unlock(&smc_server_lgr_pending);
898 smc->connect_nonblock = 0;
903 /* check if received accept type and version matches a proposed one */
904 static int smc_connect_check_aclc(struct smc_init_info *ini,
905 struct smc_clc_msg_accept_confirm *aclc)
907 if ((aclc->hdr.typev1 == SMC_TYPE_R &&
908 !smcr_indicated(ini->smc_type_v1)) ||
909 (aclc->hdr.typev1 == SMC_TYPE_D &&
910 ((!smcd_indicated(ini->smc_type_v1) &&
911 !smcd_indicated(ini->smc_type_v2)) ||
912 (aclc->hdr.version == SMC_V1 &&
913 !smcd_indicated(ini->smc_type_v1)) ||
914 (aclc->hdr.version == SMC_V2 &&
915 !smcd_indicated(ini->smc_type_v2)))))
916 return SMC_CLC_DECL_MODEUNSUPP;
921 /* perform steps before actually connecting */
922 static int __smc_connect(struct smc_sock *smc)
924 u8 version = smc_ism_v2_capable ? SMC_V2 : SMC_V1;
925 struct smc_clc_msg_accept_confirm_v2 *aclc2;
926 struct smc_clc_msg_accept_confirm *aclc;
927 struct smc_init_info *ini = NULL;
931 if (smc->use_fallback)
932 return smc_connect_fallback(smc, smc->fallback_rsn);
934 /* if peer has not signalled SMC-capability, fall back */
935 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
936 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
938 /* IPSec connections opt out of SMC optimizations */
939 if (using_ipsec(smc))
940 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC,
943 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
945 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_MEM,
948 ini->smcd_version = SMC_V1;
949 ini->smcd_version |= smc_ism_v2_capable ? SMC_V2 : 0;
950 ini->smc_type_v1 = SMC_TYPE_B;
951 ini->smc_type_v2 = smc_ism_v2_capable ? SMC_TYPE_D : SMC_TYPE_N;
953 /* get vlan id from IP device */
954 if (smc_vlan_by_tcpsk(smc->clcsock, ini)) {
955 ini->smcd_version &= ~SMC_V1;
956 ini->smc_type_v1 = SMC_TYPE_N;
957 if (!ini->smcd_version) {
958 rc = SMC_CLC_DECL_GETVLANERR;
963 rc = smc_find_proposal_devices(smc, ini);
967 buf = kzalloc(SMC_CLC_MAX_ACCEPT_LEN, GFP_KERNEL);
969 rc = SMC_CLC_DECL_MEM;
972 aclc2 = (struct smc_clc_msg_accept_confirm_v2 *)buf;
973 aclc = (struct smc_clc_msg_accept_confirm *)aclc2;
975 /* perform CLC handshake */
976 rc = smc_connect_clc(smc, aclc2, ini);
980 /* check if smc modes and versions of CLC proposal and accept match */
981 rc = smc_connect_check_aclc(ini, aclc);
982 version = aclc->hdr.version == SMC_V1 ? SMC_V1 : version;
986 /* depending on previous steps, connect using rdma or ism */
987 if (aclc->hdr.typev1 == SMC_TYPE_R)
988 rc = smc_connect_rdma(smc, aclc, ini);
989 else if (aclc->hdr.typev1 == SMC_TYPE_D)
990 rc = smc_connect_ism(smc, aclc, ini);
994 smc_connect_ism_vlan_cleanup(smc, ini);
1000 smc_connect_ism_vlan_cleanup(smc, ini);
1004 return smc_connect_decline_fallback(smc, rc, version);
1007 static void smc_connect_work(struct work_struct *work)
1009 struct smc_sock *smc = container_of(work, struct smc_sock,
1011 long timeo = smc->sk.sk_sndtimeo;
1015 timeo = MAX_SCHEDULE_TIMEOUT;
1016 lock_sock(smc->clcsock->sk);
1017 if (smc->clcsock->sk->sk_err) {
1018 smc->sk.sk_err = smc->clcsock->sk->sk_err;
1019 } else if ((1 << smc->clcsock->sk->sk_state) &
1020 (TCPF_SYN_SENT | TCP_SYN_RECV)) {
1021 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
1022 if ((rc == -EPIPE) &&
1023 ((1 << smc->clcsock->sk->sk_state) &
1024 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
1027 release_sock(smc->clcsock->sk);
1028 lock_sock(&smc->sk);
1029 if (rc != 0 || smc->sk.sk_err) {
1030 smc->sk.sk_state = SMC_CLOSED;
1031 if (rc == -EPIPE || rc == -EAGAIN)
1032 smc->sk.sk_err = EPIPE;
1033 else if (signal_pending(current))
1034 smc->sk.sk_err = -sock_intr_errno(timeo);
1035 sock_put(&smc->sk); /* passive closing */
1039 rc = __smc_connect(smc);
1041 smc->sk.sk_err = -rc;
1044 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
1045 if (smc->sk.sk_err) {
1046 smc->sk.sk_state_change(&smc->sk);
1047 } else { /* allow polling before and after fallback decision */
1048 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
1049 smc->sk.sk_write_space(&smc->sk);
1052 release_sock(&smc->sk);
1055 static int smc_connect(struct socket *sock, struct sockaddr *addr,
1056 int alen, int flags)
1058 struct sock *sk = sock->sk;
1059 struct smc_sock *smc;
1064 /* separate smc parameter checking to be safe */
1065 if (alen < sizeof(addr->sa_family))
1067 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
1071 switch (sk->sk_state) {
1082 smc_copy_sock_settings_to_clc(smc);
1083 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
1084 if (smc->connect_nonblock) {
1088 rc = kernel_connect(smc->clcsock, addr, alen, flags);
1089 if (rc && rc != -EINPROGRESS)
1092 sock_hold(&smc->sk); /* sock put in passive closing */
1093 if (smc->use_fallback)
1095 if (flags & O_NONBLOCK) {
1096 if (queue_work(smc_hs_wq, &smc->connect_work))
1097 smc->connect_nonblock = 1;
1100 rc = __smc_connect(smc);
1104 rc = 0; /* success cases including fallback */
1113 static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
1115 struct socket *new_clcsock = NULL;
1116 struct sock *lsk = &lsmc->sk;
1117 struct sock *new_sk;
1121 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
1124 lsk->sk_err = ENOMEM;
1129 *new_smc = smc_sk(new_sk);
1131 mutex_lock(&lsmc->clcsock_release_lock);
1133 rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK);
1134 mutex_unlock(&lsmc->clcsock_release_lock);
1136 if (rc < 0 && rc != -EAGAIN)
1138 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
1139 new_sk->sk_prot->unhash(new_sk);
1141 sock_release(new_clcsock);
1142 new_sk->sk_state = SMC_CLOSED;
1143 sock_set_flag(new_sk, SOCK_DEAD);
1144 sock_put(new_sk); /* final */
1149 /* new clcsock has inherited the smc listen-specific sk_data_ready
1150 * function; switch it back to the original sk_data_ready function
1152 new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready;
1153 (*new_smc)->clcsock = new_clcsock;
1158 /* add a just created sock to the accept queue of the listen sock as
1159 * candidate for a following socket accept call from user space
1161 static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
1163 struct smc_sock *par = smc_sk(parent);
1165 sock_hold(sk); /* sock_put in smc_accept_unlink () */
1166 spin_lock(&par->accept_q_lock);
1167 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
1168 spin_unlock(&par->accept_q_lock);
1169 sk_acceptq_added(parent);
1172 /* remove a socket from the accept queue of its parental listening socket */
1173 static void smc_accept_unlink(struct sock *sk)
1175 struct smc_sock *par = smc_sk(sk)->listen_smc;
1177 spin_lock(&par->accept_q_lock);
1178 list_del_init(&smc_sk(sk)->accept_q);
1179 spin_unlock(&par->accept_q_lock);
1180 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
1181 sock_put(sk); /* sock_hold in smc_accept_enqueue */
1184 /* remove a sock from the accept queue to bind it to a new socket created
1185 * for a socket accept call from user space
1187 struct sock *smc_accept_dequeue(struct sock *parent,
1188 struct socket *new_sock)
1190 struct smc_sock *isk, *n;
1191 struct sock *new_sk;
1193 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
1194 new_sk = (struct sock *)isk;
1196 smc_accept_unlink(new_sk);
1197 if (new_sk->sk_state == SMC_CLOSED) {
1198 new_sk->sk_prot->unhash(new_sk);
1200 sock_release(isk->clcsock);
1201 isk->clcsock = NULL;
1203 sock_put(new_sk); /* final */
1207 sock_graft(new_sk, new_sock);
1208 if (isk->use_fallback) {
1209 smc_sk(new_sk)->clcsock->file = new_sock->file;
1210 isk->clcsock->file->private_data = isk->clcsock;
1218 /* clean up for a created but never accepted sock */
1219 void smc_close_non_accepted(struct sock *sk)
1221 struct smc_sock *smc = smc_sk(sk);
1223 sock_hold(sk); /* sock_put below */
1225 if (!sk->sk_lingertime)
1226 /* wait for peer closing */
1227 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
1230 sock_put(sk); /* sock_hold above */
1231 sock_put(sk); /* final sock_put */
1234 static int smcr_serv_conf_first_link(struct smc_sock *smc)
1236 struct smc_link *link = smc->conn.lnk;
1237 struct smc_llc_qentry *qentry;
1240 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
1241 return SMC_CLC_DECL_ERR_REGRMB;
1243 /* send CONFIRM LINK request to client over the RoCE fabric */
1244 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
1246 return SMC_CLC_DECL_TIMEOUT_CL;
1248 /* receive CONFIRM LINK response from client over the RoCE fabric */
1249 qentry = smc_llc_wait(link->lgr, link, SMC_LLC_WAIT_TIME,
1250 SMC_LLC_CONFIRM_LINK);
1252 struct smc_clc_msg_decline dclc;
1254 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
1255 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
1256 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
1258 smc_llc_save_peer_uid(qentry);
1259 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_RESP);
1260 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
1262 return SMC_CLC_DECL_RMBE_EC;
1264 /* confirm_rkey is implicit on 1st contact */
1265 smc->conn.rmb_desc->is_conf_rkey = true;
1267 smc_llc_link_active(link);
1268 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
1270 /* initial contact - try to establish second link */
1271 smc_llc_srv_add_link(link);
1275 /* listen worker: finish */
1276 static void smc_listen_out(struct smc_sock *new_smc)
1278 struct smc_sock *lsmc = new_smc->listen_smc;
1279 struct sock *newsmcsk = &new_smc->sk;
1281 if (lsmc->sk.sk_state == SMC_LISTEN) {
1282 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
1283 smc_accept_enqueue(&lsmc->sk, newsmcsk);
1284 release_sock(&lsmc->sk);
1285 } else { /* no longer listening */
1286 smc_close_non_accepted(newsmcsk);
1289 /* Wake up accept */
1290 lsmc->sk.sk_data_ready(&lsmc->sk);
1291 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
1294 /* listen worker: finish in state connected */
1295 static void smc_listen_out_connected(struct smc_sock *new_smc)
1297 struct sock *newsmcsk = &new_smc->sk;
1299 sk_refcnt_debug_inc(newsmcsk);
1300 if (newsmcsk->sk_state == SMC_INIT)
1301 newsmcsk->sk_state = SMC_ACTIVE;
1303 smc_listen_out(new_smc);
1306 /* listen worker: finish in error state */
1307 static void smc_listen_out_err(struct smc_sock *new_smc)
1309 struct sock *newsmcsk = &new_smc->sk;
1311 if (newsmcsk->sk_state == SMC_INIT)
1312 sock_put(&new_smc->sk); /* passive closing */
1313 newsmcsk->sk_state = SMC_CLOSED;
1315 smc_listen_out(new_smc);
1318 /* listen worker: decline and fall back if possible */
1319 static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1320 int local_first, u8 version)
1322 /* RDMA setup failed, switch back to TCP */
1324 smc_lgr_cleanup_early(&new_smc->conn);
1326 smc_conn_free(&new_smc->conn);
1327 if (reason_code < 0) { /* error, no fallback possible */
1328 smc_listen_out_err(new_smc);
1331 smc_switch_to_fallback(new_smc);
1332 new_smc->fallback_rsn = reason_code;
1333 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
1334 if (smc_clc_send_decline(new_smc, reason_code, version) < 0) {
1335 smc_listen_out_err(new_smc);
1339 smc_listen_out_connected(new_smc);
1342 /* listen worker: version checking */
1343 static int smc_listen_v2_check(struct smc_sock *new_smc,
1344 struct smc_clc_msg_proposal *pclc,
1345 struct smc_init_info *ini)
1347 struct smc_clc_smcd_v2_extension *pclc_smcd_v2_ext;
1348 struct smc_clc_v2_extension *pclc_v2_ext;
1350 ini->smc_type_v1 = pclc->hdr.typev1;
1351 ini->smc_type_v2 = pclc->hdr.typev2;
1352 ini->smcd_version = ini->smc_type_v1 != SMC_TYPE_N ? SMC_V1 : 0;
1353 if (pclc->hdr.version > SMC_V1)
1354 ini->smcd_version |=
1355 ini->smc_type_v2 != SMC_TYPE_N ? SMC_V2 : 0;
1356 if (!smc_ism_v2_capable) {
1357 ini->smcd_version &= ~SMC_V2;
1360 pclc_v2_ext = smc_get_clc_v2_ext(pclc);
1362 ini->smcd_version &= ~SMC_V2;
1365 pclc_smcd_v2_ext = smc_get_clc_smcd_v2_ext(pclc_v2_ext);
1366 if (!pclc_smcd_v2_ext)
1367 ini->smcd_version &= ~SMC_V2;
1370 if (!ini->smcd_version) {
1371 if (pclc->hdr.typev1 == SMC_TYPE_B ||
1372 pclc->hdr.typev2 == SMC_TYPE_B)
1373 return SMC_CLC_DECL_NOSMCDEV;
1374 if (pclc->hdr.typev1 == SMC_TYPE_D ||
1375 pclc->hdr.typev2 == SMC_TYPE_D)
1376 return SMC_CLC_DECL_NOSMCDDEV;
1377 return SMC_CLC_DECL_NOSMCRDEV;
1383 /* listen worker: check prefixes */
1384 static int smc_listen_prfx_check(struct smc_sock *new_smc,
1385 struct smc_clc_msg_proposal *pclc)
1387 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1388 struct socket *newclcsock = new_smc->clcsock;
1390 if (pclc->hdr.typev1 == SMC_TYPE_N)
1392 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1393 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
1394 return SMC_CLC_DECL_DIFFPREFIX;
1399 /* listen worker: initialize connection and buffers */
1400 static int smc_listen_rdma_init(struct smc_sock *new_smc,
1401 struct smc_init_info *ini)
1405 /* allocate connection / link group */
1406 rc = smc_conn_create(new_smc, ini);
1410 /* create send buffer and rmb */
1411 if (smc_buf_create(new_smc, false))
1412 return SMC_CLC_DECL_MEM;
1417 /* listen worker: initialize connection and buffers for SMC-D */
1418 static int smc_listen_ism_init(struct smc_sock *new_smc,
1419 struct smc_init_info *ini)
1423 rc = smc_conn_create(new_smc, ini);
1427 /* Create send and receive buffers */
1428 rc = smc_buf_create(new_smc, true);
1430 if (ini->first_contact_local)
1431 smc_lgr_cleanup_early(&new_smc->conn);
1433 smc_conn_free(&new_smc->conn);
1434 return (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB :
1441 static bool smc_is_already_selected(struct smcd_dev *smcd,
1442 struct smc_init_info *ini,
1447 for (i = 0; i < matches; i++)
1448 if (smcd == ini->ism_dev[i])
1454 /* check for ISM devices matching proposed ISM devices */
1455 static void smc_check_ism_v2_match(struct smc_init_info *ini,
1456 u16 proposed_chid, u64 proposed_gid,
1457 unsigned int *matches)
1459 struct smcd_dev *smcd;
1461 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
1462 if (smcd->going_away)
1464 if (smc_is_already_selected(smcd, ini, *matches))
1466 if (smc_ism_get_chid(smcd) == proposed_chid &&
1467 !smc_ism_cantalk(proposed_gid, ISM_RESERVED_VLANID, smcd)) {
1468 ini->ism_peer_gid[*matches] = proposed_gid;
1469 ini->ism_dev[*matches] = smcd;
1476 static void smc_find_ism_v2_device_serv(struct smc_sock *new_smc,
1477 struct smc_clc_msg_proposal *pclc,
1478 struct smc_init_info *ini)
1480 struct smc_clc_smcd_v2_extension *smcd_v2_ext;
1481 struct smc_clc_v2_extension *smc_v2_ext;
1482 struct smc_clc_msg_smcd *pclc_smcd;
1483 unsigned int matches = 0;
1488 if (!(ini->smcd_version & SMC_V2) || !smcd_indicated(ini->smc_type_v2))
1491 pclc_smcd = smc_get_clc_msg_smcd(pclc);
1492 smc_v2_ext = smc_get_clc_v2_ext(pclc);
1493 smcd_v2_ext = smc_get_clc_smcd_v2_ext(smc_v2_ext);
1495 !smc_v2_ext->hdr.flag.seid) /* no system EID support for SMCD */
1498 mutex_lock(&smcd_dev_list.mutex);
1499 if (pclc_smcd->ism.chid)
1500 /* check for ISM device matching proposed native ISM device */
1501 smc_check_ism_v2_match(ini, ntohs(pclc_smcd->ism.chid),
1502 ntohll(pclc_smcd->ism.gid), &matches);
1503 for (i = 1; i <= smc_v2_ext->hdr.ism_gid_cnt; i++) {
1504 /* check for ISM devices matching proposed non-native ISM
1507 smc_check_ism_v2_match(ini,
1508 ntohs(smcd_v2_ext->gidchid[i - 1].chid),
1509 ntohll(smcd_v2_ext->gidchid[i - 1].gid),
1512 mutex_unlock(&smcd_dev_list.mutex);
1514 if (ini->ism_dev[0]) {
1515 smc_ism_get_system_eid(ini->ism_dev[0], &eid);
1516 if (memcmp(eid, smcd_v2_ext->system_eid, SMC_MAX_EID_LEN))
1522 /* separate - outside the smcd_dev_list.lock */
1523 smcd_version = ini->smcd_version;
1524 for (i = 0; i < matches; i++) {
1525 ini->smcd_version = SMC_V2;
1526 ini->is_smcd = true;
1527 ini->ism_selected = i;
1528 if (smc_listen_ism_init(new_smc, ini))
1529 /* try next active ISM device */
1531 return; /* matching and usable V2 ISM device found */
1533 /* no V2 ISM device could be initialized */
1534 ini->smcd_version = smcd_version; /* restore original value */
1537 ini->smcd_version &= ~SMC_V2;
1538 ini->ism_dev[0] = NULL;
1539 ini->is_smcd = false;
1542 static void smc_find_ism_v1_device_serv(struct smc_sock *new_smc,
1543 struct smc_clc_msg_proposal *pclc,
1544 struct smc_init_info *ini)
1546 struct smc_clc_msg_smcd *pclc_smcd = smc_get_clc_msg_smcd(pclc);
1548 /* check if ISM V1 is available */
1549 if (!(ini->smcd_version & SMC_V1) || !smcd_indicated(ini->smc_type_v1))
1551 ini->is_smcd = true; /* prepare ISM check */
1552 ini->ism_peer_gid[0] = ntohll(pclc_smcd->ism.gid);
1553 if (smc_find_ism_device(new_smc, ini))
1555 ini->ism_selected = 0;
1556 if (!smc_listen_ism_init(new_smc, ini))
1557 return; /* V1 ISM device found */
1560 ini->ism_dev[0] = NULL;
1561 ini->is_smcd = false;
1564 /* listen worker: register buffers */
1565 static int smc_listen_rdma_reg(struct smc_sock *new_smc, bool local_first)
1567 struct smc_connection *conn = &new_smc->conn;
1570 if (smcr_lgr_reg_rmbs(conn->lnk, conn->rmb_desc))
1571 return SMC_CLC_DECL_ERR_REGRMB;
1573 smc_rmb_sync_sg_for_device(&new_smc->conn);
1578 static int smc_find_rdma_v1_device_serv(struct smc_sock *new_smc,
1579 struct smc_clc_msg_proposal *pclc,
1580 struct smc_init_info *ini)
1584 if (!smcr_indicated(ini->smc_type_v1))
1585 return SMC_CLC_DECL_NOSMCDEV;
1587 /* prepare RDMA check */
1588 ini->ib_lcl = &pclc->lcl;
1589 rc = smc_find_rdma_device(new_smc, ini);
1591 /* no RDMA device found */
1592 if (ini->smc_type_v1 == SMC_TYPE_B)
1593 /* neither ISM nor RDMA device found */
1594 rc = SMC_CLC_DECL_NOSMCDEV;
1597 rc = smc_listen_rdma_init(new_smc, ini);
1600 return smc_listen_rdma_reg(new_smc, ini->first_contact_local);
1603 /* determine the local device matching to proposal */
1604 static int smc_listen_find_device(struct smc_sock *new_smc,
1605 struct smc_clc_msg_proposal *pclc,
1606 struct smc_init_info *ini)
1610 /* check for ISM device matching V2 proposed device */
1611 smc_find_ism_v2_device_serv(new_smc, pclc, ini);
1612 if (ini->ism_dev[0])
1615 if (!(ini->smcd_version & SMC_V1))
1616 return SMC_CLC_DECL_NOSMCDEV;
1618 /* check for matching IP prefix and subnet length */
1619 rc = smc_listen_prfx_check(new_smc, pclc);
1623 /* get vlan id from IP device */
1624 if (smc_vlan_by_tcpsk(new_smc->clcsock, ini))
1625 return SMC_CLC_DECL_GETVLANERR;
1627 /* check for ISM device matching V1 proposed device */
1628 smc_find_ism_v1_device_serv(new_smc, pclc, ini);
1629 if (ini->ism_dev[0])
1632 if (pclc->hdr.typev1 == SMC_TYPE_D)
1633 return SMC_CLC_DECL_NOSMCDDEV; /* skip RDMA and decline */
1635 /* check if RDMA is available */
1636 return smc_find_rdma_v1_device_serv(new_smc, pclc, ini);
1639 /* listen worker: finish RDMA setup */
1640 static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1641 struct smc_clc_msg_accept_confirm *cclc,
1644 struct smc_link *link = new_smc->conn.lnk;
1645 int reason_code = 0;
1648 smc_link_save_peer_info(link, cclc);
1650 if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc))
1651 return SMC_CLC_DECL_ERR_RTOK;
1654 if (smc_ib_ready_link(link))
1655 return SMC_CLC_DECL_ERR_RDYLNK;
1656 /* QP confirmation over RoCE fabric */
1657 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
1658 reason_code = smcr_serv_conf_first_link(new_smc);
1659 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
1664 /* setup for connection of server */
1665 static void smc_listen_work(struct work_struct *work)
1667 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1669 u8 version = smc_ism_v2_capable ? SMC_V2 : SMC_V1;
1670 struct socket *newclcsock = new_smc->clcsock;
1671 struct smc_clc_msg_accept_confirm *cclc;
1672 struct smc_clc_msg_proposal_area *buf;
1673 struct smc_clc_msg_proposal *pclc;
1674 struct smc_init_info *ini = NULL;
1677 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1678 return smc_listen_out_err(new_smc);
1680 if (new_smc->use_fallback) {
1681 smc_listen_out_connected(new_smc);
1685 /* check if peer is smc capable */
1686 if (!tcp_sk(newclcsock->sk)->syn_smc) {
1687 smc_switch_to_fallback(new_smc);
1688 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
1689 smc_listen_out_connected(new_smc);
1693 /* do inband token exchange -
1694 * wait for and receive SMC Proposal CLC message
1696 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
1698 rc = SMC_CLC_DECL_MEM;
1701 pclc = (struct smc_clc_msg_proposal *)buf;
1702 rc = smc_clc_wait_msg(new_smc, pclc, sizeof(*buf),
1703 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
1706 version = pclc->hdr.version == SMC_V1 ? SMC_V1 : version;
1708 /* IPSec connections opt out of SMC optimizations */
1709 if (using_ipsec(new_smc)) {
1710 rc = SMC_CLC_DECL_IPSEC;
1714 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
1716 rc = SMC_CLC_DECL_MEM;
1720 /* initial version checking */
1721 rc = smc_listen_v2_check(new_smc, pclc, ini);
1725 mutex_lock(&smc_server_lgr_pending);
1726 smc_close_init(new_smc);
1727 smc_rx_init(new_smc);
1728 smc_tx_init(new_smc);
1730 /* determine ISM or RoCE device used for connection */
1731 rc = smc_listen_find_device(new_smc, pclc, ini);
1735 /* send SMC Accept CLC message */
1736 rc = smc_clc_send_accept(new_smc, ini->first_contact_local,
1737 ini->smcd_version == SMC_V2 ? SMC_V2 : SMC_V1);
1741 /* SMC-D does not need this lock any more */
1743 mutex_unlock(&smc_server_lgr_pending);
1745 /* receive SMC Confirm CLC message */
1746 memset(buf, 0, sizeof(*buf));
1747 cclc = (struct smc_clc_msg_accept_confirm *)buf;
1748 rc = smc_clc_wait_msg(new_smc, cclc, sizeof(*buf),
1749 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
1757 if (!ini->is_smcd) {
1758 rc = smc_listen_rdma_finish(new_smc, cclc,
1759 ini->first_contact_local);
1762 mutex_unlock(&smc_server_lgr_pending);
1764 smc_conn_save_peer_info(new_smc, cclc);
1765 smc_listen_out_connected(new_smc);
1769 mutex_unlock(&smc_server_lgr_pending);
1771 smc_listen_decline(new_smc, rc, ini ? ini->first_contact_local : 0,
1778 static void smc_tcp_listen_work(struct work_struct *work)
1780 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1782 struct sock *lsk = &lsmc->sk;
1783 struct smc_sock *new_smc;
1787 while (lsk->sk_state == SMC_LISTEN) {
1788 rc = smc_clcsock_accept(lsmc, &new_smc);
1789 if (rc) /* clcsock accept queue empty or error */
1794 new_smc->listen_smc = lsmc;
1795 new_smc->use_fallback = lsmc->use_fallback;
1796 new_smc->fallback_rsn = lsmc->fallback_rsn;
1797 sock_hold(lsk); /* sock_put in smc_listen_work */
1798 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1799 smc_copy_sock_settings_to_smc(new_smc);
1800 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1801 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
1802 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1803 if (!queue_work(smc_hs_wq, &new_smc->smc_listen_work))
1804 sock_put(&new_smc->sk);
1809 sock_put(&lsmc->sk); /* sock_hold in smc_clcsock_data_ready() */
1812 static void smc_clcsock_data_ready(struct sock *listen_clcsock)
1814 struct smc_sock *lsmc;
1816 lsmc = (struct smc_sock *)
1817 ((uintptr_t)listen_clcsock->sk_user_data & ~SK_USER_DATA_NOCOPY);
1820 lsmc->clcsk_data_ready(listen_clcsock);
1821 if (lsmc->sk.sk_state == SMC_LISTEN) {
1822 sock_hold(&lsmc->sk); /* sock_put in smc_tcp_listen_work() */
1823 if (!queue_work(smc_hs_wq, &lsmc->tcp_listen_work))
1824 sock_put(&lsmc->sk);
1828 static int smc_listen(struct socket *sock, int backlog)
1830 struct sock *sk = sock->sk;
1831 struct smc_sock *smc;
1838 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
1839 smc->connect_nonblock)
1843 if (sk->sk_state == SMC_LISTEN) {
1844 sk->sk_max_ack_backlog = backlog;
1847 /* some socket options are handled in core, so we could not apply
1848 * them to the clc socket -- copy smc socket options to clc socket
1850 smc_copy_sock_settings_to_clc(smc);
1851 if (!smc->use_fallback)
1852 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
1854 /* save original sk_data_ready function and establish
1855 * smc-specific sk_data_ready function
1857 smc->clcsk_data_ready = smc->clcsock->sk->sk_data_ready;
1858 smc->clcsock->sk->sk_data_ready = smc_clcsock_data_ready;
1859 smc->clcsock->sk->sk_user_data =
1860 (void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY);
1861 rc = kernel_listen(smc->clcsock, backlog);
1864 sk->sk_max_ack_backlog = backlog;
1865 sk->sk_ack_backlog = 0;
1866 sk->sk_state = SMC_LISTEN;
1873 static int smc_accept(struct socket *sock, struct socket *new_sock,
1874 int flags, bool kern)
1876 struct sock *sk = sock->sk, *nsk;
1877 DECLARE_WAITQUEUE(wait, current);
1878 struct smc_sock *lsmc;
1883 sock_hold(sk); /* sock_put below */
1886 if (lsmc->sk.sk_state != SMC_LISTEN) {
1892 /* Wait for an incoming connection */
1893 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1894 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1895 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1896 set_current_state(TASK_INTERRUPTIBLE);
1902 timeo = schedule_timeout(timeo);
1903 /* wakeup by sk_data_ready in smc_listen_work() */
1904 sched_annotate_sleep();
1906 if (signal_pending(current)) {
1907 rc = sock_intr_errno(timeo);
1911 set_current_state(TASK_RUNNING);
1912 remove_wait_queue(sk_sleep(sk), &wait);
1915 rc = sock_error(nsk);
1920 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1921 /* wait till data arrives on the socket */
1922 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1924 if (smc_sk(nsk)->use_fallback) {
1925 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1928 if (skb_queue_empty(&clcsk->sk_receive_queue))
1929 sk_wait_data(clcsk, &timeo, NULL);
1930 release_sock(clcsk);
1931 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1933 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
1939 sock_put(sk); /* sock_hold above */
1943 static int smc_getname(struct socket *sock, struct sockaddr *addr,
1946 struct smc_sock *smc;
1948 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1949 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
1952 smc = smc_sk(sock->sk);
1954 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
1957 static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1959 struct sock *sk = sock->sk;
1960 struct smc_sock *smc;
1965 if ((sk->sk_state != SMC_ACTIVE) &&
1966 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1967 (sk->sk_state != SMC_INIT))
1970 if (msg->msg_flags & MSG_FASTOPEN) {
1971 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
1972 smc_switch_to_fallback(smc);
1973 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
1980 if (smc->use_fallback)
1981 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1983 rc = smc_tx_sendmsg(smc, msg, len);
1989 static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1992 struct sock *sk = sock->sk;
1993 struct smc_sock *smc;
1998 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1999 /* socket was connected before, no more data to read */
2003 if ((sk->sk_state == SMC_INIT) ||
2004 (sk->sk_state == SMC_LISTEN) ||
2005 (sk->sk_state == SMC_CLOSED))
2008 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2013 if (smc->use_fallback) {
2014 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
2016 msg->msg_namelen = 0;
2017 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
2025 static __poll_t smc_accept_poll(struct sock *parent)
2027 struct smc_sock *isk = smc_sk(parent);
2030 spin_lock(&isk->accept_q_lock);
2031 if (!list_empty(&isk->accept_q))
2032 mask = EPOLLIN | EPOLLRDNORM;
2033 spin_unlock(&isk->accept_q_lock);
2038 static __poll_t smc_poll(struct file *file, struct socket *sock,
2041 struct sock *sk = sock->sk;
2042 struct smc_sock *smc;
2048 smc = smc_sk(sock->sk);
2049 if (smc->use_fallback) {
2050 /* delegate to CLC child sock */
2051 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
2052 sk->sk_err = smc->clcsock->sk->sk_err;
2054 if (sk->sk_state != SMC_CLOSED)
2055 sock_poll_wait(file, sock, wait);
2058 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
2059 (sk->sk_state == SMC_CLOSED))
2061 if (sk->sk_state == SMC_LISTEN) {
2062 /* woken up by sk_data_ready in smc_listen_work() */
2063 mask |= smc_accept_poll(sk);
2064 } else if (smc->use_fallback) { /* as result of connect_work()*/
2065 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
2067 sk->sk_err = smc->clcsock->sk->sk_err;
2069 if ((sk->sk_state != SMC_INIT &&
2070 atomic_read(&smc->conn.sndbuf_space)) ||
2071 sk->sk_shutdown & SEND_SHUTDOWN) {
2072 mask |= EPOLLOUT | EPOLLWRNORM;
2074 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
2075 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
2077 if (atomic_read(&smc->conn.bytes_to_rcv))
2078 mask |= EPOLLIN | EPOLLRDNORM;
2079 if (sk->sk_shutdown & RCV_SHUTDOWN)
2080 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
2081 if (sk->sk_state == SMC_APPCLOSEWAIT1)
2083 if (smc->conn.urg_state == SMC_URG_VALID)
2091 static int smc_shutdown(struct socket *sock, int how)
2093 struct sock *sk = sock->sk;
2094 struct smc_sock *smc;
2100 if ((how < SHUT_RD) || (how > SHUT_RDWR))
2106 if ((sk->sk_state != SMC_ACTIVE) &&
2107 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
2108 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
2109 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2110 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
2111 (sk->sk_state != SMC_APPFINCLOSEWAIT))
2113 if (smc->use_fallback) {
2114 rc = kernel_sock_shutdown(smc->clcsock, how);
2115 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
2116 if (sk->sk_shutdown == SHUTDOWN_MASK)
2117 sk->sk_state = SMC_CLOSED;
2121 case SHUT_RDWR: /* shutdown in both directions */
2122 rc = smc_close_active(smc);
2125 rc = smc_close_shutdown_write(smc);
2129 /* nothing more to do because peer is not involved */
2133 rc1 = kernel_sock_shutdown(smc->clcsock, how);
2134 /* map sock_shutdown_cmd constants to sk_shutdown value range */
2135 sk->sk_shutdown |= how + 1;
2139 return rc ? rc : rc1;
2142 static int smc_setsockopt(struct socket *sock, int level, int optname,
2143 sockptr_t optval, unsigned int optlen)
2145 struct sock *sk = sock->sk;
2146 struct smc_sock *smc;
2151 /* generic setsockopts reaching us here always apply to the
2154 if (unlikely(!smc->clcsock->ops->setsockopt))
2157 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
2159 if (smc->clcsock->sk->sk_err) {
2160 sk->sk_err = smc->clcsock->sk->sk_err;
2161 sk->sk_error_report(sk);
2164 if (optlen < sizeof(int))
2166 if (copy_from_sockptr(&val, optval, sizeof(int)))
2170 if (rc || smc->use_fallback)
2175 case TCP_FASTOPEN_CONNECT:
2176 case TCP_FASTOPEN_KEY:
2177 case TCP_FASTOPEN_NO_COOKIE:
2178 /* option not supported by SMC */
2179 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
2180 smc_switch_to_fallback(smc);
2181 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
2187 if (sk->sk_state != SMC_INIT &&
2188 sk->sk_state != SMC_LISTEN &&
2189 sk->sk_state != SMC_CLOSED) {
2191 mod_delayed_work(smc->conn.lgr->tx_wq,
2192 &smc->conn.tx_work, 0);
2196 if (sk->sk_state != SMC_INIT &&
2197 sk->sk_state != SMC_LISTEN &&
2198 sk->sk_state != SMC_CLOSED) {
2200 mod_delayed_work(smc->conn.lgr->tx_wq,
2201 &smc->conn.tx_work, 0);
2204 case TCP_DEFER_ACCEPT:
2205 smc->sockopt_defer_accept = val;
2216 static int smc_getsockopt(struct socket *sock, int level, int optname,
2217 char __user *optval, int __user *optlen)
2219 struct smc_sock *smc;
2221 smc = smc_sk(sock->sk);
2222 /* socket options apply to the CLC socket */
2223 if (unlikely(!smc->clcsock->ops->getsockopt))
2225 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
2229 static int smc_ioctl(struct socket *sock, unsigned int cmd,
2232 union smc_host_cursor cons, urg;
2233 struct smc_connection *conn;
2234 struct smc_sock *smc;
2237 smc = smc_sk(sock->sk);
2239 lock_sock(&smc->sk);
2240 if (smc->use_fallback) {
2241 if (!smc->clcsock) {
2242 release_sock(&smc->sk);
2245 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
2246 release_sock(&smc->sk);
2250 case SIOCINQ: /* same as FIONREAD */
2251 if (smc->sk.sk_state == SMC_LISTEN) {
2252 release_sock(&smc->sk);
2255 if (smc->sk.sk_state == SMC_INIT ||
2256 smc->sk.sk_state == SMC_CLOSED)
2259 answ = atomic_read(&smc->conn.bytes_to_rcv);
2262 /* output queue size (not send + not acked) */
2263 if (smc->sk.sk_state == SMC_LISTEN) {
2264 release_sock(&smc->sk);
2267 if (smc->sk.sk_state == SMC_INIT ||
2268 smc->sk.sk_state == SMC_CLOSED)
2271 answ = smc->conn.sndbuf_desc->len -
2272 atomic_read(&smc->conn.sndbuf_space);
2275 /* output queue size (not send only) */
2276 if (smc->sk.sk_state == SMC_LISTEN) {
2277 release_sock(&smc->sk);
2280 if (smc->sk.sk_state == SMC_INIT ||
2281 smc->sk.sk_state == SMC_CLOSED)
2284 answ = smc_tx_prepared_sends(&smc->conn);
2287 if (smc->sk.sk_state == SMC_LISTEN) {
2288 release_sock(&smc->sk);
2291 if (smc->sk.sk_state == SMC_INIT ||
2292 smc->sk.sk_state == SMC_CLOSED) {
2295 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
2296 smc_curs_copy(&urg, &conn->urg_curs, conn);
2297 answ = smc_curs_diff(conn->rmb_desc->len,
2302 release_sock(&smc->sk);
2303 return -ENOIOCTLCMD;
2305 release_sock(&smc->sk);
2307 return put_user(answ, (int __user *)arg);
2310 static ssize_t smc_sendpage(struct socket *sock, struct page *page,
2311 int offset, size_t size, int flags)
2313 struct sock *sk = sock->sk;
2314 struct smc_sock *smc;
2319 if (sk->sk_state != SMC_ACTIVE) {
2324 if (smc->use_fallback)
2325 rc = kernel_sendpage(smc->clcsock, page, offset,
2328 rc = sock_no_sendpage(sock, page, offset, size, flags);
2334 /* Map the affected portions of the rmbe into an spd, note the number of bytes
2335 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
2336 * updates till whenever a respective page has been fully processed.
2337 * Note that subsequent recv() calls have to wait till all splice() processing
2340 static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
2341 struct pipe_inode_info *pipe, size_t len,
2344 struct sock *sk = sock->sk;
2345 struct smc_sock *smc;
2350 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2351 /* socket was connected before, no more data to read */
2355 if (sk->sk_state == SMC_INIT ||
2356 sk->sk_state == SMC_LISTEN ||
2357 sk->sk_state == SMC_CLOSED)
2360 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2365 if (smc->use_fallback) {
2366 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
2373 if (flags & SPLICE_F_NONBLOCK)
2374 flags = MSG_DONTWAIT;
2377 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
2385 /* must look like tcp */
2386 static const struct proto_ops smc_sock_ops = {
2388 .owner = THIS_MODULE,
2389 .release = smc_release,
2391 .connect = smc_connect,
2392 .socketpair = sock_no_socketpair,
2393 .accept = smc_accept,
2394 .getname = smc_getname,
2397 .listen = smc_listen,
2398 .shutdown = smc_shutdown,
2399 .setsockopt = smc_setsockopt,
2400 .getsockopt = smc_getsockopt,
2401 .sendmsg = smc_sendmsg,
2402 .recvmsg = smc_recvmsg,
2403 .mmap = sock_no_mmap,
2404 .sendpage = smc_sendpage,
2405 .splice_read = smc_splice_read,
2408 static int smc_create(struct net *net, struct socket *sock, int protocol,
2411 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
2412 struct smc_sock *smc;
2416 rc = -ESOCKTNOSUPPORT;
2417 if (sock->type != SOCK_STREAM)
2420 rc = -EPROTONOSUPPORT;
2421 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
2425 sock->ops = &smc_sock_ops;
2426 sk = smc_sock_alloc(net, sock, protocol);
2430 /* create internal TCP socket for CLC handshake and fallback */
2432 smc->use_fallback = false; /* assume rdma capability first */
2433 smc->fallback_rsn = 0;
2434 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
2437 sk_common_release(sk);
2440 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
2441 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
2447 static const struct net_proto_family smc_sock_family_ops = {
2449 .owner = THIS_MODULE,
2450 .create = smc_create,
2453 unsigned int smc_net_id;
2455 static __net_init int smc_net_init(struct net *net)
2457 return smc_pnet_net_init(net);
2460 static void __net_exit smc_net_exit(struct net *net)
2462 smc_pnet_net_exit(net);
2465 static struct pernet_operations smc_net_ops = {
2466 .init = smc_net_init,
2467 .exit = smc_net_exit,
2469 .size = sizeof(struct smc_net),
2472 static int __init smc_init(void)
2476 rc = register_pernet_subsys(&smc_net_ops);
2483 rc = smc_pnet_init();
2485 goto out_pernet_subsys;
2488 smc_hs_wq = alloc_workqueue("smc_hs_wq", 0, 0);
2492 smc_close_wq = alloc_workqueue("smc_close_wq", 0, 0);
2494 goto out_alloc_hs_wq;
2496 rc = smc_core_init();
2498 pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
2502 rc = smc_llc_init();
2504 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
2508 rc = smc_cdc_init();
2510 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
2514 rc = proto_register(&smc_proto, 1);
2516 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
2520 rc = proto_register(&smc_proto6, 1);
2522 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2526 rc = sock_register(&smc_sock_family_ops);
2528 pr_err("%s: sock_register fails with %d\n", __func__, rc);
2531 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
2532 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
2534 rc = smc_ib_register_client();
2536 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2540 static_branch_enable(&tcp_have_smc);
2544 sock_unregister(PF_SMC);
2546 proto_unregister(&smc_proto6);
2548 proto_unregister(&smc_proto);
2552 destroy_workqueue(smc_close_wq);
2554 destroy_workqueue(smc_hs_wq);
2558 unregister_pernet_subsys(&smc_net_ops);
2563 static void __exit smc_exit(void)
2565 static_branch_disable(&tcp_have_smc);
2566 sock_unregister(PF_SMC);
2568 smc_ib_unregister_client();
2569 destroy_workqueue(smc_close_wq);
2570 destroy_workqueue(smc_hs_wq);
2571 proto_unregister(&smc_proto6);
2572 proto_unregister(&smc_proto);
2574 unregister_pernet_subsys(&smc_net_ops);
2578 module_init(smc_init);
2579 module_exit(smc_exit);
2581 MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2582 MODULE_DESCRIPTION("smc socket address family");
2583 MODULE_LICENSE("GPL");
2584 MODULE_ALIAS_NETPROTO(PF_SMC);