at commit c03c1a14
| 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 <ubraun@linux.vnet.ibm.com> |
| 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 | .ht = HLIST_HEAD_INIT, |
| 192 | }; |
| 193 | |
| 194 | struct smc_hashinfo smc_v6_hashinfo = { |
| 195 | .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock), |
| 196 | .ht = HLIST_HEAD_INIT, |
| 197 | }; |
| 198 | |
| 199 | int smc_hash_sk(struct sock *sk) |
| 200 | { |
| 201 | struct smc_hashinfo *h = sk->sk_prot->h.smc_hash; |
| 202 | struct hlist_head *head; |
| 203 | |
| 204 | head = &h->ht; |
| 205 | |
| 206 | write_lock_bh(&h->lock); |
| 207 | sk_add_node(sk, head); |
| 208 | write_unlock_bh(&h->lock); |
| 209 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | void smc_unhash_sk(struct sock *sk) |
| 215 | { |
| 216 | struct smc_hashinfo *h = sk->sk_prot->h.smc_hash; |
| 217 | |
| 218 | write_lock_bh(&h->lock); |
| 219 | if (sk_del_node_init(sk)) |
| 220 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); |
| 221 | write_unlock_bh(&h->lock); |
| 222 | } |
| 223 | |
| 224 | /* This will be called before user really release sock_lock. So do the |
| 225 | * work which we didn't do because of user hold the sock_lock in the |
| 226 | * BH context |
| 227 | */ |
| 228 | void smc_release_cb(struct sock *sk) |
| 229 | { |
| 230 | struct smc_sock *smc = smc_sk(sk); |
| 231 | |
| 232 | if (smc->conn.tx_in_release_sock) { |
| 233 | smc_tx_pending(&smc->conn); |
| 234 | smc->conn.tx_in_release_sock = false; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | struct proto smc_proto = { |
| 239 | .name = "SMC", |
| 240 | .owner = THIS_MODULE, |
| 241 | .keepalive = smc_set_keepalive, |
| 242 | .hash = smc_hash_sk, |
| 243 | .unhash = smc_unhash_sk, |
| 244 | .release_cb = smc_release_cb, |
| 245 | .obj_size = sizeof(struct smc_sock), |
| 246 | .h.smc_hash = &smc_v4_hashinfo, |
| 247 | .slab_flags = SLAB_TYPESAFE_BY_RCU, |
| 248 | }; |
| 249 | EXPORT_SYMBOL_GPL(smc_proto); |
| 250 | |
| 251 | struct proto smc_proto6 = { |
| 252 | .name = "SMC6", |
| 253 | .owner = THIS_MODULE, |
| 254 | .keepalive = smc_set_keepalive, |
| 255 | .hash = smc_hash_sk, |
| 256 | .unhash = smc_unhash_sk, |
| 257 | .release_cb = smc_release_cb, |
| 258 | .obj_size = sizeof(struct smc_sock), |
| 259 | .h.smc_hash = &smc_v6_hashinfo, |
| 260 | .slab_flags = SLAB_TYPESAFE_BY_RCU, |
| 261 | }; |
| 262 | EXPORT_SYMBOL_GPL(smc_proto6); |
| 263 | |
| 264 | static void smc_fback_restore_callbacks(struct smc_sock *smc) |
| 265 | { |
| 266 | struct sock *clcsk = smc->clcsock->sk; |
| 267 | |
| 268 | write_lock_bh(&clcsk->sk_callback_lock); |
| 269 | rcu_assign_sk_user_data(clcsk, NULL); |
| 270 | |
| 271 | smc_clcsock_restore_cb(&clcsk->sk_state_change, &smc->clcsk_state_change); |
| 272 | smc_clcsock_restore_cb(&clcsk->sk_data_ready, &smc->clcsk_data_ready); |
| 273 | smc_clcsock_restore_cb(&clcsk->sk_write_space, &smc->clcsk_write_space); |
| 274 | smc_clcsock_restore_cb(&clcsk->sk_error_report, &smc->clcsk_error_report); |
| 275 | |
| 276 | write_unlock_bh(&clcsk->sk_callback_lock); |
| 277 | } |
| 278 | |
| 279 | static void smc_restore_fallback_changes(struct smc_sock *smc) |
| 280 | { |
| 281 | if (smc->clcsock->file) { /* non-accepted sockets have no file yet */ |
| 282 | smc->clcsock->file->private_data = smc->sk.sk_socket; |
| 283 | smc->clcsock->file = NULL; |
| 284 | smc_fback_restore_callbacks(smc); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | static int __smc_release(struct smc_sock *smc) |
| 289 | { |
| 290 | struct sock *sk = &smc->sk; |
| 291 | int rc = 0; |
| 292 | |
| 293 | if (!smc->use_fallback) { |
| 294 | rc = smc_close_active(smc); |
| 295 | smc_sock_set_flag(sk, SOCK_DEAD); |
| 296 | sk->sk_shutdown |= SHUTDOWN_MASK; |
| 297 | } else { |
| 298 | if (sk->sk_state != SMC_CLOSED) { |
| 299 | if (sk->sk_state != SMC_LISTEN && |
| 300 | sk->sk_state != SMC_INIT) |
| 301 | sock_put(sk); /* passive closing */ |
| 302 | if (sk->sk_state == SMC_LISTEN) { |
| 303 | /* wake up clcsock accept */ |
| 304 | rc = kernel_sock_shutdown(smc->clcsock, |
| 305 | SHUT_RDWR); |
| 306 | } |
| 307 | sk->sk_state = SMC_CLOSED; |
| 308 | sk->sk_state_change(sk); |
| 309 | } |
| 310 | smc_restore_fallback_changes(smc); |
| 311 | } |
| 312 | |
| 313 | sk->sk_prot->unhash(sk); |
| 314 | |
| 315 | if (sk->sk_state == SMC_CLOSED) { |
| 316 | if (smc->clcsock) { |
| 317 | release_sock(sk); |
| 318 | smc_clcsock_release(smc); |
| 319 | lock_sock(sk); |
| 320 | } |
| 321 | if (!smc->use_fallback) |
| 322 | smc_conn_free(&smc->conn); |
| 323 | } |
| 324 | |
| 325 | return rc; |
| 326 | } |
| 327 | |
| 328 | int smc_release(struct socket *sock) |
| 329 | { |
| 330 | struct sock *sk = sock->sk; |
| 331 | struct smc_sock *smc; |
| 332 | int old_state, rc = 0; |
| 333 | |
| 334 | if (!sk) |
| 335 | goto out; |
| 336 | |
| 337 | sock_hold(sk); /* sock_put below */ |
| 338 | smc = smc_sk(sk); |
| 339 | |
| 340 | old_state = sk->sk_state; |
| 341 | |
| 342 | /* cleanup for a dangling non-blocking connect */ |
| 343 | if (smc->connect_nonblock && old_state == SMC_INIT) |
| 344 | tcp_abort(smc->clcsock->sk, ECONNABORTED); |
| 345 | |
| 346 | if (cancel_work_sync(&smc->connect_work)) |
| 347 | sock_put(&smc->sk); /* sock_hold in smc_connect for passive closing */ |
| 348 | |
| 349 | if (sk->sk_state == SMC_LISTEN) |
| 350 | /* smc_close_non_accepted() is called and acquires |
| 351 | * sock lock for child sockets again |
| 352 | */ |
| 353 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
| 354 | else |
| 355 | lock_sock(sk); |
| 356 | |
| 357 | if (old_state == SMC_INIT && sk->sk_state == SMC_ACTIVE && |
| 358 | !smc->use_fallback) |
| 359 | smc_close_active_abort(smc); |
| 360 | |
| 361 | rc = __smc_release(smc); |
| 362 | |
| 363 | /* detach socket */ |
| 364 | sock_orphan(sk); |
| 365 | sock->sk = NULL; |
| 366 | release_sock(sk); |
| 367 | |
| 368 | sock_put(sk); /* sock_hold above */ |
| 369 | sock_put(sk); /* final sock_put */ |
| 370 | out: |
| 371 | return rc; |
| 372 | } |
| 373 | |
| 374 | static void smc_destruct(struct sock *sk) |
| 375 | { |
| 376 | if (sk->sk_state != SMC_CLOSED) |
| 377 | return; |
| 378 | if (!sock_flag(sk, SOCK_DEAD)) |
| 379 | return; |
| 380 | switch (sk->sk_family) { |
| 381 | case AF_INET: |
| 382 | inet_sock_destruct(sk); |
| 383 | break; |
| 384 | #if IS_ENABLED(CONFIG_IPV6) |
| 385 | case AF_INET6: |
| 386 | inet6_sock_destruct(sk); |
| 387 | break; |
| 388 | #endif |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | static struct lock_class_key smc_key; |
| 393 | static struct lock_class_key smc_slock_key; |
| 394 | |
| 395 | void smc_sk_init(struct net *net, struct sock *sk, int protocol) |
| 396 | { |
| 397 | struct smc_sock *smc = smc_sk(sk); |
| 398 | |
| 399 | sk->sk_state = SMC_INIT; |
| 400 | sk->sk_destruct = smc_destruct; |
| 401 | sk->sk_protocol = protocol; |
| 402 | WRITE_ONCE(sk->sk_sndbuf, 2 * READ_ONCE(net->smc.sysctl_wmem)); |
| 403 | WRITE_ONCE(sk->sk_rcvbuf, 2 * READ_ONCE(net->smc.sysctl_rmem)); |
| 404 | INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work); |
| 405 | INIT_WORK(&smc->connect_work, smc_connect_work); |
| 406 | INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work); |
| 407 | INIT_LIST_HEAD(&smc->accept_q); |
| 408 | sock_lock_init_class_and_name(sk, "slock-AF_SMC", &smc_slock_key, |
| 409 | "sk_lock-AF_SMC", &smc_key); |
| 410 | spin_lock_init(&smc->accept_q_lock); |
| 411 | spin_lock_init(&smc->conn.send_lock); |
| 412 | mutex_init(&smc->clcsock_release_lock); |
| 413 | smc_init_saved_callbacks(smc); |
| 414 | smc->limit_smc_hs = net->smc.limit_smc_hs; |
| 415 | smc->use_fallback = false; /* assume rdma capability first */ |
| 416 | smc->fallback_rsn = 0; |
| 417 | smc_close_init(smc); |
| 418 | sk->sk_prot->hash(sk); |
| 419 | } |
| 420 | |
| 421 | static struct sock *smc_sock_alloc(struct net *net, struct socket *sock, |
| 422 | int protocol) |
| 423 | { |
| 424 | struct proto *prot; |
| 425 | struct sock *sk; |
| 426 | |
| 427 | prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto; |
| 428 | sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0); |
| 429 | if (!sk) |
| 430 | return NULL; |
| 431 | |
| 432 | sock_init_data(sock, sk); /* sets sk_refcnt to 1 */ |
| 433 | smc_sk_init(net, sk, protocol); |
| 434 | |
| 435 | return sk; |
| 436 | } |
| 437 | |
| 438 | int smc_bind(struct socket *sock, struct sockaddr_unsized *uaddr, |
| 439 | int addr_len) |
| 440 | { |
| 441 | struct sockaddr_in *addr = (struct sockaddr_in *)uaddr; |
| 442 | struct sock *sk = sock->sk; |
| 443 | struct smc_sock *smc; |
| 444 | int rc; |
| 445 | |
| 446 | smc = smc_sk(sk); |
| 447 | |
| 448 | /* replicate tests from inet_bind(), to be safe wrt. future changes */ |
| 449 | rc = -EINVAL; |
| 450 | if (addr_len < sizeof(struct sockaddr_in)) |
| 451 | goto out; |
| 452 | |
| 453 | rc = -EAFNOSUPPORT; |
| 454 | if (addr->sin_family != AF_INET && |
| 455 | addr->sin_family != AF_INET6 && |
| 456 | addr->sin_family != AF_UNSPEC) |
| 457 | goto out; |
| 458 | /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */ |
| 459 | if (addr->sin_family == AF_UNSPEC && |
| 460 | addr->sin_addr.s_addr != htonl(INADDR_ANY)) |
| 461 | goto out; |
| 462 | |
| 463 | lock_sock(sk); |
| 464 | |
| 465 | /* Check if socket is already active */ |
| 466 | rc = -EINVAL; |
| 467 | if (sk->sk_state != SMC_INIT || smc->connect_nonblock) |
| 468 | goto out_rel; |
| 469 | |
| 470 | smc->clcsock->sk->sk_reuse = sk->sk_reuse; |
| 471 | smc->clcsock->sk->sk_reuseport = sk->sk_reuseport; |
| 472 | rc = kernel_bind(smc->clcsock, uaddr, addr_len); |
| 473 | |
| 474 | out_rel: |
| 475 | release_sock(sk); |
| 476 | out: |
| 477 | return rc; |
| 478 | } |
| 479 | |
| 480 | /* copy only relevant settings and flags of SOL_SOCKET level from smc to |
| 481 | * clc socket (since smc is not called for these options from net/core) |
| 482 | */ |
| 483 | |
| 484 | #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \ |
| 485 | (1UL << SOCK_KEEPOPEN) | \ |
| 486 | (1UL << SOCK_LINGER) | \ |
| 487 | (1UL << SOCK_BROADCAST) | \ |
| 488 | (1UL << SOCK_TIMESTAMP) | \ |
| 489 | (1UL << SOCK_DBG) | \ |
| 490 | (1UL << SOCK_RCVTSTAMP) | \ |
| 491 | (1UL << SOCK_RCVTSTAMPNS) | \ |
| 492 | (1UL << SOCK_LOCALROUTE) | \ |
| 493 | (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \ |
| 494 | (1UL << SOCK_RXQ_OVFL) | \ |
| 495 | (1UL << SOCK_WIFI_STATUS) | \ |
| 496 | (1UL << SOCK_NOFCS) | \ |
| 497 | (1UL << SOCK_FILTER_LOCKED) | \ |
| 498 | (1UL << SOCK_TSTAMP_NEW)) |
| 499 | |
| 500 | /* if set, use value set by setsockopt() - else use IPv4 or SMC sysctl value */ |
| 501 | static void smc_adjust_sock_bufsizes(struct sock *nsk, struct sock *osk, |
| 502 | unsigned long mask) |
| 503 | { |
| 504 | nsk->sk_userlocks = osk->sk_userlocks; |
| 505 | if (osk->sk_userlocks & SOCK_SNDBUF_LOCK) |
| 506 | nsk->sk_sndbuf = osk->sk_sndbuf; |
| 507 | if (osk->sk_userlocks & SOCK_RCVBUF_LOCK) |
| 508 | nsk->sk_rcvbuf = osk->sk_rcvbuf; |
| 509 | } |
| 510 | |
| 511 | static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk, |
| 512 | unsigned long mask) |
| 513 | { |
| 514 | /* options we don't get control via setsockopt for */ |
| 515 | nsk->sk_type = osk->sk_type; |
| 516 | nsk->sk_sndtimeo = READ_ONCE(osk->sk_sndtimeo); |
| 517 | nsk->sk_rcvtimeo = READ_ONCE(osk->sk_rcvtimeo); |
| 518 | nsk->sk_mark = READ_ONCE(osk->sk_mark); |
| 519 | nsk->sk_priority = READ_ONCE(osk->sk_priority); |
| 520 | nsk->sk_rcvlowat = osk->sk_rcvlowat; |
| 521 | nsk->sk_bound_dev_if = osk->sk_bound_dev_if; |
| 522 | nsk->sk_err = osk->sk_err; |
| 523 | |
| 524 | nsk->sk_flags &= ~mask; |
| 525 | nsk->sk_flags |= osk->sk_flags & mask; |
| 526 | |
| 527 | smc_adjust_sock_bufsizes(nsk, osk, mask); |
| 528 | } |
| 529 | |
| 530 | static void smc_copy_sock_settings_to_clc(struct smc_sock *smc) |
| 531 | { |
| 532 | smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC); |
| 533 | } |
| 534 | |
| 535 | #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \ |
| 536 | (1UL << SOCK_KEEPOPEN) | \ |
| 537 | (1UL << SOCK_LINGER) | \ |
| 538 | (1UL << SOCK_DBG)) |
| 539 | /* copy only settings and flags relevant for smc from clc to smc socket */ |
| 540 | static void smc_copy_sock_settings_to_smc(struct smc_sock *smc) |
| 541 | { |
| 542 | smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC); |
| 543 | } |
| 544 | |
| 545 | /* register the new vzalloced sndbuf on all links */ |
| 546 | static int smcr_lgr_reg_sndbufs(struct smc_link *link, |
| 547 | struct smc_buf_desc *snd_desc) |
| 548 | { |
| 549 | struct smc_link_group *lgr = link->lgr; |
| 550 | int i, rc = 0; |
| 551 | |
| 552 | if (!snd_desc->is_vm) |
| 553 | return -EINVAL; |
| 554 | |
| 555 | /* protect against parallel smcr_link_reg_buf() */ |
| 556 | down_write(&lgr->llc_conf_mutex); |
| 557 | for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) { |
| 558 | if (!smc_link_active(&lgr->lnk[i])) |
| 559 | continue; |
| 560 | rc = smcr_link_reg_buf(&lgr->lnk[i], snd_desc); |
| 561 | if (rc) |
| 562 | break; |
| 563 | } |
| 564 | up_write(&lgr->llc_conf_mutex); |
| 565 | return rc; |
| 566 | } |
| 567 | |
| 568 | /* register the new rmb on all links */ |
| 569 | static int smcr_lgr_reg_rmbs(struct smc_link *link, |
| 570 | struct smc_buf_desc *rmb_desc) |
| 571 | { |
| 572 | struct smc_link_group *lgr = link->lgr; |
| 573 | bool do_slow = false; |
| 574 | int i, rc = 0; |
| 575 | |
| 576 | rc = smc_llc_flow_initiate(lgr, SMC_LLC_FLOW_RKEY); |
| 577 | if (rc) |
| 578 | return rc; |
| 579 | |
| 580 | down_read(&lgr->llc_conf_mutex); |
| 581 | for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) { |
| 582 | if (!smc_link_active(&lgr->lnk[i])) |
| 583 | continue; |
| 584 | if (!rmb_desc->is_reg_mr[link->link_idx]) { |
| 585 | up_read(&lgr->llc_conf_mutex); |
| 586 | goto slow_path; |
| 587 | } |
| 588 | } |
| 589 | /* mr register already */ |
| 590 | goto fast_path; |
| 591 | slow_path: |
| 592 | do_slow = true; |
| 593 | /* protect against parallel smc_llc_cli_rkey_exchange() and |
| 594 | * parallel smcr_link_reg_buf() |
| 595 | */ |
| 596 | down_write(&lgr->llc_conf_mutex); |
| 597 | for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) { |
| 598 | if (!smc_link_active(&lgr->lnk[i])) |
| 599 | continue; |
| 600 | rc = smcr_link_reg_buf(&lgr->lnk[i], rmb_desc); |
| 601 | if (rc) |
| 602 | goto out; |
| 603 | } |
| 604 | fast_path: |
| 605 | /* exchange confirm_rkey msg with peer */ |
| 606 | rc = smc_llc_do_confirm_rkey(link, rmb_desc); |
| 607 | if (rc) { |
| 608 | rc = -EFAULT; |
| 609 | goto out; |
| 610 | } |
| 611 | rmb_desc->is_conf_rkey = true; |
| 612 | out: |
| 613 | do_slow ? up_write(&lgr->llc_conf_mutex) : up_read(&lgr->llc_conf_mutex); |
| 614 | smc_llc_flow_stop(lgr, &lgr->llc_flow_lcl); |
| 615 | return rc; |
| 616 | } |
| 617 | |
| 618 | static int smcr_clnt_conf_first_link(struct smc_sock *smc) |
| 619 | { |
| 620 | struct smc_link *link = smc->conn.lnk; |
| 621 | struct smc_llc_qentry *qentry; |
| 622 | int rc; |
| 623 | |
| 624 | /* Receive CONFIRM LINK request from server over RoCE fabric. |
| 625 | * Increasing the client's timeout by twice as much as the server's |
| 626 | * timeout by default can temporarily avoid decline messages of |
| 627 | * both sides crossing or colliding |
| 628 | */ |
| 629 | qentry = smc_llc_wait(link->lgr, NULL, 2 * SMC_LLC_WAIT_TIME, |
| 630 | SMC_LLC_CONFIRM_LINK); |
| 631 | if (!qentry) { |
| 632 | struct smc_clc_msg_decline dclc; |
| 633 | |
| 634 | rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc), |
| 635 | SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT); |
| 636 | return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc; |
| 637 | } |
| 638 | smc_llc_save_peer_uid(qentry); |
| 639 | rc = smc_llc_eval_conf_link(qentry, SMC_LLC_REQ); |
| 640 | smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl); |
| 641 | if (rc) |
| 642 | return SMC_CLC_DECL_RMBE_EC; |
| 643 | |
| 644 | rc = smc_ib_modify_qp_rts(link); |
| 645 | if (rc) |
| 646 | return SMC_CLC_DECL_ERR_RDYLNK; |
| 647 | |
| 648 | smc_wr_remember_qp_attr(link); |
| 649 | |
| 650 | /* reg the sndbuf if it was vzalloced */ |
| 651 | if (smc->conn.sndbuf_desc->is_vm) { |
| 652 | if (smcr_link_reg_buf(link, smc->conn.sndbuf_desc)) |
| 653 | return SMC_CLC_DECL_ERR_REGBUF; |
| 654 | } |
| 655 | |
| 656 | /* reg the rmb */ |
| 657 | if (smcr_link_reg_buf(link, smc->conn.rmb_desc)) |
| 658 | return SMC_CLC_DECL_ERR_REGBUF; |
| 659 | |
| 660 | /* confirm_rkey is implicit on 1st contact */ |
| 661 | smc->conn.rmb_desc->is_conf_rkey = true; |
| 662 | |
| 663 | /* send CONFIRM LINK response over RoCE fabric */ |
| 664 | rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP); |
| 665 | if (rc < 0) |
| 666 | return SMC_CLC_DECL_TIMEOUT_CL; |
| 667 | |
| 668 | smc_llc_link_active(link); |
| 669 | smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE); |
| 670 | |
| 671 | if (link->lgr->max_links > 1) { |
| 672 | /* optional 2nd link, receive ADD LINK request from server */ |
| 673 | qentry = smc_llc_wait(link->lgr, NULL, SMC_LLC_WAIT_TIME, |
| 674 | SMC_LLC_ADD_LINK); |
| 675 | if (!qentry) { |
| 676 | struct smc_clc_msg_decline dclc; |
| 677 | |
| 678 | rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc), |
| 679 | SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT); |
| 680 | if (rc == -EAGAIN) |
| 681 | rc = 0; /* no DECLINE received, go with one link */ |
| 682 | return rc; |
| 683 | } |
| 684 | smc_llc_flow_qentry_clr(&link->lgr->llc_flow_lcl); |
| 685 | smc_llc_cli_add_link(link, qentry); |
| 686 | } |
| 687 | return 0; |
| 688 | } |
| 689 | |
| 690 | static bool smc_isascii(char *hostname) |
| 691 | { |
| 692 | int i; |
| 693 | |
| 694 | for (i = 0; i < SMC_MAX_HOSTNAME_LEN; i++) |
| 695 | if (!isascii(hostname[i])) |
| 696 | return false; |
| 697 | return true; |
| 698 | } |
| 699 | |
| 700 | static void smc_conn_save_peer_info_fce(struct smc_sock *smc, |
| 701 | struct smc_clc_msg_accept_confirm *clc) |
| 702 | { |
| 703 | struct smc_clc_first_contact_ext *fce; |
| 704 | int clc_v2_len; |
| 705 | |
| 706 | if (clc->hdr.version == SMC_V1 || |
| 707 | !(clc->hdr.typev2 & SMC_FIRST_CONTACT_MASK)) |
| 708 | return; |
| 709 | |
| 710 | if (smc->conn.lgr->is_smcd) { |
| 711 | memcpy(smc->conn.lgr->negotiated_eid, clc->d1.eid, |
| 712 | SMC_MAX_EID_LEN); |
| 713 | clc_v2_len = offsetofend(struct smc_clc_msg_accept_confirm, d1); |
| 714 | } else { |
| 715 | memcpy(smc->conn.lgr->negotiated_eid, clc->r1.eid, |
| 716 | SMC_MAX_EID_LEN); |
| 717 | clc_v2_len = offsetofend(struct smc_clc_msg_accept_confirm, r1); |
| 718 | } |
| 719 | fce = (struct smc_clc_first_contact_ext *)(((u8 *)clc) + clc_v2_len); |
| 720 | smc->conn.lgr->peer_os = fce->os_type; |
| 721 | smc->conn.lgr->peer_smc_release = fce->release; |
| 722 | if (smc_isascii(fce->hostname)) |
| 723 | memcpy(smc->conn.lgr->peer_hostname, fce->hostname, |
| 724 | SMC_MAX_HOSTNAME_LEN); |
| 725 | } |
| 726 | |
| 727 | static void smcr_conn_save_peer_info(struct smc_sock *smc, |
| 728 | struct smc_clc_msg_accept_confirm *clc) |
| 729 | { |
| 730 | int bufsize = smc_uncompress_bufsize(clc->r0.rmbe_size); |
| 731 | |
| 732 | smc->conn.peer_rmbe_idx = clc->r0.rmbe_idx; |
| 733 | smc->conn.local_tx_ctrl.token = ntohl(clc->r0.rmbe_alert_token); |
| 734 | smc->conn.peer_rmbe_size = bufsize; |
| 735 | atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size); |
| 736 | smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1); |
| 737 | } |
| 738 | |
| 739 | static void smcd_conn_save_peer_info(struct smc_sock *smc, |
| 740 | struct smc_clc_msg_accept_confirm *clc) |
| 741 | { |
| 742 | int bufsize = smc_uncompress_bufsize(clc->d0.dmbe_size); |
| 743 | |
| 744 | smc->conn.peer_rmbe_idx = clc->d0.dmbe_idx; |
| 745 | smc->conn.peer_token = ntohll(clc->d0.token); |
| 746 | /* msg header takes up space in the buffer */ |
| 747 | smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg); |
| 748 | atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size); |
| 749 | smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx; |
| 750 | } |
| 751 | |
| 752 | static void smc_conn_save_peer_info(struct smc_sock *smc, |
| 753 | struct smc_clc_msg_accept_confirm *clc) |
| 754 | { |
| 755 | if (smc->conn.lgr->is_smcd) |
| 756 | smcd_conn_save_peer_info(smc, clc); |
| 757 | else |
| 758 | smcr_conn_save_peer_info(smc, clc); |
| 759 | smc_conn_save_peer_info_fce(smc, clc); |
| 760 | } |
| 761 | |
| 762 | static void smc_link_save_peer_info(struct smc_link *link, |
| 763 | struct smc_clc_msg_accept_confirm *clc, |
| 764 | struct smc_init_info *ini) |
| 765 | { |
| 766 | link->peer_qpn = ntoh24(clc->r0.qpn); |
| 767 | memcpy(link->peer_gid, ini->peer_gid, SMC_GID_SIZE); |
| 768 | memcpy(link->peer_mac, ini->peer_mac, sizeof(link->peer_mac)); |
| 769 | link->peer_psn = ntoh24(clc->r0.psn); |
| 770 | link->peer_mtu = clc->r0.qp_mtu; |
| 771 | } |
| 772 | |
| 773 | static void smc_stat_inc_fback_rsn_cnt(struct smc_sock *smc, |
| 774 | struct smc_stats_fback *fback_arr) |
| 775 | { |
| 776 | int cnt; |
| 777 | |
| 778 | for (cnt = 0; cnt < SMC_MAX_FBACK_RSN_CNT; cnt++) { |
| 779 | if (fback_arr[cnt].fback_code == smc->fallback_rsn) { |
| 780 | fback_arr[cnt].count++; |
| 781 | break; |
| 782 | } |
| 783 | if (!fback_arr[cnt].fback_code) { |
| 784 | fback_arr[cnt].fback_code = smc->fallback_rsn; |
| 785 | fback_arr[cnt].count++; |
| 786 | break; |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | static void smc_stat_fallback(struct smc_sock *smc) |
| 792 | { |
| 793 | struct net *net = sock_net(&smc->sk); |
| 794 | |
| 795 | mutex_lock(&net->smc.mutex_fback_rsn); |
| 796 | if (smc->listen_smc) { |
| 797 | smc_stat_inc_fback_rsn_cnt(smc, net->smc.fback_rsn->srv); |
| 798 | net->smc.fback_rsn->srv_fback_cnt++; |
| 799 | } else { |
| 800 | smc_stat_inc_fback_rsn_cnt(smc, net->smc.fback_rsn->clnt); |
| 801 | net->smc.fback_rsn->clnt_fback_cnt++; |
| 802 | } |
| 803 | mutex_unlock(&net->smc.mutex_fback_rsn); |
| 804 | } |
| 805 | |
| 806 | /* must be called under rcu read lock */ |
| 807 | static void smc_fback_wakeup_waitqueue(struct smc_sock *smc, void *key) |
| 808 | { |
| 809 | struct socket_wq *wq; |
| 810 | __poll_t flags; |
| 811 | |
| 812 | wq = rcu_dereference(smc->sk.sk_wq); |
| 813 | if (!skwq_has_sleeper(wq)) |
| 814 | return; |
| 815 | |
| 816 | /* wake up smc sk->sk_wq */ |
| 817 | if (!key) { |
| 818 | /* sk_state_change */ |
| 819 | wake_up_interruptible_all(&wq->wait); |
| 820 | } else { |
| 821 | flags = key_to_poll(key); |
| 822 | if (flags & (EPOLLIN | EPOLLOUT)) |
| 823 | /* sk_data_ready or sk_write_space */ |
| 824 | wake_up_interruptible_sync_poll(&wq->wait, flags); |
| 825 | else if (flags & EPOLLERR) |
| 826 | /* sk_error_report */ |
| 827 | wake_up_interruptible_poll(&wq->wait, flags); |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | static int smc_fback_mark_woken(wait_queue_entry_t *wait, |
| 832 | unsigned int mode, int sync, void *key) |
| 833 | { |
| 834 | struct smc_mark_woken *mark = |
| 835 | container_of(wait, struct smc_mark_woken, wait_entry); |
| 836 | |
| 837 | mark->woken = true; |
| 838 | mark->key = key; |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | static void smc_fback_forward_wakeup(struct smc_sock *smc, struct sock *clcsk, |
| 843 | void (*clcsock_callback)(struct sock *sk)) |
| 844 | { |
| 845 | struct smc_mark_woken mark = { .woken = false }; |
| 846 | struct socket_wq *wq; |
| 847 | |
| 848 | init_waitqueue_func_entry(&mark.wait_entry, |
| 849 | smc_fback_mark_woken); |
| 850 | rcu_read_lock(); |
| 851 | wq = rcu_dereference(clcsk->sk_wq); |
| 852 | if (!wq) |
| 853 | goto out; |
| 854 | add_wait_queue(sk_sleep(clcsk), &mark.wait_entry); |
| 855 | clcsock_callback(clcsk); |
| 856 | remove_wait_queue(sk_sleep(clcsk), &mark.wait_entry); |
| 857 | |
| 858 | if (mark.woken) |
| 859 | smc_fback_wakeup_waitqueue(smc, mark.key); |
| 860 | out: |
| 861 | rcu_read_unlock(); |
| 862 | } |
| 863 | |
| 864 | static void smc_fback_state_change(struct sock *clcsk) |
| 865 | { |
| 866 | struct smc_sock *smc; |
| 867 | |
| 868 | read_lock_bh(&clcsk->sk_callback_lock); |
| 869 | smc = smc_clcsock_user_data(clcsk); |
| 870 | if (smc) |
| 871 | smc_fback_forward_wakeup(smc, clcsk, |
| 872 | smc->clcsk_state_change); |
| 873 | read_unlock_bh(&clcsk->sk_callback_lock); |
| 874 | } |
| 875 | |
| 876 | static void smc_fback_data_ready(struct sock *clcsk) |
| 877 | { |
| 878 | struct smc_sock *smc; |
| 879 | |
| 880 | read_lock_bh(&clcsk->sk_callback_lock); |
| 881 | smc = smc_clcsock_user_data(clcsk); |
| 882 | if (smc) |
| 883 | smc_fback_forward_wakeup(smc, clcsk, |
| 884 | smc->clcsk_data_ready); |
| 885 | read_unlock_bh(&clcsk->sk_callback_lock); |
| 886 | } |
| 887 | |
| 888 | static void smc_fback_write_space(struct sock *clcsk) |
| 889 | { |
| 890 | struct smc_sock *smc; |
| 891 | |
| 892 | read_lock_bh(&clcsk->sk_callback_lock); |
| 893 | smc = smc_clcsock_user_data(clcsk); |
| 894 | if (smc) |
| 895 | smc_fback_forward_wakeup(smc, clcsk, |
| 896 | smc->clcsk_write_space); |
| 897 | read_unlock_bh(&clcsk->sk_callback_lock); |
| 898 | } |
| 899 | |
| 900 | static void smc_fback_error_report(struct sock *clcsk) |
| 901 | { |
| 902 | struct smc_sock *smc; |
| 903 | |
| 904 | read_lock_bh(&clcsk->sk_callback_lock); |
| 905 | smc = smc_clcsock_user_data(clcsk); |
| 906 | if (smc) |
| 907 | smc_fback_forward_wakeup(smc, clcsk, |
| 908 | smc->clcsk_error_report); |
| 909 | read_unlock_bh(&clcsk->sk_callback_lock); |
| 910 | } |
| 911 | |
| 912 | static void smc_fback_replace_callbacks(struct smc_sock *smc) |
| 913 | { |
| 914 | struct sock *clcsk = smc->clcsock->sk; |
| 915 | |
| 916 | write_lock_bh(&clcsk->sk_callback_lock); |
| 917 | __rcu_assign_sk_user_data_with_flags(clcsk, smc, SK_USER_DATA_NOCOPY); |
| 918 | |
| 919 | smc_clcsock_replace_cb(&clcsk->sk_state_change, smc_fback_state_change, |
| 920 | &smc->clcsk_state_change); |
| 921 | smc_clcsock_replace_cb(&clcsk->sk_data_ready, smc_fback_data_ready, |
| 922 | &smc->clcsk_data_ready); |
| 923 | smc_clcsock_replace_cb(&clcsk->sk_write_space, smc_fback_write_space, |
| 924 | &smc->clcsk_write_space); |
| 925 | smc_clcsock_replace_cb(&clcsk->sk_error_report, smc_fback_error_report, |
| 926 | &smc->clcsk_error_report); |
| 927 | |
| 928 | write_unlock_bh(&clcsk->sk_callback_lock); |
| 929 | } |
| 930 | |
| 931 | static int smc_switch_to_fallback(struct smc_sock *smc, int reason_code) |
| 932 | { |
| 933 | int rc = 0; |
| 934 | |
| 935 | mutex_lock(&smc->clcsock_release_lock); |
| 936 | if (!smc->clcsock) { |
| 937 | rc = -EBADF; |
| 938 | goto out; |
| 939 | } |
| 940 | |
| 941 | smc->use_fallback = true; |
| 942 | smc->fallback_rsn = reason_code; |
| 943 | smc_stat_fallback(smc); |
| 944 | trace_smc_switch_to_fallback(smc, reason_code); |
| 945 | if (smc->sk.sk_socket && smc->sk.sk_socket->file) { |
| 946 | smc->clcsock->file = smc->sk.sk_socket->file; |
| 947 | smc->clcsock->file->private_data = smc->clcsock; |
| 948 | smc->clcsock->wq.fasync_list = |
| 949 | smc->sk.sk_socket->wq.fasync_list; |
| 950 | smc->sk.sk_socket->wq.fasync_list = NULL; |
| 951 | |
| 952 | /* There might be some wait entries remaining |
| 953 | * in smc sk->sk_wq and they should be woken up |
| 954 | * as clcsock's wait queue is woken up. |
| 955 | */ |
| 956 | smc_fback_replace_callbacks(smc); |
| 957 | } |
| 958 | out: |
| 959 | mutex_unlock(&smc->clcsock_release_lock); |
| 960 | return rc; |
| 961 | } |
| 962 | |
| 963 | /* fall back during connect */ |
| 964 | static int smc_connect_fallback(struct smc_sock *smc, int reason_code) |
| 965 | { |
| 966 | struct net *net = sock_net(&smc->sk); |
| 967 | int rc = 0; |
| 968 | |
| 969 | rc = smc_switch_to_fallback(smc, reason_code); |
| 970 | if (rc) { /* fallback fails */ |
| 971 | this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt); |
| 972 | if (smc->sk.sk_state == SMC_INIT) |
| 973 | sock_put(&smc->sk); /* passive closing */ |
| 974 | return rc; |
| 975 | } |
| 976 | smc_copy_sock_settings_to_clc(smc); |
| 977 | smc->connect_nonblock = 0; |
| 978 | if (smc->sk.sk_state == SMC_INIT) |
| 979 | smc->sk.sk_state = SMC_ACTIVE; |
| 980 | return 0; |
| 981 | } |
| 982 | |
| 983 | /* decline and fall back during connect */ |
| 984 | static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code, |
| 985 | u8 version) |
| 986 | { |
| 987 | struct net *net = sock_net(&smc->sk); |
| 988 | int rc; |
| 989 | |
| 990 | if (reason_code < 0) { /* error, fallback is not possible */ |
| 991 | this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt); |
| 992 | if (smc->sk.sk_state == SMC_INIT) |
| 993 | sock_put(&smc->sk); /* passive closing */ |
| 994 | return reason_code; |
| 995 | } |
| 996 | if (reason_code != SMC_CLC_DECL_PEERDECL) { |
| 997 | rc = smc_clc_send_decline(smc, reason_code, version); |
| 998 | if (rc < 0) { |
| 999 | this_cpu_inc(net->smc.smc_stats->clnt_hshake_err_cnt); |
| 1000 | if (smc->sk.sk_state == SMC_INIT) |
| 1001 | sock_put(&smc->sk); /* passive closing */ |
| 1002 | return rc; |
| 1003 | } |
| 1004 | } |
| 1005 | return smc_connect_fallback(smc, reason_code); |
| 1006 | } |
| 1007 | |
| 1008 | static void smc_conn_abort(struct smc_sock *smc, int local_first) |
| 1009 | { |
| 1010 | struct smc_connection *conn = &smc->conn; |
| 1011 | struct smc_link_group *lgr = conn->lgr; |
| 1012 | bool lgr_valid = false; |
| 1013 | |
| 1014 | if (smc_conn_lgr_valid(conn)) |
| 1015 | lgr_valid = true; |
| 1016 | |
| 1017 | smc_conn_free(conn); |
| 1018 | if (local_first && lgr_valid) |
| 1019 | smc_lgr_cleanup_early(lgr); |
| 1020 | } |
| 1021 | |
| 1022 | /* check if there is a rdma device available for this connection. */ |
| 1023 | /* called for connect and listen */ |
| 1024 | static int smc_find_rdma_device(struct smc_sock *smc, struct smc_init_info *ini) |
| 1025 | { |
| 1026 | /* PNET table look up: search active ib_device and port |
| 1027 | * within same PNETID that also contains the ethernet device |
| 1028 | * used for the internal TCP socket |
| 1029 | */ |
| 1030 | smc_pnet_find_roce_resource(smc->clcsock->sk, ini); |
| 1031 | if (!ini->check_smcrv2 && !ini->ib_dev) |
| 1032 | return SMC_CLC_DECL_NOSMCRDEV; |
| 1033 | if (ini->check_smcrv2 && !ini->smcrv2.ib_dev_v2) |
| 1034 | return SMC_CLC_DECL_NOSMCRDEV; |
| 1035 | return 0; |
| 1036 | } |
| 1037 | |
| 1038 | /* check if there is an ISM device available for this connection. */ |
| 1039 | /* called for connect and listen */ |
| 1040 | static int smc_find_ism_device(struct smc_sock *smc, struct smc_init_info *ini) |
| 1041 | { |
| 1042 | /* Find ISM device with same PNETID as connecting interface */ |
| 1043 | smc_pnet_find_ism_resource(smc->clcsock->sk, ini); |
| 1044 | if (!ini->ism_dev[0]) |
| 1045 | return SMC_CLC_DECL_NOSMCDDEV; |
| 1046 | else |
| 1047 | ini->ism_chid[0] = smc_ism_get_chid(ini->ism_dev[0]); |
| 1048 | return 0; |
| 1049 | } |
| 1050 | |
| 1051 | /* is chid unique for the ism devices that are already determined? */ |
| 1052 | static bool smc_find_ism_v2_is_unique_chid(u16 chid, struct smc_init_info *ini, |
| 1053 | int cnt) |
| 1054 | { |
| 1055 | int i = (!ini->ism_dev[0]) ? 1 : 0; |
| 1056 | |
| 1057 | for (; i < cnt; i++) |
| 1058 | if (ini->ism_chid[i] == chid) |
| 1059 | return false; |
| 1060 | return true; |
| 1061 | } |
| 1062 | |
| 1063 | /* determine possible V2 ISM devices (either without PNETID or with PNETID plus |
| 1064 | * PNETID matching net_device) |
| 1065 | */ |
| 1066 | static int smc_find_ism_v2_device_clnt(struct smc_sock *smc, |
| 1067 | struct smc_init_info *ini) |
| 1068 | { |
| 1069 | int rc = SMC_CLC_DECL_NOSMCDDEV; |
| 1070 | struct smcd_dev *smcd; |
| 1071 | int i = 1, entry = 1; |
| 1072 | bool is_emulated; |
| 1073 | u16 chid; |
| 1074 | |
| 1075 | if (smcd_indicated(ini->smc_type_v1)) |
| 1076 | rc = 0; /* already initialized for V1 */ |
| 1077 | mutex_lock(&smcd_dev_list.mutex); |
| 1078 | list_for_each_entry(smcd, &smcd_dev_list.list, list) { |
| 1079 | if (smcd->going_away || smcd == ini->ism_dev[0]) |
| 1080 | continue; |
| 1081 | chid = smc_ism_get_chid(smcd); |
| 1082 | if (!smc_find_ism_v2_is_unique_chid(chid, ini, i)) |
| 1083 | continue; |
| 1084 | is_emulated = __smc_ism_is_emulated(chid); |
| 1085 | if (!smc_pnet_is_pnetid_set(smcd->pnetid) || |
| 1086 | smc_pnet_is_ndev_pnetid(sock_net(&smc->sk), smcd->pnetid)) { |
| 1087 | if (is_emulated && entry == SMCD_CLC_MAX_V2_GID_ENTRIES) |
| 1088 | /* It's the last GID-CHID entry left in CLC |
| 1089 | * Proposal SMC-Dv2 extension, but an Emulated- |
| 1090 | * ISM device will take two entries. So give |
| 1091 | * up it and try the next potential ISM device. |
| 1092 | */ |
| 1093 | continue; |
| 1094 | ini->ism_dev[i] = smcd; |
| 1095 | ini->ism_chid[i] = chid; |
| 1096 | ini->is_smcd = true; |
| 1097 | rc = 0; |
| 1098 | i++; |
| 1099 | entry = is_emulated ? entry + 2 : entry + 1; |
| 1100 | if (entry > SMCD_CLC_MAX_V2_GID_ENTRIES) |
| 1101 | break; |
| 1102 | } |
| 1103 | } |
| 1104 | mutex_unlock(&smcd_dev_list.mutex); |
| 1105 | ini->ism_offered_cnt = i - 1; |
| 1106 | if (!ini->ism_dev[0] && !ini->ism_dev[1]) |
| 1107 | ini->smcd_version = 0; |
| 1108 | |
| 1109 | return rc; |
| 1110 | } |
| 1111 | |
| 1112 | /* Check for VLAN ID and register it on ISM device just for CLC handshake */ |
| 1113 | static int smc_connect_ism_vlan_setup(struct smc_init_info *ini) |
| 1114 | { |
| 1115 | if (ini->vlan_id && smc_ism_get_vlan(ini->ism_dev[0], ini->vlan_id)) |
| 1116 | return SMC_CLC_DECL_ISMVLANERR; |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
| 1120 | static int smc_find_proposal_devices(struct smc_sock *smc, |
| 1121 | struct smc_init_info *ini) |
| 1122 | { |
| 1123 | int rc = 0; |
| 1124 | |
| 1125 | /* check if there is an ism device available */ |
| 1126 | if (!(ini->smcd_version & SMC_V1) || |
| 1127 | smc_find_ism_device(smc, ini) || |
| 1128 | smc_connect_ism_vlan_setup(ini)) |
| 1129 | ini->smcd_version &= ~SMC_V1; |
| 1130 | /* else ISM V1 is supported for this connection */ |
| 1131 | |
| 1132 | /* check if there is an rdma device available */ |
| 1133 | if (!(ini->smcr_version & SMC_V1) || |
| 1134 | smc_find_rdma_device(smc, ini)) |
| 1135 | ini->smcr_version &= ~SMC_V1; |
| 1136 | /* else RDMA is supported for this connection */ |
| 1137 | |
| 1138 | ini->smc_type_v1 = smc_indicated_type(ini->smcd_version & SMC_V1, |
| 1139 | ini->smcr_version & SMC_V1); |
| 1140 | |
| 1141 | /* check if there is an ism v2 device available */ |
| 1142 | if (!(ini->smcd_version & SMC_V2) || |
| 1143 | !smc_ism_is_v2_capable() || |
| 1144 | smc_find_ism_v2_device_clnt(smc, ini)) |
| 1145 | ini->smcd_version &= ~SMC_V2; |
| 1146 | |
| 1147 | /* check if there is an rdma v2 device available */ |
| 1148 | ini->check_smcrv2 = true; |
| 1149 | ini->smcrv2.saddr = smc->clcsock->sk->sk_rcv_saddr; |
| 1150 | if (!(ini->smcr_version & SMC_V2) || |
| 1151 | #if IS_ENABLED(CONFIG_IPV6) |
| 1152 | (smc->clcsock->sk->sk_family == AF_INET6 && |
| 1153 | !ipv6_addr_v4mapped(&smc->clcsock->sk->sk_v6_rcv_saddr)) || |
| 1154 | #endif |
| 1155 | !smc_clc_ueid_count() || |
| 1156 | smc_find_rdma_device(smc, ini)) |
| 1157 | ini->smcr_version &= ~SMC_V2; |
| 1158 | ini->check_smcrv2 = false; |
| 1159 | |
| 1160 | ini->smc_type_v2 = smc_indicated_type(ini->smcd_version & SMC_V2, |
| 1161 | ini->smcr_version & SMC_V2); |
| 1162 | |
| 1163 | /* if neither ISM nor RDMA are supported, fallback */ |
| 1164 | if (ini->smc_type_v1 == SMC_TYPE_N && ini->smc_type_v2 == SMC_TYPE_N) |
| 1165 | rc = SMC_CLC_DECL_NOSMCDEV; |
| 1166 | |
| 1167 | return rc; |
| 1168 | } |
| 1169 | |
| 1170 | /* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is |
| 1171 | * used, the VLAN ID will be registered again during the connection setup. |
| 1172 | */ |
| 1173 | static int smc_connect_ism_vlan_cleanup(struct smc_init_info *ini) |
| 1174 | { |
| 1175 | if (!smcd_indicated(ini->smc_type_v1)) |
| 1176 | return 0; |
| 1177 | if (ini->vlan_id && smc_ism_put_vlan(ini->ism_dev[0], ini->vlan_id)) |
| 1178 | return SMC_CLC_DECL_CNFERR; |
| 1179 | return 0; |
| 1180 | } |
| 1181 | |
| 1182 | #define SMC_CLC_MAX_ACCEPT_LEN \ |
| 1183 | (sizeof(struct smc_clc_msg_accept_confirm) + \ |
| 1184 | sizeof(struct smc_clc_first_contact_ext_v2x) + \ |
| 1185 | sizeof(struct smc_clc_msg_trail)) |
| 1186 | |
| 1187 | /* CLC handshake during connect */ |
| 1188 | static int smc_connect_clc(struct smc_sock *smc, |
| 1189 | struct smc_clc_msg_accept_confirm *aclc, |
| 1190 | struct smc_init_info *ini) |
| 1191 | { |
| 1192 | int rc = 0; |
| 1193 | |
| 1194 | /* do inband token exchange */ |
| 1195 | rc = smc_clc_send_proposal(smc, ini); |
| 1196 | if (rc) |
| 1197 | return rc; |
| 1198 | /* receive SMC Accept CLC message */ |
| 1199 | return smc_clc_wait_msg(smc, aclc, SMC_CLC_MAX_ACCEPT_LEN, |
| 1200 | SMC_CLC_ACCEPT, CLC_WAIT_TIME); |
| 1201 | } |
| 1202 | |
| 1203 | void smc_fill_gid_list(struct smc_link_group *lgr, |
| 1204 | struct smc_gidlist *gidlist, |
| 1205 | struct smc_ib_device *known_dev, u8 *known_gid) |
| 1206 | { |
| 1207 | struct smc_init_info *alt_ini = NULL; |
| 1208 | |
| 1209 | memset(gidlist, 0, sizeof(*gidlist)); |
| 1210 | memcpy(gidlist->list[gidlist->len++], known_gid, SMC_GID_SIZE); |
| 1211 | |
| 1212 | alt_ini = kzalloc_obj(*alt_ini); |
| 1213 | if (!alt_ini) |
| 1214 | goto out; |
| 1215 | |
| 1216 | alt_ini->vlan_id = lgr->vlan_id; |
| 1217 | alt_ini->check_smcrv2 = true; |
| 1218 | alt_ini->smcrv2.saddr = lgr->saddr; |
| 1219 | smc_pnet_find_alt_roce(lgr, alt_ini, known_dev); |
| 1220 | |
| 1221 | if (!alt_ini->smcrv2.ib_dev_v2) |
| 1222 | goto out; |
| 1223 | |
| 1224 | memcpy(gidlist->list[gidlist->len++], alt_ini->smcrv2.ib_gid_v2, |
| 1225 | SMC_GID_SIZE); |
| 1226 | |
| 1227 | out: |
| 1228 | kfree(alt_ini); |
| 1229 | } |
| 1230 | |
| 1231 | static int smc_connect_rdma_v2_prepare(struct smc_sock *smc, |
| 1232 | struct smc_clc_msg_accept_confirm *aclc, |
| 1233 | struct smc_init_info *ini) |
| 1234 | { |
| 1235 | struct smc_clc_first_contact_ext *fce = |
| 1236 | smc_get_clc_first_contact_ext(aclc, false); |
| 1237 | struct net *net = sock_net(&smc->sk); |
| 1238 | int rc; |
| 1239 | |
| 1240 | if (!ini->first_contact_peer || aclc->hdr.version == SMC_V1) |
| 1241 | return 0; |
| 1242 | |
| 1243 | if (fce->v2_direct) { |
| 1244 | memcpy(ini->smcrv2.nexthop_mac, &aclc->r0.lcl.mac, ETH_ALEN); |
| 1245 | ini->smcrv2.uses_gateway = false; |
| 1246 | } else { |
| 1247 | if (smc_ib_find_route(net, smc->clcsock->sk->sk_rcv_saddr, |
| 1248 | smc_ib_gid_to_ipv4(aclc->r0.lcl.gid), |
| 1249 | ini->smcrv2.nexthop_mac, |
| 1250 | &ini->smcrv2.uses_gateway)) |
| 1251 | return SMC_CLC_DECL_NOROUTE; |
| 1252 | if (!ini->smcrv2.uses_gateway) { |
| 1253 | /* mismatch: peer claims indirect, but its direct */ |
| 1254 | return SMC_CLC_DECL_NOINDIRECT; |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | ini->release_nr = fce->release; |
| 1259 | rc = smc_clc_clnt_v2x_features_validate(fce, ini); |
| 1260 | if (rc) |
| 1261 | return rc; |
| 1262 | |
| 1263 | return 0; |
| 1264 | } |
| 1265 | |
| 1266 | /* setup for RDMA connection of client */ |
| 1267 | static int smc_connect_rdma(struct smc_sock *smc, |
| 1268 | struct smc_clc_msg_accept_confirm *aclc, |
| 1269 | struct smc_init_info *ini) |
| 1270 | { |
| 1271 | int i, reason_code = 0; |
| 1272 | struct smc_link *link; |
| 1273 | u8 *eid = NULL; |
| 1274 | |
| 1275 | ini->is_smcd = false; |
| 1276 | ini->ib_clcqpn = ntoh24(aclc->r0.qpn); |
| 1277 | ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK; |
| 1278 | memcpy(ini->peer_systemid, aclc->r0.lcl.id_for_peer, SMC_SYSTEMID_LEN); |
| 1279 | memcpy(ini->peer_gid, aclc->r0.lcl.gid, SMC_GID_SIZE); |
| 1280 | memcpy(ini->peer_mac, aclc->r0.lcl.mac, ETH_ALEN); |
| 1281 | ini->max_conns = SMC_CONN_PER_LGR_MAX; |
| 1282 | ini->max_links = SMC_LINKS_ADD_LNK_MAX; |
| 1283 | |
| 1284 | reason_code = smc_connect_rdma_v2_prepare(smc, aclc, ini); |
| 1285 | if (reason_code) |
| 1286 | return reason_code; |
| 1287 | |
| 1288 | mutex_lock(&smc_client_lgr_pending); |
| 1289 | reason_code = smc_conn_create(smc, ini); |
| 1290 | if (reason_code) { |
| 1291 | mutex_unlock(&smc_client_lgr_pending); |
| 1292 | return reason_code; |
| 1293 | } |
| 1294 | |
| 1295 | smc_conn_save_peer_info(smc, aclc); |
| 1296 | |
| 1297 | if (ini->first_contact_local) { |
| 1298 | link = smc->conn.lnk; |
| 1299 | } else { |
| 1300 | /* set link that was assigned by server */ |
| 1301 | link = NULL; |
| 1302 | for (i = 0; i < SMC_LINKS_PER_LGR_MAX; i++) { |
| 1303 | struct smc_link *l = &smc->conn.lgr->lnk[i]; |
| 1304 | |
| 1305 | if (l->peer_qpn == ntoh24(aclc->r0.qpn) && |
| 1306 | !memcmp(l->peer_gid, &aclc->r0.lcl.gid, |
| 1307 | SMC_GID_SIZE) && |
| 1308 | (aclc->hdr.version > SMC_V1 || |
| 1309 | !memcmp(l->peer_mac, &aclc->r0.lcl.mac, |
| 1310 | sizeof(l->peer_mac)))) { |
| 1311 | link = l; |
| 1312 | break; |
| 1313 | } |
| 1314 | } |
| 1315 | if (!link) { |
| 1316 | reason_code = SMC_CLC_DECL_NOSRVLINK; |
| 1317 | goto connect_abort; |
| 1318 | } |
| 1319 | smc_switch_link_and_count(&smc->conn, link); |
| 1320 | } |
| 1321 | |
| 1322 | /* create send buffer and rmb */ |
| 1323 | if (smc_buf_create(smc, false)) { |
| 1324 | reason_code = SMC_CLC_DECL_MEM; |
| 1325 | goto connect_abort; |
| 1326 | } |
| 1327 | |
| 1328 | if (ini->first_contact_local) |
| 1329 | smc_link_save_peer_info(link, aclc, ini); |
| 1330 | |
| 1331 | if (smc_rmb_rtoken_handling(&smc->conn, link, aclc)) { |
| 1332 | reason_code = SMC_CLC_DECL_ERR_RTOK; |
| 1333 | goto connect_abort; |
| 1334 | } |
| 1335 | |
| 1336 | smc_rx_init(smc); |
| 1337 | |
| 1338 | if (ini->first_contact_local) { |
| 1339 | if (smc_ib_ready_link(link)) { |
| 1340 | reason_code = SMC_CLC_DECL_ERR_RDYLNK; |
| 1341 | goto connect_abort; |
| 1342 | } |
| 1343 | } else { |
| 1344 | /* reg sendbufs if they were vzalloced */ |
| 1345 | if (smc->conn.sndbuf_desc->is_vm) { |
| 1346 | if (smcr_lgr_reg_sndbufs(link, smc->conn.sndbuf_desc)) { |
| 1347 | reason_code = SMC_CLC_DECL_ERR_REGBUF; |
| 1348 | goto connect_abort; |
| 1349 | } |
| 1350 | } |
| 1351 | if (smcr_lgr_reg_rmbs(link, smc->conn.rmb_desc)) { |
| 1352 | reason_code = SMC_CLC_DECL_ERR_REGBUF; |
| 1353 | goto connect_abort; |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | if (aclc->hdr.version > SMC_V1) { |
| 1358 | eid = aclc->r1.eid; |
| 1359 | if (ini->first_contact_local) |
| 1360 | smc_fill_gid_list(link->lgr, &ini->smcrv2.gidlist, |
| 1361 | link->smcibdev, link->gid); |
| 1362 | } |
| 1363 | |
| 1364 | reason_code = smc_clc_send_confirm(smc, ini->first_contact_local, |
| 1365 | aclc->hdr.version, eid, ini); |
| 1366 | if (reason_code) |
| 1367 | goto connect_abort; |
| 1368 | |
| 1369 | smc_tx_init(smc); |
| 1370 | |
| 1371 | if (ini->first_contact_local) { |
| 1372 | /* QP confirmation over RoCE fabric */ |
| 1373 | smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK); |
| 1374 | reason_code = smcr_clnt_conf_first_link(smc); |
| 1375 | smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl); |
| 1376 | if (reason_code) |
| 1377 | goto connect_abort; |
| 1378 | } |
| 1379 | mutex_unlock(&smc_client_lgr_pending); |
| 1380 | |
| 1381 | smc_copy_sock_settings_to_clc(smc); |
| 1382 | smc->connect_nonblock = 0; |
| 1383 | if (smc->sk.sk_state == SMC_INIT) |
| 1384 | smc->sk.sk_state = SMC_ACTIVE; |
| 1385 | |
| 1386 | return 0; |
| 1387 | connect_abort: |
| 1388 | smc_conn_abort(smc, ini->first_contact_local); |
| 1389 | mutex_unlock(&smc_client_lgr_pending); |
| 1390 | smc->connect_nonblock = 0; |
| 1391 | |
| 1392 | return reason_code; |
| 1393 | } |
| 1394 | |
| 1395 | /* The server has chosen one of the proposed ISM devices for the communication. |
| 1396 | * Determine from the CHID of the received CLC ACCEPT the ISM device chosen. |
| 1397 | */ |
| 1398 | static int |
| 1399 | smc_v2_determine_accepted_chid(struct smc_clc_msg_accept_confirm *aclc, |
| 1400 | struct smc_init_info *ini) |
| 1401 | { |
| 1402 | int i; |
| 1403 | |
| 1404 | for (i = 0; i < ini->ism_offered_cnt + 1; i++) { |
| 1405 | if (ini->ism_dev[i] && |
| 1406 | ini->ism_chid[i] == ntohs(aclc->d1.chid)) { |
| 1407 | ini->ism_selected = i; |
| 1408 | return 0; |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | return -EPROTO; |
| 1413 | } |
| 1414 | |
| 1415 | /* setup for ISM connection of client */ |
| 1416 | static int smc_connect_ism(struct smc_sock *smc, |
| 1417 | struct smc_clc_msg_accept_confirm *aclc, |
| 1418 | struct smc_init_info *ini) |
| 1419 | { |
| 1420 | u8 *eid = NULL; |
| 1421 | int rc = 0; |
| 1422 | |
| 1423 | ini->is_smcd = true; |
| 1424 | ini->first_contact_peer = aclc->hdr.typev2 & SMC_FIRST_CONTACT_MASK; |
| 1425 | |
| 1426 | if (aclc->hdr.version == SMC_V2) { |
| 1427 | if (ini->first_contact_peer) { |
| 1428 | struct smc_clc_first_contact_ext *fce = |
| 1429 | smc_get_clc_first_contact_ext(aclc, true); |
| 1430 | |
| 1431 | ini->release_nr = fce->release; |
| 1432 | rc = smc_clc_clnt_v2x_features_validate(fce, ini); |
| 1433 | if (rc) |
| 1434 | return rc; |
| 1435 | } |
| 1436 | |
| 1437 | rc = smc_v2_determine_accepted_chid(aclc, ini); |
| 1438 | if (rc) |
| 1439 | return rc; |
| 1440 | |
| 1441 | if (__smc_ism_is_emulated(ini->ism_chid[ini->ism_selected])) |
| 1442 | ini->ism_peer_gid[ini->ism_selected].gid_ext = |
| 1443 | ntohll(aclc->d1.gid_ext); |
| 1444 | /* for non-Emulated-ISM devices, peer gid_ext remains 0. */ |
| 1445 | } |
| 1446 | ini->ism_peer_gid[ini->ism_selected].gid = ntohll(aclc->d0.gid); |
| 1447 | |
| 1448 | /* there is only one lgr role for SMC-D; use server lock */ |
| 1449 | mutex_lock(&smc_server_lgr_pending); |
| 1450 | rc = smc_conn_create(smc, ini); |
| 1451 | if (rc) { |
| 1452 | mutex_unlock(&smc_server_lgr_pending); |
| 1453 | return rc; |
| 1454 | } |
| 1455 | |
| 1456 | /* Create send and receive buffers */ |
| 1457 | rc = smc_buf_create(smc, true); |
| 1458 | if (rc) { |
| 1459 | rc = (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB : SMC_CLC_DECL_MEM; |
| 1460 | goto connect_abort; |
| 1461 | } |
| 1462 | |
| 1463 | smc_conn_save_peer_info(smc, aclc); |
| 1464 | |
| 1465 | if (smc_ism_support_dmb_nocopy(smc->conn.lgr->smcd)) { |
| 1466 | rc = smcd_buf_attach(smc); |
| 1467 | if (rc) { |
| 1468 | rc = SMC_CLC_DECL_MEM; /* try to fallback */ |
| 1469 | goto connect_abort; |
| 1470 | } |
| 1471 | } |
| 1472 | smc_rx_init(smc); |
| 1473 | smc_tx_init(smc); |
| 1474 | |
| 1475 | if (aclc->hdr.version > SMC_V1) |
| 1476 | eid = aclc->d1.eid; |
| 1477 | |
| 1478 | rc = smc_clc_send_confirm(smc, ini->first_contact_local, |
| 1479 | aclc->hdr.version, eid, ini); |
| 1480 | if (rc) |
| 1481 | goto connect_abort; |
| 1482 | mutex_unlock(&smc_server_lgr_pending); |
| 1483 | |
| 1484 | smc_copy_sock_settings_to_clc(smc); |
| 1485 | smc->connect_nonblock = 0; |
| 1486 | if (smc->sk.sk_state == SMC_INIT) |
| 1487 | smc->sk.sk_state = SMC_ACTIVE; |
| 1488 | |
| 1489 | return 0; |
| 1490 | connect_abort: |
| 1491 | smc_conn_abort(smc, ini->first_contact_local); |
| 1492 | mutex_unlock(&smc_server_lgr_pending); |
| 1493 | smc->connect_nonblock = 0; |
| 1494 | |
| 1495 | return rc; |
| 1496 | } |
| 1497 | |
| 1498 | /* check if received accept type and version matches a proposed one */ |
| 1499 | static int smc_connect_check_aclc(struct smc_init_info *ini, |
| 1500 | struct smc_clc_msg_accept_confirm *aclc) |
| 1501 | { |
| 1502 | if (aclc->hdr.version >= SMC_V2) { |
| 1503 | if ((aclc->hdr.typev1 == SMC_TYPE_R && |
| 1504 | !smcr_indicated(ini->smc_type_v2)) || |
| 1505 | (aclc->hdr.typev1 == SMC_TYPE_D && |
| 1506 | !smcd_indicated(ini->smc_type_v2))) |
| 1507 | return SMC_CLC_DECL_MODEUNSUPP; |
| 1508 | } else { |
| 1509 | if ((aclc->hdr.typev1 == SMC_TYPE_R && |
| 1510 | !smcr_indicated(ini->smc_type_v1)) || |
| 1511 | (aclc->hdr.typev1 == SMC_TYPE_D && |
| 1512 | !smcd_indicated(ini->smc_type_v1))) |
| 1513 | return SMC_CLC_DECL_MODEUNSUPP; |
| 1514 | } |
| 1515 | |
| 1516 | return 0; |
| 1517 | } |
| 1518 | |
| 1519 | /* perform steps before actually connecting */ |
| 1520 | static int __smc_connect(struct smc_sock *smc) |
| 1521 | { |
| 1522 | u8 version = smc_ism_is_v2_capable() ? SMC_V2 : SMC_V1; |
| 1523 | struct smc_clc_msg_accept_confirm *aclc; |
| 1524 | struct smc_init_info *ini = NULL; |
| 1525 | u8 *buf = NULL; |
| 1526 | int rc = 0; |
| 1527 | |
| 1528 | if (smc->use_fallback) |
| 1529 | return smc_connect_fallback(smc, smc->fallback_rsn); |
| 1530 | |
| 1531 | /* if peer has not signalled SMC-capability, fall back */ |
| 1532 | if (!tcp_sk(smc->clcsock->sk)->syn_smc) |
| 1533 | return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC); |
| 1534 | |
| 1535 | /* IPSec connections opt out of SMC optimizations */ |
| 1536 | if (using_ipsec(smc)) |
| 1537 | return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC, |
| 1538 | version); |
| 1539 | |
| 1540 | ini = kzalloc_obj(*ini); |
| 1541 | if (!ini) |
| 1542 | return smc_connect_decline_fallback(smc, SMC_CLC_DECL_MEM, |
| 1543 | version); |
| 1544 | |
| 1545 | ini->smcd_version = SMC_V1 | SMC_V2; |
| 1546 | ini->smcr_version = SMC_V1 | SMC_V2; |
| 1547 | ini->smc_type_v1 = SMC_TYPE_B; |
| 1548 | ini->smc_type_v2 = SMC_TYPE_B; |
| 1549 | |
| 1550 | /* get vlan id from IP device */ |
| 1551 | if (smc_vlan_by_tcpsk(smc->clcsock, ini)) { |
| 1552 | ini->smcd_version &= ~SMC_V1; |
| 1553 | ini->smcr_version = 0; |
| 1554 | ini->smc_type_v1 = SMC_TYPE_N; |
| 1555 | } |
| 1556 | |
| 1557 | rc = smc_find_proposal_devices(smc, ini); |
| 1558 | if (rc) |
| 1559 | goto fallback; |
| 1560 | |
| 1561 | buf = kzalloc(SMC_CLC_MAX_ACCEPT_LEN, GFP_KERNEL); |
| 1562 | if (!buf) { |
| 1563 | rc = SMC_CLC_DECL_MEM; |
| 1564 | goto fallback; |
| 1565 | } |
| 1566 | aclc = (struct smc_clc_msg_accept_confirm *)buf; |
| 1567 | |
| 1568 | /* perform CLC handshake */ |
| 1569 | rc = smc_connect_clc(smc, aclc, ini); |
| 1570 | if (rc) { |
| 1571 | /* -EAGAIN on timeout, see tcp_recvmsg() */ |
| 1572 | if (rc == -EAGAIN) { |
| 1573 | rc = -ETIMEDOUT; |
| 1574 | smc->sk.sk_err = ETIMEDOUT; |
| 1575 | } |
| 1576 | goto vlan_cleanup; |
| 1577 | } |
| 1578 | |
| 1579 | /* check if smc modes and versions of CLC proposal and accept match */ |
| 1580 | rc = smc_connect_check_aclc(ini, aclc); |
| 1581 | version = aclc->hdr.version == SMC_V1 ? SMC_V1 : SMC_V2; |
| 1582 | if (rc) |
| 1583 | goto vlan_cleanup; |
| 1584 | |
| 1585 | /* depending on previous steps, connect using rdma or ism */ |
| 1586 | if (aclc->hdr.typev1 == SMC_TYPE_R) { |
| 1587 | ini->smcr_version = version; |
| 1588 | rc = smc_connect_rdma(smc, aclc, ini); |
| 1589 | } else if (aclc->hdr.typev1 == SMC_TYPE_D) { |
| 1590 | ini->smcd_version = version; |
| 1591 | rc = smc_connect_ism(smc, aclc, ini); |
| 1592 | } |
| 1593 | if (rc) |
| 1594 | goto vlan_cleanup; |
| 1595 | |
| 1596 | SMC_STAT_CLNT_SUCC_INC(sock_net(smc->clcsock->sk), aclc); |
| 1597 | smc_connect_ism_vlan_cleanup(ini); |
| 1598 | kfree(buf); |
| 1599 | kfree(ini); |
| 1600 | return 0; |
| 1601 | |
| 1602 | vlan_cleanup: |
| 1603 | smc_connect_ism_vlan_cleanup(ini); |
| 1604 | kfree(buf); |
| 1605 | fallback: |
| 1606 | kfree(ini); |
| 1607 | return smc_connect_decline_fallback(smc, rc, version); |
| 1608 | } |
| 1609 | |
| 1610 | static void smc_connect_work(struct work_struct *work) |
| 1611 | { |
| 1612 | struct smc_sock *smc = container_of(work, struct smc_sock, |
| 1613 | connect_work); |
| 1614 | long timeo = READ_ONCE(smc->sk.sk_sndtimeo); |
| 1615 | int rc = 0; |
| 1616 | |
| 1617 | if (!timeo) |
| 1618 | timeo = MAX_SCHEDULE_TIMEOUT; |
| 1619 | lock_sock(smc->clcsock->sk); |
| 1620 | if (smc->clcsock->sk->sk_err) { |
| 1621 | smc->sk.sk_err = smc->clcsock->sk->sk_err; |
| 1622 | } else if ((1 << smc->clcsock->sk->sk_state) & |
| 1623 | (TCPF_SYN_SENT | TCPF_SYN_RECV)) { |
| 1624 | rc = sk_stream_wait_connect(smc->clcsock->sk, &timeo); |
| 1625 | if ((rc == -EPIPE) && |
| 1626 | ((1 << smc->clcsock->sk->sk_state) & |
| 1627 | (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT))) |
| 1628 | rc = 0; |
| 1629 | } |
| 1630 | release_sock(smc->clcsock->sk); |
| 1631 | lock_sock(&smc->sk); |
| 1632 | if (rc != 0 || smc->sk.sk_err) { |
| 1633 | smc->sk.sk_state = SMC_CLOSED; |
| 1634 | if (!smc->sk.sk_err) |
| 1635 | smc->sk.sk_err = (rc == -EAGAIN) ? EPIPE : -rc; |
| 1636 | sock_put(&smc->sk); /* passive closing */ |
| 1637 | goto out; |
| 1638 | } |
| 1639 | |
| 1640 | rc = __smc_connect(smc); |
| 1641 | if (rc < 0) |
| 1642 | smc->sk.sk_err = -rc; |
| 1643 | |
| 1644 | out: |
| 1645 | if (!sock_flag(&smc->sk, SOCK_DEAD)) { |
| 1646 | if (smc->sk.sk_err) { |
| 1647 | smc->sk.sk_state_change(&smc->sk); |
| 1648 | } else { /* allow polling before and after fallback decision */ |
| 1649 | smc->clcsock->sk->sk_write_space(smc->clcsock->sk); |
| 1650 | smc->sk.sk_write_space(&smc->sk); |
| 1651 | } |
| 1652 | } |
| 1653 | release_sock(&smc->sk); |
| 1654 | } |
| 1655 | |
| 1656 | int smc_connect(struct socket *sock, struct sockaddr_unsized *addr, |
| 1657 | int alen, int flags) |
| 1658 | { |
| 1659 | struct sock *sk = sock->sk; |
| 1660 | struct smc_sock *smc; |
| 1661 | int rc = -EINVAL; |
| 1662 | |
| 1663 | smc = smc_sk(sk); |
| 1664 | |
| 1665 | /* separate smc parameter checking to be safe */ |
| 1666 | if (alen < sizeof(addr->sa_family)) |
| 1667 | goto out_err; |
| 1668 | if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6) |
| 1669 | goto out_err; |
| 1670 | |
| 1671 | lock_sock(sk); |
| 1672 | switch (sock->state) { |
| 1673 | default: |
| 1674 | rc = -EINVAL; |
| 1675 | goto out; |
| 1676 | case SS_CONNECTED: |
| 1677 | rc = sk->sk_state == SMC_ACTIVE ? -EISCONN : -EINVAL; |
| 1678 | goto out; |
| 1679 | case SS_CONNECTING: |
| 1680 | if (sk->sk_state == SMC_ACTIVE) |
| 1681 | goto connected; |
| 1682 | break; |
| 1683 | case SS_UNCONNECTED: |
| 1684 | sock->state = SS_CONNECTING; |
| 1685 | break; |
| 1686 | } |
| 1687 | |
| 1688 | switch (sk->sk_state) { |
| 1689 | default: |
| 1690 | goto out; |
| 1691 | case SMC_CLOSED: |
| 1692 | rc = sock_error(sk) ? : -ECONNABORTED; |
| 1693 | sock->state = SS_UNCONNECTED; |
| 1694 | goto out; |
| 1695 | case SMC_ACTIVE: |
| 1696 | rc = -EISCONN; |
| 1697 | goto out; |
| 1698 | case SMC_INIT: |
| 1699 | break; |
| 1700 | } |
| 1701 | |
| 1702 | smc_copy_sock_settings_to_clc(smc); |
| 1703 | tcp_sk(smc->clcsock->sk)->syn_smc = 1; |
| 1704 | if (smc->connect_nonblock) { |
| 1705 | rc = -EALREADY; |
| 1706 | goto out; |
| 1707 | } |
| 1708 | rc = kernel_connect(smc->clcsock, (struct sockaddr_unsized *)addr, alen, flags); |
| 1709 | if (rc && rc != -EINPROGRESS) |
| 1710 | goto out; |
| 1711 | |
| 1712 | if (smc->use_fallback) { |
| 1713 | sock->state = rc ? SS_CONNECTING : SS_CONNECTED; |
| 1714 | goto out; |
| 1715 | } |
| 1716 | sock_hold(&smc->sk); /* sock put in passive closing */ |
| 1717 | if (flags & O_NONBLOCK) { |
| 1718 | if (queue_work(smc_hs_wq, &smc->connect_work)) |
| 1719 | smc->connect_nonblock = 1; |
| 1720 | rc = -EINPROGRESS; |
| 1721 | goto out; |
| 1722 | } else { |
| 1723 | rc = __smc_connect(smc); |
| 1724 | if (rc < 0) |
| 1725 | goto out; |
| 1726 | } |
| 1727 | |
| 1728 | connected: |
| 1729 | rc = 0; |
| 1730 | sock->state = SS_CONNECTED; |
| 1731 | out: |
| 1732 | release_sock(sk); |
| 1733 | out_err: |
| 1734 | return rc; |
| 1735 | } |
| 1736 | |
| 1737 | static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc) |
| 1738 | { |
| 1739 | struct socket *new_clcsock = NULL; |
| 1740 | struct sock *lsk = &lsmc->sk; |
| 1741 | struct sock *new_sk; |
| 1742 | int rc = -EINVAL; |
| 1743 | |
| 1744 | release_sock(lsk); |
| 1745 | new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol); |
| 1746 | if (!new_sk) { |
| 1747 | rc = -ENOMEM; |
| 1748 | lsk->sk_err = ENOMEM; |
| 1749 | *new_smc = NULL; |
| 1750 | lock_sock(lsk); |
| 1751 | goto out; |
| 1752 | } |
| 1753 | *new_smc = smc_sk(new_sk); |
| 1754 | |
| 1755 | mutex_lock(&lsmc->clcsock_release_lock); |
| 1756 | if (lsmc->clcsock) |
| 1757 | rc = kernel_accept(lsmc->clcsock, &new_clcsock, SOCK_NONBLOCK); |
| 1758 | mutex_unlock(&lsmc->clcsock_release_lock); |
| 1759 | lock_sock(lsk); |
| 1760 | if (rc < 0 && rc != -EAGAIN) |
| 1761 | lsk->sk_err = -rc; |
| 1762 | if (rc < 0 || lsk->sk_state == SMC_CLOSED) { |
| 1763 | new_sk->sk_prot->unhash(new_sk); |
| 1764 | if (new_clcsock) |
| 1765 | sock_release(new_clcsock); |
| 1766 | new_sk->sk_state = SMC_CLOSED; |
| 1767 | smc_sock_set_flag(new_sk, SOCK_DEAD); |
| 1768 | sock_put(new_sk); /* final */ |
| 1769 | *new_smc = NULL; |
| 1770 | goto out; |
| 1771 | } |
| 1772 | |
| 1773 | /* new clcsock has inherited the smc listen-specific sk_data_ready |
| 1774 | * function; switch it back to the original sk_data_ready function |
| 1775 | */ |
| 1776 | new_clcsock->sk->sk_data_ready = lsmc->clcsk_data_ready; |
| 1777 | |
| 1778 | /* if new clcsock has also inherited the fallback-specific callback |
| 1779 | * functions, switch them back to the original ones. |
| 1780 | */ |
| 1781 | if (lsmc->use_fallback) { |
| 1782 | if (lsmc->clcsk_state_change) |
| 1783 | new_clcsock->sk->sk_state_change = lsmc->clcsk_state_change; |
| 1784 | if (lsmc->clcsk_write_space) |
| 1785 | new_clcsock->sk->sk_write_space = lsmc->clcsk_write_space; |
| 1786 | if (lsmc->clcsk_error_report) |
| 1787 | new_clcsock->sk->sk_error_report = lsmc->clcsk_error_report; |
| 1788 | } |
| 1789 | |
| 1790 | (*new_smc)->clcsock = new_clcsock; |
| 1791 | out: |
| 1792 | return rc; |
| 1793 | } |
| 1794 | |
| 1795 | /* add a just created sock to the accept queue of the listen sock as |
| 1796 | * candidate for a following socket accept call from user space |
| 1797 | */ |
| 1798 | static void smc_accept_enqueue(struct sock *parent, struct sock *sk) |
| 1799 | { |
| 1800 | struct smc_sock *par = smc_sk(parent); |
| 1801 | |
| 1802 | sock_hold(sk); /* sock_put in smc_accept_unlink () */ |
| 1803 | spin_lock(&par->accept_q_lock); |
| 1804 | list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q); |
| 1805 | spin_unlock(&par->accept_q_lock); |
| 1806 | sk_acceptq_added(parent); |
| 1807 | } |
| 1808 | |
| 1809 | /* remove a socket from the accept queue of its parental listening socket */ |
| 1810 | static void smc_accept_unlink(struct sock *sk) |
| 1811 | { |
| 1812 | struct smc_sock *par = smc_sk(sk)->listen_smc; |
| 1813 | |
| 1814 | spin_lock(&par->accept_q_lock); |
| 1815 | list_del_init(&smc_sk(sk)->accept_q); |
| 1816 | spin_unlock(&par->accept_q_lock); |
| 1817 | sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk); |
| 1818 | sock_put(sk); /* sock_hold in smc_accept_enqueue */ |
| 1819 | } |
| 1820 | |
| 1821 | /* remove a sock from the accept queue to bind it to a new socket created |
| 1822 | * for a socket accept call from user space |
| 1823 | */ |
| 1824 | struct sock *smc_accept_dequeue(struct sock *parent, |
| 1825 | struct socket *new_sock) |
| 1826 | { |
| 1827 | struct smc_sock *isk, *n; |
| 1828 | struct sock *new_sk; |
| 1829 | |
| 1830 | list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) { |
| 1831 | new_sk = (struct sock *)isk; |
| 1832 | |
| 1833 | smc_accept_unlink(new_sk); |
| 1834 | if (new_sk->sk_state == SMC_CLOSED) { |
| 1835 | new_sk->sk_prot->unhash(new_sk); |
| 1836 | if (isk->clcsock) { |
| 1837 | sock_release(isk->clcsock); |
| 1838 | isk->clcsock = NULL; |
| 1839 | } |
| 1840 | sock_put(new_sk); /* final */ |
| 1841 | continue; |
| 1842 | } |
| 1843 | if (new_sock) { |
| 1844 | sock_graft(new_sk, new_sock); |
| 1845 | new_sock->state = SS_CONNECTED; |
| 1846 | if (isk->use_fallback) { |
| 1847 | smc_sk(new_sk)->clcsock->file = new_sock->file; |
| 1848 | isk->clcsock->file->private_data = isk->clcsock; |
| 1849 | } |
| 1850 | } |
| 1851 | return new_sk; |
| 1852 | } |
| 1853 | return NULL; |
| 1854 | } |
| 1855 | |
| 1856 | /* clean up for a created but never accepted sock */ |
| 1857 | void smc_close_non_accepted(struct sock *sk) |
| 1858 | { |
| 1859 | struct smc_sock *smc = smc_sk(sk); |
| 1860 | |
| 1861 | sock_hold(sk); /* sock_put below */ |
| 1862 | lock_sock(sk); |
| 1863 | if (!sk->sk_lingertime) |
| 1864 | /* wait for peer closing */ |
| 1865 | WRITE_ONCE(sk->sk_lingertime, SMC_MAX_STREAM_WAIT_TIMEOUT); |
| 1866 | __smc_release(smc); |
| 1867 | release_sock(sk); |
| 1868 | sock_put(sk); /* sock_hold above */ |
| 1869 | sock_put(sk); /* final sock_put */ |
| 1870 | } |
| 1871 | |
| 1872 | static int smcr_serv_conf_first_link(struct smc_sock *smc) |
| 1873 | { |
| 1874 | struct smc_link *link = smc->conn.lnk; |
| 1875 | struct smc_llc_qentry *qentry; |
| 1876 | int rc; |
| 1877 | |
| 1878 | /* reg the sndbuf if it was vzalloced*/ |
| 1879 | if (smc->conn.sndbuf_desc->is_vm) { |
| 1880 | if (smcr_link_reg_buf(link, smc->conn.sndbuf_desc)) |
| 1881 | return SMC_CLC_DECL_ERR_REGBUF; |
| 1882 | } |
| 1883 | |
| 1884 | /* reg the rmb */ |
| 1885 | if (smcr_link_reg_buf(link, smc->conn.rmb_desc)) |
| 1886 | return SMC_CLC_DECL_ERR_REGBUF; |
| 1887 | |
| 1888 | /* send CONFIRM LINK request to client over the RoCE fabric */ |
| 1889 | rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ); |
| 1890 | if (rc < 0) |
| 1891 | return SMC_CLC_DECL_TIMEOUT_CL; |
| 1892 | |
| 1893 | /* receive CONFIRM LINK response from client over the RoCE fabric */ |
| 1894 | qentry = smc_llc_wait(link->lgr, link, SMC_LLC_WAIT_TIME, |
| 1895 | SMC_LLC_CONFIRM_LINK); |
| 1896 | if (!qentry) { |
| 1897 | struct smc_clc_msg_decline dclc; |
| 1898 | |
| 1899 | rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc), |
| 1900 | SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT); |
| 1901 | return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc; |
| 1902 | } |
| 1903 | smc_llc_save_peer_uid(qentry); |
| 1904 | rc = smc_llc_eval_conf_link(qentry, SMC_LLC_RESP); |
| 1905 | smc_llc_flow_qentry_del(&link->lgr->llc_flow_lcl); |
| 1906 | if (rc) |
| 1907 | return SMC_CLC_DECL_RMBE_EC; |
| 1908 | |
| 1909 | /* confirm_rkey is implicit on 1st contact */ |
| 1910 | smc->conn.rmb_desc->is_conf_rkey = true; |
| 1911 | |
| 1912 | smc_llc_link_active(link); |
| 1913 | smcr_lgr_set_type(link->lgr, SMC_LGR_SINGLE); |
| 1914 | |
| 1915 | if (link->lgr->max_links > 1) { |
| 1916 | down_write(&link->lgr->llc_conf_mutex); |
| 1917 | /* initial contact - try to establish second link */ |
| 1918 | smc_llc_srv_add_link(link, NULL); |
| 1919 | up_write(&link->lgr->llc_conf_mutex); |
| 1920 | } |
| 1921 | return 0; |
| 1922 | } |
| 1923 | |
| 1924 | /* listen worker: finish */ |
| 1925 | static void smc_listen_out(struct smc_sock *new_smc) |
| 1926 | { |
| 1927 | struct smc_sock *lsmc = new_smc->listen_smc; |
| 1928 | struct sock *newsmcsk = &new_smc->sk; |
| 1929 | |
| 1930 | if (tcp_sk(new_smc->clcsock->sk)->syn_smc) |
| 1931 | atomic_dec(&lsmc->queued_smc_hs); |
| 1932 | |
| 1933 | release_sock(newsmcsk); /* lock in smc_listen_work() */ |
| 1934 | lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING); |
| 1935 | if (lsmc->sk.sk_state == SMC_LISTEN) { |
| 1936 | smc_accept_enqueue(&lsmc->sk, newsmcsk); |
| 1937 | release_sock(&lsmc->sk); |
| 1938 | } else { /* no longer listening */ |
| 1939 | release_sock(&lsmc->sk); |
| 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 | int rc = -EBADF; |
| 2788 | |
| 2789 | if (peer && (sock->sk->sk_state != SMC_ACTIVE) && |
| 2790 | (sock->sk->sk_state != SMC_APPCLOSEWAIT1)) |
| 2791 | return -ENOTCONN; |
| 2792 | |
| 2793 | smc = smc_sk(sock->sk); |
| 2794 | |
| 2795 | mutex_lock(&smc->clcsock_release_lock); |
| 2796 | if (smc->clcsock) |
| 2797 | rc = smc->clcsock->ops->getname(smc->clcsock, addr, peer); |
| 2798 | mutex_unlock(&smc->clcsock_release_lock); |
| 2799 | return rc; |
| 2800 | } |
| 2801 | |
| 2802 | int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) |
| 2803 | { |
| 2804 | struct sock *sk = sock->sk; |
| 2805 | struct smc_sock *smc; |
| 2806 | int rc; |
| 2807 | |
| 2808 | smc = smc_sk(sk); |
| 2809 | lock_sock(sk); |
| 2810 | |
| 2811 | /* SMC does not support connect with fastopen */ |
| 2812 | if (msg->msg_flags & MSG_FASTOPEN) { |
| 2813 | /* not connected yet, fallback */ |
| 2814 | if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) { |
| 2815 | rc = smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP); |
| 2816 | if (rc) |
| 2817 | goto out; |
| 2818 | } else { |
| 2819 | rc = -EINVAL; |
| 2820 | goto out; |
| 2821 | } |
| 2822 | } else if ((sk->sk_state != SMC_ACTIVE) && |
| 2823 | (sk->sk_state != SMC_APPCLOSEWAIT1) && |
| 2824 | (sk->sk_state != SMC_INIT)) { |
| 2825 | rc = -EPIPE; |
| 2826 | goto out; |
| 2827 | } |
| 2828 | |
| 2829 | if (smc->use_fallback) { |
| 2830 | rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len); |
| 2831 | } else { |
| 2832 | rc = smc_tx_sendmsg(smc, msg, len); |
| 2833 | SMC_STAT_TX_PAYLOAD(smc, len, rc); |
| 2834 | } |
| 2835 | out: |
| 2836 | release_sock(sk); |
| 2837 | return rc; |
| 2838 | } |
| 2839 | |
| 2840 | int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, |
| 2841 | int flags) |
| 2842 | { |
| 2843 | struct sock *sk = sock->sk; |
| 2844 | struct smc_sock *smc; |
| 2845 | int rc = -ENOTCONN; |
| 2846 | |
| 2847 | smc = smc_sk(sk); |
| 2848 | lock_sock(sk); |
| 2849 | if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) { |
| 2850 | /* socket was connected before, no more data to read */ |
| 2851 | rc = 0; |
| 2852 | goto out; |
| 2853 | } |
| 2854 | if ((sk->sk_state == SMC_INIT) || |
| 2855 | (sk->sk_state == SMC_LISTEN) || |
| 2856 | (sk->sk_state == SMC_CLOSED)) |
| 2857 | goto out; |
| 2858 | |
| 2859 | if (sk->sk_state == SMC_PEERFINCLOSEWAIT) { |
| 2860 | rc = 0; |
| 2861 | goto out; |
| 2862 | } |
| 2863 | |
| 2864 | if (smc->use_fallback) { |
| 2865 | rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags); |
| 2866 | } else { |
| 2867 | msg->msg_namelen = 0; |
| 2868 | rc = smc_rx_recvmsg(smc, msg, NULL, len, flags); |
| 2869 | SMC_STAT_RX_PAYLOAD(smc, rc, rc); |
| 2870 | } |
| 2871 | |
| 2872 | out: |
| 2873 | release_sock(sk); |
| 2874 | return rc; |
| 2875 | } |
| 2876 | |
| 2877 | static __poll_t smc_accept_poll(struct sock *parent) |
| 2878 | { |
| 2879 | struct smc_sock *isk = smc_sk(parent); |
| 2880 | __poll_t mask = 0; |
| 2881 | |
| 2882 | spin_lock(&isk->accept_q_lock); |
| 2883 | if (!list_empty(&isk->accept_q)) |
| 2884 | mask = EPOLLIN | EPOLLRDNORM; |
| 2885 | spin_unlock(&isk->accept_q_lock); |
| 2886 | |
| 2887 | return mask; |
| 2888 | } |
| 2889 | |
| 2890 | __poll_t smc_poll(struct file *file, struct socket *sock, |
| 2891 | poll_table *wait) |
| 2892 | { |
| 2893 | struct sock *sk = sock->sk; |
| 2894 | struct smc_sock *smc; |
| 2895 | __poll_t mask = 0; |
| 2896 | |
| 2897 | if (!sk) |
| 2898 | return EPOLLNVAL; |
| 2899 | |
| 2900 | smc = smc_sk(sock->sk); |
| 2901 | if (smc->use_fallback) { |
| 2902 | /* delegate to CLC child sock */ |
| 2903 | mask = smc->clcsock->ops->poll(file, smc->clcsock, wait); |
| 2904 | sk->sk_err = smc->clcsock->sk->sk_err; |
| 2905 | } else { |
| 2906 | if (sk->sk_state != SMC_CLOSED) |
| 2907 | sock_poll_wait(file, sock, wait); |
| 2908 | if (sk->sk_err) |
| 2909 | mask |= EPOLLERR; |
| 2910 | if ((sk->sk_shutdown == SHUTDOWN_MASK) || |
| 2911 | (sk->sk_state == SMC_CLOSED)) |
| 2912 | mask |= EPOLLHUP; |
| 2913 | if (sk->sk_state == SMC_LISTEN) { |
| 2914 | /* woken up by sk_data_ready in smc_listen_work() */ |
| 2915 | mask |= smc_accept_poll(sk); |
| 2916 | } else if (smc->use_fallback) { /* as result of connect_work()*/ |
| 2917 | mask |= smc->clcsock->ops->poll(file, smc->clcsock, |
| 2918 | wait); |
| 2919 | sk->sk_err = smc->clcsock->sk->sk_err; |
| 2920 | } else { |
| 2921 | if ((sk->sk_state != SMC_INIT && |
| 2922 | atomic_read(&smc->conn.sndbuf_space)) || |
| 2923 | sk->sk_shutdown & SEND_SHUTDOWN) { |
| 2924 | mask |= EPOLLOUT | EPOLLWRNORM; |
| 2925 | } else { |
| 2926 | sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); |
| 2927 | set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
| 2928 | |
| 2929 | if (sk->sk_state != SMC_INIT) { |
| 2930 | /* Race breaker the same way as tcp_poll(). */ |
| 2931 | smp_mb__after_atomic(); |
| 2932 | if (atomic_read(&smc->conn.sndbuf_space)) |
| 2933 | mask |= EPOLLOUT | EPOLLWRNORM; |
| 2934 | } |
| 2935 | } |
| 2936 | if (atomic_read(&smc->conn.bytes_to_rcv)) |
| 2937 | mask |= EPOLLIN | EPOLLRDNORM; |
| 2938 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 2939 | mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; |
| 2940 | if (sk->sk_state == SMC_APPCLOSEWAIT1) |
| 2941 | mask |= EPOLLIN; |
| 2942 | if (smc->conn.urg_state == SMC_URG_VALID) |
| 2943 | mask |= EPOLLPRI; |
| 2944 | } |
| 2945 | } |
| 2946 | |
| 2947 | return mask; |
| 2948 | } |
| 2949 | |
| 2950 | int smc_shutdown(struct socket *sock, int how) |
| 2951 | { |
| 2952 | struct sock *sk = sock->sk; |
| 2953 | bool do_shutdown = true; |
| 2954 | struct smc_sock *smc; |
| 2955 | int rc = -EINVAL; |
| 2956 | int old_state; |
| 2957 | int rc1 = 0; |
| 2958 | |
| 2959 | smc = smc_sk(sk); |
| 2960 | |
| 2961 | if ((how < SHUT_RD) || (how > SHUT_RDWR)) |
| 2962 | return rc; |
| 2963 | |
| 2964 | lock_sock(sk); |
| 2965 | |
| 2966 | if (sock->state == SS_CONNECTING) { |
| 2967 | if (sk->sk_state == SMC_ACTIVE) |
| 2968 | sock->state = SS_CONNECTED; |
| 2969 | else if (sk->sk_state == SMC_PEERCLOSEWAIT1 || |
| 2970 | sk->sk_state == SMC_PEERCLOSEWAIT2 || |
| 2971 | sk->sk_state == SMC_APPCLOSEWAIT1 || |
| 2972 | sk->sk_state == SMC_APPCLOSEWAIT2 || |
| 2973 | sk->sk_state == SMC_APPFINCLOSEWAIT) |
| 2974 | sock->state = SS_DISCONNECTING; |
| 2975 | } |
| 2976 | |
| 2977 | rc = -ENOTCONN; |
| 2978 | if ((sk->sk_state != SMC_ACTIVE) && |
| 2979 | (sk->sk_state != SMC_PEERCLOSEWAIT1) && |
| 2980 | (sk->sk_state != SMC_PEERCLOSEWAIT2) && |
| 2981 | (sk->sk_state != SMC_APPCLOSEWAIT1) && |
| 2982 | (sk->sk_state != SMC_APPCLOSEWAIT2) && |
| 2983 | (sk->sk_state != SMC_APPFINCLOSEWAIT)) |
| 2984 | goto out; |
| 2985 | if (smc->use_fallback) { |
| 2986 | rc = kernel_sock_shutdown(smc->clcsock, how); |
| 2987 | sk->sk_shutdown = smc->clcsock->sk->sk_shutdown; |
| 2988 | if (sk->sk_shutdown == SHUTDOWN_MASK) { |
| 2989 | sk->sk_state = SMC_CLOSED; |
| 2990 | sk->sk_socket->state = SS_UNCONNECTED; |
| 2991 | sock_put(sk); |
| 2992 | } |
| 2993 | goto out; |
| 2994 | } |
| 2995 | switch (how) { |
| 2996 | case SHUT_RDWR: /* shutdown in both directions */ |
| 2997 | old_state = sk->sk_state; |
| 2998 | rc = smc_close_active(smc); |
| 2999 | if (old_state == SMC_ACTIVE && |
| 3000 | sk->sk_state == SMC_PEERCLOSEWAIT1) |
| 3001 | do_shutdown = false; |
| 3002 | break; |
| 3003 | case SHUT_WR: |
| 3004 | rc = smc_close_shutdown_write(smc); |
| 3005 | break; |
| 3006 | case SHUT_RD: |
| 3007 | rc = 0; |
| 3008 | /* nothing more to do because peer is not involved */ |
| 3009 | break; |
| 3010 | } |
| 3011 | if (do_shutdown && smc->clcsock) |
| 3012 | rc1 = kernel_sock_shutdown(smc->clcsock, how); |
| 3013 | /* map sock_shutdown_cmd constants to sk_shutdown value range */ |
| 3014 | sk->sk_shutdown |= how + 1; |
| 3015 | |
| 3016 | if (sk->sk_state == SMC_CLOSED) |
| 3017 | sock->state = SS_UNCONNECTED; |
| 3018 | else |
| 3019 | sock->state = SS_DISCONNECTING; |
| 3020 | out: |
| 3021 | release_sock(sk); |
| 3022 | return rc ? rc : rc1; |
| 3023 | } |
| 3024 | |
| 3025 | static int __smc_getsockopt(struct socket *sock, int level, int optname, |
| 3026 | char __user *optval, int __user *optlen) |
| 3027 | { |
| 3028 | struct smc_sock *smc; |
| 3029 | int val, len; |
| 3030 | |
| 3031 | smc = smc_sk(sock->sk); |
| 3032 | |
| 3033 | if (get_user(len, optlen)) |
| 3034 | return -EFAULT; |
| 3035 | |
| 3036 | len = min_t(int, len, sizeof(int)); |
| 3037 | |
| 3038 | if (len < 0) |
| 3039 | return -EINVAL; |
| 3040 | |
| 3041 | switch (optname) { |
| 3042 | case SMC_LIMIT_HS: |
| 3043 | val = smc->limit_smc_hs; |
| 3044 | break; |
| 3045 | default: |
| 3046 | return -EOPNOTSUPP; |
| 3047 | } |
| 3048 | |
| 3049 | if (put_user(len, optlen)) |
| 3050 | return -EFAULT; |
| 3051 | if (copy_to_user(optval, &val, len)) |
| 3052 | return -EFAULT; |
| 3053 | |
| 3054 | return 0; |
| 3055 | } |
| 3056 | |
| 3057 | static int __smc_setsockopt(struct socket *sock, int level, int optname, |
| 3058 | sockptr_t optval, unsigned int optlen) |
| 3059 | { |
| 3060 | struct sock *sk = sock->sk; |
| 3061 | struct smc_sock *smc; |
| 3062 | int val, rc; |
| 3063 | |
| 3064 | smc = smc_sk(sk); |
| 3065 | |
| 3066 | /* pre-fetch user data outside the lock */ |
| 3067 | if (optname == SMC_LIMIT_HS) { |
| 3068 | if (optlen < sizeof(int)) |
| 3069 | return -EINVAL; |
| 3070 | if (copy_from_sockptr(&val, optval, sizeof(int))) |
| 3071 | return -EFAULT; |
| 3072 | } |
| 3073 | |
| 3074 | lock_sock(sk); |
| 3075 | switch (optname) { |
| 3076 | case SMC_LIMIT_HS: |
| 3077 | smc->limit_smc_hs = !!val; |
| 3078 | rc = 0; |
| 3079 | break; |
| 3080 | default: |
| 3081 | rc = -EOPNOTSUPP; |
| 3082 | break; |
| 3083 | } |
| 3084 | release_sock(sk); |
| 3085 | |
| 3086 | return rc; |
| 3087 | } |
| 3088 | |
| 3089 | int smc_setsockopt(struct socket *sock, int level, int optname, |
| 3090 | sockptr_t optval, unsigned int optlen) |
| 3091 | { |
| 3092 | struct sock *sk = sock->sk; |
| 3093 | struct smc_sock *smc; |
| 3094 | int val, rc; |
| 3095 | |
| 3096 | if (level == SOL_TCP && optname == TCP_ULP) |
| 3097 | return -EOPNOTSUPP; |
| 3098 | else if (level == SOL_SMC) |
| 3099 | return __smc_setsockopt(sock, level, optname, optval, optlen); |
| 3100 | |
| 3101 | smc = smc_sk(sk); |
| 3102 | |
| 3103 | /* generic setsockopts reaching us here always apply to the |
| 3104 | * CLC socket |
| 3105 | */ |
| 3106 | mutex_lock(&smc->clcsock_release_lock); |
| 3107 | if (!smc->clcsock) { |
| 3108 | mutex_unlock(&smc->clcsock_release_lock); |
| 3109 | return -EBADF; |
| 3110 | } |
| 3111 | if (unlikely(!smc->clcsock->ops->setsockopt)) |
| 3112 | rc = -EOPNOTSUPP; |
| 3113 | else |
| 3114 | rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname, |
| 3115 | optval, optlen); |
| 3116 | if (smc->clcsock->sk->sk_err) { |
| 3117 | sk->sk_err = smc->clcsock->sk->sk_err; |
| 3118 | sk_error_report(sk); |
| 3119 | } |
| 3120 | mutex_unlock(&smc->clcsock_release_lock); |
| 3121 | |
| 3122 | if (optlen < sizeof(int)) |
| 3123 | return -EINVAL; |
| 3124 | if (copy_from_sockptr(&val, optval, sizeof(int))) |
| 3125 | return -EFAULT; |
| 3126 | |
| 3127 | lock_sock(sk); |
| 3128 | if (rc || smc->use_fallback) |
| 3129 | goto out; |
| 3130 | switch (optname) { |
| 3131 | case TCP_FASTOPEN: |
| 3132 | case TCP_FASTOPEN_CONNECT: |
| 3133 | case TCP_FASTOPEN_KEY: |
| 3134 | case TCP_FASTOPEN_NO_COOKIE: |
| 3135 | /* option not supported by SMC */ |
| 3136 | if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) { |
| 3137 | rc = smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP); |
| 3138 | } else { |
| 3139 | rc = -EINVAL; |
| 3140 | } |
| 3141 | break; |
| 3142 | case TCP_NODELAY: |
| 3143 | if (sk->sk_state != SMC_INIT && |
| 3144 | sk->sk_state != SMC_LISTEN && |
| 3145 | sk->sk_state != SMC_CLOSED) { |
| 3146 | if (val) { |
| 3147 | SMC_STAT_INC(smc, ndly_cnt); |
| 3148 | smc_tx_pending(&smc->conn); |
| 3149 | cancel_delayed_work(&smc->conn.tx_work); |
| 3150 | } |
| 3151 | } |
| 3152 | break; |
| 3153 | case TCP_CORK: |
| 3154 | if (sk->sk_state != SMC_INIT && |
| 3155 | sk->sk_state != SMC_LISTEN && |
| 3156 | sk->sk_state != SMC_CLOSED) { |
| 3157 | if (!val) { |
| 3158 | SMC_STAT_INC(smc, cork_cnt); |
| 3159 | smc_tx_pending(&smc->conn); |
| 3160 | cancel_delayed_work(&smc->conn.tx_work); |
| 3161 | } |
| 3162 | } |
| 3163 | break; |
| 3164 | case TCP_DEFER_ACCEPT: |
| 3165 | smc->sockopt_defer_accept = val; |
| 3166 | break; |
| 3167 | default: |
| 3168 | break; |
| 3169 | } |
| 3170 | out: |
| 3171 | release_sock(sk); |
| 3172 | |
| 3173 | return rc; |
| 3174 | } |
| 3175 | |
| 3176 | int smc_getsockopt(struct socket *sock, int level, int optname, |
| 3177 | char __user *optval, int __user *optlen) |
| 3178 | { |
| 3179 | struct smc_sock *smc; |
| 3180 | int rc; |
| 3181 | |
| 3182 | if (level == SOL_SMC) |
| 3183 | return __smc_getsockopt(sock, level, optname, optval, optlen); |
| 3184 | |
| 3185 | smc = smc_sk(sock->sk); |
| 3186 | mutex_lock(&smc->clcsock_release_lock); |
| 3187 | if (!smc->clcsock) { |
| 3188 | mutex_unlock(&smc->clcsock_release_lock); |
| 3189 | return -EBADF; |
| 3190 | } |
| 3191 | /* socket options apply to the CLC socket */ |
| 3192 | if (unlikely(!smc->clcsock->ops->getsockopt)) { |
| 3193 | mutex_unlock(&smc->clcsock_release_lock); |
| 3194 | return -EOPNOTSUPP; |
| 3195 | } |
| 3196 | rc = smc->clcsock->ops->getsockopt(smc->clcsock, level, optname, |
| 3197 | optval, optlen); |
| 3198 | mutex_unlock(&smc->clcsock_release_lock); |
| 3199 | return rc; |
| 3200 | } |
| 3201 | |
| 3202 | int smc_ioctl(struct socket *sock, unsigned int cmd, |
| 3203 | unsigned long arg) |
| 3204 | { |
| 3205 | union smc_host_cursor cons, urg; |
| 3206 | struct smc_connection *conn; |
| 3207 | struct smc_sock *smc; |
| 3208 | int answ; |
| 3209 | |
| 3210 | smc = smc_sk(sock->sk); |
| 3211 | conn = &smc->conn; |
| 3212 | lock_sock(&smc->sk); |
| 3213 | if (smc->use_fallback) { |
| 3214 | if (!smc->clcsock) { |
| 3215 | release_sock(&smc->sk); |
| 3216 | return -EBADF; |
| 3217 | } |
| 3218 | answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg); |
| 3219 | release_sock(&smc->sk); |
| 3220 | return answ; |
| 3221 | } |
| 3222 | switch (cmd) { |
| 3223 | case SIOCINQ: /* same as FIONREAD */ |
| 3224 | if (smc->sk.sk_state == SMC_LISTEN) { |
| 3225 | release_sock(&smc->sk); |
| 3226 | return -EINVAL; |
| 3227 | } |
| 3228 | if (smc->sk.sk_state == SMC_INIT || |
| 3229 | smc->sk.sk_state == SMC_CLOSED) |
| 3230 | answ = 0; |
| 3231 | else |
| 3232 | answ = atomic_read(&smc->conn.bytes_to_rcv); |
| 3233 | break; |
| 3234 | case SIOCOUTQ: |
| 3235 | /* output queue size (not send + not acked) */ |
| 3236 | if (smc->sk.sk_state == SMC_LISTEN) { |
| 3237 | release_sock(&smc->sk); |
| 3238 | return -EINVAL; |
| 3239 | } |
| 3240 | if (smc->sk.sk_state == SMC_INIT || |
| 3241 | smc->sk.sk_state == SMC_CLOSED || |
| 3242 | !READ_ONCE(smc->conn.sndbuf_desc)) |
| 3243 | answ = 0; |
| 3244 | else |
| 3245 | answ = smc->conn.sndbuf_desc->len - |
| 3246 | atomic_read(&smc->conn.sndbuf_space); |
| 3247 | break; |
| 3248 | case SIOCOUTQNSD: |
| 3249 | /* output queue size (not send only) */ |
| 3250 | if (smc->sk.sk_state == SMC_LISTEN) { |
| 3251 | release_sock(&smc->sk); |
| 3252 | return -EINVAL; |
| 3253 | } |
| 3254 | if (smc->sk.sk_state == SMC_INIT || |
| 3255 | smc->sk.sk_state == SMC_CLOSED) |
| 3256 | answ = 0; |
| 3257 | else |
| 3258 | answ = smc_tx_prepared_sends(&smc->conn); |
| 3259 | break; |
| 3260 | case SIOCATMARK: |
| 3261 | if (smc->sk.sk_state == SMC_LISTEN) { |
| 3262 | release_sock(&smc->sk); |
| 3263 | return -EINVAL; |
| 3264 | } |
| 3265 | if (smc->sk.sk_state == SMC_INIT || |
| 3266 | smc->sk.sk_state == SMC_CLOSED) { |
| 3267 | answ = 0; |
| 3268 | } else { |
| 3269 | smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn); |
| 3270 | smc_curs_copy(&urg, &conn->urg_curs, conn); |
| 3271 | answ = smc_curs_diff(conn->rmb_desc->len, |
| 3272 | &cons, &urg) == 1; |
| 3273 | } |
| 3274 | break; |
| 3275 | default: |
| 3276 | release_sock(&smc->sk); |
| 3277 | return -ENOIOCTLCMD; |
| 3278 | } |
| 3279 | release_sock(&smc->sk); |
| 3280 | |
| 3281 | return put_user(answ, (int __user *)arg); |
| 3282 | } |
| 3283 | |
| 3284 | /* Map the affected portions of the rmbe into an spd, note the number of bytes |
| 3285 | * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor |
| 3286 | * updates till whenever a respective page has been fully processed. |
| 3287 | * Note that subsequent recv() calls have to wait till all splice() processing |
| 3288 | * completed. |
| 3289 | */ |
| 3290 | ssize_t smc_splice_read(struct socket *sock, loff_t *ppos, |
| 3291 | struct pipe_inode_info *pipe, size_t len, |
| 3292 | unsigned int flags) |
| 3293 | { |
| 3294 | struct sock *sk = sock->sk; |
| 3295 | struct smc_sock *smc; |
| 3296 | int rc = -ENOTCONN; |
| 3297 | |
| 3298 | smc = smc_sk(sk); |
| 3299 | lock_sock(sk); |
| 3300 | if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) { |
| 3301 | /* socket was connected before, no more data to read */ |
| 3302 | rc = 0; |
| 3303 | goto out; |
| 3304 | } |
| 3305 | if (sk->sk_state == SMC_INIT || |
| 3306 | sk->sk_state == SMC_LISTEN || |
| 3307 | sk->sk_state == SMC_CLOSED) |
| 3308 | goto out; |
| 3309 | |
| 3310 | if (sk->sk_state == SMC_PEERFINCLOSEWAIT) { |
| 3311 | rc = 0; |
| 3312 | goto out; |
| 3313 | } |
| 3314 | |
| 3315 | if (smc->use_fallback) { |
| 3316 | rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos, |
| 3317 | pipe, len, flags); |
| 3318 | } else { |
| 3319 | if (*ppos) { |
| 3320 | rc = -ESPIPE; |
| 3321 | goto out; |
| 3322 | } |
| 3323 | if (flags & SPLICE_F_NONBLOCK) |
| 3324 | flags = MSG_DONTWAIT; |
| 3325 | else |
| 3326 | flags = 0; |
| 3327 | SMC_STAT_INC(smc, splice_cnt); |
| 3328 | rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags); |
| 3329 | } |
| 3330 | out: |
| 3331 | release_sock(sk); |
| 3332 | |
| 3333 | return rc; |
| 3334 | } |
| 3335 | |
| 3336 | /* must look like tcp */ |
| 3337 | static const struct proto_ops smc_sock_ops = { |
| 3338 | .family = PF_SMC, |
| 3339 | .owner = THIS_MODULE, |
| 3340 | .release = smc_release, |
| 3341 | .bind = smc_bind, |
| 3342 | .connect = smc_connect, |
| 3343 | .socketpair = sock_no_socketpair, |
| 3344 | .accept = smc_accept, |
| 3345 | .getname = smc_getname, |
| 3346 | .poll = smc_poll, |
| 3347 | .ioctl = smc_ioctl, |
| 3348 | .listen = smc_listen, |
| 3349 | .shutdown = smc_shutdown, |
| 3350 | .setsockopt = smc_setsockopt, |
| 3351 | .getsockopt = smc_getsockopt, |
| 3352 | .sendmsg = smc_sendmsg, |
| 3353 | .recvmsg = smc_recvmsg, |
| 3354 | .mmap = sock_no_mmap, |
| 3355 | .splice_read = smc_splice_read, |
| 3356 | }; |
| 3357 | |
| 3358 | int smc_create_clcsk(struct net *net, struct sock *sk, int family) |
| 3359 | { |
| 3360 | struct smc_sock *smc = smc_sk(sk); |
| 3361 | int rc; |
| 3362 | |
| 3363 | rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP, |
| 3364 | &smc->clcsock); |
| 3365 | if (rc) |
| 3366 | return rc; |
| 3367 | |
| 3368 | /* smc_clcsock_release() does not wait smc->clcsock->sk's |
| 3369 | * destruction; its sk_state might not be TCP_CLOSE after |
| 3370 | * smc->sk is close()d, and TCP timers can be fired later, |
| 3371 | * which need net ref. |
| 3372 | */ |
| 3373 | sk = smc->clcsock->sk; |
| 3374 | sk_net_refcnt_upgrade(sk); |
| 3375 | return 0; |
| 3376 | } |
| 3377 | |
| 3378 | static int smc_create(struct net *net, struct socket *sock, int protocol, |
| 3379 | int kern) |
| 3380 | { |
| 3381 | int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET; |
| 3382 | struct sock *sk; |
| 3383 | int rc; |
| 3384 | |
| 3385 | rc = -ESOCKTNOSUPPORT; |
| 3386 | if (sock->type != SOCK_STREAM) |
| 3387 | goto out; |
| 3388 | |
| 3389 | rc = -EPROTONOSUPPORT; |
| 3390 | if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6) |
| 3391 | goto out; |
| 3392 | |
| 3393 | rc = -ENOBUFS; |
| 3394 | sock->ops = &smc_sock_ops; |
| 3395 | sock->state = SS_UNCONNECTED; |
| 3396 | sk = smc_sock_alloc(net, sock, protocol); |
| 3397 | if (!sk) |
| 3398 | goto out; |
| 3399 | |
| 3400 | rc = smc_create_clcsk(net, sk, family); |
| 3401 | if (rc) { |
| 3402 | sk_common_release(sk); |
| 3403 | sock->sk = NULL; |
| 3404 | } |
| 3405 | out: |
| 3406 | return rc; |
| 3407 | } |
| 3408 | |
| 3409 | static const struct net_proto_family smc_sock_family_ops = { |
| 3410 | .family = PF_SMC, |
| 3411 | .owner = THIS_MODULE, |
| 3412 | .create = smc_create, |
| 3413 | }; |
| 3414 | |
| 3415 | unsigned int smc_net_id; |
| 3416 | |
| 3417 | static __net_init int smc_net_init(struct net *net) |
| 3418 | { |
| 3419 | int rc; |
| 3420 | |
| 3421 | rc = smc_sysctl_net_init(net); |
| 3422 | if (rc) |
| 3423 | return rc; |
| 3424 | return smc_pnet_net_init(net); |
| 3425 | } |
| 3426 | |
| 3427 | static void __net_exit smc_net_exit(struct net *net) |
| 3428 | { |
| 3429 | smc_sysctl_net_exit(net); |
| 3430 | smc_pnet_net_exit(net); |
| 3431 | } |
| 3432 | |
| 3433 | static __net_init int smc_net_stat_init(struct net *net) |
| 3434 | { |
| 3435 | return smc_stats_init(net); |
| 3436 | } |
| 3437 | |
| 3438 | static void __net_exit smc_net_stat_exit(struct net *net) |
| 3439 | { |
| 3440 | smc_stats_exit(net); |
| 3441 | } |
| 3442 | |
| 3443 | static struct pernet_operations smc_net_ops = { |
| 3444 | .init = smc_net_init, |
| 3445 | .exit = smc_net_exit, |
| 3446 | .id = &smc_net_id, |
| 3447 | .size = sizeof(struct smc_net), |
| 3448 | }; |
| 3449 | |
| 3450 | static struct pernet_operations smc_net_stat_ops = { |
| 3451 | .init = smc_net_stat_init, |
| 3452 | .exit = smc_net_stat_exit, |
| 3453 | }; |
| 3454 | |
| 3455 | static int __init smc_init(void) |
| 3456 | { |
| 3457 | int rc; |
| 3458 | |
| 3459 | rc = register_pernet_subsys(&smc_net_ops); |
| 3460 | if (rc) |
| 3461 | return rc; |
| 3462 | |
| 3463 | rc = register_pernet_subsys(&smc_net_stat_ops); |
| 3464 | if (rc) |
| 3465 | goto out_pernet_subsys; |
| 3466 | |
| 3467 | rc = smc_ism_init(); |
| 3468 | if (rc) |
| 3469 | goto out_pernet_subsys_stat; |
| 3470 | smc_clc_init(); |
| 3471 | |
| 3472 | rc = smc_nl_init(); |
| 3473 | if (rc) |
| 3474 | goto out_ism; |
| 3475 | |
| 3476 | rc = smc_pnet_init(); |
| 3477 | if (rc) |
| 3478 | goto out_nl; |
| 3479 | |
| 3480 | rc = -ENOMEM; |
| 3481 | |
| 3482 | smc_tcp_ls_wq = alloc_workqueue("smc_tcp_ls_wq", WQ_PERCPU, 0); |
| 3483 | if (!smc_tcp_ls_wq) |
| 3484 | goto out_pnet; |
| 3485 | |
| 3486 | smc_hs_wq = alloc_workqueue("smc_hs_wq", WQ_PERCPU, 0); |
| 3487 | if (!smc_hs_wq) |
| 3488 | goto out_alloc_tcp_ls_wq; |
| 3489 | |
| 3490 | smc_close_wq = alloc_workqueue("smc_close_wq", WQ_PERCPU, 0); |
| 3491 | if (!smc_close_wq) |
| 3492 | goto out_alloc_hs_wq; |
| 3493 | |
| 3494 | rc = smc_core_init(); |
| 3495 | if (rc) { |
| 3496 | pr_err("%s: smc_core_init fails with %d\n", __func__, rc); |
| 3497 | goto out_alloc_wqs; |
| 3498 | } |
| 3499 | |
| 3500 | rc = smc_llc_init(); |
| 3501 | if (rc) { |
| 3502 | pr_err("%s: smc_llc_init fails with %d\n", __func__, rc); |
| 3503 | goto out_core; |
| 3504 | } |
| 3505 | |
| 3506 | rc = smc_cdc_init(); |
| 3507 | if (rc) { |
| 3508 | pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc); |
| 3509 | goto out_core; |
| 3510 | } |
| 3511 | |
| 3512 | rc = proto_register(&smc_proto, 1); |
| 3513 | if (rc) { |
| 3514 | pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc); |
| 3515 | goto out_core; |
| 3516 | } |
| 3517 | |
| 3518 | rc = proto_register(&smc_proto6, 1); |
| 3519 | if (rc) { |
| 3520 | pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc); |
| 3521 | goto out_proto; |
| 3522 | } |
| 3523 | |
| 3524 | rc = sock_register(&smc_sock_family_ops); |
| 3525 | if (rc) { |
| 3526 | pr_err("%s: sock_register fails with %d\n", __func__, rc); |
| 3527 | goto out_proto6; |
| 3528 | } |
| 3529 | |
| 3530 | rc = smc_ib_register_client(); |
| 3531 | if (rc) { |
| 3532 | pr_err("%s: ib_register fails with %d\n", __func__, rc); |
| 3533 | goto out_sock; |
| 3534 | } |
| 3535 | rc = smc_inet_init(); |
| 3536 | if (rc) { |
| 3537 | pr_err("%s: smc_inet_init fails with %d\n", __func__, rc); |
| 3538 | goto out_ib; |
| 3539 | } |
| 3540 | rc = bpf_smc_hs_ctrl_init(); |
| 3541 | if (rc) { |
| 3542 | pr_err("%s: bpf_smc_hs_ctrl_init fails with %d\n", __func__, |
| 3543 | rc); |
| 3544 | goto out_inet; |
| 3545 | } |
| 3546 | static_branch_enable(&tcp_have_smc); |
| 3547 | return 0; |
| 3548 | out_inet: |
| 3549 | smc_inet_exit(); |
| 3550 | out_ib: |
| 3551 | smc_ib_unregister_client(); |
| 3552 | out_sock: |
| 3553 | sock_unregister(PF_SMC); |
| 3554 | out_proto6: |
| 3555 | proto_unregister(&smc_proto6); |
| 3556 | out_proto: |
| 3557 | proto_unregister(&smc_proto); |
| 3558 | out_core: |
| 3559 | smc_core_exit(); |
| 3560 | out_alloc_wqs: |
| 3561 | destroy_workqueue(smc_close_wq); |
| 3562 | out_alloc_hs_wq: |
| 3563 | destroy_workqueue(smc_hs_wq); |
| 3564 | out_alloc_tcp_ls_wq: |
| 3565 | destroy_workqueue(smc_tcp_ls_wq); |
| 3566 | out_pnet: |
| 3567 | smc_pnet_exit(); |
| 3568 | out_nl: |
| 3569 | smc_nl_exit(); |
| 3570 | out_ism: |
| 3571 | smc_clc_exit(); |
| 3572 | smc_ism_exit(); |
| 3573 | out_pernet_subsys_stat: |
| 3574 | unregister_pernet_subsys(&smc_net_stat_ops); |
| 3575 | out_pernet_subsys: |
| 3576 | unregister_pernet_subsys(&smc_net_ops); |
| 3577 | |
| 3578 | return rc; |
| 3579 | } |
| 3580 | |
| 3581 | static void __exit smc_exit(void) |
| 3582 | { |
| 3583 | static_branch_disable(&tcp_have_smc); |
| 3584 | smc_inet_exit(); |
| 3585 | sock_unregister(PF_SMC); |
| 3586 | smc_core_exit(); |
| 3587 | smc_ib_unregister_client(); |
| 3588 | smc_ism_exit(); |
| 3589 | destroy_workqueue(smc_close_wq); |
| 3590 | destroy_workqueue(smc_tcp_ls_wq); |
| 3591 | destroy_workqueue(smc_hs_wq); |
| 3592 | proto_unregister(&smc_proto6); |
| 3593 | proto_unregister(&smc_proto); |
| 3594 | smc_pnet_exit(); |
| 3595 | smc_nl_exit(); |
| 3596 | smc_clc_exit(); |
| 3597 | unregister_pernet_subsys(&smc_net_stat_ops); |
| 3598 | unregister_pernet_subsys(&smc_net_ops); |
| 3599 | rcu_barrier(); |
| 3600 | } |
| 3601 | |
| 3602 | module_init(smc_init); |
| 3603 | module_exit(smc_exit); |
| 3604 | |
| 3605 | MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>"); |
| 3606 | MODULE_DESCRIPTION("smc socket address family"); |
| 3607 | MODULE_LICENSE("GPL"); |
| 3608 | MODULE_ALIAS_NETPROTO(PF_SMC); |
| 3609 | /* 256 for IPPROTO_SMC and 1 for SOCK_STREAM */ |
| 3610 | MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET, 256, 1); |
| 3611 | #if IS_ENABLED(CONFIG_IPV6) |
| 3612 | MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET6, 256, 1); |
| 3613 | #endif /* CONFIG_IPV6 */ |
| 3614 | MODULE_ALIAS_GENL_FAMILY(SMC_GENL_FAMILY_NAME); |