blob: cbbb947dbfcf4e1942ce5ab5d0226715b9a9accd [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
33#include "smc.h"
Ursula Brauna046d572017-01-09 16:55:16 +010034#include "smc_clc.h"
Ursula Braun9bf9abe2017-01-09 16:55:21 +010035#include "smc_llc.h"
Ursula Braun5f083182017-01-09 16:55:22 +010036#include "smc_cdc.h"
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010037#include "smc_core.h"
Ursula Brauna4cf0442017-01-09 16:55:14 +010038#include "smc_ib.h"
Hans Wippel41349842018-06-28 19:05:11 +020039#include "smc_ism.h"
Thomas Richter6812baa2017-01-09 16:55:15 +010040#include "smc_pnet.h"
Ursula Braune6727f32017-01-09 16:55:23 +010041#include "smc_tx.h"
Ursula Braun952310c2017-01-09 16:55:24 +010042#include "smc_rx.h"
Ursula Braunb38d7322017-01-09 16:55:25 +010043#include "smc_close.h"
Ursula Braunac713872017-01-09 16:55:13 +010044
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010045static DEFINE_MUTEX(smc_create_lgr_pending); /* serialize link group
46 * creation
47 */
48
Ursula Brauna046d572017-01-09 16:55:16 +010049static void smc_tcp_listen_work(struct work_struct *);
50
Ursula Braunac713872017-01-09 16:55:13 +010051static void smc_set_keepalive(struct sock *sk, int val)
52{
53 struct smc_sock *smc = smc_sk(sk);
54
55 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
56}
57
Ursula Braunf16a7dd2017-01-09 16:55:26 +010058static struct smc_hashinfo smc_v4_hashinfo = {
59 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
60};
61
Karsten Graulaaa4d332018-03-16 15:06:41 +010062static struct smc_hashinfo smc_v6_hashinfo = {
63 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
64};
65
Ursula Braunf16a7dd2017-01-09 16:55:26 +010066int smc_hash_sk(struct sock *sk)
67{
68 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
69 struct hlist_head *head;
70
71 head = &h->ht;
72
73 write_lock_bh(&h->lock);
74 sk_add_node(sk, head);
75 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
76 write_unlock_bh(&h->lock);
77
78 return 0;
79}
80EXPORT_SYMBOL_GPL(smc_hash_sk);
81
82void smc_unhash_sk(struct sock *sk)
83{
84 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
85
86 write_lock_bh(&h->lock);
87 if (sk_del_node_init(sk))
88 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
89 write_unlock_bh(&h->lock);
90}
91EXPORT_SYMBOL_GPL(smc_unhash_sk);
92
93struct proto smc_proto = {
Ursula Braunac713872017-01-09 16:55:13 +010094 .name = "SMC",
95 .owner = THIS_MODULE,
96 .keepalive = smc_set_keepalive,
Ursula Braunf16a7dd2017-01-09 16:55:26 +010097 .hash = smc_hash_sk,
98 .unhash = smc_unhash_sk,
Ursula Braunac713872017-01-09 16:55:13 +010099 .obj_size = sizeof(struct smc_sock),
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100100 .h.smc_hash = &smc_v4_hashinfo,
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800101 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Ursula Braunac713872017-01-09 16:55:13 +0100102};
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100103EXPORT_SYMBOL_GPL(smc_proto);
Ursula Braunac713872017-01-09 16:55:13 +0100104
Karsten Graulaaa4d332018-03-16 15:06:41 +0100105struct proto smc_proto6 = {
106 .name = "SMC6",
107 .owner = THIS_MODULE,
108 .keepalive = smc_set_keepalive,
109 .hash = smc_hash_sk,
110 .unhash = smc_unhash_sk,
111 .obj_size = sizeof(struct smc_sock),
112 .h.smc_hash = &smc_v6_hashinfo,
113 .slab_flags = SLAB_TYPESAFE_BY_RCU,
114};
115EXPORT_SYMBOL_GPL(smc_proto6);
116
Ursula Braunac713872017-01-09 16:55:13 +0100117static int smc_release(struct socket *sock)
118{
119 struct sock *sk = sock->sk;
120 struct smc_sock *smc;
Ursula Braunb38d7322017-01-09 16:55:25 +0100121 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +0100122
123 if (!sk)
124 goto out;
125
126 smc = smc_sk(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100127 if (sk->sk_state == SMC_LISTEN)
128 /* smc_close_non_accepted() is called and acquires
129 * sock lock for child sockets again
130 */
131 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
132 else
133 lock_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +0100134
Ursula Braun51f1de72018-01-26 09:28:48 +0100135 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100136 rc = smc_close_active(smc);
137 sock_set_flag(sk, SOCK_DEAD);
138 sk->sk_shutdown |= SHUTDOWN_MASK;
139 }
Ursula Braunac713872017-01-09 16:55:13 +0100140 if (smc->clcsock) {
141 sock_release(smc->clcsock);
142 smc->clcsock = NULL;
143 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100144 if (smc->use_fallback) {
145 sock_put(sk); /* passive closing */
146 sk->sk_state = SMC_CLOSED;
147 sk->sk_state_change(sk);
148 }
Ursula Braunac713872017-01-09 16:55:13 +0100149
150 /* detach socket */
151 sock_orphan(sk);
152 sock->sk = NULL;
Ursula Braun51f1de72018-01-26 09:28:48 +0100153 if (!smc->use_fallback && sk->sk_state == SMC_CLOSED)
Ursula Braunb38d7322017-01-09 16:55:25 +0100154 smc_conn_free(&smc->conn);
Ursula Braunac713872017-01-09 16:55:13 +0100155 release_sock(sk);
156
Ursula Braun51f1de72018-01-26 09:28:48 +0100157 sk->sk_prot->unhash(sk);
158 sock_put(sk); /* final sock_put */
Ursula Braunac713872017-01-09 16:55:13 +0100159out:
Ursula Braunb38d7322017-01-09 16:55:25 +0100160 return rc;
Ursula Braunac713872017-01-09 16:55:13 +0100161}
162
163static void smc_destruct(struct sock *sk)
164{
165 if (sk->sk_state != SMC_CLOSED)
166 return;
167 if (!sock_flag(sk, SOCK_DEAD))
168 return;
169
170 sk_refcnt_debug_dec(sk);
171}
172
Karsten Graulaaa4d332018-03-16 15:06:41 +0100173static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
174 int protocol)
Ursula Braunac713872017-01-09 16:55:13 +0100175{
176 struct smc_sock *smc;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100177 struct proto *prot;
Ursula Braunac713872017-01-09 16:55:13 +0100178 struct sock *sk;
179
Karsten Graulaaa4d332018-03-16 15:06:41 +0100180 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
181 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
Ursula Braunac713872017-01-09 16:55:13 +0100182 if (!sk)
183 return NULL;
184
185 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
186 sk->sk_state = SMC_INIT;
187 sk->sk_destruct = smc_destruct;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100188 sk->sk_protocol = protocol;
Ursula Braunac713872017-01-09 16:55:13 +0100189 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100190 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700191 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
Ursula Brauna046d572017-01-09 16:55:16 +0100192 INIT_LIST_HEAD(&smc->accept_q);
193 spin_lock_init(&smc->accept_q_lock);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700194 spin_lock_init(&smc->conn.send_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100195 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100196 sk_refcnt_debug_inc(sk);
Ursula Braunac713872017-01-09 16:55:13 +0100197
198 return sk;
199}
200
201static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
202 int addr_len)
203{
204 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
205 struct sock *sk = sock->sk;
206 struct smc_sock *smc;
207 int rc;
208
209 smc = smc_sk(sk);
210
211 /* replicate tests from inet_bind(), to be safe wrt. future changes */
212 rc = -EINVAL;
213 if (addr_len < sizeof(struct sockaddr_in))
214 goto out;
215
216 rc = -EAFNOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100217 if (addr->sin_family != AF_INET &&
218 addr->sin_family != AF_INET6 &&
219 addr->sin_family != AF_UNSPEC)
220 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100221 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
Karsten Graulaaa4d332018-03-16 15:06:41 +0100222 if (addr->sin_family == AF_UNSPEC &&
223 addr->sin_addr.s_addr != htonl(INADDR_ANY))
Ursula Braunac713872017-01-09 16:55:13 +0100224 goto out;
225
226 lock_sock(sk);
227
228 /* Check if socket is already active */
229 rc = -EINVAL;
230 if (sk->sk_state != SMC_INIT)
231 goto out_rel;
232
233 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
234 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
235
236out_rel:
237 release_sock(sk);
238out:
239 return rc;
240}
241
242static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
243 unsigned long mask)
244{
245 /* options we don't get control via setsockopt for */
246 nsk->sk_type = osk->sk_type;
247 nsk->sk_sndbuf = osk->sk_sndbuf;
248 nsk->sk_rcvbuf = osk->sk_rcvbuf;
249 nsk->sk_sndtimeo = osk->sk_sndtimeo;
250 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
251 nsk->sk_mark = osk->sk_mark;
252 nsk->sk_priority = osk->sk_priority;
253 nsk->sk_rcvlowat = osk->sk_rcvlowat;
254 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
255 nsk->sk_err = osk->sk_err;
256
257 nsk->sk_flags &= ~mask;
258 nsk->sk_flags |= osk->sk_flags & mask;
259}
260
261#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
262 (1UL << SOCK_KEEPOPEN) | \
263 (1UL << SOCK_LINGER) | \
264 (1UL << SOCK_BROADCAST) | \
265 (1UL << SOCK_TIMESTAMP) | \
266 (1UL << SOCK_DBG) | \
267 (1UL << SOCK_RCVTSTAMP) | \
268 (1UL << SOCK_RCVTSTAMPNS) | \
269 (1UL << SOCK_LOCALROUTE) | \
270 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
271 (1UL << SOCK_RXQ_OVFL) | \
272 (1UL << SOCK_WIFI_STATUS) | \
273 (1UL << SOCK_NOFCS) | \
274 (1UL << SOCK_FILTER_LOCKED))
275/* copy only relevant settings and flags of SOL_SOCKET level from smc to
276 * clc socket (since smc is not called for these options from net/core)
277 */
278static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
279{
280 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
281}
282
283#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
284 (1UL << SOCK_KEEPOPEN) | \
285 (1UL << SOCK_LINGER) | \
286 (1UL << SOCK_DBG))
287/* copy only settings and flags relevant for smc from clc to smc socket */
288static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
289{
290 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
291}
292
Karsten Graul44aa81c2018-05-15 17:04:55 +0200293/* register a new rmb, optionally send confirm_rkey msg to register with peer */
294static int smc_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc,
295 bool conf_rkey)
Karsten Graule63a5f82018-05-03 17:57:37 +0200296{
297 /* register memory region for new rmb */
Karsten Graula6920d12018-05-03 17:57:38 +0200298 if (smc_wr_reg_send(link, rmb_desc->mr_rx[SMC_SINGLE_LINK])) {
299 rmb_desc->regerr = 1;
Karsten Graule63a5f82018-05-03 17:57:37 +0200300 return -EFAULT;
Karsten Graula6920d12018-05-03 17:57:38 +0200301 }
Karsten Graul44aa81c2018-05-15 17:04:55 +0200302 if (!conf_rkey)
303 return 0;
304 /* exchange confirm_rkey msg with peer */
305 if (smc_llc_do_confirm_rkey(link, rmb_desc)) {
306 rmb_desc->regerr = 1;
307 return -EFAULT;
308 }
Karsten Graule63a5f82018-05-03 17:57:37 +0200309 return 0;
310}
311
Stefan Raspl0f627122018-03-01 13:51:26 +0100312static int smc_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100313{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200314 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100315 struct smc_link_group *lgr = smc->conn.lgr;
316 struct smc_link *link;
317 int rest;
318 int rc;
319
320 link = &lgr->lnk[SMC_SINGLE_LINK];
321 /* receive CONFIRM LINK request from server over RoCE fabric */
322 rest = wait_for_completion_interruptible_timeout(
323 &link->llc_confirm,
324 SMC_LLC_WAIT_FIRST_TIME);
325 if (rest <= 0) {
326 struct smc_clc_msg_decline dclc;
327
328 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
329 SMC_CLC_DECLINE);
330 return rc;
331 }
332
Karsten Graul75d320d2018-03-01 13:51:31 +0100333 if (link->llc_confirm_rc)
334 return SMC_CLC_DECL_RMBE_EC;
335
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100336 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
Karsten Graul44aa81c2018-05-15 17:04:55 +0200342 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Ursula Braun652a1e42017-07-28 13:56:17 +0200343 return SMC_CLC_DECL_INTERR;
344
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100345 /* send CONFIRM LINK response over RoCE fabric */
346 rc = smc_llc_send_confirm_link(link,
347 link->smcibdev->mac[link->ibport - 1],
Stefan Raspl0f627122018-03-01 13:51:26 +0100348 &link->smcibdev->gid[link->ibport - 1],
349 SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100350 if (rc < 0)
351 return SMC_CLC_DECL_TCL;
352
Karsten Graul52bedf32018-03-01 13:51:32 +0100353 /* receive ADD LINK request from server over RoCE fabric */
354 rest = wait_for_completion_interruptible_timeout(&link->llc_add,
355 SMC_LLC_WAIT_TIME);
356 if (rest <= 0) {
357 struct smc_clc_msg_decline dclc;
358
359 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
360 SMC_CLC_DECLINE);
361 return rc;
362 }
363
364 /* send add link reject message, only one link supported for now */
365 rc = smc_llc_send_add_link(link,
366 link->smcibdev->mac[link->ibport - 1],
367 &link->smcibdev->gid[link->ibport - 1],
368 SMC_LLC_RESP);
369 if (rc < 0)
370 return SMC_CLC_DECL_TCL;
371
Karsten Graul877ae5b2018-05-02 16:56:44 +0200372 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100373
Karsten Graul75d320d2018-03-01 13:51:31 +0100374 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100375}
376
Hans Wippel41349842018-06-28 19:05:11 +0200377static void smcr_conn_save_peer_info(struct smc_sock *smc,
378 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100379{
Hans Wippel95d8d2632018-05-18 09:34:13 +0200380 int bufsize = smc_uncompress_bufsize(clc->rmbe_size);
381
Hans Wippel92a138e2018-05-18 09:34:12 +0200382 smc->conn.peer_rmbe_idx = clc->rmbe_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100383 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200384 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100385 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200386 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100387}
388
Hans Wippel41349842018-06-28 19:05:11 +0200389static void smcd_conn_save_peer_info(struct smc_sock *smc,
390 struct smc_clc_msg_accept_confirm *clc)
391{
392 int bufsize = smc_uncompress_bufsize(clc->dmbe_size);
393
394 smc->conn.peer_rmbe_idx = clc->dmbe_idx;
395 smc->conn.peer_token = clc->token;
396 /* msg header takes up space in the buffer */
397 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
398 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
399 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
400}
401
402static void smc_conn_save_peer_info(struct smc_sock *smc,
403 struct smc_clc_msg_accept_confirm *clc)
404{
405 if (smc->conn.lgr->is_smcd)
406 smcd_conn_save_peer_info(smc, clc);
407 else
408 smcr_conn_save_peer_info(smc, clc);
409}
410
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100411static void smc_link_save_peer_info(struct smc_link *link,
412 struct smc_clc_msg_accept_confirm *clc)
413{
414 link->peer_qpn = ntoh24(clc->qpn);
415 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
416 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
417 link->peer_psn = ntoh24(clc->psn);
418 link->peer_mtu = clc->qp_mtu;
419}
420
Hans Wippel3b2dec22018-05-18 09:34:18 +0200421/* fall back during connect */
422static int smc_connect_fallback(struct smc_sock *smc)
423{
424 smc->use_fallback = true;
425 smc_copy_sock_settings_to_clc(smc);
426 if (smc->sk.sk_state == SMC_INIT)
427 smc->sk.sk_state = SMC_ACTIVE;
428 return 0;
429}
430
431/* decline and fall back during connect */
432static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
433{
434 int rc;
435
436 if (reason_code < 0) /* error, fallback is not possible */
437 return reason_code;
438 if (reason_code != SMC_CLC_DECL_REPLY) {
439 rc = smc_clc_send_decline(smc, reason_code);
440 if (rc < 0)
441 return rc;
442 }
443 return smc_connect_fallback(smc);
444}
445
446/* abort connecting */
447static int smc_connect_abort(struct smc_sock *smc, int reason_code,
448 int local_contact)
449{
450 if (local_contact == SMC_FIRST_CONTACT)
451 smc_lgr_forget(smc->conn.lgr);
452 mutex_unlock(&smc_create_lgr_pending);
453 smc_conn_free(&smc->conn);
454 if (reason_code < 0 && smc->sk.sk_state == SMC_INIT)
455 sock_put(&smc->sk); /* passive closing */
456 return reason_code;
457}
458
459/* check if there is a rdma device available for this connection. */
460/* called for connect and listen */
461static int smc_check_rdma(struct smc_sock *smc, struct smc_ib_device **ibdev,
462 u8 *ibport)
463{
464 int reason_code = 0;
465
466 /* PNET table look up: search active ib_device and port
467 * within same PNETID that also contains the ethernet device
468 * used for the internal TCP socket
469 */
470 smc_pnet_find_roce_resource(smc->clcsock->sk, ibdev, ibport);
471 if (!(*ibdev))
472 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
473
474 return reason_code;
475}
476
Hans Wippel41349842018-06-28 19:05:11 +0200477/* check if there is an ISM device available for this connection. */
478/* called for connect and listen */
479static int smc_check_ism(struct smc_sock *smc, struct smcd_dev **ismdev)
480{
481 /* Find ISM device with same PNETID as connecting interface */
482 smc_pnet_find_ism_resource(smc->clcsock->sk, ismdev);
483 if (!(*ismdev))
484 return SMC_CLC_DECL_CNFERR; /* configuration error */
485 return 0;
486}
487
488/* Check for VLAN ID and register it on ISM device just for CLC handshake */
489static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
490 struct smcd_dev *ismdev,
491 unsigned short vlan_id)
492{
493 if (vlan_id && smc_ism_get_vlan(ismdev, vlan_id))
494 return SMC_CLC_DECL_CNFERR;
495 return 0;
496}
497
498/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
499 * used, the VLAN ID will be registered again during the connection setup.
500 */
501static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
502 struct smcd_dev *ismdev,
503 unsigned short vlan_id)
504{
505 if (!is_smcd)
506 return 0;
507 if (vlan_id && smc_ism_put_vlan(ismdev, vlan_id))
508 return SMC_CLC_DECL_CNFERR;
509 return 0;
510}
511
Hans Wippel3b2dec22018-05-18 09:34:18 +0200512/* CLC handshake during connect */
Hans Wippelc758dfd2018-06-28 19:05:09 +0200513static int smc_connect_clc(struct smc_sock *smc, int smc_type,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200514 struct smc_clc_msg_accept_confirm *aclc,
Hans Wippel41349842018-06-28 19:05:11 +0200515 struct smc_ib_device *ibdev, u8 ibport,
516 struct smcd_dev *ismdev)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200517{
518 int rc = 0;
519
520 /* do inband token exchange */
Hans Wippel41349842018-06-28 19:05:11 +0200521 rc = smc_clc_send_proposal(smc, smc_type, ibdev, ibport, ismdev);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200522 if (rc)
523 return rc;
524 /* receive SMC Accept CLC message */
525 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT);
526}
527
Ursula Brauna046d572017-01-09 16:55:16 +0100528/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200529static int smc_connect_rdma(struct smc_sock *smc,
530 struct smc_clc_msg_accept_confirm *aclc,
531 struct smc_ib_device *ibdev, u8 ibport)
532{
533 int local_contact = SMC_FIRST_CONTACT;
534 struct smc_link *link;
535 int reason_code = 0;
536
537 mutex_lock(&smc_create_lgr_pending);
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200538 local_contact = smc_conn_create(smc, false, aclc->hdr.flag, ibdev,
539 ibport, &aclc->lcl, NULL, 0);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200540 if (local_contact < 0) {
541 if (local_contact == -ENOMEM)
542 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
543 else if (local_contact == -ENOLINK)
544 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
545 else
546 reason_code = SMC_CLC_DECL_INTERR; /* other error */
547 return smc_connect_abort(smc, reason_code, 0);
548 }
549 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
550
551 smc_conn_save_peer_info(smc, aclc);
552
553 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200554 if (smc_buf_create(smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +0200555 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, local_contact);
556
557 if (local_contact == SMC_FIRST_CONTACT)
558 smc_link_save_peer_info(link, aclc);
559
560 if (smc_rmb_rtoken_handling(&smc->conn, aclc))
561 return smc_connect_abort(smc, SMC_CLC_DECL_INTERR,
562 local_contact);
563
564 smc_close_init(smc);
565 smc_rx_init(smc);
566
567 if (local_contact == SMC_FIRST_CONTACT) {
568 if (smc_ib_ready_link(link))
569 return smc_connect_abort(smc, SMC_CLC_DECL_INTERR,
570 local_contact);
571 } else {
572 if (!smc->conn.rmb_desc->reused &&
573 smc_reg_rmb(link, smc->conn.rmb_desc, true))
574 return smc_connect_abort(smc, SMC_CLC_DECL_INTERR,
575 local_contact);
576 }
577 smc_rmb_sync_sg_for_device(&smc->conn);
578
579 reason_code = smc_clc_send_confirm(smc);
580 if (reason_code)
581 return smc_connect_abort(smc, reason_code, local_contact);
582
583 smc_tx_init(smc);
584
585 if (local_contact == SMC_FIRST_CONTACT) {
586 /* QP confirmation over RoCE fabric */
587 reason_code = smc_clnt_conf_first_link(smc);
588 if (reason_code)
589 return smc_connect_abort(smc, reason_code,
590 local_contact);
591 }
592 mutex_unlock(&smc_create_lgr_pending);
593
594 smc_copy_sock_settings_to_clc(smc);
595 if (smc->sk.sk_state == SMC_INIT)
596 smc->sk.sk_state = SMC_ACTIVE;
597
598 return 0;
599}
600
Hans Wippel41349842018-06-28 19:05:11 +0200601/* setup for ISM connection of client */
602static int smc_connect_ism(struct smc_sock *smc,
603 struct smc_clc_msg_accept_confirm *aclc,
604 struct smcd_dev *ismdev)
605{
606 int local_contact = SMC_FIRST_CONTACT;
607 int rc = 0;
608
609 mutex_lock(&smc_create_lgr_pending);
610 local_contact = smc_conn_create(smc, true, aclc->hdr.flag, NULL, 0,
611 NULL, ismdev, aclc->gid);
612 if (local_contact < 0)
613 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, 0);
614
615 /* Create send and receive buffers */
616 if (smc_buf_create(smc, true))
617 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, local_contact);
618
619 smc_conn_save_peer_info(smc, aclc);
620 smc_close_init(smc);
621 smc_rx_init(smc);
622 smc_tx_init(smc);
623
624 rc = smc_clc_send_confirm(smc);
625 if (rc)
626 return smc_connect_abort(smc, rc, local_contact);
627 mutex_unlock(&smc_create_lgr_pending);
628
629 smc_copy_sock_settings_to_clc(smc);
630 if (smc->sk.sk_state == SMC_INIT)
631 smc->sk.sk_state = SMC_ACTIVE;
632
633 return 0;
634}
635
Hans Wippel3b2dec22018-05-18 09:34:18 +0200636/* perform steps before actually connecting */
637static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100638{
Hans Wippel41349842018-06-28 19:05:11 +0200639 bool ism_supported = false, rdma_supported = false;
Ursula Brauna046d572017-01-09 16:55:16 +0100640 struct smc_clc_msg_accept_confirm aclc;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200641 struct smc_ib_device *ibdev;
Hans Wippel41349842018-06-28 19:05:11 +0200642 struct smcd_dev *ismdev;
643 unsigned short vlan;
644 int smc_type;
Ursula Brauna046d572017-01-09 16:55:16 +0100645 int rc = 0;
646 u8 ibport;
647
Ursula Braun51f1de72018-01-26 09:28:48 +0100648 sock_hold(&smc->sk); /* sock put in passive closing */
649
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200650 if (smc->use_fallback)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200651 return smc_connect_fallback(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200652
Hans Wippel3b2dec22018-05-18 09:34:18 +0200653 /* if peer has not signalled SMC-capability, fall back */
654 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
655 return smc_connect_fallback(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200656
Ursula Brauna046d572017-01-09 16:55:16 +0100657 /* IPSec connections opt out of SMC-R optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200658 if (using_ipsec(smc))
659 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
Ursula Brauna046d572017-01-09 16:55:16 +0100660
Hans Wippel41349842018-06-28 19:05:11 +0200661 /* check for VLAN ID */
662 if (smc_vlan_by_tcpsk(smc->clcsock, &vlan))
663 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_CNFERR);
664
665 /* check if there is an ism device available */
666 if (!smc_check_ism(smc, &ismdev) &&
667 !smc_connect_ism_vlan_setup(smc, ismdev, vlan)) {
668 /* ISM is supported for this connection */
669 ism_supported = true;
670 smc_type = SMC_TYPE_D;
671 }
672
673 /* check if there is a rdma device available */
674 if (!smc_check_rdma(smc, &ibdev, &ibport)) {
675 /* RDMA is supported for this connection */
676 rdma_supported = true;
677 if (ism_supported)
678 smc_type = SMC_TYPE_B; /* both */
679 else
680 smc_type = SMC_TYPE_R; /* only RDMA */
681 }
682
683 /* if neither ISM nor RDMA are supported, fallback */
684 if (!rdma_supported && !ism_supported)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200685 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_CNFERR);
Ursula Brauna046d572017-01-09 16:55:16 +0100686
Hans Wippel3b2dec22018-05-18 09:34:18 +0200687 /* perform CLC handshake */
Hans Wippel41349842018-06-28 19:05:11 +0200688 rc = smc_connect_clc(smc, smc_type, &aclc, ibdev, ibport, ismdev);
689 if (rc) {
690 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200691 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200692 }
Ursula Brauna046d572017-01-09 16:55:16 +0100693
Hans Wippel41349842018-06-28 19:05:11 +0200694 /* depending on previous steps, connect using rdma or ism */
695 if (rdma_supported && aclc.hdr.path == SMC_TYPE_R)
696 rc = smc_connect_rdma(smc, &aclc, ibdev, ibport);
697 else if (ism_supported && aclc.hdr.path == SMC_TYPE_D)
698 rc = smc_connect_ism(smc, &aclc, ismdev);
699 else
700 rc = SMC_CLC_DECL_CNFERR;
701 if (rc) {
702 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200703 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200704 }
Ursula Brauna046d572017-01-09 16:55:16 +0100705
Hans Wippel41349842018-06-28 19:05:11 +0200706 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200707 return 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100708}
709
Ursula Braunac713872017-01-09 16:55:13 +0100710static int smc_connect(struct socket *sock, struct sockaddr *addr,
711 int alen, int flags)
712{
713 struct sock *sk = sock->sk;
714 struct smc_sock *smc;
715 int rc = -EINVAL;
716
717 smc = smc_sk(sk);
718
719 /* separate smc parameter checking to be safe */
720 if (alen < sizeof(addr->sa_family))
721 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100722 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100723 goto out_err;
724
725 lock_sock(sk);
726 switch (sk->sk_state) {
727 default:
728 goto out;
729 case SMC_ACTIVE:
730 rc = -EISCONN;
731 goto out;
732 case SMC_INIT:
733 rc = 0;
734 break;
735 }
736
737 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200738 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +0100739 rc = kernel_connect(smc->clcsock, addr, alen, flags);
740 if (rc)
741 goto out;
742
Hans Wippel3b2dec22018-05-18 09:34:18 +0200743 rc = __smc_connect(smc);
Ursula Brauna046d572017-01-09 16:55:16 +0100744 if (rc < 0)
745 goto out;
746 else
747 rc = 0; /* success cases including fallback */
Ursula Braunac713872017-01-09 16:55:13 +0100748
749out:
750 release_sock(sk);
751out_err:
752 return rc;
753}
754
755static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
756{
Ursula Braun3163c502018-01-24 10:28:12 +0100757 struct socket *new_clcsock = NULL;
758 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100759 struct sock *new_sk;
760 int rc;
761
Ursula Braun3163c502018-01-24 10:28:12 +0100762 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100763 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100764 if (!new_sk) {
765 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100766 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100767 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100768 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100769 goto out;
770 }
771 *new_smc = smc_sk(new_sk);
772
773 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
Ursula Braun3163c502018-01-24 10:28:12 +0100774 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100775 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100776 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100777 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Brauna046d572017-01-09 16:55:16 +0100778 if (new_clcsock)
779 sock_release(new_clcsock);
780 new_sk->sk_state = SMC_CLOSED;
781 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun3163c502018-01-24 10:28:12 +0100782 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100783 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100784 *new_smc = NULL;
785 goto out;
786 }
787
788 (*new_smc)->clcsock = new_clcsock;
789out:
790 return rc;
791}
792
Ursula Brauna046d572017-01-09 16:55:16 +0100793/* add a just created sock to the accept queue of the listen sock as
794 * candidate for a following socket accept call from user space
795 */
796static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
797{
798 struct smc_sock *par = smc_sk(parent);
799
Ursula Braun51f1de72018-01-26 09:28:48 +0100800 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100801 spin_lock(&par->accept_q_lock);
802 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
803 spin_unlock(&par->accept_q_lock);
804 sk_acceptq_added(parent);
805}
806
807/* remove a socket from the accept queue of its parental listening socket */
808static void smc_accept_unlink(struct sock *sk)
809{
810 struct smc_sock *par = smc_sk(sk)->listen_smc;
811
812 spin_lock(&par->accept_q_lock);
813 list_del_init(&smc_sk(sk)->accept_q);
814 spin_unlock(&par->accept_q_lock);
815 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100816 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100817}
818
819/* remove a sock from the accept queue to bind it to a new socket created
820 * for a socket accept call from user space
821 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100822struct sock *smc_accept_dequeue(struct sock *parent,
823 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100824{
825 struct smc_sock *isk, *n;
826 struct sock *new_sk;
827
828 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
829 new_sk = (struct sock *)isk;
830
831 smc_accept_unlink(new_sk);
832 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braun127f4972018-01-26 09:28:49 +0100833 if (isk->clcsock) {
834 sock_release(isk->clcsock);
835 isk->clcsock = NULL;
836 }
Ursula Braun288c83902017-04-10 14:58:04 +0200837 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100838 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +0100839 continue;
840 }
841 if (new_sock)
842 sock_graft(new_sk, new_sock);
843 return new_sk;
844 }
845 return NULL;
846}
847
848/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +0100849void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +0100850{
851 struct smc_sock *smc = smc_sk(sk);
852
Ursula Braunb38d7322017-01-09 16:55:25 +0100853 lock_sock(sk);
854 if (!sk->sk_lingertime)
855 /* wait for peer closing */
856 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100857 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100858 smc_close_active(smc);
Ursula Braun288c83902017-04-10 14:58:04 +0200859 sock_set_flag(sk, SOCK_DEAD);
860 sk->sk_shutdown |= SHUTDOWN_MASK;
861 }
Ursula Brauna046d572017-01-09 16:55:16 +0100862 if (smc->clcsock) {
863 struct socket *tcp;
864
865 tcp = smc->clcsock;
866 smc->clcsock = NULL;
867 sock_release(tcp);
868 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100869 if (smc->use_fallback) {
Ursula Braun51f1de72018-01-26 09:28:48 +0100870 sock_put(sk); /* passive closing */
871 sk->sk_state = SMC_CLOSED;
872 } else {
873 if (sk->sk_state == SMC_CLOSED)
874 smc_conn_free(&smc->conn);
Ursula Braunb38d7322017-01-09 16:55:25 +0100875 }
876 release_sock(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100877 sk->sk_prot->unhash(sk);
878 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +0100879}
880
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100881static int smc_serv_conf_first_link(struct smc_sock *smc)
882{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200883 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100884 struct smc_link_group *lgr = smc->conn.lgr;
885 struct smc_link *link;
886 int rest;
887 int rc;
888
889 link = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun652a1e42017-07-28 13:56:17 +0200890
Karsten Graul44aa81c2018-05-15 17:04:55 +0200891 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Ursula Braun652a1e42017-07-28 13:56:17 +0200892 return SMC_CLC_DECL_INTERR;
893
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100894 /* send CONFIRM LINK request to client over the RoCE fabric */
895 rc = smc_llc_send_confirm_link(link,
896 link->smcibdev->mac[link->ibport - 1],
897 &link->smcibdev->gid[link->ibport - 1],
898 SMC_LLC_REQ);
899 if (rc < 0)
900 return SMC_CLC_DECL_TCL;
901
902 /* receive CONFIRM LINK response from client over the RoCE fabric */
903 rest = wait_for_completion_interruptible_timeout(
904 &link->llc_confirm_resp,
905 SMC_LLC_WAIT_FIRST_TIME);
906 if (rest <= 0) {
907 struct smc_clc_msg_decline dclc;
908
909 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
910 SMC_CLC_DECLINE);
Karsten Graul75d320d2018-03-01 13:51:31 +0100911 return rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100912 }
913
Karsten Graul75d320d2018-03-01 13:51:31 +0100914 if (link->llc_confirm_resp_rc)
915 return SMC_CLC_DECL_RMBE_EC;
916
Karsten Graul52bedf32018-03-01 13:51:32 +0100917 /* send ADD LINK request to client over the RoCE fabric */
918 rc = smc_llc_send_add_link(link,
919 link->smcibdev->mac[link->ibport - 1],
920 &link->smcibdev->gid[link->ibport - 1],
921 SMC_LLC_REQ);
922 if (rc < 0)
923 return SMC_CLC_DECL_TCL;
924
925 /* receive ADD LINK response from client over the RoCE fabric */
926 rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
927 SMC_LLC_WAIT_TIME);
928 if (rest <= 0) {
929 struct smc_clc_msg_decline dclc;
930
931 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
932 SMC_CLC_DECLINE);
933 return rc;
934 }
935
Karsten Graul877ae5b2018-05-02 16:56:44 +0200936 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100937
Karsten Graul75d320d2018-03-01 13:51:31 +0100938 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100939}
940
Hans Wippel3b2dec22018-05-18 09:34:18 +0200941/* listen worker: finish */
942static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100943{
Ursula Brauna046d572017-01-09 16:55:16 +0100944 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +0100945 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +0100946
Ursula Braunb38d7322017-01-09 16:55:25 +0100947 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +0100948 if (lsmc->sk.sk_state == SMC_LISTEN) {
949 smc_accept_enqueue(&lsmc->sk, newsmcsk);
950 } else { /* no longer listening */
951 smc_close_non_accepted(newsmcsk);
952 }
953 release_sock(&lsmc->sk);
954
955 /* Wake up accept */
956 lsmc->sk.sk_data_ready(&lsmc->sk);
957 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200958}
Ursula Brauna046d572017-01-09 16:55:16 +0100959
Hans Wippel3b2dec22018-05-18 09:34:18 +0200960/* listen worker: finish in state connected */
961static void smc_listen_out_connected(struct smc_sock *new_smc)
962{
963 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +0100964
Hans Wippel3b2dec22018-05-18 09:34:18 +0200965 sk_refcnt_debug_inc(newsmcsk);
966 if (newsmcsk->sk_state == SMC_INIT)
967 newsmcsk->sk_state = SMC_ACTIVE;
968
969 smc_listen_out(new_smc);
970}
971
972/* listen worker: finish in error state */
973static void smc_listen_out_err(struct smc_sock *new_smc)
974{
975 struct sock *newsmcsk = &new_smc->sk;
976
Ursula Braun51f1de72018-01-26 09:28:48 +0100977 if (newsmcsk->sk_state == SMC_INIT)
978 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +0100979 newsmcsk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +0100980 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200981
982 smc_listen_out(new_smc);
983}
984
985/* listen worker: decline and fall back if possible */
986static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
987 int local_contact)
988{
989 /* RDMA setup failed, switch back to TCP */
990 if (local_contact == SMC_FIRST_CONTACT)
991 smc_lgr_forget(new_smc->conn.lgr);
992 if (reason_code < 0) { /* error, no fallback possible */
993 smc_listen_out_err(new_smc);
994 return;
995 }
996 smc_conn_free(&new_smc->conn);
997 new_smc->use_fallback = true;
998 if (reason_code && reason_code != SMC_CLC_DECL_REPLY) {
999 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1000 smc_listen_out_err(new_smc);
1001 return;
1002 }
1003 }
1004 smc_listen_out_connected(new_smc);
1005}
1006
1007/* listen worker: check prefixes */
1008static int smc_listen_rdma_check(struct smc_sock *new_smc,
1009 struct smc_clc_msg_proposal *pclc)
1010{
1011 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1012 struct socket *newclcsock = new_smc->clcsock;
1013
1014 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1015 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
1016 return SMC_CLC_DECL_CNFERR;
1017
1018 return 0;
1019}
1020
1021/* listen worker: initialize connection and buffers */
1022static int smc_listen_rdma_init(struct smc_sock *new_smc,
1023 struct smc_clc_msg_proposal *pclc,
1024 struct smc_ib_device *ibdev, u8 ibport,
1025 int *local_contact)
1026{
1027 /* allocate connection / link group */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001028 *local_contact = smc_conn_create(new_smc, false, 0, ibdev, ibport,
1029 &pclc->lcl, NULL, 0);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001030 if (*local_contact < 0) {
1031 if (*local_contact == -ENOMEM)
1032 return SMC_CLC_DECL_MEM;/* insufficient memory*/
1033 return SMC_CLC_DECL_INTERR; /* other error */
1034 }
1035
1036 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001037 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001038 return SMC_CLC_DECL_MEM;
1039
1040 return 0;
1041}
1042
Hans Wippel41349842018-06-28 19:05:11 +02001043/* listen worker: initialize connection and buffers for SMC-D */
1044static int smc_listen_ism_init(struct smc_sock *new_smc,
1045 struct smc_clc_msg_proposal *pclc,
1046 struct smcd_dev *ismdev,
1047 int *local_contact)
1048{
1049 struct smc_clc_msg_smcd *pclc_smcd;
1050
1051 pclc_smcd = smc_get_clc_msg_smcd(pclc);
1052 *local_contact = smc_conn_create(new_smc, true, 0, NULL, 0, NULL,
1053 ismdev, pclc_smcd->gid);
1054 if (*local_contact < 0) {
1055 if (*local_contact == -ENOMEM)
1056 return SMC_CLC_DECL_MEM;/* insufficient memory*/
1057 return SMC_CLC_DECL_INTERR; /* other error */
1058 }
1059
1060 /* Check if peer can be reached via ISM device */
1061 if (smc_ism_cantalk(new_smc->conn.lgr->peer_gid,
1062 new_smc->conn.lgr->vlan_id,
1063 new_smc->conn.lgr->smcd)) {
1064 if (*local_contact == SMC_FIRST_CONTACT)
1065 smc_lgr_forget(new_smc->conn.lgr);
1066 smc_conn_free(&new_smc->conn);
1067 return SMC_CLC_DECL_CNFERR;
1068 }
1069
1070 /* Create send and receive buffers */
1071 if (smc_buf_create(new_smc, true)) {
1072 if (*local_contact == SMC_FIRST_CONTACT)
1073 smc_lgr_forget(new_smc->conn.lgr);
1074 smc_conn_free(&new_smc->conn);
1075 return SMC_CLC_DECL_MEM;
1076 }
1077
1078 return 0;
1079}
1080
Hans Wippel3b2dec22018-05-18 09:34:18 +02001081/* listen worker: register buffers */
1082static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
1083{
1084 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1085
1086 if (local_contact != SMC_FIRST_CONTACT) {
1087 if (!new_smc->conn.rmb_desc->reused) {
1088 if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true))
1089 return SMC_CLC_DECL_INTERR;
1090 }
1091 }
1092 smc_rmb_sync_sg_for_device(&new_smc->conn);
1093
1094 return 0;
1095}
1096
1097/* listen worker: finish RDMA setup */
1098static void smc_listen_rdma_finish(struct smc_sock *new_smc,
1099 struct smc_clc_msg_accept_confirm *cclc,
1100 int local_contact)
1101{
1102 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1103 int reason_code = 0;
1104
1105 if (local_contact == SMC_FIRST_CONTACT)
1106 smc_link_save_peer_info(link, cclc);
1107
1108 if (smc_rmb_rtoken_handling(&new_smc->conn, cclc)) {
1109 reason_code = SMC_CLC_DECL_INTERR;
1110 goto decline;
1111 }
1112
1113 if (local_contact == SMC_FIRST_CONTACT) {
1114 if (smc_ib_ready_link(link)) {
1115 reason_code = SMC_CLC_DECL_INTERR;
1116 goto decline;
1117 }
1118 /* QP confirmation over RoCE fabric */
1119 reason_code = smc_serv_conf_first_link(new_smc);
1120 if (reason_code)
1121 goto decline;
1122 }
1123 return;
1124
1125decline:
1126 mutex_unlock(&smc_create_lgr_pending);
1127 smc_listen_decline(new_smc, reason_code, local_contact);
1128}
1129
1130/* setup for RDMA connection of server */
1131static void smc_listen_work(struct work_struct *work)
1132{
1133 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1134 smc_listen_work);
1135 struct socket *newclcsock = new_smc->clcsock;
1136 struct smc_clc_msg_accept_confirm cclc;
1137 struct smc_clc_msg_proposal *pclc;
1138 struct smc_ib_device *ibdev;
Hans Wippel41349842018-06-28 19:05:11 +02001139 bool ism_supported = false;
1140 struct smcd_dev *ismdev;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001141 u8 buf[SMC_CLC_MAX_LEN];
1142 int local_contact = 0;
1143 int reason_code = 0;
1144 int rc = 0;
1145 u8 ibport;
1146
1147 if (new_smc->use_fallback) {
1148 smc_listen_out_connected(new_smc);
1149 return;
1150 }
1151
1152 /* check if peer is smc capable */
1153 if (!tcp_sk(newclcsock->sk)->syn_smc) {
1154 new_smc->use_fallback = true;
1155 smc_listen_out_connected(new_smc);
1156 return;
1157 }
1158
1159 /* do inband token exchange -
1160 * wait for and receive SMC Proposal CLC message
1161 */
1162 pclc = (struct smc_clc_msg_proposal *)&buf;
1163 reason_code = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
1164 SMC_CLC_PROPOSAL);
1165 if (reason_code) {
1166 smc_listen_decline(new_smc, reason_code, 0);
1167 return;
1168 }
1169
1170 /* IPSec connections opt out of SMC-R optimizations */
1171 if (using_ipsec(new_smc)) {
1172 smc_listen_decline(new_smc, SMC_CLC_DECL_IPSEC, 0);
1173 return;
1174 }
1175
1176 mutex_lock(&smc_create_lgr_pending);
1177 smc_close_init(new_smc);
1178 smc_rx_init(new_smc);
1179 smc_tx_init(new_smc);
1180
Hans Wippel41349842018-06-28 19:05:11 +02001181 /* check if ISM is available */
1182 if ((pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) &&
1183 !smc_check_ism(new_smc, &ismdev) &&
1184 !smc_listen_ism_init(new_smc, pclc, ismdev, &local_contact)) {
1185 ism_supported = true;
1186 }
1187
Hans Wippel3b2dec22018-05-18 09:34:18 +02001188 /* check if RDMA is available */
Hans Wippel41349842018-06-28 19:05:11 +02001189 if (!ism_supported &&
1190 ((pclc->hdr.path != SMC_TYPE_R && pclc->hdr.path != SMC_TYPE_B) ||
1191 smc_check_rdma(new_smc, &ibdev, &ibport) ||
1192 smc_listen_rdma_check(new_smc, pclc) ||
1193 smc_listen_rdma_init(new_smc, pclc, ibdev, ibport,
1194 &local_contact) ||
1195 smc_listen_rdma_reg(new_smc, local_contact))) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001196 /* SMC not supported, decline */
1197 mutex_unlock(&smc_create_lgr_pending);
1198 smc_listen_decline(new_smc, SMC_CLC_DECL_CNFERR, local_contact);
1199 return;
1200 }
1201
1202 /* send SMC Accept CLC message */
1203 rc = smc_clc_send_accept(new_smc, local_contact);
1204 if (rc) {
1205 mutex_unlock(&smc_create_lgr_pending);
1206 smc_listen_decline(new_smc, rc, local_contact);
1207 return;
1208 }
1209
1210 /* receive SMC Confirm CLC message */
1211 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
1212 SMC_CLC_CONFIRM);
1213 if (reason_code) {
1214 mutex_unlock(&smc_create_lgr_pending);
1215 smc_listen_decline(new_smc, reason_code, local_contact);
1216 return;
1217 }
1218
1219 /* finish worker */
Hans Wippel41349842018-06-28 19:05:11 +02001220 if (!ism_supported)
1221 smc_listen_rdma_finish(new_smc, &cclc, local_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001222 smc_conn_save_peer_info(new_smc, &cclc);
1223 mutex_unlock(&smc_create_lgr_pending);
1224 smc_listen_out_connected(new_smc);
Ursula Brauna046d572017-01-09 16:55:16 +01001225}
1226
1227static void smc_tcp_listen_work(struct work_struct *work)
1228{
1229 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1230 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001231 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001232 struct smc_sock *new_smc;
1233 int rc = 0;
1234
Ursula Braun3163c502018-01-24 10:28:12 +01001235 lock_sock(lsk);
1236 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001237 rc = smc_clcsock_accept(lsmc, &new_smc);
1238 if (rc)
1239 goto out;
1240 if (!new_smc)
1241 continue;
1242
1243 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001244 new_smc->use_fallback = lsmc->use_fallback;
Ursula Braun3163c502018-01-24 10:28:12 +01001245 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001246 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1247 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braun51f1de72018-01-26 09:28:48 +01001248 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1249 if (!schedule_work(&new_smc->smc_listen_work))
1250 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001251 }
1252
1253out:
Ursula Braun3163c502018-01-24 10:28:12 +01001254 release_sock(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001255 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +01001256}
1257
Ursula Braunac713872017-01-09 16:55:13 +01001258static int smc_listen(struct socket *sock, int backlog)
1259{
1260 struct sock *sk = sock->sk;
1261 struct smc_sock *smc;
1262 int rc;
1263
1264 smc = smc_sk(sk);
1265 lock_sock(sk);
1266
1267 rc = -EINVAL;
1268 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
1269 goto out;
1270
1271 rc = 0;
1272 if (sk->sk_state == SMC_LISTEN) {
1273 sk->sk_max_ack_backlog = backlog;
1274 goto out;
1275 }
1276 /* some socket options are handled in core, so we could not apply
1277 * them to the clc socket -- copy smc socket options to clc socket
1278 */
1279 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001280 if (!smc->use_fallback)
1281 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001282
1283 rc = kernel_listen(smc->clcsock, backlog);
1284 if (rc)
1285 goto out;
1286 sk->sk_max_ack_backlog = backlog;
1287 sk->sk_ack_backlog = 0;
1288 sk->sk_state = SMC_LISTEN;
Ursula Brauna046d572017-01-09 16:55:16 +01001289 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun51f1de72018-01-26 09:28:48 +01001290 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1291 if (!schedule_work(&smc->tcp_listen_work))
1292 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001293
1294out:
1295 release_sock(sk);
1296 return rc;
1297}
1298
1299static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001300 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001301{
Ursula Brauna046d572017-01-09 16:55:16 +01001302 struct sock *sk = sock->sk, *nsk;
1303 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001304 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001305 long timeo;
1306 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001307
1308 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001309 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001310 lock_sock(sk);
1311
1312 if (lsmc->sk.sk_state != SMC_LISTEN) {
1313 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001314 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001315 goto out;
1316 }
1317
Ursula Brauna046d572017-01-09 16:55:16 +01001318 /* Wait for an incoming connection */
1319 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1320 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1321 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1322 set_current_state(TASK_INTERRUPTIBLE);
1323 if (!timeo) {
1324 rc = -EAGAIN;
1325 break;
1326 }
1327 release_sock(sk);
1328 timeo = schedule_timeout(timeo);
1329 /* wakeup by sk_data_ready in smc_listen_work() */
1330 sched_annotate_sleep();
1331 lock_sock(sk);
1332 if (signal_pending(current)) {
1333 rc = sock_intr_errno(timeo);
1334 break;
1335 }
1336 }
1337 set_current_state(TASK_RUNNING);
1338 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001339
Ursula Brauna046d572017-01-09 16:55:16 +01001340 if (!rc)
1341 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001342 release_sock(sk);
1343 if (rc)
1344 goto out;
1345
1346 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1347 /* wait till data arrives on the socket */
1348 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1349 MSEC_PER_SEC);
1350 if (smc_sk(nsk)->use_fallback) {
1351 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1352
1353 lock_sock(clcsk);
1354 if (skb_queue_empty(&clcsk->sk_receive_queue))
1355 sk_wait_data(clcsk, &timeo, NULL);
1356 release_sock(clcsk);
1357 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1358 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001359 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001360 release_sock(nsk);
1361 }
1362 }
Ursula Braunac713872017-01-09 16:55:13 +01001363
1364out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001365 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001366 return rc;
1367}
1368
1369static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001370 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001371{
1372 struct smc_sock *smc;
1373
Ursula Braunb38d7322017-01-09 16:55:25 +01001374 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1375 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001376 return -ENOTCONN;
1377
1378 smc = smc_sk(sock->sk);
1379
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001380 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001381}
1382
1383static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1384{
1385 struct sock *sk = sock->sk;
1386 struct smc_sock *smc;
1387 int rc = -EPIPE;
1388
1389 smc = smc_sk(sk);
1390 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001391 if ((sk->sk_state != SMC_ACTIVE) &&
1392 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1393 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001394 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001395
1396 if (msg->msg_flags & MSG_FASTOPEN) {
1397 if (sk->sk_state == SMC_INIT) {
1398 smc->use_fallback = true;
1399 } else {
1400 rc = -EINVAL;
1401 goto out;
1402 }
1403 }
1404
Ursula Braunac713872017-01-09 16:55:13 +01001405 if (smc->use_fallback)
1406 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1407 else
Ursula Braune6727f32017-01-09 16:55:23 +01001408 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001409out:
1410 release_sock(sk);
1411 return rc;
1412}
1413
1414static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1415 int flags)
1416{
1417 struct sock *sk = sock->sk;
1418 struct smc_sock *smc;
1419 int rc = -ENOTCONN;
1420
1421 smc = smc_sk(sk);
1422 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001423 if ((sk->sk_state == SMC_INIT) ||
1424 (sk->sk_state == SMC_LISTEN) ||
1425 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001426 goto out;
1427
Ursula Braunb38d7322017-01-09 16:55:25 +01001428 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1429 rc = 0;
1430 goto out;
1431 }
1432
Stefan Raspl9014db22018-05-03 18:12:39 +02001433 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001434 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001435 } else {
1436 msg->msg_namelen = 0;
1437 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1438 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001439
Ursula Braunac713872017-01-09 16:55:13 +01001440out:
1441 release_sock(sk);
1442 return rc;
1443}
1444
Al Viroade994f2017-07-03 00:01:49 -04001445static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001446{
Ursula Braun8dce2782018-01-26 09:28:47 +01001447 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001448 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001449
Ursula Braun8dce2782018-01-26 09:28:47 +01001450 spin_lock(&isk->accept_q_lock);
1451 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001452 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001453 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001454
Ursula Braun8dce2782018-01-26 09:28:47 +01001455 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001456}
1457
Cong Wangc0129a02018-06-11 14:07:14 -07001458static __poll_t smc_poll_mask(struct socket *sock, __poll_t events)
Ursula Braunac713872017-01-09 16:55:13 +01001459{
1460 struct sock *sk = sock->sk;
Al Viroe6c8adc2017-07-03 22:25:56 -04001461 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001462 struct smc_sock *smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001463 int rc;
Ursula Braunac713872017-01-09 16:55:13 +01001464
Ursula Braun8dce2782018-01-26 09:28:47 +01001465 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001466 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001467
Ursula Braunac713872017-01-09 16:55:13 +01001468 smc = smc_sk(sock->sk);
Ursula Braun8dce2782018-01-26 09:28:47 +01001469 sock_hold(sk);
1470 lock_sock(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001471 if ((sk->sk_state == SMC_INIT) || smc->use_fallback) {
1472 /* delegate to CLC child sock */
Ursula Braun8dce2782018-01-26 09:28:47 +01001473 release_sock(sk);
Cong Wangc0129a02018-06-11 14:07:14 -07001474 mask = smc->clcsock->ops->poll_mask(smc->clcsock, events);
Ursula Braunac713872017-01-09 16:55:13 +01001475 lock_sock(sk);
Ursula Braun784813a2018-05-02 16:53:56 +02001476 sk->sk_err = smc->clcsock->sk->sk_err;
1477 if (sk->sk_err) {
1478 mask |= EPOLLERR;
1479 } else {
1480 /* if non-blocking connect finished ... */
1481 if (sk->sk_state == SMC_INIT &&
1482 mask & EPOLLOUT &&
1483 smc->clcsock->sk->sk_state != TCP_CLOSE) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001484 rc = __smc_connect(smc);
Ursula Brauna046d572017-01-09 16:55:16 +01001485 if (rc < 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001486 mask |= EPOLLERR;
Ursula Braun8dce2782018-01-26 09:28:47 +01001487 /* success cases including fallback */
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001488 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Brauna046d572017-01-09 16:55:16 +01001489 }
Ursula Braunac713872017-01-09 16:55:13 +01001490 }
Ursula Braunac713872017-01-09 16:55:13 +01001491 } else {
Ursula Brauna046d572017-01-09 16:55:16 +01001492 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001493 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001494 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1495 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001496 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001497 if (sk->sk_state == SMC_LISTEN) {
1498 /* woken up by sk_data_ready in smc_listen_work() */
1499 mask = smc_accept_poll(sk);
1500 } else {
1501 if (atomic_read(&smc->conn.sndbuf_space) ||
1502 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001503 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001504 } else {
1505 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1506 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1507 }
1508 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001509 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001510 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001511 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001512 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001513 mask |= EPOLLIN;
Ursula Braun8dce2782018-01-26 09:28:47 +01001514 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001515 if (smc->conn.urg_state == SMC_URG_VALID)
1516 mask |= EPOLLPRI;
Ursula Braunb38d7322017-01-09 16:55:25 +01001517
Ursula Braunac713872017-01-09 16:55:13 +01001518 }
Ursula Braun8dce2782018-01-26 09:28:47 +01001519 release_sock(sk);
1520 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001521
1522 return mask;
1523}
1524
1525static int smc_shutdown(struct socket *sock, int how)
1526{
1527 struct sock *sk = sock->sk;
1528 struct smc_sock *smc;
1529 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001530 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001531
1532 smc = smc_sk(sk);
1533
1534 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001535 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001536
1537 lock_sock(sk);
1538
1539 rc = -ENOTCONN;
Ursula Braunb38d7322017-01-09 16:55:25 +01001540 if ((sk->sk_state != SMC_LISTEN) &&
1541 (sk->sk_state != SMC_ACTIVE) &&
1542 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1543 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1544 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1545 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1546 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001547 goto out;
1548 if (smc->use_fallback) {
1549 rc = kernel_sock_shutdown(smc->clcsock, how);
1550 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1551 if (sk->sk_shutdown == SHUTDOWN_MASK)
1552 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001553 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001554 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001555 switch (how) {
1556 case SHUT_RDWR: /* shutdown in both directions */
1557 rc = smc_close_active(smc);
1558 break;
1559 case SHUT_WR:
1560 rc = smc_close_shutdown_write(smc);
1561 break;
1562 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001563 rc = 0;
1564 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001565 break;
1566 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001567 if (smc->clcsock)
1568 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001569 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1570 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001571
1572out:
1573 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001574 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001575}
1576
1577static int smc_setsockopt(struct socket *sock, int level, int optname,
1578 char __user *optval, unsigned int optlen)
1579{
1580 struct sock *sk = sock->sk;
1581 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001582 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001583
1584 smc = smc_sk(sk);
1585
1586 /* generic setsockopts reaching us here always apply to the
1587 * CLC socket
1588 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001589 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1590 optval, optlen);
1591 if (smc->clcsock->sk->sk_err) {
1592 sk->sk_err = smc->clcsock->sk->sk_err;
1593 sk->sk_error_report(sk);
1594 }
1595 if (rc)
1596 return rc;
1597
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001598 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00001599 return -EINVAL;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001600 get_user(val, (int __user *)optval);
1601
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001602 lock_sock(sk);
1603 switch (optname) {
1604 case TCP_ULP:
1605 case TCP_FASTOPEN:
1606 case TCP_FASTOPEN_CONNECT:
1607 case TCP_FASTOPEN_KEY:
1608 case TCP_FASTOPEN_NO_COOKIE:
1609 /* option not supported by SMC */
1610 if (sk->sk_state == SMC_INIT) {
1611 smc->use_fallback = true;
1612 } else {
1613 if (!smc->use_fallback)
1614 rc = -EINVAL;
1615 }
1616 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001617 case TCP_NODELAY:
1618 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001619 if (val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001620 mod_delayed_work(system_wq, &smc->conn.tx_work,
1621 0);
1622 }
1623 break;
1624 case TCP_CORK:
1625 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001626 if (!val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001627 mod_delayed_work(system_wq, &smc->conn.tx_work,
1628 0);
1629 }
1630 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001631 case TCP_DEFER_ACCEPT:
1632 smc->sockopt_defer_accept = val;
1633 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001634 default:
1635 break;
1636 }
1637 release_sock(sk);
1638
1639 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001640}
1641
1642static int smc_getsockopt(struct socket *sock, int level, int optname,
1643 char __user *optval, int __user *optlen)
1644{
1645 struct smc_sock *smc;
1646
1647 smc = smc_sk(sock->sk);
1648 /* socket options apply to the CLC socket */
1649 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1650 optval, optlen);
1651}
1652
1653static int smc_ioctl(struct socket *sock, unsigned int cmd,
1654 unsigned long arg)
1655{
Stefan Rasplde8474e2018-05-23 16:38:11 +02001656 union smc_host_cursor cons, urg;
1657 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01001658 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001659 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001660
1661 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001662 conn = &smc->conn;
Ursula Braun9b67e262018-05-02 16:56:46 +02001663 if (smc->use_fallback) {
1664 if (!smc->clcsock)
1665 return -EBADF;
Ursula Braunac713872017-01-09 16:55:13 +01001666 return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
Ursula Braun9b67e262018-05-02 16:56:46 +02001667 }
1668 switch (cmd) {
1669 case SIOCINQ: /* same as FIONREAD */
1670 if (smc->sk.sk_state == SMC_LISTEN)
1671 return -EINVAL;
Ursula Braun2351abe2018-05-23 16:38:09 +02001672 if (smc->sk.sk_state == SMC_INIT ||
1673 smc->sk.sk_state == SMC_CLOSED)
1674 answ = 0;
1675 else
1676 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02001677 break;
1678 case SIOCOUTQ:
1679 /* output queue size (not send + not acked) */
1680 if (smc->sk.sk_state == SMC_LISTEN)
1681 return -EINVAL;
Ursula Braun2351abe2018-05-23 16:38:09 +02001682 if (smc->sk.sk_state == SMC_INIT ||
1683 smc->sk.sk_state == SMC_CLOSED)
1684 answ = 0;
1685 else
1686 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02001687 atomic_read(&smc->conn.sndbuf_space);
1688 break;
1689 case SIOCOUTQNSD:
1690 /* output queue size (not send only) */
1691 if (smc->sk.sk_state == SMC_LISTEN)
1692 return -EINVAL;
Ursula Braun2351abe2018-05-23 16:38:09 +02001693 if (smc->sk.sk_state == SMC_INIT ||
1694 smc->sk.sk_state == SMC_CLOSED)
1695 answ = 0;
1696 else
1697 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02001698 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02001699 case SIOCATMARK:
1700 if (smc->sk.sk_state == SMC_LISTEN)
1701 return -EINVAL;
1702 if (smc->sk.sk_state == SMC_INIT ||
1703 smc->sk.sk_state == SMC_CLOSED) {
1704 answ = 0;
1705 } else {
1706 smc_curs_write(&cons,
1707 smc_curs_read(&conn->local_tx_ctrl.cons, conn),
1708 conn);
1709 smc_curs_write(&urg,
1710 smc_curs_read(&conn->urg_curs, conn),
1711 conn);
1712 answ = smc_curs_diff(conn->rmb_desc->len,
1713 &cons, &urg) == 1;
1714 }
1715 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02001716 default:
1717 return -ENOIOCTLCMD;
1718 }
1719
1720 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001721}
1722
1723static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1724 int offset, size_t size, int flags)
1725{
1726 struct sock *sk = sock->sk;
1727 struct smc_sock *smc;
1728 int rc = -EPIPE;
1729
1730 smc = smc_sk(sk);
1731 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001732 if (sk->sk_state != SMC_ACTIVE) {
1733 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001734 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001735 }
1736 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001737 if (smc->use_fallback)
1738 rc = kernel_sendpage(smc->clcsock, page, offset,
1739 size, flags);
1740 else
1741 rc = sock_no_sendpage(sock, page, offset, size, flags);
1742
1743out:
Ursula Braunac713872017-01-09 16:55:13 +01001744 return rc;
1745}
1746
Stefan Raspl9014db22018-05-03 18:12:39 +02001747/* Map the affected portions of the rmbe into an spd, note the number of bytes
1748 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1749 * updates till whenever a respective page has been fully processed.
1750 * Note that subsequent recv() calls have to wait till all splice() processing
1751 * completed.
1752 */
Ursula Braunac713872017-01-09 16:55:13 +01001753static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1754 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001755 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001756{
1757 struct sock *sk = sock->sk;
1758 struct smc_sock *smc;
1759 int rc = -ENOTCONN;
1760
1761 smc = smc_sk(sk);
1762 lock_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001763
1764 if (sk->sk_state == SMC_INIT ||
1765 sk->sk_state == SMC_LISTEN ||
1766 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001767 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001768
1769 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1770 rc = 0;
1771 goto out;
1772 }
1773
Ursula Braunac713872017-01-09 16:55:13 +01001774 if (smc->use_fallback) {
1775 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1776 pipe, len, flags);
1777 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001778 if (*ppos) {
1779 rc = -ESPIPE;
1780 goto out;
1781 }
1782 if (flags & SPLICE_F_NONBLOCK)
1783 flags = MSG_DONTWAIT;
1784 else
1785 flags = 0;
1786 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001787 }
1788out:
1789 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001790
Ursula Braunac713872017-01-09 16:55:13 +01001791 return rc;
1792}
1793
1794/* must look like tcp */
1795static const struct proto_ops smc_sock_ops = {
1796 .family = PF_SMC,
1797 .owner = THIS_MODULE,
1798 .release = smc_release,
1799 .bind = smc_bind,
1800 .connect = smc_connect,
1801 .socketpair = sock_no_socketpair,
1802 .accept = smc_accept,
1803 .getname = smc_getname,
Cong Wangc0129a02018-06-11 14:07:14 -07001804 .poll_mask = smc_poll_mask,
Ursula Braunac713872017-01-09 16:55:13 +01001805 .ioctl = smc_ioctl,
1806 .listen = smc_listen,
1807 .shutdown = smc_shutdown,
1808 .setsockopt = smc_setsockopt,
1809 .getsockopt = smc_getsockopt,
1810 .sendmsg = smc_sendmsg,
1811 .recvmsg = smc_recvmsg,
1812 .mmap = sock_no_mmap,
1813 .sendpage = smc_sendpage,
1814 .splice_read = smc_splice_read,
1815};
1816
1817static int smc_create(struct net *net, struct socket *sock, int protocol,
1818 int kern)
1819{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001820 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001821 struct smc_sock *smc;
1822 struct sock *sk;
1823 int rc;
1824
1825 rc = -ESOCKTNOSUPPORT;
1826 if (sock->type != SOCK_STREAM)
1827 goto out;
1828
1829 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001830 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01001831 goto out;
1832
1833 rc = -ENOBUFS;
1834 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001835 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001836 if (!sk)
1837 goto out;
1838
1839 /* create internal TCP socket for CLC handshake and fallback */
1840 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001841 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graulaaa4d332018-03-16 15:06:41 +01001842 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1843 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01001844 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01001845 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01001846 goto out;
1847 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01001848 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1849 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01001850
1851out:
1852 return rc;
1853}
1854
1855static const struct net_proto_family smc_sock_family_ops = {
1856 .family = PF_SMC,
1857 .owner = THIS_MODULE,
1858 .create = smc_create,
1859};
1860
1861static int __init smc_init(void)
1862{
1863 int rc;
1864
Thomas Richter6812baa2017-01-09 16:55:15 +01001865 rc = smc_pnet_init();
1866 if (rc)
1867 return rc;
1868
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001869 rc = smc_llc_init();
1870 if (rc) {
1871 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1872 goto out_pnet;
1873 }
1874
Ursula Braun5f083182017-01-09 16:55:22 +01001875 rc = smc_cdc_init();
1876 if (rc) {
1877 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1878 goto out_pnet;
1879 }
1880
Ursula Braunac713872017-01-09 16:55:13 +01001881 rc = proto_register(&smc_proto, 1);
1882 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01001883 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Thomas Richter6812baa2017-01-09 16:55:15 +01001884 goto out_pnet;
Ursula Braunac713872017-01-09 16:55:13 +01001885 }
1886
Karsten Graulaaa4d332018-03-16 15:06:41 +01001887 rc = proto_register(&smc_proto6, 1);
1888 if (rc) {
1889 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
1890 goto out_proto;
1891 }
1892
Ursula Braunac713872017-01-09 16:55:13 +01001893 rc = sock_register(&smc_sock_family_ops);
1894 if (rc) {
1895 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001896 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01001897 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01001898 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001899 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01001900
Ursula Brauna4cf0442017-01-09 16:55:14 +01001901 rc = smc_ib_register_client();
1902 if (rc) {
1903 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1904 goto out_sock;
1905 }
1906
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001907 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01001908 return 0;
1909
Ursula Brauna4cf0442017-01-09 16:55:14 +01001910out_sock:
1911 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001912out_proto6:
1913 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01001914out_proto:
1915 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001916out_pnet:
1917 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001918 return rc;
1919}
1920
1921static void __exit smc_exit(void)
1922{
Hans Wippel9fda3512018-05-18 09:34:11 +02001923 smc_core_exit();
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001924 static_branch_disable(&tcp_have_smc);
Ursula Brauna4cf0442017-01-09 16:55:14 +01001925 smc_ib_unregister_client();
Ursula Braunac713872017-01-09 16:55:13 +01001926 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001927 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01001928 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001929 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001930}
1931
1932module_init(smc_init);
1933module_exit(smc_exit);
1934
1935MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
1936MODULE_DESCRIPTION("smc socket address family");
1937MODULE_LICENSE("GPL");
1938MODULE_ALIAS_NETPROTO(PF_SMC);