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: |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 10 | * - support for alternate links postponed |
| 11 | * - partial support for non-blocking sockets only |
| 12 | * - support for urgent data postponed |
| 13 | * |
Karsten Graul | aaa4d33 | 2018-03-16 15:06:41 +0100 | [diff] [blame] | 14 | * Copyright IBM Corp. 2016, 2018 |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 15 | * |
| 16 | * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com> |
| 17 | * based on prototype from Frank Blaschka |
| 18 | */ |
| 19 | |
| 20 | #define KMSG_COMPONENT "smc" |
| 21 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/socket.h> |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 25 | #include <linux/workqueue.h> |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 26 | #include <linux/in.h> |
Ingo Molnar | c3edc40 | 2017-02-02 08:35:14 +0100 | [diff] [blame] | 27 | #include <linux/sched/signal.h> |
| 28 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 29 | #include <net/sock.h> |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 30 | #include <net/tcp.h> |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 31 | #include <net/smc.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 | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 40 | #include "smc_tx.h" |
Ursula Braun | 952310c | 2017-01-09 16:55:24 +0100 | [diff] [blame] | 41 | #include "smc_rx.h" |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 42 | #include "smc_close.h" |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 43 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 44 | static DEFINE_MUTEX(smc_create_lgr_pending); /* serialize link group |
| 45 | * creation |
| 46 | */ |
| 47 | |
| 48 | struct smc_lgr_list smc_lgr_list = { /* established link groups */ |
| 49 | .lock = __SPIN_LOCK_UNLOCKED(smc_lgr_list.lock), |
| 50 | .list = LIST_HEAD_INIT(smc_lgr_list.list), |
| 51 | }; |
| 52 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 53 | static void smc_tcp_listen_work(struct work_struct *); |
| 54 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 55 | static void smc_set_keepalive(struct sock *sk, int val) |
| 56 | { |
| 57 | struct smc_sock *smc = smc_sk(sk); |
| 58 | |
| 59 | smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val); |
| 60 | } |
| 61 | |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 62 | static struct smc_hashinfo smc_v4_hashinfo = { |
| 63 | .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock), |
| 64 | }; |
| 65 | |
Karsten Graul | aaa4d33 | 2018-03-16 15:06:41 +0100 | [diff] [blame] | 66 | static struct smc_hashinfo smc_v6_hashinfo = { |
| 67 | .lock = __RW_LOCK_UNLOCKED(smc_v6_hashinfo.lock), |
| 68 | }; |
| 69 | |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 70 | int smc_hash_sk(struct sock *sk) |
| 71 | { |
| 72 | struct smc_hashinfo *h = sk->sk_prot->h.smc_hash; |
| 73 | struct hlist_head *head; |
| 74 | |
| 75 | head = &h->ht; |
| 76 | |
| 77 | write_lock_bh(&h->lock); |
| 78 | sk_add_node(sk, head); |
| 79 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); |
| 80 | write_unlock_bh(&h->lock); |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | EXPORT_SYMBOL_GPL(smc_hash_sk); |
| 85 | |
| 86 | void smc_unhash_sk(struct sock *sk) |
| 87 | { |
| 88 | struct smc_hashinfo *h = sk->sk_prot->h.smc_hash; |
| 89 | |
| 90 | write_lock_bh(&h->lock); |
| 91 | if (sk_del_node_init(sk)) |
| 92 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); |
| 93 | write_unlock_bh(&h->lock); |
| 94 | } |
| 95 | EXPORT_SYMBOL_GPL(smc_unhash_sk); |
| 96 | |
| 97 | struct proto smc_proto = { |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 98 | .name = "SMC", |
| 99 | .owner = THIS_MODULE, |
| 100 | .keepalive = smc_set_keepalive, |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 101 | .hash = smc_hash_sk, |
| 102 | .unhash = smc_unhash_sk, |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 103 | .obj_size = sizeof(struct smc_sock), |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 104 | .h.smc_hash = &smc_v4_hashinfo, |
Paul E. McKenney | 5f0d5a3 | 2017-01-18 02:53:44 -0800 | [diff] [blame] | 105 | .slab_flags = SLAB_TYPESAFE_BY_RCU, |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 106 | }; |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 107 | EXPORT_SYMBOL_GPL(smc_proto); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 108 | |
Karsten Graul | aaa4d33 | 2018-03-16 15:06:41 +0100 | [diff] [blame] | 109 | struct proto smc_proto6 = { |
| 110 | .name = "SMC6", |
| 111 | .owner = THIS_MODULE, |
| 112 | .keepalive = smc_set_keepalive, |
| 113 | .hash = smc_hash_sk, |
| 114 | .unhash = smc_unhash_sk, |
| 115 | .obj_size = sizeof(struct smc_sock), |
| 116 | .h.smc_hash = &smc_v6_hashinfo, |
| 117 | .slab_flags = SLAB_TYPESAFE_BY_RCU, |
| 118 | }; |
| 119 | EXPORT_SYMBOL_GPL(smc_proto6); |
| 120 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 121 | static int smc_release(struct socket *sock) |
| 122 | { |
| 123 | struct sock *sk = sock->sk; |
| 124 | struct smc_sock *smc; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 125 | int rc = 0; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 126 | |
| 127 | if (!sk) |
| 128 | goto out; |
| 129 | |
| 130 | smc = smc_sk(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 131 | if (sk->sk_state == SMC_LISTEN) |
| 132 | /* smc_close_non_accepted() is called and acquires |
| 133 | * sock lock for child sockets again |
| 134 | */ |
| 135 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
| 136 | else |
| 137 | lock_sock(sk); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 138 | |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 139 | if (!smc->use_fallback) { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 140 | rc = smc_close_active(smc); |
| 141 | sock_set_flag(sk, SOCK_DEAD); |
| 142 | sk->sk_shutdown |= SHUTDOWN_MASK; |
| 143 | } |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 144 | if (smc->clcsock) { |
| 145 | sock_release(smc->clcsock); |
| 146 | smc->clcsock = NULL; |
| 147 | } |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 148 | if (smc->use_fallback) { |
| 149 | sock_put(sk); /* passive closing */ |
| 150 | sk->sk_state = SMC_CLOSED; |
| 151 | sk->sk_state_change(sk); |
| 152 | } |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 153 | |
| 154 | /* detach socket */ |
| 155 | sock_orphan(sk); |
| 156 | sock->sk = NULL; |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 157 | if (!smc->use_fallback && sk->sk_state == SMC_CLOSED) |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 158 | smc_conn_free(&smc->conn); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 159 | release_sock(sk); |
| 160 | |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 161 | sk->sk_prot->unhash(sk); |
| 162 | sock_put(sk); /* final sock_put */ |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 163 | out: |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 164 | return rc; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static void smc_destruct(struct sock *sk) |
| 168 | { |
| 169 | if (sk->sk_state != SMC_CLOSED) |
| 170 | return; |
| 171 | if (!sock_flag(sk, SOCK_DEAD)) |
| 172 | return; |
| 173 | |
| 174 | sk_refcnt_debug_dec(sk); |
| 175 | } |
| 176 | |
Karsten Graul | aaa4d33 | 2018-03-16 15:06:41 +0100 | [diff] [blame] | 177 | static struct sock *smc_sock_alloc(struct net *net, struct socket *sock, |
| 178 | int protocol) |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 179 | { |
| 180 | struct smc_sock *smc; |
Karsten Graul | aaa4d33 | 2018-03-16 15:06:41 +0100 | [diff] [blame] | 181 | struct proto *prot; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 182 | struct sock *sk; |
| 183 | |
Karsten Graul | aaa4d33 | 2018-03-16 15:06:41 +0100 | [diff] [blame] | 184 | prot = (protocol == SMCPROTO_SMC6) ? &smc_proto6 : &smc_proto; |
| 185 | sk = sk_alloc(net, PF_SMC, GFP_KERNEL, prot, 0); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 186 | if (!sk) |
| 187 | return NULL; |
| 188 | |
| 189 | sock_init_data(sock, sk); /* sets sk_refcnt to 1 */ |
| 190 | sk->sk_state = SMC_INIT; |
| 191 | sk->sk_destruct = smc_destruct; |
Karsten Graul | aaa4d33 | 2018-03-16 15:06:41 +0100 | [diff] [blame] | 192 | sk->sk_protocol = protocol; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 193 | smc = smc_sk(sk); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 194 | INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work); |
| 195 | INIT_LIST_HEAD(&smc->accept_q); |
| 196 | spin_lock_init(&smc->accept_q_lock); |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 197 | sk->sk_prot->hash(sk); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 198 | sk_refcnt_debug_inc(sk); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 199 | |
| 200 | return sk; |
| 201 | } |
| 202 | |
| 203 | static int smc_bind(struct socket *sock, struct sockaddr *uaddr, |
| 204 | int addr_len) |
| 205 | { |
| 206 | struct sockaddr_in *addr = (struct sockaddr_in *)uaddr; |
| 207 | struct sock *sk = sock->sk; |
| 208 | struct smc_sock *smc; |
| 209 | int rc; |
| 210 | |
| 211 | smc = smc_sk(sk); |
| 212 | |
| 213 | /* replicate tests from inet_bind(), to be safe wrt. future changes */ |
| 214 | rc = -EINVAL; |
| 215 | if (addr_len < sizeof(struct sockaddr_in)) |
| 216 | goto out; |
| 217 | |
| 218 | rc = -EAFNOSUPPORT; |
Karsten Graul | aaa4d33 | 2018-03-16 15:06:41 +0100 | [diff] [blame] | 219 | if (addr->sin_family != AF_INET && |
| 220 | addr->sin_family != AF_INET6 && |
| 221 | addr->sin_family != AF_UNSPEC) |
| 222 | goto out; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 223 | /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */ |
Karsten Graul | aaa4d33 | 2018-03-16 15:06:41 +0100 | [diff] [blame] | 224 | if (addr->sin_family == AF_UNSPEC && |
| 225 | addr->sin_addr.s_addr != htonl(INADDR_ANY)) |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 226 | goto out; |
| 227 | |
| 228 | lock_sock(sk); |
| 229 | |
| 230 | /* Check if socket is already active */ |
| 231 | rc = -EINVAL; |
| 232 | if (sk->sk_state != SMC_INIT) |
| 233 | goto out_rel; |
| 234 | |
| 235 | smc->clcsock->sk->sk_reuse = sk->sk_reuse; |
| 236 | rc = kernel_bind(smc->clcsock, uaddr, addr_len); |
| 237 | |
| 238 | out_rel: |
| 239 | release_sock(sk); |
| 240 | out: |
| 241 | return rc; |
| 242 | } |
| 243 | |
| 244 | static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk, |
| 245 | unsigned long mask) |
| 246 | { |
| 247 | /* options we don't get control via setsockopt for */ |
| 248 | nsk->sk_type = osk->sk_type; |
| 249 | nsk->sk_sndbuf = osk->sk_sndbuf; |
| 250 | nsk->sk_rcvbuf = osk->sk_rcvbuf; |
| 251 | nsk->sk_sndtimeo = osk->sk_sndtimeo; |
| 252 | nsk->sk_rcvtimeo = osk->sk_rcvtimeo; |
| 253 | nsk->sk_mark = osk->sk_mark; |
| 254 | nsk->sk_priority = osk->sk_priority; |
| 255 | nsk->sk_rcvlowat = osk->sk_rcvlowat; |
| 256 | nsk->sk_bound_dev_if = osk->sk_bound_dev_if; |
| 257 | nsk->sk_err = osk->sk_err; |
| 258 | |
| 259 | nsk->sk_flags &= ~mask; |
| 260 | nsk->sk_flags |= osk->sk_flags & mask; |
| 261 | } |
| 262 | |
| 263 | #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \ |
| 264 | (1UL << SOCK_KEEPOPEN) | \ |
| 265 | (1UL << SOCK_LINGER) | \ |
| 266 | (1UL << SOCK_BROADCAST) | \ |
| 267 | (1UL << SOCK_TIMESTAMP) | \ |
| 268 | (1UL << SOCK_DBG) | \ |
| 269 | (1UL << SOCK_RCVTSTAMP) | \ |
| 270 | (1UL << SOCK_RCVTSTAMPNS) | \ |
| 271 | (1UL << SOCK_LOCALROUTE) | \ |
| 272 | (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \ |
| 273 | (1UL << SOCK_RXQ_OVFL) | \ |
| 274 | (1UL << SOCK_WIFI_STATUS) | \ |
| 275 | (1UL << SOCK_NOFCS) | \ |
| 276 | (1UL << SOCK_FILTER_LOCKED)) |
| 277 | /* copy only relevant settings and flags of SOL_SOCKET level from smc to |
| 278 | * clc socket (since smc is not called for these options from net/core) |
| 279 | */ |
| 280 | static void smc_copy_sock_settings_to_clc(struct smc_sock *smc) |
| 281 | { |
| 282 | smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC); |
| 283 | } |
| 284 | |
| 285 | #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \ |
| 286 | (1UL << SOCK_KEEPOPEN) | \ |
| 287 | (1UL << SOCK_LINGER) | \ |
| 288 | (1UL << SOCK_DBG)) |
| 289 | /* copy only settings and flags relevant for smc from clc to smc socket */ |
| 290 | static void smc_copy_sock_settings_to_smc(struct smc_sock *smc) |
| 291 | { |
| 292 | smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC); |
| 293 | } |
| 294 | |
Stefan Raspl | 0f62712 | 2018-03-01 13:51:26 +0100 | [diff] [blame] | 295 | static int smc_clnt_conf_first_link(struct smc_sock *smc) |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 296 | { |
| 297 | struct smc_link_group *lgr = smc->conn.lgr; |
| 298 | struct smc_link *link; |
| 299 | int rest; |
| 300 | int rc; |
| 301 | |
| 302 | link = &lgr->lnk[SMC_SINGLE_LINK]; |
| 303 | /* receive CONFIRM LINK request from server over RoCE fabric */ |
| 304 | rest = wait_for_completion_interruptible_timeout( |
| 305 | &link->llc_confirm, |
| 306 | SMC_LLC_WAIT_FIRST_TIME); |
| 307 | if (rest <= 0) { |
| 308 | struct smc_clc_msg_decline dclc; |
| 309 | |
| 310 | rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc), |
| 311 | SMC_CLC_DECLINE); |
| 312 | return rc; |
| 313 | } |
| 314 | |
Karsten Graul | 75d320d | 2018-03-01 13:51:31 +0100 | [diff] [blame] | 315 | if (link->llc_confirm_rc) |
| 316 | return SMC_CLC_DECL_RMBE_EC; |
| 317 | |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 318 | rc = smc_ib_modify_qp_rts(link); |
| 319 | if (rc) |
| 320 | return SMC_CLC_DECL_INTERR; |
| 321 | |
| 322 | smc_wr_remember_qp_attr(link); |
Ursula Braun | 652a1e4 | 2017-07-28 13:56:17 +0200 | [diff] [blame] | 323 | |
| 324 | rc = smc_wr_reg_send(link, |
| 325 | smc->conn.rmb_desc->mr_rx[SMC_SINGLE_LINK]); |
| 326 | if (rc) |
| 327 | return SMC_CLC_DECL_INTERR; |
| 328 | |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 329 | /* send CONFIRM LINK response over RoCE fabric */ |
| 330 | rc = smc_llc_send_confirm_link(link, |
| 331 | link->smcibdev->mac[link->ibport - 1], |
Stefan Raspl | 0f62712 | 2018-03-01 13:51:26 +0100 | [diff] [blame] | 332 | &link->smcibdev->gid[link->ibport - 1], |
| 333 | SMC_LLC_RESP); |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 334 | if (rc < 0) |
| 335 | return SMC_CLC_DECL_TCL; |
| 336 | |
Karsten Graul | 52bedf3 | 2018-03-01 13:51:32 +0100 | [diff] [blame] | 337 | /* receive ADD LINK request from server over RoCE fabric */ |
| 338 | rest = wait_for_completion_interruptible_timeout(&link->llc_add, |
| 339 | SMC_LLC_WAIT_TIME); |
| 340 | if (rest <= 0) { |
| 341 | struct smc_clc_msg_decline dclc; |
| 342 | |
| 343 | rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc), |
| 344 | SMC_CLC_DECLINE); |
| 345 | return rc; |
| 346 | } |
| 347 | |
| 348 | /* send add link reject message, only one link supported for now */ |
| 349 | rc = smc_llc_send_add_link(link, |
| 350 | link->smcibdev->mac[link->ibport - 1], |
| 351 | &link->smcibdev->gid[link->ibport - 1], |
| 352 | SMC_LLC_RESP); |
| 353 | if (rc < 0) |
| 354 | return SMC_CLC_DECL_TCL; |
| 355 | |
| 356 | link->state = SMC_LNK_ACTIVE; |
| 357 | |
Karsten Graul | 75d320d | 2018-03-01 13:51:31 +0100 | [diff] [blame] | 358 | return 0; |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 359 | } |
| 360 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 361 | static void smc_conn_save_peer_info(struct smc_sock *smc, |
| 362 | struct smc_clc_msg_accept_confirm *clc) |
| 363 | { |
| 364 | smc->conn.peer_conn_idx = clc->conn_idx; |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 365 | smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token); |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 366 | smc->conn.peer_rmbe_size = smc_uncompress_bufsize(clc->rmbe_size); |
| 367 | atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static void smc_link_save_peer_info(struct smc_link *link, |
| 371 | struct smc_clc_msg_accept_confirm *clc) |
| 372 | { |
| 373 | link->peer_qpn = ntoh24(clc->qpn); |
| 374 | memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE); |
| 375 | memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac)); |
| 376 | link->peer_psn = ntoh24(clc->psn); |
| 377 | link->peer_mtu = clc->qp_mtu; |
| 378 | } |
| 379 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 380 | /* setup for RDMA connection of client */ |
| 381 | static int smc_connect_rdma(struct smc_sock *smc) |
| 382 | { |
| 383 | struct smc_clc_msg_accept_confirm aclc; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 384 | int local_contact = SMC_FIRST_CONTACT; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 385 | struct smc_ib_device *smcibdev; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 386 | struct smc_link *link; |
| 387 | u8 srv_first_contact; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 388 | int reason_code = 0; |
| 389 | int rc = 0; |
| 390 | u8 ibport; |
| 391 | |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 392 | sock_hold(&smc->sk); /* sock put in passive closing */ |
| 393 | |
Ursula Braun | ee9dfbe | 2018-04-26 17:18:21 +0200 | [diff] [blame] | 394 | if (smc->use_fallback) |
| 395 | goto out_connected; |
| 396 | |
Ursula Braun | c5c1cc9 | 2017-10-25 11:01:46 +0200 | [diff] [blame] | 397 | if (!tcp_sk(smc->clcsock->sk)->syn_smc) { |
| 398 | /* peer has not signalled SMC-capability */ |
| 399 | smc->use_fallback = true; |
| 400 | goto out_connected; |
| 401 | } |
| 402 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 403 | /* IPSec connections opt out of SMC-R optimizations */ |
| 404 | if (using_ipsec(smc)) { |
| 405 | reason_code = SMC_CLC_DECL_IPSEC; |
| 406 | goto decline_rdma; |
| 407 | } |
| 408 | |
| 409 | /* PNET table look up: search active ib_device and port |
| 410 | * within same PNETID that also contains the ethernet device |
| 411 | * used for the internal TCP socket |
| 412 | */ |
| 413 | smc_pnet_find_roce_resource(smc->clcsock->sk, &smcibdev, &ibport); |
| 414 | if (!smcibdev) { |
| 415 | reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */ |
| 416 | goto decline_rdma; |
| 417 | } |
| 418 | |
| 419 | /* do inband token exchange */ |
| 420 | reason_code = smc_clc_send_proposal(smc, smcibdev, ibport); |
| 421 | if (reason_code < 0) { |
| 422 | rc = reason_code; |
| 423 | goto out_err; |
| 424 | } |
| 425 | if (reason_code > 0) /* configuration error */ |
| 426 | goto decline_rdma; |
| 427 | /* receive SMC Accept CLC message */ |
| 428 | reason_code = smc_clc_wait_msg(smc, &aclc, sizeof(aclc), |
| 429 | SMC_CLC_ACCEPT); |
| 430 | if (reason_code < 0) { |
| 431 | rc = reason_code; |
| 432 | goto out_err; |
| 433 | } |
| 434 | if (reason_code > 0) |
| 435 | goto decline_rdma; |
| 436 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 437 | srv_first_contact = aclc.hdr.flag; |
| 438 | mutex_lock(&smc_create_lgr_pending); |
Karsten Graul | be6d467 | 2018-03-01 13:51:28 +0100 | [diff] [blame] | 439 | local_contact = smc_conn_create(smc, smcibdev, ibport, &aclc.lcl, |
| 440 | srv_first_contact); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 441 | if (local_contact < 0) { |
| 442 | rc = local_contact; |
| 443 | if (rc == -ENOMEM) |
| 444 | reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/ |
| 445 | else if (rc == -ENOLINK) |
| 446 | reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */ |
| 447 | goto decline_rdma_unlock; |
| 448 | } |
| 449 | link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK]; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 450 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 451 | smc_conn_save_peer_info(smc, &aclc); |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 452 | |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 453 | /* create send buffer and rmb */ |
| 454 | rc = smc_buf_create(smc); |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 455 | if (rc) { |
| 456 | reason_code = SMC_CLC_DECL_MEM; |
| 457 | goto decline_rdma_unlock; |
| 458 | } |
| 459 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 460 | if (local_contact == SMC_FIRST_CONTACT) |
| 461 | smc_link_save_peer_info(link, &aclc); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 462 | |
| 463 | rc = smc_rmb_rtoken_handling(&smc->conn, &aclc); |
| 464 | if (rc) { |
| 465 | reason_code = SMC_CLC_DECL_INTERR; |
| 466 | goto decline_rdma_unlock; |
| 467 | } |
| 468 | |
Ursula Braun | 46c28db | 2017-04-10 14:58:01 +0200 | [diff] [blame] | 469 | smc_close_init(smc); |
| 470 | smc_rx_init(smc); |
| 471 | |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 472 | if (local_contact == SMC_FIRST_CONTACT) { |
| 473 | rc = smc_ib_ready_link(link); |
| 474 | if (rc) { |
| 475 | reason_code = SMC_CLC_DECL_INTERR; |
| 476 | goto decline_rdma_unlock; |
| 477 | } |
Ursula Braun | 652a1e4 | 2017-07-28 13:56:17 +0200 | [diff] [blame] | 478 | } else { |
| 479 | struct smc_buf_desc *buf_desc = smc->conn.rmb_desc; |
| 480 | |
| 481 | if (!buf_desc->reused) { |
| 482 | /* register memory region for new rmb */ |
| 483 | rc = smc_wr_reg_send(link, |
| 484 | buf_desc->mr_rx[SMC_SINGLE_LINK]); |
| 485 | if (rc) { |
| 486 | reason_code = SMC_CLC_DECL_INTERR; |
| 487 | goto decline_rdma_unlock; |
| 488 | } |
| 489 | } |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 490 | } |
Ursula Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 491 | smc_rmb_sync_sg_for_device(&smc->conn); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 492 | |
| 493 | rc = smc_clc_send_confirm(smc); |
| 494 | if (rc) |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 495 | goto out_err_unlock; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 496 | |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 497 | if (local_contact == SMC_FIRST_CONTACT) { |
| 498 | /* QP confirmation over RoCE fabric */ |
Stefan Raspl | 0f62712 | 2018-03-01 13:51:26 +0100 | [diff] [blame] | 499 | reason_code = smc_clnt_conf_first_link(smc); |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 500 | if (reason_code < 0) { |
| 501 | rc = reason_code; |
| 502 | goto out_err_unlock; |
| 503 | } |
| 504 | if (reason_code > 0) |
| 505 | goto decline_rdma_unlock; |
| 506 | } |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 507 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 508 | mutex_unlock(&smc_create_lgr_pending); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 509 | smc_tx_init(smc); |
| 510 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 511 | out_connected: |
| 512 | smc_copy_sock_settings_to_clc(smc); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 513 | if (smc->sk.sk_state == SMC_INIT) |
| 514 | smc->sk.sk_state = SMC_ACTIVE; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 515 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 516 | return rc ? rc : local_contact; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 517 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 518 | decline_rdma_unlock: |
Ursula Braun | 610db66 | 2018-01-25 11:15:34 +0100 | [diff] [blame] | 519 | if (local_contact == SMC_FIRST_CONTACT) |
| 520 | smc_lgr_forget(smc->conn.lgr); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 521 | mutex_unlock(&smc_create_lgr_pending); |
| 522 | smc_conn_free(&smc->conn); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 523 | decline_rdma: |
| 524 | /* RDMA setup failed, switch back to TCP */ |
| 525 | smc->use_fallback = true; |
| 526 | if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) { |
Ursula Braun | bfbedfd | 2017-09-21 09:16:32 +0200 | [diff] [blame] | 527 | rc = smc_clc_send_decline(smc, reason_code); |
Ursula Braun | 0c9f151 | 2017-12-07 13:38:45 +0100 | [diff] [blame] | 528 | if (rc < 0) |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 529 | goto out_err; |
| 530 | } |
| 531 | goto out_connected; |
| 532 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 533 | out_err_unlock: |
Ursula Braun | 610db66 | 2018-01-25 11:15:34 +0100 | [diff] [blame] | 534 | if (local_contact == SMC_FIRST_CONTACT) |
| 535 | smc_lgr_forget(smc->conn.lgr); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 536 | mutex_unlock(&smc_create_lgr_pending); |
| 537 | smc_conn_free(&smc->conn); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 538 | out_err: |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 539 | if (smc->sk.sk_state == SMC_INIT) |
| 540 | sock_put(&smc->sk); /* passive closing */ |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 541 | return rc; |
| 542 | } |
| 543 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 544 | static int smc_connect(struct socket *sock, struct sockaddr *addr, |
| 545 | int alen, int flags) |
| 546 | { |
| 547 | struct sock *sk = sock->sk; |
| 548 | struct smc_sock *smc; |
| 549 | int rc = -EINVAL; |
| 550 | |
| 551 | smc = smc_sk(sk); |
| 552 | |
| 553 | /* separate smc parameter checking to be safe */ |
| 554 | if (alen < sizeof(addr->sa_family)) |
| 555 | goto out_err; |
Karsten Graul | aaa4d33 | 2018-03-16 15:06:41 +0100 | [diff] [blame] | 556 | if (addr->sa_family != AF_INET && addr->sa_family != AF_INET6) |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 557 | goto out_err; |
| 558 | |
| 559 | lock_sock(sk); |
| 560 | switch (sk->sk_state) { |
| 561 | default: |
| 562 | goto out; |
| 563 | case SMC_ACTIVE: |
| 564 | rc = -EISCONN; |
| 565 | goto out; |
| 566 | case SMC_INIT: |
| 567 | rc = 0; |
| 568 | break; |
| 569 | } |
| 570 | |
| 571 | smc_copy_sock_settings_to_clc(smc); |
Ursula Braun | c5c1cc9 | 2017-10-25 11:01:46 +0200 | [diff] [blame] | 572 | tcp_sk(smc->clcsock->sk)->syn_smc = 1; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 573 | rc = kernel_connect(smc->clcsock, addr, alen, flags); |
| 574 | if (rc) |
| 575 | goto out; |
| 576 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 577 | /* setup RDMA connection */ |
| 578 | rc = smc_connect_rdma(smc); |
| 579 | if (rc < 0) |
| 580 | goto out; |
| 581 | else |
| 582 | rc = 0; /* success cases including fallback */ |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 583 | |
| 584 | out: |
| 585 | release_sock(sk); |
| 586 | out_err: |
| 587 | return rc; |
| 588 | } |
| 589 | |
| 590 | static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc) |
| 591 | { |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 592 | struct socket *new_clcsock = NULL; |
| 593 | struct sock *lsk = &lsmc->sk; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 594 | struct sock *new_sk; |
| 595 | int rc; |
| 596 | |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 597 | release_sock(lsk); |
Karsten Graul | aaa4d33 | 2018-03-16 15:06:41 +0100 | [diff] [blame] | 598 | new_sk = smc_sock_alloc(sock_net(lsk), NULL, lsk->sk_protocol); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 599 | if (!new_sk) { |
| 600 | rc = -ENOMEM; |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 601 | lsk->sk_err = ENOMEM; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 602 | *new_smc = NULL; |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 603 | lock_sock(lsk); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 604 | goto out; |
| 605 | } |
| 606 | *new_smc = smc_sk(new_sk); |
| 607 | |
| 608 | rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0); |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 609 | lock_sock(lsk); |
Ursula Braun | 35a6b17 | 2018-01-24 10:28:13 +0100 | [diff] [blame] | 610 | if (rc < 0) |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 611 | lsk->sk_err = -rc; |
Ursula Braun | 35a6b17 | 2018-01-24 10:28:13 +0100 | [diff] [blame] | 612 | if (rc < 0 || lsk->sk_state == SMC_CLOSED) { |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 613 | if (new_clcsock) |
| 614 | sock_release(new_clcsock); |
| 615 | new_sk->sk_state = SMC_CLOSED; |
| 616 | sock_set_flag(new_sk, SOCK_DEAD); |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 617 | new_sk->sk_prot->unhash(new_sk); |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 618 | sock_put(new_sk); /* final */ |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 619 | *new_smc = NULL; |
| 620 | goto out; |
| 621 | } |
| 622 | |
| 623 | (*new_smc)->clcsock = new_clcsock; |
| 624 | out: |
| 625 | return rc; |
| 626 | } |
| 627 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 628 | /* add a just created sock to the accept queue of the listen sock as |
| 629 | * candidate for a following socket accept call from user space |
| 630 | */ |
| 631 | static void smc_accept_enqueue(struct sock *parent, struct sock *sk) |
| 632 | { |
| 633 | struct smc_sock *par = smc_sk(parent); |
| 634 | |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 635 | sock_hold(sk); /* sock_put in smc_accept_unlink () */ |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 636 | spin_lock(&par->accept_q_lock); |
| 637 | list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q); |
| 638 | spin_unlock(&par->accept_q_lock); |
| 639 | sk_acceptq_added(parent); |
| 640 | } |
| 641 | |
| 642 | /* remove a socket from the accept queue of its parental listening socket */ |
| 643 | static void smc_accept_unlink(struct sock *sk) |
| 644 | { |
| 645 | struct smc_sock *par = smc_sk(sk)->listen_smc; |
| 646 | |
| 647 | spin_lock(&par->accept_q_lock); |
| 648 | list_del_init(&smc_sk(sk)->accept_q); |
| 649 | spin_unlock(&par->accept_q_lock); |
| 650 | sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk); |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 651 | sock_put(sk); /* sock_hold in smc_accept_enqueue */ |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | /* remove a sock from the accept queue to bind it to a new socket created |
| 655 | * for a socket accept call from user space |
| 656 | */ |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 657 | struct sock *smc_accept_dequeue(struct sock *parent, |
| 658 | struct socket *new_sock) |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 659 | { |
| 660 | struct smc_sock *isk, *n; |
| 661 | struct sock *new_sk; |
| 662 | |
| 663 | list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) { |
| 664 | new_sk = (struct sock *)isk; |
| 665 | |
| 666 | smc_accept_unlink(new_sk); |
| 667 | if (new_sk->sk_state == SMC_CLOSED) { |
Ursula Braun | 127f497 | 2018-01-26 09:28:49 +0100 | [diff] [blame] | 668 | if (isk->clcsock) { |
| 669 | sock_release(isk->clcsock); |
| 670 | isk->clcsock = NULL; |
| 671 | } |
Ursula Braun | 288c8390 | 2017-04-10 14:58:04 +0200 | [diff] [blame] | 672 | new_sk->sk_prot->unhash(new_sk); |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 673 | sock_put(new_sk); /* final */ |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 674 | continue; |
| 675 | } |
| 676 | if (new_sock) |
| 677 | sock_graft(new_sk, new_sock); |
| 678 | return new_sk; |
| 679 | } |
| 680 | return NULL; |
| 681 | } |
| 682 | |
| 683 | /* clean up for a created but never accepted sock */ |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 684 | void smc_close_non_accepted(struct sock *sk) |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 685 | { |
| 686 | struct smc_sock *smc = smc_sk(sk); |
| 687 | |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 688 | lock_sock(sk); |
| 689 | if (!sk->sk_lingertime) |
| 690 | /* wait for peer closing */ |
| 691 | sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT; |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 692 | if (!smc->use_fallback) { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 693 | smc_close_active(smc); |
Ursula Braun | 288c8390 | 2017-04-10 14:58:04 +0200 | [diff] [blame] | 694 | sock_set_flag(sk, SOCK_DEAD); |
| 695 | sk->sk_shutdown |= SHUTDOWN_MASK; |
| 696 | } |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 697 | if (smc->clcsock) { |
| 698 | struct socket *tcp; |
| 699 | |
| 700 | tcp = smc->clcsock; |
| 701 | smc->clcsock = NULL; |
| 702 | sock_release(tcp); |
| 703 | } |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 704 | if (smc->use_fallback) { |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 705 | sock_put(sk); /* passive closing */ |
| 706 | sk->sk_state = SMC_CLOSED; |
| 707 | } else { |
| 708 | if (sk->sk_state == SMC_CLOSED) |
| 709 | smc_conn_free(&smc->conn); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 710 | } |
| 711 | release_sock(sk); |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 712 | sk->sk_prot->unhash(sk); |
| 713 | sock_put(sk); /* final sock_put */ |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 714 | } |
| 715 | |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 716 | static int smc_serv_conf_first_link(struct smc_sock *smc) |
| 717 | { |
| 718 | struct smc_link_group *lgr = smc->conn.lgr; |
| 719 | struct smc_link *link; |
| 720 | int rest; |
| 721 | int rc; |
| 722 | |
| 723 | link = &lgr->lnk[SMC_SINGLE_LINK]; |
Ursula Braun | 652a1e4 | 2017-07-28 13:56:17 +0200 | [diff] [blame] | 724 | |
| 725 | rc = smc_wr_reg_send(link, |
| 726 | smc->conn.rmb_desc->mr_rx[SMC_SINGLE_LINK]); |
| 727 | if (rc) |
| 728 | return SMC_CLC_DECL_INTERR; |
| 729 | |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 730 | /* send CONFIRM LINK request to client over the RoCE fabric */ |
| 731 | rc = smc_llc_send_confirm_link(link, |
| 732 | link->smcibdev->mac[link->ibport - 1], |
| 733 | &link->smcibdev->gid[link->ibport - 1], |
| 734 | SMC_LLC_REQ); |
| 735 | if (rc < 0) |
| 736 | return SMC_CLC_DECL_TCL; |
| 737 | |
| 738 | /* receive CONFIRM LINK response from client over the RoCE fabric */ |
| 739 | rest = wait_for_completion_interruptible_timeout( |
| 740 | &link->llc_confirm_resp, |
| 741 | SMC_LLC_WAIT_FIRST_TIME); |
| 742 | if (rest <= 0) { |
| 743 | struct smc_clc_msg_decline dclc; |
| 744 | |
| 745 | rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc), |
| 746 | SMC_CLC_DECLINE); |
Karsten Graul | 75d320d | 2018-03-01 13:51:31 +0100 | [diff] [blame] | 747 | return rc; |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 748 | } |
| 749 | |
Karsten Graul | 75d320d | 2018-03-01 13:51:31 +0100 | [diff] [blame] | 750 | if (link->llc_confirm_resp_rc) |
| 751 | return SMC_CLC_DECL_RMBE_EC; |
| 752 | |
Karsten Graul | 52bedf3 | 2018-03-01 13:51:32 +0100 | [diff] [blame] | 753 | /* send ADD LINK request to client over the RoCE fabric */ |
| 754 | rc = smc_llc_send_add_link(link, |
| 755 | link->smcibdev->mac[link->ibport - 1], |
| 756 | &link->smcibdev->gid[link->ibport - 1], |
| 757 | SMC_LLC_REQ); |
| 758 | if (rc < 0) |
| 759 | return SMC_CLC_DECL_TCL; |
| 760 | |
| 761 | /* receive ADD LINK response from client over the RoCE fabric */ |
| 762 | rest = wait_for_completion_interruptible_timeout(&link->llc_add_resp, |
| 763 | SMC_LLC_WAIT_TIME); |
| 764 | if (rest <= 0) { |
| 765 | struct smc_clc_msg_decline dclc; |
| 766 | |
| 767 | rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc), |
| 768 | SMC_CLC_DECLINE); |
| 769 | return rc; |
| 770 | } |
| 771 | |
| 772 | link->state = SMC_LNK_ACTIVE; |
| 773 | |
Karsten Graul | 75d320d | 2018-03-01 13:51:31 +0100 | [diff] [blame] | 774 | return 0; |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 775 | } |
| 776 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 777 | /* setup for RDMA connection of server */ |
| 778 | static void smc_listen_work(struct work_struct *work) |
| 779 | { |
| 780 | struct smc_sock *new_smc = container_of(work, struct smc_sock, |
| 781 | smc_listen_work); |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 782 | struct smc_clc_msg_proposal_prefix *pclc_prfx; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 783 | struct socket *newclcsock = new_smc->clcsock; |
| 784 | struct smc_sock *lsmc = new_smc->listen_smc; |
| 785 | struct smc_clc_msg_accept_confirm cclc; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 786 | int local_contact = SMC_REUSE_CONTACT; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 787 | struct sock *newsmcsk = &new_smc->sk; |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 788 | struct smc_clc_msg_proposal *pclc; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 789 | struct smc_ib_device *smcibdev; |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 790 | u8 buf[SMC_CLC_MAX_LEN]; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 791 | struct smc_link *link; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 792 | int reason_code = 0; |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 793 | int rc = 0; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 794 | u8 ibport; |
| 795 | |
Ursula Braun | ee9dfbe | 2018-04-26 17:18:21 +0200 | [diff] [blame] | 796 | if (new_smc->use_fallback) |
| 797 | goto out_connected; |
| 798 | |
Ursula Braun | c5c1cc9 | 2017-10-25 11:01:46 +0200 | [diff] [blame] | 799 | /* check if peer is smc capable */ |
| 800 | if (!tcp_sk(newclcsock->sk)->syn_smc) { |
| 801 | new_smc->use_fallback = true; |
| 802 | goto out_connected; |
| 803 | } |
| 804 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 805 | /* do inband token exchange - |
| 806 | *wait for and receive SMC Proposal CLC message |
| 807 | */ |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 808 | reason_code = smc_clc_wait_msg(new_smc, &buf, sizeof(buf), |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 809 | SMC_CLC_PROPOSAL); |
| 810 | if (reason_code < 0) |
| 811 | goto out_err; |
| 812 | if (reason_code > 0) |
| 813 | goto decline_rdma; |
| 814 | |
| 815 | /* IPSec connections opt out of SMC-R optimizations */ |
| 816 | if (using_ipsec(new_smc)) { |
| 817 | reason_code = SMC_CLC_DECL_IPSEC; |
| 818 | goto decline_rdma; |
| 819 | } |
| 820 | |
| 821 | /* PNET table look up: search active ib_device and port |
| 822 | * within same PNETID that also contains the ethernet device |
| 823 | * used for the internal TCP socket |
| 824 | */ |
| 825 | smc_pnet_find_roce_resource(newclcsock->sk, &smcibdev, &ibport); |
| 826 | if (!smcibdev) { |
| 827 | reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */ |
| 828 | goto decline_rdma; |
| 829 | } |
| 830 | |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 831 | pclc = (struct smc_clc_msg_proposal *)&buf; |
| 832 | pclc_prfx = smc_clc_proposal_get_prefix(pclc); |
Karsten Graul | c246d94 | 2018-03-16 15:06:39 +0100 | [diff] [blame] | 833 | |
| 834 | rc = smc_clc_prfx_match(newclcsock, pclc_prfx); |
| 835 | if (rc) { |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 836 | reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */ |
| 837 | goto decline_rdma; |
| 838 | } |
| 839 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 840 | /* allocate connection / link group */ |
| 841 | mutex_lock(&smc_create_lgr_pending); |
Karsten Graul | be6d467 | 2018-03-01 13:51:28 +0100 | [diff] [blame] | 842 | local_contact = smc_conn_create(new_smc, smcibdev, ibport, &pclc->lcl, |
| 843 | 0); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 844 | if (local_contact < 0) { |
| 845 | rc = local_contact; |
| 846 | if (rc == -ENOMEM) |
| 847 | reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/ |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 848 | goto decline_rdma_unlock; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 849 | } |
| 850 | link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK]; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 851 | |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 852 | /* create send buffer and rmb */ |
| 853 | rc = smc_buf_create(new_smc); |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 854 | if (rc) { |
| 855 | reason_code = SMC_CLC_DECL_MEM; |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 856 | goto decline_rdma_unlock; |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 857 | } |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 858 | |
Ursula Braun | 46c28db | 2017-04-10 14:58:01 +0200 | [diff] [blame] | 859 | smc_close_init(new_smc); |
| 860 | smc_rx_init(new_smc); |
| 861 | |
Ursula Braun | 652a1e4 | 2017-07-28 13:56:17 +0200 | [diff] [blame] | 862 | if (local_contact != SMC_FIRST_CONTACT) { |
| 863 | struct smc_buf_desc *buf_desc = new_smc->conn.rmb_desc; |
| 864 | |
| 865 | if (!buf_desc->reused) { |
| 866 | /* register memory region for new rmb */ |
| 867 | rc = smc_wr_reg_send(link, |
| 868 | buf_desc->mr_rx[SMC_SINGLE_LINK]); |
| 869 | if (rc) { |
| 870 | reason_code = SMC_CLC_DECL_INTERR; |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 871 | goto decline_rdma_unlock; |
Ursula Braun | 652a1e4 | 2017-07-28 13:56:17 +0200 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | } |
Ursula Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 875 | smc_rmb_sync_sg_for_device(&new_smc->conn); |
Ursula Braun | 652a1e4 | 2017-07-28 13:56:17 +0200 | [diff] [blame] | 876 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 877 | rc = smc_clc_send_accept(new_smc, local_contact); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 878 | if (rc) |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 879 | goto out_err_unlock; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 880 | |
| 881 | /* receive SMC Confirm CLC message */ |
| 882 | reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc), |
| 883 | SMC_CLC_CONFIRM); |
| 884 | if (reason_code < 0) |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 885 | goto out_err_unlock; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 886 | if (reason_code > 0) |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 887 | goto decline_rdma_unlock; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 888 | smc_conn_save_peer_info(new_smc, &cclc); |
| 889 | if (local_contact == SMC_FIRST_CONTACT) |
| 890 | smc_link_save_peer_info(link, &cclc); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 891 | |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 892 | rc = smc_rmb_rtoken_handling(&new_smc->conn, &cclc); |
| 893 | if (rc) { |
| 894 | reason_code = SMC_CLC_DECL_INTERR; |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 895 | goto decline_rdma_unlock; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 896 | } |
| 897 | |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 898 | if (local_contact == SMC_FIRST_CONTACT) { |
| 899 | rc = smc_ib_ready_link(link); |
| 900 | if (rc) { |
| 901 | reason_code = SMC_CLC_DECL_INTERR; |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 902 | goto decline_rdma_unlock; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 903 | } |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 904 | /* QP confirmation over RoCE fabric */ |
| 905 | reason_code = smc_serv_conf_first_link(new_smc); |
Ursula Braun | 0c9f151 | 2017-12-07 13:38:45 +0100 | [diff] [blame] | 906 | if (reason_code < 0) |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 907 | /* peer is not aware of a problem */ |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 908 | goto out_err_unlock; |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 909 | if (reason_code > 0) |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 910 | goto decline_rdma_unlock; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 911 | } |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 912 | |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 913 | smc_tx_init(new_smc); |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 914 | mutex_unlock(&smc_create_lgr_pending); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 915 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 916 | out_connected: |
| 917 | sk_refcnt_debug_inc(newsmcsk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 918 | if (newsmcsk->sk_state == SMC_INIT) |
| 919 | newsmcsk->sk_state = SMC_ACTIVE; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 920 | enqueue: |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 921 | lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 922 | if (lsmc->sk.sk_state == SMC_LISTEN) { |
| 923 | smc_accept_enqueue(&lsmc->sk, newsmcsk); |
| 924 | } else { /* no longer listening */ |
| 925 | smc_close_non_accepted(newsmcsk); |
| 926 | } |
| 927 | release_sock(&lsmc->sk); |
| 928 | |
| 929 | /* Wake up accept */ |
| 930 | lsmc->sk.sk_data_ready(&lsmc->sk); |
| 931 | sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */ |
| 932 | return; |
| 933 | |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 934 | decline_rdma_unlock: |
Ursula Braun | 610db66 | 2018-01-25 11:15:34 +0100 | [diff] [blame] | 935 | if (local_contact == SMC_FIRST_CONTACT) |
| 936 | smc_lgr_forget(new_smc->conn.lgr); |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 937 | mutex_unlock(&smc_create_lgr_pending); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 938 | decline_rdma: |
| 939 | /* RDMA setup failed, switch back to TCP */ |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 940 | smc_conn_free(&new_smc->conn); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 941 | new_smc->use_fallback = true; |
| 942 | if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) { |
Ursula Braun | 0c9f151 | 2017-12-07 13:38:45 +0100 | [diff] [blame] | 943 | if (smc_clc_send_decline(new_smc, reason_code) < 0) |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 944 | goto out_err; |
| 945 | } |
| 946 | goto out_connected; |
| 947 | |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 948 | out_err_unlock: |
Ursula Braun | 610db66 | 2018-01-25 11:15:34 +0100 | [diff] [blame] | 949 | if (local_contact == SMC_FIRST_CONTACT) |
| 950 | smc_lgr_forget(new_smc->conn.lgr); |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 951 | mutex_unlock(&smc_create_lgr_pending); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 952 | out_err: |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 953 | if (newsmcsk->sk_state == SMC_INIT) |
| 954 | sock_put(&new_smc->sk); /* passive closing */ |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 955 | newsmcsk->sk_state = SMC_CLOSED; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 956 | smc_conn_free(&new_smc->conn); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 957 | goto enqueue; /* queue new sock with sk_err set */ |
| 958 | } |
| 959 | |
| 960 | static void smc_tcp_listen_work(struct work_struct *work) |
| 961 | { |
| 962 | struct smc_sock *lsmc = container_of(work, struct smc_sock, |
| 963 | tcp_listen_work); |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 964 | struct sock *lsk = &lsmc->sk; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 965 | struct smc_sock *new_smc; |
| 966 | int rc = 0; |
| 967 | |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 968 | lock_sock(lsk); |
| 969 | while (lsk->sk_state == SMC_LISTEN) { |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 970 | rc = smc_clcsock_accept(lsmc, &new_smc); |
| 971 | if (rc) |
| 972 | goto out; |
| 973 | if (!new_smc) |
| 974 | continue; |
| 975 | |
| 976 | new_smc->listen_smc = lsmc; |
Ursula Braun | ee9dfbe | 2018-04-26 17:18:21 +0200 | [diff] [blame] | 977 | new_smc->use_fallback = lsmc->use_fallback; |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 978 | sock_hold(lsk); /* sock_put in smc_listen_work */ |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 979 | INIT_WORK(&new_smc->smc_listen_work, smc_listen_work); |
| 980 | smc_copy_sock_settings_to_smc(new_smc); |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 981 | sock_hold(&new_smc->sk); /* sock_put in passive closing */ |
| 982 | if (!schedule_work(&new_smc->smc_listen_work)) |
| 983 | sock_put(&new_smc->sk); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | out: |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 987 | release_sock(lsk); |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 988 | sock_put(&lsmc->sk); /* sock_hold in smc_listen */ |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 989 | } |
| 990 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 991 | static int smc_listen(struct socket *sock, int backlog) |
| 992 | { |
| 993 | struct sock *sk = sock->sk; |
| 994 | struct smc_sock *smc; |
| 995 | int rc; |
| 996 | |
| 997 | smc = smc_sk(sk); |
| 998 | lock_sock(sk); |
| 999 | |
| 1000 | rc = -EINVAL; |
| 1001 | if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN)) |
| 1002 | goto out; |
| 1003 | |
| 1004 | rc = 0; |
| 1005 | if (sk->sk_state == SMC_LISTEN) { |
| 1006 | sk->sk_max_ack_backlog = backlog; |
| 1007 | goto out; |
| 1008 | } |
| 1009 | /* some socket options are handled in core, so we could not apply |
| 1010 | * them to the clc socket -- copy smc socket options to clc socket |
| 1011 | */ |
| 1012 | smc_copy_sock_settings_to_clc(smc); |
Ursula Braun | ee9dfbe | 2018-04-26 17:18:21 +0200 | [diff] [blame] | 1013 | if (!smc->use_fallback) |
| 1014 | tcp_sk(smc->clcsock->sk)->syn_smc = 1; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1015 | |
| 1016 | rc = kernel_listen(smc->clcsock, backlog); |
| 1017 | if (rc) |
| 1018 | goto out; |
| 1019 | sk->sk_max_ack_backlog = backlog; |
| 1020 | sk->sk_ack_backlog = 0; |
| 1021 | sk->sk_state = SMC_LISTEN; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1022 | INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work); |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 1023 | sock_hold(sk); /* sock_hold in tcp_listen_worker */ |
| 1024 | if (!schedule_work(&smc->tcp_listen_work)) |
| 1025 | sock_put(sk); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1026 | |
| 1027 | out: |
| 1028 | release_sock(sk); |
| 1029 | return rc; |
| 1030 | } |
| 1031 | |
| 1032 | static int smc_accept(struct socket *sock, struct socket *new_sock, |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 1033 | int flags, bool kern) |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1034 | { |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1035 | struct sock *sk = sock->sk, *nsk; |
| 1036 | DECLARE_WAITQUEUE(wait, current); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1037 | struct smc_sock *lsmc; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1038 | long timeo; |
| 1039 | int rc = 0; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1040 | |
| 1041 | lsmc = smc_sk(sk); |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 1042 | sock_hold(sk); /* sock_put below */ |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1043 | lock_sock(sk); |
| 1044 | |
| 1045 | if (lsmc->sk.sk_state != SMC_LISTEN) { |
| 1046 | rc = -EINVAL; |
| 1047 | goto out; |
| 1048 | } |
| 1049 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1050 | /* Wait for an incoming connection */ |
| 1051 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 1052 | add_wait_queue_exclusive(sk_sleep(sk), &wait); |
| 1053 | while (!(nsk = smc_accept_dequeue(sk, new_sock))) { |
| 1054 | set_current_state(TASK_INTERRUPTIBLE); |
| 1055 | if (!timeo) { |
| 1056 | rc = -EAGAIN; |
| 1057 | break; |
| 1058 | } |
| 1059 | release_sock(sk); |
| 1060 | timeo = schedule_timeout(timeo); |
| 1061 | /* wakeup by sk_data_ready in smc_listen_work() */ |
| 1062 | sched_annotate_sleep(); |
| 1063 | lock_sock(sk); |
| 1064 | if (signal_pending(current)) { |
| 1065 | rc = sock_intr_errno(timeo); |
| 1066 | break; |
| 1067 | } |
| 1068 | } |
| 1069 | set_current_state(TASK_RUNNING); |
| 1070 | remove_wait_queue(sk_sleep(sk), &wait); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1071 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1072 | if (!rc) |
| 1073 | rc = sock_error(nsk); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1074 | |
| 1075 | out: |
| 1076 | release_sock(sk); |
Ursula Braun | 51f1de7 | 2018-01-26 09:28:48 +0100 | [diff] [blame] | 1077 | sock_put(sk); /* sock_hold above */ |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1078 | return rc; |
| 1079 | } |
| 1080 | |
| 1081 | static int smc_getname(struct socket *sock, struct sockaddr *addr, |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 1082 | int peer) |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1083 | { |
| 1084 | struct smc_sock *smc; |
| 1085 | |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1086 | if (peer && (sock->sk->sk_state != SMC_ACTIVE) && |
| 1087 | (sock->sk->sk_state != SMC_APPCLOSEWAIT1)) |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1088 | return -ENOTCONN; |
| 1089 | |
| 1090 | smc = smc_sk(sock->sk); |
| 1091 | |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 1092 | return smc->clcsock->ops->getname(smc->clcsock, addr, peer); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) |
| 1096 | { |
| 1097 | struct sock *sk = sock->sk; |
| 1098 | struct smc_sock *smc; |
| 1099 | int rc = -EPIPE; |
| 1100 | |
| 1101 | smc = smc_sk(sk); |
| 1102 | lock_sock(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1103 | if ((sk->sk_state != SMC_ACTIVE) && |
| 1104 | (sk->sk_state != SMC_APPCLOSEWAIT1) && |
| 1105 | (sk->sk_state != SMC_INIT)) |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1106 | goto out; |
Ursula Braun | ee9dfbe | 2018-04-26 17:18:21 +0200 | [diff] [blame] | 1107 | |
| 1108 | if (msg->msg_flags & MSG_FASTOPEN) { |
| 1109 | if (sk->sk_state == SMC_INIT) { |
| 1110 | smc->use_fallback = true; |
| 1111 | } else { |
| 1112 | rc = -EINVAL; |
| 1113 | goto out; |
| 1114 | } |
| 1115 | } |
| 1116 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1117 | if (smc->use_fallback) |
| 1118 | rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len); |
| 1119 | else |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 1120 | rc = smc_tx_sendmsg(smc, msg, len); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1121 | out: |
| 1122 | release_sock(sk); |
| 1123 | return rc; |
| 1124 | } |
| 1125 | |
| 1126 | static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, |
| 1127 | int flags) |
| 1128 | { |
| 1129 | struct sock *sk = sock->sk; |
| 1130 | struct smc_sock *smc; |
| 1131 | int rc = -ENOTCONN; |
| 1132 | |
| 1133 | smc = smc_sk(sk); |
| 1134 | lock_sock(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1135 | if ((sk->sk_state == SMC_INIT) || |
| 1136 | (sk->sk_state == SMC_LISTEN) || |
| 1137 | (sk->sk_state == SMC_CLOSED)) |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1138 | goto out; |
| 1139 | |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1140 | if (sk->sk_state == SMC_PEERFINCLOSEWAIT) { |
| 1141 | rc = 0; |
| 1142 | goto out; |
| 1143 | } |
| 1144 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1145 | if (smc->use_fallback) |
| 1146 | rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags); |
| 1147 | else |
Ursula Braun | 952310c | 2017-01-09 16:55:24 +0100 | [diff] [blame] | 1148 | rc = smc_rx_recvmsg(smc, msg, len, flags); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1149 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1150 | out: |
| 1151 | release_sock(sk); |
| 1152 | return rc; |
| 1153 | } |
| 1154 | |
Al Viro | ade994f | 2017-07-03 00:01:49 -0400 | [diff] [blame] | 1155 | static __poll_t smc_accept_poll(struct sock *parent) |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1156 | { |
Ursula Braun | 8dce278 | 2018-01-26 09:28:47 +0100 | [diff] [blame] | 1157 | struct smc_sock *isk = smc_sk(parent); |
Al Viro | 63e2480 | 2018-02-01 10:02:53 -0500 | [diff] [blame] | 1158 | __poll_t mask = 0; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1159 | |
Ursula Braun | 8dce278 | 2018-01-26 09:28:47 +0100 | [diff] [blame] | 1160 | spin_lock(&isk->accept_q_lock); |
| 1161 | if (!list_empty(&isk->accept_q)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1162 | mask = EPOLLIN | EPOLLRDNORM; |
Ursula Braun | 8dce278 | 2018-01-26 09:28:47 +0100 | [diff] [blame] | 1163 | spin_unlock(&isk->accept_q_lock); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1164 | |
Ursula Braun | 8dce278 | 2018-01-26 09:28:47 +0100 | [diff] [blame] | 1165 | return mask; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1166 | } |
| 1167 | |
Al Viro | ade994f | 2017-07-03 00:01:49 -0400 | [diff] [blame] | 1168 | static __poll_t smc_poll(struct file *file, struct socket *sock, |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1169 | poll_table *wait) |
| 1170 | { |
| 1171 | struct sock *sk = sock->sk; |
Al Viro | e6c8adc | 2017-07-03 22:25:56 -0400 | [diff] [blame] | 1172 | __poll_t mask = 0; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1173 | struct smc_sock *smc; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1174 | int rc; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1175 | |
Ursula Braun | 8dce278 | 2018-01-26 09:28:47 +0100 | [diff] [blame] | 1176 | if (!sk) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1177 | return EPOLLNVAL; |
Ursula Braun | 8dce278 | 2018-01-26 09:28:47 +0100 | [diff] [blame] | 1178 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1179 | smc = smc_sk(sock->sk); |
Ursula Braun | 8dce278 | 2018-01-26 09:28:47 +0100 | [diff] [blame] | 1180 | sock_hold(sk); |
| 1181 | lock_sock(sk); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1182 | if ((sk->sk_state == SMC_INIT) || smc->use_fallback) { |
| 1183 | /* delegate to CLC child sock */ |
Ursula Braun | 8dce278 | 2018-01-26 09:28:47 +0100 | [diff] [blame] | 1184 | release_sock(sk); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1185 | mask = smc->clcsock->ops->poll(file, smc->clcsock, wait); |
| 1186 | /* if non-blocking connect finished ... */ |
| 1187 | lock_sock(sk); |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1188 | if ((sk->sk_state == SMC_INIT) && (mask & EPOLLOUT)) { |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1189 | sk->sk_err = smc->clcsock->sk->sk_err; |
| 1190 | if (sk->sk_err) { |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1191 | mask |= EPOLLERR; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1192 | } else { |
| 1193 | rc = smc_connect_rdma(smc); |
| 1194 | if (rc < 0) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1195 | mask |= EPOLLERR; |
Ursula Braun | 8dce278 | 2018-01-26 09:28:47 +0100 | [diff] [blame] | 1196 | /* success cases including fallback */ |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1197 | mask |= EPOLLOUT | EPOLLWRNORM; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1198 | } |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1199 | } |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1200 | } else { |
Ursula Braun | 8dce278 | 2018-01-26 09:28:47 +0100 | [diff] [blame] | 1201 | if (sk->sk_state != SMC_CLOSED) { |
| 1202 | release_sock(sk); |
| 1203 | sock_poll_wait(file, sk_sleep(sk), wait); |
| 1204 | lock_sock(sk); |
| 1205 | } |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1206 | if (sk->sk_err) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1207 | mask |= EPOLLERR; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1208 | if ((sk->sk_shutdown == SHUTDOWN_MASK) || |
| 1209 | (sk->sk_state == SMC_CLOSED)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1210 | mask |= EPOLLHUP; |
Ursula Braun | 8dce278 | 2018-01-26 09:28:47 +0100 | [diff] [blame] | 1211 | if (sk->sk_state == SMC_LISTEN) { |
| 1212 | /* woken up by sk_data_ready in smc_listen_work() */ |
| 1213 | mask = smc_accept_poll(sk); |
| 1214 | } else { |
| 1215 | if (atomic_read(&smc->conn.sndbuf_space) || |
| 1216 | sk->sk_shutdown & SEND_SHUTDOWN) { |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1217 | mask |= EPOLLOUT | EPOLLWRNORM; |
Ursula Braun | 8dce278 | 2018-01-26 09:28:47 +0100 | [diff] [blame] | 1218 | } else { |
| 1219 | sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); |
| 1220 | set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
| 1221 | } |
| 1222 | if (atomic_read(&smc->conn.bytes_to_rcv)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1223 | mask |= EPOLLIN | EPOLLRDNORM; |
Ursula Braun | 8dce278 | 2018-01-26 09:28:47 +0100 | [diff] [blame] | 1224 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1225 | mask |= EPOLLIN | EPOLLRDNORM | EPOLLRDHUP; |
Ursula Braun | 8dce278 | 2018-01-26 09:28:47 +0100 | [diff] [blame] | 1226 | if (sk->sk_state == SMC_APPCLOSEWAIT1) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1227 | mask |= EPOLLIN; |
Ursula Braun | 8dce278 | 2018-01-26 09:28:47 +0100 | [diff] [blame] | 1228 | } |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1229 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1230 | } |
Ursula Braun | 8dce278 | 2018-01-26 09:28:47 +0100 | [diff] [blame] | 1231 | release_sock(sk); |
| 1232 | sock_put(sk); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1233 | |
| 1234 | return mask; |
| 1235 | } |
| 1236 | |
| 1237 | static int smc_shutdown(struct socket *sock, int how) |
| 1238 | { |
| 1239 | struct sock *sk = sock->sk; |
| 1240 | struct smc_sock *smc; |
| 1241 | int rc = -EINVAL; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1242 | int rc1 = 0; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1243 | |
| 1244 | smc = smc_sk(sk); |
| 1245 | |
| 1246 | if ((how < SHUT_RD) || (how > SHUT_RDWR)) |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1247 | return rc; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1248 | |
| 1249 | lock_sock(sk); |
| 1250 | |
| 1251 | rc = -ENOTCONN; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1252 | if ((sk->sk_state != SMC_LISTEN) && |
| 1253 | (sk->sk_state != SMC_ACTIVE) && |
| 1254 | (sk->sk_state != SMC_PEERCLOSEWAIT1) && |
| 1255 | (sk->sk_state != SMC_PEERCLOSEWAIT2) && |
| 1256 | (sk->sk_state != SMC_APPCLOSEWAIT1) && |
| 1257 | (sk->sk_state != SMC_APPCLOSEWAIT2) && |
| 1258 | (sk->sk_state != SMC_APPFINCLOSEWAIT)) |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1259 | goto out; |
| 1260 | if (smc->use_fallback) { |
| 1261 | rc = kernel_sock_shutdown(smc->clcsock, how); |
| 1262 | sk->sk_shutdown = smc->clcsock->sk->sk_shutdown; |
| 1263 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
| 1264 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1265 | goto out; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1266 | } |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1267 | switch (how) { |
| 1268 | case SHUT_RDWR: /* shutdown in both directions */ |
| 1269 | rc = smc_close_active(smc); |
| 1270 | break; |
| 1271 | case SHUT_WR: |
| 1272 | rc = smc_close_shutdown_write(smc); |
| 1273 | break; |
| 1274 | case SHUT_RD: |
Ursula Braun | 1255fcb | 2018-04-19 15:56:40 +0200 | [diff] [blame] | 1275 | rc = 0; |
| 1276 | /* nothing more to do because peer is not involved */ |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1277 | break; |
| 1278 | } |
Ursula Braun | 1255fcb | 2018-04-19 15:56:40 +0200 | [diff] [blame] | 1279 | if (smc->clcsock) |
| 1280 | rc1 = kernel_sock_shutdown(smc->clcsock, how); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1281 | /* map sock_shutdown_cmd constants to sk_shutdown value range */ |
| 1282 | sk->sk_shutdown |= how + 1; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1283 | |
| 1284 | out: |
| 1285 | release_sock(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1286 | return rc ? rc : rc1; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | static int smc_setsockopt(struct socket *sock, int level, int optname, |
| 1290 | char __user *optval, unsigned int optlen) |
| 1291 | { |
| 1292 | struct sock *sk = sock->sk; |
| 1293 | struct smc_sock *smc; |
Ursula Braun | 01d2f7e | 2018-04-26 17:18:22 +0200 | [diff] [blame^] | 1294 | int val, rc; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1295 | |
| 1296 | smc = smc_sk(sk); |
| 1297 | |
| 1298 | /* generic setsockopts reaching us here always apply to the |
| 1299 | * CLC socket |
| 1300 | */ |
Ursula Braun | ee9dfbe | 2018-04-26 17:18:21 +0200 | [diff] [blame] | 1301 | rc = smc->clcsock->ops->setsockopt(smc->clcsock, level, optname, |
| 1302 | optval, optlen); |
| 1303 | if (smc->clcsock->sk->sk_err) { |
| 1304 | sk->sk_err = smc->clcsock->sk->sk_err; |
| 1305 | sk->sk_error_report(sk); |
| 1306 | } |
| 1307 | if (rc) |
| 1308 | return rc; |
| 1309 | |
Ursula Braun | 01d2f7e | 2018-04-26 17:18:22 +0200 | [diff] [blame^] | 1310 | if (optlen < sizeof(int)) |
| 1311 | return rc; |
| 1312 | get_user(val, (int __user *)optval); |
| 1313 | |
Ursula Braun | ee9dfbe | 2018-04-26 17:18:21 +0200 | [diff] [blame] | 1314 | lock_sock(sk); |
| 1315 | switch (optname) { |
| 1316 | case TCP_ULP: |
| 1317 | case TCP_FASTOPEN: |
| 1318 | case TCP_FASTOPEN_CONNECT: |
| 1319 | case TCP_FASTOPEN_KEY: |
| 1320 | case TCP_FASTOPEN_NO_COOKIE: |
| 1321 | /* option not supported by SMC */ |
| 1322 | if (sk->sk_state == SMC_INIT) { |
| 1323 | smc->use_fallback = true; |
| 1324 | } else { |
| 1325 | if (!smc->use_fallback) |
| 1326 | rc = -EINVAL; |
| 1327 | } |
| 1328 | break; |
Ursula Braun | 01d2f7e | 2018-04-26 17:18:22 +0200 | [diff] [blame^] | 1329 | case TCP_NODELAY: |
| 1330 | if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) { |
| 1331 | if (val) |
| 1332 | mod_delayed_work(system_wq, &smc->conn.tx_work, |
| 1333 | 0); |
| 1334 | } |
| 1335 | break; |
| 1336 | case TCP_CORK: |
| 1337 | if (sk->sk_state != SMC_INIT && sk->sk_state != SMC_LISTEN) { |
| 1338 | if (!val) |
| 1339 | mod_delayed_work(system_wq, &smc->conn.tx_work, |
| 1340 | 0); |
| 1341 | } |
| 1342 | break; |
Ursula Braun | ee9dfbe | 2018-04-26 17:18:21 +0200 | [diff] [blame] | 1343 | default: |
| 1344 | break; |
| 1345 | } |
| 1346 | release_sock(sk); |
| 1347 | |
| 1348 | return rc; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | static int smc_getsockopt(struct socket *sock, int level, int optname, |
| 1352 | char __user *optval, int __user *optlen) |
| 1353 | { |
| 1354 | struct smc_sock *smc; |
| 1355 | |
| 1356 | smc = smc_sk(sock->sk); |
| 1357 | /* socket options apply to the CLC socket */ |
| 1358 | return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname, |
| 1359 | optval, optlen); |
| 1360 | } |
| 1361 | |
| 1362 | static int smc_ioctl(struct socket *sock, unsigned int cmd, |
| 1363 | unsigned long arg) |
| 1364 | { |
| 1365 | struct smc_sock *smc; |
| 1366 | |
| 1367 | smc = smc_sk(sock->sk); |
| 1368 | if (smc->use_fallback) |
| 1369 | return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg); |
| 1370 | else |
| 1371 | return sock_no_ioctl(sock, cmd, arg); |
| 1372 | } |
| 1373 | |
| 1374 | static ssize_t smc_sendpage(struct socket *sock, struct page *page, |
| 1375 | int offset, size_t size, int flags) |
| 1376 | { |
| 1377 | struct sock *sk = sock->sk; |
| 1378 | struct smc_sock *smc; |
| 1379 | int rc = -EPIPE; |
| 1380 | |
| 1381 | smc = smc_sk(sk); |
| 1382 | lock_sock(sk); |
| 1383 | if (sk->sk_state != SMC_ACTIVE) |
| 1384 | goto out; |
| 1385 | if (smc->use_fallback) |
| 1386 | rc = kernel_sendpage(smc->clcsock, page, offset, |
| 1387 | size, flags); |
| 1388 | else |
| 1389 | rc = sock_no_sendpage(sock, page, offset, size, flags); |
| 1390 | |
| 1391 | out: |
| 1392 | release_sock(sk); |
| 1393 | return rc; |
| 1394 | } |
| 1395 | |
| 1396 | static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos, |
| 1397 | struct pipe_inode_info *pipe, size_t len, |
| 1398 | unsigned int flags) |
| 1399 | { |
| 1400 | struct sock *sk = sock->sk; |
| 1401 | struct smc_sock *smc; |
| 1402 | int rc = -ENOTCONN; |
| 1403 | |
| 1404 | smc = smc_sk(sk); |
| 1405 | lock_sock(sk); |
| 1406 | if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED)) |
| 1407 | goto out; |
| 1408 | if (smc->use_fallback) { |
| 1409 | rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos, |
| 1410 | pipe, len, flags); |
| 1411 | } else { |
| 1412 | rc = -EOPNOTSUPP; |
| 1413 | } |
| 1414 | out: |
| 1415 | release_sock(sk); |
| 1416 | return rc; |
| 1417 | } |
| 1418 | |
| 1419 | /* must look like tcp */ |
| 1420 | static const struct proto_ops smc_sock_ops = { |
| 1421 | .family = PF_SMC, |
| 1422 | .owner = THIS_MODULE, |
| 1423 | .release = smc_release, |
| 1424 | .bind = smc_bind, |
| 1425 | .connect = smc_connect, |
| 1426 | .socketpair = sock_no_socketpair, |
| 1427 | .accept = smc_accept, |
| 1428 | .getname = smc_getname, |
| 1429 | .poll = smc_poll, |
| 1430 | .ioctl = smc_ioctl, |
| 1431 | .listen = smc_listen, |
| 1432 | .shutdown = smc_shutdown, |
| 1433 | .setsockopt = smc_setsockopt, |
| 1434 | .getsockopt = smc_getsockopt, |
| 1435 | .sendmsg = smc_sendmsg, |
| 1436 | .recvmsg = smc_recvmsg, |
| 1437 | .mmap = sock_no_mmap, |
| 1438 | .sendpage = smc_sendpage, |
| 1439 | .splice_read = smc_splice_read, |
| 1440 | }; |
| 1441 | |
| 1442 | static int smc_create(struct net *net, struct socket *sock, int protocol, |
| 1443 | int kern) |
| 1444 | { |
Karsten Graul | aaa4d33 | 2018-03-16 15:06:41 +0100 | [diff] [blame] | 1445 | int family = (protocol == SMCPROTO_SMC6) ? PF_INET6 : PF_INET; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1446 | struct smc_sock *smc; |
| 1447 | struct sock *sk; |
| 1448 | int rc; |
| 1449 | |
| 1450 | rc = -ESOCKTNOSUPPORT; |
| 1451 | if (sock->type != SOCK_STREAM) |
| 1452 | goto out; |
| 1453 | |
| 1454 | rc = -EPROTONOSUPPORT; |
Karsten Graul | aaa4d33 | 2018-03-16 15:06:41 +0100 | [diff] [blame] | 1455 | if (protocol != SMCPROTO_SMC && protocol != SMCPROTO_SMC6) |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1456 | goto out; |
| 1457 | |
| 1458 | rc = -ENOBUFS; |
| 1459 | sock->ops = &smc_sock_ops; |
Karsten Graul | aaa4d33 | 2018-03-16 15:06:41 +0100 | [diff] [blame] | 1460 | sk = smc_sock_alloc(net, sock, protocol); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1461 | if (!sk) |
| 1462 | goto out; |
| 1463 | |
| 1464 | /* create internal TCP socket for CLC handshake and fallback */ |
| 1465 | smc = smc_sk(sk); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1466 | smc->use_fallback = false; /* assume rdma capability first */ |
Karsten Graul | aaa4d33 | 2018-03-16 15:06:41 +0100 | [diff] [blame] | 1467 | rc = sock_create_kern(net, family, SOCK_STREAM, IPPROTO_TCP, |
| 1468 | &smc->clcsock); |
Davide Caratti | a5dcb73 | 2018-02-28 12:44:09 +0100 | [diff] [blame] | 1469 | if (rc) { |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1470 | sk_common_release(sk); |
Davide Caratti | a5dcb73 | 2018-02-28 12:44:09 +0100 | [diff] [blame] | 1471 | goto out; |
| 1472 | } |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 1473 | smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE); |
| 1474 | 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] | 1475 | |
| 1476 | out: |
| 1477 | return rc; |
| 1478 | } |
| 1479 | |
| 1480 | static const struct net_proto_family smc_sock_family_ops = { |
| 1481 | .family = PF_SMC, |
| 1482 | .owner = THIS_MODULE, |
| 1483 | .create = smc_create, |
| 1484 | }; |
| 1485 | |
| 1486 | static int __init smc_init(void) |
| 1487 | { |
| 1488 | int rc; |
| 1489 | |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 1490 | rc = smc_pnet_init(); |
| 1491 | if (rc) |
| 1492 | return rc; |
| 1493 | |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 1494 | rc = smc_llc_init(); |
| 1495 | if (rc) { |
| 1496 | pr_err("%s: smc_llc_init fails with %d\n", __func__, rc); |
| 1497 | goto out_pnet; |
| 1498 | } |
| 1499 | |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 1500 | rc = smc_cdc_init(); |
| 1501 | if (rc) { |
| 1502 | pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc); |
| 1503 | goto out_pnet; |
| 1504 | } |
| 1505 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1506 | rc = proto_register(&smc_proto, 1); |
| 1507 | if (rc) { |
Karsten Graul | aaa4d33 | 2018-03-16 15:06:41 +0100 | [diff] [blame] | 1508 | pr_err("%s: proto_register(v4) fails with %d\n", __func__, rc); |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 1509 | goto out_pnet; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1510 | } |
| 1511 | |
Karsten Graul | aaa4d33 | 2018-03-16 15:06:41 +0100 | [diff] [blame] | 1512 | rc = proto_register(&smc_proto6, 1); |
| 1513 | if (rc) { |
| 1514 | pr_err("%s: proto_register(v6) fails with %d\n", __func__, rc); |
| 1515 | goto out_proto; |
| 1516 | } |
| 1517 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1518 | rc = sock_register(&smc_sock_family_ops); |
| 1519 | if (rc) { |
| 1520 | pr_err("%s: sock_register fails with %d\n", __func__, rc); |
Karsten Graul | aaa4d33 | 2018-03-16 15:06:41 +0100 | [diff] [blame] | 1521 | goto out_proto6; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1522 | } |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 1523 | INIT_HLIST_HEAD(&smc_v4_hashinfo.ht); |
Karsten Graul | aaa4d33 | 2018-03-16 15:06:41 +0100 | [diff] [blame] | 1524 | INIT_HLIST_HEAD(&smc_v6_hashinfo.ht); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1525 | |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 1526 | rc = smc_ib_register_client(); |
| 1527 | if (rc) { |
| 1528 | pr_err("%s: ib_register fails with %d\n", __func__, rc); |
| 1529 | goto out_sock; |
| 1530 | } |
| 1531 | |
Ursula Braun | c5c1cc9 | 2017-10-25 11:01:46 +0200 | [diff] [blame] | 1532 | static_branch_enable(&tcp_have_smc); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1533 | return 0; |
| 1534 | |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 1535 | out_sock: |
| 1536 | sock_unregister(PF_SMC); |
Karsten Graul | aaa4d33 | 2018-03-16 15:06:41 +0100 | [diff] [blame] | 1537 | out_proto6: |
| 1538 | proto_unregister(&smc_proto6); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1539 | out_proto: |
| 1540 | proto_unregister(&smc_proto); |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 1541 | out_pnet: |
| 1542 | smc_pnet_exit(); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1543 | return rc; |
| 1544 | } |
| 1545 | |
| 1546 | static void __exit smc_exit(void) |
| 1547 | { |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 1548 | struct smc_link_group *lgr, *lg; |
| 1549 | LIST_HEAD(lgr_freeing_list); |
| 1550 | |
| 1551 | spin_lock_bh(&smc_lgr_list.lock); |
| 1552 | if (!list_empty(&smc_lgr_list.list)) |
| 1553 | list_splice_init(&smc_lgr_list.list, &lgr_freeing_list); |
| 1554 | spin_unlock_bh(&smc_lgr_list.lock); |
| 1555 | list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) { |
| 1556 | list_del_init(&lgr->list); |
Ursula Braun | 268ffcc | 2018-03-14 11:01:01 +0100 | [diff] [blame] | 1557 | cancel_delayed_work_sync(&lgr->free_work); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 1558 | smc_lgr_free(lgr); /* free link group */ |
| 1559 | } |
Ursula Braun | c5c1cc9 | 2017-10-25 11:01:46 +0200 | [diff] [blame] | 1560 | static_branch_disable(&tcp_have_smc); |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 1561 | smc_ib_unregister_client(); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1562 | sock_unregister(PF_SMC); |
Karsten Graul | aaa4d33 | 2018-03-16 15:06:41 +0100 | [diff] [blame] | 1563 | proto_unregister(&smc_proto6); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1564 | proto_unregister(&smc_proto); |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 1565 | smc_pnet_exit(); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | module_init(smc_init); |
| 1569 | module_exit(smc_exit); |
| 1570 | |
| 1571 | MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>"); |
| 1572 | MODULE_DESCRIPTION("smc socket address family"); |
| 1573 | MODULE_LICENSE("GPL"); |
| 1574 | MODULE_ALIAS_NETPROTO(PF_SMC); |