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>
33 #include <asm/ioctls.h>
35 #include <net/net_namespace.h>
36 #include <net/netns/generic.h>
37 #include "smc_netns.h"
49 #include "smc_close.h"
51 static DEFINE_MUTEX(smc_server_lgr_pending
); /* serialize link group
54 static DEFINE_MUTEX(smc_client_lgr_pending
); /* serialize link group
58 static void smc_tcp_listen_work(struct work_struct
*);
59 static void smc_connect_work(struct work_struct
*);
61 static void smc_set_keepalive(struct sock
*sk
, int val
)
63 struct smc_sock
*smc
= smc_sk(sk
);
65 smc
->clcsock
->sk
->sk_prot
->keepalive(smc
->clcsock
->sk
, val
);
68 static struct smc_hashinfo smc_v4_hashinfo
= {
69 .lock
= __RW_LOCK_UNLOCKED(smc_v4_hashinfo
.lock
),
72 static struct smc_hashinfo smc_v6_hashinfo
= {
73 .lock
= __RW_LOCK_UNLOCKED(smc_v6_hashinfo
.lock
),
76 int smc_hash_sk(struct sock
*sk
)
78 struct smc_hashinfo
*h
= sk
->sk_prot
->h
.smc_hash
;
79 struct hlist_head
*head
;
83 write_lock_bh(&h
->lock
);
84 sk_add_node(sk
, head
);
85 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, 1);
86 write_unlock_bh(&h
->lock
);
90 EXPORT_SYMBOL_GPL(smc_hash_sk
);
92 void smc_unhash_sk(struct sock
*sk
)
94 struct smc_hashinfo
*h
= sk
->sk_prot
->h
.smc_hash
;
96 write_lock_bh(&h
->lock
);
97 if (sk_del_node_init(sk
))
98 sock_prot_inuse_add(sock_net(sk
), sk
->sk_prot
, -1);
99 write_unlock_bh(&h
->lock
);
101 EXPORT_SYMBOL_GPL(smc_unhash_sk
);
103 struct proto smc_proto
= {
105 .owner
= THIS_MODULE
,
106 .keepalive
= smc_set_keepalive
,
108 .unhash
= smc_unhash_sk
,
109 .obj_size
= sizeof(struct smc_sock
),
110 .h
.smc_hash
= &smc_v4_hashinfo
,
111 .slab_flags
= SLAB_TYPESAFE_BY_RCU
,
113 EXPORT_SYMBOL_GPL(smc_proto
);
115 struct proto smc_proto6
= {
117 .owner
= THIS_MODULE
,
118 .keepalive
= smc_set_keepalive
,
120 .unhash
= smc_unhash_sk
,
121 .obj_size
= sizeof(struct smc_sock
),
122 .h
.smc_hash
= &smc_v6_hashinfo
,
123 .slab_flags
= SLAB_TYPESAFE_BY_RCU
,
125 EXPORT_SYMBOL_GPL(smc_proto6
);
127 static void smc_restore_fallback_changes(struct smc_sock
*smc
)
129 smc
->clcsock
->file
->private_data
= smc
->sk
.sk_socket
;
130 smc
->clcsock
->file
= NULL
;
133 static int __smc_release(struct smc_sock
*smc
)
135 struct sock
*sk
= &smc
->sk
;
138 if (!smc
->use_fallback
) {
139 rc
= smc_close_active(smc
);
140 sock_set_flag(sk
, SOCK_DEAD
);
141 sk
->sk_shutdown
|= SHUTDOWN_MASK
;
143 if (sk
->sk_state
!= SMC_LISTEN
&& sk
->sk_state
!= SMC_INIT
)
144 sock_put(sk
); /* passive closing */
145 if (sk
->sk_state
== SMC_LISTEN
) {
146 /* wake up clcsock accept */
147 rc
= kernel_sock_shutdown(smc
->clcsock
, SHUT_RDWR
);
149 sk
->sk_state
= SMC_CLOSED
;
150 sk
->sk_state_change(sk
);
151 smc_restore_fallback_changes(smc
);
154 sk
->sk_prot
->unhash(sk
);
156 if (sk
->sk_state
== SMC_CLOSED
) {
159 smc_clcsock_release(smc
);
162 if (!smc
->use_fallback
)
163 smc_conn_free(&smc
->conn
);
169 static int smc_release(struct socket
*sock
)
171 struct sock
*sk
= sock
->sk
;
172 struct smc_sock
*smc
;
178 sock_hold(sk
); /* sock_put below */
181 /* cleanup for a dangling non-blocking connect */
182 if (smc
->connect_nonblock
&& sk
->sk_state
== SMC_INIT
)
183 tcp_abort(smc
->clcsock
->sk
, ECONNABORTED
);
184 flush_work(&smc
->connect_work
);
186 if (sk
->sk_state
== SMC_LISTEN
)
187 /* smc_close_non_accepted() is called and acquires
188 * sock lock for child sockets again
190 lock_sock_nested(sk
, SINGLE_DEPTH_NESTING
);
194 rc
= __smc_release(smc
);
201 sock_put(sk
); /* sock_hold above */
202 sock_put(sk
); /* final sock_put */
207 static void smc_destruct(struct sock
*sk
)
209 if (sk
->sk_state
!= SMC_CLOSED
)
211 if (!sock_flag(sk
, SOCK_DEAD
))
214 sk_refcnt_debug_dec(sk
);
217 static struct sock
*smc_sock_alloc(struct net
*net
, struct socket
*sock
,
220 struct smc_sock
*smc
;
224 prot
= (protocol
== SMCPROTO_SMC6
) ? &smc_proto6
: &smc_proto
;
225 sk
= sk_alloc(net
, PF_SMC
, GFP_KERNEL
, prot
, 0);
229 sock_init_data(sock
, sk
); /* sets sk_refcnt to 1 */
230 sk
->sk_state
= SMC_INIT
;
231 sk
->sk_destruct
= smc_destruct
;
232 sk
->sk_protocol
= protocol
;
234 INIT_WORK(&smc
->tcp_listen_work
, smc_tcp_listen_work
);
235 INIT_WORK(&smc
->connect_work
, smc_connect_work
);
236 INIT_DELAYED_WORK(&smc
->conn
.tx_work
, smc_tx_work
);
237 INIT_LIST_HEAD(&smc
->accept_q
);
238 spin_lock_init(&smc
->accept_q_lock
);
239 spin_lock_init(&smc
->conn
.send_lock
);
240 sk
->sk_prot
->hash(sk
);
241 sk_refcnt_debug_inc(sk
);
242 mutex_init(&smc
->clcsock_release_lock
);
247 static int smc_bind(struct socket
*sock
, struct sockaddr
*uaddr
,
250 struct sockaddr_in
*addr
= (struct sockaddr_in
*)uaddr
;
251 struct sock
*sk
= sock
->sk
;
252 struct smc_sock
*smc
;
257 /* replicate tests from inet_bind(), to be safe wrt. future changes */
259 if (addr_len
< sizeof(struct sockaddr_in
))
263 if (addr
->sin_family
!= AF_INET
&&
264 addr
->sin_family
!= AF_INET6
&&
265 addr
->sin_family
!= AF_UNSPEC
)
267 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
268 if (addr
->sin_family
== AF_UNSPEC
&&
269 addr
->sin_addr
.s_addr
!= htonl(INADDR_ANY
))
274 /* Check if socket is already active */
276 if (sk
->sk_state
!= SMC_INIT
|| smc
->connect_nonblock
)
279 smc
->clcsock
->sk
->sk_reuse
= sk
->sk_reuse
;
280 rc
= kernel_bind(smc
->clcsock
, uaddr
, addr_len
);
288 static void smc_copy_sock_settings(struct sock
*nsk
, struct sock
*osk
,
291 /* options we don't get control via setsockopt for */
292 nsk
->sk_type
= osk
->sk_type
;
293 nsk
->sk_sndbuf
= osk
->sk_sndbuf
;
294 nsk
->sk_rcvbuf
= osk
->sk_rcvbuf
;
295 nsk
->sk_sndtimeo
= osk
->sk_sndtimeo
;
296 nsk
->sk_rcvtimeo
= osk
->sk_rcvtimeo
;
297 nsk
->sk_mark
= osk
->sk_mark
;
298 nsk
->sk_priority
= osk
->sk_priority
;
299 nsk
->sk_rcvlowat
= osk
->sk_rcvlowat
;
300 nsk
->sk_bound_dev_if
= osk
->sk_bound_dev_if
;
301 nsk
->sk_err
= osk
->sk_err
;
303 nsk
->sk_flags
&= ~mask
;
304 nsk
->sk_flags
|= osk
->sk_flags
& mask
;
307 #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
308 (1UL << SOCK_KEEPOPEN) | \
309 (1UL << SOCK_LINGER) | \
310 (1UL << SOCK_BROADCAST) | \
311 (1UL << SOCK_TIMESTAMP) | \
312 (1UL << SOCK_DBG) | \
313 (1UL << SOCK_RCVTSTAMP) | \
314 (1UL << SOCK_RCVTSTAMPNS) | \
315 (1UL << SOCK_LOCALROUTE) | \
316 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
317 (1UL << SOCK_RXQ_OVFL) | \
318 (1UL << SOCK_WIFI_STATUS) | \
319 (1UL << SOCK_NOFCS) | \
320 (1UL << SOCK_FILTER_LOCKED) | \
321 (1UL << SOCK_TSTAMP_NEW))
322 /* copy only relevant settings and flags of SOL_SOCKET level from smc to
323 * clc socket (since smc is not called for these options from net/core)
325 static void smc_copy_sock_settings_to_clc(struct smc_sock
*smc
)
327 smc_copy_sock_settings(smc
->clcsock
->sk
, &smc
->sk
, SK_FLAGS_SMC_TO_CLC
);
330 #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
331 (1UL << SOCK_KEEPOPEN) | \
332 (1UL << SOCK_LINGER) | \
334 /* copy only settings and flags relevant for smc from clc to smc socket */
335 static void smc_copy_sock_settings_to_smc(struct smc_sock
*smc
)
337 smc_copy_sock_settings(&smc
->sk
, smc
->clcsock
->sk
, SK_FLAGS_CLC_TO_SMC
);
340 /* register a new rmb, send confirm_rkey msg to register with peer */
341 static int smc_reg_rmb(struct smc_link
*link
, struct smc_buf_desc
*rmb_desc
,
344 if (!rmb_desc
->wr_reg
) {
345 /* register memory region for new rmb */
346 if (smc_wr_reg_send(link
, rmb_desc
->mr_rx
[SMC_SINGLE_LINK
])) {
347 rmb_desc
->regerr
= 1;
350 rmb_desc
->wr_reg
= 1;
354 /* exchange confirm_rkey msg with peer */
355 if (smc_llc_do_confirm_rkey(link
, rmb_desc
)) {
356 rmb_desc
->regerr
= 1;
362 static int smc_clnt_conf_first_link(struct smc_sock
*smc
)
364 struct net
*net
= sock_net(smc
->clcsock
->sk
);
365 struct smc_link_group
*lgr
= smc
->conn
.lgr
;
366 struct smc_link
*link
;
370 link
= &lgr
->lnk
[SMC_SINGLE_LINK
];
371 /* receive CONFIRM LINK request from server over RoCE fabric */
372 rest
= wait_for_completion_interruptible_timeout(
374 SMC_LLC_WAIT_FIRST_TIME
);
376 struct smc_clc_msg_decline dclc
;
378 rc
= smc_clc_wait_msg(smc
, &dclc
, sizeof(dclc
),
379 SMC_CLC_DECLINE
, CLC_WAIT_TIME_SHORT
);
380 return rc
== -EAGAIN
? SMC_CLC_DECL_TIMEOUT_CL
: rc
;
383 if (link
->llc_confirm_rc
)
384 return SMC_CLC_DECL_RMBE_EC
;
386 rc
= smc_ib_modify_qp_rts(link
);
388 return SMC_CLC_DECL_ERR_RDYLNK
;
390 smc_wr_remember_qp_attr(link
);
392 if (smc_reg_rmb(link
, smc
->conn
.rmb_desc
, false))
393 return SMC_CLC_DECL_ERR_REGRMB
;
395 /* send CONFIRM LINK response over RoCE fabric */
396 rc
= smc_llc_send_confirm_link(link
, SMC_LLC_RESP
);
398 return SMC_CLC_DECL_TIMEOUT_CL
;
400 /* receive ADD LINK request from server over RoCE fabric */
401 rest
= wait_for_completion_interruptible_timeout(&link
->llc_add
,
404 struct smc_clc_msg_decline dclc
;
406 rc
= smc_clc_wait_msg(smc
, &dclc
, sizeof(dclc
),
407 SMC_CLC_DECLINE
, CLC_WAIT_TIME_SHORT
);
408 return rc
== -EAGAIN
? SMC_CLC_DECL_TIMEOUT_AL
: rc
;
411 /* send add link reject message, only one link supported for now */
412 rc
= smc_llc_send_add_link(link
,
413 link
->smcibdev
->mac
[link
->ibport
- 1],
414 link
->gid
, SMC_LLC_RESP
);
416 return SMC_CLC_DECL_TIMEOUT_AL
;
418 smc_llc_link_active(link
, net
->ipv4
.sysctl_tcp_keepalive_time
);
423 static void smcr_conn_save_peer_info(struct smc_sock
*smc
,
424 struct smc_clc_msg_accept_confirm
*clc
)
426 int bufsize
= smc_uncompress_bufsize(clc
->rmbe_size
);
428 smc
->conn
.peer_rmbe_idx
= clc
->rmbe_idx
;
429 smc
->conn
.local_tx_ctrl
.token
= ntohl(clc
->rmbe_alert_token
);
430 smc
->conn
.peer_rmbe_size
= bufsize
;
431 atomic_set(&smc
->conn
.peer_rmbe_space
, smc
->conn
.peer_rmbe_size
);
432 smc
->conn
.tx_off
= bufsize
* (smc
->conn
.peer_rmbe_idx
- 1);
435 static void smcd_conn_save_peer_info(struct smc_sock
*smc
,
436 struct smc_clc_msg_accept_confirm
*clc
)
438 int bufsize
= smc_uncompress_bufsize(clc
->dmbe_size
);
440 smc
->conn
.peer_rmbe_idx
= clc
->dmbe_idx
;
441 smc
->conn
.peer_token
= clc
->token
;
442 /* msg header takes up space in the buffer */
443 smc
->conn
.peer_rmbe_size
= bufsize
- sizeof(struct smcd_cdc_msg
);
444 atomic_set(&smc
->conn
.peer_rmbe_space
, smc
->conn
.peer_rmbe_size
);
445 smc
->conn
.tx_off
= bufsize
* smc
->conn
.peer_rmbe_idx
;
448 static void smc_conn_save_peer_info(struct smc_sock
*smc
,
449 struct smc_clc_msg_accept_confirm
*clc
)
451 if (smc
->conn
.lgr
->is_smcd
)
452 smcd_conn_save_peer_info(smc
, clc
);
454 smcr_conn_save_peer_info(smc
, clc
);
457 static void smc_link_save_peer_info(struct smc_link
*link
,
458 struct smc_clc_msg_accept_confirm
*clc
)
460 link
->peer_qpn
= ntoh24(clc
->qpn
);
461 memcpy(link
->peer_gid
, clc
->lcl
.gid
, SMC_GID_SIZE
);
462 memcpy(link
->peer_mac
, clc
->lcl
.mac
, sizeof(link
->peer_mac
));
463 link
->peer_psn
= ntoh24(clc
->psn
);
464 link
->peer_mtu
= clc
->qp_mtu
;
467 static void smc_switch_to_fallback(struct smc_sock
*smc
)
469 smc
->use_fallback
= true;
470 if (smc
->sk
.sk_socket
&& smc
->sk
.sk_socket
->file
) {
471 smc
->clcsock
->file
= smc
->sk
.sk_socket
->file
;
472 smc
->clcsock
->file
->private_data
= smc
->clcsock
;
476 /* fall back during connect */
477 static int smc_connect_fallback(struct smc_sock
*smc
, int reason_code
)
479 smc_switch_to_fallback(smc
);
480 smc
->fallback_rsn
= reason_code
;
481 smc_copy_sock_settings_to_clc(smc
);
482 smc
->connect_nonblock
= 0;
483 if (smc
->sk
.sk_state
== SMC_INIT
)
484 smc
->sk
.sk_state
= SMC_ACTIVE
;
488 /* decline and fall back during connect */
489 static int smc_connect_decline_fallback(struct smc_sock
*smc
, int reason_code
)
493 if (reason_code
< 0) { /* error, fallback is not possible */
494 if (smc
->sk
.sk_state
== SMC_INIT
)
495 sock_put(&smc
->sk
); /* passive closing */
498 if (reason_code
!= SMC_CLC_DECL_PEERDECL
) {
499 rc
= smc_clc_send_decline(smc
, reason_code
);
501 if (smc
->sk
.sk_state
== SMC_INIT
)
502 sock_put(&smc
->sk
); /* passive closing */
506 return smc_connect_fallback(smc
, reason_code
);
509 /* abort connecting */
510 static int smc_connect_abort(struct smc_sock
*smc
, int reason_code
,
513 if (local_contact
== SMC_FIRST_CONTACT
)
514 smc_lgr_forget(smc
->conn
.lgr
);
515 if (smc
->conn
.lgr
->is_smcd
)
516 /* there is only one lgr role for SMC-D; use server lock */
517 mutex_unlock(&smc_server_lgr_pending
);
519 mutex_unlock(&smc_client_lgr_pending
);
521 smc_conn_free(&smc
->conn
);
522 smc
->connect_nonblock
= 0;
526 /* check if there is a rdma device available for this connection. */
527 /* called for connect and listen */
528 static int smc_find_rdma_device(struct smc_sock
*smc
, struct smc_init_info
*ini
)
530 /* PNET table look up: search active ib_device and port
531 * within same PNETID that also contains the ethernet device
532 * used for the internal TCP socket
534 smc_pnet_find_roce_resource(smc
->clcsock
->sk
, ini
);
536 return SMC_CLC_DECL_NOSMCRDEV
;
540 /* check if there is an ISM device available for this connection. */
541 /* called for connect and listen */
542 static int smc_find_ism_device(struct smc_sock
*smc
, struct smc_init_info
*ini
)
544 /* Find ISM device with same PNETID as connecting interface */
545 smc_pnet_find_ism_resource(smc
->clcsock
->sk
, ini
);
547 return SMC_CLC_DECL_NOSMCDDEV
;
551 /* Check for VLAN ID and register it on ISM device just for CLC handshake */
552 static int smc_connect_ism_vlan_setup(struct smc_sock
*smc
,
553 struct smc_init_info
*ini
)
555 if (ini
->vlan_id
&& smc_ism_get_vlan(ini
->ism_dev
, ini
->vlan_id
))
556 return SMC_CLC_DECL_ISMVLANERR
;
560 /* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
561 * used, the VLAN ID will be registered again during the connection setup.
563 static int smc_connect_ism_vlan_cleanup(struct smc_sock
*smc
, bool is_smcd
,
564 struct smc_init_info
*ini
)
568 if (ini
->vlan_id
&& smc_ism_put_vlan(ini
->ism_dev
, ini
->vlan_id
))
569 return SMC_CLC_DECL_CNFERR
;
573 /* CLC handshake during connect */
574 static int smc_connect_clc(struct smc_sock
*smc
, int smc_type
,
575 struct smc_clc_msg_accept_confirm
*aclc
,
576 struct smc_init_info
*ini
)
580 /* do inband token exchange */
581 rc
= smc_clc_send_proposal(smc
, smc_type
, ini
);
584 /* receive SMC Accept CLC message */
585 return smc_clc_wait_msg(smc
, aclc
, sizeof(*aclc
), SMC_CLC_ACCEPT
,
589 /* setup for RDMA connection of client */
590 static int smc_connect_rdma(struct smc_sock
*smc
,
591 struct smc_clc_msg_accept_confirm
*aclc
,
592 struct smc_init_info
*ini
)
594 struct smc_link
*link
;
597 ini
->is_smcd
= false;
598 ini
->ib_lcl
= &aclc
->lcl
;
599 ini
->ib_clcqpn
= ntoh24(aclc
->qpn
);
600 ini
->srv_first_contact
= aclc
->hdr
.flag
;
602 mutex_lock(&smc_client_lgr_pending
);
603 reason_code
= smc_conn_create(smc
, ini
);
605 mutex_unlock(&smc_client_lgr_pending
);
608 link
= &smc
->conn
.lgr
->lnk
[SMC_SINGLE_LINK
];
610 smc_conn_save_peer_info(smc
, aclc
);
612 /* create send buffer and rmb */
613 if (smc_buf_create(smc
, false))
614 return smc_connect_abort(smc
, SMC_CLC_DECL_MEM
,
615 ini
->cln_first_contact
);
617 if (ini
->cln_first_contact
== SMC_FIRST_CONTACT
)
618 smc_link_save_peer_info(link
, aclc
);
620 if (smc_rmb_rtoken_handling(&smc
->conn
, aclc
))
621 return smc_connect_abort(smc
, SMC_CLC_DECL_ERR_RTOK
,
622 ini
->cln_first_contact
);
627 if (ini
->cln_first_contact
== SMC_FIRST_CONTACT
) {
628 if (smc_ib_ready_link(link
))
629 return smc_connect_abort(smc
, SMC_CLC_DECL_ERR_RDYLNK
,
630 ini
->cln_first_contact
);
632 if (smc_reg_rmb(link
, smc
->conn
.rmb_desc
, true))
633 return smc_connect_abort(smc
, SMC_CLC_DECL_ERR_REGRMB
,
634 ini
->cln_first_contact
);
636 smc_rmb_sync_sg_for_device(&smc
->conn
);
638 reason_code
= smc_clc_send_confirm(smc
);
640 return smc_connect_abort(smc
, reason_code
,
641 ini
->cln_first_contact
);
645 if (ini
->cln_first_contact
== SMC_FIRST_CONTACT
) {
646 /* QP confirmation over RoCE fabric */
647 reason_code
= smc_clnt_conf_first_link(smc
);
649 return smc_connect_abort(smc
, reason_code
,
650 ini
->cln_first_contact
);
652 mutex_unlock(&smc_client_lgr_pending
);
654 smc_copy_sock_settings_to_clc(smc
);
655 smc
->connect_nonblock
= 0;
656 if (smc
->sk
.sk_state
== SMC_INIT
)
657 smc
->sk
.sk_state
= SMC_ACTIVE
;
662 /* setup for ISM connection of client */
663 static int smc_connect_ism(struct smc_sock
*smc
,
664 struct smc_clc_msg_accept_confirm
*aclc
,
665 struct smc_init_info
*ini
)
670 ini
->ism_gid
= aclc
->gid
;
671 ini
->srv_first_contact
= aclc
->hdr
.flag
;
673 /* there is only one lgr role for SMC-D; use server lock */
674 mutex_lock(&smc_server_lgr_pending
);
675 rc
= smc_conn_create(smc
, ini
);
677 mutex_unlock(&smc_server_lgr_pending
);
681 /* Create send and receive buffers */
682 if (smc_buf_create(smc
, true))
683 return smc_connect_abort(smc
, SMC_CLC_DECL_MEM
,
684 ini
->cln_first_contact
);
686 smc_conn_save_peer_info(smc
, aclc
);
691 rc
= smc_clc_send_confirm(smc
);
693 return smc_connect_abort(smc
, rc
, ini
->cln_first_contact
);
694 mutex_unlock(&smc_server_lgr_pending
);
696 smc_copy_sock_settings_to_clc(smc
);
697 smc
->connect_nonblock
= 0;
698 if (smc
->sk
.sk_state
== SMC_INIT
)
699 smc
->sk
.sk_state
= SMC_ACTIVE
;
704 /* perform steps before actually connecting */
705 static int __smc_connect(struct smc_sock
*smc
)
707 bool ism_supported
= false, rdma_supported
= false;
708 struct smc_clc_msg_accept_confirm aclc
;
709 struct smc_init_info ini
= {0};
713 if (smc
->use_fallback
)
714 return smc_connect_fallback(smc
, smc
->fallback_rsn
);
716 /* if peer has not signalled SMC-capability, fall back */
717 if (!tcp_sk(smc
->clcsock
->sk
)->syn_smc
)
718 return smc_connect_fallback(smc
, SMC_CLC_DECL_PEERNOSMC
);
720 /* IPSec connections opt out of SMC-R optimizations */
721 if (using_ipsec(smc
))
722 return smc_connect_decline_fallback(smc
, SMC_CLC_DECL_IPSEC
);
724 /* get vlan id from IP device */
725 if (smc_vlan_by_tcpsk(smc
->clcsock
, &ini
))
726 return smc_connect_decline_fallback(smc
,
727 SMC_CLC_DECL_GETVLANERR
);
729 /* check if there is an ism device available */
730 if (!smc_find_ism_device(smc
, &ini
) &&
731 !smc_connect_ism_vlan_setup(smc
, &ini
)) {
732 /* ISM is supported for this connection */
733 ism_supported
= true;
734 smc_type
= SMC_TYPE_D
;
737 /* check if there is a rdma device available */
738 if (!smc_find_rdma_device(smc
, &ini
)) {
739 /* RDMA is supported for this connection */
740 rdma_supported
= true;
742 smc_type
= SMC_TYPE_B
; /* both */
744 smc_type
= SMC_TYPE_R
; /* only RDMA */
747 /* if neither ISM nor RDMA are supported, fallback */
748 if (!rdma_supported
&& !ism_supported
)
749 return smc_connect_decline_fallback(smc
, SMC_CLC_DECL_NOSMCDEV
);
751 /* perform CLC handshake */
752 rc
= smc_connect_clc(smc
, smc_type
, &aclc
, &ini
);
754 smc_connect_ism_vlan_cleanup(smc
, ism_supported
, &ini
);
755 return smc_connect_decline_fallback(smc
, rc
);
758 /* depending on previous steps, connect using rdma or ism */
759 if (rdma_supported
&& aclc
.hdr
.path
== SMC_TYPE_R
)
760 rc
= smc_connect_rdma(smc
, &aclc
, &ini
);
761 else if (ism_supported
&& aclc
.hdr
.path
== SMC_TYPE_D
)
762 rc
= smc_connect_ism(smc
, &aclc
, &ini
);
764 rc
= SMC_CLC_DECL_MODEUNSUPP
;
766 smc_connect_ism_vlan_cleanup(smc
, ism_supported
, &ini
);
767 return smc_connect_decline_fallback(smc
, rc
);
770 smc_connect_ism_vlan_cleanup(smc
, ism_supported
, &ini
);
774 static void smc_connect_work(struct work_struct
*work
)
776 struct smc_sock
*smc
= container_of(work
, struct smc_sock
,
778 long timeo
= smc
->sk
.sk_sndtimeo
;
782 timeo
= MAX_SCHEDULE_TIMEOUT
;
783 lock_sock(smc
->clcsock
->sk
);
784 if (smc
->clcsock
->sk
->sk_err
) {
785 smc
->sk
.sk_err
= smc
->clcsock
->sk
->sk_err
;
786 } else if ((1 << smc
->clcsock
->sk
->sk_state
) &
787 (TCPF_SYN_SENT
| TCP_SYN_RECV
)) {
788 rc
= sk_stream_wait_connect(smc
->clcsock
->sk
, &timeo
);
789 if ((rc
== -EPIPE
) &&
790 ((1 << smc
->clcsock
->sk
->sk_state
) &
791 (TCPF_ESTABLISHED
| TCPF_CLOSE_WAIT
)))
794 release_sock(smc
->clcsock
->sk
);
796 if (rc
!= 0 || smc
->sk
.sk_err
) {
797 smc
->sk
.sk_state
= SMC_CLOSED
;
798 if (rc
== -EPIPE
|| rc
== -EAGAIN
)
799 smc
->sk
.sk_err
= EPIPE
;
800 else if (signal_pending(current
))
801 smc
->sk
.sk_err
= -sock_intr_errno(timeo
);
802 sock_put(&smc
->sk
); /* passive closing */
806 rc
= __smc_connect(smc
);
808 smc
->sk
.sk_err
= -rc
;
811 if (!sock_flag(&smc
->sk
, SOCK_DEAD
)) {
812 if (smc
->sk
.sk_err
) {
813 smc
->sk
.sk_state_change(&smc
->sk
);
814 } else { /* allow polling before and after fallback decision */
815 smc
->clcsock
->sk
->sk_write_space(smc
->clcsock
->sk
);
816 smc
->sk
.sk_write_space(&smc
->sk
);
819 release_sock(&smc
->sk
);
822 static int smc_connect(struct socket
*sock
, struct sockaddr
*addr
,
825 struct sock
*sk
= sock
->sk
;
826 struct smc_sock
*smc
;
831 /* separate smc parameter checking to be safe */
832 if (alen
< sizeof(addr
->sa_family
))
834 if (addr
->sa_family
!= AF_INET
&& addr
->sa_family
!= AF_INET6
)
838 switch (sk
->sk_state
) {
849 smc_copy_sock_settings_to_clc(smc
);
850 tcp_sk(smc
->clcsock
->sk
)->syn_smc
= 1;
851 if (smc
->connect_nonblock
) {
855 rc
= kernel_connect(smc
->clcsock
, addr
, alen
, flags
);
856 if (rc
&& rc
!= -EINPROGRESS
)
859 sock_hold(&smc
->sk
); /* sock put in passive closing */
860 if (flags
& O_NONBLOCK
) {
861 if (schedule_work(&smc
->connect_work
))
862 smc
->connect_nonblock
= 1;
865 rc
= __smc_connect(smc
);
869 rc
= 0; /* success cases including fallback */
878 static int smc_clcsock_accept(struct smc_sock
*lsmc
, struct smc_sock
**new_smc
)
880 struct socket
*new_clcsock
= NULL
;
881 struct sock
*lsk
= &lsmc
->sk
;
886 new_sk
= smc_sock_alloc(sock_net(lsk
), NULL
, lsk
->sk_protocol
);
889 lsk
->sk_err
= ENOMEM
;
894 *new_smc
= smc_sk(new_sk
);
896 mutex_lock(&lsmc
->clcsock_release_lock
);
898 rc
= kernel_accept(lsmc
->clcsock
, &new_clcsock
, 0);
899 mutex_unlock(&lsmc
->clcsock_release_lock
);
903 if (rc
< 0 || lsk
->sk_state
== SMC_CLOSED
) {
904 new_sk
->sk_prot
->unhash(new_sk
);
906 sock_release(new_clcsock
);
907 new_sk
->sk_state
= SMC_CLOSED
;
908 sock_set_flag(new_sk
, SOCK_DEAD
);
909 sock_put(new_sk
); /* final */
914 (*new_smc
)->clcsock
= new_clcsock
;
919 /* add a just created sock to the accept queue of the listen sock as
920 * candidate for a following socket accept call from user space
922 static void smc_accept_enqueue(struct sock
*parent
, struct sock
*sk
)
924 struct smc_sock
*par
= smc_sk(parent
);
926 sock_hold(sk
); /* sock_put in smc_accept_unlink () */
927 spin_lock(&par
->accept_q_lock
);
928 list_add_tail(&smc_sk(sk
)->accept_q
, &par
->accept_q
);
929 spin_unlock(&par
->accept_q_lock
);
930 sk_acceptq_added(parent
);
933 /* remove a socket from the accept queue of its parental listening socket */
934 static void smc_accept_unlink(struct sock
*sk
)
936 struct smc_sock
*par
= smc_sk(sk
)->listen_smc
;
938 spin_lock(&par
->accept_q_lock
);
939 list_del_init(&smc_sk(sk
)->accept_q
);
940 spin_unlock(&par
->accept_q_lock
);
941 sk_acceptq_removed(&smc_sk(sk
)->listen_smc
->sk
);
942 sock_put(sk
); /* sock_hold in smc_accept_enqueue */
945 /* remove a sock from the accept queue to bind it to a new socket created
946 * for a socket accept call from user space
948 struct sock
*smc_accept_dequeue(struct sock
*parent
,
949 struct socket
*new_sock
)
951 struct smc_sock
*isk
, *n
;
954 list_for_each_entry_safe(isk
, n
, &smc_sk(parent
)->accept_q
, accept_q
) {
955 new_sk
= (struct sock
*)isk
;
957 smc_accept_unlink(new_sk
);
958 if (new_sk
->sk_state
== SMC_CLOSED
) {
959 new_sk
->sk_prot
->unhash(new_sk
);
961 sock_release(isk
->clcsock
);
964 sock_put(new_sk
); /* final */
968 sock_graft(new_sk
, new_sock
);
969 if (isk
->use_fallback
) {
970 smc_sk(new_sk
)->clcsock
->file
= new_sock
->file
;
971 isk
->clcsock
->file
->private_data
= isk
->clcsock
;
979 /* clean up for a created but never accepted sock */
980 void smc_close_non_accepted(struct sock
*sk
)
982 struct smc_sock
*smc
= smc_sk(sk
);
984 sock_hold(sk
); /* sock_put below */
986 if (!sk
->sk_lingertime
)
987 /* wait for peer closing */
988 sk
->sk_lingertime
= SMC_MAX_STREAM_WAIT_TIMEOUT
;
991 sock_put(sk
); /* sock_hold above */
992 sock_put(sk
); /* final sock_put */
995 static int smc_serv_conf_first_link(struct smc_sock
*smc
)
997 struct net
*net
= sock_net(smc
->clcsock
->sk
);
998 struct smc_link_group
*lgr
= smc
->conn
.lgr
;
999 struct smc_link
*link
;
1003 link
= &lgr
->lnk
[SMC_SINGLE_LINK
];
1005 if (smc_reg_rmb(link
, smc
->conn
.rmb_desc
, false))
1006 return SMC_CLC_DECL_ERR_REGRMB
;
1008 /* send CONFIRM LINK request to client over the RoCE fabric */
1009 rc
= smc_llc_send_confirm_link(link
, SMC_LLC_REQ
);
1011 return SMC_CLC_DECL_TIMEOUT_CL
;
1013 /* receive CONFIRM LINK response from client over the RoCE fabric */
1014 rest
= wait_for_completion_interruptible_timeout(
1015 &link
->llc_confirm_resp
,
1016 SMC_LLC_WAIT_FIRST_TIME
);
1018 struct smc_clc_msg_decline dclc
;
1020 rc
= smc_clc_wait_msg(smc
, &dclc
, sizeof(dclc
),
1021 SMC_CLC_DECLINE
, CLC_WAIT_TIME_SHORT
);
1022 return rc
== -EAGAIN
? SMC_CLC_DECL_TIMEOUT_CL
: rc
;
1025 if (link
->llc_confirm_resp_rc
)
1026 return SMC_CLC_DECL_RMBE_EC
;
1028 /* send ADD LINK request to client over the RoCE fabric */
1029 rc
= smc_llc_send_add_link(link
,
1030 link
->smcibdev
->mac
[link
->ibport
- 1],
1031 link
->gid
, SMC_LLC_REQ
);
1033 return SMC_CLC_DECL_TIMEOUT_AL
;
1035 /* receive ADD LINK response from client over the RoCE fabric */
1036 rest
= wait_for_completion_interruptible_timeout(&link
->llc_add_resp
,
1039 struct smc_clc_msg_decline dclc
;
1041 rc
= smc_clc_wait_msg(smc
, &dclc
, sizeof(dclc
),
1042 SMC_CLC_DECLINE
, CLC_WAIT_TIME_SHORT
);
1043 return rc
== -EAGAIN
? SMC_CLC_DECL_TIMEOUT_AL
: rc
;
1046 smc_llc_link_active(link
, net
->ipv4
.sysctl_tcp_keepalive_time
);
1051 /* listen worker: finish */
1052 static void smc_listen_out(struct smc_sock
*new_smc
)
1054 struct smc_sock
*lsmc
= new_smc
->listen_smc
;
1055 struct sock
*newsmcsk
= &new_smc
->sk
;
1057 if (lsmc
->sk
.sk_state
== SMC_LISTEN
) {
1058 lock_sock_nested(&lsmc
->sk
, SINGLE_DEPTH_NESTING
);
1059 smc_accept_enqueue(&lsmc
->sk
, newsmcsk
);
1060 release_sock(&lsmc
->sk
);
1061 } else { /* no longer listening */
1062 smc_close_non_accepted(newsmcsk
);
1065 /* Wake up accept */
1066 lsmc
->sk
.sk_data_ready(&lsmc
->sk
);
1067 sock_put(&lsmc
->sk
); /* sock_hold in smc_tcp_listen_work */
1070 /* listen worker: finish in state connected */
1071 static void smc_listen_out_connected(struct smc_sock
*new_smc
)
1073 struct sock
*newsmcsk
= &new_smc
->sk
;
1075 sk_refcnt_debug_inc(newsmcsk
);
1076 if (newsmcsk
->sk_state
== SMC_INIT
)
1077 newsmcsk
->sk_state
= SMC_ACTIVE
;
1079 smc_listen_out(new_smc
);
1082 /* listen worker: finish in error state */
1083 static void smc_listen_out_err(struct smc_sock
*new_smc
)
1085 struct sock
*newsmcsk
= &new_smc
->sk
;
1087 if (newsmcsk
->sk_state
== SMC_INIT
)
1088 sock_put(&new_smc
->sk
); /* passive closing */
1089 newsmcsk
->sk_state
= SMC_CLOSED
;
1090 smc_conn_free(&new_smc
->conn
);
1092 smc_listen_out(new_smc
);
1095 /* listen worker: decline and fall back if possible */
1096 static void smc_listen_decline(struct smc_sock
*new_smc
, int reason_code
,
1099 /* RDMA setup failed, switch back to TCP */
1100 if (local_contact
== SMC_FIRST_CONTACT
)
1101 smc_lgr_forget(new_smc
->conn
.lgr
);
1102 if (reason_code
< 0) { /* error, no fallback possible */
1103 smc_listen_out_err(new_smc
);
1106 smc_conn_free(&new_smc
->conn
);
1107 smc_switch_to_fallback(new_smc
);
1108 new_smc
->fallback_rsn
= reason_code
;
1109 if (reason_code
&& reason_code
!= SMC_CLC_DECL_PEERDECL
) {
1110 if (smc_clc_send_decline(new_smc
, reason_code
) < 0) {
1111 smc_listen_out_err(new_smc
);
1115 smc_listen_out_connected(new_smc
);
1118 /* listen worker: check prefixes */
1119 static int smc_listen_prfx_check(struct smc_sock
*new_smc
,
1120 struct smc_clc_msg_proposal
*pclc
)
1122 struct smc_clc_msg_proposal_prefix
*pclc_prfx
;
1123 struct socket
*newclcsock
= new_smc
->clcsock
;
1125 pclc_prfx
= smc_clc_proposal_get_prefix(pclc
);
1126 if (smc_clc_prfx_match(newclcsock
, pclc_prfx
))
1127 return SMC_CLC_DECL_DIFFPREFIX
;
1132 /* listen worker: initialize connection and buffers */
1133 static int smc_listen_rdma_init(struct smc_sock
*new_smc
,
1134 struct smc_init_info
*ini
)
1138 /* allocate connection / link group */
1139 rc
= smc_conn_create(new_smc
, ini
);
1143 /* create send buffer and rmb */
1144 if (smc_buf_create(new_smc
, false))
1145 return SMC_CLC_DECL_MEM
;
1150 /* listen worker: initialize connection and buffers for SMC-D */
1151 static int smc_listen_ism_init(struct smc_sock
*new_smc
,
1152 struct smc_clc_msg_proposal
*pclc
,
1153 struct smc_init_info
*ini
)
1155 struct smc_clc_msg_smcd
*pclc_smcd
;
1158 pclc_smcd
= smc_get_clc_msg_smcd(pclc
);
1159 ini
->ism_gid
= pclc_smcd
->gid
;
1160 rc
= smc_conn_create(new_smc
, ini
);
1164 /* Check if peer can be reached via ISM device */
1165 if (smc_ism_cantalk(new_smc
->conn
.lgr
->peer_gid
,
1166 new_smc
->conn
.lgr
->vlan_id
,
1167 new_smc
->conn
.lgr
->smcd
)) {
1168 if (ini
->cln_first_contact
== SMC_FIRST_CONTACT
)
1169 smc_lgr_forget(new_smc
->conn
.lgr
);
1170 smc_conn_free(&new_smc
->conn
);
1171 return SMC_CLC_DECL_SMCDNOTALK
;
1174 /* Create send and receive buffers */
1175 if (smc_buf_create(new_smc
, true)) {
1176 if (ini
->cln_first_contact
== SMC_FIRST_CONTACT
)
1177 smc_lgr_forget(new_smc
->conn
.lgr
);
1178 smc_conn_free(&new_smc
->conn
);
1179 return SMC_CLC_DECL_MEM
;
1185 /* listen worker: register buffers */
1186 static int smc_listen_rdma_reg(struct smc_sock
*new_smc
, int local_contact
)
1188 struct smc_link
*link
= &new_smc
->conn
.lgr
->lnk
[SMC_SINGLE_LINK
];
1190 if (local_contact
!= SMC_FIRST_CONTACT
) {
1191 if (smc_reg_rmb(link
, new_smc
->conn
.rmb_desc
, true))
1192 return SMC_CLC_DECL_ERR_REGRMB
;
1194 smc_rmb_sync_sg_for_device(&new_smc
->conn
);
1199 /* listen worker: finish RDMA setup */
1200 static int smc_listen_rdma_finish(struct smc_sock
*new_smc
,
1201 struct smc_clc_msg_accept_confirm
*cclc
,
1204 struct smc_link
*link
= &new_smc
->conn
.lgr
->lnk
[SMC_SINGLE_LINK
];
1205 int reason_code
= 0;
1207 if (local_contact
== SMC_FIRST_CONTACT
)
1208 smc_link_save_peer_info(link
, cclc
);
1210 if (smc_rmb_rtoken_handling(&new_smc
->conn
, cclc
)) {
1211 reason_code
= SMC_CLC_DECL_ERR_RTOK
;
1215 if (local_contact
== SMC_FIRST_CONTACT
) {
1216 if (smc_ib_ready_link(link
)) {
1217 reason_code
= SMC_CLC_DECL_ERR_RDYLNK
;
1220 /* QP confirmation over RoCE fabric */
1221 reason_code
= smc_serv_conf_first_link(new_smc
);
1228 smc_listen_decline(new_smc
, reason_code
, local_contact
);
1232 /* setup for RDMA connection of server */
1233 static void smc_listen_work(struct work_struct
*work
)
1235 struct smc_sock
*new_smc
= container_of(work
, struct smc_sock
,
1237 struct socket
*newclcsock
= new_smc
->clcsock
;
1238 struct smc_clc_msg_accept_confirm cclc
;
1239 struct smc_clc_msg_proposal
*pclc
;
1240 struct smc_init_info ini
= {0};
1241 bool ism_supported
= false;
1242 u8 buf
[SMC_CLC_MAX_LEN
];
1245 if (new_smc
->listen_smc
->sk
.sk_state
!= SMC_LISTEN
)
1246 return smc_listen_out_err(new_smc
);
1248 if (new_smc
->use_fallback
) {
1249 smc_listen_out_connected(new_smc
);
1253 /* check if peer is smc capable */
1254 if (!tcp_sk(newclcsock
->sk
)->syn_smc
) {
1255 smc_switch_to_fallback(new_smc
);
1256 new_smc
->fallback_rsn
= SMC_CLC_DECL_PEERNOSMC
;
1257 smc_listen_out_connected(new_smc
);
1261 /* do inband token exchange -
1262 * wait for and receive SMC Proposal CLC message
1264 pclc
= (struct smc_clc_msg_proposal
*)&buf
;
1265 rc
= smc_clc_wait_msg(new_smc
, pclc
, SMC_CLC_MAX_LEN
,
1266 SMC_CLC_PROPOSAL
, CLC_WAIT_TIME
);
1270 /* IPSec connections opt out of SMC-R optimizations */
1271 if (using_ipsec(new_smc
)) {
1272 rc
= SMC_CLC_DECL_IPSEC
;
1276 /* check for matching IP prefix and subnet length */
1277 rc
= smc_listen_prfx_check(new_smc
, pclc
);
1281 /* get vlan id from IP device */
1282 if (smc_vlan_by_tcpsk(new_smc
->clcsock
, &ini
)) {
1283 rc
= SMC_CLC_DECL_GETVLANERR
;
1287 mutex_lock(&smc_server_lgr_pending
);
1288 smc_close_init(new_smc
);
1289 smc_rx_init(new_smc
);
1290 smc_tx_init(new_smc
);
1292 /* check if ISM is available */
1293 if (pclc
->hdr
.path
== SMC_TYPE_D
|| pclc
->hdr
.path
== SMC_TYPE_B
) {
1294 ini
.is_smcd
= true; /* prepare ISM check */
1295 rc
= smc_find_ism_device(new_smc
, &ini
);
1297 rc
= smc_listen_ism_init(new_smc
, pclc
, &ini
);
1299 ism_supported
= true;
1300 else if (pclc
->hdr
.path
== SMC_TYPE_D
)
1301 goto out_unlock
; /* skip RDMA and decline */
1304 /* check if RDMA is available */
1305 if (!ism_supported
) { /* SMC_TYPE_R or SMC_TYPE_B */
1306 /* prepare RDMA check */
1307 ini
.is_smcd
= false;
1309 ini
.ib_lcl
= &pclc
->lcl
;
1310 rc
= smc_find_rdma_device(new_smc
, &ini
);
1312 /* no RDMA device found */
1313 if (pclc
->hdr
.path
== SMC_TYPE_B
)
1314 /* neither ISM nor RDMA device found */
1315 rc
= SMC_CLC_DECL_NOSMCDEV
;
1318 rc
= smc_listen_rdma_init(new_smc
, &ini
);
1321 rc
= smc_listen_rdma_reg(new_smc
, ini
.cln_first_contact
);
1326 /* send SMC Accept CLC message */
1327 rc
= smc_clc_send_accept(new_smc
, ini
.cln_first_contact
);
1331 /* SMC-D does not need this lock any more */
1333 mutex_unlock(&smc_server_lgr_pending
);
1335 /* receive SMC Confirm CLC message */
1336 rc
= smc_clc_wait_msg(new_smc
, &cclc
, sizeof(cclc
),
1337 SMC_CLC_CONFIRM
, CLC_WAIT_TIME
);
1345 if (!ism_supported
) {
1346 rc
= smc_listen_rdma_finish(new_smc
, &cclc
,
1347 ini
.cln_first_contact
);
1348 mutex_unlock(&smc_server_lgr_pending
);
1352 smc_conn_save_peer_info(new_smc
, &cclc
);
1353 smc_listen_out_connected(new_smc
);
1357 mutex_unlock(&smc_server_lgr_pending
);
1359 smc_listen_decline(new_smc
, rc
, ini
.cln_first_contact
);
1362 static void smc_tcp_listen_work(struct work_struct
*work
)
1364 struct smc_sock
*lsmc
= container_of(work
, struct smc_sock
,
1366 struct sock
*lsk
= &lsmc
->sk
;
1367 struct smc_sock
*new_smc
;
1371 while (lsk
->sk_state
== SMC_LISTEN
) {
1372 rc
= smc_clcsock_accept(lsmc
, &new_smc
);
1378 new_smc
->listen_smc
= lsmc
;
1379 new_smc
->use_fallback
= lsmc
->use_fallback
;
1380 new_smc
->fallback_rsn
= lsmc
->fallback_rsn
;
1381 sock_hold(lsk
); /* sock_put in smc_listen_work */
1382 INIT_WORK(&new_smc
->smc_listen_work
, smc_listen_work
);
1383 smc_copy_sock_settings_to_smc(new_smc
);
1384 new_smc
->sk
.sk_sndbuf
= lsmc
->sk
.sk_sndbuf
;
1385 new_smc
->sk
.sk_rcvbuf
= lsmc
->sk
.sk_rcvbuf
;
1386 sock_hold(&new_smc
->sk
); /* sock_put in passive closing */
1387 if (!schedule_work(&new_smc
->smc_listen_work
))
1388 sock_put(&new_smc
->sk
);
1393 sock_put(&lsmc
->sk
); /* sock_hold in smc_listen */
1396 static int smc_listen(struct socket
*sock
, int backlog
)
1398 struct sock
*sk
= sock
->sk
;
1399 struct smc_sock
*smc
;
1406 if ((sk
->sk_state
!= SMC_INIT
&& sk
->sk_state
!= SMC_LISTEN
) ||
1407 smc
->connect_nonblock
)
1411 if (sk
->sk_state
== SMC_LISTEN
) {
1412 sk
->sk_max_ack_backlog
= backlog
;
1415 /* some socket options are handled in core, so we could not apply
1416 * them to the clc socket -- copy smc socket options to clc socket
1418 smc_copy_sock_settings_to_clc(smc
);
1419 if (!smc
->use_fallback
)
1420 tcp_sk(smc
->clcsock
->sk
)->syn_smc
= 1;
1422 rc
= kernel_listen(smc
->clcsock
, backlog
);
1425 sk
->sk_max_ack_backlog
= backlog
;
1426 sk
->sk_ack_backlog
= 0;
1427 sk
->sk_state
= SMC_LISTEN
;
1428 sock_hold(sk
); /* sock_hold in tcp_listen_worker */
1429 if (!schedule_work(&smc
->tcp_listen_work
))
1437 static int smc_accept(struct socket
*sock
, struct socket
*new_sock
,
1438 int flags
, bool kern
)
1440 struct sock
*sk
= sock
->sk
, *nsk
;
1441 DECLARE_WAITQUEUE(wait
, current
);
1442 struct smc_sock
*lsmc
;
1447 sock_hold(sk
); /* sock_put below */
1450 if (lsmc
->sk
.sk_state
!= SMC_LISTEN
) {
1456 /* Wait for an incoming connection */
1457 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
1458 add_wait_queue_exclusive(sk_sleep(sk
), &wait
);
1459 while (!(nsk
= smc_accept_dequeue(sk
, new_sock
))) {
1460 set_current_state(TASK_INTERRUPTIBLE
);
1466 timeo
= schedule_timeout(timeo
);
1467 /* wakeup by sk_data_ready in smc_listen_work() */
1468 sched_annotate_sleep();
1470 if (signal_pending(current
)) {
1471 rc
= sock_intr_errno(timeo
);
1475 set_current_state(TASK_RUNNING
);
1476 remove_wait_queue(sk_sleep(sk
), &wait
);
1479 rc
= sock_error(nsk
);
1484 if (lsmc
->sockopt_defer_accept
&& !(flags
& O_NONBLOCK
)) {
1485 /* wait till data arrives on the socket */
1486 timeo
= msecs_to_jiffies(lsmc
->sockopt_defer_accept
*
1488 if (smc_sk(nsk
)->use_fallback
) {
1489 struct sock
*clcsk
= smc_sk(nsk
)->clcsock
->sk
;
1492 if (skb_queue_empty(&clcsk
->sk_receive_queue
))
1493 sk_wait_data(clcsk
, &timeo
, NULL
);
1494 release_sock(clcsk
);
1495 } else if (!atomic_read(&smc_sk(nsk
)->conn
.bytes_to_rcv
)) {
1497 smc_rx_wait(smc_sk(nsk
), &timeo
, smc_rx_data_available
);
1503 sock_put(sk
); /* sock_hold above */
1507 static int smc_getname(struct socket
*sock
, struct sockaddr
*addr
,
1510 struct smc_sock
*smc
;
1512 if (peer
&& (sock
->sk
->sk_state
!= SMC_ACTIVE
) &&
1513 (sock
->sk
->sk_state
!= SMC_APPCLOSEWAIT1
))
1516 smc
= smc_sk(sock
->sk
);
1518 return smc
->clcsock
->ops
->getname(smc
->clcsock
, addr
, peer
);
1521 static int smc_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
)
1523 struct sock
*sk
= sock
->sk
;
1524 struct smc_sock
*smc
;
1529 if ((sk
->sk_state
!= SMC_ACTIVE
) &&
1530 (sk
->sk_state
!= SMC_APPCLOSEWAIT1
) &&
1531 (sk
->sk_state
!= SMC_INIT
))
1534 if (msg
->msg_flags
& MSG_FASTOPEN
) {
1535 if (sk
->sk_state
== SMC_INIT
&& !smc
->connect_nonblock
) {
1536 smc_switch_to_fallback(smc
);
1537 smc
->fallback_rsn
= SMC_CLC_DECL_OPTUNSUPP
;
1544 if (smc
->use_fallback
)
1545 rc
= smc
->clcsock
->ops
->sendmsg(smc
->clcsock
, msg
, len
);
1547 rc
= smc_tx_sendmsg(smc
, msg
, len
);
1553 static int smc_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
,
1556 struct sock
*sk
= sock
->sk
;
1557 struct smc_sock
*smc
;
1562 if (sk
->sk_state
== SMC_CLOSED
&& (sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
1563 /* socket was connected before, no more data to read */
1567 if ((sk
->sk_state
== SMC_INIT
) ||
1568 (sk
->sk_state
== SMC_LISTEN
) ||
1569 (sk
->sk_state
== SMC_CLOSED
))
1572 if (sk
->sk_state
== SMC_PEERFINCLOSEWAIT
) {
1577 if (smc
->use_fallback
) {
1578 rc
= smc
->clcsock
->ops
->recvmsg(smc
->clcsock
, msg
, len
, flags
);
1580 msg
->msg_namelen
= 0;
1581 rc
= smc_rx_recvmsg(smc
, msg
, NULL
, len
, flags
);
1589 static __poll_t
smc_accept_poll(struct sock
*parent
)
1591 struct smc_sock
*isk
= smc_sk(parent
);
1594 spin_lock(&isk
->accept_q_lock
);
1595 if (!list_empty(&isk
->accept_q
))
1596 mask
= EPOLLIN
| EPOLLRDNORM
;
1597 spin_unlock(&isk
->accept_q_lock
);
1602 static __poll_t
smc_poll(struct file
*file
, struct socket
*sock
,
1605 struct sock
*sk
= sock
->sk
;
1606 struct smc_sock
*smc
;
1612 smc
= smc_sk(sock
->sk
);
1613 if (smc
->use_fallback
) {
1614 /* delegate to CLC child sock */
1615 mask
= smc
->clcsock
->ops
->poll(file
, smc
->clcsock
, wait
);
1616 sk
->sk_err
= smc
->clcsock
->sk
->sk_err
;
1618 if (sk
->sk_state
!= SMC_CLOSED
)
1619 sock_poll_wait(file
, sock
, wait
);
1622 if ((sk
->sk_shutdown
== SHUTDOWN_MASK
) ||
1623 (sk
->sk_state
== SMC_CLOSED
))
1625 if (sk
->sk_state
== SMC_LISTEN
) {
1626 /* woken up by sk_data_ready in smc_listen_work() */
1627 mask
|= smc_accept_poll(sk
);
1628 } else if (smc
->use_fallback
) { /* as result of connect_work()*/
1629 mask
|= smc
->clcsock
->ops
->poll(file
, smc
->clcsock
,
1631 sk
->sk_err
= smc
->clcsock
->sk
->sk_err
;
1633 if ((sk
->sk_state
!= SMC_INIT
&&
1634 atomic_read(&smc
->conn
.sndbuf_space
)) ||
1635 sk
->sk_shutdown
& SEND_SHUTDOWN
) {
1636 mask
|= EPOLLOUT
| EPOLLWRNORM
;
1638 sk_set_bit(SOCKWQ_ASYNC_NOSPACE
, sk
);
1639 set_bit(SOCK_NOSPACE
, &sk
->sk_socket
->flags
);
1641 if (atomic_read(&smc
->conn
.bytes_to_rcv
))
1642 mask
|= EPOLLIN
| EPOLLRDNORM
;
1643 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
1644 mask
|= EPOLLIN
| EPOLLRDNORM
| EPOLLRDHUP
;
1645 if (sk
->sk_state
== SMC_APPCLOSEWAIT1
)
1647 if (smc
->conn
.urg_state
== SMC_URG_VALID
)
1655 static int smc_shutdown(struct socket
*sock
, int how
)
1657 struct sock
*sk
= sock
->sk
;
1658 struct smc_sock
*smc
;
1664 if ((how
< SHUT_RD
) || (how
> SHUT_RDWR
))
1670 if ((sk
->sk_state
!= SMC_ACTIVE
) &&
1671 (sk
->sk_state
!= SMC_PEERCLOSEWAIT1
) &&
1672 (sk
->sk_state
!= SMC_PEERCLOSEWAIT2
) &&
1673 (sk
->sk_state
!= SMC_APPCLOSEWAIT1
) &&
1674 (sk
->sk_state
!= SMC_APPCLOSEWAIT2
) &&
1675 (sk
->sk_state
!= SMC_APPFINCLOSEWAIT
))
1677 if (smc
->use_fallback
) {
1678 rc
= kernel_sock_shutdown(smc
->clcsock
, how
);
1679 sk
->sk_shutdown
= smc
->clcsock
->sk
->sk_shutdown
;
1680 if (sk
->sk_shutdown
== SHUTDOWN_MASK
)
1681 sk
->sk_state
= SMC_CLOSED
;
1685 case SHUT_RDWR
: /* shutdown in both directions */
1686 rc
= smc_close_active(smc
);
1689 rc
= smc_close_shutdown_write(smc
);
1693 /* nothing more to do because peer is not involved */
1697 rc1
= kernel_sock_shutdown(smc
->clcsock
, how
);
1698 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1699 sk
->sk_shutdown
|= how
+ 1;
1703 return rc
? rc
: rc1
;
1706 static int smc_setsockopt(struct socket
*sock
, int level
, int optname
,
1707 char __user
*optval
, unsigned int optlen
)
1709 struct sock
*sk
= sock
->sk
;
1710 struct smc_sock
*smc
;
1715 /* generic setsockopts reaching us here always apply to the
1718 rc
= smc
->clcsock
->ops
->setsockopt(smc
->clcsock
, level
, optname
,
1720 if (smc
->clcsock
->sk
->sk_err
) {
1721 sk
->sk_err
= smc
->clcsock
->sk
->sk_err
;
1722 sk
->sk_error_report(sk
);
1727 if (optlen
< sizeof(int))
1729 if (get_user(val
, (int __user
*)optval
))
1736 case TCP_FASTOPEN_CONNECT
:
1737 case TCP_FASTOPEN_KEY
:
1738 case TCP_FASTOPEN_NO_COOKIE
:
1739 /* option not supported by SMC */
1740 if (sk
->sk_state
== SMC_INIT
&& !smc
->connect_nonblock
) {
1741 smc_switch_to_fallback(smc
);
1742 smc
->fallback_rsn
= SMC_CLC_DECL_OPTUNSUPP
;
1744 if (!smc
->use_fallback
)
1749 if (sk
->sk_state
!= SMC_INIT
&&
1750 sk
->sk_state
!= SMC_LISTEN
&&
1751 sk
->sk_state
!= SMC_CLOSED
) {
1752 if (val
&& !smc
->use_fallback
)
1753 mod_delayed_work(system_wq
, &smc
->conn
.tx_work
,
1758 if (sk
->sk_state
!= SMC_INIT
&&
1759 sk
->sk_state
!= SMC_LISTEN
&&
1760 sk
->sk_state
!= SMC_CLOSED
) {
1761 if (!val
&& !smc
->use_fallback
)
1762 mod_delayed_work(system_wq
, &smc
->conn
.tx_work
,
1766 case TCP_DEFER_ACCEPT
:
1767 smc
->sockopt_defer_accept
= val
;
1777 static int smc_getsockopt(struct socket
*sock
, int level
, int optname
,
1778 char __user
*optval
, int __user
*optlen
)
1780 struct smc_sock
*smc
;
1782 smc
= smc_sk(sock
->sk
);
1783 /* socket options apply to the CLC socket */
1784 return smc
->clcsock
->ops
->getsockopt(smc
->clcsock
, level
, optname
,
1788 static int smc_ioctl(struct socket
*sock
, unsigned int cmd
,
1791 union smc_host_cursor cons
, urg
;
1792 struct smc_connection
*conn
;
1793 struct smc_sock
*smc
;
1796 smc
= smc_sk(sock
->sk
);
1798 lock_sock(&smc
->sk
);
1799 if (smc
->use_fallback
) {
1800 if (!smc
->clcsock
) {
1801 release_sock(&smc
->sk
);
1804 answ
= smc
->clcsock
->ops
->ioctl(smc
->clcsock
, cmd
, arg
);
1805 release_sock(&smc
->sk
);
1809 case SIOCINQ
: /* same as FIONREAD */
1810 if (smc
->sk
.sk_state
== SMC_LISTEN
) {
1811 release_sock(&smc
->sk
);
1814 if (smc
->sk
.sk_state
== SMC_INIT
||
1815 smc
->sk
.sk_state
== SMC_CLOSED
)
1818 answ
= atomic_read(&smc
->conn
.bytes_to_rcv
);
1821 /* output queue size (not send + not acked) */
1822 if (smc
->sk
.sk_state
== SMC_LISTEN
) {
1823 release_sock(&smc
->sk
);
1826 if (smc
->sk
.sk_state
== SMC_INIT
||
1827 smc
->sk
.sk_state
== SMC_CLOSED
)
1830 answ
= smc
->conn
.sndbuf_desc
->len
-
1831 atomic_read(&smc
->conn
.sndbuf_space
);
1834 /* output queue size (not send only) */
1835 if (smc
->sk
.sk_state
== SMC_LISTEN
) {
1836 release_sock(&smc
->sk
);
1839 if (smc
->sk
.sk_state
== SMC_INIT
||
1840 smc
->sk
.sk_state
== SMC_CLOSED
)
1843 answ
= smc_tx_prepared_sends(&smc
->conn
);
1846 if (smc
->sk
.sk_state
== SMC_LISTEN
) {
1847 release_sock(&smc
->sk
);
1850 if (smc
->sk
.sk_state
== SMC_INIT
||
1851 smc
->sk
.sk_state
== SMC_CLOSED
) {
1854 smc_curs_copy(&cons
, &conn
->local_tx_ctrl
.cons
, conn
);
1855 smc_curs_copy(&urg
, &conn
->urg_curs
, conn
);
1856 answ
= smc_curs_diff(conn
->rmb_desc
->len
,
1861 release_sock(&smc
->sk
);
1862 return -ENOIOCTLCMD
;
1864 release_sock(&smc
->sk
);
1866 return put_user(answ
, (int __user
*)arg
);
1869 static ssize_t
smc_sendpage(struct socket
*sock
, struct page
*page
,
1870 int offset
, size_t size
, int flags
)
1872 struct sock
*sk
= sock
->sk
;
1873 struct smc_sock
*smc
;
1878 if (sk
->sk_state
!= SMC_ACTIVE
) {
1883 if (smc
->use_fallback
)
1884 rc
= kernel_sendpage(smc
->clcsock
, page
, offset
,
1887 rc
= sock_no_sendpage(sock
, page
, offset
, size
, flags
);
1893 /* Map the affected portions of the rmbe into an spd, note the number of bytes
1894 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1895 * updates till whenever a respective page has been fully processed.
1896 * Note that subsequent recv() calls have to wait till all splice() processing
1899 static ssize_t
smc_splice_read(struct socket
*sock
, loff_t
*ppos
,
1900 struct pipe_inode_info
*pipe
, size_t len
,
1903 struct sock
*sk
= sock
->sk
;
1904 struct smc_sock
*smc
;
1909 if (sk
->sk_state
== SMC_CLOSED
&& (sk
->sk_shutdown
& RCV_SHUTDOWN
)) {
1910 /* socket was connected before, no more data to read */
1914 if (sk
->sk_state
== SMC_INIT
||
1915 sk
->sk_state
== SMC_LISTEN
||
1916 sk
->sk_state
== SMC_CLOSED
)
1919 if (sk
->sk_state
== SMC_PEERFINCLOSEWAIT
) {
1924 if (smc
->use_fallback
) {
1925 rc
= smc
->clcsock
->ops
->splice_read(smc
->clcsock
, ppos
,
1932 if (flags
& SPLICE_F_NONBLOCK
)
1933 flags
= MSG_DONTWAIT
;
1936 rc
= smc_rx_recvmsg(smc
, NULL
, pipe
, len
, flags
);
1944 /* must look like tcp */
1945 static const struct proto_ops smc_sock_ops
= {
1947 .owner
= THIS_MODULE
,
1948 .release
= smc_release
,
1950 .connect
= smc_connect
,
1951 .socketpair
= sock_no_socketpair
,
1952 .accept
= smc_accept
,
1953 .getname
= smc_getname
,
1956 .listen
= smc_listen
,
1957 .shutdown
= smc_shutdown
,
1958 .setsockopt
= smc_setsockopt
,
1959 .getsockopt
= smc_getsockopt
,
1960 .sendmsg
= smc_sendmsg
,
1961 .recvmsg
= smc_recvmsg
,
1962 .mmap
= sock_no_mmap
,
1963 .sendpage
= smc_sendpage
,
1964 .splice_read
= smc_splice_read
,
1967 static int smc_create(struct net
*net
, struct socket
*sock
, int protocol
,
1970 int family
= (protocol
== SMCPROTO_SMC6
) ? PF_INET6
: PF_INET
;
1971 struct smc_sock
*smc
;
1975 rc
= -ESOCKTNOSUPPORT
;
1976 if (sock
->type
!= SOCK_STREAM
)
1979 rc
= -EPROTONOSUPPORT
;
1980 if (protocol
!= SMCPROTO_SMC
&& protocol
!= SMCPROTO_SMC6
)
1984 sock
->ops
= &smc_sock_ops
;
1985 sk
= smc_sock_alloc(net
, sock
, protocol
);
1989 /* create internal TCP socket for CLC handshake and fallback */
1991 smc
->use_fallback
= false; /* assume rdma capability first */
1992 smc
->fallback_rsn
= 0;
1993 rc
= sock_create_kern(net
, family
, SOCK_STREAM
, IPPROTO_TCP
,
1996 sk_common_release(sk
);
1999 smc
->sk
.sk_sndbuf
= max(smc
->clcsock
->sk
->sk_sndbuf
, SMC_BUF_MIN_SIZE
);
2000 smc
->sk
.sk_rcvbuf
= max(smc
->clcsock
->sk
->sk_rcvbuf
, SMC_BUF_MIN_SIZE
);
2006 static const struct net_proto_family smc_sock_family_ops
= {
2008 .owner
= THIS_MODULE
,
2009 .create
= smc_create
,
2012 unsigned int smc_net_id
;
2014 static __net_init
int smc_net_init(struct net
*net
)
2016 return smc_pnet_net_init(net
);
2019 static void __net_exit
smc_net_exit(struct net
*net
)
2021 smc_pnet_net_exit(net
);
2024 static struct pernet_operations smc_net_ops
= {
2025 .init
= smc_net_init
,
2026 .exit
= smc_net_exit
,
2028 .size
= sizeof(struct smc_net
),
2031 static int __init
smc_init(void)
2035 rc
= register_pernet_subsys(&smc_net_ops
);
2039 rc
= smc_pnet_init();
2041 goto out_pernet_subsys
;
2043 rc
= smc_core_init();
2045 pr_err("%s: smc_core_init fails with %d\n", __func__
, rc
);
2049 rc
= smc_llc_init();
2051 pr_err("%s: smc_llc_init fails with %d\n", __func__
, rc
);
2055 rc
= smc_cdc_init();
2057 pr_err("%s: smc_cdc_init fails with %d\n", __func__
, rc
);
2061 rc
= proto_register(&smc_proto
, 1);
2063 pr_err("%s: proto_register(v4) fails with %d\n", __func__
, rc
);
2067 rc
= proto_register(&smc_proto6
, 1);
2069 pr_err("%s: proto_register(v6) fails with %d\n", __func__
, rc
);
2073 rc
= sock_register(&smc_sock_family_ops
);
2075 pr_err("%s: sock_register fails with %d\n", __func__
, rc
);
2078 INIT_HLIST_HEAD(&smc_v4_hashinfo
.ht
);
2079 INIT_HLIST_HEAD(&smc_v6_hashinfo
.ht
);
2081 rc
= smc_ib_register_client();
2083 pr_err("%s: ib_register fails with %d\n", __func__
, rc
);
2087 static_branch_enable(&tcp_have_smc
);
2091 sock_unregister(PF_SMC
);
2093 proto_unregister(&smc_proto6
);
2095 proto_unregister(&smc_proto
);
2101 unregister_pernet_subsys(&smc_net_ops
);
2106 static void __exit
smc_exit(void)
2108 static_branch_disable(&tcp_have_smc
);
2109 sock_unregister(PF_SMC
);
2111 smc_ib_unregister_client();
2112 proto_unregister(&smc_proto6
);
2113 proto_unregister(&smc_proto
);
2115 unregister_pernet_subsys(&smc_net_ops
);
2119 module_init(smc_init
);
2120 module_exit(smc_exit
);
2122 MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2123 MODULE_DESCRIPTION("smc socket address family");
2124 MODULE_LICENSE("GPL");
2125 MODULE_ALIAS_NETPROTO(PF_SMC
);