blob: ab3aef1ddfa4922377a00d2fdeff484420366875 [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 Graulc7674c02018-11-22 10:26:43 +0100340/* register a new rmb, send confirm_rkey msg to register with peer */
Karsten Graulb9247542020-04-29 17:10:41 +0200341static int smcr_link_reg_rmb(struct smc_link *link,
342 struct smc_buf_desc *rmb_desc, bool conf_rkey)
Karsten Graule63a5f82018-05-03 17:57:37 +0200343{
Karsten Graulb9247542020-04-29 17:10:41 +0200344 if (!rmb_desc->is_reg_mr[link->link_idx]) {
Karsten Graulc7674c02018-11-22 10:26:43 +0100345 /* register memory region for new rmb */
Karsten Graul387707f2020-04-29 17:10:40 +0200346 if (smc_wr_reg_send(link, rmb_desc->mr_rx[link->link_idx])) {
Karsten Graulb9247542020-04-29 17:10:41 +0200347 rmb_desc->is_reg_err = true;
Karsten Graulc7674c02018-11-22 10:26:43 +0100348 return -EFAULT;
349 }
Karsten Graulb9247542020-04-29 17:10:41 +0200350 rmb_desc->is_reg_mr[link->link_idx] = true;
Karsten Graula6920d12018-05-03 17:57:38 +0200351 }
Karsten Graul44aa81c2018-05-15 17:04:55 +0200352 if (!conf_rkey)
353 return 0;
Karsten Graulb9247542020-04-29 17:10:41 +0200354
Karsten Graul44aa81c2018-05-15 17:04:55 +0200355 /* exchange confirm_rkey msg with peer */
Karsten Graulb9247542020-04-29 17:10:41 +0200356 if (!rmb_desc->is_conf_rkey) {
357 if (smc_llc_do_confirm_rkey(link, rmb_desc)) {
358 rmb_desc->is_reg_err = true;
359 return -EFAULT;
360 }
361 rmb_desc->is_conf_rkey = true;
Karsten Graul44aa81c2018-05-15 17:04:55 +0200362 }
Karsten Graule63a5f82018-05-03 17:57:37 +0200363 return 0;
364}
365
Karsten Graulb9247542020-04-29 17:10:41 +0200366/* register the new rmb on all links */
367static int smcr_lgr_reg_rmbs(struct smc_link_group *lgr,
368 struct smc_buf_desc *rmb_desc)
369{
370 int i, rc;
371
372 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
373 if (lgr->lnk[i].state != SMC_LNK_ACTIVE)
374 continue;
375 rc = smcr_link_reg_rmb(&lgr->lnk[i], rmb_desc, true);
376 if (rc)
377 return rc;
378 }
379 return 0;
380}
381
382static int smcr_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100383{
Karsten Graul387707f2020-04-29 17:10:40 +0200384 struct smc_link *link = smc->conn.lnk;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100385 int rest;
386 int rc;
387
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100388 /* receive CONFIRM LINK request from server over RoCE fabric */
389 rest = wait_for_completion_interruptible_timeout(
390 &link->llc_confirm,
391 SMC_LLC_WAIT_FIRST_TIME);
392 if (rest <= 0) {
393 struct smc_clc_msg_decline dclc;
394
395 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100396 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100397 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100398 }
399
Karsten Graul75d320d2018-03-01 13:51:31 +0100400 if (link->llc_confirm_rc)
401 return SMC_CLC_DECL_RMBE_EC;
402
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100403 rc = smc_ib_modify_qp_rts(link);
404 if (rc)
Karsten Graul603cc142018-07-25 16:35:32 +0200405 return SMC_CLC_DECL_ERR_RDYLNK;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100406
407 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200408
Karsten Graulb9247542020-04-29 17:10:41 +0200409 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200410 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200411
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100412 /* send CONFIRM LINK response over RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200413 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100414 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200415 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100416
Karsten Graul52bedf32018-03-01 13:51:32 +0100417 /* receive ADD LINK request from server over RoCE fabric */
418 rest = wait_for_completion_interruptible_timeout(&link->llc_add,
419 SMC_LLC_WAIT_TIME);
420 if (rest <= 0) {
421 struct smc_clc_msg_decline dclc;
422
423 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100424 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100425 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
Karsten Graul52bedf32018-03-01 13:51:32 +0100426 }
427
428 /* send add link reject message, only one link supported for now */
429 rc = smc_llc_send_add_link(link,
430 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +0200431 link->gid, SMC_LLC_RESP);
Karsten Graul52bedf32018-03-01 13:51:32 +0100432 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200433 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +0100434
Karsten Graul00a049c2020-04-29 17:10:49 +0200435 smc_llc_link_active(link);
Karsten Graul52bedf32018-03-01 13:51:32 +0100436
Karsten Graul75d320d2018-03-01 13:51:31 +0100437 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100438}
439
Hans Wippel41349842018-06-28 19:05:11 +0200440static void smcr_conn_save_peer_info(struct smc_sock *smc,
441 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100442{
Hans Wippel95d8d2632018-05-18 09:34:13 +0200443 int bufsize = smc_uncompress_bufsize(clc->rmbe_size);
444
Hans Wippel92a138e2018-05-18 09:34:12 +0200445 smc->conn.peer_rmbe_idx = clc->rmbe_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100446 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200447 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100448 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200449 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100450}
451
Hans Wippel41349842018-06-28 19:05:11 +0200452static void smcd_conn_save_peer_info(struct smc_sock *smc,
453 struct smc_clc_msg_accept_confirm *clc)
454{
455 int bufsize = smc_uncompress_bufsize(clc->dmbe_size);
456
457 smc->conn.peer_rmbe_idx = clc->dmbe_idx;
458 smc->conn.peer_token = clc->token;
459 /* msg header takes up space in the buffer */
460 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
461 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
462 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
463}
464
465static void smc_conn_save_peer_info(struct smc_sock *smc,
466 struct smc_clc_msg_accept_confirm *clc)
467{
468 if (smc->conn.lgr->is_smcd)
469 smcd_conn_save_peer_info(smc, clc);
470 else
471 smcr_conn_save_peer_info(smc, clc);
472}
473
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100474static void smc_link_save_peer_info(struct smc_link *link,
475 struct smc_clc_msg_accept_confirm *clc)
476{
477 link->peer_qpn = ntoh24(clc->qpn);
478 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
479 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
480 link->peer_psn = ntoh24(clc->psn);
481 link->peer_mtu = clc->qp_mtu;
482}
483
Ursula Braun07603b22019-04-11 11:17:32 +0200484static void smc_switch_to_fallback(struct smc_sock *smc)
485{
486 smc->use_fallback = true;
487 if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
488 smc->clcsock->file = smc->sk.sk_socket->file;
489 smc->clcsock->file->private_data = smc->clcsock;
Ursula Braun67f562e2020-02-14 08:58:59 +0100490 smc->clcsock->wq.fasync_list =
491 smc->sk.sk_socket->wq.fasync_list;
Ursula Braun07603b22019-04-11 11:17:32 +0200492 }
493}
494
Hans Wippel3b2dec22018-05-18 09:34:18 +0200495/* fall back during connect */
Karsten Graul603cc142018-07-25 16:35:32 +0200496static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200497{
Ursula Braun07603b22019-04-11 11:17:32 +0200498 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +0200499 smc->fallback_rsn = reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200500 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200501 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200502 if (smc->sk.sk_state == SMC_INIT)
503 smc->sk.sk_state = SMC_ACTIVE;
504 return 0;
505}
506
507/* decline and fall back during connect */
508static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
509{
510 int rc;
511
Ursula Braune1bbdd52018-07-05 16:15:30 +0200512 if (reason_code < 0) { /* error, fallback is not possible */
513 if (smc->sk.sk_state == SMC_INIT)
514 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200515 return reason_code;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200516 }
Karsten Graul603cc142018-07-25 16:35:32 +0200517 if (reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200518 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200519 if (rc < 0) {
520 if (smc->sk.sk_state == SMC_INIT)
521 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200522 return rc;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200523 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200524 }
Karsten Graul603cc142018-07-25 16:35:32 +0200525 return smc_connect_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200526}
527
528/* abort connecting */
529static int smc_connect_abort(struct smc_sock *smc, int reason_code,
530 int local_contact)
531{
Ursula Braun51e3dfa2020-02-25 16:34:36 +0100532 bool is_smcd = smc->conn.lgr->is_smcd;
533
Hans Wippel3b2dec22018-05-18 09:34:18 +0200534 if (local_contact == SMC_FIRST_CONTACT)
Ursula Braun51e3dfa2020-02-25 16:34:36 +0100535 smc_lgr_cleanup_early(&smc->conn);
536 else
537 smc_conn_free(&smc->conn);
538 if (is_smcd)
Hans Wippel72a36a82019-02-07 15:56:18 +0100539 /* there is only one lgr role for SMC-D; use server lock */
540 mutex_unlock(&smc_server_lgr_pending);
541 else
542 mutex_unlock(&smc_client_lgr_pending);
543
Ursula Braun50717a32019-04-12 12:57:23 +0200544 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200545 return reason_code;
546}
547
548/* check if there is a rdma device available for this connection. */
549/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200550static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200551{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200552 /* PNET table look up: search active ib_device and port
553 * within same PNETID that also contains the ethernet device
554 * used for the internal TCP socket
555 */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200556 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +0200557 if (!ini->ib_dev)
558 return SMC_CLC_DECL_NOSMCRDEV;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200559 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200560}
561
Hans Wippel41349842018-06-28 19:05:11 +0200562/* check if there is an ISM device available for this connection. */
563/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200564static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200565{
566 /* Find ISM device with same PNETID as connecting interface */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200567 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
568 if (!ini->ism_dev)
Karsten Graul9aa68d22019-04-12 12:57:29 +0200569 return SMC_CLC_DECL_NOSMCDDEV;
Hans Wippel41349842018-06-28 19:05:11 +0200570 return 0;
571}
572
573/* Check for VLAN ID and register it on ISM device just for CLC handshake */
574static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200575 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200576{
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200577 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev, ini->vlan_id))
Karsten Graul7a627252019-04-12 12:57:30 +0200578 return SMC_CLC_DECL_ISMVLANERR;
Hans Wippel41349842018-06-28 19:05:11 +0200579 return 0;
580}
581
582/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
583 * used, the VLAN ID will be registered again during the connection setup.
584 */
585static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200586 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200587{
588 if (!is_smcd)
589 return 0;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200590 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev, ini->vlan_id))
Hans Wippel41349842018-06-28 19:05:11 +0200591 return SMC_CLC_DECL_CNFERR;
592 return 0;
593}
594
Hans Wippel3b2dec22018-05-18 09:34:18 +0200595/* CLC handshake during connect */
Hans Wippelc758dfd2018-06-28 19:05:09 +0200596static int smc_connect_clc(struct smc_sock *smc, int smc_type,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200597 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200598 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200599{
600 int rc = 0;
601
602 /* do inband token exchange */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200603 rc = smc_clc_send_proposal(smc, smc_type, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200604 if (rc)
605 return rc;
606 /* receive SMC Accept CLC message */
Ursula Braun2b59f582018-11-22 10:26:39 +0100607 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT,
608 CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200609}
610
Ursula Brauna046d572017-01-09 16:55:16 +0100611/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200612static int smc_connect_rdma(struct smc_sock *smc,
613 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200614 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200615{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200616 struct smc_link *link;
617 int reason_code = 0;
618
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200619 ini->is_smcd = false;
620 ini->ib_lcl = &aclc->lcl;
621 ini->ib_clcqpn = ntoh24(aclc->qpn);
622 ini->srv_first_contact = aclc->hdr.flag;
623
Hans Wippel72a36a82019-02-07 15:56:18 +0100624 mutex_lock(&smc_client_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200625 reason_code = smc_conn_create(smc, ini);
626 if (reason_code) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100627 mutex_unlock(&smc_client_lgr_pending);
628 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200629 }
Karsten Graul387707f2020-04-29 17:10:40 +0200630 link = smc->conn.lnk;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200631
632 smc_conn_save_peer_info(smc, aclc);
633
634 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200635 if (smc_buf_create(smc, false))
Karsten Graul7a627252019-04-12 12:57:30 +0200636 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
637 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200638
Karsten Graul7a627252019-04-12 12:57:30 +0200639 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200640 smc_link_save_peer_info(link, aclc);
641
Karsten Graule07d31d2020-04-29 17:10:42 +0200642 if (smc_rmb_rtoken_handling(&smc->conn, link, aclc))
Karsten Graul603cc142018-07-25 16:35:32 +0200643 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
Karsten Graul7a627252019-04-12 12:57:30 +0200644 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200645
646 smc_close_init(smc);
647 smc_rx_init(smc);
648
Karsten Graul7a627252019-04-12 12:57:30 +0200649 if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200650 if (smc_ib_ready_link(link))
Karsten Graul603cc142018-07-25 16:35:32 +0200651 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
Karsten Graul7a627252019-04-12 12:57:30 +0200652 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200653 } else {
Karsten Graulb9247542020-04-29 17:10:41 +0200654 if (smcr_lgr_reg_rmbs(smc->conn.lgr, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +0200655 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
Karsten Graul7a627252019-04-12 12:57:30 +0200656 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200657 }
658 smc_rmb_sync_sg_for_device(&smc->conn);
659
660 reason_code = smc_clc_send_confirm(smc);
661 if (reason_code)
Karsten Graul7a627252019-04-12 12:57:30 +0200662 return smc_connect_abort(smc, reason_code,
663 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200664
665 smc_tx_init(smc);
666
Karsten Graul7a627252019-04-12 12:57:30 +0200667 if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200668 /* QP confirmation over RoCE fabric */
Karsten Graulb9247542020-04-29 17:10:41 +0200669 reason_code = smcr_clnt_conf_first_link(smc);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200670 if (reason_code)
671 return smc_connect_abort(smc, reason_code,
Karsten Graul7a627252019-04-12 12:57:30 +0200672 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200673 }
Hans Wippel72a36a82019-02-07 15:56:18 +0100674 mutex_unlock(&smc_client_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200675
676 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200677 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200678 if (smc->sk.sk_state == SMC_INIT)
679 smc->sk.sk_state = SMC_ACTIVE;
680
681 return 0;
682}
683
Hans Wippel41349842018-06-28 19:05:11 +0200684/* setup for ISM connection of client */
685static int smc_connect_ism(struct smc_sock *smc,
686 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200687 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200688{
Hans Wippel41349842018-06-28 19:05:11 +0200689 int rc = 0;
690
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200691 ini->is_smcd = true;
692 ini->ism_gid = aclc->gid;
693 ini->srv_first_contact = aclc->hdr.flag;
694
Hans Wippel72a36a82019-02-07 15:56:18 +0100695 /* there is only one lgr role for SMC-D; use server lock */
696 mutex_lock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200697 rc = smc_conn_create(smc, ini);
698 if (rc) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100699 mutex_unlock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200700 return rc;
Hans Wippel72a36a82019-02-07 15:56:18 +0100701 }
Hans Wippel41349842018-06-28 19:05:11 +0200702
703 /* Create send and receive buffers */
704 if (smc_buf_create(smc, true))
Karsten Graul7a627252019-04-12 12:57:30 +0200705 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
706 ini->cln_first_contact);
Hans Wippel41349842018-06-28 19:05:11 +0200707
708 smc_conn_save_peer_info(smc, aclc);
709 smc_close_init(smc);
710 smc_rx_init(smc);
711 smc_tx_init(smc);
712
713 rc = smc_clc_send_confirm(smc);
714 if (rc)
Karsten Graul7a627252019-04-12 12:57:30 +0200715 return smc_connect_abort(smc, rc, ini->cln_first_contact);
Hans Wippel72a36a82019-02-07 15:56:18 +0100716 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel41349842018-06-28 19:05:11 +0200717
718 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200719 smc->connect_nonblock = 0;
Hans Wippel41349842018-06-28 19:05:11 +0200720 if (smc->sk.sk_state == SMC_INIT)
721 smc->sk.sk_state = SMC_ACTIVE;
722
723 return 0;
724}
725
Hans Wippel3b2dec22018-05-18 09:34:18 +0200726/* perform steps before actually connecting */
727static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100728{
Hans Wippel41349842018-06-28 19:05:11 +0200729 bool ism_supported = false, rdma_supported = false;
Ursula Brauna046d572017-01-09 16:55:16 +0100730 struct smc_clc_msg_accept_confirm aclc;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200731 struct smc_init_info ini = {0};
Hans Wippel41349842018-06-28 19:05:11 +0200732 int smc_type;
Ursula Brauna046d572017-01-09 16:55:16 +0100733 int rc = 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100734
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200735 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +0200736 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200737
Hans Wippel3b2dec22018-05-18 09:34:18 +0200738 /* if peer has not signalled SMC-capability, fall back */
739 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +0200740 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200741
Ursula Brauna046d572017-01-09 16:55:16 +0100742 /* IPSec connections opt out of SMC-R optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200743 if (using_ipsec(smc))
744 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
Ursula Brauna046d572017-01-09 16:55:16 +0100745
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200746 /* get vlan id from IP device */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200747 if (smc_vlan_by_tcpsk(smc->clcsock, &ini))
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200748 return smc_connect_decline_fallback(smc,
749 SMC_CLC_DECL_GETVLANERR);
Hans Wippel41349842018-06-28 19:05:11 +0200750
751 /* check if there is an ism device available */
Karsten Graul228bae02019-04-12 12:57:28 +0200752 if (!smc_find_ism_device(smc, &ini) &&
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200753 !smc_connect_ism_vlan_setup(smc, &ini)) {
Hans Wippel41349842018-06-28 19:05:11 +0200754 /* ISM is supported for this connection */
755 ism_supported = true;
756 smc_type = SMC_TYPE_D;
757 }
758
759 /* check if there is a rdma device available */
Karsten Graul228bae02019-04-12 12:57:28 +0200760 if (!smc_find_rdma_device(smc, &ini)) {
Hans Wippel41349842018-06-28 19:05:11 +0200761 /* RDMA is supported for this connection */
762 rdma_supported = true;
763 if (ism_supported)
764 smc_type = SMC_TYPE_B; /* both */
765 else
766 smc_type = SMC_TYPE_R; /* only RDMA */
767 }
768
769 /* if neither ISM nor RDMA are supported, fallback */
770 if (!rdma_supported && !ism_supported)
Karsten Graul603cc142018-07-25 16:35:32 +0200771 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV);
Ursula Brauna046d572017-01-09 16:55:16 +0100772
Hans Wippel3b2dec22018-05-18 09:34:18 +0200773 /* perform CLC handshake */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200774 rc = smc_connect_clc(smc, smc_type, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200775 if (rc) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200776 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200777 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200778 }
Ursula Brauna046d572017-01-09 16:55:16 +0100779
Hans Wippel41349842018-06-28 19:05:11 +0200780 /* depending on previous steps, connect using rdma or ism */
781 if (rdma_supported && aclc.hdr.path == SMC_TYPE_R)
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200782 rc = smc_connect_rdma(smc, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200783 else if (ism_supported && aclc.hdr.path == SMC_TYPE_D)
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200784 rc = smc_connect_ism(smc, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200785 else
Karsten Graul603cc142018-07-25 16:35:32 +0200786 rc = SMC_CLC_DECL_MODEUNSUPP;
Hans Wippel41349842018-06-28 19:05:11 +0200787 if (rc) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200788 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200789 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200790 }
Ursula Brauna046d572017-01-09 16:55:16 +0100791
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200792 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200793 return 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100794}
795
Ursula Braun24ac3a02018-06-27 17:59:50 +0200796static void smc_connect_work(struct work_struct *work)
797{
798 struct smc_sock *smc = container_of(work, struct smc_sock,
799 connect_work);
Ursula Braun50717a32019-04-12 12:57:23 +0200800 long timeo = smc->sk.sk_sndtimeo;
801 int rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200802
Ursula Braun50717a32019-04-12 12:57:23 +0200803 if (!timeo)
804 timeo = MAX_SCHEDULE_TIMEOUT;
805 lock_sock(smc->clcsock->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200806 if (smc->clcsock->sk->sk_err) {
807 smc->sk.sk_err = smc->clcsock->sk->sk_err;
Ursula Braun50717a32019-04-12 12:57:23 +0200808 } else if ((1 << smc->clcsock->sk->sk_state) &
809 (TCPF_SYN_SENT | TCP_SYN_RECV)) {
810 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
811 if ((rc == -EPIPE) &&
812 ((1 << smc->clcsock->sk->sk_state) &
813 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
814 rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200815 }
Ursula Braun50717a32019-04-12 12:57:23 +0200816 release_sock(smc->clcsock->sk);
817 lock_sock(&smc->sk);
818 if (rc != 0 || smc->sk.sk_err) {
819 smc->sk.sk_state = SMC_CLOSED;
820 if (rc == -EPIPE || rc == -EAGAIN)
821 smc->sk.sk_err = EPIPE;
822 else if (signal_pending(current))
823 smc->sk.sk_err = -sock_intr_errno(timeo);
Ursula Braun6d6dd522019-11-12 16:03:41 +0100824 sock_put(&smc->sk); /* passive closing */
Ursula Braun24ac3a02018-06-27 17:59:50 +0200825 goto out;
826 }
827
828 rc = __smc_connect(smc);
829 if (rc < 0)
830 smc->sk.sk_err = -rc;
831
832out:
Ursula Braun07603b22019-04-11 11:17:32 +0200833 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
834 if (smc->sk.sk_err) {
835 smc->sk.sk_state_change(&smc->sk);
836 } else { /* allow polling before and after fallback decision */
837 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
838 smc->sk.sk_write_space(&smc->sk);
839 }
840 }
Ursula Braun24ac3a02018-06-27 17:59:50 +0200841 release_sock(&smc->sk);
842}
843
Ursula Braunac713872017-01-09 16:55:13 +0100844static int smc_connect(struct socket *sock, struct sockaddr *addr,
845 int alen, int flags)
846{
847 struct sock *sk = sock->sk;
848 struct smc_sock *smc;
849 int rc = -EINVAL;
850
851 smc = smc_sk(sk);
852
853 /* separate smc parameter checking to be safe */
854 if (alen < sizeof(addr->sa_family))
855 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100856 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100857 goto out_err;
858
859 lock_sock(sk);
860 switch (sk->sk_state) {
861 default:
862 goto out;
863 case SMC_ACTIVE:
864 rc = -EISCONN;
865 goto out;
866 case SMC_INIT:
867 rc = 0;
868 break;
869 }
870
871 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200872 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun50717a32019-04-12 12:57:23 +0200873 if (smc->connect_nonblock) {
874 rc = -EALREADY;
875 goto out;
876 }
877 rc = kernel_connect(smc->clcsock, addr, alen, flags);
878 if (rc && rc != -EINPROGRESS)
879 goto out;
Ursula Braun301428e2019-10-29 12:41:26 +0100880
881 sock_hold(&smc->sk); /* sock put in passive closing */
Ursula Braun86434742019-12-12 22:35:58 +0100882 if (smc->use_fallback)
883 goto out;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200884 if (flags & O_NONBLOCK) {
Ursula Braun50717a32019-04-12 12:57:23 +0200885 if (schedule_work(&smc->connect_work))
886 smc->connect_nonblock = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200887 rc = -EINPROGRESS;
888 } else {
Ursula Braun24ac3a02018-06-27 17:59:50 +0200889 rc = __smc_connect(smc);
890 if (rc < 0)
891 goto out;
892 else
893 rc = 0; /* success cases including fallback */
894 }
Ursula Braunac713872017-01-09 16:55:13 +0100895
896out:
897 release_sock(sk);
898out_err:
899 return rc;
900}
901
902static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
903{
Ursula Braun3163c502018-01-24 10:28:12 +0100904 struct socket *new_clcsock = NULL;
905 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100906 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -0800907 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +0100908
Ursula Braun3163c502018-01-24 10:28:12 +0100909 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100910 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100911 if (!new_sk) {
912 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100913 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100914 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100915 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100916 goto out;
917 }
918 *new_smc = smc_sk(new_sk);
919
Myungho Jung78abe3d2018-12-18 09:02:25 -0800920 mutex_lock(&lsmc->clcsock_release_lock);
921 if (lsmc->clcsock)
922 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
923 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +0100924 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100925 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100926 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100927 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +0200928 new_sk->sk_prot->unhash(new_sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100929 if (new_clcsock)
930 sock_release(new_clcsock);
931 new_sk->sk_state = SMC_CLOSED;
932 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun51f1de72018-01-26 09:28:48 +0100933 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100934 *new_smc = NULL;
935 goto out;
936 }
937
938 (*new_smc)->clcsock = new_clcsock;
939out:
940 return rc;
941}
942
Ursula Brauna046d572017-01-09 16:55:16 +0100943/* add a just created sock to the accept queue of the listen sock as
944 * candidate for a following socket accept call from user space
945 */
946static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
947{
948 struct smc_sock *par = smc_sk(parent);
949
Ursula Braun51f1de72018-01-26 09:28:48 +0100950 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100951 spin_lock(&par->accept_q_lock);
952 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
953 spin_unlock(&par->accept_q_lock);
954 sk_acceptq_added(parent);
955}
956
957/* remove a socket from the accept queue of its parental listening socket */
958static void smc_accept_unlink(struct sock *sk)
959{
960 struct smc_sock *par = smc_sk(sk)->listen_smc;
961
962 spin_lock(&par->accept_q_lock);
963 list_del_init(&smc_sk(sk)->accept_q);
964 spin_unlock(&par->accept_q_lock);
965 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100966 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100967}
968
969/* remove a sock from the accept queue to bind it to a new socket created
970 * for a socket accept call from user space
971 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100972struct sock *smc_accept_dequeue(struct sock *parent,
973 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100974{
975 struct smc_sock *isk, *n;
976 struct sock *new_sk;
977
978 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
979 new_sk = (struct sock *)isk;
980
981 smc_accept_unlink(new_sk);
982 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +0200983 new_sk->sk_prot->unhash(new_sk);
Ursula Braun127f4972018-01-26 09:28:49 +0100984 if (isk->clcsock) {
985 sock_release(isk->clcsock);
986 isk->clcsock = NULL;
987 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100988 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +0100989 continue;
990 }
Ursula Braun07603b22019-04-11 11:17:32 +0200991 if (new_sock) {
Ursula Brauna046d572017-01-09 16:55:16 +0100992 sock_graft(new_sk, new_sock);
Ursula Braun07603b22019-04-11 11:17:32 +0200993 if (isk->use_fallback) {
994 smc_sk(new_sk)->clcsock->file = new_sock->file;
995 isk->clcsock->file->private_data = isk->clcsock;
996 }
997 }
Ursula Brauna046d572017-01-09 16:55:16 +0100998 return new_sk;
999 }
1000 return NULL;
1001}
1002
1003/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +01001004void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +01001005{
1006 struct smc_sock *smc = smc_sk(sk);
1007
Ursula Braun81cf4f42019-10-21 16:13:15 +02001008 sock_hold(sk); /* sock_put below */
Ursula Braunb38d7322017-01-09 16:55:25 +01001009 lock_sock(sk);
1010 if (!sk->sk_lingertime)
1011 /* wait for peer closing */
1012 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun39f41f32019-06-27 15:04:52 +02001013 __smc_release(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +01001014 release_sock(sk);
Ursula Braun81cf4f42019-10-21 16:13:15 +02001015 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +01001016 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +01001017}
1018
Karsten Graulb9247542020-04-29 17:10:41 +02001019static int smcr_serv_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001020{
Karsten Graul387707f2020-04-29 17:10:40 +02001021 struct smc_link *link = smc->conn.lnk;
Karsten Graul4667bb42020-04-30 15:55:42 +02001022 struct smc_llc_qentry *qentry;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001023 int rc;
1024
Karsten Graul4667bb42020-04-30 15:55:42 +02001025 link->lgr->type = SMC_LGR_SINGLE;
1026
Karsten Graulb9247542020-04-29 17:10:41 +02001027 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +02001028 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +02001029
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001030 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +02001031 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001032 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001033 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001034
1035 /* receive CONFIRM LINK response from client over the RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001036 qentry = smc_llc_wait(link->lgr, link, SMC_LLC_WAIT_TIME,
1037 SMC_LLC_CONFIRM_LINK);
1038 if (!qentry) {
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001039 struct smc_clc_msg_decline dclc;
1040
1041 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001042 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001043 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001044 }
Karsten Graul4667bb42020-04-30 15:55:42 +02001045 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_RESP);
1046 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
1047 if (rc)
Karsten Graul75d320d2018-03-01 13:51:31 +01001048 return SMC_CLC_DECL_RMBE_EC;
1049
Karsten Graul4667bb42020-04-30 15:55:42 +02001050 /* confirm_rkey is implicit on 1st contact */
1051 smc->conn.rmb_desc->is_conf_rkey = true;
Karsten Graul52bedf32018-03-01 13:51:32 +01001052
Karsten Graul00a049c2020-04-29 17:10:49 +02001053 smc_llc_link_active(link);
Karsten Graul52bedf32018-03-01 13:51:32 +01001054
Karsten Graul4667bb42020-04-30 15:55:42 +02001055 /* initial contact - try to establish second link */
1056 /* tbd: call smc_llc_srv_add_link(link); */
Karsten Graul75d320d2018-03-01 13:51:31 +01001057 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001058}
1059
Hans Wippel3b2dec22018-05-18 09:34:18 +02001060/* listen worker: finish */
1061static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001062{
Ursula Brauna046d572017-01-09 16:55:16 +01001063 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001064 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001065
Ursula Brauna046d572017-01-09 16:55:16 +01001066 if (lsmc->sk.sk_state == SMC_LISTEN) {
Karsten Graulfd577702019-04-11 11:17:30 +02001067 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001068 smc_accept_enqueue(&lsmc->sk, newsmcsk);
Karsten Graulfd577702019-04-11 11:17:30 +02001069 release_sock(&lsmc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001070 } else { /* no longer listening */
1071 smc_close_non_accepted(newsmcsk);
1072 }
Ursula Brauna046d572017-01-09 16:55:16 +01001073
1074 /* Wake up accept */
1075 lsmc->sk.sk_data_ready(&lsmc->sk);
1076 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001077}
Ursula Brauna046d572017-01-09 16:55:16 +01001078
Hans Wippel3b2dec22018-05-18 09:34:18 +02001079/* listen worker: finish in state connected */
1080static void smc_listen_out_connected(struct smc_sock *new_smc)
1081{
1082 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001083
Hans Wippel3b2dec22018-05-18 09:34:18 +02001084 sk_refcnt_debug_inc(newsmcsk);
1085 if (newsmcsk->sk_state == SMC_INIT)
1086 newsmcsk->sk_state = SMC_ACTIVE;
1087
1088 smc_listen_out(new_smc);
1089}
1090
1091/* listen worker: finish in error state */
1092static void smc_listen_out_err(struct smc_sock *new_smc)
1093{
1094 struct sock *newsmcsk = &new_smc->sk;
1095
Ursula Braun51f1de72018-01-26 09:28:48 +01001096 if (newsmcsk->sk_state == SMC_INIT)
1097 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001098 newsmcsk->sk_state = SMC_CLOSED;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001099
1100 smc_listen_out(new_smc);
1101}
1102
1103/* listen worker: decline and fall back if possible */
1104static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1105 int local_contact)
1106{
1107 /* RDMA setup failed, switch back to TCP */
1108 if (local_contact == SMC_FIRST_CONTACT)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001109 smc_lgr_cleanup_early(&new_smc->conn);
1110 else
1111 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001112 if (reason_code < 0) { /* error, no fallback possible */
1113 smc_listen_out_err(new_smc);
1114 return;
1115 }
Ursula Braun07603b22019-04-11 11:17:32 +02001116 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001117 new_smc->fallback_rsn = reason_code;
1118 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001119 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1120 smc_listen_out_err(new_smc);
1121 return;
1122 }
1123 }
1124 smc_listen_out_connected(new_smc);
1125}
1126
1127/* listen worker: check prefixes */
Karsten Graul59886692019-04-12 12:57:25 +02001128static int smc_listen_prfx_check(struct smc_sock *new_smc,
Hans Wippel3b2dec22018-05-18 09:34:18 +02001129 struct smc_clc_msg_proposal *pclc)
1130{
1131 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1132 struct socket *newclcsock = new_smc->clcsock;
1133
1134 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1135 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
Karsten Graul59886692019-04-12 12:57:25 +02001136 return SMC_CLC_DECL_DIFFPREFIX;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001137
1138 return 0;
1139}
1140
1141/* listen worker: initialize connection and buffers */
1142static int smc_listen_rdma_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001143 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001144{
Karsten Graul7a627252019-04-12 12:57:30 +02001145 int rc;
1146
Hans Wippel3b2dec22018-05-18 09:34:18 +02001147 /* allocate connection / link group */
Karsten Graul7a627252019-04-12 12:57:30 +02001148 rc = smc_conn_create(new_smc, ini);
1149 if (rc)
1150 return rc;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001151
1152 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001153 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001154 return SMC_CLC_DECL_MEM;
1155
1156 return 0;
1157}
1158
Hans Wippel41349842018-06-28 19:05:11 +02001159/* listen worker: initialize connection and buffers for SMC-D */
1160static int smc_listen_ism_init(struct smc_sock *new_smc,
1161 struct smc_clc_msg_proposal *pclc,
Karsten Graul7a627252019-04-12 12:57:30 +02001162 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +02001163{
1164 struct smc_clc_msg_smcd *pclc_smcd;
Karsten Graul7a627252019-04-12 12:57:30 +02001165 int rc;
Hans Wippel41349842018-06-28 19:05:11 +02001166
1167 pclc_smcd = smc_get_clc_msg_smcd(pclc);
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001168 ini->ism_gid = pclc_smcd->gid;
Karsten Graul7a627252019-04-12 12:57:30 +02001169 rc = smc_conn_create(new_smc, ini);
1170 if (rc)
1171 return rc;
Hans Wippel41349842018-06-28 19:05:11 +02001172
1173 /* Check if peer can be reached via ISM device */
1174 if (smc_ism_cantalk(new_smc->conn.lgr->peer_gid,
1175 new_smc->conn.lgr->vlan_id,
1176 new_smc->conn.lgr->smcd)) {
Karsten Graul7a627252019-04-12 12:57:30 +02001177 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001178 smc_lgr_cleanup_early(&new_smc->conn);
1179 else
1180 smc_conn_free(&new_smc->conn);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001181 return SMC_CLC_DECL_SMCDNOTALK;
Hans Wippel41349842018-06-28 19:05:11 +02001182 }
1183
1184 /* Create send and receive buffers */
1185 if (smc_buf_create(new_smc, true)) {
Karsten Graul7a627252019-04-12 12:57:30 +02001186 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001187 smc_lgr_cleanup_early(&new_smc->conn);
1188 else
1189 smc_conn_free(&new_smc->conn);
Hans Wippel41349842018-06-28 19:05:11 +02001190 return SMC_CLC_DECL_MEM;
1191 }
1192
1193 return 0;
1194}
1195
Hans Wippel3b2dec22018-05-18 09:34:18 +02001196/* listen worker: register buffers */
1197static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
1198{
Karsten Graulb9247542020-04-29 17:10:41 +02001199 struct smc_connection *conn = &new_smc->conn;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001200
1201 if (local_contact != SMC_FIRST_CONTACT) {
Karsten Graulb9247542020-04-29 17:10:41 +02001202 if (smcr_lgr_reg_rmbs(conn->lgr, conn->rmb_desc))
Karsten Graulc7674c02018-11-22 10:26:43 +01001203 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001204 }
1205 smc_rmb_sync_sg_for_device(&new_smc->conn);
1206
1207 return 0;
1208}
1209
1210/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001211static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1212 struct smc_clc_msg_accept_confirm *cclc,
1213 int local_contact)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001214{
Karsten Graul387707f2020-04-29 17:10:40 +02001215 struct smc_link *link = new_smc->conn.lnk;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001216 int reason_code = 0;
1217
1218 if (local_contact == SMC_FIRST_CONTACT)
1219 smc_link_save_peer_info(link, cclc);
1220
Karsten Graule07d31d2020-04-29 17:10:42 +02001221 if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001222 reason_code = SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001223 goto decline;
1224 }
1225
1226 if (local_contact == SMC_FIRST_CONTACT) {
1227 if (smc_ib_ready_link(link)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001228 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001229 goto decline;
1230 }
1231 /* QP confirmation over RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001232 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
Karsten Graulb9247542020-04-29 17:10:41 +02001233 reason_code = smcr_serv_conf_first_link(new_smc);
Karsten Graul4667bb42020-04-30 15:55:42 +02001234 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001235 if (reason_code)
1236 goto decline;
1237 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001238 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001239
1240decline:
Hans Wippel3b2dec22018-05-18 09:34:18 +02001241 smc_listen_decline(new_smc, reason_code, local_contact);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001242 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001243}
1244
1245/* setup for RDMA connection of server */
1246static void smc_listen_work(struct work_struct *work)
1247{
1248 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1249 smc_listen_work);
1250 struct socket *newclcsock = new_smc->clcsock;
1251 struct smc_clc_msg_accept_confirm cclc;
1252 struct smc_clc_msg_proposal *pclc;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001253 struct smc_init_info ini = {0};
Hans Wippel41349842018-06-28 19:05:11 +02001254 bool ism_supported = false;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001255 u8 buf[SMC_CLC_MAX_LEN];
Hans Wippel3b2dec22018-05-18 09:34:18 +02001256 int rc = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001257
Karsten Graulfd577702019-04-11 11:17:30 +02001258 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1259 return smc_listen_out_err(new_smc);
1260
Hans Wippel3b2dec22018-05-18 09:34:18 +02001261 if (new_smc->use_fallback) {
1262 smc_listen_out_connected(new_smc);
1263 return;
1264 }
1265
1266 /* check if peer is smc capable */
1267 if (!tcp_sk(newclcsock->sk)->syn_smc) {
Ursula Braun07603b22019-04-11 11:17:32 +02001268 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001269 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001270 smc_listen_out_connected(new_smc);
1271 return;
1272 }
1273
1274 /* do inband token exchange -
1275 * wait for and receive SMC Proposal CLC message
1276 */
1277 pclc = (struct smc_clc_msg_proposal *)&buf;
Karsten Graul228bae02019-04-12 12:57:28 +02001278 rc = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
1279 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001280 if (rc)
1281 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001282
1283 /* IPSec connections opt out of SMC-R optimizations */
1284 if (using_ipsec(new_smc)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001285 rc = SMC_CLC_DECL_IPSEC;
1286 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001287 }
1288
Karsten Graul59886692019-04-12 12:57:25 +02001289 /* check for matching IP prefix and subnet length */
1290 rc = smc_listen_prfx_check(new_smc, pclc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001291 if (rc)
1292 goto out_decl;
Karsten Graul59886692019-04-12 12:57:25 +02001293
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001294 /* get vlan id from IP device */
1295 if (smc_vlan_by_tcpsk(new_smc->clcsock, &ini)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001296 rc = SMC_CLC_DECL_GETVLANERR;
1297 goto out_decl;
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001298 }
1299
Hans Wippel72a36a82019-02-07 15:56:18 +01001300 mutex_lock(&smc_server_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001301 smc_close_init(new_smc);
1302 smc_rx_init(new_smc);
1303 smc_tx_init(new_smc);
1304
Hans Wippel41349842018-06-28 19:05:11 +02001305 /* check if ISM is available */
Karsten Graul9aa68d22019-04-12 12:57:29 +02001306 if (pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) {
1307 ini.is_smcd = true; /* prepare ISM check */
1308 rc = smc_find_ism_device(new_smc, &ini);
1309 if (!rc)
Karsten Graul7a627252019-04-12 12:57:30 +02001310 rc = smc_listen_ism_init(new_smc, pclc, &ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001311 if (!rc)
1312 ism_supported = true;
1313 else if (pclc->hdr.path == SMC_TYPE_D)
1314 goto out_unlock; /* skip RDMA and decline */
1315 }
1316
1317 /* check if RDMA is available */
1318 if (!ism_supported) { /* SMC_TYPE_R or SMC_TYPE_B */
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001319 /* prepare RDMA check */
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001320 ini.is_smcd = false;
Ursula Braunca5f8d22019-10-23 15:44:06 +02001321 ini.ism_dev = NULL;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001322 ini.ib_lcl = &pclc->lcl;
Karsten Graul9aa68d22019-04-12 12:57:29 +02001323 rc = smc_find_rdma_device(new_smc, &ini);
1324 if (rc) {
1325 /* no RDMA device found */
1326 if (pclc->hdr.path == SMC_TYPE_B)
1327 /* neither ISM nor RDMA device found */
1328 rc = SMC_CLC_DECL_NOSMCDEV;
1329 goto out_unlock;
1330 }
Karsten Graul7a627252019-04-12 12:57:30 +02001331 rc = smc_listen_rdma_init(new_smc, &ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001332 if (rc)
1333 goto out_unlock;
Karsten Graul7a627252019-04-12 12:57:30 +02001334 rc = smc_listen_rdma_reg(new_smc, ini.cln_first_contact);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001335 if (rc)
1336 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001337 }
1338
1339 /* send SMC Accept CLC message */
Karsten Graul7a627252019-04-12 12:57:30 +02001340 rc = smc_clc_send_accept(new_smc, ini.cln_first_contact);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001341 if (rc)
1342 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001343
Hans Wippel62c71392019-02-07 15:56:17 +01001344 /* SMC-D does not need this lock any more */
1345 if (ism_supported)
Hans Wippel72a36a82019-02-07 15:56:18 +01001346 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001347
Hans Wippel3b2dec22018-05-18 09:34:18 +02001348 /* receive SMC Confirm CLC message */
Karsten Graul228bae02019-04-12 12:57:28 +02001349 rc = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
1350 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
1351 if (rc) {
Hans Wippel62c71392019-02-07 15:56:17 +01001352 if (!ism_supported)
Karsten Graul9aa68d22019-04-12 12:57:29 +02001353 goto out_unlock;
1354 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001355 }
1356
1357 /* finish worker */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001358 if (!ism_supported) {
Karsten Graul7a627252019-04-12 12:57:30 +02001359 rc = smc_listen_rdma_finish(new_smc, &cclc,
1360 ini.cln_first_contact);
Hans Wippel72a36a82019-02-07 15:56:18 +01001361 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001362 if (rc)
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001363 return;
1364 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001365 smc_conn_save_peer_info(new_smc, &cclc);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001366 smc_listen_out_connected(new_smc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001367 return;
1368
1369out_unlock:
1370 mutex_unlock(&smc_server_lgr_pending);
1371out_decl:
Karsten Graul7a627252019-04-12 12:57:30 +02001372 smc_listen_decline(new_smc, rc, ini.cln_first_contact);
Ursula Brauna046d572017-01-09 16:55:16 +01001373}
1374
1375static void smc_tcp_listen_work(struct work_struct *work)
1376{
1377 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1378 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001379 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001380 struct smc_sock *new_smc;
1381 int rc = 0;
1382
Ursula Braun3163c502018-01-24 10:28:12 +01001383 lock_sock(lsk);
1384 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001385 rc = smc_clcsock_accept(lsmc, &new_smc);
1386 if (rc)
1387 goto out;
1388 if (!new_smc)
1389 continue;
1390
1391 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001392 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001393 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001394 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001395 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1396 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001397 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1398 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001399 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1400 if (!schedule_work(&new_smc->smc_listen_work))
1401 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001402 }
1403
1404out:
Ursula Braun3163c502018-01-24 10:28:12 +01001405 release_sock(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001406 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +01001407}
1408
Ursula Braunac713872017-01-09 16:55:13 +01001409static int smc_listen(struct socket *sock, int backlog)
1410{
1411 struct sock *sk = sock->sk;
1412 struct smc_sock *smc;
1413 int rc;
1414
1415 smc = smc_sk(sk);
1416 lock_sock(sk);
1417
1418 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +02001419 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
1420 smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +01001421 goto out;
1422
1423 rc = 0;
1424 if (sk->sk_state == SMC_LISTEN) {
1425 sk->sk_max_ack_backlog = backlog;
1426 goto out;
1427 }
1428 /* some socket options are handled in core, so we could not apply
1429 * them to the clc socket -- copy smc socket options to clc socket
1430 */
1431 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001432 if (!smc->use_fallback)
1433 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001434
1435 rc = kernel_listen(smc->clcsock, backlog);
1436 if (rc)
1437 goto out;
1438 sk->sk_max_ack_backlog = backlog;
1439 sk->sk_ack_backlog = 0;
1440 sk->sk_state = SMC_LISTEN;
Ursula Braun51f1de72018-01-26 09:28:48 +01001441 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1442 if (!schedule_work(&smc->tcp_listen_work))
1443 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001444
1445out:
1446 release_sock(sk);
1447 return rc;
1448}
1449
1450static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001451 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001452{
Ursula Brauna046d572017-01-09 16:55:16 +01001453 struct sock *sk = sock->sk, *nsk;
1454 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001455 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001456 long timeo;
1457 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001458
1459 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001460 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001461 lock_sock(sk);
1462
1463 if (lsmc->sk.sk_state != SMC_LISTEN) {
1464 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001465 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001466 goto out;
1467 }
1468
Ursula Brauna046d572017-01-09 16:55:16 +01001469 /* Wait for an incoming connection */
1470 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1471 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1472 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1473 set_current_state(TASK_INTERRUPTIBLE);
1474 if (!timeo) {
1475 rc = -EAGAIN;
1476 break;
1477 }
1478 release_sock(sk);
1479 timeo = schedule_timeout(timeo);
1480 /* wakeup by sk_data_ready in smc_listen_work() */
1481 sched_annotate_sleep();
1482 lock_sock(sk);
1483 if (signal_pending(current)) {
1484 rc = sock_intr_errno(timeo);
1485 break;
1486 }
1487 }
1488 set_current_state(TASK_RUNNING);
1489 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001490
Ursula Brauna046d572017-01-09 16:55:16 +01001491 if (!rc)
1492 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001493 release_sock(sk);
1494 if (rc)
1495 goto out;
1496
1497 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1498 /* wait till data arrives on the socket */
1499 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1500 MSEC_PER_SEC);
1501 if (smc_sk(nsk)->use_fallback) {
1502 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1503
1504 lock_sock(clcsk);
1505 if (skb_queue_empty(&clcsk->sk_receive_queue))
1506 sk_wait_data(clcsk, &timeo, NULL);
1507 release_sock(clcsk);
1508 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1509 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001510 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001511 release_sock(nsk);
1512 }
1513 }
Ursula Braunac713872017-01-09 16:55:13 +01001514
1515out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001516 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001517 return rc;
1518}
1519
1520static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001521 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001522{
1523 struct smc_sock *smc;
1524
Ursula Braunb38d7322017-01-09 16:55:25 +01001525 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1526 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001527 return -ENOTCONN;
1528
1529 smc = smc_sk(sock->sk);
1530
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001531 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001532}
1533
1534static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1535{
1536 struct sock *sk = sock->sk;
1537 struct smc_sock *smc;
1538 int rc = -EPIPE;
1539
1540 smc = smc_sk(sk);
1541 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001542 if ((sk->sk_state != SMC_ACTIVE) &&
1543 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1544 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001545 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001546
1547 if (msg->msg_flags & MSG_FASTOPEN) {
Ursula Brauncd206362019-08-02 10:47:50 +02001548 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001549 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001550 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001551 } else {
1552 rc = -EINVAL;
1553 goto out;
1554 }
1555 }
1556
Ursula Braunac713872017-01-09 16:55:13 +01001557 if (smc->use_fallback)
1558 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1559 else
Ursula Braune6727f32017-01-09 16:55:23 +01001560 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001561out:
1562 release_sock(sk);
1563 return rc;
1564}
1565
1566static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1567 int flags)
1568{
1569 struct sock *sk = sock->sk;
1570 struct smc_sock *smc;
1571 int rc = -ENOTCONN;
1572
1573 smc = smc_sk(sk);
1574 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001575 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1576 /* socket was connected before, no more data to read */
1577 rc = 0;
1578 goto out;
1579 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001580 if ((sk->sk_state == SMC_INIT) ||
1581 (sk->sk_state == SMC_LISTEN) ||
1582 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001583 goto out;
1584
Ursula Braunb38d7322017-01-09 16:55:25 +01001585 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1586 rc = 0;
1587 goto out;
1588 }
1589
Stefan Raspl9014db22018-05-03 18:12:39 +02001590 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001591 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001592 } else {
1593 msg->msg_namelen = 0;
1594 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1595 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001596
Ursula Braunac713872017-01-09 16:55:13 +01001597out:
1598 release_sock(sk);
1599 return rc;
1600}
1601
Al Viroade994f2017-07-03 00:01:49 -04001602static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001603{
Ursula Braun8dce2782018-01-26 09:28:47 +01001604 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001605 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001606
Ursula Braun8dce2782018-01-26 09:28:47 +01001607 spin_lock(&isk->accept_q_lock);
1608 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001609 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001610 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001611
Ursula Braun8dce2782018-01-26 09:28:47 +01001612 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001613}
1614
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001615static __poll_t smc_poll(struct file *file, struct socket *sock,
1616 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01001617{
1618 struct sock *sk = sock->sk;
Ursula Braunac713872017-01-09 16:55:13 +01001619 struct smc_sock *smc;
Ursula Braun50717a32019-04-12 12:57:23 +02001620 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001621
Ursula Braun8dce2782018-01-26 09:28:47 +01001622 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001623 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001624
Ursula Braunac713872017-01-09 16:55:13 +01001625 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02001626 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01001627 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001628 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02001629 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braunac713872017-01-09 16:55:13 +01001630 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07001631 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02001632 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01001633 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001634 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001635 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1636 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001637 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001638 if (sk->sk_state == SMC_LISTEN) {
1639 /* woken up by sk_data_ready in smc_listen_work() */
Ursula Braun50717a32019-04-12 12:57:23 +02001640 mask |= smc_accept_poll(sk);
1641 } else if (smc->use_fallback) { /* as result of connect_work()*/
1642 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
1643 wait);
1644 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun8dce2782018-01-26 09:28:47 +01001645 } else {
Ursula Braun50717a32019-04-12 12:57:23 +02001646 if ((sk->sk_state != SMC_INIT &&
1647 atomic_read(&smc->conn.sndbuf_space)) ||
Ursula Braun8dce2782018-01-26 09:28:47 +01001648 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001649 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001650 } else {
1651 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1652 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1653 }
1654 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001655 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001656 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001657 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001658 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001659 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02001660 if (smc->conn.urg_state == SMC_URG_VALID)
1661 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01001662 }
Ursula Braunac713872017-01-09 16:55:13 +01001663 }
1664
1665 return mask;
1666}
1667
1668static int smc_shutdown(struct socket *sock, int how)
1669{
1670 struct sock *sk = sock->sk;
1671 struct smc_sock *smc;
1672 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001673 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001674
1675 smc = smc_sk(sk);
1676
1677 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001678 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001679
1680 lock_sock(sk);
1681
1682 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02001683 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01001684 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1685 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1686 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1687 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1688 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001689 goto out;
1690 if (smc->use_fallback) {
1691 rc = kernel_sock_shutdown(smc->clcsock, how);
1692 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1693 if (sk->sk_shutdown == SHUTDOWN_MASK)
1694 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001695 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001696 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001697 switch (how) {
1698 case SHUT_RDWR: /* shutdown in both directions */
1699 rc = smc_close_active(smc);
1700 break;
1701 case SHUT_WR:
1702 rc = smc_close_shutdown_write(smc);
1703 break;
1704 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001705 rc = 0;
1706 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001707 break;
1708 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001709 if (smc->clcsock)
1710 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001711 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1712 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001713
1714out:
1715 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001716 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001717}
1718
1719static int smc_setsockopt(struct socket *sock, int level, int optname,
1720 char __user *optval, unsigned int optlen)
1721{
1722 struct sock *sk = sock->sk;
1723 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001724 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001725
1726 smc = smc_sk(sk);
1727
1728 /* generic setsockopts reaching us here always apply to the
1729 * CLC socket
1730 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001731 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1732 optval, optlen);
1733 if (smc->clcsock->sk->sk_err) {
1734 sk->sk_err = smc->clcsock->sk->sk_err;
1735 sk->sk_error_report(sk);
1736 }
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001737
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001738 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00001739 return -EINVAL;
Ursula Braunac0107e2018-07-18 15:22:50 +02001740 if (get_user(val, (int __user *)optval))
1741 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001742
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001743 lock_sock(sk);
Ursula Braun86434742019-12-12 22:35:58 +01001744 if (rc || smc->use_fallback)
1745 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001746 switch (optname) {
1747 case TCP_ULP:
1748 case TCP_FASTOPEN:
1749 case TCP_FASTOPEN_CONNECT:
1750 case TCP_FASTOPEN_KEY:
1751 case TCP_FASTOPEN_NO_COOKIE:
1752 /* option not supported by SMC */
Ursula Braun8204df72019-11-15 12:39:30 +01001753 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001754 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001755 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001756 } else {
Ursula Braun86434742019-12-12 22:35:58 +01001757 rc = -EINVAL;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001758 }
1759 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001760 case TCP_NODELAY:
Ursula Braunf9cedf12019-08-02 10:16:38 +02001761 if (sk->sk_state != SMC_INIT &&
1762 sk->sk_state != SMC_LISTEN &&
1763 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01001764 if (val)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001765 mod_delayed_work(system_wq, &smc->conn.tx_work,
1766 0);
1767 }
1768 break;
1769 case TCP_CORK:
Ursula Braunf9cedf12019-08-02 10:16:38 +02001770 if (sk->sk_state != SMC_INIT &&
1771 sk->sk_state != SMC_LISTEN &&
1772 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01001773 if (!val)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001774 mod_delayed_work(system_wq, &smc->conn.tx_work,
1775 0);
1776 }
1777 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001778 case TCP_DEFER_ACCEPT:
1779 smc->sockopt_defer_accept = val;
1780 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001781 default:
1782 break;
1783 }
Ursula Braun86434742019-12-12 22:35:58 +01001784out:
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001785 release_sock(sk);
1786
1787 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001788}
1789
1790static int smc_getsockopt(struct socket *sock, int level, int optname,
1791 char __user *optval, int __user *optlen)
1792{
1793 struct smc_sock *smc;
1794
1795 smc = smc_sk(sock->sk);
1796 /* socket options apply to the CLC socket */
1797 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1798 optval, optlen);
1799}
1800
1801static int smc_ioctl(struct socket *sock, unsigned int cmd,
1802 unsigned long arg)
1803{
Stefan Rasplde8474e2018-05-23 16:38:11 +02001804 union smc_host_cursor cons, urg;
1805 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01001806 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001807 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001808
1809 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001810 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02001811 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02001812 if (smc->use_fallback) {
1813 if (!smc->clcsock) {
1814 release_sock(&smc->sk);
1815 return -EBADF;
1816 }
1817 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1818 release_sock(&smc->sk);
1819 return answ;
1820 }
Ursula Braun9b67e262018-05-02 16:56:46 +02001821 switch (cmd) {
1822 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02001823 if (smc->sk.sk_state == SMC_LISTEN) {
1824 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001825 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001826 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001827 if (smc->sk.sk_state == SMC_INIT ||
1828 smc->sk.sk_state == SMC_CLOSED)
1829 answ = 0;
1830 else
1831 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02001832 break;
1833 case SIOCOUTQ:
1834 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02001835 if (smc->sk.sk_state == SMC_LISTEN) {
1836 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001837 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001838 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001839 if (smc->sk.sk_state == SMC_INIT ||
1840 smc->sk.sk_state == SMC_CLOSED)
1841 answ = 0;
1842 else
1843 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02001844 atomic_read(&smc->conn.sndbuf_space);
1845 break;
1846 case SIOCOUTQNSD:
1847 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02001848 if (smc->sk.sk_state == SMC_LISTEN) {
1849 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001850 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001851 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001852 if (smc->sk.sk_state == SMC_INIT ||
1853 smc->sk.sk_state == SMC_CLOSED)
1854 answ = 0;
1855 else
1856 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02001857 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02001858 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02001859 if (smc->sk.sk_state == SMC_LISTEN) {
1860 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001861 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001862 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001863 if (smc->sk.sk_state == SMC_INIT ||
1864 smc->sk.sk_state == SMC_CLOSED) {
1865 answ = 0;
1866 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02001867 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1868 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001869 answ = smc_curs_diff(conn->rmb_desc->len,
1870 &cons, &urg) == 1;
1871 }
1872 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02001873 default:
Ursula Braun1992d992018-07-16 13:56:52 +02001874 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001875 return -ENOIOCTLCMD;
1876 }
Ursula Braun1992d992018-07-16 13:56:52 +02001877 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001878
1879 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001880}
1881
1882static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1883 int offset, size_t size, int flags)
1884{
1885 struct sock *sk = sock->sk;
1886 struct smc_sock *smc;
1887 int rc = -EPIPE;
1888
1889 smc = smc_sk(sk);
1890 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001891 if (sk->sk_state != SMC_ACTIVE) {
1892 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001893 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001894 }
1895 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001896 if (smc->use_fallback)
1897 rc = kernel_sendpage(smc->clcsock, page, offset,
1898 size, flags);
1899 else
1900 rc = sock_no_sendpage(sock, page, offset, size, flags);
1901
1902out:
Ursula Braunac713872017-01-09 16:55:13 +01001903 return rc;
1904}
1905
Stefan Raspl9014db22018-05-03 18:12:39 +02001906/* Map the affected portions of the rmbe into an spd, note the number of bytes
1907 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1908 * updates till whenever a respective page has been fully processed.
1909 * Note that subsequent recv() calls have to wait till all splice() processing
1910 * completed.
1911 */
Ursula Braunac713872017-01-09 16:55:13 +01001912static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1913 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001914 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001915{
1916 struct sock *sk = sock->sk;
1917 struct smc_sock *smc;
1918 int rc = -ENOTCONN;
1919
1920 smc = smc_sk(sk);
1921 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001922 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1923 /* socket was connected before, no more data to read */
1924 rc = 0;
1925 goto out;
1926 }
Stefan Raspl9014db22018-05-03 18:12:39 +02001927 if (sk->sk_state == SMC_INIT ||
1928 sk->sk_state == SMC_LISTEN ||
1929 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001930 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001931
1932 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1933 rc = 0;
1934 goto out;
1935 }
1936
Ursula Braunac713872017-01-09 16:55:13 +01001937 if (smc->use_fallback) {
1938 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1939 pipe, len, flags);
1940 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001941 if (*ppos) {
1942 rc = -ESPIPE;
1943 goto out;
1944 }
1945 if (flags & SPLICE_F_NONBLOCK)
1946 flags = MSG_DONTWAIT;
1947 else
1948 flags = 0;
1949 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001950 }
1951out:
1952 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001953
Ursula Braunac713872017-01-09 16:55:13 +01001954 return rc;
1955}
1956
1957/* must look like tcp */
1958static const struct proto_ops smc_sock_ops = {
1959 .family = PF_SMC,
1960 .owner = THIS_MODULE,
1961 .release = smc_release,
1962 .bind = smc_bind,
1963 .connect = smc_connect,
1964 .socketpair = sock_no_socketpair,
1965 .accept = smc_accept,
1966 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001967 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01001968 .ioctl = smc_ioctl,
1969 .listen = smc_listen,
1970 .shutdown = smc_shutdown,
1971 .setsockopt = smc_setsockopt,
1972 .getsockopt = smc_getsockopt,
1973 .sendmsg = smc_sendmsg,
1974 .recvmsg = smc_recvmsg,
1975 .mmap = sock_no_mmap,
1976 .sendpage = smc_sendpage,
1977 .splice_read = smc_splice_read,
1978};
1979
1980static int smc_create(struct net *net, struct socket *sock, int protocol,
1981 int kern)
1982{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001983 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001984 struct smc_sock *smc;
1985 struct sock *sk;
1986 int rc;
1987
1988 rc = -ESOCKTNOSUPPORT;
1989 if (sock->type != SOCK_STREAM)
1990 goto out;
1991
1992 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001993 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01001994 goto out;
1995
1996 rc = -ENOBUFS;
1997 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001998 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001999 if (!sk)
2000 goto out;
2001
2002 /* create internal TCP socket for CLC handshake and fallback */
2003 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01002004 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02002005 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002006 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
2007 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01002008 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01002009 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01002010 goto out;
2011 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01002012 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
2013 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01002014
2015out:
2016 return rc;
2017}
2018
2019static const struct net_proto_family smc_sock_family_ops = {
2020 .family = PF_SMC,
2021 .owner = THIS_MODULE,
2022 .create = smc_create,
2023};
2024
Hans Wippel64e28b52019-02-21 13:01:02 +01002025unsigned int smc_net_id;
2026
2027static __net_init int smc_net_init(struct net *net)
2028{
2029 return smc_pnet_net_init(net);
2030}
2031
2032static void __net_exit smc_net_exit(struct net *net)
2033{
2034 smc_pnet_net_exit(net);
2035}
2036
2037static struct pernet_operations smc_net_ops = {
2038 .init = smc_net_init,
2039 .exit = smc_net_exit,
2040 .id = &smc_net_id,
2041 .size = sizeof(struct smc_net),
2042};
2043
Ursula Braunac713872017-01-09 16:55:13 +01002044static int __init smc_init(void)
2045{
2046 int rc;
2047
Hans Wippel64e28b52019-02-21 13:01:02 +01002048 rc = register_pernet_subsys(&smc_net_ops);
2049 if (rc)
2050 return rc;
2051
Thomas Richter6812baa2017-01-09 16:55:15 +01002052 rc = smc_pnet_init();
2053 if (rc)
YueHaibing8c33bf12019-06-26 17:47:50 +02002054 goto out_pernet_subsys;
Thomas Richter6812baa2017-01-09 16:55:15 +01002055
Ursula Braun6dabd402019-11-16 17:47:29 +01002056 rc = smc_core_init();
2057 if (rc) {
2058 pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
2059 goto out_pnet;
2060 }
2061
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002062 rc = smc_llc_init();
2063 if (rc) {
2064 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002065 goto out_core;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002066 }
2067
Ursula Braun5f083182017-01-09 16:55:22 +01002068 rc = smc_cdc_init();
2069 if (rc) {
2070 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002071 goto out_core;
Ursula Braun5f083182017-01-09 16:55:22 +01002072 }
2073
Ursula Braunac713872017-01-09 16:55:13 +01002074 rc = proto_register(&smc_proto, 1);
2075 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01002076 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002077 goto out_core;
Ursula Braunac713872017-01-09 16:55:13 +01002078 }
2079
Karsten Graulaaa4d332018-03-16 15:06:41 +01002080 rc = proto_register(&smc_proto6, 1);
2081 if (rc) {
2082 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2083 goto out_proto;
2084 }
2085
Ursula Braunac713872017-01-09 16:55:13 +01002086 rc = sock_register(&smc_sock_family_ops);
2087 if (rc) {
2088 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002089 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01002090 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01002091 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002092 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01002093
Ursula Brauna4cf0442017-01-09 16:55:14 +01002094 rc = smc_ib_register_client();
2095 if (rc) {
2096 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2097 goto out_sock;
2098 }
2099
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002100 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002101 return 0;
2102
Ursula Brauna4cf0442017-01-09 16:55:14 +01002103out_sock:
2104 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002105out_proto6:
2106 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002107out_proto:
2108 proto_unregister(&smc_proto);
Ursula Braun6dabd402019-11-16 17:47:29 +01002109out_core:
2110 smc_core_exit();
Thomas Richter6812baa2017-01-09 16:55:15 +01002111out_pnet:
2112 smc_pnet_exit();
YueHaibing8c33bf12019-06-26 17:47:50 +02002113out_pernet_subsys:
2114 unregister_pernet_subsys(&smc_net_ops);
2115
Ursula Braunac713872017-01-09 16:55:13 +01002116 return rc;
2117}
2118
2119static void __exit smc_exit(void)
2120{
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002121 static_branch_disable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002122 sock_unregister(PF_SMC);
Ursula Braun6dabd402019-11-16 17:47:29 +01002123 smc_core_exit();
2124 smc_ib_unregister_client();
Karsten Graulaaa4d332018-03-16 15:06:41 +01002125 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002126 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002127 smc_pnet_exit();
Hans Wippel64e28b52019-02-21 13:01:02 +01002128 unregister_pernet_subsys(&smc_net_ops);
Ursula Braun4ead9c92019-11-16 17:47:31 +01002129 rcu_barrier();
Ursula Braunac713872017-01-09 16:55:13 +01002130}
2131
2132module_init(smc_init);
2133module_exit(smc_exit);
2134
2135MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2136MODULE_DESCRIPTION("smc socket address family");
2137MODULE_LICENSE("GPL");
2138MODULE_ALIAS_NETPROTO(PF_SMC);