blob: cf0e11978b66c41efc66577463412928a05b90e5 [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 Braun5f083182017-01-09 16:55:22 +010029#include <linux/in.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010030#include <linux/sched/signal.h>
31
Ursula Braunac713872017-01-09 16:55:13 +010032#include <net/sock.h>
Ursula Brauna046d572017-01-09 16:55:16 +010033#include <net/tcp.h>
Ursula Braunf16a7dd2017-01-09 16:55:26 +010034#include <net/smc.h>
Ursula Braunac713872017-01-09 16:55:13 +010035
36#include "smc.h"
Ursula Brauna046d572017-01-09 16:55:16 +010037#include "smc_clc.h"
Ursula Braun9bf9abe2017-01-09 16:55:21 +010038#include "smc_llc.h"
Ursula Braun5f083182017-01-09 16:55:22 +010039#include "smc_cdc.h"
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010040#include "smc_core.h"
Ursula Brauna4cf0442017-01-09 16:55:14 +010041#include "smc_ib.h"
Thomas Richter6812baa2017-01-09 16:55:15 +010042#include "smc_pnet.h"
Ursula Braune6727f32017-01-09 16:55:23 +010043#include "smc_tx.h"
Ursula Braun952310c2017-01-09 16:55:24 +010044#include "smc_rx.h"
Ursula Braunb38d7322017-01-09 16:55:25 +010045#include "smc_close.h"
Ursula Braunac713872017-01-09 16:55:13 +010046
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010047static DEFINE_MUTEX(smc_create_lgr_pending); /* serialize link group
48 * creation
49 */
50
51struct smc_lgr_list smc_lgr_list = { /* established link groups */
52 .lock = __SPIN_LOCK_UNLOCKED(smc_lgr_list.lock),
53 .list = LIST_HEAD_INIT(smc_lgr_list.list),
54};
55
Ursula Brauna046d572017-01-09 16:55:16 +010056static void smc_tcp_listen_work(struct work_struct *);
57
Ursula Braunac713872017-01-09 16:55:13 +010058static void smc_set_keepalive(struct sock *sk, int val)
59{
60 struct smc_sock *smc = smc_sk(sk);
61
62 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
63}
64
Ursula Braunf16a7dd2017-01-09 16:55:26 +010065static struct smc_hashinfo smc_v4_hashinfo = {
66 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
67};
68
69int smc_hash_sk(struct sock *sk)
70{
71 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
72 struct hlist_head *head;
73
74 head = &h->ht;
75
76 write_lock_bh(&h->lock);
77 sk_add_node(sk, head);
78 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
79 write_unlock_bh(&h->lock);
80
81 return 0;
82}
83EXPORT_SYMBOL_GPL(smc_hash_sk);
84
85void smc_unhash_sk(struct sock *sk)
86{
87 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
88
89 write_lock_bh(&h->lock);
90 if (sk_del_node_init(sk))
91 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
92 write_unlock_bh(&h->lock);
93}
94EXPORT_SYMBOL_GPL(smc_unhash_sk);
95
96struct proto smc_proto = {
Ursula Braunac713872017-01-09 16:55:13 +010097 .name = "SMC",
98 .owner = THIS_MODULE,
99 .keepalive = smc_set_keepalive,
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100100 .hash = smc_hash_sk,
101 .unhash = smc_unhash_sk,
Ursula Braunac713872017-01-09 16:55:13 +0100102 .obj_size = sizeof(struct smc_sock),
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100103 .h.smc_hash = &smc_v4_hashinfo,
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800104 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Ursula Braunac713872017-01-09 16:55:13 +0100105};
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100106EXPORT_SYMBOL_GPL(smc_proto);
Ursula Braunac713872017-01-09 16:55:13 +0100107
108static int smc_release(struct socket *sock)
109{
110 struct sock *sk = sock->sk;
111 struct smc_sock *smc;
Ursula Braunb38d7322017-01-09 16:55:25 +0100112 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +0100113
114 if (!sk)
115 goto out;
116
117 smc = smc_sk(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100118 sock_hold(sk);
119 if (sk->sk_state == SMC_LISTEN)
120 /* smc_close_non_accepted() is called and acquires
121 * sock lock for child sockets again
122 */
123 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
124 else
125 lock_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +0100126
Ursula Braunb38d7322017-01-09 16:55:25 +0100127 if (smc->use_fallback) {
128 sk->sk_state = SMC_CLOSED;
129 sk->sk_state_change(sk);
130 } else {
131 rc = smc_close_active(smc);
132 sock_set_flag(sk, SOCK_DEAD);
133 sk->sk_shutdown |= SHUTDOWN_MASK;
134 }
Ursula Braunac713872017-01-09 16:55:13 +0100135 if (smc->clcsock) {
136 sock_release(smc->clcsock);
137 smc->clcsock = NULL;
138 }
139
140 /* detach socket */
141 sock_orphan(sk);
142 sock->sk = NULL;
Ursula Braunb38d7322017-01-09 16:55:25 +0100143 if (smc->use_fallback) {
144 schedule_delayed_work(&smc->sock_put_work, TCP_TIMEWAIT_LEN);
145 } else if (sk->sk_state == SMC_CLOSED) {
146 smc_conn_free(&smc->conn);
147 schedule_delayed_work(&smc->sock_put_work,
148 SMC_CLOSE_SOCK_PUT_DELAY);
149 }
Ursula Braunac713872017-01-09 16:55:13 +0100150 release_sock(sk);
151
152 sock_put(sk);
153out:
Ursula Braunb38d7322017-01-09 16:55:25 +0100154 return rc;
Ursula Braunac713872017-01-09 16:55:13 +0100155}
156
157static void smc_destruct(struct sock *sk)
158{
159 if (sk->sk_state != SMC_CLOSED)
160 return;
161 if (!sock_flag(sk, SOCK_DEAD))
162 return;
163
164 sk_refcnt_debug_dec(sk);
165}
166
167static struct sock *smc_sock_alloc(struct net *net, struct socket *sock)
168{
169 struct smc_sock *smc;
170 struct sock *sk;
171
172 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, &smc_proto, 0);
173 if (!sk)
174 return NULL;
175
176 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
177 sk->sk_state = SMC_INIT;
178 sk->sk_destruct = smc_destruct;
179 sk->sk_protocol = SMCPROTO_SMC;
Ursula Braunac713872017-01-09 16:55:13 +0100180 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100181 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
182 INIT_LIST_HEAD(&smc->accept_q);
183 spin_lock_init(&smc->accept_q_lock);
Ursula Braunb38d7322017-01-09 16:55:25 +0100184 INIT_DELAYED_WORK(&smc->sock_put_work, smc_close_sock_put_work);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100185 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100186 sk_refcnt_debug_inc(sk);
Ursula Braunac713872017-01-09 16:55:13 +0100187
188 return sk;
189}
190
191static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
192 int addr_len)
193{
194 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
195 struct sock *sk = sock->sk;
196 struct smc_sock *smc;
197 int rc;
198
199 smc = smc_sk(sk);
200
201 /* replicate tests from inet_bind(), to be safe wrt. future changes */
202 rc = -EINVAL;
203 if (addr_len < sizeof(struct sockaddr_in))
204 goto out;
205
206 rc = -EAFNOSUPPORT;
207 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
208 if ((addr->sin_family != AF_INET) &&
209 ((addr->sin_family != AF_UNSPEC) ||
210 (addr->sin_addr.s_addr != htonl(INADDR_ANY))))
211 goto out;
212
213 lock_sock(sk);
214
215 /* Check if socket is already active */
216 rc = -EINVAL;
217 if (sk->sk_state != SMC_INIT)
218 goto out_rel;
219
220 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
221 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
222
223out_rel:
224 release_sock(sk);
225out:
226 return rc;
227}
228
229static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
230 unsigned long mask)
231{
232 /* options we don't get control via setsockopt for */
233 nsk->sk_type = osk->sk_type;
234 nsk->sk_sndbuf = osk->sk_sndbuf;
235 nsk->sk_rcvbuf = osk->sk_rcvbuf;
236 nsk->sk_sndtimeo = osk->sk_sndtimeo;
237 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
238 nsk->sk_mark = osk->sk_mark;
239 nsk->sk_priority = osk->sk_priority;
240 nsk->sk_rcvlowat = osk->sk_rcvlowat;
241 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
242 nsk->sk_err = osk->sk_err;
243
244 nsk->sk_flags &= ~mask;
245 nsk->sk_flags |= osk->sk_flags & mask;
246}
247
248#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
249 (1UL << SOCK_KEEPOPEN) | \
250 (1UL << SOCK_LINGER) | \
251 (1UL << SOCK_BROADCAST) | \
252 (1UL << SOCK_TIMESTAMP) | \
253 (1UL << SOCK_DBG) | \
254 (1UL << SOCK_RCVTSTAMP) | \
255 (1UL << SOCK_RCVTSTAMPNS) | \
256 (1UL << SOCK_LOCALROUTE) | \
257 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
258 (1UL << SOCK_RXQ_OVFL) | \
259 (1UL << SOCK_WIFI_STATUS) | \
260 (1UL << SOCK_NOFCS) | \
261 (1UL << SOCK_FILTER_LOCKED))
262/* copy only relevant settings and flags of SOL_SOCKET level from smc to
263 * clc socket (since smc is not called for these options from net/core)
264 */
265static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
266{
267 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
268}
269
270#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
271 (1UL << SOCK_KEEPOPEN) | \
272 (1UL << SOCK_LINGER) | \
273 (1UL << SOCK_DBG))
274/* copy only settings and flags relevant for smc from clc to smc socket */
275static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
276{
277 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
278}
279
Ursula Brauna046d572017-01-09 16:55:16 +0100280/* determine subnet and mask of internal TCP socket */
281int smc_netinfo_by_tcpsk(struct socket *clcsock,
282 __be32 *subnet, u8 *prefix_len)
283{
284 struct dst_entry *dst = sk_dst_get(clcsock->sk);
Ursula Braun731b0082017-09-21 09:16:28 +0200285 struct in_device *in_dev;
Ursula Brauna046d572017-01-09 16:55:16 +0100286 struct sockaddr_in addr;
287 int rc = -ENOENT;
288 int len;
289
290 if (!dst) {
291 rc = -ENOTCONN;
292 goto out;
293 }
294 if (!dst->dev) {
295 rc = -ENODEV;
296 goto out_rel;
297 }
298
299 /* get address to which the internal TCP socket is bound */
300 kernel_getsockname(clcsock, (struct sockaddr *)&addr, &len);
301 /* analyze IPv4 specific data of net_device belonging to TCP socket */
Ursula Braun731b0082017-09-21 09:16:28 +0200302 rcu_read_lock();
303 in_dev = __in_dev_get_rcu(dst->dev);
304 for_ifa(in_dev) {
305 if (!inet_ifa_match(addr.sin_addr.s_addr, ifa))
Ursula Brauna046d572017-01-09 16:55:16 +0100306 continue;
307 *prefix_len = inet_mask_len(ifa->ifa_mask);
308 *subnet = ifa->ifa_address & ifa->ifa_mask;
309 rc = 0;
310 break;
Ursula Braun731b0082017-09-21 09:16:28 +0200311 } endfor_ifa(in_dev);
312 rcu_read_unlock();
Ursula Brauna046d572017-01-09 16:55:16 +0100313
314out_rel:
315 dst_release(dst);
316out:
317 return rc;
318}
319
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100320static int smc_clnt_conf_first_link(struct smc_sock *smc, union ib_gid *gid)
321{
322 struct smc_link_group *lgr = smc->conn.lgr;
323 struct smc_link *link;
324 int rest;
325 int rc;
326
327 link = &lgr->lnk[SMC_SINGLE_LINK];
328 /* receive CONFIRM LINK request from server over RoCE fabric */
329 rest = wait_for_completion_interruptible_timeout(
330 &link->llc_confirm,
331 SMC_LLC_WAIT_FIRST_TIME);
332 if (rest <= 0) {
333 struct smc_clc_msg_decline dclc;
334
335 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
336 SMC_CLC_DECLINE);
337 return rc;
338 }
339
340 rc = smc_ib_modify_qp_rts(link);
341 if (rc)
342 return SMC_CLC_DECL_INTERR;
343
344 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200345
346 rc = smc_wr_reg_send(link,
347 smc->conn.rmb_desc->mr_rx[SMC_SINGLE_LINK]);
348 if (rc)
349 return SMC_CLC_DECL_INTERR;
350
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100351 /* send CONFIRM LINK response over RoCE fabric */
352 rc = smc_llc_send_confirm_link(link,
353 link->smcibdev->mac[link->ibport - 1],
354 gid, SMC_LLC_RESP);
355 if (rc < 0)
356 return SMC_CLC_DECL_TCL;
357
358 return rc;
359}
360
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100361static void smc_conn_save_peer_info(struct smc_sock *smc,
362 struct smc_clc_msg_accept_confirm *clc)
363{
364 smc->conn.peer_conn_idx = clc->conn_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100365 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100366 smc->conn.peer_rmbe_size = smc_uncompress_bufsize(clc->rmbe_size);
367 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100368}
369
370static void smc_link_save_peer_info(struct smc_link *link,
371 struct smc_clc_msg_accept_confirm *clc)
372{
373 link->peer_qpn = ntoh24(clc->qpn);
374 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
375 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
376 link->peer_psn = ntoh24(clc->psn);
377 link->peer_mtu = clc->qp_mtu;
378}
379
Ursula Braun610db662018-01-25 11:15:34 +0100380static void smc_lgr_forget(struct smc_link_group *lgr)
381{
382 spin_lock_bh(&smc_lgr_list.lock);
383 /* do not use this link group for new connections */
384 if (!list_empty(&lgr->list))
385 list_del_init(&lgr->list);
386 spin_unlock_bh(&smc_lgr_list.lock);
387}
388
Ursula Brauna046d572017-01-09 16:55:16 +0100389/* setup for RDMA connection of client */
390static int smc_connect_rdma(struct smc_sock *smc)
391{
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100392 struct sockaddr_in *inaddr = (struct sockaddr_in *)smc->addr;
Ursula Brauna046d572017-01-09 16:55:16 +0100393 struct smc_clc_msg_accept_confirm aclc;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100394 int local_contact = SMC_FIRST_CONTACT;
Ursula Brauna046d572017-01-09 16:55:16 +0100395 struct smc_ib_device *smcibdev;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100396 struct smc_link *link;
397 u8 srv_first_contact;
Ursula Brauna046d572017-01-09 16:55:16 +0100398 int reason_code = 0;
399 int rc = 0;
400 u8 ibport;
401
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200402 if (!tcp_sk(smc->clcsock->sk)->syn_smc) {
403 /* peer has not signalled SMC-capability */
404 smc->use_fallback = true;
405 goto out_connected;
406 }
407
Ursula Brauna046d572017-01-09 16:55:16 +0100408 /* IPSec connections opt out of SMC-R optimizations */
409 if (using_ipsec(smc)) {
410 reason_code = SMC_CLC_DECL_IPSEC;
411 goto decline_rdma;
412 }
413
414 /* PNET table look up: search active ib_device and port
415 * within same PNETID that also contains the ethernet device
416 * used for the internal TCP socket
417 */
418 smc_pnet_find_roce_resource(smc->clcsock->sk, &smcibdev, &ibport);
419 if (!smcibdev) {
420 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
421 goto decline_rdma;
422 }
423
424 /* do inband token exchange */
425 reason_code = smc_clc_send_proposal(smc, smcibdev, ibport);
426 if (reason_code < 0) {
427 rc = reason_code;
428 goto out_err;
429 }
430 if (reason_code > 0) /* configuration error */
431 goto decline_rdma;
432 /* receive SMC Accept CLC message */
433 reason_code = smc_clc_wait_msg(smc, &aclc, sizeof(aclc),
434 SMC_CLC_ACCEPT);
435 if (reason_code < 0) {
436 rc = reason_code;
437 goto out_err;
438 }
439 if (reason_code > 0)
440 goto decline_rdma;
441
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100442 srv_first_contact = aclc.hdr.flag;
443 mutex_lock(&smc_create_lgr_pending);
444 local_contact = smc_conn_create(smc, inaddr->sin_addr.s_addr, smcibdev,
445 ibport, &aclc.lcl, srv_first_contact);
446 if (local_contact < 0) {
447 rc = local_contact;
448 if (rc == -ENOMEM)
449 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
450 else if (rc == -ENOLINK)
451 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
452 goto decline_rdma_unlock;
453 }
454 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
Ursula Brauna046d572017-01-09 16:55:16 +0100455
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100456 smc_conn_save_peer_info(smc, &aclc);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100457
Ursula Braun3e034722017-07-28 13:56:20 +0200458 /* create send buffer and rmb */
459 rc = smc_buf_create(smc);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100460 if (rc) {
461 reason_code = SMC_CLC_DECL_MEM;
462 goto decline_rdma_unlock;
463 }
464
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100465 if (local_contact == SMC_FIRST_CONTACT)
466 smc_link_save_peer_info(link, &aclc);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100467
468 rc = smc_rmb_rtoken_handling(&smc->conn, &aclc);
469 if (rc) {
470 reason_code = SMC_CLC_DECL_INTERR;
471 goto decline_rdma_unlock;
472 }
473
Ursula Braun46c28db2017-04-10 14:58:01 +0200474 smc_close_init(smc);
475 smc_rx_init(smc);
476
Ursula Braunbd4ad572017-01-09 16:55:20 +0100477 if (local_contact == SMC_FIRST_CONTACT) {
478 rc = smc_ib_ready_link(link);
479 if (rc) {
480 reason_code = SMC_CLC_DECL_INTERR;
481 goto decline_rdma_unlock;
482 }
Ursula Braun652a1e42017-07-28 13:56:17 +0200483 } else {
484 struct smc_buf_desc *buf_desc = smc->conn.rmb_desc;
485
486 if (!buf_desc->reused) {
487 /* register memory region for new rmb */
488 rc = smc_wr_reg_send(link,
489 buf_desc->mr_rx[SMC_SINGLE_LINK]);
490 if (rc) {
491 reason_code = SMC_CLC_DECL_INTERR;
492 goto decline_rdma_unlock;
493 }
494 }
Ursula Braunbd4ad572017-01-09 16:55:20 +0100495 }
Ursula Braun10428dd2017-07-28 13:56:22 +0200496 smc_rmb_sync_sg_for_device(&smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100497
498 rc = smc_clc_send_confirm(smc);
499 if (rc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100500 goto out_err_unlock;
Ursula Brauna046d572017-01-09 16:55:16 +0100501
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100502 if (local_contact == SMC_FIRST_CONTACT) {
503 /* QP confirmation over RoCE fabric */
504 reason_code = smc_clnt_conf_first_link(
505 smc, &smcibdev->gid[ibport - 1]);
506 if (reason_code < 0) {
507 rc = reason_code;
508 goto out_err_unlock;
509 }
510 if (reason_code > 0)
511 goto decline_rdma_unlock;
512 }
Ursula Brauna046d572017-01-09 16:55:16 +0100513
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100514 mutex_unlock(&smc_create_lgr_pending);
Ursula Braune6727f32017-01-09 16:55:23 +0100515 smc_tx_init(smc);
516
Ursula Brauna046d572017-01-09 16:55:16 +0100517out_connected:
518 smc_copy_sock_settings_to_clc(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +0100519 if (smc->sk.sk_state == SMC_INIT)
520 smc->sk.sk_state = SMC_ACTIVE;
Ursula Brauna046d572017-01-09 16:55:16 +0100521
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100522 return rc ? rc : local_contact;
Ursula Brauna046d572017-01-09 16:55:16 +0100523
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100524decline_rdma_unlock:
Ursula Braun610db662018-01-25 11:15:34 +0100525 if (local_contact == SMC_FIRST_CONTACT)
526 smc_lgr_forget(smc->conn.lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100527 mutex_unlock(&smc_create_lgr_pending);
528 smc_conn_free(&smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100529decline_rdma:
530 /* RDMA setup failed, switch back to TCP */
531 smc->use_fallback = true;
532 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
Ursula Braunbfbedfd2017-09-21 09:16:32 +0200533 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braun0c9f1512017-12-07 13:38:45 +0100534 if (rc < 0)
Ursula Brauna046d572017-01-09 16:55:16 +0100535 goto out_err;
536 }
537 goto out_connected;
538
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100539out_err_unlock:
Ursula Braun610db662018-01-25 11:15:34 +0100540 if (local_contact == SMC_FIRST_CONTACT)
541 smc_lgr_forget(smc->conn.lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100542 mutex_unlock(&smc_create_lgr_pending);
543 smc_conn_free(&smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100544out_err:
545 return rc;
546}
547
Ursula Braunac713872017-01-09 16:55:13 +0100548static int smc_connect(struct socket *sock, struct sockaddr *addr,
549 int alen, int flags)
550{
551 struct sock *sk = sock->sk;
552 struct smc_sock *smc;
553 int rc = -EINVAL;
554
555 smc = smc_sk(sk);
556
557 /* separate smc parameter checking to be safe */
558 if (alen < sizeof(addr->sa_family))
559 goto out_err;
560 if (addr->sa_family != AF_INET)
561 goto out_err;
Ursula Brauna046d572017-01-09 16:55:16 +0100562 smc->addr = addr; /* needed for nonblocking connect */
Ursula Braunac713872017-01-09 16:55:13 +0100563
564 lock_sock(sk);
565 switch (sk->sk_state) {
566 default:
567 goto out;
568 case SMC_ACTIVE:
569 rc = -EISCONN;
570 goto out;
571 case SMC_INIT:
572 rc = 0;
573 break;
574 }
575
576 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200577 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +0100578 rc = kernel_connect(smc->clcsock, addr, alen, flags);
579 if (rc)
580 goto out;
581
Ursula Brauna046d572017-01-09 16:55:16 +0100582 /* setup RDMA connection */
583 rc = smc_connect_rdma(smc);
584 if (rc < 0)
585 goto out;
586 else
587 rc = 0; /* success cases including fallback */
Ursula Braunac713872017-01-09 16:55:13 +0100588
589out:
590 release_sock(sk);
591out_err:
592 return rc;
593}
594
595static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
596{
Ursula Braun3163c502018-01-24 10:28:12 +0100597 struct socket *new_clcsock = NULL;
598 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100599 struct sock *new_sk;
600 int rc;
601
Ursula Braun3163c502018-01-24 10:28:12 +0100602 release_sock(lsk);
603 new_sk = smc_sock_alloc(sock_net(lsk), NULL);
Ursula Braunac713872017-01-09 16:55:13 +0100604 if (!new_sk) {
605 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100606 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100607 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100608 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100609 goto out;
610 }
611 *new_smc = smc_sk(new_sk);
612
613 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
Ursula Braun3163c502018-01-24 10:28:12 +0100614 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100615 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100616 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100617 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Brauna046d572017-01-09 16:55:16 +0100618 if (new_clcsock)
619 sock_release(new_clcsock);
620 new_sk->sk_state = SMC_CLOSED;
621 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun3163c502018-01-24 10:28:12 +0100622 new_sk->sk_prot->unhash(new_sk);
Ursula Braunac713872017-01-09 16:55:13 +0100623 sock_put(new_sk);
624 *new_smc = NULL;
625 goto out;
626 }
627
628 (*new_smc)->clcsock = new_clcsock;
629out:
630 return rc;
631}
632
Ursula Brauna046d572017-01-09 16:55:16 +0100633/* add a just created sock to the accept queue of the listen sock as
634 * candidate for a following socket accept call from user space
635 */
636static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
637{
638 struct smc_sock *par = smc_sk(parent);
639
640 sock_hold(sk);
641 spin_lock(&par->accept_q_lock);
642 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
643 spin_unlock(&par->accept_q_lock);
644 sk_acceptq_added(parent);
645}
646
647/* remove a socket from the accept queue of its parental listening socket */
648static void smc_accept_unlink(struct sock *sk)
649{
650 struct smc_sock *par = smc_sk(sk)->listen_smc;
651
652 spin_lock(&par->accept_q_lock);
653 list_del_init(&smc_sk(sk)->accept_q);
654 spin_unlock(&par->accept_q_lock);
655 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
656 sock_put(sk);
657}
658
659/* remove a sock from the accept queue to bind it to a new socket created
660 * for a socket accept call from user space
661 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100662struct sock *smc_accept_dequeue(struct sock *parent,
663 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100664{
665 struct smc_sock *isk, *n;
666 struct sock *new_sk;
667
668 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
669 new_sk = (struct sock *)isk;
670
671 smc_accept_unlink(new_sk);
672 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braun288c83902017-04-10 14:58:04 +0200673 new_sk->sk_prot->unhash(new_sk);
674 sock_put(new_sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100675 continue;
676 }
677 if (new_sock)
678 sock_graft(new_sk, new_sock);
679 return new_sk;
680 }
681 return NULL;
682}
683
684/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +0100685void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +0100686{
687 struct smc_sock *smc = smc_sk(sk);
688
689 sock_hold(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100690 lock_sock(sk);
691 if (!sk->sk_lingertime)
692 /* wait for peer closing */
693 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun288c83902017-04-10 14:58:04 +0200694 if (smc->use_fallback) {
695 sk->sk_state = SMC_CLOSED;
696 } else {
Ursula Braunb38d7322017-01-09 16:55:25 +0100697 smc_close_active(smc);
Ursula Braun288c83902017-04-10 14:58:04 +0200698 sock_set_flag(sk, SOCK_DEAD);
699 sk->sk_shutdown |= SHUTDOWN_MASK;
700 }
Ursula Brauna046d572017-01-09 16:55:16 +0100701 if (smc->clcsock) {
702 struct socket *tcp;
703
704 tcp = smc->clcsock;
705 smc->clcsock = NULL;
706 sock_release(tcp);
707 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100708 if (smc->use_fallback) {
709 schedule_delayed_work(&smc->sock_put_work, TCP_TIMEWAIT_LEN);
Ursula Braun288c83902017-04-10 14:58:04 +0200710 } else if (sk->sk_state == SMC_CLOSED) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100711 smc_conn_free(&smc->conn);
712 schedule_delayed_work(&smc->sock_put_work,
713 SMC_CLOSE_SOCK_PUT_DELAY);
714 }
715 release_sock(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100716 sock_put(sk);
717}
718
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100719static int smc_serv_conf_first_link(struct smc_sock *smc)
720{
721 struct smc_link_group *lgr = smc->conn.lgr;
722 struct smc_link *link;
723 int rest;
724 int rc;
725
726 link = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun652a1e42017-07-28 13:56:17 +0200727
728 rc = smc_wr_reg_send(link,
729 smc->conn.rmb_desc->mr_rx[SMC_SINGLE_LINK]);
730 if (rc)
731 return SMC_CLC_DECL_INTERR;
732
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100733 /* send CONFIRM LINK request to client over the RoCE fabric */
734 rc = smc_llc_send_confirm_link(link,
735 link->smcibdev->mac[link->ibport - 1],
736 &link->smcibdev->gid[link->ibport - 1],
737 SMC_LLC_REQ);
738 if (rc < 0)
739 return SMC_CLC_DECL_TCL;
740
741 /* receive CONFIRM LINK response from client over the RoCE fabric */
742 rest = wait_for_completion_interruptible_timeout(
743 &link->llc_confirm_resp,
744 SMC_LLC_WAIT_FIRST_TIME);
745 if (rest <= 0) {
746 struct smc_clc_msg_decline dclc;
747
748 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
749 SMC_CLC_DECLINE);
750 }
751
752 return rc;
753}
754
Ursula Brauna046d572017-01-09 16:55:16 +0100755/* setup for RDMA connection of server */
756static void smc_listen_work(struct work_struct *work)
757{
758 struct smc_sock *new_smc = container_of(work, struct smc_sock,
759 smc_listen_work);
Ursula Braune7b7a642017-12-07 13:38:49 +0100760 struct smc_clc_msg_proposal_prefix *pclc_prfx;
Ursula Brauna046d572017-01-09 16:55:16 +0100761 struct socket *newclcsock = new_smc->clcsock;
762 struct smc_sock *lsmc = new_smc->listen_smc;
763 struct smc_clc_msg_accept_confirm cclc;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100764 int local_contact = SMC_REUSE_CONTACT;
Ursula Brauna046d572017-01-09 16:55:16 +0100765 struct sock *newsmcsk = &new_smc->sk;
Ursula Braune7b7a642017-12-07 13:38:49 +0100766 struct smc_clc_msg_proposal *pclc;
Ursula Brauna046d572017-01-09 16:55:16 +0100767 struct smc_ib_device *smcibdev;
768 struct sockaddr_in peeraddr;
Ursula Braune7b7a642017-12-07 13:38:49 +0100769 u8 buf[SMC_CLC_MAX_LEN];
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100770 struct smc_link *link;
Ursula Brauna046d572017-01-09 16:55:16 +0100771 int reason_code = 0;
772 int rc = 0, len;
773 __be32 subnet;
774 u8 prefix_len;
775 u8 ibport;
776
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200777 /* check if peer is smc capable */
778 if (!tcp_sk(newclcsock->sk)->syn_smc) {
779 new_smc->use_fallback = true;
780 goto out_connected;
781 }
782
Ursula Brauna046d572017-01-09 16:55:16 +0100783 /* do inband token exchange -
784 *wait for and receive SMC Proposal CLC message
785 */
Ursula Braune7b7a642017-12-07 13:38:49 +0100786 reason_code = smc_clc_wait_msg(new_smc, &buf, sizeof(buf),
Ursula Brauna046d572017-01-09 16:55:16 +0100787 SMC_CLC_PROPOSAL);
788 if (reason_code < 0)
789 goto out_err;
790 if (reason_code > 0)
791 goto decline_rdma;
792
793 /* IPSec connections opt out of SMC-R optimizations */
794 if (using_ipsec(new_smc)) {
795 reason_code = SMC_CLC_DECL_IPSEC;
796 goto decline_rdma;
797 }
798
799 /* PNET table look up: search active ib_device and port
800 * within same PNETID that also contains the ethernet device
801 * used for the internal TCP socket
802 */
803 smc_pnet_find_roce_resource(newclcsock->sk, &smcibdev, &ibport);
804 if (!smcibdev) {
805 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
806 goto decline_rdma;
807 }
808
809 /* determine subnet and mask from internal TCP socket */
810 rc = smc_netinfo_by_tcpsk(newclcsock, &subnet, &prefix_len);
811 if (rc) {
812 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
813 goto decline_rdma;
814 }
Ursula Braune7b7a642017-12-07 13:38:49 +0100815
816 pclc = (struct smc_clc_msg_proposal *)&buf;
817 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
818 if (pclc_prfx->outgoing_subnet != subnet ||
819 pclc_prfx->prefix_len != prefix_len) {
Ursula Brauna046d572017-01-09 16:55:16 +0100820 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
821 goto decline_rdma;
822 }
823
824 /* get address of the peer connected to the internal TCP socket */
825 kernel_getpeername(newclcsock, (struct sockaddr *)&peeraddr, &len);
826
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100827 /* allocate connection / link group */
828 mutex_lock(&smc_create_lgr_pending);
829 local_contact = smc_conn_create(new_smc, peeraddr.sin_addr.s_addr,
Ursula Braune7b7a642017-12-07 13:38:49 +0100830 smcibdev, ibport, &pclc->lcl, 0);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100831 if (local_contact < 0) {
832 rc = local_contact;
833 if (rc == -ENOMEM)
834 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
Ursula Braun145686b2017-10-25 11:01:44 +0200835 goto decline_rdma_unlock;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100836 }
837 link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
Ursula Brauna046d572017-01-09 16:55:16 +0100838
Ursula Braun3e034722017-07-28 13:56:20 +0200839 /* create send buffer and rmb */
840 rc = smc_buf_create(new_smc);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100841 if (rc) {
842 reason_code = SMC_CLC_DECL_MEM;
Ursula Braun145686b2017-10-25 11:01:44 +0200843 goto decline_rdma_unlock;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100844 }
Ursula Brauna046d572017-01-09 16:55:16 +0100845
Ursula Braun46c28db2017-04-10 14:58:01 +0200846 smc_close_init(new_smc);
847 smc_rx_init(new_smc);
848
Ursula Braun652a1e42017-07-28 13:56:17 +0200849 if (local_contact != SMC_FIRST_CONTACT) {
850 struct smc_buf_desc *buf_desc = new_smc->conn.rmb_desc;
851
852 if (!buf_desc->reused) {
853 /* register memory region for new rmb */
854 rc = smc_wr_reg_send(link,
855 buf_desc->mr_rx[SMC_SINGLE_LINK]);
856 if (rc) {
857 reason_code = SMC_CLC_DECL_INTERR;
Ursula Braun145686b2017-10-25 11:01:44 +0200858 goto decline_rdma_unlock;
Ursula Braun652a1e42017-07-28 13:56:17 +0200859 }
860 }
861 }
Ursula Braun10428dd2017-07-28 13:56:22 +0200862 smc_rmb_sync_sg_for_device(&new_smc->conn);
Ursula Braun652a1e42017-07-28 13:56:17 +0200863
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100864 rc = smc_clc_send_accept(new_smc, local_contact);
Ursula Brauna046d572017-01-09 16:55:16 +0100865 if (rc)
Ursula Braun145686b2017-10-25 11:01:44 +0200866 goto out_err_unlock;
Ursula Brauna046d572017-01-09 16:55:16 +0100867
868 /* receive SMC Confirm CLC message */
869 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
870 SMC_CLC_CONFIRM);
871 if (reason_code < 0)
Ursula Braun145686b2017-10-25 11:01:44 +0200872 goto out_err_unlock;
Ursula Brauna046d572017-01-09 16:55:16 +0100873 if (reason_code > 0)
Ursula Braun145686b2017-10-25 11:01:44 +0200874 goto decline_rdma_unlock;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100875 smc_conn_save_peer_info(new_smc, &cclc);
876 if (local_contact == SMC_FIRST_CONTACT)
877 smc_link_save_peer_info(link, &cclc);
Ursula Brauna046d572017-01-09 16:55:16 +0100878
Ursula Braunbd4ad572017-01-09 16:55:20 +0100879 rc = smc_rmb_rtoken_handling(&new_smc->conn, &cclc);
880 if (rc) {
881 reason_code = SMC_CLC_DECL_INTERR;
Ursula Braun145686b2017-10-25 11:01:44 +0200882 goto decline_rdma_unlock;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100883 }
884
Ursula Braunbd4ad572017-01-09 16:55:20 +0100885 if (local_contact == SMC_FIRST_CONTACT) {
886 rc = smc_ib_ready_link(link);
887 if (rc) {
888 reason_code = SMC_CLC_DECL_INTERR;
Ursula Braun145686b2017-10-25 11:01:44 +0200889 goto decline_rdma_unlock;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100890 }
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100891 /* QP confirmation over RoCE fabric */
892 reason_code = smc_serv_conf_first_link(new_smc);
Ursula Braun0c9f1512017-12-07 13:38:45 +0100893 if (reason_code < 0)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100894 /* peer is not aware of a problem */
Ursula Braun145686b2017-10-25 11:01:44 +0200895 goto out_err_unlock;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100896 if (reason_code > 0)
Ursula Braun145686b2017-10-25 11:01:44 +0200897 goto decline_rdma_unlock;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100898 }
Ursula Brauna046d572017-01-09 16:55:16 +0100899
Ursula Braune6727f32017-01-09 16:55:23 +0100900 smc_tx_init(new_smc);
Ursula Braun145686b2017-10-25 11:01:44 +0200901 mutex_unlock(&smc_create_lgr_pending);
Ursula Braune6727f32017-01-09 16:55:23 +0100902
Ursula Brauna046d572017-01-09 16:55:16 +0100903out_connected:
904 sk_refcnt_debug_inc(newsmcsk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100905 if (newsmcsk->sk_state == SMC_INIT)
906 newsmcsk->sk_state = SMC_ACTIVE;
Ursula Brauna046d572017-01-09 16:55:16 +0100907enqueue:
Ursula Braunb38d7322017-01-09 16:55:25 +0100908 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +0100909 if (lsmc->sk.sk_state == SMC_LISTEN) {
910 smc_accept_enqueue(&lsmc->sk, newsmcsk);
911 } else { /* no longer listening */
912 smc_close_non_accepted(newsmcsk);
913 }
914 release_sock(&lsmc->sk);
915
916 /* Wake up accept */
917 lsmc->sk.sk_data_ready(&lsmc->sk);
918 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
919 return;
920
Ursula Braun145686b2017-10-25 11:01:44 +0200921decline_rdma_unlock:
Ursula Braun610db662018-01-25 11:15:34 +0100922 if (local_contact == SMC_FIRST_CONTACT)
923 smc_lgr_forget(new_smc->conn.lgr);
Ursula Braun145686b2017-10-25 11:01:44 +0200924 mutex_unlock(&smc_create_lgr_pending);
Ursula Brauna046d572017-01-09 16:55:16 +0100925decline_rdma:
926 /* RDMA setup failed, switch back to TCP */
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100927 smc_conn_free(&new_smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100928 new_smc->use_fallback = true;
929 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
Ursula Braun0c9f1512017-12-07 13:38:45 +0100930 if (smc_clc_send_decline(new_smc, reason_code) < 0)
Ursula Brauna046d572017-01-09 16:55:16 +0100931 goto out_err;
932 }
933 goto out_connected;
934
Ursula Braun145686b2017-10-25 11:01:44 +0200935out_err_unlock:
Ursula Braun610db662018-01-25 11:15:34 +0100936 if (local_contact == SMC_FIRST_CONTACT)
937 smc_lgr_forget(new_smc->conn.lgr);
Ursula Braun145686b2017-10-25 11:01:44 +0200938 mutex_unlock(&smc_create_lgr_pending);
Ursula Brauna046d572017-01-09 16:55:16 +0100939out_err:
940 newsmcsk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100941 smc_conn_free(&new_smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100942 goto enqueue; /* queue new sock with sk_err set */
943}
944
945static void smc_tcp_listen_work(struct work_struct *work)
946{
947 struct smc_sock *lsmc = container_of(work, struct smc_sock,
948 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +0100949 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +0100950 struct smc_sock *new_smc;
951 int rc = 0;
952
Ursula Braun3163c502018-01-24 10:28:12 +0100953 lock_sock(lsk);
954 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +0100955 rc = smc_clcsock_accept(lsmc, &new_smc);
956 if (rc)
957 goto out;
958 if (!new_smc)
959 continue;
960
961 new_smc->listen_smc = lsmc;
962 new_smc->use_fallback = false; /* assume rdma capability first*/
Ursula Braun3163c502018-01-24 10:28:12 +0100963 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +0100964 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
965 smc_copy_sock_settings_to_smc(new_smc);
966 schedule_work(&new_smc->smc_listen_work);
967 }
968
969out:
Ursula Braun3163c502018-01-24 10:28:12 +0100970 release_sock(lsk);
971 lsk->sk_data_ready(lsk); /* no more listening, wake accept */
Ursula Brauna046d572017-01-09 16:55:16 +0100972}
973
Ursula Braunac713872017-01-09 16:55:13 +0100974static int smc_listen(struct socket *sock, int backlog)
975{
976 struct sock *sk = sock->sk;
977 struct smc_sock *smc;
978 int rc;
979
980 smc = smc_sk(sk);
981 lock_sock(sk);
982
983 rc = -EINVAL;
984 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
985 goto out;
986
987 rc = 0;
988 if (sk->sk_state == SMC_LISTEN) {
989 sk->sk_max_ack_backlog = backlog;
990 goto out;
991 }
992 /* some socket options are handled in core, so we could not apply
993 * them to the clc socket -- copy smc socket options to clc socket
994 */
995 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200996 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +0100997
998 rc = kernel_listen(smc->clcsock, backlog);
999 if (rc)
1000 goto out;
1001 sk->sk_max_ack_backlog = backlog;
1002 sk->sk_ack_backlog = 0;
1003 sk->sk_state = SMC_LISTEN;
Ursula Brauna046d572017-01-09 16:55:16 +01001004 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
1005 schedule_work(&smc->tcp_listen_work);
Ursula Braunac713872017-01-09 16:55:13 +01001006
1007out:
1008 release_sock(sk);
1009 return rc;
1010}
1011
1012static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001013 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001014{
Ursula Brauna046d572017-01-09 16:55:16 +01001015 struct sock *sk = sock->sk, *nsk;
1016 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001017 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001018 long timeo;
1019 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001020
1021 lsmc = smc_sk(sk);
1022 lock_sock(sk);
1023
1024 if (lsmc->sk.sk_state != SMC_LISTEN) {
1025 rc = -EINVAL;
1026 goto out;
1027 }
1028
Ursula Brauna046d572017-01-09 16:55:16 +01001029 /* Wait for an incoming connection */
1030 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1031 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1032 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1033 set_current_state(TASK_INTERRUPTIBLE);
1034 if (!timeo) {
1035 rc = -EAGAIN;
1036 break;
1037 }
1038 release_sock(sk);
1039 timeo = schedule_timeout(timeo);
1040 /* wakeup by sk_data_ready in smc_listen_work() */
1041 sched_annotate_sleep();
1042 lock_sock(sk);
1043 if (signal_pending(current)) {
1044 rc = sock_intr_errno(timeo);
1045 break;
1046 }
1047 }
1048 set_current_state(TASK_RUNNING);
1049 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001050
Ursula Brauna046d572017-01-09 16:55:16 +01001051 if (!rc)
1052 rc = sock_error(nsk);
Ursula Braunac713872017-01-09 16:55:13 +01001053
1054out:
1055 release_sock(sk);
1056 return rc;
1057}
1058
1059static int smc_getname(struct socket *sock, struct sockaddr *addr,
1060 int *len, int peer)
1061{
1062 struct smc_sock *smc;
1063
Ursula Braunb38d7322017-01-09 16:55:25 +01001064 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1065 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001066 return -ENOTCONN;
1067
1068 smc = smc_sk(sock->sk);
1069
1070 return smc->clcsock->ops->getname(smc->clcsock, addr, len, peer);
1071}
1072
1073static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1074{
1075 struct sock *sk = sock->sk;
1076 struct smc_sock *smc;
1077 int rc = -EPIPE;
1078
1079 smc = smc_sk(sk);
1080 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001081 if ((sk->sk_state != SMC_ACTIVE) &&
1082 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1083 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001084 goto out;
1085 if (smc->use_fallback)
1086 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1087 else
Ursula Braune6727f32017-01-09 16:55:23 +01001088 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001089out:
1090 release_sock(sk);
1091 return rc;
1092}
1093
1094static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1095 int flags)
1096{
1097 struct sock *sk = sock->sk;
1098 struct smc_sock *smc;
1099 int rc = -ENOTCONN;
1100
1101 smc = smc_sk(sk);
1102 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001103 if ((sk->sk_state == SMC_INIT) ||
1104 (sk->sk_state == SMC_LISTEN) ||
1105 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001106 goto out;
1107
Ursula Braunb38d7322017-01-09 16:55:25 +01001108 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1109 rc = 0;
1110 goto out;
1111 }
1112
Ursula Braunac713872017-01-09 16:55:13 +01001113 if (smc->use_fallback)
1114 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
1115 else
Ursula Braun952310c2017-01-09 16:55:24 +01001116 rc = smc_rx_recvmsg(smc, msg, len, flags);
Ursula Braunb38d7322017-01-09 16:55:25 +01001117
Ursula Braunac713872017-01-09 16:55:13 +01001118out:
1119 release_sock(sk);
1120 return rc;
1121}
1122
Ursula Brauna046d572017-01-09 16:55:16 +01001123static unsigned int smc_accept_poll(struct sock *parent)
1124{
1125 struct smc_sock *isk;
1126 struct sock *sk;
1127
1128 lock_sock(parent);
1129 list_for_each_entry(isk, &smc_sk(parent)->accept_q, accept_q) {
1130 sk = (struct sock *)isk;
1131
1132 if (sk->sk_state == SMC_ACTIVE) {
1133 release_sock(parent);
1134 return POLLIN | POLLRDNORM;
1135 }
1136 }
1137 release_sock(parent);
1138
1139 return 0;
1140}
1141
Ursula Braunac713872017-01-09 16:55:13 +01001142static unsigned int smc_poll(struct file *file, struct socket *sock,
1143 poll_table *wait)
1144{
1145 struct sock *sk = sock->sk;
1146 unsigned int mask = 0;
1147 struct smc_sock *smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001148 int rc;
Ursula Braunac713872017-01-09 16:55:13 +01001149
1150 smc = smc_sk(sock->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001151 if ((sk->sk_state == SMC_INIT) || smc->use_fallback) {
1152 /* delegate to CLC child sock */
Ursula Braunac713872017-01-09 16:55:13 +01001153 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
1154 /* if non-blocking connect finished ... */
1155 lock_sock(sk);
1156 if ((sk->sk_state == SMC_INIT) && (mask & POLLOUT)) {
Ursula Brauna046d572017-01-09 16:55:16 +01001157 sk->sk_err = smc->clcsock->sk->sk_err;
1158 if (sk->sk_err) {
1159 mask |= POLLERR;
1160 } else {
1161 rc = smc_connect_rdma(smc);
1162 if (rc < 0)
1163 mask |= POLLERR;
1164 else
1165 /* success cases including fallback */
1166 mask |= POLLOUT | POLLWRNORM;
1167 }
Ursula Braunac713872017-01-09 16:55:13 +01001168 }
1169 release_sock(sk);
1170 } else {
Ursula Brauna046d572017-01-09 16:55:16 +01001171 sock_poll_wait(file, sk_sleep(sk), wait);
1172 if (sk->sk_state == SMC_LISTEN)
1173 /* woken up by sk_data_ready in smc_listen_work() */
1174 mask |= smc_accept_poll(sk);
1175 if (sk->sk_err)
1176 mask |= POLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001177 if (atomic_read(&smc->conn.sndbuf_space) ||
1178 (sk->sk_shutdown & SEND_SHUTDOWN)) {
Ursula Braune6727f32017-01-09 16:55:23 +01001179 mask |= POLLOUT | POLLWRNORM;
1180 } else {
1181 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1182 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1183 }
Ursula Braun952310c2017-01-09 16:55:24 +01001184 if (atomic_read(&smc->conn.bytes_to_rcv))
1185 mask |= POLLIN | POLLRDNORM;
Ursula Braunb38d7322017-01-09 16:55:25 +01001186 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1187 (sk->sk_state == SMC_CLOSED))
1188 mask |= POLLHUP;
1189 if (sk->sk_shutdown & RCV_SHUTDOWN)
1190 mask |= POLLIN | POLLRDNORM | POLLRDHUP;
1191 if (sk->sk_state == SMC_APPCLOSEWAIT1)
1192 mask |= POLLIN;
1193
Ursula Braunac713872017-01-09 16:55:13 +01001194 }
1195
1196 return mask;
1197}
1198
1199static int smc_shutdown(struct socket *sock, int how)
1200{
1201 struct sock *sk = sock->sk;
1202 struct smc_sock *smc;
1203 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001204 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001205
1206 smc = smc_sk(sk);
1207
1208 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001209 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001210
1211 lock_sock(sk);
1212
1213 rc = -ENOTCONN;
Ursula Braunb38d7322017-01-09 16:55:25 +01001214 if ((sk->sk_state != SMC_LISTEN) &&
1215 (sk->sk_state != SMC_ACTIVE) &&
1216 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1217 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1218 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1219 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1220 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001221 goto out;
1222 if (smc->use_fallback) {
1223 rc = kernel_sock_shutdown(smc->clcsock, how);
1224 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1225 if (sk->sk_shutdown == SHUTDOWN_MASK)
1226 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001227 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001228 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001229 switch (how) {
1230 case SHUT_RDWR: /* shutdown in both directions */
1231 rc = smc_close_active(smc);
1232 break;
1233 case SHUT_WR:
1234 rc = smc_close_shutdown_write(smc);
1235 break;
1236 case SHUT_RD:
1237 if (sk->sk_state == SMC_LISTEN)
1238 rc = smc_close_active(smc);
1239 else
1240 rc = 0;
1241 /* nothing more to do because peer is not involved */
1242 break;
1243 }
1244 rc1 = kernel_sock_shutdown(smc->clcsock, how);
1245 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1246 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001247
1248out:
1249 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001250 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001251}
1252
1253static int smc_setsockopt(struct socket *sock, int level, int optname,
1254 char __user *optval, unsigned int optlen)
1255{
1256 struct sock *sk = sock->sk;
1257 struct smc_sock *smc;
1258
1259 smc = smc_sk(sk);
1260
1261 /* generic setsockopts reaching us here always apply to the
1262 * CLC socket
1263 */
1264 return smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1265 optval, optlen);
1266}
1267
1268static int smc_getsockopt(struct socket *sock, int level, int optname,
1269 char __user *optval, int __user *optlen)
1270{
1271 struct smc_sock *smc;
1272
1273 smc = smc_sk(sock->sk);
1274 /* socket options apply to the CLC socket */
1275 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1276 optval, optlen);
1277}
1278
1279static int smc_ioctl(struct socket *sock, unsigned int cmd,
1280 unsigned long arg)
1281{
1282 struct smc_sock *smc;
1283
1284 smc = smc_sk(sock->sk);
1285 if (smc->use_fallback)
1286 return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1287 else
1288 return sock_no_ioctl(sock, cmd, arg);
1289}
1290
1291static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1292 int offset, size_t size, int flags)
1293{
1294 struct sock *sk = sock->sk;
1295 struct smc_sock *smc;
1296 int rc = -EPIPE;
1297
1298 smc = smc_sk(sk);
1299 lock_sock(sk);
1300 if (sk->sk_state != SMC_ACTIVE)
1301 goto out;
1302 if (smc->use_fallback)
1303 rc = kernel_sendpage(smc->clcsock, page, offset,
1304 size, flags);
1305 else
1306 rc = sock_no_sendpage(sock, page, offset, size, flags);
1307
1308out:
1309 release_sock(sk);
1310 return rc;
1311}
1312
1313static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1314 struct pipe_inode_info *pipe, size_t len,
1315 unsigned int flags)
1316{
1317 struct sock *sk = sock->sk;
1318 struct smc_sock *smc;
1319 int rc = -ENOTCONN;
1320
1321 smc = smc_sk(sk);
1322 lock_sock(sk);
1323 if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED))
1324 goto out;
1325 if (smc->use_fallback) {
1326 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1327 pipe, len, flags);
1328 } else {
1329 rc = -EOPNOTSUPP;
1330 }
1331out:
1332 release_sock(sk);
1333 return rc;
1334}
1335
1336/* must look like tcp */
1337static const struct proto_ops smc_sock_ops = {
1338 .family = PF_SMC,
1339 .owner = THIS_MODULE,
1340 .release = smc_release,
1341 .bind = smc_bind,
1342 .connect = smc_connect,
1343 .socketpair = sock_no_socketpair,
1344 .accept = smc_accept,
1345 .getname = smc_getname,
1346 .poll = smc_poll,
1347 .ioctl = smc_ioctl,
1348 .listen = smc_listen,
1349 .shutdown = smc_shutdown,
1350 .setsockopt = smc_setsockopt,
1351 .getsockopt = smc_getsockopt,
1352 .sendmsg = smc_sendmsg,
1353 .recvmsg = smc_recvmsg,
1354 .mmap = sock_no_mmap,
1355 .sendpage = smc_sendpage,
1356 .splice_read = smc_splice_read,
1357};
1358
1359static int smc_create(struct net *net, struct socket *sock, int protocol,
1360 int kern)
1361{
1362 struct smc_sock *smc;
1363 struct sock *sk;
1364 int rc;
1365
1366 rc = -ESOCKTNOSUPPORT;
1367 if (sock->type != SOCK_STREAM)
1368 goto out;
1369
1370 rc = -EPROTONOSUPPORT;
1371 if ((protocol != IPPROTO_IP) && (protocol != IPPROTO_TCP))
1372 goto out;
1373
1374 rc = -ENOBUFS;
1375 sock->ops = &smc_sock_ops;
1376 sk = smc_sock_alloc(net, sock);
1377 if (!sk)
1378 goto out;
1379
1380 /* create internal TCP socket for CLC handshake and fallback */
1381 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001382 smc->use_fallback = false; /* assume rdma capability first */
Ursula Braunac713872017-01-09 16:55:13 +01001383 rc = sock_create_kern(net, PF_INET, SOCK_STREAM,
1384 IPPROTO_TCP, &smc->clcsock);
1385 if (rc)
1386 sk_common_release(sk);
Ursula Brauncd6851f2017-01-09 16:55:18 +01001387 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1388 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01001389
1390out:
1391 return rc;
1392}
1393
1394static const struct net_proto_family smc_sock_family_ops = {
1395 .family = PF_SMC,
1396 .owner = THIS_MODULE,
1397 .create = smc_create,
1398};
1399
1400static int __init smc_init(void)
1401{
1402 int rc;
1403
Thomas Richter6812baa2017-01-09 16:55:15 +01001404 rc = smc_pnet_init();
1405 if (rc)
1406 return rc;
1407
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001408 rc = smc_llc_init();
1409 if (rc) {
1410 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1411 goto out_pnet;
1412 }
1413
Ursula Braun5f083182017-01-09 16:55:22 +01001414 rc = smc_cdc_init();
1415 if (rc) {
1416 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1417 goto out_pnet;
1418 }
1419
Ursula Braunac713872017-01-09 16:55:13 +01001420 rc = proto_register(&smc_proto, 1);
1421 if (rc) {
1422 pr_err("%s: proto_register fails with %d\n", __func__, rc);
Thomas Richter6812baa2017-01-09 16:55:15 +01001423 goto out_pnet;
Ursula Braunac713872017-01-09 16:55:13 +01001424 }
1425
1426 rc = sock_register(&smc_sock_family_ops);
1427 if (rc) {
1428 pr_err("%s: sock_register fails with %d\n", __func__, rc);
1429 goto out_proto;
1430 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01001431 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01001432
Ursula Brauna4cf0442017-01-09 16:55:14 +01001433 rc = smc_ib_register_client();
1434 if (rc) {
1435 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1436 goto out_sock;
1437 }
1438
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001439 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01001440 return 0;
1441
Ursula Brauna4cf0442017-01-09 16:55:14 +01001442out_sock:
1443 sock_unregister(PF_SMC);
Ursula Braunac713872017-01-09 16:55:13 +01001444out_proto:
1445 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001446out_pnet:
1447 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001448 return rc;
1449}
1450
1451static void __exit smc_exit(void)
1452{
Ursula Braun0cfdd8f2017-01-09 16:55:17 +01001453 struct smc_link_group *lgr, *lg;
1454 LIST_HEAD(lgr_freeing_list);
1455
1456 spin_lock_bh(&smc_lgr_list.lock);
1457 if (!list_empty(&smc_lgr_list.list))
1458 list_splice_init(&smc_lgr_list.list, &lgr_freeing_list);
1459 spin_unlock_bh(&smc_lgr_list.lock);
1460 list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) {
1461 list_del_init(&lgr->list);
1462 smc_lgr_free(lgr); /* free link group */
1463 }
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001464 static_branch_disable(&tcp_have_smc);
Ursula Brauna4cf0442017-01-09 16:55:14 +01001465 smc_ib_unregister_client();
Ursula Braunac713872017-01-09 16:55:13 +01001466 sock_unregister(PF_SMC);
1467 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001468 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001469}
1470
1471module_init(smc_init);
1472module_exit(smc_exit);
1473
1474MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
1475MODULE_DESCRIPTION("smc socket address family");
1476MODULE_LICENSE("GPL");
1477MODULE_ALIAS_NETPROTO(PF_SMC);