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