blob: 5b932583e4076feacb786d315bc4d6181fd9a17b [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 Braun39f41f32019-06-27 15:04:52 +0200126static int __smc_release(struct smc_sock *smc)
Ursula Braunac713872017-01-09 16:55:13 +0100127{
Ursula Braun39f41f32019-06-27 15:04:52 +0200128 struct sock *sk = &smc->sk;
Ursula Braunb38d7322017-01-09 16:55:25 +0100129 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +0100130
Ursula Braun51f1de72018-01-26 09:28:48 +0100131 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100132 rc = smc_close_active(smc);
133 sock_set_flag(sk, SOCK_DEAD);
134 sk->sk_shutdown |= SHUTDOWN_MASK;
Ursula Braunb03faa12019-02-07 15:56:15 +0100135 } else {
136 if (sk->sk_state != SMC_LISTEN && sk->sk_state != SMC_INIT)
137 sock_put(sk); /* passive closing */
138 if (sk->sk_state == SMC_LISTEN) {
139 /* wake up clcsock accept */
140 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
141 }
142 sk->sk_state = SMC_CLOSED;
143 sk->sk_state_change(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100144 }
Cong Wang26d92e92019-01-05 23:45:26 -0800145
146 sk->sk_prot->unhash(sk);
147
Ursula Braunb03faa12019-02-07 15:56:15 +0100148 if (sk->sk_state == SMC_CLOSED) {
149 if (smc->clcsock) {
Karsten Graulfd577702019-04-11 11:17:30 +0200150 release_sock(sk);
151 smc_clcsock_release(smc);
152 lock_sock(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800153 }
Ursula Braunb03faa12019-02-07 15:56:15 +0100154 if (!smc->use_fallback)
155 smc_conn_free(&smc->conn);
Ursula Braun51f1de72018-01-26 09:28:48 +0100156 }
Ursula Braunac713872017-01-09 16:55:13 +0100157
Ursula Braun39f41f32019-06-27 15:04:52 +0200158 return rc;
159}
160
161static int smc_release(struct socket *sock)
162{
163 struct sock *sk = sock->sk;
164 struct smc_sock *smc;
165 int rc = 0;
166
167 if (!sk)
168 goto out;
169
170 smc = smc_sk(sk);
171
172 /* cleanup for a dangling non-blocking connect */
173 if (smc->connect_nonblock && sk->sk_state == SMC_INIT)
174 tcp_abort(smc->clcsock->sk, ECONNABORTED);
175 flush_work(&smc->connect_work);
176
177 if (sk->sk_state == SMC_LISTEN)
178 /* smc_close_non_accepted() is called and acquires
179 * sock lock for child sockets again
180 */
181 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
182 else
183 lock_sock(sk);
184
185 rc = __smc_release(smc);
186
Ursula Braunac713872017-01-09 16:55:13 +0100187 /* detach socket */
188 sock_orphan(sk);
189 sock->sk = NULL;
190 release_sock(sk);
191
Ursula Braun51f1de72018-01-26 09:28:48 +0100192 sock_put(sk); /* final sock_put */
Ursula Braunac713872017-01-09 16:55:13 +0100193out:
Ursula Braunb38d7322017-01-09 16:55:25 +0100194 return rc;
Ursula Braunac713872017-01-09 16:55:13 +0100195}
196
197static void smc_destruct(struct sock *sk)
198{
199 if (sk->sk_state != SMC_CLOSED)
200 return;
201 if (!sock_flag(sk, SOCK_DEAD))
202 return;
203
204 sk_refcnt_debug_dec(sk);
205}
206
Karsten Graulaaa4d332018-03-16 15:06:41 +0100207static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
208 int protocol)
Ursula Braunac713872017-01-09 16:55:13 +0100209{
210 struct smc_sock *smc;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100211 struct proto *prot;
Ursula Braunac713872017-01-09 16:55:13 +0100212 struct sock *sk;
213
Karsten Graulaaa4d332018-03-16 15:06:41 +0100214 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
215 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
Ursula Braunac713872017-01-09 16:55:13 +0100216 if (!sk)
217 return NULL;
218
219 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
220 sk->sk_state = SMC_INIT;
221 sk->sk_destruct = smc_destruct;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100222 sk->sk_protocol = protocol;
Ursula Braunac713872017-01-09 16:55:13 +0100223 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100224 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200225 INIT_WORK(&smc->connect_work, smc_connect_work);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700226 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
Ursula Brauna046d572017-01-09 16:55:16 +0100227 INIT_LIST_HEAD(&smc->accept_q);
228 spin_lock_init(&smc->accept_q_lock);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700229 spin_lock_init(&smc->conn.send_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100230 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100231 sk_refcnt_debug_inc(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800232 mutex_init(&smc->clcsock_release_lock);
Ursula Braunac713872017-01-09 16:55:13 +0100233
234 return sk;
235}
236
237static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
238 int addr_len)
239{
240 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
241 struct sock *sk = sock->sk;
242 struct smc_sock *smc;
243 int rc;
244
245 smc = smc_sk(sk);
246
247 /* replicate tests from inet_bind(), to be safe wrt. future changes */
248 rc = -EINVAL;
249 if (addr_len < sizeof(struct sockaddr_in))
250 goto out;
251
252 rc = -EAFNOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100253 if (addr->sin_family != AF_INET &&
254 addr->sin_family != AF_INET6 &&
255 addr->sin_family != AF_UNSPEC)
256 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100257 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
Karsten Graulaaa4d332018-03-16 15:06:41 +0100258 if (addr->sin_family == AF_UNSPEC &&
259 addr->sin_addr.s_addr != htonl(INADDR_ANY))
Ursula Braunac713872017-01-09 16:55:13 +0100260 goto out;
261
262 lock_sock(sk);
263
264 /* Check if socket is already active */
265 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +0200266 if (sk->sk_state != SMC_INIT || smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +0100267 goto out_rel;
268
269 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
270 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
271
272out_rel:
273 release_sock(sk);
274out:
275 return rc;
276}
277
278static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
279 unsigned long mask)
280{
281 /* options we don't get control via setsockopt for */
282 nsk->sk_type = osk->sk_type;
283 nsk->sk_sndbuf = osk->sk_sndbuf;
284 nsk->sk_rcvbuf = osk->sk_rcvbuf;
285 nsk->sk_sndtimeo = osk->sk_sndtimeo;
286 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
287 nsk->sk_mark = osk->sk_mark;
288 nsk->sk_priority = osk->sk_priority;
289 nsk->sk_rcvlowat = osk->sk_rcvlowat;
290 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
291 nsk->sk_err = osk->sk_err;
292
293 nsk->sk_flags &= ~mask;
294 nsk->sk_flags |= osk->sk_flags & mask;
295}
296
297#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
298 (1UL << SOCK_KEEPOPEN) | \
299 (1UL << SOCK_LINGER) | \
300 (1UL << SOCK_BROADCAST) | \
301 (1UL << SOCK_TIMESTAMP) | \
302 (1UL << SOCK_DBG) | \
303 (1UL << SOCK_RCVTSTAMP) | \
304 (1UL << SOCK_RCVTSTAMPNS) | \
305 (1UL << SOCK_LOCALROUTE) | \
306 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
307 (1UL << SOCK_RXQ_OVFL) | \
308 (1UL << SOCK_WIFI_STATUS) | \
309 (1UL << SOCK_NOFCS) | \
Deepa Dinamani97184752019-02-02 07:34:51 -0800310 (1UL << SOCK_FILTER_LOCKED) | \
311 (1UL << SOCK_TSTAMP_NEW))
Ursula Braunac713872017-01-09 16:55:13 +0100312/* copy only relevant settings and flags of SOL_SOCKET level from smc to
313 * clc socket (since smc is not called for these options from net/core)
314 */
315static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
316{
317 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
318}
319
320#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
321 (1UL << SOCK_KEEPOPEN) | \
322 (1UL << SOCK_LINGER) | \
323 (1UL << SOCK_DBG))
324/* copy only settings and flags relevant for smc from clc to smc socket */
325static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
326{
327 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
328}
329
Karsten Graulc7674c02018-11-22 10:26:43 +0100330/* register a new rmb, send confirm_rkey msg to register with peer */
Karsten Graul44aa81c2018-05-15 17:04:55 +0200331static int smc_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc,
332 bool conf_rkey)
Karsten Graule63a5f82018-05-03 17:57:37 +0200333{
Karsten Graulc7674c02018-11-22 10:26:43 +0100334 if (!rmb_desc->wr_reg) {
335 /* register memory region for new rmb */
336 if (smc_wr_reg_send(link, rmb_desc->mr_rx[SMC_SINGLE_LINK])) {
337 rmb_desc->regerr = 1;
338 return -EFAULT;
339 }
340 rmb_desc->wr_reg = 1;
Karsten Graula6920d12018-05-03 17:57:38 +0200341 }
Karsten Graul44aa81c2018-05-15 17:04:55 +0200342 if (!conf_rkey)
343 return 0;
344 /* exchange confirm_rkey msg with peer */
345 if (smc_llc_do_confirm_rkey(link, rmb_desc)) {
346 rmb_desc->regerr = 1;
347 return -EFAULT;
348 }
Karsten Graule63a5f82018-05-03 17:57:37 +0200349 return 0;
350}
351
Stefan Raspl0f627122018-03-01 13:51:26 +0100352static int smc_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100353{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200354 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100355 struct smc_link_group *lgr = smc->conn.lgr;
356 struct smc_link *link;
357 int rest;
358 int rc;
359
360 link = &lgr->lnk[SMC_SINGLE_LINK];
361 /* receive CONFIRM LINK request from server over RoCE fabric */
362 rest = wait_for_completion_interruptible_timeout(
363 &link->llc_confirm,
364 SMC_LLC_WAIT_FIRST_TIME);
365 if (rest <= 0) {
366 struct smc_clc_msg_decline dclc;
367
368 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100369 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100370 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100371 }
372
Karsten Graul75d320d2018-03-01 13:51:31 +0100373 if (link->llc_confirm_rc)
374 return SMC_CLC_DECL_RMBE_EC;
375
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100376 rc = smc_ib_modify_qp_rts(link);
377 if (rc)
Karsten Graul603cc142018-07-25 16:35:32 +0200378 return SMC_CLC_DECL_ERR_RDYLNK;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100379
380 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200381
Karsten Graul44aa81c2018-05-15 17:04:55 +0200382 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200383 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200384
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100385 /* send CONFIRM LINK response over RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200386 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100387 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200388 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100389
Karsten Graul52bedf32018-03-01 13:51:32 +0100390 /* receive ADD LINK request from server over RoCE fabric */
391 rest = wait_for_completion_interruptible_timeout(&link->llc_add,
392 SMC_LLC_WAIT_TIME);
393 if (rest <= 0) {
394 struct smc_clc_msg_decline dclc;
395
396 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100397 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100398 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
Karsten Graul52bedf32018-03-01 13:51:32 +0100399 }
400
401 /* send add link reject message, only one link supported for now */
402 rc = smc_llc_send_add_link(link,
403 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +0200404 link->gid, SMC_LLC_RESP);
Karsten Graul52bedf32018-03-01 13:51:32 +0100405 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200406 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +0100407
Karsten Graul877ae5b2018-05-02 16:56:44 +0200408 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100409
Karsten Graul75d320d2018-03-01 13:51:31 +0100410 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100411}
412
Hans Wippel41349842018-06-28 19:05:11 +0200413static void smcr_conn_save_peer_info(struct smc_sock *smc,
414 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100415{
Hans Wippel95d8d2632018-05-18 09:34:13 +0200416 int bufsize = smc_uncompress_bufsize(clc->rmbe_size);
417
Hans Wippel92a138e2018-05-18 09:34:12 +0200418 smc->conn.peer_rmbe_idx = clc->rmbe_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100419 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200420 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100421 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200422 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100423}
424
Hans Wippel41349842018-06-28 19:05:11 +0200425static void smcd_conn_save_peer_info(struct smc_sock *smc,
426 struct smc_clc_msg_accept_confirm *clc)
427{
428 int bufsize = smc_uncompress_bufsize(clc->dmbe_size);
429
430 smc->conn.peer_rmbe_idx = clc->dmbe_idx;
431 smc->conn.peer_token = clc->token;
432 /* msg header takes up space in the buffer */
433 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
434 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
435 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
436}
437
438static void smc_conn_save_peer_info(struct smc_sock *smc,
439 struct smc_clc_msg_accept_confirm *clc)
440{
441 if (smc->conn.lgr->is_smcd)
442 smcd_conn_save_peer_info(smc, clc);
443 else
444 smcr_conn_save_peer_info(smc, clc);
445}
446
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100447static void smc_link_save_peer_info(struct smc_link *link,
448 struct smc_clc_msg_accept_confirm *clc)
449{
450 link->peer_qpn = ntoh24(clc->qpn);
451 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
452 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
453 link->peer_psn = ntoh24(clc->psn);
454 link->peer_mtu = clc->qp_mtu;
455}
456
Ursula Braun07603b22019-04-11 11:17:32 +0200457static void smc_switch_to_fallback(struct smc_sock *smc)
458{
459 smc->use_fallback = true;
460 if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
461 smc->clcsock->file = smc->sk.sk_socket->file;
462 smc->clcsock->file->private_data = smc->clcsock;
463 }
464}
465
Hans Wippel3b2dec22018-05-18 09:34:18 +0200466/* fall back during connect */
Karsten Graul603cc142018-07-25 16:35:32 +0200467static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200468{
Ursula Braun07603b22019-04-11 11:17:32 +0200469 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +0200470 smc->fallback_rsn = reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200471 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200472 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200473 if (smc->sk.sk_state == SMC_INIT)
474 smc->sk.sk_state = SMC_ACTIVE;
475 return 0;
476}
477
478/* decline and fall back during connect */
479static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
480{
481 int rc;
482
Ursula Braune1bbdd52018-07-05 16:15:30 +0200483 if (reason_code < 0) { /* error, fallback is not possible */
484 if (smc->sk.sk_state == SMC_INIT)
485 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200486 return reason_code;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200487 }
Karsten Graul603cc142018-07-25 16:35:32 +0200488 if (reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200489 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200490 if (rc < 0) {
491 if (smc->sk.sk_state == SMC_INIT)
492 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200493 return rc;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200494 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200495 }
Karsten Graul603cc142018-07-25 16:35:32 +0200496 return smc_connect_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200497}
498
499/* abort connecting */
500static int smc_connect_abort(struct smc_sock *smc, int reason_code,
501 int local_contact)
502{
503 if (local_contact == SMC_FIRST_CONTACT)
504 smc_lgr_forget(smc->conn.lgr);
Hans Wippel72a36a82019-02-07 15:56:18 +0100505 if (smc->conn.lgr->is_smcd)
506 /* there is only one lgr role for SMC-D; use server lock */
507 mutex_unlock(&smc_server_lgr_pending);
508 else
509 mutex_unlock(&smc_client_lgr_pending);
510
Hans Wippel3b2dec22018-05-18 09:34:18 +0200511 smc_conn_free(&smc->conn);
Ursula Braun50717a32019-04-12 12:57:23 +0200512 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200513 return reason_code;
514}
515
516/* check if there is a rdma device available for this connection. */
517/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200518static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200519{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200520 /* PNET table look up: search active ib_device and port
521 * within same PNETID that also contains the ethernet device
522 * used for the internal TCP socket
523 */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200524 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +0200525 if (!ini->ib_dev)
526 return SMC_CLC_DECL_NOSMCRDEV;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200527 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200528}
529
Hans Wippel41349842018-06-28 19:05:11 +0200530/* check if there is an ISM device available for this connection. */
531/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200532static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200533{
534 /* Find ISM device with same PNETID as connecting interface */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200535 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
536 if (!ini->ism_dev)
Karsten Graul9aa68d22019-04-12 12:57:29 +0200537 return SMC_CLC_DECL_NOSMCDDEV;
Hans Wippel41349842018-06-28 19:05:11 +0200538 return 0;
539}
540
541/* Check for VLAN ID and register it on ISM device just for CLC handshake */
542static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200543 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200544{
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200545 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev, ini->vlan_id))
Karsten Graul7a627252019-04-12 12:57:30 +0200546 return SMC_CLC_DECL_ISMVLANERR;
Hans Wippel41349842018-06-28 19:05:11 +0200547 return 0;
548}
549
550/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
551 * used, the VLAN ID will be registered again during the connection setup.
552 */
553static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200554 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200555{
556 if (!is_smcd)
557 return 0;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200558 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev, ini->vlan_id))
Hans Wippel41349842018-06-28 19:05:11 +0200559 return SMC_CLC_DECL_CNFERR;
560 return 0;
561}
562
Hans Wippel3b2dec22018-05-18 09:34:18 +0200563/* CLC handshake during connect */
Hans Wippelc758dfd2018-06-28 19:05:09 +0200564static int smc_connect_clc(struct smc_sock *smc, int smc_type,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200565 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200566 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200567{
568 int rc = 0;
569
570 /* do inband token exchange */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200571 rc = smc_clc_send_proposal(smc, smc_type, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200572 if (rc)
573 return rc;
574 /* receive SMC Accept CLC message */
Ursula Braun2b59f582018-11-22 10:26:39 +0100575 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT,
576 CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200577}
578
Ursula Brauna046d572017-01-09 16:55:16 +0100579/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200580static int smc_connect_rdma(struct smc_sock *smc,
581 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200582 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200583{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200584 struct smc_link *link;
585 int reason_code = 0;
586
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200587 ini->is_smcd = false;
588 ini->ib_lcl = &aclc->lcl;
589 ini->ib_clcqpn = ntoh24(aclc->qpn);
590 ini->srv_first_contact = aclc->hdr.flag;
591
Hans Wippel72a36a82019-02-07 15:56:18 +0100592 mutex_lock(&smc_client_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200593 reason_code = smc_conn_create(smc, ini);
594 if (reason_code) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100595 mutex_unlock(&smc_client_lgr_pending);
596 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200597 }
598 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
599
600 smc_conn_save_peer_info(smc, aclc);
601
602 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200603 if (smc_buf_create(smc, false))
Karsten Graul7a627252019-04-12 12:57:30 +0200604 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
605 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200606
Karsten Graul7a627252019-04-12 12:57:30 +0200607 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200608 smc_link_save_peer_info(link, aclc);
609
610 if (smc_rmb_rtoken_handling(&smc->conn, aclc))
Karsten Graul603cc142018-07-25 16:35:32 +0200611 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
Karsten Graul7a627252019-04-12 12:57:30 +0200612 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200613
614 smc_close_init(smc);
615 smc_rx_init(smc);
616
Karsten Graul7a627252019-04-12 12:57:30 +0200617 if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200618 if (smc_ib_ready_link(link))
Karsten Graul603cc142018-07-25 16:35:32 +0200619 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
Karsten Graul7a627252019-04-12 12:57:30 +0200620 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200621 } else {
Karsten Graulc7674c02018-11-22 10:26:43 +0100622 if (smc_reg_rmb(link, smc->conn.rmb_desc, true))
Karsten Graul603cc142018-07-25 16:35:32 +0200623 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
Karsten Graul7a627252019-04-12 12:57:30 +0200624 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200625 }
626 smc_rmb_sync_sg_for_device(&smc->conn);
627
628 reason_code = smc_clc_send_confirm(smc);
629 if (reason_code)
Karsten Graul7a627252019-04-12 12:57:30 +0200630 return smc_connect_abort(smc, reason_code,
631 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200632
633 smc_tx_init(smc);
634
Karsten Graul7a627252019-04-12 12:57:30 +0200635 if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200636 /* QP confirmation over RoCE fabric */
637 reason_code = smc_clnt_conf_first_link(smc);
638 if (reason_code)
639 return smc_connect_abort(smc, reason_code,
Karsten Graul7a627252019-04-12 12:57:30 +0200640 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200641 }
Hans Wippel72a36a82019-02-07 15:56:18 +0100642 mutex_unlock(&smc_client_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200643
644 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200645 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200646 if (smc->sk.sk_state == SMC_INIT)
647 smc->sk.sk_state = SMC_ACTIVE;
648
649 return 0;
650}
651
Hans Wippel41349842018-06-28 19:05:11 +0200652/* setup for ISM connection of client */
653static int smc_connect_ism(struct smc_sock *smc,
654 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200655 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200656{
Hans Wippel41349842018-06-28 19:05:11 +0200657 int rc = 0;
658
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200659 ini->is_smcd = true;
660 ini->ism_gid = aclc->gid;
661 ini->srv_first_contact = aclc->hdr.flag;
662
Hans Wippel72a36a82019-02-07 15:56:18 +0100663 /* there is only one lgr role for SMC-D; use server lock */
664 mutex_lock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200665 rc = smc_conn_create(smc, ini);
666 if (rc) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100667 mutex_unlock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200668 return rc;
Hans Wippel72a36a82019-02-07 15:56:18 +0100669 }
Hans Wippel41349842018-06-28 19:05:11 +0200670
671 /* Create send and receive buffers */
672 if (smc_buf_create(smc, true))
Karsten Graul7a627252019-04-12 12:57:30 +0200673 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
674 ini->cln_first_contact);
Hans Wippel41349842018-06-28 19:05:11 +0200675
676 smc_conn_save_peer_info(smc, aclc);
677 smc_close_init(smc);
678 smc_rx_init(smc);
679 smc_tx_init(smc);
680
681 rc = smc_clc_send_confirm(smc);
682 if (rc)
Karsten Graul7a627252019-04-12 12:57:30 +0200683 return smc_connect_abort(smc, rc, ini->cln_first_contact);
Hans Wippel72a36a82019-02-07 15:56:18 +0100684 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel41349842018-06-28 19:05:11 +0200685
686 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200687 smc->connect_nonblock = 0;
Hans Wippel41349842018-06-28 19:05:11 +0200688 if (smc->sk.sk_state == SMC_INIT)
689 smc->sk.sk_state = SMC_ACTIVE;
690
691 return 0;
692}
693
Hans Wippel3b2dec22018-05-18 09:34:18 +0200694/* perform steps before actually connecting */
695static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100696{
Hans Wippel41349842018-06-28 19:05:11 +0200697 bool ism_supported = false, rdma_supported = false;
Ursula Brauna046d572017-01-09 16:55:16 +0100698 struct smc_clc_msg_accept_confirm aclc;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200699 struct smc_init_info ini = {0};
Hans Wippel41349842018-06-28 19:05:11 +0200700 int smc_type;
Ursula Brauna046d572017-01-09 16:55:16 +0100701 int rc = 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100702
Ursula Braun51f1de72018-01-26 09:28:48 +0100703 sock_hold(&smc->sk); /* sock put in passive closing */
704
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200705 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +0200706 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200707
Hans Wippel3b2dec22018-05-18 09:34:18 +0200708 /* if peer has not signalled SMC-capability, fall back */
709 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +0200710 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200711
Ursula Brauna046d572017-01-09 16:55:16 +0100712 /* IPSec connections opt out of SMC-R optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200713 if (using_ipsec(smc))
714 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
Ursula Brauna046d572017-01-09 16:55:16 +0100715
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200716 /* get vlan id from IP device */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200717 if (smc_vlan_by_tcpsk(smc->clcsock, &ini))
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200718 return smc_connect_decline_fallback(smc,
719 SMC_CLC_DECL_GETVLANERR);
Hans Wippel41349842018-06-28 19:05:11 +0200720
721 /* check if there is an ism device available */
Karsten Graul228bae02019-04-12 12:57:28 +0200722 if (!smc_find_ism_device(smc, &ini) &&
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200723 !smc_connect_ism_vlan_setup(smc, &ini)) {
Hans Wippel41349842018-06-28 19:05:11 +0200724 /* ISM is supported for this connection */
725 ism_supported = true;
726 smc_type = SMC_TYPE_D;
727 }
728
729 /* check if there is a rdma device available */
Karsten Graul228bae02019-04-12 12:57:28 +0200730 if (!smc_find_rdma_device(smc, &ini)) {
Hans Wippel41349842018-06-28 19:05:11 +0200731 /* RDMA is supported for this connection */
732 rdma_supported = true;
733 if (ism_supported)
734 smc_type = SMC_TYPE_B; /* both */
735 else
736 smc_type = SMC_TYPE_R; /* only RDMA */
737 }
738
739 /* if neither ISM nor RDMA are supported, fallback */
740 if (!rdma_supported && !ism_supported)
Karsten Graul603cc142018-07-25 16:35:32 +0200741 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV);
Ursula Brauna046d572017-01-09 16:55:16 +0100742
Hans Wippel3b2dec22018-05-18 09:34:18 +0200743 /* perform CLC handshake */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200744 rc = smc_connect_clc(smc, smc_type, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200745 if (rc) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200746 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200747 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200748 }
Ursula Brauna046d572017-01-09 16:55:16 +0100749
Hans Wippel41349842018-06-28 19:05:11 +0200750 /* depending on previous steps, connect using rdma or ism */
751 if (rdma_supported && aclc.hdr.path == SMC_TYPE_R)
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200752 rc = smc_connect_rdma(smc, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200753 else if (ism_supported && aclc.hdr.path == SMC_TYPE_D)
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200754 rc = smc_connect_ism(smc, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200755 else
Karsten Graul603cc142018-07-25 16:35:32 +0200756 rc = SMC_CLC_DECL_MODEUNSUPP;
Hans Wippel41349842018-06-28 19:05:11 +0200757 if (rc) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200758 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200759 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200760 }
Ursula Brauna046d572017-01-09 16:55:16 +0100761
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200762 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200763 return 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100764}
765
Ursula Braun24ac3a02018-06-27 17:59:50 +0200766static void smc_connect_work(struct work_struct *work)
767{
768 struct smc_sock *smc = container_of(work, struct smc_sock,
769 connect_work);
Ursula Braun50717a32019-04-12 12:57:23 +0200770 long timeo = smc->sk.sk_sndtimeo;
771 int rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200772
Ursula Braun50717a32019-04-12 12:57:23 +0200773 if (!timeo)
774 timeo = MAX_SCHEDULE_TIMEOUT;
775 lock_sock(smc->clcsock->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200776 if (smc->clcsock->sk->sk_err) {
777 smc->sk.sk_err = smc->clcsock->sk->sk_err;
Ursula Braun50717a32019-04-12 12:57:23 +0200778 } else if ((1 << smc->clcsock->sk->sk_state) &
779 (TCPF_SYN_SENT | TCP_SYN_RECV)) {
780 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
781 if ((rc == -EPIPE) &&
782 ((1 << smc->clcsock->sk->sk_state) &
783 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
784 rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200785 }
Ursula Braun50717a32019-04-12 12:57:23 +0200786 release_sock(smc->clcsock->sk);
787 lock_sock(&smc->sk);
788 if (rc != 0 || smc->sk.sk_err) {
789 smc->sk.sk_state = SMC_CLOSED;
790 if (rc == -EPIPE || rc == -EAGAIN)
791 smc->sk.sk_err = EPIPE;
792 else if (signal_pending(current))
793 smc->sk.sk_err = -sock_intr_errno(timeo);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200794 goto out;
795 }
796
797 rc = __smc_connect(smc);
798 if (rc < 0)
799 smc->sk.sk_err = -rc;
800
801out:
Ursula Braun07603b22019-04-11 11:17:32 +0200802 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
803 if (smc->sk.sk_err) {
804 smc->sk.sk_state_change(&smc->sk);
805 } else { /* allow polling before and after fallback decision */
806 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
807 smc->sk.sk_write_space(&smc->sk);
808 }
809 }
Ursula Braun24ac3a02018-06-27 17:59:50 +0200810 release_sock(&smc->sk);
811}
812
Ursula Braunac713872017-01-09 16:55:13 +0100813static int smc_connect(struct socket *sock, struct sockaddr *addr,
814 int alen, int flags)
815{
816 struct sock *sk = sock->sk;
817 struct smc_sock *smc;
818 int rc = -EINVAL;
819
820 smc = smc_sk(sk);
821
822 /* separate smc parameter checking to be safe */
823 if (alen < sizeof(addr->sa_family))
824 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100825 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100826 goto out_err;
827
828 lock_sock(sk);
829 switch (sk->sk_state) {
830 default:
831 goto out;
832 case SMC_ACTIVE:
833 rc = -EISCONN;
834 goto out;
835 case SMC_INIT:
836 rc = 0;
837 break;
838 }
839
840 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200841 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun50717a32019-04-12 12:57:23 +0200842 if (smc->connect_nonblock) {
843 rc = -EALREADY;
844 goto out;
845 }
846 rc = kernel_connect(smc->clcsock, addr, alen, flags);
847 if (rc && rc != -EINPROGRESS)
848 goto out;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200849 if (flags & O_NONBLOCK) {
Ursula Braun50717a32019-04-12 12:57:23 +0200850 if (schedule_work(&smc->connect_work))
851 smc->connect_nonblock = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200852 rc = -EINPROGRESS;
853 } else {
Ursula Braun24ac3a02018-06-27 17:59:50 +0200854 rc = __smc_connect(smc);
855 if (rc < 0)
856 goto out;
857 else
858 rc = 0; /* success cases including fallback */
859 }
Ursula Braunac713872017-01-09 16:55:13 +0100860
861out:
862 release_sock(sk);
863out_err:
864 return rc;
865}
866
867static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
868{
Ursula Braun3163c502018-01-24 10:28:12 +0100869 struct socket *new_clcsock = NULL;
870 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100871 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -0800872 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +0100873
Ursula Braun3163c502018-01-24 10:28:12 +0100874 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100875 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100876 if (!new_sk) {
877 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100878 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100879 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100880 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100881 goto out;
882 }
883 *new_smc = smc_sk(new_sk);
884
Myungho Jung78abe3d2018-12-18 09:02:25 -0800885 mutex_lock(&lsmc->clcsock_release_lock);
886 if (lsmc->clcsock)
887 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
888 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +0100889 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100890 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100891 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100892 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +0200893 new_sk->sk_prot->unhash(new_sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100894 if (new_clcsock)
895 sock_release(new_clcsock);
896 new_sk->sk_state = SMC_CLOSED;
897 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun51f1de72018-01-26 09:28:48 +0100898 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100899 *new_smc = NULL;
900 goto out;
901 }
902
903 (*new_smc)->clcsock = new_clcsock;
904out:
905 return rc;
906}
907
Ursula Brauna046d572017-01-09 16:55:16 +0100908/* add a just created sock to the accept queue of the listen sock as
909 * candidate for a following socket accept call from user space
910 */
911static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
912{
913 struct smc_sock *par = smc_sk(parent);
914
Ursula Braun51f1de72018-01-26 09:28:48 +0100915 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100916 spin_lock(&par->accept_q_lock);
917 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
918 spin_unlock(&par->accept_q_lock);
919 sk_acceptq_added(parent);
920}
921
922/* remove a socket from the accept queue of its parental listening socket */
923static void smc_accept_unlink(struct sock *sk)
924{
925 struct smc_sock *par = smc_sk(sk)->listen_smc;
926
927 spin_lock(&par->accept_q_lock);
928 list_del_init(&smc_sk(sk)->accept_q);
929 spin_unlock(&par->accept_q_lock);
930 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100931 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100932}
933
934/* remove a sock from the accept queue to bind it to a new socket created
935 * for a socket accept call from user space
936 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100937struct sock *smc_accept_dequeue(struct sock *parent,
938 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100939{
940 struct smc_sock *isk, *n;
941 struct sock *new_sk;
942
943 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
944 new_sk = (struct sock *)isk;
945
946 smc_accept_unlink(new_sk);
947 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +0200948 new_sk->sk_prot->unhash(new_sk);
Ursula Braun127f4972018-01-26 09:28:49 +0100949 if (isk->clcsock) {
950 sock_release(isk->clcsock);
951 isk->clcsock = NULL;
952 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100953 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +0100954 continue;
955 }
Ursula Braun07603b22019-04-11 11:17:32 +0200956 if (new_sock) {
Ursula Brauna046d572017-01-09 16:55:16 +0100957 sock_graft(new_sk, new_sock);
Ursula Braun07603b22019-04-11 11:17:32 +0200958 if (isk->use_fallback) {
959 smc_sk(new_sk)->clcsock->file = new_sock->file;
960 isk->clcsock->file->private_data = isk->clcsock;
961 }
962 }
Ursula Brauna046d572017-01-09 16:55:16 +0100963 return new_sk;
964 }
965 return NULL;
966}
967
968/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +0100969void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +0100970{
971 struct smc_sock *smc = smc_sk(sk);
972
Ursula Braunb38d7322017-01-09 16:55:25 +0100973 lock_sock(sk);
974 if (!sk->sk_lingertime)
975 /* wait for peer closing */
976 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun39f41f32019-06-27 15:04:52 +0200977 __smc_release(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +0100978 release_sock(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100979 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +0100980}
981
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100982static int smc_serv_conf_first_link(struct smc_sock *smc)
983{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200984 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100985 struct smc_link_group *lgr = smc->conn.lgr;
986 struct smc_link *link;
987 int rest;
988 int rc;
989
990 link = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun652a1e42017-07-28 13:56:17 +0200991
Karsten Graul44aa81c2018-05-15 17:04:55 +0200992 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200993 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200994
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100995 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200996 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100997 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200998 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100999
1000 /* receive CONFIRM LINK response from client over the RoCE fabric */
1001 rest = wait_for_completion_interruptible_timeout(
1002 &link->llc_confirm_resp,
1003 SMC_LLC_WAIT_FIRST_TIME);
1004 if (rest <= 0) {
1005 struct smc_clc_msg_decline dclc;
1006
1007 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001008 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001009 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001010 }
1011
Karsten Graul75d320d2018-03-01 13:51:31 +01001012 if (link->llc_confirm_resp_rc)
1013 return SMC_CLC_DECL_RMBE_EC;
1014
Karsten Graul52bedf32018-03-01 13:51:32 +01001015 /* send ADD LINK request to client over the RoCE fabric */
1016 rc = smc_llc_send_add_link(link,
1017 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +02001018 link->gid, SMC_LLC_REQ);
Karsten Graul52bedf32018-03-01 13:51:32 +01001019 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001020 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +01001021
1022 /* receive ADD LINK response from client over the RoCE fabric */
1023 rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
1024 SMC_LLC_WAIT_TIME);
1025 if (rest <= 0) {
1026 struct smc_clc_msg_decline dclc;
1027
1028 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001029 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001030 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
Karsten Graul52bedf32018-03-01 13:51:32 +01001031 }
1032
Karsten Graul877ae5b2018-05-02 16:56:44 +02001033 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +01001034
Karsten Graul75d320d2018-03-01 13:51:31 +01001035 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001036}
1037
Hans Wippel3b2dec22018-05-18 09:34:18 +02001038/* listen worker: finish */
1039static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001040{
Ursula Brauna046d572017-01-09 16:55:16 +01001041 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001042 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001043
Ursula Brauna046d572017-01-09 16:55:16 +01001044 if (lsmc->sk.sk_state == SMC_LISTEN) {
Karsten Graulfd577702019-04-11 11:17:30 +02001045 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001046 smc_accept_enqueue(&lsmc->sk, newsmcsk);
Karsten Graulfd577702019-04-11 11:17:30 +02001047 release_sock(&lsmc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001048 } else { /* no longer listening */
1049 smc_close_non_accepted(newsmcsk);
1050 }
Ursula Brauna046d572017-01-09 16:55:16 +01001051
1052 /* Wake up accept */
1053 lsmc->sk.sk_data_ready(&lsmc->sk);
1054 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001055}
Ursula Brauna046d572017-01-09 16:55:16 +01001056
Hans Wippel3b2dec22018-05-18 09:34:18 +02001057/* listen worker: finish in state connected */
1058static void smc_listen_out_connected(struct smc_sock *new_smc)
1059{
1060 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001061
Hans Wippel3b2dec22018-05-18 09:34:18 +02001062 sk_refcnt_debug_inc(newsmcsk);
1063 if (newsmcsk->sk_state == SMC_INIT)
1064 newsmcsk->sk_state = SMC_ACTIVE;
1065
1066 smc_listen_out(new_smc);
1067}
1068
1069/* listen worker: finish in error state */
1070static void smc_listen_out_err(struct smc_sock *new_smc)
1071{
1072 struct sock *newsmcsk = &new_smc->sk;
1073
Ursula Braun51f1de72018-01-26 09:28:48 +01001074 if (newsmcsk->sk_state == SMC_INIT)
1075 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001076 newsmcsk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001077 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001078
1079 smc_listen_out(new_smc);
1080}
1081
1082/* listen worker: decline and fall back if possible */
1083static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1084 int local_contact)
1085{
1086 /* RDMA setup failed, switch back to TCP */
1087 if (local_contact == SMC_FIRST_CONTACT)
1088 smc_lgr_forget(new_smc->conn.lgr);
1089 if (reason_code < 0) { /* error, no fallback possible */
1090 smc_listen_out_err(new_smc);
1091 return;
1092 }
1093 smc_conn_free(&new_smc->conn);
Ursula Braun07603b22019-04-11 11:17:32 +02001094 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001095 new_smc->fallback_rsn = reason_code;
1096 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001097 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1098 smc_listen_out_err(new_smc);
1099 return;
1100 }
1101 }
1102 smc_listen_out_connected(new_smc);
1103}
1104
1105/* listen worker: check prefixes */
Karsten Graul59886692019-04-12 12:57:25 +02001106static int smc_listen_prfx_check(struct smc_sock *new_smc,
Hans Wippel3b2dec22018-05-18 09:34:18 +02001107 struct smc_clc_msg_proposal *pclc)
1108{
1109 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1110 struct socket *newclcsock = new_smc->clcsock;
1111
1112 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1113 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
Karsten Graul59886692019-04-12 12:57:25 +02001114 return SMC_CLC_DECL_DIFFPREFIX;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001115
1116 return 0;
1117}
1118
1119/* listen worker: initialize connection and buffers */
1120static int smc_listen_rdma_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001121 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001122{
Karsten Graul7a627252019-04-12 12:57:30 +02001123 int rc;
1124
Hans Wippel3b2dec22018-05-18 09:34:18 +02001125 /* allocate connection / link group */
Karsten Graul7a627252019-04-12 12:57:30 +02001126 rc = smc_conn_create(new_smc, ini);
1127 if (rc)
1128 return rc;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001129
1130 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001131 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001132 return SMC_CLC_DECL_MEM;
1133
1134 return 0;
1135}
1136
Hans Wippel41349842018-06-28 19:05:11 +02001137/* listen worker: initialize connection and buffers for SMC-D */
1138static int smc_listen_ism_init(struct smc_sock *new_smc,
1139 struct smc_clc_msg_proposal *pclc,
Karsten Graul7a627252019-04-12 12:57:30 +02001140 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +02001141{
1142 struct smc_clc_msg_smcd *pclc_smcd;
Karsten Graul7a627252019-04-12 12:57:30 +02001143 int rc;
Hans Wippel41349842018-06-28 19:05:11 +02001144
1145 pclc_smcd = smc_get_clc_msg_smcd(pclc);
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001146 ini->ism_gid = pclc_smcd->gid;
Karsten Graul7a627252019-04-12 12:57:30 +02001147 rc = smc_conn_create(new_smc, ini);
1148 if (rc)
1149 return rc;
Hans Wippel41349842018-06-28 19:05:11 +02001150
1151 /* Check if peer can be reached via ISM device */
1152 if (smc_ism_cantalk(new_smc->conn.lgr->peer_gid,
1153 new_smc->conn.lgr->vlan_id,
1154 new_smc->conn.lgr->smcd)) {
Karsten Graul7a627252019-04-12 12:57:30 +02001155 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Hans Wippel41349842018-06-28 19:05:11 +02001156 smc_lgr_forget(new_smc->conn.lgr);
1157 smc_conn_free(&new_smc->conn);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001158 return SMC_CLC_DECL_SMCDNOTALK;
Hans Wippel41349842018-06-28 19:05:11 +02001159 }
1160
1161 /* Create send and receive buffers */
1162 if (smc_buf_create(new_smc, true)) {
Karsten Graul7a627252019-04-12 12:57:30 +02001163 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Hans Wippel41349842018-06-28 19:05:11 +02001164 smc_lgr_forget(new_smc->conn.lgr);
1165 smc_conn_free(&new_smc->conn);
1166 return SMC_CLC_DECL_MEM;
1167 }
1168
1169 return 0;
1170}
1171
Hans Wippel3b2dec22018-05-18 09:34:18 +02001172/* listen worker: register buffers */
1173static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
1174{
1175 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1176
1177 if (local_contact != SMC_FIRST_CONTACT) {
Karsten Graulc7674c02018-11-22 10:26:43 +01001178 if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true))
1179 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001180 }
1181 smc_rmb_sync_sg_for_device(&new_smc->conn);
1182
1183 return 0;
1184}
1185
1186/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001187static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1188 struct smc_clc_msg_accept_confirm *cclc,
1189 int local_contact)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001190{
1191 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1192 int reason_code = 0;
1193
1194 if (local_contact == SMC_FIRST_CONTACT)
1195 smc_link_save_peer_info(link, cclc);
1196
1197 if (smc_rmb_rtoken_handling(&new_smc->conn, cclc)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001198 reason_code = SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001199 goto decline;
1200 }
1201
1202 if (local_contact == SMC_FIRST_CONTACT) {
1203 if (smc_ib_ready_link(link)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001204 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001205 goto decline;
1206 }
1207 /* QP confirmation over RoCE fabric */
1208 reason_code = smc_serv_conf_first_link(new_smc);
1209 if (reason_code)
1210 goto decline;
1211 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001212 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001213
1214decline:
Hans Wippel3b2dec22018-05-18 09:34:18 +02001215 smc_listen_decline(new_smc, reason_code, local_contact);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001216 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001217}
1218
1219/* setup for RDMA connection of server */
1220static void smc_listen_work(struct work_struct *work)
1221{
1222 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1223 smc_listen_work);
1224 struct socket *newclcsock = new_smc->clcsock;
1225 struct smc_clc_msg_accept_confirm cclc;
1226 struct smc_clc_msg_proposal *pclc;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001227 struct smc_init_info ini = {0};
Hans Wippel41349842018-06-28 19:05:11 +02001228 bool ism_supported = false;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001229 u8 buf[SMC_CLC_MAX_LEN];
Hans Wippel3b2dec22018-05-18 09:34:18 +02001230 int rc = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001231
Karsten Graulfd577702019-04-11 11:17:30 +02001232 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1233 return smc_listen_out_err(new_smc);
1234
Hans Wippel3b2dec22018-05-18 09:34:18 +02001235 if (new_smc->use_fallback) {
1236 smc_listen_out_connected(new_smc);
1237 return;
1238 }
1239
1240 /* check if peer is smc capable */
1241 if (!tcp_sk(newclcsock->sk)->syn_smc) {
Ursula Braun07603b22019-04-11 11:17:32 +02001242 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001243 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001244 smc_listen_out_connected(new_smc);
1245 return;
1246 }
1247
1248 /* do inband token exchange -
1249 * wait for and receive SMC Proposal CLC message
1250 */
1251 pclc = (struct smc_clc_msg_proposal *)&buf;
Karsten Graul228bae02019-04-12 12:57:28 +02001252 rc = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
1253 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001254 if (rc)
1255 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001256
1257 /* IPSec connections opt out of SMC-R optimizations */
1258 if (using_ipsec(new_smc)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001259 rc = SMC_CLC_DECL_IPSEC;
1260 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001261 }
1262
Karsten Graul59886692019-04-12 12:57:25 +02001263 /* check for matching IP prefix and subnet length */
1264 rc = smc_listen_prfx_check(new_smc, pclc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001265 if (rc)
1266 goto out_decl;
Karsten Graul59886692019-04-12 12:57:25 +02001267
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001268 /* get vlan id from IP device */
1269 if (smc_vlan_by_tcpsk(new_smc->clcsock, &ini)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001270 rc = SMC_CLC_DECL_GETVLANERR;
1271 goto out_decl;
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001272 }
1273
Hans Wippel72a36a82019-02-07 15:56:18 +01001274 mutex_lock(&smc_server_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001275 smc_close_init(new_smc);
1276 smc_rx_init(new_smc);
1277 smc_tx_init(new_smc);
1278
Hans Wippel41349842018-06-28 19:05:11 +02001279 /* check if ISM is available */
Karsten Graul9aa68d22019-04-12 12:57:29 +02001280 if (pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) {
1281 ini.is_smcd = true; /* prepare ISM check */
1282 rc = smc_find_ism_device(new_smc, &ini);
1283 if (!rc)
Karsten Graul7a627252019-04-12 12:57:30 +02001284 rc = smc_listen_ism_init(new_smc, pclc, &ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001285 if (!rc)
1286 ism_supported = true;
1287 else if (pclc->hdr.path == SMC_TYPE_D)
1288 goto out_unlock; /* skip RDMA and decline */
1289 }
1290
1291 /* check if RDMA is available */
1292 if (!ism_supported) { /* SMC_TYPE_R or SMC_TYPE_B */
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001293 /* prepare RDMA check */
1294 memset(&ini, 0, sizeof(ini));
1295 ini.is_smcd = false;
1296 ini.ib_lcl = &pclc->lcl;
Karsten Graul9aa68d22019-04-12 12:57:29 +02001297 rc = smc_find_rdma_device(new_smc, &ini);
1298 if (rc) {
1299 /* no RDMA device found */
1300 if (pclc->hdr.path == SMC_TYPE_B)
1301 /* neither ISM nor RDMA device found */
1302 rc = SMC_CLC_DECL_NOSMCDEV;
1303 goto out_unlock;
1304 }
Karsten Graul7a627252019-04-12 12:57:30 +02001305 rc = smc_listen_rdma_init(new_smc, &ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001306 if (rc)
1307 goto out_unlock;
Karsten Graul7a627252019-04-12 12:57:30 +02001308 rc = smc_listen_rdma_reg(new_smc, ini.cln_first_contact);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001309 if (rc)
1310 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001311 }
1312
1313 /* send SMC Accept CLC message */
Karsten Graul7a627252019-04-12 12:57:30 +02001314 rc = smc_clc_send_accept(new_smc, ini.cln_first_contact);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001315 if (rc)
1316 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001317
Hans Wippel62c71392019-02-07 15:56:17 +01001318 /* SMC-D does not need this lock any more */
1319 if (ism_supported)
Hans Wippel72a36a82019-02-07 15:56:18 +01001320 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001321
Hans Wippel3b2dec22018-05-18 09:34:18 +02001322 /* receive SMC Confirm CLC message */
Karsten Graul228bae02019-04-12 12:57:28 +02001323 rc = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
1324 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
1325 if (rc) {
Hans Wippel62c71392019-02-07 15:56:17 +01001326 if (!ism_supported)
Karsten Graul9aa68d22019-04-12 12:57:29 +02001327 goto out_unlock;
1328 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001329 }
1330
1331 /* finish worker */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001332 if (!ism_supported) {
Karsten Graul7a627252019-04-12 12:57:30 +02001333 rc = smc_listen_rdma_finish(new_smc, &cclc,
1334 ini.cln_first_contact);
Hans Wippel72a36a82019-02-07 15:56:18 +01001335 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001336 if (rc)
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001337 return;
1338 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001339 smc_conn_save_peer_info(new_smc, &cclc);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001340 smc_listen_out_connected(new_smc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001341 return;
1342
1343out_unlock:
1344 mutex_unlock(&smc_server_lgr_pending);
1345out_decl:
Karsten Graul7a627252019-04-12 12:57:30 +02001346 smc_listen_decline(new_smc, rc, ini.cln_first_contact);
Ursula Brauna046d572017-01-09 16:55:16 +01001347}
1348
1349static void smc_tcp_listen_work(struct work_struct *work)
1350{
1351 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1352 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001353 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001354 struct smc_sock *new_smc;
1355 int rc = 0;
1356
Ursula Braun3163c502018-01-24 10:28:12 +01001357 lock_sock(lsk);
1358 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001359 rc = smc_clcsock_accept(lsmc, &new_smc);
1360 if (rc)
1361 goto out;
1362 if (!new_smc)
1363 continue;
1364
1365 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001366 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001367 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001368 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001369 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1370 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001371 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1372 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001373 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1374 if (!schedule_work(&new_smc->smc_listen_work))
1375 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001376 }
1377
1378out:
Ursula Braun3163c502018-01-24 10:28:12 +01001379 release_sock(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001380 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +01001381}
1382
Ursula Braunac713872017-01-09 16:55:13 +01001383static int smc_listen(struct socket *sock, int backlog)
1384{
1385 struct sock *sk = sock->sk;
1386 struct smc_sock *smc;
1387 int rc;
1388
1389 smc = smc_sk(sk);
1390 lock_sock(sk);
1391
1392 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +02001393 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
1394 smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +01001395 goto out;
1396
1397 rc = 0;
1398 if (sk->sk_state == SMC_LISTEN) {
1399 sk->sk_max_ack_backlog = backlog;
1400 goto out;
1401 }
1402 /* some socket options are handled in core, so we could not apply
1403 * them to the clc socket -- copy smc socket options to clc socket
1404 */
1405 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001406 if (!smc->use_fallback)
1407 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001408
1409 rc = kernel_listen(smc->clcsock, backlog);
1410 if (rc)
1411 goto out;
1412 sk->sk_max_ack_backlog = backlog;
1413 sk->sk_ack_backlog = 0;
1414 sk->sk_state = SMC_LISTEN;
Ursula Braun51f1de72018-01-26 09:28:48 +01001415 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1416 if (!schedule_work(&smc->tcp_listen_work))
1417 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001418
1419out:
1420 release_sock(sk);
1421 return rc;
1422}
1423
1424static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001425 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001426{
Ursula Brauna046d572017-01-09 16:55:16 +01001427 struct sock *sk = sock->sk, *nsk;
1428 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001429 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001430 long timeo;
1431 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001432
1433 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001434 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001435 lock_sock(sk);
1436
1437 if (lsmc->sk.sk_state != SMC_LISTEN) {
1438 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001439 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001440 goto out;
1441 }
1442
Ursula Brauna046d572017-01-09 16:55:16 +01001443 /* Wait for an incoming connection */
1444 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1445 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1446 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1447 set_current_state(TASK_INTERRUPTIBLE);
1448 if (!timeo) {
1449 rc = -EAGAIN;
1450 break;
1451 }
1452 release_sock(sk);
1453 timeo = schedule_timeout(timeo);
1454 /* wakeup by sk_data_ready in smc_listen_work() */
1455 sched_annotate_sleep();
1456 lock_sock(sk);
1457 if (signal_pending(current)) {
1458 rc = sock_intr_errno(timeo);
1459 break;
1460 }
1461 }
1462 set_current_state(TASK_RUNNING);
1463 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001464
Ursula Brauna046d572017-01-09 16:55:16 +01001465 if (!rc)
1466 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001467 release_sock(sk);
1468 if (rc)
1469 goto out;
1470
1471 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1472 /* wait till data arrives on the socket */
1473 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1474 MSEC_PER_SEC);
1475 if (smc_sk(nsk)->use_fallback) {
1476 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1477
1478 lock_sock(clcsk);
1479 if (skb_queue_empty(&clcsk->sk_receive_queue))
1480 sk_wait_data(clcsk, &timeo, NULL);
1481 release_sock(clcsk);
1482 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1483 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001484 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001485 release_sock(nsk);
1486 }
1487 }
Ursula Braunac713872017-01-09 16:55:13 +01001488
1489out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001490 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001491 return rc;
1492}
1493
1494static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001495 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001496{
1497 struct smc_sock *smc;
1498
Ursula Braunb38d7322017-01-09 16:55:25 +01001499 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1500 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001501 return -ENOTCONN;
1502
1503 smc = smc_sk(sock->sk);
1504
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001505 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001506}
1507
1508static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1509{
1510 struct sock *sk = sock->sk;
1511 struct smc_sock *smc;
1512 int rc = -EPIPE;
1513
1514 smc = smc_sk(sk);
1515 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001516 if ((sk->sk_state != SMC_ACTIVE) &&
1517 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1518 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001519 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001520
1521 if (msg->msg_flags & MSG_FASTOPEN) {
Ursula Brauncd206362019-08-02 10:47:50 +02001522 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001523 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001524 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001525 } else {
1526 rc = -EINVAL;
1527 goto out;
1528 }
1529 }
1530
Ursula Braunac713872017-01-09 16:55:13 +01001531 if (smc->use_fallback)
1532 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1533 else
Ursula Braune6727f32017-01-09 16:55:23 +01001534 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001535out:
1536 release_sock(sk);
1537 return rc;
1538}
1539
1540static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1541 int flags)
1542{
1543 struct sock *sk = sock->sk;
1544 struct smc_sock *smc;
1545 int rc = -ENOTCONN;
1546
1547 smc = smc_sk(sk);
1548 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001549 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1550 /* socket was connected before, no more data to read */
1551 rc = 0;
1552 goto out;
1553 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001554 if ((sk->sk_state == SMC_INIT) ||
1555 (sk->sk_state == SMC_LISTEN) ||
1556 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001557 goto out;
1558
Ursula Braunb38d7322017-01-09 16:55:25 +01001559 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1560 rc = 0;
1561 goto out;
1562 }
1563
Stefan Raspl9014db22018-05-03 18:12:39 +02001564 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001565 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001566 } else {
1567 msg->msg_namelen = 0;
1568 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1569 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001570
Ursula Braunac713872017-01-09 16:55:13 +01001571out:
1572 release_sock(sk);
1573 return rc;
1574}
1575
Al Viroade994f2017-07-03 00:01:49 -04001576static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001577{
Ursula Braun8dce2782018-01-26 09:28:47 +01001578 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001579 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001580
Ursula Braun8dce2782018-01-26 09:28:47 +01001581 spin_lock(&isk->accept_q_lock);
1582 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001583 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001584 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001585
Ursula Braun8dce2782018-01-26 09:28:47 +01001586 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001587}
1588
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001589static __poll_t smc_poll(struct file *file, struct socket *sock,
1590 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01001591{
1592 struct sock *sk = sock->sk;
Ursula Braunac713872017-01-09 16:55:13 +01001593 struct smc_sock *smc;
Ursula Braun50717a32019-04-12 12:57:23 +02001594 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001595
Ursula Braun8dce2782018-01-26 09:28:47 +01001596 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001597 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001598
Ursula Braunac713872017-01-09 16:55:13 +01001599 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02001600 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01001601 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001602 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02001603 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braunac713872017-01-09 16:55:13 +01001604 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07001605 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02001606 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01001607 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001608 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001609 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1610 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001611 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001612 if (sk->sk_state == SMC_LISTEN) {
1613 /* woken up by sk_data_ready in smc_listen_work() */
Ursula Braun50717a32019-04-12 12:57:23 +02001614 mask |= smc_accept_poll(sk);
1615 } else if (smc->use_fallback) { /* as result of connect_work()*/
1616 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
1617 wait);
1618 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun8dce2782018-01-26 09:28:47 +01001619 } else {
Ursula Braun50717a32019-04-12 12:57:23 +02001620 if ((sk->sk_state != SMC_INIT &&
1621 atomic_read(&smc->conn.sndbuf_space)) ||
Ursula Braun8dce2782018-01-26 09:28:47 +01001622 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001623 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001624 } else {
1625 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1626 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1627 }
1628 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001629 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001630 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001631 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001632 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001633 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02001634 if (smc->conn.urg_state == SMC_URG_VALID)
1635 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01001636 }
Ursula Braunac713872017-01-09 16:55:13 +01001637 }
1638
1639 return mask;
1640}
1641
1642static int smc_shutdown(struct socket *sock, int how)
1643{
1644 struct sock *sk = sock->sk;
1645 struct smc_sock *smc;
1646 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001647 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001648
1649 smc = smc_sk(sk);
1650
1651 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001652 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001653
1654 lock_sock(sk);
1655
1656 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02001657 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01001658 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1659 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1660 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1661 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1662 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001663 goto out;
1664 if (smc->use_fallback) {
1665 rc = kernel_sock_shutdown(smc->clcsock, how);
1666 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1667 if (sk->sk_shutdown == SHUTDOWN_MASK)
1668 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001669 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001670 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001671 switch (how) {
1672 case SHUT_RDWR: /* shutdown in both directions */
1673 rc = smc_close_active(smc);
1674 break;
1675 case SHUT_WR:
1676 rc = smc_close_shutdown_write(smc);
1677 break;
1678 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001679 rc = 0;
1680 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001681 break;
1682 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001683 if (smc->clcsock)
1684 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001685 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1686 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001687
1688out:
1689 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001690 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001691}
1692
1693static int smc_setsockopt(struct socket *sock, int level, int optname,
1694 char __user *optval, unsigned int optlen)
1695{
1696 struct sock *sk = sock->sk;
1697 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001698 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001699
1700 smc = smc_sk(sk);
1701
1702 /* generic setsockopts reaching us here always apply to the
1703 * CLC socket
1704 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001705 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1706 optval, optlen);
1707 if (smc->clcsock->sk->sk_err) {
1708 sk->sk_err = smc->clcsock->sk->sk_err;
1709 sk->sk_error_report(sk);
1710 }
1711 if (rc)
1712 return rc;
1713
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001714 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00001715 return -EINVAL;
Ursula Braunac0107e2018-07-18 15:22:50 +02001716 if (get_user(val, (int __user *)optval))
1717 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001718
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001719 lock_sock(sk);
1720 switch (optname) {
1721 case TCP_ULP:
1722 case TCP_FASTOPEN:
1723 case TCP_FASTOPEN_CONNECT:
1724 case TCP_FASTOPEN_KEY:
1725 case TCP_FASTOPEN_NO_COOKIE:
1726 /* option not supported by SMC */
1727 if (sk->sk_state == SMC_INIT) {
Ursula Braun07603b22019-04-11 11:17:32 +02001728 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001729 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001730 } else {
1731 if (!smc->use_fallback)
1732 rc = -EINVAL;
1733 }
1734 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001735 case TCP_NODELAY:
Ursula Braunf9cedf12019-08-02 10:16:38 +02001736 if (sk->sk_state != SMC_INIT &&
1737 sk->sk_state != SMC_LISTEN &&
1738 sk->sk_state != SMC_CLOSED) {
Ursula Braun569bc642018-05-15 17:04:54 +02001739 if (val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001740 mod_delayed_work(system_wq, &smc->conn.tx_work,
1741 0);
1742 }
1743 break;
1744 case TCP_CORK:
Ursula Braunf9cedf12019-08-02 10:16:38 +02001745 if (sk->sk_state != SMC_INIT &&
1746 sk->sk_state != SMC_LISTEN &&
1747 sk->sk_state != SMC_CLOSED) {
Ursula Braun569bc642018-05-15 17:04:54 +02001748 if (!val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001749 mod_delayed_work(system_wq, &smc->conn.tx_work,
1750 0);
1751 }
1752 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001753 case TCP_DEFER_ACCEPT:
1754 smc->sockopt_defer_accept = val;
1755 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001756 default:
1757 break;
1758 }
1759 release_sock(sk);
1760
1761 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001762}
1763
1764static int smc_getsockopt(struct socket *sock, int level, int optname,
1765 char __user *optval, int __user *optlen)
1766{
1767 struct smc_sock *smc;
1768
1769 smc = smc_sk(sock->sk);
1770 /* socket options apply to the CLC socket */
1771 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1772 optval, optlen);
1773}
1774
1775static int smc_ioctl(struct socket *sock, unsigned int cmd,
1776 unsigned long arg)
1777{
Stefan Rasplde8474e2018-05-23 16:38:11 +02001778 union smc_host_cursor cons, urg;
1779 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01001780 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001781 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001782
1783 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001784 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02001785 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02001786 if (smc->use_fallback) {
1787 if (!smc->clcsock) {
1788 release_sock(&smc->sk);
1789 return -EBADF;
1790 }
1791 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1792 release_sock(&smc->sk);
1793 return answ;
1794 }
Ursula Braun9b67e262018-05-02 16:56:46 +02001795 switch (cmd) {
1796 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02001797 if (smc->sk.sk_state == SMC_LISTEN) {
1798 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001799 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001800 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001801 if (smc->sk.sk_state == SMC_INIT ||
1802 smc->sk.sk_state == SMC_CLOSED)
1803 answ = 0;
1804 else
1805 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02001806 break;
1807 case SIOCOUTQ:
1808 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02001809 if (smc->sk.sk_state == SMC_LISTEN) {
1810 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001811 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001812 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001813 if (smc->sk.sk_state == SMC_INIT ||
1814 smc->sk.sk_state == SMC_CLOSED)
1815 answ = 0;
1816 else
1817 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02001818 atomic_read(&smc->conn.sndbuf_space);
1819 break;
1820 case SIOCOUTQNSD:
1821 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02001822 if (smc->sk.sk_state == SMC_LISTEN) {
1823 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001824 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001825 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001826 if (smc->sk.sk_state == SMC_INIT ||
1827 smc->sk.sk_state == SMC_CLOSED)
1828 answ = 0;
1829 else
1830 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02001831 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02001832 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02001833 if (smc->sk.sk_state == SMC_LISTEN) {
1834 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001835 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001836 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001837 if (smc->sk.sk_state == SMC_INIT ||
1838 smc->sk.sk_state == SMC_CLOSED) {
1839 answ = 0;
1840 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02001841 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1842 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001843 answ = smc_curs_diff(conn->rmb_desc->len,
1844 &cons, &urg) == 1;
1845 }
1846 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02001847 default:
Ursula Braun1992d992018-07-16 13:56:52 +02001848 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001849 return -ENOIOCTLCMD;
1850 }
Ursula Braun1992d992018-07-16 13:56:52 +02001851 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001852
1853 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001854}
1855
1856static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1857 int offset, size_t size, int flags)
1858{
1859 struct sock *sk = sock->sk;
1860 struct smc_sock *smc;
1861 int rc = -EPIPE;
1862
1863 smc = smc_sk(sk);
1864 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001865 if (sk->sk_state != SMC_ACTIVE) {
1866 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001867 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001868 }
1869 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001870 if (smc->use_fallback)
1871 rc = kernel_sendpage(smc->clcsock, page, offset,
1872 size, flags);
1873 else
1874 rc = sock_no_sendpage(sock, page, offset, size, flags);
1875
1876out:
Ursula Braunac713872017-01-09 16:55:13 +01001877 return rc;
1878}
1879
Stefan Raspl9014db22018-05-03 18:12:39 +02001880/* Map the affected portions of the rmbe into an spd, note the number of bytes
1881 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1882 * updates till whenever a respective page has been fully processed.
1883 * Note that subsequent recv() calls have to wait till all splice() processing
1884 * completed.
1885 */
Ursula Braunac713872017-01-09 16:55:13 +01001886static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1887 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001888 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001889{
1890 struct sock *sk = sock->sk;
1891 struct smc_sock *smc;
1892 int rc = -ENOTCONN;
1893
1894 smc = smc_sk(sk);
1895 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001896 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1897 /* socket was connected before, no more data to read */
1898 rc = 0;
1899 goto out;
1900 }
Stefan Raspl9014db22018-05-03 18:12:39 +02001901 if (sk->sk_state == SMC_INIT ||
1902 sk->sk_state == SMC_LISTEN ||
1903 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001904 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001905
1906 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1907 rc = 0;
1908 goto out;
1909 }
1910
Ursula Braunac713872017-01-09 16:55:13 +01001911 if (smc->use_fallback) {
1912 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1913 pipe, len, flags);
1914 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001915 if (*ppos) {
1916 rc = -ESPIPE;
1917 goto out;
1918 }
1919 if (flags & SPLICE_F_NONBLOCK)
1920 flags = MSG_DONTWAIT;
1921 else
1922 flags = 0;
1923 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001924 }
1925out:
1926 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001927
Ursula Braunac713872017-01-09 16:55:13 +01001928 return rc;
1929}
1930
1931/* must look like tcp */
1932static const struct proto_ops smc_sock_ops = {
1933 .family = PF_SMC,
1934 .owner = THIS_MODULE,
1935 .release = smc_release,
1936 .bind = smc_bind,
1937 .connect = smc_connect,
1938 .socketpair = sock_no_socketpair,
1939 .accept = smc_accept,
1940 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001941 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01001942 .ioctl = smc_ioctl,
1943 .listen = smc_listen,
1944 .shutdown = smc_shutdown,
1945 .setsockopt = smc_setsockopt,
1946 .getsockopt = smc_getsockopt,
1947 .sendmsg = smc_sendmsg,
1948 .recvmsg = smc_recvmsg,
1949 .mmap = sock_no_mmap,
1950 .sendpage = smc_sendpage,
1951 .splice_read = smc_splice_read,
1952};
1953
1954static int smc_create(struct net *net, struct socket *sock, int protocol,
1955 int kern)
1956{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001957 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001958 struct smc_sock *smc;
1959 struct sock *sk;
1960 int rc;
1961
1962 rc = -ESOCKTNOSUPPORT;
1963 if (sock->type != SOCK_STREAM)
1964 goto out;
1965
1966 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001967 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01001968 goto out;
1969
1970 rc = -ENOBUFS;
1971 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001972 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001973 if (!sk)
1974 goto out;
1975
1976 /* create internal TCP socket for CLC handshake and fallback */
1977 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001978 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02001979 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001980 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1981 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01001982 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01001983 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01001984 goto out;
1985 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01001986 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1987 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01001988
1989out:
1990 return rc;
1991}
1992
1993static const struct net_proto_family smc_sock_family_ops = {
1994 .family = PF_SMC,
1995 .owner = THIS_MODULE,
1996 .create = smc_create,
1997};
1998
Hans Wippel64e28b52019-02-21 13:01:02 +01001999unsigned int smc_net_id;
2000
2001static __net_init int smc_net_init(struct net *net)
2002{
2003 return smc_pnet_net_init(net);
2004}
2005
2006static void __net_exit smc_net_exit(struct net *net)
2007{
2008 smc_pnet_net_exit(net);
2009}
2010
2011static struct pernet_operations smc_net_ops = {
2012 .init = smc_net_init,
2013 .exit = smc_net_exit,
2014 .id = &smc_net_id,
2015 .size = sizeof(struct smc_net),
2016};
2017
Ursula Braunac713872017-01-09 16:55:13 +01002018static int __init smc_init(void)
2019{
2020 int rc;
2021
Hans Wippel64e28b52019-02-21 13:01:02 +01002022 rc = register_pernet_subsys(&smc_net_ops);
2023 if (rc)
2024 return rc;
2025
Thomas Richter6812baa2017-01-09 16:55:15 +01002026 rc = smc_pnet_init();
2027 if (rc)
YueHaibing8c33bf12019-06-26 17:47:50 +02002028 goto out_pernet_subsys;
Thomas Richter6812baa2017-01-09 16:55:15 +01002029
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002030 rc = smc_llc_init();
2031 if (rc) {
2032 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
2033 goto out_pnet;
2034 }
2035
Ursula Braun5f083182017-01-09 16:55:22 +01002036 rc = smc_cdc_init();
2037 if (rc) {
2038 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
2039 goto out_pnet;
2040 }
2041
Ursula Braunac713872017-01-09 16:55:13 +01002042 rc = proto_register(&smc_proto, 1);
2043 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01002044 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Thomas Richter6812baa2017-01-09 16:55:15 +01002045 goto out_pnet;
Ursula Braunac713872017-01-09 16:55:13 +01002046 }
2047
Karsten Graulaaa4d332018-03-16 15:06:41 +01002048 rc = proto_register(&smc_proto6, 1);
2049 if (rc) {
2050 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2051 goto out_proto;
2052 }
2053
Ursula Braunac713872017-01-09 16:55:13 +01002054 rc = sock_register(&smc_sock_family_ops);
2055 if (rc) {
2056 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002057 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01002058 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01002059 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002060 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01002061
Ursula Brauna4cf0442017-01-09 16:55:14 +01002062 rc = smc_ib_register_client();
2063 if (rc) {
2064 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2065 goto out_sock;
2066 }
2067
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002068 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002069 return 0;
2070
Ursula Brauna4cf0442017-01-09 16:55:14 +01002071out_sock:
2072 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002073out_proto6:
2074 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002075out_proto:
2076 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002077out_pnet:
2078 smc_pnet_exit();
YueHaibing8c33bf12019-06-26 17:47:50 +02002079out_pernet_subsys:
2080 unregister_pernet_subsys(&smc_net_ops);
2081
Ursula Braunac713872017-01-09 16:55:13 +01002082 return rc;
2083}
2084
2085static void __exit smc_exit(void)
2086{
Hans Wippel9fda3512018-05-18 09:34:11 +02002087 smc_core_exit();
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002088 static_branch_disable(&tcp_have_smc);
Ursula Brauna4cf0442017-01-09 16:55:14 +01002089 smc_ib_unregister_client();
Ursula Braunac713872017-01-09 16:55:13 +01002090 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002091 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002092 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002093 smc_pnet_exit();
Hans Wippel64e28b52019-02-21 13:01:02 +01002094 unregister_pernet_subsys(&smc_net_ops);
Ursula Braunac713872017-01-09 16:55:13 +01002095}
2096
2097module_init(smc_init);
2098module_exit(smc_exit);
2099
2100MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2101MODULE_DESCRIPTION("smc socket address family");
2102MODULE_LICENSE("GPL");
2103MODULE_ALIAS_NETPROTO(PF_SMC);