blob: 05cbcd3a6f6079149e2f809dc1df7c78c0cbbcbe [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 Brauna046d572017-01-09 16:55:16 +0100380/* setup for RDMA connection of client */
381static int smc_connect_rdma(struct smc_sock *smc)
382{
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100383 struct sockaddr_in *inaddr = (struct sockaddr_in *)smc->addr;
Ursula Brauna046d572017-01-09 16:55:16 +0100384 struct smc_clc_msg_accept_confirm aclc;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100385 int local_contact = SMC_FIRST_CONTACT;
Ursula Brauna046d572017-01-09 16:55:16 +0100386 struct smc_ib_device *smcibdev;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100387 struct smc_link *link;
388 u8 srv_first_contact;
Ursula Brauna046d572017-01-09 16:55:16 +0100389 int reason_code = 0;
390 int rc = 0;
391 u8 ibport;
392
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200393 if (!tcp_sk(smc->clcsock->sk)->syn_smc) {
394 /* peer has not signalled SMC-capability */
395 smc->use_fallback = true;
396 goto out_connected;
397 }
398
Ursula Brauna046d572017-01-09 16:55:16 +0100399 /* IPSec connections opt out of SMC-R optimizations */
400 if (using_ipsec(smc)) {
401 reason_code = SMC_CLC_DECL_IPSEC;
402 goto decline_rdma;
403 }
404
405 /* PNET table look up: search active ib_device and port
406 * within same PNETID that also contains the ethernet device
407 * used for the internal TCP socket
408 */
409 smc_pnet_find_roce_resource(smc->clcsock->sk, &smcibdev, &ibport);
410 if (!smcibdev) {
411 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
412 goto decline_rdma;
413 }
414
415 /* do inband token exchange */
416 reason_code = smc_clc_send_proposal(smc, smcibdev, ibport);
417 if (reason_code < 0) {
418 rc = reason_code;
419 goto out_err;
420 }
421 if (reason_code > 0) /* configuration error */
422 goto decline_rdma;
423 /* receive SMC Accept CLC message */
424 reason_code = smc_clc_wait_msg(smc, &aclc, sizeof(aclc),
425 SMC_CLC_ACCEPT);
426 if (reason_code < 0) {
427 rc = reason_code;
428 goto out_err;
429 }
430 if (reason_code > 0)
431 goto decline_rdma;
432
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100433 srv_first_contact = aclc.hdr.flag;
434 mutex_lock(&smc_create_lgr_pending);
435 local_contact = smc_conn_create(smc, inaddr->sin_addr.s_addr, smcibdev,
436 ibport, &aclc.lcl, srv_first_contact);
437 if (local_contact < 0) {
438 rc = local_contact;
439 if (rc == -ENOMEM)
440 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
441 else if (rc == -ENOLINK)
442 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
443 goto decline_rdma_unlock;
444 }
445 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
Ursula Brauna046d572017-01-09 16:55:16 +0100446
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100447 smc_conn_save_peer_info(smc, &aclc);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100448
Ursula Braun3e034722017-07-28 13:56:20 +0200449 /* create send buffer and rmb */
450 rc = smc_buf_create(smc);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100451 if (rc) {
452 reason_code = SMC_CLC_DECL_MEM;
453 goto decline_rdma_unlock;
454 }
455
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100456 if (local_contact == SMC_FIRST_CONTACT)
457 smc_link_save_peer_info(link, &aclc);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100458
459 rc = smc_rmb_rtoken_handling(&smc->conn, &aclc);
460 if (rc) {
461 reason_code = SMC_CLC_DECL_INTERR;
462 goto decline_rdma_unlock;
463 }
464
Ursula Braun46c28db2017-04-10 14:58:01 +0200465 smc_close_init(smc);
466 smc_rx_init(smc);
467
Ursula Braunbd4ad572017-01-09 16:55:20 +0100468 if (local_contact == SMC_FIRST_CONTACT) {
469 rc = smc_ib_ready_link(link);
470 if (rc) {
471 reason_code = SMC_CLC_DECL_INTERR;
472 goto decline_rdma_unlock;
473 }
Ursula Braun652a1e42017-07-28 13:56:17 +0200474 } else {
475 struct smc_buf_desc *buf_desc = smc->conn.rmb_desc;
476
477 if (!buf_desc->reused) {
478 /* register memory region for new rmb */
479 rc = smc_wr_reg_send(link,
480 buf_desc->mr_rx[SMC_SINGLE_LINK]);
481 if (rc) {
482 reason_code = SMC_CLC_DECL_INTERR;
483 goto decline_rdma_unlock;
484 }
485 }
Ursula Braunbd4ad572017-01-09 16:55:20 +0100486 }
Ursula Braun10428dd2017-07-28 13:56:22 +0200487 smc_rmb_sync_sg_for_device(&smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100488
489 rc = smc_clc_send_confirm(smc);
490 if (rc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100491 goto out_err_unlock;
Ursula Brauna046d572017-01-09 16:55:16 +0100492
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100493 if (local_contact == SMC_FIRST_CONTACT) {
494 /* QP confirmation over RoCE fabric */
495 reason_code = smc_clnt_conf_first_link(
496 smc, &smcibdev->gid[ibport - 1]);
497 if (reason_code < 0) {
498 rc = reason_code;
499 goto out_err_unlock;
500 }
501 if (reason_code > 0)
502 goto decline_rdma_unlock;
503 }
Ursula Brauna046d572017-01-09 16:55:16 +0100504
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100505 mutex_unlock(&smc_create_lgr_pending);
Ursula Braune6727f32017-01-09 16:55:23 +0100506 smc_tx_init(smc);
507
Ursula Brauna046d572017-01-09 16:55:16 +0100508out_connected:
509 smc_copy_sock_settings_to_clc(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +0100510 if (smc->sk.sk_state == SMC_INIT)
511 smc->sk.sk_state = SMC_ACTIVE;
Ursula Brauna046d572017-01-09 16:55:16 +0100512
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100513 return rc ? rc : local_contact;
Ursula Brauna046d572017-01-09 16:55:16 +0100514
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100515decline_rdma_unlock:
516 mutex_unlock(&smc_create_lgr_pending);
517 smc_conn_free(&smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100518decline_rdma:
519 /* RDMA setup failed, switch back to TCP */
520 smc->use_fallback = true;
521 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
Ursula Braunbfbedfd2017-09-21 09:16:32 +0200522 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braun0c9f1512017-12-07 13:38:45 +0100523 if (rc < 0)
Ursula Brauna046d572017-01-09 16:55:16 +0100524 goto out_err;
525 }
526 goto out_connected;
527
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100528out_err_unlock:
529 mutex_unlock(&smc_create_lgr_pending);
530 smc_conn_free(&smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100531out_err:
532 return rc;
533}
534
Ursula Braunac713872017-01-09 16:55:13 +0100535static int smc_connect(struct socket *sock, struct sockaddr *addr,
536 int alen, int flags)
537{
538 struct sock *sk = sock->sk;
539 struct smc_sock *smc;
540 int rc = -EINVAL;
541
542 smc = smc_sk(sk);
543
544 /* separate smc parameter checking to be safe */
545 if (alen < sizeof(addr->sa_family))
546 goto out_err;
547 if (addr->sa_family != AF_INET)
548 goto out_err;
Ursula Brauna046d572017-01-09 16:55:16 +0100549 smc->addr = addr; /* needed for nonblocking connect */
Ursula Braunac713872017-01-09 16:55:13 +0100550
551 lock_sock(sk);
552 switch (sk->sk_state) {
553 default:
554 goto out;
555 case SMC_ACTIVE:
556 rc = -EISCONN;
557 goto out;
558 case SMC_INIT:
559 rc = 0;
560 break;
561 }
562
563 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200564 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +0100565 rc = kernel_connect(smc->clcsock, addr, alen, flags);
566 if (rc)
567 goto out;
568
Ursula Brauna046d572017-01-09 16:55:16 +0100569 /* setup RDMA connection */
570 rc = smc_connect_rdma(smc);
571 if (rc < 0)
572 goto out;
573 else
574 rc = 0; /* success cases including fallback */
Ursula Braunac713872017-01-09 16:55:13 +0100575
576out:
577 release_sock(sk);
578out_err:
579 return rc;
580}
581
582static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
583{
Ursula Braun3163c502018-01-24 10:28:12 +0100584 struct socket *new_clcsock = NULL;
585 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100586 struct sock *new_sk;
587 int rc;
588
Ursula Braun3163c502018-01-24 10:28:12 +0100589 release_sock(lsk);
590 new_sk = smc_sock_alloc(sock_net(lsk), NULL);
Ursula Braunac713872017-01-09 16:55:13 +0100591 if (!new_sk) {
592 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100593 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100594 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100595 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100596 goto out;
597 }
598 *new_smc = smc_sk(new_sk);
599
600 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
Ursula Braun3163c502018-01-24 10:28:12 +0100601 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100602 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100603 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100604 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Brauna046d572017-01-09 16:55:16 +0100605 if (new_clcsock)
606 sock_release(new_clcsock);
607 new_sk->sk_state = SMC_CLOSED;
608 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun3163c502018-01-24 10:28:12 +0100609 new_sk->sk_prot->unhash(new_sk);
Ursula Braunac713872017-01-09 16:55:13 +0100610 sock_put(new_sk);
611 *new_smc = NULL;
612 goto out;
613 }
614
615 (*new_smc)->clcsock = new_clcsock;
616out:
617 return rc;
618}
619
Ursula Brauna046d572017-01-09 16:55:16 +0100620/* add a just created sock to the accept queue of the listen sock as
621 * candidate for a following socket accept call from user space
622 */
623static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
624{
625 struct smc_sock *par = smc_sk(parent);
626
627 sock_hold(sk);
628 spin_lock(&par->accept_q_lock);
629 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
630 spin_unlock(&par->accept_q_lock);
631 sk_acceptq_added(parent);
632}
633
634/* remove a socket from the accept queue of its parental listening socket */
635static void smc_accept_unlink(struct sock *sk)
636{
637 struct smc_sock *par = smc_sk(sk)->listen_smc;
638
639 spin_lock(&par->accept_q_lock);
640 list_del_init(&smc_sk(sk)->accept_q);
641 spin_unlock(&par->accept_q_lock);
642 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
643 sock_put(sk);
644}
645
646/* remove a sock from the accept queue to bind it to a new socket created
647 * for a socket accept call from user space
648 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100649struct sock *smc_accept_dequeue(struct sock *parent,
650 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100651{
652 struct smc_sock *isk, *n;
653 struct sock *new_sk;
654
655 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
656 new_sk = (struct sock *)isk;
657
658 smc_accept_unlink(new_sk);
659 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braun288c83902017-04-10 14:58:04 +0200660 new_sk->sk_prot->unhash(new_sk);
661 sock_put(new_sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100662 continue;
663 }
664 if (new_sock)
665 sock_graft(new_sk, new_sock);
666 return new_sk;
667 }
668 return NULL;
669}
670
671/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +0100672void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +0100673{
674 struct smc_sock *smc = smc_sk(sk);
675
676 sock_hold(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100677 lock_sock(sk);
678 if (!sk->sk_lingertime)
679 /* wait for peer closing */
680 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun288c83902017-04-10 14:58:04 +0200681 if (smc->use_fallback) {
682 sk->sk_state = SMC_CLOSED;
683 } else {
Ursula Braunb38d7322017-01-09 16:55:25 +0100684 smc_close_active(smc);
Ursula Braun288c83902017-04-10 14:58:04 +0200685 sock_set_flag(sk, SOCK_DEAD);
686 sk->sk_shutdown |= SHUTDOWN_MASK;
687 }
Ursula Brauna046d572017-01-09 16:55:16 +0100688 if (smc->clcsock) {
689 struct socket *tcp;
690
691 tcp = smc->clcsock;
692 smc->clcsock = NULL;
693 sock_release(tcp);
694 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100695 if (smc->use_fallback) {
696 schedule_delayed_work(&smc->sock_put_work, TCP_TIMEWAIT_LEN);
Ursula Braun288c83902017-04-10 14:58:04 +0200697 } else if (sk->sk_state == SMC_CLOSED) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100698 smc_conn_free(&smc->conn);
699 schedule_delayed_work(&smc->sock_put_work,
700 SMC_CLOSE_SOCK_PUT_DELAY);
701 }
702 release_sock(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100703 sock_put(sk);
704}
705
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100706static int smc_serv_conf_first_link(struct smc_sock *smc)
707{
708 struct smc_link_group *lgr = smc->conn.lgr;
709 struct smc_link *link;
710 int rest;
711 int rc;
712
713 link = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun652a1e42017-07-28 13:56:17 +0200714
715 rc = smc_wr_reg_send(link,
716 smc->conn.rmb_desc->mr_rx[SMC_SINGLE_LINK]);
717 if (rc)
718 return SMC_CLC_DECL_INTERR;
719
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100720 /* send CONFIRM LINK request to client over the RoCE fabric */
721 rc = smc_llc_send_confirm_link(link,
722 link->smcibdev->mac[link->ibport - 1],
723 &link->smcibdev->gid[link->ibport - 1],
724 SMC_LLC_REQ);
725 if (rc < 0)
726 return SMC_CLC_DECL_TCL;
727
728 /* receive CONFIRM LINK response from client over the RoCE fabric */
729 rest = wait_for_completion_interruptible_timeout(
730 &link->llc_confirm_resp,
731 SMC_LLC_WAIT_FIRST_TIME);
732 if (rest <= 0) {
733 struct smc_clc_msg_decline dclc;
734
735 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
736 SMC_CLC_DECLINE);
737 }
738
739 return rc;
740}
741
Ursula Brauna046d572017-01-09 16:55:16 +0100742/* setup for RDMA connection of server */
743static void smc_listen_work(struct work_struct *work)
744{
745 struct smc_sock *new_smc = container_of(work, struct smc_sock,
746 smc_listen_work);
Ursula Braune7b7a642017-12-07 13:38:49 +0100747 struct smc_clc_msg_proposal_prefix *pclc_prfx;
Ursula Brauna046d572017-01-09 16:55:16 +0100748 struct socket *newclcsock = new_smc->clcsock;
749 struct smc_sock *lsmc = new_smc->listen_smc;
750 struct smc_clc_msg_accept_confirm cclc;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100751 int local_contact = SMC_REUSE_CONTACT;
Ursula Brauna046d572017-01-09 16:55:16 +0100752 struct sock *newsmcsk = &new_smc->sk;
Ursula Braune7b7a642017-12-07 13:38:49 +0100753 struct smc_clc_msg_proposal *pclc;
Ursula Brauna046d572017-01-09 16:55:16 +0100754 struct smc_ib_device *smcibdev;
755 struct sockaddr_in peeraddr;
Ursula Braune7b7a642017-12-07 13:38:49 +0100756 u8 buf[SMC_CLC_MAX_LEN];
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100757 struct smc_link *link;
Ursula Brauna046d572017-01-09 16:55:16 +0100758 int reason_code = 0;
759 int rc = 0, len;
760 __be32 subnet;
761 u8 prefix_len;
762 u8 ibport;
763
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200764 /* check if peer is smc capable */
765 if (!tcp_sk(newclcsock->sk)->syn_smc) {
766 new_smc->use_fallback = true;
767 goto out_connected;
768 }
769
Ursula Brauna046d572017-01-09 16:55:16 +0100770 /* do inband token exchange -
771 *wait for and receive SMC Proposal CLC message
772 */
Ursula Braune7b7a642017-12-07 13:38:49 +0100773 reason_code = smc_clc_wait_msg(new_smc, &buf, sizeof(buf),
Ursula Brauna046d572017-01-09 16:55:16 +0100774 SMC_CLC_PROPOSAL);
775 if (reason_code < 0)
776 goto out_err;
777 if (reason_code > 0)
778 goto decline_rdma;
779
780 /* IPSec connections opt out of SMC-R optimizations */
781 if (using_ipsec(new_smc)) {
782 reason_code = SMC_CLC_DECL_IPSEC;
783 goto decline_rdma;
784 }
785
786 /* PNET table look up: search active ib_device and port
787 * within same PNETID that also contains the ethernet device
788 * used for the internal TCP socket
789 */
790 smc_pnet_find_roce_resource(newclcsock->sk, &smcibdev, &ibport);
791 if (!smcibdev) {
792 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
793 goto decline_rdma;
794 }
795
796 /* determine subnet and mask from internal TCP socket */
797 rc = smc_netinfo_by_tcpsk(newclcsock, &subnet, &prefix_len);
798 if (rc) {
799 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
800 goto decline_rdma;
801 }
Ursula Braune7b7a642017-12-07 13:38:49 +0100802
803 pclc = (struct smc_clc_msg_proposal *)&buf;
804 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
805 if (pclc_prfx->outgoing_subnet != subnet ||
806 pclc_prfx->prefix_len != prefix_len) {
Ursula Brauna046d572017-01-09 16:55:16 +0100807 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
808 goto decline_rdma;
809 }
810
811 /* get address of the peer connected to the internal TCP socket */
812 kernel_getpeername(newclcsock, (struct sockaddr *)&peeraddr, &len);
813
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100814 /* allocate connection / link group */
815 mutex_lock(&smc_create_lgr_pending);
816 local_contact = smc_conn_create(new_smc, peeraddr.sin_addr.s_addr,
Ursula Braune7b7a642017-12-07 13:38:49 +0100817 smcibdev, ibport, &pclc->lcl, 0);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100818 if (local_contact < 0) {
819 rc = local_contact;
820 if (rc == -ENOMEM)
821 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
Ursula Braun145686b2017-10-25 11:01:44 +0200822 goto decline_rdma_unlock;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100823 }
824 link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
Ursula Brauna046d572017-01-09 16:55:16 +0100825
Ursula Braun3e034722017-07-28 13:56:20 +0200826 /* create send buffer and rmb */
827 rc = smc_buf_create(new_smc);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100828 if (rc) {
829 reason_code = SMC_CLC_DECL_MEM;
Ursula Braun145686b2017-10-25 11:01:44 +0200830 goto decline_rdma_unlock;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100831 }
Ursula Brauna046d572017-01-09 16:55:16 +0100832
Ursula Braun46c28db2017-04-10 14:58:01 +0200833 smc_close_init(new_smc);
834 smc_rx_init(new_smc);
835
Ursula Braun652a1e42017-07-28 13:56:17 +0200836 if (local_contact != SMC_FIRST_CONTACT) {
837 struct smc_buf_desc *buf_desc = new_smc->conn.rmb_desc;
838
839 if (!buf_desc->reused) {
840 /* register memory region for new rmb */
841 rc = smc_wr_reg_send(link,
842 buf_desc->mr_rx[SMC_SINGLE_LINK]);
843 if (rc) {
844 reason_code = SMC_CLC_DECL_INTERR;
Ursula Braun145686b2017-10-25 11:01:44 +0200845 goto decline_rdma_unlock;
Ursula Braun652a1e42017-07-28 13:56:17 +0200846 }
847 }
848 }
Ursula Braun10428dd2017-07-28 13:56:22 +0200849 smc_rmb_sync_sg_for_device(&new_smc->conn);
Ursula Braun652a1e42017-07-28 13:56:17 +0200850
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100851 rc = smc_clc_send_accept(new_smc, local_contact);
Ursula Brauna046d572017-01-09 16:55:16 +0100852 if (rc)
Ursula Braun145686b2017-10-25 11:01:44 +0200853 goto out_err_unlock;
Ursula Brauna046d572017-01-09 16:55:16 +0100854
855 /* receive SMC Confirm CLC message */
856 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
857 SMC_CLC_CONFIRM);
858 if (reason_code < 0)
Ursula Braun145686b2017-10-25 11:01:44 +0200859 goto out_err_unlock;
Ursula Brauna046d572017-01-09 16:55:16 +0100860 if (reason_code > 0)
Ursula Braun145686b2017-10-25 11:01:44 +0200861 goto decline_rdma_unlock;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100862 smc_conn_save_peer_info(new_smc, &cclc);
863 if (local_contact == SMC_FIRST_CONTACT)
864 smc_link_save_peer_info(link, &cclc);
Ursula Brauna046d572017-01-09 16:55:16 +0100865
Ursula Braunbd4ad572017-01-09 16:55:20 +0100866 rc = smc_rmb_rtoken_handling(&new_smc->conn, &cclc);
867 if (rc) {
868 reason_code = SMC_CLC_DECL_INTERR;
Ursula Braun145686b2017-10-25 11:01:44 +0200869 goto decline_rdma_unlock;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100870 }
871
Ursula Braunbd4ad572017-01-09 16:55:20 +0100872 if (local_contact == SMC_FIRST_CONTACT) {
873 rc = smc_ib_ready_link(link);
874 if (rc) {
875 reason_code = SMC_CLC_DECL_INTERR;
Ursula Braun145686b2017-10-25 11:01:44 +0200876 goto decline_rdma_unlock;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100877 }
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100878 /* QP confirmation over RoCE fabric */
879 reason_code = smc_serv_conf_first_link(new_smc);
Ursula Braun0c9f1512017-12-07 13:38:45 +0100880 if (reason_code < 0)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100881 /* peer is not aware of a problem */
Ursula Braun145686b2017-10-25 11:01:44 +0200882 goto out_err_unlock;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100883 if (reason_code > 0)
Ursula Braun145686b2017-10-25 11:01:44 +0200884 goto decline_rdma_unlock;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100885 }
Ursula Brauna046d572017-01-09 16:55:16 +0100886
Ursula Braune6727f32017-01-09 16:55:23 +0100887 smc_tx_init(new_smc);
Ursula Braun145686b2017-10-25 11:01:44 +0200888 mutex_unlock(&smc_create_lgr_pending);
Ursula Braune6727f32017-01-09 16:55:23 +0100889
Ursula Brauna046d572017-01-09 16:55:16 +0100890out_connected:
891 sk_refcnt_debug_inc(newsmcsk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100892 if (newsmcsk->sk_state == SMC_INIT)
893 newsmcsk->sk_state = SMC_ACTIVE;
Ursula Brauna046d572017-01-09 16:55:16 +0100894enqueue:
Ursula Braunb38d7322017-01-09 16:55:25 +0100895 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +0100896 if (lsmc->sk.sk_state == SMC_LISTEN) {
897 smc_accept_enqueue(&lsmc->sk, newsmcsk);
898 } else { /* no longer listening */
899 smc_close_non_accepted(newsmcsk);
900 }
901 release_sock(&lsmc->sk);
902
903 /* Wake up accept */
904 lsmc->sk.sk_data_ready(&lsmc->sk);
905 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
906 return;
907
Ursula Braun145686b2017-10-25 11:01:44 +0200908decline_rdma_unlock:
909 mutex_unlock(&smc_create_lgr_pending);
Ursula Brauna046d572017-01-09 16:55:16 +0100910decline_rdma:
911 /* RDMA setup failed, switch back to TCP */
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100912 smc_conn_free(&new_smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100913 new_smc->use_fallback = true;
914 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
Ursula Braun0c9f1512017-12-07 13:38:45 +0100915 if (smc_clc_send_decline(new_smc, reason_code) < 0)
Ursula Brauna046d572017-01-09 16:55:16 +0100916 goto out_err;
917 }
918 goto out_connected;
919
Ursula Braun145686b2017-10-25 11:01:44 +0200920out_err_unlock:
921 mutex_unlock(&smc_create_lgr_pending);
Ursula Brauna046d572017-01-09 16:55:16 +0100922out_err:
923 newsmcsk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100924 smc_conn_free(&new_smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100925 goto enqueue; /* queue new sock with sk_err set */
926}
927
928static void smc_tcp_listen_work(struct work_struct *work)
929{
930 struct smc_sock *lsmc = container_of(work, struct smc_sock,
931 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +0100932 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +0100933 struct smc_sock *new_smc;
934 int rc = 0;
935
Ursula Braun3163c502018-01-24 10:28:12 +0100936 lock_sock(lsk);
937 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +0100938 rc = smc_clcsock_accept(lsmc, &new_smc);
939 if (rc)
940 goto out;
941 if (!new_smc)
942 continue;
943
944 new_smc->listen_smc = lsmc;
945 new_smc->use_fallback = false; /* assume rdma capability first*/
Ursula Braun3163c502018-01-24 10:28:12 +0100946 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +0100947 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
948 smc_copy_sock_settings_to_smc(new_smc);
949 schedule_work(&new_smc->smc_listen_work);
950 }
951
952out:
Ursula Braun3163c502018-01-24 10:28:12 +0100953 release_sock(lsk);
954 lsk->sk_data_ready(lsk); /* no more listening, wake accept */
Ursula Brauna046d572017-01-09 16:55:16 +0100955}
956
Ursula Braunac713872017-01-09 16:55:13 +0100957static int smc_listen(struct socket *sock, int backlog)
958{
959 struct sock *sk = sock->sk;
960 struct smc_sock *smc;
961 int rc;
962
963 smc = smc_sk(sk);
964 lock_sock(sk);
965
966 rc = -EINVAL;
967 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
968 goto out;
969
970 rc = 0;
971 if (sk->sk_state == SMC_LISTEN) {
972 sk->sk_max_ack_backlog = backlog;
973 goto out;
974 }
975 /* some socket options are handled in core, so we could not apply
976 * them to the clc socket -- copy smc socket options to clc socket
977 */
978 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200979 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +0100980
981 rc = kernel_listen(smc->clcsock, backlog);
982 if (rc)
983 goto out;
984 sk->sk_max_ack_backlog = backlog;
985 sk->sk_ack_backlog = 0;
986 sk->sk_state = SMC_LISTEN;
Ursula Brauna046d572017-01-09 16:55:16 +0100987 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
988 schedule_work(&smc->tcp_listen_work);
Ursula Braunac713872017-01-09 16:55:13 +0100989
990out:
991 release_sock(sk);
992 return rc;
993}
994
995static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +0000996 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +0100997{
Ursula Brauna046d572017-01-09 16:55:16 +0100998 struct sock *sk = sock->sk, *nsk;
999 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001000 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001001 long timeo;
1002 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001003
1004 lsmc = smc_sk(sk);
1005 lock_sock(sk);
1006
1007 if (lsmc->sk.sk_state != SMC_LISTEN) {
1008 rc = -EINVAL;
1009 goto out;
1010 }
1011
Ursula Brauna046d572017-01-09 16:55:16 +01001012 /* Wait for an incoming connection */
1013 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1014 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1015 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1016 set_current_state(TASK_INTERRUPTIBLE);
1017 if (!timeo) {
1018 rc = -EAGAIN;
1019 break;
1020 }
1021 release_sock(sk);
1022 timeo = schedule_timeout(timeo);
1023 /* wakeup by sk_data_ready in smc_listen_work() */
1024 sched_annotate_sleep();
1025 lock_sock(sk);
1026 if (signal_pending(current)) {
1027 rc = sock_intr_errno(timeo);
1028 break;
1029 }
1030 }
1031 set_current_state(TASK_RUNNING);
1032 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001033
Ursula Brauna046d572017-01-09 16:55:16 +01001034 if (!rc)
1035 rc = sock_error(nsk);
Ursula Braunac713872017-01-09 16:55:13 +01001036
1037out:
1038 release_sock(sk);
1039 return rc;
1040}
1041
1042static int smc_getname(struct socket *sock, struct sockaddr *addr,
1043 int *len, int peer)
1044{
1045 struct smc_sock *smc;
1046
Ursula Braunb38d7322017-01-09 16:55:25 +01001047 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1048 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001049 return -ENOTCONN;
1050
1051 smc = smc_sk(sock->sk);
1052
1053 return smc->clcsock->ops->getname(smc->clcsock, addr, len, peer);
1054}
1055
1056static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1057{
1058 struct sock *sk = sock->sk;
1059 struct smc_sock *smc;
1060 int rc = -EPIPE;
1061
1062 smc = smc_sk(sk);
1063 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001064 if ((sk->sk_state != SMC_ACTIVE) &&
1065 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1066 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001067 goto out;
1068 if (smc->use_fallback)
1069 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1070 else
Ursula Braune6727f32017-01-09 16:55:23 +01001071 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001072out:
1073 release_sock(sk);
1074 return rc;
1075}
1076
1077static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1078 int flags)
1079{
1080 struct sock *sk = sock->sk;
1081 struct smc_sock *smc;
1082 int rc = -ENOTCONN;
1083
1084 smc = smc_sk(sk);
1085 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001086 if ((sk->sk_state == SMC_INIT) ||
1087 (sk->sk_state == SMC_LISTEN) ||
1088 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001089 goto out;
1090
Ursula Braunb38d7322017-01-09 16:55:25 +01001091 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1092 rc = 0;
1093 goto out;
1094 }
1095
Ursula Braunac713872017-01-09 16:55:13 +01001096 if (smc->use_fallback)
1097 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
1098 else
Ursula Braun952310c2017-01-09 16:55:24 +01001099 rc = smc_rx_recvmsg(smc, msg, len, flags);
Ursula Braunb38d7322017-01-09 16:55:25 +01001100
Ursula Braunac713872017-01-09 16:55:13 +01001101out:
1102 release_sock(sk);
1103 return rc;
1104}
1105
Ursula Brauna046d572017-01-09 16:55:16 +01001106static unsigned int smc_accept_poll(struct sock *parent)
1107{
1108 struct smc_sock *isk;
1109 struct sock *sk;
1110
1111 lock_sock(parent);
1112 list_for_each_entry(isk, &smc_sk(parent)->accept_q, accept_q) {
1113 sk = (struct sock *)isk;
1114
1115 if (sk->sk_state == SMC_ACTIVE) {
1116 release_sock(parent);
1117 return POLLIN | POLLRDNORM;
1118 }
1119 }
1120 release_sock(parent);
1121
1122 return 0;
1123}
1124
Ursula Braunac713872017-01-09 16:55:13 +01001125static unsigned int smc_poll(struct file *file, struct socket *sock,
1126 poll_table *wait)
1127{
1128 struct sock *sk = sock->sk;
1129 unsigned int mask = 0;
1130 struct smc_sock *smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001131 int rc;
Ursula Braunac713872017-01-09 16:55:13 +01001132
1133 smc = smc_sk(sock->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001134 if ((sk->sk_state == SMC_INIT) || smc->use_fallback) {
1135 /* delegate to CLC child sock */
Ursula Braunac713872017-01-09 16:55:13 +01001136 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
1137 /* if non-blocking connect finished ... */
1138 lock_sock(sk);
1139 if ((sk->sk_state == SMC_INIT) && (mask & POLLOUT)) {
Ursula Brauna046d572017-01-09 16:55:16 +01001140 sk->sk_err = smc->clcsock->sk->sk_err;
1141 if (sk->sk_err) {
1142 mask |= POLLERR;
1143 } else {
1144 rc = smc_connect_rdma(smc);
1145 if (rc < 0)
1146 mask |= POLLERR;
1147 else
1148 /* success cases including fallback */
1149 mask |= POLLOUT | POLLWRNORM;
1150 }
Ursula Braunac713872017-01-09 16:55:13 +01001151 }
1152 release_sock(sk);
1153 } else {
Ursula Brauna046d572017-01-09 16:55:16 +01001154 sock_poll_wait(file, sk_sleep(sk), wait);
1155 if (sk->sk_state == SMC_LISTEN)
1156 /* woken up by sk_data_ready in smc_listen_work() */
1157 mask |= smc_accept_poll(sk);
1158 if (sk->sk_err)
1159 mask |= POLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001160 if (atomic_read(&smc->conn.sndbuf_space) ||
1161 (sk->sk_shutdown & SEND_SHUTDOWN)) {
Ursula Braune6727f32017-01-09 16:55:23 +01001162 mask |= POLLOUT | POLLWRNORM;
1163 } else {
1164 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1165 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1166 }
Ursula Braun952310c2017-01-09 16:55:24 +01001167 if (atomic_read(&smc->conn.bytes_to_rcv))
1168 mask |= POLLIN | POLLRDNORM;
Ursula Braunb38d7322017-01-09 16:55:25 +01001169 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1170 (sk->sk_state == SMC_CLOSED))
1171 mask |= POLLHUP;
1172 if (sk->sk_shutdown & RCV_SHUTDOWN)
1173 mask |= POLLIN | POLLRDNORM | POLLRDHUP;
1174 if (sk->sk_state == SMC_APPCLOSEWAIT1)
1175 mask |= POLLIN;
1176
Ursula Braunac713872017-01-09 16:55:13 +01001177 }
1178
1179 return mask;
1180}
1181
1182static int smc_shutdown(struct socket *sock, int how)
1183{
1184 struct sock *sk = sock->sk;
1185 struct smc_sock *smc;
1186 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001187 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001188
1189 smc = smc_sk(sk);
1190
1191 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001192 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001193
1194 lock_sock(sk);
1195
1196 rc = -ENOTCONN;
Ursula Braunb38d7322017-01-09 16:55:25 +01001197 if ((sk->sk_state != SMC_LISTEN) &&
1198 (sk->sk_state != SMC_ACTIVE) &&
1199 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1200 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1201 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1202 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1203 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001204 goto out;
1205 if (smc->use_fallback) {
1206 rc = kernel_sock_shutdown(smc->clcsock, how);
1207 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1208 if (sk->sk_shutdown == SHUTDOWN_MASK)
1209 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001210 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001211 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001212 switch (how) {
1213 case SHUT_RDWR: /* shutdown in both directions */
1214 rc = smc_close_active(smc);
1215 break;
1216 case SHUT_WR:
1217 rc = smc_close_shutdown_write(smc);
1218 break;
1219 case SHUT_RD:
1220 if (sk->sk_state == SMC_LISTEN)
1221 rc = smc_close_active(smc);
1222 else
1223 rc = 0;
1224 /* nothing more to do because peer is not involved */
1225 break;
1226 }
1227 rc1 = kernel_sock_shutdown(smc->clcsock, how);
1228 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1229 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001230
1231out:
1232 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001233 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001234}
1235
1236static int smc_setsockopt(struct socket *sock, int level, int optname,
1237 char __user *optval, unsigned int optlen)
1238{
1239 struct sock *sk = sock->sk;
1240 struct smc_sock *smc;
1241
1242 smc = smc_sk(sk);
1243
1244 /* generic setsockopts reaching us here always apply to the
1245 * CLC socket
1246 */
1247 return smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1248 optval, optlen);
1249}
1250
1251static int smc_getsockopt(struct socket *sock, int level, int optname,
1252 char __user *optval, int __user *optlen)
1253{
1254 struct smc_sock *smc;
1255
1256 smc = smc_sk(sock->sk);
1257 /* socket options apply to the CLC socket */
1258 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1259 optval, optlen);
1260}
1261
1262static int smc_ioctl(struct socket *sock, unsigned int cmd,
1263 unsigned long arg)
1264{
1265 struct smc_sock *smc;
1266
1267 smc = smc_sk(sock->sk);
1268 if (smc->use_fallback)
1269 return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1270 else
1271 return sock_no_ioctl(sock, cmd, arg);
1272}
1273
1274static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1275 int offset, size_t size, int flags)
1276{
1277 struct sock *sk = sock->sk;
1278 struct smc_sock *smc;
1279 int rc = -EPIPE;
1280
1281 smc = smc_sk(sk);
1282 lock_sock(sk);
1283 if (sk->sk_state != SMC_ACTIVE)
1284 goto out;
1285 if (smc->use_fallback)
1286 rc = kernel_sendpage(smc->clcsock, page, offset,
1287 size, flags);
1288 else
1289 rc = sock_no_sendpage(sock, page, offset, size, flags);
1290
1291out:
1292 release_sock(sk);
1293 return rc;
1294}
1295
1296static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1297 struct pipe_inode_info *pipe, size_t len,
1298 unsigned int flags)
1299{
1300 struct sock *sk = sock->sk;
1301 struct smc_sock *smc;
1302 int rc = -ENOTCONN;
1303
1304 smc = smc_sk(sk);
1305 lock_sock(sk);
1306 if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED))
1307 goto out;
1308 if (smc->use_fallback) {
1309 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1310 pipe, len, flags);
1311 } else {
1312 rc = -EOPNOTSUPP;
1313 }
1314out:
1315 release_sock(sk);
1316 return rc;
1317}
1318
1319/* must look like tcp */
1320static const struct proto_ops smc_sock_ops = {
1321 .family = PF_SMC,
1322 .owner = THIS_MODULE,
1323 .release = smc_release,
1324 .bind = smc_bind,
1325 .connect = smc_connect,
1326 .socketpair = sock_no_socketpair,
1327 .accept = smc_accept,
1328 .getname = smc_getname,
1329 .poll = smc_poll,
1330 .ioctl = smc_ioctl,
1331 .listen = smc_listen,
1332 .shutdown = smc_shutdown,
1333 .setsockopt = smc_setsockopt,
1334 .getsockopt = smc_getsockopt,
1335 .sendmsg = smc_sendmsg,
1336 .recvmsg = smc_recvmsg,
1337 .mmap = sock_no_mmap,
1338 .sendpage = smc_sendpage,
1339 .splice_read = smc_splice_read,
1340};
1341
1342static int smc_create(struct net *net, struct socket *sock, int protocol,
1343 int kern)
1344{
1345 struct smc_sock *smc;
1346 struct sock *sk;
1347 int rc;
1348
1349 rc = -ESOCKTNOSUPPORT;
1350 if (sock->type != SOCK_STREAM)
1351 goto out;
1352
1353 rc = -EPROTONOSUPPORT;
1354 if ((protocol != IPPROTO_IP) && (protocol != IPPROTO_TCP))
1355 goto out;
1356
1357 rc = -ENOBUFS;
1358 sock->ops = &smc_sock_ops;
1359 sk = smc_sock_alloc(net, sock);
1360 if (!sk)
1361 goto out;
1362
1363 /* create internal TCP socket for CLC handshake and fallback */
1364 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001365 smc->use_fallback = false; /* assume rdma capability first */
Ursula Braunac713872017-01-09 16:55:13 +01001366 rc = sock_create_kern(net, PF_INET, SOCK_STREAM,
1367 IPPROTO_TCP, &smc->clcsock);
1368 if (rc)
1369 sk_common_release(sk);
Ursula Brauncd6851f2017-01-09 16:55:18 +01001370 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1371 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01001372
1373out:
1374 return rc;
1375}
1376
1377static const struct net_proto_family smc_sock_family_ops = {
1378 .family = PF_SMC,
1379 .owner = THIS_MODULE,
1380 .create = smc_create,
1381};
1382
1383static int __init smc_init(void)
1384{
1385 int rc;
1386
Thomas Richter6812baa2017-01-09 16:55:15 +01001387 rc = smc_pnet_init();
1388 if (rc)
1389 return rc;
1390
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001391 rc = smc_llc_init();
1392 if (rc) {
1393 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1394 goto out_pnet;
1395 }
1396
Ursula Braun5f083182017-01-09 16:55:22 +01001397 rc = smc_cdc_init();
1398 if (rc) {
1399 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1400 goto out_pnet;
1401 }
1402
Ursula Braunac713872017-01-09 16:55:13 +01001403 rc = proto_register(&smc_proto, 1);
1404 if (rc) {
1405 pr_err("%s: proto_register fails with %d\n", __func__, rc);
Thomas Richter6812baa2017-01-09 16:55:15 +01001406 goto out_pnet;
Ursula Braunac713872017-01-09 16:55:13 +01001407 }
1408
1409 rc = sock_register(&smc_sock_family_ops);
1410 if (rc) {
1411 pr_err("%s: sock_register fails with %d\n", __func__, rc);
1412 goto out_proto;
1413 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01001414 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01001415
Ursula Brauna4cf0442017-01-09 16:55:14 +01001416 rc = smc_ib_register_client();
1417 if (rc) {
1418 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1419 goto out_sock;
1420 }
1421
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001422 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01001423 return 0;
1424
Ursula Brauna4cf0442017-01-09 16:55:14 +01001425out_sock:
1426 sock_unregister(PF_SMC);
Ursula Braunac713872017-01-09 16:55:13 +01001427out_proto:
1428 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001429out_pnet:
1430 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001431 return rc;
1432}
1433
1434static void __exit smc_exit(void)
1435{
Ursula Braun0cfdd8f2017-01-09 16:55:17 +01001436 struct smc_link_group *lgr, *lg;
1437 LIST_HEAD(lgr_freeing_list);
1438
1439 spin_lock_bh(&smc_lgr_list.lock);
1440 if (!list_empty(&smc_lgr_list.list))
1441 list_splice_init(&smc_lgr_list.list, &lgr_freeing_list);
1442 spin_unlock_bh(&smc_lgr_list.lock);
1443 list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) {
1444 list_del_init(&lgr->list);
1445 smc_lgr_free(lgr); /* free link group */
1446 }
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001447 static_branch_disable(&tcp_have_smc);
Ursula Brauna4cf0442017-01-09 16:55:14 +01001448 smc_ib_unregister_client();
Ursula Braunac713872017-01-09 16:55:13 +01001449 sock_unregister(PF_SMC);
1450 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001451 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001452}
1453
1454module_init(smc_init);
1455module_exit(smc_exit);
1456
1457MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
1458MODULE_DESCRIPTION("smc socket address family");
1459MODULE_LICENSE("GPL");
1460MODULE_ALIAS_NETPROTO(PF_SMC);