blob: e066899de72d08151109c27d732859e5ff25c1b8 [file] [log] [blame]
Ursula Braunac713872017-01-09 16:55:13 +01001/*
2 * Shared Memory Communications over RDMA (SMC-R) and RoCE
3 *
4 * AF_SMC protocol family socket handler keeping the AF_INET sock address type
5 * applies to SOCK_STREAM sockets only
6 * offers an alternative communication option for TCP-protocol sockets
7 * applicable with RoCE-cards only
8 *
Ursula Brauna046d572017-01-09 16:55:16 +01009 * Initial restrictions:
Ursula Brauna046d572017-01-09 16:55:16 +010010 * - support for alternate links postponed
Ursula Brauna046d572017-01-09 16:55:16 +010011 *
Karsten Graulaaa4d332018-03-16 15:06:41 +010012 * Copyright IBM Corp. 2016, 2018
Ursula Braunac713872017-01-09 16:55:13 +010013 *
14 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
15 * based on prototype from Frank Blaschka
16 */
17
18#define KMSG_COMPONENT "smc"
19#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
20
21#include <linux/module.h>
22#include <linux/socket.h>
Ursula Brauna046d572017-01-09 16:55:16 +010023#include <linux/workqueue.h>
Ursula Braun5f083182017-01-09 16:55:22 +010024#include <linux/in.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010025#include <linux/sched/signal.h>
Hans Wippel41349842018-06-28 19:05:11 +020026#include <linux/if_vlan.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010027
Ursula Braunac713872017-01-09 16:55:13 +010028#include <net/sock.h>
Ursula Brauna046d572017-01-09 16:55:16 +010029#include <net/tcp.h>
Ursula Braunf16a7dd2017-01-09 16:55:26 +010030#include <net/smc.h>
Ursula Braun9b67e262018-05-02 16:56:46 +020031#include <asm/ioctls.h>
Ursula Braunac713872017-01-09 16:55:13 +010032
Hans Wippel64e28b52019-02-21 13:01:02 +010033#include <net/net_namespace.h>
34#include <net/netns/generic.h>
35#include "smc_netns.h"
36
Ursula Braunac713872017-01-09 16:55:13 +010037#include "smc.h"
Ursula Brauna046d572017-01-09 16:55:16 +010038#include "smc_clc.h"
Ursula Braun9bf9abe2017-01-09 16:55:21 +010039#include "smc_llc.h"
Ursula Braun5f083182017-01-09 16:55:22 +010040#include "smc_cdc.h"
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010041#include "smc_core.h"
Ursula Brauna4cf0442017-01-09 16:55:14 +010042#include "smc_ib.h"
Hans Wippel41349842018-06-28 19:05:11 +020043#include "smc_ism.h"
Thomas Richter6812baa2017-01-09 16:55:15 +010044#include "smc_pnet.h"
Ursula Braune6727f32017-01-09 16:55:23 +010045#include "smc_tx.h"
Ursula Braun952310c2017-01-09 16:55:24 +010046#include "smc_rx.h"
Ursula Braunb38d7322017-01-09 16:55:25 +010047#include "smc_close.h"
Ursula Braunac713872017-01-09 16:55:13 +010048
Hans Wippel72a36a82019-02-07 15:56:18 +010049static DEFINE_MUTEX(smc_server_lgr_pending); /* serialize link group
50 * creation on server
51 */
52static DEFINE_MUTEX(smc_client_lgr_pending); /* serialize link group
53 * creation on client
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010054 */
55
Ursula Brauna046d572017-01-09 16:55:16 +010056static void smc_tcp_listen_work(struct work_struct *);
Ursula Braun24ac3a02018-06-27 17:59:50 +020057static void smc_connect_work(struct work_struct *);
Ursula Brauna046d572017-01-09 16:55:16 +010058
Ursula Braunac713872017-01-09 16:55:13 +010059static void smc_set_keepalive(struct sock *sk, int val)
60{
61 struct smc_sock *smc = smc_sk(sk);
62
63 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
64}
65
Ursula Braunf16a7dd2017-01-09 16:55:26 +010066static struct smc_hashinfo smc_v4_hashinfo = {
67 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
68};
69
Karsten Graulaaa4d332018-03-16 15:06:41 +010070static struct smc_hashinfo smc_v6_hashinfo = {
71 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
72};
73
Ursula Braunf16a7dd2017-01-09 16:55:26 +010074int smc_hash_sk(struct sock *sk)
75{
76 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
77 struct hlist_head *head;
78
79 head = &h->ht;
80
81 write_lock_bh(&h->lock);
82 sk_add_node(sk, head);
83 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
84 write_unlock_bh(&h->lock);
85
86 return 0;
87}
88EXPORT_SYMBOL_GPL(smc_hash_sk);
89
90void smc_unhash_sk(struct sock *sk)
91{
92 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
93
94 write_lock_bh(&h->lock);
95 if (sk_del_node_init(sk))
96 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
97 write_unlock_bh(&h->lock);
98}
99EXPORT_SYMBOL_GPL(smc_unhash_sk);
100
101struct proto smc_proto = {
Ursula Braunac713872017-01-09 16:55:13 +0100102 .name = "SMC",
103 .owner = THIS_MODULE,
104 .keepalive = smc_set_keepalive,
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100105 .hash = smc_hash_sk,
106 .unhash = smc_unhash_sk,
Ursula Braunac713872017-01-09 16:55:13 +0100107 .obj_size = sizeof(struct smc_sock),
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100108 .h.smc_hash = &smc_v4_hashinfo,
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800109 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Ursula Braunac713872017-01-09 16:55:13 +0100110};
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100111EXPORT_SYMBOL_GPL(smc_proto);
Ursula Braunac713872017-01-09 16:55:13 +0100112
Karsten Graulaaa4d332018-03-16 15:06:41 +0100113struct proto smc_proto6 = {
114 .name = "SMC6",
115 .owner = THIS_MODULE,
116 .keepalive = smc_set_keepalive,
117 .hash = smc_hash_sk,
118 .unhash = smc_unhash_sk,
119 .obj_size = sizeof(struct smc_sock),
120 .h.smc_hash = &smc_v6_hashinfo,
121 .slab_flags = SLAB_TYPESAFE_BY_RCU,
122};
123EXPORT_SYMBOL_GPL(smc_proto6);
124
Ursula Braunac713872017-01-09 16:55:13 +0100125static int smc_release(struct socket *sock)
126{
127 struct sock *sk = sock->sk;
128 struct smc_sock *smc;
Ursula Braunb38d7322017-01-09 16:55:25 +0100129 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +0100130
131 if (!sk)
132 goto out;
133
134 smc = smc_sk(sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200135
136 /* cleanup for a dangling non-blocking connect */
Ursula Braun50717a32019-04-12 12:57:23 +0200137 if (smc->connect_nonblock && sk->sk_state == SMC_INIT)
Hans Wippelf07920a2018-11-20 16:46:39 +0100138 tcp_abort(smc->clcsock->sk, ECONNABORTED);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200139 flush_work(&smc->connect_work);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200140
Ursula Braunb38d7322017-01-09 16:55:25 +0100141 if (sk->sk_state == SMC_LISTEN)
142 /* smc_close_non_accepted() is called and acquires
143 * sock lock for child sockets again
144 */
145 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
146 else
147 lock_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +0100148
Ursula Braun51f1de72018-01-26 09:28:48 +0100149 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100150 rc = smc_close_active(smc);
151 sock_set_flag(sk, SOCK_DEAD);
152 sk->sk_shutdown |= SHUTDOWN_MASK;
Ursula Braunb03faa12019-02-07 15:56:15 +0100153 } else {
154 if (sk->sk_state != SMC_LISTEN && sk->sk_state != SMC_INIT)
155 sock_put(sk); /* passive closing */
156 if (sk->sk_state == SMC_LISTEN) {
157 /* wake up clcsock accept */
158 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
159 }
160 sk->sk_state = SMC_CLOSED;
161 sk->sk_state_change(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100162 }
Cong Wang26d92e92019-01-05 23:45:26 -0800163
164 sk->sk_prot->unhash(sk);
165
Ursula Braunb03faa12019-02-07 15:56:15 +0100166 if (sk->sk_state == SMC_CLOSED) {
167 if (smc->clcsock) {
168 mutex_lock(&smc->clcsock_release_lock);
169 sock_release(smc->clcsock);
170 smc->clcsock = NULL;
171 mutex_unlock(&smc->clcsock_release_lock);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800172 }
Ursula Braunb03faa12019-02-07 15:56:15 +0100173 if (!smc->use_fallback)
174 smc_conn_free(&smc->conn);
Ursula Braun51f1de72018-01-26 09:28:48 +0100175 }
Ursula Braunac713872017-01-09 16:55:13 +0100176
177 /* detach socket */
178 sock_orphan(sk);
179 sock->sk = NULL;
180 release_sock(sk);
181
Ursula Braun51f1de72018-01-26 09:28:48 +0100182 sock_put(sk); /* final sock_put */
Ursula Braunac713872017-01-09 16:55:13 +0100183out:
Ursula Braunb38d7322017-01-09 16:55:25 +0100184 return rc;
Ursula Braunac713872017-01-09 16:55:13 +0100185}
186
187static void smc_destruct(struct sock *sk)
188{
189 if (sk->sk_state != SMC_CLOSED)
190 return;
191 if (!sock_flag(sk, SOCK_DEAD))
192 return;
193
194 sk_refcnt_debug_dec(sk);
195}
196
Karsten Graulaaa4d332018-03-16 15:06:41 +0100197static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
198 int protocol)
Ursula Braunac713872017-01-09 16:55:13 +0100199{
200 struct smc_sock *smc;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100201 struct proto *prot;
Ursula Braunac713872017-01-09 16:55:13 +0100202 struct sock *sk;
203
Karsten Graulaaa4d332018-03-16 15:06:41 +0100204 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
205 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
Ursula Braunac713872017-01-09 16:55:13 +0100206 if (!sk)
207 return NULL;
208
209 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
210 sk->sk_state = SMC_INIT;
211 sk->sk_destruct = smc_destruct;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100212 sk->sk_protocol = protocol;
Ursula Braunac713872017-01-09 16:55:13 +0100213 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100214 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200215 INIT_WORK(&smc->connect_work, smc_connect_work);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700216 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
Ursula Brauna046d572017-01-09 16:55:16 +0100217 INIT_LIST_HEAD(&smc->accept_q);
218 spin_lock_init(&smc->accept_q_lock);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700219 spin_lock_init(&smc->conn.send_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100220 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100221 sk_refcnt_debug_inc(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800222 mutex_init(&smc->clcsock_release_lock);
Ursula Braunac713872017-01-09 16:55:13 +0100223
224 return sk;
225}
226
227static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
228 int addr_len)
229{
230 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
231 struct sock *sk = sock->sk;
232 struct smc_sock *smc;
233 int rc;
234
235 smc = smc_sk(sk);
236
237 /* replicate tests from inet_bind(), to be safe wrt. future changes */
238 rc = -EINVAL;
239 if (addr_len < sizeof(struct sockaddr_in))
240 goto out;
241
242 rc = -EAFNOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100243 if (addr->sin_family != AF_INET &&
244 addr->sin_family != AF_INET6 &&
245 addr->sin_family != AF_UNSPEC)
246 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100247 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
Karsten Graulaaa4d332018-03-16 15:06:41 +0100248 if (addr->sin_family == AF_UNSPEC &&
249 addr->sin_addr.s_addr != htonl(INADDR_ANY))
Ursula Braunac713872017-01-09 16:55:13 +0100250 goto out;
251
252 lock_sock(sk);
253
254 /* Check if socket is already active */
255 rc = -EINVAL;
256 if (sk->sk_state != SMC_INIT)
257 goto out_rel;
258
259 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
260 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
261
262out_rel:
263 release_sock(sk);
264out:
265 return rc;
266}
267
268static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
269 unsigned long mask)
270{
271 /* options we don't get control via setsockopt for */
272 nsk->sk_type = osk->sk_type;
273 nsk->sk_sndbuf = osk->sk_sndbuf;
274 nsk->sk_rcvbuf = osk->sk_rcvbuf;
275 nsk->sk_sndtimeo = osk->sk_sndtimeo;
276 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
277 nsk->sk_mark = osk->sk_mark;
278 nsk->sk_priority = osk->sk_priority;
279 nsk->sk_rcvlowat = osk->sk_rcvlowat;
280 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
281 nsk->sk_err = osk->sk_err;
282
283 nsk->sk_flags &= ~mask;
284 nsk->sk_flags |= osk->sk_flags & mask;
285}
286
287#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
288 (1UL << SOCK_KEEPOPEN) | \
289 (1UL << SOCK_LINGER) | \
290 (1UL << SOCK_BROADCAST) | \
291 (1UL << SOCK_TIMESTAMP) | \
292 (1UL << SOCK_DBG) | \
293 (1UL << SOCK_RCVTSTAMP) | \
294 (1UL << SOCK_RCVTSTAMPNS) | \
295 (1UL << SOCK_LOCALROUTE) | \
296 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
297 (1UL << SOCK_RXQ_OVFL) | \
298 (1UL << SOCK_WIFI_STATUS) | \
299 (1UL << SOCK_NOFCS) | \
Deepa Dinamani97184752019-02-02 07:34:51 -0800300 (1UL << SOCK_FILTER_LOCKED) | \
301 (1UL << SOCK_TSTAMP_NEW))
Ursula Braunac713872017-01-09 16:55:13 +0100302/* copy only relevant settings and flags of SOL_SOCKET level from smc to
303 * clc socket (since smc is not called for these options from net/core)
304 */
305static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
306{
307 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
308}
309
310#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
311 (1UL << SOCK_KEEPOPEN) | \
312 (1UL << SOCK_LINGER) | \
313 (1UL << SOCK_DBG))
314/* copy only settings and flags relevant for smc from clc to smc socket */
315static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
316{
317 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
318}
319
Karsten Graulc7674c02018-11-22 10:26:43 +0100320/* register a new rmb, send confirm_rkey msg to register with peer */
Karsten Graul44aa81c2018-05-15 17:04:55 +0200321static int smc_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc,
322 bool conf_rkey)
Karsten Graule63a5f82018-05-03 17:57:37 +0200323{
Karsten Graulc7674c02018-11-22 10:26:43 +0100324 if (!rmb_desc->wr_reg) {
325 /* register memory region for new rmb */
326 if (smc_wr_reg_send(link, rmb_desc->mr_rx[SMC_SINGLE_LINK])) {
327 rmb_desc->regerr = 1;
328 return -EFAULT;
329 }
330 rmb_desc->wr_reg = 1;
Karsten Graula6920d12018-05-03 17:57:38 +0200331 }
Karsten Graul44aa81c2018-05-15 17:04:55 +0200332 if (!conf_rkey)
333 return 0;
334 /* exchange confirm_rkey msg with peer */
335 if (smc_llc_do_confirm_rkey(link, rmb_desc)) {
336 rmb_desc->regerr = 1;
337 return -EFAULT;
338 }
Karsten Graule63a5f82018-05-03 17:57:37 +0200339 return 0;
340}
341
Stefan Raspl0f627122018-03-01 13:51:26 +0100342static int smc_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100343{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200344 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100345 struct smc_link_group *lgr = smc->conn.lgr;
346 struct smc_link *link;
347 int rest;
348 int rc;
349
350 link = &lgr->lnk[SMC_SINGLE_LINK];
351 /* receive CONFIRM LINK request from server over RoCE fabric */
352 rest = wait_for_completion_interruptible_timeout(
353 &link->llc_confirm,
354 SMC_LLC_WAIT_FIRST_TIME);
355 if (rest <= 0) {
356 struct smc_clc_msg_decline dclc;
357
358 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100359 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100360 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100361 }
362
Karsten Graul75d320d2018-03-01 13:51:31 +0100363 if (link->llc_confirm_rc)
364 return SMC_CLC_DECL_RMBE_EC;
365
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100366 rc = smc_ib_modify_qp_rts(link);
367 if (rc)
Karsten Graul603cc142018-07-25 16:35:32 +0200368 return SMC_CLC_DECL_ERR_RDYLNK;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100369
370 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200371
Karsten Graul44aa81c2018-05-15 17:04:55 +0200372 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200373 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200374
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100375 /* send CONFIRM LINK response over RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200376 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100377 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200378 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100379
Karsten Graul52bedf32018-03-01 13:51:32 +0100380 /* receive ADD LINK request from server over RoCE fabric */
381 rest = wait_for_completion_interruptible_timeout(&link->llc_add,
382 SMC_LLC_WAIT_TIME);
383 if (rest <= 0) {
384 struct smc_clc_msg_decline dclc;
385
386 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100387 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100388 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
Karsten Graul52bedf32018-03-01 13:51:32 +0100389 }
390
391 /* send add link reject message, only one link supported for now */
392 rc = smc_llc_send_add_link(link,
393 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +0200394 link->gid, SMC_LLC_RESP);
Karsten Graul52bedf32018-03-01 13:51:32 +0100395 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200396 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +0100397
Karsten Graul877ae5b2018-05-02 16:56:44 +0200398 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100399
Karsten Graul75d320d2018-03-01 13:51:31 +0100400 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100401}
402
Hans Wippel41349842018-06-28 19:05:11 +0200403static void smcr_conn_save_peer_info(struct smc_sock *smc,
404 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100405{
Hans Wippel95d8d2632018-05-18 09:34:13 +0200406 int bufsize = smc_uncompress_bufsize(clc->rmbe_size);
407
Hans Wippel92a138e2018-05-18 09:34:12 +0200408 smc->conn.peer_rmbe_idx = clc->rmbe_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100409 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200410 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100411 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200412 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100413}
414
Hans Wippel41349842018-06-28 19:05:11 +0200415static void smcd_conn_save_peer_info(struct smc_sock *smc,
416 struct smc_clc_msg_accept_confirm *clc)
417{
418 int bufsize = smc_uncompress_bufsize(clc->dmbe_size);
419
420 smc->conn.peer_rmbe_idx = clc->dmbe_idx;
421 smc->conn.peer_token = clc->token;
422 /* msg header takes up space in the buffer */
423 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
424 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
425 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
426}
427
428static void smc_conn_save_peer_info(struct smc_sock *smc,
429 struct smc_clc_msg_accept_confirm *clc)
430{
431 if (smc->conn.lgr->is_smcd)
432 smcd_conn_save_peer_info(smc, clc);
433 else
434 smcr_conn_save_peer_info(smc, clc);
435}
436
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100437static void smc_link_save_peer_info(struct smc_link *link,
438 struct smc_clc_msg_accept_confirm *clc)
439{
440 link->peer_qpn = ntoh24(clc->qpn);
441 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
442 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
443 link->peer_psn = ntoh24(clc->psn);
444 link->peer_mtu = clc->qp_mtu;
445}
446
Hans Wippel3b2dec22018-05-18 09:34:18 +0200447/* fall back during connect */
Karsten Graul603cc142018-07-25 16:35:32 +0200448static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200449{
450 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +0200451 smc->fallback_rsn = reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200452 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200453 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200454 if (smc->sk.sk_state == SMC_INIT)
455 smc->sk.sk_state = SMC_ACTIVE;
456 return 0;
457}
458
459/* decline and fall back during connect */
460static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
461{
462 int rc;
463
Ursula Braune1bbdd52018-07-05 16:15:30 +0200464 if (reason_code < 0) { /* error, fallback is not possible */
465 if (smc->sk.sk_state == SMC_INIT)
466 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200467 return reason_code;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200468 }
Karsten Graul603cc142018-07-25 16:35:32 +0200469 if (reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200470 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200471 if (rc < 0) {
472 if (smc->sk.sk_state == SMC_INIT)
473 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200474 return rc;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200475 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200476 }
Karsten Graul603cc142018-07-25 16:35:32 +0200477 return smc_connect_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200478}
479
480/* abort connecting */
481static int smc_connect_abort(struct smc_sock *smc, int reason_code,
482 int local_contact)
483{
484 if (local_contact == SMC_FIRST_CONTACT)
485 smc_lgr_forget(smc->conn.lgr);
Hans Wippel72a36a82019-02-07 15:56:18 +0100486 if (smc->conn.lgr->is_smcd)
487 /* there is only one lgr role for SMC-D; use server lock */
488 mutex_unlock(&smc_server_lgr_pending);
489 else
490 mutex_unlock(&smc_client_lgr_pending);
491
Hans Wippel3b2dec22018-05-18 09:34:18 +0200492 smc_conn_free(&smc->conn);
Ursula Braun50717a32019-04-12 12:57:23 +0200493 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200494 return reason_code;
495}
496
497/* check if there is a rdma device available for this connection. */
498/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200499static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200500{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200501 /* PNET table look up: search active ib_device and port
502 * within same PNETID that also contains the ethernet device
503 * used for the internal TCP socket
504 */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200505 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +0200506 if (!ini->ib_dev)
507 return SMC_CLC_DECL_NOSMCRDEV;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200508 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200509}
510
Hans Wippel41349842018-06-28 19:05:11 +0200511/* check if there is an ISM device available for this connection. */
512/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200513static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200514{
515 /* Find ISM device with same PNETID as connecting interface */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200516 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
517 if (!ini->ism_dev)
Karsten Graul9aa68d22019-04-12 12:57:29 +0200518 return SMC_CLC_DECL_NOSMCDDEV;
Hans Wippel41349842018-06-28 19:05:11 +0200519 return 0;
520}
521
522/* Check for VLAN ID and register it on ISM device just for CLC handshake */
523static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200524 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200525{
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200526 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev, ini->vlan_id))
Karsten Graul7a627252019-04-12 12:57:30 +0200527 return SMC_CLC_DECL_ISMVLANERR;
Hans Wippel41349842018-06-28 19:05:11 +0200528 return 0;
529}
530
531/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
532 * used, the VLAN ID will be registered again during the connection setup.
533 */
534static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200535 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200536{
537 if (!is_smcd)
538 return 0;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200539 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev, ini->vlan_id))
Hans Wippel41349842018-06-28 19:05:11 +0200540 return SMC_CLC_DECL_CNFERR;
541 return 0;
542}
543
Hans Wippel3b2dec22018-05-18 09:34:18 +0200544/* CLC handshake during connect */
Hans Wippelc758dfd2018-06-28 19:05:09 +0200545static int smc_connect_clc(struct smc_sock *smc, int smc_type,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200546 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200547 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200548{
549 int rc = 0;
550
551 /* do inband token exchange */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200552 rc = smc_clc_send_proposal(smc, smc_type, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200553 if (rc)
554 return rc;
555 /* receive SMC Accept CLC message */
Ursula Braun2b59f582018-11-22 10:26:39 +0100556 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT,
557 CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200558}
559
Ursula Brauna046d572017-01-09 16:55:16 +0100560/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200561static int smc_connect_rdma(struct smc_sock *smc,
562 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200563 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200564{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200565 struct smc_link *link;
566 int reason_code = 0;
567
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200568 ini->is_smcd = false;
569 ini->ib_lcl = &aclc->lcl;
570 ini->ib_clcqpn = ntoh24(aclc->qpn);
571 ini->srv_first_contact = aclc->hdr.flag;
572
Hans Wippel72a36a82019-02-07 15:56:18 +0100573 mutex_lock(&smc_client_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200574 reason_code = smc_conn_create(smc, ini);
575 if (reason_code) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100576 mutex_unlock(&smc_client_lgr_pending);
577 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200578 }
579 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
580
581 smc_conn_save_peer_info(smc, aclc);
582
583 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200584 if (smc_buf_create(smc, false))
Karsten Graul7a627252019-04-12 12:57:30 +0200585 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
586 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200587
Karsten Graul7a627252019-04-12 12:57:30 +0200588 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200589 smc_link_save_peer_info(link, aclc);
590
591 if (smc_rmb_rtoken_handling(&smc->conn, aclc))
Karsten Graul603cc142018-07-25 16:35:32 +0200592 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
Karsten Graul7a627252019-04-12 12:57:30 +0200593 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200594
595 smc_close_init(smc);
596 smc_rx_init(smc);
597
Karsten Graul7a627252019-04-12 12:57:30 +0200598 if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200599 if (smc_ib_ready_link(link))
Karsten Graul603cc142018-07-25 16:35:32 +0200600 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
Karsten Graul7a627252019-04-12 12:57:30 +0200601 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200602 } else {
Karsten Graulc7674c02018-11-22 10:26:43 +0100603 if (smc_reg_rmb(link, smc->conn.rmb_desc, true))
Karsten Graul603cc142018-07-25 16:35:32 +0200604 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
Karsten Graul7a627252019-04-12 12:57:30 +0200605 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200606 }
607 smc_rmb_sync_sg_for_device(&smc->conn);
608
609 reason_code = smc_clc_send_confirm(smc);
610 if (reason_code)
Karsten Graul7a627252019-04-12 12:57:30 +0200611 return smc_connect_abort(smc, reason_code,
612 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200613
614 smc_tx_init(smc);
615
Karsten Graul7a627252019-04-12 12:57:30 +0200616 if (ini->cln_first_contact == SMC_FIRST_CONTACT) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200617 /* QP confirmation over RoCE fabric */
618 reason_code = smc_clnt_conf_first_link(smc);
619 if (reason_code)
620 return smc_connect_abort(smc, reason_code,
Karsten Graul7a627252019-04-12 12:57:30 +0200621 ini->cln_first_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200622 }
Hans Wippel72a36a82019-02-07 15:56:18 +0100623 mutex_unlock(&smc_client_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200624
625 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200626 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200627 if (smc->sk.sk_state == SMC_INIT)
628 smc->sk.sk_state = SMC_ACTIVE;
629
630 return 0;
631}
632
Hans Wippel41349842018-06-28 19:05:11 +0200633/* setup for ISM connection of client */
634static int smc_connect_ism(struct smc_sock *smc,
635 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200636 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200637{
Hans Wippel41349842018-06-28 19:05:11 +0200638 int rc = 0;
639
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200640 ini->is_smcd = true;
641 ini->ism_gid = aclc->gid;
642 ini->srv_first_contact = aclc->hdr.flag;
643
Hans Wippel72a36a82019-02-07 15:56:18 +0100644 /* there is only one lgr role for SMC-D; use server lock */
645 mutex_lock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200646 rc = smc_conn_create(smc, ini);
647 if (rc) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100648 mutex_unlock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200649 return rc;
Hans Wippel72a36a82019-02-07 15:56:18 +0100650 }
Hans Wippel41349842018-06-28 19:05:11 +0200651
652 /* Create send and receive buffers */
653 if (smc_buf_create(smc, true))
Karsten Graul7a627252019-04-12 12:57:30 +0200654 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
655 ini->cln_first_contact);
Hans Wippel41349842018-06-28 19:05:11 +0200656
657 smc_conn_save_peer_info(smc, aclc);
658 smc_close_init(smc);
659 smc_rx_init(smc);
660 smc_tx_init(smc);
661
662 rc = smc_clc_send_confirm(smc);
663 if (rc)
Karsten Graul7a627252019-04-12 12:57:30 +0200664 return smc_connect_abort(smc, rc, ini->cln_first_contact);
Hans Wippel72a36a82019-02-07 15:56:18 +0100665 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel41349842018-06-28 19:05:11 +0200666
667 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200668 smc->connect_nonblock = 0;
Hans Wippel41349842018-06-28 19:05:11 +0200669 if (smc->sk.sk_state == SMC_INIT)
670 smc->sk.sk_state = SMC_ACTIVE;
671
672 return 0;
673}
674
Hans Wippel3b2dec22018-05-18 09:34:18 +0200675/* perform steps before actually connecting */
676static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100677{
Hans Wippel41349842018-06-28 19:05:11 +0200678 bool ism_supported = false, rdma_supported = false;
Ursula Brauna046d572017-01-09 16:55:16 +0100679 struct smc_clc_msg_accept_confirm aclc;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200680 struct smc_init_info ini = {0};
Hans Wippel41349842018-06-28 19:05:11 +0200681 int smc_type;
Ursula Brauna046d572017-01-09 16:55:16 +0100682 int rc = 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100683
Ursula Braun51f1de72018-01-26 09:28:48 +0100684 sock_hold(&smc->sk); /* sock put in passive closing */
685
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200686 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +0200687 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200688
Hans Wippel3b2dec22018-05-18 09:34:18 +0200689 /* if peer has not signalled SMC-capability, fall back */
690 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +0200691 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200692
Ursula Brauna046d572017-01-09 16:55:16 +0100693 /* IPSec connections opt out of SMC-R optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200694 if (using_ipsec(smc))
695 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
Ursula Brauna046d572017-01-09 16:55:16 +0100696
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200697 /* get vlan id from IP device */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200698 if (smc_vlan_by_tcpsk(smc->clcsock, &ini))
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200699 return smc_connect_decline_fallback(smc,
700 SMC_CLC_DECL_GETVLANERR);
Hans Wippel41349842018-06-28 19:05:11 +0200701
702 /* check if there is an ism device available */
Karsten Graul228bae02019-04-12 12:57:28 +0200703 if (!smc_find_ism_device(smc, &ini) &&
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200704 !smc_connect_ism_vlan_setup(smc, &ini)) {
Hans Wippel41349842018-06-28 19:05:11 +0200705 /* ISM is supported for this connection */
706 ism_supported = true;
707 smc_type = SMC_TYPE_D;
708 }
709
710 /* check if there is a rdma device available */
Karsten Graul228bae02019-04-12 12:57:28 +0200711 if (!smc_find_rdma_device(smc, &ini)) {
Hans Wippel41349842018-06-28 19:05:11 +0200712 /* RDMA is supported for this connection */
713 rdma_supported = true;
714 if (ism_supported)
715 smc_type = SMC_TYPE_B; /* both */
716 else
717 smc_type = SMC_TYPE_R; /* only RDMA */
718 }
719
720 /* if neither ISM nor RDMA are supported, fallback */
721 if (!rdma_supported && !ism_supported)
Karsten Graul603cc142018-07-25 16:35:32 +0200722 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV);
Ursula Brauna046d572017-01-09 16:55:16 +0100723
Hans Wippel3b2dec22018-05-18 09:34:18 +0200724 /* perform CLC handshake */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200725 rc = smc_connect_clc(smc, smc_type, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200726 if (rc) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200727 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200728 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200729 }
Ursula Brauna046d572017-01-09 16:55:16 +0100730
Hans Wippel41349842018-06-28 19:05:11 +0200731 /* depending on previous steps, connect using rdma or ism */
732 if (rdma_supported && aclc.hdr.path == SMC_TYPE_R)
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200733 rc = smc_connect_rdma(smc, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200734 else if (ism_supported && aclc.hdr.path == SMC_TYPE_D)
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200735 rc = smc_connect_ism(smc, &aclc, &ini);
Hans Wippel41349842018-06-28 19:05:11 +0200736 else
Karsten Graul603cc142018-07-25 16:35:32 +0200737 rc = SMC_CLC_DECL_MODEUNSUPP;
Hans Wippel41349842018-06-28 19:05:11 +0200738 if (rc) {
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200739 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200740 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200741 }
Ursula Brauna046d572017-01-09 16:55:16 +0100742
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200743 smc_connect_ism_vlan_cleanup(smc, ism_supported, &ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200744 return 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100745}
746
Ursula Braun24ac3a02018-06-27 17:59:50 +0200747static void smc_connect_work(struct work_struct *work)
748{
749 struct smc_sock *smc = container_of(work, struct smc_sock,
750 connect_work);
Ursula Braun50717a32019-04-12 12:57:23 +0200751 long timeo = smc->sk.sk_sndtimeo;
752 int rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200753
Ursula Braun50717a32019-04-12 12:57:23 +0200754 if (!timeo)
755 timeo = MAX_SCHEDULE_TIMEOUT;
756 lock_sock(smc->clcsock->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200757 if (smc->clcsock->sk->sk_err) {
758 smc->sk.sk_err = smc->clcsock->sk->sk_err;
Ursula Braun50717a32019-04-12 12:57:23 +0200759 } else if ((1 << smc->clcsock->sk->sk_state) &
760 (TCPF_SYN_SENT | TCP_SYN_RECV)) {
761 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
762 if ((rc == -EPIPE) &&
763 ((1 << smc->clcsock->sk->sk_state) &
764 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
765 rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200766 }
Ursula Braun50717a32019-04-12 12:57:23 +0200767 release_sock(smc->clcsock->sk);
768 lock_sock(&smc->sk);
769 if (rc != 0 || smc->sk.sk_err) {
770 smc->sk.sk_state = SMC_CLOSED;
771 if (rc == -EPIPE || rc == -EAGAIN)
772 smc->sk.sk_err = EPIPE;
773 else if (signal_pending(current))
774 smc->sk.sk_err = -sock_intr_errno(timeo);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200775 goto out;
776 }
777
778 rc = __smc_connect(smc);
779 if (rc < 0)
780 smc->sk.sk_err = -rc;
781
782out:
Ursula Braun648a5a72018-09-18 15:46:34 +0200783 if (smc->sk.sk_err)
784 smc->sk.sk_state_change(&smc->sk);
785 else
786 smc->sk.sk_write_space(&smc->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200787 release_sock(&smc->sk);
788}
789
Ursula Braunac713872017-01-09 16:55:13 +0100790static int smc_connect(struct socket *sock, struct sockaddr *addr,
791 int alen, int flags)
792{
793 struct sock *sk = sock->sk;
794 struct smc_sock *smc;
795 int rc = -EINVAL;
796
797 smc = smc_sk(sk);
798
799 /* separate smc parameter checking to be safe */
800 if (alen < sizeof(addr->sa_family))
801 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100802 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100803 goto out_err;
804
805 lock_sock(sk);
806 switch (sk->sk_state) {
807 default:
808 goto out;
809 case SMC_ACTIVE:
810 rc = -EISCONN;
811 goto out;
812 case SMC_INIT:
813 rc = 0;
814 break;
815 }
816
817 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200818 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun50717a32019-04-12 12:57:23 +0200819 if (smc->connect_nonblock) {
820 rc = -EALREADY;
821 goto out;
822 }
823 rc = kernel_connect(smc->clcsock, addr, alen, flags);
824 if (rc && rc != -EINPROGRESS)
825 goto out;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200826 if (flags & O_NONBLOCK) {
Ursula Braun50717a32019-04-12 12:57:23 +0200827 if (schedule_work(&smc->connect_work))
828 smc->connect_nonblock = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200829 rc = -EINPROGRESS;
830 } else {
Ursula Braun24ac3a02018-06-27 17:59:50 +0200831 rc = __smc_connect(smc);
832 if (rc < 0)
833 goto out;
834 else
835 rc = 0; /* success cases including fallback */
836 }
Ursula Braunac713872017-01-09 16:55:13 +0100837
838out:
839 release_sock(sk);
840out_err:
841 return rc;
842}
843
844static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
845{
Ursula Braun3163c502018-01-24 10:28:12 +0100846 struct socket *new_clcsock = NULL;
847 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100848 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -0800849 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +0100850
Ursula Braun3163c502018-01-24 10:28:12 +0100851 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100852 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100853 if (!new_sk) {
854 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100855 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100856 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100857 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100858 goto out;
859 }
860 *new_smc = smc_sk(new_sk);
861
Myungho Jung78abe3d2018-12-18 09:02:25 -0800862 mutex_lock(&lsmc->clcsock_release_lock);
863 if (lsmc->clcsock)
864 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
865 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +0100866 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100867 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100868 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100869 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Brauna046d572017-01-09 16:55:16 +0100870 if (new_clcsock)
871 sock_release(new_clcsock);
872 new_sk->sk_state = SMC_CLOSED;
873 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun3163c502018-01-24 10:28:12 +0100874 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100875 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100876 *new_smc = NULL;
877 goto out;
878 }
879
880 (*new_smc)->clcsock = new_clcsock;
881out:
882 return rc;
883}
884
Ursula Brauna046d572017-01-09 16:55:16 +0100885/* add a just created sock to the accept queue of the listen sock as
886 * candidate for a following socket accept call from user space
887 */
888static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
889{
890 struct smc_sock *par = smc_sk(parent);
891
Ursula Braun51f1de72018-01-26 09:28:48 +0100892 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100893 spin_lock(&par->accept_q_lock);
894 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
895 spin_unlock(&par->accept_q_lock);
896 sk_acceptq_added(parent);
897}
898
899/* remove a socket from the accept queue of its parental listening socket */
900static void smc_accept_unlink(struct sock *sk)
901{
902 struct smc_sock *par = smc_sk(sk)->listen_smc;
903
904 spin_lock(&par->accept_q_lock);
905 list_del_init(&smc_sk(sk)->accept_q);
906 spin_unlock(&par->accept_q_lock);
907 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100908 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100909}
910
911/* remove a sock from the accept queue to bind it to a new socket created
912 * for a socket accept call from user space
913 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100914struct sock *smc_accept_dequeue(struct sock *parent,
915 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100916{
917 struct smc_sock *isk, *n;
918 struct sock *new_sk;
919
920 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
921 new_sk = (struct sock *)isk;
922
923 smc_accept_unlink(new_sk);
924 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braun127f4972018-01-26 09:28:49 +0100925 if (isk->clcsock) {
926 sock_release(isk->clcsock);
927 isk->clcsock = NULL;
928 }
Ursula Braun288c83902017-04-10 14:58:04 +0200929 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100930 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +0100931 continue;
932 }
933 if (new_sock)
934 sock_graft(new_sk, new_sock);
935 return new_sk;
936 }
937 return NULL;
938}
939
940/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +0100941void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +0100942{
943 struct smc_sock *smc = smc_sk(sk);
944
Ursula Braunb38d7322017-01-09 16:55:25 +0100945 lock_sock(sk);
946 if (!sk->sk_lingertime)
947 /* wait for peer closing */
948 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100949 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100950 smc_close_active(smc);
Ursula Braun288c83902017-04-10 14:58:04 +0200951 sock_set_flag(sk, SOCK_DEAD);
952 sk->sk_shutdown |= SHUTDOWN_MASK;
953 }
Ursula Brauna046d572017-01-09 16:55:16 +0100954 if (smc->clcsock) {
955 struct socket *tcp;
956
957 tcp = smc->clcsock;
958 smc->clcsock = NULL;
959 sock_release(tcp);
960 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100961 if (smc->use_fallback) {
Ursula Braun51f1de72018-01-26 09:28:48 +0100962 sock_put(sk); /* passive closing */
963 sk->sk_state = SMC_CLOSED;
964 } else {
965 if (sk->sk_state == SMC_CLOSED)
966 smc_conn_free(&smc->conn);
Ursula Braunb38d7322017-01-09 16:55:25 +0100967 }
968 release_sock(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100969 sk->sk_prot->unhash(sk);
970 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +0100971}
972
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100973static int smc_serv_conf_first_link(struct smc_sock *smc)
974{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200975 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100976 struct smc_link_group *lgr = smc->conn.lgr;
977 struct smc_link *link;
978 int rest;
979 int rc;
980
981 link = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun652a1e42017-07-28 13:56:17 +0200982
Karsten Graul44aa81c2018-05-15 17:04:55 +0200983 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200984 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200985
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100986 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200987 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100988 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200989 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100990
991 /* receive CONFIRM LINK response from client over the RoCE fabric */
992 rest = wait_for_completion_interruptible_timeout(
993 &link->llc_confirm_resp,
994 SMC_LLC_WAIT_FIRST_TIME);
995 if (rest <= 0) {
996 struct smc_clc_msg_decline dclc;
997
998 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100999 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001000 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001001 }
1002
Karsten Graul75d320d2018-03-01 13:51:31 +01001003 if (link->llc_confirm_resp_rc)
1004 return SMC_CLC_DECL_RMBE_EC;
1005
Karsten Graul52bedf32018-03-01 13:51:32 +01001006 /* send ADD LINK request to client over the RoCE fabric */
1007 rc = smc_llc_send_add_link(link,
1008 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +02001009 link->gid, SMC_LLC_REQ);
Karsten Graul52bedf32018-03-01 13:51:32 +01001010 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001011 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +01001012
1013 /* receive ADD LINK response from client over the RoCE fabric */
1014 rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
1015 SMC_LLC_WAIT_TIME);
1016 if (rest <= 0) {
1017 struct smc_clc_msg_decline dclc;
1018
1019 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001020 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001021 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
Karsten Graul52bedf32018-03-01 13:51:32 +01001022 }
1023
Karsten Graul877ae5b2018-05-02 16:56:44 +02001024 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +01001025
Karsten Graul75d320d2018-03-01 13:51:31 +01001026 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001027}
1028
Hans Wippel3b2dec22018-05-18 09:34:18 +02001029/* listen worker: finish */
1030static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001031{
Ursula Brauna046d572017-01-09 16:55:16 +01001032 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001033 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001034
Ursula Braunb38d7322017-01-09 16:55:25 +01001035 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001036 if (lsmc->sk.sk_state == SMC_LISTEN) {
1037 smc_accept_enqueue(&lsmc->sk, newsmcsk);
1038 } else { /* no longer listening */
1039 smc_close_non_accepted(newsmcsk);
1040 }
1041 release_sock(&lsmc->sk);
1042
1043 /* Wake up accept */
1044 lsmc->sk.sk_data_ready(&lsmc->sk);
1045 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001046}
Ursula Brauna046d572017-01-09 16:55:16 +01001047
Hans Wippel3b2dec22018-05-18 09:34:18 +02001048/* listen worker: finish in state connected */
1049static void smc_listen_out_connected(struct smc_sock *new_smc)
1050{
1051 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001052
Hans Wippel3b2dec22018-05-18 09:34:18 +02001053 sk_refcnt_debug_inc(newsmcsk);
1054 if (newsmcsk->sk_state == SMC_INIT)
1055 newsmcsk->sk_state = SMC_ACTIVE;
1056
1057 smc_listen_out(new_smc);
1058}
1059
1060/* listen worker: finish in error state */
1061static void smc_listen_out_err(struct smc_sock *new_smc)
1062{
1063 struct sock *newsmcsk = &new_smc->sk;
1064
Ursula Braun51f1de72018-01-26 09:28:48 +01001065 if (newsmcsk->sk_state == SMC_INIT)
1066 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001067 newsmcsk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001068 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001069
1070 smc_listen_out(new_smc);
1071}
1072
1073/* listen worker: decline and fall back if possible */
1074static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1075 int local_contact)
1076{
1077 /* RDMA setup failed, switch back to TCP */
1078 if (local_contact == SMC_FIRST_CONTACT)
1079 smc_lgr_forget(new_smc->conn.lgr);
1080 if (reason_code < 0) { /* error, no fallback possible */
1081 smc_listen_out_err(new_smc);
1082 return;
1083 }
1084 smc_conn_free(&new_smc->conn);
1085 new_smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001086 new_smc->fallback_rsn = reason_code;
1087 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001088 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1089 smc_listen_out_err(new_smc);
1090 return;
1091 }
1092 }
1093 smc_listen_out_connected(new_smc);
1094}
1095
1096/* listen worker: check prefixes */
Karsten Graul59886692019-04-12 12:57:25 +02001097static int smc_listen_prfx_check(struct smc_sock *new_smc,
Hans Wippel3b2dec22018-05-18 09:34:18 +02001098 struct smc_clc_msg_proposal *pclc)
1099{
1100 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1101 struct socket *newclcsock = new_smc->clcsock;
1102
1103 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1104 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
Karsten Graul59886692019-04-12 12:57:25 +02001105 return SMC_CLC_DECL_DIFFPREFIX;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001106
1107 return 0;
1108}
1109
1110/* listen worker: initialize connection and buffers */
1111static int smc_listen_rdma_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001112 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001113{
Karsten Graul7a627252019-04-12 12:57:30 +02001114 int rc;
1115
Hans Wippel3b2dec22018-05-18 09:34:18 +02001116 /* allocate connection / link group */
Karsten Graul7a627252019-04-12 12:57:30 +02001117 rc = smc_conn_create(new_smc, ini);
1118 if (rc)
1119 return rc;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001120
1121 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001122 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001123 return SMC_CLC_DECL_MEM;
1124
1125 return 0;
1126}
1127
Hans Wippel41349842018-06-28 19:05:11 +02001128/* listen worker: initialize connection and buffers for SMC-D */
1129static int smc_listen_ism_init(struct smc_sock *new_smc,
1130 struct smc_clc_msg_proposal *pclc,
Karsten Graul7a627252019-04-12 12:57:30 +02001131 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +02001132{
1133 struct smc_clc_msg_smcd *pclc_smcd;
Karsten Graul7a627252019-04-12 12:57:30 +02001134 int rc;
Hans Wippel41349842018-06-28 19:05:11 +02001135
1136 pclc_smcd = smc_get_clc_msg_smcd(pclc);
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001137 ini->ism_gid = pclc_smcd->gid;
Karsten Graul7a627252019-04-12 12:57:30 +02001138 rc = smc_conn_create(new_smc, ini);
1139 if (rc)
1140 return rc;
Hans Wippel41349842018-06-28 19:05:11 +02001141
1142 /* Check if peer can be reached via ISM device */
1143 if (smc_ism_cantalk(new_smc->conn.lgr->peer_gid,
1144 new_smc->conn.lgr->vlan_id,
1145 new_smc->conn.lgr->smcd)) {
Karsten Graul7a627252019-04-12 12:57:30 +02001146 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Hans Wippel41349842018-06-28 19:05:11 +02001147 smc_lgr_forget(new_smc->conn.lgr);
1148 smc_conn_free(&new_smc->conn);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001149 return SMC_CLC_DECL_SMCDNOTALK;
Hans Wippel41349842018-06-28 19:05:11 +02001150 }
1151
1152 /* Create send and receive buffers */
1153 if (smc_buf_create(new_smc, true)) {
Karsten Graul7a627252019-04-12 12:57:30 +02001154 if (ini->cln_first_contact == SMC_FIRST_CONTACT)
Hans Wippel41349842018-06-28 19:05:11 +02001155 smc_lgr_forget(new_smc->conn.lgr);
1156 smc_conn_free(&new_smc->conn);
1157 return SMC_CLC_DECL_MEM;
1158 }
1159
1160 return 0;
1161}
1162
Hans Wippel3b2dec22018-05-18 09:34:18 +02001163/* listen worker: register buffers */
1164static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
1165{
1166 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1167
1168 if (local_contact != SMC_FIRST_CONTACT) {
Karsten Graulc7674c02018-11-22 10:26:43 +01001169 if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true))
1170 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001171 }
1172 smc_rmb_sync_sg_for_device(&new_smc->conn);
1173
1174 return 0;
1175}
1176
1177/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001178static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1179 struct smc_clc_msg_accept_confirm *cclc,
1180 int local_contact)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001181{
1182 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1183 int reason_code = 0;
1184
1185 if (local_contact == SMC_FIRST_CONTACT)
1186 smc_link_save_peer_info(link, cclc);
1187
1188 if (smc_rmb_rtoken_handling(&new_smc->conn, cclc)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001189 reason_code = SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001190 goto decline;
1191 }
1192
1193 if (local_contact == SMC_FIRST_CONTACT) {
1194 if (smc_ib_ready_link(link)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001195 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001196 goto decline;
1197 }
1198 /* QP confirmation over RoCE fabric */
1199 reason_code = smc_serv_conf_first_link(new_smc);
1200 if (reason_code)
1201 goto decline;
1202 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001203 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001204
1205decline:
Hans Wippel3b2dec22018-05-18 09:34:18 +02001206 smc_listen_decline(new_smc, reason_code, local_contact);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001207 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001208}
1209
1210/* setup for RDMA connection of server */
1211static void smc_listen_work(struct work_struct *work)
1212{
1213 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1214 smc_listen_work);
1215 struct socket *newclcsock = new_smc->clcsock;
1216 struct smc_clc_msg_accept_confirm cclc;
1217 struct smc_clc_msg_proposal *pclc;
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001218 struct smc_init_info ini = {0};
Hans Wippel41349842018-06-28 19:05:11 +02001219 bool ism_supported = false;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001220 u8 buf[SMC_CLC_MAX_LEN];
Hans Wippel3b2dec22018-05-18 09:34:18 +02001221 int rc = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001222
1223 if (new_smc->use_fallback) {
1224 smc_listen_out_connected(new_smc);
1225 return;
1226 }
1227
1228 /* check if peer is smc capable */
1229 if (!tcp_sk(newclcsock->sk)->syn_smc) {
1230 new_smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001231 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001232 smc_listen_out_connected(new_smc);
1233 return;
1234 }
1235
1236 /* do inband token exchange -
1237 * wait for and receive SMC Proposal CLC message
1238 */
1239 pclc = (struct smc_clc_msg_proposal *)&buf;
Karsten Graul228bae02019-04-12 12:57:28 +02001240 rc = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
1241 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001242 if (rc)
1243 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001244
1245 /* IPSec connections opt out of SMC-R optimizations */
1246 if (using_ipsec(new_smc)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001247 rc = SMC_CLC_DECL_IPSEC;
1248 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001249 }
1250
Karsten Graul59886692019-04-12 12:57:25 +02001251 /* check for matching IP prefix and subnet length */
1252 rc = smc_listen_prfx_check(new_smc, pclc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001253 if (rc)
1254 goto out_decl;
Karsten Graul59886692019-04-12 12:57:25 +02001255
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001256 /* get vlan id from IP device */
1257 if (smc_vlan_by_tcpsk(new_smc->clcsock, &ini)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001258 rc = SMC_CLC_DECL_GETVLANERR;
1259 goto out_decl;
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001260 }
1261
Hans Wippel72a36a82019-02-07 15:56:18 +01001262 mutex_lock(&smc_server_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001263 smc_close_init(new_smc);
1264 smc_rx_init(new_smc);
1265 smc_tx_init(new_smc);
1266
Hans Wippel41349842018-06-28 19:05:11 +02001267 /* check if ISM is available */
Karsten Graul9aa68d22019-04-12 12:57:29 +02001268 if (pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) {
1269 ini.is_smcd = true; /* prepare ISM check */
1270 rc = smc_find_ism_device(new_smc, &ini);
1271 if (!rc)
Karsten Graul7a627252019-04-12 12:57:30 +02001272 rc = smc_listen_ism_init(new_smc, pclc, &ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001273 if (!rc)
1274 ism_supported = true;
1275 else if (pclc->hdr.path == SMC_TYPE_D)
1276 goto out_unlock; /* skip RDMA and decline */
1277 }
1278
1279 /* check if RDMA is available */
1280 if (!ism_supported) { /* SMC_TYPE_R or SMC_TYPE_B */
Karsten Graulbc36d2f2019-04-12 12:57:26 +02001281 /* prepare RDMA check */
1282 memset(&ini, 0, sizeof(ini));
1283 ini.is_smcd = false;
1284 ini.ib_lcl = &pclc->lcl;
Karsten Graul9aa68d22019-04-12 12:57:29 +02001285 rc = smc_find_rdma_device(new_smc, &ini);
1286 if (rc) {
1287 /* no RDMA device found */
1288 if (pclc->hdr.path == SMC_TYPE_B)
1289 /* neither ISM nor RDMA device found */
1290 rc = SMC_CLC_DECL_NOSMCDEV;
1291 goto out_unlock;
1292 }
Karsten Graul7a627252019-04-12 12:57:30 +02001293 rc = smc_listen_rdma_init(new_smc, &ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001294 if (rc)
1295 goto out_unlock;
Karsten Graul7a627252019-04-12 12:57:30 +02001296 rc = smc_listen_rdma_reg(new_smc, ini.cln_first_contact);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001297 if (rc)
1298 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001299 }
1300
1301 /* send SMC Accept CLC message */
Karsten Graul7a627252019-04-12 12:57:30 +02001302 rc = smc_clc_send_accept(new_smc, ini.cln_first_contact);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001303 if (rc)
1304 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001305
Hans Wippel62c71392019-02-07 15:56:17 +01001306 /* SMC-D does not need this lock any more */
1307 if (ism_supported)
Hans Wippel72a36a82019-02-07 15:56:18 +01001308 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001309
Hans Wippel3b2dec22018-05-18 09:34:18 +02001310 /* receive SMC Confirm CLC message */
Karsten Graul228bae02019-04-12 12:57:28 +02001311 rc = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
1312 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
1313 if (rc) {
Hans Wippel62c71392019-02-07 15:56:17 +01001314 if (!ism_supported)
Karsten Graul9aa68d22019-04-12 12:57:29 +02001315 goto out_unlock;
1316 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001317 }
1318
1319 /* finish worker */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001320 if (!ism_supported) {
Karsten Graul7a627252019-04-12 12:57:30 +02001321 rc = smc_listen_rdma_finish(new_smc, &cclc,
1322 ini.cln_first_contact);
Hans Wippel72a36a82019-02-07 15:56:18 +01001323 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001324 if (rc)
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001325 return;
1326 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001327 smc_conn_save_peer_info(new_smc, &cclc);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001328 smc_listen_out_connected(new_smc);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001329 return;
1330
1331out_unlock:
1332 mutex_unlock(&smc_server_lgr_pending);
1333out_decl:
Karsten Graul7a627252019-04-12 12:57:30 +02001334 smc_listen_decline(new_smc, rc, ini.cln_first_contact);
Ursula Brauna046d572017-01-09 16:55:16 +01001335}
1336
1337static void smc_tcp_listen_work(struct work_struct *work)
1338{
1339 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1340 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001341 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001342 struct smc_sock *new_smc;
1343 int rc = 0;
1344
Ursula Braun3163c502018-01-24 10:28:12 +01001345 lock_sock(lsk);
1346 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001347 rc = smc_clcsock_accept(lsmc, &new_smc);
1348 if (rc)
1349 goto out;
1350 if (!new_smc)
1351 continue;
1352
1353 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001354 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001355 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001356 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001357 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1358 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001359 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1360 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001361 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1362 if (!schedule_work(&new_smc->smc_listen_work))
1363 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001364 }
1365
1366out:
Ursula Braun3163c502018-01-24 10:28:12 +01001367 release_sock(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001368 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +01001369}
1370
Ursula Braunac713872017-01-09 16:55:13 +01001371static int smc_listen(struct socket *sock, int backlog)
1372{
1373 struct sock *sk = sock->sk;
1374 struct smc_sock *smc;
1375 int rc;
1376
1377 smc = smc_sk(sk);
1378 lock_sock(sk);
1379
1380 rc = -EINVAL;
1381 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
1382 goto out;
1383
1384 rc = 0;
1385 if (sk->sk_state == SMC_LISTEN) {
1386 sk->sk_max_ack_backlog = backlog;
1387 goto out;
1388 }
1389 /* some socket options are handled in core, so we could not apply
1390 * them to the clc socket -- copy smc socket options to clc socket
1391 */
1392 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001393 if (!smc->use_fallback)
1394 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001395
1396 rc = kernel_listen(smc->clcsock, backlog);
1397 if (rc)
1398 goto out;
1399 sk->sk_max_ack_backlog = backlog;
1400 sk->sk_ack_backlog = 0;
1401 sk->sk_state = SMC_LISTEN;
Ursula Braun51f1de72018-01-26 09:28:48 +01001402 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1403 if (!schedule_work(&smc->tcp_listen_work))
1404 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001405
1406out:
1407 release_sock(sk);
1408 return rc;
1409}
1410
1411static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001412 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001413{
Ursula Brauna046d572017-01-09 16:55:16 +01001414 struct sock *sk = sock->sk, *nsk;
1415 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001416 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001417 long timeo;
1418 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001419
1420 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001421 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001422 lock_sock(sk);
1423
1424 if (lsmc->sk.sk_state != SMC_LISTEN) {
1425 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001426 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001427 goto out;
1428 }
1429
Ursula Brauna046d572017-01-09 16:55:16 +01001430 /* Wait for an incoming connection */
1431 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1432 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1433 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1434 set_current_state(TASK_INTERRUPTIBLE);
1435 if (!timeo) {
1436 rc = -EAGAIN;
1437 break;
1438 }
1439 release_sock(sk);
1440 timeo = schedule_timeout(timeo);
1441 /* wakeup by sk_data_ready in smc_listen_work() */
1442 sched_annotate_sleep();
1443 lock_sock(sk);
1444 if (signal_pending(current)) {
1445 rc = sock_intr_errno(timeo);
1446 break;
1447 }
1448 }
1449 set_current_state(TASK_RUNNING);
1450 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001451
Ursula Brauna046d572017-01-09 16:55:16 +01001452 if (!rc)
1453 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001454 release_sock(sk);
1455 if (rc)
1456 goto out;
1457
1458 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1459 /* wait till data arrives on the socket */
1460 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1461 MSEC_PER_SEC);
1462 if (smc_sk(nsk)->use_fallback) {
1463 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1464
1465 lock_sock(clcsk);
1466 if (skb_queue_empty(&clcsk->sk_receive_queue))
1467 sk_wait_data(clcsk, &timeo, NULL);
1468 release_sock(clcsk);
1469 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1470 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001471 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001472 release_sock(nsk);
1473 }
1474 }
Ursula Braunac713872017-01-09 16:55:13 +01001475
1476out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001477 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001478 return rc;
1479}
1480
1481static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001482 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001483{
1484 struct smc_sock *smc;
1485
Ursula Braunb38d7322017-01-09 16:55:25 +01001486 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1487 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001488 return -ENOTCONN;
1489
1490 smc = smc_sk(sock->sk);
1491
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001492 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001493}
1494
1495static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1496{
1497 struct sock *sk = sock->sk;
1498 struct smc_sock *smc;
1499 int rc = -EPIPE;
1500
1501 smc = smc_sk(sk);
1502 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001503 if ((sk->sk_state != SMC_ACTIVE) &&
1504 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1505 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001506 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001507
1508 if (msg->msg_flags & MSG_FASTOPEN) {
1509 if (sk->sk_state == SMC_INIT) {
1510 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001511 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001512 } else {
1513 rc = -EINVAL;
1514 goto out;
1515 }
1516 }
1517
Ursula Braunac713872017-01-09 16:55:13 +01001518 if (smc->use_fallback)
1519 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1520 else
Ursula Braune6727f32017-01-09 16:55:23 +01001521 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001522out:
1523 release_sock(sk);
1524 return rc;
1525}
1526
1527static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1528 int flags)
1529{
1530 struct sock *sk = sock->sk;
1531 struct smc_sock *smc;
1532 int rc = -ENOTCONN;
1533
1534 smc = smc_sk(sk);
1535 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001536 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1537 /* socket was connected before, no more data to read */
1538 rc = 0;
1539 goto out;
1540 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001541 if ((sk->sk_state == SMC_INIT) ||
1542 (sk->sk_state == SMC_LISTEN) ||
1543 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001544 goto out;
1545
Ursula Braunb38d7322017-01-09 16:55:25 +01001546 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1547 rc = 0;
1548 goto out;
1549 }
1550
Stefan Raspl9014db22018-05-03 18:12:39 +02001551 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001552 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001553 } else {
1554 msg->msg_namelen = 0;
1555 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1556 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001557
Ursula Braunac713872017-01-09 16:55:13 +01001558out:
1559 release_sock(sk);
1560 return rc;
1561}
1562
Al Viroade994f2017-07-03 00:01:49 -04001563static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001564{
Ursula Braun8dce2782018-01-26 09:28:47 +01001565 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001566 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001567
Ursula Braun8dce2782018-01-26 09:28:47 +01001568 spin_lock(&isk->accept_q_lock);
1569 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001570 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001571 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001572
Ursula Braun8dce2782018-01-26 09:28:47 +01001573 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001574}
1575
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001576static __poll_t smc_poll(struct file *file, struct socket *sock,
1577 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01001578{
1579 struct sock *sk = sock->sk;
Ursula Braunac713872017-01-09 16:55:13 +01001580 struct smc_sock *smc;
Ursula Braun50717a32019-04-12 12:57:23 +02001581 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001582
Ursula Braun8dce2782018-01-26 09:28:47 +01001583 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001584 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001585
Ursula Braunac713872017-01-09 16:55:13 +01001586 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02001587 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01001588 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001589 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02001590 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braunac713872017-01-09 16:55:13 +01001591 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07001592 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02001593 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01001594 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001595 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001596 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1597 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001598 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001599 if (sk->sk_state == SMC_LISTEN) {
1600 /* woken up by sk_data_ready in smc_listen_work() */
Ursula Braun50717a32019-04-12 12:57:23 +02001601 mask |= smc_accept_poll(sk);
1602 } else if (smc->use_fallback) { /* as result of connect_work()*/
1603 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
1604 wait);
1605 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun8dce2782018-01-26 09:28:47 +01001606 } else {
Ursula Braun50717a32019-04-12 12:57:23 +02001607 if ((sk->sk_state != SMC_INIT &&
1608 atomic_read(&smc->conn.sndbuf_space)) ||
Ursula Braun8dce2782018-01-26 09:28:47 +01001609 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001610 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001611 } else {
1612 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1613 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1614 }
1615 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001616 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001617 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001618 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001619 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001620 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02001621 if (smc->conn.urg_state == SMC_URG_VALID)
1622 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01001623 }
Ursula Braunac713872017-01-09 16:55:13 +01001624 }
1625
1626 return mask;
1627}
1628
1629static int smc_shutdown(struct socket *sock, int how)
1630{
1631 struct sock *sk = sock->sk;
1632 struct smc_sock *smc;
1633 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001634 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001635
1636 smc = smc_sk(sk);
1637
1638 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001639 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001640
1641 lock_sock(sk);
1642
1643 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02001644 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01001645 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1646 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1647 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1648 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1649 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001650 goto out;
1651 if (smc->use_fallback) {
1652 rc = kernel_sock_shutdown(smc->clcsock, how);
1653 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1654 if (sk->sk_shutdown == SHUTDOWN_MASK)
1655 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001656 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001657 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001658 switch (how) {
1659 case SHUT_RDWR: /* shutdown in both directions */
1660 rc = smc_close_active(smc);
1661 break;
1662 case SHUT_WR:
1663 rc = smc_close_shutdown_write(smc);
1664 break;
1665 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001666 rc = 0;
1667 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001668 break;
1669 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001670 if (smc->clcsock)
1671 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001672 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1673 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001674
1675out:
1676 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001677 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001678}
1679
1680static int smc_setsockopt(struct socket *sock, int level, int optname,
1681 char __user *optval, unsigned int optlen)
1682{
1683 struct sock *sk = sock->sk;
1684 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001685 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001686
1687 smc = smc_sk(sk);
1688
1689 /* generic setsockopts reaching us here always apply to the
1690 * CLC socket
1691 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001692 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1693 optval, optlen);
1694 if (smc->clcsock->sk->sk_err) {
1695 sk->sk_err = smc->clcsock->sk->sk_err;
1696 sk->sk_error_report(sk);
1697 }
1698 if (rc)
1699 return rc;
1700
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001701 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00001702 return -EINVAL;
Ursula Braunac0107e2018-07-18 15:22:50 +02001703 if (get_user(val, (int __user *)optval))
1704 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001705
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001706 lock_sock(sk);
1707 switch (optname) {
1708 case TCP_ULP:
1709 case TCP_FASTOPEN:
1710 case TCP_FASTOPEN_CONNECT:
1711 case TCP_FASTOPEN_KEY:
1712 case TCP_FASTOPEN_NO_COOKIE:
1713 /* option not supported by SMC */
1714 if (sk->sk_state == SMC_INIT) {
1715 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001716 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001717 } else {
1718 if (!smc->use_fallback)
1719 rc = -EINVAL;
1720 }
1721 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001722 case TCP_NODELAY:
1723 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001724 if (val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001725 mod_delayed_work(system_wq, &smc->conn.tx_work,
1726 0);
1727 }
1728 break;
1729 case TCP_CORK:
1730 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001731 if (!val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001732 mod_delayed_work(system_wq, &smc->conn.tx_work,
1733 0);
1734 }
1735 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001736 case TCP_DEFER_ACCEPT:
1737 smc->sockopt_defer_accept = val;
1738 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001739 default:
1740 break;
1741 }
1742 release_sock(sk);
1743
1744 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001745}
1746
1747static int smc_getsockopt(struct socket *sock, int level, int optname,
1748 char __user *optval, int __user *optlen)
1749{
1750 struct smc_sock *smc;
1751
1752 smc = smc_sk(sock->sk);
1753 /* socket options apply to the CLC socket */
1754 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1755 optval, optlen);
1756}
1757
1758static int smc_ioctl(struct socket *sock, unsigned int cmd,
1759 unsigned long arg)
1760{
Stefan Rasplde8474e2018-05-23 16:38:11 +02001761 union smc_host_cursor cons, urg;
1762 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01001763 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001764 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001765
1766 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001767 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02001768 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02001769 if (smc->use_fallback) {
1770 if (!smc->clcsock) {
1771 release_sock(&smc->sk);
1772 return -EBADF;
1773 }
1774 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1775 release_sock(&smc->sk);
1776 return answ;
1777 }
Ursula Braun9b67e262018-05-02 16:56:46 +02001778 switch (cmd) {
1779 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02001780 if (smc->sk.sk_state == SMC_LISTEN) {
1781 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001782 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001783 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001784 if (smc->sk.sk_state == SMC_INIT ||
1785 smc->sk.sk_state == SMC_CLOSED)
1786 answ = 0;
1787 else
1788 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02001789 break;
1790 case SIOCOUTQ:
1791 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02001792 if (smc->sk.sk_state == SMC_LISTEN) {
1793 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001794 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001795 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001796 if (smc->sk.sk_state == SMC_INIT ||
1797 smc->sk.sk_state == SMC_CLOSED)
1798 answ = 0;
1799 else
1800 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02001801 atomic_read(&smc->conn.sndbuf_space);
1802 break;
1803 case SIOCOUTQNSD:
1804 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02001805 if (smc->sk.sk_state == SMC_LISTEN) {
1806 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001807 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001808 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001809 if (smc->sk.sk_state == SMC_INIT ||
1810 smc->sk.sk_state == SMC_CLOSED)
1811 answ = 0;
1812 else
1813 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02001814 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02001815 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02001816 if (smc->sk.sk_state == SMC_LISTEN) {
1817 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001818 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001819 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001820 if (smc->sk.sk_state == SMC_INIT ||
1821 smc->sk.sk_state == SMC_CLOSED) {
1822 answ = 0;
1823 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02001824 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1825 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001826 answ = smc_curs_diff(conn->rmb_desc->len,
1827 &cons, &urg) == 1;
1828 }
1829 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02001830 default:
Ursula Braun1992d992018-07-16 13:56:52 +02001831 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001832 return -ENOIOCTLCMD;
1833 }
Ursula Braun1992d992018-07-16 13:56:52 +02001834 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001835
1836 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001837}
1838
1839static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1840 int offset, size_t size, int flags)
1841{
1842 struct sock *sk = sock->sk;
1843 struct smc_sock *smc;
1844 int rc = -EPIPE;
1845
1846 smc = smc_sk(sk);
1847 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001848 if (sk->sk_state != SMC_ACTIVE) {
1849 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001850 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001851 }
1852 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001853 if (smc->use_fallback)
1854 rc = kernel_sendpage(smc->clcsock, page, offset,
1855 size, flags);
1856 else
1857 rc = sock_no_sendpage(sock, page, offset, size, flags);
1858
1859out:
Ursula Braunac713872017-01-09 16:55:13 +01001860 return rc;
1861}
1862
Stefan Raspl9014db22018-05-03 18:12:39 +02001863/* Map the affected portions of the rmbe into an spd, note the number of bytes
1864 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1865 * updates till whenever a respective page has been fully processed.
1866 * Note that subsequent recv() calls have to wait till all splice() processing
1867 * completed.
1868 */
Ursula Braunac713872017-01-09 16:55:13 +01001869static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1870 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001871 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001872{
1873 struct sock *sk = sock->sk;
1874 struct smc_sock *smc;
1875 int rc = -ENOTCONN;
1876
1877 smc = smc_sk(sk);
1878 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001879 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1880 /* socket was connected before, no more data to read */
1881 rc = 0;
1882 goto out;
1883 }
Stefan Raspl9014db22018-05-03 18:12:39 +02001884 if (sk->sk_state == SMC_INIT ||
1885 sk->sk_state == SMC_LISTEN ||
1886 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001887 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001888
1889 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1890 rc = 0;
1891 goto out;
1892 }
1893
Ursula Braunac713872017-01-09 16:55:13 +01001894 if (smc->use_fallback) {
1895 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1896 pipe, len, flags);
1897 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001898 if (*ppos) {
1899 rc = -ESPIPE;
1900 goto out;
1901 }
1902 if (flags & SPLICE_F_NONBLOCK)
1903 flags = MSG_DONTWAIT;
1904 else
1905 flags = 0;
1906 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001907 }
1908out:
1909 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001910
Ursula Braunac713872017-01-09 16:55:13 +01001911 return rc;
1912}
1913
1914/* must look like tcp */
1915static const struct proto_ops smc_sock_ops = {
1916 .family = PF_SMC,
1917 .owner = THIS_MODULE,
1918 .release = smc_release,
1919 .bind = smc_bind,
1920 .connect = smc_connect,
1921 .socketpair = sock_no_socketpair,
1922 .accept = smc_accept,
1923 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001924 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01001925 .ioctl = smc_ioctl,
1926 .listen = smc_listen,
1927 .shutdown = smc_shutdown,
1928 .setsockopt = smc_setsockopt,
1929 .getsockopt = smc_getsockopt,
1930 .sendmsg = smc_sendmsg,
1931 .recvmsg = smc_recvmsg,
1932 .mmap = sock_no_mmap,
1933 .sendpage = smc_sendpage,
1934 .splice_read = smc_splice_read,
1935};
1936
1937static int smc_create(struct net *net, struct socket *sock, int protocol,
1938 int kern)
1939{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001940 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001941 struct smc_sock *smc;
1942 struct sock *sk;
1943 int rc;
1944
1945 rc = -ESOCKTNOSUPPORT;
1946 if (sock->type != SOCK_STREAM)
1947 goto out;
1948
1949 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001950 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01001951 goto out;
1952
1953 rc = -ENOBUFS;
1954 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001955 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001956 if (!sk)
1957 goto out;
1958
1959 /* create internal TCP socket for CLC handshake and fallback */
1960 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001961 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02001962 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001963 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1964 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01001965 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01001966 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01001967 goto out;
1968 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01001969 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1970 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01001971
1972out:
1973 return rc;
1974}
1975
1976static const struct net_proto_family smc_sock_family_ops = {
1977 .family = PF_SMC,
1978 .owner = THIS_MODULE,
1979 .create = smc_create,
1980};
1981
Hans Wippel64e28b52019-02-21 13:01:02 +01001982unsigned int smc_net_id;
1983
1984static __net_init int smc_net_init(struct net *net)
1985{
1986 return smc_pnet_net_init(net);
1987}
1988
1989static void __net_exit smc_net_exit(struct net *net)
1990{
1991 smc_pnet_net_exit(net);
1992}
1993
1994static struct pernet_operations smc_net_ops = {
1995 .init = smc_net_init,
1996 .exit = smc_net_exit,
1997 .id = &smc_net_id,
1998 .size = sizeof(struct smc_net),
1999};
2000
Ursula Braunac713872017-01-09 16:55:13 +01002001static int __init smc_init(void)
2002{
2003 int rc;
2004
Hans Wippel64e28b52019-02-21 13:01:02 +01002005 rc = register_pernet_subsys(&smc_net_ops);
2006 if (rc)
2007 return rc;
2008
Thomas Richter6812baa2017-01-09 16:55:15 +01002009 rc = smc_pnet_init();
2010 if (rc)
2011 return rc;
2012
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002013 rc = smc_llc_init();
2014 if (rc) {
2015 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
2016 goto out_pnet;
2017 }
2018
Ursula Braun5f083182017-01-09 16:55:22 +01002019 rc = smc_cdc_init();
2020 if (rc) {
2021 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
2022 goto out_pnet;
2023 }
2024
Ursula Braunac713872017-01-09 16:55:13 +01002025 rc = proto_register(&smc_proto, 1);
2026 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01002027 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Thomas Richter6812baa2017-01-09 16:55:15 +01002028 goto out_pnet;
Ursula Braunac713872017-01-09 16:55:13 +01002029 }
2030
Karsten Graulaaa4d332018-03-16 15:06:41 +01002031 rc = proto_register(&smc_proto6, 1);
2032 if (rc) {
2033 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2034 goto out_proto;
2035 }
2036
Ursula Braunac713872017-01-09 16:55:13 +01002037 rc = sock_register(&smc_sock_family_ops);
2038 if (rc) {
2039 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002040 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01002041 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01002042 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002043 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01002044
Ursula Brauna4cf0442017-01-09 16:55:14 +01002045 rc = smc_ib_register_client();
2046 if (rc) {
2047 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2048 goto out_sock;
2049 }
2050
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002051 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002052 return 0;
2053
Ursula Brauna4cf0442017-01-09 16:55:14 +01002054out_sock:
2055 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002056out_proto6:
2057 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002058out_proto:
2059 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002060out_pnet:
2061 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01002062 return rc;
2063}
2064
2065static void __exit smc_exit(void)
2066{
Hans Wippel9fda3512018-05-18 09:34:11 +02002067 smc_core_exit();
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002068 static_branch_disable(&tcp_have_smc);
Ursula Brauna4cf0442017-01-09 16:55:14 +01002069 smc_ib_unregister_client();
Ursula Braunac713872017-01-09 16:55:13 +01002070 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002071 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002072 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002073 smc_pnet_exit();
Hans Wippel64e28b52019-02-21 13:01:02 +01002074 unregister_pernet_subsys(&smc_net_ops);
Ursula Braunac713872017-01-09 16:55:13 +01002075}
2076
2077module_init(smc_init);
2078module_exit(smc_exit);
2079
2080MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2081MODULE_DESCRIPTION("smc socket address family");
2082MODULE_LICENSE("GPL");
2083MODULE_ALIAS_NETPROTO(PF_SMC);