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"
48 #include "smc_netlink.h"
51 #include "smc_close.h"
52 #include "smc_stats.h"
54 static DEFINE_MUTEX(smc_server_lgr_pending); /* serialize link group
57 static DEFINE_MUTEX(smc_client_lgr_pending); /* serialize link group
61 struct workqueue_struct *smc_hs_wq; /* wq for handshake work */
62 struct workqueue_struct *smc_close_wq; /* wq for close work */
64 static void smc_tcp_listen_work(struct work_struct *);
65 static void smc_connect_work(struct work_struct *);
67 static void smc_set_keepalive(struct sock *sk, int val)
69 struct smc_sock *smc = smc_sk(sk);
71 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
74 static struct smc_hashinfo smc_v4_hashinfo = {
75 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
78 static struct smc_hashinfo smc_v6_hashinfo = {
79 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
82 int smc_hash_sk(struct sock *sk)
84 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
85 struct hlist_head *head;
89 write_lock_bh(&h->lock);
90 sk_add_node(sk, head);
91 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
92 write_unlock_bh(&h->lock);
96 EXPORT_SYMBOL_GPL(smc_hash_sk);
98 void smc_unhash_sk(struct sock *sk)
100 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
102 write_lock_bh(&h->lock);
103 if (sk_del_node_init(sk))
104 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
105 write_unlock_bh(&h->lock);
107 EXPORT_SYMBOL_GPL(smc_unhash_sk);
109 struct proto smc_proto = {
111 .owner = THIS_MODULE,
112 .keepalive = smc_set_keepalive,
114 .unhash = smc_unhash_sk,
115 .obj_size = sizeof(struct smc_sock),
116 .h.smc_hash = &smc_v4_hashinfo,
117 .slab_flags = SLAB_TYPESAFE_BY_RCU,
119 EXPORT_SYMBOL_GPL(smc_proto);
121 struct proto smc_proto6 = {
123 .owner = THIS_MODULE,
124 .keepalive = smc_set_keepalive,
126 .unhash = smc_unhash_sk,
127 .obj_size = sizeof(struct smc_sock),
128 .h.smc_hash = &smc_v6_hashinfo,
129 .slab_flags = SLAB_TYPESAFE_BY_RCU,
131 EXPORT_SYMBOL_GPL(smc_proto6);
133 static void smc_restore_fallback_changes(struct smc_sock *smc)
135 if (smc->clcsock->file) { /* non-accepted sockets have no file yet */
136 smc->clcsock->file->private_data = smc->sk.sk_socket;
137 smc->clcsock->file = NULL;
141 static int __smc_release(struct smc_sock *smc)
143 struct sock *sk = &smc->sk;
146 if (!smc->use_fallback) {
147 rc = smc_close_active(smc);
148 sock_set_flag(sk, SOCK_DEAD);
149 sk->sk_shutdown |= SHUTDOWN_MASK;
151 if (sk->sk_state != SMC_CLOSED) {
152 if (sk->sk_state != SMC_LISTEN &&
153 sk->sk_state != SMC_INIT)
154 sock_put(sk); /* passive closing */
155 if (sk->sk_state == SMC_LISTEN) {
156 /* wake up clcsock accept */
157 rc = kernel_sock_shutdown(smc->clcsock,
160 sk->sk_state = SMC_CLOSED;
161 sk->sk_state_change(sk);
163 smc_restore_fallback_changes(smc);
166 sk->sk_prot->unhash(sk);
168 if (sk->sk_state == SMC_CLOSED) {
171 smc_clcsock_release(smc);
174 if (!smc->use_fallback)
175 smc_conn_free(&smc->conn);
181 static int smc_release(struct socket *sock)
183 struct sock *sk = sock->sk;
184 struct smc_sock *smc;
190 sock_hold(sk); /* sock_put below */
193 /* cleanup for a dangling non-blocking connect */
194 if (smc->connect_nonblock && sk->sk_state == SMC_INIT)
195 tcp_abort(smc->clcsock->sk, ECONNABORTED);
197 if (cancel_work_sync(&smc->connect_work))
198 sock_put(&smc->sk); /* sock_hold in smc_connect for passive closing */
200 if (sk->sk_state == SMC_LISTEN)
201 /* smc_close_non_accepted() is called and acquires
202 * sock lock for child sockets again
204 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
208 rc = __smc_release(smc);
215 sock_put(sk); /* sock_hold above */
216 sock_put(sk); /* final sock_put */
221 static void smc_destruct(struct sock *sk)
223 if (sk->sk_state != SMC_CLOSED)
225 if (!sock_flag(sk, SOCK_DEAD))
228 sk_refcnt_debug_dec(sk);
231 static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
234 struct smc_sock *smc;
238 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
239 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
243 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
244 sk->sk_state = SMC_INIT;
245 sk->sk_destruct = smc_destruct;
246 sk->sk_protocol = protocol;
248 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
249 INIT_WORK(&smc->connect_work, smc_connect_work);
250 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
251 INIT_LIST_HEAD(&smc->accept_q);
252 spin_lock_init(&smc->accept_q_lock);
253 spin_lock_init(&smc->conn.send_lock);
254 sk->sk_prot->hash(sk);
255 sk_refcnt_debug_inc(sk);
256 mutex_init(&smc->clcsock_release_lock);
261 static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
264 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
265 struct sock *sk = sock->sk;
266 struct smc_sock *smc;
271 /* replicate tests from inet_bind(), to be safe wrt. future changes */
273 if (addr_len < sizeof(struct sockaddr_in))
277 if (addr->sin_family != AF_INET &&
278 addr->sin_family != AF_INET6 &&
279 addr->sin_family != AF_UNSPEC)
281 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
282 if (addr->sin_family == AF_UNSPEC &&
283 addr->sin_addr.s_addr != htonl(INADDR_ANY))
288 /* Check if socket is already active */
290 if (sk->sk_state != SMC_INIT || smc->connect_nonblock)
293 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
294 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
302 static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
305 /* options we don't get control via setsockopt for */
306 nsk->sk_type = osk->sk_type;
307 nsk->sk_sndbuf = osk->sk_sndbuf;
308 nsk->sk_rcvbuf = osk->sk_rcvbuf;
309 nsk->sk_sndtimeo = osk->sk_sndtimeo;
310 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
311 nsk->sk_mark = osk->sk_mark;
312 nsk->sk_priority = osk->sk_priority;
313 nsk->sk_rcvlowat = osk->sk_rcvlowat;
314 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
315 nsk->sk_err = osk->sk_err;
317 nsk->sk_flags &= ~mask;
318 nsk->sk_flags |= osk->sk_flags & mask;
321 #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
322 (1UL << SOCK_KEEPOPEN) | \
323 (1UL << SOCK_LINGER) | \
324 (1UL << SOCK_BROADCAST) | \
325 (1UL << SOCK_TIMESTAMP) | \
326 (1UL << SOCK_DBG) | \
327 (1UL << SOCK_RCVTSTAMP) | \
328 (1UL << SOCK_RCVTSTAMPNS) | \
329 (1UL << SOCK_LOCALROUTE) | \
330 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
331 (1UL << SOCK_RXQ_OVFL) | \
332 (1UL << SOCK_WIFI_STATUS) | \
333 (1UL << SOCK_NOFCS) | \
334 (1UL << SOCK_FILTER_LOCKED) | \
335 (1UL << SOCK_TSTAMP_NEW))
336 /* copy only relevant settings and flags of SOL_SOCKET level from smc to
337 * clc socket (since smc is not called for these options from net/core)
339 static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
341 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
344 #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
345 (1UL << SOCK_KEEPOPEN) | \
346 (1UL << SOCK_LINGER) | \
348 /* copy only settings and flags relevant for smc from clc to smc socket */
349 static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
351 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
354 /* register the new rmb on all links */
355 static int smcr_lgr_reg_rmbs(struct smc_link *link,
356 struct smc_buf_desc *rmb_desc)
358 struct smc_link_group *lgr = link->lgr;
361 rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY);
364 /* protect against parallel smc_llc_cli_rkey_exchange() and
365 * parallel smcr_link_reg_rmb()
367 mutex_lock(&lgr->llc_conf_mutex);
368 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
369 if (!smc_link_active(&lgr->lnk[i]))
371 rc = smcr_link_reg_rmb(&lgr->lnk[i], rmb_desc);
376 /* exchange confirm_rkey msg with peer */
377 rc = smc_llc_do_confirm_rkey(link, rmb_desc);
382 rmb_desc->is_conf_rkey = true;
384 mutex_unlock(&lgr->llc_conf_mutex);
385 smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
389 static int smcr_clnt_conf_first_link(struct smc_sock *smc)
391 struct smc_link *link = smc->conn.lnk;
392 struct smc_llc_qentry *qentry;
395 /* receive CONFIRM LINK request from server over RoCE fabric */
396 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
397 SMC_LLC_CONFIRM_LINK);
399 struct smc_clc_msg_decline dclc;
401 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
402 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
403 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
405 smc_llc_save_peer_uid(qentry);
406 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_REQ);
407 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
409 return SMC_CLC_DECL_RMBE_EC;
411 rc = smc_ib_modify_qp_rts(link);
413 return SMC_CLC_DECL_ERR_RDYLNK;
415 smc_wr_remember_qp_attr(link);
417 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
418 return SMC_CLC_DECL_ERR_REGRMB;
420 /* confirm_rkey is implicit on 1st contact */
421 smc->conn.rmb_desc->is_conf_rkey = true;
423 /* send CONFIRM LINK response over RoCE fabric */
424 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
426 return SMC_CLC_DECL_TIMEOUT_CL;
428 smc_llc_link_active(link);
429 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
431 /* optional 2nd link, receive ADD LINK request from server */
432 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
435 struct smc_clc_msg_decline dclc;
437 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
438 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
440 rc = 0; /* no DECLINE received, go with one link */
443 smc_llc_flow_qentry_clr(&link->lgr->llc_flow_lcl);
444 smc_llc_cli_add_link(link, qentry);
448 static void smcr_conn_save_peer_info(struct smc_sock *smc,
449 struct smc_clc_msg_accept_confirm *clc)
451 int bufsize = smc_uncompress_bufsize(clc->r0.rmbe_size);
453 smc->conn.peer_rmbe_idx = clc->r0.rmbe_idx;
454 smc->conn.local_tx_ctrl.token = ntohl(clc->r0.rmbe_alert_token);
455 smc->conn.peer_rmbe_size = bufsize;
456 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
457 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
460 static bool smc_isascii(char *hostname)
464 for (i = 0; i < SMC_MAX_HOSTNAME_LEN; i++)
465 if (!isascii(hostname[i]))
470 static void smcd_conn_save_peer_info(struct smc_sock *smc,
471 struct smc_clc_msg_accept_confirm *clc)
473 int bufsize = smc_uncompress_bufsize(clc->d0.dmbe_size);
475 smc->conn.peer_rmbe_idx = clc->d0.dmbe_idx;
476 smc->conn.peer_token = clc->d0.token;
477 /* msg header takes up space in the buffer */
478 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
479 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
480 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
481 if (clc->hdr.version > SMC_V1 &&
482 (clc->hdr.typev2 & SMC_FIRST_CONTACT_MASK)) {
483 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
484 (struct smc_clc_msg_accept_confirm_v2 *)clc;
485 struct smc_clc_first_contact_ext *fce =
486 (struct smc_clc_first_contact_ext *)
487 (((u8 *)clc_v2) + sizeof(*clc_v2));
489 memcpy(smc->conn.lgr->negotiated_eid, clc_v2->eid,
491 smc->conn.lgr->peer_os = fce->os_type;
492 smc->conn.lgr->peer_smc_release = fce->release;
493 if (smc_isascii(fce->hostname))
494 memcpy(smc->conn.lgr->peer_hostname, fce->hostname,
495 SMC_MAX_HOSTNAME_LEN);
499 static void smc_conn_save_peer_info(struct smc_sock *smc,
500 struct smc_clc_msg_accept_confirm *clc)
502 if (smc->conn.lgr->is_smcd)
503 smcd_conn_save_peer_info(smc, clc);
505 smcr_conn_save_peer_info(smc, clc);
508 static void smc_link_save_peer_info(struct smc_link *link,
509 struct smc_clc_msg_accept_confirm *clc)
511 link->peer_qpn = ntoh24(clc->r0.qpn);
512 memcpy(link->peer_gid, clc->r0.lcl.gid, SMC_GID_SIZE);
513 memcpy(link->peer_mac, clc->r0.lcl.mac, sizeof(link->peer_mac));
514 link->peer_psn = ntoh24(clc->r0.psn);
515 link->peer_mtu = clc->r0.qp_mtu;
518 static void smc_stat_inc_fback_rsn_cnt(struct smc_sock *smc,
519 struct smc_stats_fback *fback_arr)
523 for (cnt = 0; cnt < SMC_MAX_FBACK_RSN_CNT; cnt++) {
524 if (fback_arr[cnt].fback_code == smc->fallback_rsn) {
525 fback_arr[cnt].count++;
528 if (!fback_arr[cnt].fback_code) {
529 fback_arr[cnt].fback_code = smc->fallback_rsn;
530 fback_arr[cnt].count++;
536 static void smc_stat_fallback(struct smc_sock *smc)
538 struct net *net = sock_net(&smc->sk);
540 mutex_lock(&net->smc.mutex_fback_rsn);
541 if (smc->listen_smc) {
542 smc_stat_inc_fback_rsn_cnt(smc, net->smc.fback_rsn->srv);
543 net->smc.fback_rsn->srv_fback_cnt++;
545 smc_stat_inc_fback_rsn_cnt(smc, net->smc.fback_rsn->clnt);
546 net->smc.fback_rsn->clnt_fback_cnt++;
548 mutex_unlock(&net->smc.mutex_fback_rsn);
551 static void smc_switch_to_fallback(struct smc_sock *smc, int reason_code)
553 wait_queue_head_t *smc_wait = sk_sleep(&smc->sk);
554 wait_queue_head_t *clc_wait = sk_sleep(smc->clcsock->sk);
557 smc->use_fallback = true;
558 smc->fallback_rsn = reason_code;
559 smc_stat_fallback(smc);
560 if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
561 smc->clcsock->file = smc->sk.sk_socket->file;
562 smc->clcsock->file->private_data = smc->clcsock;
563 smc->clcsock->wq.fasync_list =
564 smc->sk.sk_socket->wq.fasync_list;
566 /* There may be some entries remaining in
567 * smc socket->wq, which should be removed
568 * to clcsocket->wq during the fallback.
570 spin_lock_irqsave(&smc_wait->lock, flags);
571 spin_lock_nested(&clc_wait->lock, SINGLE_DEPTH_NESTING);
572 list_splice_init(&smc_wait->head, &clc_wait->head);
573 spin_unlock(&clc_wait->lock);
574 spin_unlock_irqrestore(&smc_wait->lock, flags);
578 /* fall back during connect */
579 static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
581 smc_switch_to_fallback(smc, reason_code);
582 smc_copy_sock_settings_to_clc(smc);
583 smc->connect_nonblock = 0;
584 if (smc->sk.sk_state == SMC_INIT)
585 smc->sk.sk_state = SMC_ACTIVE;
589 /* decline and fall back during connect */
590 static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code,
593 struct net *net = sock_net(&smc->sk);
596 if (reason_code < 0) { /* error, fallback is not possible */
597 this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
598 if (smc->sk.sk_state == SMC_INIT)
599 sock_put(&smc->sk); /* passive closing */
602 if (reason_code != SMC_CLC_DECL_PEERDECL) {
603 rc = smc_clc_send_decline(smc, reason_code, version);
605 this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
606 if (smc->sk.sk_state == SMC_INIT)
607 sock_put(&smc->sk); /* passive closing */
611 return smc_connect_fallback(smc, reason_code);
614 static void smc_conn_abort(struct smc_sock *smc, int local_first)
617 smc_lgr_cleanup_early(&smc->conn);
619 smc_conn_free(&smc->conn);
622 /* check if there is a rdma device available for this connection. */
623 /* called for connect and listen */
624 static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
626 /* PNET table look up: search active ib_device and port
627 * within same PNETID that also contains the ethernet device
628 * used for the internal TCP socket
630 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
632 return SMC_CLC_DECL_NOSMCRDEV;
636 /* check if there is an ISM device available for this connection. */
637 /* called for connect and listen */
638 static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
640 /* Find ISM device with same PNETID as connecting interface */
641 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
642 if (!ini->ism_dev[0])
643 return SMC_CLC_DECL_NOSMCDDEV;
645 ini->ism_chid[0] = smc_ism_get_chid(ini->ism_dev[0]);
649 /* is chid unique for the ism devices that are already determined? */
650 static bool smc_find_ism_v2_is_unique_chid(u16 chid, struct smc_init_info *ini,
653 int i = (!ini->ism_dev[0]) ? 1 : 0;
656 if (ini->ism_chid[i] == chid)
661 /* determine possible V2 ISM devices (either without PNETID or with PNETID plus
662 * PNETID matching net_device)
664 static int smc_find_ism_v2_device_clnt(struct smc_sock *smc,
665 struct smc_init_info *ini)
667 int rc = SMC_CLC_DECL_NOSMCDDEV;
668 struct smcd_dev *smcd;
672 if (smcd_indicated(ini->smc_type_v1))
673 rc = 0; /* already initialized for V1 */
674 mutex_lock(&smcd_dev_list.mutex);
675 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
676 if (smcd->going_away || smcd == ini->ism_dev[0])
678 chid = smc_ism_get_chid(smcd);
679 if (!smc_find_ism_v2_is_unique_chid(chid, ini, i))
681 if (!smc_pnet_is_pnetid_set(smcd->pnetid) ||
682 smc_pnet_is_ndev_pnetid(sock_net(&smc->sk), smcd->pnetid)) {
683 ini->ism_dev[i] = smcd;
684 ini->ism_chid[i] = chid;
688 if (i > SMC_MAX_ISM_DEVS)
692 mutex_unlock(&smcd_dev_list.mutex);
693 ini->ism_offered_cnt = i - 1;
694 if (!ini->ism_dev[0] && !ini->ism_dev[1])
695 ini->smcd_version = 0;
700 /* Check for VLAN ID and register it on ISM device just for CLC handshake */
701 static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
702 struct smc_init_info *ini)
704 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev[0], ini->vlan_id))
705 return SMC_CLC_DECL_ISMVLANERR;
709 static int smc_find_proposal_devices(struct smc_sock *smc,
710 struct smc_init_info *ini)
714 /* check if there is an ism device available */
715 if (ini->smcd_version & SMC_V1) {
716 if (smc_find_ism_device(smc, ini) ||
717 smc_connect_ism_vlan_setup(smc, ini)) {
718 if (ini->smc_type_v1 == SMC_TYPE_B)
719 ini->smc_type_v1 = SMC_TYPE_R;
721 ini->smc_type_v1 = SMC_TYPE_N;
722 } /* else ISM V1 is supported for this connection */
723 if (smc_find_rdma_device(smc, ini)) {
724 if (ini->smc_type_v1 == SMC_TYPE_B)
725 ini->smc_type_v1 = SMC_TYPE_D;
727 ini->smc_type_v1 = SMC_TYPE_N;
728 } /* else RDMA is supported for this connection */
730 if (smc_ism_is_v2_capable() && smc_find_ism_v2_device_clnt(smc, ini))
731 ini->smc_type_v2 = SMC_TYPE_N;
733 /* if neither ISM nor RDMA are supported, fallback */
734 if (!smcr_indicated(ini->smc_type_v1) &&
735 ini->smc_type_v1 == SMC_TYPE_N && ini->smc_type_v2 == SMC_TYPE_N)
736 rc = SMC_CLC_DECL_NOSMCDEV;
741 /* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
742 * used, the VLAN ID will be registered again during the connection setup.
744 static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc,
745 struct smc_init_info *ini)
747 if (!smcd_indicated(ini->smc_type_v1))
749 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev[0], ini->vlan_id))
750 return SMC_CLC_DECL_CNFERR;
754 #define SMC_CLC_MAX_ACCEPT_LEN \
755 (sizeof(struct smc_clc_msg_accept_confirm_v2) + \
756 sizeof(struct smc_clc_first_contact_ext) + \
757 sizeof(struct smc_clc_msg_trail))
759 /* CLC handshake during connect */
760 static int smc_connect_clc(struct smc_sock *smc,
761 struct smc_clc_msg_accept_confirm_v2 *aclc2,
762 struct smc_init_info *ini)
766 /* do inband token exchange */
767 rc = smc_clc_send_proposal(smc, ini);
770 /* receive SMC Accept CLC message */
771 return smc_clc_wait_msg(smc, aclc2, SMC_CLC_MAX_ACCEPT_LEN,
772 SMC_CLC_ACCEPT, CLC_WAIT_TIME);
775 /* setup for RDMA connection of client */
776 static int smc_connect_rdma(struct smc_sock *smc,
777 struct smc_clc_msg_accept_confirm *aclc,
778 struct smc_init_info *ini)
780 int i, reason_code = 0;
781 struct smc_link *link;
783 ini->is_smcd = false;
784 ini->ib_lcl = &aclc->r0.lcl;
785 ini->ib_clcqpn = ntoh24(aclc->r0.qpn);
786 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
788 mutex_lock(&smc_client_lgr_pending);
789 reason_code = smc_conn_create(smc, ini);
791 mutex_unlock(&smc_client_lgr_pending);
795 smc_conn_save_peer_info(smc, aclc);
797 if (ini->first_contact_local) {
798 link = smc->conn.lnk;
800 /* set link that was assigned by server */
802 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
803 struct smc_link *l = &smc->conn.lgr->lnk[i];
805 if (l->peer_qpn == ntoh24(aclc->r0.qpn) &&
806 !memcmp(l->peer_gid, &aclc->r0.lcl.gid,
808 !memcmp(l->peer_mac, &aclc->r0.lcl.mac,
809 sizeof(l->peer_mac))) {
815 reason_code = SMC_CLC_DECL_NOSRVLINK;
818 smc_switch_link_and_count(&smc->conn, link);
821 /* create send buffer and rmb */
822 if (smc_buf_create(smc, false)) {
823 reason_code = SMC_CLC_DECL_MEM;
827 if (ini->first_contact_local)
828 smc_link_save_peer_info(link, aclc);
830 if (smc_rmb_rtoken_handling(&smc->conn, link, aclc)) {
831 reason_code = SMC_CLC_DECL_ERR_RTOK;
838 if (ini->first_contact_local) {
839 if (smc_ib_ready_link(link)) {
840 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
844 if (smcr_lgr_reg_rmbs(link, smc->conn.rmb_desc)) {
845 reason_code = SMC_CLC_DECL_ERR_REGRMB;
849 smc_rmb_sync_sg_for_device(&smc->conn);
851 reason_code = smc_clc_send_confirm(smc, ini->first_contact_local,
858 if (ini->first_contact_local) {
859 /* QP confirmation over RoCE fabric */
860 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
861 reason_code = smcr_clnt_conf_first_link(smc);
862 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
866 mutex_unlock(&smc_client_lgr_pending);
868 smc_copy_sock_settings_to_clc(smc);
869 smc->connect_nonblock = 0;
870 if (smc->sk.sk_state == SMC_INIT)
871 smc->sk.sk_state = SMC_ACTIVE;
875 smc_conn_abort(smc, ini->first_contact_local);
876 mutex_unlock(&smc_client_lgr_pending);
877 smc->connect_nonblock = 0;
882 /* The server has chosen one of the proposed ISM devices for the communication.
883 * Determine from the CHID of the received CLC ACCEPT the ISM device chosen.
886 smc_v2_determine_accepted_chid(struct smc_clc_msg_accept_confirm_v2 *aclc,
887 struct smc_init_info *ini)
891 for (i = 0; i < ini->ism_offered_cnt + 1; i++) {
892 if (ini->ism_chid[i] == ntohs(aclc->chid)) {
893 ini->ism_selected = i;
901 /* setup for ISM connection of client */
902 static int smc_connect_ism(struct smc_sock *smc,
903 struct smc_clc_msg_accept_confirm *aclc,
904 struct smc_init_info *ini)
909 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
911 if (aclc->hdr.version == SMC_V2) {
912 struct smc_clc_msg_accept_confirm_v2 *aclc_v2 =
913 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
915 rc = smc_v2_determine_accepted_chid(aclc_v2, ini);
919 ini->ism_peer_gid[ini->ism_selected] = aclc->d0.gid;
921 /* there is only one lgr role for SMC-D; use server lock */
922 mutex_lock(&smc_server_lgr_pending);
923 rc = smc_conn_create(smc, ini);
925 mutex_unlock(&smc_server_lgr_pending);
929 /* Create send and receive buffers */
930 rc = smc_buf_create(smc, true);
932 rc = (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB : SMC_CLC_DECL_MEM;
936 smc_conn_save_peer_info(smc, aclc);
941 rc = smc_clc_send_confirm(smc, ini->first_contact_local,
945 mutex_unlock(&smc_server_lgr_pending);
947 smc_copy_sock_settings_to_clc(smc);
948 smc->connect_nonblock = 0;
949 if (smc->sk.sk_state == SMC_INIT)
950 smc->sk.sk_state = SMC_ACTIVE;
954 smc_conn_abort(smc, ini->first_contact_local);
955 mutex_unlock(&smc_server_lgr_pending);
956 smc->connect_nonblock = 0;
961 /* check if received accept type and version matches a proposed one */
962 static int smc_connect_check_aclc(struct smc_init_info *ini,
963 struct smc_clc_msg_accept_confirm *aclc)
965 if ((aclc->hdr.typev1 == SMC_TYPE_R &&
966 !smcr_indicated(ini->smc_type_v1)) ||
967 (aclc->hdr.typev1 == SMC_TYPE_D &&
968 ((!smcd_indicated(ini->smc_type_v1) &&
969 !smcd_indicated(ini->smc_type_v2)) ||
970 (aclc->hdr.version == SMC_V1 &&
971 !smcd_indicated(ini->smc_type_v1)) ||
972 (aclc->hdr.version == SMC_V2 &&
973 !smcd_indicated(ini->smc_type_v2)))))
974 return SMC_CLC_DECL_MODEUNSUPP;
979 /* perform steps before actually connecting */
980 static int __smc_connect(struct smc_sock *smc)
982 u8 version = smc_ism_is_v2_capable() ? SMC_V2 : SMC_V1;
983 struct smc_clc_msg_accept_confirm_v2 *aclc2;
984 struct smc_clc_msg_accept_confirm *aclc;
985 struct smc_init_info *ini = NULL;
989 if (smc->use_fallback)
990 return smc_connect_fallback(smc, smc->fallback_rsn);
992 /* if peer has not signalled SMC-capability, fall back */
993 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
994 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
996 /* IPSec connections opt out of SMC optimizations */
997 if (using_ipsec(smc))
998 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC,
1001 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
1003 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_MEM,
1006 ini->smcd_version = SMC_V1;
1007 ini->smcd_version |= smc_ism_is_v2_capable() ? SMC_V2 : 0;
1008 ini->smc_type_v1 = SMC_TYPE_B;
1009 ini->smc_type_v2 = smc_ism_is_v2_capable() ? SMC_TYPE_D : SMC_TYPE_N;
1011 /* get vlan id from IP device */
1012 if (smc_vlan_by_tcpsk(smc->clcsock, ini)) {
1013 ini->smcd_version &= ~SMC_V1;
1014 ini->smc_type_v1 = SMC_TYPE_N;
1015 if (!ini->smcd_version) {
1016 rc = SMC_CLC_DECL_GETVLANERR;
1021 rc = smc_find_proposal_devices(smc, ini);
1025 buf = kzalloc(SMC_CLC_MAX_ACCEPT_LEN, GFP_KERNEL);
1027 rc = SMC_CLC_DECL_MEM;
1030 aclc2 = (struct smc_clc_msg_accept_confirm_v2 *)buf;
1031 aclc = (struct smc_clc_msg_accept_confirm *)aclc2;
1033 /* perform CLC handshake */
1034 rc = smc_connect_clc(smc, aclc2, ini);
1038 /* check if smc modes and versions of CLC proposal and accept match */
1039 rc = smc_connect_check_aclc(ini, aclc);
1040 version = aclc->hdr.version == SMC_V1 ? SMC_V1 : SMC_V2;
1041 ini->smcd_version = version;
1045 /* depending on previous steps, connect using rdma or ism */
1046 if (aclc->hdr.typev1 == SMC_TYPE_R)
1047 rc = smc_connect_rdma(smc, aclc, ini);
1048 else if (aclc->hdr.typev1 == SMC_TYPE_D)
1049 rc = smc_connect_ism(smc, aclc, ini);
1053 SMC_STAT_CLNT_SUCC_INC(sock_net(smc->clcsock->sk), aclc);
1054 smc_connect_ism_vlan_cleanup(smc, ini);
1060 smc_connect_ism_vlan_cleanup(smc, ini);
1064 return smc_connect_decline_fallback(smc, rc, version);
1067 static void smc_connect_work(struct work_struct *work)
1069 struct smc_sock *smc = container_of(work, struct smc_sock,
1071 long timeo = smc->sk.sk_sndtimeo;
1075 timeo = MAX_SCHEDULE_TIMEOUT;
1076 lock_sock(smc->clcsock->sk);
1077 if (smc->clcsock->sk->sk_err) {
1078 smc->sk.sk_err = smc->clcsock->sk->sk_err;
1079 } else if ((1 << smc->clcsock->sk->sk_state) &
1080 (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
1081 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
1082 if ((rc == -EPIPE) &&
1083 ((1 << smc->clcsock->sk->sk_state) &
1084 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
1087 release_sock(smc->clcsock->sk);
1088 lock_sock(&smc->sk);
1089 if (rc != 0 || smc->sk.sk_err) {
1090 smc->sk.sk_state = SMC_CLOSED;
1091 if (rc == -EPIPE || rc == -EAGAIN)
1092 smc->sk.sk_err = EPIPE;
1093 else if (signal_pending(current))
1094 smc->sk.sk_err = -sock_intr_errno(timeo);
1095 sock_put(&smc->sk); /* passive closing */
1099 rc = __smc_connect(smc);
1101 smc->sk.sk_err = -rc;
1104 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
1105 if (smc->sk.sk_err) {
1106 smc->sk.sk_state_change(&smc->sk);
1107 } else { /* allow polling before and after fallback decision */
1108 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
1109 smc->sk.sk_write_space(&smc->sk);
1112 release_sock(&smc->sk);
1115 static int smc_connect(struct socket *sock, struct sockaddr *addr,
1116 int alen, int flags)
1118 struct sock *sk = sock->sk;
1119 struct smc_sock *smc;
1124 /* separate smc parameter checking to be safe */
1125 if (alen < sizeof(addr->sa_family))
1127 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
1131 switch (sk->sk_state) {
1141 smc_copy_sock_settings_to_clc(smc);
1142 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
1143 if (smc->connect_nonblock) {
1147 rc = kernel_connect(smc->clcsock, addr, alen, flags);
1148 if (rc && rc != -EINPROGRESS)
1151 sock_hold(&smc->sk); /* sock put in passive closing */
1152 if (smc->use_fallback)
1154 if (flags & O_NONBLOCK) {
1155 if (queue_work(smc_hs_wq, &smc->connect_work))
1156 smc->connect_nonblock = 1;
1159 rc = __smc_connect(smc);
1163 rc = 0; /* success cases including fallback */
1172 static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
1174 struct socket *new_clcsock = NULL;
1175 struct sock *lsk = &lsmc->sk;
1176 struct sock *new_sk;
1180 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
1183 lsk->sk_err = ENOMEM;
1188 *new_smc = smc_sk(new_sk);
1190 mutex_lock(&lsmc->clcsock_release_lock);
1192 rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK);
1193 mutex_unlock(&lsmc->clcsock_release_lock);
1195 if (rc < 0 && rc != -EAGAIN)
1197 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
1198 new_sk->sk_prot->unhash(new_sk);
1200 sock_release(new_clcsock);
1201 new_sk->sk_state = SMC_CLOSED;
1202 sock_set_flag(new_sk, SOCK_DEAD);
1203 sock_put(new_sk); /* final */
1208 /* new clcsock has inherited the smc listen-specific sk_data_ready
1209 * function; switch it back to the original sk_data_ready function
1211 new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready;
1212 (*new_smc)->clcsock = new_clcsock;
1217 /* add a just created sock to the accept queue of the listen sock as
1218 * candidate for a following socket accept call from user space
1220 static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
1222 struct smc_sock *par = smc_sk(parent);
1224 sock_hold(sk); /* sock_put in smc_accept_unlink () */
1225 spin_lock(&par->accept_q_lock);
1226 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
1227 spin_unlock(&par->accept_q_lock);
1228 sk_acceptq_added(parent);
1231 /* remove a socket from the accept queue of its parental listening socket */
1232 static void smc_accept_unlink(struct sock *sk)
1234 struct smc_sock *par = smc_sk(sk)->listen_smc;
1236 spin_lock(&par->accept_q_lock);
1237 list_del_init(&smc_sk(sk)->accept_q);
1238 spin_unlock(&par->accept_q_lock);
1239 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
1240 sock_put(sk); /* sock_hold in smc_accept_enqueue */
1243 /* remove a sock from the accept queue to bind it to a new socket created
1244 * for a socket accept call from user space
1246 struct sock *smc_accept_dequeue(struct sock *parent,
1247 struct socket *new_sock)
1249 struct smc_sock *isk, *n;
1250 struct sock *new_sk;
1252 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
1253 new_sk = (struct sock *)isk;
1255 smc_accept_unlink(new_sk);
1256 if (new_sk->sk_state == SMC_CLOSED) {
1257 new_sk->sk_prot->unhash(new_sk);
1259 sock_release(isk->clcsock);
1260 isk->clcsock = NULL;
1262 sock_put(new_sk); /* final */
1266 sock_graft(new_sk, new_sock);
1267 if (isk->use_fallback) {
1268 smc_sk(new_sk)->clcsock->file = new_sock->file;
1269 isk->clcsock->file->private_data = isk->clcsock;
1277 /* clean up for a created but never accepted sock */
1278 void smc_close_non_accepted(struct sock *sk)
1280 struct smc_sock *smc = smc_sk(sk);
1282 sock_hold(sk); /* sock_put below */
1284 if (!sk->sk_lingertime)
1285 /* wait for peer closing */
1286 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
1289 sock_put(sk); /* sock_hold above */
1290 sock_put(sk); /* final sock_put */
1293 static int smcr_serv_conf_first_link(struct smc_sock *smc)
1295 struct smc_link *link = smc->conn.lnk;
1296 struct smc_llc_qentry *qentry;
1299 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
1300 return SMC_CLC_DECL_ERR_REGRMB;
1302 /* send CONFIRM LINK request to client over the RoCE fabric */
1303 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
1305 return SMC_CLC_DECL_TIMEOUT_CL;
1307 /* receive CONFIRM LINK response from client over the RoCE fabric */
1308 qentry = smc_llc_wait(link->lgr, link, SMC_LLC_WAIT_TIME,
1309 SMC_LLC_CONFIRM_LINK);
1311 struct smc_clc_msg_decline dclc;
1313 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
1314 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
1315 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
1317 smc_llc_save_peer_uid(qentry);
1318 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_RESP);
1319 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
1321 return SMC_CLC_DECL_RMBE_EC;
1323 /* confirm_rkey is implicit on 1st contact */
1324 smc->conn.rmb_desc->is_conf_rkey = true;
1326 smc_llc_link_active(link);
1327 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
1329 /* initial contact - try to establish second link */
1330 smc_llc_srv_add_link(link);
1334 /* listen worker: finish */
1335 static void smc_listen_out(struct smc_sock *new_smc)
1337 struct smc_sock *lsmc = new_smc->listen_smc;
1338 struct sock *newsmcsk = &new_smc->sk;
1340 if (lsmc->sk.sk_state == SMC_LISTEN) {
1341 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
1342 smc_accept_enqueue(&lsmc->sk, newsmcsk);
1343 release_sock(&lsmc->sk);
1344 } else { /* no longer listening */
1345 smc_close_non_accepted(newsmcsk);
1348 /* Wake up accept */
1349 lsmc->sk.sk_data_ready(&lsmc->sk);
1350 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
1353 /* listen worker: finish in state connected */
1354 static void smc_listen_out_connected(struct smc_sock *new_smc)
1356 struct sock *newsmcsk = &new_smc->sk;
1358 sk_refcnt_debug_inc(newsmcsk);
1359 if (newsmcsk->sk_state == SMC_INIT)
1360 newsmcsk->sk_state = SMC_ACTIVE;
1362 smc_listen_out(new_smc);
1365 /* listen worker: finish in error state */
1366 static void smc_listen_out_err(struct smc_sock *new_smc)
1368 struct sock *newsmcsk = &new_smc->sk;
1369 struct net *net = sock_net(newsmcsk);
1371 this_cpu_inc(net->smc.smc_stats->srv_hshake_err_cnt);
1372 if (newsmcsk->sk_state == SMC_INIT)
1373 sock_put(&new_smc->sk); /* passive closing */
1374 newsmcsk->sk_state = SMC_CLOSED;
1376 smc_listen_out(new_smc);
1379 /* listen worker: decline and fall back if possible */
1380 static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1381 int local_first, u8 version)
1383 /* RDMA setup failed, switch back to TCP */
1384 smc_conn_abort(new_smc, local_first);
1385 if (reason_code < 0) { /* error, no fallback possible */
1386 smc_listen_out_err(new_smc);
1389 smc_switch_to_fallback(new_smc, reason_code);
1390 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
1391 if (smc_clc_send_decline(new_smc, reason_code, version) < 0) {
1392 smc_listen_out_err(new_smc);
1396 smc_listen_out_connected(new_smc);
1399 /* listen worker: version checking */
1400 static int smc_listen_v2_check(struct smc_sock *new_smc,
1401 struct smc_clc_msg_proposal *pclc,
1402 struct smc_init_info *ini)
1404 struct smc_clc_smcd_v2_extension *pclc_smcd_v2_ext;
1405 struct smc_clc_v2_extension *pclc_v2_ext;
1406 int rc = SMC_CLC_DECL_PEERNOSMC;
1408 ini->smc_type_v1 = pclc->hdr.typev1;
1409 ini->smc_type_v2 = pclc->hdr.typev2;
1410 ini->smcd_version = ini->smc_type_v1 != SMC_TYPE_N ? SMC_V1 : 0;
1411 if (pclc->hdr.version > SMC_V1)
1412 ini->smcd_version |=
1413 ini->smc_type_v2 != SMC_TYPE_N ? SMC_V2 : 0;
1414 if (!(ini->smcd_version & SMC_V2)) {
1415 rc = SMC_CLC_DECL_PEERNOSMC;
1418 if (!smc_ism_is_v2_capable()) {
1419 ini->smcd_version &= ~SMC_V2;
1420 rc = SMC_CLC_DECL_NOISM2SUPP;
1423 pclc_v2_ext = smc_get_clc_v2_ext(pclc);
1425 ini->smcd_version &= ~SMC_V2;
1426 rc = SMC_CLC_DECL_NOV2EXT;
1429 pclc_smcd_v2_ext = smc_get_clc_smcd_v2_ext(pclc_v2_ext);
1430 if (!pclc_smcd_v2_ext) {
1431 ini->smcd_version &= ~SMC_V2;
1432 rc = SMC_CLC_DECL_NOV2DEXT;
1436 if (!ini->smcd_version)
1442 /* listen worker: check prefixes */
1443 static int smc_listen_prfx_check(struct smc_sock *new_smc,
1444 struct smc_clc_msg_proposal *pclc)
1446 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1447 struct socket *newclcsock = new_smc->clcsock;
1449 if (pclc->hdr.typev1 == SMC_TYPE_N)
1451 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1452 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
1453 return SMC_CLC_DECL_DIFFPREFIX;
1458 /* listen worker: initialize connection and buffers */
1459 static int smc_listen_rdma_init(struct smc_sock *new_smc,
1460 struct smc_init_info *ini)
1464 /* allocate connection / link group */
1465 rc = smc_conn_create(new_smc, ini);
1469 /* create send buffer and rmb */
1470 if (smc_buf_create(new_smc, false))
1471 return SMC_CLC_DECL_MEM;
1476 /* listen worker: initialize connection and buffers for SMC-D */
1477 static int smc_listen_ism_init(struct smc_sock *new_smc,
1478 struct smc_init_info *ini)
1482 rc = smc_conn_create(new_smc, ini);
1486 /* Create send and receive buffers */
1487 rc = smc_buf_create(new_smc, true);
1489 smc_conn_abort(new_smc, ini->first_contact_local);
1490 return (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB :
1497 static bool smc_is_already_selected(struct smcd_dev *smcd,
1498 struct smc_init_info *ini,
1503 for (i = 0; i < matches; i++)
1504 if (smcd == ini->ism_dev[i])
1510 /* check for ISM devices matching proposed ISM devices */
1511 static void smc_check_ism_v2_match(struct smc_init_info *ini,
1512 u16 proposed_chid, u64 proposed_gid,
1513 unsigned int *matches)
1515 struct smcd_dev *smcd;
1517 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
1518 if (smcd->going_away)
1520 if (smc_is_already_selected(smcd, ini, *matches))
1522 if (smc_ism_get_chid(smcd) == proposed_chid &&
1523 !smc_ism_cantalk(proposed_gid, ISM_RESERVED_VLANID, smcd)) {
1524 ini->ism_peer_gid[*matches] = proposed_gid;
1525 ini->ism_dev[*matches] = smcd;
1532 static void smc_find_ism_store_rc(u32 rc, struct smc_init_info *ini)
1538 static void smc_find_ism_v2_device_serv(struct smc_sock *new_smc,
1539 struct smc_clc_msg_proposal *pclc,
1540 struct smc_init_info *ini)
1542 struct smc_clc_smcd_v2_extension *smcd_v2_ext;
1543 struct smc_clc_v2_extension *smc_v2_ext;
1544 struct smc_clc_msg_smcd *pclc_smcd;
1545 unsigned int matches = 0;
1550 if (!(ini->smcd_version & SMC_V2) || !smcd_indicated(ini->smc_type_v2))
1553 pclc_smcd = smc_get_clc_msg_smcd(pclc);
1554 smc_v2_ext = smc_get_clc_v2_ext(pclc);
1555 smcd_v2_ext = smc_get_clc_smcd_v2_ext(smc_v2_ext);
1557 !smc_v2_ext->hdr.flag.seid) { /* no system EID support for SMCD */
1558 smc_find_ism_store_rc(SMC_CLC_DECL_NOSEID, ini);
1562 mutex_lock(&smcd_dev_list.mutex);
1563 if (pclc_smcd->ism.chid)
1564 /* check for ISM device matching proposed native ISM device */
1565 smc_check_ism_v2_match(ini, ntohs(pclc_smcd->ism.chid),
1566 ntohll(pclc_smcd->ism.gid), &matches);
1567 for (i = 1; i <= smc_v2_ext->hdr.ism_gid_cnt; i++) {
1568 /* check for ISM devices matching proposed non-native ISM
1571 smc_check_ism_v2_match(ini,
1572 ntohs(smcd_v2_ext->gidchid[i - 1].chid),
1573 ntohll(smcd_v2_ext->gidchid[i - 1].gid),
1576 mutex_unlock(&smcd_dev_list.mutex);
1578 if (ini->ism_dev[0]) {
1579 smc_ism_get_system_eid(ini->ism_dev[0], &eid);
1580 if (memcmp(eid, smcd_v2_ext->system_eid, SMC_MAX_EID_LEN))
1586 /* separate - outside the smcd_dev_list.lock */
1587 smcd_version = ini->smcd_version;
1588 for (i = 0; i < matches; i++) {
1589 ini->smcd_version = SMC_V2;
1590 ini->is_smcd = true;
1591 ini->ism_selected = i;
1592 rc = smc_listen_ism_init(new_smc, ini);
1594 smc_find_ism_store_rc(rc, ini);
1595 /* try next active ISM device */
1598 return; /* matching and usable V2 ISM device found */
1600 /* no V2 ISM device could be initialized */
1601 ini->smcd_version = smcd_version; /* restore original value */
1604 ini->smcd_version &= ~SMC_V2;
1605 ini->ism_dev[0] = NULL;
1606 ini->is_smcd = false;
1609 static void smc_find_ism_v1_device_serv(struct smc_sock *new_smc,
1610 struct smc_clc_msg_proposal *pclc,
1611 struct smc_init_info *ini)
1613 struct smc_clc_msg_smcd *pclc_smcd = smc_get_clc_msg_smcd(pclc);
1616 /* check if ISM V1 is available */
1617 if (!(ini->smcd_version & SMC_V1) || !smcd_indicated(ini->smc_type_v1))
1619 ini->is_smcd = true; /* prepare ISM check */
1620 ini->ism_peer_gid[0] = ntohll(pclc_smcd->ism.gid);
1621 rc = smc_find_ism_device(new_smc, ini);
1624 ini->ism_selected = 0;
1625 rc = smc_listen_ism_init(new_smc, ini);
1627 return; /* V1 ISM device found */
1630 smc_find_ism_store_rc(rc, ini);
1631 ini->ism_dev[0] = NULL;
1632 ini->is_smcd = false;
1635 /* listen worker: register buffers */
1636 static int smc_listen_rdma_reg(struct smc_sock *new_smc, bool local_first)
1638 struct smc_connection *conn = &new_smc->conn;
1641 if (smcr_lgr_reg_rmbs(conn->lnk, conn->rmb_desc))
1642 return SMC_CLC_DECL_ERR_REGRMB;
1644 smc_rmb_sync_sg_for_device(&new_smc->conn);
1649 static int smc_find_rdma_v1_device_serv(struct smc_sock *new_smc,
1650 struct smc_clc_msg_proposal *pclc,
1651 struct smc_init_info *ini)
1655 if (!smcr_indicated(ini->smc_type_v1))
1656 return SMC_CLC_DECL_NOSMCDEV;
1658 /* prepare RDMA check */
1659 ini->ib_lcl = &pclc->lcl;
1660 rc = smc_find_rdma_device(new_smc, ini);
1662 /* no RDMA device found */
1663 if (ini->smc_type_v1 == SMC_TYPE_B)
1664 /* neither ISM nor RDMA device found */
1665 rc = SMC_CLC_DECL_NOSMCDEV;
1668 rc = smc_listen_rdma_init(new_smc, ini);
1671 return smc_listen_rdma_reg(new_smc, ini->first_contact_local);
1674 /* determine the local device matching to proposal */
1675 static int smc_listen_find_device(struct smc_sock *new_smc,
1676 struct smc_clc_msg_proposal *pclc,
1677 struct smc_init_info *ini)
1681 /* check for ISM device matching V2 proposed device */
1682 smc_find_ism_v2_device_serv(new_smc, pclc, ini);
1683 if (ini->ism_dev[0])
1686 if (!(ini->smcd_version & SMC_V1))
1687 return ini->rc ?: SMC_CLC_DECL_NOSMCD2DEV;
1689 /* check for matching IP prefix and subnet length */
1690 rc = smc_listen_prfx_check(new_smc, pclc);
1692 return ini->rc ?: rc;
1694 /* get vlan id from IP device */
1695 if (smc_vlan_by_tcpsk(new_smc->clcsock, ini))
1696 return ini->rc ?: SMC_CLC_DECL_GETVLANERR;
1698 /* check for ISM device matching V1 proposed device */
1699 smc_find_ism_v1_device_serv(new_smc, pclc, ini);
1700 if (ini->ism_dev[0])
1703 if (pclc->hdr.typev1 == SMC_TYPE_D)
1704 /* skip RDMA and decline */
1705 return ini->rc ?: SMC_CLC_DECL_NOSMCDDEV;
1707 /* check if RDMA is available */
1708 rc = smc_find_rdma_v1_device_serv(new_smc, pclc, ini);
1709 smc_find_ism_store_rc(rc, ini);
1711 return (!rc) ? 0 : ini->rc;
1714 /* listen worker: finish RDMA setup */
1715 static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1716 struct smc_clc_msg_accept_confirm *cclc,
1719 struct smc_link *link = new_smc->conn.lnk;
1720 int reason_code = 0;
1723 smc_link_save_peer_info(link, cclc);
1725 if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc))
1726 return SMC_CLC_DECL_ERR_RTOK;
1729 if (smc_ib_ready_link(link))
1730 return SMC_CLC_DECL_ERR_RDYLNK;
1731 /* QP confirmation over RoCE fabric */
1732 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
1733 reason_code = smcr_serv_conf_first_link(new_smc);
1734 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
1739 /* setup for connection of server */
1740 static void smc_listen_work(struct work_struct *work)
1742 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1744 u8 version = smc_ism_is_v2_capable() ? SMC_V2 : SMC_V1;
1745 struct socket *newclcsock = new_smc->clcsock;
1746 struct smc_clc_msg_accept_confirm *cclc;
1747 struct smc_clc_msg_proposal_area *buf;
1748 struct smc_clc_msg_proposal *pclc;
1749 struct smc_init_info *ini = NULL;
1752 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1753 return smc_listen_out_err(new_smc);
1755 if (new_smc->use_fallback) {
1756 smc_listen_out_connected(new_smc);
1760 /* check if peer is smc capable */
1761 if (!tcp_sk(newclcsock->sk)->syn_smc) {
1762 smc_switch_to_fallback(new_smc, SMC_CLC_DECL_PEERNOSMC);
1763 smc_listen_out_connected(new_smc);
1767 /* do inband token exchange -
1768 * wait for and receive SMC Proposal CLC message
1770 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
1772 rc = SMC_CLC_DECL_MEM;
1775 pclc = (struct smc_clc_msg_proposal *)buf;
1776 rc = smc_clc_wait_msg(new_smc, pclc, sizeof(*buf),
1777 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
1780 version = pclc->hdr.version == SMC_V1 ? SMC_V1 : version;
1782 /* IPSec connections opt out of SMC optimizations */
1783 if (using_ipsec(new_smc)) {
1784 rc = SMC_CLC_DECL_IPSEC;
1788 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
1790 rc = SMC_CLC_DECL_MEM;
1794 /* initial version checking */
1795 rc = smc_listen_v2_check(new_smc, pclc, ini);
1799 mutex_lock(&smc_server_lgr_pending);
1800 smc_close_init(new_smc);
1801 smc_rx_init(new_smc);
1802 smc_tx_init(new_smc);
1804 /* determine ISM or RoCE device used for connection */
1805 rc = smc_listen_find_device(new_smc, pclc, ini);
1809 /* send SMC Accept CLC message */
1810 rc = smc_clc_send_accept(new_smc, ini->first_contact_local,
1811 ini->smcd_version == SMC_V2 ? SMC_V2 : SMC_V1);
1815 /* SMC-D does not need this lock any more */
1817 mutex_unlock(&smc_server_lgr_pending);
1819 /* receive SMC Confirm CLC message */
1820 memset(buf, 0, sizeof(*buf));
1821 cclc = (struct smc_clc_msg_accept_confirm *)buf;
1822 rc = smc_clc_wait_msg(new_smc, cclc, sizeof(*buf),
1823 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
1831 if (!ini->is_smcd) {
1832 rc = smc_listen_rdma_finish(new_smc, cclc,
1833 ini->first_contact_local);
1836 mutex_unlock(&smc_server_lgr_pending);
1838 smc_conn_save_peer_info(new_smc, cclc);
1839 smc_listen_out_connected(new_smc);
1840 SMC_STAT_SERV_SUCC_INC(sock_net(newclcsock->sk), ini);
1844 mutex_unlock(&smc_server_lgr_pending);
1846 smc_listen_decline(new_smc, rc, ini ? ini->first_contact_local : 0,
1853 static void smc_tcp_listen_work(struct work_struct *work)
1855 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1857 struct sock *lsk = &lsmc->sk;
1858 struct smc_sock *new_smc;
1862 while (lsk->sk_state == SMC_LISTEN) {
1863 rc = smc_clcsock_accept(lsmc, &new_smc);
1864 if (rc) /* clcsock accept queue empty or error */
1869 new_smc->listen_smc = lsmc;
1870 new_smc->use_fallback = lsmc->use_fallback;
1871 new_smc->fallback_rsn = lsmc->fallback_rsn;
1872 sock_hold(lsk); /* sock_put in smc_listen_work */
1873 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1874 smc_copy_sock_settings_to_smc(new_smc);
1875 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1876 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
1877 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1878 if (!queue_work(smc_hs_wq, &new_smc->smc_listen_work))
1879 sock_put(&new_smc->sk);
1884 sock_put(&lsmc->sk); /* sock_hold in smc_clcsock_data_ready() */
1887 static void smc_clcsock_data_ready(struct sock *listen_clcsock)
1889 struct smc_sock *lsmc;
1891 lsmc = (struct smc_sock *)
1892 ((uintptr_t)listen_clcsock->sk_user_data & ~SK_USER_DATA_NOCOPY);
1895 lsmc->clcsk_data_ready(listen_clcsock);
1896 if (lsmc->sk.sk_state == SMC_LISTEN) {
1897 sock_hold(&lsmc->sk); /* sock_put in smc_tcp_listen_work() */
1898 if (!queue_work(smc_hs_wq, &lsmc->tcp_listen_work))
1899 sock_put(&lsmc->sk);
1903 static int smc_listen(struct socket *sock, int backlog)
1905 struct sock *sk = sock->sk;
1906 struct smc_sock *smc;
1913 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
1914 smc->connect_nonblock)
1918 if (sk->sk_state == SMC_LISTEN) {
1919 sk->sk_max_ack_backlog = backlog;
1922 /* some socket options are handled in core, so we could not apply
1923 * them to the clc socket -- copy smc socket options to clc socket
1925 smc_copy_sock_settings_to_clc(smc);
1926 if (!smc->use_fallback)
1927 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
1929 /* save original sk_data_ready function and establish
1930 * smc-specific sk_data_ready function
1932 smc->clcsk_data_ready = smc->clcsock->sk->sk_data_ready;
1933 smc->clcsock->sk->sk_data_ready = smc_clcsock_data_ready;
1934 smc->clcsock->sk->sk_user_data =
1935 (void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY);
1936 rc = kernel_listen(smc->clcsock, backlog);
1938 smc->clcsock->sk->sk_data_ready = smc->clcsk_data_ready;
1941 sk->sk_max_ack_backlog = backlog;
1942 sk->sk_ack_backlog = 0;
1943 sk->sk_state = SMC_LISTEN;
1950 static int smc_accept(struct socket *sock, struct socket *new_sock,
1951 int flags, bool kern)
1953 struct sock *sk = sock->sk, *nsk;
1954 DECLARE_WAITQUEUE(wait, current);
1955 struct smc_sock *lsmc;
1960 sock_hold(sk); /* sock_put below */
1963 if (lsmc->sk.sk_state != SMC_LISTEN) {
1969 /* Wait for an incoming connection */
1970 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1971 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1972 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1973 set_current_state(TASK_INTERRUPTIBLE);
1979 timeo = schedule_timeout(timeo);
1980 /* wakeup by sk_data_ready in smc_listen_work() */
1981 sched_annotate_sleep();
1983 if (signal_pending(current)) {
1984 rc = sock_intr_errno(timeo);
1988 set_current_state(TASK_RUNNING);
1989 remove_wait_queue(sk_sleep(sk), &wait);
1992 rc = sock_error(nsk);
1997 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1998 /* wait till data arrives on the socket */
1999 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
2001 if (smc_sk(nsk)->use_fallback) {
2002 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
2005 if (skb_queue_empty(&clcsk->sk_receive_queue))
2006 sk_wait_data(clcsk, &timeo, NULL);
2007 release_sock(clcsk);
2008 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
2010 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
2016 sock_put(sk); /* sock_hold above */
2020 static int smc_getname(struct socket *sock, struct sockaddr *addr,
2023 struct smc_sock *smc;
2025 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
2026 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
2029 smc = smc_sk(sock->sk);
2031 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
2034 static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
2036 struct sock *sk = sock->sk;
2037 struct smc_sock *smc;
2042 if ((sk->sk_state != SMC_ACTIVE) &&
2043 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2044 (sk->sk_state != SMC_INIT))
2047 if (msg->msg_flags & MSG_FASTOPEN) {
2048 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
2049 smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP);
2056 if (smc->use_fallback) {
2057 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
2059 rc = smc_tx_sendmsg(smc, msg, len);
2060 SMC_STAT_TX_PAYLOAD(smc, len, rc);
2067 static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
2070 struct sock *sk = sock->sk;
2071 struct smc_sock *smc;
2076 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2077 /* socket was connected before, no more data to read */
2081 if ((sk->sk_state == SMC_INIT) ||
2082 (sk->sk_state == SMC_LISTEN) ||
2083 (sk->sk_state == SMC_CLOSED))
2086 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2091 if (smc->use_fallback) {
2092 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
2094 msg->msg_namelen = 0;
2095 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
2096 SMC_STAT_RX_PAYLOAD(smc, rc, rc);
2104 static __poll_t smc_accept_poll(struct sock *parent)
2106 struct smc_sock *isk = smc_sk(parent);
2109 spin_lock(&isk->accept_q_lock);
2110 if (!list_empty(&isk->accept_q))
2111 mask = EPOLLIN | EPOLLRDNORM;
2112 spin_unlock(&isk->accept_q_lock);
2117 static __poll_t smc_poll(struct file *file, struct socket *sock,
2120 struct sock *sk = sock->sk;
2121 struct smc_sock *smc;
2127 smc = smc_sk(sock->sk);
2128 if (smc->use_fallback) {
2129 /* delegate to CLC child sock */
2130 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
2131 sk->sk_err = smc->clcsock->sk->sk_err;
2133 if (sk->sk_state != SMC_CLOSED)
2134 sock_poll_wait(file, sock, wait);
2137 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
2138 (sk->sk_state == SMC_CLOSED))
2140 if (sk->sk_state == SMC_LISTEN) {
2141 /* woken up by sk_data_ready in smc_listen_work() */
2142 mask |= smc_accept_poll(sk);
2143 } else if (smc->use_fallback) { /* as result of connect_work()*/
2144 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
2146 sk->sk_err = smc->clcsock->sk->sk_err;
2148 if ((sk->sk_state != SMC_INIT &&
2149 atomic_read(&smc->conn.sndbuf_space)) ||
2150 sk->sk_shutdown & SEND_SHUTDOWN) {
2151 mask |= EPOLLOUT | EPOLLWRNORM;
2153 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
2154 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
2156 if (atomic_read(&smc->conn.bytes_to_rcv))
2157 mask |= EPOLLIN | EPOLLRDNORM;
2158 if (sk->sk_shutdown & RCV_SHUTDOWN)
2159 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
2160 if (sk->sk_state == SMC_APPCLOSEWAIT1)
2162 if (smc->conn.urg_state == SMC_URG_VALID)
2170 static int smc_shutdown(struct socket *sock, int how)
2172 struct sock *sk = sock->sk;
2173 bool do_shutdown = true;
2174 struct smc_sock *smc;
2181 if ((how < SHUT_RD) || (how > SHUT_RDWR))
2187 if ((sk->sk_state != SMC_ACTIVE) &&
2188 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
2189 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
2190 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2191 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
2192 (sk->sk_state != SMC_APPFINCLOSEWAIT))
2194 if (smc->use_fallback) {
2195 rc = kernel_sock_shutdown(smc->clcsock, how);
2196 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
2197 if (sk->sk_shutdown == SHUTDOWN_MASK)
2198 sk->sk_state = SMC_CLOSED;
2202 case SHUT_RDWR: /* shutdown in both directions */
2203 old_state = sk->sk_state;
2204 rc = smc_close_active(smc);
2205 if (old_state == SMC_ACTIVE &&
2206 sk->sk_state == SMC_PEERCLOSEWAIT1)
2207 do_shutdown = false;
2210 rc = smc_close_shutdown_write(smc);
2214 /* nothing more to do because peer is not involved */
2217 if (do_shutdown && smc->clcsock)
2218 rc1 = kernel_sock_shutdown(smc->clcsock, how);
2219 /* map sock_shutdown_cmd constants to sk_shutdown value range */
2220 sk->sk_shutdown |= how + 1;
2224 return rc ? rc : rc1;
2227 static int smc_setsockopt(struct socket *sock, int level, int optname,
2228 sockptr_t optval, unsigned int optlen)
2230 struct sock *sk = sock->sk;
2231 struct smc_sock *smc;
2234 if (level == SOL_TCP && optname == TCP_ULP)
2239 /* generic setsockopts reaching us here always apply to the
2242 if (unlikely(!smc->clcsock->ops->setsockopt))
2245 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
2247 if (smc->clcsock->sk->sk_err) {
2248 sk->sk_err = smc->clcsock->sk->sk_err;
2249 sk_error_report(sk);
2252 if (optlen < sizeof(int))
2254 if (copy_from_sockptr(&val, optval, sizeof(int)))
2258 if (rc || smc->use_fallback)
2262 case TCP_FASTOPEN_CONNECT:
2263 case TCP_FASTOPEN_KEY:
2264 case TCP_FASTOPEN_NO_COOKIE:
2265 /* option not supported by SMC */
2266 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
2267 smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP);
2273 if (sk->sk_state != SMC_INIT &&
2274 sk->sk_state != SMC_LISTEN &&
2275 sk->sk_state != SMC_CLOSED) {
2277 SMC_STAT_INC(smc, ndly_cnt);
2278 mod_delayed_work(smc->conn.lgr->tx_wq,
2279 &smc->conn.tx_work, 0);
2284 if (sk->sk_state != SMC_INIT &&
2285 sk->sk_state != SMC_LISTEN &&
2286 sk->sk_state != SMC_CLOSED) {
2288 SMC_STAT_INC(smc, cork_cnt);
2289 mod_delayed_work(smc->conn.lgr->tx_wq,
2290 &smc->conn.tx_work, 0);
2294 case TCP_DEFER_ACCEPT:
2295 smc->sockopt_defer_accept = val;
2306 static int smc_getsockopt(struct socket *sock, int level, int optname,
2307 char __user *optval, int __user *optlen)
2309 struct smc_sock *smc;
2311 smc = smc_sk(sock->sk);
2312 /* socket options apply to the CLC socket */
2313 if (unlikely(!smc->clcsock->ops->getsockopt))
2315 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
2319 static int smc_ioctl(struct socket *sock, unsigned int cmd,
2322 union smc_host_cursor cons, urg;
2323 struct smc_connection *conn;
2324 struct smc_sock *smc;
2327 smc = smc_sk(sock->sk);
2329 lock_sock(&smc->sk);
2330 if (smc->use_fallback) {
2331 if (!smc->clcsock) {
2332 release_sock(&smc->sk);
2335 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
2336 release_sock(&smc->sk);
2340 case SIOCINQ: /* same as FIONREAD */
2341 if (smc->sk.sk_state == SMC_LISTEN) {
2342 release_sock(&smc->sk);
2345 if (smc->sk.sk_state == SMC_INIT ||
2346 smc->sk.sk_state == SMC_CLOSED)
2349 answ = atomic_read(&smc->conn.bytes_to_rcv);
2352 /* output queue size (not send + not acked) */
2353 if (smc->sk.sk_state == SMC_LISTEN) {
2354 release_sock(&smc->sk);
2357 if (smc->sk.sk_state == SMC_INIT ||
2358 smc->sk.sk_state == SMC_CLOSED)
2361 answ = smc->conn.sndbuf_desc->len -
2362 atomic_read(&smc->conn.sndbuf_space);
2365 /* output queue size (not send only) */
2366 if (smc->sk.sk_state == SMC_LISTEN) {
2367 release_sock(&smc->sk);
2370 if (smc->sk.sk_state == SMC_INIT ||
2371 smc->sk.sk_state == SMC_CLOSED)
2374 answ = smc_tx_prepared_sends(&smc->conn);
2377 if (smc->sk.sk_state == SMC_LISTEN) {
2378 release_sock(&smc->sk);
2381 if (smc->sk.sk_state == SMC_INIT ||
2382 smc->sk.sk_state == SMC_CLOSED) {
2385 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
2386 smc_curs_copy(&urg, &conn->urg_curs, conn);
2387 answ = smc_curs_diff(conn->rmb_desc->len,
2392 release_sock(&smc->sk);
2393 return -ENOIOCTLCMD;
2395 release_sock(&smc->sk);
2397 return put_user(answ, (int __user *)arg);
2400 static ssize_t smc_sendpage(struct socket *sock, struct page *page,
2401 int offset, size_t size, int flags)
2403 struct sock *sk = sock->sk;
2404 struct smc_sock *smc;
2409 if (sk->sk_state != SMC_ACTIVE) {
2414 if (smc->use_fallback) {
2415 rc = kernel_sendpage(smc->clcsock, page, offset,
2418 SMC_STAT_INC(smc, sendpage_cnt);
2419 rc = sock_no_sendpage(sock, page, offset, size, flags);
2426 /* Map the affected portions of the rmbe into an spd, note the number of bytes
2427 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
2428 * updates till whenever a respective page has been fully processed.
2429 * Note that subsequent recv() calls have to wait till all splice() processing
2432 static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
2433 struct pipe_inode_info *pipe, size_t len,
2436 struct sock *sk = sock->sk;
2437 struct smc_sock *smc;
2442 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2443 /* socket was connected before, no more data to read */
2447 if (sk->sk_state == SMC_INIT ||
2448 sk->sk_state == SMC_LISTEN ||
2449 sk->sk_state == SMC_CLOSED)
2452 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2457 if (smc->use_fallback) {
2458 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
2465 if (flags & SPLICE_F_NONBLOCK)
2466 flags = MSG_DONTWAIT;
2469 SMC_STAT_INC(smc, splice_cnt);
2470 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
2478 /* must look like tcp */
2479 static const struct proto_ops smc_sock_ops = {
2481 .owner = THIS_MODULE,
2482 .release = smc_release,
2484 .connect = smc_connect,
2485 .socketpair = sock_no_socketpair,
2486 .accept = smc_accept,
2487 .getname = smc_getname,
2490 .listen = smc_listen,
2491 .shutdown = smc_shutdown,
2492 .setsockopt = smc_setsockopt,
2493 .getsockopt = smc_getsockopt,
2494 .sendmsg = smc_sendmsg,
2495 .recvmsg = smc_recvmsg,
2496 .mmap = sock_no_mmap,
2497 .sendpage = smc_sendpage,
2498 .splice_read = smc_splice_read,
2501 static int smc_create(struct net *net, struct socket *sock, int protocol,
2504 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
2505 struct smc_sock *smc;
2509 rc = -ESOCKTNOSUPPORT;
2510 if (sock->type != SOCK_STREAM)
2513 rc = -EPROTONOSUPPORT;
2514 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
2518 sock->ops = &smc_sock_ops;
2519 sk = smc_sock_alloc(net, sock, protocol);
2523 /* create internal TCP socket for CLC handshake and fallback */
2525 smc->use_fallback = false; /* assume rdma capability first */
2526 smc->fallback_rsn = 0;
2527 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
2530 sk_common_release(sk);
2533 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
2534 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
2540 static const struct net_proto_family smc_sock_family_ops = {
2542 .owner = THIS_MODULE,
2543 .create = smc_create,
2546 unsigned int smc_net_id;
2548 static __net_init int smc_net_init(struct net *net)
2550 return smc_pnet_net_init(net);
2553 static void __net_exit smc_net_exit(struct net *net)
2555 smc_pnet_net_exit(net);
2558 static __net_init int smc_net_stat_init(struct net *net)
2560 return smc_stats_init(net);
2563 static void __net_exit smc_net_stat_exit(struct net *net)
2565 smc_stats_exit(net);
2568 static struct pernet_operations smc_net_ops = {
2569 .init = smc_net_init,
2570 .exit = smc_net_exit,
2572 .size = sizeof(struct smc_net),
2575 static struct pernet_operations smc_net_stat_ops = {
2576 .init = smc_net_stat_init,
2577 .exit = smc_net_stat_exit,
2580 static int __init smc_init(void)
2584 rc = register_pernet_subsys(&smc_net_ops);
2588 rc = register_pernet_subsys(&smc_net_stat_ops);
2597 goto out_pernet_subsys;
2599 rc = smc_pnet_init();
2604 smc_hs_wq = alloc_workqueue("smc_hs_wq", 0, 0);
2608 smc_close_wq = alloc_workqueue("smc_close_wq", 0, 0);
2610 goto out_alloc_hs_wq;
2612 rc = smc_core_init();
2614 pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
2618 rc = smc_llc_init();
2620 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
2624 rc = smc_cdc_init();
2626 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
2630 rc = proto_register(&smc_proto, 1);
2632 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
2636 rc = proto_register(&smc_proto6, 1);
2638 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2642 rc = sock_register(&smc_sock_family_ops);
2644 pr_err("%s: sock_register fails with %d\n", __func__, rc);
2647 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
2648 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
2650 rc = smc_ib_register_client();
2652 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2656 static_branch_enable(&tcp_have_smc);
2660 sock_unregister(PF_SMC);
2662 proto_unregister(&smc_proto6);
2664 proto_unregister(&smc_proto);
2668 destroy_workqueue(smc_close_wq);
2670 destroy_workqueue(smc_hs_wq);
2676 unregister_pernet_subsys(&smc_net_ops);
2681 static void __exit smc_exit(void)
2683 static_branch_disable(&tcp_have_smc);
2684 sock_unregister(PF_SMC);
2686 smc_ib_unregister_client();
2687 destroy_workqueue(smc_close_wq);
2688 destroy_workqueue(smc_hs_wq);
2689 proto_unregister(&smc_proto6);
2690 proto_unregister(&smc_proto);
2693 unregister_pernet_subsys(&smc_net_stat_ops);
2694 unregister_pernet_subsys(&smc_net_ops);
2698 module_init(smc_init);
2699 module_exit(smc_exit);
2701 MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2702 MODULE_DESCRIPTION("smc socket address family");
2703 MODULE_LICENSE("GPL");
2704 MODULE_ALIAS_NETPROTO(PF_SMC);