blob: 77ef53596d18c5fd091b6888efbc8b35063087a8 [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
Hans Wippel64e28b52019-02-21 13:01:02 +010033#include <net/net_namespace.h>
34#include <net/netns/generic.h>
35#include "smc_netns.h"
36
Ursula Braunac713872017-01-09 16:55:13 +010037#include "smc.h"
Ursula Brauna046d572017-01-09 16:55:16 +010038#include "smc_clc.h"
Ursula Braun9bf9abe2017-01-09 16:55:21 +010039#include "smc_llc.h"
Ursula Braun5f083182017-01-09 16:55:22 +010040#include "smc_cdc.h"
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010041#include "smc_core.h"
Ursula Brauna4cf0442017-01-09 16:55:14 +010042#include "smc_ib.h"
Hans Wippel41349842018-06-28 19:05:11 +020043#include "smc_ism.h"
Thomas Richter6812baa2017-01-09 16:55:15 +010044#include "smc_pnet.h"
Ursula Braune6727f32017-01-09 16:55:23 +010045#include "smc_tx.h"
Ursula Braun952310c2017-01-09 16:55:24 +010046#include "smc_rx.h"
Ursula Braunb38d7322017-01-09 16:55:25 +010047#include "smc_close.h"
Ursula Braunac713872017-01-09 16:55:13 +010048
Hans Wippel72a36a82019-02-07 15:56:18 +010049static DEFINE_MUTEX(smc_server_lgr_pending); /* serialize link group
50 * creation on server
51 */
52static DEFINE_MUTEX(smc_client_lgr_pending); /* serialize link group
53 * creation on client
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010054 */
55
Ursula Brauna046d572017-01-09 16:55:16 +010056static void smc_tcp_listen_work(struct work_struct *);
Ursula Braun24ac3a02018-06-27 17:59:50 +020057static void smc_connect_work(struct work_struct *);
Ursula Brauna046d572017-01-09 16:55:16 +010058
Ursula Braunac713872017-01-09 16:55:13 +010059static void smc_set_keepalive(struct sock *sk, int val)
60{
61 struct smc_sock *smc = smc_sk(sk);
62
63 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
64}
65
Ursula Braunf16a7dd2017-01-09 16:55:26 +010066static struct smc_hashinfo smc_v4_hashinfo = {
67 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
68};
69
Karsten Graulaaa4d332018-03-16 15:06:41 +010070static struct smc_hashinfo smc_v6_hashinfo = {
71 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
72};
73
Ursula Braunf16a7dd2017-01-09 16:55:26 +010074int smc_hash_sk(struct sock *sk)
75{
76 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
77 struct hlist_head *head;
78
79 head = &h->ht;
80
81 write_lock_bh(&h->lock);
82 sk_add_node(sk, head);
83 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
84 write_unlock_bh(&h->lock);
85
86 return 0;
87}
88EXPORT_SYMBOL_GPL(smc_hash_sk);
89
90void smc_unhash_sk(struct sock *sk)
91{
92 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
93
94 write_lock_bh(&h->lock);
95 if (sk_del_node_init(sk))
96 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
97 write_unlock_bh(&h->lock);
98}
99EXPORT_SYMBOL_GPL(smc_unhash_sk);
100
101struct proto smc_proto = {
Ursula Braunac713872017-01-09 16:55:13 +0100102 .name = "SMC",
103 .owner = THIS_MODULE,
104 .keepalive = smc_set_keepalive,
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100105 .hash = smc_hash_sk,
106 .unhash = smc_unhash_sk,
Ursula Braunac713872017-01-09 16:55:13 +0100107 .obj_size = sizeof(struct smc_sock),
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100108 .h.smc_hash = &smc_v4_hashinfo,
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800109 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Ursula Braunac713872017-01-09 16:55:13 +0100110};
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100111EXPORT_SYMBOL_GPL(smc_proto);
Ursula Braunac713872017-01-09 16:55:13 +0100112
Karsten Graulaaa4d332018-03-16 15:06:41 +0100113struct proto smc_proto6 = {
114 .name = "SMC6",
115 .owner = THIS_MODULE,
116 .keepalive = smc_set_keepalive,
117 .hash = smc_hash_sk,
118 .unhash = smc_unhash_sk,
119 .obj_size = sizeof(struct smc_sock),
120 .h.smc_hash = &smc_v6_hashinfo,
121 .slab_flags = SLAB_TYPESAFE_BY_RCU,
122};
123EXPORT_SYMBOL_GPL(smc_proto6);
124
Ursula Braunac713872017-01-09 16:55:13 +0100125static int smc_release(struct socket *sock)
126{
127 struct sock *sk = sock->sk;
128 struct smc_sock *smc;
Ursula Braunb38d7322017-01-09 16:55:25 +0100129 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +0100130
131 if (!sk)
132 goto out;
133
134 smc = smc_sk(sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200135
136 /* cleanup for a dangling non-blocking connect */
Hans Wippelf07920a2018-11-20 16:46:39 +0100137 if (smc->connect_info && sk->sk_state == SMC_INIT)
138 tcp_abort(smc->clcsock->sk, ECONNABORTED);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200139 flush_work(&smc->connect_work);
140 kfree(smc->connect_info);
141 smc->connect_info = NULL;
142
Ursula Braunb38d7322017-01-09 16:55:25 +0100143 if (sk->sk_state == SMC_LISTEN)
144 /* smc_close_non_accepted() is called and acquires
145 * sock lock for child sockets again
146 */
147 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
148 else
149 lock_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +0100150
Ursula Braun51f1de72018-01-26 09:28:48 +0100151 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100152 rc = smc_close_active(smc);
153 sock_set_flag(sk, SOCK_DEAD);
154 sk->sk_shutdown |= SHUTDOWN_MASK;
Ursula Braunb03faa12019-02-07 15:56:15 +0100155 } else {
156 if (sk->sk_state != SMC_LISTEN && sk->sk_state != SMC_INIT)
157 sock_put(sk); /* passive closing */
158 if (sk->sk_state == SMC_LISTEN) {
159 /* wake up clcsock accept */
160 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
161 }
162 sk->sk_state = SMC_CLOSED;
163 sk->sk_state_change(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +0100164 }
Cong Wang26d92e92019-01-05 23:45:26 -0800165
166 sk->sk_prot->unhash(sk);
167
Ursula Braunb03faa12019-02-07 15:56:15 +0100168 if (sk->sk_state == SMC_CLOSED) {
169 if (smc->clcsock) {
170 mutex_lock(&smc->clcsock_release_lock);
171 sock_release(smc->clcsock);
172 smc->clcsock = NULL;
173 mutex_unlock(&smc->clcsock_release_lock);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800174 }
Ursula Braunb03faa12019-02-07 15:56:15 +0100175 if (!smc->use_fallback)
176 smc_conn_free(&smc->conn);
Ursula Braun51f1de72018-01-26 09:28:48 +0100177 }
Ursula Braunac713872017-01-09 16:55:13 +0100178
179 /* detach socket */
180 sock_orphan(sk);
181 sock->sk = NULL;
182 release_sock(sk);
183
Ursula Braun51f1de72018-01-26 09:28:48 +0100184 sock_put(sk); /* final sock_put */
Ursula Braunac713872017-01-09 16:55:13 +0100185out:
Ursula Braunb38d7322017-01-09 16:55:25 +0100186 return rc;
Ursula Braunac713872017-01-09 16:55:13 +0100187}
188
189static void smc_destruct(struct sock *sk)
190{
191 if (sk->sk_state != SMC_CLOSED)
192 return;
193 if (!sock_flag(sk, SOCK_DEAD))
194 return;
195
196 sk_refcnt_debug_dec(sk);
197}
198
Karsten Graulaaa4d332018-03-16 15:06:41 +0100199static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
200 int protocol)
Ursula Braunac713872017-01-09 16:55:13 +0100201{
202 struct smc_sock *smc;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100203 struct proto *prot;
Ursula Braunac713872017-01-09 16:55:13 +0100204 struct sock *sk;
205
Karsten Graulaaa4d332018-03-16 15:06:41 +0100206 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
207 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
Ursula Braunac713872017-01-09 16:55:13 +0100208 if (!sk)
209 return NULL;
210
211 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
212 sk->sk_state = SMC_INIT;
213 sk->sk_destruct = smc_destruct;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100214 sk->sk_protocol = protocol;
Ursula Braunac713872017-01-09 16:55:13 +0100215 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100216 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200217 INIT_WORK(&smc->connect_work, smc_connect_work);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700218 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
Ursula Brauna046d572017-01-09 16:55:16 +0100219 INIT_LIST_HEAD(&smc->accept_q);
220 spin_lock_init(&smc->accept_q_lock);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700221 spin_lock_init(&smc->conn.send_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100222 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100223 sk_refcnt_debug_inc(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800224 mutex_init(&smc->clcsock_release_lock);
Ursula Braunac713872017-01-09 16:55:13 +0100225
226 return sk;
227}
228
229static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
230 int addr_len)
231{
232 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
233 struct sock *sk = sock->sk;
234 struct smc_sock *smc;
235 int rc;
236
237 smc = smc_sk(sk);
238
239 /* replicate tests from inet_bind(), to be safe wrt. future changes */
240 rc = -EINVAL;
241 if (addr_len < sizeof(struct sockaddr_in))
242 goto out;
243
244 rc = -EAFNOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100245 if (addr->sin_family != AF_INET &&
246 addr->sin_family != AF_INET6 &&
247 addr->sin_family != AF_UNSPEC)
248 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100249 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
Karsten Graulaaa4d332018-03-16 15:06:41 +0100250 if (addr->sin_family == AF_UNSPEC &&
251 addr->sin_addr.s_addr != htonl(INADDR_ANY))
Ursula Braunac713872017-01-09 16:55:13 +0100252 goto out;
253
254 lock_sock(sk);
255
256 /* Check if socket is already active */
257 rc = -EINVAL;
258 if (sk->sk_state != SMC_INIT)
259 goto out_rel;
260
261 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
262 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
263
264out_rel:
265 release_sock(sk);
266out:
267 return rc;
268}
269
270static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
271 unsigned long mask)
272{
273 /* options we don't get control via setsockopt for */
274 nsk->sk_type = osk->sk_type;
275 nsk->sk_sndbuf = osk->sk_sndbuf;
276 nsk->sk_rcvbuf = osk->sk_rcvbuf;
277 nsk->sk_sndtimeo = osk->sk_sndtimeo;
278 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
279 nsk->sk_mark = osk->sk_mark;
280 nsk->sk_priority = osk->sk_priority;
281 nsk->sk_rcvlowat = osk->sk_rcvlowat;
282 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
283 nsk->sk_err = osk->sk_err;
284
285 nsk->sk_flags &= ~mask;
286 nsk->sk_flags |= osk->sk_flags & mask;
287}
288
289#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
290 (1UL << SOCK_KEEPOPEN) | \
291 (1UL << SOCK_LINGER) | \
292 (1UL << SOCK_BROADCAST) | \
293 (1UL << SOCK_TIMESTAMP) | \
294 (1UL << SOCK_DBG) | \
295 (1UL << SOCK_RCVTSTAMP) | \
296 (1UL << SOCK_RCVTSTAMPNS) | \
297 (1UL << SOCK_LOCALROUTE) | \
298 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
299 (1UL << SOCK_RXQ_OVFL) | \
300 (1UL << SOCK_WIFI_STATUS) | \
301 (1UL << SOCK_NOFCS) | \
Deepa Dinamani97184752019-02-02 07:34:51 -0800302 (1UL << SOCK_FILTER_LOCKED) | \
303 (1UL << SOCK_TSTAMP_NEW))
Ursula Braunac713872017-01-09 16:55:13 +0100304/* copy only relevant settings and flags of SOL_SOCKET level from smc to
305 * clc socket (since smc is not called for these options from net/core)
306 */
307static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
308{
309 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
310}
311
312#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
313 (1UL << SOCK_KEEPOPEN) | \
314 (1UL << SOCK_LINGER) | \
315 (1UL << SOCK_DBG))
316/* copy only settings and flags relevant for smc from clc to smc socket */
317static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
318{
319 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
320}
321
Karsten Graulc7674c02018-11-22 10:26:43 +0100322/* register a new rmb, send confirm_rkey msg to register with peer */
Karsten Graul44aa81c2018-05-15 17:04:55 +0200323static int smc_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc,
324 bool conf_rkey)
Karsten Graule63a5f82018-05-03 17:57:37 +0200325{
Karsten Graulc7674c02018-11-22 10:26:43 +0100326 if (!rmb_desc->wr_reg) {
327 /* register memory region for new rmb */
328 if (smc_wr_reg_send(link, rmb_desc->mr_rx[SMC_SINGLE_LINK])) {
329 rmb_desc->regerr = 1;
330 return -EFAULT;
331 }
332 rmb_desc->wr_reg = 1;
Karsten Graula6920d12018-05-03 17:57:38 +0200333 }
Karsten Graul44aa81c2018-05-15 17:04:55 +0200334 if (!conf_rkey)
335 return 0;
336 /* exchange confirm_rkey msg with peer */
337 if (smc_llc_do_confirm_rkey(link, rmb_desc)) {
338 rmb_desc->regerr = 1;
339 return -EFAULT;
340 }
Karsten Graule63a5f82018-05-03 17:57:37 +0200341 return 0;
342}
343
Stefan Raspl0f627122018-03-01 13:51:26 +0100344static int smc_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100345{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200346 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100347 struct smc_link_group *lgr = smc->conn.lgr;
348 struct smc_link *link;
349 int rest;
350 int rc;
351
352 link = &lgr->lnk[SMC_SINGLE_LINK];
353 /* receive CONFIRM LINK request from server over RoCE fabric */
354 rest = wait_for_completion_interruptible_timeout(
355 &link->llc_confirm,
356 SMC_LLC_WAIT_FIRST_TIME);
357 if (rest <= 0) {
358 struct smc_clc_msg_decline dclc;
359
360 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100361 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100362 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100363 }
364
Karsten Graul75d320d2018-03-01 13:51:31 +0100365 if (link->llc_confirm_rc)
366 return SMC_CLC_DECL_RMBE_EC;
367
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100368 rc = smc_ib_modify_qp_rts(link);
369 if (rc)
Karsten Graul603cc142018-07-25 16:35:32 +0200370 return SMC_CLC_DECL_ERR_RDYLNK;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100371
372 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200373
Karsten Graul44aa81c2018-05-15 17:04:55 +0200374 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200375 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200376
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100377 /* send CONFIRM LINK response over RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200378 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100379 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200380 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100381
Karsten Graul52bedf32018-03-01 13:51:32 +0100382 /* receive ADD LINK request from server over RoCE fabric */
383 rest = wait_for_completion_interruptible_timeout(&link->llc_add,
384 SMC_LLC_WAIT_TIME);
385 if (rest <= 0) {
386 struct smc_clc_msg_decline dclc;
387
388 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100389 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100390 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
Karsten Graul52bedf32018-03-01 13:51:32 +0100391 }
392
393 /* send add link reject message, only one link supported for now */
394 rc = smc_llc_send_add_link(link,
395 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +0200396 link->gid, SMC_LLC_RESP);
Karsten Graul52bedf32018-03-01 13:51:32 +0100397 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200398 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +0100399
Karsten Graul877ae5b2018-05-02 16:56:44 +0200400 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100401
Karsten Graul75d320d2018-03-01 13:51:31 +0100402 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100403}
404
Hans Wippel41349842018-06-28 19:05:11 +0200405static void smcr_conn_save_peer_info(struct smc_sock *smc,
406 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100407{
Hans Wippel95d8d2632018-05-18 09:34:13 +0200408 int bufsize = smc_uncompress_bufsize(clc->rmbe_size);
409
Hans Wippel92a138e2018-05-18 09:34:12 +0200410 smc->conn.peer_rmbe_idx = clc->rmbe_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100411 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200412 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100413 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200414 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100415}
416
Hans Wippel41349842018-06-28 19:05:11 +0200417static void smcd_conn_save_peer_info(struct smc_sock *smc,
418 struct smc_clc_msg_accept_confirm *clc)
419{
420 int bufsize = smc_uncompress_bufsize(clc->dmbe_size);
421
422 smc->conn.peer_rmbe_idx = clc->dmbe_idx;
423 smc->conn.peer_token = clc->token;
424 /* msg header takes up space in the buffer */
425 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
426 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
427 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
428}
429
430static void smc_conn_save_peer_info(struct smc_sock *smc,
431 struct smc_clc_msg_accept_confirm *clc)
432{
433 if (smc->conn.lgr->is_smcd)
434 smcd_conn_save_peer_info(smc, clc);
435 else
436 smcr_conn_save_peer_info(smc, clc);
437}
438
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100439static void smc_link_save_peer_info(struct smc_link *link,
440 struct smc_clc_msg_accept_confirm *clc)
441{
442 link->peer_qpn = ntoh24(clc->qpn);
443 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
444 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
445 link->peer_psn = ntoh24(clc->psn);
446 link->peer_mtu = clc->qp_mtu;
447}
448
Hans Wippel3b2dec22018-05-18 09:34:18 +0200449/* fall back during connect */
Karsten Graul603cc142018-07-25 16:35:32 +0200450static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200451{
452 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +0200453 smc->fallback_rsn = reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200454 smc_copy_sock_settings_to_clc(smc);
455 if (smc->sk.sk_state == SMC_INIT)
456 smc->sk.sk_state = SMC_ACTIVE;
457 return 0;
458}
459
460/* decline and fall back during connect */
461static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
462{
463 int rc;
464
Ursula Braune1bbdd52018-07-05 16:15:30 +0200465 if (reason_code < 0) { /* error, fallback is not possible */
466 if (smc->sk.sk_state == SMC_INIT)
467 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200468 return reason_code;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200469 }
Karsten Graul603cc142018-07-25 16:35:32 +0200470 if (reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200471 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200472 if (rc < 0) {
473 if (smc->sk.sk_state == SMC_INIT)
474 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200475 return rc;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200476 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200477 }
Karsten Graul603cc142018-07-25 16:35:32 +0200478 return smc_connect_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200479}
480
481/* abort connecting */
482static int smc_connect_abort(struct smc_sock *smc, int reason_code,
483 int local_contact)
484{
485 if (local_contact == SMC_FIRST_CONTACT)
486 smc_lgr_forget(smc->conn.lgr);
Hans Wippel72a36a82019-02-07 15:56:18 +0100487 if (smc->conn.lgr->is_smcd)
488 /* there is only one lgr role for SMC-D; use server lock */
489 mutex_unlock(&smc_server_lgr_pending);
490 else
491 mutex_unlock(&smc_client_lgr_pending);
492
Hans Wippel3b2dec22018-05-18 09:34:18 +0200493 smc_conn_free(&smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200494 return reason_code;
495}
496
497/* check if there is a rdma device available for this connection. */
498/* called for connect and listen */
499static int smc_check_rdma(struct smc_sock *smc, struct smc_ib_device **ibdev,
Ursula Braun7005ada2018-07-25 16:35:31 +0200500 u8 *ibport, unsigned short vlan_id, u8 gid[])
Hans Wippel3b2dec22018-05-18 09:34:18 +0200501{
502 int reason_code = 0;
503
504 /* PNET table look up: search active ib_device and port
505 * within same PNETID that also contains the ethernet device
506 * used for the internal TCP socket
507 */
Ursula Braun7005ada2018-07-25 16:35:31 +0200508 smc_pnet_find_roce_resource(smc->clcsock->sk, ibdev, ibport, vlan_id,
509 gid);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200510 if (!(*ibdev))
511 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
512
513 return reason_code;
514}
515
Hans Wippel41349842018-06-28 19:05:11 +0200516/* check if there is an ISM device available for this connection. */
517/* called for connect and listen */
518static int smc_check_ism(struct smc_sock *smc, struct smcd_dev **ismdev)
519{
520 /* Find ISM device with same PNETID as connecting interface */
521 smc_pnet_find_ism_resource(smc->clcsock->sk, ismdev);
522 if (!(*ismdev))
523 return SMC_CLC_DECL_CNFERR; /* configuration error */
524 return 0;
525}
526
527/* Check for VLAN ID and register it on ISM device just for CLC handshake */
528static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
529 struct smcd_dev *ismdev,
530 unsigned short vlan_id)
531{
532 if (vlan_id && smc_ism_get_vlan(ismdev, vlan_id))
533 return SMC_CLC_DECL_CNFERR;
534 return 0;
535}
536
537/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
538 * used, the VLAN ID will be registered again during the connection setup.
539 */
540static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
541 struct smcd_dev *ismdev,
542 unsigned short vlan_id)
543{
544 if (!is_smcd)
545 return 0;
546 if (vlan_id && smc_ism_put_vlan(ismdev, vlan_id))
547 return SMC_CLC_DECL_CNFERR;
548 return 0;
549}
550
Hans Wippel3b2dec22018-05-18 09:34:18 +0200551/* CLC handshake during connect */
Hans Wippelc758dfd2018-06-28 19:05:09 +0200552static int smc_connect_clc(struct smc_sock *smc, int smc_type,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200553 struct smc_clc_msg_accept_confirm *aclc,
Hans Wippel41349842018-06-28 19:05:11 +0200554 struct smc_ib_device *ibdev, u8 ibport,
Ursula Braun7005ada2018-07-25 16:35:31 +0200555 u8 gid[], struct smcd_dev *ismdev)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200556{
557 int rc = 0;
558
559 /* do inband token exchange */
Ursula Braun7005ada2018-07-25 16:35:31 +0200560 rc = smc_clc_send_proposal(smc, smc_type, ibdev, ibport, gid, ismdev);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200561 if (rc)
562 return rc;
563 /* receive SMC Accept CLC message */
Ursula Braun2b59f582018-11-22 10:26:39 +0100564 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT,
565 CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200566}
567
Ursula Brauna046d572017-01-09 16:55:16 +0100568/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200569static int smc_connect_rdma(struct smc_sock *smc,
570 struct smc_clc_msg_accept_confirm *aclc,
571 struct smc_ib_device *ibdev, u8 ibport)
572{
573 int local_contact = SMC_FIRST_CONTACT;
574 struct smc_link *link;
575 int reason_code = 0;
576
Hans Wippel72a36a82019-02-07 15:56:18 +0100577 mutex_lock(&smc_client_lgr_pending);
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200578 local_contact = smc_conn_create(smc, false, aclc->hdr.flag, ibdev,
Karsten Graulee05ff72018-11-20 16:46:40 +0100579 ibport, ntoh24(aclc->qpn), &aclc->lcl,
580 NULL, 0);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200581 if (local_contact < 0) {
582 if (local_contact == -ENOMEM)
583 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
584 else if (local_contact == -ENOLINK)
585 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
586 else
587 reason_code = SMC_CLC_DECL_INTERR; /* other error */
Hans Wippel72a36a82019-02-07 15:56:18 +0100588 mutex_unlock(&smc_client_lgr_pending);
589 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200590 }
591 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
592
593 smc_conn_save_peer_info(smc, aclc);
594
595 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200596 if (smc_buf_create(smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +0200597 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, local_contact);
598
599 if (local_contact == SMC_FIRST_CONTACT)
600 smc_link_save_peer_info(link, aclc);
601
602 if (smc_rmb_rtoken_handling(&smc->conn, aclc))
Karsten Graul603cc142018-07-25 16:35:32 +0200603 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200604 local_contact);
605
606 smc_close_init(smc);
607 smc_rx_init(smc);
608
609 if (local_contact == SMC_FIRST_CONTACT) {
610 if (smc_ib_ready_link(link))
Karsten Graul603cc142018-07-25 16:35:32 +0200611 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200612 local_contact);
613 } else {
Karsten Graulc7674c02018-11-22 10:26:43 +0100614 if (smc_reg_rmb(link, smc->conn.rmb_desc, true))
Karsten Graul603cc142018-07-25 16:35:32 +0200615 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200616 local_contact);
617 }
618 smc_rmb_sync_sg_for_device(&smc->conn);
619
620 reason_code = smc_clc_send_confirm(smc);
621 if (reason_code)
622 return smc_connect_abort(smc, reason_code, local_contact);
623
624 smc_tx_init(smc);
625
626 if (local_contact == SMC_FIRST_CONTACT) {
627 /* QP confirmation over RoCE fabric */
628 reason_code = smc_clnt_conf_first_link(smc);
629 if (reason_code)
630 return smc_connect_abort(smc, reason_code,
631 local_contact);
632 }
Hans Wippel72a36a82019-02-07 15:56:18 +0100633 mutex_unlock(&smc_client_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200634
635 smc_copy_sock_settings_to_clc(smc);
636 if (smc->sk.sk_state == SMC_INIT)
637 smc->sk.sk_state = SMC_ACTIVE;
638
639 return 0;
640}
641
Hans Wippel41349842018-06-28 19:05:11 +0200642/* setup for ISM connection of client */
643static int smc_connect_ism(struct smc_sock *smc,
644 struct smc_clc_msg_accept_confirm *aclc,
645 struct smcd_dev *ismdev)
646{
647 int local_contact = SMC_FIRST_CONTACT;
648 int rc = 0;
649
Hans Wippel72a36a82019-02-07 15:56:18 +0100650 /* there is only one lgr role for SMC-D; use server lock */
651 mutex_lock(&smc_server_lgr_pending);
Karsten Graulee05ff72018-11-20 16:46:40 +0100652 local_contact = smc_conn_create(smc, true, aclc->hdr.flag, NULL, 0, 0,
Hans Wippel41349842018-06-28 19:05:11 +0200653 NULL, ismdev, aclc->gid);
Hans Wippel72a36a82019-02-07 15:56:18 +0100654 if (local_contact < 0) {
655 mutex_unlock(&smc_server_lgr_pending);
656 return SMC_CLC_DECL_MEM;
657 }
Hans Wippel41349842018-06-28 19:05:11 +0200658
659 /* Create send and receive buffers */
660 if (smc_buf_create(smc, true))
661 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, local_contact);
662
663 smc_conn_save_peer_info(smc, aclc);
664 smc_close_init(smc);
665 smc_rx_init(smc);
666 smc_tx_init(smc);
667
668 rc = smc_clc_send_confirm(smc);
669 if (rc)
670 return smc_connect_abort(smc, rc, local_contact);
Hans Wippel72a36a82019-02-07 15:56:18 +0100671 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel41349842018-06-28 19:05:11 +0200672
673 smc_copy_sock_settings_to_clc(smc);
674 if (smc->sk.sk_state == SMC_INIT)
675 smc->sk.sk_state = SMC_ACTIVE;
676
677 return 0;
678}
679
Hans Wippel3b2dec22018-05-18 09:34:18 +0200680/* perform steps before actually connecting */
681static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100682{
Hans Wippel41349842018-06-28 19:05:11 +0200683 bool ism_supported = false, rdma_supported = false;
Ursula Brauna046d572017-01-09 16:55:16 +0100684 struct smc_clc_msg_accept_confirm aclc;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200685 struct smc_ib_device *ibdev;
Hans Wippel41349842018-06-28 19:05:11 +0200686 struct smcd_dev *ismdev;
Ursula Braun7005ada2018-07-25 16:35:31 +0200687 u8 gid[SMC_GID_SIZE];
Hans Wippel41349842018-06-28 19:05:11 +0200688 unsigned short vlan;
689 int smc_type;
Ursula Brauna046d572017-01-09 16:55:16 +0100690 int rc = 0;
691 u8 ibport;
692
Ursula Braun51f1de72018-01-26 09:28:48 +0100693 sock_hold(&smc->sk); /* sock put in passive closing */
694
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200695 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +0200696 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200697
Hans Wippel3b2dec22018-05-18 09:34:18 +0200698 /* if peer has not signalled SMC-capability, fall back */
699 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +0200700 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200701
Ursula Brauna046d572017-01-09 16:55:16 +0100702 /* IPSec connections opt out of SMC-R optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200703 if (using_ipsec(smc))
704 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
Ursula Brauna046d572017-01-09 16:55:16 +0100705
Hans Wippel41349842018-06-28 19:05:11 +0200706 /* check for VLAN ID */
707 if (smc_vlan_by_tcpsk(smc->clcsock, &vlan))
708 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_CNFERR);
709
710 /* check if there is an ism device available */
711 if (!smc_check_ism(smc, &ismdev) &&
712 !smc_connect_ism_vlan_setup(smc, ismdev, vlan)) {
713 /* ISM is supported for this connection */
714 ism_supported = true;
715 smc_type = SMC_TYPE_D;
716 }
717
718 /* check if there is a rdma device available */
Ursula Braun7005ada2018-07-25 16:35:31 +0200719 if (!smc_check_rdma(smc, &ibdev, &ibport, vlan, gid)) {
Hans Wippel41349842018-06-28 19:05:11 +0200720 /* RDMA is supported for this connection */
721 rdma_supported = true;
722 if (ism_supported)
723 smc_type = SMC_TYPE_B; /* both */
724 else
725 smc_type = SMC_TYPE_R; /* only RDMA */
726 }
727
728 /* if neither ISM nor RDMA are supported, fallback */
729 if (!rdma_supported && !ism_supported)
Karsten Graul603cc142018-07-25 16:35:32 +0200730 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV);
Ursula Brauna046d572017-01-09 16:55:16 +0100731
Hans Wippel3b2dec22018-05-18 09:34:18 +0200732 /* perform CLC handshake */
Ursula Braun7005ada2018-07-25 16:35:31 +0200733 rc = smc_connect_clc(smc, smc_type, &aclc, ibdev, ibport, gid, ismdev);
Hans Wippel41349842018-06-28 19:05:11 +0200734 if (rc) {
735 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200736 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200737 }
Ursula Brauna046d572017-01-09 16:55:16 +0100738
Hans Wippel41349842018-06-28 19:05:11 +0200739 /* depending on previous steps, connect using rdma or ism */
740 if (rdma_supported && aclc.hdr.path == SMC_TYPE_R)
741 rc = smc_connect_rdma(smc, &aclc, ibdev, ibport);
742 else if (ism_supported && aclc.hdr.path == SMC_TYPE_D)
743 rc = smc_connect_ism(smc, &aclc, ismdev);
744 else
Karsten Graul603cc142018-07-25 16:35:32 +0200745 rc = SMC_CLC_DECL_MODEUNSUPP;
Hans Wippel41349842018-06-28 19:05:11 +0200746 if (rc) {
747 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200748 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200749 }
Ursula Brauna046d572017-01-09 16:55:16 +0100750
Hans Wippel41349842018-06-28 19:05:11 +0200751 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200752 return 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100753}
754
Ursula Braun24ac3a02018-06-27 17:59:50 +0200755static void smc_connect_work(struct work_struct *work)
756{
757 struct smc_sock *smc = container_of(work, struct smc_sock,
758 connect_work);
759 int rc;
760
761 lock_sock(&smc->sk);
762 rc = kernel_connect(smc->clcsock, &smc->connect_info->addr,
763 smc->connect_info->alen, smc->connect_info->flags);
764 if (smc->clcsock->sk->sk_err) {
765 smc->sk.sk_err = smc->clcsock->sk->sk_err;
766 goto out;
767 }
768 if (rc < 0) {
769 smc->sk.sk_err = -rc;
770 goto out;
771 }
772
773 rc = __smc_connect(smc);
774 if (rc < 0)
775 smc->sk.sk_err = -rc;
776
777out:
Ursula Braun648a5a72018-09-18 15:46:34 +0200778 if (smc->sk.sk_err)
779 smc->sk.sk_state_change(&smc->sk);
780 else
781 smc->sk.sk_write_space(&smc->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200782 kfree(smc->connect_info);
783 smc->connect_info = NULL;
784 release_sock(&smc->sk);
785}
786
Ursula Braunac713872017-01-09 16:55:13 +0100787static int smc_connect(struct socket *sock, struct sockaddr *addr,
788 int alen, int flags)
789{
790 struct sock *sk = sock->sk;
791 struct smc_sock *smc;
792 int rc = -EINVAL;
793
794 smc = smc_sk(sk);
795
796 /* separate smc parameter checking to be safe */
797 if (alen < sizeof(addr->sa_family))
798 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100799 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100800 goto out_err;
801
802 lock_sock(sk);
803 switch (sk->sk_state) {
804 default:
805 goto out;
806 case SMC_ACTIVE:
807 rc = -EISCONN;
808 goto out;
809 case SMC_INIT:
810 rc = 0;
811 break;
812 }
813
814 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200815 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200816 if (flags & O_NONBLOCK) {
817 if (smc->connect_info) {
818 rc = -EALREADY;
819 goto out;
820 }
821 smc->connect_info = kzalloc(alen + 2 * sizeof(int), GFP_KERNEL);
822 if (!smc->connect_info) {
823 rc = -ENOMEM;
824 goto out;
825 }
826 smc->connect_info->alen = alen;
827 smc->connect_info->flags = flags ^ O_NONBLOCK;
828 memcpy(&smc->connect_info->addr, addr, alen);
829 schedule_work(&smc->connect_work);
830 rc = -EINPROGRESS;
831 } else {
832 rc = kernel_connect(smc->clcsock, addr, alen, flags);
833 if (rc)
834 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100835
Ursula Braun24ac3a02018-06-27 17:59:50 +0200836 rc = __smc_connect(smc);
837 if (rc < 0)
838 goto out;
839 else
840 rc = 0; /* success cases including fallback */
841 }
Ursula Braunac713872017-01-09 16:55:13 +0100842
843out:
844 release_sock(sk);
845out_err:
846 return rc;
847}
848
849static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
850{
Ursula Braun3163c502018-01-24 10:28:12 +0100851 struct socket *new_clcsock = NULL;
852 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100853 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -0800854 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +0100855
Ursula Braun3163c502018-01-24 10:28:12 +0100856 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100857 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100858 if (!new_sk) {
859 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100860 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100861 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100862 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100863 goto out;
864 }
865 *new_smc = smc_sk(new_sk);
866
Myungho Jung78abe3d2018-12-18 09:02:25 -0800867 mutex_lock(&lsmc->clcsock_release_lock);
868 if (lsmc->clcsock)
869 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
870 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +0100871 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100872 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100873 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100874 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Brauna046d572017-01-09 16:55:16 +0100875 if (new_clcsock)
876 sock_release(new_clcsock);
877 new_sk->sk_state = SMC_CLOSED;
878 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun3163c502018-01-24 10:28:12 +0100879 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100880 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100881 *new_smc = NULL;
882 goto out;
883 }
884
885 (*new_smc)->clcsock = new_clcsock;
886out:
887 return rc;
888}
889
Ursula Brauna046d572017-01-09 16:55:16 +0100890/* add a just created sock to the accept queue of the listen sock as
891 * candidate for a following socket accept call from user space
892 */
893static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
894{
895 struct smc_sock *par = smc_sk(parent);
896
Ursula Braun51f1de72018-01-26 09:28:48 +0100897 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100898 spin_lock(&par->accept_q_lock);
899 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
900 spin_unlock(&par->accept_q_lock);
901 sk_acceptq_added(parent);
902}
903
904/* remove a socket from the accept queue of its parental listening socket */
905static void smc_accept_unlink(struct sock *sk)
906{
907 struct smc_sock *par = smc_sk(sk)->listen_smc;
908
909 spin_lock(&par->accept_q_lock);
910 list_del_init(&smc_sk(sk)->accept_q);
911 spin_unlock(&par->accept_q_lock);
912 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100913 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100914}
915
916/* remove a sock from the accept queue to bind it to a new socket created
917 * for a socket accept call from user space
918 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100919struct sock *smc_accept_dequeue(struct sock *parent,
920 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100921{
922 struct smc_sock *isk, *n;
923 struct sock *new_sk;
924
925 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
926 new_sk = (struct sock *)isk;
927
928 smc_accept_unlink(new_sk);
929 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braun127f4972018-01-26 09:28:49 +0100930 if (isk->clcsock) {
931 sock_release(isk->clcsock);
932 isk->clcsock = NULL;
933 }
Ursula Braun288c83902017-04-10 14:58:04 +0200934 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100935 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +0100936 continue;
937 }
938 if (new_sock)
939 sock_graft(new_sk, new_sock);
940 return new_sk;
941 }
942 return NULL;
943}
944
945/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +0100946void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +0100947{
948 struct smc_sock *smc = smc_sk(sk);
949
Ursula Braunb38d7322017-01-09 16:55:25 +0100950 lock_sock(sk);
951 if (!sk->sk_lingertime)
952 /* wait for peer closing */
953 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100954 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100955 smc_close_active(smc);
Ursula Braun288c83902017-04-10 14:58:04 +0200956 sock_set_flag(sk, SOCK_DEAD);
957 sk->sk_shutdown |= SHUTDOWN_MASK;
958 }
Ursula Brauna046d572017-01-09 16:55:16 +0100959 if (smc->clcsock) {
960 struct socket *tcp;
961
962 tcp = smc->clcsock;
963 smc->clcsock = NULL;
964 sock_release(tcp);
965 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100966 if (smc->use_fallback) {
Ursula Braun51f1de72018-01-26 09:28:48 +0100967 sock_put(sk); /* passive closing */
968 sk->sk_state = SMC_CLOSED;
969 } else {
970 if (sk->sk_state == SMC_CLOSED)
971 smc_conn_free(&smc->conn);
Ursula Braunb38d7322017-01-09 16:55:25 +0100972 }
973 release_sock(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100974 sk->sk_prot->unhash(sk);
975 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +0100976}
977
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100978static int smc_serv_conf_first_link(struct smc_sock *smc)
979{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200980 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100981 struct smc_link_group *lgr = smc->conn.lgr;
982 struct smc_link *link;
983 int rest;
984 int rc;
985
986 link = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun652a1e42017-07-28 13:56:17 +0200987
Karsten Graul44aa81c2018-05-15 17:04:55 +0200988 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200989 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200990
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100991 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200992 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100993 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200994 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100995
996 /* receive CONFIRM LINK response from client over the RoCE fabric */
997 rest = wait_for_completion_interruptible_timeout(
998 &link->llc_confirm_resp,
999 SMC_LLC_WAIT_FIRST_TIME);
1000 if (rest <= 0) {
1001 struct smc_clc_msg_decline dclc;
1002
1003 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001004 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001005 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001006 }
1007
Karsten Graul75d320d2018-03-01 13:51:31 +01001008 if (link->llc_confirm_resp_rc)
1009 return SMC_CLC_DECL_RMBE_EC;
1010
Karsten Graul52bedf32018-03-01 13:51:32 +01001011 /* send ADD LINK request to client over the RoCE fabric */
1012 rc = smc_llc_send_add_link(link,
1013 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +02001014 link->gid, SMC_LLC_REQ);
Karsten Graul52bedf32018-03-01 13:51:32 +01001015 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001016 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +01001017
1018 /* receive ADD LINK response from client over the RoCE fabric */
1019 rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
1020 SMC_LLC_WAIT_TIME);
1021 if (rest <= 0) {
1022 struct smc_clc_msg_decline dclc;
1023
1024 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001025 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001026 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
Karsten Graul52bedf32018-03-01 13:51:32 +01001027 }
1028
Karsten Graul877ae5b2018-05-02 16:56:44 +02001029 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +01001030
Karsten Graul75d320d2018-03-01 13:51:31 +01001031 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001032}
1033
Hans Wippel3b2dec22018-05-18 09:34:18 +02001034/* listen worker: finish */
1035static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001036{
Ursula Brauna046d572017-01-09 16:55:16 +01001037 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001038 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001039
Ursula Braunb38d7322017-01-09 16:55:25 +01001040 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001041 if (lsmc->sk.sk_state == SMC_LISTEN) {
1042 smc_accept_enqueue(&lsmc->sk, newsmcsk);
1043 } else { /* no longer listening */
1044 smc_close_non_accepted(newsmcsk);
1045 }
1046 release_sock(&lsmc->sk);
1047
1048 /* Wake up accept */
1049 lsmc->sk.sk_data_ready(&lsmc->sk);
1050 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001051}
Ursula Brauna046d572017-01-09 16:55:16 +01001052
Hans Wippel3b2dec22018-05-18 09:34:18 +02001053/* listen worker: finish in state connected */
1054static void smc_listen_out_connected(struct smc_sock *new_smc)
1055{
1056 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001057
Hans Wippel3b2dec22018-05-18 09:34:18 +02001058 sk_refcnt_debug_inc(newsmcsk);
1059 if (newsmcsk->sk_state == SMC_INIT)
1060 newsmcsk->sk_state = SMC_ACTIVE;
1061
1062 smc_listen_out(new_smc);
1063}
1064
1065/* listen worker: finish in error state */
1066static void smc_listen_out_err(struct smc_sock *new_smc)
1067{
1068 struct sock *newsmcsk = &new_smc->sk;
1069
Ursula Braun51f1de72018-01-26 09:28:48 +01001070 if (newsmcsk->sk_state == SMC_INIT)
1071 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001072 newsmcsk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001073 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001074
1075 smc_listen_out(new_smc);
1076}
1077
1078/* listen worker: decline and fall back if possible */
1079static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1080 int local_contact)
1081{
1082 /* RDMA setup failed, switch back to TCP */
1083 if (local_contact == SMC_FIRST_CONTACT)
1084 smc_lgr_forget(new_smc->conn.lgr);
1085 if (reason_code < 0) { /* error, no fallback possible */
1086 smc_listen_out_err(new_smc);
1087 return;
1088 }
1089 smc_conn_free(&new_smc->conn);
1090 new_smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001091 new_smc->fallback_rsn = reason_code;
1092 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001093 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1094 smc_listen_out_err(new_smc);
1095 return;
1096 }
1097 }
1098 smc_listen_out_connected(new_smc);
1099}
1100
1101/* listen worker: check prefixes */
1102static int smc_listen_rdma_check(struct smc_sock *new_smc,
1103 struct smc_clc_msg_proposal *pclc)
1104{
1105 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1106 struct socket *newclcsock = new_smc->clcsock;
1107
1108 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1109 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
1110 return SMC_CLC_DECL_CNFERR;
1111
1112 return 0;
1113}
1114
1115/* listen worker: initialize connection and buffers */
1116static int smc_listen_rdma_init(struct smc_sock *new_smc,
1117 struct smc_clc_msg_proposal *pclc,
1118 struct smc_ib_device *ibdev, u8 ibport,
1119 int *local_contact)
1120{
1121 /* allocate connection / link group */
Karsten Graulee05ff72018-11-20 16:46:40 +01001122 *local_contact = smc_conn_create(new_smc, false, 0, ibdev, ibport, 0,
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001123 &pclc->lcl, NULL, 0);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001124 if (*local_contact < 0) {
1125 if (*local_contact == -ENOMEM)
1126 return SMC_CLC_DECL_MEM;/* insufficient memory*/
1127 return SMC_CLC_DECL_INTERR; /* other error */
1128 }
1129
1130 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001131 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001132 return SMC_CLC_DECL_MEM;
1133
1134 return 0;
1135}
1136
Hans Wippel41349842018-06-28 19:05:11 +02001137/* listen worker: initialize connection and buffers for SMC-D */
1138static int smc_listen_ism_init(struct smc_sock *new_smc,
1139 struct smc_clc_msg_proposal *pclc,
1140 struct smcd_dev *ismdev,
1141 int *local_contact)
1142{
1143 struct smc_clc_msg_smcd *pclc_smcd;
1144
1145 pclc_smcd = smc_get_clc_msg_smcd(pclc);
Karsten Graulee05ff72018-11-20 16:46:40 +01001146 *local_contact = smc_conn_create(new_smc, true, 0, NULL, 0, 0, NULL,
Hans Wippel41349842018-06-28 19:05:11 +02001147 ismdev, pclc_smcd->gid);
1148 if (*local_contact < 0) {
1149 if (*local_contact == -ENOMEM)
1150 return SMC_CLC_DECL_MEM;/* insufficient memory*/
1151 return SMC_CLC_DECL_INTERR; /* other error */
1152 }
1153
1154 /* Check if peer can be reached via ISM device */
1155 if (smc_ism_cantalk(new_smc->conn.lgr->peer_gid,
1156 new_smc->conn.lgr->vlan_id,
1157 new_smc->conn.lgr->smcd)) {
1158 if (*local_contact == SMC_FIRST_CONTACT)
1159 smc_lgr_forget(new_smc->conn.lgr);
1160 smc_conn_free(&new_smc->conn);
1161 return SMC_CLC_DECL_CNFERR;
1162 }
1163
1164 /* Create send and receive buffers */
1165 if (smc_buf_create(new_smc, true)) {
1166 if (*local_contact == SMC_FIRST_CONTACT)
1167 smc_lgr_forget(new_smc->conn.lgr);
1168 smc_conn_free(&new_smc->conn);
1169 return SMC_CLC_DECL_MEM;
1170 }
1171
1172 return 0;
1173}
1174
Hans Wippel3b2dec22018-05-18 09:34:18 +02001175/* listen worker: register buffers */
1176static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
1177{
1178 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1179
1180 if (local_contact != SMC_FIRST_CONTACT) {
Karsten Graulc7674c02018-11-22 10:26:43 +01001181 if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true))
1182 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001183 }
1184 smc_rmb_sync_sg_for_device(&new_smc->conn);
1185
1186 return 0;
1187}
1188
1189/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001190static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1191 struct smc_clc_msg_accept_confirm *cclc,
1192 int local_contact)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001193{
1194 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1195 int reason_code = 0;
1196
1197 if (local_contact == SMC_FIRST_CONTACT)
1198 smc_link_save_peer_info(link, cclc);
1199
1200 if (smc_rmb_rtoken_handling(&new_smc->conn, cclc)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001201 reason_code = SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001202 goto decline;
1203 }
1204
1205 if (local_contact == SMC_FIRST_CONTACT) {
1206 if (smc_ib_ready_link(link)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001207 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001208 goto decline;
1209 }
1210 /* QP confirmation over RoCE fabric */
1211 reason_code = smc_serv_conf_first_link(new_smc);
1212 if (reason_code)
1213 goto decline;
1214 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001215 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001216
1217decline:
Hans Wippel3b2dec22018-05-18 09:34:18 +02001218 smc_listen_decline(new_smc, reason_code, local_contact);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001219 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001220}
1221
1222/* setup for RDMA connection of server */
1223static void smc_listen_work(struct work_struct *work)
1224{
1225 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1226 smc_listen_work);
1227 struct socket *newclcsock = new_smc->clcsock;
1228 struct smc_clc_msg_accept_confirm cclc;
1229 struct smc_clc_msg_proposal *pclc;
1230 struct smc_ib_device *ibdev;
Hans Wippel41349842018-06-28 19:05:11 +02001231 bool ism_supported = false;
1232 struct smcd_dev *ismdev;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001233 u8 buf[SMC_CLC_MAX_LEN];
1234 int local_contact = 0;
Ursula Braun7005ada2018-07-25 16:35:31 +02001235 unsigned short vlan;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001236 int reason_code = 0;
1237 int rc = 0;
1238 u8 ibport;
1239
1240 if (new_smc->use_fallback) {
1241 smc_listen_out_connected(new_smc);
1242 return;
1243 }
1244
1245 /* check if peer is smc capable */
1246 if (!tcp_sk(newclcsock->sk)->syn_smc) {
1247 new_smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001248 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001249 smc_listen_out_connected(new_smc);
1250 return;
1251 }
1252
1253 /* do inband token exchange -
1254 * wait for and receive SMC Proposal CLC message
1255 */
1256 pclc = (struct smc_clc_msg_proposal *)&buf;
1257 reason_code = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
Ursula Braun2b59f582018-11-22 10:26:39 +01001258 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001259 if (reason_code) {
1260 smc_listen_decline(new_smc, reason_code, 0);
1261 return;
1262 }
1263
1264 /* IPSec connections opt out of SMC-R optimizations */
1265 if (using_ipsec(new_smc)) {
1266 smc_listen_decline(new_smc, SMC_CLC_DECL_IPSEC, 0);
1267 return;
1268 }
1269
Hans Wippel72a36a82019-02-07 15:56:18 +01001270 mutex_lock(&smc_server_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001271 smc_close_init(new_smc);
1272 smc_rx_init(new_smc);
1273 smc_tx_init(new_smc);
1274
Hans Wippel41349842018-06-28 19:05:11 +02001275 /* check if ISM is available */
1276 if ((pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) &&
1277 !smc_check_ism(new_smc, &ismdev) &&
1278 !smc_listen_ism_init(new_smc, pclc, ismdev, &local_contact)) {
1279 ism_supported = true;
1280 }
1281
Hans Wippel3b2dec22018-05-18 09:34:18 +02001282 /* check if RDMA is available */
Hans Wippel41349842018-06-28 19:05:11 +02001283 if (!ism_supported &&
1284 ((pclc->hdr.path != SMC_TYPE_R && pclc->hdr.path != SMC_TYPE_B) ||
Ursula Braun7005ada2018-07-25 16:35:31 +02001285 smc_vlan_by_tcpsk(new_smc->clcsock, &vlan) ||
1286 smc_check_rdma(new_smc, &ibdev, &ibport, vlan, NULL) ||
Hans Wippel41349842018-06-28 19:05:11 +02001287 smc_listen_rdma_check(new_smc, pclc) ||
1288 smc_listen_rdma_init(new_smc, pclc, ibdev, ibport,
1289 &local_contact) ||
1290 smc_listen_rdma_reg(new_smc, local_contact))) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001291 /* SMC not supported, decline */
Hans Wippel72a36a82019-02-07 15:56:18 +01001292 mutex_unlock(&smc_server_lgr_pending);
Karsten Graul603cc142018-07-25 16:35:32 +02001293 smc_listen_decline(new_smc, SMC_CLC_DECL_MODEUNSUPP,
1294 local_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001295 return;
1296 }
1297
1298 /* send SMC Accept CLC message */
1299 rc = smc_clc_send_accept(new_smc, local_contact);
1300 if (rc) {
Hans Wippel72a36a82019-02-07 15:56:18 +01001301 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001302 smc_listen_decline(new_smc, rc, local_contact);
1303 return;
1304 }
1305
Hans Wippel62c71392019-02-07 15:56:17 +01001306 /* SMC-D does not need this lock any more */
1307 if (ism_supported)
Hans Wippel72a36a82019-02-07 15:56:18 +01001308 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001309
Hans Wippel3b2dec22018-05-18 09:34:18 +02001310 /* receive SMC Confirm CLC message */
1311 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001312 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001313 if (reason_code) {
Hans Wippel62c71392019-02-07 15:56:17 +01001314 if (!ism_supported)
Hans Wippel72a36a82019-02-07 15:56:18 +01001315 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001316 smc_listen_decline(new_smc, reason_code, local_contact);
1317 return;
1318 }
1319
1320 /* finish worker */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001321 if (!ism_supported) {
Hans Wippel62c71392019-02-07 15:56:17 +01001322 rc = smc_listen_rdma_finish(new_smc, &cclc, local_contact);
Hans Wippel72a36a82019-02-07 15:56:18 +01001323 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001324 if (rc)
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001325 return;
1326 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001327 smc_conn_save_peer_info(new_smc, &cclc);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001328 smc_listen_out_connected(new_smc);
Ursula Brauna046d572017-01-09 16:55:16 +01001329}
1330
1331static void smc_tcp_listen_work(struct work_struct *work)
1332{
1333 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1334 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001335 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001336 struct smc_sock *new_smc;
1337 int rc = 0;
1338
Ursula Braun3163c502018-01-24 10:28:12 +01001339 lock_sock(lsk);
1340 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001341 rc = smc_clcsock_accept(lsmc, &new_smc);
1342 if (rc)
1343 goto out;
1344 if (!new_smc)
1345 continue;
1346
1347 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001348 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001349 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001350 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001351 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1352 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001353 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1354 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001355 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1356 if (!schedule_work(&new_smc->smc_listen_work))
1357 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001358 }
1359
1360out:
Ursula Braun3163c502018-01-24 10:28:12 +01001361 release_sock(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001362 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +01001363}
1364
Ursula Braunac713872017-01-09 16:55:13 +01001365static int smc_listen(struct socket *sock, int backlog)
1366{
1367 struct sock *sk = sock->sk;
1368 struct smc_sock *smc;
1369 int rc;
1370
1371 smc = smc_sk(sk);
1372 lock_sock(sk);
1373
1374 rc = -EINVAL;
1375 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
1376 goto out;
1377
1378 rc = 0;
1379 if (sk->sk_state == SMC_LISTEN) {
1380 sk->sk_max_ack_backlog = backlog;
1381 goto out;
1382 }
1383 /* some socket options are handled in core, so we could not apply
1384 * them to the clc socket -- copy smc socket options to clc socket
1385 */
1386 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001387 if (!smc->use_fallback)
1388 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001389
1390 rc = kernel_listen(smc->clcsock, backlog);
1391 if (rc)
1392 goto out;
1393 sk->sk_max_ack_backlog = backlog;
1394 sk->sk_ack_backlog = 0;
1395 sk->sk_state = SMC_LISTEN;
Ursula Braun51f1de72018-01-26 09:28:48 +01001396 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1397 if (!schedule_work(&smc->tcp_listen_work))
1398 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001399
1400out:
1401 release_sock(sk);
1402 return rc;
1403}
1404
1405static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001406 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001407{
Ursula Brauna046d572017-01-09 16:55:16 +01001408 struct sock *sk = sock->sk, *nsk;
1409 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001410 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001411 long timeo;
1412 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001413
1414 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001415 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001416 lock_sock(sk);
1417
1418 if (lsmc->sk.sk_state != SMC_LISTEN) {
1419 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001420 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001421 goto out;
1422 }
1423
Ursula Brauna046d572017-01-09 16:55:16 +01001424 /* Wait for an incoming connection */
1425 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1426 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1427 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1428 set_current_state(TASK_INTERRUPTIBLE);
1429 if (!timeo) {
1430 rc = -EAGAIN;
1431 break;
1432 }
1433 release_sock(sk);
1434 timeo = schedule_timeout(timeo);
1435 /* wakeup by sk_data_ready in smc_listen_work() */
1436 sched_annotate_sleep();
1437 lock_sock(sk);
1438 if (signal_pending(current)) {
1439 rc = sock_intr_errno(timeo);
1440 break;
1441 }
1442 }
1443 set_current_state(TASK_RUNNING);
1444 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001445
Ursula Brauna046d572017-01-09 16:55:16 +01001446 if (!rc)
1447 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001448 release_sock(sk);
1449 if (rc)
1450 goto out;
1451
1452 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1453 /* wait till data arrives on the socket */
1454 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1455 MSEC_PER_SEC);
1456 if (smc_sk(nsk)->use_fallback) {
1457 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1458
1459 lock_sock(clcsk);
1460 if (skb_queue_empty(&clcsk->sk_receive_queue))
1461 sk_wait_data(clcsk, &timeo, NULL);
1462 release_sock(clcsk);
1463 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1464 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001465 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001466 release_sock(nsk);
1467 }
1468 }
Ursula Braunac713872017-01-09 16:55:13 +01001469
1470out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001471 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001472 return rc;
1473}
1474
1475static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001476 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001477{
1478 struct smc_sock *smc;
1479
Ursula Braunb38d7322017-01-09 16:55:25 +01001480 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1481 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001482 return -ENOTCONN;
1483
1484 smc = smc_sk(sock->sk);
1485
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001486 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001487}
1488
1489static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1490{
1491 struct sock *sk = sock->sk;
1492 struct smc_sock *smc;
1493 int rc = -EPIPE;
1494
1495 smc = smc_sk(sk);
1496 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001497 if ((sk->sk_state != SMC_ACTIVE) &&
1498 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1499 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001500 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001501
1502 if (msg->msg_flags & MSG_FASTOPEN) {
1503 if (sk->sk_state == SMC_INIT) {
1504 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001505 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001506 } else {
1507 rc = -EINVAL;
1508 goto out;
1509 }
1510 }
1511
Ursula Braunac713872017-01-09 16:55:13 +01001512 if (smc->use_fallback)
1513 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1514 else
Ursula Braune6727f32017-01-09 16:55:23 +01001515 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001516out:
1517 release_sock(sk);
1518 return rc;
1519}
1520
1521static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1522 int flags)
1523{
1524 struct sock *sk = sock->sk;
1525 struct smc_sock *smc;
1526 int rc = -ENOTCONN;
1527
1528 smc = smc_sk(sk);
1529 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001530 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1531 /* socket was connected before, no more data to read */
1532 rc = 0;
1533 goto out;
1534 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001535 if ((sk->sk_state == SMC_INIT) ||
1536 (sk->sk_state == SMC_LISTEN) ||
1537 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001538 goto out;
1539
Ursula Braunb38d7322017-01-09 16:55:25 +01001540 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1541 rc = 0;
1542 goto out;
1543 }
1544
Stefan Raspl9014db22018-05-03 18:12:39 +02001545 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001546 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001547 } else {
1548 msg->msg_namelen = 0;
1549 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1550 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001551
Ursula Braunac713872017-01-09 16:55:13 +01001552out:
1553 release_sock(sk);
1554 return rc;
1555}
1556
Al Viroade994f2017-07-03 00:01:49 -04001557static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001558{
Ursula Braun8dce2782018-01-26 09:28:47 +01001559 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001560 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001561
Ursula Braun8dce2782018-01-26 09:28:47 +01001562 spin_lock(&isk->accept_q_lock);
1563 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001564 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001565 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001566
Ursula Braun8dce2782018-01-26 09:28:47 +01001567 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001568}
1569
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001570static __poll_t smc_poll(struct file *file, struct socket *sock,
1571 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01001572{
1573 struct sock *sk = sock->sk;
Al Viroe6c8adc2017-07-03 22:25:56 -04001574 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001575 struct smc_sock *smc;
1576
Ursula Braun8dce2782018-01-26 09:28:47 +01001577 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001578 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001579
Ursula Braunac713872017-01-09 16:55:13 +01001580 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02001581 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01001582 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001583 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02001584 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001585 if (sk->sk_err)
Ursula Braun784813a2018-05-02 16:53:56 +02001586 mask |= EPOLLERR;
Ursula Braunac713872017-01-09 16:55:13 +01001587 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07001588 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02001589 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01001590 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001591 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001592 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1593 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001594 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001595 if (sk->sk_state == SMC_LISTEN) {
1596 /* woken up by sk_data_ready in smc_listen_work() */
1597 mask = smc_accept_poll(sk);
1598 } else {
1599 if (atomic_read(&smc->conn.sndbuf_space) ||
1600 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001601 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001602 } else {
1603 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1604 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1605 }
1606 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001607 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001608 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001609 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001610 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001611 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02001612 if (smc->conn.urg_state == SMC_URG_VALID)
1613 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01001614 }
Ursula Braunac713872017-01-09 16:55:13 +01001615 }
1616
1617 return mask;
1618}
1619
1620static int smc_shutdown(struct socket *sock, int how)
1621{
1622 struct sock *sk = sock->sk;
1623 struct smc_sock *smc;
1624 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001625 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001626
1627 smc = smc_sk(sk);
1628
1629 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001630 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001631
1632 lock_sock(sk);
1633
1634 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02001635 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01001636 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1637 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1638 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1639 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1640 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001641 goto out;
1642 if (smc->use_fallback) {
1643 rc = kernel_sock_shutdown(smc->clcsock, how);
1644 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1645 if (sk->sk_shutdown == SHUTDOWN_MASK)
1646 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001647 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001648 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001649 switch (how) {
1650 case SHUT_RDWR: /* shutdown in both directions */
1651 rc = smc_close_active(smc);
1652 break;
1653 case SHUT_WR:
1654 rc = smc_close_shutdown_write(smc);
1655 break;
1656 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001657 rc = 0;
1658 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001659 break;
1660 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001661 if (smc->clcsock)
1662 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001663 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1664 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001665
1666out:
1667 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001668 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001669}
1670
1671static int smc_setsockopt(struct socket *sock, int level, int optname,
1672 char __user *optval, unsigned int optlen)
1673{
1674 struct sock *sk = sock->sk;
1675 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001676 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001677
1678 smc = smc_sk(sk);
1679
1680 /* generic setsockopts reaching us here always apply to the
1681 * CLC socket
1682 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001683 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1684 optval, optlen);
1685 if (smc->clcsock->sk->sk_err) {
1686 sk->sk_err = smc->clcsock->sk->sk_err;
1687 sk->sk_error_report(sk);
1688 }
1689 if (rc)
1690 return rc;
1691
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001692 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00001693 return -EINVAL;
Ursula Braunac0107e2018-07-18 15:22:50 +02001694 if (get_user(val, (int __user *)optval))
1695 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001696
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001697 lock_sock(sk);
1698 switch (optname) {
1699 case TCP_ULP:
1700 case TCP_FASTOPEN:
1701 case TCP_FASTOPEN_CONNECT:
1702 case TCP_FASTOPEN_KEY:
1703 case TCP_FASTOPEN_NO_COOKIE:
1704 /* option not supported by SMC */
1705 if (sk->sk_state == SMC_INIT) {
1706 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001707 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001708 } else {
1709 if (!smc->use_fallback)
1710 rc = -EINVAL;
1711 }
1712 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001713 case TCP_NODELAY:
1714 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001715 if (val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001716 mod_delayed_work(system_wq, &smc->conn.tx_work,
1717 0);
1718 }
1719 break;
1720 case TCP_CORK:
1721 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001722 if (!val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001723 mod_delayed_work(system_wq, &smc->conn.tx_work,
1724 0);
1725 }
1726 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001727 case TCP_DEFER_ACCEPT:
1728 smc->sockopt_defer_accept = val;
1729 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001730 default:
1731 break;
1732 }
1733 release_sock(sk);
1734
1735 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001736}
1737
1738static int smc_getsockopt(struct socket *sock, int level, int optname,
1739 char __user *optval, int __user *optlen)
1740{
1741 struct smc_sock *smc;
1742
1743 smc = smc_sk(sock->sk);
1744 /* socket options apply to the CLC socket */
1745 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1746 optval, optlen);
1747}
1748
1749static int smc_ioctl(struct socket *sock, unsigned int cmd,
1750 unsigned long arg)
1751{
Stefan Rasplde8474e2018-05-23 16:38:11 +02001752 union smc_host_cursor cons, urg;
1753 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01001754 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001755 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001756
1757 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001758 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02001759 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02001760 if (smc->use_fallback) {
1761 if (!smc->clcsock) {
1762 release_sock(&smc->sk);
1763 return -EBADF;
1764 }
1765 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1766 release_sock(&smc->sk);
1767 return answ;
1768 }
Ursula Braun9b67e262018-05-02 16:56:46 +02001769 switch (cmd) {
1770 case SIOCINQ: /* same as FIONREAD */
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 = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02001780 break;
1781 case SIOCOUTQ:
1782 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02001783 if (smc->sk.sk_state == SMC_LISTEN) {
1784 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001785 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001786 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001787 if (smc->sk.sk_state == SMC_INIT ||
1788 smc->sk.sk_state == SMC_CLOSED)
1789 answ = 0;
1790 else
1791 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02001792 atomic_read(&smc->conn.sndbuf_space);
1793 break;
1794 case SIOCOUTQNSD:
1795 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02001796 if (smc->sk.sk_state == SMC_LISTEN) {
1797 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001798 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001799 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001800 if (smc->sk.sk_state == SMC_INIT ||
1801 smc->sk.sk_state == SMC_CLOSED)
1802 answ = 0;
1803 else
1804 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02001805 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02001806 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02001807 if (smc->sk.sk_state == SMC_LISTEN) {
1808 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001809 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001810 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001811 if (smc->sk.sk_state == SMC_INIT ||
1812 smc->sk.sk_state == SMC_CLOSED) {
1813 answ = 0;
1814 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02001815 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1816 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001817 answ = smc_curs_diff(conn->rmb_desc->len,
1818 &cons, &urg) == 1;
1819 }
1820 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02001821 default:
Ursula Braun1992d992018-07-16 13:56:52 +02001822 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001823 return -ENOIOCTLCMD;
1824 }
Ursula Braun1992d992018-07-16 13:56:52 +02001825 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001826
1827 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001828}
1829
1830static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1831 int offset, size_t size, int flags)
1832{
1833 struct sock *sk = sock->sk;
1834 struct smc_sock *smc;
1835 int rc = -EPIPE;
1836
1837 smc = smc_sk(sk);
1838 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001839 if (sk->sk_state != SMC_ACTIVE) {
1840 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001841 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001842 }
1843 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001844 if (smc->use_fallback)
1845 rc = kernel_sendpage(smc->clcsock, page, offset,
1846 size, flags);
1847 else
1848 rc = sock_no_sendpage(sock, page, offset, size, flags);
1849
1850out:
Ursula Braunac713872017-01-09 16:55:13 +01001851 return rc;
1852}
1853
Stefan Raspl9014db22018-05-03 18:12:39 +02001854/* Map the affected portions of the rmbe into an spd, note the number of bytes
1855 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1856 * updates till whenever a respective page has been fully processed.
1857 * Note that subsequent recv() calls have to wait till all splice() processing
1858 * completed.
1859 */
Ursula Braunac713872017-01-09 16:55:13 +01001860static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1861 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001862 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001863{
1864 struct sock *sk = sock->sk;
1865 struct smc_sock *smc;
1866 int rc = -ENOTCONN;
1867
1868 smc = smc_sk(sk);
1869 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001870 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1871 /* socket was connected before, no more data to read */
1872 rc = 0;
1873 goto out;
1874 }
Stefan Raspl9014db22018-05-03 18:12:39 +02001875 if (sk->sk_state == SMC_INIT ||
1876 sk->sk_state == SMC_LISTEN ||
1877 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001878 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001879
1880 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1881 rc = 0;
1882 goto out;
1883 }
1884
Ursula Braunac713872017-01-09 16:55:13 +01001885 if (smc->use_fallback) {
1886 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1887 pipe, len, flags);
1888 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001889 if (*ppos) {
1890 rc = -ESPIPE;
1891 goto out;
1892 }
1893 if (flags & SPLICE_F_NONBLOCK)
1894 flags = MSG_DONTWAIT;
1895 else
1896 flags = 0;
1897 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001898 }
1899out:
1900 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001901
Ursula Braunac713872017-01-09 16:55:13 +01001902 return rc;
1903}
1904
1905/* must look like tcp */
1906static const struct proto_ops smc_sock_ops = {
1907 .family = PF_SMC,
1908 .owner = THIS_MODULE,
1909 .release = smc_release,
1910 .bind = smc_bind,
1911 .connect = smc_connect,
1912 .socketpair = sock_no_socketpair,
1913 .accept = smc_accept,
1914 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001915 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01001916 .ioctl = smc_ioctl,
1917 .listen = smc_listen,
1918 .shutdown = smc_shutdown,
1919 .setsockopt = smc_setsockopt,
1920 .getsockopt = smc_getsockopt,
1921 .sendmsg = smc_sendmsg,
1922 .recvmsg = smc_recvmsg,
1923 .mmap = sock_no_mmap,
1924 .sendpage = smc_sendpage,
1925 .splice_read = smc_splice_read,
1926};
1927
1928static int smc_create(struct net *net, struct socket *sock, int protocol,
1929 int kern)
1930{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001931 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001932 struct smc_sock *smc;
1933 struct sock *sk;
1934 int rc;
1935
1936 rc = -ESOCKTNOSUPPORT;
1937 if (sock->type != SOCK_STREAM)
1938 goto out;
1939
1940 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001941 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01001942 goto out;
1943
1944 rc = -ENOBUFS;
1945 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001946 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001947 if (!sk)
1948 goto out;
1949
1950 /* create internal TCP socket for CLC handshake and fallback */
1951 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001952 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02001953 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001954 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1955 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01001956 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01001957 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01001958 goto out;
1959 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01001960 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1961 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01001962
1963out:
1964 return rc;
1965}
1966
1967static const struct net_proto_family smc_sock_family_ops = {
1968 .family = PF_SMC,
1969 .owner = THIS_MODULE,
1970 .create = smc_create,
1971};
1972
Hans Wippel64e28b52019-02-21 13:01:02 +01001973unsigned int smc_net_id;
1974
1975static __net_init int smc_net_init(struct net *net)
1976{
1977 return smc_pnet_net_init(net);
1978}
1979
1980static void __net_exit smc_net_exit(struct net *net)
1981{
1982 smc_pnet_net_exit(net);
1983}
1984
1985static struct pernet_operations smc_net_ops = {
1986 .init = smc_net_init,
1987 .exit = smc_net_exit,
1988 .id = &smc_net_id,
1989 .size = sizeof(struct smc_net),
1990};
1991
Ursula Braunac713872017-01-09 16:55:13 +01001992static int __init smc_init(void)
1993{
1994 int rc;
1995
Hans Wippel64e28b52019-02-21 13:01:02 +01001996 rc = register_pernet_subsys(&smc_net_ops);
1997 if (rc)
1998 return rc;
1999
Thomas Richter6812baa2017-01-09 16:55:15 +01002000 rc = smc_pnet_init();
2001 if (rc)
2002 return rc;
2003
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002004 rc = smc_llc_init();
2005 if (rc) {
2006 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
2007 goto out_pnet;
2008 }
2009
Ursula Braun5f083182017-01-09 16:55:22 +01002010 rc = smc_cdc_init();
2011 if (rc) {
2012 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
2013 goto out_pnet;
2014 }
2015
Ursula Braunac713872017-01-09 16:55:13 +01002016 rc = proto_register(&smc_proto, 1);
2017 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01002018 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Thomas Richter6812baa2017-01-09 16:55:15 +01002019 goto out_pnet;
Ursula Braunac713872017-01-09 16:55:13 +01002020 }
2021
Karsten Graulaaa4d332018-03-16 15:06:41 +01002022 rc = proto_register(&smc_proto6, 1);
2023 if (rc) {
2024 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2025 goto out_proto;
2026 }
2027
Ursula Braunac713872017-01-09 16:55:13 +01002028 rc = sock_register(&smc_sock_family_ops);
2029 if (rc) {
2030 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002031 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01002032 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01002033 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002034 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01002035
Ursula Brauna4cf0442017-01-09 16:55:14 +01002036 rc = smc_ib_register_client();
2037 if (rc) {
2038 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2039 goto out_sock;
2040 }
2041
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002042 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002043 return 0;
2044
Ursula Brauna4cf0442017-01-09 16:55:14 +01002045out_sock:
2046 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002047out_proto6:
2048 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002049out_proto:
2050 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002051out_pnet:
2052 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01002053 return rc;
2054}
2055
2056static void __exit smc_exit(void)
2057{
Hans Wippel9fda3512018-05-18 09:34:11 +02002058 smc_core_exit();
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002059 static_branch_disable(&tcp_have_smc);
Ursula Brauna4cf0442017-01-09 16:55:14 +01002060 smc_ib_unregister_client();
Ursula Braunac713872017-01-09 16:55:13 +01002061 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002062 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002063 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002064 smc_pnet_exit();
Hans Wippel64e28b52019-02-21 13:01:02 +01002065 unregister_pernet_subsys(&smc_net_ops);
Ursula Braunac713872017-01-09 16:55:13 +01002066}
2067
2068module_init(smc_init);
2069module_exit(smc_exit);
2070
2071MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2072MODULE_DESCRIPTION("smc socket address family");
2073MODULE_LICENSE("GPL");
2074MODULE_ALIAS_NETPROTO(PF_SMC);