| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 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 |
* |
| 10 |
* Initial restrictions: |
| 11 |
* - support for alternate links postponed |
| 12 |
* |
| 13 |
* Copyright IBM Corp. 2016, 2018 |
| 14 |
* |
| 15 |
* Author(s): Ursula Braun <[email protected]> |
| 16 |
* based on prototype from Frank Blaschka |
| 17 |
*/ |
| 18 |
|
| 19 |
#define pr_fmt(fmt) "smc: " fmt |
| 20 |
|
| 21 |
#include <linux/module.h> |
| 22 |
#include <linux/socket.h> |
| 23 |
#include <linux/workqueue.h> |
| 24 |
#include <linux/in.h> |
| 25 |
#include <linux/sched/signal.h> |
| 26 |
#include <linux/if_vlan.h> |
| 27 |
#include <linux/rcupdate_wait.h> |
| 28 |
#include <linux/ctype.h> |
| 29 |
#include <linux/splice.h> |
| 30 |
|
| 31 |
#include <net/sock.h> |
| 32 |
#include <net/inet_common.h> |
| 33 |
#if IS_ENABLED(CONFIG_IPV6) |
| 34 |
#include <net/ipv6.h> |
| 35 |
#endif |
| 36 |
#include <net/tcp.h> |
| 37 |
#include <net/smc.h> |
| 38 |
#include <asm/ioctls.h> |
| 39 |
|
| 40 |
#include <net/net_namespace.h> |
| 41 |
#include <net/netns/generic.h> |
| 42 |
#include "smc_netns.h" |
| 43 |
|
| 44 |
#include "smc.h" |
| 45 |
#include "smc_clc.h" |
| 46 |
#include "smc_llc.h" |
| 47 |
#include "smc_cdc.h" |
| 48 |
#include "smc_core.h" |
| 49 |
#include "smc_ib.h" |
| 50 |
#include "smc_ism.h" |
| 51 |
#include "smc_pnet.h" |
| 52 |
#include "smc_netlink.h" |
| 53 |
#include "smc_tx.h" |
| 54 |
#include "smc_rx.h" |
| 55 |
#include "smc_close.h" |
| 56 |
#include "smc_stats.h" |
| 57 |
#include "smc_tracepoint.h" |
| 58 |
#include "smc_sysctl.h" |
| 59 |
#include "smc_inet.h" |
| 60 |
#include "smc_hs_bpf.h" |
| 61 |
|
| 62 |
static DEFINE_MUTEX(smc_server_lgr_pending); /* serialize link group |
| 63 |
* creation on server |
| 64 |
*/ |
| 65 |
static DEFINE_MUTEX(smc_client_lgr_pending); /* serialize link group |
| 66 |
* creation on client |
| 67 |
*/ |
| 68 |
|
| 69 |
static struct workqueue_struct *smc_tcp_ls_wq; /* wq for tcp listen work */ |
| 70 |
struct workqueue_struct *smc_hs_wq; /* wq for handshake work */ |
| 71 |
struct workqueue_struct *smc_close_wq; /* wq for close work */ |
| 72 |
|
| 73 |
static void smc_tcp_listen_work(struct work_struct *); |
| 74 |
static void smc_connect_work(struct work_struct *); |
| 75 |
|
| 76 |
int smc_nl_dump_hs_limitation(struct sk_buff *skb, struct netlink_callback *cb) |
| 77 |
{ |
| 78 |
struct smc_nl_dmp_ctx *cb_ctx = smc_nl_dmp_ctx(cb); |
| 79 |
void *hdr; |
| 80 |
|
| 81 |
if (cb_ctx->pos[0]) |
| 82 |
goto out; |
| 83 |
|
| 84 |
hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
| 85 |
&smc_gen_nl_family, NLM_F_MULTI, |
| 86 |
SMC_NETLINK_DUMP_HS_LIMITATION); |
| 87 |
if (!hdr) |
| 88 |
return -ENOMEM; |
| 89 |
|
| 90 |
if (nla_put_u8(skb, SMC_NLA_HS_LIMITATION_ENABLED, |
| 91 |
sock_net(skb->sk)->smc.limit_smc_hs)) |
| 92 |
goto err; |
| 93 |
|
| 94 |
genlmsg_end(skb, hdr); |
| 95 |
cb_ctx->pos[0] = 1; |
| 96 |
out: |
| 97 |
return skb->len; |
| 98 |
err: |
| 99 |
genlmsg_cancel(skb, hdr); |
| 100 |
return -EMSGSIZE; |
| 101 |
} |
| 102 |
|
| 103 |
int smc_nl_enable_hs_limitation(struct sk_buff *skb, struct genl_info *info) |
| 104 |
{ |
| 105 |
sock_net(skb->sk)->smc.limit_smc_hs = true; |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
int smc_nl_disable_hs_limitation(struct sk_buff *skb, struct genl_info *info) |
| 110 |
{ |
| 111 |
sock_net(skb->sk)->smc.limit_smc_hs = false; |
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
static void smc_set_keepalive(struct sock *sk, int val) |
| 116 |
{ |
| 117 |
struct smc_sock *smc = smc_sk(sk); |
| 118 |
|
| 119 |
smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val); |
| 120 |
} |
| 121 |
|
| 122 |
static struct sock *smc_tcp_syn_recv_sock(const struct sock *sk, |
| 123 |
struct sk_buff *skb, |
| 124 |
struct request_sock *req, |
| 125 |
struct dst_entry *dst, |
| 126 |
struct request_sock *req_unhash, |
| 127 |
bool *own_req, |
| 128 |
void (*opt_child_init)(struct sock *newsk, |
| 129 |
const struct sock *sk)) |
| 130 |
{ |
| 131 |
struct smc_sock *smc; |
| 132 |
struct sock *child; |
| 133 |
|
| 134 |
rcu_read_lock(); |
| 135 |
smc = smc_clcsock_user_data_rcu(sk); |
| 136 |
if (!smc || !refcount_inc_not_zero(&smc->sk.sk_refcnt)) { |
| 137 |
rcu_read_unlock(); |
| 138 |
smc = NULL; |
| 139 |
goto drop; |
| 140 |
} |
| 141 |
rcu_read_unlock(); |
| 142 |
|
| 143 |
if (READ_ONCE(sk->sk_ack_backlog) + atomic_read(&smc->queued_smc_hs) > |
| 144 |
sk->sk_max_ack_backlog) |
| 145 |
goto drop; |
| 146 |
|
| 147 |
if (sk_acceptq_is_full(&smc->sk)) { |
| 148 |
NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS); |
| 149 |
goto drop; |
| 150 |
} |
| 151 |
|
| 152 |
/* passthrough to original syn recv sock fct */ |
| 153 |
child = smc->ori_af_ops->syn_recv_sock(sk, skb, req, dst, req_unhash, |
| 154 |
own_req, opt_child_init); |
| 155 |
/* child must not inherit smc or its ops */ |
| 156 |
if (child) { |
| 157 |
rcu_assign_sk_user_data(child, NULL); |
| 158 |
|
| 159 |
/* v4-mapped sockets don't inherit parent ops. Don't restore. */ |
| 160 |
if (inet_csk(child)->icsk_af_ops == inet_csk(sk)->icsk_af_ops) |
| 161 |
inet_csk(child)->icsk_af_ops = smc->ori_af_ops; |
| 162 |
} |
| 163 |
sock_put(&smc->sk); |
| 164 |
return child; |
| 165 |
|
| 166 |
drop: |
| 167 |
dst_release(dst); |
| 168 |
tcp_listendrop(sk); |
| 169 |
if (smc) |
| 170 |
sock_put(&smc->sk); |
| 171 |
return NULL; |
| 172 |
} |
| 173 |
|
| 174 |
static bool smc_hs_congested(const struct sock *sk) |
| 175 |
{ |
| 176 |
const struct smc_sock *smc; |
| 177 |
|
| 178 |
smc = smc_clcsock_user_data(sk); |
| 179 |
|
| 180 |
if (!smc) |
| 181 |
return true; |
| 182 |
|
| 183 |
if (workqueue_congested(WORK_CPU_UNBOUND, smc_hs_wq)) |
| 184 |
return true; |
| 185 |
|
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
struct smc_hashinfo smc_v4_hashinfo = { |
| 190 |
.lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock), |
| 191 |
}; |
| 192 |
|
| 193 |
struct smc_hashinfo smc_v6_hashinfo = { |
| 194 |
.lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock), |
| 195 |
}; |
| 196 |
|
| 197 |
int smc_hash_sk(struct sock *sk) |
| 198 |
{ |
| 199 |
struct smc_hashinfo *h = sk->sk_prot->h.smc_hash; |
| 200 |
struct hlist_head *head; |
| 201 |
|
| 202 |
head = &h->ht; |
| 203 |
|
| 204 |
write_lock_bh(&h->lock); |
| 205 |
sk_add_node(sk, head); |
| 206 |
write_unlock_bh(&h->lock); |
| 207 |
sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); |
| 208 |
|
| 209 |
return 0; |
| 210 |
} |
| 211 |
|
| 212 |
void smc_unhash_sk(struct sock *sk) |
| 213 |
{ |
| 214 |
struct smc_hashinfo *h = sk->sk_prot->h.smc_hash; |
| 215 |
|
| 216 |
write_lock_bh(&h->lock); |
| 217 |
if (sk_del_node_init(sk)) |
| 218 |
sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); |
| 219 |
write_unlock_bh(&h->lock); |
| 220 |
} |
| 221 |
|
| 222 |
/* This will be called before user really release sock_lock. So do the |
| 223 |
* work which we didn't do because of user hold the sock_lock in the |
| 224 |
* BH context |
| 225 |
*/ |
| 226 |
void smc_release_cb(struct sock *sk) |
| 227 |
{ |
| 228 |
struct smc_sock *smc = smc_sk(sk); |
| 229 |
|
| 230 |
if (smc->conn.tx_in_release_sock) { |
| 231 |
smc_tx_pending(&smc->conn); |
| 232 |
smc->conn.tx_in_release_sock = false; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
struct proto smc_proto = { |
| 237 |
.name = "SMC", |
| 238 |
.owner = THIS_MODULE, |
| 239 |
.keepalive = smc_set_keepalive, |
| 240 |
.hash = smc_hash_sk, |
| 241 |
.unhash = smc_unhash_sk, |
| 242 |
.release_cb = smc_release_cb, |
| 243 |
.obj_size = sizeof(struct smc_sock), |
| 244 |
.h.smc_hash = &smc_v4_hashinfo, |
| 245 |
.slab_flags = SLAB_TYPESAFE_BY_RCU, |
| 246 |
}; |
| 247 |
EXPORT_SYMBOL_GPL(smc_proto); |
| 248 |
|
| 249 |
struct proto smc_proto6 = { |
| 250 |
.name = "SMC6", |
| 251 |
.owner = THIS_MODULE, |
| 252 |
.keepalive = smc_set_keepalive, |
| 253 |
.hash = smc_hash_sk, |
| 254 |
.unhash = smc_unhash_sk, |
| 255 |
.release_cb = smc_release_cb, |
| 256 |
.obj_size = sizeof(struct smc_sock), |
| 257 |
.h.smc_hash = &smc_v6_hashinfo, |
| 258 |
.slab_flags = SLAB_TYPESAFE_BY_RCU, |
| 259 |
}; |
| 260 |
EXPORT_SYMBOL_GPL(smc_proto6); |
| 261 |
|
| 262 |
static void smc_fback_restore_callbacks(struct smc_sock *smc) |
| 263 |
{ |
| 264 |
struct sock *clcsk = smc->clcsock->sk; |
| 265 |
|
| 266 |
write_lock_bh(&clcsk->sk_callback_lock); |
| 267 |
rcu_assign_sk_user_data(clcsk, NULL); |
| 268 |
|
| 269 |
smc_clcsock_restore_cb(&clcsk->sk_state_change, &smc->clcsk_state_change); |
| 270 |
smc_clcsock_restore_cb(&clcsk->sk_data_ready, &smc->clcsk_data_ready); |
| 271 |
smc_clcsock_restore_cb(&clcsk->sk_write_space, &smc->clcsk_write_space); |
| 272 |
smc_clcsock_restore_cb(&clcsk->sk_error_report, &smc->clcsk_error_report); |
| 273 |
|
| 274 |
write_unlock_bh(&clcsk->sk_callback_lock); |
| 275 |
} |
| 276 |
|
| 277 |
static void smc_restore_fallback_changes(struct smc_sock *smc) |
| 278 |
{ |
| 279 |
if (smc->clcsock->file) { /* non-accepted sockets have no file yet */ |
| 280 |
smc->clcsock->file->private_data = smc->sk.sk_socket; |
| 281 |
smc->clcsock->file = NULL; |
| 282 |
smc_fback_restore_callbacks(smc); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
static int __smc_release(struct smc_sock *smc) |
| 287 |
{ |
| 288 |
struct sock *sk = &smc->sk; |
| 289 |
int rc = 0; |
| 290 |
|
| 291 |
if (!smc->use_fallback) { |
| 292 |
rc = smc_close_active(smc); |
| 293 |
smc_sock_set_flag(sk, SOCK_DEAD); |
| 294 |
sk->sk_shutdown |= SHUTDOWN_MASK; |
| 295 |
} else { |
| 296 |
if (sk->sk_state != SMC_CLOSED) { |
| 297 |
if (sk->sk_state != SMC_LISTEN && |
| 298 |
sk->sk_state != SMC_INIT) |
| 299 |
sock_put(sk); /* passive closing */ |
| 300 |
if (sk->sk_state == SMC_LISTEN) { |
| 301 |
/* wake up clcsock accept */ |
| 302 |
rc = kernel_sock_shutdown(smc->clcsock, |
| 303 |
SHUT_RDWR); |
| 304 |
} |
| 305 |
sk->sk_state = SMC_CLOSED; |
| 306 |
sk->sk_state_change(sk); |
| 307 |
} |
| 308 |
smc_restore_fallback_changes(smc); |
| 309 |
} |
| 310 |
|
| 311 |
sk->sk_prot->unhash(sk); |
| 312 |
|
| 313 |
if (sk->sk_state == SMC_CLOSED) { |
| 314 |
if (smc->clcsock) { |
| 315 |
release_sock(sk); |
| 316 |
smc_clcsock_release(smc); |
| 317 |
lock_sock(sk); |
| 318 |
} |
| 319 |
if (!smc->use_fallback) |
| 320 |
smc_conn_free(&smc->conn); |
| 321 |
} |
| 322 |
|
| 323 |
return rc; |
| 324 |
} |
| 325 |
|
| 326 |
int smc_release(struct socket *sock) |
| 327 |
{ |
| 328 |
struct sock *sk = sock->sk; |
| 329 |
struct smc_sock *smc; |
| 330 |
int old_state, rc = 0; |
| 331 |
|
| 332 |
if (!sk) |
| 333 |
goto out; |
| 334 |
|
| 335 |
sock_hold(sk); /* sock_put below */ |
| 336 |
smc = smc_sk(sk); |
| 337 |
|
| 338 |
old_state = sk->sk_state; |
| 339 |
|
| 340 |
/* cleanup for a dangling non-blocking connect */ |
| 341 |
if (smc->connect_nonblock && old_state == SMC_INIT) |
| 342 |
tcp_abort(smc->clcsock->sk, ECONNABORTED); |
| 343 |
|
| 344 |
if (cancel_work_sync(&smc->connect_work)) |
| 345 |
sock_put(&smc->sk); /* sock_hold in smc_connect for passive closing */ |
| 346 |
|
| 347 |
if (sk->sk_state == SMC_LISTEN) |
| 348 |
/* smc_close_non_accepted() is called and acquires |
| 349 |
* sock lock for child sockets again |
| 350 |
*/ |
| 351 |
lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
| 352 |
else |
| 353 |
lock_sock(sk); |
| 354 |
|
| 355 |
if (old_state == SMC_INIT && sk->sk_state == SMC_ACTIVE && |
| 356 |
!smc->use_fallback) |
| 357 |
smc_close_active_abort(smc); |
| 358 |
|
| 359 |
rc = __smc_release(smc); |
| 360 |
|
| 361 |
/* detach socket */ |
| 362 |
sock_orphan(sk); |
| 363 |
sock->sk = NULL; |
| 364 |
release_sock(sk); |
| 365 |
|
| 366 |
sock_put(sk); /* sock_hold above */ |
| 367 |
sock_put(sk); /* final sock_put */ |
| 368 |
out: |
| 369 |
return rc; |
| 370 |
} |
| 371 |
|
| 372 |
static void smc_destruct(struct sock *sk) |
| 373 |
{ |
| 374 |
if (sk->sk_state != SMC_CLOSED) |
| 375 |
return; |
| 376 |
if (!sock_flag(sk, SOCK_DEAD)) |
| 377 |
return; |
| 378 |
switch (sk->sk_family) { |
| 379 |
case AF_INET: |
| 380 |
inet_sock_destruct(sk); |
| 381 |
break; |
| 382 |
#if IS_ENABLED(CONFIG_IPV6) |
| 383 |
case AF_INET6: |
| 384 |
inet6_sock_destruct(sk); |
| 385 |
break; |
| 386 |
#endif |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
static struct lock_class_key smc_key; |
| 391 |
static struct lock_class_key smc_slock_key; |
| 392 |
|
| 393 |
void smc_sk_init(struct net *net, struct sock *sk, int protocol) |
| 394 |
{ |
| 395 |
struct smc_sock *smc = smc_sk(sk); |
| 396 |
|
| 397 |
sk->sk_state = SMC_INIT; |
| 398 |
sk->sk_destruct = smc_destruct; |
| 399 |
sk->sk_protocol = protocol; |
| 400 |
WRITE_ONCE(sk->sk_sndbuf, 2 * READ_ONCE(net->smc.sysctl_wmem)); |
| 401 |
WRITE_ONCE(sk->sk_rcvbuf, 2 * READ_ONCE(net->smc.sysctl_rmem)); |
| 402 |
INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work); |
| 403 |
INIT_WORK(&smc->connect_work, smc_connect_work); |
| 404 |
INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work); |
| 405 |
INIT_LIST_HEAD(&smc->accept_q); |
| 406 |
sock_lock_init_class_and_name(sk, "slock-AF_SMC", &smc_slock_key, |
| 407 |
"sk_lock-AF_SMC", &smc_key); |
| 408 |
spin_lock_init(&smc->accept_q_lock); |
| 409 |
spin_lock_init(&smc->conn.send_lock); |
| 410 |
sk->sk_prot->hash(sk); |
| 411 |
mutex_init(&smc->clcsock_release_lock); |
| 412 |
smc_init_saved_callbacks(smc); |
| 413 |
smc->limit_smc_hs = net->smc.limit_smc_hs; |
| 414 |
smc->use_fallback = false; /* assume rdma capability first */ |
| 415 |
smc->fallback_rsn = 0; |
| 416 |
smc_close_init(smc); |
| 417 |
} |
| 418 |
|
| 419 |
static struct sock *smc_sock_alloc(struct net *net, struct socket *sock, |
| 420 |
int protocol) |
| 421 |
{ |
| 422 |
struct proto *prot; |
| 423 |
struct sock *sk; |
| 424 |
|
| 425 |
prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto; |
| 426 |
sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0); |
| 427 |
if (!sk) |
| 428 |
return NULL; |
| 429 |
|
| 430 |
sock_init_data(sock, sk); /* sets sk_refcnt to 1 */ |
| 431 |
smc_sk_init(net, sk, protocol); |
| 432 |
|
| 433 |
return sk; |
| 434 |
} |
| 435 |
|
| 436 |
int smc_bind(struct socket *sock, struct sockaddr_unsized *uaddr, |
| 437 |
int addr_len) |
| 438 |
{ |
| 439 |
struct sockaddr_in *addr = (struct sockaddr_in *)uaddr; |
| 440 |
struct sock *sk = sock->sk; |
| 441 |
struct smc_sock *smc; |
| 442 |
int rc; |
| 443 |
|
| 444 |
smc = smc_sk(sk); |
| 445 |
|
| 446 |
/* replicate tests from inet_bind(), to be safe wrt. future changes */ |
| 447 |
rc = -EINVAL; |
| 448 |
if (addr_len < sizeof(struct sockaddr_in)) |
| 449 |
goto out; |
| 450 |
|
| 451 |
rc = -EAFNOSUPPORT; |
| 452 |
if (addr->sin_family != AF_INET && |
| 453 |
addr->sin_family != AF_INET6 && |
| 454 |
addr->sin_family != AF_UNSPEC) |
| 455 |
goto out; |
| 456 |
/* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */ |
| 457 |
if (addr->sin_family == AF_UNSPEC && |
| 458 |
addr->sin_addr.s_addr != htonl(INADDR_ANY)) |
| 459 |
goto out; |
| 460 |
|
| 461 |
lock_sock(sk); |
| 462 |
|
| 463 |
/* Check if socket is already active */ |
| 464 |
rc = -EINVAL; |
| 465 |
if (sk->sk_state != SMC_INIT || smc->connect_nonblock) |
| 466 |
goto out_rel; |
| 467 |
|
| 468 |
smc->clcsock->sk->sk_reuse = sk->sk_reuse; |
| 469 |
smc->clcsock->sk->sk_reuseport = sk->sk_reuseport; |
| 470 |
rc = kernel_bind(smc->clcsock, uaddr, addr_len); |
| 471 |
|
| 472 |
out_rel: |
| 473 |
release_sock(sk); |
| 474 |
out: |
| 475 |
return rc; |
| 476 |
} |
| 477 |
|
| 478 |
/* copy only relevant settings and flags of SOL_SOCKET level from smc to |
| 479 |
* clc socket (since smc is not called for these options from net/core) |
| 480 |
*/ |
| 481 |
|
| 482 |
#define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \ |
| 483 |
(1UL << SOCK_KEEPOPEN) | \ |
| 484 |
(1UL << SOCK_LINGER) | \ |
| 485 |
(1UL << SOCK_BROADCAST) | \ |
| 486 |
(1UL << SOCK_TIMESTAMP) | \ |
| 487 |
(1UL << SOCK_DBG) | \ |
| 488 |
(1UL << SOCK_RCVTSTAMP) | \ |
| 489 |
(1UL << SOCK_RCVTSTAMPNS) | \ |
| 490 |
(1UL << SOCK_LOCALROUTE) | \ |
| 491 |
(1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \ |
| 492 |
(1UL << SOCK_RXQ_OVFL) | \ |
| 493 |
(1UL << SOCK_WIFI_STATUS) | \ |
| 494 |
(1UL << SOCK_NOFCS) | \ |
| 495 |
(1UL << SOCK_FILTER_LOCKED) | \ |
| 496 |
(1UL << SOCK_TSTAMP_NEW)) |
| 497 |
|
| 498 |
/* if set, use value set by setsockopt() - else use IPv4 or SMC sysctl value */ |
| 499 |
static void smc_adjust_sock_bufsizes(struct sock *nsk, struct sock *osk, |
| 500 |
unsigned long mask) |
| 501 |
{ |
| 502 |
nsk->sk_userlocks = osk->sk_userlocks; |
| 503 |
if (osk->sk_userlocks & SOCK_SNDBUF_LOCK) |
| 504 |
nsk->sk_sndbuf = osk->sk_sndbuf; |
| 505 |
if (osk->sk_userlocks & SOCK_RCVBUF_LOCK) |
| 506 |
nsk->sk_rcvbuf = osk->sk_rcvbuf; |
| 507 |
} |
| 508 |
|
| 509 |
static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk, |
| 510 |
unsigned long mask) |
| 511 |
{ |
| 512 |
/* options we don't get control via setsockopt for */ |
| 513 |
nsk->sk_type = osk->sk_type; |
| 514 |
nsk->sk_sndtimeo = READ_ONCE(osk->sk_sndtimeo); |
| 515 |
nsk->sk_rcvtimeo = READ_ONCE(osk->sk_rcvtimeo); |
| 516 |
nsk->sk_mark = READ_ONCE(osk->sk_mark); |
| 517 |
nsk->sk_priority = READ_ONCE(osk->sk_priority); |
| 518 |
nsk->sk_rcvlowat = osk->sk_rcvlowat; |
| 519 |
nsk->sk_bound_dev_if = osk->sk_bound_dev_if; |
| 520 |
nsk->sk_err = osk->sk_err; |
| 521 |
|
| 522 |
nsk->sk_flags &= ~mask; |
| 523 |
nsk->sk_flags |= osk->sk_flags & mask; |
| 524 |
|
| 525 |
smc_adjust_sock_bufsizes(nsk, osk, mask); |
| 526 |
} |
| 527 |
|
| 528 |
static void smc_copy_sock_settings_to_clc(struct smc_sock *smc) |
| 529 |
{ |
| 530 |
smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC); |
| 531 |
} |
| 532 |
|
| 533 |
#define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \ |
| 534 |
(1UL << SOCK_KEEPOPEN) | \ |
| 535 |
(1UL << SOCK_LINGER) | \ |
| 536 |
(1UL << SOCK_DBG)) |
| 537 |
/* copy only settings and flags relevant for smc from clc to smc socket */ |
| 538 |
static void smc_copy_sock_settings_to_smc(struct smc_sock *smc) |
| 539 |
{ |
| 540 |
smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC); |
| 541 |
} |
| 542 |
|
| 543 |
/* register the new vzalloced sndbuf on all links */ |
| 544 |
static int smcr_lgr_reg_sndbufs(struct smc_link *link, |
| 545 |
struct smc_buf_desc *snd_desc) |
| 546 |
{ |
| 547 |
struct smc_link_group *lgr = link->lgr; |
| 548 |
int i, rc = 0; |
| 549 |
|
| 550 |
if (!snd_desc->is_vm) |
| 551 |
return -EINVAL; |
| 552 |
|
| 553 |
/* protect against parallel smcr_link_reg_buf() */ |
| 554 |
down_write(&lgr->llc_conf_mutex); |
| 555 |
for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) { |
| 556 |
if (!smc_link_active(&lgr->lnk[i])) |
| 557 |
continue; |
| 558 |
rc = smcr_link_reg_buf(&lgr->lnk[i], snd_desc); |
| 559 |
if (rc) |
| 560 |
break; |
| 561 |
} |
| 562 |
up_write(&lgr->llc_conf_mutex); |
| 563 |
return rc; |
| 564 |
} |
| 565 |
|
| 566 |
/* register the new rmb on all links */ |
| 567 |
static int smcr_lgr_reg_rmbs(struct smc_link *link, |
| 568 |
struct smc_buf_desc *rmb_desc) |
| 569 |
{ |
| 570 |
struct smc_link_group *lgr = link->lgr; |
| 571 |
bool do_slow = false; |
| 572 |
int i, rc = 0; |
| 573 |
|
| 574 |
rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY); |
| 575 |
if (rc) |
| 576 |
return rc; |
| 577 |
|
| 578 |
down_read(&lgr->llc_conf_mutex); |
| 579 |
for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) { |
| 580 |
if (!smc_link_active(&lgr->lnk[i])) |
| 581 |
continue; |
| 582 |
if (!rmb_desc->is_reg_mr[link->link_idx]) { |
| 583 |
up_read(&lgr->llc_conf_mutex); |
| 584 |
goto slow_path; |
| 585 |
} |
| 586 |
} |
| 587 |
/* mr register already */ |
| 588 |
goto fast_path; |
| 589 |
slow_path: |
| 590 |
do_slow = true; |
| 591 |
/* protect against parallel smc_llc_cli_rkey_exchange() and |
| 592 |
* parallel smcr_link_reg_buf() |
| 593 |
*/ |
| 594 |
down_write(&lgr->llc_conf_mutex); |
| 595 |
for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) { |
| 596 |
if (!smc_link_active(&lgr->lnk[i])) |
| 597 |
continue; |
| 598 |
rc = smcr_link_reg_buf(&lgr->lnk[i], rmb_desc); |
| 599 |
if (rc) |
| 600 |
goto out; |
| 601 |
} |
| 602 |
fast_path: |
| 603 |
/* exchange confirm_rkey msg with peer */ |
| 604 |
rc = smc_llc_do_confirm_rkey(link, rmb_desc); |
| 605 |
if (rc) { |
| 606 |
rc = -EFAULT; |
| 607 |
goto out; |
| 608 |
} |
| 609 |
rmb_desc->is_conf_rkey = true; |
| 610 |
out: |
| 611 |
do_slow ? up_write(&lgr->llc_conf_mutex) : up_read(&lgr->llc_conf_mutex); |
| 612 |
smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl); |
| 613 |
return rc; |
| 614 |
} |
| 615 |
|
| 616 |
static int smcr_clnt_conf_first_link(struct smc_sock *smc) |
| 617 |
{ |
| 618 |
struct smc_link *link = smc->conn.lnk; |
| 619 |
struct smc_llc_qentry *qentry; |
| 620 |
int rc; |
| 621 |
|
| 622 |
/* Receive CONFIRM LINK request from server over RoCE fabric. |
| 623 |
* Increasing the client's timeout by twice as much as the server's |
| 624 |
* timeout by default can temporarily avoid decline messages of |
| 625 |
* both sides crossing or colliding |
| 626 |
*/ |
| 627 |
qentry = smc_llc_wait(link->lgr, NULL, 2 * SMC_LLC_WAIT_TIME, |
| 628 |
SMC_LLC_CONFIRM_LINK); |
| 629 |
if (!qentry) { |
| 630 |
struct smc_clc_msg_decline dclc; |
| 631 |
|
| 632 |
rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc), |
| 633 |
SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT); |
| 634 |
return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc; |
| 635 |
} |
| 636 |
smc_llc_save_peer_uid(qentry); |
| 637 |
rc = smc_llc_eval_conf_link(qentry, SMC_LLC_REQ); |
| 638 |
smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl); |
| 639 |
if (rc) |
| 640 |
return SMC_CLC_DECL_RMBE_EC; |
| 641 |
|
| 642 |
rc = smc_ib_modify_qp_rts(link); |
| 643 |
if (rc) |
| 644 |
return SMC_CLC_DECL_ERR_RDYLNK; |
| 645 |
|
| 646 |
smc_wr_remember_qp_attr(link); |
| 647 |
|
| 648 |
/* reg the sndbuf if it was vzalloced */ |
| 649 |
if (smc->conn.sndbuf_desc->is_vm) { |
| 650 |
if (smcr_link_reg_buf(link, smc->conn.sndbuf_desc)) |
| 651 |
return SMC_CLC_DECL_ERR_REGBUF; |
| 652 |
} |
| 653 |
|
| 654 |
/* reg the rmb */ |
| 655 |
if (smcr_link_reg_buf(link, smc->conn.rmb_desc)) |
| 656 |
return SMC_CLC_DECL_ERR_REGBUF; |
| 657 |
|
| 658 |
/* confirm_rkey is implicit on 1st contact */ |
| 659 |
smc->conn.rmb_desc->is_conf_rkey = true; |
| 660 |
|
| 661 |
/* send CONFIRM LINK response over RoCE fabric */ |
| 662 |
rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP); |
| 663 |
if (rc < 0) |
| 664 |
return SMC_CLC_DECL_TIMEOUT_CL; |
| 665 |
|
| 666 |
smc_llc_link_active(link); |
| 667 |
smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE); |
| 668 |
|
| 669 |
if (link->lgr->max_links > 1) { |
| 670 |
/* optional 2nd link, receive ADD LINK request from server */ |
| 671 |
qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME, |
| 672 |
SMC_LLC_ADD_LINK); |
| 673 |
if (!qentry) { |
| 674 |
struct smc_clc_msg_decline dclc; |
| 675 |
|
| 676 |
rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc), |
| 677 |
SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT); |
| 678 |
if (rc == -EAGAIN) |
| 679 |
rc = 0; /* no DECLINE received, go with one link */ |
| 680 |
return rc; |
| 681 |
} |
| 682 |
smc_llc_flow_qentry_clr(&link->lgr->llc_flow_lcl); |
| 683 |
smc_llc_cli_add_link(link, qentry); |
| 684 |
} |
| 685 |
return 0; |
| 686 |
} |
| 687 |
|
| 688 |
static bool smc_isascii(char *hostname) |
| 689 |
{ |
| 690 |
int i; |
| 691 |
|
| 692 |
for (i = 0; i < SMC_MAX_HOSTNAME_LEN; i++) |
| 693 |
if (!isascii(hostname[i])) |
| 694 |
return false; |
| 695 |
return true; |
| 696 |
} |
| 697 |
|
| 698 |
static void smc_conn_save_peer_info_fce(struct smc_sock *smc, |
| 699 |
struct smc_clc_msg_accept_confirm *clc) |
| 700 |
{ |
| 701 |
struct smc_clc_first_contact_ext *fce; |
| 702 |
int clc_v2_len; |
| 703 |
|
| 704 |
if (clc->hdr.version == SMC_V1 || |
| 705 |
!(clc->hdr.typev2 & SMC_FIRST_CONTACT_MASK)) |
| 706 |
return; |
| 707 |
|
| 708 |
if (smc->conn.lgr->is_smcd) { |
| 709 |
memcpy(smc->conn.lgr->negotiated_eid, clc->d1.eid, |
| 710 |
SMC_MAX_EID_LEN); |
| 711 |
clc_v2_len = offsetofend(struct smc_clc_msg_accept_confirm, d1); |
| 712 |
} else { |
| 713 |
memcpy(smc->conn.lgr->negotiated_eid, clc->r1.eid, |
| 714 |
SMC_MAX_EID_LEN); |
| 715 |
clc_v2_len = offsetofend(struct smc_clc_msg_accept_confirm, r1); |
| 716 |
} |
| 717 |
fce = (struct smc_clc_first_contact_ext *)(((u8 *)clc) + clc_v2_len); |
| 718 |
smc->conn.lgr->peer_os = fce->os_type; |
| 719 |
smc->conn.lgr->peer_smc_release = fce->release; |
| 720 |
if (smc_isascii(fce->hostname)) |
| 721 |
memcpy(smc->conn.lgr->peer_hostname, fce->hostname, |
| 722 |
SMC_MAX_HOSTNAME_LEN); |
| 723 |
} |
| 724 |
|
| 725 |
static void smcr_conn_save_peer_info(struct smc_sock *smc, |
| 726 |
struct smc_clc_msg_accept_confirm *clc) |
| 727 |
{ |
| 728 |
int bufsize = smc_uncompress_bufsize(clc->r0.rmbe_size); |
| 729 |
|
| 730 |
smc->conn.peer_rmbe_idx = clc->r0.rmbe_idx; |
| 731 |
smc->conn.local_tx_ctrl.token = ntohl(clc->r0.rmbe_alert_token); |
| 732 |
smc->conn.peer_rmbe_size = bufsize; |
| 733 |
atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size); |
| 734 |
smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1); |
| 735 |
} |
| 736 |
|
| 737 |
static void smcd_conn_save_peer_info(struct smc_sock *smc, |
| 738 |
struct smc_clc_msg_accept_confirm *clc) |
| 739 |
{ |
| 740 |
int bufsize = smc_uncompress_bufsize(clc->d0.dmbe_size); |
| 741 |
|
| 742 |
smc->conn.peer_rmbe_idx = clc->d0.dmbe_idx; |
| 743 |
smc->conn.peer_token = ntohll(clc->d0.token); |
| 744 |
/* msg header takes up space in the buffer */ |
| 745 |
smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg); |
| 746 |
atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size); |
| 747 |
smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx; |
| 748 |
} |
| 749 |
|
| 750 |
static void smc_conn_save_peer_info(struct smc_sock *smc, |
| 751 |
struct smc_clc_msg_accept_confirm *clc) |
| 752 |
{ |
| 753 |
if (smc->conn.lgr->is_smcd) |
| 754 |
smcd_conn_save_peer_info(smc, clc); |
| 755 |
else |
| 756 |
smcr_conn_save_peer_info(smc, clc); |
| 757 |
smc_conn_save_peer_info_fce(smc, clc); |
| 758 |
} |
| 759 |
|
| 760 |
static void smc_link_save_peer_info(struct smc_link *link, |
| 761 |
struct smc_clc_msg_accept_confirm *clc, |
| 762 |
struct smc_init_info *ini) |
| 763 |
{ |
| 764 |
link->peer_qpn = ntoh24(clc->r0.qpn); |
| 765 |
memcpy(link->peer_gid, ini->peer_gid, SMC_GID_SIZE); |
| 766 |
memcpy(link->peer_mac, ini->peer_mac, sizeof(link->peer_mac)); |
| 767 |
link->peer_psn = ntoh24(clc->r0.psn); |
| 768 |
link->peer_mtu = clc->r0.qp_mtu; |
| 769 |
} |
| 770 |
|
| 771 |
static void smc_stat_inc_fback_rsn_cnt(struct smc_sock *smc, |
| 772 |
struct smc_stats_fback *fback_arr) |
| 773 |
{ |
| 774 |
int cnt; |
| 775 |
|
| 776 |
for (cnt = 0; cnt < SMC_MAX_FBACK_RSN_CNT; cnt++) { |
| 777 |
if (fback_arr[cnt].fback_code == smc->fallback_rsn) { |
| 778 |
fback_arr[cnt].count++; |
| 779 |
break; |
| 780 |
} |
| 781 |
if (!fback_arr[cnt].fback_code) { |
| 782 |
fback_arr[cnt].fback_code = smc->fallback_rsn; |
| 783 |
fback_arr[cnt].count++; |
| 784 |
break; |
| 785 |
} |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
static void smc_stat_fallback(struct smc_sock *smc) |
| 790 |
{ |
| 791 |
struct net *net = sock_net(&smc->sk); |
| 792 |
|
| 793 |
mutex_lock(&net->smc.mutex_fback_rsn); |
| 794 |
if (smc->listen_smc) { |
| 795 |
smc_stat_inc_fback_rsn_cnt(smc, net->smc.fback_rsn->srv); |
| 796 |
net->smc.fback_rsn->srv_fback_cnt++; |
| 797 |
} else { |
| 798 |
smc_stat_inc_fback_rsn_cnt(smc, net->smc.fback_rsn->clnt); |
| 799 |
net->smc.fback_rsn->clnt_fback_cnt++; |
| 800 |
} |
| 801 |
mutex_unlock(&net->smc.mutex_fback_rsn); |
| 802 |
} |
| 803 |
|
| 804 |
/* must be called under rcu read lock */ |
| 805 |
static void smc_fback_wakeup_waitqueue(struct smc_sock *smc, void *key) |
| 806 |
{ |
| 807 |
struct socket_wq *wq; |
| 808 |
__poll_t flags; |
| 809 |
|
| 810 |
wq = rcu_dereference(smc->sk.sk_wq); |
| 811 |
if (!skwq_has_sleeper(wq)) |
| 812 |
return; |
| 813 |
|
| 814 |
/* wake up smc sk->sk_wq */ |
| 815 |
if (!key) { |
| 816 |
/* sk_state_change */ |
| 817 |
wake_up_interruptible_all(&wq->wait); |
| 818 |
} else { |
| 819 |
flags = key_to_poll(key); |
| 820 |
if (flags & (EPOLLIN | EPOLLOUT)) |
| 821 |
/* sk_data_ready or sk_write_space */ |
| 822 |
wake_up_interruptible_sync_poll(&wq->wait, flags); |
| 823 |
else if (flags & EPOLLERR) |
| 824 |
/* sk_error_report */ |
| 825 |
wake_up_interruptible_poll(&wq->wait, flags); |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
static int smc_fback_mark_woken(wait_queue_entry_t *wait, |
| 830 |
unsigned int mode, int sync, void *key) |
| 831 |
{ |
| 832 |
struct smc_mark_woken *mark = |
| 833 |
container_of(wait, struct smc_mark_woken, wait_entry); |
| 834 |
|
| 835 |
mark->woken = true; |
| 836 |
mark->key = key; |
| 837 |
return 0; |
| 838 |
} |
| 839 |
|
| 840 |
static void smc_fback_forward_wakeup(struct smc_sock *smc, struct sock *clcsk, |
| 841 |
void (*clcsock_callback)(struct sock *sk)) |
| 842 |
{ |
| 843 |
struct smc_mark_woken mark = { .woken = false }; |
| 844 |
struct socket_wq *wq; |
| 845 |
|
| 846 |
init_waitqueue_func_entry(&mark.wait_entry, |
| 847 |
smc_fback_mark_woken); |
| 848 |
rcu_read_lock(); |
| 849 |
wq = rcu_dereference(clcsk->sk_wq); |
| 850 |
if (!wq) |
| 851 |
goto out; |
| 852 |
add_wait_queue(sk_sleep(clcsk), &mark.wait_entry); |
| 853 |
clcsock_callback(clcsk); |
| 854 |
remove_wait_queue(sk_sleep(clcsk), &mark.wait_entry); |
| 855 |
|
| 856 |
if (mark.woken) |
| 857 |
smc_fback_wakeup_waitqueue(smc, mark.key); |
| 858 |
out: |
| 859 |
rcu_read_unlock(); |
| 860 |
} |
| 861 |
|
| 862 |
static void smc_fback_state_change(struct sock *clcsk) |
| 863 |
{ |
| 864 |
struct smc_sock *smc; |
| 865 |
|
| 866 |
read_lock_bh(&clcsk->sk_callback_lock); |
| 867 |
smc = smc_clcsock_user_data(clcsk); |
| 868 |
if (smc) |
| 869 |
smc_fback_forward_wakeup(smc, clcsk, |
| 870 |
smc->clcsk_state_change); |
| 871 |
read_unlock_bh(&clcsk->sk_callback_lock); |
| 872 |
} |
| 873 |
|
| 874 |
static void smc_fback_data_ready(struct sock *clcsk) |
| 875 |
{ |
| 876 |
struct smc_sock *smc; |
| 877 |
|
| 878 |
read_lock_bh(&clcsk->sk_callback_lock); |
| 879 |
smc = smc_clcsock_user_data(clcsk); |
| 880 |
if (smc) |
| 881 |
smc_fback_forward_wakeup(smc, clcsk, |
| 882 |
smc->clcsk_data_ready); |
| 883 |
read_unlock_bh(&clcsk->sk_callback_lock); |
| 884 |
} |
| 885 |
|
| 886 |
static void smc_fback_write_space(struct sock *clcsk) |
| 887 |
{ |
| 888 |
struct smc_sock *smc; |
| 889 |
|
| 890 |
read_lock_bh(&clcsk->sk_callback_lock); |
| 891 |
smc = smc_clcsock_user_data(clcsk); |
| 892 |
if (smc) |
| 893 |
smc_fback_forward_wakeup(smc, clcsk, |
| 894 |
smc->clcsk_write_space); |
| 895 |
read_unlock_bh(&clcsk->sk_callback_lock); |
| 896 |
} |
| 897 |
|
| 898 |
static void smc_fback_error_report(struct sock *clcsk) |
| 899 |
{ |
| 900 |
struct smc_sock *smc; |
| 901 |
|
| 902 |
read_lock_bh(&clcsk->sk_callback_lock); |
| 903 |
smc = smc_clcsock_user_data(clcsk); |
| 904 |
if (smc) |
| 905 |
smc_fback_forward_wakeup(smc, clcsk, |
| 906 |
smc->clcsk_error_report); |
| 907 |
read_unlock_bh(&clcsk->sk_callback_lock); |
| 908 |
} |
| 909 |
|
| 910 |
static void smc_fback_replace_callbacks(struct smc_sock *smc) |
| 911 |
{ |
| 912 |
struct sock *clcsk = smc->clcsock->sk; |
| 913 |
|
| 914 |
write_lock_bh(&clcsk->sk_callback_lock); |
| 915 |
__rcu_assign_sk_user_data_with_flags(clcsk, smc, SK_USER_DATA_NOCOPY); |
| 916 |
|
| 917 |
smc_clcsock_replace_cb(&clcsk->sk_state_change, smc_fback_state_change, |
| 918 |
&smc->clcsk_state_change); |
| 919 |
smc_clcsock_replace_cb(&clcsk->sk_data_ready, smc_fback_data_ready, |
| 920 |
&smc->clcsk_data_ready); |
| 921 |
smc_clcsock_replace_cb(&clcsk->sk_write_space, smc_fback_write_space, |
| 922 |
&smc->clcsk_write_space); |
| 923 |
smc_clcsock_replace_cb(&clcsk->sk_error_report, smc_fback_error_report, |
| 924 |
&smc->clcsk_error_report); |
| 925 |
|
| 926 |
write_unlock_bh(&clcsk->sk_callback_lock); |
| 927 |
} |
| 928 |
|
| 929 |
static int smc_switch_to_fallback(struct smc_sock *smc, int reason_code) |
| 930 |
{ |
| 931 |
int rc = 0; |
| 932 |
|
| 933 |
mutex_lock(&smc->clcsock_release_lock); |
| 934 |
if (!smc->clcsock) { |
| 935 |
rc = -EBADF; |
| 936 |
goto out; |
| 937 |
} |
| 938 |
|
| 939 |
smc->use_fallback = true; |
| 940 |
smc->fallback_rsn = reason_code; |
| 941 |
smc_stat_fallback(smc); |
| 942 |
trace_smc_switch_to_fallback(smc, reason_code); |
| 943 |
if (smc->sk.sk_socket && smc->sk.sk_socket->file) { |
| 944 |
smc->clcsock->file = smc->sk.sk_socket->file; |
| 945 |
smc->clcsock->file->private_data = smc->clcsock; |
| 946 |
smc->clcsock->wq.fasync_list = |
| 947 |
smc->sk.sk_socket->wq.fasync_list; |
| 948 |
smc->sk.sk_socket->wq.fasync_list = NULL; |
| 949 |
|
| 950 |
/* There might be some wait entries remaining |
| 951 |
* in smc sk->sk_wq and they should be woken up |
| 952 |
* as clcsock's wait queue is woken up. |
| 953 |
*/ |
| 954 |
smc_fback_replace_callbacks(smc); |
| 955 |
} |
| 956 |
out: |
| 957 |
mutex_unlock(&smc->clcsock_release_lock); |
| 958 |
return rc; |
| 959 |
} |
| 960 |
|
| 961 |
/* fall back during connect */ |
| 962 |
static int smc_connect_fallback(struct smc_sock *smc, int reason_code) |
| 963 |
{ |
| 964 |
struct net *net = sock_net(&smc->sk); |
| 965 |
int rc = 0; |
| 966 |
|
| 967 |
rc = smc_switch_to_fallback(smc, reason_code); |
| 968 |
if (rc) { /* fallback fails */ |
| 969 |
this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt); |
| 970 |
if (smc->sk.sk_state == SMC_INIT) |
| 971 |
sock_put(&smc->sk); /* passive closing */ |
| 972 |
return rc; |
| 973 |
} |
| 974 |
smc_copy_sock_settings_to_clc(smc); |
| 975 |
smc->connect_nonblock = 0; |
| 976 |
if (smc->sk.sk_state == SMC_INIT) |
| 977 |
smc->sk.sk_state = SMC_ACTIVE; |
| 978 |
return 0; |
| 979 |
} |
| 980 |
|
| 981 |
/* decline and fall back during connect */ |
| 982 |
static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code, |
| 983 |
u8 version) |
| 984 |
{ |
| 985 |
struct net *net = sock_net(&smc->sk); |
| 986 |
int rc; |
| 987 |
|
| 988 |
if (reason_code < 0) { /* error, fallback is not possible */ |
| 989 |
this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt); |
| 990 |
if (smc->sk.sk_state == SMC_INIT) |
| 991 |
sock_put(&smc->sk); /* passive closing */ |
| 992 |
return reason_code; |
| 993 |
} |
| 994 |
if (reason_code != SMC_CLC_DECL_PEERDECL) { |
| 995 |
rc = smc_clc_send_decline(smc, reason_code, version); |
| 996 |
if (rc < 0) { |
| 997 |
this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt); |
| 998 |
if (smc->sk.sk_state == SMC_INIT) |
| 999 |
sock_put(&smc->sk); /* passive closing */ |
| 1000 |
return rc; |
| 1001 |
} |
| 1002 |
} |
| 1003 |
return smc_connect_fallback(smc, reason_code); |
| 1004 |
} |
| 1005 |
|
| 1006 |
static void smc_conn_abort(struct smc_sock *smc, int local_first) |
| 1007 |
{ |
| 1008 |
struct smc_connection *conn = &smc->conn; |
| 1009 |
struct smc_link_group *lgr = conn->lgr; |
| 1010 |
bool lgr_valid = false; |
| 1011 |
|
| 1012 |
if (smc_conn_lgr_valid(conn)) |
| 1013 |
lgr_valid = true; |
| 1014 |
|
| 1015 |
smc_conn_free(conn); |
| 1016 |
if (local_first && lgr_valid) |
| 1017 |
smc_lgr_cleanup_early(lgr); |
| 1018 |
} |
| 1019 |
|
| 1020 |
/* check if there is a rdma device available for this connection. */ |
| 1021 |
/* called for connect and listen */ |
| 1022 |
static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini) |
| 1023 |
{ |
| 1024 |
/* PNET table look up: search active ib_device and port |
| 1025 |
* within same PNETID that also contains the ethernet device |
| 1026 |
* used for the internal TCP socket |
| 1027 |
*/ |
| 1028 |
smc_pnet_find_roce_resource(smc->clcsock->sk, ini); |
| 1029 |
if (!ini->check_smcrv2 && !ini->ib_dev) |
| 1030 |
return SMC_CLC_DECL_NOSMCRDEV; |
| 1031 |
if (ini->check_smcrv2 && !ini->smcrv2.ib_dev_v2) |
| 1032 |
return SMC_CLC_DECL_NOSMCRDEV; |
| 1033 |
return 0; |
| 1034 |
} |
| 1035 |
|
| 1036 |
/* check if there is an ISM device available for this connection. */ |
| 1037 |
/* called for connect and listen */ |
| 1038 |
static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini) |
| 1039 |
{ |
| 1040 |
/* Find ISM device with same PNETID as connecting interface */ |
| 1041 |
smc_pnet_find_ism_resource(smc->clcsock->sk, ini); |
| 1042 |
if (!ini->ism_dev[0]) |
| 1043 |
return SMC_CLC_DECL_NOSMCDDEV; |
| 1044 |
else |
| 1045 |
ini->ism_chid[0] = smc_ism_get_chid(ini->ism_dev[0]); |
| 1046 |
return 0; |
| 1047 |
} |
| 1048 |
|
| 1049 |
/* is chid unique for the ism devices that are already determined? */ |
| 1050 |
static bool smc_find_ism_v2_is_unique_chid(u16 chid, struct smc_init_info *ini, |
| 1051 |
int cnt) |
| 1052 |
{ |
| 1053 |
int i = (!ini->ism_dev[0]) ? 1 : 0; |
| 1054 |
|
| 1055 |
for (; i < cnt; i++) |
| 1056 |
if (ini->ism_chid[i] == chid) |
| 1057 |
return false; |
| 1058 |
return true; |
| 1059 |
} |
| 1060 |
|
| 1061 |
/* determine possible V2 ISM devices (either without PNETID or with PNETID plus |
| 1062 |
* PNETID matching net_device) |
| 1063 |
*/ |
| 1064 |
static int smc_find_ism_v2_device_clnt(struct smc_sock *smc, |
| 1065 |
struct smc_init_info *ini) |
| 1066 |
{ |
| 1067 |
int rc = SMC_CLC_DECL_NOSMCDDEV; |
| 1068 |
struct smcd_dev *smcd; |
| 1069 |
int i = 1, entry = 1; |
| 1070 |
bool is_emulated; |
| 1071 |
u16 chid; |
| 1072 |
|
| 1073 |
if (smcd_indicated(ini->smc_type_v1)) |
| 1074 |
rc = 0; /* already initialized for V1 */ |
| 1075 |
mutex_lock(&smcd_dev_list.mutex); |
| 1076 |
list_for_each_entry(smcd, &smcd_dev_list.list, list) { |
| 1077 |
if (smcd->going_away || smcd == ini->ism_dev[0]) |
| 1078 |
continue; |
| 1079 |
chid = smc_ism_get_chid(smcd); |
| 1080 |
if (!smc_find_ism_v2_is_unique_chid(chid, ini, i)) |
| 1081 |
continue; |
| 1082 |
is_emulated = __smc_ism_is_emulated(chid); |
| 1083 |
if (!smc_pnet_is_pnetid_set(smcd->pnetid) || |
| 1084 |
smc_pnet_is_ndev_pnetid(sock_net(&smc->sk), smcd->pnetid)) { |
| 1085 |
if (is_emulated && entry == SMCD_CLC_MAX_V2_GID_ENTRIES) |
| 1086 |
/* It's the last GID-CHID entry left in CLC |
| 1087 |
* Proposal SMC-Dv2 extension, but an Emulated- |
| 1088 |
* ISM device will take two entries. So give |
| 1089 |
* up it and try the next potential ISM device. |
| 1090 |
*/ |
| 1091 |
continue; |
| 1092 |
ini->ism_dev[i] = smcd; |
| 1093 |
ini->ism_chid[i] = chid; |
| 1094 |
ini->is_smcd = true; |
| 1095 |
rc = 0; |
| 1096 |
i++; |
| 1097 |
entry = is_emulated ? entry + 2 : entry + 1; |
| 1098 |
if (entry > SMCD_CLC_MAX_V2_GID_ENTRIES) |
| 1099 |
break; |
| 1100 |
} |
| 1101 |
} |
| 1102 |
mutex_unlock(&smcd_dev_list.mutex); |
| 1103 |
ini->ism_offered_cnt = i - 1; |
| 1104 |
if (!ini->ism_dev[0] && !ini->ism_dev[1]) |
| 1105 |
ini->smcd_version = 0; |
| 1106 |
|
| 1107 |
return rc; |
| 1108 |
} |
| 1109 |
|
| 1110 |
/* Check for VLAN ID and register it on ISM device just for CLC handshake */ |
| 1111 |
static int smc_connect_ism_vlan_setup(struct smc_init_info *ini) |
| 1112 |
{ |
| 1113 |
if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev[0], ini->vlan_id)) |
| 1114 |
return SMC_CLC_DECL_ISMVLANERR; |
| 1115 |
return 0; |
| 1116 |
} |
| 1117 |
|
| 1118 |
static int smc_find_proposal_devices(struct smc_sock *smc, |
| 1119 |
struct smc_init_info *ini) |
| 1120 |
{ |
| 1121 |
int rc = 0; |
| 1122 |
|
| 1123 |
/* check if there is an ism device available */ |
| 1124 |
if (!(ini->smcd_version & SMC_V1) || |
| 1125 |
smc_find_ism_device(smc, ini) || |
| 1126 |
smc_connect_ism_vlan_setup(ini)) |
| 1127 |
ini->smcd_version &= ~SMC_V1; |
| 1128 |
/* else ISM V1 is supported for this connection */ |
| 1129 |
|
| 1130 |
/* check if there is an rdma device available */ |
| 1131 |
if (!(ini->smcr_version & SMC_V1) || |
| 1132 |
smc_find_rdma_device(smc, ini)) |
| 1133 |
ini->smcr_version &= ~SMC_V1; |
| 1134 |
/* else RDMA is supported for this connection */ |
| 1135 |
|
| 1136 |
ini->smc_type_v1 = smc_indicated_type(ini->smcd_version & SMC_V1, |
| 1137 |
ini->smcr_version & SMC_V1); |
| 1138 |
|
| 1139 |
/* check if there is an ism v2 device available */ |
| 1140 |
if (!(ini->smcd_version & SMC_V2) || |
| 1141 |
!smc_ism_is_v2_capable() || |
| 1142 |
smc_find_ism_v2_device_clnt(smc, ini)) |
| 1143 |
ini->smcd_version &= ~SMC_V2; |
| 1144 |
|
| 1145 |
/* check if there is an rdma v2 device available */ |
| 1146 |
ini->check_smcrv2 = true; |
| 1147 |
ini->smcrv2.saddr = smc->clcsock->sk->sk_rcv_saddr; |
| 1148 |
if (!(ini->smcr_version & SMC_V2) || |
| 1149 |
#if IS_ENABLED(CONFIG_IPV6) |
| 1150 |
(smc->clcsock->sk->sk_family == AF_INET6 && |
| 1151 |
!ipv6_addr_v4mapped(&smc->clcsock->sk->sk_v6_rcv_saddr)) || |
| 1152 |
#endif |
| 1153 |
!smc_clc_ueid_count() || |
| 1154 |
smc_find_rdma_device(smc, ini)) |
| 1155 |
ini->smcr_version &= ~SMC_V2; |
| 1156 |
ini->check_smcrv2 = false; |
| 1157 |
|
| 1158 |
ini->smc_type_v2 = smc_indicated_type(ini->smcd_version & SMC_V2, |
| 1159 |
ini->smcr_version & SMC_V2); |
| 1160 |
|
| 1161 |
/* if neither ISM nor RDMA are supported, fallback */ |
| 1162 |
if (ini->smc_type_v1 == SMC_TYPE_N && ini->smc_type_v2 == SMC_TYPE_N) |
| 1163 |
rc = SMC_CLC_DECL_NOSMCDEV; |
| 1164 |
|
| 1165 |
return rc; |
| 1166 |
} |
| 1167 |
|
| 1168 |
/* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is |
| 1169 |
* used, the VLAN ID will be registered again during the connection setup. |
| 1170 |
*/ |
| 1171 |
static int smc_connect_ism_vlan_cleanup(struct smc_init_info *ini) |
| 1172 |
{ |
| 1173 |
if (!smcd_indicated(ini->smc_type_v1)) |
| 1174 |
return 0; |
| 1175 |
if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev[0], ini->vlan_id)) |
| 1176 |
return SMC_CLC_DECL_CNFERR; |
| 1177 |
return 0; |
| 1178 |
} |
| 1179 |
|
| 1180 |
#define SMC_CLC_MAX_ACCEPT_LEN \ |
| 1181 |
(sizeof(struct smc_clc_msg_accept_confirm) + \ |
| 1182 |
sizeof(struct smc_clc_first_contact_ext_v2x) + \ |
| 1183 |
sizeof(struct smc_clc_msg_trail)) |
| 1184 |
|
| 1185 |
/* CLC handshake during connect */ |
| 1186 |
static int smc_connect_clc(struct smc_sock *smc, |
| 1187 |
struct smc_clc_msg_accept_confirm *aclc, |
| 1188 |
struct smc_init_info *ini) |
| 1189 |
{ |
| 1190 |
int rc = 0; |
| 1191 |
|
| 1192 |
/* do inband token exchange */ |
| 1193 |
rc = smc_clc_send_proposal(smc, ini); |
| 1194 |
if (rc) |
| 1195 |
return rc; |
| 1196 |
/* receive SMC Accept CLC message */ |
| 1197 |
return smc_clc_wait_msg(smc, aclc, SMC_CLC_MAX_ACCEPT_LEN, |
| 1198 |
SMC_CLC_ACCEPT, CLC_WAIT_TIME); |
| 1199 |
} |
| 1200 |
|
| 1201 |
void smc_fill_gid_list(struct smc_link_group *lgr, |
| 1202 |
struct smc_gidlist *gidlist, |
| 1203 |
struct smc_ib_device *known_dev, u8 *known_gid) |
| 1204 |
{ |
| 1205 |
struct smc_init_info *alt_ini = NULL; |
| 1206 |
|
| 1207 |
memset(gidlist, 0, sizeof(*gidlist)); |
| 1208 |
memcpy(gidlist->list[gidlist->len++], known_gid, SMC_GID_SIZE); |
| 1209 |
|
| 1210 |
alt_ini = kzalloc_obj(*alt_ini); |
| 1211 |
if (!alt_ini) |
| 1212 |
goto out; |
| 1213 |
|
| 1214 |
alt_ini->vlan_id = lgr->vlan_id; |
| 1215 |
alt_ini->check_smcrv2 = true; |
| 1216 |
alt_ini->smcrv2.saddr = lgr->saddr; |
| 1217 |
smc_pnet_find_alt_roce(lgr, alt_ini, known_dev); |
| 1218 |
|
| 1219 |
if (!alt_ini->smcrv2.ib_dev_v2) |
| 1220 |
goto out; |
| 1221 |
|
| 1222 |
memcpy(gidlist->list[gidlist->len++], alt_ini->smcrv2.ib_gid_v2, |
| 1223 |
SMC_GID_SIZE); |
| 1224 |
|
| 1225 |
out: |
| 1226 |
kfree(alt_ini); |
| 1227 |
} |
| 1228 |
|
| 1229 |
static int smc_connect_rdma_v2_prepare(struct smc_sock *smc, |
| 1230 |
struct smc_clc_msg_accept_confirm *aclc, |
| 1231 |
struct smc_init_info *ini) |
| 1232 |
{ |
| 1233 |
struct smc_clc_first_contact_ext *fce = |
| 1234 |
smc_get_clc_first_contact_ext(aclc, false); |
| 1235 |
struct net *net = sock_net(&smc->sk); |
| 1236 |
int rc; |
| 1237 |
|
| 1238 |
if (!ini->first_contact_peer || aclc->hdr.version == SMC_V1) |
| 1239 |
return 0; |
| 1240 |
|
| 1241 |
if (fce->v2_direct) { |
| 1242 |
memcpy(ini->smcrv2.nexthop_mac, &aclc->r0.lcl.mac, ETH_ALEN); |
| 1243 |
ini->smcrv2.uses_gateway = false; |
| 1244 |
} else { |
| 1245 |
if (smc_ib_find_route(net, smc->clcsock->sk->sk_rcv_saddr, |
| 1246 |
smc_ib_gid_to_ipv4(aclc->r0.lcl.gid), |
| 1247 |
ini->smcrv2.nexthop_mac, |
| 1248 |
&ini->smcrv2.uses_gateway)) |
| 1249 |
return SMC_CLC_DECL_NOROUTE; |
| 1250 |
if (!ini->smcrv2.uses_gateway) { |
| 1251 |
/* mismatch: peer claims indirect, but its direct */ |
| 1252 |
return SMC_CLC_DECL_NOINDIRECT; |
| 1253 |
} |
| 1254 |
} |
| 1255 |
|
| 1256 |
ini->release_nr = fce->release; |
| 1257 |
rc = smc_clc_clnt_v2x_features_validate(fce, ini); |
| 1258 |
if (rc) |
| 1259 |
return rc; |
| 1260 |
|
| 1261 |
return 0; |
| 1262 |
} |
| 1263 |
|
| 1264 |
/* setup for RDMA connection of client */ |
| 1265 |
static int smc_connect_rdma(struct smc_sock *smc, |
| 1266 |
struct smc_clc_msg_accept_confirm *aclc, |
| 1267 |
struct smc_init_info *ini) |
| 1268 |
{ |
| 1269 |
int i, reason_code = 0; |
| 1270 |
struct smc_link *link; |
| 1271 |
u8 *eid = NULL; |
| 1272 |
|
| 1273 |
ini->is_smcd = false; |
| 1274 |
ini->ib_clcqpn = ntoh24(aclc->r0.qpn); |
| 1275 |
ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK; |
| 1276 |
memcpy(ini->peer_systemid, aclc->r0.lcl.id_for_peer, SMC_SYSTEMID_LEN); |
| 1277 |
memcpy(ini->peer_gid, aclc->r0.lcl.gid, SMC_GID_SIZE); |
| 1278 |
memcpy(ini->peer_mac, aclc->r0.lcl.mac, ETH_ALEN); |
| 1279 |
ini->max_conns = SMC_CONN_PER_LGR_MAX; |
| 1280 |
ini->max_links = SMC_LINKS_ADD_LNK_MAX; |
| 1281 |
|
| 1282 |
reason_code = smc_connect_rdma_v2_prepare(smc, aclc, ini); |
| 1283 |
if (reason_code) |
| 1284 |
return reason_code; |
| 1285 |
|
| 1286 |
mutex_lock(&smc_client_lgr_pending); |
| 1287 |
reason_code = smc_conn_create(smc, ini); |
| 1288 |
if (reason_code) { |
| 1289 |
mutex_unlock(&smc_client_lgr_pending); |
| 1290 |
return reason_code; |
| 1291 |
} |
| 1292 |
|
| 1293 |
smc_conn_save_peer_info(smc, aclc); |
| 1294 |
|
| 1295 |
if (ini->first_contact_local) { |
| 1296 |
link = smc->conn.lnk; |
| 1297 |
} else { |
| 1298 |
/* set link that was assigned by server */ |
| 1299 |
link = NULL; |
| 1300 |
for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) { |
| 1301 |
struct smc_link *l = &smc->conn.lgr->lnk[i]; |
| 1302 |
|
| 1303 |
if (l->peer_qpn == ntoh24(aclc->r0.qpn) && |
| 1304 |
!memcmp(l->peer_gid, &aclc->r0.lcl.gid, |
| 1305 |
SMC_GID_SIZE) && |
| 1306 |
(aclc->hdr.version > SMC_V1 || |
| 1307 |
!memcmp(l->peer_mac, &aclc->r0.lcl.mac, |
| 1308 |
sizeof(l->peer_mac)))) { |
| 1309 |
link = l; |
| 1310 |
break; |
| 1311 |
} |
| 1312 |
} |
| 1313 |
if (!link) { |
| 1314 |
reason_code = SMC_CLC_DECL_NOSRVLINK; |
| 1315 |
goto connect_abort; |
| 1316 |
} |
| 1317 |
smc_switch_link_and_count(&smc->conn, link); |
| 1318 |
} |
| 1319 |
|
| 1320 |
/* create send buffer and rmb */ |
| 1321 |
if (smc_buf_create(smc, false)) { |
| 1322 |
reason_code = SMC_CLC_DECL_MEM; |
| 1323 |
goto connect_abort; |
| 1324 |
} |
| 1325 |
|
| 1326 |
if (ini->first_contact_local) |
| 1327 |
smc_link_save_peer_info(link, aclc, ini); |
| 1328 |
|
| 1329 |
if (smc_rmb_rtoken_handling(&smc->conn, link, aclc)) { |
| 1330 |
reason_code = SMC_CLC_DECL_ERR_RTOK; |
| 1331 |
goto connect_abort; |
| 1332 |
} |
| 1333 |
|
| 1334 |
smc_rx_init(smc); |
| 1335 |
|
| 1336 |
if (ini->first_contact_local) { |
| 1337 |
if (smc_ib_ready_link(link)) { |
| 1338 |
reason_code = SMC_CLC_DECL_ERR_RDYLNK; |
| 1339 |
goto connect_abort; |
| 1340 |
} |
| 1341 |
} else { |
| 1342 |
/* reg sendbufs if they were vzalloced */ |
| 1343 |
if (smc->conn.sndbuf_desc->is_vm) { |
| 1344 |
if (smcr_lgr_reg_sndbufs(link, smc->conn.sndbuf_desc)) { |
| 1345 |
reason_code = SMC_CLC_DECL_ERR_REGBUF; |
| 1346 |
goto connect_abort; |
| 1347 |
} |
| 1348 |
} |
| 1349 |
if (smcr_lgr_reg_rmbs(link, smc->conn.rmb_desc)) { |
| 1350 |
reason_code = SMC_CLC_DECL_ERR_REGBUF; |
| 1351 |
goto connect_abort; |
| 1352 |
} |
| 1353 |
} |
| 1354 |
|
| 1355 |
if (aclc->hdr.version > SMC_V1) { |
| 1356 |
eid = aclc->r1.eid; |
| 1357 |
if (ini->first_contact_local) |
| 1358 |
smc_fill_gid_list(link->lgr, &ini->smcrv2.gidlist, |
| 1359 |
link->smcibdev, link->gid); |
| 1360 |
} |
| 1361 |
|
| 1362 |
reason_code = smc_clc_send_confirm(smc, ini->first_contact_local, |
| 1363 |
aclc->hdr.version, eid, ini); |
| 1364 |
if (reason_code) |
| 1365 |
goto connect_abort; |
| 1366 |
|
| 1367 |
smc_tx_init(smc); |
| 1368 |
|
| 1369 |
if (ini->first_contact_local) { |
| 1370 |
/* QP confirmation over RoCE fabric */ |
| 1371 |
smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK); |
| 1372 |
reason_code = smcr_clnt_conf_first_link(smc); |
| 1373 |
smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl); |
| 1374 |
if (reason_code) |
| 1375 |
goto connect_abort; |
| 1376 |
} |
| 1377 |
mutex_unlock(&smc_client_lgr_pending); |
| 1378 |
|
| 1379 |
smc_copy_sock_settings_to_clc(smc); |
| 1380 |
smc->connect_nonblock = 0; |
| 1381 |
if (smc->sk.sk_state == SMC_INIT) |
| 1382 |
smc->sk.sk_state = SMC_ACTIVE; |
| 1383 |
|
| 1384 |
return 0; |
| 1385 |
connect_abort: |
| 1386 |
smc_conn_abort(smc, ini->first_contact_local); |
| 1387 |
mutex_unlock(&smc_client_lgr_pending); |
| 1388 |
smc->connect_nonblock = 0; |
| 1389 |
|
| 1390 |
return reason_code; |
| 1391 |
} |
| 1392 |
|
| 1393 |
/* The server has chosen one of the proposed ISM devices for the communication. |
| 1394 |
* Determine from the CHID of the received CLC ACCEPT the ISM device chosen. |
| 1395 |
*/ |
| 1396 |
static int |
| 1397 |
smc_v2_determine_accepted_chid(struct smc_clc_msg_accept_confirm *aclc, |
| 1398 |
struct smc_init_info *ini) |
| 1399 |
{ |
| 1400 |
int i; |
| 1401 |
|
| 1402 |
for (i = 0; i < ini->ism_offered_cnt + 1; i++) { |
| 1403 |
if (ini->ism_chid[i] == ntohs(aclc->d1.chid)) { |
| 1404 |
ini->ism_selected = i; |
| 1405 |
return 0; |
| 1406 |
} |
| 1407 |
} |
| 1408 |
|
| 1409 |
return -EPROTO; |
| 1410 |
} |
| 1411 |
|
| 1412 |
/* setup for ISM connection of client */ |
| 1413 |
static int smc_connect_ism(struct smc_sock *smc, |
| 1414 |
struct smc_clc_msg_accept_confirm *aclc, |
| 1415 |
struct smc_init_info *ini) |
| 1416 |
{ |
| 1417 |
u8 *eid = NULL; |
| 1418 |
int rc = 0; |
| 1419 |
|
| 1420 |
ini->is_smcd = true; |
| 1421 |
ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK; |
| 1422 |
|
| 1423 |
if (aclc->hdr.version == SMC_V2) { |
| 1424 |
if (ini->first_contact_peer) { |
| 1425 |
struct smc_clc_first_contact_ext *fce = |
| 1426 |
smc_get_clc_first_contact_ext(aclc, true); |
| 1427 |
|
| 1428 |
ini->release_nr = fce->release; |
| 1429 |
rc = smc_clc_clnt_v2x_features_validate(fce, ini); |
| 1430 |
if (rc) |
| 1431 |
return rc; |
| 1432 |
} |
| 1433 |
|
| 1434 |
rc = smc_v2_determine_accepted_chid(aclc, ini); |
| 1435 |
if (rc) |
| 1436 |
return rc; |
| 1437 |
|
| 1438 |
if (__smc_ism_is_emulated(ini->ism_chid[ini->ism_selected])) |
| 1439 |
ini->ism_peer_gid[ini->ism_selected].gid_ext = |
| 1440 |
ntohll(aclc->d1.gid_ext); |
| 1441 |
/* for non-Emulated-ISM devices, peer gid_ext remains 0. */ |
| 1442 |
} |
| 1443 |
ini->ism_peer_gid[ini->ism_selected].gid = ntohll(aclc->d0.gid); |
| 1444 |
|
| 1445 |
/* there is only one lgr role for SMC-D; use server lock */ |
| 1446 |
mutex_lock(&smc_server_lgr_pending); |
| 1447 |
rc = smc_conn_create(smc, ini); |
| 1448 |
if (rc) { |
| 1449 |
mutex_unlock(&smc_server_lgr_pending); |
| 1450 |
return rc; |
| 1451 |
} |
| 1452 |
|
| 1453 |
/* Create send and receive buffers */ |
| 1454 |
rc = smc_buf_create(smc, true); |
| 1455 |
if (rc) { |
| 1456 |
rc = (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB : SMC_CLC_DECL_MEM; |
| 1457 |
goto connect_abort; |
| 1458 |
} |
| 1459 |
|
| 1460 |
smc_conn_save_peer_info(smc, aclc); |
| 1461 |
|
| 1462 |
if (smc_ism_support_dmb_nocopy(smc->conn.lgr->smcd)) { |
| 1463 |
rc = smcd_buf_attach(smc); |
| 1464 |
if (rc) { |
| 1465 |
rc = SMC_CLC_DECL_MEM; /* try to fallback */ |
| 1466 |
goto connect_abort; |
| 1467 |
} |
| 1468 |
} |
| 1469 |
smc_rx_init(smc); |
| 1470 |
smc_tx_init(smc); |
| 1471 |
|
| 1472 |
if (aclc->hdr.version > SMC_V1) |
| 1473 |
eid = aclc->d1.eid; |
| 1474 |
|
| 1475 |
rc = smc_clc_send_confirm(smc, ini->first_contact_local, |
| 1476 |
aclc->hdr.version, eid, ini); |
| 1477 |
if (rc) |
| 1478 |
goto connect_abort; |
| 1479 |
mutex_unlock(&smc_server_lgr_pending); |
| 1480 |
|
| 1481 |
smc_copy_sock_settings_to_clc(smc); |
| 1482 |
smc->connect_nonblock = 0; |
| 1483 |
if (smc->sk.sk_state == SMC_INIT) |
| 1484 |
smc->sk.sk_state = SMC_ACTIVE; |
| 1485 |
|
| 1486 |
return 0; |
| 1487 |
connect_abort: |
| 1488 |
smc_conn_abort(smc, ini->first_contact_local); |
| 1489 |
mutex_unlock(&smc_server_lgr_pending); |
| 1490 |
smc->connect_nonblock = 0; |
| 1491 |
|
| 1492 |
return rc; |
| 1493 |
} |
| 1494 |
|
| 1495 |
/* check if received accept type and version matches a proposed one */ |
| 1496 |
static int smc_connect_check_aclc(struct smc_init_info *ini, |
| 1497 |
struct smc_clc_msg_accept_confirm *aclc) |
| 1498 |
{ |
| 1499 |
if (aclc->hdr.version >= SMC_V2) { |
| 1500 |
if ((aclc->hdr.typev1 == SMC_TYPE_R && |
| 1501 |
!smcr_indicated(ini->smc_type_v2)) || |
| 1502 |
(aclc->hdr.typev1 == SMC_TYPE_D && |
| 1503 |
!smcd_indicated(ini->smc_type_v2))) |
| 1504 |
return SMC_CLC_DECL_MODEUNSUPP; |
| 1505 |
} else { |
| 1506 |
if ((aclc->hdr.typev1 == SMC_TYPE_R && |
| 1507 |
!smcr_indicated(ini->smc_type_v1)) || |
| 1508 |
(aclc->hdr.typev1 == SMC_TYPE_D && |
| 1509 |
!smcd_indicated(ini->smc_type_v1))) |
| 1510 |
return SMC_CLC_DECL_MODEUNSUPP; |
| 1511 |
} |
| 1512 |
|
| 1513 |
return 0; |
| 1514 |
} |
| 1515 |
|
| 1516 |
/* perform steps before actually connecting */ |
| 1517 |
static int __smc_connect(struct smc_sock *smc) |
| 1518 |
{ |
| 1519 |
u8 version = smc_ism_is_v2_capable() ? SMC_V2 : SMC_V1; |
| 1520 |
struct smc_clc_msg_accept_confirm *aclc; |
| 1521 |
struct smc_init_info *ini = NULL; |
| 1522 |
u8 *buf = NULL; |
| 1523 |
int rc = 0; |
| 1524 |
|
| 1525 |
if (smc->use_fallback) |
| 1526 |
return smc_connect_fallback(smc, smc->fallback_rsn); |
| 1527 |
|
| 1528 |
/* if peer has not signalled SMC-capability, fall back */ |
| 1529 |
if (!tcp_sk(smc->clcsock->sk)->syn_smc) |
| 1530 |
return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC); |
| 1531 |
|
| 1532 |
/* IPSec connections opt out of SMC optimizations */ |
| 1533 |
if (using_ipsec(smc)) |
| 1534 |
return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC, |
| 1535 |
version); |
| 1536 |
|
| 1537 |
ini = kzalloc_obj(*ini); |
| 1538 |
if (!ini) |
| 1539 |
return smc_connect_decline_fallback(smc, SMC_CLC_DECL_MEM, |
| 1540 |
version); |
| 1541 |
|
| 1542 |
ini->smcd_version = SMC_V1 | SMC_V2; |
| 1543 |
ini->smcr_version = SMC_V1 | SMC_V2; |
| 1544 |
ini->smc_type_v1 = SMC_TYPE_B; |
| 1545 |
ini->smc_type_v2 = SMC_TYPE_B; |
| 1546 |
|
| 1547 |
/* get vlan id from IP device */ |
| 1548 |
if (smc_vlan_by_tcpsk(smc->clcsock, ini)) { |
| 1549 |
ini->smcd_version &= ~SMC_V1; |
| 1550 |
ini->smcr_version = 0; |
| 1551 |
ini->smc_type_v1 = SMC_TYPE_N; |
| 1552 |
} |
| 1553 |
|
| 1554 |
rc = smc_find_proposal_devices(smc, ini); |
| 1555 |
if (rc) |
| 1556 |
goto fallback; |
| 1557 |
|
| 1558 |
buf = kzalloc(SMC_CLC_MAX_ACCEPT_LEN, GFP_KERNEL); |
| 1559 |
if (!buf) { |
| 1560 |
rc = SMC_CLC_DECL_MEM; |
| 1561 |
goto fallback; |
| 1562 |
} |
| 1563 |
aclc = (struct smc_clc_msg_accept_confirm *)buf; |
| 1564 |
|
| 1565 |
/* perform CLC handshake */ |
| 1566 |
rc = smc_connect_clc(smc, aclc, ini); |
| 1567 |
if (rc) { |
| 1568 |
/* -EAGAIN on timeout, see tcp_recvmsg() */ |
| 1569 |
if (rc == -EAGAIN) { |
| 1570 |
rc = -ETIMEDOUT; |
| 1571 |
smc->sk.sk_err = ETIMEDOUT; |
| 1572 |
} |
| 1573 |
goto vlan_cleanup; |
| 1574 |
} |
| 1575 |
|
| 1576 |
/* check if smc modes and versions of CLC proposal and accept match */ |
| 1577 |
rc = smc_connect_check_aclc(ini, aclc); |
| 1578 |
version = aclc->hdr.version == SMC_V1 ? SMC_V1 : SMC_V2; |
| 1579 |
if (rc) |
| 1580 |
goto vlan_cleanup; |
| 1581 |
|
| 1582 |
/* depending on previous steps, connect using rdma or ism */ |
| 1583 |
if (aclc->hdr.typev1 == SMC_TYPE_R) { |
| 1584 |
ini->smcr_version = version; |
| 1585 |
rc = smc_connect_rdma(smc, aclc, ini); |
| 1586 |
} else if (aclc->hdr.typev1 == SMC_TYPE_D) { |
| 1587 |
ini->smcd_version = version; |
| 1588 |
rc = smc_connect_ism(smc, aclc, ini); |
| 1589 |
} |
| 1590 |
if (rc) |
| 1591 |
goto vlan_cleanup; |
| 1592 |
|
| 1593 |
SMC_STAT_CLNT_SUCC_INC(sock_net(smc->clcsock->sk), aclc); |
| 1594 |
smc_connect_ism_vlan_cleanup(ini); |
| 1595 |
kfree(buf); |
| 1596 |
kfree(ini); |
| 1597 |
return 0; |
| 1598 |
|
| 1599 |
vlan_cleanup: |
| 1600 |
smc_connect_ism_vlan_cleanup(ini); |
| 1601 |
kfree(buf); |
| 1602 |
fallback: |
| 1603 |
kfree(ini); |
| 1604 |
return smc_connect_decline_fallback(smc, rc, version); |
| 1605 |
} |
| 1606 |
|
| 1607 |
static void smc_connect_work(struct work_struct *work) |
| 1608 |
{ |
| 1609 |
struct smc_sock *smc = container_of(work, struct smc_sock, |
| 1610 |
connect_work); |
| 1611 |
long timeo = READ_ONCE(smc->sk.sk_sndtimeo); |
| 1612 |
int rc = 0; |
| 1613 |
|
| 1614 |
if (!timeo) |
| 1615 |
timeo = MAX_SCHEDULE_TIMEOUT; |
| 1616 |
lock_sock(smc->clcsock->sk); |
| 1617 |
if (smc->clcsock->sk->sk_err) { |
| 1618 |
smc->sk.sk_err = smc->clcsock->sk->sk_err; |
| 1619 |
} else if ((1 << smc->clcsock->sk->sk_state) & |
| 1620 |
(TCPF_SYN_SENT | TCPF_SYN_RECV)) { |
| 1621 |
rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo); |
| 1622 |
if ((rc == -EPIPE) && |
| 1623 |
((1 << smc->clcsock->sk->sk_state) & |
| 1624 |
(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT))) |
| 1625 |
rc = 0; |
| 1626 |
} |
| 1627 |
release_sock(smc->clcsock->sk); |
| 1628 |
lock_sock(&smc->sk); |
| 1629 |
if (rc != 0 || smc->sk.sk_err) { |
| 1630 |
smc->sk.sk_state = SMC_CLOSED; |
| 1631 |
if (rc == -EPIPE || rc == -EAGAIN) |
| 1632 |
smc->sk.sk_err = EPIPE; |
| 1633 |
else if (rc == -ECONNREFUSED) |
| 1634 |
smc->sk.sk_err = ECONNREFUSED; |
| 1635 |
else if (signal_pending(current)) |
| 1636 |
smc->sk.sk_err = -sock_intr_errno(timeo); |
| 1637 |
sock_put(&smc->sk); /* passive closing */ |
| 1638 |
goto out; |
| 1639 |
} |
| 1640 |
|
| 1641 |
rc = __smc_connect(smc); |
| 1642 |
if (rc < 0) |
| 1643 |
smc->sk.sk_err = -rc; |
| 1644 |
|
| 1645 |
out: |
| 1646 |
if (!sock_flag(&smc->sk, SOCK_DEAD)) { |
| 1647 |
if (smc->sk.sk_err) { |
| 1648 |
smc->sk.sk_state_change(&smc->sk); |
| 1649 |
} else { /* allow polling before and after fallback decision */ |
| 1650 |
smc->clcsock->sk->sk_write_space(smc->clcsock->sk); |
| 1651 |
smc->sk.sk_write_space(&smc->sk); |
| 1652 |
} |
| 1653 |
} |
| 1654 |
release_sock(&smc->sk); |
| 1655 |
} |
| 1656 |
|
| 1657 |
int smc_connect(struct socket *sock, struct sockaddr_unsized *addr, |
| 1658 |
int alen, int flags) |
| 1659 |
{ |
| 1660 |
struct sock *sk = sock->sk; |
| 1661 |
struct smc_sock *smc; |
| 1662 |
int rc = -EINVAL; |
| 1663 |
|
| 1664 |
smc = smc_sk(sk); |
| 1665 |
|
| 1666 |
/* separate smc parameter checking to be safe */ |
| 1667 |
if (alen < sizeof(addr->sa_family)) |
| 1668 |
goto out_err; |
| 1669 |
if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6) |
| 1670 |
goto out_err; |
| 1671 |
|
| 1672 |
lock_sock(sk); |
| 1673 |
switch (sock->state) { |
| 1674 |
default: |
| 1675 |
rc = -EINVAL; |
| 1676 |
goto out; |
| 1677 |
case SS_CONNECTED: |
| 1678 |
rc = sk->sk_state == SMC_ACTIVE ? -EISCONN : -EINVAL; |
| 1679 |
goto out; |
| 1680 |
case SS_CONNECTING: |
| 1681 |
if (sk->sk_state == SMC_ACTIVE) |
| 1682 |
goto connected; |
| 1683 |
break; |
| 1684 |
case SS_UNCONNECTED: |
| 1685 |
sock->state = SS_CONNECTING; |
| 1686 |
break; |
| 1687 |
} |
| 1688 |
|
| 1689 |
switch (sk->sk_state) { |
| 1690 |
default: |
| 1691 |
goto out; |
| 1692 |
case SMC_CLOSED: |
| 1693 |
rc = sock_error(sk) ? : -ECONNABORTED; |
| 1694 |
sock->state = SS_UNCONNECTED; |
| 1695 |
goto out; |
| 1696 |
case SMC_ACTIVE: |
| 1697 |
rc = -EISCONN; |
| 1698 |
goto out; |
| 1699 |
case SMC_INIT: |
| 1700 |
break; |
| 1701 |
} |
| 1702 |
|
| 1703 |
smc_copy_sock_settings_to_clc(smc); |
| 1704 |
tcp_sk(smc->clcsock->sk)->syn_smc = 1; |
| 1705 |
if (smc->connect_nonblock) { |
| 1706 |
rc = -EALREADY; |
| 1707 |
goto out; |
| 1708 |
} |
| 1709 |
rc = kernel_connect(smc->clcsock, (struct sockaddr_unsized *)addr, alen, flags); |
| 1710 |
if (rc && rc != -EINPROGRESS) |
| 1711 |
goto out; |
| 1712 |
|
| 1713 |
if (smc->use_fallback) { |
| 1714 |
sock->state = rc ? SS_CONNECTING : SS_CONNECTED; |
| 1715 |
goto out; |
| 1716 |
} |
| 1717 |
sock_hold(&smc->sk); /* sock put in passive closing */ |
| 1718 |
if (flags & O_NONBLOCK) { |
| 1719 |
if (queue_work(smc_hs_wq, &smc->connect_work)) |
| 1720 |
smc->connect_nonblock = 1; |
| 1721 |
rc = -EINPROGRESS; |
| 1722 |
goto out; |
| 1723 |
} else { |
| 1724 |
rc = __smc_connect(smc); |
| 1725 |
if (rc < 0) |
| 1726 |
goto out; |
| 1727 |
} |
| 1728 |
|
| 1729 |
connected: |
| 1730 |
rc = 0; |
| 1731 |
sock->state = SS_CONNECTED; |
| 1732 |
out: |
| 1733 |
release_sock(sk); |
| 1734 |
out_err: |
| 1735 |
return rc; |
| 1736 |
} |
| 1737 |
|
| 1738 |
static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc) |
| 1739 |
{ |
| 1740 |
struct socket *new_clcsock = NULL; |
| 1741 |
struct sock *lsk = &lsmc->sk; |
| 1742 |
struct sock *new_sk; |
| 1743 |
int rc = -EINVAL; |
| 1744 |
|
| 1745 |
release_sock(lsk); |
| 1746 |
new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol); |
| 1747 |
if (!new_sk) { |
| 1748 |
rc = -ENOMEM; |
| 1749 |
lsk->sk_err = ENOMEM; |
| 1750 |
*new_smc = NULL; |
| 1751 |
lock_sock(lsk); |
| 1752 |
goto out; |
| 1753 |
} |
| 1754 |
*new_smc = smc_sk(new_sk); |
| 1755 |
|
| 1756 |
mutex_lock(&lsmc->clcsock_release_lock); |
| 1757 |
if (lsmc->clcsock) |
| 1758 |
rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK); |
| 1759 |
mutex_unlock(&lsmc->clcsock_release_lock); |
| 1760 |
lock_sock(lsk); |
| 1761 |
if (rc < 0 && rc != -EAGAIN) |
| 1762 |
lsk->sk_err = -rc; |
| 1763 |
if (rc < 0 || lsk->sk_state == SMC_CLOSED) { |
| 1764 |
new_sk->sk_prot->unhash(new_sk); |
| 1765 |
if (new_clcsock) |
| 1766 |
sock_release(new_clcsock); |
| 1767 |
new_sk->sk_state = SMC_CLOSED; |
| 1768 |
smc_sock_set_flag(new_sk, SOCK_DEAD); |
| 1769 |
sock_put(new_sk); /* final */ |
| 1770 |
*new_smc = NULL; |
| 1771 |
goto out; |
| 1772 |
} |
| 1773 |
|
| 1774 |
/* new clcsock has inherited the smc listen-specific sk_data_ready |
| 1775 |
* function; switch it back to the original sk_data_ready function |
| 1776 |
*/ |
| 1777 |
new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready; |
| 1778 |
|
| 1779 |
/* if new clcsock has also inherited the fallback-specific callback |
| 1780 |
* functions, switch them back to the original ones. |
| 1781 |
*/ |
| 1782 |
if (lsmc->use_fallback) { |
| 1783 |
if (lsmc->clcsk_state_change) |
| 1784 |
new_clcsock->sk->sk_state_change = lsmc->clcsk_state_change; |
| 1785 |
if (lsmc->clcsk_write_space) |
| 1786 |
new_clcsock->sk->sk_write_space = lsmc->clcsk_write_space; |
| 1787 |
if (lsmc->clcsk_error_report) |
| 1788 |
new_clcsock->sk->sk_error_report = lsmc->clcsk_error_report; |
| 1789 |
} |
| 1790 |
|
| 1791 |
(*new_smc)->clcsock = new_clcsock; |
| 1792 |
out: |
| 1793 |
return rc; |
| 1794 |
} |
| 1795 |
|
| 1796 |
/* add a just created sock to the accept queue of the listen sock as |
| 1797 |
* candidate for a following socket accept call from user space |
| 1798 |
*/ |
| 1799 |
static void smc_accept_enqueue(struct sock *parent, struct sock *sk) |
| 1800 |
{ |
| 1801 |
struct smc_sock *par = smc_sk(parent); |
| 1802 |
|
| 1803 |
sock_hold(sk); /* sock_put in smc_accept_unlink () */ |
| 1804 |
spin_lock(&par->accept_q_lock); |
| 1805 |
list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q); |
| 1806 |
spin_unlock(&par->accept_q_lock); |
| 1807 |
sk_acceptq_added(parent); |
| 1808 |
} |
| 1809 |
|
| 1810 |
/* remove a socket from the accept queue of its parental listening socket */ |
| 1811 |
static void smc_accept_unlink(struct sock *sk) |
| 1812 |
{ |
| 1813 |
struct smc_sock *par = smc_sk(sk)->listen_smc; |
| 1814 |
|
| 1815 |
spin_lock(&par->accept_q_lock); |
| 1816 |
list_del_init(&smc_sk(sk)->accept_q); |
| 1817 |
spin_unlock(&par->accept_q_lock); |
| 1818 |
sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk); |
| 1819 |
sock_put(sk); /* sock_hold in smc_accept_enqueue */ |
| 1820 |
} |
| 1821 |
|
| 1822 |
/* remove a sock from the accept queue to bind it to a new socket created |
| 1823 |
* for a socket accept call from user space |
| 1824 |
*/ |
| 1825 |
struct sock *smc_accept_dequeue(struct sock *parent, |
| 1826 |
struct socket *new_sock) |
| 1827 |
{ |
| 1828 |
struct smc_sock *isk, *n; |
| 1829 |
struct sock *new_sk; |
| 1830 |
|
| 1831 |
list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) { |
| 1832 |
new_sk = (struct sock *)isk; |
| 1833 |
|
| 1834 |
smc_accept_unlink(new_sk); |
| 1835 |
if (new_sk->sk_state == SMC_CLOSED) { |
| 1836 |
new_sk->sk_prot->unhash(new_sk); |
| 1837 |
if (isk->clcsock) { |
| 1838 |
sock_release(isk->clcsock); |
| 1839 |
isk->clcsock = NULL; |
| 1840 |
} |
| 1841 |
sock_put(new_sk); /* final */ |
| 1842 |
continue; |
| 1843 |
} |
| 1844 |
if (new_sock) { |
| 1845 |
sock_graft(new_sk, new_sock); |
| 1846 |
new_sock->state = SS_CONNECTED; |
| 1847 |
if (isk->use_fallback) { |
| 1848 |
smc_sk(new_sk)->clcsock->file = new_sock->file; |
| 1849 |
isk->clcsock->file->private_data = isk->clcsock; |
| 1850 |
} |
| 1851 |
} |
| 1852 |
return new_sk; |
| 1853 |
} |
| 1854 |
return NULL; |
| 1855 |
} |
| 1856 |
|
| 1857 |
/* clean up for a created but never accepted sock */ |
| 1858 |
void smc_close_non_accepted(struct sock *sk) |
| 1859 |
{ |
| 1860 |
struct smc_sock *smc = smc_sk(sk); |
| 1861 |
|
| 1862 |
sock_hold(sk); /* sock_put below */ |
| 1863 |
lock_sock(sk); |
| 1864 |
if (!sk->sk_lingertime) |
| 1865 |
/* wait for peer closing */ |
| 1866 |
WRITE_ONCE(sk->sk_lingertime, SMC_MAX_STREAM_WAIT_TIMEOUT); |
| 1867 |
__smc_release(smc); |
| 1868 |
release_sock(sk); |
| 1869 |
sock_put(sk); /* sock_hold above */ |
| 1870 |
sock_put(sk); /* final sock_put */ |
| 1871 |
} |
| 1872 |
|
| 1873 |
static int smcr_serv_conf_first_link(struct smc_sock *smc) |
| 1874 |
{ |
| 1875 |
struct smc_link *link = smc->conn.lnk; |
| 1876 |
struct smc_llc_qentry *qentry; |
| 1877 |
int rc; |
| 1878 |
|
| 1879 |
/* reg the sndbuf if it was vzalloced*/ |
| 1880 |
if (smc->conn.sndbuf_desc->is_vm) { |
| 1881 |
if (smcr_link_reg_buf(link, smc->conn.sndbuf_desc)) |
| 1882 |
return SMC_CLC_DECL_ERR_REGBUF; |
| 1883 |
} |
| 1884 |
|
| 1885 |
/* reg the rmb */ |
| 1886 |
if (smcr_link_reg_buf(link, smc->conn.rmb_desc)) |
| 1887 |
return SMC_CLC_DECL_ERR_REGBUF; |
| 1888 |
|
| 1889 |
/* send CONFIRM LINK request to client over the RoCE fabric */ |
| 1890 |
rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ); |
| 1891 |
if (rc < 0) |
| 1892 |
return SMC_CLC_DECL_TIMEOUT_CL; |
| 1893 |
|
| 1894 |
/* receive CONFIRM LINK response from client over the RoCE fabric */ |
| 1895 |
qentry = smc_llc_wait(link->lgr, link, SMC_LLC_WAIT_TIME, |
| 1896 |
SMC_LLC_CONFIRM_LINK); |
| 1897 |
if (!qentry) { |
| 1898 |
struct smc_clc_msg_decline dclc; |
| 1899 |
|
| 1900 |
rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc), |
| 1901 |
SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT); |
| 1902 |
return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc; |
| 1903 |
} |
| 1904 |
smc_llc_save_peer_uid(qentry); |
| 1905 |
rc = smc_llc_eval_conf_link(qentry, SMC_LLC_RESP); |
| 1906 |
smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl); |
| 1907 |
if (rc) |
| 1908 |
return SMC_CLC_DECL_RMBE_EC; |
| 1909 |
|
| 1910 |
/* confirm_rkey is implicit on 1st contact */ |
| 1911 |
smc->conn.rmb_desc->is_conf_rkey = true; |
| 1912 |
|
| 1913 |
smc_llc_link_active(link); |
| 1914 |
smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE); |
| 1915 |
|
| 1916 |
if (link->lgr->max_links > 1) { |
| 1917 |
down_write(&link->lgr->llc_conf_mutex); |
| 1918 |
/* initial contact - try to establish second link */ |
| 1919 |
smc_llc_srv_add_link(link, NULL); |
| 1920 |
up_write(&link->lgr->llc_conf_mutex); |
| 1921 |
} |
| 1922 |
return 0; |
| 1923 |
} |
| 1924 |
|
| 1925 |
/* listen worker: finish */ |
| 1926 |
static void smc_listen_out(struct smc_sock *new_smc) |
| 1927 |
{ |
| 1928 |
struct smc_sock *lsmc = new_smc->listen_smc; |
| 1929 |
struct sock *newsmcsk = &new_smc->sk; |
| 1930 |
|
| 1931 |
if (tcp_sk(new_smc->clcsock->sk)->syn_smc) |
| 1932 |
atomic_dec(&lsmc->queued_smc_hs); |
| 1933 |
|
| 1934 |
release_sock(newsmcsk); /* lock in smc_listen_work() */ |
| 1935 |
if (lsmc->sk.sk_state == SMC_LISTEN) { |
| 1936 |
lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING); |
| 1937 |
smc_accept_enqueue(&lsmc->sk, newsmcsk); |
| 1938 |
release_sock(&lsmc->sk); |
| 1939 |
} else { /* no longer listening */ |
| 1940 |
smc_close_non_accepted(newsmcsk); |
| 1941 |
} |
| 1942 |
|
| 1943 |
/* Wake up accept */ |
| 1944 |
lsmc->sk.sk_data_ready(&lsmc->sk); |
| 1945 |
sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */ |
| 1946 |
} |
| 1947 |
|
| 1948 |
/* listen worker: finish in state connected */ |
| 1949 |
static void smc_listen_out_connected(struct smc_sock *new_smc) |
| 1950 |
{ |
| 1951 |
struct sock *newsmcsk = &new_smc->sk; |
| 1952 |
|
| 1953 |
if (newsmcsk->sk_state == SMC_INIT) |
| 1954 |
newsmcsk->sk_state = SMC_ACTIVE; |
| 1955 |
|
| 1956 |
smc_listen_out(new_smc); |
| 1957 |
} |
| 1958 |
|
| 1959 |
/* listen worker: finish in error state */ |
| 1960 |
static void smc_listen_out_err(struct smc_sock *new_smc) |
| 1961 |
{ |
| 1962 |
struct sock *newsmcsk = &new_smc->sk; |
| 1963 |
struct net *net = sock_net(newsmcsk); |
| 1964 |
|
| 1965 |
this_cpu_inc(net->smc.smc_stats->srv_hshake_err_cnt); |
| 1966 |
if (newsmcsk->sk_state == SMC_INIT) |
| 1967 |
sock_put(&new_smc->sk); /* passive closing */ |
| 1968 |
newsmcsk->sk_state = SMC_CLOSED; |
| 1969 |
|
| 1970 |
smc_listen_out(new_smc); |
| 1971 |
} |
| 1972 |
|
| 1973 |
/* listen worker: decline and fall back if possible */ |
| 1974 |
static void smc_listen_decline(struct smc_sock *new_smc, int reason_code, |
| 1975 |
int local_first, u8 version) |
| 1976 |
{ |
| 1977 |
/* RDMA setup failed, switch back to TCP */ |
| 1978 |
smc_conn_abort(new_smc, local_first); |
| 1979 |
if (reason_code < 0 || |
| 1980 |
smc_switch_to_fallback(new_smc, reason_code)) { |
| 1981 |
/* error, no fallback possible */ |
| 1982 |
smc_listen_out_err(new_smc); |
| 1983 |
return; |
| 1984 |
} |
| 1985 |
if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) { |
| 1986 |
if (smc_clc_send_decline(new_smc, reason_code, version) < 0) { |
| 1987 |
smc_listen_out_err(new_smc); |
| 1988 |
return; |
| 1989 |
} |
| 1990 |
} |
| 1991 |
smc_listen_out_connected(new_smc); |
| 1992 |
} |
| 1993 |
|
| 1994 |
/* listen worker: version checking */ |
| 1995 |
static int smc_listen_v2_check(struct smc_sock *new_smc, |
| 1996 |
struct smc_clc_msg_proposal *pclc, |
| 1997 |
struct smc_init_info *ini) |
| 1998 |
{ |
| 1999 |
struct smc_clc_smcd_v2_extension *pclc_smcd_v2_ext; |
| 2000 |
struct smc_clc_v2_extension *pclc_v2_ext; |
| 2001 |
int rc = SMC_CLC_DECL_PEERNOSMC; |
| 2002 |
|
| 2003 |
ini->smc_type_v1 = pclc->hdr.typev1; |
| 2004 |
ini->smc_type_v2 = pclc->hdr.typev2; |
| 2005 |
ini->smcd_version = smcd_indicated(ini->smc_type_v1) ? SMC_V1 : 0; |
| 2006 |
ini->smcr_version = smcr_indicated(ini->smc_type_v1) ? SMC_V1 : 0; |
| 2007 |
if (pclc->hdr.version > SMC_V1) { |
| 2008 |
if (smcd_indicated(ini->smc_type_v2)) |
| 2009 |
ini->smcd_version |= SMC_V2; |
| 2010 |
if (smcr_indicated(ini->smc_type_v2)) |
| 2011 |
ini->smcr_version |= SMC_V2; |
| 2012 |
} |
| 2013 |
if (!(ini->smcd_version & SMC_V2) && !(ini->smcr_version & SMC_V2)) { |
| 2014 |
rc = SMC_CLC_DECL_PEERNOSMC; |
| 2015 |
goto out; |
| 2016 |
} |
| 2017 |
pclc_v2_ext = smc_get_clc_v2_ext(pclc); |
| 2018 |
if (!pclc_v2_ext) { |
| 2019 |
ini->smcd_version &= ~SMC_V2; |
| 2020 |
ini->smcr_version &= ~SMC_V2; |
| 2021 |
rc = SMC_CLC_DECL_NOV2EXT; |
| 2022 |
goto out; |
| 2023 |
} |
| 2024 |
pclc_smcd_v2_ext = smc_get_clc_smcd_v2_ext(pclc_v2_ext); |
| 2025 |
if (ini->smcd_version & SMC_V2) { |
| 2026 |
if (!smc_ism_is_v2_capable()) { |
| 2027 |
ini->smcd_version &= ~SMC_V2; |
| 2028 |
rc = SMC_CLC_DECL_NOISM2SUPP; |
| 2029 |
} else if (!pclc_smcd_v2_ext) { |
| 2030 |
ini->smcd_version &= ~SMC_V2; |
| 2031 |
rc = SMC_CLC_DECL_NOV2DEXT; |
| 2032 |
} else if (!pclc_v2_ext->hdr.eid_cnt && |
| 2033 |
!pclc_v2_ext->hdr.flag.seid) { |
| 2034 |
ini->smcd_version &= ~SMC_V2; |
| 2035 |
rc = SMC_CLC_DECL_NOUEID; |
| 2036 |
} |
| 2037 |
} |
| 2038 |
if (ini->smcr_version & SMC_V2) { |
| 2039 |
if (!pclc_v2_ext->hdr.eid_cnt) { |
| 2040 |
ini->smcr_version &= ~SMC_V2; |
| 2041 |
rc = SMC_CLC_DECL_NOUEID; |
| 2042 |
} |
| 2043 |
} |
| 2044 |
|
| 2045 |
ini->release_nr = pclc_v2_ext->hdr.flag.release; |
| 2046 |
if (pclc_v2_ext->hdr.flag.release > SMC_RELEASE) |
| 2047 |
ini->release_nr = SMC_RELEASE; |
| 2048 |
|
| 2049 |
out: |
| 2050 |
if (!ini->smcd_version && !ini->smcr_version) |
| 2051 |
return rc; |
| 2052 |
|
| 2053 |
return 0; |
| 2054 |
} |
| 2055 |
|
| 2056 |
/* listen worker: check prefixes */ |
| 2057 |
static int smc_listen_prfx_check(struct smc_sock *new_smc, |
| 2058 |
struct smc_clc_msg_proposal *pclc) |
| 2059 |
{ |
| 2060 |
struct smc_clc_msg_proposal_prefix *pclc_prfx; |
| 2061 |
struct socket *newclcsock = new_smc->clcsock; |
| 2062 |
|
| 2063 |
if (pclc->hdr.typev1 == SMC_TYPE_N) |
| 2064 |
return 0; |
| 2065 |
pclc_prfx = smc_clc_proposal_get_prefix(pclc); |
| 2066 |
if (!pclc_prfx) |
| 2067 |
return -EPROTO; |
| 2068 |
if (smc_clc_prfx_match(newclcsock, pclc_prfx)) |
| 2069 |
return SMC_CLC_DECL_DIFFPREFIX; |
| 2070 |
|
| 2071 |
return 0; |
| 2072 |
} |
| 2073 |
|
| 2074 |
/* listen worker: initialize connection and buffers */ |
| 2075 |
static int smc_listen_rdma_init(struct smc_sock *new_smc, |
| 2076 |
struct smc_init_info *ini) |
| 2077 |
{ |
| 2078 |
int rc; |
| 2079 |
|
| 2080 |
/* allocate connection / link group */ |
| 2081 |
rc = smc_conn_create(new_smc, ini); |
| 2082 |
if (rc) |
| 2083 |
return rc; |
| 2084 |
|
| 2085 |
/* create send buffer and rmb */ |
| 2086 |
if (smc_buf_create(new_smc, false)) { |
| 2087 |
smc_conn_abort(new_smc, ini->first_contact_local); |
| 2088 |
return SMC_CLC_DECL_MEM; |
| 2089 |
} |
| 2090 |
|
| 2091 |
return 0; |
| 2092 |
} |
| 2093 |
|
| 2094 |
/* listen worker: initialize connection and buffers for SMC-D */ |
| 2095 |
static int smc_listen_ism_init(struct smc_sock *new_smc, |
| 2096 |
struct smc_init_info *ini) |
| 2097 |
{ |
| 2098 |
int rc; |
| 2099 |
|
| 2100 |
rc = smc_conn_create(new_smc, ini); |
| 2101 |
if (rc) |
| 2102 |
return rc; |
| 2103 |
|
| 2104 |
/* Create send and receive buffers */ |
| 2105 |
rc = smc_buf_create(new_smc, true); |
| 2106 |
if (rc) { |
| 2107 |
smc_conn_abort(new_smc, ini->first_contact_local); |
| 2108 |
return (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB : |
| 2109 |
SMC_CLC_DECL_MEM; |
| 2110 |
} |
| 2111 |
|
| 2112 |
return 0; |
| 2113 |
} |
| 2114 |
|
| 2115 |
static bool smc_is_already_selected(struct smcd_dev *smcd, |
| 2116 |
struct smc_init_info *ini, |
| 2117 |
int matches) |
| 2118 |
{ |
| 2119 |
int i; |
| 2120 |
|
| 2121 |
for (i = 0; i < matches; i++) |
| 2122 |
if (smcd == ini->ism_dev[i]) |
| 2123 |
return true; |
| 2124 |
|
| 2125 |
return false; |
| 2126 |
} |
| 2127 |
|
| 2128 |
/* check for ISM devices matching proposed ISM devices */ |
| 2129 |
static void smc_check_ism_v2_match(struct smc_init_info *ini, |
| 2130 |
u16 proposed_chid, |
| 2131 |
struct smcd_gid *proposed_gid, |
| 2132 |
unsigned int *matches) |
| 2133 |
{ |
| 2134 |
struct smcd_dev *smcd; |
| 2135 |
|
| 2136 |
list_for_each_entry(smcd, &smcd_dev_list.list, list) { |
| 2137 |
if (smcd->going_away) |
| 2138 |
continue; |
| 2139 |
if (smc_is_already_selected(smcd, ini, *matches)) |
| 2140 |
continue; |
| 2141 |
if (smc_ism_get_chid(smcd) == proposed_chid && |
| 2142 |
!smc_ism_cantalk(proposed_gid, ISM_RESERVED_VLANID, smcd)) { |
| 2143 |
ini->ism_peer_gid[*matches].gid = proposed_gid->gid; |
| 2144 |
if (__smc_ism_is_emulated(proposed_chid)) |
| 2145 |
ini->ism_peer_gid[*matches].gid_ext = |
| 2146 |
proposed_gid->gid_ext; |
| 2147 |
/* non-Emulated-ISM's peer gid_ext remains 0. */ |
| 2148 |
ini->ism_dev[*matches] = smcd; |
| 2149 |
(*matches)++; |
| 2150 |
break; |
| 2151 |
} |
| 2152 |
} |
| 2153 |
} |
| 2154 |
|
| 2155 |
static void smc_init_info_store_rc(u32 rc, struct smc_init_info *ini) |
| 2156 |
{ |
| 2157 |
if (!ini->rc) |
| 2158 |
ini->rc = rc; |
| 2159 |
} |
| 2160 |
|
| 2161 |
static void smc_find_ism_v2_device_serv(struct smc_sock *new_smc, |
| 2162 |
struct smc_clc_msg_proposal *pclc, |
| 2163 |
struct smc_init_info *ini) |
| 2164 |
{ |
| 2165 |
struct smc_clc_smcd_v2_extension *smcd_v2_ext; |
| 2166 |
struct smc_clc_v2_extension *smc_v2_ext; |
| 2167 |
struct smc_clc_msg_smcd *pclc_smcd; |
| 2168 |
unsigned int matches = 0; |
| 2169 |
struct smcd_gid smcd_gid; |
| 2170 |
u8 smcd_version; |
| 2171 |
u8 *eid = NULL; |
| 2172 |
int i, rc; |
| 2173 |
u16 chid; |
| 2174 |
|
| 2175 |
if (!(ini->smcd_version & SMC_V2) || !smcd_indicated(ini->smc_type_v2)) |
| 2176 |
goto not_found; |
| 2177 |
|
| 2178 |
pclc_smcd = smc_get_clc_msg_smcd(pclc); |
| 2179 |
smc_v2_ext = smc_get_clc_v2_ext(pclc); |
| 2180 |
smcd_v2_ext = smc_get_clc_smcd_v2_ext(smc_v2_ext); |
| 2181 |
if (!pclc_smcd || !smc_v2_ext || !smcd_v2_ext) |
| 2182 |
goto not_found; |
| 2183 |
|
| 2184 |
mutex_lock(&smcd_dev_list.mutex); |
| 2185 |
if (pclc_smcd->ism.chid) { |
| 2186 |
/* check for ISM device matching proposed native ISM device */ |
| 2187 |
smcd_gid.gid = ntohll(pclc_smcd->ism.gid); |
| 2188 |
smcd_gid.gid_ext = 0; |
| 2189 |
smc_check_ism_v2_match(ini, ntohs(pclc_smcd->ism.chid), |
| 2190 |
&smcd_gid, &matches); |
| 2191 |
} |
| 2192 |
for (i = 0; i < smc_v2_ext->hdr.ism_gid_cnt; i++) { |
| 2193 |
/* check for ISM devices matching proposed non-native ISM |
| 2194 |
* devices |
| 2195 |
*/ |
| 2196 |
smcd_gid.gid = ntohll(smcd_v2_ext->gidchid[i].gid); |
| 2197 |
smcd_gid.gid_ext = 0; |
| 2198 |
chid = ntohs(smcd_v2_ext->gidchid[i].chid); |
| 2199 |
if (__smc_ism_is_emulated(chid)) { |
| 2200 |
if ((i + 1) == smc_v2_ext->hdr.ism_gid_cnt || |
| 2201 |
chid != ntohs(smcd_v2_ext->gidchid[i + 1].chid)) |
| 2202 |
/* each Emulated-ISM device takes two GID-CHID |
| 2203 |
* entries and CHID of the second entry repeats |
| 2204 |
* that of the first entry. |
| 2205 |
* |
| 2206 |
* So check if the next GID-CHID entry exists |
| 2207 |
* and both two entries' CHIDs are the same. |
| 2208 |
*/ |
| 2209 |
continue; |
| 2210 |
smcd_gid.gid_ext = |
| 2211 |
ntohll(smcd_v2_ext->gidchid[++i].gid); |
| 2212 |
} |
| 2213 |
smc_check_ism_v2_match(ini, chid, &smcd_gid, &matches); |
| 2214 |
} |
| 2215 |
mutex_unlock(&smcd_dev_list.mutex); |
| 2216 |
|
| 2217 |
if (!ini->ism_dev[0]) { |
| 2218 |
smc_init_info_store_rc(SMC_CLC_DECL_NOSMCD2DEV, ini); |
| 2219 |
goto not_found; |
| 2220 |
} |
| 2221 |
|
| 2222 |
smc_ism_get_system_eid(&eid); |
| 2223 |
if (!smc_clc_match_eid(ini->negotiated_eid, smc_v2_ext, |
| 2224 |
smcd_v2_ext->system_eid, eid)) |
| 2225 |
goto not_found; |
| 2226 |
|
| 2227 |
/* separate - outside the smcd_dev_list.lock */ |
| 2228 |
smcd_version = ini->smcd_version; |
| 2229 |
for (i = 0; i < matches; i++) { |
| 2230 |
ini->smcd_version = SMC_V2; |
| 2231 |
ini->is_smcd = true; |
| 2232 |
ini->ism_selected = i; |
| 2233 |
rc = smc_listen_ism_init(new_smc, ini); |
| 2234 |
if (rc) { |
| 2235 |
smc_init_info_store_rc(rc, ini); |
| 2236 |
/* try next active ISM device */ |
| 2237 |
continue; |
| 2238 |
} |
| 2239 |
return; /* matching and usable V2 ISM device found */ |
| 2240 |
} |
| 2241 |
/* no V2 ISM device could be initialized */ |
| 2242 |
ini->smcd_version = smcd_version; /* restore original value */ |
| 2243 |
ini->negotiated_eid[0] = 0; |
| 2244 |
|
| 2245 |
not_found: |
| 2246 |
ini->smcd_version &= ~SMC_V2; |
| 2247 |
ini->ism_dev[0] = NULL; |
| 2248 |
ini->is_smcd = false; |
| 2249 |
} |
| 2250 |
|
| 2251 |
static void smc_find_ism_v1_device_serv(struct smc_sock *new_smc, |
| 2252 |
struct smc_clc_msg_proposal *pclc, |
| 2253 |
struct smc_init_info *ini) |
| 2254 |
{ |
| 2255 |
struct smc_clc_msg_smcd *pclc_smcd = smc_get_clc_msg_smcd(pclc); |
| 2256 |
int rc = 0; |
| 2257 |
|
| 2258 |
/* check if ISM V1 is available */ |
| 2259 |
if (!(ini->smcd_version & SMC_V1) || |
| 2260 |
!smcd_indicated(ini->smc_type_v1) || |
| 2261 |
!pclc_smcd) |
| 2262 |
goto not_found; |
| 2263 |
ini->is_smcd = true; /* prepare ISM check */ |
| 2264 |
ini->ism_peer_gid[0].gid = ntohll(pclc_smcd->ism.gid); |
| 2265 |
ini->ism_peer_gid[0].gid_ext = 0; |
| 2266 |
rc = smc_find_ism_device(new_smc, ini); |
| 2267 |
if (rc) |
| 2268 |
goto not_found; |
| 2269 |
ini->ism_selected = 0; |
| 2270 |
rc = smc_listen_ism_init(new_smc, ini); |
| 2271 |
if (!rc) |
| 2272 |
return; /* V1 ISM device found */ |
| 2273 |
|
| 2274 |
not_found: |
| 2275 |
smc_init_info_store_rc(rc, ini); |
| 2276 |
ini->smcd_version &= ~SMC_V1; |
| 2277 |
ini->ism_dev[0] = NULL; |
| 2278 |
ini->is_smcd = false; |
| 2279 |
} |
| 2280 |
|
| 2281 |
/* listen worker: register buffers */ |
| 2282 |
static int smc_listen_rdma_reg(struct smc_sock *new_smc, bool local_first) |
| 2283 |
{ |
| 2284 |
struct smc_connection *conn = &new_smc->conn; |
| 2285 |
|
| 2286 |
if (!local_first) { |
| 2287 |
/* reg sendbufs if they were vzalloced */ |
| 2288 |
if (conn->sndbuf_desc->is_vm) { |
| 2289 |
if (smcr_lgr_reg_sndbufs(conn->lnk, |
| 2290 |
conn->sndbuf_desc)) |
| 2291 |
return SMC_CLC_DECL_ERR_REGBUF; |
| 2292 |
} |
| 2293 |
if (smcr_lgr_reg_rmbs(conn->lnk, conn->rmb_desc)) |
| 2294 |
return SMC_CLC_DECL_ERR_REGBUF; |
| 2295 |
} |
| 2296 |
|
| 2297 |
return 0; |
| 2298 |
} |
| 2299 |
|
| 2300 |
static void smc_find_rdma_v2_device_serv(struct smc_sock *new_smc, |
| 2301 |
struct smc_clc_msg_proposal *pclc, |
| 2302 |
struct smc_init_info *ini) |
| 2303 |
{ |
| 2304 |
struct smc_clc_v2_extension *smc_v2_ext; |
| 2305 |
u8 smcr_version; |
| 2306 |
int rc; |
| 2307 |
|
| 2308 |
if (!(ini->smcr_version & SMC_V2) || !smcr_indicated(ini->smc_type_v2)) |
| 2309 |
goto not_found; |
| 2310 |
|
| 2311 |
smc_v2_ext = smc_get_clc_v2_ext(pclc); |
| 2312 |
if (!smc_v2_ext || |
| 2313 |
!smc_clc_match_eid(ini->negotiated_eid, smc_v2_ext, NULL, NULL)) |
| 2314 |
goto not_found; |
| 2315 |
|
| 2316 |
/* prepare RDMA check */ |
| 2317 |
memcpy(ini->peer_systemid, pclc->lcl.id_for_peer, SMC_SYSTEMID_LEN); |
| 2318 |
memcpy(ini->peer_gid, smc_v2_ext->roce, SMC_GID_SIZE); |
| 2319 |
memcpy(ini->peer_mac, pclc->lcl.mac, ETH_ALEN); |
| 2320 |
ini->check_smcrv2 = true; |
| 2321 |
ini->smcrv2.clc_sk = new_smc->clcsock->sk; |
| 2322 |
ini->smcrv2.saddr = new_smc->clcsock->sk->sk_rcv_saddr; |
| 2323 |
ini->smcrv2.daddr = smc_ib_gid_to_ipv4(smc_v2_ext->roce); |
| 2324 |
rc = smc_find_rdma_device(new_smc, ini); |
| 2325 |
if (rc) { |
| 2326 |
smc_init_info_store_rc(rc, ini); |
| 2327 |
goto not_found; |
| 2328 |
} |
| 2329 |
if (!ini->smcrv2.uses_gateway) |
| 2330 |
memcpy(ini->smcrv2.nexthop_mac, pclc->lcl.mac, ETH_ALEN); |
| 2331 |
|
| 2332 |
smcr_version = ini->smcr_version; |
| 2333 |
ini->smcr_version = SMC_V2; |
| 2334 |
rc = smc_listen_rdma_init(new_smc, ini); |
| 2335 |
if (!rc) { |
| 2336 |
rc = smc_listen_rdma_reg(new_smc, ini->first_contact_local); |
| 2337 |
if (rc) |
| 2338 |
smc_conn_abort(new_smc, ini->first_contact_local); |
| 2339 |
} |
| 2340 |
if (!rc) |
| 2341 |
return; |
| 2342 |
ini->smcr_version = smcr_version; |
| 2343 |
smc_init_info_store_rc(rc, ini); |
| 2344 |
|
| 2345 |
not_found: |
| 2346 |
ini->smcr_version &= ~SMC_V2; |
| 2347 |
ini->smcrv2.ib_dev_v2 = NULL; |
| 2348 |
ini->check_smcrv2 = false; |
| 2349 |
} |
| 2350 |
|
| 2351 |
static int smc_find_rdma_v1_device_serv(struct smc_sock *new_smc, |
| 2352 |
struct smc_clc_msg_proposal *pclc, |
| 2353 |
struct smc_init_info *ini) |
| 2354 |
{ |
| 2355 |
int rc; |
| 2356 |
|
| 2357 |
if (!(ini->smcr_version & SMC_V1) || !smcr_indicated(ini->smc_type_v1)) |
| 2358 |
return SMC_CLC_DECL_NOSMCDEV; |
| 2359 |
|
| 2360 |
/* prepare RDMA check */ |
| 2361 |
memcpy(ini->peer_systemid, pclc->lcl.id_for_peer, SMC_SYSTEMID_LEN); |
| 2362 |
memcpy(ini->peer_gid, pclc->lcl.gid, SMC_GID_SIZE); |
| 2363 |
memcpy(ini->peer_mac, pclc->lcl.mac, ETH_ALEN); |
| 2364 |
rc = smc_find_rdma_device(new_smc, ini); |
| 2365 |
if (rc) { |
| 2366 |
/* no RDMA device found */ |
| 2367 |
return SMC_CLC_DECL_NOSMCDEV; |
| 2368 |
} |
| 2369 |
rc = smc_listen_rdma_init(new_smc, ini); |
| 2370 |
if (rc) |
| 2371 |
return rc; |
| 2372 |
return smc_listen_rdma_reg(new_smc, ini->first_contact_local); |
| 2373 |
} |
| 2374 |
|
| 2375 |
/* determine the local device matching to proposal */ |
| 2376 |
static int smc_listen_find_device(struct smc_sock *new_smc, |
| 2377 |
struct smc_clc_msg_proposal *pclc, |
| 2378 |
struct smc_init_info *ini) |
| 2379 |
{ |
| 2380 |
int prfx_rc; |
| 2381 |
|
| 2382 |
/* check for ISM device matching V2 proposed device */ |
| 2383 |
smc_find_ism_v2_device_serv(new_smc, pclc, ini); |
| 2384 |
if (ini->ism_dev[0]) |
| 2385 |
return 0; |
| 2386 |
|
| 2387 |
/* check for matching IP prefix and subnet length (V1) */ |
| 2388 |
prfx_rc = smc_listen_prfx_check(new_smc, pclc); |
| 2389 |
if (prfx_rc) |
| 2390 |
smc_init_info_store_rc(prfx_rc, ini); |
| 2391 |
|
| 2392 |
/* get vlan id from IP device */ |
| 2393 |
if (smc_vlan_by_tcpsk(new_smc->clcsock, ini)) |
| 2394 |
return ini->rc ?: SMC_CLC_DECL_GETVLANERR; |
| 2395 |
|
| 2396 |
/* check for ISM device matching V1 proposed device */ |
| 2397 |
if (!prfx_rc) |
| 2398 |
smc_find_ism_v1_device_serv(new_smc, pclc, ini); |
| 2399 |
if (ini->ism_dev[0]) |
| 2400 |
return 0; |
| 2401 |
|
| 2402 |
if (!smcr_indicated(pclc->hdr.typev1) && |
| 2403 |
!smcr_indicated(pclc->hdr.typev2)) |
| 2404 |
/* skip RDMA and decline */ |
| 2405 |
return ini->rc ?: SMC_CLC_DECL_NOSMCDDEV; |
| 2406 |
|
| 2407 |
/* check if RDMA V2 is available */ |
| 2408 |
smc_find_rdma_v2_device_serv(new_smc, pclc, ini); |
| 2409 |
if (ini->smcrv2.ib_dev_v2) |
| 2410 |
return 0; |
| 2411 |
|
| 2412 |
/* check if RDMA V1 is available */ |
| 2413 |
if (!prfx_rc) { |
| 2414 |
int rc; |
| 2415 |
|
| 2416 |
rc = smc_find_rdma_v1_device_serv(new_smc, pclc, ini); |
| 2417 |
smc_init_info_store_rc(rc, ini); |
| 2418 |
return (!rc) ? 0 : ini->rc; |
| 2419 |
} |
| 2420 |
return prfx_rc; |
| 2421 |
} |
| 2422 |
|
| 2423 |
/* listen worker: finish RDMA setup */ |
| 2424 |
static int smc_listen_rdma_finish(struct smc_sock *new_smc, |
| 2425 |
struct smc_clc_msg_accept_confirm *cclc, |
| 2426 |
bool local_first, |
| 2427 |
struct smc_init_info *ini) |
| 2428 |
{ |
| 2429 |
struct smc_link *link = new_smc->conn.lnk; |
| 2430 |
int reason_code = 0; |
| 2431 |
|
| 2432 |
if (local_first) |
| 2433 |
smc_link_save_peer_info(link, cclc, ini); |
| 2434 |
|
| 2435 |
if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc)) |
| 2436 |
return SMC_CLC_DECL_ERR_RTOK; |
| 2437 |
|
| 2438 |
if (local_first) { |
| 2439 |
if (smc_ib_ready_link(link)) |
| 2440 |
return SMC_CLC_DECL_ERR_RDYLNK; |
| 2441 |
/* QP confirmation over RoCE fabric */ |
| 2442 |
smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK); |
| 2443 |
reason_code = smcr_serv_conf_first_link(new_smc); |
| 2444 |
smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl); |
| 2445 |
} |
| 2446 |
return reason_code; |
| 2447 |
} |
| 2448 |
|
| 2449 |
/* setup for connection of server */ |
| 2450 |
static void smc_listen_work(struct work_struct *work) |
| 2451 |
{ |
| 2452 |
struct smc_sock *new_smc = container_of(work, struct smc_sock, |
| 2453 |
smc_listen_work); |
| 2454 |
struct socket *newclcsock = new_smc->clcsock; |
| 2455 |
struct smc_clc_msg_accept_confirm *cclc; |
| 2456 |
struct smc_clc_msg_proposal_area *buf; |
| 2457 |
struct smc_clc_msg_proposal *pclc; |
| 2458 |
struct smc_init_info *ini = NULL; |
| 2459 |
u8 proposal_version = SMC_V1; |
| 2460 |
u8 accept_version; |
| 2461 |
int rc = 0; |
| 2462 |
|
| 2463 |
lock_sock(&new_smc->sk); /* release in smc_listen_out() */ |
| 2464 |
if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN) |
| 2465 |
return smc_listen_out_err(new_smc); |
| 2466 |
|
| 2467 |
if (new_smc->use_fallback) { |
| 2468 |
smc_listen_out_connected(new_smc); |
| 2469 |
return; |
| 2470 |
} |
| 2471 |
|
| 2472 |
/* check if peer is smc capable */ |
| 2473 |
if (!tcp_sk(newclcsock->sk)->syn_smc) { |
| 2474 |
rc = smc_switch_to_fallback(new_smc, SMC_CLC_DECL_PEERNOSMC); |
| 2475 |
if (rc) |
| 2476 |
smc_listen_out_err(new_smc); |
| 2477 |
else |
| 2478 |
smc_listen_out_connected(new_smc); |
| 2479 |
return; |
| 2480 |
} |
| 2481 |
|
| 2482 |
/* do inband token exchange - |
| 2483 |
* wait for and receive SMC Proposal CLC message |
| 2484 |
*/ |
| 2485 |
buf = kzalloc_obj(*buf); |
| 2486 |
if (!buf) { |
| 2487 |
rc = SMC_CLC_DECL_MEM; |
| 2488 |
goto out_decl; |
| 2489 |
} |
| 2490 |
pclc = (struct smc_clc_msg_proposal *)buf; |
| 2491 |
rc = smc_clc_wait_msg(new_smc, pclc, sizeof(*buf), |
| 2492 |
SMC_CLC_PROPOSAL, CLC_WAIT_TIME); |
| 2493 |
if (rc) |
| 2494 |
goto out_decl; |
| 2495 |
|
| 2496 |
if (pclc->hdr.version > SMC_V1) |
| 2497 |
proposal_version = SMC_V2; |
| 2498 |
|
| 2499 |
/* IPSec connections opt out of SMC optimizations */ |
| 2500 |
if (using_ipsec(new_smc)) { |
| 2501 |
rc = SMC_CLC_DECL_IPSEC; |
| 2502 |
goto out_decl; |
| 2503 |
} |
| 2504 |
|
| 2505 |
ini = kzalloc_obj(*ini); |
| 2506 |
if (!ini) { |
| 2507 |
rc = SMC_CLC_DECL_MEM; |
| 2508 |
goto out_decl; |
| 2509 |
} |
| 2510 |
|
| 2511 |
/* initial version checking */ |
| 2512 |
rc = smc_listen_v2_check(new_smc, pclc, ini); |
| 2513 |
if (rc) |
| 2514 |
goto out_decl; |
| 2515 |
|
| 2516 |
rc = smc_clc_srv_v2x_features_validate(new_smc, pclc, ini); |
| 2517 |
if (rc) |
| 2518 |
goto out_decl; |
| 2519 |
|
| 2520 |
mutex_lock(&smc_server_lgr_pending); |
| 2521 |
smc_rx_init(new_smc); |
| 2522 |
smc_tx_init(new_smc); |
| 2523 |
|
| 2524 |
/* determine ISM or RoCE device used for connection */ |
| 2525 |
rc = smc_listen_find_device(new_smc, pclc, ini); |
| 2526 |
if (rc) |
| 2527 |
goto out_unlock; |
| 2528 |
|
| 2529 |
/* send SMC Accept CLC message */ |
| 2530 |
accept_version = ini->is_smcd ? ini->smcd_version : ini->smcr_version; |
| 2531 |
rc = smc_clc_send_accept(new_smc, ini->first_contact_local, |
| 2532 |
accept_version, ini->negotiated_eid, ini); |
| 2533 |
if (rc) |
| 2534 |
goto out_unlock; |
| 2535 |
|
| 2536 |
/* SMC-D does not need this lock any more */ |
| 2537 |
if (ini->is_smcd) |
| 2538 |
mutex_unlock(&smc_server_lgr_pending); |
| 2539 |
|
| 2540 |
/* receive SMC Confirm CLC message */ |
| 2541 |
memset(buf, 0, sizeof(*buf)); |
| 2542 |
cclc = (struct smc_clc_msg_accept_confirm *)buf; |
| 2543 |
rc = smc_clc_wait_msg(new_smc, cclc, sizeof(*buf), |
| 2544 |
SMC_CLC_CONFIRM, CLC_WAIT_TIME); |
| 2545 |
if (rc) { |
| 2546 |
if (!ini->is_smcd) |
| 2547 |
goto out_unlock; |
| 2548 |
goto out_decl; |
| 2549 |
} |
| 2550 |
|
| 2551 |
rc = smc_clc_v2x_features_confirm_check(cclc, ini); |
| 2552 |
if (rc) { |
| 2553 |
if (!ini->is_smcd) |
| 2554 |
goto out_unlock; |
| 2555 |
goto out_decl; |
| 2556 |
} |
| 2557 |
|
| 2558 |
/* fce smc release version is needed in smc_listen_rdma_finish, |
| 2559 |
* so save fce info here. |
| 2560 |
*/ |
| 2561 |
smc_conn_save_peer_info_fce(new_smc, cclc); |
| 2562 |
|
| 2563 |
/* finish worker */ |
| 2564 |
if (!ini->is_smcd) { |
| 2565 |
rc = smc_listen_rdma_finish(new_smc, cclc, |
| 2566 |
ini->first_contact_local, ini); |
| 2567 |
if (rc) |
| 2568 |
goto out_unlock; |
| 2569 |
mutex_unlock(&smc_server_lgr_pending); |
| 2570 |
} |
| 2571 |
smc_conn_save_peer_info(new_smc, cclc); |
| 2572 |
|
| 2573 |
if (ini->is_smcd && |
| 2574 |
smc_ism_support_dmb_nocopy(new_smc->conn.lgr->smcd)) { |
| 2575 |
rc = smcd_buf_attach(new_smc); |
| 2576 |
if (rc) |
| 2577 |
goto out_decl; |
| 2578 |
} |
| 2579 |
|
| 2580 |
SMC_STAT_SERV_SUCC_INC(sock_net(newclcsock->sk), ini); |
| 2581 |
/* smc_listen_out() will release smcsk */ |
| 2582 |
smc_listen_out_connected(new_smc); |
| 2583 |
goto out_free; |
| 2584 |
|
| 2585 |
out_unlock: |
| 2586 |
mutex_unlock(&smc_server_lgr_pending); |
| 2587 |
out_decl: |
| 2588 |
smc_listen_decline(new_smc, rc, ini ? ini->first_contact_local : 0, |
| 2589 |
proposal_version); |
| 2590 |
out_free: |
| 2591 |
kfree(ini); |
| 2592 |
kfree(buf); |
| 2593 |
} |
| 2594 |
|
| 2595 |
static void smc_tcp_listen_work(struct work_struct *work) |
| 2596 |
{ |
| 2597 |
struct smc_sock *lsmc = container_of(work, struct smc_sock, |
| 2598 |
tcp_listen_work); |
| 2599 |
struct sock *lsk = &lsmc->sk; |
| 2600 |
struct smc_sock *new_smc; |
| 2601 |
int rc = 0; |
| 2602 |
|
| 2603 |
lock_sock(lsk); |
| 2604 |
while (lsk->sk_state == SMC_LISTEN) { |
| 2605 |
rc = smc_clcsock_accept(lsmc, &new_smc); |
| 2606 |
if (rc) /* clcsock accept queue empty or error */ |
| 2607 |
goto out; |
| 2608 |
if (!new_smc) |
| 2609 |
continue; |
| 2610 |
|
| 2611 |
if (tcp_sk(new_smc->clcsock->sk)->syn_smc) |
| 2612 |
atomic_inc(&lsmc->queued_smc_hs); |
| 2613 |
|
| 2614 |
new_smc->listen_smc = lsmc; |
| 2615 |
new_smc->use_fallback = lsmc->use_fallback; |
| 2616 |
new_smc->fallback_rsn = lsmc->fallback_rsn; |
| 2617 |
sock_hold(lsk); /* sock_put in smc_listen_work */ |
| 2618 |
INIT_WORK(&new_smc->smc_listen_work, smc_listen_work); |
| 2619 |
smc_copy_sock_settings_to_smc(new_smc); |
| 2620 |
sock_hold(&new_smc->sk); /* sock_put in passive closing */ |
| 2621 |
if (!queue_work(smc_hs_wq, &new_smc->smc_listen_work)) |
| 2622 |
sock_put(&new_smc->sk); |
| 2623 |
} |
| 2624 |
|
| 2625 |
out: |
| 2626 |
release_sock(lsk); |
| 2627 |
sock_put(&lsmc->sk); /* sock_hold in smc_clcsock_data_ready() */ |
| 2628 |
} |
| 2629 |
|
| 2630 |
static void smc_clcsock_data_ready(struct sock *listen_clcsock) |
| 2631 |
{ |
| 2632 |
struct smc_sock *lsmc; |
| 2633 |
|
| 2634 |
read_lock_bh(&listen_clcsock->sk_callback_lock); |
| 2635 |
lsmc = smc_clcsock_user_data(listen_clcsock); |
| 2636 |
if (!lsmc) |
| 2637 |
goto out; |
| 2638 |
lsmc->clcsk_data_ready(listen_clcsock); |
| 2639 |
if (lsmc->sk.sk_state == SMC_LISTEN) { |
| 2640 |
sock_hold(&lsmc->sk); /* sock_put in smc_tcp_listen_work() */ |
| 2641 |
if (!queue_work(smc_tcp_ls_wq, &lsmc->tcp_listen_work)) |
| 2642 |
sock_put(&lsmc->sk); |
| 2643 |
} |
| 2644 |
out: |
| 2645 |
read_unlock_bh(&listen_clcsock->sk_callback_lock); |
| 2646 |
} |
| 2647 |
|
| 2648 |
int smc_listen(struct socket *sock, int backlog) |
| 2649 |
{ |
| 2650 |
struct sock *sk = sock->sk; |
| 2651 |
struct smc_sock *smc; |
| 2652 |
int rc; |
| 2653 |
|
| 2654 |
smc = smc_sk(sk); |
| 2655 |
lock_sock(sk); |
| 2656 |
|
| 2657 |
rc = -EINVAL; |
| 2658 |
if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) || |
| 2659 |
smc->connect_nonblock || sock->state != SS_UNCONNECTED) |
| 2660 |
goto out; |
| 2661 |
|
| 2662 |
rc = 0; |
| 2663 |
if (sk->sk_state == SMC_LISTEN) { |
| 2664 |
sk->sk_max_ack_backlog = backlog; |
| 2665 |
goto out; |
| 2666 |
} |
| 2667 |
/* some socket options are handled in core, so we could not apply |
| 2668 |
* them to the clc socket -- copy smc socket options to clc socket |
| 2669 |
*/ |
| 2670 |
smc_copy_sock_settings_to_clc(smc); |
| 2671 |
if (!smc->use_fallback) |
| 2672 |
tcp_sk(smc->clcsock->sk)->syn_smc = 1; |
| 2673 |
|
| 2674 |
/* save original sk_data_ready function and establish |
| 2675 |
* smc-specific sk_data_ready function |
| 2676 |
*/ |
| 2677 |
write_lock_bh(&smc->clcsock->sk->sk_callback_lock); |
| 2678 |
__rcu_assign_sk_user_data_with_flags(smc->clcsock->sk, smc, |
| 2679 |
SK_USER_DATA_NOCOPY); |
| 2680 |
smc_clcsock_replace_cb(&smc->clcsock->sk->sk_data_ready, |
| 2681 |
smc_clcsock_data_ready, &smc->clcsk_data_ready); |
| 2682 |
write_unlock_bh(&smc->clcsock->sk->sk_callback_lock); |
| 2683 |
|
| 2684 |
/* save original ops */ |
| 2685 |
smc->ori_af_ops = inet_csk(smc->clcsock->sk)->icsk_af_ops; |
| 2686 |
|
| 2687 |
smc->af_ops = *smc->ori_af_ops; |
| 2688 |
smc->af_ops.syn_recv_sock = smc_tcp_syn_recv_sock; |
| 2689 |
|
| 2690 |
inet_csk(smc->clcsock->sk)->icsk_af_ops = &smc->af_ops; |
| 2691 |
|
| 2692 |
if (smc->limit_smc_hs) |
| 2693 |
tcp_sk(smc->clcsock->sk)->smc_hs_congested = smc_hs_congested; |
| 2694 |
|
| 2695 |
rc = kernel_listen(smc->clcsock, backlog); |
| 2696 |
if (rc) { |
| 2697 |
write_lock_bh(&smc->clcsock->sk->sk_callback_lock); |
| 2698 |
smc_clcsock_restore_cb(&smc->clcsock->sk->sk_data_ready, |
| 2699 |
&smc->clcsk_data_ready); |
| 2700 |
rcu_assign_sk_user_data(smc->clcsock->sk, NULL); |
| 2701 |
write_unlock_bh(&smc->clcsock->sk->sk_callback_lock); |
| 2702 |
goto out; |
| 2703 |
} |
| 2704 |
sock_set_flag(sk, SOCK_RCU_FREE); |
| 2705 |
sk->sk_max_ack_backlog = backlog; |
| 2706 |
sk->sk_ack_backlog = 0; |
| 2707 |
sk->sk_state = SMC_LISTEN; |
| 2708 |
|
| 2709 |
out: |
| 2710 |
release_sock(sk); |
| 2711 |
return rc; |
| 2712 |
} |
| 2713 |
|
| 2714 |
int smc_accept(struct socket *sock, struct socket *new_sock, |
| 2715 |
struct proto_accept_arg *arg) |
| 2716 |
{ |
| 2717 |
struct sock *sk = sock->sk, *nsk; |
| 2718 |
DECLARE_WAITQUEUE(wait, current); |
| 2719 |
struct smc_sock *lsmc; |
| 2720 |
long timeo; |
| 2721 |
int rc = 0; |
| 2722 |
|
| 2723 |
lsmc = smc_sk(sk); |
| 2724 |
sock_hold(sk); /* sock_put below */ |
| 2725 |
lock_sock(sk); |
| 2726 |
|
| 2727 |
if (lsmc->sk.sk_state != SMC_LISTEN) { |
| 2728 |
rc = -EINVAL; |
| 2729 |
release_sock(sk); |
| 2730 |
goto out; |
| 2731 |
} |
| 2732 |
|
| 2733 |
/* Wait for an incoming connection */ |
| 2734 |
timeo = sock_rcvtimeo(sk, arg->flags & O_NONBLOCK); |
| 2735 |
add_wait_queue_exclusive(sk_sleep(sk), &wait); |
| 2736 |
while (!(nsk = smc_accept_dequeue(sk, new_sock))) { |
| 2737 |
set_current_state(TASK_INTERRUPTIBLE); |
| 2738 |
if (!timeo) { |
| 2739 |
rc = -EAGAIN; |
| 2740 |
break; |
| 2741 |
} |
| 2742 |
release_sock(sk); |
| 2743 |
timeo = schedule_timeout(timeo); |
| 2744 |
/* wakeup by sk_data_ready in smc_listen_work() */ |
| 2745 |
sched_annotate_sleep(); |
| 2746 |
lock_sock(sk); |
| 2747 |
if (signal_pending(current)) { |
| 2748 |
rc = sock_intr_errno(timeo); |
| 2749 |
break; |
| 2750 |
} |
| 2751 |
} |
| 2752 |
set_current_state(TASK_RUNNING); |
| 2753 |
remove_wait_queue(sk_sleep(sk), &wait); |
| 2754 |
|
| 2755 |
if (!rc) |
| 2756 |
rc = sock_error(nsk); |
| 2757 |
release_sock(sk); |
| 2758 |
if (rc) |
| 2759 |
goto out; |
| 2760 |
|
| 2761 |
if (lsmc->sockopt_defer_accept && !(arg->flags & O_NONBLOCK)) { |
| 2762 |
/* wait till data arrives on the socket */ |
| 2763 |
timeo = secs_to_jiffies(lsmc->sockopt_defer_accept); |
| 2764 |
if (smc_sk(nsk)->use_fallback) { |
| 2765 |
struct sock *clcsk = smc_sk(nsk)->clcsock->sk; |
| 2766 |
|
| 2767 |
lock_sock(clcsk); |
| 2768 |
if (skb_queue_empty(&clcsk->sk_receive_queue)) |
| 2769 |
sk_wait_data(clcsk, &timeo, NULL); |
| 2770 |
release_sock(clcsk); |
| 2771 |
} else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) { |
| 2772 |
lock_sock(nsk); |
| 2773 |
smc_rx_wait(smc_sk(nsk), &timeo, 0, smc_rx_data_available); |
| 2774 |
release_sock(nsk); |
| 2775 |
} |
| 2776 |
} |
| 2777 |
|
| 2778 |
out: |
| 2779 |
sock_put(sk); /* sock_hold above */ |
| 2780 |
return rc; |
| 2781 |
} |
| 2782 |
|
| 2783 |
int smc_getname(struct socket *sock, struct sockaddr *addr, |
| 2784 |
int peer) |
| 2785 |
{ |
| 2786 |
struct smc_sock *smc; |
| 2787 |
|
| 2788 |
if (peer && (sock->sk->sk_state != SMC_ACTIVE) && |
| 2789 |
(sock->sk->sk_state != SMC_APPCLOSEWAIT1)) |
| 2790 |
return -ENOTCONN; |
| 2791 |
|
| 2792 |
smc = smc_sk(sock->sk); |
| 2793 |
|
| 2794 |
return smc->clcsock->ops->getname(smc->clcsock, addr, peer); |
| 2795 |
} |
| 2796 |
|
| 2797 |
int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) |
| 2798 |
{ |
| 2799 |
struct sock *sk = sock->sk; |
| 2800 |
struct smc_sock *smc; |
| 2801 |
int rc; |
| 2802 |
|
| 2803 |
smc = smc_sk(sk); |
| 2804 |
lock_sock(sk); |
| 2805 |
|
| 2806 |
/* SMC does not support connect with fastopen */ |
| 2807 |
if (msg->msg_flags & MSG_FASTOPEN) { |
| 2808 |
/* not connected yet, fallback */ |
| 2809 |
if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) { |
| 2810 |
rc = smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP); |
| 2811 |
if (rc) |
| 2812 |
goto out; |
| 2813 |
} else { |
| 2814 |
rc = -EINVAL; |
| 2815 |
goto out; |
| 2816 |
} |
| 2817 |
} else if ((sk->sk_state != SMC_ACTIVE) && |
| 2818 |
(sk->sk_state != SMC_APPCLOSEWAIT1) && |
| 2819 |
(sk->sk_state != SMC_INIT)) { |
| 2820 |
rc = -EPIPE; |
| 2821 |
goto out; |
| 2822 |
} |
| 2823 |
|
| 2824 |
if (smc->use_fallback) { |
| 2825 |
rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len); |
| 2826 |
} else { |
| 2827 |
rc = smc_tx_sendmsg(smc, msg, len); |
| 2828 |
SMC_STAT_TX_PAYLOAD(smc, len, rc); |
| 2829 |
} |
| 2830 |
out: |
| 2831 |
release_sock(sk); |
| 2832 |
return rc; |
| 2833 |
} |
| 2834 |
|
| 2835 |
int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, |
| 2836 |
int flags) |
| 2837 |
{ |
| 2838 |
struct sock *sk = sock->sk; |
| 2839 |
struct smc_sock *smc; |
| 2840 |
int rc = -ENOTCONN; |
| 2841 |
|
| 2842 |
smc = smc_sk(sk); |
| 2843 |
lock_sock(sk); |
| 2844 |
if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) { |
| 2845 |
/* socket was connected before, no more data to read */ |
| 2846 |
rc = 0; |
| 2847 |
goto out; |
| 2848 |
} |
| 2849 |
if ((sk->sk_state == SMC_INIT) || |
| 2850 |
(sk->sk_state == SMC_LISTEN) || |
| 2851 |
(sk->sk_state == SMC_CLOSED)) |
| 2852 |
goto out; |
| 2853 |
|
| 2854 |
if (sk->sk_state == SMC_PEERFINCLOSEWAIT) { |
| 2855 |
rc = 0; |
| 2856 |
goto out; |
| 2857 |
} |
| 2858 |
|
| 2859 |
if (smc->use_fallback) { |
| 2860 |
rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags); |
| 2861 |
} else { |
| 2862 |
msg->msg_namelen = 0; |
| 2863 |
rc = smc_rx_recvmsg(smc, msg, NULL, len, flags); |
| 2864 |
SMC_STAT_RX_PAYLOAD(smc, rc, rc); |
| 2865 |
} |
| 2866 |
|
| 2867 |
out: |
| 2868 |
release_sock(sk); |
| 2869 |
return rc; |
| 2870 |
} |
| 2871 |
|
| 2872 |
static __poll_t smc_accept_poll(struct sock *parent) |
| 2873 |
{ |
| 2874 |
struct smc_sock *isk = smc_sk(parent); |
| 2875 |
__poll_t mask = 0; |
| 2876 |
|
| 2877 |
spin_lock(&isk->accept_q_lock); |
| 2878 |
if (!list_empty(&isk->accept_q)) |
| 2879 |
mask = EPOLLIN | EPOLLRDNORM; |
| 2880 |
spin_unlock(&isk->accept_q_lock); |
| 2881 |
|
| 2882 |
return mask; |
| 2883 |
} |
| 2884 |
|
| 2885 |
__poll_t smc_poll(struct file *file, struct socket *sock, |
| 2886 |
poll_table *wait) |
| 2887 |
{ |
| 2888 |
struct sock *sk = sock->sk; |
| 2889 |
struct smc_sock *smc; |
| 2890 |
__poll_t mask = 0; |
| 2891 |
|
| 2892 |
if (!sk) |
| 2893 |
return EPOLLNVAL; |
| 2894 |
|
| 2895 |
smc = smc_sk(sock->sk); |
| 2896 |
if (smc->use_fallback) { |
| 2897 |
/* delegate to CLC child sock */ |
| 2898 |
mask = smc->clcsock->ops->poll(file, smc->clcsock, wait); |
| 2899 |
sk->sk_err = smc->clcsock->sk->sk_err; |
| 2900 |
} else { |
| 2901 |
if (sk->sk_state != SMC_CLOSED) |
| 2902 |
sock_poll_wait(file, sock, wait); |
| 2903 |
if (sk->sk_err) |
| 2904 |
mask |= EPOLLERR; |
| 2905 |
if ((sk->sk_shutdown == SHUTDOWN_MASK) || |
| 2906 |
(sk->sk_state == SMC_CLOSED)) |
| 2907 |
mask |= EPOLLHUP; |
| 2908 |
if (sk->sk_state == SMC_LISTEN) { |
| 2909 |
/* woken up by sk_data_ready in smc_listen_work() */ |
| 2910 |
mask |= smc_accept_poll(sk); |
| 2911 |
} else if (smc->use_fallback) { /* as result of connect_work()*/ |
| 2912 |
mask |= smc->clcsock->ops->poll(file, smc->clcsock, |
| 2913 |
wait); |
| 2914 |
sk->sk_err = smc->clcsock->sk->sk_err; |
| 2915 |
} else { |
| 2916 |
if ((sk->sk_state != SMC_INIT && |
| 2917 |
atomic_read(&smc->conn.sndbuf_space)) || |
| 2918 |
sk->sk_shutdown & SEND_SHUTDOWN) { |
| 2919 |
mask |= EPOLLOUT | EPOLLWRNORM; |
| 2920 |
} else { |
| 2921 |
sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); |
| 2922 |
set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
| 2923 |
|
| 2924 |
if (sk->sk_state != SMC_INIT) { |
| 2925 |
/* Race breaker the same way as tcp_poll(). */ |
| 2926 |
smp_mb__after_atomic(); |
| 2927 |
if (atomic_read(&smc->conn.sndbuf_space)) |
| 2928 |
mask |= EPOLLOUT | EPOLLWRNORM; |
| 2929 |
} |
| 2930 |
} |
| 2931 |
if (atomic_read(&smc->conn.bytes_to_rcv)) |
| 2932 |
mask |= EPOLLIN | EPOLLRDNORM; |
| 2933 |
if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 2934 |
mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; |
| 2935 |
if (sk->sk_state == SMC_APPCLOSEWAIT1) |
| 2936 |
mask |= EPOLLIN; |
| 2937 |
if (smc->conn.urg_state == SMC_URG_VALID) |
| 2938 |
mask |= EPOLLPRI; |
| 2939 |
} |
| 2940 |
} |
| 2941 |
|
| 2942 |
return mask; |
| 2943 |
} |
| 2944 |
|
| 2945 |
int smc_shutdown(struct socket *sock, int how) |
| 2946 |
{ |
| 2947 |
struct sock *sk = sock->sk; |
| 2948 |
bool do_shutdown = true; |
| 2949 |
struct smc_sock *smc; |
| 2950 |
int rc = -EINVAL; |
| 2951 |
int old_state; |
| 2952 |
int rc1 = 0; |
| 2953 |
|
| 2954 |
smc = smc_sk(sk); |
| 2955 |
|
| 2956 |
if ((how < SHUT_RD) || (how > SHUT_RDWR)) |
| 2957 |
return rc; |
| 2958 |
|
| 2959 |
lock_sock(sk); |
| 2960 |
|
| 2961 |
if (sock->state == SS_CONNECTING) { |
| 2962 |
if (sk->sk_state == SMC_ACTIVE) |
| 2963 |
sock->state = SS_CONNECTED; |
| 2964 |
else if (sk->sk_state == SMC_PEERCLOSEWAIT1 || |
| 2965 |
sk->sk_state == SMC_PEERCLOSEWAIT2 || |
| 2966 |
sk->sk_state == SMC_APPCLOSEWAIT1 || |
| 2967 |
sk->sk_state == SMC_APPCLOSEWAIT2 || |
| 2968 |
sk->sk_state == SMC_APPFINCLOSEWAIT) |
| 2969 |
sock->state = SS_DISCONNECTING; |
| 2970 |
} |
| 2971 |
|
| 2972 |
rc = -ENOTCONN; |
| 2973 |
if ((sk->sk_state != SMC_ACTIVE) && |
| 2974 |
(sk->sk_state != SMC_PEERCLOSEWAIT1) && |
| 2975 |
(sk->sk_state != SMC_PEERCLOSEWAIT2) && |
| 2976 |
(sk->sk_state != SMC_APPCLOSEWAIT1) && |
| 2977 |
(sk->sk_state != SMC_APPCLOSEWAIT2) && |
| 2978 |
(sk->sk_state != SMC_APPFINCLOSEWAIT)) |
| 2979 |
goto out; |
| 2980 |
if (smc->use_fallback) { |
| 2981 |
rc = kernel_sock_shutdown(smc->clcsock, how); |
| 2982 |
sk->sk_shutdown = smc->clcsock->sk->sk_shutdown; |
| 2983 |
if (sk->sk_shutdown == SHUTDOWN_MASK) { |
| 2984 |
sk->sk_state = SMC_CLOSED; |
| 2985 |
sk->sk_socket->state = SS_UNCONNECTED; |
| 2986 |
sock_put(sk); |
| 2987 |
} |
| 2988 |
goto out; |
| 2989 |
} |
| 2990 |
switch (how) { |
| 2991 |
case SHUT_RDWR: /* shutdown in both directions */ |
| 2992 |
old_state = sk->sk_state; |
| 2993 |
rc = smc_close_active(smc); |
| 2994 |
if (old_state == SMC_ACTIVE && |
| 2995 |
sk->sk_state == SMC_PEERCLOSEWAIT1) |
| 2996 |
do_shutdown = false; |
| 2997 |
break; |
| 2998 |
case SHUT_WR: |
| 2999 |
rc = smc_close_shutdown_write(smc); |
| 3000 |
break; |
| 3001 |
case SHUT_RD: |
| 3002 |
rc = 0; |
| 3003 |
/* nothing more to do because peer is not involved */ |
| 3004 |
break; |
| 3005 |
} |
| 3006 |
if (do_shutdown && smc->clcsock) |
| 3007 |
rc1 = kernel_sock_shutdown(smc->clcsock, how); |
| 3008 |
/* map sock_shutdown_cmd constants to sk_shutdown value range */ |
| 3009 |
sk->sk_shutdown |= how + 1; |
| 3010 |
|
| 3011 |
if (sk->sk_state == SMC_CLOSED) |
| 3012 |
sock->state = SS_UNCONNECTED; |
| 3013 |
else |
| 3014 |
sock->state = SS_DISCONNECTING; |
| 3015 |
out: |
| 3016 |
release_sock(sk); |
| 3017 |
return rc ? rc : rc1; |
| 3018 |
} |
| 3019 |
|
| 3020 |
static int __smc_getsockopt(struct socket *sock, int level, int optname, |
| 3021 |
char __user *optval, int __user *optlen) |
| 3022 |
{ |
| 3023 |
struct smc_sock *smc; |
| 3024 |
int val, len; |
| 3025 |
|
| 3026 |
smc = smc_sk(sock->sk); |
| 3027 |
|
| 3028 |
if (get_user(len, optlen)) |
| 3029 |
return -EFAULT; |
| 3030 |
|
| 3031 |
len = min_t(int, len, sizeof(int)); |
| 3032 |
|
| 3033 |
if (len < 0) |
| 3034 |
return -EINVAL; |
| 3035 |
|
| 3036 |
switch (optname) { |
| 3037 |
case SMC_LIMIT_HS: |
| 3038 |
val = smc->limit_smc_hs; |
| 3039 |
break; |
| 3040 |
default: |
| 3041 |
return -EOPNOTSUPP; |
| 3042 |
} |
| 3043 |
|
| 3044 |
if (put_user(len, optlen)) |
| 3045 |
return -EFAULT; |
| 3046 |
if (copy_to_user(optval, &val, len)) |
| 3047 |
return -EFAULT; |
| 3048 |
|
| 3049 |
return 0; |
| 3050 |
} |
| 3051 |
|
| 3052 |
static int __smc_setsockopt(struct socket *sock, int level, int optname, |
| 3053 |
sockptr_t optval, unsigned int optlen) |
| 3054 |
{ |
| 3055 |
struct sock *sk = sock->sk; |
| 3056 |
struct smc_sock *smc; |
| 3057 |
int val, rc; |
| 3058 |
|
| 3059 |
smc = smc_sk(sk); |
| 3060 |
|
| 3061 |
lock_sock(sk); |
| 3062 |
switch (optname) { |
| 3063 |
case SMC_LIMIT_HS: |
| 3064 |
if (optlen < sizeof(int)) { |
| 3065 |
rc = -EINVAL; |
| 3066 |
break; |
| 3067 |
} |
| 3068 |
if (copy_from_sockptr(&val, optval, sizeof(int))) { |
| 3069 |
rc = -EFAULT; |
| 3070 |
break; |
| 3071 |
} |
| 3072 |
|
| 3073 |
smc->limit_smc_hs = !!val; |
| 3074 |
rc = 0; |
| 3075 |
break; |
| 3076 |
default: |
| 3077 |
rc = -EOPNOTSUPP; |
| 3078 |
break; |
| 3079 |
} |
| 3080 |
release_sock(sk); |
| 3081 |
|
| 3082 |
return rc; |
| 3083 |
} |
| 3084 |
|
| 3085 |
int smc_setsockopt(struct socket *sock, int level, int optname, |
| 3086 |
sockptr_t optval, unsigned int optlen) |
| 3087 |
{ |
| 3088 |
struct sock *sk = sock->sk; |
| 3089 |
struct smc_sock *smc; |
| 3090 |
int val, rc; |
| 3091 |
|
| 3092 |
if (level == SOL_TCP && optname == TCP_ULP) |
| 3093 |
return -EOPNOTSUPP; |
| 3094 |
else if (level == SOL_SMC) |
| 3095 |
return __smc_setsockopt(sock, level, optname, optval, optlen); |
| 3096 |
|
| 3097 |
smc = smc_sk(sk); |
| 3098 |
|
| 3099 |
/* generic setsockopts reaching us here always apply to the |
| 3100 |
* CLC socket |
| 3101 |
*/ |
| 3102 |
mutex_lock(&smc->clcsock_release_lock); |
| 3103 |
if (!smc->clcsock) { |
| 3104 |
mutex_unlock(&smc->clcsock_release_lock); |
| 3105 |
return -EBADF; |
| 3106 |
} |
| 3107 |
if (unlikely(!smc->clcsock->ops->setsockopt)) |
| 3108 |
rc = -EOPNOTSUPP; |
| 3109 |
else |
| 3110 |
rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname, |
| 3111 |
optval, optlen); |
| 3112 |
if (smc->clcsock->sk->sk_err) { |
| 3113 |
sk->sk_err = smc->clcsock->sk->sk_err; |
| 3114 |
sk_error_report(sk); |
| 3115 |
} |
| 3116 |
mutex_unlock(&smc->clcsock_release_lock); |
| 3117 |
|
| 3118 |
if (optlen < sizeof(int)) |
| 3119 |
return -EINVAL; |
| 3120 |
if (copy_from_sockptr(&val, optval, sizeof(int))) |
| 3121 |
return -EFAULT; |
| 3122 |
|
| 3123 |
lock_sock(sk); |
| 3124 |
if (rc || smc->use_fallback) |
| 3125 |
goto out; |
| 3126 |
switch (optname) { |
| 3127 |
case TCP_FASTOPEN: |
| 3128 |
case TCP_FASTOPEN_CONNECT: |
| 3129 |
case TCP_FASTOPEN_KEY: |
| 3130 |
case TCP_FASTOPEN_NO_COOKIE: |
| 3131 |
/* option not supported by SMC */ |
| 3132 |
if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) { |
| 3133 |
rc = smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP); |
| 3134 |
} else { |
| 3135 |
rc = -EINVAL; |
| 3136 |
} |
| 3137 |
break; |
| 3138 |
case TCP_NODELAY: |
| 3139 |
if (sk->sk_state != SMC_INIT && |
| 3140 |
sk->sk_state != SMC_LISTEN && |
| 3141 |
sk->sk_state != SMC_CLOSED) { |
| 3142 |
if (val) { |
| 3143 |
SMC_STAT_INC(smc, ndly_cnt); |
| 3144 |
smc_tx_pending(&smc->conn); |
| 3145 |
cancel_delayed_work(&smc->conn.tx_work); |
| 3146 |
} |
| 3147 |
} |
| 3148 |
break; |
| 3149 |
case TCP_CORK: |
| 3150 |
if (sk->sk_state != SMC_INIT && |
| 3151 |
sk->sk_state != SMC_LISTEN && |
| 3152 |
sk->sk_state != SMC_CLOSED) { |
| 3153 |
if (!val) { |
| 3154 |
SMC_STAT_INC(smc, cork_cnt); |
| 3155 |
smc_tx_pending(&smc->conn); |
| 3156 |
cancel_delayed_work(&smc->conn.tx_work); |
| 3157 |
} |
| 3158 |
} |
| 3159 |
break; |
| 3160 |
case TCP_DEFER_ACCEPT: |
| 3161 |
smc->sockopt_defer_accept = val; |
| 3162 |
break; |
| 3163 |
default: |
| 3164 |
break; |
| 3165 |
} |
| 3166 |
out: |
| 3167 |
release_sock(sk); |
| 3168 |
|
| 3169 |
return rc; |
| 3170 |
} |
| 3171 |
|
| 3172 |
int smc_getsockopt(struct socket *sock, int level, int optname, |
| 3173 |
char __user *optval, int __user *optlen) |
| 3174 |
{ |
| 3175 |
struct smc_sock *smc; |
| 3176 |
int rc; |
| 3177 |
|
| 3178 |
if (level == SOL_SMC) |
| 3179 |
return __smc_getsockopt(sock, level, optname, optval, optlen); |
| 3180 |
|
| 3181 |
smc = smc_sk(sock->sk); |
| 3182 |
mutex_lock(&smc->clcsock_release_lock); |
| 3183 |
if (!smc->clcsock) { |
| 3184 |
mutex_unlock(&smc->clcsock_release_lock); |
| 3185 |
return -EBADF; |
| 3186 |
} |
| 3187 |
/* socket options apply to the CLC socket */ |
| 3188 |
if (unlikely(!smc->clcsock->ops->getsockopt)) { |
| 3189 |
mutex_unlock(&smc->clcsock_release_lock); |
| 3190 |
return -EOPNOTSUPP; |
| 3191 |
} |
| 3192 |
rc = smc->clcsock->ops->getsockopt(smc->clcsock, level, optname, |
| 3193 |
optval, optlen); |
| 3194 |
mutex_unlock(&smc->clcsock_release_lock); |
| 3195 |
return rc; |
| 3196 |
} |
| 3197 |
|
| 3198 |
int smc_ioctl(struct socket *sock, unsigned int cmd, |
| 3199 |
unsigned long arg) |
| 3200 |
{ |
| 3201 |
union smc_host_cursor cons, urg; |
| 3202 |
struct smc_connection *conn; |
| 3203 |
struct smc_sock *smc; |
| 3204 |
int answ; |
| 3205 |
|
| 3206 |
smc = smc_sk(sock->sk); |
| 3207 |
conn = &smc->conn; |
| 3208 |
lock_sock(&smc->sk); |
| 3209 |
if (smc->use_fallback) { |
| 3210 |
if (!smc->clcsock) { |
| 3211 |
release_sock(&smc->sk); |
| 3212 |
return -EBADF; |
| 3213 |
} |
| 3214 |
answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg); |
| 3215 |
release_sock(&smc->sk); |
| 3216 |
return answ; |
| 3217 |
} |
| 3218 |
switch (cmd) { |
| 3219 |
case SIOCINQ: /* same as FIONREAD */ |
| 3220 |
if (smc->sk.sk_state == SMC_LISTEN) { |
| 3221 |
release_sock(&smc->sk); |
| 3222 |
return -EINVAL; |
| 3223 |
} |
| 3224 |
if (smc->sk.sk_state == SMC_INIT || |
| 3225 |
smc->sk.sk_state == SMC_CLOSED) |
| 3226 |
answ = 0; |
| 3227 |
else |
| 3228 |
answ = atomic_read(&smc->conn.bytes_to_rcv); |
| 3229 |
break; |
| 3230 |
case SIOCOUTQ: |
| 3231 |
/* output queue size (not send + not acked) */ |
| 3232 |
if (smc->sk.sk_state == SMC_LISTEN) { |
| 3233 |
release_sock(&smc->sk); |
| 3234 |
return -EINVAL; |
| 3235 |
} |
| 3236 |
if (smc->sk.sk_state == SMC_INIT || |
| 3237 |
smc->sk.sk_state == SMC_CLOSED) |
| 3238 |
answ = 0; |
| 3239 |
else |
| 3240 |
answ = smc->conn.sndbuf_desc->len - |
| 3241 |
atomic_read(&smc->conn.sndbuf_space); |
| 3242 |
break; |
| 3243 |
case SIOCOUTQNSD: |
| 3244 |
/* output queue size (not send only) */ |
| 3245 |
if (smc->sk.sk_state == SMC_LISTEN) { |
| 3246 |
release_sock(&smc->sk); |
| 3247 |
return -EINVAL; |
| 3248 |
} |
| 3249 |
if (smc->sk.sk_state == SMC_INIT || |
| 3250 |
smc->sk.sk_state == SMC_CLOSED) |
| 3251 |
answ = 0; |
| 3252 |
else |
| 3253 |
answ = smc_tx_prepared_sends(&smc->conn); |
| 3254 |
break; |
| 3255 |
case SIOCATMARK: |
| 3256 |
if (smc->sk.sk_state == SMC_LISTEN) { |
| 3257 |
release_sock(&smc->sk); |
| 3258 |
return -EINVAL; |
| 3259 |
} |
| 3260 |
if (smc->sk.sk_state == SMC_INIT || |
| 3261 |
smc->sk.sk_state == SMC_CLOSED) { |
| 3262 |
answ = 0; |
| 3263 |
} else { |
| 3264 |
smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn); |
| 3265 |
smc_curs_copy(&urg, &conn->urg_curs, conn); |
| 3266 |
answ = smc_curs_diff(conn->rmb_desc->len, |
| 3267 |
&cons, &urg) == 1; |
| 3268 |
} |
| 3269 |
break; |
| 3270 |
default: |
| 3271 |
release_sock(&smc->sk); |
| 3272 |
return -ENOIOCTLCMD; |
| 3273 |
} |
| 3274 |
release_sock(&smc->sk); |
| 3275 |
|
| 3276 |
return put_user(answ, (int __user *)arg); |
| 3277 |
} |
| 3278 |
|
| 3279 |
/* Map the affected portions of the rmbe into an spd, note the number of bytes |
| 3280 |
* to splice in conn->splice_pending, and press 'go'. Delays consumer cursor |
| 3281 |
* updates till whenever a respective page has been fully processed. |
| 3282 |
* Note that subsequent recv() calls have to wait till all splice() processing |
| 3283 |
* completed. |
| 3284 |
*/ |
| 3285 |
ssize_t smc_splice_read(struct socket *sock, loff_t *ppos, |
| 3286 |
struct pipe_inode_info *pipe, size_t len, |
| 3287 |
unsigned int flags) |
| 3288 |
{ |
| 3289 |
struct sock *sk = sock->sk; |
| 3290 |
struct smc_sock *smc; |
| 3291 |
int rc = -ENOTCONN; |
| 3292 |
|
| 3293 |
smc = smc_sk(sk); |
| 3294 |
lock_sock(sk); |
| 3295 |
if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) { |
| 3296 |
/* socket was connected before, no more data to read */ |
| 3297 |
rc = 0; |
| 3298 |
goto out; |
| 3299 |
} |
| 3300 |
if (sk->sk_state == SMC_INIT || |
| 3301 |
sk->sk_state == SMC_LISTEN || |
| 3302 |
sk->sk_state == SMC_CLOSED) |
| 3303 |
goto out; |
| 3304 |
|
| 3305 |
if (sk->sk_state == SMC_PEERFINCLOSEWAIT) { |
| 3306 |
rc = 0; |
| 3307 |
goto out; |
| 3308 |
} |
| 3309 |
|
| 3310 |
if (smc->use_fallback) { |
| 3311 |
rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos, |
| 3312 |
pipe, len, flags); |
| 3313 |
} else { |
| 3314 |
if (*ppos) { |
| 3315 |
rc = -ESPIPE; |
| 3316 |
goto out; |
| 3317 |
} |
| 3318 |
if (flags & SPLICE_F_NONBLOCK) |
| 3319 |
flags = MSG_DONTWAIT; |
| 3320 |
else |
| 3321 |
flags = 0; |
| 3322 |
SMC_STAT_INC(smc, splice_cnt); |
| 3323 |
rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags); |
| 3324 |
} |
| 3325 |
out: |
| 3326 |
release_sock(sk); |
| 3327 |
|
| 3328 |
return rc; |
| 3329 |
} |
| 3330 |
|
| 3331 |
/* must look like tcp */ |
| 3332 |
static const struct proto_ops smc_sock_ops = { |
| 3333 |
.family = PF_SMC, |
| 3334 |
.owner = THIS_MODULE, |
| 3335 |
.release = smc_release, |
| 3336 |
.bind = smc_bind, |
| 3337 |
.connect = smc_connect, |
| 3338 |
.socketpair = sock_no_socketpair, |
| 3339 |
.accept = smc_accept, |
| 3340 |
.getname = smc_getname, |
| 3341 |
.poll = smc_poll, |
| 3342 |
.ioctl = smc_ioctl, |
| 3343 |
.listen = smc_listen, |
| 3344 |
.shutdown = smc_shutdown, |
| 3345 |
.setsockopt = smc_setsockopt, |
| 3346 |
.getsockopt = smc_getsockopt, |
| 3347 |
.sendmsg = smc_sendmsg, |
| 3348 |
.recvmsg = smc_recvmsg, |
| 3349 |
.mmap = sock_no_mmap, |
| 3350 |
.splice_read = smc_splice_read, |
| 3351 |
}; |
| 3352 |
|
| 3353 |
int smc_create_clcsk(struct net *net, struct sock *sk, int family) |
| 3354 |
{ |
| 3355 |
struct smc_sock *smc = smc_sk(sk); |
| 3356 |
int rc; |
| 3357 |
|
| 3358 |
rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP, |
| 3359 |
&smc->clcsock); |
| 3360 |
if (rc) |
| 3361 |
return rc; |
| 3362 |
|
| 3363 |
/* smc_clcsock_release() does not wait smc->clcsock->sk's |
| 3364 |
* destruction; its sk_state might not be TCP_CLOSE after |
| 3365 |
* smc->sk is close()d, and TCP timers can be fired later, |
| 3366 |
* which need net ref. |
| 3367 |
*/ |
| 3368 |
sk = smc->clcsock->sk; |
| 3369 |
sk_net_refcnt_upgrade(sk); |
| 3370 |
return 0; |
| 3371 |
} |
| 3372 |
|
| 3373 |
static int smc_create(struct net *net, struct socket *sock, int protocol, |
| 3374 |
int kern) |
| 3375 |
{ |
| 3376 |
int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET; |
| 3377 |
struct sock *sk; |
| 3378 |
int rc; |
| 3379 |
|
| 3380 |
rc = -ESOCKTNOSUPPORT; |
| 3381 |
if (sock->type != SOCK_STREAM) |
| 3382 |
goto out; |
| 3383 |
|
| 3384 |
rc = -EPROTONOSUPPORT; |
| 3385 |
if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6) |
| 3386 |
goto out; |
| 3387 |
|
| 3388 |
rc = -ENOBUFS; |
| 3389 |
sock->ops = &smc_sock_ops; |
| 3390 |
sock->state = SS_UNCONNECTED; |
| 3391 |
sk = smc_sock_alloc(net, sock, protocol); |
| 3392 |
if (!sk) |
| 3393 |
goto out; |
| 3394 |
|
| 3395 |
rc = smc_create_clcsk(net, sk, family); |
| 3396 |
if (rc) { |
| 3397 |
sk_common_release(sk); |
| 3398 |
sock->sk = NULL; |
| 3399 |
} |
| 3400 |
out: |
| 3401 |
return rc; |
| 3402 |
} |
| 3403 |
|
| 3404 |
static const struct net_proto_family smc_sock_family_ops = { |
| 3405 |
.family = PF_SMC, |
| 3406 |
.owner = THIS_MODULE, |
| 3407 |
.create = smc_create, |
| 3408 |
}; |
| 3409 |
|
| 3410 |
unsigned int smc_net_id; |
| 3411 |
|
| 3412 |
static __net_init int smc_net_init(struct net *net) |
| 3413 |
{ |
| 3414 |
int rc; |
| 3415 |
|
| 3416 |
rc = smc_sysctl_net_init(net); |
| 3417 |
if (rc) |
| 3418 |
return rc; |
| 3419 |
return smc_pnet_net_init(net); |
| 3420 |
} |
| 3421 |
|
| 3422 |
static void __net_exit smc_net_exit(struct net *net) |
| 3423 |
{ |
| 3424 |
smc_sysctl_net_exit(net); |
| 3425 |
smc_pnet_net_exit(net); |
| 3426 |
} |
| 3427 |
|
| 3428 |
static __net_init int smc_net_stat_init(struct net *net) |
| 3429 |
{ |
| 3430 |
return smc_stats_init(net); |
| 3431 |
} |
| 3432 |
|
| 3433 |
static void __net_exit smc_net_stat_exit(struct net *net) |
| 3434 |
{ |
| 3435 |
smc_stats_exit(net); |
| 3436 |
} |
| 3437 |
|
| 3438 |
static struct pernet_operations smc_net_ops = { |
| 3439 |
.init = smc_net_init, |
| 3440 |
.exit = smc_net_exit, |
| 3441 |
.id = &smc_net_id, |
| 3442 |
.size = sizeof(struct smc_net), |
| 3443 |
}; |
| 3444 |
|
| 3445 |
static struct pernet_operations smc_net_stat_ops = { |
| 3446 |
.init = smc_net_stat_init, |
| 3447 |
.exit = smc_net_stat_exit, |
| 3448 |
}; |
| 3449 |
|
| 3450 |
static int __init smc_init(void) |
| 3451 |
{ |
| 3452 |
int rc; |
| 3453 |
|
| 3454 |
rc = register_pernet_subsys(&smc_net_ops); |
| 3455 |
if (rc) |
| 3456 |
return rc; |
| 3457 |
|
| 3458 |
rc = register_pernet_subsys(&smc_net_stat_ops); |
| 3459 |
if (rc) |
| 3460 |
goto out_pernet_subsys; |
| 3461 |
|
| 3462 |
rc = smc_ism_init(); |
| 3463 |
if (rc) |
| 3464 |
goto out_pernet_subsys_stat; |
| 3465 |
smc_clc_init(); |
| 3466 |
|
| 3467 |
rc = smc_nl_init(); |
| 3468 |
if (rc) |
| 3469 |
goto out_ism; |
| 3470 |
|
| 3471 |
rc = smc_pnet_init(); |
| 3472 |
if (rc) |
| 3473 |
goto out_nl; |
| 3474 |
|
| 3475 |
rc = -ENOMEM; |
| 3476 |
|
| 3477 |
smc_tcp_ls_wq = alloc_workqueue("smc_tcp_ls_wq", WQ_PERCPU, 0); |
| 3478 |
if (!smc_tcp_ls_wq) |
| 3479 |
goto out_pnet; |
| 3480 |
|
| 3481 |
smc_hs_wq = alloc_workqueue("smc_hs_wq", WQ_PERCPU, 0); |
| 3482 |
if (!smc_hs_wq) |
| 3483 |
goto out_alloc_tcp_ls_wq; |
| 3484 |
|
| 3485 |
smc_close_wq = alloc_workqueue("smc_close_wq", WQ_PERCPU, 0); |
| 3486 |
if (!smc_close_wq) |
| 3487 |
goto out_alloc_hs_wq; |
| 3488 |
|
| 3489 |
rc = smc_core_init(); |
| 3490 |
if (rc) { |
| 3491 |
pr_err("%s: smc_core_init fails with %d\n", __func__, rc); |
| 3492 |
goto out_alloc_wqs; |
| 3493 |
} |
| 3494 |
|
| 3495 |
rc = smc_llc_init(); |
| 3496 |
if (rc) { |
| 3497 |
pr_err("%s: smc_llc_init fails with %d\n", __func__, rc); |
| 3498 |
goto out_core; |
| 3499 |
} |
| 3500 |
|
| 3501 |
rc = smc_cdc_init(); |
| 3502 |
if (rc) { |
| 3503 |
pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc); |
| 3504 |
goto out_core; |
| 3505 |
} |
| 3506 |
|
| 3507 |
rc = proto_register(&smc_proto, 1); |
| 3508 |
if (rc) { |
| 3509 |
pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc); |
| 3510 |
goto out_core; |
| 3511 |
} |
| 3512 |
|
| 3513 |
rc = proto_register(&smc_proto6, 1); |
| 3514 |
if (rc) { |
| 3515 |
pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc); |
| 3516 |
goto out_proto; |
| 3517 |
} |
| 3518 |
|
| 3519 |
rc = sock_register(&smc_sock_family_ops); |
| 3520 |
if (rc) { |
| 3521 |
pr_err("%s: sock_register fails with %d\n", __func__, rc); |
| 3522 |
goto out_proto6; |
| 3523 |
} |
| 3524 |
INIT_HLIST_HEAD(&smc_v4_hashinfo.ht); |
| 3525 |
INIT_HLIST_HEAD(&smc_v6_hashinfo.ht); |
| 3526 |
|
| 3527 |
rc = smc_ib_register_client(); |
| 3528 |
if (rc) { |
| 3529 |
pr_err("%s: ib_register fails with %d\n", __func__, rc); |
| 3530 |
goto out_sock; |
| 3531 |
} |
| 3532 |
rc = smc_inet_init(); |
| 3533 |
if (rc) { |
| 3534 |
pr_err("%s: smc_inet_init fails with %d\n", __func__, rc); |
| 3535 |
goto out_ib; |
| 3536 |
} |
| 3537 |
rc = bpf_smc_hs_ctrl_init(); |
| 3538 |
if (rc) { |
| 3539 |
pr_err("%s: bpf_smc_hs_ctrl_init fails with %d\n", __func__, |
| 3540 |
rc); |
| 3541 |
goto out_inet; |
| 3542 |
} |
| 3543 |
static_branch_enable(&tcp_have_smc); |
| 3544 |
return 0; |
| 3545 |
out_inet: |
| 3546 |
smc_inet_exit(); |
| 3547 |
out_ib: |
| 3548 |
smc_ib_unregister_client(); |
| 3549 |
out_sock: |
| 3550 |
sock_unregister(PF_SMC); |
| 3551 |
out_proto6: |
| 3552 |
proto_unregister(&smc_proto6); |
| 3553 |
out_proto: |
| 3554 |
proto_unregister(&smc_proto); |
| 3555 |
out_core: |
| 3556 |
smc_core_exit(); |
| 3557 |
out_alloc_wqs: |
| 3558 |
destroy_workqueue(smc_close_wq); |
| 3559 |
out_alloc_hs_wq: |
| 3560 |
destroy_workqueue(smc_hs_wq); |
| 3561 |
out_alloc_tcp_ls_wq: |
| 3562 |
destroy_workqueue(smc_tcp_ls_wq); |
| 3563 |
out_pnet: |
| 3564 |
smc_pnet_exit(); |
| 3565 |
out_nl: |
| 3566 |
smc_nl_exit(); |
| 3567 |
out_ism: |
| 3568 |
smc_clc_exit(); |
| 3569 |
smc_ism_exit(); |
| 3570 |
out_pernet_subsys_stat: |
| 3571 |
unregister_pernet_subsys(&smc_net_stat_ops); |
| 3572 |
out_pernet_subsys: |
| 3573 |
unregister_pernet_subsys(&smc_net_ops); |
| 3574 |
|
| 3575 |
return rc; |
| 3576 |
} |
| 3577 |
|
| 3578 |
static void __exit smc_exit(void) |
| 3579 |
{ |
| 3580 |
static_branch_disable(&tcp_have_smc); |
| 3581 |
smc_inet_exit(); |
| 3582 |
sock_unregister(PF_SMC); |
| 3583 |
smc_core_exit(); |
| 3584 |
smc_ib_unregister_client(); |
| 3585 |
smc_ism_exit(); |
| 3586 |
destroy_workqueue(smc_close_wq); |
| 3587 |
destroy_workqueue(smc_tcp_ls_wq); |
| 3588 |
destroy_workqueue(smc_hs_wq); |
| 3589 |
proto_unregister(&smc_proto6); |
| 3590 |
proto_unregister(&smc_proto); |
| 3591 |
smc_pnet_exit(); |
| 3592 |
smc_nl_exit(); |
| 3593 |
smc_clc_exit(); |
| 3594 |
unregister_pernet_subsys(&smc_net_stat_ops); |
| 3595 |
unregister_pernet_subsys(&smc_net_ops); |
| 3596 |
rcu_barrier(); |
| 3597 |
} |
| 3598 |
|
| 3599 |
module_init(smc_init); |
| 3600 |
module_exit(smc_exit); |
| 3601 |
|
| 3602 |
MODULE_AUTHOR("Ursula Braun <[email protected]>"); |
| 3603 |
MODULE_DESCRIPTION("smc socket address family"); |
| 3604 |
MODULE_LICENSE("GPL"); |
| 3605 |
MODULE_ALIAS_NETPROTO(PF_SMC); |
| 3606 |
/* 256 for IPPROTO_SMC and 1 for SOCK_STREAM */ |
| 3607 |
MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET, 256, 1); |
| 3608 |
#if IS_ENABLED(CONFIG_IPV6) |
| 3609 |
MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET6, 256, 1); |
| 3610 |
#endif /* CONFIG_IPV6 */ |
| 3611 |
MODULE_ALIAS_GENL_FAMILY(SMC_GENL_FAMILY_NAME); |