blob: 267e6837911068000fbce7edbd48919c1b54304c [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:
10 * - non-blocking connect postponed
11 * - IPv6 support postponed
12 * - support for alternate links postponed
13 * - partial support for non-blocking sockets only
14 * - support for urgent data postponed
15 *
Ursula Braunac713872017-01-09 16:55:13 +010016 * Copyright IBM Corp. 2016
17 *
18 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
19 * based on prototype from Frank Blaschka
20 */
21
22#define KMSG_COMPONENT "smc"
23#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
24
25#include <linux/module.h>
26#include <linux/socket.h>
Ursula Brauna046d572017-01-09 16:55:16 +010027#include <linux/inetdevice.h>
28#include <linux/workqueue.h>
Ursula Braun5f083182017-01-09 16:55:22 +010029#include <linux/in.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010030#include <linux/sched/signal.h>
31
Ursula Braunac713872017-01-09 16:55:13 +010032#include <net/sock.h>
Ursula Brauna046d572017-01-09 16:55:16 +010033#include <net/tcp.h>
Ursula Braunf16a7dd2017-01-09 16:55:26 +010034#include <net/smc.h>
Ursula Braunac713872017-01-09 16:55:13 +010035
36#include "smc.h"
Ursula Brauna046d572017-01-09 16:55:16 +010037#include "smc_clc.h"
Ursula Braun9bf9abe2017-01-09 16:55:21 +010038#include "smc_llc.h"
Ursula Braun5f083182017-01-09 16:55:22 +010039#include "smc_cdc.h"
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010040#include "smc_core.h"
Ursula Brauna4cf0442017-01-09 16:55:14 +010041#include "smc_ib.h"
Thomas Richter6812baa2017-01-09 16:55:15 +010042#include "smc_pnet.h"
Ursula Braune6727f32017-01-09 16:55:23 +010043#include "smc_tx.h"
Ursula Braun952310c2017-01-09 16:55:24 +010044#include "smc_rx.h"
Ursula Braunb38d7322017-01-09 16:55:25 +010045#include "smc_close.h"
Ursula Braunac713872017-01-09 16:55:13 +010046
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010047static DEFINE_MUTEX(smc_create_lgr_pending); /* serialize link group
48 * creation
49 */
50
51struct smc_lgr_list smc_lgr_list = { /* established link groups */
52 .lock = __SPIN_LOCK_UNLOCKED(smc_lgr_list.lock),
53 .list = LIST_HEAD_INIT(smc_lgr_list.list),
54};
55
Ursula Brauna046d572017-01-09 16:55:16 +010056static void smc_tcp_listen_work(struct work_struct *);
57
Ursula Braunac713872017-01-09 16:55:13 +010058static void smc_set_keepalive(struct sock *sk, int val)
59{
60 struct smc_sock *smc = smc_sk(sk);
61
62 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
63}
64
Ursula Braunf16a7dd2017-01-09 16:55:26 +010065static struct smc_hashinfo smc_v4_hashinfo = {
66 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
67};
68
69int smc_hash_sk(struct sock *sk)
70{
71 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
72 struct hlist_head *head;
73
74 head = &h->ht;
75
76 write_lock_bh(&h->lock);
77 sk_add_node(sk, head);
78 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
79 write_unlock_bh(&h->lock);
80
81 return 0;
82}
83EXPORT_SYMBOL_GPL(smc_hash_sk);
84
85void smc_unhash_sk(struct sock *sk)
86{
87 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
88
89 write_lock_bh(&h->lock);
90 if (sk_del_node_init(sk))
91 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
92 write_unlock_bh(&h->lock);
93}
94EXPORT_SYMBOL_GPL(smc_unhash_sk);
95
96struct proto smc_proto = {
Ursula Braunac713872017-01-09 16:55:13 +010097 .name = "SMC",
98 .owner = THIS_MODULE,
99 .keepalive = smc_set_keepalive,
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100100 .hash = smc_hash_sk,
101 .unhash = smc_unhash_sk,
Ursula Braunac713872017-01-09 16:55:13 +0100102 .obj_size = sizeof(struct smc_sock),
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100103 .h.smc_hash = &smc_v4_hashinfo,
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800104 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Ursula Braunac713872017-01-09 16:55:13 +0100105};
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100106EXPORT_SYMBOL_GPL(smc_proto);
Ursula Braunac713872017-01-09 16:55:13 +0100107
108static int smc_release(struct socket *sock)
109{
110 struct sock *sk = sock->sk;
111 struct smc_sock *smc;
Ursula Braunb38d7322017-01-09 16:55:25 +0100112 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +0100113
114 if (!sk)
115 goto out;
116
117 smc = smc_sk(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100118 if (sk->sk_state == SMC_LISTEN)
119 /* smc_close_non_accepted() is called and acquires
120 * sock lock for child sockets again
121 */
122 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
123 else
124 lock_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +0100125
Ursula Braun51f1de72018-01-26 09:28:48 +0100126 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100127 rc = smc_close_active(smc);
128 sock_set_flag(sk, SOCK_DEAD);
129 sk->sk_shutdown |= SHUTDOWN_MASK;
130 }
Ursula Braunac713872017-01-09 16:55:13 +0100131 if (smc->clcsock) {
132 sock_release(smc->clcsock);
133 smc->clcsock = NULL;
134 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100135 if (smc->use_fallback) {
136 sock_put(sk); /* passive closing */
137 sk->sk_state = SMC_CLOSED;
138 sk->sk_state_change(sk);
139 }
Ursula Braunac713872017-01-09 16:55:13 +0100140
141 /* detach socket */
142 sock_orphan(sk);
143 sock->sk = NULL;
Ursula Braun51f1de72018-01-26 09:28:48 +0100144 if (!smc->use_fallback && sk->sk_state == SMC_CLOSED)
Ursula Braunb38d7322017-01-09 16:55:25 +0100145 smc_conn_free(&smc->conn);
Ursula Braunac713872017-01-09 16:55:13 +0100146 release_sock(sk);
147
Ursula Braun51f1de72018-01-26 09:28:48 +0100148 sk->sk_prot->unhash(sk);
149 sock_put(sk); /* final sock_put */
Ursula Braunac713872017-01-09 16:55:13 +0100150out:
Ursula Braunb38d7322017-01-09 16:55:25 +0100151 return rc;
Ursula Braunac713872017-01-09 16:55:13 +0100152}
153
154static void smc_destruct(struct sock *sk)
155{
156 if (sk->sk_state != SMC_CLOSED)
157 return;
158 if (!sock_flag(sk, SOCK_DEAD))
159 return;
160
161 sk_refcnt_debug_dec(sk);
162}
163
164static struct sock *smc_sock_alloc(struct net *net, struct socket *sock)
165{
166 struct smc_sock *smc;
167 struct sock *sk;
168
169 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, &smc_proto, 0);
170 if (!sk)
171 return NULL;
172
173 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
174 sk->sk_state = SMC_INIT;
175 sk->sk_destruct = smc_destruct;
176 sk->sk_protocol = SMCPROTO_SMC;
Ursula Braunac713872017-01-09 16:55:13 +0100177 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100178 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
179 INIT_LIST_HEAD(&smc->accept_q);
180 spin_lock_init(&smc->accept_q_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100181 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100182 sk_refcnt_debug_inc(sk);
Ursula Braunac713872017-01-09 16:55:13 +0100183
184 return sk;
185}
186
187static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
188 int addr_len)
189{
190 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
191 struct sock *sk = sock->sk;
192 struct smc_sock *smc;
193 int rc;
194
195 smc = smc_sk(sk);
196
197 /* replicate tests from inet_bind(), to be safe wrt. future changes */
198 rc = -EINVAL;
199 if (addr_len < sizeof(struct sockaddr_in))
200 goto out;
201
202 rc = -EAFNOSUPPORT;
203 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
204 if ((addr->sin_family != AF_INET) &&
205 ((addr->sin_family != AF_UNSPEC) ||
206 (addr->sin_addr.s_addr != htonl(INADDR_ANY))))
207 goto out;
208
209 lock_sock(sk);
210
211 /* Check if socket is already active */
212 rc = -EINVAL;
213 if (sk->sk_state != SMC_INIT)
214 goto out_rel;
215
216 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
217 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
218
219out_rel:
220 release_sock(sk);
221out:
222 return rc;
223}
224
225static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
226 unsigned long mask)
227{
228 /* options we don't get control via setsockopt for */
229 nsk->sk_type = osk->sk_type;
230 nsk->sk_sndbuf = osk->sk_sndbuf;
231 nsk->sk_rcvbuf = osk->sk_rcvbuf;
232 nsk->sk_sndtimeo = osk->sk_sndtimeo;
233 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
234 nsk->sk_mark = osk->sk_mark;
235 nsk->sk_priority = osk->sk_priority;
236 nsk->sk_rcvlowat = osk->sk_rcvlowat;
237 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
238 nsk->sk_err = osk->sk_err;
239
240 nsk->sk_flags &= ~mask;
241 nsk->sk_flags |= osk->sk_flags & mask;
242}
243
244#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
245 (1UL << SOCK_KEEPOPEN) | \
246 (1UL << SOCK_LINGER) | \
247 (1UL << SOCK_BROADCAST) | \
248 (1UL << SOCK_TIMESTAMP) | \
249 (1UL << SOCK_DBG) | \
250 (1UL << SOCK_RCVTSTAMP) | \
251 (1UL << SOCK_RCVTSTAMPNS) | \
252 (1UL << SOCK_LOCALROUTE) | \
253 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
254 (1UL << SOCK_RXQ_OVFL) | \
255 (1UL << SOCK_WIFI_STATUS) | \
256 (1UL << SOCK_NOFCS) | \
257 (1UL << SOCK_FILTER_LOCKED))
258/* copy only relevant settings and flags of SOL_SOCKET level from smc to
259 * clc socket (since smc is not called for these options from net/core)
260 */
261static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
262{
263 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
264}
265
266#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
267 (1UL << SOCK_KEEPOPEN) | \
268 (1UL << SOCK_LINGER) | \
269 (1UL << SOCK_DBG))
270/* copy only settings and flags relevant for smc from clc to smc socket */
271static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
272{
273 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
274}
275
Ursula Brauna046d572017-01-09 16:55:16 +0100276/* determine subnet and mask of internal TCP socket */
277int smc_netinfo_by_tcpsk(struct socket *clcsock,
278 __be32 *subnet, u8 *prefix_len)
279{
280 struct dst_entry *dst = sk_dst_get(clcsock->sk);
Ursula Braun731b0082017-09-21 09:16:28 +0200281 struct in_device *in_dev;
Ursula Brauna046d572017-01-09 16:55:16 +0100282 struct sockaddr_in addr;
283 int rc = -ENOENT;
284 int len;
285
286 if (!dst) {
287 rc = -ENOTCONN;
288 goto out;
289 }
290 if (!dst->dev) {
291 rc = -ENODEV;
292 goto out_rel;
293 }
294
295 /* get address to which the internal TCP socket is bound */
296 kernel_getsockname(clcsock, (struct sockaddr *)&addr, &len);
297 /* analyze IPv4 specific data of net_device belonging to TCP socket */
Ursula Braun731b0082017-09-21 09:16:28 +0200298 rcu_read_lock();
299 in_dev = __in_dev_get_rcu(dst->dev);
300 for_ifa(in_dev) {
301 if (!inet_ifa_match(addr.sin_addr.s_addr, ifa))
Ursula Brauna046d572017-01-09 16:55:16 +0100302 continue;
303 *prefix_len = inet_mask_len(ifa->ifa_mask);
304 *subnet = ifa->ifa_address & ifa->ifa_mask;
305 rc = 0;
306 break;
Ursula Braun731b0082017-09-21 09:16:28 +0200307 } endfor_ifa(in_dev);
308 rcu_read_unlock();
Ursula Brauna046d572017-01-09 16:55:16 +0100309
310out_rel:
311 dst_release(dst);
312out:
313 return rc;
314}
315
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100316static int smc_clnt_conf_first_link(struct smc_sock *smc, union ib_gid *gid)
317{
318 struct smc_link_group *lgr = smc->conn.lgr;
319 struct smc_link *link;
320 int rest;
321 int rc;
322
323 link = &lgr->lnk[SMC_SINGLE_LINK];
324 /* receive CONFIRM LINK request from server over RoCE fabric */
325 rest = wait_for_completion_interruptible_timeout(
326 &link->llc_confirm,
327 SMC_LLC_WAIT_FIRST_TIME);
328 if (rest <= 0) {
329 struct smc_clc_msg_decline dclc;
330
331 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
332 SMC_CLC_DECLINE);
333 return rc;
334 }
335
336 rc = smc_ib_modify_qp_rts(link);
337 if (rc)
338 return SMC_CLC_DECL_INTERR;
339
340 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200341
342 rc = smc_wr_reg_send(link,
343 smc->conn.rmb_desc->mr_rx[SMC_SINGLE_LINK]);
344 if (rc)
345 return SMC_CLC_DECL_INTERR;
346
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100347 /* send CONFIRM LINK response over RoCE fabric */
348 rc = smc_llc_send_confirm_link(link,
349 link->smcibdev->mac[link->ibport - 1],
350 gid, SMC_LLC_RESP);
351 if (rc < 0)
352 return SMC_CLC_DECL_TCL;
353
354 return rc;
355}
356
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100357static void smc_conn_save_peer_info(struct smc_sock *smc,
358 struct smc_clc_msg_accept_confirm *clc)
359{
360 smc->conn.peer_conn_idx = clc->conn_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100361 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100362 smc->conn.peer_rmbe_size = smc_uncompress_bufsize(clc->rmbe_size);
363 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100364}
365
366static void smc_link_save_peer_info(struct smc_link *link,
367 struct smc_clc_msg_accept_confirm *clc)
368{
369 link->peer_qpn = ntoh24(clc->qpn);
370 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
371 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
372 link->peer_psn = ntoh24(clc->psn);
373 link->peer_mtu = clc->qp_mtu;
374}
375
Ursula Braun610db662018-01-25 11:15:34 +0100376static void smc_lgr_forget(struct smc_link_group *lgr)
377{
378 spin_lock_bh(&smc_lgr_list.lock);
379 /* do not use this link group for new connections */
380 if (!list_empty(&lgr->list))
381 list_del_init(&lgr->list);
382 spin_unlock_bh(&smc_lgr_list.lock);
383}
384
Ursula Brauna046d572017-01-09 16:55:16 +0100385/* setup for RDMA connection of client */
386static int smc_connect_rdma(struct smc_sock *smc)
387{
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100388 struct sockaddr_in *inaddr = (struct sockaddr_in *)smc->addr;
Ursula Brauna046d572017-01-09 16:55:16 +0100389 struct smc_clc_msg_accept_confirm aclc;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100390 int local_contact = SMC_FIRST_CONTACT;
Ursula Brauna046d572017-01-09 16:55:16 +0100391 struct smc_ib_device *smcibdev;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100392 struct smc_link *link;
393 u8 srv_first_contact;
Ursula Brauna046d572017-01-09 16:55:16 +0100394 int reason_code = 0;
395 int rc = 0;
396 u8 ibport;
397
Ursula Braun51f1de72018-01-26 09:28:48 +0100398 sock_hold(&smc->sk); /* sock put in passive closing */
399
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200400 if (!tcp_sk(smc->clcsock->sk)->syn_smc) {
401 /* peer has not signalled SMC-capability */
402 smc->use_fallback = true;
403 goto out_connected;
404 }
405
Ursula Brauna046d572017-01-09 16:55:16 +0100406 /* IPSec connections opt out of SMC-R optimizations */
407 if (using_ipsec(smc)) {
408 reason_code = SMC_CLC_DECL_IPSEC;
409 goto decline_rdma;
410 }
411
412 /* PNET table look up: search active ib_device and port
413 * within same PNETID that also contains the ethernet device
414 * used for the internal TCP socket
415 */
416 smc_pnet_find_roce_resource(smc->clcsock->sk, &smcibdev, &ibport);
417 if (!smcibdev) {
418 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
419 goto decline_rdma;
420 }
421
422 /* do inband token exchange */
423 reason_code = smc_clc_send_proposal(smc, smcibdev, ibport);
424 if (reason_code < 0) {
425 rc = reason_code;
426 goto out_err;
427 }
428 if (reason_code > 0) /* configuration error */
429 goto decline_rdma;
430 /* receive SMC Accept CLC message */
431 reason_code = smc_clc_wait_msg(smc, &aclc, sizeof(aclc),
432 SMC_CLC_ACCEPT);
433 if (reason_code < 0) {
434 rc = reason_code;
435 goto out_err;
436 }
437 if (reason_code > 0)
438 goto decline_rdma;
439
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100440 srv_first_contact = aclc.hdr.flag;
441 mutex_lock(&smc_create_lgr_pending);
442 local_contact = smc_conn_create(smc, inaddr->sin_addr.s_addr, smcibdev,
443 ibport, &aclc.lcl, srv_first_contact);
444 if (local_contact < 0) {
445 rc = local_contact;
446 if (rc == -ENOMEM)
447 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
448 else if (rc == -ENOLINK)
449 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
450 goto decline_rdma_unlock;
451 }
452 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
Ursula Brauna046d572017-01-09 16:55:16 +0100453
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100454 smc_conn_save_peer_info(smc, &aclc);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100455
Ursula Braun3e034722017-07-28 13:56:20 +0200456 /* create send buffer and rmb */
457 rc = smc_buf_create(smc);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100458 if (rc) {
459 reason_code = SMC_CLC_DECL_MEM;
460 goto decline_rdma_unlock;
461 }
462
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100463 if (local_contact == SMC_FIRST_CONTACT)
464 smc_link_save_peer_info(link, &aclc);
Ursula Braunbd4ad572017-01-09 16:55:20 +0100465
466 rc = smc_rmb_rtoken_handling(&smc->conn, &aclc);
467 if (rc) {
468 reason_code = SMC_CLC_DECL_INTERR;
469 goto decline_rdma_unlock;
470 }
471
Ursula Braun46c28db2017-04-10 14:58:01 +0200472 smc_close_init(smc);
473 smc_rx_init(smc);
474
Ursula Braunbd4ad572017-01-09 16:55:20 +0100475 if (local_contact == SMC_FIRST_CONTACT) {
476 rc = smc_ib_ready_link(link);
477 if (rc) {
478 reason_code = SMC_CLC_DECL_INTERR;
479 goto decline_rdma_unlock;
480 }
Ursula Braun652a1e42017-07-28 13:56:17 +0200481 } else {
482 struct smc_buf_desc *buf_desc = smc->conn.rmb_desc;
483
484 if (!buf_desc->reused) {
485 /* register memory region for new rmb */
486 rc = smc_wr_reg_send(link,
487 buf_desc->mr_rx[SMC_SINGLE_LINK]);
488 if (rc) {
489 reason_code = SMC_CLC_DECL_INTERR;
490 goto decline_rdma_unlock;
491 }
492 }
Ursula Braunbd4ad572017-01-09 16:55:20 +0100493 }
Ursula Braun10428dd2017-07-28 13:56:22 +0200494 smc_rmb_sync_sg_for_device(&smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100495
496 rc = smc_clc_send_confirm(smc);
497 if (rc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100498 goto out_err_unlock;
Ursula Brauna046d572017-01-09 16:55:16 +0100499
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100500 if (local_contact == SMC_FIRST_CONTACT) {
501 /* QP confirmation over RoCE fabric */
502 reason_code = smc_clnt_conf_first_link(
503 smc, &smcibdev->gid[ibport - 1]);
504 if (reason_code < 0) {
505 rc = reason_code;
506 goto out_err_unlock;
507 }
508 if (reason_code > 0)
509 goto decline_rdma_unlock;
510 }
Ursula Brauna046d572017-01-09 16:55:16 +0100511
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100512 mutex_unlock(&smc_create_lgr_pending);
Ursula Braune6727f32017-01-09 16:55:23 +0100513 smc_tx_init(smc);
514
Ursula Brauna046d572017-01-09 16:55:16 +0100515out_connected:
516 smc_copy_sock_settings_to_clc(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +0100517 if (smc->sk.sk_state == SMC_INIT)
518 smc->sk.sk_state = SMC_ACTIVE;
Ursula Brauna046d572017-01-09 16:55:16 +0100519
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100520 return rc ? rc : local_contact;
Ursula Brauna046d572017-01-09 16:55:16 +0100521
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100522decline_rdma_unlock:
Ursula Braun610db662018-01-25 11:15:34 +0100523 if (local_contact == SMC_FIRST_CONTACT)
524 smc_lgr_forget(smc->conn.lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100525 mutex_unlock(&smc_create_lgr_pending);
526 smc_conn_free(&smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100527decline_rdma:
528 /* RDMA setup failed, switch back to TCP */
529 smc->use_fallback = true;
530 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
Ursula Braunbfbedfd2017-09-21 09:16:32 +0200531 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braun0c9f1512017-12-07 13:38:45 +0100532 if (rc < 0)
Ursula Brauna046d572017-01-09 16:55:16 +0100533 goto out_err;
534 }
535 goto out_connected;
536
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100537out_err_unlock:
Ursula Braun610db662018-01-25 11:15:34 +0100538 if (local_contact == SMC_FIRST_CONTACT)
539 smc_lgr_forget(smc->conn.lgr);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100540 mutex_unlock(&smc_create_lgr_pending);
541 smc_conn_free(&smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100542out_err:
Ursula Braun51f1de72018-01-26 09:28:48 +0100543 if (smc->sk.sk_state == SMC_INIT)
544 sock_put(&smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +0100545 return rc;
546}
547
Ursula Braunac713872017-01-09 16:55:13 +0100548static int smc_connect(struct socket *sock, struct sockaddr *addr,
549 int alen, int flags)
550{
551 struct sock *sk = sock->sk;
552 struct smc_sock *smc;
553 int rc = -EINVAL;
554
555 smc = smc_sk(sk);
556
557 /* separate smc parameter checking to be safe */
558 if (alen < sizeof(addr->sa_family))
559 goto out_err;
560 if (addr->sa_family != AF_INET)
561 goto out_err;
Ursula Brauna046d572017-01-09 16:55:16 +0100562 smc->addr = addr; /* needed for nonblocking connect */
Ursula Braunac713872017-01-09 16:55:13 +0100563
564 lock_sock(sk);
565 switch (sk->sk_state) {
566 default:
567 goto out;
568 case SMC_ACTIVE:
569 rc = -EISCONN;
570 goto out;
571 case SMC_INIT:
572 rc = 0;
573 break;
574 }
575
576 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200577 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +0100578 rc = kernel_connect(smc->clcsock, addr, alen, flags);
579 if (rc)
580 goto out;
581
Ursula Brauna046d572017-01-09 16:55:16 +0100582 /* setup RDMA connection */
583 rc = smc_connect_rdma(smc);
584 if (rc < 0)
585 goto out;
586 else
587 rc = 0; /* success cases including fallback */
Ursula Braunac713872017-01-09 16:55:13 +0100588
589out:
590 release_sock(sk);
591out_err:
592 return rc;
593}
594
595static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
596{
Ursula Braun3163c502018-01-24 10:28:12 +0100597 struct socket *new_clcsock = NULL;
598 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100599 struct sock *new_sk;
600 int rc;
601
Ursula Braun3163c502018-01-24 10:28:12 +0100602 release_sock(lsk);
603 new_sk = smc_sock_alloc(sock_net(lsk), NULL);
Ursula Braunac713872017-01-09 16:55:13 +0100604 if (!new_sk) {
605 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100606 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100607 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100608 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100609 goto out;
610 }
611 *new_smc = smc_sk(new_sk);
612
613 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
Ursula Braun3163c502018-01-24 10:28:12 +0100614 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100615 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100616 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100617 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Brauna046d572017-01-09 16:55:16 +0100618 if (new_clcsock)
619 sock_release(new_clcsock);
620 new_sk->sk_state = SMC_CLOSED;
621 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun3163c502018-01-24 10:28:12 +0100622 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100623 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100624 *new_smc = NULL;
625 goto out;
626 }
627
628 (*new_smc)->clcsock = new_clcsock;
629out:
630 return rc;
631}
632
Ursula Brauna046d572017-01-09 16:55:16 +0100633/* add a just created sock to the accept queue of the listen sock as
634 * candidate for a following socket accept call from user space
635 */
636static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
637{
638 struct smc_sock *par = smc_sk(parent);
639
Ursula Braun51f1de72018-01-26 09:28:48 +0100640 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100641 spin_lock(&par->accept_q_lock);
642 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
643 spin_unlock(&par->accept_q_lock);
644 sk_acceptq_added(parent);
645}
646
647/* remove a socket from the accept queue of its parental listening socket */
648static void smc_accept_unlink(struct sock *sk)
649{
650 struct smc_sock *par = smc_sk(sk)->listen_smc;
651
652 spin_lock(&par->accept_q_lock);
653 list_del_init(&smc_sk(sk)->accept_q);
654 spin_unlock(&par->accept_q_lock);
655 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100656 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100657}
658
659/* remove a sock from the accept queue to bind it to a new socket created
660 * for a socket accept call from user space
661 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100662struct sock *smc_accept_dequeue(struct sock *parent,
663 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100664{
665 struct smc_sock *isk, *n;
666 struct sock *new_sk;
667
668 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
669 new_sk = (struct sock *)isk;
670
671 smc_accept_unlink(new_sk);
672 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braun127f4972018-01-26 09:28:49 +0100673 if (isk->clcsock) {
674 sock_release(isk->clcsock);
675 isk->clcsock = NULL;
676 }
Ursula Braun288c83902017-04-10 14:58:04 +0200677 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100678 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +0100679 continue;
680 }
681 if (new_sock)
682 sock_graft(new_sk, new_sock);
683 return new_sk;
684 }
685 return NULL;
686}
687
688/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +0100689void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +0100690{
691 struct smc_sock *smc = smc_sk(sk);
692
Ursula Braunb38d7322017-01-09 16:55:25 +0100693 lock_sock(sk);
694 if (!sk->sk_lingertime)
695 /* wait for peer closing */
696 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100697 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100698 smc_close_active(smc);
Ursula Braun288c83902017-04-10 14:58:04 +0200699 sock_set_flag(sk, SOCK_DEAD);
700 sk->sk_shutdown |= SHUTDOWN_MASK;
701 }
Ursula Brauna046d572017-01-09 16:55:16 +0100702 if (smc->clcsock) {
703 struct socket *tcp;
704
705 tcp = smc->clcsock;
706 smc->clcsock = NULL;
707 sock_release(tcp);
708 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100709 if (smc->use_fallback) {
Ursula Braun51f1de72018-01-26 09:28:48 +0100710 sock_put(sk); /* passive closing */
711 sk->sk_state = SMC_CLOSED;
712 } else {
713 if (sk->sk_state == SMC_CLOSED)
714 smc_conn_free(&smc->conn);
Ursula Braunb38d7322017-01-09 16:55:25 +0100715 }
716 release_sock(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100717 sk->sk_prot->unhash(sk);
718 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +0100719}
720
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100721static int smc_serv_conf_first_link(struct smc_sock *smc)
722{
723 struct smc_link_group *lgr = smc->conn.lgr;
724 struct smc_link *link;
725 int rest;
726 int rc;
727
728 link = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun652a1e42017-07-28 13:56:17 +0200729
730 rc = smc_wr_reg_send(link,
731 smc->conn.rmb_desc->mr_rx[SMC_SINGLE_LINK]);
732 if (rc)
733 return SMC_CLC_DECL_INTERR;
734
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100735 /* send CONFIRM LINK request to client over the RoCE fabric */
736 rc = smc_llc_send_confirm_link(link,
737 link->smcibdev->mac[link->ibport - 1],
738 &link->smcibdev->gid[link->ibport - 1],
739 SMC_LLC_REQ);
740 if (rc < 0)
741 return SMC_CLC_DECL_TCL;
742
743 /* receive CONFIRM LINK response from client over the RoCE fabric */
744 rest = wait_for_completion_interruptible_timeout(
745 &link->llc_confirm_resp,
746 SMC_LLC_WAIT_FIRST_TIME);
747 if (rest <= 0) {
748 struct smc_clc_msg_decline dclc;
749
750 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
751 SMC_CLC_DECLINE);
752 }
753
754 return rc;
755}
756
Ursula Brauna046d572017-01-09 16:55:16 +0100757/* setup for RDMA connection of server */
758static void smc_listen_work(struct work_struct *work)
759{
760 struct smc_sock *new_smc = container_of(work, struct smc_sock,
761 smc_listen_work);
Ursula Braune7b7a642017-12-07 13:38:49 +0100762 struct smc_clc_msg_proposal_prefix *pclc_prfx;
Ursula Brauna046d572017-01-09 16:55:16 +0100763 struct socket *newclcsock = new_smc->clcsock;
764 struct smc_sock *lsmc = new_smc->listen_smc;
765 struct smc_clc_msg_accept_confirm cclc;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100766 int local_contact = SMC_REUSE_CONTACT;
Ursula Brauna046d572017-01-09 16:55:16 +0100767 struct sock *newsmcsk = &new_smc->sk;
Ursula Braune7b7a642017-12-07 13:38:49 +0100768 struct smc_clc_msg_proposal *pclc;
Ursula Brauna046d572017-01-09 16:55:16 +0100769 struct smc_ib_device *smcibdev;
770 struct sockaddr_in peeraddr;
Ursula Braune7b7a642017-12-07 13:38:49 +0100771 u8 buf[SMC_CLC_MAX_LEN];
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100772 struct smc_link *link;
Ursula Brauna046d572017-01-09 16:55:16 +0100773 int reason_code = 0;
774 int rc = 0, len;
775 __be32 subnet;
776 u8 prefix_len;
777 u8 ibport;
778
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200779 /* check if peer is smc capable */
780 if (!tcp_sk(newclcsock->sk)->syn_smc) {
781 new_smc->use_fallback = true;
782 goto out_connected;
783 }
784
Ursula Brauna046d572017-01-09 16:55:16 +0100785 /* do inband token exchange -
786 *wait for and receive SMC Proposal CLC message
787 */
Ursula Braune7b7a642017-12-07 13:38:49 +0100788 reason_code = smc_clc_wait_msg(new_smc, &buf, sizeof(buf),
Ursula Brauna046d572017-01-09 16:55:16 +0100789 SMC_CLC_PROPOSAL);
790 if (reason_code < 0)
791 goto out_err;
792 if (reason_code > 0)
793 goto decline_rdma;
794
795 /* IPSec connections opt out of SMC-R optimizations */
796 if (using_ipsec(new_smc)) {
797 reason_code = SMC_CLC_DECL_IPSEC;
798 goto decline_rdma;
799 }
800
801 /* PNET table look up: search active ib_device and port
802 * within same PNETID that also contains the ethernet device
803 * used for the internal TCP socket
804 */
805 smc_pnet_find_roce_resource(newclcsock->sk, &smcibdev, &ibport);
806 if (!smcibdev) {
807 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
808 goto decline_rdma;
809 }
810
811 /* determine subnet and mask from internal TCP socket */
812 rc = smc_netinfo_by_tcpsk(newclcsock, &subnet, &prefix_len);
813 if (rc) {
814 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
815 goto decline_rdma;
816 }
Ursula Braune7b7a642017-12-07 13:38:49 +0100817
818 pclc = (struct smc_clc_msg_proposal *)&buf;
819 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
820 if (pclc_prfx->outgoing_subnet != subnet ||
821 pclc_prfx->prefix_len != prefix_len) {
Ursula Brauna046d572017-01-09 16:55:16 +0100822 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
823 goto decline_rdma;
824 }
825
826 /* get address of the peer connected to the internal TCP socket */
827 kernel_getpeername(newclcsock, (struct sockaddr *)&peeraddr, &len);
828
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100829 /* allocate connection / link group */
830 mutex_lock(&smc_create_lgr_pending);
831 local_contact = smc_conn_create(new_smc, peeraddr.sin_addr.s_addr,
Ursula Braune7b7a642017-12-07 13:38:49 +0100832 smcibdev, ibport, &pclc->lcl, 0);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100833 if (local_contact < 0) {
834 rc = local_contact;
835 if (rc == -ENOMEM)
836 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
Ursula Braun145686b2017-10-25 11:01:44 +0200837 goto decline_rdma_unlock;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100838 }
839 link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
Ursula Brauna046d572017-01-09 16:55:16 +0100840
Ursula Braun3e034722017-07-28 13:56:20 +0200841 /* create send buffer and rmb */
842 rc = smc_buf_create(new_smc);
Ursula Brauncd6851f2017-01-09 16:55:18 +0100843 if (rc) {
844 reason_code = SMC_CLC_DECL_MEM;
Ursula Braun145686b2017-10-25 11:01:44 +0200845 goto decline_rdma_unlock;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100846 }
Ursula Brauna046d572017-01-09 16:55:16 +0100847
Ursula Braun46c28db2017-04-10 14:58:01 +0200848 smc_close_init(new_smc);
849 smc_rx_init(new_smc);
850
Ursula Braun652a1e42017-07-28 13:56:17 +0200851 if (local_contact != SMC_FIRST_CONTACT) {
852 struct smc_buf_desc *buf_desc = new_smc->conn.rmb_desc;
853
854 if (!buf_desc->reused) {
855 /* register memory region for new rmb */
856 rc = smc_wr_reg_send(link,
857 buf_desc->mr_rx[SMC_SINGLE_LINK]);
858 if (rc) {
859 reason_code = SMC_CLC_DECL_INTERR;
Ursula Braun145686b2017-10-25 11:01:44 +0200860 goto decline_rdma_unlock;
Ursula Braun652a1e42017-07-28 13:56:17 +0200861 }
862 }
863 }
Ursula Braun10428dd2017-07-28 13:56:22 +0200864 smc_rmb_sync_sg_for_device(&new_smc->conn);
Ursula Braun652a1e42017-07-28 13:56:17 +0200865
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100866 rc = smc_clc_send_accept(new_smc, local_contact);
Ursula Brauna046d572017-01-09 16:55:16 +0100867 if (rc)
Ursula Braun145686b2017-10-25 11:01:44 +0200868 goto out_err_unlock;
Ursula Brauna046d572017-01-09 16:55:16 +0100869
870 /* receive SMC Confirm CLC message */
871 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
872 SMC_CLC_CONFIRM);
873 if (reason_code < 0)
Ursula Braun145686b2017-10-25 11:01:44 +0200874 goto out_err_unlock;
Ursula Brauna046d572017-01-09 16:55:16 +0100875 if (reason_code > 0)
Ursula Braun145686b2017-10-25 11:01:44 +0200876 goto decline_rdma_unlock;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100877 smc_conn_save_peer_info(new_smc, &cclc);
878 if (local_contact == SMC_FIRST_CONTACT)
879 smc_link_save_peer_info(link, &cclc);
Ursula Brauna046d572017-01-09 16:55:16 +0100880
Ursula Braunbd4ad572017-01-09 16:55:20 +0100881 rc = smc_rmb_rtoken_handling(&new_smc->conn, &cclc);
882 if (rc) {
883 reason_code = SMC_CLC_DECL_INTERR;
Ursula Braun145686b2017-10-25 11:01:44 +0200884 goto decline_rdma_unlock;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100885 }
886
Ursula Braunbd4ad572017-01-09 16:55:20 +0100887 if (local_contact == SMC_FIRST_CONTACT) {
888 rc = smc_ib_ready_link(link);
889 if (rc) {
890 reason_code = SMC_CLC_DECL_INTERR;
Ursula Braun145686b2017-10-25 11:01:44 +0200891 goto decline_rdma_unlock;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100892 }
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100893 /* QP confirmation over RoCE fabric */
894 reason_code = smc_serv_conf_first_link(new_smc);
Ursula Braun0c9f1512017-12-07 13:38:45 +0100895 if (reason_code < 0)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100896 /* peer is not aware of a problem */
Ursula Braun145686b2017-10-25 11:01:44 +0200897 goto out_err_unlock;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100898 if (reason_code > 0)
Ursula Braun145686b2017-10-25 11:01:44 +0200899 goto decline_rdma_unlock;
Ursula Braunbd4ad572017-01-09 16:55:20 +0100900 }
Ursula Brauna046d572017-01-09 16:55:16 +0100901
Ursula Braune6727f32017-01-09 16:55:23 +0100902 smc_tx_init(new_smc);
Ursula Braun145686b2017-10-25 11:01:44 +0200903 mutex_unlock(&smc_create_lgr_pending);
Ursula Braune6727f32017-01-09 16:55:23 +0100904
Ursula Brauna046d572017-01-09 16:55:16 +0100905out_connected:
906 sk_refcnt_debug_inc(newsmcsk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100907 if (newsmcsk->sk_state == SMC_INIT)
908 newsmcsk->sk_state = SMC_ACTIVE;
Ursula Brauna046d572017-01-09 16:55:16 +0100909enqueue:
Ursula Braunb38d7322017-01-09 16:55:25 +0100910 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +0100911 if (lsmc->sk.sk_state == SMC_LISTEN) {
912 smc_accept_enqueue(&lsmc->sk, newsmcsk);
913 } else { /* no longer listening */
914 smc_close_non_accepted(newsmcsk);
915 }
916 release_sock(&lsmc->sk);
917
918 /* Wake up accept */
919 lsmc->sk.sk_data_ready(&lsmc->sk);
920 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
921 return;
922
Ursula Braun145686b2017-10-25 11:01:44 +0200923decline_rdma_unlock:
Ursula Braun610db662018-01-25 11:15:34 +0100924 if (local_contact == SMC_FIRST_CONTACT)
925 smc_lgr_forget(new_smc->conn.lgr);
Ursula Braun145686b2017-10-25 11:01:44 +0200926 mutex_unlock(&smc_create_lgr_pending);
Ursula Brauna046d572017-01-09 16:55:16 +0100927decline_rdma:
928 /* RDMA setup failed, switch back to TCP */
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100929 smc_conn_free(&new_smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100930 new_smc->use_fallback = true;
931 if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) {
Ursula Braun0c9f1512017-12-07 13:38:45 +0100932 if (smc_clc_send_decline(new_smc, reason_code) < 0)
Ursula Brauna046d572017-01-09 16:55:16 +0100933 goto out_err;
934 }
935 goto out_connected;
936
Ursula Braun145686b2017-10-25 11:01:44 +0200937out_err_unlock:
Ursula Braun610db662018-01-25 11:15:34 +0100938 if (local_contact == SMC_FIRST_CONTACT)
939 smc_lgr_forget(new_smc->conn.lgr);
Ursula Braun145686b2017-10-25 11:01:44 +0200940 mutex_unlock(&smc_create_lgr_pending);
Ursula Brauna046d572017-01-09 16:55:16 +0100941out_err:
Ursula Braun51f1de72018-01-26 09:28:48 +0100942 if (newsmcsk->sk_state == SMC_INIT)
943 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +0100944 newsmcsk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100945 smc_conn_free(&new_smc->conn);
Ursula Brauna046d572017-01-09 16:55:16 +0100946 goto enqueue; /* queue new sock with sk_err set */
947}
948
949static void smc_tcp_listen_work(struct work_struct *work)
950{
951 struct smc_sock *lsmc = container_of(work, struct smc_sock,
952 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +0100953 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +0100954 struct smc_sock *new_smc;
955 int rc = 0;
956
Ursula Braun3163c502018-01-24 10:28:12 +0100957 lock_sock(lsk);
958 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +0100959 rc = smc_clcsock_accept(lsmc, &new_smc);
960 if (rc)
961 goto out;
962 if (!new_smc)
963 continue;
964
965 new_smc->listen_smc = lsmc;
966 new_smc->use_fallback = false; /* assume rdma capability first*/
Ursula Braun3163c502018-01-24 10:28:12 +0100967 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +0100968 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
969 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braun51f1de72018-01-26 09:28:48 +0100970 sock_hold(&new_smc->sk); /* sock_put in passive closing */
971 if (!schedule_work(&new_smc->smc_listen_work))
972 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100973 }
974
975out:
Ursula Braun127f4972018-01-26 09:28:49 +0100976 if (lsmc->clcsock) {
977 sock_release(lsmc->clcsock);
978 lsmc->clcsock = NULL;
979 }
Ursula Braun3163c502018-01-24 10:28:12 +0100980 release_sock(lsk);
Ursula Braun127f4972018-01-26 09:28:49 +0100981 /* no more listening, wake up smc_close_wait_listen_clcsock and
982 * accept
983 */
984 lsk->sk_state_change(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100985 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +0100986}
987
Ursula Braunac713872017-01-09 16:55:13 +0100988static int smc_listen(struct socket *sock, int backlog)
989{
990 struct sock *sk = sock->sk;
991 struct smc_sock *smc;
992 int rc;
993
994 smc = smc_sk(sk);
995 lock_sock(sk);
996
997 rc = -EINVAL;
998 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
999 goto out;
1000
1001 rc = 0;
1002 if (sk->sk_state == SMC_LISTEN) {
1003 sk->sk_max_ack_backlog = backlog;
1004 goto out;
1005 }
1006 /* some socket options are handled in core, so we could not apply
1007 * them to the clc socket -- copy smc socket options to clc socket
1008 */
1009 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001010 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001011
1012 rc = kernel_listen(smc->clcsock, backlog);
1013 if (rc)
1014 goto out;
1015 sk->sk_max_ack_backlog = backlog;
1016 sk->sk_ack_backlog = 0;
1017 sk->sk_state = SMC_LISTEN;
Ursula Brauna046d572017-01-09 16:55:16 +01001018 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun51f1de72018-01-26 09:28:48 +01001019 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1020 if (!schedule_work(&smc->tcp_listen_work))
1021 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001022
1023out:
1024 release_sock(sk);
1025 return rc;
1026}
1027
1028static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001029 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001030{
Ursula Brauna046d572017-01-09 16:55:16 +01001031 struct sock *sk = sock->sk, *nsk;
1032 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001033 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001034 long timeo;
1035 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001036
1037 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001038 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001039 lock_sock(sk);
1040
1041 if (lsmc->sk.sk_state != SMC_LISTEN) {
1042 rc = -EINVAL;
1043 goto out;
1044 }
1045
Ursula Brauna046d572017-01-09 16:55:16 +01001046 /* Wait for an incoming connection */
1047 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1048 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1049 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1050 set_current_state(TASK_INTERRUPTIBLE);
1051 if (!timeo) {
1052 rc = -EAGAIN;
1053 break;
1054 }
1055 release_sock(sk);
1056 timeo = schedule_timeout(timeo);
1057 /* wakeup by sk_data_ready in smc_listen_work() */
1058 sched_annotate_sleep();
1059 lock_sock(sk);
1060 if (signal_pending(current)) {
1061 rc = sock_intr_errno(timeo);
1062 break;
1063 }
1064 }
1065 set_current_state(TASK_RUNNING);
1066 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001067
Ursula Brauna046d572017-01-09 16:55:16 +01001068 if (!rc)
1069 rc = sock_error(nsk);
Ursula Braunac713872017-01-09 16:55:13 +01001070
1071out:
1072 release_sock(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001073 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001074 return rc;
1075}
1076
1077static int smc_getname(struct socket *sock, struct sockaddr *addr,
1078 int *len, int peer)
1079{
1080 struct smc_sock *smc;
1081
Ursula Braunb38d7322017-01-09 16:55:25 +01001082 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1083 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001084 return -ENOTCONN;
1085
1086 smc = smc_sk(sock->sk);
1087
1088 return smc->clcsock->ops->getname(smc->clcsock, addr, len, peer);
1089}
1090
1091static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1092{
1093 struct sock *sk = sock->sk;
1094 struct smc_sock *smc;
1095 int rc = -EPIPE;
1096
1097 smc = smc_sk(sk);
1098 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001099 if ((sk->sk_state != SMC_ACTIVE) &&
1100 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1101 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001102 goto out;
1103 if (smc->use_fallback)
1104 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1105 else
Ursula Braune6727f32017-01-09 16:55:23 +01001106 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001107out:
1108 release_sock(sk);
1109 return rc;
1110}
1111
1112static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1113 int flags)
1114{
1115 struct sock *sk = sock->sk;
1116 struct smc_sock *smc;
1117 int rc = -ENOTCONN;
1118
1119 smc = smc_sk(sk);
1120 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001121 if ((sk->sk_state == SMC_INIT) ||
1122 (sk->sk_state == SMC_LISTEN) ||
1123 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001124 goto out;
1125
Ursula Braunb38d7322017-01-09 16:55:25 +01001126 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1127 rc = 0;
1128 goto out;
1129 }
1130
Ursula Braunac713872017-01-09 16:55:13 +01001131 if (smc->use_fallback)
1132 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
1133 else
Ursula Braun952310c2017-01-09 16:55:24 +01001134 rc = smc_rx_recvmsg(smc, msg, len, flags);
Ursula Braunb38d7322017-01-09 16:55:25 +01001135
Ursula Braunac713872017-01-09 16:55:13 +01001136out:
1137 release_sock(sk);
1138 return rc;
1139}
1140
Ursula Brauna046d572017-01-09 16:55:16 +01001141static unsigned int smc_accept_poll(struct sock *parent)
1142{
Ursula Braun8dce2782018-01-26 09:28:47 +01001143 struct smc_sock *isk = smc_sk(parent);
1144 int mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001145
Ursula Braun8dce2782018-01-26 09:28:47 +01001146 spin_lock(&isk->accept_q_lock);
1147 if (!list_empty(&isk->accept_q))
1148 mask = POLLIN | POLLRDNORM;
1149 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001150
Ursula Braun8dce2782018-01-26 09:28:47 +01001151 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001152}
1153
Ursula Braunac713872017-01-09 16:55:13 +01001154static unsigned int smc_poll(struct file *file, struct socket *sock,
1155 poll_table *wait)
1156{
1157 struct sock *sk = sock->sk;
1158 unsigned int mask = 0;
1159 struct smc_sock *smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001160 int rc;
Ursula Braunac713872017-01-09 16:55:13 +01001161
Ursula Braun8dce2782018-01-26 09:28:47 +01001162 if (!sk)
1163 return POLLNVAL;
1164
Ursula Braunac713872017-01-09 16:55:13 +01001165 smc = smc_sk(sock->sk);
Ursula Braun8dce2782018-01-26 09:28:47 +01001166 sock_hold(sk);
1167 lock_sock(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001168 if ((sk->sk_state == SMC_INIT) || smc->use_fallback) {
1169 /* delegate to CLC child sock */
Ursula Braun8dce2782018-01-26 09:28:47 +01001170 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001171 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
1172 /* if non-blocking connect finished ... */
1173 lock_sock(sk);
1174 if ((sk->sk_state == SMC_INIT) && (mask & POLLOUT)) {
Ursula Brauna046d572017-01-09 16:55:16 +01001175 sk->sk_err = smc->clcsock->sk->sk_err;
1176 if (sk->sk_err) {
1177 mask |= POLLERR;
1178 } else {
1179 rc = smc_connect_rdma(smc);
1180 if (rc < 0)
1181 mask |= POLLERR;
Ursula Braun8dce2782018-01-26 09:28:47 +01001182 /* success cases including fallback */
1183 mask |= POLLOUT | POLLWRNORM;
Ursula Brauna046d572017-01-09 16:55:16 +01001184 }
Ursula Braunac713872017-01-09 16:55:13 +01001185 }
Ursula Braunac713872017-01-09 16:55:13 +01001186 } else {
Ursula Braun8dce2782018-01-26 09:28:47 +01001187 if (sk->sk_state != SMC_CLOSED) {
1188 release_sock(sk);
1189 sock_poll_wait(file, sk_sleep(sk), wait);
1190 lock_sock(sk);
1191 }
Ursula Brauna046d572017-01-09 16:55:16 +01001192 if (sk->sk_err)
1193 mask |= POLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001194 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1195 (sk->sk_state == SMC_CLOSED))
1196 mask |= POLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001197 if (sk->sk_state == SMC_LISTEN) {
1198 /* woken up by sk_data_ready in smc_listen_work() */
1199 mask = smc_accept_poll(sk);
1200 } else {
1201 if (atomic_read(&smc->conn.sndbuf_space) ||
1202 sk->sk_shutdown & SEND_SHUTDOWN) {
1203 mask |= POLLOUT | POLLWRNORM;
1204 } else {
1205 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1206 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1207 }
1208 if (atomic_read(&smc->conn.bytes_to_rcv))
1209 mask |= POLLIN | POLLRDNORM;
1210 if (sk->sk_shutdown & RCV_SHUTDOWN)
1211 mask |= POLLIN | POLLRDNORM | POLLRDHUP;
1212 if (sk->sk_state == SMC_APPCLOSEWAIT1)
1213 mask |= POLLIN;
1214 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001215
Ursula Braunac713872017-01-09 16:55:13 +01001216 }
Ursula Braun8dce2782018-01-26 09:28:47 +01001217 release_sock(sk);
1218 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001219
1220 return mask;
1221}
1222
1223static int smc_shutdown(struct socket *sock, int how)
1224{
1225 struct sock *sk = sock->sk;
1226 struct smc_sock *smc;
1227 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001228 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001229
1230 smc = smc_sk(sk);
1231
1232 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001233 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001234
1235 lock_sock(sk);
1236
1237 rc = -ENOTCONN;
Ursula Braunb38d7322017-01-09 16:55:25 +01001238 if ((sk->sk_state != SMC_LISTEN) &&
1239 (sk->sk_state != SMC_ACTIVE) &&
1240 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1241 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1242 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1243 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1244 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001245 goto out;
1246 if (smc->use_fallback) {
1247 rc = kernel_sock_shutdown(smc->clcsock, how);
1248 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1249 if (sk->sk_shutdown == SHUTDOWN_MASK)
1250 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001251 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001252 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001253 switch (how) {
1254 case SHUT_RDWR: /* shutdown in both directions */
1255 rc = smc_close_active(smc);
1256 break;
1257 case SHUT_WR:
1258 rc = smc_close_shutdown_write(smc);
1259 break;
1260 case SHUT_RD:
1261 if (sk->sk_state == SMC_LISTEN)
1262 rc = smc_close_active(smc);
1263 else
1264 rc = 0;
1265 /* nothing more to do because peer is not involved */
1266 break;
1267 }
1268 rc1 = kernel_sock_shutdown(smc->clcsock, how);
1269 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1270 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001271
1272out:
1273 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001274 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001275}
1276
1277static int smc_setsockopt(struct socket *sock, int level, int optname,
1278 char __user *optval, unsigned int optlen)
1279{
1280 struct sock *sk = sock->sk;
1281 struct smc_sock *smc;
1282
1283 smc = smc_sk(sk);
1284
1285 /* generic setsockopts reaching us here always apply to the
1286 * CLC socket
1287 */
1288 return smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1289 optval, optlen);
1290}
1291
1292static int smc_getsockopt(struct socket *sock, int level, int optname,
1293 char __user *optval, int __user *optlen)
1294{
1295 struct smc_sock *smc;
1296
1297 smc = smc_sk(sock->sk);
1298 /* socket options apply to the CLC socket */
1299 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1300 optval, optlen);
1301}
1302
1303static int smc_ioctl(struct socket *sock, unsigned int cmd,
1304 unsigned long arg)
1305{
1306 struct smc_sock *smc;
1307
1308 smc = smc_sk(sock->sk);
1309 if (smc->use_fallback)
1310 return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1311 else
1312 return sock_no_ioctl(sock, cmd, arg);
1313}
1314
1315static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1316 int offset, size_t size, int flags)
1317{
1318 struct sock *sk = sock->sk;
1319 struct smc_sock *smc;
1320 int rc = -EPIPE;
1321
1322 smc = smc_sk(sk);
1323 lock_sock(sk);
1324 if (sk->sk_state != SMC_ACTIVE)
1325 goto out;
1326 if (smc->use_fallback)
1327 rc = kernel_sendpage(smc->clcsock, page, offset,
1328 size, flags);
1329 else
1330 rc = sock_no_sendpage(sock, page, offset, size, flags);
1331
1332out:
1333 release_sock(sk);
1334 return rc;
1335}
1336
1337static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1338 struct pipe_inode_info *pipe, size_t len,
1339 unsigned int flags)
1340{
1341 struct sock *sk = sock->sk;
1342 struct smc_sock *smc;
1343 int rc = -ENOTCONN;
1344
1345 smc = smc_sk(sk);
1346 lock_sock(sk);
1347 if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED))
1348 goto out;
1349 if (smc->use_fallback) {
1350 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1351 pipe, len, flags);
1352 } else {
1353 rc = -EOPNOTSUPP;
1354 }
1355out:
1356 release_sock(sk);
1357 return rc;
1358}
1359
1360/* must look like tcp */
1361static const struct proto_ops smc_sock_ops = {
1362 .family = PF_SMC,
1363 .owner = THIS_MODULE,
1364 .release = smc_release,
1365 .bind = smc_bind,
1366 .connect = smc_connect,
1367 .socketpair = sock_no_socketpair,
1368 .accept = smc_accept,
1369 .getname = smc_getname,
1370 .poll = smc_poll,
1371 .ioctl = smc_ioctl,
1372 .listen = smc_listen,
1373 .shutdown = smc_shutdown,
1374 .setsockopt = smc_setsockopt,
1375 .getsockopt = smc_getsockopt,
1376 .sendmsg = smc_sendmsg,
1377 .recvmsg = smc_recvmsg,
1378 .mmap = sock_no_mmap,
1379 .sendpage = smc_sendpage,
1380 .splice_read = smc_splice_read,
1381};
1382
1383static int smc_create(struct net *net, struct socket *sock, int protocol,
1384 int kern)
1385{
1386 struct smc_sock *smc;
1387 struct sock *sk;
1388 int rc;
1389
1390 rc = -ESOCKTNOSUPPORT;
1391 if (sock->type != SOCK_STREAM)
1392 goto out;
1393
1394 rc = -EPROTONOSUPPORT;
1395 if ((protocol != IPPROTO_IP) && (protocol != IPPROTO_TCP))
1396 goto out;
1397
1398 rc = -ENOBUFS;
1399 sock->ops = &smc_sock_ops;
1400 sk = smc_sock_alloc(net, sock);
1401 if (!sk)
1402 goto out;
1403
1404 /* create internal TCP socket for CLC handshake and fallback */
1405 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001406 smc->use_fallback = false; /* assume rdma capability first */
Ursula Braunac713872017-01-09 16:55:13 +01001407 rc = sock_create_kern(net, PF_INET, SOCK_STREAM,
1408 IPPROTO_TCP, &smc->clcsock);
1409 if (rc)
1410 sk_common_release(sk);
Ursula Brauncd6851f2017-01-09 16:55:18 +01001411 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1412 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01001413
1414out:
1415 return rc;
1416}
1417
1418static const struct net_proto_family smc_sock_family_ops = {
1419 .family = PF_SMC,
1420 .owner = THIS_MODULE,
1421 .create = smc_create,
1422};
1423
1424static int __init smc_init(void)
1425{
1426 int rc;
1427
Thomas Richter6812baa2017-01-09 16:55:15 +01001428 rc = smc_pnet_init();
1429 if (rc)
1430 return rc;
1431
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001432 rc = smc_llc_init();
1433 if (rc) {
1434 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1435 goto out_pnet;
1436 }
1437
Ursula Braun5f083182017-01-09 16:55:22 +01001438 rc = smc_cdc_init();
1439 if (rc) {
1440 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1441 goto out_pnet;
1442 }
1443
Ursula Braunac713872017-01-09 16:55:13 +01001444 rc = proto_register(&smc_proto, 1);
1445 if (rc) {
1446 pr_err("%s: proto_register fails with %d\n", __func__, rc);
Thomas Richter6812baa2017-01-09 16:55:15 +01001447 goto out_pnet;
Ursula Braunac713872017-01-09 16:55:13 +01001448 }
1449
1450 rc = sock_register(&smc_sock_family_ops);
1451 if (rc) {
1452 pr_err("%s: sock_register fails with %d\n", __func__, rc);
1453 goto out_proto;
1454 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01001455 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01001456
Ursula Brauna4cf0442017-01-09 16:55:14 +01001457 rc = smc_ib_register_client();
1458 if (rc) {
1459 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1460 goto out_sock;
1461 }
1462
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001463 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01001464 return 0;
1465
Ursula Brauna4cf0442017-01-09 16:55:14 +01001466out_sock:
1467 sock_unregister(PF_SMC);
Ursula Braunac713872017-01-09 16:55:13 +01001468out_proto:
1469 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001470out_pnet:
1471 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001472 return rc;
1473}
1474
1475static void __exit smc_exit(void)
1476{
Ursula Braun0cfdd8f2017-01-09 16:55:17 +01001477 struct smc_link_group *lgr, *lg;
1478 LIST_HEAD(lgr_freeing_list);
1479
1480 spin_lock_bh(&smc_lgr_list.lock);
1481 if (!list_empty(&smc_lgr_list.list))
1482 list_splice_init(&smc_lgr_list.list, &lgr_freeing_list);
1483 spin_unlock_bh(&smc_lgr_list.lock);
1484 list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) {
1485 list_del_init(&lgr->list);
1486 smc_lgr_free(lgr); /* free link group */
1487 }
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001488 static_branch_disable(&tcp_have_smc);
Ursula Brauna4cf0442017-01-09 16:55:14 +01001489 smc_ib_unregister_client();
Ursula Braunac713872017-01-09 16:55:13 +01001490 sock_unregister(PF_SMC);
1491 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001492 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001493}
1494
1495module_init(smc_init);
1496module_exit(smc_exit);
1497
1498MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
1499MODULE_DESCRIPTION("smc socket address family");
1500MODULE_LICENSE("GPL");
1501MODULE_ALIAS_NETPROTO(PF_SMC);