]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/smc/af_smc.c
net/smc: rename connection index to RMBE index
[mirror_ubuntu-jammy-kernel.git] / net / smc / af_smc.c
CommitLineData
ac713874
UB
1/*
2 * Shared Memory Communications over RDMA (SMC-R) and RoCE
3 *
4 * AF_SMC protocol family socket handler keeping the AF_INET sock address type
5 * applies to SOCK_STREAM sockets only
6 * offers an alternative communication option for TCP-protocol sockets
7 * applicable with RoCE-cards only
8 *
a046d57d 9 * Initial restrictions:
a046d57d
UB
10 * - support for alternate links postponed
11 * - partial support for non-blocking sockets only
12 * - support for urgent data postponed
13 *
aaa4d33f 14 * Copyright IBM Corp. 2016, 2018
ac713874
UB
15 *
16 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
17 * based on prototype from Frank Blaschka
18 */
19
20#define KMSG_COMPONENT "smc"
21#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
22
23#include <linux/module.h>
24#include <linux/socket.h>
a046d57d 25#include <linux/workqueue.h>
5f08318f 26#include <linux/in.h>
c3edc401
IM
27#include <linux/sched/signal.h>
28
ac713874 29#include <net/sock.h>
a046d57d 30#include <net/tcp.h>
f16a7dd5 31#include <net/smc.h>
9b67e26f 32#include <asm/ioctls.h>
ac713874
UB
33
34#include "smc.h"
a046d57d 35#include "smc_clc.h"
9bf9abea 36#include "smc_llc.h"
5f08318f 37#include "smc_cdc.h"
0cfdd8f9 38#include "smc_core.h"
a4cf0443 39#include "smc_ib.h"
6812baab 40#include "smc_pnet.h"
e6727f39 41#include "smc_tx.h"
952310cc 42#include "smc_rx.h"
b38d7324 43#include "smc_close.h"
ac713874 44
0cfdd8f9
UB
45static DEFINE_MUTEX(smc_create_lgr_pending); /* serialize link group
46 * creation
47 */
48
a046d57d
UB
49static void smc_tcp_listen_work(struct work_struct *);
50
ac713874
UB
51static void smc_set_keepalive(struct sock *sk, int val)
52{
53 struct smc_sock *smc = smc_sk(sk);
54
55 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
56}
57
f16a7dd5
UB
58static struct smc_hashinfo smc_v4_hashinfo = {
59 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
60};
61
aaa4d33f
KG
62static struct smc_hashinfo smc_v6_hashinfo = {
63 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
64};
65
f16a7dd5
UB
66int smc_hash_sk(struct sock *sk)
67{
68 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
69 struct hlist_head *head;
70
71 head = &h->ht;
72
73 write_lock_bh(&h->lock);
74 sk_add_node(sk, head);
75 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
76 write_unlock_bh(&h->lock);
77
78 return 0;
79}
80EXPORT_SYMBOL_GPL(smc_hash_sk);
81
82void smc_unhash_sk(struct sock *sk)
83{
84 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
85
86 write_lock_bh(&h->lock);
87 if (sk_del_node_init(sk))
88 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
89 write_unlock_bh(&h->lock);
90}
91EXPORT_SYMBOL_GPL(smc_unhash_sk);
92
93struct proto smc_proto = {
ac713874
UB
94 .name = "SMC",
95 .owner = THIS_MODULE,
96 .keepalive = smc_set_keepalive,
f16a7dd5
UB
97 .hash = smc_hash_sk,
98 .unhash = smc_unhash_sk,
ac713874 99 .obj_size = sizeof(struct smc_sock),
f16a7dd5 100 .h.smc_hash = &smc_v4_hashinfo,
5f0d5a3a 101 .slab_flags = SLAB_TYPESAFE_BY_RCU,
ac713874 102};
f16a7dd5 103EXPORT_SYMBOL_GPL(smc_proto);
ac713874 104
aaa4d33f
KG
105struct proto smc_proto6 = {
106 .name = "SMC6",
107 .owner = THIS_MODULE,
108 .keepalive = smc_set_keepalive,
109 .hash = smc_hash_sk,
110 .unhash = smc_unhash_sk,
111 .obj_size = sizeof(struct smc_sock),
112 .h.smc_hash = &smc_v6_hashinfo,
113 .slab_flags = SLAB_TYPESAFE_BY_RCU,
114};
115EXPORT_SYMBOL_GPL(smc_proto6);
116
ac713874
UB
117static int smc_release(struct socket *sock)
118{
119 struct sock *sk = sock->sk;
120 struct smc_sock *smc;
b38d7324 121 int rc = 0;
ac713874
UB
122
123 if (!sk)
124 goto out;
125
126 smc = smc_sk(sk);
b38d7324
UB
127 if (sk->sk_state == SMC_LISTEN)
128 /* smc_close_non_accepted() is called and acquires
129 * sock lock for child sockets again
130 */
131 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
132 else
133 lock_sock(sk);
ac713874 134
51f1de79 135 if (!smc->use_fallback) {
b38d7324
UB
136 rc = smc_close_active(smc);
137 sock_set_flag(sk, SOCK_DEAD);
138 sk->sk_shutdown |= SHUTDOWN_MASK;
139 }
ac713874
UB
140 if (smc->clcsock) {
141 sock_release(smc->clcsock);
142 smc->clcsock = NULL;
143 }
51f1de79
UB
144 if (smc->use_fallback) {
145 sock_put(sk); /* passive closing */
146 sk->sk_state = SMC_CLOSED;
147 sk->sk_state_change(sk);
148 }
ac713874
UB
149
150 /* detach socket */
151 sock_orphan(sk);
152 sock->sk = NULL;
51f1de79 153 if (!smc->use_fallback && sk->sk_state == SMC_CLOSED)
b38d7324 154 smc_conn_free(&smc->conn);
ac713874
UB
155 release_sock(sk);
156
51f1de79
UB
157 sk->sk_prot->unhash(sk);
158 sock_put(sk); /* final sock_put */
ac713874 159out:
b38d7324 160 return rc;
ac713874
UB
161}
162
163static void smc_destruct(struct sock *sk)
164{
165 if (sk->sk_state != SMC_CLOSED)
166 return;
167 if (!sock_flag(sk, SOCK_DEAD))
168 return;
169
170 sk_refcnt_debug_dec(sk);
171}
172
aaa4d33f
KG
173static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
174 int protocol)
ac713874
UB
175{
176 struct smc_sock *smc;
aaa4d33f 177 struct proto *prot;
ac713874
UB
178 struct sock *sk;
179
aaa4d33f
KG
180 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
181 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
ac713874
UB
182 if (!sk)
183 return NULL;
184
185 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
186 sk->sk_state = SMC_INIT;
187 sk->sk_destruct = smc_destruct;
aaa4d33f 188 sk->sk_protocol = protocol;
ac713874 189 smc = smc_sk(sk);
a046d57d 190 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
be7f3e59 191 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
a046d57d
UB
192 INIT_LIST_HEAD(&smc->accept_q);
193 spin_lock_init(&smc->accept_q_lock);
be7f3e59 194 spin_lock_init(&smc->conn.send_lock);
f16a7dd5 195 sk->sk_prot->hash(sk);
a046d57d 196 sk_refcnt_debug_inc(sk);
ac713874
UB
197
198 return sk;
199}
200
201static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
202 int addr_len)
203{
204 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
205 struct sock *sk = sock->sk;
206 struct smc_sock *smc;
207 int rc;
208
209 smc = smc_sk(sk);
210
211 /* replicate tests from inet_bind(), to be safe wrt. future changes */
212 rc = -EINVAL;
213 if (addr_len < sizeof(struct sockaddr_in))
214 goto out;
215
216 rc = -EAFNOSUPPORT;
aaa4d33f
KG
217 if (addr->sin_family != AF_INET &&
218 addr->sin_family != AF_INET6 &&
219 addr->sin_family != AF_UNSPEC)
220 goto out;
ac713874 221 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
aaa4d33f
KG
222 if (addr->sin_family == AF_UNSPEC &&
223 addr->sin_addr.s_addr != htonl(INADDR_ANY))
ac713874
UB
224 goto out;
225
226 lock_sock(sk);
227
228 /* Check if socket is already active */
229 rc = -EINVAL;
230 if (sk->sk_state != SMC_INIT)
231 goto out_rel;
232
233 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
234 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
235
236out_rel:
237 release_sock(sk);
238out:
239 return rc;
240}
241
242static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
243 unsigned long mask)
244{
245 /* options we don't get control via setsockopt for */
246 nsk->sk_type = osk->sk_type;
247 nsk->sk_sndbuf = osk->sk_sndbuf;
248 nsk->sk_rcvbuf = osk->sk_rcvbuf;
249 nsk->sk_sndtimeo = osk->sk_sndtimeo;
250 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
251 nsk->sk_mark = osk->sk_mark;
252 nsk->sk_priority = osk->sk_priority;
253 nsk->sk_rcvlowat = osk->sk_rcvlowat;
254 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
255 nsk->sk_err = osk->sk_err;
256
257 nsk->sk_flags &= ~mask;
258 nsk->sk_flags |= osk->sk_flags & mask;
259}
260
261#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
262 (1UL << SOCK_KEEPOPEN) | \
263 (1UL << SOCK_LINGER) | \
264 (1UL << SOCK_BROADCAST) | \
265 (1UL << SOCK_TIMESTAMP) | \
266 (1UL << SOCK_DBG) | \
267 (1UL << SOCK_RCVTSTAMP) | \
268 (1UL << SOCK_RCVTSTAMPNS) | \
269 (1UL << SOCK_LOCALROUTE) | \
270 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
271 (1UL << SOCK_RXQ_OVFL) | \
272 (1UL << SOCK_WIFI_STATUS) | \
273 (1UL << SOCK_NOFCS) | \
274 (1UL << SOCK_FILTER_LOCKED))
275/* copy only relevant settings and flags of SOL_SOCKET level from smc to
276 * clc socket (since smc is not called for these options from net/core)
277 */
278static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
279{
280 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
281}
282
283#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
284 (1UL << SOCK_KEEPOPEN) | \
285 (1UL << SOCK_LINGER) | \
286 (1UL << SOCK_DBG))
287/* copy only settings and flags relevant for smc from clc to smc socket */
288static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
289{
290 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
291}
292
44aa81ce
KG
293/* register a new rmb, optionally send confirm_rkey msg to register with peer */
294static int smc_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc,
295 bool conf_rkey)
e63a5f8c
KG
296{
297 /* register memory region for new rmb */
a6920d1d
KG
298 if (smc_wr_reg_send(link, rmb_desc->mr_rx[SMC_SINGLE_LINK])) {
299 rmb_desc->regerr = 1;
e63a5f8c 300 return -EFAULT;
a6920d1d 301 }
44aa81ce
KG
302 if (!conf_rkey)
303 return 0;
304 /* exchange confirm_rkey msg with peer */
305 if (smc_llc_do_confirm_rkey(link, rmb_desc)) {
306 rmb_desc->regerr = 1;
307 return -EFAULT;
308 }
e63a5f8c
KG
309 return 0;
310}
311
0f627126 312static int smc_clnt_conf_first_link(struct smc_sock *smc)
9bf9abea 313{
877ae5be 314 struct net *net = sock_net(smc->clcsock->sk);
9bf9abea
UB
315 struct smc_link_group *lgr = smc->conn.lgr;
316 struct smc_link *link;
317 int rest;
318 int rc;
319
320 link = &lgr->lnk[SMC_SINGLE_LINK];
321 /* receive CONFIRM LINK request from server over RoCE fabric */
322 rest = wait_for_completion_interruptible_timeout(
323 &link->llc_confirm,
324 SMC_LLC_WAIT_FIRST_TIME);
325 if (rest <= 0) {
326 struct smc_clc_msg_decline dclc;
327
328 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
329 SMC_CLC_DECLINE);
330 return rc;
331 }
332
75d320d6
KG
333 if (link->llc_confirm_rc)
334 return SMC_CLC_DECL_RMBE_EC;
335
9bf9abea
UB
336 rc = smc_ib_modify_qp_rts(link);
337 if (rc)
338 return SMC_CLC_DECL_INTERR;
339
340 smc_wr_remember_qp_attr(link);
652a1e41 341
44aa81ce 342 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
652a1e41
UB
343 return SMC_CLC_DECL_INTERR;
344
9bf9abea
UB
345 /* send CONFIRM LINK response over RoCE fabric */
346 rc = smc_llc_send_confirm_link(link,
347 link->smcibdev->mac[link->ibport - 1],
0f627126
SR
348 &link->smcibdev->gid[link->ibport - 1],
349 SMC_LLC_RESP);
9bf9abea
UB
350 if (rc < 0)
351 return SMC_CLC_DECL_TCL;
352
52bedf37
KG
353 /* receive ADD LINK request from server over RoCE fabric */
354 rest = wait_for_completion_interruptible_timeout(&link->llc_add,
355 SMC_LLC_WAIT_TIME);
356 if (rest <= 0) {
357 struct smc_clc_msg_decline dclc;
358
359 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
360 SMC_CLC_DECLINE);
361 return rc;
362 }
363
364 /* send add link reject message, only one link supported for now */
365 rc = smc_llc_send_add_link(link,
366 link->smcibdev->mac[link->ibport - 1],
367 &link->smcibdev->gid[link->ibport - 1],
368 SMC_LLC_RESP);
369 if (rc < 0)
370 return SMC_CLC_DECL_TCL;
371
877ae5be 372 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
52bedf37 373
75d320d6 374 return 0;
9bf9abea
UB
375}
376
0cfdd8f9
UB
377static void smc_conn_save_peer_info(struct smc_sock *smc,
378 struct smc_clc_msg_accept_confirm *clc)
379{
92a138e3 380 smc->conn.peer_rmbe_idx = clc->rmbe_idx;
5f08318f 381 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
cd6851f3
UB
382 smc->conn.peer_rmbe_size = smc_uncompress_bufsize(clc->rmbe_size);
383 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
0cfdd8f9
UB
384}
385
386static void smc_link_save_peer_info(struct smc_link *link,
387 struct smc_clc_msg_accept_confirm *clc)
388{
389 link->peer_qpn = ntoh24(clc->qpn);
390 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
391 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
392 link->peer_psn = ntoh24(clc->psn);
393 link->peer_mtu = clc->qp_mtu;
394}
395
a046d57d
UB
396/* setup for RDMA connection of client */
397static int smc_connect_rdma(struct smc_sock *smc)
398{
399 struct smc_clc_msg_accept_confirm aclc;
0cfdd8f9 400 int local_contact = SMC_FIRST_CONTACT;
a046d57d 401 struct smc_ib_device *smcibdev;
0cfdd8f9
UB
402 struct smc_link *link;
403 u8 srv_first_contact;
a046d57d
UB
404 int reason_code = 0;
405 int rc = 0;
406 u8 ibport;
407
51f1de79
UB
408 sock_hold(&smc->sk); /* sock put in passive closing */
409
ee9dfbef
UB
410 if (smc->use_fallback)
411 goto out_connected;
412
c5c1cc9c
UB
413 if (!tcp_sk(smc->clcsock->sk)->syn_smc) {
414 /* peer has not signalled SMC-capability */
415 smc->use_fallback = true;
416 goto out_connected;
417 }
418
a046d57d
UB
419 /* IPSec connections opt out of SMC-R optimizations */
420 if (using_ipsec(smc)) {
421 reason_code = SMC_CLC_DECL_IPSEC;
422 goto decline_rdma;
423 }
424
425 /* PNET table look up: search active ib_device and port
426 * within same PNETID that also contains the ethernet device
427 * used for the internal TCP socket
428 */
429 smc_pnet_find_roce_resource(smc->clcsock->sk, &smcibdev, &ibport);
430 if (!smcibdev) {
431 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
432 goto decline_rdma;
433 }
434
435 /* do inband token exchange */
436 reason_code = smc_clc_send_proposal(smc, smcibdev, ibport);
437 if (reason_code < 0) {
438 rc = reason_code;
439 goto out_err;
440 }
441 if (reason_code > 0) /* configuration error */
442 goto decline_rdma;
443 /* receive SMC Accept CLC message */
444 reason_code = smc_clc_wait_msg(smc, &aclc, sizeof(aclc),
445 SMC_CLC_ACCEPT);
446 if (reason_code < 0) {
447 rc = reason_code;
448 goto out_err;
449 }
450 if (reason_code > 0)
451 goto decline_rdma;
452
0cfdd8f9
UB
453 srv_first_contact = aclc.hdr.flag;
454 mutex_lock(&smc_create_lgr_pending);
be6d467b
KG
455 local_contact = smc_conn_create(smc, smcibdev, ibport, &aclc.lcl,
456 srv_first_contact);
0cfdd8f9
UB
457 if (local_contact < 0) {
458 rc = local_contact;
459 if (rc == -ENOMEM)
460 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
461 else if (rc == -ENOLINK)
462 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
1401ea04
KG
463 else
464 reason_code = SMC_CLC_DECL_INTERR; /* other error */
0cfdd8f9
UB
465 goto decline_rdma_unlock;
466 }
467 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
a046d57d 468
0cfdd8f9 469 smc_conn_save_peer_info(smc, &aclc);
cd6851f3 470
3e034725
UB
471 /* create send buffer and rmb */
472 rc = smc_buf_create(smc);
cd6851f3
UB
473 if (rc) {
474 reason_code = SMC_CLC_DECL_MEM;
475 goto decline_rdma_unlock;
476 }
477
0cfdd8f9
UB
478 if (local_contact == SMC_FIRST_CONTACT)
479 smc_link_save_peer_info(link, &aclc);
bd4ad577
UB
480
481 rc = smc_rmb_rtoken_handling(&smc->conn, &aclc);
482 if (rc) {
483 reason_code = SMC_CLC_DECL_INTERR;
484 goto decline_rdma_unlock;
485 }
486
46c28dbd
UB
487 smc_close_init(smc);
488 smc_rx_init(smc);
489
bd4ad577
UB
490 if (local_contact == SMC_FIRST_CONTACT) {
491 rc = smc_ib_ready_link(link);
492 if (rc) {
493 reason_code = SMC_CLC_DECL_INTERR;
494 goto decline_rdma_unlock;
495 }
652a1e41 496 } else {
e63a5f8c 497 if (!smc->conn.rmb_desc->reused) {
44aa81ce 498 if (smc_reg_rmb(link, smc->conn.rmb_desc, true)) {
652a1e41
UB
499 reason_code = SMC_CLC_DECL_INTERR;
500 goto decline_rdma_unlock;
501 }
502 }
bd4ad577 503 }
10428dd8 504 smc_rmb_sync_sg_for_device(&smc->conn);
a046d57d
UB
505
506 rc = smc_clc_send_confirm(smc);
507 if (rc)
0cfdd8f9 508 goto out_err_unlock;
a046d57d 509
9bf9abea
UB
510 if (local_contact == SMC_FIRST_CONTACT) {
511 /* QP confirmation over RoCE fabric */
0f627126 512 reason_code = smc_clnt_conf_first_link(smc);
9bf9abea
UB
513 if (reason_code < 0) {
514 rc = reason_code;
515 goto out_err_unlock;
516 }
517 if (reason_code > 0)
518 goto decline_rdma_unlock;
519 }
a046d57d 520
0cfdd8f9 521 mutex_unlock(&smc_create_lgr_pending);
e6727f39
UB
522 smc_tx_init(smc);
523
a046d57d
UB
524out_connected:
525 smc_copy_sock_settings_to_clc(smc);
b38d7324
UB
526 if (smc->sk.sk_state == SMC_INIT)
527 smc->sk.sk_state = SMC_ACTIVE;
a046d57d 528
0cfdd8f9 529 return rc ? rc : local_contact;
a046d57d 530
0cfdd8f9 531decline_rdma_unlock:
610db66f
UB
532 if (local_contact == SMC_FIRST_CONTACT)
533 smc_lgr_forget(smc->conn.lgr);
0cfdd8f9
UB
534 mutex_unlock(&smc_create_lgr_pending);
535 smc_conn_free(&smc->conn);
a046d57d
UB
536decline_rdma:
537 /* RDMA setup failed, switch back to TCP */
538 smc->use_fallback = true;
539 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
bfbedfd3 540 rc = smc_clc_send_decline(smc, reason_code);
0c9f1515 541 if (rc < 0)
a046d57d
UB
542 goto out_err;
543 }
544 goto out_connected;
545
0cfdd8f9 546out_err_unlock:
610db66f
UB
547 if (local_contact == SMC_FIRST_CONTACT)
548 smc_lgr_forget(smc->conn.lgr);
0cfdd8f9
UB
549 mutex_unlock(&smc_create_lgr_pending);
550 smc_conn_free(&smc->conn);
a046d57d 551out_err:
51f1de79
UB
552 if (smc->sk.sk_state == SMC_INIT)
553 sock_put(&smc->sk); /* passive closing */
a046d57d
UB
554 return rc;
555}
556
ac713874
UB
557static int smc_connect(struct socket *sock, struct sockaddr *addr,
558 int alen, int flags)
559{
560 struct sock *sk = sock->sk;
561 struct smc_sock *smc;
562 int rc = -EINVAL;
563
564 smc = smc_sk(sk);
565
566 /* separate smc parameter checking to be safe */
567 if (alen < sizeof(addr->sa_family))
568 goto out_err;
aaa4d33f 569 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
ac713874
UB
570 goto out_err;
571
572 lock_sock(sk);
573 switch (sk->sk_state) {
574 default:
575 goto out;
576 case SMC_ACTIVE:
577 rc = -EISCONN;
578 goto out;
579 case SMC_INIT:
580 rc = 0;
581 break;
582 }
583
584 smc_copy_sock_settings_to_clc(smc);
c5c1cc9c 585 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
ac713874
UB
586 rc = kernel_connect(smc->clcsock, addr, alen, flags);
587 if (rc)
588 goto out;
589
a046d57d
UB
590 /* setup RDMA connection */
591 rc = smc_connect_rdma(smc);
592 if (rc < 0)
593 goto out;
594 else
595 rc = 0; /* success cases including fallback */
ac713874
UB
596
597out:
598 release_sock(sk);
599out_err:
600 return rc;
601}
602
603static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
604{
3163c507
UB
605 struct socket *new_clcsock = NULL;
606 struct sock *lsk = &lsmc->sk;
ac713874
UB
607 struct sock *new_sk;
608 int rc;
609
3163c507 610 release_sock(lsk);
aaa4d33f 611 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
ac713874
UB
612 if (!new_sk) {
613 rc = -ENOMEM;
3163c507 614 lsk->sk_err = ENOMEM;
ac713874 615 *new_smc = NULL;
3163c507 616 lock_sock(lsk);
ac713874
UB
617 goto out;
618 }
619 *new_smc = smc_sk(new_sk);
620
621 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
3163c507 622 lock_sock(lsk);
35a6b178 623 if (rc < 0)
3163c507 624 lsk->sk_err = -rc;
35a6b178 625 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
a046d57d
UB
626 if (new_clcsock)
627 sock_release(new_clcsock);
628 new_sk->sk_state = SMC_CLOSED;
629 sock_set_flag(new_sk, SOCK_DEAD);
3163c507 630 new_sk->sk_prot->unhash(new_sk);
51f1de79 631 sock_put(new_sk); /* final */
ac713874
UB
632 *new_smc = NULL;
633 goto out;
634 }
635
636 (*new_smc)->clcsock = new_clcsock;
637out:
638 return rc;
639}
640
a046d57d
UB
641/* add a just created sock to the accept queue of the listen sock as
642 * candidate for a following socket accept call from user space
643 */
644static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
645{
646 struct smc_sock *par = smc_sk(parent);
647
51f1de79 648 sock_hold(sk); /* sock_put in smc_accept_unlink () */
a046d57d
UB
649 spin_lock(&par->accept_q_lock);
650 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
651 spin_unlock(&par->accept_q_lock);
652 sk_acceptq_added(parent);
653}
654
655/* remove a socket from the accept queue of its parental listening socket */
656static void smc_accept_unlink(struct sock *sk)
657{
658 struct smc_sock *par = smc_sk(sk)->listen_smc;
659
660 spin_lock(&par->accept_q_lock);
661 list_del_init(&smc_sk(sk)->accept_q);
662 spin_unlock(&par->accept_q_lock);
663 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
51f1de79 664 sock_put(sk); /* sock_hold in smc_accept_enqueue */
a046d57d
UB
665}
666
667/* remove a sock from the accept queue to bind it to a new socket created
668 * for a socket accept call from user space
669 */
b38d7324
UB
670struct sock *smc_accept_dequeue(struct sock *parent,
671 struct socket *new_sock)
a046d57d
UB
672{
673 struct smc_sock *isk, *n;
674 struct sock *new_sk;
675
676 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
677 new_sk = (struct sock *)isk;
678
679 smc_accept_unlink(new_sk);
680 if (new_sk->sk_state == SMC_CLOSED) {
127f4970
UB
681 if (isk->clcsock) {
682 sock_release(isk->clcsock);
683 isk->clcsock = NULL;
684 }
288c8390 685 new_sk->sk_prot->unhash(new_sk);
51f1de79 686 sock_put(new_sk); /* final */
a046d57d
UB
687 continue;
688 }
689 if (new_sock)
690 sock_graft(new_sk, new_sock);
691 return new_sk;
692 }
693 return NULL;
694}
695
696/* clean up for a created but never accepted sock */
b38d7324 697void smc_close_non_accepted(struct sock *sk)
a046d57d
UB
698{
699 struct smc_sock *smc = smc_sk(sk);
700
b38d7324
UB
701 lock_sock(sk);
702 if (!sk->sk_lingertime)
703 /* wait for peer closing */
704 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
51f1de79 705 if (!smc->use_fallback) {
b38d7324 706 smc_close_active(smc);
288c8390
UB
707 sock_set_flag(sk, SOCK_DEAD);
708 sk->sk_shutdown |= SHUTDOWN_MASK;
709 }
a046d57d
UB
710 if (smc->clcsock) {
711 struct socket *tcp;
712
713 tcp = smc->clcsock;
714 smc->clcsock = NULL;
715 sock_release(tcp);
716 }
b38d7324 717 if (smc->use_fallback) {
51f1de79
UB
718 sock_put(sk); /* passive closing */
719 sk->sk_state = SMC_CLOSED;
720 } else {
721 if (sk->sk_state == SMC_CLOSED)
722 smc_conn_free(&smc->conn);
b38d7324
UB
723 }
724 release_sock(sk);
51f1de79
UB
725 sk->sk_prot->unhash(sk);
726 sock_put(sk); /* final sock_put */
a046d57d
UB
727}
728
9bf9abea
UB
729static int smc_serv_conf_first_link(struct smc_sock *smc)
730{
877ae5be 731 struct net *net = sock_net(smc->clcsock->sk);
9bf9abea
UB
732 struct smc_link_group *lgr = smc->conn.lgr;
733 struct smc_link *link;
734 int rest;
735 int rc;
736
737 link = &lgr->lnk[SMC_SINGLE_LINK];
652a1e41 738
44aa81ce 739 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
652a1e41
UB
740 return SMC_CLC_DECL_INTERR;
741
9bf9abea
UB
742 /* send CONFIRM LINK request to client over the RoCE fabric */
743 rc = smc_llc_send_confirm_link(link,
744 link->smcibdev->mac[link->ibport - 1],
745 &link->smcibdev->gid[link->ibport - 1],
746 SMC_LLC_REQ);
747 if (rc < 0)
748 return SMC_CLC_DECL_TCL;
749
750 /* receive CONFIRM LINK response from client over the RoCE fabric */
751 rest = wait_for_completion_interruptible_timeout(
752 &link->llc_confirm_resp,
753 SMC_LLC_WAIT_FIRST_TIME);
754 if (rest <= 0) {
755 struct smc_clc_msg_decline dclc;
756
757 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
758 SMC_CLC_DECLINE);
75d320d6 759 return rc;
9bf9abea
UB
760 }
761
75d320d6
KG
762 if (link->llc_confirm_resp_rc)
763 return SMC_CLC_DECL_RMBE_EC;
764
52bedf37
KG
765 /* send ADD LINK request to client over the RoCE fabric */
766 rc = smc_llc_send_add_link(link,
767 link->smcibdev->mac[link->ibport - 1],
768 &link->smcibdev->gid[link->ibport - 1],
769 SMC_LLC_REQ);
770 if (rc < 0)
771 return SMC_CLC_DECL_TCL;
772
773 /* receive ADD LINK response from client over the RoCE fabric */
774 rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
775 SMC_LLC_WAIT_TIME);
776 if (rest <= 0) {
777 struct smc_clc_msg_decline dclc;
778
779 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
780 SMC_CLC_DECLINE);
781 return rc;
782 }
783
877ae5be 784 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
52bedf37 785
75d320d6 786 return 0;
9bf9abea
UB
787}
788
a046d57d
UB
789/* setup for RDMA connection of server */
790static void smc_listen_work(struct work_struct *work)
791{
792 struct smc_sock *new_smc = container_of(work, struct smc_sock,
793 smc_listen_work);
e7b7a64a 794 struct smc_clc_msg_proposal_prefix *pclc_prfx;
a046d57d
UB
795 struct socket *newclcsock = new_smc->clcsock;
796 struct smc_sock *lsmc = new_smc->listen_smc;
797 struct smc_clc_msg_accept_confirm cclc;
0cfdd8f9 798 int local_contact = SMC_REUSE_CONTACT;
a046d57d 799 struct sock *newsmcsk = &new_smc->sk;
e7b7a64a 800 struct smc_clc_msg_proposal *pclc;
a046d57d 801 struct smc_ib_device *smcibdev;
e7b7a64a 802 u8 buf[SMC_CLC_MAX_LEN];
0cfdd8f9 803 struct smc_link *link;
a046d57d 804 int reason_code = 0;
9b2c45d4 805 int rc = 0;
a046d57d
UB
806 u8 ibport;
807
ee9dfbef
UB
808 if (new_smc->use_fallback)
809 goto out_connected;
810
c5c1cc9c
UB
811 /* check if peer is smc capable */
812 if (!tcp_sk(newclcsock->sk)->syn_smc) {
813 new_smc->use_fallback = true;
814 goto out_connected;
815 }
816
a046d57d
UB
817 /* do inband token exchange -
818 *wait for and receive SMC Proposal CLC message
819 */
e7b7a64a 820 reason_code = smc_clc_wait_msg(new_smc, &buf, sizeof(buf),
a046d57d
UB
821 SMC_CLC_PROPOSAL);
822 if (reason_code < 0)
823 goto out_err;
824 if (reason_code > 0)
825 goto decline_rdma;
826
827 /* IPSec connections opt out of SMC-R optimizations */
828 if (using_ipsec(new_smc)) {
829 reason_code = SMC_CLC_DECL_IPSEC;
830 goto decline_rdma;
831 }
832
833 /* PNET table look up: search active ib_device and port
834 * within same PNETID that also contains the ethernet device
835 * used for the internal TCP socket
836 */
837 smc_pnet_find_roce_resource(newclcsock->sk, &smcibdev, &ibport);
838 if (!smcibdev) {
839 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
840 goto decline_rdma;
841 }
842
e7b7a64a
UB
843 pclc = (struct smc_clc_msg_proposal *)&buf;
844 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
c246d942
KG
845
846 rc = smc_clc_prfx_match(newclcsock, pclc_prfx);
847 if (rc) {
a046d57d
UB
848 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
849 goto decline_rdma;
850 }
851
0cfdd8f9
UB
852 /* allocate connection / link group */
853 mutex_lock(&smc_create_lgr_pending);
be6d467b
KG
854 local_contact = smc_conn_create(new_smc, smcibdev, ibport, &pclc->lcl,
855 0);
0cfdd8f9
UB
856 if (local_contact < 0) {
857 rc = local_contact;
858 if (rc == -ENOMEM)
859 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
145686ba 860 goto decline_rdma_unlock;
0cfdd8f9
UB
861 }
862 link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
a046d57d 863
3e034725
UB
864 /* create send buffer and rmb */
865 rc = smc_buf_create(new_smc);
cd6851f3
UB
866 if (rc) {
867 reason_code = SMC_CLC_DECL_MEM;
145686ba 868 goto decline_rdma_unlock;
cd6851f3 869 }
a046d57d 870
46c28dbd
UB
871 smc_close_init(new_smc);
872 smc_rx_init(new_smc);
873
652a1e41 874 if (local_contact != SMC_FIRST_CONTACT) {
e63a5f8c 875 if (!new_smc->conn.rmb_desc->reused) {
44aa81ce 876 if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true)) {
652a1e41 877 reason_code = SMC_CLC_DECL_INTERR;
145686ba 878 goto decline_rdma_unlock;
652a1e41
UB
879 }
880 }
881 }
10428dd8 882 smc_rmb_sync_sg_for_device(&new_smc->conn);
652a1e41 883
0cfdd8f9 884 rc = smc_clc_send_accept(new_smc, local_contact);
a046d57d 885 if (rc)
145686ba 886 goto out_err_unlock;
a046d57d
UB
887
888 /* receive SMC Confirm CLC message */
889 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
890 SMC_CLC_CONFIRM);
891 if (reason_code < 0)
145686ba 892 goto out_err_unlock;
a046d57d 893 if (reason_code > 0)
145686ba 894 goto decline_rdma_unlock;
0cfdd8f9
UB
895 smc_conn_save_peer_info(new_smc, &cclc);
896 if (local_contact == SMC_FIRST_CONTACT)
897 smc_link_save_peer_info(link, &cclc);
a046d57d 898
bd4ad577
UB
899 rc = smc_rmb_rtoken_handling(&new_smc->conn, &cclc);
900 if (rc) {
901 reason_code = SMC_CLC_DECL_INTERR;
145686ba 902 goto decline_rdma_unlock;
bd4ad577
UB
903 }
904
bd4ad577
UB
905 if (local_contact == SMC_FIRST_CONTACT) {
906 rc = smc_ib_ready_link(link);
907 if (rc) {
908 reason_code = SMC_CLC_DECL_INTERR;
145686ba 909 goto decline_rdma_unlock;
bd4ad577 910 }
9bf9abea
UB
911 /* QP confirmation over RoCE fabric */
912 reason_code = smc_serv_conf_first_link(new_smc);
0c9f1515 913 if (reason_code < 0)
9bf9abea 914 /* peer is not aware of a problem */
145686ba 915 goto out_err_unlock;
9bf9abea 916 if (reason_code > 0)
145686ba 917 goto decline_rdma_unlock;
bd4ad577 918 }
a046d57d 919
e6727f39 920 smc_tx_init(new_smc);
145686ba 921 mutex_unlock(&smc_create_lgr_pending);
e6727f39 922
a046d57d
UB
923out_connected:
924 sk_refcnt_debug_inc(newsmcsk);
b38d7324
UB
925 if (newsmcsk->sk_state == SMC_INIT)
926 newsmcsk->sk_state = SMC_ACTIVE;
a046d57d 927enqueue:
b38d7324 928 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
a046d57d
UB
929 if (lsmc->sk.sk_state == SMC_LISTEN) {
930 smc_accept_enqueue(&lsmc->sk, newsmcsk);
931 } else { /* no longer listening */
932 smc_close_non_accepted(newsmcsk);
933 }
934 release_sock(&lsmc->sk);
935
936 /* Wake up accept */
937 lsmc->sk.sk_data_ready(&lsmc->sk);
938 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
939 return;
940
145686ba 941decline_rdma_unlock:
610db66f
UB
942 if (local_contact == SMC_FIRST_CONTACT)
943 smc_lgr_forget(new_smc->conn.lgr);
145686ba 944 mutex_unlock(&smc_create_lgr_pending);
a046d57d
UB
945decline_rdma:
946 /* RDMA setup failed, switch back to TCP */
0cfdd8f9 947 smc_conn_free(&new_smc->conn);
a046d57d
UB
948 new_smc->use_fallback = true;
949 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
0c9f1515 950 if (smc_clc_send_decline(new_smc, reason_code) < 0)
a046d57d
UB
951 goto out_err;
952 }
953 goto out_connected;
954
145686ba 955out_err_unlock:
610db66f
UB
956 if (local_contact == SMC_FIRST_CONTACT)
957 smc_lgr_forget(new_smc->conn.lgr);
145686ba 958 mutex_unlock(&smc_create_lgr_pending);
a046d57d 959out_err:
51f1de79
UB
960 if (newsmcsk->sk_state == SMC_INIT)
961 sock_put(&new_smc->sk); /* passive closing */
a046d57d 962 newsmcsk->sk_state = SMC_CLOSED;
b38d7324 963 smc_conn_free(&new_smc->conn);
a046d57d
UB
964 goto enqueue; /* queue new sock with sk_err set */
965}
966
967static void smc_tcp_listen_work(struct work_struct *work)
968{
969 struct smc_sock *lsmc = container_of(work, struct smc_sock,
970 tcp_listen_work);
3163c507 971 struct sock *lsk = &lsmc->sk;
a046d57d
UB
972 struct smc_sock *new_smc;
973 int rc = 0;
974
3163c507
UB
975 lock_sock(lsk);
976 while (lsk->sk_state == SMC_LISTEN) {
a046d57d
UB
977 rc = smc_clcsock_accept(lsmc, &new_smc);
978 if (rc)
979 goto out;
980 if (!new_smc)
981 continue;
982
983 new_smc->listen_smc = lsmc;
ee9dfbef 984 new_smc->use_fallback = lsmc->use_fallback;
3163c507 985 sock_hold(lsk); /* sock_put in smc_listen_work */
a046d57d
UB
986 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
987 smc_copy_sock_settings_to_smc(new_smc);
51f1de79
UB
988 sock_hold(&new_smc->sk); /* sock_put in passive closing */
989 if (!schedule_work(&new_smc->smc_listen_work))
990 sock_put(&new_smc->sk);
a046d57d
UB
991 }
992
993out:
3163c507 994 release_sock(lsk);
51f1de79 995 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
a046d57d
UB
996}
997
ac713874
UB
998static int smc_listen(struct socket *sock, int backlog)
999{
1000 struct sock *sk = sock->sk;
1001 struct smc_sock *smc;
1002 int rc;
1003
1004 smc = smc_sk(sk);
1005 lock_sock(sk);
1006
1007 rc = -EINVAL;
1008 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
1009 goto out;
1010
1011 rc = 0;
1012 if (sk->sk_state == SMC_LISTEN) {
1013 sk->sk_max_ack_backlog = backlog;
1014 goto out;
1015 }
1016 /* some socket options are handled in core, so we could not apply
1017 * them to the clc socket -- copy smc socket options to clc socket
1018 */
1019 smc_copy_sock_settings_to_clc(smc);
ee9dfbef
UB
1020 if (!smc->use_fallback)
1021 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
ac713874
UB
1022
1023 rc = kernel_listen(smc->clcsock, backlog);
1024 if (rc)
1025 goto out;
1026 sk->sk_max_ack_backlog = backlog;
1027 sk->sk_ack_backlog = 0;
1028 sk->sk_state = SMC_LISTEN;
a046d57d 1029 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
51f1de79
UB
1030 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1031 if (!schedule_work(&smc->tcp_listen_work))
1032 sock_put(sk);
ac713874
UB
1033
1034out:
1035 release_sock(sk);
1036 return rc;
1037}
1038
1039static int smc_accept(struct socket *sock, struct socket *new_sock,
cdfbabfb 1040 int flags, bool kern)
ac713874 1041{
a046d57d
UB
1042 struct sock *sk = sock->sk, *nsk;
1043 DECLARE_WAITQUEUE(wait, current);
ac713874 1044 struct smc_sock *lsmc;
a046d57d
UB
1045 long timeo;
1046 int rc = 0;
ac713874
UB
1047
1048 lsmc = smc_sk(sk);
51f1de79 1049 sock_hold(sk); /* sock_put below */
ac713874
UB
1050 lock_sock(sk);
1051
1052 if (lsmc->sk.sk_state != SMC_LISTEN) {
1053 rc = -EINVAL;
abb190f1 1054 release_sock(sk);
ac713874
UB
1055 goto out;
1056 }
1057
a046d57d
UB
1058 /* Wait for an incoming connection */
1059 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1060 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1061 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1062 set_current_state(TASK_INTERRUPTIBLE);
1063 if (!timeo) {
1064 rc = -EAGAIN;
1065 break;
1066 }
1067 release_sock(sk);
1068 timeo = schedule_timeout(timeo);
1069 /* wakeup by sk_data_ready in smc_listen_work() */
1070 sched_annotate_sleep();
1071 lock_sock(sk);
1072 if (signal_pending(current)) {
1073 rc = sock_intr_errno(timeo);
1074 break;
1075 }
1076 }
1077 set_current_state(TASK_RUNNING);
1078 remove_wait_queue(sk_sleep(sk), &wait);
ac713874 1079
a046d57d
UB
1080 if (!rc)
1081 rc = sock_error(nsk);
abb190f1
UB
1082 release_sock(sk);
1083 if (rc)
1084 goto out;
1085
1086 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1087 /* wait till data arrives on the socket */
1088 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1089 MSEC_PER_SEC);
1090 if (smc_sk(nsk)->use_fallback) {
1091 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1092
1093 lock_sock(clcsk);
1094 if (skb_queue_empty(&clcsk->sk_receive_queue))
1095 sk_wait_data(clcsk, &timeo, NULL);
1096 release_sock(clcsk);
1097 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1098 lock_sock(nsk);
b51fa1b1 1099 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
abb190f1
UB
1100 release_sock(nsk);
1101 }
1102 }
ac713874
UB
1103
1104out:
51f1de79 1105 sock_put(sk); /* sock_hold above */
ac713874
UB
1106 return rc;
1107}
1108
1109static int smc_getname(struct socket *sock, struct sockaddr *addr,
9b2c45d4 1110 int peer)
ac713874
UB
1111{
1112 struct smc_sock *smc;
1113
b38d7324
UB
1114 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1115 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
ac713874
UB
1116 return -ENOTCONN;
1117
1118 smc = smc_sk(sock->sk);
1119
9b2c45d4 1120 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
ac713874
UB
1121}
1122
1123static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1124{
1125 struct sock *sk = sock->sk;
1126 struct smc_sock *smc;
1127 int rc = -EPIPE;
1128
1129 smc = smc_sk(sk);
1130 lock_sock(sk);
b38d7324
UB
1131 if ((sk->sk_state != SMC_ACTIVE) &&
1132 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1133 (sk->sk_state != SMC_INIT))
ac713874 1134 goto out;
ee9dfbef
UB
1135
1136 if (msg->msg_flags & MSG_FASTOPEN) {
1137 if (sk->sk_state == SMC_INIT) {
1138 smc->use_fallback = true;
1139 } else {
1140 rc = -EINVAL;
1141 goto out;
1142 }
1143 }
1144
ac713874
UB
1145 if (smc->use_fallback)
1146 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1147 else
e6727f39 1148 rc = smc_tx_sendmsg(smc, msg, len);
ac713874
UB
1149out:
1150 release_sock(sk);
1151 return rc;
1152}
1153
1154static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1155 int flags)
1156{
1157 struct sock *sk = sock->sk;
1158 struct smc_sock *smc;
1159 int rc = -ENOTCONN;
1160
1161 smc = smc_sk(sk);
1162 lock_sock(sk);
b38d7324
UB
1163 if ((sk->sk_state == SMC_INIT) ||
1164 (sk->sk_state == SMC_LISTEN) ||
1165 (sk->sk_state == SMC_CLOSED))
ac713874
UB
1166 goto out;
1167
b38d7324
UB
1168 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1169 rc = 0;
1170 goto out;
1171 }
1172
9014db20 1173 if (smc->use_fallback) {
ac713874 1174 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
9014db20
SR
1175 } else {
1176 msg->msg_namelen = 0;
1177 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1178 }
b38d7324 1179
ac713874
UB
1180out:
1181 release_sock(sk);
1182 return rc;
1183}
1184
ade994f4 1185static __poll_t smc_accept_poll(struct sock *parent)
a046d57d 1186{
8dce2786 1187 struct smc_sock *isk = smc_sk(parent);
63e2480c 1188 __poll_t mask = 0;
a046d57d 1189
8dce2786
UB
1190 spin_lock(&isk->accept_q_lock);
1191 if (!list_empty(&isk->accept_q))
a9a08845 1192 mask = EPOLLIN | EPOLLRDNORM;
8dce2786 1193 spin_unlock(&isk->accept_q_lock);
a046d57d 1194
8dce2786 1195 return mask;
a046d57d
UB
1196}
1197
ade994f4 1198static __poll_t smc_poll(struct file *file, struct socket *sock,
ac713874
UB
1199 poll_table *wait)
1200{
1201 struct sock *sk = sock->sk;
e6c8adca 1202 __poll_t mask = 0;
ac713874 1203 struct smc_sock *smc;
a046d57d 1204 int rc;
ac713874 1205
8dce2786 1206 if (!sk)
a9a08845 1207 return EPOLLNVAL;
8dce2786 1208
ac713874 1209 smc = smc_sk(sock->sk);
8dce2786
UB
1210 sock_hold(sk);
1211 lock_sock(sk);
a046d57d
UB
1212 if ((sk->sk_state == SMC_INIT) || smc->use_fallback) {
1213 /* delegate to CLC child sock */
8dce2786 1214 release_sock(sk);
ac713874 1215 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
ac713874 1216 lock_sock(sk);
784813ae
UB
1217 sk->sk_err = smc->clcsock->sk->sk_err;
1218 if (sk->sk_err) {
1219 mask |= EPOLLERR;
1220 } else {
1221 /* if non-blocking connect finished ... */
1222 if (sk->sk_state == SMC_INIT &&
1223 mask & EPOLLOUT &&
1224 smc->clcsock->sk->sk_state != TCP_CLOSE) {
a046d57d
UB
1225 rc = smc_connect_rdma(smc);
1226 if (rc < 0)
a9a08845 1227 mask |= EPOLLERR;
8dce2786 1228 /* success cases including fallback */
a9a08845 1229 mask |= EPOLLOUT | EPOLLWRNORM;
a046d57d 1230 }
ac713874 1231 }
ac713874 1232 } else {
8dce2786
UB
1233 if (sk->sk_state != SMC_CLOSED) {
1234 release_sock(sk);
1235 sock_poll_wait(file, sk_sleep(sk), wait);
1236 lock_sock(sk);
1237 }
a046d57d 1238 if (sk->sk_err)
a9a08845 1239 mask |= EPOLLERR;
b38d7324
UB
1240 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1241 (sk->sk_state == SMC_CLOSED))
a9a08845 1242 mask |= EPOLLHUP;
8dce2786
UB
1243 if (sk->sk_state == SMC_LISTEN) {
1244 /* woken up by sk_data_ready in smc_listen_work() */
1245 mask = smc_accept_poll(sk);
1246 } else {
1247 if (atomic_read(&smc->conn.sndbuf_space) ||
1248 sk->sk_shutdown & SEND_SHUTDOWN) {
a9a08845 1249 mask |= EPOLLOUT | EPOLLWRNORM;
8dce2786
UB
1250 } else {
1251 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1252 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1253 }
1254 if (atomic_read(&smc->conn.bytes_to_rcv))
a9a08845 1255 mask |= EPOLLIN | EPOLLRDNORM;
8dce2786 1256 if (sk->sk_shutdown & RCV_SHUTDOWN)
a9a08845 1257 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
8dce2786 1258 if (sk->sk_state == SMC_APPCLOSEWAIT1)
a9a08845 1259 mask |= EPOLLIN;
8dce2786 1260 }
b38d7324 1261
ac713874 1262 }
8dce2786
UB
1263 release_sock(sk);
1264 sock_put(sk);
ac713874
UB
1265
1266 return mask;
1267}
1268
1269static int smc_shutdown(struct socket *sock, int how)
1270{
1271 struct sock *sk = sock->sk;
1272 struct smc_sock *smc;
1273 int rc = -EINVAL;
b38d7324 1274 int rc1 = 0;
ac713874
UB
1275
1276 smc = smc_sk(sk);
1277
1278 if ((how < SHUT_RD) || (how > SHUT_RDWR))
b38d7324 1279 return rc;
ac713874
UB
1280
1281 lock_sock(sk);
1282
1283 rc = -ENOTCONN;
b38d7324
UB
1284 if ((sk->sk_state != SMC_LISTEN) &&
1285 (sk->sk_state != SMC_ACTIVE) &&
1286 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1287 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1288 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1289 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1290 (sk->sk_state != SMC_APPFINCLOSEWAIT))
ac713874
UB
1291 goto out;
1292 if (smc->use_fallback) {
1293 rc = kernel_sock_shutdown(smc->clcsock, how);
1294 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1295 if (sk->sk_shutdown == SHUTDOWN_MASK)
1296 sk->sk_state = SMC_CLOSED;
b38d7324 1297 goto out;
ac713874 1298 }
b38d7324
UB
1299 switch (how) {
1300 case SHUT_RDWR: /* shutdown in both directions */
1301 rc = smc_close_active(smc);
1302 break;
1303 case SHUT_WR:
1304 rc = smc_close_shutdown_write(smc);
1305 break;
1306 case SHUT_RD:
1255fcb2
UB
1307 rc = 0;
1308 /* nothing more to do because peer is not involved */
b38d7324
UB
1309 break;
1310 }
1255fcb2
UB
1311 if (smc->clcsock)
1312 rc1 = kernel_sock_shutdown(smc->clcsock, how);
b38d7324
UB
1313 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1314 sk->sk_shutdown |= how + 1;
ac713874
UB
1315
1316out:
1317 release_sock(sk);
b38d7324 1318 return rc ? rc : rc1;
ac713874
UB
1319}
1320
1321static int smc_setsockopt(struct socket *sock, int level, int optname,
1322 char __user *optval, unsigned int optlen)
1323{
1324 struct sock *sk = sock->sk;
1325 struct smc_sock *smc;
01d2f7e2 1326 int val, rc;
ac713874
UB
1327
1328 smc = smc_sk(sk);
1329
1330 /* generic setsockopts reaching us here always apply to the
1331 * CLC socket
1332 */
ee9dfbef
UB
1333 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1334 optval, optlen);
1335 if (smc->clcsock->sk->sk_err) {
1336 sk->sk_err = smc->clcsock->sk->sk_err;
1337 sk->sk_error_report(sk);
1338 }
1339 if (rc)
1340 return rc;
1341
01d2f7e2
UB
1342 if (optlen < sizeof(int))
1343 return rc;
1344 get_user(val, (int __user *)optval);
1345
ee9dfbef
UB
1346 lock_sock(sk);
1347 switch (optname) {
1348 case TCP_ULP:
1349 case TCP_FASTOPEN:
1350 case TCP_FASTOPEN_CONNECT:
1351 case TCP_FASTOPEN_KEY:
1352 case TCP_FASTOPEN_NO_COOKIE:
1353 /* option not supported by SMC */
1354 if (sk->sk_state == SMC_INIT) {
1355 smc->use_fallback = true;
1356 } else {
1357 if (!smc->use_fallback)
1358 rc = -EINVAL;
1359 }
1360 break;
01d2f7e2
UB
1361 case TCP_NODELAY:
1362 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
569bc643 1363 if (val && !smc->use_fallback)
01d2f7e2
UB
1364 mod_delayed_work(system_wq, &smc->conn.tx_work,
1365 0);
1366 }
1367 break;
1368 case TCP_CORK:
1369 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
569bc643 1370 if (!val && !smc->use_fallback)
01d2f7e2
UB
1371 mod_delayed_work(system_wq, &smc->conn.tx_work,
1372 0);
1373 }
1374 break;
abb190f1
UB
1375 case TCP_DEFER_ACCEPT:
1376 smc->sockopt_defer_accept = val;
1377 break;
ee9dfbef
UB
1378 default:
1379 break;
1380 }
1381 release_sock(sk);
1382
1383 return rc;
ac713874
UB
1384}
1385
1386static int smc_getsockopt(struct socket *sock, int level, int optname,
1387 char __user *optval, int __user *optlen)
1388{
1389 struct smc_sock *smc;
1390
1391 smc = smc_sk(sock->sk);
1392 /* socket options apply to the CLC socket */
1393 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1394 optval, optlen);
1395}
1396
1397static int smc_ioctl(struct socket *sock, unsigned int cmd,
1398 unsigned long arg)
1399{
1400 struct smc_sock *smc;
9b67e26f 1401 int answ;
ac713874
UB
1402
1403 smc = smc_sk(sock->sk);
9b67e26f
UB
1404 if (smc->use_fallback) {
1405 if (!smc->clcsock)
1406 return -EBADF;
ac713874 1407 return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
9b67e26f
UB
1408 }
1409 switch (cmd) {
1410 case SIOCINQ: /* same as FIONREAD */
1411 if (smc->sk.sk_state == SMC_LISTEN)
1412 return -EINVAL;
1413 answ = atomic_read(&smc->conn.bytes_to_rcv);
1414 break;
1415 case SIOCOUTQ:
1416 /* output queue size (not send + not acked) */
1417 if (smc->sk.sk_state == SMC_LISTEN)
1418 return -EINVAL;
69cb7dc0 1419 answ = smc->conn.sndbuf_desc->len -
9b67e26f
UB
1420 atomic_read(&smc->conn.sndbuf_space);
1421 break;
1422 case SIOCOUTQNSD:
1423 /* output queue size (not send only) */
1424 if (smc->sk.sk_state == SMC_LISTEN)
1425 return -EINVAL;
1426 answ = smc_tx_prepared_sends(&smc->conn);
1427 break;
1428 default:
1429 return -ENOIOCTLCMD;
1430 }
1431
1432 return put_user(answ, (int __user *)arg);
ac713874
UB
1433}
1434
1435static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1436 int offset, size_t size, int flags)
1437{
1438 struct sock *sk = sock->sk;
1439 struct smc_sock *smc;
1440 int rc = -EPIPE;
1441
1442 smc = smc_sk(sk);
1443 lock_sock(sk);
bda27ff5
SR
1444 if (sk->sk_state != SMC_ACTIVE) {
1445 release_sock(sk);
ac713874 1446 goto out;
bda27ff5
SR
1447 }
1448 release_sock(sk);
ac713874
UB
1449 if (smc->use_fallback)
1450 rc = kernel_sendpage(smc->clcsock, page, offset,
1451 size, flags);
1452 else
1453 rc = sock_no_sendpage(sock, page, offset, size, flags);
1454
1455out:
ac713874
UB
1456 return rc;
1457}
1458
9014db20
SR
1459/* Map the affected portions of the rmbe into an spd, note the number of bytes
1460 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1461 * updates till whenever a respective page has been fully processed.
1462 * Note that subsequent recv() calls have to wait till all splice() processing
1463 * completed.
1464 */
ac713874
UB
1465static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1466 struct pipe_inode_info *pipe, size_t len,
9014db20 1467 unsigned int flags)
ac713874
UB
1468{
1469 struct sock *sk = sock->sk;
1470 struct smc_sock *smc;
1471 int rc = -ENOTCONN;
1472
1473 smc = smc_sk(sk);
1474 lock_sock(sk);
9014db20
SR
1475
1476 if (sk->sk_state == SMC_INIT ||
1477 sk->sk_state == SMC_LISTEN ||
1478 sk->sk_state == SMC_CLOSED)
1479 goto out;
1480
1481 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1482 rc = 0;
ac713874 1483 goto out;
9014db20
SR
1484 }
1485
ac713874
UB
1486 if (smc->use_fallback) {
1487 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1488 pipe, len, flags);
1489 } else {
9014db20
SR
1490 if (*ppos) {
1491 rc = -ESPIPE;
1492 goto out;
1493 }
1494 if (flags & SPLICE_F_NONBLOCK)
1495 flags = MSG_DONTWAIT;
1496 else
1497 flags = 0;
1498 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
ac713874
UB
1499 }
1500out:
1501 release_sock(sk);
9014db20 1502
ac713874
UB
1503 return rc;
1504}
1505
1506/* must look like tcp */
1507static const struct proto_ops smc_sock_ops = {
1508 .family = PF_SMC,
1509 .owner = THIS_MODULE,
1510 .release = smc_release,
1511 .bind = smc_bind,
1512 .connect = smc_connect,
1513 .socketpair = sock_no_socketpair,
1514 .accept = smc_accept,
1515 .getname = smc_getname,
1516 .poll = smc_poll,
1517 .ioctl = smc_ioctl,
1518 .listen = smc_listen,
1519 .shutdown = smc_shutdown,
1520 .setsockopt = smc_setsockopt,
1521 .getsockopt = smc_getsockopt,
1522 .sendmsg = smc_sendmsg,
1523 .recvmsg = smc_recvmsg,
1524 .mmap = sock_no_mmap,
1525 .sendpage = smc_sendpage,
1526 .splice_read = smc_splice_read,
1527};
1528
1529static int smc_create(struct net *net, struct socket *sock, int protocol,
1530 int kern)
1531{
aaa4d33f 1532 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
ac713874
UB
1533 struct smc_sock *smc;
1534 struct sock *sk;
1535 int rc;
1536
1537 rc = -ESOCKTNOSUPPORT;
1538 if (sock->type != SOCK_STREAM)
1539 goto out;
1540
1541 rc = -EPROTONOSUPPORT;
aaa4d33f 1542 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
ac713874
UB
1543 goto out;
1544
1545 rc = -ENOBUFS;
1546 sock->ops = &smc_sock_ops;
aaa4d33f 1547 sk = smc_sock_alloc(net, sock, protocol);
ac713874
UB
1548 if (!sk)
1549 goto out;
1550
1551 /* create internal TCP socket for CLC handshake and fallback */
1552 smc = smc_sk(sk);
a046d57d 1553 smc->use_fallback = false; /* assume rdma capability first */
aaa4d33f
KG
1554 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1555 &smc->clcsock);
a5dcb73b 1556 if (rc) {
ac713874 1557 sk_common_release(sk);
a5dcb73b
DC
1558 goto out;
1559 }
cd6851f3
UB
1560 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1561 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
ac713874
UB
1562
1563out:
1564 return rc;
1565}
1566
1567static const struct net_proto_family smc_sock_family_ops = {
1568 .family = PF_SMC,
1569 .owner = THIS_MODULE,
1570 .create = smc_create,
1571};
1572
1573static int __init smc_init(void)
1574{
1575 int rc;
1576
6812baab
TR
1577 rc = smc_pnet_init();
1578 if (rc)
1579 return rc;
1580
9bf9abea
UB
1581 rc = smc_llc_init();
1582 if (rc) {
1583 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1584 goto out_pnet;
1585 }
1586
5f08318f
UB
1587 rc = smc_cdc_init();
1588 if (rc) {
1589 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1590 goto out_pnet;
1591 }
1592
ac713874
UB
1593 rc = proto_register(&smc_proto, 1);
1594 if (rc) {
aaa4d33f 1595 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
6812baab 1596 goto out_pnet;
ac713874
UB
1597 }
1598
aaa4d33f
KG
1599 rc = proto_register(&smc_proto6, 1);
1600 if (rc) {
1601 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
1602 goto out_proto;
1603 }
1604
ac713874
UB
1605 rc = sock_register(&smc_sock_family_ops);
1606 if (rc) {
1607 pr_err("%s: sock_register fails with %d\n", __func__, rc);
aaa4d33f 1608 goto out_proto6;
ac713874 1609 }
f16a7dd5 1610 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
aaa4d33f 1611 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
ac713874 1612
a4cf0443
UB
1613 rc = smc_ib_register_client();
1614 if (rc) {
1615 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1616 goto out_sock;
1617 }
1618
c5c1cc9c 1619 static_branch_enable(&tcp_have_smc);
ac713874
UB
1620 return 0;
1621
a4cf0443
UB
1622out_sock:
1623 sock_unregister(PF_SMC);
aaa4d33f
KG
1624out_proto6:
1625 proto_unregister(&smc_proto6);
ac713874
UB
1626out_proto:
1627 proto_unregister(&smc_proto);
6812baab
TR
1628out_pnet:
1629 smc_pnet_exit();
ac713874
UB
1630 return rc;
1631}
1632
1633static void __exit smc_exit(void)
1634{
9fda3510 1635 smc_core_exit();
c5c1cc9c 1636 static_branch_disable(&tcp_have_smc);
a4cf0443 1637 smc_ib_unregister_client();
ac713874 1638 sock_unregister(PF_SMC);
aaa4d33f 1639 proto_unregister(&smc_proto6);
ac713874 1640 proto_unregister(&smc_proto);
6812baab 1641 smc_pnet_exit();
ac713874
UB
1642}
1643
1644module_init(smc_init);
1645module_exit(smc_exit);
1646
1647MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
1648MODULE_DESCRIPTION("smc socket address family");
1649MODULE_LICENSE("GPL");
1650MODULE_ALIAS_NETPROTO(PF_SMC);