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>
60 static void xs_close(struct rpc_xprt
*xprt
);
61 static void xs_tcp_set_socket_timeouts(struct rpc_xprt
*xprt
,
67 static unsigned int xprt_udp_slot_table_entries
= RPC_DEF_SLOT_TABLE
;
68 static unsigned int xprt_tcp_slot_table_entries
= RPC_MIN_SLOT_TABLE
;
69 static unsigned int xprt_max_tcp_slot_table_entries
= RPC_MAX_SLOT_TABLE
;
71 static unsigned int xprt_min_resvport
= RPC_DEF_MIN_RESVPORT
;
72 static unsigned int xprt_max_resvport
= RPC_DEF_MAX_RESVPORT
;
74 #define XS_TCP_LINGER_TO (15U * HZ)
75 static unsigned int xs_tcp_fin_timeout __read_mostly
= XS_TCP_LINGER_TO
;
78 * We can register our own files under /proc/sys/sunrpc by
79 * calling register_sysctl_table() again. The files in that
80 * directory become the union of all files registered there.
82 * We simply need to make sure that we don't collide with
83 * someone else's file names!
86 static unsigned int min_slot_table_size
= RPC_MIN_SLOT_TABLE
;
87 static unsigned int max_slot_table_size
= RPC_MAX_SLOT_TABLE
;
88 static unsigned int max_tcp_slot_table_limit
= RPC_MAX_SLOT_TABLE_LIMIT
;
89 static unsigned int xprt_min_resvport_limit
= RPC_MIN_RESVPORT
;
90 static unsigned int xprt_max_resvport_limit
= RPC_MAX_RESVPORT
;
92 static struct ctl_table_header
*sunrpc_table_header
;
95 * FIXME: changing the UDP slot table size should also resize the UDP
96 * socket buffers for existing UDP transports
98 static struct ctl_table xs_tunables_table
[] = {
100 .procname
= "udp_slot_table_entries",
101 .data
= &xprt_udp_slot_table_entries
,
102 .maxlen
= sizeof(unsigned int),
104 .proc_handler
= proc_dointvec_minmax
,
105 .extra1
= &min_slot_table_size
,
106 .extra2
= &max_slot_table_size
109 .procname
= "tcp_slot_table_entries",
110 .data
= &xprt_tcp_slot_table_entries
,
111 .maxlen
= sizeof(unsigned int),
113 .proc_handler
= proc_dointvec_minmax
,
114 .extra1
= &min_slot_table_size
,
115 .extra2
= &max_slot_table_size
118 .procname
= "tcp_max_slot_table_entries",
119 .data
= &xprt_max_tcp_slot_table_entries
,
120 .maxlen
= sizeof(unsigned int),
122 .proc_handler
= proc_dointvec_minmax
,
123 .extra1
= &min_slot_table_size
,
124 .extra2
= &max_tcp_slot_table_limit
127 .procname
= "min_resvport",
128 .data
= &xprt_min_resvport
,
129 .maxlen
= sizeof(unsigned int),
131 .proc_handler
= proc_dointvec_minmax
,
132 .extra1
= &xprt_min_resvport_limit
,
133 .extra2
= &xprt_max_resvport_limit
136 .procname
= "max_resvport",
137 .data
= &xprt_max_resvport
,
138 .maxlen
= sizeof(unsigned int),
140 .proc_handler
= proc_dointvec_minmax
,
141 .extra1
= &xprt_min_resvport_limit
,
142 .extra2
= &xprt_max_resvport_limit
145 .procname
= "tcp_fin_timeout",
146 .data
= &xs_tcp_fin_timeout
,
147 .maxlen
= sizeof(xs_tcp_fin_timeout
),
149 .proc_handler
= proc_dointvec_jiffies
,
154 static struct ctl_table sunrpc_table
[] = {
156 .procname
= "sunrpc",
158 .child
= xs_tunables_table
164 * Wait duration for a reply from the RPC portmapper.
166 #define XS_BIND_TO (60U * HZ)
169 * Delay if a UDP socket connect error occurs. This is most likely some
170 * kind of resource problem on the local host.
172 #define XS_UDP_REEST_TO (2U * HZ)
175 * The reestablish timeout allows clients to delay for a bit before attempting
176 * to reconnect to a server that just dropped our connection.
178 * We implement an exponential backoff when trying to reestablish a TCP
179 * transport connection with the server. Some servers like to drop a TCP
180 * connection when they are overworked, so we start with a short timeout and
181 * increase over time if the server is down or not responding.
183 #define XS_TCP_INIT_REEST_TO (3U * HZ)
186 * TCP idle timeout; client drops the transport socket if it is idle
187 * for this long. Note that we also timeout UDP sockets to prevent
188 * holding port numbers when there is no RPC traffic.
190 #define XS_IDLE_DISC_TO (5U * 60 * HZ)
192 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
193 # undef RPC_DEBUG_DATA
194 # define RPCDBG_FACILITY RPCDBG_TRANS
197 #ifdef RPC_DEBUG_DATA
198 static void xs_pktdump(char *msg
, u32
*packet
, unsigned int count
)
200 u8
*buf
= (u8
*) packet
;
203 dprintk("RPC: %s\n", msg
);
204 for (j
= 0; j
< count
&& j
< 128; j
+= 4) {
208 dprintk("0x%04x ", j
);
210 dprintk("%02x%02x%02x%02x ",
211 buf
[j
], buf
[j
+1], buf
[j
+2], buf
[j
+3]);
216 static inline void xs_pktdump(char *msg
, u32
*packet
, unsigned int count
)
222 static inline struct rpc_xprt
*xprt_from_sock(struct sock
*sk
)
224 return (struct rpc_xprt
*) sk
->sk_user_data
;
227 static inline struct sockaddr
*xs_addr(struct rpc_xprt
*xprt
)
229 return (struct sockaddr
*) &xprt
->addr
;
232 static inline struct sockaddr_un
*xs_addr_un(struct rpc_xprt
*xprt
)
234 return (struct sockaddr_un
*) &xprt
->addr
;
237 static inline struct sockaddr_in
*xs_addr_in(struct rpc_xprt
*xprt
)
239 return (struct sockaddr_in
*) &xprt
->addr
;
242 static inline struct sockaddr_in6
*xs_addr_in6(struct rpc_xprt
*xprt
)
244 return (struct sockaddr_in6
*) &xprt
->addr
;
247 static void xs_format_common_peer_addresses(struct rpc_xprt
*xprt
)
249 struct sockaddr
*sap
= xs_addr(xprt
);
250 struct sockaddr_in6
*sin6
;
251 struct sockaddr_in
*sin
;
252 struct sockaddr_un
*sun
;
255 switch (sap
->sa_family
) {
257 sun
= xs_addr_un(xprt
);
258 strlcpy(buf
, sun
->sun_path
, sizeof(buf
));
259 xprt
->address_strings
[RPC_DISPLAY_ADDR
] =
260 kstrdup(buf
, GFP_KERNEL
);
263 (void)rpc_ntop(sap
, buf
, sizeof(buf
));
264 xprt
->address_strings
[RPC_DISPLAY_ADDR
] =
265 kstrdup(buf
, GFP_KERNEL
);
266 sin
= xs_addr_in(xprt
);
267 snprintf(buf
, sizeof(buf
), "%08x", ntohl(sin
->sin_addr
.s_addr
));
270 (void)rpc_ntop(sap
, buf
, sizeof(buf
));
271 xprt
->address_strings
[RPC_DISPLAY_ADDR
] =
272 kstrdup(buf
, GFP_KERNEL
);
273 sin6
= xs_addr_in6(xprt
);
274 snprintf(buf
, sizeof(buf
), "%pi6", &sin6
->sin6_addr
);
280 xprt
->address_strings
[RPC_DISPLAY_HEX_ADDR
] = kstrdup(buf
, GFP_KERNEL
);
283 static void xs_format_common_peer_ports(struct rpc_xprt
*xprt
)
285 struct sockaddr
*sap
= xs_addr(xprt
);
288 snprintf(buf
, sizeof(buf
), "%u", rpc_get_port(sap
));
289 xprt
->address_strings
[RPC_DISPLAY_PORT
] = kstrdup(buf
, GFP_KERNEL
);
291 snprintf(buf
, sizeof(buf
), "%4hx", rpc_get_port(sap
));
292 xprt
->address_strings
[RPC_DISPLAY_HEX_PORT
] = kstrdup(buf
, GFP_KERNEL
);
295 static void xs_format_peer_addresses(struct rpc_xprt
*xprt
,
296 const char *protocol
,
299 xprt
->address_strings
[RPC_DISPLAY_PROTO
] = protocol
;
300 xprt
->address_strings
[RPC_DISPLAY_NETID
] = netid
;
301 xs_format_common_peer_addresses(xprt
);
302 xs_format_common_peer_ports(xprt
);
305 static void xs_update_peer_port(struct rpc_xprt
*xprt
)
307 kfree(xprt
->address_strings
[RPC_DISPLAY_HEX_PORT
]);
308 kfree(xprt
->address_strings
[RPC_DISPLAY_PORT
]);
310 xs_format_common_peer_ports(xprt
);
313 static void xs_free_peer_addresses(struct rpc_xprt
*xprt
)
317 for (i
= 0; i
< RPC_DISPLAY_MAX
; i
++)
319 case RPC_DISPLAY_PROTO
:
320 case RPC_DISPLAY_NETID
:
323 kfree(xprt
->address_strings
[i
]);
328 xs_alloc_sparse_pages(struct xdr_buf
*buf
, size_t want
, gfp_t gfp
)
332 if (!want
|| !(buf
->flags
& XDRBUF_SPARSE_PAGES
))
334 n
= (buf
->page_base
+ want
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
335 for (i
= 0; i
< n
; i
++) {
338 buf
->bvec
[i
].bv_page
= buf
->pages
[i
] = alloc_page(gfp
);
339 if (!buf
->pages
[i
]) {
341 return i
> buf
->page_base
? i
- buf
->page_base
: 0;
348 xs_sock_recvmsg(struct socket
*sock
, struct msghdr
*msg
, int flags
, size_t seek
)
352 iov_iter_advance(&msg
->msg_iter
, seek
);
353 ret
= sock_recvmsg(sock
, msg
, flags
);
354 return ret
> 0 ? ret
+ seek
: ret
;
358 xs_read_kvec(struct socket
*sock
, struct msghdr
*msg
, int flags
,
359 struct kvec
*kvec
, size_t count
, size_t seek
)
361 iov_iter_kvec(&msg
->msg_iter
, READ
, kvec
, 1, count
);
362 return xs_sock_recvmsg(sock
, msg
, flags
, seek
);
366 xs_read_bvec(struct socket
*sock
, struct msghdr
*msg
, int flags
,
367 struct bio_vec
*bvec
, unsigned long nr
, size_t count
,
370 iov_iter_bvec(&msg
->msg_iter
, READ
, bvec
, nr
, count
);
371 return xs_sock_recvmsg(sock
, msg
, flags
, seek
);
375 xs_read_discard(struct socket
*sock
, struct msghdr
*msg
, int flags
,
378 iov_iter_discard(&msg
->msg_iter
, READ
, count
);
379 return sock_recvmsg(sock
, msg
, flags
);
382 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
384 xs_flush_bvec(const struct bio_vec
*bvec
, size_t count
, size_t seek
)
386 struct bvec_iter bi
= {
391 bvec_iter_advance(bvec
, &bi
, seek
& PAGE_MASK
);
392 for_each_bvec(bv
, bvec
, bi
, bi
)
393 flush_dcache_page(bv
.bv_page
);
397 xs_flush_bvec(const struct bio_vec
*bvec
, size_t count
, size_t seek
)
403 xs_read_xdr_buf(struct socket
*sock
, struct msghdr
*msg
, int flags
,
404 struct xdr_buf
*buf
, size_t count
, size_t seek
, size_t *read
)
406 size_t want
, seek_init
= seek
, offset
= 0;
409 want
= min_t(size_t, count
, buf
->head
[0].iov_len
);
411 ret
= xs_read_kvec(sock
, msg
, flags
, &buf
->head
[0], want
, seek
);
415 if (offset
== count
|| msg
->msg_flags
& (MSG_EOR
|MSG_TRUNC
))
425 want
= xs_alloc_sparse_pages(buf
,
426 min_t(size_t, count
- offset
, buf
->page_len
),
429 ret
= xs_read_bvec(sock
, msg
, flags
, buf
->bvec
,
430 xdr_buf_pagecount(buf
),
431 want
+ buf
->page_base
,
432 seek
+ buf
->page_base
);
435 xs_flush_bvec(buf
->bvec
, ret
, seek
+ buf
->page_base
);
436 offset
+= ret
- buf
->page_base
;
437 if (offset
== count
|| msg
->msg_flags
& (MSG_EOR
|MSG_TRUNC
))
447 want
= min_t(size_t, count
- offset
, buf
->tail
[0].iov_len
);
449 ret
= xs_read_kvec(sock
, msg
, flags
, &buf
->tail
[0], want
, seek
);
453 if (offset
== count
|| msg
->msg_flags
& (MSG_EOR
|MSG_TRUNC
))
457 } else if (offset
< seek_init
)
461 *read
= offset
- seek_init
;
469 xs_read_header(struct sock_xprt
*transport
, struct xdr_buf
*buf
)
471 if (!transport
->recv
.copied
) {
472 if (buf
->head
[0].iov_len
>= transport
->recv
.offset
)
473 memcpy(buf
->head
[0].iov_base
,
474 &transport
->recv
.xid
,
475 transport
->recv
.offset
);
476 transport
->recv
.copied
= transport
->recv
.offset
;
481 xs_read_stream_request_done(struct sock_xprt
*transport
)
483 return transport
->recv
.fraghdr
& cpu_to_be32(RPC_LAST_STREAM_FRAGMENT
);
487 xs_read_stream_check_eor(struct sock_xprt
*transport
,
490 if (xs_read_stream_request_done(transport
))
491 msg
->msg_flags
|= MSG_EOR
;
495 xs_read_stream_request(struct sock_xprt
*transport
, struct msghdr
*msg
,
496 int flags
, struct rpc_rqst
*req
)
498 struct xdr_buf
*buf
= &req
->rq_private_buf
;
499 size_t want
, uninitialized_var(read
);
500 ssize_t
uninitialized_var(ret
);
502 xs_read_header(transport
, buf
);
504 want
= transport
->recv
.len
- transport
->recv
.offset
;
506 ret
= xs_read_xdr_buf(transport
->sock
, msg
, flags
, buf
,
507 transport
->recv
.copied
+ want
,
508 transport
->recv
.copied
,
510 transport
->recv
.offset
+= read
;
511 transport
->recv
.copied
+= read
;
514 if (transport
->recv
.offset
== transport
->recv
.len
)
515 xs_read_stream_check_eor(transport
, msg
);
525 msg
->msg_flags
|= MSG_TRUNC
;
530 return ret
< 0 ? ret
: read
;
534 xs_read_stream_headersize(bool isfrag
)
537 return sizeof(__be32
);
538 return 3 * sizeof(__be32
);
542 xs_read_stream_header(struct sock_xprt
*transport
, struct msghdr
*msg
,
543 int flags
, size_t want
, size_t seek
)
546 .iov_base
= &transport
->recv
.fraghdr
,
549 return xs_read_kvec(transport
->sock
, msg
, flags
, &kvec
, want
, seek
);
552 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
554 xs_read_stream_call(struct sock_xprt
*transport
, struct msghdr
*msg
, int flags
)
556 struct rpc_xprt
*xprt
= &transport
->xprt
;
557 struct rpc_rqst
*req
;
560 /* Look up and lock the request corresponding to the given XID */
561 req
= xprt_lookup_bc_request(xprt
, transport
->recv
.xid
);
563 printk(KERN_WARNING
"Callback slot table overflowed\n");
566 if (transport
->recv
.copied
&& !req
->rq_private_buf
.len
)
569 ret
= xs_read_stream_request(transport
, msg
, flags
, req
);
570 if (msg
->msg_flags
& (MSG_EOR
|MSG_TRUNC
))
571 xprt_complete_bc_request(req
, transport
->recv
.copied
);
573 req
->rq_private_buf
.len
= transport
->recv
.copied
;
577 #else /* CONFIG_SUNRPC_BACKCHANNEL */
579 xs_read_stream_call(struct sock_xprt
*transport
, struct msghdr
*msg
, int flags
)
583 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
586 xs_read_stream_reply(struct sock_xprt
*transport
, struct msghdr
*msg
, int flags
)
588 struct rpc_xprt
*xprt
= &transport
->xprt
;
589 struct rpc_rqst
*req
;
592 /* Look up and lock the request corresponding to the given XID */
593 spin_lock(&xprt
->queue_lock
);
594 req
= xprt_lookup_rqst(xprt
, transport
->recv
.xid
);
595 if (!req
|| (transport
->recv
.copied
&& !req
->rq_private_buf
.len
)) {
596 msg
->msg_flags
|= MSG_TRUNC
;
600 spin_unlock(&xprt
->queue_lock
);
602 ret
= xs_read_stream_request(transport
, msg
, flags
, req
);
604 spin_lock(&xprt
->queue_lock
);
605 if (msg
->msg_flags
& (MSG_EOR
|MSG_TRUNC
))
606 xprt_complete_rqst(req
->rq_task
, transport
->recv
.copied
);
608 req
->rq_private_buf
.len
= transport
->recv
.copied
;
609 xprt_unpin_rqst(req
);
611 spin_unlock(&xprt
->queue_lock
);
616 xs_read_stream(struct sock_xprt
*transport
, int flags
)
618 struct msghdr msg
= { 0 };
619 size_t want
, read
= 0;
622 if (transport
->recv
.len
== 0) {
623 want
= xs_read_stream_headersize(transport
->recv
.copied
!= 0);
624 ret
= xs_read_stream_header(transport
, &msg
, flags
, want
,
625 transport
->recv
.offset
);
628 transport
->recv
.offset
= ret
;
629 if (transport
->recv
.offset
!= want
)
630 return transport
->recv
.offset
;
631 transport
->recv
.len
= be32_to_cpu(transport
->recv
.fraghdr
) &
632 RPC_FRAGMENT_SIZE_MASK
;
633 transport
->recv
.offset
-= sizeof(transport
->recv
.fraghdr
);
637 switch (be32_to_cpu(transport
->recv
.calldir
)) {
639 msg
.msg_flags
|= MSG_TRUNC
;
642 ret
= xs_read_stream_call(transport
, &msg
, flags
);
645 ret
= xs_read_stream_reply(transport
, &msg
, flags
);
647 if (msg
.msg_flags
& MSG_TRUNC
) {
648 transport
->recv
.calldir
= cpu_to_be32(-1);
649 transport
->recv
.copied
= -1;
654 if (transport
->recv
.offset
< transport
->recv
.len
) {
655 if (!(msg
.msg_flags
& MSG_TRUNC
))
658 ret
= xs_read_discard(transport
->sock
, &msg
, flags
,
659 transport
->recv
.len
- transport
->recv
.offset
);
662 transport
->recv
.offset
+= ret
;
664 if (transport
->recv
.offset
!= transport
->recv
.len
)
667 if (xs_read_stream_request_done(transport
)) {
668 trace_xs_stream_read_request(transport
);
669 transport
->recv
.copied
= 0;
671 transport
->recv
.offset
= 0;
672 transport
->recv
.len
= 0;
675 return ret
!= 0 ? ret
: -ESHUTDOWN
;
678 static __poll_t
xs_poll_socket(struct sock_xprt
*transport
)
680 return transport
->sock
->ops
->poll(transport
->file
, transport
->sock
,
684 static bool xs_poll_socket_readable(struct sock_xprt
*transport
)
686 __poll_t events
= xs_poll_socket(transport
);
688 return (events
& (EPOLLIN
| EPOLLRDNORM
)) && !(events
& EPOLLRDHUP
);
691 static void xs_poll_check_readable(struct sock_xprt
*transport
)
694 clear_bit(XPRT_SOCK_DATA_READY
, &transport
->sock_state
);
695 if (!xs_poll_socket_readable(transport
))
697 if (!test_and_set_bit(XPRT_SOCK_DATA_READY
, &transport
->sock_state
))
698 queue_work(xprtiod_workqueue
, &transport
->recv_worker
);
701 static void xs_stream_data_receive(struct sock_xprt
*transport
)
706 mutex_lock(&transport
->recv_mutex
);
707 if (transport
->sock
== NULL
)
710 ret
= xs_read_stream(transport
, MSG_DONTWAIT
);
716 if (ret
== -ESHUTDOWN
)
717 kernel_sock_shutdown(transport
->sock
, SHUT_RDWR
);
719 xs_poll_check_readable(transport
);
721 mutex_unlock(&transport
->recv_mutex
);
722 trace_xs_stream_read_data(&transport
->xprt
, ret
, read
);
725 static void xs_stream_data_receive_workfn(struct work_struct
*work
)
727 struct sock_xprt
*transport
=
728 container_of(work
, struct sock_xprt
, recv_worker
);
729 unsigned int pflags
= memalloc_nofs_save();
731 xs_stream_data_receive(transport
);
732 memalloc_nofs_restore(pflags
);
736 xs_stream_reset_connect(struct sock_xprt
*transport
)
738 transport
->recv
.offset
= 0;
739 transport
->recv
.len
= 0;
740 transport
->recv
.copied
= 0;
741 transport
->xmit
.offset
= 0;
745 xs_stream_start_connect(struct sock_xprt
*transport
)
747 transport
->xprt
.stat
.connect_count
++;
748 transport
->xprt
.stat
.connect_start
= jiffies
;
751 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
754 * xs_nospace - handle transmit was incomplete
755 * @req: pointer to RPC request
758 static int xs_nospace(struct rpc_rqst
*req
)
760 struct rpc_xprt
*xprt
= req
->rq_xprt
;
761 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
762 struct sock
*sk
= transport
->inet
;
765 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
766 req
->rq_task
->tk_pid
,
767 req
->rq_slen
- transport
->xmit
.offset
,
770 /* Protect against races with write_space */
771 spin_lock(&xprt
->transport_lock
);
773 /* Don't race with disconnect */
774 if (xprt_connected(xprt
)) {
775 /* wait for more buffer space */
776 sk
->sk_write_pending
++;
777 xprt_wait_for_buffer_space(xprt
);
781 spin_unlock(&xprt
->transport_lock
);
783 /* Race breaker in case memory is freed before above code is called */
784 if (ret
== -EAGAIN
) {
785 struct socket_wq
*wq
;
788 wq
= rcu_dereference(sk
->sk_wq
);
789 set_bit(SOCKWQ_ASYNC_NOSPACE
, &wq
->flags
);
792 sk
->sk_write_space(sk
);
798 xs_stream_prepare_request(struct rpc_rqst
*req
)
800 xdr_free_bvec(&req
->rq_rcv_buf
);
801 req
->rq_task
->tk_status
= xdr_alloc_bvec(&req
->rq_rcv_buf
, GFP_KERNEL
);
805 * Determine if the previous message in the stream was aborted before it
806 * could complete transmission.
809 xs_send_request_was_aborted(struct sock_xprt
*transport
, struct rpc_rqst
*req
)
811 return transport
->xmit
.offset
!= 0 && req
->rq_bytes_sent
== 0;
815 * Return the stream record marker field for a record of length < 2^31-1
818 xs_stream_record_marker(struct xdr_buf
*xdr
)
822 return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT
| (u32
)xdr
->len
);
826 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
827 * @req: pointer to RPC request
830 * 0: The request has been sent
831 * EAGAIN: The socket was blocked, please call again later to
832 * complete the request
833 * ENOTCONN: Caller needs to invoke connect logic then call again
834 * other: Some other error occured, the request was not sent
836 static int xs_local_send_request(struct rpc_rqst
*req
)
838 struct rpc_xprt
*xprt
= req
->rq_xprt
;
839 struct sock_xprt
*transport
=
840 container_of(xprt
, struct sock_xprt
, xprt
);
841 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
842 rpc_fraghdr rm
= xs_stream_record_marker(xdr
);
843 unsigned int msglen
= rm
? req
->rq_slen
+ sizeof(rm
) : req
->rq_slen
;
844 struct msghdr msg
= {
845 .msg_flags
= XS_SENDMSG_FLAGS
,
847 unsigned int uninitialized_var(sent
);
850 /* Close the stream if the previous transmission was incomplete */
851 if (xs_send_request_was_aborted(transport
, req
)) {
856 xs_pktdump("packet data:",
857 req
->rq_svec
->iov_base
, req
->rq_svec
->iov_len
);
859 req
->rq_xtime
= ktime_get();
860 status
= xprt_sock_sendmsg(transport
->sock
, &msg
, xdr
,
861 transport
->xmit
.offset
, rm
, &sent
);
862 dprintk("RPC: %s(%u) = %d\n",
863 __func__
, xdr
->len
- transport
->xmit
.offset
, status
);
865 if (status
== -EAGAIN
&& sock_writeable(transport
->inet
))
868 if (likely(sent
> 0) || status
== 0) {
869 transport
->xmit
.offset
+= sent
;
870 req
->rq_bytes_sent
= transport
->xmit
.offset
;
871 if (likely(req
->rq_bytes_sent
>= msglen
)) {
872 req
->rq_xmit_bytes_sent
+= transport
->xmit
.offset
;
873 transport
->xmit
.offset
= 0;
883 status
= xs_nospace(req
);
886 dprintk("RPC: sendmsg returned unrecognized error %d\n",
898 * xs_udp_send_request - write an RPC request to a UDP socket
899 * @req: pointer to RPC request
902 * 0: The request has been sent
903 * EAGAIN: The socket was blocked, please call again later to
904 * complete the request
905 * ENOTCONN: Caller needs to invoke connect logic then call again
906 * other: Some other error occurred, the request was not sent
908 static int xs_udp_send_request(struct rpc_rqst
*req
)
910 struct rpc_xprt
*xprt
= req
->rq_xprt
;
911 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
912 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
913 struct msghdr msg
= {
914 .msg_name
= xs_addr(xprt
),
915 .msg_namelen
= xprt
->addrlen
,
916 .msg_flags
= XS_SENDMSG_FLAGS
,
918 unsigned int uninitialized_var(sent
);
921 xs_pktdump("packet data:",
922 req
->rq_svec
->iov_base
,
923 req
->rq_svec
->iov_len
);
925 if (!xprt_bound(xprt
))
928 if (!xprt_request_get_cong(xprt
, req
))
931 req
->rq_xtime
= ktime_get();
932 status
= xprt_sock_sendmsg(transport
->sock
, &msg
, xdr
, 0, 0, &sent
);
934 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
937 /* firewall is blocking us, don't return -EAGAIN or we end up looping */
938 if (status
== -EPERM
)
941 if (status
== -EAGAIN
&& sock_writeable(transport
->inet
))
944 if (sent
> 0 || status
== 0) {
945 req
->rq_xmit_bytes_sent
+= sent
;
946 if (sent
>= req
->rq_slen
)
948 /* Still some bytes left; set up for a retry later. */
956 /* Should we call xs_close() here? */
959 status
= xs_nospace(req
);
966 /* When the server has died, an ICMP port unreachable message
967 * prompts ECONNREFUSED. */
970 dprintk("RPC: sendmsg returned unrecognized error %d\n",
978 * xs_tcp_send_request - write an RPC request to a TCP socket
979 * @req: pointer to RPC request
982 * 0: The request has been sent
983 * EAGAIN: The socket was blocked, please call again later to
984 * complete the request
985 * ENOTCONN: Caller needs to invoke connect logic then call again
986 * other: Some other error occurred, the request was not sent
988 * XXX: In the case of soft timeouts, should we eventually give up
989 * if sendmsg is not able to make progress?
991 static int xs_tcp_send_request(struct rpc_rqst
*req
)
993 struct rpc_xprt
*xprt
= req
->rq_xprt
;
994 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
995 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
996 rpc_fraghdr rm
= xs_stream_record_marker(xdr
);
997 unsigned int msglen
= rm
? req
->rq_slen
+ sizeof(rm
) : req
->rq_slen
;
998 struct msghdr msg
= {
999 .msg_flags
= XS_SENDMSG_FLAGS
,
1001 bool vm_wait
= false;
1002 unsigned int uninitialized_var(sent
);
1005 /* Close the stream if the previous transmission was incomplete */
1006 if (xs_send_request_was_aborted(transport
, req
)) {
1007 if (transport
->sock
!= NULL
)
1008 kernel_sock_shutdown(transport
->sock
, SHUT_RDWR
);
1012 xs_pktdump("packet data:",
1013 req
->rq_svec
->iov_base
,
1014 req
->rq_svec
->iov_len
);
1016 if (test_bit(XPRT_SOCK_UPD_TIMEOUT
, &transport
->sock_state
))
1017 xs_tcp_set_socket_timeouts(xprt
, transport
->sock
);
1019 /* Continue transmitting the packet/record. We must be careful
1020 * to cope with writespace callbacks arriving _after_ we have
1021 * called sendmsg(). */
1022 req
->rq_xtime
= ktime_get();
1024 status
= xprt_sock_sendmsg(transport
->sock
, &msg
, xdr
,
1025 transport
->xmit
.offset
, rm
, &sent
);
1027 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
1028 xdr
->len
- transport
->xmit
.offset
, status
);
1030 /* If we've sent the entire packet, immediately
1031 * reset the count of bytes sent. */
1032 transport
->xmit
.offset
+= sent
;
1033 req
->rq_bytes_sent
= transport
->xmit
.offset
;
1034 if (likely(req
->rq_bytes_sent
>= msglen
)) {
1035 req
->rq_xmit_bytes_sent
+= transport
->xmit
.offset
;
1036 transport
->xmit
.offset
= 0;
1040 WARN_ON_ONCE(sent
== 0 && status
== 0);
1042 if (status
== -EAGAIN
) {
1044 * Return EAGAIN if we're sure we're hitting the
1045 * socket send buffer limits.
1047 if (test_bit(SOCK_NOSPACE
, &transport
->sock
->flags
))
1050 * Did we hit a memory allocation failure?
1056 /* Retry, knowing now that we're below the
1057 * socket send buffer limit
1071 /* Should we call xs_close() here? */
1074 status
= xs_nospace(req
);
1084 dprintk("RPC: sendmsg returned unrecognized error %d\n",
1091 static void xs_save_old_callbacks(struct sock_xprt
*transport
, struct sock
*sk
)
1093 transport
->old_data_ready
= sk
->sk_data_ready
;
1094 transport
->old_state_change
= sk
->sk_state_change
;
1095 transport
->old_write_space
= sk
->sk_write_space
;
1096 transport
->old_error_report
= sk
->sk_error_report
;
1099 static void xs_restore_old_callbacks(struct sock_xprt
*transport
, struct sock
*sk
)
1101 sk
->sk_data_ready
= transport
->old_data_ready
;
1102 sk
->sk_state_change
= transport
->old_state_change
;
1103 sk
->sk_write_space
= transport
->old_write_space
;
1104 sk
->sk_error_report
= transport
->old_error_report
;
1107 static void xs_sock_reset_state_flags(struct rpc_xprt
*xprt
)
1109 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1111 clear_bit(XPRT_SOCK_DATA_READY
, &transport
->sock_state
);
1112 clear_bit(XPRT_SOCK_WAKE_ERROR
, &transport
->sock_state
);
1113 clear_bit(XPRT_SOCK_WAKE_WRITE
, &transport
->sock_state
);
1114 clear_bit(XPRT_SOCK_WAKE_DISCONNECT
, &transport
->sock_state
);
1117 static void xs_run_error_worker(struct sock_xprt
*transport
, unsigned int nr
)
1119 set_bit(nr
, &transport
->sock_state
);
1120 queue_work(xprtiod_workqueue
, &transport
->error_worker
);
1123 static void xs_sock_reset_connection_flags(struct rpc_xprt
*xprt
)
1125 smp_mb__before_atomic();
1126 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
1127 clear_bit(XPRT_CLOSING
, &xprt
->state
);
1128 xs_sock_reset_state_flags(xprt
);
1129 smp_mb__after_atomic();
1133 * xs_error_report - callback to handle TCP socket state errors
1136 * Note: we don't call sock_error() since there may be a rpc_task
1137 * using the socket, and so we don't want to clear sk->sk_err.
1139 static void xs_error_report(struct sock
*sk
)
1141 struct sock_xprt
*transport
;
1142 struct rpc_xprt
*xprt
;
1144 read_lock_bh(&sk
->sk_callback_lock
);
1145 if (!(xprt
= xprt_from_sock(sk
)))
1148 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1149 transport
->xprt_err
= -sk
->sk_err
;
1150 if (transport
->xprt_err
== 0)
1152 dprintk("RPC: xs_error_report client %p, error=%d...\n",
1153 xprt
, -transport
->xprt_err
);
1154 trace_rpc_socket_error(xprt
, sk
->sk_socket
, transport
->xprt_err
);
1156 /* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */
1157 smp_mb__before_atomic();
1158 xs_run_error_worker(transport
, XPRT_SOCK_WAKE_ERROR
);
1160 read_unlock_bh(&sk
->sk_callback_lock
);
1163 static void xs_reset_transport(struct sock_xprt
*transport
)
1165 struct socket
*sock
= transport
->sock
;
1166 struct sock
*sk
= transport
->inet
;
1167 struct rpc_xprt
*xprt
= &transport
->xprt
;
1168 struct file
*filp
= transport
->file
;
1173 if (atomic_read(&transport
->xprt
.swapper
))
1174 sk_clear_memalloc(sk
);
1176 kernel_sock_shutdown(sock
, SHUT_RDWR
);
1178 mutex_lock(&transport
->recv_mutex
);
1179 write_lock_bh(&sk
->sk_callback_lock
);
1180 transport
->inet
= NULL
;
1181 transport
->sock
= NULL
;
1182 transport
->file
= NULL
;
1184 sk
->sk_user_data
= NULL
;
1186 xs_restore_old_callbacks(transport
, sk
);
1187 xprt_clear_connected(xprt
);
1188 write_unlock_bh(&sk
->sk_callback_lock
);
1189 xs_sock_reset_connection_flags(xprt
);
1190 /* Reset stream record info */
1191 xs_stream_reset_connect(transport
);
1192 mutex_unlock(&transport
->recv_mutex
);
1194 trace_rpc_socket_close(xprt
, sock
);
1197 xprt_disconnect_done(xprt
);
1201 * xs_close - close a socket
1204 * This is used when all requests are complete; ie, no DRC state remains
1205 * on the server we want to save.
1207 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1208 * xs_reset_transport() zeroing the socket from underneath a writer.
1210 static void xs_close(struct rpc_xprt
*xprt
)
1212 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1214 dprintk("RPC: xs_close xprt %p\n", xprt
);
1216 xs_reset_transport(transport
);
1217 xprt
->reestablish_timeout
= 0;
1220 static void xs_inject_disconnect(struct rpc_xprt
*xprt
)
1222 dprintk("RPC: injecting transport disconnect on xprt=%p\n",
1224 xprt_disconnect_done(xprt
);
1227 static void xs_xprt_free(struct rpc_xprt
*xprt
)
1229 xs_free_peer_addresses(xprt
);
1234 * xs_destroy - prepare to shutdown a transport
1235 * @xprt: doomed transport
1238 static void xs_destroy(struct rpc_xprt
*xprt
)
1240 struct sock_xprt
*transport
= container_of(xprt
,
1241 struct sock_xprt
, xprt
);
1242 dprintk("RPC: xs_destroy xprt %p\n", xprt
);
1244 cancel_delayed_work_sync(&transport
->connect_worker
);
1246 cancel_work_sync(&transport
->recv_worker
);
1247 cancel_work_sync(&transport
->error_worker
);
1249 module_put(THIS_MODULE
);
1253 * xs_udp_data_read_skb - receive callback for UDP sockets
1259 static void xs_udp_data_read_skb(struct rpc_xprt
*xprt
,
1261 struct sk_buff
*skb
)
1263 struct rpc_task
*task
;
1264 struct rpc_rqst
*rovr
;
1265 int repsize
, copied
;
1271 dprintk("RPC: impossible RPC reply size %d!\n", repsize
);
1275 /* Copy the XID from the skb... */
1276 xp
= skb_header_pointer(skb
, 0, sizeof(_xid
), &_xid
);
1280 /* Look up and lock the request corresponding to the given XID */
1281 spin_lock(&xprt
->queue_lock
);
1282 rovr
= xprt_lookup_rqst(xprt
, *xp
);
1285 xprt_pin_rqst(rovr
);
1286 xprt_update_rtt(rovr
->rq_task
);
1287 spin_unlock(&xprt
->queue_lock
);
1288 task
= rovr
->rq_task
;
1290 if ((copied
= rovr
->rq_private_buf
.buflen
) > repsize
)
1293 /* Suck it into the iovec, verify checksum if not done by hw. */
1294 if (csum_partial_copy_to_xdr(&rovr
->rq_private_buf
, skb
)) {
1295 spin_lock(&xprt
->queue_lock
);
1296 __UDPX_INC_STATS(sk
, UDP_MIB_INERRORS
);
1301 spin_lock(&xprt
->transport_lock
);
1302 xprt_adjust_cwnd(xprt
, task
, copied
);
1303 spin_unlock(&xprt
->transport_lock
);
1304 spin_lock(&xprt
->queue_lock
);
1305 xprt_complete_rqst(task
, copied
);
1306 __UDPX_INC_STATS(sk
, UDP_MIB_INDATAGRAMS
);
1308 xprt_unpin_rqst(rovr
);
1310 spin_unlock(&xprt
->queue_lock
);
1313 static void xs_udp_data_receive(struct sock_xprt
*transport
)
1315 struct sk_buff
*skb
;
1319 mutex_lock(&transport
->recv_mutex
);
1320 sk
= transport
->inet
;
1324 skb
= skb_recv_udp(sk
, 0, 1, &err
);
1327 xs_udp_data_read_skb(&transport
->xprt
, sk
, skb
);
1331 xs_poll_check_readable(transport
);
1333 mutex_unlock(&transport
->recv_mutex
);
1336 static void xs_udp_data_receive_workfn(struct work_struct
*work
)
1338 struct sock_xprt
*transport
=
1339 container_of(work
, struct sock_xprt
, recv_worker
);
1340 unsigned int pflags
= memalloc_nofs_save();
1342 xs_udp_data_receive(transport
);
1343 memalloc_nofs_restore(pflags
);
1347 * xs_data_ready - "data ready" callback for UDP sockets
1348 * @sk: socket with data to read
1351 static void xs_data_ready(struct sock
*sk
)
1353 struct rpc_xprt
*xprt
;
1355 read_lock_bh(&sk
->sk_callback_lock
);
1356 dprintk("RPC: xs_data_ready...\n");
1357 xprt
= xprt_from_sock(sk
);
1359 struct sock_xprt
*transport
= container_of(xprt
,
1360 struct sock_xprt
, xprt
);
1361 transport
->old_data_ready(sk
);
1362 /* Any data means we had a useful conversation, so
1363 * then we don't need to delay the next reconnect
1365 if (xprt
->reestablish_timeout
)
1366 xprt
->reestablish_timeout
= 0;
1367 if (!test_and_set_bit(XPRT_SOCK_DATA_READY
, &transport
->sock_state
))
1368 queue_work(xprtiod_workqueue
, &transport
->recv_worker
);
1370 read_unlock_bh(&sk
->sk_callback_lock
);
1374 * Helper function to force a TCP close if the server is sending
1375 * junk and/or it has put us in CLOSE_WAIT
1377 static void xs_tcp_force_close(struct rpc_xprt
*xprt
)
1379 xprt_force_disconnect(xprt
);
1382 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1383 static size_t xs_tcp_bc_maxpayload(struct rpc_xprt
*xprt
)
1387 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1390 * xs_tcp_state_change - callback to handle TCP socket state changes
1391 * @sk: socket whose state has changed
1394 static void xs_tcp_state_change(struct sock
*sk
)
1396 struct rpc_xprt
*xprt
;
1397 struct sock_xprt
*transport
;
1399 read_lock_bh(&sk
->sk_callback_lock
);
1400 if (!(xprt
= xprt_from_sock(sk
)))
1402 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt
);
1403 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1404 sk
->sk_state
, xprt_connected(xprt
),
1405 sock_flag(sk
, SOCK_DEAD
),
1406 sock_flag(sk
, SOCK_ZAPPED
),
1409 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1410 trace_rpc_socket_state_change(xprt
, sk
->sk_socket
);
1411 switch (sk
->sk_state
) {
1412 case TCP_ESTABLISHED
:
1413 if (!xprt_test_and_set_connected(xprt
)) {
1414 xprt
->connect_cookie
++;
1415 clear_bit(XPRT_SOCK_CONNECTING
, &transport
->sock_state
);
1416 xprt_clear_connecting(xprt
);
1418 xprt
->stat
.connect_count
++;
1419 xprt
->stat
.connect_time
+= (long)jiffies
-
1420 xprt
->stat
.connect_start
;
1421 xs_run_error_worker(transport
, XPRT_SOCK_WAKE_PENDING
);
1425 /* The client initiated a shutdown of the socket */
1426 xprt
->connect_cookie
++;
1427 xprt
->reestablish_timeout
= 0;
1428 set_bit(XPRT_CLOSING
, &xprt
->state
);
1429 smp_mb__before_atomic();
1430 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1431 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
1432 smp_mb__after_atomic();
1434 case TCP_CLOSE_WAIT
:
1435 /* The server initiated a shutdown of the socket */
1436 xprt
->connect_cookie
++;
1437 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1438 xs_run_error_worker(transport
, XPRT_SOCK_WAKE_DISCONNECT
);
1442 * If the server closed down the connection, make sure that
1443 * we back off before reconnecting
1445 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
1446 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
1449 set_bit(XPRT_CLOSING
, &xprt
->state
);
1450 smp_mb__before_atomic();
1451 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1452 smp_mb__after_atomic();
1455 if (test_and_clear_bit(XPRT_SOCK_CONNECTING
,
1456 &transport
->sock_state
))
1457 xprt_clear_connecting(xprt
);
1458 clear_bit(XPRT_CLOSING
, &xprt
->state
);
1459 /* Trigger the socket release */
1460 xs_run_error_worker(transport
, XPRT_SOCK_WAKE_DISCONNECT
);
1463 read_unlock_bh(&sk
->sk_callback_lock
);
1466 static void xs_write_space(struct sock
*sk
)
1468 struct socket_wq
*wq
;
1469 struct sock_xprt
*transport
;
1470 struct rpc_xprt
*xprt
;
1474 clear_bit(SOCK_NOSPACE
, &sk
->sk_socket
->flags
);
1476 if (unlikely(!(xprt
= xprt_from_sock(sk
))))
1478 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1480 wq
= rcu_dereference(sk
->sk_wq
);
1481 if (!wq
|| test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE
, &wq
->flags
) == 0)
1484 xs_run_error_worker(transport
, XPRT_SOCK_WAKE_WRITE
);
1485 sk
->sk_write_pending
--;
1491 * xs_udp_write_space - callback invoked when socket buffer space
1493 * @sk: socket whose state has changed
1495 * Called when more output buffer space is available for this socket.
1496 * We try not to wake our writers until they can make "significant"
1497 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1498 * with a bunch of small requests.
1500 static void xs_udp_write_space(struct sock
*sk
)
1502 read_lock_bh(&sk
->sk_callback_lock
);
1504 /* from net/core/sock.c:sock_def_write_space */
1505 if (sock_writeable(sk
))
1508 read_unlock_bh(&sk
->sk_callback_lock
);
1512 * xs_tcp_write_space - callback invoked when socket buffer space
1514 * @sk: socket whose state has changed
1516 * Called when more output buffer space is available for this socket.
1517 * We try not to wake our writers until they can make "significant"
1518 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1519 * with a bunch of small requests.
1521 static void xs_tcp_write_space(struct sock
*sk
)
1523 read_lock_bh(&sk
->sk_callback_lock
);
1525 /* from net/core/stream.c:sk_stream_write_space */
1526 if (sk_stream_is_writeable(sk
))
1529 read_unlock_bh(&sk
->sk_callback_lock
);
1532 static void xs_udp_do_set_buffer_size(struct rpc_xprt
*xprt
)
1534 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1535 struct sock
*sk
= transport
->inet
;
1537 if (transport
->rcvsize
) {
1538 sk
->sk_userlocks
|= SOCK_RCVBUF_LOCK
;
1539 sk
->sk_rcvbuf
= transport
->rcvsize
* xprt
->max_reqs
* 2;
1541 if (transport
->sndsize
) {
1542 sk
->sk_userlocks
|= SOCK_SNDBUF_LOCK
;
1543 sk
->sk_sndbuf
= transport
->sndsize
* xprt
->max_reqs
* 2;
1544 sk
->sk_write_space(sk
);
1549 * xs_udp_set_buffer_size - set send and receive limits
1550 * @xprt: generic transport
1551 * @sndsize: requested size of send buffer, in bytes
1552 * @rcvsize: requested size of receive buffer, in bytes
1554 * Set socket send and receive buffer size limits.
1556 static void xs_udp_set_buffer_size(struct rpc_xprt
*xprt
, size_t sndsize
, size_t rcvsize
)
1558 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1560 transport
->sndsize
= 0;
1562 transport
->sndsize
= sndsize
+ 1024;
1563 transport
->rcvsize
= 0;
1565 transport
->rcvsize
= rcvsize
+ 1024;
1567 xs_udp_do_set_buffer_size(xprt
);
1571 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1572 * @xprt: controlling transport
1573 * @task: task that timed out
1575 * Adjust the congestion window after a retransmit timeout has occurred.
1577 static void xs_udp_timer(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
1579 spin_lock(&xprt
->transport_lock
);
1580 xprt_adjust_cwnd(xprt
, task
, -ETIMEDOUT
);
1581 spin_unlock(&xprt
->transport_lock
);
1584 static int xs_get_random_port(void)
1586 unsigned short min
= xprt_min_resvport
, max
= xprt_max_resvport
;
1587 unsigned short range
;
1588 unsigned short rand
;
1592 range
= max
- min
+ 1;
1593 rand
= (unsigned short) prandom_u32() % range
;
1598 * xs_set_reuseaddr_port - set the socket's port and address reuse options
1601 * Note that this function has to be called on all sockets that share the
1602 * same port, and it must be called before binding.
1604 static void xs_sock_set_reuseport(struct socket
*sock
)
1608 kernel_setsockopt(sock
, SOL_SOCKET
, SO_REUSEPORT
,
1609 (char *)&opt
, sizeof(opt
));
1612 static unsigned short xs_sock_getport(struct socket
*sock
)
1614 struct sockaddr_storage buf
;
1615 unsigned short port
= 0;
1617 if (kernel_getsockname(sock
, (struct sockaddr
*)&buf
) < 0)
1619 switch (buf
.ss_family
) {
1621 port
= ntohs(((struct sockaddr_in6
*)&buf
)->sin6_port
);
1624 port
= ntohs(((struct sockaddr_in
*)&buf
)->sin_port
);
1631 * xs_set_port - reset the port number in the remote endpoint address
1632 * @xprt: generic transport
1633 * @port: new port number
1636 static void xs_set_port(struct rpc_xprt
*xprt
, unsigned short port
)
1638 dprintk("RPC: setting port for xprt %p to %u\n", xprt
, port
);
1640 rpc_set_port(xs_addr(xprt
), port
);
1641 xs_update_peer_port(xprt
);
1644 static void xs_set_srcport(struct sock_xprt
*transport
, struct socket
*sock
)
1646 if (transport
->srcport
== 0 && transport
->xprt
.reuseport
)
1647 transport
->srcport
= xs_sock_getport(sock
);
1650 static int xs_get_srcport(struct sock_xprt
*transport
)
1652 int port
= transport
->srcport
;
1654 if (port
== 0 && transport
->xprt
.resvport
)
1655 port
= xs_get_random_port();
1659 static unsigned short xs_next_srcport(struct sock_xprt
*transport
, unsigned short port
)
1661 if (transport
->srcport
!= 0)
1662 transport
->srcport
= 0;
1663 if (!transport
->xprt
.resvport
)
1665 if (port
<= xprt_min_resvport
|| port
> xprt_max_resvport
)
1666 return xprt_max_resvport
;
1669 static int xs_bind(struct sock_xprt
*transport
, struct socket
*sock
)
1671 struct sockaddr_storage myaddr
;
1673 int port
= xs_get_srcport(transport
);
1674 unsigned short last
;
1677 * If we are asking for any ephemeral port (i.e. port == 0 &&
1678 * transport->xprt.resvport == 0), don't bind. Let the local
1679 * port selection happen implicitly when the socket is used
1680 * (for example at connect time).
1682 * This ensures that we can continue to establish TCP
1683 * connections even when all local ephemeral ports are already
1684 * a part of some TCP connection. This makes no difference
1685 * for UDP sockets, but also doens't harm them.
1687 * If we're asking for any reserved port (i.e. port == 0 &&
1688 * transport->xprt.resvport == 1) xs_get_srcport above will
1689 * ensure that port is non-zero and we will bind as needed.
1694 memcpy(&myaddr
, &transport
->srcaddr
, transport
->xprt
.addrlen
);
1696 rpc_set_port((struct sockaddr
*)&myaddr
, port
);
1697 err
= kernel_bind(sock
, (struct sockaddr
*)&myaddr
,
1698 transport
->xprt
.addrlen
);
1700 transport
->srcport
= port
;
1704 port
= xs_next_srcport(transport
, port
);
1707 } while (err
== -EADDRINUSE
&& nloop
!= 2);
1709 if (myaddr
.ss_family
== AF_INET
)
1710 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__
,
1711 &((struct sockaddr_in
*)&myaddr
)->sin_addr
,
1712 port
, err
? "failed" : "ok", err
);
1714 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__
,
1715 &((struct sockaddr_in6
*)&myaddr
)->sin6_addr
,
1716 port
, err
? "failed" : "ok", err
);
1721 * We don't support autobind on AF_LOCAL sockets
1723 static void xs_local_rpcbind(struct rpc_task
*task
)
1725 xprt_set_bound(task
->tk_xprt
);
1728 static void xs_local_set_port(struct rpc_xprt
*xprt
, unsigned short port
)
1732 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1733 static struct lock_class_key xs_key
[2];
1734 static struct lock_class_key xs_slock_key
[2];
1736 static inline void xs_reclassify_socketu(struct socket
*sock
)
1738 struct sock
*sk
= sock
->sk
;
1740 sock_lock_init_class_and_name(sk
, "slock-AF_LOCAL-RPC",
1741 &xs_slock_key
[1], "sk_lock-AF_LOCAL-RPC", &xs_key
[1]);
1744 static inline void xs_reclassify_socket4(struct socket
*sock
)
1746 struct sock
*sk
= sock
->sk
;
1748 sock_lock_init_class_and_name(sk
, "slock-AF_INET-RPC",
1749 &xs_slock_key
[0], "sk_lock-AF_INET-RPC", &xs_key
[0]);
1752 static inline void xs_reclassify_socket6(struct socket
*sock
)
1754 struct sock
*sk
= sock
->sk
;
1756 sock_lock_init_class_and_name(sk
, "slock-AF_INET6-RPC",
1757 &xs_slock_key
[1], "sk_lock-AF_INET6-RPC", &xs_key
[1]);
1760 static inline void xs_reclassify_socket(int family
, struct socket
*sock
)
1762 if (WARN_ON_ONCE(!sock_allow_reclassification(sock
->sk
)))
1767 xs_reclassify_socketu(sock
);
1770 xs_reclassify_socket4(sock
);
1773 xs_reclassify_socket6(sock
);
1778 static inline void xs_reclassify_socket(int family
, struct socket
*sock
)
1783 static void xs_dummy_setup_socket(struct work_struct
*work
)
1787 static struct socket
*xs_create_sock(struct rpc_xprt
*xprt
,
1788 struct sock_xprt
*transport
, int family
, int type
,
1789 int protocol
, bool reuseport
)
1792 struct socket
*sock
;
1795 err
= __sock_create(xprt
->xprt_net
, family
, type
, protocol
, &sock
, 1);
1797 dprintk("RPC: can't create %d transport socket (%d).\n",
1801 xs_reclassify_socket(family
, sock
);
1804 xs_sock_set_reuseport(sock
);
1806 err
= xs_bind(transport
, sock
);
1812 filp
= sock_alloc_file(sock
, O_NONBLOCK
, NULL
);
1814 return ERR_CAST(filp
);
1815 transport
->file
= filp
;
1819 return ERR_PTR(err
);
1822 static int xs_local_finish_connecting(struct rpc_xprt
*xprt
,
1823 struct socket
*sock
)
1825 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
,
1828 if (!transport
->inet
) {
1829 struct sock
*sk
= sock
->sk
;
1831 write_lock_bh(&sk
->sk_callback_lock
);
1833 xs_save_old_callbacks(transport
, sk
);
1835 sk
->sk_user_data
= xprt
;
1836 sk
->sk_data_ready
= xs_data_ready
;
1837 sk
->sk_write_space
= xs_udp_write_space
;
1838 sock_set_flag(sk
, SOCK_FASYNC
);
1839 sk
->sk_error_report
= xs_error_report
;
1841 xprt_clear_connected(xprt
);
1843 /* Reset to new socket */
1844 transport
->sock
= sock
;
1845 transport
->inet
= sk
;
1847 write_unlock_bh(&sk
->sk_callback_lock
);
1850 xs_stream_start_connect(transport
);
1852 return kernel_connect(sock
, xs_addr(xprt
), xprt
->addrlen
, 0);
1856 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1857 * @transport: socket transport to connect
1859 static int xs_local_setup_socket(struct sock_xprt
*transport
)
1861 struct rpc_xprt
*xprt
= &transport
->xprt
;
1863 struct socket
*sock
;
1866 status
= __sock_create(xprt
->xprt_net
, AF_LOCAL
,
1867 SOCK_STREAM
, 0, &sock
, 1);
1869 dprintk("RPC: can't create AF_LOCAL "
1870 "transport socket (%d).\n", -status
);
1873 xs_reclassify_socket(AF_LOCAL
, sock
);
1875 filp
= sock_alloc_file(sock
, O_NONBLOCK
, NULL
);
1877 status
= PTR_ERR(filp
);
1880 transport
->file
= filp
;
1882 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1883 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
1885 status
= xs_local_finish_connecting(xprt
, sock
);
1886 trace_rpc_socket_connect(xprt
, sock
, status
);
1889 dprintk("RPC: xprt %p connected to %s\n",
1890 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
1891 xprt
->stat
.connect_count
++;
1892 xprt
->stat
.connect_time
+= (long)jiffies
-
1893 xprt
->stat
.connect_start
;
1894 xprt_set_connected(xprt
);
1898 dprintk("RPC: xprt %p: socket %s does not exist\n",
1899 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
1902 dprintk("RPC: xprt %p: connection refused for %s\n",
1903 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
1906 printk(KERN_ERR
"%s: unhandled error (%d) connecting to %s\n",
1908 xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
1912 xprt_clear_connecting(xprt
);
1913 xprt_wake_pending_tasks(xprt
, status
);
1917 static void xs_local_connect(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
1919 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1922 if (RPC_IS_ASYNC(task
)) {
1924 * We want the AF_LOCAL connect to be resolved in the
1925 * filesystem namespace of the process making the rpc
1926 * call. Thus we connect synchronously.
1928 * If we want to support asynchronous AF_LOCAL calls,
1929 * we'll need to figure out how to pass a namespace to
1932 task
->tk_rpc_status
= -ENOTCONN
;
1933 rpc_exit(task
, -ENOTCONN
);
1936 ret
= xs_local_setup_socket(transport
);
1937 if (ret
&& !RPC_IS_SOFTCONN(task
))
1938 msleep_interruptible(15000);
1941 #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
1943 * Note that this should be called with XPRT_LOCKED held (or when we otherwise
1944 * know that we have exclusive access to the socket), to guard against
1945 * races with xs_reset_transport.
1947 static void xs_set_memalloc(struct rpc_xprt
*xprt
)
1949 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
,
1953 * If there's no sock, then we have nothing to set. The
1954 * reconnecting process will get it for us.
1956 if (!transport
->inet
)
1958 if (atomic_read(&xprt
->swapper
))
1959 sk_set_memalloc(transport
->inet
);
1963 * xs_enable_swap - Tag this transport as being used for swap.
1964 * @xprt: transport to tag
1966 * Take a reference to this transport on behalf of the rpc_clnt, and
1967 * optionally mark it for swapping if it wasn't already.
1970 xs_enable_swap(struct rpc_xprt
*xprt
)
1972 struct sock_xprt
*xs
= container_of(xprt
, struct sock_xprt
, xprt
);
1974 if (atomic_inc_return(&xprt
->swapper
) != 1)
1976 if (wait_on_bit_lock(&xprt
->state
, XPRT_LOCKED
, TASK_KILLABLE
))
1977 return -ERESTARTSYS
;
1979 sk_set_memalloc(xs
->inet
);
1980 xprt_release_xprt(xprt
, NULL
);
1985 * xs_disable_swap - Untag this transport as being used for swap.
1986 * @xprt: transport to tag
1988 * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
1989 * swapper refcount goes to 0, untag the socket as a memalloc socket.
1992 xs_disable_swap(struct rpc_xprt
*xprt
)
1994 struct sock_xprt
*xs
= container_of(xprt
, struct sock_xprt
, xprt
);
1996 if (!atomic_dec_and_test(&xprt
->swapper
))
1998 if (wait_on_bit_lock(&xprt
->state
, XPRT_LOCKED
, TASK_KILLABLE
))
2001 sk_clear_memalloc(xs
->inet
);
2002 xprt_release_xprt(xprt
, NULL
);
2005 static void xs_set_memalloc(struct rpc_xprt
*xprt
)
2010 xs_enable_swap(struct rpc_xprt
*xprt
)
2016 xs_disable_swap(struct rpc_xprt
*xprt
)
2021 static void xs_udp_finish_connecting(struct rpc_xprt
*xprt
, struct socket
*sock
)
2023 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2025 if (!transport
->inet
) {
2026 struct sock
*sk
= sock
->sk
;
2028 write_lock_bh(&sk
->sk_callback_lock
);
2030 xs_save_old_callbacks(transport
, sk
);
2032 sk
->sk_user_data
= xprt
;
2033 sk
->sk_data_ready
= xs_data_ready
;
2034 sk
->sk_write_space
= xs_udp_write_space
;
2035 sock_set_flag(sk
, SOCK_FASYNC
);
2037 xprt_set_connected(xprt
);
2039 /* Reset to new socket */
2040 transport
->sock
= sock
;
2041 transport
->inet
= sk
;
2043 xs_set_memalloc(xprt
);
2045 write_unlock_bh(&sk
->sk_callback_lock
);
2047 xs_udp_do_set_buffer_size(xprt
);
2049 xprt
->stat
.connect_start
= jiffies
;
2052 static void xs_udp_setup_socket(struct work_struct
*work
)
2054 struct sock_xprt
*transport
=
2055 container_of(work
, struct sock_xprt
, connect_worker
.work
);
2056 struct rpc_xprt
*xprt
= &transport
->xprt
;
2057 struct socket
*sock
;
2060 sock
= xs_create_sock(xprt
, transport
,
2061 xs_addr(xprt
)->sa_family
, SOCK_DGRAM
,
2062 IPPROTO_UDP
, false);
2066 dprintk("RPC: worker connecting xprt %p via %s to "
2067 "%s (port %s)\n", xprt
,
2068 xprt
->address_strings
[RPC_DISPLAY_PROTO
],
2069 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2070 xprt
->address_strings
[RPC_DISPLAY_PORT
]);
2072 xs_udp_finish_connecting(xprt
, sock
);
2073 trace_rpc_socket_connect(xprt
, sock
, 0);
2076 xprt_clear_connecting(xprt
);
2077 xprt_unlock_connect(xprt
, transport
);
2078 xprt_wake_pending_tasks(xprt
, status
);
2082 * xs_tcp_shutdown - gracefully shut down a TCP socket
2085 * Initiates a graceful shutdown of the TCP socket by calling the
2086 * equivalent of shutdown(SHUT_RDWR);
2088 static void xs_tcp_shutdown(struct rpc_xprt
*xprt
)
2090 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2091 struct socket
*sock
= transport
->sock
;
2092 int skst
= transport
->inet
? transport
->inet
->sk_state
: TCP_CLOSE
;
2098 kernel_sock_shutdown(sock
, SHUT_RDWR
);
2099 trace_rpc_socket_shutdown(xprt
, sock
);
2103 xs_reset_transport(transport
);
2107 static void xs_tcp_set_socket_timeouts(struct rpc_xprt
*xprt
,
2108 struct socket
*sock
)
2110 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2111 unsigned int keepidle
;
2112 unsigned int keepcnt
;
2113 unsigned int opt_on
= 1;
2116 spin_lock(&xprt
->transport_lock
);
2117 keepidle
= DIV_ROUND_UP(xprt
->timeout
->to_initval
, HZ
);
2118 keepcnt
= xprt
->timeout
->to_retries
+ 1;
2119 timeo
= jiffies_to_msecs(xprt
->timeout
->to_initval
) *
2120 (xprt
->timeout
->to_retries
+ 1);
2121 clear_bit(XPRT_SOCK_UPD_TIMEOUT
, &transport
->sock_state
);
2122 spin_unlock(&xprt
->transport_lock
);
2124 /* TCP Keepalive options */
2125 kernel_setsockopt(sock
, SOL_SOCKET
, SO_KEEPALIVE
,
2126 (char *)&opt_on
, sizeof(opt_on
));
2127 kernel_setsockopt(sock
, SOL_TCP
, TCP_KEEPIDLE
,
2128 (char *)&keepidle
, sizeof(keepidle
));
2129 kernel_setsockopt(sock
, SOL_TCP
, TCP_KEEPINTVL
,
2130 (char *)&keepidle
, sizeof(keepidle
));
2131 kernel_setsockopt(sock
, SOL_TCP
, TCP_KEEPCNT
,
2132 (char *)&keepcnt
, sizeof(keepcnt
));
2134 /* TCP user timeout (see RFC5482) */
2135 kernel_setsockopt(sock
, SOL_TCP
, TCP_USER_TIMEOUT
,
2136 (char *)&timeo
, sizeof(timeo
));
2139 static void xs_tcp_set_connect_timeout(struct rpc_xprt
*xprt
,
2140 unsigned long connect_timeout
,
2141 unsigned long reconnect_timeout
)
2143 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2144 struct rpc_timeout to
;
2145 unsigned long initval
;
2147 spin_lock(&xprt
->transport_lock
);
2148 if (reconnect_timeout
< xprt
->max_reconnect_timeout
)
2149 xprt
->max_reconnect_timeout
= reconnect_timeout
;
2150 if (connect_timeout
< xprt
->connect_timeout
) {
2151 memcpy(&to
, xprt
->timeout
, sizeof(to
));
2152 initval
= DIV_ROUND_UP(connect_timeout
, to
.to_retries
+ 1);
2153 /* Arbitrary lower limit */
2154 if (initval
< XS_TCP_INIT_REEST_TO
<< 1)
2155 initval
= XS_TCP_INIT_REEST_TO
<< 1;
2156 to
.to_initval
= initval
;
2157 to
.to_maxval
= initval
;
2158 memcpy(&transport
->tcp_timeout
, &to
,
2159 sizeof(transport
->tcp_timeout
));
2160 xprt
->timeout
= &transport
->tcp_timeout
;
2161 xprt
->connect_timeout
= connect_timeout
;
2163 set_bit(XPRT_SOCK_UPD_TIMEOUT
, &transport
->sock_state
);
2164 spin_unlock(&xprt
->transport_lock
);
2167 static int xs_tcp_finish_connecting(struct rpc_xprt
*xprt
, struct socket
*sock
)
2169 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2170 int ret
= -ENOTCONN
;
2172 if (!transport
->inet
) {
2173 struct sock
*sk
= sock
->sk
;
2174 unsigned int addr_pref
= IPV6_PREFER_SRC_PUBLIC
;
2176 /* Avoid temporary address, they are bad for long-lived
2177 * connections such as NFS mounts.
2178 * RFC4941, section 3.6 suggests that:
2179 * Individual applications, which have specific
2180 * knowledge about the normal duration of connections,
2181 * MAY override this as appropriate.
2183 kernel_setsockopt(sock
, SOL_IPV6
, IPV6_ADDR_PREFERENCES
,
2184 (char *)&addr_pref
, sizeof(addr_pref
));
2186 xs_tcp_set_socket_timeouts(xprt
, sock
);
2188 write_lock_bh(&sk
->sk_callback_lock
);
2190 xs_save_old_callbacks(transport
, sk
);
2192 sk
->sk_user_data
= xprt
;
2193 sk
->sk_data_ready
= xs_data_ready
;
2194 sk
->sk_state_change
= xs_tcp_state_change
;
2195 sk
->sk_write_space
= xs_tcp_write_space
;
2196 sock_set_flag(sk
, SOCK_FASYNC
);
2197 sk
->sk_error_report
= xs_error_report
;
2199 /* socket options */
2200 sock_reset_flag(sk
, SOCK_LINGER
);
2201 tcp_sk(sk
)->nonagle
|= TCP_NAGLE_OFF
;
2203 xprt_clear_connected(xprt
);
2205 /* Reset to new socket */
2206 transport
->sock
= sock
;
2207 transport
->inet
= sk
;
2209 write_unlock_bh(&sk
->sk_callback_lock
);
2212 if (!xprt_bound(xprt
))
2215 xs_set_memalloc(xprt
);
2217 xs_stream_start_connect(transport
);
2219 /* Tell the socket layer to start connecting... */
2220 set_bit(XPRT_SOCK_CONNECTING
, &transport
->sock_state
);
2221 ret
= kernel_connect(sock
, xs_addr(xprt
), xprt
->addrlen
, O_NONBLOCK
);
2224 xs_set_srcport(transport
, sock
);
2228 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
2229 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2231 case -EADDRNOTAVAIL
:
2232 /* Source port number is unavailable. Try a new one! */
2233 transport
->srcport
= 0;
2240 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2241 * @work: queued work item
2243 * Invoked by a work queue tasklet.
2245 static void xs_tcp_setup_socket(struct work_struct
*work
)
2247 struct sock_xprt
*transport
=
2248 container_of(work
, struct sock_xprt
, connect_worker
.work
);
2249 struct socket
*sock
= transport
->sock
;
2250 struct rpc_xprt
*xprt
= &transport
->xprt
;
2254 sock
= xs_create_sock(xprt
, transport
,
2255 xs_addr(xprt
)->sa_family
, SOCK_STREAM
,
2258 status
= PTR_ERR(sock
);
2263 dprintk("RPC: worker connecting xprt %p via %s to "
2264 "%s (port %s)\n", xprt
,
2265 xprt
->address_strings
[RPC_DISPLAY_PROTO
],
2266 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2267 xprt
->address_strings
[RPC_DISPLAY_PORT
]);
2269 status
= xs_tcp_finish_connecting(xprt
, sock
);
2270 trace_rpc_socket_connect(xprt
, sock
, status
);
2271 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2272 xprt
, -status
, xprt_connected(xprt
),
2273 sock
->sk
->sk_state
);
2276 printk("%s: connect returned unhandled error %d\n",
2279 case -EADDRNOTAVAIL
:
2280 /* We're probably in TIME_WAIT. Get rid of existing socket,
2283 xs_tcp_force_close(xprt
);
2288 xprt_unlock_connect(xprt
, transport
);
2291 /* Happens, for instance, if the user specified a link
2292 * local IPv6 address without a scope-id.
2302 * xs_tcp_force_close() wakes tasks with -EIO.
2303 * We need to wake them first to ensure the
2304 * correct error code.
2306 xprt_wake_pending_tasks(xprt
, status
);
2307 xs_tcp_force_close(xprt
);
2312 xprt_clear_connecting(xprt
);
2313 xprt_unlock_connect(xprt
, transport
);
2314 xprt_wake_pending_tasks(xprt
, status
);
2318 * xs_connect - connect a socket to a remote endpoint
2319 * @xprt: pointer to transport structure
2320 * @task: address of RPC task that manages state of connect request
2322 * TCP: If the remote end dropped the connection, delay reconnecting.
2324 * UDP socket connects are synchronous, but we use a work queue anyway
2325 * to guarantee that even unprivileged user processes can set up a
2326 * socket on a privileged port.
2328 * If a UDP socket connect fails, the delay behavior here prevents
2329 * retry floods (hard mounts).
2331 static void xs_connect(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
2333 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2334 unsigned long delay
= 0;
2336 WARN_ON_ONCE(!xprt_lock_connect(xprt
, task
, transport
));
2338 if (transport
->sock
!= NULL
) {
2339 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2341 xprt
, xprt
->reestablish_timeout
/ HZ
);
2343 /* Start by resetting any existing state */
2344 xs_reset_transport(transport
);
2346 delay
= xprt_reconnect_delay(xprt
);
2347 xprt_reconnect_backoff(xprt
, XS_TCP_INIT_REEST_TO
);
2350 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt
);
2352 queue_delayed_work(xprtiod_workqueue
,
2353 &transport
->connect_worker
,
2357 static void xs_wake_disconnect(struct sock_xprt
*transport
)
2359 if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT
, &transport
->sock_state
))
2360 xs_tcp_force_close(&transport
->xprt
);
2363 static void xs_wake_write(struct sock_xprt
*transport
)
2365 if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE
, &transport
->sock_state
))
2366 xprt_write_space(&transport
->xprt
);
2369 static void xs_wake_error(struct sock_xprt
*transport
)
2373 if (!test_bit(XPRT_SOCK_WAKE_ERROR
, &transport
->sock_state
))
2375 mutex_lock(&transport
->recv_mutex
);
2376 if (transport
->sock
== NULL
)
2378 if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR
, &transport
->sock_state
))
2380 sockerr
= xchg(&transport
->xprt_err
, 0);
2382 xprt_wake_pending_tasks(&transport
->xprt
, sockerr
);
2384 mutex_unlock(&transport
->recv_mutex
);
2387 static void xs_wake_pending(struct sock_xprt
*transport
)
2389 if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING
, &transport
->sock_state
))
2390 xprt_wake_pending_tasks(&transport
->xprt
, -EAGAIN
);
2393 static void xs_error_handle(struct work_struct
*work
)
2395 struct sock_xprt
*transport
= container_of(work
,
2396 struct sock_xprt
, error_worker
);
2398 xs_wake_disconnect(transport
);
2399 xs_wake_write(transport
);
2400 xs_wake_error(transport
);
2401 xs_wake_pending(transport
);
2405 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2406 * @xprt: rpc_xprt struct containing statistics
2410 static void xs_local_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2414 if (xprt_connected(xprt
))
2415 idle_time
= (long)(jiffies
- xprt
->last_used
) / HZ
;
2417 seq_printf(seq
, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
2418 "%llu %llu %lu %llu %llu\n",
2419 xprt
->stat
.bind_count
,
2420 xprt
->stat
.connect_count
,
2421 xprt
->stat
.connect_time
/ HZ
,
2425 xprt
->stat
.bad_xids
,
2428 xprt
->stat
.max_slots
,
2429 xprt
->stat
.sending_u
,
2430 xprt
->stat
.pending_u
);
2434 * xs_udp_print_stats - display UDP socket-specifc stats
2435 * @xprt: rpc_xprt struct containing statistics
2439 static void xs_udp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2441 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2443 seq_printf(seq
, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2446 xprt
->stat
.bind_count
,
2449 xprt
->stat
.bad_xids
,
2452 xprt
->stat
.max_slots
,
2453 xprt
->stat
.sending_u
,
2454 xprt
->stat
.pending_u
);
2458 * xs_tcp_print_stats - display TCP socket-specifc stats
2459 * @xprt: rpc_xprt struct containing statistics
2463 static void xs_tcp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2465 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2468 if (xprt_connected(xprt
))
2469 idle_time
= (long)(jiffies
- xprt
->last_used
) / HZ
;
2471 seq_printf(seq
, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2472 "%llu %llu %lu %llu %llu\n",
2474 xprt
->stat
.bind_count
,
2475 xprt
->stat
.connect_count
,
2476 xprt
->stat
.connect_time
/ HZ
,
2480 xprt
->stat
.bad_xids
,
2483 xprt
->stat
.max_slots
,
2484 xprt
->stat
.sending_u
,
2485 xprt
->stat
.pending_u
);
2489 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2490 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2491 * to use the server side send routines.
2493 static int bc_malloc(struct rpc_task
*task
)
2495 struct rpc_rqst
*rqst
= task
->tk_rqstp
;
2496 size_t size
= rqst
->rq_callsize
;
2498 struct rpc_buffer
*buf
;
2500 if (size
> PAGE_SIZE
- sizeof(struct rpc_buffer
)) {
2501 WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
2506 page
= alloc_page(GFP_KERNEL
);
2510 buf
= page_address(page
);
2511 buf
->len
= PAGE_SIZE
;
2513 rqst
->rq_buffer
= buf
->data
;
2514 rqst
->rq_rbuffer
= (char *)rqst
->rq_buffer
+ rqst
->rq_callsize
;
2519 * Free the space allocated in the bc_alloc routine
2521 static void bc_free(struct rpc_task
*task
)
2523 void *buffer
= task
->tk_rqstp
->rq_buffer
;
2524 struct rpc_buffer
*buf
;
2526 buf
= container_of(buffer
, struct rpc_buffer
, data
);
2527 free_page((unsigned long)buf
);
2530 static int bc_sendto(struct rpc_rqst
*req
)
2532 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
2533 struct sock_xprt
*transport
=
2534 container_of(req
->rq_xprt
, struct sock_xprt
, xprt
);
2535 struct msghdr msg
= {
2538 rpc_fraghdr marker
= cpu_to_be32(RPC_LAST_STREAM_FRAGMENT
|
2540 unsigned int sent
= 0;
2543 req
->rq_xtime
= ktime_get();
2544 err
= xprt_sock_sendmsg(transport
->sock
, &msg
, xdr
, 0, marker
, &sent
);
2546 if (err
< 0 || sent
!= (xdr
->len
+ sizeof(marker
)))
2552 * The send routine. Borrows from svc_send
2554 static int bc_send_request(struct rpc_rqst
*req
)
2556 struct svc_xprt
*xprt
;
2560 * Get the server socket associated with this callback xprt
2562 xprt
= req
->rq_xprt
->bc_xprt
;
2565 * Grab the mutex to serialize data as the connection is shared
2566 * with the fore channel
2568 mutex_lock(&xprt
->xpt_mutex
);
2569 if (test_bit(XPT_DEAD
, &xprt
->xpt_flags
))
2572 len
= bc_sendto(req
);
2573 mutex_unlock(&xprt
->xpt_mutex
);
2582 * The close routine. Since this is client initiated, we do nothing
2585 static void bc_close(struct rpc_xprt
*xprt
)
2590 * The xprt destroy routine. Again, because this connection is client
2591 * initiated, we do nothing
2594 static void bc_destroy(struct rpc_xprt
*xprt
)
2596 dprintk("RPC: bc_destroy xprt %p\n", xprt
);
2599 module_put(THIS_MODULE
);
2602 static const struct rpc_xprt_ops xs_local_ops
= {
2603 .reserve_xprt
= xprt_reserve_xprt
,
2604 .release_xprt
= xprt_release_xprt
,
2605 .alloc_slot
= xprt_alloc_slot
,
2606 .free_slot
= xprt_free_slot
,
2607 .rpcbind
= xs_local_rpcbind
,
2608 .set_port
= xs_local_set_port
,
2609 .connect
= xs_local_connect
,
2610 .buf_alloc
= rpc_malloc
,
2611 .buf_free
= rpc_free
,
2612 .prepare_request
= xs_stream_prepare_request
,
2613 .send_request
= xs_local_send_request
,
2614 .wait_for_reply_request
= xprt_wait_for_reply_request_def
,
2616 .destroy
= xs_destroy
,
2617 .print_stats
= xs_local_print_stats
,
2618 .enable_swap
= xs_enable_swap
,
2619 .disable_swap
= xs_disable_swap
,
2622 static const struct rpc_xprt_ops xs_udp_ops
= {
2623 .set_buffer_size
= xs_udp_set_buffer_size
,
2624 .reserve_xprt
= xprt_reserve_xprt_cong
,
2625 .release_xprt
= xprt_release_xprt_cong
,
2626 .alloc_slot
= xprt_alloc_slot
,
2627 .free_slot
= xprt_free_slot
,
2628 .rpcbind
= rpcb_getport_async
,
2629 .set_port
= xs_set_port
,
2630 .connect
= xs_connect
,
2631 .buf_alloc
= rpc_malloc
,
2632 .buf_free
= rpc_free
,
2633 .send_request
= xs_udp_send_request
,
2634 .wait_for_reply_request
= xprt_wait_for_reply_request_rtt
,
2635 .timer
= xs_udp_timer
,
2636 .release_request
= xprt_release_rqst_cong
,
2638 .destroy
= xs_destroy
,
2639 .print_stats
= xs_udp_print_stats
,
2640 .enable_swap
= xs_enable_swap
,
2641 .disable_swap
= xs_disable_swap
,
2642 .inject_disconnect
= xs_inject_disconnect
,
2645 static const struct rpc_xprt_ops xs_tcp_ops
= {
2646 .reserve_xprt
= xprt_reserve_xprt
,
2647 .release_xprt
= xprt_release_xprt
,
2648 .alloc_slot
= xprt_alloc_slot
,
2649 .free_slot
= xprt_free_slot
,
2650 .rpcbind
= rpcb_getport_async
,
2651 .set_port
= xs_set_port
,
2652 .connect
= xs_connect
,
2653 .buf_alloc
= rpc_malloc
,
2654 .buf_free
= rpc_free
,
2655 .prepare_request
= xs_stream_prepare_request
,
2656 .send_request
= xs_tcp_send_request
,
2657 .wait_for_reply_request
= xprt_wait_for_reply_request_def
,
2658 .close
= xs_tcp_shutdown
,
2659 .destroy
= xs_destroy
,
2660 .set_connect_timeout
= xs_tcp_set_connect_timeout
,
2661 .print_stats
= xs_tcp_print_stats
,
2662 .enable_swap
= xs_enable_swap
,
2663 .disable_swap
= xs_disable_swap
,
2664 .inject_disconnect
= xs_inject_disconnect
,
2665 #ifdef CONFIG_SUNRPC_BACKCHANNEL
2666 .bc_setup
= xprt_setup_bc
,
2667 .bc_maxpayload
= xs_tcp_bc_maxpayload
,
2668 .bc_num_slots
= xprt_bc_max_slots
,
2669 .bc_free_rqst
= xprt_free_bc_rqst
,
2670 .bc_destroy
= xprt_destroy_bc
,
2675 * The rpc_xprt_ops for the server backchannel
2678 static const struct rpc_xprt_ops bc_tcp_ops
= {
2679 .reserve_xprt
= xprt_reserve_xprt
,
2680 .release_xprt
= xprt_release_xprt
,
2681 .alloc_slot
= xprt_alloc_slot
,
2682 .free_slot
= xprt_free_slot
,
2683 .buf_alloc
= bc_malloc
,
2684 .buf_free
= bc_free
,
2685 .send_request
= bc_send_request
,
2686 .wait_for_reply_request
= xprt_wait_for_reply_request_def
,
2688 .destroy
= bc_destroy
,
2689 .print_stats
= xs_tcp_print_stats
,
2690 .enable_swap
= xs_enable_swap
,
2691 .disable_swap
= xs_disable_swap
,
2692 .inject_disconnect
= xs_inject_disconnect
,
2695 static int xs_init_anyaddr(const int family
, struct sockaddr
*sap
)
2697 static const struct sockaddr_in sin
= {
2698 .sin_family
= AF_INET
,
2699 .sin_addr
.s_addr
= htonl(INADDR_ANY
),
2701 static const struct sockaddr_in6 sin6
= {
2702 .sin6_family
= AF_INET6
,
2703 .sin6_addr
= IN6ADDR_ANY_INIT
,
2710 memcpy(sap
, &sin
, sizeof(sin
));
2713 memcpy(sap
, &sin6
, sizeof(sin6
));
2716 dprintk("RPC: %s: Bad address family\n", __func__
);
2717 return -EAFNOSUPPORT
;
2722 static struct rpc_xprt
*xs_setup_xprt(struct xprt_create
*args
,
2723 unsigned int slot_table_size
,
2724 unsigned int max_slot_table_size
)
2726 struct rpc_xprt
*xprt
;
2727 struct sock_xprt
*new;
2729 if (args
->addrlen
> sizeof(xprt
->addr
)) {
2730 dprintk("RPC: xs_setup_xprt: address too large\n");
2731 return ERR_PTR(-EBADF
);
2734 xprt
= xprt_alloc(args
->net
, sizeof(*new), slot_table_size
,
2735 max_slot_table_size
);
2737 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2739 return ERR_PTR(-ENOMEM
);
2742 new = container_of(xprt
, struct sock_xprt
, xprt
);
2743 mutex_init(&new->recv_mutex
);
2744 memcpy(&xprt
->addr
, args
->dstaddr
, args
->addrlen
);
2745 xprt
->addrlen
= args
->addrlen
;
2747 memcpy(&new->srcaddr
, args
->srcaddr
, args
->addrlen
);
2750 err
= xs_init_anyaddr(args
->dstaddr
->sa_family
,
2751 (struct sockaddr
*)&new->srcaddr
);
2754 return ERR_PTR(err
);
2761 static const struct rpc_timeout xs_local_default_timeout
= {
2762 .to_initval
= 10 * HZ
,
2763 .to_maxval
= 10 * HZ
,
2768 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2769 * @args: rpc transport creation arguments
2771 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2773 static struct rpc_xprt
*xs_setup_local(struct xprt_create
*args
)
2775 struct sockaddr_un
*sun
= (struct sockaddr_un
*)args
->dstaddr
;
2776 struct sock_xprt
*transport
;
2777 struct rpc_xprt
*xprt
;
2778 struct rpc_xprt
*ret
;
2780 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
,
2781 xprt_max_tcp_slot_table_entries
);
2784 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2787 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
2789 xprt
->bind_timeout
= XS_BIND_TO
;
2790 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2791 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2793 xprt
->ops
= &xs_local_ops
;
2794 xprt
->timeout
= &xs_local_default_timeout
;
2796 INIT_WORK(&transport
->recv_worker
, xs_stream_data_receive_workfn
);
2797 INIT_WORK(&transport
->error_worker
, xs_error_handle
);
2798 INIT_DELAYED_WORK(&transport
->connect_worker
, xs_dummy_setup_socket
);
2800 switch (sun
->sun_family
) {
2802 if (sun
->sun_path
[0] != '/') {
2803 dprintk("RPC: bad AF_LOCAL address: %s\n",
2805 ret
= ERR_PTR(-EINVAL
);
2808 xprt_set_bound(xprt
);
2809 xs_format_peer_addresses(xprt
, "local", RPCBIND_NETID_LOCAL
);
2810 ret
= ERR_PTR(xs_local_setup_socket(transport
));
2815 ret
= ERR_PTR(-EAFNOSUPPORT
);
2819 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2820 xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
2822 if (try_module_get(THIS_MODULE
))
2824 ret
= ERR_PTR(-EINVAL
);
2830 static const struct rpc_timeout xs_udp_default_timeout
= {
2831 .to_initval
= 5 * HZ
,
2832 .to_maxval
= 30 * HZ
,
2833 .to_increment
= 5 * HZ
,
2838 * xs_setup_udp - Set up transport to use a UDP socket
2839 * @args: rpc transport creation arguments
2842 static struct rpc_xprt
*xs_setup_udp(struct xprt_create
*args
)
2844 struct sockaddr
*addr
= args
->dstaddr
;
2845 struct rpc_xprt
*xprt
;
2846 struct sock_xprt
*transport
;
2847 struct rpc_xprt
*ret
;
2849 xprt
= xs_setup_xprt(args
, xprt_udp_slot_table_entries
,
2850 xprt_udp_slot_table_entries
);
2853 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2855 xprt
->prot
= IPPROTO_UDP
;
2856 /* XXX: header size can vary due to auth type, IPv6, etc. */
2857 xprt
->max_payload
= (1U << 16) - (MAX_HEADER
<< 3);
2859 xprt
->bind_timeout
= XS_BIND_TO
;
2860 xprt
->reestablish_timeout
= XS_UDP_REEST_TO
;
2861 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2863 xprt
->ops
= &xs_udp_ops
;
2865 xprt
->timeout
= &xs_udp_default_timeout
;
2867 INIT_WORK(&transport
->recv_worker
, xs_udp_data_receive_workfn
);
2868 INIT_WORK(&transport
->error_worker
, xs_error_handle
);
2869 INIT_DELAYED_WORK(&transport
->connect_worker
, xs_udp_setup_socket
);
2871 switch (addr
->sa_family
) {
2873 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
2874 xprt_set_bound(xprt
);
2876 xs_format_peer_addresses(xprt
, "udp", RPCBIND_NETID_UDP
);
2879 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
2880 xprt_set_bound(xprt
);
2882 xs_format_peer_addresses(xprt
, "udp", RPCBIND_NETID_UDP6
);
2885 ret
= ERR_PTR(-EAFNOSUPPORT
);
2889 if (xprt_bound(xprt
))
2890 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2891 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2892 xprt
->address_strings
[RPC_DISPLAY_PORT
],
2893 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2895 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2896 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2897 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2899 if (try_module_get(THIS_MODULE
))
2901 ret
= ERR_PTR(-EINVAL
);
2907 static const struct rpc_timeout xs_tcp_default_timeout
= {
2908 .to_initval
= 60 * HZ
,
2909 .to_maxval
= 60 * HZ
,
2914 * xs_setup_tcp - Set up transport to use a TCP socket
2915 * @args: rpc transport creation arguments
2918 static struct rpc_xprt
*xs_setup_tcp(struct xprt_create
*args
)
2920 struct sockaddr
*addr
= args
->dstaddr
;
2921 struct rpc_xprt
*xprt
;
2922 struct sock_xprt
*transport
;
2923 struct rpc_xprt
*ret
;
2924 unsigned int max_slot_table_size
= xprt_max_tcp_slot_table_entries
;
2926 if (args
->flags
& XPRT_CREATE_INFINITE_SLOTS
)
2927 max_slot_table_size
= RPC_MAX_SLOT_TABLE_LIMIT
;
2929 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
,
2930 max_slot_table_size
);
2933 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2935 xprt
->prot
= IPPROTO_TCP
;
2936 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
2938 xprt
->bind_timeout
= XS_BIND_TO
;
2939 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2940 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2942 xprt
->ops
= &xs_tcp_ops
;
2943 xprt
->timeout
= &xs_tcp_default_timeout
;
2945 xprt
->max_reconnect_timeout
= xprt
->timeout
->to_maxval
;
2946 xprt
->connect_timeout
= xprt
->timeout
->to_initval
*
2947 (xprt
->timeout
->to_retries
+ 1);
2949 INIT_WORK(&transport
->recv_worker
, xs_stream_data_receive_workfn
);
2950 INIT_WORK(&transport
->error_worker
, xs_error_handle
);
2951 INIT_DELAYED_WORK(&transport
->connect_worker
, xs_tcp_setup_socket
);
2953 switch (addr
->sa_family
) {
2955 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
2956 xprt_set_bound(xprt
);
2958 xs_format_peer_addresses(xprt
, "tcp", RPCBIND_NETID_TCP
);
2961 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
2962 xprt_set_bound(xprt
);
2964 xs_format_peer_addresses(xprt
, "tcp", RPCBIND_NETID_TCP6
);
2967 ret
= ERR_PTR(-EAFNOSUPPORT
);
2971 if (xprt_bound(xprt
))
2972 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2973 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2974 xprt
->address_strings
[RPC_DISPLAY_PORT
],
2975 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2977 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2978 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2979 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2981 if (try_module_get(THIS_MODULE
))
2983 ret
= ERR_PTR(-EINVAL
);
2990 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2991 * @args: rpc transport creation arguments
2994 static struct rpc_xprt
*xs_setup_bc_tcp(struct xprt_create
*args
)
2996 struct sockaddr
*addr
= args
->dstaddr
;
2997 struct rpc_xprt
*xprt
;
2998 struct sock_xprt
*transport
;
2999 struct svc_sock
*bc_sock
;
3000 struct rpc_xprt
*ret
;
3002 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
,
3003 xprt_tcp_slot_table_entries
);
3006 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
3008 xprt
->prot
= IPPROTO_TCP
;
3009 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
3010 xprt
->timeout
= &xs_tcp_default_timeout
;
3013 xprt_set_bound(xprt
);
3014 xprt
->bind_timeout
= 0;
3015 xprt
->reestablish_timeout
= 0;
3016 xprt
->idle_timeout
= 0;
3018 xprt
->ops
= &bc_tcp_ops
;
3020 switch (addr
->sa_family
) {
3022 xs_format_peer_addresses(xprt
, "tcp",
3026 xs_format_peer_addresses(xprt
, "tcp",
3027 RPCBIND_NETID_TCP6
);
3030 ret
= ERR_PTR(-EAFNOSUPPORT
);
3034 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
3035 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
3036 xprt
->address_strings
[RPC_DISPLAY_PORT
],
3037 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
3040 * Once we've associated a backchannel xprt with a connection,
3041 * we want to keep it around as long as the connection lasts,
3042 * in case we need to start using it for a backchannel again;
3043 * this reference won't be dropped until bc_xprt is destroyed.
3046 args
->bc_xprt
->xpt_bc_xprt
= xprt
;
3047 xprt
->bc_xprt
= args
->bc_xprt
;
3048 bc_sock
= container_of(args
->bc_xprt
, struct svc_sock
, sk_xprt
);
3049 transport
->sock
= bc_sock
->sk_sock
;
3050 transport
->inet
= bc_sock
->sk_sk
;
3053 * Since we don't want connections for the backchannel, we set
3054 * the xprt status to connected
3056 xprt_set_connected(xprt
);
3058 if (try_module_get(THIS_MODULE
))
3061 args
->bc_xprt
->xpt_bc_xprt
= NULL
;
3062 args
->bc_xprt
->xpt_bc_xps
= NULL
;
3064 ret
= ERR_PTR(-EINVAL
);
3070 static struct xprt_class xs_local_transport
= {
3071 .list
= LIST_HEAD_INIT(xs_local_transport
.list
),
3072 .name
= "named UNIX socket",
3073 .owner
= THIS_MODULE
,
3074 .ident
= XPRT_TRANSPORT_LOCAL
,
3075 .setup
= xs_setup_local
,
3078 static struct xprt_class xs_udp_transport
= {
3079 .list
= LIST_HEAD_INIT(xs_udp_transport
.list
),
3081 .owner
= THIS_MODULE
,
3082 .ident
= XPRT_TRANSPORT_UDP
,
3083 .setup
= xs_setup_udp
,
3086 static struct xprt_class xs_tcp_transport
= {
3087 .list
= LIST_HEAD_INIT(xs_tcp_transport
.list
),
3089 .owner
= THIS_MODULE
,
3090 .ident
= XPRT_TRANSPORT_TCP
,
3091 .setup
= xs_setup_tcp
,
3094 static struct xprt_class xs_bc_tcp_transport
= {
3095 .list
= LIST_HEAD_INIT(xs_bc_tcp_transport
.list
),
3096 .name
= "tcp NFSv4.1 backchannel",
3097 .owner
= THIS_MODULE
,
3098 .ident
= XPRT_TRANSPORT_BC_TCP
,
3099 .setup
= xs_setup_bc_tcp
,
3103 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
3106 int init_socket_xprt(void)
3108 if (!sunrpc_table_header
)
3109 sunrpc_table_header
= register_sysctl_table(sunrpc_table
);
3111 xprt_register_transport(&xs_local_transport
);
3112 xprt_register_transport(&xs_udp_transport
);
3113 xprt_register_transport(&xs_tcp_transport
);
3114 xprt_register_transport(&xs_bc_tcp_transport
);
3120 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3123 void cleanup_socket_xprt(void)
3125 if (sunrpc_table_header
) {
3126 unregister_sysctl_table(sunrpc_table_header
);
3127 sunrpc_table_header
= NULL
;
3130 xprt_unregister_transport(&xs_local_transport
);
3131 xprt_unregister_transport(&xs_udp_transport
);
3132 xprt_unregister_transport(&xs_tcp_transport
);
3133 xprt_unregister_transport(&xs_bc_tcp_transport
);
3136 static int param_set_uint_minmax(const char *val
,
3137 const struct kernel_param
*kp
,
3138 unsigned int min
, unsigned int max
)
3145 ret
= kstrtouint(val
, 0, &num
);
3148 if (num
< min
|| num
> max
)
3150 *((unsigned int *)kp
->arg
) = num
;
3154 static int param_set_portnr(const char *val
, const struct kernel_param
*kp
)
3156 return param_set_uint_minmax(val
, kp
,
3161 static const struct kernel_param_ops param_ops_portnr
= {
3162 .set
= param_set_portnr
,
3163 .get
= param_get_uint
,
3166 #define param_check_portnr(name, p) \
3167 __param_check(name, p, unsigned int);
3169 module_param_named(min_resvport
, xprt_min_resvport
, portnr
, 0644);
3170 module_param_named(max_resvport
, xprt_max_resvport
, portnr
, 0644);
3172 static int param_set_slot_table_size(const char *val
,
3173 const struct kernel_param
*kp
)
3175 return param_set_uint_minmax(val
, kp
,
3177 RPC_MAX_SLOT_TABLE
);
3180 static const struct kernel_param_ops param_ops_slot_table_size
= {
3181 .set
= param_set_slot_table_size
,
3182 .get
= param_get_uint
,
3185 #define param_check_slot_table_size(name, p) \
3186 __param_check(name, p, unsigned int);
3188 static int param_set_max_slot_table_size(const char *val
,
3189 const struct kernel_param
*kp
)
3191 return param_set_uint_minmax(val
, kp
,
3193 RPC_MAX_SLOT_TABLE_LIMIT
);
3196 static const struct kernel_param_ops param_ops_max_slot_table_size
= {
3197 .set
= param_set_max_slot_table_size
,
3198 .get
= param_get_uint
,
3201 #define param_check_max_slot_table_size(name, p) \
3202 __param_check(name, p, unsigned int);
3204 module_param_named(tcp_slot_table_entries
, xprt_tcp_slot_table_entries
,
3205 slot_table_size
, 0644);
3206 module_param_named(tcp_max_slot_table_entries
, xprt_max_tcp_slot_table_entries
,
3207 max_slot_table_size
, 0644);
3208 module_param_named(udp_slot_table_entries
, xprt_udp_slot_table_entries
,
3209 slot_table_size
, 0644);