Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
a246b010 CL |
2 | /* |
3 | * linux/net/sunrpc/xprtsock.c | |
4 | * | |
5 | * Client-side transport implementation for sockets. | |
6 | * | |
113aa838 AC |
7 | * TCP callback races fixes (C) 1998 Red Hat |
8 | * TCP send fixes (C) 1998 Red Hat | |
a246b010 CL |
9 | * TCP NFS related read + write fixes |
10 | * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie> | |
11 | * | |
12 | * Rewrite of larges part of the code in order to stabilize TCP stuff. | |
13 | * Fix behaviour when socket buffer is full. | |
14 | * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no> | |
55aa4f58 CL |
15 | * |
16 | * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com> | |
8f9d5b1a CL |
17 | * |
18 | * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005. | |
19 | * <gilles.quillard@bull.net> | |
a246b010 CL |
20 | */ |
21 | ||
22 | #include <linux/types.h> | |
176e21ee | 23 | #include <linux/string.h> |
a246b010 | 24 | #include <linux/slab.h> |
bc25571e | 25 | #include <linux/module.h> |
a246b010 | 26 | #include <linux/capability.h> |
a246b010 CL |
27 | #include <linux/pagemap.h> |
28 | #include <linux/errno.h> | |
29 | #include <linux/socket.h> | |
30 | #include <linux/in.h> | |
31 | #include <linux/net.h> | |
32 | #include <linux/mm.h> | |
176e21ee | 33 | #include <linux/un.h> |
a246b010 CL |
34 | #include <linux/udp.h> |
35 | #include <linux/tcp.h> | |
36 | #include <linux/sunrpc/clnt.h> | |
5976687a | 37 | #include <linux/sunrpc/addr.h> |
02107148 | 38 | #include <linux/sunrpc/sched.h> |
4cfc7e60 | 39 | #include <linux/sunrpc/svcsock.h> |
49c36fcc | 40 | #include <linux/sunrpc/xprtsock.h> |
a246b010 | 41 | #include <linux/file.h> |
9e00abc3 | 42 | #ifdef CONFIG_SUNRPC_BACKCHANNEL |
44b98efd RL |
43 | #include <linux/sunrpc/bc_xprt.h> |
44 | #endif | |
a246b010 CL |
45 | |
46 | #include <net/sock.h> | |
47 | #include <net/checksum.h> | |
48 | #include <net/udp.h> | |
49 | #include <net/tcp.h> | |
277e4ab7 | 50 | #include <linux/bvec.h> |
6a829eb8 | 51 | #include <linux/highmem.h> |
277e4ab7 | 52 | #include <linux/uio.h> |
a1231fda | 53 | #include <linux/sched/mm.h> |
a246b010 | 54 | |
40b5ea0c TM |
55 | #include <trace/events/sunrpc.h> |
56 | ||
9e55eef4 | 57 | #include "socklib.h" |
4cfc7e60 | 58 | #include "sunrpc.h" |
176e21ee CL |
59 | |
60 | static void xs_close(struct rpc_xprt *xprt); | |
3b21f757 | 61 | static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock); |
7196dbb0 TM |
62 | static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, |
63 | struct socket *sock); | |
176e21ee | 64 | |
c556b754 CL |
65 | /* |
66 | * xprtsock tunables | |
67 | */ | |
09acfea5 TM |
68 | static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE; |
69 | static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE; | |
70 | static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE; | |
c556b754 | 71 | |
09acfea5 TM |
72 | static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT; |
73 | static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT; | |
c556b754 | 74 | |
7d1e8255 | 75 | #define XS_TCP_LINGER_TO (15U * HZ) |
25fe6142 | 76 | static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO; |
7d1e8255 | 77 | |
fbf76683 CL |
78 | /* |
79 | * We can register our own files under /proc/sys/sunrpc by | |
80 | * calling register_sysctl_table() again. The files in that | |
81 | * directory become the union of all files registered there. | |
82 | * | |
83 | * We simply need to make sure that we don't collide with | |
84 | * someone else's file names! | |
85 | */ | |
86 | ||
fbf76683 CL |
87 | static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE; |
88 | static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE; | |
d9ba131d | 89 | static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT; |
fbf76683 CL |
90 | static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT; |
91 | static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT; | |
92 | ||
93 | static struct ctl_table_header *sunrpc_table_header; | |
94 | ||
d3abc739 OK |
95 | static struct xprt_class xs_local_transport; |
96 | static struct xprt_class xs_udp_transport; | |
97 | static struct xprt_class xs_tcp_transport; | |
98 | static struct xprt_class xs_bc_tcp_transport; | |
99 | ||
fbf76683 CL |
100 | /* |
101 | * FIXME: changing the UDP slot table size should also resize the UDP | |
102 | * socket buffers for existing UDP transports | |
103 | */ | |
fe2c6338 | 104 | static struct ctl_table xs_tunables_table[] = { |
fbf76683 | 105 | { |
fbf76683 CL |
106 | .procname = "udp_slot_table_entries", |
107 | .data = &xprt_udp_slot_table_entries, | |
108 | .maxlen = sizeof(unsigned int), | |
109 | .mode = 0644, | |
6d456111 | 110 | .proc_handler = proc_dointvec_minmax, |
fbf76683 CL |
111 | .extra1 = &min_slot_table_size, |
112 | .extra2 = &max_slot_table_size | |
113 | }, | |
114 | { | |
fbf76683 CL |
115 | .procname = "tcp_slot_table_entries", |
116 | .data = &xprt_tcp_slot_table_entries, | |
117 | .maxlen = sizeof(unsigned int), | |
118 | .mode = 0644, | |
6d456111 | 119 | .proc_handler = proc_dointvec_minmax, |
fbf76683 CL |
120 | .extra1 = &min_slot_table_size, |
121 | .extra2 = &max_slot_table_size | |
122 | }, | |
d9ba131d TM |
123 | { |
124 | .procname = "tcp_max_slot_table_entries", | |
125 | .data = &xprt_max_tcp_slot_table_entries, | |
126 | .maxlen = sizeof(unsigned int), | |
127 | .mode = 0644, | |
128 | .proc_handler = proc_dointvec_minmax, | |
129 | .extra1 = &min_slot_table_size, | |
130 | .extra2 = &max_tcp_slot_table_limit | |
131 | }, | |
fbf76683 | 132 | { |
fbf76683 CL |
133 | .procname = "min_resvport", |
134 | .data = &xprt_min_resvport, | |
135 | .maxlen = sizeof(unsigned int), | |
136 | .mode = 0644, | |
6d456111 | 137 | .proc_handler = proc_dointvec_minmax, |
fbf76683 | 138 | .extra1 = &xprt_min_resvport_limit, |
826799e6 | 139 | .extra2 = &xprt_max_resvport_limit |
fbf76683 CL |
140 | }, |
141 | { | |
fbf76683 CL |
142 | .procname = "max_resvport", |
143 | .data = &xprt_max_resvport, | |
144 | .maxlen = sizeof(unsigned int), | |
145 | .mode = 0644, | |
6d456111 | 146 | .proc_handler = proc_dointvec_minmax, |
826799e6 | 147 | .extra1 = &xprt_min_resvport_limit, |
fbf76683 CL |
148 | .extra2 = &xprt_max_resvport_limit |
149 | }, | |
25fe6142 TM |
150 | { |
151 | .procname = "tcp_fin_timeout", | |
152 | .data = &xs_tcp_fin_timeout, | |
153 | .maxlen = sizeof(xs_tcp_fin_timeout), | |
154 | .mode = 0644, | |
6d456111 | 155 | .proc_handler = proc_dointvec_jiffies, |
fbf76683 | 156 | }, |
f8572d8f | 157 | { }, |
fbf76683 CL |
158 | }; |
159 | ||
fe2c6338 | 160 | static struct ctl_table sunrpc_table[] = { |
fbf76683 | 161 | { |
fbf76683 CL |
162 | .procname = "sunrpc", |
163 | .mode = 0555, | |
164 | .child = xs_tunables_table | |
165 | }, | |
f8572d8f | 166 | { }, |
fbf76683 CL |
167 | }; |
168 | ||
03bf4b70 CL |
169 | /* |
170 | * Wait duration for a reply from the RPC portmapper. | |
171 | */ | |
172 | #define XS_BIND_TO (60U * HZ) | |
173 | ||
174 | /* | |
175 | * Delay if a UDP socket connect error occurs. This is most likely some | |
176 | * kind of resource problem on the local host. | |
177 | */ | |
178 | #define XS_UDP_REEST_TO (2U * HZ) | |
179 | ||
180 | /* | |
181 | * The reestablish timeout allows clients to delay for a bit before attempting | |
182 | * to reconnect to a server that just dropped our connection. | |
183 | * | |
184 | * We implement an exponential backoff when trying to reestablish a TCP | |
185 | * transport connection with the server. Some servers like to drop a TCP | |
186 | * connection when they are overworked, so we start with a short timeout and | |
187 | * increase over time if the server is down or not responding. | |
188 | */ | |
189 | #define XS_TCP_INIT_REEST_TO (3U * HZ) | |
03bf4b70 CL |
190 | |
191 | /* | |
192 | * TCP idle timeout; client drops the transport socket if it is idle | |
193 | * for this long. Note that we also timeout UDP sockets to prevent | |
194 | * holding port numbers when there is no RPC traffic. | |
195 | */ | |
196 | #define XS_IDLE_DISC_TO (5U * 60 * HZ) | |
197 | ||
f895b252 | 198 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) |
a246b010 | 199 | # undef RPC_DEBUG_DATA |
9903cd1c | 200 | # define RPCDBG_FACILITY RPCDBG_TRANS |
a246b010 CL |
201 | #endif |
202 | ||
a246b010 | 203 | #ifdef RPC_DEBUG_DATA |
9903cd1c | 204 | static void xs_pktdump(char *msg, u32 *packet, unsigned int count) |
a246b010 | 205 | { |
9903cd1c CL |
206 | u8 *buf = (u8 *) packet; |
207 | int j; | |
a246b010 | 208 | |
46121cf7 | 209 | dprintk("RPC: %s\n", msg); |
a246b010 CL |
210 | for (j = 0; j < count && j < 128; j += 4) { |
211 | if (!(j & 31)) { | |
212 | if (j) | |
213 | dprintk("\n"); | |
214 | dprintk("0x%04x ", j); | |
215 | } | |
216 | dprintk("%02x%02x%02x%02x ", | |
217 | buf[j], buf[j+1], buf[j+2], buf[j+3]); | |
218 | } | |
219 | dprintk("\n"); | |
220 | } | |
221 | #else | |
9903cd1c | 222 | static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count) |
a246b010 CL |
223 | { |
224 | /* NOP */ | |
225 | } | |
226 | #endif | |
227 | ||
2118071d TM |
228 | static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) |
229 | { | |
230 | return (struct rpc_xprt *) sk->sk_user_data; | |
231 | } | |
232 | ||
95392c59 CL |
233 | static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt) |
234 | { | |
235 | return (struct sockaddr *) &xprt->addr; | |
236 | } | |
237 | ||
176e21ee CL |
238 | static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt) |
239 | { | |
240 | return (struct sockaddr_un *) &xprt->addr; | |
241 | } | |
242 | ||
95392c59 | 243 | static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt) |
edb267a6 | 244 | { |
95392c59 CL |
245 | return (struct sockaddr_in *) &xprt->addr; |
246 | } | |
247 | ||
248 | static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt) | |
249 | { | |
250 | return (struct sockaddr_in6 *) &xprt->addr; | |
251 | } | |
252 | ||
c877b849 | 253 | static void xs_format_common_peer_addresses(struct rpc_xprt *xprt) |
edb267a6 | 254 | { |
c877b849 | 255 | struct sockaddr *sap = xs_addr(xprt); |
9dc3b095 CL |
256 | struct sockaddr_in6 *sin6; |
257 | struct sockaddr_in *sin; | |
176e21ee | 258 | struct sockaddr_un *sun; |
c877b849 | 259 | char buf[128]; |
edb267a6 | 260 | |
9dc3b095 | 261 | switch (sap->sa_family) { |
176e21ee CL |
262 | case AF_LOCAL: |
263 | sun = xs_addr_un(xprt); | |
264 | strlcpy(buf, sun->sun_path, sizeof(buf)); | |
265 | xprt->address_strings[RPC_DISPLAY_ADDR] = | |
266 | kstrdup(buf, GFP_KERNEL); | |
267 | break; | |
9dc3b095 | 268 | case AF_INET: |
176e21ee CL |
269 | (void)rpc_ntop(sap, buf, sizeof(buf)); |
270 | xprt->address_strings[RPC_DISPLAY_ADDR] = | |
271 | kstrdup(buf, GFP_KERNEL); | |
9dc3b095 | 272 | sin = xs_addr_in(xprt); |
fc0b5791 | 273 | snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr)); |
9dc3b095 CL |
274 | break; |
275 | case AF_INET6: | |
176e21ee CL |
276 | (void)rpc_ntop(sap, buf, sizeof(buf)); |
277 | xprt->address_strings[RPC_DISPLAY_ADDR] = | |
278 | kstrdup(buf, GFP_KERNEL); | |
9dc3b095 | 279 | sin6 = xs_addr_in6(xprt); |
fc0b5791 | 280 | snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr); |
9dc3b095 CL |
281 | break; |
282 | default: | |
283 | BUG(); | |
756805e7 | 284 | } |
176e21ee | 285 | |
9dc3b095 | 286 | xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL); |
edb267a6 CL |
287 | } |
288 | ||
9dc3b095 | 289 | static void xs_format_common_peer_ports(struct rpc_xprt *xprt) |
4b6473fb | 290 | { |
9dc3b095 CL |
291 | struct sockaddr *sap = xs_addr(xprt); |
292 | char buf[128]; | |
4b6473fb | 293 | |
81160e66 | 294 | snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap)); |
c877b849 | 295 | xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL); |
4b6473fb | 296 | |
81160e66 | 297 | snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap)); |
c877b849 CL |
298 | xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL); |
299 | } | |
4b6473fb | 300 | |
9dc3b095 CL |
301 | static void xs_format_peer_addresses(struct rpc_xprt *xprt, |
302 | const char *protocol, | |
303 | const char *netid) | |
c877b849 | 304 | { |
b454ae90 | 305 | xprt->address_strings[RPC_DISPLAY_PROTO] = protocol; |
b454ae90 | 306 | xprt->address_strings[RPC_DISPLAY_NETID] = netid; |
c877b849 | 307 | xs_format_common_peer_addresses(xprt); |
9dc3b095 | 308 | xs_format_common_peer_ports(xprt); |
edb267a6 | 309 | } |
4b6473fb | 310 | |
9dc3b095 | 311 | static void xs_update_peer_port(struct rpc_xprt *xprt) |
4b6473fb | 312 | { |
9dc3b095 CL |
313 | kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]); |
314 | kfree(xprt->address_strings[RPC_DISPLAY_PORT]); | |
4417c8c4 | 315 | |
9dc3b095 | 316 | xs_format_common_peer_ports(xprt); |
edb267a6 CL |
317 | } |
318 | ||
319 | static void xs_free_peer_addresses(struct rpc_xprt *xprt) | |
320 | { | |
33e01dc7 CL |
321 | unsigned int i; |
322 | ||
323 | for (i = 0; i < RPC_DISPLAY_MAX; i++) | |
324 | switch (i) { | |
325 | case RPC_DISPLAY_PROTO: | |
326 | case RPC_DISPLAY_NETID: | |
327 | continue; | |
328 | default: | |
329 | kfree(xprt->address_strings[i]); | |
330 | } | |
edb267a6 CL |
331 | } |
332 | ||
277e4ab7 TM |
333 | static size_t |
334 | xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp) | |
335 | { | |
336 | size_t i,n; | |
337 | ||
16e5e90f | 338 | if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES)) |
277e4ab7 | 339 | return want; |
277e4ab7 TM |
340 | n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT; |
341 | for (i = 0; i < n; i++) { | |
342 | if (buf->pages[i]) | |
343 | continue; | |
344 | buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp); | |
345 | if (!buf->pages[i]) { | |
16e5e90f TM |
346 | i *= PAGE_SIZE; |
347 | return i > buf->page_base ? i - buf->page_base : 0; | |
277e4ab7 TM |
348 | } |
349 | } | |
350 | return want; | |
351 | } | |
352 | ||
353 | static ssize_t | |
354 | xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek) | |
355 | { | |
356 | ssize_t ret; | |
357 | if (seek != 0) | |
358 | iov_iter_advance(&msg->msg_iter, seek); | |
359 | ret = sock_recvmsg(sock, msg, flags); | |
360 | return ret > 0 ? ret + seek : ret; | |
361 | } | |
362 | ||
363 | static ssize_t | |
364 | xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags, | |
365 | struct kvec *kvec, size_t count, size_t seek) | |
366 | { | |
0e9b4a82 | 367 | iov_iter_kvec(&msg->msg_iter, READ, kvec, 1, count); |
277e4ab7 TM |
368 | return xs_sock_recvmsg(sock, msg, flags, seek); |
369 | } | |
370 | ||
371 | static ssize_t | |
372 | xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags, | |
373 | struct bio_vec *bvec, unsigned long nr, size_t count, | |
374 | size_t seek) | |
375 | { | |
0e9b4a82 | 376 | iov_iter_bvec(&msg->msg_iter, READ, bvec, nr, count); |
277e4ab7 TM |
377 | return xs_sock_recvmsg(sock, msg, flags, seek); |
378 | } | |
379 | ||
380 | static ssize_t | |
381 | xs_read_discard(struct socket *sock, struct msghdr *msg, int flags, | |
382 | size_t count) | |
383 | { | |
b76a5afd TM |
384 | iov_iter_discard(&msg->msg_iter, READ, count); |
385 | return sock_recvmsg(sock, msg, flags); | |
277e4ab7 TM |
386 | } |
387 | ||
6a829eb8 TM |
388 | #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE |
389 | static void | |
390 | xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek) | |
391 | { | |
392 | struct bvec_iter bi = { | |
393 | .bi_size = count, | |
394 | }; | |
395 | struct bio_vec bv; | |
396 | ||
397 | bvec_iter_advance(bvec, &bi, seek & PAGE_MASK); | |
398 | for_each_bvec(bv, bvec, bi, bi) | |
399 | flush_dcache_page(bv.bv_page); | |
400 | } | |
401 | #else | |
402 | static inline void | |
403 | xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek) | |
404 | { | |
405 | } | |
406 | #endif | |
407 | ||
277e4ab7 TM |
408 | static ssize_t |
409 | xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags, | |
410 | struct xdr_buf *buf, size_t count, size_t seek, size_t *read) | |
411 | { | |
412 | size_t want, seek_init = seek, offset = 0; | |
413 | ssize_t ret; | |
414 | ||
e92053a5 TM |
415 | want = min_t(size_t, count, buf->head[0].iov_len); |
416 | if (seek < want) { | |
277e4ab7 TM |
417 | ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek); |
418 | if (ret <= 0) | |
419 | goto sock_err; | |
420 | offset += ret; | |
421 | if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) | |
422 | goto out; | |
423 | if (ret != want) | |
c4433055 | 424 | goto out; |
277e4ab7 TM |
425 | seek = 0; |
426 | } else { | |
e92053a5 TM |
427 | seek -= want; |
428 | offset += want; | |
277e4ab7 | 429 | } |
16e5e90f | 430 | |
b2648015 TM |
431 | want = xs_alloc_sparse_pages( |
432 | buf, min_t(size_t, count - offset, buf->page_len), | |
433 | GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN); | |
16e5e90f | 434 | if (seek < want) { |
277e4ab7 TM |
435 | ret = xs_read_bvec(sock, msg, flags, buf->bvec, |
436 | xdr_buf_pagecount(buf), | |
437 | want + buf->page_base, | |
438 | seek + buf->page_base); | |
439 | if (ret <= 0) | |
440 | goto sock_err; | |
6a829eb8 | 441 | xs_flush_bvec(buf->bvec, ret, seek + buf->page_base); |
ac9645c8 DA |
442 | ret -= buf->page_base; |
443 | offset += ret; | |
277e4ab7 TM |
444 | if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) |
445 | goto out; | |
446 | if (ret != want) | |
c4433055 | 447 | goto out; |
277e4ab7 TM |
448 | seek = 0; |
449 | } else { | |
16e5e90f TM |
450 | seek -= want; |
451 | offset += want; | |
277e4ab7 | 452 | } |
16e5e90f | 453 | |
e92053a5 TM |
454 | want = min_t(size_t, count - offset, buf->tail[0].iov_len); |
455 | if (seek < want) { | |
277e4ab7 TM |
456 | ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek); |
457 | if (ret <= 0) | |
458 | goto sock_err; | |
459 | offset += ret; | |
460 | if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC)) | |
461 | goto out; | |
462 | if (ret != want) | |
c4433055 | 463 | goto out; |
9734ad57 | 464 | } else if (offset < seek_init) |
e92053a5 | 465 | offset = seek_init; |
277e4ab7 | 466 | ret = -EMSGSIZE; |
277e4ab7 TM |
467 | out: |
468 | *read = offset - seek_init; | |
469 | return ret; | |
277e4ab7 TM |
470 | sock_err: |
471 | offset += seek; | |
472 | goto out; | |
473 | } | |
474 | ||
475 | static void | |
476 | xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf) | |
477 | { | |
478 | if (!transport->recv.copied) { | |
479 | if (buf->head[0].iov_len >= transport->recv.offset) | |
480 | memcpy(buf->head[0].iov_base, | |
481 | &transport->recv.xid, | |
482 | transport->recv.offset); | |
483 | transport->recv.copied = transport->recv.offset; | |
484 | } | |
485 | } | |
486 | ||
487 | static bool | |
488 | xs_read_stream_request_done(struct sock_xprt *transport) | |
489 | { | |
490 | return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT); | |
491 | } | |
492 | ||
e92053a5 TM |
493 | static void |
494 | xs_read_stream_check_eor(struct sock_xprt *transport, | |
495 | struct msghdr *msg) | |
496 | { | |
497 | if (xs_read_stream_request_done(transport)) | |
498 | msg->msg_flags |= MSG_EOR; | |
499 | } | |
500 | ||
277e4ab7 TM |
501 | static ssize_t |
502 | xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg, | |
503 | int flags, struct rpc_rqst *req) | |
504 | { | |
505 | struct xdr_buf *buf = &req->rq_private_buf; | |
3f649ab7 KC |
506 | size_t want, read; |
507 | ssize_t ret; | |
277e4ab7 TM |
508 | |
509 | xs_read_header(transport, buf); | |
510 | ||
511 | want = transport->recv.len - transport->recv.offset; | |
e92053a5 TM |
512 | if (want != 0) { |
513 | ret = xs_read_xdr_buf(transport->sock, msg, flags, buf, | |
514 | transport->recv.copied + want, | |
515 | transport->recv.copied, | |
516 | &read); | |
517 | transport->recv.offset += read; | |
518 | transport->recv.copied += read; | |
727fcc64 | 519 | } |
e92053a5 | 520 | |
727fcc64 | 521 | if (transport->recv.offset == transport->recv.len) |
e92053a5 | 522 | xs_read_stream_check_eor(transport, msg); |
277e4ab7 | 523 | |
e92053a5 TM |
524 | if (want == 0) |
525 | return 0; | |
526 | ||
277e4ab7 | 527 | switch (ret) { |
c4433055 TM |
528 | default: |
529 | break; | |
26781eab | 530 | case -EFAULT: |
277e4ab7 | 531 | case -EMSGSIZE: |
26781eab | 532 | msg->msg_flags |= MSG_TRUNC; |
c4433055 | 533 | return read; |
277e4ab7 TM |
534 | case 0: |
535 | return -ESHUTDOWN; | |
277e4ab7 | 536 | } |
c4433055 | 537 | return ret < 0 ? ret : read; |
277e4ab7 TM |
538 | } |
539 | ||
540 | static size_t | |
541 | xs_read_stream_headersize(bool isfrag) | |
542 | { | |
543 | if (isfrag) | |
544 | return sizeof(__be32); | |
545 | return 3 * sizeof(__be32); | |
546 | } | |
547 | ||
548 | static ssize_t | |
549 | xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg, | |
550 | int flags, size_t want, size_t seek) | |
551 | { | |
552 | struct kvec kvec = { | |
553 | .iov_base = &transport->recv.fraghdr, | |
554 | .iov_len = want, | |
555 | }; | |
556 | return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek); | |
557 | } | |
558 | ||
559 | #if defined(CONFIG_SUNRPC_BACKCHANNEL) | |
560 | static ssize_t | |
561 | xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) | |
562 | { | |
563 | struct rpc_xprt *xprt = &transport->xprt; | |
564 | struct rpc_rqst *req; | |
565 | ssize_t ret; | |
566 | ||
98b5cee3 BC |
567 | /* Is this transport associated with the backchannel? */ |
568 | if (!xprt->bc_serv) | |
569 | return -ESHUTDOWN; | |
570 | ||
277e4ab7 TM |
571 | /* Look up and lock the request corresponding to the given XID */ |
572 | req = xprt_lookup_bc_request(xprt, transport->recv.xid); | |
573 | if (!req) { | |
574 | printk(KERN_WARNING "Callback slot table overflowed\n"); | |
575 | return -ESHUTDOWN; | |
576 | } | |
45835a63 TM |
577 | if (transport->recv.copied && !req->rq_private_buf.len) |
578 | return -ESHUTDOWN; | |
277e4ab7 TM |
579 | |
580 | ret = xs_read_stream_request(transport, msg, flags, req); | |
581 | if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) | |
c4433055 | 582 | xprt_complete_bc_request(req, transport->recv.copied); |
45835a63 TM |
583 | else |
584 | req->rq_private_buf.len = transport->recv.copied; | |
277e4ab7 TM |
585 | |
586 | return ret; | |
587 | } | |
588 | #else /* CONFIG_SUNRPC_BACKCHANNEL */ | |
589 | static ssize_t | |
590 | xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags) | |
591 | { | |
592 | return -ESHUTDOWN; | |
593 | } | |
594 | #endif /* CONFIG_SUNRPC_BACKCHANNEL */ | |
595 | ||
596 | static ssize_t | |
597 | xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags) | |
598 | { | |
599 | struct rpc_xprt *xprt = &transport->xprt; | |
600 | struct rpc_rqst *req; | |
601 | ssize_t ret = 0; | |
602 | ||
603 | /* Look up and lock the request corresponding to the given XID */ | |
604 | spin_lock(&xprt->queue_lock); | |
605 | req = xprt_lookup_rqst(xprt, transport->recv.xid); | |
45835a63 | 606 | if (!req || (transport->recv.copied && !req->rq_private_buf.len)) { |
277e4ab7 TM |
607 | msg->msg_flags |= MSG_TRUNC; |
608 | goto out; | |
609 | } | |
610 | xprt_pin_rqst(req); | |
611 | spin_unlock(&xprt->queue_lock); | |
612 | ||
613 | ret = xs_read_stream_request(transport, msg, flags, req); | |
614 | ||
615 | spin_lock(&xprt->queue_lock); | |
616 | if (msg->msg_flags & (MSG_EOR|MSG_TRUNC)) | |
c4433055 | 617 | xprt_complete_rqst(req->rq_task, transport->recv.copied); |
45835a63 TM |
618 | else |
619 | req->rq_private_buf.len = transport->recv.copied; | |
277e4ab7 TM |
620 | xprt_unpin_rqst(req); |
621 | out: | |
622 | spin_unlock(&xprt->queue_lock); | |
623 | return ret; | |
624 | } | |
625 | ||
626 | static ssize_t | |
627 | xs_read_stream(struct sock_xprt *transport, int flags) | |
628 | { | |
629 | struct msghdr msg = { 0 }; | |
630 | size_t want, read = 0; | |
631 | ssize_t ret = 0; | |
632 | ||
633 | if (transport->recv.len == 0) { | |
634 | want = xs_read_stream_headersize(transport->recv.copied != 0); | |
635 | ret = xs_read_stream_header(transport, &msg, flags, want, | |
636 | transport->recv.offset); | |
637 | if (ret <= 0) | |
638 | goto out_err; | |
639 | transport->recv.offset = ret; | |
c4433055 TM |
640 | if (transport->recv.offset != want) |
641 | return transport->recv.offset; | |
277e4ab7 TM |
642 | transport->recv.len = be32_to_cpu(transport->recv.fraghdr) & |
643 | RPC_FRAGMENT_SIZE_MASK; | |
644 | transport->recv.offset -= sizeof(transport->recv.fraghdr); | |
645 | read = ret; | |
646 | } | |
647 | ||
648 | switch (be32_to_cpu(transport->recv.calldir)) { | |
c4433055 TM |
649 | default: |
650 | msg.msg_flags |= MSG_TRUNC; | |
651 | break; | |
277e4ab7 TM |
652 | case RPC_CALL: |
653 | ret = xs_read_stream_call(transport, &msg, flags); | |
654 | break; | |
655 | case RPC_REPLY: | |
656 | ret = xs_read_stream_reply(transport, &msg, flags); | |
657 | } | |
658 | if (msg.msg_flags & MSG_TRUNC) { | |
659 | transport->recv.calldir = cpu_to_be32(-1); | |
660 | transport->recv.copied = -1; | |
661 | } | |
662 | if (ret < 0) | |
663 | goto out_err; | |
664 | read += ret; | |
665 | if (transport->recv.offset < transport->recv.len) { | |
c4433055 TM |
666 | if (!(msg.msg_flags & MSG_TRUNC)) |
667 | return read; | |
b76a5afd | 668 | msg.msg_flags = 0; |
277e4ab7 TM |
669 | ret = xs_read_discard(transport->sock, &msg, flags, |
670 | transport->recv.len - transport->recv.offset); | |
671 | if (ret <= 0) | |
672 | goto out_err; | |
673 | transport->recv.offset += ret; | |
674 | read += ret; | |
675 | if (transport->recv.offset != transport->recv.len) | |
c4433055 | 676 | return read; |
277e4ab7 TM |
677 | } |
678 | if (xs_read_stream_request_done(transport)) { | |
c50b8ee0 | 679 | trace_xs_stream_read_request(transport); |
277e4ab7 TM |
680 | transport->recv.copied = 0; |
681 | } | |
682 | transport->recv.offset = 0; | |
683 | transport->recv.len = 0; | |
684 | return read; | |
685 | out_err: | |
79462857 | 686 | return ret != 0 ? ret : -ESHUTDOWN; |
277e4ab7 TM |
687 | } |
688 | ||
0ffe86f4 TM |
689 | static __poll_t xs_poll_socket(struct sock_xprt *transport) |
690 | { | |
a73881c9 TM |
691 | return transport->sock->ops->poll(transport->file, transport->sock, |
692 | NULL); | |
0ffe86f4 TM |
693 | } |
694 | ||
695 | static bool xs_poll_socket_readable(struct sock_xprt *transport) | |
696 | { | |
697 | __poll_t events = xs_poll_socket(transport); | |
698 | ||
699 | return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP); | |
700 | } | |
701 | ||
702 | static void xs_poll_check_readable(struct sock_xprt *transport) | |
703 | { | |
704 | ||
705 | clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); | |
706 | if (!xs_poll_socket_readable(transport)) | |
707 | return; | |
708 | if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) | |
709 | queue_work(xprtiod_workqueue, &transport->recv_worker); | |
710 | } | |
711 | ||
c50b8ee0 TM |
712 | static void xs_stream_data_receive(struct sock_xprt *transport) |
713 | { | |
714 | size_t read = 0; | |
715 | ssize_t ret = 0; | |
716 | ||
717 | mutex_lock(&transport->recv_mutex); | |
718 | if (transport->sock == NULL) | |
719 | goto out; | |
c50b8ee0 TM |
720 | for (;;) { |
721 | ret = xs_read_stream(transport, MSG_DONTWAIT); | |
c4433055 | 722 | if (ret < 0) |
c50b8ee0 TM |
723 | break; |
724 | read += ret; | |
725 | cond_resched(); | |
726 | } | |
5f52a9d4 TM |
727 | if (ret == -ESHUTDOWN) |
728 | kernel_sock_shutdown(transport->sock, SHUT_RDWR); | |
729 | else | |
730 | xs_poll_check_readable(transport); | |
c50b8ee0 TM |
731 | out: |
732 | mutex_unlock(&transport->recv_mutex); | |
733 | trace_xs_stream_read_data(&transport->xprt, ret, read); | |
734 | } | |
735 | ||
736 | static void xs_stream_data_receive_workfn(struct work_struct *work) | |
737 | { | |
738 | struct sock_xprt *transport = | |
739 | container_of(work, struct sock_xprt, recv_worker); | |
a1231fda TM |
740 | unsigned int pflags = memalloc_nofs_save(); |
741 | ||
c50b8ee0 | 742 | xs_stream_data_receive(transport); |
a1231fda | 743 | memalloc_nofs_restore(pflags); |
c50b8ee0 TM |
744 | } |
745 | ||
550aebfe TM |
746 | static void |
747 | xs_stream_reset_connect(struct sock_xprt *transport) | |
748 | { | |
749 | transport->recv.offset = 0; | |
750 | transport->recv.len = 0; | |
751 | transport->recv.copied = 0; | |
752 | transport->xmit.offset = 0; | |
ae053551 TM |
753 | } |
754 | ||
755 | static void | |
756 | xs_stream_start_connect(struct sock_xprt *transport) | |
757 | { | |
550aebfe TM |
758 | transport->xprt.stat.connect_count++; |
759 | transport->xprt.stat.connect_start = jiffies; | |
760 | } | |
761 | ||
b4b5cc85 CL |
762 | #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL) |
763 | ||
9903cd1c | 764 | /** |
c544577d | 765 | * xs_nospace - handle transmit was incomplete |
50f484e2 | 766 | * @req: pointer to RPC request |
7496b59f | 767 | * @transport: pointer to struct sock_xprt |
9903cd1c | 768 | * |
a246b010 | 769 | */ |
7496b59f | 770 | static int xs_nospace(struct rpc_rqst *req, struct sock_xprt *transport) |
a246b010 | 771 | { |
7496b59f | 772 | struct rpc_xprt *xprt = &transport->xprt; |
06ea0bfe | 773 | struct sock *sk = transport->inet; |
24ca9a84 | 774 | int ret = -EAGAIN; |
a246b010 | 775 | |
015747d2 | 776 | trace_rpc_socket_nospace(req, transport); |
262965f5 | 777 | |
b6ddf64f | 778 | /* Protect against races with write_space */ |
b5e92419 | 779 | spin_lock(&xprt->transport_lock); |
b6ddf64f TM |
780 | |
781 | /* Don't race with disconnect */ | |
782 | if (xprt_connected(xprt)) { | |
13331a55 | 783 | /* wait for more buffer space */ |
2790a624 | 784 | set_bit(XPRT_SOCK_NOSPACE, &transport->sock_state); |
7496b59f | 785 | set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
13331a55 | 786 | sk->sk_write_pending++; |
c544577d | 787 | xprt_wait_for_buffer_space(xprt); |
13331a55 | 788 | } else |
5e3771ce | 789 | ret = -ENOTCONN; |
262965f5 | 790 | |
b5e92419 | 791 | spin_unlock(&xprt->transport_lock); |
7496b59f TM |
792 | return ret; |
793 | } | |
06ea0bfe | 794 | |
7496b59f TM |
795 | static int xs_sock_nospace(struct rpc_rqst *req) |
796 | { | |
797 | struct sock_xprt *transport = | |
798 | container_of(req->rq_xprt, struct sock_xprt, xprt); | |
799 | struct sock *sk = transport->inet; | |
800 | int ret = -EAGAIN; | |
d48f9ce7 | 801 | |
7496b59f TM |
802 | lock_sock(sk); |
803 | if (!sock_writeable(sk)) | |
804 | ret = xs_nospace(req, transport); | |
805 | release_sock(sk); | |
806 | return ret; | |
807 | } | |
d48f9ce7 | 808 | |
d0afde5f | 809 | static int xs_stream_nospace(struct rpc_rqst *req, bool vm_wait) |
7496b59f TM |
810 | { |
811 | struct sock_xprt *transport = | |
812 | container_of(req->rq_xprt, struct sock_xprt, xprt); | |
813 | struct sock *sk = transport->inet; | |
814 | int ret = -EAGAIN; | |
815 | ||
d0afde5f TM |
816 | if (vm_wait) |
817 | return -ENOBUFS; | |
7496b59f TM |
818 | lock_sock(sk); |
819 | if (!sk_stream_memory_free(sk)) | |
820 | ret = xs_nospace(req, transport); | |
821 | release_sock(sk); | |
5e3771ce | 822 | return ret; |
262965f5 CL |
823 | } |
824 | ||
72691a26 | 825 | static int xs_stream_prepare_request(struct rpc_rqst *req, struct xdr_buf *buf) |
277e4ab7 | 826 | { |
72691a26 | 827 | return xdr_alloc_bvec(buf, rpc_task_gfp_mask()); |
277e4ab7 TM |
828 | } |
829 | ||
4cd34e7c TM |
830 | /* |
831 | * Determine if the previous message in the stream was aborted before it | |
832 | * could complete transmission. | |
833 | */ | |
834 | static bool | |
835 | xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req) | |
836 | { | |
837 | return transport->xmit.offset != 0 && req->rq_bytes_sent == 0; | |
838 | } | |
839 | ||
61677eee | 840 | /* |
06b5fc3a | 841 | * Return the stream record marker field for a record of length < 2^31-1 |
61677eee | 842 | */ |
06b5fc3a TM |
843 | static rpc_fraghdr |
844 | xs_stream_record_marker(struct xdr_buf *xdr) | |
61677eee | 845 | { |
06b5fc3a TM |
846 | if (!xdr->len) |
847 | return 0; | |
848 | return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len); | |
61677eee CL |
849 | } |
850 | ||
176e21ee CL |
851 | /** |
852 | * xs_local_send_request - write an RPC request to an AF_LOCAL socket | |
50f484e2 | 853 | * @req: pointer to RPC request |
176e21ee CL |
854 | * |
855 | * Return values: | |
856 | * 0: The request has been sent | |
857 | * EAGAIN: The socket was blocked, please call again later to | |
858 | * complete the request | |
859 | * ENOTCONN: Caller needs to invoke connect logic then call again | |
12b20ce3 | 860 | * other: Some other error occurred, the request was not sent |
176e21ee | 861 | */ |
adfa7144 | 862 | static int xs_local_send_request(struct rpc_rqst *req) |
176e21ee | 863 | { |
176e21ee CL |
864 | struct rpc_xprt *xprt = req->rq_xprt; |
865 | struct sock_xprt *transport = | |
866 | container_of(xprt, struct sock_xprt, xprt); | |
867 | struct xdr_buf *xdr = &req->rq_snd_buf; | |
7e3d3620 TM |
868 | rpc_fraghdr rm = xs_stream_record_marker(xdr); |
869 | unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen; | |
9e55eef4 CL |
870 | struct msghdr msg = { |
871 | .msg_flags = XS_SENDMSG_FLAGS, | |
872 | }; | |
d0afde5f | 873 | bool vm_wait; |
3f649ab7 | 874 | unsigned int sent; |
176e21ee CL |
875 | int status; |
876 | ||
4cd34e7c TM |
877 | /* Close the stream if the previous transmission was incomplete */ |
878 | if (xs_send_request_was_aborted(transport, req)) { | |
f0043206 | 879 | xprt_force_disconnect(xprt); |
4cd34e7c TM |
880 | return -ENOTCONN; |
881 | } | |
882 | ||
176e21ee CL |
883 | xs_pktdump("packet data:", |
884 | req->rq_svec->iov_base, req->rq_svec->iov_len); | |
885 | ||
d0afde5f TM |
886 | vm_wait = sk_stream_is_writeable(transport->inet) ? true : false; |
887 | ||
78215759 | 888 | req->rq_xtime = ktime_get(); |
9e55eef4 CL |
889 | status = xprt_sock_sendmsg(transport->sock, &msg, xdr, |
890 | transport->xmit.offset, rm, &sent); | |
176e21ee | 891 | dprintk("RPC: %s(%u) = %d\n", |
6c7a64e5 | 892 | __func__, xdr->len - transport->xmit.offset, status); |
743c69e7 | 893 | |
f279cd00 | 894 | if (likely(sent > 0) || status == 0) { |
6c7a64e5 TM |
895 | transport->xmit.offset += sent; |
896 | req->rq_bytes_sent = transport->xmit.offset; | |
7e3d3620 | 897 | if (likely(req->rq_bytes_sent >= msglen)) { |
6c7a64e5 | 898 | req->rq_xmit_bytes_sent += transport->xmit.offset; |
6c7a64e5 | 899 | transport->xmit.offset = 0; |
176e21ee CL |
900 | return 0; |
901 | } | |
902 | status = -EAGAIN; | |
d0afde5f | 903 | vm_wait = false; |
176e21ee CL |
904 | } |
905 | ||
906 | switch (status) { | |
907 | case -EAGAIN: | |
d0afde5f | 908 | status = xs_stream_nospace(req, vm_wait); |
176e21ee CL |
909 | break; |
910 | default: | |
911 | dprintk("RPC: sendmsg returned unrecognized error %d\n", | |
912 | -status); | |
df561f66 | 913 | fallthrough; |
176e21ee | 914 | case -EPIPE: |
f0043206 | 915 | xprt_force_disconnect(xprt); |
176e21ee CL |
916 | status = -ENOTCONN; |
917 | } | |
918 | ||
919 | return status; | |
920 | } | |
921 | ||
262965f5 CL |
922 | /** |
923 | * xs_udp_send_request - write an RPC request to a UDP socket | |
50f484e2 | 924 | * @req: pointer to RPC request |
262965f5 CL |
925 | * |
926 | * Return values: | |
927 | * 0: The request has been sent | |
928 | * EAGAIN: The socket was blocked, please call again later to | |
929 | * complete the request | |
930 | * ENOTCONN: Caller needs to invoke connect logic then call again | |
25985edc | 931 | * other: Some other error occurred, the request was not sent |
262965f5 | 932 | */ |
adfa7144 | 933 | static int xs_udp_send_request(struct rpc_rqst *req) |
262965f5 | 934 | { |
262965f5 | 935 | struct rpc_xprt *xprt = req->rq_xprt; |
ee0ac0c2 | 936 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
262965f5 | 937 | struct xdr_buf *xdr = &req->rq_snd_buf; |
9e55eef4 CL |
938 | struct msghdr msg = { |
939 | .msg_name = xs_addr(xprt), | |
940 | .msg_namelen = xprt->addrlen, | |
941 | .msg_flags = XS_SENDMSG_FLAGS, | |
942 | }; | |
3f649ab7 | 943 | unsigned int sent; |
262965f5 | 944 | int status; |
a246b010 | 945 | |
9903cd1c | 946 | xs_pktdump("packet data:", |
a246b010 CL |
947 | req->rq_svec->iov_base, |
948 | req->rq_svec->iov_len); | |
949 | ||
01d37c42 TM |
950 | if (!xprt_bound(xprt)) |
951 | return -ENOTCONN; | |
75891f50 TM |
952 | |
953 | if (!xprt_request_get_cong(xprt, req)) | |
954 | return -EBADSLT; | |
955 | ||
ff053dbb TM |
956 | status = xdr_alloc_bvec(xdr, rpc_task_gfp_mask()); |
957 | if (status < 0) | |
958 | return status; | |
78215759 | 959 | req->rq_xtime = ktime_get(); |
9e55eef4 | 960 | status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, 0, &sent); |
a246b010 | 961 | |
46121cf7 | 962 | dprintk("RPC: xs_udp_send_request(%u) = %d\n", |
6c7a64e5 | 963 | xdr->len, status); |
a246b010 | 964 | |
3dedbb5c JB |
965 | /* firewall is blocking us, don't return -EAGAIN or we end up looping */ |
966 | if (status == -EPERM) | |
967 | goto process_status; | |
968 | ||
743c69e7 N |
969 | if (status == -EAGAIN && sock_writeable(transport->inet)) |
970 | status = -ENOBUFS; | |
971 | ||
f279cd00 JB |
972 | if (sent > 0 || status == 0) { |
973 | req->rq_xmit_bytes_sent += sent; | |
974 | if (sent >= req->rq_slen) | |
2199700f TM |
975 | return 0; |
976 | /* Still some bytes left; set up for a retry later. */ | |
262965f5 | 977 | status = -EAGAIN; |
2199700f | 978 | } |
a246b010 | 979 | |
3dedbb5c | 980 | process_status: |
262965f5 | 981 | switch (status) { |
fba91afb TM |
982 | case -ENOTSOCK: |
983 | status = -ENOTCONN; | |
984 | /* Should we call xs_close() here? */ | |
985 | break; | |
b6ddf64f | 986 | case -EAGAIN: |
7496b59f | 987 | status = xs_sock_nospace(req); |
b6ddf64f | 988 | break; |
262965f5 | 989 | case -ENETUNREACH: |
3601c4a9 | 990 | case -ENOBUFS: |
262965f5 | 991 | case -EPIPE: |
a246b010 | 992 | case -ECONNREFUSED: |
3dedbb5c | 993 | case -EPERM: |
a246b010 | 994 | /* When the server has died, an ICMP port unreachable message |
9903cd1c | 995 | * prompts ECONNREFUSED. */ |
13331a55 TM |
996 | break; |
997 | default: | |
998 | dprintk("RPC: sendmsg returned unrecognized error %d\n", | |
999 | -status); | |
a246b010 | 1000 | } |
5fe46e9d | 1001 | |
262965f5 | 1002 | return status; |
a246b010 CL |
1003 | } |
1004 | ||
9903cd1c | 1005 | /** |
262965f5 | 1006 | * xs_tcp_send_request - write an RPC request to a TCP socket |
50f484e2 | 1007 | * @req: pointer to RPC request |
9903cd1c CL |
1008 | * |
1009 | * Return values: | |
262965f5 CL |
1010 | * 0: The request has been sent |
1011 | * EAGAIN: The socket was blocked, please call again later to | |
1012 | * complete the request | |
1013 | * ENOTCONN: Caller needs to invoke connect logic then call again | |
25985edc | 1014 | * other: Some other error occurred, the request was not sent |
9903cd1c CL |
1015 | * |
1016 | * XXX: In the case of soft timeouts, should we eventually give up | |
262965f5 | 1017 | * if sendmsg is not able to make progress? |
9903cd1c | 1018 | */ |
adfa7144 | 1019 | static int xs_tcp_send_request(struct rpc_rqst *req) |
a246b010 | 1020 | { |
a246b010 | 1021 | struct rpc_xprt *xprt = req->rq_xprt; |
ee0ac0c2 | 1022 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
262965f5 | 1023 | struct xdr_buf *xdr = &req->rq_snd_buf; |
7e3d3620 TM |
1024 | rpc_fraghdr rm = xs_stream_record_marker(xdr); |
1025 | unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen; | |
9e55eef4 CL |
1026 | struct msghdr msg = { |
1027 | .msg_flags = XS_SENDMSG_FLAGS, | |
1028 | }; | |
d0afde5f | 1029 | bool vm_wait; |
3f649ab7 | 1030 | unsigned int sent; |
b595bb15 | 1031 | int status; |
a246b010 | 1032 | |
4cd34e7c TM |
1033 | /* Close the stream if the previous transmission was incomplete */ |
1034 | if (xs_send_request_was_aborted(transport, req)) { | |
1035 | if (transport->sock != NULL) | |
1036 | kernel_sock_shutdown(transport->sock, SHUT_RDWR); | |
1037 | return -ENOTCONN; | |
1038 | } | |
d275880a TM |
1039 | if (!transport->inet) |
1040 | return -ENOTCONN; | |
4cd34e7c | 1041 | |
262965f5 CL |
1042 | xs_pktdump("packet data:", |
1043 | req->rq_svec->iov_base, | |
1044 | req->rq_svec->iov_len); | |
a246b010 | 1045 | |
7196dbb0 TM |
1046 | if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state)) |
1047 | xs_tcp_set_socket_timeouts(xprt, transport->sock); | |
1048 | ||
3b21f757 TM |
1049 | xs_set_srcport(transport, transport->sock); |
1050 | ||
a246b010 CL |
1051 | /* Continue transmitting the packet/record. We must be careful |
1052 | * to cope with writespace callbacks arriving _after_ we have | |
262965f5 | 1053 | * called sendmsg(). */ |
78215759 | 1054 | req->rq_xtime = ktime_get(); |
d737e5d4 | 1055 | tcp_sock_set_cork(transport->inet, true); |
d0afde5f TM |
1056 | |
1057 | vm_wait = sk_stream_is_writeable(transport->inet) ? true : false; | |
1058 | ||
1059 | do { | |
9e55eef4 CL |
1060 | status = xprt_sock_sendmsg(transport->sock, &msg, xdr, |
1061 | transport->xmit.offset, rm, &sent); | |
a246b010 | 1062 | |
46121cf7 | 1063 | dprintk("RPC: xs_tcp_send_request(%u) = %d\n", |
6c7a64e5 | 1064 | xdr->len - transport->xmit.offset, status); |
a246b010 | 1065 | |
262965f5 CL |
1066 | /* If we've sent the entire packet, immediately |
1067 | * reset the count of bytes sent. */ | |
6c7a64e5 TM |
1068 | transport->xmit.offset += sent; |
1069 | req->rq_bytes_sent = transport->xmit.offset; | |
7e3d3620 | 1070 | if (likely(req->rq_bytes_sent >= msglen)) { |
6c7a64e5 | 1071 | req->rq_xmit_bytes_sent += transport->xmit.offset; |
6c7a64e5 | 1072 | transport->xmit.offset = 0; |
d737e5d4 TM |
1073 | if (atomic_long_read(&xprt->xmit_queuelen) == 1) |
1074 | tcp_sock_set_cork(transport->inet, false); | |
262965f5 CL |
1075 | return 0; |
1076 | } | |
a246b010 | 1077 | |
9ffadfbc TM |
1078 | WARN_ON_ONCE(sent == 0 && status == 0); |
1079 | ||
d0afde5f TM |
1080 | if (sent > 0) |
1081 | vm_wait = false; | |
1082 | ||
1083 | } while (status == 0); | |
a246b010 | 1084 | |
262965f5 | 1085 | switch (status) { |
fba91afb TM |
1086 | case -ENOTSOCK: |
1087 | status = -ENOTCONN; | |
1088 | /* Should we call xs_close() here? */ | |
1089 | break; | |
262965f5 | 1090 | case -EAGAIN: |
d0afde5f | 1091 | status = xs_stream_nospace(req, vm_wait); |
262965f5 | 1092 | break; |
262965f5 | 1093 | case -ECONNRESET: |
2a9e1cfa | 1094 | case -ECONNREFUSED: |
262965f5 | 1095 | case -ENOTCONN: |
3913c78c | 1096 | case -EADDRINUSE: |
b5872f0c | 1097 | case -ENOBUFS: |
b9d2bb2e | 1098 | case -EPIPE: |
13331a55 TM |
1099 | break; |
1100 | default: | |
1101 | dprintk("RPC: sendmsg returned unrecognized error %d\n", | |
1102 | -status); | |
a246b010 | 1103 | } |
5fe46e9d | 1104 | |
a246b010 CL |
1105 | return status; |
1106 | } | |
1107 | ||
2a9e1cfa TM |
1108 | static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk) |
1109 | { | |
1110 | transport->old_data_ready = sk->sk_data_ready; | |
1111 | transport->old_state_change = sk->sk_state_change; | |
1112 | transport->old_write_space = sk->sk_write_space; | |
2118071d | 1113 | transport->old_error_report = sk->sk_error_report; |
2a9e1cfa TM |
1114 | } |
1115 | ||
1116 | static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk) | |
1117 | { | |
1118 | sk->sk_data_ready = transport->old_data_ready; | |
1119 | sk->sk_state_change = transport->old_state_change; | |
1120 | sk->sk_write_space = transport->old_write_space; | |
2118071d TM |
1121 | sk->sk_error_report = transport->old_error_report; |
1122 | } | |
1123 | ||
42d42a5b TM |
1124 | static void xs_sock_reset_state_flags(struct rpc_xprt *xprt) |
1125 | { | |
1126 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); | |
1127 | ||
1128 | clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state); | |
4f8943f8 TM |
1129 | clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state); |
1130 | clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state); | |
1131 | clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state); | |
2790a624 | 1132 | clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state); |
4f8943f8 TM |
1133 | } |
1134 | ||
1135 | static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr) | |
1136 | { | |
1137 | set_bit(nr, &transport->sock_state); | |
1138 | queue_work(xprtiod_workqueue, &transport->error_worker); | |
42d42a5b TM |
1139 | } |
1140 | ||
b70ae915 TM |
1141 | static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt) |
1142 | { | |
d896ba83 | 1143 | xprt->connect_cookie++; |
b70ae915 TM |
1144 | smp_mb__before_atomic(); |
1145 | clear_bit(XPRT_CLOSE_WAIT, &xprt->state); | |
1146 | clear_bit(XPRT_CLOSING, &xprt->state); | |
42d42a5b | 1147 | xs_sock_reset_state_flags(xprt); |
b70ae915 TM |
1148 | smp_mb__after_atomic(); |
1149 | } | |
1150 | ||
2118071d TM |
1151 | /** |
1152 | * xs_error_report - callback to handle TCP socket state errors | |
1153 | * @sk: socket | |
1154 | * | |
1155 | * Note: we don't call sock_error() since there may be a rpc_task | |
1156 | * using the socket, and so we don't want to clear sk->sk_err. | |
1157 | */ | |
1158 | static void xs_error_report(struct sock *sk) | |
1159 | { | |
4f8943f8 | 1160 | struct sock_xprt *transport; |
2118071d | 1161 | struct rpc_xprt *xprt; |
2118071d | 1162 | |
2118071d | 1163 | if (!(xprt = xprt_from_sock(sk))) |
ea9afca8 | 1164 | return; |
2118071d | 1165 | |
4f8943f8 | 1166 | transport = container_of(xprt, struct sock_xprt, xprt); |
af84537d BC |
1167 | transport->xprt_err = -sk->sk_err; |
1168 | if (transport->xprt_err == 0) | |
ea9afca8 | 1169 | return; |
2118071d | 1170 | dprintk("RPC: xs_error_report client %p, error=%d...\n", |
af84537d BC |
1171 | xprt, -transport->xprt_err); |
1172 | trace_rpc_socket_error(xprt, sk->sk_socket, transport->xprt_err); | |
1173 | ||
1174 | /* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */ | |
1175 | smp_mb__before_atomic(); | |
4f8943f8 | 1176 | xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR); |
2a9e1cfa TM |
1177 | } |
1178 | ||
fe315e76 | 1179 | static void xs_reset_transport(struct sock_xprt *transport) |
a246b010 | 1180 | { |
ee0ac0c2 CL |
1181 | struct socket *sock = transport->sock; |
1182 | struct sock *sk = transport->inet; | |
6cc7e908 | 1183 | struct rpc_xprt *xprt = &transport->xprt; |
a73881c9 | 1184 | struct file *filp = transport->file; |
a246b010 | 1185 | |
fe315e76 CL |
1186 | if (sk == NULL) |
1187 | return; | |
f0043206 TM |
1188 | /* |
1189 | * Make sure we're calling this in a context from which it is safe | |
1190 | * to call __fput_sync(). In practice that means rpciod and the | |
1191 | * system workqueue. | |
1192 | */ | |
1193 | if (!(current->flags & PF_WQ_WORKER)) { | |
1194 | WARN_ON_ONCE(1); | |
1195 | set_bit(XPRT_CLOSE_WAIT, &xprt->state); | |
1196 | return; | |
1197 | } | |
9903cd1c | 1198 | |
264d1df3 JL |
1199 | if (atomic_read(&transport->xprt.swapper)) |
1200 | sk_clear_memalloc(sk); | |
1201 | ||
09939204 TM |
1202 | kernel_sock_shutdown(sock, SHUT_RDWR); |
1203 | ||
edc1b01c | 1204 | mutex_lock(&transport->recv_mutex); |
ea9afca8 | 1205 | lock_sock(sk); |
ee0ac0c2 CL |
1206 | transport->inet = NULL; |
1207 | transport->sock = NULL; | |
a73881c9 | 1208 | transport->file = NULL; |
a246b010 | 1209 | |
9903cd1c | 1210 | sk->sk_user_data = NULL; |
2a9e1cfa TM |
1211 | |
1212 | xs_restore_old_callbacks(transport, sk); | |
0c78789e | 1213 | xprt_clear_connected(xprt); |
6cc7e908 | 1214 | xs_sock_reset_connection_flags(xprt); |
ae053551 TM |
1215 | /* Reset stream record info */ |
1216 | xs_stream_reset_connect(transport); | |
ea9afca8 | 1217 | release_sock(sk); |
edc1b01c | 1218 | mutex_unlock(&transport->recv_mutex); |
a246b010 | 1219 | |
6cc7e908 | 1220 | trace_rpc_socket_close(xprt, sock); |
f0043206 | 1221 | __fput_sync(filp); |
0445f92c TM |
1222 | |
1223 | xprt_disconnect_done(xprt); | |
fe315e76 CL |
1224 | } |
1225 | ||
1226 | /** | |
1227 | * xs_close - close a socket | |
1228 | * @xprt: transport | |
1229 | * | |
1230 | * This is used when all requests are complete; ie, no DRC state remains | |
1231 | * on the server we want to save. | |
f75e6745 TM |
1232 | * |
1233 | * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with | |
1234 | * xs_reset_transport() zeroing the socket from underneath a writer. | |
fe315e76 CL |
1235 | */ |
1236 | static void xs_close(struct rpc_xprt *xprt) | |
1237 | { | |
1238 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); | |
1239 | ||
1240 | dprintk("RPC: xs_close xprt %p\n", xprt); | |
1241 | ||
1242 | xs_reset_transport(transport); | |
61d0a8e6 | 1243 | xprt->reestablish_timeout = 0; |
a246b010 CL |
1244 | } |
1245 | ||
4a068258 CL |
1246 | static void xs_inject_disconnect(struct rpc_xprt *xprt) |
1247 | { | |
1248 | dprintk("RPC: injecting transport disconnect on xprt=%p\n", | |
1249 | xprt); | |
1250 | xprt_disconnect_done(xprt); | |
1251 | } | |
1252 | ||
315f3812 KM |
1253 | static void xs_xprt_free(struct rpc_xprt *xprt) |
1254 | { | |
1255 | xs_free_peer_addresses(xprt); | |
1256 | xprt_free(xprt); | |
1257 | } | |
1258 | ||
9903cd1c CL |
1259 | /** |
1260 | * xs_destroy - prepare to shutdown a transport | |
1261 | * @xprt: doomed transport | |
1262 | * | |
1263 | */ | |
1264 | static void xs_destroy(struct rpc_xprt *xprt) | |
a246b010 | 1265 | { |
03c78827 TM |
1266 | struct sock_xprt *transport = container_of(xprt, |
1267 | struct sock_xprt, xprt); | |
46121cf7 | 1268 | dprintk("RPC: xs_destroy xprt %p\n", xprt); |
9903cd1c | 1269 | |
03c78827 | 1270 | cancel_delayed_work_sync(&transport->connect_worker); |
a1311d87 | 1271 | xs_close(xprt); |
edc1b01c | 1272 | cancel_work_sync(&transport->recv_worker); |
b5e92419 | 1273 | cancel_work_sync(&transport->error_worker); |
315f3812 | 1274 | xs_xprt_free(xprt); |
a1311d87 | 1275 | module_put(THIS_MODULE); |
a246b010 CL |
1276 | } |
1277 | ||
9903cd1c | 1278 | /** |
f9b2ee71 TM |
1279 | * xs_udp_data_read_skb - receive callback for UDP sockets |
1280 | * @xprt: transport | |
1281 | * @sk: socket | |
1282 | * @skb: skbuff | |
9903cd1c | 1283 | * |
a246b010 | 1284 | */ |
f9b2ee71 TM |
1285 | static void xs_udp_data_read_skb(struct rpc_xprt *xprt, |
1286 | struct sock *sk, | |
1287 | struct sk_buff *skb) | |
a246b010 | 1288 | { |
9903cd1c | 1289 | struct rpc_task *task; |
a246b010 | 1290 | struct rpc_rqst *rovr; |
f9b2ee71 | 1291 | int repsize, copied; |
d8ed029d AD |
1292 | u32 _xid; |
1293 | __be32 *xp; | |
a246b010 | 1294 | |
1da8c681 | 1295 | repsize = skb->len; |
a246b010 | 1296 | if (repsize < 4) { |
46121cf7 | 1297 | dprintk("RPC: impossible RPC reply size %d!\n", repsize); |
f9b2ee71 | 1298 | return; |
a246b010 CL |
1299 | } |
1300 | ||
1301 | /* Copy the XID from the skb... */ | |
1da8c681 | 1302 | xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid); |
a246b010 | 1303 | if (xp == NULL) |
f9b2ee71 | 1304 | return; |
a246b010 CL |
1305 | |
1306 | /* Look up and lock the request corresponding to the given XID */ | |
75c84151 | 1307 | spin_lock(&xprt->queue_lock); |
a246b010 CL |
1308 | rovr = xprt_lookup_rqst(xprt, *xp); |
1309 | if (!rovr) | |
1310 | goto out_unlock; | |
729749bb | 1311 | xprt_pin_rqst(rovr); |
ecd465ee | 1312 | xprt_update_rtt(rovr->rq_task); |
75c84151 | 1313 | spin_unlock(&xprt->queue_lock); |
a246b010 CL |
1314 | task = rovr->rq_task; |
1315 | ||
a246b010 CL |
1316 | if ((copied = rovr->rq_private_buf.buflen) > repsize) |
1317 | copied = repsize; | |
1318 | ||
1319 | /* Suck it into the iovec, verify checksum if not done by hw. */ | |
1781f7f5 | 1320 | if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) { |
75c84151 | 1321 | spin_lock(&xprt->queue_lock); |
0afa6b44 | 1322 | __UDPX_INC_STATS(sk, UDP_MIB_INERRORS); |
729749bb | 1323 | goto out_unpin; |
1781f7f5 HX |
1324 | } |
1325 | ||
a246b010 | 1326 | |
b5e92419 | 1327 | spin_lock(&xprt->transport_lock); |
6a24dfb6 | 1328 | xprt_adjust_cwnd(xprt, task, copied); |
b5e92419 | 1329 | spin_unlock(&xprt->transport_lock); |
75c84151 | 1330 | spin_lock(&xprt->queue_lock); |
1570c1e4 | 1331 | xprt_complete_rqst(task, copied); |
0afa6b44 | 1332 | __UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS); |
729749bb TM |
1333 | out_unpin: |
1334 | xprt_unpin_rqst(rovr); | |
a246b010 | 1335 | out_unlock: |
75c84151 | 1336 | spin_unlock(&xprt->queue_lock); |
f9b2ee71 TM |
1337 | } |
1338 | ||
1339 | static void xs_udp_data_receive(struct sock_xprt *transport) | |
1340 | { | |
1341 | struct sk_buff *skb; | |
1342 | struct sock *sk; | |
1343 | int err; | |
1344 | ||
1345 | mutex_lock(&transport->recv_mutex); | |
1346 | sk = transport->inet; | |
1347 | if (sk == NULL) | |
1348 | goto out; | |
1349 | for (;;) { | |
ec095263 | 1350 | skb = skb_recv_udp(sk, MSG_DONTWAIT, &err); |
4f546149 | 1351 | if (skb == NULL) |
f9b2ee71 | 1352 | break; |
4f546149 TM |
1353 | xs_udp_data_read_skb(&transport->xprt, sk, skb); |
1354 | consume_skb(skb); | |
1355 | cond_resched(); | |
f9b2ee71 | 1356 | } |
0ffe86f4 | 1357 | xs_poll_check_readable(transport); |
f9b2ee71 TM |
1358 | out: |
1359 | mutex_unlock(&transport->recv_mutex); | |
1360 | } | |
1361 | ||
1362 | static void xs_udp_data_receive_workfn(struct work_struct *work) | |
1363 | { | |
1364 | struct sock_xprt *transport = | |
1365 | container_of(work, struct sock_xprt, recv_worker); | |
a1231fda TM |
1366 | unsigned int pflags = memalloc_nofs_save(); |
1367 | ||
f9b2ee71 | 1368 | xs_udp_data_receive(transport); |
a1231fda | 1369 | memalloc_nofs_restore(pflags); |
f9b2ee71 TM |
1370 | } |
1371 | ||
1372 | /** | |
f67939e4 | 1373 | * xs_data_ready - "data ready" callback for sockets |
f9b2ee71 TM |
1374 | * @sk: socket with data to read |
1375 | * | |
1376 | */ | |
1377 | static void xs_data_ready(struct sock *sk) | |
1378 | { | |
1379 | struct rpc_xprt *xprt; | |
1380 | ||
f9b2ee71 TM |
1381 | xprt = xprt_from_sock(sk); |
1382 | if (xprt != NULL) { | |
1383 | struct sock_xprt *transport = container_of(xprt, | |
1384 | struct sock_xprt, xprt); | |
f67939e4 CL |
1385 | |
1386 | trace_xs_data_ready(xprt); | |
1387 | ||
5157b956 TM |
1388 | transport->old_data_ready(sk); |
1389 | /* Any data means we had a useful conversation, so | |
1390 | * then we don't need to delay the next reconnect | |
1391 | */ | |
1392 | if (xprt->reestablish_timeout) | |
1393 | xprt->reestablish_timeout = 0; | |
42d42a5b | 1394 | if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state)) |
40a5f1b1 | 1395 | queue_work(xprtiod_workqueue, &transport->recv_worker); |
f9b2ee71 | 1396 | } |
a246b010 CL |
1397 | } |
1398 | ||
a519fc7a TM |
1399 | /* |
1400 | * Helper function to force a TCP close if the server is sending | |
1401 | * junk and/or it has put us in CLOSE_WAIT | |
1402 | */ | |
1403 | static void xs_tcp_force_close(struct rpc_xprt *xprt) | |
1404 | { | |
a519fc7a TM |
1405 | xprt_force_disconnect(xprt); |
1406 | } | |
1407 | ||
9e00abc3 | 1408 | #if defined(CONFIG_SUNRPC_BACKCHANNEL) |
6b26cc8c CL |
1409 | static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt) |
1410 | { | |
1411 | return PAGE_SIZE; | |
1412 | } | |
9e00abc3 | 1413 | #endif /* CONFIG_SUNRPC_BACKCHANNEL */ |
44b98efd | 1414 | |
efce2d0b TM |
1415 | /** |
1416 | * xs_local_state_change - callback to handle AF_LOCAL socket state changes | |
1417 | * @sk: socket whose state has changed | |
1418 | * | |
1419 | */ | |
1420 | static void xs_local_state_change(struct sock *sk) | |
1421 | { | |
1422 | struct rpc_xprt *xprt; | |
1423 | struct sock_xprt *transport; | |
1424 | ||
1425 | if (!(xprt = xprt_from_sock(sk))) | |
1426 | return; | |
1427 | transport = container_of(xprt, struct sock_xprt, xprt); | |
1428 | if (sk->sk_shutdown & SHUTDOWN_MASK) { | |
1429 | clear_bit(XPRT_CONNECTED, &xprt->state); | |
1430 | /* Trigger the socket release */ | |
1431 | xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); | |
1432 | } | |
1433 | } | |
1434 | ||
9903cd1c CL |
1435 | /** |
1436 | * xs_tcp_state_change - callback to handle TCP socket state changes | |
1437 | * @sk: socket whose state has changed | |
1438 | * | |
1439 | */ | |
1440 | static void xs_tcp_state_change(struct sock *sk) | |
a246b010 | 1441 | { |
9903cd1c | 1442 | struct rpc_xprt *xprt; |
0fdea1e8 | 1443 | struct sock_xprt *transport; |
a246b010 | 1444 | |
a246b010 | 1445 | if (!(xprt = xprt_from_sock(sk))) |
ea9afca8 | 1446 | return; |
46121cf7 | 1447 | dprintk("RPC: xs_tcp_state_change client %p...\n", xprt); |
669502ff | 1448 | dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n", |
46121cf7 CL |
1449 | sk->sk_state, xprt_connected(xprt), |
1450 | sock_flag(sk, SOCK_DEAD), | |
669502ff AC |
1451 | sock_flag(sk, SOCK_ZAPPED), |
1452 | sk->sk_shutdown); | |
a246b010 | 1453 | |
0fdea1e8 | 1454 | transport = container_of(xprt, struct sock_xprt, xprt); |
40b5ea0c | 1455 | trace_rpc_socket_state_change(xprt, sk->sk_socket); |
a246b010 CL |
1456 | switch (sk->sk_state) { |
1457 | case TCP_ESTABLISHED: | |
a246b010 | 1458 | if (!xprt_test_and_set_connected(xprt)) { |
8b71798c | 1459 | xprt->connect_cookie++; |
0fdea1e8 TM |
1460 | clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); |
1461 | xprt_clear_connecting(xprt); | |
51971139 | 1462 | |
3968a8a5 CL |
1463 | xprt->stat.connect_count++; |
1464 | xprt->stat.connect_time += (long)jiffies - | |
1465 | xprt->stat.connect_start; | |
4f8943f8 | 1466 | xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING); |
a246b010 | 1467 | } |
a246b010 | 1468 | break; |
3b948ae5 TM |
1469 | case TCP_FIN_WAIT1: |
1470 | /* The client initiated a shutdown of the socket */ | |
7c1d71cf | 1471 | xprt->connect_cookie++; |
663b8858 | 1472 | xprt->reestablish_timeout = 0; |
3b948ae5 | 1473 | set_bit(XPRT_CLOSING, &xprt->state); |
4e857c58 | 1474 | smp_mb__before_atomic(); |
3b948ae5 | 1475 | clear_bit(XPRT_CONNECTED, &xprt->state); |
ef803670 | 1476 | clear_bit(XPRT_CLOSE_WAIT, &xprt->state); |
4e857c58 | 1477 | smp_mb__after_atomic(); |
a246b010 | 1478 | break; |
632e3bdc | 1479 | case TCP_CLOSE_WAIT: |
3b948ae5 | 1480 | /* The server initiated a shutdown of the socket */ |
7c1d71cf | 1481 | xprt->connect_cookie++; |
d0bea455 | 1482 | clear_bit(XPRT_CONNECTED, &xprt->state); |
4f8943f8 | 1483 | xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); |
df561f66 | 1484 | fallthrough; |
663b8858 TM |
1485 | case TCP_CLOSING: |
1486 | /* | |
1487 | * If the server closed down the connection, make sure that | |
1488 | * we back off before reconnecting | |
1489 | */ | |
1490 | if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) | |
1491 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; | |
3b948ae5 TM |
1492 | break; |
1493 | case TCP_LAST_ACK: | |
670f9457 | 1494 | set_bit(XPRT_CLOSING, &xprt->state); |
4e857c58 | 1495 | smp_mb__before_atomic(); |
3b948ae5 | 1496 | clear_bit(XPRT_CONNECTED, &xprt->state); |
4e857c58 | 1497 | smp_mb__after_atomic(); |
3b948ae5 TM |
1498 | break; |
1499 | case TCP_CLOSE: | |
0fdea1e8 TM |
1500 | if (test_and_clear_bit(XPRT_SOCK_CONNECTING, |
1501 | &transport->sock_state)) | |
1502 | xprt_clear_connecting(xprt); | |
9b30889c | 1503 | clear_bit(XPRT_CLOSING, &xprt->state); |
9b30889c | 1504 | /* Trigger the socket release */ |
4f8943f8 | 1505 | xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT); |
a246b010 | 1506 | } |
a246b010 CL |
1507 | } |
1508 | ||
1f0fa154 IJ |
1509 | static void xs_write_space(struct sock *sk) |
1510 | { | |
4f8943f8 | 1511 | struct sock_xprt *transport; |
1f0fa154 IJ |
1512 | struct rpc_xprt *xprt; |
1513 | ||
13331a55 | 1514 | if (!sk->sk_socket) |
1f0fa154 | 1515 | return; |
13331a55 | 1516 | clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
1f0fa154 IJ |
1517 | |
1518 | if (unlikely(!(xprt = xprt_from_sock(sk)))) | |
1519 | return; | |
4f8943f8 | 1520 | transport = container_of(xprt, struct sock_xprt, xprt); |
2790a624 TM |
1521 | if (!test_and_clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state)) |
1522 | return; | |
4f8943f8 TM |
1523 | xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE); |
1524 | sk->sk_write_pending--; | |
1f0fa154 IJ |
1525 | } |
1526 | ||
9903cd1c | 1527 | /** |
c7b2cae8 CL |
1528 | * xs_udp_write_space - callback invoked when socket buffer space |
1529 | * becomes available | |
9903cd1c CL |
1530 | * @sk: socket whose state has changed |
1531 | * | |
a246b010 CL |
1532 | * Called when more output buffer space is available for this socket. |
1533 | * We try not to wake our writers until they can make "significant" | |
c7b2cae8 | 1534 | * progress, otherwise we'll waste resources thrashing kernel_sendmsg |
a246b010 CL |
1535 | * with a bunch of small requests. |
1536 | */ | |
c7b2cae8 | 1537 | static void xs_udp_write_space(struct sock *sk) |
a246b010 | 1538 | { |
c7b2cae8 | 1539 | /* from net/core/sock.c:sock_def_write_space */ |
1f0fa154 IJ |
1540 | if (sock_writeable(sk)) |
1541 | xs_write_space(sk); | |
c7b2cae8 | 1542 | } |
a246b010 | 1543 | |
c7b2cae8 CL |
1544 | /** |
1545 | * xs_tcp_write_space - callback invoked when socket buffer space | |
1546 | * becomes available | |
1547 | * @sk: socket whose state has changed | |
1548 | * | |
1549 | * Called when more output buffer space is available for this socket. | |
1550 | * We try not to wake our writers until they can make "significant" | |
1551 | * progress, otherwise we'll waste resources thrashing kernel_sendmsg | |
1552 | * with a bunch of small requests. | |
1553 | */ | |
1554 | static void xs_tcp_write_space(struct sock *sk) | |
1555 | { | |
c7b2cae8 | 1556 | /* from net/core/stream.c:sk_stream_write_space */ |
64dc6130 | 1557 | if (sk_stream_is_writeable(sk)) |
1f0fa154 | 1558 | xs_write_space(sk); |
a246b010 CL |
1559 | } |
1560 | ||
470056c2 | 1561 | static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt) |
a246b010 | 1562 | { |
ee0ac0c2 CL |
1563 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
1564 | struct sock *sk = transport->inet; | |
a246b010 | 1565 | |
7c6e066e | 1566 | if (transport->rcvsize) { |
a246b010 | 1567 | sk->sk_userlocks |= SOCK_RCVBUF_LOCK; |
7c6e066e | 1568 | sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2; |
a246b010 | 1569 | } |
7c6e066e | 1570 | if (transport->sndsize) { |
a246b010 | 1571 | sk->sk_userlocks |= SOCK_SNDBUF_LOCK; |
7c6e066e | 1572 | sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2; |
a246b010 CL |
1573 | sk->sk_write_space(sk); |
1574 | } | |
1575 | } | |
1576 | ||
43118c29 | 1577 | /** |
470056c2 | 1578 | * xs_udp_set_buffer_size - set send and receive limits |
43118c29 | 1579 | * @xprt: generic transport |
470056c2 CL |
1580 | * @sndsize: requested size of send buffer, in bytes |
1581 | * @rcvsize: requested size of receive buffer, in bytes | |
43118c29 | 1582 | * |
470056c2 | 1583 | * Set socket send and receive buffer size limits. |
43118c29 | 1584 | */ |
470056c2 | 1585 | static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize) |
43118c29 | 1586 | { |
7c6e066e CL |
1587 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
1588 | ||
1589 | transport->sndsize = 0; | |
470056c2 | 1590 | if (sndsize) |
7c6e066e CL |
1591 | transport->sndsize = sndsize + 1024; |
1592 | transport->rcvsize = 0; | |
470056c2 | 1593 | if (rcvsize) |
7c6e066e | 1594 | transport->rcvsize = rcvsize + 1024; |
470056c2 CL |
1595 | |
1596 | xs_udp_do_set_buffer_size(xprt); | |
43118c29 CL |
1597 | } |
1598 | ||
46c0ee8b CL |
1599 | /** |
1600 | * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport | |
acf0a39f | 1601 | * @xprt: controlling transport |
46c0ee8b CL |
1602 | * @task: task that timed out |
1603 | * | |
1604 | * Adjust the congestion window after a retransmit timeout has occurred. | |
1605 | */ | |
6a24dfb6 | 1606 | static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task) |
46c0ee8b | 1607 | { |
b5e92419 | 1608 | spin_lock(&xprt->transport_lock); |
6a24dfb6 | 1609 | xprt_adjust_cwnd(xprt, task, -ETIMEDOUT); |
b5e92419 | 1610 | spin_unlock(&xprt->transport_lock); |
46c0ee8b CL |
1611 | } |
1612 | ||
826799e6 | 1613 | static int xs_get_random_port(void) |
b85d8806 | 1614 | { |
826799e6 BF |
1615 | unsigned short min = xprt_min_resvport, max = xprt_max_resvport; |
1616 | unsigned short range; | |
1617 | unsigned short rand; | |
1618 | ||
1619 | if (max < min) | |
1620 | return -EADDRINUSE; | |
1621 | range = max - min + 1; | |
1622 | rand = (unsigned short) prandom_u32() % range; | |
1623 | return rand + min; | |
b85d8806 CL |
1624 | } |
1625 | ||
4dda9c8a TM |
1626 | static unsigned short xs_sock_getport(struct socket *sock) |
1627 | { | |
1628 | struct sockaddr_storage buf; | |
4dda9c8a TM |
1629 | unsigned short port = 0; |
1630 | ||
9b2c45d4 | 1631 | if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0) |
4dda9c8a TM |
1632 | goto out; |
1633 | switch (buf.ss_family) { | |
1634 | case AF_INET6: | |
1635 | port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port); | |
1636 | break; | |
1637 | case AF_INET: | |
1638 | port = ntohs(((struct sockaddr_in *)&buf)->sin_port); | |
1639 | } | |
1640 | out: | |
1641 | return port; | |
1642 | } | |
1643 | ||
92200412 CL |
1644 | /** |
1645 | * xs_set_port - reset the port number in the remote endpoint address | |
1646 | * @xprt: generic transport | |
1647 | * @port: new port number | |
1648 | * | |
1649 | */ | |
1650 | static void xs_set_port(struct rpc_xprt *xprt, unsigned short port) | |
1651 | { | |
46121cf7 | 1652 | dprintk("RPC: setting port for xprt %p to %u\n", xprt, port); |
c4efcb1d | 1653 | |
9dc3b095 CL |
1654 | rpc_set_port(xs_addr(xprt), port); |
1655 | xs_update_peer_port(xprt); | |
92200412 CL |
1656 | } |
1657 | ||
4dda9c8a TM |
1658 | static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock) |
1659 | { | |
e6237b6f | 1660 | if (transport->srcport == 0 && transport->xprt.reuseport) |
4dda9c8a TM |
1661 | transport->srcport = xs_sock_getport(sock); |
1662 | } | |
1663 | ||
826799e6 | 1664 | static int xs_get_srcport(struct sock_xprt *transport) |
67a391d7 | 1665 | { |
826799e6 | 1666 | int port = transport->srcport; |
67a391d7 TM |
1667 | |
1668 | if (port == 0 && transport->xprt.resvport) | |
1669 | port = xs_get_random_port(); | |
1670 | return port; | |
1671 | } | |
1672 | ||
421ab1be | 1673 | static unsigned short xs_sock_srcport(struct rpc_xprt *xprt) |
a8482488 OK |
1674 | { |
1675 | struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt); | |
b49ea673 N |
1676 | unsigned short ret = 0; |
1677 | mutex_lock(&sock->recv_mutex); | |
1678 | if (sock->sock) | |
1679 | ret = xs_sock_getport(sock->sock); | |
1680 | mutex_unlock(&sock->recv_mutex); | |
1681 | return ret; | |
a8482488 | 1682 | } |
421ab1be TM |
1683 | |
1684 | static int xs_sock_srcaddr(struct rpc_xprt *xprt, char *buf, size_t buflen) | |
1685 | { | |
1686 | struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt); | |
1687 | union { | |
1688 | struct sockaddr sa; | |
1689 | struct sockaddr_storage st; | |
1690 | } saddr; | |
1691 | int ret = -ENOTCONN; | |
1692 | ||
1693 | mutex_lock(&sock->recv_mutex); | |
1694 | if (sock->sock) { | |
1695 | ret = kernel_getsockname(sock->sock, &saddr.sa); | |
1696 | if (ret >= 0) | |
1697 | ret = snprintf(buf, buflen, "%pISc", &saddr.sa); | |
1698 | } | |
1699 | mutex_unlock(&sock->recv_mutex); | |
1700 | return ret; | |
1701 | } | |
a8482488 | 1702 | |
baaf4e48 | 1703 | static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port) |
67a391d7 | 1704 | { |
fbfffbd5 CL |
1705 | if (transport->srcport != 0) |
1706 | transport->srcport = 0; | |
67a391d7 TM |
1707 | if (!transport->xprt.resvport) |
1708 | return 0; | |
1709 | if (port <= xprt_min_resvport || port > xprt_max_resvport) | |
1710 | return xprt_max_resvport; | |
1711 | return --port; | |
1712 | } | |
beb59b68 | 1713 | static int xs_bind(struct sock_xprt *transport, struct socket *sock) |
a246b010 | 1714 | { |
beb59b68 | 1715 | struct sockaddr_storage myaddr; |
67a391d7 | 1716 | int err, nloop = 0; |
826799e6 | 1717 | int port = xs_get_srcport(transport); |
67a391d7 | 1718 | unsigned short last; |
a246b010 | 1719 | |
0f7a622c CP |
1720 | /* |
1721 | * If we are asking for any ephemeral port (i.e. port == 0 && | |
1722 | * transport->xprt.resvport == 0), don't bind. Let the local | |
1723 | * port selection happen implicitly when the socket is used | |
1724 | * (for example at connect time). | |
1725 | * | |
1726 | * This ensures that we can continue to establish TCP | |
1727 | * connections even when all local ephemeral ports are already | |
1728 | * a part of some TCP connection. This makes no difference | |
12b20ce3 | 1729 | * for UDP sockets, but also doesn't harm them. |
0f7a622c CP |
1730 | * |
1731 | * If we're asking for any reserved port (i.e. port == 0 && | |
1732 | * transport->xprt.resvport == 1) xs_get_srcport above will | |
1733 | * ensure that port is non-zero and we will bind as needed. | |
1734 | */ | |
826799e6 BF |
1735 | if (port <= 0) |
1736 | return port; | |
0f7a622c | 1737 | |
beb59b68 | 1738 | memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen); |
a246b010 | 1739 | do { |
beb59b68 PE |
1740 | rpc_set_port((struct sockaddr *)&myaddr, port); |
1741 | err = kernel_bind(sock, (struct sockaddr *)&myaddr, | |
1742 | transport->xprt.addrlen); | |
a246b010 | 1743 | if (err == 0) { |
bc1c56e9 N |
1744 | if (transport->xprt.reuseport) |
1745 | transport->srcport = port; | |
d3bc9a1d | 1746 | break; |
a246b010 | 1747 | } |
67a391d7 | 1748 | last = port; |
baaf4e48 | 1749 | port = xs_next_srcport(transport, port); |
67a391d7 TM |
1750 | if (port > last) |
1751 | nloop++; | |
1752 | } while (err == -EADDRINUSE && nloop != 2); | |
90058d37 | 1753 | |
4232e863 | 1754 | if (myaddr.ss_family == AF_INET) |
beb59b68 PE |
1755 | dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__, |
1756 | &((struct sockaddr_in *)&myaddr)->sin_addr, | |
1757 | port, err ? "failed" : "ok", err); | |
1758 | else | |
1759 | dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__, | |
1760 | &((struct sockaddr_in6 *)&myaddr)->sin6_addr, | |
1761 | port, err ? "failed" : "ok", err); | |
a246b010 CL |
1762 | return err; |
1763 | } | |
1764 | ||
176e21ee CL |
1765 | /* |
1766 | * We don't support autobind on AF_LOCAL sockets | |
1767 | */ | |
1768 | static void xs_local_rpcbind(struct rpc_task *task) | |
1769 | { | |
fb43d172 | 1770 | xprt_set_bound(task->tk_xprt); |
176e21ee CL |
1771 | } |
1772 | ||
1773 | static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port) | |
1774 | { | |
1775 | } | |
beb59b68 | 1776 | |
ed07536e | 1777 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
064a9177 N |
1778 | static struct lock_class_key xs_key[3]; |
1779 | static struct lock_class_key xs_slock_key[3]; | |
ed07536e | 1780 | |
176e21ee CL |
1781 | static inline void xs_reclassify_socketu(struct socket *sock) |
1782 | { | |
1783 | struct sock *sk = sock->sk; | |
1784 | ||
176e21ee | 1785 | sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC", |
064a9177 | 1786 | &xs_slock_key[0], "sk_lock-AF_LOCAL-RPC", &xs_key[0]); |
176e21ee CL |
1787 | } |
1788 | ||
8945ee5e | 1789 | static inline void xs_reclassify_socket4(struct socket *sock) |
ed07536e PZ |
1790 | { |
1791 | struct sock *sk = sock->sk; | |
8945ee5e | 1792 | |
8945ee5e | 1793 | sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC", |
064a9177 | 1794 | &xs_slock_key[1], "sk_lock-AF_INET-RPC", &xs_key[1]); |
8945ee5e | 1795 | } |
ed07536e | 1796 | |
8945ee5e CL |
1797 | static inline void xs_reclassify_socket6(struct socket *sock) |
1798 | { | |
1799 | struct sock *sk = sock->sk; | |
ed07536e | 1800 | |
8945ee5e | 1801 | sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC", |
064a9177 | 1802 | &xs_slock_key[2], "sk_lock-AF_INET6-RPC", &xs_key[2]); |
ed07536e | 1803 | } |
6bc9638a PE |
1804 | |
1805 | static inline void xs_reclassify_socket(int family, struct socket *sock) | |
1806 | { | |
fafc4e1e | 1807 | if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk))) |
1b7a1819 WAA |
1808 | return; |
1809 | ||
4232e863 | 1810 | switch (family) { |
176e21ee CL |
1811 | case AF_LOCAL: |
1812 | xs_reclassify_socketu(sock); | |
1813 | break; | |
4232e863 | 1814 | case AF_INET: |
6bc9638a | 1815 | xs_reclassify_socket4(sock); |
4232e863 CL |
1816 | break; |
1817 | case AF_INET6: | |
6bc9638a | 1818 | xs_reclassify_socket6(sock); |
4232e863 CL |
1819 | break; |
1820 | } | |
6bc9638a | 1821 | } |
ed07536e | 1822 | #else |
6bc9638a PE |
1823 | static inline void xs_reclassify_socket(int family, struct socket *sock) |
1824 | { | |
1825 | } | |
ed07536e PZ |
1826 | #endif |
1827 | ||
93dc41bd N |
1828 | static void xs_dummy_setup_socket(struct work_struct *work) |
1829 | { | |
1830 | } | |
1831 | ||
6bc9638a | 1832 | static struct socket *xs_create_sock(struct rpc_xprt *xprt, |
4dda9c8a TM |
1833 | struct sock_xprt *transport, int family, int type, |
1834 | int protocol, bool reuseport) | |
22f79326 | 1835 | { |
a73881c9 | 1836 | struct file *filp; |
22f79326 PE |
1837 | struct socket *sock; |
1838 | int err; | |
1839 | ||
6bc9638a | 1840 | err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1); |
22f79326 PE |
1841 | if (err < 0) { |
1842 | dprintk("RPC: can't create %d transport socket (%d).\n", | |
1843 | protocol, -err); | |
1844 | goto out; | |
1845 | } | |
6bc9638a | 1846 | xs_reclassify_socket(family, sock); |
22f79326 | 1847 | |
4dda9c8a | 1848 | if (reuseport) |
fe31a326 | 1849 | sock_set_reuseport(sock->sk); |
4dda9c8a | 1850 | |
4cea288a BH |
1851 | err = xs_bind(transport, sock); |
1852 | if (err) { | |
22f79326 PE |
1853 | sock_release(sock); |
1854 | goto out; | |
1855 | } | |
1856 | ||
a73881c9 TM |
1857 | filp = sock_alloc_file(sock, O_NONBLOCK, NULL); |
1858 | if (IS_ERR(filp)) | |
1859 | return ERR_CAST(filp); | |
1860 | transport->file = filp; | |
1861 | ||
22f79326 PE |
1862 | return sock; |
1863 | out: | |
1864 | return ERR_PTR(err); | |
1865 | } | |
1866 | ||
176e21ee CL |
1867 | static int xs_local_finish_connecting(struct rpc_xprt *xprt, |
1868 | struct socket *sock) | |
1869 | { | |
1870 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, | |
1871 | xprt); | |
1872 | ||
1873 | if (!transport->inet) { | |
1874 | struct sock *sk = sock->sk; | |
1875 | ||
ea9afca8 | 1876 | lock_sock(sk); |
176e21ee CL |
1877 | |
1878 | xs_save_old_callbacks(transport, sk); | |
1879 | ||
1880 | sk->sk_user_data = xprt; | |
a2648094 | 1881 | sk->sk_data_ready = xs_data_ready; |
176e21ee | 1882 | sk->sk_write_space = xs_udp_write_space; |
efce2d0b | 1883 | sk->sk_state_change = xs_local_state_change; |
2118071d | 1884 | sk->sk_error_report = xs_error_report; |
176e21ee CL |
1885 | |
1886 | xprt_clear_connected(xprt); | |
1887 | ||
1888 | /* Reset to new socket */ | |
1889 | transport->sock = sock; | |
1890 | transport->inet = sk; | |
1891 | ||
ea9afca8 | 1892 | release_sock(sk); |
176e21ee CL |
1893 | } |
1894 | ||
ae053551 | 1895 | xs_stream_start_connect(transport); |
6c7a64e5 | 1896 | |
176e21ee CL |
1897 | return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0); |
1898 | } | |
1899 | ||
1900 | /** | |
1901 | * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint | |
176e21ee | 1902 | * @transport: socket transport to connect |
176e21ee | 1903 | */ |
dc107402 | 1904 | static int xs_local_setup_socket(struct sock_xprt *transport) |
176e21ee | 1905 | { |
176e21ee | 1906 | struct rpc_xprt *xprt = &transport->xprt; |
a73881c9 | 1907 | struct file *filp; |
176e21ee | 1908 | struct socket *sock; |
68e9a246 | 1909 | int status; |
176e21ee | 1910 | |
176e21ee CL |
1911 | status = __sock_create(xprt->xprt_net, AF_LOCAL, |
1912 | SOCK_STREAM, 0, &sock, 1); | |
1913 | if (status < 0) { | |
1914 | dprintk("RPC: can't create AF_LOCAL " | |
1915 | "transport socket (%d).\n", -status); | |
1916 | goto out; | |
1917 | } | |
d1358917 | 1918 | xs_reclassify_socket(AF_LOCAL, sock); |
176e21ee | 1919 | |
a73881c9 TM |
1920 | filp = sock_alloc_file(sock, O_NONBLOCK, NULL); |
1921 | if (IS_ERR(filp)) { | |
1922 | status = PTR_ERR(filp); | |
1923 | goto out; | |
1924 | } | |
1925 | transport->file = filp; | |
1926 | ||
176e21ee CL |
1927 | dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n", |
1928 | xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); | |
1929 | ||
1930 | status = xs_local_finish_connecting(xprt, sock); | |
40b5ea0c | 1931 | trace_rpc_socket_connect(xprt, sock, status); |
176e21ee CL |
1932 | switch (status) { |
1933 | case 0: | |
1934 | dprintk("RPC: xprt %p connected to %s\n", | |
1935 | xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); | |
3968a8a5 CL |
1936 | xprt->stat.connect_count++; |
1937 | xprt->stat.connect_time += (long)jiffies - | |
1938 | xprt->stat.connect_start; | |
176e21ee | 1939 | xprt_set_connected(xprt); |
93f479d3 | 1940 | break; |
3601c4a9 | 1941 | case -ENOBUFS: |
176e21ee CL |
1942 | break; |
1943 | case -ENOENT: | |
1944 | dprintk("RPC: xprt %p: socket %s does not exist\n", | |
1945 | xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); | |
1946 | break; | |
4a20a988 TM |
1947 | case -ECONNREFUSED: |
1948 | dprintk("RPC: xprt %p: connection refused for %s\n", | |
1949 | xprt, xprt->address_strings[RPC_DISPLAY_ADDR]); | |
1950 | break; | |
176e21ee CL |
1951 | default: |
1952 | printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n", | |
1953 | __func__, -status, | |
1954 | xprt->address_strings[RPC_DISPLAY_ADDR]); | |
1955 | } | |
1956 | ||
1957 | out: | |
1958 | xprt_clear_connecting(xprt); | |
1959 | xprt_wake_pending_tasks(xprt, status); | |
dc107402 BF |
1960 | return status; |
1961 | } | |
1962 | ||
b6669737 | 1963 | static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task) |
dc107402 | 1964 | { |
dc107402 BF |
1965 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
1966 | int ret; | |
1967 | ||
aad41a7d TM |
1968 | if (transport->file) |
1969 | goto force_disconnect; | |
1970 | ||
c4f03966 | 1971 | if (RPC_IS_ASYNC(task)) { |
dc107402 BF |
1972 | /* |
1973 | * We want the AF_LOCAL connect to be resolved in the | |
1974 | * filesystem namespace of the process making the rpc | |
1975 | * call. Thus we connect synchronously. | |
1976 | * | |
1977 | * If we want to support asynchronous AF_LOCAL calls, | |
1978 | * we'll need to figure out how to pass a namespace to | |
1979 | * connect. | |
1980 | */ | |
5ad64b36 | 1981 | task->tk_rpc_status = -ENOTCONN; |
dc107402 | 1982 | rpc_exit(task, -ENOTCONN); |
aad41a7d | 1983 | goto out_wake; |
dc107402 BF |
1984 | } |
1985 | ret = xs_local_setup_socket(transport); | |
1986 | if (ret && !RPC_IS_SOFTCONN(task)) | |
1987 | msleep_interruptible(15000); | |
aad41a7d TM |
1988 | return; |
1989 | force_disconnect: | |
1990 | xprt_force_disconnect(xprt); | |
1991 | out_wake: | |
1992 | xprt_clear_connecting(xprt); | |
1993 | xprt_wake_pending_tasks(xprt, -ENOTCONN); | |
176e21ee CL |
1994 | } |
1995 | ||
3c87ef6e | 1996 | #if IS_ENABLED(CONFIG_SUNRPC_SWAP) |
d6e971d8 | 1997 | /* |
693486d5 N |
1998 | * Note that this should be called with XPRT_LOCKED held, or recv_mutex |
1999 | * held, or when we otherwise know that we have exclusive access to the | |
2000 | * socket, to guard against races with xs_reset_transport. | |
d6e971d8 | 2001 | */ |
a564b8f0 MG |
2002 | static void xs_set_memalloc(struct rpc_xprt *xprt) |
2003 | { | |
2004 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, | |
2005 | xprt); | |
2006 | ||
d6e971d8 JL |
2007 | /* |
2008 | * If there's no sock, then we have nothing to set. The | |
2009 | * reconnecting process will get it for us. | |
2010 | */ | |
2011 | if (!transport->inet) | |
2012 | return; | |
8e228133 | 2013 | if (atomic_read(&xprt->swapper)) |
a564b8f0 MG |
2014 | sk_set_memalloc(transport->inet); |
2015 | } | |
2016 | ||
2017 | /** | |
d67fa4d8 | 2018 | * xs_enable_swap - Tag this transport as being used for swap. |
a564b8f0 | 2019 | * @xprt: transport to tag |
a564b8f0 | 2020 | * |
8e228133 JL |
2021 | * Take a reference to this transport on behalf of the rpc_clnt, and |
2022 | * optionally mark it for swapping if it wasn't already. | |
a564b8f0 | 2023 | */ |
d67fa4d8 JL |
2024 | static int |
2025 | xs_enable_swap(struct rpc_xprt *xprt) | |
a564b8f0 | 2026 | { |
d6e971d8 | 2027 | struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); |
a564b8f0 | 2028 | |
693486d5 N |
2029 | mutex_lock(&xs->recv_mutex); |
2030 | if (atomic_inc_return(&xprt->swapper) == 1 && | |
2031 | xs->inet) | |
d6e971d8 | 2032 | sk_set_memalloc(xs->inet); |
693486d5 | 2033 | mutex_unlock(&xs->recv_mutex); |
8e228133 JL |
2034 | return 0; |
2035 | } | |
a564b8f0 | 2036 | |
8e228133 | 2037 | /** |
d67fa4d8 | 2038 | * xs_disable_swap - Untag this transport as being used for swap. |
8e228133 JL |
2039 | * @xprt: transport to tag |
2040 | * | |
2041 | * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the | |
2042 | * swapper refcount goes to 0, untag the socket as a memalloc socket. | |
2043 | */ | |
d67fa4d8 JL |
2044 | static void |
2045 | xs_disable_swap(struct rpc_xprt *xprt) | |
8e228133 | 2046 | { |
d6e971d8 | 2047 | struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt); |
8e228133 | 2048 | |
693486d5 N |
2049 | mutex_lock(&xs->recv_mutex); |
2050 | if (atomic_dec_and_test(&xprt->swapper) && | |
2051 | xs->inet) | |
d6e971d8 | 2052 | sk_clear_memalloc(xs->inet); |
693486d5 | 2053 | mutex_unlock(&xs->recv_mutex); |
a564b8f0 | 2054 | } |
a564b8f0 MG |
2055 | #else |
2056 | static void xs_set_memalloc(struct rpc_xprt *xprt) | |
2057 | { | |
2058 | } | |
d67fa4d8 JL |
2059 | |
2060 | static int | |
2061 | xs_enable_swap(struct rpc_xprt *xprt) | |
2062 | { | |
2063 | return -EINVAL; | |
2064 | } | |
2065 | ||
2066 | static void | |
2067 | xs_disable_swap(struct rpc_xprt *xprt) | |
2068 | { | |
2069 | } | |
a564b8f0 MG |
2070 | #endif |
2071 | ||
16be2d20 CL |
2072 | static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) |
2073 | { | |
2074 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); | |
2075 | ||
2076 | if (!transport->inet) { | |
2077 | struct sock *sk = sock->sk; | |
2078 | ||
ea9afca8 | 2079 | lock_sock(sk); |
16be2d20 | 2080 | |
2a9e1cfa TM |
2081 | xs_save_old_callbacks(transport, sk); |
2082 | ||
16be2d20 | 2083 | sk->sk_user_data = xprt; |
f9b2ee71 | 2084 | sk->sk_data_ready = xs_data_ready; |
16be2d20 | 2085 | sk->sk_write_space = xs_udp_write_space; |
16be2d20 CL |
2086 | |
2087 | xprt_set_connected(xprt); | |
2088 | ||
2089 | /* Reset to new socket */ | |
2090 | transport->sock = sock; | |
2091 | transport->inet = sk; | |
2092 | ||
a564b8f0 MG |
2093 | xs_set_memalloc(xprt); |
2094 | ||
ea9afca8 | 2095 | release_sock(sk); |
16be2d20 CL |
2096 | } |
2097 | xs_udp_do_set_buffer_size(xprt); | |
02910177 TM |
2098 | |
2099 | xprt->stat.connect_start = jiffies; | |
16be2d20 CL |
2100 | } |
2101 | ||
8c14ff2a | 2102 | static void xs_udp_setup_socket(struct work_struct *work) |
a246b010 | 2103 | { |
34161db6 TM |
2104 | struct sock_xprt *transport = |
2105 | container_of(work, struct sock_xprt, connect_worker.work); | |
c8475461 | 2106 | struct rpc_xprt *xprt = &transport->xprt; |
d099b8af | 2107 | struct socket *sock; |
b65c0310 | 2108 | int status = -EIO; |
8db55a03 | 2109 | unsigned int pflags = current->flags; |
9903cd1c | 2110 | |
8db55a03 N |
2111 | if (atomic_read(&xprt->swapper)) |
2112 | current->flags |= PF_MEMALLOC; | |
8c14ff2a | 2113 | sock = xs_create_sock(xprt, transport, |
4dda9c8a TM |
2114 | xs_addr(xprt)->sa_family, SOCK_DGRAM, |
2115 | IPPROTO_UDP, false); | |
b65c0310 | 2116 | if (IS_ERR(sock)) |
b0d93ad5 | 2117 | goto out; |
68e220bd | 2118 | |
c740eff8 CL |
2119 | dprintk("RPC: worker connecting xprt %p via %s to " |
2120 | "%s (port %s)\n", xprt, | |
2121 | xprt->address_strings[RPC_DISPLAY_PROTO], | |
2122 | xprt->address_strings[RPC_DISPLAY_ADDR], | |
2123 | xprt->address_strings[RPC_DISPLAY_PORT]); | |
68e220bd CL |
2124 | |
2125 | xs_udp_finish_connecting(xprt, sock); | |
40b5ea0c | 2126 | trace_rpc_socket_connect(xprt, sock, 0); |
b0d93ad5 CL |
2127 | status = 0; |
2128 | out: | |
b0d93ad5 | 2129 | xprt_clear_connecting(xprt); |
cf76785d | 2130 | xprt_unlock_connect(xprt, transport); |
7d1e8255 | 2131 | xprt_wake_pending_tasks(xprt, status); |
8db55a03 | 2132 | current_restore_flags(pflags, PF_MEMALLOC); |
a246b010 CL |
2133 | } |
2134 | ||
4876cc77 TM |
2135 | /** |
2136 | * xs_tcp_shutdown - gracefully shut down a TCP socket | |
2137 | * @xprt: transport | |
2138 | * | |
2139 | * Initiates a graceful shutdown of the TCP socket by calling the | |
2140 | * equivalent of shutdown(SHUT_RDWR); | |
2141 | */ | |
2142 | static void xs_tcp_shutdown(struct rpc_xprt *xprt) | |
2143 | { | |
2144 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); | |
2145 | struct socket *sock = transport->sock; | |
9b30889c | 2146 | int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE; |
4876cc77 TM |
2147 | |
2148 | if (sock == NULL) | |
2149 | return; | |
0a6ff58e TM |
2150 | if (!xprt->reuseport) { |
2151 | xs_close(xprt); | |
2152 | return; | |
2153 | } | |
9b30889c | 2154 | switch (skst) { |
7c81e6a9 TM |
2155 | case TCP_FIN_WAIT1: |
2156 | case TCP_FIN_WAIT2: | |
2157 | break; | |
2158 | case TCP_ESTABLISHED: | |
2159 | case TCP_CLOSE_WAIT: | |
4876cc77 TM |
2160 | kernel_sock_shutdown(sock, SHUT_RDWR); |
2161 | trace_rpc_socket_shutdown(xprt, sock); | |
9b30889c | 2162 | break; |
7c81e6a9 | 2163 | default: |
4876cc77 | 2164 | xs_reset_transport(transport); |
9b30889c | 2165 | } |
4876cc77 TM |
2166 | } |
2167 | ||
8d1b8c62 TM |
2168 | static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt, |
2169 | struct socket *sock) | |
2170 | { | |
7196dbb0 TM |
2171 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
2172 | unsigned int keepidle; | |
2173 | unsigned int keepcnt; | |
8d1b8c62 TM |
2174 | unsigned int timeo; |
2175 | ||
b5e92419 | 2176 | spin_lock(&xprt->transport_lock); |
7196dbb0 TM |
2177 | keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ); |
2178 | keepcnt = xprt->timeout->to_retries + 1; | |
2179 | timeo = jiffies_to_msecs(xprt->timeout->to_initval) * | |
2180 | (xprt->timeout->to_retries + 1); | |
2181 | clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); | |
b5e92419 | 2182 | spin_unlock(&xprt->transport_lock); |
7196dbb0 | 2183 | |
8d1b8c62 | 2184 | /* TCP Keepalive options */ |
ce3d9544 | 2185 | sock_set_keepalive(sock->sk); |
71c48eb8 | 2186 | tcp_sock_set_keepidle(sock->sk, keepidle); |
d41ecaac | 2187 | tcp_sock_set_keepintvl(sock->sk, keepidle); |
480aeb96 | 2188 | tcp_sock_set_keepcnt(sock->sk, keepcnt); |
8d1b8c62 TM |
2189 | |
2190 | /* TCP user timeout (see RFC5482) */ | |
c488aead | 2191 | tcp_sock_set_user_timeout(sock->sk, timeo); |
8d1b8c62 TM |
2192 | } |
2193 | ||
7196dbb0 TM |
2194 | static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt, |
2195 | unsigned long connect_timeout, | |
2196 | unsigned long reconnect_timeout) | |
2197 | { | |
2198 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); | |
2199 | struct rpc_timeout to; | |
2200 | unsigned long initval; | |
2201 | ||
b5e92419 | 2202 | spin_lock(&xprt->transport_lock); |
7196dbb0 TM |
2203 | if (reconnect_timeout < xprt->max_reconnect_timeout) |
2204 | xprt->max_reconnect_timeout = reconnect_timeout; | |
2205 | if (connect_timeout < xprt->connect_timeout) { | |
2206 | memcpy(&to, xprt->timeout, sizeof(to)); | |
2207 | initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1); | |
2208 | /* Arbitrary lower limit */ | |
2209 | if (initval < XS_TCP_INIT_REEST_TO << 1) | |
2210 | initval = XS_TCP_INIT_REEST_TO << 1; | |
2211 | to.to_initval = initval; | |
2212 | to.to_maxval = initval; | |
2213 | memcpy(&transport->tcp_timeout, &to, | |
2214 | sizeof(transport->tcp_timeout)); | |
2215 | xprt->timeout = &transport->tcp_timeout; | |
2216 | xprt->connect_timeout = connect_timeout; | |
2217 | } | |
2218 | set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state); | |
b5e92419 | 2219 | spin_unlock(&xprt->transport_lock); |
7196dbb0 TM |
2220 | } |
2221 | ||
16be2d20 | 2222 | static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock) |
a246b010 | 2223 | { |
16be2d20 | 2224 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
edb267a6 | 2225 | |
ee0ac0c2 | 2226 | if (!transport->inet) { |
b0d93ad5 | 2227 | struct sock *sk = sock->sk; |
7f260e85 | 2228 | |
d88e4d82 N |
2229 | /* Avoid temporary address, they are bad for long-lived |
2230 | * connections such as NFS mounts. | |
2231 | * RFC4941, section 3.6 suggests that: | |
2232 | * Individual applications, which have specific | |
2233 | * knowledge about the normal duration of connections, | |
2234 | * MAY override this as appropriate. | |
2235 | */ | |
18d5ad62 CH |
2236 | if (xs_addr(xprt)->sa_family == PF_INET6) { |
2237 | ip6_sock_set_addr_preferences(sk, | |
2238 | IPV6_PREFER_SRC_PUBLIC); | |
2239 | } | |
d88e4d82 | 2240 | |
8d1b8c62 | 2241 | xs_tcp_set_socket_timeouts(xprt, sock); |
d737e5d4 | 2242 | tcp_sock_set_nodelay(sk); |
775f06ab | 2243 | |
ea9afca8 | 2244 | lock_sock(sk); |
b0d93ad5 | 2245 | |
2a9e1cfa TM |
2246 | xs_save_old_callbacks(transport, sk); |
2247 | ||
b0d93ad5 | 2248 | sk->sk_user_data = xprt; |
5157b956 | 2249 | sk->sk_data_ready = xs_data_ready; |
b0d93ad5 CL |
2250 | sk->sk_state_change = xs_tcp_state_change; |
2251 | sk->sk_write_space = xs_tcp_write_space; | |
2118071d | 2252 | sk->sk_error_report = xs_error_report; |
3167e12c CL |
2253 | |
2254 | /* socket options */ | |
3167e12c | 2255 | sock_reset_flag(sk, SOCK_LINGER); |
b0d93ad5 CL |
2256 | |
2257 | xprt_clear_connected(xprt); | |
2258 | ||
2259 | /* Reset to new socket */ | |
ee0ac0c2 CL |
2260 | transport->sock = sock; |
2261 | transport->inet = sk; | |
b0d93ad5 | 2262 | |
ea9afca8 | 2263 | release_sock(sk); |
b0d93ad5 CL |
2264 | } |
2265 | ||
01d37c42 | 2266 | if (!xprt_bound(xprt)) |
280254b6 | 2267 | return -ENOTCONN; |
01d37c42 | 2268 | |
a564b8f0 MG |
2269 | xs_set_memalloc(xprt); |
2270 | ||
ae053551 | 2271 | xs_stream_start_connect(transport); |
e1806c7b | 2272 | |
b0d93ad5 | 2273 | /* Tell the socket layer to start connecting... */ |
0fdea1e8 | 2274 | set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state); |
280254b6 | 2275 | return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK); |
16be2d20 CL |
2276 | } |
2277 | ||
9903cd1c | 2278 | /** |
b61d59ff | 2279 | * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint |
acf0a39f | 2280 | * @work: queued work item |
9903cd1c CL |
2281 | * |
2282 | * Invoked by a work queue tasklet. | |
a246b010 | 2283 | */ |
cdd518d5 | 2284 | static void xs_tcp_setup_socket(struct work_struct *work) |
a246b010 | 2285 | { |
cdd518d5 PE |
2286 | struct sock_xprt *transport = |
2287 | container_of(work, struct sock_xprt, connect_worker.work); | |
ee0ac0c2 | 2288 | struct socket *sock = transport->sock; |
a9f5f0f7 | 2289 | struct rpc_xprt *xprt = &transport->xprt; |
280254b6 | 2290 | int status; |
8db55a03 | 2291 | unsigned int pflags = current->flags; |
a246b010 | 2292 | |
8db55a03 N |
2293 | if (atomic_read(&xprt->swapper)) |
2294 | current->flags |= PF_MEMALLOC; | |
89f42494 TM |
2295 | |
2296 | if (xprt_connected(xprt)) | |
2297 | goto out; | |
2298 | if (test_and_clear_bit(XPRT_SOCK_CONNECT_SENT, | |
2299 | &transport->sock_state) || | |
2300 | !sock) { | |
2301 | xs_reset_transport(transport); | |
2302 | sock = xs_create_sock(xprt, transport, xs_addr(xprt)->sa_family, | |
2303 | SOCK_STREAM, IPPROTO_TCP, true); | |
b61d59ff | 2304 | if (IS_ERR(sock)) { |
280254b6 | 2305 | xprt_wake_pending_tasks(xprt, PTR_ERR(sock)); |
3167e12c CL |
2306 | goto out; |
2307 | } | |
7d1e8255 | 2308 | } |
a246b010 | 2309 | |
c740eff8 CL |
2310 | dprintk("RPC: worker connecting xprt %p via %s to " |
2311 | "%s (port %s)\n", xprt, | |
2312 | xprt->address_strings[RPC_DISPLAY_PROTO], | |
2313 | xprt->address_strings[RPC_DISPLAY_ADDR], | |
2314 | xprt->address_strings[RPC_DISPLAY_PORT]); | |
edb267a6 | 2315 | |
16be2d20 | 2316 | status = xs_tcp_finish_connecting(xprt, sock); |
40b5ea0c | 2317 | trace_rpc_socket_connect(xprt, sock, status); |
46121cf7 CL |
2318 | dprintk("RPC: %p connect status %d connected %d sock state %d\n", |
2319 | xprt, -status, xprt_connected(xprt), | |
2320 | sock->sk->sk_state); | |
2a491991 TM |
2321 | switch (status) { |
2322 | case 0: | |
2323 | case -EINPROGRESS: | |
280254b6 | 2324 | /* SYN_SENT! */ |
89f42494 | 2325 | set_bit(XPRT_SOCK_CONNECT_SENT, &transport->sock_state); |
280254b6 TM |
2326 | if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) |
2327 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; | |
2328 | fallthrough; | |
2a491991 | 2329 | case -EALREADY: |
280254b6 TM |
2330 | goto out_unlock; |
2331 | case -EADDRNOTAVAIL: | |
2332 | /* Source port number is unavailable. Try a new one! */ | |
2333 | transport->srcport = 0; | |
2334 | status = -EAGAIN; | |
2335 | break; | |
9fcfe0c8 TM |
2336 | case -EINVAL: |
2337 | /* Happens, for instance, if the user specified a link | |
2338 | * local IPv6 address without a scope-id. | |
2339 | */ | |
3ed5e2a2 TM |
2340 | case -ECONNREFUSED: |
2341 | case -ECONNRESET: | |
eb5b46fa | 2342 | case -ENETDOWN: |
3ed5e2a2 | 2343 | case -ENETUNREACH: |
4ba161a7 | 2344 | case -EHOSTUNREACH: |
3913c78c | 2345 | case -EADDRINUSE: |
3601c4a9 | 2346 | case -ENOBUFS: |
280254b6 TM |
2347 | break; |
2348 | default: | |
2349 | printk("%s: connect returned unhandled error %d\n", | |
2350 | __func__, status); | |
2351 | status = -EAGAIN; | |
a246b010 | 2352 | } |
280254b6 TM |
2353 | |
2354 | /* xs_tcp_force_close() wakes tasks with a fixed error code. | |
2355 | * We need to wake them first to ensure the correct error code. | |
2356 | */ | |
2357 | xprt_wake_pending_tasks(xprt, status); | |
2358 | xs_tcp_force_close(xprt); | |
a246b010 | 2359 | out: |
68e220bd | 2360 | xprt_clear_connecting(xprt); |
280254b6 | 2361 | out_unlock: |
cf76785d | 2362 | xprt_unlock_connect(xprt, transport); |
8db55a03 | 2363 | current_restore_flags(pflags, PF_MEMALLOC); |
68e220bd | 2364 | } |
b0d93ad5 | 2365 | |
9903cd1c CL |
2366 | /** |
2367 | * xs_connect - connect a socket to a remote endpoint | |
1b092092 | 2368 | * @xprt: pointer to transport structure |
9903cd1c CL |
2369 | * @task: address of RPC task that manages state of connect request |
2370 | * | |
2371 | * TCP: If the remote end dropped the connection, delay reconnecting. | |
03bf4b70 CL |
2372 | * |
2373 | * UDP socket connects are synchronous, but we use a work queue anyway | |
2374 | * to guarantee that even unprivileged user processes can set up a | |
2375 | * socket on a privileged port. | |
2376 | * | |
2377 | * If a UDP socket connect fails, the delay behavior here prevents | |
2378 | * retry floods (hard mounts). | |
9903cd1c | 2379 | */ |
1b092092 | 2380 | static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task) |
a246b010 | 2381 | { |
ee0ac0c2 | 2382 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
02910177 | 2383 | unsigned long delay = 0; |
a246b010 | 2384 | |
718ba5b8 TM |
2385 | WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport)); |
2386 | ||
89f42494 | 2387 | if (transport->sock != NULL) { |
46121cf7 | 2388 | dprintk("RPC: xs_connect delayed xprt %p for %lu " |
89f42494 | 2389 | "seconds\n", xprt, xprt->reestablish_timeout / HZ); |
99b1a4c3 | 2390 | |
675dd90a CL |
2391 | delay = xprt_reconnect_delay(xprt); |
2392 | xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO); | |
02910177 | 2393 | |
02910177 | 2394 | } else |
46121cf7 | 2395 | dprintk("RPC: xs_connect scheduled xprt %p\n", xprt); |
02910177 TM |
2396 | |
2397 | queue_delayed_work(xprtiod_workqueue, | |
2398 | &transport->connect_worker, | |
2399 | delay); | |
a246b010 CL |
2400 | } |
2401 | ||
4f8943f8 TM |
2402 | static void xs_wake_disconnect(struct sock_xprt *transport) |
2403 | { | |
2404 | if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state)) | |
2405 | xs_tcp_force_close(&transport->xprt); | |
2406 | } | |
2407 | ||
2408 | static void xs_wake_write(struct sock_xprt *transport) | |
2409 | { | |
2410 | if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state)) | |
2411 | xprt_write_space(&transport->xprt); | |
2412 | } | |
2413 | ||
2414 | static void xs_wake_error(struct sock_xprt *transport) | |
2415 | { | |
2416 | int sockerr; | |
4f8943f8 TM |
2417 | |
2418 | if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state)) | |
2419 | return; | |
2420 | mutex_lock(&transport->recv_mutex); | |
2421 | if (transport->sock == NULL) | |
2422 | goto out; | |
2423 | if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state)) | |
2424 | goto out; | |
af84537d | 2425 | sockerr = xchg(&transport->xprt_err, 0); |
4f8943f8 TM |
2426 | if (sockerr < 0) |
2427 | xprt_wake_pending_tasks(&transport->xprt, sockerr); | |
2428 | out: | |
2429 | mutex_unlock(&transport->recv_mutex); | |
2430 | } | |
2431 | ||
2432 | static void xs_wake_pending(struct sock_xprt *transport) | |
2433 | { | |
2434 | if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state)) | |
2435 | xprt_wake_pending_tasks(&transport->xprt, -EAGAIN); | |
2436 | } | |
2437 | ||
2438 | static void xs_error_handle(struct work_struct *work) | |
2439 | { | |
2440 | struct sock_xprt *transport = container_of(work, | |
2441 | struct sock_xprt, error_worker); | |
2442 | ||
2443 | xs_wake_disconnect(transport); | |
2444 | xs_wake_write(transport); | |
2445 | xs_wake_error(transport); | |
2446 | xs_wake_pending(transport); | |
2447 | } | |
2448 | ||
176e21ee | 2449 | /** |
12b20ce3 | 2450 | * xs_local_print_stats - display AF_LOCAL socket-specific stats |
176e21ee CL |
2451 | * @xprt: rpc_xprt struct containing statistics |
2452 | * @seq: output file | |
2453 | * | |
2454 | */ | |
2455 | static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) | |
2456 | { | |
2457 | long idle_time = 0; | |
2458 | ||
2459 | if (xprt_connected(xprt)) | |
2460 | idle_time = (long)(jiffies - xprt->last_used) / HZ; | |
2461 | ||
2462 | seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu " | |
15a45206 | 2463 | "%llu %llu %lu %llu %llu\n", |
176e21ee CL |
2464 | xprt->stat.bind_count, |
2465 | xprt->stat.connect_count, | |
8440a886 | 2466 | xprt->stat.connect_time / HZ, |
176e21ee CL |
2467 | idle_time, |
2468 | xprt->stat.sends, | |
2469 | xprt->stat.recvs, | |
2470 | xprt->stat.bad_xids, | |
2471 | xprt->stat.req_u, | |
15a45206 AA |
2472 | xprt->stat.bklog_u, |
2473 | xprt->stat.max_slots, | |
2474 | xprt->stat.sending_u, | |
2475 | xprt->stat.pending_u); | |
176e21ee CL |
2476 | } |
2477 | ||
262ca07d | 2478 | /** |
12b20ce3 | 2479 | * xs_udp_print_stats - display UDP socket-specific stats |
262ca07d CL |
2480 | * @xprt: rpc_xprt struct containing statistics |
2481 | * @seq: output file | |
2482 | * | |
2483 | */ | |
2484 | static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) | |
2485 | { | |
c8475461 CL |
2486 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
2487 | ||
15a45206 AA |
2488 | seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu " |
2489 | "%lu %llu %llu\n", | |
fbfffbd5 | 2490 | transport->srcport, |
262ca07d CL |
2491 | xprt->stat.bind_count, |
2492 | xprt->stat.sends, | |
2493 | xprt->stat.recvs, | |
2494 | xprt->stat.bad_xids, | |
2495 | xprt->stat.req_u, | |
15a45206 AA |
2496 | xprt->stat.bklog_u, |
2497 | xprt->stat.max_slots, | |
2498 | xprt->stat.sending_u, | |
2499 | xprt->stat.pending_u); | |
262ca07d CL |
2500 | } |
2501 | ||
2502 | /** | |
12b20ce3 | 2503 | * xs_tcp_print_stats - display TCP socket-specific stats |
262ca07d CL |
2504 | * @xprt: rpc_xprt struct containing statistics |
2505 | * @seq: output file | |
2506 | * | |
2507 | */ | |
2508 | static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) | |
2509 | { | |
c8475461 | 2510 | struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); |
262ca07d CL |
2511 | long idle_time = 0; |
2512 | ||
2513 | if (xprt_connected(xprt)) | |
2514 | idle_time = (long)(jiffies - xprt->last_used) / HZ; | |
2515 | ||
15a45206 AA |
2516 | seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu " |
2517 | "%llu %llu %lu %llu %llu\n", | |
fbfffbd5 | 2518 | transport->srcport, |
262ca07d CL |
2519 | xprt->stat.bind_count, |
2520 | xprt->stat.connect_count, | |
8440a886 | 2521 | xprt->stat.connect_time / HZ, |
262ca07d CL |
2522 | idle_time, |
2523 | xprt->stat.sends, | |
2524 | xprt->stat.recvs, | |
2525 | xprt->stat.bad_xids, | |
2526 | xprt->stat.req_u, | |
15a45206 AA |
2527 | xprt->stat.bklog_u, |
2528 | xprt->stat.max_slots, | |
2529 | xprt->stat.sending_u, | |
2530 | xprt->stat.pending_u); | |
262ca07d CL |
2531 | } |
2532 | ||
4cfc7e60 RI |
2533 | /* |
2534 | * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason | |
2535 | * we allocate pages instead doing a kmalloc like rpc_malloc is because we want | |
2536 | * to use the server side send routines. | |
2537 | */ | |
5fe6eaa1 | 2538 | static int bc_malloc(struct rpc_task *task) |
4cfc7e60 | 2539 | { |
5fe6eaa1 CL |
2540 | struct rpc_rqst *rqst = task->tk_rqstp; |
2541 | size_t size = rqst->rq_callsize; | |
4cfc7e60 RI |
2542 | struct page *page; |
2543 | struct rpc_buffer *buf; | |
2544 | ||
5fe6eaa1 CL |
2545 | if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) { |
2546 | WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n", | |
2547 | size); | |
2548 | return -EINVAL; | |
2549 | } | |
4cfc7e60 | 2550 | |
b2648015 | 2551 | page = alloc_page(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN); |
4cfc7e60 | 2552 | if (!page) |
5fe6eaa1 | 2553 | return -ENOMEM; |
4cfc7e60 RI |
2554 | |
2555 | buf = page_address(page); | |
2556 | buf->len = PAGE_SIZE; | |
2557 | ||
5fe6eaa1 | 2558 | rqst->rq_buffer = buf->data; |
18e601d6 | 2559 | rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize; |
5fe6eaa1 | 2560 | return 0; |
4cfc7e60 RI |
2561 | } |
2562 | ||
2563 | /* | |
2564 | * Free the space allocated in the bc_alloc routine | |
2565 | */ | |
3435c74a | 2566 | static void bc_free(struct rpc_task *task) |
4cfc7e60 | 2567 | { |
3435c74a | 2568 | void *buffer = task->tk_rqstp->rq_buffer; |
4cfc7e60 RI |
2569 | struct rpc_buffer *buf; |
2570 | ||
4cfc7e60 RI |
2571 | buf = container_of(buffer, struct rpc_buffer, data); |
2572 | free_page((unsigned long)buf); | |
2573 | } | |
2574 | ||
4cfc7e60 RI |
2575 | static int bc_sendto(struct rpc_rqst *req) |
2576 | { | |
da1661b9 | 2577 | struct xdr_buf *xdr = &req->rq_snd_buf; |
4cfc7e60 | 2578 | struct sock_xprt *transport = |
067fb11b | 2579 | container_of(req->rq_xprt, struct sock_xprt, xprt); |
067fb11b | 2580 | struct msghdr msg = { |
da1661b9 | 2581 | .msg_flags = 0, |
067fb11b CL |
2582 | }; |
2583 | rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | | |
da1661b9 CL |
2584 | (u32)xdr->len); |
2585 | unsigned int sent = 0; | |
2586 | int err; | |
4cfc7e60 | 2587 | |
8729aaba | 2588 | req->rq_xtime = ktime_get(); |
ff053dbb TM |
2589 | err = xdr_alloc_bvec(xdr, rpc_task_gfp_mask()); |
2590 | if (err < 0) | |
2591 | return err; | |
da1661b9 CL |
2592 | err = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, marker, &sent); |
2593 | xdr_free_bvec(xdr); | |
2594 | if (err < 0 || sent != (xdr->len + sizeof(marker))) | |
067fb11b | 2595 | return -EAGAIN; |
da1661b9 | 2596 | return sent; |
4cfc7e60 RI |
2597 | } |
2598 | ||
ca4faf54 CL |
2599 | /** |
2600 | * bc_send_request - Send a backchannel Call on a TCP socket | |
2601 | * @req: rpc_rqst containing Call message to be sent | |
2602 | * | |
2603 | * xpt_mutex ensures @rqstp's whole message is written to the socket | |
2604 | * without interruption. | |
2605 | * | |
2606 | * Return values: | |
2607 | * %0 if the message was sent successfully | |
2608 | * %ENOTCONN if the message was not sent | |
4cfc7e60 | 2609 | */ |
adfa7144 | 2610 | static int bc_send_request(struct rpc_rqst *req) |
4cfc7e60 | 2611 | { |
4cfc7e60 | 2612 | struct svc_xprt *xprt; |
7fc56136 | 2613 | int len; |
4cfc7e60 | 2614 | |
4cfc7e60 RI |
2615 | /* |
2616 | * Get the server socket associated with this callback xprt | |
2617 | */ | |
2618 | xprt = req->rq_xprt->bc_xprt; | |
4cfc7e60 RI |
2619 | |
2620 | /* | |
2621 | * Grab the mutex to serialize data as the connection is shared | |
2622 | * with the fore channel | |
2623 | */ | |
c544577d | 2624 | mutex_lock(&xprt->xpt_mutex); |
4cfc7e60 RI |
2625 | if (test_bit(XPT_DEAD, &xprt->xpt_flags)) |
2626 | len = -ENOTCONN; | |
2627 | else | |
2628 | len = bc_sendto(req); | |
2629 | mutex_unlock(&xprt->xpt_mutex); | |
2630 | ||
2631 | if (len > 0) | |
2632 | len = 0; | |
2633 | ||
2634 | return len; | |
2635 | } | |
2636 | ||
2637 | /* | |
2638 | * The close routine. Since this is client initiated, we do nothing | |
2639 | */ | |
2640 | ||
2641 | static void bc_close(struct rpc_xprt *xprt) | |
2642 | { | |
6221f1d9 | 2643 | xprt_disconnect_done(xprt); |
4cfc7e60 RI |
2644 | } |
2645 | ||
2646 | /* | |
2647 | * The xprt destroy routine. Again, because this connection is client | |
2648 | * initiated, we do nothing | |
2649 | */ | |
2650 | ||
2651 | static void bc_destroy(struct rpc_xprt *xprt) | |
2652 | { | |
47f72efa KM |
2653 | dprintk("RPC: bc_destroy xprt %p\n", xprt); |
2654 | ||
2655 | xs_xprt_free(xprt); | |
2656 | module_put(THIS_MODULE); | |
4cfc7e60 RI |
2657 | } |
2658 | ||
d31ae254 | 2659 | static const struct rpc_xprt_ops xs_local_ops = { |
176e21ee | 2660 | .reserve_xprt = xprt_reserve_xprt, |
4cd34e7c | 2661 | .release_xprt = xprt_release_xprt, |
f39c1bfb | 2662 | .alloc_slot = xprt_alloc_slot, |
a9cde23a | 2663 | .free_slot = xprt_free_slot, |
176e21ee CL |
2664 | .rpcbind = xs_local_rpcbind, |
2665 | .set_port = xs_local_set_port, | |
dc107402 | 2666 | .connect = xs_local_connect, |
176e21ee CL |
2667 | .buf_alloc = rpc_malloc, |
2668 | .buf_free = rpc_free, | |
550aebfe | 2669 | .prepare_request = xs_stream_prepare_request, |
176e21ee | 2670 | .send_request = xs_local_send_request, |
8ba6a92d | 2671 | .wait_for_reply_request = xprt_wait_for_reply_request_def, |
176e21ee | 2672 | .close = xs_close, |
a1311d87 | 2673 | .destroy = xs_destroy, |
176e21ee | 2674 | .print_stats = xs_local_print_stats, |
d67fa4d8 JL |
2675 | .enable_swap = xs_enable_swap, |
2676 | .disable_swap = xs_disable_swap, | |
176e21ee CL |
2677 | }; |
2678 | ||
d31ae254 | 2679 | static const struct rpc_xprt_ops xs_udp_ops = { |
43118c29 | 2680 | .set_buffer_size = xs_udp_set_buffer_size, |
12a80469 | 2681 | .reserve_xprt = xprt_reserve_xprt_cong, |
49e9a890 | 2682 | .release_xprt = xprt_release_xprt_cong, |
f39c1bfb | 2683 | .alloc_slot = xprt_alloc_slot, |
a9cde23a | 2684 | .free_slot = xprt_free_slot, |
45160d62 | 2685 | .rpcbind = rpcb_getport_async, |
92200412 | 2686 | .set_port = xs_set_port, |
262965f5 | 2687 | .connect = xs_connect, |
421ab1be TM |
2688 | .get_srcaddr = xs_sock_srcaddr, |
2689 | .get_srcport = xs_sock_srcport, | |
02107148 CL |
2690 | .buf_alloc = rpc_malloc, |
2691 | .buf_free = rpc_free, | |
262965f5 | 2692 | .send_request = xs_udp_send_request, |
8ba6a92d | 2693 | .wait_for_reply_request = xprt_wait_for_reply_request_rtt, |
46c0ee8b | 2694 | .timer = xs_udp_timer, |
a58dd398 | 2695 | .release_request = xprt_release_rqst_cong, |
262965f5 CL |
2696 | .close = xs_close, |
2697 | .destroy = xs_destroy, | |
262ca07d | 2698 | .print_stats = xs_udp_print_stats, |
d67fa4d8 JL |
2699 | .enable_swap = xs_enable_swap, |
2700 | .disable_swap = xs_disable_swap, | |
4a068258 | 2701 | .inject_disconnect = xs_inject_disconnect, |
262965f5 CL |
2702 | }; |
2703 | ||
d31ae254 | 2704 | static const struct rpc_xprt_ops xs_tcp_ops = { |
12a80469 | 2705 | .reserve_xprt = xprt_reserve_xprt, |
4cd34e7c | 2706 | .release_xprt = xprt_release_xprt, |
36bd7de9 | 2707 | .alloc_slot = xprt_alloc_slot, |
a9cde23a | 2708 | .free_slot = xprt_free_slot, |
45160d62 | 2709 | .rpcbind = rpcb_getport_async, |
92200412 | 2710 | .set_port = xs_set_port, |
0b9e7943 | 2711 | .connect = xs_connect, |
421ab1be TM |
2712 | .get_srcaddr = xs_sock_srcaddr, |
2713 | .get_srcport = xs_sock_srcport, | |
02107148 CL |
2714 | .buf_alloc = rpc_malloc, |
2715 | .buf_free = rpc_free, | |
277e4ab7 | 2716 | .prepare_request = xs_stream_prepare_request, |
262965f5 | 2717 | .send_request = xs_tcp_send_request, |
8ba6a92d | 2718 | .wait_for_reply_request = xprt_wait_for_reply_request_def, |
c627d31b | 2719 | .close = xs_tcp_shutdown, |
9903cd1c | 2720 | .destroy = xs_destroy, |
7196dbb0 | 2721 | .set_connect_timeout = xs_tcp_set_connect_timeout, |
262ca07d | 2722 | .print_stats = xs_tcp_print_stats, |
d67fa4d8 JL |
2723 | .enable_swap = xs_enable_swap, |
2724 | .disable_swap = xs_disable_swap, | |
4a068258 | 2725 | .inject_disconnect = xs_inject_disconnect, |
42e5c3e2 CL |
2726 | #ifdef CONFIG_SUNRPC_BACKCHANNEL |
2727 | .bc_setup = xprt_setup_bc, | |
6b26cc8c | 2728 | .bc_maxpayload = xs_tcp_bc_maxpayload, |
7402a4fe | 2729 | .bc_num_slots = xprt_bc_max_slots, |
42e5c3e2 CL |
2730 | .bc_free_rqst = xprt_free_bc_rqst, |
2731 | .bc_destroy = xprt_destroy_bc, | |
2732 | #endif | |
a246b010 CL |
2733 | }; |
2734 | ||
4cfc7e60 RI |
2735 | /* |
2736 | * The rpc_xprt_ops for the server backchannel | |
2737 | */ | |
2738 | ||
d31ae254 | 2739 | static const struct rpc_xprt_ops bc_tcp_ops = { |
4cfc7e60 RI |
2740 | .reserve_xprt = xprt_reserve_xprt, |
2741 | .release_xprt = xprt_release_xprt, | |
84e28a30 | 2742 | .alloc_slot = xprt_alloc_slot, |
a9cde23a | 2743 | .free_slot = xprt_free_slot, |
4cfc7e60 RI |
2744 | .buf_alloc = bc_malloc, |
2745 | .buf_free = bc_free, | |
2746 | .send_request = bc_send_request, | |
8ba6a92d | 2747 | .wait_for_reply_request = xprt_wait_for_reply_request_def, |
4cfc7e60 RI |
2748 | .close = bc_close, |
2749 | .destroy = bc_destroy, | |
2750 | .print_stats = xs_tcp_print_stats, | |
d67fa4d8 JL |
2751 | .enable_swap = xs_enable_swap, |
2752 | .disable_swap = xs_disable_swap, | |
4a068258 | 2753 | .inject_disconnect = xs_inject_disconnect, |
4cfc7e60 RI |
2754 | }; |
2755 | ||
92476850 CL |
2756 | static int xs_init_anyaddr(const int family, struct sockaddr *sap) |
2757 | { | |
2758 | static const struct sockaddr_in sin = { | |
2759 | .sin_family = AF_INET, | |
2760 | .sin_addr.s_addr = htonl(INADDR_ANY), | |
2761 | }; | |
2762 | static const struct sockaddr_in6 sin6 = { | |
2763 | .sin6_family = AF_INET6, | |
2764 | .sin6_addr = IN6ADDR_ANY_INIT, | |
2765 | }; | |
2766 | ||
2767 | switch (family) { | |
176e21ee CL |
2768 | case AF_LOCAL: |
2769 | break; | |
92476850 CL |
2770 | case AF_INET: |
2771 | memcpy(sap, &sin, sizeof(sin)); | |
2772 | break; | |
2773 | case AF_INET6: | |
2774 | memcpy(sap, &sin6, sizeof(sin6)); | |
2775 | break; | |
2776 | default: | |
2777 | dprintk("RPC: %s: Bad address family\n", __func__); | |
2778 | return -EAFNOSUPPORT; | |
2779 | } | |
2780 | return 0; | |
2781 | } | |
2782 | ||
3c341b0b | 2783 | static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args, |
d9ba131d TM |
2784 | unsigned int slot_table_size, |
2785 | unsigned int max_slot_table_size) | |
c8541ecd CL |
2786 | { |
2787 | struct rpc_xprt *xprt; | |
ffc2e518 | 2788 | struct sock_xprt *new; |
c8541ecd | 2789 | |
96802a09 | 2790 | if (args->addrlen > sizeof(xprt->addr)) { |
46121cf7 | 2791 | dprintk("RPC: xs_setup_xprt: address too large\n"); |
c8541ecd CL |
2792 | return ERR_PTR(-EBADF); |
2793 | } | |
2794 | ||
d9ba131d TM |
2795 | xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size, |
2796 | max_slot_table_size); | |
bd1722d4 | 2797 | if (xprt == NULL) { |
46121cf7 CL |
2798 | dprintk("RPC: xs_setup_xprt: couldn't allocate " |
2799 | "rpc_xprt\n"); | |
c8541ecd CL |
2800 | return ERR_PTR(-ENOMEM); |
2801 | } | |
c8541ecd | 2802 | |
bd1722d4 | 2803 | new = container_of(xprt, struct sock_xprt, xprt); |
edc1b01c | 2804 | mutex_init(&new->recv_mutex); |
96802a09 FM |
2805 | memcpy(&xprt->addr, args->dstaddr, args->addrlen); |
2806 | xprt->addrlen = args->addrlen; | |
d3bc9a1d | 2807 | if (args->srcaddr) |
fbfffbd5 | 2808 | memcpy(&new->srcaddr, args->srcaddr, args->addrlen); |
92476850 CL |
2809 | else { |
2810 | int err; | |
2811 | err = xs_init_anyaddr(args->dstaddr->sa_family, | |
2812 | (struct sockaddr *)&new->srcaddr); | |
2aa13531 SK |
2813 | if (err != 0) { |
2814 | xprt_free(xprt); | |
92476850 | 2815 | return ERR_PTR(err); |
2aa13531 | 2816 | } |
92476850 | 2817 | } |
c8541ecd CL |
2818 | |
2819 | return xprt; | |
2820 | } | |
2821 | ||
176e21ee CL |
2822 | static const struct rpc_timeout xs_local_default_timeout = { |
2823 | .to_initval = 10 * HZ, | |
2824 | .to_maxval = 10 * HZ, | |
2825 | .to_retries = 2, | |
2826 | }; | |
2827 | ||
2828 | /** | |
2829 | * xs_setup_local - Set up transport to use an AF_LOCAL socket | |
2830 | * @args: rpc transport creation arguments | |
2831 | * | |
2832 | * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP | |
2833 | */ | |
2834 | static struct rpc_xprt *xs_setup_local(struct xprt_create *args) | |
2835 | { | |
2836 | struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr; | |
2837 | struct sock_xprt *transport; | |
2838 | struct rpc_xprt *xprt; | |
2839 | struct rpc_xprt *ret; | |
2840 | ||
d9ba131d TM |
2841 | xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, |
2842 | xprt_max_tcp_slot_table_entries); | |
176e21ee CL |
2843 | if (IS_ERR(xprt)) |
2844 | return xprt; | |
2845 | transport = container_of(xprt, struct sock_xprt, xprt); | |
2846 | ||
2847 | xprt->prot = 0; | |
d3abc739 | 2848 | xprt->xprt_class = &xs_local_transport; |
176e21ee CL |
2849 | xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; |
2850 | ||
2851 | xprt->bind_timeout = XS_BIND_TO; | |
2852 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; | |
2853 | xprt->idle_timeout = XS_IDLE_DISC_TO; | |
2854 | ||
2855 | xprt->ops = &xs_local_ops; | |
2856 | xprt->timeout = &xs_local_default_timeout; | |
2857 | ||
550aebfe | 2858 | INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); |
4f8943f8 | 2859 | INIT_WORK(&transport->error_worker, xs_error_handle); |
550aebfe | 2860 | INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket); |
93dc41bd | 2861 | |
176e21ee CL |
2862 | switch (sun->sun_family) { |
2863 | case AF_LOCAL: | |
2864 | if (sun->sun_path[0] != '/') { | |
2865 | dprintk("RPC: bad AF_LOCAL address: %s\n", | |
2866 | sun->sun_path); | |
2867 | ret = ERR_PTR(-EINVAL); | |
2868 | goto out_err; | |
2869 | } | |
2870 | xprt_set_bound(xprt); | |
176e21ee CL |
2871 | xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL); |
2872 | break; | |
2873 | default: | |
2874 | ret = ERR_PTR(-EAFNOSUPPORT); | |
2875 | goto out_err; | |
2876 | } | |
2877 | ||
2878 | dprintk("RPC: set up xprt to %s via AF_LOCAL\n", | |
2879 | xprt->address_strings[RPC_DISPLAY_ADDR]); | |
2880 | ||
2881 | if (try_module_get(THIS_MODULE)) | |
2882 | return xprt; | |
2883 | ret = ERR_PTR(-EINVAL); | |
2884 | out_err: | |
315f3812 | 2885 | xs_xprt_free(xprt); |
176e21ee CL |
2886 | return ret; |
2887 | } | |
2888 | ||
2881ae74 TM |
2889 | static const struct rpc_timeout xs_udp_default_timeout = { |
2890 | .to_initval = 5 * HZ, | |
2891 | .to_maxval = 30 * HZ, | |
2892 | .to_increment = 5 * HZ, | |
2893 | .to_retries = 5, | |
2894 | }; | |
2895 | ||
9903cd1c CL |
2896 | /** |
2897 | * xs_setup_udp - Set up transport to use a UDP socket | |
96802a09 | 2898 | * @args: rpc transport creation arguments |
9903cd1c CL |
2899 | * |
2900 | */ | |
483066d6 | 2901 | static struct rpc_xprt *xs_setup_udp(struct xprt_create *args) |
a246b010 | 2902 | { |
8f9d5b1a | 2903 | struct sockaddr *addr = args->dstaddr; |
c8541ecd | 2904 | struct rpc_xprt *xprt; |
c8475461 | 2905 | struct sock_xprt *transport; |
0a68b0be | 2906 | struct rpc_xprt *ret; |
a246b010 | 2907 | |
d9ba131d TM |
2908 | xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries, |
2909 | xprt_udp_slot_table_entries); | |
c8541ecd CL |
2910 | if (IS_ERR(xprt)) |
2911 | return xprt; | |
c8475461 | 2912 | transport = container_of(xprt, struct sock_xprt, xprt); |
a246b010 | 2913 | |
ec739ef0 | 2914 | xprt->prot = IPPROTO_UDP; |
d3abc739 | 2915 | xprt->xprt_class = &xs_udp_transport; |
a246b010 CL |
2916 | /* XXX: header size can vary due to auth type, IPv6, etc. */ |
2917 | xprt->max_payload = (1U << 16) - (MAX_HEADER << 3); | |
2918 | ||
03bf4b70 | 2919 | xprt->bind_timeout = XS_BIND_TO; |
03bf4b70 CL |
2920 | xprt->reestablish_timeout = XS_UDP_REEST_TO; |
2921 | xprt->idle_timeout = XS_IDLE_DISC_TO; | |
a246b010 | 2922 | |
262965f5 | 2923 | xprt->ops = &xs_udp_ops; |
a246b010 | 2924 | |
ba7392bb | 2925 | xprt->timeout = &xs_udp_default_timeout; |
a246b010 | 2926 | |
f9b2ee71 | 2927 | INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn); |
4f8943f8 | 2928 | INIT_WORK(&transport->error_worker, xs_error_handle); |
edc1b01c TM |
2929 | INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket); |
2930 | ||
8f9d5b1a CL |
2931 | switch (addr->sa_family) { |
2932 | case AF_INET: | |
2933 | if (((struct sockaddr_in *)addr)->sin_port != htons(0)) | |
2934 | xprt_set_bound(xprt); | |
2935 | ||
9dc3b095 | 2936 | xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP); |
8f9d5b1a CL |
2937 | break; |
2938 | case AF_INET6: | |
2939 | if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) | |
2940 | xprt_set_bound(xprt); | |
2941 | ||
9dc3b095 | 2942 | xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6); |
8f9d5b1a CL |
2943 | break; |
2944 | default: | |
0a68b0be BF |
2945 | ret = ERR_PTR(-EAFNOSUPPORT); |
2946 | goto out_err; | |
8f9d5b1a CL |
2947 | } |
2948 | ||
c740eff8 CL |
2949 | if (xprt_bound(xprt)) |
2950 | dprintk("RPC: set up xprt to %s (port %s) via %s\n", | |
2951 | xprt->address_strings[RPC_DISPLAY_ADDR], | |
2952 | xprt->address_strings[RPC_DISPLAY_PORT], | |
2953 | xprt->address_strings[RPC_DISPLAY_PROTO]); | |
2954 | else | |
2955 | dprintk("RPC: set up xprt to %s (autobind) via %s\n", | |
2956 | xprt->address_strings[RPC_DISPLAY_ADDR], | |
2957 | xprt->address_strings[RPC_DISPLAY_PROTO]); | |
edb267a6 | 2958 | |
bc25571e TT |
2959 | if (try_module_get(THIS_MODULE)) |
2960 | return xprt; | |
0a68b0be BF |
2961 | ret = ERR_PTR(-EINVAL); |
2962 | out_err: | |
315f3812 | 2963 | xs_xprt_free(xprt); |
0a68b0be | 2964 | return ret; |
a246b010 CL |
2965 | } |
2966 | ||
2881ae74 TM |
2967 | static const struct rpc_timeout xs_tcp_default_timeout = { |
2968 | .to_initval = 60 * HZ, | |
2969 | .to_maxval = 60 * HZ, | |
2970 | .to_retries = 2, | |
2971 | }; | |
2972 | ||
9903cd1c CL |
2973 | /** |
2974 | * xs_setup_tcp - Set up transport to use a TCP socket | |
96802a09 | 2975 | * @args: rpc transport creation arguments |
9903cd1c CL |
2976 | * |
2977 | */ | |
483066d6 | 2978 | static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args) |
a246b010 | 2979 | { |
8f9d5b1a | 2980 | struct sockaddr *addr = args->dstaddr; |
c8541ecd | 2981 | struct rpc_xprt *xprt; |
c8475461 | 2982 | struct sock_xprt *transport; |
0a68b0be | 2983 | struct rpc_xprt *ret; |
b7993ceb TM |
2984 | unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries; |
2985 | ||
2986 | if (args->flags & XPRT_CREATE_INFINITE_SLOTS) | |
2987 | max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT; | |
a246b010 | 2988 | |
d9ba131d | 2989 | xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, |
b7993ceb | 2990 | max_slot_table_size); |
c8541ecd CL |
2991 | if (IS_ERR(xprt)) |
2992 | return xprt; | |
c8475461 | 2993 | transport = container_of(xprt, struct sock_xprt, xprt); |
a246b010 | 2994 | |
ec739ef0 | 2995 | xprt->prot = IPPROTO_TCP; |
d3abc739 | 2996 | xprt->xprt_class = &xs_tcp_transport; |
808012fb | 2997 | xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; |
a246b010 | 2998 | |
03bf4b70 | 2999 | xprt->bind_timeout = XS_BIND_TO; |
03bf4b70 CL |
3000 | xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; |
3001 | xprt->idle_timeout = XS_IDLE_DISC_TO; | |
a246b010 | 3002 | |
262965f5 | 3003 | xprt->ops = &xs_tcp_ops; |
ba7392bb | 3004 | xprt->timeout = &xs_tcp_default_timeout; |
a246b010 | 3005 | |
3851f1cd | 3006 | xprt->max_reconnect_timeout = xprt->timeout->to_maxval; |
7196dbb0 TM |
3007 | xprt->connect_timeout = xprt->timeout->to_initval * |
3008 | (xprt->timeout->to_retries + 1); | |
3851f1cd | 3009 | |
c50b8ee0 | 3010 | INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn); |
4f8943f8 | 3011 | INIT_WORK(&transport->error_worker, xs_error_handle); |
edc1b01c TM |
3012 | INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket); |
3013 | ||
8f9d5b1a CL |
3014 | switch (addr->sa_family) { |
3015 | case AF_INET: | |
3016 | if (((struct sockaddr_in *)addr)->sin_port != htons(0)) | |
3017 | xprt_set_bound(xprt); | |
3018 | ||
9dc3b095 | 3019 | xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP); |
8f9d5b1a CL |
3020 | break; |
3021 | case AF_INET6: | |
3022 | if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0)) | |
3023 | xprt_set_bound(xprt); | |
3024 | ||
9dc3b095 | 3025 | xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6); |
8f9d5b1a CL |
3026 | break; |
3027 | default: | |
0a68b0be BF |
3028 | ret = ERR_PTR(-EAFNOSUPPORT); |
3029 | goto out_err; | |
8f9d5b1a CL |
3030 | } |
3031 | ||
c740eff8 CL |
3032 | if (xprt_bound(xprt)) |
3033 | dprintk("RPC: set up xprt to %s (port %s) via %s\n", | |
3034 | xprt->address_strings[RPC_DISPLAY_ADDR], | |
3035 | xprt->address_strings[RPC_DISPLAY_PORT], | |
3036 | xprt->address_strings[RPC_DISPLAY_PROTO]); | |
3037 | else | |
3038 | dprintk("RPC: set up xprt to %s (autobind) via %s\n", | |
3039 | xprt->address_strings[RPC_DISPLAY_ADDR], | |
3040 | xprt->address_strings[RPC_DISPLAY_PROTO]); | |
3041 | ||
bc25571e TT |
3042 | if (try_module_get(THIS_MODULE)) |
3043 | return xprt; | |
0a68b0be BF |
3044 | ret = ERR_PTR(-EINVAL); |
3045 | out_err: | |
315f3812 | 3046 | xs_xprt_free(xprt); |
0a68b0be | 3047 | return ret; |
a246b010 | 3048 | } |
282b32e1 | 3049 | |
f300baba AB |
3050 | /** |
3051 | * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket | |
3052 | * @args: rpc transport creation arguments | |
3053 | * | |
3054 | */ | |
3055 | static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args) | |
3056 | { | |
3057 | struct sockaddr *addr = args->dstaddr; | |
3058 | struct rpc_xprt *xprt; | |
3059 | struct sock_xprt *transport; | |
3060 | struct svc_sock *bc_sock; | |
0a68b0be | 3061 | struct rpc_xprt *ret; |
f300baba | 3062 | |
d9ba131d TM |
3063 | xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries, |
3064 | xprt_tcp_slot_table_entries); | |
f300baba AB |
3065 | if (IS_ERR(xprt)) |
3066 | return xprt; | |
3067 | transport = container_of(xprt, struct sock_xprt, xprt); | |
3068 | ||
3069 | xprt->prot = IPPROTO_TCP; | |
d3abc739 | 3070 | xprt->xprt_class = &xs_bc_tcp_transport; |
f300baba AB |
3071 | xprt->max_payload = RPC_MAX_FRAGMENT_SIZE; |
3072 | xprt->timeout = &xs_tcp_default_timeout; | |
3073 | ||
3074 | /* backchannel */ | |
3075 | xprt_set_bound(xprt); | |
3076 | xprt->bind_timeout = 0; | |
f300baba AB |
3077 | xprt->reestablish_timeout = 0; |
3078 | xprt->idle_timeout = 0; | |
3079 | ||
f300baba AB |
3080 | xprt->ops = &bc_tcp_ops; |
3081 | ||
3082 | switch (addr->sa_family) { | |
3083 | case AF_INET: | |
3084 | xs_format_peer_addresses(xprt, "tcp", | |
3085 | RPCBIND_NETID_TCP); | |
3086 | break; | |
3087 | case AF_INET6: | |
3088 | xs_format_peer_addresses(xprt, "tcp", | |
3089 | RPCBIND_NETID_TCP6); | |
3090 | break; | |
3091 | default: | |
0a68b0be BF |
3092 | ret = ERR_PTR(-EAFNOSUPPORT); |
3093 | goto out_err; | |
f300baba AB |
3094 | } |
3095 | ||
50fa0d40 PE |
3096 | dprintk("RPC: set up xprt to %s (port %s) via %s\n", |
3097 | xprt->address_strings[RPC_DISPLAY_ADDR], | |
3098 | xprt->address_strings[RPC_DISPLAY_PORT], | |
3099 | xprt->address_strings[RPC_DISPLAY_PROTO]); | |
f300baba | 3100 | |
99de8ea9 BF |
3101 | /* |
3102 | * Once we've associated a backchannel xprt with a connection, | |
28303ca3 WM |
3103 | * we want to keep it around as long as the connection lasts, |
3104 | * in case we need to start using it for a backchannel again; | |
3105 | * this reference won't be dropped until bc_xprt is destroyed. | |
99de8ea9 BF |
3106 | */ |
3107 | xprt_get(xprt); | |
3108 | args->bc_xprt->xpt_bc_xprt = xprt; | |
3109 | xprt->bc_xprt = args->bc_xprt; | |
3110 | bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt); | |
3111 | transport->sock = bc_sock->sk_sock; | |
3112 | transport->inet = bc_sock->sk_sk; | |
3113 | ||
f300baba AB |
3114 | /* |
3115 | * Since we don't want connections for the backchannel, we set | |
3116 | * the xprt status to connected | |
3117 | */ | |
3118 | xprt_set_connected(xprt); | |
3119 | ||
f300baba AB |
3120 | if (try_module_get(THIS_MODULE)) |
3121 | return xprt; | |
642aab58 KM |
3122 | |
3123 | args->bc_xprt->xpt_bc_xprt = NULL; | |
39a9beab | 3124 | args->bc_xprt->xpt_bc_xps = NULL; |
99de8ea9 | 3125 | xprt_put(xprt); |
0a68b0be BF |
3126 | ret = ERR_PTR(-EINVAL); |
3127 | out_err: | |
315f3812 | 3128 | xs_xprt_free(xprt); |
0a68b0be | 3129 | return ret; |
f300baba AB |
3130 | } |
3131 | ||
176e21ee CL |
3132 | static struct xprt_class xs_local_transport = { |
3133 | .list = LIST_HEAD_INIT(xs_local_transport.list), | |
3134 | .name = "named UNIX socket", | |
3135 | .owner = THIS_MODULE, | |
3136 | .ident = XPRT_TRANSPORT_LOCAL, | |
3137 | .setup = xs_setup_local, | |
d5aa6b22 | 3138 | .netid = { "" }, |
176e21ee CL |
3139 | }; |
3140 | ||
bc25571e TT |
3141 | static struct xprt_class xs_udp_transport = { |
3142 | .list = LIST_HEAD_INIT(xs_udp_transport.list), | |
3143 | .name = "udp", | |
3144 | .owner = THIS_MODULE, | |
f300baba | 3145 | .ident = XPRT_TRANSPORT_UDP, |
bc25571e | 3146 | .setup = xs_setup_udp, |
d5aa6b22 | 3147 | .netid = { "udp", "udp6", "" }, |
bc25571e TT |
3148 | }; |
3149 | ||
3150 | static struct xprt_class xs_tcp_transport = { | |
3151 | .list = LIST_HEAD_INIT(xs_tcp_transport.list), | |
3152 | .name = "tcp", | |
3153 | .owner = THIS_MODULE, | |
f300baba | 3154 | .ident = XPRT_TRANSPORT_TCP, |
bc25571e | 3155 | .setup = xs_setup_tcp, |
d5aa6b22 | 3156 | .netid = { "tcp", "tcp6", "" }, |
bc25571e TT |
3157 | }; |
3158 | ||
f300baba AB |
3159 | static struct xprt_class xs_bc_tcp_transport = { |
3160 | .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list), | |
3161 | .name = "tcp NFSv4.1 backchannel", | |
3162 | .owner = THIS_MODULE, | |
3163 | .ident = XPRT_TRANSPORT_BC_TCP, | |
3164 | .setup = xs_setup_bc_tcp, | |
d5aa6b22 | 3165 | .netid = { "" }, |
f300baba AB |
3166 | }; |
3167 | ||
282b32e1 | 3168 | /** |
bc25571e | 3169 | * init_socket_xprt - set up xprtsock's sysctls, register with RPC client |
282b32e1 CL |
3170 | * |
3171 | */ | |
3172 | int init_socket_xprt(void) | |
3173 | { | |
2b1bec5f | 3174 | if (!sunrpc_table_header) |
0b4d4147 | 3175 | sunrpc_table_header = register_sysctl_table(sunrpc_table); |
fbf76683 | 3176 | |
176e21ee | 3177 | xprt_register_transport(&xs_local_transport); |
bc25571e TT |
3178 | xprt_register_transport(&xs_udp_transport); |
3179 | xprt_register_transport(&xs_tcp_transport); | |
f300baba | 3180 | xprt_register_transport(&xs_bc_tcp_transport); |
bc25571e | 3181 | |
282b32e1 CL |
3182 | return 0; |
3183 | } | |
3184 | ||
3185 | /** | |
bc25571e | 3186 | * cleanup_socket_xprt - remove xprtsock's sysctls, unregister |
282b32e1 CL |
3187 | * |
3188 | */ | |
3189 | void cleanup_socket_xprt(void) | |
3190 | { | |
fbf76683 CL |
3191 | if (sunrpc_table_header) { |
3192 | unregister_sysctl_table(sunrpc_table_header); | |
3193 | sunrpc_table_header = NULL; | |
3194 | } | |
bc25571e | 3195 | |
176e21ee | 3196 | xprt_unregister_transport(&xs_local_transport); |
bc25571e TT |
3197 | xprt_unregister_transport(&xs_udp_transport); |
3198 | xprt_unregister_transport(&xs_tcp_transport); | |
f300baba | 3199 | xprt_unregister_transport(&xs_bc_tcp_transport); |
282b32e1 | 3200 | } |
cbf11071 | 3201 | |
9bbb9e5a | 3202 | static int param_set_portnr(const char *val, const struct kernel_param *kp) |
cbf11071 | 3203 | { |
ffb6ca33 | 3204 | return param_set_uint_minmax(val, kp, |
826799e6 | 3205 | RPC_MIN_RESVPORT, |
cbf11071 TM |
3206 | RPC_MAX_RESVPORT); |
3207 | } | |
3208 | ||
9c27847d | 3209 | static const struct kernel_param_ops param_ops_portnr = { |
9bbb9e5a RR |
3210 | .set = param_set_portnr, |
3211 | .get = param_get_uint, | |
3212 | }; | |
3213 | ||
cbf11071 TM |
3214 | #define param_check_portnr(name, p) \ |
3215 | __param_check(name, p, unsigned int); | |
3216 | ||
3217 | module_param_named(min_resvport, xprt_min_resvport, portnr, 0644); | |
3218 | module_param_named(max_resvport, xprt_max_resvport, portnr, 0644); | |
3219 | ||
9bbb9e5a RR |
3220 | static int param_set_slot_table_size(const char *val, |
3221 | const struct kernel_param *kp) | |
cbf11071 TM |
3222 | { |
3223 | return param_set_uint_minmax(val, kp, | |
3224 | RPC_MIN_SLOT_TABLE, | |
3225 | RPC_MAX_SLOT_TABLE); | |
3226 | } | |
3227 | ||
9c27847d | 3228 | static const struct kernel_param_ops param_ops_slot_table_size = { |
9bbb9e5a RR |
3229 | .set = param_set_slot_table_size, |
3230 | .get = param_get_uint, | |
3231 | }; | |
3232 | ||
cbf11071 TM |
3233 | #define param_check_slot_table_size(name, p) \ |
3234 | __param_check(name, p, unsigned int); | |
3235 | ||
d9ba131d TM |
3236 | static int param_set_max_slot_table_size(const char *val, |
3237 | const struct kernel_param *kp) | |
3238 | { | |
3239 | return param_set_uint_minmax(val, kp, | |
3240 | RPC_MIN_SLOT_TABLE, | |
3241 | RPC_MAX_SLOT_TABLE_LIMIT); | |
3242 | } | |
3243 | ||
9c27847d | 3244 | static const struct kernel_param_ops param_ops_max_slot_table_size = { |
d9ba131d TM |
3245 | .set = param_set_max_slot_table_size, |
3246 | .get = param_get_uint, | |
3247 | }; | |
3248 | ||
3249 | #define param_check_max_slot_table_size(name, p) \ | |
3250 | __param_check(name, p, unsigned int); | |
3251 | ||
cbf11071 TM |
3252 | module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries, |
3253 | slot_table_size, 0644); | |
d9ba131d TM |
3254 | module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries, |
3255 | max_slot_table_size, 0644); | |
cbf11071 TM |
3256 | module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries, |
3257 | slot_table_size, 0644); |