blob: 8c89d0b0ca185746e0f288525cb91a13734445bc [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
Wen Gu341adee2022-01-26 23:33:04 +0800569/* must be called under rcu read lock */
570static void smc_fback_wakeup_waitqueue(struct smc_sock *smc, void *key)
571{
572 struct socket_wq *wq;
573 __poll_t flags;
574
575 wq = rcu_dereference(smc->sk.sk_wq);
576 if (!skwq_has_sleeper(wq))
577 return;
578
579 /* wake up smc sk->sk_wq */
580 if (!key) {
581 /* sk_state_change */
582 wake_up_interruptible_all(&wq->wait);
583 } else {
584 flags = key_to_poll(key);
585 if (flags & (EPOLLIN | EPOLLOUT))
586 /* sk_data_ready or sk_write_space */
587 wake_up_interruptible_sync_poll(&wq->wait, flags);
588 else if (flags & EPOLLERR)
589 /* sk_error_report */
590 wake_up_interruptible_poll(&wq->wait, flags);
591 }
592}
593
594static int smc_fback_mark_woken(wait_queue_entry_t *wait,
595 unsigned int mode, int sync, void *key)
596{
597 struct smc_mark_woken *mark =
598 container_of(wait, struct smc_mark_woken, wait_entry);
599
600 mark->woken = true;
601 mark->key = key;
602 return 0;
603}
604
605static void smc_fback_forward_wakeup(struct smc_sock *smc, struct sock *clcsk,
606 void (*clcsock_callback)(struct sock *sk))
607{
608 struct smc_mark_woken mark = { .woken = false };
609 struct socket_wq *wq;
610
611 init_waitqueue_func_entry(&mark.wait_entry,
612 smc_fback_mark_woken);
613 rcu_read_lock();
614 wq = rcu_dereference(clcsk->sk_wq);
615 if (!wq)
616 goto out;
617 add_wait_queue(sk_sleep(clcsk), &mark.wait_entry);
618 clcsock_callback(clcsk);
619 remove_wait_queue(sk_sleep(clcsk), &mark.wait_entry);
620
621 if (mark.woken)
622 smc_fback_wakeup_waitqueue(smc, mark.key);
623out:
624 rcu_read_unlock();
625}
626
627static void smc_fback_state_change(struct sock *clcsk)
628{
629 struct smc_sock *smc =
630 smc_clcsock_user_data(clcsk);
631
632 if (!smc)
633 return;
634 smc_fback_forward_wakeup(smc, clcsk, smc->clcsk_state_change);
635}
636
637static void smc_fback_data_ready(struct sock *clcsk)
638{
639 struct smc_sock *smc =
640 smc_clcsock_user_data(clcsk);
641
642 if (!smc)
643 return;
644 smc_fback_forward_wakeup(smc, clcsk, smc->clcsk_data_ready);
645}
646
647static void smc_fback_write_space(struct sock *clcsk)
648{
649 struct smc_sock *smc =
650 smc_clcsock_user_data(clcsk);
651
652 if (!smc)
653 return;
654 smc_fback_forward_wakeup(smc, clcsk, smc->clcsk_write_space);
655}
656
657static void smc_fback_error_report(struct sock *clcsk)
658{
659 struct smc_sock *smc =
660 smc_clcsock_user_data(clcsk);
661
662 if (!smc)
663 return;
664 smc_fback_forward_wakeup(smc, clcsk, smc->clcsk_error_report);
665}
666
Wen Guc0bf3d82022-01-22 17:43:09 +0800667static int smc_switch_to_fallback(struct smc_sock *smc, int reason_code)
Ursula Braun07603b22019-04-11 11:17:32 +0200668{
Wen Gu341adee2022-01-26 23:33:04 +0800669 struct sock *clcsk;
Wen Gu2153bd12021-11-13 15:33:35 +0800670
Wen Guc0bf3d82022-01-22 17:43:09 +0800671 mutex_lock(&smc->clcsock_release_lock);
672 if (!smc->clcsock) {
673 mutex_unlock(&smc->clcsock_release_lock);
674 return -EBADF;
675 }
Wen Gu341adee2022-01-26 23:33:04 +0800676 clcsk = smc->clcsock->sk;
677
Ursula Braun07603b22019-04-11 11:17:32 +0200678 smc->use_fallback = true;
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +0200679 smc->fallback_rsn = reason_code;
680 smc_stat_fallback(smc);
Tony Lu48262602021-11-01 15:39:12 +0800681 trace_smc_switch_to_fallback(smc, reason_code);
Ursula Braun07603b22019-04-11 11:17:32 +0200682 if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
683 smc->clcsock->file = smc->sk.sk_socket->file;
684 smc->clcsock->file->private_data = smc->clcsock;
Ursula Braun67f562e2020-02-14 08:58:59 +0100685 smc->clcsock->wq.fasync_list =
686 smc->sk.sk_socket->wq.fasync_list;
Wen Gu2153bd12021-11-13 15:33:35 +0800687
Wen Gu341adee2022-01-26 23:33:04 +0800688 /* There might be some wait entries remaining
689 * in smc sk->sk_wq and they should be woken up
690 * as clcsock's wait queue is woken up.
Wen Gu2153bd12021-11-13 15:33:35 +0800691 */
Wen Gu341adee2022-01-26 23:33:04 +0800692 smc->clcsk_state_change = clcsk->sk_state_change;
693 smc->clcsk_data_ready = clcsk->sk_data_ready;
694 smc->clcsk_write_space = clcsk->sk_write_space;
695 smc->clcsk_error_report = clcsk->sk_error_report;
696
697 clcsk->sk_state_change = smc_fback_state_change;
698 clcsk->sk_data_ready = smc_fback_data_ready;
699 clcsk->sk_write_space = smc_fback_write_space;
700 clcsk->sk_error_report = smc_fback_error_report;
701
702 smc->clcsock->sk->sk_user_data =
703 (void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY);
Ursula Braun07603b22019-04-11 11:17:32 +0200704 }
Wen Guc0bf3d82022-01-22 17:43:09 +0800705 mutex_unlock(&smc->clcsock_release_lock);
706 return 0;
Ursula Braun07603b22019-04-11 11:17:32 +0200707}
708
Hans Wippel3b2dec22018-05-18 09:34:18 +0200709/* fall back during connect */
Karsten Graul603cc142018-07-25 16:35:32 +0200710static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200711{
Wen Guc0bf3d82022-01-22 17:43:09 +0800712 struct net *net = sock_net(&smc->sk);
713 int rc = 0;
714
715 rc = smc_switch_to_fallback(smc, reason_code);
716 if (rc) { /* fallback fails */
717 this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
718 if (smc->sk.sk_state == SMC_INIT)
719 sock_put(&smc->sk); /* passive closing */
720 return rc;
721 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200722 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200723 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200724 if (smc->sk.sk_state == SMC_INIT)
725 smc->sk.sk_state = SMC_ACTIVE;
726 return 0;
727}
728
729/* decline and fall back during connect */
Ursula Braune8d726c82020-09-26 12:44:32 +0200730static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code,
731 u8 version)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200732{
Guvenc Gulce194730a2021-06-16 16:52:58 +0200733 struct net *net = sock_net(&smc->sk);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200734 int rc;
735
Ursula Braune1bbdd52018-07-05 16:15:30 +0200736 if (reason_code < 0) { /* error, fallback is not possible */
Guvenc Gulce194730a2021-06-16 16:52:58 +0200737 this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200738 if (smc->sk.sk_state == SMC_INIT)
739 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200740 return reason_code;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200741 }
Karsten Graul603cc142018-07-25 16:35:32 +0200742 if (reason_code != SMC_CLC_DECL_PEERDECL) {
Ursula Braune8d726c82020-09-26 12:44:32 +0200743 rc = smc_clc_send_decline(smc, reason_code, version);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200744 if (rc < 0) {
Guvenc Gulce194730a2021-06-16 16:52:58 +0200745 this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200746 if (smc->sk.sk_state == SMC_INIT)
747 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200748 return rc;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200749 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200750 }
Karsten Graul603cc142018-07-25 16:35:32 +0200751 return smc_connect_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200752}
753
Karsten Graul8cf3f3e2020-12-01 20:20:36 +0100754static void smc_conn_abort(struct smc_sock *smc, int local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200755{
Wen Gu36595d82022-01-06 20:42:08 +0800756 struct smc_connection *conn = &smc->conn;
757 struct smc_link_group *lgr = conn->lgr;
Wen Guea89c6c2022-01-13 16:36:41 +0800758 bool lgr_valid = false;
759
760 if (smc_conn_lgr_valid(conn))
761 lgr_valid = true;
Wen Gu36595d82022-01-06 20:42:08 +0800762
763 smc_conn_free(conn);
Wen Guea89c6c2022-01-13 16:36:41 +0800764 if (local_first && lgr_valid)
Wen Gu36595d82022-01-06 20:42:08 +0800765 smc_lgr_cleanup_early(lgr);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200766}
767
768/* check if there is a rdma device available for this connection. */
769/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200770static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200771{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200772 /* PNET table look up: search active ib_device and port
773 * within same PNETID that also contains the ethernet device
774 * used for the internal TCP socket
775 */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200776 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
Karsten Graul42042db2021-10-16 11:37:44 +0200777 if (!ini->check_smcrv2 && !ini->ib_dev)
778 return SMC_CLC_DECL_NOSMCRDEV;
779 if (ini->check_smcrv2 && !ini->smcrv2.ib_dev_v2)
Karsten Graul9aa68d22019-04-12 12:57:29 +0200780 return SMC_CLC_DECL_NOSMCRDEV;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200781 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200782}
783
Hans Wippel41349842018-06-28 19:05:11 +0200784/* check if there is an ISM device available for this connection. */
785/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200786static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200787{
788 /* Find ISM device with same PNETID as connecting interface */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200789 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
Ursula Braun3fc64932020-09-26 12:44:23 +0200790 if (!ini->ism_dev[0])
Karsten Graul9aa68d22019-04-12 12:57:29 +0200791 return SMC_CLC_DECL_NOSMCDDEV;
Ursula Braun8caaccf2020-09-26 12:44:25 +0200792 else
793 ini->ism_chid[0] = smc_ism_get_chid(ini->ism_dev[0]);
Hans Wippel41349842018-06-28 19:05:11 +0200794 return 0;
795}
796
Karsten Graul839d6962020-10-02 17:09:26 +0200797/* is chid unique for the ism devices that are already determined? */
798static bool smc_find_ism_v2_is_unique_chid(u16 chid, struct smc_init_info *ini,
799 int cnt)
800{
801 int i = (!ini->ism_dev[0]) ? 1 : 0;
802
803 for (; i < cnt; i++)
804 if (ini->ism_chid[i] == chid)
805 return false;
806 return true;
807}
808
Ursula Braund70bf4f2020-09-26 12:44:27 +0200809/* determine possible V2 ISM devices (either without PNETID or with PNETID plus
810 * PNETID matching net_device)
811 */
812static int smc_find_ism_v2_device_clnt(struct smc_sock *smc,
813 struct smc_init_info *ini)
814{
815 int rc = SMC_CLC_DECL_NOSMCDDEV;
816 struct smcd_dev *smcd;
817 int i = 1;
Karsten Graul839d6962020-10-02 17:09:26 +0200818 u16 chid;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200819
820 if (smcd_indicated(ini->smc_type_v1))
821 rc = 0; /* already initialized for V1 */
822 mutex_lock(&smcd_dev_list.mutex);
823 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
824 if (smcd->going_away || smcd == ini->ism_dev[0])
825 continue;
Karsten Graul839d6962020-10-02 17:09:26 +0200826 chid = smc_ism_get_chid(smcd);
827 if (!smc_find_ism_v2_is_unique_chid(chid, ini, i))
828 continue;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200829 if (!smc_pnet_is_pnetid_set(smcd->pnetid) ||
830 smc_pnet_is_ndev_pnetid(sock_net(&smc->sk), smcd->pnetid)) {
831 ini->ism_dev[i] = smcd;
Karsten Graul839d6962020-10-02 17:09:26 +0200832 ini->ism_chid[i] = chid;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200833 ini->is_smcd = true;
834 rc = 0;
835 i++;
836 if (i > SMC_MAX_ISM_DEVS)
837 break;
838 }
839 }
840 mutex_unlock(&smcd_dev_list.mutex);
841 ini->ism_offered_cnt = i - 1;
842 if (!ini->ism_dev[0] && !ini->ism_dev[1])
843 ini->smcd_version = 0;
844
845 return rc;
846}
847
Hans Wippel41349842018-06-28 19:05:11 +0200848/* Check for VLAN ID and register it on ISM device just for CLC handshake */
849static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200850 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200851{
Ursula Braun3fc64932020-09-26 12:44:23 +0200852 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev[0], ini->vlan_id))
Karsten Graul7a627252019-04-12 12:57:30 +0200853 return SMC_CLC_DECL_ISMVLANERR;
Hans Wippel41349842018-06-28 19:05:11 +0200854 return 0;
855}
856
Ursula Braund70bf4f2020-09-26 12:44:27 +0200857static int smc_find_proposal_devices(struct smc_sock *smc,
858 struct smc_init_info *ini)
859{
860 int rc = 0;
861
862 /* check if there is an ism device available */
Karsten Graul42042db2021-10-16 11:37:44 +0200863 if (!(ini->smcd_version & SMC_V1) ||
864 smc_find_ism_device(smc, ini) ||
865 smc_connect_ism_vlan_setup(smc, ini))
866 ini->smcd_version &= ~SMC_V1;
867 /* else ISM V1 is supported for this connection */
868
869 /* check if there is an rdma device available */
870 if (!(ini->smcr_version & SMC_V1) ||
871 smc_find_rdma_device(smc, ini))
872 ini->smcr_version &= ~SMC_V1;
873 /* else RDMA is supported for this connection */
874
875 ini->smc_type_v1 = smc_indicated_type(ini->smcd_version & SMC_V1,
876 ini->smcr_version & SMC_V1);
877
878 /* check if there is an ism v2 device available */
879 if (!(ini->smcd_version & SMC_V2) ||
880 !smc_ism_is_v2_capable() ||
881 smc_find_ism_v2_device_clnt(smc, ini))
882 ini->smcd_version &= ~SMC_V2;
883
884 /* check if there is an rdma v2 device available */
885 ini->check_smcrv2 = true;
886 ini->smcrv2.saddr = smc->clcsock->sk->sk_rcv_saddr;
887 if (!(ini->smcr_version & SMC_V2) ||
888 smc->clcsock->sk->sk_family != AF_INET ||
889 !smc_clc_ueid_count() ||
890 smc_find_rdma_device(smc, ini))
891 ini->smcr_version &= ~SMC_V2;
892 ini->check_smcrv2 = false;
893
894 ini->smc_type_v2 = smc_indicated_type(ini->smcd_version & SMC_V2,
895 ini->smcr_version & SMC_V2);
Ursula Braund70bf4f2020-09-26 12:44:27 +0200896
897 /* if neither ISM nor RDMA are supported, fallback */
Karsten Graul42042db2021-10-16 11:37:44 +0200898 if (ini->smc_type_v1 == SMC_TYPE_N && ini->smc_type_v2 == SMC_TYPE_N)
Ursula Braund70bf4f2020-09-26 12:44:27 +0200899 rc = SMC_CLC_DECL_NOSMCDEV;
900
901 return rc;
902}
903
Hans Wippel41349842018-06-28 19:05:11 +0200904/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
905 * used, the VLAN ID will be registered again during the connection setup.
906 */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200907static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200908 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200909{
Ursula Braund70bf4f2020-09-26 12:44:27 +0200910 if (!smcd_indicated(ini->smc_type_v1))
Hans Wippel41349842018-06-28 19:05:11 +0200911 return 0;
Ursula Braun3fc64932020-09-26 12:44:23 +0200912 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev[0], ini->vlan_id))
Hans Wippel41349842018-06-28 19:05:11 +0200913 return SMC_CLC_DECL_CNFERR;
914 return 0;
915}
916
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200917#define SMC_CLC_MAX_ACCEPT_LEN \
918 (sizeof(struct smc_clc_msg_accept_confirm_v2) + \
Ursula Braunb81a5eb2020-09-26 12:44:31 +0200919 sizeof(struct smc_clc_first_contact_ext) + \
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200920 sizeof(struct smc_clc_msg_trail))
921
Hans Wippel3b2dec22018-05-18 09:34:18 +0200922/* CLC handshake during connect */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200923static int smc_connect_clc(struct smc_sock *smc,
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200924 struct smc_clc_msg_accept_confirm_v2 *aclc2,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200925 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200926{
927 int rc = 0;
928
929 /* do inband token exchange */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200930 rc = smc_clc_send_proposal(smc, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200931 if (rc)
932 return rc;
933 /* receive SMC Accept CLC message */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200934 return smc_clc_wait_msg(smc, aclc2, SMC_CLC_MAX_ACCEPT_LEN,
935 SMC_CLC_ACCEPT, CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200936}
937
Karsten Graulb4ba4652021-10-16 11:37:50 +0200938void smc_fill_gid_list(struct smc_link_group *lgr,
939 struct smc_gidlist *gidlist,
940 struct smc_ib_device *known_dev, u8 *known_gid)
Karsten Graule5c47442021-10-16 11:37:45 +0200941{
942 struct smc_init_info *alt_ini = NULL;
943
944 memset(gidlist, 0, sizeof(*gidlist));
945 memcpy(gidlist->list[gidlist->len++], known_gid, SMC_GID_SIZE);
946
947 alt_ini = kzalloc(sizeof(*alt_ini), GFP_KERNEL);
948 if (!alt_ini)
949 goto out;
950
951 alt_ini->vlan_id = lgr->vlan_id;
952 alt_ini->check_smcrv2 = true;
953 alt_ini->smcrv2.saddr = lgr->saddr;
954 smc_pnet_find_alt_roce(lgr, alt_ini, known_dev);
955
956 if (!alt_ini->smcrv2.ib_dev_v2)
957 goto out;
958
959 memcpy(gidlist->list[gidlist->len++], alt_ini->smcrv2.ib_gid_v2,
960 SMC_GID_SIZE);
961
962out:
963 kfree(alt_ini);
964}
965
966static int smc_connect_rdma_v2_prepare(struct smc_sock *smc,
967 struct smc_clc_msg_accept_confirm *aclc,
968 struct smc_init_info *ini)
969{
970 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
971 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
972 struct smc_clc_first_contact_ext *fce =
973 (struct smc_clc_first_contact_ext *)
974 (((u8 *)clc_v2) + sizeof(*clc_v2));
975
976 if (!ini->first_contact_peer || aclc->hdr.version == SMC_V1)
977 return 0;
978
979 if (fce->v2_direct) {
980 memcpy(ini->smcrv2.nexthop_mac, &aclc->r0.lcl.mac, ETH_ALEN);
981 ini->smcrv2.uses_gateway = false;
982 } else {
983 if (smc_ib_find_route(smc->clcsock->sk->sk_rcv_saddr,
984 smc_ib_gid_to_ipv4(aclc->r0.lcl.gid),
985 ini->smcrv2.nexthop_mac,
986 &ini->smcrv2.uses_gateway))
987 return SMC_CLC_DECL_NOROUTE;
988 if (!ini->smcrv2.uses_gateway) {
989 /* mismatch: peer claims indirect, but its direct */
990 return SMC_CLC_DECL_NOINDIRECT;
991 }
992 }
993 return 0;
994}
995
Ursula Brauna046d572017-01-09 16:55:16 +0100996/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200997static int smc_connect_rdma(struct smc_sock *smc,
998 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200999 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001000{
Karsten Graul0fb0b022020-04-30 15:55:43 +02001001 int i, reason_code = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001002 struct smc_link *link;
Karsten Graule5c47442021-10-16 11:37:45 +02001003 u8 *eid = NULL;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001004
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001005 ini->is_smcd = false;
Ursula Braun3d9725a2020-09-10 18:48:23 +02001006 ini->ib_clcqpn = ntoh24(aclc->r0.qpn);
Ursula Braunf1eb02f2020-09-26 12:44:20 +02001007 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
Karsten Graule5c47442021-10-16 11:37:45 +02001008 memcpy(ini->peer_systemid, aclc->r0.lcl.id_for_peer, SMC_SYSTEMID_LEN);
1009 memcpy(ini->peer_gid, aclc->r0.lcl.gid, SMC_GID_SIZE);
1010 memcpy(ini->peer_mac, aclc->r0.lcl.mac, ETH_ALEN);
1011
1012 reason_code = smc_connect_rdma_v2_prepare(smc, aclc, ini);
1013 if (reason_code)
1014 return reason_code;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001015
Hans Wippel72a36a82019-02-07 15:56:18 +01001016 mutex_lock(&smc_client_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +02001017 reason_code = smc_conn_create(smc, ini);
1018 if (reason_code) {
Hans Wippel72a36a82019-02-07 15:56:18 +01001019 mutex_unlock(&smc_client_lgr_pending);
1020 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001021 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001022
1023 smc_conn_save_peer_info(smc, aclc);
1024
Ursula Braun5ac54d82020-09-10 18:48:21 +02001025 if (ini->first_contact_local) {
Karsten Graul0fb0b022020-04-30 15:55:43 +02001026 link = smc->conn.lnk;
1027 } else {
1028 /* set link that was assigned by server */
1029 link = NULL;
1030 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
1031 struct smc_link *l = &smc->conn.lgr->lnk[i];
1032
Ursula Braun3d9725a2020-09-10 18:48:23 +02001033 if (l->peer_qpn == ntoh24(aclc->r0.qpn) &&
1034 !memcmp(l->peer_gid, &aclc->r0.lcl.gid,
1035 SMC_GID_SIZE) &&
Karsten Graule5c47442021-10-16 11:37:45 +02001036 (aclc->hdr.version > SMC_V1 ||
1037 !memcmp(l->peer_mac, &aclc->r0.lcl.mac,
1038 sizeof(l->peer_mac)))) {
Karsten Graul0fb0b022020-04-30 15:55:43 +02001039 link = l;
1040 break;
1041 }
1042 }
Karsten Graulc60a2ce2020-10-07 22:57:41 +02001043 if (!link) {
1044 reason_code = SMC_CLC_DECL_NOSRVLINK;
1045 goto connect_abort;
1046 }
Guvenc Gulce64513d22021-08-09 11:05:57 +02001047 smc_switch_link_and_count(&smc->conn, link);
Karsten Graul0fb0b022020-04-30 15:55:43 +02001048 }
1049
Hans Wippel3b2dec22018-05-18 09:34:18 +02001050 /* create send buffer and rmb */
Karsten Graulc60a2ce2020-10-07 22:57:41 +02001051 if (smc_buf_create(smc, false)) {
1052 reason_code = SMC_CLC_DECL_MEM;
1053 goto connect_abort;
1054 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001055
Ursula Braun5ac54d82020-09-10 18:48:21 +02001056 if (ini->first_contact_local)
Karsten Graule5c47442021-10-16 11:37:45 +02001057 smc_link_save_peer_info(link, aclc, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001058
Karsten Graulc60a2ce2020-10-07 22:57:41 +02001059 if (smc_rmb_rtoken_handling(&smc->conn, link, aclc)) {
1060 reason_code = SMC_CLC_DECL_ERR_RTOK;
1061 goto connect_abort;
1062 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001063
1064 smc_close_init(smc);
1065 smc_rx_init(smc);
1066
Ursula Braun5ac54d82020-09-10 18:48:21 +02001067 if (ini->first_contact_local) {
Karsten Graulc60a2ce2020-10-07 22:57:41 +02001068 if (smc_ib_ready_link(link)) {
1069 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
1070 goto connect_abort;
1071 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001072 } else {
Karsten Graulc60a2ce2020-10-07 22:57:41 +02001073 if (smcr_lgr_reg_rmbs(link, smc->conn.rmb_desc)) {
1074 reason_code = SMC_CLC_DECL_ERR_REGRMB;
1075 goto connect_abort;
1076 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001077 }
1078 smc_rmb_sync_sg_for_device(&smc->conn);
1079
Karsten Graule5c47442021-10-16 11:37:45 +02001080 if (aclc->hdr.version > SMC_V1) {
1081 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
1082 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
1083
1084 eid = clc_v2->r1.eid;
1085 if (ini->first_contact_local)
1086 smc_fill_gid_list(link->lgr, &ini->smcrv2.gidlist,
1087 link->smcibdev, link->gid);
1088 }
1089
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001090 reason_code = smc_clc_send_confirm(smc, ini->first_contact_local,
Karsten Graule5c47442021-10-16 11:37:45 +02001091 aclc->hdr.version, eid, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001092 if (reason_code)
Karsten Graulc60a2ce2020-10-07 22:57:41 +02001093 goto connect_abort;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001094
1095 smc_tx_init(smc);
1096
Ursula Braun5ac54d82020-09-10 18:48:21 +02001097 if (ini->first_contact_local) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001098 /* QP confirmation over RoCE fabric */
Karsten Graul0fb0b022020-04-30 15:55:43 +02001099 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
Karsten Graulb9247542020-04-29 17:10:41 +02001100 reason_code = smcr_clnt_conf_first_link(smc);
Karsten Graul0fb0b022020-04-30 15:55:43 +02001101 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001102 if (reason_code)
Karsten Graulc60a2ce2020-10-07 22:57:41 +02001103 goto connect_abort;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001104 }
Hans Wippel72a36a82019-02-07 15:56:18 +01001105 mutex_unlock(&smc_client_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001106
1107 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +02001108 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001109 if (smc->sk.sk_state == SMC_INIT)
1110 smc->sk.sk_state = SMC_ACTIVE;
1111
1112 return 0;
Karsten Graulc60a2ce2020-10-07 22:57:41 +02001113connect_abort:
Karsten Graul8cf3f3e2020-12-01 20:20:36 +01001114 smc_conn_abort(smc, ini->first_contact_local);
Karsten Graulc60a2ce2020-10-07 22:57:41 +02001115 mutex_unlock(&smc_client_lgr_pending);
1116 smc->connect_nonblock = 0;
1117
1118 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001119}
1120
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001121/* The server has chosen one of the proposed ISM devices for the communication.
1122 * Determine from the CHID of the received CLC ACCEPT the ISM device chosen.
1123 */
1124static int
1125smc_v2_determine_accepted_chid(struct smc_clc_msg_accept_confirm_v2 *aclc,
1126 struct smc_init_info *ini)
1127{
1128 int i;
1129
1130 for (i = 0; i < ini->ism_offered_cnt + 1; i++) {
Karsten Graule5c47442021-10-16 11:37:45 +02001131 if (ini->ism_chid[i] == ntohs(aclc->d1.chid)) {
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001132 ini->ism_selected = i;
1133 return 0;
1134 }
1135 }
1136
1137 return -EPROTO;
1138}
1139
Hans Wippel41349842018-06-28 19:05:11 +02001140/* setup for ISM connection of client */
1141static int smc_connect_ism(struct smc_sock *smc,
1142 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001143 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +02001144{
Karsten Graulfa086662021-09-14 10:35:05 +02001145 u8 *eid = NULL;
Hans Wippel41349842018-06-28 19:05:11 +02001146 int rc = 0;
1147
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001148 ini->is_smcd = true;
Ursula Braunf1eb02f2020-09-26 12:44:20 +02001149 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001150
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001151 if (aclc->hdr.version == SMC_V2) {
1152 struct smc_clc_msg_accept_confirm_v2 *aclc_v2 =
1153 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
1154
1155 rc = smc_v2_determine_accepted_chid(aclc_v2, ini);
1156 if (rc)
1157 return rc;
1158 }
1159 ini->ism_peer_gid[ini->ism_selected] = aclc->d0.gid;
1160
Hans Wippel72a36a82019-02-07 15:56:18 +01001161 /* there is only one lgr role for SMC-D; use server lock */
1162 mutex_lock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +02001163 rc = smc_conn_create(smc, ini);
1164 if (rc) {
Hans Wippel72a36a82019-02-07 15:56:18 +01001165 mutex_unlock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +02001166 return rc;
Hans Wippel72a36a82019-02-07 15:56:18 +01001167 }
Hans Wippel41349842018-06-28 19:05:11 +02001168
1169 /* Create send and receive buffers */
Karsten Graul72b7f6c2020-07-26 20:34:28 +02001170 rc = smc_buf_create(smc, true);
Karsten Graulc60a2ce2020-10-07 22:57:41 +02001171 if (rc) {
1172 rc = (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB : SMC_CLC_DECL_MEM;
1173 goto connect_abort;
1174 }
Hans Wippel41349842018-06-28 19:05:11 +02001175
1176 smc_conn_save_peer_info(smc, aclc);
1177 smc_close_init(smc);
1178 smc_rx_init(smc);
1179 smc_tx_init(smc);
1180
Karsten Graulfa086662021-09-14 10:35:05 +02001181 if (aclc->hdr.version > SMC_V1) {
1182 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
1183 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
1184
Karsten Graule5c47442021-10-16 11:37:45 +02001185 eid = clc_v2->d1.eid;
Karsten Graulfa086662021-09-14 10:35:05 +02001186 }
1187
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001188 rc = smc_clc_send_confirm(smc, ini->first_contact_local,
Karsten Graule5c47442021-10-16 11:37:45 +02001189 aclc->hdr.version, eid, NULL);
Hans Wippel41349842018-06-28 19:05:11 +02001190 if (rc)
Karsten Graulc60a2ce2020-10-07 22:57:41 +02001191 goto connect_abort;
Hans Wippel72a36a82019-02-07 15:56:18 +01001192 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel41349842018-06-28 19:05:11 +02001193
1194 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +02001195 smc->connect_nonblock = 0;
Hans Wippel41349842018-06-28 19:05:11 +02001196 if (smc->sk.sk_state == SMC_INIT)
1197 smc->sk.sk_state = SMC_ACTIVE;
1198
1199 return 0;
Karsten Graulc60a2ce2020-10-07 22:57:41 +02001200connect_abort:
Karsten Graul8cf3f3e2020-12-01 20:20:36 +01001201 smc_conn_abort(smc, ini->first_contact_local);
Karsten Graulc60a2ce2020-10-07 22:57:41 +02001202 mutex_unlock(&smc_server_lgr_pending);
1203 smc->connect_nonblock = 0;
1204
1205 return rc;
Hans Wippel41349842018-06-28 19:05:11 +02001206}
1207
Ursula Braund70bf4f2020-09-26 12:44:27 +02001208/* check if received accept type and version matches a proposed one */
1209static int smc_connect_check_aclc(struct smc_init_info *ini,
1210 struct smc_clc_msg_accept_confirm *aclc)
1211{
Karsten Graul42042db2021-10-16 11:37:44 +02001212 if (aclc->hdr.typev1 != SMC_TYPE_R &&
1213 aclc->hdr.typev1 != SMC_TYPE_D)
Ursula Braund70bf4f2020-09-26 12:44:27 +02001214 return SMC_CLC_DECL_MODEUNSUPP;
1215
Karsten Graul42042db2021-10-16 11:37:44 +02001216 if (aclc->hdr.version >= SMC_V2) {
1217 if ((aclc->hdr.typev1 == SMC_TYPE_R &&
1218 !smcr_indicated(ini->smc_type_v2)) ||
1219 (aclc->hdr.typev1 == SMC_TYPE_D &&
1220 !smcd_indicated(ini->smc_type_v2)))
1221 return SMC_CLC_DECL_MODEUNSUPP;
1222 } else {
1223 if ((aclc->hdr.typev1 == SMC_TYPE_R &&
1224 !smcr_indicated(ini->smc_type_v1)) ||
1225 (aclc->hdr.typev1 == SMC_TYPE_D &&
1226 !smcd_indicated(ini->smc_type_v1)))
1227 return SMC_CLC_DECL_MODEUNSUPP;
1228 }
1229
Ursula Braund70bf4f2020-09-26 12:44:27 +02001230 return 0;
1231}
1232
Hans Wippel3b2dec22018-05-18 09:34:18 +02001233/* perform steps before actually connecting */
1234static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001235{
Guvenc Gulce49407ae2020-12-01 20:20:42 +01001236 u8 version = smc_ism_is_v2_capable() ? SMC_V2 : SMC_V1;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001237 struct smc_clc_msg_accept_confirm_v2 *aclc2;
1238 struct smc_clc_msg_accept_confirm *aclc;
Ursula Braun3fc64932020-09-26 12:44:23 +02001239 struct smc_init_info *ini = NULL;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001240 u8 *buf = NULL;
Ursula Brauna046d572017-01-09 16:55:16 +01001241 int rc = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001242
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001243 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +02001244 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001245
Hans Wippel3b2dec22018-05-18 09:34:18 +02001246 /* if peer has not signalled SMC-capability, fall back */
1247 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +02001248 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001249
Ursula Braune8d726c82020-09-26 12:44:32 +02001250 /* IPSec connections opt out of SMC optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001251 if (using_ipsec(smc))
Ursula Braune8d726c82020-09-26 12:44:32 +02001252 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC,
1253 version);
Ursula Brauna046d572017-01-09 16:55:16 +01001254
Ursula Braun3fc64932020-09-26 12:44:23 +02001255 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
1256 if (!ini)
Ursula Braune8d726c82020-09-26 12:44:32 +02001257 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_MEM,
1258 version);
Ursula Braun3fc64932020-09-26 12:44:23 +02001259
Karsten Graul42042db2021-10-16 11:37:44 +02001260 ini->smcd_version = SMC_V1 | SMC_V2;
1261 ini->smcr_version = SMC_V1 | SMC_V2;
Ursula Braund70bf4f2020-09-26 12:44:27 +02001262 ini->smc_type_v1 = SMC_TYPE_B;
Karsten Graul42042db2021-10-16 11:37:44 +02001263 ini->smc_type_v2 = SMC_TYPE_B;
Ursula Braund70bf4f2020-09-26 12:44:27 +02001264
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001265 /* get vlan id from IP device */
Ursula Braun3fc64932020-09-26 12:44:23 +02001266 if (smc_vlan_by_tcpsk(smc->clcsock, ini)) {
Ursula Braund70bf4f2020-09-26 12:44:27 +02001267 ini->smcd_version &= ~SMC_V1;
Karsten Graul42042db2021-10-16 11:37:44 +02001268 ini->smcr_version = 0;
Ursula Braund70bf4f2020-09-26 12:44:27 +02001269 ini->smc_type_v1 = SMC_TYPE_N;
1270 if (!ini->smcd_version) {
1271 rc = SMC_CLC_DECL_GETVLANERR;
1272 goto fallback;
1273 }
Ursula Braun3fc64932020-09-26 12:44:23 +02001274 }
Hans Wippel41349842018-06-28 19:05:11 +02001275
Ursula Braund70bf4f2020-09-26 12:44:27 +02001276 rc = smc_find_proposal_devices(smc, ini);
1277 if (rc)
1278 goto fallback;
Ursula Brauna046d572017-01-09 16:55:16 +01001279
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001280 buf = kzalloc(SMC_CLC_MAX_ACCEPT_LEN, GFP_KERNEL);
1281 if (!buf) {
1282 rc = SMC_CLC_DECL_MEM;
1283 goto fallback;
1284 }
1285 aclc2 = (struct smc_clc_msg_accept_confirm_v2 *)buf;
1286 aclc = (struct smc_clc_msg_accept_confirm *)aclc2;
1287
Hans Wippel3b2dec22018-05-18 09:34:18 +02001288 /* perform CLC handshake */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001289 rc = smc_connect_clc(smc, aclc2, ini);
Ursula Braund70bf4f2020-09-26 12:44:27 +02001290 if (rc)
1291 goto vlan_cleanup;
1292
1293 /* check if smc modes and versions of CLC proposal and accept match */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001294 rc = smc_connect_check_aclc(ini, aclc);
Karsten Graul0530bd62020-11-18 22:40:37 +01001295 version = aclc->hdr.version == SMC_V1 ? SMC_V1 : SMC_V2;
Ursula Braund70bf4f2020-09-26 12:44:27 +02001296 if (rc)
1297 goto vlan_cleanup;
Ursula Brauna046d572017-01-09 16:55:16 +01001298
Hans Wippel41349842018-06-28 19:05:11 +02001299 /* depending on previous steps, connect using rdma or ism */
Karsten Graul42042db2021-10-16 11:37:44 +02001300 if (aclc->hdr.typev1 == SMC_TYPE_R) {
1301 ini->smcr_version = version;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001302 rc = smc_connect_rdma(smc, aclc, ini);
Karsten Graul42042db2021-10-16 11:37:44 +02001303 } else if (aclc->hdr.typev1 == SMC_TYPE_D) {
1304 ini->smcd_version = version;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001305 rc = smc_connect_ism(smc, aclc, ini);
Karsten Graul42042db2021-10-16 11:37:44 +02001306 }
Ursula Braund70bf4f2020-09-26 12:44:27 +02001307 if (rc)
1308 goto vlan_cleanup;
Ursula Brauna046d572017-01-09 16:55:16 +01001309
Guvenc Gulce194730a2021-06-16 16:52:58 +02001310 SMC_STAT_CLNT_SUCC_INC(sock_net(smc->clcsock->sk), aclc);
Ursula Braund70bf4f2020-09-26 12:44:27 +02001311 smc_connect_ism_vlan_cleanup(smc, ini);
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001312 kfree(buf);
Ursula Braun3fc64932020-09-26 12:44:23 +02001313 kfree(ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001314 return 0;
Ursula Braund70bf4f2020-09-26 12:44:27 +02001315
1316vlan_cleanup:
1317 smc_connect_ism_vlan_cleanup(smc, ini);
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001318 kfree(buf);
Ursula Braund70bf4f2020-09-26 12:44:27 +02001319fallback:
1320 kfree(ini);
Ursula Braune8d726c82020-09-26 12:44:32 +02001321 return smc_connect_decline_fallback(smc, rc, version);
Ursula Brauna046d572017-01-09 16:55:16 +01001322}
1323
Ursula Braun24ac3a02018-06-27 17:59:50 +02001324static void smc_connect_work(struct work_struct *work)
1325{
1326 struct smc_sock *smc = container_of(work, struct smc_sock,
1327 connect_work);
Ursula Braun50717a32019-04-12 12:57:23 +02001328 long timeo = smc->sk.sk_sndtimeo;
1329 int rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001330
Ursula Braun50717a32019-04-12 12:57:23 +02001331 if (!timeo)
1332 timeo = MAX_SCHEDULE_TIMEOUT;
1333 lock_sock(smc->clcsock->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +02001334 if (smc->clcsock->sk->sk_err) {
1335 smc->sk.sk_err = smc->clcsock->sk->sk_err;
Ursula Braun50717a32019-04-12 12:57:23 +02001336 } else if ((1 << smc->clcsock->sk->sk_state) &
Wen Guf3a3a0f2021-10-28 15:13:47 +08001337 (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
Ursula Braun50717a32019-04-12 12:57:23 +02001338 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
1339 if ((rc == -EPIPE) &&
1340 ((1 << smc->clcsock->sk->sk_state) &
1341 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
1342 rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001343 }
Ursula Braun50717a32019-04-12 12:57:23 +02001344 release_sock(smc->clcsock->sk);
1345 lock_sock(&smc->sk);
1346 if (rc != 0 || smc->sk.sk_err) {
1347 smc->sk.sk_state = SMC_CLOSED;
1348 if (rc == -EPIPE || rc == -EAGAIN)
1349 smc->sk.sk_err = EPIPE;
1350 else if (signal_pending(current))
1351 smc->sk.sk_err = -sock_intr_errno(timeo);
Ursula Braun6d6dd522019-11-12 16:03:41 +01001352 sock_put(&smc->sk); /* passive closing */
Ursula Braun24ac3a02018-06-27 17:59:50 +02001353 goto out;
1354 }
1355
1356 rc = __smc_connect(smc);
1357 if (rc < 0)
1358 smc->sk.sk_err = -rc;
1359
1360out:
Ursula Braun07603b22019-04-11 11:17:32 +02001361 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
1362 if (smc->sk.sk_err) {
1363 smc->sk.sk_state_change(&smc->sk);
1364 } else { /* allow polling before and after fallback decision */
1365 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
1366 smc->sk.sk_write_space(&smc->sk);
1367 }
1368 }
Ursula Braun24ac3a02018-06-27 17:59:50 +02001369 release_sock(&smc->sk);
1370}
1371
Ursula Braunac713872017-01-09 16:55:13 +01001372static int smc_connect(struct socket *sock, struct sockaddr *addr,
1373 int alen, int flags)
1374{
1375 struct sock *sk = sock->sk;
1376 struct smc_sock *smc;
1377 int rc = -EINVAL;
1378
1379 smc = smc_sk(sk);
1380
1381 /* separate smc parameter checking to be safe */
1382 if (alen < sizeof(addr->sa_family))
1383 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001384 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +01001385 goto out_err;
1386
1387 lock_sock(sk);
1388 switch (sk->sk_state) {
1389 default:
1390 goto out;
1391 case SMC_ACTIVE:
1392 rc = -EISCONN;
1393 goto out;
1394 case SMC_INIT:
Ursula Braunac713872017-01-09 16:55:13 +01001395 break;
1396 }
1397
1398 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001399 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun50717a32019-04-12 12:57:23 +02001400 if (smc->connect_nonblock) {
1401 rc = -EALREADY;
1402 goto out;
1403 }
1404 rc = kernel_connect(smc->clcsock, addr, alen, flags);
1405 if (rc && rc != -EINPROGRESS)
1406 goto out;
Ursula Braun301428e2019-10-29 12:41:26 +01001407
1408 sock_hold(&smc->sk); /* sock put in passive closing */
Ursula Braun86434742019-12-12 22:35:58 +01001409 if (smc->use_fallback)
1410 goto out;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001411 if (flags & O_NONBLOCK) {
Karsten Graul22ef4732020-09-10 18:48:29 +02001412 if (queue_work(smc_hs_wq, &smc->connect_work))
Ursula Braun50717a32019-04-12 12:57:23 +02001413 smc->connect_nonblock = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001414 rc = -EINPROGRESS;
1415 } else {
Ursula Braun24ac3a02018-06-27 17:59:50 +02001416 rc = __smc_connect(smc);
1417 if (rc < 0)
1418 goto out;
1419 else
1420 rc = 0; /* success cases including fallback */
1421 }
Ursula Braunac713872017-01-09 16:55:13 +01001422
1423out:
1424 release_sock(sk);
1425out_err:
1426 return rc;
1427}
1428
1429static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
1430{
Ursula Braun3163c502018-01-24 10:28:12 +01001431 struct socket *new_clcsock = NULL;
1432 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +01001433 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -08001434 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +01001435
Ursula Braun3163c502018-01-24 10:28:12 +01001436 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001437 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001438 if (!new_sk) {
1439 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +01001440 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +01001441 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +01001442 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +01001443 goto out;
1444 }
1445 *new_smc = smc_sk(new_sk);
1446
Myungho Jung78abe3d2018-12-18 09:02:25 -08001447 mutex_lock(&lsmc->clcsock_release_lock);
1448 if (lsmc->clcsock)
Ursula Brauna60a2b12020-09-10 18:48:20 +02001449 rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK);
Myungho Jung78abe3d2018-12-18 09:02:25 -08001450 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +01001451 lock_sock(lsk);
Ursula Brauna60a2b12020-09-10 18:48:20 +02001452 if (rc < 0 && rc != -EAGAIN)
Ursula Braun3163c502018-01-24 10:28:12 +01001453 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +01001454 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +02001455 new_sk->sk_prot->unhash(new_sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001456 if (new_clcsock)
1457 sock_release(new_clcsock);
1458 new_sk->sk_state = SMC_CLOSED;
1459 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun51f1de72018-01-26 09:28:48 +01001460 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +01001461 *new_smc = NULL;
1462 goto out;
1463 }
1464
Ursula Brauna60a2b12020-09-10 18:48:20 +02001465 /* new clcsock has inherited the smc listen-specific sk_data_ready
1466 * function; switch it back to the original sk_data_ready function
1467 */
1468 new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready;
Ursula Braunac713872017-01-09 16:55:13 +01001469 (*new_smc)->clcsock = new_clcsock;
1470out:
1471 return rc;
1472}
1473
Ursula Brauna046d572017-01-09 16:55:16 +01001474/* add a just created sock to the accept queue of the listen sock as
1475 * candidate for a following socket accept call from user space
1476 */
1477static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
1478{
1479 struct smc_sock *par = smc_sk(parent);
1480
Ursula Braun51f1de72018-01-26 09:28:48 +01001481 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +01001482 spin_lock(&par->accept_q_lock);
1483 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
1484 spin_unlock(&par->accept_q_lock);
1485 sk_acceptq_added(parent);
1486}
1487
1488/* remove a socket from the accept queue of its parental listening socket */
1489static void smc_accept_unlink(struct sock *sk)
1490{
1491 struct smc_sock *par = smc_sk(sk)->listen_smc;
1492
1493 spin_lock(&par->accept_q_lock);
1494 list_del_init(&smc_sk(sk)->accept_q);
1495 spin_unlock(&par->accept_q_lock);
1496 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001497 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +01001498}
1499
1500/* remove a sock from the accept queue to bind it to a new socket created
1501 * for a socket accept call from user space
1502 */
Ursula Braunb38d7322017-01-09 16:55:25 +01001503struct sock *smc_accept_dequeue(struct sock *parent,
1504 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +01001505{
1506 struct smc_sock *isk, *n;
1507 struct sock *new_sk;
1508
1509 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
1510 new_sk = (struct sock *)isk;
1511
1512 smc_accept_unlink(new_sk);
1513 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +02001514 new_sk->sk_prot->unhash(new_sk);
Ursula Braun127f4972018-01-26 09:28:49 +01001515 if (isk->clcsock) {
1516 sock_release(isk->clcsock);
1517 isk->clcsock = NULL;
1518 }
Ursula Braun51f1de72018-01-26 09:28:48 +01001519 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +01001520 continue;
1521 }
Ursula Braun07603b22019-04-11 11:17:32 +02001522 if (new_sock) {
Ursula Brauna046d572017-01-09 16:55:16 +01001523 sock_graft(new_sk, new_sock);
Ursula Braun07603b22019-04-11 11:17:32 +02001524 if (isk->use_fallback) {
1525 smc_sk(new_sk)->clcsock->file = new_sock->file;
1526 isk->clcsock->file->private_data = isk->clcsock;
1527 }
1528 }
Ursula Brauna046d572017-01-09 16:55:16 +01001529 return new_sk;
1530 }
1531 return NULL;
1532}
1533
1534/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +01001535void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +01001536{
1537 struct smc_sock *smc = smc_sk(sk);
1538
Ursula Braun81cf4f42019-10-21 16:13:15 +02001539 sock_hold(sk); /* sock_put below */
Ursula Braunb38d7322017-01-09 16:55:25 +01001540 lock_sock(sk);
1541 if (!sk->sk_lingertime)
1542 /* wait for peer closing */
1543 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun39f41f32019-06-27 15:04:52 +02001544 __smc_release(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +01001545 release_sock(sk);
Ursula Braun81cf4f42019-10-21 16:13:15 +02001546 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +01001547 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +01001548}
1549
Karsten Graulb9247542020-04-29 17:10:41 +02001550static int smcr_serv_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001551{
Karsten Graul387707f2020-04-29 17:10:40 +02001552 struct smc_link *link = smc->conn.lnk;
Karsten Graul4667bb42020-04-30 15:55:42 +02001553 struct smc_llc_qentry *qentry;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001554 int rc;
1555
Karsten Graul7562a132020-05-01 12:48:01 +02001556 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +02001557 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +02001558
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001559 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +02001560 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001561 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001562 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001563
1564 /* receive CONFIRM LINK response from client over the RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001565 qentry = smc_llc_wait(link->lgr, link, SMC_LLC_WAIT_TIME,
1566 SMC_LLC_CONFIRM_LINK);
1567 if (!qentry) {
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001568 struct smc_clc_msg_decline dclc;
1569
1570 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001571 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001572 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001573 }
Karsten Graul649758f2020-05-04 14:18:48 +02001574 smc_llc_save_peer_uid(qentry);
Karsten Graul4667bb42020-04-30 15:55:42 +02001575 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_RESP);
1576 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
1577 if (rc)
Karsten Graul75d320d2018-03-01 13:51:31 +01001578 return SMC_CLC_DECL_RMBE_EC;
1579
Karsten Graul4667bb42020-04-30 15:55:42 +02001580 /* confirm_rkey is implicit on 1st contact */
1581 smc->conn.rmb_desc->is_conf_rkey = true;
Karsten Graul52bedf32018-03-01 13:51:32 +01001582
Karsten Graul00a049c2020-04-29 17:10:49 +02001583 smc_llc_link_active(link);
Karsten Graul0a99be42020-05-05 15:01:20 +02001584 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
Karsten Graul52bedf32018-03-01 13:51:32 +01001585
Karsten Graul4667bb42020-04-30 15:55:42 +02001586 /* initial contact - try to establish second link */
Karsten Graulb4ba4652021-10-16 11:37:50 +02001587 smc_llc_srv_add_link(link, NULL);
Karsten Graul75d320d2018-03-01 13:51:31 +01001588 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001589}
1590
Hans Wippel3b2dec22018-05-18 09:34:18 +02001591/* listen worker: finish */
1592static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001593{
Ursula Brauna046d572017-01-09 16:55:16 +01001594 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001595 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001596
Ursula Brauna046d572017-01-09 16:55:16 +01001597 if (lsmc->sk.sk_state == SMC_LISTEN) {
Karsten Graulfd577702019-04-11 11:17:30 +02001598 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001599 smc_accept_enqueue(&lsmc->sk, newsmcsk);
Karsten Graulfd577702019-04-11 11:17:30 +02001600 release_sock(&lsmc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001601 } else { /* no longer listening */
1602 smc_close_non_accepted(newsmcsk);
1603 }
Ursula Brauna046d572017-01-09 16:55:16 +01001604
1605 /* Wake up accept */
1606 lsmc->sk.sk_data_ready(&lsmc->sk);
1607 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001608}
Ursula Brauna046d572017-01-09 16:55:16 +01001609
Hans Wippel3b2dec22018-05-18 09:34:18 +02001610/* listen worker: finish in state connected */
1611static void smc_listen_out_connected(struct smc_sock *new_smc)
1612{
1613 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001614
Hans Wippel3b2dec22018-05-18 09:34:18 +02001615 sk_refcnt_debug_inc(newsmcsk);
1616 if (newsmcsk->sk_state == SMC_INIT)
1617 newsmcsk->sk_state = SMC_ACTIVE;
1618
1619 smc_listen_out(new_smc);
1620}
1621
1622/* listen worker: finish in error state */
1623static void smc_listen_out_err(struct smc_sock *new_smc)
1624{
1625 struct sock *newsmcsk = &new_smc->sk;
Guvenc Gulce194730a2021-06-16 16:52:58 +02001626 struct net *net = sock_net(newsmcsk);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001627
Guvenc Gulce194730a2021-06-16 16:52:58 +02001628 this_cpu_inc(net->smc.smc_stats->srv_hshake_err_cnt);
Ursula Braun51f1de72018-01-26 09:28:48 +01001629 if (newsmcsk->sk_state == SMC_INIT)
1630 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001631 newsmcsk->sk_state = SMC_CLOSED;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001632
1633 smc_listen_out(new_smc);
1634}
1635
1636/* listen worker: decline and fall back if possible */
1637static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
Karsten Graul4a9baf42020-10-23 20:48:28 +02001638 int local_first, u8 version)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001639{
1640 /* RDMA setup failed, switch back to TCP */
Karsten Graul8cf3f3e2020-12-01 20:20:36 +01001641 smc_conn_abort(new_smc, local_first);
Wen Guc0bf3d82022-01-22 17:43:09 +08001642 if (reason_code < 0 ||
1643 smc_switch_to_fallback(new_smc, reason_code)) {
1644 /* error, no fallback possible */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001645 smc_listen_out_err(new_smc);
1646 return;
1647 }
Karsten Graul603cc142018-07-25 16:35:32 +02001648 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Ursula Braune8d726c82020-09-26 12:44:32 +02001649 if (smc_clc_send_decline(new_smc, reason_code, version) < 0) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001650 smc_listen_out_err(new_smc);
1651 return;
1652 }
1653 }
1654 smc_listen_out_connected(new_smc);
1655}
1656
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001657/* listen worker: version checking */
1658static int smc_listen_v2_check(struct smc_sock *new_smc,
1659 struct smc_clc_msg_proposal *pclc,
1660 struct smc_init_info *ini)
1661{
1662 struct smc_clc_smcd_v2_extension *pclc_smcd_v2_ext;
1663 struct smc_clc_v2_extension *pclc_v2_ext;
Karsten Graul37524042020-10-31 19:19:38 +01001664 int rc = SMC_CLC_DECL_PEERNOSMC;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001665
1666 ini->smc_type_v1 = pclc->hdr.typev1;
1667 ini->smc_type_v2 = pclc->hdr.typev2;
Karsten Graule49300a2021-10-16 11:37:46 +02001668 ini->smcd_version = smcd_indicated(ini->smc_type_v1) ? SMC_V1 : 0;
1669 ini->smcr_version = smcr_indicated(ini->smc_type_v1) ? SMC_V1 : 0;
1670 if (pclc->hdr.version > SMC_V1) {
1671 if (smcd_indicated(ini->smc_type_v2))
1672 ini->smcd_version |= SMC_V2;
1673 if (smcr_indicated(ini->smc_type_v2))
1674 ini->smcr_version |= SMC_V2;
Karsten Graul37524042020-10-31 19:19:38 +01001675 }
Karsten Graule49300a2021-10-16 11:37:46 +02001676 if (!(ini->smcd_version & SMC_V2) && !(ini->smcr_version & SMC_V2)) {
1677 rc = SMC_CLC_DECL_PEERNOSMC;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001678 goto out;
1679 }
1680 pclc_v2_ext = smc_get_clc_v2_ext(pclc);
1681 if (!pclc_v2_ext) {
1682 ini->smcd_version &= ~SMC_V2;
Karsten Graule49300a2021-10-16 11:37:46 +02001683 ini->smcr_version &= ~SMC_V2;
Karsten Graul37524042020-10-31 19:19:38 +01001684 rc = SMC_CLC_DECL_NOV2EXT;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001685 goto out;
1686 }
1687 pclc_smcd_v2_ext = smc_get_clc_smcd_v2_ext(pclc_v2_ext);
Karsten Graule49300a2021-10-16 11:37:46 +02001688 if (ini->smcd_version & SMC_V2) {
1689 if (!smc_ism_is_v2_capable()) {
1690 ini->smcd_version &= ~SMC_V2;
1691 rc = SMC_CLC_DECL_NOISM2SUPP;
1692 } else if (!pclc_smcd_v2_ext) {
1693 ini->smcd_version &= ~SMC_V2;
1694 rc = SMC_CLC_DECL_NOV2DEXT;
1695 } else if (!pclc_v2_ext->hdr.eid_cnt &&
1696 !pclc_v2_ext->hdr.flag.seid) {
1697 ini->smcd_version &= ~SMC_V2;
1698 rc = SMC_CLC_DECL_NOUEID;
1699 }
1700 }
1701 if (ini->smcr_version & SMC_V2) {
1702 if (!pclc_v2_ext->hdr.eid_cnt) {
1703 ini->smcr_version &= ~SMC_V2;
1704 rc = SMC_CLC_DECL_NOUEID;
1705 }
Karsten Graul37524042020-10-31 19:19:38 +01001706 }
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001707
1708out:
Karsten Graule49300a2021-10-16 11:37:46 +02001709 if (!ini->smcd_version && !ini->smcr_version)
Karsten Graul37524042020-10-31 19:19:38 +01001710 return rc;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001711
1712 return 0;
1713}
1714
Hans Wippel3b2dec22018-05-18 09:34:18 +02001715/* listen worker: check prefixes */
Karsten Graul59886692019-04-12 12:57:25 +02001716static int smc_listen_prfx_check(struct smc_sock *new_smc,
Hans Wippel3b2dec22018-05-18 09:34:18 +02001717 struct smc_clc_msg_proposal *pclc)
1718{
1719 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1720 struct socket *newclcsock = new_smc->clcsock;
1721
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001722 if (pclc->hdr.typev1 == SMC_TYPE_N)
1723 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001724 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1725 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
Karsten Graul59886692019-04-12 12:57:25 +02001726 return SMC_CLC_DECL_DIFFPREFIX;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001727
1728 return 0;
1729}
1730
1731/* listen worker: initialize connection and buffers */
1732static int smc_listen_rdma_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001733 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001734{
Karsten Graul7a627252019-04-12 12:57:30 +02001735 int rc;
1736
Hans Wippel3b2dec22018-05-18 09:34:18 +02001737 /* allocate connection / link group */
Karsten Graul7a627252019-04-12 12:57:30 +02001738 rc = smc_conn_create(new_smc, ini);
1739 if (rc)
1740 return rc;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001741
1742 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001743 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001744 return SMC_CLC_DECL_MEM;
1745
1746 return 0;
1747}
1748
Hans Wippel41349842018-06-28 19:05:11 +02001749/* listen worker: initialize connection and buffers for SMC-D */
1750static int smc_listen_ism_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001751 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +02001752{
Karsten Graul7a627252019-04-12 12:57:30 +02001753 int rc;
Hans Wippel41349842018-06-28 19:05:11 +02001754
Karsten Graul7a627252019-04-12 12:57:30 +02001755 rc = smc_conn_create(new_smc, ini);
1756 if (rc)
1757 return rc;
Hans Wippel41349842018-06-28 19:05:11 +02001758
Hans Wippel41349842018-06-28 19:05:11 +02001759 /* Create send and receive buffers */
Karsten Graul72b7f6c2020-07-26 20:34:28 +02001760 rc = smc_buf_create(new_smc, true);
1761 if (rc) {
Karsten Graul8cf3f3e2020-12-01 20:20:36 +01001762 smc_conn_abort(new_smc, ini->first_contact_local);
Karsten Graul72b7f6c2020-07-26 20:34:28 +02001763 return (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB :
1764 SMC_CLC_DECL_MEM;
Hans Wippel41349842018-06-28 19:05:11 +02001765 }
1766
1767 return 0;
1768}
1769
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001770static bool smc_is_already_selected(struct smcd_dev *smcd,
1771 struct smc_init_info *ini,
1772 int matches)
1773{
1774 int i;
1775
1776 for (i = 0; i < matches; i++)
1777 if (smcd == ini->ism_dev[i])
1778 return true;
1779
1780 return false;
1781}
1782
1783/* check for ISM devices matching proposed ISM devices */
1784static void smc_check_ism_v2_match(struct smc_init_info *ini,
1785 u16 proposed_chid, u64 proposed_gid,
1786 unsigned int *matches)
1787{
1788 struct smcd_dev *smcd;
1789
1790 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
1791 if (smcd->going_away)
1792 continue;
1793 if (smc_is_already_selected(smcd, ini, *matches))
1794 continue;
1795 if (smc_ism_get_chid(smcd) == proposed_chid &&
1796 !smc_ism_cantalk(proposed_gid, ISM_RESERVED_VLANID, smcd)) {
1797 ini->ism_peer_gid[*matches] = proposed_gid;
1798 ini->ism_dev[*matches] = smcd;
1799 (*matches)++;
1800 break;
1801 }
1802 }
1803}
1804
Karsten Graul37524042020-10-31 19:19:38 +01001805static void smc_find_ism_store_rc(u32 rc, struct smc_init_info *ini)
1806{
1807 if (!ini->rc)
1808 ini->rc = rc;
1809}
1810
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001811static void smc_find_ism_v2_device_serv(struct smc_sock *new_smc,
1812 struct smc_clc_msg_proposal *pclc,
1813 struct smc_init_info *ini)
1814{
1815 struct smc_clc_smcd_v2_extension *smcd_v2_ext;
1816 struct smc_clc_v2_extension *smc_v2_ext;
1817 struct smc_clc_msg_smcd *pclc_smcd;
1818 unsigned int matches = 0;
Karsten Graulf29fa002020-10-07 22:57:43 +02001819 u8 smcd_version;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001820 u8 *eid = NULL;
Karsten Graul37524042020-10-31 19:19:38 +01001821 int i, rc;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001822
1823 if (!(ini->smcd_version & SMC_V2) || !smcd_indicated(ini->smc_type_v2))
Karsten Graulf29fa002020-10-07 22:57:43 +02001824 goto not_found;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001825
1826 pclc_smcd = smc_get_clc_msg_smcd(pclc);
1827 smc_v2_ext = smc_get_clc_v2_ext(pclc);
1828 smcd_v2_ext = smc_get_clc_smcd_v2_ext(smc_v2_ext);
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001829
1830 mutex_lock(&smcd_dev_list.mutex);
1831 if (pclc_smcd->ism.chid)
1832 /* check for ISM device matching proposed native ISM device */
1833 smc_check_ism_v2_match(ini, ntohs(pclc_smcd->ism.chid),
1834 ntohll(pclc_smcd->ism.gid), &matches);
1835 for (i = 1; i <= smc_v2_ext->hdr.ism_gid_cnt; i++) {
1836 /* check for ISM devices matching proposed non-native ISM
1837 * devices
1838 */
1839 smc_check_ism_v2_match(ini,
1840 ntohs(smcd_v2_ext->gidchid[i - 1].chid),
1841 ntohll(smcd_v2_ext->gidchid[i - 1].gid),
1842 &matches);
1843 }
1844 mutex_unlock(&smcd_dev_list.mutex);
1845
Karsten Graule49300a2021-10-16 11:37:46 +02001846 if (!ini->ism_dev[0]) {
1847 smc_find_ism_store_rc(SMC_CLC_DECL_NOSMCD2DEV, ini);
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001848 goto not_found;
Karsten Graule49300a2021-10-16 11:37:46 +02001849 }
Karsten Graulfa086662021-09-14 10:35:05 +02001850
Karsten Graul11a26c52021-09-14 10:35:06 +02001851 smc_ism_get_system_eid(&eid);
Karsten Graulfa086662021-09-14 10:35:05 +02001852 if (!smc_clc_match_eid(ini->negotiated_eid, smc_v2_ext,
1853 smcd_v2_ext->system_eid, eid))
1854 goto not_found;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001855
1856 /* separate - outside the smcd_dev_list.lock */
Karsten Graulf29fa002020-10-07 22:57:43 +02001857 smcd_version = ini->smcd_version;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001858 for (i = 0; i < matches; i++) {
1859 ini->smcd_version = SMC_V2;
1860 ini->is_smcd = true;
1861 ini->ism_selected = i;
Karsten Graul37524042020-10-31 19:19:38 +01001862 rc = smc_listen_ism_init(new_smc, ini);
1863 if (rc) {
1864 smc_find_ism_store_rc(rc, ini);
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001865 /* try next active ISM device */
1866 continue;
Karsten Graul37524042020-10-31 19:19:38 +01001867 }
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001868 return; /* matching and usable V2 ISM device found */
1869 }
Karsten Graulf29fa002020-10-07 22:57:43 +02001870 /* no V2 ISM device could be initialized */
1871 ini->smcd_version = smcd_version; /* restore original value */
Karsten Graulfa086662021-09-14 10:35:05 +02001872 ini->negotiated_eid[0] = 0;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001873
1874not_found:
1875 ini->smcd_version &= ~SMC_V2;
1876 ini->ism_dev[0] = NULL;
1877 ini->is_smcd = false;
1878}
1879
1880static void smc_find_ism_v1_device_serv(struct smc_sock *new_smc,
1881 struct smc_clc_msg_proposal *pclc,
1882 struct smc_init_info *ini)
Ursula Braun7affc802020-09-26 12:44:21 +02001883{
1884 struct smc_clc_msg_smcd *pclc_smcd = smc_get_clc_msg_smcd(pclc);
Karsten Graul37524042020-10-31 19:19:38 +01001885 int rc = 0;
Ursula Braun7affc802020-09-26 12:44:21 +02001886
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001887 /* check if ISM V1 is available */
1888 if (!(ini->smcd_version & SMC_V1) || !smcd_indicated(ini->smc_type_v1))
Ursula Braun7affc802020-09-26 12:44:21 +02001889 goto not_found;
1890 ini->is_smcd = true; /* prepare ISM check */
Ursula Braun8c3dca32020-09-26 12:44:28 +02001891 ini->ism_peer_gid[0] = ntohll(pclc_smcd->ism.gid);
Karsten Graul37524042020-10-31 19:19:38 +01001892 rc = smc_find_ism_device(new_smc, ini);
1893 if (rc)
Ursula Braun7affc802020-09-26 12:44:21 +02001894 goto not_found;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001895 ini->ism_selected = 0;
Karsten Graul37524042020-10-31 19:19:38 +01001896 rc = smc_listen_ism_init(new_smc, ini);
1897 if (!rc)
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001898 return; /* V1 ISM device found */
Ursula Braun7affc802020-09-26 12:44:21 +02001899
1900not_found:
Karsten Graul37524042020-10-31 19:19:38 +01001901 smc_find_ism_store_rc(rc, ini);
Karsten Graule49300a2021-10-16 11:37:46 +02001902 ini->smcd_version &= ~SMC_V1;
Ursula Braun3fc64932020-09-26 12:44:23 +02001903 ini->ism_dev[0] = NULL;
Ursula Braun7affc802020-09-26 12:44:21 +02001904 ini->is_smcd = false;
1905}
1906
Hans Wippel3b2dec22018-05-18 09:34:18 +02001907/* listen worker: register buffers */
Ursula Braun5ac54d82020-09-10 18:48:21 +02001908static int smc_listen_rdma_reg(struct smc_sock *new_smc, bool local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001909{
Karsten Graulb9247542020-04-29 17:10:41 +02001910 struct smc_connection *conn = &new_smc->conn;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001911
Ursula Braun5ac54d82020-09-10 18:48:21 +02001912 if (!local_first) {
Karsten Graul7562a132020-05-01 12:48:01 +02001913 if (smcr_lgr_reg_rmbs(conn->lnk, conn->rmb_desc))
Karsten Graulc7674c02018-11-22 10:26:43 +01001914 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001915 }
1916 smc_rmb_sync_sg_for_device(&new_smc->conn);
1917
1918 return 0;
1919}
1920
Karsten Graule49300a2021-10-16 11:37:46 +02001921static void smc_find_rdma_v2_device_serv(struct smc_sock *new_smc,
1922 struct smc_clc_msg_proposal *pclc,
1923 struct smc_init_info *ini)
1924{
1925 struct smc_clc_v2_extension *smc_v2_ext;
1926 u8 smcr_version;
1927 int rc;
1928
1929 if (!(ini->smcr_version & SMC_V2) || !smcr_indicated(ini->smc_type_v2))
1930 goto not_found;
1931
1932 smc_v2_ext = smc_get_clc_v2_ext(pclc);
1933 if (!smc_clc_match_eid(ini->negotiated_eid, smc_v2_ext, NULL, NULL))
1934 goto not_found;
1935
1936 /* prepare RDMA check */
1937 memcpy(ini->peer_systemid, pclc->lcl.id_for_peer, SMC_SYSTEMID_LEN);
1938 memcpy(ini->peer_gid, smc_v2_ext->roce, SMC_GID_SIZE);
1939 memcpy(ini->peer_mac, pclc->lcl.mac, ETH_ALEN);
1940 ini->check_smcrv2 = true;
1941 ini->smcrv2.clc_sk = new_smc->clcsock->sk;
1942 ini->smcrv2.saddr = new_smc->clcsock->sk->sk_rcv_saddr;
1943 ini->smcrv2.daddr = smc_ib_gid_to_ipv4(smc_v2_ext->roce);
1944 rc = smc_find_rdma_device(new_smc, ini);
1945 if (rc) {
1946 smc_find_ism_store_rc(rc, ini);
1947 goto not_found;
1948 }
1949 if (!ini->smcrv2.uses_gateway)
1950 memcpy(ini->smcrv2.nexthop_mac, pclc->lcl.mac, ETH_ALEN);
1951
1952 smcr_version = ini->smcr_version;
1953 ini->smcr_version = SMC_V2;
1954 rc = smc_listen_rdma_init(new_smc, ini);
1955 if (!rc)
1956 rc = smc_listen_rdma_reg(new_smc, ini->first_contact_local);
1957 if (!rc)
1958 return;
1959 ini->smcr_version = smcr_version;
1960 smc_find_ism_store_rc(rc, ini);
1961
1962not_found:
1963 ini->smcr_version &= ~SMC_V2;
1964 ini->check_smcrv2 = false;
1965}
1966
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001967static int smc_find_rdma_v1_device_serv(struct smc_sock *new_smc,
1968 struct smc_clc_msg_proposal *pclc,
1969 struct smc_init_info *ini)
Ursula Braun7affc802020-09-26 12:44:21 +02001970{
1971 int rc;
1972
Karsten Graule49300a2021-10-16 11:37:46 +02001973 if (!(ini->smcr_version & SMC_V1) || !smcr_indicated(ini->smc_type_v1))
Ursula Braun7affc802020-09-26 12:44:21 +02001974 return SMC_CLC_DECL_NOSMCDEV;
1975
1976 /* prepare RDMA check */
Karsten Graule49300a2021-10-16 11:37:46 +02001977 memcpy(ini->peer_systemid, pclc->lcl.id_for_peer, SMC_SYSTEMID_LEN);
1978 memcpy(ini->peer_gid, pclc->lcl.gid, SMC_GID_SIZE);
1979 memcpy(ini->peer_mac, pclc->lcl.mac, ETH_ALEN);
Ursula Braun7affc802020-09-26 12:44:21 +02001980 rc = smc_find_rdma_device(new_smc, ini);
1981 if (rc) {
1982 /* no RDMA device found */
Karsten Graule49300a2021-10-16 11:37:46 +02001983 return SMC_CLC_DECL_NOSMCDEV;
Ursula Braun7affc802020-09-26 12:44:21 +02001984 }
1985 rc = smc_listen_rdma_init(new_smc, ini);
1986 if (rc)
1987 return rc;
1988 return smc_listen_rdma_reg(new_smc, ini->first_contact_local);
1989}
1990
1991/* determine the local device matching to proposal */
1992static int smc_listen_find_device(struct smc_sock *new_smc,
1993 struct smc_clc_msg_proposal *pclc,
1994 struct smc_init_info *ini)
1995{
Karsten Graule49300a2021-10-16 11:37:46 +02001996 int prfx_rc;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001997
1998 /* check for ISM device matching V2 proposed device */
1999 smc_find_ism_v2_device_serv(new_smc, pclc, ini);
2000 if (ini->ism_dev[0])
Ursula Braun7affc802020-09-26 12:44:21 +02002001 return 0;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02002002
Karsten Graule49300a2021-10-16 11:37:46 +02002003 /* check for matching IP prefix and subnet length (V1) */
2004 prfx_rc = smc_listen_prfx_check(new_smc, pclc);
2005 if (prfx_rc)
2006 smc_find_ism_store_rc(prfx_rc, ini);
Ursula Braun5c21c4c2020-09-26 12:44:29 +02002007
2008 /* get vlan id from IP device */
2009 if (smc_vlan_by_tcpsk(new_smc->clcsock, ini))
Karsten Graul37524042020-10-31 19:19:38 +01002010 return ini->rc ?: SMC_CLC_DECL_GETVLANERR;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02002011
2012 /* check for ISM device matching V1 proposed device */
Karsten Graule49300a2021-10-16 11:37:46 +02002013 if (!prfx_rc)
2014 smc_find_ism_v1_device_serv(new_smc, pclc, ini);
Ursula Braun5c21c4c2020-09-26 12:44:29 +02002015 if (ini->ism_dev[0])
2016 return 0;
2017
Karsten Graule49300a2021-10-16 11:37:46 +02002018 if (!smcr_indicated(pclc->hdr.typev1) &&
2019 !smcr_indicated(pclc->hdr.typev2))
Karsten Graul37524042020-10-31 19:19:38 +01002020 /* skip RDMA and decline */
2021 return ini->rc ?: SMC_CLC_DECL_NOSMCDDEV;
Ursula Braun7affc802020-09-26 12:44:21 +02002022
Karsten Graule49300a2021-10-16 11:37:46 +02002023 /* check if RDMA V2 is available */
2024 smc_find_rdma_v2_device_serv(new_smc, pclc, ini);
2025 if (ini->smcrv2.ib_dev_v2)
2026 return 0;
Karsten Graul37524042020-10-31 19:19:38 +01002027
Karsten Graule49300a2021-10-16 11:37:46 +02002028 /* check if RDMA V1 is available */
2029 if (!prfx_rc) {
2030 int rc;
2031
2032 rc = smc_find_rdma_v1_device_serv(new_smc, pclc, ini);
2033 smc_find_ism_store_rc(rc, ini);
2034 return (!rc) ? 0 : ini->rc;
2035 }
2036 return SMC_CLC_DECL_NOSMCDEV;
Ursula Braun7affc802020-09-26 12:44:21 +02002037}
2038
Hans Wippel3b2dec22018-05-18 09:34:18 +02002039/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02002040static int smc_listen_rdma_finish(struct smc_sock *new_smc,
2041 struct smc_clc_msg_accept_confirm *cclc,
Karsten Graule49300a2021-10-16 11:37:46 +02002042 bool local_first,
2043 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +02002044{
Karsten Graul387707f2020-04-29 17:10:40 +02002045 struct smc_link *link = new_smc->conn.lnk;
Hans Wippel3b2dec22018-05-18 09:34:18 +02002046 int reason_code = 0;
2047
Ursula Braun5ac54d82020-09-10 18:48:21 +02002048 if (local_first)
Karsten Graule49300a2021-10-16 11:37:46 +02002049 smc_link_save_peer_info(link, cclc, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +02002050
Ursula Braun0c881ad2020-09-10 18:48:25 +02002051 if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc))
2052 return SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02002053
Ursula Braun5ac54d82020-09-10 18:48:21 +02002054 if (local_first) {
Ursula Braun0c881ad2020-09-10 18:48:25 +02002055 if (smc_ib_ready_link(link))
2056 return SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02002057 /* QP confirmation over RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02002058 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
Karsten Graulb9247542020-04-29 17:10:41 +02002059 reason_code = smcr_serv_conf_first_link(new_smc);
Karsten Graul4667bb42020-04-30 15:55:42 +02002060 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
Hans Wippel3b2dec22018-05-18 09:34:18 +02002061 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02002062 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02002063}
2064
Ursula Braun7affc802020-09-26 12:44:21 +02002065/* setup for connection of server */
Hans Wippel3b2dec22018-05-18 09:34:18 +02002066static void smc_listen_work(struct work_struct *work)
2067{
2068 struct smc_sock *new_smc = container_of(work, struct smc_sock,
2069 smc_listen_work);
2070 struct socket *newclcsock = new_smc->clcsock;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02002071 struct smc_clc_msg_accept_confirm *cclc;
Ursula Braun6bb14e482020-09-10 18:48:22 +02002072 struct smc_clc_msg_proposal_area *buf;
Hans Wippel3b2dec22018-05-18 09:34:18 +02002073 struct smc_clc_msg_proposal *pclc;
Ursula Braun3fc64932020-09-26 12:44:23 +02002074 struct smc_init_info *ini = NULL;
Karsten Graule49300a2021-10-16 11:37:46 +02002075 u8 proposal_version = SMC_V1;
2076 u8 accept_version;
Hans Wippel3b2dec22018-05-18 09:34:18 +02002077 int rc = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02002078
Karsten Graulfd577702019-04-11 11:17:30 +02002079 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
2080 return smc_listen_out_err(new_smc);
2081
Hans Wippel3b2dec22018-05-18 09:34:18 +02002082 if (new_smc->use_fallback) {
2083 smc_listen_out_connected(new_smc);
2084 return;
2085 }
2086
2087 /* check if peer is smc capable */
2088 if (!tcp_sk(newclcsock->sk)->syn_smc) {
Wen Guc0bf3d82022-01-22 17:43:09 +08002089 rc = smc_switch_to_fallback(new_smc, SMC_CLC_DECL_PEERNOSMC);
2090 if (rc)
2091 smc_listen_out_err(new_smc);
2092 else
2093 smc_listen_out_connected(new_smc);
Hans Wippel3b2dec22018-05-18 09:34:18 +02002094 return;
2095 }
2096
2097 /* do inband token exchange -
2098 * wait for and receive SMC Proposal CLC message
2099 */
Ursula Braun6bb14e482020-09-10 18:48:22 +02002100 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
2101 if (!buf) {
2102 rc = SMC_CLC_DECL_MEM;
2103 goto out_decl;
2104 }
2105 pclc = (struct smc_clc_msg_proposal *)buf;
2106 rc = smc_clc_wait_msg(new_smc, pclc, sizeof(*buf),
Karsten Graul228bae02019-04-12 12:57:28 +02002107 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Karsten Graul9aa68d22019-04-12 12:57:29 +02002108 if (rc)
2109 goto out_decl;
Karsten Graule49300a2021-10-16 11:37:46 +02002110
2111 if (pclc->hdr.version > SMC_V1)
2112 proposal_version = SMC_V2;
Hans Wippel3b2dec22018-05-18 09:34:18 +02002113
Ursula Braune8d726c82020-09-26 12:44:32 +02002114 /* IPSec connections opt out of SMC optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +02002115 if (using_ipsec(new_smc)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02002116 rc = SMC_CLC_DECL_IPSEC;
2117 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02002118 }
2119
Ursula Braun3fc64932020-09-26 12:44:23 +02002120 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
2121 if (!ini) {
2122 rc = SMC_CLC_DECL_MEM;
2123 goto out_decl;
2124 }
2125
Ursula Braun5c21c4c2020-09-26 12:44:29 +02002126 /* initial version checking */
2127 rc = smc_listen_v2_check(new_smc, pclc, ini);
2128 if (rc)
Karsten Graul9aa68d22019-04-12 12:57:29 +02002129 goto out_decl;
Karsten Graulfba7e8e2019-04-12 12:57:27 +02002130
Hans Wippel72a36a82019-02-07 15:56:18 +01002131 mutex_lock(&smc_server_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02002132 smc_close_init(new_smc);
2133 smc_rx_init(new_smc);
2134 smc_tx_init(new_smc);
2135
Ursula Braun7affc802020-09-26 12:44:21 +02002136 /* determine ISM or RoCE device used for connection */
Ursula Braun3fc64932020-09-26 12:44:23 +02002137 rc = smc_listen_find_device(new_smc, pclc, ini);
Ursula Braun7affc802020-09-26 12:44:21 +02002138 if (rc)
2139 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02002140
2141 /* send SMC Accept CLC message */
Karsten Graule49300a2021-10-16 11:37:46 +02002142 accept_version = ini->is_smcd ? ini->smcd_version : ini->smcr_version;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02002143 rc = smc_clc_send_accept(new_smc, ini->first_contact_local,
Karsten Graule49300a2021-10-16 11:37:46 +02002144 accept_version, ini->negotiated_eid);
Karsten Graul9aa68d22019-04-12 12:57:29 +02002145 if (rc)
2146 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02002147
Hans Wippel62c71392019-02-07 15:56:17 +01002148 /* SMC-D does not need this lock any more */
Ursula Braun3fc64932020-09-26 12:44:23 +02002149 if (ini->is_smcd)
Hans Wippel72a36a82019-02-07 15:56:18 +01002150 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01002151
Hans Wippel3b2dec22018-05-18 09:34:18 +02002152 /* receive SMC Confirm CLC message */
Karsten Graul9047a617dc2020-10-07 22:57:42 +02002153 memset(buf, 0, sizeof(*buf));
2154 cclc = (struct smc_clc_msg_accept_confirm *)buf;
2155 rc = smc_clc_wait_msg(new_smc, cclc, sizeof(*buf),
Karsten Graul228bae02019-04-12 12:57:28 +02002156 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
2157 if (rc) {
Ursula Braun3fc64932020-09-26 12:44:23 +02002158 if (!ini->is_smcd)
Karsten Graul9aa68d22019-04-12 12:57:29 +02002159 goto out_unlock;
2160 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02002161 }
2162
2163 /* finish worker */
Ursula Braun3fc64932020-09-26 12:44:23 +02002164 if (!ini->is_smcd) {
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02002165 rc = smc_listen_rdma_finish(new_smc, cclc,
Karsten Graule49300a2021-10-16 11:37:46 +02002166 ini->first_contact_local, ini);
Hans Wippel62c71392019-02-07 15:56:17 +01002167 if (rc)
Ursula Braun0c881ad2020-09-10 18:48:25 +02002168 goto out_unlock;
2169 mutex_unlock(&smc_server_lgr_pending);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02002170 }
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02002171 smc_conn_save_peer_info(new_smc, cclc);
Hans Wippel3b2dec22018-05-18 09:34:18 +02002172 smc_listen_out_connected(new_smc);
Guvenc Gulce194730a2021-06-16 16:52:58 +02002173 SMC_STAT_SERV_SUCC_INC(sock_net(newclcsock->sk), ini);
Ursula Braunac679362020-09-17 22:46:02 +02002174 goto out_free;
Karsten Graul9aa68d22019-04-12 12:57:29 +02002175
2176out_unlock:
2177 mutex_unlock(&smc_server_lgr_pending);
2178out_decl:
Karsten Graul4a9baf42020-10-23 20:48:28 +02002179 smc_listen_decline(new_smc, rc, ini ? ini->first_contact_local : 0,
Karsten Graule49300a2021-10-16 11:37:46 +02002180 proposal_version);
Ursula Braunac679362020-09-17 22:46:02 +02002181out_free:
Ursula Braun3fc64932020-09-26 12:44:23 +02002182 kfree(ini);
Ursula Braun6bb14e482020-09-10 18:48:22 +02002183 kfree(buf);
Ursula Brauna046d572017-01-09 16:55:16 +01002184}
2185
2186static void smc_tcp_listen_work(struct work_struct *work)
2187{
2188 struct smc_sock *lsmc = container_of(work, struct smc_sock,
2189 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01002190 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01002191 struct smc_sock *new_smc;
2192 int rc = 0;
2193
Ursula Braun3163c502018-01-24 10:28:12 +01002194 lock_sock(lsk);
2195 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01002196 rc = smc_clcsock_accept(lsmc, &new_smc);
Ursula Brauna60a2b12020-09-10 18:48:20 +02002197 if (rc) /* clcsock accept queue empty or error */
Ursula Brauna046d572017-01-09 16:55:16 +01002198 goto out;
2199 if (!new_smc)
2200 continue;
2201
2202 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002203 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02002204 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01002205 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01002206 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
2207 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02002208 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
2209 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01002210 sock_hold(&new_smc->sk); /* sock_put in passive closing */
Karsten Graul22ef4732020-09-10 18:48:29 +02002211 if (!queue_work(smc_hs_wq, &new_smc->smc_listen_work))
Ursula Braun51f1de72018-01-26 09:28:48 +01002212 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01002213 }
2214
2215out:
Ursula Braun3163c502018-01-24 10:28:12 +01002216 release_sock(lsk);
Ursula Brauna60a2b12020-09-10 18:48:20 +02002217 sock_put(&lsmc->sk); /* sock_hold in smc_clcsock_data_ready() */
2218}
2219
2220static void smc_clcsock_data_ready(struct sock *listen_clcsock)
2221{
Wen Gu341adee2022-01-26 23:33:04 +08002222 struct smc_sock *lsmc =
2223 smc_clcsock_user_data(listen_clcsock);
Ursula Brauna60a2b12020-09-10 18:48:20 +02002224
Ursula Brauna60a2b12020-09-10 18:48:20 +02002225 if (!lsmc)
2226 return;
2227 lsmc->clcsk_data_ready(listen_clcsock);
2228 if (lsmc->sk.sk_state == SMC_LISTEN) {
2229 sock_hold(&lsmc->sk); /* sock_put in smc_tcp_listen_work() */
Karsten Graul22ef4732020-09-10 18:48:29 +02002230 if (!queue_work(smc_hs_wq, &lsmc->tcp_listen_work))
Ursula Brauna60a2b12020-09-10 18:48:20 +02002231 sock_put(&lsmc->sk);
2232 }
Ursula Brauna046d572017-01-09 16:55:16 +01002233}
2234
Ursula Braunac713872017-01-09 16:55:13 +01002235static int smc_listen(struct socket *sock, int backlog)
2236{
2237 struct sock *sk = sock->sk;
2238 struct smc_sock *smc;
2239 int rc;
2240
2241 smc = smc_sk(sk);
2242 lock_sock(sk);
2243
2244 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +02002245 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
2246 smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +01002247 goto out;
2248
2249 rc = 0;
2250 if (sk->sk_state == SMC_LISTEN) {
2251 sk->sk_max_ack_backlog = backlog;
2252 goto out;
2253 }
2254 /* some socket options are handled in core, so we could not apply
2255 * them to the clc socket -- copy smc socket options to clc socket
2256 */
2257 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002258 if (!smc->use_fallback)
2259 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01002260
Ursula Brauna60a2b12020-09-10 18:48:20 +02002261 /* save original sk_data_ready function and establish
2262 * smc-specific sk_data_ready function
2263 */
2264 smc->clcsk_data_ready = smc->clcsock->sk->sk_data_ready;
2265 smc->clcsock->sk->sk_data_ready = smc_clcsock_data_ready;
2266 smc->clcsock->sk->sk_user_data =
2267 (void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY);
Ursula Braunac713872017-01-09 16:55:13 +01002268 rc = kernel_listen(smc->clcsock, backlog);
Guo DaXing9ebb0c42021-11-24 13:32:38 +01002269 if (rc) {
2270 smc->clcsock->sk->sk_data_ready = smc->clcsk_data_ready;
Ursula Braunac713872017-01-09 16:55:13 +01002271 goto out;
Guo DaXing9ebb0c42021-11-24 13:32:38 +01002272 }
Ursula Braunac713872017-01-09 16:55:13 +01002273 sk->sk_max_ack_backlog = backlog;
2274 sk->sk_ack_backlog = 0;
2275 sk->sk_state = SMC_LISTEN;
2276
2277out:
2278 release_sock(sk);
2279 return rc;
2280}
2281
2282static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00002283 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01002284{
Ursula Brauna046d572017-01-09 16:55:16 +01002285 struct sock *sk = sock->sk, *nsk;
2286 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01002287 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01002288 long timeo;
2289 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01002290
2291 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01002292 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01002293 lock_sock(sk);
2294
2295 if (lsmc->sk.sk_state != SMC_LISTEN) {
2296 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02002297 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01002298 goto out;
2299 }
2300
Ursula Brauna046d572017-01-09 16:55:16 +01002301 /* Wait for an incoming connection */
2302 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2303 add_wait_queue_exclusive(sk_sleep(sk), &wait);
2304 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
2305 set_current_state(TASK_INTERRUPTIBLE);
2306 if (!timeo) {
2307 rc = -EAGAIN;
2308 break;
2309 }
2310 release_sock(sk);
2311 timeo = schedule_timeout(timeo);
2312 /* wakeup by sk_data_ready in smc_listen_work() */
2313 sched_annotate_sleep();
2314 lock_sock(sk);
2315 if (signal_pending(current)) {
2316 rc = sock_intr_errno(timeo);
2317 break;
2318 }
2319 }
2320 set_current_state(TASK_RUNNING);
2321 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01002322
Ursula Brauna046d572017-01-09 16:55:16 +01002323 if (!rc)
2324 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02002325 release_sock(sk);
2326 if (rc)
2327 goto out;
2328
2329 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
2330 /* wait till data arrives on the socket */
2331 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
2332 MSEC_PER_SEC);
2333 if (smc_sk(nsk)->use_fallback) {
2334 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
2335
2336 lock_sock(clcsk);
2337 if (skb_queue_empty(&clcsk->sk_receive_queue))
2338 sk_wait_data(clcsk, &timeo, NULL);
2339 release_sock(clcsk);
2340 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
2341 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02002342 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02002343 release_sock(nsk);
2344 }
2345 }
Ursula Braunac713872017-01-09 16:55:13 +01002346
2347out:
Ursula Braun51f1de72018-01-26 09:28:48 +01002348 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01002349 return rc;
2350}
2351
2352static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01002353 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01002354{
2355 struct smc_sock *smc;
2356
Ursula Braunb38d7322017-01-09 16:55:25 +01002357 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
2358 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01002359 return -ENOTCONN;
2360
2361 smc = smc_sk(sock->sk);
2362
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01002363 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01002364}
2365
2366static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
2367{
2368 struct sock *sk = sock->sk;
2369 struct smc_sock *smc;
2370 int rc = -EPIPE;
2371
2372 smc = smc_sk(sk);
2373 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01002374 if ((sk->sk_state != SMC_ACTIVE) &&
2375 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2376 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01002377 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002378
2379 if (msg->msg_flags & MSG_FASTOPEN) {
Ursula Brauncd206362019-08-02 10:47:50 +02002380 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Wen Guc0bf3d82022-01-22 17:43:09 +08002381 rc = smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP);
2382 if (rc)
2383 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002384 } else {
2385 rc = -EINVAL;
2386 goto out;
2387 }
2388 }
2389
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002390 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01002391 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002392 } else {
Ursula Braune6727f32017-01-09 16:55:23 +01002393 rc = smc_tx_sendmsg(smc, msg, len);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002394 SMC_STAT_TX_PAYLOAD(smc, len, rc);
2395 }
Ursula Braunac713872017-01-09 16:55:13 +01002396out:
2397 release_sock(sk);
2398 return rc;
2399}
2400
2401static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
2402 int flags)
2403{
2404 struct sock *sk = sock->sk;
2405 struct smc_sock *smc;
2406 int rc = -ENOTCONN;
2407
2408 smc = smc_sk(sk);
2409 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01002410 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2411 /* socket was connected before, no more data to read */
2412 rc = 0;
2413 goto out;
2414 }
Ursula Braunb38d7322017-01-09 16:55:25 +01002415 if ((sk->sk_state == SMC_INIT) ||
2416 (sk->sk_state == SMC_LISTEN) ||
2417 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01002418 goto out;
2419
Ursula Braunb38d7322017-01-09 16:55:25 +01002420 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2421 rc = 0;
2422 goto out;
2423 }
2424
Stefan Raspl9014db22018-05-03 18:12:39 +02002425 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01002426 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02002427 } else {
2428 msg->msg_namelen = 0;
2429 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002430 SMC_STAT_RX_PAYLOAD(smc, rc, rc);
Stefan Raspl9014db22018-05-03 18:12:39 +02002431 }
Ursula Braunb38d7322017-01-09 16:55:25 +01002432
Ursula Braunac713872017-01-09 16:55:13 +01002433out:
2434 release_sock(sk);
2435 return rc;
2436}
2437
Al Viroade994f2017-07-03 00:01:49 -04002438static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01002439{
Ursula Braun8dce2782018-01-26 09:28:47 +01002440 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05002441 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01002442
Ursula Braun8dce2782018-01-26 09:28:47 +01002443 spin_lock(&isk->accept_q_lock);
2444 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002445 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01002446 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01002447
Ursula Braun8dce2782018-01-26 09:28:47 +01002448 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01002449}
2450
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07002451static __poll_t smc_poll(struct file *file, struct socket *sock,
2452 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01002453{
2454 struct sock *sk = sock->sk;
Ursula Braunac713872017-01-09 16:55:13 +01002455 struct smc_sock *smc;
Ursula Braun50717a32019-04-12 12:57:23 +02002456 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01002457
Ursula Braun8dce2782018-01-26 09:28:47 +01002458 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002459 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01002460
Ursula Braunac713872017-01-09 16:55:13 +01002461 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02002462 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01002463 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07002464 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02002465 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braunac713872017-01-09 16:55:13 +01002466 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07002467 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02002468 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01002469 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002470 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01002471 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
2472 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002473 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01002474 if (sk->sk_state == SMC_LISTEN) {
2475 /* woken up by sk_data_ready in smc_listen_work() */
Ursula Braun50717a32019-04-12 12:57:23 +02002476 mask |= smc_accept_poll(sk);
2477 } else if (smc->use_fallback) { /* as result of connect_work()*/
2478 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
2479 wait);
2480 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun8dce2782018-01-26 09:28:47 +01002481 } else {
Ursula Braun50717a32019-04-12 12:57:23 +02002482 if ((sk->sk_state != SMC_INIT &&
2483 atomic_read(&smc->conn.sndbuf_space)) ||
Ursula Braun8dce2782018-01-26 09:28:47 +01002484 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002485 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01002486 } else {
2487 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
2488 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
2489 }
2490 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002491 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01002492 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002493 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01002494 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002495 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02002496 if (smc->conn.urg_state == SMC_URG_VALID)
2497 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01002498 }
Ursula Braunac713872017-01-09 16:55:13 +01002499 }
2500
2501 return mask;
2502}
2503
2504static int smc_shutdown(struct socket *sock, int how)
2505{
2506 struct sock *sk = sock->sk;
Tony Lubacb6c12021-11-26 10:41:35 +08002507 bool do_shutdown = true;
Ursula Braunac713872017-01-09 16:55:13 +01002508 struct smc_sock *smc;
2509 int rc = -EINVAL;
Tony Lubacb6c12021-11-26 10:41:35 +08002510 int old_state;
Ursula Braunb38d7322017-01-09 16:55:25 +01002511 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01002512
2513 smc = smc_sk(sk);
2514
2515 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01002516 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01002517
2518 lock_sock(sk);
2519
2520 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02002521 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01002522 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
2523 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
2524 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2525 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
2526 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01002527 goto out;
2528 if (smc->use_fallback) {
2529 rc = kernel_sock_shutdown(smc->clcsock, how);
2530 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
2531 if (sk->sk_shutdown == SHUTDOWN_MASK)
2532 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01002533 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01002534 }
Ursula Braunb38d7322017-01-09 16:55:25 +01002535 switch (how) {
2536 case SHUT_RDWR: /* shutdown in both directions */
Tony Lubacb6c12021-11-26 10:41:35 +08002537 old_state = sk->sk_state;
Ursula Braunb38d7322017-01-09 16:55:25 +01002538 rc = smc_close_active(smc);
Tony Lubacb6c12021-11-26 10:41:35 +08002539 if (old_state == SMC_ACTIVE &&
2540 sk->sk_state == SMC_PEERCLOSEWAIT1)
2541 do_shutdown = false;
Ursula Braunb38d7322017-01-09 16:55:25 +01002542 break;
2543 case SHUT_WR:
2544 rc = smc_close_shutdown_write(smc);
2545 break;
2546 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02002547 rc = 0;
2548 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01002549 break;
2550 }
Tony Lubacb6c12021-11-26 10:41:35 +08002551 if (do_shutdown && smc->clcsock)
Ursula Braun1255fcb2018-04-19 15:56:40 +02002552 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01002553 /* map sock_shutdown_cmd constants to sk_shutdown value range */
2554 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01002555
2556out:
2557 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01002558 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01002559}
2560
2561static int smc_setsockopt(struct socket *sock, int level, int optname,
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02002562 sockptr_t optval, unsigned int optlen)
Ursula Braunac713872017-01-09 16:55:13 +01002563{
2564 struct sock *sk = sock->sk;
2565 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002566 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01002567
Cong Wang86214362021-05-05 12:40:48 -07002568 if (level == SOL_TCP && optname == TCP_ULP)
2569 return -EOPNOTSUPP;
2570
Ursula Braunac713872017-01-09 16:55:13 +01002571 smc = smc_sk(sk);
2572
2573 /* generic setsockopts reaching us here always apply to the
2574 * CLC socket
2575 */
Wen Guc0bf3d82022-01-22 17:43:09 +08002576 mutex_lock(&smc->clcsock_release_lock);
2577 if (!smc->clcsock) {
2578 mutex_unlock(&smc->clcsock_release_lock);
2579 return -EBADF;
2580 }
Christoph Hellwiga44d9e72020-07-17 08:23:31 +02002581 if (unlikely(!smc->clcsock->ops->setsockopt))
2582 rc = -EOPNOTSUPP;
2583 else
2584 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
2585 optval, optlen);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002586 if (smc->clcsock->sk->sk_err) {
2587 sk->sk_err = smc->clcsock->sk->sk_err;
Alexander Aringe3ae2362021-06-27 18:48:21 -04002588 sk_error_report(sk);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002589 }
Wen Guc0bf3d82022-01-22 17:43:09 +08002590 mutex_unlock(&smc->clcsock_release_lock);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002591
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002592 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00002593 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02002594 if (copy_from_sockptr(&val, optval, sizeof(int)))
Ursula Braunac0107e2018-07-18 15:22:50 +02002595 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002596
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002597 lock_sock(sk);
Ursula Braun86434742019-12-12 22:35:58 +01002598 if (rc || smc->use_fallback)
2599 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002600 switch (optname) {
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002601 case TCP_FASTOPEN:
2602 case TCP_FASTOPEN_CONNECT:
2603 case TCP_FASTOPEN_KEY:
2604 case TCP_FASTOPEN_NO_COOKIE:
2605 /* option not supported by SMC */
Ursula Braun8204df72019-11-15 12:39:30 +01002606 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Wen Guc0bf3d82022-01-22 17:43:09 +08002607 rc = smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002608 } else {
Ursula Braun86434742019-12-12 22:35:58 +01002609 rc = -EINVAL;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002610 }
2611 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002612 case TCP_NODELAY:
Ursula Braunf9cedf12019-08-02 10:16:38 +02002613 if (sk->sk_state != SMC_INIT &&
2614 sk->sk_state != SMC_LISTEN &&
2615 sk->sk_state != SMC_CLOSED) {
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002616 if (val) {
Guvenc Gulce194730a2021-06-16 16:52:58 +02002617 SMC_STAT_INC(smc, ndly_cnt);
Karsten Graul22ef4732020-09-10 18:48:29 +02002618 mod_delayed_work(smc->conn.lgr->tx_wq,
2619 &smc->conn.tx_work, 0);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002620 }
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002621 }
2622 break;
2623 case TCP_CORK:
Ursula Braunf9cedf12019-08-02 10:16:38 +02002624 if (sk->sk_state != SMC_INIT &&
2625 sk->sk_state != SMC_LISTEN &&
2626 sk->sk_state != SMC_CLOSED) {
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002627 if (!val) {
Guvenc Gulce194730a2021-06-16 16:52:58 +02002628 SMC_STAT_INC(smc, cork_cnt);
Karsten Graul22ef4732020-09-10 18:48:29 +02002629 mod_delayed_work(smc->conn.lgr->tx_wq,
2630 &smc->conn.tx_work, 0);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002631 }
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002632 }
2633 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02002634 case TCP_DEFER_ACCEPT:
2635 smc->sockopt_defer_accept = val;
2636 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002637 default:
2638 break;
2639 }
Ursula Braun86434742019-12-12 22:35:58 +01002640out:
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002641 release_sock(sk);
2642
2643 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01002644}
2645
2646static int smc_getsockopt(struct socket *sock, int level, int optname,
2647 char __user *optval, int __user *optlen)
2648{
2649 struct smc_sock *smc;
Wen Guc0bf3d82022-01-22 17:43:09 +08002650 int rc;
Ursula Braunac713872017-01-09 16:55:13 +01002651
2652 smc = smc_sk(sock->sk);
Wen Guc0bf3d82022-01-22 17:43:09 +08002653 mutex_lock(&smc->clcsock_release_lock);
2654 if (!smc->clcsock) {
2655 mutex_unlock(&smc->clcsock_release_lock);
2656 return -EBADF;
2657 }
Ursula Braunac713872017-01-09 16:55:13 +01002658 /* socket options apply to the CLC socket */
Wen Guc0bf3d82022-01-22 17:43:09 +08002659 if (unlikely(!smc->clcsock->ops->getsockopt)) {
2660 mutex_unlock(&smc->clcsock_release_lock);
Christoph Hellwiga44d9e72020-07-17 08:23:31 +02002661 return -EOPNOTSUPP;
Wen Guc0bf3d82022-01-22 17:43:09 +08002662 }
2663 rc = smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
2664 optval, optlen);
2665 mutex_unlock(&smc->clcsock_release_lock);
2666 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01002667}
2668
2669static int smc_ioctl(struct socket *sock, unsigned int cmd,
2670 unsigned long arg)
2671{
Stefan Rasplde8474e2018-05-23 16:38:11 +02002672 union smc_host_cursor cons, urg;
2673 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01002674 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02002675 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01002676
2677 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02002678 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02002679 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02002680 if (smc->use_fallback) {
2681 if (!smc->clcsock) {
2682 release_sock(&smc->sk);
2683 return -EBADF;
2684 }
2685 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
2686 release_sock(&smc->sk);
2687 return answ;
2688 }
Ursula Braun9b67e262018-05-02 16:56:46 +02002689 switch (cmd) {
2690 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02002691 if (smc->sk.sk_state == SMC_LISTEN) {
2692 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002693 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002694 }
Ursula Braun2351abe2018-05-23 16:38:09 +02002695 if (smc->sk.sk_state == SMC_INIT ||
2696 smc->sk.sk_state == SMC_CLOSED)
2697 answ = 0;
2698 else
2699 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02002700 break;
2701 case SIOCOUTQ:
2702 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02002703 if (smc->sk.sk_state == SMC_LISTEN) {
2704 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002705 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002706 }
Ursula Braun2351abe2018-05-23 16:38:09 +02002707 if (smc->sk.sk_state == SMC_INIT ||
2708 smc->sk.sk_state == SMC_CLOSED)
2709 answ = 0;
2710 else
2711 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02002712 atomic_read(&smc->conn.sndbuf_space);
2713 break;
2714 case SIOCOUTQNSD:
2715 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02002716 if (smc->sk.sk_state == SMC_LISTEN) {
2717 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002718 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002719 }
Ursula Braun2351abe2018-05-23 16:38:09 +02002720 if (smc->sk.sk_state == SMC_INIT ||
2721 smc->sk.sk_state == SMC_CLOSED)
2722 answ = 0;
2723 else
2724 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02002725 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02002726 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02002727 if (smc->sk.sk_state == SMC_LISTEN) {
2728 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02002729 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002730 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02002731 if (smc->sk.sk_state == SMC_INIT ||
2732 smc->sk.sk_state == SMC_CLOSED) {
2733 answ = 0;
2734 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02002735 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
2736 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02002737 answ = smc_curs_diff(conn->rmb_desc->len,
2738 &cons, &urg) == 1;
2739 }
2740 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02002741 default:
Ursula Braun1992d992018-07-16 13:56:52 +02002742 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002743 return -ENOIOCTLCMD;
2744 }
Ursula Braun1992d992018-07-16 13:56:52 +02002745 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002746
2747 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01002748}
2749
2750static ssize_t smc_sendpage(struct socket *sock, struct page *page,
2751 int offset, size_t size, int flags)
2752{
2753 struct sock *sk = sock->sk;
2754 struct smc_sock *smc;
2755 int rc = -EPIPE;
2756
2757 smc = smc_sk(sk);
2758 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02002759 if (sk->sk_state != SMC_ACTIVE) {
2760 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01002761 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02002762 }
2763 release_sock(sk);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002764 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01002765 rc = kernel_sendpage(smc->clcsock, page, offset,
2766 size, flags);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002767 } else {
Guvenc Gulce194730a2021-06-16 16:52:58 +02002768 SMC_STAT_INC(smc, sendpage_cnt);
Ursula Braunac713872017-01-09 16:55:13 +01002769 rc = sock_no_sendpage(sock, page, offset, size, flags);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002770 }
Ursula Braunac713872017-01-09 16:55:13 +01002771
2772out:
Ursula Braunac713872017-01-09 16:55:13 +01002773 return rc;
2774}
2775
Stefan Raspl9014db22018-05-03 18:12:39 +02002776/* Map the affected portions of the rmbe into an spd, note the number of bytes
2777 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
2778 * updates till whenever a respective page has been fully processed.
2779 * Note that subsequent recv() calls have to wait till all splice() processing
2780 * completed.
2781 */
Ursula Braunac713872017-01-09 16:55:13 +01002782static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
2783 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02002784 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01002785{
2786 struct sock *sk = sock->sk;
2787 struct smc_sock *smc;
2788 int rc = -ENOTCONN;
2789
2790 smc = smc_sk(sk);
2791 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01002792 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2793 /* socket was connected before, no more data to read */
2794 rc = 0;
2795 goto out;
2796 }
Stefan Raspl9014db22018-05-03 18:12:39 +02002797 if (sk->sk_state == SMC_INIT ||
2798 sk->sk_state == SMC_LISTEN ||
2799 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01002800 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02002801
2802 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2803 rc = 0;
2804 goto out;
2805 }
2806
Ursula Braunac713872017-01-09 16:55:13 +01002807 if (smc->use_fallback) {
2808 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
2809 pipe, len, flags);
2810 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02002811 if (*ppos) {
2812 rc = -ESPIPE;
2813 goto out;
2814 }
2815 if (flags & SPLICE_F_NONBLOCK)
2816 flags = MSG_DONTWAIT;
2817 else
2818 flags = 0;
Guvenc Gulce194730a2021-06-16 16:52:58 +02002819 SMC_STAT_INC(smc, splice_cnt);
Stefan Raspl9014db22018-05-03 18:12:39 +02002820 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01002821 }
2822out:
2823 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02002824
Ursula Braunac713872017-01-09 16:55:13 +01002825 return rc;
2826}
2827
2828/* must look like tcp */
2829static const struct proto_ops smc_sock_ops = {
2830 .family = PF_SMC,
2831 .owner = THIS_MODULE,
2832 .release = smc_release,
2833 .bind = smc_bind,
2834 .connect = smc_connect,
2835 .socketpair = sock_no_socketpair,
2836 .accept = smc_accept,
2837 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07002838 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01002839 .ioctl = smc_ioctl,
2840 .listen = smc_listen,
2841 .shutdown = smc_shutdown,
2842 .setsockopt = smc_setsockopt,
2843 .getsockopt = smc_getsockopt,
2844 .sendmsg = smc_sendmsg,
2845 .recvmsg = smc_recvmsg,
2846 .mmap = sock_no_mmap,
2847 .sendpage = smc_sendpage,
2848 .splice_read = smc_splice_read,
2849};
2850
Tony Lud7cd4212021-12-28 21:44:36 +08002851static int __smc_create(struct net *net, struct socket *sock, int protocol,
2852 int kern, struct socket *clcsock)
Ursula Braunac713872017-01-09 16:55:13 +01002853{
Karsten Graulaaa4d332018-03-16 15:06:41 +01002854 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01002855 struct smc_sock *smc;
2856 struct sock *sk;
2857 int rc;
2858
2859 rc = -ESOCKTNOSUPPORT;
2860 if (sock->type != SOCK_STREAM)
2861 goto out;
2862
2863 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002864 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01002865 goto out;
2866
2867 rc = -ENOBUFS;
2868 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002869 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01002870 if (!sk)
2871 goto out;
2872
2873 /* create internal TCP socket for CLC handshake and fallback */
2874 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01002875 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02002876 smc->fallback_rsn = 0;
Tony Lud7cd4212021-12-28 21:44:36 +08002877
2878 rc = 0;
2879 if (!clcsock) {
2880 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
2881 &smc->clcsock);
2882 if (rc) {
2883 sk_common_release(sk);
2884 goto out;
2885 }
2886 } else {
2887 smc->clcsock = clcsock;
Davide Carattia5dcb732018-02-28 12:44:09 +01002888 }
Tony Lud7cd4212021-12-28 21:44:36 +08002889
Ursula Brauncd6851f2017-01-09 16:55:18 +01002890 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
2891 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01002892
2893out:
2894 return rc;
2895}
2896
Tony Lud7cd4212021-12-28 21:44:36 +08002897static int smc_create(struct net *net, struct socket *sock, int protocol,
2898 int kern)
2899{
2900 return __smc_create(net, sock, protocol, kern, NULL);
2901}
2902
Ursula Braunac713872017-01-09 16:55:13 +01002903static const struct net_proto_family smc_sock_family_ops = {
2904 .family = PF_SMC,
2905 .owner = THIS_MODULE,
2906 .create = smc_create,
2907};
2908
Tony Lud7cd4212021-12-28 21:44:36 +08002909static int smc_ulp_init(struct sock *sk)
2910{
2911 struct socket *tcp = sk->sk_socket;
2912 struct net *net = sock_net(sk);
2913 struct socket *smcsock;
2914 int protocol, ret;
2915
2916 /* only TCP can be replaced */
2917 if (tcp->type != SOCK_STREAM || sk->sk_protocol != IPPROTO_TCP ||
2918 (sk->sk_family != AF_INET && sk->sk_family != AF_INET6))
2919 return -ESOCKTNOSUPPORT;
2920 /* don't handle wq now */
2921 if (tcp->state != SS_UNCONNECTED || !tcp->file || tcp->wq.fasync_list)
2922 return -ENOTCONN;
2923
2924 if (sk->sk_family == AF_INET)
2925 protocol = SMCPROTO_SMC;
2926 else
2927 protocol = SMCPROTO_SMC6;
2928
2929 smcsock = sock_alloc();
2930 if (!smcsock)
2931 return -ENFILE;
2932
2933 smcsock->type = SOCK_STREAM;
2934 __module_get(THIS_MODULE); /* tried in __tcp_ulp_find_autoload */
2935 ret = __smc_create(net, smcsock, protocol, 1, tcp);
2936 if (ret) {
2937 sock_release(smcsock); /* module_put() which ops won't be NULL */
2938 return ret;
2939 }
2940
2941 /* replace tcp socket to smc */
2942 smcsock->file = tcp->file;
2943 smcsock->file->private_data = smcsock;
2944 smcsock->file->f_inode = SOCK_INODE(smcsock); /* replace inode when sock_close */
2945 smcsock->file->f_path.dentry->d_inode = SOCK_INODE(smcsock); /* dput() in __fput */
2946 tcp->file = NULL;
2947
2948 return ret;
2949}
2950
2951static void smc_ulp_clone(const struct request_sock *req, struct sock *newsk,
2952 const gfp_t priority)
2953{
2954 struct inet_connection_sock *icsk = inet_csk(newsk);
2955
2956 /* don't inherit ulp ops to child when listen */
2957 icsk->icsk_ulp_ops = NULL;
2958}
2959
2960static struct tcp_ulp_ops smc_ulp_ops __read_mostly = {
2961 .name = "smc",
2962 .owner = THIS_MODULE,
2963 .init = smc_ulp_init,
2964 .clone = smc_ulp_clone,
2965};
2966
Hans Wippel64e28b52019-02-21 13:01:02 +01002967unsigned int smc_net_id;
2968
2969static __net_init int smc_net_init(struct net *net)
2970{
2971 return smc_pnet_net_init(net);
2972}
2973
2974static void __net_exit smc_net_exit(struct net *net)
2975{
2976 smc_pnet_net_exit(net);
2977}
2978
Guvenc Gulce194730a2021-06-16 16:52:58 +02002979static __net_init int smc_net_stat_init(struct net *net)
2980{
2981 return smc_stats_init(net);
2982}
2983
2984static void __net_exit smc_net_stat_exit(struct net *net)
2985{
2986 smc_stats_exit(net);
2987}
2988
Hans Wippel64e28b52019-02-21 13:01:02 +01002989static struct pernet_operations smc_net_ops = {
2990 .init = smc_net_init,
2991 .exit = smc_net_exit,
2992 .id = &smc_net_id,
2993 .size = sizeof(struct smc_net),
2994};
2995
Guvenc Gulce194730a2021-06-16 16:52:58 +02002996static struct pernet_operations smc_net_stat_ops = {
2997 .init = smc_net_stat_init,
2998 .exit = smc_net_stat_exit,
2999};
3000
Ursula Braunac713872017-01-09 16:55:13 +01003001static int __init smc_init(void)
3002{
3003 int rc;
3004
Hans Wippel64e28b52019-02-21 13:01:02 +01003005 rc = register_pernet_subsys(&smc_net_ops);
3006 if (rc)
3007 return rc;
3008
Guvenc Gulce194730a2021-06-16 16:52:58 +02003009 rc = register_pernet_subsys(&smc_net_stat_ops);
3010 if (rc)
3011 return rc;
3012
Ursula Braun201091e2020-09-26 12:44:24 +02003013 smc_ism_init();
Ursula Braunb81a5eb2020-09-26 12:44:31 +02003014 smc_clc_init();
Ursula Braun201091e2020-09-26 12:44:24 +02003015
Guvenc Gulcee8372d92020-12-01 20:20:43 +01003016 rc = smc_nl_init();
Thomas Richter6812baa2017-01-09 16:55:15 +01003017 if (rc)
YueHaibing8c33bf12019-06-26 17:47:50 +02003018 goto out_pernet_subsys;
Thomas Richter6812baa2017-01-09 16:55:15 +01003019
Guvenc Gulcee8372d92020-12-01 20:20:43 +01003020 rc = smc_pnet_init();
3021 if (rc)
3022 goto out_nl;
3023
Karsten Graul22ef4732020-09-10 18:48:29 +02003024 rc = -ENOMEM;
3025 smc_hs_wq = alloc_workqueue("smc_hs_wq", 0, 0);
3026 if (!smc_hs_wq)
3027 goto out_pnet;
3028
3029 smc_close_wq = alloc_workqueue("smc_close_wq", 0, 0);
3030 if (!smc_close_wq)
3031 goto out_alloc_hs_wq;
3032
Ursula Braun6dabd402019-11-16 17:47:29 +01003033 rc = smc_core_init();
3034 if (rc) {
3035 pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
Guvenc Gulce194730a2021-06-16 16:52:58 +02003036 goto out_alloc_wqs;
Ursula Braun6dabd402019-11-16 17:47:29 +01003037 }
3038
Ursula Braun9bf9abe2017-01-09 16:55:21 +01003039 rc = smc_llc_init();
3040 if (rc) {
3041 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01003042 goto out_core;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01003043 }
3044
Ursula Braun5f083182017-01-09 16:55:22 +01003045 rc = smc_cdc_init();
3046 if (rc) {
3047 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01003048 goto out_core;
Ursula Braun5f083182017-01-09 16:55:22 +01003049 }
3050
Ursula Braunac713872017-01-09 16:55:13 +01003051 rc = proto_register(&smc_proto, 1);
3052 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01003053 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01003054 goto out_core;
Ursula Braunac713872017-01-09 16:55:13 +01003055 }
3056
Karsten Graulaaa4d332018-03-16 15:06:41 +01003057 rc = proto_register(&smc_proto6, 1);
3058 if (rc) {
3059 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
3060 goto out_proto;
3061 }
3062
Ursula Braunac713872017-01-09 16:55:13 +01003063 rc = sock_register(&smc_sock_family_ops);
3064 if (rc) {
3065 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01003066 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01003067 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01003068 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01003069 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01003070
Ursula Brauna4cf0442017-01-09 16:55:14 +01003071 rc = smc_ib_register_client();
3072 if (rc) {
3073 pr_err("%s: ib_register fails with %d\n", __func__, rc);
3074 goto out_sock;
3075 }
3076
Tony Lud7cd4212021-12-28 21:44:36 +08003077 rc = tcp_register_ulp(&smc_ulp_ops);
3078 if (rc) {
3079 pr_err("%s: tcp_ulp_register fails with %d\n", __func__, rc);
3080 goto out_sock;
3081 }
3082
Ursula Braunc5c1cc92017-10-25 11:01:46 +02003083 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01003084 return 0;
3085
Ursula Brauna4cf0442017-01-09 16:55:14 +01003086out_sock:
3087 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01003088out_proto6:
3089 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01003090out_proto:
3091 proto_unregister(&smc_proto);
Ursula Braun6dabd402019-11-16 17:47:29 +01003092out_core:
3093 smc_core_exit();
Karsten Graul22ef4732020-09-10 18:48:29 +02003094out_alloc_wqs:
3095 destroy_workqueue(smc_close_wq);
3096out_alloc_hs_wq:
3097 destroy_workqueue(smc_hs_wq);
Thomas Richter6812baa2017-01-09 16:55:15 +01003098out_pnet:
3099 smc_pnet_exit();
Guvenc Gulcee8372d92020-12-01 20:20:43 +01003100out_nl:
3101 smc_nl_exit();
YueHaibing8c33bf12019-06-26 17:47:50 +02003102out_pernet_subsys:
3103 unregister_pernet_subsys(&smc_net_ops);
3104
Ursula Braunac713872017-01-09 16:55:13 +01003105 return rc;
3106}
3107
3108static void __exit smc_exit(void)
3109{
Ursula Braunc5c1cc92017-10-25 11:01:46 +02003110 static_branch_disable(&tcp_have_smc);
Tony Lud7cd4212021-12-28 21:44:36 +08003111 tcp_unregister_ulp(&smc_ulp_ops);
Ursula Braunac713872017-01-09 16:55:13 +01003112 sock_unregister(PF_SMC);
Ursula Braun6dabd402019-11-16 17:47:29 +01003113 smc_core_exit();
3114 smc_ib_unregister_client();
Karsten Graul22ef4732020-09-10 18:48:29 +02003115 destroy_workqueue(smc_close_wq);
3116 destroy_workqueue(smc_hs_wq);
Karsten Graulaaa4d332018-03-16 15:06:41 +01003117 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01003118 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01003119 smc_pnet_exit();
Guvenc Gulcee8372d92020-12-01 20:20:43 +01003120 smc_nl_exit();
Karsten Graulfa086662021-09-14 10:35:05 +02003121 smc_clc_exit();
Guvenc Gulce194730a2021-06-16 16:52:58 +02003122 unregister_pernet_subsys(&smc_net_stat_ops);
Hans Wippel64e28b52019-02-21 13:01:02 +01003123 unregister_pernet_subsys(&smc_net_ops);
Ursula Braun4ead9c92019-11-16 17:47:31 +01003124 rcu_barrier();
Ursula Braunac713872017-01-09 16:55:13 +01003125}
3126
3127module_init(smc_init);
3128module_exit(smc_exit);
3129
3130MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
3131MODULE_DESCRIPTION("smc socket address family");
3132MODULE_LICENSE("GPL");
3133MODULE_ALIAS_NETPROTO(PF_SMC);
Tony Lud7cd4212021-12-28 21:44:36 +08003134MODULE_ALIAS_TCP_ULP("smc");