blob: 4b865250e238e04bde688259c3559e04330feca0 [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 *);
Ursula Braun24ac3a02018-06-27 17:59:50 +020050static void smc_connect_work(struct work_struct *);
Ursula Brauna046d572017-01-09 16:55:16 +010051
Ursula Braunac713872017-01-09 16:55:13 +010052static void smc_set_keepalive(struct sock *sk, int val)
53{
54 struct smc_sock *smc = smc_sk(sk);
55
56 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
57}
58
Ursula Braunf16a7dd2017-01-09 16:55:26 +010059static struct smc_hashinfo smc_v4_hashinfo = {
60 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
61};
62
Karsten Graulaaa4d332018-03-16 15:06:41 +010063static struct smc_hashinfo smc_v6_hashinfo = {
64 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
65};
66
Ursula Braunf16a7dd2017-01-09 16:55:26 +010067int smc_hash_sk(struct sock *sk)
68{
69 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
70 struct hlist_head *head;
71
72 head = &h->ht;
73
74 write_lock_bh(&h->lock);
75 sk_add_node(sk, head);
76 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
77 write_unlock_bh(&h->lock);
78
79 return 0;
80}
81EXPORT_SYMBOL_GPL(smc_hash_sk);
82
83void smc_unhash_sk(struct sock *sk)
84{
85 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
86
87 write_lock_bh(&h->lock);
88 if (sk_del_node_init(sk))
89 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
90 write_unlock_bh(&h->lock);
91}
92EXPORT_SYMBOL_GPL(smc_unhash_sk);
93
94struct proto smc_proto = {
Ursula Braunac713872017-01-09 16:55:13 +010095 .name = "SMC",
96 .owner = THIS_MODULE,
97 .keepalive = smc_set_keepalive,
Ursula Braunf16a7dd2017-01-09 16:55:26 +010098 .hash = smc_hash_sk,
99 .unhash = smc_unhash_sk,
Ursula Braunac713872017-01-09 16:55:13 +0100100 .obj_size = sizeof(struct smc_sock),
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100101 .h.smc_hash = &smc_v4_hashinfo,
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800102 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Ursula Braunac713872017-01-09 16:55:13 +0100103};
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100104EXPORT_SYMBOL_GPL(smc_proto);
Ursula Braunac713872017-01-09 16:55:13 +0100105
Karsten Graulaaa4d332018-03-16 15:06:41 +0100106struct proto smc_proto6 = {
107 .name = "SMC6",
108 .owner = THIS_MODULE,
109 .keepalive = smc_set_keepalive,
110 .hash = smc_hash_sk,
111 .unhash = smc_unhash_sk,
112 .obj_size = sizeof(struct smc_sock),
113 .h.smc_hash = &smc_v6_hashinfo,
114 .slab_flags = SLAB_TYPESAFE_BY_RCU,
115};
116EXPORT_SYMBOL_GPL(smc_proto6);
117
Ursula Braunac713872017-01-09 16:55:13 +0100118static int smc_release(struct socket *sock)
119{
120 struct sock *sk = sock->sk;
121 struct smc_sock *smc;
Ursula Braunb38d7322017-01-09 16:55:25 +0100122 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +0100123
124 if (!sk)
125 goto out;
126
127 smc = smc_sk(sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200128
129 /* cleanup for a dangling non-blocking connect */
130 flush_work(&smc->connect_work);
131 kfree(smc->connect_info);
132 smc->connect_info = NULL;
133
Ursula Braunb38d7322017-01-09 16:55:25 +0100134 if (sk->sk_state == SMC_LISTEN)
135 /* smc_close_non_accepted() is called and acquires
136 * sock lock for child sockets again
137 */
138 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
139 else
140 lock_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +0100141
Ursula Braun51f1de72018-01-26 09:28:48 +0100142 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100143 rc = smc_close_active(smc);
144 sock_set_flag(sk, SOCK_DEAD);
145 sk->sk_shutdown |= SHUTDOWN_MASK;
146 }
Ursula Braunac713872017-01-09 16:55:13 +0100147 if (smc->clcsock) {
148 sock_release(smc->clcsock);
149 smc->clcsock = NULL;
150 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100151 if (smc->use_fallback) {
Ursula Braune1bbdd52018-07-05 16:15:30 +0200152 if (sk->sk_state != SMC_LISTEN && sk->sk_state != SMC_INIT)
153 sock_put(sk); /* passive closing */
Ursula Braun51f1de72018-01-26 09:28:48 +0100154 sk->sk_state = SMC_CLOSED;
155 sk->sk_state_change(sk);
156 }
Ursula Braunac713872017-01-09 16:55:13 +0100157
158 /* detach socket */
159 sock_orphan(sk);
160 sock->sk = NULL;
Ursula Braun51f1de72018-01-26 09:28:48 +0100161 if (!smc->use_fallback && sk->sk_state == SMC_CLOSED)
Ursula Braunb38d7322017-01-09 16:55:25 +0100162 smc_conn_free(&smc->conn);
Ursula Braunac713872017-01-09 16:55:13 +0100163 release_sock(sk);
164
Ursula Braun51f1de72018-01-26 09:28:48 +0100165 sk->sk_prot->unhash(sk);
166 sock_put(sk); /* final sock_put */
Ursula Braunac713872017-01-09 16:55:13 +0100167out:
Ursula Braunb38d7322017-01-09 16:55:25 +0100168 return rc;
Ursula Braunac713872017-01-09 16:55:13 +0100169}
170
171static void smc_destruct(struct sock *sk)
172{
173 if (sk->sk_state != SMC_CLOSED)
174 return;
175 if (!sock_flag(sk, SOCK_DEAD))
176 return;
177
178 sk_refcnt_debug_dec(sk);
179}
180
Karsten Graulaaa4d332018-03-16 15:06:41 +0100181static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
182 int protocol)
Ursula Braunac713872017-01-09 16:55:13 +0100183{
184 struct smc_sock *smc;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100185 struct proto *prot;
Ursula Braunac713872017-01-09 16:55:13 +0100186 struct sock *sk;
187
Karsten Graulaaa4d332018-03-16 15:06:41 +0100188 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
189 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
Ursula Braunac713872017-01-09 16:55:13 +0100190 if (!sk)
191 return NULL;
192
193 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
194 sk->sk_state = SMC_INIT;
195 sk->sk_destruct = smc_destruct;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100196 sk->sk_protocol = protocol;
Ursula Braunac713872017-01-09 16:55:13 +0100197 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100198 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200199 INIT_WORK(&smc->connect_work, smc_connect_work);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700200 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
Ursula Brauna046d572017-01-09 16:55:16 +0100201 INIT_LIST_HEAD(&smc->accept_q);
202 spin_lock_init(&smc->accept_q_lock);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700203 spin_lock_init(&smc->conn.send_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100204 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100205 sk_refcnt_debug_inc(sk);
Ursula Braunac713872017-01-09 16:55:13 +0100206
207 return sk;
208}
209
210static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
211 int addr_len)
212{
213 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
214 struct sock *sk = sock->sk;
215 struct smc_sock *smc;
216 int rc;
217
218 smc = smc_sk(sk);
219
220 /* replicate tests from inet_bind(), to be safe wrt. future changes */
221 rc = -EINVAL;
222 if (addr_len < sizeof(struct sockaddr_in))
223 goto out;
224
225 rc = -EAFNOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100226 if (addr->sin_family != AF_INET &&
227 addr->sin_family != AF_INET6 &&
228 addr->sin_family != AF_UNSPEC)
229 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100230 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
Karsten Graulaaa4d332018-03-16 15:06:41 +0100231 if (addr->sin_family == AF_UNSPEC &&
232 addr->sin_addr.s_addr != htonl(INADDR_ANY))
Ursula Braunac713872017-01-09 16:55:13 +0100233 goto out;
234
235 lock_sock(sk);
236
237 /* Check if socket is already active */
238 rc = -EINVAL;
239 if (sk->sk_state != SMC_INIT)
240 goto out_rel;
241
242 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
243 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
244
245out_rel:
246 release_sock(sk);
247out:
248 return rc;
249}
250
251static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
252 unsigned long mask)
253{
254 /* options we don't get control via setsockopt for */
255 nsk->sk_type = osk->sk_type;
256 nsk->sk_sndbuf = osk->sk_sndbuf;
257 nsk->sk_rcvbuf = osk->sk_rcvbuf;
258 nsk->sk_sndtimeo = osk->sk_sndtimeo;
259 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
260 nsk->sk_mark = osk->sk_mark;
261 nsk->sk_priority = osk->sk_priority;
262 nsk->sk_rcvlowat = osk->sk_rcvlowat;
263 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
264 nsk->sk_err = osk->sk_err;
265
266 nsk->sk_flags &= ~mask;
267 nsk->sk_flags |= osk->sk_flags & mask;
268}
269
270#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
271 (1UL << SOCK_KEEPOPEN) | \
272 (1UL << SOCK_LINGER) | \
273 (1UL << SOCK_BROADCAST) | \
274 (1UL << SOCK_TIMESTAMP) | \
275 (1UL << SOCK_DBG) | \
276 (1UL << SOCK_RCVTSTAMP) | \
277 (1UL << SOCK_RCVTSTAMPNS) | \
278 (1UL << SOCK_LOCALROUTE) | \
279 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
280 (1UL << SOCK_RXQ_OVFL) | \
281 (1UL << SOCK_WIFI_STATUS) | \
282 (1UL << SOCK_NOFCS) | \
283 (1UL << SOCK_FILTER_LOCKED))
284/* copy only relevant settings and flags of SOL_SOCKET level from smc to
285 * clc socket (since smc is not called for these options from net/core)
286 */
287static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
288{
289 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
290}
291
292#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
293 (1UL << SOCK_KEEPOPEN) | \
294 (1UL << SOCK_LINGER) | \
295 (1UL << SOCK_DBG))
296/* copy only settings and flags relevant for smc from clc to smc socket */
297static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
298{
299 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
300}
301
Karsten Graulc7674c02018-11-22 10:26:43 +0100302/* register a new rmb, send confirm_rkey msg to register with peer */
Karsten Graul44aa81c2018-05-15 17:04:55 +0200303static int smc_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc,
304 bool conf_rkey)
Karsten Graule63a5f82018-05-03 17:57:37 +0200305{
Karsten Graulc7674c02018-11-22 10:26:43 +0100306 if (!rmb_desc->wr_reg) {
307 /* register memory region for new rmb */
308 if (smc_wr_reg_send(link, rmb_desc->mr_rx[SMC_SINGLE_LINK])) {
309 rmb_desc->regerr = 1;
310 return -EFAULT;
311 }
312 rmb_desc->wr_reg = 1;
Karsten Graula6920d12018-05-03 17:57:38 +0200313 }
Karsten Graul44aa81c2018-05-15 17:04:55 +0200314 if (!conf_rkey)
315 return 0;
316 /* exchange confirm_rkey msg with peer */
317 if (smc_llc_do_confirm_rkey(link, rmb_desc)) {
318 rmb_desc->regerr = 1;
319 return -EFAULT;
320 }
Karsten Graule63a5f82018-05-03 17:57:37 +0200321 return 0;
322}
323
Stefan Raspl0f627122018-03-01 13:51:26 +0100324static int smc_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100325{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200326 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100327 struct smc_link_group *lgr = smc->conn.lgr;
328 struct smc_link *link;
329 int rest;
330 int rc;
331
332 link = &lgr->lnk[SMC_SINGLE_LINK];
333 /* receive CONFIRM LINK request from server over RoCE fabric */
334 rest = wait_for_completion_interruptible_timeout(
335 &link->llc_confirm,
336 SMC_LLC_WAIT_FIRST_TIME);
337 if (rest <= 0) {
338 struct smc_clc_msg_decline dclc;
339
340 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100341 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100342 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100343 }
344
Karsten Graul75d320d2018-03-01 13:51:31 +0100345 if (link->llc_confirm_rc)
346 return SMC_CLC_DECL_RMBE_EC;
347
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100348 rc = smc_ib_modify_qp_rts(link);
349 if (rc)
Karsten Graul603cc142018-07-25 16:35:32 +0200350 return SMC_CLC_DECL_ERR_RDYLNK;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100351
352 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200353
Karsten Graul44aa81c2018-05-15 17:04:55 +0200354 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200355 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200356
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100357 /* send CONFIRM LINK response over RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200358 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100359 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200360 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100361
Karsten Graul52bedf32018-03-01 13:51:32 +0100362 /* receive ADD LINK request from server over RoCE fabric */
363 rest = wait_for_completion_interruptible_timeout(&link->llc_add,
364 SMC_LLC_WAIT_TIME);
365 if (rest <= 0) {
366 struct smc_clc_msg_decline dclc;
367
368 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100369 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100370 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
Karsten Graul52bedf32018-03-01 13:51:32 +0100371 }
372
373 /* send add link reject message, only one link supported for now */
374 rc = smc_llc_send_add_link(link,
375 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +0200376 link->gid, SMC_LLC_RESP);
Karsten Graul52bedf32018-03-01 13:51:32 +0100377 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200378 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +0100379
Karsten Graul877ae5b2018-05-02 16:56:44 +0200380 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100381
Karsten Graul75d320d2018-03-01 13:51:31 +0100382 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100383}
384
Hans Wippel41349842018-06-28 19:05:11 +0200385static void smcr_conn_save_peer_info(struct smc_sock *smc,
386 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100387{
Hans Wippel95d8d2632018-05-18 09:34:13 +0200388 int bufsize = smc_uncompress_bufsize(clc->rmbe_size);
389
Hans Wippel92a138e2018-05-18 09:34:12 +0200390 smc->conn.peer_rmbe_idx = clc->rmbe_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100391 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200392 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100393 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200394 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100395}
396
Hans Wippel41349842018-06-28 19:05:11 +0200397static void smcd_conn_save_peer_info(struct smc_sock *smc,
398 struct smc_clc_msg_accept_confirm *clc)
399{
400 int bufsize = smc_uncompress_bufsize(clc->dmbe_size);
401
402 smc->conn.peer_rmbe_idx = clc->dmbe_idx;
403 smc->conn.peer_token = clc->token;
404 /* msg header takes up space in the buffer */
405 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
406 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
407 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
408}
409
410static void smc_conn_save_peer_info(struct smc_sock *smc,
411 struct smc_clc_msg_accept_confirm *clc)
412{
413 if (smc->conn.lgr->is_smcd)
414 smcd_conn_save_peer_info(smc, clc);
415 else
416 smcr_conn_save_peer_info(smc, clc);
417}
418
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100419static void smc_link_save_peer_info(struct smc_link *link,
420 struct smc_clc_msg_accept_confirm *clc)
421{
422 link->peer_qpn = ntoh24(clc->qpn);
423 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
424 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
425 link->peer_psn = ntoh24(clc->psn);
426 link->peer_mtu = clc->qp_mtu;
427}
428
Hans Wippel3b2dec22018-05-18 09:34:18 +0200429/* fall back during connect */
Karsten Graul603cc142018-07-25 16:35:32 +0200430static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200431{
432 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +0200433 smc->fallback_rsn = reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200434 smc_copy_sock_settings_to_clc(smc);
435 if (smc->sk.sk_state == SMC_INIT)
436 smc->sk.sk_state = SMC_ACTIVE;
437 return 0;
438}
439
440/* decline and fall back during connect */
441static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
442{
443 int rc;
444
Ursula Braune1bbdd52018-07-05 16:15:30 +0200445 if (reason_code < 0) { /* error, fallback is not possible */
446 if (smc->sk.sk_state == SMC_INIT)
447 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200448 return reason_code;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200449 }
Karsten Graul603cc142018-07-25 16:35:32 +0200450 if (reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200451 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200452 if (rc < 0) {
453 if (smc->sk.sk_state == SMC_INIT)
454 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200455 return rc;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200456 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200457 }
Karsten Graul603cc142018-07-25 16:35:32 +0200458 return smc_connect_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200459}
460
461/* abort connecting */
462static int smc_connect_abort(struct smc_sock *smc, int reason_code,
463 int local_contact)
464{
465 if (local_contact == SMC_FIRST_CONTACT)
466 smc_lgr_forget(smc->conn.lgr);
467 mutex_unlock(&smc_create_lgr_pending);
468 smc_conn_free(&smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200469 return reason_code;
470}
471
472/* check if there is a rdma device available for this connection. */
473/* called for connect and listen */
474static int smc_check_rdma(struct smc_sock *smc, struct smc_ib_device **ibdev,
Ursula Braun7005ada2018-07-25 16:35:31 +0200475 u8 *ibport, unsigned short vlan_id, u8 gid[])
Hans Wippel3b2dec22018-05-18 09:34:18 +0200476{
477 int reason_code = 0;
478
479 /* PNET table look up: search active ib_device and port
480 * within same PNETID that also contains the ethernet device
481 * used for the internal TCP socket
482 */
Ursula Braun7005ada2018-07-25 16:35:31 +0200483 smc_pnet_find_roce_resource(smc->clcsock->sk, ibdev, ibport, vlan_id,
484 gid);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200485 if (!(*ibdev))
486 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
487
488 return reason_code;
489}
490
Hans Wippel41349842018-06-28 19:05:11 +0200491/* check if there is an ISM device available for this connection. */
492/* called for connect and listen */
493static int smc_check_ism(struct smc_sock *smc, struct smcd_dev **ismdev)
494{
495 /* Find ISM device with same PNETID as connecting interface */
496 smc_pnet_find_ism_resource(smc->clcsock->sk, ismdev);
497 if (!(*ismdev))
498 return SMC_CLC_DECL_CNFERR; /* configuration error */
499 return 0;
500}
501
502/* Check for VLAN ID and register it on ISM device just for CLC handshake */
503static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
504 struct smcd_dev *ismdev,
505 unsigned short vlan_id)
506{
507 if (vlan_id && smc_ism_get_vlan(ismdev, vlan_id))
508 return SMC_CLC_DECL_CNFERR;
509 return 0;
510}
511
512/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
513 * used, the VLAN ID will be registered again during the connection setup.
514 */
515static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
516 struct smcd_dev *ismdev,
517 unsigned short vlan_id)
518{
519 if (!is_smcd)
520 return 0;
521 if (vlan_id && smc_ism_put_vlan(ismdev, vlan_id))
522 return SMC_CLC_DECL_CNFERR;
523 return 0;
524}
525
Hans Wippel3b2dec22018-05-18 09:34:18 +0200526/* CLC handshake during connect */
Hans Wippelc758dfd2018-06-28 19:05:09 +0200527static int smc_connect_clc(struct smc_sock *smc, int smc_type,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200528 struct smc_clc_msg_accept_confirm *aclc,
Hans Wippel41349842018-06-28 19:05:11 +0200529 struct smc_ib_device *ibdev, u8 ibport,
Ursula Braun7005ada2018-07-25 16:35:31 +0200530 u8 gid[], struct smcd_dev *ismdev)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200531{
532 int rc = 0;
533
534 /* do inband token exchange */
Ursula Braun7005ada2018-07-25 16:35:31 +0200535 rc = smc_clc_send_proposal(smc, smc_type, ibdev, ibport, gid, ismdev);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200536 if (rc)
537 return rc;
538 /* receive SMC Accept CLC message */
Ursula Braun2b59f582018-11-22 10:26:39 +0100539 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT,
540 CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200541}
542
Ursula Brauna046d572017-01-09 16:55:16 +0100543/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200544static int smc_connect_rdma(struct smc_sock *smc,
545 struct smc_clc_msg_accept_confirm *aclc,
546 struct smc_ib_device *ibdev, u8 ibport)
547{
548 int local_contact = SMC_FIRST_CONTACT;
549 struct smc_link *link;
550 int reason_code = 0;
551
552 mutex_lock(&smc_create_lgr_pending);
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200553 local_contact = smc_conn_create(smc, false, aclc->hdr.flag, ibdev,
554 ibport, &aclc->lcl, NULL, 0);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200555 if (local_contact < 0) {
556 if (local_contact == -ENOMEM)
557 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
558 else if (local_contact == -ENOLINK)
559 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
560 else
561 reason_code = SMC_CLC_DECL_INTERR; /* other error */
562 return smc_connect_abort(smc, reason_code, 0);
563 }
564 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
565
566 smc_conn_save_peer_info(smc, aclc);
567
568 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200569 if (smc_buf_create(smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +0200570 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, local_contact);
571
572 if (local_contact == SMC_FIRST_CONTACT)
573 smc_link_save_peer_info(link, aclc);
574
575 if (smc_rmb_rtoken_handling(&smc->conn, aclc))
Karsten Graul603cc142018-07-25 16:35:32 +0200576 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200577 local_contact);
578
579 smc_close_init(smc);
580 smc_rx_init(smc);
581
582 if (local_contact == SMC_FIRST_CONTACT) {
583 if (smc_ib_ready_link(link))
Karsten Graul603cc142018-07-25 16:35:32 +0200584 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200585 local_contact);
586 } else {
Karsten Graulc7674c02018-11-22 10:26:43 +0100587 if (smc_reg_rmb(link, smc->conn.rmb_desc, true))
Karsten Graul603cc142018-07-25 16:35:32 +0200588 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200589 local_contact);
590 }
591 smc_rmb_sync_sg_for_device(&smc->conn);
592
593 reason_code = smc_clc_send_confirm(smc);
594 if (reason_code)
595 return smc_connect_abort(smc, reason_code, local_contact);
596
597 smc_tx_init(smc);
598
599 if (local_contact == SMC_FIRST_CONTACT) {
600 /* QP confirmation over RoCE fabric */
601 reason_code = smc_clnt_conf_first_link(smc);
602 if (reason_code)
603 return smc_connect_abort(smc, reason_code,
604 local_contact);
605 }
606 mutex_unlock(&smc_create_lgr_pending);
607
608 smc_copy_sock_settings_to_clc(smc);
609 if (smc->sk.sk_state == SMC_INIT)
610 smc->sk.sk_state = SMC_ACTIVE;
611
612 return 0;
613}
614
Hans Wippel41349842018-06-28 19:05:11 +0200615/* setup for ISM connection of client */
616static int smc_connect_ism(struct smc_sock *smc,
617 struct smc_clc_msg_accept_confirm *aclc,
618 struct smcd_dev *ismdev)
619{
620 int local_contact = SMC_FIRST_CONTACT;
621 int rc = 0;
622
623 mutex_lock(&smc_create_lgr_pending);
624 local_contact = smc_conn_create(smc, true, aclc->hdr.flag, NULL, 0,
625 NULL, ismdev, aclc->gid);
626 if (local_contact < 0)
627 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, 0);
628
629 /* Create send and receive buffers */
630 if (smc_buf_create(smc, true))
631 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, local_contact);
632
633 smc_conn_save_peer_info(smc, aclc);
634 smc_close_init(smc);
635 smc_rx_init(smc);
636 smc_tx_init(smc);
637
638 rc = smc_clc_send_confirm(smc);
639 if (rc)
640 return smc_connect_abort(smc, rc, local_contact);
641 mutex_unlock(&smc_create_lgr_pending);
642
643 smc_copy_sock_settings_to_clc(smc);
644 if (smc->sk.sk_state == SMC_INIT)
645 smc->sk.sk_state = SMC_ACTIVE;
646
647 return 0;
648}
649
Hans Wippel3b2dec22018-05-18 09:34:18 +0200650/* perform steps before actually connecting */
651static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100652{
Hans Wippel41349842018-06-28 19:05:11 +0200653 bool ism_supported = false, rdma_supported = false;
Ursula Brauna046d572017-01-09 16:55:16 +0100654 struct smc_clc_msg_accept_confirm aclc;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200655 struct smc_ib_device *ibdev;
Hans Wippel41349842018-06-28 19:05:11 +0200656 struct smcd_dev *ismdev;
Ursula Braun7005ada2018-07-25 16:35:31 +0200657 u8 gid[SMC_GID_SIZE];
Hans Wippel41349842018-06-28 19:05:11 +0200658 unsigned short vlan;
659 int smc_type;
Ursula Brauna046d572017-01-09 16:55:16 +0100660 int rc = 0;
661 u8 ibport;
662
Ursula Braun51f1de72018-01-26 09:28:48 +0100663 sock_hold(&smc->sk); /* sock put in passive closing */
664
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200665 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +0200666 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200667
Hans Wippel3b2dec22018-05-18 09:34:18 +0200668 /* if peer has not signalled SMC-capability, fall back */
669 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +0200670 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200671
Ursula Brauna046d572017-01-09 16:55:16 +0100672 /* IPSec connections opt out of SMC-R optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200673 if (using_ipsec(smc))
674 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
Ursula Brauna046d572017-01-09 16:55:16 +0100675
Hans Wippel41349842018-06-28 19:05:11 +0200676 /* check for VLAN ID */
677 if (smc_vlan_by_tcpsk(smc->clcsock, &vlan))
678 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_CNFERR);
679
680 /* check if there is an ism device available */
681 if (!smc_check_ism(smc, &ismdev) &&
682 !smc_connect_ism_vlan_setup(smc, ismdev, vlan)) {
683 /* ISM is supported for this connection */
684 ism_supported = true;
685 smc_type = SMC_TYPE_D;
686 }
687
688 /* check if there is a rdma device available */
Ursula Braun7005ada2018-07-25 16:35:31 +0200689 if (!smc_check_rdma(smc, &ibdev, &ibport, vlan, gid)) {
Hans Wippel41349842018-06-28 19:05:11 +0200690 /* RDMA is supported for this connection */
691 rdma_supported = true;
692 if (ism_supported)
693 smc_type = SMC_TYPE_B; /* both */
694 else
695 smc_type = SMC_TYPE_R; /* only RDMA */
696 }
697
698 /* if neither ISM nor RDMA are supported, fallback */
699 if (!rdma_supported && !ism_supported)
Karsten Graul603cc142018-07-25 16:35:32 +0200700 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV);
Ursula Brauna046d572017-01-09 16:55:16 +0100701
Hans Wippel3b2dec22018-05-18 09:34:18 +0200702 /* perform CLC handshake */
Ursula Braun7005ada2018-07-25 16:35:31 +0200703 rc = smc_connect_clc(smc, smc_type, &aclc, ibdev, ibport, gid, ismdev);
Hans Wippel41349842018-06-28 19:05:11 +0200704 if (rc) {
705 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200706 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200707 }
Ursula Brauna046d572017-01-09 16:55:16 +0100708
Hans Wippel41349842018-06-28 19:05:11 +0200709 /* depending on previous steps, connect using rdma or ism */
710 if (rdma_supported && aclc.hdr.path == SMC_TYPE_R)
711 rc = smc_connect_rdma(smc, &aclc, ibdev, ibport);
712 else if (ism_supported && aclc.hdr.path == SMC_TYPE_D)
713 rc = smc_connect_ism(smc, &aclc, ismdev);
714 else
Karsten Graul603cc142018-07-25 16:35:32 +0200715 rc = SMC_CLC_DECL_MODEUNSUPP;
Hans Wippel41349842018-06-28 19:05:11 +0200716 if (rc) {
717 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200718 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200719 }
Ursula Brauna046d572017-01-09 16:55:16 +0100720
Hans Wippel41349842018-06-28 19:05:11 +0200721 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200722 return 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100723}
724
Ursula Braun24ac3a02018-06-27 17:59:50 +0200725static void smc_connect_work(struct work_struct *work)
726{
727 struct smc_sock *smc = container_of(work, struct smc_sock,
728 connect_work);
729 int rc;
730
731 lock_sock(&smc->sk);
732 rc = kernel_connect(smc->clcsock, &smc->connect_info->addr,
733 smc->connect_info->alen, smc->connect_info->flags);
734 if (smc->clcsock->sk->sk_err) {
735 smc->sk.sk_err = smc->clcsock->sk->sk_err;
736 goto out;
737 }
738 if (rc < 0) {
739 smc->sk.sk_err = -rc;
740 goto out;
741 }
742
743 rc = __smc_connect(smc);
744 if (rc < 0)
745 smc->sk.sk_err = -rc;
746
747out:
Ursula Braun648a5a72018-09-18 15:46:34 +0200748 if (smc->sk.sk_err)
749 smc->sk.sk_state_change(&smc->sk);
750 else
751 smc->sk.sk_write_space(&smc->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200752 kfree(smc->connect_info);
753 smc->connect_info = NULL;
754 release_sock(&smc->sk);
755}
756
Ursula Braunac713872017-01-09 16:55:13 +0100757static int smc_connect(struct socket *sock, struct sockaddr *addr,
758 int alen, int flags)
759{
760 struct sock *sk = sock->sk;
761 struct smc_sock *smc;
762 int rc = -EINVAL;
763
764 smc = smc_sk(sk);
765
766 /* separate smc parameter checking to be safe */
767 if (alen < sizeof(addr->sa_family))
768 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100769 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100770 goto out_err;
771
772 lock_sock(sk);
773 switch (sk->sk_state) {
774 default:
775 goto out;
776 case SMC_ACTIVE:
777 rc = -EISCONN;
778 goto out;
779 case SMC_INIT:
780 rc = 0;
781 break;
782 }
783
784 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200785 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200786 if (flags & O_NONBLOCK) {
787 if (smc->connect_info) {
788 rc = -EALREADY;
789 goto out;
790 }
791 smc->connect_info = kzalloc(alen + 2 * sizeof(int), GFP_KERNEL);
792 if (!smc->connect_info) {
793 rc = -ENOMEM;
794 goto out;
795 }
796 smc->connect_info->alen = alen;
797 smc->connect_info->flags = flags ^ O_NONBLOCK;
798 memcpy(&smc->connect_info->addr, addr, alen);
799 schedule_work(&smc->connect_work);
800 rc = -EINPROGRESS;
801 } else {
802 rc = kernel_connect(smc->clcsock, addr, alen, flags);
803 if (rc)
804 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100805
Ursula Braun24ac3a02018-06-27 17:59:50 +0200806 rc = __smc_connect(smc);
807 if (rc < 0)
808 goto out;
809 else
810 rc = 0; /* success cases including fallback */
811 }
Ursula Braunac713872017-01-09 16:55:13 +0100812
813out:
814 release_sock(sk);
815out_err:
816 return rc;
817}
818
819static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
820{
Ursula Braun3163c502018-01-24 10:28:12 +0100821 struct socket *new_clcsock = NULL;
822 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100823 struct sock *new_sk;
824 int rc;
825
Ursula Braun3163c502018-01-24 10:28:12 +0100826 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100827 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100828 if (!new_sk) {
829 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100830 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100831 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100832 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100833 goto out;
834 }
835 *new_smc = smc_sk(new_sk);
836
837 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
Ursula Braun3163c502018-01-24 10:28:12 +0100838 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100839 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100840 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100841 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Brauna046d572017-01-09 16:55:16 +0100842 if (new_clcsock)
843 sock_release(new_clcsock);
844 new_sk->sk_state = SMC_CLOSED;
845 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun3163c502018-01-24 10:28:12 +0100846 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100847 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100848 *new_smc = NULL;
849 goto out;
850 }
851
852 (*new_smc)->clcsock = new_clcsock;
853out:
854 return rc;
855}
856
Ursula Brauna046d572017-01-09 16:55:16 +0100857/* add a just created sock to the accept queue of the listen sock as
858 * candidate for a following socket accept call from user space
859 */
860static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
861{
862 struct smc_sock *par = smc_sk(parent);
863
Ursula Braun51f1de72018-01-26 09:28:48 +0100864 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100865 spin_lock(&par->accept_q_lock);
866 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
867 spin_unlock(&par->accept_q_lock);
868 sk_acceptq_added(parent);
869}
870
871/* remove a socket from the accept queue of its parental listening socket */
872static void smc_accept_unlink(struct sock *sk)
873{
874 struct smc_sock *par = smc_sk(sk)->listen_smc;
875
876 spin_lock(&par->accept_q_lock);
877 list_del_init(&smc_sk(sk)->accept_q);
878 spin_unlock(&par->accept_q_lock);
879 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100880 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100881}
882
883/* remove a sock from the accept queue to bind it to a new socket created
884 * for a socket accept call from user space
885 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100886struct sock *smc_accept_dequeue(struct sock *parent,
887 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100888{
889 struct smc_sock *isk, *n;
890 struct sock *new_sk;
891
892 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
893 new_sk = (struct sock *)isk;
894
895 smc_accept_unlink(new_sk);
896 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braun127f4972018-01-26 09:28:49 +0100897 if (isk->clcsock) {
898 sock_release(isk->clcsock);
899 isk->clcsock = NULL;
900 }
Ursula Braun288c83902017-04-10 14:58:04 +0200901 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100902 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +0100903 continue;
904 }
905 if (new_sock)
906 sock_graft(new_sk, new_sock);
907 return new_sk;
908 }
909 return NULL;
910}
911
912/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +0100913void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +0100914{
915 struct smc_sock *smc = smc_sk(sk);
916
Ursula Braunb38d7322017-01-09 16:55:25 +0100917 lock_sock(sk);
918 if (!sk->sk_lingertime)
919 /* wait for peer closing */
920 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100921 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100922 smc_close_active(smc);
Ursula Braun288c83902017-04-10 14:58:04 +0200923 sock_set_flag(sk, SOCK_DEAD);
924 sk->sk_shutdown |= SHUTDOWN_MASK;
925 }
Ursula Brauna046d572017-01-09 16:55:16 +0100926 if (smc->clcsock) {
927 struct socket *tcp;
928
929 tcp = smc->clcsock;
930 smc->clcsock = NULL;
931 sock_release(tcp);
932 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100933 if (smc->use_fallback) {
Ursula Braun51f1de72018-01-26 09:28:48 +0100934 sock_put(sk); /* passive closing */
935 sk->sk_state = SMC_CLOSED;
936 } else {
937 if (sk->sk_state == SMC_CLOSED)
938 smc_conn_free(&smc->conn);
Ursula Braunb38d7322017-01-09 16:55:25 +0100939 }
940 release_sock(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100941 sk->sk_prot->unhash(sk);
942 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +0100943}
944
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100945static int smc_serv_conf_first_link(struct smc_sock *smc)
946{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200947 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100948 struct smc_link_group *lgr = smc->conn.lgr;
949 struct smc_link *link;
950 int rest;
951 int rc;
952
953 link = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun652a1e42017-07-28 13:56:17 +0200954
Karsten Graul44aa81c2018-05-15 17:04:55 +0200955 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200956 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200957
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100958 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200959 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100960 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200961 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100962
963 /* receive CONFIRM LINK response from client over the RoCE fabric */
964 rest = wait_for_completion_interruptible_timeout(
965 &link->llc_confirm_resp,
966 SMC_LLC_WAIT_FIRST_TIME);
967 if (rest <= 0) {
968 struct smc_clc_msg_decline dclc;
969
970 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100971 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100972 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100973 }
974
Karsten Graul75d320d2018-03-01 13:51:31 +0100975 if (link->llc_confirm_resp_rc)
976 return SMC_CLC_DECL_RMBE_EC;
977
Karsten Graul52bedf32018-03-01 13:51:32 +0100978 /* send ADD LINK request to client over the RoCE fabric */
979 rc = smc_llc_send_add_link(link,
980 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +0200981 link->gid, SMC_LLC_REQ);
Karsten Graul52bedf32018-03-01 13:51:32 +0100982 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200983 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +0100984
985 /* receive ADD LINK response from client over the RoCE fabric */
986 rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
987 SMC_LLC_WAIT_TIME);
988 if (rest <= 0) {
989 struct smc_clc_msg_decline dclc;
990
991 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100992 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100993 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
Karsten Graul52bedf32018-03-01 13:51:32 +0100994 }
995
Karsten Graul877ae5b2018-05-02 16:56:44 +0200996 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100997
Karsten Graul75d320d2018-03-01 13:51:31 +0100998 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100999}
1000
Hans Wippel3b2dec22018-05-18 09:34:18 +02001001/* listen worker: finish */
1002static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001003{
Ursula Brauna046d572017-01-09 16:55:16 +01001004 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001005 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001006
Ursula Braunb38d7322017-01-09 16:55:25 +01001007 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001008 if (lsmc->sk.sk_state == SMC_LISTEN) {
1009 smc_accept_enqueue(&lsmc->sk, newsmcsk);
1010 } else { /* no longer listening */
1011 smc_close_non_accepted(newsmcsk);
1012 }
1013 release_sock(&lsmc->sk);
1014
1015 /* Wake up accept */
1016 lsmc->sk.sk_data_ready(&lsmc->sk);
1017 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001018}
Ursula Brauna046d572017-01-09 16:55:16 +01001019
Hans Wippel3b2dec22018-05-18 09:34:18 +02001020/* listen worker: finish in state connected */
1021static void smc_listen_out_connected(struct smc_sock *new_smc)
1022{
1023 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001024
Hans Wippel3b2dec22018-05-18 09:34:18 +02001025 sk_refcnt_debug_inc(newsmcsk);
1026 if (newsmcsk->sk_state == SMC_INIT)
1027 newsmcsk->sk_state = SMC_ACTIVE;
1028
1029 smc_listen_out(new_smc);
1030}
1031
1032/* listen worker: finish in error state */
1033static void smc_listen_out_err(struct smc_sock *new_smc)
1034{
1035 struct sock *newsmcsk = &new_smc->sk;
1036
Ursula Braun51f1de72018-01-26 09:28:48 +01001037 if (newsmcsk->sk_state == SMC_INIT)
1038 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001039 newsmcsk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001040 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001041
1042 smc_listen_out(new_smc);
1043}
1044
1045/* listen worker: decline and fall back if possible */
1046static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1047 int local_contact)
1048{
1049 /* RDMA setup failed, switch back to TCP */
1050 if (local_contact == SMC_FIRST_CONTACT)
1051 smc_lgr_forget(new_smc->conn.lgr);
1052 if (reason_code < 0) { /* error, no fallback possible */
1053 smc_listen_out_err(new_smc);
1054 return;
1055 }
1056 smc_conn_free(&new_smc->conn);
1057 new_smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001058 new_smc->fallback_rsn = reason_code;
1059 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001060 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1061 smc_listen_out_err(new_smc);
1062 return;
1063 }
1064 }
1065 smc_listen_out_connected(new_smc);
1066}
1067
1068/* listen worker: check prefixes */
1069static int smc_listen_rdma_check(struct smc_sock *new_smc,
1070 struct smc_clc_msg_proposal *pclc)
1071{
1072 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1073 struct socket *newclcsock = new_smc->clcsock;
1074
1075 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1076 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
1077 return SMC_CLC_DECL_CNFERR;
1078
1079 return 0;
1080}
1081
1082/* listen worker: initialize connection and buffers */
1083static int smc_listen_rdma_init(struct smc_sock *new_smc,
1084 struct smc_clc_msg_proposal *pclc,
1085 struct smc_ib_device *ibdev, u8 ibport,
1086 int *local_contact)
1087{
1088 /* allocate connection / link group */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001089 *local_contact = smc_conn_create(new_smc, false, 0, ibdev, ibport,
1090 &pclc->lcl, NULL, 0);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001091 if (*local_contact < 0) {
1092 if (*local_contact == -ENOMEM)
1093 return SMC_CLC_DECL_MEM;/* insufficient memory*/
1094 return SMC_CLC_DECL_INTERR; /* other error */
1095 }
1096
1097 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001098 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001099 return SMC_CLC_DECL_MEM;
1100
1101 return 0;
1102}
1103
Hans Wippel41349842018-06-28 19:05:11 +02001104/* listen worker: initialize connection and buffers for SMC-D */
1105static int smc_listen_ism_init(struct smc_sock *new_smc,
1106 struct smc_clc_msg_proposal *pclc,
1107 struct smcd_dev *ismdev,
1108 int *local_contact)
1109{
1110 struct smc_clc_msg_smcd *pclc_smcd;
1111
1112 pclc_smcd = smc_get_clc_msg_smcd(pclc);
1113 *local_contact = smc_conn_create(new_smc, true, 0, NULL, 0, NULL,
1114 ismdev, pclc_smcd->gid);
1115 if (*local_contact < 0) {
1116 if (*local_contact == -ENOMEM)
1117 return SMC_CLC_DECL_MEM;/* insufficient memory*/
1118 return SMC_CLC_DECL_INTERR; /* other error */
1119 }
1120
1121 /* Check if peer can be reached via ISM device */
1122 if (smc_ism_cantalk(new_smc->conn.lgr->peer_gid,
1123 new_smc->conn.lgr->vlan_id,
1124 new_smc->conn.lgr->smcd)) {
1125 if (*local_contact == SMC_FIRST_CONTACT)
1126 smc_lgr_forget(new_smc->conn.lgr);
1127 smc_conn_free(&new_smc->conn);
1128 return SMC_CLC_DECL_CNFERR;
1129 }
1130
1131 /* Create send and receive buffers */
1132 if (smc_buf_create(new_smc, true)) {
1133 if (*local_contact == SMC_FIRST_CONTACT)
1134 smc_lgr_forget(new_smc->conn.lgr);
1135 smc_conn_free(&new_smc->conn);
1136 return SMC_CLC_DECL_MEM;
1137 }
1138
1139 return 0;
1140}
1141
Hans Wippel3b2dec22018-05-18 09:34:18 +02001142/* listen worker: register buffers */
1143static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
1144{
1145 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1146
1147 if (local_contact != SMC_FIRST_CONTACT) {
Karsten Graulc7674c02018-11-22 10:26:43 +01001148 if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true))
1149 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001150 }
1151 smc_rmb_sync_sg_for_device(&new_smc->conn);
1152
1153 return 0;
1154}
1155
1156/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001157static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1158 struct smc_clc_msg_accept_confirm *cclc,
1159 int local_contact)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001160{
1161 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1162 int reason_code = 0;
1163
1164 if (local_contact == SMC_FIRST_CONTACT)
1165 smc_link_save_peer_info(link, cclc);
1166
1167 if (smc_rmb_rtoken_handling(&new_smc->conn, cclc)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001168 reason_code = SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001169 goto decline;
1170 }
1171
1172 if (local_contact == SMC_FIRST_CONTACT) {
1173 if (smc_ib_ready_link(link)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001174 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001175 goto decline;
1176 }
1177 /* QP confirmation over RoCE fabric */
1178 reason_code = smc_serv_conf_first_link(new_smc);
1179 if (reason_code)
1180 goto decline;
1181 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001182 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001183
1184decline:
Hans Wippel3b2dec22018-05-18 09:34:18 +02001185 smc_listen_decline(new_smc, reason_code, local_contact);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001186 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001187}
1188
1189/* setup for RDMA connection of server */
1190static void smc_listen_work(struct work_struct *work)
1191{
1192 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1193 smc_listen_work);
1194 struct socket *newclcsock = new_smc->clcsock;
1195 struct smc_clc_msg_accept_confirm cclc;
1196 struct smc_clc_msg_proposal *pclc;
1197 struct smc_ib_device *ibdev;
Hans Wippel41349842018-06-28 19:05:11 +02001198 bool ism_supported = false;
1199 struct smcd_dev *ismdev;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001200 u8 buf[SMC_CLC_MAX_LEN];
1201 int local_contact = 0;
Ursula Braun7005ada2018-07-25 16:35:31 +02001202 unsigned short vlan;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001203 int reason_code = 0;
1204 int rc = 0;
1205 u8 ibport;
1206
1207 if (new_smc->use_fallback) {
1208 smc_listen_out_connected(new_smc);
1209 return;
1210 }
1211
1212 /* check if peer is smc capable */
1213 if (!tcp_sk(newclcsock->sk)->syn_smc) {
1214 new_smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001215 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001216 smc_listen_out_connected(new_smc);
1217 return;
1218 }
1219
1220 /* do inband token exchange -
1221 * wait for and receive SMC Proposal CLC message
1222 */
1223 pclc = (struct smc_clc_msg_proposal *)&buf;
1224 reason_code = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
Ursula Braun2b59f582018-11-22 10:26:39 +01001225 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001226 if (reason_code) {
1227 smc_listen_decline(new_smc, reason_code, 0);
1228 return;
1229 }
1230
1231 /* IPSec connections opt out of SMC-R optimizations */
1232 if (using_ipsec(new_smc)) {
1233 smc_listen_decline(new_smc, SMC_CLC_DECL_IPSEC, 0);
1234 return;
1235 }
1236
1237 mutex_lock(&smc_create_lgr_pending);
1238 smc_close_init(new_smc);
1239 smc_rx_init(new_smc);
1240 smc_tx_init(new_smc);
1241
Hans Wippel41349842018-06-28 19:05:11 +02001242 /* check if ISM is available */
1243 if ((pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) &&
1244 !smc_check_ism(new_smc, &ismdev) &&
1245 !smc_listen_ism_init(new_smc, pclc, ismdev, &local_contact)) {
1246 ism_supported = true;
1247 }
1248
Hans Wippel3b2dec22018-05-18 09:34:18 +02001249 /* check if RDMA is available */
Hans Wippel41349842018-06-28 19:05:11 +02001250 if (!ism_supported &&
1251 ((pclc->hdr.path != SMC_TYPE_R && pclc->hdr.path != SMC_TYPE_B) ||
Ursula Braun7005ada2018-07-25 16:35:31 +02001252 smc_vlan_by_tcpsk(new_smc->clcsock, &vlan) ||
1253 smc_check_rdma(new_smc, &ibdev, &ibport, vlan, NULL) ||
Hans Wippel41349842018-06-28 19:05:11 +02001254 smc_listen_rdma_check(new_smc, pclc) ||
1255 smc_listen_rdma_init(new_smc, pclc, ibdev, ibport,
1256 &local_contact) ||
1257 smc_listen_rdma_reg(new_smc, local_contact))) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001258 /* SMC not supported, decline */
1259 mutex_unlock(&smc_create_lgr_pending);
Karsten Graul603cc142018-07-25 16:35:32 +02001260 smc_listen_decline(new_smc, SMC_CLC_DECL_MODEUNSUPP,
1261 local_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001262 return;
1263 }
1264
1265 /* send SMC Accept CLC message */
1266 rc = smc_clc_send_accept(new_smc, local_contact);
1267 if (rc) {
1268 mutex_unlock(&smc_create_lgr_pending);
1269 smc_listen_decline(new_smc, rc, local_contact);
1270 return;
1271 }
1272
1273 /* receive SMC Confirm CLC message */
1274 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001275 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001276 if (reason_code) {
1277 mutex_unlock(&smc_create_lgr_pending);
1278 smc_listen_decline(new_smc, reason_code, local_contact);
1279 return;
1280 }
1281
1282 /* finish worker */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001283 if (!ism_supported) {
Ursula Braun587e41d2018-11-22 10:26:40 +01001284 if (smc_listen_rdma_finish(new_smc, &cclc, local_contact)) {
1285 mutex_unlock(&smc_create_lgr_pending);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001286 return;
Ursula Braun587e41d2018-11-22 10:26:40 +01001287 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001288 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001289 smc_conn_save_peer_info(new_smc, &cclc);
1290 mutex_unlock(&smc_create_lgr_pending);
1291 smc_listen_out_connected(new_smc);
Ursula Brauna046d572017-01-09 16:55:16 +01001292}
1293
1294static void smc_tcp_listen_work(struct work_struct *work)
1295{
1296 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1297 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001298 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001299 struct smc_sock *new_smc;
1300 int rc = 0;
1301
Ursula Braun3163c502018-01-24 10:28:12 +01001302 lock_sock(lsk);
1303 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001304 rc = smc_clcsock_accept(lsmc, &new_smc);
1305 if (rc)
1306 goto out;
1307 if (!new_smc)
1308 continue;
1309
1310 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001311 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001312 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001313 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001314 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1315 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001316 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1317 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001318 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1319 if (!schedule_work(&new_smc->smc_listen_work))
1320 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001321 }
1322
1323out:
Ursula Braun3163c502018-01-24 10:28:12 +01001324 release_sock(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001325 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +01001326}
1327
Ursula Braunac713872017-01-09 16:55:13 +01001328static int smc_listen(struct socket *sock, int backlog)
1329{
1330 struct sock *sk = sock->sk;
1331 struct smc_sock *smc;
1332 int rc;
1333
1334 smc = smc_sk(sk);
1335 lock_sock(sk);
1336
1337 rc = -EINVAL;
1338 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
1339 goto out;
1340
1341 rc = 0;
1342 if (sk->sk_state == SMC_LISTEN) {
1343 sk->sk_max_ack_backlog = backlog;
1344 goto out;
1345 }
1346 /* some socket options are handled in core, so we could not apply
1347 * them to the clc socket -- copy smc socket options to clc socket
1348 */
1349 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001350 if (!smc->use_fallback)
1351 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001352
1353 rc = kernel_listen(smc->clcsock, backlog);
1354 if (rc)
1355 goto out;
1356 sk->sk_max_ack_backlog = backlog;
1357 sk->sk_ack_backlog = 0;
1358 sk->sk_state = SMC_LISTEN;
Ursula Braun51f1de72018-01-26 09:28:48 +01001359 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1360 if (!schedule_work(&smc->tcp_listen_work))
1361 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001362
1363out:
1364 release_sock(sk);
1365 return rc;
1366}
1367
1368static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001369 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001370{
Ursula Brauna046d572017-01-09 16:55:16 +01001371 struct sock *sk = sock->sk, *nsk;
1372 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001373 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001374 long timeo;
1375 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001376
1377 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001378 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001379 lock_sock(sk);
1380
1381 if (lsmc->sk.sk_state != SMC_LISTEN) {
1382 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001383 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001384 goto out;
1385 }
1386
Ursula Brauna046d572017-01-09 16:55:16 +01001387 /* Wait for an incoming connection */
1388 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1389 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1390 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1391 set_current_state(TASK_INTERRUPTIBLE);
1392 if (!timeo) {
1393 rc = -EAGAIN;
1394 break;
1395 }
1396 release_sock(sk);
1397 timeo = schedule_timeout(timeo);
1398 /* wakeup by sk_data_ready in smc_listen_work() */
1399 sched_annotate_sleep();
1400 lock_sock(sk);
1401 if (signal_pending(current)) {
1402 rc = sock_intr_errno(timeo);
1403 break;
1404 }
1405 }
1406 set_current_state(TASK_RUNNING);
1407 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001408
Ursula Brauna046d572017-01-09 16:55:16 +01001409 if (!rc)
1410 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001411 release_sock(sk);
1412 if (rc)
1413 goto out;
1414
1415 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1416 /* wait till data arrives on the socket */
1417 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1418 MSEC_PER_SEC);
1419 if (smc_sk(nsk)->use_fallback) {
1420 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1421
1422 lock_sock(clcsk);
1423 if (skb_queue_empty(&clcsk->sk_receive_queue))
1424 sk_wait_data(clcsk, &timeo, NULL);
1425 release_sock(clcsk);
1426 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1427 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001428 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001429 release_sock(nsk);
1430 }
1431 }
Ursula Braunac713872017-01-09 16:55:13 +01001432
1433out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001434 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001435 return rc;
1436}
1437
1438static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001439 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001440{
1441 struct smc_sock *smc;
1442
Ursula Braunb38d7322017-01-09 16:55:25 +01001443 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1444 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001445 return -ENOTCONN;
1446
1447 smc = smc_sk(sock->sk);
1448
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001449 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001450}
1451
1452static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1453{
1454 struct sock *sk = sock->sk;
1455 struct smc_sock *smc;
1456 int rc = -EPIPE;
1457
1458 smc = smc_sk(sk);
1459 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001460 if ((sk->sk_state != SMC_ACTIVE) &&
1461 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1462 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001463 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001464
1465 if (msg->msg_flags & MSG_FASTOPEN) {
1466 if (sk->sk_state == SMC_INIT) {
1467 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001468 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001469 } else {
1470 rc = -EINVAL;
1471 goto out;
1472 }
1473 }
1474
Ursula Braunac713872017-01-09 16:55:13 +01001475 if (smc->use_fallback)
1476 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1477 else
Ursula Braune6727f32017-01-09 16:55:23 +01001478 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001479out:
1480 release_sock(sk);
1481 return rc;
1482}
1483
1484static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1485 int flags)
1486{
1487 struct sock *sk = sock->sk;
1488 struct smc_sock *smc;
1489 int rc = -ENOTCONN;
1490
1491 smc = smc_sk(sk);
1492 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001493 if ((sk->sk_state == SMC_INIT) ||
1494 (sk->sk_state == SMC_LISTEN) ||
1495 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001496 goto out;
1497
Ursula Braunb38d7322017-01-09 16:55:25 +01001498 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1499 rc = 0;
1500 goto out;
1501 }
1502
Stefan Raspl9014db22018-05-03 18:12:39 +02001503 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001504 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001505 } else {
1506 msg->msg_namelen = 0;
1507 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1508 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001509
Ursula Braunac713872017-01-09 16:55:13 +01001510out:
1511 release_sock(sk);
1512 return rc;
1513}
1514
Al Viroade994f2017-07-03 00:01:49 -04001515static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001516{
Ursula Braun8dce2782018-01-26 09:28:47 +01001517 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001518 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001519
Ursula Braun8dce2782018-01-26 09:28:47 +01001520 spin_lock(&isk->accept_q_lock);
1521 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001522 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001523 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001524
Ursula Braun8dce2782018-01-26 09:28:47 +01001525 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001526}
1527
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001528static __poll_t smc_poll(struct file *file, struct socket *sock,
1529 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01001530{
1531 struct sock *sk = sock->sk;
Al Viroe6c8adc2017-07-03 22:25:56 -04001532 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001533 struct smc_sock *smc;
1534
Ursula Braun8dce2782018-01-26 09:28:47 +01001535 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001536 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001537
Ursula Braunac713872017-01-09 16:55:13 +01001538 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02001539 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01001540 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001541 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02001542 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001543 if (sk->sk_err)
Ursula Braun784813a2018-05-02 16:53:56 +02001544 mask |= EPOLLERR;
Ursula Braunac713872017-01-09 16:55:13 +01001545 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07001546 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02001547 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01001548 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001549 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001550 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1551 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001552 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001553 if (sk->sk_state == SMC_LISTEN) {
1554 /* woken up by sk_data_ready in smc_listen_work() */
1555 mask = smc_accept_poll(sk);
1556 } else {
1557 if (atomic_read(&smc->conn.sndbuf_space) ||
1558 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001559 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001560 } else {
1561 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1562 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1563 }
1564 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001565 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001566 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001567 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001568 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001569 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02001570 if (smc->conn.urg_state == SMC_URG_VALID)
1571 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01001572 }
Ursula Braunac713872017-01-09 16:55:13 +01001573 }
1574
1575 return mask;
1576}
1577
1578static int smc_shutdown(struct socket *sock, int how)
1579{
1580 struct sock *sk = sock->sk;
1581 struct smc_sock *smc;
1582 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001583 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001584
1585 smc = smc_sk(sk);
1586
1587 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001588 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001589
1590 lock_sock(sk);
1591
1592 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02001593 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01001594 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1595 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1596 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1597 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1598 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001599 goto out;
1600 if (smc->use_fallback) {
1601 rc = kernel_sock_shutdown(smc->clcsock, how);
1602 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1603 if (sk->sk_shutdown == SHUTDOWN_MASK)
1604 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001605 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001606 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001607 switch (how) {
1608 case SHUT_RDWR: /* shutdown in both directions */
1609 rc = smc_close_active(smc);
1610 break;
1611 case SHUT_WR:
1612 rc = smc_close_shutdown_write(smc);
1613 break;
1614 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001615 rc = 0;
1616 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001617 break;
1618 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001619 if (smc->clcsock)
1620 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001621 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1622 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001623
1624out:
1625 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001626 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001627}
1628
1629static int smc_setsockopt(struct socket *sock, int level, int optname,
1630 char __user *optval, unsigned int optlen)
1631{
1632 struct sock *sk = sock->sk;
1633 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001634 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001635
1636 smc = smc_sk(sk);
1637
1638 /* generic setsockopts reaching us here always apply to the
1639 * CLC socket
1640 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001641 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1642 optval, optlen);
1643 if (smc->clcsock->sk->sk_err) {
1644 sk->sk_err = smc->clcsock->sk->sk_err;
1645 sk->sk_error_report(sk);
1646 }
1647 if (rc)
1648 return rc;
1649
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001650 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00001651 return -EINVAL;
Ursula Braunac0107e2018-07-18 15:22:50 +02001652 if (get_user(val, (int __user *)optval))
1653 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001654
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001655 lock_sock(sk);
1656 switch (optname) {
1657 case TCP_ULP:
1658 case TCP_FASTOPEN:
1659 case TCP_FASTOPEN_CONNECT:
1660 case TCP_FASTOPEN_KEY:
1661 case TCP_FASTOPEN_NO_COOKIE:
1662 /* option not supported by SMC */
1663 if (sk->sk_state == SMC_INIT) {
1664 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001665 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001666 } else {
1667 if (!smc->use_fallback)
1668 rc = -EINVAL;
1669 }
1670 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001671 case TCP_NODELAY:
1672 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001673 if (val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001674 mod_delayed_work(system_wq, &smc->conn.tx_work,
1675 0);
1676 }
1677 break;
1678 case TCP_CORK:
1679 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001680 if (!val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001681 mod_delayed_work(system_wq, &smc->conn.tx_work,
1682 0);
1683 }
1684 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001685 case TCP_DEFER_ACCEPT:
1686 smc->sockopt_defer_accept = val;
1687 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001688 default:
1689 break;
1690 }
1691 release_sock(sk);
1692
1693 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001694}
1695
1696static int smc_getsockopt(struct socket *sock, int level, int optname,
1697 char __user *optval, int __user *optlen)
1698{
1699 struct smc_sock *smc;
1700
1701 smc = smc_sk(sock->sk);
1702 /* socket options apply to the CLC socket */
1703 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1704 optval, optlen);
1705}
1706
1707static int smc_ioctl(struct socket *sock, unsigned int cmd,
1708 unsigned long arg)
1709{
Stefan Rasplde8474e2018-05-23 16:38:11 +02001710 union smc_host_cursor cons, urg;
1711 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01001712 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001713 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001714
1715 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001716 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02001717 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02001718 if (smc->use_fallback) {
1719 if (!smc->clcsock) {
1720 release_sock(&smc->sk);
1721 return -EBADF;
1722 }
1723 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1724 release_sock(&smc->sk);
1725 return answ;
1726 }
Ursula Braun9b67e262018-05-02 16:56:46 +02001727 switch (cmd) {
1728 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02001729 if (smc->sk.sk_state == SMC_LISTEN) {
1730 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001731 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001732 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001733 if (smc->sk.sk_state == SMC_INIT ||
1734 smc->sk.sk_state == SMC_CLOSED)
1735 answ = 0;
1736 else
1737 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02001738 break;
1739 case SIOCOUTQ:
1740 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02001741 if (smc->sk.sk_state == SMC_LISTEN) {
1742 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001743 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001744 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001745 if (smc->sk.sk_state == SMC_INIT ||
1746 smc->sk.sk_state == SMC_CLOSED)
1747 answ = 0;
1748 else
1749 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02001750 atomic_read(&smc->conn.sndbuf_space);
1751 break;
1752 case SIOCOUTQNSD:
1753 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02001754 if (smc->sk.sk_state == SMC_LISTEN) {
1755 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001756 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001757 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001758 if (smc->sk.sk_state == SMC_INIT ||
1759 smc->sk.sk_state == SMC_CLOSED)
1760 answ = 0;
1761 else
1762 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02001763 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02001764 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02001765 if (smc->sk.sk_state == SMC_LISTEN) {
1766 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001767 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001768 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001769 if (smc->sk.sk_state == SMC_INIT ||
1770 smc->sk.sk_state == SMC_CLOSED) {
1771 answ = 0;
1772 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02001773 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1774 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001775 answ = smc_curs_diff(conn->rmb_desc->len,
1776 &cons, &urg) == 1;
1777 }
1778 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02001779 default:
Ursula Braun1992d992018-07-16 13:56:52 +02001780 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001781 return -ENOIOCTLCMD;
1782 }
Ursula Braun1992d992018-07-16 13:56:52 +02001783 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001784
1785 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001786}
1787
1788static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1789 int offset, size_t size, int flags)
1790{
1791 struct sock *sk = sock->sk;
1792 struct smc_sock *smc;
1793 int rc = -EPIPE;
1794
1795 smc = smc_sk(sk);
1796 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001797 if (sk->sk_state != SMC_ACTIVE) {
1798 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001799 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001800 }
1801 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001802 if (smc->use_fallback)
1803 rc = kernel_sendpage(smc->clcsock, page, offset,
1804 size, flags);
1805 else
1806 rc = sock_no_sendpage(sock, page, offset, size, flags);
1807
1808out:
Ursula Braunac713872017-01-09 16:55:13 +01001809 return rc;
1810}
1811
Stefan Raspl9014db22018-05-03 18:12:39 +02001812/* Map the affected portions of the rmbe into an spd, note the number of bytes
1813 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1814 * updates till whenever a respective page has been fully processed.
1815 * Note that subsequent recv() calls have to wait till all splice() processing
1816 * completed.
1817 */
Ursula Braunac713872017-01-09 16:55:13 +01001818static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1819 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001820 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001821{
1822 struct sock *sk = sock->sk;
1823 struct smc_sock *smc;
1824 int rc = -ENOTCONN;
1825
1826 smc = smc_sk(sk);
1827 lock_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001828
1829 if (sk->sk_state == SMC_INIT ||
1830 sk->sk_state == SMC_LISTEN ||
1831 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001832 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001833
1834 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1835 rc = 0;
1836 goto out;
1837 }
1838
Ursula Braunac713872017-01-09 16:55:13 +01001839 if (smc->use_fallback) {
1840 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1841 pipe, len, flags);
1842 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001843 if (*ppos) {
1844 rc = -ESPIPE;
1845 goto out;
1846 }
1847 if (flags & SPLICE_F_NONBLOCK)
1848 flags = MSG_DONTWAIT;
1849 else
1850 flags = 0;
1851 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001852 }
1853out:
1854 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001855
Ursula Braunac713872017-01-09 16:55:13 +01001856 return rc;
1857}
1858
1859/* must look like tcp */
1860static const struct proto_ops smc_sock_ops = {
1861 .family = PF_SMC,
1862 .owner = THIS_MODULE,
1863 .release = smc_release,
1864 .bind = smc_bind,
1865 .connect = smc_connect,
1866 .socketpair = sock_no_socketpair,
1867 .accept = smc_accept,
1868 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001869 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01001870 .ioctl = smc_ioctl,
1871 .listen = smc_listen,
1872 .shutdown = smc_shutdown,
1873 .setsockopt = smc_setsockopt,
1874 .getsockopt = smc_getsockopt,
1875 .sendmsg = smc_sendmsg,
1876 .recvmsg = smc_recvmsg,
1877 .mmap = sock_no_mmap,
1878 .sendpage = smc_sendpage,
1879 .splice_read = smc_splice_read,
1880};
1881
1882static int smc_create(struct net *net, struct socket *sock, int protocol,
1883 int kern)
1884{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001885 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001886 struct smc_sock *smc;
1887 struct sock *sk;
1888 int rc;
1889
1890 rc = -ESOCKTNOSUPPORT;
1891 if (sock->type != SOCK_STREAM)
1892 goto out;
1893
1894 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001895 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01001896 goto out;
1897
1898 rc = -ENOBUFS;
1899 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001900 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001901 if (!sk)
1902 goto out;
1903
1904 /* create internal TCP socket for CLC handshake and fallback */
1905 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001906 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02001907 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001908 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1909 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01001910 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01001911 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01001912 goto out;
1913 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01001914 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1915 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01001916
1917out:
1918 return rc;
1919}
1920
1921static const struct net_proto_family smc_sock_family_ops = {
1922 .family = PF_SMC,
1923 .owner = THIS_MODULE,
1924 .create = smc_create,
1925};
1926
1927static int __init smc_init(void)
1928{
1929 int rc;
1930
Thomas Richter6812baa2017-01-09 16:55:15 +01001931 rc = smc_pnet_init();
1932 if (rc)
1933 return rc;
1934
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001935 rc = smc_llc_init();
1936 if (rc) {
1937 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1938 goto out_pnet;
1939 }
1940
Ursula Braun5f083182017-01-09 16:55:22 +01001941 rc = smc_cdc_init();
1942 if (rc) {
1943 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1944 goto out_pnet;
1945 }
1946
Ursula Braunac713872017-01-09 16:55:13 +01001947 rc = proto_register(&smc_proto, 1);
1948 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01001949 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Thomas Richter6812baa2017-01-09 16:55:15 +01001950 goto out_pnet;
Ursula Braunac713872017-01-09 16:55:13 +01001951 }
1952
Karsten Graulaaa4d332018-03-16 15:06:41 +01001953 rc = proto_register(&smc_proto6, 1);
1954 if (rc) {
1955 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
1956 goto out_proto;
1957 }
1958
Ursula Braunac713872017-01-09 16:55:13 +01001959 rc = sock_register(&smc_sock_family_ops);
1960 if (rc) {
1961 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001962 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01001963 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01001964 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001965 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01001966
Ursula Brauna4cf0442017-01-09 16:55:14 +01001967 rc = smc_ib_register_client();
1968 if (rc) {
1969 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1970 goto out_sock;
1971 }
1972
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001973 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01001974 return 0;
1975
Ursula Brauna4cf0442017-01-09 16:55:14 +01001976out_sock:
1977 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001978out_proto6:
1979 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01001980out_proto:
1981 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001982out_pnet:
1983 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001984 return rc;
1985}
1986
1987static void __exit smc_exit(void)
1988{
Hans Wippel9fda3512018-05-18 09:34:11 +02001989 smc_core_exit();
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001990 static_branch_disable(&tcp_have_smc);
Ursula Brauna4cf0442017-01-09 16:55:14 +01001991 smc_ib_unregister_client();
Ursula Braunac713872017-01-09 16:55:13 +01001992 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001993 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01001994 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001995 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001996}
1997
1998module_init(smc_init);
1999module_exit(smc_exit);
2000
2001MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2002MODULE_DESCRIPTION("smc socket address family");
2003MODULE_LICENSE("GPL");
2004MODULE_ALIAS_NETPROTO(PF_SMC);