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