blob: 369870b0ef790e2fb26c6f4d906c5f7de71aade1 [file] [log] [blame]
Ursula Braunac713872017-01-09 16:55:13 +01001/*
2 * Shared Memory Communications over RDMA (SMC-R) and RoCE
3 *
4 * AF_SMC protocol family socket handler keeping the AF_INET sock address type
5 * applies to SOCK_STREAM sockets only
6 * offers an alternative communication option for TCP-protocol sockets
7 * applicable with RoCE-cards only
8 *
Ursula Brauna046d572017-01-09 16:55:16 +01009 * Initial restrictions:
Ursula Brauna046d572017-01-09 16:55:16 +010010 * - support for alternate links postponed
Ursula Brauna046d572017-01-09 16:55:16 +010011 *
Karsten Graulaaa4d332018-03-16 15:06:41 +010012 * Copyright IBM Corp. 2016, 2018
Ursula Braunac713872017-01-09 16:55:13 +010013 *
14 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
15 * based on prototype from Frank Blaschka
16 */
17
18#define KMSG_COMPONENT "smc"
19#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
20
21#include <linux/module.h>
22#include <linux/socket.h>
Ursula Brauna046d572017-01-09 16:55:16 +010023#include <linux/workqueue.h>
Ursula Braun5f083182017-01-09 16:55:22 +010024#include <linux/in.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010025#include <linux/sched/signal.h>
Hans Wippel41349842018-06-28 19:05:11 +020026#include <linux/if_vlan.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010027
Ursula Braunac713872017-01-09 16:55:13 +010028#include <net/sock.h>
Ursula Brauna046d572017-01-09 16:55:16 +010029#include <net/tcp.h>
Ursula Braunf16a7dd2017-01-09 16:55:26 +010030#include <net/smc.h>
Ursula Braun9b67e262018-05-02 16:56:46 +020031#include <asm/ioctls.h>
Ursula Braunac713872017-01-09 16:55:13 +010032
33#include "smc.h"
Ursula Brauna046d572017-01-09 16:55:16 +010034#include "smc_clc.h"
Ursula Braun9bf9abe2017-01-09 16:55:21 +010035#include "smc_llc.h"
Ursula Braun5f083182017-01-09 16:55:22 +010036#include "smc_cdc.h"
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010037#include "smc_core.h"
Ursula Brauna4cf0442017-01-09 16:55:14 +010038#include "smc_ib.h"
Hans Wippel41349842018-06-28 19:05:11 +020039#include "smc_ism.h"
Thomas Richter6812baa2017-01-09 16:55:15 +010040#include "smc_pnet.h"
Ursula Braune6727f32017-01-09 16:55:23 +010041#include "smc_tx.h"
Ursula Braun952310c2017-01-09 16:55:24 +010042#include "smc_rx.h"
Ursula Braunb38d7322017-01-09 16:55:25 +010043#include "smc_close.h"
Ursula Braunac713872017-01-09 16:55:13 +010044
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010045static DEFINE_MUTEX(smc_create_lgr_pending); /* serialize link group
46 * creation
47 */
48
Ursula Brauna046d572017-01-09 16:55:16 +010049static void smc_tcp_listen_work(struct work_struct *);
Ursula Braun24ac3a02018-06-27 17:59:50 +020050static void smc_connect_work(struct work_struct *);
Ursula Brauna046d572017-01-09 16:55:16 +010051
Ursula Braunac713872017-01-09 16:55:13 +010052static void smc_set_keepalive(struct sock *sk, int val)
53{
54 struct smc_sock *smc = smc_sk(sk);
55
56 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
57}
58
Ursula Braunf16a7dd2017-01-09 16:55:26 +010059static struct smc_hashinfo smc_v4_hashinfo = {
60 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
61};
62
Karsten Graulaaa4d332018-03-16 15:06:41 +010063static struct smc_hashinfo smc_v6_hashinfo = {
64 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
65};
66
Ursula Braunf16a7dd2017-01-09 16:55:26 +010067int smc_hash_sk(struct sock *sk)
68{
69 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
70 struct hlist_head *head;
71
72 head = &h->ht;
73
74 write_lock_bh(&h->lock);
75 sk_add_node(sk, head);
76 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
77 write_unlock_bh(&h->lock);
78
79 return 0;
80}
81EXPORT_SYMBOL_GPL(smc_hash_sk);
82
83void smc_unhash_sk(struct sock *sk)
84{
85 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
86
87 write_lock_bh(&h->lock);
88 if (sk_del_node_init(sk))
89 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
90 write_unlock_bh(&h->lock);
91}
92EXPORT_SYMBOL_GPL(smc_unhash_sk);
93
94struct proto smc_proto = {
Ursula Braunac713872017-01-09 16:55:13 +010095 .name = "SMC",
96 .owner = THIS_MODULE,
97 .keepalive = smc_set_keepalive,
Ursula Braunf16a7dd2017-01-09 16:55:26 +010098 .hash = smc_hash_sk,
99 .unhash = smc_unhash_sk,
Ursula Braunac713872017-01-09 16:55:13 +0100100 .obj_size = sizeof(struct smc_sock),
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100101 .h.smc_hash = &smc_v4_hashinfo,
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800102 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Ursula Braunac713872017-01-09 16:55:13 +0100103};
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100104EXPORT_SYMBOL_GPL(smc_proto);
Ursula Braunac713872017-01-09 16:55:13 +0100105
Karsten Graulaaa4d332018-03-16 15:06:41 +0100106struct proto smc_proto6 = {
107 .name = "SMC6",
108 .owner = THIS_MODULE,
109 .keepalive = smc_set_keepalive,
110 .hash = smc_hash_sk,
111 .unhash = smc_unhash_sk,
112 .obj_size = sizeof(struct smc_sock),
113 .h.smc_hash = &smc_v6_hashinfo,
114 .slab_flags = SLAB_TYPESAFE_BY_RCU,
115};
116EXPORT_SYMBOL_GPL(smc_proto6);
117
Ursula Braunac713872017-01-09 16:55:13 +0100118static int smc_release(struct socket *sock)
119{
120 struct sock *sk = sock->sk;
121 struct smc_sock *smc;
Ursula Braunb38d7322017-01-09 16:55:25 +0100122 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +0100123
124 if (!sk)
125 goto out;
126
127 smc = smc_sk(sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200128
129 /* cleanup for a dangling non-blocking connect */
Hans Wippelf07920a2018-11-20 16:46:39 +0100130 if (smc->connect_info && sk->sk_state == SMC_INIT)
131 tcp_abort(smc->clcsock->sk, ECONNABORTED);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200132 flush_work(&smc->connect_work);
133 kfree(smc->connect_info);
134 smc->connect_info = NULL;
135
Ursula Braunb38d7322017-01-09 16:55:25 +0100136 if (sk->sk_state == SMC_LISTEN)
137 /* smc_close_non_accepted() is called and acquires
138 * sock lock for child sockets again
139 */
140 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
141 else
142 lock_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +0100143
Ursula Braun51f1de72018-01-26 09:28:48 +0100144 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100145 rc = smc_close_active(smc);
146 sock_set_flag(sk, SOCK_DEAD);
147 sk->sk_shutdown |= SHUTDOWN_MASK;
148 }
Cong Wang26d92e92019-01-05 23:45:26 -0800149
150 sk->sk_prot->unhash(sk);
151
Ursula Braunac713872017-01-09 16:55:13 +0100152 if (smc->clcsock) {
Myungho Jung78abe3d2018-12-18 09:02:25 -0800153 if (smc->use_fallback && sk->sk_state == SMC_LISTEN) {
154 /* wake up clcsock accept */
155 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
156 }
157 mutex_lock(&smc->clcsock_release_lock);
Ursula Braunac713872017-01-09 16:55:13 +0100158 sock_release(smc->clcsock);
159 smc->clcsock = NULL;
Myungho Jung78abe3d2018-12-18 09:02:25 -0800160 mutex_unlock(&smc->clcsock_release_lock);
Ursula Braunac713872017-01-09 16:55:13 +0100161 }
Ursula Braun51f1de72018-01-26 09:28:48 +0100162 if (smc->use_fallback) {
Ursula Braune1bbdd52018-07-05 16:15:30 +0200163 if (sk->sk_state != SMC_LISTEN && sk->sk_state != SMC_INIT)
164 sock_put(sk); /* passive closing */
Ursula Braun51f1de72018-01-26 09:28:48 +0100165 sk->sk_state = SMC_CLOSED;
166 sk->sk_state_change(sk);
167 }
Ursula Braunac713872017-01-09 16:55:13 +0100168
169 /* detach socket */
170 sock_orphan(sk);
171 sock->sk = NULL;
Ursula Braun51f1de72018-01-26 09:28:48 +0100172 if (!smc->use_fallback && sk->sk_state == SMC_CLOSED)
Ursula Braunb38d7322017-01-09 16:55:25 +0100173 smc_conn_free(&smc->conn);
Ursula Braunac713872017-01-09 16:55:13 +0100174 release_sock(sk);
175
Ursula Braun51f1de72018-01-26 09:28:48 +0100176 sock_put(sk); /* final sock_put */
Ursula Braunac713872017-01-09 16:55:13 +0100177out:
Ursula Braunb38d7322017-01-09 16:55:25 +0100178 return rc;
Ursula Braunac713872017-01-09 16:55:13 +0100179}
180
181static void smc_destruct(struct sock *sk)
182{
183 if (sk->sk_state != SMC_CLOSED)
184 return;
185 if (!sock_flag(sk, SOCK_DEAD))
186 return;
187
188 sk_refcnt_debug_dec(sk);
189}
190
Karsten Graulaaa4d332018-03-16 15:06:41 +0100191static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
192 int protocol)
Ursula Braunac713872017-01-09 16:55:13 +0100193{
194 struct smc_sock *smc;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100195 struct proto *prot;
Ursula Braunac713872017-01-09 16:55:13 +0100196 struct sock *sk;
197
Karsten Graulaaa4d332018-03-16 15:06:41 +0100198 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
199 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
Ursula Braunac713872017-01-09 16:55:13 +0100200 if (!sk)
201 return NULL;
202
203 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
204 sk->sk_state = SMC_INIT;
205 sk->sk_destruct = smc_destruct;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100206 sk->sk_protocol = protocol;
Ursula Braunac713872017-01-09 16:55:13 +0100207 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100208 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200209 INIT_WORK(&smc->connect_work, smc_connect_work);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700210 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
Ursula Brauna046d572017-01-09 16:55:16 +0100211 INIT_LIST_HEAD(&smc->accept_q);
212 spin_lock_init(&smc->accept_q_lock);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700213 spin_lock_init(&smc->conn.send_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100214 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100215 sk_refcnt_debug_inc(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800216 mutex_init(&smc->clcsock_release_lock);
Ursula Braunac713872017-01-09 16:55:13 +0100217
218 return sk;
219}
220
221static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
222 int addr_len)
223{
224 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
225 struct sock *sk = sock->sk;
226 struct smc_sock *smc;
227 int rc;
228
229 smc = smc_sk(sk);
230
231 /* replicate tests from inet_bind(), to be safe wrt. future changes */
232 rc = -EINVAL;
233 if (addr_len < sizeof(struct sockaddr_in))
234 goto out;
235
236 rc = -EAFNOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100237 if (addr->sin_family != AF_INET &&
238 addr->sin_family != AF_INET6 &&
239 addr->sin_family != AF_UNSPEC)
240 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100241 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
Karsten Graulaaa4d332018-03-16 15:06:41 +0100242 if (addr->sin_family == AF_UNSPEC &&
243 addr->sin_addr.s_addr != htonl(INADDR_ANY))
Ursula Braunac713872017-01-09 16:55:13 +0100244 goto out;
245
246 lock_sock(sk);
247
248 /* Check if socket is already active */
249 rc = -EINVAL;
250 if (sk->sk_state != SMC_INIT)
251 goto out_rel;
252
253 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
254 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
255
256out_rel:
257 release_sock(sk);
258out:
259 return rc;
260}
261
262static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
263 unsigned long mask)
264{
265 /* options we don't get control via setsockopt for */
266 nsk->sk_type = osk->sk_type;
267 nsk->sk_sndbuf = osk->sk_sndbuf;
268 nsk->sk_rcvbuf = osk->sk_rcvbuf;
269 nsk->sk_sndtimeo = osk->sk_sndtimeo;
270 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
271 nsk->sk_mark = osk->sk_mark;
272 nsk->sk_priority = osk->sk_priority;
273 nsk->sk_rcvlowat = osk->sk_rcvlowat;
274 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
275 nsk->sk_err = osk->sk_err;
276
277 nsk->sk_flags &= ~mask;
278 nsk->sk_flags |= osk->sk_flags & mask;
279}
280
281#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
282 (1UL << SOCK_KEEPOPEN) | \
283 (1UL << SOCK_LINGER) | \
284 (1UL << SOCK_BROADCAST) | \
285 (1UL << SOCK_TIMESTAMP) | \
286 (1UL << SOCK_DBG) | \
287 (1UL << SOCK_RCVTSTAMP) | \
288 (1UL << SOCK_RCVTSTAMPNS) | \
289 (1UL << SOCK_LOCALROUTE) | \
290 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
291 (1UL << SOCK_RXQ_OVFL) | \
292 (1UL << SOCK_WIFI_STATUS) | \
293 (1UL << SOCK_NOFCS) | \
Deepa Dinamani97184752019-02-02 07:34:51 -0800294 (1UL << SOCK_FILTER_LOCKED) | \
295 (1UL << SOCK_TSTAMP_NEW))
Ursula Braunac713872017-01-09 16:55:13 +0100296/* copy only relevant settings and flags of SOL_SOCKET level from smc to
297 * clc socket (since smc is not called for these options from net/core)
298 */
299static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
300{
301 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
302}
303
304#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
305 (1UL << SOCK_KEEPOPEN) | \
306 (1UL << SOCK_LINGER) | \
307 (1UL << SOCK_DBG))
308/* copy only settings and flags relevant for smc from clc to smc socket */
309static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
310{
311 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
312}
313
Karsten Graulc7674c02018-11-22 10:26:43 +0100314/* register a new rmb, send confirm_rkey msg to register with peer */
Karsten Graul44aa81c2018-05-15 17:04:55 +0200315static int smc_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc,
316 bool conf_rkey)
Karsten Graule63a5f82018-05-03 17:57:37 +0200317{
Karsten Graulc7674c02018-11-22 10:26:43 +0100318 if (!rmb_desc->wr_reg) {
319 /* register memory region for new rmb */
320 if (smc_wr_reg_send(link, rmb_desc->mr_rx[SMC_SINGLE_LINK])) {
321 rmb_desc->regerr = 1;
322 return -EFAULT;
323 }
324 rmb_desc->wr_reg = 1;
Karsten Graula6920d12018-05-03 17:57:38 +0200325 }
Karsten Graul44aa81c2018-05-15 17:04:55 +0200326 if (!conf_rkey)
327 return 0;
328 /* exchange confirm_rkey msg with peer */
329 if (smc_llc_do_confirm_rkey(link, rmb_desc)) {
330 rmb_desc->regerr = 1;
331 return -EFAULT;
332 }
Karsten Graule63a5f82018-05-03 17:57:37 +0200333 return 0;
334}
335
Stefan Raspl0f627122018-03-01 13:51:26 +0100336static int smc_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100337{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200338 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100339 struct smc_link_group *lgr = smc->conn.lgr;
340 struct smc_link *link;
341 int rest;
342 int rc;
343
344 link = &lgr->lnk[SMC_SINGLE_LINK];
345 /* receive CONFIRM LINK request from server over RoCE fabric */
346 rest = wait_for_completion_interruptible_timeout(
347 &link->llc_confirm,
348 SMC_LLC_WAIT_FIRST_TIME);
349 if (rest <= 0) {
350 struct smc_clc_msg_decline dclc;
351
352 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100353 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100354 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100355 }
356
Karsten Graul75d320d2018-03-01 13:51:31 +0100357 if (link->llc_confirm_rc)
358 return SMC_CLC_DECL_RMBE_EC;
359
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100360 rc = smc_ib_modify_qp_rts(link);
361 if (rc)
Karsten Graul603cc142018-07-25 16:35:32 +0200362 return SMC_CLC_DECL_ERR_RDYLNK;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100363
364 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200365
Karsten Graul44aa81c2018-05-15 17:04:55 +0200366 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200367 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200368
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100369 /* send CONFIRM LINK response over RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200370 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100371 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200372 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100373
Karsten Graul52bedf32018-03-01 13:51:32 +0100374 /* receive ADD LINK request from server over RoCE fabric */
375 rest = wait_for_completion_interruptible_timeout(&link->llc_add,
376 SMC_LLC_WAIT_TIME);
377 if (rest <= 0) {
378 struct smc_clc_msg_decline dclc;
379
380 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100381 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100382 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
Karsten Graul52bedf32018-03-01 13:51:32 +0100383 }
384
385 /* send add link reject message, only one link supported for now */
386 rc = smc_llc_send_add_link(link,
387 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +0200388 link->gid, SMC_LLC_RESP);
Karsten Graul52bedf32018-03-01 13:51:32 +0100389 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200390 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +0100391
Karsten Graul877ae5b2018-05-02 16:56:44 +0200392 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +0100393
Karsten Graul75d320d2018-03-01 13:51:31 +0100394 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100395}
396
Hans Wippel41349842018-06-28 19:05:11 +0200397static void smcr_conn_save_peer_info(struct smc_sock *smc,
398 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100399{
Hans Wippel95d8d2632018-05-18 09:34:13 +0200400 int bufsize = smc_uncompress_bufsize(clc->rmbe_size);
401
Hans Wippel92a138e2018-05-18 09:34:12 +0200402 smc->conn.peer_rmbe_idx = clc->rmbe_idx;
Ursula Braun5f083182017-01-09 16:55:22 +0100403 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200404 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100405 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200406 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100407}
408
Hans Wippel41349842018-06-28 19:05:11 +0200409static void smcd_conn_save_peer_info(struct smc_sock *smc,
410 struct smc_clc_msg_accept_confirm *clc)
411{
412 int bufsize = smc_uncompress_bufsize(clc->dmbe_size);
413
414 smc->conn.peer_rmbe_idx = clc->dmbe_idx;
415 smc->conn.peer_token = clc->token;
416 /* msg header takes up space in the buffer */
417 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
418 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
419 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
420}
421
422static void smc_conn_save_peer_info(struct smc_sock *smc,
423 struct smc_clc_msg_accept_confirm *clc)
424{
425 if (smc->conn.lgr->is_smcd)
426 smcd_conn_save_peer_info(smc, clc);
427 else
428 smcr_conn_save_peer_info(smc, clc);
429}
430
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100431static void smc_link_save_peer_info(struct smc_link *link,
432 struct smc_clc_msg_accept_confirm *clc)
433{
434 link->peer_qpn = ntoh24(clc->qpn);
435 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE);
436 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac));
437 link->peer_psn = ntoh24(clc->psn);
438 link->peer_mtu = clc->qp_mtu;
439}
440
Hans Wippel3b2dec22018-05-18 09:34:18 +0200441/* fall back during connect */
Karsten Graul603cc142018-07-25 16:35:32 +0200442static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200443{
444 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +0200445 smc->fallback_rsn = reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200446 smc_copy_sock_settings_to_clc(smc);
447 if (smc->sk.sk_state == SMC_INIT)
448 smc->sk.sk_state = SMC_ACTIVE;
449 return 0;
450}
451
452/* decline and fall back during connect */
453static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code)
454{
455 int rc;
456
Ursula Braune1bbdd52018-07-05 16:15:30 +0200457 if (reason_code < 0) { /* error, fallback is not possible */
458 if (smc->sk.sk_state == SMC_INIT)
459 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200460 return reason_code;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200461 }
Karsten Graul603cc142018-07-25 16:35:32 +0200462 if (reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200463 rc = smc_clc_send_decline(smc, reason_code);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200464 if (rc < 0) {
465 if (smc->sk.sk_state == SMC_INIT)
466 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200467 return rc;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200468 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200469 }
Karsten Graul603cc142018-07-25 16:35:32 +0200470 return smc_connect_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200471}
472
473/* abort connecting */
474static int smc_connect_abort(struct smc_sock *smc, int reason_code,
475 int local_contact)
476{
477 if (local_contact == SMC_FIRST_CONTACT)
478 smc_lgr_forget(smc->conn.lgr);
479 mutex_unlock(&smc_create_lgr_pending);
480 smc_conn_free(&smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200481 return reason_code;
482}
483
484/* check if there is a rdma device available for this connection. */
485/* called for connect and listen */
486static int smc_check_rdma(struct smc_sock *smc, struct smc_ib_device **ibdev,
Ursula Braun7005ada2018-07-25 16:35:31 +0200487 u8 *ibport, unsigned short vlan_id, u8 gid[])
Hans Wippel3b2dec22018-05-18 09:34:18 +0200488{
489 int reason_code = 0;
490
491 /* PNET table look up: search active ib_device and port
492 * within same PNETID that also contains the ethernet device
493 * used for the internal TCP socket
494 */
Ursula Braun7005ada2018-07-25 16:35:31 +0200495 smc_pnet_find_roce_resource(smc->clcsock->sk, ibdev, ibport, vlan_id,
496 gid);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200497 if (!(*ibdev))
498 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */
499
500 return reason_code;
501}
502
Hans Wippel41349842018-06-28 19:05:11 +0200503/* check if there is an ISM device available for this connection. */
504/* called for connect and listen */
505static int smc_check_ism(struct smc_sock *smc, struct smcd_dev **ismdev)
506{
507 /* Find ISM device with same PNETID as connecting interface */
508 smc_pnet_find_ism_resource(smc->clcsock->sk, ismdev);
509 if (!(*ismdev))
510 return SMC_CLC_DECL_CNFERR; /* configuration error */
511 return 0;
512}
513
514/* Check for VLAN ID and register it on ISM device just for CLC handshake */
515static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
516 struct smcd_dev *ismdev,
517 unsigned short vlan_id)
518{
519 if (vlan_id && smc_ism_get_vlan(ismdev, vlan_id))
520 return SMC_CLC_DECL_CNFERR;
521 return 0;
522}
523
524/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
525 * used, the VLAN ID will be registered again during the connection setup.
526 */
527static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd,
528 struct smcd_dev *ismdev,
529 unsigned short vlan_id)
530{
531 if (!is_smcd)
532 return 0;
533 if (vlan_id && smc_ism_put_vlan(ismdev, vlan_id))
534 return SMC_CLC_DECL_CNFERR;
535 return 0;
536}
537
Hans Wippel3b2dec22018-05-18 09:34:18 +0200538/* CLC handshake during connect */
Hans Wippelc758dfd2018-06-28 19:05:09 +0200539static int smc_connect_clc(struct smc_sock *smc, int smc_type,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200540 struct smc_clc_msg_accept_confirm *aclc,
Hans Wippel41349842018-06-28 19:05:11 +0200541 struct smc_ib_device *ibdev, u8 ibport,
Ursula Braun7005ada2018-07-25 16:35:31 +0200542 u8 gid[], struct smcd_dev *ismdev)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200543{
544 int rc = 0;
545
546 /* do inband token exchange */
Ursula Braun7005ada2018-07-25 16:35:31 +0200547 rc = smc_clc_send_proposal(smc, smc_type, ibdev, ibport, gid, ismdev);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200548 if (rc)
549 return rc;
550 /* receive SMC Accept CLC message */
Ursula Braun2b59f582018-11-22 10:26:39 +0100551 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT,
552 CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200553}
554
Ursula Brauna046d572017-01-09 16:55:16 +0100555/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200556static int smc_connect_rdma(struct smc_sock *smc,
557 struct smc_clc_msg_accept_confirm *aclc,
558 struct smc_ib_device *ibdev, u8 ibport)
559{
560 int local_contact = SMC_FIRST_CONTACT;
561 struct smc_link *link;
562 int reason_code = 0;
563
564 mutex_lock(&smc_create_lgr_pending);
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200565 local_contact = smc_conn_create(smc, false, aclc->hdr.flag, ibdev,
Karsten Graulee05ff72018-11-20 16:46:40 +0100566 ibport, ntoh24(aclc->qpn), &aclc->lcl,
567 NULL, 0);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200568 if (local_contact < 0) {
569 if (local_contact == -ENOMEM)
570 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/
571 else if (local_contact == -ENOLINK)
572 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */
573 else
574 reason_code = SMC_CLC_DECL_INTERR; /* other error */
575 return smc_connect_abort(smc, reason_code, 0);
576 }
577 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK];
578
579 smc_conn_save_peer_info(smc, aclc);
580
581 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200582 if (smc_buf_create(smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +0200583 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, local_contact);
584
585 if (local_contact == SMC_FIRST_CONTACT)
586 smc_link_save_peer_info(link, aclc);
587
588 if (smc_rmb_rtoken_handling(&smc->conn, aclc))
Karsten Graul603cc142018-07-25 16:35:32 +0200589 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200590 local_contact);
591
592 smc_close_init(smc);
593 smc_rx_init(smc);
594
595 if (local_contact == SMC_FIRST_CONTACT) {
596 if (smc_ib_ready_link(link))
Karsten Graul603cc142018-07-25 16:35:32 +0200597 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200598 local_contact);
599 } else {
Karsten Graulc7674c02018-11-22 10:26:43 +0100600 if (smc_reg_rmb(link, smc->conn.rmb_desc, true))
Karsten Graul603cc142018-07-25 16:35:32 +0200601 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
Hans Wippel3b2dec22018-05-18 09:34:18 +0200602 local_contact);
603 }
604 smc_rmb_sync_sg_for_device(&smc->conn);
605
606 reason_code = smc_clc_send_confirm(smc);
607 if (reason_code)
608 return smc_connect_abort(smc, reason_code, local_contact);
609
610 smc_tx_init(smc);
611
612 if (local_contact == SMC_FIRST_CONTACT) {
613 /* QP confirmation over RoCE fabric */
614 reason_code = smc_clnt_conf_first_link(smc);
615 if (reason_code)
616 return smc_connect_abort(smc, reason_code,
617 local_contact);
618 }
619 mutex_unlock(&smc_create_lgr_pending);
620
621 smc_copy_sock_settings_to_clc(smc);
622 if (smc->sk.sk_state == SMC_INIT)
623 smc->sk.sk_state = SMC_ACTIVE;
624
625 return 0;
626}
627
Hans Wippel41349842018-06-28 19:05:11 +0200628/* setup for ISM connection of client */
629static int smc_connect_ism(struct smc_sock *smc,
630 struct smc_clc_msg_accept_confirm *aclc,
631 struct smcd_dev *ismdev)
632{
633 int local_contact = SMC_FIRST_CONTACT;
634 int rc = 0;
635
636 mutex_lock(&smc_create_lgr_pending);
Karsten Graulee05ff72018-11-20 16:46:40 +0100637 local_contact = smc_conn_create(smc, true, aclc->hdr.flag, NULL, 0, 0,
Hans Wippel41349842018-06-28 19:05:11 +0200638 NULL, ismdev, aclc->gid);
639 if (local_contact < 0)
640 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, 0);
641
642 /* Create send and receive buffers */
643 if (smc_buf_create(smc, true))
644 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, local_contact);
645
646 smc_conn_save_peer_info(smc, aclc);
647 smc_close_init(smc);
648 smc_rx_init(smc);
649 smc_tx_init(smc);
650
651 rc = smc_clc_send_confirm(smc);
652 if (rc)
653 return smc_connect_abort(smc, rc, local_contact);
654 mutex_unlock(&smc_create_lgr_pending);
655
656 smc_copy_sock_settings_to_clc(smc);
657 if (smc->sk.sk_state == SMC_INIT)
658 smc->sk.sk_state = SMC_ACTIVE;
659
660 return 0;
661}
662
Hans Wippel3b2dec22018-05-18 09:34:18 +0200663/* perform steps before actually connecting */
664static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100665{
Hans Wippel41349842018-06-28 19:05:11 +0200666 bool ism_supported = false, rdma_supported = false;
Ursula Brauna046d572017-01-09 16:55:16 +0100667 struct smc_clc_msg_accept_confirm aclc;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200668 struct smc_ib_device *ibdev;
Hans Wippel41349842018-06-28 19:05:11 +0200669 struct smcd_dev *ismdev;
Ursula Braun7005ada2018-07-25 16:35:31 +0200670 u8 gid[SMC_GID_SIZE];
Hans Wippel41349842018-06-28 19:05:11 +0200671 unsigned short vlan;
672 int smc_type;
Ursula Brauna046d572017-01-09 16:55:16 +0100673 int rc = 0;
674 u8 ibport;
675
Ursula Braun51f1de72018-01-26 09:28:48 +0100676 sock_hold(&smc->sk); /* sock put in passive closing */
677
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200678 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +0200679 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200680
Hans Wippel3b2dec22018-05-18 09:34:18 +0200681 /* if peer has not signalled SMC-capability, fall back */
682 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +0200683 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200684
Ursula Brauna046d572017-01-09 16:55:16 +0100685 /* IPSec connections opt out of SMC-R optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200686 if (using_ipsec(smc))
687 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC);
Ursula Brauna046d572017-01-09 16:55:16 +0100688
Hans Wippel41349842018-06-28 19:05:11 +0200689 /* check for VLAN ID */
690 if (smc_vlan_by_tcpsk(smc->clcsock, &vlan))
691 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_CNFERR);
692
693 /* check if there is an ism device available */
694 if (!smc_check_ism(smc, &ismdev) &&
695 !smc_connect_ism_vlan_setup(smc, ismdev, vlan)) {
696 /* ISM is supported for this connection */
697 ism_supported = true;
698 smc_type = SMC_TYPE_D;
699 }
700
701 /* check if there is a rdma device available */
Ursula Braun7005ada2018-07-25 16:35:31 +0200702 if (!smc_check_rdma(smc, &ibdev, &ibport, vlan, gid)) {
Hans Wippel41349842018-06-28 19:05:11 +0200703 /* RDMA is supported for this connection */
704 rdma_supported = true;
705 if (ism_supported)
706 smc_type = SMC_TYPE_B; /* both */
707 else
708 smc_type = SMC_TYPE_R; /* only RDMA */
709 }
710
711 /* if neither ISM nor RDMA are supported, fallback */
712 if (!rdma_supported && !ism_supported)
Karsten Graul603cc142018-07-25 16:35:32 +0200713 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV);
Ursula Brauna046d572017-01-09 16:55:16 +0100714
Hans Wippel3b2dec22018-05-18 09:34:18 +0200715 /* perform CLC handshake */
Ursula Braun7005ada2018-07-25 16:35:31 +0200716 rc = smc_connect_clc(smc, smc_type, &aclc, ibdev, ibport, gid, ismdev);
Hans Wippel41349842018-06-28 19:05:11 +0200717 if (rc) {
718 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200719 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200720 }
Ursula Brauna046d572017-01-09 16:55:16 +0100721
Hans Wippel41349842018-06-28 19:05:11 +0200722 /* depending on previous steps, connect using rdma or ism */
723 if (rdma_supported && aclc.hdr.path == SMC_TYPE_R)
724 rc = smc_connect_rdma(smc, &aclc, ibdev, ibport);
725 else if (ism_supported && aclc.hdr.path == SMC_TYPE_D)
726 rc = smc_connect_ism(smc, &aclc, ismdev);
727 else
Karsten Graul603cc142018-07-25 16:35:32 +0200728 rc = SMC_CLC_DECL_MODEUNSUPP;
Hans Wippel41349842018-06-28 19:05:11 +0200729 if (rc) {
730 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200731 return smc_connect_decline_fallback(smc, rc);
Hans Wippel41349842018-06-28 19:05:11 +0200732 }
Ursula Brauna046d572017-01-09 16:55:16 +0100733
Hans Wippel41349842018-06-28 19:05:11 +0200734 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200735 return 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100736}
737
Ursula Braun24ac3a02018-06-27 17:59:50 +0200738static void smc_connect_work(struct work_struct *work)
739{
740 struct smc_sock *smc = container_of(work, struct smc_sock,
741 connect_work);
742 int rc;
743
744 lock_sock(&smc->sk);
745 rc = kernel_connect(smc->clcsock, &smc->connect_info->addr,
746 smc->connect_info->alen, smc->connect_info->flags);
747 if (smc->clcsock->sk->sk_err) {
748 smc->sk.sk_err = smc->clcsock->sk->sk_err;
749 goto out;
750 }
751 if (rc < 0) {
752 smc->sk.sk_err = -rc;
753 goto out;
754 }
755
756 rc = __smc_connect(smc);
757 if (rc < 0)
758 smc->sk.sk_err = -rc;
759
760out:
Ursula Braun648a5a72018-09-18 15:46:34 +0200761 if (smc->sk.sk_err)
762 smc->sk.sk_state_change(&smc->sk);
763 else
764 smc->sk.sk_write_space(&smc->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200765 kfree(smc->connect_info);
766 smc->connect_info = NULL;
767 release_sock(&smc->sk);
768}
769
Ursula Braunac713872017-01-09 16:55:13 +0100770static int smc_connect(struct socket *sock, struct sockaddr *addr,
771 int alen, int flags)
772{
773 struct sock *sk = sock->sk;
774 struct smc_sock *smc;
775 int rc = -EINVAL;
776
777 smc = smc_sk(sk);
778
779 /* separate smc parameter checking to be safe */
780 if (alen < sizeof(addr->sa_family))
781 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100782 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +0100783 goto out_err;
784
785 lock_sock(sk);
786 switch (sk->sk_state) {
787 default:
788 goto out;
789 case SMC_ACTIVE:
790 rc = -EISCONN;
791 goto out;
792 case SMC_INIT:
793 rc = 0;
794 break;
795 }
796
797 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200798 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +0200799 if (flags & O_NONBLOCK) {
800 if (smc->connect_info) {
801 rc = -EALREADY;
802 goto out;
803 }
804 smc->connect_info = kzalloc(alen + 2 * sizeof(int), GFP_KERNEL);
805 if (!smc->connect_info) {
806 rc = -ENOMEM;
807 goto out;
808 }
809 smc->connect_info->alen = alen;
810 smc->connect_info->flags = flags ^ O_NONBLOCK;
811 memcpy(&smc->connect_info->addr, addr, alen);
812 schedule_work(&smc->connect_work);
813 rc = -EINPROGRESS;
814 } else {
815 rc = kernel_connect(smc->clcsock, addr, alen, flags);
816 if (rc)
817 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100818
Ursula Braun24ac3a02018-06-27 17:59:50 +0200819 rc = __smc_connect(smc);
820 if (rc < 0)
821 goto out;
822 else
823 rc = 0; /* success cases including fallback */
824 }
Ursula Braunac713872017-01-09 16:55:13 +0100825
826out:
827 release_sock(sk);
828out_err:
829 return rc;
830}
831
832static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
833{
Ursula Braun3163c502018-01-24 10:28:12 +0100834 struct socket *new_clcsock = NULL;
835 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +0100836 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -0800837 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +0100838
Ursula Braun3163c502018-01-24 10:28:12 +0100839 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +0100840 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +0100841 if (!new_sk) {
842 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +0100843 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +0100844 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +0100845 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +0100846 goto out;
847 }
848 *new_smc = smc_sk(new_sk);
849
Myungho Jung78abe3d2018-12-18 09:02:25 -0800850 mutex_lock(&lsmc->clcsock_release_lock);
851 if (lsmc->clcsock)
852 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0);
853 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +0100854 lock_sock(lsk);
Ursula Braun35a6b172018-01-24 10:28:13 +0100855 if (rc < 0)
Ursula Braun3163c502018-01-24 10:28:12 +0100856 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +0100857 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Brauna046d572017-01-09 16:55:16 +0100858 if (new_clcsock)
859 sock_release(new_clcsock);
860 new_sk->sk_state = SMC_CLOSED;
861 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun3163c502018-01-24 10:28:12 +0100862 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100863 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +0100864 *new_smc = NULL;
865 goto out;
866 }
867
868 (*new_smc)->clcsock = new_clcsock;
869out:
870 return rc;
871}
872
Ursula Brauna046d572017-01-09 16:55:16 +0100873/* add a just created sock to the accept queue of the listen sock as
874 * candidate for a following socket accept call from user space
875 */
876static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
877{
878 struct smc_sock *par = smc_sk(parent);
879
Ursula Braun51f1de72018-01-26 09:28:48 +0100880 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +0100881 spin_lock(&par->accept_q_lock);
882 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
883 spin_unlock(&par->accept_q_lock);
884 sk_acceptq_added(parent);
885}
886
887/* remove a socket from the accept queue of its parental listening socket */
888static void smc_accept_unlink(struct sock *sk)
889{
890 struct smc_sock *par = smc_sk(sk)->listen_smc;
891
892 spin_lock(&par->accept_q_lock);
893 list_del_init(&smc_sk(sk)->accept_q);
894 spin_unlock(&par->accept_q_lock);
895 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100896 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +0100897}
898
899/* remove a sock from the accept queue to bind it to a new socket created
900 * for a socket accept call from user space
901 */
Ursula Braunb38d7322017-01-09 16:55:25 +0100902struct sock *smc_accept_dequeue(struct sock *parent,
903 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +0100904{
905 struct smc_sock *isk, *n;
906 struct sock *new_sk;
907
908 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
909 new_sk = (struct sock *)isk;
910
911 smc_accept_unlink(new_sk);
912 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braun127f4972018-01-26 09:28:49 +0100913 if (isk->clcsock) {
914 sock_release(isk->clcsock);
915 isk->clcsock = NULL;
916 }
Ursula Braun288c83902017-04-10 14:58:04 +0200917 new_sk->sk_prot->unhash(new_sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100918 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +0100919 continue;
920 }
921 if (new_sock)
922 sock_graft(new_sk, new_sock);
923 return new_sk;
924 }
925 return NULL;
926}
927
928/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +0100929void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +0100930{
931 struct smc_sock *smc = smc_sk(sk);
932
Ursula Braunb38d7322017-01-09 16:55:25 +0100933 lock_sock(sk);
934 if (!sk->sk_lingertime)
935 /* wait for peer closing */
936 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun51f1de72018-01-26 09:28:48 +0100937 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100938 smc_close_active(smc);
Ursula Braun288c83902017-04-10 14:58:04 +0200939 sock_set_flag(sk, SOCK_DEAD);
940 sk->sk_shutdown |= SHUTDOWN_MASK;
941 }
Ursula Brauna046d572017-01-09 16:55:16 +0100942 if (smc->clcsock) {
943 struct socket *tcp;
944
945 tcp = smc->clcsock;
946 smc->clcsock = NULL;
947 sock_release(tcp);
948 }
Ursula Braunb38d7322017-01-09 16:55:25 +0100949 if (smc->use_fallback) {
Ursula Braun51f1de72018-01-26 09:28:48 +0100950 sock_put(sk); /* passive closing */
951 sk->sk_state = SMC_CLOSED;
952 } else {
953 if (sk->sk_state == SMC_CLOSED)
954 smc_conn_free(&smc->conn);
Ursula Braunb38d7322017-01-09 16:55:25 +0100955 }
956 release_sock(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +0100957 sk->sk_prot->unhash(sk);
958 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +0100959}
960
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100961static int smc_serv_conf_first_link(struct smc_sock *smc)
962{
Karsten Graul877ae5b2018-05-02 16:56:44 +0200963 struct net *net = sock_net(smc->clcsock->sk);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100964 struct smc_link_group *lgr = smc->conn.lgr;
965 struct smc_link *link;
966 int rest;
967 int rc;
968
969 link = &lgr->lnk[SMC_SINGLE_LINK];
Ursula Braun652a1e42017-07-28 13:56:17 +0200970
Karsten Graul44aa81c2018-05-15 17:04:55 +0200971 if (smc_reg_rmb(link, smc->conn.rmb_desc, false))
Karsten Graul603cc142018-07-25 16:35:32 +0200972 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200973
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100974 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200975 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100976 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200977 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100978
979 /* receive CONFIRM LINK response from client over the RoCE fabric */
980 rest = wait_for_completion_interruptible_timeout(
981 &link->llc_confirm_resp,
982 SMC_LLC_WAIT_FIRST_TIME);
983 if (rest <= 0) {
984 struct smc_clc_msg_decline dclc;
985
986 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100987 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100988 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100989 }
990
Karsten Graul75d320d2018-03-01 13:51:31 +0100991 if (link->llc_confirm_resp_rc)
992 return SMC_CLC_DECL_RMBE_EC;
993
Karsten Graul52bedf32018-03-01 13:51:32 +0100994 /* send ADD LINK request to client over the RoCE fabric */
995 rc = smc_llc_send_add_link(link,
996 link->smcibdev->mac[link->ibport - 1],
Ursula Braun7005ada2018-07-25 16:35:31 +0200997 link->gid, SMC_LLC_REQ);
Karsten Graul52bedf32018-03-01 13:51:32 +0100998 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200999 return SMC_CLC_DECL_TIMEOUT_AL;
Karsten Graul52bedf32018-03-01 13:51:32 +01001000
1001 /* receive ADD LINK response from client over the RoCE fabric */
1002 rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp,
1003 SMC_LLC_WAIT_TIME);
1004 if (rest <= 0) {
1005 struct smc_clc_msg_decline dclc;
1006
1007 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001008 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001009 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc;
Karsten Graul52bedf32018-03-01 13:51:32 +01001010 }
1011
Karsten Graul877ae5b2018-05-02 16:56:44 +02001012 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time);
Karsten Graul52bedf32018-03-01 13:51:32 +01001013
Karsten Graul75d320d2018-03-01 13:51:31 +01001014 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001015}
1016
Hans Wippel3b2dec22018-05-18 09:34:18 +02001017/* listen worker: finish */
1018static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001019{
Ursula Brauna046d572017-01-09 16:55:16 +01001020 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001021 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001022
Ursula Braunb38d7322017-01-09 16:55:25 +01001023 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001024 if (lsmc->sk.sk_state == SMC_LISTEN) {
1025 smc_accept_enqueue(&lsmc->sk, newsmcsk);
1026 } else { /* no longer listening */
1027 smc_close_non_accepted(newsmcsk);
1028 }
1029 release_sock(&lsmc->sk);
1030
1031 /* Wake up accept */
1032 lsmc->sk.sk_data_ready(&lsmc->sk);
1033 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001034}
Ursula Brauna046d572017-01-09 16:55:16 +01001035
Hans Wippel3b2dec22018-05-18 09:34:18 +02001036/* listen worker: finish in state connected */
1037static void smc_listen_out_connected(struct smc_sock *new_smc)
1038{
1039 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001040
Hans Wippel3b2dec22018-05-18 09:34:18 +02001041 sk_refcnt_debug_inc(newsmcsk);
1042 if (newsmcsk->sk_state == SMC_INIT)
1043 newsmcsk->sk_state = SMC_ACTIVE;
1044
1045 smc_listen_out(new_smc);
1046}
1047
1048/* listen worker: finish in error state */
1049static void smc_listen_out_err(struct smc_sock *new_smc)
1050{
1051 struct sock *newsmcsk = &new_smc->sk;
1052
Ursula Braun51f1de72018-01-26 09:28:48 +01001053 if (newsmcsk->sk_state == SMC_INIT)
1054 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001055 newsmcsk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001056 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001057
1058 smc_listen_out(new_smc);
1059}
1060
1061/* listen worker: decline and fall back if possible */
1062static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
1063 int local_contact)
1064{
1065 /* RDMA setup failed, switch back to TCP */
1066 if (local_contact == SMC_FIRST_CONTACT)
1067 smc_lgr_forget(new_smc->conn.lgr);
1068 if (reason_code < 0) { /* error, no fallback possible */
1069 smc_listen_out_err(new_smc);
1070 return;
1071 }
1072 smc_conn_free(&new_smc->conn);
1073 new_smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001074 new_smc->fallback_rsn = reason_code;
1075 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001076 if (smc_clc_send_decline(new_smc, reason_code) < 0) {
1077 smc_listen_out_err(new_smc);
1078 return;
1079 }
1080 }
1081 smc_listen_out_connected(new_smc);
1082}
1083
1084/* listen worker: check prefixes */
1085static int smc_listen_rdma_check(struct smc_sock *new_smc,
1086 struct smc_clc_msg_proposal *pclc)
1087{
1088 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1089 struct socket *newclcsock = new_smc->clcsock;
1090
1091 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1092 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
1093 return SMC_CLC_DECL_CNFERR;
1094
1095 return 0;
1096}
1097
1098/* listen worker: initialize connection and buffers */
1099static int smc_listen_rdma_init(struct smc_sock *new_smc,
1100 struct smc_clc_msg_proposal *pclc,
1101 struct smc_ib_device *ibdev, u8 ibport,
1102 int *local_contact)
1103{
1104 /* allocate connection / link group */
Karsten Graulee05ff72018-11-20 16:46:40 +01001105 *local_contact = smc_conn_create(new_smc, false, 0, ibdev, ibport, 0,
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001106 &pclc->lcl, NULL, 0);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001107 if (*local_contact < 0) {
1108 if (*local_contact == -ENOMEM)
1109 return SMC_CLC_DECL_MEM;/* insufficient memory*/
1110 return SMC_CLC_DECL_INTERR; /* other error */
1111 }
1112
1113 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001114 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001115 return SMC_CLC_DECL_MEM;
1116
1117 return 0;
1118}
1119
Hans Wippel41349842018-06-28 19:05:11 +02001120/* listen worker: initialize connection and buffers for SMC-D */
1121static int smc_listen_ism_init(struct smc_sock *new_smc,
1122 struct smc_clc_msg_proposal *pclc,
1123 struct smcd_dev *ismdev,
1124 int *local_contact)
1125{
1126 struct smc_clc_msg_smcd *pclc_smcd;
1127
1128 pclc_smcd = smc_get_clc_msg_smcd(pclc);
Karsten Graulee05ff72018-11-20 16:46:40 +01001129 *local_contact = smc_conn_create(new_smc, true, 0, NULL, 0, 0, NULL,
Hans Wippel41349842018-06-28 19:05:11 +02001130 ismdev, pclc_smcd->gid);
1131 if (*local_contact < 0) {
1132 if (*local_contact == -ENOMEM)
1133 return SMC_CLC_DECL_MEM;/* insufficient memory*/
1134 return SMC_CLC_DECL_INTERR; /* other error */
1135 }
1136
1137 /* Check if peer can be reached via ISM device */
1138 if (smc_ism_cantalk(new_smc->conn.lgr->peer_gid,
1139 new_smc->conn.lgr->vlan_id,
1140 new_smc->conn.lgr->smcd)) {
1141 if (*local_contact == SMC_FIRST_CONTACT)
1142 smc_lgr_forget(new_smc->conn.lgr);
1143 smc_conn_free(&new_smc->conn);
1144 return SMC_CLC_DECL_CNFERR;
1145 }
1146
1147 /* Create send and receive buffers */
1148 if (smc_buf_create(new_smc, true)) {
1149 if (*local_contact == SMC_FIRST_CONTACT)
1150 smc_lgr_forget(new_smc->conn.lgr);
1151 smc_conn_free(&new_smc->conn);
1152 return SMC_CLC_DECL_MEM;
1153 }
1154
1155 return 0;
1156}
1157
Hans Wippel3b2dec22018-05-18 09:34:18 +02001158/* listen worker: register buffers */
1159static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact)
1160{
1161 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1162
1163 if (local_contact != SMC_FIRST_CONTACT) {
Karsten Graulc7674c02018-11-22 10:26:43 +01001164 if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true))
1165 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001166 }
1167 smc_rmb_sync_sg_for_device(&new_smc->conn);
1168
1169 return 0;
1170}
1171
1172/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001173static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1174 struct smc_clc_msg_accept_confirm *cclc,
1175 int local_contact)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001176{
1177 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK];
1178 int reason_code = 0;
1179
1180 if (local_contact == SMC_FIRST_CONTACT)
1181 smc_link_save_peer_info(link, cclc);
1182
1183 if (smc_rmb_rtoken_handling(&new_smc->conn, cclc)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001184 reason_code = SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001185 goto decline;
1186 }
1187
1188 if (local_contact == SMC_FIRST_CONTACT) {
1189 if (smc_ib_ready_link(link)) {
Karsten Graul603cc142018-07-25 16:35:32 +02001190 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001191 goto decline;
1192 }
1193 /* QP confirmation over RoCE fabric */
1194 reason_code = smc_serv_conf_first_link(new_smc);
1195 if (reason_code)
1196 goto decline;
1197 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001198 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001199
1200decline:
Hans Wippel3b2dec22018-05-18 09:34:18 +02001201 smc_listen_decline(new_smc, reason_code, local_contact);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001202 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001203}
1204
1205/* setup for RDMA connection of server */
1206static void smc_listen_work(struct work_struct *work)
1207{
1208 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1209 smc_listen_work);
1210 struct socket *newclcsock = new_smc->clcsock;
1211 struct smc_clc_msg_accept_confirm cclc;
1212 struct smc_clc_msg_proposal *pclc;
1213 struct smc_ib_device *ibdev;
Hans Wippel41349842018-06-28 19:05:11 +02001214 bool ism_supported = false;
1215 struct smcd_dev *ismdev;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001216 u8 buf[SMC_CLC_MAX_LEN];
1217 int local_contact = 0;
Ursula Braun7005ada2018-07-25 16:35:31 +02001218 unsigned short vlan;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001219 int reason_code = 0;
1220 int rc = 0;
1221 u8 ibport;
1222
1223 if (new_smc->use_fallback) {
1224 smc_listen_out_connected(new_smc);
1225 return;
1226 }
1227
1228 /* check if peer is smc capable */
1229 if (!tcp_sk(newclcsock->sk)->syn_smc) {
1230 new_smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001231 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001232 smc_listen_out_connected(new_smc);
1233 return;
1234 }
1235
1236 /* do inband token exchange -
1237 * wait for and receive SMC Proposal CLC message
1238 */
1239 pclc = (struct smc_clc_msg_proposal *)&buf;
1240 reason_code = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN,
Ursula Braun2b59f582018-11-22 10:26:39 +01001241 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001242 if (reason_code) {
1243 smc_listen_decline(new_smc, reason_code, 0);
1244 return;
1245 }
1246
1247 /* IPSec connections opt out of SMC-R optimizations */
1248 if (using_ipsec(new_smc)) {
1249 smc_listen_decline(new_smc, SMC_CLC_DECL_IPSEC, 0);
1250 return;
1251 }
1252
1253 mutex_lock(&smc_create_lgr_pending);
1254 smc_close_init(new_smc);
1255 smc_rx_init(new_smc);
1256 smc_tx_init(new_smc);
1257
Hans Wippel41349842018-06-28 19:05:11 +02001258 /* check if ISM is available */
1259 if ((pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) &&
1260 !smc_check_ism(new_smc, &ismdev) &&
1261 !smc_listen_ism_init(new_smc, pclc, ismdev, &local_contact)) {
1262 ism_supported = true;
1263 }
1264
Hans Wippel3b2dec22018-05-18 09:34:18 +02001265 /* check if RDMA is available */
Hans Wippel41349842018-06-28 19:05:11 +02001266 if (!ism_supported &&
1267 ((pclc->hdr.path != SMC_TYPE_R && pclc->hdr.path != SMC_TYPE_B) ||
Ursula Braun7005ada2018-07-25 16:35:31 +02001268 smc_vlan_by_tcpsk(new_smc->clcsock, &vlan) ||
1269 smc_check_rdma(new_smc, &ibdev, &ibport, vlan, NULL) ||
Hans Wippel41349842018-06-28 19:05:11 +02001270 smc_listen_rdma_check(new_smc, pclc) ||
1271 smc_listen_rdma_init(new_smc, pclc, ibdev, ibport,
1272 &local_contact) ||
1273 smc_listen_rdma_reg(new_smc, local_contact))) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001274 /* SMC not supported, decline */
1275 mutex_unlock(&smc_create_lgr_pending);
Karsten Graul603cc142018-07-25 16:35:32 +02001276 smc_listen_decline(new_smc, SMC_CLC_DECL_MODEUNSUPP,
1277 local_contact);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001278 return;
1279 }
1280
1281 /* send SMC Accept CLC message */
1282 rc = smc_clc_send_accept(new_smc, local_contact);
1283 if (rc) {
1284 mutex_unlock(&smc_create_lgr_pending);
1285 smc_listen_decline(new_smc, rc, local_contact);
1286 return;
1287 }
1288
1289 /* receive SMC Confirm CLC message */
1290 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001291 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001292 if (reason_code) {
1293 mutex_unlock(&smc_create_lgr_pending);
1294 smc_listen_decline(new_smc, reason_code, local_contact);
1295 return;
1296 }
1297
1298 /* finish worker */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001299 if (!ism_supported) {
Ursula Braun587e41d2018-11-22 10:26:40 +01001300 if (smc_listen_rdma_finish(new_smc, &cclc, local_contact)) {
1301 mutex_unlock(&smc_create_lgr_pending);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001302 return;
Ursula Braun587e41d2018-11-22 10:26:40 +01001303 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001304 }
Hans Wippel3b2dec22018-05-18 09:34:18 +02001305 smc_conn_save_peer_info(new_smc, &cclc);
1306 mutex_unlock(&smc_create_lgr_pending);
1307 smc_listen_out_connected(new_smc);
Ursula Brauna046d572017-01-09 16:55:16 +01001308}
1309
1310static void smc_tcp_listen_work(struct work_struct *work)
1311{
1312 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1313 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001314 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001315 struct smc_sock *new_smc;
1316 int rc = 0;
1317
Ursula Braun3163c502018-01-24 10:28:12 +01001318 lock_sock(lsk);
1319 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001320 rc = smc_clcsock_accept(lsmc, &new_smc);
1321 if (rc)
1322 goto out;
1323 if (!new_smc)
1324 continue;
1325
1326 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001327 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001328 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001329 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001330 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1331 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001332 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1333 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001334 sock_hold(&new_smc->sk); /* sock_put in passive closing */
1335 if (!schedule_work(&new_smc->smc_listen_work))
1336 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001337 }
1338
1339out:
Ursula Braun3163c502018-01-24 10:28:12 +01001340 release_sock(lsk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001341 sock_put(&lsmc->sk); /* sock_hold in smc_listen */
Ursula Brauna046d572017-01-09 16:55:16 +01001342}
1343
Ursula Braunac713872017-01-09 16:55:13 +01001344static int smc_listen(struct socket *sock, int backlog)
1345{
1346 struct sock *sk = sock->sk;
1347 struct smc_sock *smc;
1348 int rc;
1349
1350 smc = smc_sk(sk);
1351 lock_sock(sk);
1352
1353 rc = -EINVAL;
1354 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN))
1355 goto out;
1356
1357 rc = 0;
1358 if (sk->sk_state == SMC_LISTEN) {
1359 sk->sk_max_ack_backlog = backlog;
1360 goto out;
1361 }
1362 /* some socket options are handled in core, so we could not apply
1363 * them to the clc socket -- copy smc socket options to clc socket
1364 */
1365 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001366 if (!smc->use_fallback)
1367 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001368
1369 rc = kernel_listen(smc->clcsock, backlog);
1370 if (rc)
1371 goto out;
1372 sk->sk_max_ack_backlog = backlog;
1373 sk->sk_ack_backlog = 0;
1374 sk->sk_state = SMC_LISTEN;
Ursula Braun51f1de72018-01-26 09:28:48 +01001375 sock_hold(sk); /* sock_hold in tcp_listen_worker */
1376 if (!schedule_work(&smc->tcp_listen_work))
1377 sock_put(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001378
1379out:
1380 release_sock(sk);
1381 return rc;
1382}
1383
1384static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001385 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001386{
Ursula Brauna046d572017-01-09 16:55:16 +01001387 struct sock *sk = sock->sk, *nsk;
1388 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001389 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001390 long timeo;
1391 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001392
1393 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001394 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001395 lock_sock(sk);
1396
1397 if (lsmc->sk.sk_state != SMC_LISTEN) {
1398 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001399 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001400 goto out;
1401 }
1402
Ursula Brauna046d572017-01-09 16:55:16 +01001403 /* Wait for an incoming connection */
1404 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1405 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1406 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1407 set_current_state(TASK_INTERRUPTIBLE);
1408 if (!timeo) {
1409 rc = -EAGAIN;
1410 break;
1411 }
1412 release_sock(sk);
1413 timeo = schedule_timeout(timeo);
1414 /* wakeup by sk_data_ready in smc_listen_work() */
1415 sched_annotate_sleep();
1416 lock_sock(sk);
1417 if (signal_pending(current)) {
1418 rc = sock_intr_errno(timeo);
1419 break;
1420 }
1421 }
1422 set_current_state(TASK_RUNNING);
1423 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001424
Ursula Brauna046d572017-01-09 16:55:16 +01001425 if (!rc)
1426 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001427 release_sock(sk);
1428 if (rc)
1429 goto out;
1430
1431 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1432 /* wait till data arrives on the socket */
1433 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1434 MSEC_PER_SEC);
1435 if (smc_sk(nsk)->use_fallback) {
1436 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1437
1438 lock_sock(clcsk);
1439 if (skb_queue_empty(&clcsk->sk_receive_queue))
1440 sk_wait_data(clcsk, &timeo, NULL);
1441 release_sock(clcsk);
1442 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1443 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001444 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001445 release_sock(nsk);
1446 }
1447 }
Ursula Braunac713872017-01-09 16:55:13 +01001448
1449out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001450 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001451 return rc;
1452}
1453
1454static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001455 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001456{
1457 struct smc_sock *smc;
1458
Ursula Braunb38d7322017-01-09 16:55:25 +01001459 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1460 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001461 return -ENOTCONN;
1462
1463 smc = smc_sk(sock->sk);
1464
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001465 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001466}
1467
1468static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1469{
1470 struct sock *sk = sock->sk;
1471 struct smc_sock *smc;
1472 int rc = -EPIPE;
1473
1474 smc = smc_sk(sk);
1475 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001476 if ((sk->sk_state != SMC_ACTIVE) &&
1477 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1478 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001479 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001480
1481 if (msg->msg_flags & MSG_FASTOPEN) {
1482 if (sk->sk_state == SMC_INIT) {
1483 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001484 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001485 } else {
1486 rc = -EINVAL;
1487 goto out;
1488 }
1489 }
1490
Ursula Braunac713872017-01-09 16:55:13 +01001491 if (smc->use_fallback)
1492 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1493 else
Ursula Braune6727f32017-01-09 16:55:23 +01001494 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001495out:
1496 release_sock(sk);
1497 return rc;
1498}
1499
1500static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1501 int flags)
1502{
1503 struct sock *sk = sock->sk;
1504 struct smc_sock *smc;
1505 int rc = -ENOTCONN;
1506
1507 smc = smc_sk(sk);
1508 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001509 if ((sk->sk_state == SMC_INIT) ||
1510 (sk->sk_state == SMC_LISTEN) ||
1511 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01001512 goto out;
1513
Ursula Braunb38d7322017-01-09 16:55:25 +01001514 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1515 rc = 0;
1516 goto out;
1517 }
1518
Stefan Raspl9014db22018-05-03 18:12:39 +02001519 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01001520 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02001521 } else {
1522 msg->msg_namelen = 0;
1523 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
1524 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001525
Ursula Braunac713872017-01-09 16:55:13 +01001526out:
1527 release_sock(sk);
1528 return rc;
1529}
1530
Al Viroade994f2017-07-03 00:01:49 -04001531static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01001532{
Ursula Braun8dce2782018-01-26 09:28:47 +01001533 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05001534 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01001535
Ursula Braun8dce2782018-01-26 09:28:47 +01001536 spin_lock(&isk->accept_q_lock);
1537 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001538 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001539 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01001540
Ursula Braun8dce2782018-01-26 09:28:47 +01001541 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01001542}
1543
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001544static __poll_t smc_poll(struct file *file, struct socket *sock,
1545 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01001546{
1547 struct sock *sk = sock->sk;
Al Viroe6c8adc2017-07-03 22:25:56 -04001548 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001549 struct smc_sock *smc;
1550
Ursula Braun8dce2782018-01-26 09:28:47 +01001551 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001552 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01001553
Ursula Braunac713872017-01-09 16:55:13 +01001554 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02001555 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01001556 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001557 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02001558 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001559 if (sk->sk_err)
Ursula Braun784813a2018-05-02 16:53:56 +02001560 mask |= EPOLLERR;
Ursula Braunac713872017-01-09 16:55:13 +01001561 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07001562 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02001563 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01001564 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001565 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01001566 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
1567 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001568 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001569 if (sk->sk_state == SMC_LISTEN) {
1570 /* woken up by sk_data_ready in smc_listen_work() */
1571 mask = smc_accept_poll(sk);
1572 } else {
1573 if (atomic_read(&smc->conn.sndbuf_space) ||
1574 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001575 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001576 } else {
1577 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1578 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1579 }
1580 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001581 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01001582 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001583 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01001584 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08001585 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02001586 if (smc->conn.urg_state == SMC_URG_VALID)
1587 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01001588 }
Ursula Braunac713872017-01-09 16:55:13 +01001589 }
1590
1591 return mask;
1592}
1593
1594static int smc_shutdown(struct socket *sock, int how)
1595{
1596 struct sock *sk = sock->sk;
1597 struct smc_sock *smc;
1598 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01001599 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001600
1601 smc = smc_sk(sk);
1602
1603 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01001604 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001605
1606 lock_sock(sk);
1607
1608 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02001609 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01001610 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
1611 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
1612 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1613 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
1614 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01001615 goto out;
1616 if (smc->use_fallback) {
1617 rc = kernel_sock_shutdown(smc->clcsock, how);
1618 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
1619 if (sk->sk_shutdown == SHUTDOWN_MASK)
1620 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01001621 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01001622 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001623 switch (how) {
1624 case SHUT_RDWR: /* shutdown in both directions */
1625 rc = smc_close_active(smc);
1626 break;
1627 case SHUT_WR:
1628 rc = smc_close_shutdown_write(smc);
1629 break;
1630 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02001631 rc = 0;
1632 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01001633 break;
1634 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02001635 if (smc->clcsock)
1636 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01001637 /* map sock_shutdown_cmd constants to sk_shutdown value range */
1638 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01001639
1640out:
1641 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001642 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01001643}
1644
1645static int smc_setsockopt(struct socket *sock, int level, int optname,
1646 char __user *optval, unsigned int optlen)
1647{
1648 struct sock *sk = sock->sk;
1649 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001650 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01001651
1652 smc = smc_sk(sk);
1653
1654 /* generic setsockopts reaching us here always apply to the
1655 * CLC socket
1656 */
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001657 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
1658 optval, optlen);
1659 if (smc->clcsock->sk->sk_err) {
1660 sk->sk_err = smc->clcsock->sk->sk_err;
1661 sk->sk_error_report(sk);
1662 }
1663 if (rc)
1664 return rc;
1665
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001666 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00001667 return -EINVAL;
Ursula Braunac0107e2018-07-18 15:22:50 +02001668 if (get_user(val, (int __user *)optval))
1669 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001670
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001671 lock_sock(sk);
1672 switch (optname) {
1673 case TCP_ULP:
1674 case TCP_FASTOPEN:
1675 case TCP_FASTOPEN_CONNECT:
1676 case TCP_FASTOPEN_KEY:
1677 case TCP_FASTOPEN_NO_COOKIE:
1678 /* option not supported by SMC */
1679 if (sk->sk_state == SMC_INIT) {
1680 smc->use_fallback = true;
Karsten Graul603cc142018-07-25 16:35:32 +02001681 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001682 } else {
1683 if (!smc->use_fallback)
1684 rc = -EINVAL;
1685 }
1686 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001687 case TCP_NODELAY:
1688 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001689 if (val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001690 mod_delayed_work(system_wq, &smc->conn.tx_work,
1691 0);
1692 }
1693 break;
1694 case TCP_CORK:
1695 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) {
Ursula Braun569bc642018-05-15 17:04:54 +02001696 if (!val && !smc->use_fallback)
Ursula Braun01d2f7e2018-04-26 17:18:22 +02001697 mod_delayed_work(system_wq, &smc->conn.tx_work,
1698 0);
1699 }
1700 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02001701 case TCP_DEFER_ACCEPT:
1702 smc->sockopt_defer_accept = val;
1703 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001704 default:
1705 break;
1706 }
1707 release_sock(sk);
1708
1709 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01001710}
1711
1712static int smc_getsockopt(struct socket *sock, int level, int optname,
1713 char __user *optval, int __user *optlen)
1714{
1715 struct smc_sock *smc;
1716
1717 smc = smc_sk(sock->sk);
1718 /* socket options apply to the CLC socket */
1719 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
1720 optval, optlen);
1721}
1722
1723static int smc_ioctl(struct socket *sock, unsigned int cmd,
1724 unsigned long arg)
1725{
Stefan Rasplde8474e2018-05-23 16:38:11 +02001726 union smc_host_cursor cons, urg;
1727 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01001728 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02001729 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01001730
1731 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001732 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02001733 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02001734 if (smc->use_fallback) {
1735 if (!smc->clcsock) {
1736 release_sock(&smc->sk);
1737 return -EBADF;
1738 }
1739 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
1740 release_sock(&smc->sk);
1741 return answ;
1742 }
Ursula Braun9b67e262018-05-02 16:56:46 +02001743 switch (cmd) {
1744 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02001745 if (smc->sk.sk_state == SMC_LISTEN) {
1746 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001747 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001748 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001749 if (smc->sk.sk_state == SMC_INIT ||
1750 smc->sk.sk_state == SMC_CLOSED)
1751 answ = 0;
1752 else
1753 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02001754 break;
1755 case SIOCOUTQ:
1756 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02001757 if (smc->sk.sk_state == SMC_LISTEN) {
1758 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001759 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001760 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001761 if (smc->sk.sk_state == SMC_INIT ||
1762 smc->sk.sk_state == SMC_CLOSED)
1763 answ = 0;
1764 else
1765 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02001766 atomic_read(&smc->conn.sndbuf_space);
1767 break;
1768 case SIOCOUTQNSD:
1769 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02001770 if (smc->sk.sk_state == SMC_LISTEN) {
1771 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001772 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001773 }
Ursula Braun2351abe2018-05-23 16:38:09 +02001774 if (smc->sk.sk_state == SMC_INIT ||
1775 smc->sk.sk_state == SMC_CLOSED)
1776 answ = 0;
1777 else
1778 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02001779 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02001780 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02001781 if (smc->sk.sk_state == SMC_LISTEN) {
1782 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001783 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02001784 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02001785 if (smc->sk.sk_state == SMC_INIT ||
1786 smc->sk.sk_state == SMC_CLOSED) {
1787 answ = 0;
1788 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02001789 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
1790 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02001791 answ = smc_curs_diff(conn->rmb_desc->len,
1792 &cons, &urg) == 1;
1793 }
1794 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02001795 default:
Ursula Braun1992d992018-07-16 13:56:52 +02001796 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001797 return -ENOIOCTLCMD;
1798 }
Ursula Braun1992d992018-07-16 13:56:52 +02001799 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02001800
1801 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01001802}
1803
1804static ssize_t smc_sendpage(struct socket *sock, struct page *page,
1805 int offset, size_t size, int flags)
1806{
1807 struct sock *sk = sock->sk;
1808 struct smc_sock *smc;
1809 int rc = -EPIPE;
1810
1811 smc = smc_sk(sk);
1812 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001813 if (sk->sk_state != SMC_ACTIVE) {
1814 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001815 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02001816 }
1817 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001818 if (smc->use_fallback)
1819 rc = kernel_sendpage(smc->clcsock, page, offset,
1820 size, flags);
1821 else
1822 rc = sock_no_sendpage(sock, page, offset, size, flags);
1823
1824out:
Ursula Braunac713872017-01-09 16:55:13 +01001825 return rc;
1826}
1827
Stefan Raspl9014db22018-05-03 18:12:39 +02001828/* Map the affected portions of the rmbe into an spd, note the number of bytes
1829 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
1830 * updates till whenever a respective page has been fully processed.
1831 * Note that subsequent recv() calls have to wait till all splice() processing
1832 * completed.
1833 */
Ursula Braunac713872017-01-09 16:55:13 +01001834static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
1835 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02001836 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01001837{
1838 struct sock *sk = sock->sk;
1839 struct smc_sock *smc;
1840 int rc = -ENOTCONN;
1841
1842 smc = smc_sk(sk);
1843 lock_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001844
1845 if (sk->sk_state == SMC_INIT ||
1846 sk->sk_state == SMC_LISTEN ||
1847 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01001848 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02001849
1850 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
1851 rc = 0;
1852 goto out;
1853 }
1854
Ursula Braunac713872017-01-09 16:55:13 +01001855 if (smc->use_fallback) {
1856 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
1857 pipe, len, flags);
1858 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02001859 if (*ppos) {
1860 rc = -ESPIPE;
1861 goto out;
1862 }
1863 if (flags & SPLICE_F_NONBLOCK)
1864 flags = MSG_DONTWAIT;
1865 else
1866 flags = 0;
1867 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01001868 }
1869out:
1870 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02001871
Ursula Braunac713872017-01-09 16:55:13 +01001872 return rc;
1873}
1874
1875/* must look like tcp */
1876static const struct proto_ops smc_sock_ops = {
1877 .family = PF_SMC,
1878 .owner = THIS_MODULE,
1879 .release = smc_release,
1880 .bind = smc_bind,
1881 .connect = smc_connect,
1882 .socketpair = sock_no_socketpair,
1883 .accept = smc_accept,
1884 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001885 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01001886 .ioctl = smc_ioctl,
1887 .listen = smc_listen,
1888 .shutdown = smc_shutdown,
1889 .setsockopt = smc_setsockopt,
1890 .getsockopt = smc_getsockopt,
1891 .sendmsg = smc_sendmsg,
1892 .recvmsg = smc_recvmsg,
1893 .mmap = sock_no_mmap,
1894 .sendpage = smc_sendpage,
1895 .splice_read = smc_splice_read,
1896};
1897
1898static int smc_create(struct net *net, struct socket *sock, int protocol,
1899 int kern)
1900{
Karsten Graulaaa4d332018-03-16 15:06:41 +01001901 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01001902 struct smc_sock *smc;
1903 struct sock *sk;
1904 int rc;
1905
1906 rc = -ESOCKTNOSUPPORT;
1907 if (sock->type != SOCK_STREAM)
1908 goto out;
1909
1910 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001911 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01001912 goto out;
1913
1914 rc = -ENOBUFS;
1915 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001916 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001917 if (!sk)
1918 goto out;
1919
1920 /* create internal TCP socket for CLC handshake and fallback */
1921 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001922 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02001923 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001924 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
1925 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01001926 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01001927 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01001928 goto out;
1929 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01001930 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
1931 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01001932
1933out:
1934 return rc;
1935}
1936
1937static const struct net_proto_family smc_sock_family_ops = {
1938 .family = PF_SMC,
1939 .owner = THIS_MODULE,
1940 .create = smc_create,
1941};
1942
1943static int __init smc_init(void)
1944{
1945 int rc;
1946
Thomas Richter6812baa2017-01-09 16:55:15 +01001947 rc = smc_pnet_init();
1948 if (rc)
1949 return rc;
1950
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001951 rc = smc_llc_init();
1952 if (rc) {
1953 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
1954 goto out_pnet;
1955 }
1956
Ursula Braun5f083182017-01-09 16:55:22 +01001957 rc = smc_cdc_init();
1958 if (rc) {
1959 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
1960 goto out_pnet;
1961 }
1962
Ursula Braunac713872017-01-09 16:55:13 +01001963 rc = proto_register(&smc_proto, 1);
1964 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01001965 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Thomas Richter6812baa2017-01-09 16:55:15 +01001966 goto out_pnet;
Ursula Braunac713872017-01-09 16:55:13 +01001967 }
1968
Karsten Graulaaa4d332018-03-16 15:06:41 +01001969 rc = proto_register(&smc_proto6, 1);
1970 if (rc) {
1971 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
1972 goto out_proto;
1973 }
1974
Ursula Braunac713872017-01-09 16:55:13 +01001975 rc = sock_register(&smc_sock_family_ops);
1976 if (rc) {
1977 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001978 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01001979 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01001980 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001981 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01001982
Ursula Brauna4cf0442017-01-09 16:55:14 +01001983 rc = smc_ib_register_client();
1984 if (rc) {
1985 pr_err("%s: ib_register fails with %d\n", __func__, rc);
1986 goto out_sock;
1987 }
1988
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001989 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01001990 return 0;
1991
Ursula Brauna4cf0442017-01-09 16:55:14 +01001992out_sock:
1993 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001994out_proto6:
1995 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01001996out_proto:
1997 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01001998out_pnet:
1999 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01002000 return rc;
2001}
2002
2003static void __exit smc_exit(void)
2004{
Hans Wippel9fda3512018-05-18 09:34:11 +02002005 smc_core_exit();
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002006 static_branch_disable(&tcp_have_smc);
Ursula Brauna4cf0442017-01-09 16:55:14 +01002007 smc_ib_unregister_client();
Ursula Braunac713872017-01-09 16:55:13 +01002008 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002009 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002010 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002011 smc_pnet_exit();
Ursula Braunac713872017-01-09 16:55:13 +01002012}
2013
2014module_init(smc_init);
2015module_exit(smc_exit);
2016
2017MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2018MODULE_DESCRIPTION("smc socket address family");
2019MODULE_LICENSE("GPL");
2020MODULE_ALIAS_NETPROTO(PF_SMC);