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