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