1 // SPDX-License-Identifier: GPL-2.0
3 * linux/net/sunrpc/xprtsock.c
5 * Client-side transport implementation for sockets.
7 * TCP callback races fixes (C) 1998 Red Hat
8 * TCP send fixes (C) 1998 Red Hat
9 * TCP NFS related read + write fixes
10 * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
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>
16 * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
18 * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
19 * <gilles.quillard@bull.net>
22 #include <linux/types.h>
23 #include <linux/string.h>
24 #include <linux/slab.h>
25 #include <linux/module.h>
26 #include <linux/capability.h>
27 #include <linux/pagemap.h>
28 #include <linux/errno.h>
29 #include <linux/socket.h>
31 #include <linux/net.h>
34 #include <linux/udp.h>
35 #include <linux/tcp.h>
36 #include <linux/sunrpc/clnt.h>
37 #include <linux/sunrpc/addr.h>
38 #include <linux/sunrpc/sched.h>
39 #include <linux/sunrpc/svcsock.h>
40 #include <linux/sunrpc/xprtsock.h>
41 #include <linux/file.h>
42 #ifdef CONFIG_SUNRPC_BACKCHANNEL
43 #include <linux/sunrpc/bc_xprt.h>
47 #include <net/checksum.h>
50 #include <linux/bvec.h>
51 #include <linux/highmem.h>
52 #include <linux/uio.h>
53 #include <linux/sched/mm.h>
55 #include <trace/events/sunrpc.h>
59 static void xs_close(struct rpc_xprt
*xprt
);
60 static void xs_tcp_set_socket_timeouts(struct rpc_xprt
*xprt
,
66 static unsigned int xprt_udp_slot_table_entries
= RPC_DEF_SLOT_TABLE
;
67 static unsigned int xprt_tcp_slot_table_entries
= RPC_MIN_SLOT_TABLE
;
68 static unsigned int xprt_max_tcp_slot_table_entries
= RPC_MAX_SLOT_TABLE
;
70 static unsigned int xprt_min_resvport
= RPC_DEF_MIN_RESVPORT
;
71 static unsigned int xprt_max_resvport
= RPC_DEF_MAX_RESVPORT
;
73 #define XS_TCP_LINGER_TO (15U * HZ)
74 static unsigned int xs_tcp_fin_timeout __read_mostly
= XS_TCP_LINGER_TO
;
77 * We can register our own files under /proc/sys/sunrpc by
78 * calling register_sysctl_table() again. The files in that
79 * directory become the union of all files registered there.
81 * We simply need to make sure that we don't collide with
82 * someone else's file names!
85 static unsigned int min_slot_table_size
= RPC_MIN_SLOT_TABLE
;
86 static unsigned int max_slot_table_size
= RPC_MAX_SLOT_TABLE
;
87 static unsigned int max_tcp_slot_table_limit
= RPC_MAX_SLOT_TABLE_LIMIT
;
88 static unsigned int xprt_min_resvport_limit
= RPC_MIN_RESVPORT
;
89 static unsigned int xprt_max_resvport_limit
= RPC_MAX_RESVPORT
;
91 static struct ctl_table_header
*sunrpc_table_header
;
94 * FIXME: changing the UDP slot table size should also resize the UDP
95 * socket buffers for existing UDP transports
97 static struct ctl_table xs_tunables_table
[] = {
99 .procname
= "udp_slot_table_entries",
100 .data
= &xprt_udp_slot_table_entries
,
101 .maxlen
= sizeof(unsigned int),
103 .proc_handler
= proc_dointvec_minmax
,
104 .extra1
= &min_slot_table_size
,
105 .extra2
= &max_slot_table_size
108 .procname
= "tcp_slot_table_entries",
109 .data
= &xprt_tcp_slot_table_entries
,
110 .maxlen
= sizeof(unsigned int),
112 .proc_handler
= proc_dointvec_minmax
,
113 .extra1
= &min_slot_table_size
,
114 .extra2
= &max_slot_table_size
117 .procname
= "tcp_max_slot_table_entries",
118 .data
= &xprt_max_tcp_slot_table_entries
,
119 .maxlen
= sizeof(unsigned int),
121 .proc_handler
= proc_dointvec_minmax
,
122 .extra1
= &min_slot_table_size
,
123 .extra2
= &max_tcp_slot_table_limit
126 .procname
= "min_resvport",
127 .data
= &xprt_min_resvport
,
128 .maxlen
= sizeof(unsigned int),
130 .proc_handler
= proc_dointvec_minmax
,
131 .extra1
= &xprt_min_resvport_limit
,
132 .extra2
= &xprt_max_resvport_limit
135 .procname
= "max_resvport",
136 .data
= &xprt_max_resvport
,
137 .maxlen
= sizeof(unsigned int),
139 .proc_handler
= proc_dointvec_minmax
,
140 .extra1
= &xprt_min_resvport_limit
,
141 .extra2
= &xprt_max_resvport_limit
144 .procname
= "tcp_fin_timeout",
145 .data
= &xs_tcp_fin_timeout
,
146 .maxlen
= sizeof(xs_tcp_fin_timeout
),
148 .proc_handler
= proc_dointvec_jiffies
,
153 static struct ctl_table sunrpc_table
[] = {
155 .procname
= "sunrpc",
157 .child
= xs_tunables_table
163 * Wait duration for a reply from the RPC portmapper.
165 #define XS_BIND_TO (60U * HZ)
168 * Delay if a UDP socket connect error occurs. This is most likely some
169 * kind of resource problem on the local host.
171 #define XS_UDP_REEST_TO (2U * HZ)
174 * The reestablish timeout allows clients to delay for a bit before attempting
175 * to reconnect to a server that just dropped our connection.
177 * We implement an exponential backoff when trying to reestablish a TCP
178 * transport connection with the server. Some servers like to drop a TCP
179 * connection when they are overworked, so we start with a short timeout and
180 * increase over time if the server is down or not responding.
182 #define XS_TCP_INIT_REEST_TO (3U * HZ)
185 * TCP idle timeout; client drops the transport socket if it is idle
186 * for this long. Note that we also timeout UDP sockets to prevent
187 * holding port numbers when there is no RPC traffic.
189 #define XS_IDLE_DISC_TO (5U * 60 * HZ)
191 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
192 # undef RPC_DEBUG_DATA
193 # define RPCDBG_FACILITY RPCDBG_TRANS
196 #ifdef RPC_DEBUG_DATA
197 static void xs_pktdump(char *msg
, u32
*packet
, unsigned int count
)
199 u8
*buf
= (u8
*) packet
;
202 dprintk("RPC: %s\n", msg
);
203 for (j
= 0; j
< count
&& j
< 128; j
+= 4) {
207 dprintk("0x%04x ", j
);
209 dprintk("%02x%02x%02x%02x ",
210 buf
[j
], buf
[j
+1], buf
[j
+2], buf
[j
+3]);
215 static inline void xs_pktdump(char *msg
, u32
*packet
, unsigned int count
)
221 static inline struct rpc_xprt
*xprt_from_sock(struct sock
*sk
)
223 return (struct rpc_xprt
*) sk
->sk_user_data
;
226 static inline struct sockaddr
*xs_addr(struct rpc_xprt
*xprt
)
228 return (struct sockaddr
*) &xprt
->addr
;
231 static inline struct sockaddr_un
*xs_addr_un(struct rpc_xprt
*xprt
)
233 return (struct sockaddr_un
*) &xprt
->addr
;
236 static inline struct sockaddr_in
*xs_addr_in(struct rpc_xprt
*xprt
)
238 return (struct sockaddr_in
*) &xprt
->addr
;
241 static inline struct sockaddr_in6
*xs_addr_in6(struct rpc_xprt
*xprt
)
243 return (struct sockaddr_in6
*) &xprt
->addr
;
246 static void xs_format_common_peer_addresses(struct rpc_xprt
*xprt
)
248 struct sockaddr
*sap
= xs_addr(xprt
);
249 struct sockaddr_in6
*sin6
;
250 struct sockaddr_in
*sin
;
251 struct sockaddr_un
*sun
;
254 switch (sap
->sa_family
) {
256 sun
= xs_addr_un(xprt
);
257 strlcpy(buf
, sun
->sun_path
, sizeof(buf
));
258 xprt
->address_strings
[RPC_DISPLAY_ADDR
] =
259 kstrdup(buf
, GFP_KERNEL
);
262 (void)rpc_ntop(sap
, buf
, sizeof(buf
));
263 xprt
->address_strings
[RPC_DISPLAY_ADDR
] =
264 kstrdup(buf
, GFP_KERNEL
);
265 sin
= xs_addr_in(xprt
);
266 snprintf(buf
, sizeof(buf
), "%08x", ntohl(sin
->sin_addr
.s_addr
));
269 (void)rpc_ntop(sap
, buf
, sizeof(buf
));
270 xprt
->address_strings
[RPC_DISPLAY_ADDR
] =
271 kstrdup(buf
, GFP_KERNEL
);
272 sin6
= xs_addr_in6(xprt
);
273 snprintf(buf
, sizeof(buf
), "%pi6", &sin6
->sin6_addr
);
279 xprt
->address_strings
[RPC_DISPLAY_HEX_ADDR
] = kstrdup(buf
, GFP_KERNEL
);
282 static void xs_format_common_peer_ports(struct rpc_xprt
*xprt
)
284 struct sockaddr
*sap
= xs_addr(xprt
);
287 snprintf(buf
, sizeof(buf
), "%u", rpc_get_port(sap
));
288 xprt
->address_strings
[RPC_DISPLAY_PORT
] = kstrdup(buf
, GFP_KERNEL
);
290 snprintf(buf
, sizeof(buf
), "%4hx", rpc_get_port(sap
));
291 xprt
->address_strings
[RPC_DISPLAY_HEX_PORT
] = kstrdup(buf
, GFP_KERNEL
);
294 static void xs_format_peer_addresses(struct rpc_xprt
*xprt
,
295 const char *protocol
,
298 xprt
->address_strings
[RPC_DISPLAY_PROTO
] = protocol
;
299 xprt
->address_strings
[RPC_DISPLAY_NETID
] = netid
;
300 xs_format_common_peer_addresses(xprt
);
301 xs_format_common_peer_ports(xprt
);
304 static void xs_update_peer_port(struct rpc_xprt
*xprt
)
306 kfree(xprt
->address_strings
[RPC_DISPLAY_HEX_PORT
]);
307 kfree(xprt
->address_strings
[RPC_DISPLAY_PORT
]);
309 xs_format_common_peer_ports(xprt
);
312 static void xs_free_peer_addresses(struct rpc_xprt
*xprt
)
316 for (i
= 0; i
< RPC_DISPLAY_MAX
; i
++)
318 case RPC_DISPLAY_PROTO
:
319 case RPC_DISPLAY_NETID
:
322 kfree(xprt
->address_strings
[i
]);
327 xs_alloc_sparse_pages(struct xdr_buf
*buf
, size_t want
, gfp_t gfp
)
331 if (!want
|| !(buf
->flags
& XDRBUF_SPARSE_PAGES
))
333 n
= (buf
->page_base
+ want
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
334 for (i
= 0; i
< n
; i
++) {
337 buf
->bvec
[i
].bv_page
= buf
->pages
[i
] = alloc_page(gfp
);
338 if (!buf
->pages
[i
]) {
340 return i
> buf
->page_base
? i
- buf
->page_base
: 0;
347 xs_sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
, int flags
, size_t seek
)
351 iov_iter_advance(&msg
->msg_iter
, seek
);
352 ret
= sock_recvmsg(sock
, msg
, flags
);
353 return ret
> 0 ? ret
+ seek
: ret
;
357 xs_read_kvec(struct socket
*sock
, struct msghdr
*msg
, int flags
,
358 struct kvec
*kvec
, size_t count
, size_t seek
)
360 iov_iter_kvec(&msg
->msg_iter
, READ
, kvec
, 1, count
);
361 return xs_sock_recvmsg(sock
, msg
, flags
, seek
);
365 xs_read_bvec(struct socket
*sock
, struct msghdr
*msg
, int flags
,
366 struct bio_vec
*bvec
, unsigned long nr
, size_t count
,
369 iov_iter_bvec(&msg
->msg_iter
, READ
, bvec
, nr
, count
);
370 return xs_sock_recvmsg(sock
, msg
, flags
, seek
);
374 xs_read_discard(struct socket
*sock
, struct msghdr
*msg
, int flags
,
377 iov_iter_discard(&msg
->msg_iter
, READ
, count
);
378 return sock_recvmsg(sock
, msg
, flags
);
381 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
383 xs_flush_bvec(const struct bio_vec
*bvec
, size_t count
, size_t seek
)
385 struct bvec_iter bi
= {
390 bvec_iter_advance(bvec
, &bi
, seek
& PAGE_MASK
);
391 for_each_bvec(bv
, bvec
, bi
, bi
)
392 flush_dcache_page(bv
.bv_page
);
396 xs_flush_bvec(const struct bio_vec
*bvec
, size_t count
, size_t seek
)
402 xs_read_xdr_buf(struct socket
*sock
, struct msghdr
*msg
, int flags
,
403 struct xdr_buf
*buf
, size_t count
, size_t seek
, size_t *read
)
405 size_t want
, seek_init
= seek
, offset
= 0;
408 want
= min_t(size_t, count
, buf
->head
[0].iov_len
);
410 ret
= xs_read_kvec(sock
, msg
, flags
, &buf
->head
[0], want
, seek
);
414 if (offset
== count
|| msg
->msg_flags
& (MSG_EOR
|MSG_TRUNC
))
424 want
= xs_alloc_sparse_pages(buf
,
425 min_t(size_t, count
- offset
, buf
->page_len
),
428 ret
= xs_read_bvec(sock
, msg
, flags
, buf
->bvec
,
429 xdr_buf_pagecount(buf
),
430 want
+ buf
->page_base
,
431 seek
+ buf
->page_base
);
434 xs_flush_bvec(buf
->bvec
, ret
, seek
+ buf
->page_base
);
435 offset
+= ret
- buf
->page_base
;
436 if (offset
== count
|| msg
->msg_flags
& (MSG_EOR
|MSG_TRUNC
))
446 want
= min_t(size_t, count
- offset
, buf
->tail
[0].iov_len
);
448 ret
= xs_read_kvec(sock
, msg
, flags
, &buf
->tail
[0], want
, seek
);
452 if (offset
== count
|| msg
->msg_flags
& (MSG_EOR
|MSG_TRUNC
))
456 } else if (offset
< seek_init
)
460 *read
= offset
- seek_init
;
468 xs_read_header(struct sock_xprt
*transport
, struct xdr_buf
*buf
)
470 if (!transport
->recv
.copied
) {
471 if (buf
->head
[0].iov_len
>= transport
->recv
.offset
)
472 memcpy(buf
->head
[0].iov_base
,
473 &transport
->recv
.xid
,
474 transport
->recv
.offset
);
475 transport
->recv
.copied
= transport
->recv
.offset
;
480 xs_read_stream_request_done(struct sock_xprt
*transport
)
482 return transport
->recv
.fraghdr
& cpu_to_be32(RPC_LAST_STREAM_FRAGMENT
);
486 xs_read_stream_check_eor(struct sock_xprt
*transport
,
489 if (xs_read_stream_request_done(transport
))
490 msg
->msg_flags
|= MSG_EOR
;
494 xs_read_stream_request(struct sock_xprt
*transport
, struct msghdr
*msg
,
495 int flags
, struct rpc_rqst
*req
)
497 struct xdr_buf
*buf
= &req
->rq_private_buf
;
498 size_t want
, uninitialized_var(read
);
499 ssize_t
uninitialized_var(ret
);
501 xs_read_header(transport
, buf
);
503 want
= transport
->recv
.len
- transport
->recv
.offset
;
505 ret
= xs_read_xdr_buf(transport
->sock
, msg
, flags
, buf
,
506 transport
->recv
.copied
+ want
,
507 transport
->recv
.copied
,
509 transport
->recv
.offset
+= read
;
510 transport
->recv
.copied
+= read
;
513 if (transport
->recv
.offset
== transport
->recv
.len
)
514 xs_read_stream_check_eor(transport
, msg
);
524 msg
->msg_flags
|= MSG_TRUNC
;
529 return ret
< 0 ? ret
: read
;
533 xs_read_stream_headersize(bool isfrag
)
536 return sizeof(__be32
);
537 return 3 * sizeof(__be32
);
541 xs_read_stream_header(struct sock_xprt
*transport
, struct msghdr
*msg
,
542 int flags
, size_t want
, size_t seek
)
545 .iov_base
= &transport
->recv
.fraghdr
,
548 return xs_read_kvec(transport
->sock
, msg
, flags
, &kvec
, want
, seek
);
551 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
553 xs_read_stream_call(struct sock_xprt
*transport
, struct msghdr
*msg
, int flags
)
555 struct rpc_xprt
*xprt
= &transport
->xprt
;
556 struct rpc_rqst
*req
;
559 /* Look up and lock the request corresponding to the given XID */
560 req
= xprt_lookup_bc_request(xprt
, transport
->recv
.xid
);
562 printk(KERN_WARNING
"Callback slot table overflowed\n");
565 if (transport
->recv
.copied
&& !req
->rq_private_buf
.len
)
568 ret
= xs_read_stream_request(transport
, msg
, flags
, req
);
569 if (msg
->msg_flags
& (MSG_EOR
|MSG_TRUNC
))
570 xprt_complete_bc_request(req
, transport
->recv
.copied
);
572 req
->rq_private_buf
.len
= transport
->recv
.copied
;
576 #else /* CONFIG_SUNRPC_BACKCHANNEL */
578 xs_read_stream_call(struct sock_xprt
*transport
, struct msghdr
*msg
, int flags
)
582 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
585 xs_read_stream_reply(struct sock_xprt
*transport
, struct msghdr
*msg
, int flags
)
587 struct rpc_xprt
*xprt
= &transport
->xprt
;
588 struct rpc_rqst
*req
;
591 /* Look up and lock the request corresponding to the given XID */
592 spin_lock(&xprt
->queue_lock
);
593 req
= xprt_lookup_rqst(xprt
, transport
->recv
.xid
);
594 if (!req
|| (transport
->recv
.copied
&& !req
->rq_private_buf
.len
)) {
595 msg
->msg_flags
|= MSG_TRUNC
;
599 spin_unlock(&xprt
->queue_lock
);
601 ret
= xs_read_stream_request(transport
, msg
, flags
, req
);
603 spin_lock(&xprt
->queue_lock
);
604 if (msg
->msg_flags
& (MSG_EOR
|MSG_TRUNC
))
605 xprt_complete_rqst(req
->rq_task
, transport
->recv
.copied
);
607 req
->rq_private_buf
.len
= transport
->recv
.copied
;
608 xprt_unpin_rqst(req
);
610 spin_unlock(&xprt
->queue_lock
);
615 xs_read_stream(struct sock_xprt
*transport
, int flags
)
617 struct msghdr msg
= { 0 };
618 size_t want
, read
= 0;
621 if (transport
->recv
.len
== 0) {
622 want
= xs_read_stream_headersize(transport
->recv
.copied
!= 0);
623 ret
= xs_read_stream_header(transport
, &msg
, flags
, want
,
624 transport
->recv
.offset
);
627 transport
->recv
.offset
= ret
;
628 if (transport
->recv
.offset
!= want
)
629 return transport
->recv
.offset
;
630 transport
->recv
.len
= be32_to_cpu(transport
->recv
.fraghdr
) &
631 RPC_FRAGMENT_SIZE_MASK
;
632 transport
->recv
.offset
-= sizeof(transport
->recv
.fraghdr
);
636 switch (be32_to_cpu(transport
->recv
.calldir
)) {
638 msg
.msg_flags
|= MSG_TRUNC
;
641 ret
= xs_read_stream_call(transport
, &msg
, flags
);
644 ret
= xs_read_stream_reply(transport
, &msg
, flags
);
646 if (msg
.msg_flags
& MSG_TRUNC
) {
647 transport
->recv
.calldir
= cpu_to_be32(-1);
648 transport
->recv
.copied
= -1;
653 if (transport
->recv
.offset
< transport
->recv
.len
) {
654 if (!(msg
.msg_flags
& MSG_TRUNC
))
657 ret
= xs_read_discard(transport
->sock
, &msg
, flags
,
658 transport
->recv
.len
- transport
->recv
.offset
);
661 transport
->recv
.offset
+= ret
;
663 if (transport
->recv
.offset
!= transport
->recv
.len
)
666 if (xs_read_stream_request_done(transport
)) {
667 trace_xs_stream_read_request(transport
);
668 transport
->recv
.copied
= 0;
670 transport
->recv
.offset
= 0;
671 transport
->recv
.len
= 0;
674 return ret
!= 0 ? ret
: -ESHUTDOWN
;
677 static __poll_t
xs_poll_socket(struct sock_xprt
*transport
)
679 return transport
->sock
->ops
->poll(transport
->file
, transport
->sock
,
683 static bool xs_poll_socket_readable(struct sock_xprt
*transport
)
685 __poll_t events
= xs_poll_socket(transport
);
687 return (events
& (EPOLLIN
| EPOLLRDNORM
)) && !(events
& EPOLLRDHUP
);
690 static void xs_poll_check_readable(struct sock_xprt
*transport
)
693 clear_bit(XPRT_SOCK_DATA_READY
, &transport
->sock_state
);
694 if (!xs_poll_socket_readable(transport
))
696 if (!test_and_set_bit(XPRT_SOCK_DATA_READY
, &transport
->sock_state
))
697 queue_work(xprtiod_workqueue
, &transport
->recv_worker
);
700 static void xs_stream_data_receive(struct sock_xprt
*transport
)
705 mutex_lock(&transport
->recv_mutex
);
706 if (transport
->sock
== NULL
)
709 ret
= xs_read_stream(transport
, MSG_DONTWAIT
);
715 if (ret
== -ESHUTDOWN
)
716 kernel_sock_shutdown(transport
->sock
, SHUT_RDWR
);
718 xs_poll_check_readable(transport
);
720 mutex_unlock(&transport
->recv_mutex
);
721 trace_xs_stream_read_data(&transport
->xprt
, ret
, read
);
724 static void xs_stream_data_receive_workfn(struct work_struct
*work
)
726 struct sock_xprt
*transport
=
727 container_of(work
, struct sock_xprt
, recv_worker
);
728 unsigned int pflags
= memalloc_nofs_save();
730 xs_stream_data_receive(transport
);
731 memalloc_nofs_restore(pflags
);
735 xs_stream_reset_connect(struct sock_xprt
*transport
)
737 transport
->recv
.offset
= 0;
738 transport
->recv
.len
= 0;
739 transport
->recv
.copied
= 0;
740 transport
->xmit
.offset
= 0;
744 xs_stream_start_connect(struct sock_xprt
*transport
)
746 transport
->xprt
.stat
.connect_count
++;
747 transport
->xprt
.stat
.connect_start
= jiffies
;
750 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
752 static int xs_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t seek
)
755 iov_iter_advance(&msg
->msg_iter
, seek
);
756 return sock_sendmsg(sock
, msg
);
759 static int xs_send_kvec(struct socket
*sock
, struct msghdr
*msg
, struct kvec
*vec
, size_t seek
)
761 iov_iter_kvec(&msg
->msg_iter
, WRITE
, vec
, 1, vec
->iov_len
);
762 return xs_sendmsg(sock
, msg
, seek
);
765 static int xs_send_pagedata(struct socket
*sock
, struct msghdr
*msg
, struct xdr_buf
*xdr
, size_t base
)
769 err
= xdr_alloc_bvec(xdr
, GFP_KERNEL
);
773 iov_iter_bvec(&msg
->msg_iter
, WRITE
, xdr
->bvec
,
774 xdr_buf_pagecount(xdr
),
775 xdr
->page_len
+ xdr
->page_base
);
776 return xs_sendmsg(sock
, msg
, base
+ xdr
->page_base
);
779 #define xs_record_marker_len() sizeof(rpc_fraghdr)
783 * - sending from byte 0 of the message
784 * - the message is wholly contained in @xdr's head iovec
786 static int xs_send_rm_and_kvec(struct socket
*sock
, struct msghdr
*msg
,
787 rpc_fraghdr marker
, struct kvec
*vec
, size_t base
)
789 struct kvec iov
[2] = {
792 .iov_len
= sizeof(marker
)
796 size_t len
= iov
[0].iov_len
+ iov
[1].iov_len
;
798 iov_iter_kvec(&msg
->msg_iter
, WRITE
, iov
, 2, len
);
799 return xs_sendmsg(sock
, msg
, base
);
803 * xs_sendpages - write pages directly to a socket
804 * @sock: socket to send on
805 * @addr: UDP only -- address of destination
806 * @addrlen: UDP only -- length of destination address
807 * @xdr: buffer containing this request
808 * @base: starting position in the buffer
809 * @rm: stream record marker field
810 * @sent_p: return the total number of bytes successfully queued for sending
813 static int xs_sendpages(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
, struct xdr_buf
*xdr
, unsigned int base
, rpc_fraghdr rm
, int *sent_p
)
815 struct msghdr msg
= {
817 .msg_namelen
= addrlen
,
818 .msg_flags
= XS_SENDMSG_FLAGS
| MSG_MORE
,
820 unsigned int rmsize
= rm
? sizeof(rm
) : 0;
821 unsigned int remainder
= rmsize
+ xdr
->len
- base
;
828 want
= xdr
->head
[0].iov_len
+ rmsize
;
830 unsigned int len
= want
- base
;
833 msg
.msg_flags
&= ~MSG_MORE
;
835 err
= xs_send_rm_and_kvec(sock
, &msg
, rm
,
836 &xdr
->head
[0], base
);
838 err
= xs_send_kvec(sock
, &msg
, &xdr
->head
[0], base
);
839 if (remainder
== 0 || err
!= len
)
846 if (base
< xdr
->page_len
) {
847 unsigned int len
= xdr
->page_len
- base
;
850 msg
.msg_flags
&= ~MSG_MORE
;
851 err
= xs_send_pagedata(sock
, &msg
, xdr
, base
);
852 if (remainder
== 0 || err
!= len
)
857 base
-= xdr
->page_len
;
859 if (base
>= xdr
->tail
[0].iov_len
)
861 msg
.msg_flags
&= ~MSG_MORE
;
862 err
= xs_send_kvec(sock
, &msg
, &xdr
->tail
[0], base
);
872 * xs_nospace - handle transmit was incomplete
873 * @req: pointer to RPC request
876 static int xs_nospace(struct rpc_rqst
*req
)
878 struct rpc_xprt
*xprt
= req
->rq_xprt
;
879 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
880 struct sock
*sk
= transport
->inet
;
883 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
884 req
->rq_task
->tk_pid
,
885 req
->rq_slen
- transport
->xmit
.offset
,
888 /* Protect against races with write_space */
889 spin_lock(&xprt
->transport_lock
);
891 /* Don't race with disconnect */
892 if (xprt_connected(xprt
)) {
893 /* wait for more buffer space */
894 sk
->sk_write_pending
++;
895 xprt_wait_for_buffer_space(xprt
);
899 spin_unlock(&xprt
->transport_lock
);
901 /* Race breaker in case memory is freed before above code is called */
902 if (ret
== -EAGAIN
) {
903 struct socket_wq
*wq
;
906 wq
= rcu_dereference(sk
->sk_wq
);
907 set_bit(SOCKWQ_ASYNC_NOSPACE
, &wq
->flags
);
910 sk
->sk_write_space(sk
);
916 xs_stream_prepare_request(struct rpc_rqst
*req
)
918 xdr_free_bvec(&req
->rq_rcv_buf
);
919 req
->rq_task
->tk_status
= xdr_alloc_bvec(&req
->rq_rcv_buf
, GFP_KERNEL
);
923 * Determine if the previous message in the stream was aborted before it
924 * could complete transmission.
927 xs_send_request_was_aborted(struct sock_xprt
*transport
, struct rpc_rqst
*req
)
929 return transport
->xmit
.offset
!= 0 && req
->rq_bytes_sent
== 0;
933 * Return the stream record marker field for a record of length < 2^31-1
936 xs_stream_record_marker(struct xdr_buf
*xdr
)
940 return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT
| (u32
)xdr
->len
);
944 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
945 * @req: pointer to RPC request
948 * 0: The request has been sent
949 * EAGAIN: The socket was blocked, please call again later to
950 * complete the request
951 * ENOTCONN: Caller needs to invoke connect logic then call again
952 * other: Some other error occured, the request was not sent
954 static int xs_local_send_request(struct rpc_rqst
*req
)
956 struct rpc_xprt
*xprt
= req
->rq_xprt
;
957 struct sock_xprt
*transport
=
958 container_of(xprt
, struct sock_xprt
, xprt
);
959 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
960 rpc_fraghdr rm
= xs_stream_record_marker(xdr
);
961 unsigned int msglen
= rm
? req
->rq_slen
+ sizeof(rm
) : req
->rq_slen
;
965 /* Close the stream if the previous transmission was incomplete */
966 if (xs_send_request_was_aborted(transport
, req
)) {
971 xs_pktdump("packet data:",
972 req
->rq_svec
->iov_base
, req
->rq_svec
->iov_len
);
974 req
->rq_xtime
= ktime_get();
975 status
= xs_sendpages(transport
->sock
, NULL
, 0, xdr
,
976 transport
->xmit
.offset
, rm
, &sent
);
977 dprintk("RPC: %s(%u) = %d\n",
978 __func__
, xdr
->len
- transport
->xmit
.offset
, status
);
980 if (status
== -EAGAIN
&& sock_writeable(transport
->inet
))
983 if (likely(sent
> 0) || status
== 0) {
984 transport
->xmit
.offset
+= sent
;
985 req
->rq_bytes_sent
= transport
->xmit
.offset
;
986 if (likely(req
->rq_bytes_sent
>= msglen
)) {
987 req
->rq_xmit_bytes_sent
+= transport
->xmit
.offset
;
988 transport
->xmit
.offset
= 0;
998 status
= xs_nospace(req
);
1001 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1013 * xs_udp_send_request - write an RPC request to a UDP socket
1014 * @req: pointer to RPC request
1017 * 0: The request has been sent
1018 * EAGAIN: The socket was blocked, please call again later to
1019 * complete the request
1020 * ENOTCONN: Caller needs to invoke connect logic then call again
1021 * other: Some other error occurred, the request was not sent
1023 static int xs_udp_send_request(struct rpc_rqst
*req
)
1025 struct rpc_xprt
*xprt
= req
->rq_xprt
;
1026 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1027 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
1031 xs_pktdump("packet data:",
1032 req
->rq_svec
->iov_base
,
1033 req
->rq_svec
->iov_len
);
1035 if (!xprt_bound(xprt
))
1038 if (!xprt_request_get_cong(xprt
, req
))
1041 req
->rq_xtime
= ktime_get();
1042 status
= xs_sendpages(transport
->sock
, xs_addr(xprt
), xprt
->addrlen
,
1045 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
1048 /* firewall is blocking us, don't return -EAGAIN or we end up looping */
1049 if (status
== -EPERM
)
1050 goto process_status
;
1052 if (status
== -EAGAIN
&& sock_writeable(transport
->inet
))
1055 if (sent
> 0 || status
== 0) {
1056 req
->rq_xmit_bytes_sent
+= sent
;
1057 if (sent
>= req
->rq_slen
)
1059 /* Still some bytes left; set up for a retry later. */
1067 /* Should we call xs_close() here? */
1070 status
= xs_nospace(req
);
1077 /* When the server has died, an ICMP port unreachable message
1078 * prompts ECONNREFUSED. */
1081 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1089 * xs_tcp_send_request - write an RPC request to a TCP socket
1090 * @req: pointer to RPC request
1093 * 0: The request has been sent
1094 * EAGAIN: The socket was blocked, please call again later to
1095 * complete the request
1096 * ENOTCONN: Caller needs to invoke connect logic then call again
1097 * other: Some other error occurred, the request was not sent
1099 * XXX: In the case of soft timeouts, should we eventually give up
1100 * if sendmsg is not able to make progress?
1102 static int xs_tcp_send_request(struct rpc_rqst
*req
)
1104 struct rpc_xprt
*xprt
= req
->rq_xprt
;
1105 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1106 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
1107 rpc_fraghdr rm
= xs_stream_record_marker(xdr
);
1108 unsigned int msglen
= rm
? req
->rq_slen
+ sizeof(rm
) : req
->rq_slen
;
1109 bool vm_wait
= false;
1113 /* Close the stream if the previous transmission was incomplete */
1114 if (xs_send_request_was_aborted(transport
, req
)) {
1115 if (transport
->sock
!= NULL
)
1116 kernel_sock_shutdown(transport
->sock
, SHUT_RDWR
);
1120 xs_pktdump("packet data:",
1121 req
->rq_svec
->iov_base
,
1122 req
->rq_svec
->iov_len
);
1124 if (test_bit(XPRT_SOCK_UPD_TIMEOUT
, &transport
->sock_state
))
1125 xs_tcp_set_socket_timeouts(xprt
, transport
->sock
);
1127 /* Continue transmitting the packet/record. We must be careful
1128 * to cope with writespace callbacks arriving _after_ we have
1129 * called sendmsg(). */
1130 req
->rq_xtime
= ktime_get();
1133 status
= xs_sendpages(transport
->sock
, NULL
, 0, xdr
,
1134 transport
->xmit
.offset
, rm
, &sent
);
1136 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
1137 xdr
->len
- transport
->xmit
.offset
, status
);
1139 /* If we've sent the entire packet, immediately
1140 * reset the count of bytes sent. */
1141 transport
->xmit
.offset
+= sent
;
1142 req
->rq_bytes_sent
= transport
->xmit
.offset
;
1143 if (likely(req
->rq_bytes_sent
>= msglen
)) {
1144 req
->rq_xmit_bytes_sent
+= transport
->xmit
.offset
;
1145 transport
->xmit
.offset
= 0;
1149 WARN_ON_ONCE(sent
== 0 && status
== 0);
1151 if (status
== -EAGAIN
) {
1153 * Return EAGAIN if we're sure we're hitting the
1154 * socket send buffer limits.
1156 if (test_bit(SOCK_NOSPACE
, &transport
->sock
->flags
))
1159 * Did we hit a memory allocation failure?
1165 /* Retry, knowing now that we're below the
1166 * socket send buffer limit
1180 /* Should we call xs_close() here? */
1183 status
= xs_nospace(req
);
1193 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1200 static void xs_save_old_callbacks(struct sock_xprt
*transport
, struct sock
*sk
)
1202 transport
->old_data_ready
= sk
->sk_data_ready
;
1203 transport
->old_state_change
= sk
->sk_state_change
;
1204 transport
->old_write_space
= sk
->sk_write_space
;
1205 transport
->old_error_report
= sk
->sk_error_report
;
1208 static void xs_restore_old_callbacks(struct sock_xprt
*transport
, struct sock
*sk
)
1210 sk
->sk_data_ready
= transport
->old_data_ready
;
1211 sk
->sk_state_change
= transport
->old_state_change
;
1212 sk
->sk_write_space
= transport
->old_write_space
;
1213 sk
->sk_error_report
= transport
->old_error_report
;
1216 static void xs_sock_reset_state_flags(struct rpc_xprt
*xprt
)
1218 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1220 clear_bit(XPRT_SOCK_DATA_READY
, &transport
->sock_state
);
1221 clear_bit(XPRT_SOCK_WAKE_ERROR
, &transport
->sock_state
);
1222 clear_bit(XPRT_SOCK_WAKE_WRITE
, &transport
->sock_state
);
1223 clear_bit(XPRT_SOCK_WAKE_DISCONNECT
, &transport
->sock_state
);
1226 static void xs_run_error_worker(struct sock_xprt
*transport
, unsigned int nr
)
1228 set_bit(nr
, &transport
->sock_state
);
1229 queue_work(xprtiod_workqueue
, &transport
->error_worker
);
1232 static void xs_sock_reset_connection_flags(struct rpc_xprt
*xprt
)
1234 smp_mb__before_atomic();
1235 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
1236 clear_bit(XPRT_CLOSING
, &xprt
->state
);
1237 xs_sock_reset_state_flags(xprt
);
1238 smp_mb__after_atomic();
1242 * xs_error_report - callback to handle TCP socket state errors
1245 * Note: we don't call sock_error() since there may be a rpc_task
1246 * using the socket, and so we don't want to clear sk->sk_err.
1248 static void xs_error_report(struct sock
*sk
)
1250 struct sock_xprt
*transport
;
1251 struct rpc_xprt
*xprt
;
1254 read_lock_bh(&sk
->sk_callback_lock
);
1255 if (!(xprt
= xprt_from_sock(sk
)))
1258 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1262 dprintk("RPC: xs_error_report client %p, error=%d...\n",
1264 trace_rpc_socket_error(xprt
, sk
->sk_socket
, err
);
1265 xs_run_error_worker(transport
, XPRT_SOCK_WAKE_ERROR
);
1267 read_unlock_bh(&sk
->sk_callback_lock
);
1270 static void xs_reset_transport(struct sock_xprt
*transport
)
1272 struct socket
*sock
= transport
->sock
;
1273 struct sock
*sk
= transport
->inet
;
1274 struct rpc_xprt
*xprt
= &transport
->xprt
;
1275 struct file
*filp
= transport
->file
;
1280 if (atomic_read(&transport
->xprt
.swapper
))
1281 sk_clear_memalloc(sk
);
1283 kernel_sock_shutdown(sock
, SHUT_RDWR
);
1285 mutex_lock(&transport
->recv_mutex
);
1286 write_lock_bh(&sk
->sk_callback_lock
);
1287 transport
->inet
= NULL
;
1288 transport
->sock
= NULL
;
1289 transport
->file
= NULL
;
1291 sk
->sk_user_data
= NULL
;
1293 xs_restore_old_callbacks(transport
, sk
);
1294 xprt_clear_connected(xprt
);
1295 write_unlock_bh(&sk
->sk_callback_lock
);
1296 xs_sock_reset_connection_flags(xprt
);
1297 /* Reset stream record info */
1298 xs_stream_reset_connect(transport
);
1299 mutex_unlock(&transport
->recv_mutex
);
1301 trace_rpc_socket_close(xprt
, sock
);
1304 xprt_disconnect_done(xprt
);
1308 * xs_close - close a socket
1311 * This is used when all requests are complete; ie, no DRC state remains
1312 * on the server we want to save.
1314 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1315 * xs_reset_transport() zeroing the socket from underneath a writer.
1317 static void xs_close(struct rpc_xprt
*xprt
)
1319 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1321 dprintk("RPC: xs_close xprt %p\n", xprt
);
1323 xs_reset_transport(transport
);
1324 xprt
->reestablish_timeout
= 0;
1327 static void xs_inject_disconnect(struct rpc_xprt
*xprt
)
1329 dprintk("RPC: injecting transport disconnect on xprt=%p\n",
1331 xprt_disconnect_done(xprt
);
1334 static void xs_xprt_free(struct rpc_xprt
*xprt
)
1336 xs_free_peer_addresses(xprt
);
1341 * xs_destroy - prepare to shutdown a transport
1342 * @xprt: doomed transport
1345 static void xs_destroy(struct rpc_xprt
*xprt
)
1347 struct sock_xprt
*transport
= container_of(xprt
,
1348 struct sock_xprt
, xprt
);
1349 dprintk("RPC: xs_destroy xprt %p\n", xprt
);
1351 cancel_delayed_work_sync(&transport
->connect_worker
);
1353 cancel_work_sync(&transport
->recv_worker
);
1354 cancel_work_sync(&transport
->error_worker
);
1356 module_put(THIS_MODULE
);
1360 * xs_udp_data_read_skb - receive callback for UDP sockets
1366 static void xs_udp_data_read_skb(struct rpc_xprt
*xprt
,
1368 struct sk_buff
*skb
)
1370 struct rpc_task
*task
;
1371 struct rpc_rqst
*rovr
;
1372 int repsize
, copied
;
1378 dprintk("RPC: impossible RPC reply size %d!\n", repsize
);
1382 /* Copy the XID from the skb... */
1383 xp
= skb_header_pointer(skb
, 0, sizeof(_xid
), &_xid
);
1387 /* Look up and lock the request corresponding to the given XID */
1388 spin_lock(&xprt
->queue_lock
);
1389 rovr
= xprt_lookup_rqst(xprt
, *xp
);
1392 xprt_pin_rqst(rovr
);
1393 xprt_update_rtt(rovr
->rq_task
);
1394 spin_unlock(&xprt
->queue_lock
);
1395 task
= rovr
->rq_task
;
1397 if ((copied
= rovr
->rq_private_buf
.buflen
) > repsize
)
1400 /* Suck it into the iovec, verify checksum if not done by hw. */
1401 if (csum_partial_copy_to_xdr(&rovr
->rq_private_buf
, skb
)) {
1402 spin_lock(&xprt
->queue_lock
);
1403 __UDPX_INC_STATS(sk
, UDP_MIB_INERRORS
);
1408 spin_lock(&xprt
->transport_lock
);
1409 xprt_adjust_cwnd(xprt
, task
, copied
);
1410 spin_unlock(&xprt
->transport_lock
);
1411 spin_lock(&xprt
->queue_lock
);
1412 xprt_complete_rqst(task
, copied
);
1413 __UDPX_INC_STATS(sk
, UDP_MIB_INDATAGRAMS
);
1415 xprt_unpin_rqst(rovr
);
1417 spin_unlock(&xprt
->queue_lock
);
1420 static void xs_udp_data_receive(struct sock_xprt
*transport
)
1422 struct sk_buff
*skb
;
1426 mutex_lock(&transport
->recv_mutex
);
1427 sk
= transport
->inet
;
1431 skb
= skb_recv_udp(sk
, 0, 1, &err
);
1434 xs_udp_data_read_skb(&transport
->xprt
, sk
, skb
);
1438 xs_poll_check_readable(transport
);
1440 mutex_unlock(&transport
->recv_mutex
);
1443 static void xs_udp_data_receive_workfn(struct work_struct
*work
)
1445 struct sock_xprt
*transport
=
1446 container_of(work
, struct sock_xprt
, recv_worker
);
1447 unsigned int pflags
= memalloc_nofs_save();
1449 xs_udp_data_receive(transport
);
1450 memalloc_nofs_restore(pflags
);
1454 * xs_data_ready - "data ready" callback for UDP sockets
1455 * @sk: socket with data to read
1458 static void xs_data_ready(struct sock
*sk
)
1460 struct rpc_xprt
*xprt
;
1462 read_lock_bh(&sk
->sk_callback_lock
);
1463 dprintk("RPC: xs_data_ready...\n");
1464 xprt
= xprt_from_sock(sk
);
1466 struct sock_xprt
*transport
= container_of(xprt
,
1467 struct sock_xprt
, xprt
);
1468 transport
->old_data_ready(sk
);
1469 /* Any data means we had a useful conversation, so
1470 * then we don't need to delay the next reconnect
1472 if (xprt
->reestablish_timeout
)
1473 xprt
->reestablish_timeout
= 0;
1474 if (!test_and_set_bit(XPRT_SOCK_DATA_READY
, &transport
->sock_state
))
1475 queue_work(xprtiod_workqueue
, &transport
->recv_worker
);
1477 read_unlock_bh(&sk
->sk_callback_lock
);
1481 * Helper function to force a TCP close if the server is sending
1482 * junk and/or it has put us in CLOSE_WAIT
1484 static void xs_tcp_force_close(struct rpc_xprt
*xprt
)
1486 xprt_force_disconnect(xprt
);
1489 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1490 static size_t xs_tcp_bc_maxpayload(struct rpc_xprt
*xprt
)
1494 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1497 * xs_tcp_state_change - callback to handle TCP socket state changes
1498 * @sk: socket whose state has changed
1501 static void xs_tcp_state_change(struct sock
*sk
)
1503 struct rpc_xprt
*xprt
;
1504 struct sock_xprt
*transport
;
1506 read_lock_bh(&sk
->sk_callback_lock
);
1507 if (!(xprt
= xprt_from_sock(sk
)))
1509 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt
);
1510 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1511 sk
->sk_state
, xprt_connected(xprt
),
1512 sock_flag(sk
, SOCK_DEAD
),
1513 sock_flag(sk
, SOCK_ZAPPED
),
1516 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1517 trace_rpc_socket_state_change(xprt
, sk
->sk_socket
);
1518 switch (sk
->sk_state
) {
1519 case TCP_ESTABLISHED
:
1520 if (!xprt_test_and_set_connected(xprt
)) {
1521 xprt
->connect_cookie
++;
1522 clear_bit(XPRT_SOCK_CONNECTING
, &transport
->sock_state
);
1523 xprt_clear_connecting(xprt
);
1525 xprt
->stat
.connect_count
++;
1526 xprt
->stat
.connect_time
+= (long)jiffies
-
1527 xprt
->stat
.connect_start
;
1528 xs_run_error_worker(transport
, XPRT_SOCK_WAKE_PENDING
);
1532 /* The client initiated a shutdown of the socket */
1533 xprt
->connect_cookie
++;
1534 xprt
->reestablish_timeout
= 0;
1535 set_bit(XPRT_CLOSING
, &xprt
->state
);
1536 smp_mb__before_atomic();
1537 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1538 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
1539 smp_mb__after_atomic();
1541 case TCP_CLOSE_WAIT
:
1542 /* The server initiated a shutdown of the socket */
1543 xprt
->connect_cookie
++;
1544 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1545 xs_run_error_worker(transport
, XPRT_SOCK_WAKE_DISCONNECT
);
1549 * If the server closed down the connection, make sure that
1550 * we back off before reconnecting
1552 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
1553 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
1556 set_bit(XPRT_CLOSING
, &xprt
->state
);
1557 smp_mb__before_atomic();
1558 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1559 smp_mb__after_atomic();
1562 if (test_and_clear_bit(XPRT_SOCK_CONNECTING
,
1563 &transport
->sock_state
))
1564 xprt_clear_connecting(xprt
);
1565 clear_bit(XPRT_CLOSING
, &xprt
->state
);
1566 /* Trigger the socket release */
1567 xs_run_error_worker(transport
, XPRT_SOCK_WAKE_DISCONNECT
);
1570 read_unlock_bh(&sk
->sk_callback_lock
);
1573 static void xs_write_space(struct sock
*sk
)
1575 struct socket_wq
*wq
;
1576 struct sock_xprt
*transport
;
1577 struct rpc_xprt
*xprt
;
1581 clear_bit(SOCK_NOSPACE
, &sk
->sk_socket
->flags
);
1583 if (unlikely(!(xprt
= xprt_from_sock(sk
))))
1585 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1587 wq
= rcu_dereference(sk
->sk_wq
);
1588 if (!wq
|| test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE
, &wq
->flags
) == 0)
1591 xs_run_error_worker(transport
, XPRT_SOCK_WAKE_WRITE
);
1592 sk
->sk_write_pending
--;
1598 * xs_udp_write_space - callback invoked when socket buffer space
1600 * @sk: socket whose state has changed
1602 * Called when more output buffer space is available for this socket.
1603 * We try not to wake our writers until they can make "significant"
1604 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1605 * with a bunch of small requests.
1607 static void xs_udp_write_space(struct sock
*sk
)
1609 read_lock_bh(&sk
->sk_callback_lock
);
1611 /* from net/core/sock.c:sock_def_write_space */
1612 if (sock_writeable(sk
))
1615 read_unlock_bh(&sk
->sk_callback_lock
);
1619 * xs_tcp_write_space - callback invoked when socket buffer space
1621 * @sk: socket whose state has changed
1623 * Called when more output buffer space is available for this socket.
1624 * We try not to wake our writers until they can make "significant"
1625 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1626 * with a bunch of small requests.
1628 static void xs_tcp_write_space(struct sock
*sk
)
1630 read_lock_bh(&sk
->sk_callback_lock
);
1632 /* from net/core/stream.c:sk_stream_write_space */
1633 if (sk_stream_is_writeable(sk
))
1636 read_unlock_bh(&sk
->sk_callback_lock
);
1639 static void xs_udp_do_set_buffer_size(struct rpc_xprt
*xprt
)
1641 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1642 struct sock
*sk
= transport
->inet
;
1644 if (transport
->rcvsize
) {
1645 sk
->sk_userlocks
|= SOCK_RCVBUF_LOCK
;
1646 sk
->sk_rcvbuf
= transport
->rcvsize
* xprt
->max_reqs
* 2;
1648 if (transport
->sndsize
) {
1649 sk
->sk_userlocks
|= SOCK_SNDBUF_LOCK
;
1650 sk
->sk_sndbuf
= transport
->sndsize
* xprt
->max_reqs
* 2;
1651 sk
->sk_write_space(sk
);
1656 * xs_udp_set_buffer_size - set send and receive limits
1657 * @xprt: generic transport
1658 * @sndsize: requested size of send buffer, in bytes
1659 * @rcvsize: requested size of receive buffer, in bytes
1661 * Set socket send and receive buffer size limits.
1663 static void xs_udp_set_buffer_size(struct rpc_xprt
*xprt
, size_t sndsize
, size_t rcvsize
)
1665 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1667 transport
->sndsize
= 0;
1669 transport
->sndsize
= sndsize
+ 1024;
1670 transport
->rcvsize
= 0;
1672 transport
->rcvsize
= rcvsize
+ 1024;
1674 xs_udp_do_set_buffer_size(xprt
);
1678 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1679 * @xprt: controlling transport
1680 * @task: task that timed out
1682 * Adjust the congestion window after a retransmit timeout has occurred.
1684 static void xs_udp_timer(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
1686 spin_lock(&xprt
->transport_lock
);
1687 xprt_adjust_cwnd(xprt
, task
, -ETIMEDOUT
);
1688 spin_unlock(&xprt
->transport_lock
);
1691 static int xs_get_random_port(void)
1693 unsigned short min
= xprt_min_resvport
, max
= xprt_max_resvport
;
1694 unsigned short range
;
1695 unsigned short rand
;
1699 range
= max
- min
+ 1;
1700 rand
= (unsigned short) prandom_u32() % range
;
1705 * xs_set_reuseaddr_port - set the socket's port and address reuse options
1708 * Note that this function has to be called on all sockets that share the
1709 * same port, and it must be called before binding.
1711 static void xs_sock_set_reuseport(struct socket
*sock
)
1715 kernel_setsockopt(sock
, SOL_SOCKET
, SO_REUSEPORT
,
1716 (char *)&opt
, sizeof(opt
));
1719 static unsigned short xs_sock_getport(struct socket
*sock
)
1721 struct sockaddr_storage buf
;
1722 unsigned short port
= 0;
1724 if (kernel_getsockname(sock
, (struct sockaddr
*)&buf
) < 0)
1726 switch (buf
.ss_family
) {
1728 port
= ntohs(((struct sockaddr_in6
*)&buf
)->sin6_port
);
1731 port
= ntohs(((struct sockaddr_in
*)&buf
)->sin_port
);
1738 * xs_set_port - reset the port number in the remote endpoint address
1739 * @xprt: generic transport
1740 * @port: new port number
1743 static void xs_set_port(struct rpc_xprt
*xprt
, unsigned short port
)
1745 dprintk("RPC: setting port for xprt %p to %u\n", xprt
, port
);
1747 rpc_set_port(xs_addr(xprt
), port
);
1748 xs_update_peer_port(xprt
);
1751 static void xs_set_srcport(struct sock_xprt
*transport
, struct socket
*sock
)
1753 if (transport
->srcport
== 0)
1754 transport
->srcport
= xs_sock_getport(sock
);
1757 static int xs_get_srcport(struct sock_xprt
*transport
)
1759 int port
= transport
->srcport
;
1761 if (port
== 0 && transport
->xprt
.resvport
)
1762 port
= xs_get_random_port();
1766 static unsigned short xs_next_srcport(struct sock_xprt
*transport
, unsigned short port
)
1768 if (transport
->srcport
!= 0)
1769 transport
->srcport
= 0;
1770 if (!transport
->xprt
.resvport
)
1772 if (port
<= xprt_min_resvport
|| port
> xprt_max_resvport
)
1773 return xprt_max_resvport
;
1776 static int xs_bind(struct sock_xprt
*transport
, struct socket
*sock
)
1778 struct sockaddr_storage myaddr
;
1780 int port
= xs_get_srcport(transport
);
1781 unsigned short last
;
1784 * If we are asking for any ephemeral port (i.e. port == 0 &&
1785 * transport->xprt.resvport == 0), don't bind. Let the local
1786 * port selection happen implicitly when the socket is used
1787 * (for example at connect time).
1789 * This ensures that we can continue to establish TCP
1790 * connections even when all local ephemeral ports are already
1791 * a part of some TCP connection. This makes no difference
1792 * for UDP sockets, but also doens't harm them.
1794 * If we're asking for any reserved port (i.e. port == 0 &&
1795 * transport->xprt.resvport == 1) xs_get_srcport above will
1796 * ensure that port is non-zero and we will bind as needed.
1801 memcpy(&myaddr
, &transport
->srcaddr
, transport
->xprt
.addrlen
);
1803 rpc_set_port((struct sockaddr
*)&myaddr
, port
);
1804 err
= kernel_bind(sock
, (struct sockaddr
*)&myaddr
,
1805 transport
->xprt
.addrlen
);
1807 transport
->srcport
= port
;
1811 port
= xs_next_srcport(transport
, port
);
1814 } while (err
== -EADDRINUSE
&& nloop
!= 2);
1816 if (myaddr
.ss_family
== AF_INET
)
1817 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__
,
1818 &((struct sockaddr_in
*)&myaddr
)->sin_addr
,
1819 port
, err
? "failed" : "ok", err
);
1821 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__
,
1822 &((struct sockaddr_in6
*)&myaddr
)->sin6_addr
,
1823 port
, err
? "failed" : "ok", err
);
1828 * We don't support autobind on AF_LOCAL sockets
1830 static void xs_local_rpcbind(struct rpc_task
*task
)
1832 xprt_set_bound(task
->tk_xprt
);
1835 static void xs_local_set_port(struct rpc_xprt
*xprt
, unsigned short port
)
1839 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1840 static struct lock_class_key xs_key
[2];
1841 static struct lock_class_key xs_slock_key
[2];
1843 static inline void xs_reclassify_socketu(struct socket
*sock
)
1845 struct sock
*sk
= sock
->sk
;
1847 sock_lock_init_class_and_name(sk
, "slock-AF_LOCAL-RPC",
1848 &xs_slock_key
[1], "sk_lock-AF_LOCAL-RPC", &xs_key
[1]);
1851 static inline void xs_reclassify_socket4(struct socket
*sock
)
1853 struct sock
*sk
= sock
->sk
;
1855 sock_lock_init_class_and_name(sk
, "slock-AF_INET-RPC",
1856 &xs_slock_key
[0], "sk_lock-AF_INET-RPC", &xs_key
[0]);
1859 static inline void xs_reclassify_socket6(struct socket
*sock
)
1861 struct sock
*sk
= sock
->sk
;
1863 sock_lock_init_class_and_name(sk
, "slock-AF_INET6-RPC",
1864 &xs_slock_key
[1], "sk_lock-AF_INET6-RPC", &xs_key
[1]);
1867 static inline void xs_reclassify_socket(int family
, struct socket
*sock
)
1869 if (WARN_ON_ONCE(!sock_allow_reclassification(sock
->sk
)))
1874 xs_reclassify_socketu(sock
);
1877 xs_reclassify_socket4(sock
);
1880 xs_reclassify_socket6(sock
);
1885 static inline void xs_reclassify_socket(int family
, struct socket
*sock
)
1890 static void xs_dummy_setup_socket(struct work_struct
*work
)
1894 static struct socket
*xs_create_sock(struct rpc_xprt
*xprt
,
1895 struct sock_xprt
*transport
, int family
, int type
,
1896 int protocol
, bool reuseport
)
1899 struct socket
*sock
;
1902 err
= __sock_create(xprt
->xprt_net
, family
, type
, protocol
, &sock
, 1);
1904 dprintk("RPC: can't create %d transport socket (%d).\n",
1908 xs_reclassify_socket(family
, sock
);
1911 xs_sock_set_reuseport(sock
);
1913 err
= xs_bind(transport
, sock
);
1919 filp
= sock_alloc_file(sock
, O_NONBLOCK
, NULL
);
1921 return ERR_CAST(filp
);
1922 transport
->file
= filp
;
1926 return ERR_PTR(err
);
1929 static int xs_local_finish_connecting(struct rpc_xprt
*xprt
,
1930 struct socket
*sock
)
1932 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
,
1935 if (!transport
->inet
) {
1936 struct sock
*sk
= sock
->sk
;
1938 write_lock_bh(&sk
->sk_callback_lock
);
1940 xs_save_old_callbacks(transport
, sk
);
1942 sk
->sk_user_data
= xprt
;
1943 sk
->sk_data_ready
= xs_data_ready
;
1944 sk
->sk_write_space
= xs_udp_write_space
;
1945 sock_set_flag(sk
, SOCK_FASYNC
);
1946 sk
->sk_error_report
= xs_error_report
;
1948 xprt_clear_connected(xprt
);
1950 /* Reset to new socket */
1951 transport
->sock
= sock
;
1952 transport
->inet
= sk
;
1954 write_unlock_bh(&sk
->sk_callback_lock
);
1957 xs_stream_start_connect(transport
);
1959 return kernel_connect(sock
, xs_addr(xprt
), xprt
->addrlen
, 0);
1963 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1964 * @transport: socket transport to connect
1966 static int xs_local_setup_socket(struct sock_xprt
*transport
)
1968 struct rpc_xprt
*xprt
= &transport
->xprt
;
1970 struct socket
*sock
;
1973 status
= __sock_create(xprt
->xprt_net
, AF_LOCAL
,
1974 SOCK_STREAM
, 0, &sock
, 1);
1976 dprintk("RPC: can't create AF_LOCAL "
1977 "transport socket (%d).\n", -status
);
1980 xs_reclassify_socket(AF_LOCAL
, sock
);
1982 filp
= sock_alloc_file(sock
, O_NONBLOCK
, NULL
);
1984 status
= PTR_ERR(filp
);
1987 transport
->file
= filp
;
1989 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1990 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
1992 status
= xs_local_finish_connecting(xprt
, sock
);
1993 trace_rpc_socket_connect(xprt
, sock
, status
);
1996 dprintk("RPC: xprt %p connected to %s\n",
1997 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
1998 xprt
->stat
.connect_count
++;
1999 xprt
->stat
.connect_time
+= (long)jiffies
-
2000 xprt
->stat
.connect_start
;
2001 xprt_set_connected(xprt
);
2005 dprintk("RPC: xprt %p: socket %s does not exist\n",
2006 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
2009 dprintk("RPC: xprt %p: connection refused for %s\n",
2010 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
2013 printk(KERN_ERR
"%s: unhandled error (%d) connecting to %s\n",
2015 xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
2019 xprt_clear_connecting(xprt
);
2020 xprt_wake_pending_tasks(xprt
, status
);
2024 static void xs_local_connect(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
2026 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2029 if (RPC_IS_ASYNC(task
)) {
2031 * We want the AF_LOCAL connect to be resolved in the
2032 * filesystem namespace of the process making the rpc
2033 * call. Thus we connect synchronously.
2035 * If we want to support asynchronous AF_LOCAL calls,
2036 * we'll need to figure out how to pass a namespace to
2039 task
->tk_rpc_status
= -ENOTCONN
;
2040 rpc_exit(task
, -ENOTCONN
);
2043 ret
= xs_local_setup_socket(transport
);
2044 if (ret
&& !RPC_IS_SOFTCONN(task
))
2045 msleep_interruptible(15000);
2048 #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
2050 * Note that this should be called with XPRT_LOCKED held (or when we otherwise
2051 * know that we have exclusive access to the socket), to guard against
2052 * races with xs_reset_transport.
2054 static void xs_set_memalloc(struct rpc_xprt
*xprt
)
2056 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
,
2060 * If there's no sock, then we have nothing to set. The
2061 * reconnecting process will get it for us.
2063 if (!transport
->inet
)
2065 if (atomic_read(&xprt
->swapper
))
2066 sk_set_memalloc(transport
->inet
);
2070 * xs_enable_swap - Tag this transport as being used for swap.
2071 * @xprt: transport to tag
2073 * Take a reference to this transport on behalf of the rpc_clnt, and
2074 * optionally mark it for swapping if it wasn't already.
2077 xs_enable_swap(struct rpc_xprt
*xprt
)
2079 struct sock_xprt
*xs
= container_of(xprt
, struct sock_xprt
, xprt
);
2081 if (atomic_inc_return(&xprt
->swapper
) != 1)
2083 if (wait_on_bit_lock(&xprt
->state
, XPRT_LOCKED
, TASK_KILLABLE
))
2084 return -ERESTARTSYS
;
2086 sk_set_memalloc(xs
->inet
);
2087 xprt_release_xprt(xprt
, NULL
);
2092 * xs_disable_swap - Untag this transport as being used for swap.
2093 * @xprt: transport to tag
2095 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
2096 * swapper refcount goes to 0, untag the socket as a memalloc socket.
2099 xs_disable_swap(struct rpc_xprt
*xprt
)
2101 struct sock_xprt
*xs
= container_of(xprt
, struct sock_xprt
, xprt
);
2103 if (!atomic_dec_and_test(&xprt
->swapper
))
2105 if (wait_on_bit_lock(&xprt
->state
, XPRT_LOCKED
, TASK_KILLABLE
))
2108 sk_clear_memalloc(xs
->inet
);
2109 xprt_release_xprt(xprt
, NULL
);
2112 static void xs_set_memalloc(struct rpc_xprt
*xprt
)
2117 xs_enable_swap(struct rpc_xprt
*xprt
)
2123 xs_disable_swap(struct rpc_xprt
*xprt
)
2128 static void xs_udp_finish_connecting(struct rpc_xprt
*xprt
, struct socket
*sock
)
2130 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2132 if (!transport
->inet
) {
2133 struct sock
*sk
= sock
->sk
;
2135 write_lock_bh(&sk
->sk_callback_lock
);
2137 xs_save_old_callbacks(transport
, sk
);
2139 sk
->sk_user_data
= xprt
;
2140 sk
->sk_data_ready
= xs_data_ready
;
2141 sk
->sk_write_space
= xs_udp_write_space
;
2142 sock_set_flag(sk
, SOCK_FASYNC
);
2144 xprt_set_connected(xprt
);
2146 /* Reset to new socket */
2147 transport
->sock
= sock
;
2148 transport
->inet
= sk
;
2150 xs_set_memalloc(xprt
);
2152 write_unlock_bh(&sk
->sk_callback_lock
);
2154 xs_udp_do_set_buffer_size(xprt
);
2156 xprt
->stat
.connect_start
= jiffies
;
2159 static void xs_udp_setup_socket(struct work_struct
*work
)
2161 struct sock_xprt
*transport
=
2162 container_of(work
, struct sock_xprt
, connect_worker
.work
);
2163 struct rpc_xprt
*xprt
= &transport
->xprt
;
2164 struct socket
*sock
;
2167 sock
= xs_create_sock(xprt
, transport
,
2168 xs_addr(xprt
)->sa_family
, SOCK_DGRAM
,
2169 IPPROTO_UDP
, false);
2173 dprintk("RPC: worker connecting xprt %p via %s to "
2174 "%s (port %s)\n", xprt
,
2175 xprt
->address_strings
[RPC_DISPLAY_PROTO
],
2176 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2177 xprt
->address_strings
[RPC_DISPLAY_PORT
]);
2179 xs_udp_finish_connecting(xprt
, sock
);
2180 trace_rpc_socket_connect(xprt
, sock
, 0);
2183 xprt_clear_connecting(xprt
);
2184 xprt_unlock_connect(xprt
, transport
);
2185 xprt_wake_pending_tasks(xprt
, status
);
2189 * xs_tcp_shutdown - gracefully shut down a TCP socket
2192 * Initiates a graceful shutdown of the TCP socket by calling the
2193 * equivalent of shutdown(SHUT_RDWR);
2195 static void xs_tcp_shutdown(struct rpc_xprt
*xprt
)
2197 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2198 struct socket
*sock
= transport
->sock
;
2199 int skst
= transport
->inet
? transport
->inet
->sk_state
: TCP_CLOSE
;
2205 kernel_sock_shutdown(sock
, SHUT_RDWR
);
2206 trace_rpc_socket_shutdown(xprt
, sock
);
2210 xs_reset_transport(transport
);
2214 static void xs_tcp_set_socket_timeouts(struct rpc_xprt
*xprt
,
2215 struct socket
*sock
)
2217 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2218 unsigned int keepidle
;
2219 unsigned int keepcnt
;
2220 unsigned int opt_on
= 1;
2223 spin_lock(&xprt
->transport_lock
);
2224 keepidle
= DIV_ROUND_UP(xprt
->timeout
->to_initval
, HZ
);
2225 keepcnt
= xprt
->timeout
->to_retries
+ 1;
2226 timeo
= jiffies_to_msecs(xprt
->timeout
->to_initval
) *
2227 (xprt
->timeout
->to_retries
+ 1);
2228 clear_bit(XPRT_SOCK_UPD_TIMEOUT
, &transport
->sock_state
);
2229 spin_unlock(&xprt
->transport_lock
);
2231 /* TCP Keepalive options */
2232 kernel_setsockopt(sock
, SOL_SOCKET
, SO_KEEPALIVE
,
2233 (char *)&opt_on
, sizeof(opt_on
));
2234 kernel_setsockopt(sock
, SOL_TCP
, TCP_KEEPIDLE
,
2235 (char *)&keepidle
, sizeof(keepidle
));
2236 kernel_setsockopt(sock
, SOL_TCP
, TCP_KEEPINTVL
,
2237 (char *)&keepidle
, sizeof(keepidle
));
2238 kernel_setsockopt(sock
, SOL_TCP
, TCP_KEEPCNT
,
2239 (char *)&keepcnt
, sizeof(keepcnt
));
2241 /* TCP user timeout (see RFC5482) */
2242 kernel_setsockopt(sock
, SOL_TCP
, TCP_USER_TIMEOUT
,
2243 (char *)&timeo
, sizeof(timeo
));
2246 static void xs_tcp_set_connect_timeout(struct rpc_xprt
*xprt
,
2247 unsigned long connect_timeout
,
2248 unsigned long reconnect_timeout
)
2250 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2251 struct rpc_timeout to
;
2252 unsigned long initval
;
2254 spin_lock(&xprt
->transport_lock
);
2255 if (reconnect_timeout
< xprt
->max_reconnect_timeout
)
2256 xprt
->max_reconnect_timeout
= reconnect_timeout
;
2257 if (connect_timeout
< xprt
->connect_timeout
) {
2258 memcpy(&to
, xprt
->timeout
, sizeof(to
));
2259 initval
= DIV_ROUND_UP(connect_timeout
, to
.to_retries
+ 1);
2260 /* Arbitrary lower limit */
2261 if (initval
< XS_TCP_INIT_REEST_TO
<< 1)
2262 initval
= XS_TCP_INIT_REEST_TO
<< 1;
2263 to
.to_initval
= initval
;
2264 to
.to_maxval
= initval
;
2265 memcpy(&transport
->tcp_timeout
, &to
,
2266 sizeof(transport
->tcp_timeout
));
2267 xprt
->timeout
= &transport
->tcp_timeout
;
2268 xprt
->connect_timeout
= connect_timeout
;
2270 set_bit(XPRT_SOCK_UPD_TIMEOUT
, &transport
->sock_state
);
2271 spin_unlock(&xprt
->transport_lock
);
2274 static int xs_tcp_finish_connecting(struct rpc_xprt
*xprt
, struct socket
*sock
)
2276 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2277 int ret
= -ENOTCONN
;
2279 if (!transport
->inet
) {
2280 struct sock
*sk
= sock
->sk
;
2281 unsigned int addr_pref
= IPV6_PREFER_SRC_PUBLIC
;
2283 /* Avoid temporary address, they are bad for long-lived
2284 * connections such as NFS mounts.
2285 * RFC4941, section 3.6 suggests that:
2286 * Individual applications, which have specific
2287 * knowledge about the normal duration of connections,
2288 * MAY override this as appropriate.
2290 kernel_setsockopt(sock
, SOL_IPV6
, IPV6_ADDR_PREFERENCES
,
2291 (char *)&addr_pref
, sizeof(addr_pref
));
2293 xs_tcp_set_socket_timeouts(xprt
, sock
);
2295 write_lock_bh(&sk
->sk_callback_lock
);
2297 xs_save_old_callbacks(transport
, sk
);
2299 sk
->sk_user_data
= xprt
;
2300 sk
->sk_data_ready
= xs_data_ready
;
2301 sk
->sk_state_change
= xs_tcp_state_change
;
2302 sk
->sk_write_space
= xs_tcp_write_space
;
2303 sock_set_flag(sk
, SOCK_FASYNC
);
2304 sk
->sk_error_report
= xs_error_report
;
2306 /* socket options */
2307 sock_reset_flag(sk
, SOCK_LINGER
);
2308 tcp_sk(sk
)->nonagle
|= TCP_NAGLE_OFF
;
2310 xprt_clear_connected(xprt
);
2312 /* Reset to new socket */
2313 transport
->sock
= sock
;
2314 transport
->inet
= sk
;
2316 write_unlock_bh(&sk
->sk_callback_lock
);
2319 if (!xprt_bound(xprt
))
2322 xs_set_memalloc(xprt
);
2324 xs_stream_start_connect(transport
);
2326 /* Tell the socket layer to start connecting... */
2327 set_bit(XPRT_SOCK_CONNECTING
, &transport
->sock_state
);
2328 ret
= kernel_connect(sock
, xs_addr(xprt
), xprt
->addrlen
, O_NONBLOCK
);
2331 xs_set_srcport(transport
, sock
);
2335 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
2336 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2338 case -EADDRNOTAVAIL
:
2339 /* Source port number is unavailable. Try a new one! */
2340 transport
->srcport
= 0;
2347 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2348 * @work: queued work item
2350 * Invoked by a work queue tasklet.
2352 static void xs_tcp_setup_socket(struct work_struct
*work
)
2354 struct sock_xprt
*transport
=
2355 container_of(work
, struct sock_xprt
, connect_worker
.work
);
2356 struct socket
*sock
= transport
->sock
;
2357 struct rpc_xprt
*xprt
= &transport
->xprt
;
2361 sock
= xs_create_sock(xprt
, transport
,
2362 xs_addr(xprt
)->sa_family
, SOCK_STREAM
,
2365 status
= PTR_ERR(sock
);
2370 dprintk("RPC: worker connecting xprt %p via %s to "
2371 "%s (port %s)\n", xprt
,
2372 xprt
->address_strings
[RPC_DISPLAY_PROTO
],
2373 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2374 xprt
->address_strings
[RPC_DISPLAY_PORT
]);
2376 status
= xs_tcp_finish_connecting(xprt
, sock
);
2377 trace_rpc_socket_connect(xprt
, sock
, status
);
2378 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2379 xprt
, -status
, xprt_connected(xprt
),
2380 sock
->sk
->sk_state
);
2383 printk("%s: connect returned unhandled error %d\n",
2386 case -EADDRNOTAVAIL
:
2387 /* We're probably in TIME_WAIT. Get rid of existing socket,
2390 xs_tcp_force_close(xprt
);
2395 xprt_unlock_connect(xprt
, transport
);
2398 /* Happens, for instance, if the user specified a link
2399 * local IPv6 address without a scope-id.
2409 * xs_tcp_force_close() wakes tasks with -EIO.
2410 * We need to wake them first to ensure the
2411 * correct error code.
2413 xprt_wake_pending_tasks(xprt
, status
);
2414 xs_tcp_force_close(xprt
);
2419 xprt_clear_connecting(xprt
);
2420 xprt_unlock_connect(xprt
, transport
);
2421 xprt_wake_pending_tasks(xprt
, status
);
2425 * xs_connect - connect a socket to a remote endpoint
2426 * @xprt: pointer to transport structure
2427 * @task: address of RPC task that manages state of connect request
2429 * TCP: If the remote end dropped the connection, delay reconnecting.
2431 * UDP socket connects are synchronous, but we use a work queue anyway
2432 * to guarantee that even unprivileged user processes can set up a
2433 * socket on a privileged port.
2435 * If a UDP socket connect fails, the delay behavior here prevents
2436 * retry floods (hard mounts).
2438 static void xs_connect(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
2440 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2441 unsigned long delay
= 0;
2443 WARN_ON_ONCE(!xprt_lock_connect(xprt
, task
, transport
));
2445 if (transport
->sock
!= NULL
) {
2446 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2448 xprt
, xprt
->reestablish_timeout
/ HZ
);
2450 /* Start by resetting any existing state */
2451 xs_reset_transport(transport
);
2453 delay
= xprt_reconnect_delay(xprt
);
2454 xprt_reconnect_backoff(xprt
, XS_TCP_INIT_REEST_TO
);
2457 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt
);
2459 queue_delayed_work(xprtiod_workqueue
,
2460 &transport
->connect_worker
,
2464 static void xs_wake_disconnect(struct sock_xprt
*transport
)
2466 if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT
, &transport
->sock_state
))
2467 xs_tcp_force_close(&transport
->xprt
);
2470 static void xs_wake_write(struct sock_xprt
*transport
)
2472 if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE
, &transport
->sock_state
))
2473 xprt_write_space(&transport
->xprt
);
2476 static void xs_wake_error(struct sock_xprt
*transport
)
2479 int sockerr_len
= sizeof(sockerr
);
2481 if (!test_bit(XPRT_SOCK_WAKE_ERROR
, &transport
->sock_state
))
2483 mutex_lock(&transport
->recv_mutex
);
2484 if (transport
->sock
== NULL
)
2486 if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR
, &transport
->sock_state
))
2488 if (kernel_getsockopt(transport
->sock
, SOL_SOCKET
, SO_ERROR
,
2489 (char *)&sockerr
, &sockerr_len
) != 0)
2492 xprt_wake_pending_tasks(&transport
->xprt
, sockerr
);
2494 mutex_unlock(&transport
->recv_mutex
);
2497 static void xs_wake_pending(struct sock_xprt
*transport
)
2499 if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING
, &transport
->sock_state
))
2500 xprt_wake_pending_tasks(&transport
->xprt
, -EAGAIN
);
2503 static void xs_error_handle(struct work_struct
*work
)
2505 struct sock_xprt
*transport
= container_of(work
,
2506 struct sock_xprt
, error_worker
);
2508 xs_wake_disconnect(transport
);
2509 xs_wake_write(transport
);
2510 xs_wake_error(transport
);
2511 xs_wake_pending(transport
);
2515 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2516 * @xprt: rpc_xprt struct containing statistics
2520 static void xs_local_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2524 if (xprt_connected(xprt
))
2525 idle_time
= (long)(jiffies
- xprt
->last_used
) / HZ
;
2527 seq_printf(seq
, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
2528 "%llu %llu %lu %llu %llu\n",
2529 xprt
->stat
.bind_count
,
2530 xprt
->stat
.connect_count
,
2531 xprt
->stat
.connect_time
/ HZ
,
2535 xprt
->stat
.bad_xids
,
2538 xprt
->stat
.max_slots
,
2539 xprt
->stat
.sending_u
,
2540 xprt
->stat
.pending_u
);
2544 * xs_udp_print_stats - display UDP socket-specifc stats
2545 * @xprt: rpc_xprt struct containing statistics
2549 static void xs_udp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2551 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2553 seq_printf(seq
, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2556 xprt
->stat
.bind_count
,
2559 xprt
->stat
.bad_xids
,
2562 xprt
->stat
.max_slots
,
2563 xprt
->stat
.sending_u
,
2564 xprt
->stat
.pending_u
);
2568 * xs_tcp_print_stats - display TCP socket-specifc stats
2569 * @xprt: rpc_xprt struct containing statistics
2573 static void xs_tcp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2575 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2578 if (xprt_connected(xprt
))
2579 idle_time
= (long)(jiffies
- xprt
->last_used
) / HZ
;
2581 seq_printf(seq
, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2582 "%llu %llu %lu %llu %llu\n",
2584 xprt
->stat
.bind_count
,
2585 xprt
->stat
.connect_count
,
2586 xprt
->stat
.connect_time
/ HZ
,
2590 xprt
->stat
.bad_xids
,
2593 xprt
->stat
.max_slots
,
2594 xprt
->stat
.sending_u
,
2595 xprt
->stat
.pending_u
);
2599 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2600 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2601 * to use the server side send routines.
2603 static int bc_malloc(struct rpc_task
*task
)
2605 struct rpc_rqst
*rqst
= task
->tk_rqstp
;
2606 size_t size
= rqst
->rq_callsize
;
2608 struct rpc_buffer
*buf
;
2610 if (size
> PAGE_SIZE
- sizeof(struct rpc_buffer
)) {
2611 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
2616 page
= alloc_page(GFP_KERNEL
);
2620 buf
= page_address(page
);
2621 buf
->len
= PAGE_SIZE
;
2623 rqst
->rq_buffer
= buf
->data
;
2624 rqst
->rq_rbuffer
= (char *)rqst
->rq_buffer
+ rqst
->rq_callsize
;
2629 * Free the space allocated in the bc_alloc routine
2631 static void bc_free(struct rpc_task
*task
)
2633 void *buffer
= task
->tk_rqstp
->rq_buffer
;
2634 struct rpc_buffer
*buf
;
2636 buf
= container_of(buffer
, struct rpc_buffer
, data
);
2637 free_page((unsigned long)buf
);
2641 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2642 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2644 static int bc_sendto(struct rpc_rqst
*req
)
2647 struct xdr_buf
*xbufp
= &req
->rq_snd_buf
;
2648 struct sock_xprt
*transport
=
2649 container_of(req
->rq_xprt
, struct sock_xprt
, xprt
);
2650 unsigned long headoff
;
2651 unsigned long tailoff
;
2652 struct page
*tailpage
;
2653 struct msghdr msg
= {
2654 .msg_flags
= MSG_MORE
2656 rpc_fraghdr marker
= cpu_to_be32(RPC_LAST_STREAM_FRAGMENT
|
2659 .iov_base
= &marker
,
2660 .iov_len
= sizeof(marker
),
2663 len
= kernel_sendmsg(transport
->sock
, &msg
, &iov
, 1, iov
.iov_len
);
2664 if (len
!= iov
.iov_len
)
2668 if (xbufp
->tail
[0].iov_len
)
2669 tailpage
= virt_to_page(xbufp
->tail
[0].iov_base
);
2670 tailoff
= (unsigned long)xbufp
->tail
[0].iov_base
& ~PAGE_MASK
;
2671 headoff
= (unsigned long)xbufp
->head
[0].iov_base
& ~PAGE_MASK
;
2672 len
= svc_send_common(transport
->sock
, xbufp
,
2673 virt_to_page(xbufp
->head
[0].iov_base
), headoff
,
2675 if (len
!= xbufp
->len
)
2681 * The send routine. Borrows from svc_send
2683 static int bc_send_request(struct rpc_rqst
*req
)
2685 struct svc_xprt
*xprt
;
2688 dprintk("sending request with xid: %08x\n", ntohl(req
->rq_xid
));
2690 * Get the server socket associated with this callback xprt
2692 xprt
= req
->rq_xprt
->bc_xprt
;
2695 * Grab the mutex to serialize data as the connection is shared
2696 * with the fore channel
2698 mutex_lock(&xprt
->xpt_mutex
);
2699 if (test_bit(XPT_DEAD
, &xprt
->xpt_flags
))
2702 len
= bc_sendto(req
);
2703 mutex_unlock(&xprt
->xpt_mutex
);
2712 * The close routine. Since this is client initiated, we do nothing
2715 static void bc_close(struct rpc_xprt
*xprt
)
2720 * The xprt destroy routine. Again, because this connection is client
2721 * initiated, we do nothing
2724 static void bc_destroy(struct rpc_xprt
*xprt
)
2726 dprintk("RPC: bc_destroy xprt %p\n", xprt
);
2729 module_put(THIS_MODULE
);
2732 static const struct rpc_xprt_ops xs_local_ops
= {
2733 .reserve_xprt
= xprt_reserve_xprt
,
2734 .release_xprt
= xprt_release_xprt
,
2735 .alloc_slot
= xprt_alloc_slot
,
2736 .free_slot
= xprt_free_slot
,
2737 .rpcbind
= xs_local_rpcbind
,
2738 .set_port
= xs_local_set_port
,
2739 .connect
= xs_local_connect
,
2740 .buf_alloc
= rpc_malloc
,
2741 .buf_free
= rpc_free
,
2742 .prepare_request
= xs_stream_prepare_request
,
2743 .send_request
= xs_local_send_request
,
2744 .wait_for_reply_request
= xprt_wait_for_reply_request_def
,
2746 .destroy
= xs_destroy
,
2747 .print_stats
= xs_local_print_stats
,
2748 .enable_swap
= xs_enable_swap
,
2749 .disable_swap
= xs_disable_swap
,
2752 static const struct rpc_xprt_ops xs_udp_ops
= {
2753 .set_buffer_size
= xs_udp_set_buffer_size
,
2754 .reserve_xprt
= xprt_reserve_xprt_cong
,
2755 .release_xprt
= xprt_release_xprt_cong
,
2756 .alloc_slot
= xprt_alloc_slot
,
2757 .free_slot
= xprt_free_slot
,
2758 .rpcbind
= rpcb_getport_async
,
2759 .set_port
= xs_set_port
,
2760 .connect
= xs_connect
,
2761 .buf_alloc
= rpc_malloc
,
2762 .buf_free
= rpc_free
,
2763 .send_request
= xs_udp_send_request
,
2764 .wait_for_reply_request
= xprt_wait_for_reply_request_rtt
,
2765 .timer
= xs_udp_timer
,
2766 .release_request
= xprt_release_rqst_cong
,
2768 .destroy
= xs_destroy
,
2769 .print_stats
= xs_udp_print_stats
,
2770 .enable_swap
= xs_enable_swap
,
2771 .disable_swap
= xs_disable_swap
,
2772 .inject_disconnect
= xs_inject_disconnect
,
2775 static const struct rpc_xprt_ops xs_tcp_ops
= {
2776 .reserve_xprt
= xprt_reserve_xprt
,
2777 .release_xprt
= xprt_release_xprt
,
2778 .alloc_slot
= xprt_alloc_slot
,
2779 .free_slot
= xprt_free_slot
,
2780 .rpcbind
= rpcb_getport_async
,
2781 .set_port
= xs_set_port
,
2782 .connect
= xs_connect
,
2783 .buf_alloc
= rpc_malloc
,
2784 .buf_free
= rpc_free
,
2785 .prepare_request
= xs_stream_prepare_request
,
2786 .send_request
= xs_tcp_send_request
,
2787 .wait_for_reply_request
= xprt_wait_for_reply_request_def
,
2788 .close
= xs_tcp_shutdown
,
2789 .destroy
= xs_destroy
,
2790 .set_connect_timeout
= xs_tcp_set_connect_timeout
,
2791 .print_stats
= xs_tcp_print_stats
,
2792 .enable_swap
= xs_enable_swap
,
2793 .disable_swap
= xs_disable_swap
,
2794 .inject_disconnect
= xs_inject_disconnect
,
2795 #ifdef CONFIG_SUNRPC_BACKCHANNEL
2796 .bc_setup
= xprt_setup_bc
,
2797 .bc_maxpayload
= xs_tcp_bc_maxpayload
,
2798 .bc_num_slots
= xprt_bc_max_slots
,
2799 .bc_free_rqst
= xprt_free_bc_rqst
,
2800 .bc_destroy
= xprt_destroy_bc
,
2805 * The rpc_xprt_ops for the server backchannel
2808 static const struct rpc_xprt_ops bc_tcp_ops
= {
2809 .reserve_xprt
= xprt_reserve_xprt
,
2810 .release_xprt
= xprt_release_xprt
,
2811 .alloc_slot
= xprt_alloc_slot
,
2812 .free_slot
= xprt_free_slot
,
2813 .buf_alloc
= bc_malloc
,
2814 .buf_free
= bc_free
,
2815 .send_request
= bc_send_request
,
2816 .wait_for_reply_request
= xprt_wait_for_reply_request_def
,
2818 .destroy
= bc_destroy
,
2819 .print_stats
= xs_tcp_print_stats
,
2820 .enable_swap
= xs_enable_swap
,
2821 .disable_swap
= xs_disable_swap
,
2822 .inject_disconnect
= xs_inject_disconnect
,
2825 static int xs_init_anyaddr(const int family
, struct sockaddr
*sap
)
2827 static const struct sockaddr_in sin
= {
2828 .sin_family
= AF_INET
,
2829 .sin_addr
.s_addr
= htonl(INADDR_ANY
),
2831 static const struct sockaddr_in6 sin6
= {
2832 .sin6_family
= AF_INET6
,
2833 .sin6_addr
= IN6ADDR_ANY_INIT
,
2840 memcpy(sap
, &sin
, sizeof(sin
));
2843 memcpy(sap
, &sin6
, sizeof(sin6
));
2846 dprintk("RPC: %s: Bad address family\n", __func__
);
2847 return -EAFNOSUPPORT
;
2852 static struct rpc_xprt
*xs_setup_xprt(struct xprt_create
*args
,
2853 unsigned int slot_table_size
,
2854 unsigned int max_slot_table_size
)
2856 struct rpc_xprt
*xprt
;
2857 struct sock_xprt
*new;
2859 if (args
->addrlen
> sizeof(xprt
->addr
)) {
2860 dprintk("RPC: xs_setup_xprt: address too large\n");
2861 return ERR_PTR(-EBADF
);
2864 xprt
= xprt_alloc(args
->net
, sizeof(*new), slot_table_size
,
2865 max_slot_table_size
);
2867 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2869 return ERR_PTR(-ENOMEM
);
2872 new = container_of(xprt
, struct sock_xprt
, xprt
);
2873 mutex_init(&new->recv_mutex
);
2874 memcpy(&xprt
->addr
, args
->dstaddr
, args
->addrlen
);
2875 xprt
->addrlen
= args
->addrlen
;
2877 memcpy(&new->srcaddr
, args
->srcaddr
, args
->addrlen
);
2880 err
= xs_init_anyaddr(args
->dstaddr
->sa_family
,
2881 (struct sockaddr
*)&new->srcaddr
);
2884 return ERR_PTR(err
);
2891 static const struct rpc_timeout xs_local_default_timeout
= {
2892 .to_initval
= 10 * HZ
,
2893 .to_maxval
= 10 * HZ
,
2898 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2899 * @args: rpc transport creation arguments
2901 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2903 static struct rpc_xprt
*xs_setup_local(struct xprt_create
*args
)
2905 struct sockaddr_un
*sun
= (struct sockaddr_un
*)args
->dstaddr
;
2906 struct sock_xprt
*transport
;
2907 struct rpc_xprt
*xprt
;
2908 struct rpc_xprt
*ret
;
2910 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
,
2911 xprt_max_tcp_slot_table_entries
);
2914 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2917 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
2919 xprt
->bind_timeout
= XS_BIND_TO
;
2920 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2921 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2923 xprt
->ops
= &xs_local_ops
;
2924 xprt
->timeout
= &xs_local_default_timeout
;
2926 INIT_WORK(&transport
->recv_worker
, xs_stream_data_receive_workfn
);
2927 INIT_WORK(&transport
->error_worker
, xs_error_handle
);
2928 INIT_DELAYED_WORK(&transport
->connect_worker
, xs_dummy_setup_socket
);
2930 switch (sun
->sun_family
) {
2932 if (sun
->sun_path
[0] != '/') {
2933 dprintk("RPC: bad AF_LOCAL address: %s\n",
2935 ret
= ERR_PTR(-EINVAL
);
2938 xprt_set_bound(xprt
);
2939 xs_format_peer_addresses(xprt
, "local", RPCBIND_NETID_LOCAL
);
2940 ret
= ERR_PTR(xs_local_setup_socket(transport
));
2945 ret
= ERR_PTR(-EAFNOSUPPORT
);
2949 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2950 xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
2952 if (try_module_get(THIS_MODULE
))
2954 ret
= ERR_PTR(-EINVAL
);
2960 static const struct rpc_timeout xs_udp_default_timeout
= {
2961 .to_initval
= 5 * HZ
,
2962 .to_maxval
= 30 * HZ
,
2963 .to_increment
= 5 * HZ
,
2968 * xs_setup_udp - Set up transport to use a UDP socket
2969 * @args: rpc transport creation arguments
2972 static struct rpc_xprt
*xs_setup_udp(struct xprt_create
*args
)
2974 struct sockaddr
*addr
= args
->dstaddr
;
2975 struct rpc_xprt
*xprt
;
2976 struct sock_xprt
*transport
;
2977 struct rpc_xprt
*ret
;
2979 xprt
= xs_setup_xprt(args
, xprt_udp_slot_table_entries
,
2980 xprt_udp_slot_table_entries
);
2983 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2985 xprt
->prot
= IPPROTO_UDP
;
2986 /* XXX: header size can vary due to auth type, IPv6, etc. */
2987 xprt
->max_payload
= (1U << 16) - (MAX_HEADER
<< 3);
2989 xprt
->bind_timeout
= XS_BIND_TO
;
2990 xprt
->reestablish_timeout
= XS_UDP_REEST_TO
;
2991 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2993 xprt
->ops
= &xs_udp_ops
;
2995 xprt
->timeout
= &xs_udp_default_timeout
;
2997 INIT_WORK(&transport
->recv_worker
, xs_udp_data_receive_workfn
);
2998 INIT_WORK(&transport
->error_worker
, xs_error_handle
);
2999 INIT_DELAYED_WORK(&transport
->connect_worker
, xs_udp_setup_socket
);
3001 switch (addr
->sa_family
) {
3003 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
3004 xprt_set_bound(xprt
);
3006 xs_format_peer_addresses(xprt
, "udp", RPCBIND_NETID_UDP
);
3009 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
3010 xprt_set_bound(xprt
);
3012 xs_format_peer_addresses(xprt
, "udp", RPCBIND_NETID_UDP6
);
3015 ret
= ERR_PTR(-EAFNOSUPPORT
);
3019 if (xprt_bound(xprt
))
3020 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3021 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
3022 xprt
->address_strings
[RPC_DISPLAY_PORT
],
3023 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
3025 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
3026 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
3027 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
3029 if (try_module_get(THIS_MODULE
))
3031 ret
= ERR_PTR(-EINVAL
);
3037 static const struct rpc_timeout xs_tcp_default_timeout
= {
3038 .to_initval
= 60 * HZ
,
3039 .to_maxval
= 60 * HZ
,
3044 * xs_setup_tcp - Set up transport to use a TCP socket
3045 * @args: rpc transport creation arguments
3048 static struct rpc_xprt
*xs_setup_tcp(struct xprt_create
*args
)
3050 struct sockaddr
*addr
= args
->dstaddr
;
3051 struct rpc_xprt
*xprt
;
3052 struct sock_xprt
*transport
;
3053 struct rpc_xprt
*ret
;
3054 unsigned int max_slot_table_size
= xprt_max_tcp_slot_table_entries
;
3056 if (args
->flags
& XPRT_CREATE_INFINITE_SLOTS
)
3057 max_slot_table_size
= RPC_MAX_SLOT_TABLE_LIMIT
;
3059 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
,
3060 max_slot_table_size
);
3063 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
3065 xprt
->prot
= IPPROTO_TCP
;
3066 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
3068 xprt
->bind_timeout
= XS_BIND_TO
;
3069 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
3070 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
3072 xprt
->ops
= &xs_tcp_ops
;
3073 xprt
->timeout
= &xs_tcp_default_timeout
;
3075 xprt
->max_reconnect_timeout
= xprt
->timeout
->to_maxval
;
3076 xprt
->connect_timeout
= xprt
->timeout
->to_initval
*
3077 (xprt
->timeout
->to_retries
+ 1);
3079 INIT_WORK(&transport
->recv_worker
, xs_stream_data_receive_workfn
);
3080 INIT_WORK(&transport
->error_worker
, xs_error_handle
);
3081 INIT_DELAYED_WORK(&transport
->connect_worker
, xs_tcp_setup_socket
);
3083 switch (addr
->sa_family
) {
3085 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
3086 xprt_set_bound(xprt
);
3088 xs_format_peer_addresses(xprt
, "tcp", RPCBIND_NETID_TCP
);
3091 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
3092 xprt_set_bound(xprt
);
3094 xs_format_peer_addresses(xprt
, "tcp", RPCBIND_NETID_TCP6
);
3097 ret
= ERR_PTR(-EAFNOSUPPORT
);
3101 if (xprt_bound(xprt
))
3102 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3103 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
3104 xprt
->address_strings
[RPC_DISPLAY_PORT
],
3105 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
3107 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
3108 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
3109 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
3111 if (try_module_get(THIS_MODULE
))
3113 ret
= ERR_PTR(-EINVAL
);
3120 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3121 * @args: rpc transport creation arguments
3124 static struct rpc_xprt
*xs_setup_bc_tcp(struct xprt_create
*args
)
3126 struct sockaddr
*addr
= args
->dstaddr
;
3127 struct rpc_xprt
*xprt
;
3128 struct sock_xprt
*transport
;
3129 struct svc_sock
*bc_sock
;
3130 struct rpc_xprt
*ret
;
3132 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
,
3133 xprt_tcp_slot_table_entries
);
3136 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
3138 xprt
->prot
= IPPROTO_TCP
;
3139 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
3140 xprt
->timeout
= &xs_tcp_default_timeout
;
3143 xprt_set_bound(xprt
);
3144 xprt
->bind_timeout
= 0;
3145 xprt
->reestablish_timeout
= 0;
3146 xprt
->idle_timeout
= 0;
3148 xprt
->ops
= &bc_tcp_ops
;
3150 switch (addr
->sa_family
) {
3152 xs_format_peer_addresses(xprt
, "tcp",
3156 xs_format_peer_addresses(xprt
, "tcp",
3157 RPCBIND_NETID_TCP6
);
3160 ret
= ERR_PTR(-EAFNOSUPPORT
);
3164 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3165 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
3166 xprt
->address_strings
[RPC_DISPLAY_PORT
],
3167 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
3170 * Once we've associated a backchannel xprt with a connection,
3171 * we want to keep it around as long as the connection lasts,
3172 * in case we need to start using it for a backchannel again;
3173 * this reference won't be dropped until bc_xprt is destroyed.
3176 args
->bc_xprt
->xpt_bc_xprt
= xprt
;
3177 xprt
->bc_xprt
= args
->bc_xprt
;
3178 bc_sock
= container_of(args
->bc_xprt
, struct svc_sock
, sk_xprt
);
3179 transport
->sock
= bc_sock
->sk_sock
;
3180 transport
->inet
= bc_sock
->sk_sk
;
3183 * Since we don't want connections for the backchannel, we set
3184 * the xprt status to connected
3186 xprt_set_connected(xprt
);
3188 if (try_module_get(THIS_MODULE
))
3191 args
->bc_xprt
->xpt_bc_xprt
= NULL
;
3192 args
->bc_xprt
->xpt_bc_xps
= NULL
;
3194 ret
= ERR_PTR(-EINVAL
);
3200 static struct xprt_class xs_local_transport
= {
3201 .list
= LIST_HEAD_INIT(xs_local_transport
.list
),
3202 .name
= "named UNIX socket",
3203 .owner
= THIS_MODULE
,
3204 .ident
= XPRT_TRANSPORT_LOCAL
,
3205 .setup
= xs_setup_local
,
3208 static struct xprt_class xs_udp_transport
= {
3209 .list
= LIST_HEAD_INIT(xs_udp_transport
.list
),
3211 .owner
= THIS_MODULE
,
3212 .ident
= XPRT_TRANSPORT_UDP
,
3213 .setup
= xs_setup_udp
,
3216 static struct xprt_class xs_tcp_transport
= {
3217 .list
= LIST_HEAD_INIT(xs_tcp_transport
.list
),
3219 .owner
= THIS_MODULE
,
3220 .ident
= XPRT_TRANSPORT_TCP
,
3221 .setup
= xs_setup_tcp
,
3224 static struct xprt_class xs_bc_tcp_transport
= {
3225 .list
= LIST_HEAD_INIT(xs_bc_tcp_transport
.list
),
3226 .name
= "tcp NFSv4.1 backchannel",
3227 .owner
= THIS_MODULE
,
3228 .ident
= XPRT_TRANSPORT_BC_TCP
,
3229 .setup
= xs_setup_bc_tcp
,
3233 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
3236 int init_socket_xprt(void)
3238 if (!sunrpc_table_header
)
3239 sunrpc_table_header
= register_sysctl_table(sunrpc_table
);
3241 xprt_register_transport(&xs_local_transport
);
3242 xprt_register_transport(&xs_udp_transport
);
3243 xprt_register_transport(&xs_tcp_transport
);
3244 xprt_register_transport(&xs_bc_tcp_transport
);
3250 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3253 void cleanup_socket_xprt(void)
3255 if (sunrpc_table_header
) {
3256 unregister_sysctl_table(sunrpc_table_header
);
3257 sunrpc_table_header
= NULL
;
3260 xprt_unregister_transport(&xs_local_transport
);
3261 xprt_unregister_transport(&xs_udp_transport
);
3262 xprt_unregister_transport(&xs_tcp_transport
);
3263 xprt_unregister_transport(&xs_bc_tcp_transport
);
3266 static int param_set_uint_minmax(const char *val
,
3267 const struct kernel_param
*kp
,
3268 unsigned int min
, unsigned int max
)
3275 ret
= kstrtouint(val
, 0, &num
);
3278 if (num
< min
|| num
> max
)
3280 *((unsigned int *)kp
->arg
) = num
;
3284 static int param_set_portnr(const char *val
, const struct kernel_param
*kp
)
3286 return param_set_uint_minmax(val
, kp
,
3291 static const struct kernel_param_ops param_ops_portnr
= {
3292 .set
= param_set_portnr
,
3293 .get
= param_get_uint
,
3296 #define param_check_portnr(name, p) \
3297 __param_check(name, p, unsigned int);
3299 module_param_named(min_resvport
, xprt_min_resvport
, portnr
, 0644);
3300 module_param_named(max_resvport
, xprt_max_resvport
, portnr
, 0644);
3302 static int param_set_slot_table_size(const char *val
,
3303 const struct kernel_param
*kp
)
3305 return param_set_uint_minmax(val
, kp
,
3307 RPC_MAX_SLOT_TABLE
);
3310 static const struct kernel_param_ops param_ops_slot_table_size
= {
3311 .set
= param_set_slot_table_size
,
3312 .get
= param_get_uint
,
3315 #define param_check_slot_table_size(name, p) \
3316 __param_check(name, p, unsigned int);
3318 static int param_set_max_slot_table_size(const char *val
,
3319 const struct kernel_param
*kp
)
3321 return param_set_uint_minmax(val
, kp
,
3323 RPC_MAX_SLOT_TABLE_LIMIT
);
3326 static const struct kernel_param_ops param_ops_max_slot_table_size
= {
3327 .set
= param_set_max_slot_table_size
,
3328 .get
= param_get_uint
,
3331 #define param_check_max_slot_table_size(name, p) \
3332 __param_check(name, p, unsigned int);
3334 module_param_named(tcp_slot_table_entries
, xprt_tcp_slot_table_entries
,
3335 slot_table_size
, 0644);
3336 module_param_named(tcp_max_slot_table_entries
, xprt_max_tcp_slot_table_entries
,
3337 max_slot_table_size
, 0644);
3338 module_param_named(udp_slot_table_entries
, xprt_udp_slot_table_entries
,
3339 slot_table_size
, 0644);