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