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