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