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 return SMC_CLC_DECL_MEM; 2005 2006 return 0; 2007 } 2008 2009 /* listen worker: initialize connection and buffers for SMC-D */ 2010 static int smc_listen_ism_init(struct smc_sock *new_smc, 2011 struct smc_init_info *ini) 2012 { 2013 int rc; 2014 2015 rc = smc_conn_create(new_smc, ini); 2016 if (rc) 2017 return rc; 2018 2019 /* Create send and receive buffers */ 2020 rc = smc_buf_create(new_smc, true); 2021 if (rc) { 2022 smc_conn_abort(new_smc, ini->first_contact_local); 2023 return (rc == -ENOSPC) ? SMC_CLC_DECL_MAX_DMB : 2024 SMC_CLC_DECL_MEM; 2025 } 2026 2027 return 0; 2028 } 2029 2030 static bool smc_is_already_selected(struct smcd_dev *smcd, 2031 struct smc_init_info *ini, 2032 int matches) 2033 { 2034 int i; 2035 2036 for (i = 0; i < matches; i++) 2037 if (smcd == ini->ism_dev[i]) 2038 return true; 2039 2040 return false; 2041 } 2042 2043 /* check for ISM devices matching proposed ISM devices */ 2044 static void smc_check_ism_v2_match(struct smc_init_info *ini, 2045 u16 proposed_chid, u64 proposed_gid, 2046 unsigned int *matches) 2047 { 2048 struct smcd_dev *smcd; 2049 2050 list_for_each_entry(smcd, &smcd_dev_list.list, list) { 2051 if (smcd->going_away) 2052 continue; 2053 if (smc_is_already_selected(smcd, ini, *matches)) 2054 continue; 2055 if (smc_ism_get_chid(smcd) == proposed_chid && 2056 !smc_ism_cantalk(proposed_gid, ISM_RESERVED_VLANID, smcd)) { 2057 ini->ism_peer_gid[*matches] = proposed_gid; 2058 ini->ism_dev[*matches] = smcd; 2059 (*matches)++; 2060 break; 2061 } 2062 } 2063 } 2064 2065 static void smc_find_ism_store_rc(u32 rc, struct smc_init_info *ini) 2066 { 2067 if (!ini->rc) 2068 ini->rc = rc; 2069 } 2070 2071 static void smc_find_ism_v2_device_serv(struct smc_sock *new_smc, 2072 struct smc_clc_msg_proposal *pclc, 2073 struct smc_init_info *ini) 2074 { 2075 struct smc_clc_smcd_v2_extension *smcd_v2_ext; 2076 struct smc_clc_v2_extension *smc_v2_ext; 2077 struct smc_clc_msg_smcd *pclc_smcd; 2078 unsigned int matches = 0; 2079 u8 smcd_version; 2080 u8 *eid = NULL; 2081 int i, rc; 2082 2083 if (!(ini->smcd_version & SMC_V2) || !smcd_indicated(ini->smc_type_v2)) 2084 goto not_found; 2085 2086 pclc_smcd = smc_get_clc_msg_smcd(pclc); 2087 smc_v2_ext = smc_get_clc_v2_ext(pclc); 2088 smcd_v2_ext = smc_get_clc_smcd_v2_ext(smc_v2_ext); 2089 2090 mutex_lock(&smcd_dev_list.mutex); 2091 if (pclc_smcd->ism.chid) 2092 /* check for ISM device matching proposed native ISM device */ 2093 smc_check_ism_v2_match(ini, ntohs(pclc_smcd->ism.chid), 2094 ntohll(pclc_smcd->ism.gid), &matches); 2095 for (i = 1; i <= smc_v2_ext->hdr.ism_gid_cnt; i++) { 2096 /* check for ISM devices matching proposed non-native ISM 2097 * devices 2098 */ 2099 smc_check_ism_v2_match(ini, 2100 ntohs(smcd_v2_ext->gidchid[i - 1].chid), 2101 ntohll(smcd_v2_ext->gidchid[i - 1].gid), 2102 &matches); 2103 } 2104 mutex_unlock(&smcd_dev_list.mutex); 2105 2106 if (!ini->ism_dev[0]) { 2107 smc_find_ism_store_rc(SMC_CLC_DECL_NOSMCD2DEV, ini); 2108 goto not_found; 2109 } 2110 2111 smc_ism_get_system_eid(&eid); 2112 if (!smc_clc_match_eid(ini->negotiated_eid, smc_v2_ext, 2113 smcd_v2_ext->system_eid, eid)) 2114 goto not_found; 2115 2116 /* separate - outside the smcd_dev_list.lock */ 2117 smcd_version = ini->smcd_version; 2118 for (i = 0; i < matches; i++) { 2119 ini->smcd_version = SMC_V2; 2120 ini->is_smcd = true; 2121 ini->ism_selected = i; 2122 rc = smc_listen_ism_init(new_smc, ini); 2123 if (rc) { 2124 smc_find_ism_store_rc(rc, ini); 2125 /* try next active ISM device */ 2126 continue; 2127 } 2128 return; /* matching and usable V2 ISM device found */ 2129 } 2130 /* no V2 ISM device could be initialized */ 2131 ini->smcd_version = smcd_version; /* restore original value */ 2132 ini->negotiated_eid[0] = 0; 2133 2134 not_found: 2135 ini->smcd_version &= ~SMC_V2; 2136 ini->ism_dev[0] = NULL; 2137 ini->is_smcd = false; 2138 } 2139 2140 static void smc_find_ism_v1_device_serv(struct smc_sock *new_smc, 2141 struct smc_clc_msg_proposal *pclc, 2142 struct smc_init_info *ini) 2143 { 2144 struct smc_clc_msg_smcd *pclc_smcd = smc_get_clc_msg_smcd(pclc); 2145 int rc = 0; 2146 2147 /* check if ISM V1 is available */ 2148 if (!(ini->smcd_version & SMC_V1) || !smcd_indicated(ini->smc_type_v1)) 2149 goto not_found; 2150 ini->is_smcd = true; /* prepare ISM check */ 2151 ini->ism_peer_gid[0] = ntohll(pclc_smcd->ism.gid); 2152 rc = smc_find_ism_device(new_smc, ini); 2153 if (rc) 2154 goto not_found; 2155 ini->ism_selected = 0; 2156 rc = smc_listen_ism_init(new_smc, ini); 2157 if (!rc) 2158 return; /* V1 ISM device found */ 2159 2160 not_found: 2161 smc_find_ism_store_rc(rc, ini); 2162 ini->smcd_version &= ~SMC_V1; 2163 ini->ism_dev[0] = NULL; 2164 ini->is_smcd = false; 2165 } 2166 2167 /* listen worker: register buffers */ 2168 static int smc_listen_rdma_reg(struct smc_sock *new_smc, bool local_first) 2169 { 2170 struct smc_connection *conn = &new_smc->conn; 2171 2172 if (!local_first) { 2173 /* reg sendbufs if they were vzalloced */ 2174 if (conn->sndbuf_desc->is_vm) { 2175 if (smcr_lgr_reg_sndbufs(conn->lnk, 2176 conn->sndbuf_desc)) 2177 return SMC_CLC_DECL_ERR_REGBUF; 2178 } 2179 if (smcr_lgr_reg_rmbs(conn->lnk, conn->rmb_desc)) 2180 return SMC_CLC_DECL_ERR_REGBUF; 2181 } 2182 2183 return 0; 2184 } 2185 2186 static void smc_find_rdma_v2_device_serv(struct smc_sock *new_smc, 2187 struct smc_clc_msg_proposal *pclc, 2188 struct smc_init_info *ini) 2189 { 2190 struct smc_clc_v2_extension *smc_v2_ext; 2191 u8 smcr_version; 2192 int rc; 2193 2194 if (!(ini->smcr_version & SMC_V2) || !smcr_indicated(ini->smc_type_v2)) 2195 goto not_found; 2196 2197 smc_v2_ext = smc_get_clc_v2_ext(pclc); 2198 if (!smc_clc_match_eid(ini->negotiated_eid, smc_v2_ext, NULL, NULL)) 2199 goto not_found; 2200 2201 /* prepare RDMA check */ 2202 memcpy(ini->peer_systemid, pclc->lcl.id_for_peer, SMC_SYSTEMID_LEN); 2203 memcpy(ini->peer_gid, smc_v2_ext->roce, SMC_GID_SIZE); 2204 memcpy(ini->peer_mac, pclc->lcl.mac, ETH_ALEN); 2205 ini->check_smcrv2 = true; 2206 ini->smcrv2.clc_sk = new_smc->clcsock->sk; 2207 ini->smcrv2.saddr = new_smc->clcsock->sk->sk_rcv_saddr; 2208 ini->smcrv2.daddr = smc_ib_gid_to_ipv4(smc_v2_ext->roce); 2209 rc = smc_find_rdma_device(new_smc, ini); 2210 if (rc) { 2211 smc_find_ism_store_rc(rc, ini); 2212 goto not_found; 2213 } 2214 if (!ini->smcrv2.uses_gateway) 2215 memcpy(ini->smcrv2.nexthop_mac, pclc->lcl.mac, ETH_ALEN); 2216 2217 smcr_version = ini->smcr_version; 2218 ini->smcr_version = SMC_V2; 2219 rc = smc_listen_rdma_init(new_smc, ini); 2220 if (!rc) 2221 rc = smc_listen_rdma_reg(new_smc, ini->first_contact_local); 2222 if (!rc) 2223 return; 2224 ini->smcr_version = smcr_version; 2225 smc_find_ism_store_rc(rc, ini); 2226 2227 not_found: 2228 ini->smcr_version &= ~SMC_V2; 2229 ini->smcrv2.ib_dev_v2 = NULL; 2230 ini->check_smcrv2 = false; 2231 } 2232 2233 static int smc_find_rdma_v1_device_serv(struct smc_sock *new_smc, 2234 struct smc_clc_msg_proposal *pclc, 2235 struct smc_init_info *ini) 2236 { 2237 int rc; 2238 2239 if (!(ini->smcr_version & SMC_V1) || !smcr_indicated(ini->smc_type_v1)) 2240 return SMC_CLC_DECL_NOSMCDEV; 2241 2242 /* prepare RDMA check */ 2243 memcpy(ini->peer_systemid, pclc->lcl.id_for_peer, SMC_SYSTEMID_LEN); 2244 memcpy(ini->peer_gid, pclc->lcl.gid, SMC_GID_SIZE); 2245 memcpy(ini->peer_mac, pclc->lcl.mac, ETH_ALEN); 2246 rc = smc_find_rdma_device(new_smc, ini); 2247 if (rc) { 2248 /* no RDMA device found */ 2249 return SMC_CLC_DECL_NOSMCDEV; 2250 } 2251 rc = smc_listen_rdma_init(new_smc, ini); 2252 if (rc) 2253 return rc; 2254 return smc_listen_rdma_reg(new_smc, ini->first_contact_local); 2255 } 2256 2257 /* determine the local device matching to proposal */ 2258 static int smc_listen_find_device(struct smc_sock *new_smc, 2259 struct smc_clc_msg_proposal *pclc, 2260 struct smc_init_info *ini) 2261 { 2262 int prfx_rc; 2263 2264 /* check for ISM device matching V2 proposed device */ 2265 smc_find_ism_v2_device_serv(new_smc, pclc, ini); 2266 if (ini->ism_dev[0]) 2267 return 0; 2268 2269 /* check for matching IP prefix and subnet length (V1) */ 2270 prfx_rc = smc_listen_prfx_check(new_smc, pclc); 2271 if (prfx_rc) 2272 smc_find_ism_store_rc(prfx_rc, ini); 2273 2274 /* get vlan id from IP device */ 2275 if (smc_vlan_by_tcpsk(new_smc->clcsock, ini)) 2276 return ini->rc ?: SMC_CLC_DECL_GETVLANERR; 2277 2278 /* check for ISM device matching V1 proposed device */ 2279 if (!prfx_rc) 2280 smc_find_ism_v1_device_serv(new_smc, pclc, ini); 2281 if (ini->ism_dev[0]) 2282 return 0; 2283 2284 if (!smcr_indicated(pclc->hdr.typev1) && 2285 !smcr_indicated(pclc->hdr.typev2)) 2286 /* skip RDMA and decline */ 2287 return ini->rc ?: SMC_CLC_DECL_NOSMCDDEV; 2288 2289 /* check if RDMA V2 is available */ 2290 smc_find_rdma_v2_device_serv(new_smc, pclc, ini); 2291 if (ini->smcrv2.ib_dev_v2) 2292 return 0; 2293 2294 /* check if RDMA V1 is available */ 2295 if (!prfx_rc) { 2296 int rc; 2297 2298 rc = smc_find_rdma_v1_device_serv(new_smc, pclc, ini); 2299 smc_find_ism_store_rc(rc, ini); 2300 return (!rc) ? 0 : ini->rc; 2301 } 2302 return SMC_CLC_DECL_NOSMCDEV; 2303 } 2304 2305 /* listen worker: finish RDMA setup */ 2306 static int smc_listen_rdma_finish(struct smc_sock *new_smc, 2307 struct smc_clc_msg_accept_confirm *cclc, 2308 bool local_first, 2309 struct smc_init_info *ini) 2310 { 2311 struct smc_link *link = new_smc->conn.lnk; 2312 int reason_code = 0; 2313 2314 if (local_first) 2315 smc_link_save_peer_info(link, cclc, ini); 2316 2317 if (smc_rmb_rtoken_handling(&new_smc->conn, link, cclc)) 2318 return SMC_CLC_DECL_ERR_RTOK; 2319 2320 if (local_first) { 2321 if (smc_ib_ready_link(link)) 2322 return SMC_CLC_DECL_ERR_RDYLNK; 2323 /* QP confirmation over RoCE fabric */ 2324 smc_llc_flow_initiate(link->lgr, SMC_LLC_FLOW_ADD_LINK); 2325 reason_code = smcr_serv_conf_first_link(new_smc); 2326 smc_llc_flow_stop(link->lgr, &link->lgr->llc_flow_lcl); 2327 } 2328 return reason_code; 2329 } 2330 2331 /* setup for connection of server */ 2332 static void smc_listen_work(struct work_struct *work) 2333 { 2334 struct smc_sock *new_smc = container_of(work, struct smc_sock, 2335 smc_listen_work); 2336 struct socket *newclcsock = new_smc->clcsock; 2337 struct smc_clc_msg_accept_confirm *cclc; 2338 struct smc_clc_msg_proposal_area *buf; 2339 struct smc_clc_msg_proposal *pclc; 2340 struct smc_init_info *ini = NULL; 2341 u8 proposal_version = SMC_V1; 2342 u8 accept_version; 2343 int rc = 0; 2344 2345 if (new_smc->listen_smc->sk.sk_state != SMC_LISTEN) 2346 return smc_listen_out_err(new_smc); 2347 2348 if (new_smc->use_fallback) { 2349 smc_listen_out_connected(new_smc); 2350 return; 2351 } 2352 2353 /* check if peer is smc capable */ 2354 if (!tcp_sk(newclcsock->sk)->syn_smc) { 2355 rc = smc_switch_to_fallback(new_smc, SMC_CLC_DECL_PEERNOSMC); 2356 if (rc) 2357 smc_listen_out_err(new_smc); 2358 else 2359 smc_listen_out_connected(new_smc); 2360 return; 2361 } 2362 2363 /* do inband token exchange - 2364 * wait for and receive SMC Proposal CLC message 2365 */ 2366 buf = kzalloc(sizeof(*buf), GFP_KERNEL); 2367 if (!buf) { 2368 rc = SMC_CLC_DECL_MEM; 2369 goto out_decl; 2370 } 2371 pclc = (struct smc_clc_msg_proposal *)buf; 2372 rc = smc_clc_wait_msg(new_smc, pclc, sizeof(*buf), 2373 SMC_CLC_PROPOSAL, CLC_WAIT_TIME); 2374 if (rc) 2375 goto out_decl; 2376 2377 if (pclc->hdr.version > SMC_V1) 2378 proposal_version = SMC_V2; 2379 2380 /* IPSec connections opt out of SMC optimizations */ 2381 if (using_ipsec(new_smc)) { 2382 rc = SMC_CLC_DECL_IPSEC; 2383 goto out_decl; 2384 } 2385 2386 ini = kzalloc(sizeof(*ini), GFP_KERNEL); 2387 if (!ini) { 2388 rc = SMC_CLC_DECL_MEM; 2389 goto out_decl; 2390 } 2391 2392 /* initial version checking */ 2393 rc = smc_listen_v2_check(new_smc, pclc, ini); 2394 if (rc) 2395 goto out_decl; 2396 2397 mutex_lock(&smc_server_lgr_pending); 2398 smc_close_init(new_smc); 2399 smc_rx_init(new_smc); 2400 smc_tx_init(new_smc); 2401 2402 /* determine ISM or RoCE device used for connection */ 2403 rc = smc_listen_find_device(new_smc, pclc, ini); 2404 if (rc) 2405 goto out_unlock; 2406 2407 /* send SMC Accept CLC message */ 2408 accept_version = ini->is_smcd ? ini->smcd_version : ini->smcr_version; 2409 rc = smc_clc_send_accept(new_smc, ini->first_contact_local, 2410 accept_version, ini->negotiated_eid); 2411 if (rc) 2412 goto out_unlock; 2413 2414 /* SMC-D does not need this lock any more */ 2415 if (ini->is_smcd) 2416 mutex_unlock(&smc_server_lgr_pending); 2417 2418 /* receive SMC Confirm CLC message */ 2419 memset(buf, 0, sizeof(*buf)); 2420 cclc = (struct smc_clc_msg_accept_confirm *)buf; 2421 rc = smc_clc_wait_msg(new_smc, cclc, sizeof(*buf), 2422 SMC_CLC_CONFIRM, CLC_WAIT_TIME); 2423 if (rc) { 2424 if (!ini->is_smcd) 2425 goto out_unlock; 2426 goto out_decl; 2427 } 2428 2429 /* finish worker */ 2430 if (!ini->is_smcd) { 2431 rc = smc_listen_rdma_finish(new_smc, cclc, 2432 ini->first_contact_local, ini); 2433 if (rc) 2434 goto out_unlock; 2435 mutex_unlock(&smc_server_lgr_pending); 2436 } 2437 smc_conn_save_peer_info(new_smc, cclc); 2438 smc_listen_out_connected(new_smc); 2439 SMC_STAT_SERV_SUCC_INC(sock_net(newclcsock->sk), ini); 2440 goto out_free; 2441 2442 out_unlock: 2443 mutex_unlock(&smc_server_lgr_pending); 2444 out_decl: 2445 smc_listen_decline(new_smc, rc, ini ? ini->first_contact_local : 0, 2446 proposal_version); 2447 out_free: 2448 kfree(ini); 2449 kfree(buf); 2450 } 2451 2452 static void smc_tcp_listen_work(struct work_struct *work) 2453 { 2454 struct smc_sock *lsmc = container_of(work, struct smc_sock, 2455 tcp_listen_work); 2456 struct sock *lsk = &lsmc->sk; 2457 struct smc_sock *new_smc; 2458 int rc = 0; 2459 2460 lock_sock(lsk); 2461 while (lsk->sk_state == SMC_LISTEN) { 2462 rc = smc_clcsock_accept(lsmc, &new_smc); 2463 if (rc) /* clcsock accept queue empty or error */ 2464 goto out; 2465 if (!new_smc) 2466 continue; 2467 2468 if (tcp_sk(new_smc->clcsock->sk)->syn_smc) 2469 atomic_inc(&lsmc->queued_smc_hs); 2470 2471 new_smc->listen_smc = lsmc; 2472 new_smc->use_fallback = lsmc->use_fallback; 2473 new_smc->fallback_rsn = lsmc->fallback_rsn; 2474 sock_hold(lsk); /* sock_put in smc_listen_work */ 2475 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work); 2476 smc_copy_sock_settings_to_smc(new_smc); 2477 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf; 2478 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf; 2479 sock_hold(&new_smc->sk); /* sock_put in passive closing */ 2480 if (!queue_work(smc_hs_wq, &new_smc->smc_listen_work)) 2481 sock_put(&new_smc->sk); 2482 } 2483 2484 out: 2485 release_sock(lsk); 2486 sock_put(&lsmc->sk); /* sock_hold in smc_clcsock_data_ready() */ 2487 } 2488 2489 static void smc_clcsock_data_ready(struct sock *listen_clcsock) 2490 { 2491 struct smc_sock *lsmc; 2492 2493 read_lock_bh(&listen_clcsock->sk_callback_lock); 2494 lsmc = smc_clcsock_user_data(listen_clcsock); 2495 if (!lsmc) 2496 goto out; 2497 lsmc->clcsk_data_ready(listen_clcsock); 2498 if (lsmc->sk.sk_state == SMC_LISTEN) { 2499 sock_hold(&lsmc->sk); /* sock_put in smc_tcp_listen_work() */ 2500 if (!queue_work(smc_tcp_ls_wq, &lsmc->tcp_listen_work)) 2501 sock_put(&lsmc->sk); 2502 } 2503 out: 2504 read_unlock_bh(&listen_clcsock->sk_callback_lock); 2505 } 2506 2507 static int smc_listen(struct socket *sock, int backlog) 2508 { 2509 struct sock *sk = sock->sk; 2510 struct smc_sock *smc; 2511 int rc; 2512 2513 smc = smc_sk(sk); 2514 lock_sock(sk); 2515 2516 rc = -EINVAL; 2517 if ((sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) || 2518 smc->connect_nonblock || sock->state != SS_UNCONNECTED) 2519 goto out; 2520 2521 rc = 0; 2522 if (sk->sk_state == SMC_LISTEN) { 2523 sk->sk_max_ack_backlog = backlog; 2524 goto out; 2525 } 2526 /* some socket options are handled in core, so we could not apply 2527 * them to the clc socket -- copy smc socket options to clc socket 2528 */ 2529 smc_copy_sock_settings_to_clc(smc); 2530 if (!smc->use_fallback) 2531 tcp_sk(smc->clcsock->sk)->syn_smc = 1; 2532 2533 /* save original sk_data_ready function and establish 2534 * smc-specific sk_data_ready function 2535 */ 2536 write_lock_bh(&smc->clcsock->sk->sk_callback_lock); 2537 smc->clcsock->sk->sk_user_data = 2538 (void *)((uintptr_t)smc | SK_USER_DATA_NOCOPY); 2539 smc_clcsock_replace_cb(&smc->clcsock->sk->sk_data_ready, 2540 smc_clcsock_data_ready, &smc->clcsk_data_ready); 2541 write_unlock_bh(&smc->clcsock->sk->sk_callback_lock); 2542 2543 /* save original ops */ 2544 smc->ori_af_ops = inet_csk(smc->clcsock->sk)->icsk_af_ops; 2545 2546 smc->af_ops = *smc->ori_af_ops; 2547 smc->af_ops.syn_recv_sock = smc_tcp_syn_recv_sock; 2548 2549 inet_csk(smc->clcsock->sk)->icsk_af_ops = &smc->af_ops; 2550 2551 if (smc->limit_smc_hs) 2552 tcp_sk(smc->clcsock->sk)->smc_hs_congested = smc_hs_congested; 2553 2554 rc = kernel_listen(smc->clcsock, backlog); 2555 if (rc) { 2556 write_lock_bh(&smc->clcsock->sk->sk_callback_lock); 2557 smc_clcsock_restore_cb(&smc->clcsock->sk->sk_data_ready, 2558 &smc->clcsk_data_ready); 2559 smc->clcsock->sk->sk_user_data = NULL; 2560 write_unlock_bh(&smc->clcsock->sk->sk_callback_lock); 2561 goto out; 2562 } 2563 sk->sk_max_ack_backlog = backlog; 2564 sk->sk_ack_backlog = 0; 2565 sk->sk_state = SMC_LISTEN; 2566 2567 out: 2568 release_sock(sk); 2569 return rc; 2570 } 2571 2572 static int smc_accept(struct socket *sock, struct socket *new_sock, 2573 int flags, bool kern) 2574 { 2575 struct sock *sk = sock->sk, *nsk; 2576 DECLARE_WAITQUEUE(wait, current); 2577 struct smc_sock *lsmc; 2578 long timeo; 2579 int rc = 0; 2580 2581 lsmc = smc_sk(sk); 2582 sock_hold(sk); /* sock_put below */ 2583 lock_sock(sk); 2584 2585 if (lsmc->sk.sk_state != SMC_LISTEN) { 2586 rc = -EINVAL; 2587 release_sock(sk); 2588 goto out; 2589 } 2590 2591 /* Wait for an incoming connection */ 2592 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); 2593 add_wait_queue_exclusive(sk_sleep(sk), &wait); 2594 while (!(nsk = smc_accept_dequeue(sk, new_sock))) { 2595 set_current_state(TASK_INTERRUPTIBLE); 2596 if (!timeo) { 2597 rc = -EAGAIN; 2598 break; 2599 } 2600 release_sock(sk); 2601 timeo = schedule_timeout(timeo); 2602 /* wakeup by sk_data_ready in smc_listen_work() */ 2603 sched_annotate_sleep(); 2604 lock_sock(sk); 2605 if (signal_pending(current)) { 2606 rc = sock_intr_errno(timeo); 2607 break; 2608 } 2609 } 2610 set_current_state(TASK_RUNNING); 2611 remove_wait_queue(sk_sleep(sk), &wait); 2612 2613 if (!rc) 2614 rc = sock_error(nsk); 2615 release_sock(sk); 2616 if (rc) 2617 goto out; 2618 2619 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) { 2620 /* wait till data arrives on the socket */ 2621 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept * 2622 MSEC_PER_SEC); 2623 if (smc_sk(nsk)->use_fallback) { 2624 struct sock *clcsk = smc_sk(nsk)->clcsock->sk; 2625 2626 lock_sock(clcsk); 2627 if (skb_queue_empty(&clcsk->sk_receive_queue)) 2628 sk_wait_data(clcsk, &timeo, NULL); 2629 release_sock(clcsk); 2630 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) { 2631 lock_sock(nsk); 2632 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available); 2633 release_sock(nsk); 2634 } 2635 } 2636 2637 out: 2638 sock_put(sk); /* sock_hold above */ 2639 return rc; 2640 } 2641 2642 static int smc_getname(struct socket *sock, struct sockaddr *addr, 2643 int peer) 2644 { 2645 struct smc_sock *smc; 2646 2647 if (peer && (sock->sk->sk_state != SMC_ACTIVE) && 2648 (sock->sk->sk_state != SMC_APPCLOSEWAIT1)) 2649 return -ENOTCONN; 2650 2651 smc = smc_sk(sock->sk); 2652 2653 return smc->clcsock->ops->getname(smc->clcsock, addr, peer); 2654 } 2655 2656 static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) 2657 { 2658 struct sock *sk = sock->sk; 2659 struct smc_sock *smc; 2660 int rc; 2661 2662 smc = smc_sk(sk); 2663 lock_sock(sk); 2664 2665 /* SMC does not support connect with fastopen */ 2666 if (msg->msg_flags & MSG_FASTOPEN) { 2667 /* not connected yet, fallback */ 2668 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) { 2669 rc = smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP); 2670 if (rc) 2671 goto out; 2672 } else { 2673 rc = -EINVAL; 2674 goto out; 2675 } 2676 } else if ((sk->sk_state != SMC_ACTIVE) && 2677 (sk->sk_state != SMC_APPCLOSEWAIT1) && 2678 (sk->sk_state != SMC_INIT)) { 2679 rc = -EPIPE; 2680 goto out; 2681 } 2682 2683 if (smc->use_fallback) { 2684 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len); 2685 } else { 2686 rc = smc_tx_sendmsg(smc, msg, len); 2687 SMC_STAT_TX_PAYLOAD(smc, len, rc); 2688 } 2689 out: 2690 release_sock(sk); 2691 return rc; 2692 } 2693 2694 static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, 2695 int flags) 2696 { 2697 struct sock *sk = sock->sk; 2698 struct smc_sock *smc; 2699 int rc = -ENOTCONN; 2700 2701 smc = smc_sk(sk); 2702 lock_sock(sk); 2703 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) { 2704 /* socket was connected before, no more data to read */ 2705 rc = 0; 2706 goto out; 2707 } 2708 if ((sk->sk_state == SMC_INIT) || 2709 (sk->sk_state == SMC_LISTEN) || 2710 (sk->sk_state == SMC_CLOSED)) 2711 goto out; 2712 2713 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) { 2714 rc = 0; 2715 goto out; 2716 } 2717 2718 if (smc->use_fallback) { 2719 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags); 2720 } else { 2721 msg->msg_namelen = 0; 2722 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags); 2723 SMC_STAT_RX_PAYLOAD(smc, rc, rc); 2724 } 2725 2726 out: 2727 release_sock(sk); 2728 return rc; 2729 } 2730 2731 static __poll_t smc_accept_poll(struct sock *parent) 2732 { 2733 struct smc_sock *isk = smc_sk(parent); 2734 __poll_t mask = 0; 2735 2736 spin_lock(&isk->accept_q_lock); 2737 if (!list_empty(&isk->accept_q)) 2738 mask = EPOLLIN | EPOLLRDNORM; 2739 spin_unlock(&isk->accept_q_lock); 2740 2741 return mask; 2742 } 2743 2744 static __poll_t smc_poll(struct file *file, struct socket *sock, 2745 poll_table *wait) 2746 { 2747 struct sock *sk = sock->sk; 2748 struct smc_sock *smc; 2749 __poll_t mask = 0; 2750 2751 if (!sk) 2752 return EPOLLNVAL; 2753 2754 smc = smc_sk(sock->sk); 2755 if (smc->use_fallback) { 2756 /* delegate to CLC child sock */ 2757 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait); 2758 sk->sk_err = smc->clcsock->sk->sk_err; 2759 } else { 2760 if (sk->sk_state != SMC_CLOSED) 2761 sock_poll_wait(file, sock, wait); 2762 if (sk->sk_err) 2763 mask |= EPOLLERR; 2764 if ((sk->sk_shutdown == SHUTDOWN_MASK) || 2765 (sk->sk_state == SMC_CLOSED)) 2766 mask |= EPOLLHUP; 2767 if (sk->sk_state == SMC_LISTEN) { 2768 /* woken up by sk_data_ready in smc_listen_work() */ 2769 mask |= smc_accept_poll(sk); 2770 } else if (smc->use_fallback) { /* as result of connect_work()*/ 2771 mask |= smc->clcsock->ops->poll(file, smc->clcsock, 2772 wait); 2773 sk->sk_err = smc->clcsock->sk->sk_err; 2774 } else { 2775 if ((sk->sk_state != SMC_INIT && 2776 atomic_read(&smc->conn.sndbuf_space)) || 2777 sk->sk_shutdown & SEND_SHUTDOWN) { 2778 mask |= EPOLLOUT | EPOLLWRNORM; 2779 } else { 2780 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); 2781 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 2782 } 2783 if (atomic_read(&smc->conn.bytes_to_rcv)) 2784 mask |= EPOLLIN | EPOLLRDNORM; 2785 if (sk->sk_shutdown & RCV_SHUTDOWN) 2786 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 2787 if (sk->sk_state == SMC_APPCLOSEWAIT1) 2788 mask |= EPOLLIN; 2789 if (smc->conn.urg_state == SMC_URG_VALID) 2790 mask |= EPOLLPRI; 2791 } 2792 } 2793 2794 return mask; 2795 } 2796 2797 static int smc_shutdown(struct socket *sock, int how) 2798 { 2799 struct sock *sk = sock->sk; 2800 bool do_shutdown = true; 2801 struct smc_sock *smc; 2802 int rc = -EINVAL; 2803 int old_state; 2804 int rc1 = 0; 2805 2806 smc = smc_sk(sk); 2807 2808 if ((how < SHUT_RD) || (how > SHUT_RDWR)) 2809 return rc; 2810 2811 lock_sock(sk); 2812 2813 if (sock->state == SS_CONNECTING) { 2814 if (sk->sk_state == SMC_ACTIVE) 2815 sock->state = SS_CONNECTED; 2816 else if (sk->sk_state == SMC_PEERCLOSEWAIT1 || 2817 sk->sk_state == SMC_PEERCLOSEWAIT2 || 2818 sk->sk_state == SMC_APPCLOSEWAIT1 || 2819 sk->sk_state == SMC_APPCLOSEWAIT2 || 2820 sk->sk_state == SMC_APPFINCLOSEWAIT) 2821 sock->state = SS_DISCONNECTING; 2822 } 2823 2824 rc = -ENOTCONN; 2825 if ((sk->sk_state != SMC_ACTIVE) && 2826 (sk->sk_state != SMC_PEERCLOSEWAIT1) && 2827 (sk->sk_state != SMC_PEERCLOSEWAIT2) && 2828 (sk->sk_state != SMC_APPCLOSEWAIT1) && 2829 (sk->sk_state != SMC_APPCLOSEWAIT2) && 2830 (sk->sk_state != SMC_APPFINCLOSEWAIT)) 2831 goto out; 2832 if (smc->use_fallback) { 2833 rc = kernel_sock_shutdown(smc->clcsock, how); 2834 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown; 2835 if (sk->sk_shutdown == SHUTDOWN_MASK) { 2836 sk->sk_state = SMC_CLOSED; 2837 sk->sk_socket->state = SS_UNCONNECTED; 2838 sock_put(sk); 2839 } 2840 goto out; 2841 } 2842 switch (how) { 2843 case SHUT_RDWR: /* shutdown in both directions */ 2844 old_state = sk->sk_state; 2845 rc = smc_close_active(smc); 2846 if (old_state == SMC_ACTIVE && 2847 sk->sk_state == SMC_PEERCLOSEWAIT1) 2848 do_shutdown = false; 2849 break; 2850 case SHUT_WR: 2851 rc = smc_close_shutdown_write(smc); 2852 break; 2853 case SHUT_RD: 2854 rc = 0; 2855 /* nothing more to do because peer is not involved */ 2856 break; 2857 } 2858 if (do_shutdown && smc->clcsock) 2859 rc1 = kernel_sock_shutdown(smc->clcsock, how); 2860 /* map sock_shutdown_cmd constants to sk_shutdown value range */ 2861 sk->sk_shutdown |= how + 1; 2862 2863 if (sk->sk_state == SMC_CLOSED) 2864 sock->state = SS_UNCONNECTED; 2865 else 2866 sock->state = SS_DISCONNECTING; 2867 out: 2868 release_sock(sk); 2869 return rc ? rc : rc1; 2870 } 2871 2872 static int __smc_getsockopt(struct socket *sock, int level, int optname, 2873 char __user *optval, int __user *optlen) 2874 { 2875 struct smc_sock *smc; 2876 int val, len; 2877 2878 smc = smc_sk(sock->sk); 2879 2880 if (get_user(len, optlen)) 2881 return -EFAULT; 2882 2883 len = min_t(int, len, sizeof(int)); 2884 2885 if (len < 0) 2886 return -EINVAL; 2887 2888 switch (optname) { 2889 case SMC_LIMIT_HS: 2890 val = smc->limit_smc_hs; 2891 break; 2892 default: 2893 return -EOPNOTSUPP; 2894 } 2895 2896 if (put_user(len, optlen)) 2897 return -EFAULT; 2898 if (copy_to_user(optval, &val, len)) 2899 return -EFAULT; 2900 2901 return 0; 2902 } 2903 2904 static int __smc_setsockopt(struct socket *sock, int level, int optname, 2905 sockptr_t optval, unsigned int optlen) 2906 { 2907 struct sock *sk = sock->sk; 2908 struct smc_sock *smc; 2909 int val, rc; 2910 2911 smc = smc_sk(sk); 2912 2913 lock_sock(sk); 2914 switch (optname) { 2915 case SMC_LIMIT_HS: 2916 if (optlen < sizeof(int)) { 2917 rc = -EINVAL; 2918 break; 2919 } 2920 if (copy_from_sockptr(&val, optval, sizeof(int))) { 2921 rc = -EFAULT; 2922 break; 2923 } 2924 2925 smc->limit_smc_hs = !!val; 2926 rc = 0; 2927 break; 2928 default: 2929 rc = -EOPNOTSUPP; 2930 break; 2931 } 2932 release_sock(sk); 2933 2934 return rc; 2935 } 2936 2937 static int smc_setsockopt(struct socket *sock, int level, int optname, 2938 sockptr_t optval, unsigned int optlen) 2939 { 2940 struct sock *sk = sock->sk; 2941 struct smc_sock *smc; 2942 int val, rc; 2943 2944 if (level == SOL_TCP && optname == TCP_ULP) 2945 return -EOPNOTSUPP; 2946 else if (level == SOL_SMC) 2947 return __smc_setsockopt(sock, level, optname, optval, optlen); 2948 2949 smc = smc_sk(sk); 2950 2951 /* generic setsockopts reaching us here always apply to the 2952 * CLC socket 2953 */ 2954 mutex_lock(&smc->clcsock_release_lock); 2955 if (!smc->clcsock) { 2956 mutex_unlock(&smc->clcsock_release_lock); 2957 return -EBADF; 2958 } 2959 if (unlikely(!smc->clcsock->ops->setsockopt)) 2960 rc = -EOPNOTSUPP; 2961 else 2962 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname, 2963 optval, optlen); 2964 if (smc->clcsock->sk->sk_err) { 2965 sk->sk_err = smc->clcsock->sk->sk_err; 2966 sk_error_report(sk); 2967 } 2968 mutex_unlock(&smc->clcsock_release_lock); 2969 2970 if (optlen < sizeof(int)) 2971 return -EINVAL; 2972 if (copy_from_sockptr(&val, optval, sizeof(int))) 2973 return -EFAULT; 2974 2975 lock_sock(sk); 2976 if (rc || smc->use_fallback) 2977 goto out; 2978 switch (optname) { 2979 case TCP_FASTOPEN: 2980 case TCP_FASTOPEN_CONNECT: 2981 case TCP_FASTOPEN_KEY: 2982 case TCP_FASTOPEN_NO_COOKIE: 2983 /* option not supported by SMC */ 2984 if (sk->sk_state == SMC_INIT && !smc->connect_nonblock) { 2985 rc = smc_switch_to_fallback(smc, SMC_CLC_DECL_OPTUNSUPP); 2986 } else { 2987 rc = -EINVAL; 2988 } 2989 break; 2990 case TCP_NODELAY: 2991 if (sk->sk_state != SMC_INIT && 2992 sk->sk_state != SMC_LISTEN && 2993 sk->sk_state != SMC_CLOSED) { 2994 if (val) { 2995 SMC_STAT_INC(smc, ndly_cnt); 2996 smc_tx_pending(&smc->conn); 2997 cancel_delayed_work(&smc->conn.tx_work); 2998 } 2999 } 3000 break; 3001 case TCP_CORK: 3002 if (sk->sk_state != SMC_INIT && 3003 sk->sk_state != SMC_LISTEN && 3004 sk->sk_state != SMC_CLOSED) { 3005 if (!val) { 3006 SMC_STAT_INC(smc, cork_cnt); 3007 smc_tx_pending(&smc->conn); 3008 cancel_delayed_work(&smc->conn.tx_work); 3009 } 3010 } 3011 break; 3012 case TCP_DEFER_ACCEPT: 3013 smc->sockopt_defer_accept = val; 3014 break; 3015 default: 3016 break; 3017 } 3018 out: 3019 release_sock(sk); 3020 3021 return rc; 3022 } 3023 3024 static int smc_getsockopt(struct socket *sock, int level, int optname, 3025 char __user *optval, int __user *optlen) 3026 { 3027 struct smc_sock *smc; 3028 int rc; 3029 3030 if (level == SOL_SMC) 3031 return __smc_getsockopt(sock, level, optname, optval, optlen); 3032 3033 smc = smc_sk(sock->sk); 3034 mutex_lock(&smc->clcsock_release_lock); 3035 if (!smc->clcsock) { 3036 mutex_unlock(&smc->clcsock_release_lock); 3037 return -EBADF; 3038 } 3039 /* socket options apply to the CLC socket */ 3040 if (unlikely(!smc->clcsock->ops->getsockopt)) { 3041 mutex_unlock(&smc->clcsock_release_lock); 3042 return -EOPNOTSUPP; 3043 } 3044 rc = smc->clcsock->ops->getsockopt(smc->clcsock, level, optname, 3045 optval, optlen); 3046 mutex_unlock(&smc->clcsock_release_lock); 3047 return rc; 3048 } 3049 3050 static int smc_ioctl(struct socket *sock, unsigned int cmd, 3051 unsigned long arg) 3052 { 3053 union smc_host_cursor cons, urg; 3054 struct smc_connection *conn; 3055 struct smc_sock *smc; 3056 int answ; 3057 3058 smc = smc_sk(sock->sk); 3059 conn = &smc->conn; 3060 lock_sock(&smc->sk); 3061 if (smc->use_fallback) { 3062 if (!smc->clcsock) { 3063 release_sock(&smc->sk); 3064 return -EBADF; 3065 } 3066 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg); 3067 release_sock(&smc->sk); 3068 return answ; 3069 } 3070 switch (cmd) { 3071 case SIOCINQ: /* same as FIONREAD */ 3072 if (smc->sk.sk_state == SMC_LISTEN) { 3073 release_sock(&smc->sk); 3074 return -EINVAL; 3075 } 3076 if (smc->sk.sk_state == SMC_INIT || 3077 smc->sk.sk_state == SMC_CLOSED) 3078 answ = 0; 3079 else 3080 answ = atomic_read(&smc->conn.bytes_to_rcv); 3081 break; 3082 case SIOCOUTQ: 3083 /* output queue size (not send + not acked) */ 3084 if (smc->sk.sk_state == SMC_LISTEN) { 3085 release_sock(&smc->sk); 3086 return -EINVAL; 3087 } 3088 if (smc->sk.sk_state == SMC_INIT || 3089 smc->sk.sk_state == SMC_CLOSED) 3090 answ = 0; 3091 else 3092 answ = smc->conn.sndbuf_desc->len - 3093 atomic_read(&smc->conn.sndbuf_space); 3094 break; 3095 case SIOCOUTQNSD: 3096 /* output queue size (not send only) */ 3097 if (smc->sk.sk_state == SMC_LISTEN) { 3098 release_sock(&smc->sk); 3099 return -EINVAL; 3100 } 3101 if (smc->sk.sk_state == SMC_INIT || 3102 smc->sk.sk_state == SMC_CLOSED) 3103 answ = 0; 3104 else 3105 answ = smc_tx_prepared_sends(&smc->conn); 3106 break; 3107 case SIOCATMARK: 3108 if (smc->sk.sk_state == SMC_LISTEN) { 3109 release_sock(&smc->sk); 3110 return -EINVAL; 3111 } 3112 if (smc->sk.sk_state == SMC_INIT || 3113 smc->sk.sk_state == SMC_CLOSED) { 3114 answ = 0; 3115 } else { 3116 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn); 3117 smc_curs_copy(&urg, &conn->urg_curs, conn); 3118 answ = smc_curs_diff(conn->rmb_desc->len, 3119 &cons, &urg) == 1; 3120 } 3121 break; 3122 default: 3123 release_sock(&smc->sk); 3124 return -ENOIOCTLCMD; 3125 } 3126 release_sock(&smc->sk); 3127 3128 return put_user(answ, (int __user *)arg); 3129 } 3130 3131 static ssize_t smc_sendpage(struct socket *sock, struct page *page, 3132 int offset, size_t size, int flags) 3133 { 3134 struct sock *sk = sock->sk; 3135 struct smc_sock *smc; 3136 int rc = -EPIPE; 3137 3138 smc = smc_sk(sk); 3139 lock_sock(sk); 3140 if (sk->sk_state != SMC_ACTIVE) { 3141 release_sock(sk); 3142 goto out; 3143 } 3144 release_sock(sk); 3145 if (smc->use_fallback) { 3146 rc = kernel_sendpage(smc->clcsock, page, offset, 3147 size, flags); 3148 } else { 3149 lock_sock(sk); 3150 rc = smc_tx_sendpage(smc, page, offset, size, flags); 3151 release_sock(sk); 3152 SMC_STAT_INC(smc, sendpage_cnt); 3153 } 3154 3155 out: 3156 return rc; 3157 } 3158 3159 /* Map the affected portions of the rmbe into an spd, note the number of bytes 3160 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor 3161 * updates till whenever a respective page has been fully processed. 3162 * Note that subsequent recv() calls have to wait till all splice() processing 3163 * completed. 3164 */ 3165 static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos, 3166 struct pipe_inode_info *pipe, size_t len, 3167 unsigned int flags) 3168 { 3169 struct sock *sk = sock->sk; 3170 struct smc_sock *smc; 3171 int rc = -ENOTCONN; 3172 3173 smc = smc_sk(sk); 3174 lock_sock(sk); 3175 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) { 3176 /* socket was connected before, no more data to read */ 3177 rc = 0; 3178 goto out; 3179 } 3180 if (sk->sk_state == SMC_INIT || 3181 sk->sk_state == SMC_LISTEN || 3182 sk->sk_state == SMC_CLOSED) 3183 goto out; 3184 3185 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) { 3186 rc = 0; 3187 goto out; 3188 } 3189 3190 if (smc->use_fallback) { 3191 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos, 3192 pipe, len, flags); 3193 } else { 3194 if (*ppos) { 3195 rc = -ESPIPE; 3196 goto out; 3197 } 3198 if (flags & SPLICE_F_NONBLOCK) 3199 flags = MSG_DONTWAIT; 3200 else 3201 flags = 0; 3202 SMC_STAT_INC(smc, splice_cnt); 3203 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags); 3204 } 3205 out: 3206 release_sock(sk); 3207 3208 return rc; 3209 } 3210 3211 /* must look like tcp */ 3212 static const struct proto_ops smc_sock_ops = { 3213 .family = PF_SMC, 3214 .owner = THIS_MODULE, 3215 .release = smc_release, 3216 .bind = smc_bind, 3217 .connect = smc_connect, 3218 .socketpair = sock_no_socketpair, 3219 .accept = smc_accept, 3220 .getname = smc_getname, 3221 .poll = smc_poll, 3222 .ioctl = smc_ioctl, 3223 .listen = smc_listen, 3224 .shutdown = smc_shutdown, 3225 .setsockopt = smc_setsockopt, 3226 .getsockopt = smc_getsockopt, 3227 .sendmsg = smc_sendmsg, 3228 .recvmsg = smc_recvmsg, 3229 .mmap = sock_no_mmap, 3230 .sendpage = smc_sendpage, 3231 .splice_read = smc_splice_read, 3232 }; 3233 3234 static int __smc_create(struct net *net, struct socket *sock, int protocol, 3235 int kern, struct socket *clcsock) 3236 { 3237 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET; 3238 struct smc_sock *smc; 3239 struct sock *sk; 3240 int rc; 3241 3242 rc = -ESOCKTNOSUPPORT; 3243 if (sock->type != SOCK_STREAM) 3244 goto out; 3245 3246 rc = -EPROTONOSUPPORT; 3247 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6) 3248 goto out; 3249 3250 rc = -ENOBUFS; 3251 sock->ops = &smc_sock_ops; 3252 sock->state = SS_UNCONNECTED; 3253 sk = smc_sock_alloc(net, sock, protocol); 3254 if (!sk) 3255 goto out; 3256 3257 /* create internal TCP socket for CLC handshake and fallback */ 3258 smc = smc_sk(sk); 3259 smc->use_fallback = false; /* assume rdma capability first */ 3260 smc->fallback_rsn = 0; 3261 3262 /* default behavior from limit_smc_hs in every net namespace */ 3263 smc->limit_smc_hs = net->smc.limit_smc_hs; 3264 3265 rc = 0; 3266 if (!clcsock) { 3267 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP, 3268 &smc->clcsock); 3269 if (rc) { 3270 sk_common_release(sk); 3271 goto out; 3272 } 3273 } else { 3274 smc->clcsock = clcsock; 3275 } 3276 3277 out: 3278 return rc; 3279 } 3280 3281 static int smc_create(struct net *net, struct socket *sock, int protocol, 3282 int kern) 3283 { 3284 return __smc_create(net, sock, protocol, kern, NULL); 3285 } 3286 3287 static const struct net_proto_family smc_sock_family_ops = { 3288 .family = PF_SMC, 3289 .owner = THIS_MODULE, 3290 .create = smc_create, 3291 }; 3292 3293 static int smc_ulp_init(struct sock *sk) 3294 { 3295 struct socket *tcp = sk->sk_socket; 3296 struct net *net = sock_net(sk); 3297 struct socket *smcsock; 3298 int protocol, ret; 3299 3300 /* only TCP can be replaced */ 3301 if (tcp->type != SOCK_STREAM || sk->sk_protocol != IPPROTO_TCP || 3302 (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)) 3303 return -ESOCKTNOSUPPORT; 3304 /* don't handle wq now */ 3305 if (tcp->state != SS_UNCONNECTED || !tcp->file || tcp->wq.fasync_list) 3306 return -ENOTCONN; 3307 3308 if (sk->sk_family == AF_INET) 3309 protocol = SMCPROTO_SMC; 3310 else 3311 protocol = SMCPROTO_SMC6; 3312 3313 smcsock = sock_alloc(); 3314 if (!smcsock) 3315 return -ENFILE; 3316 3317 smcsock->type = SOCK_STREAM; 3318 __module_get(THIS_MODULE); /* tried in __tcp_ulp_find_autoload */ 3319 ret = __smc_create(net, smcsock, protocol, 1, tcp); 3320 if (ret) { 3321 sock_release(smcsock); /* module_put() which ops won't be NULL */ 3322 return ret; 3323 } 3324 3325 /* replace tcp socket to smc */ 3326 smcsock->file = tcp->file; 3327 smcsock->file->private_data = smcsock; 3328 smcsock->file->f_inode = SOCK_INODE(smcsock); /* replace inode when sock_close */ 3329 smcsock->file->f_path.dentry->d_inode = SOCK_INODE(smcsock); /* dput() in __fput */ 3330 tcp->file = NULL; 3331 3332 return ret; 3333 } 3334 3335 static void smc_ulp_clone(const struct request_sock *req, struct sock *newsk, 3336 const gfp_t priority) 3337 { 3338 struct inet_connection_sock *icsk = inet_csk(newsk); 3339 3340 /* don't inherit ulp ops to child when listen */ 3341 icsk->icsk_ulp_ops = NULL; 3342 } 3343 3344 static struct tcp_ulp_ops smc_ulp_ops __read_mostly = { 3345 .name = "smc", 3346 .owner = THIS_MODULE, 3347 .init = smc_ulp_init, 3348 .clone = smc_ulp_clone, 3349 }; 3350 3351 unsigned int smc_net_id; 3352 3353 static __net_init int smc_net_init(struct net *net) 3354 { 3355 int rc; 3356 3357 rc = smc_sysctl_net_init(net); 3358 if (rc) 3359 return rc; 3360 return smc_pnet_net_init(net); 3361 } 3362 3363 static void __net_exit smc_net_exit(struct net *net) 3364 { 3365 smc_sysctl_net_exit(net); 3366 smc_pnet_net_exit(net); 3367 } 3368 3369 static __net_init int smc_net_stat_init(struct net *net) 3370 { 3371 return smc_stats_init(net); 3372 } 3373 3374 static void __net_exit smc_net_stat_exit(struct net *net) 3375 { 3376 smc_stats_exit(net); 3377 } 3378 3379 static struct pernet_operations smc_net_ops = { 3380 .init = smc_net_init, 3381 .exit = smc_net_exit, 3382 .id = &smc_net_id, 3383 .size = sizeof(struct smc_net), 3384 }; 3385 3386 static struct pernet_operations smc_net_stat_ops = { 3387 .init = smc_net_stat_init, 3388 .exit = smc_net_stat_exit, 3389 }; 3390 3391 static int __init smc_init(void) 3392 { 3393 int rc; 3394 3395 rc = register_pernet_subsys(&smc_net_ops); 3396 if (rc) 3397 return rc; 3398 3399 rc = register_pernet_subsys(&smc_net_stat_ops); 3400 if (rc) 3401 goto out_pernet_subsys; 3402 3403 rc = smc_ism_init(); 3404 if (rc) 3405 goto out_pernet_subsys_stat; 3406 smc_clc_init(); 3407 3408 rc = smc_nl_init(); 3409 if (rc) 3410 goto out_ism; 3411 3412 rc = smc_pnet_init(); 3413 if (rc) 3414 goto out_nl; 3415 3416 rc = -ENOMEM; 3417 3418 smc_tcp_ls_wq = alloc_workqueue("smc_tcp_ls_wq", 0, 0); 3419 if (!smc_tcp_ls_wq) 3420 goto out_pnet; 3421 3422 smc_hs_wq = alloc_workqueue("smc_hs_wq", 0, 0); 3423 if (!smc_hs_wq) 3424 goto out_alloc_tcp_ls_wq; 3425 3426 smc_close_wq = alloc_workqueue("smc_close_wq", 0, 0); 3427 if (!smc_close_wq) 3428 goto out_alloc_hs_wq; 3429 3430 rc = smc_core_init(); 3431 if (rc) { 3432 pr_err("%s: smc_core_init fails with %d\n", __func__, rc); 3433 goto out_alloc_wqs; 3434 } 3435 3436 rc = smc_llc_init(); 3437 if (rc) { 3438 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc); 3439 goto out_core; 3440 } 3441 3442 rc = smc_cdc_init(); 3443 if (rc) { 3444 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc); 3445 goto out_core; 3446 } 3447 3448 rc = proto_register(&smc_proto, 1); 3449 if (rc) { 3450 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc); 3451 goto out_core; 3452 } 3453 3454 rc = proto_register(&smc_proto6, 1); 3455 if (rc) { 3456 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc); 3457 goto out_proto; 3458 } 3459 3460 rc = sock_register(&smc_sock_family_ops); 3461 if (rc) { 3462 pr_err("%s: sock_register fails with %d\n", __func__, rc); 3463 goto out_proto6; 3464 } 3465 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht); 3466 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht); 3467 3468 rc = smc_ib_register_client(); 3469 if (rc) { 3470 pr_err("%s: ib_register fails with %d\n", __func__, rc); 3471 goto out_sock; 3472 } 3473 3474 rc = tcp_register_ulp(&smc_ulp_ops); 3475 if (rc) { 3476 pr_err("%s: tcp_ulp_register fails with %d\n", __func__, rc); 3477 goto out_ib; 3478 } 3479 3480 static_branch_enable(&tcp_have_smc); 3481 return 0; 3482 3483 out_ib: 3484 smc_ib_unregister_client(); 3485 out_sock: 3486 sock_unregister(PF_SMC); 3487 out_proto6: 3488 proto_unregister(&smc_proto6); 3489 out_proto: 3490 proto_unregister(&smc_proto); 3491 out_core: 3492 smc_core_exit(); 3493 out_alloc_wqs: 3494 destroy_workqueue(smc_close_wq); 3495 out_alloc_hs_wq: 3496 destroy_workqueue(smc_hs_wq); 3497 out_alloc_tcp_ls_wq: 3498 destroy_workqueue(smc_tcp_ls_wq); 3499 out_pnet: 3500 smc_pnet_exit(); 3501 out_nl: 3502 smc_nl_exit(); 3503 out_ism: 3504 smc_clc_exit(); 3505 smc_ism_exit(); 3506 out_pernet_subsys_stat: 3507 unregister_pernet_subsys(&smc_net_stat_ops); 3508 out_pernet_subsys: 3509 unregister_pernet_subsys(&smc_net_ops); 3510 3511 return rc; 3512 } 3513 3514 static void __exit smc_exit(void) 3515 { 3516 static_branch_disable(&tcp_have_smc); 3517 tcp_unregister_ulp(&smc_ulp_ops); 3518 sock_unregister(PF_SMC); 3519 smc_core_exit(); 3520 smc_ib_unregister_client(); 3521 smc_ism_exit(); 3522 destroy_workqueue(smc_close_wq); 3523 destroy_workqueue(smc_tcp_ls_wq); 3524 destroy_workqueue(smc_hs_wq); 3525 proto_unregister(&smc_proto6); 3526 proto_unregister(&smc_proto); 3527 smc_pnet_exit(); 3528 smc_nl_exit(); 3529 smc_clc_exit(); 3530 unregister_pernet_subsys(&smc_net_stat_ops); 3531 unregister_pernet_subsys(&smc_net_ops); 3532 rcu_barrier(); 3533 } 3534 3535 module_init(smc_init); 3536 module_exit(smc_exit); 3537 3538 MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>"); 3539 MODULE_DESCRIPTION("smc socket address family"); 3540 MODULE_LICENSE("GPL"); 3541 MODULE_ALIAS_NETPROTO(PF_SMC); 3542 MODULE_ALIAS_TCP_ULP("smc"); 3543 MODULE_ALIAS_GENL_FAMILY(SMC_GENL_FAMILY_NAME); 3544