blob: ff5cd0c30741c0b562e66ce49a60bbc8d260f3b5 [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);
197 flush_work(&smc->connect_work);
198
199 if (sk->sk_state == SMC_LISTEN)
200 /* smc_close_non_accepted() is called and acquires
201 * sock lock for child sockets again
202 */
203 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
204 else
205 lock_sock(sk);
206
207 rc = __smc_release(smc);
208
Ursula Braunac713872017-01-09 16:55:13 +0100209 /* detach socket */
210 sock_orphan(sk);
211 sock->sk = NULL;
212 release_sock(sk);
213
Ursula Braun81cf4f42019-10-21 16:13:15 +0200214 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +0100215 sock_put(sk); /* final sock_put */
Ursula Braunac713872017-01-09 16:55:13 +0100216out:
Ursula Braunb38d7322017-01-09 16:55:25 +0100217 return rc;
Ursula Braunac713872017-01-09 16:55:13 +0100218}
219
220static void smc_destruct(struct sock *sk)
221{
222 if (sk->sk_state != SMC_CLOSED)
223 return;
224 if (!sock_flag(sk, SOCK_DEAD))
225 return;
226
227 sk_refcnt_debug_dec(sk);
228}
229
Karsten Graulaaa4d332018-03-16 15:06:41 +0100230static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
231 int protocol)
Ursula Braunac713872017-01-09 16:55:13 +0100232{
233 struct smc_sock *smc;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100234 struct proto *prot;
Ursula Braunac713872017-01-09 16:55:13 +0100235 struct sock *sk;
236
Karsten Graulaaa4d332018-03-16 15:06:41 +0100237 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
238 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
Ursula Braunac713872017-01-09 16:55:13 +0100239 if (!sk)
240 return NULL;
241
242 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
243 sk->sk_state = SMC_INIT;
244 sk->sk_destruct = smc_destruct;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100245 sk->sk_protocol = protocol;
Ursula Braunac713872017-01-09 16:55:13 +0100246 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100247 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200248 INIT_WORK(&smc->connect_work, smc_connect_work);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700249 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
Ursula Brauna046d572017-01-09 16:55:16 +0100250 INIT_LIST_HEAD(&smc->accept_q);
251 spin_lock_init(&smc->accept_q_lock);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700252 spin_lock_init(&smc->conn.send_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100253 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100254 sk_refcnt_debug_inc(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800255 mutex_init(&smc->clcsock_release_lock);
Ursula Braunac713872017-01-09 16:55:13 +0100256
257 return sk;
258}
259
260static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
261 int addr_len)
262{
263 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
264 struct sock *sk = sock->sk;
265 struct smc_sock *smc;
266 int rc;
267
268 smc = smc_sk(sk);
269
270 /* replicate tests from inet_bind(), to be safe wrt. future changes */
271 rc = -EINVAL;
272 if (addr_len < sizeof(struct sockaddr_in))
273 goto out;
274
275 rc = -EAFNOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100276 if (addr->sin_family != AF_INET &&
277 addr->sin_family != AF_INET6 &&
278 addr->sin_family != AF_UNSPEC)
279 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100280 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
Karsten Graulaaa4d332018-03-16 15:06:41 +0100281 if (addr->sin_family == AF_UNSPEC &&
282 addr->sin_addr.s_addr != htonl(INADDR_ANY))
Ursula Braunac713872017-01-09 16:55:13 +0100283 goto out;
284
285 lock_sock(sk);
286
287 /* Check if socket is already active */
288 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +0200289 if (sk->sk_state != SMC_INIT || smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +0100290 goto out_rel;
291
292 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
293 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
294
295out_rel:
296 release_sock(sk);
297out:
298 return rc;
299}
300
301static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
302 unsigned long mask)
303{
304 /* options we don't get control via setsockopt for */
305 nsk->sk_type = osk->sk_type;
306 nsk->sk_sndbuf = osk->sk_sndbuf;
307 nsk->sk_rcvbuf = osk->sk_rcvbuf;
308 nsk->sk_sndtimeo = osk->sk_sndtimeo;
309 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
310 nsk->sk_mark = osk->sk_mark;
311 nsk->sk_priority = osk->sk_priority;
312 nsk->sk_rcvlowat = osk->sk_rcvlowat;
313 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
314 nsk->sk_err = osk->sk_err;
315
316 nsk->sk_flags &= ~mask;
317 nsk->sk_flags |= osk->sk_flags & mask;
318}
319
320#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
321 (1UL << SOCK_KEEPOPEN) | \
322 (1UL << SOCK_LINGER) | \
323 (1UL << SOCK_BROADCAST) | \
324 (1UL << SOCK_TIMESTAMP) | \
325 (1UL << SOCK_DBG) | \
326 (1UL << SOCK_RCVTSTAMP) | \
327 (1UL << SOCK_RCVTSTAMPNS) | \
328 (1UL << SOCK_LOCALROUTE) | \
329 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
330 (1UL << SOCK_RXQ_OVFL) | \
331 (1UL << SOCK_WIFI_STATUS) | \
332 (1UL << SOCK_NOFCS) | \
Deepa Dinamani97184752019-02-02 07:34:51 -0800333 (1UL << SOCK_FILTER_LOCKED) | \
334 (1UL << SOCK_TSTAMP_NEW))
Ursula Braunac713872017-01-09 16:55:13 +0100335/* copy only relevant settings and flags of SOL_SOCKET level from smc to
336 * clc socket (since smc is not called for these options from net/core)
337 */
338static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
339{
340 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
341}
342
343#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
344 (1UL << SOCK_KEEPOPEN) | \
345 (1UL << SOCK_LINGER) | \
346 (1UL << SOCK_DBG))
347/* copy only settings and flags relevant for smc from clc to smc socket */
348static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
349{
350 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
351}
352
Karsten Graulb9247542020-04-29 17:10:41 +0200353/* register the new rmb on all links */
Karsten Graul7562a132020-05-01 12:48:01 +0200354static int smcr_lgr_reg_rmbs(struct smc_link *link,
Karsten Graulb9247542020-04-29 17:10:41 +0200355 struct smc_buf_desc *rmb_desc)
356{
Karsten Graul7562a132020-05-01 12:48:01 +0200357 struct smc_link_group *lgr = link->lgr;
358 int i, rc = 0;
Karsten Graulb9247542020-04-29 17:10:41 +0200359
Karsten Grauld5500662020-05-01 12:48:05 +0200360 rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY);
361 if (rc)
362 return rc;
363 /* protect against parallel smc_llc_cli_rkey_exchange() and
364 * parallel smcr_link_reg_rmb()
365 */
366 mutex_lock(&lgr->llc_conf_mutex);
Karsten Graulb9247542020-04-29 17:10:41 +0200367 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
Karsten Graul741a49a2020-07-18 15:06:16 +0200368 if (!smc_link_active(&lgr->lnk[i]))
Karsten Graulb9247542020-04-29 17:10:41 +0200369 continue;
Karsten Graul7562a132020-05-01 12:48:01 +0200370 rc = smcr_link_reg_rmb(&lgr->lnk[i], rmb_desc);
Karsten Graulb9247542020-04-29 17:10:41 +0200371 if (rc)
Karsten Graul7562a132020-05-01 12:48:01 +0200372 goto out;
Karsten Graulb9247542020-04-29 17:10:41 +0200373 }
Karsten Graul7562a132020-05-01 12:48:01 +0200374
375 /* exchange confirm_rkey msg with peer */
376 rc = smc_llc_do_confirm_rkey(link, rmb_desc);
377 if (rc) {
378 rc = -EFAULT;
379 goto out;
380 }
381 rmb_desc->is_conf_rkey = true;
382out:
Karsten Grauld5500662020-05-01 12:48:05 +0200383 mutex_unlock(&lgr->llc_conf_mutex);
384 smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
Karsten Graul7562a132020-05-01 12:48:01 +0200385 return rc;
Karsten Graulb9247542020-04-29 17:10:41 +0200386}
387
388static int smcr_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100389{
Karsten Graul387707f2020-04-29 17:10:40 +0200390 struct smc_link *link = smc->conn.lnk;
Karsten Graul0fb0b022020-04-30 15:55:43 +0200391 struct smc_llc_qentry *qentry;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100392 int rc;
393
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100394 /* receive CONFIRM LINK request from server over RoCE fabric */
Karsten Graul0fb0b022020-04-30 15:55:43 +0200395 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
396 SMC_LLC_CONFIRM_LINK);
397 if (!qentry) {
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100398 struct smc_clc_msg_decline dclc;
399
400 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100401 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100402 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100403 }
Karsten Graul649758f2020-05-04 14:18:48 +0200404 smc_llc_save_peer_uid(qentry);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200405 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_REQ);
406 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
407 if (rc)
Karsten Graul75d320d2018-03-01 13:51:31 +0100408 return SMC_CLC_DECL_RMBE_EC;
409
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100410 rc = smc_ib_modify_qp_rts(link);
411 if (rc)
Karsten Graul603cc142018-07-25 16:35:32 +0200412 return SMC_CLC_DECL_ERR_RDYLNK;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100413
414 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200415
Karsten Graul7562a132020-05-01 12:48:01 +0200416 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +0200417 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200418
Karsten Graul0fb0b022020-04-30 15:55:43 +0200419 /* confirm_rkey is implicit on 1st contact */
420 smc->conn.rmb_desc->is_conf_rkey = true;
421
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100422 /* send CONFIRM LINK response over RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200423 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100424 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200425 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100426
Karsten Graul0fb0b022020-04-30 15:55:43 +0200427 smc_llc_link_active(link);
Karsten Graul0a99be42020-05-05 15:01:20 +0200428 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200429
430 /* optional 2nd link, receive ADD LINK request from server */
431 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
432 SMC_LLC_ADD_LINK);
433 if (!qentry) {
Karsten Graul52bedf32018-03-01 13:51:32 +0100434 struct smc_clc_msg_decline dclc;
435
436 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100437 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200438 if (rc == -EAGAIN)
439 rc = 0; /* no DECLINE received, go with one link */
440 return rc;
Karsten Graul52bedf32018-03-01 13:51:32 +0100441 }
Karsten Graul0fb0b022020-04-30 15:55:43 +0200442 smc_llc_flow_qentry_clr(&link->lgr->llc_flow_lcl);
Karsten Graulb1570a82020-05-03 14:38:42 +0200443 smc_llc_cli_add_link(link, qentry);
Karsten Graul75d320d2018-03-01 13:51:31 +0100444 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100445}
446
Karsten Graule5c47442021-10-16 11:37:45 +0200447static bool smc_isascii(char *hostname)
448{
449 int i;
450
451 for (i = 0; i < SMC_MAX_HOSTNAME_LEN; i++)
452 if (!isascii(hostname[i]))
453 return false;
454 return true;
455}
456
457static void smc_conn_save_peer_info_fce(struct smc_sock *smc,
458 struct smc_clc_msg_accept_confirm *clc)
459{
460 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
461 (struct smc_clc_msg_accept_confirm_v2 *)clc;
462 struct smc_clc_first_contact_ext *fce;
463 int clc_v2_len;
464
465 if (clc->hdr.version == SMC_V1 ||
466 !(clc->hdr.typev2 & SMC_FIRST_CONTACT_MASK))
467 return;
468
469 if (smc->conn.lgr->is_smcd) {
470 memcpy(smc->conn.lgr->negotiated_eid, clc_v2->d1.eid,
471 SMC_MAX_EID_LEN);
472 clc_v2_len = offsetofend(struct smc_clc_msg_accept_confirm_v2,
473 d1);
474 } else {
475 memcpy(smc->conn.lgr->negotiated_eid, clc_v2->r1.eid,
476 SMC_MAX_EID_LEN);
477 clc_v2_len = offsetofend(struct smc_clc_msg_accept_confirm_v2,
478 r1);
479 }
480 fce = (struct smc_clc_first_contact_ext *)(((u8 *)clc_v2) + clc_v2_len);
481 smc->conn.lgr->peer_os = fce->os_type;
482 smc->conn.lgr->peer_smc_release = fce->release;
483 if (smc_isascii(fce->hostname))
484 memcpy(smc->conn.lgr->peer_hostname, fce->hostname,
485 SMC_MAX_HOSTNAME_LEN);
486}
487
Hans Wippel41349842018-06-28 19:05:11 +0200488static void smcr_conn_save_peer_info(struct smc_sock *smc,
489 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100490{
Ursula Braun3d9725a2020-09-10 18:48:23 +0200491 int bufsize = smc_uncompress_bufsize(clc->r0.rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200492
Ursula Braun3d9725a2020-09-10 18:48:23 +0200493 smc->conn.peer_rmbe_idx = clc->r0.rmbe_idx;
494 smc->conn.local_tx_ctrl.token = ntohl(clc->r0.rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200495 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100496 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200497 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100498}
499
Hans Wippel41349842018-06-28 19:05:11 +0200500static void smcd_conn_save_peer_info(struct smc_sock *smc,
501 struct smc_clc_msg_accept_confirm *clc)
502{
Ursula Braun3d9725a2020-09-10 18:48:23 +0200503 int bufsize = smc_uncompress_bufsize(clc->d0.dmbe_size);
Hans Wippel41349842018-06-28 19:05:11 +0200504
Ursula Braun3d9725a2020-09-10 18:48:23 +0200505 smc->conn.peer_rmbe_idx = clc->d0.dmbe_idx;
506 smc->conn.peer_token = clc->d0.token;
Hans Wippel41349842018-06-28 19:05:11 +0200507 /* msg header takes up space in the buffer */
508 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
509 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
510 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
511}
512
513static void smc_conn_save_peer_info(struct smc_sock *smc,
514 struct smc_clc_msg_accept_confirm *clc)
515{
516 if (smc->conn.lgr->is_smcd)
517 smcd_conn_save_peer_info(smc, clc);
518 else
519 smcr_conn_save_peer_info(smc, clc);
Karsten Graule5c47442021-10-16 11:37:45 +0200520 smc_conn_save_peer_info_fce(smc, clc);
Hans Wippel41349842018-06-28 19:05:11 +0200521}
522
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100523static void smc_link_save_peer_info(struct smc_link *link,
Karsten Graule5c47442021-10-16 11:37:45 +0200524 struct smc_clc_msg_accept_confirm *clc,
525 struct smc_init_info *ini)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100526{
Ursula Braun3d9725a2020-09-10 18:48:23 +0200527 link->peer_qpn = ntoh24(clc->r0.qpn);
Karsten Graule5c47442021-10-16 11:37:45 +0200528 memcpy(link->peer_gid, ini->peer_gid, SMC_GID_SIZE);
529 memcpy(link->peer_mac, ini->peer_mac, sizeof(link->peer_mac));
Ursula Braun3d9725a2020-09-10 18:48:23 +0200530 link->peer_psn = ntoh24(clc->r0.psn);
531 link->peer_mtu = clc->r0.qp_mtu;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100532}
533
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +0200534static void smc_stat_inc_fback_rsn_cnt(struct smc_sock *smc,
535 struct smc_stats_fback *fback_arr)
536{
537 int cnt;
538
539 for (cnt = 0; cnt < SMC_MAX_FBACK_RSN_CNT; cnt++) {
540 if (fback_arr[cnt].fback_code == smc->fallback_rsn) {
541 fback_arr[cnt].count++;
542 break;
543 }
544 if (!fback_arr[cnt].fback_code) {
545 fback_arr[cnt].fback_code = smc->fallback_rsn;
546 fback_arr[cnt].count++;
547 break;
548 }
549 }
550}
551
552static void smc_stat_fallback(struct smc_sock *smc)
553{
Guvenc Gulce194730a2021-06-16 16:52:58 +0200554 struct net *net = sock_net(&smc->sk);
555
556 mutex_lock(&net->smc.mutex_fback_rsn);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +0200557 if (smc->listen_smc) {
Guvenc Gulce194730a2021-06-16 16:52:58 +0200558 smc_stat_inc_fback_rsn_cnt(smc, net->smc.fback_rsn->srv);
559 net->smc.fback_rsn->srv_fback_cnt++;
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +0200560 } else {
Guvenc Gulce194730a2021-06-16 16:52:58 +0200561 smc_stat_inc_fback_rsn_cnt(smc, net->smc.fback_rsn->clnt);
562 net->smc.fback_rsn->clnt_fback_cnt++;
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +0200563 }
Guvenc Gulce194730a2021-06-16 16:52:58 +0200564 mutex_unlock(&net->smc.mutex_fback_rsn);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +0200565}
566
567static void smc_switch_to_fallback(struct smc_sock *smc, int reason_code)
Ursula Braun07603b22019-04-11 11:17:32 +0200568{
569 smc->use_fallback = true;
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +0200570 smc->fallback_rsn = reason_code;
571 smc_stat_fallback(smc);
Tony Lu48262602021-11-01 15:39:12 +0800572 trace_smc_switch_to_fallback(smc, reason_code);
Ursula Braun07603b22019-04-11 11:17:32 +0200573 if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
574 smc->clcsock->file = smc->sk.sk_socket->file;
575 smc->clcsock->file->private_data = smc->clcsock;
Ursula Braun67f562e2020-02-14 08:58:59 +0100576 smc->clcsock->wq.fasync_list =
577 smc->sk.sk_socket->wq.fasync_list;
Ursula Braun07603b22019-04-11 11:17:32 +0200578 }
579}
580
Hans Wippel3b2dec22018-05-18 09:34:18 +0200581/* fall back during connect */
Karsten Graul603cc142018-07-25 16:35:32 +0200582static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200583{
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +0200584 smc_switch_to_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200585 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200586 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200587 if (smc->sk.sk_state == SMC_INIT)
588 smc->sk.sk_state = SMC_ACTIVE;
589 return 0;
590}
591
592/* decline and fall back during connect */
Ursula Braune8d726c82020-09-26 12:44:32 +0200593static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code,
594 u8 version)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200595{
Guvenc Gulce194730a2021-06-16 16:52:58 +0200596 struct net *net = sock_net(&smc->sk);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200597 int rc;
598
Ursula Braune1bbdd52018-07-05 16:15:30 +0200599 if (reason_code < 0) { /* error, fallback is not possible */
Guvenc Gulce194730a2021-06-16 16:52:58 +0200600 this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200601 if (smc->sk.sk_state == SMC_INIT)
602 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200603 return reason_code;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200604 }
Karsten Graul603cc142018-07-25 16:35:32 +0200605 if (reason_code != SMC_CLC_DECL_PEERDECL) {
Ursula Braune8d726c82020-09-26 12:44:32 +0200606 rc = smc_clc_send_decline(smc, reason_code, version);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200607 if (rc < 0) {
Guvenc Gulce194730a2021-06-16 16:52:58 +0200608 this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200609 if (smc->sk.sk_state == SMC_INIT)
610 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200611 return rc;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200612 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200613 }
Karsten Graul603cc142018-07-25 16:35:32 +0200614 return smc_connect_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200615}
616
Karsten Graul8cf3f3e2020-12-01 20:20:36 +0100617static void smc_conn_abort(struct smc_sock *smc, int local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200618{
Ursula Braun5ac54d82020-09-10 18:48:21 +0200619 if (local_first)
Ursula Braun51e3dfa2020-02-25 16:34:36 +0100620 smc_lgr_cleanup_early(&smc->conn);
621 else
622 smc_conn_free(&smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200623}
624
625/* check if there is a rdma device available for this connection. */
626/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200627static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200628{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200629 /* PNET table look up: search active ib_device and port
630 * within same PNETID that also contains the ethernet device
631 * used for the internal TCP socket
632 */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200633 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
Karsten Graul42042db2021-10-16 11:37:44 +0200634 if (!ini->check_smcrv2 && !ini->ib_dev)
635 return SMC_CLC_DECL_NOSMCRDEV;
636 if (ini->check_smcrv2 && !ini->smcrv2.ib_dev_v2)
Karsten Graul9aa68d22019-04-12 12:57:29 +0200637 return SMC_CLC_DECL_NOSMCRDEV;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200638 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200639}
640
Hans Wippel41349842018-06-28 19:05:11 +0200641/* check if there is an ISM device available for this connection. */
642/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200643static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200644{
645 /* Find ISM device with same PNETID as connecting interface */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200646 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
Ursula Braun3fc64932020-09-26 12:44:23 +0200647 if (!ini->ism_dev[0])
Karsten Graul9aa68d22019-04-12 12:57:29 +0200648 return SMC_CLC_DECL_NOSMCDDEV;
Ursula Braun8caaccf2020-09-26 12:44:25 +0200649 else
650 ini->ism_chid[0] = smc_ism_get_chid(ini->ism_dev[0]);
Hans Wippel41349842018-06-28 19:05:11 +0200651 return 0;
652}
653
Karsten Graul839d6962020-10-02 17:09:26 +0200654/* is chid unique for the ism devices that are already determined? */
655static bool smc_find_ism_v2_is_unique_chid(u16 chid, struct smc_init_info *ini,
656 int cnt)
657{
658 int i = (!ini->ism_dev[0]) ? 1 : 0;
659
660 for (; i < cnt; i++)
661 if (ini->ism_chid[i] == chid)
662 return false;
663 return true;
664}
665
Ursula Braund70bf4f2020-09-26 12:44:27 +0200666/* determine possible V2 ISM devices (either without PNETID or with PNETID plus
667 * PNETID matching net_device)
668 */
669static int smc_find_ism_v2_device_clnt(struct smc_sock *smc,
670 struct smc_init_info *ini)
671{
672 int rc = SMC_CLC_DECL_NOSMCDDEV;
673 struct smcd_dev *smcd;
674 int i = 1;
Karsten Graul839d6962020-10-02 17:09:26 +0200675 u16 chid;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200676
677 if (smcd_indicated(ini->smc_type_v1))
678 rc = 0; /* already initialized for V1 */
679 mutex_lock(&smcd_dev_list.mutex);
680 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
681 if (smcd->going_away || smcd == ini->ism_dev[0])
682 continue;
Karsten Graul839d6962020-10-02 17:09:26 +0200683 chid = smc_ism_get_chid(smcd);
684 if (!smc_find_ism_v2_is_unique_chid(chid, ini, i))
685 continue;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200686 if (!smc_pnet_is_pnetid_set(smcd->pnetid) ||
687 smc_pnet_is_ndev_pnetid(sock_net(&smc->sk), smcd->pnetid)) {
688 ini->ism_dev[i] = smcd;
Karsten Graul839d6962020-10-02 17:09:26 +0200689 ini->ism_chid[i] = chid;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200690 ini->is_smcd = true;
691 rc = 0;
692 i++;
693 if (i > SMC_MAX_ISM_DEVS)
694 break;
695 }
696 }
697 mutex_unlock(&smcd_dev_list.mutex);
698 ini->ism_offered_cnt = i - 1;
699 if (!ini->ism_dev[0] && !ini->ism_dev[1])
700 ini->smcd_version = 0;
701
702 return rc;
703}
704
Hans Wippel41349842018-06-28 19:05:11 +0200705/* Check for VLAN ID and register it on ISM device just for CLC handshake */
706static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200707 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200708{
Ursula Braun3fc64932020-09-26 12:44:23 +0200709 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev[0], ini->vlan_id))
Karsten Graul7a627252019-04-12 12:57:30 +0200710 return SMC_CLC_DECL_ISMVLANERR;
Hans Wippel41349842018-06-28 19:05:11 +0200711 return 0;
712}
713
Ursula Braund70bf4f2020-09-26 12:44:27 +0200714static int smc_find_proposal_devices(struct smc_sock *smc,
715 struct smc_init_info *ini)
716{
717 int rc = 0;
718
719 /* check if there is an ism device available */
Karsten Graul42042db2021-10-16 11:37:44 +0200720 if (!(ini->smcd_version & SMC_V1) ||
721 smc_find_ism_device(smc, ini) ||
722 smc_connect_ism_vlan_setup(smc, ini))
723 ini->smcd_version &= ~SMC_V1;
724 /* else ISM V1 is supported for this connection */
725
726 /* check if there is an rdma device available */
727 if (!(ini->smcr_version & SMC_V1) ||
728 smc_find_rdma_device(smc, ini))
729 ini->smcr_version &= ~SMC_V1;
730 /* else RDMA is supported for this connection */
731
732 ini->smc_type_v1 = smc_indicated_type(ini->smcd_version & SMC_V1,
733 ini->smcr_version & SMC_V1);
734
735 /* check if there is an ism v2 device available */
736 if (!(ini->smcd_version & SMC_V2) ||
737 !smc_ism_is_v2_capable() ||
738 smc_find_ism_v2_device_clnt(smc, ini))
739 ini->smcd_version &= ~SMC_V2;
740
741 /* check if there is an rdma v2 device available */
742 ini->check_smcrv2 = true;
743 ini->smcrv2.saddr = smc->clcsock->sk->sk_rcv_saddr;
744 if (!(ini->smcr_version & SMC_V2) ||
745 smc->clcsock->sk->sk_family != AF_INET ||
746 !smc_clc_ueid_count() ||
747 smc_find_rdma_device(smc, ini))
748 ini->smcr_version &= ~SMC_V2;
749 ini->check_smcrv2 = false;
750
751 ini->smc_type_v2 = smc_indicated_type(ini->smcd_version & SMC_V2,
752 ini->smcr_version & SMC_V2);
Ursula Braund70bf4f2020-09-26 12:44:27 +0200753
754 /* if neither ISM nor RDMA are supported, fallback */
Karsten Graul42042db2021-10-16 11:37:44 +0200755 if (ini->smc_type_v1 == SMC_TYPE_N && ini->smc_type_v2 == SMC_TYPE_N)
Ursula Braund70bf4f2020-09-26 12:44:27 +0200756 rc = SMC_CLC_DECL_NOSMCDEV;
757
758 return rc;
759}
760
Hans Wippel41349842018-06-28 19:05:11 +0200761/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
762 * used, the VLAN ID will be registered again during the connection setup.
763 */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200764static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200765 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200766{
Ursula Braund70bf4f2020-09-26 12:44:27 +0200767 if (!smcd_indicated(ini->smc_type_v1))
Hans Wippel41349842018-06-28 19:05:11 +0200768 return 0;
Ursula Braun3fc64932020-09-26 12:44:23 +0200769 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev[0], ini->vlan_id))
Hans Wippel41349842018-06-28 19:05:11 +0200770 return SMC_CLC_DECL_CNFERR;
771 return 0;
772}
773
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200774#define SMC_CLC_MAX_ACCEPT_LEN \
775 (sizeof(struct smc_clc_msg_accept_confirm_v2) + \
Ursula Braunb81a5eb2020-09-26 12:44:31 +0200776 sizeof(struct smc_clc_first_contact_ext) + \
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200777 sizeof(struct smc_clc_msg_trail))
778
Hans Wippel3b2dec22018-05-18 09:34:18 +0200779/* CLC handshake during connect */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200780static int smc_connect_clc(struct smc_sock *smc,
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200781 struct smc_clc_msg_accept_confirm_v2 *aclc2,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200782 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200783{
784 int rc = 0;
785
786 /* do inband token exchange */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200787 rc = smc_clc_send_proposal(smc, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200788 if (rc)
789 return rc;
790 /* receive SMC Accept CLC message */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200791 return smc_clc_wait_msg(smc, aclc2, SMC_CLC_MAX_ACCEPT_LEN,
792 SMC_CLC_ACCEPT, CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200793}
794
Karsten Graulb4ba4652021-10-16 11:37:50 +0200795void smc_fill_gid_list(struct smc_link_group *lgr,
796 struct smc_gidlist *gidlist,
797 struct smc_ib_device *known_dev, u8 *known_gid)
Karsten Graule5c47442021-10-16 11:37:45 +0200798{
799 struct smc_init_info *alt_ini = NULL;
800
801 memset(gidlist, 0, sizeof(*gidlist));
802 memcpy(gidlist->list[gidlist->len++], known_gid, SMC_GID_SIZE);
803
804 alt_ini = kzalloc(sizeof(*alt_ini), GFP_KERNEL);
805 if (!alt_ini)
806 goto out;
807
808 alt_ini->vlan_id = lgr->vlan_id;
809 alt_ini->check_smcrv2 = true;
810 alt_ini->smcrv2.saddr = lgr->saddr;
811 smc_pnet_find_alt_roce(lgr, alt_ini, known_dev);
812
813 if (!alt_ini->smcrv2.ib_dev_v2)
814 goto out;
815
816 memcpy(gidlist->list[gidlist->len++], alt_ini->smcrv2.ib_gid_v2,
817 SMC_GID_SIZE);
818
819out:
820 kfree(alt_ini);
821}
822
823static int smc_connect_rdma_v2_prepare(struct smc_sock *smc,
824 struct smc_clc_msg_accept_confirm *aclc,
825 struct smc_init_info *ini)
826{
827 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
828 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
829 struct smc_clc_first_contact_ext *fce =
830 (struct smc_clc_first_contact_ext *)
831 (((u8 *)clc_v2) + sizeof(*clc_v2));
832
833 if (!ini->first_contact_peer || aclc->hdr.version == SMC_V1)
834 return 0;
835
836 if (fce->v2_direct) {
837 memcpy(ini->smcrv2.nexthop_mac, &aclc->r0.lcl.mac, ETH_ALEN);
838 ini->smcrv2.uses_gateway = false;
839 } else {
840 if (smc_ib_find_route(smc->clcsock->sk->sk_rcv_saddr,
841 smc_ib_gid_to_ipv4(aclc->r0.lcl.gid),
842 ini->smcrv2.nexthop_mac,
843 &ini->smcrv2.uses_gateway))
844 return SMC_CLC_DECL_NOROUTE;
845 if (!ini->smcrv2.uses_gateway) {
846 /* mismatch: peer claims indirect, but its direct */
847 return SMC_CLC_DECL_NOINDIRECT;
848 }
849 }
850 return 0;
851}
852
Ursula Brauna046d572017-01-09 16:55:16 +0100853/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200854static int smc_connect_rdma(struct smc_sock *smc,
855 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200856 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200857{
Karsten Graul0fb0b022020-04-30 15:55:43 +0200858 int i, reason_code = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200859 struct smc_link *link;
Karsten Graule5c47442021-10-16 11:37:45 +0200860 u8 *eid = NULL;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200861
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200862 ini->is_smcd = false;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200863 ini->ib_clcqpn = ntoh24(aclc->r0.qpn);
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200864 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
Karsten Graule5c47442021-10-16 11:37:45 +0200865 memcpy(ini->peer_systemid, aclc->r0.lcl.id_for_peer, SMC_SYSTEMID_LEN);
866 memcpy(ini->peer_gid, aclc->r0.lcl.gid, SMC_GID_SIZE);
867 memcpy(ini->peer_mac, aclc->r0.lcl.mac, ETH_ALEN);
868
869 reason_code = smc_connect_rdma_v2_prepare(smc, aclc, ini);
870 if (reason_code)
871 return reason_code;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200872
Hans Wippel72a36a82019-02-07 15:56:18 +0100873 mutex_lock(&smc_client_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200874 reason_code = smc_conn_create(smc, ini);
875 if (reason_code) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100876 mutex_unlock(&smc_client_lgr_pending);
877 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200878 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200879
880 smc_conn_save_peer_info(smc, aclc);
881
Ursula Braun5ac54d82020-09-10 18:48:21 +0200882 if (ini->first_contact_local) {
Karsten Graul0fb0b022020-04-30 15:55:43 +0200883 link = smc->conn.lnk;
884 } else {
885 /* set link that was assigned by server */
886 link = NULL;
887 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
888 struct smc_link *l = &smc->conn.lgr->lnk[i];
889
Ursula Braun3d9725a2020-09-10 18:48:23 +0200890 if (l->peer_qpn == ntoh24(aclc->r0.qpn) &&
891 !memcmp(l->peer_gid, &aclc->r0.lcl.gid,
892 SMC_GID_SIZE) &&
Karsten Graule5c47442021-10-16 11:37:45 +0200893 (aclc->hdr.version > SMC_V1 ||
894 !memcmp(l->peer_mac, &aclc->r0.lcl.mac,
895 sizeof(l->peer_mac)))) {
Karsten Graul0fb0b022020-04-30 15:55:43 +0200896 link = l;
897 break;
898 }
899 }
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200900 if (!link) {
901 reason_code = SMC_CLC_DECL_NOSRVLINK;
902 goto connect_abort;
903 }
Guvenc Gulce64513d22021-08-09 11:05:57 +0200904 smc_switch_link_and_count(&smc->conn, link);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200905 }
906
Hans Wippel3b2dec22018-05-18 09:34:18 +0200907 /* create send buffer and rmb */
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200908 if (smc_buf_create(smc, false)) {
909 reason_code = SMC_CLC_DECL_MEM;
910 goto connect_abort;
911 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200912
Ursula Braun5ac54d82020-09-10 18:48:21 +0200913 if (ini->first_contact_local)
Karsten Graule5c47442021-10-16 11:37:45 +0200914 smc_link_save_peer_info(link, aclc, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200915
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200916 if (smc_rmb_rtoken_handling(&smc->conn, link, aclc)) {
917 reason_code = SMC_CLC_DECL_ERR_RTOK;
918 goto connect_abort;
919 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200920
921 smc_close_init(smc);
922 smc_rx_init(smc);
923
Ursula Braun5ac54d82020-09-10 18:48:21 +0200924 if (ini->first_contact_local) {
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200925 if (smc_ib_ready_link(link)) {
926 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
927 goto connect_abort;
928 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200929 } else {
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200930 if (smcr_lgr_reg_rmbs(link, smc->conn.rmb_desc)) {
931 reason_code = SMC_CLC_DECL_ERR_REGRMB;
932 goto connect_abort;
933 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200934 }
935 smc_rmb_sync_sg_for_device(&smc->conn);
936
Karsten Graule5c47442021-10-16 11:37:45 +0200937 if (aclc->hdr.version > SMC_V1) {
938 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
939 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
940
941 eid = clc_v2->r1.eid;
942 if (ini->first_contact_local)
943 smc_fill_gid_list(link->lgr, &ini->smcrv2.gidlist,
944 link->smcibdev, link->gid);
945 }
946
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200947 reason_code = smc_clc_send_confirm(smc, ini->first_contact_local,
Karsten Graule5c47442021-10-16 11:37:45 +0200948 aclc->hdr.version, eid, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200949 if (reason_code)
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200950 goto connect_abort;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200951
952 smc_tx_init(smc);
953
Ursula Braun5ac54d82020-09-10 18:48:21 +0200954 if (ini->first_contact_local) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200955 /* QP confirmation over RoCE fabric */
Karsten Graul0fb0b022020-04-30 15:55:43 +0200956 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
Karsten Graulb9247542020-04-29 17:10:41 +0200957 reason_code = smcr_clnt_conf_first_link(smc);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200958 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200959 if (reason_code)
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200960 goto connect_abort;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200961 }
Hans Wippel72a36a82019-02-07 15:56:18 +0100962 mutex_unlock(&smc_client_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200963
964 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200965 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200966 if (smc->sk.sk_state == SMC_INIT)
967 smc->sk.sk_state = SMC_ACTIVE;
968
969 return 0;
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200970connect_abort:
Karsten Graul8cf3f3e2020-12-01 20:20:36 +0100971 smc_conn_abort(smc, ini->first_contact_local);
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200972 mutex_unlock(&smc_client_lgr_pending);
973 smc->connect_nonblock = 0;
974
975 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200976}
977
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200978/* The server has chosen one of the proposed ISM devices for the communication.
979 * Determine from the CHID of the received CLC ACCEPT the ISM device chosen.
980 */
981static int
982smc_v2_determine_accepted_chid(struct smc_clc_msg_accept_confirm_v2 *aclc,
983 struct smc_init_info *ini)
984{
985 int i;
986
987 for (i = 0; i < ini->ism_offered_cnt + 1; i++) {
Karsten Graule5c47442021-10-16 11:37:45 +0200988 if (ini->ism_chid[i] == ntohs(aclc->d1.chid)) {
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200989 ini->ism_selected = i;
990 return 0;
991 }
992 }
993
994 return -EPROTO;
995}
996
Hans Wippel41349842018-06-28 19:05:11 +0200997/* setup for ISM connection of client */
998static int smc_connect_ism(struct smc_sock *smc,
999 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001000 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +02001001{
Karsten Graulfa086662021-09-14 10:35:05 +02001002 u8 *eid = NULL;
Hans Wippel41349842018-06-28 19:05:11 +02001003 int rc = 0;
1004
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001005 ini->is_smcd = true;
Ursula Braunf1eb02f2020-09-26 12:44:20 +02001006 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001007
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001008 if (aclc->hdr.version == SMC_V2) {
1009 struct smc_clc_msg_accept_confirm_v2 *aclc_v2 =
1010 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
1011
1012 rc = smc_v2_determine_accepted_chid(aclc_v2, ini);
1013 if (rc)
1014 return rc;
1015 }
1016 ini->ism_peer_gid[ini->ism_selected] = aclc->d0.gid;
1017
Hans Wippel72a36a82019-02-07 15:56:18 +01001018 /* there is only one lgr role for SMC-D; use server lock */
1019 mutex_lock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +02001020 rc = smc_conn_create(smc, ini);
1021 if (rc) {
Hans Wippel72a36a82019-02-07 15:56:18 +01001022 mutex_unlock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +02001023 return rc;
Hans Wippel72a36a82019-02-07 15:56:18 +01001024 }
Hans Wippel41349842018-06-28 19:05:11 +02001025
1026 /* Create send and receive buffers */
Karsten Graul72b7f6c2020-07-26 20:34:28 +02001027 rc = smc_buf_create(smc, true);
Karsten Graulc60a2ce2020-10-07 22:57:41 +02001028 if (rc) {
1029 rc = (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB : SMC_CLC_DECL_MEM;
1030 goto connect_abort;
1031 }
Hans Wippel41349842018-06-28 19:05:11 +02001032
1033 smc_conn_save_peer_info(smc, aclc);
1034 smc_close_init(smc);
1035 smc_rx_init(smc);
1036 smc_tx_init(smc);
1037
Karsten Graulfa086662021-09-14 10:35:05 +02001038 if (aclc->hdr.version > SMC_V1) {
1039 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
1040 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
1041
Karsten Graule5c47442021-10-16 11:37:45 +02001042 eid = clc_v2->d1.eid;
Karsten Graulfa086662021-09-14 10:35:05 +02001043 }
1044
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001045 rc = smc_clc_send_confirm(smc, ini->first_contact_local,
Karsten Graule5c47442021-10-16 11:37:45 +02001046 aclc->hdr.version, eid, NULL);
Hans Wippel41349842018-06-28 19:05:11 +02001047 if (rc)
Karsten Graulc60a2ce2020-10-07 22:57:41 +02001048 goto connect_abort;
Hans Wippel72a36a82019-02-07 15:56:18 +01001049 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel41349842018-06-28 19:05:11 +02001050
1051 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +02001052 smc->connect_nonblock = 0;
Hans Wippel41349842018-06-28 19:05:11 +02001053 if (smc->sk.sk_state == SMC_INIT)
1054 smc->sk.sk_state = SMC_ACTIVE;
1055
1056 return 0;
Karsten Graulc60a2ce2020-10-07 22:57:41 +02001057connect_abort:
Karsten Graul8cf3f3e2020-12-01 20:20:36 +01001058 smc_conn_abort(smc, ini->first_contact_local);
Karsten Graulc60a2ce2020-10-07 22:57:41 +02001059 mutex_unlock(&smc_server_lgr_pending);
1060 smc->connect_nonblock = 0;
1061
1062 return rc;
Hans Wippel41349842018-06-28 19:05:11 +02001063}
1064
Ursula Braund70bf4f2020-09-26 12:44:27 +02001065/* check if received accept type and version matches a proposed one */
1066static int smc_connect_check_aclc(struct smc_init_info *ini,
1067 struct smc_clc_msg_accept_confirm *aclc)
1068{
Karsten Graul42042db2021-10-16 11:37:44 +02001069 if (aclc->hdr.typev1 != SMC_TYPE_R &&
1070 aclc->hdr.typev1 != SMC_TYPE_D)
Ursula Braund70bf4f2020-09-26 12:44:27 +02001071 return SMC_CLC_DECL_MODEUNSUPP;
1072
Karsten Graul42042db2021-10-16 11:37:44 +02001073 if (aclc->hdr.version >= SMC_V2) {
1074 if ((aclc->hdr.typev1 == SMC_TYPE_R &&
1075 !smcr_indicated(ini->smc_type_v2)) ||
1076 (aclc->hdr.typev1 == SMC_TYPE_D &&
1077 !smcd_indicated(ini->smc_type_v2)))
1078 return SMC_CLC_DECL_MODEUNSUPP;
1079 } else {
1080 if ((aclc->hdr.typev1 == SMC_TYPE_R &&
1081 !smcr_indicated(ini->smc_type_v1)) ||
1082 (aclc->hdr.typev1 == SMC_TYPE_D &&
1083 !smcd_indicated(ini->smc_type_v1)))
1084 return SMC_CLC_DECL_MODEUNSUPP;
1085 }
1086
Ursula Braund70bf4f2020-09-26 12:44:27 +02001087 return 0;
1088}
1089
Hans Wippel3b2dec22018-05-18 09:34:18 +02001090/* perform steps before actually connecting */
1091static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001092{
Guvenc Gulce49407ae2020-12-01 20:20:42 +01001093 u8 version = smc_ism_is_v2_capable() ? SMC_V2 : SMC_V1;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001094 struct smc_clc_msg_accept_confirm_v2 *aclc2;
1095 struct smc_clc_msg_accept_confirm *aclc;
Ursula Braun3fc64932020-09-26 12:44:23 +02001096 struct smc_init_info *ini = NULL;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001097 u8 *buf = NULL;
Ursula Brauna046d572017-01-09 16:55:16 +01001098 int rc = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001099
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001100 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +02001101 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001102
Hans Wippel3b2dec22018-05-18 09:34:18 +02001103 /* if peer has not signalled SMC-capability, fall back */
1104 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +02001105 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001106
Ursula Braune8d726c82020-09-26 12:44:32 +02001107 /* IPSec connections opt out of SMC optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001108 if (using_ipsec(smc))
Ursula Braune8d726c82020-09-26 12:44:32 +02001109 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC,
1110 version);
Ursula Brauna046d572017-01-09 16:55:16 +01001111
Ursula Braun3fc64932020-09-26 12:44:23 +02001112 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
1113 if (!ini)
Ursula Braune8d726c82020-09-26 12:44:32 +02001114 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_MEM,
1115 version);
Ursula Braun3fc64932020-09-26 12:44:23 +02001116
Karsten Graul42042db2021-10-16 11:37:44 +02001117 ini->smcd_version = SMC_V1 | SMC_V2;
1118 ini->smcr_version = SMC_V1 | SMC_V2;
Ursula Braund70bf4f2020-09-26 12:44:27 +02001119 ini->smc_type_v1 = SMC_TYPE_B;
Karsten Graul42042db2021-10-16 11:37:44 +02001120 ini->smc_type_v2 = SMC_TYPE_B;
Ursula Braund70bf4f2020-09-26 12:44:27 +02001121
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001122 /* get vlan id from IP device */
Ursula Braun3fc64932020-09-26 12:44:23 +02001123 if (smc_vlan_by_tcpsk(smc->clcsock, ini)) {
Ursula Braund70bf4f2020-09-26 12:44:27 +02001124 ini->smcd_version &= ~SMC_V1;
Karsten Graul42042db2021-10-16 11:37:44 +02001125 ini->smcr_version = 0;
Ursula Braund70bf4f2020-09-26 12:44:27 +02001126 ini->smc_type_v1 = SMC_TYPE_N;
1127 if (!ini->smcd_version) {
1128 rc = SMC_CLC_DECL_GETVLANERR;
1129 goto fallback;
1130 }
Ursula Braun3fc64932020-09-26 12:44:23 +02001131 }
Hans Wippel41349842018-06-28 19:05:11 +02001132
Ursula Braund70bf4f2020-09-26 12:44:27 +02001133 rc = smc_find_proposal_devices(smc, ini);
1134 if (rc)
1135 goto fallback;
Ursula Brauna046d572017-01-09 16:55:16 +01001136
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001137 buf = kzalloc(SMC_CLC_MAX_ACCEPT_LEN, GFP_KERNEL);
1138 if (!buf) {
1139 rc = SMC_CLC_DECL_MEM;
1140 goto fallback;
1141 }
1142 aclc2 = (struct smc_clc_msg_accept_confirm_v2 *)buf;
1143 aclc = (struct smc_clc_msg_accept_confirm *)aclc2;
1144
Hans Wippel3b2dec22018-05-18 09:34:18 +02001145 /* perform CLC handshake */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001146 rc = smc_connect_clc(smc, aclc2, ini);
Ursula Braund70bf4f2020-09-26 12:44:27 +02001147 if (rc)
1148 goto vlan_cleanup;
1149
1150 /* check if smc modes and versions of CLC proposal and accept match */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001151 rc = smc_connect_check_aclc(ini, aclc);
Karsten Graul0530bd62020-11-18 22:40:37 +01001152 version = aclc->hdr.version == SMC_V1 ? SMC_V1 : SMC_V2;
Ursula Braund70bf4f2020-09-26 12:44:27 +02001153 if (rc)
1154 goto vlan_cleanup;
Ursula Brauna046d572017-01-09 16:55:16 +01001155
Hans Wippel41349842018-06-28 19:05:11 +02001156 /* depending on previous steps, connect using rdma or ism */
Karsten Graul42042db2021-10-16 11:37:44 +02001157 if (aclc->hdr.typev1 == SMC_TYPE_R) {
1158 ini->smcr_version = version;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001159 rc = smc_connect_rdma(smc, aclc, ini);
Karsten Graul42042db2021-10-16 11:37:44 +02001160 } else if (aclc->hdr.typev1 == SMC_TYPE_D) {
1161 ini->smcd_version = version;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001162 rc = smc_connect_ism(smc, aclc, ini);
Karsten Graul42042db2021-10-16 11:37:44 +02001163 }
Ursula Braund70bf4f2020-09-26 12:44:27 +02001164 if (rc)
1165 goto vlan_cleanup;
Ursula Brauna046d572017-01-09 16:55:16 +01001166
Guvenc Gulce194730a2021-06-16 16:52:58 +02001167 SMC_STAT_CLNT_SUCC_INC(sock_net(smc->clcsock->sk), aclc);
Ursula Braund70bf4f2020-09-26 12:44:27 +02001168 smc_connect_ism_vlan_cleanup(smc, ini);
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001169 kfree(buf);
Ursula Braun3fc64932020-09-26 12:44:23 +02001170 kfree(ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001171 return 0;
Ursula Braund70bf4f2020-09-26 12:44:27 +02001172
1173vlan_cleanup:
1174 smc_connect_ism_vlan_cleanup(smc, ini);
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001175 kfree(buf);
Ursula Braund70bf4f2020-09-26 12:44:27 +02001176fallback:
1177 kfree(ini);
Ursula Braune8d726c82020-09-26 12:44:32 +02001178 return smc_connect_decline_fallback(smc, rc, version);
Ursula Brauna046d572017-01-09 16:55:16 +01001179}
1180
Ursula Braun24ac3a02018-06-27 17:59:50 +02001181static void smc_connect_work(struct work_struct *work)
1182{
1183 struct smc_sock *smc = container_of(work, struct smc_sock,
1184 connect_work);
Ursula Braun50717a32019-04-12 12:57:23 +02001185 long timeo = smc->sk.sk_sndtimeo;
1186 int rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001187
Ursula Braun50717a32019-04-12 12:57:23 +02001188 if (!timeo)
1189 timeo = MAX_SCHEDULE_TIMEOUT;
1190 lock_sock(smc->clcsock->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +02001191 if (smc->clcsock->sk->sk_err) {
1192 smc->sk.sk_err = smc->clcsock->sk->sk_err;
Ursula Braun50717a32019-04-12 12:57:23 +02001193 } else if ((1 << smc->clcsock->sk->sk_state) &
Wen Guf3a3a0f2021-10-28 15:13:47 +08001194 (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
Ursula Braun50717a32019-04-12 12:57:23 +02001195 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
1196 if ((rc == -EPIPE) &&
1197 ((1 << smc->clcsock->sk->sk_state) &
1198 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
1199 rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001200 }
Ursula Braun50717a32019-04-12 12:57:23 +02001201 release_sock(smc->clcsock->sk);
1202 lock_sock(&smc->sk);
1203 if (rc != 0 || smc->sk.sk_err) {
1204 smc->sk.sk_state = SMC_CLOSED;
1205 if (rc == -EPIPE || rc == -EAGAIN)
1206 smc->sk.sk_err = EPIPE;
1207 else if (signal_pending(current))
1208 smc->sk.sk_err = -sock_intr_errno(timeo);
Ursula Braun6d6dd522019-11-12 16:03:41 +01001209 sock_put(&smc->sk); /* passive closing */
Ursula Braun24ac3a02018-06-27 17:59:50 +02001210 goto out;
1211 }
1212
1213 rc = __smc_connect(smc);
1214 if (rc < 0)
1215 smc->sk.sk_err = -rc;
1216
1217out:
Ursula Braun07603b22019-04-11 11:17:32 +02001218 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
1219 if (smc->sk.sk_err) {
1220 smc->sk.sk_state_change(&smc->sk);
1221 } else { /* allow polling before and after fallback decision */
1222 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
1223 smc->sk.sk_write_space(&smc->sk);
1224 }
1225 }
Ursula Braun24ac3a02018-06-27 17:59:50 +02001226 release_sock(&smc->sk);
1227}
1228
Ursula Braunac713872017-01-09 16:55:13 +01001229static int smc_connect(struct socket *sock, struct sockaddr *addr,
1230 int alen, int flags)
1231{
1232 struct sock *sk = sock->sk;
1233 struct smc_sock *smc;
1234 int rc = -EINVAL;
1235
1236 smc = smc_sk(sk);
1237
1238 /* separate smc parameter checking to be safe */
1239 if (alen < sizeof(addr->sa_family))
1240 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001241 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +01001242 goto out_err;
1243
1244 lock_sock(sk);
1245 switch (sk->sk_state) {
1246 default:
1247 goto out;
1248 case SMC_ACTIVE:
1249 rc = -EISCONN;
1250 goto out;
1251 case SMC_INIT:
Ursula Braunac713872017-01-09 16:55:13 +01001252 break;
1253 }
1254
1255 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001256 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun50717a32019-04-12 12:57:23 +02001257 if (smc->connect_nonblock) {
1258 rc = -EALREADY;
1259 goto out;
1260 }
1261 rc = kernel_connect(smc->clcsock, addr, alen, flags);
1262 if (rc && rc != -EINPROGRESS)
1263 goto out;
Ursula Braun301428e2019-10-29 12:41:26 +01001264
1265 sock_hold(&smc->sk); /* sock put in passive closing */
Ursula Braun86434742019-12-12 22:35:58 +01001266 if (smc->use_fallback)
1267 goto out;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001268 if (flags & O_NONBLOCK) {
Karsten Graul22ef4732020-09-10 18:48:29 +02001269 if (queue_work(smc_hs_wq, &smc->connect_work))
Ursula Braun50717a32019-04-12 12:57:23 +02001270 smc->connect_nonblock = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001271 rc = -EINPROGRESS;
1272 } else {
Ursula Braun24ac3a02018-06-27 17:59:50 +02001273 rc = __smc_connect(smc);
1274 if (rc < 0)
1275 goto out;
1276 else
1277 rc = 0; /* success cases including fallback */
1278 }
Ursula Braunac713872017-01-09 16:55:13 +01001279
1280out:
1281 release_sock(sk);
1282out_err:
1283 return rc;
1284}
1285
1286static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
1287{
Ursula Braun3163c502018-01-24 10:28:12 +01001288 struct socket *new_clcsock = NULL;
1289 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +01001290 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -08001291 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +01001292
Ursula Braun3163c502018-01-24 10:28:12 +01001293 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001294 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001295 if (!new_sk) {
1296 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +01001297 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +01001298 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +01001299 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +01001300 goto out;
1301 }
1302 *new_smc = smc_sk(new_sk);
1303
Myungho Jung78abe3d2018-12-18 09:02:25 -08001304 mutex_lock(&lsmc->clcsock_release_lock);
1305 if (lsmc->clcsock)
Ursula Brauna60a2b12020-09-10 18:48:20 +02001306 rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK);
Myungho Jung78abe3d2018-12-18 09:02:25 -08001307 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +01001308 lock_sock(lsk);
Ursula Brauna60a2b12020-09-10 18:48:20 +02001309 if (rc < 0 && rc != -EAGAIN)
Ursula Braun3163c502018-01-24 10:28:12 +01001310 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +01001311 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +02001312 new_sk->sk_prot->unhash(new_sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001313 if (new_clcsock)
1314 sock_release(new_clcsock);
1315 new_sk->sk_state = SMC_CLOSED;
1316 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun51f1de72018-01-26 09:28:48 +01001317 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +01001318 *new_smc = NULL;
1319 goto out;
1320 }
1321
Ursula Brauna60a2b12020-09-10 18:48:20 +02001322 /* new clcsock has inherited the smc listen-specific sk_data_ready
1323 * function; switch it back to the original sk_data_ready function
1324 */
1325 new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready;
Ursula Braunac713872017-01-09 16:55:13 +01001326 (*new_smc)->clcsock = new_clcsock;
1327out:
1328 return rc;
1329}
1330
Ursula Brauna046d572017-01-09 16:55:16 +01001331/* add a just created sock to the accept queue of the listen sock as
1332 * candidate for a following socket accept call from user space
1333 */
1334static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
1335{
1336 struct smc_sock *par = smc_sk(parent);
1337
Ursula Braun51f1de72018-01-26 09:28:48 +01001338 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +01001339 spin_lock(&par->accept_q_lock);
1340 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
1341 spin_unlock(&par->accept_q_lock);
1342 sk_acceptq_added(parent);
1343}
1344
1345/* remove a socket from the accept queue of its parental listening socket */
1346static void smc_accept_unlink(struct sock *sk)
1347{
1348 struct smc_sock *par = smc_sk(sk)->listen_smc;
1349
1350 spin_lock(&par->accept_q_lock);
1351 list_del_init(&smc_sk(sk)->accept_q);
1352 spin_unlock(&par->accept_q_lock);
1353 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001354 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +01001355}
1356
1357/* remove a sock from the accept queue to bind it to a new socket created
1358 * for a socket accept call from user space
1359 */
Ursula Braunb38d7322017-01-09 16:55:25 +01001360struct sock *smc_accept_dequeue(struct sock *parent,
1361 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +01001362{
1363 struct smc_sock *isk, *n;
1364 struct sock *new_sk;
1365
1366 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
1367 new_sk = (struct sock *)isk;
1368
1369 smc_accept_unlink(new_sk);
1370 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +02001371 new_sk->sk_prot->unhash(new_sk);
Ursula Braun127f4972018-01-26 09:28:49 +01001372 if (isk->clcsock) {
1373 sock_release(isk->clcsock);
1374 isk->clcsock = NULL;
1375 }
Ursula Braun51f1de72018-01-26 09:28:48 +01001376 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +01001377 continue;
1378 }
Ursula Braun07603b22019-04-11 11:17:32 +02001379 if (new_sock) {
Ursula Brauna046d572017-01-09 16:55:16 +01001380 sock_graft(new_sk, new_sock);
Ursula Braun07603b22019-04-11 11:17:32 +02001381 if (isk->use_fallback) {
1382 smc_sk(new_sk)->clcsock->file = new_sock->file;
1383 isk->clcsock->file->private_data = isk->clcsock;
1384 }
1385 }
Ursula Brauna046d572017-01-09 16:55:16 +01001386 return new_sk;
1387 }
1388 return NULL;
1389}
1390
1391/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +01001392void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +01001393{
1394 struct smc_sock *smc = smc_sk(sk);
1395
Ursula Braun81cf4f42019-10-21 16:13:15 +02001396 sock_hold(sk); /* sock_put below */
Ursula Braunb38d7322017-01-09 16:55:25 +01001397 lock_sock(sk);
1398 if (!sk->sk_lingertime)
1399 /* wait for peer closing */
1400 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun39f41f32019-06-27 15:04:52 +02001401 __smc_release(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +01001402 release_sock(sk);
Ursula Braun81cf4f42019-10-21 16:13:15 +02001403 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +01001404 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +01001405}
1406
Karsten Graulb9247542020-04-29 17:10:41 +02001407static int smcr_serv_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001408{
Karsten Graul387707f2020-04-29 17:10:40 +02001409 struct smc_link *link = smc->conn.lnk;
Karsten Graul4667bb42020-04-30 15:55:42 +02001410 struct smc_llc_qentry *qentry;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001411 int rc;
1412
Karsten Graul7562a132020-05-01 12:48:01 +02001413 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +02001414 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +02001415
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001416 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +02001417 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001418 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001419 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001420
1421 /* receive CONFIRM LINK response from client over the RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001422 qentry = smc_llc_wait(link->lgr, link, SMC_LLC_WAIT_TIME,
1423 SMC_LLC_CONFIRM_LINK);
1424 if (!qentry) {
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001425 struct smc_clc_msg_decline dclc;
1426
1427 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001428 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001429 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001430 }
Karsten Graul649758f2020-05-04 14:18:48 +02001431 smc_llc_save_peer_uid(qentry);
Karsten Graul4667bb42020-04-30 15:55:42 +02001432 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_RESP);
1433 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
1434 if (rc)
Karsten Graul75d320d2018-03-01 13:51:31 +01001435 return SMC_CLC_DECL_RMBE_EC;
1436
Karsten Graul4667bb42020-04-30 15:55:42 +02001437 /* confirm_rkey is implicit on 1st contact */
1438 smc->conn.rmb_desc->is_conf_rkey = true;
Karsten Graul52bedf32018-03-01 13:51:32 +01001439
Karsten Graul00a049c2020-04-29 17:10:49 +02001440 smc_llc_link_active(link);
Karsten Graul0a99be42020-05-05 15:01:20 +02001441 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
Karsten Graul52bedf32018-03-01 13:51:32 +01001442
Karsten Graul4667bb42020-04-30 15:55:42 +02001443 /* initial contact - try to establish second link */
Karsten Graulb4ba4652021-10-16 11:37:50 +02001444 smc_llc_srv_add_link(link, NULL);
Karsten Graul75d320d2018-03-01 13:51:31 +01001445 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001446}
1447
Hans Wippel3b2dec22018-05-18 09:34:18 +02001448/* listen worker: finish */
1449static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001450{
Ursula Brauna046d572017-01-09 16:55:16 +01001451 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001452 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001453
Ursula Brauna046d572017-01-09 16:55:16 +01001454 if (lsmc->sk.sk_state == SMC_LISTEN) {
Karsten Graulfd577702019-04-11 11:17:30 +02001455 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001456 smc_accept_enqueue(&lsmc->sk, newsmcsk);
Karsten Graulfd577702019-04-11 11:17:30 +02001457 release_sock(&lsmc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001458 } else { /* no longer listening */
1459 smc_close_non_accepted(newsmcsk);
1460 }
Ursula Brauna046d572017-01-09 16:55:16 +01001461
1462 /* Wake up accept */
1463 lsmc->sk.sk_data_ready(&lsmc->sk);
1464 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001465}
Ursula Brauna046d572017-01-09 16:55:16 +01001466
Hans Wippel3b2dec22018-05-18 09:34:18 +02001467/* listen worker: finish in state connected */
1468static void smc_listen_out_connected(struct smc_sock *new_smc)
1469{
1470 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001471
Hans Wippel3b2dec22018-05-18 09:34:18 +02001472 sk_refcnt_debug_inc(newsmcsk);
1473 if (newsmcsk->sk_state == SMC_INIT)
1474 newsmcsk->sk_state = SMC_ACTIVE;
1475
1476 smc_listen_out(new_smc);
1477}
1478
1479/* listen worker: finish in error state */
1480static void smc_listen_out_err(struct smc_sock *new_smc)
1481{
1482 struct sock *newsmcsk = &new_smc->sk;
Guvenc Gulce194730a2021-06-16 16:52:58 +02001483 struct net *net = sock_net(newsmcsk);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001484
Guvenc Gulce194730a2021-06-16 16:52:58 +02001485 this_cpu_inc(net->smc.smc_stats->srv_hshake_err_cnt);
Ursula Braun51f1de72018-01-26 09:28:48 +01001486 if (newsmcsk->sk_state == SMC_INIT)
1487 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001488 newsmcsk->sk_state = SMC_CLOSED;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001489
1490 smc_listen_out(new_smc);
1491}
1492
1493/* listen worker: decline and fall back if possible */
1494static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
Karsten Graul4a9baf42020-10-23 20:48:28 +02001495 int local_first, u8 version)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001496{
1497 /* RDMA setup failed, switch back to TCP */
Karsten Graul8cf3f3e2020-12-01 20:20:36 +01001498 smc_conn_abort(new_smc, local_first);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001499 if (reason_code < 0) { /* error, no fallback possible */
1500 smc_listen_out_err(new_smc);
1501 return;
1502 }
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02001503 smc_switch_to_fallback(new_smc, reason_code);
Karsten Graul603cc142018-07-25 16:35:32 +02001504 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Ursula Braune8d726c82020-09-26 12:44:32 +02001505 if (smc_clc_send_decline(new_smc, reason_code, version) < 0) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001506 smc_listen_out_err(new_smc);
1507 return;
1508 }
1509 }
1510 smc_listen_out_connected(new_smc);
1511}
1512
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001513/* listen worker: version checking */
1514static int smc_listen_v2_check(struct smc_sock *new_smc,
1515 struct smc_clc_msg_proposal *pclc,
1516 struct smc_init_info *ini)
1517{
1518 struct smc_clc_smcd_v2_extension *pclc_smcd_v2_ext;
1519 struct smc_clc_v2_extension *pclc_v2_ext;
Karsten Graul37524042020-10-31 19:19:38 +01001520 int rc = SMC_CLC_DECL_PEERNOSMC;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001521
1522 ini->smc_type_v1 = pclc->hdr.typev1;
1523 ini->smc_type_v2 = pclc->hdr.typev2;
Karsten Graule49300a2021-10-16 11:37:46 +02001524 ini->smcd_version = smcd_indicated(ini->smc_type_v1) ? SMC_V1 : 0;
1525 ini->smcr_version = smcr_indicated(ini->smc_type_v1) ? SMC_V1 : 0;
1526 if (pclc->hdr.version > SMC_V1) {
1527 if (smcd_indicated(ini->smc_type_v2))
1528 ini->smcd_version |= SMC_V2;
1529 if (smcr_indicated(ini->smc_type_v2))
1530 ini->smcr_version |= SMC_V2;
Karsten Graul37524042020-10-31 19:19:38 +01001531 }
Karsten Graule49300a2021-10-16 11:37:46 +02001532 if (!(ini->smcd_version & SMC_V2) && !(ini->smcr_version & SMC_V2)) {
1533 rc = SMC_CLC_DECL_PEERNOSMC;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001534 goto out;
1535 }
1536 pclc_v2_ext = smc_get_clc_v2_ext(pclc);
1537 if (!pclc_v2_ext) {
1538 ini->smcd_version &= ~SMC_V2;
Karsten Graule49300a2021-10-16 11:37:46 +02001539 ini->smcr_version &= ~SMC_V2;
Karsten Graul37524042020-10-31 19:19:38 +01001540 rc = SMC_CLC_DECL_NOV2EXT;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001541 goto out;
1542 }
1543 pclc_smcd_v2_ext = smc_get_clc_smcd_v2_ext(pclc_v2_ext);
Karsten Graule49300a2021-10-16 11:37:46 +02001544 if (ini->smcd_version & SMC_V2) {
1545 if (!smc_ism_is_v2_capable()) {
1546 ini->smcd_version &= ~SMC_V2;
1547 rc = SMC_CLC_DECL_NOISM2SUPP;
1548 } else if (!pclc_smcd_v2_ext) {
1549 ini->smcd_version &= ~SMC_V2;
1550 rc = SMC_CLC_DECL_NOV2DEXT;
1551 } else if (!pclc_v2_ext->hdr.eid_cnt &&
1552 !pclc_v2_ext->hdr.flag.seid) {
1553 ini->smcd_version &= ~SMC_V2;
1554 rc = SMC_CLC_DECL_NOUEID;
1555 }
1556 }
1557 if (ini->smcr_version & SMC_V2) {
1558 if (!pclc_v2_ext->hdr.eid_cnt) {
1559 ini->smcr_version &= ~SMC_V2;
1560 rc = SMC_CLC_DECL_NOUEID;
1561 }
Karsten Graul37524042020-10-31 19:19:38 +01001562 }
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001563
1564out:
Karsten Graule49300a2021-10-16 11:37:46 +02001565 if (!ini->smcd_version && !ini->smcr_version)
Karsten Graul37524042020-10-31 19:19:38 +01001566 return rc;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001567
1568 return 0;
1569}
1570
Hans Wippel3b2dec22018-05-18 09:34:18 +02001571/* listen worker: check prefixes */
Karsten Graul59886692019-04-12 12:57:25 +02001572static int smc_listen_prfx_check(struct smc_sock *new_smc,
Hans Wippel3b2dec22018-05-18 09:34:18 +02001573 struct smc_clc_msg_proposal *pclc)
1574{
1575 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1576 struct socket *newclcsock = new_smc->clcsock;
1577
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001578 if (pclc->hdr.typev1 == SMC_TYPE_N)
1579 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001580 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1581 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
Karsten Graul59886692019-04-12 12:57:25 +02001582 return SMC_CLC_DECL_DIFFPREFIX;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001583
1584 return 0;
1585}
1586
1587/* listen worker: initialize connection and buffers */
1588static int smc_listen_rdma_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001589 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001590{
Karsten Graul7a627252019-04-12 12:57:30 +02001591 int rc;
1592
Hans Wippel3b2dec22018-05-18 09:34:18 +02001593 /* allocate connection / link group */
Karsten Graul7a627252019-04-12 12:57:30 +02001594 rc = smc_conn_create(new_smc, ini);
1595 if (rc)
1596 return rc;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001597
1598 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001599 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001600 return SMC_CLC_DECL_MEM;
1601
1602 return 0;
1603}
1604
Hans Wippel41349842018-06-28 19:05:11 +02001605/* listen worker: initialize connection and buffers for SMC-D */
1606static int smc_listen_ism_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001607 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +02001608{
Karsten Graul7a627252019-04-12 12:57:30 +02001609 int rc;
Hans Wippel41349842018-06-28 19:05:11 +02001610
Karsten Graul7a627252019-04-12 12:57:30 +02001611 rc = smc_conn_create(new_smc, ini);
1612 if (rc)
1613 return rc;
Hans Wippel41349842018-06-28 19:05:11 +02001614
Hans Wippel41349842018-06-28 19:05:11 +02001615 /* Create send and receive buffers */
Karsten Graul72b7f6c2020-07-26 20:34:28 +02001616 rc = smc_buf_create(new_smc, true);
1617 if (rc) {
Karsten Graul8cf3f3e2020-12-01 20:20:36 +01001618 smc_conn_abort(new_smc, ini->first_contact_local);
Karsten Graul72b7f6c2020-07-26 20:34:28 +02001619 return (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB :
1620 SMC_CLC_DECL_MEM;
Hans Wippel41349842018-06-28 19:05:11 +02001621 }
1622
1623 return 0;
1624}
1625
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001626static bool smc_is_already_selected(struct smcd_dev *smcd,
1627 struct smc_init_info *ini,
1628 int matches)
1629{
1630 int i;
1631
1632 for (i = 0; i < matches; i++)
1633 if (smcd == ini->ism_dev[i])
1634 return true;
1635
1636 return false;
1637}
1638
1639/* check for ISM devices matching proposed ISM devices */
1640static void smc_check_ism_v2_match(struct smc_init_info *ini,
1641 u16 proposed_chid, u64 proposed_gid,
1642 unsigned int *matches)
1643{
1644 struct smcd_dev *smcd;
1645
1646 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
1647 if (smcd->going_away)
1648 continue;
1649 if (smc_is_already_selected(smcd, ini, *matches))
1650 continue;
1651 if (smc_ism_get_chid(smcd) == proposed_chid &&
1652 !smc_ism_cantalk(proposed_gid, ISM_RESERVED_VLANID, smcd)) {
1653 ini->ism_peer_gid[*matches] = proposed_gid;
1654 ini->ism_dev[*matches] = smcd;
1655 (*matches)++;
1656 break;
1657 }
1658 }
1659}
1660
Karsten Graul37524042020-10-31 19:19:38 +01001661static void smc_find_ism_store_rc(u32 rc, struct smc_init_info *ini)
1662{
1663 if (!ini->rc)
1664 ini->rc = rc;
1665}
1666
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001667static void smc_find_ism_v2_device_serv(struct smc_sock *new_smc,
1668 struct smc_clc_msg_proposal *pclc,
1669 struct smc_init_info *ini)
1670{
1671 struct smc_clc_smcd_v2_extension *smcd_v2_ext;
1672 struct smc_clc_v2_extension *smc_v2_ext;
1673 struct smc_clc_msg_smcd *pclc_smcd;
1674 unsigned int matches = 0;
Karsten Graulf29fa002020-10-07 22:57:43 +02001675 u8 smcd_version;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001676 u8 *eid = NULL;
Karsten Graul37524042020-10-31 19:19:38 +01001677 int i, rc;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001678
1679 if (!(ini->smcd_version & SMC_V2) || !smcd_indicated(ini->smc_type_v2))
Karsten Graulf29fa002020-10-07 22:57:43 +02001680 goto not_found;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001681
1682 pclc_smcd = smc_get_clc_msg_smcd(pclc);
1683 smc_v2_ext = smc_get_clc_v2_ext(pclc);
1684 smcd_v2_ext = smc_get_clc_smcd_v2_ext(smc_v2_ext);
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001685
1686 mutex_lock(&smcd_dev_list.mutex);
1687 if (pclc_smcd->ism.chid)
1688 /* check for ISM device matching proposed native ISM device */
1689 smc_check_ism_v2_match(ini, ntohs(pclc_smcd->ism.chid),
1690 ntohll(pclc_smcd->ism.gid), &matches);
1691 for (i = 1; i <= smc_v2_ext->hdr.ism_gid_cnt; i++) {
1692 /* check for ISM devices matching proposed non-native ISM
1693 * devices
1694 */
1695 smc_check_ism_v2_match(ini,
1696 ntohs(smcd_v2_ext->gidchid[i - 1].chid),
1697 ntohll(smcd_v2_ext->gidchid[i - 1].gid),
1698 &matches);
1699 }
1700 mutex_unlock(&smcd_dev_list.mutex);
1701
Karsten Graule49300a2021-10-16 11:37:46 +02001702 if (!ini->ism_dev[0]) {
1703 smc_find_ism_store_rc(SMC_CLC_DECL_NOSMCD2DEV, ini);
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001704 goto not_found;
Karsten Graule49300a2021-10-16 11:37:46 +02001705 }
Karsten Graulfa086662021-09-14 10:35:05 +02001706
Karsten Graul11a26c52021-09-14 10:35:06 +02001707 smc_ism_get_system_eid(&eid);
Karsten Graulfa086662021-09-14 10:35:05 +02001708 if (!smc_clc_match_eid(ini->negotiated_eid, smc_v2_ext,
1709 smcd_v2_ext->system_eid, eid))
1710 goto not_found;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001711
1712 /* separate - outside the smcd_dev_list.lock */
Karsten Graulf29fa002020-10-07 22:57:43 +02001713 smcd_version = ini->smcd_version;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001714 for (i = 0; i < matches; i++) {
1715 ini->smcd_version = SMC_V2;
1716 ini->is_smcd = true;
1717 ini->ism_selected = i;
Karsten Graul37524042020-10-31 19:19:38 +01001718 rc = smc_listen_ism_init(new_smc, ini);
1719 if (rc) {
1720 smc_find_ism_store_rc(rc, ini);
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001721 /* try next active ISM device */
1722 continue;
Karsten Graul37524042020-10-31 19:19:38 +01001723 }
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001724 return; /* matching and usable V2 ISM device found */
1725 }
Karsten Graulf29fa002020-10-07 22:57:43 +02001726 /* no V2 ISM device could be initialized */
1727 ini->smcd_version = smcd_version; /* restore original value */
Karsten Graulfa086662021-09-14 10:35:05 +02001728 ini->negotiated_eid[0] = 0;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001729
1730not_found:
1731 ini->smcd_version &= ~SMC_V2;
1732 ini->ism_dev[0] = NULL;
1733 ini->is_smcd = false;
1734}
1735
1736static void smc_find_ism_v1_device_serv(struct smc_sock *new_smc,
1737 struct smc_clc_msg_proposal *pclc,
1738 struct smc_init_info *ini)
Ursula Braun7affc802020-09-26 12:44:21 +02001739{
1740 struct smc_clc_msg_smcd *pclc_smcd = smc_get_clc_msg_smcd(pclc);
Karsten Graul37524042020-10-31 19:19:38 +01001741 int rc = 0;
Ursula Braun7affc802020-09-26 12:44:21 +02001742
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001743 /* check if ISM V1 is available */
1744 if (!(ini->smcd_version & SMC_V1) || !smcd_indicated(ini->smc_type_v1))
Ursula Braun7affc802020-09-26 12:44:21 +02001745 goto not_found;
1746 ini->is_smcd = true; /* prepare ISM check */
Ursula Braun8c3dca32020-09-26 12:44:28 +02001747 ini->ism_peer_gid[0] = ntohll(pclc_smcd->ism.gid);
Karsten Graul37524042020-10-31 19:19:38 +01001748 rc = smc_find_ism_device(new_smc, ini);
1749 if (rc)
Ursula Braun7affc802020-09-26 12:44:21 +02001750 goto not_found;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001751 ini->ism_selected = 0;
Karsten Graul37524042020-10-31 19:19:38 +01001752 rc = smc_listen_ism_init(new_smc, ini);
1753 if (!rc)
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001754 return; /* V1 ISM device found */
Ursula Braun7affc802020-09-26 12:44:21 +02001755
1756not_found:
Karsten Graul37524042020-10-31 19:19:38 +01001757 smc_find_ism_store_rc(rc, ini);
Karsten Graule49300a2021-10-16 11:37:46 +02001758 ini->smcd_version &= ~SMC_V1;
Ursula Braun3fc64932020-09-26 12:44:23 +02001759 ini->ism_dev[0] = NULL;
Ursula Braun7affc802020-09-26 12:44:21 +02001760 ini->is_smcd = false;
1761}
1762
Hans Wippel3b2dec22018-05-18 09:34:18 +02001763/* listen worker: register buffers */
Ursula Braun5ac54d82020-09-10 18:48:21 +02001764static int smc_listen_rdma_reg(struct smc_sock *new_smc, bool local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001765{
Karsten Graulb9247542020-04-29 17:10:41 +02001766 struct smc_connection *conn = &new_smc->conn;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001767
Ursula Braun5ac54d82020-09-10 18:48:21 +02001768 if (!local_first) {
Karsten Graul7562a132020-05-01 12:48:01 +02001769 if (smcr_lgr_reg_rmbs(conn->lnk, conn->rmb_desc))
Karsten Graulc7674c02018-11-22 10:26:43 +01001770 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001771 }
1772 smc_rmb_sync_sg_for_device(&new_smc->conn);
1773
1774 return 0;
1775}
1776
Karsten Graule49300a2021-10-16 11:37:46 +02001777static void smc_find_rdma_v2_device_serv(struct smc_sock *new_smc,
1778 struct smc_clc_msg_proposal *pclc,
1779 struct smc_init_info *ini)
1780{
1781 struct smc_clc_v2_extension *smc_v2_ext;
1782 u8 smcr_version;
1783 int rc;
1784
1785 if (!(ini->smcr_version & SMC_V2) || !smcr_indicated(ini->smc_type_v2))
1786 goto not_found;
1787
1788 smc_v2_ext = smc_get_clc_v2_ext(pclc);
1789 if (!smc_clc_match_eid(ini->negotiated_eid, smc_v2_ext, NULL, NULL))
1790 goto not_found;
1791
1792 /* prepare RDMA check */
1793 memcpy(ini->peer_systemid, pclc->lcl.id_for_peer, SMC_SYSTEMID_LEN);
1794 memcpy(ini->peer_gid, smc_v2_ext->roce, SMC_GID_SIZE);
1795 memcpy(ini->peer_mac, pclc->lcl.mac, ETH_ALEN);
1796 ini->check_smcrv2 = true;
1797 ini->smcrv2.clc_sk = new_smc->clcsock->sk;
1798 ini->smcrv2.saddr = new_smc->clcsock->sk->sk_rcv_saddr;
1799 ini->smcrv2.daddr = smc_ib_gid_to_ipv4(smc_v2_ext->roce);
1800 rc = smc_find_rdma_device(new_smc, ini);
1801 if (rc) {
1802 smc_find_ism_store_rc(rc, ini);
1803 goto not_found;
1804 }
1805 if (!ini->smcrv2.uses_gateway)
1806 memcpy(ini->smcrv2.nexthop_mac, pclc->lcl.mac, ETH_ALEN);
1807
1808 smcr_version = ini->smcr_version;
1809 ini->smcr_version = SMC_V2;
1810 rc = smc_listen_rdma_init(new_smc, ini);
1811 if (!rc)
1812 rc = smc_listen_rdma_reg(new_smc, ini->first_contact_local);
1813 if (!rc)
1814 return;
1815 ini->smcr_version = smcr_version;
1816 smc_find_ism_store_rc(rc, ini);
1817
1818not_found:
1819 ini->smcr_version &= ~SMC_V2;
1820 ini->check_smcrv2 = false;
1821}
1822
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001823static int smc_find_rdma_v1_device_serv(struct smc_sock *new_smc,
1824 struct smc_clc_msg_proposal *pclc,
1825 struct smc_init_info *ini)
Ursula Braun7affc802020-09-26 12:44:21 +02001826{
1827 int rc;
1828
Karsten Graule49300a2021-10-16 11:37:46 +02001829 if (!(ini->smcr_version & SMC_V1) || !smcr_indicated(ini->smc_type_v1))
Ursula Braun7affc802020-09-26 12:44:21 +02001830 return SMC_CLC_DECL_NOSMCDEV;
1831
1832 /* prepare RDMA check */
Karsten Graule49300a2021-10-16 11:37:46 +02001833 memcpy(ini->peer_systemid, pclc->lcl.id_for_peer, SMC_SYSTEMID_LEN);
1834 memcpy(ini->peer_gid, pclc->lcl.gid, SMC_GID_SIZE);
1835 memcpy(ini->peer_mac, pclc->lcl.mac, ETH_ALEN);
Ursula Braun7affc802020-09-26 12:44:21 +02001836 rc = smc_find_rdma_device(new_smc, ini);
1837 if (rc) {
1838 /* no RDMA device found */
Karsten Graule49300a2021-10-16 11:37:46 +02001839 return SMC_CLC_DECL_NOSMCDEV;
Ursula Braun7affc802020-09-26 12:44:21 +02001840 }
1841 rc = smc_listen_rdma_init(new_smc, ini);
1842 if (rc)
1843 return rc;
1844 return smc_listen_rdma_reg(new_smc, ini->first_contact_local);
1845}
1846
1847/* determine the local device matching to proposal */
1848static int smc_listen_find_device(struct smc_sock *new_smc,
1849 struct smc_clc_msg_proposal *pclc,
1850 struct smc_init_info *ini)
1851{
Karsten Graule49300a2021-10-16 11:37:46 +02001852 int prfx_rc;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001853
1854 /* check for ISM device matching V2 proposed device */
1855 smc_find_ism_v2_device_serv(new_smc, pclc, ini);
1856 if (ini->ism_dev[0])
Ursula Braun7affc802020-09-26 12:44:21 +02001857 return 0;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001858
Karsten Graule49300a2021-10-16 11:37:46 +02001859 /* check for matching IP prefix and subnet length (V1) */
1860 prfx_rc = smc_listen_prfx_check(new_smc, pclc);
1861 if (prfx_rc)
1862 smc_find_ism_store_rc(prfx_rc, ini);
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001863
1864 /* get vlan id from IP device */
1865 if (smc_vlan_by_tcpsk(new_smc->clcsock, ini))
Karsten Graul37524042020-10-31 19:19:38 +01001866 return ini->rc ?: SMC_CLC_DECL_GETVLANERR;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001867
1868 /* check for ISM device matching V1 proposed device */
Karsten Graule49300a2021-10-16 11:37:46 +02001869 if (!prfx_rc)
1870 smc_find_ism_v1_device_serv(new_smc, pclc, ini);
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001871 if (ini->ism_dev[0])
1872 return 0;
1873
Karsten Graule49300a2021-10-16 11:37:46 +02001874 if (!smcr_indicated(pclc->hdr.typev1) &&
1875 !smcr_indicated(pclc->hdr.typev2))
Karsten Graul37524042020-10-31 19:19:38 +01001876 /* skip RDMA and decline */
1877 return ini->rc ?: SMC_CLC_DECL_NOSMCDDEV;
Ursula Braun7affc802020-09-26 12:44:21 +02001878
Karsten Graule49300a2021-10-16 11:37:46 +02001879 /* check if RDMA V2 is available */
1880 smc_find_rdma_v2_device_serv(new_smc, pclc, ini);
1881 if (ini->smcrv2.ib_dev_v2)
1882 return 0;
Karsten Graul37524042020-10-31 19:19:38 +01001883
Karsten Graule49300a2021-10-16 11:37:46 +02001884 /* check if RDMA V1 is available */
1885 if (!prfx_rc) {
1886 int rc;
1887
1888 rc = smc_find_rdma_v1_device_serv(new_smc, pclc, ini);
1889 smc_find_ism_store_rc(rc, ini);
1890 return (!rc) ? 0 : ini->rc;
1891 }
1892 return SMC_CLC_DECL_NOSMCDEV;
Ursula Braun7affc802020-09-26 12:44:21 +02001893}
1894
Hans Wippel3b2dec22018-05-18 09:34:18 +02001895/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001896static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1897 struct smc_clc_msg_accept_confirm *cclc,
Karsten Graule49300a2021-10-16 11:37:46 +02001898 bool local_first,
1899 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001900{
Karsten Graul387707f2020-04-29 17:10:40 +02001901 struct smc_link *link = new_smc->conn.lnk;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001902 int reason_code = 0;
1903
Ursula Braun5ac54d82020-09-10 18:48:21 +02001904 if (local_first)
Karsten Graule49300a2021-10-16 11:37:46 +02001905 smc_link_save_peer_info(link, cclc, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001906
Ursula Braun0c881ad2020-09-10 18:48:25 +02001907 if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc))
1908 return SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001909
Ursula Braun5ac54d82020-09-10 18:48:21 +02001910 if (local_first) {
Ursula Braun0c881ad2020-09-10 18:48:25 +02001911 if (smc_ib_ready_link(link))
1912 return SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001913 /* QP confirmation over RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001914 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
Karsten Graulb9247542020-04-29 17:10:41 +02001915 reason_code = smcr_serv_conf_first_link(new_smc);
Karsten Graul4667bb42020-04-30 15:55:42 +02001916 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001917 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001918 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001919}
1920
Ursula Braun7affc802020-09-26 12:44:21 +02001921/* setup for connection of server */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001922static void smc_listen_work(struct work_struct *work)
1923{
1924 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1925 smc_listen_work);
1926 struct socket *newclcsock = new_smc->clcsock;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001927 struct smc_clc_msg_accept_confirm *cclc;
Ursula Braun6bb14e482020-09-10 18:48:22 +02001928 struct smc_clc_msg_proposal_area *buf;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001929 struct smc_clc_msg_proposal *pclc;
Ursula Braun3fc64932020-09-26 12:44:23 +02001930 struct smc_init_info *ini = NULL;
Karsten Graule49300a2021-10-16 11:37:46 +02001931 u8 proposal_version = SMC_V1;
1932 u8 accept_version;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001933 int rc = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001934
Karsten Graulfd577702019-04-11 11:17:30 +02001935 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1936 return smc_listen_out_err(new_smc);
1937
Hans Wippel3b2dec22018-05-18 09:34:18 +02001938 if (new_smc->use_fallback) {
1939 smc_listen_out_connected(new_smc);
1940 return;
1941 }
1942
1943 /* check if peer is smc capable */
1944 if (!tcp_sk(newclcsock->sk)->syn_smc) {
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02001945 smc_switch_to_fallback(new_smc, SMC_CLC_DECL_PEERNOSMC);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001946 smc_listen_out_connected(new_smc);
1947 return;
1948 }
1949
1950 /* do inband token exchange -
1951 * wait for and receive SMC Proposal CLC message
1952 */
Ursula Braun6bb14e482020-09-10 18:48:22 +02001953 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
1954 if (!buf) {
1955 rc = SMC_CLC_DECL_MEM;
1956 goto out_decl;
1957 }
1958 pclc = (struct smc_clc_msg_proposal *)buf;
1959 rc = smc_clc_wait_msg(new_smc, pclc, sizeof(*buf),
Karsten Graul228bae02019-04-12 12:57:28 +02001960 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001961 if (rc)
1962 goto out_decl;
Karsten Graule49300a2021-10-16 11:37:46 +02001963
1964 if (pclc->hdr.version > SMC_V1)
1965 proposal_version = SMC_V2;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001966
Ursula Braune8d726c82020-09-26 12:44:32 +02001967 /* IPSec connections opt out of SMC optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001968 if (using_ipsec(new_smc)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001969 rc = SMC_CLC_DECL_IPSEC;
1970 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001971 }
1972
Ursula Braun3fc64932020-09-26 12:44:23 +02001973 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
1974 if (!ini) {
1975 rc = SMC_CLC_DECL_MEM;
1976 goto out_decl;
1977 }
1978
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001979 /* initial version checking */
1980 rc = smc_listen_v2_check(new_smc, pclc, ini);
1981 if (rc)
Karsten Graul9aa68d22019-04-12 12:57:29 +02001982 goto out_decl;
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001983
Hans Wippel72a36a82019-02-07 15:56:18 +01001984 mutex_lock(&smc_server_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001985 smc_close_init(new_smc);
1986 smc_rx_init(new_smc);
1987 smc_tx_init(new_smc);
1988
Ursula Braun7affc802020-09-26 12:44:21 +02001989 /* determine ISM or RoCE device used for connection */
Ursula Braun3fc64932020-09-26 12:44:23 +02001990 rc = smc_listen_find_device(new_smc, pclc, ini);
Ursula Braun7affc802020-09-26 12:44:21 +02001991 if (rc)
1992 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001993
1994 /* send SMC Accept CLC message */
Karsten Graule49300a2021-10-16 11:37:46 +02001995 accept_version = ini->is_smcd ? ini->smcd_version : ini->smcr_version;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001996 rc = smc_clc_send_accept(new_smc, ini->first_contact_local,
Karsten Graule49300a2021-10-16 11:37:46 +02001997 accept_version, ini->negotiated_eid);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001998 if (rc)
1999 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02002000
Hans Wippel62c71392019-02-07 15:56:17 +01002001 /* SMC-D does not need this lock any more */
Ursula Braun3fc64932020-09-26 12:44:23 +02002002 if (ini->is_smcd)
Hans Wippel72a36a82019-02-07 15:56:18 +01002003 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01002004
Hans Wippel3b2dec22018-05-18 09:34:18 +02002005 /* receive SMC Confirm CLC message */
Karsten Graul9047a617dc2020-10-07 22:57:42 +02002006 memset(buf, 0, sizeof(*buf));
2007 cclc = (struct smc_clc_msg_accept_confirm *)buf;
2008 rc = smc_clc_wait_msg(new_smc, cclc, sizeof(*buf),
Karsten Graul228bae02019-04-12 12:57:28 +02002009 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
2010 if (rc) {
Ursula Braun3fc64932020-09-26 12:44:23 +02002011 if (!ini->is_smcd)
Karsten Graul9aa68d22019-04-12 12:57:29 +02002012 goto out_unlock;
2013 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02002014 }
2015
2016 /* finish worker */
Ursula Braun3fc64932020-09-26 12:44:23 +02002017 if (!ini->is_smcd) {
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02002018 rc = smc_listen_rdma_finish(new_smc, cclc,
Karsten Graule49300a2021-10-16 11:37:46 +02002019 ini->first_contact_local, ini);
Hans Wippel62c71392019-02-07 15:56:17 +01002020 if (rc)
Ursula Braun0c881ad2020-09-10 18:48:25 +02002021 goto out_unlock;
2022 mutex_unlock(&smc_server_lgr_pending);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02002023 }
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02002024 smc_conn_save_peer_info(new_smc, cclc);
Hans Wippel3b2dec22018-05-18 09:34:18 +02002025 smc_listen_out_connected(new_smc);
Guvenc Gulce194730a2021-06-16 16:52:58 +02002026 SMC_STAT_SERV_SUCC_INC(sock_net(newclcsock->sk), ini);
Ursula Braunac679362020-09-17 22:46:02 +02002027 goto out_free;
Karsten Graul9aa68d22019-04-12 12:57:29 +02002028
2029out_unlock:
2030 mutex_unlock(&smc_server_lgr_pending);
2031out_decl:
Karsten Graul4a9baf42020-10-23 20:48:28 +02002032 smc_listen_decline(new_smc, rc, ini ? ini->first_contact_local : 0,
Karsten Graule49300a2021-10-16 11:37:46 +02002033 proposal_version);
Ursula Braunac679362020-09-17 22:46:02 +02002034out_free:
Ursula Braun3fc64932020-09-26 12:44:23 +02002035 kfree(ini);
Ursula Braun6bb14e482020-09-10 18:48:22 +02002036 kfree(buf);
Ursula Brauna046d572017-01-09 16:55:16 +01002037}
2038
2039static void smc_tcp_listen_work(struct work_struct *work)
2040{
2041 struct smc_sock *lsmc = container_of(work, struct smc_sock,
2042 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01002043 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01002044 struct smc_sock *new_smc;
2045 int rc = 0;
2046
Ursula Braun3163c502018-01-24 10:28:12 +01002047 lock_sock(lsk);
2048 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01002049 rc = smc_clcsock_accept(lsmc, &new_smc);
Ursula Brauna60a2b12020-09-10 18:48:20 +02002050 if (rc) /* clcsock accept queue empty or error */
Ursula Brauna046d572017-01-09 16:55:16 +01002051 goto out;
2052 if (!new_smc)
2053 continue;
2054
2055 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002056 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02002057 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01002058 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01002059 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
2060 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02002061 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
2062 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01002063 sock_hold(&new_smc->sk); /* sock_put in passive closing */
Karsten Graul22ef4732020-09-10 18:48:29 +02002064 if (!queue_work(smc_hs_wq, &new_smc->smc_listen_work))
Ursula Braun51f1de72018-01-26 09:28:48 +01002065 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01002066 }
2067
2068out:
Ursula Braun3163c502018-01-24 10:28:12 +01002069 release_sock(lsk);
Ursula Brauna60a2b12020-09-10 18:48:20 +02002070 sock_put(&lsmc->sk); /* sock_hold in smc_clcsock_data_ready() */
2071}
2072
2073static void smc_clcsock_data_ready(struct sock *listen_clcsock)
2074{
2075 struct smc_sock *lsmc;
2076
2077 lsmc = (struct smc_sock *)
2078 ((uintptr_t)listen_clcsock->sk_user_data & ~SK_USER_DATA_NOCOPY);
2079 if (!lsmc)
2080 return;
2081 lsmc->clcsk_data_ready(listen_clcsock);
2082 if (lsmc->sk.sk_state == SMC_LISTEN) {
2083 sock_hold(&lsmc->sk); /* sock_put in smc_tcp_listen_work() */
Karsten Graul22ef4732020-09-10 18:48:29 +02002084 if (!queue_work(smc_hs_wq, &lsmc->tcp_listen_work))
Ursula Brauna60a2b12020-09-10 18:48:20 +02002085 sock_put(&lsmc->sk);
2086 }
Ursula Brauna046d572017-01-09 16:55:16 +01002087}
2088
Ursula Braunac713872017-01-09 16:55:13 +01002089static int smc_listen(struct socket *sock, int backlog)
2090{
2091 struct sock *sk = sock->sk;
2092 struct smc_sock *smc;
2093 int rc;
2094
2095 smc = smc_sk(sk);
2096 lock_sock(sk);
2097
2098 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +02002099 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
2100 smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +01002101 goto out;
2102
2103 rc = 0;
2104 if (sk->sk_state == SMC_LISTEN) {
2105 sk->sk_max_ack_backlog = backlog;
2106 goto out;
2107 }
2108 /* some socket options are handled in core, so we could not apply
2109 * them to the clc socket -- copy smc socket options to clc socket
2110 */
2111 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002112 if (!smc->use_fallback)
2113 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01002114
Ursula Brauna60a2b12020-09-10 18:48:20 +02002115 /* save original sk_data_ready function and establish
2116 * smc-specific sk_data_ready function
2117 */
2118 smc->clcsk_data_ready = smc->clcsock->sk->sk_data_ready;
2119 smc->clcsock->sk->sk_data_ready = smc_clcsock_data_ready;
2120 smc->clcsock->sk->sk_user_data =
2121 (void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY);
Ursula Braunac713872017-01-09 16:55:13 +01002122 rc = kernel_listen(smc->clcsock, backlog);
2123 if (rc)
2124 goto out;
2125 sk->sk_max_ack_backlog = backlog;
2126 sk->sk_ack_backlog = 0;
2127 sk->sk_state = SMC_LISTEN;
2128
2129out:
2130 release_sock(sk);
2131 return rc;
2132}
2133
2134static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00002135 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01002136{
Ursula Brauna046d572017-01-09 16:55:16 +01002137 struct sock *sk = sock->sk, *nsk;
2138 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01002139 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01002140 long timeo;
2141 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01002142
2143 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01002144 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01002145 lock_sock(sk);
2146
2147 if (lsmc->sk.sk_state != SMC_LISTEN) {
2148 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02002149 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01002150 goto out;
2151 }
2152
Ursula Brauna046d572017-01-09 16:55:16 +01002153 /* Wait for an incoming connection */
2154 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2155 add_wait_queue_exclusive(sk_sleep(sk), &wait);
2156 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
2157 set_current_state(TASK_INTERRUPTIBLE);
2158 if (!timeo) {
2159 rc = -EAGAIN;
2160 break;
2161 }
2162 release_sock(sk);
2163 timeo = schedule_timeout(timeo);
2164 /* wakeup by sk_data_ready in smc_listen_work() */
2165 sched_annotate_sleep();
2166 lock_sock(sk);
2167 if (signal_pending(current)) {
2168 rc = sock_intr_errno(timeo);
2169 break;
2170 }
2171 }
2172 set_current_state(TASK_RUNNING);
2173 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01002174
Ursula Brauna046d572017-01-09 16:55:16 +01002175 if (!rc)
2176 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02002177 release_sock(sk);
2178 if (rc)
2179 goto out;
2180
2181 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
2182 /* wait till data arrives on the socket */
2183 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
2184 MSEC_PER_SEC);
2185 if (smc_sk(nsk)->use_fallback) {
2186 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
2187
2188 lock_sock(clcsk);
2189 if (skb_queue_empty(&clcsk->sk_receive_queue))
2190 sk_wait_data(clcsk, &timeo, NULL);
2191 release_sock(clcsk);
2192 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
2193 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02002194 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02002195 release_sock(nsk);
2196 }
2197 }
Ursula Braunac713872017-01-09 16:55:13 +01002198
2199out:
Ursula Braun51f1de72018-01-26 09:28:48 +01002200 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01002201 return rc;
2202}
2203
2204static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01002205 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01002206{
2207 struct smc_sock *smc;
2208
Ursula Braunb38d7322017-01-09 16:55:25 +01002209 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
2210 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01002211 return -ENOTCONN;
2212
2213 smc = smc_sk(sock->sk);
2214
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01002215 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01002216}
2217
2218static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
2219{
2220 struct sock *sk = sock->sk;
2221 struct smc_sock *smc;
2222 int rc = -EPIPE;
2223
2224 smc = smc_sk(sk);
2225 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01002226 if ((sk->sk_state != SMC_ACTIVE) &&
2227 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2228 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01002229 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002230
2231 if (msg->msg_flags & MSG_FASTOPEN) {
Ursula Brauncd206362019-08-02 10:47:50 +02002232 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002233 smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002234 } else {
2235 rc = -EINVAL;
2236 goto out;
2237 }
2238 }
2239
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002240 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01002241 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002242 } else {
Ursula Braune6727f32017-01-09 16:55:23 +01002243 rc = smc_tx_sendmsg(smc, msg, len);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002244 SMC_STAT_TX_PAYLOAD(smc, len, rc);
2245 }
Ursula Braunac713872017-01-09 16:55:13 +01002246out:
2247 release_sock(sk);
2248 return rc;
2249}
2250
2251static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
2252 int flags)
2253{
2254 struct sock *sk = sock->sk;
2255 struct smc_sock *smc;
2256 int rc = -ENOTCONN;
2257
2258 smc = smc_sk(sk);
2259 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01002260 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2261 /* socket was connected before, no more data to read */
2262 rc = 0;
2263 goto out;
2264 }
Ursula Braunb38d7322017-01-09 16:55:25 +01002265 if ((sk->sk_state == SMC_INIT) ||
2266 (sk->sk_state == SMC_LISTEN) ||
2267 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01002268 goto out;
2269
Ursula Braunb38d7322017-01-09 16:55:25 +01002270 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2271 rc = 0;
2272 goto out;
2273 }
2274
Stefan Raspl9014db22018-05-03 18:12:39 +02002275 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01002276 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02002277 } else {
2278 msg->msg_namelen = 0;
2279 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002280 SMC_STAT_RX_PAYLOAD(smc, rc, rc);
Stefan Raspl9014db22018-05-03 18:12:39 +02002281 }
Ursula Braunb38d7322017-01-09 16:55:25 +01002282
Ursula Braunac713872017-01-09 16:55:13 +01002283out:
2284 release_sock(sk);
2285 return rc;
2286}
2287
Al Viroade994f2017-07-03 00:01:49 -04002288static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01002289{
Ursula Braun8dce2782018-01-26 09:28:47 +01002290 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05002291 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01002292
Ursula Braun8dce2782018-01-26 09:28:47 +01002293 spin_lock(&isk->accept_q_lock);
2294 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002295 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01002296 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01002297
Ursula Braun8dce2782018-01-26 09:28:47 +01002298 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01002299}
2300
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07002301static __poll_t smc_poll(struct file *file, struct socket *sock,
2302 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01002303{
2304 struct sock *sk = sock->sk;
Ursula Braunac713872017-01-09 16:55:13 +01002305 struct smc_sock *smc;
Ursula Braun50717a32019-04-12 12:57:23 +02002306 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01002307
Ursula Braun8dce2782018-01-26 09:28:47 +01002308 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002309 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01002310
Ursula Braunac713872017-01-09 16:55:13 +01002311 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02002312 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01002313 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07002314 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02002315 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braunac713872017-01-09 16:55:13 +01002316 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07002317 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02002318 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01002319 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002320 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01002321 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
2322 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002323 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01002324 if (sk->sk_state == SMC_LISTEN) {
2325 /* woken up by sk_data_ready in smc_listen_work() */
Ursula Braun50717a32019-04-12 12:57:23 +02002326 mask |= smc_accept_poll(sk);
2327 } else if (smc->use_fallback) { /* as result of connect_work()*/
2328 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
2329 wait);
2330 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun8dce2782018-01-26 09:28:47 +01002331 } else {
Ursula Braun50717a32019-04-12 12:57:23 +02002332 if ((sk->sk_state != SMC_INIT &&
2333 atomic_read(&smc->conn.sndbuf_space)) ||
Ursula Braun8dce2782018-01-26 09:28:47 +01002334 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002335 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01002336 } else {
2337 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
2338 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
2339 }
2340 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002341 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01002342 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002343 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01002344 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002345 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02002346 if (smc->conn.urg_state == SMC_URG_VALID)
2347 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01002348 }
Ursula Braunac713872017-01-09 16:55:13 +01002349 }
2350
2351 return mask;
2352}
2353
2354static int smc_shutdown(struct socket *sock, int how)
2355{
2356 struct sock *sk = sock->sk;
2357 struct smc_sock *smc;
2358 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01002359 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01002360
2361 smc = smc_sk(sk);
2362
2363 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01002364 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01002365
2366 lock_sock(sk);
2367
2368 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02002369 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01002370 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
2371 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
2372 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2373 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
2374 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01002375 goto out;
2376 if (smc->use_fallback) {
2377 rc = kernel_sock_shutdown(smc->clcsock, how);
2378 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
2379 if (sk->sk_shutdown == SHUTDOWN_MASK)
2380 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01002381 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01002382 }
Ursula Braunb38d7322017-01-09 16:55:25 +01002383 switch (how) {
2384 case SHUT_RDWR: /* shutdown in both directions */
2385 rc = smc_close_active(smc);
2386 break;
2387 case SHUT_WR:
2388 rc = smc_close_shutdown_write(smc);
2389 break;
2390 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02002391 rc = 0;
2392 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01002393 break;
2394 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02002395 if (smc->clcsock)
2396 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01002397 /* map sock_shutdown_cmd constants to sk_shutdown value range */
2398 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01002399
2400out:
2401 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01002402 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01002403}
2404
2405static int smc_setsockopt(struct socket *sock, int level, int optname,
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02002406 sockptr_t optval, unsigned int optlen)
Ursula Braunac713872017-01-09 16:55:13 +01002407{
2408 struct sock *sk = sock->sk;
2409 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002410 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01002411
Cong Wang86214362021-05-05 12:40:48 -07002412 if (level == SOL_TCP && optname == TCP_ULP)
2413 return -EOPNOTSUPP;
2414
Ursula Braunac713872017-01-09 16:55:13 +01002415 smc = smc_sk(sk);
2416
2417 /* generic setsockopts reaching us here always apply to the
2418 * CLC socket
2419 */
Christoph Hellwiga44d9e72020-07-17 08:23:31 +02002420 if (unlikely(!smc->clcsock->ops->setsockopt))
2421 rc = -EOPNOTSUPP;
2422 else
2423 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
2424 optval, optlen);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002425 if (smc->clcsock->sk->sk_err) {
2426 sk->sk_err = smc->clcsock->sk->sk_err;
Alexander Aringe3ae2362021-06-27 18:48:21 -04002427 sk_error_report(sk);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002428 }
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002429
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002430 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00002431 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02002432 if (copy_from_sockptr(&val, optval, sizeof(int)))
Ursula Braunac0107e2018-07-18 15:22:50 +02002433 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002434
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002435 lock_sock(sk);
Ursula Braun86434742019-12-12 22:35:58 +01002436 if (rc || smc->use_fallback)
2437 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002438 switch (optname) {
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002439 case TCP_FASTOPEN:
2440 case TCP_FASTOPEN_CONNECT:
2441 case TCP_FASTOPEN_KEY:
2442 case TCP_FASTOPEN_NO_COOKIE:
2443 /* option not supported by SMC */
Ursula Braun8204df72019-11-15 12:39:30 +01002444 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002445 smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002446 } else {
Ursula Braun86434742019-12-12 22:35:58 +01002447 rc = -EINVAL;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002448 }
2449 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002450 case TCP_NODELAY:
Ursula Braunf9cedf12019-08-02 10:16:38 +02002451 if (sk->sk_state != SMC_INIT &&
2452 sk->sk_state != SMC_LISTEN &&
2453 sk->sk_state != SMC_CLOSED) {
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002454 if (val) {
Guvenc Gulce194730a2021-06-16 16:52:58 +02002455 SMC_STAT_INC(smc, ndly_cnt);
Karsten Graul22ef4732020-09-10 18:48:29 +02002456 mod_delayed_work(smc->conn.lgr->tx_wq,
2457 &smc->conn.tx_work, 0);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002458 }
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002459 }
2460 break;
2461 case TCP_CORK:
Ursula Braunf9cedf12019-08-02 10:16:38 +02002462 if (sk->sk_state != SMC_INIT &&
2463 sk->sk_state != SMC_LISTEN &&
2464 sk->sk_state != SMC_CLOSED) {
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002465 if (!val) {
Guvenc Gulce194730a2021-06-16 16:52:58 +02002466 SMC_STAT_INC(smc, cork_cnt);
Karsten Graul22ef4732020-09-10 18:48:29 +02002467 mod_delayed_work(smc->conn.lgr->tx_wq,
2468 &smc->conn.tx_work, 0);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002469 }
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002470 }
2471 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02002472 case TCP_DEFER_ACCEPT:
2473 smc->sockopt_defer_accept = val;
2474 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002475 default:
2476 break;
2477 }
Ursula Braun86434742019-12-12 22:35:58 +01002478out:
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002479 release_sock(sk);
2480
2481 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01002482}
2483
2484static int smc_getsockopt(struct socket *sock, int level, int optname,
2485 char __user *optval, int __user *optlen)
2486{
2487 struct smc_sock *smc;
2488
2489 smc = smc_sk(sock->sk);
2490 /* socket options apply to the CLC socket */
Christoph Hellwiga44d9e72020-07-17 08:23:31 +02002491 if (unlikely(!smc->clcsock->ops->getsockopt))
2492 return -EOPNOTSUPP;
Ursula Braunac713872017-01-09 16:55:13 +01002493 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
2494 optval, optlen);
2495}
2496
2497static int smc_ioctl(struct socket *sock, unsigned int cmd,
2498 unsigned long arg)
2499{
Stefan Rasplde8474e2018-05-23 16:38:11 +02002500 union smc_host_cursor cons, urg;
2501 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01002502 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02002503 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01002504
2505 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02002506 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02002507 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02002508 if (smc->use_fallback) {
2509 if (!smc->clcsock) {
2510 release_sock(&smc->sk);
2511 return -EBADF;
2512 }
2513 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
2514 release_sock(&smc->sk);
2515 return answ;
2516 }
Ursula Braun9b67e262018-05-02 16:56:46 +02002517 switch (cmd) {
2518 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02002519 if (smc->sk.sk_state == SMC_LISTEN) {
2520 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002521 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002522 }
Ursula Braun2351abe2018-05-23 16:38:09 +02002523 if (smc->sk.sk_state == SMC_INIT ||
2524 smc->sk.sk_state == SMC_CLOSED)
2525 answ = 0;
2526 else
2527 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02002528 break;
2529 case SIOCOUTQ:
2530 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02002531 if (smc->sk.sk_state == SMC_LISTEN) {
2532 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002533 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002534 }
Ursula Braun2351abe2018-05-23 16:38:09 +02002535 if (smc->sk.sk_state == SMC_INIT ||
2536 smc->sk.sk_state == SMC_CLOSED)
2537 answ = 0;
2538 else
2539 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02002540 atomic_read(&smc->conn.sndbuf_space);
2541 break;
2542 case SIOCOUTQNSD:
2543 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02002544 if (smc->sk.sk_state == SMC_LISTEN) {
2545 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002546 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002547 }
Ursula Braun2351abe2018-05-23 16:38:09 +02002548 if (smc->sk.sk_state == SMC_INIT ||
2549 smc->sk.sk_state == SMC_CLOSED)
2550 answ = 0;
2551 else
2552 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02002553 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02002554 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02002555 if (smc->sk.sk_state == SMC_LISTEN) {
2556 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02002557 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002558 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02002559 if (smc->sk.sk_state == SMC_INIT ||
2560 smc->sk.sk_state == SMC_CLOSED) {
2561 answ = 0;
2562 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02002563 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
2564 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02002565 answ = smc_curs_diff(conn->rmb_desc->len,
2566 &cons, &urg) == 1;
2567 }
2568 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02002569 default:
Ursula Braun1992d992018-07-16 13:56:52 +02002570 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002571 return -ENOIOCTLCMD;
2572 }
Ursula Braun1992d992018-07-16 13:56:52 +02002573 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002574
2575 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01002576}
2577
2578static ssize_t smc_sendpage(struct socket *sock, struct page *page,
2579 int offset, size_t size, int flags)
2580{
2581 struct sock *sk = sock->sk;
2582 struct smc_sock *smc;
2583 int rc = -EPIPE;
2584
2585 smc = smc_sk(sk);
2586 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02002587 if (sk->sk_state != SMC_ACTIVE) {
2588 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01002589 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02002590 }
2591 release_sock(sk);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002592 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01002593 rc = kernel_sendpage(smc->clcsock, page, offset,
2594 size, flags);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002595 } else {
Guvenc Gulce194730a2021-06-16 16:52:58 +02002596 SMC_STAT_INC(smc, sendpage_cnt);
Ursula Braunac713872017-01-09 16:55:13 +01002597 rc = sock_no_sendpage(sock, page, offset, size, flags);
Guvenc Gulcee0e4b8f2021-06-16 16:52:55 +02002598 }
Ursula Braunac713872017-01-09 16:55:13 +01002599
2600out:
Ursula Braunac713872017-01-09 16:55:13 +01002601 return rc;
2602}
2603
Stefan Raspl9014db22018-05-03 18:12:39 +02002604/* Map the affected portions of the rmbe into an spd, note the number of bytes
2605 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
2606 * updates till whenever a respective page has been fully processed.
2607 * Note that subsequent recv() calls have to wait till all splice() processing
2608 * completed.
2609 */
Ursula Braunac713872017-01-09 16:55:13 +01002610static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
2611 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02002612 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01002613{
2614 struct sock *sk = sock->sk;
2615 struct smc_sock *smc;
2616 int rc = -ENOTCONN;
2617
2618 smc = smc_sk(sk);
2619 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01002620 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2621 /* socket was connected before, no more data to read */
2622 rc = 0;
2623 goto out;
2624 }
Stefan Raspl9014db22018-05-03 18:12:39 +02002625 if (sk->sk_state == SMC_INIT ||
2626 sk->sk_state == SMC_LISTEN ||
2627 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01002628 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02002629
2630 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2631 rc = 0;
2632 goto out;
2633 }
2634
Ursula Braunac713872017-01-09 16:55:13 +01002635 if (smc->use_fallback) {
2636 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
2637 pipe, len, flags);
2638 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02002639 if (*ppos) {
2640 rc = -ESPIPE;
2641 goto out;
2642 }
2643 if (flags & SPLICE_F_NONBLOCK)
2644 flags = MSG_DONTWAIT;
2645 else
2646 flags = 0;
Guvenc Gulce194730a2021-06-16 16:52:58 +02002647 SMC_STAT_INC(smc, splice_cnt);
Stefan Raspl9014db22018-05-03 18:12:39 +02002648 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01002649 }
2650out:
2651 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02002652
Ursula Braunac713872017-01-09 16:55:13 +01002653 return rc;
2654}
2655
2656/* must look like tcp */
2657static const struct proto_ops smc_sock_ops = {
2658 .family = PF_SMC,
2659 .owner = THIS_MODULE,
2660 .release = smc_release,
2661 .bind = smc_bind,
2662 .connect = smc_connect,
2663 .socketpair = sock_no_socketpair,
2664 .accept = smc_accept,
2665 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07002666 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01002667 .ioctl = smc_ioctl,
2668 .listen = smc_listen,
2669 .shutdown = smc_shutdown,
2670 .setsockopt = smc_setsockopt,
2671 .getsockopt = smc_getsockopt,
2672 .sendmsg = smc_sendmsg,
2673 .recvmsg = smc_recvmsg,
2674 .mmap = sock_no_mmap,
2675 .sendpage = smc_sendpage,
2676 .splice_read = smc_splice_read,
2677};
2678
2679static int smc_create(struct net *net, struct socket *sock, int protocol,
2680 int kern)
2681{
Karsten Graulaaa4d332018-03-16 15:06:41 +01002682 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01002683 struct smc_sock *smc;
2684 struct sock *sk;
2685 int rc;
2686
2687 rc = -ESOCKTNOSUPPORT;
2688 if (sock->type != SOCK_STREAM)
2689 goto out;
2690
2691 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002692 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01002693 goto out;
2694
2695 rc = -ENOBUFS;
2696 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002697 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01002698 if (!sk)
2699 goto out;
2700
2701 /* create internal TCP socket for CLC handshake and fallback */
2702 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01002703 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02002704 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002705 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
2706 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01002707 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01002708 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01002709 goto out;
2710 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01002711 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
2712 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01002713
2714out:
2715 return rc;
2716}
2717
2718static const struct net_proto_family smc_sock_family_ops = {
2719 .family = PF_SMC,
2720 .owner = THIS_MODULE,
2721 .create = smc_create,
2722};
2723
Hans Wippel64e28b52019-02-21 13:01:02 +01002724unsigned int smc_net_id;
2725
2726static __net_init int smc_net_init(struct net *net)
2727{
2728 return smc_pnet_net_init(net);
2729}
2730
2731static void __net_exit smc_net_exit(struct net *net)
2732{
2733 smc_pnet_net_exit(net);
2734}
2735
Guvenc Gulce194730a2021-06-16 16:52:58 +02002736static __net_init int smc_net_stat_init(struct net *net)
2737{
2738 return smc_stats_init(net);
2739}
2740
2741static void __net_exit smc_net_stat_exit(struct net *net)
2742{
2743 smc_stats_exit(net);
2744}
2745
Hans Wippel64e28b52019-02-21 13:01:02 +01002746static struct pernet_operations smc_net_ops = {
2747 .init = smc_net_init,
2748 .exit = smc_net_exit,
2749 .id = &smc_net_id,
2750 .size = sizeof(struct smc_net),
2751};
2752
Guvenc Gulce194730a2021-06-16 16:52:58 +02002753static struct pernet_operations smc_net_stat_ops = {
2754 .init = smc_net_stat_init,
2755 .exit = smc_net_stat_exit,
2756};
2757
Ursula Braunac713872017-01-09 16:55:13 +01002758static int __init smc_init(void)
2759{
2760 int rc;
2761
Hans Wippel64e28b52019-02-21 13:01:02 +01002762 rc = register_pernet_subsys(&smc_net_ops);
2763 if (rc)
2764 return rc;
2765
Guvenc Gulce194730a2021-06-16 16:52:58 +02002766 rc = register_pernet_subsys(&smc_net_stat_ops);
2767 if (rc)
2768 return rc;
2769
Ursula Braun201091e2020-09-26 12:44:24 +02002770 smc_ism_init();
Ursula Braunb81a5eb2020-09-26 12:44:31 +02002771 smc_clc_init();
Ursula Braun201091e2020-09-26 12:44:24 +02002772
Guvenc Gulcee8372d92020-12-01 20:20:43 +01002773 rc = smc_nl_init();
Thomas Richter6812baa2017-01-09 16:55:15 +01002774 if (rc)
YueHaibing8c33bf12019-06-26 17:47:50 +02002775 goto out_pernet_subsys;
Thomas Richter6812baa2017-01-09 16:55:15 +01002776
Guvenc Gulcee8372d92020-12-01 20:20:43 +01002777 rc = smc_pnet_init();
2778 if (rc)
2779 goto out_nl;
2780
Karsten Graul22ef4732020-09-10 18:48:29 +02002781 rc = -ENOMEM;
2782 smc_hs_wq = alloc_workqueue("smc_hs_wq", 0, 0);
2783 if (!smc_hs_wq)
2784 goto out_pnet;
2785
2786 smc_close_wq = alloc_workqueue("smc_close_wq", 0, 0);
2787 if (!smc_close_wq)
2788 goto out_alloc_hs_wq;
2789
Ursula Braun6dabd402019-11-16 17:47:29 +01002790 rc = smc_core_init();
2791 if (rc) {
2792 pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
Guvenc Gulce194730a2021-06-16 16:52:58 +02002793 goto out_alloc_wqs;
Ursula Braun6dabd402019-11-16 17:47:29 +01002794 }
2795
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002796 rc = smc_llc_init();
2797 if (rc) {
2798 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002799 goto out_core;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002800 }
2801
Ursula Braun5f083182017-01-09 16:55:22 +01002802 rc = smc_cdc_init();
2803 if (rc) {
2804 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002805 goto out_core;
Ursula Braun5f083182017-01-09 16:55:22 +01002806 }
2807
Ursula Braunac713872017-01-09 16:55:13 +01002808 rc = proto_register(&smc_proto, 1);
2809 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01002810 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002811 goto out_core;
Ursula Braunac713872017-01-09 16:55:13 +01002812 }
2813
Karsten Graulaaa4d332018-03-16 15:06:41 +01002814 rc = proto_register(&smc_proto6, 1);
2815 if (rc) {
2816 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2817 goto out_proto;
2818 }
2819
Ursula Braunac713872017-01-09 16:55:13 +01002820 rc = sock_register(&smc_sock_family_ops);
2821 if (rc) {
2822 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002823 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01002824 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01002825 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002826 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01002827
Ursula Brauna4cf0442017-01-09 16:55:14 +01002828 rc = smc_ib_register_client();
2829 if (rc) {
2830 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2831 goto out_sock;
2832 }
2833
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002834 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002835 return 0;
2836
Ursula Brauna4cf0442017-01-09 16:55:14 +01002837out_sock:
2838 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002839out_proto6:
2840 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002841out_proto:
2842 proto_unregister(&smc_proto);
Ursula Braun6dabd402019-11-16 17:47:29 +01002843out_core:
2844 smc_core_exit();
Karsten Graul22ef4732020-09-10 18:48:29 +02002845out_alloc_wqs:
2846 destroy_workqueue(smc_close_wq);
2847out_alloc_hs_wq:
2848 destroy_workqueue(smc_hs_wq);
Thomas Richter6812baa2017-01-09 16:55:15 +01002849out_pnet:
2850 smc_pnet_exit();
Guvenc Gulcee8372d92020-12-01 20:20:43 +01002851out_nl:
2852 smc_nl_exit();
YueHaibing8c33bf12019-06-26 17:47:50 +02002853out_pernet_subsys:
2854 unregister_pernet_subsys(&smc_net_ops);
2855
Ursula Braunac713872017-01-09 16:55:13 +01002856 return rc;
2857}
2858
2859static void __exit smc_exit(void)
2860{
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002861 static_branch_disable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002862 sock_unregister(PF_SMC);
Ursula Braun6dabd402019-11-16 17:47:29 +01002863 smc_core_exit();
2864 smc_ib_unregister_client();
Karsten Graul22ef4732020-09-10 18:48:29 +02002865 destroy_workqueue(smc_close_wq);
2866 destroy_workqueue(smc_hs_wq);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002867 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002868 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002869 smc_pnet_exit();
Guvenc Gulcee8372d92020-12-01 20:20:43 +01002870 smc_nl_exit();
Karsten Graulfa086662021-09-14 10:35:05 +02002871 smc_clc_exit();
Guvenc Gulce194730a2021-06-16 16:52:58 +02002872 unregister_pernet_subsys(&smc_net_stat_ops);
Hans Wippel64e28b52019-02-21 13:01:02 +01002873 unregister_pernet_subsys(&smc_net_ops);
Ursula Braun4ead9c92019-11-16 17:47:31 +01002874 rcu_barrier();
Ursula Braunac713872017-01-09 16:55:13 +01002875}
2876
2877module_init(smc_init);
2878module_exit(smc_exit);
2879
2880MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2881MODULE_DESCRIPTION("smc socket address family");
2882MODULE_LICENSE("GPL");
2883MODULE_ALIAS_NETPROTO(PF_SMC);