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