blob: e39f6aedd3bd5c294bf1b28e4299e67daa9272de [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 Graul877ae5b2018-05-02 16:56:44 +0200384 struct net *net = sock_net(smc->clcsock->sk);
Karsten Graul387707f2020-04-29 17:10:40 +0200385 struct smc_link *link = smc->conn.lnk;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100386 int rest;
387 int rc;
388
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100389 /* receive CONFIRM LINK request from server over RoCE fabric */
390 rest = wait_for_completion_interruptible_timeout(
391 &link->llc_confirm,
392 SMC_LLC_WAIT_FIRST_TIME);
393 if (rest <= 0) {
394 struct smc_clc_msg_decline dclc;
395
396 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100397 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100398 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100399 }
400
Karsten Graul75d320d2018-03-01 13:51:31 +0100401 if (link->llc_confirm_rc)
402 return SMC_CLC_DECL_RMBE_EC;
403
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100404 rc = smc_ib_modify_qp_rts(link);
405 if (rc)
Karsten Graul603cc142018-07-25 16:35:32 +0200406 return SMC_CLC_DECL_ERR_RDYLNK;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100407
408 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200409
Karsten Graulb9247542020-04-29 17:10:41 +0200410 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200411 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200412
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100413 /* send CONFIRM LINK response over RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200414 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100415 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200416 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100417
Karsten Graul52bedf32018-03-01 13:51:32 +0100418 /* receive ADD LINK request from server over RoCE fabric */
419 rest = wait_for_completion_interruptible_timeout(&link->llc_add,
420 SMC_LLC_WAIT_TIME);
421 if (rest <= 0) {
422 struct smc_clc_msg_decline dclc;
423
424 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100425 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100426 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
Karsten Graul52bedf32018-03-01 13:51:32 +0100427 }
428
429 /* send add link reject message, only one link supported for now */
430 rc = smc_llc_send_add_link(link,
431 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +0200432 link->gid, SMC_LLC_RESP);
Karsten Graul52bedf32018-03-01 13:51:32 +0100433 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200434 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +0100435
Karsten Graul877ae5b2018-05-02 16:56:44 +0200436 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100437
Karsten Graul75d320d2018-03-01 13:51:31 +0100438 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100439}
440
Hans Wippel41349842018-06-28 19:05:11 +0200441static void smcr_conn_save_peer_info(struct smc_sock *smc,
442 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100443{
Hans Wippel95d8d2632018-05-18 09:34:13 +0200444 int bufsize = smc_uncompress_bufsize(clc->rmbe_size);
445
Hans Wippel92a138e2018-05-18 09:34:12 +0200446 smc->conn.peer_rmbe_idx = clc->rmbe_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100447 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200448 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100449 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200450 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100451}
452
Hans Wippel41349842018-06-28 19:05:11 +0200453static void smcd_conn_save_peer_info(struct smc_sock *smc,
454 struct smc_clc_msg_accept_confirm *clc)
455{
456 int bufsize = smc_uncompress_bufsize(clc->dmbe_size);
457
458 smc->conn.peer_rmbe_idx = clc->dmbe_idx;
459 smc->conn.peer_token = clc->token;
460 /* msg header takes up space in the buffer */
461 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
462 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
463 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
464}
465
466static void smc_conn_save_peer_info(struct smc_sock *smc,
467 struct smc_clc_msg_accept_confirm *clc)
468{
469 if (smc->conn.lgr->is_smcd)
470 smcd_conn_save_peer_info(smc, clc);
471 else
472 smcr_conn_save_peer_info(smc, clc);
473}
474
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100475static void smc_link_save_peer_info(struct smc_link *link,
476 struct smc_clc_msg_accept_confirm *clc)
477{
478 link->peer_qpn = ntoh24(clc->qpn);
479 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
480 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
481 link->peer_psn = ntoh24(clc->psn);
482 link->peer_mtu = clc->qp_mtu;
483}
484
Ursula Braun07603b22019-04-11 11:17:32 +0200485static void smc_switch_to_fallback(struct smc_sock *smc)
486{
487 smc->use_fallback = true;
488 if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
489 smc->clcsock->file = smc->sk.sk_socket->file;
490 smc->clcsock->file->private_data = smc->clcsock;
Ursula Braun67f562e2020-02-14 08:58:59 +0100491 smc->clcsock->wq.fasync_list =
492 smc->sk.sk_socket->wq.fasync_list;
Ursula Braun07603b22019-04-11 11:17:32 +0200493 }
494}
495
Hans Wippel3b2dec22018-05-18 09:34:18 +0200496/* fall back during connect */
Karsten Graul603cc142018-07-25 16:35:32 +0200497static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200498{
Ursula Braun07603b22019-04-11 11:17:32 +0200499 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +0200500 smc->fallback_rsn = reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200501 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200502 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200503 if (smc->sk.sk_state == SMC_INIT)
504 smc->sk.sk_state = SMC_ACTIVE;
505 return 0;
506}
507
508/* decline and fall back during connect */
509static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
510{
511 int rc;
512
Ursula Braune1bbdd52018-07-05 16:15:30 +0200513 if (reason_code < 0) { /* error, fallback is not possible */
514 if (smc->sk.sk_state == SMC_INIT)
515 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200516 return reason_code;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200517 }
Karsten Graul603cc142018-07-25 16:35:32 +0200518 if (reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200519 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200520 if (rc < 0) {
521 if (smc->sk.sk_state == SMC_INIT)
522 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200523 return rc;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200524 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200525 }
Karsten Graul603cc142018-07-25 16:35:32 +0200526 return smc_connect_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200527}
528
529/* abort connecting */
530static int smc_connect_abort(struct smc_sock *smc, int reason_code,
531 int local_contact)
532{
Ursula Braun51e3dfa2020-02-25 16:34:36 +0100533 bool is_smcd = smc->conn.lgr->is_smcd;
534
Hans Wippel3b2dec22018-05-18 09:34:18 +0200535 if (local_contact == SMC_FIRST_CONTACT)
Ursula Braun51e3dfa2020-02-25 16:34:36 +0100536 smc_lgr_cleanup_early(&smc->conn);
537 else
538 smc_conn_free(&smc->conn);
539 if (is_smcd)
Hans Wippel72a36a82019-02-07 15:56:18 +0100540 /* there is only one lgr role for SMC-D; use server lock */
541 mutex_unlock(&smc_server_lgr_pending);
542 else
543 mutex_unlock(&smc_client_lgr_pending);
544
Ursula Braun50717a32019-04-12 12:57:23 +0200545 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200546 return reason_code;
547}
548
549/* check if there is a rdma device available for this connection. */
550/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200551static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200552{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200553 /* PNET table look up: search active ib_device and port
554 * within same PNETID that also contains the ethernet device
555 * used for the internal TCP socket
556 */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200557 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +0200558 if (!ini->ib_dev)
559 return SMC_CLC_DECL_NOSMCRDEV;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200560 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200561}
562
Hans Wippel41349842018-06-28 19:05:11 +0200563/* check if there is an ISM device available for this connection. */
564/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200565static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200566{
567 /* Find ISM device with same PNETID as connecting interface */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200568 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
569 if (!ini->ism_dev)
Karsten Graul9aa68d22019-04-12 12:57:29 +0200570 return SMC_CLC_DECL_NOSMCDDEV;
Hans Wippel41349842018-06-28 19:05:11 +0200571 return 0;
572}
573
574/* Check for VLAN ID and register it on ISM device just for CLC handshake */
575static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200576 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200577{
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200578 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev, ini->vlan_id))
Karsten Graul7a627252019-04-12 12:57:30 +0200579 return SMC_CLC_DECL_ISMVLANERR;
Hans Wippel41349842018-06-28 19:05:11 +0200580 return 0;
581}
582
583/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
584 * used, the VLAN ID will be registered again during the connection setup.
585 */
586static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200587 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200588{
589 if (!is_smcd)
590 return 0;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200591 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev, ini->vlan_id))
Hans Wippel41349842018-06-28 19:05:11 +0200592 return SMC_CLC_DECL_CNFERR;
593 return 0;
594}
595
Hans Wippel3b2dec22018-05-18 09:34:18 +0200596/* CLC handshake during connect */
Hans Wippelc758dfd2018-06-28 19:05:09 +0200597static int smc_connect_clc(struct smc_sock *smc, int smc_type,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200598 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200599 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200600{
601 int rc = 0;
602
603 /* do inband token exchange */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200604 rc = smc_clc_send_proposal(smc, smc_type, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200605 if (rc)
606 return rc;
607 /* receive SMC Accept CLC message */
Ursula Braun2b59f582018-11-22 10:26:39 +0100608 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT,
609 CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200610}
611
Ursula Brauna046d572017-01-09 16:55:16 +0100612/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200613static int smc_connect_rdma(struct smc_sock *smc,
614 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200615 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200616{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200617 struct smc_link *link;
618 int reason_code = 0;
619
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200620 ini->is_smcd = false;
621 ini->ib_lcl = &aclc->lcl;
622 ini->ib_clcqpn = ntoh24(aclc->qpn);
623 ini->srv_first_contact = aclc->hdr.flag;
624
Hans Wippel72a36a82019-02-07 15:56:18 +0100625 mutex_lock(&smc_client_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200626 reason_code = smc_conn_create(smc, ini);
627 if (reason_code) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100628 mutex_unlock(&smc_client_lgr_pending);
629 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200630 }
Karsten Graul387707f2020-04-29 17:10:40 +0200631 link = smc->conn.lnk;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200632
633 smc_conn_save_peer_info(smc, aclc);
634
635 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200636 if (smc_buf_create(smc, false))
Karsten Graul7a627252019-04-12 12:57:30 +0200637 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
638 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200639
Karsten Graul7a627252019-04-12 12:57:30 +0200640 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200641 smc_link_save_peer_info(link, aclc);
642
Karsten Graule07d31d2020-04-29 17:10:42 +0200643 if (smc_rmb_rtoken_handling(&smc->conn, link, aclc))
Karsten Graul603cc142018-07-25 16:35:32 +0200644 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
Karsten Graul7a627252019-04-12 12:57:30 +0200645 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200646
647 smc_close_init(smc);
648 smc_rx_init(smc);
649
Karsten Graul7a627252019-04-12 12:57:30 +0200650 if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200651 if (smc_ib_ready_link(link))
Karsten Graul603cc142018-07-25 16:35:32 +0200652 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
Karsten Graul7a627252019-04-12 12:57:30 +0200653 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200654 } else {
Karsten Graulb9247542020-04-29 17:10:41 +0200655 if (smcr_lgr_reg_rmbs(smc->conn.lgr, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +0200656 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
Karsten Graul7a627252019-04-12 12:57:30 +0200657 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200658 }
659 smc_rmb_sync_sg_for_device(&smc->conn);
660
661 reason_code = smc_clc_send_confirm(smc);
662 if (reason_code)
Karsten Graul7a627252019-04-12 12:57:30 +0200663 return smc_connect_abort(smc, reason_code,
664 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200665
666 smc_tx_init(smc);
667
Karsten Graul7a627252019-04-12 12:57:30 +0200668 if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200669 /* QP confirmation over RoCE fabric */
Karsten Graulb9247542020-04-29 17:10:41 +0200670 reason_code = smcr_clnt_conf_first_link(smc);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200671 if (reason_code)
672 return smc_connect_abort(smc, reason_code,
Karsten Graul7a627252019-04-12 12:57:30 +0200673 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200674 }
Hans Wippel72a36a82019-02-07 15:56:18 +0100675 mutex_unlock(&smc_client_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200676
677 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200678 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200679 if (smc->sk.sk_state == SMC_INIT)
680 smc->sk.sk_state = SMC_ACTIVE;
681
682 return 0;
683}
684
Hans Wippel41349842018-06-28 19:05:11 +0200685/* setup for ISM connection of client */
686static int smc_connect_ism(struct smc_sock *smc,
687 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200688 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200689{
Hans Wippel41349842018-06-28 19:05:11 +0200690 int rc = 0;
691
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200692 ini->is_smcd = true;
693 ini->ism_gid = aclc->gid;
694 ini->srv_first_contact = aclc->hdr.flag;
695
Hans Wippel72a36a82019-02-07 15:56:18 +0100696 /* there is only one lgr role for SMC-D; use server lock */
697 mutex_lock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200698 rc = smc_conn_create(smc, ini);
699 if (rc) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100700 mutex_unlock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200701 return rc;
Hans Wippel72a36a82019-02-07 15:56:18 +0100702 }
Hans Wippel41349842018-06-28 19:05:11 +0200703
704 /* Create send and receive buffers */
705 if (smc_buf_create(smc, true))
Karsten Graul7a627252019-04-12 12:57:30 +0200706 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
707 ini->cln_first_contact);
Hans Wippel41349842018-06-28 19:05:11 +0200708
709 smc_conn_save_peer_info(smc, aclc);
710 smc_close_init(smc);
711 smc_rx_init(smc);
712 smc_tx_init(smc);
713
714 rc = smc_clc_send_confirm(smc);
715 if (rc)
Karsten Graul7a627252019-04-12 12:57:30 +0200716 return smc_connect_abort(smc, rc, ini->cln_first_contact);
Hans Wippel72a36a82019-02-07 15:56:18 +0100717 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel41349842018-06-28 19:05:11 +0200718
719 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200720 smc->connect_nonblock = 0;
Hans Wippel41349842018-06-28 19:05:11 +0200721 if (smc->sk.sk_state == SMC_INIT)
722 smc->sk.sk_state = SMC_ACTIVE;
723
724 return 0;
725}
726
Hans Wippel3b2dec22018-05-18 09:34:18 +0200727/* perform steps before actually connecting */
728static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100729{
Hans Wippel41349842018-06-28 19:05:11 +0200730 bool ism_supported = false, rdma_supported = false;
Ursula Brauna046d572017-01-09 16:55:16 +0100731 struct smc_clc_msg_accept_confirm aclc;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200732 struct smc_init_info ini = {0};
Hans Wippel41349842018-06-28 19:05:11 +0200733 int smc_type;
Ursula Brauna046d572017-01-09 16:55:16 +0100734 int rc = 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100735
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200736 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +0200737 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200738
Hans Wippel3b2dec22018-05-18 09:34:18 +0200739 /* if peer has not signalled SMC-capability, fall back */
740 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +0200741 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200742
Ursula Brauna046d572017-01-09 16:55:16 +0100743 /* IPSec connections opt out of SMC-R optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200744 if (using_ipsec(smc))
745 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
Ursula Brauna046d572017-01-09 16:55:16 +0100746
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200747 /* get vlan id from IP device */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200748 if (smc_vlan_by_tcpsk(smc->clcsock, &ini))
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200749 return smc_connect_decline_fallback(smc,
750 SMC_CLC_DECL_GETVLANERR);
Hans Wippel41349842018-06-28 19:05:11 +0200751
752 /* check if there is an ism device available */
Karsten Graul228bae02019-04-12 12:57:28 +0200753 if (!smc_find_ism_device(smc, &ini) &&
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200754 !smc_connect_ism_vlan_setup(smc, &ini)) {
Hans Wippel41349842018-06-28 19:05:11 +0200755 /* ISM is supported for this connection */
756 ism_supported = true;
757 smc_type = SMC_TYPE_D;
758 }
759
760 /* check if there is a rdma device available */
Karsten Graul228bae02019-04-12 12:57:28 +0200761 if (!smc_find_rdma_device(smc, &ini)) {
Hans Wippel41349842018-06-28 19:05:11 +0200762 /* RDMA is supported for this connection */
763 rdma_supported = true;
764 if (ism_supported)
765 smc_type = SMC_TYPE_B; /* both */
766 else
767 smc_type = SMC_TYPE_R; /* only RDMA */
768 }
769
770 /* if neither ISM nor RDMA are supported, fallback */
771 if (!rdma_supported && !ism_supported)
Karsten Graul603cc142018-07-25 16:35:32 +0200772 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV);
Ursula Brauna046d572017-01-09 16:55:16 +0100773
Hans Wippel3b2dec22018-05-18 09:34:18 +0200774 /* perform CLC handshake */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200775 rc = smc_connect_clc(smc, smc_type, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200776 if (rc) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200777 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200778 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200779 }
Ursula Brauna046d572017-01-09 16:55:16 +0100780
Hans Wippel41349842018-06-28 19:05:11 +0200781 /* depending on previous steps, connect using rdma or ism */
782 if (rdma_supported && aclc.hdr.path == SMC_TYPE_R)
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200783 rc = smc_connect_rdma(smc, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200784 else if (ism_supported && aclc.hdr.path == SMC_TYPE_D)
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200785 rc = smc_connect_ism(smc, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200786 else
Karsten Graul603cc142018-07-25 16:35:32 +0200787 rc = SMC_CLC_DECL_MODEUNSUPP;
Hans Wippel41349842018-06-28 19:05:11 +0200788 if (rc) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200789 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200790 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200791 }
Ursula Brauna046d572017-01-09 16:55:16 +0100792
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200793 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200794 return 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100795}
796
Ursula Braun24ac3a02018-06-27 17:59:50 +0200797static void smc_connect_work(struct work_struct *work)
798{
799 struct smc_sock *smc = container_of(work, struct smc_sock,
800 connect_work);
Ursula Braun50717a32019-04-12 12:57:23 +0200801 long timeo = smc->sk.sk_sndtimeo;
802 int rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200803
Ursula Braun50717a32019-04-12 12:57:23 +0200804 if (!timeo)
805 timeo = MAX_SCHEDULE_TIMEOUT;
806 lock_sock(smc->clcsock->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200807 if (smc->clcsock->sk->sk_err) {
808 smc->sk.sk_err = smc->clcsock->sk->sk_err;
Ursula Braun50717a32019-04-12 12:57:23 +0200809 } else if ((1 << smc->clcsock->sk->sk_state) &
810 (TCPF_SYN_SENT | TCP_SYN_RECV)) {
811 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
812 if ((rc == -EPIPE) &&
813 ((1 << smc->clcsock->sk->sk_state) &
814 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
815 rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200816 }
Ursula Braun50717a32019-04-12 12:57:23 +0200817 release_sock(smc->clcsock->sk);
818 lock_sock(&smc->sk);
819 if (rc != 0 || smc->sk.sk_err) {
820 smc->sk.sk_state = SMC_CLOSED;
821 if (rc == -EPIPE || rc == -EAGAIN)
822 smc->sk.sk_err = EPIPE;
823 else if (signal_pending(current))
824 smc->sk.sk_err = -sock_intr_errno(timeo);
Ursula Braun6d6dd522019-11-12 16:03:41 +0100825 sock_put(&smc->sk); /* passive closing */
Ursula Braun24ac3a02018-06-27 17:59:50 +0200826 goto out;
827 }
828
829 rc = __smc_connect(smc);
830 if (rc < 0)
831 smc->sk.sk_err = -rc;
832
833out:
Ursula Braun07603b22019-04-11 11:17:32 +0200834 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
835 if (smc->sk.sk_err) {
836 smc->sk.sk_state_change(&smc->sk);
837 } else { /* allow polling before and after fallback decision */
838 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
839 smc->sk.sk_write_space(&smc->sk);
840 }
841 }
Ursula Braun24ac3a02018-06-27 17:59:50 +0200842 release_sock(&smc->sk);
843}
844
Ursula Braunac713872017-01-09 16:55:13 +0100845static int smc_connect(struct socket *sock, struct sockaddr *addr,
846 int alen, int flags)
847{
848 struct sock *sk = sock->sk;
849 struct smc_sock *smc;
850 int rc = -EINVAL;
851
852 smc = smc_sk(sk);
853
854 /* separate smc parameter checking to be safe */
855 if (alen < sizeof(addr->sa_family))
856 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100857 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100858 goto out_err;
859
860 lock_sock(sk);
861 switch (sk->sk_state) {
862 default:
863 goto out;
864 case SMC_ACTIVE:
865 rc = -EISCONN;
866 goto out;
867 case SMC_INIT:
868 rc = 0;
869 break;
870 }
871
872 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200873 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun50717a32019-04-12 12:57:23 +0200874 if (smc->connect_nonblock) {
875 rc = -EALREADY;
876 goto out;
877 }
878 rc = kernel_connect(smc->clcsock, addr, alen, flags);
879 if (rc && rc != -EINPROGRESS)
880 goto out;
Ursula Braun301428e2019-10-29 12:41:26 +0100881
882 sock_hold(&smc->sk); /* sock put in passive closing */
Ursula Braun86434742019-12-12 22:35:58 +0100883 if (smc->use_fallback)
884 goto out;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200885 if (flags & O_NONBLOCK) {
Ursula Braun50717a32019-04-12 12:57:23 +0200886 if (schedule_work(&smc->connect_work))
887 smc->connect_nonblock = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200888 rc = -EINPROGRESS;
889 } else {
Ursula Braun24ac3a02018-06-27 17:59:50 +0200890 rc = __smc_connect(smc);
891 if (rc < 0)
892 goto out;
893 else
894 rc = 0; /* success cases including fallback */
895 }
Ursula Braunac713872017-01-09 16:55:13 +0100896
897out:
898 release_sock(sk);
899out_err:
900 return rc;
901}
902
903static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
904{
Ursula Braun3163c502018-01-24 10:28:12 +0100905 struct socket *new_clcsock = NULL;
906 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100907 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -0800908 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +0100909
Ursula Braun3163c502018-01-24 10:28:12 +0100910 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100911 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100912 if (!new_sk) {
913 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100914 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100915 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100916 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100917 goto out;
918 }
919 *new_smc = smc_sk(new_sk);
920
Myungho Jung78abe3d2018-12-18 09:02:25 -0800921 mutex_lock(&lsmc->clcsock_release_lock);
922 if (lsmc->clcsock)
923 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
924 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +0100925 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100926 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100927 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100928 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +0200929 new_sk->sk_prot->unhash(new_sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100930 if (new_clcsock)
931 sock_release(new_clcsock);
932 new_sk->sk_state = SMC_CLOSED;
933 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun51f1de72018-01-26 09:28:48 +0100934 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100935 *new_smc = NULL;
936 goto out;
937 }
938
939 (*new_smc)->clcsock = new_clcsock;
940out:
941 return rc;
942}
943
Ursula Brauna046d572017-01-09 16:55:16 +0100944/* add a just created sock to the accept queue of the listen sock as
945 * candidate for a following socket accept call from user space
946 */
947static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
948{
949 struct smc_sock *par = smc_sk(parent);
950
Ursula Braun51f1de72018-01-26 09:28:48 +0100951 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100952 spin_lock(&par->accept_q_lock);
953 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
954 spin_unlock(&par->accept_q_lock);
955 sk_acceptq_added(parent);
956}
957
958/* remove a socket from the accept queue of its parental listening socket */
959static void smc_accept_unlink(struct sock *sk)
960{
961 struct smc_sock *par = smc_sk(sk)->listen_smc;
962
963 spin_lock(&par->accept_q_lock);
964 list_del_init(&smc_sk(sk)->accept_q);
965 spin_unlock(&par->accept_q_lock);
966 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100967 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100968}
969
970/* remove a sock from the accept queue to bind it to a new socket created
971 * for a socket accept call from user space
972 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100973struct sock *smc_accept_dequeue(struct sock *parent,
974 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100975{
976 struct smc_sock *isk, *n;
977 struct sock *new_sk;
978
979 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
980 new_sk = (struct sock *)isk;
981
982 smc_accept_unlink(new_sk);
983 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +0200984 new_sk->sk_prot->unhash(new_sk);
Ursula Braun127f4972018-01-26 09:28:49 +0100985 if (isk->clcsock) {
986 sock_release(isk->clcsock);
987 isk->clcsock = NULL;
988 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100989 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +0100990 continue;
991 }
Ursula Braun07603b22019-04-11 11:17:32 +0200992 if (new_sock) {
Ursula Brauna046d572017-01-09 16:55:16 +0100993 sock_graft(new_sk, new_sock);
Ursula Braun07603b22019-04-11 11:17:32 +0200994 if (isk->use_fallback) {
995 smc_sk(new_sk)->clcsock->file = new_sock->file;
996 isk->clcsock->file->private_data = isk->clcsock;
997 }
998 }
Ursula Brauna046d572017-01-09 16:55:16 +0100999 return new_sk;
1000 }
1001 return NULL;
1002}
1003
1004/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +01001005void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +01001006{
1007 struct smc_sock *smc = smc_sk(sk);
1008
Ursula Braun81cf4f42019-10-21 16:13:15 +02001009 sock_hold(sk); /* sock_put below */
Ursula Braunb38d7322017-01-09 16:55:25 +01001010 lock_sock(sk);
1011 if (!sk->sk_lingertime)
1012 /* wait for peer closing */
1013 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun39f41f32019-06-27 15:04:52 +02001014 __smc_release(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +01001015 release_sock(sk);
Ursula Braun81cf4f42019-10-21 16:13:15 +02001016 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +01001017 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +01001018}
1019
Karsten Graulb9247542020-04-29 17:10:41 +02001020static int smcr_serv_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001021{
Karsten Graul877ae5b2018-05-02 16:56:44 +02001022 struct net *net = sock_net(smc->clcsock->sk);
Karsten Graul387707f2020-04-29 17:10:40 +02001023 struct smc_link *link = smc->conn.lnk;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001024 int rest;
1025 int rc;
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 */
1036 rest = wait_for_completion_interruptible_timeout(
1037 &link->llc_confirm_resp,
1038 SMC_LLC_WAIT_FIRST_TIME);
1039 if (rest <= 0) {
1040 struct smc_clc_msg_decline dclc;
1041
1042 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001043 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001044 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001045 }
1046
Karsten Graul75d320d2018-03-01 13:51:31 +01001047 if (link->llc_confirm_resp_rc)
1048 return SMC_CLC_DECL_RMBE_EC;
1049
Karsten Graul52bedf32018-03-01 13:51:32 +01001050 /* send ADD LINK request to client over the RoCE fabric */
1051 rc = smc_llc_send_add_link(link,
1052 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +02001053 link->gid, SMC_LLC_REQ);
Karsten Graul52bedf32018-03-01 13:51:32 +01001054 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001055 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +01001056
1057 /* receive ADD LINK response from client over the RoCE fabric */
1058 rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
1059 SMC_LLC_WAIT_TIME);
1060 if (rest <= 0) {
1061 struct smc_clc_msg_decline dclc;
1062
1063 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001064 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001065 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
Karsten Graul52bedf32018-03-01 13:51:32 +01001066 }
1067
Karsten Graul877ae5b2018-05-02 16:56:44 +02001068 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +01001069
Karsten Graul75d320d2018-03-01 13:51:31 +01001070 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001071}
1072
Hans Wippel3b2dec22018-05-18 09:34:18 +02001073/* listen worker: finish */
1074static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001075{
Ursula Brauna046d572017-01-09 16:55:16 +01001076 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001077 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001078
Ursula Brauna046d572017-01-09 16:55:16 +01001079 if (lsmc->sk.sk_state == SMC_LISTEN) {
Karsten Graulfd577702019-04-11 11:17:30 +02001080 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001081 smc_accept_enqueue(&lsmc->sk, newsmcsk);
Karsten Graulfd577702019-04-11 11:17:30 +02001082 release_sock(&lsmc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001083 } else { /* no longer listening */
1084 smc_close_non_accepted(newsmcsk);
1085 }
Ursula Brauna046d572017-01-09 16:55:16 +01001086
1087 /* Wake up accept */
1088 lsmc->sk.sk_data_ready(&lsmc->sk);
1089 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001090}
Ursula Brauna046d572017-01-09 16:55:16 +01001091
Hans Wippel3b2dec22018-05-18 09:34:18 +02001092/* listen worker: finish in state connected */
1093static void smc_listen_out_connected(struct smc_sock *new_smc)
1094{
1095 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001096
Hans Wippel3b2dec22018-05-18 09:34:18 +02001097 sk_refcnt_debug_inc(newsmcsk);
1098 if (newsmcsk->sk_state == SMC_INIT)
1099 newsmcsk->sk_state = SMC_ACTIVE;
1100
1101 smc_listen_out(new_smc);
1102}
1103
1104/* listen worker: finish in error state */
1105static void smc_listen_out_err(struct smc_sock *new_smc)
1106{
1107 struct sock *newsmcsk = &new_smc->sk;
1108
Ursula Braun51f1de72018-01-26 09:28:48 +01001109 if (newsmcsk->sk_state == SMC_INIT)
1110 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001111 newsmcsk->sk_state = SMC_CLOSED;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001112
1113 smc_listen_out(new_smc);
1114}
1115
1116/* listen worker: decline and fall back if possible */
1117static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1118 int local_contact)
1119{
1120 /* RDMA setup failed, switch back to TCP */
1121 if (local_contact == SMC_FIRST_CONTACT)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001122 smc_lgr_cleanup_early(&new_smc->conn);
1123 else
1124 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001125 if (reason_code < 0) { /* error, no fallback possible */
1126 smc_listen_out_err(new_smc);
1127 return;
1128 }
Ursula Braun07603b22019-04-11 11:17:32 +02001129 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001130 new_smc->fallback_rsn = reason_code;
1131 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001132 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1133 smc_listen_out_err(new_smc);
1134 return;
1135 }
1136 }
1137 smc_listen_out_connected(new_smc);
1138}
1139
1140/* listen worker: check prefixes */
Karsten Graul59886692019-04-12 12:57:25 +02001141static int smc_listen_prfx_check(struct smc_sock *new_smc,
Hans Wippel3b2dec22018-05-18 09:34:18 +02001142 struct smc_clc_msg_proposal *pclc)
1143{
1144 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1145 struct socket *newclcsock = new_smc->clcsock;
1146
1147 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1148 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
Karsten Graul59886692019-04-12 12:57:25 +02001149 return SMC_CLC_DECL_DIFFPREFIX;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001150
1151 return 0;
1152}
1153
1154/* listen worker: initialize connection and buffers */
1155static int smc_listen_rdma_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001156 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001157{
Karsten Graul7a627252019-04-12 12:57:30 +02001158 int rc;
1159
Hans Wippel3b2dec22018-05-18 09:34:18 +02001160 /* allocate connection / link group */
Karsten Graul7a627252019-04-12 12:57:30 +02001161 rc = smc_conn_create(new_smc, ini);
1162 if (rc)
1163 return rc;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001164
1165 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001166 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001167 return SMC_CLC_DECL_MEM;
1168
1169 return 0;
1170}
1171
Hans Wippel41349842018-06-28 19:05:11 +02001172/* listen worker: initialize connection and buffers for SMC-D */
1173static int smc_listen_ism_init(struct smc_sock *new_smc,
1174 struct smc_clc_msg_proposal *pclc,
Karsten Graul7a627252019-04-12 12:57:30 +02001175 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +02001176{
1177 struct smc_clc_msg_smcd *pclc_smcd;
Karsten Graul7a627252019-04-12 12:57:30 +02001178 int rc;
Hans Wippel41349842018-06-28 19:05:11 +02001179
1180 pclc_smcd = smc_get_clc_msg_smcd(pclc);
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001181 ini->ism_gid = pclc_smcd->gid;
Karsten Graul7a627252019-04-12 12:57:30 +02001182 rc = smc_conn_create(new_smc, ini);
1183 if (rc)
1184 return rc;
Hans Wippel41349842018-06-28 19:05:11 +02001185
1186 /* Check if peer can be reached via ISM device */
1187 if (smc_ism_cantalk(new_smc->conn.lgr->peer_gid,
1188 new_smc->conn.lgr->vlan_id,
1189 new_smc->conn.lgr->smcd)) {
Karsten Graul7a627252019-04-12 12:57:30 +02001190 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001191 smc_lgr_cleanup_early(&new_smc->conn);
1192 else
1193 smc_conn_free(&new_smc->conn);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001194 return SMC_CLC_DECL_SMCDNOTALK;
Hans Wippel41349842018-06-28 19:05:11 +02001195 }
1196
1197 /* Create send and receive buffers */
1198 if (smc_buf_create(new_smc, true)) {
Karsten Graul7a627252019-04-12 12:57:30 +02001199 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001200 smc_lgr_cleanup_early(&new_smc->conn);
1201 else
1202 smc_conn_free(&new_smc->conn);
Hans Wippel41349842018-06-28 19:05:11 +02001203 return SMC_CLC_DECL_MEM;
1204 }
1205
1206 return 0;
1207}
1208
Hans Wippel3b2dec22018-05-18 09:34:18 +02001209/* listen worker: register buffers */
1210static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
1211{
Karsten Graulb9247542020-04-29 17:10:41 +02001212 struct smc_connection *conn = &new_smc->conn;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001213
1214 if (local_contact != SMC_FIRST_CONTACT) {
Karsten Graulb9247542020-04-29 17:10:41 +02001215 if (smcr_lgr_reg_rmbs(conn->lgr, conn->rmb_desc))
Karsten Graulc7674c02018-11-22 10:26:43 +01001216 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001217 }
1218 smc_rmb_sync_sg_for_device(&new_smc->conn);
1219
1220 return 0;
1221}
1222
1223/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001224static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1225 struct smc_clc_msg_accept_confirm *cclc,
1226 int local_contact)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001227{
Karsten Graul387707f2020-04-29 17:10:40 +02001228 struct smc_link *link = new_smc->conn.lnk;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001229 int reason_code = 0;
1230
1231 if (local_contact == SMC_FIRST_CONTACT)
1232 smc_link_save_peer_info(link, cclc);
1233
Karsten Graule07d31d2020-04-29 17:10:42 +02001234 if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001235 reason_code = SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001236 goto decline;
1237 }
1238
1239 if (local_contact == SMC_FIRST_CONTACT) {
1240 if (smc_ib_ready_link(link)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001241 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001242 goto decline;
1243 }
1244 /* QP confirmation over RoCE fabric */
Karsten Graulb9247542020-04-29 17:10:41 +02001245 reason_code = smcr_serv_conf_first_link(new_smc);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001246 if (reason_code)
1247 goto decline;
1248 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001249 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001250
1251decline:
Hans Wippel3b2dec22018-05-18 09:34:18 +02001252 smc_listen_decline(new_smc, reason_code, local_contact);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001253 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001254}
1255
1256/* setup for RDMA connection of server */
1257static void smc_listen_work(struct work_struct *work)
1258{
1259 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1260 smc_listen_work);
1261 struct socket *newclcsock = new_smc->clcsock;
1262 struct smc_clc_msg_accept_confirm cclc;
1263 struct smc_clc_msg_proposal *pclc;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001264 struct smc_init_info ini = {0};
Hans Wippel41349842018-06-28 19:05:11 +02001265 bool ism_supported = false;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001266 u8 buf[SMC_CLC_MAX_LEN];
Hans Wippel3b2dec22018-05-18 09:34:18 +02001267 int rc = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001268
Karsten Graulfd577702019-04-11 11:17:30 +02001269 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1270 return smc_listen_out_err(new_smc);
1271
Hans Wippel3b2dec22018-05-18 09:34:18 +02001272 if (new_smc->use_fallback) {
1273 smc_listen_out_connected(new_smc);
1274 return;
1275 }
1276
1277 /* check if peer is smc capable */
1278 if (!tcp_sk(newclcsock->sk)->syn_smc) {
Ursula Braun07603b22019-04-11 11:17:32 +02001279 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001280 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001281 smc_listen_out_connected(new_smc);
1282 return;
1283 }
1284
1285 /* do inband token exchange -
1286 * wait for and receive SMC Proposal CLC message
1287 */
1288 pclc = (struct smc_clc_msg_proposal *)&buf;
Karsten Graul228bae02019-04-12 12:57:28 +02001289 rc = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
1290 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001291 if (rc)
1292 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001293
1294 /* IPSec connections opt out of SMC-R optimizations */
1295 if (using_ipsec(new_smc)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001296 rc = SMC_CLC_DECL_IPSEC;
1297 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001298 }
1299
Karsten Graul59886692019-04-12 12:57:25 +02001300 /* check for matching IP prefix and subnet length */
1301 rc = smc_listen_prfx_check(new_smc, pclc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001302 if (rc)
1303 goto out_decl;
Karsten Graul59886692019-04-12 12:57:25 +02001304
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001305 /* get vlan id from IP device */
1306 if (smc_vlan_by_tcpsk(new_smc->clcsock, &ini)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001307 rc = SMC_CLC_DECL_GETVLANERR;
1308 goto out_decl;
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001309 }
1310
Hans Wippel72a36a82019-02-07 15:56:18 +01001311 mutex_lock(&smc_server_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001312 smc_close_init(new_smc);
1313 smc_rx_init(new_smc);
1314 smc_tx_init(new_smc);
1315
Hans Wippel41349842018-06-28 19:05:11 +02001316 /* check if ISM is available */
Karsten Graul9aa68d22019-04-12 12:57:29 +02001317 if (pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) {
1318 ini.is_smcd = true; /* prepare ISM check */
1319 rc = smc_find_ism_device(new_smc, &ini);
1320 if (!rc)
Karsten Graul7a627252019-04-12 12:57:30 +02001321 rc = smc_listen_ism_init(new_smc, pclc, &ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001322 if (!rc)
1323 ism_supported = true;
1324 else if (pclc->hdr.path == SMC_TYPE_D)
1325 goto out_unlock; /* skip RDMA and decline */
1326 }
1327
1328 /* check if RDMA is available */
1329 if (!ism_supported) { /* SMC_TYPE_R or SMC_TYPE_B */
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001330 /* prepare RDMA check */
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001331 ini.is_smcd = false;
Ursula Braunca5f8d22019-10-23 15:44:06 +02001332 ini.ism_dev = NULL;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001333 ini.ib_lcl = &pclc->lcl;
Karsten Graul9aa68d22019-04-12 12:57:29 +02001334 rc = smc_find_rdma_device(new_smc, &ini);
1335 if (rc) {
1336 /* no RDMA device found */
1337 if (pclc->hdr.path == SMC_TYPE_B)
1338 /* neither ISM nor RDMA device found */
1339 rc = SMC_CLC_DECL_NOSMCDEV;
1340 goto out_unlock;
1341 }
Karsten Graul7a627252019-04-12 12:57:30 +02001342 rc = smc_listen_rdma_init(new_smc, &ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001343 if (rc)
1344 goto out_unlock;
Karsten Graul7a627252019-04-12 12:57:30 +02001345 rc = smc_listen_rdma_reg(new_smc, ini.cln_first_contact);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001346 if (rc)
1347 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001348 }
1349
1350 /* send SMC Accept CLC message */
Karsten Graul7a627252019-04-12 12:57:30 +02001351 rc = smc_clc_send_accept(new_smc, ini.cln_first_contact);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001352 if (rc)
1353 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001354
Hans Wippel62c71392019-02-07 15:56:17 +01001355 /* SMC-D does not need this lock any more */
1356 if (ism_supported)
Hans Wippel72a36a82019-02-07 15:56:18 +01001357 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001358
Hans Wippel3b2dec22018-05-18 09:34:18 +02001359 /* receive SMC Confirm CLC message */
Karsten Graul228bae02019-04-12 12:57:28 +02001360 rc = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
1361 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
1362 if (rc) {
Hans Wippel62c71392019-02-07 15:56:17 +01001363 if (!ism_supported)
Karsten Graul9aa68d22019-04-12 12:57:29 +02001364 goto out_unlock;
1365 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001366 }
1367
1368 /* finish worker */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001369 if (!ism_supported) {
Karsten Graul7a627252019-04-12 12:57:30 +02001370 rc = smc_listen_rdma_finish(new_smc, &cclc,
1371 ini.cln_first_contact);
Hans Wippel72a36a82019-02-07 15:56:18 +01001372 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001373 if (rc)
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001374 return;
1375 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001376 smc_conn_save_peer_info(new_smc, &cclc);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001377 smc_listen_out_connected(new_smc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001378 return;
1379
1380out_unlock:
1381 mutex_unlock(&smc_server_lgr_pending);
1382out_decl:
Karsten Graul7a627252019-04-12 12:57:30 +02001383 smc_listen_decline(new_smc, rc, ini.cln_first_contact);
Ursula Brauna046d572017-01-09 16:55:16 +01001384}
1385
1386static void smc_tcp_listen_work(struct work_struct *work)
1387{
1388 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1389 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001390 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001391 struct smc_sock *new_smc;
1392 int rc = 0;
1393
Ursula Braun3163c502018-01-24 10:28:12 +01001394 lock_sock(lsk);
1395 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001396 rc = smc_clcsock_accept(lsmc, &new_smc);
1397 if (rc)
1398 goto out;
1399 if (!new_smc)
1400 continue;
1401
1402 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001403 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001404 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001405 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001406 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1407 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001408 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1409 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001410 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1411 if (!schedule_work(&new_smc->smc_listen_work))
1412 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001413 }
1414
1415out:
Ursula Braun3163c502018-01-24 10:28:12 +01001416 release_sock(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001417 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +01001418}
1419
Ursula Braunac713872017-01-09 16:55:13 +01001420static int smc_listen(struct socket *sock, int backlog)
1421{
1422 struct sock *sk = sock->sk;
1423 struct smc_sock *smc;
1424 int rc;
1425
1426 smc = smc_sk(sk);
1427 lock_sock(sk);
1428
1429 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +02001430 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
1431 smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +01001432 goto out;
1433
1434 rc = 0;
1435 if (sk->sk_state == SMC_LISTEN) {
1436 sk->sk_max_ack_backlog = backlog;
1437 goto out;
1438 }
1439 /* some socket options are handled in core, so we could not apply
1440 * them to the clc socket -- copy smc socket options to clc socket
1441 */
1442 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001443 if (!smc->use_fallback)
1444 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001445
1446 rc = kernel_listen(smc->clcsock, backlog);
1447 if (rc)
1448 goto out;
1449 sk->sk_max_ack_backlog = backlog;
1450 sk->sk_ack_backlog = 0;
1451 sk->sk_state = SMC_LISTEN;
Ursula Braun51f1de72018-01-26 09:28:48 +01001452 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1453 if (!schedule_work(&smc->tcp_listen_work))
1454 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001455
1456out:
1457 release_sock(sk);
1458 return rc;
1459}
1460
1461static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001462 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001463{
Ursula Brauna046d572017-01-09 16:55:16 +01001464 struct sock *sk = sock->sk, *nsk;
1465 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001466 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001467 long timeo;
1468 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001469
1470 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001471 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001472 lock_sock(sk);
1473
1474 if (lsmc->sk.sk_state != SMC_LISTEN) {
1475 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001476 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001477 goto out;
1478 }
1479
Ursula Brauna046d572017-01-09 16:55:16 +01001480 /* Wait for an incoming connection */
1481 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1482 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1483 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1484 set_current_state(TASK_INTERRUPTIBLE);
1485 if (!timeo) {
1486 rc = -EAGAIN;
1487 break;
1488 }
1489 release_sock(sk);
1490 timeo = schedule_timeout(timeo);
1491 /* wakeup by sk_data_ready in smc_listen_work() */
1492 sched_annotate_sleep();
1493 lock_sock(sk);
1494 if (signal_pending(current)) {
1495 rc = sock_intr_errno(timeo);
1496 break;
1497 }
1498 }
1499 set_current_state(TASK_RUNNING);
1500 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001501
Ursula Brauna046d572017-01-09 16:55:16 +01001502 if (!rc)
1503 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001504 release_sock(sk);
1505 if (rc)
1506 goto out;
1507
1508 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1509 /* wait till data arrives on the socket */
1510 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1511 MSEC_PER_SEC);
1512 if (smc_sk(nsk)->use_fallback) {
1513 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1514
1515 lock_sock(clcsk);
1516 if (skb_queue_empty(&clcsk->sk_receive_queue))
1517 sk_wait_data(clcsk, &timeo, NULL);
1518 release_sock(clcsk);
1519 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1520 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001521 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001522 release_sock(nsk);
1523 }
1524 }
Ursula Braunac713872017-01-09 16:55:13 +01001525
1526out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001527 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001528 return rc;
1529}
1530
1531static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001532 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001533{
1534 struct smc_sock *smc;
1535
Ursula Braunb38d7322017-01-09 16:55:25 +01001536 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1537 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001538 return -ENOTCONN;
1539
1540 smc = smc_sk(sock->sk);
1541
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001542 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001543}
1544
1545static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1546{
1547 struct sock *sk = sock->sk;
1548 struct smc_sock *smc;
1549 int rc = -EPIPE;
1550
1551 smc = smc_sk(sk);
1552 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001553 if ((sk->sk_state != SMC_ACTIVE) &&
1554 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1555 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001556 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001557
1558 if (msg->msg_flags & MSG_FASTOPEN) {
Ursula Brauncd206362019-08-02 10:47:50 +02001559 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001560 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001561 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001562 } else {
1563 rc = -EINVAL;
1564 goto out;
1565 }
1566 }
1567
Ursula Braunac713872017-01-09 16:55:13 +01001568 if (smc->use_fallback)
1569 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1570 else
Ursula Braune6727f32017-01-09 16:55:23 +01001571 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001572out:
1573 release_sock(sk);
1574 return rc;
1575}
1576
1577static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1578 int flags)
1579{
1580 struct sock *sk = sock->sk;
1581 struct smc_sock *smc;
1582 int rc = -ENOTCONN;
1583
1584 smc = smc_sk(sk);
1585 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001586 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1587 /* socket was connected before, no more data to read */
1588 rc = 0;
1589 goto out;
1590 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001591 if ((sk->sk_state == SMC_INIT) ||
1592 (sk->sk_state == SMC_LISTEN) ||
1593 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001594 goto out;
1595
Ursula Braunb38d7322017-01-09 16:55:25 +01001596 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1597 rc = 0;
1598 goto out;
1599 }
1600
Stefan Raspl9014db22018-05-03 18:12:39 +02001601 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001602 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001603 } else {
1604 msg->msg_namelen = 0;
1605 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1606 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001607
Ursula Braunac713872017-01-09 16:55:13 +01001608out:
1609 release_sock(sk);
1610 return rc;
1611}
1612
Al Viroade994f2017-07-03 00:01:49 -04001613static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001614{
Ursula Braun8dce2782018-01-26 09:28:47 +01001615 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001616 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001617
Ursula Braun8dce2782018-01-26 09:28:47 +01001618 spin_lock(&isk->accept_q_lock);
1619 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001620 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001621 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001622
Ursula Braun8dce2782018-01-26 09:28:47 +01001623 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001624}
1625
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001626static __poll_t smc_poll(struct file *file, struct socket *sock,
1627 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01001628{
1629 struct sock *sk = sock->sk;
Ursula Braunac713872017-01-09 16:55:13 +01001630 struct smc_sock *smc;
Ursula Braun50717a32019-04-12 12:57:23 +02001631 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001632
Ursula Braun8dce2782018-01-26 09:28:47 +01001633 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001634 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001635
Ursula Braunac713872017-01-09 16:55:13 +01001636 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02001637 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01001638 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001639 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02001640 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braunac713872017-01-09 16:55:13 +01001641 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07001642 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02001643 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01001644 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001645 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001646 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1647 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001648 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001649 if (sk->sk_state == SMC_LISTEN) {
1650 /* woken up by sk_data_ready in smc_listen_work() */
Ursula Braun50717a32019-04-12 12:57:23 +02001651 mask |= smc_accept_poll(sk);
1652 } else if (smc->use_fallback) { /* as result of connect_work()*/
1653 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
1654 wait);
1655 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun8dce2782018-01-26 09:28:47 +01001656 } else {
Ursula Braun50717a32019-04-12 12:57:23 +02001657 if ((sk->sk_state != SMC_INIT &&
1658 atomic_read(&smc->conn.sndbuf_space)) ||
Ursula Braun8dce2782018-01-26 09:28:47 +01001659 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001660 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001661 } else {
1662 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1663 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1664 }
1665 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001666 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001667 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001668 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001669 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001670 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02001671 if (smc->conn.urg_state == SMC_URG_VALID)
1672 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01001673 }
Ursula Braunac713872017-01-09 16:55:13 +01001674 }
1675
1676 return mask;
1677}
1678
1679static int smc_shutdown(struct socket *sock, int how)
1680{
1681 struct sock *sk = sock->sk;
1682 struct smc_sock *smc;
1683 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001684 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001685
1686 smc = smc_sk(sk);
1687
1688 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001689 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001690
1691 lock_sock(sk);
1692
1693 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02001694 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01001695 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1696 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1697 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1698 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1699 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001700 goto out;
1701 if (smc->use_fallback) {
1702 rc = kernel_sock_shutdown(smc->clcsock, how);
1703 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1704 if (sk->sk_shutdown == SHUTDOWN_MASK)
1705 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001706 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001707 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001708 switch (how) {
1709 case SHUT_RDWR: /* shutdown in both directions */
1710 rc = smc_close_active(smc);
1711 break;
1712 case SHUT_WR:
1713 rc = smc_close_shutdown_write(smc);
1714 break;
1715 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001716 rc = 0;
1717 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001718 break;
1719 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001720 if (smc->clcsock)
1721 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001722 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1723 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001724
1725out:
1726 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001727 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001728}
1729
1730static int smc_setsockopt(struct socket *sock, int level, int optname,
1731 char __user *optval, unsigned int optlen)
1732{
1733 struct sock *sk = sock->sk;
1734 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001735 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001736
1737 smc = smc_sk(sk);
1738
1739 /* generic setsockopts reaching us here always apply to the
1740 * CLC socket
1741 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001742 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1743 optval, optlen);
1744 if (smc->clcsock->sk->sk_err) {
1745 sk->sk_err = smc->clcsock->sk->sk_err;
1746 sk->sk_error_report(sk);
1747 }
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001748
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001749 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00001750 return -EINVAL;
Ursula Braunac0107e2018-07-18 15:22:50 +02001751 if (get_user(val, (int __user *)optval))
1752 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001753
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001754 lock_sock(sk);
Ursula Braun86434742019-12-12 22:35:58 +01001755 if (rc || smc->use_fallback)
1756 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001757 switch (optname) {
1758 case TCP_ULP:
1759 case TCP_FASTOPEN:
1760 case TCP_FASTOPEN_CONNECT:
1761 case TCP_FASTOPEN_KEY:
1762 case TCP_FASTOPEN_NO_COOKIE:
1763 /* option not supported by SMC */
Ursula Braun8204df72019-11-15 12:39:30 +01001764 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001765 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001766 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001767 } else {
Ursula Braun86434742019-12-12 22:35:58 +01001768 rc = -EINVAL;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001769 }
1770 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001771 case TCP_NODELAY:
Ursula Braunf9cedf12019-08-02 10:16:38 +02001772 if (sk->sk_state != SMC_INIT &&
1773 sk->sk_state != SMC_LISTEN &&
1774 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01001775 if (val)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001776 mod_delayed_work(system_wq, &smc->conn.tx_work,
1777 0);
1778 }
1779 break;
1780 case TCP_CORK:
Ursula Braunf9cedf12019-08-02 10:16:38 +02001781 if (sk->sk_state != SMC_INIT &&
1782 sk->sk_state != SMC_LISTEN &&
1783 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01001784 if (!val)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001785 mod_delayed_work(system_wq, &smc->conn.tx_work,
1786 0);
1787 }
1788 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001789 case TCP_DEFER_ACCEPT:
1790 smc->sockopt_defer_accept = val;
1791 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001792 default:
1793 break;
1794 }
Ursula Braun86434742019-12-12 22:35:58 +01001795out:
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001796 release_sock(sk);
1797
1798 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001799}
1800
1801static int smc_getsockopt(struct socket *sock, int level, int optname,
1802 char __user *optval, int __user *optlen)
1803{
1804 struct smc_sock *smc;
1805
1806 smc = smc_sk(sock->sk);
1807 /* socket options apply to the CLC socket */
1808 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1809 optval, optlen);
1810}
1811
1812static int smc_ioctl(struct socket *sock, unsigned int cmd,
1813 unsigned long arg)
1814{
Stefan Rasplde8474e2018-05-23 16:38:11 +02001815 union smc_host_cursor cons, urg;
1816 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01001817 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001818 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001819
1820 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001821 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02001822 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02001823 if (smc->use_fallback) {
1824 if (!smc->clcsock) {
1825 release_sock(&smc->sk);
1826 return -EBADF;
1827 }
1828 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1829 release_sock(&smc->sk);
1830 return answ;
1831 }
Ursula Braun9b67e262018-05-02 16:56:46 +02001832 switch (cmd) {
1833 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02001834 if (smc->sk.sk_state == SMC_LISTEN) {
1835 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001836 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001837 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001838 if (smc->sk.sk_state == SMC_INIT ||
1839 smc->sk.sk_state == SMC_CLOSED)
1840 answ = 0;
1841 else
1842 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02001843 break;
1844 case SIOCOUTQ:
1845 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02001846 if (smc->sk.sk_state == SMC_LISTEN) {
1847 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001848 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001849 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001850 if (smc->sk.sk_state == SMC_INIT ||
1851 smc->sk.sk_state == SMC_CLOSED)
1852 answ = 0;
1853 else
1854 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02001855 atomic_read(&smc->conn.sndbuf_space);
1856 break;
1857 case SIOCOUTQNSD:
1858 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02001859 if (smc->sk.sk_state == SMC_LISTEN) {
1860 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001861 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001862 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001863 if (smc->sk.sk_state == SMC_INIT ||
1864 smc->sk.sk_state == SMC_CLOSED)
1865 answ = 0;
1866 else
1867 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02001868 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02001869 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02001870 if (smc->sk.sk_state == SMC_LISTEN) {
1871 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001872 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001873 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001874 if (smc->sk.sk_state == SMC_INIT ||
1875 smc->sk.sk_state == SMC_CLOSED) {
1876 answ = 0;
1877 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02001878 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1879 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001880 answ = smc_curs_diff(conn->rmb_desc->len,
1881 &cons, &urg) == 1;
1882 }
1883 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02001884 default:
Ursula Braun1992d992018-07-16 13:56:52 +02001885 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001886 return -ENOIOCTLCMD;
1887 }
Ursula Braun1992d992018-07-16 13:56:52 +02001888 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001889
1890 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001891}
1892
1893static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1894 int offset, size_t size, int flags)
1895{
1896 struct sock *sk = sock->sk;
1897 struct smc_sock *smc;
1898 int rc = -EPIPE;
1899
1900 smc = smc_sk(sk);
1901 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001902 if (sk->sk_state != SMC_ACTIVE) {
1903 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001904 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001905 }
1906 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001907 if (smc->use_fallback)
1908 rc = kernel_sendpage(smc->clcsock, page, offset,
1909 size, flags);
1910 else
1911 rc = sock_no_sendpage(sock, page, offset, size, flags);
1912
1913out:
Ursula Braunac713872017-01-09 16:55:13 +01001914 return rc;
1915}
1916
Stefan Raspl9014db22018-05-03 18:12:39 +02001917/* Map the affected portions of the rmbe into an spd, note the number of bytes
1918 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1919 * updates till whenever a respective page has been fully processed.
1920 * Note that subsequent recv() calls have to wait till all splice() processing
1921 * completed.
1922 */
Ursula Braunac713872017-01-09 16:55:13 +01001923static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1924 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001925 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001926{
1927 struct sock *sk = sock->sk;
1928 struct smc_sock *smc;
1929 int rc = -ENOTCONN;
1930
1931 smc = smc_sk(sk);
1932 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001933 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1934 /* socket was connected before, no more data to read */
1935 rc = 0;
1936 goto out;
1937 }
Stefan Raspl9014db22018-05-03 18:12:39 +02001938 if (sk->sk_state == SMC_INIT ||
1939 sk->sk_state == SMC_LISTEN ||
1940 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001941 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001942
1943 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1944 rc = 0;
1945 goto out;
1946 }
1947
Ursula Braunac713872017-01-09 16:55:13 +01001948 if (smc->use_fallback) {
1949 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1950 pipe, len, flags);
1951 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001952 if (*ppos) {
1953 rc = -ESPIPE;
1954 goto out;
1955 }
1956 if (flags & SPLICE_F_NONBLOCK)
1957 flags = MSG_DONTWAIT;
1958 else
1959 flags = 0;
1960 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001961 }
1962out:
1963 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001964
Ursula Braunac713872017-01-09 16:55:13 +01001965 return rc;
1966}
1967
1968/* must look like tcp */
1969static const struct proto_ops smc_sock_ops = {
1970 .family = PF_SMC,
1971 .owner = THIS_MODULE,
1972 .release = smc_release,
1973 .bind = smc_bind,
1974 .connect = smc_connect,
1975 .socketpair = sock_no_socketpair,
1976 .accept = smc_accept,
1977 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001978 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01001979 .ioctl = smc_ioctl,
1980 .listen = smc_listen,
1981 .shutdown = smc_shutdown,
1982 .setsockopt = smc_setsockopt,
1983 .getsockopt = smc_getsockopt,
1984 .sendmsg = smc_sendmsg,
1985 .recvmsg = smc_recvmsg,
1986 .mmap = sock_no_mmap,
1987 .sendpage = smc_sendpage,
1988 .splice_read = smc_splice_read,
1989};
1990
1991static int smc_create(struct net *net, struct socket *sock, int protocol,
1992 int kern)
1993{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001994 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001995 struct smc_sock *smc;
1996 struct sock *sk;
1997 int rc;
1998
1999 rc = -ESOCKTNOSUPPORT;
2000 if (sock->type != SOCK_STREAM)
2001 goto out;
2002
2003 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002004 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01002005 goto out;
2006
2007 rc = -ENOBUFS;
2008 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002009 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01002010 if (!sk)
2011 goto out;
2012
2013 /* create internal TCP socket for CLC handshake and fallback */
2014 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01002015 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02002016 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002017 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
2018 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01002019 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01002020 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01002021 goto out;
2022 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01002023 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
2024 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01002025
2026out:
2027 return rc;
2028}
2029
2030static const struct net_proto_family smc_sock_family_ops = {
2031 .family = PF_SMC,
2032 .owner = THIS_MODULE,
2033 .create = smc_create,
2034};
2035
Hans Wippel64e28b52019-02-21 13:01:02 +01002036unsigned int smc_net_id;
2037
2038static __net_init int smc_net_init(struct net *net)
2039{
2040 return smc_pnet_net_init(net);
2041}
2042
2043static void __net_exit smc_net_exit(struct net *net)
2044{
2045 smc_pnet_net_exit(net);
2046}
2047
2048static struct pernet_operations smc_net_ops = {
2049 .init = smc_net_init,
2050 .exit = smc_net_exit,
2051 .id = &smc_net_id,
2052 .size = sizeof(struct smc_net),
2053};
2054
Ursula Braunac713872017-01-09 16:55:13 +01002055static int __init smc_init(void)
2056{
2057 int rc;
2058
Hans Wippel64e28b52019-02-21 13:01:02 +01002059 rc = register_pernet_subsys(&smc_net_ops);
2060 if (rc)
2061 return rc;
2062
Thomas Richter6812baa2017-01-09 16:55:15 +01002063 rc = smc_pnet_init();
2064 if (rc)
YueHaibing8c33bf12019-06-26 17:47:50 +02002065 goto out_pernet_subsys;
Thomas Richter6812baa2017-01-09 16:55:15 +01002066
Ursula Braun6dabd402019-11-16 17:47:29 +01002067 rc = smc_core_init();
2068 if (rc) {
2069 pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
2070 goto out_pnet;
2071 }
2072
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002073 rc = smc_llc_init();
2074 if (rc) {
2075 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002076 goto out_core;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002077 }
2078
Ursula Braun5f083182017-01-09 16:55:22 +01002079 rc = smc_cdc_init();
2080 if (rc) {
2081 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002082 goto out_core;
Ursula Braun5f083182017-01-09 16:55:22 +01002083 }
2084
Ursula Braunac713872017-01-09 16:55:13 +01002085 rc = proto_register(&smc_proto, 1);
2086 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01002087 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002088 goto out_core;
Ursula Braunac713872017-01-09 16:55:13 +01002089 }
2090
Karsten Graulaaa4d332018-03-16 15:06:41 +01002091 rc = proto_register(&smc_proto6, 1);
2092 if (rc) {
2093 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2094 goto out_proto;
2095 }
2096
Ursula Braunac713872017-01-09 16:55:13 +01002097 rc = sock_register(&smc_sock_family_ops);
2098 if (rc) {
2099 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002100 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01002101 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01002102 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002103 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01002104
Ursula Brauna4cf0442017-01-09 16:55:14 +01002105 rc = smc_ib_register_client();
2106 if (rc) {
2107 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2108 goto out_sock;
2109 }
2110
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002111 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002112 return 0;
2113
Ursula Brauna4cf0442017-01-09 16:55:14 +01002114out_sock:
2115 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002116out_proto6:
2117 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002118out_proto:
2119 proto_unregister(&smc_proto);
Ursula Braun6dabd402019-11-16 17:47:29 +01002120out_core:
2121 smc_core_exit();
Thomas Richter6812baa2017-01-09 16:55:15 +01002122out_pnet:
2123 smc_pnet_exit();
YueHaibing8c33bf12019-06-26 17:47:50 +02002124out_pernet_subsys:
2125 unregister_pernet_subsys(&smc_net_ops);
2126
Ursula Braunac713872017-01-09 16:55:13 +01002127 return rc;
2128}
2129
2130static void __exit smc_exit(void)
2131{
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002132 static_branch_disable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002133 sock_unregister(PF_SMC);
Ursula Braun6dabd402019-11-16 17:47:29 +01002134 smc_core_exit();
2135 smc_ib_unregister_client();
Karsten Graulaaa4d332018-03-16 15:06:41 +01002136 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002137 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002138 smc_pnet_exit();
Hans Wippel64e28b52019-02-21 13:01:02 +01002139 unregister_pernet_subsys(&smc_net_ops);
Ursula Braun4ead9c92019-11-16 17:47:31 +01002140 rcu_barrier();
Ursula Braunac713872017-01-09 16:55:13 +01002141}
2142
2143module_init(smc_init);
2144module_exit(smc_exit);
2145
2146MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2147MODULE_DESCRIPTION("smc socket address family");
2148MODULE_LICENSE("GPL");
2149MODULE_ALIAS_NETPROTO(PF_SMC);