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