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