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