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