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