blob: fce7e47511516e254f727309b6657bfee8d5864b [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 Graul44aa81c2018-05-15 17:04:55 +0200302/* register a new rmb, optionally send confirm_rkey msg to register with peer */
303static 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{
306 /* register memory region for new rmb */
Karsten Graula6920d12018-05-03 17:57:38 +0200307 if (smc_wr_reg_send(link, rmb_desc->mr_rx[SMC_SINGLE_LINK])) {
308 rmb_desc->regerr = 1;
Karsten Graule63a5f82018-05-03 17:57:37 +0200309 return -EFAULT;
Karsten Graula6920d12018-05-03 17:57:38 +0200310 }
Karsten Graul44aa81c2018-05-15 17:04:55 +0200311 if (!conf_rkey)
312 return 0;
313 /* exchange confirm_rkey msg with peer */
314 if (smc_llc_do_confirm_rkey(link, rmb_desc)) {
315 rmb_desc->regerr = 1;
316 return -EFAULT;
317 }
Karsten Graule63a5f82018-05-03 17:57:37 +0200318 return 0;
319}
320
Stefan Raspl0f627122018-03-01 13:51:26 +0100321static int smc_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100322{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200323 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100324 struct smc_link_group *lgr = smc->conn.lgr;
325 struct smc_link *link;
326 int rest;
327 int rc;
328
329 link = &lgr->lnk[SMC_SINGLE_LINK];
330 /* receive CONFIRM LINK request from server over RoCE fabric */
331 rest = wait_for_completion_interruptible_timeout(
332 &link->llc_confirm,
333 SMC_LLC_WAIT_FIRST_TIME);
334 if (rest <= 0) {
335 struct smc_clc_msg_decline dclc;
336
337 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
338 SMC_CLC_DECLINE);
339 return rc;
340 }
341
Karsten Graul75d320d2018-03-01 13:51:31 +0100342 if (link->llc_confirm_rc)
343 return SMC_CLC_DECL_RMBE_EC;
344
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100345 rc = smc_ib_modify_qp_rts(link);
346 if (rc)
Karsten Graul603cc142018-07-25 16:35:32 +0200347 return SMC_CLC_DECL_ERR_RDYLNK;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100348
349 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200350
Karsten Graul44aa81c2018-05-15 17:04:55 +0200351 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200352 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200353
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100354 /* send CONFIRM LINK response over RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200355 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100356 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200357 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100358
Karsten Graul52bedf32018-03-01 13:51:32 +0100359 /* receive ADD LINK request from server over RoCE fabric */
360 rest = wait_for_completion_interruptible_timeout(&link->llc_add,
361 SMC_LLC_WAIT_TIME);
362 if (rest <= 0) {
363 struct smc_clc_msg_decline dclc;
364
365 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
366 SMC_CLC_DECLINE);
367 return rc;
368 }
369
370 /* send add link reject message, only one link supported for now */
371 rc = smc_llc_send_add_link(link,
372 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +0200373 link->gid, SMC_LLC_RESP);
Karsten Graul52bedf32018-03-01 13:51:32 +0100374 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200375 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +0100376
Karsten Graul877ae5b2018-05-02 16:56:44 +0200377 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100378
Karsten Graul75d320d2018-03-01 13:51:31 +0100379 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100380}
381
Hans Wippel41349842018-06-28 19:05:11 +0200382static void smcr_conn_save_peer_info(struct smc_sock *smc,
383 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100384{
Hans Wippel95d8d2632018-05-18 09:34:13 +0200385 int bufsize = smc_uncompress_bufsize(clc->rmbe_size);
386
Hans Wippel92a138e2018-05-18 09:34:12 +0200387 smc->conn.peer_rmbe_idx = clc->rmbe_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100388 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200389 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100390 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200391 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100392}
393
Hans Wippel41349842018-06-28 19:05:11 +0200394static void smcd_conn_save_peer_info(struct smc_sock *smc,
395 struct smc_clc_msg_accept_confirm *clc)
396{
397 int bufsize = smc_uncompress_bufsize(clc->dmbe_size);
398
399 smc->conn.peer_rmbe_idx = clc->dmbe_idx;
400 smc->conn.peer_token = clc->token;
401 /* msg header takes up space in the buffer */
402 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
403 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
404 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
405}
406
407static void smc_conn_save_peer_info(struct smc_sock *smc,
408 struct smc_clc_msg_accept_confirm *clc)
409{
410 if (smc->conn.lgr->is_smcd)
411 smcd_conn_save_peer_info(smc, clc);
412 else
413 smcr_conn_save_peer_info(smc, clc);
414}
415
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100416static void smc_link_save_peer_info(struct smc_link *link,
417 struct smc_clc_msg_accept_confirm *clc)
418{
419 link->peer_qpn = ntoh24(clc->qpn);
420 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
421 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
422 link->peer_psn = ntoh24(clc->psn);
423 link->peer_mtu = clc->qp_mtu;
424}
425
Hans Wippel3b2dec22018-05-18 09:34:18 +0200426/* fall back during connect */
Karsten Graul603cc142018-07-25 16:35:32 +0200427static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200428{
429 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +0200430 smc->fallback_rsn = reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200431 smc_copy_sock_settings_to_clc(smc);
432 if (smc->sk.sk_state == SMC_INIT)
433 smc->sk.sk_state = SMC_ACTIVE;
434 return 0;
435}
436
437/* decline and fall back during connect */
438static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
439{
440 int rc;
441
Ursula Braune1bbdd52018-07-05 16:15:30 +0200442 if (reason_code < 0) { /* error, fallback is not possible */
443 if (smc->sk.sk_state == SMC_INIT)
444 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200445 return reason_code;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200446 }
Karsten Graul603cc142018-07-25 16:35:32 +0200447 if (reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200448 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200449 if (rc < 0) {
450 if (smc->sk.sk_state == SMC_INIT)
451 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200452 return rc;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200453 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200454 }
Karsten Graul603cc142018-07-25 16:35:32 +0200455 return smc_connect_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200456}
457
458/* abort connecting */
459static int smc_connect_abort(struct smc_sock *smc, int reason_code,
460 int local_contact)
461{
462 if (local_contact == SMC_FIRST_CONTACT)
463 smc_lgr_forget(smc->conn.lgr);
464 mutex_unlock(&smc_create_lgr_pending);
465 smc_conn_free(&smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200466 return reason_code;
467}
468
469/* check if there is a rdma device available for this connection. */
470/* called for connect and listen */
471static int smc_check_rdma(struct smc_sock *smc, struct smc_ib_device **ibdev,
Ursula Braun7005ada2018-07-25 16:35:31 +0200472 u8 *ibport, unsigned short vlan_id, u8 gid[])
Hans Wippel3b2dec22018-05-18 09:34:18 +0200473{
474 int reason_code = 0;
475
476 /* PNET table look up: search active ib_device and port
477 * within same PNETID that also contains the ethernet device
478 * used for the internal TCP socket
479 */
Ursula Braun7005ada2018-07-25 16:35:31 +0200480 smc_pnet_find_roce_resource(smc->clcsock->sk, ibdev, ibport, vlan_id,
481 gid);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200482 if (!(*ibdev))
483 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
484
485 return reason_code;
486}
487
Hans Wippel41349842018-06-28 19:05:11 +0200488/* check if there is an ISM device available for this connection. */
489/* called for connect and listen */
490static int smc_check_ism(struct smc_sock *smc, struct smcd_dev **ismdev)
491{
492 /* Find ISM device with same PNETID as connecting interface */
493 smc_pnet_find_ism_resource(smc->clcsock->sk, ismdev);
494 if (!(*ismdev))
495 return SMC_CLC_DECL_CNFERR; /* configuration error */
496 return 0;
497}
498
499/* Check for VLAN ID and register it on ISM device just for CLC handshake */
500static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
501 struct smcd_dev *ismdev,
502 unsigned short vlan_id)
503{
504 if (vlan_id && smc_ism_get_vlan(ismdev, vlan_id))
505 return SMC_CLC_DECL_CNFERR;
506 return 0;
507}
508
509/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
510 * used, the VLAN ID will be registered again during the connection setup.
511 */
512static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
513 struct smcd_dev *ismdev,
514 unsigned short vlan_id)
515{
516 if (!is_smcd)
517 return 0;
518 if (vlan_id && smc_ism_put_vlan(ismdev, vlan_id))
519 return SMC_CLC_DECL_CNFERR;
520 return 0;
521}
522
Hans Wippel3b2dec22018-05-18 09:34:18 +0200523/* CLC handshake during connect */
Hans Wippelc758dfd2018-06-28 19:05:09 +0200524static int smc_connect_clc(struct smc_sock *smc, int smc_type,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200525 struct smc_clc_msg_accept_confirm *aclc,
Hans Wippel41349842018-06-28 19:05:11 +0200526 struct smc_ib_device *ibdev, u8 ibport,
Ursula Braun7005ada2018-07-25 16:35:31 +0200527 u8 gid[], struct smcd_dev *ismdev)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200528{
529 int rc = 0;
530
531 /* do inband token exchange */
Ursula Braun7005ada2018-07-25 16:35:31 +0200532 rc = smc_clc_send_proposal(smc, smc_type, ibdev, ibport, gid, ismdev);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200533 if (rc)
534 return rc;
535 /* receive SMC Accept CLC message */
536 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT);
537}
538
Ursula Brauna046d572017-01-09 16:55:16 +0100539/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200540static int smc_connect_rdma(struct smc_sock *smc,
541 struct smc_clc_msg_accept_confirm *aclc,
542 struct smc_ib_device *ibdev, u8 ibport)
543{
544 int local_contact = SMC_FIRST_CONTACT;
545 struct smc_link *link;
546 int reason_code = 0;
547
548 mutex_lock(&smc_create_lgr_pending);
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200549 local_contact = smc_conn_create(smc, false, aclc->hdr.flag, ibdev,
550 ibport, &aclc->lcl, NULL, 0);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200551 if (local_contact < 0) {
552 if (local_contact == -ENOMEM)
553 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
554 else if (local_contact == -ENOLINK)
555 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
556 else
557 reason_code = SMC_CLC_DECL_INTERR; /* other error */
558 return smc_connect_abort(smc, reason_code, 0);
559 }
560 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
561
562 smc_conn_save_peer_info(smc, aclc);
563
564 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200565 if (smc_buf_create(smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +0200566 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, local_contact);
567
568 if (local_contact == SMC_FIRST_CONTACT)
569 smc_link_save_peer_info(link, aclc);
570
571 if (smc_rmb_rtoken_handling(&smc->conn, aclc))
Karsten Graul603cc142018-07-25 16:35:32 +0200572 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200573 local_contact);
574
575 smc_close_init(smc);
576 smc_rx_init(smc);
577
578 if (local_contact == SMC_FIRST_CONTACT) {
579 if (smc_ib_ready_link(link))
Karsten Graul603cc142018-07-25 16:35:32 +0200580 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200581 local_contact);
582 } else {
583 if (!smc->conn.rmb_desc->reused &&
584 smc_reg_rmb(link, smc->conn.rmb_desc, true))
Karsten Graul603cc142018-07-25 16:35:32 +0200585 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200586 local_contact);
587 }
588 smc_rmb_sync_sg_for_device(&smc->conn);
589
590 reason_code = smc_clc_send_confirm(smc);
591 if (reason_code)
592 return smc_connect_abort(smc, reason_code, local_contact);
593
594 smc_tx_init(smc);
595
596 if (local_contact == SMC_FIRST_CONTACT) {
597 /* QP confirmation over RoCE fabric */
598 reason_code = smc_clnt_conf_first_link(smc);
599 if (reason_code)
600 return smc_connect_abort(smc, reason_code,
601 local_contact);
602 }
603 mutex_unlock(&smc_create_lgr_pending);
604
605 smc_copy_sock_settings_to_clc(smc);
606 if (smc->sk.sk_state == SMC_INIT)
607 smc->sk.sk_state = SMC_ACTIVE;
608
609 return 0;
610}
611
Hans Wippel41349842018-06-28 19:05:11 +0200612/* setup for ISM connection of client */
613static int smc_connect_ism(struct smc_sock *smc,
614 struct smc_clc_msg_accept_confirm *aclc,
615 struct smcd_dev *ismdev)
616{
617 int local_contact = SMC_FIRST_CONTACT;
618 int rc = 0;
619
620 mutex_lock(&smc_create_lgr_pending);
621 local_contact = smc_conn_create(smc, true, aclc->hdr.flag, NULL, 0,
622 NULL, ismdev, aclc->gid);
623 if (local_contact < 0)
624 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, 0);
625
626 /* Create send and receive buffers */
627 if (smc_buf_create(smc, true))
628 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, local_contact);
629
630 smc_conn_save_peer_info(smc, aclc);
631 smc_close_init(smc);
632 smc_rx_init(smc);
633 smc_tx_init(smc);
634
635 rc = smc_clc_send_confirm(smc);
636 if (rc)
637 return smc_connect_abort(smc, rc, local_contact);
638 mutex_unlock(&smc_create_lgr_pending);
639
640 smc_copy_sock_settings_to_clc(smc);
641 if (smc->sk.sk_state == SMC_INIT)
642 smc->sk.sk_state = SMC_ACTIVE;
643
644 return 0;
645}
646
Hans Wippel3b2dec22018-05-18 09:34:18 +0200647/* perform steps before actually connecting */
648static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100649{
Hans Wippel41349842018-06-28 19:05:11 +0200650 bool ism_supported = false, rdma_supported = false;
Ursula Brauna046d572017-01-09 16:55:16 +0100651 struct smc_clc_msg_accept_confirm aclc;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200652 struct smc_ib_device *ibdev;
Hans Wippel41349842018-06-28 19:05:11 +0200653 struct smcd_dev *ismdev;
Ursula Braun7005ada2018-07-25 16:35:31 +0200654 u8 gid[SMC_GID_SIZE];
Hans Wippel41349842018-06-28 19:05:11 +0200655 unsigned short vlan;
656 int smc_type;
Ursula Brauna046d572017-01-09 16:55:16 +0100657 int rc = 0;
658 u8 ibport;
659
Ursula Braun51f1de72018-01-26 09:28:48 +0100660 sock_hold(&smc->sk); /* sock put in passive closing */
661
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200662 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +0200663 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200664
Hans Wippel3b2dec22018-05-18 09:34:18 +0200665 /* if peer has not signalled SMC-capability, fall back */
666 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +0200667 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200668
Ursula Brauna046d572017-01-09 16:55:16 +0100669 /* IPSec connections opt out of SMC-R optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200670 if (using_ipsec(smc))
671 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
Ursula Brauna046d572017-01-09 16:55:16 +0100672
Hans Wippel41349842018-06-28 19:05:11 +0200673 /* check for VLAN ID */
674 if (smc_vlan_by_tcpsk(smc->clcsock, &vlan))
675 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_CNFERR);
676
677 /* check if there is an ism device available */
678 if (!smc_check_ism(smc, &ismdev) &&
679 !smc_connect_ism_vlan_setup(smc, ismdev, vlan)) {
680 /* ISM is supported for this connection */
681 ism_supported = true;
682 smc_type = SMC_TYPE_D;
683 }
684
685 /* check if there is a rdma device available */
Ursula Braun7005ada2018-07-25 16:35:31 +0200686 if (!smc_check_rdma(smc, &ibdev, &ibport, vlan, gid)) {
Hans Wippel41349842018-06-28 19:05:11 +0200687 /* RDMA is supported for this connection */
688 rdma_supported = true;
689 if (ism_supported)
690 smc_type = SMC_TYPE_B; /* both */
691 else
692 smc_type = SMC_TYPE_R; /* only RDMA */
693 }
694
695 /* if neither ISM nor RDMA are supported, fallback */
696 if (!rdma_supported && !ism_supported)
Karsten Graul603cc142018-07-25 16:35:32 +0200697 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV);
Ursula Brauna046d572017-01-09 16:55:16 +0100698
Hans Wippel3b2dec22018-05-18 09:34:18 +0200699 /* perform CLC handshake */
Ursula Braun7005ada2018-07-25 16:35:31 +0200700 rc = smc_connect_clc(smc, smc_type, &aclc, ibdev, ibport, gid, ismdev);
Hans Wippel41349842018-06-28 19:05:11 +0200701 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 /* depending on previous steps, connect using rdma or ism */
707 if (rdma_supported && aclc.hdr.path == SMC_TYPE_R)
708 rc = smc_connect_rdma(smc, &aclc, ibdev, ibport);
709 else if (ism_supported && aclc.hdr.path == SMC_TYPE_D)
710 rc = smc_connect_ism(smc, &aclc, ismdev);
711 else
Karsten Graul603cc142018-07-25 16:35:32 +0200712 rc = SMC_CLC_DECL_MODEUNSUPP;
Hans Wippel41349842018-06-28 19:05:11 +0200713 if (rc) {
714 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200715 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200716 }
Ursula Brauna046d572017-01-09 16:55:16 +0100717
Hans Wippel41349842018-06-28 19:05:11 +0200718 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200719 return 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100720}
721
Ursula Braun24ac3a02018-06-27 17:59:50 +0200722static void smc_connect_work(struct work_struct *work)
723{
724 struct smc_sock *smc = container_of(work, struct smc_sock,
725 connect_work);
726 int rc;
727
728 lock_sock(&smc->sk);
729 rc = kernel_connect(smc->clcsock, &smc->connect_info->addr,
730 smc->connect_info->alen, smc->connect_info->flags);
731 if (smc->clcsock->sk->sk_err) {
732 smc->sk.sk_err = smc->clcsock->sk->sk_err;
733 goto out;
734 }
735 if (rc < 0) {
736 smc->sk.sk_err = -rc;
737 goto out;
738 }
739
740 rc = __smc_connect(smc);
741 if (rc < 0)
742 smc->sk.sk_err = -rc;
743
744out:
745 smc->sk.sk_state_change(&smc->sk);
746 kfree(smc->connect_info);
747 smc->connect_info = NULL;
748 release_sock(&smc->sk);
749}
750
Ursula Braunac713872017-01-09 16:55:13 +0100751static int smc_connect(struct socket *sock, struct sockaddr *addr,
752 int alen, int flags)
753{
754 struct sock *sk = sock->sk;
755 struct smc_sock *smc;
756 int rc = -EINVAL;
757
758 smc = smc_sk(sk);
759
760 /* separate smc parameter checking to be safe */
761 if (alen < sizeof(addr->sa_family))
762 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100763 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100764 goto out_err;
765
766 lock_sock(sk);
767 switch (sk->sk_state) {
768 default:
769 goto out;
770 case SMC_ACTIVE:
771 rc = -EISCONN;
772 goto out;
773 case SMC_INIT:
774 rc = 0;
775 break;
776 }
777
778 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200779 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200780 if (flags & O_NONBLOCK) {
781 if (smc->connect_info) {
782 rc = -EALREADY;
783 goto out;
784 }
785 smc->connect_info = kzalloc(alen + 2 * sizeof(int), GFP_KERNEL);
786 if (!smc->connect_info) {
787 rc = -ENOMEM;
788 goto out;
789 }
790 smc->connect_info->alen = alen;
791 smc->connect_info->flags = flags ^ O_NONBLOCK;
792 memcpy(&smc->connect_info->addr, addr, alen);
793 schedule_work(&smc->connect_work);
794 rc = -EINPROGRESS;
795 } else {
796 rc = kernel_connect(smc->clcsock, addr, alen, flags);
797 if (rc)
798 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100799
Ursula Braun24ac3a02018-06-27 17:59:50 +0200800 rc = __smc_connect(smc);
801 if (rc < 0)
802 goto out;
803 else
804 rc = 0; /* success cases including fallback */
805 }
Ursula Braunac713872017-01-09 16:55:13 +0100806
807out:
808 release_sock(sk);
809out_err:
810 return rc;
811}
812
813static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
814{
Ursula Braun3163c502018-01-24 10:28:12 +0100815 struct socket *new_clcsock = NULL;
816 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100817 struct sock *new_sk;
818 int rc;
819
Ursula Braun3163c502018-01-24 10:28:12 +0100820 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100821 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100822 if (!new_sk) {
823 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100824 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100825 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100826 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100827 goto out;
828 }
829 *new_smc = smc_sk(new_sk);
830
831 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
Ursula Braun3163c502018-01-24 10:28:12 +0100832 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100833 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100834 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100835 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Brauna046d572017-01-09 16:55:16 +0100836 if (new_clcsock)
837 sock_release(new_clcsock);
838 new_sk->sk_state = SMC_CLOSED;
839 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun3163c502018-01-24 10:28:12 +0100840 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100841 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100842 *new_smc = NULL;
843 goto out;
844 }
845
846 (*new_smc)->clcsock = new_clcsock;
847out:
848 return rc;
849}
850
Ursula Brauna046d572017-01-09 16:55:16 +0100851/* add a just created sock to the accept queue of the listen sock as
852 * candidate for a following socket accept call from user space
853 */
854static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
855{
856 struct smc_sock *par = smc_sk(parent);
857
Ursula Braun51f1de72018-01-26 09:28:48 +0100858 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100859 spin_lock(&par->accept_q_lock);
860 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
861 spin_unlock(&par->accept_q_lock);
862 sk_acceptq_added(parent);
863}
864
865/* remove a socket from the accept queue of its parental listening socket */
866static void smc_accept_unlink(struct sock *sk)
867{
868 struct smc_sock *par = smc_sk(sk)->listen_smc;
869
870 spin_lock(&par->accept_q_lock);
871 list_del_init(&smc_sk(sk)->accept_q);
872 spin_unlock(&par->accept_q_lock);
873 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100874 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100875}
876
877/* remove a sock from the accept queue to bind it to a new socket created
878 * for a socket accept call from user space
879 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100880struct sock *smc_accept_dequeue(struct sock *parent,
881 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100882{
883 struct smc_sock *isk, *n;
884 struct sock *new_sk;
885
886 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
887 new_sk = (struct sock *)isk;
888
889 smc_accept_unlink(new_sk);
890 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braun127f4972018-01-26 09:28:49 +0100891 if (isk->clcsock) {
892 sock_release(isk->clcsock);
893 isk->clcsock = NULL;
894 }
Ursula Braun288c83902017-04-10 14:58:04 +0200895 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100896 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +0100897 continue;
898 }
899 if (new_sock)
900 sock_graft(new_sk, new_sock);
901 return new_sk;
902 }
903 return NULL;
904}
905
906/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +0100907void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +0100908{
909 struct smc_sock *smc = smc_sk(sk);
910
Ursula Braunb38d7322017-01-09 16:55:25 +0100911 lock_sock(sk);
912 if (!sk->sk_lingertime)
913 /* wait for peer closing */
914 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100915 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100916 smc_close_active(smc);
Ursula Braun288c83902017-04-10 14:58:04 +0200917 sock_set_flag(sk, SOCK_DEAD);
918 sk->sk_shutdown |= SHUTDOWN_MASK;
919 }
Ursula Brauna046d572017-01-09 16:55:16 +0100920 if (smc->clcsock) {
921 struct socket *tcp;
922
923 tcp = smc->clcsock;
924 smc->clcsock = NULL;
925 sock_release(tcp);
926 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100927 if (smc->use_fallback) {
Ursula Braun51f1de72018-01-26 09:28:48 +0100928 sock_put(sk); /* passive closing */
929 sk->sk_state = SMC_CLOSED;
930 } else {
931 if (sk->sk_state == SMC_CLOSED)
932 smc_conn_free(&smc->conn);
Ursula Braunb38d7322017-01-09 16:55:25 +0100933 }
934 release_sock(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100935 sk->sk_prot->unhash(sk);
936 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +0100937}
938
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100939static int smc_serv_conf_first_link(struct smc_sock *smc)
940{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200941 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100942 struct smc_link_group *lgr = smc->conn.lgr;
943 struct smc_link *link;
944 int rest;
945 int rc;
946
947 link = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun652a1e42017-07-28 13:56:17 +0200948
Karsten Graul44aa81c2018-05-15 17:04:55 +0200949 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200950 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200951
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100952 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200953 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100954 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200955 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100956
957 /* receive CONFIRM LINK response from client over the RoCE fabric */
958 rest = wait_for_completion_interruptible_timeout(
959 &link->llc_confirm_resp,
960 SMC_LLC_WAIT_FIRST_TIME);
961 if (rest <= 0) {
962 struct smc_clc_msg_decline dclc;
963
964 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
965 SMC_CLC_DECLINE);
Karsten Graul75d320d2018-03-01 13:51:31 +0100966 return rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100967 }
968
Karsten Graul75d320d2018-03-01 13:51:31 +0100969 if (link->llc_confirm_resp_rc)
970 return SMC_CLC_DECL_RMBE_EC;
971
Karsten Graul52bedf32018-03-01 13:51:32 +0100972 /* send ADD LINK request to client over the RoCE fabric */
973 rc = smc_llc_send_add_link(link,
974 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +0200975 link->gid, SMC_LLC_REQ);
Karsten Graul52bedf32018-03-01 13:51:32 +0100976 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200977 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +0100978
979 /* receive ADD LINK response from client over the RoCE fabric */
980 rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
981 SMC_LLC_WAIT_TIME);
982 if (rest <= 0) {
983 struct smc_clc_msg_decline dclc;
984
985 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
986 SMC_CLC_DECLINE);
987 return rc;
988 }
989
Karsten Graul877ae5b2018-05-02 16:56:44 +0200990 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100991
Karsten Graul75d320d2018-03-01 13:51:31 +0100992 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100993}
994
Hans Wippel3b2dec22018-05-18 09:34:18 +0200995/* listen worker: finish */
996static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100997{
Ursula Brauna046d572017-01-09 16:55:16 +0100998 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +0100999 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001000
Ursula Braunb38d7322017-01-09 16:55:25 +01001001 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001002 if (lsmc->sk.sk_state == SMC_LISTEN) {
1003 smc_accept_enqueue(&lsmc->sk, newsmcsk);
1004 } else { /* no longer listening */
1005 smc_close_non_accepted(newsmcsk);
1006 }
1007 release_sock(&lsmc->sk);
1008
1009 /* Wake up accept */
1010 lsmc->sk.sk_data_ready(&lsmc->sk);
1011 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001012}
Ursula Brauna046d572017-01-09 16:55:16 +01001013
Hans Wippel3b2dec22018-05-18 09:34:18 +02001014/* listen worker: finish in state connected */
1015static void smc_listen_out_connected(struct smc_sock *new_smc)
1016{
1017 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001018
Hans Wippel3b2dec22018-05-18 09:34:18 +02001019 sk_refcnt_debug_inc(newsmcsk);
1020 if (newsmcsk->sk_state == SMC_INIT)
1021 newsmcsk->sk_state = SMC_ACTIVE;
1022
1023 smc_listen_out(new_smc);
1024}
1025
1026/* listen worker: finish in error state */
1027static void smc_listen_out_err(struct smc_sock *new_smc)
1028{
1029 struct sock *newsmcsk = &new_smc->sk;
1030
Ursula Braun51f1de72018-01-26 09:28:48 +01001031 if (newsmcsk->sk_state == SMC_INIT)
1032 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001033 newsmcsk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001034 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001035
1036 smc_listen_out(new_smc);
1037}
1038
1039/* listen worker: decline and fall back if possible */
1040static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1041 int local_contact)
1042{
1043 /* RDMA setup failed, switch back to TCP */
1044 if (local_contact == SMC_FIRST_CONTACT)
1045 smc_lgr_forget(new_smc->conn.lgr);
1046 if (reason_code < 0) { /* error, no fallback possible */
1047 smc_listen_out_err(new_smc);
1048 return;
1049 }
1050 smc_conn_free(&new_smc->conn);
1051 new_smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001052 new_smc->fallback_rsn = reason_code;
1053 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001054 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1055 smc_listen_out_err(new_smc);
1056 return;
1057 }
1058 }
1059 smc_listen_out_connected(new_smc);
1060}
1061
1062/* listen worker: check prefixes */
1063static int smc_listen_rdma_check(struct smc_sock *new_smc,
1064 struct smc_clc_msg_proposal *pclc)
1065{
1066 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1067 struct socket *newclcsock = new_smc->clcsock;
1068
1069 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1070 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
1071 return SMC_CLC_DECL_CNFERR;
1072
1073 return 0;
1074}
1075
1076/* listen worker: initialize connection and buffers */
1077static int smc_listen_rdma_init(struct smc_sock *new_smc,
1078 struct smc_clc_msg_proposal *pclc,
1079 struct smc_ib_device *ibdev, u8 ibport,
1080 int *local_contact)
1081{
1082 /* allocate connection / link group */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001083 *local_contact = smc_conn_create(new_smc, false, 0, ibdev, ibport,
1084 &pclc->lcl, NULL, 0);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001085 if (*local_contact < 0) {
1086 if (*local_contact == -ENOMEM)
1087 return SMC_CLC_DECL_MEM;/* insufficient memory*/
1088 return SMC_CLC_DECL_INTERR; /* other error */
1089 }
1090
1091 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001092 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001093 return SMC_CLC_DECL_MEM;
1094
1095 return 0;
1096}
1097
Hans Wippel41349842018-06-28 19:05:11 +02001098/* listen worker: initialize connection and buffers for SMC-D */
1099static int smc_listen_ism_init(struct smc_sock *new_smc,
1100 struct smc_clc_msg_proposal *pclc,
1101 struct smcd_dev *ismdev,
1102 int *local_contact)
1103{
1104 struct smc_clc_msg_smcd *pclc_smcd;
1105
1106 pclc_smcd = smc_get_clc_msg_smcd(pclc);
1107 *local_contact = smc_conn_create(new_smc, true, 0, NULL, 0, NULL,
1108 ismdev, pclc_smcd->gid);
1109 if (*local_contact < 0) {
1110 if (*local_contact == -ENOMEM)
1111 return SMC_CLC_DECL_MEM;/* insufficient memory*/
1112 return SMC_CLC_DECL_INTERR; /* other error */
1113 }
1114
1115 /* Check if peer can be reached via ISM device */
1116 if (smc_ism_cantalk(new_smc->conn.lgr->peer_gid,
1117 new_smc->conn.lgr->vlan_id,
1118 new_smc->conn.lgr->smcd)) {
1119 if (*local_contact == SMC_FIRST_CONTACT)
1120 smc_lgr_forget(new_smc->conn.lgr);
1121 smc_conn_free(&new_smc->conn);
1122 return SMC_CLC_DECL_CNFERR;
1123 }
1124
1125 /* Create send and receive buffers */
1126 if (smc_buf_create(new_smc, true)) {
1127 if (*local_contact == SMC_FIRST_CONTACT)
1128 smc_lgr_forget(new_smc->conn.lgr);
1129 smc_conn_free(&new_smc->conn);
1130 return SMC_CLC_DECL_MEM;
1131 }
1132
1133 return 0;
1134}
1135
Hans Wippel3b2dec22018-05-18 09:34:18 +02001136/* listen worker: register buffers */
1137static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
1138{
1139 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1140
1141 if (local_contact != SMC_FIRST_CONTACT) {
1142 if (!new_smc->conn.rmb_desc->reused) {
1143 if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true))
Karsten Graul603cc142018-07-25 16:35:32 +02001144 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001145 }
1146 }
1147 smc_rmb_sync_sg_for_device(&new_smc->conn);
1148
1149 return 0;
1150}
1151
1152/* listen worker: finish RDMA setup */
1153static void smc_listen_rdma_finish(struct smc_sock *new_smc,
1154 struct smc_clc_msg_accept_confirm *cclc,
1155 int local_contact)
1156{
1157 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1158 int reason_code = 0;
1159
1160 if (local_contact == SMC_FIRST_CONTACT)
1161 smc_link_save_peer_info(link, cclc);
1162
1163 if (smc_rmb_rtoken_handling(&new_smc->conn, cclc)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001164 reason_code = SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001165 goto decline;
1166 }
1167
1168 if (local_contact == SMC_FIRST_CONTACT) {
1169 if (smc_ib_ready_link(link)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001170 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001171 goto decline;
1172 }
1173 /* QP confirmation over RoCE fabric */
1174 reason_code = smc_serv_conf_first_link(new_smc);
1175 if (reason_code)
1176 goto decline;
1177 }
1178 return;
1179
1180decline:
1181 mutex_unlock(&smc_create_lgr_pending);
1182 smc_listen_decline(new_smc, reason_code, local_contact);
1183}
1184
1185/* setup for RDMA connection of server */
1186static void smc_listen_work(struct work_struct *work)
1187{
1188 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1189 smc_listen_work);
1190 struct socket *newclcsock = new_smc->clcsock;
1191 struct smc_clc_msg_accept_confirm cclc;
1192 struct smc_clc_msg_proposal *pclc;
1193 struct smc_ib_device *ibdev;
Hans Wippel41349842018-06-28 19:05:11 +02001194 bool ism_supported = false;
1195 struct smcd_dev *ismdev;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001196 u8 buf[SMC_CLC_MAX_LEN];
1197 int local_contact = 0;
Ursula Braun7005ada2018-07-25 16:35:31 +02001198 unsigned short vlan;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001199 int reason_code = 0;
1200 int rc = 0;
1201 u8 ibport;
1202
1203 if (new_smc->use_fallback) {
1204 smc_listen_out_connected(new_smc);
1205 return;
1206 }
1207
1208 /* check if peer is smc capable */
1209 if (!tcp_sk(newclcsock->sk)->syn_smc) {
1210 new_smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001211 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001212 smc_listen_out_connected(new_smc);
1213 return;
1214 }
1215
1216 /* do inband token exchange -
1217 * wait for and receive SMC Proposal CLC message
1218 */
1219 pclc = (struct smc_clc_msg_proposal *)&buf;
1220 reason_code = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
1221 SMC_CLC_PROPOSAL);
1222 if (reason_code) {
1223 smc_listen_decline(new_smc, reason_code, 0);
1224 return;
1225 }
1226
1227 /* IPSec connections opt out of SMC-R optimizations */
1228 if (using_ipsec(new_smc)) {
1229 smc_listen_decline(new_smc, SMC_CLC_DECL_IPSEC, 0);
1230 return;
1231 }
1232
1233 mutex_lock(&smc_create_lgr_pending);
1234 smc_close_init(new_smc);
1235 smc_rx_init(new_smc);
1236 smc_tx_init(new_smc);
1237
Hans Wippel41349842018-06-28 19:05:11 +02001238 /* check if ISM is available */
1239 if ((pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) &&
1240 !smc_check_ism(new_smc, &ismdev) &&
1241 !smc_listen_ism_init(new_smc, pclc, ismdev, &local_contact)) {
1242 ism_supported = true;
1243 }
1244
Hans Wippel3b2dec22018-05-18 09:34:18 +02001245 /* check if RDMA is available */
Hans Wippel41349842018-06-28 19:05:11 +02001246 if (!ism_supported &&
1247 ((pclc->hdr.path != SMC_TYPE_R && pclc->hdr.path != SMC_TYPE_B) ||
Ursula Braun7005ada2018-07-25 16:35:31 +02001248 smc_vlan_by_tcpsk(new_smc->clcsock, &vlan) ||
1249 smc_check_rdma(new_smc, &ibdev, &ibport, vlan, NULL) ||
Hans Wippel41349842018-06-28 19:05:11 +02001250 smc_listen_rdma_check(new_smc, pclc) ||
1251 smc_listen_rdma_init(new_smc, pclc, ibdev, ibport,
1252 &local_contact) ||
1253 smc_listen_rdma_reg(new_smc, local_contact))) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001254 /* SMC not supported, decline */
1255 mutex_unlock(&smc_create_lgr_pending);
Karsten Graul603cc142018-07-25 16:35:32 +02001256 smc_listen_decline(new_smc, SMC_CLC_DECL_MODEUNSUPP,
1257 local_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001258 return;
1259 }
1260
1261 /* send SMC Accept CLC message */
1262 rc = smc_clc_send_accept(new_smc, local_contact);
1263 if (rc) {
1264 mutex_unlock(&smc_create_lgr_pending);
1265 smc_listen_decline(new_smc, rc, local_contact);
1266 return;
1267 }
1268
1269 /* receive SMC Confirm CLC message */
1270 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
1271 SMC_CLC_CONFIRM);
1272 if (reason_code) {
1273 mutex_unlock(&smc_create_lgr_pending);
1274 smc_listen_decline(new_smc, reason_code, local_contact);
1275 return;
1276 }
1277
1278 /* finish worker */
Hans Wippel41349842018-06-28 19:05:11 +02001279 if (!ism_supported)
1280 smc_listen_rdma_finish(new_smc, &cclc, local_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001281 smc_conn_save_peer_info(new_smc, &cclc);
1282 mutex_unlock(&smc_create_lgr_pending);
1283 smc_listen_out_connected(new_smc);
Ursula Brauna046d572017-01-09 16:55:16 +01001284}
1285
1286static void smc_tcp_listen_work(struct work_struct *work)
1287{
1288 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1289 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001290 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001291 struct smc_sock *new_smc;
1292 int rc = 0;
1293
Ursula Braun3163c502018-01-24 10:28:12 +01001294 lock_sock(lsk);
1295 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001296 rc = smc_clcsock_accept(lsmc, &new_smc);
1297 if (rc)
1298 goto out;
1299 if (!new_smc)
1300 continue;
1301
1302 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001303 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001304 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001305 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001306 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1307 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braun51f1de72018-01-26 09:28:48 +01001308 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1309 if (!schedule_work(&new_smc->smc_listen_work))
1310 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001311 }
1312
1313out:
Ursula Braun3163c502018-01-24 10:28:12 +01001314 release_sock(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001315 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +01001316}
1317
Ursula Braunac713872017-01-09 16:55:13 +01001318static int smc_listen(struct socket *sock, int backlog)
1319{
1320 struct sock *sk = sock->sk;
1321 struct smc_sock *smc;
1322 int rc;
1323
1324 smc = smc_sk(sk);
1325 lock_sock(sk);
1326
1327 rc = -EINVAL;
1328 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
1329 goto out;
1330
1331 rc = 0;
1332 if (sk->sk_state == SMC_LISTEN) {
1333 sk->sk_max_ack_backlog = backlog;
1334 goto out;
1335 }
1336 /* some socket options are handled in core, so we could not apply
1337 * them to the clc socket -- copy smc socket options to clc socket
1338 */
1339 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001340 if (!smc->use_fallback)
1341 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001342
1343 rc = kernel_listen(smc->clcsock, backlog);
1344 if (rc)
1345 goto out;
1346 sk->sk_max_ack_backlog = backlog;
1347 sk->sk_ack_backlog = 0;
1348 sk->sk_state = SMC_LISTEN;
Ursula Brauna046d572017-01-09 16:55:16 +01001349 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun51f1de72018-01-26 09:28:48 +01001350 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1351 if (!schedule_work(&smc->tcp_listen_work))
1352 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001353
1354out:
1355 release_sock(sk);
1356 return rc;
1357}
1358
1359static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001360 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001361{
Ursula Brauna046d572017-01-09 16:55:16 +01001362 struct sock *sk = sock->sk, *nsk;
1363 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001364 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001365 long timeo;
1366 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001367
1368 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001369 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001370 lock_sock(sk);
1371
1372 if (lsmc->sk.sk_state != SMC_LISTEN) {
1373 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001374 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001375 goto out;
1376 }
1377
Ursula Brauna046d572017-01-09 16:55:16 +01001378 /* Wait for an incoming connection */
1379 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1380 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1381 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1382 set_current_state(TASK_INTERRUPTIBLE);
1383 if (!timeo) {
1384 rc = -EAGAIN;
1385 break;
1386 }
1387 release_sock(sk);
1388 timeo = schedule_timeout(timeo);
1389 /* wakeup by sk_data_ready in smc_listen_work() */
1390 sched_annotate_sleep();
1391 lock_sock(sk);
1392 if (signal_pending(current)) {
1393 rc = sock_intr_errno(timeo);
1394 break;
1395 }
1396 }
1397 set_current_state(TASK_RUNNING);
1398 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001399
Ursula Brauna046d572017-01-09 16:55:16 +01001400 if (!rc)
1401 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001402 release_sock(sk);
1403 if (rc)
1404 goto out;
1405
1406 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1407 /* wait till data arrives on the socket */
1408 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1409 MSEC_PER_SEC);
1410 if (smc_sk(nsk)->use_fallback) {
1411 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1412
1413 lock_sock(clcsk);
1414 if (skb_queue_empty(&clcsk->sk_receive_queue))
1415 sk_wait_data(clcsk, &timeo, NULL);
1416 release_sock(clcsk);
1417 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1418 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001419 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001420 release_sock(nsk);
1421 }
1422 }
Ursula Braunac713872017-01-09 16:55:13 +01001423
1424out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001425 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001426 return rc;
1427}
1428
1429static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001430 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001431{
1432 struct smc_sock *smc;
1433
Ursula Braunb38d7322017-01-09 16:55:25 +01001434 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1435 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001436 return -ENOTCONN;
1437
1438 smc = smc_sk(sock->sk);
1439
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001440 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001441}
1442
1443static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1444{
1445 struct sock *sk = sock->sk;
1446 struct smc_sock *smc;
1447 int rc = -EPIPE;
1448
1449 smc = smc_sk(sk);
1450 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001451 if ((sk->sk_state != SMC_ACTIVE) &&
1452 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1453 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001454 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001455
1456 if (msg->msg_flags & MSG_FASTOPEN) {
1457 if (sk->sk_state == SMC_INIT) {
1458 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001459 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001460 } else {
1461 rc = -EINVAL;
1462 goto out;
1463 }
1464 }
1465
Ursula Braunac713872017-01-09 16:55:13 +01001466 if (smc->use_fallback)
1467 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1468 else
Ursula Braune6727f32017-01-09 16:55:23 +01001469 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001470out:
1471 release_sock(sk);
1472 return rc;
1473}
1474
1475static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1476 int flags)
1477{
1478 struct sock *sk = sock->sk;
1479 struct smc_sock *smc;
1480 int rc = -ENOTCONN;
1481
1482 smc = smc_sk(sk);
1483 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001484 if ((sk->sk_state == SMC_INIT) ||
1485 (sk->sk_state == SMC_LISTEN) ||
1486 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001487 goto out;
1488
Ursula Braunb38d7322017-01-09 16:55:25 +01001489 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1490 rc = 0;
1491 goto out;
1492 }
1493
Stefan Raspl9014db22018-05-03 18:12:39 +02001494 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001495 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001496 } else {
1497 msg->msg_namelen = 0;
1498 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1499 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001500
Ursula Braunac713872017-01-09 16:55:13 +01001501out:
1502 release_sock(sk);
1503 return rc;
1504}
1505
Al Viroade994f2017-07-03 00:01:49 -04001506static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001507{
Ursula Braun8dce2782018-01-26 09:28:47 +01001508 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001509 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001510
Ursula Braun8dce2782018-01-26 09:28:47 +01001511 spin_lock(&isk->accept_q_lock);
1512 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001513 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001514 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001515
Ursula Braun8dce2782018-01-26 09:28:47 +01001516 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001517}
1518
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001519static __poll_t smc_poll(struct file *file, struct socket *sock,
1520 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01001521{
1522 struct sock *sk = sock->sk;
Al Viroe6c8adc2017-07-03 22:25:56 -04001523 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001524 struct smc_sock *smc;
1525
Ursula Braun8dce2782018-01-26 09:28:47 +01001526 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001527 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001528
Ursula Braunac713872017-01-09 16:55:13 +01001529 smc = smc_sk(sock->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001530 if ((sk->sk_state == SMC_INIT) || smc->use_fallback) {
1531 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001532 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02001533 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001534 if (sk->sk_err)
Ursula Braun784813a2018-05-02 16:53:56 +02001535 mask |= EPOLLERR;
Ursula Braunac713872017-01-09 16:55:13 +01001536 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07001537 if (sk->sk_state != SMC_CLOSED)
1538 sock_poll_wait(file, sk_sleep(sk), wait);
Ursula Brauna046d572017-01-09 16:55:16 +01001539 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001540 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001541 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1542 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001543 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001544 if (sk->sk_state == SMC_LISTEN) {
1545 /* woken up by sk_data_ready in smc_listen_work() */
1546 mask = smc_accept_poll(sk);
1547 } else {
1548 if (atomic_read(&smc->conn.sndbuf_space) ||
1549 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001550 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001551 } else {
1552 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1553 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1554 }
1555 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001556 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001557 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001558 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001559 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001560 mask |= EPOLLIN;
Ursula Braun8dce2782018-01-26 09:28:47 +01001561 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001562 if (smc->conn.urg_state == SMC_URG_VALID)
1563 mask |= EPOLLPRI;
Ursula Braunac713872017-01-09 16:55:13 +01001564 }
1565
1566 return mask;
1567}
1568
1569static int smc_shutdown(struct socket *sock, int how)
1570{
1571 struct sock *sk = sock->sk;
1572 struct smc_sock *smc;
1573 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001574 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001575
1576 smc = smc_sk(sk);
1577
1578 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001579 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001580
1581 lock_sock(sk);
1582
1583 rc = -ENOTCONN;
Ursula Braunb38d7322017-01-09 16:55:25 +01001584 if ((sk->sk_state != SMC_LISTEN) &&
1585 (sk->sk_state != SMC_ACTIVE) &&
1586 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1587 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1588 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1589 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1590 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001591 goto out;
1592 if (smc->use_fallback) {
1593 rc = kernel_sock_shutdown(smc->clcsock, how);
1594 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1595 if (sk->sk_shutdown == SHUTDOWN_MASK)
1596 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001597 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001598 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001599 switch (how) {
1600 case SHUT_RDWR: /* shutdown in both directions */
1601 rc = smc_close_active(smc);
1602 break;
1603 case SHUT_WR:
1604 rc = smc_close_shutdown_write(smc);
1605 break;
1606 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001607 rc = 0;
1608 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001609 break;
1610 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001611 if (smc->clcsock)
1612 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001613 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1614 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001615
1616out:
1617 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001618 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001619}
1620
1621static int smc_setsockopt(struct socket *sock, int level, int optname,
1622 char __user *optval, unsigned int optlen)
1623{
1624 struct sock *sk = sock->sk;
1625 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001626 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001627
1628 smc = smc_sk(sk);
1629
1630 /* generic setsockopts reaching us here always apply to the
1631 * CLC socket
1632 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001633 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1634 optval, optlen);
1635 if (smc->clcsock->sk->sk_err) {
1636 sk->sk_err = smc->clcsock->sk->sk_err;
1637 sk->sk_error_report(sk);
1638 }
1639 if (rc)
1640 return rc;
1641
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001642 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00001643 return -EINVAL;
Ursula Braunac0107e2018-07-18 15:22:50 +02001644 if (get_user(val, (int __user *)optval))
1645 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001646
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001647 lock_sock(sk);
1648 switch (optname) {
1649 case TCP_ULP:
1650 case TCP_FASTOPEN:
1651 case TCP_FASTOPEN_CONNECT:
1652 case TCP_FASTOPEN_KEY:
1653 case TCP_FASTOPEN_NO_COOKIE:
1654 /* option not supported by SMC */
1655 if (sk->sk_state == SMC_INIT) {
1656 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001657 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001658 } else {
1659 if (!smc->use_fallback)
1660 rc = -EINVAL;
1661 }
1662 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001663 case TCP_NODELAY:
1664 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001665 if (val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001666 mod_delayed_work(system_wq, &smc->conn.tx_work,
1667 0);
1668 }
1669 break;
1670 case TCP_CORK:
1671 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001672 if (!val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001673 mod_delayed_work(system_wq, &smc->conn.tx_work,
1674 0);
1675 }
1676 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001677 case TCP_DEFER_ACCEPT:
1678 smc->sockopt_defer_accept = val;
1679 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001680 default:
1681 break;
1682 }
1683 release_sock(sk);
1684
1685 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001686}
1687
1688static int smc_getsockopt(struct socket *sock, int level, int optname,
1689 char __user *optval, int __user *optlen)
1690{
1691 struct smc_sock *smc;
1692
1693 smc = smc_sk(sock->sk);
1694 /* socket options apply to the CLC socket */
1695 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1696 optval, optlen);
1697}
1698
1699static int smc_ioctl(struct socket *sock, unsigned int cmd,
1700 unsigned long arg)
1701{
Stefan Rasplde8474e2018-05-23 16:38:11 +02001702 union smc_host_cursor cons, urg;
1703 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01001704 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001705 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001706
1707 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001708 conn = &smc->conn;
Ursula Braun9b67e262018-05-02 16:56:46 +02001709 if (smc->use_fallback) {
1710 if (!smc->clcsock)
1711 return -EBADF;
Ursula Braunac713872017-01-09 16:55:13 +01001712 return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
Ursula Braun9b67e262018-05-02 16:56:46 +02001713 }
Ursula Braun1992d992018-07-16 13:56:52 +02001714 lock_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001715 switch (cmd) {
1716 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02001717 if (smc->sk.sk_state == SMC_LISTEN) {
1718 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001719 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001720 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001721 if (smc->sk.sk_state == SMC_INIT ||
1722 smc->sk.sk_state == SMC_CLOSED)
1723 answ = 0;
1724 else
1725 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02001726 break;
1727 case SIOCOUTQ:
1728 /* output queue size (not send + not acked) */
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 = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02001738 atomic_read(&smc->conn.sndbuf_space);
1739 break;
1740 case SIOCOUTQNSD:
1741 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02001742 if (smc->sk.sk_state == SMC_LISTEN) {
1743 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001744 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001745 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001746 if (smc->sk.sk_state == SMC_INIT ||
1747 smc->sk.sk_state == SMC_CLOSED)
1748 answ = 0;
1749 else
1750 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02001751 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02001752 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02001753 if (smc->sk.sk_state == SMC_LISTEN) {
1754 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001755 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001756 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001757 if (smc->sk.sk_state == SMC_INIT ||
1758 smc->sk.sk_state == SMC_CLOSED) {
1759 answ = 0;
1760 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02001761 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1762 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001763 answ = smc_curs_diff(conn->rmb_desc->len,
1764 &cons, &urg) == 1;
1765 }
1766 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02001767 default:
Ursula Braun1992d992018-07-16 13:56:52 +02001768 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001769 return -ENOIOCTLCMD;
1770 }
Ursula Braun1992d992018-07-16 13:56:52 +02001771 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001772
1773 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001774}
1775
1776static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1777 int offset, size_t size, int flags)
1778{
1779 struct sock *sk = sock->sk;
1780 struct smc_sock *smc;
1781 int rc = -EPIPE;
1782
1783 smc = smc_sk(sk);
1784 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001785 if (sk->sk_state != SMC_ACTIVE) {
1786 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001787 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001788 }
1789 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001790 if (smc->use_fallback)
1791 rc = kernel_sendpage(smc->clcsock, page, offset,
1792 size, flags);
1793 else
1794 rc = sock_no_sendpage(sock, page, offset, size, flags);
1795
1796out:
Ursula Braunac713872017-01-09 16:55:13 +01001797 return rc;
1798}
1799
Stefan Raspl9014db22018-05-03 18:12:39 +02001800/* Map the affected portions of the rmbe into an spd, note the number of bytes
1801 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1802 * updates till whenever a respective page has been fully processed.
1803 * Note that subsequent recv() calls have to wait till all splice() processing
1804 * completed.
1805 */
Ursula Braunac713872017-01-09 16:55:13 +01001806static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1807 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001808 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001809{
1810 struct sock *sk = sock->sk;
1811 struct smc_sock *smc;
1812 int rc = -ENOTCONN;
1813
1814 smc = smc_sk(sk);
1815 lock_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001816
1817 if (sk->sk_state == SMC_INIT ||
1818 sk->sk_state == SMC_LISTEN ||
1819 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001820 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001821
1822 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1823 rc = 0;
1824 goto out;
1825 }
1826
Ursula Braunac713872017-01-09 16:55:13 +01001827 if (smc->use_fallback) {
1828 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1829 pipe, len, flags);
1830 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001831 if (*ppos) {
1832 rc = -ESPIPE;
1833 goto out;
1834 }
1835 if (flags & SPLICE_F_NONBLOCK)
1836 flags = MSG_DONTWAIT;
1837 else
1838 flags = 0;
1839 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001840 }
1841out:
1842 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001843
Ursula Braunac713872017-01-09 16:55:13 +01001844 return rc;
1845}
1846
1847/* must look like tcp */
1848static const struct proto_ops smc_sock_ops = {
1849 .family = PF_SMC,
1850 .owner = THIS_MODULE,
1851 .release = smc_release,
1852 .bind = smc_bind,
1853 .connect = smc_connect,
1854 .socketpair = sock_no_socketpair,
1855 .accept = smc_accept,
1856 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001857 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01001858 .ioctl = smc_ioctl,
1859 .listen = smc_listen,
1860 .shutdown = smc_shutdown,
1861 .setsockopt = smc_setsockopt,
1862 .getsockopt = smc_getsockopt,
1863 .sendmsg = smc_sendmsg,
1864 .recvmsg = smc_recvmsg,
1865 .mmap = sock_no_mmap,
1866 .sendpage = smc_sendpage,
1867 .splice_read = smc_splice_read,
1868};
1869
1870static int smc_create(struct net *net, struct socket *sock, int protocol,
1871 int kern)
1872{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001873 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001874 struct smc_sock *smc;
1875 struct sock *sk;
1876 int rc;
1877
1878 rc = -ESOCKTNOSUPPORT;
1879 if (sock->type != SOCK_STREAM)
1880 goto out;
1881
1882 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001883 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01001884 goto out;
1885
1886 rc = -ENOBUFS;
1887 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001888 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001889 if (!sk)
1890 goto out;
1891
1892 /* create internal TCP socket for CLC handshake and fallback */
1893 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001894 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02001895 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001896 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1897 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01001898 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01001899 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01001900 goto out;
1901 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01001902 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1903 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01001904
1905out:
1906 return rc;
1907}
1908
1909static const struct net_proto_family smc_sock_family_ops = {
1910 .family = PF_SMC,
1911 .owner = THIS_MODULE,
1912 .create = smc_create,
1913};
1914
1915static int __init smc_init(void)
1916{
1917 int rc;
1918
Thomas Richter6812baa2017-01-09 16:55:15 +01001919 rc = smc_pnet_init();
1920 if (rc)
1921 return rc;
1922
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001923 rc = smc_llc_init();
1924 if (rc) {
1925 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1926 goto out_pnet;
1927 }
1928
Ursula Braun5f083182017-01-09 16:55:22 +01001929 rc = smc_cdc_init();
1930 if (rc) {
1931 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1932 goto out_pnet;
1933 }
1934
Ursula Braunac713872017-01-09 16:55:13 +01001935 rc = proto_register(&smc_proto, 1);
1936 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01001937 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Thomas Richter6812baa2017-01-09 16:55:15 +01001938 goto out_pnet;
Ursula Braunac713872017-01-09 16:55:13 +01001939 }
1940
Karsten Graulaaa4d332018-03-16 15:06:41 +01001941 rc = proto_register(&smc_proto6, 1);
1942 if (rc) {
1943 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
1944 goto out_proto;
1945 }
1946
Ursula Braunac713872017-01-09 16:55:13 +01001947 rc = sock_register(&smc_sock_family_ops);
1948 if (rc) {
1949 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001950 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01001951 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01001952 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001953 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01001954
Ursula Brauna4cf0442017-01-09 16:55:14 +01001955 rc = smc_ib_register_client();
1956 if (rc) {
1957 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1958 goto out_sock;
1959 }
1960
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001961 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01001962 return 0;
1963
Ursula Brauna4cf0442017-01-09 16:55:14 +01001964out_sock:
1965 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001966out_proto6:
1967 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01001968out_proto:
1969 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001970out_pnet:
1971 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001972 return rc;
1973}
1974
1975static void __exit smc_exit(void)
1976{
Hans Wippel9fda3512018-05-18 09:34:11 +02001977 smc_core_exit();
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001978 static_branch_disable(&tcp_have_smc);
Ursula Brauna4cf0442017-01-09 16:55:14 +01001979 smc_ib_unregister_client();
Ursula Braunac713872017-01-09 16:55:13 +01001980 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001981 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01001982 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001983 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001984}
1985
1986module_init(smc_init);
1987module_exit(smc_exit);
1988
1989MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
1990MODULE_DESCRIPTION("smc socket address family");
1991MODULE_LICENSE("GPL");
1992MODULE_ALIAS_NETPROTO(PF_SMC);