blob: 6663a63be9e4cf008424cc99022086d179765a8c [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Ursula Braunac713872017-01-09 16:55:13 +01002/*
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
4 *
5 * AF_SMC protocol family socket handler keeping the AF_INET sock address type
6 * applies to SOCK_STREAM sockets only
7 * offers an alternative communication option for TCP-protocol sockets
8 * applicable with RoCE-cards only
9 *
Ursula Brauna046d572017-01-09 16:55:16 +010010 * Initial restrictions:
Ursula Brauna046d572017-01-09 16:55:16 +010011 * - support for alternate links postponed
Ursula Brauna046d572017-01-09 16:55:16 +010012 *
Karsten Graulaaa4d332018-03-16 15:06:41 +010013 * Copyright IBM Corp. 2016, 2018
Ursula Braunac713872017-01-09 16:55:13 +010014 *
15 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
16 * based on prototype from Frank Blaschka
17 */
18
19#define KMSG_COMPONENT "smc"
20#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
21
22#include <linux/module.h>
23#include <linux/socket.h>
Ursula Brauna046d572017-01-09 16:55:16 +010024#include <linux/workqueue.h>
Ursula Braun5f083182017-01-09 16:55:22 +010025#include <linux/in.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010026#include <linux/sched/signal.h>
Hans Wippel41349842018-06-28 19:05:11 +020027#include <linux/if_vlan.h>
Ursula Braun4ead9c92019-11-16 17:47:31 +010028#include <linux/rcupdate_wait.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010029
Ursula Braunac713872017-01-09 16:55:13 +010030#include <net/sock.h>
Ursula Brauna046d572017-01-09 16:55:16 +010031#include <net/tcp.h>
Ursula Braunf16a7dd2017-01-09 16:55:26 +010032#include <net/smc.h>
Ursula Braun9b67e262018-05-02 16:56:46 +020033#include <asm/ioctls.h>
Ursula Braunac713872017-01-09 16:55:13 +010034
Hans Wippel64e28b52019-02-21 13:01:02 +010035#include <net/net_namespace.h>
36#include <net/netns/generic.h>
37#include "smc_netns.h"
38
Ursula Braunac713872017-01-09 16:55:13 +010039#include "smc.h"
Ursula Brauna046d572017-01-09 16:55:16 +010040#include "smc_clc.h"
Ursula Braun9bf9abe2017-01-09 16:55:21 +010041#include "smc_llc.h"
Ursula Braun5f083182017-01-09 16:55:22 +010042#include "smc_cdc.h"
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010043#include "smc_core.h"
Ursula Brauna4cf0442017-01-09 16:55:14 +010044#include "smc_ib.h"
Hans Wippel41349842018-06-28 19:05:11 +020045#include "smc_ism.h"
Thomas Richter6812baa2017-01-09 16:55:15 +010046#include "smc_pnet.h"
Ursula Braune6727f32017-01-09 16:55:23 +010047#include "smc_tx.h"
Ursula Braun952310c2017-01-09 16:55:24 +010048#include "smc_rx.h"
Ursula Braunb38d7322017-01-09 16:55:25 +010049#include "smc_close.h"
Ursula Braunac713872017-01-09 16:55:13 +010050
Hans Wippel72a36a82019-02-07 15:56:18 +010051static DEFINE_MUTEX(smc_server_lgr_pending); /* serialize link group
52 * creation on server
53 */
54static DEFINE_MUTEX(smc_client_lgr_pending); /* serialize link group
55 * creation on client
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010056 */
57
Ursula Brauna046d572017-01-09 16:55:16 +010058static void smc_tcp_listen_work(struct work_struct *);
Ursula Braun24ac3a02018-06-27 17:59:50 +020059static void smc_connect_work(struct work_struct *);
Ursula Brauna046d572017-01-09 16:55:16 +010060
Ursula Braunac713872017-01-09 16:55:13 +010061static void smc_set_keepalive(struct sock *sk, int val)
62{
63 struct smc_sock *smc = smc_sk(sk);
64
65 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
66}
67
Ursula Braunf16a7dd2017-01-09 16:55:26 +010068static struct smc_hashinfo smc_v4_hashinfo = {
69 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
70};
71
Karsten Graulaaa4d332018-03-16 15:06:41 +010072static struct smc_hashinfo smc_v6_hashinfo = {
73 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
74};
75
Ursula Braunf16a7dd2017-01-09 16:55:26 +010076int smc_hash_sk(struct sock *sk)
77{
78 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
79 struct hlist_head *head;
80
81 head = &h->ht;
82
83 write_lock_bh(&h->lock);
84 sk_add_node(sk, head);
85 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
86 write_unlock_bh(&h->lock);
87
88 return 0;
89}
90EXPORT_SYMBOL_GPL(smc_hash_sk);
91
92void smc_unhash_sk(struct sock *sk)
93{
94 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
95
96 write_lock_bh(&h->lock);
97 if (sk_del_node_init(sk))
98 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
99 write_unlock_bh(&h->lock);
100}
101EXPORT_SYMBOL_GPL(smc_unhash_sk);
102
103struct proto smc_proto = {
Ursula Braunac713872017-01-09 16:55:13 +0100104 .name = "SMC",
105 .owner = THIS_MODULE,
106 .keepalive = smc_set_keepalive,
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100107 .hash = smc_hash_sk,
108 .unhash = smc_unhash_sk,
Ursula Braunac713872017-01-09 16:55:13 +0100109 .obj_size = sizeof(struct smc_sock),
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100110 .h.smc_hash = &smc_v4_hashinfo,
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800111 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Ursula Braunac713872017-01-09 16:55:13 +0100112};
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100113EXPORT_SYMBOL_GPL(smc_proto);
Ursula Braunac713872017-01-09 16:55:13 +0100114
Karsten Graulaaa4d332018-03-16 15:06:41 +0100115struct proto smc_proto6 = {
116 .name = "SMC6",
117 .owner = THIS_MODULE,
118 .keepalive = smc_set_keepalive,
119 .hash = smc_hash_sk,
120 .unhash = smc_unhash_sk,
121 .obj_size = sizeof(struct smc_sock),
122 .h.smc_hash = &smc_v6_hashinfo,
123 .slab_flags = SLAB_TYPESAFE_BY_RCU,
124};
125EXPORT_SYMBOL_GPL(smc_proto6);
126
Ursula Braunf536dff2019-10-23 15:44:05 +0200127static void smc_restore_fallback_changes(struct smc_sock *smc)
128{
129 smc->clcsock->file->private_data = smc->sk.sk_socket;
130 smc->clcsock->file = NULL;
131}
132
Ursula Braun39f41f32019-06-27 15:04:52 +0200133static int __smc_release(struct smc_sock *smc)
Ursula Braunac713872017-01-09 16:55:13 +0100134{
Ursula Braun39f41f32019-06-27 15:04:52 +0200135 struct sock *sk = &smc->sk;
Ursula Braunb38d7322017-01-09 16:55:25 +0100136 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +0100137
Ursula Braun51f1de72018-01-26 09:28:48 +0100138 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100139 rc = smc_close_active(smc);
140 sock_set_flag(sk, SOCK_DEAD);
141 sk->sk_shutdown |= SHUTDOWN_MASK;
Ursula Braunb03faa12019-02-07 15:56:15 +0100142 } else {
143 if (sk->sk_state != SMC_LISTEN && sk->sk_state != SMC_INIT)
144 sock_put(sk); /* passive closing */
145 if (sk->sk_state == SMC_LISTEN) {
146 /* wake up clcsock accept */
147 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
148 }
149 sk->sk_state = SMC_CLOSED;
150 sk->sk_state_change(sk);
Ursula Braunf536dff2019-10-23 15:44:05 +0200151 smc_restore_fallback_changes(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +0100152 }
Cong Wang26d92e92019-01-05 23:45:26 -0800153
154 sk->sk_prot->unhash(sk);
155
Ursula Braunb03faa12019-02-07 15:56:15 +0100156 if (sk->sk_state == SMC_CLOSED) {
157 if (smc->clcsock) {
Karsten Graulfd577702019-04-11 11:17:30 +0200158 release_sock(sk);
159 smc_clcsock_release(smc);
160 lock_sock(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800161 }
Ursula Braunb03faa12019-02-07 15:56:15 +0100162 if (!smc->use_fallback)
163 smc_conn_free(&smc->conn);
Ursula Braun51f1de72018-01-26 09:28:48 +0100164 }
Ursula Braunac713872017-01-09 16:55:13 +0100165
Ursula Braun39f41f32019-06-27 15:04:52 +0200166 return rc;
167}
168
169static int smc_release(struct socket *sock)
170{
171 struct sock *sk = sock->sk;
172 struct smc_sock *smc;
173 int rc = 0;
174
175 if (!sk)
176 goto out;
177
Ursula Braun81cf4f42019-10-21 16:13:15 +0200178 sock_hold(sk); /* sock_put below */
Ursula Braun39f41f32019-06-27 15:04:52 +0200179 smc = smc_sk(sk);
180
181 /* cleanup for a dangling non-blocking connect */
182 if (smc->connect_nonblock && sk->sk_state == SMC_INIT)
183 tcp_abort(smc->clcsock->sk, ECONNABORTED);
184 flush_work(&smc->connect_work);
185
186 if (sk->sk_state == SMC_LISTEN)
187 /* smc_close_non_accepted() is called and acquires
188 * sock lock for child sockets again
189 */
190 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
191 else
192 lock_sock(sk);
193
194 rc = __smc_release(smc);
195
Ursula Braunac713872017-01-09 16:55:13 +0100196 /* detach socket */
197 sock_orphan(sk);
198 sock->sk = NULL;
199 release_sock(sk);
200
Ursula Braun81cf4f42019-10-21 16:13:15 +0200201 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +0100202 sock_put(sk); /* final sock_put */
Ursula Braunac713872017-01-09 16:55:13 +0100203out:
Ursula Braunb38d7322017-01-09 16:55:25 +0100204 return rc;
Ursula Braunac713872017-01-09 16:55:13 +0100205}
206
207static void smc_destruct(struct sock *sk)
208{
209 if (sk->sk_state != SMC_CLOSED)
210 return;
211 if (!sock_flag(sk, SOCK_DEAD))
212 return;
213
214 sk_refcnt_debug_dec(sk);
215}
216
Karsten Graulaaa4d332018-03-16 15:06:41 +0100217static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
218 int protocol)
Ursula Braunac713872017-01-09 16:55:13 +0100219{
220 struct smc_sock *smc;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100221 struct proto *prot;
Ursula Braunac713872017-01-09 16:55:13 +0100222 struct sock *sk;
223
Karsten Graulaaa4d332018-03-16 15:06:41 +0100224 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
225 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
Ursula Braunac713872017-01-09 16:55:13 +0100226 if (!sk)
227 return NULL;
228
229 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
230 sk->sk_state = SMC_INIT;
231 sk->sk_destruct = smc_destruct;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100232 sk->sk_protocol = protocol;
Ursula Braunac713872017-01-09 16:55:13 +0100233 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100234 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200235 INIT_WORK(&smc->connect_work, smc_connect_work);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700236 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
Ursula Brauna046d572017-01-09 16:55:16 +0100237 INIT_LIST_HEAD(&smc->accept_q);
238 spin_lock_init(&smc->accept_q_lock);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700239 spin_lock_init(&smc->conn.send_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100240 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100241 sk_refcnt_debug_inc(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800242 mutex_init(&smc->clcsock_release_lock);
Ursula Braunac713872017-01-09 16:55:13 +0100243
244 return sk;
245}
246
247static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
248 int addr_len)
249{
250 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
251 struct sock *sk = sock->sk;
252 struct smc_sock *smc;
253 int rc;
254
255 smc = smc_sk(sk);
256
257 /* replicate tests from inet_bind(), to be safe wrt. future changes */
258 rc = -EINVAL;
259 if (addr_len < sizeof(struct sockaddr_in))
260 goto out;
261
262 rc = -EAFNOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100263 if (addr->sin_family != AF_INET &&
264 addr->sin_family != AF_INET6 &&
265 addr->sin_family != AF_UNSPEC)
266 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100267 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
Karsten Graulaaa4d332018-03-16 15:06:41 +0100268 if (addr->sin_family == AF_UNSPEC &&
269 addr->sin_addr.s_addr != htonl(INADDR_ANY))
Ursula Braunac713872017-01-09 16:55:13 +0100270 goto out;
271
272 lock_sock(sk);
273
274 /* Check if socket is already active */
275 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +0200276 if (sk->sk_state != SMC_INIT || smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +0100277 goto out_rel;
278
279 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
280 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
281
282out_rel:
283 release_sock(sk);
284out:
285 return rc;
286}
287
288static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
289 unsigned long mask)
290{
291 /* options we don't get control via setsockopt for */
292 nsk->sk_type = osk->sk_type;
293 nsk->sk_sndbuf = osk->sk_sndbuf;
294 nsk->sk_rcvbuf = osk->sk_rcvbuf;
295 nsk->sk_sndtimeo = osk->sk_sndtimeo;
296 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
297 nsk->sk_mark = osk->sk_mark;
298 nsk->sk_priority = osk->sk_priority;
299 nsk->sk_rcvlowat = osk->sk_rcvlowat;
300 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
301 nsk->sk_err = osk->sk_err;
302
303 nsk->sk_flags &= ~mask;
304 nsk->sk_flags |= osk->sk_flags & mask;
305}
306
307#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
308 (1UL << SOCK_KEEPOPEN) | \
309 (1UL << SOCK_LINGER) | \
310 (1UL << SOCK_BROADCAST) | \
311 (1UL << SOCK_TIMESTAMP) | \
312 (1UL << SOCK_DBG) | \
313 (1UL << SOCK_RCVTSTAMP) | \
314 (1UL << SOCK_RCVTSTAMPNS) | \
315 (1UL << SOCK_LOCALROUTE) | \
316 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
317 (1UL << SOCK_RXQ_OVFL) | \
318 (1UL << SOCK_WIFI_STATUS) | \
319 (1UL << SOCK_NOFCS) | \
Deepa Dinamani97184752019-02-02 07:34:51 -0800320 (1UL << SOCK_FILTER_LOCKED) | \
321 (1UL << SOCK_TSTAMP_NEW))
Ursula Braunac713872017-01-09 16:55:13 +0100322/* copy only relevant settings and flags of SOL_SOCKET level from smc to
323 * clc socket (since smc is not called for these options from net/core)
324 */
325static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
326{
327 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
328}
329
330#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
331 (1UL << SOCK_KEEPOPEN) | \
332 (1UL << SOCK_LINGER) | \
333 (1UL << SOCK_DBG))
334/* copy only settings and flags relevant for smc from clc to smc socket */
335static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
336{
337 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
338}
339
Karsten Graulb9247542020-04-29 17:10:41 +0200340/* register the new rmb on all links */
Karsten Graul7562a132020-05-01 12:48:01 +0200341static int smcr_lgr_reg_rmbs(struct smc_link *link,
Karsten Graulb9247542020-04-29 17:10:41 +0200342 struct smc_buf_desc *rmb_desc)
343{
Karsten Graul7562a132020-05-01 12:48:01 +0200344 struct smc_link_group *lgr = link->lgr;
345 int i, rc = 0;
Karsten Graulb9247542020-04-29 17:10:41 +0200346
Karsten Grauld5500662020-05-01 12:48:05 +0200347 rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY);
348 if (rc)
349 return rc;
350 /* protect against parallel smc_llc_cli_rkey_exchange() and
351 * parallel smcr_link_reg_rmb()
352 */
353 mutex_lock(&lgr->llc_conf_mutex);
Karsten Graulb9247542020-04-29 17:10:41 +0200354 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
355 if (lgr->lnk[i].state != SMC_LNK_ACTIVE)
356 continue;
Karsten Graul7562a132020-05-01 12:48:01 +0200357 rc = smcr_link_reg_rmb(&lgr->lnk[i], rmb_desc);
Karsten Graulb9247542020-04-29 17:10:41 +0200358 if (rc)
Karsten Graul7562a132020-05-01 12:48:01 +0200359 goto out;
Karsten Graulb9247542020-04-29 17:10:41 +0200360 }
Karsten Graul7562a132020-05-01 12:48:01 +0200361
362 /* exchange confirm_rkey msg with peer */
363 rc = smc_llc_do_confirm_rkey(link, rmb_desc);
364 if (rc) {
365 rc = -EFAULT;
366 goto out;
367 }
368 rmb_desc->is_conf_rkey = true;
369out:
Karsten Grauld5500662020-05-01 12:48:05 +0200370 mutex_unlock(&lgr->llc_conf_mutex);
371 smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
Karsten Graul7562a132020-05-01 12:48:01 +0200372 return rc;
Karsten Graulb9247542020-04-29 17:10:41 +0200373}
374
375static int smcr_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100376{
Karsten Graul387707f2020-04-29 17:10:40 +0200377 struct smc_link *link = smc->conn.lnk;
Karsten Graul0fb0b022020-04-30 15:55:43 +0200378 struct smc_llc_qentry *qentry;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100379 int rc;
380
Karsten Graul0fb0b022020-04-30 15:55:43 +0200381 link->lgr->type = SMC_LGR_SINGLE;
382
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100383 /* receive CONFIRM LINK request from server over RoCE fabric */
Karsten Graul0fb0b022020-04-30 15:55:43 +0200384 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
385 SMC_LLC_CONFIRM_LINK);
386 if (!qentry) {
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100387 struct smc_clc_msg_decline dclc;
388
389 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100390 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100391 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100392 }
Karsten Graul0fb0b022020-04-30 15:55:43 +0200393 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_REQ);
394 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
395 if (rc)
Karsten Graul75d320d2018-03-01 13:51:31 +0100396 return SMC_CLC_DECL_RMBE_EC;
397
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100398 rc = smc_ib_modify_qp_rts(link);
399 if (rc)
Karsten Graul603cc142018-07-25 16:35:32 +0200400 return SMC_CLC_DECL_ERR_RDYLNK;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100401
402 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200403
Karsten Graul7562a132020-05-01 12:48:01 +0200404 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +0200405 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200406
Karsten Graul0fb0b022020-04-30 15:55:43 +0200407 /* confirm_rkey is implicit on 1st contact */
408 smc->conn.rmb_desc->is_conf_rkey = true;
409
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100410 /* send CONFIRM LINK response over RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200411 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100412 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200413 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100414
Karsten Graul0fb0b022020-04-30 15:55:43 +0200415 smc_llc_link_active(link);
416
417 /* optional 2nd link, receive ADD LINK request from server */
418 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
419 SMC_LLC_ADD_LINK);
420 if (!qentry) {
Karsten Graul52bedf32018-03-01 13:51:32 +0100421 struct smc_clc_msg_decline dclc;
422
423 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100424 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200425 if (rc == -EAGAIN)
426 rc = 0; /* no DECLINE received, go with one link */
427 return rc;
Karsten Graul52bedf32018-03-01 13:51:32 +0100428 }
Karsten Graul0fb0b022020-04-30 15:55:43 +0200429 smc_llc_flow_qentry_clr(&link->lgr->llc_flow_lcl);
430 /* tbd: call smc_llc_cli_add_link(link, qentry); */
Karsten Graul75d320d2018-03-01 13:51:31 +0100431 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100432}
433
Hans Wippel41349842018-06-28 19:05:11 +0200434static void smcr_conn_save_peer_info(struct smc_sock *smc,
435 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100436{
Hans Wippel95d8d2632018-05-18 09:34:13 +0200437 int bufsize = smc_uncompress_bufsize(clc->rmbe_size);
438
Hans Wippel92a138e2018-05-18 09:34:12 +0200439 smc->conn.peer_rmbe_idx = clc->rmbe_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100440 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200441 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100442 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200443 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100444}
445
Hans Wippel41349842018-06-28 19:05:11 +0200446static void smcd_conn_save_peer_info(struct smc_sock *smc,
447 struct smc_clc_msg_accept_confirm *clc)
448{
449 int bufsize = smc_uncompress_bufsize(clc->dmbe_size);
450
451 smc->conn.peer_rmbe_idx = clc->dmbe_idx;
452 smc->conn.peer_token = clc->token;
453 /* msg header takes up space in the buffer */
454 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
455 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
456 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
457}
458
459static void smc_conn_save_peer_info(struct smc_sock *smc,
460 struct smc_clc_msg_accept_confirm *clc)
461{
462 if (smc->conn.lgr->is_smcd)
463 smcd_conn_save_peer_info(smc, clc);
464 else
465 smcr_conn_save_peer_info(smc, clc);
466}
467
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100468static void smc_link_save_peer_info(struct smc_link *link,
469 struct smc_clc_msg_accept_confirm *clc)
470{
471 link->peer_qpn = ntoh24(clc->qpn);
472 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
473 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
474 link->peer_psn = ntoh24(clc->psn);
475 link->peer_mtu = clc->qp_mtu;
476}
477
Ursula Braun07603b22019-04-11 11:17:32 +0200478static void smc_switch_to_fallback(struct smc_sock *smc)
479{
480 smc->use_fallback = true;
481 if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
482 smc->clcsock->file = smc->sk.sk_socket->file;
483 smc->clcsock->file->private_data = smc->clcsock;
Ursula Braun67f562e2020-02-14 08:58:59 +0100484 smc->clcsock->wq.fasync_list =
485 smc->sk.sk_socket->wq.fasync_list;
Ursula Braun07603b22019-04-11 11:17:32 +0200486 }
487}
488
Hans Wippel3b2dec22018-05-18 09:34:18 +0200489/* fall back during connect */
Karsten Graul603cc142018-07-25 16:35:32 +0200490static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200491{
Ursula Braun07603b22019-04-11 11:17:32 +0200492 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +0200493 smc->fallback_rsn = reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200494 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200495 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200496 if (smc->sk.sk_state == SMC_INIT)
497 smc->sk.sk_state = SMC_ACTIVE;
498 return 0;
499}
500
501/* decline and fall back during connect */
502static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
503{
504 int rc;
505
Ursula Braune1bbdd52018-07-05 16:15:30 +0200506 if (reason_code < 0) { /* error, fallback is not possible */
507 if (smc->sk.sk_state == SMC_INIT)
508 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200509 return reason_code;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200510 }
Karsten Graul603cc142018-07-25 16:35:32 +0200511 if (reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200512 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200513 if (rc < 0) {
514 if (smc->sk.sk_state == SMC_INIT)
515 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200516 return rc;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200517 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200518 }
Karsten Graul603cc142018-07-25 16:35:32 +0200519 return smc_connect_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200520}
521
522/* abort connecting */
523static int smc_connect_abort(struct smc_sock *smc, int reason_code,
524 int local_contact)
525{
Ursula Braun51e3dfa2020-02-25 16:34:36 +0100526 bool is_smcd = smc->conn.lgr->is_smcd;
527
Hans Wippel3b2dec22018-05-18 09:34:18 +0200528 if (local_contact == SMC_FIRST_CONTACT)
Ursula Braun51e3dfa2020-02-25 16:34:36 +0100529 smc_lgr_cleanup_early(&smc->conn);
530 else
531 smc_conn_free(&smc->conn);
532 if (is_smcd)
Hans Wippel72a36a82019-02-07 15:56:18 +0100533 /* there is only one lgr role for SMC-D; use server lock */
534 mutex_unlock(&smc_server_lgr_pending);
535 else
536 mutex_unlock(&smc_client_lgr_pending);
537
Ursula Braun50717a32019-04-12 12:57:23 +0200538 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200539 return reason_code;
540}
541
542/* check if there is a rdma device available for this connection. */
543/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200544static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200545{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200546 /* PNET table look up: search active ib_device and port
547 * within same PNETID that also contains the ethernet device
548 * used for the internal TCP socket
549 */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200550 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +0200551 if (!ini->ib_dev)
552 return SMC_CLC_DECL_NOSMCRDEV;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200553 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200554}
555
Hans Wippel41349842018-06-28 19:05:11 +0200556/* check if there is an ISM device available for this connection. */
557/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200558static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200559{
560 /* Find ISM device with same PNETID as connecting interface */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200561 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
562 if (!ini->ism_dev)
Karsten Graul9aa68d22019-04-12 12:57:29 +0200563 return SMC_CLC_DECL_NOSMCDDEV;
Hans Wippel41349842018-06-28 19:05:11 +0200564 return 0;
565}
566
567/* Check for VLAN ID and register it on ISM device just for CLC handshake */
568static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200569 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200570{
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200571 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev, ini->vlan_id))
Karsten Graul7a627252019-04-12 12:57:30 +0200572 return SMC_CLC_DECL_ISMVLANERR;
Hans Wippel41349842018-06-28 19:05:11 +0200573 return 0;
574}
575
576/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
577 * used, the VLAN ID will be registered again during the connection setup.
578 */
579static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200580 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200581{
582 if (!is_smcd)
583 return 0;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200584 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev, ini->vlan_id))
Hans Wippel41349842018-06-28 19:05:11 +0200585 return SMC_CLC_DECL_CNFERR;
586 return 0;
587}
588
Hans Wippel3b2dec22018-05-18 09:34:18 +0200589/* CLC handshake during connect */
Hans Wippelc758dfd2018-06-28 19:05:09 +0200590static int smc_connect_clc(struct smc_sock *smc, int smc_type,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200591 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200592 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200593{
594 int rc = 0;
595
596 /* do inband token exchange */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200597 rc = smc_clc_send_proposal(smc, smc_type, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200598 if (rc)
599 return rc;
600 /* receive SMC Accept CLC message */
Ursula Braun2b59f582018-11-22 10:26:39 +0100601 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT,
602 CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200603}
604
Ursula Brauna046d572017-01-09 16:55:16 +0100605/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200606static int smc_connect_rdma(struct smc_sock *smc,
607 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200608 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200609{
Karsten Graul0fb0b022020-04-30 15:55:43 +0200610 int i, reason_code = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200611 struct smc_link *link;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200612
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200613 ini->is_smcd = false;
614 ini->ib_lcl = &aclc->lcl;
615 ini->ib_clcqpn = ntoh24(aclc->qpn);
616 ini->srv_first_contact = aclc->hdr.flag;
617
Hans Wippel72a36a82019-02-07 15:56:18 +0100618 mutex_lock(&smc_client_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200619 reason_code = smc_conn_create(smc, ini);
620 if (reason_code) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100621 mutex_unlock(&smc_client_lgr_pending);
622 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200623 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200624
625 smc_conn_save_peer_info(smc, aclc);
626
Karsten Graul0fb0b022020-04-30 15:55:43 +0200627 if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
628 link = smc->conn.lnk;
629 } else {
630 /* set link that was assigned by server */
631 link = NULL;
632 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
633 struct smc_link *l = &smc->conn.lgr->lnk[i];
634
635 if (l->peer_qpn == ntoh24(aclc->qpn)) {
636 link = l;
637 break;
638 }
639 }
640 if (!link)
641 return smc_connect_abort(smc, SMC_CLC_DECL_NOSRVLINK,
642 ini->cln_first_contact);
643 smc->conn.lnk = link;
644 }
645
Hans Wippel3b2dec22018-05-18 09:34:18 +0200646 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200647 if (smc_buf_create(smc, false))
Karsten Graul7a627252019-04-12 12:57:30 +0200648 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
649 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200650
Karsten Graul7a627252019-04-12 12:57:30 +0200651 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200652 smc_link_save_peer_info(link, aclc);
653
Karsten Graule07d31d2020-04-29 17:10:42 +0200654 if (smc_rmb_rtoken_handling(&smc->conn, link, aclc))
Karsten Graul603cc142018-07-25 16:35:32 +0200655 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
Karsten Graul7a627252019-04-12 12:57:30 +0200656 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200657
658 smc_close_init(smc);
659 smc_rx_init(smc);
660
Karsten Graul7a627252019-04-12 12:57:30 +0200661 if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200662 if (smc_ib_ready_link(link))
Karsten Graul603cc142018-07-25 16:35:32 +0200663 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
Karsten Graul7a627252019-04-12 12:57:30 +0200664 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200665 } else {
Karsten Graul7562a132020-05-01 12:48:01 +0200666 if (smcr_lgr_reg_rmbs(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +0200667 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
Karsten Graul7a627252019-04-12 12:57:30 +0200668 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200669 }
670 smc_rmb_sync_sg_for_device(&smc->conn);
671
672 reason_code = smc_clc_send_confirm(smc);
673 if (reason_code)
Karsten Graul7a627252019-04-12 12:57:30 +0200674 return smc_connect_abort(smc, reason_code,
675 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200676
677 smc_tx_init(smc);
678
Karsten Graul7a627252019-04-12 12:57:30 +0200679 if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200680 /* QP confirmation over RoCE fabric */
Karsten Graul0fb0b022020-04-30 15:55:43 +0200681 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
Karsten Graulb9247542020-04-29 17:10:41 +0200682 reason_code = smcr_clnt_conf_first_link(smc);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200683 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200684 if (reason_code)
685 return smc_connect_abort(smc, reason_code,
Karsten Graul7a627252019-04-12 12:57:30 +0200686 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200687 }
Hans Wippel72a36a82019-02-07 15:56:18 +0100688 mutex_unlock(&smc_client_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200689
690 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200691 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200692 if (smc->sk.sk_state == SMC_INIT)
693 smc->sk.sk_state = SMC_ACTIVE;
694
695 return 0;
696}
697
Hans Wippel41349842018-06-28 19:05:11 +0200698/* setup for ISM connection of client */
699static int smc_connect_ism(struct smc_sock *smc,
700 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200701 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200702{
Hans Wippel41349842018-06-28 19:05:11 +0200703 int rc = 0;
704
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200705 ini->is_smcd = true;
706 ini->ism_gid = aclc->gid;
707 ini->srv_first_contact = aclc->hdr.flag;
708
Hans Wippel72a36a82019-02-07 15:56:18 +0100709 /* there is only one lgr role for SMC-D; use server lock */
710 mutex_lock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200711 rc = smc_conn_create(smc, ini);
712 if (rc) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100713 mutex_unlock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200714 return rc;
Hans Wippel72a36a82019-02-07 15:56:18 +0100715 }
Hans Wippel41349842018-06-28 19:05:11 +0200716
717 /* Create send and receive buffers */
718 if (smc_buf_create(smc, true))
Karsten Graul7a627252019-04-12 12:57:30 +0200719 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
720 ini->cln_first_contact);
Hans Wippel41349842018-06-28 19:05:11 +0200721
722 smc_conn_save_peer_info(smc, aclc);
723 smc_close_init(smc);
724 smc_rx_init(smc);
725 smc_tx_init(smc);
726
727 rc = smc_clc_send_confirm(smc);
728 if (rc)
Karsten Graul7a627252019-04-12 12:57:30 +0200729 return smc_connect_abort(smc, rc, ini->cln_first_contact);
Hans Wippel72a36a82019-02-07 15:56:18 +0100730 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel41349842018-06-28 19:05:11 +0200731
732 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200733 smc->connect_nonblock = 0;
Hans Wippel41349842018-06-28 19:05:11 +0200734 if (smc->sk.sk_state == SMC_INIT)
735 smc->sk.sk_state = SMC_ACTIVE;
736
737 return 0;
738}
739
Hans Wippel3b2dec22018-05-18 09:34:18 +0200740/* perform steps before actually connecting */
741static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100742{
Hans Wippel41349842018-06-28 19:05:11 +0200743 bool ism_supported = false, rdma_supported = false;
Ursula Brauna046d572017-01-09 16:55:16 +0100744 struct smc_clc_msg_accept_confirm aclc;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200745 struct smc_init_info ini = {0};
Hans Wippel41349842018-06-28 19:05:11 +0200746 int smc_type;
Ursula Brauna046d572017-01-09 16:55:16 +0100747 int rc = 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100748
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200749 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +0200750 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200751
Hans Wippel3b2dec22018-05-18 09:34:18 +0200752 /* if peer has not signalled SMC-capability, fall back */
753 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +0200754 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200755
Ursula Brauna046d572017-01-09 16:55:16 +0100756 /* IPSec connections opt out of SMC-R optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200757 if (using_ipsec(smc))
758 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
Ursula Brauna046d572017-01-09 16:55:16 +0100759
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200760 /* get vlan id from IP device */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200761 if (smc_vlan_by_tcpsk(smc->clcsock, &ini))
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200762 return smc_connect_decline_fallback(smc,
763 SMC_CLC_DECL_GETVLANERR);
Hans Wippel41349842018-06-28 19:05:11 +0200764
765 /* check if there is an ism device available */
Karsten Graul228bae02019-04-12 12:57:28 +0200766 if (!smc_find_ism_device(smc, &ini) &&
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200767 !smc_connect_ism_vlan_setup(smc, &ini)) {
Hans Wippel41349842018-06-28 19:05:11 +0200768 /* ISM is supported for this connection */
769 ism_supported = true;
770 smc_type = SMC_TYPE_D;
771 }
772
773 /* check if there is a rdma device available */
Karsten Graul228bae02019-04-12 12:57:28 +0200774 if (!smc_find_rdma_device(smc, &ini)) {
Hans Wippel41349842018-06-28 19:05:11 +0200775 /* RDMA is supported for this connection */
776 rdma_supported = true;
777 if (ism_supported)
778 smc_type = SMC_TYPE_B; /* both */
779 else
780 smc_type = SMC_TYPE_R; /* only RDMA */
781 }
782
783 /* if neither ISM nor RDMA are supported, fallback */
784 if (!rdma_supported && !ism_supported)
Karsten Graul603cc142018-07-25 16:35:32 +0200785 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV);
Ursula Brauna046d572017-01-09 16:55:16 +0100786
Hans Wippel3b2dec22018-05-18 09:34:18 +0200787 /* perform CLC handshake */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200788 rc = smc_connect_clc(smc, smc_type, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200789 if (rc) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200790 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200791 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200792 }
Ursula Brauna046d572017-01-09 16:55:16 +0100793
Hans Wippel41349842018-06-28 19:05:11 +0200794 /* depending on previous steps, connect using rdma or ism */
795 if (rdma_supported && aclc.hdr.path == SMC_TYPE_R)
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200796 rc = smc_connect_rdma(smc, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200797 else if (ism_supported && aclc.hdr.path == SMC_TYPE_D)
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200798 rc = smc_connect_ism(smc, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200799 else
Karsten Graul603cc142018-07-25 16:35:32 +0200800 rc = SMC_CLC_DECL_MODEUNSUPP;
Hans Wippel41349842018-06-28 19:05:11 +0200801 if (rc) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200802 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200803 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200804 }
Ursula Brauna046d572017-01-09 16:55:16 +0100805
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200806 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200807 return 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100808}
809
Ursula Braun24ac3a02018-06-27 17:59:50 +0200810static void smc_connect_work(struct work_struct *work)
811{
812 struct smc_sock *smc = container_of(work, struct smc_sock,
813 connect_work);
Ursula Braun50717a32019-04-12 12:57:23 +0200814 long timeo = smc->sk.sk_sndtimeo;
815 int rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200816
Ursula Braun50717a32019-04-12 12:57:23 +0200817 if (!timeo)
818 timeo = MAX_SCHEDULE_TIMEOUT;
819 lock_sock(smc->clcsock->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200820 if (smc->clcsock->sk->sk_err) {
821 smc->sk.sk_err = smc->clcsock->sk->sk_err;
Ursula Braun50717a32019-04-12 12:57:23 +0200822 } else if ((1 << smc->clcsock->sk->sk_state) &
823 (TCPF_SYN_SENT | TCP_SYN_RECV)) {
824 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
825 if ((rc == -EPIPE) &&
826 ((1 << smc->clcsock->sk->sk_state) &
827 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
828 rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200829 }
Ursula Braun50717a32019-04-12 12:57:23 +0200830 release_sock(smc->clcsock->sk);
831 lock_sock(&smc->sk);
832 if (rc != 0 || smc->sk.sk_err) {
833 smc->sk.sk_state = SMC_CLOSED;
834 if (rc == -EPIPE || rc == -EAGAIN)
835 smc->sk.sk_err = EPIPE;
836 else if (signal_pending(current))
837 smc->sk.sk_err = -sock_intr_errno(timeo);
Ursula Braun6d6dd522019-11-12 16:03:41 +0100838 sock_put(&smc->sk); /* passive closing */
Ursula Braun24ac3a02018-06-27 17:59:50 +0200839 goto out;
840 }
841
842 rc = __smc_connect(smc);
843 if (rc < 0)
844 smc->sk.sk_err = -rc;
845
846out:
Ursula Braun07603b22019-04-11 11:17:32 +0200847 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
848 if (smc->sk.sk_err) {
849 smc->sk.sk_state_change(&smc->sk);
850 } else { /* allow polling before and after fallback decision */
851 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
852 smc->sk.sk_write_space(&smc->sk);
853 }
854 }
Ursula Braun24ac3a02018-06-27 17:59:50 +0200855 release_sock(&smc->sk);
856}
857
Ursula Braunac713872017-01-09 16:55:13 +0100858static int smc_connect(struct socket *sock, struct sockaddr *addr,
859 int alen, int flags)
860{
861 struct sock *sk = sock->sk;
862 struct smc_sock *smc;
863 int rc = -EINVAL;
864
865 smc = smc_sk(sk);
866
867 /* separate smc parameter checking to be safe */
868 if (alen < sizeof(addr->sa_family))
869 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100870 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100871 goto out_err;
872
873 lock_sock(sk);
874 switch (sk->sk_state) {
875 default:
876 goto out;
877 case SMC_ACTIVE:
878 rc = -EISCONN;
879 goto out;
880 case SMC_INIT:
881 rc = 0;
882 break;
883 }
884
885 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200886 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun50717a32019-04-12 12:57:23 +0200887 if (smc->connect_nonblock) {
888 rc = -EALREADY;
889 goto out;
890 }
891 rc = kernel_connect(smc->clcsock, addr, alen, flags);
892 if (rc && rc != -EINPROGRESS)
893 goto out;
Ursula Braun301428e2019-10-29 12:41:26 +0100894
895 sock_hold(&smc->sk); /* sock put in passive closing */
Ursula Braun86434742019-12-12 22:35:58 +0100896 if (smc->use_fallback)
897 goto out;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200898 if (flags & O_NONBLOCK) {
Ursula Braun50717a32019-04-12 12:57:23 +0200899 if (schedule_work(&smc->connect_work))
900 smc->connect_nonblock = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200901 rc = -EINPROGRESS;
902 } else {
Ursula Braun24ac3a02018-06-27 17:59:50 +0200903 rc = __smc_connect(smc);
904 if (rc < 0)
905 goto out;
906 else
907 rc = 0; /* success cases including fallback */
908 }
Ursula Braunac713872017-01-09 16:55:13 +0100909
910out:
911 release_sock(sk);
912out_err:
913 return rc;
914}
915
916static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
917{
Ursula Braun3163c502018-01-24 10:28:12 +0100918 struct socket *new_clcsock = NULL;
919 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100920 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -0800921 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +0100922
Ursula Braun3163c502018-01-24 10:28:12 +0100923 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100924 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100925 if (!new_sk) {
926 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100927 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100928 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100929 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100930 goto out;
931 }
932 *new_smc = smc_sk(new_sk);
933
Myungho Jung78abe3d2018-12-18 09:02:25 -0800934 mutex_lock(&lsmc->clcsock_release_lock);
935 if (lsmc->clcsock)
936 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
937 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +0100938 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100939 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100940 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100941 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +0200942 new_sk->sk_prot->unhash(new_sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100943 if (new_clcsock)
944 sock_release(new_clcsock);
945 new_sk->sk_state = SMC_CLOSED;
946 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun51f1de72018-01-26 09:28:48 +0100947 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100948 *new_smc = NULL;
949 goto out;
950 }
951
952 (*new_smc)->clcsock = new_clcsock;
953out:
954 return rc;
955}
956
Ursula Brauna046d572017-01-09 16:55:16 +0100957/* add a just created sock to the accept queue of the listen sock as
958 * candidate for a following socket accept call from user space
959 */
960static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
961{
962 struct smc_sock *par = smc_sk(parent);
963
Ursula Braun51f1de72018-01-26 09:28:48 +0100964 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100965 spin_lock(&par->accept_q_lock);
966 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
967 spin_unlock(&par->accept_q_lock);
968 sk_acceptq_added(parent);
969}
970
971/* remove a socket from the accept queue of its parental listening socket */
972static void smc_accept_unlink(struct sock *sk)
973{
974 struct smc_sock *par = smc_sk(sk)->listen_smc;
975
976 spin_lock(&par->accept_q_lock);
977 list_del_init(&smc_sk(sk)->accept_q);
978 spin_unlock(&par->accept_q_lock);
979 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100980 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100981}
982
983/* remove a sock from the accept queue to bind it to a new socket created
984 * for a socket accept call from user space
985 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100986struct sock *smc_accept_dequeue(struct sock *parent,
987 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100988{
989 struct smc_sock *isk, *n;
990 struct sock *new_sk;
991
992 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
993 new_sk = (struct sock *)isk;
994
995 smc_accept_unlink(new_sk);
996 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +0200997 new_sk->sk_prot->unhash(new_sk);
Ursula Braun127f4972018-01-26 09:28:49 +0100998 if (isk->clcsock) {
999 sock_release(isk->clcsock);
1000 isk->clcsock = NULL;
1001 }
Ursula Braun51f1de72018-01-26 09:28:48 +01001002 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +01001003 continue;
1004 }
Ursula Braun07603b22019-04-11 11:17:32 +02001005 if (new_sock) {
Ursula Brauna046d572017-01-09 16:55:16 +01001006 sock_graft(new_sk, new_sock);
Ursula Braun07603b22019-04-11 11:17:32 +02001007 if (isk->use_fallback) {
1008 smc_sk(new_sk)->clcsock->file = new_sock->file;
1009 isk->clcsock->file->private_data = isk->clcsock;
1010 }
1011 }
Ursula Brauna046d572017-01-09 16:55:16 +01001012 return new_sk;
1013 }
1014 return NULL;
1015}
1016
1017/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +01001018void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +01001019{
1020 struct smc_sock *smc = smc_sk(sk);
1021
Ursula Braun81cf4f42019-10-21 16:13:15 +02001022 sock_hold(sk); /* sock_put below */
Ursula Braunb38d7322017-01-09 16:55:25 +01001023 lock_sock(sk);
1024 if (!sk->sk_lingertime)
1025 /* wait for peer closing */
1026 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun39f41f32019-06-27 15:04:52 +02001027 __smc_release(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +01001028 release_sock(sk);
Ursula Braun81cf4f42019-10-21 16:13:15 +02001029 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +01001030 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +01001031}
1032
Karsten Graulb9247542020-04-29 17:10:41 +02001033static int smcr_serv_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001034{
Karsten Graul387707f2020-04-29 17:10:40 +02001035 struct smc_link *link = smc->conn.lnk;
Karsten Graul4667bb42020-04-30 15:55:42 +02001036 struct smc_llc_qentry *qentry;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001037 int rc;
1038
Karsten Graul4667bb42020-04-30 15:55:42 +02001039 link->lgr->type = SMC_LGR_SINGLE;
1040
Karsten Graul7562a132020-05-01 12:48:01 +02001041 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +02001042 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +02001043
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001044 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +02001045 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001046 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001047 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001048
1049 /* receive CONFIRM LINK response from client over the RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001050 qentry = smc_llc_wait(link->lgr, link, SMC_LLC_WAIT_TIME,
1051 SMC_LLC_CONFIRM_LINK);
1052 if (!qentry) {
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001053 struct smc_clc_msg_decline dclc;
1054
1055 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001056 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001057 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001058 }
Karsten Graul4667bb42020-04-30 15:55:42 +02001059 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_RESP);
1060 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
1061 if (rc)
Karsten Graul75d320d2018-03-01 13:51:31 +01001062 return SMC_CLC_DECL_RMBE_EC;
1063
Karsten Graul4667bb42020-04-30 15:55:42 +02001064 /* confirm_rkey is implicit on 1st contact */
1065 smc->conn.rmb_desc->is_conf_rkey = true;
Karsten Graul52bedf32018-03-01 13:51:32 +01001066
Karsten Graul00a049c2020-04-29 17:10:49 +02001067 smc_llc_link_active(link);
Karsten Graul52bedf32018-03-01 13:51:32 +01001068
Karsten Graul4667bb42020-04-30 15:55:42 +02001069 /* initial contact - try to establish second link */
1070 /* tbd: call smc_llc_srv_add_link(link); */
Karsten Graul75d320d2018-03-01 13:51:31 +01001071 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001072}
1073
Hans Wippel3b2dec22018-05-18 09:34:18 +02001074/* listen worker: finish */
1075static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001076{
Ursula Brauna046d572017-01-09 16:55:16 +01001077 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001078 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001079
Ursula Brauna046d572017-01-09 16:55:16 +01001080 if (lsmc->sk.sk_state == SMC_LISTEN) {
Karsten Graulfd577702019-04-11 11:17:30 +02001081 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001082 smc_accept_enqueue(&lsmc->sk, newsmcsk);
Karsten Graulfd577702019-04-11 11:17:30 +02001083 release_sock(&lsmc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001084 } else { /* no longer listening */
1085 smc_close_non_accepted(newsmcsk);
1086 }
Ursula Brauna046d572017-01-09 16:55:16 +01001087
1088 /* Wake up accept */
1089 lsmc->sk.sk_data_ready(&lsmc->sk);
1090 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001091}
Ursula Brauna046d572017-01-09 16:55:16 +01001092
Hans Wippel3b2dec22018-05-18 09:34:18 +02001093/* listen worker: finish in state connected */
1094static void smc_listen_out_connected(struct smc_sock *new_smc)
1095{
1096 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001097
Hans Wippel3b2dec22018-05-18 09:34:18 +02001098 sk_refcnt_debug_inc(newsmcsk);
1099 if (newsmcsk->sk_state == SMC_INIT)
1100 newsmcsk->sk_state = SMC_ACTIVE;
1101
1102 smc_listen_out(new_smc);
1103}
1104
1105/* listen worker: finish in error state */
1106static void smc_listen_out_err(struct smc_sock *new_smc)
1107{
1108 struct sock *newsmcsk = &new_smc->sk;
1109
Ursula Braun51f1de72018-01-26 09:28:48 +01001110 if (newsmcsk->sk_state == SMC_INIT)
1111 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001112 newsmcsk->sk_state = SMC_CLOSED;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001113
1114 smc_listen_out(new_smc);
1115}
1116
1117/* listen worker: decline and fall back if possible */
1118static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1119 int local_contact)
1120{
1121 /* RDMA setup failed, switch back to TCP */
1122 if (local_contact == SMC_FIRST_CONTACT)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001123 smc_lgr_cleanup_early(&new_smc->conn);
1124 else
1125 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001126 if (reason_code < 0) { /* error, no fallback possible */
1127 smc_listen_out_err(new_smc);
1128 return;
1129 }
Ursula Braun07603b22019-04-11 11:17:32 +02001130 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001131 new_smc->fallback_rsn = reason_code;
1132 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001133 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1134 smc_listen_out_err(new_smc);
1135 return;
1136 }
1137 }
1138 smc_listen_out_connected(new_smc);
1139}
1140
1141/* listen worker: check prefixes */
Karsten Graul59886692019-04-12 12:57:25 +02001142static int smc_listen_prfx_check(struct smc_sock *new_smc,
Hans Wippel3b2dec22018-05-18 09:34:18 +02001143 struct smc_clc_msg_proposal *pclc)
1144{
1145 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1146 struct socket *newclcsock = new_smc->clcsock;
1147
1148 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1149 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
Karsten Graul59886692019-04-12 12:57:25 +02001150 return SMC_CLC_DECL_DIFFPREFIX;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001151
1152 return 0;
1153}
1154
1155/* listen worker: initialize connection and buffers */
1156static int smc_listen_rdma_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001157 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001158{
Karsten Graul7a627252019-04-12 12:57:30 +02001159 int rc;
1160
Hans Wippel3b2dec22018-05-18 09:34:18 +02001161 /* allocate connection / link group */
Karsten Graul7a627252019-04-12 12:57:30 +02001162 rc = smc_conn_create(new_smc, ini);
1163 if (rc)
1164 return rc;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001165
1166 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001167 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001168 return SMC_CLC_DECL_MEM;
1169
1170 return 0;
1171}
1172
Hans Wippel41349842018-06-28 19:05:11 +02001173/* listen worker: initialize connection and buffers for SMC-D */
1174static int smc_listen_ism_init(struct smc_sock *new_smc,
1175 struct smc_clc_msg_proposal *pclc,
Karsten Graul7a627252019-04-12 12:57:30 +02001176 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +02001177{
1178 struct smc_clc_msg_smcd *pclc_smcd;
Karsten Graul7a627252019-04-12 12:57:30 +02001179 int rc;
Hans Wippel41349842018-06-28 19:05:11 +02001180
1181 pclc_smcd = smc_get_clc_msg_smcd(pclc);
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001182 ini->ism_gid = pclc_smcd->gid;
Karsten Graul7a627252019-04-12 12:57:30 +02001183 rc = smc_conn_create(new_smc, ini);
1184 if (rc)
1185 return rc;
Hans Wippel41349842018-06-28 19:05:11 +02001186
1187 /* Check if peer can be reached via ISM device */
1188 if (smc_ism_cantalk(new_smc->conn.lgr->peer_gid,
1189 new_smc->conn.lgr->vlan_id,
1190 new_smc->conn.lgr->smcd)) {
Karsten Graul7a627252019-04-12 12:57:30 +02001191 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001192 smc_lgr_cleanup_early(&new_smc->conn);
1193 else
1194 smc_conn_free(&new_smc->conn);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001195 return SMC_CLC_DECL_SMCDNOTALK;
Hans Wippel41349842018-06-28 19:05:11 +02001196 }
1197
1198 /* Create send and receive buffers */
1199 if (smc_buf_create(new_smc, true)) {
Karsten Graul7a627252019-04-12 12:57:30 +02001200 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001201 smc_lgr_cleanup_early(&new_smc->conn);
1202 else
1203 smc_conn_free(&new_smc->conn);
Hans Wippel41349842018-06-28 19:05:11 +02001204 return SMC_CLC_DECL_MEM;
1205 }
1206
1207 return 0;
1208}
1209
Hans Wippel3b2dec22018-05-18 09:34:18 +02001210/* listen worker: register buffers */
1211static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
1212{
Karsten Graulb9247542020-04-29 17:10:41 +02001213 struct smc_connection *conn = &new_smc->conn;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001214
1215 if (local_contact != SMC_FIRST_CONTACT) {
Karsten Graul7562a132020-05-01 12:48:01 +02001216 if (smcr_lgr_reg_rmbs(conn->lnk, conn->rmb_desc))
Karsten Graulc7674c02018-11-22 10:26:43 +01001217 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001218 }
1219 smc_rmb_sync_sg_for_device(&new_smc->conn);
1220
1221 return 0;
1222}
1223
1224/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001225static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1226 struct smc_clc_msg_accept_confirm *cclc,
1227 int local_contact)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001228{
Karsten Graul387707f2020-04-29 17:10:40 +02001229 struct smc_link *link = new_smc->conn.lnk;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001230 int reason_code = 0;
1231
1232 if (local_contact == SMC_FIRST_CONTACT)
1233 smc_link_save_peer_info(link, cclc);
1234
Karsten Graule07d31d2020-04-29 17:10:42 +02001235 if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001236 reason_code = SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001237 goto decline;
1238 }
1239
1240 if (local_contact == SMC_FIRST_CONTACT) {
1241 if (smc_ib_ready_link(link)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001242 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001243 goto decline;
1244 }
1245 /* QP confirmation over RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001246 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
Karsten Graulb9247542020-04-29 17:10:41 +02001247 reason_code = smcr_serv_conf_first_link(new_smc);
Karsten Graul4667bb42020-04-30 15:55:42 +02001248 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001249 if (reason_code)
1250 goto decline;
1251 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001252 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001253
1254decline:
Hans Wippel3b2dec22018-05-18 09:34:18 +02001255 smc_listen_decline(new_smc, reason_code, local_contact);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001256 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001257}
1258
1259/* setup for RDMA connection of server */
1260static void smc_listen_work(struct work_struct *work)
1261{
1262 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1263 smc_listen_work);
1264 struct socket *newclcsock = new_smc->clcsock;
1265 struct smc_clc_msg_accept_confirm cclc;
1266 struct smc_clc_msg_proposal *pclc;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001267 struct smc_init_info ini = {0};
Hans Wippel41349842018-06-28 19:05:11 +02001268 bool ism_supported = false;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001269 u8 buf[SMC_CLC_MAX_LEN];
Hans Wippel3b2dec22018-05-18 09:34:18 +02001270 int rc = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001271
Karsten Graulfd577702019-04-11 11:17:30 +02001272 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1273 return smc_listen_out_err(new_smc);
1274
Hans Wippel3b2dec22018-05-18 09:34:18 +02001275 if (new_smc->use_fallback) {
1276 smc_listen_out_connected(new_smc);
1277 return;
1278 }
1279
1280 /* check if peer is smc capable */
1281 if (!tcp_sk(newclcsock->sk)->syn_smc) {
Ursula Braun07603b22019-04-11 11:17:32 +02001282 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001283 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001284 smc_listen_out_connected(new_smc);
1285 return;
1286 }
1287
1288 /* do inband token exchange -
1289 * wait for and receive SMC Proposal CLC message
1290 */
1291 pclc = (struct smc_clc_msg_proposal *)&buf;
Karsten Graul228bae02019-04-12 12:57:28 +02001292 rc = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
1293 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001294 if (rc)
1295 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001296
1297 /* IPSec connections opt out of SMC-R optimizations */
1298 if (using_ipsec(new_smc)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001299 rc = SMC_CLC_DECL_IPSEC;
1300 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001301 }
1302
Karsten Graul59886692019-04-12 12:57:25 +02001303 /* check for matching IP prefix and subnet length */
1304 rc = smc_listen_prfx_check(new_smc, pclc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001305 if (rc)
1306 goto out_decl;
Karsten Graul59886692019-04-12 12:57:25 +02001307
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001308 /* get vlan id from IP device */
1309 if (smc_vlan_by_tcpsk(new_smc->clcsock, &ini)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001310 rc = SMC_CLC_DECL_GETVLANERR;
1311 goto out_decl;
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001312 }
1313
Hans Wippel72a36a82019-02-07 15:56:18 +01001314 mutex_lock(&smc_server_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001315 smc_close_init(new_smc);
1316 smc_rx_init(new_smc);
1317 smc_tx_init(new_smc);
1318
Hans Wippel41349842018-06-28 19:05:11 +02001319 /* check if ISM is available */
Karsten Graul9aa68d22019-04-12 12:57:29 +02001320 if (pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) {
1321 ini.is_smcd = true; /* prepare ISM check */
1322 rc = smc_find_ism_device(new_smc, &ini);
1323 if (!rc)
Karsten Graul7a627252019-04-12 12:57:30 +02001324 rc = smc_listen_ism_init(new_smc, pclc, &ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001325 if (!rc)
1326 ism_supported = true;
1327 else if (pclc->hdr.path == SMC_TYPE_D)
1328 goto out_unlock; /* skip RDMA and decline */
1329 }
1330
1331 /* check if RDMA is available */
1332 if (!ism_supported) { /* SMC_TYPE_R or SMC_TYPE_B */
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001333 /* prepare RDMA check */
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001334 ini.is_smcd = false;
Ursula Braunca5f8d22019-10-23 15:44:06 +02001335 ini.ism_dev = NULL;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001336 ini.ib_lcl = &pclc->lcl;
Karsten Graul9aa68d22019-04-12 12:57:29 +02001337 rc = smc_find_rdma_device(new_smc, &ini);
1338 if (rc) {
1339 /* no RDMA device found */
1340 if (pclc->hdr.path == SMC_TYPE_B)
1341 /* neither ISM nor RDMA device found */
1342 rc = SMC_CLC_DECL_NOSMCDEV;
1343 goto out_unlock;
1344 }
Karsten Graul7a627252019-04-12 12:57:30 +02001345 rc = smc_listen_rdma_init(new_smc, &ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001346 if (rc)
1347 goto out_unlock;
Karsten Graul7a627252019-04-12 12:57:30 +02001348 rc = smc_listen_rdma_reg(new_smc, ini.cln_first_contact);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001349 if (rc)
1350 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001351 }
1352
1353 /* send SMC Accept CLC message */
Karsten Graul7a627252019-04-12 12:57:30 +02001354 rc = smc_clc_send_accept(new_smc, ini.cln_first_contact);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001355 if (rc)
1356 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001357
Hans Wippel62c71392019-02-07 15:56:17 +01001358 /* SMC-D does not need this lock any more */
1359 if (ism_supported)
Hans Wippel72a36a82019-02-07 15:56:18 +01001360 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001361
Hans Wippel3b2dec22018-05-18 09:34:18 +02001362 /* receive SMC Confirm CLC message */
Karsten Graul228bae02019-04-12 12:57:28 +02001363 rc = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
1364 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
1365 if (rc) {
Hans Wippel62c71392019-02-07 15:56:17 +01001366 if (!ism_supported)
Karsten Graul9aa68d22019-04-12 12:57:29 +02001367 goto out_unlock;
1368 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001369 }
1370
1371 /* finish worker */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001372 if (!ism_supported) {
Karsten Graul7a627252019-04-12 12:57:30 +02001373 rc = smc_listen_rdma_finish(new_smc, &cclc,
1374 ini.cln_first_contact);
Hans Wippel72a36a82019-02-07 15:56:18 +01001375 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001376 if (rc)
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001377 return;
1378 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001379 smc_conn_save_peer_info(new_smc, &cclc);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001380 smc_listen_out_connected(new_smc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001381 return;
1382
1383out_unlock:
1384 mutex_unlock(&smc_server_lgr_pending);
1385out_decl:
Karsten Graul7a627252019-04-12 12:57:30 +02001386 smc_listen_decline(new_smc, rc, ini.cln_first_contact);
Ursula Brauna046d572017-01-09 16:55:16 +01001387}
1388
1389static void smc_tcp_listen_work(struct work_struct *work)
1390{
1391 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1392 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001393 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001394 struct smc_sock *new_smc;
1395 int rc = 0;
1396
Ursula Braun3163c502018-01-24 10:28:12 +01001397 lock_sock(lsk);
1398 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001399 rc = smc_clcsock_accept(lsmc, &new_smc);
1400 if (rc)
1401 goto out;
1402 if (!new_smc)
1403 continue;
1404
1405 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001406 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001407 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001408 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001409 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1410 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001411 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1412 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001413 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1414 if (!schedule_work(&new_smc->smc_listen_work))
1415 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001416 }
1417
1418out:
Ursula Braun3163c502018-01-24 10:28:12 +01001419 release_sock(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001420 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +01001421}
1422
Ursula Braunac713872017-01-09 16:55:13 +01001423static int smc_listen(struct socket *sock, int backlog)
1424{
1425 struct sock *sk = sock->sk;
1426 struct smc_sock *smc;
1427 int rc;
1428
1429 smc = smc_sk(sk);
1430 lock_sock(sk);
1431
1432 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +02001433 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
1434 smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +01001435 goto out;
1436
1437 rc = 0;
1438 if (sk->sk_state == SMC_LISTEN) {
1439 sk->sk_max_ack_backlog = backlog;
1440 goto out;
1441 }
1442 /* some socket options are handled in core, so we could not apply
1443 * them to the clc socket -- copy smc socket options to clc socket
1444 */
1445 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001446 if (!smc->use_fallback)
1447 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001448
1449 rc = kernel_listen(smc->clcsock, backlog);
1450 if (rc)
1451 goto out;
1452 sk->sk_max_ack_backlog = backlog;
1453 sk->sk_ack_backlog = 0;
1454 sk->sk_state = SMC_LISTEN;
Ursula Braun51f1de72018-01-26 09:28:48 +01001455 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1456 if (!schedule_work(&smc->tcp_listen_work))
1457 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001458
1459out:
1460 release_sock(sk);
1461 return rc;
1462}
1463
1464static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001465 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001466{
Ursula Brauna046d572017-01-09 16:55:16 +01001467 struct sock *sk = sock->sk, *nsk;
1468 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001469 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001470 long timeo;
1471 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001472
1473 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001474 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001475 lock_sock(sk);
1476
1477 if (lsmc->sk.sk_state != SMC_LISTEN) {
1478 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001479 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001480 goto out;
1481 }
1482
Ursula Brauna046d572017-01-09 16:55:16 +01001483 /* Wait for an incoming connection */
1484 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1485 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1486 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1487 set_current_state(TASK_INTERRUPTIBLE);
1488 if (!timeo) {
1489 rc = -EAGAIN;
1490 break;
1491 }
1492 release_sock(sk);
1493 timeo = schedule_timeout(timeo);
1494 /* wakeup by sk_data_ready in smc_listen_work() */
1495 sched_annotate_sleep();
1496 lock_sock(sk);
1497 if (signal_pending(current)) {
1498 rc = sock_intr_errno(timeo);
1499 break;
1500 }
1501 }
1502 set_current_state(TASK_RUNNING);
1503 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001504
Ursula Brauna046d572017-01-09 16:55:16 +01001505 if (!rc)
1506 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001507 release_sock(sk);
1508 if (rc)
1509 goto out;
1510
1511 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1512 /* wait till data arrives on the socket */
1513 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1514 MSEC_PER_SEC);
1515 if (smc_sk(nsk)->use_fallback) {
1516 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1517
1518 lock_sock(clcsk);
1519 if (skb_queue_empty(&clcsk->sk_receive_queue))
1520 sk_wait_data(clcsk, &timeo, NULL);
1521 release_sock(clcsk);
1522 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1523 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001524 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001525 release_sock(nsk);
1526 }
1527 }
Ursula Braunac713872017-01-09 16:55:13 +01001528
1529out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001530 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001531 return rc;
1532}
1533
1534static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001535 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001536{
1537 struct smc_sock *smc;
1538
Ursula Braunb38d7322017-01-09 16:55:25 +01001539 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1540 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001541 return -ENOTCONN;
1542
1543 smc = smc_sk(sock->sk);
1544
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001545 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001546}
1547
1548static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1549{
1550 struct sock *sk = sock->sk;
1551 struct smc_sock *smc;
1552 int rc = -EPIPE;
1553
1554 smc = smc_sk(sk);
1555 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001556 if ((sk->sk_state != SMC_ACTIVE) &&
1557 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1558 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001559 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001560
1561 if (msg->msg_flags & MSG_FASTOPEN) {
Ursula Brauncd206362019-08-02 10:47:50 +02001562 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001563 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001564 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001565 } else {
1566 rc = -EINVAL;
1567 goto out;
1568 }
1569 }
1570
Ursula Braunac713872017-01-09 16:55:13 +01001571 if (smc->use_fallback)
1572 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1573 else
Ursula Braune6727f32017-01-09 16:55:23 +01001574 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001575out:
1576 release_sock(sk);
1577 return rc;
1578}
1579
1580static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1581 int flags)
1582{
1583 struct sock *sk = sock->sk;
1584 struct smc_sock *smc;
1585 int rc = -ENOTCONN;
1586
1587 smc = smc_sk(sk);
1588 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001589 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1590 /* socket was connected before, no more data to read */
1591 rc = 0;
1592 goto out;
1593 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001594 if ((sk->sk_state == SMC_INIT) ||
1595 (sk->sk_state == SMC_LISTEN) ||
1596 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001597 goto out;
1598
Ursula Braunb38d7322017-01-09 16:55:25 +01001599 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1600 rc = 0;
1601 goto out;
1602 }
1603
Stefan Raspl9014db22018-05-03 18:12:39 +02001604 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001605 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001606 } else {
1607 msg->msg_namelen = 0;
1608 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1609 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001610
Ursula Braunac713872017-01-09 16:55:13 +01001611out:
1612 release_sock(sk);
1613 return rc;
1614}
1615
Al Viroade994f2017-07-03 00:01:49 -04001616static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001617{
Ursula Braun8dce2782018-01-26 09:28:47 +01001618 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001619 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001620
Ursula Braun8dce2782018-01-26 09:28:47 +01001621 spin_lock(&isk->accept_q_lock);
1622 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001623 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001624 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001625
Ursula Braun8dce2782018-01-26 09:28:47 +01001626 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001627}
1628
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001629static __poll_t smc_poll(struct file *file, struct socket *sock,
1630 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01001631{
1632 struct sock *sk = sock->sk;
Ursula Braunac713872017-01-09 16:55:13 +01001633 struct smc_sock *smc;
Ursula Braun50717a32019-04-12 12:57:23 +02001634 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001635
Ursula Braun8dce2782018-01-26 09:28:47 +01001636 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001637 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001638
Ursula Braunac713872017-01-09 16:55:13 +01001639 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02001640 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01001641 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001642 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02001643 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braunac713872017-01-09 16:55:13 +01001644 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07001645 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02001646 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01001647 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001648 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001649 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1650 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001651 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001652 if (sk->sk_state == SMC_LISTEN) {
1653 /* woken up by sk_data_ready in smc_listen_work() */
Ursula Braun50717a32019-04-12 12:57:23 +02001654 mask |= smc_accept_poll(sk);
1655 } else if (smc->use_fallback) { /* as result of connect_work()*/
1656 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
1657 wait);
1658 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun8dce2782018-01-26 09:28:47 +01001659 } else {
Ursula Braun50717a32019-04-12 12:57:23 +02001660 if ((sk->sk_state != SMC_INIT &&
1661 atomic_read(&smc->conn.sndbuf_space)) ||
Ursula Braun8dce2782018-01-26 09:28:47 +01001662 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001663 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001664 } else {
1665 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1666 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1667 }
1668 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001669 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001670 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001671 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001672 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001673 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02001674 if (smc->conn.urg_state == SMC_URG_VALID)
1675 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01001676 }
Ursula Braunac713872017-01-09 16:55:13 +01001677 }
1678
1679 return mask;
1680}
1681
1682static int smc_shutdown(struct socket *sock, int how)
1683{
1684 struct sock *sk = sock->sk;
1685 struct smc_sock *smc;
1686 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001687 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001688
1689 smc = smc_sk(sk);
1690
1691 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001692 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001693
1694 lock_sock(sk);
1695
1696 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02001697 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01001698 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1699 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1700 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1701 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1702 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001703 goto out;
1704 if (smc->use_fallback) {
1705 rc = kernel_sock_shutdown(smc->clcsock, how);
1706 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1707 if (sk->sk_shutdown == SHUTDOWN_MASK)
1708 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001709 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001710 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001711 switch (how) {
1712 case SHUT_RDWR: /* shutdown in both directions */
1713 rc = smc_close_active(smc);
1714 break;
1715 case SHUT_WR:
1716 rc = smc_close_shutdown_write(smc);
1717 break;
1718 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001719 rc = 0;
1720 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001721 break;
1722 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001723 if (smc->clcsock)
1724 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001725 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1726 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001727
1728out:
1729 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001730 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001731}
1732
1733static int smc_setsockopt(struct socket *sock, int level, int optname,
1734 char __user *optval, unsigned int optlen)
1735{
1736 struct sock *sk = sock->sk;
1737 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001738 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001739
1740 smc = smc_sk(sk);
1741
1742 /* generic setsockopts reaching us here always apply to the
1743 * CLC socket
1744 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001745 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1746 optval, optlen);
1747 if (smc->clcsock->sk->sk_err) {
1748 sk->sk_err = smc->clcsock->sk->sk_err;
1749 sk->sk_error_report(sk);
1750 }
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001751
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001752 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00001753 return -EINVAL;
Ursula Braunac0107e2018-07-18 15:22:50 +02001754 if (get_user(val, (int __user *)optval))
1755 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001756
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001757 lock_sock(sk);
Ursula Braun86434742019-12-12 22:35:58 +01001758 if (rc || smc->use_fallback)
1759 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001760 switch (optname) {
1761 case TCP_ULP:
1762 case TCP_FASTOPEN:
1763 case TCP_FASTOPEN_CONNECT:
1764 case TCP_FASTOPEN_KEY:
1765 case TCP_FASTOPEN_NO_COOKIE:
1766 /* option not supported by SMC */
Ursula Braun8204df72019-11-15 12:39:30 +01001767 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001768 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001769 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001770 } else {
Ursula Braun86434742019-12-12 22:35:58 +01001771 rc = -EINVAL;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001772 }
1773 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001774 case TCP_NODELAY:
Ursula Braunf9cedf12019-08-02 10:16:38 +02001775 if (sk->sk_state != SMC_INIT &&
1776 sk->sk_state != SMC_LISTEN &&
1777 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01001778 if (val)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001779 mod_delayed_work(system_wq, &smc->conn.tx_work,
1780 0);
1781 }
1782 break;
1783 case TCP_CORK:
Ursula Braunf9cedf12019-08-02 10:16:38 +02001784 if (sk->sk_state != SMC_INIT &&
1785 sk->sk_state != SMC_LISTEN &&
1786 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01001787 if (!val)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001788 mod_delayed_work(system_wq, &smc->conn.tx_work,
1789 0);
1790 }
1791 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001792 case TCP_DEFER_ACCEPT:
1793 smc->sockopt_defer_accept = val;
1794 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001795 default:
1796 break;
1797 }
Ursula Braun86434742019-12-12 22:35:58 +01001798out:
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001799 release_sock(sk);
1800
1801 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001802}
1803
1804static int smc_getsockopt(struct socket *sock, int level, int optname,
1805 char __user *optval, int __user *optlen)
1806{
1807 struct smc_sock *smc;
1808
1809 smc = smc_sk(sock->sk);
1810 /* socket options apply to the CLC socket */
1811 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1812 optval, optlen);
1813}
1814
1815static int smc_ioctl(struct socket *sock, unsigned int cmd,
1816 unsigned long arg)
1817{
Stefan Rasplde8474e2018-05-23 16:38:11 +02001818 union smc_host_cursor cons, urg;
1819 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01001820 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001821 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001822
1823 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001824 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02001825 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02001826 if (smc->use_fallback) {
1827 if (!smc->clcsock) {
1828 release_sock(&smc->sk);
1829 return -EBADF;
1830 }
1831 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1832 release_sock(&smc->sk);
1833 return answ;
1834 }
Ursula Braun9b67e262018-05-02 16:56:46 +02001835 switch (cmd) {
1836 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02001837 if (smc->sk.sk_state == SMC_LISTEN) {
1838 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001839 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001840 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001841 if (smc->sk.sk_state == SMC_INIT ||
1842 smc->sk.sk_state == SMC_CLOSED)
1843 answ = 0;
1844 else
1845 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02001846 break;
1847 case SIOCOUTQ:
1848 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02001849 if (smc->sk.sk_state == SMC_LISTEN) {
1850 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001851 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001852 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001853 if (smc->sk.sk_state == SMC_INIT ||
1854 smc->sk.sk_state == SMC_CLOSED)
1855 answ = 0;
1856 else
1857 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02001858 atomic_read(&smc->conn.sndbuf_space);
1859 break;
1860 case SIOCOUTQNSD:
1861 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02001862 if (smc->sk.sk_state == SMC_LISTEN) {
1863 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001864 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001865 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001866 if (smc->sk.sk_state == SMC_INIT ||
1867 smc->sk.sk_state == SMC_CLOSED)
1868 answ = 0;
1869 else
1870 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02001871 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02001872 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02001873 if (smc->sk.sk_state == SMC_LISTEN) {
1874 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001875 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001876 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001877 if (smc->sk.sk_state == SMC_INIT ||
1878 smc->sk.sk_state == SMC_CLOSED) {
1879 answ = 0;
1880 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02001881 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1882 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001883 answ = smc_curs_diff(conn->rmb_desc->len,
1884 &cons, &urg) == 1;
1885 }
1886 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02001887 default:
Ursula Braun1992d992018-07-16 13:56:52 +02001888 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001889 return -ENOIOCTLCMD;
1890 }
Ursula Braun1992d992018-07-16 13:56:52 +02001891 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001892
1893 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001894}
1895
1896static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1897 int offset, size_t size, int flags)
1898{
1899 struct sock *sk = sock->sk;
1900 struct smc_sock *smc;
1901 int rc = -EPIPE;
1902
1903 smc = smc_sk(sk);
1904 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001905 if (sk->sk_state != SMC_ACTIVE) {
1906 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001907 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001908 }
1909 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001910 if (smc->use_fallback)
1911 rc = kernel_sendpage(smc->clcsock, page, offset,
1912 size, flags);
1913 else
1914 rc = sock_no_sendpage(sock, page, offset, size, flags);
1915
1916out:
Ursula Braunac713872017-01-09 16:55:13 +01001917 return rc;
1918}
1919
Stefan Raspl9014db22018-05-03 18:12:39 +02001920/* Map the affected portions of the rmbe into an spd, note the number of bytes
1921 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1922 * updates till whenever a respective page has been fully processed.
1923 * Note that subsequent recv() calls have to wait till all splice() processing
1924 * completed.
1925 */
Ursula Braunac713872017-01-09 16:55:13 +01001926static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1927 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001928 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001929{
1930 struct sock *sk = sock->sk;
1931 struct smc_sock *smc;
1932 int rc = -ENOTCONN;
1933
1934 smc = smc_sk(sk);
1935 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001936 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1937 /* socket was connected before, no more data to read */
1938 rc = 0;
1939 goto out;
1940 }
Stefan Raspl9014db22018-05-03 18:12:39 +02001941 if (sk->sk_state == SMC_INIT ||
1942 sk->sk_state == SMC_LISTEN ||
1943 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001944 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001945
1946 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1947 rc = 0;
1948 goto out;
1949 }
1950
Ursula Braunac713872017-01-09 16:55:13 +01001951 if (smc->use_fallback) {
1952 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1953 pipe, len, flags);
1954 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001955 if (*ppos) {
1956 rc = -ESPIPE;
1957 goto out;
1958 }
1959 if (flags & SPLICE_F_NONBLOCK)
1960 flags = MSG_DONTWAIT;
1961 else
1962 flags = 0;
1963 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001964 }
1965out:
1966 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001967
Ursula Braunac713872017-01-09 16:55:13 +01001968 return rc;
1969}
1970
1971/* must look like tcp */
1972static const struct proto_ops smc_sock_ops = {
1973 .family = PF_SMC,
1974 .owner = THIS_MODULE,
1975 .release = smc_release,
1976 .bind = smc_bind,
1977 .connect = smc_connect,
1978 .socketpair = sock_no_socketpair,
1979 .accept = smc_accept,
1980 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001981 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01001982 .ioctl = smc_ioctl,
1983 .listen = smc_listen,
1984 .shutdown = smc_shutdown,
1985 .setsockopt = smc_setsockopt,
1986 .getsockopt = smc_getsockopt,
1987 .sendmsg = smc_sendmsg,
1988 .recvmsg = smc_recvmsg,
1989 .mmap = sock_no_mmap,
1990 .sendpage = smc_sendpage,
1991 .splice_read = smc_splice_read,
1992};
1993
1994static int smc_create(struct net *net, struct socket *sock, int protocol,
1995 int kern)
1996{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001997 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001998 struct smc_sock *smc;
1999 struct sock *sk;
2000 int rc;
2001
2002 rc = -ESOCKTNOSUPPORT;
2003 if (sock->type != SOCK_STREAM)
2004 goto out;
2005
2006 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002007 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01002008 goto out;
2009
2010 rc = -ENOBUFS;
2011 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002012 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01002013 if (!sk)
2014 goto out;
2015
2016 /* create internal TCP socket for CLC handshake and fallback */
2017 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01002018 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02002019 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002020 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
2021 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01002022 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01002023 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01002024 goto out;
2025 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01002026 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
2027 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01002028
2029out:
2030 return rc;
2031}
2032
2033static const struct net_proto_family smc_sock_family_ops = {
2034 .family = PF_SMC,
2035 .owner = THIS_MODULE,
2036 .create = smc_create,
2037};
2038
Hans Wippel64e28b52019-02-21 13:01:02 +01002039unsigned int smc_net_id;
2040
2041static __net_init int smc_net_init(struct net *net)
2042{
2043 return smc_pnet_net_init(net);
2044}
2045
2046static void __net_exit smc_net_exit(struct net *net)
2047{
2048 smc_pnet_net_exit(net);
2049}
2050
2051static struct pernet_operations smc_net_ops = {
2052 .init = smc_net_init,
2053 .exit = smc_net_exit,
2054 .id = &smc_net_id,
2055 .size = sizeof(struct smc_net),
2056};
2057
Ursula Braunac713872017-01-09 16:55:13 +01002058static int __init smc_init(void)
2059{
2060 int rc;
2061
Hans Wippel64e28b52019-02-21 13:01:02 +01002062 rc = register_pernet_subsys(&smc_net_ops);
2063 if (rc)
2064 return rc;
2065
Thomas Richter6812baa2017-01-09 16:55:15 +01002066 rc = smc_pnet_init();
2067 if (rc)
YueHaibing8c33bf12019-06-26 17:47:50 +02002068 goto out_pernet_subsys;
Thomas Richter6812baa2017-01-09 16:55:15 +01002069
Ursula Braun6dabd402019-11-16 17:47:29 +01002070 rc = smc_core_init();
2071 if (rc) {
2072 pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
2073 goto out_pnet;
2074 }
2075
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002076 rc = smc_llc_init();
2077 if (rc) {
2078 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002079 goto out_core;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002080 }
2081
Ursula Braun5f083182017-01-09 16:55:22 +01002082 rc = smc_cdc_init();
2083 if (rc) {
2084 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002085 goto out_core;
Ursula Braun5f083182017-01-09 16:55:22 +01002086 }
2087
Ursula Braunac713872017-01-09 16:55:13 +01002088 rc = proto_register(&smc_proto, 1);
2089 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01002090 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002091 goto out_core;
Ursula Braunac713872017-01-09 16:55:13 +01002092 }
2093
Karsten Graulaaa4d332018-03-16 15:06:41 +01002094 rc = proto_register(&smc_proto6, 1);
2095 if (rc) {
2096 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2097 goto out_proto;
2098 }
2099
Ursula Braunac713872017-01-09 16:55:13 +01002100 rc = sock_register(&smc_sock_family_ops);
2101 if (rc) {
2102 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002103 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01002104 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01002105 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002106 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01002107
Ursula Brauna4cf0442017-01-09 16:55:14 +01002108 rc = smc_ib_register_client();
2109 if (rc) {
2110 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2111 goto out_sock;
2112 }
2113
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002114 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002115 return 0;
2116
Ursula Brauna4cf0442017-01-09 16:55:14 +01002117out_sock:
2118 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002119out_proto6:
2120 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002121out_proto:
2122 proto_unregister(&smc_proto);
Ursula Braun6dabd402019-11-16 17:47:29 +01002123out_core:
2124 smc_core_exit();
Thomas Richter6812baa2017-01-09 16:55:15 +01002125out_pnet:
2126 smc_pnet_exit();
YueHaibing8c33bf12019-06-26 17:47:50 +02002127out_pernet_subsys:
2128 unregister_pernet_subsys(&smc_net_ops);
2129
Ursula Braunac713872017-01-09 16:55:13 +01002130 return rc;
2131}
2132
2133static void __exit smc_exit(void)
2134{
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002135 static_branch_disable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002136 sock_unregister(PF_SMC);
Ursula Braun6dabd402019-11-16 17:47:29 +01002137 smc_core_exit();
2138 smc_ib_unregister_client();
Karsten Graulaaa4d332018-03-16 15:06:41 +01002139 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002140 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002141 smc_pnet_exit();
Hans Wippel64e28b52019-02-21 13:01:02 +01002142 unregister_pernet_subsys(&smc_net_ops);
Ursula Braun4ead9c92019-11-16 17:47:31 +01002143 rcu_barrier();
Ursula Braunac713872017-01-09 16:55:13 +01002144}
2145
2146module_init(smc_init);
2147module_exit(smc_exit);
2148
2149MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2150MODULE_DESCRIPTION("smc socket address family");
2151MODULE_LICENSE("GPL");
2152MODULE_ALIAS_NETPROTO(PF_SMC);