blob: a7495c84ae15b3f4656b0c0c4b993da898bb16bd [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
Ursula Braunac713872017-01-09 16:55:13 +01001166 if (smc->use_fallback)
1167 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
1168 else
Ursula Braun952310c2017-01-09 16:55:24 +01001169 rc = smc_rx_recvmsg(smc, msg, len, flags);
Ursula Braunb38d7322017-01-09 16:55:25 +01001170
Ursula Braunac713872017-01-09 16:55:13 +01001171out:
1172 release_sock(sk);
1173 return rc;
1174}
1175
Al Viroade994f2017-07-03 00:01:49 -04001176static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001177{
Ursula Braun8dce2782018-01-26 09:28:47 +01001178 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001179 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001180
Ursula Braun8dce2782018-01-26 09:28:47 +01001181 spin_lock(&isk->accept_q_lock);
1182 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001183 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001184 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001185
Ursula Braun8dce2782018-01-26 09:28:47 +01001186 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001187}
1188
Al Viroade994f2017-07-03 00:01:49 -04001189static __poll_t smc_poll(struct file *file, struct socket *sock,
Ursula Braunac713872017-01-09 16:55:13 +01001190 poll_table *wait)
1191{
1192 struct sock *sk = sock->sk;
Al Viroe6c8adc2017-07-03 22:25:56 -04001193 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001194 struct smc_sock *smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001195 int rc;
Ursula Braunac713872017-01-09 16:55:13 +01001196
Ursula Braun8dce2782018-01-26 09:28:47 +01001197 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001198 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001199
Ursula Braunac713872017-01-09 16:55:13 +01001200 smc = smc_sk(sock->sk);
Ursula Braun8dce2782018-01-26 09:28:47 +01001201 sock_hold(sk);
1202 lock_sock(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001203 if ((sk->sk_state == SMC_INIT) || smc->use_fallback) {
1204 /* delegate to CLC child sock */
Ursula Braun8dce2782018-01-26 09:28:47 +01001205 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001206 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braunac713872017-01-09 16:55:13 +01001207 lock_sock(sk);
Ursula Braun784813a2018-05-02 16:53:56 +02001208 sk->sk_err = smc->clcsock->sk->sk_err;
1209 if (sk->sk_err) {
1210 mask |= EPOLLERR;
1211 } else {
1212 /* if non-blocking connect finished ... */
1213 if (sk->sk_state == SMC_INIT &&
1214 mask & EPOLLOUT &&
1215 smc->clcsock->sk->sk_state != TCP_CLOSE) {
Ursula Brauna046d572017-01-09 16:55:16 +01001216 rc = smc_connect_rdma(smc);
1217 if (rc < 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001218 mask |= EPOLLERR;
Ursula Braun8dce2782018-01-26 09:28:47 +01001219 /* success cases including fallback */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001220 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Brauna046d572017-01-09 16:55:16 +01001221 }
Ursula Braunac713872017-01-09 16:55:13 +01001222 }
Ursula Braunac713872017-01-09 16:55:13 +01001223 } else {
Ursula Braun8dce2782018-01-26 09:28:47 +01001224 if (sk->sk_state != SMC_CLOSED) {
1225 release_sock(sk);
1226 sock_poll_wait(file, sk_sleep(sk), wait);
1227 lock_sock(sk);
1228 }
Ursula Brauna046d572017-01-09 16:55:16 +01001229 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001230 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001231 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1232 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001233 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001234 if (sk->sk_state == SMC_LISTEN) {
1235 /* woken up by sk_data_ready in smc_listen_work() */
1236 mask = smc_accept_poll(sk);
1237 } else {
1238 if (atomic_read(&smc->conn.sndbuf_space) ||
1239 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001240 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001241 } else {
1242 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1243 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1244 }
1245 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001246 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001247 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001248 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001249 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001250 mask |= EPOLLIN;
Ursula Braun8dce2782018-01-26 09:28:47 +01001251 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001252
Ursula Braunac713872017-01-09 16:55:13 +01001253 }
Ursula Braun8dce2782018-01-26 09:28:47 +01001254 release_sock(sk);
1255 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001256
1257 return mask;
1258}
1259
1260static int smc_shutdown(struct socket *sock, int how)
1261{
1262 struct sock *sk = sock->sk;
1263 struct smc_sock *smc;
1264 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001265 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001266
1267 smc = smc_sk(sk);
1268
1269 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001270 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001271
1272 lock_sock(sk);
1273
1274 rc = -ENOTCONN;
Ursula Braunb38d7322017-01-09 16:55:25 +01001275 if ((sk->sk_state != SMC_LISTEN) &&
1276 (sk->sk_state != SMC_ACTIVE) &&
1277 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1278 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1279 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1280 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1281 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001282 goto out;
1283 if (smc->use_fallback) {
1284 rc = kernel_sock_shutdown(smc->clcsock, how);
1285 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1286 if (sk->sk_shutdown == SHUTDOWN_MASK)
1287 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001288 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001289 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001290 switch (how) {
1291 case SHUT_RDWR: /* shutdown in both directions */
1292 rc = smc_close_active(smc);
1293 break;
1294 case SHUT_WR:
1295 rc = smc_close_shutdown_write(smc);
1296 break;
1297 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001298 rc = 0;
1299 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001300 break;
1301 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001302 if (smc->clcsock)
1303 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001304 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1305 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001306
1307out:
1308 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001309 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001310}
1311
1312static int smc_setsockopt(struct socket *sock, int level, int optname,
1313 char __user *optval, unsigned int optlen)
1314{
1315 struct sock *sk = sock->sk;
1316 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001317 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001318
1319 smc = smc_sk(sk);
1320
1321 /* generic setsockopts reaching us here always apply to the
1322 * CLC socket
1323 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001324 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1325 optval, optlen);
1326 if (smc->clcsock->sk->sk_err) {
1327 sk->sk_err = smc->clcsock->sk->sk_err;
1328 sk->sk_error_report(sk);
1329 }
1330 if (rc)
1331 return rc;
1332
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001333 if (optlen < sizeof(int))
1334 return rc;
1335 get_user(val, (int __user *)optval);
1336
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001337 lock_sock(sk);
1338 switch (optname) {
1339 case TCP_ULP:
1340 case TCP_FASTOPEN:
1341 case TCP_FASTOPEN_CONNECT:
1342 case TCP_FASTOPEN_KEY:
1343 case TCP_FASTOPEN_NO_COOKIE:
1344 /* option not supported by SMC */
1345 if (sk->sk_state == SMC_INIT) {
1346 smc->use_fallback = true;
1347 } else {
1348 if (!smc->use_fallback)
1349 rc = -EINVAL;
1350 }
1351 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001352 case TCP_NODELAY:
1353 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
1354 if (val)
1355 mod_delayed_work(system_wq, &smc->conn.tx_work,
1356 0);
1357 }
1358 break;
1359 case TCP_CORK:
1360 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
1361 if (!val)
1362 mod_delayed_work(system_wq, &smc->conn.tx_work,
1363 0);
1364 }
1365 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001366 case TCP_DEFER_ACCEPT:
1367 smc->sockopt_defer_accept = val;
1368 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001369 default:
1370 break;
1371 }
1372 release_sock(sk);
1373
1374 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001375}
1376
1377static int smc_getsockopt(struct socket *sock, int level, int optname,
1378 char __user *optval, int __user *optlen)
1379{
1380 struct smc_sock *smc;
1381
1382 smc = smc_sk(sock->sk);
1383 /* socket options apply to the CLC socket */
1384 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1385 optval, optlen);
1386}
1387
1388static int smc_ioctl(struct socket *sock, unsigned int cmd,
1389 unsigned long arg)
1390{
1391 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001392 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001393
1394 smc = smc_sk(sock->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001395 if (smc->use_fallback) {
1396 if (!smc->clcsock)
1397 return -EBADF;
Ursula Braunac713872017-01-09 16:55:13 +01001398 return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
Ursula Braun9b67e262018-05-02 16:56:46 +02001399 }
1400 switch (cmd) {
1401 case SIOCINQ: /* same as FIONREAD */
1402 if (smc->sk.sk_state == SMC_LISTEN)
1403 return -EINVAL;
1404 answ = atomic_read(&smc->conn.bytes_to_rcv);
1405 break;
1406 case SIOCOUTQ:
1407 /* output queue size (not send + not acked) */
1408 if (smc->sk.sk_state == SMC_LISTEN)
1409 return -EINVAL;
1410 answ = smc->conn.sndbuf_size -
1411 atomic_read(&smc->conn.sndbuf_space);
1412 break;
1413 case SIOCOUTQNSD:
1414 /* output queue size (not send only) */
1415 if (smc->sk.sk_state == SMC_LISTEN)
1416 return -EINVAL;
1417 answ = smc_tx_prepared_sends(&smc->conn);
1418 break;
1419 default:
1420 return -ENOIOCTLCMD;
1421 }
1422
1423 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001424}
1425
1426static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1427 int offset, size_t size, int flags)
1428{
1429 struct sock *sk = sock->sk;
1430 struct smc_sock *smc;
1431 int rc = -EPIPE;
1432
1433 smc = smc_sk(sk);
1434 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001435 if (sk->sk_state != SMC_ACTIVE) {
1436 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001437 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001438 }
1439 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001440 if (smc->use_fallback)
1441 rc = kernel_sendpage(smc->clcsock, page, offset,
1442 size, flags);
1443 else
1444 rc = sock_no_sendpage(sock, page, offset, size, flags);
1445
1446out:
Ursula Braunac713872017-01-09 16:55:13 +01001447 return rc;
1448}
1449
1450static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1451 struct pipe_inode_info *pipe, size_t len,
1452 unsigned int flags)
1453{
1454 struct sock *sk = sock->sk;
1455 struct smc_sock *smc;
1456 int rc = -ENOTCONN;
1457
1458 smc = smc_sk(sk);
1459 lock_sock(sk);
1460 if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED))
1461 goto out;
1462 if (smc->use_fallback) {
1463 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1464 pipe, len, flags);
1465 } else {
1466 rc = -EOPNOTSUPP;
1467 }
1468out:
1469 release_sock(sk);
1470 return rc;
1471}
1472
1473/* must look like tcp */
1474static const struct proto_ops smc_sock_ops = {
1475 .family = PF_SMC,
1476 .owner = THIS_MODULE,
1477 .release = smc_release,
1478 .bind = smc_bind,
1479 .connect = smc_connect,
1480 .socketpair = sock_no_socketpair,
1481 .accept = smc_accept,
1482 .getname = smc_getname,
1483 .poll = smc_poll,
1484 .ioctl = smc_ioctl,
1485 .listen = smc_listen,
1486 .shutdown = smc_shutdown,
1487 .setsockopt = smc_setsockopt,
1488 .getsockopt = smc_getsockopt,
1489 .sendmsg = smc_sendmsg,
1490 .recvmsg = smc_recvmsg,
1491 .mmap = sock_no_mmap,
1492 .sendpage = smc_sendpage,
1493 .splice_read = smc_splice_read,
1494};
1495
1496static int smc_create(struct net *net, struct socket *sock, int protocol,
1497 int kern)
1498{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001499 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001500 struct smc_sock *smc;
1501 struct sock *sk;
1502 int rc;
1503
1504 rc = -ESOCKTNOSUPPORT;
1505 if (sock->type != SOCK_STREAM)
1506 goto out;
1507
1508 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001509 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01001510 goto out;
1511
1512 rc = -ENOBUFS;
1513 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001514 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001515 if (!sk)
1516 goto out;
1517
1518 /* create internal TCP socket for CLC handshake and fallback */
1519 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001520 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graulaaa4d332018-03-16 15:06:41 +01001521 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1522 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01001523 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01001524 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01001525 goto out;
1526 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01001527 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1528 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01001529
1530out:
1531 return rc;
1532}
1533
1534static const struct net_proto_family smc_sock_family_ops = {
1535 .family = PF_SMC,
1536 .owner = THIS_MODULE,
1537 .create = smc_create,
1538};
1539
1540static int __init smc_init(void)
1541{
1542 int rc;
1543
Thomas Richter6812baa2017-01-09 16:55:15 +01001544 rc = smc_pnet_init();
1545 if (rc)
1546 return rc;
1547
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001548 rc = smc_llc_init();
1549 if (rc) {
1550 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1551 goto out_pnet;
1552 }
1553
Ursula Braun5f083182017-01-09 16:55:22 +01001554 rc = smc_cdc_init();
1555 if (rc) {
1556 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1557 goto out_pnet;
1558 }
1559
Ursula Braunac713872017-01-09 16:55:13 +01001560 rc = proto_register(&smc_proto, 1);
1561 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01001562 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Thomas Richter6812baa2017-01-09 16:55:15 +01001563 goto out_pnet;
Ursula Braunac713872017-01-09 16:55:13 +01001564 }
1565
Karsten Graulaaa4d332018-03-16 15:06:41 +01001566 rc = proto_register(&smc_proto6, 1);
1567 if (rc) {
1568 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
1569 goto out_proto;
1570 }
1571
Ursula Braunac713872017-01-09 16:55:13 +01001572 rc = sock_register(&smc_sock_family_ops);
1573 if (rc) {
1574 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001575 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01001576 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01001577 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001578 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01001579
Ursula Brauna4cf0442017-01-09 16:55:14 +01001580 rc = smc_ib_register_client();
1581 if (rc) {
1582 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1583 goto out_sock;
1584 }
1585
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001586 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01001587 return 0;
1588
Ursula Brauna4cf0442017-01-09 16:55:14 +01001589out_sock:
1590 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001591out_proto6:
1592 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01001593out_proto:
1594 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001595out_pnet:
1596 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001597 return rc;
1598}
1599
1600static void __exit smc_exit(void)
1601{
Ursula Braun0cfdd8f2017-01-09 16:55:17 +01001602 struct smc_link_group *lgr, *lg;
1603 LIST_HEAD(lgr_freeing_list);
1604
1605 spin_lock_bh(&smc_lgr_list.lock);
1606 if (!list_empty(&smc_lgr_list.list))
1607 list_splice_init(&smc_lgr_list.list, &lgr_freeing_list);
1608 spin_unlock_bh(&smc_lgr_list.lock);
1609 list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) {
1610 list_del_init(&lgr->list);
Ursula Braun268ffcc2018-03-14 11:01:01 +01001611 cancel_delayed_work_sync(&lgr->free_work);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +01001612 smc_lgr_free(lgr); /* free link group */
1613 }
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001614 static_branch_disable(&tcp_have_smc);
Ursula Brauna4cf0442017-01-09 16:55:14 +01001615 smc_ib_unregister_client();
Ursula Braunac713872017-01-09 16:55:13 +01001616 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001617 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01001618 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001619 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001620}
1621
1622module_init(smc_init);
1623module_exit(smc_exit);
1624
1625MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
1626MODULE_DESCRIPTION("smc socket address family");
1627MODULE_LICENSE("GPL");
1628MODULE_ALIAS_NETPROTO(PF_SMC);