Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Shared Memory Communications over RDMA (SMC-R) and RoCE |
| 3 | * |
| 4 | * AF_SMC protocol family socket handler keeping the AF_INET sock address type |
| 5 | * applies to SOCK_STREAM sockets only |
| 6 | * offers an alternative communication option for TCP-protocol sockets |
| 7 | * applicable with RoCE-cards only |
| 8 | * |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 9 | * Initial restrictions: |
| 10 | * - non-blocking connect postponed |
| 11 | * - IPv6 support postponed |
| 12 | * - support for alternate links postponed |
| 13 | * - partial support for non-blocking sockets only |
| 14 | * - support for urgent data postponed |
| 15 | * |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 16 | * Copyright IBM Corp. 2016 |
| 17 | * |
| 18 | * Author(s): Ursula Braun <ubraun@linux.vnet.ibm.com> |
| 19 | * based on prototype from Frank Blaschka |
| 20 | */ |
| 21 | |
| 22 | #define KMSG_COMPONENT "smc" |
| 23 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt |
| 24 | |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/socket.h> |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 27 | #include <linux/inetdevice.h> |
| 28 | #include <linux/workqueue.h> |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 29 | #include <linux/in.h> |
Ingo Molnar | c3edc40 | 2017-02-02 08:35:14 +0100 | [diff] [blame] | 30 | #include <linux/sched/signal.h> |
| 31 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 32 | #include <net/sock.h> |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 33 | #include <net/tcp.h> |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 34 | #include <net/smc.h> |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 35 | |
| 36 | #include "smc.h" |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 37 | #include "smc_clc.h" |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 38 | #include "smc_llc.h" |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 39 | #include "smc_cdc.h" |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 40 | #include "smc_core.h" |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 41 | #include "smc_ib.h" |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 42 | #include "smc_pnet.h" |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 43 | #include "smc_tx.h" |
Ursula Braun | 952310c | 2017-01-09 16:55:24 +0100 | [diff] [blame] | 44 | #include "smc_rx.h" |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 45 | #include "smc_close.h" |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 46 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 47 | static DEFINE_MUTEX(smc_create_lgr_pending); /* serialize link group |
| 48 | * creation |
| 49 | */ |
| 50 | |
| 51 | struct smc_lgr_list smc_lgr_list = { /* established link groups */ |
| 52 | .lock = __SPIN_LOCK_UNLOCKED(smc_lgr_list.lock), |
| 53 | .list = LIST_HEAD_INIT(smc_lgr_list.list), |
| 54 | }; |
| 55 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 56 | static void smc_tcp_listen_work(struct work_struct *); |
| 57 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 58 | static void smc_set_keepalive(struct sock *sk, int val) |
| 59 | { |
| 60 | struct smc_sock *smc = smc_sk(sk); |
| 61 | |
| 62 | smc->clcsock->sk->sk_prot->keepalive(smc->clcsock->sk, val); |
| 63 | } |
| 64 | |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 65 | static struct smc_hashinfo smc_v4_hashinfo = { |
| 66 | .lock = __RW_LOCK_UNLOCKED(smc_v4_hashinfo.lock), |
| 67 | }; |
| 68 | |
| 69 | int smc_hash_sk(struct sock *sk) |
| 70 | { |
| 71 | struct smc_hashinfo *h = sk->sk_prot->h.smc_hash; |
| 72 | struct hlist_head *head; |
| 73 | |
| 74 | head = &h->ht; |
| 75 | |
| 76 | write_lock_bh(&h->lock); |
| 77 | sk_add_node(sk, head); |
| 78 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); |
| 79 | write_unlock_bh(&h->lock); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | EXPORT_SYMBOL_GPL(smc_hash_sk); |
| 84 | |
| 85 | void smc_unhash_sk(struct sock *sk) |
| 86 | { |
| 87 | struct smc_hashinfo *h = sk->sk_prot->h.smc_hash; |
| 88 | |
| 89 | write_lock_bh(&h->lock); |
| 90 | if (sk_del_node_init(sk)) |
| 91 | sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); |
| 92 | write_unlock_bh(&h->lock); |
| 93 | } |
| 94 | EXPORT_SYMBOL_GPL(smc_unhash_sk); |
| 95 | |
| 96 | struct proto smc_proto = { |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 97 | .name = "SMC", |
| 98 | .owner = THIS_MODULE, |
| 99 | .keepalive = smc_set_keepalive, |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 100 | .hash = smc_hash_sk, |
| 101 | .unhash = smc_unhash_sk, |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 102 | .obj_size = sizeof(struct smc_sock), |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 103 | .h.smc_hash = &smc_v4_hashinfo, |
Paul E. McKenney | 5f0d5a3 | 2017-01-18 02:53:44 -0800 | [diff] [blame] | 104 | .slab_flags = SLAB_TYPESAFE_BY_RCU, |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 105 | }; |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 106 | EXPORT_SYMBOL_GPL(smc_proto); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 107 | |
| 108 | static int smc_release(struct socket *sock) |
| 109 | { |
| 110 | struct sock *sk = sock->sk; |
| 111 | struct smc_sock *smc; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 112 | int rc = 0; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 113 | |
| 114 | if (!sk) |
| 115 | goto out; |
| 116 | |
| 117 | smc = smc_sk(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 118 | sock_hold(sk); |
| 119 | if (sk->sk_state == SMC_LISTEN) |
| 120 | /* smc_close_non_accepted() is called and acquires |
| 121 | * sock lock for child sockets again |
| 122 | */ |
| 123 | lock_sock_nested(sk, SINGLE_DEPTH_NESTING); |
| 124 | else |
| 125 | lock_sock(sk); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 126 | |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 127 | if (smc->use_fallback) { |
| 128 | sk->sk_state = SMC_CLOSED; |
| 129 | sk->sk_state_change(sk); |
| 130 | } else { |
| 131 | rc = smc_close_active(smc); |
| 132 | sock_set_flag(sk, SOCK_DEAD); |
| 133 | sk->sk_shutdown |= SHUTDOWN_MASK; |
| 134 | } |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 135 | if (smc->clcsock) { |
| 136 | sock_release(smc->clcsock); |
| 137 | smc->clcsock = NULL; |
| 138 | } |
| 139 | |
| 140 | /* detach socket */ |
| 141 | sock_orphan(sk); |
| 142 | sock->sk = NULL; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 143 | if (smc->use_fallback) { |
| 144 | schedule_delayed_work(&smc->sock_put_work, TCP_TIMEWAIT_LEN); |
| 145 | } else if (sk->sk_state == SMC_CLOSED) { |
| 146 | smc_conn_free(&smc->conn); |
| 147 | schedule_delayed_work(&smc->sock_put_work, |
| 148 | SMC_CLOSE_SOCK_PUT_DELAY); |
| 149 | } |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 150 | release_sock(sk); |
| 151 | |
| 152 | sock_put(sk); |
| 153 | out: |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 154 | return rc; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static void smc_destruct(struct sock *sk) |
| 158 | { |
| 159 | if (sk->sk_state != SMC_CLOSED) |
| 160 | return; |
| 161 | if (!sock_flag(sk, SOCK_DEAD)) |
| 162 | return; |
| 163 | |
| 164 | sk_refcnt_debug_dec(sk); |
| 165 | } |
| 166 | |
| 167 | static struct sock *smc_sock_alloc(struct net *net, struct socket *sock) |
| 168 | { |
| 169 | struct smc_sock *smc; |
| 170 | struct sock *sk; |
| 171 | |
| 172 | sk = sk_alloc(net, PF_SMC, GFP_KERNEL, &smc_proto, 0); |
| 173 | if (!sk) |
| 174 | return NULL; |
| 175 | |
| 176 | sock_init_data(sock, sk); /* sets sk_refcnt to 1 */ |
| 177 | sk->sk_state = SMC_INIT; |
| 178 | sk->sk_destruct = smc_destruct; |
| 179 | sk->sk_protocol = SMCPROTO_SMC; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 180 | smc = smc_sk(sk); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 181 | INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work); |
| 182 | INIT_LIST_HEAD(&smc->accept_q); |
| 183 | spin_lock_init(&smc->accept_q_lock); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 184 | INIT_DELAYED_WORK(&smc->sock_put_work, smc_close_sock_put_work); |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 185 | sk->sk_prot->hash(sk); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 186 | sk_refcnt_debug_inc(sk); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 187 | |
| 188 | return sk; |
| 189 | } |
| 190 | |
| 191 | static int smc_bind(struct socket *sock, struct sockaddr *uaddr, |
| 192 | int addr_len) |
| 193 | { |
| 194 | struct sockaddr_in *addr = (struct sockaddr_in *)uaddr; |
| 195 | struct sock *sk = sock->sk; |
| 196 | struct smc_sock *smc; |
| 197 | int rc; |
| 198 | |
| 199 | smc = smc_sk(sk); |
| 200 | |
| 201 | /* replicate tests from inet_bind(), to be safe wrt. future changes */ |
| 202 | rc = -EINVAL; |
| 203 | if (addr_len < sizeof(struct sockaddr_in)) |
| 204 | goto out; |
| 205 | |
| 206 | rc = -EAFNOSUPPORT; |
| 207 | /* accept AF_UNSPEC (mapped to AF_INET) only if s_addr is INADDR_ANY */ |
| 208 | if ((addr->sin_family != AF_INET) && |
| 209 | ((addr->sin_family != AF_UNSPEC) || |
| 210 | (addr->sin_addr.s_addr != htonl(INADDR_ANY)))) |
| 211 | goto out; |
| 212 | |
| 213 | lock_sock(sk); |
| 214 | |
| 215 | /* Check if socket is already active */ |
| 216 | rc = -EINVAL; |
| 217 | if (sk->sk_state != SMC_INIT) |
| 218 | goto out_rel; |
| 219 | |
| 220 | smc->clcsock->sk->sk_reuse = sk->sk_reuse; |
| 221 | rc = kernel_bind(smc->clcsock, uaddr, addr_len); |
| 222 | |
| 223 | out_rel: |
| 224 | release_sock(sk); |
| 225 | out: |
| 226 | return rc; |
| 227 | } |
| 228 | |
| 229 | static void smc_copy_sock_settings(struct sock *nsk, struct sock *osk, |
| 230 | unsigned long mask) |
| 231 | { |
| 232 | /* options we don't get control via setsockopt for */ |
| 233 | nsk->sk_type = osk->sk_type; |
| 234 | nsk->sk_sndbuf = osk->sk_sndbuf; |
| 235 | nsk->sk_rcvbuf = osk->sk_rcvbuf; |
| 236 | nsk->sk_sndtimeo = osk->sk_sndtimeo; |
| 237 | nsk->sk_rcvtimeo = osk->sk_rcvtimeo; |
| 238 | nsk->sk_mark = osk->sk_mark; |
| 239 | nsk->sk_priority = osk->sk_priority; |
| 240 | nsk->sk_rcvlowat = osk->sk_rcvlowat; |
| 241 | nsk->sk_bound_dev_if = osk->sk_bound_dev_if; |
| 242 | nsk->sk_err = osk->sk_err; |
| 243 | |
| 244 | nsk->sk_flags &= ~mask; |
| 245 | nsk->sk_flags |= osk->sk_flags & mask; |
| 246 | } |
| 247 | |
| 248 | #define SK_FLAGS_SMC_TO_CLC ((1UL << SOCK_URGINLINE) | \ |
| 249 | (1UL << SOCK_KEEPOPEN) | \ |
| 250 | (1UL << SOCK_LINGER) | \ |
| 251 | (1UL << SOCK_BROADCAST) | \ |
| 252 | (1UL << SOCK_TIMESTAMP) | \ |
| 253 | (1UL << SOCK_DBG) | \ |
| 254 | (1UL << SOCK_RCVTSTAMP) | \ |
| 255 | (1UL << SOCK_RCVTSTAMPNS) | \ |
| 256 | (1UL << SOCK_LOCALROUTE) | \ |
| 257 | (1UL << SOCK_TIMESTAMPING_RX_SOFTWARE) | \ |
| 258 | (1UL << SOCK_RXQ_OVFL) | \ |
| 259 | (1UL << SOCK_WIFI_STATUS) | \ |
| 260 | (1UL << SOCK_NOFCS) | \ |
| 261 | (1UL << SOCK_FILTER_LOCKED)) |
| 262 | /* copy only relevant settings and flags of SOL_SOCKET level from smc to |
| 263 | * clc socket (since smc is not called for these options from net/core) |
| 264 | */ |
| 265 | static void smc_copy_sock_settings_to_clc(struct smc_sock *smc) |
| 266 | { |
| 267 | smc_copy_sock_settings(smc->clcsock->sk, &smc->sk, SK_FLAGS_SMC_TO_CLC); |
| 268 | } |
| 269 | |
| 270 | #define SK_FLAGS_CLC_TO_SMC ((1UL << SOCK_URGINLINE) | \ |
| 271 | (1UL << SOCK_KEEPOPEN) | \ |
| 272 | (1UL << SOCK_LINGER) | \ |
| 273 | (1UL << SOCK_DBG)) |
| 274 | /* copy only settings and flags relevant for smc from clc to smc socket */ |
| 275 | static void smc_copy_sock_settings_to_smc(struct smc_sock *smc) |
| 276 | { |
| 277 | smc_copy_sock_settings(&smc->sk, smc->clcsock->sk, SK_FLAGS_CLC_TO_SMC); |
| 278 | } |
| 279 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 280 | /* determine subnet and mask of internal TCP socket */ |
| 281 | int smc_netinfo_by_tcpsk(struct socket *clcsock, |
| 282 | __be32 *subnet, u8 *prefix_len) |
| 283 | { |
| 284 | struct dst_entry *dst = sk_dst_get(clcsock->sk); |
Ursula Braun | 731b008 | 2017-09-21 09:16:28 +0200 | [diff] [blame] | 285 | struct in_device *in_dev; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 286 | struct sockaddr_in addr; |
| 287 | int rc = -ENOENT; |
| 288 | int len; |
| 289 | |
| 290 | if (!dst) { |
| 291 | rc = -ENOTCONN; |
| 292 | goto out; |
| 293 | } |
| 294 | if (!dst->dev) { |
| 295 | rc = -ENODEV; |
| 296 | goto out_rel; |
| 297 | } |
| 298 | |
| 299 | /* get address to which the internal TCP socket is bound */ |
| 300 | kernel_getsockname(clcsock, (struct sockaddr *)&addr, &len); |
| 301 | /* analyze IPv4 specific data of net_device belonging to TCP socket */ |
Ursula Braun | 731b008 | 2017-09-21 09:16:28 +0200 | [diff] [blame] | 302 | rcu_read_lock(); |
| 303 | in_dev = __in_dev_get_rcu(dst->dev); |
| 304 | for_ifa(in_dev) { |
| 305 | if (!inet_ifa_match(addr.sin_addr.s_addr, ifa)) |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 306 | continue; |
| 307 | *prefix_len = inet_mask_len(ifa->ifa_mask); |
| 308 | *subnet = ifa->ifa_address & ifa->ifa_mask; |
| 309 | rc = 0; |
| 310 | break; |
Ursula Braun | 731b008 | 2017-09-21 09:16:28 +0200 | [diff] [blame] | 311 | } endfor_ifa(in_dev); |
| 312 | rcu_read_unlock(); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 313 | |
| 314 | out_rel: |
| 315 | dst_release(dst); |
| 316 | out: |
| 317 | return rc; |
| 318 | } |
| 319 | |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 320 | static int smc_clnt_conf_first_link(struct smc_sock *smc, union ib_gid *gid) |
| 321 | { |
| 322 | struct smc_link_group *lgr = smc->conn.lgr; |
| 323 | struct smc_link *link; |
| 324 | int rest; |
| 325 | int rc; |
| 326 | |
| 327 | link = &lgr->lnk[SMC_SINGLE_LINK]; |
| 328 | /* receive CONFIRM LINK request from server over RoCE fabric */ |
| 329 | rest = wait_for_completion_interruptible_timeout( |
| 330 | &link->llc_confirm, |
| 331 | SMC_LLC_WAIT_FIRST_TIME); |
| 332 | if (rest <= 0) { |
| 333 | struct smc_clc_msg_decline dclc; |
| 334 | |
| 335 | rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc), |
| 336 | SMC_CLC_DECLINE); |
| 337 | return rc; |
| 338 | } |
| 339 | |
| 340 | rc = smc_ib_modify_qp_rts(link); |
| 341 | if (rc) |
| 342 | return SMC_CLC_DECL_INTERR; |
| 343 | |
| 344 | smc_wr_remember_qp_attr(link); |
Ursula Braun | 652a1e4 | 2017-07-28 13:56:17 +0200 | [diff] [blame] | 345 | |
| 346 | rc = smc_wr_reg_send(link, |
| 347 | smc->conn.rmb_desc->mr_rx[SMC_SINGLE_LINK]); |
| 348 | if (rc) |
| 349 | return SMC_CLC_DECL_INTERR; |
| 350 | |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 351 | /* send CONFIRM LINK response over RoCE fabric */ |
| 352 | rc = smc_llc_send_confirm_link(link, |
| 353 | link->smcibdev->mac[link->ibport - 1], |
| 354 | gid, SMC_LLC_RESP); |
| 355 | if (rc < 0) |
| 356 | return SMC_CLC_DECL_TCL; |
| 357 | |
| 358 | return rc; |
| 359 | } |
| 360 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 361 | static void smc_conn_save_peer_info(struct smc_sock *smc, |
| 362 | struct smc_clc_msg_accept_confirm *clc) |
| 363 | { |
| 364 | smc->conn.peer_conn_idx = clc->conn_idx; |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 365 | smc->conn.local_tx_ctrl.token = ntohl(clc->rmbe_alert_token); |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 366 | smc->conn.peer_rmbe_size = smc_uncompress_bufsize(clc->rmbe_size); |
| 367 | atomic_set(&smc->conn.peer_rmbe_space, smc->conn.peer_rmbe_size); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static void smc_link_save_peer_info(struct smc_link *link, |
| 371 | struct smc_clc_msg_accept_confirm *clc) |
| 372 | { |
| 373 | link->peer_qpn = ntoh24(clc->qpn); |
| 374 | memcpy(link->peer_gid, clc->lcl.gid, SMC_GID_SIZE); |
| 375 | memcpy(link->peer_mac, clc->lcl.mac, sizeof(link->peer_mac)); |
| 376 | link->peer_psn = ntoh24(clc->psn); |
| 377 | link->peer_mtu = clc->qp_mtu; |
| 378 | } |
| 379 | |
Ursula Braun | 610db66 | 2018-01-25 11:15:34 +0100 | [diff] [blame^] | 380 | static void smc_lgr_forget(struct smc_link_group *lgr) |
| 381 | { |
| 382 | spin_lock_bh(&smc_lgr_list.lock); |
| 383 | /* do not use this link group for new connections */ |
| 384 | if (!list_empty(&lgr->list)) |
| 385 | list_del_init(&lgr->list); |
| 386 | spin_unlock_bh(&smc_lgr_list.lock); |
| 387 | } |
| 388 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 389 | /* setup for RDMA connection of client */ |
| 390 | static int smc_connect_rdma(struct smc_sock *smc) |
| 391 | { |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 392 | struct sockaddr_in *inaddr = (struct sockaddr_in *)smc->addr; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 393 | struct smc_clc_msg_accept_confirm aclc; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 394 | int local_contact = SMC_FIRST_CONTACT; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 395 | struct smc_ib_device *smcibdev; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 396 | struct smc_link *link; |
| 397 | u8 srv_first_contact; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 398 | int reason_code = 0; |
| 399 | int rc = 0; |
| 400 | u8 ibport; |
| 401 | |
Ursula Braun | c5c1cc9 | 2017-10-25 11:01:46 +0200 | [diff] [blame] | 402 | if (!tcp_sk(smc->clcsock->sk)->syn_smc) { |
| 403 | /* peer has not signalled SMC-capability */ |
| 404 | smc->use_fallback = true; |
| 405 | goto out_connected; |
| 406 | } |
| 407 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 408 | /* IPSec connections opt out of SMC-R optimizations */ |
| 409 | if (using_ipsec(smc)) { |
| 410 | reason_code = SMC_CLC_DECL_IPSEC; |
| 411 | goto decline_rdma; |
| 412 | } |
| 413 | |
| 414 | /* PNET table look up: search active ib_device and port |
| 415 | * within same PNETID that also contains the ethernet device |
| 416 | * used for the internal TCP socket |
| 417 | */ |
| 418 | smc_pnet_find_roce_resource(smc->clcsock->sk, &smcibdev, &ibport); |
| 419 | if (!smcibdev) { |
| 420 | reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */ |
| 421 | goto decline_rdma; |
| 422 | } |
| 423 | |
| 424 | /* do inband token exchange */ |
| 425 | reason_code = smc_clc_send_proposal(smc, smcibdev, ibport); |
| 426 | if (reason_code < 0) { |
| 427 | rc = reason_code; |
| 428 | goto out_err; |
| 429 | } |
| 430 | if (reason_code > 0) /* configuration error */ |
| 431 | goto decline_rdma; |
| 432 | /* receive SMC Accept CLC message */ |
| 433 | reason_code = smc_clc_wait_msg(smc, &aclc, sizeof(aclc), |
| 434 | SMC_CLC_ACCEPT); |
| 435 | if (reason_code < 0) { |
| 436 | rc = reason_code; |
| 437 | goto out_err; |
| 438 | } |
| 439 | if (reason_code > 0) |
| 440 | goto decline_rdma; |
| 441 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 442 | srv_first_contact = aclc.hdr.flag; |
| 443 | mutex_lock(&smc_create_lgr_pending); |
| 444 | local_contact = smc_conn_create(smc, inaddr->sin_addr.s_addr, smcibdev, |
| 445 | ibport, &aclc.lcl, srv_first_contact); |
| 446 | if (local_contact < 0) { |
| 447 | rc = local_contact; |
| 448 | if (rc == -ENOMEM) |
| 449 | reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/ |
| 450 | else if (rc == -ENOLINK) |
| 451 | reason_code = SMC_CLC_DECL_SYNCERR; /* synchr. error */ |
| 452 | goto decline_rdma_unlock; |
| 453 | } |
| 454 | link = &smc->conn.lgr->lnk[SMC_SINGLE_LINK]; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 455 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 456 | smc_conn_save_peer_info(smc, &aclc); |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 457 | |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 458 | /* create send buffer and rmb */ |
| 459 | rc = smc_buf_create(smc); |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 460 | if (rc) { |
| 461 | reason_code = SMC_CLC_DECL_MEM; |
| 462 | goto decline_rdma_unlock; |
| 463 | } |
| 464 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 465 | if (local_contact == SMC_FIRST_CONTACT) |
| 466 | smc_link_save_peer_info(link, &aclc); |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 467 | |
| 468 | rc = smc_rmb_rtoken_handling(&smc->conn, &aclc); |
| 469 | if (rc) { |
| 470 | reason_code = SMC_CLC_DECL_INTERR; |
| 471 | goto decline_rdma_unlock; |
| 472 | } |
| 473 | |
Ursula Braun | 46c28db | 2017-04-10 14:58:01 +0200 | [diff] [blame] | 474 | smc_close_init(smc); |
| 475 | smc_rx_init(smc); |
| 476 | |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 477 | if (local_contact == SMC_FIRST_CONTACT) { |
| 478 | rc = smc_ib_ready_link(link); |
| 479 | if (rc) { |
| 480 | reason_code = SMC_CLC_DECL_INTERR; |
| 481 | goto decline_rdma_unlock; |
| 482 | } |
Ursula Braun | 652a1e4 | 2017-07-28 13:56:17 +0200 | [diff] [blame] | 483 | } else { |
| 484 | struct smc_buf_desc *buf_desc = smc->conn.rmb_desc; |
| 485 | |
| 486 | if (!buf_desc->reused) { |
| 487 | /* register memory region for new rmb */ |
| 488 | rc = smc_wr_reg_send(link, |
| 489 | buf_desc->mr_rx[SMC_SINGLE_LINK]); |
| 490 | if (rc) { |
| 491 | reason_code = SMC_CLC_DECL_INTERR; |
| 492 | goto decline_rdma_unlock; |
| 493 | } |
| 494 | } |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 495 | } |
Ursula Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 496 | smc_rmb_sync_sg_for_device(&smc->conn); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 497 | |
| 498 | rc = smc_clc_send_confirm(smc); |
| 499 | if (rc) |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 500 | goto out_err_unlock; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 501 | |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 502 | if (local_contact == SMC_FIRST_CONTACT) { |
| 503 | /* QP confirmation over RoCE fabric */ |
| 504 | reason_code = smc_clnt_conf_first_link( |
| 505 | smc, &smcibdev->gid[ibport - 1]); |
| 506 | if (reason_code < 0) { |
| 507 | rc = reason_code; |
| 508 | goto out_err_unlock; |
| 509 | } |
| 510 | if (reason_code > 0) |
| 511 | goto decline_rdma_unlock; |
| 512 | } |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 513 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 514 | mutex_unlock(&smc_create_lgr_pending); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 515 | smc_tx_init(smc); |
| 516 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 517 | out_connected: |
| 518 | smc_copy_sock_settings_to_clc(smc); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 519 | if (smc->sk.sk_state == SMC_INIT) |
| 520 | smc->sk.sk_state = SMC_ACTIVE; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 521 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 522 | return rc ? rc : local_contact; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 523 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 524 | decline_rdma_unlock: |
Ursula Braun | 610db66 | 2018-01-25 11:15:34 +0100 | [diff] [blame^] | 525 | if (local_contact == SMC_FIRST_CONTACT) |
| 526 | smc_lgr_forget(smc->conn.lgr); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 527 | mutex_unlock(&smc_create_lgr_pending); |
| 528 | smc_conn_free(&smc->conn); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 529 | decline_rdma: |
| 530 | /* RDMA setup failed, switch back to TCP */ |
| 531 | smc->use_fallback = true; |
| 532 | if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) { |
Ursula Braun | bfbedfd | 2017-09-21 09:16:32 +0200 | [diff] [blame] | 533 | rc = smc_clc_send_decline(smc, reason_code); |
Ursula Braun | 0c9f151 | 2017-12-07 13:38:45 +0100 | [diff] [blame] | 534 | if (rc < 0) |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 535 | goto out_err; |
| 536 | } |
| 537 | goto out_connected; |
| 538 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 539 | out_err_unlock: |
Ursula Braun | 610db66 | 2018-01-25 11:15:34 +0100 | [diff] [blame^] | 540 | if (local_contact == SMC_FIRST_CONTACT) |
| 541 | smc_lgr_forget(smc->conn.lgr); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 542 | mutex_unlock(&smc_create_lgr_pending); |
| 543 | smc_conn_free(&smc->conn); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 544 | out_err: |
| 545 | return rc; |
| 546 | } |
| 547 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 548 | static int smc_connect(struct socket *sock, struct sockaddr *addr, |
| 549 | int alen, int flags) |
| 550 | { |
| 551 | struct sock *sk = sock->sk; |
| 552 | struct smc_sock *smc; |
| 553 | int rc = -EINVAL; |
| 554 | |
| 555 | smc = smc_sk(sk); |
| 556 | |
| 557 | /* separate smc parameter checking to be safe */ |
| 558 | if (alen < sizeof(addr->sa_family)) |
| 559 | goto out_err; |
| 560 | if (addr->sa_family != AF_INET) |
| 561 | goto out_err; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 562 | smc->addr = addr; /* needed for nonblocking connect */ |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 563 | |
| 564 | lock_sock(sk); |
| 565 | switch (sk->sk_state) { |
| 566 | default: |
| 567 | goto out; |
| 568 | case SMC_ACTIVE: |
| 569 | rc = -EISCONN; |
| 570 | goto out; |
| 571 | case SMC_INIT: |
| 572 | rc = 0; |
| 573 | break; |
| 574 | } |
| 575 | |
| 576 | smc_copy_sock_settings_to_clc(smc); |
Ursula Braun | c5c1cc9 | 2017-10-25 11:01:46 +0200 | [diff] [blame] | 577 | tcp_sk(smc->clcsock->sk)->syn_smc = 1; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 578 | rc = kernel_connect(smc->clcsock, addr, alen, flags); |
| 579 | if (rc) |
| 580 | goto out; |
| 581 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 582 | /* setup RDMA connection */ |
| 583 | rc = smc_connect_rdma(smc); |
| 584 | if (rc < 0) |
| 585 | goto out; |
| 586 | else |
| 587 | rc = 0; /* success cases including fallback */ |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 588 | |
| 589 | out: |
| 590 | release_sock(sk); |
| 591 | out_err: |
| 592 | return rc; |
| 593 | } |
| 594 | |
| 595 | static int smc_clcsock_accept(struct smc_sock *lsmc, struct smc_sock **new_smc) |
| 596 | { |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 597 | struct socket *new_clcsock = NULL; |
| 598 | struct sock *lsk = &lsmc->sk; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 599 | struct sock *new_sk; |
| 600 | int rc; |
| 601 | |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 602 | release_sock(lsk); |
| 603 | new_sk = smc_sock_alloc(sock_net(lsk), NULL); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 604 | if (!new_sk) { |
| 605 | rc = -ENOMEM; |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 606 | lsk->sk_err = ENOMEM; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 607 | *new_smc = NULL; |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 608 | lock_sock(lsk); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 609 | goto out; |
| 610 | } |
| 611 | *new_smc = smc_sk(new_sk); |
| 612 | |
| 613 | rc = kernel_accept(lsmc->clcsock, &new_clcsock, 0); |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 614 | lock_sock(lsk); |
Ursula Braun | 35a6b17 | 2018-01-24 10:28:13 +0100 | [diff] [blame] | 615 | if (rc < 0) |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 616 | lsk->sk_err = -rc; |
Ursula Braun | 35a6b17 | 2018-01-24 10:28:13 +0100 | [diff] [blame] | 617 | if (rc < 0 || lsk->sk_state == SMC_CLOSED) { |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 618 | if (new_clcsock) |
| 619 | sock_release(new_clcsock); |
| 620 | new_sk->sk_state = SMC_CLOSED; |
| 621 | sock_set_flag(new_sk, SOCK_DEAD); |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 622 | new_sk->sk_prot->unhash(new_sk); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 623 | sock_put(new_sk); |
| 624 | *new_smc = NULL; |
| 625 | goto out; |
| 626 | } |
| 627 | |
| 628 | (*new_smc)->clcsock = new_clcsock; |
| 629 | out: |
| 630 | return rc; |
| 631 | } |
| 632 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 633 | /* add a just created sock to the accept queue of the listen sock as |
| 634 | * candidate for a following socket accept call from user space |
| 635 | */ |
| 636 | static void smc_accept_enqueue(struct sock *parent, struct sock *sk) |
| 637 | { |
| 638 | struct smc_sock *par = smc_sk(parent); |
| 639 | |
| 640 | sock_hold(sk); |
| 641 | spin_lock(&par->accept_q_lock); |
| 642 | list_add_tail(&smc_sk(sk)->accept_q, &par->accept_q); |
| 643 | spin_unlock(&par->accept_q_lock); |
| 644 | sk_acceptq_added(parent); |
| 645 | } |
| 646 | |
| 647 | /* remove a socket from the accept queue of its parental listening socket */ |
| 648 | static void smc_accept_unlink(struct sock *sk) |
| 649 | { |
| 650 | struct smc_sock *par = smc_sk(sk)->listen_smc; |
| 651 | |
| 652 | spin_lock(&par->accept_q_lock); |
| 653 | list_del_init(&smc_sk(sk)->accept_q); |
| 654 | spin_unlock(&par->accept_q_lock); |
| 655 | sk_acceptq_removed(&smc_sk(sk)->listen_smc->sk); |
| 656 | sock_put(sk); |
| 657 | } |
| 658 | |
| 659 | /* remove a sock from the accept queue to bind it to a new socket created |
| 660 | * for a socket accept call from user space |
| 661 | */ |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 662 | struct sock *smc_accept_dequeue(struct sock *parent, |
| 663 | struct socket *new_sock) |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 664 | { |
| 665 | struct smc_sock *isk, *n; |
| 666 | struct sock *new_sk; |
| 667 | |
| 668 | list_for_each_entry_safe(isk, n, &smc_sk(parent)->accept_q, accept_q) { |
| 669 | new_sk = (struct sock *)isk; |
| 670 | |
| 671 | smc_accept_unlink(new_sk); |
| 672 | if (new_sk->sk_state == SMC_CLOSED) { |
Ursula Braun | 288c8390 | 2017-04-10 14:58:04 +0200 | [diff] [blame] | 673 | new_sk->sk_prot->unhash(new_sk); |
| 674 | sock_put(new_sk); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 675 | continue; |
| 676 | } |
| 677 | if (new_sock) |
| 678 | sock_graft(new_sk, new_sock); |
| 679 | return new_sk; |
| 680 | } |
| 681 | return NULL; |
| 682 | } |
| 683 | |
| 684 | /* clean up for a created but never accepted sock */ |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 685 | void smc_close_non_accepted(struct sock *sk) |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 686 | { |
| 687 | struct smc_sock *smc = smc_sk(sk); |
| 688 | |
| 689 | sock_hold(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 690 | lock_sock(sk); |
| 691 | if (!sk->sk_lingertime) |
| 692 | /* wait for peer closing */ |
| 693 | sk->sk_lingertime = SMC_MAX_STREAM_WAIT_TIMEOUT; |
Ursula Braun | 288c8390 | 2017-04-10 14:58:04 +0200 | [diff] [blame] | 694 | if (smc->use_fallback) { |
| 695 | sk->sk_state = SMC_CLOSED; |
| 696 | } else { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 697 | smc_close_active(smc); |
Ursula Braun | 288c8390 | 2017-04-10 14:58:04 +0200 | [diff] [blame] | 698 | sock_set_flag(sk, SOCK_DEAD); |
| 699 | sk->sk_shutdown |= SHUTDOWN_MASK; |
| 700 | } |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 701 | if (smc->clcsock) { |
| 702 | struct socket *tcp; |
| 703 | |
| 704 | tcp = smc->clcsock; |
| 705 | smc->clcsock = NULL; |
| 706 | sock_release(tcp); |
| 707 | } |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 708 | if (smc->use_fallback) { |
| 709 | schedule_delayed_work(&smc->sock_put_work, TCP_TIMEWAIT_LEN); |
Ursula Braun | 288c8390 | 2017-04-10 14:58:04 +0200 | [diff] [blame] | 710 | } else if (sk->sk_state == SMC_CLOSED) { |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 711 | smc_conn_free(&smc->conn); |
| 712 | schedule_delayed_work(&smc->sock_put_work, |
| 713 | SMC_CLOSE_SOCK_PUT_DELAY); |
| 714 | } |
| 715 | release_sock(sk); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 716 | sock_put(sk); |
| 717 | } |
| 718 | |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 719 | static int smc_serv_conf_first_link(struct smc_sock *smc) |
| 720 | { |
| 721 | struct smc_link_group *lgr = smc->conn.lgr; |
| 722 | struct smc_link *link; |
| 723 | int rest; |
| 724 | int rc; |
| 725 | |
| 726 | link = &lgr->lnk[SMC_SINGLE_LINK]; |
Ursula Braun | 652a1e4 | 2017-07-28 13:56:17 +0200 | [diff] [blame] | 727 | |
| 728 | rc = smc_wr_reg_send(link, |
| 729 | smc->conn.rmb_desc->mr_rx[SMC_SINGLE_LINK]); |
| 730 | if (rc) |
| 731 | return SMC_CLC_DECL_INTERR; |
| 732 | |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 733 | /* send CONFIRM LINK request to client over the RoCE fabric */ |
| 734 | rc = smc_llc_send_confirm_link(link, |
| 735 | link->smcibdev->mac[link->ibport - 1], |
| 736 | &link->smcibdev->gid[link->ibport - 1], |
| 737 | SMC_LLC_REQ); |
| 738 | if (rc < 0) |
| 739 | return SMC_CLC_DECL_TCL; |
| 740 | |
| 741 | /* receive CONFIRM LINK response from client over the RoCE fabric */ |
| 742 | rest = wait_for_completion_interruptible_timeout( |
| 743 | &link->llc_confirm_resp, |
| 744 | SMC_LLC_WAIT_FIRST_TIME); |
| 745 | if (rest <= 0) { |
| 746 | struct smc_clc_msg_decline dclc; |
| 747 | |
| 748 | rc = smc_clc_wait_msg(smc, &dclc, sizeof(dclc), |
| 749 | SMC_CLC_DECLINE); |
| 750 | } |
| 751 | |
| 752 | return rc; |
| 753 | } |
| 754 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 755 | /* setup for RDMA connection of server */ |
| 756 | static void smc_listen_work(struct work_struct *work) |
| 757 | { |
| 758 | struct smc_sock *new_smc = container_of(work, struct smc_sock, |
| 759 | smc_listen_work); |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 760 | struct smc_clc_msg_proposal_prefix *pclc_prfx; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 761 | struct socket *newclcsock = new_smc->clcsock; |
| 762 | struct smc_sock *lsmc = new_smc->listen_smc; |
| 763 | struct smc_clc_msg_accept_confirm cclc; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 764 | int local_contact = SMC_REUSE_CONTACT; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 765 | struct sock *newsmcsk = &new_smc->sk; |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 766 | struct smc_clc_msg_proposal *pclc; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 767 | struct smc_ib_device *smcibdev; |
| 768 | struct sockaddr_in peeraddr; |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 769 | u8 buf[SMC_CLC_MAX_LEN]; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 770 | struct smc_link *link; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 771 | int reason_code = 0; |
| 772 | int rc = 0, len; |
| 773 | __be32 subnet; |
| 774 | u8 prefix_len; |
| 775 | u8 ibport; |
| 776 | |
Ursula Braun | c5c1cc9 | 2017-10-25 11:01:46 +0200 | [diff] [blame] | 777 | /* check if peer is smc capable */ |
| 778 | if (!tcp_sk(newclcsock->sk)->syn_smc) { |
| 779 | new_smc->use_fallback = true; |
| 780 | goto out_connected; |
| 781 | } |
| 782 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 783 | /* do inband token exchange - |
| 784 | *wait for and receive SMC Proposal CLC message |
| 785 | */ |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 786 | reason_code = smc_clc_wait_msg(new_smc, &buf, sizeof(buf), |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 787 | SMC_CLC_PROPOSAL); |
| 788 | if (reason_code < 0) |
| 789 | goto out_err; |
| 790 | if (reason_code > 0) |
| 791 | goto decline_rdma; |
| 792 | |
| 793 | /* IPSec connections opt out of SMC-R optimizations */ |
| 794 | if (using_ipsec(new_smc)) { |
| 795 | reason_code = SMC_CLC_DECL_IPSEC; |
| 796 | goto decline_rdma; |
| 797 | } |
| 798 | |
| 799 | /* PNET table look up: search active ib_device and port |
| 800 | * within same PNETID that also contains the ethernet device |
| 801 | * used for the internal TCP socket |
| 802 | */ |
| 803 | smc_pnet_find_roce_resource(newclcsock->sk, &smcibdev, &ibport); |
| 804 | if (!smcibdev) { |
| 805 | reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */ |
| 806 | goto decline_rdma; |
| 807 | } |
| 808 | |
| 809 | /* determine subnet and mask from internal TCP socket */ |
| 810 | rc = smc_netinfo_by_tcpsk(newclcsock, &subnet, &prefix_len); |
| 811 | if (rc) { |
| 812 | reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */ |
| 813 | goto decline_rdma; |
| 814 | } |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 815 | |
| 816 | pclc = (struct smc_clc_msg_proposal *)&buf; |
| 817 | pclc_prfx = smc_clc_proposal_get_prefix(pclc); |
| 818 | if (pclc_prfx->outgoing_subnet != subnet || |
| 819 | pclc_prfx->prefix_len != prefix_len) { |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 820 | reason_code = SMC_CLC_DECL_CNFERR; /* configuration error */ |
| 821 | goto decline_rdma; |
| 822 | } |
| 823 | |
| 824 | /* get address of the peer connected to the internal TCP socket */ |
| 825 | kernel_getpeername(newclcsock, (struct sockaddr *)&peeraddr, &len); |
| 826 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 827 | /* allocate connection / link group */ |
| 828 | mutex_lock(&smc_create_lgr_pending); |
| 829 | local_contact = smc_conn_create(new_smc, peeraddr.sin_addr.s_addr, |
Ursula Braun | e7b7a64 | 2017-12-07 13:38:49 +0100 | [diff] [blame] | 830 | smcibdev, ibport, &pclc->lcl, 0); |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 831 | if (local_contact < 0) { |
| 832 | rc = local_contact; |
| 833 | if (rc == -ENOMEM) |
| 834 | reason_code = SMC_CLC_DECL_MEM;/* insufficient memory*/ |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 835 | goto decline_rdma_unlock; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 836 | } |
| 837 | link = &new_smc->conn.lgr->lnk[SMC_SINGLE_LINK]; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 838 | |
Ursula Braun | 3e03472 | 2017-07-28 13:56:20 +0200 | [diff] [blame] | 839 | /* create send buffer and rmb */ |
| 840 | rc = smc_buf_create(new_smc); |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 841 | if (rc) { |
| 842 | reason_code = SMC_CLC_DECL_MEM; |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 843 | goto decline_rdma_unlock; |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 844 | } |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 845 | |
Ursula Braun | 46c28db | 2017-04-10 14:58:01 +0200 | [diff] [blame] | 846 | smc_close_init(new_smc); |
| 847 | smc_rx_init(new_smc); |
| 848 | |
Ursula Braun | 652a1e4 | 2017-07-28 13:56:17 +0200 | [diff] [blame] | 849 | if (local_contact != SMC_FIRST_CONTACT) { |
| 850 | struct smc_buf_desc *buf_desc = new_smc->conn.rmb_desc; |
| 851 | |
| 852 | if (!buf_desc->reused) { |
| 853 | /* register memory region for new rmb */ |
| 854 | rc = smc_wr_reg_send(link, |
| 855 | buf_desc->mr_rx[SMC_SINGLE_LINK]); |
| 856 | if (rc) { |
| 857 | reason_code = SMC_CLC_DECL_INTERR; |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 858 | goto decline_rdma_unlock; |
Ursula Braun | 652a1e4 | 2017-07-28 13:56:17 +0200 | [diff] [blame] | 859 | } |
| 860 | } |
| 861 | } |
Ursula Braun | 10428dd | 2017-07-28 13:56:22 +0200 | [diff] [blame] | 862 | smc_rmb_sync_sg_for_device(&new_smc->conn); |
Ursula Braun | 652a1e4 | 2017-07-28 13:56:17 +0200 | [diff] [blame] | 863 | |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 864 | rc = smc_clc_send_accept(new_smc, local_contact); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 865 | if (rc) |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 866 | goto out_err_unlock; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 867 | |
| 868 | /* receive SMC Confirm CLC message */ |
| 869 | reason_code = smc_clc_wait_msg(new_smc, &cclc, sizeof(cclc), |
| 870 | SMC_CLC_CONFIRM); |
| 871 | if (reason_code < 0) |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 872 | goto out_err_unlock; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 873 | if (reason_code > 0) |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 874 | goto decline_rdma_unlock; |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 875 | smc_conn_save_peer_info(new_smc, &cclc); |
| 876 | if (local_contact == SMC_FIRST_CONTACT) |
| 877 | smc_link_save_peer_info(link, &cclc); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 878 | |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 879 | rc = smc_rmb_rtoken_handling(&new_smc->conn, &cclc); |
| 880 | if (rc) { |
| 881 | reason_code = SMC_CLC_DECL_INTERR; |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 882 | goto decline_rdma_unlock; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 883 | } |
| 884 | |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 885 | if (local_contact == SMC_FIRST_CONTACT) { |
| 886 | rc = smc_ib_ready_link(link); |
| 887 | if (rc) { |
| 888 | reason_code = SMC_CLC_DECL_INTERR; |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 889 | goto decline_rdma_unlock; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 890 | } |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 891 | /* QP confirmation over RoCE fabric */ |
| 892 | reason_code = smc_serv_conf_first_link(new_smc); |
Ursula Braun | 0c9f151 | 2017-12-07 13:38:45 +0100 | [diff] [blame] | 893 | if (reason_code < 0) |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 894 | /* peer is not aware of a problem */ |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 895 | goto out_err_unlock; |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 896 | if (reason_code > 0) |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 897 | goto decline_rdma_unlock; |
Ursula Braun | bd4ad57 | 2017-01-09 16:55:20 +0100 | [diff] [blame] | 898 | } |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 899 | |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 900 | smc_tx_init(new_smc); |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 901 | mutex_unlock(&smc_create_lgr_pending); |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 902 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 903 | out_connected: |
| 904 | sk_refcnt_debug_inc(newsmcsk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 905 | if (newsmcsk->sk_state == SMC_INIT) |
| 906 | newsmcsk->sk_state = SMC_ACTIVE; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 907 | enqueue: |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 908 | lock_sock_nested(&lsmc->sk, SINGLE_DEPTH_NESTING); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 909 | if (lsmc->sk.sk_state == SMC_LISTEN) { |
| 910 | smc_accept_enqueue(&lsmc->sk, newsmcsk); |
| 911 | } else { /* no longer listening */ |
| 912 | smc_close_non_accepted(newsmcsk); |
| 913 | } |
| 914 | release_sock(&lsmc->sk); |
| 915 | |
| 916 | /* Wake up accept */ |
| 917 | lsmc->sk.sk_data_ready(&lsmc->sk); |
| 918 | sock_put(&lsmc->sk); /* sock_hold in smc_tcp_listen_work */ |
| 919 | return; |
| 920 | |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 921 | decline_rdma_unlock: |
Ursula Braun | 610db66 | 2018-01-25 11:15:34 +0100 | [diff] [blame^] | 922 | if (local_contact == SMC_FIRST_CONTACT) |
| 923 | smc_lgr_forget(new_smc->conn.lgr); |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 924 | mutex_unlock(&smc_create_lgr_pending); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 925 | decline_rdma: |
| 926 | /* RDMA setup failed, switch back to TCP */ |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 927 | smc_conn_free(&new_smc->conn); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 928 | new_smc->use_fallback = true; |
| 929 | if (reason_code && (reason_code != SMC_CLC_DECL_REPLY)) { |
Ursula Braun | 0c9f151 | 2017-12-07 13:38:45 +0100 | [diff] [blame] | 930 | if (smc_clc_send_decline(new_smc, reason_code) < 0) |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 931 | goto out_err; |
| 932 | } |
| 933 | goto out_connected; |
| 934 | |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 935 | out_err_unlock: |
Ursula Braun | 610db66 | 2018-01-25 11:15:34 +0100 | [diff] [blame^] | 936 | if (local_contact == SMC_FIRST_CONTACT) |
| 937 | smc_lgr_forget(new_smc->conn.lgr); |
Ursula Braun | 145686b | 2017-10-25 11:01:44 +0200 | [diff] [blame] | 938 | mutex_unlock(&smc_create_lgr_pending); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 939 | out_err: |
| 940 | newsmcsk->sk_state = SMC_CLOSED; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 941 | smc_conn_free(&new_smc->conn); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 942 | goto enqueue; /* queue new sock with sk_err set */ |
| 943 | } |
| 944 | |
| 945 | static void smc_tcp_listen_work(struct work_struct *work) |
| 946 | { |
| 947 | struct smc_sock *lsmc = container_of(work, struct smc_sock, |
| 948 | tcp_listen_work); |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 949 | struct sock *lsk = &lsmc->sk; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 950 | struct smc_sock *new_smc; |
| 951 | int rc = 0; |
| 952 | |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 953 | lock_sock(lsk); |
| 954 | while (lsk->sk_state == SMC_LISTEN) { |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 955 | rc = smc_clcsock_accept(lsmc, &new_smc); |
| 956 | if (rc) |
| 957 | goto out; |
| 958 | if (!new_smc) |
| 959 | continue; |
| 960 | |
| 961 | new_smc->listen_smc = lsmc; |
| 962 | new_smc->use_fallback = false; /* assume rdma capability first*/ |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 963 | sock_hold(lsk); /* sock_put in smc_listen_work */ |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 964 | INIT_WORK(&new_smc->smc_listen_work, smc_listen_work); |
| 965 | smc_copy_sock_settings_to_smc(new_smc); |
| 966 | schedule_work(&new_smc->smc_listen_work); |
| 967 | } |
| 968 | |
| 969 | out: |
Ursula Braun | 3163c50 | 2018-01-24 10:28:12 +0100 | [diff] [blame] | 970 | release_sock(lsk); |
| 971 | lsk->sk_data_ready(lsk); /* no more listening, wake accept */ |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 972 | } |
| 973 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 974 | static int smc_listen(struct socket *sock, int backlog) |
| 975 | { |
| 976 | struct sock *sk = sock->sk; |
| 977 | struct smc_sock *smc; |
| 978 | int rc; |
| 979 | |
| 980 | smc = smc_sk(sk); |
| 981 | lock_sock(sk); |
| 982 | |
| 983 | rc = -EINVAL; |
| 984 | if ((sk->sk_state != SMC_INIT) && (sk->sk_state != SMC_LISTEN)) |
| 985 | goto out; |
| 986 | |
| 987 | rc = 0; |
| 988 | if (sk->sk_state == SMC_LISTEN) { |
| 989 | sk->sk_max_ack_backlog = backlog; |
| 990 | goto out; |
| 991 | } |
| 992 | /* some socket options are handled in core, so we could not apply |
| 993 | * them to the clc socket -- copy smc socket options to clc socket |
| 994 | */ |
| 995 | smc_copy_sock_settings_to_clc(smc); |
Ursula Braun | c5c1cc9 | 2017-10-25 11:01:46 +0200 | [diff] [blame] | 996 | tcp_sk(smc->clcsock->sk)->syn_smc = 1; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 997 | |
| 998 | rc = kernel_listen(smc->clcsock, backlog); |
| 999 | if (rc) |
| 1000 | goto out; |
| 1001 | sk->sk_max_ack_backlog = backlog; |
| 1002 | sk->sk_ack_backlog = 0; |
| 1003 | sk->sk_state = SMC_LISTEN; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1004 | INIT_WORK(&smc->tcp_listen_work, smc_tcp_listen_work); |
| 1005 | schedule_work(&smc->tcp_listen_work); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1006 | |
| 1007 | out: |
| 1008 | release_sock(sk); |
| 1009 | return rc; |
| 1010 | } |
| 1011 | |
| 1012 | static int smc_accept(struct socket *sock, struct socket *new_sock, |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 1013 | int flags, bool kern) |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1014 | { |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1015 | struct sock *sk = sock->sk, *nsk; |
| 1016 | DECLARE_WAITQUEUE(wait, current); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1017 | struct smc_sock *lsmc; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1018 | long timeo; |
| 1019 | int rc = 0; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1020 | |
| 1021 | lsmc = smc_sk(sk); |
| 1022 | lock_sock(sk); |
| 1023 | |
| 1024 | if (lsmc->sk.sk_state != SMC_LISTEN) { |
| 1025 | rc = -EINVAL; |
| 1026 | goto out; |
| 1027 | } |
| 1028 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1029 | /* Wait for an incoming connection */ |
| 1030 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 1031 | add_wait_queue_exclusive(sk_sleep(sk), &wait); |
| 1032 | while (!(nsk = smc_accept_dequeue(sk, new_sock))) { |
| 1033 | set_current_state(TASK_INTERRUPTIBLE); |
| 1034 | if (!timeo) { |
| 1035 | rc = -EAGAIN; |
| 1036 | break; |
| 1037 | } |
| 1038 | release_sock(sk); |
| 1039 | timeo = schedule_timeout(timeo); |
| 1040 | /* wakeup by sk_data_ready in smc_listen_work() */ |
| 1041 | sched_annotate_sleep(); |
| 1042 | lock_sock(sk); |
| 1043 | if (signal_pending(current)) { |
| 1044 | rc = sock_intr_errno(timeo); |
| 1045 | break; |
| 1046 | } |
| 1047 | } |
| 1048 | set_current_state(TASK_RUNNING); |
| 1049 | remove_wait_queue(sk_sleep(sk), &wait); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1050 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1051 | if (!rc) |
| 1052 | rc = sock_error(nsk); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1053 | |
| 1054 | out: |
| 1055 | release_sock(sk); |
| 1056 | return rc; |
| 1057 | } |
| 1058 | |
| 1059 | static int smc_getname(struct socket *sock, struct sockaddr *addr, |
| 1060 | int *len, int peer) |
| 1061 | { |
| 1062 | struct smc_sock *smc; |
| 1063 | |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1064 | if (peer && (sock->sk->sk_state != SMC_ACTIVE) && |
| 1065 | (sock->sk->sk_state != SMC_APPCLOSEWAIT1)) |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1066 | return -ENOTCONN; |
| 1067 | |
| 1068 | smc = smc_sk(sock->sk); |
| 1069 | |
| 1070 | return smc->clcsock->ops->getname(smc->clcsock, addr, len, peer); |
| 1071 | } |
| 1072 | |
| 1073 | static int smc_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) |
| 1074 | { |
| 1075 | struct sock *sk = sock->sk; |
| 1076 | struct smc_sock *smc; |
| 1077 | int rc = -EPIPE; |
| 1078 | |
| 1079 | smc = smc_sk(sk); |
| 1080 | lock_sock(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1081 | if ((sk->sk_state != SMC_ACTIVE) && |
| 1082 | (sk->sk_state != SMC_APPCLOSEWAIT1) && |
| 1083 | (sk->sk_state != SMC_INIT)) |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1084 | goto out; |
| 1085 | if (smc->use_fallback) |
| 1086 | rc = smc->clcsock->ops->sendmsg(smc->clcsock, msg, len); |
| 1087 | else |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 1088 | rc = smc_tx_sendmsg(smc, msg, len); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1089 | out: |
| 1090 | release_sock(sk); |
| 1091 | return rc; |
| 1092 | } |
| 1093 | |
| 1094 | static int smc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, |
| 1095 | int flags) |
| 1096 | { |
| 1097 | struct sock *sk = sock->sk; |
| 1098 | struct smc_sock *smc; |
| 1099 | int rc = -ENOTCONN; |
| 1100 | |
| 1101 | smc = smc_sk(sk); |
| 1102 | lock_sock(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1103 | if ((sk->sk_state == SMC_INIT) || |
| 1104 | (sk->sk_state == SMC_LISTEN) || |
| 1105 | (sk->sk_state == SMC_CLOSED)) |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1106 | goto out; |
| 1107 | |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1108 | if (sk->sk_state == SMC_PEERFINCLOSEWAIT) { |
| 1109 | rc = 0; |
| 1110 | goto out; |
| 1111 | } |
| 1112 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1113 | if (smc->use_fallback) |
| 1114 | rc = smc->clcsock->ops->recvmsg(smc->clcsock, msg, len, flags); |
| 1115 | else |
Ursula Braun | 952310c | 2017-01-09 16:55:24 +0100 | [diff] [blame] | 1116 | rc = smc_rx_recvmsg(smc, msg, len, flags); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1117 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1118 | out: |
| 1119 | release_sock(sk); |
| 1120 | return rc; |
| 1121 | } |
| 1122 | |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1123 | static unsigned int smc_accept_poll(struct sock *parent) |
| 1124 | { |
| 1125 | struct smc_sock *isk; |
| 1126 | struct sock *sk; |
| 1127 | |
| 1128 | lock_sock(parent); |
| 1129 | list_for_each_entry(isk, &smc_sk(parent)->accept_q, accept_q) { |
| 1130 | sk = (struct sock *)isk; |
| 1131 | |
| 1132 | if (sk->sk_state == SMC_ACTIVE) { |
| 1133 | release_sock(parent); |
| 1134 | return POLLIN | POLLRDNORM; |
| 1135 | } |
| 1136 | } |
| 1137 | release_sock(parent); |
| 1138 | |
| 1139 | return 0; |
| 1140 | } |
| 1141 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1142 | static unsigned int smc_poll(struct file *file, struct socket *sock, |
| 1143 | poll_table *wait) |
| 1144 | { |
| 1145 | struct sock *sk = sock->sk; |
| 1146 | unsigned int mask = 0; |
| 1147 | struct smc_sock *smc; |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1148 | int rc; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1149 | |
| 1150 | smc = smc_sk(sock->sk); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1151 | if ((sk->sk_state == SMC_INIT) || smc->use_fallback) { |
| 1152 | /* delegate to CLC child sock */ |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1153 | mask = smc->clcsock->ops->poll(file, smc->clcsock, wait); |
| 1154 | /* if non-blocking connect finished ... */ |
| 1155 | lock_sock(sk); |
| 1156 | if ((sk->sk_state == SMC_INIT) && (mask & POLLOUT)) { |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1157 | sk->sk_err = smc->clcsock->sk->sk_err; |
| 1158 | if (sk->sk_err) { |
| 1159 | mask |= POLLERR; |
| 1160 | } else { |
| 1161 | rc = smc_connect_rdma(smc); |
| 1162 | if (rc < 0) |
| 1163 | mask |= POLLERR; |
| 1164 | else |
| 1165 | /* success cases including fallback */ |
| 1166 | mask |= POLLOUT | POLLWRNORM; |
| 1167 | } |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1168 | } |
| 1169 | release_sock(sk); |
| 1170 | } else { |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1171 | sock_poll_wait(file, sk_sleep(sk), wait); |
| 1172 | if (sk->sk_state == SMC_LISTEN) |
| 1173 | /* woken up by sk_data_ready in smc_listen_work() */ |
| 1174 | mask |= smc_accept_poll(sk); |
| 1175 | if (sk->sk_err) |
| 1176 | mask |= POLLERR; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1177 | if (atomic_read(&smc->conn.sndbuf_space) || |
| 1178 | (sk->sk_shutdown & SEND_SHUTDOWN)) { |
Ursula Braun | e6727f3 | 2017-01-09 16:55:23 +0100 | [diff] [blame] | 1179 | mask |= POLLOUT | POLLWRNORM; |
| 1180 | } else { |
| 1181 | sk_set_bit(SOCKWQ_ASYNC_NOSPACE, sk); |
| 1182 | set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
| 1183 | } |
Ursula Braun | 952310c | 2017-01-09 16:55:24 +0100 | [diff] [blame] | 1184 | if (atomic_read(&smc->conn.bytes_to_rcv)) |
| 1185 | mask |= POLLIN | POLLRDNORM; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1186 | if ((sk->sk_shutdown == SHUTDOWN_MASK) || |
| 1187 | (sk->sk_state == SMC_CLOSED)) |
| 1188 | mask |= POLLHUP; |
| 1189 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 1190 | mask |= POLLIN | POLLRDNORM | POLLRDHUP; |
| 1191 | if (sk->sk_state == SMC_APPCLOSEWAIT1) |
| 1192 | mask |= POLLIN; |
| 1193 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | return mask; |
| 1197 | } |
| 1198 | |
| 1199 | static int smc_shutdown(struct socket *sock, int how) |
| 1200 | { |
| 1201 | struct sock *sk = sock->sk; |
| 1202 | struct smc_sock *smc; |
| 1203 | int rc = -EINVAL; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1204 | int rc1 = 0; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1205 | |
| 1206 | smc = smc_sk(sk); |
| 1207 | |
| 1208 | if ((how < SHUT_RD) || (how > SHUT_RDWR)) |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1209 | return rc; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1210 | |
| 1211 | lock_sock(sk); |
| 1212 | |
| 1213 | rc = -ENOTCONN; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1214 | if ((sk->sk_state != SMC_LISTEN) && |
| 1215 | (sk->sk_state != SMC_ACTIVE) && |
| 1216 | (sk->sk_state != SMC_PEERCLOSEWAIT1) && |
| 1217 | (sk->sk_state != SMC_PEERCLOSEWAIT2) && |
| 1218 | (sk->sk_state != SMC_APPCLOSEWAIT1) && |
| 1219 | (sk->sk_state != SMC_APPCLOSEWAIT2) && |
| 1220 | (sk->sk_state != SMC_APPFINCLOSEWAIT)) |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1221 | goto out; |
| 1222 | if (smc->use_fallback) { |
| 1223 | rc = kernel_sock_shutdown(smc->clcsock, how); |
| 1224 | sk->sk_shutdown = smc->clcsock->sk->sk_shutdown; |
| 1225 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
| 1226 | sk->sk_state = SMC_CLOSED; |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1227 | goto out; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1228 | } |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1229 | switch (how) { |
| 1230 | case SHUT_RDWR: /* shutdown in both directions */ |
| 1231 | rc = smc_close_active(smc); |
| 1232 | break; |
| 1233 | case SHUT_WR: |
| 1234 | rc = smc_close_shutdown_write(smc); |
| 1235 | break; |
| 1236 | case SHUT_RD: |
| 1237 | if (sk->sk_state == SMC_LISTEN) |
| 1238 | rc = smc_close_active(smc); |
| 1239 | else |
| 1240 | rc = 0; |
| 1241 | /* nothing more to do because peer is not involved */ |
| 1242 | break; |
| 1243 | } |
| 1244 | rc1 = kernel_sock_shutdown(smc->clcsock, how); |
| 1245 | /* map sock_shutdown_cmd constants to sk_shutdown value range */ |
| 1246 | sk->sk_shutdown |= how + 1; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1247 | |
| 1248 | out: |
| 1249 | release_sock(sk); |
Ursula Braun | b38d732 | 2017-01-09 16:55:25 +0100 | [diff] [blame] | 1250 | return rc ? rc : rc1; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | static int smc_setsockopt(struct socket *sock, int level, int optname, |
| 1254 | char __user *optval, unsigned int optlen) |
| 1255 | { |
| 1256 | struct sock *sk = sock->sk; |
| 1257 | struct smc_sock *smc; |
| 1258 | |
| 1259 | smc = smc_sk(sk); |
| 1260 | |
| 1261 | /* generic setsockopts reaching us here always apply to the |
| 1262 | * CLC socket |
| 1263 | */ |
| 1264 | return smc->clcsock->ops->setsockopt(smc->clcsock, level, optname, |
| 1265 | optval, optlen); |
| 1266 | } |
| 1267 | |
| 1268 | static int smc_getsockopt(struct socket *sock, int level, int optname, |
| 1269 | char __user *optval, int __user *optlen) |
| 1270 | { |
| 1271 | struct smc_sock *smc; |
| 1272 | |
| 1273 | smc = smc_sk(sock->sk); |
| 1274 | /* socket options apply to the CLC socket */ |
| 1275 | return smc->clcsock->ops->getsockopt(smc->clcsock, level, optname, |
| 1276 | optval, optlen); |
| 1277 | } |
| 1278 | |
| 1279 | static int smc_ioctl(struct socket *sock, unsigned int cmd, |
| 1280 | unsigned long arg) |
| 1281 | { |
| 1282 | struct smc_sock *smc; |
| 1283 | |
| 1284 | smc = smc_sk(sock->sk); |
| 1285 | if (smc->use_fallback) |
| 1286 | return smc->clcsock->ops->ioctl(smc->clcsock, cmd, arg); |
| 1287 | else |
| 1288 | return sock_no_ioctl(sock, cmd, arg); |
| 1289 | } |
| 1290 | |
| 1291 | static ssize_t smc_sendpage(struct socket *sock, struct page *page, |
| 1292 | int offset, size_t size, int flags) |
| 1293 | { |
| 1294 | struct sock *sk = sock->sk; |
| 1295 | struct smc_sock *smc; |
| 1296 | int rc = -EPIPE; |
| 1297 | |
| 1298 | smc = smc_sk(sk); |
| 1299 | lock_sock(sk); |
| 1300 | if (sk->sk_state != SMC_ACTIVE) |
| 1301 | goto out; |
| 1302 | if (smc->use_fallback) |
| 1303 | rc = kernel_sendpage(smc->clcsock, page, offset, |
| 1304 | size, flags); |
| 1305 | else |
| 1306 | rc = sock_no_sendpage(sock, page, offset, size, flags); |
| 1307 | |
| 1308 | out: |
| 1309 | release_sock(sk); |
| 1310 | return rc; |
| 1311 | } |
| 1312 | |
| 1313 | static ssize_t smc_splice_read(struct socket *sock, loff_t *ppos, |
| 1314 | struct pipe_inode_info *pipe, size_t len, |
| 1315 | unsigned int flags) |
| 1316 | { |
| 1317 | struct sock *sk = sock->sk; |
| 1318 | struct smc_sock *smc; |
| 1319 | int rc = -ENOTCONN; |
| 1320 | |
| 1321 | smc = smc_sk(sk); |
| 1322 | lock_sock(sk); |
| 1323 | if ((sk->sk_state != SMC_ACTIVE) && (sk->sk_state != SMC_CLOSED)) |
| 1324 | goto out; |
| 1325 | if (smc->use_fallback) { |
| 1326 | rc = smc->clcsock->ops->splice_read(smc->clcsock, ppos, |
| 1327 | pipe, len, flags); |
| 1328 | } else { |
| 1329 | rc = -EOPNOTSUPP; |
| 1330 | } |
| 1331 | out: |
| 1332 | release_sock(sk); |
| 1333 | return rc; |
| 1334 | } |
| 1335 | |
| 1336 | /* must look like tcp */ |
| 1337 | static const struct proto_ops smc_sock_ops = { |
| 1338 | .family = PF_SMC, |
| 1339 | .owner = THIS_MODULE, |
| 1340 | .release = smc_release, |
| 1341 | .bind = smc_bind, |
| 1342 | .connect = smc_connect, |
| 1343 | .socketpair = sock_no_socketpair, |
| 1344 | .accept = smc_accept, |
| 1345 | .getname = smc_getname, |
| 1346 | .poll = smc_poll, |
| 1347 | .ioctl = smc_ioctl, |
| 1348 | .listen = smc_listen, |
| 1349 | .shutdown = smc_shutdown, |
| 1350 | .setsockopt = smc_setsockopt, |
| 1351 | .getsockopt = smc_getsockopt, |
| 1352 | .sendmsg = smc_sendmsg, |
| 1353 | .recvmsg = smc_recvmsg, |
| 1354 | .mmap = sock_no_mmap, |
| 1355 | .sendpage = smc_sendpage, |
| 1356 | .splice_read = smc_splice_read, |
| 1357 | }; |
| 1358 | |
| 1359 | static int smc_create(struct net *net, struct socket *sock, int protocol, |
| 1360 | int kern) |
| 1361 | { |
| 1362 | struct smc_sock *smc; |
| 1363 | struct sock *sk; |
| 1364 | int rc; |
| 1365 | |
| 1366 | rc = -ESOCKTNOSUPPORT; |
| 1367 | if (sock->type != SOCK_STREAM) |
| 1368 | goto out; |
| 1369 | |
| 1370 | rc = -EPROTONOSUPPORT; |
| 1371 | if ((protocol != IPPROTO_IP) && (protocol != IPPROTO_TCP)) |
| 1372 | goto out; |
| 1373 | |
| 1374 | rc = -ENOBUFS; |
| 1375 | sock->ops = &smc_sock_ops; |
| 1376 | sk = smc_sock_alloc(net, sock); |
| 1377 | if (!sk) |
| 1378 | goto out; |
| 1379 | |
| 1380 | /* create internal TCP socket for CLC handshake and fallback */ |
| 1381 | smc = smc_sk(sk); |
Ursula Braun | a046d57 | 2017-01-09 16:55:16 +0100 | [diff] [blame] | 1382 | smc->use_fallback = false; /* assume rdma capability first */ |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1383 | rc = sock_create_kern(net, PF_INET, SOCK_STREAM, |
| 1384 | IPPROTO_TCP, &smc->clcsock); |
| 1385 | if (rc) |
| 1386 | sk_common_release(sk); |
Ursula Braun | cd6851f | 2017-01-09 16:55:18 +0100 | [diff] [blame] | 1387 | smc->sk.sk_sndbuf = max(smc->clcsock->sk->sk_sndbuf, SMC_BUF_MIN_SIZE); |
| 1388 | 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] | 1389 | |
| 1390 | out: |
| 1391 | return rc; |
| 1392 | } |
| 1393 | |
| 1394 | static const struct net_proto_family smc_sock_family_ops = { |
| 1395 | .family = PF_SMC, |
| 1396 | .owner = THIS_MODULE, |
| 1397 | .create = smc_create, |
| 1398 | }; |
| 1399 | |
| 1400 | static int __init smc_init(void) |
| 1401 | { |
| 1402 | int rc; |
| 1403 | |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 1404 | rc = smc_pnet_init(); |
| 1405 | if (rc) |
| 1406 | return rc; |
| 1407 | |
Ursula Braun | 9bf9abe | 2017-01-09 16:55:21 +0100 | [diff] [blame] | 1408 | rc = smc_llc_init(); |
| 1409 | if (rc) { |
| 1410 | pr_err("%s: smc_llc_init fails with %d\n", __func__, rc); |
| 1411 | goto out_pnet; |
| 1412 | } |
| 1413 | |
Ursula Braun | 5f08318 | 2017-01-09 16:55:22 +0100 | [diff] [blame] | 1414 | rc = smc_cdc_init(); |
| 1415 | if (rc) { |
| 1416 | pr_err("%s: smc_cdc_init fails with %d\n", __func__, rc); |
| 1417 | goto out_pnet; |
| 1418 | } |
| 1419 | |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1420 | rc = proto_register(&smc_proto, 1); |
| 1421 | if (rc) { |
| 1422 | pr_err("%s: proto_register fails with %d\n", __func__, rc); |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 1423 | goto out_pnet; |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1424 | } |
| 1425 | |
| 1426 | rc = sock_register(&smc_sock_family_ops); |
| 1427 | if (rc) { |
| 1428 | pr_err("%s: sock_register fails with %d\n", __func__, rc); |
| 1429 | goto out_proto; |
| 1430 | } |
Ursula Braun | f16a7dd | 2017-01-09 16:55:26 +0100 | [diff] [blame] | 1431 | INIT_HLIST_HEAD(&smc_v4_hashinfo.ht); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1432 | |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 1433 | rc = smc_ib_register_client(); |
| 1434 | if (rc) { |
| 1435 | pr_err("%s: ib_register fails with %d\n", __func__, rc); |
| 1436 | goto out_sock; |
| 1437 | } |
| 1438 | |
Ursula Braun | c5c1cc9 | 2017-10-25 11:01:46 +0200 | [diff] [blame] | 1439 | static_branch_enable(&tcp_have_smc); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1440 | return 0; |
| 1441 | |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 1442 | out_sock: |
| 1443 | sock_unregister(PF_SMC); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1444 | out_proto: |
| 1445 | proto_unregister(&smc_proto); |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 1446 | out_pnet: |
| 1447 | smc_pnet_exit(); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1448 | return rc; |
| 1449 | } |
| 1450 | |
| 1451 | static void __exit smc_exit(void) |
| 1452 | { |
Ursula Braun | 0cfdd8f | 2017-01-09 16:55:17 +0100 | [diff] [blame] | 1453 | struct smc_link_group *lgr, *lg; |
| 1454 | LIST_HEAD(lgr_freeing_list); |
| 1455 | |
| 1456 | spin_lock_bh(&smc_lgr_list.lock); |
| 1457 | if (!list_empty(&smc_lgr_list.list)) |
| 1458 | list_splice_init(&smc_lgr_list.list, &lgr_freeing_list); |
| 1459 | spin_unlock_bh(&smc_lgr_list.lock); |
| 1460 | list_for_each_entry_safe(lgr, lg, &lgr_freeing_list, list) { |
| 1461 | list_del_init(&lgr->list); |
| 1462 | smc_lgr_free(lgr); /* free link group */ |
| 1463 | } |
Ursula Braun | c5c1cc9 | 2017-10-25 11:01:46 +0200 | [diff] [blame] | 1464 | static_branch_disable(&tcp_have_smc); |
Ursula Braun | a4cf044 | 2017-01-09 16:55:14 +0100 | [diff] [blame] | 1465 | smc_ib_unregister_client(); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1466 | sock_unregister(PF_SMC); |
| 1467 | proto_unregister(&smc_proto); |
Thomas Richter | 6812baa | 2017-01-09 16:55:15 +0100 | [diff] [blame] | 1468 | smc_pnet_exit(); |
Ursula Braun | ac71387 | 2017-01-09 16:55:13 +0100 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | module_init(smc_init); |
| 1472 | module_exit(smc_exit); |
| 1473 | |
| 1474 | MODULE_AUTHOR("Ursula Braun <ubraun@linux.vnet.ibm.com>"); |
| 1475 | MODULE_DESCRIPTION("smc socket address family"); |
| 1476 | MODULE_LICENSE("GPL"); |
| 1477 | MODULE_ALIAS_NETPROTO(PF_SMC); |