blob: cee5bf4a9bb95a517861ddebc0c4deaf603da245 [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 Braun6d6dd522019-11-12 16:03:41 +0100802 sock_put(&smc->sk); /* passive closing */
Ursula Braun24ac3a02018-06-27 17:59:50 +0200803 goto out;
804 }
805
806 rc = __smc_connect(smc);
807 if (rc < 0)
808 smc->sk.sk_err = -rc;
809
810out:
Ursula Braun07603b22019-04-11 11:17:32 +0200811 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
812 if (smc->sk.sk_err) {
813 smc->sk.sk_state_change(&smc->sk);
814 } else { /* allow polling before and after fallback decision */
815 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
816 smc->sk.sk_write_space(&smc->sk);
817 }
818 }
Ursula Braun24ac3a02018-06-27 17:59:50 +0200819 release_sock(&smc->sk);
820}
821
Ursula Braunac713872017-01-09 16:55:13 +0100822static int smc_connect(struct socket *sock, struct sockaddr *addr,
823 int alen, int flags)
824{
825 struct sock *sk = sock->sk;
826 struct smc_sock *smc;
827 int rc = -EINVAL;
828
829 smc = smc_sk(sk);
830
831 /* separate smc parameter checking to be safe */
832 if (alen < sizeof(addr->sa_family))
833 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100834 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100835 goto out_err;
836
837 lock_sock(sk);
838 switch (sk->sk_state) {
839 default:
840 goto out;
841 case SMC_ACTIVE:
842 rc = -EISCONN;
843 goto out;
844 case SMC_INIT:
845 rc = 0;
846 break;
847 }
848
849 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200850 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun50717a32019-04-12 12:57:23 +0200851 if (smc->connect_nonblock) {
852 rc = -EALREADY;
853 goto out;
854 }
855 rc = kernel_connect(smc->clcsock, addr, alen, flags);
856 if (rc && rc != -EINPROGRESS)
857 goto out;
Ursula Braun301428e2019-10-29 12:41:26 +0100858
859 sock_hold(&smc->sk); /* sock put in passive closing */
Ursula Braun86434742019-12-12 22:35:58 +0100860 if (smc->use_fallback)
861 goto out;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200862 if (flags & O_NONBLOCK) {
Ursula Braun50717a32019-04-12 12:57:23 +0200863 if (schedule_work(&smc->connect_work))
864 smc->connect_nonblock = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200865 rc = -EINPROGRESS;
866 } else {
Ursula Braun24ac3a02018-06-27 17:59:50 +0200867 rc = __smc_connect(smc);
868 if (rc < 0)
869 goto out;
870 else
871 rc = 0; /* success cases including fallback */
872 }
Ursula Braunac713872017-01-09 16:55:13 +0100873
874out:
875 release_sock(sk);
876out_err:
877 return rc;
878}
879
880static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
881{
Ursula Braun3163c502018-01-24 10:28:12 +0100882 struct socket *new_clcsock = NULL;
883 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100884 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -0800885 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +0100886
Ursula Braun3163c502018-01-24 10:28:12 +0100887 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100888 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100889 if (!new_sk) {
890 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100891 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100892 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100893 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100894 goto out;
895 }
896 *new_smc = smc_sk(new_sk);
897
Myungho Jung78abe3d2018-12-18 09:02:25 -0800898 mutex_lock(&lsmc->clcsock_release_lock);
899 if (lsmc->clcsock)
900 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
901 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +0100902 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100903 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100904 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100905 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +0200906 new_sk->sk_prot->unhash(new_sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100907 if (new_clcsock)
908 sock_release(new_clcsock);
909 new_sk->sk_state = SMC_CLOSED;
910 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun51f1de72018-01-26 09:28:48 +0100911 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100912 *new_smc = NULL;
913 goto out;
914 }
915
916 (*new_smc)->clcsock = new_clcsock;
917out:
918 return rc;
919}
920
Ursula Brauna046d572017-01-09 16:55:16 +0100921/* add a just created sock to the accept queue of the listen sock as
922 * candidate for a following socket accept call from user space
923 */
924static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
925{
926 struct smc_sock *par = smc_sk(parent);
927
Ursula Braun51f1de72018-01-26 09:28:48 +0100928 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100929 spin_lock(&par->accept_q_lock);
930 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
931 spin_unlock(&par->accept_q_lock);
932 sk_acceptq_added(parent);
933}
934
935/* remove a socket from the accept queue of its parental listening socket */
936static void smc_accept_unlink(struct sock *sk)
937{
938 struct smc_sock *par = smc_sk(sk)->listen_smc;
939
940 spin_lock(&par->accept_q_lock);
941 list_del_init(&smc_sk(sk)->accept_q);
942 spin_unlock(&par->accept_q_lock);
943 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100944 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100945}
946
947/* remove a sock from the accept queue to bind it to a new socket created
948 * for a socket accept call from user space
949 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100950struct sock *smc_accept_dequeue(struct sock *parent,
951 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100952{
953 struct smc_sock *isk, *n;
954 struct sock *new_sk;
955
956 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
957 new_sk = (struct sock *)isk;
958
959 smc_accept_unlink(new_sk);
960 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +0200961 new_sk->sk_prot->unhash(new_sk);
Ursula Braun127f4972018-01-26 09:28:49 +0100962 if (isk->clcsock) {
963 sock_release(isk->clcsock);
964 isk->clcsock = NULL;
965 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100966 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +0100967 continue;
968 }
Ursula Braun07603b22019-04-11 11:17:32 +0200969 if (new_sock) {
Ursula Brauna046d572017-01-09 16:55:16 +0100970 sock_graft(new_sk, new_sock);
Ursula Braun07603b22019-04-11 11:17:32 +0200971 if (isk->use_fallback) {
972 smc_sk(new_sk)->clcsock->file = new_sock->file;
973 isk->clcsock->file->private_data = isk->clcsock;
974 }
975 }
Ursula Brauna046d572017-01-09 16:55:16 +0100976 return new_sk;
977 }
978 return NULL;
979}
980
981/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +0100982void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +0100983{
984 struct smc_sock *smc = smc_sk(sk);
985
Ursula Braun81cf4f42019-10-21 16:13:15 +0200986 sock_hold(sk); /* sock_put below */
Ursula Braunb38d7322017-01-09 16:55:25 +0100987 lock_sock(sk);
988 if (!sk->sk_lingertime)
989 /* wait for peer closing */
990 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun39f41f32019-06-27 15:04:52 +0200991 __smc_release(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +0100992 release_sock(sk);
Ursula Braun81cf4f42019-10-21 16:13:15 +0200993 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +0100994 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +0100995}
996
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100997static int smc_serv_conf_first_link(struct smc_sock *smc)
998{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200999 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001000 struct smc_link_group *lgr = smc->conn.lgr;
1001 struct smc_link *link;
1002 int rest;
1003 int rc;
1004
1005 link = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun652a1e42017-07-28 13:56:17 +02001006
Karsten Graul44aa81c2018-05-15 17:04:55 +02001007 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +02001008 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +02001009
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001010 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +02001011 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001012 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001013 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001014
1015 /* receive CONFIRM LINK response from client over the RoCE fabric */
1016 rest = wait_for_completion_interruptible_timeout(
1017 &link->llc_confirm_resp,
1018 SMC_LLC_WAIT_FIRST_TIME);
1019 if (rest <= 0) {
1020 struct smc_clc_msg_decline dclc;
1021
1022 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001023 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001024 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001025 }
1026
Karsten Graul75d320d2018-03-01 13:51:31 +01001027 if (link->llc_confirm_resp_rc)
1028 return SMC_CLC_DECL_RMBE_EC;
1029
Karsten Graul52bedf32018-03-01 13:51:32 +01001030 /* send ADD LINK request to client over the RoCE fabric */
1031 rc = smc_llc_send_add_link(link,
1032 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +02001033 link->gid, SMC_LLC_REQ);
Karsten Graul52bedf32018-03-01 13:51:32 +01001034 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001035 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +01001036
1037 /* receive ADD LINK response from client over the RoCE fabric */
1038 rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
1039 SMC_LLC_WAIT_TIME);
1040 if (rest <= 0) {
1041 struct smc_clc_msg_decline dclc;
1042
1043 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001044 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001045 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
Karsten Graul52bedf32018-03-01 13:51:32 +01001046 }
1047
Karsten Graul877ae5b2018-05-02 16:56:44 +02001048 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +01001049
Karsten Graul75d320d2018-03-01 13:51:31 +01001050 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001051}
1052
Hans Wippel3b2dec22018-05-18 09:34:18 +02001053/* listen worker: finish */
1054static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001055{
Ursula Brauna046d572017-01-09 16:55:16 +01001056 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001057 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001058
Ursula Brauna046d572017-01-09 16:55:16 +01001059 if (lsmc->sk.sk_state == SMC_LISTEN) {
Karsten Graulfd577702019-04-11 11:17:30 +02001060 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001061 smc_accept_enqueue(&lsmc->sk, newsmcsk);
Karsten Graulfd577702019-04-11 11:17:30 +02001062 release_sock(&lsmc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001063 } else { /* no longer listening */
1064 smc_close_non_accepted(newsmcsk);
1065 }
Ursula Brauna046d572017-01-09 16:55:16 +01001066
1067 /* Wake up accept */
1068 lsmc->sk.sk_data_ready(&lsmc->sk);
1069 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001070}
Ursula Brauna046d572017-01-09 16:55:16 +01001071
Hans Wippel3b2dec22018-05-18 09:34:18 +02001072/* listen worker: finish in state connected */
1073static void smc_listen_out_connected(struct smc_sock *new_smc)
1074{
1075 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001076
Hans Wippel3b2dec22018-05-18 09:34:18 +02001077 sk_refcnt_debug_inc(newsmcsk);
1078 if (newsmcsk->sk_state == SMC_INIT)
1079 newsmcsk->sk_state = SMC_ACTIVE;
1080
1081 smc_listen_out(new_smc);
1082}
1083
1084/* listen worker: finish in error state */
1085static void smc_listen_out_err(struct smc_sock *new_smc)
1086{
1087 struct sock *newsmcsk = &new_smc->sk;
1088
Ursula Braun51f1de72018-01-26 09:28:48 +01001089 if (newsmcsk->sk_state == SMC_INIT)
1090 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001091 newsmcsk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001092 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001093
1094 smc_listen_out(new_smc);
1095}
1096
1097/* listen worker: decline and fall back if possible */
1098static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1099 int local_contact)
1100{
1101 /* RDMA setup failed, switch back to TCP */
1102 if (local_contact == SMC_FIRST_CONTACT)
1103 smc_lgr_forget(new_smc->conn.lgr);
1104 if (reason_code < 0) { /* error, no fallback possible */
1105 smc_listen_out_err(new_smc);
1106 return;
1107 }
1108 smc_conn_free(&new_smc->conn);
Ursula Braun07603b22019-04-11 11:17:32 +02001109 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001110 new_smc->fallback_rsn = reason_code;
1111 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001112 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1113 smc_listen_out_err(new_smc);
1114 return;
1115 }
1116 }
1117 smc_listen_out_connected(new_smc);
1118}
1119
1120/* listen worker: check prefixes */
Karsten Graul59886692019-04-12 12:57:25 +02001121static int smc_listen_prfx_check(struct smc_sock *new_smc,
Hans Wippel3b2dec22018-05-18 09:34:18 +02001122 struct smc_clc_msg_proposal *pclc)
1123{
1124 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1125 struct socket *newclcsock = new_smc->clcsock;
1126
1127 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1128 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
Karsten Graul59886692019-04-12 12:57:25 +02001129 return SMC_CLC_DECL_DIFFPREFIX;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001130
1131 return 0;
1132}
1133
1134/* listen worker: initialize connection and buffers */
1135static int smc_listen_rdma_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001136 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001137{
Karsten Graul7a627252019-04-12 12:57:30 +02001138 int rc;
1139
Hans Wippel3b2dec22018-05-18 09:34:18 +02001140 /* allocate connection / link group */
Karsten Graul7a627252019-04-12 12:57:30 +02001141 rc = smc_conn_create(new_smc, ini);
1142 if (rc)
1143 return rc;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001144
1145 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001146 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001147 return SMC_CLC_DECL_MEM;
1148
1149 return 0;
1150}
1151
Hans Wippel41349842018-06-28 19:05:11 +02001152/* listen worker: initialize connection and buffers for SMC-D */
1153static int smc_listen_ism_init(struct smc_sock *new_smc,
1154 struct smc_clc_msg_proposal *pclc,
Karsten Graul7a627252019-04-12 12:57:30 +02001155 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +02001156{
1157 struct smc_clc_msg_smcd *pclc_smcd;
Karsten Graul7a627252019-04-12 12:57:30 +02001158 int rc;
Hans Wippel41349842018-06-28 19:05:11 +02001159
1160 pclc_smcd = smc_get_clc_msg_smcd(pclc);
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001161 ini->ism_gid = pclc_smcd->gid;
Karsten Graul7a627252019-04-12 12:57:30 +02001162 rc = smc_conn_create(new_smc, ini);
1163 if (rc)
1164 return rc;
Hans Wippel41349842018-06-28 19:05:11 +02001165
1166 /* Check if peer can be reached via ISM device */
1167 if (smc_ism_cantalk(new_smc->conn.lgr->peer_gid,
1168 new_smc->conn.lgr->vlan_id,
1169 new_smc->conn.lgr->smcd)) {
Karsten Graul7a627252019-04-12 12:57:30 +02001170 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Hans Wippel41349842018-06-28 19:05:11 +02001171 smc_lgr_forget(new_smc->conn.lgr);
1172 smc_conn_free(&new_smc->conn);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001173 return SMC_CLC_DECL_SMCDNOTALK;
Hans Wippel41349842018-06-28 19:05:11 +02001174 }
1175
1176 /* Create send and receive buffers */
1177 if (smc_buf_create(new_smc, true)) {
Karsten Graul7a627252019-04-12 12:57:30 +02001178 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Hans Wippel41349842018-06-28 19:05:11 +02001179 smc_lgr_forget(new_smc->conn.lgr);
1180 smc_conn_free(&new_smc->conn);
1181 return SMC_CLC_DECL_MEM;
1182 }
1183
1184 return 0;
1185}
1186
Hans Wippel3b2dec22018-05-18 09:34:18 +02001187/* listen worker: register buffers */
1188static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
1189{
1190 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1191
1192 if (local_contact != SMC_FIRST_CONTACT) {
Karsten Graulc7674c02018-11-22 10:26:43 +01001193 if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true))
1194 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001195 }
1196 smc_rmb_sync_sg_for_device(&new_smc->conn);
1197
1198 return 0;
1199}
1200
1201/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001202static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1203 struct smc_clc_msg_accept_confirm *cclc,
1204 int local_contact)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001205{
1206 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1207 int reason_code = 0;
1208
1209 if (local_contact == SMC_FIRST_CONTACT)
1210 smc_link_save_peer_info(link, cclc);
1211
1212 if (smc_rmb_rtoken_handling(&new_smc->conn, cclc)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001213 reason_code = SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001214 goto decline;
1215 }
1216
1217 if (local_contact == SMC_FIRST_CONTACT) {
1218 if (smc_ib_ready_link(link)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001219 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001220 goto decline;
1221 }
1222 /* QP confirmation over RoCE fabric */
1223 reason_code = smc_serv_conf_first_link(new_smc);
1224 if (reason_code)
1225 goto decline;
1226 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001227 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001228
1229decline:
Hans Wippel3b2dec22018-05-18 09:34:18 +02001230 smc_listen_decline(new_smc, reason_code, local_contact);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001231 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001232}
1233
1234/* setup for RDMA connection of server */
1235static void smc_listen_work(struct work_struct *work)
1236{
1237 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1238 smc_listen_work);
1239 struct socket *newclcsock = new_smc->clcsock;
1240 struct smc_clc_msg_accept_confirm cclc;
1241 struct smc_clc_msg_proposal *pclc;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001242 struct smc_init_info ini = {0};
Hans Wippel41349842018-06-28 19:05:11 +02001243 bool ism_supported = false;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001244 u8 buf[SMC_CLC_MAX_LEN];
Hans Wippel3b2dec22018-05-18 09:34:18 +02001245 int rc = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001246
Karsten Graulfd577702019-04-11 11:17:30 +02001247 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1248 return smc_listen_out_err(new_smc);
1249
Hans Wippel3b2dec22018-05-18 09:34:18 +02001250 if (new_smc->use_fallback) {
1251 smc_listen_out_connected(new_smc);
1252 return;
1253 }
1254
1255 /* check if peer is smc capable */
1256 if (!tcp_sk(newclcsock->sk)->syn_smc) {
Ursula Braun07603b22019-04-11 11:17:32 +02001257 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001258 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001259 smc_listen_out_connected(new_smc);
1260 return;
1261 }
1262
1263 /* do inband token exchange -
1264 * wait for and receive SMC Proposal CLC message
1265 */
1266 pclc = (struct smc_clc_msg_proposal *)&buf;
Karsten Graul228bae02019-04-12 12:57:28 +02001267 rc = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
1268 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001269 if (rc)
1270 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001271
1272 /* IPSec connections opt out of SMC-R optimizations */
1273 if (using_ipsec(new_smc)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001274 rc = SMC_CLC_DECL_IPSEC;
1275 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001276 }
1277
Karsten Graul59886692019-04-12 12:57:25 +02001278 /* check for matching IP prefix and subnet length */
1279 rc = smc_listen_prfx_check(new_smc, pclc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001280 if (rc)
1281 goto out_decl;
Karsten Graul59886692019-04-12 12:57:25 +02001282
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001283 /* get vlan id from IP device */
1284 if (smc_vlan_by_tcpsk(new_smc->clcsock, &ini)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001285 rc = SMC_CLC_DECL_GETVLANERR;
1286 goto out_decl;
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001287 }
1288
Hans Wippel72a36a82019-02-07 15:56:18 +01001289 mutex_lock(&smc_server_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001290 smc_close_init(new_smc);
1291 smc_rx_init(new_smc);
1292 smc_tx_init(new_smc);
1293
Hans Wippel41349842018-06-28 19:05:11 +02001294 /* check if ISM is available */
Karsten Graul9aa68d22019-04-12 12:57:29 +02001295 if (pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) {
1296 ini.is_smcd = true; /* prepare ISM check */
1297 rc = smc_find_ism_device(new_smc, &ini);
1298 if (!rc)
Karsten Graul7a627252019-04-12 12:57:30 +02001299 rc = smc_listen_ism_init(new_smc, pclc, &ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001300 if (!rc)
1301 ism_supported = true;
1302 else if (pclc->hdr.path == SMC_TYPE_D)
1303 goto out_unlock; /* skip RDMA and decline */
1304 }
1305
1306 /* check if RDMA is available */
1307 if (!ism_supported) { /* SMC_TYPE_R or SMC_TYPE_B */
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001308 /* prepare RDMA check */
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001309 ini.is_smcd = false;
Ursula Braunca5f8d22019-10-23 15:44:06 +02001310 ini.ism_dev = NULL;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001311 ini.ib_lcl = &pclc->lcl;
Karsten Graul9aa68d22019-04-12 12:57:29 +02001312 rc = smc_find_rdma_device(new_smc, &ini);
1313 if (rc) {
1314 /* no RDMA device found */
1315 if (pclc->hdr.path == SMC_TYPE_B)
1316 /* neither ISM nor RDMA device found */
1317 rc = SMC_CLC_DECL_NOSMCDEV;
1318 goto out_unlock;
1319 }
Karsten Graul7a627252019-04-12 12:57:30 +02001320 rc = smc_listen_rdma_init(new_smc, &ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001321 if (rc)
1322 goto out_unlock;
Karsten Graul7a627252019-04-12 12:57:30 +02001323 rc = smc_listen_rdma_reg(new_smc, ini.cln_first_contact);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001324 if (rc)
1325 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001326 }
1327
1328 /* send SMC Accept CLC message */
Karsten Graul7a627252019-04-12 12:57:30 +02001329 rc = smc_clc_send_accept(new_smc, ini.cln_first_contact);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001330 if (rc)
1331 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001332
Hans Wippel62c71392019-02-07 15:56:17 +01001333 /* SMC-D does not need this lock any more */
1334 if (ism_supported)
Hans Wippel72a36a82019-02-07 15:56:18 +01001335 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001336
Hans Wippel3b2dec22018-05-18 09:34:18 +02001337 /* receive SMC Confirm CLC message */
Karsten Graul228bae02019-04-12 12:57:28 +02001338 rc = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
1339 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
1340 if (rc) {
Hans Wippel62c71392019-02-07 15:56:17 +01001341 if (!ism_supported)
Karsten Graul9aa68d22019-04-12 12:57:29 +02001342 goto out_unlock;
1343 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001344 }
1345
1346 /* finish worker */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001347 if (!ism_supported) {
Karsten Graul7a627252019-04-12 12:57:30 +02001348 rc = smc_listen_rdma_finish(new_smc, &cclc,
1349 ini.cln_first_contact);
Hans Wippel72a36a82019-02-07 15:56:18 +01001350 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001351 if (rc)
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001352 return;
1353 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001354 smc_conn_save_peer_info(new_smc, &cclc);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001355 smc_listen_out_connected(new_smc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001356 return;
1357
1358out_unlock:
1359 mutex_unlock(&smc_server_lgr_pending);
1360out_decl:
Karsten Graul7a627252019-04-12 12:57:30 +02001361 smc_listen_decline(new_smc, rc, ini.cln_first_contact);
Ursula Brauna046d572017-01-09 16:55:16 +01001362}
1363
1364static void smc_tcp_listen_work(struct work_struct *work)
1365{
1366 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1367 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001368 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001369 struct smc_sock *new_smc;
1370 int rc = 0;
1371
Ursula Braun3163c502018-01-24 10:28:12 +01001372 lock_sock(lsk);
1373 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001374 rc = smc_clcsock_accept(lsmc, &new_smc);
1375 if (rc)
1376 goto out;
1377 if (!new_smc)
1378 continue;
1379
1380 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001381 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001382 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001383 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001384 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1385 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001386 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1387 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001388 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1389 if (!schedule_work(&new_smc->smc_listen_work))
1390 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001391 }
1392
1393out:
Ursula Braun3163c502018-01-24 10:28:12 +01001394 release_sock(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001395 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +01001396}
1397
Ursula Braunac713872017-01-09 16:55:13 +01001398static int smc_listen(struct socket *sock, int backlog)
1399{
1400 struct sock *sk = sock->sk;
1401 struct smc_sock *smc;
1402 int rc;
1403
1404 smc = smc_sk(sk);
1405 lock_sock(sk);
1406
1407 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +02001408 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
1409 smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +01001410 goto out;
1411
1412 rc = 0;
1413 if (sk->sk_state == SMC_LISTEN) {
1414 sk->sk_max_ack_backlog = backlog;
1415 goto out;
1416 }
1417 /* some socket options are handled in core, so we could not apply
1418 * them to the clc socket -- copy smc socket options to clc socket
1419 */
1420 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001421 if (!smc->use_fallback)
1422 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001423
1424 rc = kernel_listen(smc->clcsock, backlog);
1425 if (rc)
1426 goto out;
1427 sk->sk_max_ack_backlog = backlog;
1428 sk->sk_ack_backlog = 0;
1429 sk->sk_state = SMC_LISTEN;
Ursula Braun51f1de72018-01-26 09:28:48 +01001430 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1431 if (!schedule_work(&smc->tcp_listen_work))
1432 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001433
1434out:
1435 release_sock(sk);
1436 return rc;
1437}
1438
1439static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001440 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001441{
Ursula Brauna046d572017-01-09 16:55:16 +01001442 struct sock *sk = sock->sk, *nsk;
1443 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001444 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001445 long timeo;
1446 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001447
1448 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001449 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001450 lock_sock(sk);
1451
1452 if (lsmc->sk.sk_state != SMC_LISTEN) {
1453 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001454 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001455 goto out;
1456 }
1457
Ursula Brauna046d572017-01-09 16:55:16 +01001458 /* Wait for an incoming connection */
1459 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1460 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1461 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1462 set_current_state(TASK_INTERRUPTIBLE);
1463 if (!timeo) {
1464 rc = -EAGAIN;
1465 break;
1466 }
1467 release_sock(sk);
1468 timeo = schedule_timeout(timeo);
1469 /* wakeup by sk_data_ready in smc_listen_work() */
1470 sched_annotate_sleep();
1471 lock_sock(sk);
1472 if (signal_pending(current)) {
1473 rc = sock_intr_errno(timeo);
1474 break;
1475 }
1476 }
1477 set_current_state(TASK_RUNNING);
1478 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001479
Ursula Brauna046d572017-01-09 16:55:16 +01001480 if (!rc)
1481 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001482 release_sock(sk);
1483 if (rc)
1484 goto out;
1485
1486 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1487 /* wait till data arrives on the socket */
1488 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1489 MSEC_PER_SEC);
1490 if (smc_sk(nsk)->use_fallback) {
1491 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1492
1493 lock_sock(clcsk);
1494 if (skb_queue_empty(&clcsk->sk_receive_queue))
1495 sk_wait_data(clcsk, &timeo, NULL);
1496 release_sock(clcsk);
1497 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1498 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001499 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001500 release_sock(nsk);
1501 }
1502 }
Ursula Braunac713872017-01-09 16:55:13 +01001503
1504out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001505 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001506 return rc;
1507}
1508
1509static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001510 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001511{
1512 struct smc_sock *smc;
1513
Ursula Braunb38d7322017-01-09 16:55:25 +01001514 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1515 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001516 return -ENOTCONN;
1517
1518 smc = smc_sk(sock->sk);
1519
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001520 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001521}
1522
1523static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1524{
1525 struct sock *sk = sock->sk;
1526 struct smc_sock *smc;
1527 int rc = -EPIPE;
1528
1529 smc = smc_sk(sk);
1530 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001531 if ((sk->sk_state != SMC_ACTIVE) &&
1532 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1533 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001534 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001535
1536 if (msg->msg_flags & MSG_FASTOPEN) {
Ursula Brauncd206362019-08-02 10:47:50 +02001537 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001538 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001539 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001540 } else {
1541 rc = -EINVAL;
1542 goto out;
1543 }
1544 }
1545
Ursula Braunac713872017-01-09 16:55:13 +01001546 if (smc->use_fallback)
1547 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1548 else
Ursula Braune6727f32017-01-09 16:55:23 +01001549 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001550out:
1551 release_sock(sk);
1552 return rc;
1553}
1554
1555static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1556 int flags)
1557{
1558 struct sock *sk = sock->sk;
1559 struct smc_sock *smc;
1560 int rc = -ENOTCONN;
1561
1562 smc = smc_sk(sk);
1563 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001564 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1565 /* socket was connected before, no more data to read */
1566 rc = 0;
1567 goto out;
1568 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001569 if ((sk->sk_state == SMC_INIT) ||
1570 (sk->sk_state == SMC_LISTEN) ||
1571 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001572 goto out;
1573
Ursula Braunb38d7322017-01-09 16:55:25 +01001574 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1575 rc = 0;
1576 goto out;
1577 }
1578
Stefan Raspl9014db22018-05-03 18:12:39 +02001579 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001580 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001581 } else {
1582 msg->msg_namelen = 0;
1583 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1584 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001585
Ursula Braunac713872017-01-09 16:55:13 +01001586out:
1587 release_sock(sk);
1588 return rc;
1589}
1590
Al Viroade994f2017-07-03 00:01:49 -04001591static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001592{
Ursula Braun8dce2782018-01-26 09:28:47 +01001593 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001594 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001595
Ursula Braun8dce2782018-01-26 09:28:47 +01001596 spin_lock(&isk->accept_q_lock);
1597 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001598 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001599 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001600
Ursula Braun8dce2782018-01-26 09:28:47 +01001601 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001602}
1603
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001604static __poll_t smc_poll(struct file *file, struct socket *sock,
1605 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01001606{
1607 struct sock *sk = sock->sk;
Ursula Braunac713872017-01-09 16:55:13 +01001608 struct smc_sock *smc;
Ursula Braun50717a32019-04-12 12:57:23 +02001609 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001610
Ursula Braun8dce2782018-01-26 09:28:47 +01001611 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001612 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001613
Ursula Braunac713872017-01-09 16:55:13 +01001614 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02001615 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01001616 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001617 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02001618 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braunac713872017-01-09 16:55:13 +01001619 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07001620 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02001621 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01001622 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001623 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001624 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1625 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001626 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001627 if (sk->sk_state == SMC_LISTEN) {
1628 /* woken up by sk_data_ready in smc_listen_work() */
Ursula Braun50717a32019-04-12 12:57:23 +02001629 mask |= smc_accept_poll(sk);
1630 } else if (smc->use_fallback) { /* as result of connect_work()*/
1631 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
1632 wait);
1633 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun8dce2782018-01-26 09:28:47 +01001634 } else {
Ursula Braun50717a32019-04-12 12:57:23 +02001635 if ((sk->sk_state != SMC_INIT &&
1636 atomic_read(&smc->conn.sndbuf_space)) ||
Ursula Braun8dce2782018-01-26 09:28:47 +01001637 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001638 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001639 } else {
1640 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1641 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1642 }
1643 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001644 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001645 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001646 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001647 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001648 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02001649 if (smc->conn.urg_state == SMC_URG_VALID)
1650 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01001651 }
Ursula Braunac713872017-01-09 16:55:13 +01001652 }
1653
1654 return mask;
1655}
1656
1657static int smc_shutdown(struct socket *sock, int how)
1658{
1659 struct sock *sk = sock->sk;
1660 struct smc_sock *smc;
1661 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001662 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001663
1664 smc = smc_sk(sk);
1665
1666 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001667 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001668
1669 lock_sock(sk);
1670
1671 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02001672 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01001673 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1674 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1675 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1676 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1677 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001678 goto out;
1679 if (smc->use_fallback) {
1680 rc = kernel_sock_shutdown(smc->clcsock, how);
1681 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1682 if (sk->sk_shutdown == SHUTDOWN_MASK)
1683 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001684 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001685 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001686 switch (how) {
1687 case SHUT_RDWR: /* shutdown in both directions */
1688 rc = smc_close_active(smc);
1689 break;
1690 case SHUT_WR:
1691 rc = smc_close_shutdown_write(smc);
1692 break;
1693 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001694 rc = 0;
1695 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001696 break;
1697 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001698 if (smc->clcsock)
1699 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001700 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1701 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001702
1703out:
1704 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001705 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001706}
1707
1708static int smc_setsockopt(struct socket *sock, int level, int optname,
1709 char __user *optval, unsigned int optlen)
1710{
1711 struct sock *sk = sock->sk;
1712 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001713 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001714
1715 smc = smc_sk(sk);
1716
1717 /* generic setsockopts reaching us here always apply to the
1718 * CLC socket
1719 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001720 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1721 optval, optlen);
1722 if (smc->clcsock->sk->sk_err) {
1723 sk->sk_err = smc->clcsock->sk->sk_err;
1724 sk->sk_error_report(sk);
1725 }
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001726
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001727 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00001728 return -EINVAL;
Ursula Braunac0107e2018-07-18 15:22:50 +02001729 if (get_user(val, (int __user *)optval))
1730 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001731
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001732 lock_sock(sk);
Ursula Braun86434742019-12-12 22:35:58 +01001733 if (rc || smc->use_fallback)
1734 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001735 switch (optname) {
1736 case TCP_ULP:
1737 case TCP_FASTOPEN:
1738 case TCP_FASTOPEN_CONNECT:
1739 case TCP_FASTOPEN_KEY:
1740 case TCP_FASTOPEN_NO_COOKIE:
1741 /* option not supported by SMC */
Ursula Braun8204df72019-11-15 12:39:30 +01001742 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001743 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001744 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001745 } else {
Ursula Braun86434742019-12-12 22:35:58 +01001746 rc = -EINVAL;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001747 }
1748 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001749 case TCP_NODELAY:
Ursula Braunf9cedf12019-08-02 10:16:38 +02001750 if (sk->sk_state != SMC_INIT &&
1751 sk->sk_state != SMC_LISTEN &&
1752 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01001753 if (val)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001754 mod_delayed_work(system_wq, &smc->conn.tx_work,
1755 0);
1756 }
1757 break;
1758 case TCP_CORK:
Ursula Braunf9cedf12019-08-02 10:16:38 +02001759 if (sk->sk_state != SMC_INIT &&
1760 sk->sk_state != SMC_LISTEN &&
1761 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01001762 if (!val)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001763 mod_delayed_work(system_wq, &smc->conn.tx_work,
1764 0);
1765 }
1766 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001767 case TCP_DEFER_ACCEPT:
1768 smc->sockopt_defer_accept = val;
1769 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001770 default:
1771 break;
1772 }
Ursula Braun86434742019-12-12 22:35:58 +01001773out:
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001774 release_sock(sk);
1775
1776 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001777}
1778
1779static int smc_getsockopt(struct socket *sock, int level, int optname,
1780 char __user *optval, int __user *optlen)
1781{
1782 struct smc_sock *smc;
1783
1784 smc = smc_sk(sock->sk);
1785 /* socket options apply to the CLC socket */
1786 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1787 optval, optlen);
1788}
1789
1790static int smc_ioctl(struct socket *sock, unsigned int cmd,
1791 unsigned long arg)
1792{
Stefan Rasplde8474e2018-05-23 16:38:11 +02001793 union smc_host_cursor cons, urg;
1794 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01001795 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001796 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001797
1798 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001799 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02001800 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02001801 if (smc->use_fallback) {
1802 if (!smc->clcsock) {
1803 release_sock(&smc->sk);
1804 return -EBADF;
1805 }
1806 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1807 release_sock(&smc->sk);
1808 return answ;
1809 }
Ursula Braun9b67e262018-05-02 16:56:46 +02001810 switch (cmd) {
1811 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02001812 if (smc->sk.sk_state == SMC_LISTEN) {
1813 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001814 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001815 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001816 if (smc->sk.sk_state == SMC_INIT ||
1817 smc->sk.sk_state == SMC_CLOSED)
1818 answ = 0;
1819 else
1820 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02001821 break;
1822 case SIOCOUTQ:
1823 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02001824 if (smc->sk.sk_state == SMC_LISTEN) {
1825 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001826 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001827 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001828 if (smc->sk.sk_state == SMC_INIT ||
1829 smc->sk.sk_state == SMC_CLOSED)
1830 answ = 0;
1831 else
1832 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02001833 atomic_read(&smc->conn.sndbuf_space);
1834 break;
1835 case SIOCOUTQNSD:
1836 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02001837 if (smc->sk.sk_state == SMC_LISTEN) {
1838 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001839 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001840 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001841 if (smc->sk.sk_state == SMC_INIT ||
1842 smc->sk.sk_state == SMC_CLOSED)
1843 answ = 0;
1844 else
1845 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02001846 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02001847 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02001848 if (smc->sk.sk_state == SMC_LISTEN) {
1849 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001850 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001851 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001852 if (smc->sk.sk_state == SMC_INIT ||
1853 smc->sk.sk_state == SMC_CLOSED) {
1854 answ = 0;
1855 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02001856 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1857 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001858 answ = smc_curs_diff(conn->rmb_desc->len,
1859 &cons, &urg) == 1;
1860 }
1861 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02001862 default:
Ursula Braun1992d992018-07-16 13:56:52 +02001863 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001864 return -ENOIOCTLCMD;
1865 }
Ursula Braun1992d992018-07-16 13:56:52 +02001866 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001867
1868 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001869}
1870
1871static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1872 int offset, size_t size, int flags)
1873{
1874 struct sock *sk = sock->sk;
1875 struct smc_sock *smc;
1876 int rc = -EPIPE;
1877
1878 smc = smc_sk(sk);
1879 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001880 if (sk->sk_state != SMC_ACTIVE) {
1881 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001882 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001883 }
1884 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001885 if (smc->use_fallback)
1886 rc = kernel_sendpage(smc->clcsock, page, offset,
1887 size, flags);
1888 else
1889 rc = sock_no_sendpage(sock, page, offset, size, flags);
1890
1891out:
Ursula Braunac713872017-01-09 16:55:13 +01001892 return rc;
1893}
1894
Stefan Raspl9014db22018-05-03 18:12:39 +02001895/* Map the affected portions of the rmbe into an spd, note the number of bytes
1896 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1897 * updates till whenever a respective page has been fully processed.
1898 * Note that subsequent recv() calls have to wait till all splice() processing
1899 * completed.
1900 */
Ursula Braunac713872017-01-09 16:55:13 +01001901static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1902 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001903 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001904{
1905 struct sock *sk = sock->sk;
1906 struct smc_sock *smc;
1907 int rc = -ENOTCONN;
1908
1909 smc = smc_sk(sk);
1910 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001911 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1912 /* socket was connected before, no more data to read */
1913 rc = 0;
1914 goto out;
1915 }
Stefan Raspl9014db22018-05-03 18:12:39 +02001916 if (sk->sk_state == SMC_INIT ||
1917 sk->sk_state == SMC_LISTEN ||
1918 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001919 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001920
1921 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1922 rc = 0;
1923 goto out;
1924 }
1925
Ursula Braunac713872017-01-09 16:55:13 +01001926 if (smc->use_fallback) {
1927 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1928 pipe, len, flags);
1929 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001930 if (*ppos) {
1931 rc = -ESPIPE;
1932 goto out;
1933 }
1934 if (flags & SPLICE_F_NONBLOCK)
1935 flags = MSG_DONTWAIT;
1936 else
1937 flags = 0;
1938 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001939 }
1940out:
1941 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001942
Ursula Braunac713872017-01-09 16:55:13 +01001943 return rc;
1944}
1945
1946/* must look like tcp */
1947static const struct proto_ops smc_sock_ops = {
1948 .family = PF_SMC,
1949 .owner = THIS_MODULE,
1950 .release = smc_release,
1951 .bind = smc_bind,
1952 .connect = smc_connect,
1953 .socketpair = sock_no_socketpair,
1954 .accept = smc_accept,
1955 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001956 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01001957 .ioctl = smc_ioctl,
1958 .listen = smc_listen,
1959 .shutdown = smc_shutdown,
1960 .setsockopt = smc_setsockopt,
1961 .getsockopt = smc_getsockopt,
1962 .sendmsg = smc_sendmsg,
1963 .recvmsg = smc_recvmsg,
1964 .mmap = sock_no_mmap,
1965 .sendpage = smc_sendpage,
1966 .splice_read = smc_splice_read,
1967};
1968
1969static int smc_create(struct net *net, struct socket *sock, int protocol,
1970 int kern)
1971{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001972 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001973 struct smc_sock *smc;
1974 struct sock *sk;
1975 int rc;
1976
1977 rc = -ESOCKTNOSUPPORT;
1978 if (sock->type != SOCK_STREAM)
1979 goto out;
1980
1981 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001982 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01001983 goto out;
1984
1985 rc = -ENOBUFS;
1986 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001987 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001988 if (!sk)
1989 goto out;
1990
1991 /* create internal TCP socket for CLC handshake and fallback */
1992 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001993 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02001994 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001995 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1996 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01001997 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01001998 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01001999 goto out;
2000 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01002001 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
2002 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01002003
2004out:
2005 return rc;
2006}
2007
2008static const struct net_proto_family smc_sock_family_ops = {
2009 .family = PF_SMC,
2010 .owner = THIS_MODULE,
2011 .create = smc_create,
2012};
2013
Hans Wippel64e28b52019-02-21 13:01:02 +01002014unsigned int smc_net_id;
2015
2016static __net_init int smc_net_init(struct net *net)
2017{
2018 return smc_pnet_net_init(net);
2019}
2020
2021static void __net_exit smc_net_exit(struct net *net)
2022{
2023 smc_pnet_net_exit(net);
2024}
2025
2026static struct pernet_operations smc_net_ops = {
2027 .init = smc_net_init,
2028 .exit = smc_net_exit,
2029 .id = &smc_net_id,
2030 .size = sizeof(struct smc_net),
2031};
2032
Ursula Braunac713872017-01-09 16:55:13 +01002033static int __init smc_init(void)
2034{
2035 int rc;
2036
Hans Wippel64e28b52019-02-21 13:01:02 +01002037 rc = register_pernet_subsys(&smc_net_ops);
2038 if (rc)
2039 return rc;
2040
Thomas Richter6812baa2017-01-09 16:55:15 +01002041 rc = smc_pnet_init();
2042 if (rc)
YueHaibing8c33bf12019-06-26 17:47:50 +02002043 goto out_pernet_subsys;
Thomas Richter6812baa2017-01-09 16:55:15 +01002044
Ursula Braun6dabd402019-11-16 17:47:29 +01002045 rc = smc_core_init();
2046 if (rc) {
2047 pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
2048 goto out_pnet;
2049 }
2050
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002051 rc = smc_llc_init();
2052 if (rc) {
2053 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002054 goto out_core;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002055 }
2056
Ursula Braun5f083182017-01-09 16:55:22 +01002057 rc = smc_cdc_init();
2058 if (rc) {
2059 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002060 goto out_core;
Ursula Braun5f083182017-01-09 16:55:22 +01002061 }
2062
Ursula Braunac713872017-01-09 16:55:13 +01002063 rc = proto_register(&smc_proto, 1);
2064 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01002065 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002066 goto out_core;
Ursula Braunac713872017-01-09 16:55:13 +01002067 }
2068
Karsten Graulaaa4d332018-03-16 15:06:41 +01002069 rc = proto_register(&smc_proto6, 1);
2070 if (rc) {
2071 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2072 goto out_proto;
2073 }
2074
Ursula Braunac713872017-01-09 16:55:13 +01002075 rc = sock_register(&smc_sock_family_ops);
2076 if (rc) {
2077 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002078 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01002079 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01002080 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002081 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01002082
Ursula Brauna4cf0442017-01-09 16:55:14 +01002083 rc = smc_ib_register_client();
2084 if (rc) {
2085 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2086 goto out_sock;
2087 }
2088
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002089 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002090 return 0;
2091
Ursula Brauna4cf0442017-01-09 16:55:14 +01002092out_sock:
2093 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002094out_proto6:
2095 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002096out_proto:
2097 proto_unregister(&smc_proto);
Ursula Braun6dabd402019-11-16 17:47:29 +01002098out_core:
2099 smc_core_exit();
Thomas Richter6812baa2017-01-09 16:55:15 +01002100out_pnet:
2101 smc_pnet_exit();
YueHaibing8c33bf12019-06-26 17:47:50 +02002102out_pernet_subsys:
2103 unregister_pernet_subsys(&smc_net_ops);
2104
Ursula Braunac713872017-01-09 16:55:13 +01002105 return rc;
2106}
2107
2108static void __exit smc_exit(void)
2109{
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002110 static_branch_disable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002111 sock_unregister(PF_SMC);
Ursula Braun6dabd402019-11-16 17:47:29 +01002112 smc_core_exit();
2113 smc_ib_unregister_client();
Karsten Graulaaa4d332018-03-16 15:06:41 +01002114 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002115 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002116 smc_pnet_exit();
Hans Wippel64e28b52019-02-21 13:01:02 +01002117 unregister_pernet_subsys(&smc_net_ops);
Ursula Braun4ead9c92019-11-16 17:47:31 +01002118 rcu_barrier();
Ursula Braunac713872017-01-09 16:55:13 +01002119}
2120
2121module_init(smc_init);
2122module_exit(smc_exit);
2123
2124MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2125MODULE_DESCRIPTION("smc socket address family");
2126MODULE_LICENSE("GPL");
2127MODULE_ALIAS_NETPROTO(PF_SMC);