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