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