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