blob: 670e802a73cb0d71fa56fb82ed8d89813eeddebd [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 */
556static int smc_connect_abort(struct smc_sock *smc, int reason_code,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200557 int local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200558{
Ursula Braun51e3dfa2020-02-25 16:34:36 +0100559 bool is_smcd = smc->conn.lgr->is_smcd;
560
Ursula Braun5ac54d82020-09-10 18:48:21 +0200561 if (local_first)
Ursula Braun51e3dfa2020-02-25 16:34:36 +0100562 smc_lgr_cleanup_early(&smc->conn);
563 else
564 smc_conn_free(&smc->conn);
565 if (is_smcd)
Hans Wippel72a36a82019-02-07 15:56:18 +0100566 /* there is only one lgr role for SMC-D; use server lock */
567 mutex_unlock(&smc_server_lgr_pending);
568 else
569 mutex_unlock(&smc_client_lgr_pending);
570
Ursula Braun50717a32019-04-12 12:57:23 +0200571 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200572 return reason_code;
573}
574
575/* check if there is a rdma device available for this connection. */
576/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200577static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200578{
Hans Wippel3b2dec22018-05-18 09:34:18 +0200579 /* PNET table look up: search active ib_device and port
580 * within same PNETID that also contains the ethernet device
581 * used for the internal TCP socket
582 */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200583 smc_pnet_find_roce_resource(smc->clcsock->sk, ini);
Karsten Graul9aa68d22019-04-12 12:57:29 +0200584 if (!ini->ib_dev)
585 return SMC_CLC_DECL_NOSMCRDEV;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200586 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200587}
588
Hans Wippel41349842018-06-28 19:05:11 +0200589/* check if there is an ISM device available for this connection. */
590/* called for connect and listen */
Karsten Graul228bae02019-04-12 12:57:28 +0200591static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200592{
593 /* Find ISM device with same PNETID as connecting interface */
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200594 smc_pnet_find_ism_resource(smc->clcsock->sk, ini);
Ursula Braun3fc64932020-09-26 12:44:23 +0200595 if (!ini->ism_dev[0])
Karsten Graul9aa68d22019-04-12 12:57:29 +0200596 return SMC_CLC_DECL_NOSMCDDEV;
Ursula Braun8caaccf2020-09-26 12:44:25 +0200597 else
598 ini->ism_chid[0] = smc_ism_get_chid(ini->ism_dev[0]);
Hans Wippel41349842018-06-28 19:05:11 +0200599 return 0;
600}
601
Karsten Graul839d6962020-10-02 17:09:26 +0200602/* is chid unique for the ism devices that are already determined? */
603static bool smc_find_ism_v2_is_unique_chid(u16 chid, struct smc_init_info *ini,
604 int cnt)
605{
606 int i = (!ini->ism_dev[0]) ? 1 : 0;
607
608 for (; i < cnt; i++)
609 if (ini->ism_chid[i] == chid)
610 return false;
611 return true;
612}
613
Ursula Braund70bf4f2020-09-26 12:44:27 +0200614/* determine possible V2 ISM devices (either without PNETID or with PNETID plus
615 * PNETID matching net_device)
616 */
617static int smc_find_ism_v2_device_clnt(struct smc_sock *smc,
618 struct smc_init_info *ini)
619{
620 int rc = SMC_CLC_DECL_NOSMCDDEV;
621 struct smcd_dev *smcd;
622 int i = 1;
Karsten Graul839d6962020-10-02 17:09:26 +0200623 u16 chid;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200624
625 if (smcd_indicated(ini->smc_type_v1))
626 rc = 0; /* already initialized for V1 */
627 mutex_lock(&smcd_dev_list.mutex);
628 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
629 if (smcd->going_away || smcd == ini->ism_dev[0])
630 continue;
Karsten Graul839d6962020-10-02 17:09:26 +0200631 chid = smc_ism_get_chid(smcd);
632 if (!smc_find_ism_v2_is_unique_chid(chid, ini, i))
633 continue;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200634 if (!smc_pnet_is_pnetid_set(smcd->pnetid) ||
635 smc_pnet_is_ndev_pnetid(sock_net(&smc->sk), smcd->pnetid)) {
636 ini->ism_dev[i] = smcd;
Karsten Graul839d6962020-10-02 17:09:26 +0200637 ini->ism_chid[i] = chid;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200638 ini->is_smcd = true;
639 rc = 0;
640 i++;
641 if (i > SMC_MAX_ISM_DEVS)
642 break;
643 }
644 }
645 mutex_unlock(&smcd_dev_list.mutex);
646 ini->ism_offered_cnt = i - 1;
647 if (!ini->ism_dev[0] && !ini->ism_dev[1])
648 ini->smcd_version = 0;
649
650 return rc;
651}
652
Hans Wippel41349842018-06-28 19:05:11 +0200653/* Check for VLAN ID and register it on ISM device just for CLC handshake */
654static int smc_connect_ism_vlan_setup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200655 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200656{
Ursula Braun3fc64932020-09-26 12:44:23 +0200657 if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev[0], ini->vlan_id))
Karsten Graul7a627252019-04-12 12:57:30 +0200658 return SMC_CLC_DECL_ISMVLANERR;
Hans Wippel41349842018-06-28 19:05:11 +0200659 return 0;
660}
661
Ursula Braund70bf4f2020-09-26 12:44:27 +0200662static int smc_find_proposal_devices(struct smc_sock *smc,
663 struct smc_init_info *ini)
664{
665 int rc = 0;
666
667 /* check if there is an ism device available */
668 if (ini->smcd_version & SMC_V1) {
669 if (smc_find_ism_device(smc, ini) ||
670 smc_connect_ism_vlan_setup(smc, ini)) {
671 if (ini->smc_type_v1 == SMC_TYPE_B)
672 ini->smc_type_v1 = SMC_TYPE_R;
673 else
674 ini->smc_type_v1 = SMC_TYPE_N;
675 } /* else ISM V1 is supported for this connection */
676 if (smc_find_rdma_device(smc, ini)) {
677 if (ini->smc_type_v1 == SMC_TYPE_B)
678 ini->smc_type_v1 = SMC_TYPE_D;
679 else
680 ini->smc_type_v1 = SMC_TYPE_N;
681 } /* else RDMA is supported for this connection */
682 }
683 if (smc_ism_v2_capable && smc_find_ism_v2_device_clnt(smc, ini))
684 ini->smc_type_v2 = SMC_TYPE_N;
685
686 /* if neither ISM nor RDMA are supported, fallback */
687 if (!smcr_indicated(ini->smc_type_v1) &&
688 ini->smc_type_v1 == SMC_TYPE_N && ini->smc_type_v2 == SMC_TYPE_N)
689 rc = SMC_CLC_DECL_NOSMCDEV;
690
691 return rc;
692}
693
Hans Wippel41349842018-06-28 19:05:11 +0200694/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is
695 * used, the VLAN ID will be registered again during the connection setup.
696 */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200697static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200698 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200699{
Ursula Braund70bf4f2020-09-26 12:44:27 +0200700 if (!smcd_indicated(ini->smc_type_v1))
Hans Wippel41349842018-06-28 19:05:11 +0200701 return 0;
Ursula Braun3fc64932020-09-26 12:44:23 +0200702 if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev[0], ini->vlan_id))
Hans Wippel41349842018-06-28 19:05:11 +0200703 return SMC_CLC_DECL_CNFERR;
704 return 0;
705}
706
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200707#define SMC_CLC_MAX_ACCEPT_LEN \
708 (sizeof(struct smc_clc_msg_accept_confirm_v2) + \
Ursula Braunb81a5eb2020-09-26 12:44:31 +0200709 sizeof(struct smc_clc_first_contact_ext) + \
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200710 sizeof(struct smc_clc_msg_trail))
711
Hans Wippel3b2dec22018-05-18 09:34:18 +0200712/* CLC handshake during connect */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200713static int smc_connect_clc(struct smc_sock *smc,
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200714 struct smc_clc_msg_accept_confirm_v2 *aclc2,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200715 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200716{
717 int rc = 0;
718
719 /* do inband token exchange */
Ursula Braund70bf4f2020-09-26 12:44:27 +0200720 rc = smc_clc_send_proposal(smc, ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200721 if (rc)
722 return rc;
723 /* receive SMC Accept CLC message */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200724 return smc_clc_wait_msg(smc, aclc2, SMC_CLC_MAX_ACCEPT_LEN,
725 SMC_CLC_ACCEPT, CLC_WAIT_TIME);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200726}
727
Ursula Brauna046d572017-01-09 16:55:16 +0100728/* setup for RDMA connection of client */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200729static int smc_connect_rdma(struct smc_sock *smc,
730 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200731 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200732{
Karsten Graul0fb0b022020-04-30 15:55:43 +0200733 int i, reason_code = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200734 struct smc_link *link;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200735
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200736 ini->is_smcd = false;
Ursula Braun3d9725a2020-09-10 18:48:23 +0200737 ini->ib_lcl = &aclc->r0.lcl;
738 ini->ib_clcqpn = ntoh24(aclc->r0.qpn);
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200739 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200740
Hans Wippel72a36a82019-02-07 15:56:18 +0100741 mutex_lock(&smc_client_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200742 reason_code = smc_conn_create(smc, ini);
743 if (reason_code) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100744 mutex_unlock(&smc_client_lgr_pending);
745 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200746 }
Hans Wippel3b2dec22018-05-18 09:34:18 +0200747
748 smc_conn_save_peer_info(smc, aclc);
749
Ursula Braun5ac54d82020-09-10 18:48:21 +0200750 if (ini->first_contact_local) {
Karsten Graul0fb0b022020-04-30 15:55:43 +0200751 link = smc->conn.lnk;
752 } else {
753 /* set link that was assigned by server */
754 link = NULL;
755 for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) {
756 struct smc_link *l = &smc->conn.lgr->lnk[i];
757
Ursula Braun3d9725a2020-09-10 18:48:23 +0200758 if (l->peer_qpn == ntoh24(aclc->r0.qpn) &&
759 !memcmp(l->peer_gid, &aclc->r0.lcl.gid,
760 SMC_GID_SIZE) &&
761 !memcmp(l->peer_mac, &aclc->r0.lcl.mac,
762 sizeof(l->peer_mac))) {
Karsten Graul0fb0b022020-04-30 15:55:43 +0200763 link = l;
764 break;
765 }
766 }
767 if (!link)
768 return smc_connect_abort(smc, SMC_CLC_DECL_NOSRVLINK,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200769 ini->first_contact_local);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200770 smc->conn.lnk = link;
771 }
772
Hans Wippel3b2dec22018-05-18 09:34:18 +0200773 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +0200774 if (smc_buf_create(smc, false))
Karsten Graul7a627252019-04-12 12:57:30 +0200775 return smc_connect_abort(smc, SMC_CLC_DECL_MEM,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200776 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200777
Ursula Braun5ac54d82020-09-10 18:48:21 +0200778 if (ini->first_contact_local)
Hans Wippel3b2dec22018-05-18 09:34:18 +0200779 smc_link_save_peer_info(link, aclc);
780
Karsten Graule07d31d2020-04-29 17:10:42 +0200781 if (smc_rmb_rtoken_handling(&smc->conn, link, aclc))
Karsten Graul603cc142018-07-25 16:35:32 +0200782 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200783 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200784
785 smc_close_init(smc);
786 smc_rx_init(smc);
787
Ursula Braun5ac54d82020-09-10 18:48:21 +0200788 if (ini->first_contact_local) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200789 if (smc_ib_ready_link(link))
Karsten Graul603cc142018-07-25 16:35:32 +0200790 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200791 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200792 } else {
Karsten Graul7562a132020-05-01 12:48:01 +0200793 if (smcr_lgr_reg_rmbs(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +0200794 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200795 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200796 }
797 smc_rmb_sync_sg_for_device(&smc->conn);
798
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200799 reason_code = smc_clc_send_confirm(smc, ini->first_contact_local,
800 SMC_V1);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200801 if (reason_code)
Karsten Graul7a627252019-04-12 12:57:30 +0200802 return smc_connect_abort(smc, reason_code,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200803 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200804
805 smc_tx_init(smc);
806
Ursula Braun5ac54d82020-09-10 18:48:21 +0200807 if (ini->first_contact_local) {
Hans Wippel3b2dec22018-05-18 09:34:18 +0200808 /* QP confirmation over RoCE fabric */
Karsten Graul0fb0b022020-04-30 15:55:43 +0200809 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
Karsten Graulb9247542020-04-29 17:10:41 +0200810 reason_code = smcr_clnt_conf_first_link(smc);
Karsten Graul0fb0b022020-04-30 15:55:43 +0200811 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200812 if (reason_code)
813 return smc_connect_abort(smc, reason_code,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200814 ini->first_contact_local);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200815 }
Hans Wippel72a36a82019-02-07 15:56:18 +0100816 mutex_unlock(&smc_client_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200817
818 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200819 smc->connect_nonblock = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +0200820 if (smc->sk.sk_state == SMC_INIT)
821 smc->sk.sk_state = SMC_ACTIVE;
822
823 return 0;
824}
825
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200826/* The server has chosen one of the proposed ISM devices for the communication.
827 * Determine from the CHID of the received CLC ACCEPT the ISM device chosen.
828 */
829static int
830smc_v2_determine_accepted_chid(struct smc_clc_msg_accept_confirm_v2 *aclc,
831 struct smc_init_info *ini)
832{
833 int i;
834
835 for (i = 0; i < ini->ism_offered_cnt + 1; i++) {
836 if (ini->ism_chid[i] == ntohs(aclc->chid)) {
837 ini->ism_selected = i;
838 return 0;
839 }
840 }
841
842 return -EPROTO;
843}
844
Hans Wippel41349842018-06-28 19:05:11 +0200845/* setup for ISM connection of client */
846static int smc_connect_ism(struct smc_sock *smc,
847 struct smc_clc_msg_accept_confirm *aclc,
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200848 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +0200849{
Hans Wippel41349842018-06-28 19:05:11 +0200850 int rc = 0;
851
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200852 ini->is_smcd = true;
Ursula Braunf1eb02f2020-09-26 12:44:20 +0200853 ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK;
Karsten Graulbc36d2f2019-04-12 12:57:26 +0200854
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200855 if (aclc->hdr.version == SMC_V2) {
856 struct smc_clc_msg_accept_confirm_v2 *aclc_v2 =
857 (struct smc_clc_msg_accept_confirm_v2 *)aclc;
858
859 rc = smc_v2_determine_accepted_chid(aclc_v2, ini);
860 if (rc)
861 return rc;
862 }
863 ini->ism_peer_gid[ini->ism_selected] = aclc->d0.gid;
864
Hans Wippel72a36a82019-02-07 15:56:18 +0100865 /* there is only one lgr role for SMC-D; use server lock */
866 mutex_lock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200867 rc = smc_conn_create(smc, ini);
868 if (rc) {
Hans Wippel72a36a82019-02-07 15:56:18 +0100869 mutex_unlock(&smc_server_lgr_pending);
Karsten Graul7a627252019-04-12 12:57:30 +0200870 return rc;
Hans Wippel72a36a82019-02-07 15:56:18 +0100871 }
Hans Wippel41349842018-06-28 19:05:11 +0200872
873 /* Create send and receive buffers */
Karsten Graul72b7f6c2020-07-26 20:34:28 +0200874 rc = smc_buf_create(smc, true);
875 if (rc)
876 return smc_connect_abort(smc, (rc == -ENOSPC) ?
877 SMC_CLC_DECL_MAX_DMB :
878 SMC_CLC_DECL_MEM,
Ursula Braun5ac54d82020-09-10 18:48:21 +0200879 ini->first_contact_local);
Hans Wippel41349842018-06-28 19:05:11 +0200880
881 smc_conn_save_peer_info(smc, aclc);
882 smc_close_init(smc);
883 smc_rx_init(smc);
884 smc_tx_init(smc);
885
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200886 rc = smc_clc_send_confirm(smc, ini->first_contact_local,
887 aclc->hdr.version);
Hans Wippel41349842018-06-28 19:05:11 +0200888 if (rc)
Ursula Braun5ac54d82020-09-10 18:48:21 +0200889 return smc_connect_abort(smc, rc, ini->first_contact_local);
Hans Wippel72a36a82019-02-07 15:56:18 +0100890 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel41349842018-06-28 19:05:11 +0200891
892 smc_copy_sock_settings_to_clc(smc);
Ursula Braun50717a32019-04-12 12:57:23 +0200893 smc->connect_nonblock = 0;
Hans Wippel41349842018-06-28 19:05:11 +0200894 if (smc->sk.sk_state == SMC_INIT)
895 smc->sk.sk_state = SMC_ACTIVE;
896
897 return 0;
898}
899
Ursula Braund70bf4f2020-09-26 12:44:27 +0200900/* check if received accept type and version matches a proposed one */
901static int smc_connect_check_aclc(struct smc_init_info *ini,
902 struct smc_clc_msg_accept_confirm *aclc)
903{
904 if ((aclc->hdr.typev1 == SMC_TYPE_R &&
905 !smcr_indicated(ini->smc_type_v1)) ||
906 (aclc->hdr.typev1 == SMC_TYPE_D &&
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200907 ((!smcd_indicated(ini->smc_type_v1) &&
908 !smcd_indicated(ini->smc_type_v2)) ||
909 (aclc->hdr.version == SMC_V1 &&
910 !smcd_indicated(ini->smc_type_v1)) ||
911 (aclc->hdr.version == SMC_V2 &&
912 !smcd_indicated(ini->smc_type_v2)))))
Ursula Braund70bf4f2020-09-26 12:44:27 +0200913 return SMC_CLC_DECL_MODEUNSUPP;
914
915 return 0;
916}
917
Hans Wippel3b2dec22018-05-18 09:34:18 +0200918/* perform steps before actually connecting */
919static int __smc_connect(struct smc_sock *smc)
Ursula Brauna046d572017-01-09 16:55:16 +0100920{
Ursula Braune8d726c82020-09-26 12:44:32 +0200921 u8 version = smc_ism_v2_capable ? SMC_V2 : SMC_V1;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200922 struct smc_clc_msg_accept_confirm_v2 *aclc2;
923 struct smc_clc_msg_accept_confirm *aclc;
Ursula Braun3fc64932020-09-26 12:44:23 +0200924 struct smc_init_info *ini = NULL;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200925 u8 *buf = NULL;
Ursula Brauna046d572017-01-09 16:55:16 +0100926 int rc = 0;
Ursula Brauna046d572017-01-09 16:55:16 +0100927
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200928 if (smc->use_fallback)
Karsten Graul603cc142018-07-25 16:35:32 +0200929 return smc_connect_fallback(smc, smc->fallback_rsn);
Ursula Braunee9dfbe2018-04-26 17:18:21 +0200930
Hans Wippel3b2dec22018-05-18 09:34:18 +0200931 /* if peer has not signalled SMC-capability, fall back */
932 if (!tcp_sk(smc->clcsock->sk)->syn_smc)
Karsten Graul603cc142018-07-25 16:35:32 +0200933 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC);
Ursula Braunc5c1cc92017-10-25 11:01:46 +0200934
Ursula Braune8d726c82020-09-26 12:44:32 +0200935 /* IPSec connections opt out of SMC optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +0200936 if (using_ipsec(smc))
Ursula Braune8d726c82020-09-26 12:44:32 +0200937 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC,
938 version);
Ursula Brauna046d572017-01-09 16:55:16 +0100939
Ursula Braun3fc64932020-09-26 12:44:23 +0200940 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
941 if (!ini)
Ursula Braune8d726c82020-09-26 12:44:32 +0200942 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_MEM,
943 version);
Ursula Braun3fc64932020-09-26 12:44:23 +0200944
Ursula Braund70bf4f2020-09-26 12:44:27 +0200945 ini->smcd_version = SMC_V1;
946 ini->smcd_version |= smc_ism_v2_capable ? SMC_V2 : 0;
947 ini->smc_type_v1 = SMC_TYPE_B;
948 ini->smc_type_v2 = smc_ism_v2_capable ? SMC_TYPE_D : SMC_TYPE_N;
949
Karsten Graulfba7e8e2019-04-12 12:57:27 +0200950 /* get vlan id from IP device */
Ursula Braun3fc64932020-09-26 12:44:23 +0200951 if (smc_vlan_by_tcpsk(smc->clcsock, ini)) {
Ursula Braund70bf4f2020-09-26 12:44:27 +0200952 ini->smcd_version &= ~SMC_V1;
953 ini->smc_type_v1 = SMC_TYPE_N;
954 if (!ini->smcd_version) {
955 rc = SMC_CLC_DECL_GETVLANERR;
956 goto fallback;
957 }
Ursula Braun3fc64932020-09-26 12:44:23 +0200958 }
Hans Wippel41349842018-06-28 19:05:11 +0200959
Ursula Braund70bf4f2020-09-26 12:44:27 +0200960 rc = smc_find_proposal_devices(smc, ini);
961 if (rc)
962 goto fallback;
Ursula Brauna046d572017-01-09 16:55:16 +0100963
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200964 buf = kzalloc(SMC_CLC_MAX_ACCEPT_LEN, GFP_KERNEL);
965 if (!buf) {
966 rc = SMC_CLC_DECL_MEM;
967 goto fallback;
968 }
969 aclc2 = (struct smc_clc_msg_accept_confirm_v2 *)buf;
970 aclc = (struct smc_clc_msg_accept_confirm *)aclc2;
971
Hans Wippel3b2dec22018-05-18 09:34:18 +0200972 /* perform CLC handshake */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200973 rc = smc_connect_clc(smc, aclc2, ini);
Ursula Braund70bf4f2020-09-26 12:44:27 +0200974 if (rc)
975 goto vlan_cleanup;
976
977 /* check if smc modes and versions of CLC proposal and accept match */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200978 rc = smc_connect_check_aclc(ini, aclc);
Ursula Braune8d726c82020-09-26 12:44:32 +0200979 version = aclc->hdr.version == SMC_V1 ? SMC_V1 : version;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200980 if (rc)
981 goto vlan_cleanup;
Ursula Brauna046d572017-01-09 16:55:16 +0100982
Hans Wippel41349842018-06-28 19:05:11 +0200983 /* depending on previous steps, connect using rdma or ism */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200984 if (aclc->hdr.typev1 == SMC_TYPE_R)
985 rc = smc_connect_rdma(smc, aclc, ini);
986 else if (aclc->hdr.typev1 == SMC_TYPE_D)
987 rc = smc_connect_ism(smc, aclc, ini);
Ursula Braund70bf4f2020-09-26 12:44:27 +0200988 if (rc)
989 goto vlan_cleanup;
Ursula Brauna046d572017-01-09 16:55:16 +0100990
Ursula Braund70bf4f2020-09-26 12:44:27 +0200991 smc_connect_ism_vlan_cleanup(smc, ini);
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200992 kfree(buf);
Ursula Braun3fc64932020-09-26 12:44:23 +0200993 kfree(ini);
Hans Wippel3b2dec22018-05-18 09:34:18 +0200994 return 0;
Ursula Braund70bf4f2020-09-26 12:44:27 +0200995
996vlan_cleanup:
997 smc_connect_ism_vlan_cleanup(smc, ini);
Ursula Brauna7c9c5f2020-09-26 12:44:30 +0200998 kfree(buf);
Ursula Braund70bf4f2020-09-26 12:44:27 +0200999fallback:
1000 kfree(ini);
Ursula Braune8d726c82020-09-26 12:44:32 +02001001 return smc_connect_decline_fallback(smc, rc, version);
Ursula Brauna046d572017-01-09 16:55:16 +01001002}
1003
Ursula Braun24ac3a02018-06-27 17:59:50 +02001004static void smc_connect_work(struct work_struct *work)
1005{
1006 struct smc_sock *smc = container_of(work, struct smc_sock,
1007 connect_work);
Ursula Braun50717a32019-04-12 12:57:23 +02001008 long timeo = smc->sk.sk_sndtimeo;
1009 int rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001010
Ursula Braun50717a32019-04-12 12:57:23 +02001011 if (!timeo)
1012 timeo = MAX_SCHEDULE_TIMEOUT;
1013 lock_sock(smc->clcsock->sk);
Ursula Braun24ac3a02018-06-27 17:59:50 +02001014 if (smc->clcsock->sk->sk_err) {
1015 smc->sk.sk_err = smc->clcsock->sk->sk_err;
Ursula Braun50717a32019-04-12 12:57:23 +02001016 } else if ((1 << smc->clcsock->sk->sk_state) &
1017 (TCPF_SYN_SENT | TCP_SYN_RECV)) {
1018 rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo);
1019 if ((rc == -EPIPE) &&
1020 ((1 << smc->clcsock->sk->sk_state) &
1021 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)))
1022 rc = 0;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001023 }
Ursula Braun50717a32019-04-12 12:57:23 +02001024 release_sock(smc->clcsock->sk);
1025 lock_sock(&smc->sk);
1026 if (rc != 0 || smc->sk.sk_err) {
1027 smc->sk.sk_state = SMC_CLOSED;
1028 if (rc == -EPIPE || rc == -EAGAIN)
1029 smc->sk.sk_err = EPIPE;
1030 else if (signal_pending(current))
1031 smc->sk.sk_err = -sock_intr_errno(timeo);
Ursula Braun6d6dd522019-11-12 16:03:41 +01001032 sock_put(&smc->sk); /* passive closing */
Ursula Braun24ac3a02018-06-27 17:59:50 +02001033 goto out;
1034 }
1035
1036 rc = __smc_connect(smc);
1037 if (rc < 0)
1038 smc->sk.sk_err = -rc;
1039
1040out:
Ursula Braun07603b22019-04-11 11:17:32 +02001041 if (!sock_flag(&smc->sk, SOCK_DEAD)) {
1042 if (smc->sk.sk_err) {
1043 smc->sk.sk_state_change(&smc->sk);
1044 } else { /* allow polling before and after fallback decision */
1045 smc->clcsock->sk->sk_write_space(smc->clcsock->sk);
1046 smc->sk.sk_write_space(&smc->sk);
1047 }
1048 }
Ursula Braun24ac3a02018-06-27 17:59:50 +02001049 release_sock(&smc->sk);
1050}
1051
Ursula Braunac713872017-01-09 16:55:13 +01001052static int smc_connect(struct socket *sock, struct sockaddr *addr,
1053 int alen, int flags)
1054{
1055 struct sock *sk = sock->sk;
1056 struct smc_sock *smc;
1057 int rc = -EINVAL;
1058
1059 smc = smc_sk(sk);
1060
1061 /* separate smc parameter checking to be safe */
1062 if (alen < sizeof(addr->sa_family))
1063 goto out_err;
Karsten Graulaaa4d332018-03-16 15:06:41 +01001064 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6)
Ursula Braunac713872017-01-09 16:55:13 +01001065 goto out_err;
1066
1067 lock_sock(sk);
1068 switch (sk->sk_state) {
1069 default:
1070 goto out;
1071 case SMC_ACTIVE:
1072 rc = -EISCONN;
1073 goto out;
1074 case SMC_INIT:
1075 rc = 0;
1076 break;
1077 }
1078
1079 smc_copy_sock_settings_to_clc(smc);
Ursula Braunc5c1cc92017-10-25 11:01:46 +02001080 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braun50717a32019-04-12 12:57:23 +02001081 if (smc->connect_nonblock) {
1082 rc = -EALREADY;
1083 goto out;
1084 }
1085 rc = kernel_connect(smc->clcsock, addr, alen, flags);
1086 if (rc && rc != -EINPROGRESS)
1087 goto out;
Ursula Braun301428e2019-10-29 12:41:26 +01001088
1089 sock_hold(&smc->sk); /* sock put in passive closing */
Ursula Braun86434742019-12-12 22:35:58 +01001090 if (smc->use_fallback)
1091 goto out;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001092 if (flags & O_NONBLOCK) {
Karsten Graul22ef4732020-09-10 18:48:29 +02001093 if (queue_work(smc_hs_wq, &smc->connect_work))
Ursula Braun50717a32019-04-12 12:57:23 +02001094 smc->connect_nonblock = 1;
Ursula Braun24ac3a02018-06-27 17:59:50 +02001095 rc = -EINPROGRESS;
1096 } else {
Ursula Braun24ac3a02018-06-27 17:59:50 +02001097 rc = __smc_connect(smc);
1098 if (rc < 0)
1099 goto out;
1100 else
1101 rc = 0; /* success cases including fallback */
1102 }
Ursula Braunac713872017-01-09 16:55:13 +01001103
1104out:
1105 release_sock(sk);
1106out_err:
1107 return rc;
1108}
1109
1110static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc)
1111{
Ursula Braun3163c502018-01-24 10:28:12 +01001112 struct socket *new_clcsock = NULL;
1113 struct sock *lsk = &lsmc->sk;
Ursula Braunac713872017-01-09 16:55:13 +01001114 struct sock *new_sk;
Myungho Jung78abe3d2018-12-18 09:02:25 -08001115 int rc = -EINVAL;
Ursula Braunac713872017-01-09 16:55:13 +01001116
Ursula Braun3163c502018-01-24 10:28:12 +01001117 release_sock(lsk);
Karsten Graulaaa4d332018-03-16 15:06:41 +01001118 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol);
Ursula Braunac713872017-01-09 16:55:13 +01001119 if (!new_sk) {
1120 rc = -ENOMEM;
Ursula Braun3163c502018-01-24 10:28:12 +01001121 lsk->sk_err = ENOMEM;
Ursula Braunac713872017-01-09 16:55:13 +01001122 *new_smc = NULL;
Ursula Braun3163c502018-01-24 10:28:12 +01001123 lock_sock(lsk);
Ursula Braunac713872017-01-09 16:55:13 +01001124 goto out;
1125 }
1126 *new_smc = smc_sk(new_sk);
1127
Myungho Jung78abe3d2018-12-18 09:02:25 -08001128 mutex_lock(&lsmc->clcsock_release_lock);
1129 if (lsmc->clcsock)
Ursula Brauna60a2b12020-09-10 18:48:20 +02001130 rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK);
Myungho Jung78abe3d2018-12-18 09:02:25 -08001131 mutex_unlock(&lsmc->clcsock_release_lock);
Ursula Braun3163c502018-01-24 10:28:12 +01001132 lock_sock(lsk);
Ursula Brauna60a2b12020-09-10 18:48:20 +02001133 if (rc < 0 && rc != -EAGAIN)
Ursula Braun3163c502018-01-24 10:28:12 +01001134 lsk->sk_err = -rc;
Ursula Braun35a6b172018-01-24 10:28:13 +01001135 if (rc < 0 || lsk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +02001136 new_sk->sk_prot->unhash(new_sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001137 if (new_clcsock)
1138 sock_release(new_clcsock);
1139 new_sk->sk_state = SMC_CLOSED;
1140 sock_set_flag(new_sk, SOCK_DEAD);
Ursula Braun51f1de72018-01-26 09:28:48 +01001141 sock_put(new_sk); /* final */
Ursula Braunac713872017-01-09 16:55:13 +01001142 *new_smc = NULL;
1143 goto out;
1144 }
1145
Ursula Brauna60a2b12020-09-10 18:48:20 +02001146 /* new clcsock has inherited the smc listen-specific sk_data_ready
1147 * function; switch it back to the original sk_data_ready function
1148 */
1149 new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready;
Ursula Braunac713872017-01-09 16:55:13 +01001150 (*new_smc)->clcsock = new_clcsock;
1151out:
1152 return rc;
1153}
1154
Ursula Brauna046d572017-01-09 16:55:16 +01001155/* add a just created sock to the accept queue of the listen sock as
1156 * candidate for a following socket accept call from user space
1157 */
1158static void smc_accept_enqueue(struct sock *parent, struct sock *sk)
1159{
1160 struct smc_sock *par = smc_sk(parent);
1161
Ursula Braun51f1de72018-01-26 09:28:48 +01001162 sock_hold(sk); /* sock_put in smc_accept_unlink () */
Ursula Brauna046d572017-01-09 16:55:16 +01001163 spin_lock(&par->accept_q_lock);
1164 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q);
1165 spin_unlock(&par->accept_q_lock);
1166 sk_acceptq_added(parent);
1167}
1168
1169/* remove a socket from the accept queue of its parental listening socket */
1170static void smc_accept_unlink(struct sock *sk)
1171{
1172 struct smc_sock *par = smc_sk(sk)->listen_smc;
1173
1174 spin_lock(&par->accept_q_lock);
1175 list_del_init(&smc_sk(sk)->accept_q);
1176 spin_unlock(&par->accept_q_lock);
1177 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001178 sock_put(sk); /* sock_hold in smc_accept_enqueue */
Ursula Brauna046d572017-01-09 16:55:16 +01001179}
1180
1181/* remove a sock from the accept queue to bind it to a new socket created
1182 * for a socket accept call from user space
1183 */
Ursula Braunb38d7322017-01-09 16:55:25 +01001184struct sock *smc_accept_dequeue(struct sock *parent,
1185 struct socket *new_sock)
Ursula Brauna046d572017-01-09 16:55:16 +01001186{
1187 struct smc_sock *isk, *n;
1188 struct sock *new_sk;
1189
1190 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) {
1191 new_sk = (struct sock *)isk;
1192
1193 smc_accept_unlink(new_sk);
1194 if (new_sk->sk_state == SMC_CLOSED) {
Ursula Braunf61bca52019-04-11 11:17:34 +02001195 new_sk->sk_prot->unhash(new_sk);
Ursula Braun127f4972018-01-26 09:28:49 +01001196 if (isk->clcsock) {
1197 sock_release(isk->clcsock);
1198 isk->clcsock = NULL;
1199 }
Ursula Braun51f1de72018-01-26 09:28:48 +01001200 sock_put(new_sk); /* final */
Ursula Brauna046d572017-01-09 16:55:16 +01001201 continue;
1202 }
Ursula Braun07603b22019-04-11 11:17:32 +02001203 if (new_sock) {
Ursula Brauna046d572017-01-09 16:55:16 +01001204 sock_graft(new_sk, new_sock);
Ursula Braun07603b22019-04-11 11:17:32 +02001205 if (isk->use_fallback) {
1206 smc_sk(new_sk)->clcsock->file = new_sock->file;
1207 isk->clcsock->file->private_data = isk->clcsock;
1208 }
1209 }
Ursula Brauna046d572017-01-09 16:55:16 +01001210 return new_sk;
1211 }
1212 return NULL;
1213}
1214
1215/* clean up for a created but never accepted sock */
Ursula Braunb38d7322017-01-09 16:55:25 +01001216void smc_close_non_accepted(struct sock *sk)
Ursula Brauna046d572017-01-09 16:55:16 +01001217{
1218 struct smc_sock *smc = smc_sk(sk);
1219
Ursula Braun81cf4f42019-10-21 16:13:15 +02001220 sock_hold(sk); /* sock_put below */
Ursula Braunb38d7322017-01-09 16:55:25 +01001221 lock_sock(sk);
1222 if (!sk->sk_lingertime)
1223 /* wait for peer closing */
1224 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT;
Ursula Braun39f41f32019-06-27 15:04:52 +02001225 __smc_release(smc);
Ursula Braunb38d7322017-01-09 16:55:25 +01001226 release_sock(sk);
Ursula Braun81cf4f42019-10-21 16:13:15 +02001227 sock_put(sk); /* sock_hold above */
Ursula Braun51f1de72018-01-26 09:28:48 +01001228 sock_put(sk); /* final sock_put */
Ursula Brauna046d572017-01-09 16:55:16 +01001229}
1230
Karsten Graulb9247542020-04-29 17:10:41 +02001231static int smcr_serv_conf_first_link(struct smc_sock *smc)
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001232{
Karsten Graul387707f2020-04-29 17:10:40 +02001233 struct smc_link *link = smc->conn.lnk;
Karsten Graul4667bb42020-04-30 15:55:42 +02001234 struct smc_llc_qentry *qentry;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001235 int rc;
1236
Karsten Graul7562a132020-05-01 12:48:01 +02001237 if (smcr_link_reg_rmb(link, smc->conn.rmb_desc))
Karsten Graul603cc142018-07-25 16:35:32 +02001238 return SMC_CLC_DECL_ERR_REGRMB;
Ursula Braun652a1e42017-07-28 13:56:17 +02001239
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001240 /* send CONFIRM LINK request to client over the RoCE fabric */
Ursula Braun947541f2018-07-25 16:35:30 +02001241 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ);
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001242 if (rc < 0)
Karsten Graul603cc142018-07-25 16:35:32 +02001243 return SMC_CLC_DECL_TIMEOUT_CL;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001244
1245 /* receive CONFIRM LINK response from client over the RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001246 qentry = smc_llc_wait(link->lgr, link, SMC_LLC_WAIT_TIME,
1247 SMC_LLC_CONFIRM_LINK);
1248 if (!qentry) {
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001249 struct smc_clc_msg_decline dclc;
1250
1251 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc),
Ursula Braun2b59f582018-11-22 10:26:39 +01001252 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT);
Ursula Braun9ed28552018-11-22 10:26:37 +01001253 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001254 }
Karsten Graul649758f2020-05-04 14:18:48 +02001255 smc_llc_save_peer_uid(qentry);
Karsten Graul4667bb42020-04-30 15:55:42 +02001256 rc = smc_llc_eval_conf_link(qentry, SMC_LLC_RESP);
1257 smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl);
1258 if (rc)
Karsten Graul75d320d2018-03-01 13:51:31 +01001259 return SMC_CLC_DECL_RMBE_EC;
1260
Karsten Graul4667bb42020-04-30 15:55:42 +02001261 /* confirm_rkey is implicit on 1st contact */
1262 smc->conn.rmb_desc->is_conf_rkey = true;
Karsten Graul52bedf32018-03-01 13:51:32 +01001263
Karsten Graul00a049c2020-04-29 17:10:49 +02001264 smc_llc_link_active(link);
Karsten Graul0a99be42020-05-05 15:01:20 +02001265 smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE);
Karsten Graul52bedf32018-03-01 13:51:32 +01001266
Karsten Graul4667bb42020-04-30 15:55:42 +02001267 /* initial contact - try to establish second link */
Karsten Graul2d2209f2020-05-03 14:38:43 +02001268 smc_llc_srv_add_link(link);
Karsten Graul75d320d2018-03-01 13:51:31 +01001269 return 0;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01001270}
1271
Hans Wippel3b2dec22018-05-18 09:34:18 +02001272/* listen worker: finish */
1273static void smc_listen_out(struct smc_sock *new_smc)
Ursula Brauna046d572017-01-09 16:55:16 +01001274{
Ursula Brauna046d572017-01-09 16:55:16 +01001275 struct smc_sock *lsmc = new_smc->listen_smc;
Ursula Brauna046d572017-01-09 16:55:16 +01001276 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001277
Ursula Brauna046d572017-01-09 16:55:16 +01001278 if (lsmc->sk.sk_state == SMC_LISTEN) {
Karsten Graulfd577702019-04-11 11:17:30 +02001279 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING);
Ursula Brauna046d572017-01-09 16:55:16 +01001280 smc_accept_enqueue(&lsmc->sk, newsmcsk);
Karsten Graulfd577702019-04-11 11:17:30 +02001281 release_sock(&lsmc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001282 } else { /* no longer listening */
1283 smc_close_non_accepted(newsmcsk);
1284 }
Ursula Brauna046d572017-01-09 16:55:16 +01001285
1286 /* Wake up accept */
1287 lsmc->sk.sk_data_ready(&lsmc->sk);
1288 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001289}
Ursula Brauna046d572017-01-09 16:55:16 +01001290
Hans Wippel3b2dec22018-05-18 09:34:18 +02001291/* listen worker: finish in state connected */
1292static void smc_listen_out_connected(struct smc_sock *new_smc)
1293{
1294 struct sock *newsmcsk = &new_smc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001295
Hans Wippel3b2dec22018-05-18 09:34:18 +02001296 sk_refcnt_debug_inc(newsmcsk);
1297 if (newsmcsk->sk_state == SMC_INIT)
1298 newsmcsk->sk_state = SMC_ACTIVE;
1299
1300 smc_listen_out(new_smc);
1301}
1302
1303/* listen worker: finish in error state */
1304static void smc_listen_out_err(struct smc_sock *new_smc)
1305{
1306 struct sock *newsmcsk = &new_smc->sk;
1307
Ursula Braun51f1de72018-01-26 09:28:48 +01001308 if (newsmcsk->sk_state == SMC_INIT)
1309 sock_put(&new_smc->sk); /* passive closing */
Ursula Brauna046d572017-01-09 16:55:16 +01001310 newsmcsk->sk_state = SMC_CLOSED;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001311
1312 smc_listen_out(new_smc);
1313}
1314
1315/* listen worker: decline and fall back if possible */
1316static void smc_listen_decline(struct smc_sock *new_smc, int reason_code,
Ursula Braune8d726c82020-09-26 12:44:32 +02001317 struct smc_init_info *ini, u8 version)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001318{
1319 /* RDMA setup failed, switch back to TCP */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001320 if (ini->first_contact_local)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001321 smc_lgr_cleanup_early(&new_smc->conn);
1322 else
1323 smc_conn_free(&new_smc->conn);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001324 if (reason_code < 0) { /* error, no fallback possible */
1325 smc_listen_out_err(new_smc);
1326 return;
1327 }
Ursula Braun07603b22019-04-11 11:17:32 +02001328 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001329 new_smc->fallback_rsn = reason_code;
1330 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) {
Ursula Braune8d726c82020-09-26 12:44:32 +02001331 if (smc_clc_send_decline(new_smc, reason_code, version) < 0) {
Hans Wippel3b2dec22018-05-18 09:34:18 +02001332 smc_listen_out_err(new_smc);
1333 return;
1334 }
1335 }
1336 smc_listen_out_connected(new_smc);
1337}
1338
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001339/* listen worker: version checking */
1340static int smc_listen_v2_check(struct smc_sock *new_smc,
1341 struct smc_clc_msg_proposal *pclc,
1342 struct smc_init_info *ini)
1343{
1344 struct smc_clc_smcd_v2_extension *pclc_smcd_v2_ext;
1345 struct smc_clc_v2_extension *pclc_v2_ext;
1346
1347 ini->smc_type_v1 = pclc->hdr.typev1;
1348 ini->smc_type_v2 = pclc->hdr.typev2;
1349 ini->smcd_version = ini->smc_type_v1 != SMC_TYPE_N ? SMC_V1 : 0;
1350 if (pclc->hdr.version > SMC_V1)
1351 ini->smcd_version |=
1352 ini->smc_type_v2 != SMC_TYPE_N ? SMC_V2 : 0;
1353 if (!smc_ism_v2_capable) {
1354 ini->smcd_version &= ~SMC_V2;
1355 goto out;
1356 }
1357 pclc_v2_ext = smc_get_clc_v2_ext(pclc);
1358 if (!pclc_v2_ext) {
1359 ini->smcd_version &= ~SMC_V2;
1360 goto out;
1361 }
1362 pclc_smcd_v2_ext = smc_get_clc_smcd_v2_ext(pclc_v2_ext);
1363 if (!pclc_smcd_v2_ext)
1364 ini->smcd_version &= ~SMC_V2;
1365
1366out:
1367 if (!ini->smcd_version) {
1368 if (pclc->hdr.typev1 == SMC_TYPE_B ||
1369 pclc->hdr.typev2 == SMC_TYPE_B)
1370 return SMC_CLC_DECL_NOSMCDEV;
1371 if (pclc->hdr.typev1 == SMC_TYPE_D ||
1372 pclc->hdr.typev2 == SMC_TYPE_D)
1373 return SMC_CLC_DECL_NOSMCDDEV;
1374 return SMC_CLC_DECL_NOSMCRDEV;
1375 }
1376
1377 return 0;
1378}
1379
Hans Wippel3b2dec22018-05-18 09:34:18 +02001380/* listen worker: check prefixes */
Karsten Graul59886692019-04-12 12:57:25 +02001381static int smc_listen_prfx_check(struct smc_sock *new_smc,
Hans Wippel3b2dec22018-05-18 09:34:18 +02001382 struct smc_clc_msg_proposal *pclc)
1383{
1384 struct smc_clc_msg_proposal_prefix *pclc_prfx;
1385 struct socket *newclcsock = new_smc->clcsock;
1386
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001387 if (pclc->hdr.typev1 == SMC_TYPE_N)
1388 return 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001389 pclc_prfx = smc_clc_proposal_get_prefix(pclc);
1390 if (smc_clc_prfx_match(newclcsock, pclc_prfx))
Karsten Graul59886692019-04-12 12:57:25 +02001391 return SMC_CLC_DECL_DIFFPREFIX;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001392
1393 return 0;
1394}
1395
1396/* listen worker: initialize connection and buffers */
1397static int smc_listen_rdma_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001398 struct smc_init_info *ini)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001399{
Karsten Graul7a627252019-04-12 12:57:30 +02001400 int rc;
1401
Hans Wippel3b2dec22018-05-18 09:34:18 +02001402 /* allocate connection / link group */
Karsten Graul7a627252019-04-12 12:57:30 +02001403 rc = smc_conn_create(new_smc, ini);
1404 if (rc)
1405 return rc;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001406
1407 /* create send buffer and rmb */
Hans Wippelc6ba7c92018-06-28 19:05:07 +02001408 if (smc_buf_create(new_smc, false))
Hans Wippel3b2dec22018-05-18 09:34:18 +02001409 return SMC_CLC_DECL_MEM;
1410
1411 return 0;
1412}
1413
Hans Wippel41349842018-06-28 19:05:11 +02001414/* listen worker: initialize connection and buffers for SMC-D */
1415static int smc_listen_ism_init(struct smc_sock *new_smc,
Karsten Graul7a627252019-04-12 12:57:30 +02001416 struct smc_init_info *ini)
Hans Wippel41349842018-06-28 19:05:11 +02001417{
Karsten Graul7a627252019-04-12 12:57:30 +02001418 int rc;
Hans Wippel41349842018-06-28 19:05:11 +02001419
Karsten Graul7a627252019-04-12 12:57:30 +02001420 rc = smc_conn_create(new_smc, ini);
1421 if (rc)
1422 return rc;
Hans Wippel41349842018-06-28 19:05:11 +02001423
Hans Wippel41349842018-06-28 19:05:11 +02001424 /* Create send and receive buffers */
Karsten Graul72b7f6c2020-07-26 20:34:28 +02001425 rc = smc_buf_create(new_smc, true);
1426 if (rc) {
Ursula Braun5ac54d82020-09-10 18:48:21 +02001427 if (ini->first_contact_local)
Ursula Braun51e3dfa2020-02-25 16:34:36 +01001428 smc_lgr_cleanup_early(&new_smc->conn);
1429 else
1430 smc_conn_free(&new_smc->conn);
Karsten Graul72b7f6c2020-07-26 20:34:28 +02001431 return (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB :
1432 SMC_CLC_DECL_MEM;
Hans Wippel41349842018-06-28 19:05:11 +02001433 }
1434
1435 return 0;
1436}
1437
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001438static bool smc_is_already_selected(struct smcd_dev *smcd,
1439 struct smc_init_info *ini,
1440 int matches)
1441{
1442 int i;
1443
1444 for (i = 0; i < matches; i++)
1445 if (smcd == ini->ism_dev[i])
1446 return true;
1447
1448 return false;
1449}
1450
1451/* check for ISM devices matching proposed ISM devices */
1452static void smc_check_ism_v2_match(struct smc_init_info *ini,
1453 u16 proposed_chid, u64 proposed_gid,
1454 unsigned int *matches)
1455{
1456 struct smcd_dev *smcd;
1457
1458 list_for_each_entry(smcd, &smcd_dev_list.list, list) {
1459 if (smcd->going_away)
1460 continue;
1461 if (smc_is_already_selected(smcd, ini, *matches))
1462 continue;
1463 if (smc_ism_get_chid(smcd) == proposed_chid &&
1464 !smc_ism_cantalk(proposed_gid, ISM_RESERVED_VLANID, smcd)) {
1465 ini->ism_peer_gid[*matches] = proposed_gid;
1466 ini->ism_dev[*matches] = smcd;
1467 (*matches)++;
1468 break;
1469 }
1470 }
1471}
1472
1473static void smc_find_ism_v2_device_serv(struct smc_sock *new_smc,
1474 struct smc_clc_msg_proposal *pclc,
1475 struct smc_init_info *ini)
1476{
1477 struct smc_clc_smcd_v2_extension *smcd_v2_ext;
1478 struct smc_clc_v2_extension *smc_v2_ext;
1479 struct smc_clc_msg_smcd *pclc_smcd;
1480 unsigned int matches = 0;
1481 u8 *eid = NULL;
1482 int i;
1483
1484 if (!(ini->smcd_version & SMC_V2) || !smcd_indicated(ini->smc_type_v2))
1485 return;
1486
1487 pclc_smcd = smc_get_clc_msg_smcd(pclc);
1488 smc_v2_ext = smc_get_clc_v2_ext(pclc);
1489 smcd_v2_ext = smc_get_clc_smcd_v2_ext(smc_v2_ext);
1490 if (!smcd_v2_ext ||
1491 !smc_v2_ext->hdr.flag.seid) /* no system EID support for SMCD */
1492 goto not_found;
1493
1494 mutex_lock(&smcd_dev_list.mutex);
1495 if (pclc_smcd->ism.chid)
1496 /* check for ISM device matching proposed native ISM device */
1497 smc_check_ism_v2_match(ini, ntohs(pclc_smcd->ism.chid),
1498 ntohll(pclc_smcd->ism.gid), &matches);
1499 for (i = 1; i <= smc_v2_ext->hdr.ism_gid_cnt; i++) {
1500 /* check for ISM devices matching proposed non-native ISM
1501 * devices
1502 */
1503 smc_check_ism_v2_match(ini,
1504 ntohs(smcd_v2_ext->gidchid[i - 1].chid),
1505 ntohll(smcd_v2_ext->gidchid[i - 1].gid),
1506 &matches);
1507 }
1508 mutex_unlock(&smcd_dev_list.mutex);
1509
1510 if (ini->ism_dev[0]) {
1511 smc_ism_get_system_eid(ini->ism_dev[0], &eid);
1512 if (memcmp(eid, smcd_v2_ext->system_eid, SMC_MAX_EID_LEN))
1513 goto not_found;
1514 } else {
1515 goto not_found;
1516 }
1517
1518 /* separate - outside the smcd_dev_list.lock */
1519 for (i = 0; i < matches; i++) {
1520 ini->smcd_version = SMC_V2;
1521 ini->is_smcd = true;
1522 ini->ism_selected = i;
1523 if (smc_listen_ism_init(new_smc, ini))
1524 /* try next active ISM device */
1525 continue;
1526 return; /* matching and usable V2 ISM device found */
1527 }
1528
1529not_found:
1530 ini->smcd_version &= ~SMC_V2;
1531 ini->ism_dev[0] = NULL;
1532 ini->is_smcd = false;
1533}
1534
1535static void smc_find_ism_v1_device_serv(struct smc_sock *new_smc,
1536 struct smc_clc_msg_proposal *pclc,
1537 struct smc_init_info *ini)
Ursula Braun7affc802020-09-26 12:44:21 +02001538{
1539 struct smc_clc_msg_smcd *pclc_smcd = smc_get_clc_msg_smcd(pclc);
1540
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001541 /* check if ISM V1 is available */
1542 if (!(ini->smcd_version & SMC_V1) || !smcd_indicated(ini->smc_type_v1))
Ursula Braun7affc802020-09-26 12:44:21 +02001543 goto not_found;
1544 ini->is_smcd = true; /* prepare ISM check */
Ursula Braun8c3dca32020-09-26 12:44:28 +02001545 ini->ism_peer_gid[0] = ntohll(pclc_smcd->ism.gid);
Ursula Braun7affc802020-09-26 12:44:21 +02001546 if (smc_find_ism_device(new_smc, ini))
1547 goto not_found;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001548 ini->ism_selected = 0;
Ursula Braun7affc802020-09-26 12:44:21 +02001549 if (!smc_listen_ism_init(new_smc, ini))
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001550 return; /* V1 ISM device found */
Ursula Braun7affc802020-09-26 12:44:21 +02001551
1552not_found:
Ursula Braun3fc64932020-09-26 12:44:23 +02001553 ini->ism_dev[0] = NULL;
Ursula Braun7affc802020-09-26 12:44:21 +02001554 ini->is_smcd = false;
1555}
1556
Hans Wippel3b2dec22018-05-18 09:34:18 +02001557/* listen worker: register buffers */
Ursula Braun5ac54d82020-09-10 18:48:21 +02001558static int smc_listen_rdma_reg(struct smc_sock *new_smc, bool local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001559{
Karsten Graulb9247542020-04-29 17:10:41 +02001560 struct smc_connection *conn = &new_smc->conn;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001561
Ursula Braun5ac54d82020-09-10 18:48:21 +02001562 if (!local_first) {
Karsten Graul7562a132020-05-01 12:48:01 +02001563 if (smcr_lgr_reg_rmbs(conn->lnk, conn->rmb_desc))
Karsten Graulc7674c02018-11-22 10:26:43 +01001564 return SMC_CLC_DECL_ERR_REGRMB;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001565 }
1566 smc_rmb_sync_sg_for_device(&new_smc->conn);
1567
1568 return 0;
1569}
1570
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001571static int smc_find_rdma_v1_device_serv(struct smc_sock *new_smc,
1572 struct smc_clc_msg_proposal *pclc,
1573 struct smc_init_info *ini)
Ursula Braun7affc802020-09-26 12:44:21 +02001574{
1575 int rc;
1576
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001577 if (!smcr_indicated(ini->smc_type_v1))
Ursula Braun7affc802020-09-26 12:44:21 +02001578 return SMC_CLC_DECL_NOSMCDEV;
1579
1580 /* prepare RDMA check */
1581 ini->ib_lcl = &pclc->lcl;
1582 rc = smc_find_rdma_device(new_smc, ini);
1583 if (rc) {
1584 /* no RDMA device found */
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001585 if (ini->smc_type_v1 == SMC_TYPE_B)
Ursula Braun7affc802020-09-26 12:44:21 +02001586 /* neither ISM nor RDMA device found */
1587 rc = SMC_CLC_DECL_NOSMCDEV;
1588 return rc;
1589 }
1590 rc = smc_listen_rdma_init(new_smc, ini);
1591 if (rc)
1592 return rc;
1593 return smc_listen_rdma_reg(new_smc, ini->first_contact_local);
1594}
1595
1596/* determine the local device matching to proposal */
1597static int smc_listen_find_device(struct smc_sock *new_smc,
1598 struct smc_clc_msg_proposal *pclc,
1599 struct smc_init_info *ini)
1600{
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001601 int rc;
1602
1603 /* check for ISM device matching V2 proposed device */
1604 smc_find_ism_v2_device_serv(new_smc, pclc, ini);
1605 if (ini->ism_dev[0])
Ursula Braun7affc802020-09-26 12:44:21 +02001606 return 0;
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001607
1608 if (!(ini->smcd_version & SMC_V1))
1609 return SMC_CLC_DECL_NOSMCDEV;
1610
1611 /* check for matching IP prefix and subnet length */
1612 rc = smc_listen_prfx_check(new_smc, pclc);
1613 if (rc)
1614 return rc;
1615
1616 /* get vlan id from IP device */
1617 if (smc_vlan_by_tcpsk(new_smc->clcsock, ini))
1618 return SMC_CLC_DECL_GETVLANERR;
1619
1620 /* check for ISM device matching V1 proposed device */
1621 smc_find_ism_v1_device_serv(new_smc, pclc, ini);
1622 if (ini->ism_dev[0])
1623 return 0;
1624
Ursula Braun7affc802020-09-26 12:44:21 +02001625 if (pclc->hdr.typev1 == SMC_TYPE_D)
1626 return SMC_CLC_DECL_NOSMCDDEV; /* skip RDMA and decline */
1627
1628 /* check if RDMA is available */
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001629 return smc_find_rdma_v1_device_serv(new_smc, pclc, ini);
Ursula Braun7affc802020-09-26 12:44:21 +02001630}
1631
Hans Wippel3b2dec22018-05-18 09:34:18 +02001632/* listen worker: finish RDMA setup */
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001633static int smc_listen_rdma_finish(struct smc_sock *new_smc,
1634 struct smc_clc_msg_accept_confirm *cclc,
Ursula Braun5ac54d82020-09-10 18:48:21 +02001635 bool local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001636{
Karsten Graul387707f2020-04-29 17:10:40 +02001637 struct smc_link *link = new_smc->conn.lnk;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001638 int reason_code = 0;
1639
Ursula Braun5ac54d82020-09-10 18:48:21 +02001640 if (local_first)
Hans Wippel3b2dec22018-05-18 09:34:18 +02001641 smc_link_save_peer_info(link, cclc);
1642
Ursula Braun0c881ad2020-09-10 18:48:25 +02001643 if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc))
1644 return SMC_CLC_DECL_ERR_RTOK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001645
Ursula Braun5ac54d82020-09-10 18:48:21 +02001646 if (local_first) {
Ursula Braun0c881ad2020-09-10 18:48:25 +02001647 if (smc_ib_ready_link(link))
1648 return SMC_CLC_DECL_ERR_RDYLNK;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001649 /* QP confirmation over RoCE fabric */
Karsten Graul4667bb42020-04-30 15:55:42 +02001650 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK);
Karsten Graulb9247542020-04-29 17:10:41 +02001651 reason_code = smcr_serv_conf_first_link(new_smc);
Karsten Graul4667bb42020-04-30 15:55:42 +02001652 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001653 }
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001654 return reason_code;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001655}
1656
Ursula Braun7affc802020-09-26 12:44:21 +02001657/* setup for connection of server */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001658static void smc_listen_work(struct work_struct *work)
1659{
1660 struct smc_sock *new_smc = container_of(work, struct smc_sock,
1661 smc_listen_work);
Ursula Braune8d726c82020-09-26 12:44:32 +02001662 u8 version = smc_ism_v2_capable ? SMC_V2 : SMC_V1;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001663 struct socket *newclcsock = new_smc->clcsock;
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001664 struct smc_clc_msg_accept_confirm_v2 *cclc2;
1665 struct smc_clc_msg_accept_confirm *cclc;
Ursula Braun6bb14e482020-09-10 18:48:22 +02001666 struct smc_clc_msg_proposal_area *buf;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001667 struct smc_clc_msg_proposal *pclc;
Ursula Braun3fc64932020-09-26 12:44:23 +02001668 struct smc_init_info *ini = NULL;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001669 int rc = 0;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001670
Karsten Graulfd577702019-04-11 11:17:30 +02001671 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN)
1672 return smc_listen_out_err(new_smc);
1673
Hans Wippel3b2dec22018-05-18 09:34:18 +02001674 if (new_smc->use_fallback) {
1675 smc_listen_out_connected(new_smc);
1676 return;
1677 }
1678
1679 /* check if peer is smc capable */
1680 if (!tcp_sk(newclcsock->sk)->syn_smc) {
Ursula Braun07603b22019-04-11 11:17:32 +02001681 smc_switch_to_fallback(new_smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001682 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001683 smc_listen_out_connected(new_smc);
1684 return;
1685 }
1686
1687 /* do inband token exchange -
1688 * wait for and receive SMC Proposal CLC message
1689 */
Ursula Braun6bb14e482020-09-10 18:48:22 +02001690 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
1691 if (!buf) {
1692 rc = SMC_CLC_DECL_MEM;
1693 goto out_decl;
1694 }
1695 pclc = (struct smc_clc_msg_proposal *)buf;
1696 rc = smc_clc_wait_msg(new_smc, pclc, sizeof(*buf),
Karsten Graul228bae02019-04-12 12:57:28 +02001697 SMC_CLC_PROPOSAL, CLC_WAIT_TIME);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001698 if (rc)
1699 goto out_decl;
Ursula Braune8d726c82020-09-26 12:44:32 +02001700 version = pclc->hdr.version == SMC_V1 ? SMC_V1 : version;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001701
Ursula Braune8d726c82020-09-26 12:44:32 +02001702 /* IPSec connections opt out of SMC optimizations */
Hans Wippel3b2dec22018-05-18 09:34:18 +02001703 if (using_ipsec(new_smc)) {
Karsten Graul9aa68d22019-04-12 12:57:29 +02001704 rc = SMC_CLC_DECL_IPSEC;
1705 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001706 }
1707
Ursula Braun3fc64932020-09-26 12:44:23 +02001708 ini = kzalloc(sizeof(*ini), GFP_KERNEL);
1709 if (!ini) {
1710 rc = SMC_CLC_DECL_MEM;
1711 goto out_decl;
1712 }
1713
Ursula Braun5c21c4c2020-09-26 12:44:29 +02001714 /* initial version checking */
1715 rc = smc_listen_v2_check(new_smc, pclc, ini);
1716 if (rc)
Karsten Graul9aa68d22019-04-12 12:57:29 +02001717 goto out_decl;
Karsten Graulfba7e8e2019-04-12 12:57:27 +02001718
Hans Wippel72a36a82019-02-07 15:56:18 +01001719 mutex_lock(&smc_server_lgr_pending);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001720 smc_close_init(new_smc);
1721 smc_rx_init(new_smc);
1722 smc_tx_init(new_smc);
1723
Ursula Braun7affc802020-09-26 12:44:21 +02001724 /* determine ISM or RoCE device used for connection */
Ursula Braun3fc64932020-09-26 12:44:23 +02001725 rc = smc_listen_find_device(new_smc, pclc, ini);
Ursula Braun7affc802020-09-26 12:44:21 +02001726 if (rc)
1727 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001728
1729 /* send SMC Accept CLC message */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001730 rc = smc_clc_send_accept(new_smc, ini->first_contact_local,
1731 ini->smcd_version == SMC_V2 ? SMC_V2 : SMC_V1);
Karsten Graul9aa68d22019-04-12 12:57:29 +02001732 if (rc)
1733 goto out_unlock;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001734
Hans Wippel62c71392019-02-07 15:56:17 +01001735 /* SMC-D does not need this lock any more */
Ursula Braun3fc64932020-09-26 12:44:23 +02001736 if (ini->is_smcd)
Hans Wippel72a36a82019-02-07 15:56:18 +01001737 mutex_unlock(&smc_server_lgr_pending);
Hans Wippel62c71392019-02-07 15:56:17 +01001738
Hans Wippel3b2dec22018-05-18 09:34:18 +02001739 /* receive SMC Confirm CLC message */
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001740 cclc2 = (struct smc_clc_msg_accept_confirm_v2 *)buf;
1741 cclc = (struct smc_clc_msg_accept_confirm *)cclc2;
1742 memset(buf, 0, sizeof(struct smc_clc_msg_proposal_area));
1743 rc = smc_clc_wait_msg(new_smc, cclc2,
1744 sizeof(struct smc_clc_msg_proposal_area),
Karsten Graul228bae02019-04-12 12:57:28 +02001745 SMC_CLC_CONFIRM, CLC_WAIT_TIME);
1746 if (rc) {
Ursula Braun3fc64932020-09-26 12:44:23 +02001747 if (!ini->is_smcd)
Karsten Graul9aa68d22019-04-12 12:57:29 +02001748 goto out_unlock;
1749 goto out_decl;
Hans Wippel3b2dec22018-05-18 09:34:18 +02001750 }
1751
1752 /* finish worker */
Ursula Braun3fc64932020-09-26 12:44:23 +02001753 if (!ini->is_smcd) {
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001754 rc = smc_listen_rdma_finish(new_smc, cclc,
Ursula Braun3fc64932020-09-26 12:44:23 +02001755 ini->first_contact_local);
Hans Wippel62c71392019-02-07 15:56:17 +01001756 if (rc)
Ursula Braun0c881ad2020-09-10 18:48:25 +02001757 goto out_unlock;
1758 mutex_unlock(&smc_server_lgr_pending);
Ursula Braun1ca52fc2018-09-18 15:46:35 +02001759 }
Ursula Brauna7c9c5f2020-09-26 12:44:30 +02001760 smc_conn_save_peer_info(new_smc, cclc);
Hans Wippel3b2dec22018-05-18 09:34:18 +02001761 smc_listen_out_connected(new_smc);
Ursula Braunac679362020-09-17 22:46:02 +02001762 goto out_free;
Karsten Graul9aa68d22019-04-12 12:57:29 +02001763
1764out_unlock:
1765 mutex_unlock(&smc_server_lgr_pending);
1766out_decl:
Ursula Braune8d726c82020-09-26 12:44:32 +02001767 smc_listen_decline(new_smc, rc, ini, version);
Ursula Braunac679362020-09-17 22:46:02 +02001768out_free:
Ursula Braun3fc64932020-09-26 12:44:23 +02001769 kfree(ini);
Ursula Braun6bb14e482020-09-10 18:48:22 +02001770 kfree(buf);
Ursula Brauna046d572017-01-09 16:55:16 +01001771}
1772
1773static void smc_tcp_listen_work(struct work_struct *work)
1774{
1775 struct smc_sock *lsmc = container_of(work, struct smc_sock,
1776 tcp_listen_work);
Ursula Braun3163c502018-01-24 10:28:12 +01001777 struct sock *lsk = &lsmc->sk;
Ursula Brauna046d572017-01-09 16:55:16 +01001778 struct smc_sock *new_smc;
1779 int rc = 0;
1780
Ursula Braun3163c502018-01-24 10:28:12 +01001781 lock_sock(lsk);
1782 while (lsk->sk_state == SMC_LISTEN) {
Ursula Brauna046d572017-01-09 16:55:16 +01001783 rc = smc_clcsock_accept(lsmc, &new_smc);
Ursula Brauna60a2b12020-09-10 18:48:20 +02001784 if (rc) /* clcsock accept queue empty or error */
Ursula Brauna046d572017-01-09 16:55:16 +01001785 goto out;
1786 if (!new_smc)
1787 continue;
1788
1789 new_smc->listen_smc = lsmc;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001790 new_smc->use_fallback = lsmc->use_fallback;
Karsten Graul603cc142018-07-25 16:35:32 +02001791 new_smc->fallback_rsn = lsmc->fallback_rsn;
Ursula Braun3163c502018-01-24 10:28:12 +01001792 sock_hold(lsk); /* sock_put in smc_listen_work */
Ursula Brauna046d572017-01-09 16:55:16 +01001793 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work);
1794 smc_copy_sock_settings_to_smc(new_smc);
Ursula Braunbd58c7e2018-08-08 14:13:20 +02001795 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf;
1796 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf;
Ursula Braun51f1de72018-01-26 09:28:48 +01001797 sock_hold(&new_smc->sk); /* sock_put in passive closing */
Karsten Graul22ef4732020-09-10 18:48:29 +02001798 if (!queue_work(smc_hs_wq, &new_smc->smc_listen_work))
Ursula Braun51f1de72018-01-26 09:28:48 +01001799 sock_put(&new_smc->sk);
Ursula Brauna046d572017-01-09 16:55:16 +01001800 }
1801
1802out:
Ursula Braun3163c502018-01-24 10:28:12 +01001803 release_sock(lsk);
Ursula Brauna60a2b12020-09-10 18:48:20 +02001804 sock_put(&lsmc->sk); /* sock_hold in smc_clcsock_data_ready() */
1805}
1806
1807static void smc_clcsock_data_ready(struct sock *listen_clcsock)
1808{
1809 struct smc_sock *lsmc;
1810
1811 lsmc = (struct smc_sock *)
1812 ((uintptr_t)listen_clcsock->sk_user_data & ~SK_USER_DATA_NOCOPY);
1813 if (!lsmc)
1814 return;
1815 lsmc->clcsk_data_ready(listen_clcsock);
1816 if (lsmc->sk.sk_state == SMC_LISTEN) {
1817 sock_hold(&lsmc->sk); /* sock_put in smc_tcp_listen_work() */
Karsten Graul22ef4732020-09-10 18:48:29 +02001818 if (!queue_work(smc_hs_wq, &lsmc->tcp_listen_work))
Ursula Brauna60a2b12020-09-10 18:48:20 +02001819 sock_put(&lsmc->sk);
1820 }
Ursula Brauna046d572017-01-09 16:55:16 +01001821}
1822
Ursula Braunac713872017-01-09 16:55:13 +01001823static int smc_listen(struct socket *sock, int backlog)
1824{
1825 struct sock *sk = sock->sk;
1826 struct smc_sock *smc;
1827 int rc;
1828
1829 smc = smc_sk(sk);
1830 lock_sock(sk);
1831
1832 rc = -EINVAL;
Ursula Brauncd206362019-08-02 10:47:50 +02001833 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) ||
1834 smc->connect_nonblock)
Ursula Braunac713872017-01-09 16:55:13 +01001835 goto out;
1836
1837 rc = 0;
1838 if (sk->sk_state == SMC_LISTEN) {
1839 sk->sk_max_ack_backlog = backlog;
1840 goto out;
1841 }
1842 /* some socket options are handled in core, so we could not apply
1843 * them to the clc socket -- copy smc socket options to clc socket
1844 */
1845 smc_copy_sock_settings_to_clc(smc);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001846 if (!smc->use_fallback)
1847 tcp_sk(smc->clcsock->sk)->syn_smc = 1;
Ursula Braunac713872017-01-09 16:55:13 +01001848
Ursula Brauna60a2b12020-09-10 18:48:20 +02001849 /* save original sk_data_ready function and establish
1850 * smc-specific sk_data_ready function
1851 */
1852 smc->clcsk_data_ready = smc->clcsock->sk->sk_data_ready;
1853 smc->clcsock->sk->sk_data_ready = smc_clcsock_data_ready;
1854 smc->clcsock->sk->sk_user_data =
1855 (void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY);
Ursula Braunac713872017-01-09 16:55:13 +01001856 rc = kernel_listen(smc->clcsock, backlog);
1857 if (rc)
1858 goto out;
1859 sk->sk_max_ack_backlog = backlog;
1860 sk->sk_ack_backlog = 0;
1861 sk->sk_state = SMC_LISTEN;
1862
1863out:
1864 release_sock(sk);
1865 return rc;
1866}
1867
1868static int smc_accept(struct socket *sock, struct socket *new_sock,
David Howellscdfbabf2017-03-09 08:09:05 +00001869 int flags, bool kern)
Ursula Braunac713872017-01-09 16:55:13 +01001870{
Ursula Brauna046d572017-01-09 16:55:16 +01001871 struct sock *sk = sock->sk, *nsk;
1872 DECLARE_WAITQUEUE(wait, current);
Ursula Braunac713872017-01-09 16:55:13 +01001873 struct smc_sock *lsmc;
Ursula Brauna046d572017-01-09 16:55:16 +01001874 long timeo;
1875 int rc = 0;
Ursula Braunac713872017-01-09 16:55:13 +01001876
1877 lsmc = smc_sk(sk);
Ursula Braun51f1de72018-01-26 09:28:48 +01001878 sock_hold(sk); /* sock_put below */
Ursula Braunac713872017-01-09 16:55:13 +01001879 lock_sock(sk);
1880
1881 if (lsmc->sk.sk_state != SMC_LISTEN) {
1882 rc = -EINVAL;
Ursula Braunabb190f2018-04-26 17:18:23 +02001883 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01001884 goto out;
1885 }
1886
Ursula Brauna046d572017-01-09 16:55:16 +01001887 /* Wait for an incoming connection */
1888 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1889 add_wait_queue_exclusive(sk_sleep(sk), &wait);
1890 while (!(nsk = smc_accept_dequeue(sk, new_sock))) {
1891 set_current_state(TASK_INTERRUPTIBLE);
1892 if (!timeo) {
1893 rc = -EAGAIN;
1894 break;
1895 }
1896 release_sock(sk);
1897 timeo = schedule_timeout(timeo);
1898 /* wakeup by sk_data_ready in smc_listen_work() */
1899 sched_annotate_sleep();
1900 lock_sock(sk);
1901 if (signal_pending(current)) {
1902 rc = sock_intr_errno(timeo);
1903 break;
1904 }
1905 }
1906 set_current_state(TASK_RUNNING);
1907 remove_wait_queue(sk_sleep(sk), &wait);
Ursula Braunac713872017-01-09 16:55:13 +01001908
Ursula Brauna046d572017-01-09 16:55:16 +01001909 if (!rc)
1910 rc = sock_error(nsk);
Ursula Braunabb190f2018-04-26 17:18:23 +02001911 release_sock(sk);
1912 if (rc)
1913 goto out;
1914
1915 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) {
1916 /* wait till data arrives on the socket */
1917 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept *
1918 MSEC_PER_SEC);
1919 if (smc_sk(nsk)->use_fallback) {
1920 struct sock *clcsk = smc_sk(nsk)->clcsock->sk;
1921
1922 lock_sock(clcsk);
1923 if (skb_queue_empty(&clcsk->sk_receive_queue))
1924 sk_wait_data(clcsk, &timeo, NULL);
1925 release_sock(clcsk);
1926 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) {
1927 lock_sock(nsk);
Stefan Rasplb51fa1b2018-05-03 18:12:37 +02001928 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available);
Ursula Braunabb190f2018-04-26 17:18:23 +02001929 release_sock(nsk);
1930 }
1931 }
Ursula Braunac713872017-01-09 16:55:13 +01001932
1933out:
Ursula Braun51f1de72018-01-26 09:28:48 +01001934 sock_put(sk); /* sock_hold above */
Ursula Braunac713872017-01-09 16:55:13 +01001935 return rc;
1936}
1937
1938static int smc_getname(struct socket *sock, struct sockaddr *addr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001939 int peer)
Ursula Braunac713872017-01-09 16:55:13 +01001940{
1941 struct smc_sock *smc;
1942
Ursula Braunb38d7322017-01-09 16:55:25 +01001943 if (peer && (sock->sk->sk_state != SMC_ACTIVE) &&
1944 (sock->sk->sk_state != SMC_APPCLOSEWAIT1))
Ursula Braunac713872017-01-09 16:55:13 +01001945 return -ENOTCONN;
1946
1947 smc = smc_sk(sock->sk);
1948
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001949 return smc->clcsock->ops->getname(smc->clcsock, addr, peer);
Ursula Braunac713872017-01-09 16:55:13 +01001950}
1951
1952static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1953{
1954 struct sock *sk = sock->sk;
1955 struct smc_sock *smc;
1956 int rc = -EPIPE;
1957
1958 smc = smc_sk(sk);
1959 lock_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01001960 if ((sk->sk_state != SMC_ACTIVE) &&
1961 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
1962 (sk->sk_state != SMC_INIT))
Ursula Braunac713872017-01-09 16:55:13 +01001963 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001964
1965 if (msg->msg_flags & MSG_FASTOPEN) {
Ursula Brauncd206362019-08-02 10:47:50 +02001966 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02001967 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02001968 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02001969 } else {
1970 rc = -EINVAL;
1971 goto out;
1972 }
1973 }
1974
Ursula Braunac713872017-01-09 16:55:13 +01001975 if (smc->use_fallback)
1976 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len);
1977 else
Ursula Braune6727f32017-01-09 16:55:23 +01001978 rc = smc_tx_sendmsg(smc, msg, len);
Ursula Braunac713872017-01-09 16:55:13 +01001979out:
1980 release_sock(sk);
1981 return rc;
1982}
1983
1984static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1985 int flags)
1986{
1987 struct sock *sk = sock->sk;
1988 struct smc_sock *smc;
1989 int rc = -ENOTCONN;
1990
1991 smc = smc_sk(sk);
1992 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01001993 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
1994 /* socket was connected before, no more data to read */
1995 rc = 0;
1996 goto out;
1997 }
Ursula Braunb38d7322017-01-09 16:55:25 +01001998 if ((sk->sk_state == SMC_INIT) ||
1999 (sk->sk_state == SMC_LISTEN) ||
2000 (sk->sk_state == SMC_CLOSED))
Ursula Braunac713872017-01-09 16:55:13 +01002001 goto out;
2002
Ursula Braunb38d7322017-01-09 16:55:25 +01002003 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2004 rc = 0;
2005 goto out;
2006 }
2007
Stefan Raspl9014db22018-05-03 18:12:39 +02002008 if (smc->use_fallback) {
Ursula Braunac713872017-01-09 16:55:13 +01002009 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags);
Stefan Raspl9014db22018-05-03 18:12:39 +02002010 } else {
2011 msg->msg_namelen = 0;
2012 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags);
2013 }
Ursula Braunb38d7322017-01-09 16:55:25 +01002014
Ursula Braunac713872017-01-09 16:55:13 +01002015out:
2016 release_sock(sk);
2017 return rc;
2018}
2019
Al Viroade994f2017-07-03 00:01:49 -04002020static __poll_t smc_accept_poll(struct sock *parent)
Ursula Brauna046d572017-01-09 16:55:16 +01002021{
Ursula Braun8dce2782018-01-26 09:28:47 +01002022 struct smc_sock *isk = smc_sk(parent);
Al Viro63e24802018-02-01 10:02:53 -05002023 __poll_t mask = 0;
Ursula Brauna046d572017-01-09 16:55:16 +01002024
Ursula Braun8dce2782018-01-26 09:28:47 +01002025 spin_lock(&isk->accept_q_lock);
2026 if (!list_empty(&isk->accept_q))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002027 mask = EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01002028 spin_unlock(&isk->accept_q_lock);
Ursula Brauna046d572017-01-09 16:55:16 +01002029
Ursula Braun8dce2782018-01-26 09:28:47 +01002030 return mask;
Ursula Brauna046d572017-01-09 16:55:16 +01002031}
2032
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07002033static __poll_t smc_poll(struct file *file, struct socket *sock,
2034 poll_table *wait)
Ursula Braunac713872017-01-09 16:55:13 +01002035{
2036 struct sock *sk = sock->sk;
Ursula Braunac713872017-01-09 16:55:13 +01002037 struct smc_sock *smc;
Ursula Braun50717a32019-04-12 12:57:23 +02002038 __poll_t mask = 0;
Ursula Braunac713872017-01-09 16:55:13 +01002039
Ursula Braun8dce2782018-01-26 09:28:47 +01002040 if (!sk)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002041 return EPOLLNVAL;
Ursula Braun8dce2782018-01-26 09:28:47 +01002042
Ursula Braunac713872017-01-09 16:55:13 +01002043 smc = smc_sk(sock->sk);
Ursula Braun648a5a72018-09-18 15:46:34 +02002044 if (smc->use_fallback) {
Ursula Brauna046d572017-01-09 16:55:16 +01002045 /* delegate to CLC child sock */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07002046 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait);
Ursula Braun784813a2018-05-02 16:53:56 +02002047 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braunac713872017-01-09 16:55:13 +01002048 } else {
Linus Torvalds410da1e2018-07-03 09:53:43 -07002049 if (sk->sk_state != SMC_CLOSED)
Karsten Graul89ab0662018-10-23 13:40:39 +02002050 sock_poll_wait(file, sock, wait);
Ursula Brauna046d572017-01-09 16:55:16 +01002051 if (sk->sk_err)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002052 mask |= EPOLLERR;
Ursula Braunb38d7322017-01-09 16:55:25 +01002053 if ((sk->sk_shutdown == SHUTDOWN_MASK) ||
2054 (sk->sk_state == SMC_CLOSED))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002055 mask |= EPOLLHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01002056 if (sk->sk_state == SMC_LISTEN) {
2057 /* woken up by sk_data_ready in smc_listen_work() */
Ursula Braun50717a32019-04-12 12:57:23 +02002058 mask |= smc_accept_poll(sk);
2059 } else if (smc->use_fallback) { /* as result of connect_work()*/
2060 mask |= smc->clcsock->ops->poll(file, smc->clcsock,
2061 wait);
2062 sk->sk_err = smc->clcsock->sk->sk_err;
Ursula Braun8dce2782018-01-26 09:28:47 +01002063 } else {
Ursula Braun50717a32019-04-12 12:57:23 +02002064 if ((sk->sk_state != SMC_INIT &&
2065 atomic_read(&smc->conn.sndbuf_space)) ||
Ursula Braun8dce2782018-01-26 09:28:47 +01002066 sk->sk_shutdown & SEND_SHUTDOWN) {
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002067 mask |= EPOLLOUT | EPOLLWRNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01002068 } else {
2069 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk);
2070 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
2071 }
2072 if (atomic_read(&smc->conn.bytes_to_rcv))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002073 mask |= EPOLLIN | EPOLLRDNORM;
Ursula Braun8dce2782018-01-26 09:28:47 +01002074 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002075 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
Ursula Braun8dce2782018-01-26 09:28:47 +01002076 if (sk->sk_state == SMC_APPCLOSEWAIT1)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002077 mask |= EPOLLIN;
Karsten Graul71d117f2018-09-18 15:46:37 +02002078 if (smc->conn.urg_state == SMC_URG_VALID)
2079 mask |= EPOLLPRI;
Ursula Braun8dce2782018-01-26 09:28:47 +01002080 }
Ursula Braunac713872017-01-09 16:55:13 +01002081 }
2082
2083 return mask;
2084}
2085
2086static int smc_shutdown(struct socket *sock, int how)
2087{
2088 struct sock *sk = sock->sk;
2089 struct smc_sock *smc;
2090 int rc = -EINVAL;
Ursula Braunb38d7322017-01-09 16:55:25 +01002091 int rc1 = 0;
Ursula Braunac713872017-01-09 16:55:13 +01002092
2093 smc = smc_sk(sk);
2094
2095 if ((how < SHUT_RD) || (how > SHUT_RDWR))
Ursula Braunb38d7322017-01-09 16:55:25 +01002096 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01002097
2098 lock_sock(sk);
2099
2100 rc = -ENOTCONN;
Ursula Brauncaa21e12018-08-08 14:13:19 +02002101 if ((sk->sk_state != SMC_ACTIVE) &&
Ursula Braunb38d7322017-01-09 16:55:25 +01002102 (sk->sk_state != SMC_PEERCLOSEWAIT1) &&
2103 (sk->sk_state != SMC_PEERCLOSEWAIT2) &&
2104 (sk->sk_state != SMC_APPCLOSEWAIT1) &&
2105 (sk->sk_state != SMC_APPCLOSEWAIT2) &&
2106 (sk->sk_state != SMC_APPFINCLOSEWAIT))
Ursula Braunac713872017-01-09 16:55:13 +01002107 goto out;
2108 if (smc->use_fallback) {
2109 rc = kernel_sock_shutdown(smc->clcsock, how);
2110 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown;
2111 if (sk->sk_shutdown == SHUTDOWN_MASK)
2112 sk->sk_state = SMC_CLOSED;
Ursula Braunb38d7322017-01-09 16:55:25 +01002113 goto out;
Ursula Braunac713872017-01-09 16:55:13 +01002114 }
Ursula Braunb38d7322017-01-09 16:55:25 +01002115 switch (how) {
2116 case SHUT_RDWR: /* shutdown in both directions */
2117 rc = smc_close_active(smc);
2118 break;
2119 case SHUT_WR:
2120 rc = smc_close_shutdown_write(smc);
2121 break;
2122 case SHUT_RD:
Ursula Braun1255fcb2018-04-19 15:56:40 +02002123 rc = 0;
2124 /* nothing more to do because peer is not involved */
Ursula Braunb38d7322017-01-09 16:55:25 +01002125 break;
2126 }
Ursula Braun1255fcb2018-04-19 15:56:40 +02002127 if (smc->clcsock)
2128 rc1 = kernel_sock_shutdown(smc->clcsock, how);
Ursula Braunb38d7322017-01-09 16:55:25 +01002129 /* map sock_shutdown_cmd constants to sk_shutdown value range */
2130 sk->sk_shutdown |= how + 1;
Ursula Braunac713872017-01-09 16:55:13 +01002131
2132out:
2133 release_sock(sk);
Ursula Braunb38d7322017-01-09 16:55:25 +01002134 return rc ? rc : rc1;
Ursula Braunac713872017-01-09 16:55:13 +01002135}
2136
2137static int smc_setsockopt(struct socket *sock, int level, int optname,
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02002138 sockptr_t optval, unsigned int optlen)
Ursula Braunac713872017-01-09 16:55:13 +01002139{
2140 struct sock *sk = sock->sk;
2141 struct smc_sock *smc;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002142 int val, rc;
Ursula Braunac713872017-01-09 16:55:13 +01002143
2144 smc = smc_sk(sk);
2145
2146 /* generic setsockopts reaching us here always apply to the
2147 * CLC socket
2148 */
Christoph Hellwiga44d9e72020-07-17 08:23:31 +02002149 if (unlikely(!smc->clcsock->ops->setsockopt))
2150 rc = -EOPNOTSUPP;
2151 else
2152 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname,
2153 optval, optlen);
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002154 if (smc->clcsock->sk->sk_err) {
2155 sk->sk_err = smc->clcsock->sk->sk_err;
2156 sk->sk_error_report(sk);
2157 }
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002158
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002159 if (optlen < sizeof(int))
Wei Yongjun3dc9f552018-05-31 02:31:22 +00002160 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02002161 if (copy_from_sockptr(&val, optval, sizeof(int)))
Ursula Braunac0107e2018-07-18 15:22:50 +02002162 return -EFAULT;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002163
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002164 lock_sock(sk);
Ursula Braun86434742019-12-12 22:35:58 +01002165 if (rc || smc->use_fallback)
2166 goto out;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002167 switch (optname) {
2168 case TCP_ULP:
2169 case TCP_FASTOPEN:
2170 case TCP_FASTOPEN_CONNECT:
2171 case TCP_FASTOPEN_KEY:
2172 case TCP_FASTOPEN_NO_COOKIE:
2173 /* option not supported by SMC */
Ursula Braun8204df72019-11-15 12:39:30 +01002174 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) {
Ursula Braun07603b22019-04-11 11:17:32 +02002175 smc_switch_to_fallback(smc);
Karsten Graul603cc142018-07-25 16:35:32 +02002176 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002177 } else {
Ursula Braun86434742019-12-12 22:35:58 +01002178 rc = -EINVAL;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002179 }
2180 break;
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002181 case TCP_NODELAY:
Ursula Braunf9cedf12019-08-02 10:16:38 +02002182 if (sk->sk_state != SMC_INIT &&
2183 sk->sk_state != SMC_LISTEN &&
2184 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01002185 if (val)
Karsten Graul22ef4732020-09-10 18:48:29 +02002186 mod_delayed_work(smc->conn.lgr->tx_wq,
2187 &smc->conn.tx_work, 0);
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002188 }
2189 break;
2190 case TCP_CORK:
Ursula Braunf9cedf12019-08-02 10:16:38 +02002191 if (sk->sk_state != SMC_INIT &&
2192 sk->sk_state != SMC_LISTEN &&
2193 sk->sk_state != SMC_CLOSED) {
Ursula Braun86434742019-12-12 22:35:58 +01002194 if (!val)
Karsten Graul22ef4732020-09-10 18:48:29 +02002195 mod_delayed_work(smc->conn.lgr->tx_wq,
2196 &smc->conn.tx_work, 0);
Ursula Braun01d2f7e2018-04-26 17:18:22 +02002197 }
2198 break;
Ursula Braunabb190f2018-04-26 17:18:23 +02002199 case TCP_DEFER_ACCEPT:
2200 smc->sockopt_defer_accept = val;
2201 break;
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002202 default:
2203 break;
2204 }
Ursula Braun86434742019-12-12 22:35:58 +01002205out:
Ursula Braunee9dfbe2018-04-26 17:18:21 +02002206 release_sock(sk);
2207
2208 return rc;
Ursula Braunac713872017-01-09 16:55:13 +01002209}
2210
2211static int smc_getsockopt(struct socket *sock, int level, int optname,
2212 char __user *optval, int __user *optlen)
2213{
2214 struct smc_sock *smc;
2215
2216 smc = smc_sk(sock->sk);
2217 /* socket options apply to the CLC socket */
Christoph Hellwiga44d9e72020-07-17 08:23:31 +02002218 if (unlikely(!smc->clcsock->ops->getsockopt))
2219 return -EOPNOTSUPP;
Ursula Braunac713872017-01-09 16:55:13 +01002220 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname,
2221 optval, optlen);
2222}
2223
2224static int smc_ioctl(struct socket *sock, unsigned int cmd,
2225 unsigned long arg)
2226{
Stefan Rasplde8474e2018-05-23 16:38:11 +02002227 union smc_host_cursor cons, urg;
2228 struct smc_connection *conn;
Ursula Braunac713872017-01-09 16:55:13 +01002229 struct smc_sock *smc;
Ursula Braun9b67e262018-05-02 16:56:46 +02002230 int answ;
Ursula Braunac713872017-01-09 16:55:13 +01002231
2232 smc = smc_sk(sock->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02002233 conn = &smc->conn;
Ursula Braun1992d992018-07-16 13:56:52 +02002234 lock_sock(&smc->sk);
Ursula Braun7311d662018-08-08 14:13:21 +02002235 if (smc->use_fallback) {
2236 if (!smc->clcsock) {
2237 release_sock(&smc->sk);
2238 return -EBADF;
2239 }
2240 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg);
2241 release_sock(&smc->sk);
2242 return answ;
2243 }
Ursula Braun9b67e262018-05-02 16:56:46 +02002244 switch (cmd) {
2245 case SIOCINQ: /* same as FIONREAD */
Ursula Braun1992d992018-07-16 13:56:52 +02002246 if (smc->sk.sk_state == SMC_LISTEN) {
2247 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002248 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002249 }
Ursula Braun2351abe2018-05-23 16:38:09 +02002250 if (smc->sk.sk_state == SMC_INIT ||
2251 smc->sk.sk_state == SMC_CLOSED)
2252 answ = 0;
2253 else
2254 answ = atomic_read(&smc->conn.bytes_to_rcv);
Ursula Braun9b67e262018-05-02 16:56:46 +02002255 break;
2256 case SIOCOUTQ:
2257 /* output queue size (not send + not acked) */
Ursula Braun1992d992018-07-16 13:56:52 +02002258 if (smc->sk.sk_state == SMC_LISTEN) {
2259 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002260 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002261 }
Ursula Braun2351abe2018-05-23 16:38:09 +02002262 if (smc->sk.sk_state == SMC_INIT ||
2263 smc->sk.sk_state == SMC_CLOSED)
2264 answ = 0;
2265 else
2266 answ = smc->conn.sndbuf_desc->len -
Ursula Braun9b67e262018-05-02 16:56:46 +02002267 atomic_read(&smc->conn.sndbuf_space);
2268 break;
2269 case SIOCOUTQNSD:
2270 /* output queue size (not send only) */
Ursula Braun1992d992018-07-16 13:56:52 +02002271 if (smc->sk.sk_state == SMC_LISTEN) {
2272 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002273 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002274 }
Ursula Braun2351abe2018-05-23 16:38:09 +02002275 if (smc->sk.sk_state == SMC_INIT ||
2276 smc->sk.sk_state == SMC_CLOSED)
2277 answ = 0;
2278 else
2279 answ = smc_tx_prepared_sends(&smc->conn);
Ursula Braun9b67e262018-05-02 16:56:46 +02002280 break;
Stefan Rasplde8474e2018-05-23 16:38:11 +02002281 case SIOCATMARK:
Ursula Braun1992d992018-07-16 13:56:52 +02002282 if (smc->sk.sk_state == SMC_LISTEN) {
2283 release_sock(&smc->sk);
Stefan Rasplde8474e2018-05-23 16:38:11 +02002284 return -EINVAL;
Ursula Braun1992d992018-07-16 13:56:52 +02002285 }
Stefan Rasplde8474e2018-05-23 16:38:11 +02002286 if (smc->sk.sk_state == SMC_INIT ||
2287 smc->sk.sk_state == SMC_CLOSED) {
2288 answ = 0;
2289 } else {
Stefan Rasplbac6de72018-07-23 13:53:09 +02002290 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn);
2291 smc_curs_copy(&urg, &conn->urg_curs, conn);
Stefan Rasplde8474e2018-05-23 16:38:11 +02002292 answ = smc_curs_diff(conn->rmb_desc->len,
2293 &cons, &urg) == 1;
2294 }
2295 break;
Ursula Braun9b67e262018-05-02 16:56:46 +02002296 default:
Ursula Braun1992d992018-07-16 13:56:52 +02002297 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002298 return -ENOIOCTLCMD;
2299 }
Ursula Braun1992d992018-07-16 13:56:52 +02002300 release_sock(&smc->sk);
Ursula Braun9b67e262018-05-02 16:56:46 +02002301
2302 return put_user(answ, (int __user *)arg);
Ursula Braunac713872017-01-09 16:55:13 +01002303}
2304
2305static ssize_t smc_sendpage(struct socket *sock, struct page *page,
2306 int offset, size_t size, int flags)
2307{
2308 struct sock *sk = sock->sk;
2309 struct smc_sock *smc;
2310 int rc = -EPIPE;
2311
2312 smc = smc_sk(sk);
2313 lock_sock(sk);
Stefan Rasplbda27ff2018-05-03 17:57:39 +02002314 if (sk->sk_state != SMC_ACTIVE) {
2315 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01002316 goto out;
Stefan Rasplbda27ff2018-05-03 17:57:39 +02002317 }
2318 release_sock(sk);
Ursula Braunac713872017-01-09 16:55:13 +01002319 if (smc->use_fallback)
2320 rc = kernel_sendpage(smc->clcsock, page, offset,
2321 size, flags);
2322 else
2323 rc = sock_no_sendpage(sock, page, offset, size, flags);
2324
2325out:
Ursula Braunac713872017-01-09 16:55:13 +01002326 return rc;
2327}
2328
Stefan Raspl9014db22018-05-03 18:12:39 +02002329/* Map the affected portions of the rmbe into an spd, note the number of bytes
2330 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor
2331 * updates till whenever a respective page has been fully processed.
2332 * Note that subsequent recv() calls have to wait till all splice() processing
2333 * completed.
2334 */
Ursula Braunac713872017-01-09 16:55:13 +01002335static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos,
2336 struct pipe_inode_info *pipe, size_t len,
Stefan Raspl9014db22018-05-03 18:12:39 +02002337 unsigned int flags)
Ursula Braunac713872017-01-09 16:55:13 +01002338{
2339 struct sock *sk = sock->sk;
2340 struct smc_sock *smc;
2341 int rc = -ENOTCONN;
2342
2343 smc = smc_sk(sk);
2344 lock_sock(sk);
Karsten Graul51c5aba2019-01-30 18:51:04 +01002345 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) {
2346 /* socket was connected before, no more data to read */
2347 rc = 0;
2348 goto out;
2349 }
Stefan Raspl9014db22018-05-03 18:12:39 +02002350 if (sk->sk_state == SMC_INIT ||
2351 sk->sk_state == SMC_LISTEN ||
2352 sk->sk_state == SMC_CLOSED)
Ursula Braunac713872017-01-09 16:55:13 +01002353 goto out;
Stefan Raspl9014db22018-05-03 18:12:39 +02002354
2355 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) {
2356 rc = 0;
2357 goto out;
2358 }
2359
Ursula Braunac713872017-01-09 16:55:13 +01002360 if (smc->use_fallback) {
2361 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos,
2362 pipe, len, flags);
2363 } else {
Stefan Raspl9014db22018-05-03 18:12:39 +02002364 if (*ppos) {
2365 rc = -ESPIPE;
2366 goto out;
2367 }
2368 if (flags & SPLICE_F_NONBLOCK)
2369 flags = MSG_DONTWAIT;
2370 else
2371 flags = 0;
2372 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags);
Ursula Braunac713872017-01-09 16:55:13 +01002373 }
2374out:
2375 release_sock(sk);
Stefan Raspl9014db22018-05-03 18:12:39 +02002376
Ursula Braunac713872017-01-09 16:55:13 +01002377 return rc;
2378}
2379
2380/* must look like tcp */
2381static const struct proto_ops smc_sock_ops = {
2382 .family = PF_SMC,
2383 .owner = THIS_MODULE,
2384 .release = smc_release,
2385 .bind = smc_bind,
2386 .connect = smc_connect,
2387 .socketpair = sock_no_socketpair,
2388 .accept = smc_accept,
2389 .getname = smc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07002390 .poll = smc_poll,
Ursula Braunac713872017-01-09 16:55:13 +01002391 .ioctl = smc_ioctl,
2392 .listen = smc_listen,
2393 .shutdown = smc_shutdown,
2394 .setsockopt = smc_setsockopt,
2395 .getsockopt = smc_getsockopt,
2396 .sendmsg = smc_sendmsg,
2397 .recvmsg = smc_recvmsg,
2398 .mmap = sock_no_mmap,
2399 .sendpage = smc_sendpage,
2400 .splice_read = smc_splice_read,
2401};
2402
2403static int smc_create(struct net *net, struct socket *sock, int protocol,
2404 int kern)
2405{
Karsten Graulaaa4d332018-03-16 15:06:41 +01002406 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET;
Ursula Braunac713872017-01-09 16:55:13 +01002407 struct smc_sock *smc;
2408 struct sock *sk;
2409 int rc;
2410
2411 rc = -ESOCKTNOSUPPORT;
2412 if (sock->type != SOCK_STREAM)
2413 goto out;
2414
2415 rc = -EPROTONOSUPPORT;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002416 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6)
Ursula Braunac713872017-01-09 16:55:13 +01002417 goto out;
2418
2419 rc = -ENOBUFS;
2420 sock->ops = &smc_sock_ops;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002421 sk = smc_sock_alloc(net, sock, protocol);
Ursula Braunac713872017-01-09 16:55:13 +01002422 if (!sk)
2423 goto out;
2424
2425 /* create internal TCP socket for CLC handshake and fallback */
2426 smc = smc_sk(sk);
Ursula Brauna046d572017-01-09 16:55:16 +01002427 smc->use_fallback = false; /* assume rdma capability first */
Karsten Graul603cc142018-07-25 16:35:32 +02002428 smc->fallback_rsn = 0;
Karsten Graulaaa4d332018-03-16 15:06:41 +01002429 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP,
2430 &smc->clcsock);
Davide Carattia5dcb732018-02-28 12:44:09 +01002431 if (rc) {
Ursula Braunac713872017-01-09 16:55:13 +01002432 sk_common_release(sk);
Davide Carattia5dcb732018-02-28 12:44:09 +01002433 goto out;
2434 }
Ursula Brauncd6851f2017-01-09 16:55:18 +01002435 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE);
2436 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE);
Ursula Braunac713872017-01-09 16:55:13 +01002437
2438out:
2439 return rc;
2440}
2441
2442static const struct net_proto_family smc_sock_family_ops = {
2443 .family = PF_SMC,
2444 .owner = THIS_MODULE,
2445 .create = smc_create,
2446};
2447
Hans Wippel64e28b52019-02-21 13:01:02 +01002448unsigned int smc_net_id;
2449
2450static __net_init int smc_net_init(struct net *net)
2451{
2452 return smc_pnet_net_init(net);
2453}
2454
2455static void __net_exit smc_net_exit(struct net *net)
2456{
2457 smc_pnet_net_exit(net);
2458}
2459
2460static struct pernet_operations smc_net_ops = {
2461 .init = smc_net_init,
2462 .exit = smc_net_exit,
2463 .id = &smc_net_id,
2464 .size = sizeof(struct smc_net),
2465};
2466
Ursula Braunac713872017-01-09 16:55:13 +01002467static int __init smc_init(void)
2468{
2469 int rc;
2470
Hans Wippel64e28b52019-02-21 13:01:02 +01002471 rc = register_pernet_subsys(&smc_net_ops);
2472 if (rc)
2473 return rc;
2474
Ursula Braun201091e2020-09-26 12:44:24 +02002475 smc_ism_init();
Ursula Braunb81a5eb2020-09-26 12:44:31 +02002476 smc_clc_init();
Ursula Braun201091e2020-09-26 12:44:24 +02002477
Thomas Richter6812baa2017-01-09 16:55:15 +01002478 rc = smc_pnet_init();
2479 if (rc)
YueHaibing8c33bf12019-06-26 17:47:50 +02002480 goto out_pernet_subsys;
Thomas Richter6812baa2017-01-09 16:55:15 +01002481
Karsten Graul22ef4732020-09-10 18:48:29 +02002482 rc = -ENOMEM;
2483 smc_hs_wq = alloc_workqueue("smc_hs_wq", 0, 0);
2484 if (!smc_hs_wq)
2485 goto out_pnet;
2486
2487 smc_close_wq = alloc_workqueue("smc_close_wq", 0, 0);
2488 if (!smc_close_wq)
2489 goto out_alloc_hs_wq;
2490
Ursula Braun6dabd402019-11-16 17:47:29 +01002491 rc = smc_core_init();
2492 if (rc) {
2493 pr_err("%s: smc_core_init fails with %d\n", __func__, rc);
Karsten Graul22ef4732020-09-10 18:48:29 +02002494 goto out_alloc_wqs;
Ursula Braun6dabd402019-11-16 17:47:29 +01002495 }
2496
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002497 rc = smc_llc_init();
2498 if (rc) {
2499 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002500 goto out_core;
Ursula Braun9bf9abe2017-01-09 16:55:21 +01002501 }
2502
Ursula Braun5f083182017-01-09 16:55:22 +01002503 rc = smc_cdc_init();
2504 if (rc) {
2505 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002506 goto out_core;
Ursula Braun5f083182017-01-09 16:55:22 +01002507 }
2508
Ursula Braunac713872017-01-09 16:55:13 +01002509 rc = proto_register(&smc_proto, 1);
2510 if (rc) {
Karsten Graulaaa4d332018-03-16 15:06:41 +01002511 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc);
Ursula Braun6dabd402019-11-16 17:47:29 +01002512 goto out_core;
Ursula Braunac713872017-01-09 16:55:13 +01002513 }
2514
Karsten Graulaaa4d332018-03-16 15:06:41 +01002515 rc = proto_register(&smc_proto6, 1);
2516 if (rc) {
2517 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc);
2518 goto out_proto;
2519 }
2520
Ursula Braunac713872017-01-09 16:55:13 +01002521 rc = sock_register(&smc_sock_family_ops);
2522 if (rc) {
2523 pr_err("%s: sock_register fails with %d\n", __func__, rc);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002524 goto out_proto6;
Ursula Braunac713872017-01-09 16:55:13 +01002525 }
Ursula Braunf16a7dd2017-01-09 16:55:26 +01002526 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002527 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht);
Ursula Braunac713872017-01-09 16:55:13 +01002528
Ursula Brauna4cf0442017-01-09 16:55:14 +01002529 rc = smc_ib_register_client();
2530 if (rc) {
2531 pr_err("%s: ib_register fails with %d\n", __func__, rc);
2532 goto out_sock;
2533 }
2534
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002535 static_branch_enable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002536 return 0;
2537
Ursula Brauna4cf0442017-01-09 16:55:14 +01002538out_sock:
2539 sock_unregister(PF_SMC);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002540out_proto6:
2541 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002542out_proto:
2543 proto_unregister(&smc_proto);
Ursula Braun6dabd402019-11-16 17:47:29 +01002544out_core:
2545 smc_core_exit();
Karsten Graul22ef4732020-09-10 18:48:29 +02002546out_alloc_wqs:
2547 destroy_workqueue(smc_close_wq);
2548out_alloc_hs_wq:
2549 destroy_workqueue(smc_hs_wq);
Thomas Richter6812baa2017-01-09 16:55:15 +01002550out_pnet:
2551 smc_pnet_exit();
YueHaibing8c33bf12019-06-26 17:47:50 +02002552out_pernet_subsys:
2553 unregister_pernet_subsys(&smc_net_ops);
2554
Ursula Braunac713872017-01-09 16:55:13 +01002555 return rc;
2556}
2557
2558static void __exit smc_exit(void)
2559{
Ursula Braunc5c1cc92017-10-25 11:01:46 +02002560 static_branch_disable(&tcp_have_smc);
Ursula Braunac713872017-01-09 16:55:13 +01002561 sock_unregister(PF_SMC);
Ursula Braun6dabd402019-11-16 17:47:29 +01002562 smc_core_exit();
2563 smc_ib_unregister_client();
Karsten Graul22ef4732020-09-10 18:48:29 +02002564 destroy_workqueue(smc_close_wq);
2565 destroy_workqueue(smc_hs_wq);
Karsten Graulaaa4d332018-03-16 15:06:41 +01002566 proto_unregister(&smc_proto6);
Ursula Braunac713872017-01-09 16:55:13 +01002567 proto_unregister(&smc_proto);
Thomas Richter6812baa2017-01-09 16:55:15 +01002568 smc_pnet_exit();
Hans Wippel64e28b52019-02-21 13:01:02 +01002569 unregister_pernet_subsys(&smc_net_ops);
Ursula Braun4ead9c92019-11-16 17:47:31 +01002570 rcu_barrier();
Ursula Braunac713872017-01-09 16:55:13 +01002571}
2572
2573module_init(smc_init);
2574module_exit(smc_exit);
2575
2576MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>");
2577MODULE_DESCRIPTION("smc socket address family");
2578MODULE_LICENSE("GPL");
2579MODULE_ALIAS_NETPROTO(PF_SMC);