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