Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 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 | * |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 9 | * Initial restrictions: |
| 10 | * - non-blocking connect postponed |
| 11 | * - IPv6 support postponed |
| 12 | * - support for alternate links postponed |
| 13 | * - partial support for non-blocking sockets only |
| 14 | * - support for urgent data postponed |
| 15 | * |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 16 | * Copyright IBM Corp. 2016 |
| 17 | * |
| 18 | * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com> |
| 19 | * based on prototype from Frank Blaschka |
| 20 | */ |
| 21 | |
| 22 | #define KMSG_COMPONENT "smc" |
| 23 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 24 | |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/socket.h> |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 27 | #include <linux/inetdevice.h> |
| 28 | #include <linux/workqueue.h> |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame^] | 29 | #include <linux/in.h> |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 30 | #include <net/sock.h> |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 31 | #include <net/tcp.h> |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 32 | |
| 33 | #include "smc.h" |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 34 | #include "smc_clc.h" |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 35 | #include "smc_llc.h" |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame^] | 36 | #include "smc_cdc.h" |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 37 | #include "smc_core.h" |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 38 | #include "smc_ib.h" |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 39 | #include "smc_pnet.h" |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 40 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 41 | static DEFINE_MUTEX(smc_create_lgr_pending); /* serialize link group |
| 42 | * creation |
| 43 | */ |
| 44 | |
| 45 | struct smc_lgr_list smc_lgr_list = { /* established link groups */ |
| 46 | .lock = __SPIN_LOCK_UNLOCKED(smc_lgr_list.lock), |
| 47 | .list = LIST_HEAD_INIT(smc_lgr_list.list), |
| 48 | }; |
| 49 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 50 | static void smc_tcp_listen_work(struct work_struct *); |
| 51 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 52 | static void smc_set_keepalive(struct sock *sk, int val) |
| 53 | { |
| 54 | struct smc_sock *smc = smc_sk(sk); |
| 55 | |
| 56 | smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val); |
| 57 | } |
| 58 | |
| 59 | static struct proto smc_proto = { |
| 60 | .name = "SMC", |
| 61 | .owner = THIS_MODULE, |
| 62 | .keepalive = smc_set_keepalive, |
| 63 | .obj_size = sizeof(struct smc_sock), |
| 64 | .slab_flags = SLAB_DESTROY_BY_RCU, |
| 65 | }; |
| 66 | |
| 67 | static int smc_release(struct socket *sock) |
| 68 | { |
| 69 | struct sock *sk = sock->sk; |
| 70 | struct smc_sock *smc; |
| 71 | |
| 72 | if (!sk) |
| 73 | goto out; |
| 74 | |
| 75 | smc = smc_sk(sk); |
| 76 | lock_sock(sk); |
| 77 | |
| 78 | sk->sk_state = SMC_CLOSED; |
| 79 | if (smc->clcsock) { |
| 80 | sock_release(smc->clcsock); |
| 81 | smc->clcsock = NULL; |
| 82 | } |
| 83 | |
| 84 | /* detach socket */ |
| 85 | sock_orphan(sk); |
| 86 | sock->sk = NULL; |
| 87 | release_sock(sk); |
| 88 | |
| 89 | sock_put(sk); |
| 90 | out: |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static void smc_destruct(struct sock *sk) |
| 95 | { |
| 96 | if (sk->sk_state != SMC_CLOSED) |
| 97 | return; |
| 98 | if (!sock_flag(sk, SOCK_DEAD)) |
| 99 | return; |
| 100 | |
| 101 | sk_refcnt_debug_dec(sk); |
| 102 | } |
| 103 | |
| 104 | static struct sock *smc_sock_alloc(struct net *net, struct socket *sock) |
| 105 | { |
| 106 | struct smc_sock *smc; |
| 107 | struct sock *sk; |
| 108 | |
| 109 | sk = sk_alloc(net, PF_SMC, GFP_KERNEL, &smc_proto, 0); |
| 110 | if (!sk) |
| 111 | return NULL; |
| 112 | |
| 113 | sock_init_data(sock, sk); /* sets sk_refcnt to 1 */ |
| 114 | sk->sk_state = SMC_INIT; |
| 115 | sk->sk_destruct = smc_destruct; |
| 116 | sk->sk_protocol = SMCPROTO_SMC; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 117 | smc = smc_sk(sk); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 118 | INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work); |
| 119 | INIT_LIST_HEAD(&smc->accept_q); |
| 120 | spin_lock_init(&smc->accept_q_lock); |
| 121 | sk_refcnt_debug_inc(sk); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 122 | |
| 123 | return sk; |
| 124 | } |
| 125 | |
| 126 | static int smc_bind(struct socket *sock, struct sockaddr *uaddr, |
| 127 | int addr_len) |
| 128 | { |
| 129 | struct sockaddr_in *addr = (struct sockaddr_in *)uaddr; |
| 130 | struct sock *sk = sock->sk; |
| 131 | struct smc_sock *smc; |
| 132 | int rc; |
| 133 | |
| 134 | smc = smc_sk(sk); |
| 135 | |
| 136 | /* replicate tests from inet_bind(), to be safe wrt. future changes */ |
| 137 | rc = -EINVAL; |
| 138 | if (addr_len < sizeof(struct sockaddr_in)) |
| 139 | goto out; |
| 140 | |
| 141 | rc = -EAFNOSUPPORT; |
| 142 | /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */ |
| 143 | if ((addr->sin_family != AF_INET) && |
| 144 | ((addr->sin_family != AF_UNSPEC) || |
| 145 | (addr->sin_addr.s_addr != htonl(INADDR_ANY)))) |
| 146 | goto out; |
| 147 | |
| 148 | lock_sock(sk); |
| 149 | |
| 150 | /* Check if socket is already active */ |
| 151 | rc = -EINVAL; |
| 152 | if (sk->sk_state != SMC_INIT) |
| 153 | goto out_rel; |
| 154 | |
| 155 | smc->clcsock->sk->sk_reuse = sk->sk_reuse; |
| 156 | rc = kernel_bind(smc->clcsock, uaddr, addr_len); |
| 157 | |
| 158 | out_rel: |
| 159 | release_sock(sk); |
| 160 | out: |
| 161 | return rc; |
| 162 | } |
| 163 | |
| 164 | static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk, |
| 165 | unsigned long mask) |
| 166 | { |
| 167 | /* options we don't get control via setsockopt for */ |
| 168 | nsk->sk_type = osk->sk_type; |
| 169 | nsk->sk_sndbuf = osk->sk_sndbuf; |
| 170 | nsk->sk_rcvbuf = osk->sk_rcvbuf; |
| 171 | nsk->sk_sndtimeo = osk->sk_sndtimeo; |
| 172 | nsk->sk_rcvtimeo = osk->sk_rcvtimeo; |
| 173 | nsk->sk_mark = osk->sk_mark; |
| 174 | nsk->sk_priority = osk->sk_priority; |
| 175 | nsk->sk_rcvlowat = osk->sk_rcvlowat; |
| 176 | nsk->sk_bound_dev_if = osk->sk_bound_dev_if; |
| 177 | nsk->sk_err = osk->sk_err; |
| 178 | |
| 179 | nsk->sk_flags &= ~mask; |
| 180 | nsk->sk_flags |= osk->sk_flags & mask; |
| 181 | } |
| 182 | |
| 183 | #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \ |
| 184 | (1UL << SOCK_KEEPOPEN) | \ |
| 185 | (1UL << SOCK_LINGER) | \ |
| 186 | (1UL << SOCK_BROADCAST) | \ |
| 187 | (1UL << SOCK_TIMESTAMP) | \ |
| 188 | (1UL << SOCK_DBG) | \ |
| 189 | (1UL << SOCK_RCVTSTAMP) | \ |
| 190 | (1UL << SOCK_RCVTSTAMPNS) | \ |
| 191 | (1UL << SOCK_LOCALROUTE) | \ |
| 192 | (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \ |
| 193 | (1UL << SOCK_RXQ_OVFL) | \ |
| 194 | (1UL << SOCK_WIFI_STATUS) | \ |
| 195 | (1UL << SOCK_NOFCS) | \ |
| 196 | (1UL << SOCK_FILTER_LOCKED)) |
| 197 | /* copy only relevant settings and flags of SOL_SOCKET level from smc to |
| 198 | * clc socket (since smc is not called for these options from net/core) |
| 199 | */ |
| 200 | static void smc_copy_sock_settings_to_clc(struct smc_sock *smc) |
| 201 | { |
| 202 | smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC); |
| 203 | } |
| 204 | |
| 205 | #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \ |
| 206 | (1UL << SOCK_KEEPOPEN) | \ |
| 207 | (1UL << SOCK_LINGER) | \ |
| 208 | (1UL << SOCK_DBG)) |
| 209 | /* copy only settings and flags relevant for smc from clc to smc socket */ |
| 210 | static void smc_copy_sock_settings_to_smc(struct smc_sock *smc) |
| 211 | { |
| 212 | smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC); |
| 213 | } |
| 214 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 215 | /* determine subnet and mask of internal TCP socket */ |
| 216 | int smc_netinfo_by_tcpsk(struct socket *clcsock, |
| 217 | __be32 *subnet, u8 *prefix_len) |
| 218 | { |
| 219 | struct dst_entry *dst = sk_dst_get(clcsock->sk); |
| 220 | struct sockaddr_in addr; |
| 221 | int rc = -ENOENT; |
| 222 | int len; |
| 223 | |
| 224 | if (!dst) { |
| 225 | rc = -ENOTCONN; |
| 226 | goto out; |
| 227 | } |
| 228 | if (!dst->dev) { |
| 229 | rc = -ENODEV; |
| 230 | goto out_rel; |
| 231 | } |
| 232 | |
| 233 | /* get address to which the internal TCP socket is bound */ |
| 234 | kernel_getsockname(clcsock, (struct sockaddr *)&addr, &len); |
| 235 | /* analyze IPv4 specific data of net_device belonging to TCP socket */ |
| 236 | for_ifa(dst->dev->ip_ptr) { |
| 237 | if (ifa->ifa_address != addr.sin_addr.s_addr) |
| 238 | continue; |
| 239 | *prefix_len = inet_mask_len(ifa->ifa_mask); |
| 240 | *subnet = ifa->ifa_address & ifa->ifa_mask; |
| 241 | rc = 0; |
| 242 | break; |
| 243 | } endfor_ifa(dst->dev->ip_ptr); |
| 244 | |
| 245 | out_rel: |
| 246 | dst_release(dst); |
| 247 | out: |
| 248 | return rc; |
| 249 | } |
| 250 | |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 251 | static int smc_clnt_conf_first_link(struct smc_sock *smc, union ib_gid *gid) |
| 252 | { |
| 253 | struct smc_link_group *lgr = smc->conn.lgr; |
| 254 | struct smc_link *link; |
| 255 | int rest; |
| 256 | int rc; |
| 257 | |
| 258 | link = &lgr->lnk[SMC_SINGLE_LINK]; |
| 259 | /* receive CONFIRM LINK request from server over RoCE fabric */ |
| 260 | rest = wait_for_completion_interruptible_timeout( |
| 261 | &link->llc_confirm, |
| 262 | SMC_LLC_WAIT_FIRST_TIME); |
| 263 | if (rest <= 0) { |
| 264 | struct smc_clc_msg_decline dclc; |
| 265 | |
| 266 | rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc), |
| 267 | SMC_CLC_DECLINE); |
| 268 | return rc; |
| 269 | } |
| 270 | |
| 271 | rc = smc_ib_modify_qp_rts(link); |
| 272 | if (rc) |
| 273 | return SMC_CLC_DECL_INTERR; |
| 274 | |
| 275 | smc_wr_remember_qp_attr(link); |
| 276 | /* send CONFIRM LINK response over RoCE fabric */ |
| 277 | rc = smc_llc_send_confirm_link(link, |
| 278 | link->smcibdev->mac[link->ibport - 1], |
| 279 | gid, SMC_LLC_RESP); |
| 280 | if (rc < 0) |
| 281 | return SMC_CLC_DECL_TCL; |
| 282 | |
| 283 | return rc; |
| 284 | } |
| 285 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 286 | static void smc_conn_save_peer_info(struct smc_sock *smc, |
| 287 | struct smc_clc_msg_accept_confirm *clc) |
| 288 | { |
| 289 | smc->conn.peer_conn_idx = clc->conn_idx; |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame^] | 290 | smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token); |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 291 | smc->conn.peer_rmbe_size = smc_uncompress_bufsize(clc->rmbe_size); |
| 292 | atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static void smc_link_save_peer_info(struct smc_link *link, |
| 296 | struct smc_clc_msg_accept_confirm *clc) |
| 297 | { |
| 298 | link->peer_qpn = ntoh24(clc->qpn); |
| 299 | memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE); |
| 300 | memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac)); |
| 301 | link->peer_psn = ntoh24(clc->psn); |
| 302 | link->peer_mtu = clc->qp_mtu; |
| 303 | } |
| 304 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 305 | /* setup for RDMA connection of client */ |
| 306 | static int smc_connect_rdma(struct smc_sock *smc) |
| 307 | { |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 308 | struct sockaddr_in *inaddr = (struct sockaddr_in *)smc->addr; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 309 | struct smc_clc_msg_accept_confirm aclc; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 310 | int local_contact = SMC_FIRST_CONTACT; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 311 | struct smc_ib_device *smcibdev; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 312 | struct smc_link *link; |
| 313 | u8 srv_first_contact; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 314 | int reason_code = 0; |
| 315 | int rc = 0; |
| 316 | u8 ibport; |
| 317 | |
| 318 | /* IPSec connections opt out of SMC-R optimizations */ |
| 319 | if (using_ipsec(smc)) { |
| 320 | reason_code = SMC_CLC_DECL_IPSEC; |
| 321 | goto decline_rdma; |
| 322 | } |
| 323 | |
| 324 | /* PNET table look up: search active ib_device and port |
| 325 | * within same PNETID that also contains the ethernet device |
| 326 | * used for the internal TCP socket |
| 327 | */ |
| 328 | smc_pnet_find_roce_resource(smc->clcsock->sk, &smcibdev, &ibport); |
| 329 | if (!smcibdev) { |
| 330 | reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */ |
| 331 | goto decline_rdma; |
| 332 | } |
| 333 | |
| 334 | /* do inband token exchange */ |
| 335 | reason_code = smc_clc_send_proposal(smc, smcibdev, ibport); |
| 336 | if (reason_code < 0) { |
| 337 | rc = reason_code; |
| 338 | goto out_err; |
| 339 | } |
| 340 | if (reason_code > 0) /* configuration error */ |
| 341 | goto decline_rdma; |
| 342 | /* receive SMC Accept CLC message */ |
| 343 | reason_code = smc_clc_wait_msg(smc, &aclc, sizeof(aclc), |
| 344 | SMC_CLC_ACCEPT); |
| 345 | if (reason_code < 0) { |
| 346 | rc = reason_code; |
| 347 | goto out_err; |
| 348 | } |
| 349 | if (reason_code > 0) |
| 350 | goto decline_rdma; |
| 351 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 352 | srv_first_contact = aclc.hdr.flag; |
| 353 | mutex_lock(&smc_create_lgr_pending); |
| 354 | local_contact = smc_conn_create(smc, inaddr->sin_addr.s_addr, smcibdev, |
| 355 | ibport, &aclc.lcl, srv_first_contact); |
| 356 | if (local_contact < 0) { |
| 357 | rc = local_contact; |
| 358 | if (rc == -ENOMEM) |
| 359 | reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/ |
| 360 | else if (rc == -ENOLINK) |
| 361 | reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */ |
| 362 | goto decline_rdma_unlock; |
| 363 | } |
| 364 | link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK]; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 365 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 366 | smc_conn_save_peer_info(smc, &aclc); |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 367 | |
| 368 | rc = smc_sndbuf_create(smc); |
| 369 | if (rc) { |
| 370 | reason_code = SMC_CLC_DECL_MEM; |
| 371 | goto decline_rdma_unlock; |
| 372 | } |
| 373 | rc = smc_rmb_create(smc); |
| 374 | if (rc) { |
| 375 | reason_code = SMC_CLC_DECL_MEM; |
| 376 | goto decline_rdma_unlock; |
| 377 | } |
| 378 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 379 | if (local_contact == SMC_FIRST_CONTACT) |
| 380 | smc_link_save_peer_info(link, &aclc); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 381 | |
| 382 | rc = smc_rmb_rtoken_handling(&smc->conn, &aclc); |
| 383 | if (rc) { |
| 384 | reason_code = SMC_CLC_DECL_INTERR; |
| 385 | goto decline_rdma_unlock; |
| 386 | } |
| 387 | |
| 388 | if (local_contact == SMC_FIRST_CONTACT) { |
| 389 | rc = smc_ib_ready_link(link); |
| 390 | if (rc) { |
| 391 | reason_code = SMC_CLC_DECL_INTERR; |
| 392 | goto decline_rdma_unlock; |
| 393 | } |
| 394 | } |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 395 | |
| 396 | rc = smc_clc_send_confirm(smc); |
| 397 | if (rc) |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 398 | goto out_err_unlock; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 399 | |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 400 | if (local_contact == SMC_FIRST_CONTACT) { |
| 401 | /* QP confirmation over RoCE fabric */ |
| 402 | reason_code = smc_clnt_conf_first_link( |
| 403 | smc, &smcibdev->gid[ibport - 1]); |
| 404 | if (reason_code < 0) { |
| 405 | rc = reason_code; |
| 406 | goto out_err_unlock; |
| 407 | } |
| 408 | if (reason_code > 0) |
| 409 | goto decline_rdma_unlock; |
| 410 | } |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 411 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 412 | mutex_unlock(&smc_create_lgr_pending); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 413 | out_connected: |
| 414 | smc_copy_sock_settings_to_clc(smc); |
| 415 | smc->sk.sk_state = SMC_ACTIVE; |
| 416 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 417 | return rc ? rc : local_contact; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 418 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 419 | decline_rdma_unlock: |
| 420 | mutex_unlock(&smc_create_lgr_pending); |
| 421 | smc_conn_free(&smc->conn); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 422 | decline_rdma: |
| 423 | /* RDMA setup failed, switch back to TCP */ |
| 424 | smc->use_fallback = true; |
| 425 | if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) { |
| 426 | rc = smc_clc_send_decline(smc, reason_code, 0); |
| 427 | if (rc < sizeof(struct smc_clc_msg_decline)) |
| 428 | goto out_err; |
| 429 | } |
| 430 | goto out_connected; |
| 431 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 432 | out_err_unlock: |
| 433 | mutex_unlock(&smc_create_lgr_pending); |
| 434 | smc_conn_free(&smc->conn); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 435 | out_err: |
| 436 | return rc; |
| 437 | } |
| 438 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 439 | static int smc_connect(struct socket *sock, struct sockaddr *addr, |
| 440 | int alen, int flags) |
| 441 | { |
| 442 | struct sock *sk = sock->sk; |
| 443 | struct smc_sock *smc; |
| 444 | int rc = -EINVAL; |
| 445 | |
| 446 | smc = smc_sk(sk); |
| 447 | |
| 448 | /* separate smc parameter checking to be safe */ |
| 449 | if (alen < sizeof(addr->sa_family)) |
| 450 | goto out_err; |
| 451 | if (addr->sa_family != AF_INET) |
| 452 | goto out_err; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 453 | smc->addr = addr; /* needed for nonblocking connect */ |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 454 | |
| 455 | lock_sock(sk); |
| 456 | switch (sk->sk_state) { |
| 457 | default: |
| 458 | goto out; |
| 459 | case SMC_ACTIVE: |
| 460 | rc = -EISCONN; |
| 461 | goto out; |
| 462 | case SMC_INIT: |
| 463 | rc = 0; |
| 464 | break; |
| 465 | } |
| 466 | |
| 467 | smc_copy_sock_settings_to_clc(smc); |
| 468 | rc = kernel_connect(smc->clcsock, addr, alen, flags); |
| 469 | if (rc) |
| 470 | goto out; |
| 471 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 472 | /* setup RDMA connection */ |
| 473 | rc = smc_connect_rdma(smc); |
| 474 | if (rc < 0) |
| 475 | goto out; |
| 476 | else |
| 477 | rc = 0; /* success cases including fallback */ |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 478 | |
| 479 | out: |
| 480 | release_sock(sk); |
| 481 | out_err: |
| 482 | return rc; |
| 483 | } |
| 484 | |
| 485 | static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc) |
| 486 | { |
| 487 | struct sock *sk = &lsmc->sk; |
| 488 | struct socket *new_clcsock; |
| 489 | struct sock *new_sk; |
| 490 | int rc; |
| 491 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 492 | release_sock(&lsmc->sk); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 493 | new_sk = smc_sock_alloc(sock_net(sk), NULL); |
| 494 | if (!new_sk) { |
| 495 | rc = -ENOMEM; |
| 496 | lsmc->sk.sk_err = ENOMEM; |
| 497 | *new_smc = NULL; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 498 | lock_sock(&lsmc->sk); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 499 | goto out; |
| 500 | } |
| 501 | *new_smc = smc_sk(new_sk); |
| 502 | |
| 503 | rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 504 | lock_sock(&lsmc->sk); |
| 505 | if (rc < 0) { |
| 506 | lsmc->sk.sk_err = -rc; |
| 507 | new_sk->sk_state = SMC_CLOSED; |
| 508 | sock_set_flag(new_sk, SOCK_DEAD); |
| 509 | sock_put(new_sk); |
| 510 | *new_smc = NULL; |
| 511 | goto out; |
| 512 | } |
| 513 | if (lsmc->sk.sk_state == SMC_CLOSED) { |
| 514 | if (new_clcsock) |
| 515 | sock_release(new_clcsock); |
| 516 | new_sk->sk_state = SMC_CLOSED; |
| 517 | sock_set_flag(new_sk, SOCK_DEAD); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 518 | sock_put(new_sk); |
| 519 | *new_smc = NULL; |
| 520 | goto out; |
| 521 | } |
| 522 | |
| 523 | (*new_smc)->clcsock = new_clcsock; |
| 524 | out: |
| 525 | return rc; |
| 526 | } |
| 527 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 528 | /* add a just created sock to the accept queue of the listen sock as |
| 529 | * candidate for a following socket accept call from user space |
| 530 | */ |
| 531 | static void smc_accept_enqueue(struct sock *parent, struct sock *sk) |
| 532 | { |
| 533 | struct smc_sock *par = smc_sk(parent); |
| 534 | |
| 535 | sock_hold(sk); |
| 536 | spin_lock(&par->accept_q_lock); |
| 537 | list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q); |
| 538 | spin_unlock(&par->accept_q_lock); |
| 539 | sk_acceptq_added(parent); |
| 540 | } |
| 541 | |
| 542 | /* remove a socket from the accept queue of its parental listening socket */ |
| 543 | static void smc_accept_unlink(struct sock *sk) |
| 544 | { |
| 545 | struct smc_sock *par = smc_sk(sk)->listen_smc; |
| 546 | |
| 547 | spin_lock(&par->accept_q_lock); |
| 548 | list_del_init(&smc_sk(sk)->accept_q); |
| 549 | spin_unlock(&par->accept_q_lock); |
| 550 | sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk); |
| 551 | sock_put(sk); |
| 552 | } |
| 553 | |
| 554 | /* remove a sock from the accept queue to bind it to a new socket created |
| 555 | * for a socket accept call from user space |
| 556 | */ |
| 557 | static struct sock *smc_accept_dequeue(struct sock *parent, |
| 558 | struct socket *new_sock) |
| 559 | { |
| 560 | struct smc_sock *isk, *n; |
| 561 | struct sock *new_sk; |
| 562 | |
| 563 | list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) { |
| 564 | new_sk = (struct sock *)isk; |
| 565 | |
| 566 | smc_accept_unlink(new_sk); |
| 567 | if (new_sk->sk_state == SMC_CLOSED) { |
| 568 | /* tbd in follow-on patch: close this sock */ |
| 569 | continue; |
| 570 | } |
| 571 | if (new_sock) |
| 572 | sock_graft(new_sk, new_sock); |
| 573 | return new_sk; |
| 574 | } |
| 575 | return NULL; |
| 576 | } |
| 577 | |
| 578 | /* clean up for a created but never accepted sock */ |
| 579 | static void smc_close_non_accepted(struct sock *sk) |
| 580 | { |
| 581 | struct smc_sock *smc = smc_sk(sk); |
| 582 | |
| 583 | sock_hold(sk); |
| 584 | if (smc->clcsock) { |
| 585 | struct socket *tcp; |
| 586 | |
| 587 | tcp = smc->clcsock; |
| 588 | smc->clcsock = NULL; |
| 589 | sock_release(tcp); |
| 590 | } |
| 591 | /* more closing stuff to be added with socket closing patch */ |
| 592 | sock_put(sk); |
| 593 | } |
| 594 | |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 595 | static int smc_serv_conf_first_link(struct smc_sock *smc) |
| 596 | { |
| 597 | struct smc_link_group *lgr = smc->conn.lgr; |
| 598 | struct smc_link *link; |
| 599 | int rest; |
| 600 | int rc; |
| 601 | |
| 602 | link = &lgr->lnk[SMC_SINGLE_LINK]; |
| 603 | /* send CONFIRM LINK request to client over the RoCE fabric */ |
| 604 | rc = smc_llc_send_confirm_link(link, |
| 605 | link->smcibdev->mac[link->ibport - 1], |
| 606 | &link->smcibdev->gid[link->ibport - 1], |
| 607 | SMC_LLC_REQ); |
| 608 | if (rc < 0) |
| 609 | return SMC_CLC_DECL_TCL; |
| 610 | |
| 611 | /* receive CONFIRM LINK response from client over the RoCE fabric */ |
| 612 | rest = wait_for_completion_interruptible_timeout( |
| 613 | &link->llc_confirm_resp, |
| 614 | SMC_LLC_WAIT_FIRST_TIME); |
| 615 | if (rest <= 0) { |
| 616 | struct smc_clc_msg_decline dclc; |
| 617 | |
| 618 | rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc), |
| 619 | SMC_CLC_DECLINE); |
| 620 | } |
| 621 | |
| 622 | return rc; |
| 623 | } |
| 624 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 625 | /* setup for RDMA connection of server */ |
| 626 | static void smc_listen_work(struct work_struct *work) |
| 627 | { |
| 628 | struct smc_sock *new_smc = container_of(work, struct smc_sock, |
| 629 | smc_listen_work); |
| 630 | struct socket *newclcsock = new_smc->clcsock; |
| 631 | struct smc_sock *lsmc = new_smc->listen_smc; |
| 632 | struct smc_clc_msg_accept_confirm cclc; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 633 | int local_contact = SMC_REUSE_CONTACT; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 634 | struct sock *newsmcsk = &new_smc->sk; |
| 635 | struct smc_clc_msg_proposal pclc; |
| 636 | struct smc_ib_device *smcibdev; |
| 637 | struct sockaddr_in peeraddr; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 638 | struct smc_link *link; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 639 | int reason_code = 0; |
| 640 | int rc = 0, len; |
| 641 | __be32 subnet; |
| 642 | u8 prefix_len; |
| 643 | u8 ibport; |
| 644 | |
| 645 | /* do inband token exchange - |
| 646 | *wait for and receive SMC Proposal CLC message |
| 647 | */ |
| 648 | reason_code = smc_clc_wait_msg(new_smc, &pclc, sizeof(pclc), |
| 649 | SMC_CLC_PROPOSAL); |
| 650 | if (reason_code < 0) |
| 651 | goto out_err; |
| 652 | if (reason_code > 0) |
| 653 | goto decline_rdma; |
| 654 | |
| 655 | /* IPSec connections opt out of SMC-R optimizations */ |
| 656 | if (using_ipsec(new_smc)) { |
| 657 | reason_code = SMC_CLC_DECL_IPSEC; |
| 658 | goto decline_rdma; |
| 659 | } |
| 660 | |
| 661 | /* PNET table look up: search active ib_device and port |
| 662 | * within same PNETID that also contains the ethernet device |
| 663 | * used for the internal TCP socket |
| 664 | */ |
| 665 | smc_pnet_find_roce_resource(newclcsock->sk, &smcibdev, &ibport); |
| 666 | if (!smcibdev) { |
| 667 | reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */ |
| 668 | goto decline_rdma; |
| 669 | } |
| 670 | |
| 671 | /* determine subnet and mask from internal TCP socket */ |
| 672 | rc = smc_netinfo_by_tcpsk(newclcsock, &subnet, &prefix_len); |
| 673 | if (rc) { |
| 674 | reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */ |
| 675 | goto decline_rdma; |
| 676 | } |
| 677 | if ((pclc.outgoing_subnet != subnet) || |
| 678 | (pclc.prefix_len != prefix_len)) { |
| 679 | reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */ |
| 680 | goto decline_rdma; |
| 681 | } |
| 682 | |
| 683 | /* get address of the peer connected to the internal TCP socket */ |
| 684 | kernel_getpeername(newclcsock, (struct sockaddr *)&peeraddr, &len); |
| 685 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 686 | /* allocate connection / link group */ |
| 687 | mutex_lock(&smc_create_lgr_pending); |
| 688 | local_contact = smc_conn_create(new_smc, peeraddr.sin_addr.s_addr, |
| 689 | smcibdev, ibport, &pclc.lcl, 0); |
| 690 | if (local_contact == SMC_REUSE_CONTACT) |
| 691 | /* lock no longer needed, free it due to following |
| 692 | * smc_clc_wait_msg() call |
| 693 | */ |
| 694 | mutex_unlock(&smc_create_lgr_pending); |
| 695 | if (local_contact < 0) { |
| 696 | rc = local_contact; |
| 697 | if (rc == -ENOMEM) |
| 698 | reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/ |
| 699 | else if (rc == -ENOLINK) |
| 700 | reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */ |
| 701 | goto decline_rdma; |
| 702 | } |
| 703 | link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK]; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 704 | |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 705 | rc = smc_sndbuf_create(new_smc); |
| 706 | if (rc) { |
| 707 | reason_code = SMC_CLC_DECL_MEM; |
| 708 | goto decline_rdma; |
| 709 | } |
| 710 | rc = smc_rmb_create(new_smc); |
| 711 | if (rc) { |
| 712 | reason_code = SMC_CLC_DECL_MEM; |
| 713 | goto decline_rdma; |
| 714 | } |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 715 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 716 | rc = smc_clc_send_accept(new_smc, local_contact); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 717 | if (rc) |
| 718 | goto out_err; |
| 719 | |
| 720 | /* receive SMC Confirm CLC message */ |
| 721 | reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc), |
| 722 | SMC_CLC_CONFIRM); |
| 723 | if (reason_code < 0) |
| 724 | goto out_err; |
| 725 | if (reason_code > 0) |
| 726 | goto decline_rdma; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 727 | smc_conn_save_peer_info(new_smc, &cclc); |
| 728 | if (local_contact == SMC_FIRST_CONTACT) |
| 729 | smc_link_save_peer_info(link, &cclc); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 730 | |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 731 | rc = smc_rmb_rtoken_handling(&new_smc->conn, &cclc); |
| 732 | if (rc) { |
| 733 | reason_code = SMC_CLC_DECL_INTERR; |
| 734 | goto decline_rdma; |
| 735 | } |
| 736 | |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 737 | if (local_contact == SMC_FIRST_CONTACT) { |
| 738 | rc = smc_ib_ready_link(link); |
| 739 | if (rc) { |
| 740 | reason_code = SMC_CLC_DECL_INTERR; |
| 741 | goto decline_rdma; |
| 742 | } |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 743 | /* QP confirmation over RoCE fabric */ |
| 744 | reason_code = smc_serv_conf_first_link(new_smc); |
| 745 | if (reason_code < 0) { |
| 746 | /* peer is not aware of a problem */ |
| 747 | rc = reason_code; |
| 748 | goto out_err; |
| 749 | } |
| 750 | if (reason_code > 0) |
| 751 | goto decline_rdma; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 752 | } |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 753 | |
| 754 | out_connected: |
| 755 | sk_refcnt_debug_inc(newsmcsk); |
| 756 | newsmcsk->sk_state = SMC_ACTIVE; |
| 757 | enqueue: |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 758 | if (local_contact == SMC_FIRST_CONTACT) |
| 759 | mutex_unlock(&smc_create_lgr_pending); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 760 | lock_sock(&lsmc->sk); |
| 761 | if (lsmc->sk.sk_state == SMC_LISTEN) { |
| 762 | smc_accept_enqueue(&lsmc->sk, newsmcsk); |
| 763 | } else { /* no longer listening */ |
| 764 | smc_close_non_accepted(newsmcsk); |
| 765 | } |
| 766 | release_sock(&lsmc->sk); |
| 767 | |
| 768 | /* Wake up accept */ |
| 769 | lsmc->sk.sk_data_ready(&lsmc->sk); |
| 770 | sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */ |
| 771 | return; |
| 772 | |
| 773 | decline_rdma: |
| 774 | /* RDMA setup failed, switch back to TCP */ |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 775 | smc_conn_free(&new_smc->conn); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 776 | new_smc->use_fallback = true; |
| 777 | if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) { |
| 778 | rc = smc_clc_send_decline(new_smc, reason_code, 0); |
| 779 | if (rc < sizeof(struct smc_clc_msg_decline)) |
| 780 | goto out_err; |
| 781 | } |
| 782 | goto out_connected; |
| 783 | |
| 784 | out_err: |
| 785 | newsmcsk->sk_state = SMC_CLOSED; |
| 786 | goto enqueue; /* queue new sock with sk_err set */ |
| 787 | } |
| 788 | |
| 789 | static void smc_tcp_listen_work(struct work_struct *work) |
| 790 | { |
| 791 | struct smc_sock *lsmc = container_of(work, struct smc_sock, |
| 792 | tcp_listen_work); |
| 793 | struct smc_sock *new_smc; |
| 794 | int rc = 0; |
| 795 | |
| 796 | lock_sock(&lsmc->sk); |
| 797 | while (lsmc->sk.sk_state == SMC_LISTEN) { |
| 798 | rc = smc_clcsock_accept(lsmc, &new_smc); |
| 799 | if (rc) |
| 800 | goto out; |
| 801 | if (!new_smc) |
| 802 | continue; |
| 803 | |
| 804 | new_smc->listen_smc = lsmc; |
| 805 | new_smc->use_fallback = false; /* assume rdma capability first*/ |
| 806 | sock_hold(&lsmc->sk); /* sock_put in smc_listen_work */ |
| 807 | INIT_WORK(&new_smc->smc_listen_work, smc_listen_work); |
| 808 | smc_copy_sock_settings_to_smc(new_smc); |
| 809 | schedule_work(&new_smc->smc_listen_work); |
| 810 | } |
| 811 | |
| 812 | out: |
| 813 | release_sock(&lsmc->sk); |
| 814 | lsmc->sk.sk_data_ready(&lsmc->sk); /* no more listening, wake accept */ |
| 815 | } |
| 816 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 817 | static int smc_listen(struct socket *sock, int backlog) |
| 818 | { |
| 819 | struct sock *sk = sock->sk; |
| 820 | struct smc_sock *smc; |
| 821 | int rc; |
| 822 | |
| 823 | smc = smc_sk(sk); |
| 824 | lock_sock(sk); |
| 825 | |
| 826 | rc = -EINVAL; |
| 827 | if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN)) |
| 828 | goto out; |
| 829 | |
| 830 | rc = 0; |
| 831 | if (sk->sk_state == SMC_LISTEN) { |
| 832 | sk->sk_max_ack_backlog = backlog; |
| 833 | goto out; |
| 834 | } |
| 835 | /* some socket options are handled in core, so we could not apply |
| 836 | * them to the clc socket -- copy smc socket options to clc socket |
| 837 | */ |
| 838 | smc_copy_sock_settings_to_clc(smc); |
| 839 | |
| 840 | rc = kernel_listen(smc->clcsock, backlog); |
| 841 | if (rc) |
| 842 | goto out; |
| 843 | sk->sk_max_ack_backlog = backlog; |
| 844 | sk->sk_ack_backlog = 0; |
| 845 | sk->sk_state = SMC_LISTEN; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 846 | INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work); |
| 847 | schedule_work(&smc->tcp_listen_work); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 848 | |
| 849 | out: |
| 850 | release_sock(sk); |
| 851 | return rc; |
| 852 | } |
| 853 | |
| 854 | static int smc_accept(struct socket *sock, struct socket *new_sock, |
| 855 | int flags) |
| 856 | { |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 857 | struct sock *sk = sock->sk, *nsk; |
| 858 | DECLARE_WAITQUEUE(wait, current); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 859 | struct smc_sock *lsmc; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 860 | long timeo; |
| 861 | int rc = 0; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 862 | |
| 863 | lsmc = smc_sk(sk); |
| 864 | lock_sock(sk); |
| 865 | |
| 866 | if (lsmc->sk.sk_state != SMC_LISTEN) { |
| 867 | rc = -EINVAL; |
| 868 | goto out; |
| 869 | } |
| 870 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 871 | /* Wait for an incoming connection */ |
| 872 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 873 | add_wait_queue_exclusive(sk_sleep(sk), &wait); |
| 874 | while (!(nsk = smc_accept_dequeue(sk, new_sock))) { |
| 875 | set_current_state(TASK_INTERRUPTIBLE); |
| 876 | if (!timeo) { |
| 877 | rc = -EAGAIN; |
| 878 | break; |
| 879 | } |
| 880 | release_sock(sk); |
| 881 | timeo = schedule_timeout(timeo); |
| 882 | /* wakeup by sk_data_ready in smc_listen_work() */ |
| 883 | sched_annotate_sleep(); |
| 884 | lock_sock(sk); |
| 885 | if (signal_pending(current)) { |
| 886 | rc = sock_intr_errno(timeo); |
| 887 | break; |
| 888 | } |
| 889 | } |
| 890 | set_current_state(TASK_RUNNING); |
| 891 | remove_wait_queue(sk_sleep(sk), &wait); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 892 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 893 | if (!rc) |
| 894 | rc = sock_error(nsk); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 895 | |
| 896 | out: |
| 897 | release_sock(sk); |
| 898 | return rc; |
| 899 | } |
| 900 | |
| 901 | static int smc_getname(struct socket *sock, struct sockaddr *addr, |
| 902 | int *len, int peer) |
| 903 | { |
| 904 | struct smc_sock *smc; |
| 905 | |
| 906 | if (peer && (sock->sk->sk_state != SMC_ACTIVE)) |
| 907 | return -ENOTCONN; |
| 908 | |
| 909 | smc = smc_sk(sock->sk); |
| 910 | |
| 911 | return smc->clcsock->ops->getname(smc->clcsock, addr, len, peer); |
| 912 | } |
| 913 | |
| 914 | static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) |
| 915 | { |
| 916 | struct sock *sk = sock->sk; |
| 917 | struct smc_sock *smc; |
| 918 | int rc = -EPIPE; |
| 919 | |
| 920 | smc = smc_sk(sk); |
| 921 | lock_sock(sk); |
| 922 | if (sk->sk_state != SMC_ACTIVE) |
| 923 | goto out; |
| 924 | if (smc->use_fallback) |
| 925 | rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len); |
| 926 | else |
| 927 | rc = sock_no_sendmsg(sock, msg, len); |
| 928 | out: |
| 929 | release_sock(sk); |
| 930 | return rc; |
| 931 | } |
| 932 | |
| 933 | static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, |
| 934 | int flags) |
| 935 | { |
| 936 | struct sock *sk = sock->sk; |
| 937 | struct smc_sock *smc; |
| 938 | int rc = -ENOTCONN; |
| 939 | |
| 940 | smc = smc_sk(sk); |
| 941 | lock_sock(sk); |
| 942 | if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED)) |
| 943 | goto out; |
| 944 | |
| 945 | if (smc->use_fallback) |
| 946 | rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags); |
| 947 | else |
| 948 | rc = sock_no_recvmsg(sock, msg, len, flags); |
| 949 | out: |
| 950 | release_sock(sk); |
| 951 | return rc; |
| 952 | } |
| 953 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 954 | static unsigned int smc_accept_poll(struct sock *parent) |
| 955 | { |
| 956 | struct smc_sock *isk; |
| 957 | struct sock *sk; |
| 958 | |
| 959 | lock_sock(parent); |
| 960 | list_for_each_entry(isk, &smc_sk(parent)->accept_q, accept_q) { |
| 961 | sk = (struct sock *)isk; |
| 962 | |
| 963 | if (sk->sk_state == SMC_ACTIVE) { |
| 964 | release_sock(parent); |
| 965 | return POLLIN | POLLRDNORM; |
| 966 | } |
| 967 | } |
| 968 | release_sock(parent); |
| 969 | |
| 970 | return 0; |
| 971 | } |
| 972 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 973 | static unsigned int smc_poll(struct file *file, struct socket *sock, |
| 974 | poll_table *wait) |
| 975 | { |
| 976 | struct sock *sk = sock->sk; |
| 977 | unsigned int mask = 0; |
| 978 | struct smc_sock *smc; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 979 | int rc; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 980 | |
| 981 | smc = smc_sk(sock->sk); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 982 | if ((sk->sk_state == SMC_INIT) || smc->use_fallback) { |
| 983 | /* delegate to CLC child sock */ |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 984 | mask = smc->clcsock->ops->poll(file, smc->clcsock, wait); |
| 985 | /* if non-blocking connect finished ... */ |
| 986 | lock_sock(sk); |
| 987 | if ((sk->sk_state == SMC_INIT) && (mask & POLLOUT)) { |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 988 | sk->sk_err = smc->clcsock->sk->sk_err; |
| 989 | if (sk->sk_err) { |
| 990 | mask |= POLLERR; |
| 991 | } else { |
| 992 | rc = smc_connect_rdma(smc); |
| 993 | if (rc < 0) |
| 994 | mask |= POLLERR; |
| 995 | else |
| 996 | /* success cases including fallback */ |
| 997 | mask |= POLLOUT | POLLWRNORM; |
| 998 | } |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 999 | } |
| 1000 | release_sock(sk); |
| 1001 | } else { |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1002 | sock_poll_wait(file, sk_sleep(sk), wait); |
| 1003 | if (sk->sk_state == SMC_LISTEN) |
| 1004 | /* woken up by sk_data_ready in smc_listen_work() */ |
| 1005 | mask |= smc_accept_poll(sk); |
| 1006 | if (sk->sk_err) |
| 1007 | mask |= POLLERR; |
| 1008 | /* for now - to be enhanced in follow-on patch */ |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | return mask; |
| 1012 | } |
| 1013 | |
| 1014 | static int smc_shutdown(struct socket *sock, int how) |
| 1015 | { |
| 1016 | struct sock *sk = sock->sk; |
| 1017 | struct smc_sock *smc; |
| 1018 | int rc = -EINVAL; |
| 1019 | |
| 1020 | smc = smc_sk(sk); |
| 1021 | |
| 1022 | if ((how < SHUT_RD) || (how > SHUT_RDWR)) |
| 1023 | goto out_err; |
| 1024 | |
| 1025 | lock_sock(sk); |
| 1026 | |
| 1027 | rc = -ENOTCONN; |
| 1028 | if (sk->sk_state == SMC_CLOSED) |
| 1029 | goto out; |
| 1030 | if (smc->use_fallback) { |
| 1031 | rc = kernel_sock_shutdown(smc->clcsock, how); |
| 1032 | sk->sk_shutdown = smc->clcsock->sk->sk_shutdown; |
| 1033 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
| 1034 | sk->sk_state = SMC_CLOSED; |
| 1035 | } else { |
| 1036 | rc = sock_no_shutdown(sock, how); |
| 1037 | } |
| 1038 | |
| 1039 | out: |
| 1040 | release_sock(sk); |
| 1041 | |
| 1042 | out_err: |
| 1043 | return rc; |
| 1044 | } |
| 1045 | |
| 1046 | static int smc_setsockopt(struct socket *sock, int level, int optname, |
| 1047 | char __user *optval, unsigned int optlen) |
| 1048 | { |
| 1049 | struct sock *sk = sock->sk; |
| 1050 | struct smc_sock *smc; |
| 1051 | |
| 1052 | smc = smc_sk(sk); |
| 1053 | |
| 1054 | /* generic setsockopts reaching us here always apply to the |
| 1055 | * CLC socket |
| 1056 | */ |
| 1057 | return smc->clcsock->ops->setsockopt(smc->clcsock, level, optname, |
| 1058 | optval, optlen); |
| 1059 | } |
| 1060 | |
| 1061 | static int smc_getsockopt(struct socket *sock, int level, int optname, |
| 1062 | char __user *optval, int __user *optlen) |
| 1063 | { |
| 1064 | struct smc_sock *smc; |
| 1065 | |
| 1066 | smc = smc_sk(sock->sk); |
| 1067 | /* socket options apply to the CLC socket */ |
| 1068 | return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname, |
| 1069 | optval, optlen); |
| 1070 | } |
| 1071 | |
| 1072 | static int smc_ioctl(struct socket *sock, unsigned int cmd, |
| 1073 | unsigned long arg) |
| 1074 | { |
| 1075 | struct smc_sock *smc; |
| 1076 | |
| 1077 | smc = smc_sk(sock->sk); |
| 1078 | if (smc->use_fallback) |
| 1079 | return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg); |
| 1080 | else |
| 1081 | return sock_no_ioctl(sock, cmd, arg); |
| 1082 | } |
| 1083 | |
| 1084 | static ssize_t smc_sendpage(struct socket *sock, struct page *page, |
| 1085 | int offset, size_t size, int flags) |
| 1086 | { |
| 1087 | struct sock *sk = sock->sk; |
| 1088 | struct smc_sock *smc; |
| 1089 | int rc = -EPIPE; |
| 1090 | |
| 1091 | smc = smc_sk(sk); |
| 1092 | lock_sock(sk); |
| 1093 | if (sk->sk_state != SMC_ACTIVE) |
| 1094 | goto out; |
| 1095 | if (smc->use_fallback) |
| 1096 | rc = kernel_sendpage(smc->clcsock, page, offset, |
| 1097 | size, flags); |
| 1098 | else |
| 1099 | rc = sock_no_sendpage(sock, page, offset, size, flags); |
| 1100 | |
| 1101 | out: |
| 1102 | release_sock(sk); |
| 1103 | return rc; |
| 1104 | } |
| 1105 | |
| 1106 | static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos, |
| 1107 | struct pipe_inode_info *pipe, size_t len, |
| 1108 | unsigned int flags) |
| 1109 | { |
| 1110 | struct sock *sk = sock->sk; |
| 1111 | struct smc_sock *smc; |
| 1112 | int rc = -ENOTCONN; |
| 1113 | |
| 1114 | smc = smc_sk(sk); |
| 1115 | lock_sock(sk); |
| 1116 | if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED)) |
| 1117 | goto out; |
| 1118 | if (smc->use_fallback) { |
| 1119 | rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos, |
| 1120 | pipe, len, flags); |
| 1121 | } else { |
| 1122 | rc = -EOPNOTSUPP; |
| 1123 | } |
| 1124 | out: |
| 1125 | release_sock(sk); |
| 1126 | return rc; |
| 1127 | } |
| 1128 | |
| 1129 | /* must look like tcp */ |
| 1130 | static const struct proto_ops smc_sock_ops = { |
| 1131 | .family = PF_SMC, |
| 1132 | .owner = THIS_MODULE, |
| 1133 | .release = smc_release, |
| 1134 | .bind = smc_bind, |
| 1135 | .connect = smc_connect, |
| 1136 | .socketpair = sock_no_socketpair, |
| 1137 | .accept = smc_accept, |
| 1138 | .getname = smc_getname, |
| 1139 | .poll = smc_poll, |
| 1140 | .ioctl = smc_ioctl, |
| 1141 | .listen = smc_listen, |
| 1142 | .shutdown = smc_shutdown, |
| 1143 | .setsockopt = smc_setsockopt, |
| 1144 | .getsockopt = smc_getsockopt, |
| 1145 | .sendmsg = smc_sendmsg, |
| 1146 | .recvmsg = smc_recvmsg, |
| 1147 | .mmap = sock_no_mmap, |
| 1148 | .sendpage = smc_sendpage, |
| 1149 | .splice_read = smc_splice_read, |
| 1150 | }; |
| 1151 | |
| 1152 | static int smc_create(struct net *net, struct socket *sock, int protocol, |
| 1153 | int kern) |
| 1154 | { |
| 1155 | struct smc_sock *smc; |
| 1156 | struct sock *sk; |
| 1157 | int rc; |
| 1158 | |
| 1159 | rc = -ESOCKTNOSUPPORT; |
| 1160 | if (sock->type != SOCK_STREAM) |
| 1161 | goto out; |
| 1162 | |
| 1163 | rc = -EPROTONOSUPPORT; |
| 1164 | if ((protocol != IPPROTO_IP) && (protocol != IPPROTO_TCP)) |
| 1165 | goto out; |
| 1166 | |
| 1167 | rc = -ENOBUFS; |
| 1168 | sock->ops = &smc_sock_ops; |
| 1169 | sk = smc_sock_alloc(net, sock); |
| 1170 | if (!sk) |
| 1171 | goto out; |
| 1172 | |
| 1173 | /* create internal TCP socket for CLC handshake and fallback */ |
| 1174 | smc = smc_sk(sk); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1175 | smc->use_fallback = false; /* assume rdma capability first */ |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1176 | rc = sock_create_kern(net, PF_INET, SOCK_STREAM, |
| 1177 | IPPROTO_TCP, &smc->clcsock); |
| 1178 | if (rc) |
| 1179 | sk_common_release(sk); |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 1180 | smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE); |
| 1181 | smc->sk.sk_rcvbuf = max(smc->clcsock->sk->sk_rcvbuf, SMC_BUF_MIN_SIZE); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1182 | |
| 1183 | out: |
| 1184 | return rc; |
| 1185 | } |
| 1186 | |
| 1187 | static const struct net_proto_family smc_sock_family_ops = { |
| 1188 | .family = PF_SMC, |
| 1189 | .owner = THIS_MODULE, |
| 1190 | .create = smc_create, |
| 1191 | }; |
| 1192 | |
| 1193 | static int __init smc_init(void) |
| 1194 | { |
| 1195 | int rc; |
| 1196 | |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 1197 | rc = smc_pnet_init(); |
| 1198 | if (rc) |
| 1199 | return rc; |
| 1200 | |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 1201 | rc = smc_llc_init(); |
| 1202 | if (rc) { |
| 1203 | pr_err("%s: smc_llc_init fails with %d\n", __func__, rc); |
| 1204 | goto out_pnet; |
| 1205 | } |
| 1206 | |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame^] | 1207 | rc = smc_cdc_init(); |
| 1208 | if (rc) { |
| 1209 | pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc); |
| 1210 | goto out_pnet; |
| 1211 | } |
| 1212 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1213 | rc = proto_register(&smc_proto, 1); |
| 1214 | if (rc) { |
| 1215 | pr_err("%s: proto_register fails with %d\n", __func__, rc); |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 1216 | goto out_pnet; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | rc = sock_register(&smc_sock_family_ops); |
| 1220 | if (rc) { |
| 1221 | pr_err("%s: sock_register fails with %d\n", __func__, rc); |
| 1222 | goto out_proto; |
| 1223 | } |
| 1224 | |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 1225 | rc = smc_ib_register_client(); |
| 1226 | if (rc) { |
| 1227 | pr_err("%s: ib_register fails with %d\n", __func__, rc); |
| 1228 | goto out_sock; |
| 1229 | } |
| 1230 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1231 | return 0; |
| 1232 | |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 1233 | out_sock: |
| 1234 | sock_unregister(PF_SMC); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1235 | out_proto: |
| 1236 | proto_unregister(&smc_proto); |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 1237 | out_pnet: |
| 1238 | smc_pnet_exit(); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1239 | return rc; |
| 1240 | } |
| 1241 | |
| 1242 | static void __exit smc_exit(void) |
| 1243 | { |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 1244 | struct smc_link_group *lgr, *lg; |
| 1245 | LIST_HEAD(lgr_freeing_list); |
| 1246 | |
| 1247 | spin_lock_bh(&smc_lgr_list.lock); |
| 1248 | if (!list_empty(&smc_lgr_list.list)) |
| 1249 | list_splice_init(&smc_lgr_list.list, &lgr_freeing_list); |
| 1250 | spin_unlock_bh(&smc_lgr_list.lock); |
| 1251 | list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) { |
| 1252 | list_del_init(&lgr->list); |
| 1253 | smc_lgr_free(lgr); /* free link group */ |
| 1254 | } |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 1255 | smc_ib_unregister_client(); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1256 | sock_unregister(PF_SMC); |
| 1257 | proto_unregister(&smc_proto); |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 1258 | smc_pnet_exit(); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | module_init(smc_init); |
| 1262 | module_exit(smc_exit); |
| 1263 | |
| 1264 | MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>"); |
| 1265 | MODULE_DESCRIPTION("smc socket address family"); |
| 1266 | MODULE_LICENSE("GPL"); |
| 1267 | MODULE_ALIAS_NETPROTO(PF_SMC); |