blob: cde4dc0ed17341f6397c386c49ca061ab7e9f468 [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 Graul44aa81c2018-05-15 17:04:55 +0200341static int smc_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc,
342 bool conf_rkey)
Karsten Graule63a5f82018-05-03 17:57:37 +0200343{
Karsten Graulc7674c02018-11-22 10:26:43 +0100344 if (!rmb_desc->wr_reg) {
345 /* register memory region for new rmb */
346 if (smc_wr_reg_send(link, rmb_desc->mr_rx[SMC_SINGLE_LINK])) {
347 rmb_desc->regerr = 1;
348 return -EFAULT;
349 }
350 rmb_desc->wr_reg = 1;
Karsten Graula6920d12018-05-03 17:57:38 +0200351 }
Karsten Graul44aa81c2018-05-15 17:04:55 +0200352 if (!conf_rkey)
353 return 0;
354 /* exchange confirm_rkey msg with peer */
355 if (smc_llc_do_confirm_rkey(link, rmb_desc)) {
356 rmb_desc->regerr = 1;
357 return -EFAULT;
358 }
Karsten Graule63a5f82018-05-03 17:57:37 +0200359 return 0;
360}
361
Stefan Raspl0f627122018-03-01 13:51:26 +0100362static int smc_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100363{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200364 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100365 struct smc_link_group *lgr = smc->conn.lgr;
366 struct smc_link *link;
367 int rest;
368 int rc;
369
370 link = &lgr->lnk[SMC_SINGLE_LINK];
371 /* receive CONFIRM LINK request from server over RoCE fabric */
372 rest = wait_for_completion_interruptible_timeout(
373 &link->llc_confirm,
374 SMC_LLC_WAIT_FIRST_TIME);
375 if (rest <= 0) {
376 struct smc_clc_msg_decline dclc;
377
378 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100379 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100380 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100381 }
382
Karsten Graul75d320d2018-03-01 13:51:31 +0100383 if (link->llc_confirm_rc)
384 return SMC_CLC_DECL_RMBE_EC;
385
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100386 rc = smc_ib_modify_qp_rts(link);
387 if (rc)
Karsten Graul603cc142018-07-25 16:35:32 +0200388 return SMC_CLC_DECL_ERR_RDYLNK;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100389
390 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200391
Karsten Graul44aa81c2018-05-15 17:04:55 +0200392 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200393 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200394
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100395 /* send CONFIRM LINK response over RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200396 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100397 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200398 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100399
Karsten Graul52bedf32018-03-01 13:51:32 +0100400 /* receive ADD LINK request from server over RoCE fabric */
401 rest = wait_for_completion_interruptible_timeout(&link->llc_add,
402 SMC_LLC_WAIT_TIME);
403 if (rest <= 0) {
404 struct smc_clc_msg_decline dclc;
405
406 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100407 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100408 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
Karsten Graul52bedf32018-03-01 13:51:32 +0100409 }
410
411 /* send add link reject message, only one link supported for now */
412 rc = smc_llc_send_add_link(link,
413 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +0200414 link->gid, SMC_LLC_RESP);
Karsten Graul52bedf32018-03-01 13:51:32 +0100415 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200416 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +0100417
Karsten Graul877ae5b2018-05-02 16:56:44 +0200418 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100419
Karsten Graul75d320d2018-03-01 13:51:31 +0100420 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100421}
422
Hans Wippel41349842018-06-28 19:05:11 +0200423static void smcr_conn_save_peer_info(struct smc_sock *smc,
424 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100425{
Hans Wippel95d8d2632018-05-18 09:34:13 +0200426 int bufsize = smc_uncompress_bufsize(clc->rmbe_size);
427
Hans Wippel92a138e2018-05-18 09:34:12 +0200428 smc->conn.peer_rmbe_idx = clc->rmbe_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100429 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200430 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100431 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200432 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100433}
434
Hans Wippel41349842018-06-28 19:05:11 +0200435static void smcd_conn_save_peer_info(struct smc_sock *smc,
436 struct smc_clc_msg_accept_confirm *clc)
437{
438 int bufsize = smc_uncompress_bufsize(clc->dmbe_size);
439
440 smc->conn.peer_rmbe_idx = clc->dmbe_idx;
441 smc->conn.peer_token = clc->token;
442 /* msg header takes up space in the buffer */
443 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
444 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
445 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
446}
447
448static void smc_conn_save_peer_info(struct smc_sock *smc,
449 struct smc_clc_msg_accept_confirm *clc)
450{
451 if (smc->conn.lgr->is_smcd)
452 smcd_conn_save_peer_info(smc, clc);
453 else
454 smcr_conn_save_peer_info(smc, clc);
455}
456
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100457static void smc_link_save_peer_info(struct smc_link *link,
458 struct smc_clc_msg_accept_confirm *clc)
459{
460 link->peer_qpn = ntoh24(clc->qpn);
461 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
462 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
463 link->peer_psn = ntoh24(clc->psn);
464 link->peer_mtu = clc->qp_mtu;
465}
466
Ursula Braun07603b22019-04-11 11:17:32 +0200467static void smc_switch_to_fallback(struct smc_sock *smc)
468{
469 smc->use_fallback = true;
470 if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
471 smc->clcsock->file = smc->sk.sk_socket->file;
472 smc->clcsock->file->private_data = smc->clcsock;
473 }
474}
475
Hans Wippel3b2dec22018-05-18 09:34:18 +0200476/* fall back during connect */
Karsten Graul603cc142018-07-25 16:35:32 +0200477static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200478{
Ursula Braun07603b22019-04-11 11:17:32 +0200479 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +0200480 smc->fallback_rsn = reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200481 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200482 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200483 if (smc->sk.sk_state == SMC_INIT)
484 smc->sk.sk_state = SMC_ACTIVE;
485 return 0;
486}
487
488/* decline and fall back during connect */
489static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
490{
491 int rc;
492
Ursula Braune1bbdd52018-07-05 16:15:30 +0200493 if (reason_code < 0) { /* error, fallback is not possible */
494 if (smc->sk.sk_state == SMC_INIT)
495 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200496 return reason_code;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200497 }
Karsten Graul603cc142018-07-25 16:35:32 +0200498 if (reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200499 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200500 if (rc < 0) {
501 if (smc->sk.sk_state == SMC_INIT)
502 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200503 return rc;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200504 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200505 }
Karsten Graul603cc142018-07-25 16:35:32 +0200506 return smc_connect_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200507}
508
509/* abort connecting */
510static int smc_connect_abort(struct smc_sock *smc, int reason_code,
511 int local_contact)
512{
513 if (local_contact == SMC_FIRST_CONTACT)
514 smc_lgr_forget(smc->conn.lgr);
Hans Wippel72a36a82019-02-07 15:56:18 +0100515 if (smc->conn.lgr->is_smcd)
516 /* there is only one lgr role for SMC-D; use server lock */
517 mutex_unlock(&smc_server_lgr_pending);
518 else
519 mutex_unlock(&smc_client_lgr_pending);
520
Hans Wippel3b2dec22018-05-18 09:34:18 +0200521 smc_conn_free(&smc->conn);
Ursula Braun50717a32019-04-12 12:57:23 +0200522 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200523 return reason_code;
524}
525
526/* check if there is a rdma device available for this connection. */
527/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200528static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200529{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200530 /* PNET table look up: search active ib_device and port
531 * within same PNETID that also contains the ethernet device
532 * used for the internal TCP socket
533 */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200534 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +0200535 if (!ini->ib_dev)
536 return SMC_CLC_DECL_NOSMCRDEV;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200537 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200538}
539
Hans Wippel41349842018-06-28 19:05:11 +0200540/* check if there is an ISM device available for this connection. */
541/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200542static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200543{
544 /* Find ISM device with same PNETID as connecting interface */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200545 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
546 if (!ini->ism_dev)
Karsten Graul9aa68d22019-04-12 12:57:29 +0200547 return SMC_CLC_DECL_NOSMCDDEV;
Hans Wippel41349842018-06-28 19:05:11 +0200548 return 0;
549}
550
551/* Check for VLAN ID and register it on ISM device just for CLC handshake */
552static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200553 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200554{
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200555 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev, ini->vlan_id))
Karsten Graul7a627252019-04-12 12:57:30 +0200556 return SMC_CLC_DECL_ISMVLANERR;
Hans Wippel41349842018-06-28 19:05:11 +0200557 return 0;
558}
559
560/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
561 * used, the VLAN ID will be registered again during the connection setup.
562 */
563static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200564 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200565{
566 if (!is_smcd)
567 return 0;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200568 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev, ini->vlan_id))
Hans Wippel41349842018-06-28 19:05:11 +0200569 return SMC_CLC_DECL_CNFERR;
570 return 0;
571}
572
Hans Wippel3b2dec22018-05-18 09:34:18 +0200573/* CLC handshake during connect */
Hans Wippelc758dfd2018-06-28 19:05:09 +0200574static int smc_connect_clc(struct smc_sock *smc, int smc_type,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200575 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200576 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200577{
578 int rc = 0;
579
580 /* do inband token exchange */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200581 rc = smc_clc_send_proposal(smc, smc_type, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200582 if (rc)
583 return rc;
584 /* receive SMC Accept CLC message */
Ursula Braun2b59f582018-11-22 10:26:39 +0100585 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT,
586 CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200587}
588
Ursula Brauna046d572017-01-09 16:55:16 +0100589/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200590static int smc_connect_rdma(struct smc_sock *smc,
591 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200592 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200593{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200594 struct smc_link *link;
595 int reason_code = 0;
596
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200597 ini->is_smcd = false;
598 ini->ib_lcl = &aclc->lcl;
599 ini->ib_clcqpn = ntoh24(aclc->qpn);
600 ini->srv_first_contact = aclc->hdr.flag;
601
Hans Wippel72a36a82019-02-07 15:56:18 +0100602 mutex_lock(&smc_client_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200603 reason_code = smc_conn_create(smc, ini);
604 if (reason_code) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100605 mutex_unlock(&smc_client_lgr_pending);
606 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200607 }
608 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
609
610 smc_conn_save_peer_info(smc, aclc);
611
612 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200613 if (smc_buf_create(smc, false))
Karsten Graul7a627252019-04-12 12:57:30 +0200614 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
615 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200616
Karsten Graul7a627252019-04-12 12:57:30 +0200617 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200618 smc_link_save_peer_info(link, aclc);
619
620 if (smc_rmb_rtoken_handling(&smc->conn, aclc))
Karsten Graul603cc142018-07-25 16:35:32 +0200621 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
Karsten Graul7a627252019-04-12 12:57:30 +0200622 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200623
624 smc_close_init(smc);
625 smc_rx_init(smc);
626
Karsten Graul7a627252019-04-12 12:57:30 +0200627 if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200628 if (smc_ib_ready_link(link))
Karsten Graul603cc142018-07-25 16:35:32 +0200629 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
Karsten Graul7a627252019-04-12 12:57:30 +0200630 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200631 } else {
Karsten Graulc7674c02018-11-22 10:26:43 +0100632 if (smc_reg_rmb(link, smc->conn.rmb_desc, true))
Karsten Graul603cc142018-07-25 16:35:32 +0200633 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
Karsten Graul7a627252019-04-12 12:57:30 +0200634 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200635 }
636 smc_rmb_sync_sg_for_device(&smc->conn);
637
638 reason_code = smc_clc_send_confirm(smc);
639 if (reason_code)
Karsten Graul7a627252019-04-12 12:57:30 +0200640 return smc_connect_abort(smc, reason_code,
641 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200642
643 smc_tx_init(smc);
644
Karsten Graul7a627252019-04-12 12:57:30 +0200645 if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200646 /* QP confirmation over RoCE fabric */
647 reason_code = smc_clnt_conf_first_link(smc);
648 if (reason_code)
649 return smc_connect_abort(smc, reason_code,
Karsten Graul7a627252019-04-12 12:57:30 +0200650 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200651 }
Hans Wippel72a36a82019-02-07 15:56:18 +0100652 mutex_unlock(&smc_client_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200653
654 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200655 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200656 if (smc->sk.sk_state == SMC_INIT)
657 smc->sk.sk_state = SMC_ACTIVE;
658
659 return 0;
660}
661
Hans Wippel41349842018-06-28 19:05:11 +0200662/* setup for ISM connection of client */
663static int smc_connect_ism(struct smc_sock *smc,
664 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200665 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200666{
Hans Wippel41349842018-06-28 19:05:11 +0200667 int rc = 0;
668
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200669 ini->is_smcd = true;
670 ini->ism_gid = aclc->gid;
671 ini->srv_first_contact = aclc->hdr.flag;
672
Hans Wippel72a36a82019-02-07 15:56:18 +0100673 /* there is only one lgr role for SMC-D; use server lock */
674 mutex_lock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200675 rc = smc_conn_create(smc, ini);
676 if (rc) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100677 mutex_unlock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200678 return rc;
Hans Wippel72a36a82019-02-07 15:56:18 +0100679 }
Hans Wippel41349842018-06-28 19:05:11 +0200680
681 /* Create send and receive buffers */
682 if (smc_buf_create(smc, true))
Karsten Graul7a627252019-04-12 12:57:30 +0200683 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
684 ini->cln_first_contact);
Hans Wippel41349842018-06-28 19:05:11 +0200685
686 smc_conn_save_peer_info(smc, aclc);
687 smc_close_init(smc);
688 smc_rx_init(smc);
689 smc_tx_init(smc);
690
691 rc = smc_clc_send_confirm(smc);
692 if (rc)
Karsten Graul7a627252019-04-12 12:57:30 +0200693 return smc_connect_abort(smc, rc, ini->cln_first_contact);
Hans Wippel72a36a82019-02-07 15:56:18 +0100694 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel41349842018-06-28 19:05:11 +0200695
696 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200697 smc->connect_nonblock = 0;
Hans Wippel41349842018-06-28 19:05:11 +0200698 if (smc->sk.sk_state == SMC_INIT)
699 smc->sk.sk_state = SMC_ACTIVE;
700
701 return 0;
702}
703
Hans Wippel3b2dec22018-05-18 09:34:18 +0200704/* perform steps before actually connecting */
705static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100706{
Hans Wippel41349842018-06-28 19:05:11 +0200707 bool ism_supported = false, rdma_supported = false;
Ursula Brauna046d572017-01-09 16:55:16 +0100708 struct smc_clc_msg_accept_confirm aclc;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200709 struct smc_init_info ini = {0};
Hans Wippel41349842018-06-28 19:05:11 +0200710 int smc_type;
Ursula Brauna046d572017-01-09 16:55:16 +0100711 int rc = 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100712
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200713 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +0200714 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200715
Hans Wippel3b2dec22018-05-18 09:34:18 +0200716 /* if peer has not signalled SMC-capability, fall back */
717 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +0200718 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200719
Ursula Brauna046d572017-01-09 16:55:16 +0100720 /* IPSec connections opt out of SMC-R optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200721 if (using_ipsec(smc))
722 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
Ursula Brauna046d572017-01-09 16:55:16 +0100723
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200724 /* get vlan id from IP device */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200725 if (smc_vlan_by_tcpsk(smc->clcsock, &ini))
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200726 return smc_connect_decline_fallback(smc,
727 SMC_CLC_DECL_GETVLANERR);
Hans Wippel41349842018-06-28 19:05:11 +0200728
729 /* check if there is an ism device available */
Karsten Graul228bae02019-04-12 12:57:28 +0200730 if (!smc_find_ism_device(smc, &ini) &&
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200731 !smc_connect_ism_vlan_setup(smc, &ini)) {
Hans Wippel41349842018-06-28 19:05:11 +0200732 /* ISM is supported for this connection */
733 ism_supported = true;
734 smc_type = SMC_TYPE_D;
735 }
736
737 /* check if there is a rdma device available */
Karsten Graul228bae02019-04-12 12:57:28 +0200738 if (!smc_find_rdma_device(smc, &ini)) {
Hans Wippel41349842018-06-28 19:05:11 +0200739 /* RDMA is supported for this connection */
740 rdma_supported = true;
741 if (ism_supported)
742 smc_type = SMC_TYPE_B; /* both */
743 else
744 smc_type = SMC_TYPE_R; /* only RDMA */
745 }
746
747 /* if neither ISM nor RDMA are supported, fallback */
748 if (!rdma_supported && !ism_supported)
Karsten Graul603cc142018-07-25 16:35:32 +0200749 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV);
Ursula Brauna046d572017-01-09 16:55:16 +0100750
Hans Wippel3b2dec22018-05-18 09:34:18 +0200751 /* perform CLC handshake */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200752 rc = smc_connect_clc(smc, smc_type, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200753 if (rc) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200754 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200755 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200756 }
Ursula Brauna046d572017-01-09 16:55:16 +0100757
Hans Wippel41349842018-06-28 19:05:11 +0200758 /* depending on previous steps, connect using rdma or ism */
759 if (rdma_supported && aclc.hdr.path == SMC_TYPE_R)
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200760 rc = smc_connect_rdma(smc, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200761 else if (ism_supported && aclc.hdr.path == SMC_TYPE_D)
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200762 rc = smc_connect_ism(smc, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200763 else
Karsten Graul603cc142018-07-25 16:35:32 +0200764 rc = SMC_CLC_DECL_MODEUNSUPP;
Hans Wippel41349842018-06-28 19:05:11 +0200765 if (rc) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200766 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200767 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200768 }
Ursula Brauna046d572017-01-09 16:55:16 +0100769
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200770 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200771 return 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100772}
773
Ursula Braun24ac3a02018-06-27 17:59:50 +0200774static void smc_connect_work(struct work_struct *work)
775{
776 struct smc_sock *smc = container_of(work, struct smc_sock,
777 connect_work);
Ursula Braun50717a32019-04-12 12:57:23 +0200778 long timeo = smc->sk.sk_sndtimeo;
779 int rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200780
Ursula Braun50717a32019-04-12 12:57:23 +0200781 if (!timeo)
782 timeo = MAX_SCHEDULE_TIMEOUT;
783 lock_sock(smc->clcsock->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200784 if (smc->clcsock->sk->sk_err) {
785 smc->sk.sk_err = smc->clcsock->sk->sk_err;
Ursula Braun50717a32019-04-12 12:57:23 +0200786 } else if ((1 << smc->clcsock->sk->sk_state) &
787 (TCPF_SYN_SENT | TCP_SYN_RECV)) {
788 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
789 if ((rc == -EPIPE) &&
790 ((1 << smc->clcsock->sk->sk_state) &
791 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
792 rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200793 }
Ursula Braun50717a32019-04-12 12:57:23 +0200794 release_sock(smc->clcsock->sk);
795 lock_sock(&smc->sk);
796 if (rc != 0 || smc->sk.sk_err) {
797 smc->sk.sk_state = SMC_CLOSED;
798 if (rc == -EPIPE || rc == -EAGAIN)
799 smc->sk.sk_err = EPIPE;
800 else if (signal_pending(current))
801 smc->sk.sk_err = -sock_intr_errno(timeo);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200802 goto out;
803 }
804
805 rc = __smc_connect(smc);
806 if (rc < 0)
807 smc->sk.sk_err = -rc;
808
809out:
Ursula Braun07603b22019-04-11 11:17:32 +0200810 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
811 if (smc->sk.sk_err) {
812 smc->sk.sk_state_change(&smc->sk);
813 } else { /* allow polling before and after fallback decision */
814 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
815 smc->sk.sk_write_space(&smc->sk);
816 }
817 }
Ursula Braun24ac3a02018-06-27 17:59:50 +0200818 release_sock(&smc->sk);
819}
820
Ursula Braunac713872017-01-09 16:55:13 +0100821static int smc_connect(struct socket *sock, struct sockaddr *addr,
822 int alen, int flags)
823{
824 struct sock *sk = sock->sk;
825 struct smc_sock *smc;
826 int rc = -EINVAL;
827
828 smc = smc_sk(sk);
829
830 /* separate smc parameter checking to be safe */
831 if (alen < sizeof(addr->sa_family))
832 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100833 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100834 goto out_err;
835
836 lock_sock(sk);
837 switch (sk->sk_state) {
838 default:
839 goto out;
840 case SMC_ACTIVE:
841 rc = -EISCONN;
842 goto out;
843 case SMC_INIT:
844 rc = 0;
845 break;
846 }
847
848 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200849 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun50717a32019-04-12 12:57:23 +0200850 if (smc->connect_nonblock) {
851 rc = -EALREADY;
852 goto out;
853 }
854 rc = kernel_connect(smc->clcsock, addr, alen, flags);
855 if (rc && rc != -EINPROGRESS)
856 goto out;
Ursula Braun301428e2019-10-29 12:41:26 +0100857
858 sock_hold(&smc->sk); /* sock put in passive closing */
Ursula Braun24ac3a02018-06-27 17:59:50 +0200859 if (flags & O_NONBLOCK) {
Ursula Braun50717a32019-04-12 12:57:23 +0200860 if (schedule_work(&smc->connect_work))
861 smc->connect_nonblock = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200862 rc = -EINPROGRESS;
863 } else {
Ursula Braun24ac3a02018-06-27 17:59:50 +0200864 rc = __smc_connect(smc);
865 if (rc < 0)
866 goto out;
867 else
868 rc = 0; /* success cases including fallback */
869 }
Ursula Braunac713872017-01-09 16:55:13 +0100870
871out:
872 release_sock(sk);
873out_err:
874 return rc;
875}
876
877static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
878{
Ursula Braun3163c502018-01-24 10:28:12 +0100879 struct socket *new_clcsock = NULL;
880 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100881 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -0800882 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +0100883
Ursula Braun3163c502018-01-24 10:28:12 +0100884 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100885 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100886 if (!new_sk) {
887 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100888 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100889 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100890 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100891 goto out;
892 }
893 *new_smc = smc_sk(new_sk);
894
Myungho Jung78abe3d2018-12-18 09:02:25 -0800895 mutex_lock(&lsmc->clcsock_release_lock);
896 if (lsmc->clcsock)
897 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
898 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +0100899 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100900 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100901 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100902 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +0200903 new_sk->sk_prot->unhash(new_sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100904 if (new_clcsock)
905 sock_release(new_clcsock);
906 new_sk->sk_state = SMC_CLOSED;
907 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun51f1de72018-01-26 09:28:48 +0100908 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100909 *new_smc = NULL;
910 goto out;
911 }
912
913 (*new_smc)->clcsock = new_clcsock;
914out:
915 return rc;
916}
917
Ursula Brauna046d572017-01-09 16:55:16 +0100918/* add a just created sock to the accept queue of the listen sock as
919 * candidate for a following socket accept call from user space
920 */
921static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
922{
923 struct smc_sock *par = smc_sk(parent);
924
Ursula Braun51f1de72018-01-26 09:28:48 +0100925 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100926 spin_lock(&par->accept_q_lock);
927 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
928 spin_unlock(&par->accept_q_lock);
929 sk_acceptq_added(parent);
930}
931
932/* remove a socket from the accept queue of its parental listening socket */
933static void smc_accept_unlink(struct sock *sk)
934{
935 struct smc_sock *par = smc_sk(sk)->listen_smc;
936
937 spin_lock(&par->accept_q_lock);
938 list_del_init(&smc_sk(sk)->accept_q);
939 spin_unlock(&par->accept_q_lock);
940 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100941 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100942}
943
944/* remove a sock from the accept queue to bind it to a new socket created
945 * for a socket accept call from user space
946 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100947struct sock *smc_accept_dequeue(struct sock *parent,
948 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100949{
950 struct smc_sock *isk, *n;
951 struct sock *new_sk;
952
953 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
954 new_sk = (struct sock *)isk;
955
956 smc_accept_unlink(new_sk);
957 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +0200958 new_sk->sk_prot->unhash(new_sk);
Ursula Braun127f4972018-01-26 09:28:49 +0100959 if (isk->clcsock) {
960 sock_release(isk->clcsock);
961 isk->clcsock = NULL;
962 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100963 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +0100964 continue;
965 }
Ursula Braun07603b22019-04-11 11:17:32 +0200966 if (new_sock) {
Ursula Brauna046d572017-01-09 16:55:16 +0100967 sock_graft(new_sk, new_sock);
Ursula Braun07603b22019-04-11 11:17:32 +0200968 if (isk->use_fallback) {
969 smc_sk(new_sk)->clcsock->file = new_sock->file;
970 isk->clcsock->file->private_data = isk->clcsock;
971 }
972 }
Ursula Brauna046d572017-01-09 16:55:16 +0100973 return new_sk;
974 }
975 return NULL;
976}
977
978/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +0100979void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +0100980{
981 struct smc_sock *smc = smc_sk(sk);
982
Ursula Braun81cf4f42019-10-21 16:13:15 +0200983 sock_hold(sk); /* sock_put below */
Ursula Braunb38d7322017-01-09 16:55:25 +0100984 lock_sock(sk);
985 if (!sk->sk_lingertime)
986 /* wait for peer closing */
987 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun39f41f32019-06-27 15:04:52 +0200988 __smc_release(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +0100989 release_sock(sk);
Ursula Braun81cf4f42019-10-21 16:13:15 +0200990 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +0100991 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +0100992}
993
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100994static int smc_serv_conf_first_link(struct smc_sock *smc)
995{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200996 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100997 struct smc_link_group *lgr = smc->conn.lgr;
998 struct smc_link *link;
999 int rest;
1000 int rc;
1001
1002 link = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun652a1e42017-07-28 13:56:17 +02001003
Karsten Graul44aa81c2018-05-15 17:04:55 +02001004 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +02001005 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +02001006
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001007 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +02001008 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001009 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001010 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001011
1012 /* receive CONFIRM LINK response from client over the RoCE fabric */
1013 rest = wait_for_completion_interruptible_timeout(
1014 &link->llc_confirm_resp,
1015 SMC_LLC_WAIT_FIRST_TIME);
1016 if (rest <= 0) {
1017 struct smc_clc_msg_decline dclc;
1018
1019 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001020 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001021 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001022 }
1023
Karsten Graul75d320d2018-03-01 13:51:31 +01001024 if (link->llc_confirm_resp_rc)
1025 return SMC_CLC_DECL_RMBE_EC;
1026
Karsten Graul52bedf32018-03-01 13:51:32 +01001027 /* send ADD LINK request to client over the RoCE fabric */
1028 rc = smc_llc_send_add_link(link,
1029 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +02001030 link->gid, SMC_LLC_REQ);
Karsten Graul52bedf32018-03-01 13:51:32 +01001031 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001032 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +01001033
1034 /* receive ADD LINK response from client over the RoCE fabric */
1035 rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
1036 SMC_LLC_WAIT_TIME);
1037 if (rest <= 0) {
1038 struct smc_clc_msg_decline dclc;
1039
1040 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001041 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001042 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
Karsten Graul52bedf32018-03-01 13:51:32 +01001043 }
1044
Karsten Graul877ae5b2018-05-02 16:56:44 +02001045 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +01001046
Karsten Graul75d320d2018-03-01 13:51:31 +01001047 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001048}
1049
Hans Wippel3b2dec22018-05-18 09:34:18 +02001050/* listen worker: finish */
1051static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001052{
Ursula Brauna046d572017-01-09 16:55:16 +01001053 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001054 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001055
Ursula Brauna046d572017-01-09 16:55:16 +01001056 if (lsmc->sk.sk_state == SMC_LISTEN) {
Karsten Graulfd577702019-04-11 11:17:30 +02001057 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001058 smc_accept_enqueue(&lsmc->sk, newsmcsk);
Karsten Graulfd577702019-04-11 11:17:30 +02001059 release_sock(&lsmc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001060 } else { /* no longer listening */
1061 smc_close_non_accepted(newsmcsk);
1062 }
Ursula Brauna046d572017-01-09 16:55:16 +01001063
1064 /* Wake up accept */
1065 lsmc->sk.sk_data_ready(&lsmc->sk);
1066 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001067}
Ursula Brauna046d572017-01-09 16:55:16 +01001068
Hans Wippel3b2dec22018-05-18 09:34:18 +02001069/* listen worker: finish in state connected */
1070static void smc_listen_out_connected(struct smc_sock *new_smc)
1071{
1072 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001073
Hans Wippel3b2dec22018-05-18 09:34:18 +02001074 sk_refcnt_debug_inc(newsmcsk);
1075 if (newsmcsk->sk_state == SMC_INIT)
1076 newsmcsk->sk_state = SMC_ACTIVE;
1077
1078 smc_listen_out(new_smc);
1079}
1080
1081/* listen worker: finish in error state */
1082static void smc_listen_out_err(struct smc_sock *new_smc)
1083{
1084 struct sock *newsmcsk = &new_smc->sk;
1085
Ursula Braun51f1de72018-01-26 09:28:48 +01001086 if (newsmcsk->sk_state == SMC_INIT)
1087 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001088 newsmcsk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001089 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001090
1091 smc_listen_out(new_smc);
1092}
1093
1094/* listen worker: decline and fall back if possible */
1095static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1096 int local_contact)
1097{
1098 /* RDMA setup failed, switch back to TCP */
1099 if (local_contact == SMC_FIRST_CONTACT)
1100 smc_lgr_forget(new_smc->conn.lgr);
1101 if (reason_code < 0) { /* error, no fallback possible */
1102 smc_listen_out_err(new_smc);
1103 return;
1104 }
1105 smc_conn_free(&new_smc->conn);
Ursula Braun07603b22019-04-11 11:17:32 +02001106 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001107 new_smc->fallback_rsn = reason_code;
1108 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001109 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1110 smc_listen_out_err(new_smc);
1111 return;
1112 }
1113 }
1114 smc_listen_out_connected(new_smc);
1115}
1116
1117/* listen worker: check prefixes */
Karsten Graul59886692019-04-12 12:57:25 +02001118static int smc_listen_prfx_check(struct smc_sock *new_smc,
Hans Wippel3b2dec22018-05-18 09:34:18 +02001119 struct smc_clc_msg_proposal *pclc)
1120{
1121 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1122 struct socket *newclcsock = new_smc->clcsock;
1123
1124 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1125 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
Karsten Graul59886692019-04-12 12:57:25 +02001126 return SMC_CLC_DECL_DIFFPREFIX;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001127
1128 return 0;
1129}
1130
1131/* listen worker: initialize connection and buffers */
1132static int smc_listen_rdma_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001133 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001134{
Karsten Graul7a627252019-04-12 12:57:30 +02001135 int rc;
1136
Hans Wippel3b2dec22018-05-18 09:34:18 +02001137 /* allocate connection / link group */
Karsten Graul7a627252019-04-12 12:57:30 +02001138 rc = smc_conn_create(new_smc, ini);
1139 if (rc)
1140 return rc;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001141
1142 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001143 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001144 return SMC_CLC_DECL_MEM;
1145
1146 return 0;
1147}
1148
Hans Wippel41349842018-06-28 19:05:11 +02001149/* listen worker: initialize connection and buffers for SMC-D */
1150static int smc_listen_ism_init(struct smc_sock *new_smc,
1151 struct smc_clc_msg_proposal *pclc,
Karsten Graul7a627252019-04-12 12:57:30 +02001152 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +02001153{
1154 struct smc_clc_msg_smcd *pclc_smcd;
Karsten Graul7a627252019-04-12 12:57:30 +02001155 int rc;
Hans Wippel41349842018-06-28 19:05:11 +02001156
1157 pclc_smcd = smc_get_clc_msg_smcd(pclc);
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001158 ini->ism_gid = pclc_smcd->gid;
Karsten Graul7a627252019-04-12 12:57:30 +02001159 rc = smc_conn_create(new_smc, ini);
1160 if (rc)
1161 return rc;
Hans Wippel41349842018-06-28 19:05:11 +02001162
1163 /* Check if peer can be reached via ISM device */
1164 if (smc_ism_cantalk(new_smc->conn.lgr->peer_gid,
1165 new_smc->conn.lgr->vlan_id,
1166 new_smc->conn.lgr->smcd)) {
Karsten Graul7a627252019-04-12 12:57:30 +02001167 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Hans Wippel41349842018-06-28 19:05:11 +02001168 smc_lgr_forget(new_smc->conn.lgr);
1169 smc_conn_free(&new_smc->conn);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001170 return SMC_CLC_DECL_SMCDNOTALK;
Hans Wippel41349842018-06-28 19:05:11 +02001171 }
1172
1173 /* Create send and receive buffers */
1174 if (smc_buf_create(new_smc, true)) {
Karsten Graul7a627252019-04-12 12:57:30 +02001175 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Hans Wippel41349842018-06-28 19:05:11 +02001176 smc_lgr_forget(new_smc->conn.lgr);
1177 smc_conn_free(&new_smc->conn);
1178 return SMC_CLC_DECL_MEM;
1179 }
1180
1181 return 0;
1182}
1183
Hans Wippel3b2dec22018-05-18 09:34:18 +02001184/* listen worker: register buffers */
1185static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
1186{
1187 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1188
1189 if (local_contact != SMC_FIRST_CONTACT) {
Karsten Graulc7674c02018-11-22 10:26:43 +01001190 if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true))
1191 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001192 }
1193 smc_rmb_sync_sg_for_device(&new_smc->conn);
1194
1195 return 0;
1196}
1197
1198/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001199static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1200 struct smc_clc_msg_accept_confirm *cclc,
1201 int local_contact)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001202{
1203 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1204 int reason_code = 0;
1205
1206 if (local_contact == SMC_FIRST_CONTACT)
1207 smc_link_save_peer_info(link, cclc);
1208
1209 if (smc_rmb_rtoken_handling(&new_smc->conn, cclc)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001210 reason_code = SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001211 goto decline;
1212 }
1213
1214 if (local_contact == SMC_FIRST_CONTACT) {
1215 if (smc_ib_ready_link(link)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001216 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001217 goto decline;
1218 }
1219 /* QP confirmation over RoCE fabric */
1220 reason_code = smc_serv_conf_first_link(new_smc);
1221 if (reason_code)
1222 goto decline;
1223 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001224 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001225
1226decline:
Hans Wippel3b2dec22018-05-18 09:34:18 +02001227 smc_listen_decline(new_smc, reason_code, local_contact);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001228 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001229}
1230
1231/* setup for RDMA connection of server */
1232static void smc_listen_work(struct work_struct *work)
1233{
1234 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1235 smc_listen_work);
1236 struct socket *newclcsock = new_smc->clcsock;
1237 struct smc_clc_msg_accept_confirm cclc;
1238 struct smc_clc_msg_proposal *pclc;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001239 struct smc_init_info ini = {0};
Hans Wippel41349842018-06-28 19:05:11 +02001240 bool ism_supported = false;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001241 u8 buf[SMC_CLC_MAX_LEN];
Hans Wippel3b2dec22018-05-18 09:34:18 +02001242 int rc = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001243
Karsten Graulfd577702019-04-11 11:17:30 +02001244 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1245 return smc_listen_out_err(new_smc);
1246
Hans Wippel3b2dec22018-05-18 09:34:18 +02001247 if (new_smc->use_fallback) {
1248 smc_listen_out_connected(new_smc);
1249 return;
1250 }
1251
1252 /* check if peer is smc capable */
1253 if (!tcp_sk(newclcsock->sk)->syn_smc) {
Ursula Braun07603b22019-04-11 11:17:32 +02001254 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001255 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001256 smc_listen_out_connected(new_smc);
1257 return;
1258 }
1259
1260 /* do inband token exchange -
1261 * wait for and receive SMC Proposal CLC message
1262 */
1263 pclc = (struct smc_clc_msg_proposal *)&buf;
Karsten Graul228bae02019-04-12 12:57:28 +02001264 rc = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
1265 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001266 if (rc)
1267 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001268
1269 /* IPSec connections opt out of SMC-R optimizations */
1270 if (using_ipsec(new_smc)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001271 rc = SMC_CLC_DECL_IPSEC;
1272 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001273 }
1274
Karsten Graul59886692019-04-12 12:57:25 +02001275 /* check for matching IP prefix and subnet length */
1276 rc = smc_listen_prfx_check(new_smc, pclc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001277 if (rc)
1278 goto out_decl;
Karsten Graul59886692019-04-12 12:57:25 +02001279
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001280 /* get vlan id from IP device */
1281 if (smc_vlan_by_tcpsk(new_smc->clcsock, &ini)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001282 rc = SMC_CLC_DECL_GETVLANERR;
1283 goto out_decl;
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001284 }
1285
Hans Wippel72a36a82019-02-07 15:56:18 +01001286 mutex_lock(&smc_server_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001287 smc_close_init(new_smc);
1288 smc_rx_init(new_smc);
1289 smc_tx_init(new_smc);
1290
Hans Wippel41349842018-06-28 19:05:11 +02001291 /* check if ISM is available */
Karsten Graul9aa68d22019-04-12 12:57:29 +02001292 if (pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) {
1293 ini.is_smcd = true; /* prepare ISM check */
1294 rc = smc_find_ism_device(new_smc, &ini);
1295 if (!rc)
Karsten Graul7a627252019-04-12 12:57:30 +02001296 rc = smc_listen_ism_init(new_smc, pclc, &ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001297 if (!rc)
1298 ism_supported = true;
1299 else if (pclc->hdr.path == SMC_TYPE_D)
1300 goto out_unlock; /* skip RDMA and decline */
1301 }
1302
1303 /* check if RDMA is available */
1304 if (!ism_supported) { /* SMC_TYPE_R or SMC_TYPE_B */
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001305 /* prepare RDMA check */
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001306 ini.is_smcd = false;
Ursula Braunca5f8d22019-10-23 15:44:06 +02001307 ini.ism_dev = NULL;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001308 ini.ib_lcl = &pclc->lcl;
Karsten Graul9aa68d22019-04-12 12:57:29 +02001309 rc = smc_find_rdma_device(new_smc, &ini);
1310 if (rc) {
1311 /* no RDMA device found */
1312 if (pclc->hdr.path == SMC_TYPE_B)
1313 /* neither ISM nor RDMA device found */
1314 rc = SMC_CLC_DECL_NOSMCDEV;
1315 goto out_unlock;
1316 }
Karsten Graul7a627252019-04-12 12:57:30 +02001317 rc = smc_listen_rdma_init(new_smc, &ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001318 if (rc)
1319 goto out_unlock;
Karsten Graul7a627252019-04-12 12:57:30 +02001320 rc = smc_listen_rdma_reg(new_smc, ini.cln_first_contact);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001321 if (rc)
1322 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001323 }
1324
1325 /* send SMC Accept CLC message */
Karsten Graul7a627252019-04-12 12:57:30 +02001326 rc = smc_clc_send_accept(new_smc, ini.cln_first_contact);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001327 if (rc)
1328 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001329
Hans Wippel62c71392019-02-07 15:56:17 +01001330 /* SMC-D does not need this lock any more */
1331 if (ism_supported)
Hans Wippel72a36a82019-02-07 15:56:18 +01001332 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001333
Hans Wippel3b2dec22018-05-18 09:34:18 +02001334 /* receive SMC Confirm CLC message */
Karsten Graul228bae02019-04-12 12:57:28 +02001335 rc = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
1336 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
1337 if (rc) {
Hans Wippel62c71392019-02-07 15:56:17 +01001338 if (!ism_supported)
Karsten Graul9aa68d22019-04-12 12:57:29 +02001339 goto out_unlock;
1340 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001341 }
1342
1343 /* finish worker */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001344 if (!ism_supported) {
Karsten Graul7a627252019-04-12 12:57:30 +02001345 rc = smc_listen_rdma_finish(new_smc, &cclc,
1346 ini.cln_first_contact);
Hans Wippel72a36a82019-02-07 15:56:18 +01001347 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001348 if (rc)
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001349 return;
1350 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001351 smc_conn_save_peer_info(new_smc, &cclc);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001352 smc_listen_out_connected(new_smc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001353 return;
1354
1355out_unlock:
1356 mutex_unlock(&smc_server_lgr_pending);
1357out_decl:
Karsten Graul7a627252019-04-12 12:57:30 +02001358 smc_listen_decline(new_smc, rc, ini.cln_first_contact);
Ursula Brauna046d572017-01-09 16:55:16 +01001359}
1360
1361static void smc_tcp_listen_work(struct work_struct *work)
1362{
1363 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1364 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001365 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001366 struct smc_sock *new_smc;
1367 int rc = 0;
1368
Ursula Braun3163c502018-01-24 10:28:12 +01001369 lock_sock(lsk);
1370 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001371 rc = smc_clcsock_accept(lsmc, &new_smc);
1372 if (rc)
1373 goto out;
1374 if (!new_smc)
1375 continue;
1376
1377 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001378 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001379 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001380 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001381 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1382 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001383 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1384 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001385 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1386 if (!schedule_work(&new_smc->smc_listen_work))
1387 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001388 }
1389
1390out:
Ursula Braun3163c502018-01-24 10:28:12 +01001391 release_sock(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001392 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +01001393}
1394
Ursula Braunac713872017-01-09 16:55:13 +01001395static int smc_listen(struct socket *sock, int backlog)
1396{
1397 struct sock *sk = sock->sk;
1398 struct smc_sock *smc;
1399 int rc;
1400
1401 smc = smc_sk(sk);
1402 lock_sock(sk);
1403
1404 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +02001405 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
1406 smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +01001407 goto out;
1408
1409 rc = 0;
1410 if (sk->sk_state == SMC_LISTEN) {
1411 sk->sk_max_ack_backlog = backlog;
1412 goto out;
1413 }
1414 /* some socket options are handled in core, so we could not apply
1415 * them to the clc socket -- copy smc socket options to clc socket
1416 */
1417 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001418 if (!smc->use_fallback)
1419 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001420
1421 rc = kernel_listen(smc->clcsock, backlog);
1422 if (rc)
1423 goto out;
1424 sk->sk_max_ack_backlog = backlog;
1425 sk->sk_ack_backlog = 0;
1426 sk->sk_state = SMC_LISTEN;
Ursula Braun51f1de72018-01-26 09:28:48 +01001427 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1428 if (!schedule_work(&smc->tcp_listen_work))
1429 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001430
1431out:
1432 release_sock(sk);
1433 return rc;
1434}
1435
1436static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001437 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001438{
Ursula Brauna046d572017-01-09 16:55:16 +01001439 struct sock *sk = sock->sk, *nsk;
1440 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001441 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001442 long timeo;
1443 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001444
1445 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001446 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001447 lock_sock(sk);
1448
1449 if (lsmc->sk.sk_state != SMC_LISTEN) {
1450 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001451 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001452 goto out;
1453 }
1454
Ursula Brauna046d572017-01-09 16:55:16 +01001455 /* Wait for an incoming connection */
1456 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1457 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1458 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1459 set_current_state(TASK_INTERRUPTIBLE);
1460 if (!timeo) {
1461 rc = -EAGAIN;
1462 break;
1463 }
1464 release_sock(sk);
1465 timeo = schedule_timeout(timeo);
1466 /* wakeup by sk_data_ready in smc_listen_work() */
1467 sched_annotate_sleep();
1468 lock_sock(sk);
1469 if (signal_pending(current)) {
1470 rc = sock_intr_errno(timeo);
1471 break;
1472 }
1473 }
1474 set_current_state(TASK_RUNNING);
1475 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001476
Ursula Brauna046d572017-01-09 16:55:16 +01001477 if (!rc)
1478 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001479 release_sock(sk);
1480 if (rc)
1481 goto out;
1482
1483 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1484 /* wait till data arrives on the socket */
1485 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1486 MSEC_PER_SEC);
1487 if (smc_sk(nsk)->use_fallback) {
1488 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1489
1490 lock_sock(clcsk);
1491 if (skb_queue_empty(&clcsk->sk_receive_queue))
1492 sk_wait_data(clcsk, &timeo, NULL);
1493 release_sock(clcsk);
1494 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1495 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001496 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001497 release_sock(nsk);
1498 }
1499 }
Ursula Braunac713872017-01-09 16:55:13 +01001500
1501out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001502 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001503 return rc;
1504}
1505
1506static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001507 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001508{
1509 struct smc_sock *smc;
1510
Ursula Braunb38d7322017-01-09 16:55:25 +01001511 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1512 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001513 return -ENOTCONN;
1514
1515 smc = smc_sk(sock->sk);
1516
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001517 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001518}
1519
1520static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1521{
1522 struct sock *sk = sock->sk;
1523 struct smc_sock *smc;
1524 int rc = -EPIPE;
1525
1526 smc = smc_sk(sk);
1527 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001528 if ((sk->sk_state != SMC_ACTIVE) &&
1529 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1530 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001531 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001532
1533 if (msg->msg_flags & MSG_FASTOPEN) {
Ursula Brauncd206362019-08-02 10:47:50 +02001534 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001535 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001536 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001537 } else {
1538 rc = -EINVAL;
1539 goto out;
1540 }
1541 }
1542
Ursula Braunac713872017-01-09 16:55:13 +01001543 if (smc->use_fallback)
1544 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1545 else
Ursula Braune6727f32017-01-09 16:55:23 +01001546 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001547out:
1548 release_sock(sk);
1549 return rc;
1550}
1551
1552static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1553 int flags)
1554{
1555 struct sock *sk = sock->sk;
1556 struct smc_sock *smc;
1557 int rc = -ENOTCONN;
1558
1559 smc = smc_sk(sk);
1560 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001561 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1562 /* socket was connected before, no more data to read */
1563 rc = 0;
1564 goto out;
1565 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001566 if ((sk->sk_state == SMC_INIT) ||
1567 (sk->sk_state == SMC_LISTEN) ||
1568 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001569 goto out;
1570
Ursula Braunb38d7322017-01-09 16:55:25 +01001571 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1572 rc = 0;
1573 goto out;
1574 }
1575
Stefan Raspl9014db22018-05-03 18:12:39 +02001576 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001577 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001578 } else {
1579 msg->msg_namelen = 0;
1580 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1581 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001582
Ursula Braunac713872017-01-09 16:55:13 +01001583out:
1584 release_sock(sk);
1585 return rc;
1586}
1587
Al Viroade994f2017-07-03 00:01:49 -04001588static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001589{
Ursula Braun8dce2782018-01-26 09:28:47 +01001590 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001591 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001592
Ursula Braun8dce2782018-01-26 09:28:47 +01001593 spin_lock(&isk->accept_q_lock);
1594 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001595 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001596 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001597
Ursula Braun8dce2782018-01-26 09:28:47 +01001598 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001599}
1600
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001601static __poll_t smc_poll(struct file *file, struct socket *sock,
1602 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01001603{
1604 struct sock *sk = sock->sk;
Ursula Braunac713872017-01-09 16:55:13 +01001605 struct smc_sock *smc;
Ursula Braun50717a32019-04-12 12:57:23 +02001606 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001607
Ursula Braun8dce2782018-01-26 09:28:47 +01001608 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001609 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001610
Ursula Braunac713872017-01-09 16:55:13 +01001611 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02001612 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01001613 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001614 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02001615 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braunac713872017-01-09 16:55:13 +01001616 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07001617 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02001618 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01001619 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001620 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001621 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1622 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001623 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001624 if (sk->sk_state == SMC_LISTEN) {
1625 /* woken up by sk_data_ready in smc_listen_work() */
Ursula Braun50717a32019-04-12 12:57:23 +02001626 mask |= smc_accept_poll(sk);
1627 } else if (smc->use_fallback) { /* as result of connect_work()*/
1628 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
1629 wait);
1630 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun8dce2782018-01-26 09:28:47 +01001631 } else {
Ursula Braun50717a32019-04-12 12:57:23 +02001632 if ((sk->sk_state != SMC_INIT &&
1633 atomic_read(&smc->conn.sndbuf_space)) ||
Ursula Braun8dce2782018-01-26 09:28:47 +01001634 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001635 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001636 } else {
1637 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1638 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1639 }
1640 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001641 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001642 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001643 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001644 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001645 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02001646 if (smc->conn.urg_state == SMC_URG_VALID)
1647 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01001648 }
Ursula Braunac713872017-01-09 16:55:13 +01001649 }
1650
1651 return mask;
1652}
1653
1654static int smc_shutdown(struct socket *sock, int how)
1655{
1656 struct sock *sk = sock->sk;
1657 struct smc_sock *smc;
1658 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001659 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001660
1661 smc = smc_sk(sk);
1662
1663 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001664 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001665
1666 lock_sock(sk);
1667
1668 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02001669 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01001670 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1671 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1672 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1673 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1674 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001675 goto out;
1676 if (smc->use_fallback) {
1677 rc = kernel_sock_shutdown(smc->clcsock, how);
1678 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1679 if (sk->sk_shutdown == SHUTDOWN_MASK)
1680 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001681 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001682 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001683 switch (how) {
1684 case SHUT_RDWR: /* shutdown in both directions */
1685 rc = smc_close_active(smc);
1686 break;
1687 case SHUT_WR:
1688 rc = smc_close_shutdown_write(smc);
1689 break;
1690 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001691 rc = 0;
1692 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001693 break;
1694 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001695 if (smc->clcsock)
1696 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001697 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1698 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001699
1700out:
1701 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001702 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001703}
1704
1705static int smc_setsockopt(struct socket *sock, int level, int optname,
1706 char __user *optval, unsigned int optlen)
1707{
1708 struct sock *sk = sock->sk;
1709 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001710 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001711
1712 smc = smc_sk(sk);
1713
1714 /* generic setsockopts reaching us here always apply to the
1715 * CLC socket
1716 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001717 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1718 optval, optlen);
1719 if (smc->clcsock->sk->sk_err) {
1720 sk->sk_err = smc->clcsock->sk->sk_err;
1721 sk->sk_error_report(sk);
1722 }
1723 if (rc)
1724 return rc;
1725
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001726 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00001727 return -EINVAL;
Ursula Braunac0107e2018-07-18 15:22:50 +02001728 if (get_user(val, (int __user *)optval))
1729 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001730
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001731 lock_sock(sk);
1732 switch (optname) {
1733 case TCP_ULP:
1734 case TCP_FASTOPEN:
1735 case TCP_FASTOPEN_CONNECT:
1736 case TCP_FASTOPEN_KEY:
1737 case TCP_FASTOPEN_NO_COOKIE:
1738 /* option not supported by SMC */
1739 if (sk->sk_state == SMC_INIT) {
Ursula Braun07603b22019-04-11 11:17:32 +02001740 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001741 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001742 } else {
1743 if (!smc->use_fallback)
1744 rc = -EINVAL;
1745 }
1746 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001747 case TCP_NODELAY:
Ursula Braunf9cedf12019-08-02 10:16:38 +02001748 if (sk->sk_state != SMC_INIT &&
1749 sk->sk_state != SMC_LISTEN &&
1750 sk->sk_state != SMC_CLOSED) {
Ursula Braun569bc642018-05-15 17:04:54 +02001751 if (val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001752 mod_delayed_work(system_wq, &smc->conn.tx_work,
1753 0);
1754 }
1755 break;
1756 case TCP_CORK:
Ursula Braunf9cedf12019-08-02 10:16:38 +02001757 if (sk->sk_state != SMC_INIT &&
1758 sk->sk_state != SMC_LISTEN &&
1759 sk->sk_state != SMC_CLOSED) {
Ursula Braun569bc642018-05-15 17:04:54 +02001760 if (!val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001761 mod_delayed_work(system_wq, &smc->conn.tx_work,
1762 0);
1763 }
1764 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001765 case TCP_DEFER_ACCEPT:
1766 smc->sockopt_defer_accept = val;
1767 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001768 default:
1769 break;
1770 }
1771 release_sock(sk);
1772
1773 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001774}
1775
1776static int smc_getsockopt(struct socket *sock, int level, int optname,
1777 char __user *optval, int __user *optlen)
1778{
1779 struct smc_sock *smc;
1780
1781 smc = smc_sk(sock->sk);
1782 /* socket options apply to the CLC socket */
1783 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1784 optval, optlen);
1785}
1786
1787static int smc_ioctl(struct socket *sock, unsigned int cmd,
1788 unsigned long arg)
1789{
Stefan Rasplde8474e2018-05-23 16:38:11 +02001790 union smc_host_cursor cons, urg;
1791 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01001792 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001793 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001794
1795 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001796 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02001797 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02001798 if (smc->use_fallback) {
1799 if (!smc->clcsock) {
1800 release_sock(&smc->sk);
1801 return -EBADF;
1802 }
1803 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1804 release_sock(&smc->sk);
1805 return answ;
1806 }
Ursula Braun9b67e262018-05-02 16:56:46 +02001807 switch (cmd) {
1808 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02001809 if (smc->sk.sk_state == SMC_LISTEN) {
1810 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001811 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001812 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001813 if (smc->sk.sk_state == SMC_INIT ||
1814 smc->sk.sk_state == SMC_CLOSED)
1815 answ = 0;
1816 else
1817 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02001818 break;
1819 case SIOCOUTQ:
1820 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02001821 if (smc->sk.sk_state == SMC_LISTEN) {
1822 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001823 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001824 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001825 if (smc->sk.sk_state == SMC_INIT ||
1826 smc->sk.sk_state == SMC_CLOSED)
1827 answ = 0;
1828 else
1829 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02001830 atomic_read(&smc->conn.sndbuf_space);
1831 break;
1832 case SIOCOUTQNSD:
1833 /* output queue size (not send only) */
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 = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02001843 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02001844 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02001845 if (smc->sk.sk_state == SMC_LISTEN) {
1846 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001847 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001848 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001849 if (smc->sk.sk_state == SMC_INIT ||
1850 smc->sk.sk_state == SMC_CLOSED) {
1851 answ = 0;
1852 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02001853 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1854 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001855 answ = smc_curs_diff(conn->rmb_desc->len,
1856 &cons, &urg) == 1;
1857 }
1858 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02001859 default:
Ursula Braun1992d992018-07-16 13:56:52 +02001860 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001861 return -ENOIOCTLCMD;
1862 }
Ursula Braun1992d992018-07-16 13:56:52 +02001863 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001864
1865 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001866}
1867
1868static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1869 int offset, size_t size, int flags)
1870{
1871 struct sock *sk = sock->sk;
1872 struct smc_sock *smc;
1873 int rc = -EPIPE;
1874
1875 smc = smc_sk(sk);
1876 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001877 if (sk->sk_state != SMC_ACTIVE) {
1878 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001879 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001880 }
1881 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001882 if (smc->use_fallback)
1883 rc = kernel_sendpage(smc->clcsock, page, offset,
1884 size, flags);
1885 else
1886 rc = sock_no_sendpage(sock, page, offset, size, flags);
1887
1888out:
Ursula Braunac713872017-01-09 16:55:13 +01001889 return rc;
1890}
1891
Stefan Raspl9014db22018-05-03 18:12:39 +02001892/* Map the affected portions of the rmbe into an spd, note the number of bytes
1893 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1894 * updates till whenever a respective page has been fully processed.
1895 * Note that subsequent recv() calls have to wait till all splice() processing
1896 * completed.
1897 */
Ursula Braunac713872017-01-09 16:55:13 +01001898static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1899 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001900 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001901{
1902 struct sock *sk = sock->sk;
1903 struct smc_sock *smc;
1904 int rc = -ENOTCONN;
1905
1906 smc = smc_sk(sk);
1907 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001908 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1909 /* socket was connected before, no more data to read */
1910 rc = 0;
1911 goto out;
1912 }
Stefan Raspl9014db22018-05-03 18:12:39 +02001913 if (sk->sk_state == SMC_INIT ||
1914 sk->sk_state == SMC_LISTEN ||
1915 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001916 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001917
1918 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1919 rc = 0;
1920 goto out;
1921 }
1922
Ursula Braunac713872017-01-09 16:55:13 +01001923 if (smc->use_fallback) {
1924 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1925 pipe, len, flags);
1926 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001927 if (*ppos) {
1928 rc = -ESPIPE;
1929 goto out;
1930 }
1931 if (flags & SPLICE_F_NONBLOCK)
1932 flags = MSG_DONTWAIT;
1933 else
1934 flags = 0;
1935 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001936 }
1937out:
1938 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001939
Ursula Braunac713872017-01-09 16:55:13 +01001940 return rc;
1941}
1942
1943/* must look like tcp */
1944static const struct proto_ops smc_sock_ops = {
1945 .family = PF_SMC,
1946 .owner = THIS_MODULE,
1947 .release = smc_release,
1948 .bind = smc_bind,
1949 .connect = smc_connect,
1950 .socketpair = sock_no_socketpair,
1951 .accept = smc_accept,
1952 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001953 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01001954 .ioctl = smc_ioctl,
1955 .listen = smc_listen,
1956 .shutdown = smc_shutdown,
1957 .setsockopt = smc_setsockopt,
1958 .getsockopt = smc_getsockopt,
1959 .sendmsg = smc_sendmsg,
1960 .recvmsg = smc_recvmsg,
1961 .mmap = sock_no_mmap,
1962 .sendpage = smc_sendpage,
1963 .splice_read = smc_splice_read,
1964};
1965
1966static int smc_create(struct net *net, struct socket *sock, int protocol,
1967 int kern)
1968{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001969 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001970 struct smc_sock *smc;
1971 struct sock *sk;
1972 int rc;
1973
1974 rc = -ESOCKTNOSUPPORT;
1975 if (sock->type != SOCK_STREAM)
1976 goto out;
1977
1978 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001979 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01001980 goto out;
1981
1982 rc = -ENOBUFS;
1983 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001984 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001985 if (!sk)
1986 goto out;
1987
1988 /* create internal TCP socket for CLC handshake and fallback */
1989 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001990 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02001991 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001992 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1993 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01001994 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01001995 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01001996 goto out;
1997 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01001998 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1999 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01002000
2001out:
2002 return rc;
2003}
2004
2005static const struct net_proto_family smc_sock_family_ops = {
2006 .family = PF_SMC,
2007 .owner = THIS_MODULE,
2008 .create = smc_create,
2009};
2010
Hans Wippel64e28b52019-02-21 13:01:02 +01002011unsigned int smc_net_id;
2012
2013static __net_init int smc_net_init(struct net *net)
2014{
2015 return smc_pnet_net_init(net);
2016}
2017
2018static void __net_exit smc_net_exit(struct net *net)
2019{
2020 smc_pnet_net_exit(net);
2021}
2022
2023static struct pernet_operations smc_net_ops = {
2024 .init = smc_net_init,
2025 .exit = smc_net_exit,
2026 .id = &smc_net_id,
2027 .size = sizeof(struct smc_net),
2028};
2029
Ursula Braunac713872017-01-09 16:55:13 +01002030static int __init smc_init(void)
2031{
2032 int rc;
2033
Hans Wippel64e28b52019-02-21 13:01:02 +01002034 rc = register_pernet_subsys(&smc_net_ops);
2035 if (rc)
2036 return rc;
2037
Thomas Richter6812baa2017-01-09 16:55:15 +01002038 rc = smc_pnet_init();
2039 if (rc)
YueHaibing8c33bf12019-06-26 17:47:50 +02002040 goto out_pernet_subsys;
Thomas Richter6812baa2017-01-09 16:55:15 +01002041
Ursula Braun6dabd402019-11-16 17:47:29 +01002042 rc = smc_core_init();
2043 if (rc) {
2044 pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
2045 goto out_pnet;
2046 }
2047
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002048 rc = smc_llc_init();
2049 if (rc) {
2050 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002051 goto out_core;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002052 }
2053
Ursula Braun5f083182017-01-09 16:55:22 +01002054 rc = smc_cdc_init();
2055 if (rc) {
2056 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002057 goto out_core;
Ursula Braun5f083182017-01-09 16:55:22 +01002058 }
2059
Ursula Braunac713872017-01-09 16:55:13 +01002060 rc = proto_register(&smc_proto, 1);
2061 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01002062 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002063 goto out_core;
Ursula Braunac713872017-01-09 16:55:13 +01002064 }
2065
Karsten Graulaaa4d332018-03-16 15:06:41 +01002066 rc = proto_register(&smc_proto6, 1);
2067 if (rc) {
2068 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2069 goto out_proto;
2070 }
2071
Ursula Braunac713872017-01-09 16:55:13 +01002072 rc = sock_register(&smc_sock_family_ops);
2073 if (rc) {
2074 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002075 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01002076 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01002077 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002078 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01002079
Ursula Brauna4cf0442017-01-09 16:55:14 +01002080 rc = smc_ib_register_client();
2081 if (rc) {
2082 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2083 goto out_sock;
2084 }
2085
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002086 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002087 return 0;
2088
Ursula Brauna4cf0442017-01-09 16:55:14 +01002089out_sock:
2090 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002091out_proto6:
2092 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002093out_proto:
2094 proto_unregister(&smc_proto);
Ursula Braun6dabd402019-11-16 17:47:29 +01002095out_core:
2096 smc_core_exit();
Thomas Richter6812baa2017-01-09 16:55:15 +01002097out_pnet:
2098 smc_pnet_exit();
YueHaibing8c33bf12019-06-26 17:47:50 +02002099out_pernet_subsys:
2100 unregister_pernet_subsys(&smc_net_ops);
2101
Ursula Braunac713872017-01-09 16:55:13 +01002102 return rc;
2103}
2104
2105static void __exit smc_exit(void)
2106{
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002107 static_branch_disable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002108 sock_unregister(PF_SMC);
Ursula Braun6dabd402019-11-16 17:47:29 +01002109 smc_core_exit();
2110 smc_ib_unregister_client();
Karsten Graulaaa4d332018-03-16 15:06:41 +01002111 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002112 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002113 smc_pnet_exit();
Hans Wippel64e28b52019-02-21 13:01:02 +01002114 unregister_pernet_subsys(&smc_net_ops);
Ursula Braun4ead9c92019-11-16 17:47:31 +01002115 rcu_barrier();
Ursula Braunac713872017-01-09 16:55:13 +01002116}
2117
2118module_init(smc_init);
2119module_exit(smc_exit);
2120
2121MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2122MODULE_DESCRIPTION("smc socket address family");
2123MODULE_LICENSE("GPL");
2124MODULE_ALIAS_NETPROTO(PF_SMC);