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
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"
48 #include "smc_netlink.h"
51 #include "smc_close.h"
52 #include "smc_stats.h"
53 #include "smc_tracepoint.h"
55 static DEFINE_MUTEX(smc_server_lgr_pending); /* serialize link group
58 static DEFINE_MUTEX(smc_client_lgr_pending); /* serialize link group
62 struct workqueue_struct *smc_hs_wq; /* wq for handshake work */
63 struct workqueue_struct *smc_close_wq; /* wq for close work */
65 static void smc_tcp_listen_work(struct work_struct *);
66 static void smc_connect_work(struct work_struct *);
68 static void smc_set_keepalive(struct sock *sk, int val)
70 struct smc_sock *smc = smc_sk(sk);
72 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
75 static struct smc_hashinfo smc_v4_hashinfo = {
76 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
79 static struct smc_hashinfo smc_v6_hashinfo = {
80 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
83 int smc_hash_sk(struct sock *sk)
85 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
86 struct hlist_head *head;
90 write_lock_bh(&h->lock);
91 sk_add_node(sk, head);
92 write_unlock_bh(&h->lock);
93 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
97 EXPORT_SYMBOL_GPL(smc_hash_sk);
99 void smc_unhash_sk(struct sock *sk)
101 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
103 write_lock_bh(&h->lock);
104 if (sk_del_node_init(sk))
105 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
106 write_unlock_bh(&h->lock);
108 EXPORT_SYMBOL_GPL(smc_unhash_sk);
110 struct proto smc_proto = {
112 .owner = THIS_MODULE,
113 .keepalive = smc_set_keepalive,
115 .unhash = smc_unhash_sk,
116 .obj_size = sizeof(struct smc_sock),
117 .h.smc_hash = &smc_v4_hashinfo,
118 .slab_flags = SLAB_TYPESAFE_BY_RCU,
120 EXPORT_SYMBOL_GPL(smc_proto);
122 struct proto smc_proto6 = {
124 .owner = THIS_MODULE,
125 .keepalive = smc_set_keepalive,
127 .unhash = smc_unhash_sk,
128 .obj_size = sizeof(struct smc_sock),
129 .h.smc_hash = &smc_v6_hashinfo,
130 .slab_flags = SLAB_TYPESAFE_BY_RCU,
132 EXPORT_SYMBOL_GPL(smc_proto6);
134 static void smc_restore_fallback_changes(struct smc_sock *smc)
136 if (smc->clcsock->file) { /* non-accepted sockets have no file yet */
137 smc->clcsock->file->private_data = smc->sk.sk_socket;
138 smc->clcsock->file = NULL;
142 static int __smc_release(struct smc_sock *smc)
144 struct sock *sk = &smc->sk;
147 if (!smc->use_fallback) {
148 rc = smc_close_active(smc);
149 sock_set_flag(sk, SOCK_DEAD);
150 sk->sk_shutdown |= SHUTDOWN_MASK;
152 if (sk->sk_state != SMC_CLOSED) {
153 if (sk->sk_state != SMC_LISTEN &&
154 sk->sk_state != SMC_INIT)
155 sock_put(sk); /* passive closing */
156 if (sk->sk_state == SMC_LISTEN) {
157 /* wake up clcsock accept */
158 rc = kernel_sock_shutdown(smc->clcsock,
161 sk->sk_state = SMC_CLOSED;
162 sk->sk_state_change(sk);
164 smc_restore_fallback_changes(smc);
167 sk->sk_prot->unhash(sk);
169 if (sk->sk_state == SMC_CLOSED) {
172 smc_clcsock_release(smc);
175 if (!smc->use_fallback)
176 smc_conn_free(&smc->conn);
182 static int smc_release(struct socket *sock)
184 struct sock *sk = sock->sk;
185 struct smc_sock *smc;
191 sock_hold(sk); /* sock_put below */
194 /* cleanup for a dangling non-blocking connect */
195 if (smc->connect_nonblock && sk->sk_state == SMC_INIT)
196 tcp_abort(smc->clcsock->sk, ECONNABORTED);
198 if (cancel_work_sync(&smc->connect_work))
199 sock_put(&smc->sk); /* sock_hold in smc_connect for passive closing */
201 if (sk->sk_state == SMC_LISTEN)
202 /* smc_close_non_accepted() is called and acquires
203 * sock lock for child sockets again
205 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
209 rc = __smc_release(smc);
216 sock_put(sk); /* sock_hold above */
217 sock_put(sk); /* final sock_put */
222 static void smc_destruct(struct sock *sk)
224 if (sk->sk_state != SMC_CLOSED)
226 if (!sock_flag(sk, SOCK_DEAD))
229 sk_refcnt_debug_dec(sk);
232 static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
235 struct smc_sock *smc;
239 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
240 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
244 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
245 sk->sk_state = SMC_INIT;
246 sk->sk_destruct = smc_destruct;
247 sk->sk_protocol = protocol;
249 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
250 INIT_WORK(&smc->connect_work, smc_connect_work);
251 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
252 INIT_LIST_HEAD(&smc->accept_q);
253 spin_lock_init(&smc->accept_q_lock);
254 spin_lock_init(&smc->conn.send_lock);
255 sk->sk_prot->hash(sk);
256 sk_refcnt_debug_inc(sk);
257 mutex_init(&smc->clcsock_release_lock);
262 static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
265 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
266 struct sock *sk = sock->sk;
267 struct smc_sock *smc;
272 /* replicate tests from inet_bind(), to be safe wrt. future changes */
274 if (addr_len < sizeof(struct sockaddr_in))
278 if (addr->sin_family != AF_INET &&
279 addr->sin_family != AF_INET6 &&
280 addr->sin_family != AF_UNSPEC)
282 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
283 if (addr->sin_family == AF_UNSPEC &&
284 addr->sin_addr.s_addr != htonl(INADDR_ANY))
289 /* Check if socket is already active */
291 if (sk->sk_state != SMC_INIT || smc->connect_nonblock)
294 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
295 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
303 static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
306 /* options we don't get control via setsockopt for */
307 nsk->sk_type = osk->sk_type;
308 nsk->sk_sndbuf = osk->sk_sndbuf;
309 nsk->sk_rcvbuf = osk->sk_rcvbuf;
310 nsk->sk_sndtimeo = osk->sk_sndtimeo;
311 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
312 nsk->sk_mark = osk->sk_mark;
313 nsk->sk_priority = osk->sk_priority;
314 nsk->sk_rcvlowat = osk->sk_rcvlowat;
315 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
316 nsk->sk_err = osk->sk_err;
318 nsk->sk_flags &= ~mask;
319 nsk->sk_flags |= osk->sk_flags & mask;
322 #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
323 (1UL << SOCK_KEEPOPEN) | \
324 (1UL << SOCK_LINGER) | \
325 (1UL << SOCK_BROADCAST) | \
326 (1UL << SOCK_TIMESTAMP) | \
327 (1UL << SOCK_DBG) | \
328 (1UL << SOCK_RCVTSTAMP) | \
329 (1UL << SOCK_RCVTSTAMPNS) | \
330 (1UL << SOCK_LOCALROUTE) | \
331 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
332 (1UL << SOCK_RXQ_OVFL) | \
333 (1UL << SOCK_WIFI_STATUS) | \
334 (1UL << SOCK_NOFCS) | \
335 (1UL << SOCK_FILTER_LOCKED) | \
336 (1UL << SOCK_TSTAMP_NEW))
337 /* copy only relevant settings and flags of SOL_SOCKET level from smc to
338 * clc socket (since smc is not called for these options from net/core)
340 static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
342 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
345 #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
346 (1UL << SOCK_KEEPOPEN) | \
347 (1UL << SOCK_LINGER) | \
349 /* copy only settings and flags relevant for smc from clc to smc socket */
350 static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
352 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
355 /* register the new rmb on all links */
356 static int smcr_lgr_reg_rmbs(struct smc_link *link,
357 struct smc_buf_desc *rmb_desc)
359 struct smc_link_group *lgr = link->lgr;
362 rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY);
365 /* protect against parallel smc_llc_cli_rkey_exchange() and
366 * parallel smcr_link_reg_rmb()
368 mutex_lock(&lgr->llc_conf_mutex);
369 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
370 if (!smc_link_active(&lgr->lnk[i]))
372 rc = smcr_link_reg_rmb(&lgr->lnk[i], rmb_desc);
377 /* exchange confirm_rkey msg with peer */
378 rc = smc_llc_do_confirm_rkey(link, rmb_desc);
383 rmb_desc->is_conf_rkey = true;
385 mutex_unlock(&lgr->llc_conf_mutex);
386 smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
390 static int smcr_clnt_conf_first_link(struct smc_sock *smc)
392 struct smc_link *link = smc->conn.lnk;
393 struct smc_llc_qentry *qentry;
396 /* receive CONFIRM LINK request from server over RoCE fabric */
397 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
398 SMC_LLC_CONFIRM_LINK);
400 struct smc_clc_msg_decline dclc;
402 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
403 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
404 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
406 smc_llc_save_peer_uid(qentry);
407 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_REQ);
408 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
410 return SMC_CLC_DECL_RMBE_EC;
412 rc = smc_ib_modify_qp_rts(link);
414 return SMC_CLC_DECL_ERR_RDYLNK;
416 smc_wr_remember_qp_attr(link);
418 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
419 return SMC_CLC_DECL_ERR_REGRMB;
421 /* confirm_rkey is implicit on 1st contact */
422 smc->conn.rmb_desc->is_conf_rkey = true;
424 /* send CONFIRM LINK response over RoCE fabric */
425 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
427 return SMC_CLC_DECL_TIMEOUT_CL;
429 smc_llc_link_active(link);
430 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
432 /* optional 2nd link, receive ADD LINK request from server */
433 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
436 struct smc_clc_msg_decline dclc;
438 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
439 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
441 rc = 0; /* no DECLINE received, go with one link */
444 smc_llc_flow_qentry_clr(&link->lgr->llc_flow_lcl);
445 smc_llc_cli_add_link(link, qentry);
449 static bool smc_isascii(char *hostname)
453 for (i = 0; i < SMC_MAX_HOSTNAME_LEN; i++)
454 if (!isascii(hostname[i]))
459 static void smc_conn_save_peer_info_fce(struct smc_sock *smc,
460 struct smc_clc_msg_accept_confirm *clc)
462 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
463 (struct smc_clc_msg_accept_confirm_v2 *)clc;
464 struct smc_clc_first_contact_ext *fce;
467 if (clc->hdr.version == SMC_V1 ||
468 !(clc->hdr.typev2 & SMC_FIRST_CONTACT_MASK))
471 if (smc->conn.lgr->is_smcd) {
472 memcpy(smc->conn.lgr->negotiated_eid, clc_v2->d1.eid,
474 clc_v2_len = offsetofend(struct smc_clc_msg_accept_confirm_v2,
477 memcpy(smc->conn.lgr->negotiated_eid, clc_v2->r1.eid,
479 clc_v2_len = offsetofend(struct smc_clc_msg_accept_confirm_v2,
482 fce = (struct smc_clc_first_contact_ext *)(((u8 *)clc_v2) + clc_v2_len);
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);
490 static void smcr_conn_save_peer_info(struct smc_sock *smc,
491 struct smc_clc_msg_accept_confirm *clc)
493 int bufsize = smc_uncompress_bufsize(clc->r0.rmbe_size);
495 smc->conn.peer_rmbe_idx = clc->r0.rmbe_idx;
496 smc->conn.local_tx_ctrl.token = ntohl(clc->r0.rmbe_alert_token);
497 smc->conn.peer_rmbe_size = bufsize;
498 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
499 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
502 static void smcd_conn_save_peer_info(struct smc_sock *smc,
503 struct smc_clc_msg_accept_confirm *clc)
505 int bufsize = smc_uncompress_bufsize(clc->d0.dmbe_size);
507 smc->conn.peer_rmbe_idx = clc->d0.dmbe_idx;
508 smc->conn.peer_token = clc->d0.token;
509 /* msg header takes up space in the buffer */
510 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
511 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
512 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
515 static void smc_conn_save_peer_info(struct smc_sock *smc,
516 struct smc_clc_msg_accept_confirm *clc)
518 if (smc->conn.lgr->is_smcd)
519 smcd_conn_save_peer_info(smc, clc);
521 smcr_conn_save_peer_info(smc, clc);
522 smc_conn_save_peer_info_fce(smc, clc);
525 static void smc_link_save_peer_info(struct smc_link *link,
526 struct smc_clc_msg_accept_confirm *clc,
527 struct smc_init_info *ini)
529 link->peer_qpn = ntoh24(clc->r0.qpn);
530 memcpy(link->peer_gid, ini->peer_gid, SMC_GID_SIZE);
531 memcpy(link->peer_mac, ini->peer_mac, sizeof(link->peer_mac));
532 link->peer_psn = ntoh24(clc->r0.psn);
533 link->peer_mtu = clc->r0.qp_mtu;
536 static void smc_stat_inc_fback_rsn_cnt(struct smc_sock *smc,
537 struct smc_stats_fback *fback_arr)
541 for (cnt = 0; cnt < SMC_MAX_FBACK_RSN_CNT; cnt++) {
542 if (fback_arr[cnt].fback_code == smc->fallback_rsn) {
543 fback_arr[cnt].count++;
546 if (!fback_arr[cnt].fback_code) {
547 fback_arr[cnt].fback_code = smc->fallback_rsn;
548 fback_arr[cnt].count++;
554 static void smc_stat_fallback(struct smc_sock *smc)
556 struct net *net = sock_net(&smc->sk);
558 mutex_lock(&net->smc.mutex_fback_rsn);
559 if (smc->listen_smc) {
560 smc_stat_inc_fback_rsn_cnt(smc, net->smc.fback_rsn->srv);
561 net->smc.fback_rsn->srv_fback_cnt++;
563 smc_stat_inc_fback_rsn_cnt(smc, net->smc.fback_rsn->clnt);
564 net->smc.fback_rsn->clnt_fback_cnt++;
566 mutex_unlock(&net->smc.mutex_fback_rsn);
569 static void smc_switch_to_fallback(struct smc_sock *smc, int reason_code)
571 wait_queue_head_t *smc_wait = sk_sleep(&smc->sk);
572 wait_queue_head_t *clc_wait = sk_sleep(smc->clcsock->sk);
575 smc->use_fallback = true;
576 smc->fallback_rsn = reason_code;
577 smc_stat_fallback(smc);
578 trace_smc_switch_to_fallback(smc, reason_code);
579 if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
580 smc->clcsock->file = smc->sk.sk_socket->file;
581 smc->clcsock->file->private_data = smc->clcsock;
582 smc->clcsock->wq.fasync_list =
583 smc->sk.sk_socket->wq.fasync_list;
585 /* There may be some entries remaining in
586 * smc socket->wq, which should be removed
587 * to clcsocket->wq during the fallback.
589 spin_lock_irqsave(&smc_wait->lock, flags);
590 spin_lock_nested(&clc_wait->lock, SINGLE_DEPTH_NESTING);
591 list_splice_init(&smc_wait->head, &clc_wait->head);
592 spin_unlock(&clc_wait->lock);
593 spin_unlock_irqrestore(&smc_wait->lock, flags);
597 /* fall back during connect */
598 static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
600 smc_switch_to_fallback(smc, reason_code);
601 smc_copy_sock_settings_to_clc(smc);
602 smc->connect_nonblock = 0;
603 if (smc->sk.sk_state == SMC_INIT)
604 smc->sk.sk_state = SMC_ACTIVE;
608 /* decline and fall back during connect */
609 static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code,
612 struct net *net = sock_net(&smc->sk);
615 if (reason_code < 0) { /* error, fallback is not possible */
616 this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
617 if (smc->sk.sk_state == SMC_INIT)
618 sock_put(&smc->sk); /* passive closing */
621 if (reason_code != SMC_CLC_DECL_PEERDECL) {
622 rc = smc_clc_send_decline(smc, reason_code, version);
624 this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
625 if (smc->sk.sk_state == SMC_INIT)
626 sock_put(&smc->sk); /* passive closing */
630 return smc_connect_fallback(smc, reason_code);
633 static void smc_conn_abort(struct smc_sock *smc, int local_first)
636 smc_lgr_cleanup_early(&smc->conn);
638 smc_conn_free(&smc->conn);
641 /* check if there is a rdma device available for this connection. */
642 /* called for connect and listen */
643 static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
645 /* PNET table look up: search active ib_device and port
646 * within same PNETID that also contains the ethernet device
647 * used for the internal TCP socket
649 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
650 if (!ini->check_smcrv2 && !ini->ib_dev)
651 return SMC_CLC_DECL_NOSMCRDEV;
652 if (ini->check_smcrv2 && !ini->smcrv2.ib_dev_v2)
653 return SMC_CLC_DECL_NOSMCRDEV;
657 /* check if there is an ISM device available for this connection. */
658 /* called for connect and listen */
659 static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
661 /* Find ISM device with same PNETID as connecting interface */
662 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
663 if (!ini->ism_dev[0])
664 return SMC_CLC_DECL_NOSMCDDEV;
666 ini->ism_chid[0] = smc_ism_get_chid(ini->ism_dev[0]);
670 /* is chid unique for the ism devices that are already determined? */
671 static bool smc_find_ism_v2_is_unique_chid(u16 chid, struct smc_init_info *ini,
674 int i = (!ini->ism_dev[0]) ? 1 : 0;
677 if (ini->ism_chid[i] == chid)
682 /* determine possible V2 ISM devices (either without PNETID or with PNETID plus
683 * PNETID matching net_device)
685 static int smc_find_ism_v2_device_clnt(struct smc_sock *smc,
686 struct smc_init_info *ini)
688 int rc = SMC_CLC_DECL_NOSMCDDEV;
689 struct smcd_dev *smcd;
693 if (smcd_indicated(ini->smc_type_v1))
694 rc = 0; /* already initialized for V1 */
695 mutex_lock(&smcd_dev_list.mutex);
696 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
697 if (smcd->going_away || smcd == ini->ism_dev[0])
699 chid = smc_ism_get_chid(smcd);
700 if (!smc_find_ism_v2_is_unique_chid(chid, ini, i))
702 if (!smc_pnet_is_pnetid_set(smcd->pnetid) ||
703 smc_pnet_is_ndev_pnetid(sock_net(&smc->sk), smcd->pnetid)) {
704 ini->ism_dev[i] = smcd;
705 ini->ism_chid[i] = chid;
709 if (i > SMC_MAX_ISM_DEVS)
713 mutex_unlock(&smcd_dev_list.mutex);
714 ini->ism_offered_cnt = i - 1;
715 if (!ini->ism_dev[0] && !ini->ism_dev[1])
716 ini->smcd_version = 0;
721 /* Check for VLAN ID and register it on ISM device just for CLC handshake */
722 static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
723 struct smc_init_info *ini)
725 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev[0], ini->vlan_id))
726 return SMC_CLC_DECL_ISMVLANERR;
730 static int smc_find_proposal_devices(struct smc_sock *smc,
731 struct smc_init_info *ini)
735 /* check if there is an ism device available */
736 if (!(ini->smcd_version & SMC_V1) ||
737 smc_find_ism_device(smc, ini) ||
738 smc_connect_ism_vlan_setup(smc, ini))
739 ini->smcd_version &= ~SMC_V1;
740 /* else ISM V1 is supported for this connection */
742 /* check if there is an rdma device available */
743 if (!(ini->smcr_version & SMC_V1) ||
744 smc_find_rdma_device(smc, ini))
745 ini->smcr_version &= ~SMC_V1;
746 /* else RDMA is supported for this connection */
748 ini->smc_type_v1 = smc_indicated_type(ini->smcd_version & SMC_V1,
749 ini->smcr_version & SMC_V1);
751 /* check if there is an ism v2 device available */
752 if (!(ini->smcd_version & SMC_V2) ||
753 !smc_ism_is_v2_capable() ||
754 smc_find_ism_v2_device_clnt(smc, ini))
755 ini->smcd_version &= ~SMC_V2;
757 /* check if there is an rdma v2 device available */
758 ini->check_smcrv2 = true;
759 ini->smcrv2.saddr = smc->clcsock->sk->sk_rcv_saddr;
760 if (!(ini->smcr_version & SMC_V2) ||
761 smc->clcsock->sk->sk_family != AF_INET ||
762 !smc_clc_ueid_count() ||
763 smc_find_rdma_device(smc, ini))
764 ini->smcr_version &= ~SMC_V2;
765 ini->check_smcrv2 = false;
767 ini->smc_type_v2 = smc_indicated_type(ini->smcd_version & SMC_V2,
768 ini->smcr_version & SMC_V2);
770 /* if neither ISM nor RDMA are supported, fallback */
771 if (ini->smc_type_v1 == SMC_TYPE_N && ini->smc_type_v2 == SMC_TYPE_N)
772 rc = SMC_CLC_DECL_NOSMCDEV;
777 /* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
778 * used, the VLAN ID will be registered again during the connection setup.
780 static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc,
781 struct smc_init_info *ini)
783 if (!smcd_indicated(ini->smc_type_v1))
785 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev[0], ini->vlan_id))
786 return SMC_CLC_DECL_CNFERR;
790 #define SMC_CLC_MAX_ACCEPT_LEN \
791 (sizeof(struct smc_clc_msg_accept_confirm_v2) + \
792 sizeof(struct smc_clc_first_contact_ext) + \
793 sizeof(struct smc_clc_msg_trail))
795 /* CLC handshake during connect */
796 static int smc_connect_clc(struct smc_sock *smc,
797 struct smc_clc_msg_accept_confirm_v2 *aclc2,
798 struct smc_init_info *ini)
802 /* do inband token exchange */
803 rc = smc_clc_send_proposal(smc, ini);
806 /* receive SMC Accept CLC message */
807 return smc_clc_wait_msg(smc, aclc2, SMC_CLC_MAX_ACCEPT_LEN,
808 SMC_CLC_ACCEPT, CLC_WAIT_TIME);
811 void smc_fill_gid_list(struct smc_link_group *lgr,
812 struct smc_gidlist *gidlist,
813 struct smc_ib_device *known_dev, u8 *known_gid)
815 struct smc_init_info *alt_ini = NULL;
817 memset(gidlist, 0, sizeof(*gidlist));
818 memcpy(gidlist->list[gidlist->len++], known_gid, SMC_GID_SIZE);
820 alt_ini = kzalloc(sizeof(*alt_ini), GFP_KERNEL);
824 alt_ini->vlan_id = lgr->vlan_id;
825 alt_ini->check_smcrv2 = true;
826 alt_ini->smcrv2.saddr = lgr->saddr;
827 smc_pnet_find_alt_roce(lgr, alt_ini, known_dev);
829 if (!alt_ini->smcrv2.ib_dev_v2)
832 memcpy(gidlist->list[gidlist->len++], alt_ini->smcrv2.ib_gid_v2,
839 static int smc_connect_rdma_v2_prepare(struct smc_sock *smc,
840 struct smc_clc_msg_accept_confirm *aclc,
841 struct smc_init_info *ini)
843 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
844 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
845 struct smc_clc_first_contact_ext *fce =
846 (struct smc_clc_first_contact_ext *)
847 (((u8 *)clc_v2) + sizeof(*clc_v2));
849 if (!ini->first_contact_peer || aclc->hdr.version == SMC_V1)
852 if (fce->v2_direct) {
853 memcpy(ini->smcrv2.nexthop_mac, &aclc->r0.lcl.mac, ETH_ALEN);
854 ini->smcrv2.uses_gateway = false;
856 if (smc_ib_find_route(smc->clcsock->sk->sk_rcv_saddr,
857 smc_ib_gid_to_ipv4(aclc->r0.lcl.gid),
858 ini->smcrv2.nexthop_mac,
859 &ini->smcrv2.uses_gateway))
860 return SMC_CLC_DECL_NOROUTE;
861 if (!ini->smcrv2.uses_gateway) {
862 /* mismatch: peer claims indirect, but its direct */
863 return SMC_CLC_DECL_NOINDIRECT;
869 /* setup for RDMA connection of client */
870 static int smc_connect_rdma(struct smc_sock *smc,
871 struct smc_clc_msg_accept_confirm *aclc,
872 struct smc_init_info *ini)
874 int i, reason_code = 0;
875 struct smc_link *link;
878 ini->is_smcd = false;
879 ini->ib_clcqpn = ntoh24(aclc->r0.qpn);
880 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
881 memcpy(ini->peer_systemid, aclc->r0.lcl.id_for_peer, SMC_SYSTEMID_LEN);
882 memcpy(ini->peer_gid, aclc->r0.lcl.gid, SMC_GID_SIZE);
883 memcpy(ini->peer_mac, aclc->r0.lcl.mac, ETH_ALEN);
885 reason_code = smc_connect_rdma_v2_prepare(smc, aclc, ini);
889 mutex_lock(&smc_client_lgr_pending);
890 reason_code = smc_conn_create(smc, ini);
892 mutex_unlock(&smc_client_lgr_pending);
896 smc_conn_save_peer_info(smc, aclc);
898 if (ini->first_contact_local) {
899 link = smc->conn.lnk;
901 /* set link that was assigned by server */
903 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
904 struct smc_link *l = &smc->conn.lgr->lnk[i];
906 if (l->peer_qpn == ntoh24(aclc->r0.qpn) &&
907 !memcmp(l->peer_gid, &aclc->r0.lcl.gid,
909 (aclc->hdr.version > SMC_V1 ||
910 !memcmp(l->peer_mac, &aclc->r0.lcl.mac,
911 sizeof(l->peer_mac)))) {
917 reason_code = SMC_CLC_DECL_NOSRVLINK;
920 smc_switch_link_and_count(&smc->conn, link);
923 /* create send buffer and rmb */
924 if (smc_buf_create(smc, false)) {
925 reason_code = SMC_CLC_DECL_MEM;
929 if (ini->first_contact_local)
930 smc_link_save_peer_info(link, aclc, ini);
932 if (smc_rmb_rtoken_handling(&smc->conn, link, aclc)) {
933 reason_code = SMC_CLC_DECL_ERR_RTOK;
940 if (ini->first_contact_local) {
941 if (smc_ib_ready_link(link)) {
942 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
946 if (smcr_lgr_reg_rmbs(link, smc->conn.rmb_desc)) {
947 reason_code = SMC_CLC_DECL_ERR_REGRMB;
951 smc_rmb_sync_sg_for_device(&smc->conn);
953 if (aclc->hdr.version > SMC_V1) {
954 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
955 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
957 eid = clc_v2->r1.eid;
958 if (ini->first_contact_local)
959 smc_fill_gid_list(link->lgr, &ini->smcrv2.gidlist,
960 link->smcibdev, link->gid);
963 reason_code = smc_clc_send_confirm(smc, ini->first_contact_local,
964 aclc->hdr.version, eid, ini);
970 if (ini->first_contact_local) {
971 /* QP confirmation over RoCE fabric */
972 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
973 reason_code = smcr_clnt_conf_first_link(smc);
974 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
978 mutex_unlock(&smc_client_lgr_pending);
980 smc_copy_sock_settings_to_clc(smc);
981 smc->connect_nonblock = 0;
982 if (smc->sk.sk_state == SMC_INIT)
983 smc->sk.sk_state = SMC_ACTIVE;
987 smc_conn_abort(smc, ini->first_contact_local);
988 mutex_unlock(&smc_client_lgr_pending);
989 smc->connect_nonblock = 0;
994 /* The server has chosen one of the proposed ISM devices for the communication.
995 * Determine from the CHID of the received CLC ACCEPT the ISM device chosen.
998 smc_v2_determine_accepted_chid(struct smc_clc_msg_accept_confirm_v2 *aclc,
999 struct smc_init_info *ini)
1003 for (i = 0; i < ini->ism_offered_cnt + 1; i++) {
1004 if (ini->ism_chid[i] == ntohs(aclc->d1.chid)) {
1005 ini->ism_selected = i;
1013 /* setup for ISM connection of client */
1014 static int smc_connect_ism(struct smc_sock *smc,
1015 struct smc_clc_msg_accept_confirm *aclc,
1016 struct smc_init_info *ini)
1021 ini->is_smcd = true;
1022 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
1024 if (aclc->hdr.version == SMC_V2) {
1025 struct smc_clc_msg_accept_confirm_v2 *aclc_v2 =
1026 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
1028 rc = smc_v2_determine_accepted_chid(aclc_v2, ini);
1032 ini->ism_peer_gid[ini->ism_selected] = aclc->d0.gid;
1034 /* there is only one lgr role for SMC-D; use server lock */
1035 mutex_lock(&smc_server_lgr_pending);
1036 rc = smc_conn_create(smc, ini);
1038 mutex_unlock(&smc_server_lgr_pending);
1042 /* Create send and receive buffers */
1043 rc = smc_buf_create(smc, true);
1045 rc = (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB : SMC_CLC_DECL_MEM;
1049 smc_conn_save_peer_info(smc, aclc);
1050 smc_close_init(smc);
1054 if (aclc->hdr.version > SMC_V1) {
1055 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
1056 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
1058 eid = clc_v2->d1.eid;
1061 rc = smc_clc_send_confirm(smc, ini->first_contact_local,
1062 aclc->hdr.version, eid, NULL);
1065 mutex_unlock(&smc_server_lgr_pending);
1067 smc_copy_sock_settings_to_clc(smc);
1068 smc->connect_nonblock = 0;
1069 if (smc->sk.sk_state == SMC_INIT)
1070 smc->sk.sk_state = SMC_ACTIVE;
1074 smc_conn_abort(smc, ini->first_contact_local);
1075 mutex_unlock(&smc_server_lgr_pending);
1076 smc->connect_nonblock = 0;
1081 /* check if received accept type and version matches a proposed one */
1082 static int smc_connect_check_aclc(struct smc_init_info *ini,
1083 struct smc_clc_msg_accept_confirm *aclc)
1085 if (aclc->hdr.typev1 != SMC_TYPE_R &&
1086 aclc->hdr.typev1 != SMC_TYPE_D)
1087 return SMC_CLC_DECL_MODEUNSUPP;
1089 if (aclc->hdr.version >= SMC_V2) {
1090 if ((aclc->hdr.typev1 == SMC_TYPE_R &&
1091 !smcr_indicated(ini->smc_type_v2)) ||
1092 (aclc->hdr.typev1 == SMC_TYPE_D &&
1093 !smcd_indicated(ini->smc_type_v2)))
1094 return SMC_CLC_DECL_MODEUNSUPP;
1096 if ((aclc->hdr.typev1 == SMC_TYPE_R &&
1097 !smcr_indicated(ini->smc_type_v1)) ||
1098 (aclc->hdr.typev1 == SMC_TYPE_D &&
1099 !smcd_indicated(ini->smc_type_v1)))
1100 return SMC_CLC_DECL_MODEUNSUPP;
1106 /* perform steps before actually connecting */
1107 static int __smc_connect(struct smc_sock *smc)
1109 u8 version = smc_ism_is_v2_capable() ? SMC_V2 : SMC_V1;
1110 struct smc_clc_msg_accept_confirm_v2 *aclc2;
1111 struct smc_clc_msg_accept_confirm *aclc;
1112 struct smc_init_info *ini = NULL;
1116 if (smc->use_fallback)
1117 return smc_connect_fallback(smc, smc->fallback_rsn);
1119 /* if peer has not signalled SMC-capability, fall back */
1120 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
1121 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
1123 /* IPSec connections opt out of SMC optimizations */
1124 if (using_ipsec(smc))
1125 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC,
1128 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
1130 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_MEM,
1133 ini->smcd_version = SMC_V1 | SMC_V2;
1134 ini->smcr_version = SMC_V1 | SMC_V2;
1135 ini->smc_type_v1 = SMC_TYPE_B;
1136 ini->smc_type_v2 = SMC_TYPE_B;
1138 /* get vlan id from IP device */
1139 if (smc_vlan_by_tcpsk(smc->clcsock, ini)) {
1140 ini->smcd_version &= ~SMC_V1;
1141 ini->smcr_version = 0;
1142 ini->smc_type_v1 = SMC_TYPE_N;
1143 if (!ini->smcd_version) {
1144 rc = SMC_CLC_DECL_GETVLANERR;
1149 rc = smc_find_proposal_devices(smc, ini);
1153 buf = kzalloc(SMC_CLC_MAX_ACCEPT_LEN, GFP_KERNEL);
1155 rc = SMC_CLC_DECL_MEM;
1158 aclc2 = (struct smc_clc_msg_accept_confirm_v2 *)buf;
1159 aclc = (struct smc_clc_msg_accept_confirm *)aclc2;
1161 /* perform CLC handshake */
1162 rc = smc_connect_clc(smc, aclc2, ini);
1166 /* check if smc modes and versions of CLC proposal and accept match */
1167 rc = smc_connect_check_aclc(ini, aclc);
1168 version = aclc->hdr.version == SMC_V1 ? SMC_V1 : SMC_V2;
1172 /* depending on previous steps, connect using rdma or ism */
1173 if (aclc->hdr.typev1 == SMC_TYPE_R) {
1174 ini->smcr_version = version;
1175 rc = smc_connect_rdma(smc, aclc, ini);
1176 } else if (aclc->hdr.typev1 == SMC_TYPE_D) {
1177 ini->smcd_version = version;
1178 rc = smc_connect_ism(smc, aclc, ini);
1183 SMC_STAT_CLNT_SUCC_INC(sock_net(smc->clcsock->sk), aclc);
1184 smc_connect_ism_vlan_cleanup(smc, ini);
1190 smc_connect_ism_vlan_cleanup(smc, ini);
1194 return smc_connect_decline_fallback(smc, rc, version);
1197 static void smc_connect_work(struct work_struct *work)
1199 struct smc_sock *smc = container_of(work, struct smc_sock,
1201 long timeo = smc->sk.sk_sndtimeo;
1205 timeo = MAX_SCHEDULE_TIMEOUT;
1206 lock_sock(smc->clcsock->sk);
1207 if (smc->clcsock->sk->sk_err) {
1208 smc->sk.sk_err = smc->clcsock->sk->sk_err;
1209 } else if ((1 << smc->clcsock->sk->sk_state) &
1210 (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
1211 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
1212 if ((rc == -EPIPE) &&
1213 ((1 << smc->clcsock->sk->sk_state) &
1214 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
1217 release_sock(smc->clcsock->sk);
1218 lock_sock(&smc->sk);
1219 if (rc != 0 || smc->sk.sk_err) {
1220 smc->sk.sk_state = SMC_CLOSED;
1221 if (rc == -EPIPE || rc == -EAGAIN)
1222 smc->sk.sk_err = EPIPE;
1223 else if (signal_pending(current))
1224 smc->sk.sk_err = -sock_intr_errno(timeo);
1225 sock_put(&smc->sk); /* passive closing */
1229 rc = __smc_connect(smc);
1231 smc->sk.sk_err = -rc;
1234 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
1235 if (smc->sk.sk_err) {
1236 smc->sk.sk_state_change(&smc->sk);
1237 } else { /* allow polling before and after fallback decision */
1238 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
1239 smc->sk.sk_write_space(&smc->sk);
1242 release_sock(&smc->sk);
1245 static int smc_connect(struct socket *sock, struct sockaddr *addr,
1246 int alen, int flags)
1248 struct sock *sk = sock->sk;
1249 struct smc_sock *smc;
1254 /* separate smc parameter checking to be safe */
1255 if (alen < sizeof(addr->sa_family))
1257 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
1261 switch (sk->sk_state) {
1271 smc_copy_sock_settings_to_clc(smc);
1272 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
1273 if (smc->connect_nonblock) {
1277 rc = kernel_connect(smc->clcsock, addr, alen, flags);
1278 if (rc && rc != -EINPROGRESS)
1281 sock_hold(&smc->sk); /* sock put in passive closing */
1282 if (smc->use_fallback)
1284 if (flags & O_NONBLOCK) {
1285 if (queue_work(smc_hs_wq, &smc->connect_work))
1286 smc->connect_nonblock = 1;
1289 rc = __smc_connect(smc);
1293 rc = 0; /* success cases including fallback */
1302 static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
1304 struct socket *new_clcsock = NULL;
1305 struct sock *lsk = &lsmc->sk;
1306 struct sock *new_sk;
1310 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
1313 lsk->sk_err = ENOMEM;
1318 *new_smc = smc_sk(new_sk);
1320 mutex_lock(&lsmc->clcsock_release_lock);
1322 rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK);
1323 mutex_unlock(&lsmc->clcsock_release_lock);
1325 if (rc < 0 && rc != -EAGAIN)
1327 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
1328 new_sk->sk_prot->unhash(new_sk);
1330 sock_release(new_clcsock);
1331 new_sk->sk_state = SMC_CLOSED;
1332 sock_set_flag(new_sk, SOCK_DEAD);
1333 sock_put(new_sk); /* final */
1338 /* new clcsock has inherited the smc listen-specific sk_data_ready
1339 * function; switch it back to the original sk_data_ready function
1341 new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready;
1342 (*new_smc)->clcsock = new_clcsock;
1347 /* add a just created sock to the accept queue of the listen sock as
1348 * candidate for a following socket accept call from user space
1350 static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
1352 struct smc_sock *par = smc_sk(parent);
1354 sock_hold(sk); /* sock_put in smc_accept_unlink () */
1355 spin_lock(&par->accept_q_lock);
1356 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
1357 spin_unlock(&par->accept_q_lock);
1358 sk_acceptq_added(parent);
1361 /* remove a socket from the accept queue of its parental listening socket */
1362 static void smc_accept_unlink(struct sock *sk)
1364 struct smc_sock *par = smc_sk(sk)->listen_smc;
1366 spin_lock(&par->accept_q_lock);
1367 list_del_init(&smc_sk(sk)->accept_q);
1368 spin_unlock(&par->accept_q_lock);
1369 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
1370 sock_put(sk); /* sock_hold in smc_accept_enqueue */
1373 /* remove a sock from the accept queue to bind it to a new socket created
1374 * for a socket accept call from user space
1376 struct sock *smc_accept_dequeue(struct sock *parent,
1377 struct socket *new_sock)
1379 struct smc_sock *isk, *n;
1380 struct sock *new_sk;
1382 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
1383 new_sk = (struct sock *)isk;
1385 smc_accept_unlink(new_sk);
1386 if (new_sk->sk_state == SMC_CLOSED) {
1387 new_sk->sk_prot->unhash(new_sk);
1389 sock_release(isk->clcsock);
1390 isk->clcsock = NULL;
1392 sock_put(new_sk); /* final */
1396 sock_graft(new_sk, new_sock);
1397 if (isk->use_fallback) {
1398 smc_sk(new_sk)->clcsock->file = new_sock->file;
1399 isk->clcsock->file->private_data = isk->clcsock;
1407 /* clean up for a created but never accepted sock */
1408 void smc_close_non_accepted(struct sock *sk)
1410 struct smc_sock *smc = smc_sk(sk);
1412 sock_hold(sk); /* sock_put below */
1414 if (!sk->sk_lingertime)
1415 /* wait for peer closing */
1416 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
1419 sock_put(sk); /* sock_hold above */
1420 sock_put(sk); /* final sock_put */
1423 static int smcr_serv_conf_first_link(struct smc_sock *smc)
1425 struct smc_link *link = smc->conn.lnk;
1426 struct smc_llc_qentry *qentry;
1429 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
1430 return SMC_CLC_DECL_ERR_REGRMB;
1432 /* send CONFIRM LINK request to client over the RoCE fabric */
1433 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
1435 return SMC_CLC_DECL_TIMEOUT_CL;
1437 /* receive CONFIRM LINK response from client over the RoCE fabric */
1438 qentry = smc_llc_wait(link->lgr, link, SMC_LLC_WAIT_TIME,
1439 SMC_LLC_CONFIRM_LINK);
1441 struct smc_clc_msg_decline dclc;
1443 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
1444 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
1445 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
1447 smc_llc_save_peer_uid(qentry);
1448 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_RESP);
1449 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
1451 return SMC_CLC_DECL_RMBE_EC;
1453 /* confirm_rkey is implicit on 1st contact */
1454 smc->conn.rmb_desc->is_conf_rkey = true;
1456 smc_llc_link_active(link);
1457 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
1459 /* initial contact - try to establish second link */
1460 smc_llc_srv_add_link(link, NULL);
1464 /* listen worker: finish */
1465 static void smc_listen_out(struct smc_sock *new_smc)
1467 struct smc_sock *lsmc = new_smc->listen_smc;
1468 struct sock *newsmcsk = &new_smc->sk;
1470 if (lsmc->sk.sk_state == SMC_LISTEN) {
1471 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
1472 smc_accept_enqueue(&lsmc->sk, newsmcsk);
1473 release_sock(&lsmc->sk);
1474 } else { /* no longer listening */
1475 smc_close_non_accepted(newsmcsk);
1478 /* Wake up accept */
1479 lsmc->sk.sk_data_ready(&lsmc->sk);
1480 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
1483 /* listen worker: finish in state connected */
1484 static void smc_listen_out_connected(struct smc_sock *new_smc)
1486 struct sock *newsmcsk = &new_smc->sk;
1488 sk_refcnt_debug_inc(newsmcsk);
1489 if (newsmcsk->sk_state == SMC_INIT)
1490 newsmcsk->sk_state = SMC_ACTIVE;
1492 smc_listen_out(new_smc);
1495 /* listen worker: finish in error state */
1496 static void smc_listen_out_err(struct smc_sock *new_smc)
1498 struct sock *newsmcsk = &new_smc->sk;
1499 struct net *net = sock_net(newsmcsk);
1501 this_cpu_inc(net->smc.smc_stats->srv_hshake_err_cnt);
1502 if (newsmcsk->sk_state == SMC_INIT)
1503 sock_put(&new_smc->sk); /* passive closing */
1504 newsmcsk->sk_state = SMC_CLOSED;
1506 smc_listen_out(new_smc);
1509 /* listen worker: decline and fall back if possible */
1510 static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1511 int local_first, u8 version)
1513 /* RDMA setup failed, switch back to TCP */
1514 smc_conn_abort(new_smc, local_first);
1515 if (reason_code < 0) { /* error, no fallback possible */
1516 smc_listen_out_err(new_smc);
1519 smc_switch_to_fallback(new_smc, reason_code);
1520 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
1521 if (smc_clc_send_decline(new_smc, reason_code, version) < 0) {
1522 smc_listen_out_err(new_smc);
1526 smc_listen_out_connected(new_smc);
1529 /* listen worker: version checking */
1530 static int smc_listen_v2_check(struct smc_sock *new_smc,
1531 struct smc_clc_msg_proposal *pclc,
1532 struct smc_init_info *ini)
1534 struct smc_clc_smcd_v2_extension *pclc_smcd_v2_ext;
1535 struct smc_clc_v2_extension *pclc_v2_ext;
1536 int rc = SMC_CLC_DECL_PEERNOSMC;
1538 ini->smc_type_v1 = pclc->hdr.typev1;
1539 ini->smc_type_v2 = pclc->hdr.typev2;
1540 ini->smcd_version = smcd_indicated(ini->smc_type_v1) ? SMC_V1 : 0;
1541 ini->smcr_version = smcr_indicated(ini->smc_type_v1) ? SMC_V1 : 0;
1542 if (pclc->hdr.version > SMC_V1) {
1543 if (smcd_indicated(ini->smc_type_v2))
1544 ini->smcd_version |= SMC_V2;
1545 if (smcr_indicated(ini->smc_type_v2))
1546 ini->smcr_version |= SMC_V2;
1548 if (!(ini->smcd_version & SMC_V2) && !(ini->smcr_version & SMC_V2)) {
1549 rc = SMC_CLC_DECL_PEERNOSMC;
1552 pclc_v2_ext = smc_get_clc_v2_ext(pclc);
1554 ini->smcd_version &= ~SMC_V2;
1555 ini->smcr_version &= ~SMC_V2;
1556 rc = SMC_CLC_DECL_NOV2EXT;
1559 pclc_smcd_v2_ext = smc_get_clc_smcd_v2_ext(pclc_v2_ext);
1560 if (ini->smcd_version & SMC_V2) {
1561 if (!smc_ism_is_v2_capable()) {
1562 ini->smcd_version &= ~SMC_V2;
1563 rc = SMC_CLC_DECL_NOISM2SUPP;
1564 } else if (!pclc_smcd_v2_ext) {
1565 ini->smcd_version &= ~SMC_V2;
1566 rc = SMC_CLC_DECL_NOV2DEXT;
1567 } else if (!pclc_v2_ext->hdr.eid_cnt &&
1568 !pclc_v2_ext->hdr.flag.seid) {
1569 ini->smcd_version &= ~SMC_V2;
1570 rc = SMC_CLC_DECL_NOUEID;
1573 if (ini->smcr_version & SMC_V2) {
1574 if (!pclc_v2_ext->hdr.eid_cnt) {
1575 ini->smcr_version &= ~SMC_V2;
1576 rc = SMC_CLC_DECL_NOUEID;
1581 if (!ini->smcd_version && !ini->smcr_version)
1587 /* listen worker: check prefixes */
1588 static int smc_listen_prfx_check(struct smc_sock *new_smc,
1589 struct smc_clc_msg_proposal *pclc)
1591 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1592 struct socket *newclcsock = new_smc->clcsock;
1594 if (pclc->hdr.typev1 == SMC_TYPE_N)
1596 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1597 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
1598 return SMC_CLC_DECL_DIFFPREFIX;
1603 /* listen worker: initialize connection and buffers */
1604 static int smc_listen_rdma_init(struct smc_sock *new_smc,
1605 struct smc_init_info *ini)
1609 /* allocate connection / link group */
1610 rc = smc_conn_create(new_smc, ini);
1614 /* create send buffer and rmb */
1615 if (smc_buf_create(new_smc, false))
1616 return SMC_CLC_DECL_MEM;
1621 /* listen worker: initialize connection and buffers for SMC-D */
1622 static int smc_listen_ism_init(struct smc_sock *new_smc,
1623 struct smc_init_info *ini)
1627 rc = smc_conn_create(new_smc, ini);
1631 /* Create send and receive buffers */
1632 rc = smc_buf_create(new_smc, true);
1634 smc_conn_abort(new_smc, ini->first_contact_local);
1635 return (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB :
1642 static bool smc_is_already_selected(struct smcd_dev *smcd,
1643 struct smc_init_info *ini,
1648 for (i = 0; i < matches; i++)
1649 if (smcd == ini->ism_dev[i])
1655 /* check for ISM devices matching proposed ISM devices */
1656 static void smc_check_ism_v2_match(struct smc_init_info *ini,
1657 u16 proposed_chid, u64 proposed_gid,
1658 unsigned int *matches)
1660 struct smcd_dev *smcd;
1662 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
1663 if (smcd->going_away)
1665 if (smc_is_already_selected(smcd, ini, *matches))
1667 if (smc_ism_get_chid(smcd) == proposed_chid &&
1668 !smc_ism_cantalk(proposed_gid, ISM_RESERVED_VLANID, smcd)) {
1669 ini->ism_peer_gid[*matches] = proposed_gid;
1670 ini->ism_dev[*matches] = smcd;
1677 static void smc_find_ism_store_rc(u32 rc, struct smc_init_info *ini)
1683 static void smc_find_ism_v2_device_serv(struct smc_sock *new_smc,
1684 struct smc_clc_msg_proposal *pclc,
1685 struct smc_init_info *ini)
1687 struct smc_clc_smcd_v2_extension *smcd_v2_ext;
1688 struct smc_clc_v2_extension *smc_v2_ext;
1689 struct smc_clc_msg_smcd *pclc_smcd;
1690 unsigned int matches = 0;
1695 if (!(ini->smcd_version & SMC_V2) || !smcd_indicated(ini->smc_type_v2))
1698 pclc_smcd = smc_get_clc_msg_smcd(pclc);
1699 smc_v2_ext = smc_get_clc_v2_ext(pclc);
1700 smcd_v2_ext = smc_get_clc_smcd_v2_ext(smc_v2_ext);
1702 mutex_lock(&smcd_dev_list.mutex);
1703 if (pclc_smcd->ism.chid)
1704 /* check for ISM device matching proposed native ISM device */
1705 smc_check_ism_v2_match(ini, ntohs(pclc_smcd->ism.chid),
1706 ntohll(pclc_smcd->ism.gid), &matches);
1707 for (i = 1; i <= smc_v2_ext->hdr.ism_gid_cnt; i++) {
1708 /* check for ISM devices matching proposed non-native ISM
1711 smc_check_ism_v2_match(ini,
1712 ntohs(smcd_v2_ext->gidchid[i - 1].chid),
1713 ntohll(smcd_v2_ext->gidchid[i - 1].gid),
1716 mutex_unlock(&smcd_dev_list.mutex);
1718 if (!ini->ism_dev[0]) {
1719 smc_find_ism_store_rc(SMC_CLC_DECL_NOSMCD2DEV, ini);
1723 smc_ism_get_system_eid(&eid);
1724 if (!smc_clc_match_eid(ini->negotiated_eid, smc_v2_ext,
1725 smcd_v2_ext->system_eid, eid))
1728 /* separate - outside the smcd_dev_list.lock */
1729 smcd_version = ini->smcd_version;
1730 for (i = 0; i < matches; i++) {
1731 ini->smcd_version = SMC_V2;
1732 ini->is_smcd = true;
1733 ini->ism_selected = i;
1734 rc = smc_listen_ism_init(new_smc, ini);
1736 smc_find_ism_store_rc(rc, ini);
1737 /* try next active ISM device */
1740 return; /* matching and usable V2 ISM device found */
1742 /* no V2 ISM device could be initialized */
1743 ini->smcd_version = smcd_version; /* restore original value */
1744 ini->negotiated_eid[0] = 0;
1747 ini->smcd_version &= ~SMC_V2;
1748 ini->ism_dev[0] = NULL;
1749 ini->is_smcd = false;
1752 static void smc_find_ism_v1_device_serv(struct smc_sock *new_smc,
1753 struct smc_clc_msg_proposal *pclc,
1754 struct smc_init_info *ini)
1756 struct smc_clc_msg_smcd *pclc_smcd = smc_get_clc_msg_smcd(pclc);
1759 /* check if ISM V1 is available */
1760 if (!(ini->smcd_version & SMC_V1) || !smcd_indicated(ini->smc_type_v1))
1762 ini->is_smcd = true; /* prepare ISM check */
1763 ini->ism_peer_gid[0] = ntohll(pclc_smcd->ism.gid);
1764 rc = smc_find_ism_device(new_smc, ini);
1767 ini->ism_selected = 0;
1768 rc = smc_listen_ism_init(new_smc, ini);
1770 return; /* V1 ISM device found */
1773 smc_find_ism_store_rc(rc, ini);
1774 ini->smcd_version &= ~SMC_V1;
1775 ini->ism_dev[0] = NULL;
1776 ini->is_smcd = false;
1779 /* listen worker: register buffers */
1780 static int smc_listen_rdma_reg(struct smc_sock *new_smc, bool local_first)
1782 struct smc_connection *conn = &new_smc->conn;
1785 if (smcr_lgr_reg_rmbs(conn->lnk, conn->rmb_desc))
1786 return SMC_CLC_DECL_ERR_REGRMB;
1788 smc_rmb_sync_sg_for_device(&new_smc->conn);
1793 static void smc_find_rdma_v2_device_serv(struct smc_sock *new_smc,
1794 struct smc_clc_msg_proposal *pclc,
1795 struct smc_init_info *ini)
1797 struct smc_clc_v2_extension *smc_v2_ext;
1801 if (!(ini->smcr_version & SMC_V2) || !smcr_indicated(ini->smc_type_v2))
1804 smc_v2_ext = smc_get_clc_v2_ext(pclc);
1805 if (!smc_clc_match_eid(ini->negotiated_eid, smc_v2_ext, NULL, NULL))
1808 /* prepare RDMA check */
1809 memcpy(ini->peer_systemid, pclc->lcl.id_for_peer, SMC_SYSTEMID_LEN);
1810 memcpy(ini->peer_gid, smc_v2_ext->roce, SMC_GID_SIZE);
1811 memcpy(ini->peer_mac, pclc->lcl.mac, ETH_ALEN);
1812 ini->check_smcrv2 = true;
1813 ini->smcrv2.clc_sk = new_smc->clcsock->sk;
1814 ini->smcrv2.saddr = new_smc->clcsock->sk->sk_rcv_saddr;
1815 ini->smcrv2.daddr = smc_ib_gid_to_ipv4(smc_v2_ext->roce);
1816 rc = smc_find_rdma_device(new_smc, ini);
1818 smc_find_ism_store_rc(rc, ini);
1821 if (!ini->smcrv2.uses_gateway)
1822 memcpy(ini->smcrv2.nexthop_mac, pclc->lcl.mac, ETH_ALEN);
1824 smcr_version = ini->smcr_version;
1825 ini->smcr_version = SMC_V2;
1826 rc = smc_listen_rdma_init(new_smc, ini);
1828 rc = smc_listen_rdma_reg(new_smc, ini->first_contact_local);
1831 ini->smcr_version = smcr_version;
1832 smc_find_ism_store_rc(rc, ini);
1835 ini->smcr_version &= ~SMC_V2;
1836 ini->check_smcrv2 = false;
1839 static int smc_find_rdma_v1_device_serv(struct smc_sock *new_smc,
1840 struct smc_clc_msg_proposal *pclc,
1841 struct smc_init_info *ini)
1845 if (!(ini->smcr_version & SMC_V1) || !smcr_indicated(ini->smc_type_v1))
1846 return SMC_CLC_DECL_NOSMCDEV;
1848 /* prepare RDMA check */
1849 memcpy(ini->peer_systemid, pclc->lcl.id_for_peer, SMC_SYSTEMID_LEN);
1850 memcpy(ini->peer_gid, pclc->lcl.gid, SMC_GID_SIZE);
1851 memcpy(ini->peer_mac, pclc->lcl.mac, ETH_ALEN);
1852 rc = smc_find_rdma_device(new_smc, ini);
1854 /* no RDMA device found */
1855 return SMC_CLC_DECL_NOSMCDEV;
1857 rc = smc_listen_rdma_init(new_smc, ini);
1860 return smc_listen_rdma_reg(new_smc, ini->first_contact_local);
1863 /* determine the local device matching to proposal */
1864 static int smc_listen_find_device(struct smc_sock *new_smc,
1865 struct smc_clc_msg_proposal *pclc,
1866 struct smc_init_info *ini)
1870 /* check for ISM device matching V2 proposed device */
1871 smc_find_ism_v2_device_serv(new_smc, pclc, ini);
1872 if (ini->ism_dev[0])
1875 /* check for matching IP prefix and subnet length (V1) */
1876 prfx_rc = smc_listen_prfx_check(new_smc, pclc);
1878 smc_find_ism_store_rc(prfx_rc, ini);
1880 /* get vlan id from IP device */
1881 if (smc_vlan_by_tcpsk(new_smc->clcsock, ini))
1882 return ini->rc ?: SMC_CLC_DECL_GETVLANERR;
1884 /* check for ISM device matching V1 proposed device */
1886 smc_find_ism_v1_device_serv(new_smc, pclc, ini);
1887 if (ini->ism_dev[0])
1890 if (!smcr_indicated(pclc->hdr.typev1) &&
1891 !smcr_indicated(pclc->hdr.typev2))
1892 /* skip RDMA and decline */
1893 return ini->rc ?: SMC_CLC_DECL_NOSMCDDEV;
1895 /* check if RDMA V2 is available */
1896 smc_find_rdma_v2_device_serv(new_smc, pclc, ini);
1897 if (ini->smcrv2.ib_dev_v2)
1900 /* check if RDMA V1 is available */
1904 rc = smc_find_rdma_v1_device_serv(new_smc, pclc, ini);
1905 smc_find_ism_store_rc(rc, ini);
1906 return (!rc) ? 0 : ini->rc;
1908 return SMC_CLC_DECL_NOSMCDEV;
1911 /* listen worker: finish RDMA setup */
1912 static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1913 struct smc_clc_msg_accept_confirm *cclc,
1915 struct smc_init_info *ini)
1917 struct smc_link *link = new_smc->conn.lnk;
1918 int reason_code = 0;
1921 smc_link_save_peer_info(link, cclc, ini);
1923 if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc))
1924 return SMC_CLC_DECL_ERR_RTOK;
1927 if (smc_ib_ready_link(link))
1928 return SMC_CLC_DECL_ERR_RDYLNK;
1929 /* QP confirmation over RoCE fabric */
1930 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
1931 reason_code = smcr_serv_conf_first_link(new_smc);
1932 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
1937 /* setup for connection of server */
1938 static void smc_listen_work(struct work_struct *work)
1940 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1942 struct socket *newclcsock = new_smc->clcsock;
1943 struct smc_clc_msg_accept_confirm *cclc;
1944 struct smc_clc_msg_proposal_area *buf;
1945 struct smc_clc_msg_proposal *pclc;
1946 struct smc_init_info *ini = NULL;
1947 u8 proposal_version = SMC_V1;
1951 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1952 return smc_listen_out_err(new_smc);
1954 if (new_smc->use_fallback) {
1955 smc_listen_out_connected(new_smc);
1959 /* check if peer is smc capable */
1960 if (!tcp_sk(newclcsock->sk)->syn_smc) {
1961 smc_switch_to_fallback(new_smc, SMC_CLC_DECL_PEERNOSMC);
1962 smc_listen_out_connected(new_smc);
1966 /* do inband token exchange -
1967 * wait for and receive SMC Proposal CLC message
1969 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
1971 rc = SMC_CLC_DECL_MEM;
1974 pclc = (struct smc_clc_msg_proposal *)buf;
1975 rc = smc_clc_wait_msg(new_smc, pclc, sizeof(*buf),
1976 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
1980 if (pclc->hdr.version > SMC_V1)
1981 proposal_version = SMC_V2;
1983 /* IPSec connections opt out of SMC optimizations */
1984 if (using_ipsec(new_smc)) {
1985 rc = SMC_CLC_DECL_IPSEC;
1989 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
1991 rc = SMC_CLC_DECL_MEM;
1995 /* initial version checking */
1996 rc = smc_listen_v2_check(new_smc, pclc, ini);
2000 mutex_lock(&smc_server_lgr_pending);
2001 smc_close_init(new_smc);
2002 smc_rx_init(new_smc);
2003 smc_tx_init(new_smc);
2005 /* determine ISM or RoCE device used for connection */
2006 rc = smc_listen_find_device(new_smc, pclc, ini);
2010 /* send SMC Accept CLC message */
2011 accept_version = ini->is_smcd ? ini->smcd_version : ini->smcr_version;
2012 rc = smc_clc_send_accept(new_smc, ini->first_contact_local,
2013 accept_version, ini->negotiated_eid);
2017 /* SMC-D does not need this lock any more */
2019 mutex_unlock(&smc_server_lgr_pending);
2021 /* receive SMC Confirm CLC message */
2022 memset(buf, 0, sizeof(*buf));
2023 cclc = (struct smc_clc_msg_accept_confirm *)buf;
2024 rc = smc_clc_wait_msg(new_smc, cclc, sizeof(*buf),
2025 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
2033 if (!ini->is_smcd) {
2034 rc = smc_listen_rdma_finish(new_smc, cclc,
2035 ini->first_contact_local, ini);
2038 mutex_unlock(&smc_server_lgr_pending);
2040 smc_conn_save_peer_info(new_smc, cclc);
2041 smc_listen_out_connected(new_smc);
2042 SMC_STAT_SERV_SUCC_INC(sock_net(newclcsock->sk), ini);
2046 mutex_unlock(&smc_server_lgr_pending);
2048 smc_listen_decline(new_smc, rc, ini ? ini->first_contact_local : 0,
2055 static void smc_tcp_listen_work(struct work_struct *work)
2057 struct smc_sock *lsmc = container_of(work, struct smc_sock,
2059 struct sock *lsk = &lsmc->sk;
2060 struct smc_sock *new_smc;
2064 while (lsk->sk_state == SMC_LISTEN) {
2065 rc = smc_clcsock_accept(lsmc, &new_smc);
2066 if (rc) /* clcsock accept queue empty or error */
2071 new_smc->listen_smc = lsmc;
2072 new_smc->use_fallback = lsmc->use_fallback;
2073 new_smc->fallback_rsn = lsmc->fallback_rsn;
2074 sock_hold(lsk); /* sock_put in smc_listen_work */
2075 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
2076 smc_copy_sock_settings_to_smc(new_smc);
2077 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
2078 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
2079 sock_hold(&new_smc->sk); /* sock_put in passive closing */
2080 if (!queue_work(smc_hs_wq, &new_smc->smc_listen_work))
2081 sock_put(&new_smc->sk);
2086 sock_put(&lsmc->sk); /* sock_hold in smc_clcsock_data_ready() */
2089 static void smc_clcsock_data_ready(struct sock *listen_clcsock)
2091 struct smc_sock *lsmc;
2093 lsmc = (struct smc_sock *)
2094 ((uintptr_t)listen_clcsock->sk_user_data & ~SK_USER_DATA_NOCOPY);
2097 lsmc->clcsk_data_ready(listen_clcsock);
2098 if (lsmc->sk.sk_state == SMC_LISTEN) {
2099 sock_hold(&lsmc->sk); /* sock_put in smc_tcp_listen_work() */
2100 if (!queue_work(smc_hs_wq, &lsmc->tcp_listen_work))
2101 sock_put(&lsmc->sk);
2105 static int smc_listen(struct socket *sock, int backlog)
2107 struct sock *sk = sock->sk;
2108 struct smc_sock *smc;
2115 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
2116 smc->connect_nonblock)
2120 if (sk->sk_state == SMC_LISTEN) {
2121 sk->sk_max_ack_backlog = backlog;
2124 /* some socket options are handled in core, so we could not apply
2125 * them to the clc socket -- copy smc socket options to clc socket
2127 smc_copy_sock_settings_to_clc(smc);
2128 if (!smc->use_fallback)
2129 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
2131 /* save original sk_data_ready function and establish
2132 * smc-specific sk_data_ready function
2134 smc->clcsk_data_ready = smc->clcsock->sk->sk_data_ready;
2135 smc->clcsock->sk->sk_data_ready = smc_clcsock_data_ready;
2136 smc->clcsock->sk->sk_user_data =
2137 (void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY);
2138 rc = kernel_listen(smc->clcsock, backlog);
2140 smc->clcsock->sk->sk_data_ready = smc->clcsk_data_ready;
2143 sk->sk_max_ack_backlog = backlog;
2144 sk->sk_ack_backlog = 0;
2145 sk->sk_state = SMC_LISTEN;
2152 static int smc_accept(struct socket *sock, struct socket *new_sock,
2153 int flags, bool kern)
2155 struct sock *sk = sock->sk, *nsk;
2156 DECLARE_WAITQUEUE(wait, current);
2157 struct smc_sock *lsmc;
2162 sock_hold(sk); /* sock_put below */
2165 if (lsmc->sk.sk_state != SMC_LISTEN) {
2171 /* Wait for an incoming connection */
2172 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2173 add_wait_queue_exclusive(sk_sleep(sk), &wait);
2174 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
2175 set_current_state(TASK_INTERRUPTIBLE);
2181 timeo = schedule_timeout(timeo);
2182 /* wakeup by sk_data_ready in smc_listen_work() */
2183 sched_annotate_sleep();
2185 if (signal_pending(current)) {
2186 rc = sock_intr_errno(timeo);
2190 set_current_state(TASK_RUNNING);
2191 remove_wait_queue(sk_sleep(sk), &wait);
2194 rc = sock_error(nsk);
2199 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
2200 /* wait till data arrives on the socket */
2201 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
2203 if (smc_sk(nsk)->use_fallback) {
2204 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
2207 if (skb_queue_empty(&clcsk->sk_receive_queue))
2208 sk_wait_data(clcsk, &timeo, NULL);
2209 release_sock(clcsk);
2210 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
2212 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
2218 sock_put(sk); /* sock_hold above */
2222 static int smc_getname(struct socket *sock, struct sockaddr *addr,
2225 struct smc_sock *smc;
2227 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
2228 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
2231 smc = smc_sk(sock->sk);
2233 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
2236 static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
2238 struct sock *sk = sock->sk;
2239 struct smc_sock *smc;
2244 if ((sk->sk_state != SMC_ACTIVE) &&
2245 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2246 (sk->sk_state != SMC_INIT))
2249 if (msg->msg_flags & MSG_FASTOPEN) {
2250 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
2251 smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP);
2258 if (smc->use_fallback) {
2259 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
2261 rc = smc_tx_sendmsg(smc, msg, len);
2262 SMC_STAT_TX_PAYLOAD(smc, len, rc);
2269 static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
2272 struct sock *sk = sock->sk;
2273 struct smc_sock *smc;
2278 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2279 /* socket was connected before, no more data to read */
2283 if ((sk->sk_state == SMC_INIT) ||
2284 (sk->sk_state == SMC_LISTEN) ||
2285 (sk->sk_state == SMC_CLOSED))
2288 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2293 if (smc->use_fallback) {
2294 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
2296 msg->msg_namelen = 0;
2297 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
2298 SMC_STAT_RX_PAYLOAD(smc, rc, rc);
2306 static __poll_t smc_accept_poll(struct sock *parent)
2308 struct smc_sock *isk = smc_sk(parent);
2311 spin_lock(&isk->accept_q_lock);
2312 if (!list_empty(&isk->accept_q))
2313 mask = EPOLLIN | EPOLLRDNORM;
2314 spin_unlock(&isk->accept_q_lock);
2319 static __poll_t smc_poll(struct file *file, struct socket *sock,
2322 struct sock *sk = sock->sk;
2323 struct smc_sock *smc;
2329 smc = smc_sk(sock->sk);
2330 if (smc->use_fallback) {
2331 /* delegate to CLC child sock */
2332 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
2333 sk->sk_err = smc->clcsock->sk->sk_err;
2335 if (sk->sk_state != SMC_CLOSED)
2336 sock_poll_wait(file, sock, wait);
2339 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
2340 (sk->sk_state == SMC_CLOSED))
2342 if (sk->sk_state == SMC_LISTEN) {
2343 /* woken up by sk_data_ready in smc_listen_work() */
2344 mask |= smc_accept_poll(sk);
2345 } else if (smc->use_fallback) { /* as result of connect_work()*/
2346 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
2348 sk->sk_err = smc->clcsock->sk->sk_err;
2350 if ((sk->sk_state != SMC_INIT &&
2351 atomic_read(&smc->conn.sndbuf_space)) ||
2352 sk->sk_shutdown & SEND_SHUTDOWN) {
2353 mask |= EPOLLOUT | EPOLLWRNORM;
2355 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
2356 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
2358 if (atomic_read(&smc->conn.bytes_to_rcv))
2359 mask |= EPOLLIN | EPOLLRDNORM;
2360 if (sk->sk_shutdown & RCV_SHUTDOWN)
2361 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
2362 if (sk->sk_state == SMC_APPCLOSEWAIT1)
2364 if (smc->conn.urg_state == SMC_URG_VALID)
2372 static int smc_shutdown(struct socket *sock, int how)
2374 struct sock *sk = sock->sk;
2375 bool do_shutdown = true;
2376 struct smc_sock *smc;
2383 if ((how < SHUT_RD) || (how > SHUT_RDWR))
2389 if ((sk->sk_state != SMC_ACTIVE) &&
2390 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
2391 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
2392 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2393 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
2394 (sk->sk_state != SMC_APPFINCLOSEWAIT))
2396 if (smc->use_fallback) {
2397 rc = kernel_sock_shutdown(smc->clcsock, how);
2398 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
2399 if (sk->sk_shutdown == SHUTDOWN_MASK)
2400 sk->sk_state = SMC_CLOSED;
2404 case SHUT_RDWR: /* shutdown in both directions */
2405 old_state = sk->sk_state;
2406 rc = smc_close_active(smc);
2407 if (old_state == SMC_ACTIVE &&
2408 sk->sk_state == SMC_PEERCLOSEWAIT1)
2409 do_shutdown = false;
2412 rc = smc_close_shutdown_write(smc);
2416 /* nothing more to do because peer is not involved */
2419 if (do_shutdown && smc->clcsock)
2420 rc1 = kernel_sock_shutdown(smc->clcsock, how);
2421 /* map sock_shutdown_cmd constants to sk_shutdown value range */
2422 sk->sk_shutdown |= how + 1;
2426 return rc ? rc : rc1;
2429 static int smc_setsockopt(struct socket *sock, int level, int optname,
2430 sockptr_t optval, unsigned int optlen)
2432 struct sock *sk = sock->sk;
2433 struct smc_sock *smc;
2436 if (level == SOL_TCP && optname == TCP_ULP)
2441 /* generic setsockopts reaching us here always apply to the
2444 if (unlikely(!smc->clcsock->ops->setsockopt))
2447 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
2449 if (smc->clcsock->sk->sk_err) {
2450 sk->sk_err = smc->clcsock->sk->sk_err;
2451 sk_error_report(sk);
2454 if (optlen < sizeof(int))
2456 if (copy_from_sockptr(&val, optval, sizeof(int)))
2460 if (rc || smc->use_fallback)
2464 case TCP_FASTOPEN_CONNECT:
2465 case TCP_FASTOPEN_KEY:
2466 case TCP_FASTOPEN_NO_COOKIE:
2467 /* option not supported by SMC */
2468 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
2469 smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP);
2475 if (sk->sk_state != SMC_INIT &&
2476 sk->sk_state != SMC_LISTEN &&
2477 sk->sk_state != SMC_CLOSED) {
2479 SMC_STAT_INC(smc, ndly_cnt);
2480 mod_delayed_work(smc->conn.lgr->tx_wq,
2481 &smc->conn.tx_work, 0);
2486 if (sk->sk_state != SMC_INIT &&
2487 sk->sk_state != SMC_LISTEN &&
2488 sk->sk_state != SMC_CLOSED) {
2490 SMC_STAT_INC(smc, cork_cnt);
2491 mod_delayed_work(smc->conn.lgr->tx_wq,
2492 &smc->conn.tx_work, 0);
2496 case TCP_DEFER_ACCEPT:
2497 smc->sockopt_defer_accept = val;
2508 static int smc_getsockopt(struct socket *sock, int level, int optname,
2509 char __user *optval, int __user *optlen)
2511 struct smc_sock *smc;
2513 smc = smc_sk(sock->sk);
2514 /* socket options apply to the CLC socket */
2515 if (unlikely(!smc->clcsock->ops->getsockopt))
2517 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
2521 static int smc_ioctl(struct socket *sock, unsigned int cmd,
2524 union smc_host_cursor cons, urg;
2525 struct smc_connection *conn;
2526 struct smc_sock *smc;
2529 smc = smc_sk(sock->sk);
2531 lock_sock(&smc->sk);
2532 if (smc->use_fallback) {
2533 if (!smc->clcsock) {
2534 release_sock(&smc->sk);
2537 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
2538 release_sock(&smc->sk);
2542 case SIOCINQ: /* same as FIONREAD */
2543 if (smc->sk.sk_state == SMC_LISTEN) {
2544 release_sock(&smc->sk);
2547 if (smc->sk.sk_state == SMC_INIT ||
2548 smc->sk.sk_state == SMC_CLOSED)
2551 answ = atomic_read(&smc->conn.bytes_to_rcv);
2554 /* output queue size (not send + not acked) */
2555 if (smc->sk.sk_state == SMC_LISTEN) {
2556 release_sock(&smc->sk);
2559 if (smc->sk.sk_state == SMC_INIT ||
2560 smc->sk.sk_state == SMC_CLOSED)
2563 answ = smc->conn.sndbuf_desc->len -
2564 atomic_read(&smc->conn.sndbuf_space);
2567 /* output queue size (not send only) */
2568 if (smc->sk.sk_state == SMC_LISTEN) {
2569 release_sock(&smc->sk);
2572 if (smc->sk.sk_state == SMC_INIT ||
2573 smc->sk.sk_state == SMC_CLOSED)
2576 answ = smc_tx_prepared_sends(&smc->conn);
2579 if (smc->sk.sk_state == SMC_LISTEN) {
2580 release_sock(&smc->sk);
2583 if (smc->sk.sk_state == SMC_INIT ||
2584 smc->sk.sk_state == SMC_CLOSED) {
2587 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
2588 smc_curs_copy(&urg, &conn->urg_curs, conn);
2589 answ = smc_curs_diff(conn->rmb_desc->len,
2594 release_sock(&smc->sk);
2595 return -ENOIOCTLCMD;
2597 release_sock(&smc->sk);
2599 return put_user(answ, (int __user *)arg);
2602 static ssize_t smc_sendpage(struct socket *sock, struct page *page,
2603 int offset, size_t size, int flags)
2605 struct sock *sk = sock->sk;
2606 struct smc_sock *smc;
2611 if (sk->sk_state != SMC_ACTIVE) {
2616 if (smc->use_fallback) {
2617 rc = kernel_sendpage(smc->clcsock, page, offset,
2620 SMC_STAT_INC(smc, sendpage_cnt);
2621 rc = sock_no_sendpage(sock, page, offset, size, flags);
2628 /* Map the affected portions of the rmbe into an spd, note the number of bytes
2629 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
2630 * updates till whenever a respective page has been fully processed.
2631 * Note that subsequent recv() calls have to wait till all splice() processing
2634 static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
2635 struct pipe_inode_info *pipe, size_t len,
2638 struct sock *sk = sock->sk;
2639 struct smc_sock *smc;
2644 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2645 /* socket was connected before, no more data to read */
2649 if (sk->sk_state == SMC_INIT ||
2650 sk->sk_state == SMC_LISTEN ||
2651 sk->sk_state == SMC_CLOSED)
2654 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2659 if (smc->use_fallback) {
2660 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
2667 if (flags & SPLICE_F_NONBLOCK)
2668 flags = MSG_DONTWAIT;
2671 SMC_STAT_INC(smc, splice_cnt);
2672 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
2680 /* must look like tcp */
2681 static const struct proto_ops smc_sock_ops = {
2683 .owner = THIS_MODULE,
2684 .release = smc_release,
2686 .connect = smc_connect,
2687 .socketpair = sock_no_socketpair,
2688 .accept = smc_accept,
2689 .getname = smc_getname,
2692 .listen = smc_listen,
2693 .shutdown = smc_shutdown,
2694 .setsockopt = smc_setsockopt,
2695 .getsockopt = smc_getsockopt,
2696 .sendmsg = smc_sendmsg,
2697 .recvmsg = smc_recvmsg,
2698 .mmap = sock_no_mmap,
2699 .sendpage = smc_sendpage,
2700 .splice_read = smc_splice_read,
2703 static int smc_create(struct net *net, struct socket *sock, int protocol,
2706 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
2707 struct smc_sock *smc;
2711 rc = -ESOCKTNOSUPPORT;
2712 if (sock->type != SOCK_STREAM)
2715 rc = -EPROTONOSUPPORT;
2716 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
2720 sock->ops = &smc_sock_ops;
2721 sk = smc_sock_alloc(net, sock, protocol);
2725 /* create internal TCP socket for CLC handshake and fallback */
2727 smc->use_fallback = false; /* assume rdma capability first */
2728 smc->fallback_rsn = 0;
2729 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
2732 sk_common_release(sk);
2735 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
2736 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
2742 static const struct net_proto_family smc_sock_family_ops = {
2744 .owner = THIS_MODULE,
2745 .create = smc_create,
2748 unsigned int smc_net_id;
2750 static __net_init int smc_net_init(struct net *net)
2752 return smc_pnet_net_init(net);
2755 static void __net_exit smc_net_exit(struct net *net)
2757 smc_pnet_net_exit(net);
2760 static __net_init int smc_net_stat_init(struct net *net)
2762 return smc_stats_init(net);
2765 static void __net_exit smc_net_stat_exit(struct net *net)
2767 smc_stats_exit(net);
2770 static struct pernet_operations smc_net_ops = {
2771 .init = smc_net_init,
2772 .exit = smc_net_exit,
2774 .size = sizeof(struct smc_net),
2777 static struct pernet_operations smc_net_stat_ops = {
2778 .init = smc_net_stat_init,
2779 .exit = smc_net_stat_exit,
2782 static int __init smc_init(void)
2786 rc = register_pernet_subsys(&smc_net_ops);
2790 rc = register_pernet_subsys(&smc_net_stat_ops);
2799 goto out_pernet_subsys;
2801 rc = smc_pnet_init();
2806 smc_hs_wq = alloc_workqueue("smc_hs_wq", 0, 0);
2810 smc_close_wq = alloc_workqueue("smc_close_wq", 0, 0);
2812 goto out_alloc_hs_wq;
2814 rc = smc_core_init();
2816 pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
2820 rc = smc_llc_init();
2822 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
2826 rc = smc_cdc_init();
2828 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
2832 rc = proto_register(&smc_proto, 1);
2834 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
2838 rc = proto_register(&smc_proto6, 1);
2840 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2844 rc = sock_register(&smc_sock_family_ops);
2846 pr_err("%s: sock_register fails with %d\n", __func__, rc);
2849 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
2850 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
2852 rc = smc_ib_register_client();
2854 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2858 static_branch_enable(&tcp_have_smc);
2862 sock_unregister(PF_SMC);
2864 proto_unregister(&smc_proto6);
2866 proto_unregister(&smc_proto);
2870 destroy_workqueue(smc_close_wq);
2872 destroy_workqueue(smc_hs_wq);
2878 unregister_pernet_subsys(&smc_net_ops);
2883 static void __exit smc_exit(void)
2885 static_branch_disable(&tcp_have_smc);
2886 sock_unregister(PF_SMC);
2888 smc_ib_unregister_client();
2889 destroy_workqueue(smc_close_wq);
2890 destroy_workqueue(smc_hs_wq);
2891 proto_unregister(&smc_proto6);
2892 proto_unregister(&smc_proto);
2896 unregister_pernet_subsys(&smc_net_stat_ops);
2897 unregister_pernet_subsys(&smc_net_ops);
2901 module_init(smc_init);
2902 module_exit(smc_exit);
2905 MODULE_DESCRIPTION("smc socket address family");
2906 MODULE_LICENSE("GPL");
2907 MODULE_ALIAS_NETPROTO(PF_SMC);