blob: a38f470130d35a53c63d5de9a368162896a17e64 [file] [log] [blame]
Ursula Braunac713872017-01-09 16:55:13 +01001/*
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 *
Ursula Brauna046d572017-01-09 16:55:16 +01009 * Initial restrictions:
10 * - non-blocking connect postponed
11 * - IPv6 support postponed
12 * - support for alternate links postponed
13 * - partial support for non-blocking sockets only
14 * - support for urgent data postponed
15 *
Ursula Braunac713872017-01-09 16:55:13 +010016 * Copyright IBM Corp. 2016
17 *
18 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
19 * based on prototype from Frank Blaschka
20 */
21
22#define KMSG_COMPONENT "smc"
23#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
24
25#include <linux/module.h>
26#include <linux/socket.h>
Ursula Brauna046d572017-01-09 16:55:16 +010027#include <linux/inetdevice.h>
28#include <linux/workqueue.h>
Ursula Braunac713872017-01-09 16:55:13 +010029#include <net/sock.h>
Ursula Brauna046d572017-01-09 16:55:16 +010030#include <net/tcp.h>
Ursula Braunac713872017-01-09 16:55:13 +010031
32#include "smc.h"
Ursula Brauna046d572017-01-09 16:55:16 +010033#include "smc_clc.h"
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010034#include "smc_core.h"
Ursula Brauna4cf0442017-01-09 16:55:14 +010035#include "smc_ib.h"
Thomas Richter6812baa2017-01-09 16:55:15 +010036#include "smc_pnet.h"
Ursula Braunac713872017-01-09 16:55:13 +010037
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010038static DEFINE_MUTEX(smc_create_lgr_pending); /* serialize link group
39 * creation
40 */
41
42struct smc_lgr_list smc_lgr_list = { /* established link groups */
43 .lock = __SPIN_LOCK_UNLOCKED(smc_lgr_list.lock),
44 .list = LIST_HEAD_INIT(smc_lgr_list.list),
45};
46
Ursula Brauna046d572017-01-09 16:55:16 +010047static void smc_tcp_listen_work(struct work_struct *);
48
Ursula Braunac713872017-01-09 16:55:13 +010049static void smc_set_keepalive(struct sock *sk, int val)
50{
51 struct smc_sock *smc = smc_sk(sk);
52
53 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
54}
55
56static struct proto smc_proto = {
57 .name = "SMC",
58 .owner = THIS_MODULE,
59 .keepalive = smc_set_keepalive,
60 .obj_size = sizeof(struct smc_sock),
61 .slab_flags = SLAB_DESTROY_BY_RCU,
62};
63
64static int smc_release(struct socket *sock)
65{
66 struct sock *sk = sock->sk;
67 struct smc_sock *smc;
68
69 if (!sk)
70 goto out;
71
72 smc = smc_sk(sk);
73 lock_sock(sk);
74
75 sk->sk_state = SMC_CLOSED;
76 if (smc->clcsock) {
77 sock_release(smc->clcsock);
78 smc->clcsock = NULL;
79 }
80
81 /* detach socket */
82 sock_orphan(sk);
83 sock->sk = NULL;
84 release_sock(sk);
85
86 sock_put(sk);
87out:
88 return 0;
89}
90
91static void smc_destruct(struct sock *sk)
92{
93 if (sk->sk_state != SMC_CLOSED)
94 return;
95 if (!sock_flag(sk, SOCK_DEAD))
96 return;
97
98 sk_refcnt_debug_dec(sk);
99}
100
101static struct sock *smc_sock_alloc(struct net *net, struct socket *sock)
102{
103 struct smc_sock *smc;
104 struct sock *sk;
105
106 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, &smc_proto, 0);
107 if (!sk)
108 return NULL;
109
110 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
111 sk->sk_state = SMC_INIT;
112 sk->sk_destruct = smc_destruct;
113 sk->sk_protocol = SMCPROTO_SMC;
Ursula Braunac713872017-01-09 16:55:13 +0100114 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100115 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
116 INIT_LIST_HEAD(&smc->accept_q);
117 spin_lock_init(&smc->accept_q_lock);
118 sk_refcnt_debug_inc(sk);
Ursula Braunac713872017-01-09 16:55:13 +0100119
120 return sk;
121}
122
123static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
124 int addr_len)
125{
126 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
127 struct sock *sk = sock->sk;
128 struct smc_sock *smc;
129 int rc;
130
131 smc = smc_sk(sk);
132
133 /* replicate tests from inet_bind(), to be safe wrt. future changes */
134 rc = -EINVAL;
135 if (addr_len < sizeof(struct sockaddr_in))
136 goto out;
137
138 rc = -EAFNOSUPPORT;
139 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
140 if ((addr->sin_family != AF_INET) &&
141 ((addr->sin_family != AF_UNSPEC) ||
142 (addr->sin_addr.s_addr != htonl(INADDR_ANY))))
143 goto out;
144
145 lock_sock(sk);
146
147 /* Check if socket is already active */
148 rc = -EINVAL;
149 if (sk->sk_state != SMC_INIT)
150 goto out_rel;
151
152 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
153 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
154
155out_rel:
156 release_sock(sk);
157out:
158 return rc;
159}
160
161static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
162 unsigned long mask)
163{
164 /* options we don't get control via setsockopt for */
165 nsk->sk_type = osk->sk_type;
166 nsk->sk_sndbuf = osk->sk_sndbuf;
167 nsk->sk_rcvbuf = osk->sk_rcvbuf;
168 nsk->sk_sndtimeo = osk->sk_sndtimeo;
169 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
170 nsk->sk_mark = osk->sk_mark;
171 nsk->sk_priority = osk->sk_priority;
172 nsk->sk_rcvlowat = osk->sk_rcvlowat;
173 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
174 nsk->sk_err = osk->sk_err;
175
176 nsk->sk_flags &= ~mask;
177 nsk->sk_flags |= osk->sk_flags & mask;
178}
179
180#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
181 (1UL << SOCK_KEEPOPEN) | \
182 (1UL << SOCK_LINGER) | \
183 (1UL << SOCK_BROADCAST) | \
184 (1UL << SOCK_TIMESTAMP) | \
185 (1UL << SOCK_DBG) | \
186 (1UL << SOCK_RCVTSTAMP) | \
187 (1UL << SOCK_RCVTSTAMPNS) | \
188 (1UL << SOCK_LOCALROUTE) | \
189 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
190 (1UL << SOCK_RXQ_OVFL) | \
191 (1UL << SOCK_WIFI_STATUS) | \
192 (1UL << SOCK_NOFCS) | \
193 (1UL << SOCK_FILTER_LOCKED))
194/* copy only relevant settings and flags of SOL_SOCKET level from smc to
195 * clc socket (since smc is not called for these options from net/core)
196 */
197static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
198{
199 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
200}
201
202#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
203 (1UL << SOCK_KEEPOPEN) | \
204 (1UL << SOCK_LINGER) | \
205 (1UL << SOCK_DBG))
206/* copy only settings and flags relevant for smc from clc to smc socket */
207static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
208{
209 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
210}
211
Ursula Brauna046d572017-01-09 16:55:16 +0100212/* determine subnet and mask of internal TCP socket */
213int smc_netinfo_by_tcpsk(struct socket *clcsock,
214 __be32 *subnet, u8 *prefix_len)
215{
216 struct dst_entry *dst = sk_dst_get(clcsock->sk);
217 struct sockaddr_in addr;
218 int rc = -ENOENT;
219 int len;
220
221 if (!dst) {
222 rc = -ENOTCONN;
223 goto out;
224 }
225 if (!dst->dev) {
226 rc = -ENODEV;
227 goto out_rel;
228 }
229
230 /* get address to which the internal TCP socket is bound */
231 kernel_getsockname(clcsock, (struct sockaddr *)&addr, &len);
232 /* analyze IPv4 specific data of net_device belonging to TCP socket */
233 for_ifa(dst->dev->ip_ptr) {
234 if (ifa->ifa_address != addr.sin_addr.s_addr)
235 continue;
236 *prefix_len = inet_mask_len(ifa->ifa_mask);
237 *subnet = ifa->ifa_address & ifa->ifa_mask;
238 rc = 0;
239 break;
240 } endfor_ifa(dst->dev->ip_ptr);
241
242out_rel:
243 dst_release(dst);
244out:
245 return rc;
246}
247
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100248static void smc_conn_save_peer_info(struct smc_sock *smc,
249 struct smc_clc_msg_accept_confirm *clc)
250{
251 smc->conn.peer_conn_idx = clc->conn_idx;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100252 smc->conn.peer_rmbe_size = smc_uncompress_bufsize(clc->rmbe_size);
253 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100254}
255
256static void smc_link_save_peer_info(struct smc_link *link,
257 struct smc_clc_msg_accept_confirm *clc)
258{
259 link->peer_qpn = ntoh24(clc->qpn);
260 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
261 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
262 link->peer_psn = ntoh24(clc->psn);
263 link->peer_mtu = clc->qp_mtu;
264}
265
Ursula Brauna046d572017-01-09 16:55:16 +0100266/* setup for RDMA connection of client */
267static int smc_connect_rdma(struct smc_sock *smc)
268{
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100269 struct sockaddr_in *inaddr = (struct sockaddr_in *)smc->addr;
Ursula Brauna046d572017-01-09 16:55:16 +0100270 struct smc_clc_msg_accept_confirm aclc;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100271 int local_contact = SMC_FIRST_CONTACT;
Ursula Brauna046d572017-01-09 16:55:16 +0100272 struct smc_ib_device *smcibdev;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100273 struct smc_link *link;
274 u8 srv_first_contact;
Ursula Brauna046d572017-01-09 16:55:16 +0100275 int reason_code = 0;
276 int rc = 0;
277 u8 ibport;
278
279 /* IPSec connections opt out of SMC-R optimizations */
280 if (using_ipsec(smc)) {
281 reason_code = SMC_CLC_DECL_IPSEC;
282 goto decline_rdma;
283 }
284
285 /* PNET table look up: search active ib_device and port
286 * within same PNETID that also contains the ethernet device
287 * used for the internal TCP socket
288 */
289 smc_pnet_find_roce_resource(smc->clcsock->sk, &smcibdev, &ibport);
290 if (!smcibdev) {
291 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
292 goto decline_rdma;
293 }
294
295 /* do inband token exchange */
296 reason_code = smc_clc_send_proposal(smc, smcibdev, ibport);
297 if (reason_code < 0) {
298 rc = reason_code;
299 goto out_err;
300 }
301 if (reason_code > 0) /* configuration error */
302 goto decline_rdma;
303 /* receive SMC Accept CLC message */
304 reason_code = smc_clc_wait_msg(smc, &aclc, sizeof(aclc),
305 SMC_CLC_ACCEPT);
306 if (reason_code < 0) {
307 rc = reason_code;
308 goto out_err;
309 }
310 if (reason_code > 0)
311 goto decline_rdma;
312
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100313 srv_first_contact = aclc.hdr.flag;
314 mutex_lock(&smc_create_lgr_pending);
315 local_contact = smc_conn_create(smc, inaddr->sin_addr.s_addr, smcibdev,
316 ibport, &aclc.lcl, srv_first_contact);
317 if (local_contact < 0) {
318 rc = local_contact;
319 if (rc == -ENOMEM)
320 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
321 else if (rc == -ENOLINK)
322 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
323 goto decline_rdma_unlock;
324 }
325 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
Ursula Brauna046d572017-01-09 16:55:16 +0100326
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100327 smc_conn_save_peer_info(smc, &aclc);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100328
329 rc = smc_sndbuf_create(smc);
330 if (rc) {
331 reason_code = SMC_CLC_DECL_MEM;
332 goto decline_rdma_unlock;
333 }
334 rc = smc_rmb_create(smc);
335 if (rc) {
336 reason_code = SMC_CLC_DECL_MEM;
337 goto decline_rdma_unlock;
338 }
339
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100340 if (local_contact == SMC_FIRST_CONTACT)
341 smc_link_save_peer_info(link, &aclc);
Ursula Brauna046d572017-01-09 16:55:16 +0100342 /* tbd in follow-on patch: more steps to setup RDMA communcication,
343 * create rmbs, map rmbs, rtoken_handling, modify_qp
344 */
345
346 rc = smc_clc_send_confirm(smc);
347 if (rc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100348 goto out_err_unlock;
Ursula Brauna046d572017-01-09 16:55:16 +0100349
350 /* tbd in follow-on patch: llc_confirm */
351
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100352 mutex_unlock(&smc_create_lgr_pending);
Ursula Brauna046d572017-01-09 16:55:16 +0100353out_connected:
354 smc_copy_sock_settings_to_clc(smc);
355 smc->sk.sk_state = SMC_ACTIVE;
356
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100357 return rc ? rc : local_contact;
Ursula Brauna046d572017-01-09 16:55:16 +0100358
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100359decline_rdma_unlock:
360 mutex_unlock(&smc_create_lgr_pending);
361 smc_conn_free(&smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100362decline_rdma:
363 /* RDMA setup failed, switch back to TCP */
364 smc->use_fallback = true;
365 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
366 rc = smc_clc_send_decline(smc, reason_code, 0);
367 if (rc < sizeof(struct smc_clc_msg_decline))
368 goto out_err;
369 }
370 goto out_connected;
371
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100372out_err_unlock:
373 mutex_unlock(&smc_create_lgr_pending);
374 smc_conn_free(&smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100375out_err:
376 return rc;
377}
378
Ursula Braunac713872017-01-09 16:55:13 +0100379static int smc_connect(struct socket *sock, struct sockaddr *addr,
380 int alen, int flags)
381{
382 struct sock *sk = sock->sk;
383 struct smc_sock *smc;
384 int rc = -EINVAL;
385
386 smc = smc_sk(sk);
387
388 /* separate smc parameter checking to be safe */
389 if (alen < sizeof(addr->sa_family))
390 goto out_err;
391 if (addr->sa_family != AF_INET)
392 goto out_err;
Ursula Brauna046d572017-01-09 16:55:16 +0100393 smc->addr = addr; /* needed for nonblocking connect */
Ursula Braunac713872017-01-09 16:55:13 +0100394
395 lock_sock(sk);
396 switch (sk->sk_state) {
397 default:
398 goto out;
399 case SMC_ACTIVE:
400 rc = -EISCONN;
401 goto out;
402 case SMC_INIT:
403 rc = 0;
404 break;
405 }
406
407 smc_copy_sock_settings_to_clc(smc);
408 rc = kernel_connect(smc->clcsock, addr, alen, flags);
409 if (rc)
410 goto out;
411
Ursula Brauna046d572017-01-09 16:55:16 +0100412 /* setup RDMA connection */
413 rc = smc_connect_rdma(smc);
414 if (rc < 0)
415 goto out;
416 else
417 rc = 0; /* success cases including fallback */
Ursula Braunac713872017-01-09 16:55:13 +0100418
419out:
420 release_sock(sk);
421out_err:
422 return rc;
423}
424
425static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
426{
427 struct sock *sk = &lsmc->sk;
428 struct socket *new_clcsock;
429 struct sock *new_sk;
430 int rc;
431
Ursula Brauna046d572017-01-09 16:55:16 +0100432 release_sock(&lsmc->sk);
Ursula Braunac713872017-01-09 16:55:13 +0100433 new_sk = smc_sock_alloc(sock_net(sk), NULL);
434 if (!new_sk) {
435 rc = -ENOMEM;
436 lsmc->sk.sk_err = ENOMEM;
437 *new_smc = NULL;
Ursula Brauna046d572017-01-09 16:55:16 +0100438 lock_sock(&lsmc->sk);
Ursula Braunac713872017-01-09 16:55:13 +0100439 goto out;
440 }
441 *new_smc = smc_sk(new_sk);
442
443 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
Ursula Brauna046d572017-01-09 16:55:16 +0100444 lock_sock(&lsmc->sk);
445 if (rc < 0) {
446 lsmc->sk.sk_err = -rc;
447 new_sk->sk_state = SMC_CLOSED;
448 sock_set_flag(new_sk, SOCK_DEAD);
449 sock_put(new_sk);
450 *new_smc = NULL;
451 goto out;
452 }
453 if (lsmc->sk.sk_state == SMC_CLOSED) {
454 if (new_clcsock)
455 sock_release(new_clcsock);
456 new_sk->sk_state = SMC_CLOSED;
457 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braunac713872017-01-09 16:55:13 +0100458 sock_put(new_sk);
459 *new_smc = NULL;
460 goto out;
461 }
462
463 (*new_smc)->clcsock = new_clcsock;
464out:
465 return rc;
466}
467
Ursula Brauna046d572017-01-09 16:55:16 +0100468/* add a just created sock to the accept queue of the listen sock as
469 * candidate for a following socket accept call from user space
470 */
471static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
472{
473 struct smc_sock *par = smc_sk(parent);
474
475 sock_hold(sk);
476 spin_lock(&par->accept_q_lock);
477 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
478 spin_unlock(&par->accept_q_lock);
479 sk_acceptq_added(parent);
480}
481
482/* remove a socket from the accept queue of its parental listening socket */
483static void smc_accept_unlink(struct sock *sk)
484{
485 struct smc_sock *par = smc_sk(sk)->listen_smc;
486
487 spin_lock(&par->accept_q_lock);
488 list_del_init(&smc_sk(sk)->accept_q);
489 spin_unlock(&par->accept_q_lock);
490 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
491 sock_put(sk);
492}
493
494/* remove a sock from the accept queue to bind it to a new socket created
495 * for a socket accept call from user space
496 */
497static struct sock *smc_accept_dequeue(struct sock *parent,
498 struct socket *new_sock)
499{
500 struct smc_sock *isk, *n;
501 struct sock *new_sk;
502
503 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
504 new_sk = (struct sock *)isk;
505
506 smc_accept_unlink(new_sk);
507 if (new_sk->sk_state == SMC_CLOSED) {
508 /* tbd in follow-on patch: close this sock */
509 continue;
510 }
511 if (new_sock)
512 sock_graft(new_sk, new_sock);
513 return new_sk;
514 }
515 return NULL;
516}
517
518/* clean up for a created but never accepted sock */
519static void smc_close_non_accepted(struct sock *sk)
520{
521 struct smc_sock *smc = smc_sk(sk);
522
523 sock_hold(sk);
524 if (smc->clcsock) {
525 struct socket *tcp;
526
527 tcp = smc->clcsock;
528 smc->clcsock = NULL;
529 sock_release(tcp);
530 }
531 /* more closing stuff to be added with socket closing patch */
532 sock_put(sk);
533}
534
535/* setup for RDMA connection of server */
536static void smc_listen_work(struct work_struct *work)
537{
538 struct smc_sock *new_smc = container_of(work, struct smc_sock,
539 smc_listen_work);
540 struct socket *newclcsock = new_smc->clcsock;
541 struct smc_sock *lsmc = new_smc->listen_smc;
542 struct smc_clc_msg_accept_confirm cclc;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100543 int local_contact = SMC_REUSE_CONTACT;
Ursula Brauna046d572017-01-09 16:55:16 +0100544 struct sock *newsmcsk = &new_smc->sk;
545 struct smc_clc_msg_proposal pclc;
546 struct smc_ib_device *smcibdev;
547 struct sockaddr_in peeraddr;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100548 struct smc_link *link;
Ursula Brauna046d572017-01-09 16:55:16 +0100549 int reason_code = 0;
550 int rc = 0, len;
551 __be32 subnet;
552 u8 prefix_len;
553 u8 ibport;
554
555 /* do inband token exchange -
556 *wait for and receive SMC Proposal CLC message
557 */
558 reason_code = smc_clc_wait_msg(new_smc, &pclc, sizeof(pclc),
559 SMC_CLC_PROPOSAL);
560 if (reason_code < 0)
561 goto out_err;
562 if (reason_code > 0)
563 goto decline_rdma;
564
565 /* IPSec connections opt out of SMC-R optimizations */
566 if (using_ipsec(new_smc)) {
567 reason_code = SMC_CLC_DECL_IPSEC;
568 goto decline_rdma;
569 }
570
571 /* PNET table look up: search active ib_device and port
572 * within same PNETID that also contains the ethernet device
573 * used for the internal TCP socket
574 */
575 smc_pnet_find_roce_resource(newclcsock->sk, &smcibdev, &ibport);
576 if (!smcibdev) {
577 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
578 goto decline_rdma;
579 }
580
581 /* determine subnet and mask from internal TCP socket */
582 rc = smc_netinfo_by_tcpsk(newclcsock, &subnet, &prefix_len);
583 if (rc) {
584 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
585 goto decline_rdma;
586 }
587 if ((pclc.outgoing_subnet != subnet) ||
588 (pclc.prefix_len != prefix_len)) {
589 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
590 goto decline_rdma;
591 }
592
593 /* get address of the peer connected to the internal TCP socket */
594 kernel_getpeername(newclcsock, (struct sockaddr *)&peeraddr, &len);
595
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100596 /* allocate connection / link group */
597 mutex_lock(&smc_create_lgr_pending);
598 local_contact = smc_conn_create(new_smc, peeraddr.sin_addr.s_addr,
599 smcibdev, ibport, &pclc.lcl, 0);
600 if (local_contact == SMC_REUSE_CONTACT)
601 /* lock no longer needed, free it due to following
602 * smc_clc_wait_msg() call
603 */
604 mutex_unlock(&smc_create_lgr_pending);
605 if (local_contact < 0) {
606 rc = local_contact;
607 if (rc == -ENOMEM)
608 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
609 else if (rc == -ENOLINK)
610 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
611 goto decline_rdma;
612 }
613 link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
Ursula Brauna046d572017-01-09 16:55:16 +0100614
Ursula Brauncd6851f2017-01-09 16:55:18 +0100615 rc = smc_sndbuf_create(new_smc);
616 if (rc) {
617 reason_code = SMC_CLC_DECL_MEM;
618 goto decline_rdma;
619 }
620 rc = smc_rmb_create(new_smc);
621 if (rc) {
622 reason_code = SMC_CLC_DECL_MEM;
623 goto decline_rdma;
624 }
Ursula Brauna046d572017-01-09 16:55:16 +0100625
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100626 rc = smc_clc_send_accept(new_smc, local_contact);
Ursula Brauna046d572017-01-09 16:55:16 +0100627 if (rc)
628 goto out_err;
629
630 /* receive SMC Confirm CLC message */
631 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
632 SMC_CLC_CONFIRM);
633 if (reason_code < 0)
634 goto out_err;
635 if (reason_code > 0)
636 goto decline_rdma;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100637 smc_conn_save_peer_info(new_smc, &cclc);
638 if (local_contact == SMC_FIRST_CONTACT)
639 smc_link_save_peer_info(link, &cclc);
Ursula Brauna046d572017-01-09 16:55:16 +0100640
641 /* tbd in follow-on patch: more steps to setup RDMA communcication,
642 * rtoken_handling, modify_qp
643 */
644
645out_connected:
646 sk_refcnt_debug_inc(newsmcsk);
647 newsmcsk->sk_state = SMC_ACTIVE;
648enqueue:
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100649 if (local_contact == SMC_FIRST_CONTACT)
650 mutex_unlock(&smc_create_lgr_pending);
Ursula Brauna046d572017-01-09 16:55:16 +0100651 lock_sock(&lsmc->sk);
652 if (lsmc->sk.sk_state == SMC_LISTEN) {
653 smc_accept_enqueue(&lsmc->sk, newsmcsk);
654 } else { /* no longer listening */
655 smc_close_non_accepted(newsmcsk);
656 }
657 release_sock(&lsmc->sk);
658
659 /* Wake up accept */
660 lsmc->sk.sk_data_ready(&lsmc->sk);
661 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
662 return;
663
664decline_rdma:
665 /* RDMA setup failed, switch back to TCP */
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100666 smc_conn_free(&new_smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100667 new_smc->use_fallback = true;
668 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
669 rc = smc_clc_send_decline(new_smc, reason_code, 0);
670 if (rc < sizeof(struct smc_clc_msg_decline))
671 goto out_err;
672 }
673 goto out_connected;
674
675out_err:
676 newsmcsk->sk_state = SMC_CLOSED;
677 goto enqueue; /* queue new sock with sk_err set */
678}
679
680static void smc_tcp_listen_work(struct work_struct *work)
681{
682 struct smc_sock *lsmc = container_of(work, struct smc_sock,
683 tcp_listen_work);
684 struct smc_sock *new_smc;
685 int rc = 0;
686
687 lock_sock(&lsmc->sk);
688 while (lsmc->sk.sk_state == SMC_LISTEN) {
689 rc = smc_clcsock_accept(lsmc, &new_smc);
690 if (rc)
691 goto out;
692 if (!new_smc)
693 continue;
694
695 new_smc->listen_smc = lsmc;
696 new_smc->use_fallback = false; /* assume rdma capability first*/
697 sock_hold(&lsmc->sk); /* sock_put in smc_listen_work */
698 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
699 smc_copy_sock_settings_to_smc(new_smc);
700 schedule_work(&new_smc->smc_listen_work);
701 }
702
703out:
704 release_sock(&lsmc->sk);
705 lsmc->sk.sk_data_ready(&lsmc->sk); /* no more listening, wake accept */
706}
707
Ursula Braunac713872017-01-09 16:55:13 +0100708static int smc_listen(struct socket *sock, int backlog)
709{
710 struct sock *sk = sock->sk;
711 struct smc_sock *smc;
712 int rc;
713
714 smc = smc_sk(sk);
715 lock_sock(sk);
716
717 rc = -EINVAL;
718 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
719 goto out;
720
721 rc = 0;
722 if (sk->sk_state == SMC_LISTEN) {
723 sk->sk_max_ack_backlog = backlog;
724 goto out;
725 }
726 /* some socket options are handled in core, so we could not apply
727 * them to the clc socket -- copy smc socket options to clc socket
728 */
729 smc_copy_sock_settings_to_clc(smc);
730
731 rc = kernel_listen(smc->clcsock, backlog);
732 if (rc)
733 goto out;
734 sk->sk_max_ack_backlog = backlog;
735 sk->sk_ack_backlog = 0;
736 sk->sk_state = SMC_LISTEN;
Ursula Brauna046d572017-01-09 16:55:16 +0100737 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
738 schedule_work(&smc->tcp_listen_work);
Ursula Braunac713872017-01-09 16:55:13 +0100739
740out:
741 release_sock(sk);
742 return rc;
743}
744
745static int smc_accept(struct socket *sock, struct socket *new_sock,
746 int flags)
747{
Ursula Brauna046d572017-01-09 16:55:16 +0100748 struct sock *sk = sock->sk, *nsk;
749 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +0100750 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +0100751 long timeo;
752 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +0100753
754 lsmc = smc_sk(sk);
755 lock_sock(sk);
756
757 if (lsmc->sk.sk_state != SMC_LISTEN) {
758 rc = -EINVAL;
759 goto out;
760 }
761
Ursula Brauna046d572017-01-09 16:55:16 +0100762 /* Wait for an incoming connection */
763 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
764 add_wait_queue_exclusive(sk_sleep(sk), &wait);
765 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
766 set_current_state(TASK_INTERRUPTIBLE);
767 if (!timeo) {
768 rc = -EAGAIN;
769 break;
770 }
771 release_sock(sk);
772 timeo = schedule_timeout(timeo);
773 /* wakeup by sk_data_ready in smc_listen_work() */
774 sched_annotate_sleep();
775 lock_sock(sk);
776 if (signal_pending(current)) {
777 rc = sock_intr_errno(timeo);
778 break;
779 }
780 }
781 set_current_state(TASK_RUNNING);
782 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +0100783
Ursula Brauna046d572017-01-09 16:55:16 +0100784 if (!rc)
785 rc = sock_error(nsk);
Ursula Braunac713872017-01-09 16:55:13 +0100786
787out:
788 release_sock(sk);
789 return rc;
790}
791
792static int smc_getname(struct socket *sock, struct sockaddr *addr,
793 int *len, int peer)
794{
795 struct smc_sock *smc;
796
797 if (peer && (sock->sk->sk_state != SMC_ACTIVE))
798 return -ENOTCONN;
799
800 smc = smc_sk(sock->sk);
801
802 return smc->clcsock->ops->getname(smc->clcsock, addr, len, peer);
803}
804
805static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
806{
807 struct sock *sk = sock->sk;
808 struct smc_sock *smc;
809 int rc = -EPIPE;
810
811 smc = smc_sk(sk);
812 lock_sock(sk);
813 if (sk->sk_state != SMC_ACTIVE)
814 goto out;
815 if (smc->use_fallback)
816 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
817 else
818 rc = sock_no_sendmsg(sock, msg, len);
819out:
820 release_sock(sk);
821 return rc;
822}
823
824static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
825 int flags)
826{
827 struct sock *sk = sock->sk;
828 struct smc_sock *smc;
829 int rc = -ENOTCONN;
830
831 smc = smc_sk(sk);
832 lock_sock(sk);
833 if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED))
834 goto out;
835
836 if (smc->use_fallback)
837 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
838 else
839 rc = sock_no_recvmsg(sock, msg, len, flags);
840out:
841 release_sock(sk);
842 return rc;
843}
844
Ursula Brauna046d572017-01-09 16:55:16 +0100845static unsigned int smc_accept_poll(struct sock *parent)
846{
847 struct smc_sock *isk;
848 struct sock *sk;
849
850 lock_sock(parent);
851 list_for_each_entry(isk, &smc_sk(parent)->accept_q, accept_q) {
852 sk = (struct sock *)isk;
853
854 if (sk->sk_state == SMC_ACTIVE) {
855 release_sock(parent);
856 return POLLIN | POLLRDNORM;
857 }
858 }
859 release_sock(parent);
860
861 return 0;
862}
863
Ursula Braunac713872017-01-09 16:55:13 +0100864static unsigned int smc_poll(struct file *file, struct socket *sock,
865 poll_table *wait)
866{
867 struct sock *sk = sock->sk;
868 unsigned int mask = 0;
869 struct smc_sock *smc;
Ursula Brauna046d572017-01-09 16:55:16 +0100870 int rc;
Ursula Braunac713872017-01-09 16:55:13 +0100871
872 smc = smc_sk(sock->sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100873 if ((sk->sk_state == SMC_INIT) || smc->use_fallback) {
874 /* delegate to CLC child sock */
Ursula Braunac713872017-01-09 16:55:13 +0100875 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
876 /* if non-blocking connect finished ... */
877 lock_sock(sk);
878 if ((sk->sk_state == SMC_INIT) && (mask & POLLOUT)) {
Ursula Brauna046d572017-01-09 16:55:16 +0100879 sk->sk_err = smc->clcsock->sk->sk_err;
880 if (sk->sk_err) {
881 mask |= POLLERR;
882 } else {
883 rc = smc_connect_rdma(smc);
884 if (rc < 0)
885 mask |= POLLERR;
886 else
887 /* success cases including fallback */
888 mask |= POLLOUT | POLLWRNORM;
889 }
Ursula Braunac713872017-01-09 16:55:13 +0100890 }
891 release_sock(sk);
892 } else {
Ursula Brauna046d572017-01-09 16:55:16 +0100893 sock_poll_wait(file, sk_sleep(sk), wait);
894 if (sk->sk_state == SMC_LISTEN)
895 /* woken up by sk_data_ready in smc_listen_work() */
896 mask |= smc_accept_poll(sk);
897 if (sk->sk_err)
898 mask |= POLLERR;
899 /* for now - to be enhanced in follow-on patch */
Ursula Braunac713872017-01-09 16:55:13 +0100900 }
901
902 return mask;
903}
904
905static int smc_shutdown(struct socket *sock, int how)
906{
907 struct sock *sk = sock->sk;
908 struct smc_sock *smc;
909 int rc = -EINVAL;
910
911 smc = smc_sk(sk);
912
913 if ((how < SHUT_RD) || (how > SHUT_RDWR))
914 goto out_err;
915
916 lock_sock(sk);
917
918 rc = -ENOTCONN;
919 if (sk->sk_state == SMC_CLOSED)
920 goto out;
921 if (smc->use_fallback) {
922 rc = kernel_sock_shutdown(smc->clcsock, how);
923 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
924 if (sk->sk_shutdown == SHUTDOWN_MASK)
925 sk->sk_state = SMC_CLOSED;
926 } else {
927 rc = sock_no_shutdown(sock, how);
928 }
929
930out:
931 release_sock(sk);
932
933out_err:
934 return rc;
935}
936
937static int smc_setsockopt(struct socket *sock, int level, int optname,
938 char __user *optval, unsigned int optlen)
939{
940 struct sock *sk = sock->sk;
941 struct smc_sock *smc;
942
943 smc = smc_sk(sk);
944
945 /* generic setsockopts reaching us here always apply to the
946 * CLC socket
947 */
948 return smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
949 optval, optlen);
950}
951
952static int smc_getsockopt(struct socket *sock, int level, int optname,
953 char __user *optval, int __user *optlen)
954{
955 struct smc_sock *smc;
956
957 smc = smc_sk(sock->sk);
958 /* socket options apply to the CLC socket */
959 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
960 optval, optlen);
961}
962
963static int smc_ioctl(struct socket *sock, unsigned int cmd,
964 unsigned long arg)
965{
966 struct smc_sock *smc;
967
968 smc = smc_sk(sock->sk);
969 if (smc->use_fallback)
970 return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
971 else
972 return sock_no_ioctl(sock, cmd, arg);
973}
974
975static ssize_t smc_sendpage(struct socket *sock, struct page *page,
976 int offset, size_t size, int flags)
977{
978 struct sock *sk = sock->sk;
979 struct smc_sock *smc;
980 int rc = -EPIPE;
981
982 smc = smc_sk(sk);
983 lock_sock(sk);
984 if (sk->sk_state != SMC_ACTIVE)
985 goto out;
986 if (smc->use_fallback)
987 rc = kernel_sendpage(smc->clcsock, page, offset,
988 size, flags);
989 else
990 rc = sock_no_sendpage(sock, page, offset, size, flags);
991
992out:
993 release_sock(sk);
994 return rc;
995}
996
997static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
998 struct pipe_inode_info *pipe, size_t len,
999 unsigned int flags)
1000{
1001 struct sock *sk = sock->sk;
1002 struct smc_sock *smc;
1003 int rc = -ENOTCONN;
1004
1005 smc = smc_sk(sk);
1006 lock_sock(sk);
1007 if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED))
1008 goto out;
1009 if (smc->use_fallback) {
1010 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1011 pipe, len, flags);
1012 } else {
1013 rc = -EOPNOTSUPP;
1014 }
1015out:
1016 release_sock(sk);
1017 return rc;
1018}
1019
1020/* must look like tcp */
1021static const struct proto_ops smc_sock_ops = {
1022 .family = PF_SMC,
1023 .owner = THIS_MODULE,
1024 .release = smc_release,
1025 .bind = smc_bind,
1026 .connect = smc_connect,
1027 .socketpair = sock_no_socketpair,
1028 .accept = smc_accept,
1029 .getname = smc_getname,
1030 .poll = smc_poll,
1031 .ioctl = smc_ioctl,
1032 .listen = smc_listen,
1033 .shutdown = smc_shutdown,
1034 .setsockopt = smc_setsockopt,
1035 .getsockopt = smc_getsockopt,
1036 .sendmsg = smc_sendmsg,
1037 .recvmsg = smc_recvmsg,
1038 .mmap = sock_no_mmap,
1039 .sendpage = smc_sendpage,
1040 .splice_read = smc_splice_read,
1041};
1042
1043static int smc_create(struct net *net, struct socket *sock, int protocol,
1044 int kern)
1045{
1046 struct smc_sock *smc;
1047 struct sock *sk;
1048 int rc;
1049
1050 rc = -ESOCKTNOSUPPORT;
1051 if (sock->type != SOCK_STREAM)
1052 goto out;
1053
1054 rc = -EPROTONOSUPPORT;
1055 if ((protocol != IPPROTO_IP) && (protocol != IPPROTO_TCP))
1056 goto out;
1057
1058 rc = -ENOBUFS;
1059 sock->ops = &smc_sock_ops;
1060 sk = smc_sock_alloc(net, sock);
1061 if (!sk)
1062 goto out;
1063
1064 /* create internal TCP socket for CLC handshake and fallback */
1065 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001066 smc->use_fallback = false; /* assume rdma capability first */
Ursula Braunac713872017-01-09 16:55:13 +01001067 rc = sock_create_kern(net, PF_INET, SOCK_STREAM,
1068 IPPROTO_TCP, &smc->clcsock);
1069 if (rc)
1070 sk_common_release(sk);
Ursula Brauncd6851f2017-01-09 16:55:18 +01001071 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1072 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01001073
1074out:
1075 return rc;
1076}
1077
1078static const struct net_proto_family smc_sock_family_ops = {
1079 .family = PF_SMC,
1080 .owner = THIS_MODULE,
1081 .create = smc_create,
1082};
1083
1084static int __init smc_init(void)
1085{
1086 int rc;
1087
Thomas Richter6812baa2017-01-09 16:55:15 +01001088 rc = smc_pnet_init();
1089 if (rc)
1090 return rc;
1091
Ursula Braunac713872017-01-09 16:55:13 +01001092 rc = proto_register(&smc_proto, 1);
1093 if (rc) {
1094 pr_err("%s: proto_register fails with %d\n", __func__, rc);
Thomas Richter6812baa2017-01-09 16:55:15 +01001095 goto out_pnet;
Ursula Braunac713872017-01-09 16:55:13 +01001096 }
1097
1098 rc = sock_register(&smc_sock_family_ops);
1099 if (rc) {
1100 pr_err("%s: sock_register fails with %d\n", __func__, rc);
1101 goto out_proto;
1102 }
1103
Ursula Brauna4cf0442017-01-09 16:55:14 +01001104 rc = smc_ib_register_client();
1105 if (rc) {
1106 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1107 goto out_sock;
1108 }
1109
Ursula Braunac713872017-01-09 16:55:13 +01001110 return 0;
1111
Ursula Brauna4cf0442017-01-09 16:55:14 +01001112out_sock:
1113 sock_unregister(PF_SMC);
Ursula Braunac713872017-01-09 16:55:13 +01001114out_proto:
1115 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001116out_pnet:
1117 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001118 return rc;
1119}
1120
1121static void __exit smc_exit(void)
1122{
Ursula Braun0cfdd8f2017-01-09 16:55:17 +01001123 struct smc_link_group *lgr, *lg;
1124 LIST_HEAD(lgr_freeing_list);
1125
1126 spin_lock_bh(&smc_lgr_list.lock);
1127 if (!list_empty(&smc_lgr_list.list))
1128 list_splice_init(&smc_lgr_list.list, &lgr_freeing_list);
1129 spin_unlock_bh(&smc_lgr_list.lock);
1130 list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) {
1131 list_del_init(&lgr->list);
1132 smc_lgr_free(lgr); /* free link group */
1133 }
Ursula Brauna4cf0442017-01-09 16:55:14 +01001134 smc_ib_unregister_client();
Ursula Braunac713872017-01-09 16:55:13 +01001135 sock_unregister(PF_SMC);
1136 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001137 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001138}
1139
1140module_init(smc_init);
1141module_exit(smc_exit);
1142
1143MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
1144MODULE_DESCRIPTION("smc socket address family");
1145MODULE_LICENSE("GPL");
1146MODULE_ALIAS_NETPROTO(PF_SMC);