blob: cea3c36ea0da6ed07d9375a0e1ac4dd27317132b [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>
Ingo Molnarc3edc402017-02-02 08:35:14 +010028
Ursula Braunac713872017-01-09 16:55:13 +010029#include <net/sock.h>
Ursula Brauna046d572017-01-09 16:55:16 +010030#include <net/tcp.h>
Ursula Braunf16a7dd2017-01-09 16:55:26 +010031#include <net/smc.h>
Ursula Braun9b67e262018-05-02 16:56:46 +020032#include <asm/ioctls.h>
Ursula Braunac713872017-01-09 16:55:13 +010033
Hans Wippel64e28b52019-02-21 13:01:02 +010034#include <net/net_namespace.h>
35#include <net/netns/generic.h>
36#include "smc_netns.h"
37
Ursula Braunac713872017-01-09 16:55:13 +010038#include "smc.h"
Ursula Brauna046d572017-01-09 16:55:16 +010039#include "smc_clc.h"
Ursula Braun9bf9abe2017-01-09 16:55:21 +010040#include "smc_llc.h"
Ursula Braun5f083182017-01-09 16:55:22 +010041#include "smc_cdc.h"
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010042#include "smc_core.h"
Ursula Brauna4cf0442017-01-09 16:55:14 +010043#include "smc_ib.h"
Hans Wippel41349842018-06-28 19:05:11 +020044#include "smc_ism.h"
Thomas Richter6812baa2017-01-09 16:55:15 +010045#include "smc_pnet.h"
Ursula Braune6727f32017-01-09 16:55:23 +010046#include "smc_tx.h"
Ursula Braun952310c2017-01-09 16:55:24 +010047#include "smc_rx.h"
Ursula Braunb38d7322017-01-09 16:55:25 +010048#include "smc_close.h"
Ursula Braunac713872017-01-09 16:55:13 +010049
Hans Wippel72a36a82019-02-07 15:56:18 +010050static DEFINE_MUTEX(smc_server_lgr_pending); /* serialize link group
51 * creation on server
52 */
53static DEFINE_MUTEX(smc_client_lgr_pending); /* serialize link group
54 * creation on client
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010055 */
56
Ursula Brauna046d572017-01-09 16:55:16 +010057static void smc_tcp_listen_work(struct work_struct *);
Ursula Braun24ac3a02018-06-27 17:59:50 +020058static void smc_connect_work(struct work_struct *);
Ursula Brauna046d572017-01-09 16:55:16 +010059
Ursula Braunac713872017-01-09 16:55:13 +010060static void smc_set_keepalive(struct sock *sk, int val)
61{
62 struct smc_sock *smc = smc_sk(sk);
63
64 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
65}
66
Ursula Braunf16a7dd2017-01-09 16:55:26 +010067static struct smc_hashinfo smc_v4_hashinfo = {
68 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
69};
70
Karsten Graulaaa4d332018-03-16 15:06:41 +010071static struct smc_hashinfo smc_v6_hashinfo = {
72 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
73};
74
Ursula Braunf16a7dd2017-01-09 16:55:26 +010075int smc_hash_sk(struct sock *sk)
76{
77 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
78 struct hlist_head *head;
79
80 head = &h->ht;
81
82 write_lock_bh(&h->lock);
83 sk_add_node(sk, head);
84 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
85 write_unlock_bh(&h->lock);
86
87 return 0;
88}
89EXPORT_SYMBOL_GPL(smc_hash_sk);
90
91void smc_unhash_sk(struct sock *sk)
92{
93 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
94
95 write_lock_bh(&h->lock);
96 if (sk_del_node_init(sk))
97 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
98 write_unlock_bh(&h->lock);
99}
100EXPORT_SYMBOL_GPL(smc_unhash_sk);
101
102struct proto smc_proto = {
Ursula Braunac713872017-01-09 16:55:13 +0100103 .name = "SMC",
104 .owner = THIS_MODULE,
105 .keepalive = smc_set_keepalive,
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100106 .hash = smc_hash_sk,
107 .unhash = smc_unhash_sk,
Ursula Braunac713872017-01-09 16:55:13 +0100108 .obj_size = sizeof(struct smc_sock),
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100109 .h.smc_hash = &smc_v4_hashinfo,
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800110 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Ursula Braunac713872017-01-09 16:55:13 +0100111};
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100112EXPORT_SYMBOL_GPL(smc_proto);
Ursula Braunac713872017-01-09 16:55:13 +0100113
Karsten Graulaaa4d332018-03-16 15:06:41 +0100114struct proto smc_proto6 = {
115 .name = "SMC6",
116 .owner = THIS_MODULE,
117 .keepalive = smc_set_keepalive,
118 .hash = smc_hash_sk,
119 .unhash = smc_unhash_sk,
120 .obj_size = sizeof(struct smc_sock),
121 .h.smc_hash = &smc_v6_hashinfo,
122 .slab_flags = SLAB_TYPESAFE_BY_RCU,
123};
124EXPORT_SYMBOL_GPL(smc_proto6);
125
Ursula Braunf536dff2019-10-23 15:44:05 +0200126static void smc_restore_fallback_changes(struct smc_sock *smc)
127{
128 smc->clcsock->file->private_data = smc->sk.sk_socket;
129 smc->clcsock->file = NULL;
130}
131
Ursula Braun39f41f32019-06-27 15:04:52 +0200132static int __smc_release(struct smc_sock *smc)
Ursula Braunac713872017-01-09 16:55:13 +0100133{
Ursula Braun39f41f32019-06-27 15:04:52 +0200134 struct sock *sk = &smc->sk;
Ursula Braunb38d7322017-01-09 16:55:25 +0100135 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +0100136
Ursula Braun51f1de72018-01-26 09:28:48 +0100137 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100138 rc = smc_close_active(smc);
139 sock_set_flag(sk, SOCK_DEAD);
140 sk->sk_shutdown |= SHUTDOWN_MASK;
Ursula Braunb03faa12019-02-07 15:56:15 +0100141 } else {
142 if (sk->sk_state != SMC_LISTEN && sk->sk_state != SMC_INIT)
143 sock_put(sk); /* passive closing */
144 if (sk->sk_state == SMC_LISTEN) {
145 /* wake up clcsock accept */
146 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
147 }
148 sk->sk_state = SMC_CLOSED;
149 sk->sk_state_change(sk);
Ursula Braunf536dff2019-10-23 15:44:05 +0200150 smc_restore_fallback_changes(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +0100151 }
Cong Wang26d92e92019-01-05 23:45:26 -0800152
153 sk->sk_prot->unhash(sk);
154
Ursula Braunb03faa12019-02-07 15:56:15 +0100155 if (sk->sk_state == SMC_CLOSED) {
156 if (smc->clcsock) {
Karsten Graulfd577702019-04-11 11:17:30 +0200157 release_sock(sk);
158 smc_clcsock_release(smc);
159 lock_sock(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800160 }
Ursula Braunb03faa12019-02-07 15:56:15 +0100161 if (!smc->use_fallback)
162 smc_conn_free(&smc->conn);
Ursula Braun51f1de72018-01-26 09:28:48 +0100163 }
Ursula Braunac713872017-01-09 16:55:13 +0100164
Ursula Braun39f41f32019-06-27 15:04:52 +0200165 return rc;
166}
167
168static int smc_release(struct socket *sock)
169{
170 struct sock *sk = sock->sk;
171 struct smc_sock *smc;
172 int rc = 0;
173
174 if (!sk)
175 goto out;
176
177 smc = smc_sk(sk);
178
179 /* cleanup for a dangling non-blocking connect */
180 if (smc->connect_nonblock && sk->sk_state == SMC_INIT)
181 tcp_abort(smc->clcsock->sk, ECONNABORTED);
182 flush_work(&smc->connect_work);
183
184 if (sk->sk_state == SMC_LISTEN)
185 /* smc_close_non_accepted() is called and acquires
186 * sock lock for child sockets again
187 */
188 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
189 else
190 lock_sock(sk);
191
192 rc = __smc_release(smc);
193
Ursula Braunac713872017-01-09 16:55:13 +0100194 /* detach socket */
195 sock_orphan(sk);
196 sock->sk = NULL;
197 release_sock(sk);
198
Ursula Braun51f1de72018-01-26 09:28:48 +0100199 sock_put(sk); /* final sock_put */
Ursula Braunac713872017-01-09 16:55:13 +0100200out:
Ursula Braunb38d7322017-01-09 16:55:25 +0100201 return rc;
Ursula Braunac713872017-01-09 16:55:13 +0100202}
203
204static void smc_destruct(struct sock *sk)
205{
206 if (sk->sk_state != SMC_CLOSED)
207 return;
208 if (!sock_flag(sk, SOCK_DEAD))
209 return;
210
211 sk_refcnt_debug_dec(sk);
212}
213
Karsten Graulaaa4d332018-03-16 15:06:41 +0100214static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
215 int protocol)
Ursula Braunac713872017-01-09 16:55:13 +0100216{
217 struct smc_sock *smc;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100218 struct proto *prot;
Ursula Braunac713872017-01-09 16:55:13 +0100219 struct sock *sk;
220
Karsten Graulaaa4d332018-03-16 15:06:41 +0100221 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
222 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
Ursula Braunac713872017-01-09 16:55:13 +0100223 if (!sk)
224 return NULL;
225
226 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
227 sk->sk_state = SMC_INIT;
228 sk->sk_destruct = smc_destruct;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100229 sk->sk_protocol = protocol;
Ursula Braunac713872017-01-09 16:55:13 +0100230 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100231 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200232 INIT_WORK(&smc->connect_work, smc_connect_work);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700233 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
Ursula Brauna046d572017-01-09 16:55:16 +0100234 INIT_LIST_HEAD(&smc->accept_q);
235 spin_lock_init(&smc->accept_q_lock);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700236 spin_lock_init(&smc->conn.send_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100237 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100238 sk_refcnt_debug_inc(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800239 mutex_init(&smc->clcsock_release_lock);
Ursula Braunac713872017-01-09 16:55:13 +0100240
241 return sk;
242}
243
244static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
245 int addr_len)
246{
247 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
248 struct sock *sk = sock->sk;
249 struct smc_sock *smc;
250 int rc;
251
252 smc = smc_sk(sk);
253
254 /* replicate tests from inet_bind(), to be safe wrt. future changes */
255 rc = -EINVAL;
256 if (addr_len < sizeof(struct sockaddr_in))
257 goto out;
258
259 rc = -EAFNOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100260 if (addr->sin_family != AF_INET &&
261 addr->sin_family != AF_INET6 &&
262 addr->sin_family != AF_UNSPEC)
263 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100264 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
Karsten Graulaaa4d332018-03-16 15:06:41 +0100265 if (addr->sin_family == AF_UNSPEC &&
266 addr->sin_addr.s_addr != htonl(INADDR_ANY))
Ursula Braunac713872017-01-09 16:55:13 +0100267 goto out;
268
269 lock_sock(sk);
270
271 /* Check if socket is already active */
272 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +0200273 if (sk->sk_state != SMC_INIT || smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +0100274 goto out_rel;
275
276 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
277 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
278
279out_rel:
280 release_sock(sk);
281out:
282 return rc;
283}
284
285static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
286 unsigned long mask)
287{
288 /* options we don't get control via setsockopt for */
289 nsk->sk_type = osk->sk_type;
290 nsk->sk_sndbuf = osk->sk_sndbuf;
291 nsk->sk_rcvbuf = osk->sk_rcvbuf;
292 nsk->sk_sndtimeo = osk->sk_sndtimeo;
293 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
294 nsk->sk_mark = osk->sk_mark;
295 nsk->sk_priority = osk->sk_priority;
296 nsk->sk_rcvlowat = osk->sk_rcvlowat;
297 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
298 nsk->sk_err = osk->sk_err;
299
300 nsk->sk_flags &= ~mask;
301 nsk->sk_flags |= osk->sk_flags & mask;
302}
303
304#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
305 (1UL << SOCK_KEEPOPEN) | \
306 (1UL << SOCK_LINGER) | \
307 (1UL << SOCK_BROADCAST) | \
308 (1UL << SOCK_TIMESTAMP) | \
309 (1UL << SOCK_DBG) | \
310 (1UL << SOCK_RCVTSTAMP) | \
311 (1UL << SOCK_RCVTSTAMPNS) | \
312 (1UL << SOCK_LOCALROUTE) | \
313 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
314 (1UL << SOCK_RXQ_OVFL) | \
315 (1UL << SOCK_WIFI_STATUS) | \
316 (1UL << SOCK_NOFCS) | \
Deepa Dinamani97184752019-02-02 07:34:51 -0800317 (1UL << SOCK_FILTER_LOCKED) | \
318 (1UL << SOCK_TSTAMP_NEW))
Ursula Braunac713872017-01-09 16:55:13 +0100319/* copy only relevant settings and flags of SOL_SOCKET level from smc to
320 * clc socket (since smc is not called for these options from net/core)
321 */
322static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
323{
324 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
325}
326
327#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
328 (1UL << SOCK_KEEPOPEN) | \
329 (1UL << SOCK_LINGER) | \
330 (1UL << SOCK_DBG))
331/* copy only settings and flags relevant for smc from clc to smc socket */
332static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
333{
334 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
335}
336
Karsten Graulc7674c02018-11-22 10:26:43 +0100337/* register a new rmb, send confirm_rkey msg to register with peer */
Karsten Graul44aa81c2018-05-15 17:04:55 +0200338static int smc_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc,
339 bool conf_rkey)
Karsten Graule63a5f82018-05-03 17:57:37 +0200340{
Karsten Graulc7674c02018-11-22 10:26:43 +0100341 if (!rmb_desc->wr_reg) {
342 /* register memory region for new rmb */
343 if (smc_wr_reg_send(link, rmb_desc->mr_rx[SMC_SINGLE_LINK])) {
344 rmb_desc->regerr = 1;
345 return -EFAULT;
346 }
347 rmb_desc->wr_reg = 1;
Karsten Graula6920d12018-05-03 17:57:38 +0200348 }
Karsten Graul44aa81c2018-05-15 17:04:55 +0200349 if (!conf_rkey)
350 return 0;
351 /* exchange confirm_rkey msg with peer */
352 if (smc_llc_do_confirm_rkey(link, rmb_desc)) {
353 rmb_desc->regerr = 1;
354 return -EFAULT;
355 }
Karsten Graule63a5f82018-05-03 17:57:37 +0200356 return 0;
357}
358
Stefan Raspl0f627122018-03-01 13:51:26 +0100359static int smc_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100360{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200361 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100362 struct smc_link_group *lgr = smc->conn.lgr;
363 struct smc_link *link;
364 int rest;
365 int rc;
366
367 link = &lgr->lnk[SMC_SINGLE_LINK];
368 /* receive CONFIRM LINK request from server over RoCE fabric */
369 rest = wait_for_completion_interruptible_timeout(
370 &link->llc_confirm,
371 SMC_LLC_WAIT_FIRST_TIME);
372 if (rest <= 0) {
373 struct smc_clc_msg_decline dclc;
374
375 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100376 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100377 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100378 }
379
Karsten Graul75d320d2018-03-01 13:51:31 +0100380 if (link->llc_confirm_rc)
381 return SMC_CLC_DECL_RMBE_EC;
382
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100383 rc = smc_ib_modify_qp_rts(link);
384 if (rc)
Karsten Graul603cc142018-07-25 16:35:32 +0200385 return SMC_CLC_DECL_ERR_RDYLNK;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100386
387 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200388
Karsten Graul44aa81c2018-05-15 17:04:55 +0200389 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200390 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200391
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100392 /* send CONFIRM LINK response over RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200393 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100394 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200395 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100396
Karsten Graul52bedf32018-03-01 13:51:32 +0100397 /* receive ADD LINK request from server over RoCE fabric */
398 rest = wait_for_completion_interruptible_timeout(&link->llc_add,
399 SMC_LLC_WAIT_TIME);
400 if (rest <= 0) {
401 struct smc_clc_msg_decline dclc;
402
403 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100404 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100405 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
Karsten Graul52bedf32018-03-01 13:51:32 +0100406 }
407
408 /* send add link reject message, only one link supported for now */
409 rc = smc_llc_send_add_link(link,
410 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +0200411 link->gid, SMC_LLC_RESP);
Karsten Graul52bedf32018-03-01 13:51:32 +0100412 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200413 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +0100414
Karsten Graul877ae5b2018-05-02 16:56:44 +0200415 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100416
Karsten Graul75d320d2018-03-01 13:51:31 +0100417 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100418}
419
Hans Wippel41349842018-06-28 19:05:11 +0200420static void smcr_conn_save_peer_info(struct smc_sock *smc,
421 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100422{
Hans Wippel95d8d2632018-05-18 09:34:13 +0200423 int bufsize = smc_uncompress_bufsize(clc->rmbe_size);
424
Hans Wippel92a138e2018-05-18 09:34:12 +0200425 smc->conn.peer_rmbe_idx = clc->rmbe_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100426 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200427 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100428 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200429 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100430}
431
Hans Wippel41349842018-06-28 19:05:11 +0200432static void smcd_conn_save_peer_info(struct smc_sock *smc,
433 struct smc_clc_msg_accept_confirm *clc)
434{
435 int bufsize = smc_uncompress_bufsize(clc->dmbe_size);
436
437 smc->conn.peer_rmbe_idx = clc->dmbe_idx;
438 smc->conn.peer_token = clc->token;
439 /* msg header takes up space in the buffer */
440 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
441 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
442 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
443}
444
445static void smc_conn_save_peer_info(struct smc_sock *smc,
446 struct smc_clc_msg_accept_confirm *clc)
447{
448 if (smc->conn.lgr->is_smcd)
449 smcd_conn_save_peer_info(smc, clc);
450 else
451 smcr_conn_save_peer_info(smc, clc);
452}
453
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100454static void smc_link_save_peer_info(struct smc_link *link,
455 struct smc_clc_msg_accept_confirm *clc)
456{
457 link->peer_qpn = ntoh24(clc->qpn);
458 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
459 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
460 link->peer_psn = ntoh24(clc->psn);
461 link->peer_mtu = clc->qp_mtu;
462}
463
Ursula Braun07603b22019-04-11 11:17:32 +0200464static void smc_switch_to_fallback(struct smc_sock *smc)
465{
466 smc->use_fallback = true;
467 if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
468 smc->clcsock->file = smc->sk.sk_socket->file;
469 smc->clcsock->file->private_data = smc->clcsock;
470 }
471}
472
Hans Wippel3b2dec22018-05-18 09:34:18 +0200473/* fall back during connect */
Karsten Graul603cc142018-07-25 16:35:32 +0200474static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200475{
Ursula Braun07603b22019-04-11 11:17:32 +0200476 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +0200477 smc->fallback_rsn = reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200478 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200479 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200480 if (smc->sk.sk_state == SMC_INIT)
481 smc->sk.sk_state = SMC_ACTIVE;
482 return 0;
483}
484
485/* decline and fall back during connect */
486static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
487{
488 int rc;
489
Ursula Braune1bbdd52018-07-05 16:15:30 +0200490 if (reason_code < 0) { /* error, fallback is not possible */
491 if (smc->sk.sk_state == SMC_INIT)
492 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200493 return reason_code;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200494 }
Karsten Graul603cc142018-07-25 16:35:32 +0200495 if (reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200496 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200497 if (rc < 0) {
498 if (smc->sk.sk_state == SMC_INIT)
499 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200500 return rc;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200501 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200502 }
Karsten Graul603cc142018-07-25 16:35:32 +0200503 return smc_connect_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200504}
505
506/* abort connecting */
507static int smc_connect_abort(struct smc_sock *smc, int reason_code,
508 int local_contact)
509{
510 if (local_contact == SMC_FIRST_CONTACT)
511 smc_lgr_forget(smc->conn.lgr);
Hans Wippel72a36a82019-02-07 15:56:18 +0100512 if (smc->conn.lgr->is_smcd)
513 /* there is only one lgr role for SMC-D; use server lock */
514 mutex_unlock(&smc_server_lgr_pending);
515 else
516 mutex_unlock(&smc_client_lgr_pending);
517
Hans Wippel3b2dec22018-05-18 09:34:18 +0200518 smc_conn_free(&smc->conn);
Ursula Braun50717a32019-04-12 12:57:23 +0200519 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200520 return reason_code;
521}
522
523/* check if there is a rdma device available for this connection. */
524/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200525static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200526{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200527 /* PNET table look up: search active ib_device and port
528 * within same PNETID that also contains the ethernet device
529 * used for the internal TCP socket
530 */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200531 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +0200532 if (!ini->ib_dev)
533 return SMC_CLC_DECL_NOSMCRDEV;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200534 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200535}
536
Hans Wippel41349842018-06-28 19:05:11 +0200537/* check if there is an ISM device available for this connection. */
538/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200539static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200540{
541 /* Find ISM device with same PNETID as connecting interface */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200542 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
543 if (!ini->ism_dev)
Karsten Graul9aa68d22019-04-12 12:57:29 +0200544 return SMC_CLC_DECL_NOSMCDDEV;
Hans Wippel41349842018-06-28 19:05:11 +0200545 return 0;
546}
547
548/* Check for VLAN ID and register it on ISM device just for CLC handshake */
549static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200550 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200551{
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200552 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev, ini->vlan_id))
Karsten Graul7a627252019-04-12 12:57:30 +0200553 return SMC_CLC_DECL_ISMVLANERR;
Hans Wippel41349842018-06-28 19:05:11 +0200554 return 0;
555}
556
557/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
558 * used, the VLAN ID will be registered again during the connection setup.
559 */
560static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200561 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200562{
563 if (!is_smcd)
564 return 0;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200565 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev, ini->vlan_id))
Hans Wippel41349842018-06-28 19:05:11 +0200566 return SMC_CLC_DECL_CNFERR;
567 return 0;
568}
569
Hans Wippel3b2dec22018-05-18 09:34:18 +0200570/* CLC handshake during connect */
Hans Wippelc758dfd2018-06-28 19:05:09 +0200571static int smc_connect_clc(struct smc_sock *smc, int smc_type,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200572 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200573 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200574{
575 int rc = 0;
576
577 /* do inband token exchange */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200578 rc = smc_clc_send_proposal(smc, smc_type, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200579 if (rc)
580 return rc;
581 /* receive SMC Accept CLC message */
Ursula Braun2b59f582018-11-22 10:26:39 +0100582 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT,
583 CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200584}
585
Ursula Brauna046d572017-01-09 16:55:16 +0100586/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200587static int smc_connect_rdma(struct smc_sock *smc,
588 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200589 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200590{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200591 struct smc_link *link;
592 int reason_code = 0;
593
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200594 ini->is_smcd = false;
595 ini->ib_lcl = &aclc->lcl;
596 ini->ib_clcqpn = ntoh24(aclc->qpn);
597 ini->srv_first_contact = aclc->hdr.flag;
598
Hans Wippel72a36a82019-02-07 15:56:18 +0100599 mutex_lock(&smc_client_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200600 reason_code = smc_conn_create(smc, ini);
601 if (reason_code) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100602 mutex_unlock(&smc_client_lgr_pending);
603 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200604 }
605 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
606
607 smc_conn_save_peer_info(smc, aclc);
608
609 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200610 if (smc_buf_create(smc, false))
Karsten Graul7a627252019-04-12 12:57:30 +0200611 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
612 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200613
Karsten Graul7a627252019-04-12 12:57:30 +0200614 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200615 smc_link_save_peer_info(link, aclc);
616
617 if (smc_rmb_rtoken_handling(&smc->conn, aclc))
Karsten Graul603cc142018-07-25 16:35:32 +0200618 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
Karsten Graul7a627252019-04-12 12:57:30 +0200619 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200620
621 smc_close_init(smc);
622 smc_rx_init(smc);
623
Karsten Graul7a627252019-04-12 12:57:30 +0200624 if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200625 if (smc_ib_ready_link(link))
Karsten Graul603cc142018-07-25 16:35:32 +0200626 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
Karsten Graul7a627252019-04-12 12:57:30 +0200627 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200628 } else {
Karsten Graulc7674c02018-11-22 10:26:43 +0100629 if (smc_reg_rmb(link, smc->conn.rmb_desc, true))
Karsten Graul603cc142018-07-25 16:35:32 +0200630 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
Karsten Graul7a627252019-04-12 12:57:30 +0200631 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200632 }
633 smc_rmb_sync_sg_for_device(&smc->conn);
634
635 reason_code = smc_clc_send_confirm(smc);
636 if (reason_code)
Karsten Graul7a627252019-04-12 12:57:30 +0200637 return smc_connect_abort(smc, reason_code,
638 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200639
640 smc_tx_init(smc);
641
Karsten Graul7a627252019-04-12 12:57:30 +0200642 if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200643 /* QP confirmation over RoCE fabric */
644 reason_code = smc_clnt_conf_first_link(smc);
645 if (reason_code)
646 return smc_connect_abort(smc, reason_code,
Karsten Graul7a627252019-04-12 12:57:30 +0200647 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200648 }
Hans Wippel72a36a82019-02-07 15:56:18 +0100649 mutex_unlock(&smc_client_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200650
651 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200652 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200653 if (smc->sk.sk_state == SMC_INIT)
654 smc->sk.sk_state = SMC_ACTIVE;
655
656 return 0;
657}
658
Hans Wippel41349842018-06-28 19:05:11 +0200659/* setup for ISM connection of client */
660static int smc_connect_ism(struct smc_sock *smc,
661 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200662 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200663{
Hans Wippel41349842018-06-28 19:05:11 +0200664 int rc = 0;
665
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200666 ini->is_smcd = true;
667 ini->ism_gid = aclc->gid;
668 ini->srv_first_contact = aclc->hdr.flag;
669
Hans Wippel72a36a82019-02-07 15:56:18 +0100670 /* there is only one lgr role for SMC-D; use server lock */
671 mutex_lock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200672 rc = smc_conn_create(smc, ini);
673 if (rc) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100674 mutex_unlock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200675 return rc;
Hans Wippel72a36a82019-02-07 15:56:18 +0100676 }
Hans Wippel41349842018-06-28 19:05:11 +0200677
678 /* Create send and receive buffers */
679 if (smc_buf_create(smc, true))
Karsten Graul7a627252019-04-12 12:57:30 +0200680 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
681 ini->cln_first_contact);
Hans Wippel41349842018-06-28 19:05:11 +0200682
683 smc_conn_save_peer_info(smc, aclc);
684 smc_close_init(smc);
685 smc_rx_init(smc);
686 smc_tx_init(smc);
687
688 rc = smc_clc_send_confirm(smc);
689 if (rc)
Karsten Graul7a627252019-04-12 12:57:30 +0200690 return smc_connect_abort(smc, rc, ini->cln_first_contact);
Hans Wippel72a36a82019-02-07 15:56:18 +0100691 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel41349842018-06-28 19:05:11 +0200692
693 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200694 smc->connect_nonblock = 0;
Hans Wippel41349842018-06-28 19:05:11 +0200695 if (smc->sk.sk_state == SMC_INIT)
696 smc->sk.sk_state = SMC_ACTIVE;
697
698 return 0;
699}
700
Hans Wippel3b2dec22018-05-18 09:34:18 +0200701/* perform steps before actually connecting */
702static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100703{
Hans Wippel41349842018-06-28 19:05:11 +0200704 bool ism_supported = false, rdma_supported = false;
Ursula Brauna046d572017-01-09 16:55:16 +0100705 struct smc_clc_msg_accept_confirm aclc;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200706 struct smc_init_info ini = {0};
Hans Wippel41349842018-06-28 19:05:11 +0200707 int smc_type;
Ursula Brauna046d572017-01-09 16:55:16 +0100708 int rc = 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100709
Ursula Braun51f1de72018-01-26 09:28:48 +0100710 sock_hold(&smc->sk); /* sock put in passive closing */
711
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200712 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +0200713 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200714
Hans Wippel3b2dec22018-05-18 09:34:18 +0200715 /* if peer has not signalled SMC-capability, fall back */
716 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +0200717 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200718
Ursula Brauna046d572017-01-09 16:55:16 +0100719 /* IPSec connections opt out of SMC-R optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200720 if (using_ipsec(smc))
721 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
Ursula Brauna046d572017-01-09 16:55:16 +0100722
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200723 /* get vlan id from IP device */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200724 if (smc_vlan_by_tcpsk(smc->clcsock, &ini))
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200725 return smc_connect_decline_fallback(smc,
726 SMC_CLC_DECL_GETVLANERR);
Hans Wippel41349842018-06-28 19:05:11 +0200727
728 /* check if there is an ism device available */
Karsten Graul228bae02019-04-12 12:57:28 +0200729 if (!smc_find_ism_device(smc, &ini) &&
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200730 !smc_connect_ism_vlan_setup(smc, &ini)) {
Hans Wippel41349842018-06-28 19:05:11 +0200731 /* ISM is supported for this connection */
732 ism_supported = true;
733 smc_type = SMC_TYPE_D;
734 }
735
736 /* check if there is a rdma device available */
Karsten Graul228bae02019-04-12 12:57:28 +0200737 if (!smc_find_rdma_device(smc, &ini)) {
Hans Wippel41349842018-06-28 19:05:11 +0200738 /* RDMA is supported for this connection */
739 rdma_supported = true;
740 if (ism_supported)
741 smc_type = SMC_TYPE_B; /* both */
742 else
743 smc_type = SMC_TYPE_R; /* only RDMA */
744 }
745
746 /* if neither ISM nor RDMA are supported, fallback */
747 if (!rdma_supported && !ism_supported)
Karsten Graul603cc142018-07-25 16:35:32 +0200748 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV);
Ursula Brauna046d572017-01-09 16:55:16 +0100749
Hans Wippel3b2dec22018-05-18 09:34:18 +0200750 /* perform CLC handshake */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200751 rc = smc_connect_clc(smc, smc_type, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200752 if (rc) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200753 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200754 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200755 }
Ursula Brauna046d572017-01-09 16:55:16 +0100756
Hans Wippel41349842018-06-28 19:05:11 +0200757 /* depending on previous steps, connect using rdma or ism */
758 if (rdma_supported && aclc.hdr.path == SMC_TYPE_R)
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200759 rc = smc_connect_rdma(smc, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200760 else if (ism_supported && aclc.hdr.path == SMC_TYPE_D)
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200761 rc = smc_connect_ism(smc, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200762 else
Karsten Graul603cc142018-07-25 16:35:32 +0200763 rc = SMC_CLC_DECL_MODEUNSUPP;
Hans Wippel41349842018-06-28 19:05:11 +0200764 if (rc) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200765 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200766 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200767 }
Ursula Brauna046d572017-01-09 16:55:16 +0100768
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 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100771}
772
Ursula Braun24ac3a02018-06-27 17:59:50 +0200773static void smc_connect_work(struct work_struct *work)
774{
775 struct smc_sock *smc = container_of(work, struct smc_sock,
776 connect_work);
Ursula Braun50717a32019-04-12 12:57:23 +0200777 long timeo = smc->sk.sk_sndtimeo;
778 int rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200779
Ursula Braun50717a32019-04-12 12:57:23 +0200780 if (!timeo)
781 timeo = MAX_SCHEDULE_TIMEOUT;
782 lock_sock(smc->clcsock->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200783 if (smc->clcsock->sk->sk_err) {
784 smc->sk.sk_err = smc->clcsock->sk->sk_err;
Ursula Braun50717a32019-04-12 12:57:23 +0200785 } else if ((1 << smc->clcsock->sk->sk_state) &
786 (TCPF_SYN_SENT | TCP_SYN_RECV)) {
787 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
788 if ((rc == -EPIPE) &&
789 ((1 << smc->clcsock->sk->sk_state) &
790 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
791 rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200792 }
Ursula Braun50717a32019-04-12 12:57:23 +0200793 release_sock(smc->clcsock->sk);
794 lock_sock(&smc->sk);
795 if (rc != 0 || smc->sk.sk_err) {
796 smc->sk.sk_state = SMC_CLOSED;
797 if (rc == -EPIPE || rc == -EAGAIN)
798 smc->sk.sk_err = EPIPE;
799 else if (signal_pending(current))
800 smc->sk.sk_err = -sock_intr_errno(timeo);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200801 goto out;
802 }
803
804 rc = __smc_connect(smc);
805 if (rc < 0)
806 smc->sk.sk_err = -rc;
807
808out:
Ursula Braun07603b22019-04-11 11:17:32 +0200809 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
810 if (smc->sk.sk_err) {
811 smc->sk.sk_state_change(&smc->sk);
812 } else { /* allow polling before and after fallback decision */
813 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
814 smc->sk.sk_write_space(&smc->sk);
815 }
816 }
Ursula Braun24ac3a02018-06-27 17:59:50 +0200817 release_sock(&smc->sk);
818}
819
Ursula Braunac713872017-01-09 16:55:13 +0100820static int smc_connect(struct socket *sock, struct sockaddr *addr,
821 int alen, int flags)
822{
823 struct sock *sk = sock->sk;
824 struct smc_sock *smc;
825 int rc = -EINVAL;
826
827 smc = smc_sk(sk);
828
829 /* separate smc parameter checking to be safe */
830 if (alen < sizeof(addr->sa_family))
831 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100832 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100833 goto out_err;
834
835 lock_sock(sk);
836 switch (sk->sk_state) {
837 default:
838 goto out;
839 case SMC_ACTIVE:
840 rc = -EISCONN;
841 goto out;
842 case SMC_INIT:
843 rc = 0;
844 break;
845 }
846
847 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200848 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun50717a32019-04-12 12:57:23 +0200849 if (smc->connect_nonblock) {
850 rc = -EALREADY;
851 goto out;
852 }
853 rc = kernel_connect(smc->clcsock, addr, alen, flags);
854 if (rc && rc != -EINPROGRESS)
855 goto out;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200856 if (flags & O_NONBLOCK) {
Ursula Braun50717a32019-04-12 12:57:23 +0200857 if (schedule_work(&smc->connect_work))
858 smc->connect_nonblock = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200859 rc = -EINPROGRESS;
860 } else {
Ursula Braun24ac3a02018-06-27 17:59:50 +0200861 rc = __smc_connect(smc);
862 if (rc < 0)
863 goto out;
864 else
865 rc = 0; /* success cases including fallback */
866 }
Ursula Braunac713872017-01-09 16:55:13 +0100867
868out:
869 release_sock(sk);
870out_err:
871 return rc;
872}
873
874static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
875{
Ursula Braun3163c502018-01-24 10:28:12 +0100876 struct socket *new_clcsock = NULL;
877 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100878 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -0800879 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +0100880
Ursula Braun3163c502018-01-24 10:28:12 +0100881 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100882 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100883 if (!new_sk) {
884 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100885 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100886 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100887 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100888 goto out;
889 }
890 *new_smc = smc_sk(new_sk);
891
Myungho Jung78abe3d2018-12-18 09:02:25 -0800892 mutex_lock(&lsmc->clcsock_release_lock);
893 if (lsmc->clcsock)
894 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
895 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +0100896 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100897 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100898 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100899 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +0200900 new_sk->sk_prot->unhash(new_sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100901 if (new_clcsock)
902 sock_release(new_clcsock);
903 new_sk->sk_state = SMC_CLOSED;
904 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun51f1de72018-01-26 09:28:48 +0100905 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100906 *new_smc = NULL;
907 goto out;
908 }
909
910 (*new_smc)->clcsock = new_clcsock;
911out:
912 return rc;
913}
914
Ursula Brauna046d572017-01-09 16:55:16 +0100915/* add a just created sock to the accept queue of the listen sock as
916 * candidate for a following socket accept call from user space
917 */
918static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
919{
920 struct smc_sock *par = smc_sk(parent);
921
Ursula Braun51f1de72018-01-26 09:28:48 +0100922 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100923 spin_lock(&par->accept_q_lock);
924 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
925 spin_unlock(&par->accept_q_lock);
926 sk_acceptq_added(parent);
927}
928
929/* remove a socket from the accept queue of its parental listening socket */
930static void smc_accept_unlink(struct sock *sk)
931{
932 struct smc_sock *par = smc_sk(sk)->listen_smc;
933
934 spin_lock(&par->accept_q_lock);
935 list_del_init(&smc_sk(sk)->accept_q);
936 spin_unlock(&par->accept_q_lock);
937 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100938 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100939}
940
941/* remove a sock from the accept queue to bind it to a new socket created
942 * for a socket accept call from user space
943 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100944struct sock *smc_accept_dequeue(struct sock *parent,
945 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100946{
947 struct smc_sock *isk, *n;
948 struct sock *new_sk;
949
950 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
951 new_sk = (struct sock *)isk;
952
953 smc_accept_unlink(new_sk);
954 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +0200955 new_sk->sk_prot->unhash(new_sk);
Ursula Braun127f4972018-01-26 09:28:49 +0100956 if (isk->clcsock) {
957 sock_release(isk->clcsock);
958 isk->clcsock = NULL;
959 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100960 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +0100961 continue;
962 }
Ursula Braun07603b22019-04-11 11:17:32 +0200963 if (new_sock) {
Ursula Brauna046d572017-01-09 16:55:16 +0100964 sock_graft(new_sk, new_sock);
Ursula Braun07603b22019-04-11 11:17:32 +0200965 if (isk->use_fallback) {
966 smc_sk(new_sk)->clcsock->file = new_sock->file;
967 isk->clcsock->file->private_data = isk->clcsock;
968 }
969 }
Ursula Brauna046d572017-01-09 16:55:16 +0100970 return new_sk;
971 }
972 return NULL;
973}
974
975/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +0100976void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +0100977{
978 struct smc_sock *smc = smc_sk(sk);
979
Ursula Braunb38d7322017-01-09 16:55:25 +0100980 lock_sock(sk);
981 if (!sk->sk_lingertime)
982 /* wait for peer closing */
983 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun39f41f32019-06-27 15:04:52 +0200984 __smc_release(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +0100985 release_sock(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100986 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +0100987}
988
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100989static int smc_serv_conf_first_link(struct smc_sock *smc)
990{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200991 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100992 struct smc_link_group *lgr = smc->conn.lgr;
993 struct smc_link *link;
994 int rest;
995 int rc;
996
997 link = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun652a1e42017-07-28 13:56:17 +0200998
Karsten Graul44aa81c2018-05-15 17:04:55 +0200999 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +02001000 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +02001001
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001002 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +02001003 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001004 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001005 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001006
1007 /* receive CONFIRM LINK response from client over the RoCE fabric */
1008 rest = wait_for_completion_interruptible_timeout(
1009 &link->llc_confirm_resp,
1010 SMC_LLC_WAIT_FIRST_TIME);
1011 if (rest <= 0) {
1012 struct smc_clc_msg_decline dclc;
1013
1014 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001015 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001016 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001017 }
1018
Karsten Graul75d320d2018-03-01 13:51:31 +01001019 if (link->llc_confirm_resp_rc)
1020 return SMC_CLC_DECL_RMBE_EC;
1021
Karsten Graul52bedf32018-03-01 13:51:32 +01001022 /* send ADD LINK request to client over the RoCE fabric */
1023 rc = smc_llc_send_add_link(link,
1024 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +02001025 link->gid, SMC_LLC_REQ);
Karsten Graul52bedf32018-03-01 13:51:32 +01001026 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001027 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +01001028
1029 /* receive ADD LINK response from client over the RoCE fabric */
1030 rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
1031 SMC_LLC_WAIT_TIME);
1032 if (rest <= 0) {
1033 struct smc_clc_msg_decline dclc;
1034
1035 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001036 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001037 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
Karsten Graul52bedf32018-03-01 13:51:32 +01001038 }
1039
Karsten Graul877ae5b2018-05-02 16:56:44 +02001040 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +01001041
Karsten Graul75d320d2018-03-01 13:51:31 +01001042 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001043}
1044
Hans Wippel3b2dec22018-05-18 09:34:18 +02001045/* listen worker: finish */
1046static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001047{
Ursula Brauna046d572017-01-09 16:55:16 +01001048 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001049 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001050
Ursula Brauna046d572017-01-09 16:55:16 +01001051 if (lsmc->sk.sk_state == SMC_LISTEN) {
Karsten Graulfd577702019-04-11 11:17:30 +02001052 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001053 smc_accept_enqueue(&lsmc->sk, newsmcsk);
Karsten Graulfd577702019-04-11 11:17:30 +02001054 release_sock(&lsmc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001055 } else { /* no longer listening */
1056 smc_close_non_accepted(newsmcsk);
1057 }
Ursula Brauna046d572017-01-09 16:55:16 +01001058
1059 /* Wake up accept */
1060 lsmc->sk.sk_data_ready(&lsmc->sk);
1061 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001062}
Ursula Brauna046d572017-01-09 16:55:16 +01001063
Hans Wippel3b2dec22018-05-18 09:34:18 +02001064/* listen worker: finish in state connected */
1065static void smc_listen_out_connected(struct smc_sock *new_smc)
1066{
1067 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001068
Hans Wippel3b2dec22018-05-18 09:34:18 +02001069 sk_refcnt_debug_inc(newsmcsk);
1070 if (newsmcsk->sk_state == SMC_INIT)
1071 newsmcsk->sk_state = SMC_ACTIVE;
1072
1073 smc_listen_out(new_smc);
1074}
1075
1076/* listen worker: finish in error state */
1077static void smc_listen_out_err(struct smc_sock *new_smc)
1078{
1079 struct sock *newsmcsk = &new_smc->sk;
1080
Ursula Braun51f1de72018-01-26 09:28:48 +01001081 if (newsmcsk->sk_state == SMC_INIT)
1082 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001083 newsmcsk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001084 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001085
1086 smc_listen_out(new_smc);
1087}
1088
1089/* listen worker: decline and fall back if possible */
1090static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1091 int local_contact)
1092{
1093 /* RDMA setup failed, switch back to TCP */
1094 if (local_contact == SMC_FIRST_CONTACT)
1095 smc_lgr_forget(new_smc->conn.lgr);
1096 if (reason_code < 0) { /* error, no fallback possible */
1097 smc_listen_out_err(new_smc);
1098 return;
1099 }
1100 smc_conn_free(&new_smc->conn);
Ursula Braun07603b22019-04-11 11:17:32 +02001101 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001102 new_smc->fallback_rsn = reason_code;
1103 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001104 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1105 smc_listen_out_err(new_smc);
1106 return;
1107 }
1108 }
1109 smc_listen_out_connected(new_smc);
1110}
1111
1112/* listen worker: check prefixes */
Karsten Graul59886692019-04-12 12:57:25 +02001113static int smc_listen_prfx_check(struct smc_sock *new_smc,
Hans Wippel3b2dec22018-05-18 09:34:18 +02001114 struct smc_clc_msg_proposal *pclc)
1115{
1116 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1117 struct socket *newclcsock = new_smc->clcsock;
1118
1119 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1120 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
Karsten Graul59886692019-04-12 12:57:25 +02001121 return SMC_CLC_DECL_DIFFPREFIX;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001122
1123 return 0;
1124}
1125
1126/* listen worker: initialize connection and buffers */
1127static int smc_listen_rdma_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001128 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001129{
Karsten Graul7a627252019-04-12 12:57:30 +02001130 int rc;
1131
Hans Wippel3b2dec22018-05-18 09:34:18 +02001132 /* allocate connection / link group */
Karsten Graul7a627252019-04-12 12:57:30 +02001133 rc = smc_conn_create(new_smc, ini);
1134 if (rc)
1135 return rc;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001136
1137 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001138 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001139 return SMC_CLC_DECL_MEM;
1140
1141 return 0;
1142}
1143
Hans Wippel41349842018-06-28 19:05:11 +02001144/* listen worker: initialize connection and buffers for SMC-D */
1145static int smc_listen_ism_init(struct smc_sock *new_smc,
1146 struct smc_clc_msg_proposal *pclc,
Karsten Graul7a627252019-04-12 12:57:30 +02001147 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +02001148{
1149 struct smc_clc_msg_smcd *pclc_smcd;
Karsten Graul7a627252019-04-12 12:57:30 +02001150 int rc;
Hans Wippel41349842018-06-28 19:05:11 +02001151
1152 pclc_smcd = smc_get_clc_msg_smcd(pclc);
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001153 ini->ism_gid = pclc_smcd->gid;
Karsten Graul7a627252019-04-12 12:57:30 +02001154 rc = smc_conn_create(new_smc, ini);
1155 if (rc)
1156 return rc;
Hans Wippel41349842018-06-28 19:05:11 +02001157
1158 /* Check if peer can be reached via ISM device */
1159 if (smc_ism_cantalk(new_smc->conn.lgr->peer_gid,
1160 new_smc->conn.lgr->vlan_id,
1161 new_smc->conn.lgr->smcd)) {
Karsten Graul7a627252019-04-12 12:57:30 +02001162 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Hans Wippel41349842018-06-28 19:05:11 +02001163 smc_lgr_forget(new_smc->conn.lgr);
1164 smc_conn_free(&new_smc->conn);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001165 return SMC_CLC_DECL_SMCDNOTALK;
Hans Wippel41349842018-06-28 19:05:11 +02001166 }
1167
1168 /* Create send and receive buffers */
1169 if (smc_buf_create(new_smc, true)) {
Karsten Graul7a627252019-04-12 12:57:30 +02001170 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Hans Wippel41349842018-06-28 19:05:11 +02001171 smc_lgr_forget(new_smc->conn.lgr);
1172 smc_conn_free(&new_smc->conn);
1173 return SMC_CLC_DECL_MEM;
1174 }
1175
1176 return 0;
1177}
1178
Hans Wippel3b2dec22018-05-18 09:34:18 +02001179/* listen worker: register buffers */
1180static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
1181{
1182 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1183
1184 if (local_contact != SMC_FIRST_CONTACT) {
Karsten Graulc7674c02018-11-22 10:26:43 +01001185 if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true))
1186 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001187 }
1188 smc_rmb_sync_sg_for_device(&new_smc->conn);
1189
1190 return 0;
1191}
1192
1193/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001194static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1195 struct smc_clc_msg_accept_confirm *cclc,
1196 int local_contact)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001197{
1198 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1199 int reason_code = 0;
1200
1201 if (local_contact == SMC_FIRST_CONTACT)
1202 smc_link_save_peer_info(link, cclc);
1203
1204 if (smc_rmb_rtoken_handling(&new_smc->conn, cclc)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001205 reason_code = SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001206 goto decline;
1207 }
1208
1209 if (local_contact == SMC_FIRST_CONTACT) {
1210 if (smc_ib_ready_link(link)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001211 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001212 goto decline;
1213 }
1214 /* QP confirmation over RoCE fabric */
1215 reason_code = smc_serv_conf_first_link(new_smc);
1216 if (reason_code)
1217 goto decline;
1218 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001219 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001220
1221decline:
Hans Wippel3b2dec22018-05-18 09:34:18 +02001222 smc_listen_decline(new_smc, reason_code, local_contact);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001223 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001224}
1225
1226/* setup for RDMA connection of server */
1227static void smc_listen_work(struct work_struct *work)
1228{
1229 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1230 smc_listen_work);
1231 struct socket *newclcsock = new_smc->clcsock;
1232 struct smc_clc_msg_accept_confirm cclc;
1233 struct smc_clc_msg_proposal *pclc;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001234 struct smc_init_info ini = {0};
Hans Wippel41349842018-06-28 19:05:11 +02001235 bool ism_supported = false;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001236 u8 buf[SMC_CLC_MAX_LEN];
Hans Wippel3b2dec22018-05-18 09:34:18 +02001237 int rc = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001238
Karsten Graulfd577702019-04-11 11:17:30 +02001239 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1240 return smc_listen_out_err(new_smc);
1241
Hans Wippel3b2dec22018-05-18 09:34:18 +02001242 if (new_smc->use_fallback) {
1243 smc_listen_out_connected(new_smc);
1244 return;
1245 }
1246
1247 /* check if peer is smc capable */
1248 if (!tcp_sk(newclcsock->sk)->syn_smc) {
Ursula Braun07603b22019-04-11 11:17:32 +02001249 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001250 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001251 smc_listen_out_connected(new_smc);
1252 return;
1253 }
1254
1255 /* do inband token exchange -
1256 * wait for and receive SMC Proposal CLC message
1257 */
1258 pclc = (struct smc_clc_msg_proposal *)&buf;
Karsten Graul228bae02019-04-12 12:57:28 +02001259 rc = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
1260 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001261 if (rc)
1262 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001263
1264 /* IPSec connections opt out of SMC-R optimizations */
1265 if (using_ipsec(new_smc)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001266 rc = SMC_CLC_DECL_IPSEC;
1267 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001268 }
1269
Karsten Graul59886692019-04-12 12:57:25 +02001270 /* check for matching IP prefix and subnet length */
1271 rc = smc_listen_prfx_check(new_smc, pclc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001272 if (rc)
1273 goto out_decl;
Karsten Graul59886692019-04-12 12:57:25 +02001274
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001275 /* get vlan id from IP device */
1276 if (smc_vlan_by_tcpsk(new_smc->clcsock, &ini)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001277 rc = SMC_CLC_DECL_GETVLANERR;
1278 goto out_decl;
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001279 }
1280
Hans Wippel72a36a82019-02-07 15:56:18 +01001281 mutex_lock(&smc_server_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001282 smc_close_init(new_smc);
1283 smc_rx_init(new_smc);
1284 smc_tx_init(new_smc);
1285
Hans Wippel41349842018-06-28 19:05:11 +02001286 /* check if ISM is available */
Karsten Graul9aa68d22019-04-12 12:57:29 +02001287 if (pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) {
1288 ini.is_smcd = true; /* prepare ISM check */
1289 rc = smc_find_ism_device(new_smc, &ini);
1290 if (!rc)
Karsten Graul7a627252019-04-12 12:57:30 +02001291 rc = smc_listen_ism_init(new_smc, pclc, &ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001292 if (!rc)
1293 ism_supported = true;
1294 else if (pclc->hdr.path == SMC_TYPE_D)
1295 goto out_unlock; /* skip RDMA and decline */
1296 }
1297
1298 /* check if RDMA is available */
1299 if (!ism_supported) { /* SMC_TYPE_R or SMC_TYPE_B */
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001300 /* prepare RDMA check */
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001301 ini.is_smcd = false;
Ursula Braunca5f8d22019-10-23 15:44:06 +02001302 ini.ism_dev = NULL;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001303 ini.ib_lcl = &pclc->lcl;
Karsten Graul9aa68d22019-04-12 12:57:29 +02001304 rc = smc_find_rdma_device(new_smc, &ini);
1305 if (rc) {
1306 /* no RDMA device found */
1307 if (pclc->hdr.path == SMC_TYPE_B)
1308 /* neither ISM nor RDMA device found */
1309 rc = SMC_CLC_DECL_NOSMCDEV;
1310 goto out_unlock;
1311 }
Karsten Graul7a627252019-04-12 12:57:30 +02001312 rc = smc_listen_rdma_init(new_smc, &ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001313 if (rc)
1314 goto out_unlock;
Karsten Graul7a627252019-04-12 12:57:30 +02001315 rc = smc_listen_rdma_reg(new_smc, ini.cln_first_contact);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001316 if (rc)
1317 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001318 }
1319
1320 /* send SMC Accept CLC message */
Karsten Graul7a627252019-04-12 12:57:30 +02001321 rc = smc_clc_send_accept(new_smc, ini.cln_first_contact);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001322 if (rc)
1323 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001324
Hans Wippel62c71392019-02-07 15:56:17 +01001325 /* SMC-D does not need this lock any more */
1326 if (ism_supported)
Hans Wippel72a36a82019-02-07 15:56:18 +01001327 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001328
Hans Wippel3b2dec22018-05-18 09:34:18 +02001329 /* receive SMC Confirm CLC message */
Karsten Graul228bae02019-04-12 12:57:28 +02001330 rc = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
1331 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
1332 if (rc) {
Hans Wippel62c71392019-02-07 15:56:17 +01001333 if (!ism_supported)
Karsten Graul9aa68d22019-04-12 12:57:29 +02001334 goto out_unlock;
1335 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001336 }
1337
1338 /* finish worker */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001339 if (!ism_supported) {
Karsten Graul7a627252019-04-12 12:57:30 +02001340 rc = smc_listen_rdma_finish(new_smc, &cclc,
1341 ini.cln_first_contact);
Hans Wippel72a36a82019-02-07 15:56:18 +01001342 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001343 if (rc)
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001344 return;
1345 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001346 smc_conn_save_peer_info(new_smc, &cclc);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001347 smc_listen_out_connected(new_smc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001348 return;
1349
1350out_unlock:
1351 mutex_unlock(&smc_server_lgr_pending);
1352out_decl:
Karsten Graul7a627252019-04-12 12:57:30 +02001353 smc_listen_decline(new_smc, rc, ini.cln_first_contact);
Ursula Brauna046d572017-01-09 16:55:16 +01001354}
1355
1356static void smc_tcp_listen_work(struct work_struct *work)
1357{
1358 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1359 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001360 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001361 struct smc_sock *new_smc;
1362 int rc = 0;
1363
Ursula Braun3163c502018-01-24 10:28:12 +01001364 lock_sock(lsk);
1365 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001366 rc = smc_clcsock_accept(lsmc, &new_smc);
1367 if (rc)
1368 goto out;
1369 if (!new_smc)
1370 continue;
1371
1372 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001373 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001374 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001375 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001376 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1377 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001378 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1379 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001380 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1381 if (!schedule_work(&new_smc->smc_listen_work))
1382 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001383 }
1384
1385out:
Ursula Braun3163c502018-01-24 10:28:12 +01001386 release_sock(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001387 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +01001388}
1389
Ursula Braunac713872017-01-09 16:55:13 +01001390static int smc_listen(struct socket *sock, int backlog)
1391{
1392 struct sock *sk = sock->sk;
1393 struct smc_sock *smc;
1394 int rc;
1395
1396 smc = smc_sk(sk);
1397 lock_sock(sk);
1398
1399 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +02001400 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
1401 smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +01001402 goto out;
1403
1404 rc = 0;
1405 if (sk->sk_state == SMC_LISTEN) {
1406 sk->sk_max_ack_backlog = backlog;
1407 goto out;
1408 }
1409 /* some socket options are handled in core, so we could not apply
1410 * them to the clc socket -- copy smc socket options to clc socket
1411 */
1412 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001413 if (!smc->use_fallback)
1414 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001415
1416 rc = kernel_listen(smc->clcsock, backlog);
1417 if (rc)
1418 goto out;
1419 sk->sk_max_ack_backlog = backlog;
1420 sk->sk_ack_backlog = 0;
1421 sk->sk_state = SMC_LISTEN;
Ursula Braun51f1de72018-01-26 09:28:48 +01001422 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1423 if (!schedule_work(&smc->tcp_listen_work))
1424 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001425
1426out:
1427 release_sock(sk);
1428 return rc;
1429}
1430
1431static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001432 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001433{
Ursula Brauna046d572017-01-09 16:55:16 +01001434 struct sock *sk = sock->sk, *nsk;
1435 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001436 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001437 long timeo;
1438 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001439
1440 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001441 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001442 lock_sock(sk);
1443
1444 if (lsmc->sk.sk_state != SMC_LISTEN) {
1445 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001446 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001447 goto out;
1448 }
1449
Ursula Brauna046d572017-01-09 16:55:16 +01001450 /* Wait for an incoming connection */
1451 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1452 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1453 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1454 set_current_state(TASK_INTERRUPTIBLE);
1455 if (!timeo) {
1456 rc = -EAGAIN;
1457 break;
1458 }
1459 release_sock(sk);
1460 timeo = schedule_timeout(timeo);
1461 /* wakeup by sk_data_ready in smc_listen_work() */
1462 sched_annotate_sleep();
1463 lock_sock(sk);
1464 if (signal_pending(current)) {
1465 rc = sock_intr_errno(timeo);
1466 break;
1467 }
1468 }
1469 set_current_state(TASK_RUNNING);
1470 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001471
Ursula Brauna046d572017-01-09 16:55:16 +01001472 if (!rc)
1473 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001474 release_sock(sk);
1475 if (rc)
1476 goto out;
1477
1478 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1479 /* wait till data arrives on the socket */
1480 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1481 MSEC_PER_SEC);
1482 if (smc_sk(nsk)->use_fallback) {
1483 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1484
1485 lock_sock(clcsk);
1486 if (skb_queue_empty(&clcsk->sk_receive_queue))
1487 sk_wait_data(clcsk, &timeo, NULL);
1488 release_sock(clcsk);
1489 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1490 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001491 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001492 release_sock(nsk);
1493 }
1494 }
Ursula Braunac713872017-01-09 16:55:13 +01001495
1496out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001497 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001498 return rc;
1499}
1500
1501static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001502 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001503{
1504 struct smc_sock *smc;
1505
Ursula Braunb38d7322017-01-09 16:55:25 +01001506 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1507 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001508 return -ENOTCONN;
1509
1510 smc = smc_sk(sock->sk);
1511
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001512 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001513}
1514
1515static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1516{
1517 struct sock *sk = sock->sk;
1518 struct smc_sock *smc;
1519 int rc = -EPIPE;
1520
1521 smc = smc_sk(sk);
1522 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001523 if ((sk->sk_state != SMC_ACTIVE) &&
1524 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1525 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001526 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001527
1528 if (msg->msg_flags & MSG_FASTOPEN) {
Ursula Brauncd206362019-08-02 10:47:50 +02001529 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001530 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001531 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001532 } else {
1533 rc = -EINVAL;
1534 goto out;
1535 }
1536 }
1537
Ursula Braunac713872017-01-09 16:55:13 +01001538 if (smc->use_fallback)
1539 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1540 else
Ursula Braune6727f32017-01-09 16:55:23 +01001541 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001542out:
1543 release_sock(sk);
1544 return rc;
1545}
1546
1547static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1548 int flags)
1549{
1550 struct sock *sk = sock->sk;
1551 struct smc_sock *smc;
1552 int rc = -ENOTCONN;
1553
1554 smc = smc_sk(sk);
1555 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001556 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1557 /* socket was connected before, no more data to read */
1558 rc = 0;
1559 goto out;
1560 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001561 if ((sk->sk_state == SMC_INIT) ||
1562 (sk->sk_state == SMC_LISTEN) ||
1563 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001564 goto out;
1565
Ursula Braunb38d7322017-01-09 16:55:25 +01001566 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1567 rc = 0;
1568 goto out;
1569 }
1570
Stefan Raspl9014db22018-05-03 18:12:39 +02001571 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001572 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001573 } else {
1574 msg->msg_namelen = 0;
1575 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1576 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001577
Ursula Braunac713872017-01-09 16:55:13 +01001578out:
1579 release_sock(sk);
1580 return rc;
1581}
1582
Al Viroade994f2017-07-03 00:01:49 -04001583static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001584{
Ursula Braun8dce2782018-01-26 09:28:47 +01001585 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001586 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001587
Ursula Braun8dce2782018-01-26 09:28:47 +01001588 spin_lock(&isk->accept_q_lock);
1589 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001590 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001591 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001592
Ursula Braun8dce2782018-01-26 09:28:47 +01001593 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001594}
1595
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001596static __poll_t smc_poll(struct file *file, struct socket *sock,
1597 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01001598{
1599 struct sock *sk = sock->sk;
Ursula Braunac713872017-01-09 16:55:13 +01001600 struct smc_sock *smc;
Ursula Braun50717a32019-04-12 12:57:23 +02001601 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001602
Ursula Braun8dce2782018-01-26 09:28:47 +01001603 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001604 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001605
Ursula Braunac713872017-01-09 16:55:13 +01001606 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02001607 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01001608 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001609 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02001610 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braunac713872017-01-09 16:55:13 +01001611 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07001612 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02001613 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01001614 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001615 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001616 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1617 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001618 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001619 if (sk->sk_state == SMC_LISTEN) {
1620 /* woken up by sk_data_ready in smc_listen_work() */
Ursula Braun50717a32019-04-12 12:57:23 +02001621 mask |= smc_accept_poll(sk);
1622 } else if (smc->use_fallback) { /* as result of connect_work()*/
1623 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
1624 wait);
1625 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun8dce2782018-01-26 09:28:47 +01001626 } else {
Ursula Braun50717a32019-04-12 12:57:23 +02001627 if ((sk->sk_state != SMC_INIT &&
1628 atomic_read(&smc->conn.sndbuf_space)) ||
Ursula Braun8dce2782018-01-26 09:28:47 +01001629 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001630 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001631 } else {
1632 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1633 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1634 }
1635 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001636 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001637 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001638 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001639 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001640 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02001641 if (smc->conn.urg_state == SMC_URG_VALID)
1642 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01001643 }
Ursula Braunac713872017-01-09 16:55:13 +01001644 }
1645
1646 return mask;
1647}
1648
1649static int smc_shutdown(struct socket *sock, int how)
1650{
1651 struct sock *sk = sock->sk;
1652 struct smc_sock *smc;
1653 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001654 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001655
1656 smc = smc_sk(sk);
1657
1658 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001659 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001660
1661 lock_sock(sk);
1662
1663 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02001664 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01001665 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1666 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1667 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1668 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1669 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001670 goto out;
1671 if (smc->use_fallback) {
1672 rc = kernel_sock_shutdown(smc->clcsock, how);
1673 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1674 if (sk->sk_shutdown == SHUTDOWN_MASK)
1675 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001676 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001677 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001678 switch (how) {
1679 case SHUT_RDWR: /* shutdown in both directions */
1680 rc = smc_close_active(smc);
1681 break;
1682 case SHUT_WR:
1683 rc = smc_close_shutdown_write(smc);
1684 break;
1685 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001686 rc = 0;
1687 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001688 break;
1689 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001690 if (smc->clcsock)
1691 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001692 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1693 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001694
1695out:
1696 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001697 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001698}
1699
1700static int smc_setsockopt(struct socket *sock, int level, int optname,
1701 char __user *optval, unsigned int optlen)
1702{
1703 struct sock *sk = sock->sk;
1704 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001705 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001706
1707 smc = smc_sk(sk);
1708
1709 /* generic setsockopts reaching us here always apply to the
1710 * CLC socket
1711 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001712 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1713 optval, optlen);
1714 if (smc->clcsock->sk->sk_err) {
1715 sk->sk_err = smc->clcsock->sk->sk_err;
1716 sk->sk_error_report(sk);
1717 }
1718 if (rc)
1719 return rc;
1720
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001721 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00001722 return -EINVAL;
Ursula Braunac0107e2018-07-18 15:22:50 +02001723 if (get_user(val, (int __user *)optval))
1724 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001725
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001726 lock_sock(sk);
1727 switch (optname) {
1728 case TCP_ULP:
1729 case TCP_FASTOPEN:
1730 case TCP_FASTOPEN_CONNECT:
1731 case TCP_FASTOPEN_KEY:
1732 case TCP_FASTOPEN_NO_COOKIE:
1733 /* option not supported by SMC */
1734 if (sk->sk_state == SMC_INIT) {
Ursula Braun07603b22019-04-11 11:17:32 +02001735 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001736 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001737 } else {
1738 if (!smc->use_fallback)
1739 rc = -EINVAL;
1740 }
1741 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001742 case TCP_NODELAY:
Ursula Braunf9cedf12019-08-02 10:16:38 +02001743 if (sk->sk_state != SMC_INIT &&
1744 sk->sk_state != SMC_LISTEN &&
1745 sk->sk_state != SMC_CLOSED) {
Ursula Braun569bc642018-05-15 17:04:54 +02001746 if (val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001747 mod_delayed_work(system_wq, &smc->conn.tx_work,
1748 0);
1749 }
1750 break;
1751 case TCP_CORK:
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 Braun569bc642018-05-15 17:04:54 +02001755 if (!val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001756 mod_delayed_work(system_wq, &smc->conn.tx_work,
1757 0);
1758 }
1759 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001760 case TCP_DEFER_ACCEPT:
1761 smc->sockopt_defer_accept = val;
1762 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001763 default:
1764 break;
1765 }
1766 release_sock(sk);
1767
1768 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001769}
1770
1771static int smc_getsockopt(struct socket *sock, int level, int optname,
1772 char __user *optval, int __user *optlen)
1773{
1774 struct smc_sock *smc;
1775
1776 smc = smc_sk(sock->sk);
1777 /* socket options apply to the CLC socket */
1778 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1779 optval, optlen);
1780}
1781
1782static int smc_ioctl(struct socket *sock, unsigned int cmd,
1783 unsigned long arg)
1784{
Stefan Rasplde8474e2018-05-23 16:38:11 +02001785 union smc_host_cursor cons, urg;
1786 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01001787 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001788 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001789
1790 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001791 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02001792 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02001793 if (smc->use_fallback) {
1794 if (!smc->clcsock) {
1795 release_sock(&smc->sk);
1796 return -EBADF;
1797 }
1798 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1799 release_sock(&smc->sk);
1800 return answ;
1801 }
Ursula Braun9b67e262018-05-02 16:56:46 +02001802 switch (cmd) {
1803 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02001804 if (smc->sk.sk_state == SMC_LISTEN) {
1805 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001806 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001807 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001808 if (smc->sk.sk_state == SMC_INIT ||
1809 smc->sk.sk_state == SMC_CLOSED)
1810 answ = 0;
1811 else
1812 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02001813 break;
1814 case SIOCOUTQ:
1815 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02001816 if (smc->sk.sk_state == SMC_LISTEN) {
1817 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001818 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001819 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001820 if (smc->sk.sk_state == SMC_INIT ||
1821 smc->sk.sk_state == SMC_CLOSED)
1822 answ = 0;
1823 else
1824 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02001825 atomic_read(&smc->conn.sndbuf_space);
1826 break;
1827 case SIOCOUTQNSD:
1828 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02001829 if (smc->sk.sk_state == SMC_LISTEN) {
1830 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001831 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001832 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001833 if (smc->sk.sk_state == SMC_INIT ||
1834 smc->sk.sk_state == SMC_CLOSED)
1835 answ = 0;
1836 else
1837 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02001838 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02001839 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02001840 if (smc->sk.sk_state == SMC_LISTEN) {
1841 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001842 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001843 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001844 if (smc->sk.sk_state == SMC_INIT ||
1845 smc->sk.sk_state == SMC_CLOSED) {
1846 answ = 0;
1847 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02001848 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1849 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001850 answ = smc_curs_diff(conn->rmb_desc->len,
1851 &cons, &urg) == 1;
1852 }
1853 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02001854 default:
Ursula Braun1992d992018-07-16 13:56:52 +02001855 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001856 return -ENOIOCTLCMD;
1857 }
Ursula Braun1992d992018-07-16 13:56:52 +02001858 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001859
1860 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001861}
1862
1863static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1864 int offset, size_t size, int flags)
1865{
1866 struct sock *sk = sock->sk;
1867 struct smc_sock *smc;
1868 int rc = -EPIPE;
1869
1870 smc = smc_sk(sk);
1871 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001872 if (sk->sk_state != SMC_ACTIVE) {
1873 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001874 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001875 }
1876 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001877 if (smc->use_fallback)
1878 rc = kernel_sendpage(smc->clcsock, page, offset,
1879 size, flags);
1880 else
1881 rc = sock_no_sendpage(sock, page, offset, size, flags);
1882
1883out:
Ursula Braunac713872017-01-09 16:55:13 +01001884 return rc;
1885}
1886
Stefan Raspl9014db22018-05-03 18:12:39 +02001887/* Map the affected portions of the rmbe into an spd, note the number of bytes
1888 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1889 * updates till whenever a respective page has been fully processed.
1890 * Note that subsequent recv() calls have to wait till all splice() processing
1891 * completed.
1892 */
Ursula Braunac713872017-01-09 16:55:13 +01001893static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1894 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001895 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001896{
1897 struct sock *sk = sock->sk;
1898 struct smc_sock *smc;
1899 int rc = -ENOTCONN;
1900
1901 smc = smc_sk(sk);
1902 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001903 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1904 /* socket was connected before, no more data to read */
1905 rc = 0;
1906 goto out;
1907 }
Stefan Raspl9014db22018-05-03 18:12:39 +02001908 if (sk->sk_state == SMC_INIT ||
1909 sk->sk_state == SMC_LISTEN ||
1910 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001911 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001912
1913 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1914 rc = 0;
1915 goto out;
1916 }
1917
Ursula Braunac713872017-01-09 16:55:13 +01001918 if (smc->use_fallback) {
1919 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1920 pipe, len, flags);
1921 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001922 if (*ppos) {
1923 rc = -ESPIPE;
1924 goto out;
1925 }
1926 if (flags & SPLICE_F_NONBLOCK)
1927 flags = MSG_DONTWAIT;
1928 else
1929 flags = 0;
1930 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001931 }
1932out:
1933 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001934
Ursula Braunac713872017-01-09 16:55:13 +01001935 return rc;
1936}
1937
1938/* must look like tcp */
1939static const struct proto_ops smc_sock_ops = {
1940 .family = PF_SMC,
1941 .owner = THIS_MODULE,
1942 .release = smc_release,
1943 .bind = smc_bind,
1944 .connect = smc_connect,
1945 .socketpair = sock_no_socketpair,
1946 .accept = smc_accept,
1947 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001948 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01001949 .ioctl = smc_ioctl,
1950 .listen = smc_listen,
1951 .shutdown = smc_shutdown,
1952 .setsockopt = smc_setsockopt,
1953 .getsockopt = smc_getsockopt,
1954 .sendmsg = smc_sendmsg,
1955 .recvmsg = smc_recvmsg,
1956 .mmap = sock_no_mmap,
1957 .sendpage = smc_sendpage,
1958 .splice_read = smc_splice_read,
1959};
1960
1961static int smc_create(struct net *net, struct socket *sock, int protocol,
1962 int kern)
1963{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001964 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001965 struct smc_sock *smc;
1966 struct sock *sk;
1967 int rc;
1968
1969 rc = -ESOCKTNOSUPPORT;
1970 if (sock->type != SOCK_STREAM)
1971 goto out;
1972
1973 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001974 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01001975 goto out;
1976
1977 rc = -ENOBUFS;
1978 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001979 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001980 if (!sk)
1981 goto out;
1982
1983 /* create internal TCP socket for CLC handshake and fallback */
1984 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001985 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02001986 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001987 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1988 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01001989 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01001990 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01001991 goto out;
1992 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01001993 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1994 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01001995
1996out:
1997 return rc;
1998}
1999
2000static const struct net_proto_family smc_sock_family_ops = {
2001 .family = PF_SMC,
2002 .owner = THIS_MODULE,
2003 .create = smc_create,
2004};
2005
Hans Wippel64e28b52019-02-21 13:01:02 +01002006unsigned int smc_net_id;
2007
2008static __net_init int smc_net_init(struct net *net)
2009{
2010 return smc_pnet_net_init(net);
2011}
2012
2013static void __net_exit smc_net_exit(struct net *net)
2014{
2015 smc_pnet_net_exit(net);
2016}
2017
2018static struct pernet_operations smc_net_ops = {
2019 .init = smc_net_init,
2020 .exit = smc_net_exit,
2021 .id = &smc_net_id,
2022 .size = sizeof(struct smc_net),
2023};
2024
Ursula Braunac713872017-01-09 16:55:13 +01002025static int __init smc_init(void)
2026{
2027 int rc;
2028
Hans Wippel64e28b52019-02-21 13:01:02 +01002029 rc = register_pernet_subsys(&smc_net_ops);
2030 if (rc)
2031 return rc;
2032
Thomas Richter6812baa2017-01-09 16:55:15 +01002033 rc = smc_pnet_init();
2034 if (rc)
YueHaibing8c33bf12019-06-26 17:47:50 +02002035 goto out_pernet_subsys;
Thomas Richter6812baa2017-01-09 16:55:15 +01002036
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002037 rc = smc_llc_init();
2038 if (rc) {
2039 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
2040 goto out_pnet;
2041 }
2042
Ursula Braun5f083182017-01-09 16:55:22 +01002043 rc = smc_cdc_init();
2044 if (rc) {
2045 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
2046 goto out_pnet;
2047 }
2048
Ursula Braunac713872017-01-09 16:55:13 +01002049 rc = proto_register(&smc_proto, 1);
2050 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01002051 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Thomas Richter6812baa2017-01-09 16:55:15 +01002052 goto out_pnet;
Ursula Braunac713872017-01-09 16:55:13 +01002053 }
2054
Karsten Graulaaa4d332018-03-16 15:06:41 +01002055 rc = proto_register(&smc_proto6, 1);
2056 if (rc) {
2057 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2058 goto out_proto;
2059 }
2060
Ursula Braunac713872017-01-09 16:55:13 +01002061 rc = sock_register(&smc_sock_family_ops);
2062 if (rc) {
2063 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002064 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01002065 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01002066 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002067 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01002068
Ursula Brauna4cf0442017-01-09 16:55:14 +01002069 rc = smc_ib_register_client();
2070 if (rc) {
2071 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2072 goto out_sock;
2073 }
2074
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002075 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002076 return 0;
2077
Ursula Brauna4cf0442017-01-09 16:55:14 +01002078out_sock:
2079 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002080out_proto6:
2081 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002082out_proto:
2083 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002084out_pnet:
2085 smc_pnet_exit();
YueHaibing8c33bf12019-06-26 17:47:50 +02002086out_pernet_subsys:
2087 unregister_pernet_subsys(&smc_net_ops);
2088
Ursula Braunac713872017-01-09 16:55:13 +01002089 return rc;
2090}
2091
2092static void __exit smc_exit(void)
2093{
Hans Wippel9fda3512018-05-18 09:34:11 +02002094 smc_core_exit();
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002095 static_branch_disable(&tcp_have_smc);
Ursula Brauna4cf0442017-01-09 16:55:14 +01002096 smc_ib_unregister_client();
Ursula Braunac713872017-01-09 16:55:13 +01002097 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002098 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002099 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002100 smc_pnet_exit();
Hans Wippel64e28b52019-02-21 13:01:02 +01002101 unregister_pernet_subsys(&smc_net_ops);
Ursula Braunac713872017-01-09 16:55:13 +01002102}
2103
2104module_init(smc_init);
2105module_exit(smc_exit);
2106
2107MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2108MODULE_DESCRIPTION("smc socket address family");
2109MODULE_LICENSE("GPL");
2110MODULE_ALIAS_NETPROTO(PF_SMC);