blob: 5fbaf1901571cd2e41a65773ac2f3dc7fa80525d [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 */
Hans Wippelf07920a2018-11-20 16:46:39 +0100130 if (smc->connect_info && sk->sk_state == SMC_INIT)
131 tcp_abort(smc->clcsock->sk, ECONNABORTED);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200132 flush_work(&smc->connect_work);
133 kfree(smc->connect_info);
134 smc->connect_info = NULL;
135
Ursula Braunb38d7322017-01-09 16:55:25 +0100136 if (sk->sk_state == SMC_LISTEN)
137 /* smc_close_non_accepted() is called and acquires
138 * sock lock for child sockets again
139 */
140 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
141 else
142 lock_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +0100143
Ursula Braun51f1de72018-01-26 09:28:48 +0100144 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100145 rc = smc_close_active(smc);
146 sock_set_flag(sk, SOCK_DEAD);
147 sk->sk_shutdown |= SHUTDOWN_MASK;
148 }
Ursula Braunac713872017-01-09 16:55:13 +0100149 if (smc->clcsock) {
150 sock_release(smc->clcsock);
151 smc->clcsock = NULL;
152 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100153 if (smc->use_fallback) {
Ursula Braune1bbdd52018-07-05 16:15:30 +0200154 if (sk->sk_state != SMC_LISTEN && sk->sk_state != SMC_INIT)
155 sock_put(sk); /* passive closing */
Ursula Braun51f1de72018-01-26 09:28:48 +0100156 sk->sk_state = SMC_CLOSED;
157 sk->sk_state_change(sk);
158 }
Ursula Braunac713872017-01-09 16:55:13 +0100159
160 /* detach socket */
161 sock_orphan(sk);
162 sock->sk = NULL;
Ursula Braun51f1de72018-01-26 09:28:48 +0100163 if (!smc->use_fallback && sk->sk_state == SMC_CLOSED)
Ursula Braunb38d7322017-01-09 16:55:25 +0100164 smc_conn_free(&smc->conn);
Ursula Braunac713872017-01-09 16:55:13 +0100165 release_sock(sk);
166
Ursula Braun51f1de72018-01-26 09:28:48 +0100167 sk->sk_prot->unhash(sk);
168 sock_put(sk); /* final sock_put */
Ursula Braunac713872017-01-09 16:55:13 +0100169out:
Ursula Braunb38d7322017-01-09 16:55:25 +0100170 return rc;
Ursula Braunac713872017-01-09 16:55:13 +0100171}
172
173static void smc_destruct(struct sock *sk)
174{
175 if (sk->sk_state != SMC_CLOSED)
176 return;
177 if (!sock_flag(sk, SOCK_DEAD))
178 return;
179
180 sk_refcnt_debug_dec(sk);
181}
182
Karsten Graulaaa4d332018-03-16 15:06:41 +0100183static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
184 int protocol)
Ursula Braunac713872017-01-09 16:55:13 +0100185{
186 struct smc_sock *smc;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100187 struct proto *prot;
Ursula Braunac713872017-01-09 16:55:13 +0100188 struct sock *sk;
189
Karsten Graulaaa4d332018-03-16 15:06:41 +0100190 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
191 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
Ursula Braunac713872017-01-09 16:55:13 +0100192 if (!sk)
193 return NULL;
194
195 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
196 sk->sk_state = SMC_INIT;
197 sk->sk_destruct = smc_destruct;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100198 sk->sk_protocol = protocol;
Ursula Braunac713872017-01-09 16:55:13 +0100199 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100200 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200201 INIT_WORK(&smc->connect_work, smc_connect_work);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700202 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
Ursula Brauna046d572017-01-09 16:55:16 +0100203 INIT_LIST_HEAD(&smc->accept_q);
204 spin_lock_init(&smc->accept_q_lock);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700205 spin_lock_init(&smc->conn.send_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100206 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100207 sk_refcnt_debug_inc(sk);
Ursula Braunac713872017-01-09 16:55:13 +0100208
209 return sk;
210}
211
212static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
213 int addr_len)
214{
215 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
216 struct sock *sk = sock->sk;
217 struct smc_sock *smc;
218 int rc;
219
220 smc = smc_sk(sk);
221
222 /* replicate tests from inet_bind(), to be safe wrt. future changes */
223 rc = -EINVAL;
224 if (addr_len < sizeof(struct sockaddr_in))
225 goto out;
226
227 rc = -EAFNOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100228 if (addr->sin_family != AF_INET &&
229 addr->sin_family != AF_INET6 &&
230 addr->sin_family != AF_UNSPEC)
231 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100232 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
Karsten Graulaaa4d332018-03-16 15:06:41 +0100233 if (addr->sin_family == AF_UNSPEC &&
234 addr->sin_addr.s_addr != htonl(INADDR_ANY))
Ursula Braunac713872017-01-09 16:55:13 +0100235 goto out;
236
237 lock_sock(sk);
238
239 /* Check if socket is already active */
240 rc = -EINVAL;
241 if (sk->sk_state != SMC_INIT)
242 goto out_rel;
243
244 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
245 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
246
247out_rel:
248 release_sock(sk);
249out:
250 return rc;
251}
252
253static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
254 unsigned long mask)
255{
256 /* options we don't get control via setsockopt for */
257 nsk->sk_type = osk->sk_type;
258 nsk->sk_sndbuf = osk->sk_sndbuf;
259 nsk->sk_rcvbuf = osk->sk_rcvbuf;
260 nsk->sk_sndtimeo = osk->sk_sndtimeo;
261 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
262 nsk->sk_mark = osk->sk_mark;
263 nsk->sk_priority = osk->sk_priority;
264 nsk->sk_rcvlowat = osk->sk_rcvlowat;
265 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
266 nsk->sk_err = osk->sk_err;
267
268 nsk->sk_flags &= ~mask;
269 nsk->sk_flags |= osk->sk_flags & mask;
270}
271
272#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
273 (1UL << SOCK_KEEPOPEN) | \
274 (1UL << SOCK_LINGER) | \
275 (1UL << SOCK_BROADCAST) | \
276 (1UL << SOCK_TIMESTAMP) | \
277 (1UL << SOCK_DBG) | \
278 (1UL << SOCK_RCVTSTAMP) | \
279 (1UL << SOCK_RCVTSTAMPNS) | \
280 (1UL << SOCK_LOCALROUTE) | \
281 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
282 (1UL << SOCK_RXQ_OVFL) | \
283 (1UL << SOCK_WIFI_STATUS) | \
284 (1UL << SOCK_NOFCS) | \
285 (1UL << SOCK_FILTER_LOCKED))
286/* copy only relevant settings and flags of SOL_SOCKET level from smc to
287 * clc socket (since smc is not called for these options from net/core)
288 */
289static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
290{
291 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
292}
293
294#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
295 (1UL << SOCK_KEEPOPEN) | \
296 (1UL << SOCK_LINGER) | \
297 (1UL << SOCK_DBG))
298/* copy only settings and flags relevant for smc from clc to smc socket */
299static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
300{
301 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
302}
303
Karsten Graul44aa81c2018-05-15 17:04:55 +0200304/* register a new rmb, optionally send confirm_rkey msg to register with peer */
305static int smc_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc,
306 bool conf_rkey)
Karsten Graule63a5f82018-05-03 17:57:37 +0200307{
308 /* register memory region for new rmb */
Karsten Graula6920d12018-05-03 17:57:38 +0200309 if (smc_wr_reg_send(link, rmb_desc->mr_rx[SMC_SINGLE_LINK])) {
310 rmb_desc->regerr = 1;
Karsten Graule63a5f82018-05-03 17:57:37 +0200311 return -EFAULT;
Karsten Graula6920d12018-05-03 17:57:38 +0200312 }
Karsten Graul44aa81c2018-05-15 17:04:55 +0200313 if (!conf_rkey)
314 return 0;
315 /* exchange confirm_rkey msg with peer */
316 if (smc_llc_do_confirm_rkey(link, rmb_desc)) {
317 rmb_desc->regerr = 1;
318 return -EFAULT;
319 }
Karsten Graule63a5f82018-05-03 17:57:37 +0200320 return 0;
321}
322
Stefan Raspl0f627122018-03-01 13:51:26 +0100323static int smc_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100324{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200325 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100326 struct smc_link_group *lgr = smc->conn.lgr;
327 struct smc_link *link;
328 int rest;
329 int rc;
330
331 link = &lgr->lnk[SMC_SINGLE_LINK];
332 /* receive CONFIRM LINK request from server over RoCE fabric */
333 rest = wait_for_completion_interruptible_timeout(
334 &link->llc_confirm,
335 SMC_LLC_WAIT_FIRST_TIME);
336 if (rest <= 0) {
337 struct smc_clc_msg_decline dclc;
338
339 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
340 SMC_CLC_DECLINE);
341 return rc;
342 }
343
Karsten Graul75d320d2018-03-01 13:51:31 +0100344 if (link->llc_confirm_rc)
345 return SMC_CLC_DECL_RMBE_EC;
346
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100347 rc = smc_ib_modify_qp_rts(link);
348 if (rc)
Karsten Graul603cc142018-07-25 16:35:32 +0200349 return SMC_CLC_DECL_ERR_RDYLNK;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100350
351 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200352
Karsten Graul44aa81c2018-05-15 17:04:55 +0200353 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200354 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200355
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100356 /* send CONFIRM LINK response over RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200357 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100358 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200359 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100360
Karsten Graul52bedf32018-03-01 13:51:32 +0100361 /* receive ADD LINK request from server over RoCE fabric */
362 rest = wait_for_completion_interruptible_timeout(&link->llc_add,
363 SMC_LLC_WAIT_TIME);
364 if (rest <= 0) {
365 struct smc_clc_msg_decline dclc;
366
367 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
368 SMC_CLC_DECLINE);
369 return rc;
370 }
371
372 /* send add link reject message, only one link supported for now */
373 rc = smc_llc_send_add_link(link,
374 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +0200375 link->gid, SMC_LLC_RESP);
Karsten Graul52bedf32018-03-01 13:51:32 +0100376 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200377 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +0100378
Karsten Graul877ae5b2018-05-02 16:56:44 +0200379 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100380
Karsten Graul75d320d2018-03-01 13:51:31 +0100381 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100382}
383
Hans Wippel41349842018-06-28 19:05:11 +0200384static void smcr_conn_save_peer_info(struct smc_sock *smc,
385 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100386{
Hans Wippel95d8d2632018-05-18 09:34:13 +0200387 int bufsize = smc_uncompress_bufsize(clc->rmbe_size);
388
Hans Wippel92a138e2018-05-18 09:34:12 +0200389 smc->conn.peer_rmbe_idx = clc->rmbe_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100390 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200391 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100392 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200393 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100394}
395
Hans Wippel41349842018-06-28 19:05:11 +0200396static void smcd_conn_save_peer_info(struct smc_sock *smc,
397 struct smc_clc_msg_accept_confirm *clc)
398{
399 int bufsize = smc_uncompress_bufsize(clc->dmbe_size);
400
401 smc->conn.peer_rmbe_idx = clc->dmbe_idx;
402 smc->conn.peer_token = clc->token;
403 /* msg header takes up space in the buffer */
404 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
405 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
406 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
407}
408
409static void smc_conn_save_peer_info(struct smc_sock *smc,
410 struct smc_clc_msg_accept_confirm *clc)
411{
412 if (smc->conn.lgr->is_smcd)
413 smcd_conn_save_peer_info(smc, clc);
414 else
415 smcr_conn_save_peer_info(smc, clc);
416}
417
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100418static void smc_link_save_peer_info(struct smc_link *link,
419 struct smc_clc_msg_accept_confirm *clc)
420{
421 link->peer_qpn = ntoh24(clc->qpn);
422 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
423 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
424 link->peer_psn = ntoh24(clc->psn);
425 link->peer_mtu = clc->qp_mtu;
426}
427
Hans Wippel3b2dec22018-05-18 09:34:18 +0200428/* fall back during connect */
Karsten Graul603cc142018-07-25 16:35:32 +0200429static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200430{
431 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +0200432 smc->fallback_rsn = reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200433 smc_copy_sock_settings_to_clc(smc);
434 if (smc->sk.sk_state == SMC_INIT)
435 smc->sk.sk_state = SMC_ACTIVE;
436 return 0;
437}
438
439/* decline and fall back during connect */
440static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
441{
442 int rc;
443
Ursula Braune1bbdd52018-07-05 16:15:30 +0200444 if (reason_code < 0) { /* error, fallback is not possible */
445 if (smc->sk.sk_state == SMC_INIT)
446 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200447 return reason_code;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200448 }
Karsten Graul603cc142018-07-25 16:35:32 +0200449 if (reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200450 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200451 if (rc < 0) {
452 if (smc->sk.sk_state == SMC_INIT)
453 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200454 return rc;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200455 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200456 }
Karsten Graul603cc142018-07-25 16:35:32 +0200457 return smc_connect_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200458}
459
460/* abort connecting */
461static int smc_connect_abort(struct smc_sock *smc, int reason_code,
462 int local_contact)
463{
464 if (local_contact == SMC_FIRST_CONTACT)
465 smc_lgr_forget(smc->conn.lgr);
466 mutex_unlock(&smc_create_lgr_pending);
467 smc_conn_free(&smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200468 return reason_code;
469}
470
471/* check if there is a rdma device available for this connection. */
472/* called for connect and listen */
473static int smc_check_rdma(struct smc_sock *smc, struct smc_ib_device **ibdev,
Ursula Braun7005ada2018-07-25 16:35:31 +0200474 u8 *ibport, unsigned short vlan_id, u8 gid[])
Hans Wippel3b2dec22018-05-18 09:34:18 +0200475{
476 int reason_code = 0;
477
478 /* PNET table look up: search active ib_device and port
479 * within same PNETID that also contains the ethernet device
480 * used for the internal TCP socket
481 */
Ursula Braun7005ada2018-07-25 16:35:31 +0200482 smc_pnet_find_roce_resource(smc->clcsock->sk, ibdev, ibport, vlan_id,
483 gid);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200484 if (!(*ibdev))
485 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
486
487 return reason_code;
488}
489
Hans Wippel41349842018-06-28 19:05:11 +0200490/* check if there is an ISM device available for this connection. */
491/* called for connect and listen */
492static int smc_check_ism(struct smc_sock *smc, struct smcd_dev **ismdev)
493{
494 /* Find ISM device with same PNETID as connecting interface */
495 smc_pnet_find_ism_resource(smc->clcsock->sk, ismdev);
496 if (!(*ismdev))
497 return SMC_CLC_DECL_CNFERR; /* configuration error */
498 return 0;
499}
500
501/* Check for VLAN ID and register it on ISM device just for CLC handshake */
502static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
503 struct smcd_dev *ismdev,
504 unsigned short vlan_id)
505{
506 if (vlan_id && smc_ism_get_vlan(ismdev, vlan_id))
507 return SMC_CLC_DECL_CNFERR;
508 return 0;
509}
510
511/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
512 * used, the VLAN ID will be registered again during the connection setup.
513 */
514static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
515 struct smcd_dev *ismdev,
516 unsigned short vlan_id)
517{
518 if (!is_smcd)
519 return 0;
520 if (vlan_id && smc_ism_put_vlan(ismdev, vlan_id))
521 return SMC_CLC_DECL_CNFERR;
522 return 0;
523}
524
Hans Wippel3b2dec22018-05-18 09:34:18 +0200525/* CLC handshake during connect */
Hans Wippelc758dfd2018-06-28 19:05:09 +0200526static int smc_connect_clc(struct smc_sock *smc, int smc_type,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200527 struct smc_clc_msg_accept_confirm *aclc,
Hans Wippel41349842018-06-28 19:05:11 +0200528 struct smc_ib_device *ibdev, u8 ibport,
Ursula Braun7005ada2018-07-25 16:35:31 +0200529 u8 gid[], struct smcd_dev *ismdev)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200530{
531 int rc = 0;
532
533 /* do inband token exchange */
Ursula Braun7005ada2018-07-25 16:35:31 +0200534 rc = smc_clc_send_proposal(smc, smc_type, ibdev, ibport, gid, ismdev);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200535 if (rc)
536 return rc;
537 /* receive SMC Accept CLC message */
538 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT);
539}
540
Ursula Brauna046d572017-01-09 16:55:16 +0100541/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200542static int smc_connect_rdma(struct smc_sock *smc,
543 struct smc_clc_msg_accept_confirm *aclc,
544 struct smc_ib_device *ibdev, u8 ibport)
545{
546 int local_contact = SMC_FIRST_CONTACT;
547 struct smc_link *link;
548 int reason_code = 0;
549
550 mutex_lock(&smc_create_lgr_pending);
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200551 local_contact = smc_conn_create(smc, false, aclc->hdr.flag, ibdev,
Karsten Graulee05ff72018-11-20 16:46:40 +0100552 ibport, ntoh24(aclc->qpn), &aclc->lcl,
553 NULL, 0);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200554 if (local_contact < 0) {
555 if (local_contact == -ENOMEM)
556 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
557 else if (local_contact == -ENOLINK)
558 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
559 else
560 reason_code = SMC_CLC_DECL_INTERR; /* other error */
561 return smc_connect_abort(smc, reason_code, 0);
562 }
563 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
564
565 smc_conn_save_peer_info(smc, aclc);
566
567 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200568 if (smc_buf_create(smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +0200569 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, local_contact);
570
571 if (local_contact == SMC_FIRST_CONTACT)
572 smc_link_save_peer_info(link, aclc);
573
574 if (smc_rmb_rtoken_handling(&smc->conn, aclc))
Karsten Graul603cc142018-07-25 16:35:32 +0200575 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200576 local_contact);
577
578 smc_close_init(smc);
579 smc_rx_init(smc);
580
581 if (local_contact == SMC_FIRST_CONTACT) {
582 if (smc_ib_ready_link(link))
Karsten Graul603cc142018-07-25 16:35:32 +0200583 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200584 local_contact);
585 } else {
586 if (!smc->conn.rmb_desc->reused &&
587 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);
Karsten Graulee05ff72018-11-20 16:46:40 +0100624 local_contact = smc_conn_create(smc, true, aclc->hdr.flag, NULL, 0, 0,
Hans Wippel41349842018-06-28 19:05:11 +0200625 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),
971 SMC_CLC_DECLINE);
Karsten Graul75d320d2018-03-01 13:51:31 +0100972 return 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),
992 SMC_CLC_DECLINE);
993 return rc;
994 }
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 */
Karsten Graulee05ff72018-11-20 16:46:40 +01001089 *local_contact = smc_conn_create(new_smc, false, 0, ibdev, ibport, 0,
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001090 &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);
Karsten Graulee05ff72018-11-20 16:46:40 +01001113 *local_contact = smc_conn_create(new_smc, true, 0, NULL, 0, 0, NULL,
Hans Wippel41349842018-06-28 19:05:11 +02001114 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) {
1148 if (!new_smc->conn.rmb_desc->reused) {
1149 if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true))
Karsten Graul603cc142018-07-25 16:35:32 +02001150 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001151 }
1152 }
1153 smc_rmb_sync_sg_for_device(&new_smc->conn);
1154
1155 return 0;
1156}
1157
1158/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001159static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1160 struct smc_clc_msg_accept_confirm *cclc,
1161 int local_contact)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001162{
1163 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1164 int reason_code = 0;
1165
1166 if (local_contact == SMC_FIRST_CONTACT)
1167 smc_link_save_peer_info(link, cclc);
1168
1169 if (smc_rmb_rtoken_handling(&new_smc->conn, cclc)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001170 reason_code = SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001171 goto decline;
1172 }
1173
1174 if (local_contact == SMC_FIRST_CONTACT) {
1175 if (smc_ib_ready_link(link)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001176 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001177 goto decline;
1178 }
1179 /* QP confirmation over RoCE fabric */
1180 reason_code = smc_serv_conf_first_link(new_smc);
1181 if (reason_code)
1182 goto decline;
1183 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001184 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001185
1186decline:
1187 mutex_unlock(&smc_create_lgr_pending);
1188 smc_listen_decline(new_smc, reason_code, local_contact);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001189 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001190}
1191
1192/* setup for RDMA connection of server */
1193static void smc_listen_work(struct work_struct *work)
1194{
1195 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1196 smc_listen_work);
1197 struct socket *newclcsock = new_smc->clcsock;
1198 struct smc_clc_msg_accept_confirm cclc;
1199 struct smc_clc_msg_proposal *pclc;
1200 struct smc_ib_device *ibdev;
Hans Wippel41349842018-06-28 19:05:11 +02001201 bool ism_supported = false;
1202 struct smcd_dev *ismdev;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001203 u8 buf[SMC_CLC_MAX_LEN];
1204 int local_contact = 0;
Ursula Braun7005ada2018-07-25 16:35:31 +02001205 unsigned short vlan;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001206 int reason_code = 0;
1207 int rc = 0;
1208 u8 ibport;
1209
1210 if (new_smc->use_fallback) {
1211 smc_listen_out_connected(new_smc);
1212 return;
1213 }
1214
1215 /* check if peer is smc capable */
1216 if (!tcp_sk(newclcsock->sk)->syn_smc) {
1217 new_smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001218 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001219 smc_listen_out_connected(new_smc);
1220 return;
1221 }
1222
1223 /* do inband token exchange -
1224 * wait for and receive SMC Proposal CLC message
1225 */
1226 pclc = (struct smc_clc_msg_proposal *)&buf;
1227 reason_code = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
1228 SMC_CLC_PROPOSAL);
1229 if (reason_code) {
1230 smc_listen_decline(new_smc, reason_code, 0);
1231 return;
1232 }
1233
1234 /* IPSec connections opt out of SMC-R optimizations */
1235 if (using_ipsec(new_smc)) {
1236 smc_listen_decline(new_smc, SMC_CLC_DECL_IPSEC, 0);
1237 return;
1238 }
1239
1240 mutex_lock(&smc_create_lgr_pending);
1241 smc_close_init(new_smc);
1242 smc_rx_init(new_smc);
1243 smc_tx_init(new_smc);
1244
Hans Wippel41349842018-06-28 19:05:11 +02001245 /* check if ISM is available */
1246 if ((pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) &&
1247 !smc_check_ism(new_smc, &ismdev) &&
1248 !smc_listen_ism_init(new_smc, pclc, ismdev, &local_contact)) {
1249 ism_supported = true;
1250 }
1251
Hans Wippel3b2dec22018-05-18 09:34:18 +02001252 /* check if RDMA is available */
Hans Wippel41349842018-06-28 19:05:11 +02001253 if (!ism_supported &&
1254 ((pclc->hdr.path != SMC_TYPE_R && pclc->hdr.path != SMC_TYPE_B) ||
Ursula Braun7005ada2018-07-25 16:35:31 +02001255 smc_vlan_by_tcpsk(new_smc->clcsock, &vlan) ||
1256 smc_check_rdma(new_smc, &ibdev, &ibport, vlan, NULL) ||
Hans Wippel41349842018-06-28 19:05:11 +02001257 smc_listen_rdma_check(new_smc, pclc) ||
1258 smc_listen_rdma_init(new_smc, pclc, ibdev, ibport,
1259 &local_contact) ||
1260 smc_listen_rdma_reg(new_smc, local_contact))) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001261 /* SMC not supported, decline */
1262 mutex_unlock(&smc_create_lgr_pending);
Karsten Graul603cc142018-07-25 16:35:32 +02001263 smc_listen_decline(new_smc, SMC_CLC_DECL_MODEUNSUPP,
1264 local_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001265 return;
1266 }
1267
1268 /* send SMC Accept CLC message */
1269 rc = smc_clc_send_accept(new_smc, local_contact);
1270 if (rc) {
1271 mutex_unlock(&smc_create_lgr_pending);
1272 smc_listen_decline(new_smc, rc, local_contact);
1273 return;
1274 }
1275
1276 /* receive SMC Confirm CLC message */
1277 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
1278 SMC_CLC_CONFIRM);
1279 if (reason_code) {
1280 mutex_unlock(&smc_create_lgr_pending);
1281 smc_listen_decline(new_smc, reason_code, local_contact);
1282 return;
1283 }
1284
1285 /* finish worker */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001286 if (!ism_supported) {
1287 if (smc_listen_rdma_finish(new_smc, &cclc, local_contact))
1288 return;
1289 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001290 smc_conn_save_peer_info(new_smc, &cclc);
1291 mutex_unlock(&smc_create_lgr_pending);
1292 smc_listen_out_connected(new_smc);
Ursula Brauna046d572017-01-09 16:55:16 +01001293}
1294
1295static void smc_tcp_listen_work(struct work_struct *work)
1296{
1297 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1298 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001299 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001300 struct smc_sock *new_smc;
1301 int rc = 0;
1302
Ursula Braun3163c502018-01-24 10:28:12 +01001303 lock_sock(lsk);
1304 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001305 rc = smc_clcsock_accept(lsmc, &new_smc);
1306 if (rc)
1307 goto out;
1308 if (!new_smc)
1309 continue;
1310
1311 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001312 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001313 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001314 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001315 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1316 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001317 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1318 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001319 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1320 if (!schedule_work(&new_smc->smc_listen_work))
1321 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001322 }
1323
1324out:
Ursula Braun3163c502018-01-24 10:28:12 +01001325 release_sock(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001326 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +01001327}
1328
Ursula Braunac713872017-01-09 16:55:13 +01001329static int smc_listen(struct socket *sock, int backlog)
1330{
1331 struct sock *sk = sock->sk;
1332 struct smc_sock *smc;
1333 int rc;
1334
1335 smc = smc_sk(sk);
1336 lock_sock(sk);
1337
1338 rc = -EINVAL;
1339 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
1340 goto out;
1341
1342 rc = 0;
1343 if (sk->sk_state == SMC_LISTEN) {
1344 sk->sk_max_ack_backlog = backlog;
1345 goto out;
1346 }
1347 /* some socket options are handled in core, so we could not apply
1348 * them to the clc socket -- copy smc socket options to clc socket
1349 */
1350 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001351 if (!smc->use_fallback)
1352 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001353
1354 rc = kernel_listen(smc->clcsock, backlog);
1355 if (rc)
1356 goto out;
1357 sk->sk_max_ack_backlog = backlog;
1358 sk->sk_ack_backlog = 0;
1359 sk->sk_state = SMC_LISTEN;
Ursula Brauna046d572017-01-09 16:55:16 +01001360 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun51f1de72018-01-26 09:28:48 +01001361 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1362 if (!schedule_work(&smc->tcp_listen_work))
1363 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001364
1365out:
1366 release_sock(sk);
1367 return rc;
1368}
1369
1370static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001371 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001372{
Ursula Brauna046d572017-01-09 16:55:16 +01001373 struct sock *sk = sock->sk, *nsk;
1374 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001375 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001376 long timeo;
1377 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001378
1379 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001380 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001381 lock_sock(sk);
1382
1383 if (lsmc->sk.sk_state != SMC_LISTEN) {
1384 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001385 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001386 goto out;
1387 }
1388
Ursula Brauna046d572017-01-09 16:55:16 +01001389 /* Wait for an incoming connection */
1390 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1391 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1392 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1393 set_current_state(TASK_INTERRUPTIBLE);
1394 if (!timeo) {
1395 rc = -EAGAIN;
1396 break;
1397 }
1398 release_sock(sk);
1399 timeo = schedule_timeout(timeo);
1400 /* wakeup by sk_data_ready in smc_listen_work() */
1401 sched_annotate_sleep();
1402 lock_sock(sk);
1403 if (signal_pending(current)) {
1404 rc = sock_intr_errno(timeo);
1405 break;
1406 }
1407 }
1408 set_current_state(TASK_RUNNING);
1409 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001410
Ursula Brauna046d572017-01-09 16:55:16 +01001411 if (!rc)
1412 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001413 release_sock(sk);
1414 if (rc)
1415 goto out;
1416
1417 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1418 /* wait till data arrives on the socket */
1419 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1420 MSEC_PER_SEC);
1421 if (smc_sk(nsk)->use_fallback) {
1422 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1423
1424 lock_sock(clcsk);
1425 if (skb_queue_empty(&clcsk->sk_receive_queue))
1426 sk_wait_data(clcsk, &timeo, NULL);
1427 release_sock(clcsk);
1428 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1429 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001430 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001431 release_sock(nsk);
1432 }
1433 }
Ursula Braunac713872017-01-09 16:55:13 +01001434
1435out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001436 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001437 return rc;
1438}
1439
1440static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001441 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001442{
1443 struct smc_sock *smc;
1444
Ursula Braunb38d7322017-01-09 16:55:25 +01001445 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1446 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001447 return -ENOTCONN;
1448
1449 smc = smc_sk(sock->sk);
1450
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001451 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001452}
1453
1454static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1455{
1456 struct sock *sk = sock->sk;
1457 struct smc_sock *smc;
1458 int rc = -EPIPE;
1459
1460 smc = smc_sk(sk);
1461 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001462 if ((sk->sk_state != SMC_ACTIVE) &&
1463 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1464 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001465 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001466
1467 if (msg->msg_flags & MSG_FASTOPEN) {
1468 if (sk->sk_state == SMC_INIT) {
1469 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001470 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001471 } else {
1472 rc = -EINVAL;
1473 goto out;
1474 }
1475 }
1476
Ursula Braunac713872017-01-09 16:55:13 +01001477 if (smc->use_fallback)
1478 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1479 else
Ursula Braune6727f32017-01-09 16:55:23 +01001480 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001481out:
1482 release_sock(sk);
1483 return rc;
1484}
1485
1486static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1487 int flags)
1488{
1489 struct sock *sk = sock->sk;
1490 struct smc_sock *smc;
1491 int rc = -ENOTCONN;
1492
1493 smc = smc_sk(sk);
1494 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001495 if ((sk->sk_state == SMC_INIT) ||
1496 (sk->sk_state == SMC_LISTEN) ||
1497 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001498 goto out;
1499
Ursula Braunb38d7322017-01-09 16:55:25 +01001500 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1501 rc = 0;
1502 goto out;
1503 }
1504
Stefan Raspl9014db22018-05-03 18:12:39 +02001505 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001506 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001507 } else {
1508 msg->msg_namelen = 0;
1509 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1510 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001511
Ursula Braunac713872017-01-09 16:55:13 +01001512out:
1513 release_sock(sk);
1514 return rc;
1515}
1516
Al Viroade994f2017-07-03 00:01:49 -04001517static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001518{
Ursula Braun8dce2782018-01-26 09:28:47 +01001519 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001520 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001521
Ursula Braun8dce2782018-01-26 09:28:47 +01001522 spin_lock(&isk->accept_q_lock);
1523 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001524 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001525 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001526
Ursula Braun8dce2782018-01-26 09:28:47 +01001527 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001528}
1529
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001530static __poll_t smc_poll(struct file *file, struct socket *sock,
1531 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01001532{
1533 struct sock *sk = sock->sk;
Al Viroe6c8adc2017-07-03 22:25:56 -04001534 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001535 struct smc_sock *smc;
1536
Ursula Braun8dce2782018-01-26 09:28:47 +01001537 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001538 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001539
Ursula Braunac713872017-01-09 16:55:13 +01001540 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02001541 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01001542 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001543 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02001544 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001545 if (sk->sk_err)
Ursula Braun784813a2018-05-02 16:53:56 +02001546 mask |= EPOLLERR;
Ursula Braunac713872017-01-09 16:55:13 +01001547 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07001548 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02001549 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01001550 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001551 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001552 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1553 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001554 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001555 if (sk->sk_state == SMC_LISTEN) {
1556 /* woken up by sk_data_ready in smc_listen_work() */
1557 mask = smc_accept_poll(sk);
1558 } else {
1559 if (atomic_read(&smc->conn.sndbuf_space) ||
1560 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001561 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001562 } else {
1563 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1564 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1565 }
1566 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001567 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001568 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001569 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001570 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001571 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02001572 if (smc->conn.urg_state == SMC_URG_VALID)
1573 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01001574 }
Ursula Braunac713872017-01-09 16:55:13 +01001575 }
1576
1577 return mask;
1578}
1579
1580static int smc_shutdown(struct socket *sock, int how)
1581{
1582 struct sock *sk = sock->sk;
1583 struct smc_sock *smc;
1584 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001585 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001586
1587 smc = smc_sk(sk);
1588
1589 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001590 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001591
1592 lock_sock(sk);
1593
1594 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02001595 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01001596 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1597 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1598 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1599 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1600 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001601 goto out;
1602 if (smc->use_fallback) {
1603 rc = kernel_sock_shutdown(smc->clcsock, how);
1604 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1605 if (sk->sk_shutdown == SHUTDOWN_MASK)
1606 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001607 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001608 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001609 switch (how) {
1610 case SHUT_RDWR: /* shutdown in both directions */
1611 rc = smc_close_active(smc);
1612 break;
1613 case SHUT_WR:
1614 rc = smc_close_shutdown_write(smc);
1615 break;
1616 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001617 rc = 0;
1618 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001619 break;
1620 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001621 if (smc->clcsock)
1622 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001623 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1624 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001625
1626out:
1627 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001628 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001629}
1630
1631static int smc_setsockopt(struct socket *sock, int level, int optname,
1632 char __user *optval, unsigned int optlen)
1633{
1634 struct sock *sk = sock->sk;
1635 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001636 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001637
1638 smc = smc_sk(sk);
1639
1640 /* generic setsockopts reaching us here always apply to the
1641 * CLC socket
1642 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001643 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1644 optval, optlen);
1645 if (smc->clcsock->sk->sk_err) {
1646 sk->sk_err = smc->clcsock->sk->sk_err;
1647 sk->sk_error_report(sk);
1648 }
1649 if (rc)
1650 return rc;
1651
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001652 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00001653 return -EINVAL;
Ursula Braunac0107e2018-07-18 15:22:50 +02001654 if (get_user(val, (int __user *)optval))
1655 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001656
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001657 lock_sock(sk);
1658 switch (optname) {
1659 case TCP_ULP:
1660 case TCP_FASTOPEN:
1661 case TCP_FASTOPEN_CONNECT:
1662 case TCP_FASTOPEN_KEY:
1663 case TCP_FASTOPEN_NO_COOKIE:
1664 /* option not supported by SMC */
1665 if (sk->sk_state == SMC_INIT) {
1666 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001667 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001668 } else {
1669 if (!smc->use_fallback)
1670 rc = -EINVAL;
1671 }
1672 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001673 case TCP_NODELAY:
1674 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001675 if (val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001676 mod_delayed_work(system_wq, &smc->conn.tx_work,
1677 0);
1678 }
1679 break;
1680 case TCP_CORK:
1681 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001682 if (!val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001683 mod_delayed_work(system_wq, &smc->conn.tx_work,
1684 0);
1685 }
1686 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001687 case TCP_DEFER_ACCEPT:
1688 smc->sockopt_defer_accept = val;
1689 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001690 default:
1691 break;
1692 }
1693 release_sock(sk);
1694
1695 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001696}
1697
1698static int smc_getsockopt(struct socket *sock, int level, int optname,
1699 char __user *optval, int __user *optlen)
1700{
1701 struct smc_sock *smc;
1702
1703 smc = smc_sk(sock->sk);
1704 /* socket options apply to the CLC socket */
1705 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1706 optval, optlen);
1707}
1708
1709static int smc_ioctl(struct socket *sock, unsigned int cmd,
1710 unsigned long arg)
1711{
Stefan Rasplde8474e2018-05-23 16:38:11 +02001712 union smc_host_cursor cons, urg;
1713 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01001714 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001715 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001716
1717 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001718 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02001719 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02001720 if (smc->use_fallback) {
1721 if (!smc->clcsock) {
1722 release_sock(&smc->sk);
1723 return -EBADF;
1724 }
1725 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1726 release_sock(&smc->sk);
1727 return answ;
1728 }
Ursula Braun9b67e262018-05-02 16:56:46 +02001729 switch (cmd) {
1730 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02001731 if (smc->sk.sk_state == SMC_LISTEN) {
1732 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001733 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001734 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001735 if (smc->sk.sk_state == SMC_INIT ||
1736 smc->sk.sk_state == SMC_CLOSED)
1737 answ = 0;
1738 else
1739 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02001740 break;
1741 case SIOCOUTQ:
1742 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02001743 if (smc->sk.sk_state == SMC_LISTEN) {
1744 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001745 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001746 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001747 if (smc->sk.sk_state == SMC_INIT ||
1748 smc->sk.sk_state == SMC_CLOSED)
1749 answ = 0;
1750 else
1751 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02001752 atomic_read(&smc->conn.sndbuf_space);
1753 break;
1754 case SIOCOUTQNSD:
1755 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02001756 if (smc->sk.sk_state == SMC_LISTEN) {
1757 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001758 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001759 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001760 if (smc->sk.sk_state == SMC_INIT ||
1761 smc->sk.sk_state == SMC_CLOSED)
1762 answ = 0;
1763 else
1764 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02001765 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02001766 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02001767 if (smc->sk.sk_state == SMC_LISTEN) {
1768 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001769 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001770 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001771 if (smc->sk.sk_state == SMC_INIT ||
1772 smc->sk.sk_state == SMC_CLOSED) {
1773 answ = 0;
1774 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02001775 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1776 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001777 answ = smc_curs_diff(conn->rmb_desc->len,
1778 &cons, &urg) == 1;
1779 }
1780 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02001781 default:
Ursula Braun1992d992018-07-16 13:56:52 +02001782 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001783 return -ENOIOCTLCMD;
1784 }
Ursula Braun1992d992018-07-16 13:56:52 +02001785 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001786
1787 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001788}
1789
1790static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1791 int offset, size_t size, int flags)
1792{
1793 struct sock *sk = sock->sk;
1794 struct smc_sock *smc;
1795 int rc = -EPIPE;
1796
1797 smc = smc_sk(sk);
1798 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001799 if (sk->sk_state != SMC_ACTIVE) {
1800 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001801 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001802 }
1803 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001804 if (smc->use_fallback)
1805 rc = kernel_sendpage(smc->clcsock, page, offset,
1806 size, flags);
1807 else
1808 rc = sock_no_sendpage(sock, page, offset, size, flags);
1809
1810out:
Ursula Braunac713872017-01-09 16:55:13 +01001811 return rc;
1812}
1813
Stefan Raspl9014db22018-05-03 18:12:39 +02001814/* Map the affected portions of the rmbe into an spd, note the number of bytes
1815 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1816 * updates till whenever a respective page has been fully processed.
1817 * Note that subsequent recv() calls have to wait till all splice() processing
1818 * completed.
1819 */
Ursula Braunac713872017-01-09 16:55:13 +01001820static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1821 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001822 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001823{
1824 struct sock *sk = sock->sk;
1825 struct smc_sock *smc;
1826 int rc = -ENOTCONN;
1827
1828 smc = smc_sk(sk);
1829 lock_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001830
1831 if (sk->sk_state == SMC_INIT ||
1832 sk->sk_state == SMC_LISTEN ||
1833 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001834 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001835
1836 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1837 rc = 0;
1838 goto out;
1839 }
1840
Ursula Braunac713872017-01-09 16:55:13 +01001841 if (smc->use_fallback) {
1842 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1843 pipe, len, flags);
1844 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001845 if (*ppos) {
1846 rc = -ESPIPE;
1847 goto out;
1848 }
1849 if (flags & SPLICE_F_NONBLOCK)
1850 flags = MSG_DONTWAIT;
1851 else
1852 flags = 0;
1853 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001854 }
1855out:
1856 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001857
Ursula Braunac713872017-01-09 16:55:13 +01001858 return rc;
1859}
1860
1861/* must look like tcp */
1862static const struct proto_ops smc_sock_ops = {
1863 .family = PF_SMC,
1864 .owner = THIS_MODULE,
1865 .release = smc_release,
1866 .bind = smc_bind,
1867 .connect = smc_connect,
1868 .socketpair = sock_no_socketpair,
1869 .accept = smc_accept,
1870 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001871 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01001872 .ioctl = smc_ioctl,
1873 .listen = smc_listen,
1874 .shutdown = smc_shutdown,
1875 .setsockopt = smc_setsockopt,
1876 .getsockopt = smc_getsockopt,
1877 .sendmsg = smc_sendmsg,
1878 .recvmsg = smc_recvmsg,
1879 .mmap = sock_no_mmap,
1880 .sendpage = smc_sendpage,
1881 .splice_read = smc_splice_read,
1882};
1883
1884static int smc_create(struct net *net, struct socket *sock, int protocol,
1885 int kern)
1886{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001887 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001888 struct smc_sock *smc;
1889 struct sock *sk;
1890 int rc;
1891
1892 rc = -ESOCKTNOSUPPORT;
1893 if (sock->type != SOCK_STREAM)
1894 goto out;
1895
1896 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001897 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01001898 goto out;
1899
1900 rc = -ENOBUFS;
1901 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001902 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001903 if (!sk)
1904 goto out;
1905
1906 /* create internal TCP socket for CLC handshake and fallback */
1907 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001908 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02001909 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001910 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1911 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01001912 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01001913 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01001914 goto out;
1915 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01001916 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1917 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01001918
1919out:
1920 return rc;
1921}
1922
1923static const struct net_proto_family smc_sock_family_ops = {
1924 .family = PF_SMC,
1925 .owner = THIS_MODULE,
1926 .create = smc_create,
1927};
1928
1929static int __init smc_init(void)
1930{
1931 int rc;
1932
Thomas Richter6812baa2017-01-09 16:55:15 +01001933 rc = smc_pnet_init();
1934 if (rc)
1935 return rc;
1936
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001937 rc = smc_llc_init();
1938 if (rc) {
1939 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1940 goto out_pnet;
1941 }
1942
Ursula Braun5f083182017-01-09 16:55:22 +01001943 rc = smc_cdc_init();
1944 if (rc) {
1945 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1946 goto out_pnet;
1947 }
1948
Ursula Braunac713872017-01-09 16:55:13 +01001949 rc = proto_register(&smc_proto, 1);
1950 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01001951 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Thomas Richter6812baa2017-01-09 16:55:15 +01001952 goto out_pnet;
Ursula Braunac713872017-01-09 16:55:13 +01001953 }
1954
Karsten Graulaaa4d332018-03-16 15:06:41 +01001955 rc = proto_register(&smc_proto6, 1);
1956 if (rc) {
1957 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
1958 goto out_proto;
1959 }
1960
Ursula Braunac713872017-01-09 16:55:13 +01001961 rc = sock_register(&smc_sock_family_ops);
1962 if (rc) {
1963 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001964 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01001965 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01001966 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001967 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01001968
Ursula Brauna4cf0442017-01-09 16:55:14 +01001969 rc = smc_ib_register_client();
1970 if (rc) {
1971 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1972 goto out_sock;
1973 }
1974
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001975 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01001976 return 0;
1977
Ursula Brauna4cf0442017-01-09 16:55:14 +01001978out_sock:
1979 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001980out_proto6:
1981 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01001982out_proto:
1983 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001984out_pnet:
1985 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001986 return rc;
1987}
1988
1989static void __exit smc_exit(void)
1990{
Hans Wippel9fda3512018-05-18 09:34:11 +02001991 smc_core_exit();
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001992 static_branch_disable(&tcp_have_smc);
Ursula Brauna4cf0442017-01-09 16:55:14 +01001993 smc_ib_unregister_client();
Ursula Braunac713872017-01-09 16:55:13 +01001994 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001995 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01001996 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001997 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01001998}
1999
2000module_init(smc_init);
2001module_exit(smc_exit);
2002
2003MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2004MODULE_DESCRIPTION("smc socket address family");
2005MODULE_LICENSE("GPL");
2006MODULE_ALIAS_NETPROTO(PF_SMC);