blob: 82be0bd0f6e8135a5d159e8a1620ef23c3986413 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Ursula Braunac713872017-01-09 16:55:13 +01002/*
3 * Shared Memory Communications over RDMA (SMC-R) and RoCE
4 *
5 * AF_SMC protocol family socket handler keeping the AF_INET sock address type
6 * applies to SOCK_STREAM sockets only
7 * offers an alternative communication option for TCP-protocol sockets
8 * applicable with RoCE-cards only
9 *
Ursula Brauna046d572017-01-09 16:55:16 +010010 * Initial restrictions:
Ursula Brauna046d572017-01-09 16:55:16 +010011 * - support for alternate links postponed
Ursula Brauna046d572017-01-09 16:55:16 +010012 *
Karsten Graulaaa4d332018-03-16 15:06:41 +010013 * Copyright IBM Corp. 2016, 2018
Ursula Braunac713872017-01-09 16:55:13 +010014 *
15 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com>
16 * based on prototype from Frank Blaschka
17 */
18
19#define KMSG_COMPONENT "smc"
20#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
21
22#include <linux/module.h>
23#include <linux/socket.h>
Ursula Brauna046d572017-01-09 16:55:16 +010024#include <linux/workqueue.h>
Ursula Braun5f083182017-01-09 16:55:22 +010025#include <linux/in.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010026#include <linux/sched/signal.h>
Hans Wippel41349842018-06-28 19:05:11 +020027#include <linux/if_vlan.h>
Ursula Braun4ead9c92019-11-16 17:47:31 +010028#include <linux/rcupdate_wait.h>
Ursula Braunb81a5eb2020-09-26 12:44:31 +020029#include <linux/ctype.h>
Ingo Molnarc3edc402017-02-02 08:35:14 +010030
Ursula Braunac713872017-01-09 16:55:13 +010031#include <net/sock.h>
Ursula Brauna046d572017-01-09 16:55:16 +010032#include <net/tcp.h>
Ursula Braunf16a7dd2017-01-09 16:55:26 +010033#include <net/smc.h>
Ursula Braun9b67e262018-05-02 16:56:46 +020034#include <asm/ioctls.h>
Ursula Braunac713872017-01-09 16:55:13 +010035
Hans Wippel64e28b52019-02-21 13:01:02 +010036#include <net/net_namespace.h>
37#include <net/netns/generic.h>
38#include "smc_netns.h"
39
Ursula Braunac713872017-01-09 16:55:13 +010040#include "smc.h"
Ursula Brauna046d572017-01-09 16:55:16 +010041#include "smc_clc.h"
Ursula Braun9bf9abe2017-01-09 16:55:21 +010042#include "smc_llc.h"
Ursula Braun5f083182017-01-09 16:55:22 +010043#include "smc_cdc.h"
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010044#include "smc_core.h"
Ursula Brauna4cf0442017-01-09 16:55:14 +010045#include "smc_ib.h"
Hans Wippel41349842018-06-28 19:05:11 +020046#include "smc_ism.h"
Thomas Richter6812baa2017-01-09 16:55:15 +010047#include "smc_pnet.h"
Ursula Braune6727f32017-01-09 16:55:23 +010048#include "smc_tx.h"
Ursula Braun952310c2017-01-09 16:55:24 +010049#include "smc_rx.h"
Ursula Braunb38d7322017-01-09 16:55:25 +010050#include "smc_close.h"
Ursula Braunac713872017-01-09 16:55:13 +010051
Hans Wippel72a36a82019-02-07 15:56:18 +010052static DEFINE_MUTEX(smc_server_lgr_pending); /* serialize link group
53 * creation on server
54 */
55static DEFINE_MUTEX(smc_client_lgr_pending); /* serialize link group
56 * creation on client
Ursula Braun0cfdd8f2017-01-09 16:55:17 +010057 */
58
Karsten Graul22ef4732020-09-10 18:48:29 +020059struct workqueue_struct *smc_hs_wq; /* wq for handshake work */
60struct workqueue_struct *smc_close_wq; /* wq for close work */
61
Ursula Brauna046d572017-01-09 16:55:16 +010062static void smc_tcp_listen_work(struct work_struct *);
Ursula Braun24ac3a02018-06-27 17:59:50 +020063static void smc_connect_work(struct work_struct *);
Ursula Brauna046d572017-01-09 16:55:16 +010064
Ursula Braunac713872017-01-09 16:55:13 +010065static void smc_set_keepalive(struct sock *sk, int val)
66{
67 struct smc_sock *smc = smc_sk(sk);
68
69 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val);
70}
71
Ursula Braunf16a7dd2017-01-09 16:55:26 +010072static struct smc_hashinfo smc_v4_hashinfo = {
73 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock),
74};
75
Karsten Graulaaa4d332018-03-16 15:06:41 +010076static struct smc_hashinfo smc_v6_hashinfo = {
77 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock),
78};
79
Ursula Braunf16a7dd2017-01-09 16:55:26 +010080int smc_hash_sk(struct sock *sk)
81{
82 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
83 struct hlist_head *head;
84
85 head = &h->ht;
86
87 write_lock_bh(&h->lock);
88 sk_add_node(sk, head);
89 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
90 write_unlock_bh(&h->lock);
91
92 return 0;
93}
94EXPORT_SYMBOL_GPL(smc_hash_sk);
95
96void smc_unhash_sk(struct sock *sk)
97{
98 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash;
99
100 write_lock_bh(&h->lock);
101 if (sk_del_node_init(sk))
102 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
103 write_unlock_bh(&h->lock);
104}
105EXPORT_SYMBOL_GPL(smc_unhash_sk);
106
107struct proto smc_proto = {
Ursula Braunac713872017-01-09 16:55:13 +0100108 .name = "SMC",
109 .owner = THIS_MODULE,
110 .keepalive = smc_set_keepalive,
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100111 .hash = smc_hash_sk,
112 .unhash = smc_unhash_sk,
Ursula Braunac713872017-01-09 16:55:13 +0100113 .obj_size = sizeof(struct smc_sock),
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100114 .h.smc_hash = &smc_v4_hashinfo,
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -0800115 .slab_flags = SLAB_TYPESAFE_BY_RCU,
Ursula Braunac713872017-01-09 16:55:13 +0100116};
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100117EXPORT_SYMBOL_GPL(smc_proto);
Ursula Braunac713872017-01-09 16:55:13 +0100118
Karsten Graulaaa4d332018-03-16 15:06:41 +0100119struct proto smc_proto6 = {
120 .name = "SMC6",
121 .owner = THIS_MODULE,
122 .keepalive = smc_set_keepalive,
123 .hash = smc_hash_sk,
124 .unhash = smc_unhash_sk,
125 .obj_size = sizeof(struct smc_sock),
126 .h.smc_hash = &smc_v6_hashinfo,
127 .slab_flags = SLAB_TYPESAFE_BY_RCU,
128};
129EXPORT_SYMBOL_GPL(smc_proto6);
130
Ursula Braunf536dff2019-10-23 15:44:05 +0200131static void smc_restore_fallback_changes(struct smc_sock *smc)
132{
Karsten Graul1ad24052020-07-18 15:06:18 +0200133 if (smc->clcsock->file) { /* non-accepted sockets have no file yet */
134 smc->clcsock->file->private_data = smc->sk.sk_socket;
135 smc->clcsock->file = NULL;
136 }
Ursula Braunf536dff2019-10-23 15:44:05 +0200137}
138
Ursula Braun39f41f32019-06-27 15:04:52 +0200139static int __smc_release(struct smc_sock *smc)
Ursula Braunac713872017-01-09 16:55:13 +0100140{
Ursula Braun39f41f32019-06-27 15:04:52 +0200141 struct sock *sk = &smc->sk;
Ursula Braunb38d7322017-01-09 16:55:25 +0100142 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +0100143
Ursula Braun51f1de72018-01-26 09:28:48 +0100144 if (!smc->use_fallback) {
Ursula Braunb38d7322017-01-09 16:55:25 +0100145 rc = smc_close_active(smc);
146 sock_set_flag(sk, SOCK_DEAD);
147 sk->sk_shutdown |= SHUTDOWN_MASK;
Ursula Braunb03faa12019-02-07 15:56:15 +0100148 } else {
149 if (sk->sk_state != SMC_LISTEN && sk->sk_state != SMC_INIT)
150 sock_put(sk); /* passive closing */
151 if (sk->sk_state == SMC_LISTEN) {
152 /* wake up clcsock accept */
153 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR);
154 }
155 sk->sk_state = SMC_CLOSED;
156 sk->sk_state_change(sk);
Ursula Braunf536dff2019-10-23 15:44:05 +0200157 smc_restore_fallback_changes(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +0100158 }
Cong Wang26d92e92019-01-05 23:45:26 -0800159
160 sk->sk_prot->unhash(sk);
161
Ursula Braunb03faa12019-02-07 15:56:15 +0100162 if (sk->sk_state == SMC_CLOSED) {
163 if (smc->clcsock) {
Karsten Graulfd577702019-04-11 11:17:30 +0200164 release_sock(sk);
165 smc_clcsock_release(smc);
166 lock_sock(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800167 }
Ursula Braunb03faa12019-02-07 15:56:15 +0100168 if (!smc->use_fallback)
169 smc_conn_free(&smc->conn);
Ursula Braun51f1de72018-01-26 09:28:48 +0100170 }
Ursula Braunac713872017-01-09 16:55:13 +0100171
Ursula Braun39f41f32019-06-27 15:04:52 +0200172 return rc;
173}
174
175static int smc_release(struct socket *sock)
176{
177 struct sock *sk = sock->sk;
178 struct smc_sock *smc;
179 int rc = 0;
180
181 if (!sk)
182 goto out;
183
Ursula Braun81cf4f42019-10-21 16:13:15 +0200184 sock_hold(sk); /* sock_put below */
Ursula Braun39f41f32019-06-27 15:04:52 +0200185 smc = smc_sk(sk);
186
187 /* cleanup for a dangling non-blocking connect */
188 if (smc->connect_nonblock && sk->sk_state == SMC_INIT)
189 tcp_abort(smc->clcsock->sk, ECONNABORTED);
190 flush_work(&smc->connect_work);
191
192 if (sk->sk_state == SMC_LISTEN)
193 /* smc_close_non_accepted() is called and acquires
194 * sock lock for child sockets again
195 */
196 lock_sock_nested(sk, SINGLE_DEPTH_NESTING);
197 else
198 lock_sock(sk);
199
200 rc = __smc_release(smc);
201
Ursula Braunac713872017-01-09 16:55:13 +0100202 /* detach socket */
203 sock_orphan(sk);
204 sock->sk = NULL;
205 release_sock(sk);
206
Ursula Braun81cf4f42019-10-21 16:13:15 +0200207 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +0100208 sock_put(sk); /* final sock_put */
Ursula Braunac713872017-01-09 16:55:13 +0100209out:
Ursula Braunb38d7322017-01-09 16:55:25 +0100210 return rc;
Ursula Braunac713872017-01-09 16:55:13 +0100211}
212
213static void smc_destruct(struct sock *sk)
214{
215 if (sk->sk_state != SMC_CLOSED)
216 return;
217 if (!sock_flag(sk, SOCK_DEAD))
218 return;
219
220 sk_refcnt_debug_dec(sk);
221}
222
Karsten Graulaaa4d332018-03-16 15:06:41 +0100223static struct sock *smc_sock_alloc(struct net *net, struct socket *sock,
224 int protocol)
Ursula Braunac713872017-01-09 16:55:13 +0100225{
226 struct smc_sock *smc;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100227 struct proto *prot;
Ursula Braunac713872017-01-09 16:55:13 +0100228 struct sock *sk;
229
Karsten Graulaaa4d332018-03-16 15:06:41 +0100230 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto;
231 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0);
Ursula Braunac713872017-01-09 16:55:13 +0100232 if (!sk)
233 return NULL;
234
235 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */
236 sk->sk_state = SMC_INIT;
237 sk->sk_destruct = smc_destruct;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100238 sk->sk_protocol = protocol;
Ursula Braunac713872017-01-09 16:55:13 +0100239 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100240 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work);
Ursula Braun24ac3a02018-06-27 17:59:50 +0200241 INIT_WORK(&smc->connect_work, smc_connect_work);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700242 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work);
Ursula Brauna046d572017-01-09 16:55:16 +0100243 INIT_LIST_HEAD(&smc->accept_q);
244 spin_lock_init(&smc->accept_q_lock);
Eric Dumazetbe7f3e52018-05-17 03:54:21 -0700245 spin_lock_init(&smc->conn.send_lock);
Ursula Braunf16a7dd2017-01-09 16:55:26 +0100246 sk->sk_prot->hash(sk);
Ursula Brauna046d572017-01-09 16:55:16 +0100247 sk_refcnt_debug_inc(sk);
Myungho Jung78abe3d2018-12-18 09:02:25 -0800248 mutex_init(&smc->clcsock_release_lock);
Ursula Braunac713872017-01-09 16:55:13 +0100249
250 return sk;
251}
252
253static int smc_bind(struct socket *sock, struct sockaddr *uaddr,
254 int addr_len)
255{
256 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
257 struct sock *sk = sock->sk;
258 struct smc_sock *smc;
259 int rc;
260
261 smc = smc_sk(sk);
262
263 /* replicate tests from inet_bind(), to be safe wrt. future changes */
264 rc = -EINVAL;
265 if (addr_len < sizeof(struct sockaddr_in))
266 goto out;
267
268 rc = -EAFNOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +0100269 if (addr->sin_family != AF_INET &&
270 addr->sin_family != AF_INET6 &&
271 addr->sin_family != AF_UNSPEC)
272 goto out;
Ursula Braunac713872017-01-09 16:55:13 +0100273 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */
Karsten Graulaaa4d332018-03-16 15:06:41 +0100274 if (addr->sin_family == AF_UNSPEC &&
275 addr->sin_addr.s_addr != htonl(INADDR_ANY))
Ursula Braunac713872017-01-09 16:55:13 +0100276 goto out;
277
278 lock_sock(sk);
279
280 /* Check if socket is already active */
281 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +0200282 if (sk->sk_state != SMC_INIT || smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +0100283 goto out_rel;
284
285 smc->clcsock->sk->sk_reuse = sk->sk_reuse;
286 rc = kernel_bind(smc->clcsock, uaddr, addr_len);
287
288out_rel:
289 release_sock(sk);
290out:
291 return rc;
292}
293
294static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk,
295 unsigned long mask)
296{
297 /* options we don't get control via setsockopt for */
298 nsk->sk_type = osk->sk_type;
299 nsk->sk_sndbuf = osk->sk_sndbuf;
300 nsk->sk_rcvbuf = osk->sk_rcvbuf;
301 nsk->sk_sndtimeo = osk->sk_sndtimeo;
302 nsk->sk_rcvtimeo = osk->sk_rcvtimeo;
303 nsk->sk_mark = osk->sk_mark;
304 nsk->sk_priority = osk->sk_priority;
305 nsk->sk_rcvlowat = osk->sk_rcvlowat;
306 nsk->sk_bound_dev_if = osk->sk_bound_dev_if;
307 nsk->sk_err = osk->sk_err;
308
309 nsk->sk_flags &= ~mask;
310 nsk->sk_flags |= osk->sk_flags & mask;
311}
312
313#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \
314 (1UL << SOCK_KEEPOPEN) | \
315 (1UL << SOCK_LINGER) | \
316 (1UL << SOCK_BROADCAST) | \
317 (1UL << SOCK_TIMESTAMP) | \
318 (1UL << SOCK_DBG) | \
319 (1UL << SOCK_RCVTSTAMP) | \
320 (1UL << SOCK_RCVTSTAMPNS) | \
321 (1UL << SOCK_LOCALROUTE) | \
322 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \
323 (1UL << SOCK_RXQ_OVFL) | \
324 (1UL << SOCK_WIFI_STATUS) | \
325 (1UL << SOCK_NOFCS) | \
Deepa Dinamani97184752019-02-02 07:34:51 -0800326 (1UL << SOCK_FILTER_LOCKED) | \
327 (1UL << SOCK_TSTAMP_NEW))
Ursula Braunac713872017-01-09 16:55:13 +0100328/* copy only relevant settings and flags of SOL_SOCKET level from smc to
329 * clc socket (since smc is not called for these options from net/core)
330 */
331static void smc_copy_sock_settings_to_clc(struct smc_sock *smc)
332{
333 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC);
334}
335
336#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \
337 (1UL << SOCK_KEEPOPEN) | \
338 (1UL << SOCK_LINGER) | \
339 (1UL << SOCK_DBG))
340/* copy only settings and flags relevant for smc from clc to smc socket */
341static void smc_copy_sock_settings_to_smc(struct smc_sock *smc)
342{
343 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC);
344}
345
Karsten Graulb9247542020-04-29 17:10:41 +0200346/* register the new rmb on all links */
Karsten Graul7562a132020-05-01 12:48:01 +0200347static int smcr_lgr_reg_rmbs(struct smc_link *link,
Karsten Graulb9247542020-04-29 17:10:41 +0200348 struct smc_buf_desc *rmb_desc)
349{
Karsten Graul7562a132020-05-01 12:48:01 +0200350 struct smc_link_group *lgr = link->lgr;
351 int i, rc = 0;
Karsten Graulb9247542020-04-29 17:10:41 +0200352
Karsten Grauld5500662020-05-01 12:48:05 +0200353 rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY);
354 if (rc)
355 return rc;
356 /* protect against parallel smc_llc_cli_rkey_exchange() and
357 * parallel smcr_link_reg_rmb()
358 */
359 mutex_lock(&lgr->llc_conf_mutex);
Karsten Graulb9247542020-04-29 17:10:41 +0200360 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
Karsten Graul741a49a2020-07-18 15:06:16 +0200361 if (!smc_link_active(&lgr->lnk[i]))
Karsten Graulb9247542020-04-29 17:10:41 +0200362 continue;
Karsten Graul7562a132020-05-01 12:48:01 +0200363 rc = smcr_link_reg_rmb(&lgr->lnk[i], rmb_desc);
Karsten Graulb9247542020-04-29 17:10:41 +0200364 if (rc)
Karsten Graul7562a132020-05-01 12:48:01 +0200365 goto out;
Karsten Graulb9247542020-04-29 17:10:41 +0200366 }
Karsten Graul7562a132020-05-01 12:48:01 +0200367
368 /* exchange confirm_rkey msg with peer */
369 rc = smc_llc_do_confirm_rkey(link, rmb_desc);
370 if (rc) {
371 rc = -EFAULT;
372 goto out;
373 }
374 rmb_desc->is_conf_rkey = true;
375out:
Karsten Grauld5500662020-05-01 12:48:05 +0200376 mutex_unlock(&lgr->llc_conf_mutex);
377 smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl);
Karsten Graul7562a132020-05-01 12:48:01 +0200378 return rc;
Karsten Graulb9247542020-04-29 17:10:41 +0200379}
380
381static int smcr_clnt_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100382{
Karsten Graul387707f2020-04-29 17:10:40 +0200383 struct smc_link *link = smc->conn.lnk;
Karsten Graul0fb0b022020-04-30 15:55:43 +0200384 struct smc_llc_qentry *qentry;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100385 int rc;
386
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100387 /* receive CONFIRM LINK request from server over RoCE fabric */
Karsten Graul0fb0b022020-04-30 15:55:43 +0200388 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
389 SMC_LLC_CONFIRM_LINK);
390 if (!qentry) {
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100391 struct smc_clc_msg_decline dclc;
392
393 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100394 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +0100395 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100396 }
Karsten Graul649758f2020-05-04 14:18:48 +0200397 smc_llc_save_peer_uid(qentry);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200398 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_REQ);
399 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
400 if (rc)
Karsten Graul75d320d2018-03-01 13:51:31 +0100401 return SMC_CLC_DECL_RMBE_EC;
402
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100403 rc = smc_ib_modify_qp_rts(link);
404 if (rc)
Karsten Graul603cc142018-07-25 16:35:32 +0200405 return SMC_CLC_DECL_ERR_RDYLNK;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100406
407 smc_wr_remember_qp_attr(link);
Ursula Braun652a1e42017-07-28 13:56:17 +0200408
Karsten Graul7562a132020-05-01 12:48:01 +0200409 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +0200410 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +0200411
Karsten Graul0fb0b022020-04-30 15:55:43 +0200412 /* confirm_rkey is implicit on 1st contact */
413 smc->conn.rmb_desc->is_conf_rkey = true;
414
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100415 /* send CONFIRM LINK response over RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +0200416 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP);
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100417 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +0200418 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100419
Karsten Graul0fb0b022020-04-30 15:55:43 +0200420 smc_llc_link_active(link);
Karsten Graul0a99be42020-05-05 15:01:20 +0200421 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200422
423 /* optional 2nd link, receive ADD LINK request from server */
424 qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME,
425 SMC_LLC_ADD_LINK);
426 if (!qentry) {
Karsten Graul52bedf32018-03-01 13:51:32 +0100427 struct smc_clc_msg_decline dclc;
428
429 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +0100430 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200431 if (rc == -EAGAIN)
432 rc = 0; /* no DECLINE received, go with one link */
433 return rc;
Karsten Graul52bedf32018-03-01 13:51:32 +0100434 }
Karsten Graul0fb0b022020-04-30 15:55:43 +0200435 smc_llc_flow_qentry_clr(&link->lgr->llc_flow_lcl);
Karsten Graulb1570a82020-05-03 14:38:42 +0200436 smc_llc_cli_add_link(link, qentry);
Karsten Graul75d320d2018-03-01 13:51:31 +0100437 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +0100438}
439
Hans Wippel41349842018-06-28 19:05:11 +0200440static void smcr_conn_save_peer_info(struct smc_sock *smc,
441 struct smc_clc_msg_accept_confirm *clc)
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100442{
Ursula Braun3d9725a2020-09-10 18:48:23 +0200443 int bufsize = smc_uncompress_bufsize(clc->r0.rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200444
Ursula Braun3d9725a2020-09-10 18:48:23 +0200445 smc->conn.peer_rmbe_idx = clc->r0.rmbe_idx;
446 smc->conn.local_tx_ctrl.token = ntohl(clc->r0.rmbe_alert_token);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200447 smc->conn.peer_rmbe_size = bufsize;
Ursula Brauncd6851f2017-01-09 16:55:18 +0100448 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
Hans Wippel95d8d2632018-05-18 09:34:13 +0200449 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1);
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100450}
451
Ursula Braunb81a5eb2020-09-26 12:44:31 +0200452static bool smc_isascii(char *hostname)
453{
454 int i;
455
456 for (i = 0; i < SMC_MAX_HOSTNAME_LEN; i++)
457 if (!isascii(hostname[i]))
458 return false;
459 return true;
460}
461
Hans Wippel41349842018-06-28 19:05:11 +0200462static void smcd_conn_save_peer_info(struct smc_sock *smc,
463 struct smc_clc_msg_accept_confirm *clc)
464{
Ursula Braun3d9725a2020-09-10 18:48:23 +0200465 int bufsize = smc_uncompress_bufsize(clc->d0.dmbe_size);
Hans Wippel41349842018-06-28 19:05:11 +0200466
Ursula Braun3d9725a2020-09-10 18:48:23 +0200467 smc->conn.peer_rmbe_idx = clc->d0.dmbe_idx;
468 smc->conn.peer_token = clc->d0.token;
Hans Wippel41349842018-06-28 19:05:11 +0200469 /* msg header takes up space in the buffer */
470 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg);
471 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size);
472 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx;
Ursula Braunb81a5eb2020-09-26 12:44:31 +0200473 if (clc->hdr.version > SMC_V1 &&
474 (clc->hdr.typev2 & SMC_FIRST_CONTACT_MASK)) {
475 struct smc_clc_msg_accept_confirm_v2 *clc_v2 =
476 (struct smc_clc_msg_accept_confirm_v2 *)clc;
477 struct smc_clc_first_contact_ext *fce =
478 (struct smc_clc_first_contact_ext *)
479 (((u8 *)clc_v2) + sizeof(*clc_v2));
480
481 memcpy(smc->conn.lgr->negotiated_eid, clc_v2->eid,
482 SMC_MAX_EID_LEN);
483 smc->conn.lgr->peer_os = fce->os_type;
484 smc->conn.lgr->peer_smc_release = fce->release;
485 if (smc_isascii(fce->hostname))
486 memcpy(smc->conn.lgr->peer_hostname, fce->hostname,
487 SMC_MAX_HOSTNAME_LEN);
488 }
Hans Wippel41349842018-06-28 19:05:11 +0200489}
490
491static void smc_conn_save_peer_info(struct smc_sock *smc,
492 struct smc_clc_msg_accept_confirm *clc)
493{
494 if (smc->conn.lgr->is_smcd)
495 smcd_conn_save_peer_info(smc, clc);
496 else
497 smcr_conn_save_peer_info(smc, clc);
498}
499
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100500static void smc_link_save_peer_info(struct smc_link *link,
501 struct smc_clc_msg_accept_confirm *clc)
502{
Ursula Braun3d9725a2020-09-10 18:48:23 +0200503 link->peer_qpn = ntoh24(clc->r0.qpn);
504 memcpy(link->peer_gid, clc->r0.lcl.gid, SMC_GID_SIZE);
505 memcpy(link->peer_mac, clc->r0.lcl.mac, sizeof(link->peer_mac));
506 link->peer_psn = ntoh24(clc->r0.psn);
507 link->peer_mtu = clc->r0.qp_mtu;
Ursula Braun0cfdd8f2017-01-09 16:55:17 +0100508}
509
Ursula Braun07603b22019-04-11 11:17:32 +0200510static void smc_switch_to_fallback(struct smc_sock *smc)
511{
512 smc->use_fallback = true;
513 if (smc->sk.sk_socket && smc->sk.sk_socket->file) {
514 smc->clcsock->file = smc->sk.sk_socket->file;
515 smc->clcsock->file->private_data = smc->clcsock;
Ursula Braun67f562e2020-02-14 08:58:59 +0100516 smc->clcsock->wq.fasync_list =
517 smc->sk.sk_socket->wq.fasync_list;
Ursula Braun07603b22019-04-11 11:17:32 +0200518 }
519}
520
Hans Wippel3b2dec22018-05-18 09:34:18 +0200521/* fall back during connect */
Karsten Graul603cc142018-07-25 16:35:32 +0200522static int smc_connect_fallback(struct smc_sock *smc, int reason_code)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200523{
Ursula Braun07603b22019-04-11 11:17:32 +0200524 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +0200525 smc->fallback_rsn = reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200526 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200527 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200528 if (smc->sk.sk_state == SMC_INIT)
529 smc->sk.sk_state = SMC_ACTIVE;
530 return 0;
531}
532
533/* decline and fall back during connect */
Ursula Braune8d726c82020-09-26 12:44:32 +0200534static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code,
535 u8 version)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200536{
537 int rc;
538
Ursula Braune1bbdd52018-07-05 16:15:30 +0200539 if (reason_code < 0) { /* error, fallback is not possible */
540 if (smc->sk.sk_state == SMC_INIT)
541 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200542 return reason_code;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200543 }
Karsten Graul603cc142018-07-25 16:35:32 +0200544 if (reason_code != SMC_CLC_DECL_PEERDECL) {
Ursula Braune8d726c82020-09-26 12:44:32 +0200545 rc = smc_clc_send_decline(smc, reason_code, version);
Ursula Braune1bbdd52018-07-05 16:15:30 +0200546 if (rc < 0) {
547 if (smc->sk.sk_state == SMC_INIT)
548 sock_put(&smc->sk); /* passive closing */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200549 return rc;
Ursula Braune1bbdd52018-07-05 16:15:30 +0200550 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200551 }
Karsten Graul603cc142018-07-25 16:35:32 +0200552 return smc_connect_fallback(smc, reason_code);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200553}
554
555/* abort connecting */
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200556static void smc_connect_abort(struct smc_sock *smc, int local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200557{
Ursula Braun5ac54d82020-09-10 18:48:21 +0200558 if (local_first)
Ursula Braun51e3dfa2020-02-25 16:34:36 +0100559 smc_lgr_cleanup_early(&smc->conn);
560 else
561 smc_conn_free(&smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200562}
563
564/* check if there is a rdma device available for this connection. */
565/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200566static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200567{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200568 /* PNET table look up: search active ib_device and port
569 * within same PNETID that also contains the ethernet device
570 * used for the internal TCP socket
571 */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200572 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +0200573 if (!ini->ib_dev)
574 return SMC_CLC_DECL_NOSMCRDEV;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200575 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200576}
577
Hans Wippel41349842018-06-28 19:05:11 +0200578/* check if there is an ISM device available for this connection. */
579/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200580static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200581{
582 /* Find ISM device with same PNETID as connecting interface */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200583 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
Ursula Braun3fc64932020-09-26 12:44:23 +0200584 if (!ini->ism_dev[0])
Karsten Graul9aa68d22019-04-12 12:57:29 +0200585 return SMC_CLC_DECL_NOSMCDDEV;
Ursula Braun8caaccf2020-09-26 12:44:25 +0200586 else
587 ini->ism_chid[0] = smc_ism_get_chid(ini->ism_dev[0]);
Hans Wippel41349842018-06-28 19:05:11 +0200588 return 0;
589}
590
Karsten Graul839d6962020-10-02 17:09:26 +0200591/* is chid unique for the ism devices that are already determined? */
592static bool smc_find_ism_v2_is_unique_chid(u16 chid, struct smc_init_info *ini,
593 int cnt)
594{
595 int i = (!ini->ism_dev[0]) ? 1 : 0;
596
597 for (; i < cnt; i++)
598 if (ini->ism_chid[i] == chid)
599 return false;
600 return true;
601}
602
Ursula Braund70bf4f2020-09-26 12:44:27 +0200603/* determine possible V2 ISM devices (either without PNETID or with PNETID plus
604 * PNETID matching net_device)
605 */
606static int smc_find_ism_v2_device_clnt(struct smc_sock *smc,
607 struct smc_init_info *ini)
608{
609 int rc = SMC_CLC_DECL_NOSMCDDEV;
610 struct smcd_dev *smcd;
611 int i = 1;
Karsten Graul839d6962020-10-02 17:09:26 +0200612 u16 chid;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200613
614 if (smcd_indicated(ini->smc_type_v1))
615 rc = 0; /* already initialized for V1 */
616 mutex_lock(&smcd_dev_list.mutex);
617 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
618 if (smcd->going_away || smcd == ini->ism_dev[0])
619 continue;
Karsten Graul839d6962020-10-02 17:09:26 +0200620 chid = smc_ism_get_chid(smcd);
621 if (!smc_find_ism_v2_is_unique_chid(chid, ini, i))
622 continue;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200623 if (!smc_pnet_is_pnetid_set(smcd->pnetid) ||
624 smc_pnet_is_ndev_pnetid(sock_net(&smc->sk), smcd->pnetid)) {
625 ini->ism_dev[i] = smcd;
Karsten Graul839d6962020-10-02 17:09:26 +0200626 ini->ism_chid[i] = chid;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200627 ini->is_smcd = true;
628 rc = 0;
629 i++;
630 if (i > SMC_MAX_ISM_DEVS)
631 break;
632 }
633 }
634 mutex_unlock(&smcd_dev_list.mutex);
635 ini->ism_offered_cnt = i - 1;
636 if (!ini->ism_dev[0] && !ini->ism_dev[1])
637 ini->smcd_version = 0;
638
639 return rc;
640}
641
Hans Wippel41349842018-06-28 19:05:11 +0200642/* Check for VLAN ID and register it on ISM device just for CLC handshake */
643static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200644 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200645{
Ursula Braun3fc64932020-09-26 12:44:23 +0200646 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev[0], ini->vlan_id))
Karsten Graul7a627252019-04-12 12:57:30 +0200647 return SMC_CLC_DECL_ISMVLANERR;
Hans Wippel41349842018-06-28 19:05:11 +0200648 return 0;
649}
650
Ursula Braund70bf4f2020-09-26 12:44:27 +0200651static int smc_find_proposal_devices(struct smc_sock *smc,
652 struct smc_init_info *ini)
653{
654 int rc = 0;
655
656 /* check if there is an ism device available */
657 if (ini->smcd_version & SMC_V1) {
658 if (smc_find_ism_device(smc, ini) ||
659 smc_connect_ism_vlan_setup(smc, ini)) {
660 if (ini->smc_type_v1 == SMC_TYPE_B)
661 ini->smc_type_v1 = SMC_TYPE_R;
662 else
663 ini->smc_type_v1 = SMC_TYPE_N;
664 } /* else ISM V1 is supported for this connection */
665 if (smc_find_rdma_device(smc, ini)) {
666 if (ini->smc_type_v1 == SMC_TYPE_B)
667 ini->smc_type_v1 = SMC_TYPE_D;
668 else
669 ini->smc_type_v1 = SMC_TYPE_N;
670 } /* else RDMA is supported for this connection */
671 }
672 if (smc_ism_v2_capable && smc_find_ism_v2_device_clnt(smc, ini))
673 ini->smc_type_v2 = SMC_TYPE_N;
674
675 /* if neither ISM nor RDMA are supported, fallback */
676 if (!smcr_indicated(ini->smc_type_v1) &&
677 ini->smc_type_v1 == SMC_TYPE_N && ini->smc_type_v2 == SMC_TYPE_N)
678 rc = SMC_CLC_DECL_NOSMCDEV;
679
680 return rc;
681}
682
Hans Wippel41349842018-06-28 19:05:11 +0200683/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
684 * used, the VLAN ID will be registered again during the connection setup.
685 */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200686static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200687 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200688{
Ursula Braund70bf4f2020-09-26 12:44:27 +0200689 if (!smcd_indicated(ini->smc_type_v1))
Hans Wippel41349842018-06-28 19:05:11 +0200690 return 0;
Ursula Braun3fc64932020-09-26 12:44:23 +0200691 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev[0], ini->vlan_id))
Hans Wippel41349842018-06-28 19:05:11 +0200692 return SMC_CLC_DECL_CNFERR;
693 return 0;
694}
695
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200696#define SMC_CLC_MAX_ACCEPT_LEN \
697 (sizeof(struct smc_clc_msg_accept_confirm_v2) + \
Ursula Braunb81a5eb2020-09-26 12:44:31 +0200698 sizeof(struct smc_clc_first_contact_ext) + \
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200699 sizeof(struct smc_clc_msg_trail))
700
Hans Wippel3b2dec22018-05-18 09:34:18 +0200701/* CLC handshake during connect */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200702static int smc_connect_clc(struct smc_sock *smc,
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200703 struct smc_clc_msg_accept_confirm_v2 *aclc2,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200704 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200705{
706 int rc = 0;
707
708 /* do inband token exchange */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200709 rc = smc_clc_send_proposal(smc, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200710 if (rc)
711 return rc;
712 /* receive SMC Accept CLC message */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200713 return smc_clc_wait_msg(smc, aclc2, SMC_CLC_MAX_ACCEPT_LEN,
714 SMC_CLC_ACCEPT, CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200715}
716
Ursula Brauna046d572017-01-09 16:55:16 +0100717/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200718static int smc_connect_rdma(struct smc_sock *smc,
719 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200720 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200721{
Karsten Graul0fb0b022020-04-30 15:55:43 +0200722 int i, reason_code = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200723 struct smc_link *link;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200724
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200725 ini->is_smcd = false;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200726 ini->ib_lcl = &aclc->r0.lcl;
727 ini->ib_clcqpn = ntoh24(aclc->r0.qpn);
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200728 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200729
Hans Wippel72a36a82019-02-07 15:56:18 +0100730 mutex_lock(&smc_client_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200731 reason_code = smc_conn_create(smc, ini);
732 if (reason_code) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100733 mutex_unlock(&smc_client_lgr_pending);
734 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200735 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200736
737 smc_conn_save_peer_info(smc, aclc);
738
Ursula Braun5ac54d82020-09-10 18:48:21 +0200739 if (ini->first_contact_local) {
Karsten Graul0fb0b022020-04-30 15:55:43 +0200740 link = smc->conn.lnk;
741 } else {
742 /* set link that was assigned by server */
743 link = NULL;
744 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
745 struct smc_link *l = &smc->conn.lgr->lnk[i];
746
Ursula Braun3d9725a2020-09-10 18:48:23 +0200747 if (l->peer_qpn == ntoh24(aclc->r0.qpn) &&
748 !memcmp(l->peer_gid, &aclc->r0.lcl.gid,
749 SMC_GID_SIZE) &&
750 !memcmp(l->peer_mac, &aclc->r0.lcl.mac,
751 sizeof(l->peer_mac))) {
Karsten Graul0fb0b022020-04-30 15:55:43 +0200752 link = l;
753 break;
754 }
755 }
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200756 if (!link) {
757 reason_code = SMC_CLC_DECL_NOSRVLINK;
758 goto connect_abort;
759 }
Karsten Graul0fb0b022020-04-30 15:55:43 +0200760 smc->conn.lnk = link;
761 }
762
Hans Wippel3b2dec22018-05-18 09:34:18 +0200763 /* create send buffer and rmb */
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200764 if (smc_buf_create(smc, false)) {
765 reason_code = SMC_CLC_DECL_MEM;
766 goto connect_abort;
767 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200768
Ursula Braun5ac54d82020-09-10 18:48:21 +0200769 if (ini->first_contact_local)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200770 smc_link_save_peer_info(link, aclc);
771
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200772 if (smc_rmb_rtoken_handling(&smc->conn, link, aclc)) {
773 reason_code = SMC_CLC_DECL_ERR_RTOK;
774 goto connect_abort;
775 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200776
777 smc_close_init(smc);
778 smc_rx_init(smc);
779
Ursula Braun5ac54d82020-09-10 18:48:21 +0200780 if (ini->first_contact_local) {
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200781 if (smc_ib_ready_link(link)) {
782 reason_code = SMC_CLC_DECL_ERR_RDYLNK;
783 goto connect_abort;
784 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200785 } else {
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200786 if (smcr_lgr_reg_rmbs(link, smc->conn.rmb_desc)) {
787 reason_code = SMC_CLC_DECL_ERR_REGRMB;
788 goto connect_abort;
789 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200790 }
791 smc_rmb_sync_sg_for_device(&smc->conn);
792
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200793 reason_code = smc_clc_send_confirm(smc, ini->first_contact_local,
794 SMC_V1);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200795 if (reason_code)
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200796 goto connect_abort;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200797
798 smc_tx_init(smc);
799
Ursula Braun5ac54d82020-09-10 18:48:21 +0200800 if (ini->first_contact_local) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200801 /* QP confirmation over RoCE fabric */
Karsten Graul0fb0b022020-04-30 15:55:43 +0200802 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
Karsten Graulb9247542020-04-29 17:10:41 +0200803 reason_code = smcr_clnt_conf_first_link(smc);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200804 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200805 if (reason_code)
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200806 goto connect_abort;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200807 }
Hans Wippel72a36a82019-02-07 15:56:18 +0100808 mutex_unlock(&smc_client_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200809
810 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200811 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200812 if (smc->sk.sk_state == SMC_INIT)
813 smc->sk.sk_state = SMC_ACTIVE;
814
815 return 0;
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200816connect_abort:
817 smc_connect_abort(smc, ini->first_contact_local);
818 mutex_unlock(&smc_client_lgr_pending);
819 smc->connect_nonblock = 0;
820
821 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200822}
823
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200824/* The server has chosen one of the proposed ISM devices for the communication.
825 * Determine from the CHID of the received CLC ACCEPT the ISM device chosen.
826 */
827static int
828smc_v2_determine_accepted_chid(struct smc_clc_msg_accept_confirm_v2 *aclc,
829 struct smc_init_info *ini)
830{
831 int i;
832
833 for (i = 0; i < ini->ism_offered_cnt + 1; i++) {
834 if (ini->ism_chid[i] == ntohs(aclc->chid)) {
835 ini->ism_selected = i;
836 return 0;
837 }
838 }
839
840 return -EPROTO;
841}
842
Hans Wippel41349842018-06-28 19:05:11 +0200843/* setup for ISM connection of client */
844static int smc_connect_ism(struct smc_sock *smc,
845 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200846 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200847{
Hans Wippel41349842018-06-28 19:05:11 +0200848 int rc = 0;
849
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200850 ini->is_smcd = true;
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200851 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200852
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200853 if (aclc->hdr.version == SMC_V2) {
854 struct smc_clc_msg_accept_confirm_v2 *aclc_v2 =
855 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
856
857 rc = smc_v2_determine_accepted_chid(aclc_v2, ini);
858 if (rc)
859 return rc;
860 }
861 ini->ism_peer_gid[ini->ism_selected] = aclc->d0.gid;
862
Hans Wippel72a36a82019-02-07 15:56:18 +0100863 /* there is only one lgr role for SMC-D; use server lock */
864 mutex_lock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200865 rc = smc_conn_create(smc, ini);
866 if (rc) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100867 mutex_unlock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200868 return rc;
Hans Wippel72a36a82019-02-07 15:56:18 +0100869 }
Hans Wippel41349842018-06-28 19:05:11 +0200870
871 /* Create send and receive buffers */
Karsten Graul72b7f6c2020-07-26 20:34:28 +0200872 rc = smc_buf_create(smc, true);
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200873 if (rc) {
874 rc = (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB : SMC_CLC_DECL_MEM;
875 goto connect_abort;
876 }
Hans Wippel41349842018-06-28 19:05:11 +0200877
878 smc_conn_save_peer_info(smc, aclc);
879 smc_close_init(smc);
880 smc_rx_init(smc);
881 smc_tx_init(smc);
882
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200883 rc = smc_clc_send_confirm(smc, ini->first_contact_local,
884 aclc->hdr.version);
Hans Wippel41349842018-06-28 19:05:11 +0200885 if (rc)
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200886 goto connect_abort;
Hans Wippel72a36a82019-02-07 15:56:18 +0100887 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel41349842018-06-28 19:05:11 +0200888
889 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200890 smc->connect_nonblock = 0;
Hans Wippel41349842018-06-28 19:05:11 +0200891 if (smc->sk.sk_state == SMC_INIT)
892 smc->sk.sk_state = SMC_ACTIVE;
893
894 return 0;
Karsten Graulc60a2ce2020-10-07 22:57:41 +0200895connect_abort:
896 smc_connect_abort(smc, ini->first_contact_local);
897 mutex_unlock(&smc_server_lgr_pending);
898 smc->connect_nonblock = 0;
899
900 return rc;
Hans Wippel41349842018-06-28 19:05:11 +0200901}
902
Ursula Braund70bf4f2020-09-26 12:44:27 +0200903/* check if received accept type and version matches a proposed one */
904static int smc_connect_check_aclc(struct smc_init_info *ini,
905 struct smc_clc_msg_accept_confirm *aclc)
906{
907 if ((aclc->hdr.typev1 == SMC_TYPE_R &&
908 !smcr_indicated(ini->smc_type_v1)) ||
909 (aclc->hdr.typev1 == SMC_TYPE_D &&
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200910 ((!smcd_indicated(ini->smc_type_v1) &&
911 !smcd_indicated(ini->smc_type_v2)) ||
912 (aclc->hdr.version == SMC_V1 &&
913 !smcd_indicated(ini->smc_type_v1)) ||
914 (aclc->hdr.version == SMC_V2 &&
915 !smcd_indicated(ini->smc_type_v2)))))
Ursula Braund70bf4f2020-09-26 12:44:27 +0200916 return SMC_CLC_DECL_MODEUNSUPP;
917
918 return 0;
919}
920
Hans Wippel3b2dec22018-05-18 09:34:18 +0200921/* perform steps before actually connecting */
922static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100923{
Ursula Braune8d726c82020-09-26 12:44:32 +0200924 u8 version = smc_ism_v2_capable ? SMC_V2 : SMC_V1;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200925 struct smc_clc_msg_accept_confirm_v2 *aclc2;
926 struct smc_clc_msg_accept_confirm *aclc;
Ursula Braun3fc64932020-09-26 12:44:23 +0200927 struct smc_init_info *ini = NULL;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200928 u8 *buf = NULL;
Ursula Brauna046d572017-01-09 16:55:16 +0100929 int rc = 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100930
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200931 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +0200932 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200933
Hans Wippel3b2dec22018-05-18 09:34:18 +0200934 /* if peer has not signalled SMC-capability, fall back */
935 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +0200936 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200937
Ursula Braune8d726c82020-09-26 12:44:32 +0200938 /* IPSec connections opt out of SMC optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200939 if (using_ipsec(smc))
Ursula Braune8d726c82020-09-26 12:44:32 +0200940 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC,
941 version);
Ursula Brauna046d572017-01-09 16:55:16 +0100942
Ursula Braun3fc64932020-09-26 12:44:23 +0200943 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
944 if (!ini)
Ursula Braune8d726c82020-09-26 12:44:32 +0200945 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_MEM,
946 version);
Ursula Braun3fc64932020-09-26 12:44:23 +0200947
Ursula Braund70bf4f2020-09-26 12:44:27 +0200948 ini->smcd_version = SMC_V1;
949 ini->smcd_version |= smc_ism_v2_capable ? SMC_V2 : 0;
950 ini->smc_type_v1 = SMC_TYPE_B;
951 ini->smc_type_v2 = smc_ism_v2_capable ? SMC_TYPE_D : SMC_TYPE_N;
952
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200953 /* get vlan id from IP device */
Ursula Braun3fc64932020-09-26 12:44:23 +0200954 if (smc_vlan_by_tcpsk(smc->clcsock, ini)) {
Ursula Braund70bf4f2020-09-26 12:44:27 +0200955 ini->smcd_version &= ~SMC_V1;
956 ini->smc_type_v1 = SMC_TYPE_N;
957 if (!ini->smcd_version) {
958 rc = SMC_CLC_DECL_GETVLANERR;
959 goto fallback;
960 }
Ursula Braun3fc64932020-09-26 12:44:23 +0200961 }
Hans Wippel41349842018-06-28 19:05:11 +0200962
Ursula Braund70bf4f2020-09-26 12:44:27 +0200963 rc = smc_find_proposal_devices(smc, ini);
964 if (rc)
965 goto fallback;
Ursula Brauna046d572017-01-09 16:55:16 +0100966
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200967 buf = kzalloc(SMC_CLC_MAX_ACCEPT_LEN, GFP_KERNEL);
968 if (!buf) {
969 rc = SMC_CLC_DECL_MEM;
970 goto fallback;
971 }
972 aclc2 = (struct smc_clc_msg_accept_confirm_v2 *)buf;
973 aclc = (struct smc_clc_msg_accept_confirm *)aclc2;
974
Hans Wippel3b2dec22018-05-18 09:34:18 +0200975 /* perform CLC handshake */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200976 rc = smc_connect_clc(smc, aclc2, ini);
Ursula Braund70bf4f2020-09-26 12:44:27 +0200977 if (rc)
978 goto vlan_cleanup;
979
980 /* check if smc modes and versions of CLC proposal and accept match */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200981 rc = smc_connect_check_aclc(ini, aclc);
Ursula Braune8d726c82020-09-26 12:44:32 +0200982 version = aclc->hdr.version == SMC_V1 ? SMC_V1 : version;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200983 if (rc)
984 goto vlan_cleanup;
Ursula Brauna046d572017-01-09 16:55:16 +0100985
Hans Wippel41349842018-06-28 19:05:11 +0200986 /* depending on previous steps, connect using rdma or ism */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200987 if (aclc->hdr.typev1 == SMC_TYPE_R)
988 rc = smc_connect_rdma(smc, aclc, ini);
989 else if (aclc->hdr.typev1 == SMC_TYPE_D)
990 rc = smc_connect_ism(smc, aclc, ini);
Ursula Braund70bf4f2020-09-26 12:44:27 +0200991 if (rc)
992 goto vlan_cleanup;
Ursula Brauna046d572017-01-09 16:55:16 +0100993
Ursula Braund70bf4f2020-09-26 12:44:27 +0200994 smc_connect_ism_vlan_cleanup(smc, ini);
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200995 kfree(buf);
Ursula Braun3fc64932020-09-26 12:44:23 +0200996 kfree(ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200997 return 0;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200998
999vlan_cleanup:
1000 smc_connect_ism_vlan_cleanup(smc, ini);
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001001 kfree(buf);
Ursula Braund70bf4f2020-09-26 12:44:27 +02001002fallback:
1003 kfree(ini);
Ursula Braune8d726c82020-09-26 12:44:32 +02001004 return smc_connect_decline_fallback(smc, rc, version);
Ursula Brauna046d572017-01-09 16:55:16 +01001005}
1006
Ursula Braun24ac3a02018-06-27 17:59:50 +02001007static void smc_connect_work(struct work_struct *work)
1008{
1009 struct smc_sock *smc = container_of(work, struct smc_sock,
1010 connect_work);
Ursula Braun50717a32019-04-12 12:57:23 +02001011 long timeo = smc->sk.sk_sndtimeo;
1012 int rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001013
Ursula Braun50717a32019-04-12 12:57:23 +02001014 if (!timeo)
1015 timeo = MAX_SCHEDULE_TIMEOUT;
1016 lock_sock(smc->clcsock->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +02001017 if (smc->clcsock->sk->sk_err) {
1018 smc->sk.sk_err = smc->clcsock->sk->sk_err;
Ursula Braun50717a32019-04-12 12:57:23 +02001019 } else if ((1 << smc->clcsock->sk->sk_state) &
1020 (TCPF_SYN_SENT | TCP_SYN_RECV)) {
1021 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
1022 if ((rc == -EPIPE) &&
1023 ((1 << smc->clcsock->sk->sk_state) &
1024 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
1025 rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001026 }
Ursula Braun50717a32019-04-12 12:57:23 +02001027 release_sock(smc->clcsock->sk);
1028 lock_sock(&smc->sk);
1029 if (rc != 0 || smc->sk.sk_err) {
1030 smc->sk.sk_state = SMC_CLOSED;
1031 if (rc == -EPIPE || rc == -EAGAIN)
1032 smc->sk.sk_err = EPIPE;
1033 else if (signal_pending(current))
1034 smc->sk.sk_err = -sock_intr_errno(timeo);
Ursula Braun6d6dd522019-11-12 16:03:41 +01001035 sock_put(&smc->sk); /* passive closing */
Ursula Braun24ac3a02018-06-27 17:59:50 +02001036 goto out;
1037 }
1038
1039 rc = __smc_connect(smc);
1040 if (rc < 0)
1041 smc->sk.sk_err = -rc;
1042
1043out:
Ursula Braun07603b22019-04-11 11:17:32 +02001044 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
1045 if (smc->sk.sk_err) {
1046 smc->sk.sk_state_change(&smc->sk);
1047 } else { /* allow polling before and after fallback decision */
1048 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
1049 smc->sk.sk_write_space(&smc->sk);
1050 }
1051 }
Ursula Braun24ac3a02018-06-27 17:59:50 +02001052 release_sock(&smc->sk);
1053}
1054
Ursula Braunac713872017-01-09 16:55:13 +01001055static int smc_connect(struct socket *sock, struct sockaddr *addr,
1056 int alen, int flags)
1057{
1058 struct sock *sk = sock->sk;
1059 struct smc_sock *smc;
1060 int rc = -EINVAL;
1061
1062 smc = smc_sk(sk);
1063
1064 /* separate smc parameter checking to be safe */
1065 if (alen < sizeof(addr->sa_family))
1066 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001067 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +01001068 goto out_err;
1069
1070 lock_sock(sk);
1071 switch (sk->sk_state) {
1072 default:
1073 goto out;
1074 case SMC_ACTIVE:
1075 rc = -EISCONN;
1076 goto out;
1077 case SMC_INIT:
1078 rc = 0;
1079 break;
1080 }
1081
1082 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001083 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun50717a32019-04-12 12:57:23 +02001084 if (smc->connect_nonblock) {
1085 rc = -EALREADY;
1086 goto out;
1087 }
1088 rc = kernel_connect(smc->clcsock, addr, alen, flags);
1089 if (rc && rc != -EINPROGRESS)
1090 goto out;
Ursula Braun301428e2019-10-29 12:41:26 +01001091
1092 sock_hold(&smc->sk); /* sock put in passive closing */
Ursula Braun86434742019-12-12 22:35:58 +01001093 if (smc->use_fallback)
1094 goto out;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001095 if (flags & O_NONBLOCK) {
Karsten Graul22ef4732020-09-10 18:48:29 +02001096 if (queue_work(smc_hs_wq, &smc->connect_work))
Ursula Braun50717a32019-04-12 12:57:23 +02001097 smc->connect_nonblock = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001098 rc = -EINPROGRESS;
1099 } else {
Ursula Braun24ac3a02018-06-27 17:59:50 +02001100 rc = __smc_connect(smc);
1101 if (rc < 0)
1102 goto out;
1103 else
1104 rc = 0; /* success cases including fallback */
1105 }
Ursula Braunac713872017-01-09 16:55:13 +01001106
1107out:
1108 release_sock(sk);
1109out_err:
1110 return rc;
1111}
1112
1113static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
1114{
Ursula Braun3163c502018-01-24 10:28:12 +01001115 struct socket *new_clcsock = NULL;
1116 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +01001117 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -08001118 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +01001119
Ursula Braun3163c502018-01-24 10:28:12 +01001120 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001121 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001122 if (!new_sk) {
1123 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +01001124 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +01001125 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +01001126 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +01001127 goto out;
1128 }
1129 *new_smc = smc_sk(new_sk);
1130
Myungho Jung78abe3d2018-12-18 09:02:25 -08001131 mutex_lock(&lsmc->clcsock_release_lock);
1132 if (lsmc->clcsock)
Ursula Brauna60a2b12020-09-10 18:48:20 +02001133 rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK);
Myungho Jung78abe3d2018-12-18 09:02:25 -08001134 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +01001135 lock_sock(lsk);
Ursula Brauna60a2b12020-09-10 18:48:20 +02001136 if (rc < 0 && rc != -EAGAIN)
Ursula Braun3163c502018-01-24 10:28:12 +01001137 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +01001138 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +02001139 new_sk->sk_prot->unhash(new_sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001140 if (new_clcsock)
1141 sock_release(new_clcsock);
1142 new_sk->sk_state = SMC_CLOSED;
1143 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun51f1de72018-01-26 09:28:48 +01001144 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +01001145 *new_smc = NULL;
1146 goto out;
1147 }
1148
Ursula Brauna60a2b12020-09-10 18:48:20 +02001149 /* new clcsock has inherited the smc listen-specific sk_data_ready
1150 * function; switch it back to the original sk_data_ready function
1151 */
1152 new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready;
Ursula Braunac713872017-01-09 16:55:13 +01001153 (*new_smc)->clcsock = new_clcsock;
1154out:
1155 return rc;
1156}
1157
Ursula Brauna046d572017-01-09 16:55:16 +01001158/* add a just created sock to the accept queue of the listen sock as
1159 * candidate for a following socket accept call from user space
1160 */
1161static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
1162{
1163 struct smc_sock *par = smc_sk(parent);
1164
Ursula Braun51f1de72018-01-26 09:28:48 +01001165 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +01001166 spin_lock(&par->accept_q_lock);
1167 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
1168 spin_unlock(&par->accept_q_lock);
1169 sk_acceptq_added(parent);
1170}
1171
1172/* remove a socket from the accept queue of its parental listening socket */
1173static void smc_accept_unlink(struct sock *sk)
1174{
1175 struct smc_sock *par = smc_sk(sk)->listen_smc;
1176
1177 spin_lock(&par->accept_q_lock);
1178 list_del_init(&smc_sk(sk)->accept_q);
1179 spin_unlock(&par->accept_q_lock);
1180 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001181 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +01001182}
1183
1184/* remove a sock from the accept queue to bind it to a new socket created
1185 * for a socket accept call from user space
1186 */
Ursula Braunb38d7322017-01-09 16:55:25 +01001187struct sock *smc_accept_dequeue(struct sock *parent,
1188 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +01001189{
1190 struct smc_sock *isk, *n;
1191 struct sock *new_sk;
1192
1193 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
1194 new_sk = (struct sock *)isk;
1195
1196 smc_accept_unlink(new_sk);
1197 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +02001198 new_sk->sk_prot->unhash(new_sk);
Ursula Braun127f4972018-01-26 09:28:49 +01001199 if (isk->clcsock) {
1200 sock_release(isk->clcsock);
1201 isk->clcsock = NULL;
1202 }
Ursula Braun51f1de72018-01-26 09:28:48 +01001203 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +01001204 continue;
1205 }
Ursula Braun07603b22019-04-11 11:17:32 +02001206 if (new_sock) {
Ursula Brauna046d572017-01-09 16:55:16 +01001207 sock_graft(new_sk, new_sock);
Ursula Braun07603b22019-04-11 11:17:32 +02001208 if (isk->use_fallback) {
1209 smc_sk(new_sk)->clcsock->file = new_sock->file;
1210 isk->clcsock->file->private_data = isk->clcsock;
1211 }
1212 }
Ursula Brauna046d572017-01-09 16:55:16 +01001213 return new_sk;
1214 }
1215 return NULL;
1216}
1217
1218/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +01001219void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +01001220{
1221 struct smc_sock *smc = smc_sk(sk);
1222
Ursula Braun81cf4f42019-10-21 16:13:15 +02001223 sock_hold(sk); /* sock_put below */
Ursula Braunb38d7322017-01-09 16:55:25 +01001224 lock_sock(sk);
1225 if (!sk->sk_lingertime)
1226 /* wait for peer closing */
1227 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun39f41f32019-06-27 15:04:52 +02001228 __smc_release(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +01001229 release_sock(sk);
Ursula Braun81cf4f42019-10-21 16:13:15 +02001230 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +01001231 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +01001232}
1233
Karsten Graulb9247542020-04-29 17:10:41 +02001234static int smcr_serv_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001235{
Karsten Graul387707f2020-04-29 17:10:40 +02001236 struct smc_link *link = smc->conn.lnk;
Karsten Graul4667bb42020-04-30 15:55:42 +02001237 struct smc_llc_qentry *qentry;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001238 int rc;
1239
Karsten Graul7562a132020-05-01 12:48:01 +02001240 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +02001241 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +02001242
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001243 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +02001244 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001245 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001246 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001247
1248 /* receive CONFIRM LINK response from client over the RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001249 qentry = smc_llc_wait(link->lgr, link, SMC_LLC_WAIT_TIME,
1250 SMC_LLC_CONFIRM_LINK);
1251 if (!qentry) {
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001252 struct smc_clc_msg_decline dclc;
1253
1254 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001255 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001256 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001257 }
Karsten Graul649758f2020-05-04 14:18:48 +02001258 smc_llc_save_peer_uid(qentry);
Karsten Graul4667bb42020-04-30 15:55:42 +02001259 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_RESP);
1260 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
1261 if (rc)
Karsten Graul75d320d2018-03-01 13:51:31 +01001262 return SMC_CLC_DECL_RMBE_EC;
1263
Karsten Graul4667bb42020-04-30 15:55:42 +02001264 /* confirm_rkey is implicit on 1st contact */
1265 smc->conn.rmb_desc->is_conf_rkey = true;
Karsten Graul52bedf32018-03-01 13:51:32 +01001266
Karsten Graul00a049c2020-04-29 17:10:49 +02001267 smc_llc_link_active(link);
Karsten Graul0a99be42020-05-05 15:01:20 +02001268 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
Karsten Graul52bedf32018-03-01 13:51:32 +01001269
Karsten Graul4667bb42020-04-30 15:55:42 +02001270 /* initial contact - try to establish second link */
Karsten Graul2d2209f2020-05-03 14:38:43 +02001271 smc_llc_srv_add_link(link);
Karsten Graul75d320d2018-03-01 13:51:31 +01001272 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001273}
1274
Hans Wippel3b2dec22018-05-18 09:34:18 +02001275/* listen worker: finish */
1276static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001277{
Ursula Brauna046d572017-01-09 16:55:16 +01001278 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001279 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001280
Ursula Brauna046d572017-01-09 16:55:16 +01001281 if (lsmc->sk.sk_state == SMC_LISTEN) {
Karsten Graulfd577702019-04-11 11:17:30 +02001282 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001283 smc_accept_enqueue(&lsmc->sk, newsmcsk);
Karsten Graulfd577702019-04-11 11:17:30 +02001284 release_sock(&lsmc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001285 } else { /* no longer listening */
1286 smc_close_non_accepted(newsmcsk);
1287 }
Ursula Brauna046d572017-01-09 16:55:16 +01001288
1289 /* Wake up accept */
1290 lsmc->sk.sk_data_ready(&lsmc->sk);
1291 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001292}
Ursula Brauna046d572017-01-09 16:55:16 +01001293
Hans Wippel3b2dec22018-05-18 09:34:18 +02001294/* listen worker: finish in state connected */
1295static void smc_listen_out_connected(struct smc_sock *new_smc)
1296{
1297 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001298
Hans Wippel3b2dec22018-05-18 09:34:18 +02001299 sk_refcnt_debug_inc(newsmcsk);
1300 if (newsmcsk->sk_state == SMC_INIT)
1301 newsmcsk->sk_state = SMC_ACTIVE;
1302
1303 smc_listen_out(new_smc);
1304}
1305
1306/* listen worker: finish in error state */
1307static void smc_listen_out_err(struct smc_sock *new_smc)
1308{
1309 struct sock *newsmcsk = &new_smc->sk;
1310
Ursula Braun51f1de72018-01-26 09:28:48 +01001311 if (newsmcsk->sk_state == SMC_INIT)
1312 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001313 newsmcsk->sk_state = SMC_CLOSED;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001314
1315 smc_listen_out(new_smc);
1316}
1317
1318/* listen worker: decline and fall back if possible */
1319static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
Ursula Braune8d726c82020-09-26 12:44:32 +02001320 struct smc_init_info *ini, u8 version)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001321{
1322 /* RDMA setup failed, switch back to TCP */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001323 if (ini->first_contact_local)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001324 smc_lgr_cleanup_early(&new_smc->conn);
1325 else
1326 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001327 if (reason_code < 0) { /* error, no fallback possible */
1328 smc_listen_out_err(new_smc);
1329 return;
1330 }
Ursula Braun07603b22019-04-11 11:17:32 +02001331 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001332 new_smc->fallback_rsn = reason_code;
1333 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Ursula Braune8d726c82020-09-26 12:44:32 +02001334 if (smc_clc_send_decline(new_smc, reason_code, version) < 0) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001335 smc_listen_out_err(new_smc);
1336 return;
1337 }
1338 }
1339 smc_listen_out_connected(new_smc);
1340}
1341
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001342/* listen worker: version checking */
1343static int smc_listen_v2_check(struct smc_sock *new_smc,
1344 struct smc_clc_msg_proposal *pclc,
1345 struct smc_init_info *ini)
1346{
1347 struct smc_clc_smcd_v2_extension *pclc_smcd_v2_ext;
1348 struct smc_clc_v2_extension *pclc_v2_ext;
1349
1350 ini->smc_type_v1 = pclc->hdr.typev1;
1351 ini->smc_type_v2 = pclc->hdr.typev2;
1352 ini->smcd_version = ini->smc_type_v1 != SMC_TYPE_N ? SMC_V1 : 0;
1353 if (pclc->hdr.version > SMC_V1)
1354 ini->smcd_version |=
1355 ini->smc_type_v2 != SMC_TYPE_N ? SMC_V2 : 0;
1356 if (!smc_ism_v2_capable) {
1357 ini->smcd_version &= ~SMC_V2;
1358 goto out;
1359 }
1360 pclc_v2_ext = smc_get_clc_v2_ext(pclc);
1361 if (!pclc_v2_ext) {
1362 ini->smcd_version &= ~SMC_V2;
1363 goto out;
1364 }
1365 pclc_smcd_v2_ext = smc_get_clc_smcd_v2_ext(pclc_v2_ext);
1366 if (!pclc_smcd_v2_ext)
1367 ini->smcd_version &= ~SMC_V2;
1368
1369out:
1370 if (!ini->smcd_version) {
1371 if (pclc->hdr.typev1 == SMC_TYPE_B ||
1372 pclc->hdr.typev2 == SMC_TYPE_B)
1373 return SMC_CLC_DECL_NOSMCDEV;
1374 if (pclc->hdr.typev1 == SMC_TYPE_D ||
1375 pclc->hdr.typev2 == SMC_TYPE_D)
1376 return SMC_CLC_DECL_NOSMCDDEV;
1377 return SMC_CLC_DECL_NOSMCRDEV;
1378 }
1379
1380 return 0;
1381}
1382
Hans Wippel3b2dec22018-05-18 09:34:18 +02001383/* listen worker: check prefixes */
Karsten Graul59886692019-04-12 12:57:25 +02001384static int smc_listen_prfx_check(struct smc_sock *new_smc,
Hans Wippel3b2dec22018-05-18 09:34:18 +02001385 struct smc_clc_msg_proposal *pclc)
1386{
1387 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1388 struct socket *newclcsock = new_smc->clcsock;
1389
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001390 if (pclc->hdr.typev1 == SMC_TYPE_N)
1391 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001392 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1393 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
Karsten Graul59886692019-04-12 12:57:25 +02001394 return SMC_CLC_DECL_DIFFPREFIX;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001395
1396 return 0;
1397}
1398
1399/* listen worker: initialize connection and buffers */
1400static int smc_listen_rdma_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001401 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001402{
Karsten Graul7a627252019-04-12 12:57:30 +02001403 int rc;
1404
Hans Wippel3b2dec22018-05-18 09:34:18 +02001405 /* allocate connection / link group */
Karsten Graul7a627252019-04-12 12:57:30 +02001406 rc = smc_conn_create(new_smc, ini);
1407 if (rc)
1408 return rc;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001409
1410 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001411 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001412 return SMC_CLC_DECL_MEM;
1413
1414 return 0;
1415}
1416
Hans Wippel41349842018-06-28 19:05:11 +02001417/* listen worker: initialize connection and buffers for SMC-D */
1418static int smc_listen_ism_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001419 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +02001420{
Karsten Graul7a627252019-04-12 12:57:30 +02001421 int rc;
Hans Wippel41349842018-06-28 19:05:11 +02001422
Karsten Graul7a627252019-04-12 12:57:30 +02001423 rc = smc_conn_create(new_smc, ini);
1424 if (rc)
1425 return rc;
Hans Wippel41349842018-06-28 19:05:11 +02001426
Hans Wippel41349842018-06-28 19:05:11 +02001427 /* Create send and receive buffers */
Karsten Graul72b7f6c2020-07-26 20:34:28 +02001428 rc = smc_buf_create(new_smc, true);
1429 if (rc) {
Ursula Braun5ac54d82020-09-10 18:48:21 +02001430 if (ini->first_contact_local)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001431 smc_lgr_cleanup_early(&new_smc->conn);
1432 else
1433 smc_conn_free(&new_smc->conn);
Karsten Graul72b7f6c2020-07-26 20:34:28 +02001434 return (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB :
1435 SMC_CLC_DECL_MEM;
Hans Wippel41349842018-06-28 19:05:11 +02001436 }
1437
1438 return 0;
1439}
1440
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001441static bool smc_is_already_selected(struct smcd_dev *smcd,
1442 struct smc_init_info *ini,
1443 int matches)
1444{
1445 int i;
1446
1447 for (i = 0; i < matches; i++)
1448 if (smcd == ini->ism_dev[i])
1449 return true;
1450
1451 return false;
1452}
1453
1454/* check for ISM devices matching proposed ISM devices */
1455static void smc_check_ism_v2_match(struct smc_init_info *ini,
1456 u16 proposed_chid, u64 proposed_gid,
1457 unsigned int *matches)
1458{
1459 struct smcd_dev *smcd;
1460
1461 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
1462 if (smcd->going_away)
1463 continue;
1464 if (smc_is_already_selected(smcd, ini, *matches))
1465 continue;
1466 if (smc_ism_get_chid(smcd) == proposed_chid &&
1467 !smc_ism_cantalk(proposed_gid, ISM_RESERVED_VLANID, smcd)) {
1468 ini->ism_peer_gid[*matches] = proposed_gid;
1469 ini->ism_dev[*matches] = smcd;
1470 (*matches)++;
1471 break;
1472 }
1473 }
1474}
1475
1476static void smc_find_ism_v2_device_serv(struct smc_sock *new_smc,
1477 struct smc_clc_msg_proposal *pclc,
1478 struct smc_init_info *ini)
1479{
1480 struct smc_clc_smcd_v2_extension *smcd_v2_ext;
1481 struct smc_clc_v2_extension *smc_v2_ext;
1482 struct smc_clc_msg_smcd *pclc_smcd;
1483 unsigned int matches = 0;
Karsten Graulf29fa002020-10-07 22:57:43 +02001484 u8 smcd_version;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001485 u8 *eid = NULL;
1486 int i;
1487
1488 if (!(ini->smcd_version & SMC_V2) || !smcd_indicated(ini->smc_type_v2))
Karsten Graulf29fa002020-10-07 22:57:43 +02001489 goto not_found;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001490
1491 pclc_smcd = smc_get_clc_msg_smcd(pclc);
1492 smc_v2_ext = smc_get_clc_v2_ext(pclc);
1493 smcd_v2_ext = smc_get_clc_smcd_v2_ext(smc_v2_ext);
1494 if (!smcd_v2_ext ||
1495 !smc_v2_ext->hdr.flag.seid) /* no system EID support for SMCD */
1496 goto not_found;
1497
1498 mutex_lock(&smcd_dev_list.mutex);
1499 if (pclc_smcd->ism.chid)
1500 /* check for ISM device matching proposed native ISM device */
1501 smc_check_ism_v2_match(ini, ntohs(pclc_smcd->ism.chid),
1502 ntohll(pclc_smcd->ism.gid), &matches);
1503 for (i = 1; i <= smc_v2_ext->hdr.ism_gid_cnt; i++) {
1504 /* check for ISM devices matching proposed non-native ISM
1505 * devices
1506 */
1507 smc_check_ism_v2_match(ini,
1508 ntohs(smcd_v2_ext->gidchid[i - 1].chid),
1509 ntohll(smcd_v2_ext->gidchid[i - 1].gid),
1510 &matches);
1511 }
1512 mutex_unlock(&smcd_dev_list.mutex);
1513
1514 if (ini->ism_dev[0]) {
1515 smc_ism_get_system_eid(ini->ism_dev[0], &eid);
1516 if (memcmp(eid, smcd_v2_ext->system_eid, SMC_MAX_EID_LEN))
1517 goto not_found;
1518 } else {
1519 goto not_found;
1520 }
1521
1522 /* separate - outside the smcd_dev_list.lock */
Karsten Graulf29fa002020-10-07 22:57:43 +02001523 smcd_version = ini->smcd_version;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001524 for (i = 0; i < matches; i++) {
1525 ini->smcd_version = SMC_V2;
1526 ini->is_smcd = true;
1527 ini->ism_selected = i;
1528 if (smc_listen_ism_init(new_smc, ini))
1529 /* try next active ISM device */
1530 continue;
1531 return; /* matching and usable V2 ISM device found */
1532 }
Karsten Graulf29fa002020-10-07 22:57:43 +02001533 /* no V2 ISM device could be initialized */
1534 ini->smcd_version = smcd_version; /* restore original value */
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001535
1536not_found:
1537 ini->smcd_version &= ~SMC_V2;
1538 ini->ism_dev[0] = NULL;
1539 ini->is_smcd = false;
1540}
1541
1542static void smc_find_ism_v1_device_serv(struct smc_sock *new_smc,
1543 struct smc_clc_msg_proposal *pclc,
1544 struct smc_init_info *ini)
Ursula Braun7affc802020-09-26 12:44:21 +02001545{
1546 struct smc_clc_msg_smcd *pclc_smcd = smc_get_clc_msg_smcd(pclc);
1547
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001548 /* check if ISM V1 is available */
1549 if (!(ini->smcd_version & SMC_V1) || !smcd_indicated(ini->smc_type_v1))
Ursula Braun7affc802020-09-26 12:44:21 +02001550 goto not_found;
1551 ini->is_smcd = true; /* prepare ISM check */
Ursula Braun8c3dca32020-09-26 12:44:28 +02001552 ini->ism_peer_gid[0] = ntohll(pclc_smcd->ism.gid);
Ursula Braun7affc802020-09-26 12:44:21 +02001553 if (smc_find_ism_device(new_smc, ini))
1554 goto not_found;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001555 ini->ism_selected = 0;
Ursula Braun7affc802020-09-26 12:44:21 +02001556 if (!smc_listen_ism_init(new_smc, ini))
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001557 return; /* V1 ISM device found */
Ursula Braun7affc802020-09-26 12:44:21 +02001558
1559not_found:
Ursula Braun3fc64932020-09-26 12:44:23 +02001560 ini->ism_dev[0] = NULL;
Ursula Braun7affc802020-09-26 12:44:21 +02001561 ini->is_smcd = false;
1562}
1563
Hans Wippel3b2dec22018-05-18 09:34:18 +02001564/* listen worker: register buffers */
Ursula Braun5ac54d82020-09-10 18:48:21 +02001565static int smc_listen_rdma_reg(struct smc_sock *new_smc, bool local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001566{
Karsten Graulb9247542020-04-29 17:10:41 +02001567 struct smc_connection *conn = &new_smc->conn;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001568
Ursula Braun5ac54d82020-09-10 18:48:21 +02001569 if (!local_first) {
Karsten Graul7562a132020-05-01 12:48:01 +02001570 if (smcr_lgr_reg_rmbs(conn->lnk, conn->rmb_desc))
Karsten Graulc7674c02018-11-22 10:26:43 +01001571 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001572 }
1573 smc_rmb_sync_sg_for_device(&new_smc->conn);
1574
1575 return 0;
1576}
1577
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001578static int smc_find_rdma_v1_device_serv(struct smc_sock *new_smc,
1579 struct smc_clc_msg_proposal *pclc,
1580 struct smc_init_info *ini)
Ursula Braun7affc802020-09-26 12:44:21 +02001581{
1582 int rc;
1583
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001584 if (!smcr_indicated(ini->smc_type_v1))
Ursula Braun7affc802020-09-26 12:44:21 +02001585 return SMC_CLC_DECL_NOSMCDEV;
1586
1587 /* prepare RDMA check */
1588 ini->ib_lcl = &pclc->lcl;
1589 rc = smc_find_rdma_device(new_smc, ini);
1590 if (rc) {
1591 /* no RDMA device found */
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001592 if (ini->smc_type_v1 == SMC_TYPE_B)
Ursula Braun7affc802020-09-26 12:44:21 +02001593 /* neither ISM nor RDMA device found */
1594 rc = SMC_CLC_DECL_NOSMCDEV;
1595 return rc;
1596 }
1597 rc = smc_listen_rdma_init(new_smc, ini);
1598 if (rc)
1599 return rc;
1600 return smc_listen_rdma_reg(new_smc, ini->first_contact_local);
1601}
1602
1603/* determine the local device matching to proposal */
1604static int smc_listen_find_device(struct smc_sock *new_smc,
1605 struct smc_clc_msg_proposal *pclc,
1606 struct smc_init_info *ini)
1607{
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001608 int rc;
1609
1610 /* check for ISM device matching V2 proposed device */
1611 smc_find_ism_v2_device_serv(new_smc, pclc, ini);
1612 if (ini->ism_dev[0])
Ursula Braun7affc802020-09-26 12:44:21 +02001613 return 0;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001614
1615 if (!(ini->smcd_version & SMC_V1))
1616 return SMC_CLC_DECL_NOSMCDEV;
1617
1618 /* check for matching IP prefix and subnet length */
1619 rc = smc_listen_prfx_check(new_smc, pclc);
1620 if (rc)
1621 return rc;
1622
1623 /* get vlan id from IP device */
1624 if (smc_vlan_by_tcpsk(new_smc->clcsock, ini))
1625 return SMC_CLC_DECL_GETVLANERR;
1626
1627 /* check for ISM device matching V1 proposed device */
1628 smc_find_ism_v1_device_serv(new_smc, pclc, ini);
1629 if (ini->ism_dev[0])
1630 return 0;
1631
Ursula Braun7affc802020-09-26 12:44:21 +02001632 if (pclc->hdr.typev1 == SMC_TYPE_D)
1633 return SMC_CLC_DECL_NOSMCDDEV; /* skip RDMA and decline */
1634
1635 /* check if RDMA is available */
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001636 return smc_find_rdma_v1_device_serv(new_smc, pclc, ini);
Ursula Braun7affc802020-09-26 12:44:21 +02001637}
1638
Hans Wippel3b2dec22018-05-18 09:34:18 +02001639/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001640static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1641 struct smc_clc_msg_accept_confirm *cclc,
Ursula Braun5ac54d82020-09-10 18:48:21 +02001642 bool local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001643{
Karsten Graul387707f2020-04-29 17:10:40 +02001644 struct smc_link *link = new_smc->conn.lnk;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001645 int reason_code = 0;
1646
Ursula Braun5ac54d82020-09-10 18:48:21 +02001647 if (local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001648 smc_link_save_peer_info(link, cclc);
1649
Ursula Braun0c881ad2020-09-10 18:48:25 +02001650 if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc))
1651 return SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001652
Ursula Braun5ac54d82020-09-10 18:48:21 +02001653 if (local_first) {
Ursula Braun0c881ad2020-09-10 18:48:25 +02001654 if (smc_ib_ready_link(link))
1655 return SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001656 /* QP confirmation over RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001657 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
Karsten Graulb9247542020-04-29 17:10:41 +02001658 reason_code = smcr_serv_conf_first_link(new_smc);
Karsten Graul4667bb42020-04-30 15:55:42 +02001659 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001660 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001661 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001662}
1663
Ursula Braun7affc802020-09-26 12:44:21 +02001664/* setup for connection of server */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001665static void smc_listen_work(struct work_struct *work)
1666{
1667 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1668 smc_listen_work);
Ursula Braune8d726c82020-09-26 12:44:32 +02001669 u8 version = smc_ism_v2_capable ? SMC_V2 : SMC_V1;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001670 struct socket *newclcsock = new_smc->clcsock;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001671 struct smc_clc_msg_accept_confirm *cclc;
Ursula Braun6bb14e482020-09-10 18:48:22 +02001672 struct smc_clc_msg_proposal_area *buf;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001673 struct smc_clc_msg_proposal *pclc;
Ursula Braun3fc64932020-09-26 12:44:23 +02001674 struct smc_init_info *ini = NULL;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001675 int rc = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001676
Karsten Graulfd577702019-04-11 11:17:30 +02001677 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1678 return smc_listen_out_err(new_smc);
1679
Hans Wippel3b2dec22018-05-18 09:34:18 +02001680 if (new_smc->use_fallback) {
1681 smc_listen_out_connected(new_smc);
1682 return;
1683 }
1684
1685 /* check if peer is smc capable */
1686 if (!tcp_sk(newclcsock->sk)->syn_smc) {
Ursula Braun07603b22019-04-11 11:17:32 +02001687 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001688 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001689 smc_listen_out_connected(new_smc);
1690 return;
1691 }
1692
1693 /* do inband token exchange -
1694 * wait for and receive SMC Proposal CLC message
1695 */
Ursula Braun6bb14e482020-09-10 18:48:22 +02001696 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
1697 if (!buf) {
1698 rc = SMC_CLC_DECL_MEM;
1699 goto out_decl;
1700 }
1701 pclc = (struct smc_clc_msg_proposal *)buf;
1702 rc = smc_clc_wait_msg(new_smc, pclc, sizeof(*buf),
Karsten Graul228bae02019-04-12 12:57:28 +02001703 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001704 if (rc)
1705 goto out_decl;
Ursula Braune8d726c82020-09-26 12:44:32 +02001706 version = pclc->hdr.version == SMC_V1 ? SMC_V1 : version;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001707
Ursula Braune8d726c82020-09-26 12:44:32 +02001708 /* IPSec connections opt out of SMC optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001709 if (using_ipsec(new_smc)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001710 rc = SMC_CLC_DECL_IPSEC;
1711 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001712 }
1713
Ursula Braun3fc64932020-09-26 12:44:23 +02001714 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
1715 if (!ini) {
1716 rc = SMC_CLC_DECL_MEM;
1717 goto out_decl;
1718 }
1719
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001720 /* initial version checking */
1721 rc = smc_listen_v2_check(new_smc, pclc, ini);
1722 if (rc)
Karsten Graul9aa68d22019-04-12 12:57:29 +02001723 goto out_decl;
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001724
Hans Wippel72a36a82019-02-07 15:56:18 +01001725 mutex_lock(&smc_server_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001726 smc_close_init(new_smc);
1727 smc_rx_init(new_smc);
1728 smc_tx_init(new_smc);
1729
Ursula Braun7affc802020-09-26 12:44:21 +02001730 /* determine ISM or RoCE device used for connection */
Ursula Braun3fc64932020-09-26 12:44:23 +02001731 rc = smc_listen_find_device(new_smc, pclc, ini);
Ursula Braun7affc802020-09-26 12:44:21 +02001732 if (rc)
1733 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001734
1735 /* send SMC Accept CLC message */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001736 rc = smc_clc_send_accept(new_smc, ini->first_contact_local,
1737 ini->smcd_version == SMC_V2 ? SMC_V2 : SMC_V1);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001738 if (rc)
1739 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001740
Hans Wippel62c71392019-02-07 15:56:17 +01001741 /* SMC-D does not need this lock any more */
Ursula Braun3fc64932020-09-26 12:44:23 +02001742 if (ini->is_smcd)
Hans Wippel72a36a82019-02-07 15:56:18 +01001743 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001744
Hans Wippel3b2dec22018-05-18 09:34:18 +02001745 /* receive SMC Confirm CLC message */
Karsten Graul9047a617dc2020-10-07 22:57:42 +02001746 memset(buf, 0, sizeof(*buf));
1747 cclc = (struct smc_clc_msg_accept_confirm *)buf;
1748 rc = smc_clc_wait_msg(new_smc, cclc, sizeof(*buf),
Karsten Graul228bae02019-04-12 12:57:28 +02001749 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
1750 if (rc) {
Ursula Braun3fc64932020-09-26 12:44:23 +02001751 if (!ini->is_smcd)
Karsten Graul9aa68d22019-04-12 12:57:29 +02001752 goto out_unlock;
1753 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001754 }
1755
1756 /* finish worker */
Ursula Braun3fc64932020-09-26 12:44:23 +02001757 if (!ini->is_smcd) {
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001758 rc = smc_listen_rdma_finish(new_smc, cclc,
Ursula Braun3fc64932020-09-26 12:44:23 +02001759 ini->first_contact_local);
Hans Wippel62c71392019-02-07 15:56:17 +01001760 if (rc)
Ursula Braun0c881ad2020-09-10 18:48:25 +02001761 goto out_unlock;
1762 mutex_unlock(&smc_server_lgr_pending);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001763 }
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001764 smc_conn_save_peer_info(new_smc, cclc);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001765 smc_listen_out_connected(new_smc);
Ursula Braunac679362020-09-17 22:46:02 +02001766 goto out_free;
Karsten Graul9aa68d22019-04-12 12:57:29 +02001767
1768out_unlock:
1769 mutex_unlock(&smc_server_lgr_pending);
1770out_decl:
Ursula Braune8d726c82020-09-26 12:44:32 +02001771 smc_listen_decline(new_smc, rc, ini, version);
Ursula Braunac679362020-09-17 22:46:02 +02001772out_free:
Ursula Braun3fc64932020-09-26 12:44:23 +02001773 kfree(ini);
Ursula Braun6bb14e482020-09-10 18:48:22 +02001774 kfree(buf);
Ursula Brauna046d572017-01-09 16:55:16 +01001775}
1776
1777static void smc_tcp_listen_work(struct work_struct *work)
1778{
1779 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1780 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001781 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001782 struct smc_sock *new_smc;
1783 int rc = 0;
1784
Ursula Braun3163c502018-01-24 10:28:12 +01001785 lock_sock(lsk);
1786 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001787 rc = smc_clcsock_accept(lsmc, &new_smc);
Ursula Brauna60a2b12020-09-10 18:48:20 +02001788 if (rc) /* clcsock accept queue empty or error */
Ursula Brauna046d572017-01-09 16:55:16 +01001789 goto out;
1790 if (!new_smc)
1791 continue;
1792
1793 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001794 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001795 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001796 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001797 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1798 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001799 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1800 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001801 sock_hold(&new_smc->sk); /* sock_put in passive closing */
Karsten Graul22ef4732020-09-10 18:48:29 +02001802 if (!queue_work(smc_hs_wq, &new_smc->smc_listen_work))
Ursula Braun51f1de72018-01-26 09:28:48 +01001803 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001804 }
1805
1806out:
Ursula Braun3163c502018-01-24 10:28:12 +01001807 release_sock(lsk);
Ursula Brauna60a2b12020-09-10 18:48:20 +02001808 sock_put(&lsmc->sk); /* sock_hold in smc_clcsock_data_ready() */
1809}
1810
1811static void smc_clcsock_data_ready(struct sock *listen_clcsock)
1812{
1813 struct smc_sock *lsmc;
1814
1815 lsmc = (struct smc_sock *)
1816 ((uintptr_t)listen_clcsock->sk_user_data & ~SK_USER_DATA_NOCOPY);
1817 if (!lsmc)
1818 return;
1819 lsmc->clcsk_data_ready(listen_clcsock);
1820 if (lsmc->sk.sk_state == SMC_LISTEN) {
1821 sock_hold(&lsmc->sk); /* sock_put in smc_tcp_listen_work() */
Karsten Graul22ef4732020-09-10 18:48:29 +02001822 if (!queue_work(smc_hs_wq, &lsmc->tcp_listen_work))
Ursula Brauna60a2b12020-09-10 18:48:20 +02001823 sock_put(&lsmc->sk);
1824 }
Ursula Brauna046d572017-01-09 16:55:16 +01001825}
1826
Ursula Braunac713872017-01-09 16:55:13 +01001827static int smc_listen(struct socket *sock, int backlog)
1828{
1829 struct sock *sk = sock->sk;
1830 struct smc_sock *smc;
1831 int rc;
1832
1833 smc = smc_sk(sk);
1834 lock_sock(sk);
1835
1836 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +02001837 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
1838 smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +01001839 goto out;
1840
1841 rc = 0;
1842 if (sk->sk_state == SMC_LISTEN) {
1843 sk->sk_max_ack_backlog = backlog;
1844 goto out;
1845 }
1846 /* some socket options are handled in core, so we could not apply
1847 * them to the clc socket -- copy smc socket options to clc socket
1848 */
1849 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001850 if (!smc->use_fallback)
1851 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001852
Ursula Brauna60a2b12020-09-10 18:48:20 +02001853 /* save original sk_data_ready function and establish
1854 * smc-specific sk_data_ready function
1855 */
1856 smc->clcsk_data_ready = smc->clcsock->sk->sk_data_ready;
1857 smc->clcsock->sk->sk_data_ready = smc_clcsock_data_ready;
1858 smc->clcsock->sk->sk_user_data =
1859 (void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY);
Ursula Braunac713872017-01-09 16:55:13 +01001860 rc = kernel_listen(smc->clcsock, backlog);
1861 if (rc)
1862 goto out;
1863 sk->sk_max_ack_backlog = backlog;
1864 sk->sk_ack_backlog = 0;
1865 sk->sk_state = SMC_LISTEN;
1866
1867out:
1868 release_sock(sk);
1869 return rc;
1870}
1871
1872static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001873 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001874{
Ursula Brauna046d572017-01-09 16:55:16 +01001875 struct sock *sk = sock->sk, *nsk;
1876 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001877 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001878 long timeo;
1879 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001880
1881 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001882 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001883 lock_sock(sk);
1884
1885 if (lsmc->sk.sk_state != SMC_LISTEN) {
1886 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001887 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001888 goto out;
1889 }
1890
Ursula Brauna046d572017-01-09 16:55:16 +01001891 /* Wait for an incoming connection */
1892 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1893 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1894 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1895 set_current_state(TASK_INTERRUPTIBLE);
1896 if (!timeo) {
1897 rc = -EAGAIN;
1898 break;
1899 }
1900 release_sock(sk);
1901 timeo = schedule_timeout(timeo);
1902 /* wakeup by sk_data_ready in smc_listen_work() */
1903 sched_annotate_sleep();
1904 lock_sock(sk);
1905 if (signal_pending(current)) {
1906 rc = sock_intr_errno(timeo);
1907 break;
1908 }
1909 }
1910 set_current_state(TASK_RUNNING);
1911 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001912
Ursula Brauna046d572017-01-09 16:55:16 +01001913 if (!rc)
1914 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001915 release_sock(sk);
1916 if (rc)
1917 goto out;
1918
1919 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1920 /* wait till data arrives on the socket */
1921 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1922 MSEC_PER_SEC);
1923 if (smc_sk(nsk)->use_fallback) {
1924 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1925
1926 lock_sock(clcsk);
1927 if (skb_queue_empty(&clcsk->sk_receive_queue))
1928 sk_wait_data(clcsk, &timeo, NULL);
1929 release_sock(clcsk);
1930 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1931 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001932 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001933 release_sock(nsk);
1934 }
1935 }
Ursula Braunac713872017-01-09 16:55:13 +01001936
1937out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001938 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001939 return rc;
1940}
1941
1942static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001943 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001944{
1945 struct smc_sock *smc;
1946
Ursula Braunb38d7322017-01-09 16:55:25 +01001947 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1948 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001949 return -ENOTCONN;
1950
1951 smc = smc_sk(sock->sk);
1952
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001953 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001954}
1955
1956static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1957{
1958 struct sock *sk = sock->sk;
1959 struct smc_sock *smc;
1960 int rc = -EPIPE;
1961
1962 smc = smc_sk(sk);
1963 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001964 if ((sk->sk_state != SMC_ACTIVE) &&
1965 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1966 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001967 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001968
1969 if (msg->msg_flags & MSG_FASTOPEN) {
Ursula Brauncd206362019-08-02 10:47:50 +02001970 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001971 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001972 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001973 } else {
1974 rc = -EINVAL;
1975 goto out;
1976 }
1977 }
1978
Ursula Braunac713872017-01-09 16:55:13 +01001979 if (smc->use_fallback)
1980 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1981 else
Ursula Braune6727f32017-01-09 16:55:23 +01001982 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001983out:
1984 release_sock(sk);
1985 return rc;
1986}
1987
1988static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1989 int flags)
1990{
1991 struct sock *sk = sock->sk;
1992 struct smc_sock *smc;
1993 int rc = -ENOTCONN;
1994
1995 smc = smc_sk(sk);
1996 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001997 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1998 /* socket was connected before, no more data to read */
1999 rc = 0;
2000 goto out;
2001 }
Ursula Braunb38d7322017-01-09 16:55:25 +01002002 if ((sk->sk_state == SMC_INIT) ||
2003 (sk->sk_state == SMC_LISTEN) ||
2004 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01002005 goto out;
2006
Ursula Braunb38d7322017-01-09 16:55:25 +01002007 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2008 rc = 0;
2009 goto out;
2010 }
2011
Stefan Raspl9014db22018-05-03 18:12:39 +02002012 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01002013 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02002014 } else {
2015 msg->msg_namelen = 0;
2016 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
2017 }
Ursula Braunb38d7322017-01-09 16:55:25 +01002018
Ursula Braunac713872017-01-09 16:55:13 +01002019out:
2020 release_sock(sk);
2021 return rc;
2022}
2023
Al Viroade994f2017-07-03 00:01:49 -04002024static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01002025{
Ursula Braun8dce2782018-01-26 09:28:47 +01002026 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05002027 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01002028
Ursula Braun8dce2782018-01-26 09:28:47 +01002029 spin_lock(&isk->accept_q_lock);
2030 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002031 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01002032 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01002033
Ursula Braun8dce2782018-01-26 09:28:47 +01002034 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01002035}
2036
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07002037static __poll_t smc_poll(struct file *file, struct socket *sock,
2038 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01002039{
2040 struct sock *sk = sock->sk;
Ursula Braunac713872017-01-09 16:55:13 +01002041 struct smc_sock *smc;
Ursula Braun50717a32019-04-12 12:57:23 +02002042 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01002043
Ursula Braun8dce2782018-01-26 09:28:47 +01002044 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002045 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01002046
Ursula Braunac713872017-01-09 16:55:13 +01002047 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02002048 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01002049 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07002050 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02002051 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braunac713872017-01-09 16:55:13 +01002052 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07002053 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02002054 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01002055 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002056 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01002057 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
2058 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002059 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01002060 if (sk->sk_state == SMC_LISTEN) {
2061 /* woken up by sk_data_ready in smc_listen_work() */
Ursula Braun50717a32019-04-12 12:57:23 +02002062 mask |= smc_accept_poll(sk);
2063 } else if (smc->use_fallback) { /* as result of connect_work()*/
2064 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
2065 wait);
2066 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun8dce2782018-01-26 09:28:47 +01002067 } else {
Ursula Braun50717a32019-04-12 12:57:23 +02002068 if ((sk->sk_state != SMC_INIT &&
2069 atomic_read(&smc->conn.sndbuf_space)) ||
Ursula Braun8dce2782018-01-26 09:28:47 +01002070 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002071 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01002072 } else {
2073 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
2074 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
2075 }
2076 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002077 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01002078 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002079 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01002080 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002081 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02002082 if (smc->conn.urg_state == SMC_URG_VALID)
2083 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01002084 }
Ursula Braunac713872017-01-09 16:55:13 +01002085 }
2086
2087 return mask;
2088}
2089
2090static int smc_shutdown(struct socket *sock, int how)
2091{
2092 struct sock *sk = sock->sk;
2093 struct smc_sock *smc;
2094 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01002095 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01002096
2097 smc = smc_sk(sk);
2098
2099 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01002100 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01002101
2102 lock_sock(sk);
2103
2104 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02002105 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01002106 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
2107 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
2108 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2109 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
2110 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01002111 goto out;
2112 if (smc->use_fallback) {
2113 rc = kernel_sock_shutdown(smc->clcsock, how);
2114 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
2115 if (sk->sk_shutdown == SHUTDOWN_MASK)
2116 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01002117 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01002118 }
Ursula Braunb38d7322017-01-09 16:55:25 +01002119 switch (how) {
2120 case SHUT_RDWR: /* shutdown in both directions */
2121 rc = smc_close_active(smc);
2122 break;
2123 case SHUT_WR:
2124 rc = smc_close_shutdown_write(smc);
2125 break;
2126 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02002127 rc = 0;
2128 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01002129 break;
2130 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02002131 if (smc->clcsock)
2132 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01002133 /* map sock_shutdown_cmd constants to sk_shutdown value range */
2134 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01002135
2136out:
2137 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01002138 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01002139}
2140
2141static int smc_setsockopt(struct socket *sock, int level, int optname,
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02002142 sockptr_t optval, unsigned int optlen)
Ursula Braunac713872017-01-09 16:55:13 +01002143{
2144 struct sock *sk = sock->sk;
2145 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002146 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01002147
2148 smc = smc_sk(sk);
2149
2150 /* generic setsockopts reaching us here always apply to the
2151 * CLC socket
2152 */
Christoph Hellwiga44d9e72020-07-17 08:23:31 +02002153 if (unlikely(!smc->clcsock->ops->setsockopt))
2154 rc = -EOPNOTSUPP;
2155 else
2156 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
2157 optval, optlen);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002158 if (smc->clcsock->sk->sk_err) {
2159 sk->sk_err = smc->clcsock->sk->sk_err;
2160 sk->sk_error_report(sk);
2161 }
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002162
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002163 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00002164 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02002165 if (copy_from_sockptr(&val, optval, sizeof(int)))
Ursula Braunac0107e2018-07-18 15:22:50 +02002166 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002167
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002168 lock_sock(sk);
Ursula Braun86434742019-12-12 22:35:58 +01002169 if (rc || smc->use_fallback)
2170 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002171 switch (optname) {
2172 case TCP_ULP:
2173 case TCP_FASTOPEN:
2174 case TCP_FASTOPEN_CONNECT:
2175 case TCP_FASTOPEN_KEY:
2176 case TCP_FASTOPEN_NO_COOKIE:
2177 /* option not supported by SMC */
Ursula Braun8204df72019-11-15 12:39:30 +01002178 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02002179 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02002180 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002181 } else {
Ursula Braun86434742019-12-12 22:35:58 +01002182 rc = -EINVAL;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002183 }
2184 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002185 case TCP_NODELAY:
Ursula Braunf9cedf12019-08-02 10:16:38 +02002186 if (sk->sk_state != SMC_INIT &&
2187 sk->sk_state != SMC_LISTEN &&
2188 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01002189 if (val)
Karsten Graul22ef4732020-09-10 18:48:29 +02002190 mod_delayed_work(smc->conn.lgr->tx_wq,
2191 &smc->conn.tx_work, 0);
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002192 }
2193 break;
2194 case TCP_CORK:
Ursula Braunf9cedf12019-08-02 10:16:38 +02002195 if (sk->sk_state != SMC_INIT &&
2196 sk->sk_state != SMC_LISTEN &&
2197 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01002198 if (!val)
Karsten Graul22ef4732020-09-10 18:48:29 +02002199 mod_delayed_work(smc->conn.lgr->tx_wq,
2200 &smc->conn.tx_work, 0);
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002201 }
2202 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02002203 case TCP_DEFER_ACCEPT:
2204 smc->sockopt_defer_accept = val;
2205 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002206 default:
2207 break;
2208 }
Ursula Braun86434742019-12-12 22:35:58 +01002209out:
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002210 release_sock(sk);
2211
2212 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01002213}
2214
2215static int smc_getsockopt(struct socket *sock, int level, int optname,
2216 char __user *optval, int __user *optlen)
2217{
2218 struct smc_sock *smc;
2219
2220 smc = smc_sk(sock->sk);
2221 /* socket options apply to the CLC socket */
Christoph Hellwiga44d9e72020-07-17 08:23:31 +02002222 if (unlikely(!smc->clcsock->ops->getsockopt))
2223 return -EOPNOTSUPP;
Ursula Braunac713872017-01-09 16:55:13 +01002224 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
2225 optval, optlen);
2226}
2227
2228static int smc_ioctl(struct socket *sock, unsigned int cmd,
2229 unsigned long arg)
2230{
Stefan Rasplde8474e2018-05-23 16:38:11 +02002231 union smc_host_cursor cons, urg;
2232 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01002233 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02002234 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01002235
2236 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02002237 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02002238 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02002239 if (smc->use_fallback) {
2240 if (!smc->clcsock) {
2241 release_sock(&smc->sk);
2242 return -EBADF;
2243 }
2244 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
2245 release_sock(&smc->sk);
2246 return answ;
2247 }
Ursula Braun9b67e262018-05-02 16:56:46 +02002248 switch (cmd) {
2249 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02002250 if (smc->sk.sk_state == SMC_LISTEN) {
2251 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002252 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002253 }
Ursula Braun2351abe2018-05-23 16:38:09 +02002254 if (smc->sk.sk_state == SMC_INIT ||
2255 smc->sk.sk_state == SMC_CLOSED)
2256 answ = 0;
2257 else
2258 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02002259 break;
2260 case SIOCOUTQ:
2261 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02002262 if (smc->sk.sk_state == SMC_LISTEN) {
2263 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002264 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002265 }
Ursula Braun2351abe2018-05-23 16:38:09 +02002266 if (smc->sk.sk_state == SMC_INIT ||
2267 smc->sk.sk_state == SMC_CLOSED)
2268 answ = 0;
2269 else
2270 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02002271 atomic_read(&smc->conn.sndbuf_space);
2272 break;
2273 case SIOCOUTQNSD:
2274 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02002275 if (smc->sk.sk_state == SMC_LISTEN) {
2276 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002277 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002278 }
Ursula Braun2351abe2018-05-23 16:38:09 +02002279 if (smc->sk.sk_state == SMC_INIT ||
2280 smc->sk.sk_state == SMC_CLOSED)
2281 answ = 0;
2282 else
2283 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02002284 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02002285 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02002286 if (smc->sk.sk_state == SMC_LISTEN) {
2287 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02002288 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002289 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02002290 if (smc->sk.sk_state == SMC_INIT ||
2291 smc->sk.sk_state == SMC_CLOSED) {
2292 answ = 0;
2293 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02002294 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
2295 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02002296 answ = smc_curs_diff(conn->rmb_desc->len,
2297 &cons, &urg) == 1;
2298 }
2299 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02002300 default:
Ursula Braun1992d992018-07-16 13:56:52 +02002301 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002302 return -ENOIOCTLCMD;
2303 }
Ursula Braun1992d992018-07-16 13:56:52 +02002304 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002305
2306 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01002307}
2308
2309static ssize_t smc_sendpage(struct socket *sock, struct page *page,
2310 int offset, size_t size, int flags)
2311{
2312 struct sock *sk = sock->sk;
2313 struct smc_sock *smc;
2314 int rc = -EPIPE;
2315
2316 smc = smc_sk(sk);
2317 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02002318 if (sk->sk_state != SMC_ACTIVE) {
2319 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01002320 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02002321 }
2322 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01002323 if (smc->use_fallback)
2324 rc = kernel_sendpage(smc->clcsock, page, offset,
2325 size, flags);
2326 else
2327 rc = sock_no_sendpage(sock, page, offset, size, flags);
2328
2329out:
Ursula Braunac713872017-01-09 16:55:13 +01002330 return rc;
2331}
2332
Stefan Raspl9014db22018-05-03 18:12:39 +02002333/* Map the affected portions of the rmbe into an spd, note the number of bytes
2334 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
2335 * updates till whenever a respective page has been fully processed.
2336 * Note that subsequent recv() calls have to wait till all splice() processing
2337 * completed.
2338 */
Ursula Braunac713872017-01-09 16:55:13 +01002339static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
2340 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02002341 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01002342{
2343 struct sock *sk = sock->sk;
2344 struct smc_sock *smc;
2345 int rc = -ENOTCONN;
2346
2347 smc = smc_sk(sk);
2348 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01002349 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2350 /* socket was connected before, no more data to read */
2351 rc = 0;
2352 goto out;
2353 }
Stefan Raspl9014db22018-05-03 18:12:39 +02002354 if (sk->sk_state == SMC_INIT ||
2355 sk->sk_state == SMC_LISTEN ||
2356 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01002357 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02002358
2359 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2360 rc = 0;
2361 goto out;
2362 }
2363
Ursula Braunac713872017-01-09 16:55:13 +01002364 if (smc->use_fallback) {
2365 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
2366 pipe, len, flags);
2367 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02002368 if (*ppos) {
2369 rc = -ESPIPE;
2370 goto out;
2371 }
2372 if (flags & SPLICE_F_NONBLOCK)
2373 flags = MSG_DONTWAIT;
2374 else
2375 flags = 0;
2376 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01002377 }
2378out:
2379 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02002380
Ursula Braunac713872017-01-09 16:55:13 +01002381 return rc;
2382}
2383
2384/* must look like tcp */
2385static const struct proto_ops smc_sock_ops = {
2386 .family = PF_SMC,
2387 .owner = THIS_MODULE,
2388 .release = smc_release,
2389 .bind = smc_bind,
2390 .connect = smc_connect,
2391 .socketpair = sock_no_socketpair,
2392 .accept = smc_accept,
2393 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07002394 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01002395 .ioctl = smc_ioctl,
2396 .listen = smc_listen,
2397 .shutdown = smc_shutdown,
2398 .setsockopt = smc_setsockopt,
2399 .getsockopt = smc_getsockopt,
2400 .sendmsg = smc_sendmsg,
2401 .recvmsg = smc_recvmsg,
2402 .mmap = sock_no_mmap,
2403 .sendpage = smc_sendpage,
2404 .splice_read = smc_splice_read,
2405};
2406
2407static int smc_create(struct net *net, struct socket *sock, int protocol,
2408 int kern)
2409{
Karsten Graulaaa4d332018-03-16 15:06:41 +01002410 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01002411 struct smc_sock *smc;
2412 struct sock *sk;
2413 int rc;
2414
2415 rc = -ESOCKTNOSUPPORT;
2416 if (sock->type != SOCK_STREAM)
2417 goto out;
2418
2419 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002420 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01002421 goto out;
2422
2423 rc = -ENOBUFS;
2424 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002425 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01002426 if (!sk)
2427 goto out;
2428
2429 /* create internal TCP socket for CLC handshake and fallback */
2430 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01002431 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02002432 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002433 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
2434 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01002435 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01002436 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01002437 goto out;
2438 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01002439 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
2440 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01002441
2442out:
2443 return rc;
2444}
2445
2446static const struct net_proto_family smc_sock_family_ops = {
2447 .family = PF_SMC,
2448 .owner = THIS_MODULE,
2449 .create = smc_create,
2450};
2451
Hans Wippel64e28b52019-02-21 13:01:02 +01002452unsigned int smc_net_id;
2453
2454static __net_init int smc_net_init(struct net *net)
2455{
2456 return smc_pnet_net_init(net);
2457}
2458
2459static void __net_exit smc_net_exit(struct net *net)
2460{
2461 smc_pnet_net_exit(net);
2462}
2463
2464static struct pernet_operations smc_net_ops = {
2465 .init = smc_net_init,
2466 .exit = smc_net_exit,
2467 .id = &smc_net_id,
2468 .size = sizeof(struct smc_net),
2469};
2470
Ursula Braunac713872017-01-09 16:55:13 +01002471static int __init smc_init(void)
2472{
2473 int rc;
2474
Hans Wippel64e28b52019-02-21 13:01:02 +01002475 rc = register_pernet_subsys(&smc_net_ops);
2476 if (rc)
2477 return rc;
2478
Ursula Braun201091e2020-09-26 12:44:24 +02002479 smc_ism_init();
Ursula Braunb81a5eb2020-09-26 12:44:31 +02002480 smc_clc_init();
Ursula Braun201091e2020-09-26 12:44:24 +02002481
Thomas Richter6812baa2017-01-09 16:55:15 +01002482 rc = smc_pnet_init();
2483 if (rc)
YueHaibing8c33bf12019-06-26 17:47:50 +02002484 goto out_pernet_subsys;
Thomas Richter6812baa2017-01-09 16:55:15 +01002485
Karsten Graul22ef4732020-09-10 18:48:29 +02002486 rc = -ENOMEM;
2487 smc_hs_wq = alloc_workqueue("smc_hs_wq", 0, 0);
2488 if (!smc_hs_wq)
2489 goto out_pnet;
2490
2491 smc_close_wq = alloc_workqueue("smc_close_wq", 0, 0);
2492 if (!smc_close_wq)
2493 goto out_alloc_hs_wq;
2494
Ursula Braun6dabd402019-11-16 17:47:29 +01002495 rc = smc_core_init();
2496 if (rc) {
2497 pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
Karsten Graul22ef4732020-09-10 18:48:29 +02002498 goto out_alloc_wqs;
Ursula Braun6dabd402019-11-16 17:47:29 +01002499 }
2500
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002501 rc = smc_llc_init();
2502 if (rc) {
2503 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002504 goto out_core;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002505 }
2506
Ursula Braun5f083182017-01-09 16:55:22 +01002507 rc = smc_cdc_init();
2508 if (rc) {
2509 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002510 goto out_core;
Ursula Braun5f083182017-01-09 16:55:22 +01002511 }
2512
Ursula Braunac713872017-01-09 16:55:13 +01002513 rc = proto_register(&smc_proto, 1);
2514 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01002515 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002516 goto out_core;
Ursula Braunac713872017-01-09 16:55:13 +01002517 }
2518
Karsten Graulaaa4d332018-03-16 15:06:41 +01002519 rc = proto_register(&smc_proto6, 1);
2520 if (rc) {
2521 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2522 goto out_proto;
2523 }
2524
Ursula Braunac713872017-01-09 16:55:13 +01002525 rc = sock_register(&smc_sock_family_ops);
2526 if (rc) {
2527 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002528 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01002529 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01002530 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002531 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01002532
Ursula Brauna4cf0442017-01-09 16:55:14 +01002533 rc = smc_ib_register_client();
2534 if (rc) {
2535 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2536 goto out_sock;
2537 }
2538
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002539 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002540 return 0;
2541
Ursula Brauna4cf0442017-01-09 16:55:14 +01002542out_sock:
2543 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002544out_proto6:
2545 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002546out_proto:
2547 proto_unregister(&smc_proto);
Ursula Braun6dabd402019-11-16 17:47:29 +01002548out_core:
2549 smc_core_exit();
Karsten Graul22ef4732020-09-10 18:48:29 +02002550out_alloc_wqs:
2551 destroy_workqueue(smc_close_wq);
2552out_alloc_hs_wq:
2553 destroy_workqueue(smc_hs_wq);
Thomas Richter6812baa2017-01-09 16:55:15 +01002554out_pnet:
2555 smc_pnet_exit();
YueHaibing8c33bf12019-06-26 17:47:50 +02002556out_pernet_subsys:
2557 unregister_pernet_subsys(&smc_net_ops);
2558
Ursula Braunac713872017-01-09 16:55:13 +01002559 return rc;
2560}
2561
2562static void __exit smc_exit(void)
2563{
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002564 static_branch_disable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002565 sock_unregister(PF_SMC);
Ursula Braun6dabd402019-11-16 17:47:29 +01002566 smc_core_exit();
2567 smc_ib_unregister_client();
Karsten Graul22ef4732020-09-10 18:48:29 +02002568 destroy_workqueue(smc_close_wq);
2569 destroy_workqueue(smc_hs_wq);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002570 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002571 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002572 smc_pnet_exit();
Hans Wippel64e28b52019-02-21 13:01:02 +01002573 unregister_pernet_subsys(&smc_net_ops);
Ursula Braun4ead9c92019-11-16 17:47:31 +01002574 rcu_barrier();
Ursula Braunac713872017-01-09 16:55:13 +01002575}
2576
2577module_init(smc_init);
2578module_exit(smc_exit);
2579
2580MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2581MODULE_DESCRIPTION("smc socket address family");
2582MODULE_LICENSE("GPL");
2583MODULE_ALIAS_NETPROTO(PF_SMC);