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