blob: 4e4421c95ca17007b6b1a9084f80468b32b38795 [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{
129 smc->clcsock->file->private_data = smc->sk.sk_socket;
130 smc->clcsock->file = NULL;
131}
132
Ursula Braun39f41f32019-06-27 15:04:52 +0200133static int __smc_release(struct smc_sock *smc)
Ursula Braunac713872017-01-09 16:55:13 +0100134{
Ursula Braun39f41f32019-06-27 15:04:52 +0200135 struct sock *sk = &smc->sk;
Ursula Braunb38d7322017-01-09 16:55:25 +0100136 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +0100137
Ursula Braun51f1de72018-01-26 09:28:48 +0100138 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100139 rc = smc_close_active(smc);
140 sock_set_flag(sk, SOCK_DEAD);
141 sk->sk_shutdown |= SHUTDOWN_MASK;
Ursula Braunb03faa12019-02-07 15:56:15 +0100142 } else {
143 if (sk->sk_state != SMC_LISTEN && sk->sk_state != SMC_INIT)
144 sock_put(sk); /* passive closing */
145 if (sk->sk_state == SMC_LISTEN) {
146 /* wake up clcsock accept */
147 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
148 }
149 sk->sk_state = SMC_CLOSED;
150 sk->sk_state_change(sk);
Ursula Braunf536dff2019-10-23 15:44:05 +0200151 smc_restore_fallback_changes(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +0100152 }
Cong Wang26d92e92019-01-05 23:45:26 -0800153
154 sk->sk_prot->unhash(sk);
155
Ursula Braunb03faa12019-02-07 15:56:15 +0100156 if (sk->sk_state == SMC_CLOSED) {
157 if (smc->clcsock) {
Karsten Graulfd577702019-04-11 11:17:30 +0200158 release_sock(sk);
159 smc_clcsock_release(smc);
160 lock_sock(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800161 }
Ursula Braunb03faa12019-02-07 15:56:15 +0100162 if (!smc->use_fallback)
163 smc_conn_free(&smc->conn);
Ursula Braun51f1de72018-01-26 09:28:48 +0100164 }
Ursula Braunac713872017-01-09 16:55:13 +0100165
Ursula Braun39f41f32019-06-27 15:04:52 +0200166 return rc;
167}
168
169static int smc_release(struct socket *sock)
170{
171 struct sock *sk = sock->sk;
172 struct smc_sock *smc;
173 int rc = 0;
174
175 if (!sk)
176 goto out;
177
Ursula Braun81cf4f42019-10-21 16:13:15 +0200178 sock_hold(sk); /* sock_put below */
Ursula Braun39f41f32019-06-27 15:04:52 +0200179 smc = smc_sk(sk);
180
181 /* cleanup for a dangling non-blocking connect */
182 if (smc->connect_nonblock && sk->sk_state == SMC_INIT)
183 tcp_abort(smc->clcsock->sk, ECONNABORTED);
184 flush_work(&smc->connect_work);
185
186 if (sk->sk_state == SMC_LISTEN)
187 /* smc_close_non_accepted() is called and acquires
188 * sock lock for child sockets again
189 */
190 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
191 else
192 lock_sock(sk);
193
194 rc = __smc_release(smc);
195
Ursula Braunac713872017-01-09 16:55:13 +0100196 /* detach socket */
197 sock_orphan(sk);
198 sock->sk = NULL;
199 release_sock(sk);
200
Ursula Braun81cf4f42019-10-21 16:13:15 +0200201 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +0100202 sock_put(sk); /* final sock_put */
Ursula Braunac713872017-01-09 16:55:13 +0100203out:
Ursula Braunb38d7322017-01-09 16:55:25 +0100204 return rc;
Ursula Braunac713872017-01-09 16:55:13 +0100205}
206
207static void smc_destruct(struct sock *sk)
208{
209 if (sk->sk_state != SMC_CLOSED)
210 return;
211 if (!sock_flag(sk, SOCK_DEAD))
212 return;
213
214 sk_refcnt_debug_dec(sk);
215}
216
Karsten Graulaaa4d332018-03-16 15:06:41 +0100217static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
218 int protocol)
Ursula Braunac713872017-01-09 16:55:13 +0100219{
220 struct smc_sock *smc;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100221 struct proto *prot;
Ursula Braunac713872017-01-09 16:55:13 +0100222 struct sock *sk;
223
Karsten Graulaaa4d332018-03-16 15:06:41 +0100224 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
225 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
Ursula Braunac713872017-01-09 16:55:13 +0100226 if (!sk)
227 return NULL;
228
229 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
230 sk->sk_state = SMC_INIT;
231 sk->sk_destruct = smc_destruct;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100232 sk->sk_protocol = protocol;
Ursula Braunac713872017-01-09 16:55:13 +0100233 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100234 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200235 INIT_WORK(&smc->connect_work, smc_connect_work);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700236 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
Ursula Brauna046d572017-01-09 16:55:16 +0100237 INIT_LIST_HEAD(&smc->accept_q);
238 spin_lock_init(&smc->accept_q_lock);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700239 spin_lock_init(&smc->conn.send_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100240 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100241 sk_refcnt_debug_inc(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800242 mutex_init(&smc->clcsock_release_lock);
Ursula Braunac713872017-01-09 16:55:13 +0100243
244 return sk;
245}
246
247static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
248 int addr_len)
249{
250 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
251 struct sock *sk = sock->sk;
252 struct smc_sock *smc;
253 int rc;
254
255 smc = smc_sk(sk);
256
257 /* replicate tests from inet_bind(), to be safe wrt. future changes */
258 rc = -EINVAL;
259 if (addr_len < sizeof(struct sockaddr_in))
260 goto out;
261
262 rc = -EAFNOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100263 if (addr->sin_family != AF_INET &&
264 addr->sin_family != AF_INET6 &&
265 addr->sin_family != AF_UNSPEC)
266 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100267 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
Karsten Graulaaa4d332018-03-16 15:06:41 +0100268 if (addr->sin_family == AF_UNSPEC &&
269 addr->sin_addr.s_addr != htonl(INADDR_ANY))
Ursula Braunac713872017-01-09 16:55:13 +0100270 goto out;
271
272 lock_sock(sk);
273
274 /* Check if socket is already active */
275 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +0200276 if (sk->sk_state != SMC_INIT || smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +0100277 goto out_rel;
278
279 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
280 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
281
282out_rel:
283 release_sock(sk);
284out:
285 return rc;
286}
287
288static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
289 unsigned long mask)
290{
291 /* options we don't get control via setsockopt for */
292 nsk->sk_type = osk->sk_type;
293 nsk->sk_sndbuf = osk->sk_sndbuf;
294 nsk->sk_rcvbuf = osk->sk_rcvbuf;
295 nsk->sk_sndtimeo = osk->sk_sndtimeo;
296 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
297 nsk->sk_mark = osk->sk_mark;
298 nsk->sk_priority = osk->sk_priority;
299 nsk->sk_rcvlowat = osk->sk_rcvlowat;
300 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
301 nsk->sk_err = osk->sk_err;
302
303 nsk->sk_flags &= ~mask;
304 nsk->sk_flags |= osk->sk_flags & mask;
305}
306
307#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
308 (1UL << SOCK_KEEPOPEN) | \
309 (1UL << SOCK_LINGER) | \
310 (1UL << SOCK_BROADCAST) | \
311 (1UL << SOCK_TIMESTAMP) | \
312 (1UL << SOCK_DBG) | \
313 (1UL << SOCK_RCVTSTAMP) | \
314 (1UL << SOCK_RCVTSTAMPNS) | \
315 (1UL << SOCK_LOCALROUTE) | \
316 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
317 (1UL << SOCK_RXQ_OVFL) | \
318 (1UL << SOCK_WIFI_STATUS) | \
319 (1UL << SOCK_NOFCS) | \
Deepa Dinamani97184752019-02-02 07:34:51 -0800320 (1UL << SOCK_FILTER_LOCKED) | \
321 (1UL << SOCK_TSTAMP_NEW))
Ursula Braunac713872017-01-09 16:55:13 +0100322/* copy only relevant settings and flags of SOL_SOCKET level from smc to
323 * clc socket (since smc is not called for these options from net/core)
324 */
325static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
326{
327 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
328}
329
330#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
331 (1UL << SOCK_KEEPOPEN) | \
332 (1UL << SOCK_LINGER) | \
333 (1UL << SOCK_DBG))
334/* copy only settings and flags relevant for smc from clc to smc socket */
335static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
336{
337 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
338}
339
Karsten Graulb9247542020-04-29 17:10:41 +0200340/* register the new rmb on all links */
Karsten Graul7562a132020-05-01 12:48:01 +0200341static int smcr_lgr_reg_rmbs(struct smc_link *link,
Karsten Graulb9247542020-04-29 17:10:41 +0200342 struct smc_buf_desc *rmb_desc)
343{
Karsten Graul7562a132020-05-01 12:48:01 +0200344 struct smc_link_group *lgr = link->lgr;
345 int i, rc = 0;
Karsten Graulb9247542020-04-29 17:10:41 +0200346
Karsten Grauld5500662020-05-01 12:48:05 +0200347 rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY);
348 if (rc)
349 return rc;
350 /* protect against parallel smc_llc_cli_rkey_exchange() and
351 * parallel smcr_link_reg_rmb()
352 */
353 mutex_lock(&lgr->llc_conf_mutex);
Karsten Graulb9247542020-04-29 17:10:41 +0200354 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
355 if (lgr->lnk[i].state != SMC_LNK_ACTIVE)
356 continue;
Karsten Graul7562a132020-05-01 12:48:01 +0200357 rc = smcr_link_reg_rmb(&lgr->lnk[i], rmb_desc);
Karsten Graulb9247542020-04-29 17:10:41 +0200358 if (rc)
Karsten Graul7562a132020-05-01 12:48:01 +0200359 goto out;
Karsten Graulb9247542020-04-29 17:10:41 +0200360 }
Karsten Graul7562a132020-05-01 12:48:01 +0200361
362 /* exchange confirm_rkey msg with peer */
363 rc = smc_llc_do_confirm_rkey(link, rmb_desc);
364 if (rc) {
365 rc = -EFAULT;
366 goto out;
367 }
368 rmb_desc->is_conf_rkey = true;
369out:
Karsten Grauld5500662020-05-01 12:48:05 +0200370 mutex_unlock(&lgr->llc_conf_mutex);
371 smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
Karsten Graul7562a132020-05-01 12:48:01 +0200372 return rc;
Karsten Graulb9247542020-04-29 17:10:41 +0200373}
374
375static int smcr_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100376{
Karsten Graul387707f2020-04-29 17:10:40 +0200377 struct smc_link *link = smc->conn.lnk;
Karsten Graul0fb0b022020-04-30 15:55:43 +0200378 struct smc_llc_qentry *qentry;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100379 int rc;
380
Karsten Graul0fb0b022020-04-30 15:55:43 +0200381 link->lgr->type = SMC_LGR_SINGLE;
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);
417
418 /* optional 2nd link, receive ADD LINK request from server */
419 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
420 SMC_LLC_ADD_LINK);
421 if (!qentry) {
Karsten Graul52bedf32018-03-01 13:51:32 +0100422 struct smc_clc_msg_decline dclc;
423
424 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100425 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200426 if (rc == -EAGAIN)
427 rc = 0; /* no DECLINE received, go with one link */
428 return rc;
Karsten Graul52bedf32018-03-01 13:51:32 +0100429 }
Karsten Graul0fb0b022020-04-30 15:55:43 +0200430 smc_llc_flow_qentry_clr(&link->lgr->llc_flow_lcl);
Karsten Graulb1570a82020-05-03 14:38:42 +0200431 smc_llc_cli_add_link(link, qentry);
Karsten Graul75d320d2018-03-01 13:51:31 +0100432 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100433}
434
Hans Wippel41349842018-06-28 19:05:11 +0200435static void smcr_conn_save_peer_info(struct smc_sock *smc,
436 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100437{
Hans Wippel95d8d2632018-05-18 09:34:13 +0200438 int bufsize = smc_uncompress_bufsize(clc->rmbe_size);
439
Hans Wippel92a138e2018-05-18 09:34:12 +0200440 smc->conn.peer_rmbe_idx = clc->rmbe_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100441 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200442 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100443 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200444 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100445}
446
Hans Wippel41349842018-06-28 19:05:11 +0200447static void smcd_conn_save_peer_info(struct smc_sock *smc,
448 struct smc_clc_msg_accept_confirm *clc)
449{
450 int bufsize = smc_uncompress_bufsize(clc->dmbe_size);
451
452 smc->conn.peer_rmbe_idx = clc->dmbe_idx;
453 smc->conn.peer_token = clc->token;
454 /* msg header takes up space in the buffer */
455 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
456 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
457 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
458}
459
460static void smc_conn_save_peer_info(struct smc_sock *smc,
461 struct smc_clc_msg_accept_confirm *clc)
462{
463 if (smc->conn.lgr->is_smcd)
464 smcd_conn_save_peer_info(smc, clc);
465 else
466 smcr_conn_save_peer_info(smc, clc);
467}
468
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100469static void smc_link_save_peer_info(struct smc_link *link,
470 struct smc_clc_msg_accept_confirm *clc)
471{
472 link->peer_qpn = ntoh24(clc->qpn);
473 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
474 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
475 link->peer_psn = ntoh24(clc->psn);
476 link->peer_mtu = clc->qp_mtu;
477}
478
Ursula Braun07603b22019-04-11 11:17:32 +0200479static void smc_switch_to_fallback(struct smc_sock *smc)
480{
481 smc->use_fallback = true;
482 if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
483 smc->clcsock->file = smc->sk.sk_socket->file;
484 smc->clcsock->file->private_data = smc->clcsock;
Ursula Braun67f562e2020-02-14 08:58:59 +0100485 smc->clcsock->wq.fasync_list =
486 smc->sk.sk_socket->wq.fasync_list;
Ursula Braun07603b22019-04-11 11:17:32 +0200487 }
488}
489
Hans Wippel3b2dec22018-05-18 09:34:18 +0200490/* fall back during connect */
Karsten Graul603cc142018-07-25 16:35:32 +0200491static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200492{
Ursula Braun07603b22019-04-11 11:17:32 +0200493 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +0200494 smc->fallback_rsn = reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200495 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200496 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200497 if (smc->sk.sk_state == SMC_INIT)
498 smc->sk.sk_state = SMC_ACTIVE;
499 return 0;
500}
501
502/* decline and fall back during connect */
503static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
504{
505 int rc;
506
Ursula Braune1bbdd52018-07-05 16:15:30 +0200507 if (reason_code < 0) { /* error, fallback is not possible */
508 if (smc->sk.sk_state == SMC_INIT)
509 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200510 return reason_code;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200511 }
Karsten Graul603cc142018-07-25 16:35:32 +0200512 if (reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200513 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200514 if (rc < 0) {
515 if (smc->sk.sk_state == SMC_INIT)
516 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200517 return rc;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200518 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200519 }
Karsten Graul603cc142018-07-25 16:35:32 +0200520 return smc_connect_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200521}
522
523/* abort connecting */
524static int smc_connect_abort(struct smc_sock *smc, int reason_code,
525 int local_contact)
526{
Ursula Braun51e3dfa2020-02-25 16:34:36 +0100527 bool is_smcd = smc->conn.lgr->is_smcd;
528
Hans Wippel3b2dec22018-05-18 09:34:18 +0200529 if (local_contact == SMC_FIRST_CONTACT)
Ursula Braun51e3dfa2020-02-25 16:34:36 +0100530 smc_lgr_cleanup_early(&smc->conn);
531 else
532 smc_conn_free(&smc->conn);
533 if (is_smcd)
Hans Wippel72a36a82019-02-07 15:56:18 +0100534 /* there is only one lgr role for SMC-D; use server lock */
535 mutex_unlock(&smc_server_lgr_pending);
536 else
537 mutex_unlock(&smc_client_lgr_pending);
538
Ursula Braun50717a32019-04-12 12:57:23 +0200539 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200540 return reason_code;
541}
542
543/* check if there is a rdma device available for this connection. */
544/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200545static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200546{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200547 /* PNET table look up: search active ib_device and port
548 * within same PNETID that also contains the ethernet device
549 * used for the internal TCP socket
550 */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200551 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +0200552 if (!ini->ib_dev)
553 return SMC_CLC_DECL_NOSMCRDEV;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200554 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200555}
556
Hans Wippel41349842018-06-28 19:05:11 +0200557/* check if there is an ISM device available for this connection. */
558/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200559static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200560{
561 /* Find ISM device with same PNETID as connecting interface */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200562 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
563 if (!ini->ism_dev)
Karsten Graul9aa68d22019-04-12 12:57:29 +0200564 return SMC_CLC_DECL_NOSMCDDEV;
Hans Wippel41349842018-06-28 19:05:11 +0200565 return 0;
566}
567
568/* Check for VLAN ID and register it on ISM device just for CLC handshake */
569static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200570 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200571{
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200572 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev, ini->vlan_id))
Karsten Graul7a627252019-04-12 12:57:30 +0200573 return SMC_CLC_DECL_ISMVLANERR;
Hans Wippel41349842018-06-28 19:05:11 +0200574 return 0;
575}
576
577/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
578 * used, the VLAN ID will be registered again during the connection setup.
579 */
580static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200581 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200582{
583 if (!is_smcd)
584 return 0;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200585 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev, ini->vlan_id))
Hans Wippel41349842018-06-28 19:05:11 +0200586 return SMC_CLC_DECL_CNFERR;
587 return 0;
588}
589
Hans Wippel3b2dec22018-05-18 09:34:18 +0200590/* CLC handshake during connect */
Hans Wippelc758dfd2018-06-28 19:05:09 +0200591static int smc_connect_clc(struct smc_sock *smc, int smc_type,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200592 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200593 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200594{
595 int rc = 0;
596
597 /* do inband token exchange */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200598 rc = smc_clc_send_proposal(smc, smc_type, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200599 if (rc)
600 return rc;
601 /* receive SMC Accept CLC message */
Ursula Braun2b59f582018-11-22 10:26:39 +0100602 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT,
603 CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200604}
605
Ursula Brauna046d572017-01-09 16:55:16 +0100606/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200607static int smc_connect_rdma(struct smc_sock *smc,
608 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200609 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200610{
Karsten Graul0fb0b022020-04-30 15:55:43 +0200611 int i, reason_code = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200612 struct smc_link *link;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200613
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200614 ini->is_smcd = false;
615 ini->ib_lcl = &aclc->lcl;
616 ini->ib_clcqpn = ntoh24(aclc->qpn);
617 ini->srv_first_contact = aclc->hdr.flag;
618
Hans Wippel72a36a82019-02-07 15:56:18 +0100619 mutex_lock(&smc_client_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200620 reason_code = smc_conn_create(smc, ini);
621 if (reason_code) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100622 mutex_unlock(&smc_client_lgr_pending);
623 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200624 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200625
626 smc_conn_save_peer_info(smc, aclc);
627
Karsten Graul0fb0b022020-04-30 15:55:43 +0200628 if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
629 link = smc->conn.lnk;
630 } else {
631 /* set link that was assigned by server */
632 link = NULL;
633 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
634 struct smc_link *l = &smc->conn.lgr->lnk[i];
635
636 if (l->peer_qpn == ntoh24(aclc->qpn)) {
637 link = l;
638 break;
639 }
640 }
641 if (!link)
642 return smc_connect_abort(smc, SMC_CLC_DECL_NOSRVLINK,
643 ini->cln_first_contact);
644 smc->conn.lnk = link;
645 }
646
Hans Wippel3b2dec22018-05-18 09:34:18 +0200647 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200648 if (smc_buf_create(smc, false))
Karsten Graul7a627252019-04-12 12:57:30 +0200649 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
650 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200651
Karsten Graul7a627252019-04-12 12:57:30 +0200652 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200653 smc_link_save_peer_info(link, aclc);
654
Karsten Graule07d31d2020-04-29 17:10:42 +0200655 if (smc_rmb_rtoken_handling(&smc->conn, link, aclc))
Karsten Graul603cc142018-07-25 16:35:32 +0200656 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
Karsten Graul7a627252019-04-12 12:57:30 +0200657 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200658
659 smc_close_init(smc);
660 smc_rx_init(smc);
661
Karsten Graul7a627252019-04-12 12:57:30 +0200662 if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200663 if (smc_ib_ready_link(link))
Karsten Graul603cc142018-07-25 16:35:32 +0200664 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
Karsten Graul7a627252019-04-12 12:57:30 +0200665 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200666 } else {
Karsten Graul7562a132020-05-01 12:48:01 +0200667 if (smcr_lgr_reg_rmbs(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +0200668 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
Karsten Graul7a627252019-04-12 12:57:30 +0200669 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200670 }
671 smc_rmb_sync_sg_for_device(&smc->conn);
672
673 reason_code = smc_clc_send_confirm(smc);
674 if (reason_code)
Karsten Graul7a627252019-04-12 12:57:30 +0200675 return smc_connect_abort(smc, reason_code,
676 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200677
678 smc_tx_init(smc);
679
Karsten Graul7a627252019-04-12 12:57:30 +0200680 if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200681 /* QP confirmation over RoCE fabric */
Karsten Graul0fb0b022020-04-30 15:55:43 +0200682 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
Karsten Graulb9247542020-04-29 17:10:41 +0200683 reason_code = smcr_clnt_conf_first_link(smc);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200684 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200685 if (reason_code)
686 return smc_connect_abort(smc, reason_code,
Karsten Graul7a627252019-04-12 12:57:30 +0200687 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200688 }
Hans Wippel72a36a82019-02-07 15:56:18 +0100689 mutex_unlock(&smc_client_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200690
691 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200692 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200693 if (smc->sk.sk_state == SMC_INIT)
694 smc->sk.sk_state = SMC_ACTIVE;
695
696 return 0;
697}
698
Hans Wippel41349842018-06-28 19:05:11 +0200699/* setup for ISM connection of client */
700static int smc_connect_ism(struct smc_sock *smc,
701 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200702 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200703{
Hans Wippel41349842018-06-28 19:05:11 +0200704 int rc = 0;
705
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200706 ini->is_smcd = true;
707 ini->ism_gid = aclc->gid;
708 ini->srv_first_contact = aclc->hdr.flag;
709
Hans Wippel72a36a82019-02-07 15:56:18 +0100710 /* there is only one lgr role for SMC-D; use server lock */
711 mutex_lock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200712 rc = smc_conn_create(smc, ini);
713 if (rc) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100714 mutex_unlock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200715 return rc;
Hans Wippel72a36a82019-02-07 15:56:18 +0100716 }
Hans Wippel41349842018-06-28 19:05:11 +0200717
718 /* Create send and receive buffers */
719 if (smc_buf_create(smc, true))
Karsten Graul7a627252019-04-12 12:57:30 +0200720 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
721 ini->cln_first_contact);
Hans Wippel41349842018-06-28 19:05:11 +0200722
723 smc_conn_save_peer_info(smc, aclc);
724 smc_close_init(smc);
725 smc_rx_init(smc);
726 smc_tx_init(smc);
727
728 rc = smc_clc_send_confirm(smc);
729 if (rc)
Karsten Graul7a627252019-04-12 12:57:30 +0200730 return smc_connect_abort(smc, rc, ini->cln_first_contact);
Hans Wippel72a36a82019-02-07 15:56:18 +0100731 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel41349842018-06-28 19:05:11 +0200732
733 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200734 smc->connect_nonblock = 0;
Hans Wippel41349842018-06-28 19:05:11 +0200735 if (smc->sk.sk_state == SMC_INIT)
736 smc->sk.sk_state = SMC_ACTIVE;
737
738 return 0;
739}
740
Hans Wippel3b2dec22018-05-18 09:34:18 +0200741/* perform steps before actually connecting */
742static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100743{
Hans Wippel41349842018-06-28 19:05:11 +0200744 bool ism_supported = false, rdma_supported = false;
Ursula Brauna046d572017-01-09 16:55:16 +0100745 struct smc_clc_msg_accept_confirm aclc;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200746 struct smc_init_info ini = {0};
Hans Wippel41349842018-06-28 19:05:11 +0200747 int smc_type;
Ursula Brauna046d572017-01-09 16:55:16 +0100748 int rc = 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100749
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200750 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +0200751 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200752
Hans Wippel3b2dec22018-05-18 09:34:18 +0200753 /* if peer has not signalled SMC-capability, fall back */
754 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +0200755 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200756
Ursula Brauna046d572017-01-09 16:55:16 +0100757 /* IPSec connections opt out of SMC-R optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200758 if (using_ipsec(smc))
759 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
Ursula Brauna046d572017-01-09 16:55:16 +0100760
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200761 /* get vlan id from IP device */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200762 if (smc_vlan_by_tcpsk(smc->clcsock, &ini))
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200763 return smc_connect_decline_fallback(smc,
764 SMC_CLC_DECL_GETVLANERR);
Hans Wippel41349842018-06-28 19:05:11 +0200765
766 /* check if there is an ism device available */
Karsten Graul228bae02019-04-12 12:57:28 +0200767 if (!smc_find_ism_device(smc, &ini) &&
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200768 !smc_connect_ism_vlan_setup(smc, &ini)) {
Hans Wippel41349842018-06-28 19:05:11 +0200769 /* ISM is supported for this connection */
770 ism_supported = true;
771 smc_type = SMC_TYPE_D;
772 }
773
774 /* check if there is a rdma device available */
Karsten Graul228bae02019-04-12 12:57:28 +0200775 if (!smc_find_rdma_device(smc, &ini)) {
Hans Wippel41349842018-06-28 19:05:11 +0200776 /* RDMA is supported for this connection */
777 rdma_supported = true;
778 if (ism_supported)
779 smc_type = SMC_TYPE_B; /* both */
780 else
781 smc_type = SMC_TYPE_R; /* only RDMA */
782 }
783
784 /* if neither ISM nor RDMA are supported, fallback */
785 if (!rdma_supported && !ism_supported)
Karsten Graul603cc142018-07-25 16:35:32 +0200786 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV);
Ursula Brauna046d572017-01-09 16:55:16 +0100787
Hans Wippel3b2dec22018-05-18 09:34:18 +0200788 /* perform CLC handshake */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200789 rc = smc_connect_clc(smc, smc_type, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200790 if (rc) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200791 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200792 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200793 }
Ursula Brauna046d572017-01-09 16:55:16 +0100794
Hans Wippel41349842018-06-28 19:05:11 +0200795 /* depending on previous steps, connect using rdma or ism */
796 if (rdma_supported && aclc.hdr.path == SMC_TYPE_R)
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200797 rc = smc_connect_rdma(smc, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200798 else if (ism_supported && aclc.hdr.path == SMC_TYPE_D)
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200799 rc = smc_connect_ism(smc, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200800 else
Karsten Graul603cc142018-07-25 16:35:32 +0200801 rc = SMC_CLC_DECL_MODEUNSUPP;
Hans Wippel41349842018-06-28 19:05:11 +0200802 if (rc) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200803 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200804 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200805 }
Ursula Brauna046d572017-01-09 16:55:16 +0100806
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200807 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200808 return 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100809}
810
Ursula Braun24ac3a02018-06-27 17:59:50 +0200811static void smc_connect_work(struct work_struct *work)
812{
813 struct smc_sock *smc = container_of(work, struct smc_sock,
814 connect_work);
Ursula Braun50717a32019-04-12 12:57:23 +0200815 long timeo = smc->sk.sk_sndtimeo;
816 int rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200817
Ursula Braun50717a32019-04-12 12:57:23 +0200818 if (!timeo)
819 timeo = MAX_SCHEDULE_TIMEOUT;
820 lock_sock(smc->clcsock->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200821 if (smc->clcsock->sk->sk_err) {
822 smc->sk.sk_err = smc->clcsock->sk->sk_err;
Ursula Braun50717a32019-04-12 12:57:23 +0200823 } else if ((1 << smc->clcsock->sk->sk_state) &
824 (TCPF_SYN_SENT | TCP_SYN_RECV)) {
825 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
826 if ((rc == -EPIPE) &&
827 ((1 << smc->clcsock->sk->sk_state) &
828 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
829 rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200830 }
Ursula Braun50717a32019-04-12 12:57:23 +0200831 release_sock(smc->clcsock->sk);
832 lock_sock(&smc->sk);
833 if (rc != 0 || smc->sk.sk_err) {
834 smc->sk.sk_state = SMC_CLOSED;
835 if (rc == -EPIPE || rc == -EAGAIN)
836 smc->sk.sk_err = EPIPE;
837 else if (signal_pending(current))
838 smc->sk.sk_err = -sock_intr_errno(timeo);
Ursula Braun6d6dd522019-11-12 16:03:41 +0100839 sock_put(&smc->sk); /* passive closing */
Ursula Braun24ac3a02018-06-27 17:59:50 +0200840 goto out;
841 }
842
843 rc = __smc_connect(smc);
844 if (rc < 0)
845 smc->sk.sk_err = -rc;
846
847out:
Ursula Braun07603b22019-04-11 11:17:32 +0200848 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
849 if (smc->sk.sk_err) {
850 smc->sk.sk_state_change(&smc->sk);
851 } else { /* allow polling before and after fallback decision */
852 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
853 smc->sk.sk_write_space(&smc->sk);
854 }
855 }
Ursula Braun24ac3a02018-06-27 17:59:50 +0200856 release_sock(&smc->sk);
857}
858
Ursula Braunac713872017-01-09 16:55:13 +0100859static int smc_connect(struct socket *sock, struct sockaddr *addr,
860 int alen, int flags)
861{
862 struct sock *sk = sock->sk;
863 struct smc_sock *smc;
864 int rc = -EINVAL;
865
866 smc = smc_sk(sk);
867
868 /* separate smc parameter checking to be safe */
869 if (alen < sizeof(addr->sa_family))
870 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100871 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100872 goto out_err;
873
874 lock_sock(sk);
875 switch (sk->sk_state) {
876 default:
877 goto out;
878 case SMC_ACTIVE:
879 rc = -EISCONN;
880 goto out;
881 case SMC_INIT:
882 rc = 0;
883 break;
884 }
885
886 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200887 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun50717a32019-04-12 12:57:23 +0200888 if (smc->connect_nonblock) {
889 rc = -EALREADY;
890 goto out;
891 }
892 rc = kernel_connect(smc->clcsock, addr, alen, flags);
893 if (rc && rc != -EINPROGRESS)
894 goto out;
Ursula Braun301428e2019-10-29 12:41:26 +0100895
896 sock_hold(&smc->sk); /* sock put in passive closing */
Ursula Braun86434742019-12-12 22:35:58 +0100897 if (smc->use_fallback)
898 goto out;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200899 if (flags & O_NONBLOCK) {
Ursula Braun50717a32019-04-12 12:57:23 +0200900 if (schedule_work(&smc->connect_work))
901 smc->connect_nonblock = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200902 rc = -EINPROGRESS;
903 } else {
Ursula Braun24ac3a02018-06-27 17:59:50 +0200904 rc = __smc_connect(smc);
905 if (rc < 0)
906 goto out;
907 else
908 rc = 0; /* success cases including fallback */
909 }
Ursula Braunac713872017-01-09 16:55:13 +0100910
911out:
912 release_sock(sk);
913out_err:
914 return rc;
915}
916
917static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
918{
Ursula Braun3163c502018-01-24 10:28:12 +0100919 struct socket *new_clcsock = NULL;
920 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100921 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -0800922 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +0100923
Ursula Braun3163c502018-01-24 10:28:12 +0100924 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100925 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100926 if (!new_sk) {
927 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100928 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100929 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100930 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100931 goto out;
932 }
933 *new_smc = smc_sk(new_sk);
934
Myungho Jung78abe3d2018-12-18 09:02:25 -0800935 mutex_lock(&lsmc->clcsock_release_lock);
936 if (lsmc->clcsock)
937 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
938 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +0100939 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100940 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100941 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100942 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +0200943 new_sk->sk_prot->unhash(new_sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100944 if (new_clcsock)
945 sock_release(new_clcsock);
946 new_sk->sk_state = SMC_CLOSED;
947 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun51f1de72018-01-26 09:28:48 +0100948 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100949 *new_smc = NULL;
950 goto out;
951 }
952
953 (*new_smc)->clcsock = new_clcsock;
954out:
955 return rc;
956}
957
Ursula Brauna046d572017-01-09 16:55:16 +0100958/* add a just created sock to the accept queue of the listen sock as
959 * candidate for a following socket accept call from user space
960 */
961static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
962{
963 struct smc_sock *par = smc_sk(parent);
964
Ursula Braun51f1de72018-01-26 09:28:48 +0100965 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100966 spin_lock(&par->accept_q_lock);
967 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
968 spin_unlock(&par->accept_q_lock);
969 sk_acceptq_added(parent);
970}
971
972/* remove a socket from the accept queue of its parental listening socket */
973static void smc_accept_unlink(struct sock *sk)
974{
975 struct smc_sock *par = smc_sk(sk)->listen_smc;
976
977 spin_lock(&par->accept_q_lock);
978 list_del_init(&smc_sk(sk)->accept_q);
979 spin_unlock(&par->accept_q_lock);
980 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100981 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100982}
983
984/* remove a sock from the accept queue to bind it to a new socket created
985 * for a socket accept call from user space
986 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100987struct sock *smc_accept_dequeue(struct sock *parent,
988 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100989{
990 struct smc_sock *isk, *n;
991 struct sock *new_sk;
992
993 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
994 new_sk = (struct sock *)isk;
995
996 smc_accept_unlink(new_sk);
997 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +0200998 new_sk->sk_prot->unhash(new_sk);
Ursula Braun127f4972018-01-26 09:28:49 +0100999 if (isk->clcsock) {
1000 sock_release(isk->clcsock);
1001 isk->clcsock = NULL;
1002 }
Ursula Braun51f1de72018-01-26 09:28:48 +01001003 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +01001004 continue;
1005 }
Ursula Braun07603b22019-04-11 11:17:32 +02001006 if (new_sock) {
Ursula Brauna046d572017-01-09 16:55:16 +01001007 sock_graft(new_sk, new_sock);
Ursula Braun07603b22019-04-11 11:17:32 +02001008 if (isk->use_fallback) {
1009 smc_sk(new_sk)->clcsock->file = new_sock->file;
1010 isk->clcsock->file->private_data = isk->clcsock;
1011 }
1012 }
Ursula Brauna046d572017-01-09 16:55:16 +01001013 return new_sk;
1014 }
1015 return NULL;
1016}
1017
1018/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +01001019void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +01001020{
1021 struct smc_sock *smc = smc_sk(sk);
1022
Ursula Braun81cf4f42019-10-21 16:13:15 +02001023 sock_hold(sk); /* sock_put below */
Ursula Braunb38d7322017-01-09 16:55:25 +01001024 lock_sock(sk);
1025 if (!sk->sk_lingertime)
1026 /* wait for peer closing */
1027 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun39f41f32019-06-27 15:04:52 +02001028 __smc_release(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +01001029 release_sock(sk);
Ursula Braun81cf4f42019-10-21 16:13:15 +02001030 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +01001031 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +01001032}
1033
Karsten Graulb9247542020-04-29 17:10:41 +02001034static int smcr_serv_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001035{
Karsten Graul387707f2020-04-29 17:10:40 +02001036 struct smc_link *link = smc->conn.lnk;
Karsten Graul4667bb42020-04-30 15:55:42 +02001037 struct smc_llc_qentry *qentry;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001038 int rc;
1039
Karsten Graul4667bb42020-04-30 15:55:42 +02001040 link->lgr->type = SMC_LGR_SINGLE;
1041
Karsten Graul7562a132020-05-01 12:48:01 +02001042 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +02001043 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +02001044
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001045 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +02001046 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001047 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001048 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001049
1050 /* receive CONFIRM LINK response from client over the RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001051 qentry = smc_llc_wait(link->lgr, link, SMC_LLC_WAIT_TIME,
1052 SMC_LLC_CONFIRM_LINK);
1053 if (!qentry) {
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001054 struct smc_clc_msg_decline dclc;
1055
1056 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001057 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001058 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001059 }
Karsten Graul649758f2020-05-04 14:18:48 +02001060 smc_llc_save_peer_uid(qentry);
Karsten Graul4667bb42020-04-30 15:55:42 +02001061 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_RESP);
1062 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
1063 if (rc)
Karsten Graul75d320d2018-03-01 13:51:31 +01001064 return SMC_CLC_DECL_RMBE_EC;
1065
Karsten Graul4667bb42020-04-30 15:55:42 +02001066 /* confirm_rkey is implicit on 1st contact */
1067 smc->conn.rmb_desc->is_conf_rkey = true;
Karsten Graul52bedf32018-03-01 13:51:32 +01001068
Karsten Graul00a049c2020-04-29 17:10:49 +02001069 smc_llc_link_active(link);
Karsten Graul52bedf32018-03-01 13:51:32 +01001070
Karsten Graul4667bb42020-04-30 15:55:42 +02001071 /* initial contact - try to establish second link */
Karsten Graul2d2209f2020-05-03 14:38:43 +02001072 smc_llc_srv_add_link(link);
Karsten Graul75d320d2018-03-01 13:51:31 +01001073 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001074}
1075
Hans Wippel3b2dec22018-05-18 09:34:18 +02001076/* listen worker: finish */
1077static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001078{
Ursula Brauna046d572017-01-09 16:55:16 +01001079 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001080 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001081
Ursula Brauna046d572017-01-09 16:55:16 +01001082 if (lsmc->sk.sk_state == SMC_LISTEN) {
Karsten Graulfd577702019-04-11 11:17:30 +02001083 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001084 smc_accept_enqueue(&lsmc->sk, newsmcsk);
Karsten Graulfd577702019-04-11 11:17:30 +02001085 release_sock(&lsmc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001086 } else { /* no longer listening */
1087 smc_close_non_accepted(newsmcsk);
1088 }
Ursula Brauna046d572017-01-09 16:55:16 +01001089
1090 /* Wake up accept */
1091 lsmc->sk.sk_data_ready(&lsmc->sk);
1092 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001093}
Ursula Brauna046d572017-01-09 16:55:16 +01001094
Hans Wippel3b2dec22018-05-18 09:34:18 +02001095/* listen worker: finish in state connected */
1096static void smc_listen_out_connected(struct smc_sock *new_smc)
1097{
1098 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001099
Hans Wippel3b2dec22018-05-18 09:34:18 +02001100 sk_refcnt_debug_inc(newsmcsk);
1101 if (newsmcsk->sk_state == SMC_INIT)
1102 newsmcsk->sk_state = SMC_ACTIVE;
1103
1104 smc_listen_out(new_smc);
1105}
1106
1107/* listen worker: finish in error state */
1108static void smc_listen_out_err(struct smc_sock *new_smc)
1109{
1110 struct sock *newsmcsk = &new_smc->sk;
1111
Ursula Braun51f1de72018-01-26 09:28:48 +01001112 if (newsmcsk->sk_state == SMC_INIT)
1113 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001114 newsmcsk->sk_state = SMC_CLOSED;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001115
1116 smc_listen_out(new_smc);
1117}
1118
1119/* listen worker: decline and fall back if possible */
1120static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1121 int local_contact)
1122{
1123 /* RDMA setup failed, switch back to TCP */
1124 if (local_contact == SMC_FIRST_CONTACT)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001125 smc_lgr_cleanup_early(&new_smc->conn);
1126 else
1127 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001128 if (reason_code < 0) { /* error, no fallback possible */
1129 smc_listen_out_err(new_smc);
1130 return;
1131 }
Ursula Braun07603b22019-04-11 11:17:32 +02001132 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001133 new_smc->fallback_rsn = reason_code;
1134 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001135 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1136 smc_listen_out_err(new_smc);
1137 return;
1138 }
1139 }
1140 smc_listen_out_connected(new_smc);
1141}
1142
1143/* listen worker: check prefixes */
Karsten Graul59886692019-04-12 12:57:25 +02001144static int smc_listen_prfx_check(struct smc_sock *new_smc,
Hans Wippel3b2dec22018-05-18 09:34:18 +02001145 struct smc_clc_msg_proposal *pclc)
1146{
1147 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1148 struct socket *newclcsock = new_smc->clcsock;
1149
1150 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1151 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
Karsten Graul59886692019-04-12 12:57:25 +02001152 return SMC_CLC_DECL_DIFFPREFIX;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001153
1154 return 0;
1155}
1156
1157/* listen worker: initialize connection and buffers */
1158static int smc_listen_rdma_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001159 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001160{
Karsten Graul7a627252019-04-12 12:57:30 +02001161 int rc;
1162
Hans Wippel3b2dec22018-05-18 09:34:18 +02001163 /* allocate connection / link group */
Karsten Graul7a627252019-04-12 12:57:30 +02001164 rc = smc_conn_create(new_smc, ini);
1165 if (rc)
1166 return rc;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001167
1168 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001169 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001170 return SMC_CLC_DECL_MEM;
1171
1172 return 0;
1173}
1174
Hans Wippel41349842018-06-28 19:05:11 +02001175/* listen worker: initialize connection and buffers for SMC-D */
1176static int smc_listen_ism_init(struct smc_sock *new_smc,
1177 struct smc_clc_msg_proposal *pclc,
Karsten Graul7a627252019-04-12 12:57:30 +02001178 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +02001179{
1180 struct smc_clc_msg_smcd *pclc_smcd;
Karsten Graul7a627252019-04-12 12:57:30 +02001181 int rc;
Hans Wippel41349842018-06-28 19:05:11 +02001182
1183 pclc_smcd = smc_get_clc_msg_smcd(pclc);
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001184 ini->ism_gid = pclc_smcd->gid;
Karsten Graul7a627252019-04-12 12:57:30 +02001185 rc = smc_conn_create(new_smc, ini);
1186 if (rc)
1187 return rc;
Hans Wippel41349842018-06-28 19:05:11 +02001188
1189 /* Check if peer can be reached via ISM device */
1190 if (smc_ism_cantalk(new_smc->conn.lgr->peer_gid,
1191 new_smc->conn.lgr->vlan_id,
1192 new_smc->conn.lgr->smcd)) {
Karsten Graul7a627252019-04-12 12:57:30 +02001193 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001194 smc_lgr_cleanup_early(&new_smc->conn);
1195 else
1196 smc_conn_free(&new_smc->conn);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001197 return SMC_CLC_DECL_SMCDNOTALK;
Hans Wippel41349842018-06-28 19:05:11 +02001198 }
1199
1200 /* Create send and receive buffers */
1201 if (smc_buf_create(new_smc, true)) {
Karsten Graul7a627252019-04-12 12:57:30 +02001202 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001203 smc_lgr_cleanup_early(&new_smc->conn);
1204 else
1205 smc_conn_free(&new_smc->conn);
Hans Wippel41349842018-06-28 19:05:11 +02001206 return SMC_CLC_DECL_MEM;
1207 }
1208
1209 return 0;
1210}
1211
Hans Wippel3b2dec22018-05-18 09:34:18 +02001212/* listen worker: register buffers */
1213static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
1214{
Karsten Graulb9247542020-04-29 17:10:41 +02001215 struct smc_connection *conn = &new_smc->conn;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001216
1217 if (local_contact != SMC_FIRST_CONTACT) {
Karsten Graul7562a132020-05-01 12:48:01 +02001218 if (smcr_lgr_reg_rmbs(conn->lnk, conn->rmb_desc))
Karsten Graulc7674c02018-11-22 10:26:43 +01001219 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001220 }
1221 smc_rmb_sync_sg_for_device(&new_smc->conn);
1222
1223 return 0;
1224}
1225
1226/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001227static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1228 struct smc_clc_msg_accept_confirm *cclc,
1229 int local_contact)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001230{
Karsten Graul387707f2020-04-29 17:10:40 +02001231 struct smc_link *link = new_smc->conn.lnk;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001232 int reason_code = 0;
1233
1234 if (local_contact == SMC_FIRST_CONTACT)
1235 smc_link_save_peer_info(link, cclc);
1236
Karsten Graule07d31d2020-04-29 17:10:42 +02001237 if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001238 reason_code = SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001239 goto decline;
1240 }
1241
1242 if (local_contact == SMC_FIRST_CONTACT) {
1243 if (smc_ib_ready_link(link)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001244 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001245 goto decline;
1246 }
1247 /* QP confirmation over RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001248 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
Karsten Graulb9247542020-04-29 17:10:41 +02001249 reason_code = smcr_serv_conf_first_link(new_smc);
Karsten Graul4667bb42020-04-30 15:55:42 +02001250 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001251 if (reason_code)
1252 goto decline;
1253 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001254 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001255
1256decline:
Hans Wippel3b2dec22018-05-18 09:34:18 +02001257 smc_listen_decline(new_smc, reason_code, local_contact);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001258 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001259}
1260
1261/* setup for RDMA connection of server */
1262static void smc_listen_work(struct work_struct *work)
1263{
1264 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1265 smc_listen_work);
1266 struct socket *newclcsock = new_smc->clcsock;
1267 struct smc_clc_msg_accept_confirm cclc;
1268 struct smc_clc_msg_proposal *pclc;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001269 struct smc_init_info ini = {0};
Hans Wippel41349842018-06-28 19:05:11 +02001270 bool ism_supported = false;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001271 u8 buf[SMC_CLC_MAX_LEN];
Hans Wippel3b2dec22018-05-18 09:34:18 +02001272 int rc = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001273
Karsten Graulfd577702019-04-11 11:17:30 +02001274 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1275 return smc_listen_out_err(new_smc);
1276
Hans Wippel3b2dec22018-05-18 09:34:18 +02001277 if (new_smc->use_fallback) {
1278 smc_listen_out_connected(new_smc);
1279 return;
1280 }
1281
1282 /* check if peer is smc capable */
1283 if (!tcp_sk(newclcsock->sk)->syn_smc) {
Ursula Braun07603b22019-04-11 11:17:32 +02001284 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001285 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001286 smc_listen_out_connected(new_smc);
1287 return;
1288 }
1289
1290 /* do inband token exchange -
1291 * wait for and receive SMC Proposal CLC message
1292 */
1293 pclc = (struct smc_clc_msg_proposal *)&buf;
Karsten Graul228bae02019-04-12 12:57:28 +02001294 rc = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
1295 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001296 if (rc)
1297 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001298
1299 /* IPSec connections opt out of SMC-R optimizations */
1300 if (using_ipsec(new_smc)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001301 rc = SMC_CLC_DECL_IPSEC;
1302 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001303 }
1304
Karsten Graul59886692019-04-12 12:57:25 +02001305 /* check for matching IP prefix and subnet length */
1306 rc = smc_listen_prfx_check(new_smc, pclc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001307 if (rc)
1308 goto out_decl;
Karsten Graul59886692019-04-12 12:57:25 +02001309
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001310 /* get vlan id from IP device */
1311 if (smc_vlan_by_tcpsk(new_smc->clcsock, &ini)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001312 rc = SMC_CLC_DECL_GETVLANERR;
1313 goto out_decl;
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001314 }
1315
Hans Wippel72a36a82019-02-07 15:56:18 +01001316 mutex_lock(&smc_server_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001317 smc_close_init(new_smc);
1318 smc_rx_init(new_smc);
1319 smc_tx_init(new_smc);
1320
Hans Wippel41349842018-06-28 19:05:11 +02001321 /* check if ISM is available */
Karsten Graul9aa68d22019-04-12 12:57:29 +02001322 if (pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) {
1323 ini.is_smcd = true; /* prepare ISM check */
1324 rc = smc_find_ism_device(new_smc, &ini);
1325 if (!rc)
Karsten Graul7a627252019-04-12 12:57:30 +02001326 rc = smc_listen_ism_init(new_smc, pclc, &ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001327 if (!rc)
1328 ism_supported = true;
1329 else if (pclc->hdr.path == SMC_TYPE_D)
1330 goto out_unlock; /* skip RDMA and decline */
1331 }
1332
1333 /* check if RDMA is available */
1334 if (!ism_supported) { /* SMC_TYPE_R or SMC_TYPE_B */
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001335 /* prepare RDMA check */
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001336 ini.is_smcd = false;
Ursula Braunca5f8d22019-10-23 15:44:06 +02001337 ini.ism_dev = NULL;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001338 ini.ib_lcl = &pclc->lcl;
Karsten Graul9aa68d22019-04-12 12:57:29 +02001339 rc = smc_find_rdma_device(new_smc, &ini);
1340 if (rc) {
1341 /* no RDMA device found */
1342 if (pclc->hdr.path == SMC_TYPE_B)
1343 /* neither ISM nor RDMA device found */
1344 rc = SMC_CLC_DECL_NOSMCDEV;
1345 goto out_unlock;
1346 }
Karsten Graul7a627252019-04-12 12:57:30 +02001347 rc = smc_listen_rdma_init(new_smc, &ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001348 if (rc)
1349 goto out_unlock;
Karsten Graul7a627252019-04-12 12:57:30 +02001350 rc = smc_listen_rdma_reg(new_smc, ini.cln_first_contact);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001351 if (rc)
1352 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001353 }
1354
1355 /* send SMC Accept CLC message */
Karsten Graul7a627252019-04-12 12:57:30 +02001356 rc = smc_clc_send_accept(new_smc, ini.cln_first_contact);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001357 if (rc)
1358 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001359
Hans Wippel62c71392019-02-07 15:56:17 +01001360 /* SMC-D does not need this lock any more */
1361 if (ism_supported)
Hans Wippel72a36a82019-02-07 15:56:18 +01001362 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001363
Hans Wippel3b2dec22018-05-18 09:34:18 +02001364 /* receive SMC Confirm CLC message */
Karsten Graul228bae02019-04-12 12:57:28 +02001365 rc = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
1366 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
1367 if (rc) {
Hans Wippel62c71392019-02-07 15:56:17 +01001368 if (!ism_supported)
Karsten Graul9aa68d22019-04-12 12:57:29 +02001369 goto out_unlock;
1370 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001371 }
1372
1373 /* finish worker */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001374 if (!ism_supported) {
Karsten Graul7a627252019-04-12 12:57:30 +02001375 rc = smc_listen_rdma_finish(new_smc, &cclc,
1376 ini.cln_first_contact);
Hans Wippel72a36a82019-02-07 15:56:18 +01001377 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001378 if (rc)
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001379 return;
1380 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001381 smc_conn_save_peer_info(new_smc, &cclc);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001382 smc_listen_out_connected(new_smc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001383 return;
1384
1385out_unlock:
1386 mutex_unlock(&smc_server_lgr_pending);
1387out_decl:
Karsten Graul7a627252019-04-12 12:57:30 +02001388 smc_listen_decline(new_smc, rc, ini.cln_first_contact);
Ursula Brauna046d572017-01-09 16:55:16 +01001389}
1390
1391static void smc_tcp_listen_work(struct work_struct *work)
1392{
1393 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1394 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001395 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001396 struct smc_sock *new_smc;
1397 int rc = 0;
1398
Ursula Braun3163c502018-01-24 10:28:12 +01001399 lock_sock(lsk);
1400 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001401 rc = smc_clcsock_accept(lsmc, &new_smc);
1402 if (rc)
1403 goto out;
1404 if (!new_smc)
1405 continue;
1406
1407 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001408 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001409 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001410 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001411 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1412 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001413 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1414 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001415 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1416 if (!schedule_work(&new_smc->smc_listen_work))
1417 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001418 }
1419
1420out:
Ursula Braun3163c502018-01-24 10:28:12 +01001421 release_sock(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001422 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +01001423}
1424
Ursula Braunac713872017-01-09 16:55:13 +01001425static int smc_listen(struct socket *sock, int backlog)
1426{
1427 struct sock *sk = sock->sk;
1428 struct smc_sock *smc;
1429 int rc;
1430
1431 smc = smc_sk(sk);
1432 lock_sock(sk);
1433
1434 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +02001435 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
1436 smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +01001437 goto out;
1438
1439 rc = 0;
1440 if (sk->sk_state == SMC_LISTEN) {
1441 sk->sk_max_ack_backlog = backlog;
1442 goto out;
1443 }
1444 /* some socket options are handled in core, so we could not apply
1445 * them to the clc socket -- copy smc socket options to clc socket
1446 */
1447 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001448 if (!smc->use_fallback)
1449 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001450
1451 rc = kernel_listen(smc->clcsock, backlog);
1452 if (rc)
1453 goto out;
1454 sk->sk_max_ack_backlog = backlog;
1455 sk->sk_ack_backlog = 0;
1456 sk->sk_state = SMC_LISTEN;
Ursula Braun51f1de72018-01-26 09:28:48 +01001457 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1458 if (!schedule_work(&smc->tcp_listen_work))
1459 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001460
1461out:
1462 release_sock(sk);
1463 return rc;
1464}
1465
1466static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001467 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001468{
Ursula Brauna046d572017-01-09 16:55:16 +01001469 struct sock *sk = sock->sk, *nsk;
1470 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001471 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001472 long timeo;
1473 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001474
1475 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001476 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001477 lock_sock(sk);
1478
1479 if (lsmc->sk.sk_state != SMC_LISTEN) {
1480 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001481 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001482 goto out;
1483 }
1484
Ursula Brauna046d572017-01-09 16:55:16 +01001485 /* Wait for an incoming connection */
1486 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1487 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1488 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1489 set_current_state(TASK_INTERRUPTIBLE);
1490 if (!timeo) {
1491 rc = -EAGAIN;
1492 break;
1493 }
1494 release_sock(sk);
1495 timeo = schedule_timeout(timeo);
1496 /* wakeup by sk_data_ready in smc_listen_work() */
1497 sched_annotate_sleep();
1498 lock_sock(sk);
1499 if (signal_pending(current)) {
1500 rc = sock_intr_errno(timeo);
1501 break;
1502 }
1503 }
1504 set_current_state(TASK_RUNNING);
1505 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001506
Ursula Brauna046d572017-01-09 16:55:16 +01001507 if (!rc)
1508 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001509 release_sock(sk);
1510 if (rc)
1511 goto out;
1512
1513 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1514 /* wait till data arrives on the socket */
1515 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1516 MSEC_PER_SEC);
1517 if (smc_sk(nsk)->use_fallback) {
1518 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1519
1520 lock_sock(clcsk);
1521 if (skb_queue_empty(&clcsk->sk_receive_queue))
1522 sk_wait_data(clcsk, &timeo, NULL);
1523 release_sock(clcsk);
1524 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1525 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001526 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001527 release_sock(nsk);
1528 }
1529 }
Ursula Braunac713872017-01-09 16:55:13 +01001530
1531out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001532 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001533 return rc;
1534}
1535
1536static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001537 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001538{
1539 struct smc_sock *smc;
1540
Ursula Braunb38d7322017-01-09 16:55:25 +01001541 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1542 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001543 return -ENOTCONN;
1544
1545 smc = smc_sk(sock->sk);
1546
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001547 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001548}
1549
1550static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1551{
1552 struct sock *sk = sock->sk;
1553 struct smc_sock *smc;
1554 int rc = -EPIPE;
1555
1556 smc = smc_sk(sk);
1557 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001558 if ((sk->sk_state != SMC_ACTIVE) &&
1559 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1560 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001561 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001562
1563 if (msg->msg_flags & MSG_FASTOPEN) {
Ursula Brauncd206362019-08-02 10:47:50 +02001564 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001565 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001566 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001567 } else {
1568 rc = -EINVAL;
1569 goto out;
1570 }
1571 }
1572
Ursula Braunac713872017-01-09 16:55:13 +01001573 if (smc->use_fallback)
1574 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1575 else
Ursula Braune6727f32017-01-09 16:55:23 +01001576 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001577out:
1578 release_sock(sk);
1579 return rc;
1580}
1581
1582static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1583 int flags)
1584{
1585 struct sock *sk = sock->sk;
1586 struct smc_sock *smc;
1587 int rc = -ENOTCONN;
1588
1589 smc = smc_sk(sk);
1590 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001591 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1592 /* socket was connected before, no more data to read */
1593 rc = 0;
1594 goto out;
1595 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001596 if ((sk->sk_state == SMC_INIT) ||
1597 (sk->sk_state == SMC_LISTEN) ||
1598 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001599 goto out;
1600
Ursula Braunb38d7322017-01-09 16:55:25 +01001601 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1602 rc = 0;
1603 goto out;
1604 }
1605
Stefan Raspl9014db22018-05-03 18:12:39 +02001606 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001607 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001608 } else {
1609 msg->msg_namelen = 0;
1610 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1611 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001612
Ursula Braunac713872017-01-09 16:55:13 +01001613out:
1614 release_sock(sk);
1615 return rc;
1616}
1617
Al Viroade994f2017-07-03 00:01:49 -04001618static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001619{
Ursula Braun8dce2782018-01-26 09:28:47 +01001620 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001621 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001622
Ursula Braun8dce2782018-01-26 09:28:47 +01001623 spin_lock(&isk->accept_q_lock);
1624 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001625 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001626 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001627
Ursula Braun8dce2782018-01-26 09:28:47 +01001628 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001629}
1630
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001631static __poll_t smc_poll(struct file *file, struct socket *sock,
1632 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01001633{
1634 struct sock *sk = sock->sk;
Ursula Braunac713872017-01-09 16:55:13 +01001635 struct smc_sock *smc;
Ursula Braun50717a32019-04-12 12:57:23 +02001636 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001637
Ursula Braun8dce2782018-01-26 09:28:47 +01001638 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001639 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001640
Ursula Braunac713872017-01-09 16:55:13 +01001641 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02001642 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01001643 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001644 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02001645 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braunac713872017-01-09 16:55:13 +01001646 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07001647 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02001648 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01001649 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001650 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001651 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1652 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001653 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001654 if (sk->sk_state == SMC_LISTEN) {
1655 /* woken up by sk_data_ready in smc_listen_work() */
Ursula Braun50717a32019-04-12 12:57:23 +02001656 mask |= smc_accept_poll(sk);
1657 } else if (smc->use_fallback) { /* as result of connect_work()*/
1658 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
1659 wait);
1660 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun8dce2782018-01-26 09:28:47 +01001661 } else {
Ursula Braun50717a32019-04-12 12:57:23 +02001662 if ((sk->sk_state != SMC_INIT &&
1663 atomic_read(&smc->conn.sndbuf_space)) ||
Ursula Braun8dce2782018-01-26 09:28:47 +01001664 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001665 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001666 } else {
1667 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1668 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1669 }
1670 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001671 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001672 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001673 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001674 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001675 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02001676 if (smc->conn.urg_state == SMC_URG_VALID)
1677 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01001678 }
Ursula Braunac713872017-01-09 16:55:13 +01001679 }
1680
1681 return mask;
1682}
1683
1684static int smc_shutdown(struct socket *sock, int how)
1685{
1686 struct sock *sk = sock->sk;
1687 struct smc_sock *smc;
1688 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001689 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001690
1691 smc = smc_sk(sk);
1692
1693 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001694 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001695
1696 lock_sock(sk);
1697
1698 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02001699 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01001700 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1701 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1702 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1703 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1704 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001705 goto out;
1706 if (smc->use_fallback) {
1707 rc = kernel_sock_shutdown(smc->clcsock, how);
1708 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1709 if (sk->sk_shutdown == SHUTDOWN_MASK)
1710 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001711 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001712 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001713 switch (how) {
1714 case SHUT_RDWR: /* shutdown in both directions */
1715 rc = smc_close_active(smc);
1716 break;
1717 case SHUT_WR:
1718 rc = smc_close_shutdown_write(smc);
1719 break;
1720 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001721 rc = 0;
1722 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001723 break;
1724 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001725 if (smc->clcsock)
1726 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001727 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1728 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001729
1730out:
1731 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001732 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001733}
1734
1735static int smc_setsockopt(struct socket *sock, int level, int optname,
1736 char __user *optval, unsigned int optlen)
1737{
1738 struct sock *sk = sock->sk;
1739 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001740 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001741
1742 smc = smc_sk(sk);
1743
1744 /* generic setsockopts reaching us here always apply to the
1745 * CLC socket
1746 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001747 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1748 optval, optlen);
1749 if (smc->clcsock->sk->sk_err) {
1750 sk->sk_err = smc->clcsock->sk->sk_err;
1751 sk->sk_error_report(sk);
1752 }
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001753
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001754 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00001755 return -EINVAL;
Ursula Braunac0107e2018-07-18 15:22:50 +02001756 if (get_user(val, (int __user *)optval))
1757 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001758
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001759 lock_sock(sk);
Ursula Braun86434742019-12-12 22:35:58 +01001760 if (rc || smc->use_fallback)
1761 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001762 switch (optname) {
1763 case TCP_ULP:
1764 case TCP_FASTOPEN:
1765 case TCP_FASTOPEN_CONNECT:
1766 case TCP_FASTOPEN_KEY:
1767 case TCP_FASTOPEN_NO_COOKIE:
1768 /* option not supported by SMC */
Ursula Braun8204df72019-11-15 12:39:30 +01001769 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001770 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001771 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001772 } else {
Ursula Braun86434742019-12-12 22:35:58 +01001773 rc = -EINVAL;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001774 }
1775 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001776 case TCP_NODELAY:
Ursula Braunf9cedf12019-08-02 10:16:38 +02001777 if (sk->sk_state != SMC_INIT &&
1778 sk->sk_state != SMC_LISTEN &&
1779 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01001780 if (val)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001781 mod_delayed_work(system_wq, &smc->conn.tx_work,
1782 0);
1783 }
1784 break;
1785 case TCP_CORK:
Ursula Braunf9cedf12019-08-02 10:16:38 +02001786 if (sk->sk_state != SMC_INIT &&
1787 sk->sk_state != SMC_LISTEN &&
1788 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01001789 if (!val)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001790 mod_delayed_work(system_wq, &smc->conn.tx_work,
1791 0);
1792 }
1793 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001794 case TCP_DEFER_ACCEPT:
1795 smc->sockopt_defer_accept = val;
1796 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001797 default:
1798 break;
1799 }
Ursula Braun86434742019-12-12 22:35:58 +01001800out:
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001801 release_sock(sk);
1802
1803 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001804}
1805
1806static int smc_getsockopt(struct socket *sock, int level, int optname,
1807 char __user *optval, int __user *optlen)
1808{
1809 struct smc_sock *smc;
1810
1811 smc = smc_sk(sock->sk);
1812 /* socket options apply to the CLC socket */
1813 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1814 optval, optlen);
1815}
1816
1817static int smc_ioctl(struct socket *sock, unsigned int cmd,
1818 unsigned long arg)
1819{
Stefan Rasplde8474e2018-05-23 16:38:11 +02001820 union smc_host_cursor cons, urg;
1821 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01001822 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001823 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001824
1825 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001826 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02001827 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02001828 if (smc->use_fallback) {
1829 if (!smc->clcsock) {
1830 release_sock(&smc->sk);
1831 return -EBADF;
1832 }
1833 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1834 release_sock(&smc->sk);
1835 return answ;
1836 }
Ursula Braun9b67e262018-05-02 16:56:46 +02001837 switch (cmd) {
1838 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02001839 if (smc->sk.sk_state == SMC_LISTEN) {
1840 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001841 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001842 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001843 if (smc->sk.sk_state == SMC_INIT ||
1844 smc->sk.sk_state == SMC_CLOSED)
1845 answ = 0;
1846 else
1847 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02001848 break;
1849 case SIOCOUTQ:
1850 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02001851 if (smc->sk.sk_state == SMC_LISTEN) {
1852 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001853 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001854 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001855 if (smc->sk.sk_state == SMC_INIT ||
1856 smc->sk.sk_state == SMC_CLOSED)
1857 answ = 0;
1858 else
1859 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02001860 atomic_read(&smc->conn.sndbuf_space);
1861 break;
1862 case SIOCOUTQNSD:
1863 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02001864 if (smc->sk.sk_state == SMC_LISTEN) {
1865 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001866 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001867 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001868 if (smc->sk.sk_state == SMC_INIT ||
1869 smc->sk.sk_state == SMC_CLOSED)
1870 answ = 0;
1871 else
1872 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02001873 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02001874 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02001875 if (smc->sk.sk_state == SMC_LISTEN) {
1876 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001877 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001878 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001879 if (smc->sk.sk_state == SMC_INIT ||
1880 smc->sk.sk_state == SMC_CLOSED) {
1881 answ = 0;
1882 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02001883 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1884 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001885 answ = smc_curs_diff(conn->rmb_desc->len,
1886 &cons, &urg) == 1;
1887 }
1888 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02001889 default:
Ursula Braun1992d992018-07-16 13:56:52 +02001890 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001891 return -ENOIOCTLCMD;
1892 }
Ursula Braun1992d992018-07-16 13:56:52 +02001893 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001894
1895 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001896}
1897
1898static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1899 int offset, size_t size, int flags)
1900{
1901 struct sock *sk = sock->sk;
1902 struct smc_sock *smc;
1903 int rc = -EPIPE;
1904
1905 smc = smc_sk(sk);
1906 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001907 if (sk->sk_state != SMC_ACTIVE) {
1908 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001909 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001910 }
1911 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001912 if (smc->use_fallback)
1913 rc = kernel_sendpage(smc->clcsock, page, offset,
1914 size, flags);
1915 else
1916 rc = sock_no_sendpage(sock, page, offset, size, flags);
1917
1918out:
Ursula Braunac713872017-01-09 16:55:13 +01001919 return rc;
1920}
1921
Stefan Raspl9014db22018-05-03 18:12:39 +02001922/* Map the affected portions of the rmbe into an spd, note the number of bytes
1923 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1924 * updates till whenever a respective page has been fully processed.
1925 * Note that subsequent recv() calls have to wait till all splice() processing
1926 * completed.
1927 */
Ursula Braunac713872017-01-09 16:55:13 +01001928static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1929 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001930 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001931{
1932 struct sock *sk = sock->sk;
1933 struct smc_sock *smc;
1934 int rc = -ENOTCONN;
1935
1936 smc = smc_sk(sk);
1937 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001938 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1939 /* socket was connected before, no more data to read */
1940 rc = 0;
1941 goto out;
1942 }
Stefan Raspl9014db22018-05-03 18:12:39 +02001943 if (sk->sk_state == SMC_INIT ||
1944 sk->sk_state == SMC_LISTEN ||
1945 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001946 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001947
1948 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1949 rc = 0;
1950 goto out;
1951 }
1952
Ursula Braunac713872017-01-09 16:55:13 +01001953 if (smc->use_fallback) {
1954 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1955 pipe, len, flags);
1956 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001957 if (*ppos) {
1958 rc = -ESPIPE;
1959 goto out;
1960 }
1961 if (flags & SPLICE_F_NONBLOCK)
1962 flags = MSG_DONTWAIT;
1963 else
1964 flags = 0;
1965 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001966 }
1967out:
1968 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001969
Ursula Braunac713872017-01-09 16:55:13 +01001970 return rc;
1971}
1972
1973/* must look like tcp */
1974static const struct proto_ops smc_sock_ops = {
1975 .family = PF_SMC,
1976 .owner = THIS_MODULE,
1977 .release = smc_release,
1978 .bind = smc_bind,
1979 .connect = smc_connect,
1980 .socketpair = sock_no_socketpair,
1981 .accept = smc_accept,
1982 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001983 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01001984 .ioctl = smc_ioctl,
1985 .listen = smc_listen,
1986 .shutdown = smc_shutdown,
1987 .setsockopt = smc_setsockopt,
1988 .getsockopt = smc_getsockopt,
1989 .sendmsg = smc_sendmsg,
1990 .recvmsg = smc_recvmsg,
1991 .mmap = sock_no_mmap,
1992 .sendpage = smc_sendpage,
1993 .splice_read = smc_splice_read,
1994};
1995
1996static int smc_create(struct net *net, struct socket *sock, int protocol,
1997 int kern)
1998{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001999 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01002000 struct smc_sock *smc;
2001 struct sock *sk;
2002 int rc;
2003
2004 rc = -ESOCKTNOSUPPORT;
2005 if (sock->type != SOCK_STREAM)
2006 goto out;
2007
2008 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002009 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01002010 goto out;
2011
2012 rc = -ENOBUFS;
2013 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002014 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01002015 if (!sk)
2016 goto out;
2017
2018 /* create internal TCP socket for CLC handshake and fallback */
2019 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01002020 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02002021 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002022 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
2023 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01002024 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01002025 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01002026 goto out;
2027 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01002028 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
2029 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01002030
2031out:
2032 return rc;
2033}
2034
2035static const struct net_proto_family smc_sock_family_ops = {
2036 .family = PF_SMC,
2037 .owner = THIS_MODULE,
2038 .create = smc_create,
2039};
2040
Hans Wippel64e28b52019-02-21 13:01:02 +01002041unsigned int smc_net_id;
2042
2043static __net_init int smc_net_init(struct net *net)
2044{
2045 return smc_pnet_net_init(net);
2046}
2047
2048static void __net_exit smc_net_exit(struct net *net)
2049{
2050 smc_pnet_net_exit(net);
2051}
2052
2053static struct pernet_operations smc_net_ops = {
2054 .init = smc_net_init,
2055 .exit = smc_net_exit,
2056 .id = &smc_net_id,
2057 .size = sizeof(struct smc_net),
2058};
2059
Ursula Braunac713872017-01-09 16:55:13 +01002060static int __init smc_init(void)
2061{
2062 int rc;
2063
Hans Wippel64e28b52019-02-21 13:01:02 +01002064 rc = register_pernet_subsys(&smc_net_ops);
2065 if (rc)
2066 return rc;
2067
Thomas Richter6812baa2017-01-09 16:55:15 +01002068 rc = smc_pnet_init();
2069 if (rc)
YueHaibing8c33bf12019-06-26 17:47:50 +02002070 goto out_pernet_subsys;
Thomas Richter6812baa2017-01-09 16:55:15 +01002071
Ursula Braun6dabd402019-11-16 17:47:29 +01002072 rc = smc_core_init();
2073 if (rc) {
2074 pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
2075 goto out_pnet;
2076 }
2077
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002078 rc = smc_llc_init();
2079 if (rc) {
2080 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002081 goto out_core;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002082 }
2083
Ursula Braun5f083182017-01-09 16:55:22 +01002084 rc = smc_cdc_init();
2085 if (rc) {
2086 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002087 goto out_core;
Ursula Braun5f083182017-01-09 16:55:22 +01002088 }
2089
Ursula Braunac713872017-01-09 16:55:13 +01002090 rc = proto_register(&smc_proto, 1);
2091 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01002092 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002093 goto out_core;
Ursula Braunac713872017-01-09 16:55:13 +01002094 }
2095
Karsten Graulaaa4d332018-03-16 15:06:41 +01002096 rc = proto_register(&smc_proto6, 1);
2097 if (rc) {
2098 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2099 goto out_proto;
2100 }
2101
Ursula Braunac713872017-01-09 16:55:13 +01002102 rc = sock_register(&smc_sock_family_ops);
2103 if (rc) {
2104 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002105 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01002106 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01002107 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002108 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01002109
Ursula Brauna4cf0442017-01-09 16:55:14 +01002110 rc = smc_ib_register_client();
2111 if (rc) {
2112 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2113 goto out_sock;
2114 }
2115
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002116 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002117 return 0;
2118
Ursula Brauna4cf0442017-01-09 16:55:14 +01002119out_sock:
2120 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002121out_proto6:
2122 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002123out_proto:
2124 proto_unregister(&smc_proto);
Ursula Braun6dabd402019-11-16 17:47:29 +01002125out_core:
2126 smc_core_exit();
Thomas Richter6812baa2017-01-09 16:55:15 +01002127out_pnet:
2128 smc_pnet_exit();
YueHaibing8c33bf12019-06-26 17:47:50 +02002129out_pernet_subsys:
2130 unregister_pernet_subsys(&smc_net_ops);
2131
Ursula Braunac713872017-01-09 16:55:13 +01002132 return rc;
2133}
2134
2135static void __exit smc_exit(void)
2136{
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002137 static_branch_disable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002138 sock_unregister(PF_SMC);
Ursula Braun6dabd402019-11-16 17:47:29 +01002139 smc_core_exit();
2140 smc_ib_unregister_client();
Karsten Graulaaa4d332018-03-16 15:06:41 +01002141 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002142 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002143 smc_pnet_exit();
Hans Wippel64e28b52019-02-21 13:01:02 +01002144 unregister_pernet_subsys(&smc_net_ops);
Ursula Braun4ead9c92019-11-16 17:47:31 +01002145 rcu_barrier();
Ursula Braunac713872017-01-09 16:55:13 +01002146}
2147
2148module_init(smc_init);
2149module_exit(smc_exit);
2150
2151MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2152MODULE_DESCRIPTION("smc socket address family");
2153MODULE_LICENSE("GPL");
2154MODULE_ALIAS_NETPROTO(PF_SMC);