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