blob: 20aa4175b9f8edc2c77565addfd2ae2e98e12259 [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 Braunac713872017-01-09 16:55:13 +010032
33#include "smc.h"
Ursula Brauna046d572017-01-09 16:55:16 +010034#include "smc_clc.h"
Ursula Braun9bf9abe2017-01-09 16:55:21 +010035#include "smc_llc.h"
Ursula Braun5f083182017-01-09 16:55:22 +010036#include "smc_cdc.h"
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010037#include "smc_core.h"
Ursula Brauna4cf0442017-01-09 16:55:14 +010038#include "smc_ib.h"
Thomas Richter6812baa2017-01-09 16:55:15 +010039#include "smc_pnet.h"
Ursula Braune6727f32017-01-09 16:55:23 +010040#include "smc_tx.h"
Ursula Braun952310c2017-01-09 16:55:24 +010041#include "smc_rx.h"
Ursula Braunb38d7322017-01-09 16:55:25 +010042#include "smc_close.h"
Ursula Braunac713872017-01-09 16:55:13 +010043
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010044static DEFINE_MUTEX(smc_create_lgr_pending); /* serialize link group
45 * creation
46 */
47
48struct smc_lgr_list smc_lgr_list = { /* established link groups */
49 .lock = __SPIN_LOCK_UNLOCKED(smc_lgr_list.lock),
50 .list = LIST_HEAD_INIT(smc_lgr_list.list),
51};
52
Ursula Brauna046d572017-01-09 16:55:16 +010053static void smc_tcp_listen_work(struct work_struct *);
54
Ursula Braunac713872017-01-09 16:55:13 +010055static void smc_set_keepalive(struct sock *sk, int val)
56{
57 struct smc_sock *smc = smc_sk(sk);
58
59 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
60}
61
Ursula Braunf16a7dd2017-01-09 16:55:26 +010062static struct smc_hashinfo smc_v4_hashinfo = {
63 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
64};
65
Karsten Graulaaa4d332018-03-16 15:06:41 +010066static struct smc_hashinfo smc_v6_hashinfo = {
67 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
68};
69
Ursula Braunf16a7dd2017-01-09 16:55:26 +010070int smc_hash_sk(struct sock *sk)
71{
72 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
73 struct hlist_head *head;
74
75 head = &h->ht;
76
77 write_lock_bh(&h->lock);
78 sk_add_node(sk, head);
79 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
80 write_unlock_bh(&h->lock);
81
82 return 0;
83}
84EXPORT_SYMBOL_GPL(smc_hash_sk);
85
86void smc_unhash_sk(struct sock *sk)
87{
88 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
89
90 write_lock_bh(&h->lock);
91 if (sk_del_node_init(sk))
92 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
93 write_unlock_bh(&h->lock);
94}
95EXPORT_SYMBOL_GPL(smc_unhash_sk);
96
97struct proto smc_proto = {
Ursula Braunac713872017-01-09 16:55:13 +010098 .name = "SMC",
99 .owner = THIS_MODULE,
100 .keepalive = smc_set_keepalive,
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100101 .hash = smc_hash_sk,
102 .unhash = smc_unhash_sk,
Ursula Braunac713872017-01-09 16:55:13 +0100103 .obj_size = sizeof(struct smc_sock),
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100104 .h.smc_hash = &smc_v4_hashinfo,
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800105 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Ursula Braunac713872017-01-09 16:55:13 +0100106};
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100107EXPORT_SYMBOL_GPL(smc_proto);
Ursula Braunac713872017-01-09 16:55:13 +0100108
Karsten Graulaaa4d332018-03-16 15:06:41 +0100109struct proto smc_proto6 = {
110 .name = "SMC6",
111 .owner = THIS_MODULE,
112 .keepalive = smc_set_keepalive,
113 .hash = smc_hash_sk,
114 .unhash = smc_unhash_sk,
115 .obj_size = sizeof(struct smc_sock),
116 .h.smc_hash = &smc_v6_hashinfo,
117 .slab_flags = SLAB_TYPESAFE_BY_RCU,
118};
119EXPORT_SYMBOL_GPL(smc_proto6);
120
Ursula Braunac713872017-01-09 16:55:13 +0100121static int smc_release(struct socket *sock)
122{
123 struct sock *sk = sock->sk;
124 struct smc_sock *smc;
Ursula Braunb38d7322017-01-09 16:55:25 +0100125 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +0100126
127 if (!sk)
128 goto out;
129
130 smc = smc_sk(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100131 if (sk->sk_state == SMC_LISTEN)
132 /* smc_close_non_accepted() is called and acquires
133 * sock lock for child sockets again
134 */
135 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
136 else
137 lock_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +0100138
Ursula Braun51f1de72018-01-26 09:28:48 +0100139 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100140 rc = smc_close_active(smc);
141 sock_set_flag(sk, SOCK_DEAD);
142 sk->sk_shutdown |= SHUTDOWN_MASK;
143 }
Ursula Braunac713872017-01-09 16:55:13 +0100144 if (smc->clcsock) {
145 sock_release(smc->clcsock);
146 smc->clcsock = NULL;
147 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100148 if (smc->use_fallback) {
149 sock_put(sk); /* passive closing */
150 sk->sk_state = SMC_CLOSED;
151 sk->sk_state_change(sk);
152 }
Ursula Braunac713872017-01-09 16:55:13 +0100153
154 /* detach socket */
155 sock_orphan(sk);
156 sock->sk = NULL;
Ursula Braun51f1de72018-01-26 09:28:48 +0100157 if (!smc->use_fallback && sk->sk_state == SMC_CLOSED)
Ursula Braunb38d7322017-01-09 16:55:25 +0100158 smc_conn_free(&smc->conn);
Ursula Braunac713872017-01-09 16:55:13 +0100159 release_sock(sk);
160
Ursula Braun51f1de72018-01-26 09:28:48 +0100161 sk->sk_prot->unhash(sk);
162 sock_put(sk); /* final sock_put */
Ursula Braunac713872017-01-09 16:55:13 +0100163out:
Ursula Braunb38d7322017-01-09 16:55:25 +0100164 return rc;
Ursula Braunac713872017-01-09 16:55:13 +0100165}
166
167static void smc_destruct(struct sock *sk)
168{
169 if (sk->sk_state != SMC_CLOSED)
170 return;
171 if (!sock_flag(sk, SOCK_DEAD))
172 return;
173
174 sk_refcnt_debug_dec(sk);
175}
176
Karsten Graulaaa4d332018-03-16 15:06:41 +0100177static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
178 int protocol)
Ursula Braunac713872017-01-09 16:55:13 +0100179{
180 struct smc_sock *smc;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100181 struct proto *prot;
Ursula Braunac713872017-01-09 16:55:13 +0100182 struct sock *sk;
183
Karsten Graulaaa4d332018-03-16 15:06:41 +0100184 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
185 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
Ursula Braunac713872017-01-09 16:55:13 +0100186 if (!sk)
187 return NULL;
188
189 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
190 sk->sk_state = SMC_INIT;
191 sk->sk_destruct = smc_destruct;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100192 sk->sk_protocol = protocol;
Ursula Braunac713872017-01-09 16:55:13 +0100193 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100194 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
195 INIT_LIST_HEAD(&smc->accept_q);
196 spin_lock_init(&smc->accept_q_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100197 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100198 sk_refcnt_debug_inc(sk);
Ursula Braunac713872017-01-09 16:55:13 +0100199
200 return sk;
201}
202
203static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
204 int addr_len)
205{
206 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
207 struct sock *sk = sock->sk;
208 struct smc_sock *smc;
209 int rc;
210
211 smc = smc_sk(sk);
212
213 /* replicate tests from inet_bind(), to be safe wrt. future changes */
214 rc = -EINVAL;
215 if (addr_len < sizeof(struct sockaddr_in))
216 goto out;
217
218 rc = -EAFNOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100219 if (addr->sin_family != AF_INET &&
220 addr->sin_family != AF_INET6 &&
221 addr->sin_family != AF_UNSPEC)
222 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100223 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
Karsten Graulaaa4d332018-03-16 15:06:41 +0100224 if (addr->sin_family == AF_UNSPEC &&
225 addr->sin_addr.s_addr != htonl(INADDR_ANY))
Ursula Braunac713872017-01-09 16:55:13 +0100226 goto out;
227
228 lock_sock(sk);
229
230 /* Check if socket is already active */
231 rc = -EINVAL;
232 if (sk->sk_state != SMC_INIT)
233 goto out_rel;
234
235 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
236 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
237
238out_rel:
239 release_sock(sk);
240out:
241 return rc;
242}
243
244static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
245 unsigned long mask)
246{
247 /* options we don't get control via setsockopt for */
248 nsk->sk_type = osk->sk_type;
249 nsk->sk_sndbuf = osk->sk_sndbuf;
250 nsk->sk_rcvbuf = osk->sk_rcvbuf;
251 nsk->sk_sndtimeo = osk->sk_sndtimeo;
252 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
253 nsk->sk_mark = osk->sk_mark;
254 nsk->sk_priority = osk->sk_priority;
255 nsk->sk_rcvlowat = osk->sk_rcvlowat;
256 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
257 nsk->sk_err = osk->sk_err;
258
259 nsk->sk_flags &= ~mask;
260 nsk->sk_flags |= osk->sk_flags & mask;
261}
262
263#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
264 (1UL << SOCK_KEEPOPEN) | \
265 (1UL << SOCK_LINGER) | \
266 (1UL << SOCK_BROADCAST) | \
267 (1UL << SOCK_TIMESTAMP) | \
268 (1UL << SOCK_DBG) | \
269 (1UL << SOCK_RCVTSTAMP) | \
270 (1UL << SOCK_RCVTSTAMPNS) | \
271 (1UL << SOCK_LOCALROUTE) | \
272 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
273 (1UL << SOCK_RXQ_OVFL) | \
274 (1UL << SOCK_WIFI_STATUS) | \
275 (1UL << SOCK_NOFCS) | \
276 (1UL << SOCK_FILTER_LOCKED))
277/* copy only relevant settings and flags of SOL_SOCKET level from smc to
278 * clc socket (since smc is not called for these options from net/core)
279 */
280static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
281{
282 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
283}
284
285#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
286 (1UL << SOCK_KEEPOPEN) | \
287 (1UL << SOCK_LINGER) | \
288 (1UL << SOCK_DBG))
289/* copy only settings and flags relevant for smc from clc to smc socket */
290static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
291{
292 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
293}
294
Stefan Raspl0f627122018-03-01 13:51:26 +0100295static int smc_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100296{
297 struct smc_link_group *lgr = smc->conn.lgr;
298 struct smc_link *link;
299 int rest;
300 int rc;
301
302 link = &lgr->lnk[SMC_SINGLE_LINK];
303 /* receive CONFIRM LINK request from server over RoCE fabric */
304 rest = wait_for_completion_interruptible_timeout(
305 &link->llc_confirm,
306 SMC_LLC_WAIT_FIRST_TIME);
307 if (rest <= 0) {
308 struct smc_clc_msg_decline dclc;
309
310 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
311 SMC_CLC_DECLINE);
312 return rc;
313 }
314
Karsten Graul75d320d2018-03-01 13:51:31 +0100315 if (link->llc_confirm_rc)
316 return SMC_CLC_DECL_RMBE_EC;
317
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100318 rc = smc_ib_modify_qp_rts(link);
319 if (rc)
320 return SMC_CLC_DECL_INTERR;
321
322 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200323
324 rc = smc_wr_reg_send(link,
325 smc->conn.rmb_desc->mr_rx[SMC_SINGLE_LINK]);
326 if (rc)
327 return SMC_CLC_DECL_INTERR;
328
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100329 /* send CONFIRM LINK response over RoCE fabric */
330 rc = smc_llc_send_confirm_link(link,
331 link->smcibdev->mac[link->ibport - 1],
Stefan Raspl0f627122018-03-01 13:51:26 +0100332 &link->smcibdev->gid[link->ibport - 1],
333 SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100334 if (rc < 0)
335 return SMC_CLC_DECL_TCL;
336
Karsten Graul52bedf32018-03-01 13:51:32 +0100337 /* receive ADD LINK request from server over RoCE fabric */
338 rest = wait_for_completion_interruptible_timeout(&link->llc_add,
339 SMC_LLC_WAIT_TIME);
340 if (rest <= 0) {
341 struct smc_clc_msg_decline dclc;
342
343 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
344 SMC_CLC_DECLINE);
345 return rc;
346 }
347
348 /* send add link reject message, only one link supported for now */
349 rc = smc_llc_send_add_link(link,
350 link->smcibdev->mac[link->ibport - 1],
351 &link->smcibdev->gid[link->ibport - 1],
352 SMC_LLC_RESP);
353 if (rc < 0)
354 return SMC_CLC_DECL_TCL;
355
356 link->state = SMC_LNK_ACTIVE;
357
Karsten Graul75d320d2018-03-01 13:51:31 +0100358 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100359}
360
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100361static void smc_conn_save_peer_info(struct smc_sock *smc,
362 struct smc_clc_msg_accept_confirm *clc)
363{
364 smc->conn.peer_conn_idx = clc->conn_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100365 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100366 smc->conn.peer_rmbe_size = smc_uncompress_bufsize(clc->rmbe_size);
367 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100368}
369
370static void smc_link_save_peer_info(struct smc_link *link,
371 struct smc_clc_msg_accept_confirm *clc)
372{
373 link->peer_qpn = ntoh24(clc->qpn);
374 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
375 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
376 link->peer_psn = ntoh24(clc->psn);
377 link->peer_mtu = clc->qp_mtu;
378}
379
Ursula Brauna046d572017-01-09 16:55:16 +0100380/* setup for RDMA connection of client */
381static int smc_connect_rdma(struct smc_sock *smc)
382{
383 struct smc_clc_msg_accept_confirm aclc;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100384 int local_contact = SMC_FIRST_CONTACT;
Ursula Brauna046d572017-01-09 16:55:16 +0100385 struct smc_ib_device *smcibdev;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100386 struct smc_link *link;
387 u8 srv_first_contact;
Ursula Brauna046d572017-01-09 16:55:16 +0100388 int reason_code = 0;
389 int rc = 0;
390 u8 ibport;
391
Ursula Braun51f1de72018-01-26 09:28:48 +0100392 sock_hold(&smc->sk); /* sock put in passive closing */
393
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200394 if (smc->use_fallback)
395 goto out_connected;
396
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200397 if (!tcp_sk(smc->clcsock->sk)->syn_smc) {
398 /* peer has not signalled SMC-capability */
399 smc->use_fallback = true;
400 goto out_connected;
401 }
402
Ursula Brauna046d572017-01-09 16:55:16 +0100403 /* IPSec connections opt out of SMC-R optimizations */
404 if (using_ipsec(smc)) {
405 reason_code = SMC_CLC_DECL_IPSEC;
406 goto decline_rdma;
407 }
408
409 /* PNET table look up: search active ib_device and port
410 * within same PNETID that also contains the ethernet device
411 * used for the internal TCP socket
412 */
413 smc_pnet_find_roce_resource(smc->clcsock->sk, &smcibdev, &ibport);
414 if (!smcibdev) {
415 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
416 goto decline_rdma;
417 }
418
419 /* do inband token exchange */
420 reason_code = smc_clc_send_proposal(smc, smcibdev, ibport);
421 if (reason_code < 0) {
422 rc = reason_code;
423 goto out_err;
424 }
425 if (reason_code > 0) /* configuration error */
426 goto decline_rdma;
427 /* receive SMC Accept CLC message */
428 reason_code = smc_clc_wait_msg(smc, &aclc, sizeof(aclc),
429 SMC_CLC_ACCEPT);
430 if (reason_code < 0) {
431 rc = reason_code;
432 goto out_err;
433 }
434 if (reason_code > 0)
435 goto decline_rdma;
436
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100437 srv_first_contact = aclc.hdr.flag;
438 mutex_lock(&smc_create_lgr_pending);
Karsten Graulbe6d4672018-03-01 13:51:28 +0100439 local_contact = smc_conn_create(smc, smcibdev, ibport, &aclc.lcl,
440 srv_first_contact);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100441 if (local_contact < 0) {
442 rc = local_contact;
443 if (rc == -ENOMEM)
444 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
445 else if (rc == -ENOLINK)
446 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
447 goto decline_rdma_unlock;
448 }
449 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
Ursula Brauna046d572017-01-09 16:55:16 +0100450
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100451 smc_conn_save_peer_info(smc, &aclc);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100452
Ursula Braun3e034722017-07-28 13:56:20 +0200453 /* create send buffer and rmb */
454 rc = smc_buf_create(smc);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100455 if (rc) {
456 reason_code = SMC_CLC_DECL_MEM;
457 goto decline_rdma_unlock;
458 }
459
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100460 if (local_contact == SMC_FIRST_CONTACT)
461 smc_link_save_peer_info(link, &aclc);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100462
463 rc = smc_rmb_rtoken_handling(&smc->conn, &aclc);
464 if (rc) {
465 reason_code = SMC_CLC_DECL_INTERR;
466 goto decline_rdma_unlock;
467 }
468
Ursula Braun46c28db2017-04-10 14:58:01 +0200469 smc_close_init(smc);
470 smc_rx_init(smc);
471
Ursula Braunbd4ad572017-01-09 16:55:20 +0100472 if (local_contact == SMC_FIRST_CONTACT) {
473 rc = smc_ib_ready_link(link);
474 if (rc) {
475 reason_code = SMC_CLC_DECL_INTERR;
476 goto decline_rdma_unlock;
477 }
Ursula Braun652a1e42017-07-28 13:56:17 +0200478 } else {
479 struct smc_buf_desc *buf_desc = smc->conn.rmb_desc;
480
481 if (!buf_desc->reused) {
482 /* register memory region for new rmb */
483 rc = smc_wr_reg_send(link,
484 buf_desc->mr_rx[SMC_SINGLE_LINK]);
485 if (rc) {
486 reason_code = SMC_CLC_DECL_INTERR;
487 goto decline_rdma_unlock;
488 }
489 }
Ursula Braunbd4ad572017-01-09 16:55:20 +0100490 }
Ursula Braun10428dd2017-07-28 13:56:22 +0200491 smc_rmb_sync_sg_for_device(&smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100492
493 rc = smc_clc_send_confirm(smc);
494 if (rc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100495 goto out_err_unlock;
Ursula Brauna046d572017-01-09 16:55:16 +0100496
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100497 if (local_contact == SMC_FIRST_CONTACT) {
498 /* QP confirmation over RoCE fabric */
Stefan Raspl0f627122018-03-01 13:51:26 +0100499 reason_code = smc_clnt_conf_first_link(smc);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100500 if (reason_code < 0) {
501 rc = reason_code;
502 goto out_err_unlock;
503 }
504 if (reason_code > 0)
505 goto decline_rdma_unlock;
506 }
Ursula Brauna046d572017-01-09 16:55:16 +0100507
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100508 mutex_unlock(&smc_create_lgr_pending);
Ursula Braune6727f32017-01-09 16:55:23 +0100509 smc_tx_init(smc);
510
Ursula Brauna046d572017-01-09 16:55:16 +0100511out_connected:
512 smc_copy_sock_settings_to_clc(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +0100513 if (smc->sk.sk_state == SMC_INIT)
514 smc->sk.sk_state = SMC_ACTIVE;
Ursula Brauna046d572017-01-09 16:55:16 +0100515
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100516 return rc ? rc : local_contact;
Ursula Brauna046d572017-01-09 16:55:16 +0100517
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100518decline_rdma_unlock:
Ursula Braun610db662018-01-25 11:15:34 +0100519 if (local_contact == SMC_FIRST_CONTACT)
520 smc_lgr_forget(smc->conn.lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100521 mutex_unlock(&smc_create_lgr_pending);
522 smc_conn_free(&smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100523decline_rdma:
524 /* RDMA setup failed, switch back to TCP */
525 smc->use_fallback = true;
526 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
Ursula Braunbfbedfd2017-09-21 09:16:32 +0200527 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braun0c9f1512017-12-07 13:38:45 +0100528 if (rc < 0)
Ursula Brauna046d572017-01-09 16:55:16 +0100529 goto out_err;
530 }
531 goto out_connected;
532
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100533out_err_unlock:
Ursula Braun610db662018-01-25 11:15:34 +0100534 if (local_contact == SMC_FIRST_CONTACT)
535 smc_lgr_forget(smc->conn.lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100536 mutex_unlock(&smc_create_lgr_pending);
537 smc_conn_free(&smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100538out_err:
Ursula Braun51f1de72018-01-26 09:28:48 +0100539 if (smc->sk.sk_state == SMC_INIT)
540 sock_put(&smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +0100541 return rc;
542}
543
Ursula Braunac713872017-01-09 16:55:13 +0100544static int smc_connect(struct socket *sock, struct sockaddr *addr,
545 int alen, int flags)
546{
547 struct sock *sk = sock->sk;
548 struct smc_sock *smc;
549 int rc = -EINVAL;
550
551 smc = smc_sk(sk);
552
553 /* separate smc parameter checking to be safe */
554 if (alen < sizeof(addr->sa_family))
555 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100556 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100557 goto out_err;
558
559 lock_sock(sk);
560 switch (sk->sk_state) {
561 default:
562 goto out;
563 case SMC_ACTIVE:
564 rc = -EISCONN;
565 goto out;
566 case SMC_INIT:
567 rc = 0;
568 break;
569 }
570
571 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200572 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +0100573 rc = kernel_connect(smc->clcsock, addr, alen, flags);
574 if (rc)
575 goto out;
576
Ursula Brauna046d572017-01-09 16:55:16 +0100577 /* setup RDMA connection */
578 rc = smc_connect_rdma(smc);
579 if (rc < 0)
580 goto out;
581 else
582 rc = 0; /* success cases including fallback */
Ursula Braunac713872017-01-09 16:55:13 +0100583
584out:
585 release_sock(sk);
586out_err:
587 return rc;
588}
589
590static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
591{
Ursula Braun3163c502018-01-24 10:28:12 +0100592 struct socket *new_clcsock = NULL;
593 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100594 struct sock *new_sk;
595 int rc;
596
Ursula Braun3163c502018-01-24 10:28:12 +0100597 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100598 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100599 if (!new_sk) {
600 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100601 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100602 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100603 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100604 goto out;
605 }
606 *new_smc = smc_sk(new_sk);
607
608 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
Ursula Braun3163c502018-01-24 10:28:12 +0100609 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100610 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100611 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100612 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Brauna046d572017-01-09 16:55:16 +0100613 if (new_clcsock)
614 sock_release(new_clcsock);
615 new_sk->sk_state = SMC_CLOSED;
616 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun3163c502018-01-24 10:28:12 +0100617 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100618 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100619 *new_smc = NULL;
620 goto out;
621 }
622
623 (*new_smc)->clcsock = new_clcsock;
624out:
625 return rc;
626}
627
Ursula Brauna046d572017-01-09 16:55:16 +0100628/* add a just created sock to the accept queue of the listen sock as
629 * candidate for a following socket accept call from user space
630 */
631static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
632{
633 struct smc_sock *par = smc_sk(parent);
634
Ursula Braun51f1de72018-01-26 09:28:48 +0100635 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100636 spin_lock(&par->accept_q_lock);
637 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
638 spin_unlock(&par->accept_q_lock);
639 sk_acceptq_added(parent);
640}
641
642/* remove a socket from the accept queue of its parental listening socket */
643static void smc_accept_unlink(struct sock *sk)
644{
645 struct smc_sock *par = smc_sk(sk)->listen_smc;
646
647 spin_lock(&par->accept_q_lock);
648 list_del_init(&smc_sk(sk)->accept_q);
649 spin_unlock(&par->accept_q_lock);
650 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100651 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100652}
653
654/* remove a sock from the accept queue to bind it to a new socket created
655 * for a socket accept call from user space
656 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100657struct sock *smc_accept_dequeue(struct sock *parent,
658 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100659{
660 struct smc_sock *isk, *n;
661 struct sock *new_sk;
662
663 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
664 new_sk = (struct sock *)isk;
665
666 smc_accept_unlink(new_sk);
667 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braun127f4972018-01-26 09:28:49 +0100668 if (isk->clcsock) {
669 sock_release(isk->clcsock);
670 isk->clcsock = NULL;
671 }
Ursula Braun288c83902017-04-10 14:58:04 +0200672 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100673 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +0100674 continue;
675 }
676 if (new_sock)
677 sock_graft(new_sk, new_sock);
678 return new_sk;
679 }
680 return NULL;
681}
682
683/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +0100684void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +0100685{
686 struct smc_sock *smc = smc_sk(sk);
687
Ursula Braunb38d7322017-01-09 16:55:25 +0100688 lock_sock(sk);
689 if (!sk->sk_lingertime)
690 /* wait for peer closing */
691 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100692 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100693 smc_close_active(smc);
Ursula Braun288c83902017-04-10 14:58:04 +0200694 sock_set_flag(sk, SOCK_DEAD);
695 sk->sk_shutdown |= SHUTDOWN_MASK;
696 }
Ursula Brauna046d572017-01-09 16:55:16 +0100697 if (smc->clcsock) {
698 struct socket *tcp;
699
700 tcp = smc->clcsock;
701 smc->clcsock = NULL;
702 sock_release(tcp);
703 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100704 if (smc->use_fallback) {
Ursula Braun51f1de72018-01-26 09:28:48 +0100705 sock_put(sk); /* passive closing */
706 sk->sk_state = SMC_CLOSED;
707 } else {
708 if (sk->sk_state == SMC_CLOSED)
709 smc_conn_free(&smc->conn);
Ursula Braunb38d7322017-01-09 16:55:25 +0100710 }
711 release_sock(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100712 sk->sk_prot->unhash(sk);
713 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +0100714}
715
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100716static int smc_serv_conf_first_link(struct smc_sock *smc)
717{
718 struct smc_link_group *lgr = smc->conn.lgr;
719 struct smc_link *link;
720 int rest;
721 int rc;
722
723 link = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun652a1e42017-07-28 13:56:17 +0200724
725 rc = smc_wr_reg_send(link,
726 smc->conn.rmb_desc->mr_rx[SMC_SINGLE_LINK]);
727 if (rc)
728 return SMC_CLC_DECL_INTERR;
729
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100730 /* send CONFIRM LINK request to client over the RoCE fabric */
731 rc = smc_llc_send_confirm_link(link,
732 link->smcibdev->mac[link->ibport - 1],
733 &link->smcibdev->gid[link->ibport - 1],
734 SMC_LLC_REQ);
735 if (rc < 0)
736 return SMC_CLC_DECL_TCL;
737
738 /* receive CONFIRM LINK response from client over the RoCE fabric */
739 rest = wait_for_completion_interruptible_timeout(
740 &link->llc_confirm_resp,
741 SMC_LLC_WAIT_FIRST_TIME);
742 if (rest <= 0) {
743 struct smc_clc_msg_decline dclc;
744
745 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
746 SMC_CLC_DECLINE);
Karsten Graul75d320d2018-03-01 13:51:31 +0100747 return rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100748 }
749
Karsten Graul75d320d2018-03-01 13:51:31 +0100750 if (link->llc_confirm_resp_rc)
751 return SMC_CLC_DECL_RMBE_EC;
752
Karsten Graul52bedf32018-03-01 13:51:32 +0100753 /* send ADD LINK request to client over the RoCE fabric */
754 rc = smc_llc_send_add_link(link,
755 link->smcibdev->mac[link->ibport - 1],
756 &link->smcibdev->gid[link->ibport - 1],
757 SMC_LLC_REQ);
758 if (rc < 0)
759 return SMC_CLC_DECL_TCL;
760
761 /* receive ADD LINK response from client over the RoCE fabric */
762 rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
763 SMC_LLC_WAIT_TIME);
764 if (rest <= 0) {
765 struct smc_clc_msg_decline dclc;
766
767 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
768 SMC_CLC_DECLINE);
769 return rc;
770 }
771
772 link->state = SMC_LNK_ACTIVE;
773
Karsten Graul75d320d2018-03-01 13:51:31 +0100774 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100775}
776
Ursula Brauna046d572017-01-09 16:55:16 +0100777/* setup for RDMA connection of server */
778static void smc_listen_work(struct work_struct *work)
779{
780 struct smc_sock *new_smc = container_of(work, struct smc_sock,
781 smc_listen_work);
Ursula Braune7b7a642017-12-07 13:38:49 +0100782 struct smc_clc_msg_proposal_prefix *pclc_prfx;
Ursula Brauna046d572017-01-09 16:55:16 +0100783 struct socket *newclcsock = new_smc->clcsock;
784 struct smc_sock *lsmc = new_smc->listen_smc;
785 struct smc_clc_msg_accept_confirm cclc;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100786 int local_contact = SMC_REUSE_CONTACT;
Ursula Brauna046d572017-01-09 16:55:16 +0100787 struct sock *newsmcsk = &new_smc->sk;
Ursula Braune7b7a642017-12-07 13:38:49 +0100788 struct smc_clc_msg_proposal *pclc;
Ursula Brauna046d572017-01-09 16:55:16 +0100789 struct smc_ib_device *smcibdev;
Ursula Braune7b7a642017-12-07 13:38:49 +0100790 u8 buf[SMC_CLC_MAX_LEN];
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100791 struct smc_link *link;
Ursula Brauna046d572017-01-09 16:55:16 +0100792 int reason_code = 0;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100793 int rc = 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100794 u8 ibport;
795
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200796 if (new_smc->use_fallback)
797 goto out_connected;
798
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200799 /* check if peer is smc capable */
800 if (!tcp_sk(newclcsock->sk)->syn_smc) {
801 new_smc->use_fallback = true;
802 goto out_connected;
803 }
804
Ursula Brauna046d572017-01-09 16:55:16 +0100805 /* do inband token exchange -
806 *wait for and receive SMC Proposal CLC message
807 */
Ursula Braune7b7a642017-12-07 13:38:49 +0100808 reason_code = smc_clc_wait_msg(new_smc, &buf, sizeof(buf),
Ursula Brauna046d572017-01-09 16:55:16 +0100809 SMC_CLC_PROPOSAL);
810 if (reason_code < 0)
811 goto out_err;
812 if (reason_code > 0)
813 goto decline_rdma;
814
815 /* IPSec connections opt out of SMC-R optimizations */
816 if (using_ipsec(new_smc)) {
817 reason_code = SMC_CLC_DECL_IPSEC;
818 goto decline_rdma;
819 }
820
821 /* PNET table look up: search active ib_device and port
822 * within same PNETID that also contains the ethernet device
823 * used for the internal TCP socket
824 */
825 smc_pnet_find_roce_resource(newclcsock->sk, &smcibdev, &ibport);
826 if (!smcibdev) {
827 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
828 goto decline_rdma;
829 }
830
Ursula Braune7b7a642017-12-07 13:38:49 +0100831 pclc = (struct smc_clc_msg_proposal *)&buf;
832 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
Karsten Graulc246d942018-03-16 15:06:39 +0100833
834 rc = smc_clc_prfx_match(newclcsock, pclc_prfx);
835 if (rc) {
Ursula Brauna046d572017-01-09 16:55:16 +0100836 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
837 goto decline_rdma;
838 }
839
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100840 /* allocate connection / link group */
841 mutex_lock(&smc_create_lgr_pending);
Karsten Graulbe6d4672018-03-01 13:51:28 +0100842 local_contact = smc_conn_create(new_smc, smcibdev, ibport, &pclc->lcl,
843 0);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100844 if (local_contact < 0) {
845 rc = local_contact;
846 if (rc == -ENOMEM)
847 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
Ursula Braun145686b2017-10-25 11:01:44 +0200848 goto decline_rdma_unlock;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100849 }
850 link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
Ursula Brauna046d572017-01-09 16:55:16 +0100851
Ursula Braun3e034722017-07-28 13:56:20 +0200852 /* create send buffer and rmb */
853 rc = smc_buf_create(new_smc);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100854 if (rc) {
855 reason_code = SMC_CLC_DECL_MEM;
Ursula Braun145686b2017-10-25 11:01:44 +0200856 goto decline_rdma_unlock;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100857 }
Ursula Brauna046d572017-01-09 16:55:16 +0100858
Ursula Braun46c28db2017-04-10 14:58:01 +0200859 smc_close_init(new_smc);
860 smc_rx_init(new_smc);
861
Ursula Braun652a1e42017-07-28 13:56:17 +0200862 if (local_contact != SMC_FIRST_CONTACT) {
863 struct smc_buf_desc *buf_desc = new_smc->conn.rmb_desc;
864
865 if (!buf_desc->reused) {
866 /* register memory region for new rmb */
867 rc = smc_wr_reg_send(link,
868 buf_desc->mr_rx[SMC_SINGLE_LINK]);
869 if (rc) {
870 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);
1092 smc_rx_wait_data(smc_sk(nsk), &timeo);
1093 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);
1207 /* if non-blocking connect finished ... */
1208 lock_sock(sk);
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001209 if ((sk->sk_state == SMC_INIT) && (mask & EPOLLOUT)) {
Ursula Brauna046d572017-01-09 16:55:16 +01001210 sk->sk_err = smc->clcsock->sk->sk_err;
1211 if (sk->sk_err) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001212 mask |= EPOLLERR;
Ursula Brauna046d572017-01-09 16:55:16 +01001213 } else {
1214 rc = smc_connect_rdma(smc);
1215 if (rc < 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001216 mask |= EPOLLERR;
Ursula Braun8dce2782018-01-26 09:28:47 +01001217 /* success cases including fallback */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001218 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Brauna046d572017-01-09 16:55:16 +01001219 }
Ursula Braunac713872017-01-09 16:55:13 +01001220 }
Ursula Braunac713872017-01-09 16:55:13 +01001221 } else {
Ursula Braun8dce2782018-01-26 09:28:47 +01001222 if (sk->sk_state != SMC_CLOSED) {
1223 release_sock(sk);
1224 sock_poll_wait(file, sk_sleep(sk), wait);
1225 lock_sock(sk);
1226 }
Ursula Brauna046d572017-01-09 16:55:16 +01001227 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001228 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001229 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1230 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001231 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001232 if (sk->sk_state == SMC_LISTEN) {
1233 /* woken up by sk_data_ready in smc_listen_work() */
1234 mask = smc_accept_poll(sk);
1235 } else {
1236 if (atomic_read(&smc->conn.sndbuf_space) ||
1237 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001238 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001239 } else {
1240 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1241 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1242 }
1243 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001244 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001245 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001246 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001247 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001248 mask |= EPOLLIN;
Ursula Braun8dce2782018-01-26 09:28:47 +01001249 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001250
Ursula Braunac713872017-01-09 16:55:13 +01001251 }
Ursula Braun8dce2782018-01-26 09:28:47 +01001252 release_sock(sk);
1253 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001254
1255 return mask;
1256}
1257
1258static int smc_shutdown(struct socket *sock, int how)
1259{
1260 struct sock *sk = sock->sk;
1261 struct smc_sock *smc;
1262 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001263 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001264
1265 smc = smc_sk(sk);
1266
1267 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001268 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001269
1270 lock_sock(sk);
1271
1272 rc = -ENOTCONN;
Ursula Braunb38d7322017-01-09 16:55:25 +01001273 if ((sk->sk_state != SMC_LISTEN) &&
1274 (sk->sk_state != SMC_ACTIVE) &&
1275 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1276 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1277 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1278 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1279 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001280 goto out;
1281 if (smc->use_fallback) {
1282 rc = kernel_sock_shutdown(smc->clcsock, how);
1283 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1284 if (sk->sk_shutdown == SHUTDOWN_MASK)
1285 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001286 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001287 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001288 switch (how) {
1289 case SHUT_RDWR: /* shutdown in both directions */
1290 rc = smc_close_active(smc);
1291 break;
1292 case SHUT_WR:
1293 rc = smc_close_shutdown_write(smc);
1294 break;
1295 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001296 rc = 0;
1297 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001298 break;
1299 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001300 if (smc->clcsock)
1301 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001302 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1303 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001304
1305out:
1306 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001307 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001308}
1309
1310static int smc_setsockopt(struct socket *sock, int level, int optname,
1311 char __user *optval, unsigned int optlen)
1312{
1313 struct sock *sk = sock->sk;
1314 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001315 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001316
1317 smc = smc_sk(sk);
1318
1319 /* generic setsockopts reaching us here always apply to the
1320 * CLC socket
1321 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001322 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1323 optval, optlen);
1324 if (smc->clcsock->sk->sk_err) {
1325 sk->sk_err = smc->clcsock->sk->sk_err;
1326 sk->sk_error_report(sk);
1327 }
1328 if (rc)
1329 return rc;
1330
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001331 if (optlen < sizeof(int))
1332 return rc;
1333 get_user(val, (int __user *)optval);
1334
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001335 lock_sock(sk);
1336 switch (optname) {
1337 case TCP_ULP:
1338 case TCP_FASTOPEN:
1339 case TCP_FASTOPEN_CONNECT:
1340 case TCP_FASTOPEN_KEY:
1341 case TCP_FASTOPEN_NO_COOKIE:
1342 /* option not supported by SMC */
1343 if (sk->sk_state == SMC_INIT) {
1344 smc->use_fallback = true;
1345 } else {
1346 if (!smc->use_fallback)
1347 rc = -EINVAL;
1348 }
1349 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001350 case TCP_NODELAY:
1351 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
1352 if (val)
1353 mod_delayed_work(system_wq, &smc->conn.tx_work,
1354 0);
1355 }
1356 break;
1357 case TCP_CORK:
1358 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
1359 if (!val)
1360 mod_delayed_work(system_wq, &smc->conn.tx_work,
1361 0);
1362 }
1363 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001364 case TCP_DEFER_ACCEPT:
1365 smc->sockopt_defer_accept = val;
1366 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001367 default:
1368 break;
1369 }
1370 release_sock(sk);
1371
1372 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001373}
1374
1375static int smc_getsockopt(struct socket *sock, int level, int optname,
1376 char __user *optval, int __user *optlen)
1377{
1378 struct smc_sock *smc;
1379
1380 smc = smc_sk(sock->sk);
1381 /* socket options apply to the CLC socket */
1382 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1383 optval, optlen);
1384}
1385
1386static int smc_ioctl(struct socket *sock, unsigned int cmd,
1387 unsigned long arg)
1388{
1389 struct smc_sock *smc;
1390
1391 smc = smc_sk(sock->sk);
1392 if (smc->use_fallback)
1393 return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1394 else
1395 return sock_no_ioctl(sock, cmd, arg);
1396}
1397
1398static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1399 int offset, size_t size, int flags)
1400{
1401 struct sock *sk = sock->sk;
1402 struct smc_sock *smc;
1403 int rc = -EPIPE;
1404
1405 smc = smc_sk(sk);
1406 lock_sock(sk);
1407 if (sk->sk_state != SMC_ACTIVE)
1408 goto out;
1409 if (smc->use_fallback)
1410 rc = kernel_sendpage(smc->clcsock, page, offset,
1411 size, flags);
1412 else
1413 rc = sock_no_sendpage(sock, page, offset, size, flags);
1414
1415out:
1416 release_sock(sk);
1417 return rc;
1418}
1419
1420static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1421 struct pipe_inode_info *pipe, size_t len,
1422 unsigned int flags)
1423{
1424 struct sock *sk = sock->sk;
1425 struct smc_sock *smc;
1426 int rc = -ENOTCONN;
1427
1428 smc = smc_sk(sk);
1429 lock_sock(sk);
1430 if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED))
1431 goto out;
1432 if (smc->use_fallback) {
1433 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1434 pipe, len, flags);
1435 } else {
1436 rc = -EOPNOTSUPP;
1437 }
1438out:
1439 release_sock(sk);
1440 return rc;
1441}
1442
1443/* must look like tcp */
1444static const struct proto_ops smc_sock_ops = {
1445 .family = PF_SMC,
1446 .owner = THIS_MODULE,
1447 .release = smc_release,
1448 .bind = smc_bind,
1449 .connect = smc_connect,
1450 .socketpair = sock_no_socketpair,
1451 .accept = smc_accept,
1452 .getname = smc_getname,
1453 .poll = smc_poll,
1454 .ioctl = smc_ioctl,
1455 .listen = smc_listen,
1456 .shutdown = smc_shutdown,
1457 .setsockopt = smc_setsockopt,
1458 .getsockopt = smc_getsockopt,
1459 .sendmsg = smc_sendmsg,
1460 .recvmsg = smc_recvmsg,
1461 .mmap = sock_no_mmap,
1462 .sendpage = smc_sendpage,
1463 .splice_read = smc_splice_read,
1464};
1465
1466static int smc_create(struct net *net, struct socket *sock, int protocol,
1467 int kern)
1468{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001469 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001470 struct smc_sock *smc;
1471 struct sock *sk;
1472 int rc;
1473
1474 rc = -ESOCKTNOSUPPORT;
1475 if (sock->type != SOCK_STREAM)
1476 goto out;
1477
1478 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001479 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01001480 goto out;
1481
1482 rc = -ENOBUFS;
1483 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001484 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001485 if (!sk)
1486 goto out;
1487
1488 /* create internal TCP socket for CLC handshake and fallback */
1489 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001490 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graulaaa4d332018-03-16 15:06:41 +01001491 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1492 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01001493 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01001494 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01001495 goto out;
1496 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01001497 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1498 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01001499
1500out:
1501 return rc;
1502}
1503
1504static const struct net_proto_family smc_sock_family_ops = {
1505 .family = PF_SMC,
1506 .owner = THIS_MODULE,
1507 .create = smc_create,
1508};
1509
1510static int __init smc_init(void)
1511{
1512 int rc;
1513
Thomas Richter6812baa2017-01-09 16:55:15 +01001514 rc = smc_pnet_init();
1515 if (rc)
1516 return rc;
1517
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001518 rc = smc_llc_init();
1519 if (rc) {
1520 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1521 goto out_pnet;
1522 }
1523
Ursula Braun5f083182017-01-09 16:55:22 +01001524 rc = smc_cdc_init();
1525 if (rc) {
1526 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1527 goto out_pnet;
1528 }
1529
Ursula Braunac713872017-01-09 16:55:13 +01001530 rc = proto_register(&smc_proto, 1);
1531 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01001532 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Thomas Richter6812baa2017-01-09 16:55:15 +01001533 goto out_pnet;
Ursula Braunac713872017-01-09 16:55:13 +01001534 }
1535
Karsten Graulaaa4d332018-03-16 15:06:41 +01001536 rc = proto_register(&smc_proto6, 1);
1537 if (rc) {
1538 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
1539 goto out_proto;
1540 }
1541
Ursula Braunac713872017-01-09 16:55:13 +01001542 rc = sock_register(&smc_sock_family_ops);
1543 if (rc) {
1544 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001545 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01001546 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01001547 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001548 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01001549
Ursula Brauna4cf0442017-01-09 16:55:14 +01001550 rc = smc_ib_register_client();
1551 if (rc) {
1552 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1553 goto out_sock;
1554 }
1555
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001556 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01001557 return 0;
1558
Ursula Brauna4cf0442017-01-09 16:55:14 +01001559out_sock:
1560 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001561out_proto6:
1562 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01001563out_proto:
1564 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001565out_pnet:
1566 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001567 return rc;
1568}
1569
1570static void __exit smc_exit(void)
1571{
Ursula Braun0cfdd8f2017-01-09 16:55:17 +01001572 struct smc_link_group *lgr, *lg;
1573 LIST_HEAD(lgr_freeing_list);
1574
1575 spin_lock_bh(&smc_lgr_list.lock);
1576 if (!list_empty(&smc_lgr_list.list))
1577 list_splice_init(&smc_lgr_list.list, &lgr_freeing_list);
1578 spin_unlock_bh(&smc_lgr_list.lock);
1579 list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) {
1580 list_del_init(&lgr->list);
Ursula Braun268ffcc2018-03-14 11:01:01 +01001581 cancel_delayed_work_sync(&lgr->free_work);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +01001582 smc_lgr_free(lgr); /* free link group */
1583 }
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001584 static_branch_disable(&tcp_have_smc);
Ursula Brauna4cf0442017-01-09 16:55:14 +01001585 smc_ib_unregister_client();
Ursula Braunac713872017-01-09 16:55:13 +01001586 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001587 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01001588 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001589 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001590}
1591
1592module_init(smc_init);
1593module_exit(smc_exit);
1594
1595MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
1596MODULE_DESCRIPTION("smc socket address family");
1597MODULE_LICENSE("GPL");
1598MODULE_ALIAS_NETPROTO(PF_SMC);