blob: e017b6a4452b30d58fca1a20a56278bcdae1799a [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
Ursula Brauna046d572017-01-09 16:55:16 +010011 *
Karsten Graulaaa4d332018-03-16 15:06:41 +010012 * Copyright IBM Corp. 2016, 2018
Ursula Braunac713872017-01-09 16:55:13 +010013 *
14 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
15 * based on prototype from Frank Blaschka
16 */
17
18#define KMSG_COMPONENT "smc"
19#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
20
21#include <linux/module.h>
22#include <linux/socket.h>
Ursula Brauna046d572017-01-09 16:55:16 +010023#include <linux/workqueue.h>
Ursula Braun5f083182017-01-09 16:55:22 +010024#include <linux/in.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010025#include <linux/sched/signal.h>
26
Ursula Braunac713872017-01-09 16:55:13 +010027#include <net/sock.h>
Ursula Brauna046d572017-01-09 16:55:16 +010028#include <net/tcp.h>
Ursula Braunf16a7dd2017-01-09 16:55:26 +010029#include <net/smc.h>
Ursula Braun9b67e262018-05-02 16:56:46 +020030#include <asm/ioctls.h>
Ursula Braunac713872017-01-09 16:55:13 +010031
32#include "smc.h"
Ursula Brauna046d572017-01-09 16:55:16 +010033#include "smc_clc.h"
Ursula Braun9bf9abe2017-01-09 16:55:21 +010034#include "smc_llc.h"
Ursula Braun5f083182017-01-09 16:55:22 +010035#include "smc_cdc.h"
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010036#include "smc_core.h"
Ursula Brauna4cf0442017-01-09 16:55:14 +010037#include "smc_ib.h"
Thomas Richter6812baa2017-01-09 16:55:15 +010038#include "smc_pnet.h"
Ursula Braune6727f32017-01-09 16:55:23 +010039#include "smc_tx.h"
Ursula Braun952310c2017-01-09 16:55:24 +010040#include "smc_rx.h"
Ursula Braunb38d7322017-01-09 16:55:25 +010041#include "smc_close.h"
Ursula Braunac713872017-01-09 16:55:13 +010042
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010043static DEFINE_MUTEX(smc_create_lgr_pending); /* serialize link group
44 * creation
45 */
46
Ursula Brauna046d572017-01-09 16:55:16 +010047static void smc_tcp_listen_work(struct work_struct *);
Ursula Braun24ac3a02018-06-27 17:59:50 +020048static void smc_connect_work(struct work_struct *);
Ursula Brauna046d572017-01-09 16:55:16 +010049
Ursula Braunac713872017-01-09 16:55:13 +010050static void smc_set_keepalive(struct sock *sk, int val)
51{
52 struct smc_sock *smc = smc_sk(sk);
53
54 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
55}
56
Ursula Braunf16a7dd2017-01-09 16:55:26 +010057static struct smc_hashinfo smc_v4_hashinfo = {
58 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
59};
60
Karsten Graulaaa4d332018-03-16 15:06:41 +010061static struct smc_hashinfo smc_v6_hashinfo = {
62 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
63};
64
Ursula Braunf16a7dd2017-01-09 16:55:26 +010065int smc_hash_sk(struct sock *sk)
66{
67 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
68 struct hlist_head *head;
69
70 head = &h->ht;
71
72 write_lock_bh(&h->lock);
73 sk_add_node(sk, head);
74 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
75 write_unlock_bh(&h->lock);
76
77 return 0;
78}
79EXPORT_SYMBOL_GPL(smc_hash_sk);
80
81void smc_unhash_sk(struct sock *sk)
82{
83 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
84
85 write_lock_bh(&h->lock);
86 if (sk_del_node_init(sk))
87 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
88 write_unlock_bh(&h->lock);
89}
90EXPORT_SYMBOL_GPL(smc_unhash_sk);
91
92struct proto smc_proto = {
Ursula Braunac713872017-01-09 16:55:13 +010093 .name = "SMC",
94 .owner = THIS_MODULE,
95 .keepalive = smc_set_keepalive,
Ursula Braunf16a7dd2017-01-09 16:55:26 +010096 .hash = smc_hash_sk,
97 .unhash = smc_unhash_sk,
Ursula Braunac713872017-01-09 16:55:13 +010098 .obj_size = sizeof(struct smc_sock),
Ursula Braunf16a7dd2017-01-09 16:55:26 +010099 .h.smc_hash = &smc_v4_hashinfo,
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800100 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Ursula Braunac713872017-01-09 16:55:13 +0100101};
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100102EXPORT_SYMBOL_GPL(smc_proto);
Ursula Braunac713872017-01-09 16:55:13 +0100103
Karsten Graulaaa4d332018-03-16 15:06:41 +0100104struct proto smc_proto6 = {
105 .name = "SMC6",
106 .owner = THIS_MODULE,
107 .keepalive = smc_set_keepalive,
108 .hash = smc_hash_sk,
109 .unhash = smc_unhash_sk,
110 .obj_size = sizeof(struct smc_sock),
111 .h.smc_hash = &smc_v6_hashinfo,
112 .slab_flags = SLAB_TYPESAFE_BY_RCU,
113};
114EXPORT_SYMBOL_GPL(smc_proto6);
115
Ursula Braunac713872017-01-09 16:55:13 +0100116static int smc_release(struct socket *sock)
117{
118 struct sock *sk = sock->sk;
119 struct smc_sock *smc;
Ursula Braunb38d7322017-01-09 16:55:25 +0100120 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +0100121
122 if (!sk)
123 goto out;
124
125 smc = smc_sk(sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200126
127 /* cleanup for a dangling non-blocking connect */
128 flush_work(&smc->connect_work);
129 kfree(smc->connect_info);
130 smc->connect_info = NULL;
131
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);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200196 INIT_WORK(&smc->connect_work, smc_connect_work);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700197 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
Ursula Brauna046d572017-01-09 16:55:16 +0100198 INIT_LIST_HEAD(&smc->accept_q);
199 spin_lock_init(&smc->accept_q_lock);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700200 spin_lock_init(&smc->conn.send_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100201 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100202 sk_refcnt_debug_inc(sk);
Ursula Braunac713872017-01-09 16:55:13 +0100203
204 return sk;
205}
206
207static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
208 int addr_len)
209{
210 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
211 struct sock *sk = sock->sk;
212 struct smc_sock *smc;
213 int rc;
214
215 smc = smc_sk(sk);
216
217 /* replicate tests from inet_bind(), to be safe wrt. future changes */
218 rc = -EINVAL;
219 if (addr_len < sizeof(struct sockaddr_in))
220 goto out;
221
222 rc = -EAFNOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100223 if (addr->sin_family != AF_INET &&
224 addr->sin_family != AF_INET6 &&
225 addr->sin_family != AF_UNSPEC)
226 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100227 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
Karsten Graulaaa4d332018-03-16 15:06:41 +0100228 if (addr->sin_family == AF_UNSPEC &&
229 addr->sin_addr.s_addr != htonl(INADDR_ANY))
Ursula Braunac713872017-01-09 16:55:13 +0100230 goto out;
231
232 lock_sock(sk);
233
234 /* Check if socket is already active */
235 rc = -EINVAL;
236 if (sk->sk_state != SMC_INIT)
237 goto out_rel;
238
239 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
240 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
241
242out_rel:
243 release_sock(sk);
244out:
245 return rc;
246}
247
248static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
249 unsigned long mask)
250{
251 /* options we don't get control via setsockopt for */
252 nsk->sk_type = osk->sk_type;
253 nsk->sk_sndbuf = osk->sk_sndbuf;
254 nsk->sk_rcvbuf = osk->sk_rcvbuf;
255 nsk->sk_sndtimeo = osk->sk_sndtimeo;
256 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
257 nsk->sk_mark = osk->sk_mark;
258 nsk->sk_priority = osk->sk_priority;
259 nsk->sk_rcvlowat = osk->sk_rcvlowat;
260 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
261 nsk->sk_err = osk->sk_err;
262
263 nsk->sk_flags &= ~mask;
264 nsk->sk_flags |= osk->sk_flags & mask;
265}
266
267#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
268 (1UL << SOCK_KEEPOPEN) | \
269 (1UL << SOCK_LINGER) | \
270 (1UL << SOCK_BROADCAST) | \
271 (1UL << SOCK_TIMESTAMP) | \
272 (1UL << SOCK_DBG) | \
273 (1UL << SOCK_RCVTSTAMP) | \
274 (1UL << SOCK_RCVTSTAMPNS) | \
275 (1UL << SOCK_LOCALROUTE) | \
276 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
277 (1UL << SOCK_RXQ_OVFL) | \
278 (1UL << SOCK_WIFI_STATUS) | \
279 (1UL << SOCK_NOFCS) | \
280 (1UL << SOCK_FILTER_LOCKED))
281/* copy only relevant settings and flags of SOL_SOCKET level from smc to
282 * clc socket (since smc is not called for these options from net/core)
283 */
284static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
285{
286 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
287}
288
289#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
290 (1UL << SOCK_KEEPOPEN) | \
291 (1UL << SOCK_LINGER) | \
292 (1UL << SOCK_DBG))
293/* copy only settings and flags relevant for smc from clc to smc socket */
294static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
295{
296 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
297}
298
Karsten Graul44aa81c2018-05-15 17:04:55 +0200299/* register a new rmb, optionally send confirm_rkey msg to register with peer */
300static int smc_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc,
301 bool conf_rkey)
Karsten Graule63a5f82018-05-03 17:57:37 +0200302{
303 /* register memory region for new rmb */
Karsten Graula6920d12018-05-03 17:57:38 +0200304 if (smc_wr_reg_send(link, rmb_desc->mr_rx[SMC_SINGLE_LINK])) {
305 rmb_desc->regerr = 1;
Karsten Graule63a5f82018-05-03 17:57:37 +0200306 return -EFAULT;
Karsten Graula6920d12018-05-03 17:57:38 +0200307 }
Karsten Graul44aa81c2018-05-15 17:04:55 +0200308 if (!conf_rkey)
309 return 0;
310 /* exchange confirm_rkey msg with peer */
311 if (smc_llc_do_confirm_rkey(link, rmb_desc)) {
312 rmb_desc->regerr = 1;
313 return -EFAULT;
314 }
Karsten Graule63a5f82018-05-03 17:57:37 +0200315 return 0;
316}
317
Stefan Raspl0f627122018-03-01 13:51:26 +0100318static int smc_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100319{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200320 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100321 struct smc_link_group *lgr = smc->conn.lgr;
322 struct smc_link *link;
323 int rest;
324 int rc;
325
326 link = &lgr->lnk[SMC_SINGLE_LINK];
327 /* receive CONFIRM LINK request from server over RoCE fabric */
328 rest = wait_for_completion_interruptible_timeout(
329 &link->llc_confirm,
330 SMC_LLC_WAIT_FIRST_TIME);
331 if (rest <= 0) {
332 struct smc_clc_msg_decline dclc;
333
334 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
335 SMC_CLC_DECLINE);
336 return rc;
337 }
338
Karsten Graul75d320d2018-03-01 13:51:31 +0100339 if (link->llc_confirm_rc)
340 return SMC_CLC_DECL_RMBE_EC;
341
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100342 rc = smc_ib_modify_qp_rts(link);
343 if (rc)
344 return SMC_CLC_DECL_INTERR;
345
346 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200347
Karsten Graul44aa81c2018-05-15 17:04:55 +0200348 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Ursula Braun652a1e42017-07-28 13:56:17 +0200349 return SMC_CLC_DECL_INTERR;
350
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100351 /* send CONFIRM LINK response over RoCE fabric */
352 rc = smc_llc_send_confirm_link(link,
353 link->smcibdev->mac[link->ibport - 1],
Stefan Raspl0f627122018-03-01 13:51:26 +0100354 &link->smcibdev->gid[link->ibport - 1],
355 SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100356 if (rc < 0)
357 return SMC_CLC_DECL_TCL;
358
Karsten Graul52bedf32018-03-01 13:51:32 +0100359 /* receive ADD LINK request from server over RoCE fabric */
360 rest = wait_for_completion_interruptible_timeout(&link->llc_add,
361 SMC_LLC_WAIT_TIME);
362 if (rest <= 0) {
363 struct smc_clc_msg_decline dclc;
364
365 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
366 SMC_CLC_DECLINE);
367 return rc;
368 }
369
370 /* send add link reject message, only one link supported for now */
371 rc = smc_llc_send_add_link(link,
372 link->smcibdev->mac[link->ibport - 1],
373 &link->smcibdev->gid[link->ibport - 1],
374 SMC_LLC_RESP);
375 if (rc < 0)
376 return SMC_CLC_DECL_TCL;
377
Karsten Graul877ae5b2018-05-02 16:56:44 +0200378 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100379
Karsten Graul75d320d2018-03-01 13:51:31 +0100380 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100381}
382
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100383static void smc_conn_save_peer_info(struct smc_sock *smc,
384 struct smc_clc_msg_accept_confirm *clc)
385{
Hans Wippel95d8d2632018-05-18 09:34:13 +0200386 int bufsize = smc_uncompress_bufsize(clc->rmbe_size);
387
Hans Wippel92a138e2018-05-18 09:34:12 +0200388 smc->conn.peer_rmbe_idx = clc->rmbe_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100389 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200390 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100391 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200392 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100393}
394
395static void smc_link_save_peer_info(struct smc_link *link,
396 struct smc_clc_msg_accept_confirm *clc)
397{
398 link->peer_qpn = ntoh24(clc->qpn);
399 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
400 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
401 link->peer_psn = ntoh24(clc->psn);
402 link->peer_mtu = clc->qp_mtu;
403}
404
Hans Wippel3b2dec22018-05-18 09:34:18 +0200405/* fall back during connect */
406static int smc_connect_fallback(struct smc_sock *smc)
407{
408 smc->use_fallback = true;
409 smc_copy_sock_settings_to_clc(smc);
410 if (smc->sk.sk_state == SMC_INIT)
411 smc->sk.sk_state = SMC_ACTIVE;
412 return 0;
413}
414
415/* decline and fall back during connect */
416static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
417{
418 int rc;
419
420 if (reason_code < 0) /* error, fallback is not possible */
421 return reason_code;
422 if (reason_code != SMC_CLC_DECL_REPLY) {
423 rc = smc_clc_send_decline(smc, reason_code);
424 if (rc < 0)
425 return rc;
426 }
427 return smc_connect_fallback(smc);
428}
429
430/* abort connecting */
431static int smc_connect_abort(struct smc_sock *smc, int reason_code,
432 int local_contact)
433{
434 if (local_contact == SMC_FIRST_CONTACT)
435 smc_lgr_forget(smc->conn.lgr);
436 mutex_unlock(&smc_create_lgr_pending);
437 smc_conn_free(&smc->conn);
438 if (reason_code < 0 && smc->sk.sk_state == SMC_INIT)
439 sock_put(&smc->sk); /* passive closing */
440 return reason_code;
441}
442
443/* check if there is a rdma device available for this connection. */
444/* called for connect and listen */
445static int smc_check_rdma(struct smc_sock *smc, struct smc_ib_device **ibdev,
446 u8 *ibport)
447{
448 int reason_code = 0;
449
450 /* PNET table look up: search active ib_device and port
451 * within same PNETID that also contains the ethernet device
452 * used for the internal TCP socket
453 */
454 smc_pnet_find_roce_resource(smc->clcsock->sk, ibdev, ibport);
455 if (!(*ibdev))
456 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
457
458 return reason_code;
459}
460
461/* CLC handshake during connect */
462static int smc_connect_clc(struct smc_sock *smc,
463 struct smc_clc_msg_accept_confirm *aclc,
464 struct smc_ib_device *ibdev, u8 ibport)
465{
466 int rc = 0;
467
468 /* do inband token exchange */
469 rc = smc_clc_send_proposal(smc, ibdev, ibport);
470 if (rc)
471 return rc;
472 /* receive SMC Accept CLC message */
473 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT);
474}
475
Ursula Brauna046d572017-01-09 16:55:16 +0100476/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200477static int smc_connect_rdma(struct smc_sock *smc,
478 struct smc_clc_msg_accept_confirm *aclc,
479 struct smc_ib_device *ibdev, u8 ibport)
480{
481 int local_contact = SMC_FIRST_CONTACT;
482 struct smc_link *link;
483 int reason_code = 0;
484
485 mutex_lock(&smc_create_lgr_pending);
486 local_contact = smc_conn_create(smc, ibdev, ibport, &aclc->lcl,
487 aclc->hdr.flag);
488 if (local_contact < 0) {
489 if (local_contact == -ENOMEM)
490 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
491 else if (local_contact == -ENOLINK)
492 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
493 else
494 reason_code = SMC_CLC_DECL_INTERR; /* other error */
495 return smc_connect_abort(smc, reason_code, 0);
496 }
497 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
498
499 smc_conn_save_peer_info(smc, aclc);
500
501 /* create send buffer and rmb */
502 if (smc_buf_create(smc))
503 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, local_contact);
504
505 if (local_contact == SMC_FIRST_CONTACT)
506 smc_link_save_peer_info(link, aclc);
507
508 if (smc_rmb_rtoken_handling(&smc->conn, aclc))
509 return smc_connect_abort(smc, SMC_CLC_DECL_INTERR,
510 local_contact);
511
512 smc_close_init(smc);
513 smc_rx_init(smc);
514
515 if (local_contact == SMC_FIRST_CONTACT) {
516 if (smc_ib_ready_link(link))
517 return smc_connect_abort(smc, SMC_CLC_DECL_INTERR,
518 local_contact);
519 } else {
520 if (!smc->conn.rmb_desc->reused &&
521 smc_reg_rmb(link, smc->conn.rmb_desc, true))
522 return smc_connect_abort(smc, SMC_CLC_DECL_INTERR,
523 local_contact);
524 }
525 smc_rmb_sync_sg_for_device(&smc->conn);
526
527 reason_code = smc_clc_send_confirm(smc);
528 if (reason_code)
529 return smc_connect_abort(smc, reason_code, local_contact);
530
531 smc_tx_init(smc);
532
533 if (local_contact == SMC_FIRST_CONTACT) {
534 /* QP confirmation over RoCE fabric */
535 reason_code = smc_clnt_conf_first_link(smc);
536 if (reason_code)
537 return smc_connect_abort(smc, reason_code,
538 local_contact);
539 }
540 mutex_unlock(&smc_create_lgr_pending);
541
542 smc_copy_sock_settings_to_clc(smc);
543 if (smc->sk.sk_state == SMC_INIT)
544 smc->sk.sk_state = SMC_ACTIVE;
545
546 return 0;
547}
548
549/* perform steps before actually connecting */
550static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100551{
552 struct smc_clc_msg_accept_confirm aclc;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200553 struct smc_ib_device *ibdev;
Ursula Brauna046d572017-01-09 16:55:16 +0100554 int rc = 0;
555 u8 ibport;
556
Ursula Braun51f1de72018-01-26 09:28:48 +0100557 sock_hold(&smc->sk); /* sock put in passive closing */
558
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200559 if (smc->use_fallback)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200560 return smc_connect_fallback(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200561
Hans Wippel3b2dec22018-05-18 09:34:18 +0200562 /* if peer has not signalled SMC-capability, fall back */
563 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
564 return smc_connect_fallback(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200565
Ursula Brauna046d572017-01-09 16:55:16 +0100566 /* IPSec connections opt out of SMC-R optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200567 if (using_ipsec(smc))
568 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
Ursula Brauna046d572017-01-09 16:55:16 +0100569
Hans Wippel3b2dec22018-05-18 09:34:18 +0200570 /* check if a RDMA device is available; if not, fall back */
571 if (smc_check_rdma(smc, &ibdev, &ibport))
572 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_CNFERR);
Ursula Brauna046d572017-01-09 16:55:16 +0100573
Hans Wippel3b2dec22018-05-18 09:34:18 +0200574 /* perform CLC handshake */
575 rc = smc_connect_clc(smc, &aclc, ibdev, ibport);
Ursula Brauna046d572017-01-09 16:55:16 +0100576 if (rc)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200577 return smc_connect_decline_fallback(smc, rc);
Ursula Brauna046d572017-01-09 16:55:16 +0100578
Hans Wippel3b2dec22018-05-18 09:34:18 +0200579 /* connect using rdma */
580 rc = smc_connect_rdma(smc, &aclc, ibdev, ibport);
581 if (rc)
582 return smc_connect_decline_fallback(smc, rc);
Ursula Brauna046d572017-01-09 16:55:16 +0100583
Hans Wippel3b2dec22018-05-18 09:34:18 +0200584 return 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100585}
586
Ursula Braun24ac3a02018-06-27 17:59:50 +0200587static void smc_connect_work(struct work_struct *work)
588{
589 struct smc_sock *smc = container_of(work, struct smc_sock,
590 connect_work);
591 int rc;
592
593 lock_sock(&smc->sk);
594 rc = kernel_connect(smc->clcsock, &smc->connect_info->addr,
595 smc->connect_info->alen, smc->connect_info->flags);
596 if (smc->clcsock->sk->sk_err) {
597 smc->sk.sk_err = smc->clcsock->sk->sk_err;
598 goto out;
599 }
600 if (rc < 0) {
601 smc->sk.sk_err = -rc;
602 goto out;
603 }
604
605 rc = __smc_connect(smc);
606 if (rc < 0)
607 smc->sk.sk_err = -rc;
608
609out:
610 smc->sk.sk_state_change(&smc->sk);
611 kfree(smc->connect_info);
612 smc->connect_info = NULL;
613 release_sock(&smc->sk);
614}
615
Ursula Braunac713872017-01-09 16:55:13 +0100616static int smc_connect(struct socket *sock, struct sockaddr *addr,
617 int alen, int flags)
618{
619 struct sock *sk = sock->sk;
620 struct smc_sock *smc;
621 int rc = -EINVAL;
622
623 smc = smc_sk(sk);
624
625 /* separate smc parameter checking to be safe */
626 if (alen < sizeof(addr->sa_family))
627 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100628 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100629 goto out_err;
630
631 lock_sock(sk);
632 switch (sk->sk_state) {
633 default:
634 goto out;
635 case SMC_ACTIVE:
636 rc = -EISCONN;
637 goto out;
638 case SMC_INIT:
639 rc = 0;
640 break;
641 }
642
643 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200644 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200645 if (flags & O_NONBLOCK) {
646 if (smc->connect_info) {
647 rc = -EALREADY;
648 goto out;
649 }
650 smc->connect_info = kzalloc(alen + 2 * sizeof(int), GFP_KERNEL);
651 if (!smc->connect_info) {
652 rc = -ENOMEM;
653 goto out;
654 }
655 smc->connect_info->alen = alen;
656 smc->connect_info->flags = flags ^ O_NONBLOCK;
657 memcpy(&smc->connect_info->addr, addr, alen);
658 schedule_work(&smc->connect_work);
659 rc = -EINPROGRESS;
660 } else {
661 rc = kernel_connect(smc->clcsock, addr, alen, flags);
662 if (rc)
663 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100664
Ursula Braun24ac3a02018-06-27 17:59:50 +0200665 rc = __smc_connect(smc);
666 if (rc < 0)
667 goto out;
668 else
669 rc = 0; /* success cases including fallback */
670 }
Ursula Braunac713872017-01-09 16:55:13 +0100671
672out:
673 release_sock(sk);
674out_err:
675 return rc;
676}
677
678static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
679{
Ursula Braun3163c502018-01-24 10:28:12 +0100680 struct socket *new_clcsock = NULL;
681 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100682 struct sock *new_sk;
683 int rc;
684
Ursula Braun3163c502018-01-24 10:28:12 +0100685 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100686 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100687 if (!new_sk) {
688 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100689 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100690 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100691 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100692 goto out;
693 }
694 *new_smc = smc_sk(new_sk);
695
696 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
Ursula Braun3163c502018-01-24 10:28:12 +0100697 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100698 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100699 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100700 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Brauna046d572017-01-09 16:55:16 +0100701 if (new_clcsock)
702 sock_release(new_clcsock);
703 new_sk->sk_state = SMC_CLOSED;
704 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun3163c502018-01-24 10:28:12 +0100705 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100706 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100707 *new_smc = NULL;
708 goto out;
709 }
710
711 (*new_smc)->clcsock = new_clcsock;
712out:
713 return rc;
714}
715
Ursula Brauna046d572017-01-09 16:55:16 +0100716/* add a just created sock to the accept queue of the listen sock as
717 * candidate for a following socket accept call from user space
718 */
719static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
720{
721 struct smc_sock *par = smc_sk(parent);
722
Ursula Braun51f1de72018-01-26 09:28:48 +0100723 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100724 spin_lock(&par->accept_q_lock);
725 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
726 spin_unlock(&par->accept_q_lock);
727 sk_acceptq_added(parent);
728}
729
730/* remove a socket from the accept queue of its parental listening socket */
731static void smc_accept_unlink(struct sock *sk)
732{
733 struct smc_sock *par = smc_sk(sk)->listen_smc;
734
735 spin_lock(&par->accept_q_lock);
736 list_del_init(&smc_sk(sk)->accept_q);
737 spin_unlock(&par->accept_q_lock);
738 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100739 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100740}
741
742/* remove a sock from the accept queue to bind it to a new socket created
743 * for a socket accept call from user space
744 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100745struct sock *smc_accept_dequeue(struct sock *parent,
746 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100747{
748 struct smc_sock *isk, *n;
749 struct sock *new_sk;
750
751 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
752 new_sk = (struct sock *)isk;
753
754 smc_accept_unlink(new_sk);
755 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braun127f4972018-01-26 09:28:49 +0100756 if (isk->clcsock) {
757 sock_release(isk->clcsock);
758 isk->clcsock = NULL;
759 }
Ursula Braun288c83902017-04-10 14:58:04 +0200760 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100761 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +0100762 continue;
763 }
764 if (new_sock)
765 sock_graft(new_sk, new_sock);
766 return new_sk;
767 }
768 return NULL;
769}
770
771/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +0100772void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +0100773{
774 struct smc_sock *smc = smc_sk(sk);
775
Ursula Braunb38d7322017-01-09 16:55:25 +0100776 lock_sock(sk);
777 if (!sk->sk_lingertime)
778 /* wait for peer closing */
779 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100780 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100781 smc_close_active(smc);
Ursula Braun288c83902017-04-10 14:58:04 +0200782 sock_set_flag(sk, SOCK_DEAD);
783 sk->sk_shutdown |= SHUTDOWN_MASK;
784 }
Ursula Brauna046d572017-01-09 16:55:16 +0100785 if (smc->clcsock) {
786 struct socket *tcp;
787
788 tcp = smc->clcsock;
789 smc->clcsock = NULL;
790 sock_release(tcp);
791 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100792 if (smc->use_fallback) {
Ursula Braun51f1de72018-01-26 09:28:48 +0100793 sock_put(sk); /* passive closing */
794 sk->sk_state = SMC_CLOSED;
795 } else {
796 if (sk->sk_state == SMC_CLOSED)
797 smc_conn_free(&smc->conn);
Ursula Braunb38d7322017-01-09 16:55:25 +0100798 }
799 release_sock(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100800 sk->sk_prot->unhash(sk);
801 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +0100802}
803
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100804static int smc_serv_conf_first_link(struct smc_sock *smc)
805{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200806 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100807 struct smc_link_group *lgr = smc->conn.lgr;
808 struct smc_link *link;
809 int rest;
810 int rc;
811
812 link = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun652a1e42017-07-28 13:56:17 +0200813
Karsten Graul44aa81c2018-05-15 17:04:55 +0200814 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Ursula Braun652a1e42017-07-28 13:56:17 +0200815 return SMC_CLC_DECL_INTERR;
816
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100817 /* send CONFIRM LINK request to client over the RoCE fabric */
818 rc = smc_llc_send_confirm_link(link,
819 link->smcibdev->mac[link->ibport - 1],
820 &link->smcibdev->gid[link->ibport - 1],
821 SMC_LLC_REQ);
822 if (rc < 0)
823 return SMC_CLC_DECL_TCL;
824
825 /* receive CONFIRM LINK response from client over the RoCE fabric */
826 rest = wait_for_completion_interruptible_timeout(
827 &link->llc_confirm_resp,
828 SMC_LLC_WAIT_FIRST_TIME);
829 if (rest <= 0) {
830 struct smc_clc_msg_decline dclc;
831
832 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
833 SMC_CLC_DECLINE);
Karsten Graul75d320d2018-03-01 13:51:31 +0100834 return rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100835 }
836
Karsten Graul75d320d2018-03-01 13:51:31 +0100837 if (link->llc_confirm_resp_rc)
838 return SMC_CLC_DECL_RMBE_EC;
839
Karsten Graul52bedf32018-03-01 13:51:32 +0100840 /* send ADD LINK request to client over the RoCE fabric */
841 rc = smc_llc_send_add_link(link,
842 link->smcibdev->mac[link->ibport - 1],
843 &link->smcibdev->gid[link->ibport - 1],
844 SMC_LLC_REQ);
845 if (rc < 0)
846 return SMC_CLC_DECL_TCL;
847
848 /* receive ADD LINK response from client over the RoCE fabric */
849 rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
850 SMC_LLC_WAIT_TIME);
851 if (rest <= 0) {
852 struct smc_clc_msg_decline dclc;
853
854 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
855 SMC_CLC_DECLINE);
856 return rc;
857 }
858
Karsten Graul877ae5b2018-05-02 16:56:44 +0200859 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100860
Karsten Graul75d320d2018-03-01 13:51:31 +0100861 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100862}
863
Hans Wippel3b2dec22018-05-18 09:34:18 +0200864/* listen worker: finish */
865static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100866{
Ursula Brauna046d572017-01-09 16:55:16 +0100867 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +0100868 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +0100869
Ursula Braunb38d7322017-01-09 16:55:25 +0100870 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +0100871 if (lsmc->sk.sk_state == SMC_LISTEN) {
872 smc_accept_enqueue(&lsmc->sk, newsmcsk);
873 } else { /* no longer listening */
874 smc_close_non_accepted(newsmcsk);
875 }
876 release_sock(&lsmc->sk);
877
878 /* Wake up accept */
879 lsmc->sk.sk_data_ready(&lsmc->sk);
880 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200881}
Ursula Brauna046d572017-01-09 16:55:16 +0100882
Hans Wippel3b2dec22018-05-18 09:34:18 +0200883/* listen worker: finish in state connected */
884static void smc_listen_out_connected(struct smc_sock *new_smc)
885{
886 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +0100887
Hans Wippel3b2dec22018-05-18 09:34:18 +0200888 sk_refcnt_debug_inc(newsmcsk);
889 if (newsmcsk->sk_state == SMC_INIT)
890 newsmcsk->sk_state = SMC_ACTIVE;
891
892 smc_listen_out(new_smc);
893}
894
895/* listen worker: finish in error state */
896static void smc_listen_out_err(struct smc_sock *new_smc)
897{
898 struct sock *newsmcsk = &new_smc->sk;
899
Ursula Braun51f1de72018-01-26 09:28:48 +0100900 if (newsmcsk->sk_state == SMC_INIT)
901 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +0100902 newsmcsk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100903 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200904
905 smc_listen_out(new_smc);
906}
907
908/* listen worker: decline and fall back if possible */
909static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
910 int local_contact)
911{
912 /* RDMA setup failed, switch back to TCP */
913 if (local_contact == SMC_FIRST_CONTACT)
914 smc_lgr_forget(new_smc->conn.lgr);
915 if (reason_code < 0) { /* error, no fallback possible */
916 smc_listen_out_err(new_smc);
917 return;
918 }
919 smc_conn_free(&new_smc->conn);
920 new_smc->use_fallback = true;
921 if (reason_code && reason_code != SMC_CLC_DECL_REPLY) {
922 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
923 smc_listen_out_err(new_smc);
924 return;
925 }
926 }
927 smc_listen_out_connected(new_smc);
928}
929
930/* listen worker: check prefixes */
931static int smc_listen_rdma_check(struct smc_sock *new_smc,
932 struct smc_clc_msg_proposal *pclc)
933{
934 struct smc_clc_msg_proposal_prefix *pclc_prfx;
935 struct socket *newclcsock = new_smc->clcsock;
936
937 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
938 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
939 return SMC_CLC_DECL_CNFERR;
940
941 return 0;
942}
943
944/* listen worker: initialize connection and buffers */
945static int smc_listen_rdma_init(struct smc_sock *new_smc,
946 struct smc_clc_msg_proposal *pclc,
947 struct smc_ib_device *ibdev, u8 ibport,
948 int *local_contact)
949{
950 /* allocate connection / link group */
951 *local_contact = smc_conn_create(new_smc, ibdev, ibport, &pclc->lcl, 0);
952 if (*local_contact < 0) {
953 if (*local_contact == -ENOMEM)
954 return SMC_CLC_DECL_MEM;/* insufficient memory*/
955 return SMC_CLC_DECL_INTERR; /* other error */
956 }
957
958 /* create send buffer and rmb */
959 if (smc_buf_create(new_smc))
960 return SMC_CLC_DECL_MEM;
961
962 return 0;
963}
964
965/* listen worker: register buffers */
966static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
967{
968 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
969
970 if (local_contact != SMC_FIRST_CONTACT) {
971 if (!new_smc->conn.rmb_desc->reused) {
972 if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true))
973 return SMC_CLC_DECL_INTERR;
974 }
975 }
976 smc_rmb_sync_sg_for_device(&new_smc->conn);
977
978 return 0;
979}
980
981/* listen worker: finish RDMA setup */
982static void smc_listen_rdma_finish(struct smc_sock *new_smc,
983 struct smc_clc_msg_accept_confirm *cclc,
984 int local_contact)
985{
986 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
987 int reason_code = 0;
988
989 if (local_contact == SMC_FIRST_CONTACT)
990 smc_link_save_peer_info(link, cclc);
991
992 if (smc_rmb_rtoken_handling(&new_smc->conn, cclc)) {
993 reason_code = SMC_CLC_DECL_INTERR;
994 goto decline;
995 }
996
997 if (local_contact == SMC_FIRST_CONTACT) {
998 if (smc_ib_ready_link(link)) {
999 reason_code = SMC_CLC_DECL_INTERR;
1000 goto decline;
1001 }
1002 /* QP confirmation over RoCE fabric */
1003 reason_code = smc_serv_conf_first_link(new_smc);
1004 if (reason_code)
1005 goto decline;
1006 }
1007 return;
1008
1009decline:
1010 mutex_unlock(&smc_create_lgr_pending);
1011 smc_listen_decline(new_smc, reason_code, local_contact);
1012}
1013
1014/* setup for RDMA connection of server */
1015static void smc_listen_work(struct work_struct *work)
1016{
1017 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1018 smc_listen_work);
1019 struct socket *newclcsock = new_smc->clcsock;
1020 struct smc_clc_msg_accept_confirm cclc;
1021 struct smc_clc_msg_proposal *pclc;
1022 struct smc_ib_device *ibdev;
1023 u8 buf[SMC_CLC_MAX_LEN];
1024 int local_contact = 0;
1025 int reason_code = 0;
1026 int rc = 0;
1027 u8 ibport;
1028
1029 if (new_smc->use_fallback) {
1030 smc_listen_out_connected(new_smc);
1031 return;
1032 }
1033
1034 /* check if peer is smc capable */
1035 if (!tcp_sk(newclcsock->sk)->syn_smc) {
1036 new_smc->use_fallback = true;
1037 smc_listen_out_connected(new_smc);
1038 return;
1039 }
1040
1041 /* do inband token exchange -
1042 * wait for and receive SMC Proposal CLC message
1043 */
1044 pclc = (struct smc_clc_msg_proposal *)&buf;
1045 reason_code = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
1046 SMC_CLC_PROPOSAL);
1047 if (reason_code) {
1048 smc_listen_decline(new_smc, reason_code, 0);
1049 return;
1050 }
1051
1052 /* IPSec connections opt out of SMC-R optimizations */
1053 if (using_ipsec(new_smc)) {
1054 smc_listen_decline(new_smc, SMC_CLC_DECL_IPSEC, 0);
1055 return;
1056 }
1057
1058 mutex_lock(&smc_create_lgr_pending);
1059 smc_close_init(new_smc);
1060 smc_rx_init(new_smc);
1061 smc_tx_init(new_smc);
1062
1063 /* check if RDMA is available */
1064 if (smc_check_rdma(new_smc, &ibdev, &ibport) ||
1065 smc_listen_rdma_check(new_smc, pclc) ||
1066 smc_listen_rdma_init(new_smc, pclc, ibdev, ibport,
1067 &local_contact) ||
1068 smc_listen_rdma_reg(new_smc, local_contact)) {
1069 /* SMC not supported, decline */
1070 mutex_unlock(&smc_create_lgr_pending);
1071 smc_listen_decline(new_smc, SMC_CLC_DECL_CNFERR, local_contact);
1072 return;
1073 }
1074
1075 /* send SMC Accept CLC message */
1076 rc = smc_clc_send_accept(new_smc, local_contact);
1077 if (rc) {
1078 mutex_unlock(&smc_create_lgr_pending);
1079 smc_listen_decline(new_smc, rc, local_contact);
1080 return;
1081 }
1082
1083 /* receive SMC Confirm CLC message */
1084 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
1085 SMC_CLC_CONFIRM);
1086 if (reason_code) {
1087 mutex_unlock(&smc_create_lgr_pending);
1088 smc_listen_decline(new_smc, reason_code, local_contact);
1089 return;
1090 }
1091
1092 /* finish worker */
1093 smc_listen_rdma_finish(new_smc, &cclc, local_contact);
1094 smc_conn_save_peer_info(new_smc, &cclc);
1095 mutex_unlock(&smc_create_lgr_pending);
1096 smc_listen_out_connected(new_smc);
Ursula Brauna046d572017-01-09 16:55:16 +01001097}
1098
1099static void smc_tcp_listen_work(struct work_struct *work)
1100{
1101 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1102 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001103 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001104 struct smc_sock *new_smc;
1105 int rc = 0;
1106
Ursula Braun3163c502018-01-24 10:28:12 +01001107 lock_sock(lsk);
1108 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001109 rc = smc_clcsock_accept(lsmc, &new_smc);
1110 if (rc)
1111 goto out;
1112 if (!new_smc)
1113 continue;
1114
1115 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001116 new_smc->use_fallback = lsmc->use_fallback;
Ursula Braun3163c502018-01-24 10:28:12 +01001117 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001118 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1119 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braun51f1de72018-01-26 09:28:48 +01001120 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1121 if (!schedule_work(&new_smc->smc_listen_work))
1122 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001123 }
1124
1125out:
Ursula Braun3163c502018-01-24 10:28:12 +01001126 release_sock(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001127 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +01001128}
1129
Ursula Braunac713872017-01-09 16:55:13 +01001130static int smc_listen(struct socket *sock, int backlog)
1131{
1132 struct sock *sk = sock->sk;
1133 struct smc_sock *smc;
1134 int rc;
1135
1136 smc = smc_sk(sk);
1137 lock_sock(sk);
1138
1139 rc = -EINVAL;
1140 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
1141 goto out;
1142
1143 rc = 0;
1144 if (sk->sk_state == SMC_LISTEN) {
1145 sk->sk_max_ack_backlog = backlog;
1146 goto out;
1147 }
1148 /* some socket options are handled in core, so we could not apply
1149 * them to the clc socket -- copy smc socket options to clc socket
1150 */
1151 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001152 if (!smc->use_fallback)
1153 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001154
1155 rc = kernel_listen(smc->clcsock, backlog);
1156 if (rc)
1157 goto out;
1158 sk->sk_max_ack_backlog = backlog;
1159 sk->sk_ack_backlog = 0;
1160 sk->sk_state = SMC_LISTEN;
Ursula Brauna046d572017-01-09 16:55:16 +01001161 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun51f1de72018-01-26 09:28:48 +01001162 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1163 if (!schedule_work(&smc->tcp_listen_work))
1164 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001165
1166out:
1167 release_sock(sk);
1168 return rc;
1169}
1170
1171static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001172 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001173{
Ursula Brauna046d572017-01-09 16:55:16 +01001174 struct sock *sk = sock->sk, *nsk;
1175 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001176 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001177 long timeo;
1178 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001179
1180 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001181 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001182 lock_sock(sk);
1183
1184 if (lsmc->sk.sk_state != SMC_LISTEN) {
1185 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001186 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001187 goto out;
1188 }
1189
Ursula Brauna046d572017-01-09 16:55:16 +01001190 /* Wait for an incoming connection */
1191 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1192 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1193 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1194 set_current_state(TASK_INTERRUPTIBLE);
1195 if (!timeo) {
1196 rc = -EAGAIN;
1197 break;
1198 }
1199 release_sock(sk);
1200 timeo = schedule_timeout(timeo);
1201 /* wakeup by sk_data_ready in smc_listen_work() */
1202 sched_annotate_sleep();
1203 lock_sock(sk);
1204 if (signal_pending(current)) {
1205 rc = sock_intr_errno(timeo);
1206 break;
1207 }
1208 }
1209 set_current_state(TASK_RUNNING);
1210 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001211
Ursula Brauna046d572017-01-09 16:55:16 +01001212 if (!rc)
1213 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001214 release_sock(sk);
1215 if (rc)
1216 goto out;
1217
1218 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1219 /* wait till data arrives on the socket */
1220 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1221 MSEC_PER_SEC);
1222 if (smc_sk(nsk)->use_fallback) {
1223 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1224
1225 lock_sock(clcsk);
1226 if (skb_queue_empty(&clcsk->sk_receive_queue))
1227 sk_wait_data(clcsk, &timeo, NULL);
1228 release_sock(clcsk);
1229 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1230 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001231 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001232 release_sock(nsk);
1233 }
1234 }
Ursula Braunac713872017-01-09 16:55:13 +01001235
1236out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001237 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001238 return rc;
1239}
1240
1241static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001242 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001243{
1244 struct smc_sock *smc;
1245
Ursula Braunb38d7322017-01-09 16:55:25 +01001246 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1247 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001248 return -ENOTCONN;
1249
1250 smc = smc_sk(sock->sk);
1251
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001252 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001253}
1254
1255static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1256{
1257 struct sock *sk = sock->sk;
1258 struct smc_sock *smc;
1259 int rc = -EPIPE;
1260
1261 smc = smc_sk(sk);
1262 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001263 if ((sk->sk_state != SMC_ACTIVE) &&
1264 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1265 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001266 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001267
1268 if (msg->msg_flags & MSG_FASTOPEN) {
1269 if (sk->sk_state == SMC_INIT) {
1270 smc->use_fallback = true;
1271 } else {
1272 rc = -EINVAL;
1273 goto out;
1274 }
1275 }
1276
Ursula Braunac713872017-01-09 16:55:13 +01001277 if (smc->use_fallback)
1278 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1279 else
Ursula Braune6727f32017-01-09 16:55:23 +01001280 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001281out:
1282 release_sock(sk);
1283 return rc;
1284}
1285
1286static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1287 int flags)
1288{
1289 struct sock *sk = sock->sk;
1290 struct smc_sock *smc;
1291 int rc = -ENOTCONN;
1292
1293 smc = smc_sk(sk);
1294 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001295 if ((sk->sk_state == SMC_INIT) ||
1296 (sk->sk_state == SMC_LISTEN) ||
1297 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001298 goto out;
1299
Ursula Braunb38d7322017-01-09 16:55:25 +01001300 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1301 rc = 0;
1302 goto out;
1303 }
1304
Stefan Raspl9014db22018-05-03 18:12:39 +02001305 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001306 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001307 } else {
1308 msg->msg_namelen = 0;
1309 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1310 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001311
Ursula Braunac713872017-01-09 16:55:13 +01001312out:
1313 release_sock(sk);
1314 return rc;
1315}
1316
Al Viroade994f2017-07-03 00:01:49 -04001317static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001318{
Ursula Braun8dce2782018-01-26 09:28:47 +01001319 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001320 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001321
Ursula Braun8dce2782018-01-26 09:28:47 +01001322 spin_lock(&isk->accept_q_lock);
1323 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001324 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001325 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001326
Ursula Braun8dce2782018-01-26 09:28:47 +01001327 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001328}
1329
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001330static __poll_t smc_poll(struct file *file, struct socket *sock,
1331 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01001332{
1333 struct sock *sk = sock->sk;
Al Viroe6c8adc2017-07-03 22:25:56 -04001334 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001335 struct smc_sock *smc;
1336
Ursula Braun8dce2782018-01-26 09:28:47 +01001337 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001338 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001339
Ursula Braunac713872017-01-09 16:55:13 +01001340 smc = smc_sk(sock->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001341 if ((sk->sk_state == SMC_INIT) || smc->use_fallback) {
1342 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001343 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02001344 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001345 if (sk->sk_err)
Ursula Braun784813a2018-05-02 16:53:56 +02001346 mask |= EPOLLERR;
Ursula Braunac713872017-01-09 16:55:13 +01001347 } else {
Ursula Brauna046d572017-01-09 16:55:16 +01001348 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001349 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001350 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1351 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001352 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001353 if (sk->sk_state == SMC_LISTEN) {
1354 /* woken up by sk_data_ready in smc_listen_work() */
1355 mask = smc_accept_poll(sk);
1356 } else {
1357 if (atomic_read(&smc->conn.sndbuf_space) ||
1358 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001359 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001360 } else {
1361 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1362 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1363 }
1364 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001365 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001366 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001367 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001368 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001369 mask |= EPOLLIN;
Ursula Braun8dce2782018-01-26 09:28:47 +01001370 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001371 if (smc->conn.urg_state == SMC_URG_VALID)
1372 mask |= EPOLLPRI;
Ursula Braunb38d7322017-01-09 16:55:25 +01001373
Ursula Braunac713872017-01-09 16:55:13 +01001374 }
1375
1376 return mask;
1377}
1378
1379static int smc_shutdown(struct socket *sock, int how)
1380{
1381 struct sock *sk = sock->sk;
1382 struct smc_sock *smc;
1383 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001384 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001385
1386 smc = smc_sk(sk);
1387
1388 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001389 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001390
1391 lock_sock(sk);
1392
1393 rc = -ENOTCONN;
Ursula Braunb38d7322017-01-09 16:55:25 +01001394 if ((sk->sk_state != SMC_LISTEN) &&
1395 (sk->sk_state != SMC_ACTIVE) &&
1396 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1397 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1398 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1399 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1400 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001401 goto out;
1402 if (smc->use_fallback) {
1403 rc = kernel_sock_shutdown(smc->clcsock, how);
1404 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1405 if (sk->sk_shutdown == SHUTDOWN_MASK)
1406 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001407 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001408 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001409 switch (how) {
1410 case SHUT_RDWR: /* shutdown in both directions */
1411 rc = smc_close_active(smc);
1412 break;
1413 case SHUT_WR:
1414 rc = smc_close_shutdown_write(smc);
1415 break;
1416 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001417 rc = 0;
1418 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001419 break;
1420 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001421 if (smc->clcsock)
1422 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001423 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1424 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001425
1426out:
1427 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001428 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001429}
1430
1431static int smc_setsockopt(struct socket *sock, int level, int optname,
1432 char __user *optval, unsigned int optlen)
1433{
1434 struct sock *sk = sock->sk;
1435 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001436 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001437
1438 smc = smc_sk(sk);
1439
1440 /* generic setsockopts reaching us here always apply to the
1441 * CLC socket
1442 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001443 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1444 optval, optlen);
1445 if (smc->clcsock->sk->sk_err) {
1446 sk->sk_err = smc->clcsock->sk->sk_err;
1447 sk->sk_error_report(sk);
1448 }
1449 if (rc)
1450 return rc;
1451
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001452 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00001453 return -EINVAL;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001454 get_user(val, (int __user *)optval);
1455
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001456 lock_sock(sk);
1457 switch (optname) {
1458 case TCP_ULP:
1459 case TCP_FASTOPEN:
1460 case TCP_FASTOPEN_CONNECT:
1461 case TCP_FASTOPEN_KEY:
1462 case TCP_FASTOPEN_NO_COOKIE:
1463 /* option not supported by SMC */
1464 if (sk->sk_state == SMC_INIT) {
1465 smc->use_fallback = true;
1466 } else {
1467 if (!smc->use_fallback)
1468 rc = -EINVAL;
1469 }
1470 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001471 case TCP_NODELAY:
1472 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001473 if (val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001474 mod_delayed_work(system_wq, &smc->conn.tx_work,
1475 0);
1476 }
1477 break;
1478 case TCP_CORK:
1479 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001480 if (!val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001481 mod_delayed_work(system_wq, &smc->conn.tx_work,
1482 0);
1483 }
1484 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001485 case TCP_DEFER_ACCEPT:
1486 smc->sockopt_defer_accept = val;
1487 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001488 default:
1489 break;
1490 }
1491 release_sock(sk);
1492
1493 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001494}
1495
1496static int smc_getsockopt(struct socket *sock, int level, int optname,
1497 char __user *optval, int __user *optlen)
1498{
1499 struct smc_sock *smc;
1500
1501 smc = smc_sk(sock->sk);
1502 /* socket options apply to the CLC socket */
1503 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1504 optval, optlen);
1505}
1506
1507static int smc_ioctl(struct socket *sock, unsigned int cmd,
1508 unsigned long arg)
1509{
Stefan Rasplde8474e2018-05-23 16:38:11 +02001510 union smc_host_cursor cons, urg;
1511 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01001512 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001513 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001514
1515 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001516 conn = &smc->conn;
Ursula Braun9b67e262018-05-02 16:56:46 +02001517 if (smc->use_fallback) {
1518 if (!smc->clcsock)
1519 return -EBADF;
Ursula Braunac713872017-01-09 16:55:13 +01001520 return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
Ursula Braun9b67e262018-05-02 16:56:46 +02001521 }
1522 switch (cmd) {
1523 case SIOCINQ: /* same as FIONREAD */
1524 if (smc->sk.sk_state == SMC_LISTEN)
1525 return -EINVAL;
Ursula Braun2351abe2018-05-23 16:38:09 +02001526 if (smc->sk.sk_state == SMC_INIT ||
1527 smc->sk.sk_state == SMC_CLOSED)
1528 answ = 0;
1529 else
1530 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02001531 break;
1532 case SIOCOUTQ:
1533 /* output queue size (not send + not acked) */
1534 if (smc->sk.sk_state == SMC_LISTEN)
1535 return -EINVAL;
Ursula Braun2351abe2018-05-23 16:38:09 +02001536 if (smc->sk.sk_state == SMC_INIT ||
1537 smc->sk.sk_state == SMC_CLOSED)
1538 answ = 0;
1539 else
1540 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02001541 atomic_read(&smc->conn.sndbuf_space);
1542 break;
1543 case SIOCOUTQNSD:
1544 /* output queue size (not send only) */
1545 if (smc->sk.sk_state == SMC_LISTEN)
1546 return -EINVAL;
Ursula Braun2351abe2018-05-23 16:38:09 +02001547 if (smc->sk.sk_state == SMC_INIT ||
1548 smc->sk.sk_state == SMC_CLOSED)
1549 answ = 0;
1550 else
1551 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02001552 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02001553 case SIOCATMARK:
1554 if (smc->sk.sk_state == SMC_LISTEN)
1555 return -EINVAL;
1556 if (smc->sk.sk_state == SMC_INIT ||
1557 smc->sk.sk_state == SMC_CLOSED) {
1558 answ = 0;
1559 } else {
1560 smc_curs_write(&cons,
1561 smc_curs_read(&conn->local_tx_ctrl.cons, conn),
1562 conn);
1563 smc_curs_write(&urg,
1564 smc_curs_read(&conn->urg_curs, conn),
1565 conn);
1566 answ = smc_curs_diff(conn->rmb_desc->len,
1567 &cons, &urg) == 1;
1568 }
1569 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02001570 default:
1571 return -ENOIOCTLCMD;
1572 }
1573
1574 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001575}
1576
1577static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1578 int offset, size_t size, int flags)
1579{
1580 struct sock *sk = sock->sk;
1581 struct smc_sock *smc;
1582 int rc = -EPIPE;
1583
1584 smc = smc_sk(sk);
1585 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001586 if (sk->sk_state != SMC_ACTIVE) {
1587 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001588 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001589 }
1590 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001591 if (smc->use_fallback)
1592 rc = kernel_sendpage(smc->clcsock, page, offset,
1593 size, flags);
1594 else
1595 rc = sock_no_sendpage(sock, page, offset, size, flags);
1596
1597out:
Ursula Braunac713872017-01-09 16:55:13 +01001598 return rc;
1599}
1600
Stefan Raspl9014db22018-05-03 18:12:39 +02001601/* Map the affected portions of the rmbe into an spd, note the number of bytes
1602 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1603 * updates till whenever a respective page has been fully processed.
1604 * Note that subsequent recv() calls have to wait till all splice() processing
1605 * completed.
1606 */
Ursula Braunac713872017-01-09 16:55:13 +01001607static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1608 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001609 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001610{
1611 struct sock *sk = sock->sk;
1612 struct smc_sock *smc;
1613 int rc = -ENOTCONN;
1614
1615 smc = smc_sk(sk);
1616 lock_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001617
1618 if (sk->sk_state == SMC_INIT ||
1619 sk->sk_state == SMC_LISTEN ||
1620 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001621 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001622
1623 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1624 rc = 0;
1625 goto out;
1626 }
1627
Ursula Braunac713872017-01-09 16:55:13 +01001628 if (smc->use_fallback) {
1629 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1630 pipe, len, flags);
1631 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001632 if (*ppos) {
1633 rc = -ESPIPE;
1634 goto out;
1635 }
1636 if (flags & SPLICE_F_NONBLOCK)
1637 flags = MSG_DONTWAIT;
1638 else
1639 flags = 0;
1640 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001641 }
1642out:
1643 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001644
Ursula Braunac713872017-01-09 16:55:13 +01001645 return rc;
1646}
1647
1648/* must look like tcp */
1649static const struct proto_ops smc_sock_ops = {
1650 .family = PF_SMC,
1651 .owner = THIS_MODULE,
1652 .release = smc_release,
1653 .bind = smc_bind,
1654 .connect = smc_connect,
1655 .socketpair = sock_no_socketpair,
1656 .accept = smc_accept,
1657 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001658 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01001659 .ioctl = smc_ioctl,
1660 .listen = smc_listen,
1661 .shutdown = smc_shutdown,
1662 .setsockopt = smc_setsockopt,
1663 .getsockopt = smc_getsockopt,
1664 .sendmsg = smc_sendmsg,
1665 .recvmsg = smc_recvmsg,
1666 .mmap = sock_no_mmap,
1667 .sendpage = smc_sendpage,
1668 .splice_read = smc_splice_read,
1669};
1670
1671static int smc_create(struct net *net, struct socket *sock, int protocol,
1672 int kern)
1673{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001674 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001675 struct smc_sock *smc;
1676 struct sock *sk;
1677 int rc;
1678
1679 rc = -ESOCKTNOSUPPORT;
1680 if (sock->type != SOCK_STREAM)
1681 goto out;
1682
1683 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001684 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01001685 goto out;
1686
1687 rc = -ENOBUFS;
1688 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001689 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001690 if (!sk)
1691 goto out;
1692
1693 /* create internal TCP socket for CLC handshake and fallback */
1694 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001695 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graulaaa4d332018-03-16 15:06:41 +01001696 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1697 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01001698 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01001699 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01001700 goto out;
1701 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01001702 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1703 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01001704
1705out:
1706 return rc;
1707}
1708
1709static const struct net_proto_family smc_sock_family_ops = {
1710 .family = PF_SMC,
1711 .owner = THIS_MODULE,
1712 .create = smc_create,
1713};
1714
1715static int __init smc_init(void)
1716{
1717 int rc;
1718
Thomas Richter6812baa2017-01-09 16:55:15 +01001719 rc = smc_pnet_init();
1720 if (rc)
1721 return rc;
1722
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001723 rc = smc_llc_init();
1724 if (rc) {
1725 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1726 goto out_pnet;
1727 }
1728
Ursula Braun5f083182017-01-09 16:55:22 +01001729 rc = smc_cdc_init();
1730 if (rc) {
1731 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1732 goto out_pnet;
1733 }
1734
Ursula Braunac713872017-01-09 16:55:13 +01001735 rc = proto_register(&smc_proto, 1);
1736 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01001737 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Thomas Richter6812baa2017-01-09 16:55:15 +01001738 goto out_pnet;
Ursula Braunac713872017-01-09 16:55:13 +01001739 }
1740
Karsten Graulaaa4d332018-03-16 15:06:41 +01001741 rc = proto_register(&smc_proto6, 1);
1742 if (rc) {
1743 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
1744 goto out_proto;
1745 }
1746
Ursula Braunac713872017-01-09 16:55:13 +01001747 rc = sock_register(&smc_sock_family_ops);
1748 if (rc) {
1749 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001750 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01001751 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01001752 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001753 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01001754
Ursula Brauna4cf0442017-01-09 16:55:14 +01001755 rc = smc_ib_register_client();
1756 if (rc) {
1757 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1758 goto out_sock;
1759 }
1760
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001761 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01001762 return 0;
1763
Ursula Brauna4cf0442017-01-09 16:55:14 +01001764out_sock:
1765 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001766out_proto6:
1767 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01001768out_proto:
1769 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001770out_pnet:
1771 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001772 return rc;
1773}
1774
1775static void __exit smc_exit(void)
1776{
Hans Wippel9fda3512018-05-18 09:34:11 +02001777 smc_core_exit();
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001778 static_branch_disable(&tcp_have_smc);
Ursula Brauna4cf0442017-01-09 16:55:14 +01001779 smc_ib_unregister_client();
Ursula Braunac713872017-01-09 16:55:13 +01001780 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001781 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01001782 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001783 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001784}
1785
1786module_init(smc_init);
1787module_exit(smc_exit);
1788
1789MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
1790MODULE_DESCRIPTION("smc socket address family");
1791MODULE_LICENSE("GPL");
1792MODULE_ALIAS_NETPROTO(PF_SMC);