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