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
29 #include <net/sock.h>
30 #include <net/tcp.h>
31 #include <net/smc.h>
32 #include <asm/ioctls.h>
33
34 #include <net/net_namespace.h>
35 #include <net/netns/generic.h>
36 #include "smc_netns.h"
37
38 #include "smc.h"
39 #include "smc_clc.h"
40 #include "smc_llc.h"
41 #include "smc_cdc.h"
42 #include "smc_core.h"
43 #include "smc_ib.h"
44 #include "smc_ism.h"
45 #include "smc_pnet.h"
46 #include "smc_tx.h"
47 #include "smc_rx.h"
48 #include "smc_close.h"
49
50 static DEFINE_MUTEX(smc_server_lgr_pending); /* serialize link group
51 * creation on server
52 */
53 static DEFINE_MUTEX(smc_client_lgr_pending); /* serialize link group
54 * creation on client
55 */
56
57 static void smc_tcp_listen_work(struct work_struct *);
58 static void smc_connect_work(struct work_struct *);
59
smc_set_keepalive(struct sock * sk,int val)60 static void smc_set_keepalive(struct sock *sk, int val)
61 {
62 struct smc_sock *smc = smc_sk(sk);
63
64 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
65 }
66
67 static struct smc_hashinfo smc_v4_hashinfo = {
68 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
69 };
70
71 static struct smc_hashinfo smc_v6_hashinfo = {
72 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
73 };
74
smc_hash_sk(struct sock * sk)75 int smc_hash_sk(struct sock *sk)
76 {
77 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
78 struct hlist_head *head;
79
80 head = &h->ht;
81
82 write_lock_bh(&h->lock);
83 sk_add_node(sk, head);
84 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
85 write_unlock_bh(&h->lock);
86
87 return 0;
88 }
89 EXPORT_SYMBOL_GPL(smc_hash_sk);
90
smc_unhash_sk(struct sock * sk)91 void smc_unhash_sk(struct sock *sk)
92 {
93 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
94
95 write_lock_bh(&h->lock);
96 if (sk_del_node_init(sk))
97 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
98 write_unlock_bh(&h->lock);
99 }
100 EXPORT_SYMBOL_GPL(smc_unhash_sk);
101
102 struct proto smc_proto = {
103 .name = "SMC",
104 .owner = THIS_MODULE,
105 .keepalive = smc_set_keepalive,
106 .hash = smc_hash_sk,
107 .unhash = smc_unhash_sk,
108 .obj_size = sizeof(struct smc_sock),
109 .h.smc_hash = &smc_v4_hashinfo,
110 .slab_flags = SLAB_TYPESAFE_BY_RCU,
111 };
112 EXPORT_SYMBOL_GPL(smc_proto);
113
114 struct proto smc_proto6 = {
115 .name = "SMC6",
116 .owner = THIS_MODULE,
117 .keepalive = smc_set_keepalive,
118 .hash = smc_hash_sk,
119 .unhash = smc_unhash_sk,
120 .obj_size = sizeof(struct smc_sock),
121 .h.smc_hash = &smc_v6_hashinfo,
122 .slab_flags = SLAB_TYPESAFE_BY_RCU,
123 };
124 EXPORT_SYMBOL_GPL(smc_proto6);
125
smc_restore_fallback_changes(struct smc_sock * smc)126 static void smc_restore_fallback_changes(struct smc_sock *smc)
127 {
128 smc->clcsock->file->private_data = smc->sk.sk_socket;
129 smc->clcsock->file = NULL;
130 }
131
__smc_release(struct smc_sock * smc)132 static int __smc_release(struct smc_sock *smc)
133 {
134 struct sock *sk = &smc->sk;
135 int rc = 0;
136
137 if (!smc->use_fallback) {
138 rc = smc_close_active(smc);
139 sock_set_flag(sk, SOCK_DEAD);
140 sk->sk_shutdown |= SHUTDOWN_MASK;
141 } else {
142 if (sk->sk_state != SMC_LISTEN && sk->sk_state != SMC_INIT)
143 sock_put(sk); /* passive closing */
144 if (sk->sk_state == SMC_LISTEN) {
145 /* wake up clcsock accept */
146 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
147 }
148 sk->sk_state = SMC_CLOSED;
149 sk->sk_state_change(sk);
150 smc_restore_fallback_changes(smc);
151 }
152
153 sk->sk_prot->unhash(sk);
154
155 if (sk->sk_state == SMC_CLOSED) {
156 if (smc->clcsock) {
157 release_sock(sk);
158 smc_clcsock_release(smc);
159 lock_sock(sk);
160 }
161 if (!smc->use_fallback)
162 smc_conn_free(&smc->conn);
163 }
164
165 return rc;
166 }
167
smc_release(struct socket * sock)168 static int smc_release(struct socket *sock)
169 {
170 struct sock *sk = sock->sk;
171 struct smc_sock *smc;
172 int rc = 0;
173
174 if (!sk)
175 goto out;
176
177 smc = smc_sk(sk);
178
179 /* cleanup for a dangling non-blocking connect */
180 if (smc->connect_nonblock && sk->sk_state == SMC_INIT)
181 tcp_abort(smc->clcsock->sk, ECONNABORTED);
182 flush_work(&smc->connect_work);
183
184 if (sk->sk_state == SMC_LISTEN)
185 /* smc_close_non_accepted() is called and acquires
186 * sock lock for child sockets again
187 */
188 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
189 else
190 lock_sock(sk);
191
192 rc = __smc_release(smc);
193
194 /* detach socket */
195 sock_orphan(sk);
196 sock->sk = NULL;
197 release_sock(sk);
198
199 sock_put(sk); /* final sock_put */
200 out:
201 return rc;
202 }
203
smc_destruct(struct sock * sk)204 static void smc_destruct(struct sock *sk)
205 {
206 if (sk->sk_state != SMC_CLOSED)
207 return;
208 if (!sock_flag(sk, SOCK_DEAD))
209 return;
210
211 sk_refcnt_debug_dec(sk);
212 }
213
smc_sock_alloc(struct net * net,struct socket * sock,int protocol)214 static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
215 int protocol)
216 {
217 struct smc_sock *smc;
218 struct proto *prot;
219 struct sock *sk;
220
221 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
222 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
223 if (!sk)
224 return NULL;
225
226 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
227 sk->sk_state = SMC_INIT;
228 sk->sk_destruct = smc_destruct;
229 sk->sk_protocol = protocol;
230 smc = smc_sk(sk);
231 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
232 INIT_WORK(&smc->connect_work, smc_connect_work);
233 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
234 INIT_LIST_HEAD(&smc->accept_q);
235 spin_lock_init(&smc->accept_q_lock);
236 spin_lock_init(&smc->conn.send_lock);
237 sk->sk_prot->hash(sk);
238 sk_refcnt_debug_inc(sk);
239 mutex_init(&smc->clcsock_release_lock);
240
241 return sk;
242 }
243
smc_bind(struct socket * sock,struct sockaddr * uaddr,int addr_len)244 static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
245 int addr_len)
246 {
247 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
248 struct sock *sk = sock->sk;
249 struct smc_sock *smc;
250 int rc;
251
252 smc = smc_sk(sk);
253
254 /* replicate tests from inet_bind(), to be safe wrt. future changes */
255 rc = -EINVAL;
256 if (addr_len < sizeof(struct sockaddr_in))
257 goto out;
258
259 rc = -EAFNOSUPPORT;
260 if (addr->sin_family != AF_INET &&
261 addr->sin_family != AF_INET6 &&
262 addr->sin_family != AF_UNSPEC)
263 goto out;
264 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
265 if (addr->sin_family == AF_UNSPEC &&
266 addr->sin_addr.s_addr != htonl(INADDR_ANY))
267 goto out;
268
269 lock_sock(sk);
270
271 /* Check if socket is already active */
272 rc = -EINVAL;
273 if (sk->sk_state != SMC_INIT || smc->connect_nonblock)
274 goto out_rel;
275
276 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
277 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
278
279 out_rel:
280 release_sock(sk);
281 out:
282 return rc;
283 }
284
smc_copy_sock_settings(struct sock * nsk,struct sock * osk,unsigned long mask)285 static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
286 unsigned long mask)
287 {
288 /* options we don't get control via setsockopt for */
289 nsk->sk_type = osk->sk_type;
290 nsk->sk_sndbuf = osk->sk_sndbuf;
291 nsk->sk_rcvbuf = osk->sk_rcvbuf;
292 nsk->sk_sndtimeo = osk->sk_sndtimeo;
293 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
294 nsk->sk_mark = osk->sk_mark;
295 nsk->sk_priority = osk->sk_priority;
296 nsk->sk_rcvlowat = osk->sk_rcvlowat;
297 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
298 nsk->sk_err = osk->sk_err;
299
300 nsk->sk_flags &= ~mask;
301 nsk->sk_flags |= osk->sk_flags & mask;
302 }
303
304 #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
305 (1UL << SOCK_KEEPOPEN) | \
306 (1UL << SOCK_LINGER) | \
307 (1UL << SOCK_BROADCAST) | \
308 (1UL << SOCK_TIMESTAMP) | \
309 (1UL << SOCK_DBG) | \
310 (1UL << SOCK_RCVTSTAMP) | \
311 (1UL << SOCK_RCVTSTAMPNS) | \
312 (1UL << SOCK_LOCALROUTE) | \
313 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
314 (1UL << SOCK_RXQ_OVFL) | \
315 (1UL << SOCK_WIFI_STATUS) | \
316 (1UL << SOCK_NOFCS) | \
317 (1UL << SOCK_FILTER_LOCKED) | \
318 (1UL << SOCK_TSTAMP_NEW))
319 /* copy only relevant settings and flags of SOL_SOCKET level from smc to
320 * clc socket (since smc is not called for these options from net/core)
321 */
smc_copy_sock_settings_to_clc(struct smc_sock * smc)322 static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
323 {
324 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
325 }
326
327 #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
328 (1UL << SOCK_KEEPOPEN) | \
329 (1UL << SOCK_LINGER) | \
330 (1UL << SOCK_DBG))
331 /* copy only settings and flags relevant for smc from clc to smc socket */
smc_copy_sock_settings_to_smc(struct smc_sock * smc)332 static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
333 {
334 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
335 }
336
337 /* register a new rmb, send confirm_rkey msg to register with peer */
smc_reg_rmb(struct smc_link * link,struct smc_buf_desc * rmb_desc,bool conf_rkey)338 static int smc_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc,
339 bool conf_rkey)
340 {
341 if (!rmb_desc->wr_reg) {
342 /* register memory region for new rmb */
343 if (smc_wr_reg_send(link, rmb_desc->mr_rx[SMC_SINGLE_LINK])) {
344 rmb_desc->regerr = 1;
345 return -EFAULT;
346 }
347 rmb_desc->wr_reg = 1;
348 }
349 if (!conf_rkey)
350 return 0;
351 /* exchange confirm_rkey msg with peer */
352 if (smc_llc_do_confirm_rkey(link, rmb_desc)) {
353 rmb_desc->regerr = 1;
354 return -EFAULT;
355 }
356 return 0;
357 }
358
smc_clnt_conf_first_link(struct smc_sock * smc)359 static int smc_clnt_conf_first_link(struct smc_sock *smc)
360 {
361 struct net *net = sock_net(smc->clcsock->sk);
362 struct smc_link_group *lgr = smc->conn.lgr;
363 struct smc_link *link;
364 int rest;
365 int rc;
366
367 link = &lgr->lnk[SMC_SINGLE_LINK];
368 /* receive CONFIRM LINK request from server over RoCE fabric */
369 rest = wait_for_completion_interruptible_timeout(
370 &link->llc_confirm,
371 SMC_LLC_WAIT_FIRST_TIME);
372 if (rest <= 0) {
373 struct smc_clc_msg_decline dclc;
374
375 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
376 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
377 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
378 }
379
380 if (link->llc_confirm_rc)
381 return SMC_CLC_DECL_RMBE_EC;
382
383 rc = smc_ib_modify_qp_rts(link);
384 if (rc)
385 return SMC_CLC_DECL_ERR_RDYLNK;
386
387 smc_wr_remember_qp_attr(link);
388
389 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
390 return SMC_CLC_DECL_ERR_REGRMB;
391
392 /* send CONFIRM LINK response over RoCE fabric */
393 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
394 if (rc < 0)
395 return SMC_CLC_DECL_TIMEOUT_CL;
396
397 /* receive ADD LINK request from server over RoCE fabric */
398 rest = wait_for_completion_interruptible_timeout(&link->llc_add,
399 SMC_LLC_WAIT_TIME);
400 if (rest <= 0) {
401 struct smc_clc_msg_decline dclc;
402
403 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
404 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
405 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
406 }
407
408 /* send add link reject message, only one link supported for now */
409 rc = smc_llc_send_add_link(link,
410 link->smcibdev->mac[link->ibport - 1],
411 link->gid, SMC_LLC_RESP);
412 if (rc < 0)
413 return SMC_CLC_DECL_TIMEOUT_AL;
414
415 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
416
417 return 0;
418 }
419
smcr_conn_save_peer_info(struct smc_sock * smc,struct smc_clc_msg_accept_confirm * clc)420 static void smcr_conn_save_peer_info(struct smc_sock *smc,
421 struct smc_clc_msg_accept_confirm *clc)
422 {
423 int bufsize = smc_uncompress_bufsize(clc->rmbe_size);
424
425 smc->conn.peer_rmbe_idx = clc->rmbe_idx;
426 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
427 smc->conn.peer_rmbe_size = bufsize;
428 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
429 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
430 }
431
smcd_conn_save_peer_info(struct smc_sock * smc,struct smc_clc_msg_accept_confirm * clc)432 static void smcd_conn_save_peer_info(struct smc_sock *smc,
433 struct smc_clc_msg_accept_confirm *clc)
434 {
435 int bufsize = smc_uncompress_bufsize(clc->dmbe_size);
436
437 smc->conn.peer_rmbe_idx = clc->dmbe_idx;
438 smc->conn.peer_token = clc->token;
439 /* msg header takes up space in the buffer */
440 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
441 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
442 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
443 }
444
smc_conn_save_peer_info(struct smc_sock * smc,struct smc_clc_msg_accept_confirm * clc)445 static void smc_conn_save_peer_info(struct smc_sock *smc,
446 struct smc_clc_msg_accept_confirm *clc)
447 {
448 if (smc->conn.lgr->is_smcd)
449 smcd_conn_save_peer_info(smc, clc);
450 else
451 smcr_conn_save_peer_info(smc, clc);
452 }
453
smc_link_save_peer_info(struct smc_link * link,struct smc_clc_msg_accept_confirm * clc)454 static void smc_link_save_peer_info(struct smc_link *link,
455 struct smc_clc_msg_accept_confirm *clc)
456 {
457 link->peer_qpn = ntoh24(clc->qpn);
458 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
459 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
460 link->peer_psn = ntoh24(clc->psn);
461 link->peer_mtu = clc->qp_mtu;
462 }
463
smc_switch_to_fallback(struct smc_sock * smc)464 static void smc_switch_to_fallback(struct smc_sock *smc)
465 {
466 smc->use_fallback = true;
467 if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
468 smc->clcsock->file = smc->sk.sk_socket->file;
469 smc->clcsock->file->private_data = smc->clcsock;
470 }
471 }
472
473 /* fall back during connect */
smc_connect_fallback(struct smc_sock * smc,int reason_code)474 static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
475 {
476 smc_switch_to_fallback(smc);
477 smc->fallback_rsn = reason_code;
478 smc_copy_sock_settings_to_clc(smc);
479 smc->connect_nonblock = 0;
480 if (smc->sk.sk_state == SMC_INIT)
481 smc->sk.sk_state = SMC_ACTIVE;
482 return 0;
483 }
484
485 /* decline and fall back during connect */
smc_connect_decline_fallback(struct smc_sock * smc,int reason_code)486 static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
487 {
488 int rc;
489
490 if (reason_code < 0) { /* error, fallback is not possible */
491 if (smc->sk.sk_state == SMC_INIT)
492 sock_put(&smc->sk); /* passive closing */
493 return reason_code;
494 }
495 if (reason_code != SMC_CLC_DECL_PEERDECL) {
496 rc = smc_clc_send_decline(smc, reason_code);
497 if (rc < 0) {
498 if (smc->sk.sk_state == SMC_INIT)
499 sock_put(&smc->sk); /* passive closing */
500 return rc;
501 }
502 }
503 return smc_connect_fallback(smc, reason_code);
504 }
505
506 /* abort connecting */
smc_connect_abort(struct smc_sock * smc,int reason_code,int local_contact)507 static int smc_connect_abort(struct smc_sock *smc, int reason_code,
508 int local_contact)
509 {
510 if (local_contact == SMC_FIRST_CONTACT)
511 smc_lgr_forget(smc->conn.lgr);
512 if (smc->conn.lgr->is_smcd)
513 /* there is only one lgr role for SMC-D; use server lock */
514 mutex_unlock(&smc_server_lgr_pending);
515 else
516 mutex_unlock(&smc_client_lgr_pending);
517
518 smc_conn_free(&smc->conn);
519 smc->connect_nonblock = 0;
520 return reason_code;
521 }
522
523 /* check if there is a rdma device available for this connection. */
524 /* called for connect and listen */
smc_find_rdma_device(struct smc_sock * smc,struct smc_init_info * ini)525 static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
526 {
527 /* PNET table look up: search active ib_device and port
528 * within same PNETID that also contains the ethernet device
529 * used for the internal TCP socket
530 */
531 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
532 if (!ini->ib_dev)
533 return SMC_CLC_DECL_NOSMCRDEV;
534 return 0;
535 }
536
537 /* check if there is an ISM device available for this connection. */
538 /* called for connect and listen */
smc_find_ism_device(struct smc_sock * smc,struct smc_init_info * ini)539 static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
540 {
541 /* Find ISM device with same PNETID as connecting interface */
542 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
543 if (!ini->ism_dev)
544 return SMC_CLC_DECL_NOSMCDDEV;
545 return 0;
546 }
547
548 /* 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)549 static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
550 struct smc_init_info *ini)
551 {
552 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev, ini->vlan_id))
553 return SMC_CLC_DECL_ISMVLANERR;
554 return 0;
555 }
556
557 /* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
558 * used, the VLAN ID will be registered again during the connection setup.
559 */
smc_connect_ism_vlan_cleanup(struct smc_sock * smc,bool is_smcd,struct smc_init_info * ini)560 static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
561 struct smc_init_info *ini)
562 {
563 if (!is_smcd)
564 return 0;
565 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev, ini->vlan_id))
566 return SMC_CLC_DECL_CNFERR;
567 return 0;
568 }
569
570 /* CLC handshake during connect */
smc_connect_clc(struct smc_sock * smc,int smc_type,struct smc_clc_msg_accept_confirm * aclc,struct smc_init_info * ini)571 static int smc_connect_clc(struct smc_sock *smc, int smc_type,
572 struct smc_clc_msg_accept_confirm *aclc,
573 struct smc_init_info *ini)
574 {
575 int rc = 0;
576
577 /* do inband token exchange */
578 rc = smc_clc_send_proposal(smc, smc_type, ini);
579 if (rc)
580 return rc;
581 /* receive SMC Accept CLC message */
582 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT,
583 CLC_WAIT_TIME);
584 }
585
586 /* 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)587 static int smc_connect_rdma(struct smc_sock *smc,
588 struct smc_clc_msg_accept_confirm *aclc,
589 struct smc_init_info *ini)
590 {
591 struct smc_link *link;
592 int reason_code = 0;
593
594 ini->is_smcd = false;
595 ini->ib_lcl = &aclc->lcl;
596 ini->ib_clcqpn = ntoh24(aclc->qpn);
597 ini->srv_first_contact = aclc->hdr.flag;
598
599 mutex_lock(&smc_client_lgr_pending);
600 reason_code = smc_conn_create(smc, ini);
601 if (reason_code) {
602 mutex_unlock(&smc_client_lgr_pending);
603 return reason_code;
604 }
605 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
606
607 smc_conn_save_peer_info(smc, aclc);
608
609 /* create send buffer and rmb */
610 if (smc_buf_create(smc, false))
611 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
612 ini->cln_first_contact);
613
614 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
615 smc_link_save_peer_info(link, aclc);
616
617 if (smc_rmb_rtoken_handling(&smc->conn, aclc))
618 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
619 ini->cln_first_contact);
620
621 smc_close_init(smc);
622 smc_rx_init(smc);
623
624 if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
625 if (smc_ib_ready_link(link))
626 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
627 ini->cln_first_contact);
628 } else {
629 if (smc_reg_rmb(link, smc->conn.rmb_desc, true))
630 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
631 ini->cln_first_contact);
632 }
633 smc_rmb_sync_sg_for_device(&smc->conn);
634
635 reason_code = smc_clc_send_confirm(smc);
636 if (reason_code)
637 return smc_connect_abort(smc, reason_code,
638 ini->cln_first_contact);
639
640 smc_tx_init(smc);
641
642 if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
643 /* QP confirmation over RoCE fabric */
644 reason_code = smc_clnt_conf_first_link(smc);
645 if (reason_code)
646 return smc_connect_abort(smc, reason_code,
647 ini->cln_first_contact);
648 }
649 mutex_unlock(&smc_client_lgr_pending);
650
651 smc_copy_sock_settings_to_clc(smc);
652 smc->connect_nonblock = 0;
653 if (smc->sk.sk_state == SMC_INIT)
654 smc->sk.sk_state = SMC_ACTIVE;
655
656 return 0;
657 }
658
659 /* 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)660 static int smc_connect_ism(struct smc_sock *smc,
661 struct smc_clc_msg_accept_confirm *aclc,
662 struct smc_init_info *ini)
663 {
664 int rc = 0;
665
666 ini->is_smcd = true;
667 ini->ism_gid = aclc->gid;
668 ini->srv_first_contact = aclc->hdr.flag;
669
670 /* there is only one lgr role for SMC-D; use server lock */
671 mutex_lock(&smc_server_lgr_pending);
672 rc = smc_conn_create(smc, ini);
673 if (rc) {
674 mutex_unlock(&smc_server_lgr_pending);
675 return rc;
676 }
677
678 /* Create send and receive buffers */
679 if (smc_buf_create(smc, true))
680 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
681 ini->cln_first_contact);
682
683 smc_conn_save_peer_info(smc, aclc);
684 smc_close_init(smc);
685 smc_rx_init(smc);
686 smc_tx_init(smc);
687
688 rc = smc_clc_send_confirm(smc);
689 if (rc)
690 return smc_connect_abort(smc, rc, ini->cln_first_contact);
691 mutex_unlock(&smc_server_lgr_pending);
692
693 smc_copy_sock_settings_to_clc(smc);
694 smc->connect_nonblock = 0;
695 if (smc->sk.sk_state == SMC_INIT)
696 smc->sk.sk_state = SMC_ACTIVE;
697
698 return 0;
699 }
700
701 /* perform steps before actually connecting */
__smc_connect(struct smc_sock * smc)702 static int __smc_connect(struct smc_sock *smc)
703 {
704 bool ism_supported = false, rdma_supported = false;
705 struct smc_clc_msg_accept_confirm aclc;
706 struct smc_init_info ini = {0};
707 int smc_type;
708 int rc = 0;
709
710 if (smc->use_fallback)
711 return smc_connect_fallback(smc, smc->fallback_rsn);
712
713 /* if peer has not signalled SMC-capability, fall back */
714 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
715 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
716
717 /* IPSec connections opt out of SMC-R optimizations */
718 if (using_ipsec(smc))
719 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
720
721 /* get vlan id from IP device */
722 if (smc_vlan_by_tcpsk(smc->clcsock, &ini))
723 return smc_connect_decline_fallback(smc,
724 SMC_CLC_DECL_GETVLANERR);
725
726 /* check if there is an ism device available */
727 if (!smc_find_ism_device(smc, &ini) &&
728 !smc_connect_ism_vlan_setup(smc, &ini)) {
729 /* ISM is supported for this connection */
730 ism_supported = true;
731 smc_type = SMC_TYPE_D;
732 }
733
734 /* check if there is a rdma device available */
735 if (!smc_find_rdma_device(smc, &ini)) {
736 /* RDMA is supported for this connection */
737 rdma_supported = true;
738 if (ism_supported)
739 smc_type = SMC_TYPE_B; /* both */
740 else
741 smc_type = SMC_TYPE_R; /* only RDMA */
742 }
743
744 /* if neither ISM nor RDMA are supported, fallback */
745 if (!rdma_supported && !ism_supported)
746 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV);
747
748 /* perform CLC handshake */
749 rc = smc_connect_clc(smc, smc_type, &aclc, &ini);
750 if (rc) {
751 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
752 return smc_connect_decline_fallback(smc, rc);
753 }
754
755 /* depending on previous steps, connect using rdma or ism */
756 if (rdma_supported && aclc.hdr.path == SMC_TYPE_R)
757 rc = smc_connect_rdma(smc, &aclc, &ini);
758 else if (ism_supported && aclc.hdr.path == SMC_TYPE_D)
759 rc = smc_connect_ism(smc, &aclc, &ini);
760 else
761 rc = SMC_CLC_DECL_MODEUNSUPP;
762 if (rc) {
763 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
764 return smc_connect_decline_fallback(smc, rc);
765 }
766
767 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
768 return 0;
769 }
770
smc_connect_work(struct work_struct * work)771 static void smc_connect_work(struct work_struct *work)
772 {
773 struct smc_sock *smc = container_of(work, struct smc_sock,
774 connect_work);
775 long timeo = smc->sk.sk_sndtimeo;
776 int rc = 0;
777
778 if (!timeo)
779 timeo = MAX_SCHEDULE_TIMEOUT;
780 lock_sock(smc->clcsock->sk);
781 if (smc->clcsock->sk->sk_err) {
782 smc->sk.sk_err = smc->clcsock->sk->sk_err;
783 } else if ((1 << smc->clcsock->sk->sk_state) &
784 (TCPF_SYN_SENT | TCP_SYN_RECV)) {
785 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
786 if ((rc == -EPIPE) &&
787 ((1 << smc->clcsock->sk->sk_state) &
788 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
789 rc = 0;
790 }
791 release_sock(smc->clcsock->sk);
792 lock_sock(&smc->sk);
793 if (rc != 0 || smc->sk.sk_err) {
794 smc->sk.sk_state = SMC_CLOSED;
795 if (rc == -EPIPE || rc == -EAGAIN)
796 smc->sk.sk_err = EPIPE;
797 else if (signal_pending(current))
798 smc->sk.sk_err = -sock_intr_errno(timeo);
799 sock_put(&smc->sk); /* passive closing */
800 goto out;
801 }
802
803 rc = __smc_connect(smc);
804 if (rc < 0)
805 smc->sk.sk_err = -rc;
806
807 out:
808 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
809 if (smc->sk.sk_err) {
810 smc->sk.sk_state_change(&smc->sk);
811 } else { /* allow polling before and after fallback decision */
812 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
813 smc->sk.sk_write_space(&smc->sk);
814 }
815 }
816 release_sock(&smc->sk);
817 }
818
smc_connect(struct socket * sock,struct sockaddr * addr,int alen,int flags)819 static int smc_connect(struct socket *sock, struct sockaddr *addr,
820 int alen, int flags)
821 {
822 struct sock *sk = sock->sk;
823 struct smc_sock *smc;
824 int rc = -EINVAL;
825
826 smc = smc_sk(sk);
827
828 /* separate smc parameter checking to be safe */
829 if (alen < sizeof(addr->sa_family))
830 goto out_err;
831 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
832 goto out_err;
833
834 lock_sock(sk);
835 switch (sk->sk_state) {
836 default:
837 goto out;
838 case SMC_ACTIVE:
839 rc = -EISCONN;
840 goto out;
841 case SMC_INIT:
842 rc = 0;
843 break;
844 }
845
846 smc_copy_sock_settings_to_clc(smc);
847 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
848 if (smc->connect_nonblock) {
849 rc = -EALREADY;
850 goto out;
851 }
852 rc = kernel_connect(smc->clcsock, addr, alen, flags);
853 if (rc && rc != -EINPROGRESS)
854 goto out;
855
856 sock_hold(&smc->sk); /* sock put in passive closing */
857 if (smc->use_fallback)
858 goto out;
859 if (flags & O_NONBLOCK) {
860 if (schedule_work(&smc->connect_work))
861 smc->connect_nonblock = 1;
862 rc = -EINPROGRESS;
863 } else {
864 rc = __smc_connect(smc);
865 if (rc < 0)
866 goto out;
867 else
868 rc = 0; /* success cases including fallback */
869 }
870
871 out:
872 release_sock(sk);
873 out_err:
874 return rc;
875 }
876
smc_clcsock_accept(struct smc_sock * lsmc,struct smc_sock ** new_smc)877 static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
878 {
879 struct socket *new_clcsock = NULL;
880 struct sock *lsk = &lsmc->sk;
881 struct sock *new_sk;
882 int rc = -EINVAL;
883
884 release_sock(lsk);
885 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
886 if (!new_sk) {
887 rc = -ENOMEM;
888 lsk->sk_err = ENOMEM;
889 *new_smc = NULL;
890 lock_sock(lsk);
891 goto out;
892 }
893 *new_smc = smc_sk(new_sk);
894
895 mutex_lock(&lsmc->clcsock_release_lock);
896 if (lsmc->clcsock)
897 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
898 mutex_unlock(&lsmc->clcsock_release_lock);
899 lock_sock(lsk);
900 if (rc < 0)
901 lsk->sk_err = -rc;
902 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
903 new_sk->sk_prot->unhash(new_sk);
904 if (new_clcsock)
905 sock_release(new_clcsock);
906 new_sk->sk_state = SMC_CLOSED;
907 sock_set_flag(new_sk, SOCK_DEAD);
908 sock_put(new_sk); /* final */
909 *new_smc = NULL;
910 goto out;
911 }
912
913 (*new_smc)->clcsock = new_clcsock;
914 out:
915 return rc;
916 }
917
918 /* add a just created sock to the accept queue of the listen sock as
919 * candidate for a following socket accept call from user space
920 */
smc_accept_enqueue(struct sock * parent,struct sock * sk)921 static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
922 {
923 struct smc_sock *par = smc_sk(parent);
924
925 sock_hold(sk); /* sock_put in smc_accept_unlink () */
926 spin_lock(&par->accept_q_lock);
927 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
928 spin_unlock(&par->accept_q_lock);
929 sk_acceptq_added(parent);
930 }
931
932 /* remove a socket from the accept queue of its parental listening socket */
smc_accept_unlink(struct sock * sk)933 static void smc_accept_unlink(struct sock *sk)
934 {
935 struct smc_sock *par = smc_sk(sk)->listen_smc;
936
937 spin_lock(&par->accept_q_lock);
938 list_del_init(&smc_sk(sk)->accept_q);
939 spin_unlock(&par->accept_q_lock);
940 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
941 sock_put(sk); /* sock_hold in smc_accept_enqueue */
942 }
943
944 /* remove a sock from the accept queue to bind it to a new socket created
945 * for a socket accept call from user space
946 */
smc_accept_dequeue(struct sock * parent,struct socket * new_sock)947 struct sock *smc_accept_dequeue(struct sock *parent,
948 struct socket *new_sock)
949 {
950 struct smc_sock *isk, *n;
951 struct sock *new_sk;
952
953 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
954 new_sk = (struct sock *)isk;
955
956 smc_accept_unlink(new_sk);
957 if (new_sk->sk_state == SMC_CLOSED) {
958 new_sk->sk_prot->unhash(new_sk);
959 if (isk->clcsock) {
960 sock_release(isk->clcsock);
961 isk->clcsock = NULL;
962 }
963 sock_put(new_sk); /* final */
964 continue;
965 }
966 if (new_sock) {
967 sock_graft(new_sk, new_sock);
968 if (isk->use_fallback) {
969 smc_sk(new_sk)->clcsock->file = new_sock->file;
970 isk->clcsock->file->private_data = isk->clcsock;
971 }
972 }
973 return new_sk;
974 }
975 return NULL;
976 }
977
978 /* clean up for a created but never accepted sock */
smc_close_non_accepted(struct sock * sk)979 void smc_close_non_accepted(struct sock *sk)
980 {
981 struct smc_sock *smc = smc_sk(sk);
982
983 lock_sock(sk);
984 if (!sk->sk_lingertime)
985 /* wait for peer closing */
986 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
987 __smc_release(smc);
988 release_sock(sk);
989 sock_put(sk); /* final sock_put */
990 }
991
smc_serv_conf_first_link(struct smc_sock * smc)992 static int smc_serv_conf_first_link(struct smc_sock *smc)
993 {
994 struct net *net = sock_net(smc->clcsock->sk);
995 struct smc_link_group *lgr = smc->conn.lgr;
996 struct smc_link *link;
997 int rest;
998 int rc;
999
1000 link = &lgr->lnk[SMC_SINGLE_LINK];
1001
1002 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
1003 return SMC_CLC_DECL_ERR_REGRMB;
1004
1005 /* send CONFIRM LINK request to client over the RoCE fabric */
1006 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
1007 if (rc < 0)
1008 return SMC_CLC_DECL_TIMEOUT_CL;
1009
1010 /* receive CONFIRM LINK response from client over the RoCE fabric */
1011 rest = wait_for_completion_interruptible_timeout(
1012 &link->llc_confirm_resp,
1013 SMC_LLC_WAIT_FIRST_TIME);
1014 if (rest <= 0) {
1015 struct smc_clc_msg_decline dclc;
1016
1017 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
1018 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
1019 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
1020 }
1021
1022 if (link->llc_confirm_resp_rc)
1023 return SMC_CLC_DECL_RMBE_EC;
1024
1025 /* send ADD LINK request to client over the RoCE fabric */
1026 rc = smc_llc_send_add_link(link,
1027 link->smcibdev->mac[link->ibport - 1],
1028 link->gid, SMC_LLC_REQ);
1029 if (rc < 0)
1030 return SMC_CLC_DECL_TIMEOUT_AL;
1031
1032 /* receive ADD LINK response from client over the RoCE fabric */
1033 rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
1034 SMC_LLC_WAIT_TIME);
1035 if (rest <= 0) {
1036 struct smc_clc_msg_decline dclc;
1037
1038 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
1039 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
1040 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
1041 }
1042
1043 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
1044
1045 return 0;
1046 }
1047
1048 /* listen worker: finish */
smc_listen_out(struct smc_sock * new_smc)1049 static void smc_listen_out(struct smc_sock *new_smc)
1050 {
1051 struct smc_sock *lsmc = new_smc->listen_smc;
1052 struct sock *newsmcsk = &new_smc->sk;
1053
1054 if (lsmc->sk.sk_state == SMC_LISTEN) {
1055 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
1056 smc_accept_enqueue(&lsmc->sk, newsmcsk);
1057 release_sock(&lsmc->sk);
1058 } else { /* no longer listening */
1059 smc_close_non_accepted(newsmcsk);
1060 }
1061
1062 /* Wake up accept */
1063 lsmc->sk.sk_data_ready(&lsmc->sk);
1064 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
1065 }
1066
1067 /* listen worker: finish in state connected */
smc_listen_out_connected(struct smc_sock * new_smc)1068 static void smc_listen_out_connected(struct smc_sock *new_smc)
1069 {
1070 struct sock *newsmcsk = &new_smc->sk;
1071
1072 sk_refcnt_debug_inc(newsmcsk);
1073 if (newsmcsk->sk_state == SMC_INIT)
1074 newsmcsk->sk_state = SMC_ACTIVE;
1075
1076 smc_listen_out(new_smc);
1077 }
1078
1079 /* listen worker: finish in error state */
smc_listen_out_err(struct smc_sock * new_smc)1080 static void smc_listen_out_err(struct smc_sock *new_smc)
1081 {
1082 struct sock *newsmcsk = &new_smc->sk;
1083
1084 if (newsmcsk->sk_state == SMC_INIT)
1085 sock_put(&new_smc->sk); /* passive closing */
1086 newsmcsk->sk_state = SMC_CLOSED;
1087 smc_conn_free(&new_smc->conn);
1088
1089 smc_listen_out(new_smc);
1090 }
1091
1092 /* listen worker: decline and fall back if possible */
smc_listen_decline(struct smc_sock * new_smc,int reason_code,int local_contact)1093 static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1094 int local_contact)
1095 {
1096 /* RDMA setup failed, switch back to TCP */
1097 if (local_contact == SMC_FIRST_CONTACT)
1098 smc_lgr_forget(new_smc->conn.lgr);
1099 if (reason_code < 0) { /* error, no fallback possible */
1100 smc_listen_out_err(new_smc);
1101 return;
1102 }
1103 smc_conn_free(&new_smc->conn);
1104 smc_switch_to_fallback(new_smc);
1105 new_smc->fallback_rsn = reason_code;
1106 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
1107 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1108 smc_listen_out_err(new_smc);
1109 return;
1110 }
1111 }
1112 smc_listen_out_connected(new_smc);
1113 }
1114
1115 /* listen worker: check prefixes */
smc_listen_prfx_check(struct smc_sock * new_smc,struct smc_clc_msg_proposal * pclc)1116 static int smc_listen_prfx_check(struct smc_sock *new_smc,
1117 struct smc_clc_msg_proposal *pclc)
1118 {
1119 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1120 struct socket *newclcsock = new_smc->clcsock;
1121
1122 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1123 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
1124 return SMC_CLC_DECL_DIFFPREFIX;
1125
1126 return 0;
1127 }
1128
1129 /* listen worker: initialize connection and buffers */
smc_listen_rdma_init(struct smc_sock * new_smc,struct smc_init_info * ini)1130 static int smc_listen_rdma_init(struct smc_sock *new_smc,
1131 struct smc_init_info *ini)
1132 {
1133 int rc;
1134
1135 /* allocate connection / link group */
1136 rc = smc_conn_create(new_smc, ini);
1137 if (rc)
1138 return rc;
1139
1140 /* create send buffer and rmb */
1141 if (smc_buf_create(new_smc, false))
1142 return SMC_CLC_DECL_MEM;
1143
1144 return 0;
1145 }
1146
1147 /* listen worker: initialize connection and buffers for SMC-D */
smc_listen_ism_init(struct smc_sock * new_smc,struct smc_clc_msg_proposal * pclc,struct smc_init_info * ini)1148 static int smc_listen_ism_init(struct smc_sock *new_smc,
1149 struct smc_clc_msg_proposal *pclc,
1150 struct smc_init_info *ini)
1151 {
1152 struct smc_clc_msg_smcd *pclc_smcd;
1153 int rc;
1154
1155 pclc_smcd = smc_get_clc_msg_smcd(pclc);
1156 ini->ism_gid = pclc_smcd->gid;
1157 rc = smc_conn_create(new_smc, ini);
1158 if (rc)
1159 return rc;
1160
1161 /* Check if peer can be reached via ISM device */
1162 if (smc_ism_cantalk(new_smc->conn.lgr->peer_gid,
1163 new_smc->conn.lgr->vlan_id,
1164 new_smc->conn.lgr->smcd)) {
1165 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
1166 smc_lgr_forget(new_smc->conn.lgr);
1167 smc_conn_free(&new_smc->conn);
1168 return SMC_CLC_DECL_SMCDNOTALK;
1169 }
1170
1171 /* Create send and receive buffers */
1172 if (smc_buf_create(new_smc, true)) {
1173 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
1174 smc_lgr_forget(new_smc->conn.lgr);
1175 smc_conn_free(&new_smc->conn);
1176 return SMC_CLC_DECL_MEM;
1177 }
1178
1179 return 0;
1180 }
1181
1182 /* listen worker: register buffers */
smc_listen_rdma_reg(struct smc_sock * new_smc,int local_contact)1183 static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
1184 {
1185 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1186
1187 if (local_contact != SMC_FIRST_CONTACT) {
1188 if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true))
1189 return SMC_CLC_DECL_ERR_REGRMB;
1190 }
1191 smc_rmb_sync_sg_for_device(&new_smc->conn);
1192
1193 return 0;
1194 }
1195
1196 /* listen worker: finish RDMA setup */
smc_listen_rdma_finish(struct smc_sock * new_smc,struct smc_clc_msg_accept_confirm * cclc,int local_contact)1197 static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1198 struct smc_clc_msg_accept_confirm *cclc,
1199 int local_contact)
1200 {
1201 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1202 int reason_code = 0;
1203
1204 if (local_contact == SMC_FIRST_CONTACT)
1205 smc_link_save_peer_info(link, cclc);
1206
1207 if (smc_rmb_rtoken_handling(&new_smc->conn, cclc)) {
1208 reason_code = SMC_CLC_DECL_ERR_RTOK;
1209 goto decline;
1210 }
1211
1212 if (local_contact == SMC_FIRST_CONTACT) {
1213 if (smc_ib_ready_link(link)) {
1214 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
1215 goto decline;
1216 }
1217 /* QP confirmation over RoCE fabric */
1218 reason_code = smc_serv_conf_first_link(new_smc);
1219 if (reason_code)
1220 goto decline;
1221 }
1222 return 0;
1223
1224 decline:
1225 smc_listen_decline(new_smc, reason_code, local_contact);
1226 return reason_code;
1227 }
1228
1229 /* setup for RDMA connection of server */
smc_listen_work(struct work_struct * work)1230 static void smc_listen_work(struct work_struct *work)
1231 {
1232 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1233 smc_listen_work);
1234 struct socket *newclcsock = new_smc->clcsock;
1235 struct smc_clc_msg_accept_confirm cclc;
1236 struct smc_clc_msg_proposal *pclc;
1237 struct smc_init_info ini = {0};
1238 bool ism_supported = false;
1239 u8 buf[SMC_CLC_MAX_LEN];
1240 int rc = 0;
1241
1242 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1243 return smc_listen_out_err(new_smc);
1244
1245 if (new_smc->use_fallback) {
1246 smc_listen_out_connected(new_smc);
1247 return;
1248 }
1249
1250 /* check if peer is smc capable */
1251 if (!tcp_sk(newclcsock->sk)->syn_smc) {
1252 smc_switch_to_fallback(new_smc);
1253 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
1254 smc_listen_out_connected(new_smc);
1255 return;
1256 }
1257
1258 /* do inband token exchange -
1259 * wait for and receive SMC Proposal CLC message
1260 */
1261 pclc = (struct smc_clc_msg_proposal *)&buf;
1262 rc = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
1263 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
1264 if (rc)
1265 goto out_decl;
1266
1267 /* IPSec connections opt out of SMC-R optimizations */
1268 if (using_ipsec(new_smc)) {
1269 rc = SMC_CLC_DECL_IPSEC;
1270 goto out_decl;
1271 }
1272
1273 /* check for matching IP prefix and subnet length */
1274 rc = smc_listen_prfx_check(new_smc, pclc);
1275 if (rc)
1276 goto out_decl;
1277
1278 /* get vlan id from IP device */
1279 if (smc_vlan_by_tcpsk(new_smc->clcsock, &ini)) {
1280 rc = SMC_CLC_DECL_GETVLANERR;
1281 goto out_decl;
1282 }
1283
1284 mutex_lock(&smc_server_lgr_pending);
1285 smc_close_init(new_smc);
1286 smc_rx_init(new_smc);
1287 smc_tx_init(new_smc);
1288
1289 /* check if ISM is available */
1290 if (pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) {
1291 ini.is_smcd = true; /* prepare ISM check */
1292 rc = smc_find_ism_device(new_smc, &ini);
1293 if (!rc)
1294 rc = smc_listen_ism_init(new_smc, pclc, &ini);
1295 if (!rc)
1296 ism_supported = true;
1297 else if (pclc->hdr.path == SMC_TYPE_D)
1298 goto out_unlock; /* skip RDMA and decline */
1299 }
1300
1301 /* check if RDMA is available */
1302 if (!ism_supported) { /* SMC_TYPE_R or SMC_TYPE_B */
1303 /* prepare RDMA check */
1304 ini.is_smcd = false;
1305 ini.ism_dev = NULL;
1306 ini.ib_lcl = &pclc->lcl;
1307 rc = smc_find_rdma_device(new_smc, &ini);
1308 if (rc) {
1309 /* no RDMA device found */
1310 if (pclc->hdr.path == SMC_TYPE_B)
1311 /* neither ISM nor RDMA device found */
1312 rc = SMC_CLC_DECL_NOSMCDEV;
1313 goto out_unlock;
1314 }
1315 rc = smc_listen_rdma_init(new_smc, &ini);
1316 if (rc)
1317 goto out_unlock;
1318 rc = smc_listen_rdma_reg(new_smc, ini.cln_first_contact);
1319 if (rc)
1320 goto out_unlock;
1321 }
1322
1323 /* send SMC Accept CLC message */
1324 rc = smc_clc_send_accept(new_smc, ini.cln_first_contact);
1325 if (rc)
1326 goto out_unlock;
1327
1328 /* SMC-D does not need this lock any more */
1329 if (ism_supported)
1330 mutex_unlock(&smc_server_lgr_pending);
1331
1332 /* receive SMC Confirm CLC message */
1333 rc = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
1334 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
1335 if (rc) {
1336 if (!ism_supported)
1337 goto out_unlock;
1338 goto out_decl;
1339 }
1340
1341 /* finish worker */
1342 if (!ism_supported) {
1343 rc = smc_listen_rdma_finish(new_smc, &cclc,
1344 ini.cln_first_contact);
1345 mutex_unlock(&smc_server_lgr_pending);
1346 if (rc)
1347 return;
1348 }
1349 smc_conn_save_peer_info(new_smc, &cclc);
1350 smc_listen_out_connected(new_smc);
1351 return;
1352
1353 out_unlock:
1354 mutex_unlock(&smc_server_lgr_pending);
1355 out_decl:
1356 smc_listen_decline(new_smc, rc, ini.cln_first_contact);
1357 }
1358
smc_tcp_listen_work(struct work_struct * work)1359 static void smc_tcp_listen_work(struct work_struct *work)
1360 {
1361 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1362 tcp_listen_work);
1363 struct sock *lsk = &lsmc->sk;
1364 struct smc_sock *new_smc;
1365 int rc = 0;
1366
1367 lock_sock(lsk);
1368 while (lsk->sk_state == SMC_LISTEN) {
1369 rc = smc_clcsock_accept(lsmc, &new_smc);
1370 if (rc)
1371 goto out;
1372 if (!new_smc)
1373 continue;
1374
1375 new_smc->listen_smc = lsmc;
1376 new_smc->use_fallback = lsmc->use_fallback;
1377 new_smc->fallback_rsn = lsmc->fallback_rsn;
1378 sock_hold(lsk); /* sock_put in smc_listen_work */
1379 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1380 smc_copy_sock_settings_to_smc(new_smc);
1381 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1382 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
1383 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1384 if (!schedule_work(&new_smc->smc_listen_work))
1385 sock_put(&new_smc->sk);
1386 }
1387
1388 out:
1389 release_sock(lsk);
1390 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
1391 }
1392
smc_listen(struct socket * sock,int backlog)1393 static int smc_listen(struct socket *sock, int backlog)
1394 {
1395 struct sock *sk = sock->sk;
1396 struct smc_sock *smc;
1397 int rc;
1398
1399 smc = smc_sk(sk);
1400 lock_sock(sk);
1401
1402 rc = -EINVAL;
1403 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
1404 smc->connect_nonblock)
1405 goto out;
1406
1407 rc = 0;
1408 if (sk->sk_state == SMC_LISTEN) {
1409 sk->sk_max_ack_backlog = backlog;
1410 goto out;
1411 }
1412 /* some socket options are handled in core, so we could not apply
1413 * them to the clc socket -- copy smc socket options to clc socket
1414 */
1415 smc_copy_sock_settings_to_clc(smc);
1416 if (!smc->use_fallback)
1417 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
1418
1419 rc = kernel_listen(smc->clcsock, backlog);
1420 if (rc)
1421 goto out;
1422 sk->sk_max_ack_backlog = backlog;
1423 sk->sk_ack_backlog = 0;
1424 sk->sk_state = SMC_LISTEN;
1425 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1426 if (!schedule_work(&smc->tcp_listen_work))
1427 sock_put(sk);
1428
1429 out:
1430 release_sock(sk);
1431 return rc;
1432 }
1433
smc_accept(struct socket * sock,struct socket * new_sock,int flags,bool kern)1434 static int smc_accept(struct socket *sock, struct socket *new_sock,
1435 int flags, bool kern)
1436 {
1437 struct sock *sk = sock->sk, *nsk;
1438 DECLARE_WAITQUEUE(wait, current);
1439 struct smc_sock *lsmc;
1440 long timeo;
1441 int rc = 0;
1442
1443 lsmc = smc_sk(sk);
1444 sock_hold(sk); /* sock_put below */
1445 lock_sock(sk);
1446
1447 if (lsmc->sk.sk_state != SMC_LISTEN) {
1448 rc = -EINVAL;
1449 release_sock(sk);
1450 goto out;
1451 }
1452
1453 /* Wait for an incoming connection */
1454 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1455 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1456 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1457 set_current_state(TASK_INTERRUPTIBLE);
1458 if (!timeo) {
1459 rc = -EAGAIN;
1460 break;
1461 }
1462 release_sock(sk);
1463 timeo = schedule_timeout(timeo);
1464 /* wakeup by sk_data_ready in smc_listen_work() */
1465 sched_annotate_sleep();
1466 lock_sock(sk);
1467 if (signal_pending(current)) {
1468 rc = sock_intr_errno(timeo);
1469 break;
1470 }
1471 }
1472 set_current_state(TASK_RUNNING);
1473 remove_wait_queue(sk_sleep(sk), &wait);
1474
1475 if (!rc)
1476 rc = sock_error(nsk);
1477 release_sock(sk);
1478 if (rc)
1479 goto out;
1480
1481 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1482 /* wait till data arrives on the socket */
1483 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1484 MSEC_PER_SEC);
1485 if (smc_sk(nsk)->use_fallback) {
1486 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1487
1488 lock_sock(clcsk);
1489 if (skb_queue_empty(&clcsk->sk_receive_queue))
1490 sk_wait_data(clcsk, &timeo, NULL);
1491 release_sock(clcsk);
1492 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1493 lock_sock(nsk);
1494 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
1495 release_sock(nsk);
1496 }
1497 }
1498
1499 out:
1500 sock_put(sk); /* sock_hold above */
1501 return rc;
1502 }
1503
smc_getname(struct socket * sock,struct sockaddr * addr,int peer)1504 static int smc_getname(struct socket *sock, struct sockaddr *addr,
1505 int peer)
1506 {
1507 struct smc_sock *smc;
1508
1509 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1510 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
1511 return -ENOTCONN;
1512
1513 smc = smc_sk(sock->sk);
1514
1515 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
1516 }
1517
smc_sendmsg(struct socket * sock,struct msghdr * msg,size_t len)1518 static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1519 {
1520 struct sock *sk = sock->sk;
1521 struct smc_sock *smc;
1522 int rc = -EPIPE;
1523
1524 smc = smc_sk(sk);
1525 lock_sock(sk);
1526 if ((sk->sk_state != SMC_ACTIVE) &&
1527 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1528 (sk->sk_state != SMC_INIT))
1529 goto out;
1530
1531 if (msg->msg_flags & MSG_FASTOPEN) {
1532 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
1533 smc_switch_to_fallback(smc);
1534 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
1535 } else {
1536 rc = -EINVAL;
1537 goto out;
1538 }
1539 }
1540
1541 if (smc->use_fallback)
1542 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1543 else
1544 rc = smc_tx_sendmsg(smc, msg, len);
1545 out:
1546 release_sock(sk);
1547 return rc;
1548 }
1549
smc_recvmsg(struct socket * sock,struct msghdr * msg,size_t len,int flags)1550 static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1551 int flags)
1552 {
1553 struct sock *sk = sock->sk;
1554 struct smc_sock *smc;
1555 int rc = -ENOTCONN;
1556
1557 smc = smc_sk(sk);
1558 lock_sock(sk);
1559 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1560 /* socket was connected before, no more data to read */
1561 rc = 0;
1562 goto out;
1563 }
1564 if ((sk->sk_state == SMC_INIT) ||
1565 (sk->sk_state == SMC_LISTEN) ||
1566 (sk->sk_state == SMC_CLOSED))
1567 goto out;
1568
1569 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1570 rc = 0;
1571 goto out;
1572 }
1573
1574 if (smc->use_fallback) {
1575 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
1576 } else {
1577 msg->msg_namelen = 0;
1578 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1579 }
1580
1581 out:
1582 release_sock(sk);
1583 return rc;
1584 }
1585
smc_accept_poll(struct sock * parent)1586 static __poll_t smc_accept_poll(struct sock *parent)
1587 {
1588 struct smc_sock *isk = smc_sk(parent);
1589 __poll_t mask = 0;
1590
1591 spin_lock(&isk->accept_q_lock);
1592 if (!list_empty(&isk->accept_q))
1593 mask = EPOLLIN | EPOLLRDNORM;
1594 spin_unlock(&isk->accept_q_lock);
1595
1596 return mask;
1597 }
1598
smc_poll(struct file * file,struct socket * sock,poll_table * wait)1599 static __poll_t smc_poll(struct file *file, struct socket *sock,
1600 poll_table *wait)
1601 {
1602 struct sock *sk = sock->sk;
1603 struct smc_sock *smc;
1604 __poll_t mask = 0;
1605
1606 if (!sk)
1607 return EPOLLNVAL;
1608
1609 smc = smc_sk(sock->sk);
1610 if (smc->use_fallback) {
1611 /* delegate to CLC child sock */
1612 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
1613 sk->sk_err = smc->clcsock->sk->sk_err;
1614 } else {
1615 if (sk->sk_state != SMC_CLOSED)
1616 sock_poll_wait(file, sock, wait);
1617 if (sk->sk_err)
1618 mask |= EPOLLERR;
1619 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1620 (sk->sk_state == SMC_CLOSED))
1621 mask |= EPOLLHUP;
1622 if (sk->sk_state == SMC_LISTEN) {
1623 /* woken up by sk_data_ready in smc_listen_work() */
1624 mask |= smc_accept_poll(sk);
1625 } else if (smc->use_fallback) { /* as result of connect_work()*/
1626 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
1627 wait);
1628 sk->sk_err = smc->clcsock->sk->sk_err;
1629 } else {
1630 if ((sk->sk_state != SMC_INIT &&
1631 atomic_read(&smc->conn.sndbuf_space)) ||
1632 sk->sk_shutdown & SEND_SHUTDOWN) {
1633 mask |= EPOLLOUT | EPOLLWRNORM;
1634 } else {
1635 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1636 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1637 }
1638 if (atomic_read(&smc->conn.bytes_to_rcv))
1639 mask |= EPOLLIN | EPOLLRDNORM;
1640 if (sk->sk_shutdown & RCV_SHUTDOWN)
1641 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
1642 if (sk->sk_state == SMC_APPCLOSEWAIT1)
1643 mask |= EPOLLIN;
1644 if (smc->conn.urg_state == SMC_URG_VALID)
1645 mask |= EPOLLPRI;
1646 }
1647 }
1648
1649 return mask;
1650 }
1651
smc_shutdown(struct socket * sock,int how)1652 static int smc_shutdown(struct socket *sock, int how)
1653 {
1654 struct sock *sk = sock->sk;
1655 struct smc_sock *smc;
1656 int rc = -EINVAL;
1657 int rc1 = 0;
1658
1659 smc = smc_sk(sk);
1660
1661 if ((how < SHUT_RD) || (how > SHUT_RDWR))
1662 return rc;
1663
1664 lock_sock(sk);
1665
1666 rc = -ENOTCONN;
1667 if ((sk->sk_state != SMC_ACTIVE) &&
1668 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1669 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1670 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1671 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1672 (sk->sk_state != SMC_APPFINCLOSEWAIT))
1673 goto out;
1674 if (smc->use_fallback) {
1675 rc = kernel_sock_shutdown(smc->clcsock, how);
1676 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1677 if (sk->sk_shutdown == SHUTDOWN_MASK)
1678 sk->sk_state = SMC_CLOSED;
1679 goto out;
1680 }
1681 switch (how) {
1682 case SHUT_RDWR: /* shutdown in both directions */
1683 rc = smc_close_active(smc);
1684 break;
1685 case SHUT_WR:
1686 rc = smc_close_shutdown_write(smc);
1687 break;
1688 case SHUT_RD:
1689 rc = 0;
1690 /* nothing more to do because peer is not involved */
1691 break;
1692 }
1693 if (smc->clcsock)
1694 rc1 = kernel_sock_shutdown(smc->clcsock, how);
1695 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1696 sk->sk_shutdown |= how + 1;
1697
1698 out:
1699 release_sock(sk);
1700 return rc ? rc : rc1;
1701 }
1702
smc_setsockopt(struct socket * sock,int level,int optname,char __user * optval,unsigned int optlen)1703 static int smc_setsockopt(struct socket *sock, int level, int optname,
1704 char __user *optval, unsigned int optlen)
1705 {
1706 struct sock *sk = sock->sk;
1707 struct smc_sock *smc;
1708 int val, rc;
1709
1710 smc = smc_sk(sk);
1711
1712 /* generic setsockopts reaching us here always apply to the
1713 * CLC socket
1714 */
1715 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1716 optval, optlen);
1717 if (smc->clcsock->sk->sk_err) {
1718 sk->sk_err = smc->clcsock->sk->sk_err;
1719 sk->sk_error_report(sk);
1720 }
1721
1722 if (optlen < sizeof(int))
1723 return -EINVAL;
1724 if (get_user(val, (int __user *)optval))
1725 return -EFAULT;
1726
1727 lock_sock(sk);
1728 if (rc || smc->use_fallback)
1729 goto out;
1730 switch (optname) {
1731 case TCP_ULP:
1732 case TCP_FASTOPEN:
1733 case TCP_FASTOPEN_CONNECT:
1734 case TCP_FASTOPEN_KEY:
1735 case TCP_FASTOPEN_NO_COOKIE:
1736 /* option not supported by SMC */
1737 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
1738 smc_switch_to_fallback(smc);
1739 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
1740 } else {
1741 rc = -EINVAL;
1742 }
1743 break;
1744 case TCP_NODELAY:
1745 if (sk->sk_state != SMC_INIT &&
1746 sk->sk_state != SMC_LISTEN &&
1747 sk->sk_state != SMC_CLOSED) {
1748 if (val)
1749 mod_delayed_work(system_wq, &smc->conn.tx_work,
1750 0);
1751 }
1752 break;
1753 case TCP_CORK:
1754 if (sk->sk_state != SMC_INIT &&
1755 sk->sk_state != SMC_LISTEN &&
1756 sk->sk_state != SMC_CLOSED) {
1757 if (!val)
1758 mod_delayed_work(system_wq, &smc->conn.tx_work,
1759 0);
1760 }
1761 break;
1762 case TCP_DEFER_ACCEPT:
1763 smc->sockopt_defer_accept = val;
1764 break;
1765 default:
1766 break;
1767 }
1768 out:
1769 release_sock(sk);
1770
1771 return rc;
1772 }
1773
smc_getsockopt(struct socket * sock,int level,int optname,char __user * optval,int __user * optlen)1774 static int smc_getsockopt(struct socket *sock, int level, int optname,
1775 char __user *optval, int __user *optlen)
1776 {
1777 struct smc_sock *smc;
1778
1779 smc = smc_sk(sock->sk);
1780 /* socket options apply to the CLC socket */
1781 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1782 optval, optlen);
1783 }
1784
smc_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)1785 static int smc_ioctl(struct socket *sock, unsigned int cmd,
1786 unsigned long arg)
1787 {
1788 union smc_host_cursor cons, urg;
1789 struct smc_connection *conn;
1790 struct smc_sock *smc;
1791 int answ;
1792
1793 smc = smc_sk(sock->sk);
1794 conn = &smc->conn;
1795 lock_sock(&smc->sk);
1796 if (smc->use_fallback) {
1797 if (!smc->clcsock) {
1798 release_sock(&smc->sk);
1799 return -EBADF;
1800 }
1801 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1802 release_sock(&smc->sk);
1803 return answ;
1804 }
1805 switch (cmd) {
1806 case SIOCINQ: /* same as FIONREAD */
1807 if (smc->sk.sk_state == SMC_LISTEN) {
1808 release_sock(&smc->sk);
1809 return -EINVAL;
1810 }
1811 if (smc->sk.sk_state == SMC_INIT ||
1812 smc->sk.sk_state == SMC_CLOSED)
1813 answ = 0;
1814 else
1815 answ = atomic_read(&smc->conn.bytes_to_rcv);
1816 break;
1817 case SIOCOUTQ:
1818 /* output queue size (not send + not acked) */
1819 if (smc->sk.sk_state == SMC_LISTEN) {
1820 release_sock(&smc->sk);
1821 return -EINVAL;
1822 }
1823 if (smc->sk.sk_state == SMC_INIT ||
1824 smc->sk.sk_state == SMC_CLOSED)
1825 answ = 0;
1826 else
1827 answ = smc->conn.sndbuf_desc->len -
1828 atomic_read(&smc->conn.sndbuf_space);
1829 break;
1830 case SIOCOUTQNSD:
1831 /* output queue size (not send only) */
1832 if (smc->sk.sk_state == SMC_LISTEN) {
1833 release_sock(&smc->sk);
1834 return -EINVAL;
1835 }
1836 if (smc->sk.sk_state == SMC_INIT ||
1837 smc->sk.sk_state == SMC_CLOSED)
1838 answ = 0;
1839 else
1840 answ = smc_tx_prepared_sends(&smc->conn);
1841 break;
1842 case SIOCATMARK:
1843 if (smc->sk.sk_state == SMC_LISTEN) {
1844 release_sock(&smc->sk);
1845 return -EINVAL;
1846 }
1847 if (smc->sk.sk_state == SMC_INIT ||
1848 smc->sk.sk_state == SMC_CLOSED) {
1849 answ = 0;
1850 } else {
1851 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1852 smc_curs_copy(&urg, &conn->urg_curs, conn);
1853 answ = smc_curs_diff(conn->rmb_desc->len,
1854 &cons, &urg) == 1;
1855 }
1856 break;
1857 default:
1858 release_sock(&smc->sk);
1859 return -ENOIOCTLCMD;
1860 }
1861 release_sock(&smc->sk);
1862
1863 return put_user(answ, (int __user *)arg);
1864 }
1865
smc_sendpage(struct socket * sock,struct page * page,int offset,size_t size,int flags)1866 static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1867 int offset, size_t size, int flags)
1868 {
1869 struct sock *sk = sock->sk;
1870 struct smc_sock *smc;
1871 int rc = -EPIPE;
1872
1873 smc = smc_sk(sk);
1874 lock_sock(sk);
1875 if (sk->sk_state != SMC_ACTIVE) {
1876 release_sock(sk);
1877 goto out;
1878 }
1879 release_sock(sk);
1880 if (smc->use_fallback)
1881 rc = kernel_sendpage(smc->clcsock, page, offset,
1882 size, flags);
1883 else
1884 rc = sock_no_sendpage(sock, page, offset, size, flags);
1885
1886 out:
1887 return rc;
1888 }
1889
1890 /* Map the affected portions of the rmbe into an spd, note the number of bytes
1891 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1892 * updates till whenever a respective page has been fully processed.
1893 * Note that subsequent recv() calls have to wait till all splice() processing
1894 * completed.
1895 */
smc_splice_read(struct socket * sock,loff_t * ppos,struct pipe_inode_info * pipe,size_t len,unsigned int flags)1896 static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1897 struct pipe_inode_info *pipe, size_t len,
1898 unsigned int flags)
1899 {
1900 struct sock *sk = sock->sk;
1901 struct smc_sock *smc;
1902 int rc = -ENOTCONN;
1903
1904 smc = smc_sk(sk);
1905 lock_sock(sk);
1906 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1907 /* socket was connected before, no more data to read */
1908 rc = 0;
1909 goto out;
1910 }
1911 if (sk->sk_state == SMC_INIT ||
1912 sk->sk_state == SMC_LISTEN ||
1913 sk->sk_state == SMC_CLOSED)
1914 goto out;
1915
1916 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1917 rc = 0;
1918 goto out;
1919 }
1920
1921 if (smc->use_fallback) {
1922 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1923 pipe, len, flags);
1924 } else {
1925 if (*ppos) {
1926 rc = -ESPIPE;
1927 goto out;
1928 }
1929 if (flags & SPLICE_F_NONBLOCK)
1930 flags = MSG_DONTWAIT;
1931 else
1932 flags = 0;
1933 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
1934 }
1935 out:
1936 release_sock(sk);
1937
1938 return rc;
1939 }
1940
1941 /* must look like tcp */
1942 static const struct proto_ops smc_sock_ops = {
1943 .family = PF_SMC,
1944 .owner = THIS_MODULE,
1945 .release = smc_release,
1946 .bind = smc_bind,
1947 .connect = smc_connect,
1948 .socketpair = sock_no_socketpair,
1949 .accept = smc_accept,
1950 .getname = smc_getname,
1951 .poll = smc_poll,
1952 .ioctl = smc_ioctl,
1953 .listen = smc_listen,
1954 .shutdown = smc_shutdown,
1955 .setsockopt = smc_setsockopt,
1956 .getsockopt = smc_getsockopt,
1957 .sendmsg = smc_sendmsg,
1958 .recvmsg = smc_recvmsg,
1959 .mmap = sock_no_mmap,
1960 .sendpage = smc_sendpage,
1961 .splice_read = smc_splice_read,
1962 };
1963
smc_create(struct net * net,struct socket * sock,int protocol,int kern)1964 static int smc_create(struct net *net, struct socket *sock, int protocol,
1965 int kern)
1966 {
1967 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
1968 struct smc_sock *smc;
1969 struct sock *sk;
1970 int rc;
1971
1972 rc = -ESOCKTNOSUPPORT;
1973 if (sock->type != SOCK_STREAM)
1974 goto out;
1975
1976 rc = -EPROTONOSUPPORT;
1977 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
1978 goto out;
1979
1980 rc = -ENOBUFS;
1981 sock->ops = &smc_sock_ops;
1982 sk = smc_sock_alloc(net, sock, protocol);
1983 if (!sk)
1984 goto out;
1985
1986 /* create internal TCP socket for CLC handshake and fallback */
1987 smc = smc_sk(sk);
1988 smc->use_fallback = false; /* assume rdma capability first */
1989 smc->fallback_rsn = 0;
1990 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1991 &smc->clcsock);
1992 if (rc) {
1993 sk_common_release(sk);
1994 goto out;
1995 }
1996 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1997 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
1998
1999 out:
2000 return rc;
2001 }
2002
2003 static const struct net_proto_family smc_sock_family_ops = {
2004 .family = PF_SMC,
2005 .owner = THIS_MODULE,
2006 .create = smc_create,
2007 };
2008
2009 unsigned int smc_net_id;
2010
smc_net_init(struct net * net)2011 static __net_init int smc_net_init(struct net *net)
2012 {
2013 return smc_pnet_net_init(net);
2014 }
2015
smc_net_exit(struct net * net)2016 static void __net_exit smc_net_exit(struct net *net)
2017 {
2018 smc_pnet_net_exit(net);
2019 }
2020
2021 static struct pernet_operations smc_net_ops = {
2022 .init = smc_net_init,
2023 .exit = smc_net_exit,
2024 .id = &smc_net_id,
2025 .size = sizeof(struct smc_net),
2026 };
2027
smc_init(void)2028 static int __init smc_init(void)
2029 {
2030 int rc;
2031
2032 rc = register_pernet_subsys(&smc_net_ops);
2033 if (rc)
2034 return rc;
2035
2036 rc = smc_pnet_init();
2037 if (rc)
2038 goto out_pernet_subsys;
2039
2040 rc = smc_llc_init();
2041 if (rc) {
2042 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
2043 goto out_pnet;
2044 }
2045
2046 rc = smc_cdc_init();
2047 if (rc) {
2048 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
2049 goto out_pnet;
2050 }
2051
2052 rc = proto_register(&smc_proto, 1);
2053 if (rc) {
2054 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
2055 goto out_pnet;
2056 }
2057
2058 rc = proto_register(&smc_proto6, 1);
2059 if (rc) {
2060 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2061 goto out_proto;
2062 }
2063
2064 rc = sock_register(&smc_sock_family_ops);
2065 if (rc) {
2066 pr_err("%s: sock_register fails with %d\n", __func__, rc);
2067 goto out_proto6;
2068 }
2069 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
2070 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
2071
2072 rc = smc_ib_register_client();
2073 if (rc) {
2074 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2075 goto out_sock;
2076 }
2077
2078 static_branch_enable(&tcp_have_smc);
2079 return 0;
2080
2081 out_sock:
2082 sock_unregister(PF_SMC);
2083 out_proto6:
2084 proto_unregister(&smc_proto6);
2085 out_proto:
2086 proto_unregister(&smc_proto);
2087 out_pnet:
2088 smc_pnet_exit();
2089 out_pernet_subsys:
2090 unregister_pernet_subsys(&smc_net_ops);
2091
2092 return rc;
2093 }
2094
smc_exit(void)2095 static void __exit smc_exit(void)
2096 {
2097 smc_core_exit();
2098 static_branch_disable(&tcp_have_smc);
2099 smc_ib_unregister_client();
2100 sock_unregister(PF_SMC);
2101 proto_unregister(&smc_proto6);
2102 proto_unregister(&smc_proto);
2103 smc_pnet_exit();
2104 unregister_pernet_subsys(&smc_net_ops);
2105 }
2106
2107 module_init(smc_init);
2108 module_exit(smc_exit);
2109
2110 MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2111 MODULE_DESCRIPTION("smc socket address family");
2112 MODULE_LICENSE("GPL");
2113 MODULE_ALIAS_NETPROTO(PF_SMC);
2114