blob: 961854e56736642cbec3d323296d26fd905aff78 [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>
Ursula Braunb81a5eb2020-09-26 12:44:31 +020029#include <linux/ctype.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010030
Ursula Braunac713872017-01-09 16:55:13 +010031#include <net/sock.h>
Ursula Brauna046d572017-01-09 16:55:16 +010032#include <net/tcp.h>
Ursula Braunf16a7dd2017-01-09 16:55:26 +010033#include <net/smc.h>
Ursula Braun9b67e262018-05-02 16:56:46 +020034#include <asm/ioctls.h>
Ursula Braunac713872017-01-09 16:55:13 +010035
Hans Wippel64e28b52019-02-21 13:01:02 +010036#include <net/net_namespace.h>
37#include <net/netns/generic.h>
38#include "smc_netns.h"
39
Ursula Braunac713872017-01-09 16:55:13 +010040#include "smc.h"
Ursula Brauna046d572017-01-09 16:55:16 +010041#include "smc_clc.h"
Ursula Braun9bf9abe2017-01-09 16:55:21 +010042#include "smc_llc.h"
Ursula Braun5f083182017-01-09 16:55:22 +010043#include "smc_cdc.h"
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010044#include "smc_core.h"
Ursula Brauna4cf0442017-01-09 16:55:14 +010045#include "smc_ib.h"
Hans Wippel41349842018-06-28 19:05:11 +020046#include "smc_ism.h"
Thomas Richter6812baa2017-01-09 16:55:15 +010047#include "smc_pnet.h"
Guvenc Gulcee8372d92020-12-01 20:20:43 +010048#include "smc_netlink.h"
Ursula Braune6727f32017-01-09 16:55:23 +010049#include "smc_tx.h"
Ursula Braun952310c2017-01-09 16:55:24 +010050#include "smc_rx.h"
Ursula Braunb38d7322017-01-09 16:55:25 +010051#include "smc_close.h"
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +020052#include "smc_stats.h"
Tony Lu48262602021-11-01 15:39:12 +080053#include "smc_tracepoint.h"
Ursula Braunac713872017-01-09 16:55:13 +010054
Hans Wippel72a36a82019-02-07 15:56:18 +010055static DEFINE_MUTEX(smc_server_lgr_pending); /* serialize link group
56 * creation on server
57 */
58static DEFINE_MUTEX(smc_client_lgr_pending); /* serialize link group
59 * creation on client
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010060 */
61
Karsten Graul22ef4732020-09-10 18:48:29 +020062struct workqueue_struct *smc_hs_wq; /* wq for handshake work */
63struct workqueue_struct *smc_close_wq; /* wq for close work */
64
Ursula Brauna046d572017-01-09 16:55:16 +010065static void smc_tcp_listen_work(struct work_struct *);
Ursula Braun24ac3a02018-06-27 17:59:50 +020066static void smc_connect_work(struct work_struct *);
Ursula Brauna046d572017-01-09 16:55:16 +010067
Ursula Braunac713872017-01-09 16:55:13 +010068static void smc_set_keepalive(struct sock *sk, int val)
69{
70 struct smc_sock *smc = smc_sk(sk);
71
72 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
73}
74
Ursula Braunf16a7dd2017-01-09 16:55:26 +010075static struct smc_hashinfo smc_v4_hashinfo = {
76 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
77};
78
Karsten Graulaaa4d332018-03-16 15:06:41 +010079static struct smc_hashinfo smc_v6_hashinfo = {
80 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
81};
82
Ursula Braunf16a7dd2017-01-09 16:55:26 +010083int smc_hash_sk(struct sock *sk)
84{
85 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
86 struct hlist_head *head;
87
88 head = &h->ht;
89
90 write_lock_bh(&h->lock);
91 sk_add_node(sk, head);
Ursula Braunf16a7dd2017-01-09 16:55:26 +010092 write_unlock_bh(&h->lock);
Eric Dumazetb3cb7642021-11-15 09:11:50 -080093 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
Ursula Braunf16a7dd2017-01-09 16:55:26 +010094
95 return 0;
96}
97EXPORT_SYMBOL_GPL(smc_hash_sk);
98
99void smc_unhash_sk(struct sock *sk)
100{
101 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
102
103 write_lock_bh(&h->lock);
104 if (sk_del_node_init(sk))
105 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
106 write_unlock_bh(&h->lock);
107}
108EXPORT_SYMBOL_GPL(smc_unhash_sk);
109
110struct proto smc_proto = {
Ursula Braunac713872017-01-09 16:55:13 +0100111 .name = "SMC",
112 .owner = THIS_MODULE,
113 .keepalive = smc_set_keepalive,
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100114 .hash = smc_hash_sk,
115 .unhash = smc_unhash_sk,
Ursula Braunac713872017-01-09 16:55:13 +0100116 .obj_size = sizeof(struct smc_sock),
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100117 .h.smc_hash = &smc_v4_hashinfo,
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800118 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Ursula Braunac713872017-01-09 16:55:13 +0100119};
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100120EXPORT_SYMBOL_GPL(smc_proto);
Ursula Braunac713872017-01-09 16:55:13 +0100121
Karsten Graulaaa4d332018-03-16 15:06:41 +0100122struct proto smc_proto6 = {
123 .name = "SMC6",
124 .owner = THIS_MODULE,
125 .keepalive = smc_set_keepalive,
126 .hash = smc_hash_sk,
127 .unhash = smc_unhash_sk,
128 .obj_size = sizeof(struct smc_sock),
129 .h.smc_hash = &smc_v6_hashinfo,
130 .slab_flags = SLAB_TYPESAFE_BY_RCU,
131};
132EXPORT_SYMBOL_GPL(smc_proto6);
133
Ursula Braunf536dff2019-10-23 15:44:05 +0200134static void smc_restore_fallback_changes(struct smc_sock *smc)
135{
Karsten Graul1ad24052020-07-18 15:06:18 +0200136 if (smc->clcsock->file) { /* non-accepted sockets have no file yet */
137 smc->clcsock->file->private_data = smc->sk.sk_socket;
138 smc->clcsock->file = NULL;
139 }
Ursula Braunf536dff2019-10-23 15:44:05 +0200140}
141
Ursula Braun39f41f32019-06-27 15:04:52 +0200142static int __smc_release(struct smc_sock *smc)
Ursula Braunac713872017-01-09 16:55:13 +0100143{
Ursula Braun39f41f32019-06-27 15:04:52 +0200144 struct sock *sk = &smc->sk;
Ursula Braunb38d7322017-01-09 16:55:25 +0100145 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +0100146
Ursula Braun51f1de72018-01-26 09:28:48 +0100147 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100148 rc = smc_close_active(smc);
149 sock_set_flag(sk, SOCK_DEAD);
150 sk->sk_shutdown |= SHUTDOWN_MASK;
Ursula Braunb03faa12019-02-07 15:56:15 +0100151 } else {
Dust Lie5d5aad2021-11-10 15:02:34 +0800152 if (sk->sk_state != SMC_CLOSED) {
153 if (sk->sk_state != SMC_LISTEN &&
154 sk->sk_state != SMC_INIT)
155 sock_put(sk); /* passive closing */
156 if (sk->sk_state == SMC_LISTEN) {
157 /* wake up clcsock accept */
158 rc = kernel_sock_shutdown(smc->clcsock,
159 SHUT_RDWR);
160 }
161 sk->sk_state = SMC_CLOSED;
162 sk->sk_state_change(sk);
Ursula Braunb03faa12019-02-07 15:56:15 +0100163 }
Ursula Braunf536dff2019-10-23 15:44:05 +0200164 smc_restore_fallback_changes(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +0100165 }
Cong Wang26d92e92019-01-05 23:45:26 -0800166
167 sk->sk_prot->unhash(sk);
168
Ursula Braunb03faa12019-02-07 15:56:15 +0100169 if (sk->sk_state == SMC_CLOSED) {
170 if (smc->clcsock) {
Karsten Graulfd577702019-04-11 11:17:30 +0200171 release_sock(sk);
172 smc_clcsock_release(smc);
173 lock_sock(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800174 }
Ursula Braunb03faa12019-02-07 15:56:15 +0100175 if (!smc->use_fallback)
176 smc_conn_free(&smc->conn);
Ursula Braun51f1de72018-01-26 09:28:48 +0100177 }
Ursula Braunac713872017-01-09 16:55:13 +0100178
Ursula Braun39f41f32019-06-27 15:04:52 +0200179 return rc;
180}
181
182static int smc_release(struct socket *sock)
183{
184 struct sock *sk = sock->sk;
185 struct smc_sock *smc;
186 int rc = 0;
187
188 if (!sk)
189 goto out;
190
Ursula Braun81cf4f42019-10-21 16:13:15 +0200191 sock_hold(sk); /* sock_put below */
Ursula Braun39f41f32019-06-27 15:04:52 +0200192 smc = smc_sk(sk);
193
194 /* cleanup for a dangling non-blocking connect */
195 if (smc->connect_nonblock && sk->sk_state == SMC_INIT)
196 tcp_abort(smc->clcsock->sk, ECONNABORTED);
D. Wythe5c15b312021-12-15 20:29:21 +0800197
198 if (cancel_work_sync(&smc->connect_work))
199 sock_put(&smc->sk); /* sock_hold in smc_connect for passive closing */
Ursula Braun39f41f32019-06-27 15:04:52 +0200200
201 if (sk->sk_state == SMC_LISTEN)
202 /* smc_close_non_accepted() is called and acquires
203 * sock lock for child sockets again
204 */
205 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
206 else
207 lock_sock(sk);
208
209 rc = __smc_release(smc);
210
Ursula Braunac713872017-01-09 16:55:13 +0100211 /* detach socket */
212 sock_orphan(sk);
213 sock->sk = NULL;
214 release_sock(sk);
215
Ursula Braun81cf4f42019-10-21 16:13:15 +0200216 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +0100217 sock_put(sk); /* final sock_put */
Ursula Braunac713872017-01-09 16:55:13 +0100218out:
Ursula Braunb38d7322017-01-09 16:55:25 +0100219 return rc;
Ursula Braunac713872017-01-09 16:55:13 +0100220}
221
222static void smc_destruct(struct sock *sk)
223{
224 if (sk->sk_state != SMC_CLOSED)
225 return;
226 if (!sock_flag(sk, SOCK_DEAD))
227 return;
228
229 sk_refcnt_debug_dec(sk);
230}
231
Karsten Graulaaa4d332018-03-16 15:06:41 +0100232static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
233 int protocol)
Ursula Braunac713872017-01-09 16:55:13 +0100234{
235 struct smc_sock *smc;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100236 struct proto *prot;
Ursula Braunac713872017-01-09 16:55:13 +0100237 struct sock *sk;
238
Karsten Graulaaa4d332018-03-16 15:06:41 +0100239 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
240 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
Ursula Braunac713872017-01-09 16:55:13 +0100241 if (!sk)
242 return NULL;
243
244 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
245 sk->sk_state = SMC_INIT;
246 sk->sk_destruct = smc_destruct;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100247 sk->sk_protocol = protocol;
Ursula Braunac713872017-01-09 16:55:13 +0100248 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100249 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200250 INIT_WORK(&smc->connect_work, smc_connect_work);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700251 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
Ursula Brauna046d572017-01-09 16:55:16 +0100252 INIT_LIST_HEAD(&smc->accept_q);
253 spin_lock_init(&smc->accept_q_lock);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700254 spin_lock_init(&smc->conn.send_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100255 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100256 sk_refcnt_debug_inc(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800257 mutex_init(&smc->clcsock_release_lock);
Ursula Braunac713872017-01-09 16:55:13 +0100258
259 return sk;
260}
261
262static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
263 int addr_len)
264{
265 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
266 struct sock *sk = sock->sk;
267 struct smc_sock *smc;
268 int rc;
269
270 smc = smc_sk(sk);
271
272 /* replicate tests from inet_bind(), to be safe wrt. future changes */
273 rc = -EINVAL;
274 if (addr_len < sizeof(struct sockaddr_in))
275 goto out;
276
277 rc = -EAFNOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100278 if (addr->sin_family != AF_INET &&
279 addr->sin_family != AF_INET6 &&
280 addr->sin_family != AF_UNSPEC)
281 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100282 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
Karsten Graulaaa4d332018-03-16 15:06:41 +0100283 if (addr->sin_family == AF_UNSPEC &&
284 addr->sin_addr.s_addr != htonl(INADDR_ANY))
Ursula Braunac713872017-01-09 16:55:13 +0100285 goto out;
286
287 lock_sock(sk);
288
289 /* Check if socket is already active */
290 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +0200291 if (sk->sk_state != SMC_INIT || smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +0100292 goto out_rel;
293
294 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
295 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
296
297out_rel:
298 release_sock(sk);
299out:
300 return rc;
301}
302
303static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
304 unsigned long mask)
305{
306 /* options we don't get control via setsockopt for */
307 nsk->sk_type = osk->sk_type;
308 nsk->sk_sndbuf = osk->sk_sndbuf;
309 nsk->sk_rcvbuf = osk->sk_rcvbuf;
310 nsk->sk_sndtimeo = osk->sk_sndtimeo;
311 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
312 nsk->sk_mark = osk->sk_mark;
313 nsk->sk_priority = osk->sk_priority;
314 nsk->sk_rcvlowat = osk->sk_rcvlowat;
315 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
316 nsk->sk_err = osk->sk_err;
317
318 nsk->sk_flags &= ~mask;
319 nsk->sk_flags |= osk->sk_flags & mask;
320}
321
322#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
323 (1UL << SOCK_KEEPOPEN) | \
324 (1UL << SOCK_LINGER) | \
325 (1UL << SOCK_BROADCAST) | \
326 (1UL << SOCK_TIMESTAMP) | \
327 (1UL << SOCK_DBG) | \
328 (1UL << SOCK_RCVTSTAMP) | \
329 (1UL << SOCK_RCVTSTAMPNS) | \
330 (1UL << SOCK_LOCALROUTE) | \
331 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
332 (1UL << SOCK_RXQ_OVFL) | \
333 (1UL << SOCK_WIFI_STATUS) | \
334 (1UL << SOCK_NOFCS) | \
Deepa Dinamani97184752019-02-02 07:34:51 -0800335 (1UL << SOCK_FILTER_LOCKED) | \
336 (1UL << SOCK_TSTAMP_NEW))
Ursula Braunac713872017-01-09 16:55:13 +0100337/* copy only relevant settings and flags of SOL_SOCKET level from smc to
338 * clc socket (since smc is not called for these options from net/core)
339 */
340static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
341{
342 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
343}
344
345#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
346 (1UL << SOCK_KEEPOPEN) | \
347 (1UL << SOCK_LINGER) | \
348 (1UL << SOCK_DBG))
349/* copy only settings and flags relevant for smc from clc to smc socket */
350static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
351{
352 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
353}
354
Karsten Graulb9247542020-04-29 17:10:41 +0200355/* register the new rmb on all links */
Karsten Graul7562a132020-05-01 12:48:01 +0200356static int smcr_lgr_reg_rmbs(struct smc_link *link,
Karsten Graulb9247542020-04-29 17:10:41 +0200357 struct smc_buf_desc *rmb_desc)
358{
Karsten Graul7562a132020-05-01 12:48:01 +0200359 struct smc_link_group *lgr = link->lgr;
360 int i, rc = 0;
Karsten Graulb9247542020-04-29 17:10:41 +0200361
Karsten Grauld5500662020-05-01 12:48:05 +0200362 rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY);
363 if (rc)
364 return rc;
365 /* protect against parallel smc_llc_cli_rkey_exchange() and
366 * parallel smcr_link_reg_rmb()
367 */
368 mutex_lock(&lgr->llc_conf_mutex);
Karsten Graulb9247542020-04-29 17:10:41 +0200369 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
Karsten Graul741a49a2020-07-18 15:06:16 +0200370 if (!smc_link_active(&lgr->lnk[i]))
Karsten Graulb9247542020-04-29 17:10:41 +0200371 continue;
Karsten Graul7562a132020-05-01 12:48:01 +0200372 rc = smcr_link_reg_rmb(&lgr->lnk[i], rmb_desc);
Karsten Graulb9247542020-04-29 17:10:41 +0200373 if (rc)
Karsten Graul7562a132020-05-01 12:48:01 +0200374 goto out;
Karsten Graulb9247542020-04-29 17:10:41 +0200375 }
Karsten Graul7562a132020-05-01 12:48:01 +0200376
377 /* exchange confirm_rkey msg with peer */
378 rc = smc_llc_do_confirm_rkey(link, rmb_desc);
379 if (rc) {
380 rc = -EFAULT;
381 goto out;
382 }
383 rmb_desc->is_conf_rkey = true;
384out:
Karsten Grauld5500662020-05-01 12:48:05 +0200385 mutex_unlock(&lgr->llc_conf_mutex);
386 smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
Karsten Graul7562a132020-05-01 12:48:01 +0200387 return rc;
Karsten Graulb9247542020-04-29 17:10:41 +0200388}
389
390static int smcr_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100391{
Karsten Graul387707f2020-04-29 17:10:40 +0200392 struct smc_link *link = smc->conn.lnk;
Karsten Graul0fb0b022020-04-30 15:55:43 +0200393 struct smc_llc_qentry *qentry;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100394 int rc;
395
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100396 /* receive CONFIRM LINK request from server over RoCE fabric */
Karsten Graul0fb0b022020-04-30 15:55:43 +0200397 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
398 SMC_LLC_CONFIRM_LINK);
399 if (!qentry) {
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100400 struct smc_clc_msg_decline dclc;
401
402 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100403 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100404 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100405 }
Karsten Graul649758f2020-05-04 14:18:48 +0200406 smc_llc_save_peer_uid(qentry);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200407 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_REQ);
408 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
409 if (rc)
Karsten Graul75d320d2018-03-01 13:51:31 +0100410 return SMC_CLC_DECL_RMBE_EC;
411
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100412 rc = smc_ib_modify_qp_rts(link);
413 if (rc)
Karsten Graul603cc142018-07-25 16:35:32 +0200414 return SMC_CLC_DECL_ERR_RDYLNK;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100415
416 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200417
Karsten Graul7562a132020-05-01 12:48:01 +0200418 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +0200419 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200420
Karsten Graul0fb0b022020-04-30 15:55:43 +0200421 /* confirm_rkey is implicit on 1st contact */
422 smc->conn.rmb_desc->is_conf_rkey = true;
423
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100424 /* send CONFIRM LINK response over RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200425 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100426 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200427 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100428
Karsten Graul0fb0b022020-04-30 15:55:43 +0200429 smc_llc_link_active(link);
Karsten Graul0a99be42020-05-05 15:01:20 +0200430 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200431
432 /* optional 2nd link, receive ADD LINK request from server */
433 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
434 SMC_LLC_ADD_LINK);
435 if (!qentry) {
Karsten Graul52bedf32018-03-01 13:51:32 +0100436 struct smc_clc_msg_decline dclc;
437
438 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100439 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200440 if (rc == -EAGAIN)
441 rc = 0; /* no DECLINE received, go with one link */
442 return rc;
Karsten Graul52bedf32018-03-01 13:51:32 +0100443 }
Karsten Graul0fb0b022020-04-30 15:55:43 +0200444 smc_llc_flow_qentry_clr(&link->lgr->llc_flow_lcl);
Karsten Graulb1570a82020-05-03 14:38:42 +0200445 smc_llc_cli_add_link(link, qentry);
Karsten Graul75d320d2018-03-01 13:51:31 +0100446 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100447}
448
Karsten Graule5c47442021-10-16 11:37:45 +0200449static bool smc_isascii(char *hostname)
450{
451 int i;
452
453 for (i = 0; i < SMC_MAX_HOSTNAME_LEN; i++)
454 if (!isascii(hostname[i]))
455 return false;
456 return true;
457}
458
459static void smc_conn_save_peer_info_fce(struct smc_sock *smc,
460 struct smc_clc_msg_accept_confirm *clc)
461{
462 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
463 (struct smc_clc_msg_accept_confirm_v2 *)clc;
464 struct smc_clc_first_contact_ext *fce;
465 int clc_v2_len;
466
467 if (clc->hdr.version == SMC_V1 ||
468 !(clc->hdr.typev2 & SMC_FIRST_CONTACT_MASK))
469 return;
470
471 if (smc->conn.lgr->is_smcd) {
472 memcpy(smc->conn.lgr->negotiated_eid, clc_v2->d1.eid,
473 SMC_MAX_EID_LEN);
474 clc_v2_len = offsetofend(struct smc_clc_msg_accept_confirm_v2,
475 d1);
476 } else {
477 memcpy(smc->conn.lgr->negotiated_eid, clc_v2->r1.eid,
478 SMC_MAX_EID_LEN);
479 clc_v2_len = offsetofend(struct smc_clc_msg_accept_confirm_v2,
480 r1);
481 }
482 fce = (struct smc_clc_first_contact_ext *)(((u8 *)clc_v2) + clc_v2_len);
483 smc->conn.lgr->peer_os = fce->os_type;
484 smc->conn.lgr->peer_smc_release = fce->release;
485 if (smc_isascii(fce->hostname))
486 memcpy(smc->conn.lgr->peer_hostname, fce->hostname,
487 SMC_MAX_HOSTNAME_LEN);
488}
489
Hans Wippel41349842018-06-28 19:05:11 +0200490static void smcr_conn_save_peer_info(struct smc_sock *smc,
491 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100492{
Ursula Braun3d9725a2020-09-10 18:48:23 +0200493 int bufsize = smc_uncompress_bufsize(clc->r0.rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200494
Ursula Braun3d9725a2020-09-10 18:48:23 +0200495 smc->conn.peer_rmbe_idx = clc->r0.rmbe_idx;
496 smc->conn.local_tx_ctrl.token = ntohl(clc->r0.rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200497 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100498 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200499 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100500}
501
Hans Wippel41349842018-06-28 19:05:11 +0200502static void smcd_conn_save_peer_info(struct smc_sock *smc,
503 struct smc_clc_msg_accept_confirm *clc)
504{
Ursula Braun3d9725a2020-09-10 18:48:23 +0200505 int bufsize = smc_uncompress_bufsize(clc->d0.dmbe_size);
Hans Wippel41349842018-06-28 19:05:11 +0200506
Ursula Braun3d9725a2020-09-10 18:48:23 +0200507 smc->conn.peer_rmbe_idx = clc->d0.dmbe_idx;
508 smc->conn.peer_token = clc->d0.token;
Hans Wippel41349842018-06-28 19:05:11 +0200509 /* msg header takes up space in the buffer */
510 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
511 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
512 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
513}
514
515static void smc_conn_save_peer_info(struct smc_sock *smc,
516 struct smc_clc_msg_accept_confirm *clc)
517{
518 if (smc->conn.lgr->is_smcd)
519 smcd_conn_save_peer_info(smc, clc);
520 else
521 smcr_conn_save_peer_info(smc, clc);
Karsten Graule5c47442021-10-16 11:37:45 +0200522 smc_conn_save_peer_info_fce(smc, clc);
Hans Wippel41349842018-06-28 19:05:11 +0200523}
524
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100525static void smc_link_save_peer_info(struct smc_link *link,
Karsten Graule5c47442021-10-16 11:37:45 +0200526 struct smc_clc_msg_accept_confirm *clc,
527 struct smc_init_info *ini)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100528{
Ursula Braun3d9725a2020-09-10 18:48:23 +0200529 link->peer_qpn = ntoh24(clc->r0.qpn);
Karsten Graule5c47442021-10-16 11:37:45 +0200530 memcpy(link->peer_gid, ini->peer_gid, SMC_GID_SIZE);
531 memcpy(link->peer_mac, ini->peer_mac, sizeof(link->peer_mac));
Ursula Braun3d9725a2020-09-10 18:48:23 +0200532 link->peer_psn = ntoh24(clc->r0.psn);
533 link->peer_mtu = clc->r0.qp_mtu;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100534}
535
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +0200536static void smc_stat_inc_fback_rsn_cnt(struct smc_sock *smc,
537 struct smc_stats_fback *fback_arr)
538{
539 int cnt;
540
541 for (cnt = 0; cnt < SMC_MAX_FBACK_RSN_CNT; cnt++) {
542 if (fback_arr[cnt].fback_code == smc->fallback_rsn) {
543 fback_arr[cnt].count++;
544 break;
545 }
546 if (!fback_arr[cnt].fback_code) {
547 fback_arr[cnt].fback_code = smc->fallback_rsn;
548 fback_arr[cnt].count++;
549 break;
550 }
551 }
552}
553
554static void smc_stat_fallback(struct smc_sock *smc)
555{
Guvenc Gulce194730a2021-06-16 16:52:58 +0200556 struct net *net = sock_net(&smc->sk);
557
558 mutex_lock(&net->smc.mutex_fback_rsn);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +0200559 if (smc->listen_smc) {
Guvenc Gulce194730a2021-06-16 16:52:58 +0200560 smc_stat_inc_fback_rsn_cnt(smc, net->smc.fback_rsn->srv);
561 net->smc.fback_rsn->srv_fback_cnt++;
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +0200562 } else {
Guvenc Gulce194730a2021-06-16 16:52:58 +0200563 smc_stat_inc_fback_rsn_cnt(smc, net->smc.fback_rsn->clnt);
564 net->smc.fback_rsn->clnt_fback_cnt++;
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +0200565 }
Guvenc Gulce194730a2021-06-16 16:52:58 +0200566 mutex_unlock(&net->smc.mutex_fback_rsn);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +0200567}
568
569static void smc_switch_to_fallback(struct smc_sock *smc, int reason_code)
Ursula Braun07603b22019-04-11 11:17:32 +0200570{
Wen Gu2153bd12021-11-13 15:33:35 +0800571 wait_queue_head_t *smc_wait = sk_sleep(&smc->sk);
572 wait_queue_head_t *clc_wait = sk_sleep(smc->clcsock->sk);
573 unsigned long flags;
574
Ursula Braun07603b22019-04-11 11:17:32 +0200575 smc->use_fallback = true;
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +0200576 smc->fallback_rsn = reason_code;
577 smc_stat_fallback(smc);
Tony Lu48262602021-11-01 15:39:12 +0800578 trace_smc_switch_to_fallback(smc, reason_code);
Ursula Braun07603b22019-04-11 11:17:32 +0200579 if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
580 smc->clcsock->file = smc->sk.sk_socket->file;
581 smc->clcsock->file->private_data = smc->clcsock;
Ursula Braun67f562e2020-02-14 08:58:59 +0100582 smc->clcsock->wq.fasync_list =
583 smc->sk.sk_socket->wq.fasync_list;
Wen Gu2153bd12021-11-13 15:33:35 +0800584
585 /* There may be some entries remaining in
586 * smc socket->wq, which should be removed
587 * to clcsocket->wq during the fallback.
588 */
589 spin_lock_irqsave(&smc_wait->lock, flags);
Wen Gu7a614322021-11-22 20:32:53 +0800590 spin_lock_nested(&clc_wait->lock, SINGLE_DEPTH_NESTING);
Wen Gu2153bd12021-11-13 15:33:35 +0800591 list_splice_init(&smc_wait->head, &clc_wait->head);
592 spin_unlock(&clc_wait->lock);
593 spin_unlock_irqrestore(&smc_wait->lock, flags);
Ursula Braun07603b22019-04-11 11:17:32 +0200594 }
595}
596
Hans Wippel3b2dec22018-05-18 09:34:18 +0200597/* fall back during connect */
Karsten Graul603cc142018-07-25 16:35:32 +0200598static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200599{
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +0200600 smc_switch_to_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200601 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200602 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200603 if (smc->sk.sk_state == SMC_INIT)
604 smc->sk.sk_state = SMC_ACTIVE;
605 return 0;
606}
607
608/* decline and fall back during connect */
Ursula Braune8d726c82020-09-26 12:44:32 +0200609static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code,
610 u8 version)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200611{
Guvenc Gulce194730a2021-06-16 16:52:58 +0200612 struct net *net = sock_net(&smc->sk);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200613 int rc;
614
Ursula Braune1bbdd52018-07-05 16:15:30 +0200615 if (reason_code < 0) { /* error, fallback is not possible */
Guvenc Gulce194730a2021-06-16 16:52:58 +0200616 this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200617 if (smc->sk.sk_state == SMC_INIT)
618 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200619 return reason_code;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200620 }
Karsten Graul603cc142018-07-25 16:35:32 +0200621 if (reason_code != SMC_CLC_DECL_PEERDECL) {
Ursula Braune8d726c82020-09-26 12:44:32 +0200622 rc = smc_clc_send_decline(smc, reason_code, version);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200623 if (rc < 0) {
Guvenc Gulce194730a2021-06-16 16:52:58 +0200624 this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200625 if (smc->sk.sk_state == SMC_INIT)
626 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200627 return rc;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200628 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200629 }
Karsten Graul603cc142018-07-25 16:35:32 +0200630 return smc_connect_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200631}
632
Karsten Graul8cf3f3e2020-12-01 20:20:36 +0100633static void smc_conn_abort(struct smc_sock *smc, int local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200634{
Wen Gu36595d82022-01-06 20:42:08 +0800635 struct smc_connection *conn = &smc->conn;
636 struct smc_link_group *lgr = conn->lgr;
Wen Guea89c6c2022-01-13 16:36:41 +0800637 bool lgr_valid = false;
638
639 if (smc_conn_lgr_valid(conn))
640 lgr_valid = true;
Wen Gu36595d82022-01-06 20:42:08 +0800641
642 smc_conn_free(conn);
Wen Guea89c6c2022-01-13 16:36:41 +0800643 if (local_first && lgr_valid)
Wen Gu36595d82022-01-06 20:42:08 +0800644 smc_lgr_cleanup_early(lgr);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200645}
646
647/* check if there is a rdma device available for this connection. */
648/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200649static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200650{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200651 /* PNET table look up: search active ib_device and port
652 * within same PNETID that also contains the ethernet device
653 * used for the internal TCP socket
654 */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200655 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
Karsten Graul42042db2021-10-16 11:37:44 +0200656 if (!ini->check_smcrv2 && !ini->ib_dev)
657 return SMC_CLC_DECL_NOSMCRDEV;
658 if (ini->check_smcrv2 && !ini->smcrv2.ib_dev_v2)
Karsten Graul9aa68d22019-04-12 12:57:29 +0200659 return SMC_CLC_DECL_NOSMCRDEV;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200660 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200661}
662
Hans Wippel41349842018-06-28 19:05:11 +0200663/* check if there is an ISM device available for this connection. */
664/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200665static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200666{
667 /* Find ISM device with same PNETID as connecting interface */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200668 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
Ursula Braun3fc64932020-09-26 12:44:23 +0200669 if (!ini->ism_dev[0])
Karsten Graul9aa68d22019-04-12 12:57:29 +0200670 return SMC_CLC_DECL_NOSMCDDEV;
Ursula Braun8caaccf2020-09-26 12:44:25 +0200671 else
672 ini->ism_chid[0] = smc_ism_get_chid(ini->ism_dev[0]);
Hans Wippel41349842018-06-28 19:05:11 +0200673 return 0;
674}
675
Karsten Graul839d6962020-10-02 17:09:26 +0200676/* is chid unique for the ism devices that are already determined? */
677static bool smc_find_ism_v2_is_unique_chid(u16 chid, struct smc_init_info *ini,
678 int cnt)
679{
680 int i = (!ini->ism_dev[0]) ? 1 : 0;
681
682 for (; i < cnt; i++)
683 if (ini->ism_chid[i] == chid)
684 return false;
685 return true;
686}
687
Ursula Braund70bf4f2020-09-26 12:44:27 +0200688/* determine possible V2 ISM devices (either without PNETID or with PNETID plus
689 * PNETID matching net_device)
690 */
691static int smc_find_ism_v2_device_clnt(struct smc_sock *smc,
692 struct smc_init_info *ini)
693{
694 int rc = SMC_CLC_DECL_NOSMCDDEV;
695 struct smcd_dev *smcd;
696 int i = 1;
Karsten Graul839d6962020-10-02 17:09:26 +0200697 u16 chid;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200698
699 if (smcd_indicated(ini->smc_type_v1))
700 rc = 0; /* already initialized for V1 */
701 mutex_lock(&smcd_dev_list.mutex);
702 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
703 if (smcd->going_away || smcd == ini->ism_dev[0])
704 continue;
Karsten Graul839d6962020-10-02 17:09:26 +0200705 chid = smc_ism_get_chid(smcd);
706 if (!smc_find_ism_v2_is_unique_chid(chid, ini, i))
707 continue;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200708 if (!smc_pnet_is_pnetid_set(smcd->pnetid) ||
709 smc_pnet_is_ndev_pnetid(sock_net(&smc->sk), smcd->pnetid)) {
710 ini->ism_dev[i] = smcd;
Karsten Graul839d6962020-10-02 17:09:26 +0200711 ini->ism_chid[i] = chid;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200712 ini->is_smcd = true;
713 rc = 0;
714 i++;
715 if (i > SMC_MAX_ISM_DEVS)
716 break;
717 }
718 }
719 mutex_unlock(&smcd_dev_list.mutex);
720 ini->ism_offered_cnt = i - 1;
721 if (!ini->ism_dev[0] && !ini->ism_dev[1])
722 ini->smcd_version = 0;
723
724 return rc;
725}
726
Hans Wippel41349842018-06-28 19:05:11 +0200727/* Check for VLAN ID and register it on ISM device just for CLC handshake */
728static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200729 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200730{
Ursula Braun3fc64932020-09-26 12:44:23 +0200731 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev[0], ini->vlan_id))
Karsten Graul7a627252019-04-12 12:57:30 +0200732 return SMC_CLC_DECL_ISMVLANERR;
Hans Wippel41349842018-06-28 19:05:11 +0200733 return 0;
734}
735
Ursula Braund70bf4f2020-09-26 12:44:27 +0200736static int smc_find_proposal_devices(struct smc_sock *smc,
737 struct smc_init_info *ini)
738{
739 int rc = 0;
740
741 /* check if there is an ism device available */
Karsten Graul42042db2021-10-16 11:37:44 +0200742 if (!(ini->smcd_version & SMC_V1) ||
743 smc_find_ism_device(smc, ini) ||
744 smc_connect_ism_vlan_setup(smc, ini))
745 ini->smcd_version &= ~SMC_V1;
746 /* else ISM V1 is supported for this connection */
747
748 /* check if there is an rdma device available */
749 if (!(ini->smcr_version & SMC_V1) ||
750 smc_find_rdma_device(smc, ini))
751 ini->smcr_version &= ~SMC_V1;
752 /* else RDMA is supported for this connection */
753
754 ini->smc_type_v1 = smc_indicated_type(ini->smcd_version & SMC_V1,
755 ini->smcr_version & SMC_V1);
756
757 /* check if there is an ism v2 device available */
758 if (!(ini->smcd_version & SMC_V2) ||
759 !smc_ism_is_v2_capable() ||
760 smc_find_ism_v2_device_clnt(smc, ini))
761 ini->smcd_version &= ~SMC_V2;
762
763 /* check if there is an rdma v2 device available */
764 ini->check_smcrv2 = true;
765 ini->smcrv2.saddr = smc->clcsock->sk->sk_rcv_saddr;
766 if (!(ini->smcr_version & SMC_V2) ||
767 smc->clcsock->sk->sk_family != AF_INET ||
768 !smc_clc_ueid_count() ||
769 smc_find_rdma_device(smc, ini))
770 ini->smcr_version &= ~SMC_V2;
771 ini->check_smcrv2 = false;
772
773 ini->smc_type_v2 = smc_indicated_type(ini->smcd_version & SMC_V2,
774 ini->smcr_version & SMC_V2);
Ursula Braund70bf4f2020-09-26 12:44:27 +0200775
776 /* if neither ISM nor RDMA are supported, fallback */
Karsten Graul42042db2021-10-16 11:37:44 +0200777 if (ini->smc_type_v1 == SMC_TYPE_N && ini->smc_type_v2 == SMC_TYPE_N)
Ursula Braund70bf4f2020-09-26 12:44:27 +0200778 rc = SMC_CLC_DECL_NOSMCDEV;
779
780 return rc;
781}
782
Hans Wippel41349842018-06-28 19:05:11 +0200783/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
784 * used, the VLAN ID will be registered again during the connection setup.
785 */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200786static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200787 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200788{
Ursula Braund70bf4f2020-09-26 12:44:27 +0200789 if (!smcd_indicated(ini->smc_type_v1))
Hans Wippel41349842018-06-28 19:05:11 +0200790 return 0;
Ursula Braun3fc64932020-09-26 12:44:23 +0200791 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev[0], ini->vlan_id))
Hans Wippel41349842018-06-28 19:05:11 +0200792 return SMC_CLC_DECL_CNFERR;
793 return 0;
794}
795
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200796#define SMC_CLC_MAX_ACCEPT_LEN \
797 (sizeof(struct smc_clc_msg_accept_confirm_v2) + \
Ursula Braunb81a5eb2020-09-26 12:44:31 +0200798 sizeof(struct smc_clc_first_contact_ext) + \
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200799 sizeof(struct smc_clc_msg_trail))
800
Hans Wippel3b2dec22018-05-18 09:34:18 +0200801/* CLC handshake during connect */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200802static int smc_connect_clc(struct smc_sock *smc,
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200803 struct smc_clc_msg_accept_confirm_v2 *aclc2,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200804 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200805{
806 int rc = 0;
807
808 /* do inband token exchange */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200809 rc = smc_clc_send_proposal(smc, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200810 if (rc)
811 return rc;
812 /* receive SMC Accept CLC message */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200813 return smc_clc_wait_msg(smc, aclc2, SMC_CLC_MAX_ACCEPT_LEN,
814 SMC_CLC_ACCEPT, CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200815}
816
Karsten Graulb4ba4652021-10-16 11:37:50 +0200817void smc_fill_gid_list(struct smc_link_group *lgr,
818 struct smc_gidlist *gidlist,
819 struct smc_ib_device *known_dev, u8 *known_gid)
Karsten Graule5c47442021-10-16 11:37:45 +0200820{
821 struct smc_init_info *alt_ini = NULL;
822
823 memset(gidlist, 0, sizeof(*gidlist));
824 memcpy(gidlist->list[gidlist->len++], known_gid, SMC_GID_SIZE);
825
826 alt_ini = kzalloc(sizeof(*alt_ini), GFP_KERNEL);
827 if (!alt_ini)
828 goto out;
829
830 alt_ini->vlan_id = lgr->vlan_id;
831 alt_ini->check_smcrv2 = true;
832 alt_ini->smcrv2.saddr = lgr->saddr;
833 smc_pnet_find_alt_roce(lgr, alt_ini, known_dev);
834
835 if (!alt_ini->smcrv2.ib_dev_v2)
836 goto out;
837
838 memcpy(gidlist->list[gidlist->len++], alt_ini->smcrv2.ib_gid_v2,
839 SMC_GID_SIZE);
840
841out:
842 kfree(alt_ini);
843}
844
845static int smc_connect_rdma_v2_prepare(struct smc_sock *smc,
846 struct smc_clc_msg_accept_confirm *aclc,
847 struct smc_init_info *ini)
848{
849 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
850 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
851 struct smc_clc_first_contact_ext *fce =
852 (struct smc_clc_first_contact_ext *)
853 (((u8 *)clc_v2) + sizeof(*clc_v2));
854
855 if (!ini->first_contact_peer || aclc->hdr.version == SMC_V1)
856 return 0;
857
858 if (fce->v2_direct) {
859 memcpy(ini->smcrv2.nexthop_mac, &aclc->r0.lcl.mac, ETH_ALEN);
860 ini->smcrv2.uses_gateway = false;
861 } else {
862 if (smc_ib_find_route(smc->clcsock->sk->sk_rcv_saddr,
863 smc_ib_gid_to_ipv4(aclc->r0.lcl.gid),
864 ini->smcrv2.nexthop_mac,
865 &ini->smcrv2.uses_gateway))
866 return SMC_CLC_DECL_NOROUTE;
867 if (!ini->smcrv2.uses_gateway) {
868 /* mismatch: peer claims indirect, but its direct */
869 return SMC_CLC_DECL_NOINDIRECT;
870 }
871 }
872 return 0;
873}
874
Ursula Brauna046d572017-01-09 16:55:16 +0100875/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200876static int smc_connect_rdma(struct smc_sock *smc,
877 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200878 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200879{
Karsten Graul0fb0b022020-04-30 15:55:43 +0200880 int i, reason_code = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200881 struct smc_link *link;
Karsten Graule5c47442021-10-16 11:37:45 +0200882 u8 *eid = NULL;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200883
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200884 ini->is_smcd = false;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200885 ini->ib_clcqpn = ntoh24(aclc->r0.qpn);
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200886 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
Karsten Graule5c47442021-10-16 11:37:45 +0200887 memcpy(ini->peer_systemid, aclc->r0.lcl.id_for_peer, SMC_SYSTEMID_LEN);
888 memcpy(ini->peer_gid, aclc->r0.lcl.gid, SMC_GID_SIZE);
889 memcpy(ini->peer_mac, aclc->r0.lcl.mac, ETH_ALEN);
890
891 reason_code = smc_connect_rdma_v2_prepare(smc, aclc, ini);
892 if (reason_code)
893 return reason_code;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200894
Hans Wippel72a36a82019-02-07 15:56:18 +0100895 mutex_lock(&smc_client_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200896 reason_code = smc_conn_create(smc, ini);
897 if (reason_code) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100898 mutex_unlock(&smc_client_lgr_pending);
899 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200900 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200901
902 smc_conn_save_peer_info(smc, aclc);
903
Ursula Braun5ac54d82020-09-10 18:48:21 +0200904 if (ini->first_contact_local) {
Karsten Graul0fb0b022020-04-30 15:55:43 +0200905 link = smc->conn.lnk;
906 } else {
907 /* set link that was assigned by server */
908 link = NULL;
909 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
910 struct smc_link *l = &smc->conn.lgr->lnk[i];
911
Ursula Braun3d9725a2020-09-10 18:48:23 +0200912 if (l->peer_qpn == ntoh24(aclc->r0.qpn) &&
913 !memcmp(l->peer_gid, &aclc->r0.lcl.gid,
914 SMC_GID_SIZE) &&
Karsten Graule5c47442021-10-16 11:37:45 +0200915 (aclc->hdr.version > SMC_V1 ||
916 !memcmp(l->peer_mac, &aclc->r0.lcl.mac,
917 sizeof(l->peer_mac)))) {
Karsten Graul0fb0b022020-04-30 15:55:43 +0200918 link = l;
919 break;
920 }
921 }
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200922 if (!link) {
923 reason_code = SMC_CLC_DECL_NOSRVLINK;
924 goto connect_abort;
925 }
Guvenc Gulce64513d22021-08-09 11:05:57 +0200926 smc_switch_link_and_count(&smc->conn, link);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200927 }
928
Hans Wippel3b2dec22018-05-18 09:34:18 +0200929 /* create send buffer and rmb */
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200930 if (smc_buf_create(smc, false)) {
931 reason_code = SMC_CLC_DECL_MEM;
932 goto connect_abort;
933 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200934
Ursula Braun5ac54d82020-09-10 18:48:21 +0200935 if (ini->first_contact_local)
Karsten Graule5c47442021-10-16 11:37:45 +0200936 smc_link_save_peer_info(link, aclc, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200937
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200938 if (smc_rmb_rtoken_handling(&smc->conn, link, aclc)) {
939 reason_code = SMC_CLC_DECL_ERR_RTOK;
940 goto connect_abort;
941 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200942
943 smc_close_init(smc);
944 smc_rx_init(smc);
945
Ursula Braun5ac54d82020-09-10 18:48:21 +0200946 if (ini->first_contact_local) {
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200947 if (smc_ib_ready_link(link)) {
948 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
949 goto connect_abort;
950 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200951 } else {
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200952 if (smcr_lgr_reg_rmbs(link, smc->conn.rmb_desc)) {
953 reason_code = SMC_CLC_DECL_ERR_REGRMB;
954 goto connect_abort;
955 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200956 }
957 smc_rmb_sync_sg_for_device(&smc->conn);
958
Karsten Graule5c47442021-10-16 11:37:45 +0200959 if (aclc->hdr.version > SMC_V1) {
960 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
961 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
962
963 eid = clc_v2->r1.eid;
964 if (ini->first_contact_local)
965 smc_fill_gid_list(link->lgr, &ini->smcrv2.gidlist,
966 link->smcibdev, link->gid);
967 }
968
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200969 reason_code = smc_clc_send_confirm(smc, ini->first_contact_local,
Karsten Graule5c47442021-10-16 11:37:45 +0200970 aclc->hdr.version, eid, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200971 if (reason_code)
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200972 goto connect_abort;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200973
974 smc_tx_init(smc);
975
Ursula Braun5ac54d82020-09-10 18:48:21 +0200976 if (ini->first_contact_local) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200977 /* QP confirmation over RoCE fabric */
Karsten Graul0fb0b022020-04-30 15:55:43 +0200978 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
Karsten Graulb9247542020-04-29 17:10:41 +0200979 reason_code = smcr_clnt_conf_first_link(smc);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200980 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200981 if (reason_code)
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200982 goto connect_abort;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200983 }
Hans Wippel72a36a82019-02-07 15:56:18 +0100984 mutex_unlock(&smc_client_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200985
986 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200987 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200988 if (smc->sk.sk_state == SMC_INIT)
989 smc->sk.sk_state = SMC_ACTIVE;
990
991 return 0;
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200992connect_abort:
Karsten Graul8cf3f3e2020-12-01 20:20:36 +0100993 smc_conn_abort(smc, ini->first_contact_local);
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200994 mutex_unlock(&smc_client_lgr_pending);
995 smc->connect_nonblock = 0;
996
997 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200998}
999
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001000/* The server has chosen one of the proposed ISM devices for the communication.
1001 * Determine from the CHID of the received CLC ACCEPT the ISM device chosen.
1002 */
1003static int
1004smc_v2_determine_accepted_chid(struct smc_clc_msg_accept_confirm_v2 *aclc,
1005 struct smc_init_info *ini)
1006{
1007 int i;
1008
1009 for (i = 0; i < ini->ism_offered_cnt + 1; i++) {
Karsten Graule5c47442021-10-16 11:37:45 +02001010 if (ini->ism_chid[i] == ntohs(aclc->d1.chid)) {
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001011 ini->ism_selected = i;
1012 return 0;
1013 }
1014 }
1015
1016 return -EPROTO;
1017}
1018
Hans Wippel41349842018-06-28 19:05:11 +02001019/* setup for ISM connection of client */
1020static int smc_connect_ism(struct smc_sock *smc,
1021 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001022 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +02001023{
Karsten Graulfa086662021-09-14 10:35:05 +02001024 u8 *eid = NULL;
Hans Wippel41349842018-06-28 19:05:11 +02001025 int rc = 0;
1026
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001027 ini->is_smcd = true;
Ursula Braunf1eb02f2020-09-26 12:44:20 +02001028 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001029
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001030 if (aclc->hdr.version == SMC_V2) {
1031 struct smc_clc_msg_accept_confirm_v2 *aclc_v2 =
1032 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
1033
1034 rc = smc_v2_determine_accepted_chid(aclc_v2, ini);
1035 if (rc)
1036 return rc;
1037 }
1038 ini->ism_peer_gid[ini->ism_selected] = aclc->d0.gid;
1039
Hans Wippel72a36a82019-02-07 15:56:18 +01001040 /* there is only one lgr role for SMC-D; use server lock */
1041 mutex_lock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +02001042 rc = smc_conn_create(smc, ini);
1043 if (rc) {
Hans Wippel72a36a82019-02-07 15:56:18 +01001044 mutex_unlock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +02001045 return rc;
Hans Wippel72a36a82019-02-07 15:56:18 +01001046 }
Hans Wippel41349842018-06-28 19:05:11 +02001047
1048 /* Create send and receive buffers */
Karsten Graul72b7f6c2020-07-26 20:34:28 +02001049 rc = smc_buf_create(smc, true);
Karsten Graulc60a2ce2020-10-07 22:57:41 +02001050 if (rc) {
1051 rc = (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB : SMC_CLC_DECL_MEM;
1052 goto connect_abort;
1053 }
Hans Wippel41349842018-06-28 19:05:11 +02001054
1055 smc_conn_save_peer_info(smc, aclc);
1056 smc_close_init(smc);
1057 smc_rx_init(smc);
1058 smc_tx_init(smc);
1059
Karsten Graulfa086662021-09-14 10:35:05 +02001060 if (aclc->hdr.version > SMC_V1) {
1061 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
1062 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
1063
Karsten Graule5c47442021-10-16 11:37:45 +02001064 eid = clc_v2->d1.eid;
Karsten Graulfa086662021-09-14 10:35:05 +02001065 }
1066
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001067 rc = smc_clc_send_confirm(smc, ini->first_contact_local,
Karsten Graule5c47442021-10-16 11:37:45 +02001068 aclc->hdr.version, eid, NULL);
Hans Wippel41349842018-06-28 19:05:11 +02001069 if (rc)
Karsten Graulc60a2ce2020-10-07 22:57:41 +02001070 goto connect_abort;
Hans Wippel72a36a82019-02-07 15:56:18 +01001071 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel41349842018-06-28 19:05:11 +02001072
1073 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +02001074 smc->connect_nonblock = 0;
Hans Wippel41349842018-06-28 19:05:11 +02001075 if (smc->sk.sk_state == SMC_INIT)
1076 smc->sk.sk_state = SMC_ACTIVE;
1077
1078 return 0;
Karsten Graulc60a2ce2020-10-07 22:57:41 +02001079connect_abort:
Karsten Graul8cf3f3e2020-12-01 20:20:36 +01001080 smc_conn_abort(smc, ini->first_contact_local);
Karsten Graulc60a2ce2020-10-07 22:57:41 +02001081 mutex_unlock(&smc_server_lgr_pending);
1082 smc->connect_nonblock = 0;
1083
1084 return rc;
Hans Wippel41349842018-06-28 19:05:11 +02001085}
1086
Ursula Braund70bf4f2020-09-26 12:44:27 +02001087/* check if received accept type and version matches a proposed one */
1088static int smc_connect_check_aclc(struct smc_init_info *ini,
1089 struct smc_clc_msg_accept_confirm *aclc)
1090{
Karsten Graul42042db2021-10-16 11:37:44 +02001091 if (aclc->hdr.typev1 != SMC_TYPE_R &&
1092 aclc->hdr.typev1 != SMC_TYPE_D)
Ursula Braund70bf4f2020-09-26 12:44:27 +02001093 return SMC_CLC_DECL_MODEUNSUPP;
1094
Karsten Graul42042db2021-10-16 11:37:44 +02001095 if (aclc->hdr.version >= SMC_V2) {
1096 if ((aclc->hdr.typev1 == SMC_TYPE_R &&
1097 !smcr_indicated(ini->smc_type_v2)) ||
1098 (aclc->hdr.typev1 == SMC_TYPE_D &&
1099 !smcd_indicated(ini->smc_type_v2)))
1100 return SMC_CLC_DECL_MODEUNSUPP;
1101 } else {
1102 if ((aclc->hdr.typev1 == SMC_TYPE_R &&
1103 !smcr_indicated(ini->smc_type_v1)) ||
1104 (aclc->hdr.typev1 == SMC_TYPE_D &&
1105 !smcd_indicated(ini->smc_type_v1)))
1106 return SMC_CLC_DECL_MODEUNSUPP;
1107 }
1108
Ursula Braund70bf4f2020-09-26 12:44:27 +02001109 return 0;
1110}
1111
Hans Wippel3b2dec22018-05-18 09:34:18 +02001112/* perform steps before actually connecting */
1113static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001114{
Guvenc Gulce49407ae2020-12-01 20:20:42 +01001115 u8 version = smc_ism_is_v2_capable() ? SMC_V2 : SMC_V1;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001116 struct smc_clc_msg_accept_confirm_v2 *aclc2;
1117 struct smc_clc_msg_accept_confirm *aclc;
Ursula Braun3fc64932020-09-26 12:44:23 +02001118 struct smc_init_info *ini = NULL;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001119 u8 *buf = NULL;
Ursula Brauna046d572017-01-09 16:55:16 +01001120 int rc = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001121
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001122 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +02001123 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001124
Hans Wippel3b2dec22018-05-18 09:34:18 +02001125 /* if peer has not signalled SMC-capability, fall back */
1126 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +02001127 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001128
Ursula Braune8d726c82020-09-26 12:44:32 +02001129 /* IPSec connections opt out of SMC optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001130 if (using_ipsec(smc))
Ursula Braune8d726c82020-09-26 12:44:32 +02001131 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC,
1132 version);
Ursula Brauna046d572017-01-09 16:55:16 +01001133
Ursula Braun3fc64932020-09-26 12:44:23 +02001134 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
1135 if (!ini)
Ursula Braune8d726c82020-09-26 12:44:32 +02001136 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_MEM,
1137 version);
Ursula Braun3fc64932020-09-26 12:44:23 +02001138
Karsten Graul42042db2021-10-16 11:37:44 +02001139 ini->smcd_version = SMC_V1 | SMC_V2;
1140 ini->smcr_version = SMC_V1 | SMC_V2;
Ursula Braund70bf4f2020-09-26 12:44:27 +02001141 ini->smc_type_v1 = SMC_TYPE_B;
Karsten Graul42042db2021-10-16 11:37:44 +02001142 ini->smc_type_v2 = SMC_TYPE_B;
Ursula Braund70bf4f2020-09-26 12:44:27 +02001143
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001144 /* get vlan id from IP device */
Ursula Braun3fc64932020-09-26 12:44:23 +02001145 if (smc_vlan_by_tcpsk(smc->clcsock, ini)) {
Ursula Braund70bf4f2020-09-26 12:44:27 +02001146 ini->smcd_version &= ~SMC_V1;
Karsten Graul42042db2021-10-16 11:37:44 +02001147 ini->smcr_version = 0;
Ursula Braund70bf4f2020-09-26 12:44:27 +02001148 ini->smc_type_v1 = SMC_TYPE_N;
1149 if (!ini->smcd_version) {
1150 rc = SMC_CLC_DECL_GETVLANERR;
1151 goto fallback;
1152 }
Ursula Braun3fc64932020-09-26 12:44:23 +02001153 }
Hans Wippel41349842018-06-28 19:05:11 +02001154
Ursula Braund70bf4f2020-09-26 12:44:27 +02001155 rc = smc_find_proposal_devices(smc, ini);
1156 if (rc)
1157 goto fallback;
Ursula Brauna046d572017-01-09 16:55:16 +01001158
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001159 buf = kzalloc(SMC_CLC_MAX_ACCEPT_LEN, GFP_KERNEL);
1160 if (!buf) {
1161 rc = SMC_CLC_DECL_MEM;
1162 goto fallback;
1163 }
1164 aclc2 = (struct smc_clc_msg_accept_confirm_v2 *)buf;
1165 aclc = (struct smc_clc_msg_accept_confirm *)aclc2;
1166
Hans Wippel3b2dec22018-05-18 09:34:18 +02001167 /* perform CLC handshake */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001168 rc = smc_connect_clc(smc, aclc2, ini);
Ursula Braund70bf4f2020-09-26 12:44:27 +02001169 if (rc)
1170 goto vlan_cleanup;
1171
1172 /* check if smc modes and versions of CLC proposal and accept match */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001173 rc = smc_connect_check_aclc(ini, aclc);
Karsten Graul0530bd62020-11-18 22:40:37 +01001174 version = aclc->hdr.version == SMC_V1 ? SMC_V1 : SMC_V2;
Ursula Braund70bf4f2020-09-26 12:44:27 +02001175 if (rc)
1176 goto vlan_cleanup;
Ursula Brauna046d572017-01-09 16:55:16 +01001177
Hans Wippel41349842018-06-28 19:05:11 +02001178 /* depending on previous steps, connect using rdma or ism */
Karsten Graul42042db2021-10-16 11:37:44 +02001179 if (aclc->hdr.typev1 == SMC_TYPE_R) {
1180 ini->smcr_version = version;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001181 rc = smc_connect_rdma(smc, aclc, ini);
Karsten Graul42042db2021-10-16 11:37:44 +02001182 } else if (aclc->hdr.typev1 == SMC_TYPE_D) {
1183 ini->smcd_version = version;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001184 rc = smc_connect_ism(smc, aclc, ini);
Karsten Graul42042db2021-10-16 11:37:44 +02001185 }
Ursula Braund70bf4f2020-09-26 12:44:27 +02001186 if (rc)
1187 goto vlan_cleanup;
Ursula Brauna046d572017-01-09 16:55:16 +01001188
Guvenc Gulce194730a2021-06-16 16:52:58 +02001189 SMC_STAT_CLNT_SUCC_INC(sock_net(smc->clcsock->sk), aclc);
Ursula Braund70bf4f2020-09-26 12:44:27 +02001190 smc_connect_ism_vlan_cleanup(smc, ini);
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001191 kfree(buf);
Ursula Braun3fc64932020-09-26 12:44:23 +02001192 kfree(ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001193 return 0;
Ursula Braund70bf4f2020-09-26 12:44:27 +02001194
1195vlan_cleanup:
1196 smc_connect_ism_vlan_cleanup(smc, ini);
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001197 kfree(buf);
Ursula Braund70bf4f2020-09-26 12:44:27 +02001198fallback:
1199 kfree(ini);
Ursula Braune8d726c82020-09-26 12:44:32 +02001200 return smc_connect_decline_fallback(smc, rc, version);
Ursula Brauna046d572017-01-09 16:55:16 +01001201}
1202
Ursula Braun24ac3a02018-06-27 17:59:50 +02001203static void smc_connect_work(struct work_struct *work)
1204{
1205 struct smc_sock *smc = container_of(work, struct smc_sock,
1206 connect_work);
Ursula Braun50717a32019-04-12 12:57:23 +02001207 long timeo = smc->sk.sk_sndtimeo;
1208 int rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001209
Ursula Braun50717a32019-04-12 12:57:23 +02001210 if (!timeo)
1211 timeo = MAX_SCHEDULE_TIMEOUT;
1212 lock_sock(smc->clcsock->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +02001213 if (smc->clcsock->sk->sk_err) {
1214 smc->sk.sk_err = smc->clcsock->sk->sk_err;
Ursula Braun50717a32019-04-12 12:57:23 +02001215 } else if ((1 << smc->clcsock->sk->sk_state) &
Wen Guf3a3a0f2021-10-28 15:13:47 +08001216 (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
Ursula Braun50717a32019-04-12 12:57:23 +02001217 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
1218 if ((rc == -EPIPE) &&
1219 ((1 << smc->clcsock->sk->sk_state) &
1220 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
1221 rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001222 }
Ursula Braun50717a32019-04-12 12:57:23 +02001223 release_sock(smc->clcsock->sk);
1224 lock_sock(&smc->sk);
1225 if (rc != 0 || smc->sk.sk_err) {
1226 smc->sk.sk_state = SMC_CLOSED;
1227 if (rc == -EPIPE || rc == -EAGAIN)
1228 smc->sk.sk_err = EPIPE;
1229 else if (signal_pending(current))
1230 smc->sk.sk_err = -sock_intr_errno(timeo);
Ursula Braun6d6dd522019-11-12 16:03:41 +01001231 sock_put(&smc->sk); /* passive closing */
Ursula Braun24ac3a02018-06-27 17:59:50 +02001232 goto out;
1233 }
1234
1235 rc = __smc_connect(smc);
1236 if (rc < 0)
1237 smc->sk.sk_err = -rc;
1238
1239out:
Ursula Braun07603b22019-04-11 11:17:32 +02001240 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
1241 if (smc->sk.sk_err) {
1242 smc->sk.sk_state_change(&smc->sk);
1243 } else { /* allow polling before and after fallback decision */
1244 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
1245 smc->sk.sk_write_space(&smc->sk);
1246 }
1247 }
Ursula Braun24ac3a02018-06-27 17:59:50 +02001248 release_sock(&smc->sk);
1249}
1250
Ursula Braunac713872017-01-09 16:55:13 +01001251static int smc_connect(struct socket *sock, struct sockaddr *addr,
1252 int alen, int flags)
1253{
1254 struct sock *sk = sock->sk;
1255 struct smc_sock *smc;
1256 int rc = -EINVAL;
1257
1258 smc = smc_sk(sk);
1259
1260 /* separate smc parameter checking to be safe */
1261 if (alen < sizeof(addr->sa_family))
1262 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001263 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +01001264 goto out_err;
1265
1266 lock_sock(sk);
1267 switch (sk->sk_state) {
1268 default:
1269 goto out;
1270 case SMC_ACTIVE:
1271 rc = -EISCONN;
1272 goto out;
1273 case SMC_INIT:
Ursula Braunac713872017-01-09 16:55:13 +01001274 break;
1275 }
1276
1277 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001278 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun50717a32019-04-12 12:57:23 +02001279 if (smc->connect_nonblock) {
1280 rc = -EALREADY;
1281 goto out;
1282 }
1283 rc = kernel_connect(smc->clcsock, addr, alen, flags);
1284 if (rc && rc != -EINPROGRESS)
1285 goto out;
Ursula Braun301428e2019-10-29 12:41:26 +01001286
1287 sock_hold(&smc->sk); /* sock put in passive closing */
Ursula Braun86434742019-12-12 22:35:58 +01001288 if (smc->use_fallback)
1289 goto out;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001290 if (flags & O_NONBLOCK) {
Karsten Graul22ef4732020-09-10 18:48:29 +02001291 if (queue_work(smc_hs_wq, &smc->connect_work))
Ursula Braun50717a32019-04-12 12:57:23 +02001292 smc->connect_nonblock = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001293 rc = -EINPROGRESS;
1294 } else {
Ursula Braun24ac3a02018-06-27 17:59:50 +02001295 rc = __smc_connect(smc);
1296 if (rc < 0)
1297 goto out;
1298 else
1299 rc = 0; /* success cases including fallback */
1300 }
Ursula Braunac713872017-01-09 16:55:13 +01001301
1302out:
1303 release_sock(sk);
1304out_err:
1305 return rc;
1306}
1307
1308static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
1309{
Ursula Braun3163c502018-01-24 10:28:12 +01001310 struct socket *new_clcsock = NULL;
1311 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +01001312 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -08001313 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +01001314
Ursula Braun3163c502018-01-24 10:28:12 +01001315 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001316 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001317 if (!new_sk) {
1318 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +01001319 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +01001320 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +01001321 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +01001322 goto out;
1323 }
1324 *new_smc = smc_sk(new_sk);
1325
Myungho Jung78abe3d2018-12-18 09:02:25 -08001326 mutex_lock(&lsmc->clcsock_release_lock);
1327 if (lsmc->clcsock)
Ursula Brauna60a2b12020-09-10 18:48:20 +02001328 rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK);
Myungho Jung78abe3d2018-12-18 09:02:25 -08001329 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +01001330 lock_sock(lsk);
Ursula Brauna60a2b12020-09-10 18:48:20 +02001331 if (rc < 0 && rc != -EAGAIN)
Ursula Braun3163c502018-01-24 10:28:12 +01001332 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +01001333 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +02001334 new_sk->sk_prot->unhash(new_sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001335 if (new_clcsock)
1336 sock_release(new_clcsock);
1337 new_sk->sk_state = SMC_CLOSED;
1338 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun51f1de72018-01-26 09:28:48 +01001339 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +01001340 *new_smc = NULL;
1341 goto out;
1342 }
1343
Ursula Brauna60a2b12020-09-10 18:48:20 +02001344 /* new clcsock has inherited the smc listen-specific sk_data_ready
1345 * function; switch it back to the original sk_data_ready function
1346 */
1347 new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready;
Ursula Braunac713872017-01-09 16:55:13 +01001348 (*new_smc)->clcsock = new_clcsock;
1349out:
1350 return rc;
1351}
1352
Ursula Brauna046d572017-01-09 16:55:16 +01001353/* add a just created sock to the accept queue of the listen sock as
1354 * candidate for a following socket accept call from user space
1355 */
1356static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
1357{
1358 struct smc_sock *par = smc_sk(parent);
1359
Ursula Braun51f1de72018-01-26 09:28:48 +01001360 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +01001361 spin_lock(&par->accept_q_lock);
1362 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
1363 spin_unlock(&par->accept_q_lock);
1364 sk_acceptq_added(parent);
1365}
1366
1367/* remove a socket from the accept queue of its parental listening socket */
1368static void smc_accept_unlink(struct sock *sk)
1369{
1370 struct smc_sock *par = smc_sk(sk)->listen_smc;
1371
1372 spin_lock(&par->accept_q_lock);
1373 list_del_init(&smc_sk(sk)->accept_q);
1374 spin_unlock(&par->accept_q_lock);
1375 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001376 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +01001377}
1378
1379/* remove a sock from the accept queue to bind it to a new socket created
1380 * for a socket accept call from user space
1381 */
Ursula Braunb38d7322017-01-09 16:55:25 +01001382struct sock *smc_accept_dequeue(struct sock *parent,
1383 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +01001384{
1385 struct smc_sock *isk, *n;
1386 struct sock *new_sk;
1387
1388 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
1389 new_sk = (struct sock *)isk;
1390
1391 smc_accept_unlink(new_sk);
1392 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +02001393 new_sk->sk_prot->unhash(new_sk);
Ursula Braun127f4972018-01-26 09:28:49 +01001394 if (isk->clcsock) {
1395 sock_release(isk->clcsock);
1396 isk->clcsock = NULL;
1397 }
Ursula Braun51f1de72018-01-26 09:28:48 +01001398 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +01001399 continue;
1400 }
Ursula Braun07603b22019-04-11 11:17:32 +02001401 if (new_sock) {
Ursula Brauna046d572017-01-09 16:55:16 +01001402 sock_graft(new_sk, new_sock);
Ursula Braun07603b22019-04-11 11:17:32 +02001403 if (isk->use_fallback) {
1404 smc_sk(new_sk)->clcsock->file = new_sock->file;
1405 isk->clcsock->file->private_data = isk->clcsock;
1406 }
1407 }
Ursula Brauna046d572017-01-09 16:55:16 +01001408 return new_sk;
1409 }
1410 return NULL;
1411}
1412
1413/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +01001414void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +01001415{
1416 struct smc_sock *smc = smc_sk(sk);
1417
Ursula Braun81cf4f42019-10-21 16:13:15 +02001418 sock_hold(sk); /* sock_put below */
Ursula Braunb38d7322017-01-09 16:55:25 +01001419 lock_sock(sk);
1420 if (!sk->sk_lingertime)
1421 /* wait for peer closing */
1422 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun39f41f32019-06-27 15:04:52 +02001423 __smc_release(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +01001424 release_sock(sk);
Ursula Braun81cf4f42019-10-21 16:13:15 +02001425 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +01001426 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +01001427}
1428
Karsten Graulb9247542020-04-29 17:10:41 +02001429static int smcr_serv_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001430{
Karsten Graul387707f2020-04-29 17:10:40 +02001431 struct smc_link *link = smc->conn.lnk;
Karsten Graul4667bb42020-04-30 15:55:42 +02001432 struct smc_llc_qentry *qentry;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001433 int rc;
1434
Karsten Graul7562a132020-05-01 12:48:01 +02001435 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +02001436 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +02001437
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001438 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +02001439 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001440 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001441 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001442
1443 /* receive CONFIRM LINK response from client over the RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001444 qentry = smc_llc_wait(link->lgr, link, SMC_LLC_WAIT_TIME,
1445 SMC_LLC_CONFIRM_LINK);
1446 if (!qentry) {
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001447 struct smc_clc_msg_decline dclc;
1448
1449 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001450 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001451 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001452 }
Karsten Graul649758f2020-05-04 14:18:48 +02001453 smc_llc_save_peer_uid(qentry);
Karsten Graul4667bb42020-04-30 15:55:42 +02001454 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_RESP);
1455 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
1456 if (rc)
Karsten Graul75d320d2018-03-01 13:51:31 +01001457 return SMC_CLC_DECL_RMBE_EC;
1458
Karsten Graul4667bb42020-04-30 15:55:42 +02001459 /* confirm_rkey is implicit on 1st contact */
1460 smc->conn.rmb_desc->is_conf_rkey = true;
Karsten Graul52bedf32018-03-01 13:51:32 +01001461
Karsten Graul00a049c2020-04-29 17:10:49 +02001462 smc_llc_link_active(link);
Karsten Graul0a99be42020-05-05 15:01:20 +02001463 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
Karsten Graul52bedf32018-03-01 13:51:32 +01001464
Karsten Graul4667bb42020-04-30 15:55:42 +02001465 /* initial contact - try to establish second link */
Karsten Graulb4ba4652021-10-16 11:37:50 +02001466 smc_llc_srv_add_link(link, NULL);
Karsten Graul75d320d2018-03-01 13:51:31 +01001467 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001468}
1469
Hans Wippel3b2dec22018-05-18 09:34:18 +02001470/* listen worker: finish */
1471static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001472{
Ursula Brauna046d572017-01-09 16:55:16 +01001473 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001474 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001475
Ursula Brauna046d572017-01-09 16:55:16 +01001476 if (lsmc->sk.sk_state == SMC_LISTEN) {
Karsten Graulfd577702019-04-11 11:17:30 +02001477 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001478 smc_accept_enqueue(&lsmc->sk, newsmcsk);
Karsten Graulfd577702019-04-11 11:17:30 +02001479 release_sock(&lsmc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001480 } else { /* no longer listening */
1481 smc_close_non_accepted(newsmcsk);
1482 }
Ursula Brauna046d572017-01-09 16:55:16 +01001483
1484 /* Wake up accept */
1485 lsmc->sk.sk_data_ready(&lsmc->sk);
1486 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001487}
Ursula Brauna046d572017-01-09 16:55:16 +01001488
Hans Wippel3b2dec22018-05-18 09:34:18 +02001489/* listen worker: finish in state connected */
1490static void smc_listen_out_connected(struct smc_sock *new_smc)
1491{
1492 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001493
Hans Wippel3b2dec22018-05-18 09:34:18 +02001494 sk_refcnt_debug_inc(newsmcsk);
1495 if (newsmcsk->sk_state == SMC_INIT)
1496 newsmcsk->sk_state = SMC_ACTIVE;
1497
1498 smc_listen_out(new_smc);
1499}
1500
1501/* listen worker: finish in error state */
1502static void smc_listen_out_err(struct smc_sock *new_smc)
1503{
1504 struct sock *newsmcsk = &new_smc->sk;
Guvenc Gulce194730a2021-06-16 16:52:58 +02001505 struct net *net = sock_net(newsmcsk);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001506
Guvenc Gulce194730a2021-06-16 16:52:58 +02001507 this_cpu_inc(net->smc.smc_stats->srv_hshake_err_cnt);
Ursula Braun51f1de72018-01-26 09:28:48 +01001508 if (newsmcsk->sk_state == SMC_INIT)
1509 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001510 newsmcsk->sk_state = SMC_CLOSED;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001511
1512 smc_listen_out(new_smc);
1513}
1514
1515/* listen worker: decline and fall back if possible */
1516static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
Karsten Graul4a9baf42020-10-23 20:48:28 +02001517 int local_first, u8 version)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001518{
1519 /* RDMA setup failed, switch back to TCP */
Karsten Graul8cf3f3e2020-12-01 20:20:36 +01001520 smc_conn_abort(new_smc, local_first);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001521 if (reason_code < 0) { /* error, no fallback possible */
1522 smc_listen_out_err(new_smc);
1523 return;
1524 }
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02001525 smc_switch_to_fallback(new_smc, reason_code);
Karsten Graul603cc142018-07-25 16:35:32 +02001526 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Ursula Braune8d726c82020-09-26 12:44:32 +02001527 if (smc_clc_send_decline(new_smc, reason_code, version) < 0) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001528 smc_listen_out_err(new_smc);
1529 return;
1530 }
1531 }
1532 smc_listen_out_connected(new_smc);
1533}
1534
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001535/* listen worker: version checking */
1536static int smc_listen_v2_check(struct smc_sock *new_smc,
1537 struct smc_clc_msg_proposal *pclc,
1538 struct smc_init_info *ini)
1539{
1540 struct smc_clc_smcd_v2_extension *pclc_smcd_v2_ext;
1541 struct smc_clc_v2_extension *pclc_v2_ext;
Karsten Graul37524042020-10-31 19:19:38 +01001542 int rc = SMC_CLC_DECL_PEERNOSMC;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001543
1544 ini->smc_type_v1 = pclc->hdr.typev1;
1545 ini->smc_type_v2 = pclc->hdr.typev2;
Karsten Graule49300a2021-10-16 11:37:46 +02001546 ini->smcd_version = smcd_indicated(ini->smc_type_v1) ? SMC_V1 : 0;
1547 ini->smcr_version = smcr_indicated(ini->smc_type_v1) ? SMC_V1 : 0;
1548 if (pclc->hdr.version > SMC_V1) {
1549 if (smcd_indicated(ini->smc_type_v2))
1550 ini->smcd_version |= SMC_V2;
1551 if (smcr_indicated(ini->smc_type_v2))
1552 ini->smcr_version |= SMC_V2;
Karsten Graul37524042020-10-31 19:19:38 +01001553 }
Karsten Graule49300a2021-10-16 11:37:46 +02001554 if (!(ini->smcd_version & SMC_V2) && !(ini->smcr_version & SMC_V2)) {
1555 rc = SMC_CLC_DECL_PEERNOSMC;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001556 goto out;
1557 }
1558 pclc_v2_ext = smc_get_clc_v2_ext(pclc);
1559 if (!pclc_v2_ext) {
1560 ini->smcd_version &= ~SMC_V2;
Karsten Graule49300a2021-10-16 11:37:46 +02001561 ini->smcr_version &= ~SMC_V2;
Karsten Graul37524042020-10-31 19:19:38 +01001562 rc = SMC_CLC_DECL_NOV2EXT;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001563 goto out;
1564 }
1565 pclc_smcd_v2_ext = smc_get_clc_smcd_v2_ext(pclc_v2_ext);
Karsten Graule49300a2021-10-16 11:37:46 +02001566 if (ini->smcd_version & SMC_V2) {
1567 if (!smc_ism_is_v2_capable()) {
1568 ini->smcd_version &= ~SMC_V2;
1569 rc = SMC_CLC_DECL_NOISM2SUPP;
1570 } else if (!pclc_smcd_v2_ext) {
1571 ini->smcd_version &= ~SMC_V2;
1572 rc = SMC_CLC_DECL_NOV2DEXT;
1573 } else if (!pclc_v2_ext->hdr.eid_cnt &&
1574 !pclc_v2_ext->hdr.flag.seid) {
1575 ini->smcd_version &= ~SMC_V2;
1576 rc = SMC_CLC_DECL_NOUEID;
1577 }
1578 }
1579 if (ini->smcr_version & SMC_V2) {
1580 if (!pclc_v2_ext->hdr.eid_cnt) {
1581 ini->smcr_version &= ~SMC_V2;
1582 rc = SMC_CLC_DECL_NOUEID;
1583 }
Karsten Graul37524042020-10-31 19:19:38 +01001584 }
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001585
1586out:
Karsten Graule49300a2021-10-16 11:37:46 +02001587 if (!ini->smcd_version && !ini->smcr_version)
Karsten Graul37524042020-10-31 19:19:38 +01001588 return rc;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001589
1590 return 0;
1591}
1592
Hans Wippel3b2dec22018-05-18 09:34:18 +02001593/* listen worker: check prefixes */
Karsten Graul59886692019-04-12 12:57:25 +02001594static int smc_listen_prfx_check(struct smc_sock *new_smc,
Hans Wippel3b2dec22018-05-18 09:34:18 +02001595 struct smc_clc_msg_proposal *pclc)
1596{
1597 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1598 struct socket *newclcsock = new_smc->clcsock;
1599
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001600 if (pclc->hdr.typev1 == SMC_TYPE_N)
1601 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001602 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1603 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
Karsten Graul59886692019-04-12 12:57:25 +02001604 return SMC_CLC_DECL_DIFFPREFIX;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001605
1606 return 0;
1607}
1608
1609/* listen worker: initialize connection and buffers */
1610static int smc_listen_rdma_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001611 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001612{
Karsten Graul7a627252019-04-12 12:57:30 +02001613 int rc;
1614
Hans Wippel3b2dec22018-05-18 09:34:18 +02001615 /* allocate connection / link group */
Karsten Graul7a627252019-04-12 12:57:30 +02001616 rc = smc_conn_create(new_smc, ini);
1617 if (rc)
1618 return rc;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001619
1620 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001621 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001622 return SMC_CLC_DECL_MEM;
1623
1624 return 0;
1625}
1626
Hans Wippel41349842018-06-28 19:05:11 +02001627/* listen worker: initialize connection and buffers for SMC-D */
1628static int smc_listen_ism_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001629 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +02001630{
Karsten Graul7a627252019-04-12 12:57:30 +02001631 int rc;
Hans Wippel41349842018-06-28 19:05:11 +02001632
Karsten Graul7a627252019-04-12 12:57:30 +02001633 rc = smc_conn_create(new_smc, ini);
1634 if (rc)
1635 return rc;
Hans Wippel41349842018-06-28 19:05:11 +02001636
Hans Wippel41349842018-06-28 19:05:11 +02001637 /* Create send and receive buffers */
Karsten Graul72b7f6c2020-07-26 20:34:28 +02001638 rc = smc_buf_create(new_smc, true);
1639 if (rc) {
Karsten Graul8cf3f3e2020-12-01 20:20:36 +01001640 smc_conn_abort(new_smc, ini->first_contact_local);
Karsten Graul72b7f6c2020-07-26 20:34:28 +02001641 return (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB :
1642 SMC_CLC_DECL_MEM;
Hans Wippel41349842018-06-28 19:05:11 +02001643 }
1644
1645 return 0;
1646}
1647
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001648static bool smc_is_already_selected(struct smcd_dev *smcd,
1649 struct smc_init_info *ini,
1650 int matches)
1651{
1652 int i;
1653
1654 for (i = 0; i < matches; i++)
1655 if (smcd == ini->ism_dev[i])
1656 return true;
1657
1658 return false;
1659}
1660
1661/* check for ISM devices matching proposed ISM devices */
1662static void smc_check_ism_v2_match(struct smc_init_info *ini,
1663 u16 proposed_chid, u64 proposed_gid,
1664 unsigned int *matches)
1665{
1666 struct smcd_dev *smcd;
1667
1668 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
1669 if (smcd->going_away)
1670 continue;
1671 if (smc_is_already_selected(smcd, ini, *matches))
1672 continue;
1673 if (smc_ism_get_chid(smcd) == proposed_chid &&
1674 !smc_ism_cantalk(proposed_gid, ISM_RESERVED_VLANID, smcd)) {
1675 ini->ism_peer_gid[*matches] = proposed_gid;
1676 ini->ism_dev[*matches] = smcd;
1677 (*matches)++;
1678 break;
1679 }
1680 }
1681}
1682
Karsten Graul37524042020-10-31 19:19:38 +01001683static void smc_find_ism_store_rc(u32 rc, struct smc_init_info *ini)
1684{
1685 if (!ini->rc)
1686 ini->rc = rc;
1687}
1688
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001689static void smc_find_ism_v2_device_serv(struct smc_sock *new_smc,
1690 struct smc_clc_msg_proposal *pclc,
1691 struct smc_init_info *ini)
1692{
1693 struct smc_clc_smcd_v2_extension *smcd_v2_ext;
1694 struct smc_clc_v2_extension *smc_v2_ext;
1695 struct smc_clc_msg_smcd *pclc_smcd;
1696 unsigned int matches = 0;
Karsten Graulf29fa002020-10-07 22:57:43 +02001697 u8 smcd_version;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001698 u8 *eid = NULL;
Karsten Graul37524042020-10-31 19:19:38 +01001699 int i, rc;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001700
1701 if (!(ini->smcd_version & SMC_V2) || !smcd_indicated(ini->smc_type_v2))
Karsten Graulf29fa002020-10-07 22:57:43 +02001702 goto not_found;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001703
1704 pclc_smcd = smc_get_clc_msg_smcd(pclc);
1705 smc_v2_ext = smc_get_clc_v2_ext(pclc);
1706 smcd_v2_ext = smc_get_clc_smcd_v2_ext(smc_v2_ext);
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001707
1708 mutex_lock(&smcd_dev_list.mutex);
1709 if (pclc_smcd->ism.chid)
1710 /* check for ISM device matching proposed native ISM device */
1711 smc_check_ism_v2_match(ini, ntohs(pclc_smcd->ism.chid),
1712 ntohll(pclc_smcd->ism.gid), &matches);
1713 for (i = 1; i <= smc_v2_ext->hdr.ism_gid_cnt; i++) {
1714 /* check for ISM devices matching proposed non-native ISM
1715 * devices
1716 */
1717 smc_check_ism_v2_match(ini,
1718 ntohs(smcd_v2_ext->gidchid[i - 1].chid),
1719 ntohll(smcd_v2_ext->gidchid[i - 1].gid),
1720 &matches);
1721 }
1722 mutex_unlock(&smcd_dev_list.mutex);
1723
Karsten Graule49300a2021-10-16 11:37:46 +02001724 if (!ini->ism_dev[0]) {
1725 smc_find_ism_store_rc(SMC_CLC_DECL_NOSMCD2DEV, ini);
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001726 goto not_found;
Karsten Graule49300a2021-10-16 11:37:46 +02001727 }
Karsten Graulfa086662021-09-14 10:35:05 +02001728
Karsten Graul11a26c52021-09-14 10:35:06 +02001729 smc_ism_get_system_eid(&eid);
Karsten Graulfa086662021-09-14 10:35:05 +02001730 if (!smc_clc_match_eid(ini->negotiated_eid, smc_v2_ext,
1731 smcd_v2_ext->system_eid, eid))
1732 goto not_found;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001733
1734 /* separate - outside the smcd_dev_list.lock */
Karsten Graulf29fa002020-10-07 22:57:43 +02001735 smcd_version = ini->smcd_version;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001736 for (i = 0; i < matches; i++) {
1737 ini->smcd_version = SMC_V2;
1738 ini->is_smcd = true;
1739 ini->ism_selected = i;
Karsten Graul37524042020-10-31 19:19:38 +01001740 rc = smc_listen_ism_init(new_smc, ini);
1741 if (rc) {
1742 smc_find_ism_store_rc(rc, ini);
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001743 /* try next active ISM device */
1744 continue;
Karsten Graul37524042020-10-31 19:19:38 +01001745 }
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001746 return; /* matching and usable V2 ISM device found */
1747 }
Karsten Graulf29fa002020-10-07 22:57:43 +02001748 /* no V2 ISM device could be initialized */
1749 ini->smcd_version = smcd_version; /* restore original value */
Karsten Graulfa086662021-09-14 10:35:05 +02001750 ini->negotiated_eid[0] = 0;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001751
1752not_found:
1753 ini->smcd_version &= ~SMC_V2;
1754 ini->ism_dev[0] = NULL;
1755 ini->is_smcd = false;
1756}
1757
1758static void smc_find_ism_v1_device_serv(struct smc_sock *new_smc,
1759 struct smc_clc_msg_proposal *pclc,
1760 struct smc_init_info *ini)
Ursula Braun7affc802020-09-26 12:44:21 +02001761{
1762 struct smc_clc_msg_smcd *pclc_smcd = smc_get_clc_msg_smcd(pclc);
Karsten Graul37524042020-10-31 19:19:38 +01001763 int rc = 0;
Ursula Braun7affc802020-09-26 12:44:21 +02001764
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001765 /* check if ISM V1 is available */
1766 if (!(ini->smcd_version & SMC_V1) || !smcd_indicated(ini->smc_type_v1))
Ursula Braun7affc802020-09-26 12:44:21 +02001767 goto not_found;
1768 ini->is_smcd = true; /* prepare ISM check */
Ursula Braun8c3dca32020-09-26 12:44:28 +02001769 ini->ism_peer_gid[0] = ntohll(pclc_smcd->ism.gid);
Karsten Graul37524042020-10-31 19:19:38 +01001770 rc = smc_find_ism_device(new_smc, ini);
1771 if (rc)
Ursula Braun7affc802020-09-26 12:44:21 +02001772 goto not_found;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001773 ini->ism_selected = 0;
Karsten Graul37524042020-10-31 19:19:38 +01001774 rc = smc_listen_ism_init(new_smc, ini);
1775 if (!rc)
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001776 return; /* V1 ISM device found */
Ursula Braun7affc802020-09-26 12:44:21 +02001777
1778not_found:
Karsten Graul37524042020-10-31 19:19:38 +01001779 smc_find_ism_store_rc(rc, ini);
Karsten Graule49300a2021-10-16 11:37:46 +02001780 ini->smcd_version &= ~SMC_V1;
Ursula Braun3fc64932020-09-26 12:44:23 +02001781 ini->ism_dev[0] = NULL;
Ursula Braun7affc802020-09-26 12:44:21 +02001782 ini->is_smcd = false;
1783}
1784
Hans Wippel3b2dec22018-05-18 09:34:18 +02001785/* listen worker: register buffers */
Ursula Braun5ac54d82020-09-10 18:48:21 +02001786static int smc_listen_rdma_reg(struct smc_sock *new_smc, bool local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001787{
Karsten Graulb9247542020-04-29 17:10:41 +02001788 struct smc_connection *conn = &new_smc->conn;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001789
Ursula Braun5ac54d82020-09-10 18:48:21 +02001790 if (!local_first) {
Karsten Graul7562a132020-05-01 12:48:01 +02001791 if (smcr_lgr_reg_rmbs(conn->lnk, conn->rmb_desc))
Karsten Graulc7674c02018-11-22 10:26:43 +01001792 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001793 }
1794 smc_rmb_sync_sg_for_device(&new_smc->conn);
1795
1796 return 0;
1797}
1798
Karsten Graule49300a2021-10-16 11:37:46 +02001799static void smc_find_rdma_v2_device_serv(struct smc_sock *new_smc,
1800 struct smc_clc_msg_proposal *pclc,
1801 struct smc_init_info *ini)
1802{
1803 struct smc_clc_v2_extension *smc_v2_ext;
1804 u8 smcr_version;
1805 int rc;
1806
1807 if (!(ini->smcr_version & SMC_V2) || !smcr_indicated(ini->smc_type_v2))
1808 goto not_found;
1809
1810 smc_v2_ext = smc_get_clc_v2_ext(pclc);
1811 if (!smc_clc_match_eid(ini->negotiated_eid, smc_v2_ext, NULL, NULL))
1812 goto not_found;
1813
1814 /* prepare RDMA check */
1815 memcpy(ini->peer_systemid, pclc->lcl.id_for_peer, SMC_SYSTEMID_LEN);
1816 memcpy(ini->peer_gid, smc_v2_ext->roce, SMC_GID_SIZE);
1817 memcpy(ini->peer_mac, pclc->lcl.mac, ETH_ALEN);
1818 ini->check_smcrv2 = true;
1819 ini->smcrv2.clc_sk = new_smc->clcsock->sk;
1820 ini->smcrv2.saddr = new_smc->clcsock->sk->sk_rcv_saddr;
1821 ini->smcrv2.daddr = smc_ib_gid_to_ipv4(smc_v2_ext->roce);
1822 rc = smc_find_rdma_device(new_smc, ini);
1823 if (rc) {
1824 smc_find_ism_store_rc(rc, ini);
1825 goto not_found;
1826 }
1827 if (!ini->smcrv2.uses_gateway)
1828 memcpy(ini->smcrv2.nexthop_mac, pclc->lcl.mac, ETH_ALEN);
1829
1830 smcr_version = ini->smcr_version;
1831 ini->smcr_version = SMC_V2;
1832 rc = smc_listen_rdma_init(new_smc, ini);
1833 if (!rc)
1834 rc = smc_listen_rdma_reg(new_smc, ini->first_contact_local);
1835 if (!rc)
1836 return;
1837 ini->smcr_version = smcr_version;
1838 smc_find_ism_store_rc(rc, ini);
1839
1840not_found:
1841 ini->smcr_version &= ~SMC_V2;
1842 ini->check_smcrv2 = false;
1843}
1844
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001845static int smc_find_rdma_v1_device_serv(struct smc_sock *new_smc,
1846 struct smc_clc_msg_proposal *pclc,
1847 struct smc_init_info *ini)
Ursula Braun7affc802020-09-26 12:44:21 +02001848{
1849 int rc;
1850
Karsten Graule49300a2021-10-16 11:37:46 +02001851 if (!(ini->smcr_version & SMC_V1) || !smcr_indicated(ini->smc_type_v1))
Ursula Braun7affc802020-09-26 12:44:21 +02001852 return SMC_CLC_DECL_NOSMCDEV;
1853
1854 /* prepare RDMA check */
Karsten Graule49300a2021-10-16 11:37:46 +02001855 memcpy(ini->peer_systemid, pclc->lcl.id_for_peer, SMC_SYSTEMID_LEN);
1856 memcpy(ini->peer_gid, pclc->lcl.gid, SMC_GID_SIZE);
1857 memcpy(ini->peer_mac, pclc->lcl.mac, ETH_ALEN);
Ursula Braun7affc802020-09-26 12:44:21 +02001858 rc = smc_find_rdma_device(new_smc, ini);
1859 if (rc) {
1860 /* no RDMA device found */
Karsten Graule49300a2021-10-16 11:37:46 +02001861 return SMC_CLC_DECL_NOSMCDEV;
Ursula Braun7affc802020-09-26 12:44:21 +02001862 }
1863 rc = smc_listen_rdma_init(new_smc, ini);
1864 if (rc)
1865 return rc;
1866 return smc_listen_rdma_reg(new_smc, ini->first_contact_local);
1867}
1868
1869/* determine the local device matching to proposal */
1870static int smc_listen_find_device(struct smc_sock *new_smc,
1871 struct smc_clc_msg_proposal *pclc,
1872 struct smc_init_info *ini)
1873{
Karsten Graule49300a2021-10-16 11:37:46 +02001874 int prfx_rc;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001875
1876 /* check for ISM device matching V2 proposed device */
1877 smc_find_ism_v2_device_serv(new_smc, pclc, ini);
1878 if (ini->ism_dev[0])
Ursula Braun7affc802020-09-26 12:44:21 +02001879 return 0;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001880
Karsten Graule49300a2021-10-16 11:37:46 +02001881 /* check for matching IP prefix and subnet length (V1) */
1882 prfx_rc = smc_listen_prfx_check(new_smc, pclc);
1883 if (prfx_rc)
1884 smc_find_ism_store_rc(prfx_rc, ini);
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001885
1886 /* get vlan id from IP device */
1887 if (smc_vlan_by_tcpsk(new_smc->clcsock, ini))
Karsten Graul37524042020-10-31 19:19:38 +01001888 return ini->rc ?: SMC_CLC_DECL_GETVLANERR;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001889
1890 /* check for ISM device matching V1 proposed device */
Karsten Graule49300a2021-10-16 11:37:46 +02001891 if (!prfx_rc)
1892 smc_find_ism_v1_device_serv(new_smc, pclc, ini);
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001893 if (ini->ism_dev[0])
1894 return 0;
1895
Karsten Graule49300a2021-10-16 11:37:46 +02001896 if (!smcr_indicated(pclc->hdr.typev1) &&
1897 !smcr_indicated(pclc->hdr.typev2))
Karsten Graul37524042020-10-31 19:19:38 +01001898 /* skip RDMA and decline */
1899 return ini->rc ?: SMC_CLC_DECL_NOSMCDDEV;
Ursula Braun7affc802020-09-26 12:44:21 +02001900
Karsten Graule49300a2021-10-16 11:37:46 +02001901 /* check if RDMA V2 is available */
1902 smc_find_rdma_v2_device_serv(new_smc, pclc, ini);
1903 if (ini->smcrv2.ib_dev_v2)
1904 return 0;
Karsten Graul37524042020-10-31 19:19:38 +01001905
Karsten Graule49300a2021-10-16 11:37:46 +02001906 /* check if RDMA V1 is available */
1907 if (!prfx_rc) {
1908 int rc;
1909
1910 rc = smc_find_rdma_v1_device_serv(new_smc, pclc, ini);
1911 smc_find_ism_store_rc(rc, ini);
1912 return (!rc) ? 0 : ini->rc;
1913 }
1914 return SMC_CLC_DECL_NOSMCDEV;
Ursula Braun7affc802020-09-26 12:44:21 +02001915}
1916
Hans Wippel3b2dec22018-05-18 09:34:18 +02001917/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001918static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1919 struct smc_clc_msg_accept_confirm *cclc,
Karsten Graule49300a2021-10-16 11:37:46 +02001920 bool local_first,
1921 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001922{
Karsten Graul387707f2020-04-29 17:10:40 +02001923 struct smc_link *link = new_smc->conn.lnk;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001924 int reason_code = 0;
1925
Ursula Braun5ac54d82020-09-10 18:48:21 +02001926 if (local_first)
Karsten Graule49300a2021-10-16 11:37:46 +02001927 smc_link_save_peer_info(link, cclc, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001928
Ursula Braun0c881ad2020-09-10 18:48:25 +02001929 if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc))
1930 return SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001931
Ursula Braun5ac54d82020-09-10 18:48:21 +02001932 if (local_first) {
Ursula Braun0c881ad2020-09-10 18:48:25 +02001933 if (smc_ib_ready_link(link))
1934 return SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001935 /* QP confirmation over RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001936 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
Karsten Graulb9247542020-04-29 17:10:41 +02001937 reason_code = smcr_serv_conf_first_link(new_smc);
Karsten Graul4667bb42020-04-30 15:55:42 +02001938 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001939 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001940 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001941}
1942
Ursula Braun7affc802020-09-26 12:44:21 +02001943/* setup for connection of server */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001944static void smc_listen_work(struct work_struct *work)
1945{
1946 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1947 smc_listen_work);
1948 struct socket *newclcsock = new_smc->clcsock;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001949 struct smc_clc_msg_accept_confirm *cclc;
Ursula Braun6bb14e482020-09-10 18:48:22 +02001950 struct smc_clc_msg_proposal_area *buf;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001951 struct smc_clc_msg_proposal *pclc;
Ursula Braun3fc64932020-09-26 12:44:23 +02001952 struct smc_init_info *ini = NULL;
Karsten Graule49300a2021-10-16 11:37:46 +02001953 u8 proposal_version = SMC_V1;
1954 u8 accept_version;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001955 int rc = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001956
Karsten Graulfd577702019-04-11 11:17:30 +02001957 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1958 return smc_listen_out_err(new_smc);
1959
Hans Wippel3b2dec22018-05-18 09:34:18 +02001960 if (new_smc->use_fallback) {
1961 smc_listen_out_connected(new_smc);
1962 return;
1963 }
1964
1965 /* check if peer is smc capable */
1966 if (!tcp_sk(newclcsock->sk)->syn_smc) {
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02001967 smc_switch_to_fallback(new_smc, SMC_CLC_DECL_PEERNOSMC);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001968 smc_listen_out_connected(new_smc);
1969 return;
1970 }
1971
1972 /* do inband token exchange -
1973 * wait for and receive SMC Proposal CLC message
1974 */
Ursula Braun6bb14e482020-09-10 18:48:22 +02001975 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
1976 if (!buf) {
1977 rc = SMC_CLC_DECL_MEM;
1978 goto out_decl;
1979 }
1980 pclc = (struct smc_clc_msg_proposal *)buf;
1981 rc = smc_clc_wait_msg(new_smc, pclc, sizeof(*buf),
Karsten Graul228bae02019-04-12 12:57:28 +02001982 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001983 if (rc)
1984 goto out_decl;
Karsten Graule49300a2021-10-16 11:37:46 +02001985
1986 if (pclc->hdr.version > SMC_V1)
1987 proposal_version = SMC_V2;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001988
Ursula Braune8d726c82020-09-26 12:44:32 +02001989 /* IPSec connections opt out of SMC optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001990 if (using_ipsec(new_smc)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001991 rc = SMC_CLC_DECL_IPSEC;
1992 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001993 }
1994
Ursula Braun3fc64932020-09-26 12:44:23 +02001995 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
1996 if (!ini) {
1997 rc = SMC_CLC_DECL_MEM;
1998 goto out_decl;
1999 }
2000
Ursula Braun5c21c4c2020-09-26 12:44:29 +02002001 /* initial version checking */
2002 rc = smc_listen_v2_check(new_smc, pclc, ini);
2003 if (rc)
Karsten Graul9aa68d22019-04-12 12:57:29 +02002004 goto out_decl;
Karsten Graulfba7e8e2019-04-12 12:57:27 +02002005
Hans Wippel72a36a82019-02-07 15:56:18 +01002006 mutex_lock(&smc_server_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02002007 smc_close_init(new_smc);
2008 smc_rx_init(new_smc);
2009 smc_tx_init(new_smc);
2010
Ursula Braun7affc802020-09-26 12:44:21 +02002011 /* determine ISM or RoCE device used for connection */
Ursula Braun3fc64932020-09-26 12:44:23 +02002012 rc = smc_listen_find_device(new_smc, pclc, ini);
Ursula Braun7affc802020-09-26 12:44:21 +02002013 if (rc)
2014 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02002015
2016 /* send SMC Accept CLC message */
Karsten Graule49300a2021-10-16 11:37:46 +02002017 accept_version = ini->is_smcd ? ini->smcd_version : ini->smcr_version;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02002018 rc = smc_clc_send_accept(new_smc, ini->first_contact_local,
Karsten Graule49300a2021-10-16 11:37:46 +02002019 accept_version, ini->negotiated_eid);
Karsten Graul9aa68d22019-04-12 12:57:29 +02002020 if (rc)
2021 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02002022
Hans Wippel62c71392019-02-07 15:56:17 +01002023 /* SMC-D does not need this lock any more */
Ursula Braun3fc64932020-09-26 12:44:23 +02002024 if (ini->is_smcd)
Hans Wippel72a36a82019-02-07 15:56:18 +01002025 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01002026
Hans Wippel3b2dec22018-05-18 09:34:18 +02002027 /* receive SMC Confirm CLC message */
Karsten Graul9047a617dc2020-10-07 22:57:42 +02002028 memset(buf, 0, sizeof(*buf));
2029 cclc = (struct smc_clc_msg_accept_confirm *)buf;
2030 rc = smc_clc_wait_msg(new_smc, cclc, sizeof(*buf),
Karsten Graul228bae02019-04-12 12:57:28 +02002031 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
2032 if (rc) {
Ursula Braun3fc64932020-09-26 12:44:23 +02002033 if (!ini->is_smcd)
Karsten Graul9aa68d22019-04-12 12:57:29 +02002034 goto out_unlock;
2035 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02002036 }
2037
2038 /* finish worker */
Ursula Braun3fc64932020-09-26 12:44:23 +02002039 if (!ini->is_smcd) {
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02002040 rc = smc_listen_rdma_finish(new_smc, cclc,
Karsten Graule49300a2021-10-16 11:37:46 +02002041 ini->first_contact_local, ini);
Hans Wippel62c71392019-02-07 15:56:17 +01002042 if (rc)
Ursula Braun0c881ad2020-09-10 18:48:25 +02002043 goto out_unlock;
2044 mutex_unlock(&smc_server_lgr_pending);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02002045 }
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02002046 smc_conn_save_peer_info(new_smc, cclc);
Hans Wippel3b2dec22018-05-18 09:34:18 +02002047 smc_listen_out_connected(new_smc);
Guvenc Gulce194730a2021-06-16 16:52:58 +02002048 SMC_STAT_SERV_SUCC_INC(sock_net(newclcsock->sk), ini);
Ursula Braunac679362020-09-17 22:46:02 +02002049 goto out_free;
Karsten Graul9aa68d22019-04-12 12:57:29 +02002050
2051out_unlock:
2052 mutex_unlock(&smc_server_lgr_pending);
2053out_decl:
Karsten Graul4a9baf42020-10-23 20:48:28 +02002054 smc_listen_decline(new_smc, rc, ini ? ini->first_contact_local : 0,
Karsten Graule49300a2021-10-16 11:37:46 +02002055 proposal_version);
Ursula Braunac679362020-09-17 22:46:02 +02002056out_free:
Ursula Braun3fc64932020-09-26 12:44:23 +02002057 kfree(ini);
Ursula Braun6bb14e482020-09-10 18:48:22 +02002058 kfree(buf);
Ursula Brauna046d572017-01-09 16:55:16 +01002059}
2060
2061static void smc_tcp_listen_work(struct work_struct *work)
2062{
2063 struct smc_sock *lsmc = container_of(work, struct smc_sock,
2064 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01002065 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01002066 struct smc_sock *new_smc;
2067 int rc = 0;
2068
Ursula Braun3163c502018-01-24 10:28:12 +01002069 lock_sock(lsk);
2070 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01002071 rc = smc_clcsock_accept(lsmc, &new_smc);
Ursula Brauna60a2b12020-09-10 18:48:20 +02002072 if (rc) /* clcsock accept queue empty or error */
Ursula Brauna046d572017-01-09 16:55:16 +01002073 goto out;
2074 if (!new_smc)
2075 continue;
2076
2077 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002078 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02002079 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01002080 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01002081 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
2082 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02002083 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
2084 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01002085 sock_hold(&new_smc->sk); /* sock_put in passive closing */
Karsten Graul22ef4732020-09-10 18:48:29 +02002086 if (!queue_work(smc_hs_wq, &new_smc->smc_listen_work))
Ursula Braun51f1de72018-01-26 09:28:48 +01002087 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01002088 }
2089
2090out:
Ursula Braun3163c502018-01-24 10:28:12 +01002091 release_sock(lsk);
Ursula Brauna60a2b12020-09-10 18:48:20 +02002092 sock_put(&lsmc->sk); /* sock_hold in smc_clcsock_data_ready() */
2093}
2094
2095static void smc_clcsock_data_ready(struct sock *listen_clcsock)
2096{
2097 struct smc_sock *lsmc;
2098
2099 lsmc = (struct smc_sock *)
2100 ((uintptr_t)listen_clcsock->sk_user_data & ~SK_USER_DATA_NOCOPY);
2101 if (!lsmc)
2102 return;
2103 lsmc->clcsk_data_ready(listen_clcsock);
2104 if (lsmc->sk.sk_state == SMC_LISTEN) {
2105 sock_hold(&lsmc->sk); /* sock_put in smc_tcp_listen_work() */
Karsten Graul22ef4732020-09-10 18:48:29 +02002106 if (!queue_work(smc_hs_wq, &lsmc->tcp_listen_work))
Ursula Brauna60a2b12020-09-10 18:48:20 +02002107 sock_put(&lsmc->sk);
2108 }
Ursula Brauna046d572017-01-09 16:55:16 +01002109}
2110
Ursula Braunac713872017-01-09 16:55:13 +01002111static int smc_listen(struct socket *sock, int backlog)
2112{
2113 struct sock *sk = sock->sk;
2114 struct smc_sock *smc;
2115 int rc;
2116
2117 smc = smc_sk(sk);
2118 lock_sock(sk);
2119
2120 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +02002121 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
2122 smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +01002123 goto out;
2124
2125 rc = 0;
2126 if (sk->sk_state == SMC_LISTEN) {
2127 sk->sk_max_ack_backlog = backlog;
2128 goto out;
2129 }
2130 /* some socket options are handled in core, so we could not apply
2131 * them to the clc socket -- copy smc socket options to clc socket
2132 */
2133 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002134 if (!smc->use_fallback)
2135 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01002136
Ursula Brauna60a2b12020-09-10 18:48:20 +02002137 /* save original sk_data_ready function and establish
2138 * smc-specific sk_data_ready function
2139 */
2140 smc->clcsk_data_ready = smc->clcsock->sk->sk_data_ready;
2141 smc->clcsock->sk->sk_data_ready = smc_clcsock_data_ready;
2142 smc->clcsock->sk->sk_user_data =
2143 (void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY);
Ursula Braunac713872017-01-09 16:55:13 +01002144 rc = kernel_listen(smc->clcsock, backlog);
Guo DaXing9ebb0c42021-11-24 13:32:38 +01002145 if (rc) {
2146 smc->clcsock->sk->sk_data_ready = smc->clcsk_data_ready;
Ursula Braunac713872017-01-09 16:55:13 +01002147 goto out;
Guo DaXing9ebb0c42021-11-24 13:32:38 +01002148 }
Ursula Braunac713872017-01-09 16:55:13 +01002149 sk->sk_max_ack_backlog = backlog;
2150 sk->sk_ack_backlog = 0;
2151 sk->sk_state = SMC_LISTEN;
2152
2153out:
2154 release_sock(sk);
2155 return rc;
2156}
2157
2158static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00002159 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01002160{
Ursula Brauna046d572017-01-09 16:55:16 +01002161 struct sock *sk = sock->sk, *nsk;
2162 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01002163 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01002164 long timeo;
2165 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01002166
2167 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01002168 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01002169 lock_sock(sk);
2170
2171 if (lsmc->sk.sk_state != SMC_LISTEN) {
2172 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02002173 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01002174 goto out;
2175 }
2176
Ursula Brauna046d572017-01-09 16:55:16 +01002177 /* Wait for an incoming connection */
2178 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2179 add_wait_queue_exclusive(sk_sleep(sk), &wait);
2180 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
2181 set_current_state(TASK_INTERRUPTIBLE);
2182 if (!timeo) {
2183 rc = -EAGAIN;
2184 break;
2185 }
2186 release_sock(sk);
2187 timeo = schedule_timeout(timeo);
2188 /* wakeup by sk_data_ready in smc_listen_work() */
2189 sched_annotate_sleep();
2190 lock_sock(sk);
2191 if (signal_pending(current)) {
2192 rc = sock_intr_errno(timeo);
2193 break;
2194 }
2195 }
2196 set_current_state(TASK_RUNNING);
2197 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01002198
Ursula Brauna046d572017-01-09 16:55:16 +01002199 if (!rc)
2200 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02002201 release_sock(sk);
2202 if (rc)
2203 goto out;
2204
2205 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
2206 /* wait till data arrives on the socket */
2207 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
2208 MSEC_PER_SEC);
2209 if (smc_sk(nsk)->use_fallback) {
2210 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
2211
2212 lock_sock(clcsk);
2213 if (skb_queue_empty(&clcsk->sk_receive_queue))
2214 sk_wait_data(clcsk, &timeo, NULL);
2215 release_sock(clcsk);
2216 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
2217 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02002218 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02002219 release_sock(nsk);
2220 }
2221 }
Ursula Braunac713872017-01-09 16:55:13 +01002222
2223out:
Ursula Braun51f1de72018-01-26 09:28:48 +01002224 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01002225 return rc;
2226}
2227
2228static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01002229 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01002230{
2231 struct smc_sock *smc;
2232
Ursula Braunb38d7322017-01-09 16:55:25 +01002233 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
2234 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01002235 return -ENOTCONN;
2236
2237 smc = smc_sk(sock->sk);
2238
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01002239 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01002240}
2241
2242static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
2243{
2244 struct sock *sk = sock->sk;
2245 struct smc_sock *smc;
2246 int rc = -EPIPE;
2247
2248 smc = smc_sk(sk);
2249 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01002250 if ((sk->sk_state != SMC_ACTIVE) &&
2251 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2252 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01002253 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002254
2255 if (msg->msg_flags & MSG_FASTOPEN) {
Ursula Brauncd206362019-08-02 10:47:50 +02002256 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002257 smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002258 } else {
2259 rc = -EINVAL;
2260 goto out;
2261 }
2262 }
2263
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002264 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01002265 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002266 } else {
Ursula Braune6727f32017-01-09 16:55:23 +01002267 rc = smc_tx_sendmsg(smc, msg, len);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002268 SMC_STAT_TX_PAYLOAD(smc, len, rc);
2269 }
Ursula Braunac713872017-01-09 16:55:13 +01002270out:
2271 release_sock(sk);
2272 return rc;
2273}
2274
2275static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
2276 int flags)
2277{
2278 struct sock *sk = sock->sk;
2279 struct smc_sock *smc;
2280 int rc = -ENOTCONN;
2281
2282 smc = smc_sk(sk);
2283 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01002284 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2285 /* socket was connected before, no more data to read */
2286 rc = 0;
2287 goto out;
2288 }
Ursula Braunb38d7322017-01-09 16:55:25 +01002289 if ((sk->sk_state == SMC_INIT) ||
2290 (sk->sk_state == SMC_LISTEN) ||
2291 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01002292 goto out;
2293
Ursula Braunb38d7322017-01-09 16:55:25 +01002294 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2295 rc = 0;
2296 goto out;
2297 }
2298
Stefan Raspl9014db22018-05-03 18:12:39 +02002299 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01002300 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02002301 } else {
2302 msg->msg_namelen = 0;
2303 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002304 SMC_STAT_RX_PAYLOAD(smc, rc, rc);
Stefan Raspl9014db22018-05-03 18:12:39 +02002305 }
Ursula Braunb38d7322017-01-09 16:55:25 +01002306
Ursula Braunac713872017-01-09 16:55:13 +01002307out:
2308 release_sock(sk);
2309 return rc;
2310}
2311
Al Viroade994f2017-07-03 00:01:49 -04002312static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01002313{
Ursula Braun8dce2782018-01-26 09:28:47 +01002314 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05002315 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01002316
Ursula Braun8dce2782018-01-26 09:28:47 +01002317 spin_lock(&isk->accept_q_lock);
2318 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002319 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01002320 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01002321
Ursula Braun8dce2782018-01-26 09:28:47 +01002322 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01002323}
2324
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07002325static __poll_t smc_poll(struct file *file, struct socket *sock,
2326 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01002327{
2328 struct sock *sk = sock->sk;
Ursula Braunac713872017-01-09 16:55:13 +01002329 struct smc_sock *smc;
Ursula Braun50717a32019-04-12 12:57:23 +02002330 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01002331
Ursula Braun8dce2782018-01-26 09:28:47 +01002332 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002333 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01002334
Ursula Braunac713872017-01-09 16:55:13 +01002335 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02002336 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01002337 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07002338 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02002339 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braunac713872017-01-09 16:55:13 +01002340 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07002341 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02002342 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01002343 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002344 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01002345 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
2346 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002347 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01002348 if (sk->sk_state == SMC_LISTEN) {
2349 /* woken up by sk_data_ready in smc_listen_work() */
Ursula Braun50717a32019-04-12 12:57:23 +02002350 mask |= smc_accept_poll(sk);
2351 } else if (smc->use_fallback) { /* as result of connect_work()*/
2352 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
2353 wait);
2354 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun8dce2782018-01-26 09:28:47 +01002355 } else {
Ursula Braun50717a32019-04-12 12:57:23 +02002356 if ((sk->sk_state != SMC_INIT &&
2357 atomic_read(&smc->conn.sndbuf_space)) ||
Ursula Braun8dce2782018-01-26 09:28:47 +01002358 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002359 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01002360 } else {
2361 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
2362 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
2363 }
2364 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002365 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01002366 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002367 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01002368 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002369 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02002370 if (smc->conn.urg_state == SMC_URG_VALID)
2371 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01002372 }
Ursula Braunac713872017-01-09 16:55:13 +01002373 }
2374
2375 return mask;
2376}
2377
2378static int smc_shutdown(struct socket *sock, int how)
2379{
2380 struct sock *sk = sock->sk;
Tony Lubacb6c12021-11-26 10:41:35 +08002381 bool do_shutdown = true;
Ursula Braunac713872017-01-09 16:55:13 +01002382 struct smc_sock *smc;
2383 int rc = -EINVAL;
Tony Lubacb6c12021-11-26 10:41:35 +08002384 int old_state;
Ursula Braunb38d7322017-01-09 16:55:25 +01002385 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01002386
2387 smc = smc_sk(sk);
2388
2389 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01002390 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01002391
2392 lock_sock(sk);
2393
2394 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02002395 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01002396 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
2397 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
2398 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2399 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
2400 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01002401 goto out;
2402 if (smc->use_fallback) {
2403 rc = kernel_sock_shutdown(smc->clcsock, how);
2404 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
2405 if (sk->sk_shutdown == SHUTDOWN_MASK)
2406 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01002407 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01002408 }
Ursula Braunb38d7322017-01-09 16:55:25 +01002409 switch (how) {
2410 case SHUT_RDWR: /* shutdown in both directions */
Tony Lubacb6c12021-11-26 10:41:35 +08002411 old_state = sk->sk_state;
Ursula Braunb38d7322017-01-09 16:55:25 +01002412 rc = smc_close_active(smc);
Tony Lubacb6c12021-11-26 10:41:35 +08002413 if (old_state == SMC_ACTIVE &&
2414 sk->sk_state == SMC_PEERCLOSEWAIT1)
2415 do_shutdown = false;
Ursula Braunb38d7322017-01-09 16:55:25 +01002416 break;
2417 case SHUT_WR:
2418 rc = smc_close_shutdown_write(smc);
2419 break;
2420 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02002421 rc = 0;
2422 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01002423 break;
2424 }
Tony Lubacb6c12021-11-26 10:41:35 +08002425 if (do_shutdown && smc->clcsock)
Ursula Braun1255fcb2018-04-19 15:56:40 +02002426 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01002427 /* map sock_shutdown_cmd constants to sk_shutdown value range */
2428 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01002429
2430out:
2431 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01002432 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01002433}
2434
2435static int smc_setsockopt(struct socket *sock, int level, int optname,
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02002436 sockptr_t optval, unsigned int optlen)
Ursula Braunac713872017-01-09 16:55:13 +01002437{
2438 struct sock *sk = sock->sk;
2439 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002440 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01002441
Cong Wang86214362021-05-05 12:40:48 -07002442 if (level == SOL_TCP && optname == TCP_ULP)
2443 return -EOPNOTSUPP;
2444
Ursula Braunac713872017-01-09 16:55:13 +01002445 smc = smc_sk(sk);
2446
2447 /* generic setsockopts reaching us here always apply to the
2448 * CLC socket
2449 */
Christoph Hellwiga44d9e72020-07-17 08:23:31 +02002450 if (unlikely(!smc->clcsock->ops->setsockopt))
2451 rc = -EOPNOTSUPP;
2452 else
2453 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
2454 optval, optlen);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002455 if (smc->clcsock->sk->sk_err) {
2456 sk->sk_err = smc->clcsock->sk->sk_err;
Alexander Aringe3ae2362021-06-27 18:48:21 -04002457 sk_error_report(sk);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002458 }
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002459
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002460 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00002461 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02002462 if (copy_from_sockptr(&val, optval, sizeof(int)))
Ursula Braunac0107e2018-07-18 15:22:50 +02002463 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002464
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002465 lock_sock(sk);
Ursula Braun86434742019-12-12 22:35:58 +01002466 if (rc || smc->use_fallback)
2467 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002468 switch (optname) {
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002469 case TCP_FASTOPEN:
2470 case TCP_FASTOPEN_CONNECT:
2471 case TCP_FASTOPEN_KEY:
2472 case TCP_FASTOPEN_NO_COOKIE:
2473 /* option not supported by SMC */
Ursula Braun8204df72019-11-15 12:39:30 +01002474 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002475 smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002476 } else {
Ursula Braun86434742019-12-12 22:35:58 +01002477 rc = -EINVAL;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002478 }
2479 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002480 case TCP_NODELAY:
Ursula Braunf9cedf12019-08-02 10:16:38 +02002481 if (sk->sk_state != SMC_INIT &&
2482 sk->sk_state != SMC_LISTEN &&
2483 sk->sk_state != SMC_CLOSED) {
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002484 if (val) {
Guvenc Gulce194730a2021-06-16 16:52:58 +02002485 SMC_STAT_INC(smc, ndly_cnt);
Karsten Graul22ef4732020-09-10 18:48:29 +02002486 mod_delayed_work(smc->conn.lgr->tx_wq,
2487 &smc->conn.tx_work, 0);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002488 }
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002489 }
2490 break;
2491 case TCP_CORK:
Ursula Braunf9cedf12019-08-02 10:16:38 +02002492 if (sk->sk_state != SMC_INIT &&
2493 sk->sk_state != SMC_LISTEN &&
2494 sk->sk_state != SMC_CLOSED) {
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002495 if (!val) {
Guvenc Gulce194730a2021-06-16 16:52:58 +02002496 SMC_STAT_INC(smc, cork_cnt);
Karsten Graul22ef4732020-09-10 18:48:29 +02002497 mod_delayed_work(smc->conn.lgr->tx_wq,
2498 &smc->conn.tx_work, 0);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002499 }
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002500 }
2501 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02002502 case TCP_DEFER_ACCEPT:
2503 smc->sockopt_defer_accept = val;
2504 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002505 default:
2506 break;
2507 }
Ursula Braun86434742019-12-12 22:35:58 +01002508out:
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002509 release_sock(sk);
2510
2511 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01002512}
2513
2514static int smc_getsockopt(struct socket *sock, int level, int optname,
2515 char __user *optval, int __user *optlen)
2516{
2517 struct smc_sock *smc;
2518
2519 smc = smc_sk(sock->sk);
2520 /* socket options apply to the CLC socket */
Christoph Hellwiga44d9e72020-07-17 08:23:31 +02002521 if (unlikely(!smc->clcsock->ops->getsockopt))
2522 return -EOPNOTSUPP;
Ursula Braunac713872017-01-09 16:55:13 +01002523 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
2524 optval, optlen);
2525}
2526
2527static int smc_ioctl(struct socket *sock, unsigned int cmd,
2528 unsigned long arg)
2529{
Stefan Rasplde8474e2018-05-23 16:38:11 +02002530 union smc_host_cursor cons, urg;
2531 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01002532 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02002533 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01002534
2535 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02002536 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02002537 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02002538 if (smc->use_fallback) {
2539 if (!smc->clcsock) {
2540 release_sock(&smc->sk);
2541 return -EBADF;
2542 }
2543 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
2544 release_sock(&smc->sk);
2545 return answ;
2546 }
Ursula Braun9b67e262018-05-02 16:56:46 +02002547 switch (cmd) {
2548 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02002549 if (smc->sk.sk_state == SMC_LISTEN) {
2550 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002551 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002552 }
Ursula Braun2351abe2018-05-23 16:38:09 +02002553 if (smc->sk.sk_state == SMC_INIT ||
2554 smc->sk.sk_state == SMC_CLOSED)
2555 answ = 0;
2556 else
2557 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02002558 break;
2559 case SIOCOUTQ:
2560 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02002561 if (smc->sk.sk_state == SMC_LISTEN) {
2562 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002563 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002564 }
Ursula Braun2351abe2018-05-23 16:38:09 +02002565 if (smc->sk.sk_state == SMC_INIT ||
2566 smc->sk.sk_state == SMC_CLOSED)
2567 answ = 0;
2568 else
2569 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02002570 atomic_read(&smc->conn.sndbuf_space);
2571 break;
2572 case SIOCOUTQNSD:
2573 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02002574 if (smc->sk.sk_state == SMC_LISTEN) {
2575 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002576 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002577 }
Ursula Braun2351abe2018-05-23 16:38:09 +02002578 if (smc->sk.sk_state == SMC_INIT ||
2579 smc->sk.sk_state == SMC_CLOSED)
2580 answ = 0;
2581 else
2582 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02002583 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02002584 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02002585 if (smc->sk.sk_state == SMC_LISTEN) {
2586 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02002587 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002588 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02002589 if (smc->sk.sk_state == SMC_INIT ||
2590 smc->sk.sk_state == SMC_CLOSED) {
2591 answ = 0;
2592 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02002593 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
2594 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02002595 answ = smc_curs_diff(conn->rmb_desc->len,
2596 &cons, &urg) == 1;
2597 }
2598 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02002599 default:
Ursula Braun1992d992018-07-16 13:56:52 +02002600 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002601 return -ENOIOCTLCMD;
2602 }
Ursula Braun1992d992018-07-16 13:56:52 +02002603 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002604
2605 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01002606}
2607
2608static ssize_t smc_sendpage(struct socket *sock, struct page *page,
2609 int offset, size_t size, int flags)
2610{
2611 struct sock *sk = sock->sk;
2612 struct smc_sock *smc;
2613 int rc = -EPIPE;
2614
2615 smc = smc_sk(sk);
2616 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02002617 if (sk->sk_state != SMC_ACTIVE) {
2618 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01002619 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02002620 }
2621 release_sock(sk);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002622 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01002623 rc = kernel_sendpage(smc->clcsock, page, offset,
2624 size, flags);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002625 } else {
Guvenc Gulce194730a2021-06-16 16:52:58 +02002626 SMC_STAT_INC(smc, sendpage_cnt);
Ursula Braunac713872017-01-09 16:55:13 +01002627 rc = sock_no_sendpage(sock, page, offset, size, flags);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002628 }
Ursula Braunac713872017-01-09 16:55:13 +01002629
2630out:
Ursula Braunac713872017-01-09 16:55:13 +01002631 return rc;
2632}
2633
Stefan Raspl9014db22018-05-03 18:12:39 +02002634/* Map the affected portions of the rmbe into an spd, note the number of bytes
2635 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
2636 * updates till whenever a respective page has been fully processed.
2637 * Note that subsequent recv() calls have to wait till all splice() processing
2638 * completed.
2639 */
Ursula Braunac713872017-01-09 16:55:13 +01002640static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
2641 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02002642 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01002643{
2644 struct sock *sk = sock->sk;
2645 struct smc_sock *smc;
2646 int rc = -ENOTCONN;
2647
2648 smc = smc_sk(sk);
2649 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01002650 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2651 /* socket was connected before, no more data to read */
2652 rc = 0;
2653 goto out;
2654 }
Stefan Raspl9014db22018-05-03 18:12:39 +02002655 if (sk->sk_state == SMC_INIT ||
2656 sk->sk_state == SMC_LISTEN ||
2657 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01002658 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02002659
2660 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2661 rc = 0;
2662 goto out;
2663 }
2664
Ursula Braunac713872017-01-09 16:55:13 +01002665 if (smc->use_fallback) {
2666 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
2667 pipe, len, flags);
2668 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02002669 if (*ppos) {
2670 rc = -ESPIPE;
2671 goto out;
2672 }
2673 if (flags & SPLICE_F_NONBLOCK)
2674 flags = MSG_DONTWAIT;
2675 else
2676 flags = 0;
Guvenc Gulce194730a2021-06-16 16:52:58 +02002677 SMC_STAT_INC(smc, splice_cnt);
Stefan Raspl9014db22018-05-03 18:12:39 +02002678 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01002679 }
2680out:
2681 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02002682
Ursula Braunac713872017-01-09 16:55:13 +01002683 return rc;
2684}
2685
2686/* must look like tcp */
2687static const struct proto_ops smc_sock_ops = {
2688 .family = PF_SMC,
2689 .owner = THIS_MODULE,
2690 .release = smc_release,
2691 .bind = smc_bind,
2692 .connect = smc_connect,
2693 .socketpair = sock_no_socketpair,
2694 .accept = smc_accept,
2695 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07002696 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01002697 .ioctl = smc_ioctl,
2698 .listen = smc_listen,
2699 .shutdown = smc_shutdown,
2700 .setsockopt = smc_setsockopt,
2701 .getsockopt = smc_getsockopt,
2702 .sendmsg = smc_sendmsg,
2703 .recvmsg = smc_recvmsg,
2704 .mmap = sock_no_mmap,
2705 .sendpage = smc_sendpage,
2706 .splice_read = smc_splice_read,
2707};
2708
Tony Lud7cd4212021-12-28 21:44:36 +08002709static int __smc_create(struct net *net, struct socket *sock, int protocol,
2710 int kern, struct socket *clcsock)
Ursula Braunac713872017-01-09 16:55:13 +01002711{
Karsten Graulaaa4d332018-03-16 15:06:41 +01002712 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01002713 struct smc_sock *smc;
2714 struct sock *sk;
2715 int rc;
2716
2717 rc = -ESOCKTNOSUPPORT;
2718 if (sock->type != SOCK_STREAM)
2719 goto out;
2720
2721 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002722 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01002723 goto out;
2724
2725 rc = -ENOBUFS;
2726 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002727 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01002728 if (!sk)
2729 goto out;
2730
2731 /* create internal TCP socket for CLC handshake and fallback */
2732 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01002733 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02002734 smc->fallback_rsn = 0;
Tony Lud7cd4212021-12-28 21:44:36 +08002735
2736 rc = 0;
2737 if (!clcsock) {
2738 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
2739 &smc->clcsock);
2740 if (rc) {
2741 sk_common_release(sk);
2742 goto out;
2743 }
2744 } else {
2745 smc->clcsock = clcsock;
Davide Carattia5dcb732018-02-28 12:44:09 +01002746 }
Tony Lud7cd4212021-12-28 21:44:36 +08002747
Ursula Brauncd6851f2017-01-09 16:55:18 +01002748 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
2749 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01002750
2751out:
2752 return rc;
2753}
2754
Tony Lud7cd4212021-12-28 21:44:36 +08002755static int smc_create(struct net *net, struct socket *sock, int protocol,
2756 int kern)
2757{
2758 return __smc_create(net, sock, protocol, kern, NULL);
2759}
2760
Ursula Braunac713872017-01-09 16:55:13 +01002761static const struct net_proto_family smc_sock_family_ops = {
2762 .family = PF_SMC,
2763 .owner = THIS_MODULE,
2764 .create = smc_create,
2765};
2766
Tony Lud7cd4212021-12-28 21:44:36 +08002767static int smc_ulp_init(struct sock *sk)
2768{
2769 struct socket *tcp = sk->sk_socket;
2770 struct net *net = sock_net(sk);
2771 struct socket *smcsock;
2772 int protocol, ret;
2773
2774 /* only TCP can be replaced */
2775 if (tcp->type != SOCK_STREAM || sk->sk_protocol != IPPROTO_TCP ||
2776 (sk->sk_family != AF_INET && sk->sk_family != AF_INET6))
2777 return -ESOCKTNOSUPPORT;
2778 /* don't handle wq now */
2779 if (tcp->state != SS_UNCONNECTED || !tcp->file || tcp->wq.fasync_list)
2780 return -ENOTCONN;
2781
2782 if (sk->sk_family == AF_INET)
2783 protocol = SMCPROTO_SMC;
2784 else
2785 protocol = SMCPROTO_SMC6;
2786
2787 smcsock = sock_alloc();
2788 if (!smcsock)
2789 return -ENFILE;
2790
2791 smcsock->type = SOCK_STREAM;
2792 __module_get(THIS_MODULE); /* tried in __tcp_ulp_find_autoload */
2793 ret = __smc_create(net, smcsock, protocol, 1, tcp);
2794 if (ret) {
2795 sock_release(smcsock); /* module_put() which ops won't be NULL */
2796 return ret;
2797 }
2798
2799 /* replace tcp socket to smc */
2800 smcsock->file = tcp->file;
2801 smcsock->file->private_data = smcsock;
2802 smcsock->file->f_inode = SOCK_INODE(smcsock); /* replace inode when sock_close */
2803 smcsock->file->f_path.dentry->d_inode = SOCK_INODE(smcsock); /* dput() in __fput */
2804 tcp->file = NULL;
2805
2806 return ret;
2807}
2808
2809static void smc_ulp_clone(const struct request_sock *req, struct sock *newsk,
2810 const gfp_t priority)
2811{
2812 struct inet_connection_sock *icsk = inet_csk(newsk);
2813
2814 /* don't inherit ulp ops to child when listen */
2815 icsk->icsk_ulp_ops = NULL;
2816}
2817
2818static struct tcp_ulp_ops smc_ulp_ops __read_mostly = {
2819 .name = "smc",
2820 .owner = THIS_MODULE,
2821 .init = smc_ulp_init,
2822 .clone = smc_ulp_clone,
2823};
2824
Hans Wippel64e28b52019-02-21 13:01:02 +01002825unsigned int smc_net_id;
2826
2827static __net_init int smc_net_init(struct net *net)
2828{
2829 return smc_pnet_net_init(net);
2830}
2831
2832static void __net_exit smc_net_exit(struct net *net)
2833{
2834 smc_pnet_net_exit(net);
2835}
2836
Guvenc Gulce194730a2021-06-16 16:52:58 +02002837static __net_init int smc_net_stat_init(struct net *net)
2838{
2839 return smc_stats_init(net);
2840}
2841
2842static void __net_exit smc_net_stat_exit(struct net *net)
2843{
2844 smc_stats_exit(net);
2845}
2846
Hans Wippel64e28b52019-02-21 13:01:02 +01002847static struct pernet_operations smc_net_ops = {
2848 .init = smc_net_init,
2849 .exit = smc_net_exit,
2850 .id = &smc_net_id,
2851 .size = sizeof(struct smc_net),
2852};
2853
Guvenc Gulce194730a2021-06-16 16:52:58 +02002854static struct pernet_operations smc_net_stat_ops = {
2855 .init = smc_net_stat_init,
2856 .exit = smc_net_stat_exit,
2857};
2858
Ursula Braunac713872017-01-09 16:55:13 +01002859static int __init smc_init(void)
2860{
2861 int rc;
2862
Hans Wippel64e28b52019-02-21 13:01:02 +01002863 rc = register_pernet_subsys(&smc_net_ops);
2864 if (rc)
2865 return rc;
2866
Guvenc Gulce194730a2021-06-16 16:52:58 +02002867 rc = register_pernet_subsys(&smc_net_stat_ops);
2868 if (rc)
2869 return rc;
2870
Ursula Braun201091e2020-09-26 12:44:24 +02002871 smc_ism_init();
Ursula Braunb81a5eb2020-09-26 12:44:31 +02002872 smc_clc_init();
Ursula Braun201091e2020-09-26 12:44:24 +02002873
Guvenc Gulcee8372d92020-12-01 20:20:43 +01002874 rc = smc_nl_init();
Thomas Richter6812baa2017-01-09 16:55:15 +01002875 if (rc)
YueHaibing8c33bf12019-06-26 17:47:50 +02002876 goto out_pernet_subsys;
Thomas Richter6812baa2017-01-09 16:55:15 +01002877
Guvenc Gulcee8372d92020-12-01 20:20:43 +01002878 rc = smc_pnet_init();
2879 if (rc)
2880 goto out_nl;
2881
Karsten Graul22ef4732020-09-10 18:48:29 +02002882 rc = -ENOMEM;
2883 smc_hs_wq = alloc_workqueue("smc_hs_wq", 0, 0);
2884 if (!smc_hs_wq)
2885 goto out_pnet;
2886
2887 smc_close_wq = alloc_workqueue("smc_close_wq", 0, 0);
2888 if (!smc_close_wq)
2889 goto out_alloc_hs_wq;
2890
Ursula Braun6dabd402019-11-16 17:47:29 +01002891 rc = smc_core_init();
2892 if (rc) {
2893 pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
Guvenc Gulce194730a2021-06-16 16:52:58 +02002894 goto out_alloc_wqs;
Ursula Braun6dabd402019-11-16 17:47:29 +01002895 }
2896
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002897 rc = smc_llc_init();
2898 if (rc) {
2899 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002900 goto out_core;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002901 }
2902
Ursula Braun5f083182017-01-09 16:55:22 +01002903 rc = smc_cdc_init();
2904 if (rc) {
2905 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002906 goto out_core;
Ursula Braun5f083182017-01-09 16:55:22 +01002907 }
2908
Ursula Braunac713872017-01-09 16:55:13 +01002909 rc = proto_register(&smc_proto, 1);
2910 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01002911 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002912 goto out_core;
Ursula Braunac713872017-01-09 16:55:13 +01002913 }
2914
Karsten Graulaaa4d332018-03-16 15:06:41 +01002915 rc = proto_register(&smc_proto6, 1);
2916 if (rc) {
2917 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2918 goto out_proto;
2919 }
2920
Ursula Braunac713872017-01-09 16:55:13 +01002921 rc = sock_register(&smc_sock_family_ops);
2922 if (rc) {
2923 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002924 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01002925 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01002926 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002927 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01002928
Ursula Brauna4cf0442017-01-09 16:55:14 +01002929 rc = smc_ib_register_client();
2930 if (rc) {
2931 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2932 goto out_sock;
2933 }
2934
Tony Lud7cd4212021-12-28 21:44:36 +08002935 rc = tcp_register_ulp(&smc_ulp_ops);
2936 if (rc) {
2937 pr_err("%s: tcp_ulp_register fails with %d\n", __func__, rc);
2938 goto out_sock;
2939 }
2940
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002941 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002942 return 0;
2943
Ursula Brauna4cf0442017-01-09 16:55:14 +01002944out_sock:
2945 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002946out_proto6:
2947 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002948out_proto:
2949 proto_unregister(&smc_proto);
Ursula Braun6dabd402019-11-16 17:47:29 +01002950out_core:
2951 smc_core_exit();
Karsten Graul22ef4732020-09-10 18:48:29 +02002952out_alloc_wqs:
2953 destroy_workqueue(smc_close_wq);
2954out_alloc_hs_wq:
2955 destroy_workqueue(smc_hs_wq);
Thomas Richter6812baa2017-01-09 16:55:15 +01002956out_pnet:
2957 smc_pnet_exit();
Guvenc Gulcee8372d92020-12-01 20:20:43 +01002958out_nl:
2959 smc_nl_exit();
YueHaibing8c33bf12019-06-26 17:47:50 +02002960out_pernet_subsys:
2961 unregister_pernet_subsys(&smc_net_ops);
2962
Ursula Braunac713872017-01-09 16:55:13 +01002963 return rc;
2964}
2965
2966static void __exit smc_exit(void)
2967{
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002968 static_branch_disable(&tcp_have_smc);
Tony Lud7cd4212021-12-28 21:44:36 +08002969 tcp_unregister_ulp(&smc_ulp_ops);
Ursula Braunac713872017-01-09 16:55:13 +01002970 sock_unregister(PF_SMC);
Ursula Braun6dabd402019-11-16 17:47:29 +01002971 smc_core_exit();
2972 smc_ib_unregister_client();
Karsten Graul22ef4732020-09-10 18:48:29 +02002973 destroy_workqueue(smc_close_wq);
2974 destroy_workqueue(smc_hs_wq);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002975 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002976 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002977 smc_pnet_exit();
Guvenc Gulcee8372d92020-12-01 20:20:43 +01002978 smc_nl_exit();
Karsten Graulfa086662021-09-14 10:35:05 +02002979 smc_clc_exit();
Guvenc Gulce194730a2021-06-16 16:52:58 +02002980 unregister_pernet_subsys(&smc_net_stat_ops);
Hans Wippel64e28b52019-02-21 13:01:02 +01002981 unregister_pernet_subsys(&smc_net_ops);
Ursula Braun4ead9c92019-11-16 17:47:31 +01002982 rcu_barrier();
Ursula Braunac713872017-01-09 16:55:13 +01002983}
2984
2985module_init(smc_init);
2986module_exit(smc_exit);
2987
2988MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2989MODULE_DESCRIPTION("smc socket address family");
2990MODULE_LICENSE("GPL");
2991MODULE_ALIAS_NETPROTO(PF_SMC);
Tony Lud7cd4212021-12-28 21:44:36 +08002992MODULE_ALIAS_TCP_ULP("smc");