blob: 6ad4f6c771c3fa63d3ae714dbe65879910963a21 [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);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700196 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
Ursula Brauna046d572017-01-09 16:55:16 +0100197 INIT_LIST_HEAD(&smc->accept_q);
198 spin_lock_init(&smc->accept_q_lock);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700199 spin_lock_init(&smc->conn.send_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100200 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100201 sk_refcnt_debug_inc(sk);
Ursula Braunac713872017-01-09 16:55:13 +0100202
203 return sk;
204}
205
206static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
207 int addr_len)
208{
209 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
210 struct sock *sk = sock->sk;
211 struct smc_sock *smc;
212 int rc;
213
214 smc = smc_sk(sk);
215
216 /* replicate tests from inet_bind(), to be safe wrt. future changes */
217 rc = -EINVAL;
218 if (addr_len < sizeof(struct sockaddr_in))
219 goto out;
220
221 rc = -EAFNOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100222 if (addr->sin_family != AF_INET &&
223 addr->sin_family != AF_INET6 &&
224 addr->sin_family != AF_UNSPEC)
225 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100226 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
Karsten Graulaaa4d332018-03-16 15:06:41 +0100227 if (addr->sin_family == AF_UNSPEC &&
228 addr->sin_addr.s_addr != htonl(INADDR_ANY))
Ursula Braunac713872017-01-09 16:55:13 +0100229 goto out;
230
231 lock_sock(sk);
232
233 /* Check if socket is already active */
234 rc = -EINVAL;
235 if (sk->sk_state != SMC_INIT)
236 goto out_rel;
237
238 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
239 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
240
241out_rel:
242 release_sock(sk);
243out:
244 return rc;
245}
246
247static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
248 unsigned long mask)
249{
250 /* options we don't get control via setsockopt for */
251 nsk->sk_type = osk->sk_type;
252 nsk->sk_sndbuf = osk->sk_sndbuf;
253 nsk->sk_rcvbuf = osk->sk_rcvbuf;
254 nsk->sk_sndtimeo = osk->sk_sndtimeo;
255 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
256 nsk->sk_mark = osk->sk_mark;
257 nsk->sk_priority = osk->sk_priority;
258 nsk->sk_rcvlowat = osk->sk_rcvlowat;
259 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
260 nsk->sk_err = osk->sk_err;
261
262 nsk->sk_flags &= ~mask;
263 nsk->sk_flags |= osk->sk_flags & mask;
264}
265
266#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
267 (1UL << SOCK_KEEPOPEN) | \
268 (1UL << SOCK_LINGER) | \
269 (1UL << SOCK_BROADCAST) | \
270 (1UL << SOCK_TIMESTAMP) | \
271 (1UL << SOCK_DBG) | \
272 (1UL << SOCK_RCVTSTAMP) | \
273 (1UL << SOCK_RCVTSTAMPNS) | \
274 (1UL << SOCK_LOCALROUTE) | \
275 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
276 (1UL << SOCK_RXQ_OVFL) | \
277 (1UL << SOCK_WIFI_STATUS) | \
278 (1UL << SOCK_NOFCS) | \
279 (1UL << SOCK_FILTER_LOCKED))
280/* copy only relevant settings and flags of SOL_SOCKET level from smc to
281 * clc socket (since smc is not called for these options from net/core)
282 */
283static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
284{
285 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
286}
287
288#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
289 (1UL << SOCK_KEEPOPEN) | \
290 (1UL << SOCK_LINGER) | \
291 (1UL << SOCK_DBG))
292/* copy only settings and flags relevant for smc from clc to smc socket */
293static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
294{
295 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
296}
297
Karsten Graul44aa81c2018-05-15 17:04:55 +0200298/* register a new rmb, optionally send confirm_rkey msg to register with peer */
299static int smc_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc,
300 bool conf_rkey)
Karsten Graule63a5f82018-05-03 17:57:37 +0200301{
302 /* register memory region for new rmb */
Karsten Graula6920d12018-05-03 17:57:38 +0200303 if (smc_wr_reg_send(link, rmb_desc->mr_rx[SMC_SINGLE_LINK])) {
304 rmb_desc->regerr = 1;
Karsten Graule63a5f82018-05-03 17:57:37 +0200305 return -EFAULT;
Karsten Graula6920d12018-05-03 17:57:38 +0200306 }
Karsten Graul44aa81c2018-05-15 17:04:55 +0200307 if (!conf_rkey)
308 return 0;
309 /* exchange confirm_rkey msg with peer */
310 if (smc_llc_do_confirm_rkey(link, rmb_desc)) {
311 rmb_desc->regerr = 1;
312 return -EFAULT;
313 }
Karsten Graule63a5f82018-05-03 17:57:37 +0200314 return 0;
315}
316
Stefan Raspl0f627122018-03-01 13:51:26 +0100317static int smc_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100318{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200319 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100320 struct smc_link_group *lgr = smc->conn.lgr;
321 struct smc_link *link;
322 int rest;
323 int rc;
324
325 link = &lgr->lnk[SMC_SINGLE_LINK];
326 /* receive CONFIRM LINK request from server over RoCE fabric */
327 rest = wait_for_completion_interruptible_timeout(
328 &link->llc_confirm,
329 SMC_LLC_WAIT_FIRST_TIME);
330 if (rest <= 0) {
331 struct smc_clc_msg_decline dclc;
332
333 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
334 SMC_CLC_DECLINE);
335 return rc;
336 }
337
Karsten Graul75d320d2018-03-01 13:51:31 +0100338 if (link->llc_confirm_rc)
339 return SMC_CLC_DECL_RMBE_EC;
340
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100341 rc = smc_ib_modify_qp_rts(link);
342 if (rc)
343 return SMC_CLC_DECL_INTERR;
344
345 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200346
Karsten Graul44aa81c2018-05-15 17:04:55 +0200347 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Ursula Braun652a1e42017-07-28 13:56:17 +0200348 return SMC_CLC_DECL_INTERR;
349
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100350 /* send CONFIRM LINK response over RoCE fabric */
351 rc = smc_llc_send_confirm_link(link,
352 link->smcibdev->mac[link->ibport - 1],
Stefan Raspl0f627122018-03-01 13:51:26 +0100353 &link->smcibdev->gid[link->ibport - 1],
354 SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100355 if (rc < 0)
356 return SMC_CLC_DECL_TCL;
357
Karsten Graul52bedf32018-03-01 13:51:32 +0100358 /* receive ADD LINK request from server over RoCE fabric */
359 rest = wait_for_completion_interruptible_timeout(&link->llc_add,
360 SMC_LLC_WAIT_TIME);
361 if (rest <= 0) {
362 struct smc_clc_msg_decline dclc;
363
364 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
365 SMC_CLC_DECLINE);
366 return rc;
367 }
368
369 /* send add link reject message, only one link supported for now */
370 rc = smc_llc_send_add_link(link,
371 link->smcibdev->mac[link->ibport - 1],
372 &link->smcibdev->gid[link->ibport - 1],
373 SMC_LLC_RESP);
374 if (rc < 0)
375 return SMC_CLC_DECL_TCL;
376
Karsten Graul877ae5b2018-05-02 16:56:44 +0200377 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100378
Karsten Graul75d320d2018-03-01 13:51:31 +0100379 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100380}
381
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100382static void smc_conn_save_peer_info(struct smc_sock *smc,
383 struct smc_clc_msg_accept_confirm *clc)
384{
385 smc->conn.peer_conn_idx = clc->conn_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100386 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100387 smc->conn.peer_rmbe_size = smc_uncompress_bufsize(clc->rmbe_size);
388 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100389}
390
391static void smc_link_save_peer_info(struct smc_link *link,
392 struct smc_clc_msg_accept_confirm *clc)
393{
394 link->peer_qpn = ntoh24(clc->qpn);
395 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
396 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
397 link->peer_psn = ntoh24(clc->psn);
398 link->peer_mtu = clc->qp_mtu;
399}
400
Ursula Brauna046d572017-01-09 16:55:16 +0100401/* setup for RDMA connection of client */
402static int smc_connect_rdma(struct smc_sock *smc)
403{
404 struct smc_clc_msg_accept_confirm aclc;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100405 int local_contact = SMC_FIRST_CONTACT;
Ursula Brauna046d572017-01-09 16:55:16 +0100406 struct smc_ib_device *smcibdev;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100407 struct smc_link *link;
408 u8 srv_first_contact;
Ursula Brauna046d572017-01-09 16:55:16 +0100409 int reason_code = 0;
410 int rc = 0;
411 u8 ibport;
412
Ursula Braun51f1de72018-01-26 09:28:48 +0100413 sock_hold(&smc->sk); /* sock put in passive closing */
414
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200415 if (smc->use_fallback)
416 goto out_connected;
417
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200418 if (!tcp_sk(smc->clcsock->sk)->syn_smc) {
419 /* peer has not signalled SMC-capability */
420 smc->use_fallback = true;
421 goto out_connected;
422 }
423
Ursula Brauna046d572017-01-09 16:55:16 +0100424 /* IPSec connections opt out of SMC-R optimizations */
425 if (using_ipsec(smc)) {
426 reason_code = SMC_CLC_DECL_IPSEC;
427 goto decline_rdma;
428 }
429
430 /* PNET table look up: search active ib_device and port
431 * within same PNETID that also contains the ethernet device
432 * used for the internal TCP socket
433 */
434 smc_pnet_find_roce_resource(smc->clcsock->sk, &smcibdev, &ibport);
435 if (!smcibdev) {
436 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
437 goto decline_rdma;
438 }
439
440 /* do inband token exchange */
441 reason_code = smc_clc_send_proposal(smc, smcibdev, ibport);
442 if (reason_code < 0) {
443 rc = reason_code;
444 goto out_err;
445 }
446 if (reason_code > 0) /* configuration error */
447 goto decline_rdma;
448 /* receive SMC Accept CLC message */
449 reason_code = smc_clc_wait_msg(smc, &aclc, sizeof(aclc),
450 SMC_CLC_ACCEPT);
451 if (reason_code < 0) {
452 rc = reason_code;
453 goto out_err;
454 }
455 if (reason_code > 0)
456 goto decline_rdma;
457
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100458 srv_first_contact = aclc.hdr.flag;
459 mutex_lock(&smc_create_lgr_pending);
Karsten Graulbe6d4672018-03-01 13:51:28 +0100460 local_contact = smc_conn_create(smc, smcibdev, ibport, &aclc.lcl,
461 srv_first_contact);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100462 if (local_contact < 0) {
463 rc = local_contact;
464 if (rc == -ENOMEM)
465 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
466 else if (rc == -ENOLINK)
467 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
Karsten Graul1401ea02018-05-15 17:05:00 +0200468 else
469 reason_code = SMC_CLC_DECL_INTERR; /* other error */
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100470 goto decline_rdma_unlock;
471 }
472 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
Ursula Brauna046d572017-01-09 16:55:16 +0100473
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100474 smc_conn_save_peer_info(smc, &aclc);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100475
Ursula Braun3e034722017-07-28 13:56:20 +0200476 /* create send buffer and rmb */
477 rc = smc_buf_create(smc);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100478 if (rc) {
479 reason_code = SMC_CLC_DECL_MEM;
480 goto decline_rdma_unlock;
481 }
482
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100483 if (local_contact == SMC_FIRST_CONTACT)
484 smc_link_save_peer_info(link, &aclc);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100485
486 rc = smc_rmb_rtoken_handling(&smc->conn, &aclc);
487 if (rc) {
488 reason_code = SMC_CLC_DECL_INTERR;
489 goto decline_rdma_unlock;
490 }
491
Ursula Braun46c28db2017-04-10 14:58:01 +0200492 smc_close_init(smc);
493 smc_rx_init(smc);
494
Ursula Braunbd4ad572017-01-09 16:55:20 +0100495 if (local_contact == SMC_FIRST_CONTACT) {
496 rc = smc_ib_ready_link(link);
497 if (rc) {
498 reason_code = SMC_CLC_DECL_INTERR;
499 goto decline_rdma_unlock;
500 }
Ursula Braun652a1e42017-07-28 13:56:17 +0200501 } else {
Karsten Graule63a5f82018-05-03 17:57:37 +0200502 if (!smc->conn.rmb_desc->reused) {
Karsten Graul44aa81c2018-05-15 17:04:55 +0200503 if (smc_reg_rmb(link, smc->conn.rmb_desc, true)) {
Ursula Braun652a1e42017-07-28 13:56:17 +0200504 reason_code = SMC_CLC_DECL_INTERR;
505 goto decline_rdma_unlock;
506 }
507 }
Ursula Braunbd4ad572017-01-09 16:55:20 +0100508 }
Ursula Braun10428dd2017-07-28 13:56:22 +0200509 smc_rmb_sync_sg_for_device(&smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100510
511 rc = smc_clc_send_confirm(smc);
512 if (rc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100513 goto out_err_unlock;
Ursula Brauna046d572017-01-09 16:55:16 +0100514
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100515 if (local_contact == SMC_FIRST_CONTACT) {
516 /* QP confirmation over RoCE fabric */
Stefan Raspl0f627122018-03-01 13:51:26 +0100517 reason_code = smc_clnt_conf_first_link(smc);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100518 if (reason_code < 0) {
519 rc = reason_code;
520 goto out_err_unlock;
521 }
522 if (reason_code > 0)
523 goto decline_rdma_unlock;
524 }
Ursula Brauna046d572017-01-09 16:55:16 +0100525
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100526 mutex_unlock(&smc_create_lgr_pending);
Ursula Braune6727f32017-01-09 16:55:23 +0100527 smc_tx_init(smc);
528
Ursula Brauna046d572017-01-09 16:55:16 +0100529out_connected:
530 smc_copy_sock_settings_to_clc(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +0100531 if (smc->sk.sk_state == SMC_INIT)
532 smc->sk.sk_state = SMC_ACTIVE;
Ursula Brauna046d572017-01-09 16:55:16 +0100533
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100534 return rc ? rc : local_contact;
Ursula Brauna046d572017-01-09 16:55:16 +0100535
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100536decline_rdma_unlock:
Ursula Braun610db662018-01-25 11:15:34 +0100537 if (local_contact == SMC_FIRST_CONTACT)
538 smc_lgr_forget(smc->conn.lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100539 mutex_unlock(&smc_create_lgr_pending);
540 smc_conn_free(&smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100541decline_rdma:
542 /* RDMA setup failed, switch back to TCP */
543 smc->use_fallback = true;
544 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
Ursula Braunbfbedfd2017-09-21 09:16:32 +0200545 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braun0c9f1512017-12-07 13:38:45 +0100546 if (rc < 0)
Ursula Brauna046d572017-01-09 16:55:16 +0100547 goto out_err;
548 }
549 goto out_connected;
550
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100551out_err_unlock:
Ursula Braun610db662018-01-25 11:15:34 +0100552 if (local_contact == SMC_FIRST_CONTACT)
553 smc_lgr_forget(smc->conn.lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100554 mutex_unlock(&smc_create_lgr_pending);
555 smc_conn_free(&smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100556out_err:
Ursula Braun51f1de72018-01-26 09:28:48 +0100557 if (smc->sk.sk_state == SMC_INIT)
558 sock_put(&smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +0100559 return rc;
560}
561
Ursula Braunac713872017-01-09 16:55:13 +0100562static int smc_connect(struct socket *sock, struct sockaddr *addr,
563 int alen, int flags)
564{
565 struct sock *sk = sock->sk;
566 struct smc_sock *smc;
567 int rc = -EINVAL;
568
569 smc = smc_sk(sk);
570
571 /* separate smc parameter checking to be safe */
572 if (alen < sizeof(addr->sa_family))
573 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100574 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100575 goto out_err;
576
577 lock_sock(sk);
578 switch (sk->sk_state) {
579 default:
580 goto out;
581 case SMC_ACTIVE:
582 rc = -EISCONN;
583 goto out;
584 case SMC_INIT:
585 rc = 0;
586 break;
587 }
588
589 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200590 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +0100591 rc = kernel_connect(smc->clcsock, addr, alen, flags);
592 if (rc)
593 goto out;
594
Ursula Brauna046d572017-01-09 16:55:16 +0100595 /* setup RDMA connection */
596 rc = smc_connect_rdma(smc);
597 if (rc < 0)
598 goto out;
599 else
600 rc = 0; /* success cases including fallback */
Ursula Braunac713872017-01-09 16:55:13 +0100601
602out:
603 release_sock(sk);
604out_err:
605 return rc;
606}
607
608static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
609{
Ursula Braun3163c502018-01-24 10:28:12 +0100610 struct socket *new_clcsock = NULL;
611 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100612 struct sock *new_sk;
613 int rc;
614
Ursula Braun3163c502018-01-24 10:28:12 +0100615 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100616 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100617 if (!new_sk) {
618 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100619 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100620 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100621 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100622 goto out;
623 }
624 *new_smc = smc_sk(new_sk);
625
626 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
Ursula Braun3163c502018-01-24 10:28:12 +0100627 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100628 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100629 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100630 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Brauna046d572017-01-09 16:55:16 +0100631 if (new_clcsock)
632 sock_release(new_clcsock);
633 new_sk->sk_state = SMC_CLOSED;
634 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun3163c502018-01-24 10:28:12 +0100635 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100636 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100637 *new_smc = NULL;
638 goto out;
639 }
640
641 (*new_smc)->clcsock = new_clcsock;
642out:
643 return rc;
644}
645
Ursula Brauna046d572017-01-09 16:55:16 +0100646/* add a just created sock to the accept queue of the listen sock as
647 * candidate for a following socket accept call from user space
648 */
649static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
650{
651 struct smc_sock *par = smc_sk(parent);
652
Ursula Braun51f1de72018-01-26 09:28:48 +0100653 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100654 spin_lock(&par->accept_q_lock);
655 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
656 spin_unlock(&par->accept_q_lock);
657 sk_acceptq_added(parent);
658}
659
660/* remove a socket from the accept queue of its parental listening socket */
661static void smc_accept_unlink(struct sock *sk)
662{
663 struct smc_sock *par = smc_sk(sk)->listen_smc;
664
665 spin_lock(&par->accept_q_lock);
666 list_del_init(&smc_sk(sk)->accept_q);
667 spin_unlock(&par->accept_q_lock);
668 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100669 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100670}
671
672/* remove a sock from the accept queue to bind it to a new socket created
673 * for a socket accept call from user space
674 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100675struct sock *smc_accept_dequeue(struct sock *parent,
676 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100677{
678 struct smc_sock *isk, *n;
679 struct sock *new_sk;
680
681 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
682 new_sk = (struct sock *)isk;
683
684 smc_accept_unlink(new_sk);
685 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braun127f4972018-01-26 09:28:49 +0100686 if (isk->clcsock) {
687 sock_release(isk->clcsock);
688 isk->clcsock = NULL;
689 }
Ursula Braun288c83902017-04-10 14:58:04 +0200690 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100691 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +0100692 continue;
693 }
694 if (new_sock)
695 sock_graft(new_sk, new_sock);
696 return new_sk;
697 }
698 return NULL;
699}
700
701/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +0100702void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +0100703{
704 struct smc_sock *smc = smc_sk(sk);
705
Ursula Braunb38d7322017-01-09 16:55:25 +0100706 lock_sock(sk);
707 if (!sk->sk_lingertime)
708 /* wait for peer closing */
709 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100710 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100711 smc_close_active(smc);
Ursula Braun288c83902017-04-10 14:58:04 +0200712 sock_set_flag(sk, SOCK_DEAD);
713 sk->sk_shutdown |= SHUTDOWN_MASK;
714 }
Ursula Brauna046d572017-01-09 16:55:16 +0100715 if (smc->clcsock) {
716 struct socket *tcp;
717
718 tcp = smc->clcsock;
719 smc->clcsock = NULL;
720 sock_release(tcp);
721 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100722 if (smc->use_fallback) {
Ursula Braun51f1de72018-01-26 09:28:48 +0100723 sock_put(sk); /* passive closing */
724 sk->sk_state = SMC_CLOSED;
725 } else {
726 if (sk->sk_state == SMC_CLOSED)
727 smc_conn_free(&smc->conn);
Ursula Braunb38d7322017-01-09 16:55:25 +0100728 }
729 release_sock(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100730 sk->sk_prot->unhash(sk);
731 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +0100732}
733
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100734static int smc_serv_conf_first_link(struct smc_sock *smc)
735{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200736 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100737 struct smc_link_group *lgr = smc->conn.lgr;
738 struct smc_link *link;
739 int rest;
740 int rc;
741
742 link = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun652a1e42017-07-28 13:56:17 +0200743
Karsten Graul44aa81c2018-05-15 17:04:55 +0200744 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Ursula Braun652a1e42017-07-28 13:56:17 +0200745 return SMC_CLC_DECL_INTERR;
746
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100747 /* send CONFIRM LINK request to client over the RoCE fabric */
748 rc = smc_llc_send_confirm_link(link,
749 link->smcibdev->mac[link->ibport - 1],
750 &link->smcibdev->gid[link->ibport - 1],
751 SMC_LLC_REQ);
752 if (rc < 0)
753 return SMC_CLC_DECL_TCL;
754
755 /* receive CONFIRM LINK response from client over the RoCE fabric */
756 rest = wait_for_completion_interruptible_timeout(
757 &link->llc_confirm_resp,
758 SMC_LLC_WAIT_FIRST_TIME);
759 if (rest <= 0) {
760 struct smc_clc_msg_decline dclc;
761
762 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
763 SMC_CLC_DECLINE);
Karsten Graul75d320d2018-03-01 13:51:31 +0100764 return rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100765 }
766
Karsten Graul75d320d2018-03-01 13:51:31 +0100767 if (link->llc_confirm_resp_rc)
768 return SMC_CLC_DECL_RMBE_EC;
769
Karsten Graul52bedf32018-03-01 13:51:32 +0100770 /* send ADD LINK request to client over the RoCE fabric */
771 rc = smc_llc_send_add_link(link,
772 link->smcibdev->mac[link->ibport - 1],
773 &link->smcibdev->gid[link->ibport - 1],
774 SMC_LLC_REQ);
775 if (rc < 0)
776 return SMC_CLC_DECL_TCL;
777
778 /* receive ADD LINK response from client over the RoCE fabric */
779 rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
780 SMC_LLC_WAIT_TIME);
781 if (rest <= 0) {
782 struct smc_clc_msg_decline dclc;
783
784 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
785 SMC_CLC_DECLINE);
786 return rc;
787 }
788
Karsten Graul877ae5b2018-05-02 16:56:44 +0200789 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100790
Karsten Graul75d320d2018-03-01 13:51:31 +0100791 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100792}
793
Ursula Brauna046d572017-01-09 16:55:16 +0100794/* setup for RDMA connection of server */
795static void smc_listen_work(struct work_struct *work)
796{
797 struct smc_sock *new_smc = container_of(work, struct smc_sock,
798 smc_listen_work);
Ursula Braune7b7a642017-12-07 13:38:49 +0100799 struct smc_clc_msg_proposal_prefix *pclc_prfx;
Ursula Brauna046d572017-01-09 16:55:16 +0100800 struct socket *newclcsock = new_smc->clcsock;
801 struct smc_sock *lsmc = new_smc->listen_smc;
802 struct smc_clc_msg_accept_confirm cclc;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100803 int local_contact = SMC_REUSE_CONTACT;
Ursula Brauna046d572017-01-09 16:55:16 +0100804 struct sock *newsmcsk = &new_smc->sk;
Ursula Braune7b7a642017-12-07 13:38:49 +0100805 struct smc_clc_msg_proposal *pclc;
Ursula Brauna046d572017-01-09 16:55:16 +0100806 struct smc_ib_device *smcibdev;
Ursula Braune7b7a642017-12-07 13:38:49 +0100807 u8 buf[SMC_CLC_MAX_LEN];
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100808 struct smc_link *link;
Ursula Brauna046d572017-01-09 16:55:16 +0100809 int reason_code = 0;
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100810 int rc = 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100811 u8 ibport;
812
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200813 if (new_smc->use_fallback)
814 goto out_connected;
815
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200816 /* check if peer is smc capable */
817 if (!tcp_sk(newclcsock->sk)->syn_smc) {
818 new_smc->use_fallback = true;
819 goto out_connected;
820 }
821
Ursula Brauna046d572017-01-09 16:55:16 +0100822 /* do inband token exchange -
823 *wait for and receive SMC Proposal CLC message
824 */
Ursula Braune7b7a642017-12-07 13:38:49 +0100825 reason_code = smc_clc_wait_msg(new_smc, &buf, sizeof(buf),
Ursula Brauna046d572017-01-09 16:55:16 +0100826 SMC_CLC_PROPOSAL);
827 if (reason_code < 0)
828 goto out_err;
829 if (reason_code > 0)
830 goto decline_rdma;
831
832 /* IPSec connections opt out of SMC-R optimizations */
833 if (using_ipsec(new_smc)) {
834 reason_code = SMC_CLC_DECL_IPSEC;
835 goto decline_rdma;
836 }
837
838 /* PNET table look up: search active ib_device and port
839 * within same PNETID that also contains the ethernet device
840 * used for the internal TCP socket
841 */
842 smc_pnet_find_roce_resource(newclcsock->sk, &smcibdev, &ibport);
843 if (!smcibdev) {
844 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
845 goto decline_rdma;
846 }
847
Ursula Braune7b7a642017-12-07 13:38:49 +0100848 pclc = (struct smc_clc_msg_proposal *)&buf;
849 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
Karsten Graulc246d942018-03-16 15:06:39 +0100850
851 rc = smc_clc_prfx_match(newclcsock, pclc_prfx);
852 if (rc) {
Ursula Brauna046d572017-01-09 16:55:16 +0100853 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
854 goto decline_rdma;
855 }
856
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100857 /* allocate connection / link group */
858 mutex_lock(&smc_create_lgr_pending);
Karsten Graulbe6d4672018-03-01 13:51:28 +0100859 local_contact = smc_conn_create(new_smc, smcibdev, ibport, &pclc->lcl,
860 0);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100861 if (local_contact < 0) {
862 rc = local_contact;
863 if (rc == -ENOMEM)
864 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
Ursula Braun145686b2017-10-25 11:01:44 +0200865 goto decline_rdma_unlock;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100866 }
867 link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
Ursula Brauna046d572017-01-09 16:55:16 +0100868
Ursula Braun3e034722017-07-28 13:56:20 +0200869 /* create send buffer and rmb */
870 rc = smc_buf_create(new_smc);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100871 if (rc) {
872 reason_code = SMC_CLC_DECL_MEM;
Ursula Braun145686b2017-10-25 11:01:44 +0200873 goto decline_rdma_unlock;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100874 }
Ursula Brauna046d572017-01-09 16:55:16 +0100875
Ursula Braun46c28db2017-04-10 14:58:01 +0200876 smc_close_init(new_smc);
877 smc_rx_init(new_smc);
878
Ursula Braun652a1e42017-07-28 13:56:17 +0200879 if (local_contact != SMC_FIRST_CONTACT) {
Karsten Graule63a5f82018-05-03 17:57:37 +0200880 if (!new_smc->conn.rmb_desc->reused) {
Karsten Graul44aa81c2018-05-15 17:04:55 +0200881 if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true)) {
Ursula Braun652a1e42017-07-28 13:56:17 +0200882 reason_code = SMC_CLC_DECL_INTERR;
Ursula Braun145686b2017-10-25 11:01:44 +0200883 goto decline_rdma_unlock;
Ursula Braun652a1e42017-07-28 13:56:17 +0200884 }
885 }
886 }
Ursula Braun10428dd2017-07-28 13:56:22 +0200887 smc_rmb_sync_sg_for_device(&new_smc->conn);
Ursula Braun652a1e42017-07-28 13:56:17 +0200888
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100889 rc = smc_clc_send_accept(new_smc, local_contact);
Ursula Brauna046d572017-01-09 16:55:16 +0100890 if (rc)
Ursula Braun145686b2017-10-25 11:01:44 +0200891 goto out_err_unlock;
Ursula Brauna046d572017-01-09 16:55:16 +0100892
893 /* receive SMC Confirm CLC message */
894 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
895 SMC_CLC_CONFIRM);
896 if (reason_code < 0)
Ursula Braun145686b2017-10-25 11:01:44 +0200897 goto out_err_unlock;
Ursula Brauna046d572017-01-09 16:55:16 +0100898 if (reason_code > 0)
Ursula Braun145686b2017-10-25 11:01:44 +0200899 goto decline_rdma_unlock;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100900 smc_conn_save_peer_info(new_smc, &cclc);
901 if (local_contact == SMC_FIRST_CONTACT)
902 smc_link_save_peer_info(link, &cclc);
Ursula Brauna046d572017-01-09 16:55:16 +0100903
Ursula Braunbd4ad572017-01-09 16:55:20 +0100904 rc = smc_rmb_rtoken_handling(&new_smc->conn, &cclc);
905 if (rc) {
906 reason_code = SMC_CLC_DECL_INTERR;
Ursula Braun145686b2017-10-25 11:01:44 +0200907 goto decline_rdma_unlock;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100908 }
909
Ursula Braunbd4ad572017-01-09 16:55:20 +0100910 if (local_contact == SMC_FIRST_CONTACT) {
911 rc = smc_ib_ready_link(link);
912 if (rc) {
913 reason_code = SMC_CLC_DECL_INTERR;
Ursula Braun145686b2017-10-25 11:01:44 +0200914 goto decline_rdma_unlock;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100915 }
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100916 /* QP confirmation over RoCE fabric */
917 reason_code = smc_serv_conf_first_link(new_smc);
Ursula Braun0c9f1512017-12-07 13:38:45 +0100918 if (reason_code < 0)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100919 /* peer is not aware of a problem */
Ursula Braun145686b2017-10-25 11:01:44 +0200920 goto out_err_unlock;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100921 if (reason_code > 0)
Ursula Braun145686b2017-10-25 11:01:44 +0200922 goto decline_rdma_unlock;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100923 }
Ursula Brauna046d572017-01-09 16:55:16 +0100924
Ursula Braune6727f32017-01-09 16:55:23 +0100925 smc_tx_init(new_smc);
Ursula Braun145686b2017-10-25 11:01:44 +0200926 mutex_unlock(&smc_create_lgr_pending);
Ursula Braune6727f32017-01-09 16:55:23 +0100927
Ursula Brauna046d572017-01-09 16:55:16 +0100928out_connected:
929 sk_refcnt_debug_inc(newsmcsk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100930 if (newsmcsk->sk_state == SMC_INIT)
931 newsmcsk->sk_state = SMC_ACTIVE;
Ursula Brauna046d572017-01-09 16:55:16 +0100932enqueue:
Ursula Braunb38d7322017-01-09 16:55:25 +0100933 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +0100934 if (lsmc->sk.sk_state == SMC_LISTEN) {
935 smc_accept_enqueue(&lsmc->sk, newsmcsk);
936 } else { /* no longer listening */
937 smc_close_non_accepted(newsmcsk);
938 }
939 release_sock(&lsmc->sk);
940
941 /* Wake up accept */
942 lsmc->sk.sk_data_ready(&lsmc->sk);
943 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
944 return;
945
Ursula Braun145686b2017-10-25 11:01:44 +0200946decline_rdma_unlock:
Ursula Braun610db662018-01-25 11:15:34 +0100947 if (local_contact == SMC_FIRST_CONTACT)
948 smc_lgr_forget(new_smc->conn.lgr);
Ursula Braun145686b2017-10-25 11:01:44 +0200949 mutex_unlock(&smc_create_lgr_pending);
Ursula Brauna046d572017-01-09 16:55:16 +0100950decline_rdma:
951 /* RDMA setup failed, switch back to TCP */
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100952 smc_conn_free(&new_smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100953 new_smc->use_fallback = true;
954 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
Ursula Braun0c9f1512017-12-07 13:38:45 +0100955 if (smc_clc_send_decline(new_smc, reason_code) < 0)
Ursula Brauna046d572017-01-09 16:55:16 +0100956 goto out_err;
957 }
958 goto out_connected;
959
Ursula Braun145686b2017-10-25 11:01:44 +0200960out_err_unlock:
Ursula Braun610db662018-01-25 11:15:34 +0100961 if (local_contact == SMC_FIRST_CONTACT)
962 smc_lgr_forget(new_smc->conn.lgr);
Ursula Braun145686b2017-10-25 11:01:44 +0200963 mutex_unlock(&smc_create_lgr_pending);
Ursula Brauna046d572017-01-09 16:55:16 +0100964out_err:
Ursula Braun51f1de72018-01-26 09:28:48 +0100965 if (newsmcsk->sk_state == SMC_INIT)
966 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +0100967 newsmcsk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100968 smc_conn_free(&new_smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100969 goto enqueue; /* queue new sock with sk_err set */
970}
971
972static void smc_tcp_listen_work(struct work_struct *work)
973{
974 struct smc_sock *lsmc = container_of(work, struct smc_sock,
975 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +0100976 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +0100977 struct smc_sock *new_smc;
978 int rc = 0;
979
Ursula Braun3163c502018-01-24 10:28:12 +0100980 lock_sock(lsk);
981 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +0100982 rc = smc_clcsock_accept(lsmc, &new_smc);
983 if (rc)
984 goto out;
985 if (!new_smc)
986 continue;
987
988 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200989 new_smc->use_fallback = lsmc->use_fallback;
Ursula Braun3163c502018-01-24 10:28:12 +0100990 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +0100991 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
992 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braun51f1de72018-01-26 09:28:48 +0100993 sock_hold(&new_smc->sk); /* sock_put in passive closing */
994 if (!schedule_work(&new_smc->smc_listen_work))
995 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100996 }
997
998out:
Ursula Braun3163c502018-01-24 10:28:12 +0100999 release_sock(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001000 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +01001001}
1002
Ursula Braunac713872017-01-09 16:55:13 +01001003static int smc_listen(struct socket *sock, int backlog)
1004{
1005 struct sock *sk = sock->sk;
1006 struct smc_sock *smc;
1007 int rc;
1008
1009 smc = smc_sk(sk);
1010 lock_sock(sk);
1011
1012 rc = -EINVAL;
1013 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
1014 goto out;
1015
1016 rc = 0;
1017 if (sk->sk_state == SMC_LISTEN) {
1018 sk->sk_max_ack_backlog = backlog;
1019 goto out;
1020 }
1021 /* some socket options are handled in core, so we could not apply
1022 * them to the clc socket -- copy smc socket options to clc socket
1023 */
1024 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001025 if (!smc->use_fallback)
1026 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001027
1028 rc = kernel_listen(smc->clcsock, backlog);
1029 if (rc)
1030 goto out;
1031 sk->sk_max_ack_backlog = backlog;
1032 sk->sk_ack_backlog = 0;
1033 sk->sk_state = SMC_LISTEN;
Ursula Brauna046d572017-01-09 16:55:16 +01001034 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun51f1de72018-01-26 09:28:48 +01001035 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1036 if (!schedule_work(&smc->tcp_listen_work))
1037 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001038
1039out:
1040 release_sock(sk);
1041 return rc;
1042}
1043
1044static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001045 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001046{
Ursula Brauna046d572017-01-09 16:55:16 +01001047 struct sock *sk = sock->sk, *nsk;
1048 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001049 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001050 long timeo;
1051 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001052
1053 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001054 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001055 lock_sock(sk);
1056
1057 if (lsmc->sk.sk_state != SMC_LISTEN) {
1058 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001059 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001060 goto out;
1061 }
1062
Ursula Brauna046d572017-01-09 16:55:16 +01001063 /* Wait for an incoming connection */
1064 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1065 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1066 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1067 set_current_state(TASK_INTERRUPTIBLE);
1068 if (!timeo) {
1069 rc = -EAGAIN;
1070 break;
1071 }
1072 release_sock(sk);
1073 timeo = schedule_timeout(timeo);
1074 /* wakeup by sk_data_ready in smc_listen_work() */
1075 sched_annotate_sleep();
1076 lock_sock(sk);
1077 if (signal_pending(current)) {
1078 rc = sock_intr_errno(timeo);
1079 break;
1080 }
1081 }
1082 set_current_state(TASK_RUNNING);
1083 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001084
Ursula Brauna046d572017-01-09 16:55:16 +01001085 if (!rc)
1086 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001087 release_sock(sk);
1088 if (rc)
1089 goto out;
1090
1091 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1092 /* wait till data arrives on the socket */
1093 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1094 MSEC_PER_SEC);
1095 if (smc_sk(nsk)->use_fallback) {
1096 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1097
1098 lock_sock(clcsk);
1099 if (skb_queue_empty(&clcsk->sk_receive_queue))
1100 sk_wait_data(clcsk, &timeo, NULL);
1101 release_sock(clcsk);
1102 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1103 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001104 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001105 release_sock(nsk);
1106 }
1107 }
Ursula Braunac713872017-01-09 16:55:13 +01001108
1109out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001110 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001111 return rc;
1112}
1113
1114static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001115 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001116{
1117 struct smc_sock *smc;
1118
Ursula Braunb38d7322017-01-09 16:55:25 +01001119 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1120 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001121 return -ENOTCONN;
1122
1123 smc = smc_sk(sock->sk);
1124
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001125 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001126}
1127
1128static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1129{
1130 struct sock *sk = sock->sk;
1131 struct smc_sock *smc;
1132 int rc = -EPIPE;
1133
1134 smc = smc_sk(sk);
1135 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001136 if ((sk->sk_state != SMC_ACTIVE) &&
1137 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1138 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001139 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001140
1141 if (msg->msg_flags & MSG_FASTOPEN) {
1142 if (sk->sk_state == SMC_INIT) {
1143 smc->use_fallback = true;
1144 } else {
1145 rc = -EINVAL;
1146 goto out;
1147 }
1148 }
1149
Ursula Braunac713872017-01-09 16:55:13 +01001150 if (smc->use_fallback)
1151 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1152 else
Ursula Braune6727f32017-01-09 16:55:23 +01001153 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001154out:
1155 release_sock(sk);
1156 return rc;
1157}
1158
1159static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1160 int flags)
1161{
1162 struct sock *sk = sock->sk;
1163 struct smc_sock *smc;
1164 int rc = -ENOTCONN;
1165
1166 smc = smc_sk(sk);
1167 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001168 if ((sk->sk_state == SMC_INIT) ||
1169 (sk->sk_state == SMC_LISTEN) ||
1170 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001171 goto out;
1172
Ursula Braunb38d7322017-01-09 16:55:25 +01001173 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1174 rc = 0;
1175 goto out;
1176 }
1177
Stefan Raspl9014db22018-05-03 18:12:39 +02001178 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001179 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001180 } else {
1181 msg->msg_namelen = 0;
1182 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1183 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001184
Ursula Braunac713872017-01-09 16:55:13 +01001185out:
1186 release_sock(sk);
1187 return rc;
1188}
1189
Al Viroade994f2017-07-03 00:01:49 -04001190static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001191{
Ursula Braun8dce2782018-01-26 09:28:47 +01001192 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001193 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001194
Ursula Braun8dce2782018-01-26 09:28:47 +01001195 spin_lock(&isk->accept_q_lock);
1196 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001197 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001198 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001199
Ursula Braun8dce2782018-01-26 09:28:47 +01001200 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001201}
1202
Al Viroade994f2017-07-03 00:01:49 -04001203static __poll_t smc_poll(struct file *file, struct socket *sock,
Ursula Braunac713872017-01-09 16:55:13 +01001204 poll_table *wait)
1205{
1206 struct sock *sk = sock->sk;
Al Viroe6c8adc2017-07-03 22:25:56 -04001207 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001208 struct smc_sock *smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001209 int rc;
Ursula Braunac713872017-01-09 16:55:13 +01001210
Ursula Braun8dce2782018-01-26 09:28:47 +01001211 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001212 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001213
Ursula Braunac713872017-01-09 16:55:13 +01001214 smc = smc_sk(sock->sk);
Ursula Braun8dce2782018-01-26 09:28:47 +01001215 sock_hold(sk);
1216 lock_sock(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001217 if ((sk->sk_state == SMC_INIT) || smc->use_fallback) {
1218 /* delegate to CLC child sock */
Ursula Braun8dce2782018-01-26 09:28:47 +01001219 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001220 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braunac713872017-01-09 16:55:13 +01001221 lock_sock(sk);
Ursula Braun784813a2018-05-02 16:53:56 +02001222 sk->sk_err = smc->clcsock->sk->sk_err;
1223 if (sk->sk_err) {
1224 mask |= EPOLLERR;
1225 } else {
1226 /* if non-blocking connect finished ... */
1227 if (sk->sk_state == SMC_INIT &&
1228 mask & EPOLLOUT &&
1229 smc->clcsock->sk->sk_state != TCP_CLOSE) {
Ursula Brauna046d572017-01-09 16:55:16 +01001230 rc = smc_connect_rdma(smc);
1231 if (rc < 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001232 mask |= EPOLLERR;
Ursula Braun8dce2782018-01-26 09:28:47 +01001233 /* success cases including fallback */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001234 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Brauna046d572017-01-09 16:55:16 +01001235 }
Ursula Braunac713872017-01-09 16:55:13 +01001236 }
Ursula Braunac713872017-01-09 16:55:13 +01001237 } else {
Ursula Braun8dce2782018-01-26 09:28:47 +01001238 if (sk->sk_state != SMC_CLOSED) {
1239 release_sock(sk);
1240 sock_poll_wait(file, sk_sleep(sk), wait);
1241 lock_sock(sk);
1242 }
Ursula Brauna046d572017-01-09 16:55:16 +01001243 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001244 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001245 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1246 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001247 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001248 if (sk->sk_state == SMC_LISTEN) {
1249 /* woken up by sk_data_ready in smc_listen_work() */
1250 mask = smc_accept_poll(sk);
1251 } else {
1252 if (atomic_read(&smc->conn.sndbuf_space) ||
1253 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001254 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001255 } else {
1256 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1257 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1258 }
1259 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001260 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001261 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001262 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001263 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001264 mask |= EPOLLIN;
Ursula Braun8dce2782018-01-26 09:28:47 +01001265 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001266
Ursula Braunac713872017-01-09 16:55:13 +01001267 }
Ursula Braun8dce2782018-01-26 09:28:47 +01001268 release_sock(sk);
1269 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001270
1271 return mask;
1272}
1273
1274static int smc_shutdown(struct socket *sock, int how)
1275{
1276 struct sock *sk = sock->sk;
1277 struct smc_sock *smc;
1278 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001279 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001280
1281 smc = smc_sk(sk);
1282
1283 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001284 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001285
1286 lock_sock(sk);
1287
1288 rc = -ENOTCONN;
Ursula Braunb38d7322017-01-09 16:55:25 +01001289 if ((sk->sk_state != SMC_LISTEN) &&
1290 (sk->sk_state != SMC_ACTIVE) &&
1291 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1292 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1293 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1294 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1295 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001296 goto out;
1297 if (smc->use_fallback) {
1298 rc = kernel_sock_shutdown(smc->clcsock, how);
1299 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1300 if (sk->sk_shutdown == SHUTDOWN_MASK)
1301 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001302 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001303 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001304 switch (how) {
1305 case SHUT_RDWR: /* shutdown in both directions */
1306 rc = smc_close_active(smc);
1307 break;
1308 case SHUT_WR:
1309 rc = smc_close_shutdown_write(smc);
1310 break;
1311 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001312 rc = 0;
1313 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001314 break;
1315 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001316 if (smc->clcsock)
1317 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001318 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1319 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001320
1321out:
1322 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001323 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001324}
1325
1326static int smc_setsockopt(struct socket *sock, int level, int optname,
1327 char __user *optval, unsigned int optlen)
1328{
1329 struct sock *sk = sock->sk;
1330 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001331 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001332
1333 smc = smc_sk(sk);
1334
1335 /* generic setsockopts reaching us here always apply to the
1336 * CLC socket
1337 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001338 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1339 optval, optlen);
1340 if (smc->clcsock->sk->sk_err) {
1341 sk->sk_err = smc->clcsock->sk->sk_err;
1342 sk->sk_error_report(sk);
1343 }
1344 if (rc)
1345 return rc;
1346
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001347 if (optlen < sizeof(int))
1348 return rc;
1349 get_user(val, (int __user *)optval);
1350
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001351 lock_sock(sk);
1352 switch (optname) {
1353 case TCP_ULP:
1354 case TCP_FASTOPEN:
1355 case TCP_FASTOPEN_CONNECT:
1356 case TCP_FASTOPEN_KEY:
1357 case TCP_FASTOPEN_NO_COOKIE:
1358 /* option not supported by SMC */
1359 if (sk->sk_state == SMC_INIT) {
1360 smc->use_fallback = true;
1361 } else {
1362 if (!smc->use_fallback)
1363 rc = -EINVAL;
1364 }
1365 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001366 case TCP_NODELAY:
1367 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001368 if (val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001369 mod_delayed_work(system_wq, &smc->conn.tx_work,
1370 0);
1371 }
1372 break;
1373 case TCP_CORK:
1374 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001375 if (!val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001376 mod_delayed_work(system_wq, &smc->conn.tx_work,
1377 0);
1378 }
1379 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001380 case TCP_DEFER_ACCEPT:
1381 smc->sockopt_defer_accept = val;
1382 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001383 default:
1384 break;
1385 }
1386 release_sock(sk);
1387
1388 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001389}
1390
1391static int smc_getsockopt(struct socket *sock, int level, int optname,
1392 char __user *optval, int __user *optlen)
1393{
1394 struct smc_sock *smc;
1395
1396 smc = smc_sk(sock->sk);
1397 /* socket options apply to the CLC socket */
1398 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1399 optval, optlen);
1400}
1401
1402static int smc_ioctl(struct socket *sock, unsigned int cmd,
1403 unsigned long arg)
1404{
1405 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001406 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001407
1408 smc = smc_sk(sock->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001409 if (smc->use_fallback) {
1410 if (!smc->clcsock)
1411 return -EBADF;
Ursula Braunac713872017-01-09 16:55:13 +01001412 return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
Ursula Braun9b67e262018-05-02 16:56:46 +02001413 }
1414 switch (cmd) {
1415 case SIOCINQ: /* same as FIONREAD */
1416 if (smc->sk.sk_state == SMC_LISTEN)
1417 return -EINVAL;
1418 answ = atomic_read(&smc->conn.bytes_to_rcv);
1419 break;
1420 case SIOCOUTQ:
1421 /* output queue size (not send + not acked) */
1422 if (smc->sk.sk_state == SMC_LISTEN)
1423 return -EINVAL;
1424 answ = smc->conn.sndbuf_size -
1425 atomic_read(&smc->conn.sndbuf_space);
1426 break;
1427 case SIOCOUTQNSD:
1428 /* output queue size (not send only) */
1429 if (smc->sk.sk_state == SMC_LISTEN)
1430 return -EINVAL;
1431 answ = smc_tx_prepared_sends(&smc->conn);
1432 break;
1433 default:
1434 return -ENOIOCTLCMD;
1435 }
1436
1437 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001438}
1439
1440static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1441 int offset, size_t size, int flags)
1442{
1443 struct sock *sk = sock->sk;
1444 struct smc_sock *smc;
1445 int rc = -EPIPE;
1446
1447 smc = smc_sk(sk);
1448 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001449 if (sk->sk_state != SMC_ACTIVE) {
1450 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001451 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001452 }
1453 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001454 if (smc->use_fallback)
1455 rc = kernel_sendpage(smc->clcsock, page, offset,
1456 size, flags);
1457 else
1458 rc = sock_no_sendpage(sock, page, offset, size, flags);
1459
1460out:
Ursula Braunac713872017-01-09 16:55:13 +01001461 return rc;
1462}
1463
Stefan Raspl9014db22018-05-03 18:12:39 +02001464/* Map the affected portions of the rmbe into an spd, note the number of bytes
1465 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1466 * updates till whenever a respective page has been fully processed.
1467 * Note that subsequent recv() calls have to wait till all splice() processing
1468 * completed.
1469 */
Ursula Braunac713872017-01-09 16:55:13 +01001470static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1471 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001472 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001473{
1474 struct sock *sk = sock->sk;
1475 struct smc_sock *smc;
1476 int rc = -ENOTCONN;
1477
1478 smc = smc_sk(sk);
1479 lock_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001480
1481 if (sk->sk_state == SMC_INIT ||
1482 sk->sk_state == SMC_LISTEN ||
1483 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001484 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001485
1486 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1487 rc = 0;
1488 goto out;
1489 }
1490
Ursula Braunac713872017-01-09 16:55:13 +01001491 if (smc->use_fallback) {
1492 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1493 pipe, len, flags);
1494 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001495 if (*ppos) {
1496 rc = -ESPIPE;
1497 goto out;
1498 }
1499 if (flags & SPLICE_F_NONBLOCK)
1500 flags = MSG_DONTWAIT;
1501 else
1502 flags = 0;
1503 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001504 }
1505out:
1506 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001507
Ursula Braunac713872017-01-09 16:55:13 +01001508 return rc;
1509}
1510
1511/* must look like tcp */
1512static const struct proto_ops smc_sock_ops = {
1513 .family = PF_SMC,
1514 .owner = THIS_MODULE,
1515 .release = smc_release,
1516 .bind = smc_bind,
1517 .connect = smc_connect,
1518 .socketpair = sock_no_socketpair,
1519 .accept = smc_accept,
1520 .getname = smc_getname,
1521 .poll = smc_poll,
1522 .ioctl = smc_ioctl,
1523 .listen = smc_listen,
1524 .shutdown = smc_shutdown,
1525 .setsockopt = smc_setsockopt,
1526 .getsockopt = smc_getsockopt,
1527 .sendmsg = smc_sendmsg,
1528 .recvmsg = smc_recvmsg,
1529 .mmap = sock_no_mmap,
1530 .sendpage = smc_sendpage,
1531 .splice_read = smc_splice_read,
1532};
1533
1534static int smc_create(struct net *net, struct socket *sock, int protocol,
1535 int kern)
1536{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001537 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001538 struct smc_sock *smc;
1539 struct sock *sk;
1540 int rc;
1541
1542 rc = -ESOCKTNOSUPPORT;
1543 if (sock->type != SOCK_STREAM)
1544 goto out;
1545
1546 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001547 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01001548 goto out;
1549
1550 rc = -ENOBUFS;
1551 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001552 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001553 if (!sk)
1554 goto out;
1555
1556 /* create internal TCP socket for CLC handshake and fallback */
1557 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001558 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graulaaa4d332018-03-16 15:06:41 +01001559 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1560 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01001561 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01001562 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01001563 goto out;
1564 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01001565 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1566 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01001567
1568out:
1569 return rc;
1570}
1571
1572static const struct net_proto_family smc_sock_family_ops = {
1573 .family = PF_SMC,
1574 .owner = THIS_MODULE,
1575 .create = smc_create,
1576};
1577
1578static int __init smc_init(void)
1579{
1580 int rc;
1581
Thomas Richter6812baa2017-01-09 16:55:15 +01001582 rc = smc_pnet_init();
1583 if (rc)
1584 return rc;
1585
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001586 rc = smc_llc_init();
1587 if (rc) {
1588 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1589 goto out_pnet;
1590 }
1591
Ursula Braun5f083182017-01-09 16:55:22 +01001592 rc = smc_cdc_init();
1593 if (rc) {
1594 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1595 goto out_pnet;
1596 }
1597
Ursula Braunac713872017-01-09 16:55:13 +01001598 rc = proto_register(&smc_proto, 1);
1599 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01001600 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Thomas Richter6812baa2017-01-09 16:55:15 +01001601 goto out_pnet;
Ursula Braunac713872017-01-09 16:55:13 +01001602 }
1603
Karsten Graulaaa4d332018-03-16 15:06:41 +01001604 rc = proto_register(&smc_proto6, 1);
1605 if (rc) {
1606 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
1607 goto out_proto;
1608 }
1609
Ursula Braunac713872017-01-09 16:55:13 +01001610 rc = sock_register(&smc_sock_family_ops);
1611 if (rc) {
1612 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001613 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01001614 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01001615 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001616 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01001617
Ursula Brauna4cf0442017-01-09 16:55:14 +01001618 rc = smc_ib_register_client();
1619 if (rc) {
1620 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1621 goto out_sock;
1622 }
1623
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001624 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01001625 return 0;
1626
Ursula Brauna4cf0442017-01-09 16:55:14 +01001627out_sock:
1628 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001629out_proto6:
1630 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01001631out_proto:
1632 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001633out_pnet:
1634 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001635 return rc;
1636}
1637
1638static void __exit smc_exit(void)
1639{
Ursula Braun0cfdd8f2017-01-09 16:55:17 +01001640 struct smc_link_group *lgr, *lg;
1641 LIST_HEAD(lgr_freeing_list);
1642
1643 spin_lock_bh(&smc_lgr_list.lock);
1644 if (!list_empty(&smc_lgr_list.list))
1645 list_splice_init(&smc_lgr_list.list, &lgr_freeing_list);
1646 spin_unlock_bh(&smc_lgr_list.lock);
1647 list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) {
1648 list_del_init(&lgr->list);
Karsten Graul3cf52eb2018-05-15 17:05:01 +02001649 smc_llc_link_inactive(&lgr->lnk[SMC_SINGLE_LINK]);
Ursula Braun268ffcc2018-03-14 11:01:01 +01001650 cancel_delayed_work_sync(&lgr->free_work);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +01001651 smc_lgr_free(lgr); /* free link group */
1652 }
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001653 static_branch_disable(&tcp_have_smc);
Ursula Brauna4cf0442017-01-09 16:55:14 +01001654 smc_ib_unregister_client();
Ursula Braunac713872017-01-09 16:55:13 +01001655 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001656 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01001657 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001658 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001659}
1660
1661module_init(smc_init);
1662module_exit(smc_exit);
1663
1664MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
1665MODULE_DESCRIPTION("smc socket address family");
1666MODULE_LICENSE("GPL");
1667MODULE_ALIAS_NETPROTO(PF_SMC);