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