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