blob: 83403be46a4a91caa45c1db07894eec87332fa74 [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:
Ursula Brauna046d572017-01-09 16:55:16 +010010 * - support for alternate links postponed
11 * - partial support for non-blocking sockets only
12 * - support for urgent data postponed
13 *
Karsten Graulaaa4d332018-03-16 15:06:41 +010014 * Copyright IBM Corp. 2016, 2018
Ursula Braunac713872017-01-09 16:55:13 +010015 *
16 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
17 * based on prototype from Frank Blaschka
18 */
19
20#define KMSG_COMPONENT "smc"
21#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
22
23#include <linux/module.h>
24#include <linux/socket.h>
Ursula Brauna046d572017-01-09 16:55:16 +010025#include <linux/workqueue.h>
Ursula Braun5f083182017-01-09 16:55:22 +010026#include <linux/in.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010027#include <linux/sched/signal.h>
28
Ursula Braunac713872017-01-09 16:55:13 +010029#include <net/sock.h>
Ursula Brauna046d572017-01-09 16:55:16 +010030#include <net/tcp.h>
Ursula Braunf16a7dd2017-01-09 16:55:26 +010031#include <net/smc.h>
Ursula Braun9b67e262018-05-02 16:56:46 +020032#include <asm/ioctls.h>
Ursula Braunac713872017-01-09 16:55:13 +010033
34#include "smc.h"
Ursula Brauna046d572017-01-09 16:55:16 +010035#include "smc_clc.h"
Ursula Braun9bf9abe2017-01-09 16:55:21 +010036#include "smc_llc.h"
Ursula Braun5f083182017-01-09 16:55:22 +010037#include "smc_cdc.h"
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010038#include "smc_core.h"
Ursula Brauna4cf0442017-01-09 16:55:14 +010039#include "smc_ib.h"
Thomas Richter6812baa2017-01-09 16:55:15 +010040#include "smc_pnet.h"
Ursula Braune6727f32017-01-09 16:55:23 +010041#include "smc_tx.h"
Ursula Braun952310c2017-01-09 16:55:24 +010042#include "smc_rx.h"
Ursula Braunb38d7322017-01-09 16:55:25 +010043#include "smc_close.h"
Ursula Braunac713872017-01-09 16:55:13 +010044
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010045static DEFINE_MUTEX(smc_create_lgr_pending); /* serialize link group
46 * creation
47 */
48
49struct smc_lgr_list smc_lgr_list = { /* established link groups */
50 .lock = __SPIN_LOCK_UNLOCKED(smc_lgr_list.lock),
51 .list = LIST_HEAD_INIT(smc_lgr_list.list),
52};
53
Ursula Brauna046d572017-01-09 16:55:16 +010054static void smc_tcp_listen_work(struct work_struct *);
55
Ursula Braunac713872017-01-09 16:55:13 +010056static void smc_set_keepalive(struct sock *sk, int val)
57{
58 struct smc_sock *smc = smc_sk(sk);
59
60 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
61}
62
Ursula Braunf16a7dd2017-01-09 16:55:26 +010063static struct smc_hashinfo smc_v4_hashinfo = {
64 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
65};
66
Karsten Graulaaa4d332018-03-16 15:06:41 +010067static struct smc_hashinfo smc_v6_hashinfo = {
68 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
69};
70
Ursula Braunf16a7dd2017-01-09 16:55:26 +010071int smc_hash_sk(struct sock *sk)
72{
73 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
74 struct hlist_head *head;
75
76 head = &h->ht;
77
78 write_lock_bh(&h->lock);
79 sk_add_node(sk, head);
80 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
81 write_unlock_bh(&h->lock);
82
83 return 0;
84}
85EXPORT_SYMBOL_GPL(smc_hash_sk);
86
87void smc_unhash_sk(struct sock *sk)
88{
89 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
90
91 write_lock_bh(&h->lock);
92 if (sk_del_node_init(sk))
93 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
94 write_unlock_bh(&h->lock);
95}
96EXPORT_SYMBOL_GPL(smc_unhash_sk);
97
98struct proto smc_proto = {
Ursula Braunac713872017-01-09 16:55:13 +010099 .name = "SMC",
100 .owner = THIS_MODULE,
101 .keepalive = smc_set_keepalive,
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100102 .hash = smc_hash_sk,
103 .unhash = smc_unhash_sk,
Ursula Braunac713872017-01-09 16:55:13 +0100104 .obj_size = sizeof(struct smc_sock),
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100105 .h.smc_hash = &smc_v4_hashinfo,
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800106 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Ursula Braunac713872017-01-09 16:55:13 +0100107};
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100108EXPORT_SYMBOL_GPL(smc_proto);
Ursula Braunac713872017-01-09 16:55:13 +0100109
Karsten Graulaaa4d332018-03-16 15:06:41 +0100110struct proto smc_proto6 = {
111 .name = "SMC6",
112 .owner = THIS_MODULE,
113 .keepalive = smc_set_keepalive,
114 .hash = smc_hash_sk,
115 .unhash = smc_unhash_sk,
116 .obj_size = sizeof(struct smc_sock),
117 .h.smc_hash = &smc_v6_hashinfo,
118 .slab_flags = SLAB_TYPESAFE_BY_RCU,
119};
120EXPORT_SYMBOL_GPL(smc_proto6);
121
Ursula Braunac713872017-01-09 16:55:13 +0100122static int smc_release(struct socket *sock)
123{
124 struct sock *sk = sock->sk;
125 struct smc_sock *smc;
Ursula Braunb38d7322017-01-09 16:55:25 +0100126 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +0100127
128 if (!sk)
129 goto out;
130
131 smc = smc_sk(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100132 if (sk->sk_state == SMC_LISTEN)
133 /* smc_close_non_accepted() is called and acquires
134 * sock lock for child sockets again
135 */
136 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
137 else
138 lock_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +0100139
Ursula Braun51f1de72018-01-26 09:28:48 +0100140 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100141 rc = smc_close_active(smc);
142 sock_set_flag(sk, SOCK_DEAD);
143 sk->sk_shutdown |= SHUTDOWN_MASK;
144 }
Ursula Braunac713872017-01-09 16:55:13 +0100145 if (smc->clcsock) {
146 sock_release(smc->clcsock);
147 smc->clcsock = NULL;
148 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100149 if (smc->use_fallback) {
150 sock_put(sk); /* passive closing */
151 sk->sk_state = SMC_CLOSED;
152 sk->sk_state_change(sk);
153 }
Ursula Braunac713872017-01-09 16:55:13 +0100154
155 /* detach socket */
156 sock_orphan(sk);
157 sock->sk = NULL;
Ursula Braun51f1de72018-01-26 09:28:48 +0100158 if (!smc->use_fallback && sk->sk_state == SMC_CLOSED)
Ursula Braunb38d7322017-01-09 16:55:25 +0100159 smc_conn_free(&smc->conn);
Ursula Braunac713872017-01-09 16:55:13 +0100160 release_sock(sk);
161
Ursula Braun51f1de72018-01-26 09:28:48 +0100162 sk->sk_prot->unhash(sk);
163 sock_put(sk); /* final sock_put */
Ursula Braunac713872017-01-09 16:55:13 +0100164out:
Ursula Braunb38d7322017-01-09 16:55:25 +0100165 return rc;
Ursula Braunac713872017-01-09 16:55:13 +0100166}
167
168static void smc_destruct(struct sock *sk)
169{
170 if (sk->sk_state != SMC_CLOSED)
171 return;
172 if (!sock_flag(sk, SOCK_DEAD))
173 return;
174
175 sk_refcnt_debug_dec(sk);
176}
177
Karsten Graulaaa4d332018-03-16 15:06:41 +0100178static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
179 int protocol)
Ursula Braunac713872017-01-09 16:55:13 +0100180{
181 struct smc_sock *smc;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100182 struct proto *prot;
Ursula Braunac713872017-01-09 16:55:13 +0100183 struct sock *sk;
184
Karsten Graulaaa4d332018-03-16 15:06:41 +0100185 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
186 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
Ursula Braunac713872017-01-09 16:55:13 +0100187 if (!sk)
188 return NULL;
189
190 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
191 sk->sk_state = SMC_INIT;
192 sk->sk_destruct = smc_destruct;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100193 sk->sk_protocol = protocol;
Ursula Braunac713872017-01-09 16:55:13 +0100194 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100195 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
196 INIT_LIST_HEAD(&smc->accept_q);
197 spin_lock_init(&smc->accept_q_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100198 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100199 sk_refcnt_debug_inc(sk);
Ursula Braunac713872017-01-09 16:55:13 +0100200
201 return sk;
202}
203
204static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
205 int addr_len)
206{
207 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
208 struct sock *sk = sock->sk;
209 struct smc_sock *smc;
210 int rc;
211
212 smc = smc_sk(sk);
213
214 /* replicate tests from inet_bind(), to be safe wrt. future changes */
215 rc = -EINVAL;
216 if (addr_len < sizeof(struct sockaddr_in))
217 goto out;
218
219 rc = -EAFNOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100220 if (addr->sin_family != AF_INET &&
221 addr->sin_family != AF_INET6 &&
222 addr->sin_family != AF_UNSPEC)
223 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100224 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
Karsten Graulaaa4d332018-03-16 15:06:41 +0100225 if (addr->sin_family == AF_UNSPEC &&
226 addr->sin_addr.s_addr != htonl(INADDR_ANY))
Ursula Braunac713872017-01-09 16:55:13 +0100227 goto out;
228
229 lock_sock(sk);
230
231 /* Check if socket is already active */
232 rc = -EINVAL;
233 if (sk->sk_state != SMC_INIT)
234 goto out_rel;
235
236 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
237 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
238
239out_rel:
240 release_sock(sk);
241out:
242 return rc;
243}
244
245static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
246 unsigned long mask)
247{
248 /* options we don't get control via setsockopt for */
249 nsk->sk_type = osk->sk_type;
250 nsk->sk_sndbuf = osk->sk_sndbuf;
251 nsk->sk_rcvbuf = osk->sk_rcvbuf;
252 nsk->sk_sndtimeo = osk->sk_sndtimeo;
253 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
254 nsk->sk_mark = osk->sk_mark;
255 nsk->sk_priority = osk->sk_priority;
256 nsk->sk_rcvlowat = osk->sk_rcvlowat;
257 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
258 nsk->sk_err = osk->sk_err;
259
260 nsk->sk_flags &= ~mask;
261 nsk->sk_flags |= osk->sk_flags & mask;
262}
263
264#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
265 (1UL << SOCK_KEEPOPEN) | \
266 (1UL << SOCK_LINGER) | \
267 (1UL << SOCK_BROADCAST) | \
268 (1UL << SOCK_TIMESTAMP) | \
269 (1UL << SOCK_DBG) | \
270 (1UL << SOCK_RCVTSTAMP) | \
271 (1UL << SOCK_RCVTSTAMPNS) | \
272 (1UL << SOCK_LOCALROUTE) | \
273 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
274 (1UL << SOCK_RXQ_OVFL) | \
275 (1UL << SOCK_WIFI_STATUS) | \
276 (1UL << SOCK_NOFCS) | \
277 (1UL << SOCK_FILTER_LOCKED))
278/* copy only relevant settings and flags of SOL_SOCKET level from smc to
279 * clc socket (since smc is not called for these options from net/core)
280 */
281static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
282{
283 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
284}
285
286#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
287 (1UL << SOCK_KEEPOPEN) | \
288 (1UL << SOCK_LINGER) | \
289 (1UL << SOCK_DBG))
290/* copy only settings and flags relevant for smc from clc to smc socket */
291static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
292{
293 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
294}
295
Karsten Graule63a5f82018-05-03 17:57:37 +0200296/* register a new rmb */
297static int smc_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc)
298{
299 /* register memory region for new rmb */
Karsten Graula6920d12018-05-03 17:57:38 +0200300 if (smc_wr_reg_send(link, rmb_desc->mr_rx[SMC_SINGLE_LINK])) {
301 rmb_desc->regerr = 1;
Karsten Graule63a5f82018-05-03 17:57:37 +0200302 return -EFAULT;
Karsten Graula6920d12018-05-03 17:57:38 +0200303 }
Karsten Graule63a5f82018-05-03 17:57:37 +0200304 return 0;
305}
306
Stefan Raspl0f627122018-03-01 13:51:26 +0100307static int smc_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100308{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200309 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100310 struct smc_link_group *lgr = smc->conn.lgr;
311 struct smc_link *link;
312 int rest;
313 int rc;
314
315 link = &lgr->lnk[SMC_SINGLE_LINK];
316 /* receive CONFIRM LINK request from server over RoCE fabric */
317 rest = wait_for_completion_interruptible_timeout(
318 &link->llc_confirm,
319 SMC_LLC_WAIT_FIRST_TIME);
320 if (rest <= 0) {
321 struct smc_clc_msg_decline dclc;
322
323 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
324 SMC_CLC_DECLINE);
325 return rc;
326 }
327
Karsten Graul75d320d2018-03-01 13:51:31 +0100328 if (link->llc_confirm_rc)
329 return SMC_CLC_DECL_RMBE_EC;
330
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100331 rc = smc_ib_modify_qp_rts(link);
332 if (rc)
333 return SMC_CLC_DECL_INTERR;
334
335 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200336
Karsten Graule63a5f82018-05-03 17:57:37 +0200337 if (smc_reg_rmb(link, smc->conn.rmb_desc))
Ursula Braun652a1e42017-07-28 13:56:17 +0200338 return SMC_CLC_DECL_INTERR;
339
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100340 /* send CONFIRM LINK response over RoCE fabric */
341 rc = smc_llc_send_confirm_link(link,
342 link->smcibdev->mac[link->ibport - 1],
Stefan Raspl0f627122018-03-01 13:51:26 +0100343 &link->smcibdev->gid[link->ibport - 1],
344 SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100345 if (rc < 0)
346 return SMC_CLC_DECL_TCL;
347
Karsten Graul52bedf32018-03-01 13:51:32 +0100348 /* receive ADD LINK request from server over RoCE fabric */
349 rest = wait_for_completion_interruptible_timeout(&link->llc_add,
350 SMC_LLC_WAIT_TIME);
351 if (rest <= 0) {
352 struct smc_clc_msg_decline dclc;
353
354 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
355 SMC_CLC_DECLINE);
356 return rc;
357 }
358
359 /* send add link reject message, only one link supported for now */
360 rc = smc_llc_send_add_link(link,
361 link->smcibdev->mac[link->ibport - 1],
362 &link->smcibdev->gid[link->ibport - 1],
363 SMC_LLC_RESP);
364 if (rc < 0)
365 return SMC_CLC_DECL_TCL;
366
Karsten Graul877ae5b2018-05-02 16:56:44 +0200367 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100368
Karsten Graul75d320d2018-03-01 13:51:31 +0100369 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100370}
371
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100372static void smc_conn_save_peer_info(struct smc_sock *smc,
373 struct smc_clc_msg_accept_confirm *clc)
374{
375 smc->conn.peer_conn_idx = clc->conn_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100376 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100377 smc->conn.peer_rmbe_size = smc_uncompress_bufsize(clc->rmbe_size);
378 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100379}
380
381static void smc_link_save_peer_info(struct smc_link *link,
382 struct smc_clc_msg_accept_confirm *clc)
383{
384 link->peer_qpn = ntoh24(clc->qpn);
385 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
386 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
387 link->peer_psn = ntoh24(clc->psn);
388 link->peer_mtu = clc->qp_mtu;
389}
390
Ursula Brauna046d572017-01-09 16:55:16 +0100391/* setup for RDMA connection of client */
392static int smc_connect_rdma(struct smc_sock *smc)
393{
394 struct smc_clc_msg_accept_confirm aclc;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100395 int local_contact = SMC_FIRST_CONTACT;
Ursula Brauna046d572017-01-09 16:55:16 +0100396 struct smc_ib_device *smcibdev;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100397 struct smc_link *link;
398 u8 srv_first_contact;
Ursula Brauna046d572017-01-09 16:55:16 +0100399 int reason_code = 0;
400 int rc = 0;
401 u8 ibport;
402
Ursula Braun51f1de72018-01-26 09:28:48 +0100403 sock_hold(&smc->sk); /* sock put in passive closing */
404
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200405 if (smc->use_fallback)
406 goto out_connected;
407
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200408 if (!tcp_sk(smc->clcsock->sk)->syn_smc) {
409 /* peer has not signalled SMC-capability */
410 smc->use_fallback = true;
411 goto out_connected;
412 }
413
Ursula Brauna046d572017-01-09 16:55:16 +0100414 /* IPSec connections opt out of SMC-R optimizations */
415 if (using_ipsec(smc)) {
416 reason_code = SMC_CLC_DECL_IPSEC;
417 goto decline_rdma;
418 }
419
420 /* PNET table look up: search active ib_device and port
421 * within same PNETID that also contains the ethernet device
422 * used for the internal TCP socket
423 */
424 smc_pnet_find_roce_resource(smc->clcsock->sk, &smcibdev, &ibport);
425 if (!smcibdev) {
426 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
427 goto decline_rdma;
428 }
429
430 /* do inband token exchange */
431 reason_code = smc_clc_send_proposal(smc, smcibdev, ibport);
432 if (reason_code < 0) {
433 rc = reason_code;
434 goto out_err;
435 }
436 if (reason_code > 0) /* configuration error */
437 goto decline_rdma;
438 /* receive SMC Accept CLC message */
439 reason_code = smc_clc_wait_msg(smc, &aclc, sizeof(aclc),
440 SMC_CLC_ACCEPT);
441 if (reason_code < 0) {
442 rc = reason_code;
443 goto out_err;
444 }
445 if (reason_code > 0)
446 goto decline_rdma;
447
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100448 srv_first_contact = aclc.hdr.flag;
449 mutex_lock(&smc_create_lgr_pending);
Karsten Graulbe6d4672018-03-01 13:51:28 +0100450 local_contact = smc_conn_create(smc, smcibdev, ibport, &aclc.lcl,
451 srv_first_contact);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100452 if (local_contact < 0) {
453 rc = local_contact;
454 if (rc == -ENOMEM)
455 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
456 else if (rc == -ENOLINK)
457 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
458 goto decline_rdma_unlock;
459 }
460 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
Ursula Brauna046d572017-01-09 16:55:16 +0100461
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100462 smc_conn_save_peer_info(smc, &aclc);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100463
Ursula Braun3e034722017-07-28 13:56:20 +0200464 /* create send buffer and rmb */
465 rc = smc_buf_create(smc);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100466 if (rc) {
467 reason_code = SMC_CLC_DECL_MEM;
468 goto decline_rdma_unlock;
469 }
470
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100471 if (local_contact == SMC_FIRST_CONTACT)
472 smc_link_save_peer_info(link, &aclc);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100473
474 rc = smc_rmb_rtoken_handling(&smc->conn, &aclc);
475 if (rc) {
476 reason_code = SMC_CLC_DECL_INTERR;
477 goto decline_rdma_unlock;
478 }
479
Ursula Braun46c28db2017-04-10 14:58:01 +0200480 smc_close_init(smc);
481 smc_rx_init(smc);
482
Ursula Braunbd4ad572017-01-09 16:55:20 +0100483 if (local_contact == SMC_FIRST_CONTACT) {
484 rc = smc_ib_ready_link(link);
485 if (rc) {
486 reason_code = SMC_CLC_DECL_INTERR;
487 goto decline_rdma_unlock;
488 }
Ursula Braun652a1e42017-07-28 13:56:17 +0200489 } else {
Karsten Graule63a5f82018-05-03 17:57:37 +0200490 if (!smc->conn.rmb_desc->reused) {
491 if (smc_reg_rmb(link, smc->conn.rmb_desc)) {
Ursula Braun652a1e42017-07-28 13:56:17 +0200492 reason_code = SMC_CLC_DECL_INTERR;
493 goto decline_rdma_unlock;
494 }
495 }
Ursula Braunbd4ad572017-01-09 16:55:20 +0100496 }
Ursula Braun10428dd2017-07-28 13:56:22 +0200497 smc_rmb_sync_sg_for_device(&smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100498
499 rc = smc_clc_send_confirm(smc);
500 if (rc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100501 goto out_err_unlock;
Ursula Brauna046d572017-01-09 16:55:16 +0100502
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100503 if (local_contact == SMC_FIRST_CONTACT) {
504 /* QP confirmation over RoCE fabric */
Stefan Raspl0f627122018-03-01 13:51:26 +0100505 reason_code = smc_clnt_conf_first_link(smc);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100506 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:
Ursula Braun51f1de72018-01-26 09:28:48 +0100545 if (smc->sk.sk_state == SMC_INIT)
546 sock_put(&smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +0100547 return rc;
548}
549
Ursula Braunac713872017-01-09 16:55:13 +0100550static int smc_connect(struct socket *sock, struct sockaddr *addr,
551 int alen, int flags)
552{
553 struct sock *sk = sock->sk;
554 struct smc_sock *smc;
555 int rc = -EINVAL;
556
557 smc = smc_sk(sk);
558
559 /* separate smc parameter checking to be safe */
560 if (alen < sizeof(addr->sa_family))
561 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100562 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100563 goto out_err;
564
565 lock_sock(sk);
566 switch (sk->sk_state) {
567 default:
568 goto out;
569 case SMC_ACTIVE:
570 rc = -EISCONN;
571 goto out;
572 case SMC_INIT:
573 rc = 0;
574 break;
575 }
576
577 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200578 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +0100579 rc = kernel_connect(smc->clcsock, addr, alen, flags);
580 if (rc)
581 goto out;
582
Ursula Brauna046d572017-01-09 16:55:16 +0100583 /* setup RDMA connection */
584 rc = smc_connect_rdma(smc);
585 if (rc < 0)
586 goto out;
587 else
588 rc = 0; /* success cases including fallback */
Ursula Braunac713872017-01-09 16:55:13 +0100589
590out:
591 release_sock(sk);
592out_err:
593 return rc;
594}
595
596static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
597{
Ursula Braun3163c502018-01-24 10:28:12 +0100598 struct socket *new_clcsock = NULL;
599 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100600 struct sock *new_sk;
601 int rc;
602
Ursula Braun3163c502018-01-24 10:28:12 +0100603 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100604 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100605 if (!new_sk) {
606 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100607 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100608 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100609 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100610 goto out;
611 }
612 *new_smc = smc_sk(new_sk);
613
614 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
Ursula Braun3163c502018-01-24 10:28:12 +0100615 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100616 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100617 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100618 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Brauna046d572017-01-09 16:55:16 +0100619 if (new_clcsock)
620 sock_release(new_clcsock);
621 new_sk->sk_state = SMC_CLOSED;
622 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun3163c502018-01-24 10:28:12 +0100623 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100624 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100625 *new_smc = NULL;
626 goto out;
627 }
628
629 (*new_smc)->clcsock = new_clcsock;
630out:
631 return rc;
632}
633
Ursula Brauna046d572017-01-09 16:55:16 +0100634/* add a just created sock to the accept queue of the listen sock as
635 * candidate for a following socket accept call from user space
636 */
637static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
638{
639 struct smc_sock *par = smc_sk(parent);
640
Ursula Braun51f1de72018-01-26 09:28:48 +0100641 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100642 spin_lock(&par->accept_q_lock);
643 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
644 spin_unlock(&par->accept_q_lock);
645 sk_acceptq_added(parent);
646}
647
648/* remove a socket from the accept queue of its parental listening socket */
649static void smc_accept_unlink(struct sock *sk)
650{
651 struct smc_sock *par = smc_sk(sk)->listen_smc;
652
653 spin_lock(&par->accept_q_lock);
654 list_del_init(&smc_sk(sk)->accept_q);
655 spin_unlock(&par->accept_q_lock);
656 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100657 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100658}
659
660/* remove a sock from the accept queue to bind it to a new socket created
661 * for a socket accept call from user space
662 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100663struct sock *smc_accept_dequeue(struct sock *parent,
664 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100665{
666 struct smc_sock *isk, *n;
667 struct sock *new_sk;
668
669 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
670 new_sk = (struct sock *)isk;
671
672 smc_accept_unlink(new_sk);
673 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braun127f4972018-01-26 09:28:49 +0100674 if (isk->clcsock) {
675 sock_release(isk->clcsock);
676 isk->clcsock = NULL;
677 }
Ursula Braun288c83902017-04-10 14:58:04 +0200678 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100679 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +0100680 continue;
681 }
682 if (new_sock)
683 sock_graft(new_sk, new_sock);
684 return new_sk;
685 }
686 return NULL;
687}
688
689/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +0100690void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +0100691{
692 struct smc_sock *smc = smc_sk(sk);
693
Ursula Braunb38d7322017-01-09 16:55:25 +0100694 lock_sock(sk);
695 if (!sk->sk_lingertime)
696 /* wait for peer closing */
697 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100698 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100699 smc_close_active(smc);
Ursula Braun288c83902017-04-10 14:58:04 +0200700 sock_set_flag(sk, SOCK_DEAD);
701 sk->sk_shutdown |= SHUTDOWN_MASK;
702 }
Ursula Brauna046d572017-01-09 16:55:16 +0100703 if (smc->clcsock) {
704 struct socket *tcp;
705
706 tcp = smc->clcsock;
707 smc->clcsock = NULL;
708 sock_release(tcp);
709 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100710 if (smc->use_fallback) {
Ursula Braun51f1de72018-01-26 09:28:48 +0100711 sock_put(sk); /* passive closing */
712 sk->sk_state = SMC_CLOSED;
713 } else {
714 if (sk->sk_state == SMC_CLOSED)
715 smc_conn_free(&smc->conn);
Ursula Braunb38d7322017-01-09 16:55:25 +0100716 }
717 release_sock(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100718 sk->sk_prot->unhash(sk);
719 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +0100720}
721
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100722static int smc_serv_conf_first_link(struct smc_sock *smc)
723{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200724 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100725 struct smc_link_group *lgr = smc->conn.lgr;
726 struct smc_link *link;
727 int rest;
728 int rc;
729
730 link = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun652a1e42017-07-28 13:56:17 +0200731
Karsten Graule63a5f82018-05-03 17:57:37 +0200732 if (smc_reg_rmb(link, smc->conn.rmb_desc))
Ursula Braun652a1e42017-07-28 13:56:17 +0200733 return SMC_CLC_DECL_INTERR;
734
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100735 /* send CONFIRM LINK request to client over the RoCE fabric */
736 rc = smc_llc_send_confirm_link(link,
737 link->smcibdev->mac[link->ibport - 1],
738 &link->smcibdev->gid[link->ibport - 1],
739 SMC_LLC_REQ);
740 if (rc < 0)
741 return SMC_CLC_DECL_TCL;
742
743 /* receive CONFIRM LINK response from client over the RoCE fabric */
744 rest = wait_for_completion_interruptible_timeout(
745 &link->llc_confirm_resp,
746 SMC_LLC_WAIT_FIRST_TIME);
747 if (rest <= 0) {
748 struct smc_clc_msg_decline dclc;
749
750 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
751 SMC_CLC_DECLINE);
Karsten Graul75d320d2018-03-01 13:51:31 +0100752 return rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100753 }
754
Karsten Graul75d320d2018-03-01 13:51:31 +0100755 if (link->llc_confirm_resp_rc)
756 return SMC_CLC_DECL_RMBE_EC;
757
Karsten Graul52bedf32018-03-01 13:51:32 +0100758 /* send ADD LINK request to client over the RoCE fabric */
759 rc = smc_llc_send_add_link(link,
760 link->smcibdev->mac[link->ibport - 1],
761 &link->smcibdev->gid[link->ibport - 1],
762 SMC_LLC_REQ);
763 if (rc < 0)
764 return SMC_CLC_DECL_TCL;
765
766 /* receive ADD LINK response from client over the RoCE fabric */
767 rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
768 SMC_LLC_WAIT_TIME);
769 if (rest <= 0) {
770 struct smc_clc_msg_decline dclc;
771
772 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
773 SMC_CLC_DECLINE);
774 return rc;
775 }
776
Karsten Graul877ae5b2018-05-02 16:56:44 +0200777 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100778
Karsten Graul75d320d2018-03-01 13:51:31 +0100779 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100780}
781
Ursula Brauna046d572017-01-09 16:55:16 +0100782/* setup for RDMA connection of server */
783static void smc_listen_work(struct work_struct *work)
784{
785 struct smc_sock *new_smc = container_of(work, struct smc_sock,
786 smc_listen_work);
Ursula Braune7b7a642017-12-07 13:38:49 +0100787 struct smc_clc_msg_proposal_prefix *pclc_prfx;
Ursula Brauna046d572017-01-09 16:55:16 +0100788 struct socket *newclcsock = new_smc->clcsock;
789 struct smc_sock *lsmc = new_smc->listen_smc;
790 struct smc_clc_msg_accept_confirm cclc;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100791 int local_contact = SMC_REUSE_CONTACT;
Ursula Brauna046d572017-01-09 16:55:16 +0100792 struct sock *newsmcsk = &new_smc->sk;
Ursula Braune7b7a642017-12-07 13:38:49 +0100793 struct smc_clc_msg_proposal *pclc;
Ursula Brauna046d572017-01-09 16:55:16 +0100794 struct smc_ib_device *smcibdev;
Ursula Braune7b7a642017-12-07 13:38:49 +0100795 u8 buf[SMC_CLC_MAX_LEN];
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100796 struct smc_link *link;
Ursula Brauna046d572017-01-09 16:55:16 +0100797 int reason_code = 0;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100798 int rc = 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100799 u8 ibport;
800
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200801 if (new_smc->use_fallback)
802 goto out_connected;
803
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200804 /* check if peer is smc capable */
805 if (!tcp_sk(newclcsock->sk)->syn_smc) {
806 new_smc->use_fallback = true;
807 goto out_connected;
808 }
809
Ursula Brauna046d572017-01-09 16:55:16 +0100810 /* do inband token exchange -
811 *wait for and receive SMC Proposal CLC message
812 */
Ursula Braune7b7a642017-12-07 13:38:49 +0100813 reason_code = smc_clc_wait_msg(new_smc, &buf, sizeof(buf),
Ursula Brauna046d572017-01-09 16:55:16 +0100814 SMC_CLC_PROPOSAL);
815 if (reason_code < 0)
816 goto out_err;
817 if (reason_code > 0)
818 goto decline_rdma;
819
820 /* IPSec connections opt out of SMC-R optimizations */
821 if (using_ipsec(new_smc)) {
822 reason_code = SMC_CLC_DECL_IPSEC;
823 goto decline_rdma;
824 }
825
826 /* PNET table look up: search active ib_device and port
827 * within same PNETID that also contains the ethernet device
828 * used for the internal TCP socket
829 */
830 smc_pnet_find_roce_resource(newclcsock->sk, &smcibdev, &ibport);
831 if (!smcibdev) {
832 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
833 goto decline_rdma;
834 }
835
Ursula Braune7b7a642017-12-07 13:38:49 +0100836 pclc = (struct smc_clc_msg_proposal *)&buf;
837 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
Karsten Graulc246d942018-03-16 15:06:39 +0100838
839 rc = smc_clc_prfx_match(newclcsock, pclc_prfx);
840 if (rc) {
Ursula Brauna046d572017-01-09 16:55:16 +0100841 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
842 goto decline_rdma;
843 }
844
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100845 /* allocate connection / link group */
846 mutex_lock(&smc_create_lgr_pending);
Karsten Graulbe6d4672018-03-01 13:51:28 +0100847 local_contact = smc_conn_create(new_smc, smcibdev, ibport, &pclc->lcl,
848 0);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100849 if (local_contact < 0) {
850 rc = local_contact;
851 if (rc == -ENOMEM)
852 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
Ursula Braun145686b2017-10-25 11:01:44 +0200853 goto decline_rdma_unlock;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100854 }
855 link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
Ursula Brauna046d572017-01-09 16:55:16 +0100856
Ursula Braun3e034722017-07-28 13:56:20 +0200857 /* create send buffer and rmb */
858 rc = smc_buf_create(new_smc);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100859 if (rc) {
860 reason_code = SMC_CLC_DECL_MEM;
Ursula Braun145686b2017-10-25 11:01:44 +0200861 goto decline_rdma_unlock;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100862 }
Ursula Brauna046d572017-01-09 16:55:16 +0100863
Ursula Braun46c28db2017-04-10 14:58:01 +0200864 smc_close_init(new_smc);
865 smc_rx_init(new_smc);
866
Ursula Braun652a1e42017-07-28 13:56:17 +0200867 if (local_contact != SMC_FIRST_CONTACT) {
Karsten Graule63a5f82018-05-03 17:57:37 +0200868 if (!new_smc->conn.rmb_desc->reused) {
869 if (smc_reg_rmb(link, new_smc->conn.rmb_desc)) {
Ursula Braun652a1e42017-07-28 13:56:17 +0200870 reason_code = SMC_CLC_DECL_INTERR;
Ursula Braun145686b2017-10-25 11:01:44 +0200871 goto decline_rdma_unlock;
Ursula Braun652a1e42017-07-28 13:56:17 +0200872 }
873 }
874 }
Ursula Braun10428dd2017-07-28 13:56:22 +0200875 smc_rmb_sync_sg_for_device(&new_smc->conn);
Ursula Braun652a1e42017-07-28 13:56:17 +0200876
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100877 rc = smc_clc_send_accept(new_smc, local_contact);
Ursula Brauna046d572017-01-09 16:55:16 +0100878 if (rc)
Ursula Braun145686b2017-10-25 11:01:44 +0200879 goto out_err_unlock;
Ursula Brauna046d572017-01-09 16:55:16 +0100880
881 /* receive SMC Confirm CLC message */
882 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
883 SMC_CLC_CONFIRM);
884 if (reason_code < 0)
Ursula Braun145686b2017-10-25 11:01:44 +0200885 goto out_err_unlock;
Ursula Brauna046d572017-01-09 16:55:16 +0100886 if (reason_code > 0)
Ursula Braun145686b2017-10-25 11:01:44 +0200887 goto decline_rdma_unlock;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100888 smc_conn_save_peer_info(new_smc, &cclc);
889 if (local_contact == SMC_FIRST_CONTACT)
890 smc_link_save_peer_info(link, &cclc);
Ursula Brauna046d572017-01-09 16:55:16 +0100891
Ursula Braunbd4ad572017-01-09 16:55:20 +0100892 rc = smc_rmb_rtoken_handling(&new_smc->conn, &cclc);
893 if (rc) {
894 reason_code = SMC_CLC_DECL_INTERR;
Ursula Braun145686b2017-10-25 11:01:44 +0200895 goto decline_rdma_unlock;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100896 }
897
Ursula Braunbd4ad572017-01-09 16:55:20 +0100898 if (local_contact == SMC_FIRST_CONTACT) {
899 rc = smc_ib_ready_link(link);
900 if (rc) {
901 reason_code = SMC_CLC_DECL_INTERR;
Ursula Braun145686b2017-10-25 11:01:44 +0200902 goto decline_rdma_unlock;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100903 }
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100904 /* QP confirmation over RoCE fabric */
905 reason_code = smc_serv_conf_first_link(new_smc);
Ursula Braun0c9f1512017-12-07 13:38:45 +0100906 if (reason_code < 0)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100907 /* peer is not aware of a problem */
Ursula Braun145686b2017-10-25 11:01:44 +0200908 goto out_err_unlock;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100909 if (reason_code > 0)
Ursula Braun145686b2017-10-25 11:01:44 +0200910 goto decline_rdma_unlock;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100911 }
Ursula Brauna046d572017-01-09 16:55:16 +0100912
Ursula Braune6727f32017-01-09 16:55:23 +0100913 smc_tx_init(new_smc);
Ursula Braun145686b2017-10-25 11:01:44 +0200914 mutex_unlock(&smc_create_lgr_pending);
Ursula Braune6727f32017-01-09 16:55:23 +0100915
Ursula Brauna046d572017-01-09 16:55:16 +0100916out_connected:
917 sk_refcnt_debug_inc(newsmcsk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100918 if (newsmcsk->sk_state == SMC_INIT)
919 newsmcsk->sk_state = SMC_ACTIVE;
Ursula Brauna046d572017-01-09 16:55:16 +0100920enqueue:
Ursula Braunb38d7322017-01-09 16:55:25 +0100921 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +0100922 if (lsmc->sk.sk_state == SMC_LISTEN) {
923 smc_accept_enqueue(&lsmc->sk, newsmcsk);
924 } else { /* no longer listening */
925 smc_close_non_accepted(newsmcsk);
926 }
927 release_sock(&lsmc->sk);
928
929 /* Wake up accept */
930 lsmc->sk.sk_data_ready(&lsmc->sk);
931 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
932 return;
933
Ursula Braun145686b2017-10-25 11:01:44 +0200934decline_rdma_unlock:
Ursula Braun610db662018-01-25 11:15:34 +0100935 if (local_contact == SMC_FIRST_CONTACT)
936 smc_lgr_forget(new_smc->conn.lgr);
Ursula Braun145686b2017-10-25 11:01:44 +0200937 mutex_unlock(&smc_create_lgr_pending);
Ursula Brauna046d572017-01-09 16:55:16 +0100938decline_rdma:
939 /* RDMA setup failed, switch back to TCP */
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100940 smc_conn_free(&new_smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100941 new_smc->use_fallback = true;
942 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
Ursula Braun0c9f1512017-12-07 13:38:45 +0100943 if (smc_clc_send_decline(new_smc, reason_code) < 0)
Ursula Brauna046d572017-01-09 16:55:16 +0100944 goto out_err;
945 }
946 goto out_connected;
947
Ursula Braun145686b2017-10-25 11:01:44 +0200948out_err_unlock:
Ursula Braun610db662018-01-25 11:15:34 +0100949 if (local_contact == SMC_FIRST_CONTACT)
950 smc_lgr_forget(new_smc->conn.lgr);
Ursula Braun145686b2017-10-25 11:01:44 +0200951 mutex_unlock(&smc_create_lgr_pending);
Ursula Brauna046d572017-01-09 16:55:16 +0100952out_err:
Ursula Braun51f1de72018-01-26 09:28:48 +0100953 if (newsmcsk->sk_state == SMC_INIT)
954 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +0100955 newsmcsk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100956 smc_conn_free(&new_smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100957 goto enqueue; /* queue new sock with sk_err set */
958}
959
960static void smc_tcp_listen_work(struct work_struct *work)
961{
962 struct smc_sock *lsmc = container_of(work, struct smc_sock,
963 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +0100964 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +0100965 struct smc_sock *new_smc;
966 int rc = 0;
967
Ursula Braun3163c502018-01-24 10:28:12 +0100968 lock_sock(lsk);
969 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +0100970 rc = smc_clcsock_accept(lsmc, &new_smc);
971 if (rc)
972 goto out;
973 if (!new_smc)
974 continue;
975
976 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200977 new_smc->use_fallback = lsmc->use_fallback;
Ursula Braun3163c502018-01-24 10:28:12 +0100978 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +0100979 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
980 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braun51f1de72018-01-26 09:28:48 +0100981 sock_hold(&new_smc->sk); /* sock_put in passive closing */
982 if (!schedule_work(&new_smc->smc_listen_work))
983 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100984 }
985
986out:
Ursula Braun3163c502018-01-24 10:28:12 +0100987 release_sock(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100988 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +0100989}
990
Ursula Braunac713872017-01-09 16:55:13 +0100991static int smc_listen(struct socket *sock, int backlog)
992{
993 struct sock *sk = sock->sk;
994 struct smc_sock *smc;
995 int rc;
996
997 smc = smc_sk(sk);
998 lock_sock(sk);
999
1000 rc = -EINVAL;
1001 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
1002 goto out;
1003
1004 rc = 0;
1005 if (sk->sk_state == SMC_LISTEN) {
1006 sk->sk_max_ack_backlog = backlog;
1007 goto out;
1008 }
1009 /* some socket options are handled in core, so we could not apply
1010 * them to the clc socket -- copy smc socket options to clc socket
1011 */
1012 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001013 if (!smc->use_fallback)
1014 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001015
1016 rc = kernel_listen(smc->clcsock, backlog);
1017 if (rc)
1018 goto out;
1019 sk->sk_max_ack_backlog = backlog;
1020 sk->sk_ack_backlog = 0;
1021 sk->sk_state = SMC_LISTEN;
Ursula Brauna046d572017-01-09 16:55:16 +01001022 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun51f1de72018-01-26 09:28:48 +01001023 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1024 if (!schedule_work(&smc->tcp_listen_work))
1025 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001026
1027out:
1028 release_sock(sk);
1029 return rc;
1030}
1031
1032static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001033 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001034{
Ursula Brauna046d572017-01-09 16:55:16 +01001035 struct sock *sk = sock->sk, *nsk;
1036 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001037 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001038 long timeo;
1039 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001040
1041 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001042 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001043 lock_sock(sk);
1044
1045 if (lsmc->sk.sk_state != SMC_LISTEN) {
1046 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001047 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001048 goto out;
1049 }
1050
Ursula Brauna046d572017-01-09 16:55:16 +01001051 /* Wait for an incoming connection */
1052 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1053 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1054 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1055 set_current_state(TASK_INTERRUPTIBLE);
1056 if (!timeo) {
1057 rc = -EAGAIN;
1058 break;
1059 }
1060 release_sock(sk);
1061 timeo = schedule_timeout(timeo);
1062 /* wakeup by sk_data_ready in smc_listen_work() */
1063 sched_annotate_sleep();
1064 lock_sock(sk);
1065 if (signal_pending(current)) {
1066 rc = sock_intr_errno(timeo);
1067 break;
1068 }
1069 }
1070 set_current_state(TASK_RUNNING);
1071 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001072
Ursula Brauna046d572017-01-09 16:55:16 +01001073 if (!rc)
1074 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001075 release_sock(sk);
1076 if (rc)
1077 goto out;
1078
1079 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1080 /* wait till data arrives on the socket */
1081 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1082 MSEC_PER_SEC);
1083 if (smc_sk(nsk)->use_fallback) {
1084 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1085
1086 lock_sock(clcsk);
1087 if (skb_queue_empty(&clcsk->sk_receive_queue))
1088 sk_wait_data(clcsk, &timeo, NULL);
1089 release_sock(clcsk);
1090 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1091 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001092 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001093 release_sock(nsk);
1094 }
1095 }
Ursula Braunac713872017-01-09 16:55:13 +01001096
1097out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001098 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001099 return rc;
1100}
1101
1102static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001103 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001104{
1105 struct smc_sock *smc;
1106
Ursula Braunb38d7322017-01-09 16:55:25 +01001107 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1108 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001109 return -ENOTCONN;
1110
1111 smc = smc_sk(sock->sk);
1112
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001113 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001114}
1115
1116static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1117{
1118 struct sock *sk = sock->sk;
1119 struct smc_sock *smc;
1120 int rc = -EPIPE;
1121
1122 smc = smc_sk(sk);
1123 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001124 if ((sk->sk_state != SMC_ACTIVE) &&
1125 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1126 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001127 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001128
1129 if (msg->msg_flags & MSG_FASTOPEN) {
1130 if (sk->sk_state == SMC_INIT) {
1131 smc->use_fallback = true;
1132 } else {
1133 rc = -EINVAL;
1134 goto out;
1135 }
1136 }
1137
Ursula Braunac713872017-01-09 16:55:13 +01001138 if (smc->use_fallback)
1139 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1140 else
Ursula Braune6727f32017-01-09 16:55:23 +01001141 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001142out:
1143 release_sock(sk);
1144 return rc;
1145}
1146
1147static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1148 int flags)
1149{
1150 struct sock *sk = sock->sk;
1151 struct smc_sock *smc;
1152 int rc = -ENOTCONN;
1153
1154 smc = smc_sk(sk);
1155 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001156 if ((sk->sk_state == SMC_INIT) ||
1157 (sk->sk_state == SMC_LISTEN) ||
1158 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001159 goto out;
1160
Ursula Braunb38d7322017-01-09 16:55:25 +01001161 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1162 rc = 0;
1163 goto out;
1164 }
1165
Stefan Raspl9014db22018-05-03 18:12:39 +02001166 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001167 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001168 } else {
1169 msg->msg_namelen = 0;
1170 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1171 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001172
Ursula Braunac713872017-01-09 16:55:13 +01001173out:
1174 release_sock(sk);
1175 return rc;
1176}
1177
Al Viroade994f2017-07-03 00:01:49 -04001178static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001179{
Ursula Braun8dce2782018-01-26 09:28:47 +01001180 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001181 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001182
Ursula Braun8dce2782018-01-26 09:28:47 +01001183 spin_lock(&isk->accept_q_lock);
1184 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001185 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001186 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001187
Ursula Braun8dce2782018-01-26 09:28:47 +01001188 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001189}
1190
Al Viroade994f2017-07-03 00:01:49 -04001191static __poll_t smc_poll(struct file *file, struct socket *sock,
Ursula Braunac713872017-01-09 16:55:13 +01001192 poll_table *wait)
1193{
1194 struct sock *sk = sock->sk;
Al Viroe6c8adc2017-07-03 22:25:56 -04001195 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001196 struct smc_sock *smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001197 int rc;
Ursula Braunac713872017-01-09 16:55:13 +01001198
Ursula Braun8dce2782018-01-26 09:28:47 +01001199 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001200 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001201
Ursula Braunac713872017-01-09 16:55:13 +01001202 smc = smc_sk(sock->sk);
Ursula Braun8dce2782018-01-26 09:28:47 +01001203 sock_hold(sk);
1204 lock_sock(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001205 if ((sk->sk_state == SMC_INIT) || smc->use_fallback) {
1206 /* delegate to CLC child sock */
Ursula Braun8dce2782018-01-26 09:28:47 +01001207 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001208 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braunac713872017-01-09 16:55:13 +01001209 lock_sock(sk);
Ursula Braun784813a2018-05-02 16:53:56 +02001210 sk->sk_err = smc->clcsock->sk->sk_err;
1211 if (sk->sk_err) {
1212 mask |= EPOLLERR;
1213 } else {
1214 /* if non-blocking connect finished ... */
1215 if (sk->sk_state == SMC_INIT &&
1216 mask & EPOLLOUT &&
1217 smc->clcsock->sk->sk_state != TCP_CLOSE) {
Ursula Brauna046d572017-01-09 16:55:16 +01001218 rc = smc_connect_rdma(smc);
1219 if (rc < 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001220 mask |= EPOLLERR;
Ursula Braun8dce2782018-01-26 09:28:47 +01001221 /* success cases including fallback */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001222 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Brauna046d572017-01-09 16:55:16 +01001223 }
Ursula Braunac713872017-01-09 16:55:13 +01001224 }
Ursula Braunac713872017-01-09 16:55:13 +01001225 } else {
Ursula Braun8dce2782018-01-26 09:28:47 +01001226 if (sk->sk_state != SMC_CLOSED) {
1227 release_sock(sk);
1228 sock_poll_wait(file, sk_sleep(sk), wait);
1229 lock_sock(sk);
1230 }
Ursula Brauna046d572017-01-09 16:55:16 +01001231 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001232 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001233 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1234 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001235 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001236 if (sk->sk_state == SMC_LISTEN) {
1237 /* woken up by sk_data_ready in smc_listen_work() */
1238 mask = smc_accept_poll(sk);
1239 } else {
1240 if (atomic_read(&smc->conn.sndbuf_space) ||
1241 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001242 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001243 } else {
1244 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1245 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1246 }
1247 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001248 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001249 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001250 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001251 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001252 mask |= EPOLLIN;
Ursula Braun8dce2782018-01-26 09:28:47 +01001253 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001254
Ursula Braunac713872017-01-09 16:55:13 +01001255 }
Ursula Braun8dce2782018-01-26 09:28:47 +01001256 release_sock(sk);
1257 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001258
1259 return mask;
1260}
1261
1262static int smc_shutdown(struct socket *sock, int how)
1263{
1264 struct sock *sk = sock->sk;
1265 struct smc_sock *smc;
1266 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001267 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001268
1269 smc = smc_sk(sk);
1270
1271 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001272 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001273
1274 lock_sock(sk);
1275
1276 rc = -ENOTCONN;
Ursula Braunb38d7322017-01-09 16:55:25 +01001277 if ((sk->sk_state != SMC_LISTEN) &&
1278 (sk->sk_state != SMC_ACTIVE) &&
1279 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1280 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1281 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1282 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1283 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001284 goto out;
1285 if (smc->use_fallback) {
1286 rc = kernel_sock_shutdown(smc->clcsock, how);
1287 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1288 if (sk->sk_shutdown == SHUTDOWN_MASK)
1289 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001290 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001291 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001292 switch (how) {
1293 case SHUT_RDWR: /* shutdown in both directions */
1294 rc = smc_close_active(smc);
1295 break;
1296 case SHUT_WR:
1297 rc = smc_close_shutdown_write(smc);
1298 break;
1299 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001300 rc = 0;
1301 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001302 break;
1303 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001304 if (smc->clcsock)
1305 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001306 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1307 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001308
1309out:
1310 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001311 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001312}
1313
1314static int smc_setsockopt(struct socket *sock, int level, int optname,
1315 char __user *optval, unsigned int optlen)
1316{
1317 struct sock *sk = sock->sk;
1318 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001319 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001320
1321 smc = smc_sk(sk);
1322
1323 /* generic setsockopts reaching us here always apply to the
1324 * CLC socket
1325 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001326 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1327 optval, optlen);
1328 if (smc->clcsock->sk->sk_err) {
1329 sk->sk_err = smc->clcsock->sk->sk_err;
1330 sk->sk_error_report(sk);
1331 }
1332 if (rc)
1333 return rc;
1334
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001335 if (optlen < sizeof(int))
1336 return rc;
1337 get_user(val, (int __user *)optval);
1338
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001339 lock_sock(sk);
1340 switch (optname) {
1341 case TCP_ULP:
1342 case TCP_FASTOPEN:
1343 case TCP_FASTOPEN_CONNECT:
1344 case TCP_FASTOPEN_KEY:
1345 case TCP_FASTOPEN_NO_COOKIE:
1346 /* option not supported by SMC */
1347 if (sk->sk_state == SMC_INIT) {
1348 smc->use_fallback = true;
1349 } else {
1350 if (!smc->use_fallback)
1351 rc = -EINVAL;
1352 }
1353 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001354 case TCP_NODELAY:
1355 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001356 if (val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001357 mod_delayed_work(system_wq, &smc->conn.tx_work,
1358 0);
1359 }
1360 break;
1361 case TCP_CORK:
1362 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001363 if (!val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001364 mod_delayed_work(system_wq, &smc->conn.tx_work,
1365 0);
1366 }
1367 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001368 case TCP_DEFER_ACCEPT:
1369 smc->sockopt_defer_accept = val;
1370 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001371 default:
1372 break;
1373 }
1374 release_sock(sk);
1375
1376 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001377}
1378
1379static int smc_getsockopt(struct socket *sock, int level, int optname,
1380 char __user *optval, int __user *optlen)
1381{
1382 struct smc_sock *smc;
1383
1384 smc = smc_sk(sock->sk);
1385 /* socket options apply to the CLC socket */
1386 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1387 optval, optlen);
1388}
1389
1390static int smc_ioctl(struct socket *sock, unsigned int cmd,
1391 unsigned long arg)
1392{
1393 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001394 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001395
1396 smc = smc_sk(sock->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001397 if (smc->use_fallback) {
1398 if (!smc->clcsock)
1399 return -EBADF;
Ursula Braunac713872017-01-09 16:55:13 +01001400 return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
Ursula Braun9b67e262018-05-02 16:56:46 +02001401 }
1402 switch (cmd) {
1403 case SIOCINQ: /* same as FIONREAD */
1404 if (smc->sk.sk_state == SMC_LISTEN)
1405 return -EINVAL;
1406 answ = atomic_read(&smc->conn.bytes_to_rcv);
1407 break;
1408 case SIOCOUTQ:
1409 /* output queue size (not send + not acked) */
1410 if (smc->sk.sk_state == SMC_LISTEN)
1411 return -EINVAL;
1412 answ = smc->conn.sndbuf_size -
1413 atomic_read(&smc->conn.sndbuf_space);
1414 break;
1415 case SIOCOUTQNSD:
1416 /* output queue size (not send only) */
1417 if (smc->sk.sk_state == SMC_LISTEN)
1418 return -EINVAL;
1419 answ = smc_tx_prepared_sends(&smc->conn);
1420 break;
1421 default:
1422 return -ENOIOCTLCMD;
1423 }
1424
1425 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001426}
1427
1428static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1429 int offset, size_t size, int flags)
1430{
1431 struct sock *sk = sock->sk;
1432 struct smc_sock *smc;
1433 int rc = -EPIPE;
1434
1435 smc = smc_sk(sk);
1436 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001437 if (sk->sk_state != SMC_ACTIVE) {
1438 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001439 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001440 }
1441 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001442 if (smc->use_fallback)
1443 rc = kernel_sendpage(smc->clcsock, page, offset,
1444 size, flags);
1445 else
1446 rc = sock_no_sendpage(sock, page, offset, size, flags);
1447
1448out:
Ursula Braunac713872017-01-09 16:55:13 +01001449 return rc;
1450}
1451
Stefan Raspl9014db22018-05-03 18:12:39 +02001452/* Map the affected portions of the rmbe into an spd, note the number of bytes
1453 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1454 * updates till whenever a respective page has been fully processed.
1455 * Note that subsequent recv() calls have to wait till all splice() processing
1456 * completed.
1457 */
Ursula Braunac713872017-01-09 16:55:13 +01001458static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1459 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001460 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001461{
1462 struct sock *sk = sock->sk;
1463 struct smc_sock *smc;
1464 int rc = -ENOTCONN;
1465
1466 smc = smc_sk(sk);
1467 lock_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001468
1469 if (sk->sk_state == SMC_INIT ||
1470 sk->sk_state == SMC_LISTEN ||
1471 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001472 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001473
1474 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1475 rc = 0;
1476 goto out;
1477 }
1478
Ursula Braunac713872017-01-09 16:55:13 +01001479 if (smc->use_fallback) {
1480 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1481 pipe, len, flags);
1482 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001483 if (*ppos) {
1484 rc = -ESPIPE;
1485 goto out;
1486 }
1487 if (flags & SPLICE_F_NONBLOCK)
1488 flags = MSG_DONTWAIT;
1489 else
1490 flags = 0;
1491 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001492 }
1493out:
1494 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001495
Ursula Braunac713872017-01-09 16:55:13 +01001496 return rc;
1497}
1498
1499/* must look like tcp */
1500static const struct proto_ops smc_sock_ops = {
1501 .family = PF_SMC,
1502 .owner = THIS_MODULE,
1503 .release = smc_release,
1504 .bind = smc_bind,
1505 .connect = smc_connect,
1506 .socketpair = sock_no_socketpair,
1507 .accept = smc_accept,
1508 .getname = smc_getname,
1509 .poll = smc_poll,
1510 .ioctl = smc_ioctl,
1511 .listen = smc_listen,
1512 .shutdown = smc_shutdown,
1513 .setsockopt = smc_setsockopt,
1514 .getsockopt = smc_getsockopt,
1515 .sendmsg = smc_sendmsg,
1516 .recvmsg = smc_recvmsg,
1517 .mmap = sock_no_mmap,
1518 .sendpage = smc_sendpage,
1519 .splice_read = smc_splice_read,
1520};
1521
1522static int smc_create(struct net *net, struct socket *sock, int protocol,
1523 int kern)
1524{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001525 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001526 struct smc_sock *smc;
1527 struct sock *sk;
1528 int rc;
1529
1530 rc = -ESOCKTNOSUPPORT;
1531 if (sock->type != SOCK_STREAM)
1532 goto out;
1533
1534 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001535 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01001536 goto out;
1537
1538 rc = -ENOBUFS;
1539 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001540 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001541 if (!sk)
1542 goto out;
1543
1544 /* create internal TCP socket for CLC handshake and fallback */
1545 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001546 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graulaaa4d332018-03-16 15:06:41 +01001547 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1548 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01001549 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01001550 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01001551 goto out;
1552 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01001553 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1554 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01001555
1556out:
1557 return rc;
1558}
1559
1560static const struct net_proto_family smc_sock_family_ops = {
1561 .family = PF_SMC,
1562 .owner = THIS_MODULE,
1563 .create = smc_create,
1564};
1565
1566static int __init smc_init(void)
1567{
1568 int rc;
1569
Thomas Richter6812baa2017-01-09 16:55:15 +01001570 rc = smc_pnet_init();
1571 if (rc)
1572 return rc;
1573
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001574 rc = smc_llc_init();
1575 if (rc) {
1576 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1577 goto out_pnet;
1578 }
1579
Ursula Braun5f083182017-01-09 16:55:22 +01001580 rc = smc_cdc_init();
1581 if (rc) {
1582 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1583 goto out_pnet;
1584 }
1585
Ursula Braunac713872017-01-09 16:55:13 +01001586 rc = proto_register(&smc_proto, 1);
1587 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01001588 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Thomas Richter6812baa2017-01-09 16:55:15 +01001589 goto out_pnet;
Ursula Braunac713872017-01-09 16:55:13 +01001590 }
1591
Karsten Graulaaa4d332018-03-16 15:06:41 +01001592 rc = proto_register(&smc_proto6, 1);
1593 if (rc) {
1594 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
1595 goto out_proto;
1596 }
1597
Ursula Braunac713872017-01-09 16:55:13 +01001598 rc = sock_register(&smc_sock_family_ops);
1599 if (rc) {
1600 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001601 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01001602 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01001603 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001604 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01001605
Ursula Brauna4cf0442017-01-09 16:55:14 +01001606 rc = smc_ib_register_client();
1607 if (rc) {
1608 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1609 goto out_sock;
1610 }
1611
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001612 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01001613 return 0;
1614
Ursula Brauna4cf0442017-01-09 16:55:14 +01001615out_sock:
1616 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001617out_proto6:
1618 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01001619out_proto:
1620 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001621out_pnet:
1622 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001623 return rc;
1624}
1625
1626static void __exit smc_exit(void)
1627{
Ursula Braun0cfdd8f2017-01-09 16:55:17 +01001628 struct smc_link_group *lgr, *lg;
1629 LIST_HEAD(lgr_freeing_list);
1630
1631 spin_lock_bh(&smc_lgr_list.lock);
1632 if (!list_empty(&smc_lgr_list.list))
1633 list_splice_init(&smc_lgr_list.list, &lgr_freeing_list);
1634 spin_unlock_bh(&smc_lgr_list.lock);
1635 list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) {
1636 list_del_init(&lgr->list);
Ursula Braun268ffcc2018-03-14 11:01:01 +01001637 cancel_delayed_work_sync(&lgr->free_work);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +01001638 smc_lgr_free(lgr); /* free link group */
1639 }
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001640 static_branch_disable(&tcp_have_smc);
Ursula Brauna4cf0442017-01-09 16:55:14 +01001641 smc_ib_unregister_client();
Ursula Braunac713872017-01-09 16:55:13 +01001642 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001643 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01001644 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001645 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001646}
1647
1648module_init(smc_init);
1649module_exit(smc_exit);
1650
1651MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
1652MODULE_DESCRIPTION("smc socket address family");
1653MODULE_LICENSE("GPL");
1654MODULE_ALIAS_NETPROTO(PF_SMC);