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