blob: 82cb0e5634bc76ca4f3be1019e3f517591d74be3 [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 */
Hans Wippelf07920a2018-11-20 16:46:39 +0100130 if (smc->connect_info && sk->sk_state == SMC_INIT)
131 tcp_abort(smc->clcsock->sk, ECONNABORTED);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200132 flush_work(&smc->connect_work);
133 kfree(smc->connect_info);
134 smc->connect_info = NULL;
135
Ursula Braunb38d7322017-01-09 16:55:25 +0100136 if (sk->sk_state == SMC_LISTEN)
137 /* smc_close_non_accepted() is called and acquires
138 * sock lock for child sockets again
139 */
140 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
141 else
142 lock_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +0100143
Ursula Braun51f1de72018-01-26 09:28:48 +0100144 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100145 rc = smc_close_active(smc);
146 sock_set_flag(sk, SOCK_DEAD);
147 sk->sk_shutdown |= SHUTDOWN_MASK;
148 }
Ursula Braunac713872017-01-09 16:55:13 +0100149 if (smc->clcsock) {
Myungho Jung78abe3d2018-12-18 09:02:25 -0800150 if (smc->use_fallback && sk->sk_state == SMC_LISTEN) {
151 /* wake up clcsock accept */
152 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
153 }
154 mutex_lock(&smc->clcsock_release_lock);
Ursula Braunac713872017-01-09 16:55:13 +0100155 sock_release(smc->clcsock);
156 smc->clcsock = NULL;
Myungho Jung78abe3d2018-12-18 09:02:25 -0800157 mutex_unlock(&smc->clcsock_release_lock);
Ursula Braunac713872017-01-09 16:55:13 +0100158 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100159 if (smc->use_fallback) {
Ursula Braune1bbdd52018-07-05 16:15:30 +0200160 if (sk->sk_state != SMC_LISTEN && sk->sk_state != SMC_INIT)
161 sock_put(sk); /* passive closing */
Ursula Braun51f1de72018-01-26 09:28:48 +0100162 sk->sk_state = SMC_CLOSED;
163 sk->sk_state_change(sk);
164 }
Ursula Braunac713872017-01-09 16:55:13 +0100165
166 /* detach socket */
167 sock_orphan(sk);
168 sock->sk = NULL;
Ursula Braun51f1de72018-01-26 09:28:48 +0100169 if (!smc->use_fallback && sk->sk_state == SMC_CLOSED)
Ursula Braunb38d7322017-01-09 16:55:25 +0100170 smc_conn_free(&smc->conn);
Ursula Braunac713872017-01-09 16:55:13 +0100171 release_sock(sk);
172
Ursula Braun51f1de72018-01-26 09:28:48 +0100173 sk->sk_prot->unhash(sk);
174 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 Jung78abe3d2018-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,
Karsten Graulee05ff72018-11-20 16:46:40 +0100559 ibport, ntoh24(aclc->qpn), &aclc->lcl,
560 NULL, 0);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200561 if (local_contact < 0) {
562 if (local_contact == -ENOMEM)
563 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
564 else if (local_contact == -ENOLINK)
565 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
566 else
567 reason_code = SMC_CLC_DECL_INTERR; /* other error */
568 return smc_connect_abort(smc, reason_code, 0);
569 }
570 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
571
572 smc_conn_save_peer_info(smc, aclc);
573
574 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200575 if (smc_buf_create(smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +0200576 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, local_contact);
577
578 if (local_contact == SMC_FIRST_CONTACT)
579 smc_link_save_peer_info(link, aclc);
580
581 if (smc_rmb_rtoken_handling(&smc->conn, aclc))
Karsten Graul603cc142018-07-25 16:35:32 +0200582 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200583 local_contact);
584
585 smc_close_init(smc);
586 smc_rx_init(smc);
587
588 if (local_contact == SMC_FIRST_CONTACT) {
589 if (smc_ib_ready_link(link))
Karsten Graul603cc142018-07-25 16:35:32 +0200590 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200591 local_contact);
592 } else {
593 if (!smc->conn.rmb_desc->reused &&
594 smc_reg_rmb(link, smc->conn.rmb_desc, true))
Karsten Graul603cc142018-07-25 16:35:32 +0200595 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200596 local_contact);
597 }
598 smc_rmb_sync_sg_for_device(&smc->conn);
599
600 reason_code = smc_clc_send_confirm(smc);
601 if (reason_code)
602 return smc_connect_abort(smc, reason_code, local_contact);
603
604 smc_tx_init(smc);
605
606 if (local_contact == SMC_FIRST_CONTACT) {
607 /* QP confirmation over RoCE fabric */
608 reason_code = smc_clnt_conf_first_link(smc);
609 if (reason_code)
610 return smc_connect_abort(smc, reason_code,
611 local_contact);
612 }
613 mutex_unlock(&smc_create_lgr_pending);
614
615 smc_copy_sock_settings_to_clc(smc);
616 if (smc->sk.sk_state == SMC_INIT)
617 smc->sk.sk_state = SMC_ACTIVE;
618
619 return 0;
620}
621
Hans Wippel41349842018-06-28 19:05:11 +0200622/* setup for ISM connection of client */
623static int smc_connect_ism(struct smc_sock *smc,
624 struct smc_clc_msg_accept_confirm *aclc,
625 struct smcd_dev *ismdev)
626{
627 int local_contact = SMC_FIRST_CONTACT;
628 int rc = 0;
629
630 mutex_lock(&smc_create_lgr_pending);
Karsten Graulee05ff72018-11-20 16:46:40 +0100631 local_contact = smc_conn_create(smc, true, aclc->hdr.flag, NULL, 0, 0,
Hans Wippel41349842018-06-28 19:05:11 +0200632 NULL, ismdev, aclc->gid);
633 if (local_contact < 0)
634 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, 0);
635
636 /* Create send and receive buffers */
637 if (smc_buf_create(smc, true))
638 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, local_contact);
639
640 smc_conn_save_peer_info(smc, aclc);
641 smc_close_init(smc);
642 smc_rx_init(smc);
643 smc_tx_init(smc);
644
645 rc = smc_clc_send_confirm(smc);
646 if (rc)
647 return smc_connect_abort(smc, rc, local_contact);
648 mutex_unlock(&smc_create_lgr_pending);
649
650 smc_copy_sock_settings_to_clc(smc);
651 if (smc->sk.sk_state == SMC_INIT)
652 smc->sk.sk_state = SMC_ACTIVE;
653
654 return 0;
655}
656
Hans Wippel3b2dec22018-05-18 09:34:18 +0200657/* perform steps before actually connecting */
658static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100659{
Hans Wippel41349842018-06-28 19:05:11 +0200660 bool ism_supported = false, rdma_supported = false;
Ursula Brauna046d572017-01-09 16:55:16 +0100661 struct smc_clc_msg_accept_confirm aclc;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200662 struct smc_ib_device *ibdev;
Hans Wippel41349842018-06-28 19:05:11 +0200663 struct smcd_dev *ismdev;
Ursula Braun7005ada2018-07-25 16:35:31 +0200664 u8 gid[SMC_GID_SIZE];
Hans Wippel41349842018-06-28 19:05:11 +0200665 unsigned short vlan;
666 int smc_type;
Ursula Brauna046d572017-01-09 16:55:16 +0100667 int rc = 0;
668 u8 ibport;
669
Ursula Braun51f1de72018-01-26 09:28:48 +0100670 sock_hold(&smc->sk); /* sock put in passive closing */
671
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200672 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +0200673 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200674
Hans Wippel3b2dec22018-05-18 09:34:18 +0200675 /* if peer has not signalled SMC-capability, fall back */
676 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +0200677 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200678
Ursula Brauna046d572017-01-09 16:55:16 +0100679 /* IPSec connections opt out of SMC-R optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200680 if (using_ipsec(smc))
681 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
Ursula Brauna046d572017-01-09 16:55:16 +0100682
Hans Wippel41349842018-06-28 19:05:11 +0200683 /* check for VLAN ID */
684 if (smc_vlan_by_tcpsk(smc->clcsock, &vlan))
685 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_CNFERR);
686
687 /* check if there is an ism device available */
688 if (!smc_check_ism(smc, &ismdev) &&
689 !smc_connect_ism_vlan_setup(smc, ismdev, vlan)) {
690 /* ISM is supported for this connection */
691 ism_supported = true;
692 smc_type = SMC_TYPE_D;
693 }
694
695 /* check if there is a rdma device available */
Ursula Braun7005ada2018-07-25 16:35:31 +0200696 if (!smc_check_rdma(smc, &ibdev, &ibport, vlan, gid)) {
Hans Wippel41349842018-06-28 19:05:11 +0200697 /* RDMA is supported for this connection */
698 rdma_supported = true;
699 if (ism_supported)
700 smc_type = SMC_TYPE_B; /* both */
701 else
702 smc_type = SMC_TYPE_R; /* only RDMA */
703 }
704
705 /* if neither ISM nor RDMA are supported, fallback */
706 if (!rdma_supported && !ism_supported)
Karsten Graul603cc142018-07-25 16:35:32 +0200707 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV);
Ursula Brauna046d572017-01-09 16:55:16 +0100708
Hans Wippel3b2dec22018-05-18 09:34:18 +0200709 /* perform CLC handshake */
Ursula Braun7005ada2018-07-25 16:35:31 +0200710 rc = smc_connect_clc(smc, smc_type, &aclc, ibdev, ibport, gid, ismdev);
Hans Wippel41349842018-06-28 19:05:11 +0200711 if (rc) {
712 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200713 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200714 }
Ursula Brauna046d572017-01-09 16:55:16 +0100715
Hans Wippel41349842018-06-28 19:05:11 +0200716 /* depending on previous steps, connect using rdma or ism */
717 if (rdma_supported && aclc.hdr.path == SMC_TYPE_R)
718 rc = smc_connect_rdma(smc, &aclc, ibdev, ibport);
719 else if (ism_supported && aclc.hdr.path == SMC_TYPE_D)
720 rc = smc_connect_ism(smc, &aclc, ismdev);
721 else
Karsten Graul603cc142018-07-25 16:35:32 +0200722 rc = SMC_CLC_DECL_MODEUNSUPP;
Hans Wippel41349842018-06-28 19:05:11 +0200723 if (rc) {
724 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200725 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200726 }
Ursula Brauna046d572017-01-09 16:55:16 +0100727
Hans Wippel41349842018-06-28 19:05:11 +0200728 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200729 return 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100730}
731
Ursula Braun24ac3a02018-06-27 17:59:50 +0200732static void smc_connect_work(struct work_struct *work)
733{
734 struct smc_sock *smc = container_of(work, struct smc_sock,
735 connect_work);
736 int rc;
737
738 lock_sock(&smc->sk);
739 rc = kernel_connect(smc->clcsock, &smc->connect_info->addr,
740 smc->connect_info->alen, smc->connect_info->flags);
741 if (smc->clcsock->sk->sk_err) {
742 smc->sk.sk_err = smc->clcsock->sk->sk_err;
743 goto out;
744 }
745 if (rc < 0) {
746 smc->sk.sk_err = -rc;
747 goto out;
748 }
749
750 rc = __smc_connect(smc);
751 if (rc < 0)
752 smc->sk.sk_err = -rc;
753
754out:
Ursula Braun648a5a72018-09-18 15:46:34 +0200755 if (smc->sk.sk_err)
756 smc->sk.sk_state_change(&smc->sk);
757 else
758 smc->sk.sk_write_space(&smc->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200759 kfree(smc->connect_info);
760 smc->connect_info = NULL;
761 release_sock(&smc->sk);
762}
763
Ursula Braunac713872017-01-09 16:55:13 +0100764static int smc_connect(struct socket *sock, struct sockaddr *addr,
765 int alen, int flags)
766{
767 struct sock *sk = sock->sk;
768 struct smc_sock *smc;
769 int rc = -EINVAL;
770
771 smc = smc_sk(sk);
772
773 /* separate smc parameter checking to be safe */
774 if (alen < sizeof(addr->sa_family))
775 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100776 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100777 goto out_err;
778
779 lock_sock(sk);
780 switch (sk->sk_state) {
781 default:
782 goto out;
783 case SMC_ACTIVE:
784 rc = -EISCONN;
785 goto out;
786 case SMC_INIT:
787 rc = 0;
788 break;
789 }
790
791 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200792 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200793 if (flags & O_NONBLOCK) {
794 if (smc->connect_info) {
795 rc = -EALREADY;
796 goto out;
797 }
798 smc->connect_info = kzalloc(alen + 2 * sizeof(int), GFP_KERNEL);
799 if (!smc->connect_info) {
800 rc = -ENOMEM;
801 goto out;
802 }
803 smc->connect_info->alen = alen;
804 smc->connect_info->flags = flags ^ O_NONBLOCK;
805 memcpy(&smc->connect_info->addr, addr, alen);
806 schedule_work(&smc->connect_work);
807 rc = -EINPROGRESS;
808 } else {
809 rc = kernel_connect(smc->clcsock, addr, alen, flags);
810 if (rc)
811 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100812
Ursula Braun24ac3a02018-06-27 17:59:50 +0200813 rc = __smc_connect(smc);
814 if (rc < 0)
815 goto out;
816 else
817 rc = 0; /* success cases including fallback */
818 }
Ursula Braunac713872017-01-09 16:55:13 +0100819
820out:
821 release_sock(sk);
822out_err:
823 return rc;
824}
825
826static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
827{
Ursula Braun3163c502018-01-24 10:28:12 +0100828 struct socket *new_clcsock = NULL;
829 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100830 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -0800831 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +0100832
Ursula Braun3163c502018-01-24 10:28:12 +0100833 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100834 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100835 if (!new_sk) {
836 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100837 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100838 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100839 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100840 goto out;
841 }
842 *new_smc = smc_sk(new_sk);
843
Myungho Jung78abe3d2018-12-18 09:02:25 -0800844 mutex_lock(&lsmc->clcsock_release_lock);
845 if (lsmc->clcsock)
846 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
847 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +0100848 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100849 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100850 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100851 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Brauna046d572017-01-09 16:55:16 +0100852 if (new_clcsock)
853 sock_release(new_clcsock);
854 new_sk->sk_state = SMC_CLOSED;
855 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun3163c502018-01-24 10:28:12 +0100856 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100857 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100858 *new_smc = NULL;
859 goto out;
860 }
861
862 (*new_smc)->clcsock = new_clcsock;
863out:
864 return rc;
865}
866
Ursula Brauna046d572017-01-09 16:55:16 +0100867/* add a just created sock to the accept queue of the listen sock as
868 * candidate for a following socket accept call from user space
869 */
870static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
871{
872 struct smc_sock *par = smc_sk(parent);
873
Ursula Braun51f1de72018-01-26 09:28:48 +0100874 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100875 spin_lock(&par->accept_q_lock);
876 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
877 spin_unlock(&par->accept_q_lock);
878 sk_acceptq_added(parent);
879}
880
881/* remove a socket from the accept queue of its parental listening socket */
882static void smc_accept_unlink(struct sock *sk)
883{
884 struct smc_sock *par = smc_sk(sk)->listen_smc;
885
886 spin_lock(&par->accept_q_lock);
887 list_del_init(&smc_sk(sk)->accept_q);
888 spin_unlock(&par->accept_q_lock);
889 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100890 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100891}
892
893/* remove a sock from the accept queue to bind it to a new socket created
894 * for a socket accept call from user space
895 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100896struct sock *smc_accept_dequeue(struct sock *parent,
897 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100898{
899 struct smc_sock *isk, *n;
900 struct sock *new_sk;
901
902 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
903 new_sk = (struct sock *)isk;
904
905 smc_accept_unlink(new_sk);
906 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braun127f4972018-01-26 09:28:49 +0100907 if (isk->clcsock) {
908 sock_release(isk->clcsock);
909 isk->clcsock = NULL;
910 }
Ursula Braun288c83902017-04-10 14:58:04 +0200911 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100912 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +0100913 continue;
914 }
915 if (new_sock)
916 sock_graft(new_sk, new_sock);
917 return new_sk;
918 }
919 return NULL;
920}
921
922/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +0100923void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +0100924{
925 struct smc_sock *smc = smc_sk(sk);
926
Ursula Braunb38d7322017-01-09 16:55:25 +0100927 lock_sock(sk);
928 if (!sk->sk_lingertime)
929 /* wait for peer closing */
930 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100931 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100932 smc_close_active(smc);
Ursula Braun288c83902017-04-10 14:58:04 +0200933 sock_set_flag(sk, SOCK_DEAD);
934 sk->sk_shutdown |= SHUTDOWN_MASK;
935 }
Ursula Brauna046d572017-01-09 16:55:16 +0100936 if (smc->clcsock) {
937 struct socket *tcp;
938
939 tcp = smc->clcsock;
940 smc->clcsock = NULL;
941 sock_release(tcp);
942 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100943 if (smc->use_fallback) {
Ursula Braun51f1de72018-01-26 09:28:48 +0100944 sock_put(sk); /* passive closing */
945 sk->sk_state = SMC_CLOSED;
946 } else {
947 if (sk->sk_state == SMC_CLOSED)
948 smc_conn_free(&smc->conn);
Ursula Braunb38d7322017-01-09 16:55:25 +0100949 }
950 release_sock(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100951 sk->sk_prot->unhash(sk);
952 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +0100953}
954
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100955static int smc_serv_conf_first_link(struct smc_sock *smc)
956{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200957 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100958 struct smc_link_group *lgr = smc->conn.lgr;
959 struct smc_link *link;
960 int rest;
961 int rc;
962
963 link = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun652a1e42017-07-28 13:56:17 +0200964
Karsten Graul44aa81c2018-05-15 17:04:55 +0200965 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200966 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200967
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100968 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200969 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100970 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200971 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100972
973 /* receive CONFIRM LINK response from client over the RoCE fabric */
974 rest = wait_for_completion_interruptible_timeout(
975 &link->llc_confirm_resp,
976 SMC_LLC_WAIT_FIRST_TIME);
977 if (rest <= 0) {
978 struct smc_clc_msg_decline dclc;
979
980 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
981 SMC_CLC_DECLINE);
Karsten Graul75d320d2018-03-01 13:51:31 +0100982 return rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100983 }
984
Karsten Graul75d320d2018-03-01 13:51:31 +0100985 if (link->llc_confirm_resp_rc)
986 return SMC_CLC_DECL_RMBE_EC;
987
Karsten Graul52bedf32018-03-01 13:51:32 +0100988 /* send ADD LINK request to client over the RoCE fabric */
989 rc = smc_llc_send_add_link(link,
990 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +0200991 link->gid, SMC_LLC_REQ);
Karsten Graul52bedf32018-03-01 13:51:32 +0100992 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200993 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +0100994
995 /* receive ADD LINK response from client over the RoCE fabric */
996 rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
997 SMC_LLC_WAIT_TIME);
998 if (rest <= 0) {
999 struct smc_clc_msg_decline dclc;
1000
1001 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
1002 SMC_CLC_DECLINE);
1003 return rc;
1004 }
1005
Karsten Graul877ae5b2018-05-02 16:56:44 +02001006 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +01001007
Karsten Graul75d320d2018-03-01 13:51:31 +01001008 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001009}
1010
Hans Wippel3b2dec22018-05-18 09:34:18 +02001011/* listen worker: finish */
1012static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001013{
Ursula Brauna046d572017-01-09 16:55:16 +01001014 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001015 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001016
Ursula Braunb38d7322017-01-09 16:55:25 +01001017 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001018 if (lsmc->sk.sk_state == SMC_LISTEN) {
1019 smc_accept_enqueue(&lsmc->sk, newsmcsk);
1020 } else { /* no longer listening */
1021 smc_close_non_accepted(newsmcsk);
1022 }
1023 release_sock(&lsmc->sk);
1024
1025 /* Wake up accept */
1026 lsmc->sk.sk_data_ready(&lsmc->sk);
1027 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001028}
Ursula Brauna046d572017-01-09 16:55:16 +01001029
Hans Wippel3b2dec22018-05-18 09:34:18 +02001030/* listen worker: finish in state connected */
1031static void smc_listen_out_connected(struct smc_sock *new_smc)
1032{
1033 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001034
Hans Wippel3b2dec22018-05-18 09:34:18 +02001035 sk_refcnt_debug_inc(newsmcsk);
1036 if (newsmcsk->sk_state == SMC_INIT)
1037 newsmcsk->sk_state = SMC_ACTIVE;
1038
1039 smc_listen_out(new_smc);
1040}
1041
1042/* listen worker: finish in error state */
1043static void smc_listen_out_err(struct smc_sock *new_smc)
1044{
1045 struct sock *newsmcsk = &new_smc->sk;
1046
Ursula Braun51f1de72018-01-26 09:28:48 +01001047 if (newsmcsk->sk_state == SMC_INIT)
1048 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001049 newsmcsk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001050 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001051
1052 smc_listen_out(new_smc);
1053}
1054
1055/* listen worker: decline and fall back if possible */
1056static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1057 int local_contact)
1058{
1059 /* RDMA setup failed, switch back to TCP */
1060 if (local_contact == SMC_FIRST_CONTACT)
1061 smc_lgr_forget(new_smc->conn.lgr);
1062 if (reason_code < 0) { /* error, no fallback possible */
1063 smc_listen_out_err(new_smc);
1064 return;
1065 }
1066 smc_conn_free(&new_smc->conn);
1067 new_smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001068 new_smc->fallback_rsn = reason_code;
1069 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001070 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1071 smc_listen_out_err(new_smc);
1072 return;
1073 }
1074 }
1075 smc_listen_out_connected(new_smc);
1076}
1077
1078/* listen worker: check prefixes */
1079static int smc_listen_rdma_check(struct smc_sock *new_smc,
1080 struct smc_clc_msg_proposal *pclc)
1081{
1082 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1083 struct socket *newclcsock = new_smc->clcsock;
1084
1085 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1086 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
1087 return SMC_CLC_DECL_CNFERR;
1088
1089 return 0;
1090}
1091
1092/* listen worker: initialize connection and buffers */
1093static int smc_listen_rdma_init(struct smc_sock *new_smc,
1094 struct smc_clc_msg_proposal *pclc,
1095 struct smc_ib_device *ibdev, u8 ibport,
1096 int *local_contact)
1097{
1098 /* allocate connection / link group */
Karsten Graulee05ff72018-11-20 16:46:40 +01001099 *local_contact = smc_conn_create(new_smc, false, 0, ibdev, ibport, 0,
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001100 &pclc->lcl, NULL, 0);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001101 if (*local_contact < 0) {
1102 if (*local_contact == -ENOMEM)
1103 return SMC_CLC_DECL_MEM;/* insufficient memory*/
1104 return SMC_CLC_DECL_INTERR; /* other error */
1105 }
1106
1107 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001108 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001109 return SMC_CLC_DECL_MEM;
1110
1111 return 0;
1112}
1113
Hans Wippel41349842018-06-28 19:05:11 +02001114/* listen worker: initialize connection and buffers for SMC-D */
1115static int smc_listen_ism_init(struct smc_sock *new_smc,
1116 struct smc_clc_msg_proposal *pclc,
1117 struct smcd_dev *ismdev,
1118 int *local_contact)
1119{
1120 struct smc_clc_msg_smcd *pclc_smcd;
1121
1122 pclc_smcd = smc_get_clc_msg_smcd(pclc);
Karsten Graulee05ff72018-11-20 16:46:40 +01001123 *local_contact = smc_conn_create(new_smc, true, 0, NULL, 0, 0, NULL,
Hans Wippel41349842018-06-28 19:05:11 +02001124 ismdev, pclc_smcd->gid);
1125 if (*local_contact < 0) {
1126 if (*local_contact == -ENOMEM)
1127 return SMC_CLC_DECL_MEM;/* insufficient memory*/
1128 return SMC_CLC_DECL_INTERR; /* other error */
1129 }
1130
1131 /* Check if peer can be reached via ISM device */
1132 if (smc_ism_cantalk(new_smc->conn.lgr->peer_gid,
1133 new_smc->conn.lgr->vlan_id,
1134 new_smc->conn.lgr->smcd)) {
1135 if (*local_contact == SMC_FIRST_CONTACT)
1136 smc_lgr_forget(new_smc->conn.lgr);
1137 smc_conn_free(&new_smc->conn);
1138 return SMC_CLC_DECL_CNFERR;
1139 }
1140
1141 /* Create send and receive buffers */
1142 if (smc_buf_create(new_smc, true)) {
1143 if (*local_contact == SMC_FIRST_CONTACT)
1144 smc_lgr_forget(new_smc->conn.lgr);
1145 smc_conn_free(&new_smc->conn);
1146 return SMC_CLC_DECL_MEM;
1147 }
1148
1149 return 0;
1150}
1151
Hans Wippel3b2dec22018-05-18 09:34:18 +02001152/* listen worker: register buffers */
1153static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
1154{
1155 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1156
1157 if (local_contact != SMC_FIRST_CONTACT) {
1158 if (!new_smc->conn.rmb_desc->reused) {
1159 if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true))
Karsten Graul603cc142018-07-25 16:35:32 +02001160 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001161 }
1162 }
1163 smc_rmb_sync_sg_for_device(&new_smc->conn);
1164
1165 return 0;
1166}
1167
1168/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001169static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1170 struct smc_clc_msg_accept_confirm *cclc,
1171 int local_contact)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001172{
1173 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1174 int reason_code = 0;
1175
1176 if (local_contact == SMC_FIRST_CONTACT)
1177 smc_link_save_peer_info(link, cclc);
1178
1179 if (smc_rmb_rtoken_handling(&new_smc->conn, cclc)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001180 reason_code = SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001181 goto decline;
1182 }
1183
1184 if (local_contact == SMC_FIRST_CONTACT) {
1185 if (smc_ib_ready_link(link)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001186 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001187 goto decline;
1188 }
1189 /* QP confirmation over RoCE fabric */
1190 reason_code = smc_serv_conf_first_link(new_smc);
1191 if (reason_code)
1192 goto decline;
1193 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001194 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001195
1196decline:
1197 mutex_unlock(&smc_create_lgr_pending);
1198 smc_listen_decline(new_smc, reason_code, local_contact);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001199 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001200}
1201
1202/* setup for RDMA connection of server */
1203static void smc_listen_work(struct work_struct *work)
1204{
1205 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1206 smc_listen_work);
1207 struct socket *newclcsock = new_smc->clcsock;
1208 struct smc_clc_msg_accept_confirm cclc;
1209 struct smc_clc_msg_proposal *pclc;
1210 struct smc_ib_device *ibdev;
Hans Wippel41349842018-06-28 19:05:11 +02001211 bool ism_supported = false;
1212 struct smcd_dev *ismdev;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001213 u8 buf[SMC_CLC_MAX_LEN];
1214 int local_contact = 0;
Ursula Braun7005ada2018-07-25 16:35:31 +02001215 unsigned short vlan;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001216 int reason_code = 0;
1217 int rc = 0;
1218 u8 ibport;
1219
1220 if (new_smc->use_fallback) {
1221 smc_listen_out_connected(new_smc);
1222 return;
1223 }
1224
1225 /* check if peer is smc capable */
1226 if (!tcp_sk(newclcsock->sk)->syn_smc) {
1227 new_smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001228 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001229 smc_listen_out_connected(new_smc);
1230 return;
1231 }
1232
1233 /* do inband token exchange -
1234 * wait for and receive SMC Proposal CLC message
1235 */
1236 pclc = (struct smc_clc_msg_proposal *)&buf;
1237 reason_code = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
1238 SMC_CLC_PROPOSAL);
1239 if (reason_code) {
1240 smc_listen_decline(new_smc, reason_code, 0);
1241 return;
1242 }
1243
1244 /* IPSec connections opt out of SMC-R optimizations */
1245 if (using_ipsec(new_smc)) {
1246 smc_listen_decline(new_smc, SMC_CLC_DECL_IPSEC, 0);
1247 return;
1248 }
1249
1250 mutex_lock(&smc_create_lgr_pending);
1251 smc_close_init(new_smc);
1252 smc_rx_init(new_smc);
1253 smc_tx_init(new_smc);
1254
Hans Wippel41349842018-06-28 19:05:11 +02001255 /* check if ISM is available */
1256 if ((pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) &&
1257 !smc_check_ism(new_smc, &ismdev) &&
1258 !smc_listen_ism_init(new_smc, pclc, ismdev, &local_contact)) {
1259 ism_supported = true;
1260 }
1261
Hans Wippel3b2dec22018-05-18 09:34:18 +02001262 /* check if RDMA is available */
Hans Wippel41349842018-06-28 19:05:11 +02001263 if (!ism_supported &&
1264 ((pclc->hdr.path != SMC_TYPE_R && pclc->hdr.path != SMC_TYPE_B) ||
Ursula Braun7005ada2018-07-25 16:35:31 +02001265 smc_vlan_by_tcpsk(new_smc->clcsock, &vlan) ||
1266 smc_check_rdma(new_smc, &ibdev, &ibport, vlan, NULL) ||
Hans Wippel41349842018-06-28 19:05:11 +02001267 smc_listen_rdma_check(new_smc, pclc) ||
1268 smc_listen_rdma_init(new_smc, pclc, ibdev, ibport,
1269 &local_contact) ||
1270 smc_listen_rdma_reg(new_smc, local_contact))) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001271 /* SMC not supported, decline */
1272 mutex_unlock(&smc_create_lgr_pending);
Karsten Graul603cc142018-07-25 16:35:32 +02001273 smc_listen_decline(new_smc, SMC_CLC_DECL_MODEUNSUPP,
1274 local_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001275 return;
1276 }
1277
1278 /* send SMC Accept CLC message */
1279 rc = smc_clc_send_accept(new_smc, local_contact);
1280 if (rc) {
1281 mutex_unlock(&smc_create_lgr_pending);
1282 smc_listen_decline(new_smc, rc, local_contact);
1283 return;
1284 }
1285
1286 /* receive SMC Confirm CLC message */
1287 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
1288 SMC_CLC_CONFIRM);
1289 if (reason_code) {
1290 mutex_unlock(&smc_create_lgr_pending);
1291 smc_listen_decline(new_smc, reason_code, local_contact);
1292 return;
1293 }
1294
1295 /* finish worker */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001296 if (!ism_supported) {
1297 if (smc_listen_rdma_finish(new_smc, &cclc, local_contact))
1298 return;
1299 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001300 smc_conn_save_peer_info(new_smc, &cclc);
1301 mutex_unlock(&smc_create_lgr_pending);
1302 smc_listen_out_connected(new_smc);
Ursula Brauna046d572017-01-09 16:55:16 +01001303}
1304
1305static void smc_tcp_listen_work(struct work_struct *work)
1306{
1307 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1308 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001309 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001310 struct smc_sock *new_smc;
1311 int rc = 0;
1312
Ursula Braun3163c502018-01-24 10:28:12 +01001313 lock_sock(lsk);
1314 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001315 rc = smc_clcsock_accept(lsmc, &new_smc);
1316 if (rc)
1317 goto out;
1318 if (!new_smc)
1319 continue;
1320
1321 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001322 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001323 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001324 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001325 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1326 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001327 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1328 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001329 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1330 if (!schedule_work(&new_smc->smc_listen_work))
1331 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001332 }
1333
1334out:
Ursula Braun3163c502018-01-24 10:28:12 +01001335 release_sock(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001336 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +01001337}
1338
Ursula Braunac713872017-01-09 16:55:13 +01001339static int smc_listen(struct socket *sock, int backlog)
1340{
1341 struct sock *sk = sock->sk;
1342 struct smc_sock *smc;
1343 int rc;
1344
1345 smc = smc_sk(sk);
1346 lock_sock(sk);
1347
1348 rc = -EINVAL;
1349 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
1350 goto out;
1351
1352 rc = 0;
1353 if (sk->sk_state == SMC_LISTEN) {
1354 sk->sk_max_ack_backlog = backlog;
1355 goto out;
1356 }
1357 /* some socket options are handled in core, so we could not apply
1358 * them to the clc socket -- copy smc socket options to clc socket
1359 */
1360 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001361 if (!smc->use_fallback)
1362 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001363
1364 rc = kernel_listen(smc->clcsock, backlog);
1365 if (rc)
1366 goto out;
1367 sk->sk_max_ack_backlog = backlog;
1368 sk->sk_ack_backlog = 0;
1369 sk->sk_state = SMC_LISTEN;
Ursula Brauna046d572017-01-09 16:55:16 +01001370 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun51f1de72018-01-26 09:28:48 +01001371 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1372 if (!schedule_work(&smc->tcp_listen_work))
1373 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001374
1375out:
1376 release_sock(sk);
1377 return rc;
1378}
1379
1380static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001381 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001382{
Ursula Brauna046d572017-01-09 16:55:16 +01001383 struct sock *sk = sock->sk, *nsk;
1384 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001385 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001386 long timeo;
1387 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001388
1389 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001390 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001391 lock_sock(sk);
1392
1393 if (lsmc->sk.sk_state != SMC_LISTEN) {
1394 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001395 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001396 goto out;
1397 }
1398
Ursula Brauna046d572017-01-09 16:55:16 +01001399 /* Wait for an incoming connection */
1400 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1401 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1402 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1403 set_current_state(TASK_INTERRUPTIBLE);
1404 if (!timeo) {
1405 rc = -EAGAIN;
1406 break;
1407 }
1408 release_sock(sk);
1409 timeo = schedule_timeout(timeo);
1410 /* wakeup by sk_data_ready in smc_listen_work() */
1411 sched_annotate_sleep();
1412 lock_sock(sk);
1413 if (signal_pending(current)) {
1414 rc = sock_intr_errno(timeo);
1415 break;
1416 }
1417 }
1418 set_current_state(TASK_RUNNING);
1419 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001420
Ursula Brauna046d572017-01-09 16:55:16 +01001421 if (!rc)
1422 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001423 release_sock(sk);
1424 if (rc)
1425 goto out;
1426
1427 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1428 /* wait till data arrives on the socket */
1429 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1430 MSEC_PER_SEC);
1431 if (smc_sk(nsk)->use_fallback) {
1432 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1433
1434 lock_sock(clcsk);
1435 if (skb_queue_empty(&clcsk->sk_receive_queue))
1436 sk_wait_data(clcsk, &timeo, NULL);
1437 release_sock(clcsk);
1438 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1439 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001440 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001441 release_sock(nsk);
1442 }
1443 }
Ursula Braunac713872017-01-09 16:55:13 +01001444
1445out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001446 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001447 return rc;
1448}
1449
1450static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001451 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001452{
1453 struct smc_sock *smc;
1454
Ursula Braunb38d7322017-01-09 16:55:25 +01001455 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1456 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001457 return -ENOTCONN;
1458
1459 smc = smc_sk(sock->sk);
1460
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001461 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001462}
1463
1464static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1465{
1466 struct sock *sk = sock->sk;
1467 struct smc_sock *smc;
1468 int rc = -EPIPE;
1469
1470 smc = smc_sk(sk);
1471 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001472 if ((sk->sk_state != SMC_ACTIVE) &&
1473 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1474 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001475 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001476
1477 if (msg->msg_flags & MSG_FASTOPEN) {
1478 if (sk->sk_state == SMC_INIT) {
1479 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001480 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001481 } else {
1482 rc = -EINVAL;
1483 goto out;
1484 }
1485 }
1486
Ursula Braunac713872017-01-09 16:55:13 +01001487 if (smc->use_fallback)
1488 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1489 else
Ursula Braune6727f32017-01-09 16:55:23 +01001490 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001491out:
1492 release_sock(sk);
1493 return rc;
1494}
1495
1496static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1497 int flags)
1498{
1499 struct sock *sk = sock->sk;
1500 struct smc_sock *smc;
1501 int rc = -ENOTCONN;
1502
1503 smc = smc_sk(sk);
1504 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001505 if ((sk->sk_state == SMC_INIT) ||
1506 (sk->sk_state == SMC_LISTEN) ||
1507 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001508 goto out;
1509
Ursula Braunb38d7322017-01-09 16:55:25 +01001510 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1511 rc = 0;
1512 goto out;
1513 }
1514
Stefan Raspl9014db22018-05-03 18:12:39 +02001515 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001516 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001517 } else {
1518 msg->msg_namelen = 0;
1519 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1520 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001521
Ursula Braunac713872017-01-09 16:55:13 +01001522out:
1523 release_sock(sk);
1524 return rc;
1525}
1526
Al Viroade994f2017-07-03 00:01:49 -04001527static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001528{
Ursula Braun8dce2782018-01-26 09:28:47 +01001529 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001530 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001531
Ursula Braun8dce2782018-01-26 09:28:47 +01001532 spin_lock(&isk->accept_q_lock);
1533 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001534 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001535 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001536
Ursula Braun8dce2782018-01-26 09:28:47 +01001537 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001538}
1539
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001540static __poll_t smc_poll(struct file *file, struct socket *sock,
1541 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01001542{
1543 struct sock *sk = sock->sk;
Al Viroe6c8adc2017-07-03 22:25:56 -04001544 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001545 struct smc_sock *smc;
1546
Ursula Braun8dce2782018-01-26 09:28:47 +01001547 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001548 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001549
Ursula Braunac713872017-01-09 16:55:13 +01001550 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02001551 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01001552 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001553 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02001554 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001555 if (sk->sk_err)
Ursula Braun784813a2018-05-02 16:53:56 +02001556 mask |= EPOLLERR;
Ursula Braunac713872017-01-09 16:55:13 +01001557 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07001558 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02001559 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01001560 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001561 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001562 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1563 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001564 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001565 if (sk->sk_state == SMC_LISTEN) {
1566 /* woken up by sk_data_ready in smc_listen_work() */
1567 mask = smc_accept_poll(sk);
1568 } else {
1569 if (atomic_read(&smc->conn.sndbuf_space) ||
1570 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001571 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001572 } else {
1573 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1574 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1575 }
1576 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001577 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001578 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001579 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001580 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001581 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02001582 if (smc->conn.urg_state == SMC_URG_VALID)
1583 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01001584 }
Ursula Braunac713872017-01-09 16:55:13 +01001585 }
1586
1587 return mask;
1588}
1589
1590static int smc_shutdown(struct socket *sock, int how)
1591{
1592 struct sock *sk = sock->sk;
1593 struct smc_sock *smc;
1594 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001595 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001596
1597 smc = smc_sk(sk);
1598
1599 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001600 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001601
1602 lock_sock(sk);
1603
1604 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02001605 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01001606 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1607 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1608 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1609 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1610 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001611 goto out;
1612 if (smc->use_fallback) {
1613 rc = kernel_sock_shutdown(smc->clcsock, how);
1614 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1615 if (sk->sk_shutdown == SHUTDOWN_MASK)
1616 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001617 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001618 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001619 switch (how) {
1620 case SHUT_RDWR: /* shutdown in both directions */
1621 rc = smc_close_active(smc);
1622 break;
1623 case SHUT_WR:
1624 rc = smc_close_shutdown_write(smc);
1625 break;
1626 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001627 rc = 0;
1628 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001629 break;
1630 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001631 if (smc->clcsock)
1632 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001633 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1634 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001635
1636out:
1637 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001638 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001639}
1640
1641static int smc_setsockopt(struct socket *sock, int level, int optname,
1642 char __user *optval, unsigned int optlen)
1643{
1644 struct sock *sk = sock->sk;
1645 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001646 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001647
1648 smc = smc_sk(sk);
1649
1650 /* generic setsockopts reaching us here always apply to the
1651 * CLC socket
1652 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001653 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1654 optval, optlen);
1655 if (smc->clcsock->sk->sk_err) {
1656 sk->sk_err = smc->clcsock->sk->sk_err;
1657 sk->sk_error_report(sk);
1658 }
1659 if (rc)
1660 return rc;
1661
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001662 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00001663 return -EINVAL;
Ursula Braunac0107e2018-07-18 15:22:50 +02001664 if (get_user(val, (int __user *)optval))
1665 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001666
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001667 lock_sock(sk);
1668 switch (optname) {
1669 case TCP_ULP:
1670 case TCP_FASTOPEN:
1671 case TCP_FASTOPEN_CONNECT:
1672 case TCP_FASTOPEN_KEY:
1673 case TCP_FASTOPEN_NO_COOKIE:
1674 /* option not supported by SMC */
1675 if (sk->sk_state == SMC_INIT) {
1676 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001677 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001678 } else {
1679 if (!smc->use_fallback)
1680 rc = -EINVAL;
1681 }
1682 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001683 case TCP_NODELAY:
1684 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001685 if (val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001686 mod_delayed_work(system_wq, &smc->conn.tx_work,
1687 0);
1688 }
1689 break;
1690 case TCP_CORK:
1691 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001692 if (!val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001693 mod_delayed_work(system_wq, &smc->conn.tx_work,
1694 0);
1695 }
1696 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001697 case TCP_DEFER_ACCEPT:
1698 smc->sockopt_defer_accept = val;
1699 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001700 default:
1701 break;
1702 }
1703 release_sock(sk);
1704
1705 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001706}
1707
1708static int smc_getsockopt(struct socket *sock, int level, int optname,
1709 char __user *optval, int __user *optlen)
1710{
1711 struct smc_sock *smc;
1712
1713 smc = smc_sk(sock->sk);
1714 /* socket options apply to the CLC socket */
1715 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1716 optval, optlen);
1717}
1718
1719static int smc_ioctl(struct socket *sock, unsigned int cmd,
1720 unsigned long arg)
1721{
Stefan Rasplde8474e2018-05-23 16:38:11 +02001722 union smc_host_cursor cons, urg;
1723 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01001724 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001725 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001726
1727 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001728 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02001729 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02001730 if (smc->use_fallback) {
1731 if (!smc->clcsock) {
1732 release_sock(&smc->sk);
1733 return -EBADF;
1734 }
1735 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1736 release_sock(&smc->sk);
1737 return answ;
1738 }
Ursula Braun9b67e262018-05-02 16:56:46 +02001739 switch (cmd) {
1740 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02001741 if (smc->sk.sk_state == SMC_LISTEN) {
1742 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001743 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001744 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001745 if (smc->sk.sk_state == SMC_INIT ||
1746 smc->sk.sk_state == SMC_CLOSED)
1747 answ = 0;
1748 else
1749 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02001750 break;
1751 case SIOCOUTQ:
1752 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02001753 if (smc->sk.sk_state == SMC_LISTEN) {
1754 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001755 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001756 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001757 if (smc->sk.sk_state == SMC_INIT ||
1758 smc->sk.sk_state == SMC_CLOSED)
1759 answ = 0;
1760 else
1761 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02001762 atomic_read(&smc->conn.sndbuf_space);
1763 break;
1764 case SIOCOUTQNSD:
1765 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02001766 if (smc->sk.sk_state == SMC_LISTEN) {
1767 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001768 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001769 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001770 if (smc->sk.sk_state == SMC_INIT ||
1771 smc->sk.sk_state == SMC_CLOSED)
1772 answ = 0;
1773 else
1774 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02001775 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02001776 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02001777 if (smc->sk.sk_state == SMC_LISTEN) {
1778 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001779 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001780 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001781 if (smc->sk.sk_state == SMC_INIT ||
1782 smc->sk.sk_state == SMC_CLOSED) {
1783 answ = 0;
1784 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02001785 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1786 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001787 answ = smc_curs_diff(conn->rmb_desc->len,
1788 &cons, &urg) == 1;
1789 }
1790 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02001791 default:
Ursula Braun1992d992018-07-16 13:56:52 +02001792 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001793 return -ENOIOCTLCMD;
1794 }
Ursula Braun1992d992018-07-16 13:56:52 +02001795 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001796
1797 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001798}
1799
1800static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1801 int offset, size_t size, int flags)
1802{
1803 struct sock *sk = sock->sk;
1804 struct smc_sock *smc;
1805 int rc = -EPIPE;
1806
1807 smc = smc_sk(sk);
1808 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001809 if (sk->sk_state != SMC_ACTIVE) {
1810 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001811 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001812 }
1813 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001814 if (smc->use_fallback)
1815 rc = kernel_sendpage(smc->clcsock, page, offset,
1816 size, flags);
1817 else
1818 rc = sock_no_sendpage(sock, page, offset, size, flags);
1819
1820out:
Ursula Braunac713872017-01-09 16:55:13 +01001821 return rc;
1822}
1823
Stefan Raspl9014db22018-05-03 18:12:39 +02001824/* Map the affected portions of the rmbe into an spd, note the number of bytes
1825 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1826 * updates till whenever a respective page has been fully processed.
1827 * Note that subsequent recv() calls have to wait till all splice() processing
1828 * completed.
1829 */
Ursula Braunac713872017-01-09 16:55:13 +01001830static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1831 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001832 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001833{
1834 struct sock *sk = sock->sk;
1835 struct smc_sock *smc;
1836 int rc = -ENOTCONN;
1837
1838 smc = smc_sk(sk);
1839 lock_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001840
1841 if (sk->sk_state == SMC_INIT ||
1842 sk->sk_state == SMC_LISTEN ||
1843 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001844 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001845
1846 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1847 rc = 0;
1848 goto out;
1849 }
1850
Ursula Braunac713872017-01-09 16:55:13 +01001851 if (smc->use_fallback) {
1852 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1853 pipe, len, flags);
1854 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001855 if (*ppos) {
1856 rc = -ESPIPE;
1857 goto out;
1858 }
1859 if (flags & SPLICE_F_NONBLOCK)
1860 flags = MSG_DONTWAIT;
1861 else
1862 flags = 0;
1863 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001864 }
1865out:
1866 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001867
Ursula Braunac713872017-01-09 16:55:13 +01001868 return rc;
1869}
1870
1871/* must look like tcp */
1872static const struct proto_ops smc_sock_ops = {
1873 .family = PF_SMC,
1874 .owner = THIS_MODULE,
1875 .release = smc_release,
1876 .bind = smc_bind,
1877 .connect = smc_connect,
1878 .socketpair = sock_no_socketpair,
1879 .accept = smc_accept,
1880 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001881 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01001882 .ioctl = smc_ioctl,
1883 .listen = smc_listen,
1884 .shutdown = smc_shutdown,
1885 .setsockopt = smc_setsockopt,
1886 .getsockopt = smc_getsockopt,
1887 .sendmsg = smc_sendmsg,
1888 .recvmsg = smc_recvmsg,
1889 .mmap = sock_no_mmap,
1890 .sendpage = smc_sendpage,
1891 .splice_read = smc_splice_read,
1892};
1893
1894static int smc_create(struct net *net, struct socket *sock, int protocol,
1895 int kern)
1896{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001897 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001898 struct smc_sock *smc;
1899 struct sock *sk;
1900 int rc;
1901
1902 rc = -ESOCKTNOSUPPORT;
1903 if (sock->type != SOCK_STREAM)
1904 goto out;
1905
1906 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001907 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01001908 goto out;
1909
1910 rc = -ENOBUFS;
1911 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001912 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001913 if (!sk)
1914 goto out;
1915
1916 /* create internal TCP socket for CLC handshake and fallback */
1917 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001918 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02001919 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001920 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1921 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01001922 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01001923 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01001924 goto out;
1925 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01001926 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1927 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01001928
1929out:
1930 return rc;
1931}
1932
1933static const struct net_proto_family smc_sock_family_ops = {
1934 .family = PF_SMC,
1935 .owner = THIS_MODULE,
1936 .create = smc_create,
1937};
1938
1939static int __init smc_init(void)
1940{
1941 int rc;
1942
Thomas Richter6812baa2017-01-09 16:55:15 +01001943 rc = smc_pnet_init();
1944 if (rc)
1945 return rc;
1946
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001947 rc = smc_llc_init();
1948 if (rc) {
1949 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1950 goto out_pnet;
1951 }
1952
Ursula Braun5f083182017-01-09 16:55:22 +01001953 rc = smc_cdc_init();
1954 if (rc) {
1955 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1956 goto out_pnet;
1957 }
1958
Ursula Braunac713872017-01-09 16:55:13 +01001959 rc = proto_register(&smc_proto, 1);
1960 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01001961 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Thomas Richter6812baa2017-01-09 16:55:15 +01001962 goto out_pnet;
Ursula Braunac713872017-01-09 16:55:13 +01001963 }
1964
Karsten Graulaaa4d332018-03-16 15:06:41 +01001965 rc = proto_register(&smc_proto6, 1);
1966 if (rc) {
1967 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
1968 goto out_proto;
1969 }
1970
Ursula Braunac713872017-01-09 16:55:13 +01001971 rc = sock_register(&smc_sock_family_ops);
1972 if (rc) {
1973 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001974 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01001975 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01001976 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001977 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01001978
Ursula Brauna4cf0442017-01-09 16:55:14 +01001979 rc = smc_ib_register_client();
1980 if (rc) {
1981 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1982 goto out_sock;
1983 }
1984
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001985 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01001986 return 0;
1987
Ursula Brauna4cf0442017-01-09 16:55:14 +01001988out_sock:
1989 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001990out_proto6:
1991 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01001992out_proto:
1993 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001994out_pnet:
1995 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001996 return rc;
1997}
1998
1999static void __exit smc_exit(void)
2000{
Hans Wippel9fda3512018-05-18 09:34:11 +02002001 smc_core_exit();
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002002 static_branch_disable(&tcp_have_smc);
Ursula Brauna4cf0442017-01-09 16:55:14 +01002003 smc_ib_unregister_client();
Ursula Braunac713872017-01-09 16:55:13 +01002004 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002005 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002006 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002007 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01002008}
2009
2010module_init(smc_init);
2011module_exit(smc_exit);
2012
2013MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2014MODULE_DESCRIPTION("smc socket address family");
2015MODULE_LICENSE("GPL");
2016MODULE_ALIAS_NETPROTO(PF_SMC);