blob: 8c1cde36adb80c137f548ecb7c1364b1bac73a2f [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Ursula Braunac713872017-01-09 16:55:13 +01002/*
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
4 *
5 * AF_SMC protocol family socket handler keeping the AF_INET sock address type
6 * applies to SOCK_STREAM sockets only
7 * offers an alternative communication option for TCP-protocol sockets
8 * applicable with RoCE-cards only
9 *
Ursula Brauna046d572017-01-09 16:55:16 +010010 * Initial restrictions:
Ursula Brauna046d572017-01-09 16:55:16 +010011 * - support for alternate links postponed
Ursula Brauna046d572017-01-09 16:55:16 +010012 *
Karsten Graulaaa4d332018-03-16 15:06:41 +010013 * Copyright IBM Corp. 2016, 2018
Ursula Braunac713872017-01-09 16:55:13 +010014 *
15 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
16 * based on prototype from Frank Blaschka
17 */
18
19#define KMSG_COMPONENT "smc"
20#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
21
22#include <linux/module.h>
23#include <linux/socket.h>
Ursula Brauna046d572017-01-09 16:55:16 +010024#include <linux/workqueue.h>
Ursula Braun5f083182017-01-09 16:55:22 +010025#include <linux/in.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010026#include <linux/sched/signal.h>
Hans Wippel41349842018-06-28 19:05:11 +020027#include <linux/if_vlan.h>
Ursula Braun4ead9c92019-11-16 17:47:31 +010028#include <linux/rcupdate_wait.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010029
Ursula Braunac713872017-01-09 16:55:13 +010030#include <net/sock.h>
Ursula Brauna046d572017-01-09 16:55:16 +010031#include <net/tcp.h>
Ursula Braunf16a7dd2017-01-09 16:55:26 +010032#include <net/smc.h>
Ursula Braun9b67e262018-05-02 16:56:46 +020033#include <asm/ioctls.h>
Ursula Braunac713872017-01-09 16:55:13 +010034
Hans Wippel64e28b52019-02-21 13:01:02 +010035#include <net/net_namespace.h>
36#include <net/netns/generic.h>
37#include "smc_netns.h"
38
Ursula Braunac713872017-01-09 16:55:13 +010039#include "smc.h"
Ursula Brauna046d572017-01-09 16:55:16 +010040#include "smc_clc.h"
Ursula Braun9bf9abe2017-01-09 16:55:21 +010041#include "smc_llc.h"
Ursula Braun5f083182017-01-09 16:55:22 +010042#include "smc_cdc.h"
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010043#include "smc_core.h"
Ursula Brauna4cf0442017-01-09 16:55:14 +010044#include "smc_ib.h"
Hans Wippel41349842018-06-28 19:05:11 +020045#include "smc_ism.h"
Thomas Richter6812baa2017-01-09 16:55:15 +010046#include "smc_pnet.h"
Ursula Braune6727f32017-01-09 16:55:23 +010047#include "smc_tx.h"
Ursula Braun952310c2017-01-09 16:55:24 +010048#include "smc_rx.h"
Ursula Braunb38d7322017-01-09 16:55:25 +010049#include "smc_close.h"
Ursula Braunac713872017-01-09 16:55:13 +010050
Hans Wippel72a36a82019-02-07 15:56:18 +010051static DEFINE_MUTEX(smc_server_lgr_pending); /* serialize link group
52 * creation on server
53 */
54static DEFINE_MUTEX(smc_client_lgr_pending); /* serialize link group
55 * creation on client
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010056 */
57
Karsten Graul22ef4732020-09-10 18:48:29 +020058struct workqueue_struct *smc_hs_wq; /* wq for handshake work */
59struct workqueue_struct *smc_close_wq; /* wq for close work */
60
Ursula Brauna046d572017-01-09 16:55:16 +010061static void smc_tcp_listen_work(struct work_struct *);
Ursula Braun24ac3a02018-06-27 17:59:50 +020062static void smc_connect_work(struct work_struct *);
Ursula Brauna046d572017-01-09 16:55:16 +010063
Ursula Braunac713872017-01-09 16:55:13 +010064static void smc_set_keepalive(struct sock *sk, int val)
65{
66 struct smc_sock *smc = smc_sk(sk);
67
68 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
69}
70
Ursula Braunf16a7dd2017-01-09 16:55:26 +010071static struct smc_hashinfo smc_v4_hashinfo = {
72 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
73};
74
Karsten Graulaaa4d332018-03-16 15:06:41 +010075static struct smc_hashinfo smc_v6_hashinfo = {
76 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
77};
78
Ursula Braunf16a7dd2017-01-09 16:55:26 +010079int smc_hash_sk(struct sock *sk)
80{
81 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
82 struct hlist_head *head;
83
84 head = &h->ht;
85
86 write_lock_bh(&h->lock);
87 sk_add_node(sk, head);
88 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
89 write_unlock_bh(&h->lock);
90
91 return 0;
92}
93EXPORT_SYMBOL_GPL(smc_hash_sk);
94
95void smc_unhash_sk(struct sock *sk)
96{
97 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
98
99 write_lock_bh(&h->lock);
100 if (sk_del_node_init(sk))
101 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
102 write_unlock_bh(&h->lock);
103}
104EXPORT_SYMBOL_GPL(smc_unhash_sk);
105
106struct proto smc_proto = {
Ursula Braunac713872017-01-09 16:55:13 +0100107 .name = "SMC",
108 .owner = THIS_MODULE,
109 .keepalive = smc_set_keepalive,
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100110 .hash = smc_hash_sk,
111 .unhash = smc_unhash_sk,
Ursula Braunac713872017-01-09 16:55:13 +0100112 .obj_size = sizeof(struct smc_sock),
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100113 .h.smc_hash = &smc_v4_hashinfo,
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800114 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Ursula Braunac713872017-01-09 16:55:13 +0100115};
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100116EXPORT_SYMBOL_GPL(smc_proto);
Ursula Braunac713872017-01-09 16:55:13 +0100117
Karsten Graulaaa4d332018-03-16 15:06:41 +0100118struct proto smc_proto6 = {
119 .name = "SMC6",
120 .owner = THIS_MODULE,
121 .keepalive = smc_set_keepalive,
122 .hash = smc_hash_sk,
123 .unhash = smc_unhash_sk,
124 .obj_size = sizeof(struct smc_sock),
125 .h.smc_hash = &smc_v6_hashinfo,
126 .slab_flags = SLAB_TYPESAFE_BY_RCU,
127};
128EXPORT_SYMBOL_GPL(smc_proto6);
129
Ursula Braunf536dff2019-10-23 15:44:05 +0200130static void smc_restore_fallback_changes(struct smc_sock *smc)
131{
Karsten Graul1ad24052020-07-18 15:06:18 +0200132 if (smc->clcsock->file) { /* non-accepted sockets have no file yet */
133 smc->clcsock->file->private_data = smc->sk.sk_socket;
134 smc->clcsock->file = NULL;
135 }
Ursula Braunf536dff2019-10-23 15:44:05 +0200136}
137
Ursula Braun39f41f32019-06-27 15:04:52 +0200138static int __smc_release(struct smc_sock *smc)
Ursula Braunac713872017-01-09 16:55:13 +0100139{
Ursula Braun39f41f32019-06-27 15:04:52 +0200140 struct sock *sk = &smc->sk;
Ursula Braunb38d7322017-01-09 16:55:25 +0100141 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +0100142
Ursula Braun51f1de72018-01-26 09:28:48 +0100143 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100144 rc = smc_close_active(smc);
145 sock_set_flag(sk, SOCK_DEAD);
146 sk->sk_shutdown |= SHUTDOWN_MASK;
Ursula Braunb03faa12019-02-07 15:56:15 +0100147 } else {
148 if (sk->sk_state != SMC_LISTEN && sk->sk_state != SMC_INIT)
149 sock_put(sk); /* passive closing */
150 if (sk->sk_state == SMC_LISTEN) {
151 /* wake up clcsock accept */
152 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
153 }
154 sk->sk_state = SMC_CLOSED;
155 sk->sk_state_change(sk);
Ursula Braunf536dff2019-10-23 15:44:05 +0200156 smc_restore_fallback_changes(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +0100157 }
Cong Wang26d92e92019-01-05 23:45:26 -0800158
159 sk->sk_prot->unhash(sk);
160
Ursula Braunb03faa12019-02-07 15:56:15 +0100161 if (sk->sk_state == SMC_CLOSED) {
162 if (smc->clcsock) {
Karsten Graulfd577702019-04-11 11:17:30 +0200163 release_sock(sk);
164 smc_clcsock_release(smc);
165 lock_sock(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800166 }
Ursula Braunb03faa12019-02-07 15:56:15 +0100167 if (!smc->use_fallback)
168 smc_conn_free(&smc->conn);
Ursula Braun51f1de72018-01-26 09:28:48 +0100169 }
Ursula Braunac713872017-01-09 16:55:13 +0100170
Ursula Braun39f41f32019-06-27 15:04:52 +0200171 return rc;
172}
173
174static int smc_release(struct socket *sock)
175{
176 struct sock *sk = sock->sk;
177 struct smc_sock *smc;
178 int rc = 0;
179
180 if (!sk)
181 goto out;
182
Ursula Braun81cf4f42019-10-21 16:13:15 +0200183 sock_hold(sk); /* sock_put below */
Ursula Braun39f41f32019-06-27 15:04:52 +0200184 smc = smc_sk(sk);
185
186 /* cleanup for a dangling non-blocking connect */
187 if (smc->connect_nonblock && sk->sk_state == SMC_INIT)
188 tcp_abort(smc->clcsock->sk, ECONNABORTED);
189 flush_work(&smc->connect_work);
190
191 if (sk->sk_state == SMC_LISTEN)
192 /* smc_close_non_accepted() is called and acquires
193 * sock lock for child sockets again
194 */
195 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
196 else
197 lock_sock(sk);
198
199 rc = __smc_release(smc);
200
Ursula Braunac713872017-01-09 16:55:13 +0100201 /* detach socket */
202 sock_orphan(sk);
203 sock->sk = NULL;
204 release_sock(sk);
205
Ursula Braun81cf4f42019-10-21 16:13:15 +0200206 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +0100207 sock_put(sk); /* final sock_put */
Ursula Braunac713872017-01-09 16:55:13 +0100208out:
Ursula Braunb38d7322017-01-09 16:55:25 +0100209 return rc;
Ursula Braunac713872017-01-09 16:55:13 +0100210}
211
212static void smc_destruct(struct sock *sk)
213{
214 if (sk->sk_state != SMC_CLOSED)
215 return;
216 if (!sock_flag(sk, SOCK_DEAD))
217 return;
218
219 sk_refcnt_debug_dec(sk);
220}
221
Karsten Graulaaa4d332018-03-16 15:06:41 +0100222static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
223 int protocol)
Ursula Braunac713872017-01-09 16:55:13 +0100224{
225 struct smc_sock *smc;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100226 struct proto *prot;
Ursula Braunac713872017-01-09 16:55:13 +0100227 struct sock *sk;
228
Karsten Graulaaa4d332018-03-16 15:06:41 +0100229 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
230 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
Ursula Braunac713872017-01-09 16:55:13 +0100231 if (!sk)
232 return NULL;
233
234 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
235 sk->sk_state = SMC_INIT;
236 sk->sk_destruct = smc_destruct;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100237 sk->sk_protocol = protocol;
Ursula Braunac713872017-01-09 16:55:13 +0100238 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100239 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200240 INIT_WORK(&smc->connect_work, smc_connect_work);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700241 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
Ursula Brauna046d572017-01-09 16:55:16 +0100242 INIT_LIST_HEAD(&smc->accept_q);
243 spin_lock_init(&smc->accept_q_lock);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700244 spin_lock_init(&smc->conn.send_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100245 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100246 sk_refcnt_debug_inc(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800247 mutex_init(&smc->clcsock_release_lock);
Ursula Braunac713872017-01-09 16:55:13 +0100248
249 return sk;
250}
251
252static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
253 int addr_len)
254{
255 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
256 struct sock *sk = sock->sk;
257 struct smc_sock *smc;
258 int rc;
259
260 smc = smc_sk(sk);
261
262 /* replicate tests from inet_bind(), to be safe wrt. future changes */
263 rc = -EINVAL;
264 if (addr_len < sizeof(struct sockaddr_in))
265 goto out;
266
267 rc = -EAFNOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100268 if (addr->sin_family != AF_INET &&
269 addr->sin_family != AF_INET6 &&
270 addr->sin_family != AF_UNSPEC)
271 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100272 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
Karsten Graulaaa4d332018-03-16 15:06:41 +0100273 if (addr->sin_family == AF_UNSPEC &&
274 addr->sin_addr.s_addr != htonl(INADDR_ANY))
Ursula Braunac713872017-01-09 16:55:13 +0100275 goto out;
276
277 lock_sock(sk);
278
279 /* Check if socket is already active */
280 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +0200281 if (sk->sk_state != SMC_INIT || smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +0100282 goto out_rel;
283
284 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
285 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
286
287out_rel:
288 release_sock(sk);
289out:
290 return rc;
291}
292
293static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
294 unsigned long mask)
295{
296 /* options we don't get control via setsockopt for */
297 nsk->sk_type = osk->sk_type;
298 nsk->sk_sndbuf = osk->sk_sndbuf;
299 nsk->sk_rcvbuf = osk->sk_rcvbuf;
300 nsk->sk_sndtimeo = osk->sk_sndtimeo;
301 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
302 nsk->sk_mark = osk->sk_mark;
303 nsk->sk_priority = osk->sk_priority;
304 nsk->sk_rcvlowat = osk->sk_rcvlowat;
305 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
306 nsk->sk_err = osk->sk_err;
307
308 nsk->sk_flags &= ~mask;
309 nsk->sk_flags |= osk->sk_flags & mask;
310}
311
312#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
313 (1UL << SOCK_KEEPOPEN) | \
314 (1UL << SOCK_LINGER) | \
315 (1UL << SOCK_BROADCAST) | \
316 (1UL << SOCK_TIMESTAMP) | \
317 (1UL << SOCK_DBG) | \
318 (1UL << SOCK_RCVTSTAMP) | \
319 (1UL << SOCK_RCVTSTAMPNS) | \
320 (1UL << SOCK_LOCALROUTE) | \
321 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
322 (1UL << SOCK_RXQ_OVFL) | \
323 (1UL << SOCK_WIFI_STATUS) | \
324 (1UL << SOCK_NOFCS) | \
Deepa Dinamani97184752019-02-02 07:34:51 -0800325 (1UL << SOCK_FILTER_LOCKED) | \
326 (1UL << SOCK_TSTAMP_NEW))
Ursula Braunac713872017-01-09 16:55:13 +0100327/* copy only relevant settings and flags of SOL_SOCKET level from smc to
328 * clc socket (since smc is not called for these options from net/core)
329 */
330static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
331{
332 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
333}
334
335#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
336 (1UL << SOCK_KEEPOPEN) | \
337 (1UL << SOCK_LINGER) | \
338 (1UL << SOCK_DBG))
339/* copy only settings and flags relevant for smc from clc to smc socket */
340static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
341{
342 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
343}
344
Karsten Graulb9247542020-04-29 17:10:41 +0200345/* register the new rmb on all links */
Karsten Graul7562a132020-05-01 12:48:01 +0200346static int smcr_lgr_reg_rmbs(struct smc_link *link,
Karsten Graulb9247542020-04-29 17:10:41 +0200347 struct smc_buf_desc *rmb_desc)
348{
Karsten Graul7562a132020-05-01 12:48:01 +0200349 struct smc_link_group *lgr = link->lgr;
350 int i, rc = 0;
Karsten Graulb9247542020-04-29 17:10:41 +0200351
Karsten Grauld5500662020-05-01 12:48:05 +0200352 rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY);
353 if (rc)
354 return rc;
355 /* protect against parallel smc_llc_cli_rkey_exchange() and
356 * parallel smcr_link_reg_rmb()
357 */
358 mutex_lock(&lgr->llc_conf_mutex);
Karsten Graulb9247542020-04-29 17:10:41 +0200359 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
Karsten Graul741a49a2020-07-18 15:06:16 +0200360 if (!smc_link_active(&lgr->lnk[i]))
Karsten Graulb9247542020-04-29 17:10:41 +0200361 continue;
Karsten Graul7562a132020-05-01 12:48:01 +0200362 rc = smcr_link_reg_rmb(&lgr->lnk[i], rmb_desc);
Karsten Graulb9247542020-04-29 17:10:41 +0200363 if (rc)
Karsten Graul7562a132020-05-01 12:48:01 +0200364 goto out;
Karsten Graulb9247542020-04-29 17:10:41 +0200365 }
Karsten Graul7562a132020-05-01 12:48:01 +0200366
367 /* exchange confirm_rkey msg with peer */
368 rc = smc_llc_do_confirm_rkey(link, rmb_desc);
369 if (rc) {
370 rc = -EFAULT;
371 goto out;
372 }
373 rmb_desc->is_conf_rkey = true;
374out:
Karsten Grauld5500662020-05-01 12:48:05 +0200375 mutex_unlock(&lgr->llc_conf_mutex);
376 smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
Karsten Graul7562a132020-05-01 12:48:01 +0200377 return rc;
Karsten Graulb9247542020-04-29 17:10:41 +0200378}
379
380static int smcr_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100381{
Karsten Graul387707f2020-04-29 17:10:40 +0200382 struct smc_link *link = smc->conn.lnk;
Karsten Graul0fb0b022020-04-30 15:55:43 +0200383 struct smc_llc_qentry *qentry;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100384 int rc;
385
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100386 /* receive CONFIRM LINK request from server over RoCE fabric */
Karsten Graul0fb0b022020-04-30 15:55:43 +0200387 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
388 SMC_LLC_CONFIRM_LINK);
389 if (!qentry) {
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100390 struct smc_clc_msg_decline dclc;
391
392 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100393 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100394 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100395 }
Karsten Graul649758f2020-05-04 14:18:48 +0200396 smc_llc_save_peer_uid(qentry);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200397 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_REQ);
398 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
399 if (rc)
Karsten Graul75d320d2018-03-01 13:51:31 +0100400 return SMC_CLC_DECL_RMBE_EC;
401
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100402 rc = smc_ib_modify_qp_rts(link);
403 if (rc)
Karsten Graul603cc142018-07-25 16:35:32 +0200404 return SMC_CLC_DECL_ERR_RDYLNK;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100405
406 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200407
Karsten Graul7562a132020-05-01 12:48:01 +0200408 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +0200409 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200410
Karsten Graul0fb0b022020-04-30 15:55:43 +0200411 /* confirm_rkey is implicit on 1st contact */
412 smc->conn.rmb_desc->is_conf_rkey = true;
413
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100414 /* send CONFIRM LINK response over RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200415 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100416 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200417 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100418
Karsten Graul0fb0b022020-04-30 15:55:43 +0200419 smc_llc_link_active(link);
Karsten Graul0a99be42020-05-05 15:01:20 +0200420 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200421
422 /* optional 2nd link, receive ADD LINK request from server */
423 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
424 SMC_LLC_ADD_LINK);
425 if (!qentry) {
Karsten Graul52bedf32018-03-01 13:51:32 +0100426 struct smc_clc_msg_decline dclc;
427
428 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100429 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200430 if (rc == -EAGAIN)
431 rc = 0; /* no DECLINE received, go with one link */
432 return rc;
Karsten Graul52bedf32018-03-01 13:51:32 +0100433 }
Karsten Graul0fb0b022020-04-30 15:55:43 +0200434 smc_llc_flow_qentry_clr(&link->lgr->llc_flow_lcl);
Karsten Graulb1570a82020-05-03 14:38:42 +0200435 smc_llc_cli_add_link(link, qentry);
Karsten Graul75d320d2018-03-01 13:51:31 +0100436 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100437}
438
Hans Wippel41349842018-06-28 19:05:11 +0200439static void smcr_conn_save_peer_info(struct smc_sock *smc,
440 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100441{
Ursula Braun3d9725a2020-09-10 18:48:23 +0200442 int bufsize = smc_uncompress_bufsize(clc->r0.rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200443
Ursula Braun3d9725a2020-09-10 18:48:23 +0200444 smc->conn.peer_rmbe_idx = clc->r0.rmbe_idx;
445 smc->conn.local_tx_ctrl.token = ntohl(clc->r0.rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200446 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100447 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200448 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100449}
450
Hans Wippel41349842018-06-28 19:05:11 +0200451static void smcd_conn_save_peer_info(struct smc_sock *smc,
452 struct smc_clc_msg_accept_confirm *clc)
453{
Ursula Braun3d9725a2020-09-10 18:48:23 +0200454 int bufsize = smc_uncompress_bufsize(clc->d0.dmbe_size);
Hans Wippel41349842018-06-28 19:05:11 +0200455
Ursula Braun3d9725a2020-09-10 18:48:23 +0200456 smc->conn.peer_rmbe_idx = clc->d0.dmbe_idx;
457 smc->conn.peer_token = clc->d0.token;
Hans Wippel41349842018-06-28 19:05:11 +0200458 /* msg header takes up space in the buffer */
459 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
460 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
461 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
462}
463
464static void smc_conn_save_peer_info(struct smc_sock *smc,
465 struct smc_clc_msg_accept_confirm *clc)
466{
467 if (smc->conn.lgr->is_smcd)
468 smcd_conn_save_peer_info(smc, clc);
469 else
470 smcr_conn_save_peer_info(smc, clc);
471}
472
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100473static void smc_link_save_peer_info(struct smc_link *link,
474 struct smc_clc_msg_accept_confirm *clc)
475{
Ursula Braun3d9725a2020-09-10 18:48:23 +0200476 link->peer_qpn = ntoh24(clc->r0.qpn);
477 memcpy(link->peer_gid, clc->r0.lcl.gid, SMC_GID_SIZE);
478 memcpy(link->peer_mac, clc->r0.lcl.mac, sizeof(link->peer_mac));
479 link->peer_psn = ntoh24(clc->r0.psn);
480 link->peer_mtu = clc->r0.qp_mtu;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100481}
482
Ursula Braun07603b22019-04-11 11:17:32 +0200483static void smc_switch_to_fallback(struct smc_sock *smc)
484{
485 smc->use_fallback = true;
486 if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
487 smc->clcsock->file = smc->sk.sk_socket->file;
488 smc->clcsock->file->private_data = smc->clcsock;
Ursula Braun67f562e2020-02-14 08:58:59 +0100489 smc->clcsock->wq.fasync_list =
490 smc->sk.sk_socket->wq.fasync_list;
Ursula Braun07603b22019-04-11 11:17:32 +0200491 }
492}
493
Hans Wippel3b2dec22018-05-18 09:34:18 +0200494/* fall back during connect */
Karsten Graul603cc142018-07-25 16:35:32 +0200495static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200496{
Ursula Braun07603b22019-04-11 11:17:32 +0200497 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +0200498 smc->fallback_rsn = reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200499 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200500 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200501 if (smc->sk.sk_state == SMC_INIT)
502 smc->sk.sk_state = SMC_ACTIVE;
503 return 0;
504}
505
506/* decline and fall back during connect */
507static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
508{
509 int rc;
510
Ursula Braune1bbdd52018-07-05 16:15:30 +0200511 if (reason_code < 0) { /* error, fallback is not possible */
512 if (smc->sk.sk_state == SMC_INIT)
513 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200514 return reason_code;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200515 }
Karsten Graul603cc142018-07-25 16:35:32 +0200516 if (reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200517 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200518 if (rc < 0) {
519 if (smc->sk.sk_state == SMC_INIT)
520 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200521 return rc;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200522 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200523 }
Karsten Graul603cc142018-07-25 16:35:32 +0200524 return smc_connect_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200525}
526
527/* abort connecting */
528static int smc_connect_abort(struct smc_sock *smc, int reason_code,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200529 int local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200530{
Ursula Braun51e3dfa2020-02-25 16:34:36 +0100531 bool is_smcd = smc->conn.lgr->is_smcd;
532
Ursula Braun5ac54d82020-09-10 18:48:21 +0200533 if (local_first)
Ursula Braun51e3dfa2020-02-25 16:34:36 +0100534 smc_lgr_cleanup_early(&smc->conn);
535 else
536 smc_conn_free(&smc->conn);
537 if (is_smcd)
Hans Wippel72a36a82019-02-07 15:56:18 +0100538 /* there is only one lgr role for SMC-D; use server lock */
539 mutex_unlock(&smc_server_lgr_pending);
540 else
541 mutex_unlock(&smc_client_lgr_pending);
542
Ursula Braun50717a32019-04-12 12:57:23 +0200543 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200544 return reason_code;
545}
546
547/* check if there is a rdma device available for this connection. */
548/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200549static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200550{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200551 /* PNET table look up: search active ib_device and port
552 * within same PNETID that also contains the ethernet device
553 * used for the internal TCP socket
554 */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200555 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +0200556 if (!ini->ib_dev)
557 return SMC_CLC_DECL_NOSMCRDEV;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200558 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200559}
560
Hans Wippel41349842018-06-28 19:05:11 +0200561/* check if there is an ISM device available for this connection. */
562/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200563static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200564{
565 /* Find ISM device with same PNETID as connecting interface */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200566 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
Ursula Braun3fc64932020-09-26 12:44:23 +0200567 if (!ini->ism_dev[0])
Karsten Graul9aa68d22019-04-12 12:57:29 +0200568 return SMC_CLC_DECL_NOSMCDDEV;
Hans Wippel41349842018-06-28 19:05:11 +0200569 return 0;
570}
571
572/* Check for VLAN ID and register it on ISM device just for CLC handshake */
573static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200574 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200575{
Ursula Braun3fc64932020-09-26 12:44:23 +0200576 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev[0], ini->vlan_id))
Karsten Graul7a627252019-04-12 12:57:30 +0200577 return SMC_CLC_DECL_ISMVLANERR;
Hans Wippel41349842018-06-28 19:05:11 +0200578 return 0;
579}
580
581/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
582 * used, the VLAN ID will be registered again during the connection setup.
583 */
584static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200585 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200586{
587 if (!is_smcd)
588 return 0;
Ursula Braun3fc64932020-09-26 12:44:23 +0200589 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev[0], ini->vlan_id))
Hans Wippel41349842018-06-28 19:05:11 +0200590 return SMC_CLC_DECL_CNFERR;
591 return 0;
592}
593
Hans Wippel3b2dec22018-05-18 09:34:18 +0200594/* CLC handshake during connect */
Hans Wippelc758dfd2018-06-28 19:05:09 +0200595static int smc_connect_clc(struct smc_sock *smc, int smc_type,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200596 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200597 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200598{
599 int rc = 0;
600
601 /* do inband token exchange */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200602 rc = smc_clc_send_proposal(smc, smc_type, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200603 if (rc)
604 return rc;
605 /* receive SMC Accept CLC message */
Ursula Braun2b59f582018-11-22 10:26:39 +0100606 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT,
607 CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200608}
609
Ursula Brauna046d572017-01-09 16:55:16 +0100610/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200611static int smc_connect_rdma(struct smc_sock *smc,
612 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200613 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200614{
Karsten Graul0fb0b022020-04-30 15:55:43 +0200615 int i, reason_code = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200616 struct smc_link *link;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200617
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200618 ini->is_smcd = false;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200619 ini->ib_lcl = &aclc->r0.lcl;
620 ini->ib_clcqpn = ntoh24(aclc->r0.qpn);
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200621 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200622
Hans Wippel72a36a82019-02-07 15:56:18 +0100623 mutex_lock(&smc_client_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200624 reason_code = smc_conn_create(smc, ini);
625 if (reason_code) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100626 mutex_unlock(&smc_client_lgr_pending);
627 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200628 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200629
630 smc_conn_save_peer_info(smc, aclc);
631
Ursula Braun5ac54d82020-09-10 18:48:21 +0200632 if (ini->first_contact_local) {
Karsten Graul0fb0b022020-04-30 15:55:43 +0200633 link = smc->conn.lnk;
634 } else {
635 /* set link that was assigned by server */
636 link = NULL;
637 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
638 struct smc_link *l = &smc->conn.lgr->lnk[i];
639
Ursula Braun3d9725a2020-09-10 18:48:23 +0200640 if (l->peer_qpn == ntoh24(aclc->r0.qpn) &&
641 !memcmp(l->peer_gid, &aclc->r0.lcl.gid,
642 SMC_GID_SIZE) &&
643 !memcmp(l->peer_mac, &aclc->r0.lcl.mac,
644 sizeof(l->peer_mac))) {
Karsten Graul0fb0b022020-04-30 15:55:43 +0200645 link = l;
646 break;
647 }
648 }
649 if (!link)
650 return smc_connect_abort(smc, SMC_CLC_DECL_NOSRVLINK,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200651 ini->first_contact_local);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200652 smc->conn.lnk = link;
653 }
654
Hans Wippel3b2dec22018-05-18 09:34:18 +0200655 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200656 if (smc_buf_create(smc, false))
Karsten Graul7a627252019-04-12 12:57:30 +0200657 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200658 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200659
Ursula Braun5ac54d82020-09-10 18:48:21 +0200660 if (ini->first_contact_local)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200661 smc_link_save_peer_info(link, aclc);
662
Karsten Graule07d31d2020-04-29 17:10:42 +0200663 if (smc_rmb_rtoken_handling(&smc->conn, link, aclc))
Karsten Graul603cc142018-07-25 16:35:32 +0200664 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200665 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200666
667 smc_close_init(smc);
668 smc_rx_init(smc);
669
Ursula Braun5ac54d82020-09-10 18:48:21 +0200670 if (ini->first_contact_local) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200671 if (smc_ib_ready_link(link))
Karsten Graul603cc142018-07-25 16:35:32 +0200672 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200673 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200674 } else {
Karsten Graul7562a132020-05-01 12:48:01 +0200675 if (smcr_lgr_reg_rmbs(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +0200676 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200677 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200678 }
679 smc_rmb_sync_sg_for_device(&smc->conn);
680
681 reason_code = smc_clc_send_confirm(smc);
682 if (reason_code)
Karsten Graul7a627252019-04-12 12:57:30 +0200683 return smc_connect_abort(smc, reason_code,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200684 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200685
686 smc_tx_init(smc);
687
Ursula Braun5ac54d82020-09-10 18:48:21 +0200688 if (ini->first_contact_local) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200689 /* QP confirmation over RoCE fabric */
Karsten Graul0fb0b022020-04-30 15:55:43 +0200690 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
Karsten Graulb9247542020-04-29 17:10:41 +0200691 reason_code = smcr_clnt_conf_first_link(smc);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200692 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200693 if (reason_code)
694 return smc_connect_abort(smc, reason_code,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200695 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200696 }
Hans Wippel72a36a82019-02-07 15:56:18 +0100697 mutex_unlock(&smc_client_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200698
699 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200700 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200701 if (smc->sk.sk_state == SMC_INIT)
702 smc->sk.sk_state = SMC_ACTIVE;
703
704 return 0;
705}
706
Hans Wippel41349842018-06-28 19:05:11 +0200707/* setup for ISM connection of client */
708static int smc_connect_ism(struct smc_sock *smc,
709 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200710 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200711{
Hans Wippel41349842018-06-28 19:05:11 +0200712 int rc = 0;
713
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200714 ini->is_smcd = true;
Ursula Braun3fc64932020-09-26 12:44:23 +0200715 ini->ism_peer_gid[0] = aclc->d0.gid;
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200716 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200717
Hans Wippel72a36a82019-02-07 15:56:18 +0100718 /* there is only one lgr role for SMC-D; use server lock */
719 mutex_lock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200720 rc = smc_conn_create(smc, ini);
721 if (rc) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100722 mutex_unlock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200723 return rc;
Hans Wippel72a36a82019-02-07 15:56:18 +0100724 }
Hans Wippel41349842018-06-28 19:05:11 +0200725
726 /* Create send and receive buffers */
Karsten Graul72b7f6c2020-07-26 20:34:28 +0200727 rc = smc_buf_create(smc, true);
728 if (rc)
729 return smc_connect_abort(smc, (rc == -ENOSPC) ?
730 SMC_CLC_DECL_MAX_DMB :
731 SMC_CLC_DECL_MEM,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200732 ini->first_contact_local);
Hans Wippel41349842018-06-28 19:05:11 +0200733
734 smc_conn_save_peer_info(smc, aclc);
735 smc_close_init(smc);
736 smc_rx_init(smc);
737 smc_tx_init(smc);
738
739 rc = smc_clc_send_confirm(smc);
740 if (rc)
Ursula Braun5ac54d82020-09-10 18:48:21 +0200741 return smc_connect_abort(smc, rc, ini->first_contact_local);
Hans Wippel72a36a82019-02-07 15:56:18 +0100742 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel41349842018-06-28 19:05:11 +0200743
744 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200745 smc->connect_nonblock = 0;
Hans Wippel41349842018-06-28 19:05:11 +0200746 if (smc->sk.sk_state == SMC_INIT)
747 smc->sk.sk_state = SMC_ACTIVE;
748
749 return 0;
750}
751
Hans Wippel3b2dec22018-05-18 09:34:18 +0200752/* perform steps before actually connecting */
753static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100754{
Hans Wippel41349842018-06-28 19:05:11 +0200755 bool ism_supported = false, rdma_supported = false;
Ursula Brauna046d572017-01-09 16:55:16 +0100756 struct smc_clc_msg_accept_confirm aclc;
Ursula Braun3fc64932020-09-26 12:44:23 +0200757 struct smc_init_info *ini = NULL;
Hans Wippel41349842018-06-28 19:05:11 +0200758 int smc_type;
Ursula Brauna046d572017-01-09 16:55:16 +0100759 int rc = 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100760
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200761 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +0200762 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200763
Hans Wippel3b2dec22018-05-18 09:34:18 +0200764 /* if peer has not signalled SMC-capability, fall back */
765 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +0200766 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200767
Ursula Brauna046d572017-01-09 16:55:16 +0100768 /* IPSec connections opt out of SMC-R optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200769 if (using_ipsec(smc))
770 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
Ursula Brauna046d572017-01-09 16:55:16 +0100771
Ursula Braun3fc64932020-09-26 12:44:23 +0200772 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
773 if (!ini)
774 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_MEM);
775
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200776 /* get vlan id from IP device */
Ursula Braun3fc64932020-09-26 12:44:23 +0200777 if (smc_vlan_by_tcpsk(smc->clcsock, ini)) {
778 kfree(ini);
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200779 return smc_connect_decline_fallback(smc,
780 SMC_CLC_DECL_GETVLANERR);
Ursula Braun3fc64932020-09-26 12:44:23 +0200781 }
Hans Wippel41349842018-06-28 19:05:11 +0200782
783 /* check if there is an ism device available */
Ursula Braun3fc64932020-09-26 12:44:23 +0200784 if (!smc_find_ism_device(smc, ini) &&
785 !smc_connect_ism_vlan_setup(smc, ini)) {
Hans Wippel41349842018-06-28 19:05:11 +0200786 /* ISM is supported for this connection */
787 ism_supported = true;
788 smc_type = SMC_TYPE_D;
789 }
790
791 /* check if there is a rdma device available */
Ursula Braun3fc64932020-09-26 12:44:23 +0200792 if (!smc_find_rdma_device(smc, ini)) {
Hans Wippel41349842018-06-28 19:05:11 +0200793 /* RDMA is supported for this connection */
794 rdma_supported = true;
795 if (ism_supported)
796 smc_type = SMC_TYPE_B; /* both */
797 else
798 smc_type = SMC_TYPE_R; /* only RDMA */
799 }
800
801 /* if neither ISM nor RDMA are supported, fallback */
Ursula Braun3fc64932020-09-26 12:44:23 +0200802 if (!rdma_supported && !ism_supported) {
803 kfree(ini);
Karsten Graul603cc142018-07-25 16:35:32 +0200804 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV);
Ursula Braun3fc64932020-09-26 12:44:23 +0200805 }
Ursula Brauna046d572017-01-09 16:55:16 +0100806
Hans Wippel3b2dec22018-05-18 09:34:18 +0200807 /* perform CLC handshake */
Ursula Braun3fc64932020-09-26 12:44:23 +0200808 rc = smc_connect_clc(smc, smc_type, &aclc, ini);
Hans Wippel41349842018-06-28 19:05:11 +0200809 if (rc) {
Ursula Braun3fc64932020-09-26 12:44:23 +0200810 smc_connect_ism_vlan_cleanup(smc, ism_supported, ini);
811 kfree(ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200812 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200813 }
Ursula Brauna046d572017-01-09 16:55:16 +0100814
Hans Wippel41349842018-06-28 19:05:11 +0200815 /* depending on previous steps, connect using rdma or ism */
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200816 if (rdma_supported && aclc.hdr.typev1 == SMC_TYPE_R)
Ursula Braun3fc64932020-09-26 12:44:23 +0200817 rc = smc_connect_rdma(smc, &aclc, ini);
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200818 else if (ism_supported && aclc.hdr.typev1 == SMC_TYPE_D)
Ursula Braun3fc64932020-09-26 12:44:23 +0200819 rc = smc_connect_ism(smc, &aclc, ini);
Hans Wippel41349842018-06-28 19:05:11 +0200820 else
Karsten Graul603cc142018-07-25 16:35:32 +0200821 rc = SMC_CLC_DECL_MODEUNSUPP;
Hans Wippel41349842018-06-28 19:05:11 +0200822 if (rc) {
Ursula Braun3fc64932020-09-26 12:44:23 +0200823 smc_connect_ism_vlan_cleanup(smc, ism_supported, ini);
824 kfree(ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200825 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200826 }
Ursula Brauna046d572017-01-09 16:55:16 +0100827
Ursula Braun3fc64932020-09-26 12:44:23 +0200828 smc_connect_ism_vlan_cleanup(smc, ism_supported, ini);
829 kfree(ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200830 return 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100831}
832
Ursula Braun24ac3a02018-06-27 17:59:50 +0200833static void smc_connect_work(struct work_struct *work)
834{
835 struct smc_sock *smc = container_of(work, struct smc_sock,
836 connect_work);
Ursula Braun50717a32019-04-12 12:57:23 +0200837 long timeo = smc->sk.sk_sndtimeo;
838 int rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200839
Ursula Braun50717a32019-04-12 12:57:23 +0200840 if (!timeo)
841 timeo = MAX_SCHEDULE_TIMEOUT;
842 lock_sock(smc->clcsock->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200843 if (smc->clcsock->sk->sk_err) {
844 smc->sk.sk_err = smc->clcsock->sk->sk_err;
Ursula Braun50717a32019-04-12 12:57:23 +0200845 } else if ((1 << smc->clcsock->sk->sk_state) &
846 (TCPF_SYN_SENT | TCP_SYN_RECV)) {
847 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
848 if ((rc == -EPIPE) &&
849 ((1 << smc->clcsock->sk->sk_state) &
850 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
851 rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200852 }
Ursula Braun50717a32019-04-12 12:57:23 +0200853 release_sock(smc->clcsock->sk);
854 lock_sock(&smc->sk);
855 if (rc != 0 || smc->sk.sk_err) {
856 smc->sk.sk_state = SMC_CLOSED;
857 if (rc == -EPIPE || rc == -EAGAIN)
858 smc->sk.sk_err = EPIPE;
859 else if (signal_pending(current))
860 smc->sk.sk_err = -sock_intr_errno(timeo);
Ursula Braun6d6dd522019-11-12 16:03:41 +0100861 sock_put(&smc->sk); /* passive closing */
Ursula Braun24ac3a02018-06-27 17:59:50 +0200862 goto out;
863 }
864
865 rc = __smc_connect(smc);
866 if (rc < 0)
867 smc->sk.sk_err = -rc;
868
869out:
Ursula Braun07603b22019-04-11 11:17:32 +0200870 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
871 if (smc->sk.sk_err) {
872 smc->sk.sk_state_change(&smc->sk);
873 } else { /* allow polling before and after fallback decision */
874 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
875 smc->sk.sk_write_space(&smc->sk);
876 }
877 }
Ursula Braun24ac3a02018-06-27 17:59:50 +0200878 release_sock(&smc->sk);
879}
880
Ursula Braunac713872017-01-09 16:55:13 +0100881static int smc_connect(struct socket *sock, struct sockaddr *addr,
882 int alen, int flags)
883{
884 struct sock *sk = sock->sk;
885 struct smc_sock *smc;
886 int rc = -EINVAL;
887
888 smc = smc_sk(sk);
889
890 /* separate smc parameter checking to be safe */
891 if (alen < sizeof(addr->sa_family))
892 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100893 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100894 goto out_err;
895
896 lock_sock(sk);
897 switch (sk->sk_state) {
898 default:
899 goto out;
900 case SMC_ACTIVE:
901 rc = -EISCONN;
902 goto out;
903 case SMC_INIT:
904 rc = 0;
905 break;
906 }
907
908 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200909 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun50717a32019-04-12 12:57:23 +0200910 if (smc->connect_nonblock) {
911 rc = -EALREADY;
912 goto out;
913 }
914 rc = kernel_connect(smc->clcsock, addr, alen, flags);
915 if (rc && rc != -EINPROGRESS)
916 goto out;
Ursula Braun301428e2019-10-29 12:41:26 +0100917
918 sock_hold(&smc->sk); /* sock put in passive closing */
Ursula Braun86434742019-12-12 22:35:58 +0100919 if (smc->use_fallback)
920 goto out;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200921 if (flags & O_NONBLOCK) {
Karsten Graul22ef4732020-09-10 18:48:29 +0200922 if (queue_work(smc_hs_wq, &smc->connect_work))
Ursula Braun50717a32019-04-12 12:57:23 +0200923 smc->connect_nonblock = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200924 rc = -EINPROGRESS;
925 } else {
Ursula Braun24ac3a02018-06-27 17:59:50 +0200926 rc = __smc_connect(smc);
927 if (rc < 0)
928 goto out;
929 else
930 rc = 0; /* success cases including fallback */
931 }
Ursula Braunac713872017-01-09 16:55:13 +0100932
933out:
934 release_sock(sk);
935out_err:
936 return rc;
937}
938
939static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
940{
Ursula Braun3163c502018-01-24 10:28:12 +0100941 struct socket *new_clcsock = NULL;
942 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100943 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -0800944 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +0100945
Ursula Braun3163c502018-01-24 10:28:12 +0100946 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100947 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100948 if (!new_sk) {
949 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100950 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100951 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100952 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100953 goto out;
954 }
955 *new_smc = smc_sk(new_sk);
956
Myungho Jung78abe3d2018-12-18 09:02:25 -0800957 mutex_lock(&lsmc->clcsock_release_lock);
958 if (lsmc->clcsock)
Ursula Brauna60a2b12020-09-10 18:48:20 +0200959 rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800960 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +0100961 lock_sock(lsk);
Ursula Brauna60a2b12020-09-10 18:48:20 +0200962 if (rc < 0 && rc != -EAGAIN)
Ursula Braun3163c502018-01-24 10:28:12 +0100963 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100964 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +0200965 new_sk->sk_prot->unhash(new_sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100966 if (new_clcsock)
967 sock_release(new_clcsock);
968 new_sk->sk_state = SMC_CLOSED;
969 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun51f1de72018-01-26 09:28:48 +0100970 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100971 *new_smc = NULL;
972 goto out;
973 }
974
Ursula Brauna60a2b12020-09-10 18:48:20 +0200975 /* new clcsock has inherited the smc listen-specific sk_data_ready
976 * function; switch it back to the original sk_data_ready function
977 */
978 new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready;
Ursula Braunac713872017-01-09 16:55:13 +0100979 (*new_smc)->clcsock = new_clcsock;
980out:
981 return rc;
982}
983
Ursula Brauna046d572017-01-09 16:55:16 +0100984/* add a just created sock to the accept queue of the listen sock as
985 * candidate for a following socket accept call from user space
986 */
987static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
988{
989 struct smc_sock *par = smc_sk(parent);
990
Ursula Braun51f1de72018-01-26 09:28:48 +0100991 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100992 spin_lock(&par->accept_q_lock);
993 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
994 spin_unlock(&par->accept_q_lock);
995 sk_acceptq_added(parent);
996}
997
998/* remove a socket from the accept queue of its parental listening socket */
999static void smc_accept_unlink(struct sock *sk)
1000{
1001 struct smc_sock *par = smc_sk(sk)->listen_smc;
1002
1003 spin_lock(&par->accept_q_lock);
1004 list_del_init(&smc_sk(sk)->accept_q);
1005 spin_unlock(&par->accept_q_lock);
1006 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001007 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +01001008}
1009
1010/* remove a sock from the accept queue to bind it to a new socket created
1011 * for a socket accept call from user space
1012 */
Ursula Braunb38d7322017-01-09 16:55:25 +01001013struct sock *smc_accept_dequeue(struct sock *parent,
1014 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +01001015{
1016 struct smc_sock *isk, *n;
1017 struct sock *new_sk;
1018
1019 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
1020 new_sk = (struct sock *)isk;
1021
1022 smc_accept_unlink(new_sk);
1023 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +02001024 new_sk->sk_prot->unhash(new_sk);
Ursula Braun127f4972018-01-26 09:28:49 +01001025 if (isk->clcsock) {
1026 sock_release(isk->clcsock);
1027 isk->clcsock = NULL;
1028 }
Ursula Braun51f1de72018-01-26 09:28:48 +01001029 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +01001030 continue;
1031 }
Ursula Braun07603b22019-04-11 11:17:32 +02001032 if (new_sock) {
Ursula Brauna046d572017-01-09 16:55:16 +01001033 sock_graft(new_sk, new_sock);
Ursula Braun07603b22019-04-11 11:17:32 +02001034 if (isk->use_fallback) {
1035 smc_sk(new_sk)->clcsock->file = new_sock->file;
1036 isk->clcsock->file->private_data = isk->clcsock;
1037 }
1038 }
Ursula Brauna046d572017-01-09 16:55:16 +01001039 return new_sk;
1040 }
1041 return NULL;
1042}
1043
1044/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +01001045void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +01001046{
1047 struct smc_sock *smc = smc_sk(sk);
1048
Ursula Braun81cf4f42019-10-21 16:13:15 +02001049 sock_hold(sk); /* sock_put below */
Ursula Braunb38d7322017-01-09 16:55:25 +01001050 lock_sock(sk);
1051 if (!sk->sk_lingertime)
1052 /* wait for peer closing */
1053 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun39f41f32019-06-27 15:04:52 +02001054 __smc_release(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +01001055 release_sock(sk);
Ursula Braun81cf4f42019-10-21 16:13:15 +02001056 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +01001057 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +01001058}
1059
Karsten Graulb9247542020-04-29 17:10:41 +02001060static int smcr_serv_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001061{
Karsten Graul387707f2020-04-29 17:10:40 +02001062 struct smc_link *link = smc->conn.lnk;
Karsten Graul4667bb42020-04-30 15:55:42 +02001063 struct smc_llc_qentry *qentry;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001064 int rc;
1065
Karsten Graul7562a132020-05-01 12:48:01 +02001066 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +02001067 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +02001068
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001069 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +02001070 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001071 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001072 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001073
1074 /* receive CONFIRM LINK response from client over the RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001075 qentry = smc_llc_wait(link->lgr, link, SMC_LLC_WAIT_TIME,
1076 SMC_LLC_CONFIRM_LINK);
1077 if (!qentry) {
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001078 struct smc_clc_msg_decline dclc;
1079
1080 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001081 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001082 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001083 }
Karsten Graul649758f2020-05-04 14:18:48 +02001084 smc_llc_save_peer_uid(qentry);
Karsten Graul4667bb42020-04-30 15:55:42 +02001085 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_RESP);
1086 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
1087 if (rc)
Karsten Graul75d320d2018-03-01 13:51:31 +01001088 return SMC_CLC_DECL_RMBE_EC;
1089
Karsten Graul4667bb42020-04-30 15:55:42 +02001090 /* confirm_rkey is implicit on 1st contact */
1091 smc->conn.rmb_desc->is_conf_rkey = true;
Karsten Graul52bedf32018-03-01 13:51:32 +01001092
Karsten Graul00a049c2020-04-29 17:10:49 +02001093 smc_llc_link_active(link);
Karsten Graul0a99be42020-05-05 15:01:20 +02001094 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
Karsten Graul52bedf32018-03-01 13:51:32 +01001095
Karsten Graul4667bb42020-04-30 15:55:42 +02001096 /* initial contact - try to establish second link */
Karsten Graul2d2209f2020-05-03 14:38:43 +02001097 smc_llc_srv_add_link(link);
Karsten Graul75d320d2018-03-01 13:51:31 +01001098 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001099}
1100
Hans Wippel3b2dec22018-05-18 09:34:18 +02001101/* listen worker: finish */
1102static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001103{
Ursula Brauna046d572017-01-09 16:55:16 +01001104 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001105 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001106
Ursula Brauna046d572017-01-09 16:55:16 +01001107 if (lsmc->sk.sk_state == SMC_LISTEN) {
Karsten Graulfd577702019-04-11 11:17:30 +02001108 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001109 smc_accept_enqueue(&lsmc->sk, newsmcsk);
Karsten Graulfd577702019-04-11 11:17:30 +02001110 release_sock(&lsmc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001111 } else { /* no longer listening */
1112 smc_close_non_accepted(newsmcsk);
1113 }
Ursula Brauna046d572017-01-09 16:55:16 +01001114
1115 /* Wake up accept */
1116 lsmc->sk.sk_data_ready(&lsmc->sk);
1117 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001118}
Ursula Brauna046d572017-01-09 16:55:16 +01001119
Hans Wippel3b2dec22018-05-18 09:34:18 +02001120/* listen worker: finish in state connected */
1121static void smc_listen_out_connected(struct smc_sock *new_smc)
1122{
1123 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001124
Hans Wippel3b2dec22018-05-18 09:34:18 +02001125 sk_refcnt_debug_inc(newsmcsk);
1126 if (newsmcsk->sk_state == SMC_INIT)
1127 newsmcsk->sk_state = SMC_ACTIVE;
1128
1129 smc_listen_out(new_smc);
1130}
1131
1132/* listen worker: finish in error state */
1133static void smc_listen_out_err(struct smc_sock *new_smc)
1134{
1135 struct sock *newsmcsk = &new_smc->sk;
1136
Ursula Braun51f1de72018-01-26 09:28:48 +01001137 if (newsmcsk->sk_state == SMC_INIT)
1138 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001139 newsmcsk->sk_state = SMC_CLOSED;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001140
1141 smc_listen_out(new_smc);
1142}
1143
1144/* listen worker: decline and fall back if possible */
1145static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
Ursula Braun5ac54d82020-09-10 18:48:21 +02001146 bool local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001147{
1148 /* RDMA setup failed, switch back to TCP */
Ursula Braun5ac54d82020-09-10 18:48:21 +02001149 if (local_first)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001150 smc_lgr_cleanup_early(&new_smc->conn);
1151 else
1152 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001153 if (reason_code < 0) { /* error, no fallback possible */
1154 smc_listen_out_err(new_smc);
1155 return;
1156 }
Ursula Braun07603b22019-04-11 11:17:32 +02001157 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001158 new_smc->fallback_rsn = reason_code;
1159 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001160 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1161 smc_listen_out_err(new_smc);
1162 return;
1163 }
1164 }
1165 smc_listen_out_connected(new_smc);
1166}
1167
1168/* listen worker: check prefixes */
Karsten Graul59886692019-04-12 12:57:25 +02001169static int smc_listen_prfx_check(struct smc_sock *new_smc,
Hans Wippel3b2dec22018-05-18 09:34:18 +02001170 struct smc_clc_msg_proposal *pclc)
1171{
1172 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1173 struct socket *newclcsock = new_smc->clcsock;
1174
1175 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1176 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
Karsten Graul59886692019-04-12 12:57:25 +02001177 return SMC_CLC_DECL_DIFFPREFIX;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001178
1179 return 0;
1180}
1181
1182/* listen worker: initialize connection and buffers */
1183static int smc_listen_rdma_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001184 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001185{
Karsten Graul7a627252019-04-12 12:57:30 +02001186 int rc;
1187
Hans Wippel3b2dec22018-05-18 09:34:18 +02001188 /* allocate connection / link group */
Karsten Graul7a627252019-04-12 12:57:30 +02001189 rc = smc_conn_create(new_smc, ini);
1190 if (rc)
1191 return rc;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001192
1193 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001194 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001195 return SMC_CLC_DECL_MEM;
1196
1197 return 0;
1198}
1199
Hans Wippel41349842018-06-28 19:05:11 +02001200/* listen worker: initialize connection and buffers for SMC-D */
1201static int smc_listen_ism_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001202 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +02001203{
Karsten Graul7a627252019-04-12 12:57:30 +02001204 int rc;
Hans Wippel41349842018-06-28 19:05:11 +02001205
Karsten Graul7a627252019-04-12 12:57:30 +02001206 rc = smc_conn_create(new_smc, ini);
1207 if (rc)
1208 return rc;
Hans Wippel41349842018-06-28 19:05:11 +02001209
Hans Wippel41349842018-06-28 19:05:11 +02001210 /* Create send and receive buffers */
Karsten Graul72b7f6c2020-07-26 20:34:28 +02001211 rc = smc_buf_create(new_smc, true);
1212 if (rc) {
Ursula Braun5ac54d82020-09-10 18:48:21 +02001213 if (ini->first_contact_local)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001214 smc_lgr_cleanup_early(&new_smc->conn);
1215 else
1216 smc_conn_free(&new_smc->conn);
Karsten Graul72b7f6c2020-07-26 20:34:28 +02001217 return (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB :
1218 SMC_CLC_DECL_MEM;
Hans Wippel41349842018-06-28 19:05:11 +02001219 }
1220
1221 return 0;
1222}
1223
Ursula Braun7affc802020-09-26 12:44:21 +02001224static void smc_find_ism_device_serv(struct smc_sock *new_smc,
1225 struct smc_clc_msg_proposal *pclc,
1226 struct smc_init_info *ini)
1227{
1228 struct smc_clc_msg_smcd *pclc_smcd = smc_get_clc_msg_smcd(pclc);
1229
1230 if (!smcd_indicated(pclc->hdr.typev1))
1231 goto not_found;
1232 ini->is_smcd = true; /* prepare ISM check */
Ursula Braun3fc64932020-09-26 12:44:23 +02001233 ini->ism_peer_gid[0] = pclc_smcd->gid;
Ursula Braun7affc802020-09-26 12:44:21 +02001234 if (smc_find_ism_device(new_smc, ini))
1235 goto not_found;
1236 if (!smc_listen_ism_init(new_smc, ini))
1237 return; /* ISM device found */
1238
1239not_found:
Ursula Braun3fc64932020-09-26 12:44:23 +02001240 ini->ism_dev[0] = NULL;
Ursula Braun7affc802020-09-26 12:44:21 +02001241 ini->is_smcd = false;
1242}
1243
Hans Wippel3b2dec22018-05-18 09:34:18 +02001244/* listen worker: register buffers */
Ursula Braun5ac54d82020-09-10 18:48:21 +02001245static int smc_listen_rdma_reg(struct smc_sock *new_smc, bool local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001246{
Karsten Graulb9247542020-04-29 17:10:41 +02001247 struct smc_connection *conn = &new_smc->conn;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001248
Ursula Braun5ac54d82020-09-10 18:48:21 +02001249 if (!local_first) {
Karsten Graul7562a132020-05-01 12:48:01 +02001250 if (smcr_lgr_reg_rmbs(conn->lnk, conn->rmb_desc))
Karsten Graulc7674c02018-11-22 10:26:43 +01001251 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001252 }
1253 smc_rmb_sync_sg_for_device(&new_smc->conn);
1254
1255 return 0;
1256}
1257
Ursula Braun7affc802020-09-26 12:44:21 +02001258static int smc_find_rdma_device_serv(struct smc_sock *new_smc,
1259 struct smc_clc_msg_proposal *pclc,
1260 struct smc_init_info *ini)
1261{
1262 int rc;
1263
1264 if (!smcr_indicated(pclc->hdr.typev1))
1265 return SMC_CLC_DECL_NOSMCDEV;
1266
1267 /* prepare RDMA check */
1268 ini->ib_lcl = &pclc->lcl;
1269 rc = smc_find_rdma_device(new_smc, ini);
1270 if (rc) {
1271 /* no RDMA device found */
1272 if (pclc->hdr.typev1 == SMC_TYPE_B)
1273 /* neither ISM nor RDMA device found */
1274 rc = SMC_CLC_DECL_NOSMCDEV;
1275 return rc;
1276 }
1277 rc = smc_listen_rdma_init(new_smc, ini);
1278 if (rc)
1279 return rc;
1280 return smc_listen_rdma_reg(new_smc, ini->first_contact_local);
1281}
1282
1283/* determine the local device matching to proposal */
1284static int smc_listen_find_device(struct smc_sock *new_smc,
1285 struct smc_clc_msg_proposal *pclc,
1286 struct smc_init_info *ini)
1287{
1288 /* check if ISM is available */
1289 smc_find_ism_device_serv(new_smc, pclc, ini);
1290 if (ini->is_smcd)
1291 return 0;
1292 if (pclc->hdr.typev1 == SMC_TYPE_D)
1293 return SMC_CLC_DECL_NOSMCDDEV; /* skip RDMA and decline */
1294
1295 /* check if RDMA is available */
1296 return smc_find_rdma_device_serv(new_smc, pclc, ini);
1297}
1298
Hans Wippel3b2dec22018-05-18 09:34:18 +02001299/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001300static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1301 struct smc_clc_msg_accept_confirm *cclc,
Ursula Braun5ac54d82020-09-10 18:48:21 +02001302 bool local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001303{
Karsten Graul387707f2020-04-29 17:10:40 +02001304 struct smc_link *link = new_smc->conn.lnk;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001305 int reason_code = 0;
1306
Ursula Braun5ac54d82020-09-10 18:48:21 +02001307 if (local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001308 smc_link_save_peer_info(link, cclc);
1309
Ursula Braun0c881ad2020-09-10 18:48:25 +02001310 if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc))
1311 return SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001312
Ursula Braun5ac54d82020-09-10 18:48:21 +02001313 if (local_first) {
Ursula Braun0c881ad2020-09-10 18:48:25 +02001314 if (smc_ib_ready_link(link))
1315 return SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001316 /* QP confirmation over RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001317 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
Karsten Graulb9247542020-04-29 17:10:41 +02001318 reason_code = smcr_serv_conf_first_link(new_smc);
Karsten Graul4667bb42020-04-30 15:55:42 +02001319 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001320 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001321 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001322}
1323
Ursula Braun7affc802020-09-26 12:44:21 +02001324/* setup for connection of server */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001325static void smc_listen_work(struct work_struct *work)
1326{
1327 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1328 smc_listen_work);
1329 struct socket *newclcsock = new_smc->clcsock;
1330 struct smc_clc_msg_accept_confirm cclc;
Ursula Braun6bb14e482020-09-10 18:48:22 +02001331 struct smc_clc_msg_proposal_area *buf;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001332 struct smc_clc_msg_proposal *pclc;
Ursula Braun3fc64932020-09-26 12:44:23 +02001333 struct smc_init_info *ini = NULL;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001334 int rc = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001335
Karsten Graulfd577702019-04-11 11:17:30 +02001336 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1337 return smc_listen_out_err(new_smc);
1338
Hans Wippel3b2dec22018-05-18 09:34:18 +02001339 if (new_smc->use_fallback) {
1340 smc_listen_out_connected(new_smc);
1341 return;
1342 }
1343
1344 /* check if peer is smc capable */
1345 if (!tcp_sk(newclcsock->sk)->syn_smc) {
Ursula Braun07603b22019-04-11 11:17:32 +02001346 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001347 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001348 smc_listen_out_connected(new_smc);
1349 return;
1350 }
1351
1352 /* do inband token exchange -
1353 * wait for and receive SMC Proposal CLC message
1354 */
Ursula Braun6bb14e482020-09-10 18:48:22 +02001355 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
1356 if (!buf) {
1357 rc = SMC_CLC_DECL_MEM;
1358 goto out_decl;
1359 }
1360 pclc = (struct smc_clc_msg_proposal *)buf;
1361 rc = smc_clc_wait_msg(new_smc, pclc, sizeof(*buf),
Karsten Graul228bae02019-04-12 12:57:28 +02001362 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001363 if (rc)
1364 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001365
1366 /* IPSec connections opt out of SMC-R optimizations */
1367 if (using_ipsec(new_smc)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001368 rc = SMC_CLC_DECL_IPSEC;
1369 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001370 }
1371
Karsten Graul59886692019-04-12 12:57:25 +02001372 /* check for matching IP prefix and subnet length */
1373 rc = smc_listen_prfx_check(new_smc, pclc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001374 if (rc)
1375 goto out_decl;
Karsten Graul59886692019-04-12 12:57:25 +02001376
Ursula Braun3fc64932020-09-26 12:44:23 +02001377 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
1378 if (!ini) {
1379 rc = SMC_CLC_DECL_MEM;
1380 goto out_decl;
1381 }
1382
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001383 /* get vlan id from IP device */
Ursula Braun3fc64932020-09-26 12:44:23 +02001384 if (smc_vlan_by_tcpsk(new_smc->clcsock, ini)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001385 rc = SMC_CLC_DECL_GETVLANERR;
1386 goto out_decl;
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001387 }
1388
Hans Wippel72a36a82019-02-07 15:56:18 +01001389 mutex_lock(&smc_server_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001390 smc_close_init(new_smc);
1391 smc_rx_init(new_smc);
1392 smc_tx_init(new_smc);
1393
Ursula Braun7affc802020-09-26 12:44:21 +02001394 /* determine ISM or RoCE device used for connection */
Ursula Braun3fc64932020-09-26 12:44:23 +02001395 rc = smc_listen_find_device(new_smc, pclc, ini);
Ursula Braun7affc802020-09-26 12:44:21 +02001396 if (rc)
1397 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001398
1399 /* send SMC Accept CLC message */
Ursula Braun3fc64932020-09-26 12:44:23 +02001400 rc = smc_clc_send_accept(new_smc, ini->first_contact_local);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001401 if (rc)
1402 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001403
Hans Wippel62c71392019-02-07 15:56:17 +01001404 /* SMC-D does not need this lock any more */
Ursula Braun3fc64932020-09-26 12:44:23 +02001405 if (ini->is_smcd)
Hans Wippel72a36a82019-02-07 15:56:18 +01001406 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001407
Hans Wippel3b2dec22018-05-18 09:34:18 +02001408 /* receive SMC Confirm CLC message */
Karsten Graul228bae02019-04-12 12:57:28 +02001409 rc = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
1410 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
1411 if (rc) {
Ursula Braun3fc64932020-09-26 12:44:23 +02001412 if (!ini->is_smcd)
Karsten Graul9aa68d22019-04-12 12:57:29 +02001413 goto out_unlock;
1414 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001415 }
1416
1417 /* finish worker */
Ursula Braun3fc64932020-09-26 12:44:23 +02001418 if (!ini->is_smcd) {
Karsten Graul7a627252019-04-12 12:57:30 +02001419 rc = smc_listen_rdma_finish(new_smc, &cclc,
Ursula Braun3fc64932020-09-26 12:44:23 +02001420 ini->first_contact_local);
Hans Wippel62c71392019-02-07 15:56:17 +01001421 if (rc)
Ursula Braun0c881ad2020-09-10 18:48:25 +02001422 goto out_unlock;
1423 mutex_unlock(&smc_server_lgr_pending);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001424 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001425 smc_conn_save_peer_info(new_smc, &cclc);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001426 smc_listen_out_connected(new_smc);
Ursula Braunac679362020-09-17 22:46:02 +02001427 goto out_free;
Karsten Graul9aa68d22019-04-12 12:57:29 +02001428
1429out_unlock:
1430 mutex_unlock(&smc_server_lgr_pending);
1431out_decl:
Ursula Braun3fc64932020-09-26 12:44:23 +02001432 smc_listen_decline(new_smc, rc, ini ? ini->first_contact_local : 0);
Ursula Braunac679362020-09-17 22:46:02 +02001433out_free:
Ursula Braun3fc64932020-09-26 12:44:23 +02001434 kfree(ini);
Ursula Braun6bb14e482020-09-10 18:48:22 +02001435 kfree(buf);
Ursula Brauna046d572017-01-09 16:55:16 +01001436}
1437
1438static void smc_tcp_listen_work(struct work_struct *work)
1439{
1440 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1441 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001442 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001443 struct smc_sock *new_smc;
1444 int rc = 0;
1445
Ursula Braun3163c502018-01-24 10:28:12 +01001446 lock_sock(lsk);
1447 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001448 rc = smc_clcsock_accept(lsmc, &new_smc);
Ursula Brauna60a2b12020-09-10 18:48:20 +02001449 if (rc) /* clcsock accept queue empty or error */
Ursula Brauna046d572017-01-09 16:55:16 +01001450 goto out;
1451 if (!new_smc)
1452 continue;
1453
1454 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001455 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001456 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001457 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001458 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1459 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001460 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1461 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001462 sock_hold(&new_smc->sk); /* sock_put in passive closing */
Karsten Graul22ef4732020-09-10 18:48:29 +02001463 if (!queue_work(smc_hs_wq, &new_smc->smc_listen_work))
Ursula Braun51f1de72018-01-26 09:28:48 +01001464 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001465 }
1466
1467out:
Ursula Braun3163c502018-01-24 10:28:12 +01001468 release_sock(lsk);
Ursula Brauna60a2b12020-09-10 18:48:20 +02001469 sock_put(&lsmc->sk); /* sock_hold in smc_clcsock_data_ready() */
1470}
1471
1472static void smc_clcsock_data_ready(struct sock *listen_clcsock)
1473{
1474 struct smc_sock *lsmc;
1475
1476 lsmc = (struct smc_sock *)
1477 ((uintptr_t)listen_clcsock->sk_user_data & ~SK_USER_DATA_NOCOPY);
1478 if (!lsmc)
1479 return;
1480 lsmc->clcsk_data_ready(listen_clcsock);
1481 if (lsmc->sk.sk_state == SMC_LISTEN) {
1482 sock_hold(&lsmc->sk); /* sock_put in smc_tcp_listen_work() */
Karsten Graul22ef4732020-09-10 18:48:29 +02001483 if (!queue_work(smc_hs_wq, &lsmc->tcp_listen_work))
Ursula Brauna60a2b12020-09-10 18:48:20 +02001484 sock_put(&lsmc->sk);
1485 }
Ursula Brauna046d572017-01-09 16:55:16 +01001486}
1487
Ursula Braunac713872017-01-09 16:55:13 +01001488static int smc_listen(struct socket *sock, int backlog)
1489{
1490 struct sock *sk = sock->sk;
1491 struct smc_sock *smc;
1492 int rc;
1493
1494 smc = smc_sk(sk);
1495 lock_sock(sk);
1496
1497 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +02001498 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
1499 smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +01001500 goto out;
1501
1502 rc = 0;
1503 if (sk->sk_state == SMC_LISTEN) {
1504 sk->sk_max_ack_backlog = backlog;
1505 goto out;
1506 }
1507 /* some socket options are handled in core, so we could not apply
1508 * them to the clc socket -- copy smc socket options to clc socket
1509 */
1510 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001511 if (!smc->use_fallback)
1512 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001513
Ursula Brauna60a2b12020-09-10 18:48:20 +02001514 /* save original sk_data_ready function and establish
1515 * smc-specific sk_data_ready function
1516 */
1517 smc->clcsk_data_ready = smc->clcsock->sk->sk_data_ready;
1518 smc->clcsock->sk->sk_data_ready = smc_clcsock_data_ready;
1519 smc->clcsock->sk->sk_user_data =
1520 (void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY);
Ursula Braunac713872017-01-09 16:55:13 +01001521 rc = kernel_listen(smc->clcsock, backlog);
1522 if (rc)
1523 goto out;
1524 sk->sk_max_ack_backlog = backlog;
1525 sk->sk_ack_backlog = 0;
1526 sk->sk_state = SMC_LISTEN;
1527
1528out:
1529 release_sock(sk);
1530 return rc;
1531}
1532
1533static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001534 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001535{
Ursula Brauna046d572017-01-09 16:55:16 +01001536 struct sock *sk = sock->sk, *nsk;
1537 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001538 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001539 long timeo;
1540 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001541
1542 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001543 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001544 lock_sock(sk);
1545
1546 if (lsmc->sk.sk_state != SMC_LISTEN) {
1547 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001548 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001549 goto out;
1550 }
1551
Ursula Brauna046d572017-01-09 16:55:16 +01001552 /* Wait for an incoming connection */
1553 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1554 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1555 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1556 set_current_state(TASK_INTERRUPTIBLE);
1557 if (!timeo) {
1558 rc = -EAGAIN;
1559 break;
1560 }
1561 release_sock(sk);
1562 timeo = schedule_timeout(timeo);
1563 /* wakeup by sk_data_ready in smc_listen_work() */
1564 sched_annotate_sleep();
1565 lock_sock(sk);
1566 if (signal_pending(current)) {
1567 rc = sock_intr_errno(timeo);
1568 break;
1569 }
1570 }
1571 set_current_state(TASK_RUNNING);
1572 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001573
Ursula Brauna046d572017-01-09 16:55:16 +01001574 if (!rc)
1575 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001576 release_sock(sk);
1577 if (rc)
1578 goto out;
1579
1580 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1581 /* wait till data arrives on the socket */
1582 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1583 MSEC_PER_SEC);
1584 if (smc_sk(nsk)->use_fallback) {
1585 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1586
1587 lock_sock(clcsk);
1588 if (skb_queue_empty(&clcsk->sk_receive_queue))
1589 sk_wait_data(clcsk, &timeo, NULL);
1590 release_sock(clcsk);
1591 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1592 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001593 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001594 release_sock(nsk);
1595 }
1596 }
Ursula Braunac713872017-01-09 16:55:13 +01001597
1598out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001599 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001600 return rc;
1601}
1602
1603static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001604 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001605{
1606 struct smc_sock *smc;
1607
Ursula Braunb38d7322017-01-09 16:55:25 +01001608 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1609 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001610 return -ENOTCONN;
1611
1612 smc = smc_sk(sock->sk);
1613
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001614 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001615}
1616
1617static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1618{
1619 struct sock *sk = sock->sk;
1620 struct smc_sock *smc;
1621 int rc = -EPIPE;
1622
1623 smc = smc_sk(sk);
1624 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001625 if ((sk->sk_state != SMC_ACTIVE) &&
1626 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1627 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001628 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001629
1630 if (msg->msg_flags & MSG_FASTOPEN) {
Ursula Brauncd206362019-08-02 10:47:50 +02001631 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001632 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001633 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001634 } else {
1635 rc = -EINVAL;
1636 goto out;
1637 }
1638 }
1639
Ursula Braunac713872017-01-09 16:55:13 +01001640 if (smc->use_fallback)
1641 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1642 else
Ursula Braune6727f32017-01-09 16:55:23 +01001643 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001644out:
1645 release_sock(sk);
1646 return rc;
1647}
1648
1649static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1650 int flags)
1651{
1652 struct sock *sk = sock->sk;
1653 struct smc_sock *smc;
1654 int rc = -ENOTCONN;
1655
1656 smc = smc_sk(sk);
1657 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001658 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1659 /* socket was connected before, no more data to read */
1660 rc = 0;
1661 goto out;
1662 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001663 if ((sk->sk_state == SMC_INIT) ||
1664 (sk->sk_state == SMC_LISTEN) ||
1665 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001666 goto out;
1667
Ursula Braunb38d7322017-01-09 16:55:25 +01001668 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1669 rc = 0;
1670 goto out;
1671 }
1672
Stefan Raspl9014db22018-05-03 18:12:39 +02001673 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001674 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001675 } else {
1676 msg->msg_namelen = 0;
1677 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1678 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001679
Ursula Braunac713872017-01-09 16:55:13 +01001680out:
1681 release_sock(sk);
1682 return rc;
1683}
1684
Al Viroade994f2017-07-03 00:01:49 -04001685static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001686{
Ursula Braun8dce2782018-01-26 09:28:47 +01001687 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001688 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001689
Ursula Braun8dce2782018-01-26 09:28:47 +01001690 spin_lock(&isk->accept_q_lock);
1691 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001692 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001693 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001694
Ursula Braun8dce2782018-01-26 09:28:47 +01001695 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001696}
1697
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001698static __poll_t smc_poll(struct file *file, struct socket *sock,
1699 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01001700{
1701 struct sock *sk = sock->sk;
Ursula Braunac713872017-01-09 16:55:13 +01001702 struct smc_sock *smc;
Ursula Braun50717a32019-04-12 12:57:23 +02001703 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001704
Ursula Braun8dce2782018-01-26 09:28:47 +01001705 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001706 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001707
Ursula Braunac713872017-01-09 16:55:13 +01001708 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02001709 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01001710 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001711 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02001712 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braunac713872017-01-09 16:55:13 +01001713 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07001714 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02001715 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01001716 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001717 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001718 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1719 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001720 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001721 if (sk->sk_state == SMC_LISTEN) {
1722 /* woken up by sk_data_ready in smc_listen_work() */
Ursula Braun50717a32019-04-12 12:57:23 +02001723 mask |= smc_accept_poll(sk);
1724 } else if (smc->use_fallback) { /* as result of connect_work()*/
1725 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
1726 wait);
1727 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun8dce2782018-01-26 09:28:47 +01001728 } else {
Ursula Braun50717a32019-04-12 12:57:23 +02001729 if ((sk->sk_state != SMC_INIT &&
1730 atomic_read(&smc->conn.sndbuf_space)) ||
Ursula Braun8dce2782018-01-26 09:28:47 +01001731 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001732 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001733 } else {
1734 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1735 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1736 }
1737 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001738 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001739 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001740 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001741 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001742 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02001743 if (smc->conn.urg_state == SMC_URG_VALID)
1744 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01001745 }
Ursula Braunac713872017-01-09 16:55:13 +01001746 }
1747
1748 return mask;
1749}
1750
1751static int smc_shutdown(struct socket *sock, int how)
1752{
1753 struct sock *sk = sock->sk;
1754 struct smc_sock *smc;
1755 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001756 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001757
1758 smc = smc_sk(sk);
1759
1760 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001761 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001762
1763 lock_sock(sk);
1764
1765 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02001766 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01001767 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1768 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1769 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1770 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1771 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001772 goto out;
1773 if (smc->use_fallback) {
1774 rc = kernel_sock_shutdown(smc->clcsock, how);
1775 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1776 if (sk->sk_shutdown == SHUTDOWN_MASK)
1777 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001778 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001779 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001780 switch (how) {
1781 case SHUT_RDWR: /* shutdown in both directions */
1782 rc = smc_close_active(smc);
1783 break;
1784 case SHUT_WR:
1785 rc = smc_close_shutdown_write(smc);
1786 break;
1787 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001788 rc = 0;
1789 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001790 break;
1791 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001792 if (smc->clcsock)
1793 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001794 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1795 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001796
1797out:
1798 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001799 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001800}
1801
1802static int smc_setsockopt(struct socket *sock, int level, int optname,
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02001803 sockptr_t optval, unsigned int optlen)
Ursula Braunac713872017-01-09 16:55:13 +01001804{
1805 struct sock *sk = sock->sk;
1806 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001807 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001808
1809 smc = smc_sk(sk);
1810
1811 /* generic setsockopts reaching us here always apply to the
1812 * CLC socket
1813 */
Christoph Hellwiga44d9e72020-07-17 08:23:31 +02001814 if (unlikely(!smc->clcsock->ops->setsockopt))
1815 rc = -EOPNOTSUPP;
1816 else
1817 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1818 optval, optlen);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001819 if (smc->clcsock->sk->sk_err) {
1820 sk->sk_err = smc->clcsock->sk->sk_err;
1821 sk->sk_error_report(sk);
1822 }
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001823
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001824 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00001825 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02001826 if (copy_from_sockptr(&val, optval, sizeof(int)))
Ursula Braunac0107e2018-07-18 15:22:50 +02001827 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001828
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001829 lock_sock(sk);
Ursula Braun86434742019-12-12 22:35:58 +01001830 if (rc || smc->use_fallback)
1831 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001832 switch (optname) {
1833 case TCP_ULP:
1834 case TCP_FASTOPEN:
1835 case TCP_FASTOPEN_CONNECT:
1836 case TCP_FASTOPEN_KEY:
1837 case TCP_FASTOPEN_NO_COOKIE:
1838 /* option not supported by SMC */
Ursula Braun8204df72019-11-15 12:39:30 +01001839 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001840 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001841 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001842 } else {
Ursula Braun86434742019-12-12 22:35:58 +01001843 rc = -EINVAL;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001844 }
1845 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001846 case TCP_NODELAY:
Ursula Braunf9cedf12019-08-02 10:16:38 +02001847 if (sk->sk_state != SMC_INIT &&
1848 sk->sk_state != SMC_LISTEN &&
1849 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01001850 if (val)
Karsten Graul22ef4732020-09-10 18:48:29 +02001851 mod_delayed_work(smc->conn.lgr->tx_wq,
1852 &smc->conn.tx_work, 0);
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001853 }
1854 break;
1855 case TCP_CORK:
Ursula Braunf9cedf12019-08-02 10:16:38 +02001856 if (sk->sk_state != SMC_INIT &&
1857 sk->sk_state != SMC_LISTEN &&
1858 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01001859 if (!val)
Karsten Graul22ef4732020-09-10 18:48:29 +02001860 mod_delayed_work(smc->conn.lgr->tx_wq,
1861 &smc->conn.tx_work, 0);
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001862 }
1863 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001864 case TCP_DEFER_ACCEPT:
1865 smc->sockopt_defer_accept = val;
1866 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001867 default:
1868 break;
1869 }
Ursula Braun86434742019-12-12 22:35:58 +01001870out:
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001871 release_sock(sk);
1872
1873 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001874}
1875
1876static int smc_getsockopt(struct socket *sock, int level, int optname,
1877 char __user *optval, int __user *optlen)
1878{
1879 struct smc_sock *smc;
1880
1881 smc = smc_sk(sock->sk);
1882 /* socket options apply to the CLC socket */
Christoph Hellwiga44d9e72020-07-17 08:23:31 +02001883 if (unlikely(!smc->clcsock->ops->getsockopt))
1884 return -EOPNOTSUPP;
Ursula Braunac713872017-01-09 16:55:13 +01001885 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1886 optval, optlen);
1887}
1888
1889static int smc_ioctl(struct socket *sock, unsigned int cmd,
1890 unsigned long arg)
1891{
Stefan Rasplde8474e2018-05-23 16:38:11 +02001892 union smc_host_cursor cons, urg;
1893 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01001894 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001895 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001896
1897 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001898 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02001899 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02001900 if (smc->use_fallback) {
1901 if (!smc->clcsock) {
1902 release_sock(&smc->sk);
1903 return -EBADF;
1904 }
1905 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1906 release_sock(&smc->sk);
1907 return answ;
1908 }
Ursula Braun9b67e262018-05-02 16:56:46 +02001909 switch (cmd) {
1910 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02001911 if (smc->sk.sk_state == SMC_LISTEN) {
1912 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001913 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001914 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001915 if (smc->sk.sk_state == SMC_INIT ||
1916 smc->sk.sk_state == SMC_CLOSED)
1917 answ = 0;
1918 else
1919 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02001920 break;
1921 case SIOCOUTQ:
1922 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02001923 if (smc->sk.sk_state == SMC_LISTEN) {
1924 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001925 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001926 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001927 if (smc->sk.sk_state == SMC_INIT ||
1928 smc->sk.sk_state == SMC_CLOSED)
1929 answ = 0;
1930 else
1931 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02001932 atomic_read(&smc->conn.sndbuf_space);
1933 break;
1934 case SIOCOUTQNSD:
1935 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02001936 if (smc->sk.sk_state == SMC_LISTEN) {
1937 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001938 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001939 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001940 if (smc->sk.sk_state == SMC_INIT ||
1941 smc->sk.sk_state == SMC_CLOSED)
1942 answ = 0;
1943 else
1944 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02001945 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02001946 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02001947 if (smc->sk.sk_state == SMC_LISTEN) {
1948 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001949 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001950 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001951 if (smc->sk.sk_state == SMC_INIT ||
1952 smc->sk.sk_state == SMC_CLOSED) {
1953 answ = 0;
1954 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02001955 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1956 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001957 answ = smc_curs_diff(conn->rmb_desc->len,
1958 &cons, &urg) == 1;
1959 }
1960 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02001961 default:
Ursula Braun1992d992018-07-16 13:56:52 +02001962 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001963 return -ENOIOCTLCMD;
1964 }
Ursula Braun1992d992018-07-16 13:56:52 +02001965 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001966
1967 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001968}
1969
1970static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1971 int offset, size_t size, int flags)
1972{
1973 struct sock *sk = sock->sk;
1974 struct smc_sock *smc;
1975 int rc = -EPIPE;
1976
1977 smc = smc_sk(sk);
1978 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001979 if (sk->sk_state != SMC_ACTIVE) {
1980 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001981 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001982 }
1983 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001984 if (smc->use_fallback)
1985 rc = kernel_sendpage(smc->clcsock, page, offset,
1986 size, flags);
1987 else
1988 rc = sock_no_sendpage(sock, page, offset, size, flags);
1989
1990out:
Ursula Braunac713872017-01-09 16:55:13 +01001991 return rc;
1992}
1993
Stefan Raspl9014db22018-05-03 18:12:39 +02001994/* Map the affected portions of the rmbe into an spd, note the number of bytes
1995 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1996 * updates till whenever a respective page has been fully processed.
1997 * Note that subsequent recv() calls have to wait till all splice() processing
1998 * completed.
1999 */
Ursula Braunac713872017-01-09 16:55:13 +01002000static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
2001 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02002002 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01002003{
2004 struct sock *sk = sock->sk;
2005 struct smc_sock *smc;
2006 int rc = -ENOTCONN;
2007
2008 smc = smc_sk(sk);
2009 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01002010 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2011 /* socket was connected before, no more data to read */
2012 rc = 0;
2013 goto out;
2014 }
Stefan Raspl9014db22018-05-03 18:12:39 +02002015 if (sk->sk_state == SMC_INIT ||
2016 sk->sk_state == SMC_LISTEN ||
2017 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01002018 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02002019
2020 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2021 rc = 0;
2022 goto out;
2023 }
2024
Ursula Braunac713872017-01-09 16:55:13 +01002025 if (smc->use_fallback) {
2026 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
2027 pipe, len, flags);
2028 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02002029 if (*ppos) {
2030 rc = -ESPIPE;
2031 goto out;
2032 }
2033 if (flags & SPLICE_F_NONBLOCK)
2034 flags = MSG_DONTWAIT;
2035 else
2036 flags = 0;
2037 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01002038 }
2039out:
2040 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02002041
Ursula Braunac713872017-01-09 16:55:13 +01002042 return rc;
2043}
2044
2045/* must look like tcp */
2046static const struct proto_ops smc_sock_ops = {
2047 .family = PF_SMC,
2048 .owner = THIS_MODULE,
2049 .release = smc_release,
2050 .bind = smc_bind,
2051 .connect = smc_connect,
2052 .socketpair = sock_no_socketpair,
2053 .accept = smc_accept,
2054 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07002055 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01002056 .ioctl = smc_ioctl,
2057 .listen = smc_listen,
2058 .shutdown = smc_shutdown,
2059 .setsockopt = smc_setsockopt,
2060 .getsockopt = smc_getsockopt,
2061 .sendmsg = smc_sendmsg,
2062 .recvmsg = smc_recvmsg,
2063 .mmap = sock_no_mmap,
2064 .sendpage = smc_sendpage,
2065 .splice_read = smc_splice_read,
2066};
2067
2068static int smc_create(struct net *net, struct socket *sock, int protocol,
2069 int kern)
2070{
Karsten Graulaaa4d332018-03-16 15:06:41 +01002071 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01002072 struct smc_sock *smc;
2073 struct sock *sk;
2074 int rc;
2075
2076 rc = -ESOCKTNOSUPPORT;
2077 if (sock->type != SOCK_STREAM)
2078 goto out;
2079
2080 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002081 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01002082 goto out;
2083
2084 rc = -ENOBUFS;
2085 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002086 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01002087 if (!sk)
2088 goto out;
2089
2090 /* create internal TCP socket for CLC handshake and fallback */
2091 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01002092 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02002093 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002094 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
2095 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01002096 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01002097 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01002098 goto out;
2099 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01002100 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
2101 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01002102
2103out:
2104 return rc;
2105}
2106
2107static const struct net_proto_family smc_sock_family_ops = {
2108 .family = PF_SMC,
2109 .owner = THIS_MODULE,
2110 .create = smc_create,
2111};
2112
Hans Wippel64e28b52019-02-21 13:01:02 +01002113unsigned int smc_net_id;
2114
2115static __net_init int smc_net_init(struct net *net)
2116{
2117 return smc_pnet_net_init(net);
2118}
2119
2120static void __net_exit smc_net_exit(struct net *net)
2121{
2122 smc_pnet_net_exit(net);
2123}
2124
2125static struct pernet_operations smc_net_ops = {
2126 .init = smc_net_init,
2127 .exit = smc_net_exit,
2128 .id = &smc_net_id,
2129 .size = sizeof(struct smc_net),
2130};
2131
Ursula Braunac713872017-01-09 16:55:13 +01002132static int __init smc_init(void)
2133{
2134 int rc;
2135
Hans Wippel64e28b52019-02-21 13:01:02 +01002136 rc = register_pernet_subsys(&smc_net_ops);
2137 if (rc)
2138 return rc;
2139
Ursula Braun201091e2020-09-26 12:44:24 +02002140 smc_ism_init();
2141
Thomas Richter6812baa2017-01-09 16:55:15 +01002142 rc = smc_pnet_init();
2143 if (rc)
YueHaibing8c33bf12019-06-26 17:47:50 +02002144 goto out_pernet_subsys;
Thomas Richter6812baa2017-01-09 16:55:15 +01002145
Karsten Graul22ef4732020-09-10 18:48:29 +02002146 rc = -ENOMEM;
2147 smc_hs_wq = alloc_workqueue("smc_hs_wq", 0, 0);
2148 if (!smc_hs_wq)
2149 goto out_pnet;
2150
2151 smc_close_wq = alloc_workqueue("smc_close_wq", 0, 0);
2152 if (!smc_close_wq)
2153 goto out_alloc_hs_wq;
2154
Ursula Braun6dabd402019-11-16 17:47:29 +01002155 rc = smc_core_init();
2156 if (rc) {
2157 pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
Karsten Graul22ef4732020-09-10 18:48:29 +02002158 goto out_alloc_wqs;
Ursula Braun6dabd402019-11-16 17:47:29 +01002159 }
2160
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002161 rc = smc_llc_init();
2162 if (rc) {
2163 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002164 goto out_core;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002165 }
2166
Ursula Braun5f083182017-01-09 16:55:22 +01002167 rc = smc_cdc_init();
2168 if (rc) {
2169 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002170 goto out_core;
Ursula Braun5f083182017-01-09 16:55:22 +01002171 }
2172
Ursula Braunac713872017-01-09 16:55:13 +01002173 rc = proto_register(&smc_proto, 1);
2174 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01002175 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002176 goto out_core;
Ursula Braunac713872017-01-09 16:55:13 +01002177 }
2178
Karsten Graulaaa4d332018-03-16 15:06:41 +01002179 rc = proto_register(&smc_proto6, 1);
2180 if (rc) {
2181 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2182 goto out_proto;
2183 }
2184
Ursula Braunac713872017-01-09 16:55:13 +01002185 rc = sock_register(&smc_sock_family_ops);
2186 if (rc) {
2187 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002188 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01002189 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01002190 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002191 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01002192
Ursula Brauna4cf0442017-01-09 16:55:14 +01002193 rc = smc_ib_register_client();
2194 if (rc) {
2195 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2196 goto out_sock;
2197 }
2198
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002199 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002200 return 0;
2201
Ursula Brauna4cf0442017-01-09 16:55:14 +01002202out_sock:
2203 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002204out_proto6:
2205 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002206out_proto:
2207 proto_unregister(&smc_proto);
Ursula Braun6dabd402019-11-16 17:47:29 +01002208out_core:
2209 smc_core_exit();
Karsten Graul22ef4732020-09-10 18:48:29 +02002210out_alloc_wqs:
2211 destroy_workqueue(smc_close_wq);
2212out_alloc_hs_wq:
2213 destroy_workqueue(smc_hs_wq);
Thomas Richter6812baa2017-01-09 16:55:15 +01002214out_pnet:
2215 smc_pnet_exit();
YueHaibing8c33bf12019-06-26 17:47:50 +02002216out_pernet_subsys:
2217 unregister_pernet_subsys(&smc_net_ops);
2218
Ursula Braunac713872017-01-09 16:55:13 +01002219 return rc;
2220}
2221
2222static void __exit smc_exit(void)
2223{
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002224 static_branch_disable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002225 sock_unregister(PF_SMC);
Ursula Braun6dabd402019-11-16 17:47:29 +01002226 smc_core_exit();
2227 smc_ib_unregister_client();
Karsten Graul22ef4732020-09-10 18:48:29 +02002228 destroy_workqueue(smc_close_wq);
2229 destroy_workqueue(smc_hs_wq);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002230 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002231 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002232 smc_pnet_exit();
Hans Wippel64e28b52019-02-21 13:01:02 +01002233 unregister_pernet_subsys(&smc_net_ops);
Ursula Braun4ead9c92019-11-16 17:47:31 +01002234 rcu_barrier();
Ursula Braunac713872017-01-09 16:55:13 +01002235}
2236
2237module_init(smc_init);
2238module_exit(smc_exit);
2239
2240MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2241MODULE_DESCRIPTION("smc socket address family");
2242MODULE_LICENSE("GPL");
2243MODULE_ALIAS_NETPROTO(PF_SMC);