1 /* 2 * Shared Memory Communications over RDMA (SMC-R) and RoCE 3 * 4 * AF_SMC protocol family socket handler keeping the AF_INET sock address type 5 * applies to SOCK_STREAM sockets only 6 * offers an alternative communication option for TCP-protocol sockets 7 * applicable with RoCE-cards only 8 * 9 * Initial restrictions: 10 * - support for alternate links postponed 11 * 12 * Copyright IBM Corp. 2016, 2018 13 * 14 * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com> 15 * based on prototype from Frank Blaschka 16 */ 17 18 #define KMSG_COMPONENT "smc" 19 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 20 21 #include <linux/module.h> 22 #include <linux/socket.h> 23 #include <linux/workqueue.h> 24 #include <linux/in.h> 25 #include <linux/sched/signal.h> 26 #include <linux/if_vlan.h> 27 28 #include <net/sock.h> 29 #include <net/tcp.h> 30 #include <net/smc.h> 31 #include <asm/ioctls.h> 32 33 #include <net/net_namespace.h> 34 #include <net/netns/generic.h> 35 #include "smc_netns.h" 36 37 #include "smc.h" 38 #include "smc_clc.h" 39 #include "smc_llc.h" 40 #include "smc_cdc.h" 41 #include "smc_core.h" 42 #include "smc_ib.h" 43 #include "smc_ism.h" 44 #include "smc_pnet.h" 45 #include "smc_tx.h" 46 #include "smc_rx.h" 47 #include "smc_close.h" 48 49 static DEFINE_MUTEX(smc_server_lgr_pending); /* serialize link group 50 * creation on server 51 */ 52 static DEFINE_MUTEX(smc_client_lgr_pending); /* serialize link group 53 * creation on client 54 */ 55 56 static void smc_tcp_listen_work(struct work_struct *); 57 static void smc_connect_work(struct work_struct *); 58 59 static void smc_set_keepalive(struct sock *sk, int val) 60 { 61 struct smc_sock *smc = smc_sk(sk); 62 63 smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val); 64 } 65 66 static struct smc_hashinfo smc_v4_hashinfo = { 67 .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock), 68 }; 69 70 static struct smc_hashinfo smc_v6_hashinfo = { 71 .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock), 72 }; 73 74 int smc_hash_sk(struct sock *sk) 75 { 76 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash; 77 struct hlist_head *head; 78 79 head = &h->ht; 80 81 write_lock_bh(&h->lock); 82 sk_add_node(sk, head); 83 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 84 write_unlock_bh(&h->lock); 85 86 return 0; 87 } 88 EXPORT_SYMBOL_GPL(smc_hash_sk); 89 90 void smc_unhash_sk(struct sock *sk) 91 { 92 struct smc_hashinfo *h = sk->sk_prot->h.smc_hash; 93 94 write_lock_bh(&h->lock); 95 if (sk_del_node_init(sk)) 96 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 97 write_unlock_bh(&h->lock); 98 } 99 EXPORT_SYMBOL_GPL(smc_unhash_sk); 100 101 struct proto smc_proto = { 102 .name = "SMC", 103 .owner = THIS_MODULE, 104 .keepalive = smc_set_keepalive, 105 .hash = smc_hash_sk, 106 .unhash = smc_unhash_sk, 107 .obj_size = sizeof(struct smc_sock), 108 .h.smc_hash = &smc_v4_hashinfo, 109 .slab_flags = SLAB_TYPESAFE_BY_RCU, 110 }; 111 EXPORT_SYMBOL_GPL(smc_proto); 112 113 struct proto smc_proto6 = { 114 .name = "SMC6", 115 .owner = THIS_MODULE, 116 .keepalive = smc_set_keepalive, 117 .hash = smc_hash_sk, 118 .unhash = smc_unhash_sk, 119 .obj_size = sizeof(struct smc_sock), 120 .h.smc_hash = &smc_v6_hashinfo, 121 .slab_flags = SLAB_TYPESAFE_BY_RCU, 122 }; 123 EXPORT_SYMBOL_GPL(smc_proto6); 124 125 static int smc_release(struct socket *sock) 126 { 127 struct sock *sk = sock->sk; 128 struct smc_sock *smc; 129 int rc = 0; 130 131 if (!sk) 132 goto out; 133 134 smc = smc_sk(sk); 135 136 /* cleanup for a dangling non-blocking connect */ 137 if (smc->connect_info && sk->sk_state == SMC_INIT) 138 tcp_abort(smc->clcsock->sk, ECONNABORTED); 139 flush_work(&smc->connect_work); 140 kfree(smc->connect_info); 141 smc->connect_info = NULL; 142 143 if (sk->sk_state == SMC_LISTEN) 144 /* smc_close_non_accepted() is called and acquires 145 * sock lock for child sockets again 146 */ 147 lock_sock_nested(sk, SINGLE_DEPTH_NESTING); 148 else 149 lock_sock(sk); 150 151 if (!smc->use_fallback) { 152 rc = smc_close_active(smc); 153 sock_set_flag(sk, SOCK_DEAD); 154 sk->sk_shutdown |= SHUTDOWN_MASK; 155 } else { 156 if (sk->sk_state != SMC_LISTEN && sk->sk_state != SMC_INIT) 157 sock_put(sk); /* passive closing */ 158 if (sk->sk_state == SMC_LISTEN) { 159 /* wake up clcsock accept */ 160 rc = kernel_sock_shutdown(smc->clcsock, SHUT_RDWR); 161 } 162 sk->sk_state = SMC_CLOSED; 163 sk->sk_state_change(sk); 164 } 165 166 sk->sk_prot->unhash(sk); 167 168 if (sk->sk_state == SMC_CLOSED) { 169 if (smc->clcsock) { 170 mutex_lock(&smc->clcsock_release_lock); 171 sock_release(smc->clcsock); 172 smc->clcsock = NULL; 173 mutex_unlock(&smc->clcsock_release_lock); 174 } 175 if (!smc->use_fallback) 176 smc_conn_free(&smc->conn); 177 } 178 179 /* detach socket */ 180 sock_orphan(sk); 181 sock->sk = NULL; 182 release_sock(sk); 183 184 sock_put(sk); /* final sock_put */ 185 out: 186 return rc; 187 } 188 189 static void smc_destruct(struct sock *sk) 190 { 191 if (sk->sk_state != SMC_CLOSED) 192 return; 193 if (!sock_flag(sk, SOCK_DEAD)) 194 return; 195 196 sk_refcnt_debug_dec(sk); 197 } 198 199 static struct sock *smc_sock_alloc(struct net *net, struct socket *sock, 200 int protocol) 201 { 202 struct smc_sock *smc; 203 struct proto *prot; 204 struct sock *sk; 205 206 prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto; 207 sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0); 208 if (!sk) 209 return NULL; 210 211 sock_init_data(sock, sk); /* sets sk_refcnt to 1 */ 212 sk->sk_state = SMC_INIT; 213 sk->sk_destruct = smc_destruct; 214 sk->sk_protocol = protocol; 215 smc = smc_sk(sk); 216 INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work); 217 INIT_WORK(&smc->connect_work, smc_connect_work); 218 INIT_DELAYED_WORK(&smc->conn.tx_work, smc_tx_work); 219 INIT_LIST_HEAD(&smc->accept_q); 220 spin_lock_init(&smc->accept_q_lock); 221 spin_lock_init(&smc->conn.send_lock); 222 sk->sk_prot->hash(sk); 223 sk_refcnt_debug_inc(sk); 224 mutex_init(&smc->clcsock_release_lock); 225 226 return sk; 227 } 228 229 static int smc_bind(struct socket *sock, struct sockaddr *uaddr, 230 int addr_len) 231 { 232 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr; 233 struct sock *sk = sock->sk; 234 struct smc_sock *smc; 235 int rc; 236 237 smc = smc_sk(sk); 238 239 /* replicate tests from inet_bind(), to be safe wrt. future changes */ 240 rc = -EINVAL; 241 if (addr_len < sizeof(struct sockaddr_in)) 242 goto out; 243 244 rc = -EAFNOSUPPORT; 245 if (addr->sin_family != AF_INET && 246 addr->sin_family != AF_INET6 && 247 addr->sin_family != AF_UNSPEC) 248 goto out; 249 /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */ 250 if (addr->sin_family == AF_UNSPEC && 251 addr->sin_addr.s_addr != htonl(INADDR_ANY)) 252 goto out; 253 254 lock_sock(sk); 255 256 /* Check if socket is already active */ 257 rc = -EINVAL; 258 if (sk->sk_state != SMC_INIT) 259 goto out_rel; 260 261 smc->clcsock->sk->sk_reuse = sk->sk_reuse; 262 rc = kernel_bind(smc->clcsock, uaddr, addr_len); 263 264 out_rel: 265 release_sock(sk); 266 out: 267 return rc; 268 } 269 270 static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk, 271 unsigned long mask) 272 { 273 /* options we don't get control via setsockopt for */ 274 nsk->sk_type = osk->sk_type; 275 nsk->sk_sndbuf = osk->sk_sndbuf; 276 nsk->sk_rcvbuf = osk->sk_rcvbuf; 277 nsk->sk_sndtimeo = osk->sk_sndtimeo; 278 nsk->sk_rcvtimeo = osk->sk_rcvtimeo; 279 nsk->sk_mark = osk->sk_mark; 280 nsk->sk_priority = osk->sk_priority; 281 nsk->sk_rcvlowat = osk->sk_rcvlowat; 282 nsk->sk_bound_dev_if = osk->sk_bound_dev_if; 283 nsk->sk_err = osk->sk_err; 284 285 nsk->sk_flags &= ~mask; 286 nsk->sk_flags |= osk->sk_flags & mask; 287 } 288 289 #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \ 290 (1UL << SOCK_KEEPOPEN) | \ 291 (1UL << SOCK_LINGER) | \ 292 (1UL << SOCK_BROADCAST) | \ 293 (1UL << SOCK_TIMESTAMP) | \ 294 (1UL << SOCK_DBG) | \ 295 (1UL << SOCK_RCVTSTAMP) | \ 296 (1UL << SOCK_RCVTSTAMPNS) | \ 297 (1UL << SOCK_LOCALROUTE) | \ 298 (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \ 299 (1UL << SOCK_RXQ_OVFL) | \ 300 (1UL << SOCK_WIFI_STATUS) | \ 301 (1UL << SOCK_NOFCS) | \ 302 (1UL << SOCK_FILTER_LOCKED) | \ 303 (1UL << SOCK_TSTAMP_NEW)) 304 /* copy only relevant settings and flags of SOL_SOCKET level from smc to 305 * clc socket (since smc is not called for these options from net/core) 306 */ 307 static void smc_copy_sock_settings_to_clc(struct smc_sock *smc) 308 { 309 smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC); 310 } 311 312 #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \ 313 (1UL << SOCK_KEEPOPEN) | \ 314 (1UL << SOCK_LINGER) | \ 315 (1UL << SOCK_DBG)) 316 /* copy only settings and flags relevant for smc from clc to smc socket */ 317 static void smc_copy_sock_settings_to_smc(struct smc_sock *smc) 318 { 319 smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC); 320 } 321 322 /* register a new rmb, send confirm_rkey msg to register with peer */ 323 static int smc_reg_rmb(struct smc_link *link, struct smc_buf_desc *rmb_desc, 324 bool conf_rkey) 325 { 326 if (!rmb_desc->wr_reg) { 327 /* register memory region for new rmb */ 328 if (smc_wr_reg_send(link, rmb_desc->mr_rx[SMC_SINGLE_LINK])) { 329 rmb_desc->regerr = 1; 330 return -EFAULT; 331 } 332 rmb_desc->wr_reg = 1; 333 } 334 if (!conf_rkey) 335 return 0; 336 /* exchange confirm_rkey msg with peer */ 337 if (smc_llc_do_confirm_rkey(link, rmb_desc)) { 338 rmb_desc->regerr = 1; 339 return -EFAULT; 340 } 341 return 0; 342 } 343 344 static int smc_clnt_conf_first_link(struct smc_sock *smc) 345 { 346 struct net *net = sock_net(smc->clcsock->sk); 347 struct smc_link_group *lgr = smc->conn.lgr; 348 struct smc_link *link; 349 int rest; 350 int rc; 351 352 link = &lgr->lnk[SMC_SINGLE_LINK]; 353 /* receive CONFIRM LINK request from server over RoCE fabric */ 354 rest = wait_for_completion_interruptible_timeout( 355 &link->llc_confirm, 356 SMC_LLC_WAIT_FIRST_TIME); 357 if (rest <= 0) { 358 struct smc_clc_msg_decline dclc; 359 360 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc), 361 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT); 362 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc; 363 } 364 365 if (link->llc_confirm_rc) 366 return SMC_CLC_DECL_RMBE_EC; 367 368 rc = smc_ib_modify_qp_rts(link); 369 if (rc) 370 return SMC_CLC_DECL_ERR_RDYLNK; 371 372 smc_wr_remember_qp_attr(link); 373 374 if (smc_reg_rmb(link, smc->conn.rmb_desc, false)) 375 return SMC_CLC_DECL_ERR_REGRMB; 376 377 /* send CONFIRM LINK response over RoCE fabric */ 378 rc = smc_llc_send_confirm_link(link, SMC_LLC_RESP); 379 if (rc < 0) 380 return SMC_CLC_DECL_TIMEOUT_CL; 381 382 /* receive ADD LINK request from server over RoCE fabric */ 383 rest = wait_for_completion_interruptible_timeout(&link->llc_add, 384 SMC_LLC_WAIT_TIME); 385 if (rest <= 0) { 386 struct smc_clc_msg_decline dclc; 387 388 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc), 389 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT); 390 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc; 391 } 392 393 /* send add link reject message, only one link supported for now */ 394 rc = smc_llc_send_add_link(link, 395 link->smcibdev->mac[link->ibport - 1], 396 link->gid, SMC_LLC_RESP); 397 if (rc < 0) 398 return SMC_CLC_DECL_TIMEOUT_AL; 399 400 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time); 401 402 return 0; 403 } 404 405 static void smcr_conn_save_peer_info(struct smc_sock *smc, 406 struct smc_clc_msg_accept_confirm *clc) 407 { 408 int bufsize = smc_uncompress_bufsize(clc->rmbe_size); 409 410 smc->conn.peer_rmbe_idx = clc->rmbe_idx; 411 smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token); 412 smc->conn.peer_rmbe_size = bufsize; 413 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size); 414 smc->conn.tx_off = bufsize * (smc->conn.peer_rmbe_idx - 1); 415 } 416 417 static void smcd_conn_save_peer_info(struct smc_sock *smc, 418 struct smc_clc_msg_accept_confirm *clc) 419 { 420 int bufsize = smc_uncompress_bufsize(clc->dmbe_size); 421 422 smc->conn.peer_rmbe_idx = clc->dmbe_idx; 423 smc->conn.peer_token = clc->token; 424 /* msg header takes up space in the buffer */ 425 smc->conn.peer_rmbe_size = bufsize - sizeof(struct smcd_cdc_msg); 426 atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size); 427 smc->conn.tx_off = bufsize * smc->conn.peer_rmbe_idx; 428 } 429 430 static void smc_conn_save_peer_info(struct smc_sock *smc, 431 struct smc_clc_msg_accept_confirm *clc) 432 { 433 if (smc->conn.lgr->is_smcd) 434 smcd_conn_save_peer_info(smc, clc); 435 else 436 smcr_conn_save_peer_info(smc, clc); 437 } 438 439 static void smc_link_save_peer_info(struct smc_link *link, 440 struct smc_clc_msg_accept_confirm *clc) 441 { 442 link->peer_qpn = ntoh24(clc->qpn); 443 memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE); 444 memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac)); 445 link->peer_psn = ntoh24(clc->psn); 446 link->peer_mtu = clc->qp_mtu; 447 } 448 449 /* fall back during connect */ 450 static int smc_connect_fallback(struct smc_sock *smc, int reason_code) 451 { 452 smc->use_fallback = true; 453 smc->fallback_rsn = reason_code; 454 smc_copy_sock_settings_to_clc(smc); 455 if (smc->sk.sk_state == SMC_INIT) 456 smc->sk.sk_state = SMC_ACTIVE; 457 return 0; 458 } 459 460 /* decline and fall back during connect */ 461 static int smc_connect_decline_fallback(struct smc_sock *smc, int reason_code) 462 { 463 int rc; 464 465 if (reason_code < 0) { /* error, fallback is not possible */ 466 if (smc->sk.sk_state == SMC_INIT) 467 sock_put(&smc->sk); /* passive closing */ 468 return reason_code; 469 } 470 if (reason_code != SMC_CLC_DECL_PEERDECL) { 471 rc = smc_clc_send_decline(smc, reason_code); 472 if (rc < 0) { 473 if (smc->sk.sk_state == SMC_INIT) 474 sock_put(&smc->sk); /* passive closing */ 475 return rc; 476 } 477 } 478 return smc_connect_fallback(smc, reason_code); 479 } 480 481 /* abort connecting */ 482 static int smc_connect_abort(struct smc_sock *smc, int reason_code, 483 int local_contact) 484 { 485 if (local_contact == SMC_FIRST_CONTACT) 486 smc_lgr_forget(smc->conn.lgr); 487 if (smc->conn.lgr->is_smcd) 488 /* there is only one lgr role for SMC-D; use server lock */ 489 mutex_unlock(&smc_server_lgr_pending); 490 else 491 mutex_unlock(&smc_client_lgr_pending); 492 493 smc_conn_free(&smc->conn); 494 return reason_code; 495 } 496 497 /* check if there is a rdma device available for this connection. */ 498 /* called for connect and listen */ 499 static int smc_check_rdma(struct smc_sock *smc, struct smc_ib_device **ibdev, 500 u8 *ibport, unsigned short vlan_id, u8 gid[]) 501 { 502 int reason_code = 0; 503 504 /* PNET table look up: search active ib_device and port 505 * within same PNETID that also contains the ethernet device 506 * used for the internal TCP socket 507 */ 508 smc_pnet_find_roce_resource(smc->clcsock->sk, ibdev, ibport, vlan_id, 509 gid); 510 if (!(*ibdev)) 511 reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */ 512 513 return reason_code; 514 } 515 516 /* check if there is an ISM device available for this connection. */ 517 /* called for connect and listen */ 518 static int smc_check_ism(struct smc_sock *smc, struct smcd_dev **ismdev) 519 { 520 /* Find ISM device with same PNETID as connecting interface */ 521 smc_pnet_find_ism_resource(smc->clcsock->sk, ismdev); 522 if (!(*ismdev)) 523 return SMC_CLC_DECL_CNFERR; /* configuration error */ 524 return 0; 525 } 526 527 /* Check for VLAN ID and register it on ISM device just for CLC handshake */ 528 static int smc_connect_ism_vlan_setup(struct smc_sock *smc, 529 struct smcd_dev *ismdev, 530 unsigned short vlan_id) 531 { 532 if (vlan_id && smc_ism_get_vlan(ismdev, vlan_id)) 533 return SMC_CLC_DECL_CNFERR; 534 return 0; 535 } 536 537 /* cleanup temporary VLAN ID registration used for CLC handshake. If ISM is 538 * used, the VLAN ID will be registered again during the connection setup. 539 */ 540 static int smc_connect_ism_vlan_cleanup(struct smc_sock *smc, bool is_smcd, 541 struct smcd_dev *ismdev, 542 unsigned short vlan_id) 543 { 544 if (!is_smcd) 545 return 0; 546 if (vlan_id && smc_ism_put_vlan(ismdev, vlan_id)) 547 return SMC_CLC_DECL_CNFERR; 548 return 0; 549 } 550 551 /* CLC handshake during connect */ 552 static int smc_connect_clc(struct smc_sock *smc, int smc_type, 553 struct smc_clc_msg_accept_confirm *aclc, 554 struct smc_ib_device *ibdev, u8 ibport, 555 u8 gid[], struct smcd_dev *ismdev) 556 { 557 int rc = 0; 558 559 /* do inband token exchange */ 560 rc = smc_clc_send_proposal(smc, smc_type, ibdev, ibport, gid, ismdev); 561 if (rc) 562 return rc; 563 /* receive SMC Accept CLC message */ 564 return smc_clc_wait_msg(smc, aclc, sizeof(*aclc), SMC_CLC_ACCEPT, 565 CLC_WAIT_TIME); 566 } 567 568 /* setup for RDMA connection of client */ 569 static int smc_connect_rdma(struct smc_sock *smc, 570 struct smc_clc_msg_accept_confirm *aclc, 571 struct smc_ib_device *ibdev, u8 ibport) 572 { 573 int local_contact = SMC_FIRST_CONTACT; 574 struct smc_link *link; 575 int reason_code = 0; 576 577 mutex_lock(&smc_client_lgr_pending); 578 local_contact = smc_conn_create(smc, false, aclc->hdr.flag, ibdev, 579 ibport, ntoh24(aclc->qpn), &aclc->lcl, 580 NULL, 0); 581 if (local_contact < 0) { 582 if (local_contact == -ENOMEM) 583 reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/ 584 else if (local_contact == -ENOLINK) 585 reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */ 586 else 587 reason_code = SMC_CLC_DECL_INTERR; /* other error */ 588 mutex_unlock(&smc_client_lgr_pending); 589 return reason_code; 590 } 591 link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK]; 592 593 smc_conn_save_peer_info(smc, aclc); 594 595 /* create send buffer and rmb */ 596 if (smc_buf_create(smc, false)) 597 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, local_contact); 598 599 if (local_contact == SMC_FIRST_CONTACT) 600 smc_link_save_peer_info(link, aclc); 601 602 if (smc_rmb_rtoken_handling(&smc->conn, aclc)) 603 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RTOK, 604 local_contact); 605 606 smc_close_init(smc); 607 smc_rx_init(smc); 608 609 if (local_contact == SMC_FIRST_CONTACT) { 610 if (smc_ib_ready_link(link)) 611 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_RDYLNK, 612 local_contact); 613 } else { 614 if (smc_reg_rmb(link, smc->conn.rmb_desc, true)) 615 return smc_connect_abort(smc, SMC_CLC_DECL_ERR_REGRMB, 616 local_contact); 617 } 618 smc_rmb_sync_sg_for_device(&smc->conn); 619 620 reason_code = smc_clc_send_confirm(smc); 621 if (reason_code) 622 return smc_connect_abort(smc, reason_code, local_contact); 623 624 smc_tx_init(smc); 625 626 if (local_contact == SMC_FIRST_CONTACT) { 627 /* QP confirmation over RoCE fabric */ 628 reason_code = smc_clnt_conf_first_link(smc); 629 if (reason_code) 630 return smc_connect_abort(smc, reason_code, 631 local_contact); 632 } 633 mutex_unlock(&smc_client_lgr_pending); 634 635 smc_copy_sock_settings_to_clc(smc); 636 if (smc->sk.sk_state == SMC_INIT) 637 smc->sk.sk_state = SMC_ACTIVE; 638 639 return 0; 640 } 641 642 /* setup for ISM connection of client */ 643 static int smc_connect_ism(struct smc_sock *smc, 644 struct smc_clc_msg_accept_confirm *aclc, 645 struct smcd_dev *ismdev) 646 { 647 int local_contact = SMC_FIRST_CONTACT; 648 int rc = 0; 649 650 /* there is only one lgr role for SMC-D; use server lock */ 651 mutex_lock(&smc_server_lgr_pending); 652 local_contact = smc_conn_create(smc, true, aclc->hdr.flag, NULL, 0, 0, 653 NULL, ismdev, aclc->gid); 654 if (local_contact < 0) { 655 mutex_unlock(&smc_server_lgr_pending); 656 return SMC_CLC_DECL_MEM; 657 } 658 659 /* Create send and receive buffers */ 660 if (smc_buf_create(smc, true)) 661 return smc_connect_abort(smc, SMC_CLC_DECL_MEM, local_contact); 662 663 smc_conn_save_peer_info(smc, aclc); 664 smc_close_init(smc); 665 smc_rx_init(smc); 666 smc_tx_init(smc); 667 668 rc = smc_clc_send_confirm(smc); 669 if (rc) 670 return smc_connect_abort(smc, rc, local_contact); 671 mutex_unlock(&smc_server_lgr_pending); 672 673 smc_copy_sock_settings_to_clc(smc); 674 if (smc->sk.sk_state == SMC_INIT) 675 smc->sk.sk_state = SMC_ACTIVE; 676 677 return 0; 678 } 679 680 /* perform steps before actually connecting */ 681 static int __smc_connect(struct smc_sock *smc) 682 { 683 bool ism_supported = false, rdma_supported = false; 684 struct smc_clc_msg_accept_confirm aclc; 685 struct smc_ib_device *ibdev; 686 struct smcd_dev *ismdev; 687 u8 gid[SMC_GID_SIZE]; 688 unsigned short vlan; 689 int smc_type; 690 int rc = 0; 691 u8 ibport; 692 693 sock_hold(&smc->sk); /* sock put in passive closing */ 694 695 if (smc->use_fallback) 696 return smc_connect_fallback(smc, smc->fallback_rsn); 697 698 /* if peer has not signalled SMC-capability, fall back */ 699 if (!tcp_sk(smc->clcsock->sk)->syn_smc) 700 return smc_connect_fallback(smc, SMC_CLC_DECL_PEERNOSMC); 701 702 /* IPSec connections opt out of SMC-R optimizations */ 703 if (using_ipsec(smc)) 704 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_IPSEC); 705 706 /* check for VLAN ID */ 707 if (smc_vlan_by_tcpsk(smc->clcsock, &vlan)) 708 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_CNFERR); 709 710 /* check if there is an ism device available */ 711 if (!smc_check_ism(smc, &ismdev) && 712 !smc_connect_ism_vlan_setup(smc, ismdev, vlan)) { 713 /* ISM is supported for this connection */ 714 ism_supported = true; 715 smc_type = SMC_TYPE_D; 716 } 717 718 /* check if there is a rdma device available */ 719 if (!smc_check_rdma(smc, &ibdev, &ibport, vlan, gid)) { 720 /* RDMA is supported for this connection */ 721 rdma_supported = true; 722 if (ism_supported) 723 smc_type = SMC_TYPE_B; /* both */ 724 else 725 smc_type = SMC_TYPE_R; /* only RDMA */ 726 } 727 728 /* if neither ISM nor RDMA are supported, fallback */ 729 if (!rdma_supported && !ism_supported) 730 return smc_connect_decline_fallback(smc, SMC_CLC_DECL_NOSMCDEV); 731 732 /* perform CLC handshake */ 733 rc = smc_connect_clc(smc, smc_type, &aclc, ibdev, ibport, gid, ismdev); 734 if (rc) { 735 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan); 736 return smc_connect_decline_fallback(smc, rc); 737 } 738 739 /* depending on previous steps, connect using rdma or ism */ 740 if (rdma_supported && aclc.hdr.path == SMC_TYPE_R) 741 rc = smc_connect_rdma(smc, &aclc, ibdev, ibport); 742 else if (ism_supported && aclc.hdr.path == SMC_TYPE_D) 743 rc = smc_connect_ism(smc, &aclc, ismdev); 744 else 745 rc = SMC_CLC_DECL_MODEUNSUPP; 746 if (rc) { 747 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan); 748 return smc_connect_decline_fallback(smc, rc); 749 } 750 751 smc_connect_ism_vlan_cleanup(smc, ism_supported, ismdev, vlan); 752 return 0; 753 } 754 755 static void smc_connect_work(struct work_struct *work) 756 { 757 struct smc_sock *smc = container_of(work, struct smc_sock, 758 connect_work); 759 int rc; 760 761 lock_sock(&smc->sk); 762 rc = kernel_connect(smc->clcsock, &smc->connect_info->addr, 763 smc->connect_info->alen, smc->connect_info->flags); 764 if (smc->clcsock->sk->sk_err) { 765 smc->sk.sk_err = smc->clcsock->sk->sk_err; 766 goto out; 767 } 768 if (rc < 0) { 769 smc->sk.sk_err = -rc; 770 goto out; 771 } 772 773 rc = __smc_connect(smc); 774 if (rc < 0) 775 smc->sk.sk_err = -rc; 776 777 out: 778 if (smc->sk.sk_err) 779 smc->sk.sk_state_change(&smc->sk); 780 else 781 smc->sk.sk_write_space(&smc->sk); 782 kfree(smc->connect_info); 783 smc->connect_info = NULL; 784 release_sock(&smc->sk); 785 } 786 787 static int smc_connect(struct socket *sock, struct sockaddr *addr, 788 int alen, int flags) 789 { 790 struct sock *sk = sock->sk; 791 struct smc_sock *smc; 792 int rc = -EINVAL; 793 794 smc = smc_sk(sk); 795 796 /* separate smc parameter checking to be safe */ 797 if (alen < sizeof(addr->sa_family)) 798 goto out_err; 799 if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6) 800 goto out_err; 801 802 lock_sock(sk); 803 switch (sk->sk_state) { 804 default: 805 goto out; 806 case SMC_ACTIVE: 807 rc = -EISCONN; 808 goto out; 809 case SMC_INIT: 810 rc = 0; 811 break; 812 } 813 814 smc_copy_sock_settings_to_clc(smc); 815 tcp_sk(smc->clcsock->sk)->syn_smc = 1; 816 if (flags & O_NONBLOCK) { 817 if (smc->connect_info) { 818 rc = -EALREADY; 819 goto out; 820 } 821 smc->connect_info = kzalloc(alen + 2 * sizeof(int), GFP_KERNEL); 822 if (!smc->connect_info) { 823 rc = -ENOMEM; 824 goto out; 825 } 826 smc->connect_info->alen = alen; 827 smc->connect_info->flags = flags ^ O_NONBLOCK; 828 memcpy(&smc->connect_info->addr, addr, alen); 829 schedule_work(&smc->connect_work); 830 rc = -EINPROGRESS; 831 } else { 832 rc = kernel_connect(smc->clcsock, addr, alen, flags); 833 if (rc) 834 goto out; 835 836 rc = __smc_connect(smc); 837 if (rc < 0) 838 goto out; 839 else 840 rc = 0; /* success cases including fallback */ 841 } 842 843 out: 844 release_sock(sk); 845 out_err: 846 return rc; 847 } 848 849 static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc) 850 { 851 struct socket *new_clcsock = NULL; 852 struct sock *lsk = &lsmc->sk; 853 struct sock *new_sk; 854 int rc = -EINVAL; 855 856 release_sock(lsk); 857 new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol); 858 if (!new_sk) { 859 rc = -ENOMEM; 860 lsk->sk_err = ENOMEM; 861 *new_smc = NULL; 862 lock_sock(lsk); 863 goto out; 864 } 865 *new_smc = smc_sk(new_sk); 866 867 mutex_lock(&lsmc->clcsock_release_lock); 868 if (lsmc->clcsock) 869 rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0); 870 mutex_unlock(&lsmc->clcsock_release_lock); 871 lock_sock(lsk); 872 if (rc < 0) 873 lsk->sk_err = -rc; 874 if (rc < 0 || lsk->sk_state == SMC_CLOSED) { 875 if (new_clcsock) 876 sock_release(new_clcsock); 877 new_sk->sk_state = SMC_CLOSED; 878 sock_set_flag(new_sk, SOCK_DEAD); 879 new_sk->sk_prot->unhash(new_sk); 880 sock_put(new_sk); /* final */ 881 *new_smc = NULL; 882 goto out; 883 } 884 885 (*new_smc)->clcsock = new_clcsock; 886 out: 887 return rc; 888 } 889 890 /* add a just created sock to the accept queue of the listen sock as 891 * candidate for a following socket accept call from user space 892 */ 893 static void smc_accept_enqueue(struct sock *parent, struct sock *sk) 894 { 895 struct smc_sock *par = smc_sk(parent); 896 897 sock_hold(sk); /* sock_put in smc_accept_unlink () */ 898 spin_lock(&par->accept_q_lock); 899 list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q); 900 spin_unlock(&par->accept_q_lock); 901 sk_acceptq_added(parent); 902 } 903 904 /* remove a socket from the accept queue of its parental listening socket */ 905 static void smc_accept_unlink(struct sock *sk) 906 { 907 struct smc_sock *par = smc_sk(sk)->listen_smc; 908 909 spin_lock(&par->accept_q_lock); 910 list_del_init(&smc_sk(sk)->accept_q); 911 spin_unlock(&par->accept_q_lock); 912 sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk); 913 sock_put(sk); /* sock_hold in smc_accept_enqueue */ 914 } 915 916 /* remove a sock from the accept queue to bind it to a new socket created 917 * for a socket accept call from user space 918 */ 919 struct sock *smc_accept_dequeue(struct sock *parent, 920 struct socket *new_sock) 921 { 922 struct smc_sock *isk, *n; 923 struct sock *new_sk; 924 925 list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) { 926 new_sk = (struct sock *)isk; 927 928 smc_accept_unlink(new_sk); 929 if (new_sk->sk_state == SMC_CLOSED) { 930 if (isk->clcsock) { 931 sock_release(isk->clcsock); 932 isk->clcsock = NULL; 933 } 934 new_sk->sk_prot->unhash(new_sk); 935 sock_put(new_sk); /* final */ 936 continue; 937 } 938 if (new_sock) 939 sock_graft(new_sk, new_sock); 940 return new_sk; 941 } 942 return NULL; 943 } 944 945 /* clean up for a created but never accepted sock */ 946 void smc_close_non_accepted(struct sock *sk) 947 { 948 struct smc_sock *smc = smc_sk(sk); 949 950 lock_sock(sk); 951 if (!sk->sk_lingertime) 952 /* wait for peer closing */ 953 sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT; 954 if (!smc->use_fallback) { 955 smc_close_active(smc); 956 sock_set_flag(sk, SOCK_DEAD); 957 sk->sk_shutdown |= SHUTDOWN_MASK; 958 } 959 if (smc->clcsock) { 960 struct socket *tcp; 961 962 tcp = smc->clcsock; 963 smc->clcsock = NULL; 964 sock_release(tcp); 965 } 966 if (smc->use_fallback) { 967 sock_put(sk); /* passive closing */ 968 sk->sk_state = SMC_CLOSED; 969 } else { 970 if (sk->sk_state == SMC_CLOSED) 971 smc_conn_free(&smc->conn); 972 } 973 release_sock(sk); 974 sk->sk_prot->unhash(sk); 975 sock_put(sk); /* final sock_put */ 976 } 977 978 static int smc_serv_conf_first_link(struct smc_sock *smc) 979 { 980 struct net *net = sock_net(smc->clcsock->sk); 981 struct smc_link_group *lgr = smc->conn.lgr; 982 struct smc_link *link; 983 int rest; 984 int rc; 985 986 link = &lgr->lnk[SMC_SINGLE_LINK]; 987 988 if (smc_reg_rmb(link, smc->conn.rmb_desc, false)) 989 return SMC_CLC_DECL_ERR_REGRMB; 990 991 /* send CONFIRM LINK request to client over the RoCE fabric */ 992 rc = smc_llc_send_confirm_link(link, SMC_LLC_REQ); 993 if (rc < 0) 994 return SMC_CLC_DECL_TIMEOUT_CL; 995 996 /* receive CONFIRM LINK response from client over the RoCE fabric */ 997 rest = wait_for_completion_interruptible_timeout( 998 &link->llc_confirm_resp, 999 SMC_LLC_WAIT_FIRST_TIME); 1000 if (rest <= 0) { 1001 struct smc_clc_msg_decline dclc; 1002 1003 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc), 1004 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT); 1005 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_CL : rc; 1006 } 1007 1008 if (link->llc_confirm_resp_rc) 1009 return SMC_CLC_DECL_RMBE_EC; 1010 1011 /* send ADD LINK request to client over the RoCE fabric */ 1012 rc = smc_llc_send_add_link(link, 1013 link->smcibdev->mac[link->ibport - 1], 1014 link->gid, SMC_LLC_REQ); 1015 if (rc < 0) 1016 return SMC_CLC_DECL_TIMEOUT_AL; 1017 1018 /* receive ADD LINK response from client over the RoCE fabric */ 1019 rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp, 1020 SMC_LLC_WAIT_TIME); 1021 if (rest <= 0) { 1022 struct smc_clc_msg_decline dclc; 1023 1024 rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc), 1025 SMC_CLC_DECLINE, CLC_WAIT_TIME_SHORT); 1026 return rc == -EAGAIN ? SMC_CLC_DECL_TIMEOUT_AL : rc; 1027 } 1028 1029 smc_llc_link_active(link, net->ipv4.sysctl_tcp_keepalive_time); 1030 1031 return 0; 1032 } 1033 1034 /* listen worker: finish */ 1035 static void smc_listen_out(struct smc_sock *new_smc) 1036 { 1037 struct smc_sock *lsmc = new_smc->listen_smc; 1038 struct sock *newsmcsk = &new_smc->sk; 1039 1040 lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING); 1041 if (lsmc->sk.sk_state == SMC_LISTEN) { 1042 smc_accept_enqueue(&lsmc->sk, newsmcsk); 1043 } else { /* no longer listening */ 1044 smc_close_non_accepted(newsmcsk); 1045 } 1046 release_sock(&lsmc->sk); 1047 1048 /* Wake up accept */ 1049 lsmc->sk.sk_data_ready(&lsmc->sk); 1050 sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */ 1051 } 1052 1053 /* listen worker: finish in state connected */ 1054 static void smc_listen_out_connected(struct smc_sock *new_smc) 1055 { 1056 struct sock *newsmcsk = &new_smc->sk; 1057 1058 sk_refcnt_debug_inc(newsmcsk); 1059 if (newsmcsk->sk_state == SMC_INIT) 1060 newsmcsk->sk_state = SMC_ACTIVE; 1061 1062 smc_listen_out(new_smc); 1063 } 1064 1065 /* listen worker: finish in error state */ 1066 static void smc_listen_out_err(struct smc_sock *new_smc) 1067 { 1068 struct sock *newsmcsk = &new_smc->sk; 1069 1070 if (newsmcsk->sk_state == SMC_INIT) 1071 sock_put(&new_smc->sk); /* passive closing */ 1072 newsmcsk->sk_state = SMC_CLOSED; 1073 smc_conn_free(&new_smc->conn); 1074 1075 smc_listen_out(new_smc); 1076 } 1077 1078 /* listen worker: decline and fall back if possible */ 1079 static void smc_listen_decline(struct smc_sock *new_smc, int reason_code, 1080 int local_contact) 1081 { 1082 /* RDMA setup failed, switch back to TCP */ 1083 if (local_contact == SMC_FIRST_CONTACT) 1084 smc_lgr_forget(new_smc->conn.lgr); 1085 if (reason_code < 0) { /* error, no fallback possible */ 1086 smc_listen_out_err(new_smc); 1087 return; 1088 } 1089 smc_conn_free(&new_smc->conn); 1090 new_smc->use_fallback = true; 1091 new_smc->fallback_rsn = reason_code; 1092 if (reason_code && reason_code != SMC_CLC_DECL_PEERDECL) { 1093 if (smc_clc_send_decline(new_smc, reason_code) < 0) { 1094 smc_listen_out_err(new_smc); 1095 return; 1096 } 1097 } 1098 smc_listen_out_connected(new_smc); 1099 } 1100 1101 /* listen worker: check prefixes */ 1102 static int smc_listen_rdma_check(struct smc_sock *new_smc, 1103 struct smc_clc_msg_proposal *pclc) 1104 { 1105 struct smc_clc_msg_proposal_prefix *pclc_prfx; 1106 struct socket *newclcsock = new_smc->clcsock; 1107 1108 pclc_prfx = smc_clc_proposal_get_prefix(pclc); 1109 if (smc_clc_prfx_match(newclcsock, pclc_prfx)) 1110 return SMC_CLC_DECL_CNFERR; 1111 1112 return 0; 1113 } 1114 1115 /* listen worker: initialize connection and buffers */ 1116 static int smc_listen_rdma_init(struct smc_sock *new_smc, 1117 struct smc_clc_msg_proposal *pclc, 1118 struct smc_ib_device *ibdev, u8 ibport, 1119 int *local_contact) 1120 { 1121 /* allocate connection / link group */ 1122 *local_contact = smc_conn_create(new_smc, false, 0, ibdev, ibport, 0, 1123 &pclc->lcl, NULL, 0); 1124 if (*local_contact < 0) { 1125 if (*local_contact == -ENOMEM) 1126 return SMC_CLC_DECL_MEM;/* insufficient memory*/ 1127 return SMC_CLC_DECL_INTERR; /* other error */ 1128 } 1129 1130 /* create send buffer and rmb */ 1131 if (smc_buf_create(new_smc, false)) 1132 return SMC_CLC_DECL_MEM; 1133 1134 return 0; 1135 } 1136 1137 /* listen worker: initialize connection and buffers for SMC-D */ 1138 static int smc_listen_ism_init(struct smc_sock *new_smc, 1139 struct smc_clc_msg_proposal *pclc, 1140 struct smcd_dev *ismdev, 1141 int *local_contact) 1142 { 1143 struct smc_clc_msg_smcd *pclc_smcd; 1144 1145 pclc_smcd = smc_get_clc_msg_smcd(pclc); 1146 *local_contact = smc_conn_create(new_smc, true, 0, NULL, 0, 0, NULL, 1147 ismdev, pclc_smcd->gid); 1148 if (*local_contact < 0) { 1149 if (*local_contact == -ENOMEM) 1150 return SMC_CLC_DECL_MEM;/* insufficient memory*/ 1151 return SMC_CLC_DECL_INTERR; /* other error */ 1152 } 1153 1154 /* Check if peer can be reached via ISM device */ 1155 if (smc_ism_cantalk(new_smc->conn.lgr->peer_gid, 1156 new_smc->conn.lgr->vlan_id, 1157 new_smc->conn.lgr->smcd)) { 1158 if (*local_contact == SMC_FIRST_CONTACT) 1159 smc_lgr_forget(new_smc->conn.lgr); 1160 smc_conn_free(&new_smc->conn); 1161 return SMC_CLC_DECL_CNFERR; 1162 } 1163 1164 /* Create send and receive buffers */ 1165 if (smc_buf_create(new_smc, true)) { 1166 if (*local_contact == SMC_FIRST_CONTACT) 1167 smc_lgr_forget(new_smc->conn.lgr); 1168 smc_conn_free(&new_smc->conn); 1169 return SMC_CLC_DECL_MEM; 1170 } 1171 1172 return 0; 1173 } 1174 1175 /* listen worker: register buffers */ 1176 static int smc_listen_rdma_reg(struct smc_sock *new_smc, int local_contact) 1177 { 1178 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK]; 1179 1180 if (local_contact != SMC_FIRST_CONTACT) { 1181 if (smc_reg_rmb(link, new_smc->conn.rmb_desc, true)) 1182 return SMC_CLC_DECL_ERR_REGRMB; 1183 } 1184 smc_rmb_sync_sg_for_device(&new_smc->conn); 1185 1186 return 0; 1187 } 1188 1189 /* listen worker: finish RDMA setup */ 1190 static int smc_listen_rdma_finish(struct smc_sock *new_smc, 1191 struct smc_clc_msg_accept_confirm *cclc, 1192 int local_contact) 1193 { 1194 struct smc_link *link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK]; 1195 int reason_code = 0; 1196 1197 if (local_contact == SMC_FIRST_CONTACT) 1198 smc_link_save_peer_info(link, cclc); 1199 1200 if (smc_rmb_rtoken_handling(&new_smc->conn, cclc)) { 1201 reason_code = SMC_CLC_DECL_ERR_RTOK; 1202 goto decline; 1203 } 1204 1205 if (local_contact == SMC_FIRST_CONTACT) { 1206 if (smc_ib_ready_link(link)) { 1207 reason_code = SMC_CLC_DECL_ERR_RDYLNK; 1208 goto decline; 1209 } 1210 /* QP confirmation over RoCE fabric */ 1211 reason_code = smc_serv_conf_first_link(new_smc); 1212 if (reason_code) 1213 goto decline; 1214 } 1215 return 0; 1216 1217 decline: 1218 smc_listen_decline(new_smc, reason_code, local_contact); 1219 return reason_code; 1220 } 1221 1222 /* setup for RDMA connection of server */ 1223 static void smc_listen_work(struct work_struct *work) 1224 { 1225 struct smc_sock *new_smc = container_of(work, struct smc_sock, 1226 smc_listen_work); 1227 struct socket *newclcsock = new_smc->clcsock; 1228 struct smc_clc_msg_accept_confirm cclc; 1229 struct smc_clc_msg_proposal *pclc; 1230 struct smc_ib_device *ibdev; 1231 bool ism_supported = false; 1232 struct smcd_dev *ismdev; 1233 u8 buf[SMC_CLC_MAX_LEN]; 1234 int local_contact = 0; 1235 unsigned short vlan; 1236 int reason_code = 0; 1237 int rc = 0; 1238 u8 ibport; 1239 1240 if (new_smc->use_fallback) { 1241 smc_listen_out_connected(new_smc); 1242 return; 1243 } 1244 1245 /* check if peer is smc capable */ 1246 if (!tcp_sk(newclcsock->sk)->syn_smc) { 1247 new_smc->use_fallback = true; 1248 new_smc->fallback_rsn = SMC_CLC_DECL_PEERNOSMC; 1249 smc_listen_out_connected(new_smc); 1250 return; 1251 } 1252 1253 /* do inband token exchange - 1254 * wait for and receive SMC Proposal CLC message 1255 */ 1256 pclc = (struct smc_clc_msg_proposal *)&buf; 1257 reason_code = smc_clc_wait_msg(new_smc, pclc, SMC_CLC_MAX_LEN, 1258 SMC_CLC_PROPOSAL, CLC_WAIT_TIME); 1259 if (reason_code) { 1260 smc_listen_decline(new_smc, reason_code, 0); 1261 return; 1262 } 1263 1264 /* IPSec connections opt out of SMC-R optimizations */ 1265 if (using_ipsec(new_smc)) { 1266 smc_listen_decline(new_smc, SMC_CLC_DECL_IPSEC, 0); 1267 return; 1268 } 1269 1270 mutex_lock(&smc_server_lgr_pending); 1271 smc_close_init(new_smc); 1272 smc_rx_init(new_smc); 1273 smc_tx_init(new_smc); 1274 1275 /* check if ISM is available */ 1276 if ((pclc->hdr.path == SMC_TYPE_D || pclc->hdr.path == SMC_TYPE_B) && 1277 !smc_check_ism(new_smc, &ismdev) && 1278 !smc_listen_ism_init(new_smc, pclc, ismdev, &local_contact)) { 1279 ism_supported = true; 1280 } 1281 1282 /* check if RDMA is available */ 1283 if (!ism_supported && 1284 ((pclc->hdr.path != SMC_TYPE_R && pclc->hdr.path != SMC_TYPE_B) || 1285 smc_vlan_by_tcpsk(new_smc->clcsock, &vlan) || 1286 smc_check_rdma(new_smc, &ibdev, &ibport, vlan, NULL) || 1287 smc_listen_rdma_check(new_smc, pclc) || 1288 smc_listen_rdma_init(new_smc, pclc, ibdev, ibport, 1289 &local_contact) || 1290 smc_listen_rdma_reg(new_smc, local_contact))) { 1291 /* SMC not supported, decline */ 1292 mutex_unlock(&smc_server_lgr_pending); 1293 smc_listen_decline(new_smc, SMC_CLC_DECL_MODEUNSUPP, 1294 local_contact); 1295 return; 1296 } 1297 1298 /* send SMC Accept CLC message */ 1299 rc = smc_clc_send_accept(new_smc, local_contact); 1300 if (rc) { 1301 mutex_unlock(&smc_server_lgr_pending); 1302 smc_listen_decline(new_smc, rc, local_contact); 1303 return; 1304 } 1305 1306 /* SMC-D does not need this lock any more */ 1307 if (ism_supported) 1308 mutex_unlock(&smc_server_lgr_pending); 1309 1310 /* receive SMC Confirm CLC message */ 1311 reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc), 1312 SMC_CLC_CONFIRM, CLC_WAIT_TIME); 1313 if (reason_code) { 1314 if (!ism_supported) 1315 mutex_unlock(&smc_server_lgr_pending); 1316 smc_listen_decline(new_smc, reason_code, local_contact); 1317 return; 1318 } 1319 1320 /* finish worker */ 1321 if (!ism_supported) { 1322 rc = smc_listen_rdma_finish(new_smc, &cclc, local_contact); 1323 mutex_unlock(&smc_server_lgr_pending); 1324 if (rc) 1325 return; 1326 } 1327 smc_conn_save_peer_info(new_smc, &cclc); 1328 smc_listen_out_connected(new_smc); 1329 } 1330 1331 static void smc_tcp_listen_work(struct work_struct *work) 1332 { 1333 struct smc_sock *lsmc = container_of(work, struct smc_sock, 1334 tcp_listen_work); 1335 struct sock *lsk = &lsmc->sk; 1336 struct smc_sock *new_smc; 1337 int rc = 0; 1338 1339 lock_sock(lsk); 1340 while (lsk->sk_state == SMC_LISTEN) { 1341 rc = smc_clcsock_accept(lsmc, &new_smc); 1342 if (rc) 1343 goto out; 1344 if (!new_smc) 1345 continue; 1346 1347 new_smc->listen_smc = lsmc; 1348 new_smc->use_fallback = lsmc->use_fallback; 1349 new_smc->fallback_rsn = lsmc->fallback_rsn; 1350 sock_hold(lsk); /* sock_put in smc_listen_work */ 1351 INIT_WORK(&new_smc->smc_listen_work, smc_listen_work); 1352 smc_copy_sock_settings_to_smc(new_smc); 1353 new_smc->sk.sk_sndbuf = lsmc->sk.sk_sndbuf; 1354 new_smc->sk.sk_rcvbuf = lsmc->sk.sk_rcvbuf; 1355 sock_hold(&new_smc->sk); /* sock_put in passive closing */ 1356 if (!schedule_work(&new_smc->smc_listen_work)) 1357 sock_put(&new_smc->sk); 1358 } 1359 1360 out: 1361 release_sock(lsk); 1362 sock_put(&lsmc->sk); /* sock_hold in smc_listen */ 1363 } 1364 1365 static int smc_listen(struct socket *sock, int backlog) 1366 { 1367 struct sock *sk = sock->sk; 1368 struct smc_sock *smc; 1369 int rc; 1370 1371 smc = smc_sk(sk); 1372 lock_sock(sk); 1373 1374 rc = -EINVAL; 1375 if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN)) 1376 goto out; 1377 1378 rc = 0; 1379 if (sk->sk_state == SMC_LISTEN) { 1380 sk->sk_max_ack_backlog = backlog; 1381 goto out; 1382 } 1383 /* some socket options are handled in core, so we could not apply 1384 * them to the clc socket -- copy smc socket options to clc socket 1385 */ 1386 smc_copy_sock_settings_to_clc(smc); 1387 if (!smc->use_fallback) 1388 tcp_sk(smc->clcsock->sk)->syn_smc = 1; 1389 1390 rc = kernel_listen(smc->clcsock, backlog); 1391 if (rc) 1392 goto out; 1393 sk->sk_max_ack_backlog = backlog; 1394 sk->sk_ack_backlog = 0; 1395 sk->sk_state = SMC_LISTEN; 1396 sock_hold(sk); /* sock_hold in tcp_listen_worker */ 1397 if (!schedule_work(&smc->tcp_listen_work)) 1398 sock_put(sk); 1399 1400 out: 1401 release_sock(sk); 1402 return rc; 1403 } 1404 1405 static int smc_accept(struct socket *sock, struct socket *new_sock, 1406 int flags, bool kern) 1407 { 1408 struct sock *sk = sock->sk, *nsk; 1409 DECLARE_WAITQUEUE(wait, current); 1410 struct smc_sock *lsmc; 1411 long timeo; 1412 int rc = 0; 1413 1414 lsmc = smc_sk(sk); 1415 sock_hold(sk); /* sock_put below */ 1416 lock_sock(sk); 1417 1418 if (lsmc->sk.sk_state != SMC_LISTEN) { 1419 rc = -EINVAL; 1420 release_sock(sk); 1421 goto out; 1422 } 1423 1424 /* Wait for an incoming connection */ 1425 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); 1426 add_wait_queue_exclusive(sk_sleep(sk), &wait); 1427 while (!(nsk = smc_accept_dequeue(sk, new_sock))) { 1428 set_current_state(TASK_INTERRUPTIBLE); 1429 if (!timeo) { 1430 rc = -EAGAIN; 1431 break; 1432 } 1433 release_sock(sk); 1434 timeo = schedule_timeout(timeo); 1435 /* wakeup by sk_data_ready in smc_listen_work() */ 1436 sched_annotate_sleep(); 1437 lock_sock(sk); 1438 if (signal_pending(current)) { 1439 rc = sock_intr_errno(timeo); 1440 break; 1441 } 1442 } 1443 set_current_state(TASK_RUNNING); 1444 remove_wait_queue(sk_sleep(sk), &wait); 1445 1446 if (!rc) 1447 rc = sock_error(nsk); 1448 release_sock(sk); 1449 if (rc) 1450 goto out; 1451 1452 if (lsmc->sockopt_defer_accept && !(flags & O_NONBLOCK)) { 1453 /* wait till data arrives on the socket */ 1454 timeo = msecs_to_jiffies(lsmc->sockopt_defer_accept * 1455 MSEC_PER_SEC); 1456 if (smc_sk(nsk)->use_fallback) { 1457 struct sock *clcsk = smc_sk(nsk)->clcsock->sk; 1458 1459 lock_sock(clcsk); 1460 if (skb_queue_empty(&clcsk->sk_receive_queue)) 1461 sk_wait_data(clcsk, &timeo, NULL); 1462 release_sock(clcsk); 1463 } else if (!atomic_read(&smc_sk(nsk)->conn.bytes_to_rcv)) { 1464 lock_sock(nsk); 1465 smc_rx_wait(smc_sk(nsk), &timeo, smc_rx_data_available); 1466 release_sock(nsk); 1467 } 1468 } 1469 1470 out: 1471 sock_put(sk); /* sock_hold above */ 1472 return rc; 1473 } 1474 1475 static int smc_getname(struct socket *sock, struct sockaddr *addr, 1476 int peer) 1477 { 1478 struct smc_sock *smc; 1479 1480 if (peer && (sock->sk->sk_state != SMC_ACTIVE) && 1481 (sock->sk->sk_state != SMC_APPCLOSEWAIT1)) 1482 return -ENOTCONN; 1483 1484 smc = smc_sk(sock->sk); 1485 1486 return smc->clcsock->ops->getname(smc->clcsock, addr, peer); 1487 } 1488 1489 static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) 1490 { 1491 struct sock *sk = sock->sk; 1492 struct smc_sock *smc; 1493 int rc = -EPIPE; 1494 1495 smc = smc_sk(sk); 1496 lock_sock(sk); 1497 if ((sk->sk_state != SMC_ACTIVE) && 1498 (sk->sk_state != SMC_APPCLOSEWAIT1) && 1499 (sk->sk_state != SMC_INIT)) 1500 goto out; 1501 1502 if (msg->msg_flags & MSG_FASTOPEN) { 1503 if (sk->sk_state == SMC_INIT) { 1504 smc->use_fallback = true; 1505 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP; 1506 } else { 1507 rc = -EINVAL; 1508 goto out; 1509 } 1510 } 1511 1512 if (smc->use_fallback) 1513 rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len); 1514 else 1515 rc = smc_tx_sendmsg(smc, msg, len); 1516 out: 1517 release_sock(sk); 1518 return rc; 1519 } 1520 1521 static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, 1522 int flags) 1523 { 1524 struct sock *sk = sock->sk; 1525 struct smc_sock *smc; 1526 int rc = -ENOTCONN; 1527 1528 smc = smc_sk(sk); 1529 lock_sock(sk); 1530 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) { 1531 /* socket was connected before, no more data to read */ 1532 rc = 0; 1533 goto out; 1534 } 1535 if ((sk->sk_state == SMC_INIT) || 1536 (sk->sk_state == SMC_LISTEN) || 1537 (sk->sk_state == SMC_CLOSED)) 1538 goto out; 1539 1540 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) { 1541 rc = 0; 1542 goto out; 1543 } 1544 1545 if (smc->use_fallback) { 1546 rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags); 1547 } else { 1548 msg->msg_namelen = 0; 1549 rc = smc_rx_recvmsg(smc, msg, NULL, len, flags); 1550 } 1551 1552 out: 1553 release_sock(sk); 1554 return rc; 1555 } 1556 1557 static __poll_t smc_accept_poll(struct sock *parent) 1558 { 1559 struct smc_sock *isk = smc_sk(parent); 1560 __poll_t mask = 0; 1561 1562 spin_lock(&isk->accept_q_lock); 1563 if (!list_empty(&isk->accept_q)) 1564 mask = EPOLLIN | EPOLLRDNORM; 1565 spin_unlock(&isk->accept_q_lock); 1566 1567 return mask; 1568 } 1569 1570 static __poll_t smc_poll(struct file *file, struct socket *sock, 1571 poll_table *wait) 1572 { 1573 struct sock *sk = sock->sk; 1574 __poll_t mask = 0; 1575 struct smc_sock *smc; 1576 1577 if (!sk) 1578 return EPOLLNVAL; 1579 1580 smc = smc_sk(sock->sk); 1581 if (smc->use_fallback) { 1582 /* delegate to CLC child sock */ 1583 mask = smc->clcsock->ops->poll(file, smc->clcsock, wait); 1584 sk->sk_err = smc->clcsock->sk->sk_err; 1585 if (sk->sk_err) 1586 mask |= EPOLLERR; 1587 } else { 1588 if (sk->sk_state != SMC_CLOSED) 1589 sock_poll_wait(file, sock, wait); 1590 if (sk->sk_err) 1591 mask |= EPOLLERR; 1592 if ((sk->sk_shutdown == SHUTDOWN_MASK) || 1593 (sk->sk_state == SMC_CLOSED)) 1594 mask |= EPOLLHUP; 1595 if (sk->sk_state == SMC_LISTEN) { 1596 /* woken up by sk_data_ready in smc_listen_work() */ 1597 mask = smc_accept_poll(sk); 1598 } else { 1599 if (atomic_read(&smc->conn.sndbuf_space) || 1600 sk->sk_shutdown & SEND_SHUTDOWN) { 1601 mask |= EPOLLOUT | EPOLLWRNORM; 1602 } else { 1603 sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); 1604 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 1605 } 1606 if (atomic_read(&smc->conn.bytes_to_rcv)) 1607 mask |= EPOLLIN | EPOLLRDNORM; 1608 if (sk->sk_shutdown & RCV_SHUTDOWN) 1609 mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; 1610 if (sk->sk_state == SMC_APPCLOSEWAIT1) 1611 mask |= EPOLLIN; 1612 if (smc->conn.urg_state == SMC_URG_VALID) 1613 mask |= EPOLLPRI; 1614 } 1615 } 1616 1617 return mask; 1618 } 1619 1620 static int smc_shutdown(struct socket *sock, int how) 1621 { 1622 struct sock *sk = sock->sk; 1623 struct smc_sock *smc; 1624 int rc = -EINVAL; 1625 int rc1 = 0; 1626 1627 smc = smc_sk(sk); 1628 1629 if ((how < SHUT_RD) || (how > SHUT_RDWR)) 1630 return rc; 1631 1632 lock_sock(sk); 1633 1634 rc = -ENOTCONN; 1635 if ((sk->sk_state != SMC_ACTIVE) && 1636 (sk->sk_state != SMC_PEERCLOSEWAIT1) && 1637 (sk->sk_state != SMC_PEERCLOSEWAIT2) && 1638 (sk->sk_state != SMC_APPCLOSEWAIT1) && 1639 (sk->sk_state != SMC_APPCLOSEWAIT2) && 1640 (sk->sk_state != SMC_APPFINCLOSEWAIT)) 1641 goto out; 1642 if (smc->use_fallback) { 1643 rc = kernel_sock_shutdown(smc->clcsock, how); 1644 sk->sk_shutdown = smc->clcsock->sk->sk_shutdown; 1645 if (sk->sk_shutdown == SHUTDOWN_MASK) 1646 sk->sk_state = SMC_CLOSED; 1647 goto out; 1648 } 1649 switch (how) { 1650 case SHUT_RDWR: /* shutdown in both directions */ 1651 rc = smc_close_active(smc); 1652 break; 1653 case SHUT_WR: 1654 rc = smc_close_shutdown_write(smc); 1655 break; 1656 case SHUT_RD: 1657 rc = 0; 1658 /* nothing more to do because peer is not involved */ 1659 break; 1660 } 1661 if (smc->clcsock) 1662 rc1 = kernel_sock_shutdown(smc->clcsock, how); 1663 /* map sock_shutdown_cmd constants to sk_shutdown value range */ 1664 sk->sk_shutdown |= how + 1; 1665 1666 out: 1667 release_sock(sk); 1668 return rc ? rc : rc1; 1669 } 1670 1671 static int smc_setsockopt(struct socket *sock, int level, int optname, 1672 char __user *optval, unsigned int optlen) 1673 { 1674 struct sock *sk = sock->sk; 1675 struct smc_sock *smc; 1676 int val, rc; 1677 1678 smc = smc_sk(sk); 1679 1680 /* generic setsockopts reaching us here always apply to the 1681 * CLC socket 1682 */ 1683 rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname, 1684 optval, optlen); 1685 if (smc->clcsock->sk->sk_err) { 1686 sk->sk_err = smc->clcsock->sk->sk_err; 1687 sk->sk_error_report(sk); 1688 } 1689 if (rc) 1690 return rc; 1691 1692 if (optlen < sizeof(int)) 1693 return -EINVAL; 1694 if (get_user(val, (int __user *)optval)) 1695 return -EFAULT; 1696 1697 lock_sock(sk); 1698 switch (optname) { 1699 case TCP_ULP: 1700 case TCP_FASTOPEN: 1701 case TCP_FASTOPEN_CONNECT: 1702 case TCP_FASTOPEN_KEY: 1703 case TCP_FASTOPEN_NO_COOKIE: 1704 /* option not supported by SMC */ 1705 if (sk->sk_state == SMC_INIT) { 1706 smc->use_fallback = true; 1707 smc->fallback_rsn = SMC_CLC_DECL_OPTUNSUPP; 1708 } else { 1709 if (!smc->use_fallback) 1710 rc = -EINVAL; 1711 } 1712 break; 1713 case TCP_NODELAY: 1714 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) { 1715 if (val && !smc->use_fallback) 1716 mod_delayed_work(system_wq, &smc->conn.tx_work, 1717 0); 1718 } 1719 break; 1720 case TCP_CORK: 1721 if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) { 1722 if (!val && !smc->use_fallback) 1723 mod_delayed_work(system_wq, &smc->conn.tx_work, 1724 0); 1725 } 1726 break; 1727 case TCP_DEFER_ACCEPT: 1728 smc->sockopt_defer_accept = val; 1729 break; 1730 default: 1731 break; 1732 } 1733 release_sock(sk); 1734 1735 return rc; 1736 } 1737 1738 static int smc_getsockopt(struct socket *sock, int level, int optname, 1739 char __user *optval, int __user *optlen) 1740 { 1741 struct smc_sock *smc; 1742 1743 smc = smc_sk(sock->sk); 1744 /* socket options apply to the CLC socket */ 1745 return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname, 1746 optval, optlen); 1747 } 1748 1749 static int smc_ioctl(struct socket *sock, unsigned int cmd, 1750 unsigned long arg) 1751 { 1752 union smc_host_cursor cons, urg; 1753 struct smc_connection *conn; 1754 struct smc_sock *smc; 1755 int answ; 1756 1757 smc = smc_sk(sock->sk); 1758 conn = &smc->conn; 1759 lock_sock(&smc->sk); 1760 if (smc->use_fallback) { 1761 if (!smc->clcsock) { 1762 release_sock(&smc->sk); 1763 return -EBADF; 1764 } 1765 answ = smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg); 1766 release_sock(&smc->sk); 1767 return answ; 1768 } 1769 switch (cmd) { 1770 case SIOCINQ: /* same as FIONREAD */ 1771 if (smc->sk.sk_state == SMC_LISTEN) { 1772 release_sock(&smc->sk); 1773 return -EINVAL; 1774 } 1775 if (smc->sk.sk_state == SMC_INIT || 1776 smc->sk.sk_state == SMC_CLOSED) 1777 answ = 0; 1778 else 1779 answ = atomic_read(&smc->conn.bytes_to_rcv); 1780 break; 1781 case SIOCOUTQ: 1782 /* output queue size (not send + not acked) */ 1783 if (smc->sk.sk_state == SMC_LISTEN) { 1784 release_sock(&smc->sk); 1785 return -EINVAL; 1786 } 1787 if (smc->sk.sk_state == SMC_INIT || 1788 smc->sk.sk_state == SMC_CLOSED) 1789 answ = 0; 1790 else 1791 answ = smc->conn.sndbuf_desc->len - 1792 atomic_read(&smc->conn.sndbuf_space); 1793 break; 1794 case SIOCOUTQNSD: 1795 /* output queue size (not send only) */ 1796 if (smc->sk.sk_state == SMC_LISTEN) { 1797 release_sock(&smc->sk); 1798 return -EINVAL; 1799 } 1800 if (smc->sk.sk_state == SMC_INIT || 1801 smc->sk.sk_state == SMC_CLOSED) 1802 answ = 0; 1803 else 1804 answ = smc_tx_prepared_sends(&smc->conn); 1805 break; 1806 case SIOCATMARK: 1807 if (smc->sk.sk_state == SMC_LISTEN) { 1808 release_sock(&smc->sk); 1809 return -EINVAL; 1810 } 1811 if (smc->sk.sk_state == SMC_INIT || 1812 smc->sk.sk_state == SMC_CLOSED) { 1813 answ = 0; 1814 } else { 1815 smc_curs_copy(&cons, &conn->local_tx_ctrl.cons, conn); 1816 smc_curs_copy(&urg, &conn->urg_curs, conn); 1817 answ = smc_curs_diff(conn->rmb_desc->len, 1818 &cons, &urg) == 1; 1819 } 1820 break; 1821 default: 1822 release_sock(&smc->sk); 1823 return -ENOIOCTLCMD; 1824 } 1825 release_sock(&smc->sk); 1826 1827 return put_user(answ, (int __user *)arg); 1828 } 1829 1830 static ssize_t smc_sendpage(struct socket *sock, struct page *page, 1831 int offset, size_t size, int flags) 1832 { 1833 struct sock *sk = sock->sk; 1834 struct smc_sock *smc; 1835 int rc = -EPIPE; 1836 1837 smc = smc_sk(sk); 1838 lock_sock(sk); 1839 if (sk->sk_state != SMC_ACTIVE) { 1840 release_sock(sk); 1841 goto out; 1842 } 1843 release_sock(sk); 1844 if (smc->use_fallback) 1845 rc = kernel_sendpage(smc->clcsock, page, offset, 1846 size, flags); 1847 else 1848 rc = sock_no_sendpage(sock, page, offset, size, flags); 1849 1850 out: 1851 return rc; 1852 } 1853 1854 /* Map the affected portions of the rmbe into an spd, note the number of bytes 1855 * to splice in conn->splice_pending, and press 'go'. Delays consumer cursor 1856 * updates till whenever a respective page has been fully processed. 1857 * Note that subsequent recv() calls have to wait till all splice() processing 1858 * completed. 1859 */ 1860 static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos, 1861 struct pipe_inode_info *pipe, size_t len, 1862 unsigned int flags) 1863 { 1864 struct sock *sk = sock->sk; 1865 struct smc_sock *smc; 1866 int rc = -ENOTCONN; 1867 1868 smc = smc_sk(sk); 1869 lock_sock(sk); 1870 if (sk->sk_state == SMC_CLOSED && (sk->sk_shutdown & RCV_SHUTDOWN)) { 1871 /* socket was connected before, no more data to read */ 1872 rc = 0; 1873 goto out; 1874 } 1875 if (sk->sk_state == SMC_INIT || 1876 sk->sk_state == SMC_LISTEN || 1877 sk->sk_state == SMC_CLOSED) 1878 goto out; 1879 1880 if (sk->sk_state == SMC_PEERFINCLOSEWAIT) { 1881 rc = 0; 1882 goto out; 1883 } 1884 1885 if (smc->use_fallback) { 1886 rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos, 1887 pipe, len, flags); 1888 } else { 1889 if (*ppos) { 1890 rc = -ESPIPE; 1891 goto out; 1892 } 1893 if (flags & SPLICE_F_NONBLOCK) 1894 flags = MSG_DONTWAIT; 1895 else 1896 flags = 0; 1897 rc = smc_rx_recvmsg(smc, NULL, pipe, len, flags); 1898 } 1899 out: 1900 release_sock(sk); 1901 1902 return rc; 1903 } 1904 1905 /* must look like tcp */ 1906 static const struct proto_ops smc_sock_ops = { 1907 .family = PF_SMC, 1908 .owner = THIS_MODULE, 1909 .release = smc_release, 1910 .bind = smc_bind, 1911 .connect = smc_connect, 1912 .socketpair = sock_no_socketpair, 1913 .accept = smc_accept, 1914 .getname = smc_getname, 1915 .poll = smc_poll, 1916 .ioctl = smc_ioctl, 1917 .listen = smc_listen, 1918 .shutdown = smc_shutdown, 1919 .setsockopt = smc_setsockopt, 1920 .getsockopt = smc_getsockopt, 1921 .sendmsg = smc_sendmsg, 1922 .recvmsg = smc_recvmsg, 1923 .mmap = sock_no_mmap, 1924 .sendpage = smc_sendpage, 1925 .splice_read = smc_splice_read, 1926 }; 1927 1928 static int smc_create(struct net *net, struct socket *sock, int protocol, 1929 int kern) 1930 { 1931 int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET; 1932 struct smc_sock *smc; 1933 struct sock *sk; 1934 int rc; 1935 1936 rc = -ESOCKTNOSUPPORT; 1937 if (sock->type != SOCK_STREAM) 1938 goto out; 1939 1940 rc = -EPROTONOSUPPORT; 1941 if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6) 1942 goto out; 1943 1944 rc = -ENOBUFS; 1945 sock->ops = &smc_sock_ops; 1946 sk = smc_sock_alloc(net, sock, protocol); 1947 if (!sk) 1948 goto out; 1949 1950 /* create internal TCP socket for CLC handshake and fallback */ 1951 smc = smc_sk(sk); 1952 smc->use_fallback = false; /* assume rdma capability first */ 1953 smc->fallback_rsn = 0; 1954 rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP, 1955 &smc->clcsock); 1956 if (rc) { 1957 sk_common_release(sk); 1958 goto out; 1959 } 1960 smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE); 1961 smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE); 1962 1963 out: 1964 return rc; 1965 } 1966 1967 static const struct net_proto_family smc_sock_family_ops = { 1968 .family = PF_SMC, 1969 .owner = THIS_MODULE, 1970 .create = smc_create, 1971 }; 1972 1973 unsigned int smc_net_id; 1974 1975 static __net_init int smc_net_init(struct net *net) 1976 { 1977 return smc_pnet_net_init(net); 1978 } 1979 1980 static void __net_exit smc_net_exit(struct net *net) 1981 { 1982 smc_pnet_net_exit(net); 1983 } 1984 1985 static struct pernet_operations smc_net_ops = { 1986 .init = smc_net_init, 1987 .exit = smc_net_exit, 1988 .id = &smc_net_id, 1989 .size = sizeof(struct smc_net), 1990 }; 1991 1992 static int __init smc_init(void) 1993 { 1994 int rc; 1995 1996 rc = register_pernet_subsys(&smc_net_ops); 1997 if (rc) 1998 return rc; 1999 2000 rc = smc_pnet_init(); 2001 if (rc) 2002 return rc; 2003 2004 rc = smc_llc_init(); 2005 if (rc) { 2006 pr_err("%s: smc_llc_init fails with %d\n", __func__, rc); 2007 goto out_pnet; 2008 } 2009 2010 rc = smc_cdc_init(); 2011 if (rc) { 2012 pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc); 2013 goto out_pnet; 2014 } 2015 2016 rc = proto_register(&smc_proto, 1); 2017 if (rc) { 2018 pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc); 2019 goto out_pnet; 2020 } 2021 2022 rc = proto_register(&smc_proto6, 1); 2023 if (rc) { 2024 pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc); 2025 goto out_proto; 2026 } 2027 2028 rc = sock_register(&smc_sock_family_ops); 2029 if (rc) { 2030 pr_err("%s: sock_register fails with %d\n", __func__, rc); 2031 goto out_proto6; 2032 } 2033 INIT_HLIST_HEAD(&smc_v4_hashinfo.ht); 2034 INIT_HLIST_HEAD(&smc_v6_hashinfo.ht); 2035 2036 rc = smc_ib_register_client(); 2037 if (rc) { 2038 pr_err("%s: ib_register fails with %d\n", __func__, rc); 2039 goto out_sock; 2040 } 2041 2042 static_branch_enable(&tcp_have_smc); 2043 return 0; 2044 2045 out_sock: 2046 sock_unregister(PF_SMC); 2047 out_proto6: 2048 proto_unregister(&smc_proto6); 2049 out_proto: 2050 proto_unregister(&smc_proto); 2051 out_pnet: 2052 smc_pnet_exit(); 2053 return rc; 2054 } 2055 2056 static void __exit smc_exit(void) 2057 { 2058 smc_core_exit(); 2059 static_branch_disable(&tcp_have_smc); 2060 smc_ib_unregister_client(); 2061 sock_unregister(PF_SMC); 2062 proto_unregister(&smc_proto6); 2063 proto_unregister(&smc_proto); 2064 smc_pnet_exit(); 2065 unregister_pernet_subsys(&smc_net_ops); 2066 } 2067 2068 module_init(smc_init); 2069 module_exit(smc_exit); 2070 2071 MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>"); 2072 MODULE_DESCRIPTION("smc socket address family"); 2073 MODULE_LICENSE("GPL"); 2074 MODULE_ALIAS_NETPROTO(PF_SMC); 2075