xref: /openbmc/linux/net/smc/af_smc.c (revision 673d6066)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Shared Memory Communications over RDMA (SMC-R) and RoCE
4  *
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
9  *
10  *  Initial restrictions:
11  *    - support for alternate links postponed
12  *
13  *  Copyright IBM Corp. 2016, 2018
14  *
15  *  Author(s):  Ursula Braun <ubraun@linux.vnet.ibm.com>
16  *              based on prototype from Frank Blaschka
17  */
18 
19 #define KMSG_COMPONENT "smc"
20 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
21 
22 #include <linux/module.h>
23 #include <linux/socket.h>
24 #include <linux/workqueue.h>
25 #include <linux/in.h>
26 #include <linux/sched/signal.h>
27 #include <linux/if_vlan.h>
28 #include <linux/rcupdate_wait.h>
29 #include <linux/ctype.h>
30 #include <linux/splice.h>
31 
32 #include <net/sock.h>
33 #include <net/tcp.h>
34 #include <net/smc.h>
35 #include <asm/ioctls.h>
36 
37 #include <net/net_namespace.h>
38 #include <net/netns/generic.h>
39 #include "smc_netns.h"
40 
41 #include "smc.h"
42 #include "smc_clc.h"
43 #include "smc_llc.h"
44 #include "smc_cdc.h"
45 #include "smc_core.h"
46 #include "smc_ib.h"
47 #include "smc_ism.h"
48 #include "smc_pnet.h"
49 #include "smc_netlink.h"
50 #include "smc_tx.h"
51 #include "smc_rx.h"
52 #include "smc_close.h"
53 #include "smc_stats.h"
54 #include "smc_tracepoint.h"
55 #include "smc_sysctl.h"
56 
57 static DEFINE_MUTEX(smc_server_lgr_pending);	/* serialize link group
58 						 * creation on server
59 						 */
60 static DEFINE_MUTEX(smc_client_lgr_pending);	/* serialize link group
61 						 * creation on client
62 						 */
63 
64 static struct workqueue_struct	*smc_tcp_ls_wq;	/* wq for tcp listen work */
65 struct workqueue_struct	*smc_hs_wq;	/* wq for handshake work */
66 struct workqueue_struct	*smc_close_wq;	/* wq for close work */
67 
68 static void smc_tcp_listen_work(struct work_struct *);
69 static void smc_connect_work(struct work_struct *);
70 
smc_nl_dump_hs_limitation(struct sk_buff * skb,struct netlink_callback * cb)71 int smc_nl_dump_hs_limitation(struct sk_buff *skb, struct netlink_callback *cb)
72 {
73 	struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb);
74 	void *hdr;
75 
76 	if (cb_ctx->pos[0])
77 		goto out;
78 
79 	hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
80 			  &smc_gen_nl_family, NLM_F_MULTI,
81 			  SMC_NETLINK_DUMP_HS_LIMITATION);
82 	if (!hdr)
83 		return -ENOMEM;
84 
85 	if (nla_put_u8(skb, SMC_NLA_HS_LIMITATION_ENABLED,
86 		       sock_net(skb->sk)->smc.limit_smc_hs))
87 		goto err;
88 
89 	genlmsg_end(skb, hdr);
90 	cb_ctx->pos[0] = 1;
91 out:
92 	return skb->len;
93 err:
94 	genlmsg_cancel(skb, hdr);
95 	return -EMSGSIZE;
96 }
97 
smc_nl_enable_hs_limitation(struct sk_buff * skb,struct genl_info * info)98 int smc_nl_enable_hs_limitation(struct sk_buff *skb, struct genl_info *info)
99 {
100 	sock_net(skb->sk)->smc.limit_smc_hs = true;
101 	return 0;
102 }
103 
smc_nl_disable_hs_limitation(struct sk_buff * skb,struct genl_info * info)104 int smc_nl_disable_hs_limitation(struct sk_buff *skb, struct genl_info *info)
105 {
106 	sock_net(skb->sk)->smc.limit_smc_hs = false;
107 	return 0;
108 }
109 
smc_set_keepalive(struct sock * sk,int val)110 static void smc_set_keepalive(struct sock *sk, int val)
111 {
112 	struct smc_sock *smc = smc_sk(sk);
113 
114 	smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
115 }
116 
smc_tcp_syn_recv_sock(const struct sock * sk,struct sk_buff * skb,struct request_sock * req,struct dst_entry * dst,struct request_sock * req_unhash,bool * own_req)117 static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk,
118 					  struct sk_buff *skb,
119 					  struct request_sock *req,
120 					  struct dst_entry *dst,
121 					  struct request_sock *req_unhash,
122 					  bool *own_req)
123 {
124 	struct smc_sock *smc;
125 	struct sock *child;
126 
127 	smc = smc_clcsock_user_data(sk);
128 
129 	if (READ_ONCE(sk->sk_ack_backlog) + atomic_read(&smc->queued_smc_hs) >
130 				sk->sk_max_ack_backlog)
131 		goto drop;
132 
133 	if (sk_acceptq_is_full(&smc->sk)) {
134 		NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
135 		goto drop;
136 	}
137 
138 	/* passthrough to original syn recv sock fct */
139 	child = smc->ori_af_ops->syn_recv_sock(sk, skb, req, dst, req_unhash,
140 					       own_req);
141 	/* child must not inherit smc or its ops */
142 	if (child) {
143 		rcu_assign_sk_user_data(child, NULL);
144 
145 		/* v4-mapped sockets don't inherit parent ops. Don't restore. */
146 		if (inet_csk(child)->icsk_af_ops == inet_csk(sk)->icsk_af_ops)
147 			inet_csk(child)->icsk_af_ops = smc->ori_af_ops;
148 	}
149 	return child;
150 
151 drop:
152 	dst_release(dst);
153 	tcp_listendrop(sk);
154 	return NULL;
155 }
156 
smc_hs_congested(const struct sock * sk)157 static bool smc_hs_congested(const struct sock *sk)
158 {
159 	const struct smc_sock *smc;
160 
161 	smc = smc_clcsock_user_data(sk);
162 
163 	if (!smc)
164 		return true;
165 
166 	if (workqueue_congested(WORK_CPU_UNBOUND, smc_hs_wq))
167 		return true;
168 
169 	return false;
170 }
171 
172 static struct smc_hashinfo smc_v4_hashinfo = {
173 	.lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
174 };
175 
176 static struct smc_hashinfo smc_v6_hashinfo = {
177 	.lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
178 };
179 
smc_hash_sk(struct sock * sk)180 int smc_hash_sk(struct sock *sk)
181 {
182 	struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
183 	struct hlist_head *head;
184 
185 	head = &h->ht;
186 
187 	write_lock_bh(&h->lock);
188 	sk_add_node(sk, head);
189 	write_unlock_bh(&h->lock);
190 	sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
191 
192 	return 0;
193 }
194 EXPORT_SYMBOL_GPL(smc_hash_sk);
195 
smc_unhash_sk(struct sock * sk)196 void smc_unhash_sk(struct sock *sk)
197 {
198 	struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
199 
200 	write_lock_bh(&h->lock);
201 	if (sk_del_node_init(sk))
202 		sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
203 	write_unlock_bh(&h->lock);
204 }
205 EXPORT_SYMBOL_GPL(smc_unhash_sk);
206 
207 /* This will be called before user really release sock_lock. So do the
208  * work which we didn't do because of user hold the sock_lock in the
209  * BH context
210  */
smc_release_cb(struct sock * sk)211 static void smc_release_cb(struct sock *sk)
212 {
213 	struct smc_sock *smc = smc_sk(sk);
214 
215 	if (smc->conn.tx_in_release_sock) {
216 		smc_tx_pending(&smc->conn);
217 		smc->conn.tx_in_release_sock = false;
218 	}
219 }
220 
221 struct proto smc_proto = {
222 	.name		= "SMC",
223 	.owner		= THIS_MODULE,
224 	.keepalive	= smc_set_keepalive,
225 	.hash		= smc_hash_sk,
226 	.unhash		= smc_unhash_sk,
227 	.release_cb	= smc_release_cb,
228 	.obj_size	= sizeof(struct smc_sock),
229 	.h.smc_hash	= &smc_v4_hashinfo,
230 	.slab_flags	= SLAB_TYPESAFE_BY_RCU,
231 };
232 EXPORT_SYMBOL_GPL(smc_proto);
233 
234 struct proto smc_proto6 = {
235 	.name		= "SMC6",
236 	.owner		= THIS_MODULE,
237 	.keepalive	= smc_set_keepalive,
238 	.hash		= smc_hash_sk,
239 	.unhash		= smc_unhash_sk,
240 	.release_cb	= smc_release_cb,
241 	.obj_size	= sizeof(struct smc_sock),
242 	.h.smc_hash	= &smc_v6_hashinfo,
243 	.slab_flags	= SLAB_TYPESAFE_BY_RCU,
244 };
245 EXPORT_SYMBOL_GPL(smc_proto6);
246 
smc_fback_restore_callbacks(struct smc_sock * smc)247 static void smc_fback_restore_callbacks(struct smc_sock *smc)
248 {
249 	struct sock *clcsk = smc->clcsock->sk;
250 
251 	write_lock_bh(&clcsk->sk_callback_lock);
252 	clcsk->sk_user_data = NULL;
253 
254 	smc_clcsock_restore_cb(&clcsk->sk_state_change, &smc->clcsk_state_change);
255 	smc_clcsock_restore_cb(&clcsk->sk_data_ready, &smc->clcsk_data_ready);
256 	smc_clcsock_restore_cb(&clcsk->sk_write_space, &smc->clcsk_write_space);
257 	smc_clcsock_restore_cb(&clcsk->sk_error_report, &smc->clcsk_error_report);
258 
259 	write_unlock_bh(&clcsk->sk_callback_lock);
260 }
261 
smc_restore_fallback_changes(struct smc_sock * smc)262 static void smc_restore_fallback_changes(struct smc_sock *smc)
263 {
264 	if (smc->clcsock->file) { /* non-accepted sockets have no file yet */
265 		smc->clcsock->file->private_data = smc->sk.sk_socket;
266 		smc->clcsock->file = NULL;
267 		smc_fback_restore_callbacks(smc);
268 	}
269 }
270 
__smc_release(struct smc_sock * smc)271 static int __smc_release(struct smc_sock *smc)
272 {
273 	struct sock *sk = &smc->sk;
274 	int rc = 0;
275 
276 	if (!smc->use_fallback) {
277 		rc = smc_close_active(smc);
278 		smc_sock_set_flag(sk, SOCK_DEAD);
279 		sk->sk_shutdown |= SHUTDOWN_MASK;
280 	} else {
281 		if (sk->sk_state != SMC_CLOSED) {
282 			if (sk->sk_state != SMC_LISTEN &&
283 			    sk->sk_state != SMC_INIT)
284 				sock_put(sk); /* passive closing */
285 			if (sk->sk_state == SMC_LISTEN) {
286 				/* wake up clcsock accept */
287 				rc = kernel_sock_shutdown(smc->clcsock,
288 							  SHUT_RDWR);
289 			}
290 			sk->sk_state = SMC_CLOSED;
291 			sk->sk_state_change(sk);
292 		}
293 		smc_restore_fallback_changes(smc);
294 	}
295 
296 	sk->sk_prot->unhash(sk);
297 
298 	if (sk->sk_state == SMC_CLOSED) {
299 		if (smc->clcsock) {
300 			release_sock(sk);
301 			smc_clcsock_release(smc);
302 			lock_sock(sk);
303 		}
304 		if (!smc->use_fallback)
305 			smc_conn_free(&smc->conn);
306 	}
307 
308 	return rc;
309 }
310 
smc_release(struct socket * sock)311 static int smc_release(struct socket *sock)
312 {
313 	struct sock *sk = sock->sk;
314 	struct smc_sock *smc;
315 	int old_state, rc = 0;
316 
317 	if (!sk)
318 		goto out;
319 
320 	sock_hold(sk); /* sock_put below */
321 	smc = smc_sk(sk);
322 
323 	old_state = sk->sk_state;
324 
325 	/* cleanup for a dangling non-blocking connect */
326 	if (smc->connect_nonblock && old_state == SMC_INIT)
327 		tcp_abort(smc->clcsock->sk, ECONNABORTED);
328 
329 	if (cancel_work_sync(&smc->connect_work))
330 		sock_put(&smc->sk); /* sock_hold in smc_connect for passive closing */
331 
332 	if (sk->sk_state == SMC_LISTEN)
333 		/* smc_close_non_accepted() is called and acquires
334 		 * sock lock for child sockets again
335 		 */
336 		lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
337 	else
338 		lock_sock(sk);
339 
340 	if (old_state == SMC_INIT && sk->sk_state == SMC_ACTIVE &&
341 	    !smc->use_fallback)
342 		smc_close_active_abort(smc);
343 
344 	rc = __smc_release(smc);
345 
346 	/* detach socket */
347 	sock_orphan(sk);
348 	sock->sk = NULL;
349 	release_sock(sk);
350 
351 	sock_put(sk); /* sock_hold above */
352 	sock_put(sk); /* final sock_put */
353 out:
354 	return rc;
355 }
356 
smc_destruct(struct sock * sk)357 static void smc_destruct(struct sock *sk)
358 {
359 	if (sk->sk_state != SMC_CLOSED)
360 		return;
361 	if (!sock_flag(sk, SOCK_DEAD))
362 		return;
363 }
364 
smc_sk_init(struct net * net,struct sock * sk,int protocol)365 void smc_sk_init(struct net *net, struct sock *sk, int protocol)
366 {
367 	struct smc_sock *smc = smc_sk(sk);
368 
369 	sk->sk_state = SMC_INIT;
370 	sk->sk_destruct = smc_destruct;
371 	sk->sk_protocol = protocol;
372 	WRITE_ONCE(sk->sk_sndbuf, 2 * READ_ONCE(net->smc.sysctl_wmem));
373 	WRITE_ONCE(sk->sk_rcvbuf, 2 * READ_ONCE(net->smc.sysctl_rmem));
374 	INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
375 	INIT_WORK(&smc->connect_work, smc_connect_work);
376 	INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
377 	INIT_LIST_HEAD(&smc->accept_q);
378 	spin_lock_init(&smc->accept_q_lock);
379 	spin_lock_init(&smc->conn.send_lock);
380 	sk->sk_prot->hash(sk);
381 	mutex_init(&smc->clcsock_release_lock);
382 	smc_init_saved_callbacks(smc);
383 	smc->limit_smc_hs = net->smc.limit_smc_hs;
384 	smc->use_fallback = false; /* assume rdma capability first */
385 	smc->fallback_rsn = 0;
386 	smc_close_init(smc);
387 }
388 
smc_sock_alloc(struct net * net,struct socket * sock,int protocol)389 static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
390 				   int protocol)
391 {
392 	struct proto *prot;
393 	struct sock *sk;
394 
395 	prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
396 	sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
397 	if (!sk)
398 		return NULL;
399 
400 	sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
401 	smc_sk_init(net, sk, protocol);
402 
403 	return sk;
404 }
405 
smc_bind(struct socket * sock,struct sockaddr * uaddr,int addr_len)406 static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
407 		    int addr_len)
408 {
409 	struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
410 	struct sock *sk = sock->sk;
411 	struct smc_sock *smc;
412 	int rc;
413 
414 	smc = smc_sk(sk);
415 
416 	/* replicate tests from inet_bind(), to be safe wrt. future changes */
417 	rc = -EINVAL;
418 	if (addr_len < sizeof(struct sockaddr_in))
419 		goto out;
420 
421 	rc = -EAFNOSUPPORT;
422 	if (addr->sin_family != AF_INET &&
423 	    addr->sin_family != AF_INET6 &&
424 	    addr->sin_family != AF_UNSPEC)
425 		goto out;
426 	/* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
427 	if (addr->sin_family == AF_UNSPEC &&
428 	    addr->sin_addr.s_addr != htonl(INADDR_ANY))
429 		goto out;
430 
431 	lock_sock(sk);
432 
433 	/* Check if socket is already active */
434 	rc = -EINVAL;
435 	if (sk->sk_state != SMC_INIT || smc->connect_nonblock)
436 		goto out_rel;
437 
438 	smc->clcsock->sk->sk_reuse = sk->sk_reuse;
439 	smc->clcsock->sk->sk_reuseport = sk->sk_reuseport;
440 	rc = kernel_bind(smc->clcsock, uaddr, addr_len);
441 
442 out_rel:
443 	release_sock(sk);
444 out:
445 	return rc;
446 }
447 
448 /* copy only relevant settings and flags of SOL_SOCKET level from smc to
449  * clc socket (since smc is not called for these options from net/core)
450  */
451 
452 #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
453 			     (1UL << SOCK_KEEPOPEN) | \
454 			     (1UL << SOCK_LINGER) | \
455 			     (1UL << SOCK_BROADCAST) | \
456 			     (1UL << SOCK_TIMESTAMP) | \
457 			     (1UL << SOCK_DBG) | \
458 			     (1UL << SOCK_RCVTSTAMP) | \
459 			     (1UL << SOCK_RCVTSTAMPNS) | \
460 			     (1UL << SOCK_LOCALROUTE) | \
461 			     (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
462 			     (1UL << SOCK_RXQ_OVFL) | \
463 			     (1UL << SOCK_WIFI_STATUS) | \
464 			     (1UL << SOCK_NOFCS) | \
465 			     (1UL << SOCK_FILTER_LOCKED) | \
466 			     (1UL << SOCK_TSTAMP_NEW))
467 
468 /* if set, use value set by setsockopt() - else use IPv4 or SMC sysctl value */
smc_adjust_sock_bufsizes(struct sock * nsk,struct sock * osk,unsigned long mask)469 static void smc_adjust_sock_bufsizes(struct sock *nsk, struct sock *osk,
470 				     unsigned long mask)
471 {
472 	nsk->sk_userlocks = osk->sk_userlocks;
473 	if (osk->sk_userlocks & SOCK_SNDBUF_LOCK)
474 		nsk->sk_sndbuf = osk->sk_sndbuf;
475 	if (osk->sk_userlocks & SOCK_RCVBUF_LOCK)
476 		nsk->sk_rcvbuf = osk->sk_rcvbuf;
477 }
478 
smc_copy_sock_settings(struct sock * nsk,struct sock * osk,unsigned long mask)479 static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
480 				   unsigned long mask)
481 {
482 	/* options we don't get control via setsockopt for */
483 	nsk->sk_type = osk->sk_type;
484 	nsk->sk_sndtimeo = osk->sk_sndtimeo;
485 	nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
486 	nsk->sk_mark = READ_ONCE(osk->sk_mark);
487 	nsk->sk_priority = osk->sk_priority;
488 	nsk->sk_rcvlowat = osk->sk_rcvlowat;
489 	nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
490 	nsk->sk_err = osk->sk_err;
491 
492 	nsk->sk_flags &= ~mask;
493 	nsk->sk_flags |= osk->sk_flags & mask;
494 
495 	smc_adjust_sock_bufsizes(nsk, osk, mask);
496 }
497 
smc_copy_sock_settings_to_clc(struct smc_sock * smc)498 static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
499 {
500 	smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
501 }
502 
503 #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
504 			     (1UL << SOCK_KEEPOPEN) | \
505 			     (1UL << SOCK_LINGER) | \
506 			     (1UL << SOCK_DBG))
507 /* copy only settings and flags relevant for smc from clc to smc socket */
smc_copy_sock_settings_to_smc(struct smc_sock * smc)508 static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
509 {
510 	smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
511 }
512 
513 /* register the new vzalloced sndbuf on all links */
smcr_lgr_reg_sndbufs(struct smc_link * link,struct smc_buf_desc * snd_desc)514 static int smcr_lgr_reg_sndbufs(struct smc_link *link,
515 				struct smc_buf_desc *snd_desc)
516 {
517 	struct smc_link_group *lgr = link->lgr;
518 	int i, rc = 0;
519 
520 	if (!snd_desc->is_vm)
521 		return -EINVAL;
522 
523 	/* protect against parallel smcr_link_reg_buf() */
524 	down_write(&lgr->llc_conf_mutex);
525 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
526 		if (!smc_link_active(&lgr->lnk[i]))
527 			continue;
528 		rc = smcr_link_reg_buf(&lgr->lnk[i], snd_desc);
529 		if (rc)
530 			break;
531 	}
532 	up_write(&lgr->llc_conf_mutex);
533 	return rc;
534 }
535 
536 /* register the new rmb on all links */
smcr_lgr_reg_rmbs(struct smc_link * link,struct smc_buf_desc * rmb_desc)537 static int smcr_lgr_reg_rmbs(struct smc_link *link,
538 			     struct smc_buf_desc *rmb_desc)
539 {
540 	struct smc_link_group *lgr = link->lgr;
541 	bool do_slow = false;
542 	int i, rc = 0;
543 
544 	rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY);
545 	if (rc)
546 		return rc;
547 
548 	down_read(&lgr->llc_conf_mutex);
549 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
550 		if (!smc_link_active(&lgr->lnk[i]))
551 			continue;
552 		if (!rmb_desc->is_reg_mr[link->link_idx]) {
553 			up_read(&lgr->llc_conf_mutex);
554 			goto slow_path;
555 		}
556 	}
557 	/* mr register already */
558 	goto fast_path;
559 slow_path:
560 	do_slow = true;
561 	/* protect against parallel smc_llc_cli_rkey_exchange() and
562 	 * parallel smcr_link_reg_buf()
563 	 */
564 	down_write(&lgr->llc_conf_mutex);
565 	for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
566 		if (!smc_link_active(&lgr->lnk[i]))
567 			continue;
568 		rc = smcr_link_reg_buf(&lgr->lnk[i], rmb_desc);
569 		if (rc)
570 			goto out;
571 	}
572 fast_path:
573 	/* exchange confirm_rkey msg with peer */
574 	rc = smc_llc_do_confirm_rkey(link, rmb_desc);
575 	if (rc) {
576 		rc = -EFAULT;
577 		goto out;
578 	}
579 	rmb_desc->is_conf_rkey = true;
580 out:
581 	do_slow ? up_write(&lgr->llc_conf_mutex) : up_read(&lgr->llc_conf_mutex);
582 	smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
583 	return rc;
584 }
585 
smcr_clnt_conf_first_link(struct smc_sock * smc)586 static int smcr_clnt_conf_first_link(struct smc_sock *smc)
587 {
588 	struct smc_link *link = smc->conn.lnk;
589 	struct smc_llc_qentry *qentry;
590 	int rc;
591 
592 	/* Receive CONFIRM LINK request from server over RoCE fabric.
593 	 * Increasing the client's timeout by twice as much as the server's
594 	 * timeout by default can temporarily avoid decline messages of
595 	 * both sides crossing or colliding
596 	 */
597 	qentry = smc_llc_wait(link->lgr, NULL, 2 * SMC_LLC_WAIT_TIME,
598 			      SMC_LLC_CONFIRM_LINK);
599 	if (!qentry) {
600 		struct smc_clc_msg_decline dclc;
601 
602 		rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
603 				      SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
604 		return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
605 	}
606 	smc_llc_save_peer_uid(qentry);
607 	rc = smc_llc_eval_conf_link(qentry, SMC_LLC_REQ);
608 	smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
609 	if (rc)
610 		return SMC_CLC_DECL_RMBE_EC;
611 
612 	rc = smc_ib_modify_qp_rts(link);
613 	if (rc)
614 		return SMC_CLC_DECL_ERR_RDYLNK;
615 
616 	smc_wr_remember_qp_attr(link);
617 
618 	/* reg the sndbuf if it was vzalloced */
619 	if (smc->conn.sndbuf_desc->is_vm) {
620 		if (smcr_link_reg_buf(link, smc->conn.sndbuf_desc))
621 			return SMC_CLC_DECL_ERR_REGBUF;
622 	}
623 
624 	/* reg the rmb */
625 	if (smcr_link_reg_buf(link, smc->conn.rmb_desc))
626 		return SMC_CLC_DECL_ERR_REGBUF;
627 
628 	/* confirm_rkey is implicit on 1st contact */
629 	smc->conn.rmb_desc->is_conf_rkey = true;
630 
631 	/* send CONFIRM LINK response over RoCE fabric */
632 	rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
633 	if (rc < 0)
634 		return SMC_CLC_DECL_TIMEOUT_CL;
635 
636 	smc_llc_link_active(link);
637 	smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
638 
639 	if (link->lgr->max_links > 1) {
640 		/* optional 2nd link, receive ADD LINK request from server */
641 		qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
642 				      SMC_LLC_ADD_LINK);
643 		if (!qentry) {
644 			struct smc_clc_msg_decline dclc;
645 
646 			rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
647 					      SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
648 			if (rc == -EAGAIN)
649 				rc = 0; /* no DECLINE received, go with one link */
650 			return rc;
651 		}
652 		smc_llc_flow_qentry_clr(&link->lgr->llc_flow_lcl);
653 		smc_llc_cli_add_link(link, qentry);
654 	}
655 	return 0;
656 }
657 
smc_isascii(char * hostname)658 static bool smc_isascii(char *hostname)
659 {
660 	int i;
661 
662 	for (i = 0; i < SMC_MAX_HOSTNAME_LEN; i++)
663 		if (!isascii(hostname[i]))
664 			return false;
665 	return true;
666 }
667 
smc_conn_save_peer_info_fce(struct smc_sock * smc,struct smc_clc_msg_accept_confirm * clc)668 static void smc_conn_save_peer_info_fce(struct smc_sock *smc,
669 					struct smc_clc_msg_accept_confirm *clc)
670 {
671 	struct smc_clc_first_contact_ext *fce;
672 	int clc_v2_len;
673 
674 	if (clc->hdr.version == SMC_V1 ||
675 	    !(clc->hdr.typev2 & SMC_FIRST_CONTACT_MASK))
676 		return;
677 
678 	if (smc->conn.lgr->is_smcd) {
679 		memcpy(smc->conn.lgr->negotiated_eid, clc->d1.eid,
680 		       SMC_MAX_EID_LEN);
681 		clc_v2_len = offsetofend(struct smc_clc_msg_accept_confirm, d1);
682 	} else {
683 		memcpy(smc->conn.lgr->negotiated_eid, clc->r1.eid,
684 		       SMC_MAX_EID_LEN);
685 		clc_v2_len = offsetofend(struct smc_clc_msg_accept_confirm, r1);
686 	}
687 	fce = (struct smc_clc_first_contact_ext *)(((u8 *)clc) + clc_v2_len);
688 	smc->conn.lgr->peer_os = fce->os_type;
689 	smc->conn.lgr->peer_smc_release = fce->release;
690 	if (smc_isascii(fce->hostname))
691 		memcpy(smc->conn.lgr->peer_hostname, fce->hostname,
692 		       SMC_MAX_HOSTNAME_LEN);
693 }
694 
smcr_conn_save_peer_info(struct smc_sock * smc,struct smc_clc_msg_accept_confirm * clc)695 static void smcr_conn_save_peer_info(struct smc_sock *smc,
696 				     struct smc_clc_msg_accept_confirm *clc)
697 {
698 	int bufsize = smc_uncompress_bufsize(clc->r0.rmbe_size);
699 
700 	smc->conn.peer_rmbe_idx = clc->r0.rmbe_idx;
701 	smc->conn.local_tx_ctrl.token = ntohl(clc->r0.rmbe_alert_token);
702 	smc->conn.peer_rmbe_size = bufsize;
703 	atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
704 	smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
705 }
706 
smcd_conn_save_peer_info(struct smc_sock * smc,struct smc_clc_msg_accept_confirm * clc)707 static void smcd_conn_save_peer_info(struct smc_sock *smc,
708 				     struct smc_clc_msg_accept_confirm *clc)
709 {
710 	int bufsize = smc_uncompress_bufsize(clc->d0.dmbe_size);
711 
712 	smc->conn.peer_rmbe_idx = clc->d0.dmbe_idx;
713 	smc->conn.peer_token = ntohll(clc->d0.token);
714 	/* msg header takes up space in the buffer */
715 	smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
716 	atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
717 	smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
718 }
719 
smc_conn_save_peer_info(struct smc_sock * smc,struct smc_clc_msg_accept_confirm * clc)720 static void smc_conn_save_peer_info(struct smc_sock *smc,
721 				    struct smc_clc_msg_accept_confirm *clc)
722 {
723 	if (smc->conn.lgr->is_smcd)
724 		smcd_conn_save_peer_info(smc, clc);
725 	else
726 		smcr_conn_save_peer_info(smc, clc);
727 	smc_conn_save_peer_info_fce(smc, clc);
728 }
729 
smc_link_save_peer_info(struct smc_link * link,struct smc_clc_msg_accept_confirm * clc,struct smc_init_info * ini)730 static void smc_link_save_peer_info(struct smc_link *link,
731 				    struct smc_clc_msg_accept_confirm *clc,
732 				    struct smc_init_info *ini)
733 {
734 	link->peer_qpn = ntoh24(clc->r0.qpn);
735 	memcpy(link->peer_gid, ini->peer_gid, SMC_GID_SIZE);
736 	memcpy(link->peer_mac, ini->peer_mac, sizeof(link->peer_mac));
737 	link->peer_psn = ntoh24(clc->r0.psn);
738 	link->peer_mtu = clc->r0.qp_mtu;
739 }
740 
smc_stat_inc_fback_rsn_cnt(struct smc_sock * smc,struct smc_stats_fback * fback_arr)741 static void smc_stat_inc_fback_rsn_cnt(struct smc_sock *smc,
742 				       struct smc_stats_fback *fback_arr)
743 {
744 	int cnt;
745 
746 	for (cnt = 0; cnt < SMC_MAX_FBACK_RSN_CNT; cnt++) {
747 		if (fback_arr[cnt].fback_code == smc->fallback_rsn) {
748 			fback_arr[cnt].count++;
749 			break;
750 		}
751 		if (!fback_arr[cnt].fback_code) {
752 			fback_arr[cnt].fback_code = smc->fallback_rsn;
753 			fback_arr[cnt].count++;
754 			break;
755 		}
756 	}
757 }
758 
smc_stat_fallback(struct smc_sock * smc)759 static void smc_stat_fallback(struct smc_sock *smc)
760 {
761 	struct net *net = sock_net(&smc->sk);
762 
763 	mutex_lock(&net->smc.mutex_fback_rsn);
764 	if (smc->listen_smc) {
765 		smc_stat_inc_fback_rsn_cnt(smc, net->smc.fback_rsn->srv);
766 		net->smc.fback_rsn->srv_fback_cnt++;
767 	} else {
768 		smc_stat_inc_fback_rsn_cnt(smc, net->smc.fback_rsn->clnt);
769 		net->smc.fback_rsn->clnt_fback_cnt++;
770 	}
771 	mutex_unlock(&net->smc.mutex_fback_rsn);
772 }
773 
774 /* must be called under rcu read lock */
smc_fback_wakeup_waitqueue(struct smc_sock * smc,void * key)775 static void smc_fback_wakeup_waitqueue(struct smc_sock *smc, void *key)
776 {
777 	struct socket_wq *wq;
778 	__poll_t flags;
779 
780 	wq = rcu_dereference(smc->sk.sk_wq);
781 	if (!skwq_has_sleeper(wq))
782 		return;
783 
784 	/* wake up smc sk->sk_wq */
785 	if (!key) {
786 		/* sk_state_change */
787 		wake_up_interruptible_all(&wq->wait);
788 	} else {
789 		flags = key_to_poll(key);
790 		if (flags & (EPOLLIN | EPOLLOUT))
791 			/* sk_data_ready or sk_write_space */
792 			wake_up_interruptible_sync_poll(&wq->wait, flags);
793 		else if (flags & EPOLLERR)
794 			/* sk_error_report */
795 			wake_up_interruptible_poll(&wq->wait, flags);
796 	}
797 }
798 
smc_fback_mark_woken(wait_queue_entry_t * wait,unsigned int mode,int sync,void * key)799 static int smc_fback_mark_woken(wait_queue_entry_t *wait,
800 				unsigned int mode, int sync, void *key)
801 {
802 	struct smc_mark_woken *mark =
803 		container_of(wait, struct smc_mark_woken, wait_entry);
804 
805 	mark->woken = true;
806 	mark->key = key;
807 	return 0;
808 }
809 
smc_fback_forward_wakeup(struct smc_sock * smc,struct sock * clcsk,void (* clcsock_callback)(struct sock * sk))810 static void smc_fback_forward_wakeup(struct smc_sock *smc, struct sock *clcsk,
811 				     void (*clcsock_callback)(struct sock *sk))
812 {
813 	struct smc_mark_woken mark = { .woken = false };
814 	struct socket_wq *wq;
815 
816 	init_waitqueue_func_entry(&mark.wait_entry,
817 				  smc_fback_mark_woken);
818 	rcu_read_lock();
819 	wq = rcu_dereference(clcsk->sk_wq);
820 	if (!wq)
821 		goto out;
822 	add_wait_queue(sk_sleep(clcsk), &mark.wait_entry);
823 	clcsock_callback(clcsk);
824 	remove_wait_queue(sk_sleep(clcsk), &mark.wait_entry);
825 
826 	if (mark.woken)
827 		smc_fback_wakeup_waitqueue(smc, mark.key);
828 out:
829 	rcu_read_unlock();
830 }
831 
smc_fback_state_change(struct sock * clcsk)832 static void smc_fback_state_change(struct sock *clcsk)
833 {
834 	struct smc_sock *smc;
835 
836 	read_lock_bh(&clcsk->sk_callback_lock);
837 	smc = smc_clcsock_user_data(clcsk);
838 	if (smc)
839 		smc_fback_forward_wakeup(smc, clcsk,
840 					 smc->clcsk_state_change);
841 	read_unlock_bh(&clcsk->sk_callback_lock);
842 }
843 
smc_fback_data_ready(struct sock * clcsk)844 static void smc_fback_data_ready(struct sock *clcsk)
845 {
846 	struct smc_sock *smc;
847 
848 	read_lock_bh(&clcsk->sk_callback_lock);
849 	smc = smc_clcsock_user_data(clcsk);
850 	if (smc)
851 		smc_fback_forward_wakeup(smc, clcsk,
852 					 smc->clcsk_data_ready);
853 	read_unlock_bh(&clcsk->sk_callback_lock);
854 }
855 
smc_fback_write_space(struct sock * clcsk)856 static void smc_fback_write_space(struct sock *clcsk)
857 {
858 	struct smc_sock *smc;
859 
860 	read_lock_bh(&clcsk->sk_callback_lock);
861 	smc = smc_clcsock_user_data(clcsk);
862 	if (smc)
863 		smc_fback_forward_wakeup(smc, clcsk,
864 					 smc->clcsk_write_space);
865 	read_unlock_bh(&clcsk->sk_callback_lock);
866 }
867 
smc_fback_error_report(struct sock * clcsk)868 static void smc_fback_error_report(struct sock *clcsk)
869 {
870 	struct smc_sock *smc;
871 
872 	read_lock_bh(&clcsk->sk_callback_lock);
873 	smc = smc_clcsock_user_data(clcsk);
874 	if (smc)
875 		smc_fback_forward_wakeup(smc, clcsk,
876 					 smc->clcsk_error_report);
877 	read_unlock_bh(&clcsk->sk_callback_lock);
878 }
879 
smc_fback_replace_callbacks(struct smc_sock * smc)880 static void smc_fback_replace_callbacks(struct smc_sock *smc)
881 {
882 	struct sock *clcsk = smc->clcsock->sk;
883 
884 	write_lock_bh(&clcsk->sk_callback_lock);
885 	clcsk->sk_user_data = (void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY);
886 
887 	smc_clcsock_replace_cb(&clcsk->sk_state_change, smc_fback_state_change,
888 			       &smc->clcsk_state_change);
889 	smc_clcsock_replace_cb(&clcsk->sk_data_ready, smc_fback_data_ready,
890 			       &smc->clcsk_data_ready);
891 	smc_clcsock_replace_cb(&clcsk->sk_write_space, smc_fback_write_space,
892 			       &smc->clcsk_write_space);
893 	smc_clcsock_replace_cb(&clcsk->sk_error_report, smc_fback_error_report,
894 			       &smc->clcsk_error_report);
895 
896 	write_unlock_bh(&clcsk->sk_callback_lock);
897 }
898 
smc_switch_to_fallback(struct smc_sock * smc,int reason_code)899 static int smc_switch_to_fallback(struct smc_sock *smc, int reason_code)
900 {
901 	int rc = 0;
902 
903 	mutex_lock(&smc->clcsock_release_lock);
904 	if (!smc->clcsock) {
905 		rc = -EBADF;
906 		goto out;
907 	}
908 
909 	smc->use_fallback = true;
910 	smc->fallback_rsn = reason_code;
911 	smc_stat_fallback(smc);
912 	trace_smc_switch_to_fallback(smc, reason_code);
913 	if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
914 		smc->clcsock->file = smc->sk.sk_socket->file;
915 		smc->clcsock->file->private_data = smc->clcsock;
916 		smc->clcsock->wq.fasync_list =
917 			smc->sk.sk_socket->wq.fasync_list;
918 
919 		/* There might be some wait entries remaining
920 		 * in smc sk->sk_wq and they should be woken up
921 		 * as clcsock's wait queue is woken up.
922 		 */
923 		smc_fback_replace_callbacks(smc);
924 	}
925 out:
926 	mutex_unlock(&smc->clcsock_release_lock);
927 	return rc;
928 }
929 
930 /* fall back during connect */
smc_connect_fallback(struct smc_sock * smc,int reason_code)931 static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
932 {
933 	struct net *net = sock_net(&smc->sk);
934 	int rc = 0;
935 
936 	rc = smc_switch_to_fallback(smc, reason_code);
937 	if (rc) { /* fallback fails */
938 		this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
939 		if (smc->sk.sk_state == SMC_INIT)
940 			sock_put(&smc->sk); /* passive closing */
941 		return rc;
942 	}
943 	smc_copy_sock_settings_to_clc(smc);
944 	smc->connect_nonblock = 0;
945 	if (smc->sk.sk_state == SMC_INIT)
946 		smc->sk.sk_state = SMC_ACTIVE;
947 	return 0;
948 }
949 
950 /* decline and fall back during connect */
smc_connect_decline_fallback(struct smc_sock * smc,int reason_code,u8 version)951 static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code,
952 					u8 version)
953 {
954 	struct net *net = sock_net(&smc->sk);
955 	int rc;
956 
957 	if (reason_code < 0) { /* error, fallback is not possible */
958 		this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
959 		if (smc->sk.sk_state == SMC_INIT)
960 			sock_put(&smc->sk); /* passive closing */
961 		return reason_code;
962 	}
963 	if (reason_code != SMC_CLC_DECL_PEERDECL) {
964 		rc = smc_clc_send_decline(smc, reason_code, version);
965 		if (rc < 0) {
966 			this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
967 			if (smc->sk.sk_state == SMC_INIT)
968 				sock_put(&smc->sk); /* passive closing */
969 			return rc;
970 		}
971 	}
972 	return smc_connect_fallback(smc, reason_code);
973 }
974 
smc_conn_abort(struct smc_sock * smc,int local_first)975 static void smc_conn_abort(struct smc_sock *smc, int local_first)
976 {
977 	struct smc_connection *conn = &smc->conn;
978 	struct smc_link_group *lgr = conn->lgr;
979 	bool lgr_valid = false;
980 
981 	if (smc_conn_lgr_valid(conn))
982 		lgr_valid = true;
983 
984 	smc_conn_free(conn);
985 	if (local_first && lgr_valid)
986 		smc_lgr_cleanup_early(lgr);
987 }
988 
989 /* check if there is a rdma device available for this connection. */
990 /* called for connect and listen */
smc_find_rdma_device(struct smc_sock * smc,struct smc_init_info * ini)991 static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
992 {
993 	/* PNET table look up: search active ib_device and port
994 	 * within same PNETID that also contains the ethernet device
995 	 * used for the internal TCP socket
996 	 */
997 	smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
998 	if (!ini->check_smcrv2 && !ini->ib_dev)
999 		return SMC_CLC_DECL_NOSMCRDEV;
1000 	if (ini->check_smcrv2 && !ini->smcrv2.ib_dev_v2)
1001 		return SMC_CLC_DECL_NOSMCRDEV;
1002 	return 0;
1003 }
1004 
1005 /* check if there is an ISM device available for this connection. */
1006 /* called for connect and listen */
smc_find_ism_device(struct smc_sock * smc,struct smc_init_info * ini)1007 static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
1008 {
1009 	/* Find ISM device with same PNETID as connecting interface  */
1010 	smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
1011 	if (!ini->ism_dev[0])
1012 		return SMC_CLC_DECL_NOSMCDDEV;
1013 	else
1014 		ini->ism_chid[0] = smc_ism_get_chid(ini->ism_dev[0]);
1015 	return 0;
1016 }
1017 
1018 /* is chid unique for the ism devices that are already determined? */
smc_find_ism_v2_is_unique_chid(u16 chid,struct smc_init_info * ini,int cnt)1019 static bool smc_find_ism_v2_is_unique_chid(u16 chid, struct smc_init_info *ini,
1020 					   int cnt)
1021 {
1022 	int i = (!ini->ism_dev[0]) ? 1 : 0;
1023 
1024 	for (; i < cnt; i++)
1025 		if (ini->ism_chid[i] == chid)
1026 			return false;
1027 	return true;
1028 }
1029 
1030 /* determine possible V2 ISM devices (either without PNETID or with PNETID plus
1031  * PNETID matching net_device)
1032  */
smc_find_ism_v2_device_clnt(struct smc_sock * smc,struct smc_init_info * ini)1033 static int smc_find_ism_v2_device_clnt(struct smc_sock *smc,
1034 				       struct smc_init_info *ini)
1035 {
1036 	int rc = SMC_CLC_DECL_NOSMCDDEV;
1037 	struct smcd_dev *smcd;
1038 	int i = 1, entry = 1;
1039 	bool is_virtual;
1040 	u16 chid;
1041 
1042 	if (smcd_indicated(ini->smc_type_v1))
1043 		rc = 0;		/* already initialized for V1 */
1044 	mutex_lock(&smcd_dev_list.mutex);
1045 	list_for_each_entry(smcd, &smcd_dev_list.list, list) {
1046 		if (smcd->going_away || smcd == ini->ism_dev[0])
1047 			continue;
1048 		chid = smc_ism_get_chid(smcd);
1049 		if (!smc_find_ism_v2_is_unique_chid(chid, ini, i))
1050 			continue;
1051 		is_virtual = __smc_ism_is_virtual(chid);
1052 		if (!smc_pnet_is_pnetid_set(smcd->pnetid) ||
1053 		    smc_pnet_is_ndev_pnetid(sock_net(&smc->sk), smcd->pnetid)) {
1054 			if (is_virtual && entry == SMCD_CLC_MAX_V2_GID_ENTRIES)
1055 				/* It's the last GID-CHID entry left in CLC
1056 				 * Proposal SMC-Dv2 extension, but a virtual
1057 				 * ISM device will take two entries. So give
1058 				 * up it and try the next potential ISM device.
1059 				 */
1060 				continue;
1061 			ini->ism_dev[i] = smcd;
1062 			ini->ism_chid[i] = chid;
1063 			ini->is_smcd = true;
1064 			rc = 0;
1065 			i++;
1066 			entry = is_virtual ? entry + 2 : entry + 1;
1067 			if (entry > SMCD_CLC_MAX_V2_GID_ENTRIES)
1068 				break;
1069 		}
1070 	}
1071 	mutex_unlock(&smcd_dev_list.mutex);
1072 	ini->ism_offered_cnt = i - 1;
1073 	if (!ini->ism_dev[0] && !ini->ism_dev[1])
1074 		ini->smcd_version = 0;
1075 
1076 	return rc;
1077 }
1078 
1079 /* Check for VLAN ID and register it on ISM device just for CLC handshake */
smc_connect_ism_vlan_setup(struct smc_sock * smc,struct smc_init_info * ini)1080 static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
1081 				      struct smc_init_info *ini)
1082 {
1083 	if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev[0], ini->vlan_id))
1084 		return SMC_CLC_DECL_ISMVLANERR;
1085 	return 0;
1086 }
1087 
smc_find_proposal_devices(struct smc_sock * smc,struct smc_init_info * ini)1088 static int smc_find_proposal_devices(struct smc_sock *smc,
1089 				     struct smc_init_info *ini)
1090 {
1091 	int rc = 0;
1092 
1093 	/* check if there is an ism device available */
1094 	if (!(ini->smcd_version & SMC_V1) ||
1095 	    smc_find_ism_device(smc, ini) ||
1096 	    smc_connect_ism_vlan_setup(smc, ini))
1097 		ini->smcd_version &= ~SMC_V1;
1098 	/* else ISM V1 is supported for this connection */
1099 
1100 	/* check if there is an rdma device available */
1101 	if (!(ini->smcr_version & SMC_V1) ||
1102 	    smc_find_rdma_device(smc, ini))
1103 		ini->smcr_version &= ~SMC_V1;
1104 	/* else RDMA is supported for this connection */
1105 
1106 	ini->smc_type_v1 = smc_indicated_type(ini->smcd_version & SMC_V1,
1107 					      ini->smcr_version & SMC_V1);
1108 
1109 	/* check if there is an ism v2 device available */
1110 	if (!(ini->smcd_version & SMC_V2) ||
1111 	    !smc_ism_is_v2_capable() ||
1112 	    smc_find_ism_v2_device_clnt(smc, ini))
1113 		ini->smcd_version &= ~SMC_V2;
1114 
1115 	/* check if there is an rdma v2 device available */
1116 	ini->check_smcrv2 = true;
1117 	ini->smcrv2.saddr = smc->clcsock->sk->sk_rcv_saddr;
1118 	if (!(ini->smcr_version & SMC_V2) ||
1119 	    smc->clcsock->sk->sk_family != AF_INET ||
1120 	    !smc_clc_ueid_count() ||
1121 	    smc_find_rdma_device(smc, ini))
1122 		ini->smcr_version &= ~SMC_V2;
1123 	ini->check_smcrv2 = false;
1124 
1125 	ini->smc_type_v2 = smc_indicated_type(ini->smcd_version & SMC_V2,
1126 					      ini->smcr_version & SMC_V2);
1127 
1128 	/* if neither ISM nor RDMA are supported, fallback */
1129 	if (ini->smc_type_v1 == SMC_TYPE_N && ini->smc_type_v2 == SMC_TYPE_N)
1130 		rc = SMC_CLC_DECL_NOSMCDEV;
1131 
1132 	return rc;
1133 }
1134 
1135 /* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
1136  * used, the VLAN ID will be registered again during the connection setup.
1137  */
smc_connect_ism_vlan_cleanup(struct smc_sock * smc,struct smc_init_info * ini)1138 static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc,
1139 					struct smc_init_info *ini)
1140 {
1141 	if (!smcd_indicated(ini->smc_type_v1))
1142 		return 0;
1143 	if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev[0], ini->vlan_id))
1144 		return SMC_CLC_DECL_CNFERR;
1145 	return 0;
1146 }
1147 
1148 #define SMC_CLC_MAX_ACCEPT_LEN \
1149 	(sizeof(struct smc_clc_msg_accept_confirm) + \
1150 	 sizeof(struct smc_clc_first_contact_ext_v2x) + \
1151 	 sizeof(struct smc_clc_msg_trail))
1152 
1153 /* CLC handshake during connect */
smc_connect_clc(struct smc_sock * smc,struct smc_clc_msg_accept_confirm * aclc,struct smc_init_info * ini)1154 static int smc_connect_clc(struct smc_sock *smc,
1155 			   struct smc_clc_msg_accept_confirm *aclc,
1156 			   struct smc_init_info *ini)
1157 {
1158 	int rc = 0;
1159 
1160 	/* do inband token exchange */
1161 	rc = smc_clc_send_proposal(smc, ini);
1162 	if (rc)
1163 		return rc;
1164 	/* receive SMC Accept CLC message */
1165 	return smc_clc_wait_msg(smc, aclc, SMC_CLC_MAX_ACCEPT_LEN,
1166 				SMC_CLC_ACCEPT, CLC_WAIT_TIME);
1167 }
1168 
smc_fill_gid_list(struct smc_link_group * lgr,struct smc_gidlist * gidlist,struct smc_ib_device * known_dev,u8 * known_gid)1169 void smc_fill_gid_list(struct smc_link_group *lgr,
1170 		       struct smc_gidlist *gidlist,
1171 		       struct smc_ib_device *known_dev, u8 *known_gid)
1172 {
1173 	struct smc_init_info *alt_ini = NULL;
1174 
1175 	memset(gidlist, 0, sizeof(*gidlist));
1176 	memcpy(gidlist->list[gidlist->len++], known_gid, SMC_GID_SIZE);
1177 
1178 	alt_ini = kzalloc(sizeof(*alt_ini), GFP_KERNEL);
1179 	if (!alt_ini)
1180 		goto out;
1181 
1182 	alt_ini->vlan_id = lgr->vlan_id;
1183 	alt_ini->check_smcrv2 = true;
1184 	alt_ini->smcrv2.saddr = lgr->saddr;
1185 	smc_pnet_find_alt_roce(lgr, alt_ini, known_dev);
1186 
1187 	if (!alt_ini->smcrv2.ib_dev_v2)
1188 		goto out;
1189 
1190 	memcpy(gidlist->list[gidlist->len++], alt_ini->smcrv2.ib_gid_v2,
1191 	       SMC_GID_SIZE);
1192 
1193 out:
1194 	kfree(alt_ini);
1195 }
1196 
smc_connect_rdma_v2_prepare(struct smc_sock * smc,struct smc_clc_msg_accept_confirm * aclc,struct smc_init_info * ini)1197 static int smc_connect_rdma_v2_prepare(struct smc_sock *smc,
1198 				       struct smc_clc_msg_accept_confirm *aclc,
1199 				       struct smc_init_info *ini)
1200 {
1201 	struct smc_clc_first_contact_ext *fce =
1202 		smc_get_clc_first_contact_ext(aclc, false);
1203 	struct net *net = sock_net(&smc->sk);
1204 	int rc;
1205 
1206 	if (!ini->first_contact_peer || aclc->hdr.version == SMC_V1)
1207 		return 0;
1208 
1209 	if (fce->v2_direct) {
1210 		memcpy(ini->smcrv2.nexthop_mac, &aclc->r0.lcl.mac, ETH_ALEN);
1211 		ini->smcrv2.uses_gateway = false;
1212 	} else {
1213 		if (smc_ib_find_route(net, smc->clcsock->sk->sk_rcv_saddr,
1214 				      smc_ib_gid_to_ipv4(aclc->r0.lcl.gid),
1215 				      ini->smcrv2.nexthop_mac,
1216 				      &ini->smcrv2.uses_gateway))
1217 			return SMC_CLC_DECL_NOROUTE;
1218 		if (!ini->smcrv2.uses_gateway) {
1219 			/* mismatch: peer claims indirect, but its direct */
1220 			return SMC_CLC_DECL_NOINDIRECT;
1221 		}
1222 	}
1223 
1224 	ini->release_nr = fce->release;
1225 	rc = smc_clc_clnt_v2x_features_validate(fce, ini);
1226 	if (rc)
1227 		return rc;
1228 
1229 	return 0;
1230 }
1231 
1232 /* setup for RDMA connection of client */
smc_connect_rdma(struct smc_sock * smc,struct smc_clc_msg_accept_confirm * aclc,struct smc_init_info * ini)1233 static int smc_connect_rdma(struct smc_sock *smc,
1234 			    struct smc_clc_msg_accept_confirm *aclc,
1235 			    struct smc_init_info *ini)
1236 {
1237 	int i, reason_code = 0;
1238 	struct smc_link *link;
1239 	u8 *eid = NULL;
1240 
1241 	ini->is_smcd = false;
1242 	ini->ib_clcqpn = ntoh24(aclc->r0.qpn);
1243 	ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
1244 	memcpy(ini->peer_systemid, aclc->r0.lcl.id_for_peer, SMC_SYSTEMID_LEN);
1245 	memcpy(ini->peer_gid, aclc->r0.lcl.gid, SMC_GID_SIZE);
1246 	memcpy(ini->peer_mac, aclc->r0.lcl.mac, ETH_ALEN);
1247 	ini->max_conns = SMC_CONN_PER_LGR_MAX;
1248 	ini->max_links = SMC_LINKS_ADD_LNK_MAX;
1249 
1250 	reason_code = smc_connect_rdma_v2_prepare(smc, aclc, ini);
1251 	if (reason_code)
1252 		return reason_code;
1253 
1254 	mutex_lock(&smc_client_lgr_pending);
1255 	reason_code = smc_conn_create(smc, ini);
1256 	if (reason_code) {
1257 		mutex_unlock(&smc_client_lgr_pending);
1258 		return reason_code;
1259 	}
1260 
1261 	smc_conn_save_peer_info(smc, aclc);
1262 
1263 	if (ini->first_contact_local) {
1264 		link = smc->conn.lnk;
1265 	} else {
1266 		/* set link that was assigned by server */
1267 		link = NULL;
1268 		for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1269 			struct smc_link *l = &smc->conn.lgr->lnk[i];
1270 
1271 			if (l->peer_qpn == ntoh24(aclc->r0.qpn) &&
1272 			    !memcmp(l->peer_gid, &aclc->r0.lcl.gid,
1273 				    SMC_GID_SIZE) &&
1274 			    (aclc->hdr.version > SMC_V1 ||
1275 			     !memcmp(l->peer_mac, &aclc->r0.lcl.mac,
1276 				     sizeof(l->peer_mac)))) {
1277 				link = l;
1278 				break;
1279 			}
1280 		}
1281 		if (!link) {
1282 			reason_code = SMC_CLC_DECL_NOSRVLINK;
1283 			goto connect_abort;
1284 		}
1285 		smc_switch_link_and_count(&smc->conn, link);
1286 	}
1287 
1288 	/* create send buffer and rmb */
1289 	if (smc_buf_create(smc, false)) {
1290 		reason_code = SMC_CLC_DECL_MEM;
1291 		goto connect_abort;
1292 	}
1293 
1294 	if (ini->first_contact_local)
1295 		smc_link_save_peer_info(link, aclc, ini);
1296 
1297 	if (smc_rmb_rtoken_handling(&smc->conn, link, aclc)) {
1298 		reason_code = SMC_CLC_DECL_ERR_RTOK;
1299 		goto connect_abort;
1300 	}
1301 
1302 	smc_rx_init(smc);
1303 
1304 	if (ini->first_contact_local) {
1305 		if (smc_ib_ready_link(link)) {
1306 			reason_code = SMC_CLC_DECL_ERR_RDYLNK;
1307 			goto connect_abort;
1308 		}
1309 	} else {
1310 		/* reg sendbufs if they were vzalloced */
1311 		if (smc->conn.sndbuf_desc->is_vm) {
1312 			if (smcr_lgr_reg_sndbufs(link, smc->conn.sndbuf_desc)) {
1313 				reason_code = SMC_CLC_DECL_ERR_REGBUF;
1314 				goto connect_abort;
1315 			}
1316 		}
1317 		if (smcr_lgr_reg_rmbs(link, smc->conn.rmb_desc)) {
1318 			reason_code = SMC_CLC_DECL_ERR_REGBUF;
1319 			goto connect_abort;
1320 		}
1321 	}
1322 
1323 	if (aclc->hdr.version > SMC_V1) {
1324 		eid = aclc->r1.eid;
1325 		if (ini->first_contact_local)
1326 			smc_fill_gid_list(link->lgr, &ini->smcrv2.gidlist,
1327 					  link->smcibdev, link->gid);
1328 	}
1329 
1330 	reason_code = smc_clc_send_confirm(smc, ini->first_contact_local,
1331 					   aclc->hdr.version, eid, ini);
1332 	if (reason_code)
1333 		goto connect_abort;
1334 
1335 	smc_tx_init(smc);
1336 
1337 	if (ini->first_contact_local) {
1338 		/* QP confirmation over RoCE fabric */
1339 		smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
1340 		reason_code = smcr_clnt_conf_first_link(smc);
1341 		smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
1342 		if (reason_code)
1343 			goto connect_abort;
1344 	}
1345 	mutex_unlock(&smc_client_lgr_pending);
1346 
1347 	smc_copy_sock_settings_to_clc(smc);
1348 	smc->connect_nonblock = 0;
1349 	if (smc->sk.sk_state == SMC_INIT)
1350 		smc->sk.sk_state = SMC_ACTIVE;
1351 
1352 	return 0;
1353 connect_abort:
1354 	smc_conn_abort(smc, ini->first_contact_local);
1355 	mutex_unlock(&smc_client_lgr_pending);
1356 	smc->connect_nonblock = 0;
1357 
1358 	return reason_code;
1359 }
1360 
1361 /* The server has chosen one of the proposed ISM devices for the communication.
1362  * Determine from the CHID of the received CLC ACCEPT the ISM device chosen.
1363  */
1364 static int
smc_v2_determine_accepted_chid(struct smc_clc_msg_accept_confirm * aclc,struct smc_init_info * ini)1365 smc_v2_determine_accepted_chid(struct smc_clc_msg_accept_confirm *aclc,
1366 			       struct smc_init_info *ini)
1367 {
1368 	int i;
1369 
1370 	for (i = 0; i < ini->ism_offered_cnt + 1; i++) {
1371 		if (ini->ism_chid[i] == ntohs(aclc->d1.chid)) {
1372 			ini->ism_selected = i;
1373 			return 0;
1374 		}
1375 	}
1376 
1377 	return -EPROTO;
1378 }
1379 
1380 /* setup for ISM connection of client */
smc_connect_ism(struct smc_sock * smc,struct smc_clc_msg_accept_confirm * aclc,struct smc_init_info * ini)1381 static int smc_connect_ism(struct smc_sock *smc,
1382 			   struct smc_clc_msg_accept_confirm *aclc,
1383 			   struct smc_init_info *ini)
1384 {
1385 	u8 *eid = NULL;
1386 	int rc = 0;
1387 
1388 	ini->is_smcd = true;
1389 	ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
1390 
1391 	if (aclc->hdr.version == SMC_V2) {
1392 		if (ini->first_contact_peer) {
1393 			struct smc_clc_first_contact_ext *fce =
1394 				smc_get_clc_first_contact_ext(aclc, true);
1395 
1396 			ini->release_nr = fce->release;
1397 			rc = smc_clc_clnt_v2x_features_validate(fce, ini);
1398 			if (rc)
1399 				return rc;
1400 		}
1401 
1402 		rc = smc_v2_determine_accepted_chid(aclc, ini);
1403 		if (rc)
1404 			return rc;
1405 
1406 		if (__smc_ism_is_virtual(ini->ism_chid[ini->ism_selected]))
1407 			ini->ism_peer_gid[ini->ism_selected].gid_ext =
1408 						ntohll(aclc->d1.gid_ext);
1409 		/* for non-virtual ISM devices, peer gid_ext remains 0. */
1410 	}
1411 	ini->ism_peer_gid[ini->ism_selected].gid = ntohll(aclc->d0.gid);
1412 
1413 	/* there is only one lgr role for SMC-D; use server lock */
1414 	mutex_lock(&smc_server_lgr_pending);
1415 	rc = smc_conn_create(smc, ini);
1416 	if (rc) {
1417 		mutex_unlock(&smc_server_lgr_pending);
1418 		return rc;
1419 	}
1420 
1421 	/* Create send and receive buffers */
1422 	rc = smc_buf_create(smc, true);
1423 	if (rc) {
1424 		rc = (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB : SMC_CLC_DECL_MEM;
1425 		goto connect_abort;
1426 	}
1427 
1428 	smc_conn_save_peer_info(smc, aclc);
1429 
1430 	if (smc_ism_support_dmb_nocopy(smc->conn.lgr->smcd)) {
1431 		rc = smcd_buf_attach(smc);
1432 		if (rc) {
1433 			rc = SMC_CLC_DECL_MEM;	/* try to fallback */
1434 			goto connect_abort;
1435 		}
1436 	}
1437 	smc_rx_init(smc);
1438 	smc_tx_init(smc);
1439 
1440 	if (aclc->hdr.version > SMC_V1)
1441 		eid = aclc->d1.eid;
1442 
1443 	rc = smc_clc_send_confirm(smc, ini->first_contact_local,
1444 				  aclc->hdr.version, eid, ini);
1445 	if (rc)
1446 		goto connect_abort;
1447 	mutex_unlock(&smc_server_lgr_pending);
1448 
1449 	smc_copy_sock_settings_to_clc(smc);
1450 	smc->connect_nonblock = 0;
1451 	if (smc->sk.sk_state == SMC_INIT)
1452 		smc->sk.sk_state = SMC_ACTIVE;
1453 
1454 	return 0;
1455 connect_abort:
1456 	smc_conn_abort(smc, ini->first_contact_local);
1457 	mutex_unlock(&smc_server_lgr_pending);
1458 	smc->connect_nonblock = 0;
1459 
1460 	return rc;
1461 }
1462 
1463 /* check if received accept type and version matches a proposed one */
smc_connect_check_aclc(struct smc_init_info * ini,struct smc_clc_msg_accept_confirm * aclc)1464 static int smc_connect_check_aclc(struct smc_init_info *ini,
1465 				  struct smc_clc_msg_accept_confirm *aclc)
1466 {
1467 	if (aclc->hdr.typev1 != SMC_TYPE_R &&
1468 	    aclc->hdr.typev1 != SMC_TYPE_D)
1469 		return SMC_CLC_DECL_MODEUNSUPP;
1470 
1471 	if (aclc->hdr.version >= SMC_V2) {
1472 		if ((aclc->hdr.typev1 == SMC_TYPE_R &&
1473 		     !smcr_indicated(ini->smc_type_v2)) ||
1474 		    (aclc->hdr.typev1 == SMC_TYPE_D &&
1475 		     !smcd_indicated(ini->smc_type_v2)))
1476 			return SMC_CLC_DECL_MODEUNSUPP;
1477 	} else {
1478 		if ((aclc->hdr.typev1 == SMC_TYPE_R &&
1479 		     !smcr_indicated(ini->smc_type_v1)) ||
1480 		    (aclc->hdr.typev1 == SMC_TYPE_D &&
1481 		     !smcd_indicated(ini->smc_type_v1)))
1482 			return SMC_CLC_DECL_MODEUNSUPP;
1483 	}
1484 
1485 	return 0;
1486 }
1487 
1488 /* perform steps before actually connecting */
__smc_connect(struct smc_sock * smc)1489 static int __smc_connect(struct smc_sock *smc)
1490 {
1491 	u8 version = smc_ism_is_v2_capable() ? SMC_V2 : SMC_V1;
1492 	struct smc_clc_msg_accept_confirm *aclc;
1493 	struct smc_init_info *ini = NULL;
1494 	u8 *buf = NULL;
1495 	int rc = 0;
1496 
1497 	if (smc->use_fallback)
1498 		return smc_connect_fallback(smc, smc->fallback_rsn);
1499 
1500 	/* if peer has not signalled SMC-capability, fall back */
1501 	if (!tcp_sk(smc->clcsock->sk)->syn_smc)
1502 		return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
1503 
1504 	/* IPSec connections opt out of SMC optimizations */
1505 	if (using_ipsec(smc))
1506 		return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC,
1507 						    version);
1508 
1509 	ini = kzalloc(sizeof(*ini), GFP_KERNEL);
1510 	if (!ini)
1511 		return smc_connect_decline_fallback(smc, SMC_CLC_DECL_MEM,
1512 						    version);
1513 
1514 	ini->smcd_version = SMC_V1 | SMC_V2;
1515 	ini->smcr_version = SMC_V1 | SMC_V2;
1516 	ini->smc_type_v1 = SMC_TYPE_B;
1517 	ini->smc_type_v2 = SMC_TYPE_B;
1518 
1519 	/* get vlan id from IP device */
1520 	if (smc_vlan_by_tcpsk(smc->clcsock, ini)) {
1521 		ini->smcd_version &= ~SMC_V1;
1522 		ini->smcr_version = 0;
1523 		ini->smc_type_v1 = SMC_TYPE_N;
1524 		if (!ini->smcd_version) {
1525 			rc = SMC_CLC_DECL_GETVLANERR;
1526 			goto fallback;
1527 		}
1528 	}
1529 
1530 	rc = smc_find_proposal_devices(smc, ini);
1531 	if (rc)
1532 		goto fallback;
1533 
1534 	buf = kzalloc(SMC_CLC_MAX_ACCEPT_LEN, GFP_KERNEL);
1535 	if (!buf) {
1536 		rc = SMC_CLC_DECL_MEM;
1537 		goto fallback;
1538 	}
1539 	aclc = (struct smc_clc_msg_accept_confirm *)buf;
1540 
1541 	/* perform CLC handshake */
1542 	rc = smc_connect_clc(smc, aclc, ini);
1543 	if (rc) {
1544 		/* -EAGAIN on timeout, see tcp_recvmsg() */
1545 		if (rc == -EAGAIN) {
1546 			rc = -ETIMEDOUT;
1547 			smc->sk.sk_err = ETIMEDOUT;
1548 		}
1549 		goto vlan_cleanup;
1550 	}
1551 
1552 	/* check if smc modes and versions of CLC proposal and accept match */
1553 	rc = smc_connect_check_aclc(ini, aclc);
1554 	version = aclc->hdr.version == SMC_V1 ? SMC_V1 : SMC_V2;
1555 	if (rc)
1556 		goto vlan_cleanup;
1557 
1558 	/* depending on previous steps, connect using rdma or ism */
1559 	if (aclc->hdr.typev1 == SMC_TYPE_R) {
1560 		ini->smcr_version = version;
1561 		rc = smc_connect_rdma(smc, aclc, ini);
1562 	} else if (aclc->hdr.typev1 == SMC_TYPE_D) {
1563 		ini->smcd_version = version;
1564 		rc = smc_connect_ism(smc, aclc, ini);
1565 	}
1566 	if (rc)
1567 		goto vlan_cleanup;
1568 
1569 	SMC_STAT_CLNT_SUCC_INC(sock_net(smc->clcsock->sk), aclc);
1570 	smc_connect_ism_vlan_cleanup(smc, ini);
1571 	kfree(buf);
1572 	kfree(ini);
1573 	return 0;
1574 
1575 vlan_cleanup:
1576 	smc_connect_ism_vlan_cleanup(smc, ini);
1577 	kfree(buf);
1578 fallback:
1579 	kfree(ini);
1580 	return smc_connect_decline_fallback(smc, rc, version);
1581 }
1582 
smc_connect_work(struct work_struct * work)1583 static void smc_connect_work(struct work_struct *work)
1584 {
1585 	struct smc_sock *smc = container_of(work, struct smc_sock,
1586 					    connect_work);
1587 	long timeo = smc->sk.sk_sndtimeo;
1588 	int rc = 0;
1589 
1590 	if (!timeo)
1591 		timeo = MAX_SCHEDULE_TIMEOUT;
1592 	lock_sock(smc->clcsock->sk);
1593 	if (smc->clcsock->sk->sk_err) {
1594 		smc->sk.sk_err = smc->clcsock->sk->sk_err;
1595 	} else if ((1 << smc->clcsock->sk->sk_state) &
1596 					(TCPF_SYN_SENT | TCPF_SYN_RECV)) {
1597 		rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
1598 		if ((rc == -EPIPE) &&
1599 		    ((1 << smc->clcsock->sk->sk_state) &
1600 					(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
1601 			rc = 0;
1602 	}
1603 	release_sock(smc->clcsock->sk);
1604 	lock_sock(&smc->sk);
1605 	if (rc != 0 || smc->sk.sk_err) {
1606 		smc->sk.sk_state = SMC_CLOSED;
1607 		if (rc == -EPIPE || rc == -EAGAIN)
1608 			smc->sk.sk_err = EPIPE;
1609 		else if (rc == -ECONNREFUSED)
1610 			smc->sk.sk_err = ECONNREFUSED;
1611 		else if (signal_pending(current))
1612 			smc->sk.sk_err = -sock_intr_errno(timeo);
1613 		sock_put(&smc->sk); /* passive closing */
1614 		goto out;
1615 	}
1616 
1617 	rc = __smc_connect(smc);
1618 	if (rc < 0)
1619 		smc->sk.sk_err = -rc;
1620 
1621 out:
1622 	if (!sock_flag(&smc->sk, SOCK_DEAD)) {
1623 		if (smc->sk.sk_err) {
1624 			smc->sk.sk_state_change(&smc->sk);
1625 		} else { /* allow polling before and after fallback decision */
1626 			smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
1627 			smc->sk.sk_write_space(&smc->sk);
1628 		}
1629 	}
1630 	release_sock(&smc->sk);
1631 }
1632 
smc_connect(struct socket * sock,struct sockaddr * addr,int alen,int flags)1633 static int smc_connect(struct socket *sock, struct sockaddr *addr,
1634 		       int alen, int flags)
1635 {
1636 	struct sock *sk = sock->sk;
1637 	struct smc_sock *smc;
1638 	int rc = -EINVAL;
1639 
1640 	smc = smc_sk(sk);
1641 
1642 	/* separate smc parameter checking to be safe */
1643 	if (alen < sizeof(addr->sa_family))
1644 		goto out_err;
1645 	if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
1646 		goto out_err;
1647 
1648 	lock_sock(sk);
1649 	switch (sock->state) {
1650 	default:
1651 		rc = -EINVAL;
1652 		goto out;
1653 	case SS_CONNECTED:
1654 		rc = sk->sk_state == SMC_ACTIVE ? -EISCONN : -EINVAL;
1655 		goto out;
1656 	case SS_CONNECTING:
1657 		if (sk->sk_state == SMC_ACTIVE)
1658 			goto connected;
1659 		break;
1660 	case SS_UNCONNECTED:
1661 		sock->state = SS_CONNECTING;
1662 		break;
1663 	}
1664 
1665 	switch (sk->sk_state) {
1666 	default:
1667 		goto out;
1668 	case SMC_CLOSED:
1669 		rc = sock_error(sk) ? : -ECONNABORTED;
1670 		sock->state = SS_UNCONNECTED;
1671 		goto out;
1672 	case SMC_ACTIVE:
1673 		rc = -EISCONN;
1674 		goto out;
1675 	case SMC_INIT:
1676 		break;
1677 	}
1678 
1679 	smc_copy_sock_settings_to_clc(smc);
1680 	tcp_sk(smc->clcsock->sk)->syn_smc = 1;
1681 	if (smc->connect_nonblock) {
1682 		rc = -EALREADY;
1683 		goto out;
1684 	}
1685 	rc = kernel_connect(smc->clcsock, addr, alen, flags);
1686 	if (rc && rc != -EINPROGRESS)
1687 		goto out;
1688 
1689 	if (smc->use_fallback) {
1690 		sock->state = rc ? SS_CONNECTING : SS_CONNECTED;
1691 		goto out;
1692 	}
1693 	sock_hold(&smc->sk); /* sock put in passive closing */
1694 	if (flags & O_NONBLOCK) {
1695 		if (queue_work(smc_hs_wq, &smc->connect_work))
1696 			smc->connect_nonblock = 1;
1697 		rc = -EINPROGRESS;
1698 		goto out;
1699 	} else {
1700 		rc = __smc_connect(smc);
1701 		if (rc < 0)
1702 			goto out;
1703 	}
1704 
1705 connected:
1706 	rc = 0;
1707 	sock->state = SS_CONNECTED;
1708 out:
1709 	release_sock(sk);
1710 out_err:
1711 	return rc;
1712 }
1713 
smc_clcsock_accept(struct smc_sock * lsmc,struct smc_sock ** new_smc)1714 static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
1715 {
1716 	struct socket *new_clcsock = NULL;
1717 	struct sock *lsk = &lsmc->sk;
1718 	struct sock *new_sk;
1719 	int rc = -EINVAL;
1720 
1721 	release_sock(lsk);
1722 	new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
1723 	if (!new_sk) {
1724 		rc = -ENOMEM;
1725 		lsk->sk_err = ENOMEM;
1726 		*new_smc = NULL;
1727 		lock_sock(lsk);
1728 		goto out;
1729 	}
1730 	*new_smc = smc_sk(new_sk);
1731 
1732 	mutex_lock(&lsmc->clcsock_release_lock);
1733 	if (lsmc->clcsock)
1734 		rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK);
1735 	mutex_unlock(&lsmc->clcsock_release_lock);
1736 	lock_sock(lsk);
1737 	if  (rc < 0 && rc != -EAGAIN)
1738 		lsk->sk_err = -rc;
1739 	if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
1740 		new_sk->sk_prot->unhash(new_sk);
1741 		if (new_clcsock)
1742 			sock_release(new_clcsock);
1743 		new_sk->sk_state = SMC_CLOSED;
1744 		smc_sock_set_flag(new_sk, SOCK_DEAD);
1745 		sock_put(new_sk); /* final */
1746 		*new_smc = NULL;
1747 		goto out;
1748 	}
1749 
1750 	/* new clcsock has inherited the smc listen-specific sk_data_ready
1751 	 * function; switch it back to the original sk_data_ready function
1752 	 */
1753 	new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready;
1754 
1755 	/* if new clcsock has also inherited the fallback-specific callback
1756 	 * functions, switch them back to the original ones.
1757 	 */
1758 	if (lsmc->use_fallback) {
1759 		if (lsmc->clcsk_state_change)
1760 			new_clcsock->sk->sk_state_change = lsmc->clcsk_state_change;
1761 		if (lsmc->clcsk_write_space)
1762 			new_clcsock->sk->sk_write_space = lsmc->clcsk_write_space;
1763 		if (lsmc->clcsk_error_report)
1764 			new_clcsock->sk->sk_error_report = lsmc->clcsk_error_report;
1765 	}
1766 
1767 	(*new_smc)->clcsock = new_clcsock;
1768 out:
1769 	return rc;
1770 }
1771 
1772 /* add a just created sock to the accept queue of the listen sock as
1773  * candidate for a following socket accept call from user space
1774  */
smc_accept_enqueue(struct sock * parent,struct sock * sk)1775 static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
1776 {
1777 	struct smc_sock *par = smc_sk(parent);
1778 
1779 	sock_hold(sk); /* sock_put in smc_accept_unlink () */
1780 	spin_lock(&par->accept_q_lock);
1781 	list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
1782 	spin_unlock(&par->accept_q_lock);
1783 	sk_acceptq_added(parent);
1784 }
1785 
1786 /* remove a socket from the accept queue of its parental listening socket */
smc_accept_unlink(struct sock * sk)1787 static void smc_accept_unlink(struct sock *sk)
1788 {
1789 	struct smc_sock *par = smc_sk(sk)->listen_smc;
1790 
1791 	spin_lock(&par->accept_q_lock);
1792 	list_del_init(&smc_sk(sk)->accept_q);
1793 	spin_unlock(&par->accept_q_lock);
1794 	sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
1795 	sock_put(sk); /* sock_hold in smc_accept_enqueue */
1796 }
1797 
1798 /* remove a sock from the accept queue to bind it to a new socket created
1799  * for a socket accept call from user space
1800  */
smc_accept_dequeue(struct sock * parent,struct socket * new_sock)1801 struct sock *smc_accept_dequeue(struct sock *parent,
1802 				struct socket *new_sock)
1803 {
1804 	struct smc_sock *isk, *n;
1805 	struct sock *new_sk;
1806 
1807 	list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
1808 		new_sk = (struct sock *)isk;
1809 
1810 		smc_accept_unlink(new_sk);
1811 		if (new_sk->sk_state == SMC_CLOSED) {
1812 			new_sk->sk_prot->unhash(new_sk);
1813 			if (isk->clcsock) {
1814 				sock_release(isk->clcsock);
1815 				isk->clcsock = NULL;
1816 			}
1817 			sock_put(new_sk); /* final */
1818 			continue;
1819 		}
1820 		if (new_sock) {
1821 			sock_graft(new_sk, new_sock);
1822 			new_sock->state = SS_CONNECTED;
1823 			if (isk->use_fallback) {
1824 				smc_sk(new_sk)->clcsock->file = new_sock->file;
1825 				isk->clcsock->file->private_data = isk->clcsock;
1826 			}
1827 		}
1828 		return new_sk;
1829 	}
1830 	return NULL;
1831 }
1832 
1833 /* clean up for a created but never accepted sock */
smc_close_non_accepted(struct sock * sk)1834 void smc_close_non_accepted(struct sock *sk)
1835 {
1836 	struct smc_sock *smc = smc_sk(sk);
1837 
1838 	sock_hold(sk); /* sock_put below */
1839 	lock_sock(sk);
1840 	if (!sk->sk_lingertime)
1841 		/* wait for peer closing */
1842 		WRITE_ONCE(sk->sk_lingertime, SMC_MAX_STREAM_WAIT_TIMEOUT);
1843 	__smc_release(smc);
1844 	release_sock(sk);
1845 	sock_put(sk); /* sock_hold above */
1846 	sock_put(sk); /* final sock_put */
1847 }
1848 
smcr_serv_conf_first_link(struct smc_sock * smc)1849 static int smcr_serv_conf_first_link(struct smc_sock *smc)
1850 {
1851 	struct smc_link *link = smc->conn.lnk;
1852 	struct smc_llc_qentry *qentry;
1853 	int rc;
1854 
1855 	/* reg the sndbuf if it was vzalloced*/
1856 	if (smc->conn.sndbuf_desc->is_vm) {
1857 		if (smcr_link_reg_buf(link, smc->conn.sndbuf_desc))
1858 			return SMC_CLC_DECL_ERR_REGBUF;
1859 	}
1860 
1861 	/* reg the rmb */
1862 	if (smcr_link_reg_buf(link, smc->conn.rmb_desc))
1863 		return SMC_CLC_DECL_ERR_REGBUF;
1864 
1865 	/* send CONFIRM LINK request to client over the RoCE fabric */
1866 	rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
1867 	if (rc < 0)
1868 		return SMC_CLC_DECL_TIMEOUT_CL;
1869 
1870 	/* receive CONFIRM LINK response from client over the RoCE fabric */
1871 	qentry = smc_llc_wait(link->lgr, link, SMC_LLC_WAIT_TIME,
1872 			      SMC_LLC_CONFIRM_LINK);
1873 	if (!qentry) {
1874 		struct smc_clc_msg_decline dclc;
1875 
1876 		rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
1877 				      SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
1878 		return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
1879 	}
1880 	smc_llc_save_peer_uid(qentry);
1881 	rc = smc_llc_eval_conf_link(qentry, SMC_LLC_RESP);
1882 	smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
1883 	if (rc)
1884 		return SMC_CLC_DECL_RMBE_EC;
1885 
1886 	/* confirm_rkey is implicit on 1st contact */
1887 	smc->conn.rmb_desc->is_conf_rkey = true;
1888 
1889 	smc_llc_link_active(link);
1890 	smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
1891 
1892 	if (link->lgr->max_links > 1) {
1893 		down_write(&link->lgr->llc_conf_mutex);
1894 		/* initial contact - try to establish second link */
1895 		smc_llc_srv_add_link(link, NULL);
1896 		up_write(&link->lgr->llc_conf_mutex);
1897 	}
1898 	return 0;
1899 }
1900 
1901 /* listen worker: finish */
smc_listen_out(struct smc_sock * new_smc)1902 static void smc_listen_out(struct smc_sock *new_smc)
1903 {
1904 	struct smc_sock *lsmc = new_smc->listen_smc;
1905 	struct sock *newsmcsk = &new_smc->sk;
1906 
1907 	if (tcp_sk(new_smc->clcsock->sk)->syn_smc)
1908 		atomic_dec(&lsmc->queued_smc_hs);
1909 
1910 	release_sock(newsmcsk); /* lock in smc_listen_work() */
1911 	if (lsmc->sk.sk_state == SMC_LISTEN) {
1912 		lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
1913 		smc_accept_enqueue(&lsmc->sk, newsmcsk);
1914 		release_sock(&lsmc->sk);
1915 	} else { /* no longer listening */
1916 		smc_close_non_accepted(newsmcsk);
1917 	}
1918 
1919 	/* Wake up accept */
1920 	lsmc->sk.sk_data_ready(&lsmc->sk);
1921 	sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
1922 }
1923 
1924 /* listen worker: finish in state connected */
smc_listen_out_connected(struct smc_sock * new_smc)1925 static void smc_listen_out_connected(struct smc_sock *new_smc)
1926 {
1927 	struct sock *newsmcsk = &new_smc->sk;
1928 
1929 	if (newsmcsk->sk_state == SMC_INIT)
1930 		newsmcsk->sk_state = SMC_ACTIVE;
1931 
1932 	smc_listen_out(new_smc);
1933 }
1934 
1935 /* listen worker: finish in error state */
smc_listen_out_err(struct smc_sock * new_smc)1936 static void smc_listen_out_err(struct smc_sock *new_smc)
1937 {
1938 	struct sock *newsmcsk = &new_smc->sk;
1939 	struct net *net = sock_net(newsmcsk);
1940 
1941 	this_cpu_inc(net->smc.smc_stats->srv_hshake_err_cnt);
1942 	if (newsmcsk->sk_state == SMC_INIT)
1943 		sock_put(&new_smc->sk); /* passive closing */
1944 	newsmcsk->sk_state = SMC_CLOSED;
1945 
1946 	smc_listen_out(new_smc);
1947 }
1948 
1949 /* listen worker: decline and fall back if possible */
smc_listen_decline(struct smc_sock * new_smc,int reason_code,int local_first,u8 version)1950 static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1951 			       int local_first, u8 version)
1952 {
1953 	/* RDMA setup failed, switch back to TCP */
1954 	smc_conn_abort(new_smc, local_first);
1955 	if (reason_code < 0 ||
1956 	    smc_switch_to_fallback(new_smc, reason_code)) {
1957 		/* error, no fallback possible */
1958 		smc_listen_out_err(new_smc);
1959 		return;
1960 	}
1961 	if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
1962 		if (smc_clc_send_decline(new_smc, reason_code, version) < 0) {
1963 			smc_listen_out_err(new_smc);
1964 			return;
1965 		}
1966 	}
1967 	smc_listen_out_connected(new_smc);
1968 }
1969 
1970 /* listen worker: version checking */
smc_listen_v2_check(struct smc_sock * new_smc,struct smc_clc_msg_proposal * pclc,struct smc_init_info * ini)1971 static int smc_listen_v2_check(struct smc_sock *new_smc,
1972 			       struct smc_clc_msg_proposal *pclc,
1973 			       struct smc_init_info *ini)
1974 {
1975 	struct smc_clc_smcd_v2_extension *pclc_smcd_v2_ext;
1976 	struct smc_clc_v2_extension *pclc_v2_ext;
1977 	int rc = SMC_CLC_DECL_PEERNOSMC;
1978 
1979 	ini->smc_type_v1 = pclc->hdr.typev1;
1980 	ini->smc_type_v2 = pclc->hdr.typev2;
1981 	ini->smcd_version = smcd_indicated(ini->smc_type_v1) ? SMC_V1 : 0;
1982 	ini->smcr_version = smcr_indicated(ini->smc_type_v1) ? SMC_V1 : 0;
1983 	if (pclc->hdr.version > SMC_V1) {
1984 		if (smcd_indicated(ini->smc_type_v2))
1985 			ini->smcd_version |= SMC_V2;
1986 		if (smcr_indicated(ini->smc_type_v2))
1987 			ini->smcr_version |= SMC_V2;
1988 	}
1989 	if (!(ini->smcd_version & SMC_V2) && !(ini->smcr_version & SMC_V2)) {
1990 		rc = SMC_CLC_DECL_PEERNOSMC;
1991 		goto out;
1992 	}
1993 	pclc_v2_ext = smc_get_clc_v2_ext(pclc);
1994 	if (!pclc_v2_ext) {
1995 		ini->smcd_version &= ~SMC_V2;
1996 		ini->smcr_version &= ~SMC_V2;
1997 		rc = SMC_CLC_DECL_NOV2EXT;
1998 		goto out;
1999 	}
2000 	pclc_smcd_v2_ext = smc_get_clc_smcd_v2_ext(pclc_v2_ext);
2001 	if (ini->smcd_version & SMC_V2) {
2002 		if (!smc_ism_is_v2_capable()) {
2003 			ini->smcd_version &= ~SMC_V2;
2004 			rc = SMC_CLC_DECL_NOISM2SUPP;
2005 		} else if (!pclc_smcd_v2_ext) {
2006 			ini->smcd_version &= ~SMC_V2;
2007 			rc = SMC_CLC_DECL_NOV2DEXT;
2008 		} else if (!pclc_v2_ext->hdr.eid_cnt &&
2009 			   !pclc_v2_ext->hdr.flag.seid) {
2010 			ini->smcd_version &= ~SMC_V2;
2011 			rc = SMC_CLC_DECL_NOUEID;
2012 		}
2013 	}
2014 	if (ini->smcr_version & SMC_V2) {
2015 		if (!pclc_v2_ext->hdr.eid_cnt) {
2016 			ini->smcr_version &= ~SMC_V2;
2017 			rc = SMC_CLC_DECL_NOUEID;
2018 		}
2019 	}
2020 
2021 	ini->release_nr = pclc_v2_ext->hdr.flag.release;
2022 	if (pclc_v2_ext->hdr.flag.release > SMC_RELEASE)
2023 		ini->release_nr = SMC_RELEASE;
2024 
2025 out:
2026 	if (!ini->smcd_version && !ini->smcr_version)
2027 		return rc;
2028 
2029 	return 0;
2030 }
2031 
2032 /* listen worker: check prefixes */
smc_listen_prfx_check(struct smc_sock * new_smc,struct smc_clc_msg_proposal * pclc)2033 static int smc_listen_prfx_check(struct smc_sock *new_smc,
2034 				 struct smc_clc_msg_proposal *pclc)
2035 {
2036 	struct smc_clc_msg_proposal_prefix *pclc_prfx;
2037 	struct socket *newclcsock = new_smc->clcsock;
2038 
2039 	if (pclc->hdr.typev1 == SMC_TYPE_N)
2040 		return 0;
2041 	pclc_prfx = smc_clc_proposal_get_prefix(pclc);
2042 	if (smc_clc_prfx_match(newclcsock, pclc_prfx))
2043 		return SMC_CLC_DECL_DIFFPREFIX;
2044 
2045 	return 0;
2046 }
2047 
2048 /* listen worker: initialize connection and buffers */
smc_listen_rdma_init(struct smc_sock * new_smc,struct smc_init_info * ini)2049 static int smc_listen_rdma_init(struct smc_sock *new_smc,
2050 				struct smc_init_info *ini)
2051 {
2052 	int rc;
2053 
2054 	/* allocate connection / link group */
2055 	rc = smc_conn_create(new_smc, ini);
2056 	if (rc)
2057 		return rc;
2058 
2059 	/* create send buffer and rmb */
2060 	if (smc_buf_create(new_smc, false)) {
2061 		smc_conn_abort(new_smc, ini->first_contact_local);
2062 		return SMC_CLC_DECL_MEM;
2063 	}
2064 
2065 	return 0;
2066 }
2067 
2068 /* listen worker: initialize connection and buffers for SMC-D */
smc_listen_ism_init(struct smc_sock * new_smc,struct smc_init_info * ini)2069 static int smc_listen_ism_init(struct smc_sock *new_smc,
2070 			       struct smc_init_info *ini)
2071 {
2072 	int rc;
2073 
2074 	rc = smc_conn_create(new_smc, ini);
2075 	if (rc)
2076 		return rc;
2077 
2078 	/* Create send and receive buffers */
2079 	rc = smc_buf_create(new_smc, true);
2080 	if (rc) {
2081 		smc_conn_abort(new_smc, ini->first_contact_local);
2082 		return (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB :
2083 					 SMC_CLC_DECL_MEM;
2084 	}
2085 
2086 	return 0;
2087 }
2088 
smc_is_already_selected(struct smcd_dev * smcd,struct smc_init_info * ini,int matches)2089 static bool smc_is_already_selected(struct smcd_dev *smcd,
2090 				    struct smc_init_info *ini,
2091 				    int matches)
2092 {
2093 	int i;
2094 
2095 	for (i = 0; i < matches; i++)
2096 		if (smcd == ini->ism_dev[i])
2097 			return true;
2098 
2099 	return false;
2100 }
2101 
2102 /* check for ISM devices matching proposed ISM devices */
smc_check_ism_v2_match(struct smc_init_info * ini,u16 proposed_chid,struct smcd_gid * proposed_gid,unsigned int * matches)2103 static void smc_check_ism_v2_match(struct smc_init_info *ini,
2104 				   u16 proposed_chid,
2105 				   struct smcd_gid *proposed_gid,
2106 				   unsigned int *matches)
2107 {
2108 	struct smcd_dev *smcd;
2109 
2110 	list_for_each_entry(smcd, &smcd_dev_list.list, list) {
2111 		if (smcd->going_away)
2112 			continue;
2113 		if (smc_is_already_selected(smcd, ini, *matches))
2114 			continue;
2115 		if (smc_ism_get_chid(smcd) == proposed_chid &&
2116 		    !smc_ism_cantalk(proposed_gid, ISM_RESERVED_VLANID, smcd)) {
2117 			ini->ism_peer_gid[*matches].gid = proposed_gid->gid;
2118 			if (__smc_ism_is_virtual(proposed_chid))
2119 				ini->ism_peer_gid[*matches].gid_ext =
2120 							proposed_gid->gid_ext;
2121 				/* non-virtual ISM's peer gid_ext remains 0. */
2122 			ini->ism_dev[*matches] = smcd;
2123 			(*matches)++;
2124 			break;
2125 		}
2126 	}
2127 }
2128 
smc_find_ism_store_rc(u32 rc,struct smc_init_info * ini)2129 static void smc_find_ism_store_rc(u32 rc, struct smc_init_info *ini)
2130 {
2131 	if (!ini->rc)
2132 		ini->rc = rc;
2133 }
2134 
smc_find_ism_v2_device_serv(struct smc_sock * new_smc,struct smc_clc_msg_proposal * pclc,struct smc_init_info * ini)2135 static void smc_find_ism_v2_device_serv(struct smc_sock *new_smc,
2136 					struct smc_clc_msg_proposal *pclc,
2137 					struct smc_init_info *ini)
2138 {
2139 	struct smc_clc_smcd_v2_extension *smcd_v2_ext;
2140 	struct smc_clc_v2_extension *smc_v2_ext;
2141 	struct smc_clc_msg_smcd *pclc_smcd;
2142 	unsigned int matches = 0;
2143 	struct smcd_gid smcd_gid;
2144 	u8 smcd_version;
2145 	u8 *eid = NULL;
2146 	int i, rc;
2147 	u16 chid;
2148 
2149 	if (!(ini->smcd_version & SMC_V2) || !smcd_indicated(ini->smc_type_v2))
2150 		goto not_found;
2151 
2152 	pclc_smcd = smc_get_clc_msg_smcd(pclc);
2153 	smc_v2_ext = smc_get_clc_v2_ext(pclc);
2154 	smcd_v2_ext = smc_get_clc_smcd_v2_ext(smc_v2_ext);
2155 
2156 	mutex_lock(&smcd_dev_list.mutex);
2157 	if (pclc_smcd->ism.chid) {
2158 		/* check for ISM device matching proposed native ISM device */
2159 		smcd_gid.gid = ntohll(pclc_smcd->ism.gid);
2160 		smcd_gid.gid_ext = 0;
2161 		smc_check_ism_v2_match(ini, ntohs(pclc_smcd->ism.chid),
2162 				       &smcd_gid, &matches);
2163 	}
2164 	for (i = 0; i < smc_v2_ext->hdr.ism_gid_cnt; i++) {
2165 		/* check for ISM devices matching proposed non-native ISM
2166 		 * devices
2167 		 */
2168 		smcd_gid.gid = ntohll(smcd_v2_ext->gidchid[i].gid);
2169 		smcd_gid.gid_ext = 0;
2170 		chid = ntohs(smcd_v2_ext->gidchid[i].chid);
2171 		if (__smc_ism_is_virtual(chid)) {
2172 			if ((i + 1) == smc_v2_ext->hdr.ism_gid_cnt ||
2173 			    chid != ntohs(smcd_v2_ext->gidchid[i + 1].chid))
2174 				/* each virtual ISM device takes two GID-CHID
2175 				 * entries and CHID of the second entry repeats
2176 				 * that of the first entry.
2177 				 *
2178 				 * So check if the next GID-CHID entry exists
2179 				 * and both two entries' CHIDs are the same.
2180 				 */
2181 				continue;
2182 			smcd_gid.gid_ext =
2183 				ntohll(smcd_v2_ext->gidchid[++i].gid);
2184 		}
2185 		smc_check_ism_v2_match(ini, chid, &smcd_gid, &matches);
2186 	}
2187 	mutex_unlock(&smcd_dev_list.mutex);
2188 
2189 	if (!ini->ism_dev[0]) {
2190 		smc_find_ism_store_rc(SMC_CLC_DECL_NOSMCD2DEV, ini);
2191 		goto not_found;
2192 	}
2193 
2194 	smc_ism_get_system_eid(&eid);
2195 	if (!smc_clc_match_eid(ini->negotiated_eid, smc_v2_ext,
2196 			       smcd_v2_ext->system_eid, eid))
2197 		goto not_found;
2198 
2199 	/* separate - outside the smcd_dev_list.lock */
2200 	smcd_version = ini->smcd_version;
2201 	for (i = 0; i < matches; i++) {
2202 		ini->smcd_version = SMC_V2;
2203 		ini->is_smcd = true;
2204 		ini->ism_selected = i;
2205 		rc = smc_listen_ism_init(new_smc, ini);
2206 		if (rc) {
2207 			smc_find_ism_store_rc(rc, ini);
2208 			/* try next active ISM device */
2209 			continue;
2210 		}
2211 		return; /* matching and usable V2 ISM device found */
2212 	}
2213 	/* no V2 ISM device could be initialized */
2214 	ini->smcd_version = smcd_version;	/* restore original value */
2215 	ini->negotiated_eid[0] = 0;
2216 
2217 not_found:
2218 	ini->smcd_version &= ~SMC_V2;
2219 	ini->ism_dev[0] = NULL;
2220 	ini->is_smcd = false;
2221 }
2222 
smc_find_ism_v1_device_serv(struct smc_sock * new_smc,struct smc_clc_msg_proposal * pclc,struct smc_init_info * ini)2223 static void smc_find_ism_v1_device_serv(struct smc_sock *new_smc,
2224 					struct smc_clc_msg_proposal *pclc,
2225 					struct smc_init_info *ini)
2226 {
2227 	struct smc_clc_msg_smcd *pclc_smcd = smc_get_clc_msg_smcd(pclc);
2228 	int rc = 0;
2229 
2230 	/* check if ISM V1 is available */
2231 	if (!(ini->smcd_version & SMC_V1) || !smcd_indicated(ini->smc_type_v1))
2232 		goto not_found;
2233 	ini->is_smcd = true; /* prepare ISM check */
2234 	ini->ism_peer_gid[0].gid = ntohll(pclc_smcd->ism.gid);
2235 	ini->ism_peer_gid[0].gid_ext = 0;
2236 	rc = smc_find_ism_device(new_smc, ini);
2237 	if (rc)
2238 		goto not_found;
2239 	ini->ism_selected = 0;
2240 	rc = smc_listen_ism_init(new_smc, ini);
2241 	if (!rc)
2242 		return;		/* V1 ISM device found */
2243 
2244 not_found:
2245 	smc_find_ism_store_rc(rc, ini);
2246 	ini->smcd_version &= ~SMC_V1;
2247 	ini->ism_dev[0] = NULL;
2248 	ini->is_smcd = false;
2249 }
2250 
2251 /* listen worker: register buffers */
smc_listen_rdma_reg(struct smc_sock * new_smc,bool local_first)2252 static int smc_listen_rdma_reg(struct smc_sock *new_smc, bool local_first)
2253 {
2254 	struct smc_connection *conn = &new_smc->conn;
2255 
2256 	if (!local_first) {
2257 		/* reg sendbufs if they were vzalloced */
2258 		if (conn->sndbuf_desc->is_vm) {
2259 			if (smcr_lgr_reg_sndbufs(conn->lnk,
2260 						 conn->sndbuf_desc))
2261 				return SMC_CLC_DECL_ERR_REGBUF;
2262 		}
2263 		if (smcr_lgr_reg_rmbs(conn->lnk, conn->rmb_desc))
2264 			return SMC_CLC_DECL_ERR_REGBUF;
2265 	}
2266 
2267 	return 0;
2268 }
2269 
smc_find_rdma_v2_device_serv(struct smc_sock * new_smc,struct smc_clc_msg_proposal * pclc,struct smc_init_info * ini)2270 static void smc_find_rdma_v2_device_serv(struct smc_sock *new_smc,
2271 					 struct smc_clc_msg_proposal *pclc,
2272 					 struct smc_init_info *ini)
2273 {
2274 	struct smc_clc_v2_extension *smc_v2_ext;
2275 	u8 smcr_version;
2276 	int rc;
2277 
2278 	if (!(ini->smcr_version & SMC_V2) || !smcr_indicated(ini->smc_type_v2))
2279 		goto not_found;
2280 
2281 	smc_v2_ext = smc_get_clc_v2_ext(pclc);
2282 	if (!smc_clc_match_eid(ini->negotiated_eid, smc_v2_ext, NULL, NULL))
2283 		goto not_found;
2284 
2285 	/* prepare RDMA check */
2286 	memcpy(ini->peer_systemid, pclc->lcl.id_for_peer, SMC_SYSTEMID_LEN);
2287 	memcpy(ini->peer_gid, smc_v2_ext->roce, SMC_GID_SIZE);
2288 	memcpy(ini->peer_mac, pclc->lcl.mac, ETH_ALEN);
2289 	ini->check_smcrv2 = true;
2290 	ini->smcrv2.clc_sk = new_smc->clcsock->sk;
2291 	ini->smcrv2.saddr = new_smc->clcsock->sk->sk_rcv_saddr;
2292 	ini->smcrv2.daddr = smc_ib_gid_to_ipv4(smc_v2_ext->roce);
2293 	rc = smc_find_rdma_device(new_smc, ini);
2294 	if (rc) {
2295 		smc_find_ism_store_rc(rc, ini);
2296 		goto not_found;
2297 	}
2298 	if (!ini->smcrv2.uses_gateway)
2299 		memcpy(ini->smcrv2.nexthop_mac, pclc->lcl.mac, ETH_ALEN);
2300 
2301 	smcr_version = ini->smcr_version;
2302 	ini->smcr_version = SMC_V2;
2303 	rc = smc_listen_rdma_init(new_smc, ini);
2304 	if (!rc) {
2305 		rc = smc_listen_rdma_reg(new_smc, ini->first_contact_local);
2306 		if (rc)
2307 			smc_conn_abort(new_smc, ini->first_contact_local);
2308 	}
2309 	if (!rc)
2310 		return;
2311 	ini->smcr_version = smcr_version;
2312 	smc_find_ism_store_rc(rc, ini);
2313 
2314 not_found:
2315 	ini->smcr_version &= ~SMC_V2;
2316 	ini->smcrv2.ib_dev_v2 = NULL;
2317 	ini->check_smcrv2 = false;
2318 }
2319 
smc_find_rdma_v1_device_serv(struct smc_sock * new_smc,struct smc_clc_msg_proposal * pclc,struct smc_init_info * ini)2320 static int smc_find_rdma_v1_device_serv(struct smc_sock *new_smc,
2321 					struct smc_clc_msg_proposal *pclc,
2322 					struct smc_init_info *ini)
2323 {
2324 	int rc;
2325 
2326 	if (!(ini->smcr_version & SMC_V1) || !smcr_indicated(ini->smc_type_v1))
2327 		return SMC_CLC_DECL_NOSMCDEV;
2328 
2329 	/* prepare RDMA check */
2330 	memcpy(ini->peer_systemid, pclc->lcl.id_for_peer, SMC_SYSTEMID_LEN);
2331 	memcpy(ini->peer_gid, pclc->lcl.gid, SMC_GID_SIZE);
2332 	memcpy(ini->peer_mac, pclc->lcl.mac, ETH_ALEN);
2333 	rc = smc_find_rdma_device(new_smc, ini);
2334 	if (rc) {
2335 		/* no RDMA device found */
2336 		return SMC_CLC_DECL_NOSMCDEV;
2337 	}
2338 	rc = smc_listen_rdma_init(new_smc, ini);
2339 	if (rc)
2340 		return rc;
2341 	return smc_listen_rdma_reg(new_smc, ini->first_contact_local);
2342 }
2343 
2344 /* determine the local device matching to proposal */
smc_listen_find_device(struct smc_sock * new_smc,struct smc_clc_msg_proposal * pclc,struct smc_init_info * ini)2345 static int smc_listen_find_device(struct smc_sock *new_smc,
2346 				  struct smc_clc_msg_proposal *pclc,
2347 				  struct smc_init_info *ini)
2348 {
2349 	int prfx_rc;
2350 
2351 	/* check for ISM device matching V2 proposed device */
2352 	smc_find_ism_v2_device_serv(new_smc, pclc, ini);
2353 	if (ini->ism_dev[0])
2354 		return 0;
2355 
2356 	/* check for matching IP prefix and subnet length (V1) */
2357 	prfx_rc = smc_listen_prfx_check(new_smc, pclc);
2358 	if (prfx_rc)
2359 		smc_find_ism_store_rc(prfx_rc, ini);
2360 
2361 	/* get vlan id from IP device */
2362 	if (smc_vlan_by_tcpsk(new_smc->clcsock, ini))
2363 		return ini->rc ?: SMC_CLC_DECL_GETVLANERR;
2364 
2365 	/* check for ISM device matching V1 proposed device */
2366 	if (!prfx_rc)
2367 		smc_find_ism_v1_device_serv(new_smc, pclc, ini);
2368 	if (ini->ism_dev[0])
2369 		return 0;
2370 
2371 	if (!smcr_indicated(pclc->hdr.typev1) &&
2372 	    !smcr_indicated(pclc->hdr.typev2))
2373 		/* skip RDMA and decline */
2374 		return ini->rc ?: SMC_CLC_DECL_NOSMCDDEV;
2375 
2376 	/* check if RDMA V2 is available */
2377 	smc_find_rdma_v2_device_serv(new_smc, pclc, ini);
2378 	if (ini->smcrv2.ib_dev_v2)
2379 		return 0;
2380 
2381 	/* check if RDMA V1 is available */
2382 	if (!prfx_rc) {
2383 		int rc;
2384 
2385 		rc = smc_find_rdma_v1_device_serv(new_smc, pclc, ini);
2386 		smc_find_ism_store_rc(rc, ini);
2387 		return (!rc) ? 0 : ini->rc;
2388 	}
2389 	return prfx_rc;
2390 }
2391 
2392 /* listen worker: finish RDMA setup */
smc_listen_rdma_finish(struct smc_sock * new_smc,struct smc_clc_msg_accept_confirm * cclc,bool local_first,struct smc_init_info * ini)2393 static int smc_listen_rdma_finish(struct smc_sock *new_smc,
2394 				  struct smc_clc_msg_accept_confirm *cclc,
2395 				  bool local_first,
2396 				  struct smc_init_info *ini)
2397 {
2398 	struct smc_link *link = new_smc->conn.lnk;
2399 	int reason_code = 0;
2400 
2401 	if (local_first)
2402 		smc_link_save_peer_info(link, cclc, ini);
2403 
2404 	if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc))
2405 		return SMC_CLC_DECL_ERR_RTOK;
2406 
2407 	if (local_first) {
2408 		if (smc_ib_ready_link(link))
2409 			return SMC_CLC_DECL_ERR_RDYLNK;
2410 		/* QP confirmation over RoCE fabric */
2411 		smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
2412 		reason_code = smcr_serv_conf_first_link(new_smc);
2413 		smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
2414 	}
2415 	return reason_code;
2416 }
2417 
2418 /* setup for connection of server */
smc_listen_work(struct work_struct * work)2419 static void smc_listen_work(struct work_struct *work)
2420 {
2421 	struct smc_sock *new_smc = container_of(work, struct smc_sock,
2422 						smc_listen_work);
2423 	struct socket *newclcsock = new_smc->clcsock;
2424 	struct smc_clc_msg_accept_confirm *cclc;
2425 	struct smc_clc_msg_proposal_area *buf;
2426 	struct smc_clc_msg_proposal *pclc;
2427 	struct smc_init_info *ini = NULL;
2428 	u8 proposal_version = SMC_V1;
2429 	u8 accept_version;
2430 	int rc = 0;
2431 
2432 	lock_sock(&new_smc->sk); /* release in smc_listen_out() */
2433 	if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
2434 		return smc_listen_out_err(new_smc);
2435 
2436 	if (new_smc->use_fallback) {
2437 		smc_listen_out_connected(new_smc);
2438 		return;
2439 	}
2440 
2441 	/* check if peer is smc capable */
2442 	if (!tcp_sk(newclcsock->sk)->syn_smc) {
2443 		rc = smc_switch_to_fallback(new_smc, SMC_CLC_DECL_PEERNOSMC);
2444 		if (rc)
2445 			smc_listen_out_err(new_smc);
2446 		else
2447 			smc_listen_out_connected(new_smc);
2448 		return;
2449 	}
2450 
2451 	/* do inband token exchange -
2452 	 * wait for and receive SMC Proposal CLC message
2453 	 */
2454 	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
2455 	if (!buf) {
2456 		rc = SMC_CLC_DECL_MEM;
2457 		goto out_decl;
2458 	}
2459 	pclc = (struct smc_clc_msg_proposal *)buf;
2460 	rc = smc_clc_wait_msg(new_smc, pclc, sizeof(*buf),
2461 			      SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
2462 	if (rc)
2463 		goto out_decl;
2464 
2465 	if (pclc->hdr.version > SMC_V1)
2466 		proposal_version = SMC_V2;
2467 
2468 	/* IPSec connections opt out of SMC optimizations */
2469 	if (using_ipsec(new_smc)) {
2470 		rc = SMC_CLC_DECL_IPSEC;
2471 		goto out_decl;
2472 	}
2473 
2474 	ini = kzalloc(sizeof(*ini), GFP_KERNEL);
2475 	if (!ini) {
2476 		rc = SMC_CLC_DECL_MEM;
2477 		goto out_decl;
2478 	}
2479 
2480 	/* initial version checking */
2481 	rc = smc_listen_v2_check(new_smc, pclc, ini);
2482 	if (rc)
2483 		goto out_decl;
2484 
2485 	rc = smc_clc_srv_v2x_features_validate(pclc, ini);
2486 	if (rc)
2487 		goto out_decl;
2488 
2489 	mutex_lock(&smc_server_lgr_pending);
2490 	smc_rx_init(new_smc);
2491 	smc_tx_init(new_smc);
2492 
2493 	/* determine ISM or RoCE device used for connection */
2494 	rc = smc_listen_find_device(new_smc, pclc, ini);
2495 	if (rc)
2496 		goto out_unlock;
2497 
2498 	/* send SMC Accept CLC message */
2499 	accept_version = ini->is_smcd ? ini->smcd_version : ini->smcr_version;
2500 	rc = smc_clc_send_accept(new_smc, ini->first_contact_local,
2501 				 accept_version, ini->negotiated_eid, ini);
2502 	if (rc)
2503 		goto out_unlock;
2504 
2505 	/* SMC-D does not need this lock any more */
2506 	if (ini->is_smcd)
2507 		mutex_unlock(&smc_server_lgr_pending);
2508 
2509 	/* receive SMC Confirm CLC message */
2510 	memset(buf, 0, sizeof(*buf));
2511 	cclc = (struct smc_clc_msg_accept_confirm *)buf;
2512 	rc = smc_clc_wait_msg(new_smc, cclc, sizeof(*buf),
2513 			      SMC_CLC_CONFIRM, CLC_WAIT_TIME);
2514 	if (rc) {
2515 		if (!ini->is_smcd)
2516 			goto out_unlock;
2517 		goto out_decl;
2518 	}
2519 
2520 	rc = smc_clc_v2x_features_confirm_check(cclc, ini);
2521 	if (rc) {
2522 		if (!ini->is_smcd)
2523 			goto out_unlock;
2524 		goto out_decl;
2525 	}
2526 
2527 	/* fce smc release version is needed in smc_listen_rdma_finish,
2528 	 * so save fce info here.
2529 	 */
2530 	smc_conn_save_peer_info_fce(new_smc, cclc);
2531 
2532 	/* finish worker */
2533 	if (!ini->is_smcd) {
2534 		rc = smc_listen_rdma_finish(new_smc, cclc,
2535 					    ini->first_contact_local, ini);
2536 		if (rc)
2537 			goto out_unlock;
2538 		mutex_unlock(&smc_server_lgr_pending);
2539 	}
2540 	smc_conn_save_peer_info(new_smc, cclc);
2541 
2542 	if (ini->is_smcd &&
2543 	    smc_ism_support_dmb_nocopy(new_smc->conn.lgr->smcd)) {
2544 		rc = smcd_buf_attach(new_smc);
2545 		if (rc)
2546 			goto out_decl;
2547 	}
2548 
2549 	smc_listen_out_connected(new_smc);
2550 	SMC_STAT_SERV_SUCC_INC(sock_net(newclcsock->sk), ini);
2551 	goto out_free;
2552 
2553 out_unlock:
2554 	mutex_unlock(&smc_server_lgr_pending);
2555 out_decl:
2556 	smc_listen_decline(new_smc, rc, ini ? ini->first_contact_local : 0,
2557 			   proposal_version);
2558 out_free:
2559 	kfree(ini);
2560 	kfree(buf);
2561 }
2562 
smc_tcp_listen_work(struct work_struct * work)2563 static void smc_tcp_listen_work(struct work_struct *work)
2564 {
2565 	struct smc_sock *lsmc = container_of(work, struct smc_sock,
2566 					     tcp_listen_work);
2567 	struct sock *lsk = &lsmc->sk;
2568 	struct smc_sock *new_smc;
2569 	int rc = 0;
2570 
2571 	lock_sock(lsk);
2572 	while (lsk->sk_state == SMC_LISTEN) {
2573 		rc = smc_clcsock_accept(lsmc, &new_smc);
2574 		if (rc) /* clcsock accept queue empty or error */
2575 			goto out;
2576 		if (!new_smc)
2577 			continue;
2578 
2579 		if (tcp_sk(new_smc->clcsock->sk)->syn_smc)
2580 			atomic_inc(&lsmc->queued_smc_hs);
2581 
2582 		new_smc->listen_smc = lsmc;
2583 		new_smc->use_fallback = lsmc->use_fallback;
2584 		new_smc->fallback_rsn = lsmc->fallback_rsn;
2585 		sock_hold(lsk); /* sock_put in smc_listen_work */
2586 		INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
2587 		smc_copy_sock_settings_to_smc(new_smc);
2588 		sock_hold(&new_smc->sk); /* sock_put in passive closing */
2589 		if (!queue_work(smc_hs_wq, &new_smc->smc_listen_work))
2590 			sock_put(&new_smc->sk);
2591 	}
2592 
2593 out:
2594 	release_sock(lsk);
2595 	sock_put(&lsmc->sk); /* sock_hold in smc_clcsock_data_ready() */
2596 }
2597 
smc_clcsock_data_ready(struct sock * listen_clcsock)2598 static void smc_clcsock_data_ready(struct sock *listen_clcsock)
2599 {
2600 	struct smc_sock *lsmc;
2601 
2602 	read_lock_bh(&listen_clcsock->sk_callback_lock);
2603 	lsmc = smc_clcsock_user_data(listen_clcsock);
2604 	if (!lsmc)
2605 		goto out;
2606 	lsmc->clcsk_data_ready(listen_clcsock);
2607 	if (lsmc->sk.sk_state == SMC_LISTEN) {
2608 		sock_hold(&lsmc->sk); /* sock_put in smc_tcp_listen_work() */
2609 		if (!queue_work(smc_tcp_ls_wq, &lsmc->tcp_listen_work))
2610 			sock_put(&lsmc->sk);
2611 	}
2612 out:
2613 	read_unlock_bh(&listen_clcsock->sk_callback_lock);
2614 }
2615 
smc_listen(struct socket * sock,int backlog)2616 static int smc_listen(struct socket *sock, int backlog)
2617 {
2618 	struct sock *sk = sock->sk;
2619 	struct smc_sock *smc;
2620 	int rc;
2621 
2622 	smc = smc_sk(sk);
2623 	lock_sock(sk);
2624 
2625 	rc = -EINVAL;
2626 	if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
2627 	    smc->connect_nonblock || sock->state != SS_UNCONNECTED)
2628 		goto out;
2629 
2630 	rc = 0;
2631 	if (sk->sk_state == SMC_LISTEN) {
2632 		sk->sk_max_ack_backlog = backlog;
2633 		goto out;
2634 	}
2635 	/* some socket options are handled in core, so we could not apply
2636 	 * them to the clc socket -- copy smc socket options to clc socket
2637 	 */
2638 	smc_copy_sock_settings_to_clc(smc);
2639 	if (!smc->use_fallback)
2640 		tcp_sk(smc->clcsock->sk)->syn_smc = 1;
2641 
2642 	/* save original sk_data_ready function and establish
2643 	 * smc-specific sk_data_ready function
2644 	 */
2645 	write_lock_bh(&smc->clcsock->sk->sk_callback_lock);
2646 	smc->clcsock->sk->sk_user_data =
2647 		(void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY);
2648 	smc_clcsock_replace_cb(&smc->clcsock->sk->sk_data_ready,
2649 			       smc_clcsock_data_ready, &smc->clcsk_data_ready);
2650 	write_unlock_bh(&smc->clcsock->sk->sk_callback_lock);
2651 
2652 	/* save original ops */
2653 	smc->ori_af_ops = inet_csk(smc->clcsock->sk)->icsk_af_ops;
2654 
2655 	smc->af_ops = *smc->ori_af_ops;
2656 	smc->af_ops.syn_recv_sock = smc_tcp_syn_recv_sock;
2657 
2658 	inet_csk(smc->clcsock->sk)->icsk_af_ops = &smc->af_ops;
2659 
2660 	if (smc->limit_smc_hs)
2661 		tcp_sk(smc->clcsock->sk)->smc_hs_congested = smc_hs_congested;
2662 
2663 	rc = kernel_listen(smc->clcsock, backlog);
2664 	if (rc) {
2665 		write_lock_bh(&smc->clcsock->sk->sk_callback_lock);
2666 		smc_clcsock_restore_cb(&smc->clcsock->sk->sk_data_ready,
2667 				       &smc->clcsk_data_ready);
2668 		smc->clcsock->sk->sk_user_data = NULL;
2669 		write_unlock_bh(&smc->clcsock->sk->sk_callback_lock);
2670 		goto out;
2671 	}
2672 	sk->sk_max_ack_backlog = backlog;
2673 	sk->sk_ack_backlog = 0;
2674 	sk->sk_state = SMC_LISTEN;
2675 
2676 out:
2677 	release_sock(sk);
2678 	return rc;
2679 }
2680 
smc_accept(struct socket * sock,struct socket * new_sock,int flags,bool kern)2681 static int smc_accept(struct socket *sock, struct socket *new_sock,
2682 		      int flags, bool kern)
2683 {
2684 	struct sock *sk = sock->sk, *nsk;
2685 	DECLARE_WAITQUEUE(wait, current);
2686 	struct smc_sock *lsmc;
2687 	long timeo;
2688 	int rc = 0;
2689 
2690 	lsmc = smc_sk(sk);
2691 	sock_hold(sk); /* sock_put below */
2692 	lock_sock(sk);
2693 
2694 	if (lsmc->sk.sk_state != SMC_LISTEN) {
2695 		rc = -EINVAL;
2696 		release_sock(sk);
2697 		goto out;
2698 	}
2699 
2700 	/* Wait for an incoming connection */
2701 	timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2702 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
2703 	while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
2704 		set_current_state(TASK_INTERRUPTIBLE);
2705 		if (!timeo) {
2706 			rc = -EAGAIN;
2707 			break;
2708 		}
2709 		release_sock(sk);
2710 		timeo = schedule_timeout(timeo);
2711 		/* wakeup by sk_data_ready in smc_listen_work() */
2712 		sched_annotate_sleep();
2713 		lock_sock(sk);
2714 		if (signal_pending(current)) {
2715 			rc = sock_intr_errno(timeo);
2716 			break;
2717 		}
2718 	}
2719 	set_current_state(TASK_RUNNING);
2720 	remove_wait_queue(sk_sleep(sk), &wait);
2721 
2722 	if (!rc)
2723 		rc = sock_error(nsk);
2724 	release_sock(sk);
2725 	if (rc)
2726 		goto out;
2727 
2728 	if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
2729 		/* wait till data arrives on the socket */
2730 		timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
2731 								MSEC_PER_SEC);
2732 		if (smc_sk(nsk)->use_fallback) {
2733 			struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
2734 
2735 			lock_sock(clcsk);
2736 			if (skb_queue_empty(&clcsk->sk_receive_queue))
2737 				sk_wait_data(clcsk, &timeo, NULL);
2738 			release_sock(clcsk);
2739 		} else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
2740 			lock_sock(nsk);
2741 			smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
2742 			release_sock(nsk);
2743 		}
2744 	}
2745 
2746 out:
2747 	sock_put(sk); /* sock_hold above */
2748 	return rc;
2749 }
2750 
smc_getname(struct socket * sock,struct sockaddr * addr,int peer)2751 static int smc_getname(struct socket *sock, struct sockaddr *addr,
2752 		       int peer)
2753 {
2754 	struct smc_sock *smc;
2755 
2756 	if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
2757 	    (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
2758 		return -ENOTCONN;
2759 
2760 	smc = smc_sk(sock->sk);
2761 
2762 	return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
2763 }
2764 
smc_sendmsg(struct socket * sock,struct msghdr * msg,size_t len)2765 static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
2766 {
2767 	struct sock *sk = sock->sk;
2768 	struct smc_sock *smc;
2769 	int rc;
2770 
2771 	smc = smc_sk(sk);
2772 	lock_sock(sk);
2773 
2774 	/* SMC does not support connect with fastopen */
2775 	if (msg->msg_flags & MSG_FASTOPEN) {
2776 		/* not connected yet, fallback */
2777 		if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
2778 			rc = smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP);
2779 			if (rc)
2780 				goto out;
2781 		} else {
2782 			rc = -EINVAL;
2783 			goto out;
2784 		}
2785 	} else if ((sk->sk_state != SMC_ACTIVE) &&
2786 		   (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2787 		   (sk->sk_state != SMC_INIT)) {
2788 		rc = -EPIPE;
2789 		goto out;
2790 	}
2791 
2792 	if (smc->use_fallback) {
2793 		rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
2794 	} else {
2795 		rc = smc_tx_sendmsg(smc, msg, len);
2796 		SMC_STAT_TX_PAYLOAD(smc, len, rc);
2797 	}
2798 out:
2799 	release_sock(sk);
2800 	return rc;
2801 }
2802 
smc_recvmsg(struct socket * sock,struct msghdr * msg,size_t len,int flags)2803 static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
2804 		       int flags)
2805 {
2806 	struct sock *sk = sock->sk;
2807 	struct smc_sock *smc;
2808 	int rc = -ENOTCONN;
2809 
2810 	smc = smc_sk(sk);
2811 	lock_sock(sk);
2812 	if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2813 		/* socket was connected before, no more data to read */
2814 		rc = 0;
2815 		goto out;
2816 	}
2817 	if ((sk->sk_state == SMC_INIT) ||
2818 	    (sk->sk_state == SMC_LISTEN) ||
2819 	    (sk->sk_state == SMC_CLOSED))
2820 		goto out;
2821 
2822 	if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2823 		rc = 0;
2824 		goto out;
2825 	}
2826 
2827 	if (smc->use_fallback) {
2828 		rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
2829 	} else {
2830 		msg->msg_namelen = 0;
2831 		rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
2832 		SMC_STAT_RX_PAYLOAD(smc, rc, rc);
2833 	}
2834 
2835 out:
2836 	release_sock(sk);
2837 	return rc;
2838 }
2839 
smc_accept_poll(struct sock * parent)2840 static __poll_t smc_accept_poll(struct sock *parent)
2841 {
2842 	struct smc_sock *isk = smc_sk(parent);
2843 	__poll_t mask = 0;
2844 
2845 	spin_lock(&isk->accept_q_lock);
2846 	if (!list_empty(&isk->accept_q))
2847 		mask = EPOLLIN | EPOLLRDNORM;
2848 	spin_unlock(&isk->accept_q_lock);
2849 
2850 	return mask;
2851 }
2852 
smc_poll(struct file * file,struct socket * sock,poll_table * wait)2853 static __poll_t smc_poll(struct file *file, struct socket *sock,
2854 			     poll_table *wait)
2855 {
2856 	struct sock *sk = sock->sk;
2857 	struct smc_sock *smc;
2858 	__poll_t mask = 0;
2859 
2860 	if (!sk)
2861 		return EPOLLNVAL;
2862 
2863 	smc = smc_sk(sock->sk);
2864 	if (smc->use_fallback) {
2865 		/* delegate to CLC child sock */
2866 		mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
2867 		sk->sk_err = smc->clcsock->sk->sk_err;
2868 	} else {
2869 		if (sk->sk_state != SMC_CLOSED)
2870 			sock_poll_wait(file, sock, wait);
2871 		if (sk->sk_err)
2872 			mask |= EPOLLERR;
2873 		if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
2874 		    (sk->sk_state == SMC_CLOSED))
2875 			mask |= EPOLLHUP;
2876 		if (sk->sk_state == SMC_LISTEN) {
2877 			/* woken up by sk_data_ready in smc_listen_work() */
2878 			mask |= smc_accept_poll(sk);
2879 		} else if (smc->use_fallback) { /* as result of connect_work()*/
2880 			mask |= smc->clcsock->ops->poll(file, smc->clcsock,
2881 							   wait);
2882 			sk->sk_err = smc->clcsock->sk->sk_err;
2883 		} else {
2884 			if ((sk->sk_state != SMC_INIT &&
2885 			     atomic_read(&smc->conn.sndbuf_space)) ||
2886 			    sk->sk_shutdown & SEND_SHUTDOWN) {
2887 				mask |= EPOLLOUT | EPOLLWRNORM;
2888 			} else {
2889 				sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
2890 				set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
2891 			}
2892 			if (atomic_read(&smc->conn.bytes_to_rcv))
2893 				mask |= EPOLLIN | EPOLLRDNORM;
2894 			if (sk->sk_shutdown & RCV_SHUTDOWN)
2895 				mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
2896 			if (sk->sk_state == SMC_APPCLOSEWAIT1)
2897 				mask |= EPOLLIN;
2898 			if (smc->conn.urg_state == SMC_URG_VALID)
2899 				mask |= EPOLLPRI;
2900 		}
2901 	}
2902 
2903 	return mask;
2904 }
2905 
smc_shutdown(struct socket * sock,int how)2906 static int smc_shutdown(struct socket *sock, int how)
2907 {
2908 	struct sock *sk = sock->sk;
2909 	bool do_shutdown = true;
2910 	struct smc_sock *smc;
2911 	int rc = -EINVAL;
2912 	int old_state;
2913 	int rc1 = 0;
2914 
2915 	smc = smc_sk(sk);
2916 
2917 	if ((how < SHUT_RD) || (how > SHUT_RDWR))
2918 		return rc;
2919 
2920 	lock_sock(sk);
2921 
2922 	if (sock->state == SS_CONNECTING) {
2923 		if (sk->sk_state == SMC_ACTIVE)
2924 			sock->state = SS_CONNECTED;
2925 		else if (sk->sk_state == SMC_PEERCLOSEWAIT1 ||
2926 			 sk->sk_state == SMC_PEERCLOSEWAIT2 ||
2927 			 sk->sk_state == SMC_APPCLOSEWAIT1 ||
2928 			 sk->sk_state == SMC_APPCLOSEWAIT2 ||
2929 			 sk->sk_state == SMC_APPFINCLOSEWAIT)
2930 			sock->state = SS_DISCONNECTING;
2931 	}
2932 
2933 	rc = -ENOTCONN;
2934 	if ((sk->sk_state != SMC_ACTIVE) &&
2935 	    (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
2936 	    (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
2937 	    (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2938 	    (sk->sk_state != SMC_APPCLOSEWAIT2) &&
2939 	    (sk->sk_state != SMC_APPFINCLOSEWAIT))
2940 		goto out;
2941 	if (smc->use_fallback) {
2942 		rc = kernel_sock_shutdown(smc->clcsock, how);
2943 		sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
2944 		if (sk->sk_shutdown == SHUTDOWN_MASK) {
2945 			sk->sk_state = SMC_CLOSED;
2946 			sk->sk_socket->state = SS_UNCONNECTED;
2947 			sock_put(sk);
2948 		}
2949 		goto out;
2950 	}
2951 	switch (how) {
2952 	case SHUT_RDWR:		/* shutdown in both directions */
2953 		old_state = sk->sk_state;
2954 		rc = smc_close_active(smc);
2955 		if (old_state == SMC_ACTIVE &&
2956 		    sk->sk_state == SMC_PEERCLOSEWAIT1)
2957 			do_shutdown = false;
2958 		break;
2959 	case SHUT_WR:
2960 		rc = smc_close_shutdown_write(smc);
2961 		break;
2962 	case SHUT_RD:
2963 		rc = 0;
2964 		/* nothing more to do because peer is not involved */
2965 		break;
2966 	}
2967 	if (do_shutdown && smc->clcsock)
2968 		rc1 = kernel_sock_shutdown(smc->clcsock, how);
2969 	/* map sock_shutdown_cmd constants to sk_shutdown value range */
2970 	sk->sk_shutdown |= how + 1;
2971 
2972 	if (sk->sk_state == SMC_CLOSED)
2973 		sock->state = SS_UNCONNECTED;
2974 	else
2975 		sock->state = SS_DISCONNECTING;
2976 out:
2977 	release_sock(sk);
2978 	return rc ? rc : rc1;
2979 }
2980 
__smc_getsockopt(struct socket * sock,int level,int optname,char __user * optval,int __user * optlen)2981 static int __smc_getsockopt(struct socket *sock, int level, int optname,
2982 			    char __user *optval, int __user *optlen)
2983 {
2984 	struct smc_sock *smc;
2985 	int val, len;
2986 
2987 	smc = smc_sk(sock->sk);
2988 
2989 	if (get_user(len, optlen))
2990 		return -EFAULT;
2991 
2992 	len = min_t(int, len, sizeof(int));
2993 
2994 	if (len < 0)
2995 		return -EINVAL;
2996 
2997 	switch (optname) {
2998 	case SMC_LIMIT_HS:
2999 		val = smc->limit_smc_hs;
3000 		break;
3001 	default:
3002 		return -EOPNOTSUPP;
3003 	}
3004 
3005 	if (put_user(len, optlen))
3006 		return -EFAULT;
3007 	if (copy_to_user(optval, &val, len))
3008 		return -EFAULT;
3009 
3010 	return 0;
3011 }
3012 
__smc_setsockopt(struct socket * sock,int level,int optname,sockptr_t optval,unsigned int optlen)3013 static int __smc_setsockopt(struct socket *sock, int level, int optname,
3014 			    sockptr_t optval, unsigned int optlen)
3015 {
3016 	struct sock *sk = sock->sk;
3017 	struct smc_sock *smc;
3018 	int val, rc;
3019 
3020 	smc = smc_sk(sk);
3021 
3022 	lock_sock(sk);
3023 	switch (optname) {
3024 	case SMC_LIMIT_HS:
3025 		if (optlen < sizeof(int)) {
3026 			rc = -EINVAL;
3027 			break;
3028 		}
3029 		if (copy_from_sockptr(&val, optval, sizeof(int))) {
3030 			rc = -EFAULT;
3031 			break;
3032 		}
3033 
3034 		smc->limit_smc_hs = !!val;
3035 		rc = 0;
3036 		break;
3037 	default:
3038 		rc = -EOPNOTSUPP;
3039 		break;
3040 	}
3041 	release_sock(sk);
3042 
3043 	return rc;
3044 }
3045 
smc_setsockopt(struct socket * sock,int level,int optname,sockptr_t optval,unsigned int optlen)3046 static int smc_setsockopt(struct socket *sock, int level, int optname,
3047 			  sockptr_t optval, unsigned int optlen)
3048 {
3049 	struct sock *sk = sock->sk;
3050 	struct smc_sock *smc;
3051 	int val, rc;
3052 
3053 	if (level == SOL_TCP && optname == TCP_ULP)
3054 		return -EOPNOTSUPP;
3055 	else if (level == SOL_SMC)
3056 		return __smc_setsockopt(sock, level, optname, optval, optlen);
3057 
3058 	smc = smc_sk(sk);
3059 
3060 	/* generic setsockopts reaching us here always apply to the
3061 	 * CLC socket
3062 	 */
3063 	mutex_lock(&smc->clcsock_release_lock);
3064 	if (!smc->clcsock) {
3065 		mutex_unlock(&smc->clcsock_release_lock);
3066 		return -EBADF;
3067 	}
3068 	if (unlikely(!smc->clcsock->ops->setsockopt))
3069 		rc = -EOPNOTSUPP;
3070 	else
3071 		rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
3072 						   optval, optlen);
3073 	if (smc->clcsock->sk->sk_err) {
3074 		sk->sk_err = smc->clcsock->sk->sk_err;
3075 		sk_error_report(sk);
3076 	}
3077 	mutex_unlock(&smc->clcsock_release_lock);
3078 
3079 	if (optlen < sizeof(int))
3080 		return -EINVAL;
3081 	if (copy_from_sockptr(&val, optval, sizeof(int)))
3082 		return -EFAULT;
3083 
3084 	lock_sock(sk);
3085 	if (rc || smc->use_fallback)
3086 		goto out;
3087 	switch (optname) {
3088 	case TCP_FASTOPEN:
3089 	case TCP_FASTOPEN_CONNECT:
3090 	case TCP_FASTOPEN_KEY:
3091 	case TCP_FASTOPEN_NO_COOKIE:
3092 		/* option not supported by SMC */
3093 		if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
3094 			rc = smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP);
3095 		} else {
3096 			rc = -EINVAL;
3097 		}
3098 		break;
3099 	case TCP_NODELAY:
3100 		if (sk->sk_state != SMC_INIT &&
3101 		    sk->sk_state != SMC_LISTEN &&
3102 		    sk->sk_state != SMC_CLOSED) {
3103 			if (val) {
3104 				SMC_STAT_INC(smc, ndly_cnt);
3105 				smc_tx_pending(&smc->conn);
3106 				cancel_delayed_work(&smc->conn.tx_work);
3107 			}
3108 		}
3109 		break;
3110 	case TCP_CORK:
3111 		if (sk->sk_state != SMC_INIT &&
3112 		    sk->sk_state != SMC_LISTEN &&
3113 		    sk->sk_state != SMC_CLOSED) {
3114 			if (!val) {
3115 				SMC_STAT_INC(smc, cork_cnt);
3116 				smc_tx_pending(&smc->conn);
3117 				cancel_delayed_work(&smc->conn.tx_work);
3118 			}
3119 		}
3120 		break;
3121 	case TCP_DEFER_ACCEPT:
3122 		smc->sockopt_defer_accept = val;
3123 		break;
3124 	default:
3125 		break;
3126 	}
3127 out:
3128 	release_sock(sk);
3129 
3130 	return rc;
3131 }
3132 
smc_getsockopt(struct socket * sock,int level,int optname,char __user * optval,int __user * optlen)3133 static int smc_getsockopt(struct socket *sock, int level, int optname,
3134 			  char __user *optval, int __user *optlen)
3135 {
3136 	struct smc_sock *smc;
3137 	int rc;
3138 
3139 	if (level == SOL_SMC)
3140 		return __smc_getsockopt(sock, level, optname, optval, optlen);
3141 
3142 	smc = smc_sk(sock->sk);
3143 	mutex_lock(&smc->clcsock_release_lock);
3144 	if (!smc->clcsock) {
3145 		mutex_unlock(&smc->clcsock_release_lock);
3146 		return -EBADF;
3147 	}
3148 	/* socket options apply to the CLC socket */
3149 	if (unlikely(!smc->clcsock->ops->getsockopt)) {
3150 		mutex_unlock(&smc->clcsock_release_lock);
3151 		return -EOPNOTSUPP;
3152 	}
3153 	rc = smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
3154 					   optval, optlen);
3155 	mutex_unlock(&smc->clcsock_release_lock);
3156 	return rc;
3157 }
3158 
smc_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)3159 static int smc_ioctl(struct socket *sock, unsigned int cmd,
3160 		     unsigned long arg)
3161 {
3162 	union smc_host_cursor cons, urg;
3163 	struct smc_connection *conn;
3164 	struct smc_sock *smc;
3165 	int answ;
3166 
3167 	smc = smc_sk(sock->sk);
3168 	conn = &smc->conn;
3169 	lock_sock(&smc->sk);
3170 	if (smc->use_fallback) {
3171 		if (!smc->clcsock) {
3172 			release_sock(&smc->sk);
3173 			return -EBADF;
3174 		}
3175 		answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
3176 		release_sock(&smc->sk);
3177 		return answ;
3178 	}
3179 	switch (cmd) {
3180 	case SIOCINQ: /* same as FIONREAD */
3181 		if (smc->sk.sk_state == SMC_LISTEN) {
3182 			release_sock(&smc->sk);
3183 			return -EINVAL;
3184 		}
3185 		if (smc->sk.sk_state == SMC_INIT ||
3186 		    smc->sk.sk_state == SMC_CLOSED)
3187 			answ = 0;
3188 		else
3189 			answ = atomic_read(&smc->conn.bytes_to_rcv);
3190 		break;
3191 	case SIOCOUTQ:
3192 		/* output queue size (not send + not acked) */
3193 		if (smc->sk.sk_state == SMC_LISTEN) {
3194 			release_sock(&smc->sk);
3195 			return -EINVAL;
3196 		}
3197 		if (smc->sk.sk_state == SMC_INIT ||
3198 		    smc->sk.sk_state == SMC_CLOSED)
3199 			answ = 0;
3200 		else
3201 			answ = smc->conn.sndbuf_desc->len -
3202 					atomic_read(&smc->conn.sndbuf_space);
3203 		break;
3204 	case SIOCOUTQNSD:
3205 		/* output queue size (not send only) */
3206 		if (smc->sk.sk_state == SMC_LISTEN) {
3207 			release_sock(&smc->sk);
3208 			return -EINVAL;
3209 		}
3210 		if (smc->sk.sk_state == SMC_INIT ||
3211 		    smc->sk.sk_state == SMC_CLOSED)
3212 			answ = 0;
3213 		else
3214 			answ = smc_tx_prepared_sends(&smc->conn);
3215 		break;
3216 	case SIOCATMARK:
3217 		if (smc->sk.sk_state == SMC_LISTEN) {
3218 			release_sock(&smc->sk);
3219 			return -EINVAL;
3220 		}
3221 		if (smc->sk.sk_state == SMC_INIT ||
3222 		    smc->sk.sk_state == SMC_CLOSED) {
3223 			answ = 0;
3224 		} else {
3225 			smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
3226 			smc_curs_copy(&urg, &conn->urg_curs, conn);
3227 			answ = smc_curs_diff(conn->rmb_desc->len,
3228 					     &cons, &urg) == 1;
3229 		}
3230 		break;
3231 	default:
3232 		release_sock(&smc->sk);
3233 		return -ENOIOCTLCMD;
3234 	}
3235 	release_sock(&smc->sk);
3236 
3237 	return put_user(answ, (int __user *)arg);
3238 }
3239 
3240 /* Map the affected portions of the rmbe into an spd, note the number of bytes
3241  * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
3242  * updates till whenever a respective page has been fully processed.
3243  * Note that subsequent recv() calls have to wait till all splice() processing
3244  * completed.
3245  */
smc_splice_read(struct socket * sock,loff_t * ppos,struct pipe_inode_info * pipe,size_t len,unsigned int flags)3246 static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
3247 			       struct pipe_inode_info *pipe, size_t len,
3248 			       unsigned int flags)
3249 {
3250 	struct sock *sk = sock->sk;
3251 	struct smc_sock *smc;
3252 	int rc = -ENOTCONN;
3253 
3254 	smc = smc_sk(sk);
3255 	lock_sock(sk);
3256 	if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
3257 		/* socket was connected before, no more data to read */
3258 		rc = 0;
3259 		goto out;
3260 	}
3261 	if (sk->sk_state == SMC_INIT ||
3262 	    sk->sk_state == SMC_LISTEN ||
3263 	    sk->sk_state == SMC_CLOSED)
3264 		goto out;
3265 
3266 	if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
3267 		rc = 0;
3268 		goto out;
3269 	}
3270 
3271 	if (smc->use_fallback) {
3272 		rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
3273 						    pipe, len, flags);
3274 	} else {
3275 		if (*ppos) {
3276 			rc = -ESPIPE;
3277 			goto out;
3278 		}
3279 		if (flags & SPLICE_F_NONBLOCK)
3280 			flags = MSG_DONTWAIT;
3281 		else
3282 			flags = 0;
3283 		SMC_STAT_INC(smc, splice_cnt);
3284 		rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
3285 	}
3286 out:
3287 	release_sock(sk);
3288 
3289 	return rc;
3290 }
3291 
3292 /* must look like tcp */
3293 static const struct proto_ops smc_sock_ops = {
3294 	.family		= PF_SMC,
3295 	.owner		= THIS_MODULE,
3296 	.release	= smc_release,
3297 	.bind		= smc_bind,
3298 	.connect	= smc_connect,
3299 	.socketpair	= sock_no_socketpair,
3300 	.accept		= smc_accept,
3301 	.getname	= smc_getname,
3302 	.poll		= smc_poll,
3303 	.ioctl		= smc_ioctl,
3304 	.listen		= smc_listen,
3305 	.shutdown	= smc_shutdown,
3306 	.setsockopt	= smc_setsockopt,
3307 	.getsockopt	= smc_getsockopt,
3308 	.sendmsg	= smc_sendmsg,
3309 	.recvmsg	= smc_recvmsg,
3310 	.mmap		= sock_no_mmap,
3311 	.splice_read	= smc_splice_read,
3312 };
3313 
smc_create_clcsk(struct net * net,struct sock * sk,int family)3314 int smc_create_clcsk(struct net *net, struct sock *sk, int family)
3315 {
3316 	struct smc_sock *smc = smc_sk(sk);
3317 	int rc;
3318 
3319 	rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
3320 			      &smc->clcsock);
3321 	if (rc) {
3322 		sk_common_release(sk);
3323 		return rc;
3324 	}
3325 
3326 	/* smc_clcsock_release() does not wait smc->clcsock->sk's
3327 	 * destruction;  its sk_state might not be TCP_CLOSE after
3328 	 * smc->sk is close()d, and TCP timers can be fired later,
3329 	 * which need net ref.
3330 	 */
3331 	sk = smc->clcsock->sk;
3332 	__netns_tracker_free(net, &sk->ns_tracker, false);
3333 	sk->sk_net_refcnt = 1;
3334 	get_net_track(net, &sk->ns_tracker, GFP_KERNEL);
3335 	sock_inuse_add(net, 1);
3336 	return 0;
3337 }
3338 
__smc_create(struct net * net,struct socket * sock,int protocol,int kern,struct socket * clcsock)3339 static int __smc_create(struct net *net, struct socket *sock, int protocol,
3340 			int kern, struct socket *clcsock)
3341 {
3342 	int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
3343 	struct smc_sock *smc;
3344 	struct sock *sk;
3345 	int rc;
3346 
3347 	rc = -ESOCKTNOSUPPORT;
3348 	if (sock->type != SOCK_STREAM)
3349 		goto out;
3350 
3351 	rc = -EPROTONOSUPPORT;
3352 	if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
3353 		goto out;
3354 
3355 	rc = -ENOBUFS;
3356 	sock->ops = &smc_sock_ops;
3357 	sock->state = SS_UNCONNECTED;
3358 	sk = smc_sock_alloc(net, sock, protocol);
3359 	if (!sk)
3360 		goto out;
3361 
3362 	/* create internal TCP socket for CLC handshake and fallback */
3363 	smc = smc_sk(sk);
3364 
3365 	rc = 0;
3366 	if (clcsock)
3367 		smc->clcsock = clcsock;
3368 	else
3369 		rc = smc_create_clcsk(net, sk, family);
3370 out:
3371 	return rc;
3372 }
3373 
smc_create(struct net * net,struct socket * sock,int protocol,int kern)3374 static int smc_create(struct net *net, struct socket *sock, int protocol,
3375 		      int kern)
3376 {
3377 	return __smc_create(net, sock, protocol, kern, NULL);
3378 }
3379 
3380 static const struct net_proto_family smc_sock_family_ops = {
3381 	.family	= PF_SMC,
3382 	.owner	= THIS_MODULE,
3383 	.create	= smc_create,
3384 };
3385 
smc_ulp_init(struct sock * sk)3386 static int smc_ulp_init(struct sock *sk)
3387 {
3388 	struct socket *tcp = sk->sk_socket;
3389 	struct net *net = sock_net(sk);
3390 	struct socket *smcsock;
3391 	int protocol, ret;
3392 
3393 	/* only TCP can be replaced */
3394 	if (tcp->type != SOCK_STREAM || sk->sk_protocol != IPPROTO_TCP ||
3395 	    (sk->sk_family != AF_INET && sk->sk_family != AF_INET6))
3396 		return -ESOCKTNOSUPPORT;
3397 	/* don't handle wq now */
3398 	if (tcp->state != SS_UNCONNECTED || !tcp->file || tcp->wq.fasync_list)
3399 		return -ENOTCONN;
3400 
3401 	if (sk->sk_family == AF_INET)
3402 		protocol = SMCPROTO_SMC;
3403 	else
3404 		protocol = SMCPROTO_SMC6;
3405 
3406 	smcsock = sock_alloc();
3407 	if (!smcsock)
3408 		return -ENFILE;
3409 
3410 	smcsock->type = SOCK_STREAM;
3411 	__module_get(THIS_MODULE); /* tried in __tcp_ulp_find_autoload */
3412 	ret = __smc_create(net, smcsock, protocol, 1, tcp);
3413 	if (ret) {
3414 		sock_release(smcsock); /* module_put() which ops won't be NULL */
3415 		return ret;
3416 	}
3417 
3418 	/* replace tcp socket to smc */
3419 	smcsock->file = tcp->file;
3420 	smcsock->file->private_data = smcsock;
3421 	smcsock->file->f_inode = SOCK_INODE(smcsock); /* replace inode when sock_close */
3422 	smcsock->file->f_path.dentry->d_inode = SOCK_INODE(smcsock); /* dput() in __fput */
3423 	tcp->file = NULL;
3424 
3425 	return ret;
3426 }
3427 
smc_ulp_clone(const struct request_sock * req,struct sock * newsk,const gfp_t priority)3428 static void smc_ulp_clone(const struct request_sock *req, struct sock *newsk,
3429 			  const gfp_t priority)
3430 {
3431 	struct inet_connection_sock *icsk = inet_csk(newsk);
3432 
3433 	/* don't inherit ulp ops to child when listen */
3434 	icsk->icsk_ulp_ops = NULL;
3435 }
3436 
3437 static struct tcp_ulp_ops smc_ulp_ops __read_mostly = {
3438 	.name		= "smc",
3439 	.owner		= THIS_MODULE,
3440 	.init		= smc_ulp_init,
3441 	.clone		= smc_ulp_clone,
3442 };
3443 
3444 unsigned int smc_net_id;
3445 
smc_net_init(struct net * net)3446 static __net_init int smc_net_init(struct net *net)
3447 {
3448 	int rc;
3449 
3450 	rc = smc_sysctl_net_init(net);
3451 	if (rc)
3452 		return rc;
3453 	return smc_pnet_net_init(net);
3454 }
3455 
smc_net_exit(struct net * net)3456 static void __net_exit smc_net_exit(struct net *net)
3457 {
3458 	smc_sysctl_net_exit(net);
3459 	smc_pnet_net_exit(net);
3460 }
3461 
smc_net_stat_init(struct net * net)3462 static __net_init int smc_net_stat_init(struct net *net)
3463 {
3464 	return smc_stats_init(net);
3465 }
3466 
smc_net_stat_exit(struct net * net)3467 static void __net_exit smc_net_stat_exit(struct net *net)
3468 {
3469 	smc_stats_exit(net);
3470 }
3471 
3472 static struct pernet_operations smc_net_ops = {
3473 	.init = smc_net_init,
3474 	.exit = smc_net_exit,
3475 	.id   = &smc_net_id,
3476 	.size = sizeof(struct smc_net),
3477 };
3478 
3479 static struct pernet_operations smc_net_stat_ops = {
3480 	.init = smc_net_stat_init,
3481 	.exit = smc_net_stat_exit,
3482 };
3483 
smc_init(void)3484 static int __init smc_init(void)
3485 {
3486 	int rc;
3487 
3488 	rc = register_pernet_subsys(&smc_net_ops);
3489 	if (rc)
3490 		return rc;
3491 
3492 	rc = register_pernet_subsys(&smc_net_stat_ops);
3493 	if (rc)
3494 		goto out_pernet_subsys;
3495 
3496 	rc = smc_ism_init();
3497 	if (rc)
3498 		goto out_pernet_subsys_stat;
3499 	smc_clc_init();
3500 
3501 	rc = smc_nl_init();
3502 	if (rc)
3503 		goto out_ism;
3504 
3505 	rc = smc_pnet_init();
3506 	if (rc)
3507 		goto out_nl;
3508 
3509 	rc = -ENOMEM;
3510 
3511 	smc_tcp_ls_wq = alloc_workqueue("smc_tcp_ls_wq", 0, 0);
3512 	if (!smc_tcp_ls_wq)
3513 		goto out_pnet;
3514 
3515 	smc_hs_wq = alloc_workqueue("smc_hs_wq", 0, 0);
3516 	if (!smc_hs_wq)
3517 		goto out_alloc_tcp_ls_wq;
3518 
3519 	smc_close_wq = alloc_workqueue("smc_close_wq", 0, 0);
3520 	if (!smc_close_wq)
3521 		goto out_alloc_hs_wq;
3522 
3523 	rc = smc_core_init();
3524 	if (rc) {
3525 		pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
3526 		goto out_alloc_wqs;
3527 	}
3528 
3529 	rc = smc_llc_init();
3530 	if (rc) {
3531 		pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
3532 		goto out_core;
3533 	}
3534 
3535 	rc = smc_cdc_init();
3536 	if (rc) {
3537 		pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
3538 		goto out_core;
3539 	}
3540 
3541 	rc = proto_register(&smc_proto, 1);
3542 	if (rc) {
3543 		pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
3544 		goto out_core;
3545 	}
3546 
3547 	rc = proto_register(&smc_proto6, 1);
3548 	if (rc) {
3549 		pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
3550 		goto out_proto;
3551 	}
3552 
3553 	rc = sock_register(&smc_sock_family_ops);
3554 	if (rc) {
3555 		pr_err("%s: sock_register fails with %d\n", __func__, rc);
3556 		goto out_proto6;
3557 	}
3558 	INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
3559 	INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
3560 
3561 	rc = smc_ib_register_client();
3562 	if (rc) {
3563 		pr_err("%s: ib_register fails with %d\n", __func__, rc);
3564 		goto out_sock;
3565 	}
3566 
3567 	rc = tcp_register_ulp(&smc_ulp_ops);
3568 	if (rc) {
3569 		pr_err("%s: tcp_ulp_register fails with %d\n", __func__, rc);
3570 		goto out_ib;
3571 	}
3572 
3573 	static_branch_enable(&tcp_have_smc);
3574 	return 0;
3575 
3576 out_ib:
3577 	smc_ib_unregister_client();
3578 out_sock:
3579 	sock_unregister(PF_SMC);
3580 out_proto6:
3581 	proto_unregister(&smc_proto6);
3582 out_proto:
3583 	proto_unregister(&smc_proto);
3584 out_core:
3585 	smc_core_exit();
3586 out_alloc_wqs:
3587 	destroy_workqueue(smc_close_wq);
3588 out_alloc_hs_wq:
3589 	destroy_workqueue(smc_hs_wq);
3590 out_alloc_tcp_ls_wq:
3591 	destroy_workqueue(smc_tcp_ls_wq);
3592 out_pnet:
3593 	smc_pnet_exit();
3594 out_nl:
3595 	smc_nl_exit();
3596 out_ism:
3597 	smc_clc_exit();
3598 	smc_ism_exit();
3599 out_pernet_subsys_stat:
3600 	unregister_pernet_subsys(&smc_net_stat_ops);
3601 out_pernet_subsys:
3602 	unregister_pernet_subsys(&smc_net_ops);
3603 
3604 	return rc;
3605 }
3606 
smc_exit(void)3607 static void __exit smc_exit(void)
3608 {
3609 	static_branch_disable(&tcp_have_smc);
3610 	tcp_unregister_ulp(&smc_ulp_ops);
3611 	sock_unregister(PF_SMC);
3612 	smc_core_exit();
3613 	smc_ib_unregister_client();
3614 	smc_ism_exit();
3615 	destroy_workqueue(smc_close_wq);
3616 	destroy_workqueue(smc_tcp_ls_wq);
3617 	destroy_workqueue(smc_hs_wq);
3618 	proto_unregister(&smc_proto6);
3619 	proto_unregister(&smc_proto);
3620 	smc_pnet_exit();
3621 	smc_nl_exit();
3622 	smc_clc_exit();
3623 	unregister_pernet_subsys(&smc_net_stat_ops);
3624 	unregister_pernet_subsys(&smc_net_ops);
3625 	rcu_barrier();
3626 }
3627 
3628 module_init(smc_init);
3629 module_exit(smc_exit);
3630 
3631 MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
3632 MODULE_DESCRIPTION("smc socket address family");
3633 MODULE_LICENSE("GPL");
3634 MODULE_ALIAS_NETPROTO(PF_SMC);
3635 MODULE_ALIAS_TCP_ULP("smc");
3636 MODULE_ALIAS_GENL_FAMILY(SMC_GENL_FAMILY_NAME);
3637