blob: 60ccc8f50368f61aa48e4078ef2eceee90257cb5 [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;
Ursula Braunb03faa12019-02-07 15:56:15 +0100148 } else {
149 if (sk->sk_state != SMC_LISTEN && sk->sk_state != SMC_INIT)
150 sock_put(sk); /* passive closing */
151 if (sk->sk_state == SMC_LISTEN) {
152 /* wake up clcsock accept */
153 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
154 }
155 sk->sk_state = SMC_CLOSED;
156 sk->sk_state_change(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100157 }
Cong Wang26d92e92019-01-05 23:45:26 -0800158
159 sk->sk_prot->unhash(sk);
160
Ursula Braunb03faa12019-02-07 15:56:15 +0100161 if (sk->sk_state == SMC_CLOSED) {
162 if (smc->clcsock) {
163 mutex_lock(&smc->clcsock_release_lock);
164 sock_release(smc->clcsock);
165 smc->clcsock = NULL;
166 mutex_unlock(&smc->clcsock_release_lock);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800167 }
Ursula Braunb03faa12019-02-07 15:56:15 +0100168 if (!smc->use_fallback)
169 smc_conn_free(&smc->conn);
Ursula Braun51f1de72018-01-26 09:28:48 +0100170 }
Ursula Braunac713872017-01-09 16:55:13 +0100171
172 /* detach socket */
173 sock_orphan(sk);
174 sock->sk = NULL;
175 release_sock(sk);
176
Ursula Braun51f1de72018-01-26 09:28:48 +0100177 sock_put(sk); /* final sock_put */
Ursula Braunac713872017-01-09 16:55:13 +0100178out:
Ursula Braunb38d7322017-01-09 16:55:25 +0100179 return rc;
Ursula Braunac713872017-01-09 16:55:13 +0100180}
181
182static void smc_destruct(struct sock *sk)
183{
184 if (sk->sk_state != SMC_CLOSED)
185 return;
186 if (!sock_flag(sk, SOCK_DEAD))
187 return;
188
189 sk_refcnt_debug_dec(sk);
190}
191
Karsten Graulaaa4d332018-03-16 15:06:41 +0100192static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
193 int protocol)
Ursula Braunac713872017-01-09 16:55:13 +0100194{
195 struct smc_sock *smc;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100196 struct proto *prot;
Ursula Braunac713872017-01-09 16:55:13 +0100197 struct sock *sk;
198
Karsten Graulaaa4d332018-03-16 15:06:41 +0100199 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
200 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
Ursula Braunac713872017-01-09 16:55:13 +0100201 if (!sk)
202 return NULL;
203
204 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
205 sk->sk_state = SMC_INIT;
206 sk->sk_destruct = smc_destruct;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100207 sk->sk_protocol = protocol;
Ursula Braunac713872017-01-09 16:55:13 +0100208 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100209 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200210 INIT_WORK(&smc->connect_work, smc_connect_work);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700211 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
Ursula Brauna046d572017-01-09 16:55:16 +0100212 INIT_LIST_HEAD(&smc->accept_q);
213 spin_lock_init(&smc->accept_q_lock);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700214 spin_lock_init(&smc->conn.send_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100215 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100216 sk_refcnt_debug_inc(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800217 mutex_init(&smc->clcsock_release_lock);
Ursula Braunac713872017-01-09 16:55:13 +0100218
219 return sk;
220}
221
222static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
223 int addr_len)
224{
225 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
226 struct sock *sk = sock->sk;
227 struct smc_sock *smc;
228 int rc;
229
230 smc = smc_sk(sk);
231
232 /* replicate tests from inet_bind(), to be safe wrt. future changes */
233 rc = -EINVAL;
234 if (addr_len < sizeof(struct sockaddr_in))
235 goto out;
236
237 rc = -EAFNOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100238 if (addr->sin_family != AF_INET &&
239 addr->sin_family != AF_INET6 &&
240 addr->sin_family != AF_UNSPEC)
241 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100242 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
Karsten Graulaaa4d332018-03-16 15:06:41 +0100243 if (addr->sin_family == AF_UNSPEC &&
244 addr->sin_addr.s_addr != htonl(INADDR_ANY))
Ursula Braunac713872017-01-09 16:55:13 +0100245 goto out;
246
247 lock_sock(sk);
248
249 /* Check if socket is already active */
250 rc = -EINVAL;
251 if (sk->sk_state != SMC_INIT)
252 goto out_rel;
253
254 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
255 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
256
257out_rel:
258 release_sock(sk);
259out:
260 return rc;
261}
262
263static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
264 unsigned long mask)
265{
266 /* options we don't get control via setsockopt for */
267 nsk->sk_type = osk->sk_type;
268 nsk->sk_sndbuf = osk->sk_sndbuf;
269 nsk->sk_rcvbuf = osk->sk_rcvbuf;
270 nsk->sk_sndtimeo = osk->sk_sndtimeo;
271 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
272 nsk->sk_mark = osk->sk_mark;
273 nsk->sk_priority = osk->sk_priority;
274 nsk->sk_rcvlowat = osk->sk_rcvlowat;
275 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
276 nsk->sk_err = osk->sk_err;
277
278 nsk->sk_flags &= ~mask;
279 nsk->sk_flags |= osk->sk_flags & mask;
280}
281
282#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
283 (1UL << SOCK_KEEPOPEN) | \
284 (1UL << SOCK_LINGER) | \
285 (1UL << SOCK_BROADCAST) | \
286 (1UL << SOCK_TIMESTAMP) | \
287 (1UL << SOCK_DBG) | \
288 (1UL << SOCK_RCVTSTAMP) | \
289 (1UL << SOCK_RCVTSTAMPNS) | \
290 (1UL << SOCK_LOCALROUTE) | \
291 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
292 (1UL << SOCK_RXQ_OVFL) | \
293 (1UL << SOCK_WIFI_STATUS) | \
294 (1UL << SOCK_NOFCS) | \
Deepa Dinamani97184752019-02-02 07:34:51 -0800295 (1UL << SOCK_FILTER_LOCKED) | \
296 (1UL << SOCK_TSTAMP_NEW))
Ursula Braunac713872017-01-09 16:55:13 +0100297/* copy only relevant settings and flags of SOL_SOCKET level from smc to
298 * clc socket (since smc is not called for these options from net/core)
299 */
300static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
301{
302 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
303}
304
305#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
306 (1UL << SOCK_KEEPOPEN) | \
307 (1UL << SOCK_LINGER) | \
308 (1UL << SOCK_DBG))
309/* copy only settings and flags relevant for smc from clc to smc socket */
310static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
311{
312 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
313}
314
Karsten Graulc7674c02018-11-22 10:26:43 +0100315/* register a new rmb, send confirm_rkey msg to register with peer */
Karsten Graul44aa81c2018-05-15 17:04:55 +0200316static int smc_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc,
317 bool conf_rkey)
Karsten Graule63a5f82018-05-03 17:57:37 +0200318{
Karsten Graulc7674c02018-11-22 10:26:43 +0100319 if (!rmb_desc->wr_reg) {
320 /* register memory region for new rmb */
321 if (smc_wr_reg_send(link, rmb_desc->mr_rx[SMC_SINGLE_LINK])) {
322 rmb_desc->regerr = 1;
323 return -EFAULT;
324 }
325 rmb_desc->wr_reg = 1;
Karsten Graula6920d12018-05-03 17:57:38 +0200326 }
Karsten Graul44aa81c2018-05-15 17:04:55 +0200327 if (!conf_rkey)
328 return 0;
329 /* exchange confirm_rkey msg with peer */
330 if (smc_llc_do_confirm_rkey(link, rmb_desc)) {
331 rmb_desc->regerr = 1;
332 return -EFAULT;
333 }
Karsten Graule63a5f82018-05-03 17:57:37 +0200334 return 0;
335}
336
Stefan Raspl0f627122018-03-01 13:51:26 +0100337static int smc_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100338{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200339 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100340 struct smc_link_group *lgr = smc->conn.lgr;
341 struct smc_link *link;
342 int rest;
343 int rc;
344
345 link = &lgr->lnk[SMC_SINGLE_LINK];
346 /* receive CONFIRM LINK request from server over RoCE fabric */
347 rest = wait_for_completion_interruptible_timeout(
348 &link->llc_confirm,
349 SMC_LLC_WAIT_FIRST_TIME);
350 if (rest <= 0) {
351 struct smc_clc_msg_decline dclc;
352
353 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100354 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100355 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100356 }
357
Karsten Graul75d320d2018-03-01 13:51:31 +0100358 if (link->llc_confirm_rc)
359 return SMC_CLC_DECL_RMBE_EC;
360
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100361 rc = smc_ib_modify_qp_rts(link);
362 if (rc)
Karsten Graul603cc142018-07-25 16:35:32 +0200363 return SMC_CLC_DECL_ERR_RDYLNK;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100364
365 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200366
Karsten Graul44aa81c2018-05-15 17:04:55 +0200367 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200368 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200369
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100370 /* send CONFIRM LINK response over RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200371 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100372 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200373 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100374
Karsten Graul52bedf32018-03-01 13:51:32 +0100375 /* receive ADD LINK request from server over RoCE fabric */
376 rest = wait_for_completion_interruptible_timeout(&link->llc_add,
377 SMC_LLC_WAIT_TIME);
378 if (rest <= 0) {
379 struct smc_clc_msg_decline dclc;
380
381 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100382 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100383 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
Karsten Graul52bedf32018-03-01 13:51:32 +0100384 }
385
386 /* send add link reject message, only one link supported for now */
387 rc = smc_llc_send_add_link(link,
388 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +0200389 link->gid, SMC_LLC_RESP);
Karsten Graul52bedf32018-03-01 13:51:32 +0100390 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200391 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +0100392
Karsten Graul877ae5b2018-05-02 16:56:44 +0200393 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100394
Karsten Graul75d320d2018-03-01 13:51:31 +0100395 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100396}
397
Hans Wippel41349842018-06-28 19:05:11 +0200398static void smcr_conn_save_peer_info(struct smc_sock *smc,
399 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100400{
Hans Wippel95d8d2632018-05-18 09:34:13 +0200401 int bufsize = smc_uncompress_bufsize(clc->rmbe_size);
402
Hans Wippel92a138e2018-05-18 09:34:12 +0200403 smc->conn.peer_rmbe_idx = clc->rmbe_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100404 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200405 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100406 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200407 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100408}
409
Hans Wippel41349842018-06-28 19:05:11 +0200410static void smcd_conn_save_peer_info(struct smc_sock *smc,
411 struct smc_clc_msg_accept_confirm *clc)
412{
413 int bufsize = smc_uncompress_bufsize(clc->dmbe_size);
414
415 smc->conn.peer_rmbe_idx = clc->dmbe_idx;
416 smc->conn.peer_token = clc->token;
417 /* msg header takes up space in the buffer */
418 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
419 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
420 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
421}
422
423static void smc_conn_save_peer_info(struct smc_sock *smc,
424 struct smc_clc_msg_accept_confirm *clc)
425{
426 if (smc->conn.lgr->is_smcd)
427 smcd_conn_save_peer_info(smc, clc);
428 else
429 smcr_conn_save_peer_info(smc, clc);
430}
431
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100432static void smc_link_save_peer_info(struct smc_link *link,
433 struct smc_clc_msg_accept_confirm *clc)
434{
435 link->peer_qpn = ntoh24(clc->qpn);
436 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
437 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
438 link->peer_psn = ntoh24(clc->psn);
439 link->peer_mtu = clc->qp_mtu;
440}
441
Hans Wippel3b2dec22018-05-18 09:34:18 +0200442/* fall back during connect */
Karsten Graul603cc142018-07-25 16:35:32 +0200443static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200444{
445 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +0200446 smc->fallback_rsn = reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200447 smc_copy_sock_settings_to_clc(smc);
448 if (smc->sk.sk_state == SMC_INIT)
449 smc->sk.sk_state = SMC_ACTIVE;
450 return 0;
451}
452
453/* decline and fall back during connect */
454static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
455{
456 int rc;
457
Ursula Braune1bbdd52018-07-05 16:15:30 +0200458 if (reason_code < 0) { /* error, fallback is not possible */
459 if (smc->sk.sk_state == SMC_INIT)
460 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200461 return reason_code;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200462 }
Karsten Graul603cc142018-07-25 16:35:32 +0200463 if (reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200464 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200465 if (rc < 0) {
466 if (smc->sk.sk_state == SMC_INIT)
467 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200468 return rc;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200469 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200470 }
Karsten Graul603cc142018-07-25 16:35:32 +0200471 return smc_connect_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200472}
473
474/* abort connecting */
475static int smc_connect_abort(struct smc_sock *smc, int reason_code,
476 int local_contact)
477{
478 if (local_contact == SMC_FIRST_CONTACT)
479 smc_lgr_forget(smc->conn.lgr);
480 mutex_unlock(&smc_create_lgr_pending);
481 smc_conn_free(&smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200482 return reason_code;
483}
484
485/* check if there is a rdma device available for this connection. */
486/* called for connect and listen */
487static int smc_check_rdma(struct smc_sock *smc, struct smc_ib_device **ibdev,
Ursula Braun7005ada2018-07-25 16:35:31 +0200488 u8 *ibport, unsigned short vlan_id, u8 gid[])
Hans Wippel3b2dec22018-05-18 09:34:18 +0200489{
490 int reason_code = 0;
491
492 /* PNET table look up: search active ib_device and port
493 * within same PNETID that also contains the ethernet device
494 * used for the internal TCP socket
495 */
Ursula Braun7005ada2018-07-25 16:35:31 +0200496 smc_pnet_find_roce_resource(smc->clcsock->sk, ibdev, ibport, vlan_id,
497 gid);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200498 if (!(*ibdev))
499 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
500
501 return reason_code;
502}
503
Hans Wippel41349842018-06-28 19:05:11 +0200504/* check if there is an ISM device available for this connection. */
505/* called for connect and listen */
506static int smc_check_ism(struct smc_sock *smc, struct smcd_dev **ismdev)
507{
508 /* Find ISM device with same PNETID as connecting interface */
509 smc_pnet_find_ism_resource(smc->clcsock->sk, ismdev);
510 if (!(*ismdev))
511 return SMC_CLC_DECL_CNFERR; /* configuration error */
512 return 0;
513}
514
515/* Check for VLAN ID and register it on ISM device just for CLC handshake */
516static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
517 struct smcd_dev *ismdev,
518 unsigned short vlan_id)
519{
520 if (vlan_id && smc_ism_get_vlan(ismdev, vlan_id))
521 return SMC_CLC_DECL_CNFERR;
522 return 0;
523}
524
525/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
526 * used, the VLAN ID will be registered again during the connection setup.
527 */
528static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
529 struct smcd_dev *ismdev,
530 unsigned short vlan_id)
531{
532 if (!is_smcd)
533 return 0;
534 if (vlan_id && smc_ism_put_vlan(ismdev, vlan_id))
535 return SMC_CLC_DECL_CNFERR;
536 return 0;
537}
538
Hans Wippel3b2dec22018-05-18 09:34:18 +0200539/* CLC handshake during connect */
Hans Wippelc758dfd2018-06-28 19:05:09 +0200540static int smc_connect_clc(struct smc_sock *smc, int smc_type,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200541 struct smc_clc_msg_accept_confirm *aclc,
Hans Wippel41349842018-06-28 19:05:11 +0200542 struct smc_ib_device *ibdev, u8 ibport,
Ursula Braun7005ada2018-07-25 16:35:31 +0200543 u8 gid[], struct smcd_dev *ismdev)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200544{
545 int rc = 0;
546
547 /* do inband token exchange */
Ursula Braun7005ada2018-07-25 16:35:31 +0200548 rc = smc_clc_send_proposal(smc, smc_type, ibdev, ibport, gid, ismdev);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200549 if (rc)
550 return rc;
551 /* receive SMC Accept CLC message */
Ursula Braun2b59f582018-11-22 10:26:39 +0100552 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT,
553 CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200554}
555
Ursula Brauna046d572017-01-09 16:55:16 +0100556/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200557static int smc_connect_rdma(struct smc_sock *smc,
558 struct smc_clc_msg_accept_confirm *aclc,
559 struct smc_ib_device *ibdev, u8 ibport)
560{
561 int local_contact = SMC_FIRST_CONTACT;
562 struct smc_link *link;
563 int reason_code = 0;
564
565 mutex_lock(&smc_create_lgr_pending);
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200566 local_contact = smc_conn_create(smc, false, aclc->hdr.flag, ibdev,
Karsten Graulee05ff72018-11-20 16:46:40 +0100567 ibport, ntoh24(aclc->qpn), &aclc->lcl,
568 NULL, 0);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200569 if (local_contact < 0) {
570 if (local_contact == -ENOMEM)
571 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
572 else if (local_contact == -ENOLINK)
573 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
574 else
575 reason_code = SMC_CLC_DECL_INTERR; /* other error */
576 return smc_connect_abort(smc, reason_code, 0);
577 }
578 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
579
580 smc_conn_save_peer_info(smc, aclc);
581
582 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200583 if (smc_buf_create(smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +0200584 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, local_contact);
585
586 if (local_contact == SMC_FIRST_CONTACT)
587 smc_link_save_peer_info(link, aclc);
588
589 if (smc_rmb_rtoken_handling(&smc->conn, aclc))
Karsten Graul603cc142018-07-25 16:35:32 +0200590 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200591 local_contact);
592
593 smc_close_init(smc);
594 smc_rx_init(smc);
595
596 if (local_contact == SMC_FIRST_CONTACT) {
597 if (smc_ib_ready_link(link))
Karsten Graul603cc142018-07-25 16:35:32 +0200598 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200599 local_contact);
600 } else {
Karsten Graulc7674c02018-11-22 10:26:43 +0100601 if (smc_reg_rmb(link, smc->conn.rmb_desc, true))
Karsten Graul603cc142018-07-25 16:35:32 +0200602 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200603 local_contact);
604 }
605 smc_rmb_sync_sg_for_device(&smc->conn);
606
607 reason_code = smc_clc_send_confirm(smc);
608 if (reason_code)
609 return smc_connect_abort(smc, reason_code, local_contact);
610
611 smc_tx_init(smc);
612
613 if (local_contact == SMC_FIRST_CONTACT) {
614 /* QP confirmation over RoCE fabric */
615 reason_code = smc_clnt_conf_first_link(smc);
616 if (reason_code)
617 return smc_connect_abort(smc, reason_code,
618 local_contact);
619 }
620 mutex_unlock(&smc_create_lgr_pending);
621
622 smc_copy_sock_settings_to_clc(smc);
623 if (smc->sk.sk_state == SMC_INIT)
624 smc->sk.sk_state = SMC_ACTIVE;
625
626 return 0;
627}
628
Hans Wippel41349842018-06-28 19:05:11 +0200629/* setup for ISM connection of client */
630static int smc_connect_ism(struct smc_sock *smc,
631 struct smc_clc_msg_accept_confirm *aclc,
632 struct smcd_dev *ismdev)
633{
634 int local_contact = SMC_FIRST_CONTACT;
635 int rc = 0;
636
637 mutex_lock(&smc_create_lgr_pending);
Karsten Graulee05ff72018-11-20 16:46:40 +0100638 local_contact = smc_conn_create(smc, true, aclc->hdr.flag, NULL, 0, 0,
Hans Wippel41349842018-06-28 19:05:11 +0200639 NULL, ismdev, aclc->gid);
640 if (local_contact < 0)
641 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, 0);
642
643 /* Create send and receive buffers */
644 if (smc_buf_create(smc, true))
645 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, local_contact);
646
647 smc_conn_save_peer_info(smc, aclc);
648 smc_close_init(smc);
649 smc_rx_init(smc);
650 smc_tx_init(smc);
651
652 rc = smc_clc_send_confirm(smc);
653 if (rc)
654 return smc_connect_abort(smc, rc, local_contact);
655 mutex_unlock(&smc_create_lgr_pending);
656
657 smc_copy_sock_settings_to_clc(smc);
658 if (smc->sk.sk_state == SMC_INIT)
659 smc->sk.sk_state = SMC_ACTIVE;
660
661 return 0;
662}
663
Hans Wippel3b2dec22018-05-18 09:34:18 +0200664/* perform steps before actually connecting */
665static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100666{
Hans Wippel41349842018-06-28 19:05:11 +0200667 bool ism_supported = false, rdma_supported = false;
Ursula Brauna046d572017-01-09 16:55:16 +0100668 struct smc_clc_msg_accept_confirm aclc;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200669 struct smc_ib_device *ibdev;
Hans Wippel41349842018-06-28 19:05:11 +0200670 struct smcd_dev *ismdev;
Ursula Braun7005ada2018-07-25 16:35:31 +0200671 u8 gid[SMC_GID_SIZE];
Hans Wippel41349842018-06-28 19:05:11 +0200672 unsigned short vlan;
673 int smc_type;
Ursula Brauna046d572017-01-09 16:55:16 +0100674 int rc = 0;
675 u8 ibport;
676
Ursula Braun51f1de72018-01-26 09:28:48 +0100677 sock_hold(&smc->sk); /* sock put in passive closing */
678
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200679 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +0200680 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200681
Hans Wippel3b2dec22018-05-18 09:34:18 +0200682 /* if peer has not signalled SMC-capability, fall back */
683 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +0200684 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200685
Ursula Brauna046d572017-01-09 16:55:16 +0100686 /* IPSec connections opt out of SMC-R optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200687 if (using_ipsec(smc))
688 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
Ursula Brauna046d572017-01-09 16:55:16 +0100689
Hans Wippel41349842018-06-28 19:05:11 +0200690 /* check for VLAN ID */
691 if (smc_vlan_by_tcpsk(smc->clcsock, &vlan))
692 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_CNFERR);
693
694 /* check if there is an ism device available */
695 if (!smc_check_ism(smc, &ismdev) &&
696 !smc_connect_ism_vlan_setup(smc, ismdev, vlan)) {
697 /* ISM is supported for this connection */
698 ism_supported = true;
699 smc_type = SMC_TYPE_D;
700 }
701
702 /* check if there is a rdma device available */
Ursula Braun7005ada2018-07-25 16:35:31 +0200703 if (!smc_check_rdma(smc, &ibdev, &ibport, vlan, gid)) {
Hans Wippel41349842018-06-28 19:05:11 +0200704 /* RDMA is supported for this connection */
705 rdma_supported = true;
706 if (ism_supported)
707 smc_type = SMC_TYPE_B; /* both */
708 else
709 smc_type = SMC_TYPE_R; /* only RDMA */
710 }
711
712 /* if neither ISM nor RDMA are supported, fallback */
713 if (!rdma_supported && !ism_supported)
Karsten Graul603cc142018-07-25 16:35:32 +0200714 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV);
Ursula Brauna046d572017-01-09 16:55:16 +0100715
Hans Wippel3b2dec22018-05-18 09:34:18 +0200716 /* perform CLC handshake */
Ursula Braun7005ada2018-07-25 16:35:31 +0200717 rc = smc_connect_clc(smc, smc_type, &aclc, ibdev, ibport, gid, ismdev);
Hans Wippel41349842018-06-28 19:05:11 +0200718 if (rc) {
719 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200720 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200721 }
Ursula Brauna046d572017-01-09 16:55:16 +0100722
Hans Wippel41349842018-06-28 19:05:11 +0200723 /* depending on previous steps, connect using rdma or ism */
724 if (rdma_supported && aclc.hdr.path == SMC_TYPE_R)
725 rc = smc_connect_rdma(smc, &aclc, ibdev, ibport);
726 else if (ism_supported && aclc.hdr.path == SMC_TYPE_D)
727 rc = smc_connect_ism(smc, &aclc, ismdev);
728 else
Karsten Graul603cc142018-07-25 16:35:32 +0200729 rc = SMC_CLC_DECL_MODEUNSUPP;
Hans Wippel41349842018-06-28 19:05:11 +0200730 if (rc) {
731 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200732 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200733 }
Ursula Brauna046d572017-01-09 16:55:16 +0100734
Hans Wippel41349842018-06-28 19:05:11 +0200735 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200736 return 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100737}
738
Ursula Braun24ac3a02018-06-27 17:59:50 +0200739static void smc_connect_work(struct work_struct *work)
740{
741 struct smc_sock *smc = container_of(work, struct smc_sock,
742 connect_work);
743 int rc;
744
745 lock_sock(&smc->sk);
746 rc = kernel_connect(smc->clcsock, &smc->connect_info->addr,
747 smc->connect_info->alen, smc->connect_info->flags);
748 if (smc->clcsock->sk->sk_err) {
749 smc->sk.sk_err = smc->clcsock->sk->sk_err;
750 goto out;
751 }
752 if (rc < 0) {
753 smc->sk.sk_err = -rc;
754 goto out;
755 }
756
757 rc = __smc_connect(smc);
758 if (rc < 0)
759 smc->sk.sk_err = -rc;
760
761out:
Ursula Braun648a5a72018-09-18 15:46:34 +0200762 if (smc->sk.sk_err)
763 smc->sk.sk_state_change(&smc->sk);
764 else
765 smc->sk.sk_write_space(&smc->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200766 kfree(smc->connect_info);
767 smc->connect_info = NULL;
768 release_sock(&smc->sk);
769}
770
Ursula Braunac713872017-01-09 16:55:13 +0100771static int smc_connect(struct socket *sock, struct sockaddr *addr,
772 int alen, int flags)
773{
774 struct sock *sk = sock->sk;
775 struct smc_sock *smc;
776 int rc = -EINVAL;
777
778 smc = smc_sk(sk);
779
780 /* separate smc parameter checking to be safe */
781 if (alen < sizeof(addr->sa_family))
782 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100783 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100784 goto out_err;
785
786 lock_sock(sk);
787 switch (sk->sk_state) {
788 default:
789 goto out;
790 case SMC_ACTIVE:
791 rc = -EISCONN;
792 goto out;
793 case SMC_INIT:
794 rc = 0;
795 break;
796 }
797
798 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200799 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200800 if (flags & O_NONBLOCK) {
801 if (smc->connect_info) {
802 rc = -EALREADY;
803 goto out;
804 }
805 smc->connect_info = kzalloc(alen + 2 * sizeof(int), GFP_KERNEL);
806 if (!smc->connect_info) {
807 rc = -ENOMEM;
808 goto out;
809 }
810 smc->connect_info->alen = alen;
811 smc->connect_info->flags = flags ^ O_NONBLOCK;
812 memcpy(&smc->connect_info->addr, addr, alen);
813 schedule_work(&smc->connect_work);
814 rc = -EINPROGRESS;
815 } else {
816 rc = kernel_connect(smc->clcsock, addr, alen, flags);
817 if (rc)
818 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100819
Ursula Braun24ac3a02018-06-27 17:59:50 +0200820 rc = __smc_connect(smc);
821 if (rc < 0)
822 goto out;
823 else
824 rc = 0; /* success cases including fallback */
825 }
Ursula Braunac713872017-01-09 16:55:13 +0100826
827out:
828 release_sock(sk);
829out_err:
830 return rc;
831}
832
833static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
834{
Ursula Braun3163c502018-01-24 10:28:12 +0100835 struct socket *new_clcsock = NULL;
836 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100837 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -0800838 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +0100839
Ursula Braun3163c502018-01-24 10:28:12 +0100840 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100841 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100842 if (!new_sk) {
843 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100844 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100845 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100846 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100847 goto out;
848 }
849 *new_smc = smc_sk(new_sk);
850
Myungho Jung78abe3d2018-12-18 09:02:25 -0800851 mutex_lock(&lsmc->clcsock_release_lock);
852 if (lsmc->clcsock)
853 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
854 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +0100855 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100856 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100857 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100858 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Brauna046d572017-01-09 16:55:16 +0100859 if (new_clcsock)
860 sock_release(new_clcsock);
861 new_sk->sk_state = SMC_CLOSED;
862 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun3163c502018-01-24 10:28:12 +0100863 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100864 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100865 *new_smc = NULL;
866 goto out;
867 }
868
869 (*new_smc)->clcsock = new_clcsock;
870out:
871 return rc;
872}
873
Ursula Brauna046d572017-01-09 16:55:16 +0100874/* add a just created sock to the accept queue of the listen sock as
875 * candidate for a following socket accept call from user space
876 */
877static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
878{
879 struct smc_sock *par = smc_sk(parent);
880
Ursula Braun51f1de72018-01-26 09:28:48 +0100881 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100882 spin_lock(&par->accept_q_lock);
883 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
884 spin_unlock(&par->accept_q_lock);
885 sk_acceptq_added(parent);
886}
887
888/* remove a socket from the accept queue of its parental listening socket */
889static void smc_accept_unlink(struct sock *sk)
890{
891 struct smc_sock *par = smc_sk(sk)->listen_smc;
892
893 spin_lock(&par->accept_q_lock);
894 list_del_init(&smc_sk(sk)->accept_q);
895 spin_unlock(&par->accept_q_lock);
896 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100897 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100898}
899
900/* remove a sock from the accept queue to bind it to a new socket created
901 * for a socket accept call from user space
902 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100903struct sock *smc_accept_dequeue(struct sock *parent,
904 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100905{
906 struct smc_sock *isk, *n;
907 struct sock *new_sk;
908
909 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
910 new_sk = (struct sock *)isk;
911
912 smc_accept_unlink(new_sk);
913 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braun127f4972018-01-26 09:28:49 +0100914 if (isk->clcsock) {
915 sock_release(isk->clcsock);
916 isk->clcsock = NULL;
917 }
Ursula Braun288c83902017-04-10 14:58:04 +0200918 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100919 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +0100920 continue;
921 }
922 if (new_sock)
923 sock_graft(new_sk, new_sock);
924 return new_sk;
925 }
926 return NULL;
927}
928
929/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +0100930void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +0100931{
932 struct smc_sock *smc = smc_sk(sk);
933
Ursula Braunb38d7322017-01-09 16:55:25 +0100934 lock_sock(sk);
935 if (!sk->sk_lingertime)
936 /* wait for peer closing */
937 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100938 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100939 smc_close_active(smc);
Ursula Braun288c83902017-04-10 14:58:04 +0200940 sock_set_flag(sk, SOCK_DEAD);
941 sk->sk_shutdown |= SHUTDOWN_MASK;
942 }
Ursula Brauna046d572017-01-09 16:55:16 +0100943 if (smc->clcsock) {
944 struct socket *tcp;
945
946 tcp = smc->clcsock;
947 smc->clcsock = NULL;
948 sock_release(tcp);
949 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100950 if (smc->use_fallback) {
Ursula Braun51f1de72018-01-26 09:28:48 +0100951 sock_put(sk); /* passive closing */
952 sk->sk_state = SMC_CLOSED;
953 } else {
954 if (sk->sk_state == SMC_CLOSED)
955 smc_conn_free(&smc->conn);
Ursula Braunb38d7322017-01-09 16:55:25 +0100956 }
957 release_sock(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100958 sk->sk_prot->unhash(sk);
959 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +0100960}
961
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100962static int smc_serv_conf_first_link(struct smc_sock *smc)
963{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200964 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100965 struct smc_link_group *lgr = smc->conn.lgr;
966 struct smc_link *link;
967 int rest;
968 int rc;
969
970 link = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun652a1e42017-07-28 13:56:17 +0200971
Karsten Graul44aa81c2018-05-15 17:04:55 +0200972 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200973 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200974
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100975 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200976 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100977 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200978 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100979
980 /* receive CONFIRM LINK response from client over the RoCE fabric */
981 rest = wait_for_completion_interruptible_timeout(
982 &link->llc_confirm_resp,
983 SMC_LLC_WAIT_FIRST_TIME);
984 if (rest <= 0) {
985 struct smc_clc_msg_decline dclc;
986
987 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100988 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100989 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100990 }
991
Karsten Graul75d320d2018-03-01 13:51:31 +0100992 if (link->llc_confirm_resp_rc)
993 return SMC_CLC_DECL_RMBE_EC;
994
Karsten Graul52bedf32018-03-01 13:51:32 +0100995 /* send ADD LINK request to client over the RoCE fabric */
996 rc = smc_llc_send_add_link(link,
997 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +0200998 link->gid, SMC_LLC_REQ);
Karsten Graul52bedf32018-03-01 13:51:32 +0100999 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001000 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +01001001
1002 /* receive ADD LINK response from client over the RoCE fabric */
1003 rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
1004 SMC_LLC_WAIT_TIME);
1005 if (rest <= 0) {
1006 struct smc_clc_msg_decline dclc;
1007
1008 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001009 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001010 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
Karsten Graul52bedf32018-03-01 13:51:32 +01001011 }
1012
Karsten Graul877ae5b2018-05-02 16:56:44 +02001013 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +01001014
Karsten Graul75d320d2018-03-01 13:51:31 +01001015 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001016}
1017
Hans Wippel3b2dec22018-05-18 09:34:18 +02001018/* listen worker: finish */
1019static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001020{
Ursula Brauna046d572017-01-09 16:55:16 +01001021 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001022 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001023
Ursula Braunb38d7322017-01-09 16:55:25 +01001024 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001025 if (lsmc->sk.sk_state == SMC_LISTEN) {
1026 smc_accept_enqueue(&lsmc->sk, newsmcsk);
1027 } else { /* no longer listening */
1028 smc_close_non_accepted(newsmcsk);
1029 }
1030 release_sock(&lsmc->sk);
1031
1032 /* Wake up accept */
1033 lsmc->sk.sk_data_ready(&lsmc->sk);
1034 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001035}
Ursula Brauna046d572017-01-09 16:55:16 +01001036
Hans Wippel3b2dec22018-05-18 09:34:18 +02001037/* listen worker: finish in state connected */
1038static void smc_listen_out_connected(struct smc_sock *new_smc)
1039{
1040 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001041
Hans Wippel3b2dec22018-05-18 09:34:18 +02001042 sk_refcnt_debug_inc(newsmcsk);
1043 if (newsmcsk->sk_state == SMC_INIT)
1044 newsmcsk->sk_state = SMC_ACTIVE;
1045
1046 smc_listen_out(new_smc);
1047}
1048
1049/* listen worker: finish in error state */
1050static void smc_listen_out_err(struct smc_sock *new_smc)
1051{
1052 struct sock *newsmcsk = &new_smc->sk;
1053
Ursula Braun51f1de72018-01-26 09:28:48 +01001054 if (newsmcsk->sk_state == SMC_INIT)
1055 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001056 newsmcsk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001057 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001058
1059 smc_listen_out(new_smc);
1060}
1061
1062/* listen worker: decline and fall back if possible */
1063static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1064 int local_contact)
1065{
1066 /* RDMA setup failed, switch back to TCP */
1067 if (local_contact == SMC_FIRST_CONTACT)
1068 smc_lgr_forget(new_smc->conn.lgr);
1069 if (reason_code < 0) { /* error, no fallback possible */
1070 smc_listen_out_err(new_smc);
1071 return;
1072 }
1073 smc_conn_free(&new_smc->conn);
1074 new_smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001075 new_smc->fallback_rsn = reason_code;
1076 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001077 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1078 smc_listen_out_err(new_smc);
1079 return;
1080 }
1081 }
1082 smc_listen_out_connected(new_smc);
1083}
1084
1085/* listen worker: check prefixes */
1086static int smc_listen_rdma_check(struct smc_sock *new_smc,
1087 struct smc_clc_msg_proposal *pclc)
1088{
1089 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1090 struct socket *newclcsock = new_smc->clcsock;
1091
1092 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1093 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
1094 return SMC_CLC_DECL_CNFERR;
1095
1096 return 0;
1097}
1098
1099/* listen worker: initialize connection and buffers */
1100static int smc_listen_rdma_init(struct smc_sock *new_smc,
1101 struct smc_clc_msg_proposal *pclc,
1102 struct smc_ib_device *ibdev, u8 ibport,
1103 int *local_contact)
1104{
1105 /* allocate connection / link group */
Karsten Graulee05ff72018-11-20 16:46:40 +01001106 *local_contact = smc_conn_create(new_smc, false, 0, ibdev, ibport, 0,
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001107 &pclc->lcl, NULL, 0);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001108 if (*local_contact < 0) {
1109 if (*local_contact == -ENOMEM)
1110 return SMC_CLC_DECL_MEM;/* insufficient memory*/
1111 return SMC_CLC_DECL_INTERR; /* other error */
1112 }
1113
1114 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001115 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001116 return SMC_CLC_DECL_MEM;
1117
1118 return 0;
1119}
1120
Hans Wippel41349842018-06-28 19:05:11 +02001121/* listen worker: initialize connection and buffers for SMC-D */
1122static int smc_listen_ism_init(struct smc_sock *new_smc,
1123 struct smc_clc_msg_proposal *pclc,
1124 struct smcd_dev *ismdev,
1125 int *local_contact)
1126{
1127 struct smc_clc_msg_smcd *pclc_smcd;
1128
1129 pclc_smcd = smc_get_clc_msg_smcd(pclc);
Karsten Graulee05ff72018-11-20 16:46:40 +01001130 *local_contact = smc_conn_create(new_smc, true, 0, NULL, 0, 0, NULL,
Hans Wippel41349842018-06-28 19:05:11 +02001131 ismdev, pclc_smcd->gid);
1132 if (*local_contact < 0) {
1133 if (*local_contact == -ENOMEM)
1134 return SMC_CLC_DECL_MEM;/* insufficient memory*/
1135 return SMC_CLC_DECL_INTERR; /* other error */
1136 }
1137
1138 /* Check if peer can be reached via ISM device */
1139 if (smc_ism_cantalk(new_smc->conn.lgr->peer_gid,
1140 new_smc->conn.lgr->vlan_id,
1141 new_smc->conn.lgr->smcd)) {
1142 if (*local_contact == SMC_FIRST_CONTACT)
1143 smc_lgr_forget(new_smc->conn.lgr);
1144 smc_conn_free(&new_smc->conn);
1145 return SMC_CLC_DECL_CNFERR;
1146 }
1147
1148 /* Create send and receive buffers */
1149 if (smc_buf_create(new_smc, true)) {
1150 if (*local_contact == SMC_FIRST_CONTACT)
1151 smc_lgr_forget(new_smc->conn.lgr);
1152 smc_conn_free(&new_smc->conn);
1153 return SMC_CLC_DECL_MEM;
1154 }
1155
1156 return 0;
1157}
1158
Hans Wippel3b2dec22018-05-18 09:34:18 +02001159/* listen worker: register buffers */
1160static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
1161{
1162 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1163
1164 if (local_contact != SMC_FIRST_CONTACT) {
Karsten Graulc7674c02018-11-22 10:26:43 +01001165 if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true))
1166 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001167 }
1168 smc_rmb_sync_sg_for_device(&new_smc->conn);
1169
1170 return 0;
1171}
1172
1173/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001174static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1175 struct smc_clc_msg_accept_confirm *cclc,
1176 int local_contact)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001177{
1178 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1179 int reason_code = 0;
1180
1181 if (local_contact == SMC_FIRST_CONTACT)
1182 smc_link_save_peer_info(link, cclc);
1183
1184 if (smc_rmb_rtoken_handling(&new_smc->conn, cclc)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001185 reason_code = SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001186 goto decline;
1187 }
1188
1189 if (local_contact == SMC_FIRST_CONTACT) {
1190 if (smc_ib_ready_link(link)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001191 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001192 goto decline;
1193 }
1194 /* QP confirmation over RoCE fabric */
1195 reason_code = smc_serv_conf_first_link(new_smc);
1196 if (reason_code)
1197 goto decline;
1198 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001199 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001200
1201decline:
Hans Wippel3b2dec22018-05-18 09:34:18 +02001202 smc_listen_decline(new_smc, reason_code, local_contact);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001203 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001204}
1205
1206/* setup for RDMA connection of server */
1207static void smc_listen_work(struct work_struct *work)
1208{
1209 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1210 smc_listen_work);
1211 struct socket *newclcsock = new_smc->clcsock;
1212 struct smc_clc_msg_accept_confirm cclc;
1213 struct smc_clc_msg_proposal *pclc;
1214 struct smc_ib_device *ibdev;
Hans Wippel41349842018-06-28 19:05:11 +02001215 bool ism_supported = false;
1216 struct smcd_dev *ismdev;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001217 u8 buf[SMC_CLC_MAX_LEN];
1218 int local_contact = 0;
Ursula Braun7005ada2018-07-25 16:35:31 +02001219 unsigned short vlan;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001220 int reason_code = 0;
1221 int rc = 0;
1222 u8 ibport;
1223
1224 if (new_smc->use_fallback) {
1225 smc_listen_out_connected(new_smc);
1226 return;
1227 }
1228
1229 /* check if peer is smc capable */
1230 if (!tcp_sk(newclcsock->sk)->syn_smc) {
1231 new_smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001232 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001233 smc_listen_out_connected(new_smc);
1234 return;
1235 }
1236
1237 /* do inband token exchange -
1238 * wait for and receive SMC Proposal CLC message
1239 */
1240 pclc = (struct smc_clc_msg_proposal *)&buf;
1241 reason_code = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
Ursula Braun2b59f582018-11-22 10:26:39 +01001242 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001243 if (reason_code) {
1244 smc_listen_decline(new_smc, reason_code, 0);
1245 return;
1246 }
1247
1248 /* IPSec connections opt out of SMC-R optimizations */
1249 if (using_ipsec(new_smc)) {
1250 smc_listen_decline(new_smc, SMC_CLC_DECL_IPSEC, 0);
1251 return;
1252 }
1253
1254 mutex_lock(&smc_create_lgr_pending);
1255 smc_close_init(new_smc);
1256 smc_rx_init(new_smc);
1257 smc_tx_init(new_smc);
1258
Hans Wippel41349842018-06-28 19:05:11 +02001259 /* check if ISM is available */
1260 if ((pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) &&
1261 !smc_check_ism(new_smc, &ismdev) &&
1262 !smc_listen_ism_init(new_smc, pclc, ismdev, &local_contact)) {
1263 ism_supported = true;
1264 }
1265
Hans Wippel3b2dec22018-05-18 09:34:18 +02001266 /* check if RDMA is available */
Hans Wippel41349842018-06-28 19:05:11 +02001267 if (!ism_supported &&
1268 ((pclc->hdr.path != SMC_TYPE_R && pclc->hdr.path != SMC_TYPE_B) ||
Ursula Braun7005ada2018-07-25 16:35:31 +02001269 smc_vlan_by_tcpsk(new_smc->clcsock, &vlan) ||
1270 smc_check_rdma(new_smc, &ibdev, &ibport, vlan, NULL) ||
Hans Wippel41349842018-06-28 19:05:11 +02001271 smc_listen_rdma_check(new_smc, pclc) ||
1272 smc_listen_rdma_init(new_smc, pclc, ibdev, ibport,
1273 &local_contact) ||
1274 smc_listen_rdma_reg(new_smc, local_contact))) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001275 /* SMC not supported, decline */
1276 mutex_unlock(&smc_create_lgr_pending);
Karsten Graul603cc142018-07-25 16:35:32 +02001277 smc_listen_decline(new_smc, SMC_CLC_DECL_MODEUNSUPP,
1278 local_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001279 return;
1280 }
1281
1282 /* send SMC Accept CLC message */
1283 rc = smc_clc_send_accept(new_smc, local_contact);
1284 if (rc) {
1285 mutex_unlock(&smc_create_lgr_pending);
1286 smc_listen_decline(new_smc, rc, local_contact);
1287 return;
1288 }
1289
1290 /* receive SMC Confirm CLC message */
1291 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001292 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001293 if (reason_code) {
1294 mutex_unlock(&smc_create_lgr_pending);
1295 smc_listen_decline(new_smc, reason_code, local_contact);
1296 return;
1297 }
1298
1299 /* finish worker */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001300 if (!ism_supported) {
Ursula Braun587e41d2018-11-22 10:26:40 +01001301 if (smc_listen_rdma_finish(new_smc, &cclc, local_contact)) {
1302 mutex_unlock(&smc_create_lgr_pending);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001303 return;
Ursula Braun587e41d2018-11-22 10:26:40 +01001304 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001305 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001306 smc_conn_save_peer_info(new_smc, &cclc);
1307 mutex_unlock(&smc_create_lgr_pending);
1308 smc_listen_out_connected(new_smc);
Ursula Brauna046d572017-01-09 16:55:16 +01001309}
1310
1311static void smc_tcp_listen_work(struct work_struct *work)
1312{
1313 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1314 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001315 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001316 struct smc_sock *new_smc;
1317 int rc = 0;
1318
Ursula Braun3163c502018-01-24 10:28:12 +01001319 lock_sock(lsk);
1320 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001321 rc = smc_clcsock_accept(lsmc, &new_smc);
1322 if (rc)
1323 goto out;
1324 if (!new_smc)
1325 continue;
1326
1327 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001328 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001329 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001330 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001331 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1332 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001333 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1334 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001335 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1336 if (!schedule_work(&new_smc->smc_listen_work))
1337 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001338 }
1339
1340out:
Ursula Braun3163c502018-01-24 10:28:12 +01001341 release_sock(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001342 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +01001343}
1344
Ursula Braunac713872017-01-09 16:55:13 +01001345static int smc_listen(struct socket *sock, int backlog)
1346{
1347 struct sock *sk = sock->sk;
1348 struct smc_sock *smc;
1349 int rc;
1350
1351 smc = smc_sk(sk);
1352 lock_sock(sk);
1353
1354 rc = -EINVAL;
1355 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
1356 goto out;
1357
1358 rc = 0;
1359 if (sk->sk_state == SMC_LISTEN) {
1360 sk->sk_max_ack_backlog = backlog;
1361 goto out;
1362 }
1363 /* some socket options are handled in core, so we could not apply
1364 * them to the clc socket -- copy smc socket options to clc socket
1365 */
1366 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001367 if (!smc->use_fallback)
1368 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001369
1370 rc = kernel_listen(smc->clcsock, backlog);
1371 if (rc)
1372 goto out;
1373 sk->sk_max_ack_backlog = backlog;
1374 sk->sk_ack_backlog = 0;
1375 sk->sk_state = SMC_LISTEN;
Ursula Braun51f1de72018-01-26 09:28:48 +01001376 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1377 if (!schedule_work(&smc->tcp_listen_work))
1378 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001379
1380out:
1381 release_sock(sk);
1382 return rc;
1383}
1384
1385static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001386 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001387{
Ursula Brauna046d572017-01-09 16:55:16 +01001388 struct sock *sk = sock->sk, *nsk;
1389 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001390 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001391 long timeo;
1392 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001393
1394 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001395 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001396 lock_sock(sk);
1397
1398 if (lsmc->sk.sk_state != SMC_LISTEN) {
1399 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001400 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001401 goto out;
1402 }
1403
Ursula Brauna046d572017-01-09 16:55:16 +01001404 /* Wait for an incoming connection */
1405 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1406 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1407 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1408 set_current_state(TASK_INTERRUPTIBLE);
1409 if (!timeo) {
1410 rc = -EAGAIN;
1411 break;
1412 }
1413 release_sock(sk);
1414 timeo = schedule_timeout(timeo);
1415 /* wakeup by sk_data_ready in smc_listen_work() */
1416 sched_annotate_sleep();
1417 lock_sock(sk);
1418 if (signal_pending(current)) {
1419 rc = sock_intr_errno(timeo);
1420 break;
1421 }
1422 }
1423 set_current_state(TASK_RUNNING);
1424 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001425
Ursula Brauna046d572017-01-09 16:55:16 +01001426 if (!rc)
1427 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001428 release_sock(sk);
1429 if (rc)
1430 goto out;
1431
1432 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1433 /* wait till data arrives on the socket */
1434 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1435 MSEC_PER_SEC);
1436 if (smc_sk(nsk)->use_fallback) {
1437 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1438
1439 lock_sock(clcsk);
1440 if (skb_queue_empty(&clcsk->sk_receive_queue))
1441 sk_wait_data(clcsk, &timeo, NULL);
1442 release_sock(clcsk);
1443 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1444 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001445 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001446 release_sock(nsk);
1447 }
1448 }
Ursula Braunac713872017-01-09 16:55:13 +01001449
1450out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001451 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001452 return rc;
1453}
1454
1455static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001456 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001457{
1458 struct smc_sock *smc;
1459
Ursula Braunb38d7322017-01-09 16:55:25 +01001460 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1461 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001462 return -ENOTCONN;
1463
1464 smc = smc_sk(sock->sk);
1465
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001466 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001467}
1468
1469static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1470{
1471 struct sock *sk = sock->sk;
1472 struct smc_sock *smc;
1473 int rc = -EPIPE;
1474
1475 smc = smc_sk(sk);
1476 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001477 if ((sk->sk_state != SMC_ACTIVE) &&
1478 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1479 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001480 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001481
1482 if (msg->msg_flags & MSG_FASTOPEN) {
1483 if (sk->sk_state == SMC_INIT) {
1484 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001485 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001486 } else {
1487 rc = -EINVAL;
1488 goto out;
1489 }
1490 }
1491
Ursula Braunac713872017-01-09 16:55:13 +01001492 if (smc->use_fallback)
1493 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1494 else
Ursula Braune6727f32017-01-09 16:55:23 +01001495 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001496out:
1497 release_sock(sk);
1498 return rc;
1499}
1500
1501static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1502 int flags)
1503{
1504 struct sock *sk = sock->sk;
1505 struct smc_sock *smc;
1506 int rc = -ENOTCONN;
1507
1508 smc = smc_sk(sk);
1509 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001510 if ((sk->sk_state == SMC_INIT) ||
1511 (sk->sk_state == SMC_LISTEN) ||
1512 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001513 goto out;
1514
Ursula Braunb38d7322017-01-09 16:55:25 +01001515 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1516 rc = 0;
1517 goto out;
1518 }
1519
Stefan Raspl9014db22018-05-03 18:12:39 +02001520 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001521 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001522 } else {
1523 msg->msg_namelen = 0;
1524 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1525 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001526
Ursula Braunac713872017-01-09 16:55:13 +01001527out:
1528 release_sock(sk);
1529 return rc;
1530}
1531
Al Viroade994f2017-07-03 00:01:49 -04001532static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001533{
Ursula Braun8dce2782018-01-26 09:28:47 +01001534 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001535 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001536
Ursula Braun8dce2782018-01-26 09:28:47 +01001537 spin_lock(&isk->accept_q_lock);
1538 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001539 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001540 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001541
Ursula Braun8dce2782018-01-26 09:28:47 +01001542 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001543}
1544
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001545static __poll_t smc_poll(struct file *file, struct socket *sock,
1546 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01001547{
1548 struct sock *sk = sock->sk;
Al Viroe6c8adc2017-07-03 22:25:56 -04001549 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001550 struct smc_sock *smc;
1551
Ursula Braun8dce2782018-01-26 09:28:47 +01001552 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001553 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001554
Ursula Braunac713872017-01-09 16:55:13 +01001555 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02001556 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01001557 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001558 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02001559 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001560 if (sk->sk_err)
Ursula Braun784813a2018-05-02 16:53:56 +02001561 mask |= EPOLLERR;
Ursula Braunac713872017-01-09 16:55:13 +01001562 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07001563 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02001564 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01001565 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001566 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001567 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1568 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001569 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001570 if (sk->sk_state == SMC_LISTEN) {
1571 /* woken up by sk_data_ready in smc_listen_work() */
1572 mask = smc_accept_poll(sk);
1573 } else {
1574 if (atomic_read(&smc->conn.sndbuf_space) ||
1575 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001576 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001577 } else {
1578 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1579 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1580 }
1581 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001582 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001583 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001584 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001585 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001586 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02001587 if (smc->conn.urg_state == SMC_URG_VALID)
1588 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01001589 }
Ursula Braunac713872017-01-09 16:55:13 +01001590 }
1591
1592 return mask;
1593}
1594
1595static int smc_shutdown(struct socket *sock, int how)
1596{
1597 struct sock *sk = sock->sk;
1598 struct smc_sock *smc;
1599 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001600 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001601
1602 smc = smc_sk(sk);
1603
1604 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001605 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001606
1607 lock_sock(sk);
1608
1609 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02001610 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01001611 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1612 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1613 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1614 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1615 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001616 goto out;
1617 if (smc->use_fallback) {
1618 rc = kernel_sock_shutdown(smc->clcsock, how);
1619 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1620 if (sk->sk_shutdown == SHUTDOWN_MASK)
1621 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001622 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001623 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001624 switch (how) {
1625 case SHUT_RDWR: /* shutdown in both directions */
1626 rc = smc_close_active(smc);
1627 break;
1628 case SHUT_WR:
1629 rc = smc_close_shutdown_write(smc);
1630 break;
1631 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001632 rc = 0;
1633 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001634 break;
1635 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001636 if (smc->clcsock)
1637 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001638 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1639 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001640
1641out:
1642 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001643 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001644}
1645
1646static int smc_setsockopt(struct socket *sock, int level, int optname,
1647 char __user *optval, unsigned int optlen)
1648{
1649 struct sock *sk = sock->sk;
1650 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001651 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001652
1653 smc = smc_sk(sk);
1654
1655 /* generic setsockopts reaching us here always apply to the
1656 * CLC socket
1657 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001658 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1659 optval, optlen);
1660 if (smc->clcsock->sk->sk_err) {
1661 sk->sk_err = smc->clcsock->sk->sk_err;
1662 sk->sk_error_report(sk);
1663 }
1664 if (rc)
1665 return rc;
1666
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001667 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00001668 return -EINVAL;
Ursula Braunac0107e2018-07-18 15:22:50 +02001669 if (get_user(val, (int __user *)optval))
1670 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001671
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001672 lock_sock(sk);
1673 switch (optname) {
1674 case TCP_ULP:
1675 case TCP_FASTOPEN:
1676 case TCP_FASTOPEN_CONNECT:
1677 case TCP_FASTOPEN_KEY:
1678 case TCP_FASTOPEN_NO_COOKIE:
1679 /* option not supported by SMC */
1680 if (sk->sk_state == SMC_INIT) {
1681 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001682 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001683 } else {
1684 if (!smc->use_fallback)
1685 rc = -EINVAL;
1686 }
1687 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001688 case TCP_NODELAY:
1689 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001690 if (val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001691 mod_delayed_work(system_wq, &smc->conn.tx_work,
1692 0);
1693 }
1694 break;
1695 case TCP_CORK:
1696 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001697 if (!val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001698 mod_delayed_work(system_wq, &smc->conn.tx_work,
1699 0);
1700 }
1701 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001702 case TCP_DEFER_ACCEPT:
1703 smc->sockopt_defer_accept = val;
1704 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001705 default:
1706 break;
1707 }
1708 release_sock(sk);
1709
1710 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001711}
1712
1713static int smc_getsockopt(struct socket *sock, int level, int optname,
1714 char __user *optval, int __user *optlen)
1715{
1716 struct smc_sock *smc;
1717
1718 smc = smc_sk(sock->sk);
1719 /* socket options apply to the CLC socket */
1720 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1721 optval, optlen);
1722}
1723
1724static int smc_ioctl(struct socket *sock, unsigned int cmd,
1725 unsigned long arg)
1726{
Stefan Rasplde8474e2018-05-23 16:38:11 +02001727 union smc_host_cursor cons, urg;
1728 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01001729 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001730 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001731
1732 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001733 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02001734 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02001735 if (smc->use_fallback) {
1736 if (!smc->clcsock) {
1737 release_sock(&smc->sk);
1738 return -EBADF;
1739 }
1740 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1741 release_sock(&smc->sk);
1742 return answ;
1743 }
Ursula Braun9b67e262018-05-02 16:56:46 +02001744 switch (cmd) {
1745 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02001746 if (smc->sk.sk_state == SMC_LISTEN) {
1747 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001748 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001749 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001750 if (smc->sk.sk_state == SMC_INIT ||
1751 smc->sk.sk_state == SMC_CLOSED)
1752 answ = 0;
1753 else
1754 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02001755 break;
1756 case SIOCOUTQ:
1757 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02001758 if (smc->sk.sk_state == SMC_LISTEN) {
1759 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001760 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001761 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001762 if (smc->sk.sk_state == SMC_INIT ||
1763 smc->sk.sk_state == SMC_CLOSED)
1764 answ = 0;
1765 else
1766 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02001767 atomic_read(&smc->conn.sndbuf_space);
1768 break;
1769 case SIOCOUTQNSD:
1770 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02001771 if (smc->sk.sk_state == SMC_LISTEN) {
1772 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001773 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001774 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001775 if (smc->sk.sk_state == SMC_INIT ||
1776 smc->sk.sk_state == SMC_CLOSED)
1777 answ = 0;
1778 else
1779 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02001780 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02001781 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02001782 if (smc->sk.sk_state == SMC_LISTEN) {
1783 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001784 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001785 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001786 if (smc->sk.sk_state == SMC_INIT ||
1787 smc->sk.sk_state == SMC_CLOSED) {
1788 answ = 0;
1789 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02001790 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1791 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001792 answ = smc_curs_diff(conn->rmb_desc->len,
1793 &cons, &urg) == 1;
1794 }
1795 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02001796 default:
Ursula Braun1992d992018-07-16 13:56:52 +02001797 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001798 return -ENOIOCTLCMD;
1799 }
Ursula Braun1992d992018-07-16 13:56:52 +02001800 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001801
1802 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001803}
1804
1805static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1806 int offset, size_t size, int flags)
1807{
1808 struct sock *sk = sock->sk;
1809 struct smc_sock *smc;
1810 int rc = -EPIPE;
1811
1812 smc = smc_sk(sk);
1813 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001814 if (sk->sk_state != SMC_ACTIVE) {
1815 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001816 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001817 }
1818 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001819 if (smc->use_fallback)
1820 rc = kernel_sendpage(smc->clcsock, page, offset,
1821 size, flags);
1822 else
1823 rc = sock_no_sendpage(sock, page, offset, size, flags);
1824
1825out:
Ursula Braunac713872017-01-09 16:55:13 +01001826 return rc;
1827}
1828
Stefan Raspl9014db22018-05-03 18:12:39 +02001829/* Map the affected portions of the rmbe into an spd, note the number of bytes
1830 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1831 * updates till whenever a respective page has been fully processed.
1832 * Note that subsequent recv() calls have to wait till all splice() processing
1833 * completed.
1834 */
Ursula Braunac713872017-01-09 16:55:13 +01001835static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1836 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001837 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001838{
1839 struct sock *sk = sock->sk;
1840 struct smc_sock *smc;
1841 int rc = -ENOTCONN;
1842
1843 smc = smc_sk(sk);
1844 lock_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001845
1846 if (sk->sk_state == SMC_INIT ||
1847 sk->sk_state == SMC_LISTEN ||
1848 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001849 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001850
1851 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1852 rc = 0;
1853 goto out;
1854 }
1855
Ursula Braunac713872017-01-09 16:55:13 +01001856 if (smc->use_fallback) {
1857 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1858 pipe, len, flags);
1859 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001860 if (*ppos) {
1861 rc = -ESPIPE;
1862 goto out;
1863 }
1864 if (flags & SPLICE_F_NONBLOCK)
1865 flags = MSG_DONTWAIT;
1866 else
1867 flags = 0;
1868 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001869 }
1870out:
1871 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001872
Ursula Braunac713872017-01-09 16:55:13 +01001873 return rc;
1874}
1875
1876/* must look like tcp */
1877static const struct proto_ops smc_sock_ops = {
1878 .family = PF_SMC,
1879 .owner = THIS_MODULE,
1880 .release = smc_release,
1881 .bind = smc_bind,
1882 .connect = smc_connect,
1883 .socketpair = sock_no_socketpair,
1884 .accept = smc_accept,
1885 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001886 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01001887 .ioctl = smc_ioctl,
1888 .listen = smc_listen,
1889 .shutdown = smc_shutdown,
1890 .setsockopt = smc_setsockopt,
1891 .getsockopt = smc_getsockopt,
1892 .sendmsg = smc_sendmsg,
1893 .recvmsg = smc_recvmsg,
1894 .mmap = sock_no_mmap,
1895 .sendpage = smc_sendpage,
1896 .splice_read = smc_splice_read,
1897};
1898
1899static int smc_create(struct net *net, struct socket *sock, int protocol,
1900 int kern)
1901{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001902 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001903 struct smc_sock *smc;
1904 struct sock *sk;
1905 int rc;
1906
1907 rc = -ESOCKTNOSUPPORT;
1908 if (sock->type != SOCK_STREAM)
1909 goto out;
1910
1911 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001912 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01001913 goto out;
1914
1915 rc = -ENOBUFS;
1916 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001917 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001918 if (!sk)
1919 goto out;
1920
1921 /* create internal TCP socket for CLC handshake and fallback */
1922 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001923 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02001924 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001925 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1926 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01001927 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01001928 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01001929 goto out;
1930 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01001931 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1932 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01001933
1934out:
1935 return rc;
1936}
1937
1938static const struct net_proto_family smc_sock_family_ops = {
1939 .family = PF_SMC,
1940 .owner = THIS_MODULE,
1941 .create = smc_create,
1942};
1943
1944static int __init smc_init(void)
1945{
1946 int rc;
1947
Thomas Richter6812baa2017-01-09 16:55:15 +01001948 rc = smc_pnet_init();
1949 if (rc)
1950 return rc;
1951
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001952 rc = smc_llc_init();
1953 if (rc) {
1954 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1955 goto out_pnet;
1956 }
1957
Ursula Braun5f083182017-01-09 16:55:22 +01001958 rc = smc_cdc_init();
1959 if (rc) {
1960 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1961 goto out_pnet;
1962 }
1963
Ursula Braunac713872017-01-09 16:55:13 +01001964 rc = proto_register(&smc_proto, 1);
1965 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01001966 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Thomas Richter6812baa2017-01-09 16:55:15 +01001967 goto out_pnet;
Ursula Braunac713872017-01-09 16:55:13 +01001968 }
1969
Karsten Graulaaa4d332018-03-16 15:06:41 +01001970 rc = proto_register(&smc_proto6, 1);
1971 if (rc) {
1972 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
1973 goto out_proto;
1974 }
1975
Ursula Braunac713872017-01-09 16:55:13 +01001976 rc = sock_register(&smc_sock_family_ops);
1977 if (rc) {
1978 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001979 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01001980 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01001981 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001982 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01001983
Ursula Brauna4cf0442017-01-09 16:55:14 +01001984 rc = smc_ib_register_client();
1985 if (rc) {
1986 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1987 goto out_sock;
1988 }
1989
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001990 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01001991 return 0;
1992
Ursula Brauna4cf0442017-01-09 16:55:14 +01001993out_sock:
1994 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001995out_proto6:
1996 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01001997out_proto:
1998 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001999out_pnet:
2000 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01002001 return rc;
2002}
2003
2004static void __exit smc_exit(void)
2005{
Hans Wippel9fda3512018-05-18 09:34:11 +02002006 smc_core_exit();
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002007 static_branch_disable(&tcp_have_smc);
Ursula Brauna4cf0442017-01-09 16:55:14 +01002008 smc_ib_unregister_client();
Ursula Braunac713872017-01-09 16:55:13 +01002009 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002010 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002011 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002012 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01002013}
2014
2015module_init(smc_init);
2016module_exit(smc_exit);
2017
2018MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2019MODULE_DESCRIPTION("smc socket address family");
2020MODULE_LICENSE("GPL");
2021MODULE_ALIAS_NETPROTO(PF_SMC);