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