blob: 6e4bad8c64a875682a22f70731d16ee6235be278 [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 */
Karsten Graul387707f2020-04-29 17:10:40 +0200346 if (smc_wr_reg_send(link, rmb_desc->mr_rx[link->link_idx])) {
Karsten Graulc7674c02018-11-22 10:26:43 +0100347 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);
Karsten Graul387707f2020-04-29 17:10:40 +0200365 struct smc_link *link = smc->conn.lnk;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100366 int rest;
367 int rc;
368
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100369 /* receive CONFIRM LINK request from server over RoCE fabric */
370 rest = wait_for_completion_interruptible_timeout(
371 &link->llc_confirm,
372 SMC_LLC_WAIT_FIRST_TIME);
373 if (rest <= 0) {
374 struct smc_clc_msg_decline dclc;
375
376 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100377 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100378 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100379 }
380
Karsten Graul75d320d2018-03-01 13:51:31 +0100381 if (link->llc_confirm_rc)
382 return SMC_CLC_DECL_RMBE_EC;
383
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100384 rc = smc_ib_modify_qp_rts(link);
385 if (rc)
Karsten Graul603cc142018-07-25 16:35:32 +0200386 return SMC_CLC_DECL_ERR_RDYLNK;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100387
388 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200389
Karsten Graul44aa81c2018-05-15 17:04:55 +0200390 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200391 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200392
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100393 /* send CONFIRM LINK response over RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200394 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100395 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200396 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100397
Karsten Graul52bedf32018-03-01 13:51:32 +0100398 /* receive ADD LINK request from server over RoCE fabric */
399 rest = wait_for_completion_interruptible_timeout(&link->llc_add,
400 SMC_LLC_WAIT_TIME);
401 if (rest <= 0) {
402 struct smc_clc_msg_decline dclc;
403
404 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100405 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100406 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
Karsten Graul52bedf32018-03-01 13:51:32 +0100407 }
408
409 /* send add link reject message, only one link supported for now */
410 rc = smc_llc_send_add_link(link,
411 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +0200412 link->gid, SMC_LLC_RESP);
Karsten Graul52bedf32018-03-01 13:51:32 +0100413 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200414 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +0100415
Karsten Graul877ae5b2018-05-02 16:56:44 +0200416 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100417
Karsten Graul75d320d2018-03-01 13:51:31 +0100418 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100419}
420
Hans Wippel41349842018-06-28 19:05:11 +0200421static void smcr_conn_save_peer_info(struct smc_sock *smc,
422 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100423{
Hans Wippel95d8d2632018-05-18 09:34:13 +0200424 int bufsize = smc_uncompress_bufsize(clc->rmbe_size);
425
Hans Wippel92a138e2018-05-18 09:34:12 +0200426 smc->conn.peer_rmbe_idx = clc->rmbe_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100427 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200428 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100429 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200430 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100431}
432
Hans Wippel41349842018-06-28 19:05:11 +0200433static void smcd_conn_save_peer_info(struct smc_sock *smc,
434 struct smc_clc_msg_accept_confirm *clc)
435{
436 int bufsize = smc_uncompress_bufsize(clc->dmbe_size);
437
438 smc->conn.peer_rmbe_idx = clc->dmbe_idx;
439 smc->conn.peer_token = clc->token;
440 /* msg header takes up space in the buffer */
441 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
442 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
443 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
444}
445
446static void smc_conn_save_peer_info(struct smc_sock *smc,
447 struct smc_clc_msg_accept_confirm *clc)
448{
449 if (smc->conn.lgr->is_smcd)
450 smcd_conn_save_peer_info(smc, clc);
451 else
452 smcr_conn_save_peer_info(smc, clc);
453}
454
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100455static void smc_link_save_peer_info(struct smc_link *link,
456 struct smc_clc_msg_accept_confirm *clc)
457{
458 link->peer_qpn = ntoh24(clc->qpn);
459 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
460 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
461 link->peer_psn = ntoh24(clc->psn);
462 link->peer_mtu = clc->qp_mtu;
463}
464
Ursula Braun07603b22019-04-11 11:17:32 +0200465static void smc_switch_to_fallback(struct smc_sock *smc)
466{
467 smc->use_fallback = true;
468 if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
469 smc->clcsock->file = smc->sk.sk_socket->file;
470 smc->clcsock->file->private_data = smc->clcsock;
Ursula Braun67f562e2020-02-14 08:58:59 +0100471 smc->clcsock->wq.fasync_list =
472 smc->sk.sk_socket->wq.fasync_list;
Ursula Braun07603b22019-04-11 11:17:32 +0200473 }
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{
Ursula Braun51e3dfa2020-02-25 16:34:36 +0100513 bool is_smcd = smc->conn.lgr->is_smcd;
514
Hans Wippel3b2dec22018-05-18 09:34:18 +0200515 if (local_contact == SMC_FIRST_CONTACT)
Ursula Braun51e3dfa2020-02-25 16:34:36 +0100516 smc_lgr_cleanup_early(&smc->conn);
517 else
518 smc_conn_free(&smc->conn);
519 if (is_smcd)
Hans Wippel72a36a82019-02-07 15:56:18 +0100520 /* there is only one lgr role for SMC-D; use server lock */
521 mutex_unlock(&smc_server_lgr_pending);
522 else
523 mutex_unlock(&smc_client_lgr_pending);
524
Ursula Braun50717a32019-04-12 12:57:23 +0200525 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200526 return reason_code;
527}
528
529/* check if there is a rdma device available for this connection. */
530/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200531static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200532{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200533 /* PNET table look up: search active ib_device and port
534 * within same PNETID that also contains the ethernet device
535 * used for the internal TCP socket
536 */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200537 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +0200538 if (!ini->ib_dev)
539 return SMC_CLC_DECL_NOSMCRDEV;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200540 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200541}
542
Hans Wippel41349842018-06-28 19:05:11 +0200543/* check if there is an ISM device available for this connection. */
544/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200545static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200546{
547 /* Find ISM device with same PNETID as connecting interface */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200548 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
549 if (!ini->ism_dev)
Karsten Graul9aa68d22019-04-12 12:57:29 +0200550 return SMC_CLC_DECL_NOSMCDDEV;
Hans Wippel41349842018-06-28 19:05:11 +0200551 return 0;
552}
553
554/* Check for VLAN ID and register it on ISM device just for CLC handshake */
555static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200556 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200557{
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200558 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev, ini->vlan_id))
Karsten Graul7a627252019-04-12 12:57:30 +0200559 return SMC_CLC_DECL_ISMVLANERR;
Hans Wippel41349842018-06-28 19:05:11 +0200560 return 0;
561}
562
563/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
564 * used, the VLAN ID will be registered again during the connection setup.
565 */
566static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200567 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200568{
569 if (!is_smcd)
570 return 0;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200571 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev, ini->vlan_id))
Hans Wippel41349842018-06-28 19:05:11 +0200572 return SMC_CLC_DECL_CNFERR;
573 return 0;
574}
575
Hans Wippel3b2dec22018-05-18 09:34:18 +0200576/* CLC handshake during connect */
Hans Wippelc758dfd2018-06-28 19:05:09 +0200577static int smc_connect_clc(struct smc_sock *smc, int smc_type,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200578 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200579 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200580{
581 int rc = 0;
582
583 /* do inband token exchange */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200584 rc = smc_clc_send_proposal(smc, smc_type, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200585 if (rc)
586 return rc;
587 /* receive SMC Accept CLC message */
Ursula Braun2b59f582018-11-22 10:26:39 +0100588 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT,
589 CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200590}
591
Ursula Brauna046d572017-01-09 16:55:16 +0100592/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200593static int smc_connect_rdma(struct smc_sock *smc,
594 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200595 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200596{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200597 struct smc_link *link;
598 int reason_code = 0;
599
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200600 ini->is_smcd = false;
601 ini->ib_lcl = &aclc->lcl;
602 ini->ib_clcqpn = ntoh24(aclc->qpn);
603 ini->srv_first_contact = aclc->hdr.flag;
604
Hans Wippel72a36a82019-02-07 15:56:18 +0100605 mutex_lock(&smc_client_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200606 reason_code = smc_conn_create(smc, ini);
607 if (reason_code) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100608 mutex_unlock(&smc_client_lgr_pending);
609 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200610 }
Karsten Graul387707f2020-04-29 17:10:40 +0200611 link = smc->conn.lnk;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200612
613 smc_conn_save_peer_info(smc, aclc);
614
615 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200616 if (smc_buf_create(smc, false))
Karsten Graul7a627252019-04-12 12:57:30 +0200617 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
618 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200619
Karsten Graul7a627252019-04-12 12:57:30 +0200620 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200621 smc_link_save_peer_info(link, aclc);
622
623 if (smc_rmb_rtoken_handling(&smc->conn, aclc))
Karsten Graul603cc142018-07-25 16:35:32 +0200624 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
Karsten Graul7a627252019-04-12 12:57:30 +0200625 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200626
627 smc_close_init(smc);
628 smc_rx_init(smc);
629
Karsten Graul7a627252019-04-12 12:57:30 +0200630 if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200631 if (smc_ib_ready_link(link))
Karsten Graul603cc142018-07-25 16:35:32 +0200632 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
Karsten Graul7a627252019-04-12 12:57:30 +0200633 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200634 } else {
Karsten Graulc7674c02018-11-22 10:26:43 +0100635 if (smc_reg_rmb(link, smc->conn.rmb_desc, true))
Karsten Graul603cc142018-07-25 16:35:32 +0200636 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
Karsten Graul7a627252019-04-12 12:57:30 +0200637 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200638 }
639 smc_rmb_sync_sg_for_device(&smc->conn);
640
641 reason_code = smc_clc_send_confirm(smc);
642 if (reason_code)
Karsten Graul7a627252019-04-12 12:57:30 +0200643 return smc_connect_abort(smc, reason_code,
644 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200645
646 smc_tx_init(smc);
647
Karsten Graul7a627252019-04-12 12:57:30 +0200648 if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200649 /* QP confirmation over RoCE fabric */
650 reason_code = smc_clnt_conf_first_link(smc);
651 if (reason_code)
652 return smc_connect_abort(smc, reason_code,
Karsten Graul7a627252019-04-12 12:57:30 +0200653 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200654 }
Hans Wippel72a36a82019-02-07 15:56:18 +0100655 mutex_unlock(&smc_client_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200656
657 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200658 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200659 if (smc->sk.sk_state == SMC_INIT)
660 smc->sk.sk_state = SMC_ACTIVE;
661
662 return 0;
663}
664
Hans Wippel41349842018-06-28 19:05:11 +0200665/* setup for ISM connection of client */
666static int smc_connect_ism(struct smc_sock *smc,
667 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200668 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200669{
Hans Wippel41349842018-06-28 19:05:11 +0200670 int rc = 0;
671
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200672 ini->is_smcd = true;
673 ini->ism_gid = aclc->gid;
674 ini->srv_first_contact = aclc->hdr.flag;
675
Hans Wippel72a36a82019-02-07 15:56:18 +0100676 /* there is only one lgr role for SMC-D; use server lock */
677 mutex_lock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200678 rc = smc_conn_create(smc, ini);
679 if (rc) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100680 mutex_unlock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200681 return rc;
Hans Wippel72a36a82019-02-07 15:56:18 +0100682 }
Hans Wippel41349842018-06-28 19:05:11 +0200683
684 /* Create send and receive buffers */
685 if (smc_buf_create(smc, true))
Karsten Graul7a627252019-04-12 12:57:30 +0200686 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
687 ini->cln_first_contact);
Hans Wippel41349842018-06-28 19:05:11 +0200688
689 smc_conn_save_peer_info(smc, aclc);
690 smc_close_init(smc);
691 smc_rx_init(smc);
692 smc_tx_init(smc);
693
694 rc = smc_clc_send_confirm(smc);
695 if (rc)
Karsten Graul7a627252019-04-12 12:57:30 +0200696 return smc_connect_abort(smc, rc, ini->cln_first_contact);
Hans Wippel72a36a82019-02-07 15:56:18 +0100697 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel41349842018-06-28 19:05:11 +0200698
699 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200700 smc->connect_nonblock = 0;
Hans Wippel41349842018-06-28 19:05:11 +0200701 if (smc->sk.sk_state == SMC_INIT)
702 smc->sk.sk_state = SMC_ACTIVE;
703
704 return 0;
705}
706
Hans Wippel3b2dec22018-05-18 09:34:18 +0200707/* perform steps before actually connecting */
708static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100709{
Hans Wippel41349842018-06-28 19:05:11 +0200710 bool ism_supported = false, rdma_supported = false;
Ursula Brauna046d572017-01-09 16:55:16 +0100711 struct smc_clc_msg_accept_confirm aclc;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200712 struct smc_init_info ini = {0};
Hans Wippel41349842018-06-28 19:05:11 +0200713 int smc_type;
Ursula Brauna046d572017-01-09 16:55:16 +0100714 int rc = 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100715
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200716 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +0200717 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200718
Hans Wippel3b2dec22018-05-18 09:34:18 +0200719 /* if peer has not signalled SMC-capability, fall back */
720 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +0200721 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200722
Ursula Brauna046d572017-01-09 16:55:16 +0100723 /* IPSec connections opt out of SMC-R optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200724 if (using_ipsec(smc))
725 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
Ursula Brauna046d572017-01-09 16:55:16 +0100726
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200727 /* get vlan id from IP device */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200728 if (smc_vlan_by_tcpsk(smc->clcsock, &ini))
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200729 return smc_connect_decline_fallback(smc,
730 SMC_CLC_DECL_GETVLANERR);
Hans Wippel41349842018-06-28 19:05:11 +0200731
732 /* check if there is an ism device available */
Karsten Graul228bae02019-04-12 12:57:28 +0200733 if (!smc_find_ism_device(smc, &ini) &&
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200734 !smc_connect_ism_vlan_setup(smc, &ini)) {
Hans Wippel41349842018-06-28 19:05:11 +0200735 /* ISM is supported for this connection */
736 ism_supported = true;
737 smc_type = SMC_TYPE_D;
738 }
739
740 /* check if there is a rdma device available */
Karsten Graul228bae02019-04-12 12:57:28 +0200741 if (!smc_find_rdma_device(smc, &ini)) {
Hans Wippel41349842018-06-28 19:05:11 +0200742 /* RDMA is supported for this connection */
743 rdma_supported = true;
744 if (ism_supported)
745 smc_type = SMC_TYPE_B; /* both */
746 else
747 smc_type = SMC_TYPE_R; /* only RDMA */
748 }
749
750 /* if neither ISM nor RDMA are supported, fallback */
751 if (!rdma_supported && !ism_supported)
Karsten Graul603cc142018-07-25 16:35:32 +0200752 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV);
Ursula Brauna046d572017-01-09 16:55:16 +0100753
Hans Wippel3b2dec22018-05-18 09:34:18 +0200754 /* perform CLC handshake */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200755 rc = smc_connect_clc(smc, smc_type, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200756 if (rc) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200757 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200758 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200759 }
Ursula Brauna046d572017-01-09 16:55:16 +0100760
Hans Wippel41349842018-06-28 19:05:11 +0200761 /* depending on previous steps, connect using rdma or ism */
762 if (rdma_supported && aclc.hdr.path == SMC_TYPE_R)
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200763 rc = smc_connect_rdma(smc, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200764 else if (ism_supported && aclc.hdr.path == SMC_TYPE_D)
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200765 rc = smc_connect_ism(smc, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200766 else
Karsten Graul603cc142018-07-25 16:35:32 +0200767 rc = SMC_CLC_DECL_MODEUNSUPP;
Hans Wippel41349842018-06-28 19:05:11 +0200768 if (rc) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200769 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200770 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200771 }
Ursula Brauna046d572017-01-09 16:55:16 +0100772
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200773 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200774 return 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100775}
776
Ursula Braun24ac3a02018-06-27 17:59:50 +0200777static void smc_connect_work(struct work_struct *work)
778{
779 struct smc_sock *smc = container_of(work, struct smc_sock,
780 connect_work);
Ursula Braun50717a32019-04-12 12:57:23 +0200781 long timeo = smc->sk.sk_sndtimeo;
782 int rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200783
Ursula Braun50717a32019-04-12 12:57:23 +0200784 if (!timeo)
785 timeo = MAX_SCHEDULE_TIMEOUT;
786 lock_sock(smc->clcsock->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200787 if (smc->clcsock->sk->sk_err) {
788 smc->sk.sk_err = smc->clcsock->sk->sk_err;
Ursula Braun50717a32019-04-12 12:57:23 +0200789 } else if ((1 << smc->clcsock->sk->sk_state) &
790 (TCPF_SYN_SENT | TCP_SYN_RECV)) {
791 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
792 if ((rc == -EPIPE) &&
793 ((1 << smc->clcsock->sk->sk_state) &
794 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
795 rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200796 }
Ursula Braun50717a32019-04-12 12:57:23 +0200797 release_sock(smc->clcsock->sk);
798 lock_sock(&smc->sk);
799 if (rc != 0 || smc->sk.sk_err) {
800 smc->sk.sk_state = SMC_CLOSED;
801 if (rc == -EPIPE || rc == -EAGAIN)
802 smc->sk.sk_err = EPIPE;
803 else if (signal_pending(current))
804 smc->sk.sk_err = -sock_intr_errno(timeo);
Ursula Braun6d6dd522019-11-12 16:03:41 +0100805 sock_put(&smc->sk); /* passive closing */
Ursula Braun24ac3a02018-06-27 17:59:50 +0200806 goto out;
807 }
808
809 rc = __smc_connect(smc);
810 if (rc < 0)
811 smc->sk.sk_err = -rc;
812
813out:
Ursula Braun07603b22019-04-11 11:17:32 +0200814 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
815 if (smc->sk.sk_err) {
816 smc->sk.sk_state_change(&smc->sk);
817 } else { /* allow polling before and after fallback decision */
818 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
819 smc->sk.sk_write_space(&smc->sk);
820 }
821 }
Ursula Braun24ac3a02018-06-27 17:59:50 +0200822 release_sock(&smc->sk);
823}
824
Ursula Braunac713872017-01-09 16:55:13 +0100825static int smc_connect(struct socket *sock, struct sockaddr *addr,
826 int alen, int flags)
827{
828 struct sock *sk = sock->sk;
829 struct smc_sock *smc;
830 int rc = -EINVAL;
831
832 smc = smc_sk(sk);
833
834 /* separate smc parameter checking to be safe */
835 if (alen < sizeof(addr->sa_family))
836 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100837 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100838 goto out_err;
839
840 lock_sock(sk);
841 switch (sk->sk_state) {
842 default:
843 goto out;
844 case SMC_ACTIVE:
845 rc = -EISCONN;
846 goto out;
847 case SMC_INIT:
848 rc = 0;
849 break;
850 }
851
852 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200853 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun50717a32019-04-12 12:57:23 +0200854 if (smc->connect_nonblock) {
855 rc = -EALREADY;
856 goto out;
857 }
858 rc = kernel_connect(smc->clcsock, addr, alen, flags);
859 if (rc && rc != -EINPROGRESS)
860 goto out;
Ursula Braun301428e2019-10-29 12:41:26 +0100861
862 sock_hold(&smc->sk); /* sock put in passive closing */
Ursula Braun86434742019-12-12 22:35:58 +0100863 if (smc->use_fallback)
864 goto out;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200865 if (flags & O_NONBLOCK) {
Ursula Braun50717a32019-04-12 12:57:23 +0200866 if (schedule_work(&smc->connect_work))
867 smc->connect_nonblock = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200868 rc = -EINPROGRESS;
869 } else {
Ursula Braun24ac3a02018-06-27 17:59:50 +0200870 rc = __smc_connect(smc);
871 if (rc < 0)
872 goto out;
873 else
874 rc = 0; /* success cases including fallback */
875 }
Ursula Braunac713872017-01-09 16:55:13 +0100876
877out:
878 release_sock(sk);
879out_err:
880 return rc;
881}
882
883static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
884{
Ursula Braun3163c502018-01-24 10:28:12 +0100885 struct socket *new_clcsock = NULL;
886 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100887 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -0800888 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +0100889
Ursula Braun3163c502018-01-24 10:28:12 +0100890 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100891 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100892 if (!new_sk) {
893 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100894 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100895 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100896 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100897 goto out;
898 }
899 *new_smc = smc_sk(new_sk);
900
Myungho Jung78abe3d2018-12-18 09:02:25 -0800901 mutex_lock(&lsmc->clcsock_release_lock);
902 if (lsmc->clcsock)
903 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
904 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +0100905 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100906 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100907 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100908 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +0200909 new_sk->sk_prot->unhash(new_sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100910 if (new_clcsock)
911 sock_release(new_clcsock);
912 new_sk->sk_state = SMC_CLOSED;
913 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun51f1de72018-01-26 09:28:48 +0100914 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100915 *new_smc = NULL;
916 goto out;
917 }
918
919 (*new_smc)->clcsock = new_clcsock;
920out:
921 return rc;
922}
923
Ursula Brauna046d572017-01-09 16:55:16 +0100924/* add a just created sock to the accept queue of the listen sock as
925 * candidate for a following socket accept call from user space
926 */
927static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
928{
929 struct smc_sock *par = smc_sk(parent);
930
Ursula Braun51f1de72018-01-26 09:28:48 +0100931 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100932 spin_lock(&par->accept_q_lock);
933 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
934 spin_unlock(&par->accept_q_lock);
935 sk_acceptq_added(parent);
936}
937
938/* remove a socket from the accept queue of its parental listening socket */
939static void smc_accept_unlink(struct sock *sk)
940{
941 struct smc_sock *par = smc_sk(sk)->listen_smc;
942
943 spin_lock(&par->accept_q_lock);
944 list_del_init(&smc_sk(sk)->accept_q);
945 spin_unlock(&par->accept_q_lock);
946 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100947 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100948}
949
950/* remove a sock from the accept queue to bind it to a new socket created
951 * for a socket accept call from user space
952 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100953struct sock *smc_accept_dequeue(struct sock *parent,
954 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100955{
956 struct smc_sock *isk, *n;
957 struct sock *new_sk;
958
959 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
960 new_sk = (struct sock *)isk;
961
962 smc_accept_unlink(new_sk);
963 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +0200964 new_sk->sk_prot->unhash(new_sk);
Ursula Braun127f4972018-01-26 09:28:49 +0100965 if (isk->clcsock) {
966 sock_release(isk->clcsock);
967 isk->clcsock = NULL;
968 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100969 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +0100970 continue;
971 }
Ursula Braun07603b22019-04-11 11:17:32 +0200972 if (new_sock) {
Ursula Brauna046d572017-01-09 16:55:16 +0100973 sock_graft(new_sk, new_sock);
Ursula Braun07603b22019-04-11 11:17:32 +0200974 if (isk->use_fallback) {
975 smc_sk(new_sk)->clcsock->file = new_sock->file;
976 isk->clcsock->file->private_data = isk->clcsock;
977 }
978 }
Ursula Brauna046d572017-01-09 16:55:16 +0100979 return new_sk;
980 }
981 return NULL;
982}
983
984/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +0100985void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +0100986{
987 struct smc_sock *smc = smc_sk(sk);
988
Ursula Braun81cf4f42019-10-21 16:13:15 +0200989 sock_hold(sk); /* sock_put below */
Ursula Braunb38d7322017-01-09 16:55:25 +0100990 lock_sock(sk);
991 if (!sk->sk_lingertime)
992 /* wait for peer closing */
993 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun39f41f32019-06-27 15:04:52 +0200994 __smc_release(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +0100995 release_sock(sk);
Ursula Braun81cf4f42019-10-21 16:13:15 +0200996 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +0100997 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +0100998}
999
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001000static int smc_serv_conf_first_link(struct smc_sock *smc)
1001{
Karsten Graul877ae5b2018-05-02 16:56:44 +02001002 struct net *net = sock_net(smc->clcsock->sk);
Karsten Graul387707f2020-04-29 17:10:40 +02001003 struct smc_link *link = smc->conn.lnk;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001004 int rest;
1005 int rc;
1006
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;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001092
1093 smc_listen_out(new_smc);
1094}
1095
1096/* listen worker: decline and fall back if possible */
1097static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1098 int local_contact)
1099{
1100 /* RDMA setup failed, switch back to TCP */
1101 if (local_contact == SMC_FIRST_CONTACT)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001102 smc_lgr_cleanup_early(&new_smc->conn);
1103 else
1104 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001105 if (reason_code < 0) { /* error, no fallback possible */
1106 smc_listen_out_err(new_smc);
1107 return;
1108 }
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)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001171 smc_lgr_cleanup_early(&new_smc->conn);
1172 else
1173 smc_conn_free(&new_smc->conn);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001174 return SMC_CLC_DECL_SMCDNOTALK;
Hans Wippel41349842018-06-28 19:05:11 +02001175 }
1176
1177 /* Create send and receive buffers */
1178 if (smc_buf_create(new_smc, true)) {
Karsten Graul7a627252019-04-12 12:57:30 +02001179 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001180 smc_lgr_cleanup_early(&new_smc->conn);
1181 else
1182 smc_conn_free(&new_smc->conn);
Hans Wippel41349842018-06-28 19:05:11 +02001183 return SMC_CLC_DECL_MEM;
1184 }
1185
1186 return 0;
1187}
1188
Hans Wippel3b2dec22018-05-18 09:34:18 +02001189/* listen worker: register buffers */
1190static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
1191{
Karsten Graul387707f2020-04-29 17:10:40 +02001192 struct smc_link *link = new_smc->conn.lnk;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001193
1194 if (local_contact != SMC_FIRST_CONTACT) {
Karsten Graulc7674c02018-11-22 10:26:43 +01001195 if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true))
1196 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001197 }
1198 smc_rmb_sync_sg_for_device(&new_smc->conn);
1199
1200 return 0;
1201}
1202
1203/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001204static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1205 struct smc_clc_msg_accept_confirm *cclc,
1206 int local_contact)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001207{
Karsten Graul387707f2020-04-29 17:10:40 +02001208 struct smc_link *link = new_smc->conn.lnk;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001209 int reason_code = 0;
1210
1211 if (local_contact == SMC_FIRST_CONTACT)
1212 smc_link_save_peer_info(link, cclc);
1213
1214 if (smc_rmb_rtoken_handling(&new_smc->conn, cclc)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001215 reason_code = SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001216 goto decline;
1217 }
1218
1219 if (local_contact == SMC_FIRST_CONTACT) {
1220 if (smc_ib_ready_link(link)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001221 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001222 goto decline;
1223 }
1224 /* QP confirmation over RoCE fabric */
1225 reason_code = smc_serv_conf_first_link(new_smc);
1226 if (reason_code)
1227 goto decline;
1228 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001229 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001230
1231decline:
Hans Wippel3b2dec22018-05-18 09:34:18 +02001232 smc_listen_decline(new_smc, reason_code, local_contact);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001233 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001234}
1235
1236/* setup for RDMA connection of server */
1237static void smc_listen_work(struct work_struct *work)
1238{
1239 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1240 smc_listen_work);
1241 struct socket *newclcsock = new_smc->clcsock;
1242 struct smc_clc_msg_accept_confirm cclc;
1243 struct smc_clc_msg_proposal *pclc;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001244 struct smc_init_info ini = {0};
Hans Wippel41349842018-06-28 19:05:11 +02001245 bool ism_supported = false;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001246 u8 buf[SMC_CLC_MAX_LEN];
Hans Wippel3b2dec22018-05-18 09:34:18 +02001247 int rc = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001248
Karsten Graulfd577702019-04-11 11:17:30 +02001249 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1250 return smc_listen_out_err(new_smc);
1251
Hans Wippel3b2dec22018-05-18 09:34:18 +02001252 if (new_smc->use_fallback) {
1253 smc_listen_out_connected(new_smc);
1254 return;
1255 }
1256
1257 /* check if peer is smc capable */
1258 if (!tcp_sk(newclcsock->sk)->syn_smc) {
Ursula Braun07603b22019-04-11 11:17:32 +02001259 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001260 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001261 smc_listen_out_connected(new_smc);
1262 return;
1263 }
1264
1265 /* do inband token exchange -
1266 * wait for and receive SMC Proposal CLC message
1267 */
1268 pclc = (struct smc_clc_msg_proposal *)&buf;
Karsten Graul228bae02019-04-12 12:57:28 +02001269 rc = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
1270 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001271 if (rc)
1272 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001273
1274 /* IPSec connections opt out of SMC-R optimizations */
1275 if (using_ipsec(new_smc)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001276 rc = SMC_CLC_DECL_IPSEC;
1277 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001278 }
1279
Karsten Graul59886692019-04-12 12:57:25 +02001280 /* check for matching IP prefix and subnet length */
1281 rc = smc_listen_prfx_check(new_smc, pclc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001282 if (rc)
1283 goto out_decl;
Karsten Graul59886692019-04-12 12:57:25 +02001284
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001285 /* get vlan id from IP device */
1286 if (smc_vlan_by_tcpsk(new_smc->clcsock, &ini)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001287 rc = SMC_CLC_DECL_GETVLANERR;
1288 goto out_decl;
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001289 }
1290
Hans Wippel72a36a82019-02-07 15:56:18 +01001291 mutex_lock(&smc_server_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001292 smc_close_init(new_smc);
1293 smc_rx_init(new_smc);
1294 smc_tx_init(new_smc);
1295
Hans Wippel41349842018-06-28 19:05:11 +02001296 /* check if ISM is available */
Karsten Graul9aa68d22019-04-12 12:57:29 +02001297 if (pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) {
1298 ini.is_smcd = true; /* prepare ISM check */
1299 rc = smc_find_ism_device(new_smc, &ini);
1300 if (!rc)
Karsten Graul7a627252019-04-12 12:57:30 +02001301 rc = smc_listen_ism_init(new_smc, pclc, &ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001302 if (!rc)
1303 ism_supported = true;
1304 else if (pclc->hdr.path == SMC_TYPE_D)
1305 goto out_unlock; /* skip RDMA and decline */
1306 }
1307
1308 /* check if RDMA is available */
1309 if (!ism_supported) { /* SMC_TYPE_R or SMC_TYPE_B */
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001310 /* prepare RDMA check */
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001311 ini.is_smcd = false;
Ursula Braunca5f8d22019-10-23 15:44:06 +02001312 ini.ism_dev = NULL;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001313 ini.ib_lcl = &pclc->lcl;
Karsten Graul9aa68d22019-04-12 12:57:29 +02001314 rc = smc_find_rdma_device(new_smc, &ini);
1315 if (rc) {
1316 /* no RDMA device found */
1317 if (pclc->hdr.path == SMC_TYPE_B)
1318 /* neither ISM nor RDMA device found */
1319 rc = SMC_CLC_DECL_NOSMCDEV;
1320 goto out_unlock;
1321 }
Karsten Graul7a627252019-04-12 12:57:30 +02001322 rc = smc_listen_rdma_init(new_smc, &ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001323 if (rc)
1324 goto out_unlock;
Karsten Graul7a627252019-04-12 12:57:30 +02001325 rc = smc_listen_rdma_reg(new_smc, ini.cln_first_contact);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001326 if (rc)
1327 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001328 }
1329
1330 /* send SMC Accept CLC message */
Karsten Graul7a627252019-04-12 12:57:30 +02001331 rc = smc_clc_send_accept(new_smc, ini.cln_first_contact);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001332 if (rc)
1333 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001334
Hans Wippel62c71392019-02-07 15:56:17 +01001335 /* SMC-D does not need this lock any more */
1336 if (ism_supported)
Hans Wippel72a36a82019-02-07 15:56:18 +01001337 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001338
Hans Wippel3b2dec22018-05-18 09:34:18 +02001339 /* receive SMC Confirm CLC message */
Karsten Graul228bae02019-04-12 12:57:28 +02001340 rc = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
1341 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
1342 if (rc) {
Hans Wippel62c71392019-02-07 15:56:17 +01001343 if (!ism_supported)
Karsten Graul9aa68d22019-04-12 12:57:29 +02001344 goto out_unlock;
1345 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001346 }
1347
1348 /* finish worker */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001349 if (!ism_supported) {
Karsten Graul7a627252019-04-12 12:57:30 +02001350 rc = smc_listen_rdma_finish(new_smc, &cclc,
1351 ini.cln_first_contact);
Hans Wippel72a36a82019-02-07 15:56:18 +01001352 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001353 if (rc)
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001354 return;
1355 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001356 smc_conn_save_peer_info(new_smc, &cclc);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001357 smc_listen_out_connected(new_smc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001358 return;
1359
1360out_unlock:
1361 mutex_unlock(&smc_server_lgr_pending);
1362out_decl:
Karsten Graul7a627252019-04-12 12:57:30 +02001363 smc_listen_decline(new_smc, rc, ini.cln_first_contact);
Ursula Brauna046d572017-01-09 16:55:16 +01001364}
1365
1366static void smc_tcp_listen_work(struct work_struct *work)
1367{
1368 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1369 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001370 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001371 struct smc_sock *new_smc;
1372 int rc = 0;
1373
Ursula Braun3163c502018-01-24 10:28:12 +01001374 lock_sock(lsk);
1375 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001376 rc = smc_clcsock_accept(lsmc, &new_smc);
1377 if (rc)
1378 goto out;
1379 if (!new_smc)
1380 continue;
1381
1382 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001383 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001384 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001385 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001386 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1387 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001388 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1389 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001390 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1391 if (!schedule_work(&new_smc->smc_listen_work))
1392 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001393 }
1394
1395out:
Ursula Braun3163c502018-01-24 10:28:12 +01001396 release_sock(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001397 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +01001398}
1399
Ursula Braunac713872017-01-09 16:55:13 +01001400static int smc_listen(struct socket *sock, int backlog)
1401{
1402 struct sock *sk = sock->sk;
1403 struct smc_sock *smc;
1404 int rc;
1405
1406 smc = smc_sk(sk);
1407 lock_sock(sk);
1408
1409 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +02001410 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
1411 smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +01001412 goto out;
1413
1414 rc = 0;
1415 if (sk->sk_state == SMC_LISTEN) {
1416 sk->sk_max_ack_backlog = backlog;
1417 goto out;
1418 }
1419 /* some socket options are handled in core, so we could not apply
1420 * them to the clc socket -- copy smc socket options to clc socket
1421 */
1422 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001423 if (!smc->use_fallback)
1424 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001425
1426 rc = kernel_listen(smc->clcsock, backlog);
1427 if (rc)
1428 goto out;
1429 sk->sk_max_ack_backlog = backlog;
1430 sk->sk_ack_backlog = 0;
1431 sk->sk_state = SMC_LISTEN;
Ursula Braun51f1de72018-01-26 09:28:48 +01001432 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1433 if (!schedule_work(&smc->tcp_listen_work))
1434 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001435
1436out:
1437 release_sock(sk);
1438 return rc;
1439}
1440
1441static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001442 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001443{
Ursula Brauna046d572017-01-09 16:55:16 +01001444 struct sock *sk = sock->sk, *nsk;
1445 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001446 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001447 long timeo;
1448 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001449
1450 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001451 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001452 lock_sock(sk);
1453
1454 if (lsmc->sk.sk_state != SMC_LISTEN) {
1455 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001456 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001457 goto out;
1458 }
1459
Ursula Brauna046d572017-01-09 16:55:16 +01001460 /* Wait for an incoming connection */
1461 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1462 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1463 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1464 set_current_state(TASK_INTERRUPTIBLE);
1465 if (!timeo) {
1466 rc = -EAGAIN;
1467 break;
1468 }
1469 release_sock(sk);
1470 timeo = schedule_timeout(timeo);
1471 /* wakeup by sk_data_ready in smc_listen_work() */
1472 sched_annotate_sleep();
1473 lock_sock(sk);
1474 if (signal_pending(current)) {
1475 rc = sock_intr_errno(timeo);
1476 break;
1477 }
1478 }
1479 set_current_state(TASK_RUNNING);
1480 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001481
Ursula Brauna046d572017-01-09 16:55:16 +01001482 if (!rc)
1483 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001484 release_sock(sk);
1485 if (rc)
1486 goto out;
1487
1488 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1489 /* wait till data arrives on the socket */
1490 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1491 MSEC_PER_SEC);
1492 if (smc_sk(nsk)->use_fallback) {
1493 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1494
1495 lock_sock(clcsk);
1496 if (skb_queue_empty(&clcsk->sk_receive_queue))
1497 sk_wait_data(clcsk, &timeo, NULL);
1498 release_sock(clcsk);
1499 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1500 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001501 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001502 release_sock(nsk);
1503 }
1504 }
Ursula Braunac713872017-01-09 16:55:13 +01001505
1506out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001507 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001508 return rc;
1509}
1510
1511static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001512 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001513{
1514 struct smc_sock *smc;
1515
Ursula Braunb38d7322017-01-09 16:55:25 +01001516 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1517 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001518 return -ENOTCONN;
1519
1520 smc = smc_sk(sock->sk);
1521
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001522 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001523}
1524
1525static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1526{
1527 struct sock *sk = sock->sk;
1528 struct smc_sock *smc;
1529 int rc = -EPIPE;
1530
1531 smc = smc_sk(sk);
1532 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001533 if ((sk->sk_state != SMC_ACTIVE) &&
1534 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1535 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001536 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001537
1538 if (msg->msg_flags & MSG_FASTOPEN) {
Ursula Brauncd206362019-08-02 10:47:50 +02001539 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001540 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001541 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001542 } else {
1543 rc = -EINVAL;
1544 goto out;
1545 }
1546 }
1547
Ursula Braunac713872017-01-09 16:55:13 +01001548 if (smc->use_fallback)
1549 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1550 else
Ursula Braune6727f32017-01-09 16:55:23 +01001551 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001552out:
1553 release_sock(sk);
1554 return rc;
1555}
1556
1557static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1558 int flags)
1559{
1560 struct sock *sk = sock->sk;
1561 struct smc_sock *smc;
1562 int rc = -ENOTCONN;
1563
1564 smc = smc_sk(sk);
1565 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001566 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1567 /* socket was connected before, no more data to read */
1568 rc = 0;
1569 goto out;
1570 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001571 if ((sk->sk_state == SMC_INIT) ||
1572 (sk->sk_state == SMC_LISTEN) ||
1573 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001574 goto out;
1575
Ursula Braunb38d7322017-01-09 16:55:25 +01001576 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1577 rc = 0;
1578 goto out;
1579 }
1580
Stefan Raspl9014db22018-05-03 18:12:39 +02001581 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001582 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001583 } else {
1584 msg->msg_namelen = 0;
1585 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1586 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001587
Ursula Braunac713872017-01-09 16:55:13 +01001588out:
1589 release_sock(sk);
1590 return rc;
1591}
1592
Al Viroade994f2017-07-03 00:01:49 -04001593static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001594{
Ursula Braun8dce2782018-01-26 09:28:47 +01001595 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001596 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001597
Ursula Braun8dce2782018-01-26 09:28:47 +01001598 spin_lock(&isk->accept_q_lock);
1599 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001600 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001601 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001602
Ursula Braun8dce2782018-01-26 09:28:47 +01001603 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001604}
1605
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001606static __poll_t smc_poll(struct file *file, struct socket *sock,
1607 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01001608{
1609 struct sock *sk = sock->sk;
Ursula Braunac713872017-01-09 16:55:13 +01001610 struct smc_sock *smc;
Ursula Braun50717a32019-04-12 12:57:23 +02001611 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001612
Ursula Braun8dce2782018-01-26 09:28:47 +01001613 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001614 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001615
Ursula Braunac713872017-01-09 16:55:13 +01001616 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02001617 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01001618 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001619 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02001620 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braunac713872017-01-09 16:55:13 +01001621 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07001622 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02001623 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01001624 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001625 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001626 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1627 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001628 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001629 if (sk->sk_state == SMC_LISTEN) {
1630 /* woken up by sk_data_ready in smc_listen_work() */
Ursula Braun50717a32019-04-12 12:57:23 +02001631 mask |= smc_accept_poll(sk);
1632 } else if (smc->use_fallback) { /* as result of connect_work()*/
1633 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
1634 wait);
1635 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun8dce2782018-01-26 09:28:47 +01001636 } else {
Ursula Braun50717a32019-04-12 12:57:23 +02001637 if ((sk->sk_state != SMC_INIT &&
1638 atomic_read(&smc->conn.sndbuf_space)) ||
Ursula Braun8dce2782018-01-26 09:28:47 +01001639 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001640 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001641 } else {
1642 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1643 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1644 }
1645 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001646 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001647 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001648 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001649 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001650 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02001651 if (smc->conn.urg_state == SMC_URG_VALID)
1652 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01001653 }
Ursula Braunac713872017-01-09 16:55:13 +01001654 }
1655
1656 return mask;
1657}
1658
1659static int smc_shutdown(struct socket *sock, int how)
1660{
1661 struct sock *sk = sock->sk;
1662 struct smc_sock *smc;
1663 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001664 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001665
1666 smc = smc_sk(sk);
1667
1668 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001669 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001670
1671 lock_sock(sk);
1672
1673 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02001674 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01001675 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1676 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1677 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1678 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1679 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001680 goto out;
1681 if (smc->use_fallback) {
1682 rc = kernel_sock_shutdown(smc->clcsock, how);
1683 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1684 if (sk->sk_shutdown == SHUTDOWN_MASK)
1685 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001686 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001687 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001688 switch (how) {
1689 case SHUT_RDWR: /* shutdown in both directions */
1690 rc = smc_close_active(smc);
1691 break;
1692 case SHUT_WR:
1693 rc = smc_close_shutdown_write(smc);
1694 break;
1695 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001696 rc = 0;
1697 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001698 break;
1699 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001700 if (smc->clcsock)
1701 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001702 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1703 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001704
1705out:
1706 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001707 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001708}
1709
1710static int smc_setsockopt(struct socket *sock, int level, int optname,
1711 char __user *optval, unsigned int optlen)
1712{
1713 struct sock *sk = sock->sk;
1714 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001715 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001716
1717 smc = smc_sk(sk);
1718
1719 /* generic setsockopts reaching us here always apply to the
1720 * CLC socket
1721 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001722 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1723 optval, optlen);
1724 if (smc->clcsock->sk->sk_err) {
1725 sk->sk_err = smc->clcsock->sk->sk_err;
1726 sk->sk_error_report(sk);
1727 }
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001728
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001729 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00001730 return -EINVAL;
Ursula Braunac0107e2018-07-18 15:22:50 +02001731 if (get_user(val, (int __user *)optval))
1732 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001733
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001734 lock_sock(sk);
Ursula Braun86434742019-12-12 22:35:58 +01001735 if (rc || smc->use_fallback)
1736 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001737 switch (optname) {
1738 case TCP_ULP:
1739 case TCP_FASTOPEN:
1740 case TCP_FASTOPEN_CONNECT:
1741 case TCP_FASTOPEN_KEY:
1742 case TCP_FASTOPEN_NO_COOKIE:
1743 /* option not supported by SMC */
Ursula Braun8204df72019-11-15 12:39:30 +01001744 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001745 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001746 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001747 } else {
Ursula Braun86434742019-12-12 22:35:58 +01001748 rc = -EINVAL;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001749 }
1750 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001751 case TCP_NODELAY:
Ursula Braunf9cedf12019-08-02 10:16:38 +02001752 if (sk->sk_state != SMC_INIT &&
1753 sk->sk_state != SMC_LISTEN &&
1754 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01001755 if (val)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001756 mod_delayed_work(system_wq, &smc->conn.tx_work,
1757 0);
1758 }
1759 break;
1760 case TCP_CORK:
Ursula Braunf9cedf12019-08-02 10:16:38 +02001761 if (sk->sk_state != SMC_INIT &&
1762 sk->sk_state != SMC_LISTEN &&
1763 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01001764 if (!val)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001765 mod_delayed_work(system_wq, &smc->conn.tx_work,
1766 0);
1767 }
1768 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001769 case TCP_DEFER_ACCEPT:
1770 smc->sockopt_defer_accept = val;
1771 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001772 default:
1773 break;
1774 }
Ursula Braun86434742019-12-12 22:35:58 +01001775out:
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001776 release_sock(sk);
1777
1778 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001779}
1780
1781static int smc_getsockopt(struct socket *sock, int level, int optname,
1782 char __user *optval, int __user *optlen)
1783{
1784 struct smc_sock *smc;
1785
1786 smc = smc_sk(sock->sk);
1787 /* socket options apply to the CLC socket */
1788 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1789 optval, optlen);
1790}
1791
1792static int smc_ioctl(struct socket *sock, unsigned int cmd,
1793 unsigned long arg)
1794{
Stefan Rasplde8474e2018-05-23 16:38:11 +02001795 union smc_host_cursor cons, urg;
1796 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01001797 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001798 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001799
1800 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001801 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02001802 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02001803 if (smc->use_fallback) {
1804 if (!smc->clcsock) {
1805 release_sock(&smc->sk);
1806 return -EBADF;
1807 }
1808 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1809 release_sock(&smc->sk);
1810 return answ;
1811 }
Ursula Braun9b67e262018-05-02 16:56:46 +02001812 switch (cmd) {
1813 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02001814 if (smc->sk.sk_state == SMC_LISTEN) {
1815 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001816 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001817 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001818 if (smc->sk.sk_state == SMC_INIT ||
1819 smc->sk.sk_state == SMC_CLOSED)
1820 answ = 0;
1821 else
1822 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02001823 break;
1824 case SIOCOUTQ:
1825 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02001826 if (smc->sk.sk_state == SMC_LISTEN) {
1827 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001828 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001829 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001830 if (smc->sk.sk_state == SMC_INIT ||
1831 smc->sk.sk_state == SMC_CLOSED)
1832 answ = 0;
1833 else
1834 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02001835 atomic_read(&smc->conn.sndbuf_space);
1836 break;
1837 case SIOCOUTQNSD:
1838 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02001839 if (smc->sk.sk_state == SMC_LISTEN) {
1840 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001841 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001842 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001843 if (smc->sk.sk_state == SMC_INIT ||
1844 smc->sk.sk_state == SMC_CLOSED)
1845 answ = 0;
1846 else
1847 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02001848 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02001849 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02001850 if (smc->sk.sk_state == SMC_LISTEN) {
1851 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001852 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001853 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001854 if (smc->sk.sk_state == SMC_INIT ||
1855 smc->sk.sk_state == SMC_CLOSED) {
1856 answ = 0;
1857 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02001858 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1859 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001860 answ = smc_curs_diff(conn->rmb_desc->len,
1861 &cons, &urg) == 1;
1862 }
1863 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02001864 default:
Ursula Braun1992d992018-07-16 13:56:52 +02001865 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001866 return -ENOIOCTLCMD;
1867 }
Ursula Braun1992d992018-07-16 13:56:52 +02001868 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001869
1870 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001871}
1872
1873static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1874 int offset, size_t size, int flags)
1875{
1876 struct sock *sk = sock->sk;
1877 struct smc_sock *smc;
1878 int rc = -EPIPE;
1879
1880 smc = smc_sk(sk);
1881 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001882 if (sk->sk_state != SMC_ACTIVE) {
1883 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001884 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001885 }
1886 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001887 if (smc->use_fallback)
1888 rc = kernel_sendpage(smc->clcsock, page, offset,
1889 size, flags);
1890 else
1891 rc = sock_no_sendpage(sock, page, offset, size, flags);
1892
1893out:
Ursula Braunac713872017-01-09 16:55:13 +01001894 return rc;
1895}
1896
Stefan Raspl9014db22018-05-03 18:12:39 +02001897/* Map the affected portions of the rmbe into an spd, note the number of bytes
1898 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1899 * updates till whenever a respective page has been fully processed.
1900 * Note that subsequent recv() calls have to wait till all splice() processing
1901 * completed.
1902 */
Ursula Braunac713872017-01-09 16:55:13 +01001903static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1904 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001905 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001906{
1907 struct sock *sk = sock->sk;
1908 struct smc_sock *smc;
1909 int rc = -ENOTCONN;
1910
1911 smc = smc_sk(sk);
1912 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001913 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1914 /* socket was connected before, no more data to read */
1915 rc = 0;
1916 goto out;
1917 }
Stefan Raspl9014db22018-05-03 18:12:39 +02001918 if (sk->sk_state == SMC_INIT ||
1919 sk->sk_state == SMC_LISTEN ||
1920 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001921 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001922
1923 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1924 rc = 0;
1925 goto out;
1926 }
1927
Ursula Braunac713872017-01-09 16:55:13 +01001928 if (smc->use_fallback) {
1929 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1930 pipe, len, flags);
1931 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001932 if (*ppos) {
1933 rc = -ESPIPE;
1934 goto out;
1935 }
1936 if (flags & SPLICE_F_NONBLOCK)
1937 flags = MSG_DONTWAIT;
1938 else
1939 flags = 0;
1940 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001941 }
1942out:
1943 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001944
Ursula Braunac713872017-01-09 16:55:13 +01001945 return rc;
1946}
1947
1948/* must look like tcp */
1949static const struct proto_ops smc_sock_ops = {
1950 .family = PF_SMC,
1951 .owner = THIS_MODULE,
1952 .release = smc_release,
1953 .bind = smc_bind,
1954 .connect = smc_connect,
1955 .socketpair = sock_no_socketpair,
1956 .accept = smc_accept,
1957 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001958 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01001959 .ioctl = smc_ioctl,
1960 .listen = smc_listen,
1961 .shutdown = smc_shutdown,
1962 .setsockopt = smc_setsockopt,
1963 .getsockopt = smc_getsockopt,
1964 .sendmsg = smc_sendmsg,
1965 .recvmsg = smc_recvmsg,
1966 .mmap = sock_no_mmap,
1967 .sendpage = smc_sendpage,
1968 .splice_read = smc_splice_read,
1969};
1970
1971static int smc_create(struct net *net, struct socket *sock, int protocol,
1972 int kern)
1973{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001974 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001975 struct smc_sock *smc;
1976 struct sock *sk;
1977 int rc;
1978
1979 rc = -ESOCKTNOSUPPORT;
1980 if (sock->type != SOCK_STREAM)
1981 goto out;
1982
1983 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001984 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01001985 goto out;
1986
1987 rc = -ENOBUFS;
1988 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001989 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001990 if (!sk)
1991 goto out;
1992
1993 /* create internal TCP socket for CLC handshake and fallback */
1994 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001995 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02001996 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001997 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1998 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01001999 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01002000 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01002001 goto out;
2002 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01002003 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
2004 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01002005
2006out:
2007 return rc;
2008}
2009
2010static const struct net_proto_family smc_sock_family_ops = {
2011 .family = PF_SMC,
2012 .owner = THIS_MODULE,
2013 .create = smc_create,
2014};
2015
Hans Wippel64e28b52019-02-21 13:01:02 +01002016unsigned int smc_net_id;
2017
2018static __net_init int smc_net_init(struct net *net)
2019{
2020 return smc_pnet_net_init(net);
2021}
2022
2023static void __net_exit smc_net_exit(struct net *net)
2024{
2025 smc_pnet_net_exit(net);
2026}
2027
2028static struct pernet_operations smc_net_ops = {
2029 .init = smc_net_init,
2030 .exit = smc_net_exit,
2031 .id = &smc_net_id,
2032 .size = sizeof(struct smc_net),
2033};
2034
Ursula Braunac713872017-01-09 16:55:13 +01002035static int __init smc_init(void)
2036{
2037 int rc;
2038
Hans Wippel64e28b52019-02-21 13:01:02 +01002039 rc = register_pernet_subsys(&smc_net_ops);
2040 if (rc)
2041 return rc;
2042
Thomas Richter6812baa2017-01-09 16:55:15 +01002043 rc = smc_pnet_init();
2044 if (rc)
YueHaibing8c33bf12019-06-26 17:47:50 +02002045 goto out_pernet_subsys;
Thomas Richter6812baa2017-01-09 16:55:15 +01002046
Ursula Braun6dabd402019-11-16 17:47:29 +01002047 rc = smc_core_init();
2048 if (rc) {
2049 pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
2050 goto out_pnet;
2051 }
2052
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002053 rc = smc_llc_init();
2054 if (rc) {
2055 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002056 goto out_core;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002057 }
2058
Ursula Braun5f083182017-01-09 16:55:22 +01002059 rc = smc_cdc_init();
2060 if (rc) {
2061 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002062 goto out_core;
Ursula Braun5f083182017-01-09 16:55:22 +01002063 }
2064
Ursula Braunac713872017-01-09 16:55:13 +01002065 rc = proto_register(&smc_proto, 1);
2066 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01002067 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002068 goto out_core;
Ursula Braunac713872017-01-09 16:55:13 +01002069 }
2070
Karsten Graulaaa4d332018-03-16 15:06:41 +01002071 rc = proto_register(&smc_proto6, 1);
2072 if (rc) {
2073 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2074 goto out_proto;
2075 }
2076
Ursula Braunac713872017-01-09 16:55:13 +01002077 rc = sock_register(&smc_sock_family_ops);
2078 if (rc) {
2079 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002080 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01002081 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01002082 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002083 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01002084
Ursula Brauna4cf0442017-01-09 16:55:14 +01002085 rc = smc_ib_register_client();
2086 if (rc) {
2087 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2088 goto out_sock;
2089 }
2090
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002091 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002092 return 0;
2093
Ursula Brauna4cf0442017-01-09 16:55:14 +01002094out_sock:
2095 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002096out_proto6:
2097 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002098out_proto:
2099 proto_unregister(&smc_proto);
Ursula Braun6dabd402019-11-16 17:47:29 +01002100out_core:
2101 smc_core_exit();
Thomas Richter6812baa2017-01-09 16:55:15 +01002102out_pnet:
2103 smc_pnet_exit();
YueHaibing8c33bf12019-06-26 17:47:50 +02002104out_pernet_subsys:
2105 unregister_pernet_subsys(&smc_net_ops);
2106
Ursula Braunac713872017-01-09 16:55:13 +01002107 return rc;
2108}
2109
2110static void __exit smc_exit(void)
2111{
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002112 static_branch_disable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002113 sock_unregister(PF_SMC);
Ursula Braun6dabd402019-11-16 17:47:29 +01002114 smc_core_exit();
2115 smc_ib_unregister_client();
Karsten Graulaaa4d332018-03-16 15:06:41 +01002116 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002117 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002118 smc_pnet_exit();
Hans Wippel64e28b52019-02-21 13:01:02 +01002119 unregister_pernet_subsys(&smc_net_ops);
Ursula Braun4ead9c92019-11-16 17:47:31 +01002120 rcu_barrier();
Ursula Braunac713872017-01-09 16:55:13 +01002121}
2122
2123module_init(smc_init);
2124module_exit(smc_exit);
2125
2126MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2127MODULE_DESCRIPTION("smc socket address family");
2128MODULE_LICENSE("GPL");
2129MODULE_ALIAS_NETPROTO(PF_SMC);