blob: 5dd4faaf7d6e58a8f6195ec127193c2707d21597 [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);
Karsten Graul0530bd62020-11-18 22:40:37 +0100982 version = aclc->hdr.version == SMC_V1 ? SMC_V1 : SMC_V2;
983 ini->smcd_version = version;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200984 if (rc)
985 goto vlan_cleanup;
Ursula Brauna046d572017-01-09 16:55:16 +0100986
Hans Wippel41349842018-06-28 19:05:11 +0200987 /* depending on previous steps, connect using rdma or ism */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200988 if (aclc->hdr.typev1 == SMC_TYPE_R)
989 rc = smc_connect_rdma(smc, aclc, ini);
990 else if (aclc->hdr.typev1 == SMC_TYPE_D)
991 rc = smc_connect_ism(smc, aclc, ini);
Ursula Braund70bf4f2020-09-26 12:44:27 +0200992 if (rc)
993 goto vlan_cleanup;
Ursula Brauna046d572017-01-09 16:55:16 +0100994
Ursula Braund70bf4f2020-09-26 12:44:27 +0200995 smc_connect_ism_vlan_cleanup(smc, ini);
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200996 kfree(buf);
Ursula Braun3fc64932020-09-26 12:44:23 +0200997 kfree(ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200998 return 0;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200999
1000vlan_cleanup:
1001 smc_connect_ism_vlan_cleanup(smc, ini);
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001002 kfree(buf);
Ursula Braund70bf4f2020-09-26 12:44:27 +02001003fallback:
1004 kfree(ini);
Ursula Braune8d726c82020-09-26 12:44:32 +02001005 return smc_connect_decline_fallback(smc, rc, version);
Ursula Brauna046d572017-01-09 16:55:16 +01001006}
1007
Ursula Braun24ac3a02018-06-27 17:59:50 +02001008static void smc_connect_work(struct work_struct *work)
1009{
1010 struct smc_sock *smc = container_of(work, struct smc_sock,
1011 connect_work);
Ursula Braun50717a32019-04-12 12:57:23 +02001012 long timeo = smc->sk.sk_sndtimeo;
1013 int rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001014
Ursula Braun50717a32019-04-12 12:57:23 +02001015 if (!timeo)
1016 timeo = MAX_SCHEDULE_TIMEOUT;
1017 lock_sock(smc->clcsock->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +02001018 if (smc->clcsock->sk->sk_err) {
1019 smc->sk.sk_err = smc->clcsock->sk->sk_err;
Ursula Braun50717a32019-04-12 12:57:23 +02001020 } else if ((1 << smc->clcsock->sk->sk_state) &
1021 (TCPF_SYN_SENT | TCP_SYN_RECV)) {
1022 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
1023 if ((rc == -EPIPE) &&
1024 ((1 << smc->clcsock->sk->sk_state) &
1025 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
1026 rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001027 }
Ursula Braun50717a32019-04-12 12:57:23 +02001028 release_sock(smc->clcsock->sk);
1029 lock_sock(&smc->sk);
1030 if (rc != 0 || smc->sk.sk_err) {
1031 smc->sk.sk_state = SMC_CLOSED;
1032 if (rc == -EPIPE || rc == -EAGAIN)
1033 smc->sk.sk_err = EPIPE;
1034 else if (signal_pending(current))
1035 smc->sk.sk_err = -sock_intr_errno(timeo);
Ursula Braun6d6dd522019-11-12 16:03:41 +01001036 sock_put(&smc->sk); /* passive closing */
Ursula Braun24ac3a02018-06-27 17:59:50 +02001037 goto out;
1038 }
1039
1040 rc = __smc_connect(smc);
1041 if (rc < 0)
1042 smc->sk.sk_err = -rc;
1043
1044out:
Ursula Braun07603b22019-04-11 11:17:32 +02001045 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
1046 if (smc->sk.sk_err) {
1047 smc->sk.sk_state_change(&smc->sk);
1048 } else { /* allow polling before and after fallback decision */
1049 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
1050 smc->sk.sk_write_space(&smc->sk);
1051 }
1052 }
Ursula Braun24ac3a02018-06-27 17:59:50 +02001053 release_sock(&smc->sk);
1054}
1055
Ursula Braunac713872017-01-09 16:55:13 +01001056static int smc_connect(struct socket *sock, struct sockaddr *addr,
1057 int alen, int flags)
1058{
1059 struct sock *sk = sock->sk;
1060 struct smc_sock *smc;
1061 int rc = -EINVAL;
1062
1063 smc = smc_sk(sk);
1064
1065 /* separate smc parameter checking to be safe */
1066 if (alen < sizeof(addr->sa_family))
1067 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001068 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +01001069 goto out_err;
1070
1071 lock_sock(sk);
1072 switch (sk->sk_state) {
1073 default:
1074 goto out;
1075 case SMC_ACTIVE:
1076 rc = -EISCONN;
1077 goto out;
1078 case SMC_INIT:
1079 rc = 0;
1080 break;
1081 }
1082
1083 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001084 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun50717a32019-04-12 12:57:23 +02001085 if (smc->connect_nonblock) {
1086 rc = -EALREADY;
1087 goto out;
1088 }
1089 rc = kernel_connect(smc->clcsock, addr, alen, flags);
1090 if (rc && rc != -EINPROGRESS)
1091 goto out;
Ursula Braun301428e2019-10-29 12:41:26 +01001092
1093 sock_hold(&smc->sk); /* sock put in passive closing */
Ursula Braun86434742019-12-12 22:35:58 +01001094 if (smc->use_fallback)
1095 goto out;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001096 if (flags & O_NONBLOCK) {
Karsten Graul22ef4732020-09-10 18:48:29 +02001097 if (queue_work(smc_hs_wq, &smc->connect_work))
Ursula Braun50717a32019-04-12 12:57:23 +02001098 smc->connect_nonblock = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001099 rc = -EINPROGRESS;
1100 } else {
Ursula Braun24ac3a02018-06-27 17:59:50 +02001101 rc = __smc_connect(smc);
1102 if (rc < 0)
1103 goto out;
1104 else
1105 rc = 0; /* success cases including fallback */
1106 }
Ursula Braunac713872017-01-09 16:55:13 +01001107
1108out:
1109 release_sock(sk);
1110out_err:
1111 return rc;
1112}
1113
1114static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
1115{
Ursula Braun3163c502018-01-24 10:28:12 +01001116 struct socket *new_clcsock = NULL;
1117 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +01001118 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -08001119 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +01001120
Ursula Braun3163c502018-01-24 10:28:12 +01001121 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001122 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001123 if (!new_sk) {
1124 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +01001125 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +01001126 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +01001127 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +01001128 goto out;
1129 }
1130 *new_smc = smc_sk(new_sk);
1131
Myungho Jung78abe3d2018-12-18 09:02:25 -08001132 mutex_lock(&lsmc->clcsock_release_lock);
1133 if (lsmc->clcsock)
Ursula Brauna60a2b12020-09-10 18:48:20 +02001134 rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK);
Myungho Jung78abe3d2018-12-18 09:02:25 -08001135 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +01001136 lock_sock(lsk);
Ursula Brauna60a2b12020-09-10 18:48:20 +02001137 if (rc < 0 && rc != -EAGAIN)
Ursula Braun3163c502018-01-24 10:28:12 +01001138 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +01001139 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +02001140 new_sk->sk_prot->unhash(new_sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001141 if (new_clcsock)
1142 sock_release(new_clcsock);
1143 new_sk->sk_state = SMC_CLOSED;
1144 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun51f1de72018-01-26 09:28:48 +01001145 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +01001146 *new_smc = NULL;
1147 goto out;
1148 }
1149
Ursula Brauna60a2b12020-09-10 18:48:20 +02001150 /* new clcsock has inherited the smc listen-specific sk_data_ready
1151 * function; switch it back to the original sk_data_ready function
1152 */
1153 new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready;
Ursula Braunac713872017-01-09 16:55:13 +01001154 (*new_smc)->clcsock = new_clcsock;
1155out:
1156 return rc;
1157}
1158
Ursula Brauna046d572017-01-09 16:55:16 +01001159/* add a just created sock to the accept queue of the listen sock as
1160 * candidate for a following socket accept call from user space
1161 */
1162static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
1163{
1164 struct smc_sock *par = smc_sk(parent);
1165
Ursula Braun51f1de72018-01-26 09:28:48 +01001166 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +01001167 spin_lock(&par->accept_q_lock);
1168 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
1169 spin_unlock(&par->accept_q_lock);
1170 sk_acceptq_added(parent);
1171}
1172
1173/* remove a socket from the accept queue of its parental listening socket */
1174static void smc_accept_unlink(struct sock *sk)
1175{
1176 struct smc_sock *par = smc_sk(sk)->listen_smc;
1177
1178 spin_lock(&par->accept_q_lock);
1179 list_del_init(&smc_sk(sk)->accept_q);
1180 spin_unlock(&par->accept_q_lock);
1181 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001182 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +01001183}
1184
1185/* remove a sock from the accept queue to bind it to a new socket created
1186 * for a socket accept call from user space
1187 */
Ursula Braunb38d7322017-01-09 16:55:25 +01001188struct sock *smc_accept_dequeue(struct sock *parent,
1189 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +01001190{
1191 struct smc_sock *isk, *n;
1192 struct sock *new_sk;
1193
1194 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
1195 new_sk = (struct sock *)isk;
1196
1197 smc_accept_unlink(new_sk);
1198 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +02001199 new_sk->sk_prot->unhash(new_sk);
Ursula Braun127f4972018-01-26 09:28:49 +01001200 if (isk->clcsock) {
1201 sock_release(isk->clcsock);
1202 isk->clcsock = NULL;
1203 }
Ursula Braun51f1de72018-01-26 09:28:48 +01001204 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +01001205 continue;
1206 }
Ursula Braun07603b22019-04-11 11:17:32 +02001207 if (new_sock) {
Ursula Brauna046d572017-01-09 16:55:16 +01001208 sock_graft(new_sk, new_sock);
Ursula Braun07603b22019-04-11 11:17:32 +02001209 if (isk->use_fallback) {
1210 smc_sk(new_sk)->clcsock->file = new_sock->file;
1211 isk->clcsock->file->private_data = isk->clcsock;
1212 }
1213 }
Ursula Brauna046d572017-01-09 16:55:16 +01001214 return new_sk;
1215 }
1216 return NULL;
1217}
1218
1219/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +01001220void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +01001221{
1222 struct smc_sock *smc = smc_sk(sk);
1223
Ursula Braun81cf4f42019-10-21 16:13:15 +02001224 sock_hold(sk); /* sock_put below */
Ursula Braunb38d7322017-01-09 16:55:25 +01001225 lock_sock(sk);
1226 if (!sk->sk_lingertime)
1227 /* wait for peer closing */
1228 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun39f41f32019-06-27 15:04:52 +02001229 __smc_release(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +01001230 release_sock(sk);
Ursula Braun81cf4f42019-10-21 16:13:15 +02001231 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +01001232 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +01001233}
1234
Karsten Graulb9247542020-04-29 17:10:41 +02001235static int smcr_serv_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001236{
Karsten Graul387707f2020-04-29 17:10:40 +02001237 struct smc_link *link = smc->conn.lnk;
Karsten Graul4667bb42020-04-30 15:55:42 +02001238 struct smc_llc_qentry *qentry;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001239 int rc;
1240
Karsten Graul7562a132020-05-01 12:48:01 +02001241 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +02001242 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +02001243
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001244 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +02001245 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001246 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001247 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001248
1249 /* receive CONFIRM LINK response from client over the RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001250 qentry = smc_llc_wait(link->lgr, link, SMC_LLC_WAIT_TIME,
1251 SMC_LLC_CONFIRM_LINK);
1252 if (!qentry) {
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001253 struct smc_clc_msg_decline dclc;
1254
1255 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001256 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001257 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001258 }
Karsten Graul649758f2020-05-04 14:18:48 +02001259 smc_llc_save_peer_uid(qentry);
Karsten Graul4667bb42020-04-30 15:55:42 +02001260 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_RESP);
1261 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
1262 if (rc)
Karsten Graul75d320d2018-03-01 13:51:31 +01001263 return SMC_CLC_DECL_RMBE_EC;
1264
Karsten Graul4667bb42020-04-30 15:55:42 +02001265 /* confirm_rkey is implicit on 1st contact */
1266 smc->conn.rmb_desc->is_conf_rkey = true;
Karsten Graul52bedf32018-03-01 13:51:32 +01001267
Karsten Graul00a049c2020-04-29 17:10:49 +02001268 smc_llc_link_active(link);
Karsten Graul0a99be42020-05-05 15:01:20 +02001269 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
Karsten Graul52bedf32018-03-01 13:51:32 +01001270
Karsten Graul4667bb42020-04-30 15:55:42 +02001271 /* initial contact - try to establish second link */
Karsten Graul2d2209f2020-05-03 14:38:43 +02001272 smc_llc_srv_add_link(link);
Karsten Graul75d320d2018-03-01 13:51:31 +01001273 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001274}
1275
Hans Wippel3b2dec22018-05-18 09:34:18 +02001276/* listen worker: finish */
1277static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001278{
Ursula Brauna046d572017-01-09 16:55:16 +01001279 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001280 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001281
Ursula Brauna046d572017-01-09 16:55:16 +01001282 if (lsmc->sk.sk_state == SMC_LISTEN) {
Karsten Graulfd577702019-04-11 11:17:30 +02001283 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001284 smc_accept_enqueue(&lsmc->sk, newsmcsk);
Karsten Graulfd577702019-04-11 11:17:30 +02001285 release_sock(&lsmc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001286 } else { /* no longer listening */
1287 smc_close_non_accepted(newsmcsk);
1288 }
Ursula Brauna046d572017-01-09 16:55:16 +01001289
1290 /* Wake up accept */
1291 lsmc->sk.sk_data_ready(&lsmc->sk);
1292 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001293}
Ursula Brauna046d572017-01-09 16:55:16 +01001294
Hans Wippel3b2dec22018-05-18 09:34:18 +02001295/* listen worker: finish in state connected */
1296static void smc_listen_out_connected(struct smc_sock *new_smc)
1297{
1298 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001299
Hans Wippel3b2dec22018-05-18 09:34:18 +02001300 sk_refcnt_debug_inc(newsmcsk);
1301 if (newsmcsk->sk_state == SMC_INIT)
1302 newsmcsk->sk_state = SMC_ACTIVE;
1303
1304 smc_listen_out(new_smc);
1305}
1306
1307/* listen worker: finish in error state */
1308static void smc_listen_out_err(struct smc_sock *new_smc)
1309{
1310 struct sock *newsmcsk = &new_smc->sk;
1311
Ursula Braun51f1de72018-01-26 09:28:48 +01001312 if (newsmcsk->sk_state == SMC_INIT)
1313 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001314 newsmcsk->sk_state = SMC_CLOSED;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001315
1316 smc_listen_out(new_smc);
1317}
1318
1319/* listen worker: decline and fall back if possible */
1320static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
Karsten Graul4a9baf42020-10-23 20:48:28 +02001321 int local_first, u8 version)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001322{
1323 /* RDMA setup failed, switch back to TCP */
Karsten Graul4a9baf42020-10-23 20:48:28 +02001324 if (local_first)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001325 smc_lgr_cleanup_early(&new_smc->conn);
1326 else
1327 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001328 if (reason_code < 0) { /* error, no fallback possible */
1329 smc_listen_out_err(new_smc);
1330 return;
1331 }
Ursula Braun07603b22019-04-11 11:17:32 +02001332 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001333 new_smc->fallback_rsn = reason_code;
1334 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Ursula Braune8d726c82020-09-26 12:44:32 +02001335 if (smc_clc_send_decline(new_smc, reason_code, version) < 0) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001336 smc_listen_out_err(new_smc);
1337 return;
1338 }
1339 }
1340 smc_listen_out_connected(new_smc);
1341}
1342
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001343/* listen worker: version checking */
1344static int smc_listen_v2_check(struct smc_sock *new_smc,
1345 struct smc_clc_msg_proposal *pclc,
1346 struct smc_init_info *ini)
1347{
1348 struct smc_clc_smcd_v2_extension *pclc_smcd_v2_ext;
1349 struct smc_clc_v2_extension *pclc_v2_ext;
1350
1351 ini->smc_type_v1 = pclc->hdr.typev1;
1352 ini->smc_type_v2 = pclc->hdr.typev2;
1353 ini->smcd_version = ini->smc_type_v1 != SMC_TYPE_N ? SMC_V1 : 0;
1354 if (pclc->hdr.version > SMC_V1)
1355 ini->smcd_version |=
1356 ini->smc_type_v2 != SMC_TYPE_N ? SMC_V2 : 0;
1357 if (!smc_ism_v2_capable) {
1358 ini->smcd_version &= ~SMC_V2;
1359 goto out;
1360 }
1361 pclc_v2_ext = smc_get_clc_v2_ext(pclc);
1362 if (!pclc_v2_ext) {
1363 ini->smcd_version &= ~SMC_V2;
1364 goto out;
1365 }
1366 pclc_smcd_v2_ext = smc_get_clc_smcd_v2_ext(pclc_v2_ext);
1367 if (!pclc_smcd_v2_ext)
1368 ini->smcd_version &= ~SMC_V2;
1369
1370out:
1371 if (!ini->smcd_version) {
1372 if (pclc->hdr.typev1 == SMC_TYPE_B ||
1373 pclc->hdr.typev2 == SMC_TYPE_B)
1374 return SMC_CLC_DECL_NOSMCDEV;
1375 if (pclc->hdr.typev1 == SMC_TYPE_D ||
1376 pclc->hdr.typev2 == SMC_TYPE_D)
1377 return SMC_CLC_DECL_NOSMCDDEV;
1378 return SMC_CLC_DECL_NOSMCRDEV;
1379 }
1380
1381 return 0;
1382}
1383
Hans Wippel3b2dec22018-05-18 09:34:18 +02001384/* listen worker: check prefixes */
Karsten Graul59886692019-04-12 12:57:25 +02001385static int smc_listen_prfx_check(struct smc_sock *new_smc,
Hans Wippel3b2dec22018-05-18 09:34:18 +02001386 struct smc_clc_msg_proposal *pclc)
1387{
1388 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1389 struct socket *newclcsock = new_smc->clcsock;
1390
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001391 if (pclc->hdr.typev1 == SMC_TYPE_N)
1392 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001393 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1394 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
Karsten Graul59886692019-04-12 12:57:25 +02001395 return SMC_CLC_DECL_DIFFPREFIX;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001396
1397 return 0;
1398}
1399
1400/* listen worker: initialize connection and buffers */
1401static int smc_listen_rdma_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001402 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001403{
Karsten Graul7a627252019-04-12 12:57:30 +02001404 int rc;
1405
Hans Wippel3b2dec22018-05-18 09:34:18 +02001406 /* allocate connection / link group */
Karsten Graul7a627252019-04-12 12:57:30 +02001407 rc = smc_conn_create(new_smc, ini);
1408 if (rc)
1409 return rc;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001410
1411 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001412 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001413 return SMC_CLC_DECL_MEM;
1414
1415 return 0;
1416}
1417
Hans Wippel41349842018-06-28 19:05:11 +02001418/* listen worker: initialize connection and buffers for SMC-D */
1419static int smc_listen_ism_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001420 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +02001421{
Karsten Graul7a627252019-04-12 12:57:30 +02001422 int rc;
Hans Wippel41349842018-06-28 19:05:11 +02001423
Karsten Graul7a627252019-04-12 12:57:30 +02001424 rc = smc_conn_create(new_smc, ini);
1425 if (rc)
1426 return rc;
Hans Wippel41349842018-06-28 19:05:11 +02001427
Hans Wippel41349842018-06-28 19:05:11 +02001428 /* Create send and receive buffers */
Karsten Graul72b7f6c2020-07-26 20:34:28 +02001429 rc = smc_buf_create(new_smc, true);
1430 if (rc) {
Ursula Braun5ac54d82020-09-10 18:48:21 +02001431 if (ini->first_contact_local)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001432 smc_lgr_cleanup_early(&new_smc->conn);
1433 else
1434 smc_conn_free(&new_smc->conn);
Karsten Graul72b7f6c2020-07-26 20:34:28 +02001435 return (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB :
1436 SMC_CLC_DECL_MEM;
Hans Wippel41349842018-06-28 19:05:11 +02001437 }
1438
1439 return 0;
1440}
1441
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001442static bool smc_is_already_selected(struct smcd_dev *smcd,
1443 struct smc_init_info *ini,
1444 int matches)
1445{
1446 int i;
1447
1448 for (i = 0; i < matches; i++)
1449 if (smcd == ini->ism_dev[i])
1450 return true;
1451
1452 return false;
1453}
1454
1455/* check for ISM devices matching proposed ISM devices */
1456static void smc_check_ism_v2_match(struct smc_init_info *ini,
1457 u16 proposed_chid, u64 proposed_gid,
1458 unsigned int *matches)
1459{
1460 struct smcd_dev *smcd;
1461
1462 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
1463 if (smcd->going_away)
1464 continue;
1465 if (smc_is_already_selected(smcd, ini, *matches))
1466 continue;
1467 if (smc_ism_get_chid(smcd) == proposed_chid &&
1468 !smc_ism_cantalk(proposed_gid, ISM_RESERVED_VLANID, smcd)) {
1469 ini->ism_peer_gid[*matches] = proposed_gid;
1470 ini->ism_dev[*matches] = smcd;
1471 (*matches)++;
1472 break;
1473 }
1474 }
1475}
1476
1477static void smc_find_ism_v2_device_serv(struct smc_sock *new_smc,
1478 struct smc_clc_msg_proposal *pclc,
1479 struct smc_init_info *ini)
1480{
1481 struct smc_clc_smcd_v2_extension *smcd_v2_ext;
1482 struct smc_clc_v2_extension *smc_v2_ext;
1483 struct smc_clc_msg_smcd *pclc_smcd;
1484 unsigned int matches = 0;
Karsten Graulf29fa002020-10-07 22:57:43 +02001485 u8 smcd_version;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001486 u8 *eid = NULL;
1487 int i;
1488
1489 if (!(ini->smcd_version & SMC_V2) || !smcd_indicated(ini->smc_type_v2))
Karsten Graulf29fa002020-10-07 22:57:43 +02001490 goto not_found;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001491
1492 pclc_smcd = smc_get_clc_msg_smcd(pclc);
1493 smc_v2_ext = smc_get_clc_v2_ext(pclc);
1494 smcd_v2_ext = smc_get_clc_smcd_v2_ext(smc_v2_ext);
1495 if (!smcd_v2_ext ||
1496 !smc_v2_ext->hdr.flag.seid) /* no system EID support for SMCD */
1497 goto not_found;
1498
1499 mutex_lock(&smcd_dev_list.mutex);
1500 if (pclc_smcd->ism.chid)
1501 /* check for ISM device matching proposed native ISM device */
1502 smc_check_ism_v2_match(ini, ntohs(pclc_smcd->ism.chid),
1503 ntohll(pclc_smcd->ism.gid), &matches);
1504 for (i = 1; i <= smc_v2_ext->hdr.ism_gid_cnt; i++) {
1505 /* check for ISM devices matching proposed non-native ISM
1506 * devices
1507 */
1508 smc_check_ism_v2_match(ini,
1509 ntohs(smcd_v2_ext->gidchid[i - 1].chid),
1510 ntohll(smcd_v2_ext->gidchid[i - 1].gid),
1511 &matches);
1512 }
1513 mutex_unlock(&smcd_dev_list.mutex);
1514
1515 if (ini->ism_dev[0]) {
1516 smc_ism_get_system_eid(ini->ism_dev[0], &eid);
1517 if (memcmp(eid, smcd_v2_ext->system_eid, SMC_MAX_EID_LEN))
1518 goto not_found;
1519 } else {
1520 goto not_found;
1521 }
1522
1523 /* separate - outside the smcd_dev_list.lock */
Karsten Graulf29fa002020-10-07 22:57:43 +02001524 smcd_version = ini->smcd_version;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001525 for (i = 0; i < matches; i++) {
1526 ini->smcd_version = SMC_V2;
1527 ini->is_smcd = true;
1528 ini->ism_selected = i;
1529 if (smc_listen_ism_init(new_smc, ini))
1530 /* try next active ISM device */
1531 continue;
1532 return; /* matching and usable V2 ISM device found */
1533 }
Karsten Graulf29fa002020-10-07 22:57:43 +02001534 /* no V2 ISM device could be initialized */
1535 ini->smcd_version = smcd_version; /* restore original value */
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001536
1537not_found:
1538 ini->smcd_version &= ~SMC_V2;
1539 ini->ism_dev[0] = NULL;
1540 ini->is_smcd = false;
1541}
1542
1543static void smc_find_ism_v1_device_serv(struct smc_sock *new_smc,
1544 struct smc_clc_msg_proposal *pclc,
1545 struct smc_init_info *ini)
Ursula Braun7affc802020-09-26 12:44:21 +02001546{
1547 struct smc_clc_msg_smcd *pclc_smcd = smc_get_clc_msg_smcd(pclc);
1548
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001549 /* check if ISM V1 is available */
1550 if (!(ini->smcd_version & SMC_V1) || !smcd_indicated(ini->smc_type_v1))
Ursula Braun7affc802020-09-26 12:44:21 +02001551 goto not_found;
1552 ini->is_smcd = true; /* prepare ISM check */
Ursula Braun8c3dca32020-09-26 12:44:28 +02001553 ini->ism_peer_gid[0] = ntohll(pclc_smcd->ism.gid);
Ursula Braun7affc802020-09-26 12:44:21 +02001554 if (smc_find_ism_device(new_smc, ini))
1555 goto not_found;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001556 ini->ism_selected = 0;
Ursula Braun7affc802020-09-26 12:44:21 +02001557 if (!smc_listen_ism_init(new_smc, ini))
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001558 return; /* V1 ISM device found */
Ursula Braun7affc802020-09-26 12:44:21 +02001559
1560not_found:
Ursula Braun3fc64932020-09-26 12:44:23 +02001561 ini->ism_dev[0] = NULL;
Ursula Braun7affc802020-09-26 12:44:21 +02001562 ini->is_smcd = false;
1563}
1564
Hans Wippel3b2dec22018-05-18 09:34:18 +02001565/* listen worker: register buffers */
Ursula Braun5ac54d82020-09-10 18:48:21 +02001566static int smc_listen_rdma_reg(struct smc_sock *new_smc, bool local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001567{
Karsten Graulb9247542020-04-29 17:10:41 +02001568 struct smc_connection *conn = &new_smc->conn;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001569
Ursula Braun5ac54d82020-09-10 18:48:21 +02001570 if (!local_first) {
Karsten Graul7562a132020-05-01 12:48:01 +02001571 if (smcr_lgr_reg_rmbs(conn->lnk, conn->rmb_desc))
Karsten Graulc7674c02018-11-22 10:26:43 +01001572 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001573 }
1574 smc_rmb_sync_sg_for_device(&new_smc->conn);
1575
1576 return 0;
1577}
1578
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001579static int smc_find_rdma_v1_device_serv(struct smc_sock *new_smc,
1580 struct smc_clc_msg_proposal *pclc,
1581 struct smc_init_info *ini)
Ursula Braun7affc802020-09-26 12:44:21 +02001582{
1583 int rc;
1584
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001585 if (!smcr_indicated(ini->smc_type_v1))
Ursula Braun7affc802020-09-26 12:44:21 +02001586 return SMC_CLC_DECL_NOSMCDEV;
1587
1588 /* prepare RDMA check */
1589 ini->ib_lcl = &pclc->lcl;
1590 rc = smc_find_rdma_device(new_smc, ini);
1591 if (rc) {
1592 /* no RDMA device found */
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001593 if (ini->smc_type_v1 == SMC_TYPE_B)
Ursula Braun7affc802020-09-26 12:44:21 +02001594 /* neither ISM nor RDMA device found */
1595 rc = SMC_CLC_DECL_NOSMCDEV;
1596 return rc;
1597 }
1598 rc = smc_listen_rdma_init(new_smc, ini);
1599 if (rc)
1600 return rc;
1601 return smc_listen_rdma_reg(new_smc, ini->first_contact_local);
1602}
1603
1604/* determine the local device matching to proposal */
1605static int smc_listen_find_device(struct smc_sock *new_smc,
1606 struct smc_clc_msg_proposal *pclc,
1607 struct smc_init_info *ini)
1608{
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001609 int rc;
1610
1611 /* check for ISM device matching V2 proposed device */
1612 smc_find_ism_v2_device_serv(new_smc, pclc, ini);
1613 if (ini->ism_dev[0])
Ursula Braun7affc802020-09-26 12:44:21 +02001614 return 0;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001615
1616 if (!(ini->smcd_version & SMC_V1))
1617 return SMC_CLC_DECL_NOSMCDEV;
1618
1619 /* check for matching IP prefix and subnet length */
1620 rc = smc_listen_prfx_check(new_smc, pclc);
1621 if (rc)
1622 return rc;
1623
1624 /* get vlan id from IP device */
1625 if (smc_vlan_by_tcpsk(new_smc->clcsock, ini))
1626 return SMC_CLC_DECL_GETVLANERR;
1627
1628 /* check for ISM device matching V1 proposed device */
1629 smc_find_ism_v1_device_serv(new_smc, pclc, ini);
1630 if (ini->ism_dev[0])
1631 return 0;
1632
Ursula Braun7affc802020-09-26 12:44:21 +02001633 if (pclc->hdr.typev1 == SMC_TYPE_D)
1634 return SMC_CLC_DECL_NOSMCDDEV; /* skip RDMA and decline */
1635
1636 /* check if RDMA is available */
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001637 return smc_find_rdma_v1_device_serv(new_smc, pclc, ini);
Ursula Braun7affc802020-09-26 12:44:21 +02001638}
1639
Hans Wippel3b2dec22018-05-18 09:34:18 +02001640/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001641static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1642 struct smc_clc_msg_accept_confirm *cclc,
Ursula Braun5ac54d82020-09-10 18:48:21 +02001643 bool local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001644{
Karsten Graul387707f2020-04-29 17:10:40 +02001645 struct smc_link *link = new_smc->conn.lnk;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001646 int reason_code = 0;
1647
Ursula Braun5ac54d82020-09-10 18:48:21 +02001648 if (local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001649 smc_link_save_peer_info(link, cclc);
1650
Ursula Braun0c881ad2020-09-10 18:48:25 +02001651 if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc))
1652 return SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001653
Ursula Braun5ac54d82020-09-10 18:48:21 +02001654 if (local_first) {
Ursula Braun0c881ad2020-09-10 18:48:25 +02001655 if (smc_ib_ready_link(link))
1656 return SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001657 /* QP confirmation over RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001658 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
Karsten Graulb9247542020-04-29 17:10:41 +02001659 reason_code = smcr_serv_conf_first_link(new_smc);
Karsten Graul4667bb42020-04-30 15:55:42 +02001660 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001661 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001662 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001663}
1664
Ursula Braun7affc802020-09-26 12:44:21 +02001665/* setup for connection of server */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001666static void smc_listen_work(struct work_struct *work)
1667{
1668 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1669 smc_listen_work);
Ursula Braune8d726c82020-09-26 12:44:32 +02001670 u8 version = smc_ism_v2_capable ? SMC_V2 : SMC_V1;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001671 struct socket *newclcsock = new_smc->clcsock;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001672 struct smc_clc_msg_accept_confirm *cclc;
Ursula Braun6bb14e482020-09-10 18:48:22 +02001673 struct smc_clc_msg_proposal_area *buf;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001674 struct smc_clc_msg_proposal *pclc;
Ursula Braun3fc64932020-09-26 12:44:23 +02001675 struct smc_init_info *ini = NULL;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001676 int rc = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001677
Karsten Graulfd577702019-04-11 11:17:30 +02001678 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1679 return smc_listen_out_err(new_smc);
1680
Hans Wippel3b2dec22018-05-18 09:34:18 +02001681 if (new_smc->use_fallback) {
1682 smc_listen_out_connected(new_smc);
1683 return;
1684 }
1685
1686 /* check if peer is smc capable */
1687 if (!tcp_sk(newclcsock->sk)->syn_smc) {
Ursula Braun07603b22019-04-11 11:17:32 +02001688 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001689 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001690 smc_listen_out_connected(new_smc);
1691 return;
1692 }
1693
1694 /* do inband token exchange -
1695 * wait for and receive SMC Proposal CLC message
1696 */
Ursula Braun6bb14e482020-09-10 18:48:22 +02001697 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
1698 if (!buf) {
1699 rc = SMC_CLC_DECL_MEM;
1700 goto out_decl;
1701 }
1702 pclc = (struct smc_clc_msg_proposal *)buf;
1703 rc = smc_clc_wait_msg(new_smc, pclc, sizeof(*buf),
Karsten Graul228bae02019-04-12 12:57:28 +02001704 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001705 if (rc)
1706 goto out_decl;
Ursula Braune8d726c82020-09-26 12:44:32 +02001707 version = pclc->hdr.version == SMC_V1 ? SMC_V1 : version;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001708
Ursula Braune8d726c82020-09-26 12:44:32 +02001709 /* IPSec connections opt out of SMC optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001710 if (using_ipsec(new_smc)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001711 rc = SMC_CLC_DECL_IPSEC;
1712 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001713 }
1714
Ursula Braun3fc64932020-09-26 12:44:23 +02001715 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
1716 if (!ini) {
1717 rc = SMC_CLC_DECL_MEM;
1718 goto out_decl;
1719 }
1720
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001721 /* initial version checking */
1722 rc = smc_listen_v2_check(new_smc, pclc, ini);
1723 if (rc)
Karsten Graul9aa68d22019-04-12 12:57:29 +02001724 goto out_decl;
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001725
Hans Wippel72a36a82019-02-07 15:56:18 +01001726 mutex_lock(&smc_server_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001727 smc_close_init(new_smc);
1728 smc_rx_init(new_smc);
1729 smc_tx_init(new_smc);
1730
Ursula Braun7affc802020-09-26 12:44:21 +02001731 /* determine ISM or RoCE device used for connection */
Ursula Braun3fc64932020-09-26 12:44:23 +02001732 rc = smc_listen_find_device(new_smc, pclc, ini);
Ursula Braun7affc802020-09-26 12:44:21 +02001733 if (rc)
1734 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001735
1736 /* send SMC Accept CLC message */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001737 rc = smc_clc_send_accept(new_smc, ini->first_contact_local,
1738 ini->smcd_version == SMC_V2 ? SMC_V2 : SMC_V1);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001739 if (rc)
1740 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001741
Hans Wippel62c71392019-02-07 15:56:17 +01001742 /* SMC-D does not need this lock any more */
Ursula Braun3fc64932020-09-26 12:44:23 +02001743 if (ini->is_smcd)
Hans Wippel72a36a82019-02-07 15:56:18 +01001744 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001745
Hans Wippel3b2dec22018-05-18 09:34:18 +02001746 /* receive SMC Confirm CLC message */
Karsten Graul9047a617dc2020-10-07 22:57:42 +02001747 memset(buf, 0, sizeof(*buf));
1748 cclc = (struct smc_clc_msg_accept_confirm *)buf;
1749 rc = smc_clc_wait_msg(new_smc, cclc, sizeof(*buf),
Karsten Graul228bae02019-04-12 12:57:28 +02001750 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
1751 if (rc) {
Ursula Braun3fc64932020-09-26 12:44:23 +02001752 if (!ini->is_smcd)
Karsten Graul9aa68d22019-04-12 12:57:29 +02001753 goto out_unlock;
1754 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001755 }
1756
1757 /* finish worker */
Ursula Braun3fc64932020-09-26 12:44:23 +02001758 if (!ini->is_smcd) {
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001759 rc = smc_listen_rdma_finish(new_smc, cclc,
Ursula Braun3fc64932020-09-26 12:44:23 +02001760 ini->first_contact_local);
Hans Wippel62c71392019-02-07 15:56:17 +01001761 if (rc)
Ursula Braun0c881ad2020-09-10 18:48:25 +02001762 goto out_unlock;
1763 mutex_unlock(&smc_server_lgr_pending);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001764 }
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001765 smc_conn_save_peer_info(new_smc, cclc);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001766 smc_listen_out_connected(new_smc);
Ursula Braunac679362020-09-17 22:46:02 +02001767 goto out_free;
Karsten Graul9aa68d22019-04-12 12:57:29 +02001768
1769out_unlock:
1770 mutex_unlock(&smc_server_lgr_pending);
1771out_decl:
Karsten Graul4a9baf42020-10-23 20:48:28 +02001772 smc_listen_decline(new_smc, rc, ini ? ini->first_contact_local : 0,
1773 version);
Ursula Braunac679362020-09-17 22:46:02 +02001774out_free:
Ursula Braun3fc64932020-09-26 12:44:23 +02001775 kfree(ini);
Ursula Braun6bb14e482020-09-10 18:48:22 +02001776 kfree(buf);
Ursula Brauna046d572017-01-09 16:55:16 +01001777}
1778
1779static void smc_tcp_listen_work(struct work_struct *work)
1780{
1781 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1782 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001783 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001784 struct smc_sock *new_smc;
1785 int rc = 0;
1786
Ursula Braun3163c502018-01-24 10:28:12 +01001787 lock_sock(lsk);
1788 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001789 rc = smc_clcsock_accept(lsmc, &new_smc);
Ursula Brauna60a2b12020-09-10 18:48:20 +02001790 if (rc) /* clcsock accept queue empty or error */
Ursula Brauna046d572017-01-09 16:55:16 +01001791 goto out;
1792 if (!new_smc)
1793 continue;
1794
1795 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001796 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001797 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001798 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001799 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1800 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001801 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1802 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001803 sock_hold(&new_smc->sk); /* sock_put in passive closing */
Karsten Graul22ef4732020-09-10 18:48:29 +02001804 if (!queue_work(smc_hs_wq, &new_smc->smc_listen_work))
Ursula Braun51f1de72018-01-26 09:28:48 +01001805 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001806 }
1807
1808out:
Ursula Braun3163c502018-01-24 10:28:12 +01001809 release_sock(lsk);
Ursula Brauna60a2b12020-09-10 18:48:20 +02001810 sock_put(&lsmc->sk); /* sock_hold in smc_clcsock_data_ready() */
1811}
1812
1813static void smc_clcsock_data_ready(struct sock *listen_clcsock)
1814{
1815 struct smc_sock *lsmc;
1816
1817 lsmc = (struct smc_sock *)
1818 ((uintptr_t)listen_clcsock->sk_user_data & ~SK_USER_DATA_NOCOPY);
1819 if (!lsmc)
1820 return;
1821 lsmc->clcsk_data_ready(listen_clcsock);
1822 if (lsmc->sk.sk_state == SMC_LISTEN) {
1823 sock_hold(&lsmc->sk); /* sock_put in smc_tcp_listen_work() */
Karsten Graul22ef4732020-09-10 18:48:29 +02001824 if (!queue_work(smc_hs_wq, &lsmc->tcp_listen_work))
Ursula Brauna60a2b12020-09-10 18:48:20 +02001825 sock_put(&lsmc->sk);
1826 }
Ursula Brauna046d572017-01-09 16:55:16 +01001827}
1828
Ursula Braunac713872017-01-09 16:55:13 +01001829static int smc_listen(struct socket *sock, int backlog)
1830{
1831 struct sock *sk = sock->sk;
1832 struct smc_sock *smc;
1833 int rc;
1834
1835 smc = smc_sk(sk);
1836 lock_sock(sk);
1837
1838 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +02001839 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
1840 smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +01001841 goto out;
1842
1843 rc = 0;
1844 if (sk->sk_state == SMC_LISTEN) {
1845 sk->sk_max_ack_backlog = backlog;
1846 goto out;
1847 }
1848 /* some socket options are handled in core, so we could not apply
1849 * them to the clc socket -- copy smc socket options to clc socket
1850 */
1851 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001852 if (!smc->use_fallback)
1853 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001854
Ursula Brauna60a2b12020-09-10 18:48:20 +02001855 /* save original sk_data_ready function and establish
1856 * smc-specific sk_data_ready function
1857 */
1858 smc->clcsk_data_ready = smc->clcsock->sk->sk_data_ready;
1859 smc->clcsock->sk->sk_data_ready = smc_clcsock_data_ready;
1860 smc->clcsock->sk->sk_user_data =
1861 (void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY);
Ursula Braunac713872017-01-09 16:55:13 +01001862 rc = kernel_listen(smc->clcsock, backlog);
1863 if (rc)
1864 goto out;
1865 sk->sk_max_ack_backlog = backlog;
1866 sk->sk_ack_backlog = 0;
1867 sk->sk_state = SMC_LISTEN;
1868
1869out:
1870 release_sock(sk);
1871 return rc;
1872}
1873
1874static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001875 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001876{
Ursula Brauna046d572017-01-09 16:55:16 +01001877 struct sock *sk = sock->sk, *nsk;
1878 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001879 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001880 long timeo;
1881 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001882
1883 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001884 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001885 lock_sock(sk);
1886
1887 if (lsmc->sk.sk_state != SMC_LISTEN) {
1888 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001889 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001890 goto out;
1891 }
1892
Ursula Brauna046d572017-01-09 16:55:16 +01001893 /* Wait for an incoming connection */
1894 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1895 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1896 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1897 set_current_state(TASK_INTERRUPTIBLE);
1898 if (!timeo) {
1899 rc = -EAGAIN;
1900 break;
1901 }
1902 release_sock(sk);
1903 timeo = schedule_timeout(timeo);
1904 /* wakeup by sk_data_ready in smc_listen_work() */
1905 sched_annotate_sleep();
1906 lock_sock(sk);
1907 if (signal_pending(current)) {
1908 rc = sock_intr_errno(timeo);
1909 break;
1910 }
1911 }
1912 set_current_state(TASK_RUNNING);
1913 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001914
Ursula Brauna046d572017-01-09 16:55:16 +01001915 if (!rc)
1916 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001917 release_sock(sk);
1918 if (rc)
1919 goto out;
1920
1921 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1922 /* wait till data arrives on the socket */
1923 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1924 MSEC_PER_SEC);
1925 if (smc_sk(nsk)->use_fallback) {
1926 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1927
1928 lock_sock(clcsk);
1929 if (skb_queue_empty(&clcsk->sk_receive_queue))
1930 sk_wait_data(clcsk, &timeo, NULL);
1931 release_sock(clcsk);
1932 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1933 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001934 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001935 release_sock(nsk);
1936 }
1937 }
Ursula Braunac713872017-01-09 16:55:13 +01001938
1939out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001940 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001941 return rc;
1942}
1943
1944static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001945 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001946{
1947 struct smc_sock *smc;
1948
Ursula Braunb38d7322017-01-09 16:55:25 +01001949 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1950 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001951 return -ENOTCONN;
1952
1953 smc = smc_sk(sock->sk);
1954
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001955 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001956}
1957
1958static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1959{
1960 struct sock *sk = sock->sk;
1961 struct smc_sock *smc;
1962 int rc = -EPIPE;
1963
1964 smc = smc_sk(sk);
1965 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001966 if ((sk->sk_state != SMC_ACTIVE) &&
1967 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1968 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001969 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001970
1971 if (msg->msg_flags & MSG_FASTOPEN) {
Ursula Brauncd206362019-08-02 10:47:50 +02001972 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001973 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001974 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001975 } else {
1976 rc = -EINVAL;
1977 goto out;
1978 }
1979 }
1980
Ursula Braunac713872017-01-09 16:55:13 +01001981 if (smc->use_fallback)
1982 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1983 else
Ursula Braune6727f32017-01-09 16:55:23 +01001984 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001985out:
1986 release_sock(sk);
1987 return rc;
1988}
1989
1990static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1991 int flags)
1992{
1993 struct sock *sk = sock->sk;
1994 struct smc_sock *smc;
1995 int rc = -ENOTCONN;
1996
1997 smc = smc_sk(sk);
1998 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001999 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2000 /* socket was connected before, no more data to read */
2001 rc = 0;
2002 goto out;
2003 }
Ursula Braunb38d7322017-01-09 16:55:25 +01002004 if ((sk->sk_state == SMC_INIT) ||
2005 (sk->sk_state == SMC_LISTEN) ||
2006 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01002007 goto out;
2008
Ursula Braunb38d7322017-01-09 16:55:25 +01002009 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2010 rc = 0;
2011 goto out;
2012 }
2013
Stefan Raspl9014db22018-05-03 18:12:39 +02002014 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01002015 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02002016 } else {
2017 msg->msg_namelen = 0;
2018 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
2019 }
Ursula Braunb38d7322017-01-09 16:55:25 +01002020
Ursula Braunac713872017-01-09 16:55:13 +01002021out:
2022 release_sock(sk);
2023 return rc;
2024}
2025
Al Viroade994f2017-07-03 00:01:49 -04002026static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01002027{
Ursula Braun8dce2782018-01-26 09:28:47 +01002028 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05002029 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01002030
Ursula Braun8dce2782018-01-26 09:28:47 +01002031 spin_lock(&isk->accept_q_lock);
2032 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002033 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01002034 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01002035
Ursula Braun8dce2782018-01-26 09:28:47 +01002036 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01002037}
2038
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07002039static __poll_t smc_poll(struct file *file, struct socket *sock,
2040 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01002041{
2042 struct sock *sk = sock->sk;
Ursula Braunac713872017-01-09 16:55:13 +01002043 struct smc_sock *smc;
Ursula Braun50717a32019-04-12 12:57:23 +02002044 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01002045
Ursula Braun8dce2782018-01-26 09:28:47 +01002046 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002047 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01002048
Ursula Braunac713872017-01-09 16:55:13 +01002049 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02002050 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01002051 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07002052 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02002053 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braunac713872017-01-09 16:55:13 +01002054 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07002055 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02002056 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01002057 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002058 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01002059 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
2060 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002061 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01002062 if (sk->sk_state == SMC_LISTEN) {
2063 /* woken up by sk_data_ready in smc_listen_work() */
Ursula Braun50717a32019-04-12 12:57:23 +02002064 mask |= smc_accept_poll(sk);
2065 } else if (smc->use_fallback) { /* as result of connect_work()*/
2066 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
2067 wait);
2068 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun8dce2782018-01-26 09:28:47 +01002069 } else {
Ursula Braun50717a32019-04-12 12:57:23 +02002070 if ((sk->sk_state != SMC_INIT &&
2071 atomic_read(&smc->conn.sndbuf_space)) ||
Ursula Braun8dce2782018-01-26 09:28:47 +01002072 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002073 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01002074 } else {
2075 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
2076 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
2077 }
2078 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002079 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01002080 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002081 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01002082 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002083 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02002084 if (smc->conn.urg_state == SMC_URG_VALID)
2085 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01002086 }
Ursula Braunac713872017-01-09 16:55:13 +01002087 }
2088
2089 return mask;
2090}
2091
2092static int smc_shutdown(struct socket *sock, int how)
2093{
2094 struct sock *sk = sock->sk;
2095 struct smc_sock *smc;
2096 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01002097 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01002098
2099 smc = smc_sk(sk);
2100
2101 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01002102 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01002103
2104 lock_sock(sk);
2105
2106 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02002107 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01002108 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
2109 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
2110 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2111 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
2112 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01002113 goto out;
2114 if (smc->use_fallback) {
2115 rc = kernel_sock_shutdown(smc->clcsock, how);
2116 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
2117 if (sk->sk_shutdown == SHUTDOWN_MASK)
2118 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01002119 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01002120 }
Ursula Braunb38d7322017-01-09 16:55:25 +01002121 switch (how) {
2122 case SHUT_RDWR: /* shutdown in both directions */
2123 rc = smc_close_active(smc);
2124 break;
2125 case SHUT_WR:
2126 rc = smc_close_shutdown_write(smc);
2127 break;
2128 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02002129 rc = 0;
2130 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01002131 break;
2132 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02002133 if (smc->clcsock)
2134 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01002135 /* map sock_shutdown_cmd constants to sk_shutdown value range */
2136 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01002137
2138out:
2139 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01002140 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01002141}
2142
2143static int smc_setsockopt(struct socket *sock, int level, int optname,
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02002144 sockptr_t optval, unsigned int optlen)
Ursula Braunac713872017-01-09 16:55:13 +01002145{
2146 struct sock *sk = sock->sk;
2147 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002148 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01002149
2150 smc = smc_sk(sk);
2151
2152 /* generic setsockopts reaching us here always apply to the
2153 * CLC socket
2154 */
Christoph Hellwiga44d9e72020-07-17 08:23:31 +02002155 if (unlikely(!smc->clcsock->ops->setsockopt))
2156 rc = -EOPNOTSUPP;
2157 else
2158 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
2159 optval, optlen);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002160 if (smc->clcsock->sk->sk_err) {
2161 sk->sk_err = smc->clcsock->sk->sk_err;
2162 sk->sk_error_report(sk);
2163 }
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002164
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002165 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00002166 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02002167 if (copy_from_sockptr(&val, optval, sizeof(int)))
Ursula Braunac0107e2018-07-18 15:22:50 +02002168 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002169
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002170 lock_sock(sk);
Ursula Braun86434742019-12-12 22:35:58 +01002171 if (rc || smc->use_fallback)
2172 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002173 switch (optname) {
2174 case TCP_ULP:
2175 case TCP_FASTOPEN:
2176 case TCP_FASTOPEN_CONNECT:
2177 case TCP_FASTOPEN_KEY:
2178 case TCP_FASTOPEN_NO_COOKIE:
2179 /* option not supported by SMC */
Ursula Braun8204df72019-11-15 12:39:30 +01002180 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02002181 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02002182 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002183 } else {
Ursula Braun86434742019-12-12 22:35:58 +01002184 rc = -EINVAL;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002185 }
2186 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002187 case TCP_NODELAY:
Ursula Braunf9cedf12019-08-02 10:16:38 +02002188 if (sk->sk_state != SMC_INIT &&
2189 sk->sk_state != SMC_LISTEN &&
2190 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01002191 if (val)
Karsten Graul22ef4732020-09-10 18:48:29 +02002192 mod_delayed_work(smc->conn.lgr->tx_wq,
2193 &smc->conn.tx_work, 0);
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002194 }
2195 break;
2196 case TCP_CORK:
Ursula Braunf9cedf12019-08-02 10:16:38 +02002197 if (sk->sk_state != SMC_INIT &&
2198 sk->sk_state != SMC_LISTEN &&
2199 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01002200 if (!val)
Karsten Graul22ef4732020-09-10 18:48:29 +02002201 mod_delayed_work(smc->conn.lgr->tx_wq,
2202 &smc->conn.tx_work, 0);
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002203 }
2204 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02002205 case TCP_DEFER_ACCEPT:
2206 smc->sockopt_defer_accept = val;
2207 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002208 default:
2209 break;
2210 }
Ursula Braun86434742019-12-12 22:35:58 +01002211out:
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002212 release_sock(sk);
2213
2214 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01002215}
2216
2217static int smc_getsockopt(struct socket *sock, int level, int optname,
2218 char __user *optval, int __user *optlen)
2219{
2220 struct smc_sock *smc;
2221
2222 smc = smc_sk(sock->sk);
2223 /* socket options apply to the CLC socket */
Christoph Hellwiga44d9e72020-07-17 08:23:31 +02002224 if (unlikely(!smc->clcsock->ops->getsockopt))
2225 return -EOPNOTSUPP;
Ursula Braunac713872017-01-09 16:55:13 +01002226 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
2227 optval, optlen);
2228}
2229
2230static int smc_ioctl(struct socket *sock, unsigned int cmd,
2231 unsigned long arg)
2232{
Stefan Rasplde8474e2018-05-23 16:38:11 +02002233 union smc_host_cursor cons, urg;
2234 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01002235 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02002236 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01002237
2238 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02002239 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02002240 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02002241 if (smc->use_fallback) {
2242 if (!smc->clcsock) {
2243 release_sock(&smc->sk);
2244 return -EBADF;
2245 }
2246 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
2247 release_sock(&smc->sk);
2248 return answ;
2249 }
Ursula Braun9b67e262018-05-02 16:56:46 +02002250 switch (cmd) {
2251 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02002252 if (smc->sk.sk_state == SMC_LISTEN) {
2253 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002254 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002255 }
Ursula Braun2351abe2018-05-23 16:38:09 +02002256 if (smc->sk.sk_state == SMC_INIT ||
2257 smc->sk.sk_state == SMC_CLOSED)
2258 answ = 0;
2259 else
2260 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02002261 break;
2262 case SIOCOUTQ:
2263 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02002264 if (smc->sk.sk_state == SMC_LISTEN) {
2265 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002266 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002267 }
Ursula Braun2351abe2018-05-23 16:38:09 +02002268 if (smc->sk.sk_state == SMC_INIT ||
2269 smc->sk.sk_state == SMC_CLOSED)
2270 answ = 0;
2271 else
2272 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02002273 atomic_read(&smc->conn.sndbuf_space);
2274 break;
2275 case SIOCOUTQNSD:
2276 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02002277 if (smc->sk.sk_state == SMC_LISTEN) {
2278 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002279 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002280 }
Ursula Braun2351abe2018-05-23 16:38:09 +02002281 if (smc->sk.sk_state == SMC_INIT ||
2282 smc->sk.sk_state == SMC_CLOSED)
2283 answ = 0;
2284 else
2285 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02002286 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02002287 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02002288 if (smc->sk.sk_state == SMC_LISTEN) {
2289 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02002290 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002291 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02002292 if (smc->sk.sk_state == SMC_INIT ||
2293 smc->sk.sk_state == SMC_CLOSED) {
2294 answ = 0;
2295 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02002296 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
2297 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02002298 answ = smc_curs_diff(conn->rmb_desc->len,
2299 &cons, &urg) == 1;
2300 }
2301 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02002302 default:
Ursula Braun1992d992018-07-16 13:56:52 +02002303 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002304 return -ENOIOCTLCMD;
2305 }
Ursula Braun1992d992018-07-16 13:56:52 +02002306 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002307
2308 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01002309}
2310
2311static ssize_t smc_sendpage(struct socket *sock, struct page *page,
2312 int offset, size_t size, int flags)
2313{
2314 struct sock *sk = sock->sk;
2315 struct smc_sock *smc;
2316 int rc = -EPIPE;
2317
2318 smc = smc_sk(sk);
2319 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02002320 if (sk->sk_state != SMC_ACTIVE) {
2321 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01002322 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02002323 }
2324 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01002325 if (smc->use_fallback)
2326 rc = kernel_sendpage(smc->clcsock, page, offset,
2327 size, flags);
2328 else
2329 rc = sock_no_sendpage(sock, page, offset, size, flags);
2330
2331out:
Ursula Braunac713872017-01-09 16:55:13 +01002332 return rc;
2333}
2334
Stefan Raspl9014db22018-05-03 18:12:39 +02002335/* Map the affected portions of the rmbe into an spd, note the number of bytes
2336 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
2337 * updates till whenever a respective page has been fully processed.
2338 * Note that subsequent recv() calls have to wait till all splice() processing
2339 * completed.
2340 */
Ursula Braunac713872017-01-09 16:55:13 +01002341static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
2342 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02002343 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01002344{
2345 struct sock *sk = sock->sk;
2346 struct smc_sock *smc;
2347 int rc = -ENOTCONN;
2348
2349 smc = smc_sk(sk);
2350 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01002351 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2352 /* socket was connected before, no more data to read */
2353 rc = 0;
2354 goto out;
2355 }
Stefan Raspl9014db22018-05-03 18:12:39 +02002356 if (sk->sk_state == SMC_INIT ||
2357 sk->sk_state == SMC_LISTEN ||
2358 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01002359 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02002360
2361 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2362 rc = 0;
2363 goto out;
2364 }
2365
Ursula Braunac713872017-01-09 16:55:13 +01002366 if (smc->use_fallback) {
2367 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
2368 pipe, len, flags);
2369 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02002370 if (*ppos) {
2371 rc = -ESPIPE;
2372 goto out;
2373 }
2374 if (flags & SPLICE_F_NONBLOCK)
2375 flags = MSG_DONTWAIT;
2376 else
2377 flags = 0;
2378 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01002379 }
2380out:
2381 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02002382
Ursula Braunac713872017-01-09 16:55:13 +01002383 return rc;
2384}
2385
2386/* must look like tcp */
2387static const struct proto_ops smc_sock_ops = {
2388 .family = PF_SMC,
2389 .owner = THIS_MODULE,
2390 .release = smc_release,
2391 .bind = smc_bind,
2392 .connect = smc_connect,
2393 .socketpair = sock_no_socketpair,
2394 .accept = smc_accept,
2395 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07002396 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01002397 .ioctl = smc_ioctl,
2398 .listen = smc_listen,
2399 .shutdown = smc_shutdown,
2400 .setsockopt = smc_setsockopt,
2401 .getsockopt = smc_getsockopt,
2402 .sendmsg = smc_sendmsg,
2403 .recvmsg = smc_recvmsg,
2404 .mmap = sock_no_mmap,
2405 .sendpage = smc_sendpage,
2406 .splice_read = smc_splice_read,
2407};
2408
2409static int smc_create(struct net *net, struct socket *sock, int protocol,
2410 int kern)
2411{
Karsten Graulaaa4d332018-03-16 15:06:41 +01002412 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01002413 struct smc_sock *smc;
2414 struct sock *sk;
2415 int rc;
2416
2417 rc = -ESOCKTNOSUPPORT;
2418 if (sock->type != SOCK_STREAM)
2419 goto out;
2420
2421 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002422 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01002423 goto out;
2424
2425 rc = -ENOBUFS;
2426 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002427 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01002428 if (!sk)
2429 goto out;
2430
2431 /* create internal TCP socket for CLC handshake and fallback */
2432 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01002433 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02002434 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002435 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
2436 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01002437 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01002438 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01002439 goto out;
2440 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01002441 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
2442 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01002443
2444out:
2445 return rc;
2446}
2447
2448static const struct net_proto_family smc_sock_family_ops = {
2449 .family = PF_SMC,
2450 .owner = THIS_MODULE,
2451 .create = smc_create,
2452};
2453
Hans Wippel64e28b52019-02-21 13:01:02 +01002454unsigned int smc_net_id;
2455
2456static __net_init int smc_net_init(struct net *net)
2457{
2458 return smc_pnet_net_init(net);
2459}
2460
2461static void __net_exit smc_net_exit(struct net *net)
2462{
2463 smc_pnet_net_exit(net);
2464}
2465
2466static struct pernet_operations smc_net_ops = {
2467 .init = smc_net_init,
2468 .exit = smc_net_exit,
2469 .id = &smc_net_id,
2470 .size = sizeof(struct smc_net),
2471};
2472
Ursula Braunac713872017-01-09 16:55:13 +01002473static int __init smc_init(void)
2474{
2475 int rc;
2476
Hans Wippel64e28b52019-02-21 13:01:02 +01002477 rc = register_pernet_subsys(&smc_net_ops);
2478 if (rc)
2479 return rc;
2480
Ursula Braun201091e2020-09-26 12:44:24 +02002481 smc_ism_init();
Ursula Braunb81a5eb2020-09-26 12:44:31 +02002482 smc_clc_init();
Ursula Braun201091e2020-09-26 12:44:24 +02002483
Thomas Richter6812baa2017-01-09 16:55:15 +01002484 rc = smc_pnet_init();
2485 if (rc)
YueHaibing8c33bf12019-06-26 17:47:50 +02002486 goto out_pernet_subsys;
Thomas Richter6812baa2017-01-09 16:55:15 +01002487
Karsten Graul22ef4732020-09-10 18:48:29 +02002488 rc = -ENOMEM;
2489 smc_hs_wq = alloc_workqueue("smc_hs_wq", 0, 0);
2490 if (!smc_hs_wq)
2491 goto out_pnet;
2492
2493 smc_close_wq = alloc_workqueue("smc_close_wq", 0, 0);
2494 if (!smc_close_wq)
2495 goto out_alloc_hs_wq;
2496
Ursula Braun6dabd402019-11-16 17:47:29 +01002497 rc = smc_core_init();
2498 if (rc) {
2499 pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
Karsten Graul22ef4732020-09-10 18:48:29 +02002500 goto out_alloc_wqs;
Ursula Braun6dabd402019-11-16 17:47:29 +01002501 }
2502
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002503 rc = smc_llc_init();
2504 if (rc) {
2505 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002506 goto out_core;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002507 }
2508
Ursula Braun5f083182017-01-09 16:55:22 +01002509 rc = smc_cdc_init();
2510 if (rc) {
2511 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002512 goto out_core;
Ursula Braun5f083182017-01-09 16:55:22 +01002513 }
2514
Ursula Braunac713872017-01-09 16:55:13 +01002515 rc = proto_register(&smc_proto, 1);
2516 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01002517 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002518 goto out_core;
Ursula Braunac713872017-01-09 16:55:13 +01002519 }
2520
Karsten Graulaaa4d332018-03-16 15:06:41 +01002521 rc = proto_register(&smc_proto6, 1);
2522 if (rc) {
2523 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2524 goto out_proto;
2525 }
2526
Ursula Braunac713872017-01-09 16:55:13 +01002527 rc = sock_register(&smc_sock_family_ops);
2528 if (rc) {
2529 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002530 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01002531 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01002532 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002533 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01002534
Ursula Brauna4cf0442017-01-09 16:55:14 +01002535 rc = smc_ib_register_client();
2536 if (rc) {
2537 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2538 goto out_sock;
2539 }
2540
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002541 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002542 return 0;
2543
Ursula Brauna4cf0442017-01-09 16:55:14 +01002544out_sock:
2545 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002546out_proto6:
2547 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002548out_proto:
2549 proto_unregister(&smc_proto);
Ursula Braun6dabd402019-11-16 17:47:29 +01002550out_core:
2551 smc_core_exit();
Karsten Graul22ef4732020-09-10 18:48:29 +02002552out_alloc_wqs:
2553 destroy_workqueue(smc_close_wq);
2554out_alloc_hs_wq:
2555 destroy_workqueue(smc_hs_wq);
Thomas Richter6812baa2017-01-09 16:55:15 +01002556out_pnet:
2557 smc_pnet_exit();
YueHaibing8c33bf12019-06-26 17:47:50 +02002558out_pernet_subsys:
2559 unregister_pernet_subsys(&smc_net_ops);
2560
Ursula Braunac713872017-01-09 16:55:13 +01002561 return rc;
2562}
2563
2564static void __exit smc_exit(void)
2565{
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002566 static_branch_disable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002567 sock_unregister(PF_SMC);
Ursula Braun6dabd402019-11-16 17:47:29 +01002568 smc_core_exit();
2569 smc_ib_unregister_client();
Karsten Graul22ef4732020-09-10 18:48:29 +02002570 destroy_workqueue(smc_close_wq);
2571 destroy_workqueue(smc_hs_wq);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002572 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002573 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002574 smc_pnet_exit();
Hans Wippel64e28b52019-02-21 13:01:02 +01002575 unregister_pernet_subsys(&smc_net_ops);
Ursula Braun4ead9c92019-11-16 17:47:31 +01002576 rcu_barrier();
Ursula Braunac713872017-01-09 16:55:13 +01002577}
2578
2579module_init(smc_init);
2580module_exit(smc_exit);
2581
2582MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2583MODULE_DESCRIPTION("smc socket address family");
2584MODULE_LICENSE("GPL");
2585MODULE_ALIAS_NETPROTO(PF_SMC);