blob: e6e506b2db99eb13dbaf8a2ae7f84801ddfb9011 [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>
Hans Wippel41349842018-06-28 19:05:11 +020026#include <linux/if_vlan.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010027
Ursula Braunac713872017-01-09 16:55:13 +010028#include <net/sock.h>
Ursula Brauna046d572017-01-09 16:55:16 +010029#include <net/tcp.h>
Ursula Braunf16a7dd2017-01-09 16:55:26 +010030#include <net/smc.h>
Ursula Braun9b67e262018-05-02 16:56:46 +020031#include <asm/ioctls.h>
Ursula Braunac713872017-01-09 16:55:13 +010032
33#include "smc.h"
Ursula Brauna046d572017-01-09 16:55:16 +010034#include "smc_clc.h"
Ursula Braun9bf9abe2017-01-09 16:55:21 +010035#include "smc_llc.h"
Ursula Braun5f083182017-01-09 16:55:22 +010036#include "smc_cdc.h"
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010037#include "smc_core.h"
Ursula Brauna4cf0442017-01-09 16:55:14 +010038#include "smc_ib.h"
Hans Wippel41349842018-06-28 19:05:11 +020039#include "smc_ism.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
Ursula Brauna046d572017-01-09 16:55:16 +010049static void smc_tcp_listen_work(struct work_struct *);
Ursula Braun24ac3a02018-06-27 17:59:50 +020050static void smc_connect_work(struct work_struct *);
Ursula Brauna046d572017-01-09 16:55:16 +010051
Ursula Braunac713872017-01-09 16:55:13 +010052static void smc_set_keepalive(struct sock *sk, int val)
53{
54 struct smc_sock *smc = smc_sk(sk);
55
56 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
57}
58
Ursula Braunf16a7dd2017-01-09 16:55:26 +010059static struct smc_hashinfo smc_v4_hashinfo = {
60 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
61};
62
Karsten Graulaaa4d332018-03-16 15:06:41 +010063static struct smc_hashinfo smc_v6_hashinfo = {
64 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
65};
66
Ursula Braunf16a7dd2017-01-09 16:55:26 +010067int smc_hash_sk(struct sock *sk)
68{
69 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
70 struct hlist_head *head;
71
72 head = &h->ht;
73
74 write_lock_bh(&h->lock);
75 sk_add_node(sk, head);
76 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
77 write_unlock_bh(&h->lock);
78
79 return 0;
80}
81EXPORT_SYMBOL_GPL(smc_hash_sk);
82
83void smc_unhash_sk(struct sock *sk)
84{
85 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
86
87 write_lock_bh(&h->lock);
88 if (sk_del_node_init(sk))
89 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
90 write_unlock_bh(&h->lock);
91}
92EXPORT_SYMBOL_GPL(smc_unhash_sk);
93
94struct proto smc_proto = {
Ursula Braunac713872017-01-09 16:55:13 +010095 .name = "SMC",
96 .owner = THIS_MODULE,
97 .keepalive = smc_set_keepalive,
Ursula Braunf16a7dd2017-01-09 16:55:26 +010098 .hash = smc_hash_sk,
99 .unhash = smc_unhash_sk,
Ursula Braunac713872017-01-09 16:55:13 +0100100 .obj_size = sizeof(struct smc_sock),
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100101 .h.smc_hash = &smc_v4_hashinfo,
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800102 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Ursula Braunac713872017-01-09 16:55:13 +0100103};
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100104EXPORT_SYMBOL_GPL(smc_proto);
Ursula Braunac713872017-01-09 16:55:13 +0100105
Karsten Graulaaa4d332018-03-16 15:06:41 +0100106struct proto smc_proto6 = {
107 .name = "SMC6",
108 .owner = THIS_MODULE,
109 .keepalive = smc_set_keepalive,
110 .hash = smc_hash_sk,
111 .unhash = smc_unhash_sk,
112 .obj_size = sizeof(struct smc_sock),
113 .h.smc_hash = &smc_v6_hashinfo,
114 .slab_flags = SLAB_TYPESAFE_BY_RCU,
115};
116EXPORT_SYMBOL_GPL(smc_proto6);
117
Ursula Braunac713872017-01-09 16:55:13 +0100118static int smc_release(struct socket *sock)
119{
120 struct sock *sk = sock->sk;
121 struct smc_sock *smc;
Ursula Braunb38d7322017-01-09 16:55:25 +0100122 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +0100123
124 if (!sk)
125 goto out;
126
127 smc = smc_sk(sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200128
129 /* cleanup for a dangling non-blocking connect */
130 flush_work(&smc->connect_work);
131 kfree(smc->connect_info);
132 smc->connect_info = NULL;
133
Ursula Braunb38d7322017-01-09 16:55:25 +0100134 if (sk->sk_state == SMC_LISTEN)
135 /* smc_close_non_accepted() is called and acquires
136 * sock lock for child sockets again
137 */
138 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
139 else
140 lock_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +0100141
Ursula Braun51f1de72018-01-26 09:28:48 +0100142 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100143 rc = smc_close_active(smc);
144 sock_set_flag(sk, SOCK_DEAD);
145 sk->sk_shutdown |= SHUTDOWN_MASK;
146 }
Cong Wang8dc262d2019-01-05 23:45:26 -0800147
148 sk->sk_prot->unhash(sk);
149
Ursula Braunac713872017-01-09 16:55:13 +0100150 if (smc->clcsock) {
Myungho Junge0280172018-12-18 09:02:25 -0800151 if (smc->use_fallback && sk->sk_state == SMC_LISTEN) {
152 /* wake up clcsock accept */
153 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
154 }
155 mutex_lock(&smc->clcsock_release_lock);
Ursula Braunac713872017-01-09 16:55:13 +0100156 sock_release(smc->clcsock);
157 smc->clcsock = NULL;
Myungho Junge0280172018-12-18 09:02:25 -0800158 mutex_unlock(&smc->clcsock_release_lock);
Ursula Braunac713872017-01-09 16:55:13 +0100159 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100160 if (smc->use_fallback) {
Ursula Braune1bbdd52018-07-05 16:15:30 +0200161 if (sk->sk_state != SMC_LISTEN && sk->sk_state != SMC_INIT)
162 sock_put(sk); /* passive closing */
Ursula Braun51f1de72018-01-26 09:28:48 +0100163 sk->sk_state = SMC_CLOSED;
164 sk->sk_state_change(sk);
165 }
Ursula Braunac713872017-01-09 16:55:13 +0100166
167 /* detach socket */
168 sock_orphan(sk);
169 sock->sk = NULL;
Ursula Braun51f1de72018-01-26 09:28:48 +0100170 if (!smc->use_fallback && sk->sk_state == SMC_CLOSED)
Ursula Braunb38d7322017-01-09 16:55:25 +0100171 smc_conn_free(&smc->conn);
Ursula Braunac713872017-01-09 16:55:13 +0100172 release_sock(sk);
173
Ursula Braun51f1de72018-01-26 09:28:48 +0100174 sock_put(sk); /* final sock_put */
Ursula Braunac713872017-01-09 16:55:13 +0100175out:
Ursula Braunb38d7322017-01-09 16:55:25 +0100176 return rc;
Ursula Braunac713872017-01-09 16:55:13 +0100177}
178
179static void smc_destruct(struct sock *sk)
180{
181 if (sk->sk_state != SMC_CLOSED)
182 return;
183 if (!sock_flag(sk, SOCK_DEAD))
184 return;
185
186 sk_refcnt_debug_dec(sk);
187}
188
Karsten Graulaaa4d332018-03-16 15:06:41 +0100189static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
190 int protocol)
Ursula Braunac713872017-01-09 16:55:13 +0100191{
192 struct smc_sock *smc;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100193 struct proto *prot;
Ursula Braunac713872017-01-09 16:55:13 +0100194 struct sock *sk;
195
Karsten Graulaaa4d332018-03-16 15:06:41 +0100196 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
197 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
Ursula Braunac713872017-01-09 16:55:13 +0100198 if (!sk)
199 return NULL;
200
201 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
202 sk->sk_state = SMC_INIT;
203 sk->sk_destruct = smc_destruct;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100204 sk->sk_protocol = protocol;
Ursula Braunac713872017-01-09 16:55:13 +0100205 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100206 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200207 INIT_WORK(&smc->connect_work, smc_connect_work);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700208 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
Ursula Brauna046d572017-01-09 16:55:16 +0100209 INIT_LIST_HEAD(&smc->accept_q);
210 spin_lock_init(&smc->accept_q_lock);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700211 spin_lock_init(&smc->conn.send_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100212 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100213 sk_refcnt_debug_inc(sk);
Myungho Junge0280172018-12-18 09:02:25 -0800214 mutex_init(&smc->clcsock_release_lock);
Ursula Braunac713872017-01-09 16:55:13 +0100215
216 return sk;
217}
218
219static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
220 int addr_len)
221{
222 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
223 struct sock *sk = sock->sk;
224 struct smc_sock *smc;
225 int rc;
226
227 smc = smc_sk(sk);
228
229 /* replicate tests from inet_bind(), to be safe wrt. future changes */
230 rc = -EINVAL;
231 if (addr_len < sizeof(struct sockaddr_in))
232 goto out;
233
234 rc = -EAFNOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100235 if (addr->sin_family != AF_INET &&
236 addr->sin_family != AF_INET6 &&
237 addr->sin_family != AF_UNSPEC)
238 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100239 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
Karsten Graulaaa4d332018-03-16 15:06:41 +0100240 if (addr->sin_family == AF_UNSPEC &&
241 addr->sin_addr.s_addr != htonl(INADDR_ANY))
Ursula Braunac713872017-01-09 16:55:13 +0100242 goto out;
243
244 lock_sock(sk);
245
246 /* Check if socket is already active */
247 rc = -EINVAL;
248 if (sk->sk_state != SMC_INIT)
249 goto out_rel;
250
251 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
252 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
253
254out_rel:
255 release_sock(sk);
256out:
257 return rc;
258}
259
260static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
261 unsigned long mask)
262{
263 /* options we don't get control via setsockopt for */
264 nsk->sk_type = osk->sk_type;
265 nsk->sk_sndbuf = osk->sk_sndbuf;
266 nsk->sk_rcvbuf = osk->sk_rcvbuf;
267 nsk->sk_sndtimeo = osk->sk_sndtimeo;
268 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
269 nsk->sk_mark = osk->sk_mark;
270 nsk->sk_priority = osk->sk_priority;
271 nsk->sk_rcvlowat = osk->sk_rcvlowat;
272 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
273 nsk->sk_err = osk->sk_err;
274
275 nsk->sk_flags &= ~mask;
276 nsk->sk_flags |= osk->sk_flags & mask;
277}
278
279#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
280 (1UL << SOCK_KEEPOPEN) | \
281 (1UL << SOCK_LINGER) | \
282 (1UL << SOCK_BROADCAST) | \
283 (1UL << SOCK_TIMESTAMP) | \
284 (1UL << SOCK_DBG) | \
285 (1UL << SOCK_RCVTSTAMP) | \
286 (1UL << SOCK_RCVTSTAMPNS) | \
287 (1UL << SOCK_LOCALROUTE) | \
288 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
289 (1UL << SOCK_RXQ_OVFL) | \
290 (1UL << SOCK_WIFI_STATUS) | \
291 (1UL << SOCK_NOFCS) | \
292 (1UL << SOCK_FILTER_LOCKED))
293/* copy only relevant settings and flags of SOL_SOCKET level from smc to
294 * clc socket (since smc is not called for these options from net/core)
295 */
296static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
297{
298 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
299}
300
301#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
302 (1UL << SOCK_KEEPOPEN) | \
303 (1UL << SOCK_LINGER) | \
304 (1UL << SOCK_DBG))
305/* copy only settings and flags relevant for smc from clc to smc socket */
306static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
307{
308 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
309}
310
Karsten Graul44aa81c2018-05-15 17:04:55 +0200311/* register a new rmb, optionally send confirm_rkey msg to register with peer */
312static int smc_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc,
313 bool conf_rkey)
Karsten Graule63a5f82018-05-03 17:57:37 +0200314{
315 /* register memory region for new rmb */
Karsten Graula6920d12018-05-03 17:57:38 +0200316 if (smc_wr_reg_send(link, rmb_desc->mr_rx[SMC_SINGLE_LINK])) {
317 rmb_desc->regerr = 1;
Karsten Graule63a5f82018-05-03 17:57:37 +0200318 return -EFAULT;
Karsten Graula6920d12018-05-03 17:57:38 +0200319 }
Karsten Graul44aa81c2018-05-15 17:04:55 +0200320 if (!conf_rkey)
321 return 0;
322 /* exchange confirm_rkey msg with peer */
323 if (smc_llc_do_confirm_rkey(link, rmb_desc)) {
324 rmb_desc->regerr = 1;
325 return -EFAULT;
326 }
Karsten Graule63a5f82018-05-03 17:57:37 +0200327 return 0;
328}
329
Stefan Raspl0f627122018-03-01 13:51:26 +0100330static int smc_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100331{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200332 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100333 struct smc_link_group *lgr = smc->conn.lgr;
334 struct smc_link *link;
335 int rest;
336 int rc;
337
338 link = &lgr->lnk[SMC_SINGLE_LINK];
339 /* receive CONFIRM LINK request from server over RoCE fabric */
340 rest = wait_for_completion_interruptible_timeout(
341 &link->llc_confirm,
342 SMC_LLC_WAIT_FIRST_TIME);
343 if (rest <= 0) {
344 struct smc_clc_msg_decline dclc;
345
346 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
347 SMC_CLC_DECLINE);
348 return rc;
349 }
350
Karsten Graul75d320d2018-03-01 13:51:31 +0100351 if (link->llc_confirm_rc)
352 return SMC_CLC_DECL_RMBE_EC;
353
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100354 rc = smc_ib_modify_qp_rts(link);
355 if (rc)
Karsten Graul603cc142018-07-25 16:35:32 +0200356 return SMC_CLC_DECL_ERR_RDYLNK;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100357
358 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200359
Karsten Graul44aa81c2018-05-15 17:04:55 +0200360 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200361 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200362
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100363 /* send CONFIRM LINK response over RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200364 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100365 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200366 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100367
Karsten Graul52bedf32018-03-01 13:51:32 +0100368 /* receive ADD LINK request from server over RoCE fabric */
369 rest = wait_for_completion_interruptible_timeout(&link->llc_add,
370 SMC_LLC_WAIT_TIME);
371 if (rest <= 0) {
372 struct smc_clc_msg_decline dclc;
373
374 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
375 SMC_CLC_DECLINE);
376 return rc;
377 }
378
379 /* send add link reject message, only one link supported for now */
380 rc = smc_llc_send_add_link(link,
381 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +0200382 link->gid, SMC_LLC_RESP);
Karsten Graul52bedf32018-03-01 13:51:32 +0100383 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200384 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +0100385
Karsten Graul877ae5b2018-05-02 16:56:44 +0200386 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100387
Karsten Graul75d320d2018-03-01 13:51:31 +0100388 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100389}
390
Hans Wippel41349842018-06-28 19:05:11 +0200391static void smcr_conn_save_peer_info(struct smc_sock *smc,
392 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100393{
Hans Wippel95d8d2632018-05-18 09:34:13 +0200394 int bufsize = smc_uncompress_bufsize(clc->rmbe_size);
395
Hans Wippel92a138e2018-05-18 09:34:12 +0200396 smc->conn.peer_rmbe_idx = clc->rmbe_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100397 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200398 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100399 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200400 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100401}
402
Hans Wippel41349842018-06-28 19:05:11 +0200403static void smcd_conn_save_peer_info(struct smc_sock *smc,
404 struct smc_clc_msg_accept_confirm *clc)
405{
406 int bufsize = smc_uncompress_bufsize(clc->dmbe_size);
407
408 smc->conn.peer_rmbe_idx = clc->dmbe_idx;
409 smc->conn.peer_token = clc->token;
410 /* msg header takes up space in the buffer */
411 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
412 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
413 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
414}
415
416static void smc_conn_save_peer_info(struct smc_sock *smc,
417 struct smc_clc_msg_accept_confirm *clc)
418{
419 if (smc->conn.lgr->is_smcd)
420 smcd_conn_save_peer_info(smc, clc);
421 else
422 smcr_conn_save_peer_info(smc, clc);
423}
424
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100425static void smc_link_save_peer_info(struct smc_link *link,
426 struct smc_clc_msg_accept_confirm *clc)
427{
428 link->peer_qpn = ntoh24(clc->qpn);
429 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
430 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
431 link->peer_psn = ntoh24(clc->psn);
432 link->peer_mtu = clc->qp_mtu;
433}
434
Hans Wippel3b2dec22018-05-18 09:34:18 +0200435/* fall back during connect */
Karsten Graul603cc142018-07-25 16:35:32 +0200436static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200437{
438 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +0200439 smc->fallback_rsn = reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200440 smc_copy_sock_settings_to_clc(smc);
441 if (smc->sk.sk_state == SMC_INIT)
442 smc->sk.sk_state = SMC_ACTIVE;
443 return 0;
444}
445
446/* decline and fall back during connect */
447static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
448{
449 int rc;
450
Ursula Braune1bbdd52018-07-05 16:15:30 +0200451 if (reason_code < 0) { /* error, fallback is not possible */
452 if (smc->sk.sk_state == SMC_INIT)
453 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200454 return reason_code;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200455 }
Karsten Graul603cc142018-07-25 16:35:32 +0200456 if (reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200457 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200458 if (rc < 0) {
459 if (smc->sk.sk_state == SMC_INIT)
460 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200461 return rc;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200462 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200463 }
Karsten Graul603cc142018-07-25 16:35:32 +0200464 return smc_connect_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200465}
466
467/* abort connecting */
468static int smc_connect_abort(struct smc_sock *smc, int reason_code,
469 int local_contact)
470{
471 if (local_contact == SMC_FIRST_CONTACT)
472 smc_lgr_forget(smc->conn.lgr);
473 mutex_unlock(&smc_create_lgr_pending);
474 smc_conn_free(&smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200475 return reason_code;
476}
477
478/* check if there is a rdma device available for this connection. */
479/* called for connect and listen */
480static int smc_check_rdma(struct smc_sock *smc, struct smc_ib_device **ibdev,
Ursula Braun7005ada2018-07-25 16:35:31 +0200481 u8 *ibport, unsigned short vlan_id, u8 gid[])
Hans Wippel3b2dec22018-05-18 09:34:18 +0200482{
483 int reason_code = 0;
484
485 /* PNET table look up: search active ib_device and port
486 * within same PNETID that also contains the ethernet device
487 * used for the internal TCP socket
488 */
Ursula Braun7005ada2018-07-25 16:35:31 +0200489 smc_pnet_find_roce_resource(smc->clcsock->sk, ibdev, ibport, vlan_id,
490 gid);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200491 if (!(*ibdev))
492 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
493
494 return reason_code;
495}
496
Hans Wippel41349842018-06-28 19:05:11 +0200497/* check if there is an ISM device available for this connection. */
498/* called for connect and listen */
499static int smc_check_ism(struct smc_sock *smc, struct smcd_dev **ismdev)
500{
501 /* Find ISM device with same PNETID as connecting interface */
502 smc_pnet_find_ism_resource(smc->clcsock->sk, ismdev);
503 if (!(*ismdev))
504 return SMC_CLC_DECL_CNFERR; /* configuration error */
505 return 0;
506}
507
508/* Check for VLAN ID and register it on ISM device just for CLC handshake */
509static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
510 struct smcd_dev *ismdev,
511 unsigned short vlan_id)
512{
513 if (vlan_id && smc_ism_get_vlan(ismdev, vlan_id))
514 return SMC_CLC_DECL_CNFERR;
515 return 0;
516}
517
518/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
519 * used, the VLAN ID will be registered again during the connection setup.
520 */
521static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
522 struct smcd_dev *ismdev,
523 unsigned short vlan_id)
524{
525 if (!is_smcd)
526 return 0;
527 if (vlan_id && smc_ism_put_vlan(ismdev, vlan_id))
528 return SMC_CLC_DECL_CNFERR;
529 return 0;
530}
531
Hans Wippel3b2dec22018-05-18 09:34:18 +0200532/* CLC handshake during connect */
Hans Wippelc758dfd2018-06-28 19:05:09 +0200533static int smc_connect_clc(struct smc_sock *smc, int smc_type,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200534 struct smc_clc_msg_accept_confirm *aclc,
Hans Wippel41349842018-06-28 19:05:11 +0200535 struct smc_ib_device *ibdev, u8 ibport,
Ursula Braun7005ada2018-07-25 16:35:31 +0200536 u8 gid[], struct smcd_dev *ismdev)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200537{
538 int rc = 0;
539
540 /* do inband token exchange */
Ursula Braun7005ada2018-07-25 16:35:31 +0200541 rc = smc_clc_send_proposal(smc, smc_type, ibdev, ibport, gid, ismdev);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200542 if (rc)
543 return rc;
544 /* receive SMC Accept CLC message */
545 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT);
546}
547
Ursula Brauna046d572017-01-09 16:55:16 +0100548/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200549static int smc_connect_rdma(struct smc_sock *smc,
550 struct smc_clc_msg_accept_confirm *aclc,
551 struct smc_ib_device *ibdev, u8 ibport)
552{
553 int local_contact = SMC_FIRST_CONTACT;
554 struct smc_link *link;
555 int reason_code = 0;
556
557 mutex_lock(&smc_create_lgr_pending);
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200558 local_contact = smc_conn_create(smc, false, aclc->hdr.flag, ibdev,
559 ibport, &aclc->lcl, NULL, 0);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200560 if (local_contact < 0) {
561 if (local_contact == -ENOMEM)
562 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
563 else if (local_contact == -ENOLINK)
564 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
565 else
566 reason_code = SMC_CLC_DECL_INTERR; /* other error */
567 return smc_connect_abort(smc, reason_code, 0);
568 }
569 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
570
571 smc_conn_save_peer_info(smc, aclc);
572
573 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200574 if (smc_buf_create(smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +0200575 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, local_contact);
576
577 if (local_contact == SMC_FIRST_CONTACT)
578 smc_link_save_peer_info(link, aclc);
579
580 if (smc_rmb_rtoken_handling(&smc->conn, aclc))
Karsten Graul603cc142018-07-25 16:35:32 +0200581 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200582 local_contact);
583
584 smc_close_init(smc);
585 smc_rx_init(smc);
586
587 if (local_contact == SMC_FIRST_CONTACT) {
588 if (smc_ib_ready_link(link))
Karsten Graul603cc142018-07-25 16:35:32 +0200589 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200590 local_contact);
591 } else {
592 if (!smc->conn.rmb_desc->reused &&
593 smc_reg_rmb(link, smc->conn.rmb_desc, true))
Karsten Graul603cc142018-07-25 16:35:32 +0200594 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200595 local_contact);
596 }
597 smc_rmb_sync_sg_for_device(&smc->conn);
598
599 reason_code = smc_clc_send_confirm(smc);
600 if (reason_code)
601 return smc_connect_abort(smc, reason_code, local_contact);
602
603 smc_tx_init(smc);
604
605 if (local_contact == SMC_FIRST_CONTACT) {
606 /* QP confirmation over RoCE fabric */
607 reason_code = smc_clnt_conf_first_link(smc);
608 if (reason_code)
609 return smc_connect_abort(smc, reason_code,
610 local_contact);
611 }
612 mutex_unlock(&smc_create_lgr_pending);
613
614 smc_copy_sock_settings_to_clc(smc);
615 if (smc->sk.sk_state == SMC_INIT)
616 smc->sk.sk_state = SMC_ACTIVE;
617
618 return 0;
619}
620
Hans Wippel41349842018-06-28 19:05:11 +0200621/* setup for ISM connection of client */
622static int smc_connect_ism(struct smc_sock *smc,
623 struct smc_clc_msg_accept_confirm *aclc,
624 struct smcd_dev *ismdev)
625{
626 int local_contact = SMC_FIRST_CONTACT;
627 int rc = 0;
628
629 mutex_lock(&smc_create_lgr_pending);
630 local_contact = smc_conn_create(smc, true, aclc->hdr.flag, NULL, 0,
631 NULL, ismdev, aclc->gid);
632 if (local_contact < 0)
633 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, 0);
634
635 /* Create send and receive buffers */
636 if (smc_buf_create(smc, true))
637 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, local_contact);
638
639 smc_conn_save_peer_info(smc, aclc);
640 smc_close_init(smc);
641 smc_rx_init(smc);
642 smc_tx_init(smc);
643
644 rc = smc_clc_send_confirm(smc);
645 if (rc)
646 return smc_connect_abort(smc, rc, local_contact);
647 mutex_unlock(&smc_create_lgr_pending);
648
649 smc_copy_sock_settings_to_clc(smc);
650 if (smc->sk.sk_state == SMC_INIT)
651 smc->sk.sk_state = SMC_ACTIVE;
652
653 return 0;
654}
655
Hans Wippel3b2dec22018-05-18 09:34:18 +0200656/* perform steps before actually connecting */
657static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100658{
Hans Wippel41349842018-06-28 19:05:11 +0200659 bool ism_supported = false, rdma_supported = false;
Ursula Brauna046d572017-01-09 16:55:16 +0100660 struct smc_clc_msg_accept_confirm aclc;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200661 struct smc_ib_device *ibdev;
Hans Wippel41349842018-06-28 19:05:11 +0200662 struct smcd_dev *ismdev;
Ursula Braun7005ada2018-07-25 16:35:31 +0200663 u8 gid[SMC_GID_SIZE];
Hans Wippel41349842018-06-28 19:05:11 +0200664 unsigned short vlan;
665 int smc_type;
Ursula Brauna046d572017-01-09 16:55:16 +0100666 int rc = 0;
667 u8 ibport;
668
Ursula Braun51f1de72018-01-26 09:28:48 +0100669 sock_hold(&smc->sk); /* sock put in passive closing */
670
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200671 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +0200672 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200673
Hans Wippel3b2dec22018-05-18 09:34:18 +0200674 /* if peer has not signalled SMC-capability, fall back */
675 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +0200676 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200677
Ursula Brauna046d572017-01-09 16:55:16 +0100678 /* IPSec connections opt out of SMC-R optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200679 if (using_ipsec(smc))
680 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
Ursula Brauna046d572017-01-09 16:55:16 +0100681
Hans Wippel41349842018-06-28 19:05:11 +0200682 /* check for VLAN ID */
683 if (smc_vlan_by_tcpsk(smc->clcsock, &vlan))
684 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_CNFERR);
685
686 /* check if there is an ism device available */
687 if (!smc_check_ism(smc, &ismdev) &&
688 !smc_connect_ism_vlan_setup(smc, ismdev, vlan)) {
689 /* ISM is supported for this connection */
690 ism_supported = true;
691 smc_type = SMC_TYPE_D;
692 }
693
694 /* check if there is a rdma device available */
Ursula Braun7005ada2018-07-25 16:35:31 +0200695 if (!smc_check_rdma(smc, &ibdev, &ibport, vlan, gid)) {
Hans Wippel41349842018-06-28 19:05:11 +0200696 /* RDMA is supported for this connection */
697 rdma_supported = true;
698 if (ism_supported)
699 smc_type = SMC_TYPE_B; /* both */
700 else
701 smc_type = SMC_TYPE_R; /* only RDMA */
702 }
703
704 /* if neither ISM nor RDMA are supported, fallback */
705 if (!rdma_supported && !ism_supported)
Karsten Graul603cc142018-07-25 16:35:32 +0200706 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV);
Ursula Brauna046d572017-01-09 16:55:16 +0100707
Hans Wippel3b2dec22018-05-18 09:34:18 +0200708 /* perform CLC handshake */
Ursula Braun7005ada2018-07-25 16:35:31 +0200709 rc = smc_connect_clc(smc, smc_type, &aclc, ibdev, ibport, gid, ismdev);
Hans Wippel41349842018-06-28 19:05:11 +0200710 if (rc) {
711 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200712 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200713 }
Ursula Brauna046d572017-01-09 16:55:16 +0100714
Hans Wippel41349842018-06-28 19:05:11 +0200715 /* depending on previous steps, connect using rdma or ism */
716 if (rdma_supported && aclc.hdr.path == SMC_TYPE_R)
717 rc = smc_connect_rdma(smc, &aclc, ibdev, ibport);
718 else if (ism_supported && aclc.hdr.path == SMC_TYPE_D)
719 rc = smc_connect_ism(smc, &aclc, ismdev);
720 else
Karsten Graul603cc142018-07-25 16:35:32 +0200721 rc = SMC_CLC_DECL_MODEUNSUPP;
Hans Wippel41349842018-06-28 19:05:11 +0200722 if (rc) {
723 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200724 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200725 }
Ursula Brauna046d572017-01-09 16:55:16 +0100726
Hans Wippel41349842018-06-28 19:05:11 +0200727 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200728 return 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100729}
730
Ursula Braun24ac3a02018-06-27 17:59:50 +0200731static void smc_connect_work(struct work_struct *work)
732{
733 struct smc_sock *smc = container_of(work, struct smc_sock,
734 connect_work);
735 int rc;
736
737 lock_sock(&smc->sk);
738 rc = kernel_connect(smc->clcsock, &smc->connect_info->addr,
739 smc->connect_info->alen, smc->connect_info->flags);
740 if (smc->clcsock->sk->sk_err) {
741 smc->sk.sk_err = smc->clcsock->sk->sk_err;
742 goto out;
743 }
744 if (rc < 0) {
745 smc->sk.sk_err = -rc;
746 goto out;
747 }
748
749 rc = __smc_connect(smc);
750 if (rc < 0)
751 smc->sk.sk_err = -rc;
752
753out:
Ursula Braun648a5a72018-09-18 15:46:34 +0200754 if (smc->sk.sk_err)
755 smc->sk.sk_state_change(&smc->sk);
756 else
757 smc->sk.sk_write_space(&smc->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200758 kfree(smc->connect_info);
759 smc->connect_info = NULL;
760 release_sock(&smc->sk);
761}
762
Ursula Braunac713872017-01-09 16:55:13 +0100763static int smc_connect(struct socket *sock, struct sockaddr *addr,
764 int alen, int flags)
765{
766 struct sock *sk = sock->sk;
767 struct smc_sock *smc;
768 int rc = -EINVAL;
769
770 smc = smc_sk(sk);
771
772 /* separate smc parameter checking to be safe */
773 if (alen < sizeof(addr->sa_family))
774 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100775 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100776 goto out_err;
777
778 lock_sock(sk);
779 switch (sk->sk_state) {
780 default:
781 goto out;
782 case SMC_ACTIVE:
783 rc = -EISCONN;
784 goto out;
785 case SMC_INIT:
786 rc = 0;
787 break;
788 }
789
790 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200791 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200792 if (flags & O_NONBLOCK) {
793 if (smc->connect_info) {
794 rc = -EALREADY;
795 goto out;
796 }
797 smc->connect_info = kzalloc(alen + 2 * sizeof(int), GFP_KERNEL);
798 if (!smc->connect_info) {
799 rc = -ENOMEM;
800 goto out;
801 }
802 smc->connect_info->alen = alen;
803 smc->connect_info->flags = flags ^ O_NONBLOCK;
804 memcpy(&smc->connect_info->addr, addr, alen);
805 schedule_work(&smc->connect_work);
806 rc = -EINPROGRESS;
807 } else {
808 rc = kernel_connect(smc->clcsock, addr, alen, flags);
809 if (rc)
810 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100811
Ursula Braun24ac3a02018-06-27 17:59:50 +0200812 rc = __smc_connect(smc);
813 if (rc < 0)
814 goto out;
815 else
816 rc = 0; /* success cases including fallback */
817 }
Ursula Braunac713872017-01-09 16:55:13 +0100818
819out:
820 release_sock(sk);
821out_err:
822 return rc;
823}
824
825static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
826{
Ursula Braun3163c502018-01-24 10:28:12 +0100827 struct socket *new_clcsock = NULL;
828 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100829 struct sock *new_sk;
Myungho Junge0280172018-12-18 09:02:25 -0800830 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +0100831
Ursula Braun3163c502018-01-24 10:28:12 +0100832 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100833 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100834 if (!new_sk) {
835 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100836 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100837 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100838 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100839 goto out;
840 }
841 *new_smc = smc_sk(new_sk);
842
Myungho Junge0280172018-12-18 09:02:25 -0800843 mutex_lock(&lsmc->clcsock_release_lock);
844 if (lsmc->clcsock)
845 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
846 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +0100847 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100848 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100849 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100850 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Brauna046d572017-01-09 16:55:16 +0100851 if (new_clcsock)
852 sock_release(new_clcsock);
853 new_sk->sk_state = SMC_CLOSED;
854 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun3163c502018-01-24 10:28:12 +0100855 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100856 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100857 *new_smc = NULL;
858 goto out;
859 }
860
861 (*new_smc)->clcsock = new_clcsock;
862out:
863 return rc;
864}
865
Ursula Brauna046d572017-01-09 16:55:16 +0100866/* add a just created sock to the accept queue of the listen sock as
867 * candidate for a following socket accept call from user space
868 */
869static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
870{
871 struct smc_sock *par = smc_sk(parent);
872
Ursula Braun51f1de72018-01-26 09:28:48 +0100873 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100874 spin_lock(&par->accept_q_lock);
875 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
876 spin_unlock(&par->accept_q_lock);
877 sk_acceptq_added(parent);
878}
879
880/* remove a socket from the accept queue of its parental listening socket */
881static void smc_accept_unlink(struct sock *sk)
882{
883 struct smc_sock *par = smc_sk(sk)->listen_smc;
884
885 spin_lock(&par->accept_q_lock);
886 list_del_init(&smc_sk(sk)->accept_q);
887 spin_unlock(&par->accept_q_lock);
888 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100889 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100890}
891
892/* remove a sock from the accept queue to bind it to a new socket created
893 * for a socket accept call from user space
894 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100895struct sock *smc_accept_dequeue(struct sock *parent,
896 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100897{
898 struct smc_sock *isk, *n;
899 struct sock *new_sk;
900
901 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
902 new_sk = (struct sock *)isk;
903
904 smc_accept_unlink(new_sk);
905 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braun127f4972018-01-26 09:28:49 +0100906 if (isk->clcsock) {
907 sock_release(isk->clcsock);
908 isk->clcsock = NULL;
909 }
Ursula Braun288c83902017-04-10 14:58:04 +0200910 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100911 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +0100912 continue;
913 }
914 if (new_sock)
915 sock_graft(new_sk, new_sock);
916 return new_sk;
917 }
918 return NULL;
919}
920
921/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +0100922void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +0100923{
924 struct smc_sock *smc = smc_sk(sk);
925
Ursula Braunb38d7322017-01-09 16:55:25 +0100926 lock_sock(sk);
927 if (!sk->sk_lingertime)
928 /* wait for peer closing */
929 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100930 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100931 smc_close_active(smc);
Ursula Braun288c83902017-04-10 14:58:04 +0200932 sock_set_flag(sk, SOCK_DEAD);
933 sk->sk_shutdown |= SHUTDOWN_MASK;
934 }
Ursula Brauna046d572017-01-09 16:55:16 +0100935 if (smc->clcsock) {
936 struct socket *tcp;
937
938 tcp = smc->clcsock;
939 smc->clcsock = NULL;
940 sock_release(tcp);
941 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100942 if (smc->use_fallback) {
Ursula Braun51f1de72018-01-26 09:28:48 +0100943 sock_put(sk); /* passive closing */
944 sk->sk_state = SMC_CLOSED;
945 } else {
946 if (sk->sk_state == SMC_CLOSED)
947 smc_conn_free(&smc->conn);
Ursula Braunb38d7322017-01-09 16:55:25 +0100948 }
949 release_sock(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100950 sk->sk_prot->unhash(sk);
951 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +0100952}
953
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100954static int smc_serv_conf_first_link(struct smc_sock *smc)
955{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200956 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100957 struct smc_link_group *lgr = smc->conn.lgr;
958 struct smc_link *link;
959 int rest;
960 int rc;
961
962 link = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun652a1e42017-07-28 13:56:17 +0200963
Karsten Graul44aa81c2018-05-15 17:04:55 +0200964 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200965 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200966
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100967 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200968 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100969 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200970 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100971
972 /* receive CONFIRM LINK response from client over the RoCE fabric */
973 rest = wait_for_completion_interruptible_timeout(
974 &link->llc_confirm_resp,
975 SMC_LLC_WAIT_FIRST_TIME);
976 if (rest <= 0) {
977 struct smc_clc_msg_decline dclc;
978
979 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
980 SMC_CLC_DECLINE);
Karsten Graul75d320d2018-03-01 13:51:31 +0100981 return rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100982 }
983
Karsten Graul75d320d2018-03-01 13:51:31 +0100984 if (link->llc_confirm_resp_rc)
985 return SMC_CLC_DECL_RMBE_EC;
986
Karsten Graul52bedf32018-03-01 13:51:32 +0100987 /* send ADD LINK request to client over the RoCE fabric */
988 rc = smc_llc_send_add_link(link,
989 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +0200990 link->gid, SMC_LLC_REQ);
Karsten Graul52bedf32018-03-01 13:51:32 +0100991 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200992 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +0100993
994 /* receive ADD LINK response from client over the RoCE fabric */
995 rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
996 SMC_LLC_WAIT_TIME);
997 if (rest <= 0) {
998 struct smc_clc_msg_decline dclc;
999
1000 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
1001 SMC_CLC_DECLINE);
1002 return rc;
1003 }
1004
Karsten Graul877ae5b2018-05-02 16:56:44 +02001005 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +01001006
Karsten Graul75d320d2018-03-01 13:51:31 +01001007 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001008}
1009
Hans Wippel3b2dec22018-05-18 09:34:18 +02001010/* listen worker: finish */
1011static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001012{
Ursula Brauna046d572017-01-09 16:55:16 +01001013 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001014 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001015
Ursula Braunb38d7322017-01-09 16:55:25 +01001016 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001017 if (lsmc->sk.sk_state == SMC_LISTEN) {
1018 smc_accept_enqueue(&lsmc->sk, newsmcsk);
1019 } else { /* no longer listening */
1020 smc_close_non_accepted(newsmcsk);
1021 }
1022 release_sock(&lsmc->sk);
1023
1024 /* Wake up accept */
1025 lsmc->sk.sk_data_ready(&lsmc->sk);
1026 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001027}
Ursula Brauna046d572017-01-09 16:55:16 +01001028
Hans Wippel3b2dec22018-05-18 09:34:18 +02001029/* listen worker: finish in state connected */
1030static void smc_listen_out_connected(struct smc_sock *new_smc)
1031{
1032 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001033
Hans Wippel3b2dec22018-05-18 09:34:18 +02001034 sk_refcnt_debug_inc(newsmcsk);
1035 if (newsmcsk->sk_state == SMC_INIT)
1036 newsmcsk->sk_state = SMC_ACTIVE;
1037
1038 smc_listen_out(new_smc);
1039}
1040
1041/* listen worker: finish in error state */
1042static void smc_listen_out_err(struct smc_sock *new_smc)
1043{
1044 struct sock *newsmcsk = &new_smc->sk;
1045
Ursula Braun51f1de72018-01-26 09:28:48 +01001046 if (newsmcsk->sk_state == SMC_INIT)
1047 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001048 newsmcsk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001049 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001050
1051 smc_listen_out(new_smc);
1052}
1053
1054/* listen worker: decline and fall back if possible */
1055static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1056 int local_contact)
1057{
1058 /* RDMA setup failed, switch back to TCP */
1059 if (local_contact == SMC_FIRST_CONTACT)
1060 smc_lgr_forget(new_smc->conn.lgr);
1061 if (reason_code < 0) { /* error, no fallback possible */
1062 smc_listen_out_err(new_smc);
1063 return;
1064 }
1065 smc_conn_free(&new_smc->conn);
1066 new_smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001067 new_smc->fallback_rsn = reason_code;
1068 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001069 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1070 smc_listen_out_err(new_smc);
1071 return;
1072 }
1073 }
1074 smc_listen_out_connected(new_smc);
1075}
1076
1077/* listen worker: check prefixes */
1078static int smc_listen_rdma_check(struct smc_sock *new_smc,
1079 struct smc_clc_msg_proposal *pclc)
1080{
1081 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1082 struct socket *newclcsock = new_smc->clcsock;
1083
1084 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1085 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
1086 return SMC_CLC_DECL_CNFERR;
1087
1088 return 0;
1089}
1090
1091/* listen worker: initialize connection and buffers */
1092static int smc_listen_rdma_init(struct smc_sock *new_smc,
1093 struct smc_clc_msg_proposal *pclc,
1094 struct smc_ib_device *ibdev, u8 ibport,
1095 int *local_contact)
1096{
1097 /* allocate connection / link group */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001098 *local_contact = smc_conn_create(new_smc, false, 0, ibdev, ibport,
1099 &pclc->lcl, NULL, 0);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001100 if (*local_contact < 0) {
1101 if (*local_contact == -ENOMEM)
1102 return SMC_CLC_DECL_MEM;/* insufficient memory*/
1103 return SMC_CLC_DECL_INTERR; /* other error */
1104 }
1105
1106 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001107 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001108 return SMC_CLC_DECL_MEM;
1109
1110 return 0;
1111}
1112
Hans Wippel41349842018-06-28 19:05:11 +02001113/* listen worker: initialize connection and buffers for SMC-D */
1114static int smc_listen_ism_init(struct smc_sock *new_smc,
1115 struct smc_clc_msg_proposal *pclc,
1116 struct smcd_dev *ismdev,
1117 int *local_contact)
1118{
1119 struct smc_clc_msg_smcd *pclc_smcd;
1120
1121 pclc_smcd = smc_get_clc_msg_smcd(pclc);
1122 *local_contact = smc_conn_create(new_smc, true, 0, NULL, 0, NULL,
1123 ismdev, pclc_smcd->gid);
1124 if (*local_contact < 0) {
1125 if (*local_contact == -ENOMEM)
1126 return SMC_CLC_DECL_MEM;/* insufficient memory*/
1127 return SMC_CLC_DECL_INTERR; /* other error */
1128 }
1129
1130 /* Check if peer can be reached via ISM device */
1131 if (smc_ism_cantalk(new_smc->conn.lgr->peer_gid,
1132 new_smc->conn.lgr->vlan_id,
1133 new_smc->conn.lgr->smcd)) {
1134 if (*local_contact == SMC_FIRST_CONTACT)
1135 smc_lgr_forget(new_smc->conn.lgr);
1136 smc_conn_free(&new_smc->conn);
1137 return SMC_CLC_DECL_CNFERR;
1138 }
1139
1140 /* Create send and receive buffers */
1141 if (smc_buf_create(new_smc, true)) {
1142 if (*local_contact == SMC_FIRST_CONTACT)
1143 smc_lgr_forget(new_smc->conn.lgr);
1144 smc_conn_free(&new_smc->conn);
1145 return SMC_CLC_DECL_MEM;
1146 }
1147
1148 return 0;
1149}
1150
Hans Wippel3b2dec22018-05-18 09:34:18 +02001151/* listen worker: register buffers */
1152static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
1153{
1154 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1155
1156 if (local_contact != SMC_FIRST_CONTACT) {
1157 if (!new_smc->conn.rmb_desc->reused) {
1158 if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true))
Karsten Graul603cc142018-07-25 16:35:32 +02001159 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001160 }
1161 }
1162 smc_rmb_sync_sg_for_device(&new_smc->conn);
1163
1164 return 0;
1165}
1166
1167/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001168static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1169 struct smc_clc_msg_accept_confirm *cclc,
1170 int local_contact)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001171{
1172 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1173 int reason_code = 0;
1174
1175 if (local_contact == SMC_FIRST_CONTACT)
1176 smc_link_save_peer_info(link, cclc);
1177
1178 if (smc_rmb_rtoken_handling(&new_smc->conn, cclc)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001179 reason_code = SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001180 goto decline;
1181 }
1182
1183 if (local_contact == SMC_FIRST_CONTACT) {
1184 if (smc_ib_ready_link(link)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001185 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001186 goto decline;
1187 }
1188 /* QP confirmation over RoCE fabric */
1189 reason_code = smc_serv_conf_first_link(new_smc);
1190 if (reason_code)
1191 goto decline;
1192 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001193 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001194
1195decline:
1196 mutex_unlock(&smc_create_lgr_pending);
1197 smc_listen_decline(new_smc, reason_code, local_contact);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001198 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001199}
1200
1201/* setup for RDMA connection of server */
1202static void smc_listen_work(struct work_struct *work)
1203{
1204 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1205 smc_listen_work);
1206 struct socket *newclcsock = new_smc->clcsock;
1207 struct smc_clc_msg_accept_confirm cclc;
1208 struct smc_clc_msg_proposal *pclc;
1209 struct smc_ib_device *ibdev;
Hans Wippel41349842018-06-28 19:05:11 +02001210 bool ism_supported = false;
1211 struct smcd_dev *ismdev;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001212 u8 buf[SMC_CLC_MAX_LEN];
1213 int local_contact = 0;
Ursula Braun7005ada2018-07-25 16:35:31 +02001214 unsigned short vlan;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001215 int reason_code = 0;
1216 int rc = 0;
1217 u8 ibport;
1218
1219 if (new_smc->use_fallback) {
1220 smc_listen_out_connected(new_smc);
1221 return;
1222 }
1223
1224 /* check if peer is smc capable */
1225 if (!tcp_sk(newclcsock->sk)->syn_smc) {
1226 new_smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001227 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001228 smc_listen_out_connected(new_smc);
1229 return;
1230 }
1231
1232 /* do inband token exchange -
1233 * wait for and receive SMC Proposal CLC message
1234 */
1235 pclc = (struct smc_clc_msg_proposal *)&buf;
1236 reason_code = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
1237 SMC_CLC_PROPOSAL);
1238 if (reason_code) {
1239 smc_listen_decline(new_smc, reason_code, 0);
1240 return;
1241 }
1242
1243 /* IPSec connections opt out of SMC-R optimizations */
1244 if (using_ipsec(new_smc)) {
1245 smc_listen_decline(new_smc, SMC_CLC_DECL_IPSEC, 0);
1246 return;
1247 }
1248
1249 mutex_lock(&smc_create_lgr_pending);
1250 smc_close_init(new_smc);
1251 smc_rx_init(new_smc);
1252 smc_tx_init(new_smc);
1253
Hans Wippel41349842018-06-28 19:05:11 +02001254 /* check if ISM is available */
1255 if ((pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) &&
1256 !smc_check_ism(new_smc, &ismdev) &&
1257 !smc_listen_ism_init(new_smc, pclc, ismdev, &local_contact)) {
1258 ism_supported = true;
1259 }
1260
Hans Wippel3b2dec22018-05-18 09:34:18 +02001261 /* check if RDMA is available */
Hans Wippel41349842018-06-28 19:05:11 +02001262 if (!ism_supported &&
1263 ((pclc->hdr.path != SMC_TYPE_R && pclc->hdr.path != SMC_TYPE_B) ||
Ursula Braun7005ada2018-07-25 16:35:31 +02001264 smc_vlan_by_tcpsk(new_smc->clcsock, &vlan) ||
1265 smc_check_rdma(new_smc, &ibdev, &ibport, vlan, NULL) ||
Hans Wippel41349842018-06-28 19:05:11 +02001266 smc_listen_rdma_check(new_smc, pclc) ||
1267 smc_listen_rdma_init(new_smc, pclc, ibdev, ibport,
1268 &local_contact) ||
1269 smc_listen_rdma_reg(new_smc, local_contact))) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001270 /* SMC not supported, decline */
1271 mutex_unlock(&smc_create_lgr_pending);
Karsten Graul603cc142018-07-25 16:35:32 +02001272 smc_listen_decline(new_smc, SMC_CLC_DECL_MODEUNSUPP,
1273 local_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001274 return;
1275 }
1276
1277 /* send SMC Accept CLC message */
1278 rc = smc_clc_send_accept(new_smc, local_contact);
1279 if (rc) {
1280 mutex_unlock(&smc_create_lgr_pending);
1281 smc_listen_decline(new_smc, rc, local_contact);
1282 return;
1283 }
1284
1285 /* receive SMC Confirm CLC message */
1286 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
1287 SMC_CLC_CONFIRM);
1288 if (reason_code) {
1289 mutex_unlock(&smc_create_lgr_pending);
1290 smc_listen_decline(new_smc, reason_code, local_contact);
1291 return;
1292 }
1293
1294 /* finish worker */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001295 if (!ism_supported) {
1296 if (smc_listen_rdma_finish(new_smc, &cclc, local_contact))
1297 return;
1298 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001299 smc_conn_save_peer_info(new_smc, &cclc);
1300 mutex_unlock(&smc_create_lgr_pending);
1301 smc_listen_out_connected(new_smc);
Ursula Brauna046d572017-01-09 16:55:16 +01001302}
1303
1304static void smc_tcp_listen_work(struct work_struct *work)
1305{
1306 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1307 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001308 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001309 struct smc_sock *new_smc;
1310 int rc = 0;
1311
Ursula Braun3163c502018-01-24 10:28:12 +01001312 lock_sock(lsk);
1313 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001314 rc = smc_clcsock_accept(lsmc, &new_smc);
1315 if (rc)
1316 goto out;
1317 if (!new_smc)
1318 continue;
1319
1320 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001321 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001322 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001323 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001324 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1325 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001326 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1327 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001328 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1329 if (!schedule_work(&new_smc->smc_listen_work))
1330 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001331 }
1332
1333out:
Ursula Braun3163c502018-01-24 10:28:12 +01001334 release_sock(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001335 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +01001336}
1337
Ursula Braunac713872017-01-09 16:55:13 +01001338static int smc_listen(struct socket *sock, int backlog)
1339{
1340 struct sock *sk = sock->sk;
1341 struct smc_sock *smc;
1342 int rc;
1343
1344 smc = smc_sk(sk);
1345 lock_sock(sk);
1346
1347 rc = -EINVAL;
1348 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
1349 goto out;
1350
1351 rc = 0;
1352 if (sk->sk_state == SMC_LISTEN) {
1353 sk->sk_max_ack_backlog = backlog;
1354 goto out;
1355 }
1356 /* some socket options are handled in core, so we could not apply
1357 * them to the clc socket -- copy smc socket options to clc socket
1358 */
1359 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001360 if (!smc->use_fallback)
1361 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001362
1363 rc = kernel_listen(smc->clcsock, backlog);
1364 if (rc)
1365 goto out;
1366 sk->sk_max_ack_backlog = backlog;
1367 sk->sk_ack_backlog = 0;
1368 sk->sk_state = SMC_LISTEN;
Ursula Brauna046d572017-01-09 16:55:16 +01001369 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun51f1de72018-01-26 09:28:48 +01001370 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1371 if (!schedule_work(&smc->tcp_listen_work))
1372 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001373
1374out:
1375 release_sock(sk);
1376 return rc;
1377}
1378
1379static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001380 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001381{
Ursula Brauna046d572017-01-09 16:55:16 +01001382 struct sock *sk = sock->sk, *nsk;
1383 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001384 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001385 long timeo;
1386 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001387
1388 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001389 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001390 lock_sock(sk);
1391
1392 if (lsmc->sk.sk_state != SMC_LISTEN) {
1393 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001394 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001395 goto out;
1396 }
1397
Ursula Brauna046d572017-01-09 16:55:16 +01001398 /* Wait for an incoming connection */
1399 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1400 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1401 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1402 set_current_state(TASK_INTERRUPTIBLE);
1403 if (!timeo) {
1404 rc = -EAGAIN;
1405 break;
1406 }
1407 release_sock(sk);
1408 timeo = schedule_timeout(timeo);
1409 /* wakeup by sk_data_ready in smc_listen_work() */
1410 sched_annotate_sleep();
1411 lock_sock(sk);
1412 if (signal_pending(current)) {
1413 rc = sock_intr_errno(timeo);
1414 break;
1415 }
1416 }
1417 set_current_state(TASK_RUNNING);
1418 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001419
Ursula Brauna046d572017-01-09 16:55:16 +01001420 if (!rc)
1421 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001422 release_sock(sk);
1423 if (rc)
1424 goto out;
1425
1426 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1427 /* wait till data arrives on the socket */
1428 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1429 MSEC_PER_SEC);
1430 if (smc_sk(nsk)->use_fallback) {
1431 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1432
1433 lock_sock(clcsk);
1434 if (skb_queue_empty(&clcsk->sk_receive_queue))
1435 sk_wait_data(clcsk, &timeo, NULL);
1436 release_sock(clcsk);
1437 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1438 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001439 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001440 release_sock(nsk);
1441 }
1442 }
Ursula Braunac713872017-01-09 16:55:13 +01001443
1444out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001445 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001446 return rc;
1447}
1448
1449static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001450 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001451{
1452 struct smc_sock *smc;
1453
Ursula Braunb38d7322017-01-09 16:55:25 +01001454 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1455 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001456 return -ENOTCONN;
1457
1458 smc = smc_sk(sock->sk);
1459
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001460 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001461}
1462
1463static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1464{
1465 struct sock *sk = sock->sk;
1466 struct smc_sock *smc;
1467 int rc = -EPIPE;
1468
1469 smc = smc_sk(sk);
1470 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001471 if ((sk->sk_state != SMC_ACTIVE) &&
1472 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1473 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001474 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001475
1476 if (msg->msg_flags & MSG_FASTOPEN) {
1477 if (sk->sk_state == SMC_INIT) {
1478 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001479 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001480 } else {
1481 rc = -EINVAL;
1482 goto out;
1483 }
1484 }
1485
Ursula Braunac713872017-01-09 16:55:13 +01001486 if (smc->use_fallback)
1487 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1488 else
Ursula Braune6727f32017-01-09 16:55:23 +01001489 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001490out:
1491 release_sock(sk);
1492 return rc;
1493}
1494
1495static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1496 int flags)
1497{
1498 struct sock *sk = sock->sk;
1499 struct smc_sock *smc;
1500 int rc = -ENOTCONN;
1501
1502 smc = smc_sk(sk);
1503 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001504 if ((sk->sk_state == SMC_INIT) ||
1505 (sk->sk_state == SMC_LISTEN) ||
1506 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001507 goto out;
1508
Ursula Braunb38d7322017-01-09 16:55:25 +01001509 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1510 rc = 0;
1511 goto out;
1512 }
1513
Stefan Raspl9014db22018-05-03 18:12:39 +02001514 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001515 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001516 } else {
1517 msg->msg_namelen = 0;
1518 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1519 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001520
Ursula Braunac713872017-01-09 16:55:13 +01001521out:
1522 release_sock(sk);
1523 return rc;
1524}
1525
Al Viroade994f2017-07-03 00:01:49 -04001526static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001527{
Ursula Braun8dce2782018-01-26 09:28:47 +01001528 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001529 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001530
Ursula Braun8dce2782018-01-26 09:28:47 +01001531 spin_lock(&isk->accept_q_lock);
1532 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001533 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001534 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001535
Ursula Braun8dce2782018-01-26 09:28:47 +01001536 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001537}
1538
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001539static __poll_t smc_poll(struct file *file, struct socket *sock,
1540 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01001541{
1542 struct sock *sk = sock->sk;
Al Viroe6c8adc2017-07-03 22:25:56 -04001543 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001544 struct smc_sock *smc;
1545
Ursula Braun8dce2782018-01-26 09:28:47 +01001546 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001547 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001548
Ursula Braunac713872017-01-09 16:55:13 +01001549 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02001550 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01001551 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001552 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02001553 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001554 if (sk->sk_err)
Ursula Braun784813a2018-05-02 16:53:56 +02001555 mask |= EPOLLERR;
Ursula Braunac713872017-01-09 16:55:13 +01001556 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07001557 if (sk->sk_state != SMC_CLOSED)
Karsten Graulfd54c182018-10-23 13:40:39 +02001558 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01001559 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001560 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001561 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1562 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001563 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001564 if (sk->sk_state == SMC_LISTEN) {
1565 /* woken up by sk_data_ready in smc_listen_work() */
1566 mask = smc_accept_poll(sk);
1567 } else {
1568 if (atomic_read(&smc->conn.sndbuf_space) ||
1569 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001570 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001571 } else {
1572 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1573 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1574 }
1575 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001576 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001577 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001578 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001579 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001580 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02001581 if (smc->conn.urg_state == SMC_URG_VALID)
1582 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01001583 }
Ursula Braunac713872017-01-09 16:55:13 +01001584 }
1585
1586 return mask;
1587}
1588
1589static int smc_shutdown(struct socket *sock, int how)
1590{
1591 struct sock *sk = sock->sk;
1592 struct smc_sock *smc;
1593 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001594 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001595
1596 smc = smc_sk(sk);
1597
1598 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001599 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001600
1601 lock_sock(sk);
1602
1603 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02001604 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01001605 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1606 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1607 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1608 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1609 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001610 goto out;
1611 if (smc->use_fallback) {
1612 rc = kernel_sock_shutdown(smc->clcsock, how);
1613 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1614 if (sk->sk_shutdown == SHUTDOWN_MASK)
1615 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001616 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001617 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001618 switch (how) {
1619 case SHUT_RDWR: /* shutdown in both directions */
1620 rc = smc_close_active(smc);
1621 break;
1622 case SHUT_WR:
1623 rc = smc_close_shutdown_write(smc);
1624 break;
1625 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001626 rc = 0;
1627 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001628 break;
1629 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001630 if (smc->clcsock)
1631 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001632 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1633 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001634
1635out:
1636 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001637 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001638}
1639
1640static int smc_setsockopt(struct socket *sock, int level, int optname,
1641 char __user *optval, unsigned int optlen)
1642{
1643 struct sock *sk = sock->sk;
1644 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001645 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001646
1647 smc = smc_sk(sk);
1648
1649 /* generic setsockopts reaching us here always apply to the
1650 * CLC socket
1651 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001652 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1653 optval, optlen);
1654 if (smc->clcsock->sk->sk_err) {
1655 sk->sk_err = smc->clcsock->sk->sk_err;
1656 sk->sk_error_report(sk);
1657 }
1658 if (rc)
1659 return rc;
1660
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001661 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00001662 return -EINVAL;
Ursula Braunac0107e2018-07-18 15:22:50 +02001663 if (get_user(val, (int __user *)optval))
1664 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001665
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001666 lock_sock(sk);
1667 switch (optname) {
1668 case TCP_ULP:
1669 case TCP_FASTOPEN:
1670 case TCP_FASTOPEN_CONNECT:
1671 case TCP_FASTOPEN_KEY:
1672 case TCP_FASTOPEN_NO_COOKIE:
1673 /* option not supported by SMC */
1674 if (sk->sk_state == SMC_INIT) {
1675 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001676 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001677 } else {
1678 if (!smc->use_fallback)
1679 rc = -EINVAL;
1680 }
1681 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001682 case TCP_NODELAY:
1683 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001684 if (val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001685 mod_delayed_work(system_wq, &smc->conn.tx_work,
1686 0);
1687 }
1688 break;
1689 case TCP_CORK:
1690 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001691 if (!val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001692 mod_delayed_work(system_wq, &smc->conn.tx_work,
1693 0);
1694 }
1695 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001696 case TCP_DEFER_ACCEPT:
1697 smc->sockopt_defer_accept = val;
1698 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001699 default:
1700 break;
1701 }
1702 release_sock(sk);
1703
1704 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001705}
1706
1707static int smc_getsockopt(struct socket *sock, int level, int optname,
1708 char __user *optval, int __user *optlen)
1709{
1710 struct smc_sock *smc;
1711
1712 smc = smc_sk(sock->sk);
1713 /* socket options apply to the CLC socket */
1714 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1715 optval, optlen);
1716}
1717
1718static int smc_ioctl(struct socket *sock, unsigned int cmd,
1719 unsigned long arg)
1720{
Stefan Rasplde8474e2018-05-23 16:38:11 +02001721 union smc_host_cursor cons, urg;
1722 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01001723 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001724 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001725
1726 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001727 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02001728 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02001729 if (smc->use_fallback) {
1730 if (!smc->clcsock) {
1731 release_sock(&smc->sk);
1732 return -EBADF;
1733 }
1734 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1735 release_sock(&smc->sk);
1736 return answ;
1737 }
Ursula Braun9b67e262018-05-02 16:56:46 +02001738 switch (cmd) {
1739 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02001740 if (smc->sk.sk_state == SMC_LISTEN) {
1741 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001742 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001743 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001744 if (smc->sk.sk_state == SMC_INIT ||
1745 smc->sk.sk_state == SMC_CLOSED)
1746 answ = 0;
1747 else
1748 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02001749 break;
1750 case SIOCOUTQ:
1751 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02001752 if (smc->sk.sk_state == SMC_LISTEN) {
1753 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001754 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001755 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001756 if (smc->sk.sk_state == SMC_INIT ||
1757 smc->sk.sk_state == SMC_CLOSED)
1758 answ = 0;
1759 else
1760 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02001761 atomic_read(&smc->conn.sndbuf_space);
1762 break;
1763 case SIOCOUTQNSD:
1764 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02001765 if (smc->sk.sk_state == SMC_LISTEN) {
1766 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001767 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001768 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001769 if (smc->sk.sk_state == SMC_INIT ||
1770 smc->sk.sk_state == SMC_CLOSED)
1771 answ = 0;
1772 else
1773 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02001774 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02001775 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02001776 if (smc->sk.sk_state == SMC_LISTEN) {
1777 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001778 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001779 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001780 if (smc->sk.sk_state == SMC_INIT ||
1781 smc->sk.sk_state == SMC_CLOSED) {
1782 answ = 0;
1783 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02001784 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1785 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001786 answ = smc_curs_diff(conn->rmb_desc->len,
1787 &cons, &urg) == 1;
1788 }
1789 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02001790 default:
Ursula Braun1992d992018-07-16 13:56:52 +02001791 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001792 return -ENOIOCTLCMD;
1793 }
Ursula Braun1992d992018-07-16 13:56:52 +02001794 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001795
1796 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001797}
1798
1799static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1800 int offset, size_t size, int flags)
1801{
1802 struct sock *sk = sock->sk;
1803 struct smc_sock *smc;
1804 int rc = -EPIPE;
1805
1806 smc = smc_sk(sk);
1807 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001808 if (sk->sk_state != SMC_ACTIVE) {
1809 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001810 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001811 }
1812 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001813 if (smc->use_fallback)
1814 rc = kernel_sendpage(smc->clcsock, page, offset,
1815 size, flags);
1816 else
1817 rc = sock_no_sendpage(sock, page, offset, size, flags);
1818
1819out:
Ursula Braunac713872017-01-09 16:55:13 +01001820 return rc;
1821}
1822
Stefan Raspl9014db22018-05-03 18:12:39 +02001823/* Map the affected portions of the rmbe into an spd, note the number of bytes
1824 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1825 * updates till whenever a respective page has been fully processed.
1826 * Note that subsequent recv() calls have to wait till all splice() processing
1827 * completed.
1828 */
Ursula Braunac713872017-01-09 16:55:13 +01001829static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1830 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001831 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001832{
1833 struct sock *sk = sock->sk;
1834 struct smc_sock *smc;
1835 int rc = -ENOTCONN;
1836
1837 smc = smc_sk(sk);
1838 lock_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001839
1840 if (sk->sk_state == SMC_INIT ||
1841 sk->sk_state == SMC_LISTEN ||
1842 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001843 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001844
1845 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1846 rc = 0;
1847 goto out;
1848 }
1849
Ursula Braunac713872017-01-09 16:55:13 +01001850 if (smc->use_fallback) {
1851 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1852 pipe, len, flags);
1853 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001854 if (*ppos) {
1855 rc = -ESPIPE;
1856 goto out;
1857 }
1858 if (flags & SPLICE_F_NONBLOCK)
1859 flags = MSG_DONTWAIT;
1860 else
1861 flags = 0;
1862 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001863 }
1864out:
1865 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001866
Ursula Braunac713872017-01-09 16:55:13 +01001867 return rc;
1868}
1869
1870/* must look like tcp */
1871static const struct proto_ops smc_sock_ops = {
1872 .family = PF_SMC,
1873 .owner = THIS_MODULE,
1874 .release = smc_release,
1875 .bind = smc_bind,
1876 .connect = smc_connect,
1877 .socketpair = sock_no_socketpair,
1878 .accept = smc_accept,
1879 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001880 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01001881 .ioctl = smc_ioctl,
1882 .listen = smc_listen,
1883 .shutdown = smc_shutdown,
1884 .setsockopt = smc_setsockopt,
1885 .getsockopt = smc_getsockopt,
1886 .sendmsg = smc_sendmsg,
1887 .recvmsg = smc_recvmsg,
1888 .mmap = sock_no_mmap,
1889 .sendpage = smc_sendpage,
1890 .splice_read = smc_splice_read,
1891};
1892
1893static int smc_create(struct net *net, struct socket *sock, int protocol,
1894 int kern)
1895{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001896 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001897 struct smc_sock *smc;
1898 struct sock *sk;
1899 int rc;
1900
1901 rc = -ESOCKTNOSUPPORT;
1902 if (sock->type != SOCK_STREAM)
1903 goto out;
1904
1905 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001906 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01001907 goto out;
1908
1909 rc = -ENOBUFS;
1910 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001911 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001912 if (!sk)
1913 goto out;
1914
1915 /* create internal TCP socket for CLC handshake and fallback */
1916 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001917 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02001918 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001919 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1920 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01001921 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01001922 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01001923 goto out;
1924 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01001925 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1926 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01001927
1928out:
1929 return rc;
1930}
1931
1932static const struct net_proto_family smc_sock_family_ops = {
1933 .family = PF_SMC,
1934 .owner = THIS_MODULE,
1935 .create = smc_create,
1936};
1937
1938static int __init smc_init(void)
1939{
1940 int rc;
1941
Thomas Richter6812baa2017-01-09 16:55:15 +01001942 rc = smc_pnet_init();
1943 if (rc)
1944 return rc;
1945
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001946 rc = smc_llc_init();
1947 if (rc) {
1948 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1949 goto out_pnet;
1950 }
1951
Ursula Braun5f083182017-01-09 16:55:22 +01001952 rc = smc_cdc_init();
1953 if (rc) {
1954 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1955 goto out_pnet;
1956 }
1957
Ursula Braunac713872017-01-09 16:55:13 +01001958 rc = proto_register(&smc_proto, 1);
1959 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01001960 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Thomas Richter6812baa2017-01-09 16:55:15 +01001961 goto out_pnet;
Ursula Braunac713872017-01-09 16:55:13 +01001962 }
1963
Karsten Graulaaa4d332018-03-16 15:06:41 +01001964 rc = proto_register(&smc_proto6, 1);
1965 if (rc) {
1966 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
1967 goto out_proto;
1968 }
1969
Ursula Braunac713872017-01-09 16:55:13 +01001970 rc = sock_register(&smc_sock_family_ops);
1971 if (rc) {
1972 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001973 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01001974 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01001975 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001976 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01001977
Ursula Brauna4cf0442017-01-09 16:55:14 +01001978 rc = smc_ib_register_client();
1979 if (rc) {
1980 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1981 goto out_sock;
1982 }
1983
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001984 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01001985 return 0;
1986
Ursula Brauna4cf0442017-01-09 16:55:14 +01001987out_sock:
1988 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001989out_proto6:
1990 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01001991out_proto:
1992 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001993out_pnet:
1994 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001995 return rc;
1996}
1997
1998static void __exit smc_exit(void)
1999{
Hans Wippel9fda3512018-05-18 09:34:11 +02002000 smc_core_exit();
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002001 static_branch_disable(&tcp_have_smc);
Ursula Brauna4cf0442017-01-09 16:55:14 +01002002 smc_ib_unregister_client();
Ursula Braunac713872017-01-09 16:55:13 +01002003 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002004 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002005 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002006 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01002007}
2008
2009module_init(smc_init);
2010module_exit(smc_exit);
2011
2012MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2013MODULE_DESCRIPTION("smc socket address family");
2014MODULE_LICENSE("GPL");
2015MODULE_ALIAS_NETPROTO(PF_SMC);