blob: 9f3e148c60c56fc5b04841d958305ef4555b5bb3 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Ursula Braunac713872017-01-09 16:55:13 +01002/*
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
4 *
5 * AF_SMC protocol family socket handler keeping the AF_INET sock address type
6 * applies to SOCK_STREAM sockets only
7 * offers an alternative communication option for TCP-protocol sockets
8 * applicable with RoCE-cards only
9 *
Ursula Brauna046d572017-01-09 16:55:16 +010010 * Initial restrictions:
Ursula Brauna046d572017-01-09 16:55:16 +010011 * - support for alternate links postponed
Ursula Brauna046d572017-01-09 16:55:16 +010012 *
Karsten Graulaaa4d332018-03-16 15:06:41 +010013 * Copyright IBM Corp. 2016, 2018
Ursula Braunac713872017-01-09 16:55:13 +010014 *
15 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
16 * based on prototype from Frank Blaschka
17 */
18
19#define KMSG_COMPONENT "smc"
20#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
21
22#include <linux/module.h>
23#include <linux/socket.h>
Ursula Brauna046d572017-01-09 16:55:16 +010024#include <linux/workqueue.h>
Ursula Braun5f083182017-01-09 16:55:22 +010025#include <linux/in.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010026#include <linux/sched/signal.h>
Hans Wippel41349842018-06-28 19:05:11 +020027#include <linux/if_vlan.h>
Ursula Braun4ead9c92019-11-16 17:47:31 +010028#include <linux/rcupdate_wait.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010029
Ursula Braunac713872017-01-09 16:55:13 +010030#include <net/sock.h>
Ursula Brauna046d572017-01-09 16:55:16 +010031#include <net/tcp.h>
Ursula Braunf16a7dd2017-01-09 16:55:26 +010032#include <net/smc.h>
Ursula Braun9b67e262018-05-02 16:56:46 +020033#include <asm/ioctls.h>
Ursula Braunac713872017-01-09 16:55:13 +010034
Hans Wippel64e28b52019-02-21 13:01:02 +010035#include <net/net_namespace.h>
36#include <net/netns/generic.h>
37#include "smc_netns.h"
38
Ursula Braunac713872017-01-09 16:55:13 +010039#include "smc.h"
Ursula Brauna046d572017-01-09 16:55:16 +010040#include "smc_clc.h"
Ursula Braun9bf9abe2017-01-09 16:55:21 +010041#include "smc_llc.h"
Ursula Braun5f083182017-01-09 16:55:22 +010042#include "smc_cdc.h"
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010043#include "smc_core.h"
Ursula Brauna4cf0442017-01-09 16:55:14 +010044#include "smc_ib.h"
Hans Wippel41349842018-06-28 19:05:11 +020045#include "smc_ism.h"
Thomas Richter6812baa2017-01-09 16:55:15 +010046#include "smc_pnet.h"
Ursula Braune6727f32017-01-09 16:55:23 +010047#include "smc_tx.h"
Ursula Braun952310c2017-01-09 16:55:24 +010048#include "smc_rx.h"
Ursula Braunb38d7322017-01-09 16:55:25 +010049#include "smc_close.h"
Ursula Braunac713872017-01-09 16:55:13 +010050
Hans Wippel72a36a82019-02-07 15:56:18 +010051static DEFINE_MUTEX(smc_server_lgr_pending); /* serialize link group
52 * creation on server
53 */
54static DEFINE_MUTEX(smc_client_lgr_pending); /* serialize link group
55 * creation on client
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010056 */
57
Ursula Brauna046d572017-01-09 16:55:16 +010058static void smc_tcp_listen_work(struct work_struct *);
Ursula Braun24ac3a02018-06-27 17:59:50 +020059static void smc_connect_work(struct work_struct *);
Ursula Brauna046d572017-01-09 16:55:16 +010060
Ursula Braunac713872017-01-09 16:55:13 +010061static void smc_set_keepalive(struct sock *sk, int val)
62{
63 struct smc_sock *smc = smc_sk(sk);
64
65 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
66}
67
Ursula Braunf16a7dd2017-01-09 16:55:26 +010068static struct smc_hashinfo smc_v4_hashinfo = {
69 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
70};
71
Karsten Graulaaa4d332018-03-16 15:06:41 +010072static struct smc_hashinfo smc_v6_hashinfo = {
73 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
74};
75
Ursula Braunf16a7dd2017-01-09 16:55:26 +010076int smc_hash_sk(struct sock *sk)
77{
78 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
79 struct hlist_head *head;
80
81 head = &h->ht;
82
83 write_lock_bh(&h->lock);
84 sk_add_node(sk, head);
85 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
86 write_unlock_bh(&h->lock);
87
88 return 0;
89}
90EXPORT_SYMBOL_GPL(smc_hash_sk);
91
92void smc_unhash_sk(struct sock *sk)
93{
94 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
95
96 write_lock_bh(&h->lock);
97 if (sk_del_node_init(sk))
98 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
99 write_unlock_bh(&h->lock);
100}
101EXPORT_SYMBOL_GPL(smc_unhash_sk);
102
103struct proto smc_proto = {
Ursula Braunac713872017-01-09 16:55:13 +0100104 .name = "SMC",
105 .owner = THIS_MODULE,
106 .keepalive = smc_set_keepalive,
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100107 .hash = smc_hash_sk,
108 .unhash = smc_unhash_sk,
Ursula Braunac713872017-01-09 16:55:13 +0100109 .obj_size = sizeof(struct smc_sock),
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100110 .h.smc_hash = &smc_v4_hashinfo,
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800111 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Ursula Braunac713872017-01-09 16:55:13 +0100112};
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100113EXPORT_SYMBOL_GPL(smc_proto);
Ursula Braunac713872017-01-09 16:55:13 +0100114
Karsten Graulaaa4d332018-03-16 15:06:41 +0100115struct proto smc_proto6 = {
116 .name = "SMC6",
117 .owner = THIS_MODULE,
118 .keepalive = smc_set_keepalive,
119 .hash = smc_hash_sk,
120 .unhash = smc_unhash_sk,
121 .obj_size = sizeof(struct smc_sock),
122 .h.smc_hash = &smc_v6_hashinfo,
123 .slab_flags = SLAB_TYPESAFE_BY_RCU,
124};
125EXPORT_SYMBOL_GPL(smc_proto6);
126
Ursula Braunf536dff2019-10-23 15:44:05 +0200127static void smc_restore_fallback_changes(struct smc_sock *smc)
128{
Karsten Graul1ad24052020-07-18 15:06:18 +0200129 if (smc->clcsock->file) { /* non-accepted sockets have no file yet */
130 smc->clcsock->file->private_data = smc->sk.sk_socket;
131 smc->clcsock->file = NULL;
132 }
Ursula Braunf536dff2019-10-23 15:44:05 +0200133}
134
Ursula Braun39f41f32019-06-27 15:04:52 +0200135static int __smc_release(struct smc_sock *smc)
Ursula Braunac713872017-01-09 16:55:13 +0100136{
Ursula Braun39f41f32019-06-27 15:04:52 +0200137 struct sock *sk = &smc->sk;
Ursula Braunb38d7322017-01-09 16:55:25 +0100138 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +0100139
Ursula Braun51f1de72018-01-26 09:28:48 +0100140 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100141 rc = smc_close_active(smc);
142 sock_set_flag(sk, SOCK_DEAD);
143 sk->sk_shutdown |= SHUTDOWN_MASK;
Ursula Braunb03faa12019-02-07 15:56:15 +0100144 } else {
145 if (sk->sk_state != SMC_LISTEN && sk->sk_state != SMC_INIT)
146 sock_put(sk); /* passive closing */
147 if (sk->sk_state == SMC_LISTEN) {
148 /* wake up clcsock accept */
149 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
150 }
151 sk->sk_state = SMC_CLOSED;
152 sk->sk_state_change(sk);
Ursula Braunf536dff2019-10-23 15:44:05 +0200153 smc_restore_fallback_changes(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +0100154 }
Cong Wang26d92e92019-01-05 23:45:26 -0800155
156 sk->sk_prot->unhash(sk);
157
Ursula Braunb03faa12019-02-07 15:56:15 +0100158 if (sk->sk_state == SMC_CLOSED) {
159 if (smc->clcsock) {
Karsten Graulfd577702019-04-11 11:17:30 +0200160 release_sock(sk);
161 smc_clcsock_release(smc);
162 lock_sock(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800163 }
Ursula Braunb03faa12019-02-07 15:56:15 +0100164 if (!smc->use_fallback)
165 smc_conn_free(&smc->conn);
Ursula Braun51f1de72018-01-26 09:28:48 +0100166 }
Ursula Braunac713872017-01-09 16:55:13 +0100167
Ursula Braun39f41f32019-06-27 15:04:52 +0200168 return rc;
169}
170
171static int smc_release(struct socket *sock)
172{
173 struct sock *sk = sock->sk;
174 struct smc_sock *smc;
175 int rc = 0;
176
177 if (!sk)
178 goto out;
179
Ursula Braun81cf4f42019-10-21 16:13:15 +0200180 sock_hold(sk); /* sock_put below */
Ursula Braun39f41f32019-06-27 15:04:52 +0200181 smc = smc_sk(sk);
182
183 /* cleanup for a dangling non-blocking connect */
184 if (smc->connect_nonblock && sk->sk_state == SMC_INIT)
185 tcp_abort(smc->clcsock->sk, ECONNABORTED);
186 flush_work(&smc->connect_work);
187
188 if (sk->sk_state == SMC_LISTEN)
189 /* smc_close_non_accepted() is called and acquires
190 * sock lock for child sockets again
191 */
192 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
193 else
194 lock_sock(sk);
195
196 rc = __smc_release(smc);
197
Ursula Braunac713872017-01-09 16:55:13 +0100198 /* detach socket */
199 sock_orphan(sk);
200 sock->sk = NULL;
201 release_sock(sk);
202
Ursula Braun81cf4f42019-10-21 16:13:15 +0200203 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +0100204 sock_put(sk); /* final sock_put */
Ursula Braunac713872017-01-09 16:55:13 +0100205out:
Ursula Braunb38d7322017-01-09 16:55:25 +0100206 return rc;
Ursula Braunac713872017-01-09 16:55:13 +0100207}
208
209static void smc_destruct(struct sock *sk)
210{
211 if (sk->sk_state != SMC_CLOSED)
212 return;
213 if (!sock_flag(sk, SOCK_DEAD))
214 return;
215
216 sk_refcnt_debug_dec(sk);
217}
218
Karsten Graulaaa4d332018-03-16 15:06:41 +0100219static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
220 int protocol)
Ursula Braunac713872017-01-09 16:55:13 +0100221{
222 struct smc_sock *smc;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100223 struct proto *prot;
Ursula Braunac713872017-01-09 16:55:13 +0100224 struct sock *sk;
225
Karsten Graulaaa4d332018-03-16 15:06:41 +0100226 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
227 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
Ursula Braunac713872017-01-09 16:55:13 +0100228 if (!sk)
229 return NULL;
230
231 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
232 sk->sk_state = SMC_INIT;
233 sk->sk_destruct = smc_destruct;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100234 sk->sk_protocol = protocol;
Ursula Braunac713872017-01-09 16:55:13 +0100235 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100236 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200237 INIT_WORK(&smc->connect_work, smc_connect_work);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700238 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
Ursula Brauna046d572017-01-09 16:55:16 +0100239 INIT_LIST_HEAD(&smc->accept_q);
240 spin_lock_init(&smc->accept_q_lock);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700241 spin_lock_init(&smc->conn.send_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100242 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100243 sk_refcnt_debug_inc(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800244 mutex_init(&smc->clcsock_release_lock);
Ursula Braunac713872017-01-09 16:55:13 +0100245
246 return sk;
247}
248
249static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
250 int addr_len)
251{
252 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
253 struct sock *sk = sock->sk;
254 struct smc_sock *smc;
255 int rc;
256
257 smc = smc_sk(sk);
258
259 /* replicate tests from inet_bind(), to be safe wrt. future changes */
260 rc = -EINVAL;
261 if (addr_len < sizeof(struct sockaddr_in))
262 goto out;
263
264 rc = -EAFNOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100265 if (addr->sin_family != AF_INET &&
266 addr->sin_family != AF_INET6 &&
267 addr->sin_family != AF_UNSPEC)
268 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100269 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
Karsten Graulaaa4d332018-03-16 15:06:41 +0100270 if (addr->sin_family == AF_UNSPEC &&
271 addr->sin_addr.s_addr != htonl(INADDR_ANY))
Ursula Braunac713872017-01-09 16:55:13 +0100272 goto out;
273
274 lock_sock(sk);
275
276 /* Check if socket is already active */
277 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +0200278 if (sk->sk_state != SMC_INIT || smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +0100279 goto out_rel;
280
281 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
282 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
283
284out_rel:
285 release_sock(sk);
286out:
287 return rc;
288}
289
290static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
291 unsigned long mask)
292{
293 /* options we don't get control via setsockopt for */
294 nsk->sk_type = osk->sk_type;
295 nsk->sk_sndbuf = osk->sk_sndbuf;
296 nsk->sk_rcvbuf = osk->sk_rcvbuf;
297 nsk->sk_sndtimeo = osk->sk_sndtimeo;
298 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
299 nsk->sk_mark = osk->sk_mark;
300 nsk->sk_priority = osk->sk_priority;
301 nsk->sk_rcvlowat = osk->sk_rcvlowat;
302 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
303 nsk->sk_err = osk->sk_err;
304
305 nsk->sk_flags &= ~mask;
306 nsk->sk_flags |= osk->sk_flags & mask;
307}
308
309#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
310 (1UL << SOCK_KEEPOPEN) | \
311 (1UL << SOCK_LINGER) | \
312 (1UL << SOCK_BROADCAST) | \
313 (1UL << SOCK_TIMESTAMP) | \
314 (1UL << SOCK_DBG) | \
315 (1UL << SOCK_RCVTSTAMP) | \
316 (1UL << SOCK_RCVTSTAMPNS) | \
317 (1UL << SOCK_LOCALROUTE) | \
318 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
319 (1UL << SOCK_RXQ_OVFL) | \
320 (1UL << SOCK_WIFI_STATUS) | \
321 (1UL << SOCK_NOFCS) | \
Deepa Dinamani97184752019-02-02 07:34:51 -0800322 (1UL << SOCK_FILTER_LOCKED) | \
323 (1UL << SOCK_TSTAMP_NEW))
Ursula Braunac713872017-01-09 16:55:13 +0100324/* copy only relevant settings and flags of SOL_SOCKET level from smc to
325 * clc socket (since smc is not called for these options from net/core)
326 */
327static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
328{
329 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
330}
331
332#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
333 (1UL << SOCK_KEEPOPEN) | \
334 (1UL << SOCK_LINGER) | \
335 (1UL << SOCK_DBG))
336/* copy only settings and flags relevant for smc from clc to smc socket */
337static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
338{
339 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
340}
341
Karsten Graulb9247542020-04-29 17:10:41 +0200342/* register the new rmb on all links */
Karsten Graul7562a132020-05-01 12:48:01 +0200343static int smcr_lgr_reg_rmbs(struct smc_link *link,
Karsten Graulb9247542020-04-29 17:10:41 +0200344 struct smc_buf_desc *rmb_desc)
345{
Karsten Graul7562a132020-05-01 12:48:01 +0200346 struct smc_link_group *lgr = link->lgr;
347 int i, rc = 0;
Karsten Graulb9247542020-04-29 17:10:41 +0200348
Karsten Grauld5500662020-05-01 12:48:05 +0200349 rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY);
350 if (rc)
351 return rc;
352 /* protect against parallel smc_llc_cli_rkey_exchange() and
353 * parallel smcr_link_reg_rmb()
354 */
355 mutex_lock(&lgr->llc_conf_mutex);
Karsten Graulb9247542020-04-29 17:10:41 +0200356 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
Karsten Graul741a49a2020-07-18 15:06:16 +0200357 if (!smc_link_active(&lgr->lnk[i]))
Karsten Graulb9247542020-04-29 17:10:41 +0200358 continue;
Karsten Graul7562a132020-05-01 12:48:01 +0200359 rc = smcr_link_reg_rmb(&lgr->lnk[i], rmb_desc);
Karsten Graulb9247542020-04-29 17:10:41 +0200360 if (rc)
Karsten Graul7562a132020-05-01 12:48:01 +0200361 goto out;
Karsten Graulb9247542020-04-29 17:10:41 +0200362 }
Karsten Graul7562a132020-05-01 12:48:01 +0200363
364 /* exchange confirm_rkey msg with peer */
365 rc = smc_llc_do_confirm_rkey(link, rmb_desc);
366 if (rc) {
367 rc = -EFAULT;
368 goto out;
369 }
370 rmb_desc->is_conf_rkey = true;
371out:
Karsten Grauld5500662020-05-01 12:48:05 +0200372 mutex_unlock(&lgr->llc_conf_mutex);
373 smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
Karsten Graul7562a132020-05-01 12:48:01 +0200374 return rc;
Karsten Graulb9247542020-04-29 17:10:41 +0200375}
376
377static int smcr_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100378{
Karsten Graul387707f2020-04-29 17:10:40 +0200379 struct smc_link *link = smc->conn.lnk;
Karsten Graul0fb0b022020-04-30 15:55:43 +0200380 struct smc_llc_qentry *qentry;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100381 int rc;
382
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100383 /* receive CONFIRM LINK request from server over RoCE fabric */
Karsten Graul0fb0b022020-04-30 15:55:43 +0200384 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
385 SMC_LLC_CONFIRM_LINK);
386 if (!qentry) {
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100387 struct smc_clc_msg_decline dclc;
388
389 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100390 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100391 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100392 }
Karsten Graul649758f2020-05-04 14:18:48 +0200393 smc_llc_save_peer_uid(qentry);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200394 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_REQ);
395 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
396 if (rc)
Karsten Graul75d320d2018-03-01 13:51:31 +0100397 return SMC_CLC_DECL_RMBE_EC;
398
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100399 rc = smc_ib_modify_qp_rts(link);
400 if (rc)
Karsten Graul603cc142018-07-25 16:35:32 +0200401 return SMC_CLC_DECL_ERR_RDYLNK;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100402
403 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200404
Karsten Graul7562a132020-05-01 12:48:01 +0200405 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +0200406 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200407
Karsten Graul0fb0b022020-04-30 15:55:43 +0200408 /* confirm_rkey is implicit on 1st contact */
409 smc->conn.rmb_desc->is_conf_rkey = true;
410
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100411 /* send CONFIRM LINK response over RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200412 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100413 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200414 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100415
Karsten Graul0fb0b022020-04-30 15:55:43 +0200416 smc_llc_link_active(link);
Karsten Graul0a99be42020-05-05 15:01:20 +0200417 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200418
419 /* optional 2nd link, receive ADD LINK request from server */
420 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
421 SMC_LLC_ADD_LINK);
422 if (!qentry) {
Karsten Graul52bedf32018-03-01 13:51:32 +0100423 struct smc_clc_msg_decline dclc;
424
425 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100426 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200427 if (rc == -EAGAIN)
428 rc = 0; /* no DECLINE received, go with one link */
429 return rc;
Karsten Graul52bedf32018-03-01 13:51:32 +0100430 }
Karsten Graul0fb0b022020-04-30 15:55:43 +0200431 smc_llc_flow_qentry_clr(&link->lgr->llc_flow_lcl);
Karsten Graulb1570a82020-05-03 14:38:42 +0200432 smc_llc_cli_add_link(link, qentry);
Karsten Graul75d320d2018-03-01 13:51:31 +0100433 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100434}
435
Hans Wippel41349842018-06-28 19:05:11 +0200436static void smcr_conn_save_peer_info(struct smc_sock *smc,
437 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100438{
Ursula Braun3d9725a2020-09-10 18:48:23 +0200439 int bufsize = smc_uncompress_bufsize(clc->r0.rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200440
Ursula Braun3d9725a2020-09-10 18:48:23 +0200441 smc->conn.peer_rmbe_idx = clc->r0.rmbe_idx;
442 smc->conn.local_tx_ctrl.token = ntohl(clc->r0.rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200443 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100444 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200445 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100446}
447
Hans Wippel41349842018-06-28 19:05:11 +0200448static void smcd_conn_save_peer_info(struct smc_sock *smc,
449 struct smc_clc_msg_accept_confirm *clc)
450{
Ursula Braun3d9725a2020-09-10 18:48:23 +0200451 int bufsize = smc_uncompress_bufsize(clc->d0.dmbe_size);
Hans Wippel41349842018-06-28 19:05:11 +0200452
Ursula Braun3d9725a2020-09-10 18:48:23 +0200453 smc->conn.peer_rmbe_idx = clc->d0.dmbe_idx;
454 smc->conn.peer_token = clc->d0.token;
Hans Wippel41349842018-06-28 19:05:11 +0200455 /* msg header takes up space in the buffer */
456 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
457 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
458 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
459}
460
461static void smc_conn_save_peer_info(struct smc_sock *smc,
462 struct smc_clc_msg_accept_confirm *clc)
463{
464 if (smc->conn.lgr->is_smcd)
465 smcd_conn_save_peer_info(smc, clc);
466 else
467 smcr_conn_save_peer_info(smc, clc);
468}
469
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100470static void smc_link_save_peer_info(struct smc_link *link,
471 struct smc_clc_msg_accept_confirm *clc)
472{
Ursula Braun3d9725a2020-09-10 18:48:23 +0200473 link->peer_qpn = ntoh24(clc->r0.qpn);
474 memcpy(link->peer_gid, clc->r0.lcl.gid, SMC_GID_SIZE);
475 memcpy(link->peer_mac, clc->r0.lcl.mac, sizeof(link->peer_mac));
476 link->peer_psn = ntoh24(clc->r0.psn);
477 link->peer_mtu = clc->r0.qp_mtu;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100478}
479
Ursula Braun07603b22019-04-11 11:17:32 +0200480static void smc_switch_to_fallback(struct smc_sock *smc)
481{
482 smc->use_fallback = true;
483 if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
484 smc->clcsock->file = smc->sk.sk_socket->file;
485 smc->clcsock->file->private_data = smc->clcsock;
Ursula Braun67f562e2020-02-14 08:58:59 +0100486 smc->clcsock->wq.fasync_list =
487 smc->sk.sk_socket->wq.fasync_list;
Ursula Braun07603b22019-04-11 11:17:32 +0200488 }
489}
490
Hans Wippel3b2dec22018-05-18 09:34:18 +0200491/* fall back during connect */
Karsten Graul603cc142018-07-25 16:35:32 +0200492static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200493{
Ursula Braun07603b22019-04-11 11:17:32 +0200494 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +0200495 smc->fallback_rsn = reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200496 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200497 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200498 if (smc->sk.sk_state == SMC_INIT)
499 smc->sk.sk_state = SMC_ACTIVE;
500 return 0;
501}
502
503/* decline and fall back during connect */
504static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
505{
506 int rc;
507
Ursula Braune1bbdd52018-07-05 16:15:30 +0200508 if (reason_code < 0) { /* error, fallback is not possible */
509 if (smc->sk.sk_state == SMC_INIT)
510 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200511 return reason_code;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200512 }
Karsten Graul603cc142018-07-25 16:35:32 +0200513 if (reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200514 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200515 if (rc < 0) {
516 if (smc->sk.sk_state == SMC_INIT)
517 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200518 return rc;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200519 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200520 }
Karsten Graul603cc142018-07-25 16:35:32 +0200521 return smc_connect_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200522}
523
524/* abort connecting */
525static int smc_connect_abort(struct smc_sock *smc, int reason_code,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200526 int local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200527{
Ursula Braun51e3dfa2020-02-25 16:34:36 +0100528 bool is_smcd = smc->conn.lgr->is_smcd;
529
Ursula Braun5ac54d82020-09-10 18:48:21 +0200530 if (local_first)
Ursula Braun51e3dfa2020-02-25 16:34:36 +0100531 smc_lgr_cleanup_early(&smc->conn);
532 else
533 smc_conn_free(&smc->conn);
534 if (is_smcd)
Hans Wippel72a36a82019-02-07 15:56:18 +0100535 /* there is only one lgr role for SMC-D; use server lock */
536 mutex_unlock(&smc_server_lgr_pending);
537 else
538 mutex_unlock(&smc_client_lgr_pending);
539
Ursula Braun50717a32019-04-12 12:57:23 +0200540 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200541 return reason_code;
542}
543
544/* check if there is a rdma device available for this connection. */
545/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200546static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200547{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200548 /* PNET table look up: search active ib_device and port
549 * within same PNETID that also contains the ethernet device
550 * used for the internal TCP socket
551 */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200552 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +0200553 if (!ini->ib_dev)
554 return SMC_CLC_DECL_NOSMCRDEV;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200555 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200556}
557
Hans Wippel41349842018-06-28 19:05:11 +0200558/* check if there is an ISM device available for this connection. */
559/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200560static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200561{
562 /* Find ISM device with same PNETID as connecting interface */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200563 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
564 if (!ini->ism_dev)
Karsten Graul9aa68d22019-04-12 12:57:29 +0200565 return SMC_CLC_DECL_NOSMCDDEV;
Hans Wippel41349842018-06-28 19:05:11 +0200566 return 0;
567}
568
569/* Check for VLAN ID and register it on ISM device just for CLC handshake */
570static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200571 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200572{
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200573 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev, ini->vlan_id))
Karsten Graul7a627252019-04-12 12:57:30 +0200574 return SMC_CLC_DECL_ISMVLANERR;
Hans Wippel41349842018-06-28 19:05:11 +0200575 return 0;
576}
577
578/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
579 * used, the VLAN ID will be registered again during the connection setup.
580 */
581static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200582 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200583{
584 if (!is_smcd)
585 return 0;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200586 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev, ini->vlan_id))
Hans Wippel41349842018-06-28 19:05:11 +0200587 return SMC_CLC_DECL_CNFERR;
588 return 0;
589}
590
Hans Wippel3b2dec22018-05-18 09:34:18 +0200591/* CLC handshake during connect */
Hans Wippelc758dfd2018-06-28 19:05:09 +0200592static int smc_connect_clc(struct smc_sock *smc, int smc_type,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200593 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200594 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200595{
596 int rc = 0;
597
598 /* do inband token exchange */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200599 rc = smc_clc_send_proposal(smc, smc_type, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200600 if (rc)
601 return rc;
602 /* receive SMC Accept CLC message */
Ursula Braun2b59f582018-11-22 10:26:39 +0100603 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT,
604 CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200605}
606
Ursula Brauna046d572017-01-09 16:55:16 +0100607/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200608static int smc_connect_rdma(struct smc_sock *smc,
609 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200610 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200611{
Karsten Graul0fb0b022020-04-30 15:55:43 +0200612 int i, reason_code = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200613 struct smc_link *link;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200614
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200615 ini->is_smcd = false;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200616 ini->ib_lcl = &aclc->r0.lcl;
617 ini->ib_clcqpn = ntoh24(aclc->r0.qpn);
Ursula Braun5ac54d82020-09-10 18:48:21 +0200618 ini->first_contact_peer = aclc->hdr.flag;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200619
Hans Wippel72a36a82019-02-07 15:56:18 +0100620 mutex_lock(&smc_client_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200621 reason_code = smc_conn_create(smc, ini);
622 if (reason_code) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100623 mutex_unlock(&smc_client_lgr_pending);
624 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200625 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200626
627 smc_conn_save_peer_info(smc, aclc);
628
Ursula Braun5ac54d82020-09-10 18:48:21 +0200629 if (ini->first_contact_local) {
Karsten Graul0fb0b022020-04-30 15:55:43 +0200630 link = smc->conn.lnk;
631 } else {
632 /* set link that was assigned by server */
633 link = NULL;
634 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
635 struct smc_link *l = &smc->conn.lgr->lnk[i];
636
Ursula Braun3d9725a2020-09-10 18:48:23 +0200637 if (l->peer_qpn == ntoh24(aclc->r0.qpn) &&
638 !memcmp(l->peer_gid, &aclc->r0.lcl.gid,
639 SMC_GID_SIZE) &&
640 !memcmp(l->peer_mac, &aclc->r0.lcl.mac,
641 sizeof(l->peer_mac))) {
Karsten Graul0fb0b022020-04-30 15:55:43 +0200642 link = l;
643 break;
644 }
645 }
646 if (!link)
647 return smc_connect_abort(smc, SMC_CLC_DECL_NOSRVLINK,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200648 ini->first_contact_local);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200649 smc->conn.lnk = link;
650 }
651
Hans Wippel3b2dec22018-05-18 09:34:18 +0200652 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200653 if (smc_buf_create(smc, false))
Karsten Graul7a627252019-04-12 12:57:30 +0200654 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200655 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200656
Ursula Braun5ac54d82020-09-10 18:48:21 +0200657 if (ini->first_contact_local)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200658 smc_link_save_peer_info(link, aclc);
659
Karsten Graule07d31d2020-04-29 17:10:42 +0200660 if (smc_rmb_rtoken_handling(&smc->conn, link, aclc))
Karsten Graul603cc142018-07-25 16:35:32 +0200661 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200662 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200663
664 smc_close_init(smc);
665 smc_rx_init(smc);
666
Ursula Braun5ac54d82020-09-10 18:48:21 +0200667 if (ini->first_contact_local) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200668 if (smc_ib_ready_link(link))
Karsten Graul603cc142018-07-25 16:35:32 +0200669 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200670 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200671 } else {
Karsten Graul7562a132020-05-01 12:48:01 +0200672 if (smcr_lgr_reg_rmbs(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +0200673 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200674 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200675 }
676 smc_rmb_sync_sg_for_device(&smc->conn);
677
678 reason_code = smc_clc_send_confirm(smc);
679 if (reason_code)
Karsten Graul7a627252019-04-12 12:57:30 +0200680 return smc_connect_abort(smc, reason_code,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200681 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200682
683 smc_tx_init(smc);
684
Ursula Braun5ac54d82020-09-10 18:48:21 +0200685 if (ini->first_contact_local) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200686 /* QP confirmation over RoCE fabric */
Karsten Graul0fb0b022020-04-30 15:55:43 +0200687 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
Karsten Graulb9247542020-04-29 17:10:41 +0200688 reason_code = smcr_clnt_conf_first_link(smc);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200689 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200690 if (reason_code)
691 return smc_connect_abort(smc, reason_code,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200692 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200693 }
Hans Wippel72a36a82019-02-07 15:56:18 +0100694 mutex_unlock(&smc_client_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200695
696 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200697 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200698 if (smc->sk.sk_state == SMC_INIT)
699 smc->sk.sk_state = SMC_ACTIVE;
700
701 return 0;
702}
703
Hans Wippel41349842018-06-28 19:05:11 +0200704/* setup for ISM connection of client */
705static int smc_connect_ism(struct smc_sock *smc,
706 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200707 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200708{
Hans Wippel41349842018-06-28 19:05:11 +0200709 int rc = 0;
710
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200711 ini->is_smcd = true;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200712 ini->ism_peer_gid = aclc->d0.gid;
Ursula Braun5ac54d82020-09-10 18:48:21 +0200713 ini->first_contact_peer = aclc->hdr.flag;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200714
Hans Wippel72a36a82019-02-07 15:56:18 +0100715 /* there is only one lgr role for SMC-D; use server lock */
716 mutex_lock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200717 rc = smc_conn_create(smc, ini);
718 if (rc) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100719 mutex_unlock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200720 return rc;
Hans Wippel72a36a82019-02-07 15:56:18 +0100721 }
Hans Wippel41349842018-06-28 19:05:11 +0200722
723 /* Create send and receive buffers */
Karsten Graul72b7f6c2020-07-26 20:34:28 +0200724 rc = smc_buf_create(smc, true);
725 if (rc)
726 return smc_connect_abort(smc, (rc == -ENOSPC) ?
727 SMC_CLC_DECL_MAX_DMB :
728 SMC_CLC_DECL_MEM,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200729 ini->first_contact_local);
Hans Wippel41349842018-06-28 19:05:11 +0200730
731 smc_conn_save_peer_info(smc, aclc);
732 smc_close_init(smc);
733 smc_rx_init(smc);
734 smc_tx_init(smc);
735
736 rc = smc_clc_send_confirm(smc);
737 if (rc)
Ursula Braun5ac54d82020-09-10 18:48:21 +0200738 return smc_connect_abort(smc, rc, ini->first_contact_local);
Hans Wippel72a36a82019-02-07 15:56:18 +0100739 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel41349842018-06-28 19:05:11 +0200740
741 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200742 smc->connect_nonblock = 0;
Hans Wippel41349842018-06-28 19:05:11 +0200743 if (smc->sk.sk_state == SMC_INIT)
744 smc->sk.sk_state = SMC_ACTIVE;
745
746 return 0;
747}
748
Hans Wippel3b2dec22018-05-18 09:34:18 +0200749/* perform steps before actually connecting */
750static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100751{
Hans Wippel41349842018-06-28 19:05:11 +0200752 bool ism_supported = false, rdma_supported = false;
Ursula Brauna046d572017-01-09 16:55:16 +0100753 struct smc_clc_msg_accept_confirm aclc;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200754 struct smc_init_info ini = {0};
Hans Wippel41349842018-06-28 19:05:11 +0200755 int smc_type;
Ursula Brauna046d572017-01-09 16:55:16 +0100756 int rc = 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100757
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200758 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +0200759 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200760
Hans Wippel3b2dec22018-05-18 09:34:18 +0200761 /* if peer has not signalled SMC-capability, fall back */
762 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +0200763 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200764
Ursula Brauna046d572017-01-09 16:55:16 +0100765 /* IPSec connections opt out of SMC-R optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200766 if (using_ipsec(smc))
767 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
Ursula Brauna046d572017-01-09 16:55:16 +0100768
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200769 /* get vlan id from IP device */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200770 if (smc_vlan_by_tcpsk(smc->clcsock, &ini))
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200771 return smc_connect_decline_fallback(smc,
772 SMC_CLC_DECL_GETVLANERR);
Hans Wippel41349842018-06-28 19:05:11 +0200773
774 /* check if there is an ism device available */
Karsten Graul228bae02019-04-12 12:57:28 +0200775 if (!smc_find_ism_device(smc, &ini) &&
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200776 !smc_connect_ism_vlan_setup(smc, &ini)) {
Hans Wippel41349842018-06-28 19:05:11 +0200777 /* ISM is supported for this connection */
778 ism_supported = true;
779 smc_type = SMC_TYPE_D;
780 }
781
782 /* check if there is a rdma device available */
Karsten Graul228bae02019-04-12 12:57:28 +0200783 if (!smc_find_rdma_device(smc, &ini)) {
Hans Wippel41349842018-06-28 19:05:11 +0200784 /* RDMA is supported for this connection */
785 rdma_supported = true;
786 if (ism_supported)
787 smc_type = SMC_TYPE_B; /* both */
788 else
789 smc_type = SMC_TYPE_R; /* only RDMA */
790 }
791
792 /* if neither ISM nor RDMA are supported, fallback */
793 if (!rdma_supported && !ism_supported)
Karsten Graul603cc142018-07-25 16:35:32 +0200794 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV);
Ursula Brauna046d572017-01-09 16:55:16 +0100795
Hans Wippel3b2dec22018-05-18 09:34:18 +0200796 /* perform CLC handshake */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200797 rc = smc_connect_clc(smc, smc_type, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200798 if (rc) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200799 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200800 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200801 }
Ursula Brauna046d572017-01-09 16:55:16 +0100802
Hans Wippel41349842018-06-28 19:05:11 +0200803 /* depending on previous steps, connect using rdma or ism */
804 if (rdma_supported && aclc.hdr.path == SMC_TYPE_R)
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200805 rc = smc_connect_rdma(smc, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200806 else if (ism_supported && aclc.hdr.path == SMC_TYPE_D)
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200807 rc = smc_connect_ism(smc, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200808 else
Karsten Graul603cc142018-07-25 16:35:32 +0200809 rc = SMC_CLC_DECL_MODEUNSUPP;
Hans Wippel41349842018-06-28 19:05:11 +0200810 if (rc) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200811 smc_connect_ism_vlan_cleanup(smc, ism_supported, &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
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200815 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200816 return 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100817}
818
Ursula Braun24ac3a02018-06-27 17:59:50 +0200819static void smc_connect_work(struct work_struct *work)
820{
821 struct smc_sock *smc = container_of(work, struct smc_sock,
822 connect_work);
Ursula Braun50717a32019-04-12 12:57:23 +0200823 long timeo = smc->sk.sk_sndtimeo;
824 int rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200825
Ursula Braun50717a32019-04-12 12:57:23 +0200826 if (!timeo)
827 timeo = MAX_SCHEDULE_TIMEOUT;
828 lock_sock(smc->clcsock->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200829 if (smc->clcsock->sk->sk_err) {
830 smc->sk.sk_err = smc->clcsock->sk->sk_err;
Ursula Braun50717a32019-04-12 12:57:23 +0200831 } else if ((1 << smc->clcsock->sk->sk_state) &
832 (TCPF_SYN_SENT | TCP_SYN_RECV)) {
833 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
834 if ((rc == -EPIPE) &&
835 ((1 << smc->clcsock->sk->sk_state) &
836 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
837 rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200838 }
Ursula Braun50717a32019-04-12 12:57:23 +0200839 release_sock(smc->clcsock->sk);
840 lock_sock(&smc->sk);
841 if (rc != 0 || smc->sk.sk_err) {
842 smc->sk.sk_state = SMC_CLOSED;
843 if (rc == -EPIPE || rc == -EAGAIN)
844 smc->sk.sk_err = EPIPE;
845 else if (signal_pending(current))
846 smc->sk.sk_err = -sock_intr_errno(timeo);
Ursula Braun6d6dd522019-11-12 16:03:41 +0100847 sock_put(&smc->sk); /* passive closing */
Ursula Braun24ac3a02018-06-27 17:59:50 +0200848 goto out;
849 }
850
851 rc = __smc_connect(smc);
852 if (rc < 0)
853 smc->sk.sk_err = -rc;
854
855out:
Ursula Braun07603b22019-04-11 11:17:32 +0200856 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
857 if (smc->sk.sk_err) {
858 smc->sk.sk_state_change(&smc->sk);
859 } else { /* allow polling before and after fallback decision */
860 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
861 smc->sk.sk_write_space(&smc->sk);
862 }
863 }
Ursula Braun24ac3a02018-06-27 17:59:50 +0200864 release_sock(&smc->sk);
865}
866
Ursula Braunac713872017-01-09 16:55:13 +0100867static int smc_connect(struct socket *sock, struct sockaddr *addr,
868 int alen, int flags)
869{
870 struct sock *sk = sock->sk;
871 struct smc_sock *smc;
872 int rc = -EINVAL;
873
874 smc = smc_sk(sk);
875
876 /* separate smc parameter checking to be safe */
877 if (alen < sizeof(addr->sa_family))
878 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100879 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100880 goto out_err;
881
882 lock_sock(sk);
883 switch (sk->sk_state) {
884 default:
885 goto out;
886 case SMC_ACTIVE:
887 rc = -EISCONN;
888 goto out;
889 case SMC_INIT:
890 rc = 0;
891 break;
892 }
893
894 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200895 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun50717a32019-04-12 12:57:23 +0200896 if (smc->connect_nonblock) {
897 rc = -EALREADY;
898 goto out;
899 }
900 rc = kernel_connect(smc->clcsock, addr, alen, flags);
901 if (rc && rc != -EINPROGRESS)
902 goto out;
Ursula Braun301428e2019-10-29 12:41:26 +0100903
904 sock_hold(&smc->sk); /* sock put in passive closing */
Ursula Braun86434742019-12-12 22:35:58 +0100905 if (smc->use_fallback)
906 goto out;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200907 if (flags & O_NONBLOCK) {
Ursula Braun50717a32019-04-12 12:57:23 +0200908 if (schedule_work(&smc->connect_work))
909 smc->connect_nonblock = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200910 rc = -EINPROGRESS;
911 } else {
Ursula Braun24ac3a02018-06-27 17:59:50 +0200912 rc = __smc_connect(smc);
913 if (rc < 0)
914 goto out;
915 else
916 rc = 0; /* success cases including fallback */
917 }
Ursula Braunac713872017-01-09 16:55:13 +0100918
919out:
920 release_sock(sk);
921out_err:
922 return rc;
923}
924
925static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
926{
Ursula Braun3163c502018-01-24 10:28:12 +0100927 struct socket *new_clcsock = NULL;
928 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100929 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -0800930 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +0100931
Ursula Braun3163c502018-01-24 10:28:12 +0100932 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100933 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100934 if (!new_sk) {
935 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100936 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100937 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100938 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100939 goto out;
940 }
941 *new_smc = smc_sk(new_sk);
942
Myungho Jung78abe3d2018-12-18 09:02:25 -0800943 mutex_lock(&lsmc->clcsock_release_lock);
944 if (lsmc->clcsock)
Ursula Brauna60a2b12020-09-10 18:48:20 +0200945 rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800946 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +0100947 lock_sock(lsk);
Ursula Brauna60a2b12020-09-10 18:48:20 +0200948 if (rc < 0 && rc != -EAGAIN)
Ursula Braun3163c502018-01-24 10:28:12 +0100949 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100950 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +0200951 new_sk->sk_prot->unhash(new_sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100952 if (new_clcsock)
953 sock_release(new_clcsock);
954 new_sk->sk_state = SMC_CLOSED;
955 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun51f1de72018-01-26 09:28:48 +0100956 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100957 *new_smc = NULL;
958 goto out;
959 }
960
Ursula Brauna60a2b12020-09-10 18:48:20 +0200961 /* new clcsock has inherited the smc listen-specific sk_data_ready
962 * function; switch it back to the original sk_data_ready function
963 */
964 new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready;
Ursula Braunac713872017-01-09 16:55:13 +0100965 (*new_smc)->clcsock = new_clcsock;
966out:
967 return rc;
968}
969
Ursula Brauna046d572017-01-09 16:55:16 +0100970/* add a just created sock to the accept queue of the listen sock as
971 * candidate for a following socket accept call from user space
972 */
973static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
974{
975 struct smc_sock *par = smc_sk(parent);
976
Ursula Braun51f1de72018-01-26 09:28:48 +0100977 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100978 spin_lock(&par->accept_q_lock);
979 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
980 spin_unlock(&par->accept_q_lock);
981 sk_acceptq_added(parent);
982}
983
984/* remove a socket from the accept queue of its parental listening socket */
985static void smc_accept_unlink(struct sock *sk)
986{
987 struct smc_sock *par = smc_sk(sk)->listen_smc;
988
989 spin_lock(&par->accept_q_lock);
990 list_del_init(&smc_sk(sk)->accept_q);
991 spin_unlock(&par->accept_q_lock);
992 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100993 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100994}
995
996/* remove a sock from the accept queue to bind it to a new socket created
997 * for a socket accept call from user space
998 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100999struct sock *smc_accept_dequeue(struct sock *parent,
1000 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +01001001{
1002 struct smc_sock *isk, *n;
1003 struct sock *new_sk;
1004
1005 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
1006 new_sk = (struct sock *)isk;
1007
1008 smc_accept_unlink(new_sk);
1009 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +02001010 new_sk->sk_prot->unhash(new_sk);
Ursula Braun127f4972018-01-26 09:28:49 +01001011 if (isk->clcsock) {
1012 sock_release(isk->clcsock);
1013 isk->clcsock = NULL;
1014 }
Ursula Braun51f1de72018-01-26 09:28:48 +01001015 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +01001016 continue;
1017 }
Ursula Braun07603b22019-04-11 11:17:32 +02001018 if (new_sock) {
Ursula Brauna046d572017-01-09 16:55:16 +01001019 sock_graft(new_sk, new_sock);
Ursula Braun07603b22019-04-11 11:17:32 +02001020 if (isk->use_fallback) {
1021 smc_sk(new_sk)->clcsock->file = new_sock->file;
1022 isk->clcsock->file->private_data = isk->clcsock;
1023 }
1024 }
Ursula Brauna046d572017-01-09 16:55:16 +01001025 return new_sk;
1026 }
1027 return NULL;
1028}
1029
1030/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +01001031void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +01001032{
1033 struct smc_sock *smc = smc_sk(sk);
1034
Ursula Braun81cf4f42019-10-21 16:13:15 +02001035 sock_hold(sk); /* sock_put below */
Ursula Braunb38d7322017-01-09 16:55:25 +01001036 lock_sock(sk);
1037 if (!sk->sk_lingertime)
1038 /* wait for peer closing */
1039 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun39f41f32019-06-27 15:04:52 +02001040 __smc_release(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +01001041 release_sock(sk);
Ursula Braun81cf4f42019-10-21 16:13:15 +02001042 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +01001043 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +01001044}
1045
Karsten Graulb9247542020-04-29 17:10:41 +02001046static int smcr_serv_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001047{
Karsten Graul387707f2020-04-29 17:10:40 +02001048 struct smc_link *link = smc->conn.lnk;
Karsten Graul4667bb42020-04-30 15:55:42 +02001049 struct smc_llc_qentry *qentry;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001050 int rc;
1051
Karsten Graul7562a132020-05-01 12:48:01 +02001052 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +02001053 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +02001054
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001055 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +02001056 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001057 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001058 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001059
1060 /* receive CONFIRM LINK response from client over the RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001061 qentry = smc_llc_wait(link->lgr, link, SMC_LLC_WAIT_TIME,
1062 SMC_LLC_CONFIRM_LINK);
1063 if (!qentry) {
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001064 struct smc_clc_msg_decline dclc;
1065
1066 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001067 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001068 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001069 }
Karsten Graul649758f2020-05-04 14:18:48 +02001070 smc_llc_save_peer_uid(qentry);
Karsten Graul4667bb42020-04-30 15:55:42 +02001071 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_RESP);
1072 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
1073 if (rc)
Karsten Graul75d320d2018-03-01 13:51:31 +01001074 return SMC_CLC_DECL_RMBE_EC;
1075
Karsten Graul4667bb42020-04-30 15:55:42 +02001076 /* confirm_rkey is implicit on 1st contact */
1077 smc->conn.rmb_desc->is_conf_rkey = true;
Karsten Graul52bedf32018-03-01 13:51:32 +01001078
Karsten Graul00a049c2020-04-29 17:10:49 +02001079 smc_llc_link_active(link);
Karsten Graul0a99be42020-05-05 15:01:20 +02001080 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
Karsten Graul52bedf32018-03-01 13:51:32 +01001081
Karsten Graul4667bb42020-04-30 15:55:42 +02001082 /* initial contact - try to establish second link */
Karsten Graul2d2209f2020-05-03 14:38:43 +02001083 smc_llc_srv_add_link(link);
Karsten Graul75d320d2018-03-01 13:51:31 +01001084 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001085}
1086
Hans Wippel3b2dec22018-05-18 09:34:18 +02001087/* listen worker: finish */
1088static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001089{
Ursula Brauna046d572017-01-09 16:55:16 +01001090 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001091 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001092
Ursula Brauna046d572017-01-09 16:55:16 +01001093 if (lsmc->sk.sk_state == SMC_LISTEN) {
Karsten Graulfd577702019-04-11 11:17:30 +02001094 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001095 smc_accept_enqueue(&lsmc->sk, newsmcsk);
Karsten Graulfd577702019-04-11 11:17:30 +02001096 release_sock(&lsmc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001097 } else { /* no longer listening */
1098 smc_close_non_accepted(newsmcsk);
1099 }
Ursula Brauna046d572017-01-09 16:55:16 +01001100
1101 /* Wake up accept */
1102 lsmc->sk.sk_data_ready(&lsmc->sk);
1103 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001104}
Ursula Brauna046d572017-01-09 16:55:16 +01001105
Hans Wippel3b2dec22018-05-18 09:34:18 +02001106/* listen worker: finish in state connected */
1107static void smc_listen_out_connected(struct smc_sock *new_smc)
1108{
1109 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001110
Hans Wippel3b2dec22018-05-18 09:34:18 +02001111 sk_refcnt_debug_inc(newsmcsk);
1112 if (newsmcsk->sk_state == SMC_INIT)
1113 newsmcsk->sk_state = SMC_ACTIVE;
1114
1115 smc_listen_out(new_smc);
1116}
1117
1118/* listen worker: finish in error state */
1119static void smc_listen_out_err(struct smc_sock *new_smc)
1120{
1121 struct sock *newsmcsk = &new_smc->sk;
1122
Ursula Braun51f1de72018-01-26 09:28:48 +01001123 if (newsmcsk->sk_state == SMC_INIT)
1124 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001125 newsmcsk->sk_state = SMC_CLOSED;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001126
1127 smc_listen_out(new_smc);
1128}
1129
1130/* listen worker: decline and fall back if possible */
1131static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
Ursula Braun5ac54d82020-09-10 18:48:21 +02001132 bool local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001133{
1134 /* RDMA setup failed, switch back to TCP */
Ursula Braun5ac54d82020-09-10 18:48:21 +02001135 if (local_first)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001136 smc_lgr_cleanup_early(&new_smc->conn);
1137 else
1138 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001139 if (reason_code < 0) { /* error, no fallback possible */
1140 smc_listen_out_err(new_smc);
1141 return;
1142 }
Ursula Braun07603b22019-04-11 11:17:32 +02001143 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001144 new_smc->fallback_rsn = reason_code;
1145 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001146 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1147 smc_listen_out_err(new_smc);
1148 return;
1149 }
1150 }
1151 smc_listen_out_connected(new_smc);
1152}
1153
1154/* listen worker: check prefixes */
Karsten Graul59886692019-04-12 12:57:25 +02001155static int smc_listen_prfx_check(struct smc_sock *new_smc,
Hans Wippel3b2dec22018-05-18 09:34:18 +02001156 struct smc_clc_msg_proposal *pclc)
1157{
1158 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1159 struct socket *newclcsock = new_smc->clcsock;
1160
1161 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1162 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
Karsten Graul59886692019-04-12 12:57:25 +02001163 return SMC_CLC_DECL_DIFFPREFIX;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001164
1165 return 0;
1166}
1167
1168/* listen worker: initialize connection and buffers */
1169static int smc_listen_rdma_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001170 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001171{
Karsten Graul7a627252019-04-12 12:57:30 +02001172 int rc;
1173
Hans Wippel3b2dec22018-05-18 09:34:18 +02001174 /* allocate connection / link group */
Karsten Graul7a627252019-04-12 12:57:30 +02001175 rc = smc_conn_create(new_smc, ini);
1176 if (rc)
1177 return rc;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001178
1179 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001180 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001181 return SMC_CLC_DECL_MEM;
1182
1183 return 0;
1184}
1185
Hans Wippel41349842018-06-28 19:05:11 +02001186/* listen worker: initialize connection and buffers for SMC-D */
1187static int smc_listen_ism_init(struct smc_sock *new_smc,
1188 struct smc_clc_msg_proposal *pclc,
Karsten Graul7a627252019-04-12 12:57:30 +02001189 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +02001190{
Karsten Graul7a627252019-04-12 12:57:30 +02001191 int rc;
Hans Wippel41349842018-06-28 19:05:11 +02001192
Karsten Graul7a627252019-04-12 12:57:30 +02001193 rc = smc_conn_create(new_smc, ini);
1194 if (rc)
1195 return rc;
Hans Wippel41349842018-06-28 19:05:11 +02001196
Hans Wippel41349842018-06-28 19:05:11 +02001197 /* Create send and receive buffers */
Karsten Graul72b7f6c2020-07-26 20:34:28 +02001198 rc = smc_buf_create(new_smc, true);
1199 if (rc) {
Ursula Braun5ac54d82020-09-10 18:48:21 +02001200 if (ini->first_contact_local)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001201 smc_lgr_cleanup_early(&new_smc->conn);
1202 else
1203 smc_conn_free(&new_smc->conn);
Karsten Graul72b7f6c2020-07-26 20:34:28 +02001204 return (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB :
1205 SMC_CLC_DECL_MEM;
Hans Wippel41349842018-06-28 19:05:11 +02001206 }
1207
1208 return 0;
1209}
1210
Hans Wippel3b2dec22018-05-18 09:34:18 +02001211/* listen worker: register buffers */
Ursula Braun5ac54d82020-09-10 18:48:21 +02001212static int smc_listen_rdma_reg(struct smc_sock *new_smc, bool local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001213{
Karsten Graulb9247542020-04-29 17:10:41 +02001214 struct smc_connection *conn = &new_smc->conn;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001215
Ursula Braun5ac54d82020-09-10 18:48:21 +02001216 if (!local_first) {
Karsten Graul7562a132020-05-01 12:48:01 +02001217 if (smcr_lgr_reg_rmbs(conn->lnk, conn->rmb_desc))
Karsten Graulc7674c02018-11-22 10:26:43 +01001218 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001219 }
1220 smc_rmb_sync_sg_for_device(&new_smc->conn);
1221
1222 return 0;
1223}
1224
1225/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001226static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1227 struct smc_clc_msg_accept_confirm *cclc,
Ursula Braun5ac54d82020-09-10 18:48:21 +02001228 bool local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001229{
Karsten Graul387707f2020-04-29 17:10:40 +02001230 struct smc_link *link = new_smc->conn.lnk;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001231 int reason_code = 0;
1232
Ursula Braun5ac54d82020-09-10 18:48:21 +02001233 if (local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001234 smc_link_save_peer_info(link, cclc);
1235
Ursula Braun0c881ad2020-09-10 18:48:25 +02001236 if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc))
1237 return SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001238
Ursula Braun5ac54d82020-09-10 18:48:21 +02001239 if (local_first) {
Ursula Braun0c881ad2020-09-10 18:48:25 +02001240 if (smc_ib_ready_link(link))
1241 return SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001242 /* QP confirmation over RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001243 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
Karsten Graulb9247542020-04-29 17:10:41 +02001244 reason_code = smcr_serv_conf_first_link(new_smc);
Karsten Graul4667bb42020-04-30 15:55:42 +02001245 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001246 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001247 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001248}
1249
1250/* setup for RDMA connection of server */
1251static void smc_listen_work(struct work_struct *work)
1252{
1253 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1254 smc_listen_work);
1255 struct socket *newclcsock = new_smc->clcsock;
1256 struct smc_clc_msg_accept_confirm cclc;
Ursula Braun6bb14e482020-09-10 18:48:22 +02001257 struct smc_clc_msg_proposal_area *buf;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001258 struct smc_clc_msg_proposal *pclc;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001259 struct smc_init_info ini = {0};
Hans Wippel41349842018-06-28 19:05:11 +02001260 bool ism_supported = false;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001261 int rc = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001262
Karsten Graulfd577702019-04-11 11:17:30 +02001263 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1264 return smc_listen_out_err(new_smc);
1265
Hans Wippel3b2dec22018-05-18 09:34:18 +02001266 if (new_smc->use_fallback) {
1267 smc_listen_out_connected(new_smc);
1268 return;
1269 }
1270
1271 /* check if peer is smc capable */
1272 if (!tcp_sk(newclcsock->sk)->syn_smc) {
Ursula Braun07603b22019-04-11 11:17:32 +02001273 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001274 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001275 smc_listen_out_connected(new_smc);
1276 return;
1277 }
1278
1279 /* do inband token exchange -
1280 * wait for and receive SMC Proposal CLC message
1281 */
Ursula Braun6bb14e482020-09-10 18:48:22 +02001282 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
1283 if (!buf) {
1284 rc = SMC_CLC_DECL_MEM;
1285 goto out_decl;
1286 }
1287 pclc = (struct smc_clc_msg_proposal *)buf;
1288 rc = smc_clc_wait_msg(new_smc, pclc, sizeof(*buf),
Karsten Graul228bae02019-04-12 12:57:28 +02001289 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001290 if (rc)
1291 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001292
1293 /* IPSec connections opt out of SMC-R optimizations */
1294 if (using_ipsec(new_smc)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001295 rc = SMC_CLC_DECL_IPSEC;
1296 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001297 }
1298
Karsten Graul59886692019-04-12 12:57:25 +02001299 /* check for matching IP prefix and subnet length */
1300 rc = smc_listen_prfx_check(new_smc, pclc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001301 if (rc)
1302 goto out_decl;
Karsten Graul59886692019-04-12 12:57:25 +02001303
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001304 /* get vlan id from IP device */
1305 if (smc_vlan_by_tcpsk(new_smc->clcsock, &ini)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001306 rc = SMC_CLC_DECL_GETVLANERR;
1307 goto out_decl;
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001308 }
1309
Hans Wippel72a36a82019-02-07 15:56:18 +01001310 mutex_lock(&smc_server_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001311 smc_close_init(new_smc);
1312 smc_rx_init(new_smc);
1313 smc_tx_init(new_smc);
1314
Hans Wippel41349842018-06-28 19:05:11 +02001315 /* check if ISM is available */
Karsten Graul9aa68d22019-04-12 12:57:29 +02001316 if (pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) {
Ursula Braun7b2977d2020-09-10 18:48:24 +02001317 struct smc_clc_msg_smcd *pclc_smcd = smc_get_clc_msg_smcd(pclc);
1318
Karsten Graul9aa68d22019-04-12 12:57:29 +02001319 ini.is_smcd = true; /* prepare ISM check */
Ursula Braun7b2977d2020-09-10 18:48:24 +02001320 ini.ism_peer_gid = pclc_smcd->gid;
Karsten Graul9aa68d22019-04-12 12:57:29 +02001321 rc = smc_find_ism_device(new_smc, &ini);
1322 if (!rc)
Karsten Graul7a627252019-04-12 12:57:30 +02001323 rc = smc_listen_ism_init(new_smc, pclc, &ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001324 if (!rc)
1325 ism_supported = true;
1326 else if (pclc->hdr.path == SMC_TYPE_D)
1327 goto out_unlock; /* skip RDMA and decline */
1328 }
1329
1330 /* check if RDMA is available */
1331 if (!ism_supported) { /* SMC_TYPE_R or SMC_TYPE_B */
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001332 /* prepare RDMA check */
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001333 ini.is_smcd = false;
Ursula Braunca5f8d22019-10-23 15:44:06 +02001334 ini.ism_dev = NULL;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001335 ini.ib_lcl = &pclc->lcl;
Karsten Graul9aa68d22019-04-12 12:57:29 +02001336 rc = smc_find_rdma_device(new_smc, &ini);
1337 if (rc) {
1338 /* no RDMA device found */
1339 if (pclc->hdr.path == SMC_TYPE_B)
1340 /* neither ISM nor RDMA device found */
1341 rc = SMC_CLC_DECL_NOSMCDEV;
1342 goto out_unlock;
1343 }
Karsten Graul7a627252019-04-12 12:57:30 +02001344 rc = smc_listen_rdma_init(new_smc, &ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001345 if (rc)
1346 goto out_unlock;
Ursula Braun5ac54d82020-09-10 18:48:21 +02001347 rc = smc_listen_rdma_reg(new_smc, ini.first_contact_local);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001348 if (rc)
1349 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001350 }
1351
1352 /* send SMC Accept CLC message */
Ursula Braun5ac54d82020-09-10 18:48:21 +02001353 rc = smc_clc_send_accept(new_smc, ini.first_contact_local);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001354 if (rc)
1355 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001356
Hans Wippel62c71392019-02-07 15:56:17 +01001357 /* SMC-D does not need this lock any more */
1358 if (ism_supported)
Hans Wippel72a36a82019-02-07 15:56:18 +01001359 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001360
Hans Wippel3b2dec22018-05-18 09:34:18 +02001361 /* receive SMC Confirm CLC message */
Karsten Graul228bae02019-04-12 12:57:28 +02001362 rc = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
1363 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
1364 if (rc) {
Hans Wippel62c71392019-02-07 15:56:17 +01001365 if (!ism_supported)
Karsten Graul9aa68d22019-04-12 12:57:29 +02001366 goto out_unlock;
1367 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001368 }
1369
1370 /* finish worker */
Ursula Braun6bb14e482020-09-10 18:48:22 +02001371 kfree(buf);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001372 if (!ism_supported) {
Karsten Graul7a627252019-04-12 12:57:30 +02001373 rc = smc_listen_rdma_finish(new_smc, &cclc,
Ursula Braun5ac54d82020-09-10 18:48:21 +02001374 ini.first_contact_local);
Hans Wippel62c71392019-02-07 15:56:17 +01001375 if (rc)
Ursula Braun0c881ad2020-09-10 18:48:25 +02001376 goto out_unlock;
1377 mutex_unlock(&smc_server_lgr_pending);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001378 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001379 smc_conn_save_peer_info(new_smc, &cclc);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001380 smc_listen_out_connected(new_smc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001381 return;
1382
1383out_unlock:
1384 mutex_unlock(&smc_server_lgr_pending);
1385out_decl:
Ursula Braun5ac54d82020-09-10 18:48:21 +02001386 smc_listen_decline(new_smc, rc, ini.first_contact_local);
Ursula Braun6bb14e482020-09-10 18:48:22 +02001387 kfree(buf);
Ursula Brauna046d572017-01-09 16:55:16 +01001388}
1389
1390static void smc_tcp_listen_work(struct work_struct *work)
1391{
1392 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1393 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001394 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001395 struct smc_sock *new_smc;
1396 int rc = 0;
1397
Ursula Braun3163c502018-01-24 10:28:12 +01001398 lock_sock(lsk);
1399 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001400 rc = smc_clcsock_accept(lsmc, &new_smc);
Ursula Brauna60a2b12020-09-10 18:48:20 +02001401 if (rc) /* clcsock accept queue empty or error */
Ursula Brauna046d572017-01-09 16:55:16 +01001402 goto out;
1403 if (!new_smc)
1404 continue;
1405
1406 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001407 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001408 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001409 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001410 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1411 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001412 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1413 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001414 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1415 if (!schedule_work(&new_smc->smc_listen_work))
1416 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001417 }
1418
1419out:
Ursula Braun3163c502018-01-24 10:28:12 +01001420 release_sock(lsk);
Ursula Brauna60a2b12020-09-10 18:48:20 +02001421 sock_put(&lsmc->sk); /* sock_hold in smc_clcsock_data_ready() */
1422}
1423
1424static void smc_clcsock_data_ready(struct sock *listen_clcsock)
1425{
1426 struct smc_sock *lsmc;
1427
1428 lsmc = (struct smc_sock *)
1429 ((uintptr_t)listen_clcsock->sk_user_data & ~SK_USER_DATA_NOCOPY);
1430 if (!lsmc)
1431 return;
1432 lsmc->clcsk_data_ready(listen_clcsock);
1433 if (lsmc->sk.sk_state == SMC_LISTEN) {
1434 sock_hold(&lsmc->sk); /* sock_put in smc_tcp_listen_work() */
1435 if (!schedule_work(&lsmc->tcp_listen_work))
1436 sock_put(&lsmc->sk);
1437 }
Ursula Brauna046d572017-01-09 16:55:16 +01001438}
1439
Ursula Braunac713872017-01-09 16:55:13 +01001440static int smc_listen(struct socket *sock, int backlog)
1441{
1442 struct sock *sk = sock->sk;
1443 struct smc_sock *smc;
1444 int rc;
1445
1446 smc = smc_sk(sk);
1447 lock_sock(sk);
1448
1449 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +02001450 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
1451 smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +01001452 goto out;
1453
1454 rc = 0;
1455 if (sk->sk_state == SMC_LISTEN) {
1456 sk->sk_max_ack_backlog = backlog;
1457 goto out;
1458 }
1459 /* some socket options are handled in core, so we could not apply
1460 * them to the clc socket -- copy smc socket options to clc socket
1461 */
1462 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001463 if (!smc->use_fallback)
1464 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001465
Ursula Brauna60a2b12020-09-10 18:48:20 +02001466 /* save original sk_data_ready function and establish
1467 * smc-specific sk_data_ready function
1468 */
1469 smc->clcsk_data_ready = smc->clcsock->sk->sk_data_ready;
1470 smc->clcsock->sk->sk_data_ready = smc_clcsock_data_ready;
1471 smc->clcsock->sk->sk_user_data =
1472 (void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY);
Ursula Braunac713872017-01-09 16:55:13 +01001473 rc = kernel_listen(smc->clcsock, backlog);
1474 if (rc)
1475 goto out;
1476 sk->sk_max_ack_backlog = backlog;
1477 sk->sk_ack_backlog = 0;
1478 sk->sk_state = SMC_LISTEN;
1479
1480out:
1481 release_sock(sk);
1482 return rc;
1483}
1484
1485static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001486 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001487{
Ursula Brauna046d572017-01-09 16:55:16 +01001488 struct sock *sk = sock->sk, *nsk;
1489 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001490 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001491 long timeo;
1492 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001493
1494 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001495 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001496 lock_sock(sk);
1497
1498 if (lsmc->sk.sk_state != SMC_LISTEN) {
1499 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001500 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001501 goto out;
1502 }
1503
Ursula Brauna046d572017-01-09 16:55:16 +01001504 /* Wait for an incoming connection */
1505 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1506 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1507 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1508 set_current_state(TASK_INTERRUPTIBLE);
1509 if (!timeo) {
1510 rc = -EAGAIN;
1511 break;
1512 }
1513 release_sock(sk);
1514 timeo = schedule_timeout(timeo);
1515 /* wakeup by sk_data_ready in smc_listen_work() */
1516 sched_annotate_sleep();
1517 lock_sock(sk);
1518 if (signal_pending(current)) {
1519 rc = sock_intr_errno(timeo);
1520 break;
1521 }
1522 }
1523 set_current_state(TASK_RUNNING);
1524 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001525
Ursula Brauna046d572017-01-09 16:55:16 +01001526 if (!rc)
1527 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001528 release_sock(sk);
1529 if (rc)
1530 goto out;
1531
1532 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1533 /* wait till data arrives on the socket */
1534 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1535 MSEC_PER_SEC);
1536 if (smc_sk(nsk)->use_fallback) {
1537 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1538
1539 lock_sock(clcsk);
1540 if (skb_queue_empty(&clcsk->sk_receive_queue))
1541 sk_wait_data(clcsk, &timeo, NULL);
1542 release_sock(clcsk);
1543 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1544 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001545 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001546 release_sock(nsk);
1547 }
1548 }
Ursula Braunac713872017-01-09 16:55:13 +01001549
1550out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001551 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001552 return rc;
1553}
1554
1555static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001556 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001557{
1558 struct smc_sock *smc;
1559
Ursula Braunb38d7322017-01-09 16:55:25 +01001560 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1561 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001562 return -ENOTCONN;
1563
1564 smc = smc_sk(sock->sk);
1565
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001566 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001567}
1568
1569static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1570{
1571 struct sock *sk = sock->sk;
1572 struct smc_sock *smc;
1573 int rc = -EPIPE;
1574
1575 smc = smc_sk(sk);
1576 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001577 if ((sk->sk_state != SMC_ACTIVE) &&
1578 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1579 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001580 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001581
1582 if (msg->msg_flags & MSG_FASTOPEN) {
Ursula Brauncd206362019-08-02 10:47:50 +02001583 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001584 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001585 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001586 } else {
1587 rc = -EINVAL;
1588 goto out;
1589 }
1590 }
1591
Ursula Braunac713872017-01-09 16:55:13 +01001592 if (smc->use_fallback)
1593 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1594 else
Ursula Braune6727f32017-01-09 16:55:23 +01001595 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001596out:
1597 release_sock(sk);
1598 return rc;
1599}
1600
1601static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1602 int flags)
1603{
1604 struct sock *sk = sock->sk;
1605 struct smc_sock *smc;
1606 int rc = -ENOTCONN;
1607
1608 smc = smc_sk(sk);
1609 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001610 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1611 /* socket was connected before, no more data to read */
1612 rc = 0;
1613 goto out;
1614 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001615 if ((sk->sk_state == SMC_INIT) ||
1616 (sk->sk_state == SMC_LISTEN) ||
1617 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001618 goto out;
1619
Ursula Braunb38d7322017-01-09 16:55:25 +01001620 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1621 rc = 0;
1622 goto out;
1623 }
1624
Stefan Raspl9014db22018-05-03 18:12:39 +02001625 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001626 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001627 } else {
1628 msg->msg_namelen = 0;
1629 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1630 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001631
Ursula Braunac713872017-01-09 16:55:13 +01001632out:
1633 release_sock(sk);
1634 return rc;
1635}
1636
Al Viroade994f2017-07-03 00:01:49 -04001637static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001638{
Ursula Braun8dce2782018-01-26 09:28:47 +01001639 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001640 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001641
Ursula Braun8dce2782018-01-26 09:28:47 +01001642 spin_lock(&isk->accept_q_lock);
1643 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001644 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001645 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001646
Ursula Braun8dce2782018-01-26 09:28:47 +01001647 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001648}
1649
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001650static __poll_t smc_poll(struct file *file, struct socket *sock,
1651 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01001652{
1653 struct sock *sk = sock->sk;
Ursula Braunac713872017-01-09 16:55:13 +01001654 struct smc_sock *smc;
Ursula Braun50717a32019-04-12 12:57:23 +02001655 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001656
Ursula Braun8dce2782018-01-26 09:28:47 +01001657 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001658 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001659
Ursula Braunac713872017-01-09 16:55:13 +01001660 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02001661 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01001662 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001663 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02001664 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braunac713872017-01-09 16:55:13 +01001665 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07001666 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02001667 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01001668 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001669 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001670 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1671 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001672 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001673 if (sk->sk_state == SMC_LISTEN) {
1674 /* woken up by sk_data_ready in smc_listen_work() */
Ursula Braun50717a32019-04-12 12:57:23 +02001675 mask |= smc_accept_poll(sk);
1676 } else if (smc->use_fallback) { /* as result of connect_work()*/
1677 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
1678 wait);
1679 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun8dce2782018-01-26 09:28:47 +01001680 } else {
Ursula Braun50717a32019-04-12 12:57:23 +02001681 if ((sk->sk_state != SMC_INIT &&
1682 atomic_read(&smc->conn.sndbuf_space)) ||
Ursula Braun8dce2782018-01-26 09:28:47 +01001683 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001684 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001685 } else {
1686 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1687 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1688 }
1689 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001690 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001691 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001692 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001693 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001694 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02001695 if (smc->conn.urg_state == SMC_URG_VALID)
1696 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01001697 }
Ursula Braunac713872017-01-09 16:55:13 +01001698 }
1699
1700 return mask;
1701}
1702
1703static int smc_shutdown(struct socket *sock, int how)
1704{
1705 struct sock *sk = sock->sk;
1706 struct smc_sock *smc;
1707 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001708 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001709
1710 smc = smc_sk(sk);
1711
1712 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001713 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001714
1715 lock_sock(sk);
1716
1717 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02001718 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01001719 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1720 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1721 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1722 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1723 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001724 goto out;
1725 if (smc->use_fallback) {
1726 rc = kernel_sock_shutdown(smc->clcsock, how);
1727 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1728 if (sk->sk_shutdown == SHUTDOWN_MASK)
1729 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001730 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001731 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001732 switch (how) {
1733 case SHUT_RDWR: /* shutdown in both directions */
1734 rc = smc_close_active(smc);
1735 break;
1736 case SHUT_WR:
1737 rc = smc_close_shutdown_write(smc);
1738 break;
1739 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001740 rc = 0;
1741 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001742 break;
1743 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001744 if (smc->clcsock)
1745 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001746 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1747 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001748
1749out:
1750 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001751 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001752}
1753
1754static int smc_setsockopt(struct socket *sock, int level, int optname,
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02001755 sockptr_t optval, unsigned int optlen)
Ursula Braunac713872017-01-09 16:55:13 +01001756{
1757 struct sock *sk = sock->sk;
1758 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001759 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001760
1761 smc = smc_sk(sk);
1762
1763 /* generic setsockopts reaching us here always apply to the
1764 * CLC socket
1765 */
Christoph Hellwiga44d9e72020-07-17 08:23:31 +02001766 if (unlikely(!smc->clcsock->ops->setsockopt))
1767 rc = -EOPNOTSUPP;
1768 else
1769 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1770 optval, optlen);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001771 if (smc->clcsock->sk->sk_err) {
1772 sk->sk_err = smc->clcsock->sk->sk_err;
1773 sk->sk_error_report(sk);
1774 }
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001775
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001776 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00001777 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02001778 if (copy_from_sockptr(&val, optval, sizeof(int)))
Ursula Braunac0107e2018-07-18 15:22:50 +02001779 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001780
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001781 lock_sock(sk);
Ursula Braun86434742019-12-12 22:35:58 +01001782 if (rc || smc->use_fallback)
1783 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001784 switch (optname) {
1785 case TCP_ULP:
1786 case TCP_FASTOPEN:
1787 case TCP_FASTOPEN_CONNECT:
1788 case TCP_FASTOPEN_KEY:
1789 case TCP_FASTOPEN_NO_COOKIE:
1790 /* option not supported by SMC */
Ursula Braun8204df72019-11-15 12:39:30 +01001791 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001792 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001793 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001794 } else {
Ursula Braun86434742019-12-12 22:35:58 +01001795 rc = -EINVAL;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001796 }
1797 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001798 case TCP_NODELAY:
Ursula Braunf9cedf12019-08-02 10:16:38 +02001799 if (sk->sk_state != SMC_INIT &&
1800 sk->sk_state != SMC_LISTEN &&
1801 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01001802 if (val)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001803 mod_delayed_work(system_wq, &smc->conn.tx_work,
1804 0);
1805 }
1806 break;
1807 case TCP_CORK:
Ursula Braunf9cedf12019-08-02 10:16:38 +02001808 if (sk->sk_state != SMC_INIT &&
1809 sk->sk_state != SMC_LISTEN &&
1810 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01001811 if (!val)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001812 mod_delayed_work(system_wq, &smc->conn.tx_work,
1813 0);
1814 }
1815 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001816 case TCP_DEFER_ACCEPT:
1817 smc->sockopt_defer_accept = val;
1818 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001819 default:
1820 break;
1821 }
Ursula Braun86434742019-12-12 22:35:58 +01001822out:
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001823 release_sock(sk);
1824
1825 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001826}
1827
1828static int smc_getsockopt(struct socket *sock, int level, int optname,
1829 char __user *optval, int __user *optlen)
1830{
1831 struct smc_sock *smc;
1832
1833 smc = smc_sk(sock->sk);
1834 /* socket options apply to the CLC socket */
Christoph Hellwiga44d9e72020-07-17 08:23:31 +02001835 if (unlikely(!smc->clcsock->ops->getsockopt))
1836 return -EOPNOTSUPP;
Ursula Braunac713872017-01-09 16:55:13 +01001837 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1838 optval, optlen);
1839}
1840
1841static int smc_ioctl(struct socket *sock, unsigned int cmd,
1842 unsigned long arg)
1843{
Stefan Rasplde8474e2018-05-23 16:38:11 +02001844 union smc_host_cursor cons, urg;
1845 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01001846 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001847 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001848
1849 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001850 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02001851 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02001852 if (smc->use_fallback) {
1853 if (!smc->clcsock) {
1854 release_sock(&smc->sk);
1855 return -EBADF;
1856 }
1857 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1858 release_sock(&smc->sk);
1859 return answ;
1860 }
Ursula Braun9b67e262018-05-02 16:56:46 +02001861 switch (cmd) {
1862 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02001863 if (smc->sk.sk_state == SMC_LISTEN) {
1864 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001865 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001866 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001867 if (smc->sk.sk_state == SMC_INIT ||
1868 smc->sk.sk_state == SMC_CLOSED)
1869 answ = 0;
1870 else
1871 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02001872 break;
1873 case SIOCOUTQ:
1874 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02001875 if (smc->sk.sk_state == SMC_LISTEN) {
1876 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001877 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001878 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001879 if (smc->sk.sk_state == SMC_INIT ||
1880 smc->sk.sk_state == SMC_CLOSED)
1881 answ = 0;
1882 else
1883 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02001884 atomic_read(&smc->conn.sndbuf_space);
1885 break;
1886 case SIOCOUTQNSD:
1887 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02001888 if (smc->sk.sk_state == SMC_LISTEN) {
1889 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001890 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001891 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001892 if (smc->sk.sk_state == SMC_INIT ||
1893 smc->sk.sk_state == SMC_CLOSED)
1894 answ = 0;
1895 else
1896 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02001897 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02001898 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02001899 if (smc->sk.sk_state == SMC_LISTEN) {
1900 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001901 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001902 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001903 if (smc->sk.sk_state == SMC_INIT ||
1904 smc->sk.sk_state == SMC_CLOSED) {
1905 answ = 0;
1906 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02001907 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1908 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001909 answ = smc_curs_diff(conn->rmb_desc->len,
1910 &cons, &urg) == 1;
1911 }
1912 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02001913 default:
Ursula Braun1992d992018-07-16 13:56:52 +02001914 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001915 return -ENOIOCTLCMD;
1916 }
Ursula Braun1992d992018-07-16 13:56:52 +02001917 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001918
1919 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001920}
1921
1922static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1923 int offset, size_t size, int flags)
1924{
1925 struct sock *sk = sock->sk;
1926 struct smc_sock *smc;
1927 int rc = -EPIPE;
1928
1929 smc = smc_sk(sk);
1930 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001931 if (sk->sk_state != SMC_ACTIVE) {
1932 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001933 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001934 }
1935 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001936 if (smc->use_fallback)
1937 rc = kernel_sendpage(smc->clcsock, page, offset,
1938 size, flags);
1939 else
1940 rc = sock_no_sendpage(sock, page, offset, size, flags);
1941
1942out:
Ursula Braunac713872017-01-09 16:55:13 +01001943 return rc;
1944}
1945
Stefan Raspl9014db22018-05-03 18:12:39 +02001946/* Map the affected portions of the rmbe into an spd, note the number of bytes
1947 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1948 * updates till whenever a respective page has been fully processed.
1949 * Note that subsequent recv() calls have to wait till all splice() processing
1950 * completed.
1951 */
Ursula Braunac713872017-01-09 16:55:13 +01001952static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1953 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001954 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001955{
1956 struct sock *sk = sock->sk;
1957 struct smc_sock *smc;
1958 int rc = -ENOTCONN;
1959
1960 smc = smc_sk(sk);
1961 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001962 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1963 /* socket was connected before, no more data to read */
1964 rc = 0;
1965 goto out;
1966 }
Stefan Raspl9014db22018-05-03 18:12:39 +02001967 if (sk->sk_state == SMC_INIT ||
1968 sk->sk_state == SMC_LISTEN ||
1969 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001970 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001971
1972 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1973 rc = 0;
1974 goto out;
1975 }
1976
Ursula Braunac713872017-01-09 16:55:13 +01001977 if (smc->use_fallback) {
1978 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1979 pipe, len, flags);
1980 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001981 if (*ppos) {
1982 rc = -ESPIPE;
1983 goto out;
1984 }
1985 if (flags & SPLICE_F_NONBLOCK)
1986 flags = MSG_DONTWAIT;
1987 else
1988 flags = 0;
1989 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001990 }
1991out:
1992 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001993
Ursula Braunac713872017-01-09 16:55:13 +01001994 return rc;
1995}
1996
1997/* must look like tcp */
1998static const struct proto_ops smc_sock_ops = {
1999 .family = PF_SMC,
2000 .owner = THIS_MODULE,
2001 .release = smc_release,
2002 .bind = smc_bind,
2003 .connect = smc_connect,
2004 .socketpair = sock_no_socketpair,
2005 .accept = smc_accept,
2006 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07002007 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01002008 .ioctl = smc_ioctl,
2009 .listen = smc_listen,
2010 .shutdown = smc_shutdown,
2011 .setsockopt = smc_setsockopt,
2012 .getsockopt = smc_getsockopt,
2013 .sendmsg = smc_sendmsg,
2014 .recvmsg = smc_recvmsg,
2015 .mmap = sock_no_mmap,
2016 .sendpage = smc_sendpage,
2017 .splice_read = smc_splice_read,
2018};
2019
2020static int smc_create(struct net *net, struct socket *sock, int protocol,
2021 int kern)
2022{
Karsten Graulaaa4d332018-03-16 15:06:41 +01002023 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01002024 struct smc_sock *smc;
2025 struct sock *sk;
2026 int rc;
2027
2028 rc = -ESOCKTNOSUPPORT;
2029 if (sock->type != SOCK_STREAM)
2030 goto out;
2031
2032 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002033 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01002034 goto out;
2035
2036 rc = -ENOBUFS;
2037 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002038 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01002039 if (!sk)
2040 goto out;
2041
2042 /* create internal TCP socket for CLC handshake and fallback */
2043 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01002044 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02002045 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002046 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
2047 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01002048 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01002049 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01002050 goto out;
2051 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01002052 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
2053 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01002054
2055out:
2056 return rc;
2057}
2058
2059static const struct net_proto_family smc_sock_family_ops = {
2060 .family = PF_SMC,
2061 .owner = THIS_MODULE,
2062 .create = smc_create,
2063};
2064
Hans Wippel64e28b52019-02-21 13:01:02 +01002065unsigned int smc_net_id;
2066
2067static __net_init int smc_net_init(struct net *net)
2068{
2069 return smc_pnet_net_init(net);
2070}
2071
2072static void __net_exit smc_net_exit(struct net *net)
2073{
2074 smc_pnet_net_exit(net);
2075}
2076
2077static struct pernet_operations smc_net_ops = {
2078 .init = smc_net_init,
2079 .exit = smc_net_exit,
2080 .id = &smc_net_id,
2081 .size = sizeof(struct smc_net),
2082};
2083
Ursula Braunac713872017-01-09 16:55:13 +01002084static int __init smc_init(void)
2085{
2086 int rc;
2087
Hans Wippel64e28b52019-02-21 13:01:02 +01002088 rc = register_pernet_subsys(&smc_net_ops);
2089 if (rc)
2090 return rc;
2091
Thomas Richter6812baa2017-01-09 16:55:15 +01002092 rc = smc_pnet_init();
2093 if (rc)
YueHaibing8c33bf12019-06-26 17:47:50 +02002094 goto out_pernet_subsys;
Thomas Richter6812baa2017-01-09 16:55:15 +01002095
Ursula Braun6dabd402019-11-16 17:47:29 +01002096 rc = smc_core_init();
2097 if (rc) {
2098 pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
2099 goto out_pnet;
2100 }
2101
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002102 rc = smc_llc_init();
2103 if (rc) {
2104 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002105 goto out_core;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002106 }
2107
Ursula Braun5f083182017-01-09 16:55:22 +01002108 rc = smc_cdc_init();
2109 if (rc) {
2110 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002111 goto out_core;
Ursula Braun5f083182017-01-09 16:55:22 +01002112 }
2113
Ursula Braunac713872017-01-09 16:55:13 +01002114 rc = proto_register(&smc_proto, 1);
2115 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01002116 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002117 goto out_core;
Ursula Braunac713872017-01-09 16:55:13 +01002118 }
2119
Karsten Graulaaa4d332018-03-16 15:06:41 +01002120 rc = proto_register(&smc_proto6, 1);
2121 if (rc) {
2122 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2123 goto out_proto;
2124 }
2125
Ursula Braunac713872017-01-09 16:55:13 +01002126 rc = sock_register(&smc_sock_family_ops);
2127 if (rc) {
2128 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002129 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01002130 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01002131 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002132 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01002133
Ursula Brauna4cf0442017-01-09 16:55:14 +01002134 rc = smc_ib_register_client();
2135 if (rc) {
2136 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2137 goto out_sock;
2138 }
2139
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002140 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002141 return 0;
2142
Ursula Brauna4cf0442017-01-09 16:55:14 +01002143out_sock:
2144 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002145out_proto6:
2146 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002147out_proto:
2148 proto_unregister(&smc_proto);
Ursula Braun6dabd402019-11-16 17:47:29 +01002149out_core:
2150 smc_core_exit();
Thomas Richter6812baa2017-01-09 16:55:15 +01002151out_pnet:
2152 smc_pnet_exit();
YueHaibing8c33bf12019-06-26 17:47:50 +02002153out_pernet_subsys:
2154 unregister_pernet_subsys(&smc_net_ops);
2155
Ursula Braunac713872017-01-09 16:55:13 +01002156 return rc;
2157}
2158
2159static void __exit smc_exit(void)
2160{
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002161 static_branch_disable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002162 sock_unregister(PF_SMC);
Ursula Braun6dabd402019-11-16 17:47:29 +01002163 smc_core_exit();
2164 smc_ib_unregister_client();
Karsten Graulaaa4d332018-03-16 15:06:41 +01002165 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002166 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002167 smc_pnet_exit();
Hans Wippel64e28b52019-02-21 13:01:02 +01002168 unregister_pernet_subsys(&smc_net_ops);
Ursula Braun4ead9c92019-11-16 17:47:31 +01002169 rcu_barrier();
Ursula Braunac713872017-01-09 16:55:13 +01002170}
2171
2172module_init(smc_init);
2173module_exit(smc_exit);
2174
2175MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2176MODULE_DESCRIPTION("smc socket address family");
2177MODULE_LICENSE("GPL");
2178MODULE_ALIAS_NETPROTO(PF_SMC);