2 * linux/net/sunrpc/xprtsock.c
4 * Client-side transport implementation for sockets.
6 * TCP callback races fixes (C) 1998 Red Hat
7 * TCP send fixes (C) 1998 Red Hat
8 * TCP NFS related read + write fixes
9 * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
11 * Rewrite of larges part of the code in order to stabilize TCP stuff.
12 * Fix behaviour when socket buffer is full.
13 * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
15 * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
17 * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
18 * <gilles.quillard@bull.net>
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/capability.h>
26 #include <linux/pagemap.h>
27 #include <linux/errno.h>
28 #include <linux/socket.h>
30 #include <linux/net.h>
33 #include <linux/udp.h>
34 #include <linux/tcp.h>
35 #include <linux/sunrpc/clnt.h>
36 #include <linux/sunrpc/addr.h>
37 #include <linux/sunrpc/sched.h>
38 #include <linux/sunrpc/svcsock.h>
39 #include <linux/sunrpc/xprtsock.h>
40 #include <linux/file.h>
41 #ifdef CONFIG_SUNRPC_BACKCHANNEL
42 #include <linux/sunrpc/bc_xprt.h>
46 #include <net/checksum.h>
50 #include <trace/events/sunrpc.h>
54 static void xs_close(struct rpc_xprt
*xprt
);
59 static unsigned int xprt_udp_slot_table_entries
= RPC_DEF_SLOT_TABLE
;
60 static unsigned int xprt_tcp_slot_table_entries
= RPC_MIN_SLOT_TABLE
;
61 static unsigned int xprt_max_tcp_slot_table_entries
= RPC_MAX_SLOT_TABLE
;
63 static unsigned int xprt_min_resvport
= RPC_DEF_MIN_RESVPORT
;
64 static unsigned int xprt_max_resvport
= RPC_DEF_MAX_RESVPORT
;
66 #define XS_TCP_LINGER_TO (15U * HZ)
67 static unsigned int xs_tcp_fin_timeout __read_mostly
= XS_TCP_LINGER_TO
;
70 * We can register our own files under /proc/sys/sunrpc by
71 * calling register_sysctl_table() again. The files in that
72 * directory become the union of all files registered there.
74 * We simply need to make sure that we don't collide with
75 * someone else's file names!
80 static unsigned int min_slot_table_size
= RPC_MIN_SLOT_TABLE
;
81 static unsigned int max_slot_table_size
= RPC_MAX_SLOT_TABLE
;
82 static unsigned int max_tcp_slot_table_limit
= RPC_MAX_SLOT_TABLE_LIMIT
;
83 static unsigned int xprt_min_resvport_limit
= RPC_MIN_RESVPORT
;
84 static unsigned int xprt_max_resvport_limit
= RPC_MAX_RESVPORT
;
86 static struct ctl_table_header
*sunrpc_table_header
;
89 * FIXME: changing the UDP slot table size should also resize the UDP
90 * socket buffers for existing UDP transports
92 static struct ctl_table xs_tunables_table
[] = {
94 .procname
= "udp_slot_table_entries",
95 .data
= &xprt_udp_slot_table_entries
,
96 .maxlen
= sizeof(unsigned int),
98 .proc_handler
= proc_dointvec_minmax
,
99 .extra1
= &min_slot_table_size
,
100 .extra2
= &max_slot_table_size
103 .procname
= "tcp_slot_table_entries",
104 .data
= &xprt_tcp_slot_table_entries
,
105 .maxlen
= sizeof(unsigned int),
107 .proc_handler
= proc_dointvec_minmax
,
108 .extra1
= &min_slot_table_size
,
109 .extra2
= &max_slot_table_size
112 .procname
= "tcp_max_slot_table_entries",
113 .data
= &xprt_max_tcp_slot_table_entries
,
114 .maxlen
= sizeof(unsigned int),
116 .proc_handler
= proc_dointvec_minmax
,
117 .extra1
= &min_slot_table_size
,
118 .extra2
= &max_tcp_slot_table_limit
121 .procname
= "min_resvport",
122 .data
= &xprt_min_resvport
,
123 .maxlen
= sizeof(unsigned int),
125 .proc_handler
= proc_dointvec_minmax
,
126 .extra1
= &xprt_min_resvport_limit
,
127 .extra2
= &xprt_max_resvport_limit
130 .procname
= "max_resvport",
131 .data
= &xprt_max_resvport
,
132 .maxlen
= sizeof(unsigned int),
134 .proc_handler
= proc_dointvec_minmax
,
135 .extra1
= &xprt_min_resvport_limit
,
136 .extra2
= &xprt_max_resvport_limit
139 .procname
= "tcp_fin_timeout",
140 .data
= &xs_tcp_fin_timeout
,
141 .maxlen
= sizeof(xs_tcp_fin_timeout
),
143 .proc_handler
= proc_dointvec_jiffies
,
148 static struct ctl_table sunrpc_table
[] = {
150 .procname
= "sunrpc",
152 .child
= xs_tunables_table
160 * Wait duration for a reply from the RPC portmapper.
162 #define XS_BIND_TO (60U * HZ)
165 * Delay if a UDP socket connect error occurs. This is most likely some
166 * kind of resource problem on the local host.
168 #define XS_UDP_REEST_TO (2U * HZ)
171 * The reestablish timeout allows clients to delay for a bit before attempting
172 * to reconnect to a server that just dropped our connection.
174 * We implement an exponential backoff when trying to reestablish a TCP
175 * transport connection with the server. Some servers like to drop a TCP
176 * connection when they are overworked, so we start with a short timeout and
177 * increase over time if the server is down or not responding.
179 #define XS_TCP_INIT_REEST_TO (3U * HZ)
180 #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
183 * TCP idle timeout; client drops the transport socket if it is idle
184 * for this long. Note that we also timeout UDP sockets to prevent
185 * holding port numbers when there is no RPC traffic.
187 #define XS_IDLE_DISC_TO (5U * 60 * HZ)
190 # undef RPC_DEBUG_DATA
191 # define RPCDBG_FACILITY RPCDBG_TRANS
194 #ifdef RPC_DEBUG_DATA
195 static void xs_pktdump(char *msg
, u32
*packet
, unsigned int count
)
197 u8
*buf
= (u8
*) packet
;
200 dprintk("RPC: %s\n", msg
);
201 for (j
= 0; j
< count
&& j
< 128; j
+= 4) {
205 dprintk("0x%04x ", j
);
207 dprintk("%02x%02x%02x%02x ",
208 buf
[j
], buf
[j
+1], buf
[j
+2], buf
[j
+3]);
213 static inline void xs_pktdump(char *msg
, u32
*packet
, unsigned int count
)
220 struct rpc_xprt xprt
;
225 struct socket
* sock
;
229 * State of TCP reply receive
238 unsigned long tcp_copied
,
242 * Connection of transports
244 struct delayed_work connect_worker
;
245 struct sockaddr_storage srcaddr
;
246 unsigned short srcport
;
249 * UDP socket buffer size parameters
255 * Saved socket callback addresses
257 void (*old_data_ready
)(struct sock
*, int);
258 void (*old_state_change
)(struct sock
*);
259 void (*old_write_space
)(struct sock
*);
263 * TCP receive state flags
265 #define TCP_RCV_LAST_FRAG (1UL << 0)
266 #define TCP_RCV_COPY_FRAGHDR (1UL << 1)
267 #define TCP_RCV_COPY_XID (1UL << 2)
268 #define TCP_RCV_COPY_DATA (1UL << 3)
269 #define TCP_RCV_READ_CALLDIR (1UL << 4)
270 #define TCP_RCV_COPY_CALLDIR (1UL << 5)
275 #define TCP_RPC_REPLY (1UL << 6)
277 static inline struct sockaddr
*xs_addr(struct rpc_xprt
*xprt
)
279 return (struct sockaddr
*) &xprt
->addr
;
282 static inline struct sockaddr_un
*xs_addr_un(struct rpc_xprt
*xprt
)
284 return (struct sockaddr_un
*) &xprt
->addr
;
287 static inline struct sockaddr_in
*xs_addr_in(struct rpc_xprt
*xprt
)
289 return (struct sockaddr_in
*) &xprt
->addr
;
292 static inline struct sockaddr_in6
*xs_addr_in6(struct rpc_xprt
*xprt
)
294 return (struct sockaddr_in6
*) &xprt
->addr
;
297 static void xs_format_common_peer_addresses(struct rpc_xprt
*xprt
)
299 struct sockaddr
*sap
= xs_addr(xprt
);
300 struct sockaddr_in6
*sin6
;
301 struct sockaddr_in
*sin
;
302 struct sockaddr_un
*sun
;
305 switch (sap
->sa_family
) {
307 sun
= xs_addr_un(xprt
);
308 strlcpy(buf
, sun
->sun_path
, sizeof(buf
));
309 xprt
->address_strings
[RPC_DISPLAY_ADDR
] =
310 kstrdup(buf
, GFP_KERNEL
);
313 (void)rpc_ntop(sap
, buf
, sizeof(buf
));
314 xprt
->address_strings
[RPC_DISPLAY_ADDR
] =
315 kstrdup(buf
, GFP_KERNEL
);
316 sin
= xs_addr_in(xprt
);
317 snprintf(buf
, sizeof(buf
), "%08x", ntohl(sin
->sin_addr
.s_addr
));
320 (void)rpc_ntop(sap
, buf
, sizeof(buf
));
321 xprt
->address_strings
[RPC_DISPLAY_ADDR
] =
322 kstrdup(buf
, GFP_KERNEL
);
323 sin6
= xs_addr_in6(xprt
);
324 snprintf(buf
, sizeof(buf
), "%pi6", &sin6
->sin6_addr
);
330 xprt
->address_strings
[RPC_DISPLAY_HEX_ADDR
] = kstrdup(buf
, GFP_KERNEL
);
333 static void xs_format_common_peer_ports(struct rpc_xprt
*xprt
)
335 struct sockaddr
*sap
= xs_addr(xprt
);
338 snprintf(buf
, sizeof(buf
), "%u", rpc_get_port(sap
));
339 xprt
->address_strings
[RPC_DISPLAY_PORT
] = kstrdup(buf
, GFP_KERNEL
);
341 snprintf(buf
, sizeof(buf
), "%4hx", rpc_get_port(sap
));
342 xprt
->address_strings
[RPC_DISPLAY_HEX_PORT
] = kstrdup(buf
, GFP_KERNEL
);
345 static void xs_format_peer_addresses(struct rpc_xprt
*xprt
,
346 const char *protocol
,
349 xprt
->address_strings
[RPC_DISPLAY_PROTO
] = protocol
;
350 xprt
->address_strings
[RPC_DISPLAY_NETID
] = netid
;
351 xs_format_common_peer_addresses(xprt
);
352 xs_format_common_peer_ports(xprt
);
355 static void xs_update_peer_port(struct rpc_xprt
*xprt
)
357 kfree(xprt
->address_strings
[RPC_DISPLAY_HEX_PORT
]);
358 kfree(xprt
->address_strings
[RPC_DISPLAY_PORT
]);
360 xs_format_common_peer_ports(xprt
);
363 static void xs_free_peer_addresses(struct rpc_xprt
*xprt
)
367 for (i
= 0; i
< RPC_DISPLAY_MAX
; i
++)
369 case RPC_DISPLAY_PROTO
:
370 case RPC_DISPLAY_NETID
:
373 kfree(xprt
->address_strings
[i
]);
377 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
379 static int xs_send_kvec(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
, struct kvec
*vec
, unsigned int base
, int more
)
381 struct msghdr msg
= {
383 .msg_namelen
= addrlen
,
384 .msg_flags
= XS_SENDMSG_FLAGS
| (more
? MSG_MORE
: 0),
387 .iov_base
= vec
->iov_base
+ base
,
388 .iov_len
= vec
->iov_len
- base
,
391 if (iov
.iov_len
!= 0)
392 return kernel_sendmsg(sock
, &msg
, &iov
, 1, iov
.iov_len
);
393 return kernel_sendmsg(sock
, &msg
, NULL
, 0, 0);
396 static int xs_send_pagedata(struct socket
*sock
, struct xdr_buf
*xdr
, unsigned int base
, int more
, bool zerocopy
)
398 ssize_t (*do_sendpage
)(struct socket
*sock
, struct page
*page
,
399 int offset
, size_t size
, int flags
);
401 unsigned int remainder
;
404 remainder
= xdr
->page_len
- base
;
405 base
+= xdr
->page_base
;
406 ppage
= xdr
->pages
+ (base
>> PAGE_SHIFT
);
408 do_sendpage
= sock
->ops
->sendpage
;
410 do_sendpage
= sock_no_sendpage
;
412 unsigned int len
= min_t(unsigned int, PAGE_SIZE
- base
, remainder
);
413 int flags
= XS_SENDMSG_FLAGS
;
416 if (remainder
!= 0 || more
)
418 err
= do_sendpage(sock
, *ppage
, base
, len
, flags
);
419 if (remainder
== 0 || err
!= len
)
433 * xs_sendpages - write pages directly to a socket
434 * @sock: socket to send on
435 * @addr: UDP only -- address of destination
436 * @addrlen: UDP only -- length of destination address
437 * @xdr: buffer containing this request
438 * @base: starting position in the buffer
439 * @zerocopy: true if it is safe to use sendpage()
442 static int xs_sendpages(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
, struct xdr_buf
*xdr
, unsigned int base
, bool zerocopy
)
444 unsigned int remainder
= xdr
->len
- base
;
450 clear_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
);
456 if (base
< xdr
->head
[0].iov_len
|| addr
!= NULL
) {
457 unsigned int len
= xdr
->head
[0].iov_len
- base
;
459 err
= xs_send_kvec(sock
, addr
, addrlen
, &xdr
->head
[0], base
, remainder
!= 0);
460 if (remainder
== 0 || err
!= len
)
465 base
-= xdr
->head
[0].iov_len
;
467 if (base
< xdr
->page_len
) {
468 unsigned int len
= xdr
->page_len
- base
;
470 err
= xs_send_pagedata(sock
, xdr
, base
, remainder
!= 0, zerocopy
);
471 if (remainder
== 0 || err
!= len
)
476 base
-= xdr
->page_len
;
478 if (base
>= xdr
->tail
[0].iov_len
)
480 err
= xs_send_kvec(sock
, NULL
, 0, &xdr
->tail
[0], base
, 0);
489 static void xs_nospace_callback(struct rpc_task
*task
)
491 struct sock_xprt
*transport
= container_of(task
->tk_rqstp
->rq_xprt
, struct sock_xprt
, xprt
);
493 transport
->inet
->sk_write_pending
--;
494 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
498 * xs_nospace - place task on wait queue if transmit was incomplete
499 * @task: task to put to sleep
502 static int xs_nospace(struct rpc_task
*task
)
504 struct rpc_rqst
*req
= task
->tk_rqstp
;
505 struct rpc_xprt
*xprt
= req
->rq_xprt
;
506 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
507 struct sock
*sk
= transport
->inet
;
510 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
511 task
->tk_pid
, req
->rq_slen
- req
->rq_bytes_sent
,
514 /* Protect against races with write_space */
515 spin_lock_bh(&xprt
->transport_lock
);
517 /* Don't race with disconnect */
518 if (xprt_connected(xprt
)) {
519 if (test_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
)) {
521 * Notify TCP that we're limited by the application
524 set_bit(SOCK_NOSPACE
, &transport
->sock
->flags
);
525 sk
->sk_write_pending
++;
526 /* ...and wait for more buffer space */
527 xprt_wait_for_buffer_space(task
, xs_nospace_callback
);
530 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
534 spin_unlock_bh(&xprt
->transport_lock
);
536 /* Race breaker in case memory is freed before above code is called */
537 sk
->sk_write_space(sk
);
542 * Construct a stream transport record marker in @buf.
544 static inline void xs_encode_stream_record_marker(struct xdr_buf
*buf
)
546 u32 reclen
= buf
->len
- sizeof(rpc_fraghdr
);
547 rpc_fraghdr
*base
= buf
->head
[0].iov_base
;
548 *base
= cpu_to_be32(RPC_LAST_STREAM_FRAGMENT
| reclen
);
552 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
553 * @task: RPC task that manages the state of an RPC request
556 * 0: The request has been sent
557 * EAGAIN: The socket was blocked, please call again later to
558 * complete the request
559 * ENOTCONN: Caller needs to invoke connect logic then call again
560 * other: Some other error occured, the request was not sent
562 static int xs_local_send_request(struct rpc_task
*task
)
564 struct rpc_rqst
*req
= task
->tk_rqstp
;
565 struct rpc_xprt
*xprt
= req
->rq_xprt
;
566 struct sock_xprt
*transport
=
567 container_of(xprt
, struct sock_xprt
, xprt
);
568 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
571 xs_encode_stream_record_marker(&req
->rq_snd_buf
);
573 xs_pktdump("packet data:",
574 req
->rq_svec
->iov_base
, req
->rq_svec
->iov_len
);
576 status
= xs_sendpages(transport
->sock
, NULL
, 0,
577 xdr
, req
->rq_bytes_sent
, true);
578 dprintk("RPC: %s(%u) = %d\n",
579 __func__
, xdr
->len
- req
->rq_bytes_sent
, status
);
580 if (likely(status
>= 0)) {
581 req
->rq_bytes_sent
+= status
;
582 req
->rq_xmit_bytes_sent
+= status
;
583 if (likely(req
->rq_bytes_sent
>= req
->rq_slen
)) {
584 req
->rq_bytes_sent
= 0;
592 status
= xs_nospace(task
);
595 dprintk("RPC: sendmsg returned unrecognized error %d\n",
606 * xs_udp_send_request - write an RPC request to a UDP socket
607 * @task: address of RPC task that manages the state of an RPC request
610 * 0: The request has been sent
611 * EAGAIN: The socket was blocked, please call again later to
612 * complete the request
613 * ENOTCONN: Caller needs to invoke connect logic then call again
614 * other: Some other error occurred, the request was not sent
616 static int xs_udp_send_request(struct rpc_task
*task
)
618 struct rpc_rqst
*req
= task
->tk_rqstp
;
619 struct rpc_xprt
*xprt
= req
->rq_xprt
;
620 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
621 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
624 xs_pktdump("packet data:",
625 req
->rq_svec
->iov_base
,
626 req
->rq_svec
->iov_len
);
628 if (!xprt_bound(xprt
))
630 status
= xs_sendpages(transport
->sock
,
633 req
->rq_bytes_sent
, true);
635 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
636 xdr
->len
- req
->rq_bytes_sent
, status
);
639 req
->rq_xmit_bytes_sent
+= status
;
640 if (status
>= req
->rq_slen
)
642 /* Still some bytes left; set up for a retry later. */
649 /* Should we call xs_close() here? */
652 status
= xs_nospace(task
);
655 dprintk("RPC: sendmsg returned unrecognized error %d\n",
660 /* When the server has died, an ICMP port unreachable message
661 * prompts ECONNREFUSED. */
662 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
669 * xs_tcp_shutdown - gracefully shut down a TCP socket
672 * Initiates a graceful shutdown of the TCP socket by calling the
673 * equivalent of shutdown(SHUT_WR);
675 static void xs_tcp_shutdown(struct rpc_xprt
*xprt
)
677 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
678 struct socket
*sock
= transport
->sock
;
681 kernel_sock_shutdown(sock
, SHUT_WR
);
682 trace_rpc_socket_shutdown(xprt
, sock
);
687 * xs_tcp_send_request - write an RPC request to a TCP socket
688 * @task: address of RPC task that manages the state of an RPC request
691 * 0: The request has been sent
692 * EAGAIN: The socket was blocked, please call again later to
693 * complete the request
694 * ENOTCONN: Caller needs to invoke connect logic then call again
695 * other: Some other error occurred, the request was not sent
697 * XXX: In the case of soft timeouts, should we eventually give up
698 * if sendmsg is not able to make progress?
700 static int xs_tcp_send_request(struct rpc_task
*task
)
702 struct rpc_rqst
*req
= task
->tk_rqstp
;
703 struct rpc_xprt
*xprt
= req
->rq_xprt
;
704 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
705 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
706 bool zerocopy
= true;
709 xs_encode_stream_record_marker(&req
->rq_snd_buf
);
711 xs_pktdump("packet data:",
712 req
->rq_svec
->iov_base
,
713 req
->rq_svec
->iov_len
);
714 /* Don't use zero copy if this is a resend. If the RPC call
715 * completes while the socket holds a reference to the pages,
716 * then we may end up resending corrupted data.
718 if (task
->tk_flags
& RPC_TASK_SENT
)
721 /* Continue transmitting the packet/record. We must be careful
722 * to cope with writespace callbacks arriving _after_ we have
723 * called sendmsg(). */
725 status
= xs_sendpages(transport
->sock
,
726 NULL
, 0, xdr
, req
->rq_bytes_sent
,
729 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
730 xdr
->len
- req
->rq_bytes_sent
, status
);
732 if (unlikely(status
< 0))
735 /* If we've sent the entire packet, immediately
736 * reset the count of bytes sent. */
737 req
->rq_bytes_sent
+= status
;
738 req
->rq_xmit_bytes_sent
+= status
;
739 if (likely(req
->rq_bytes_sent
>= req
->rq_slen
)) {
740 req
->rq_bytes_sent
= 0;
753 /* Should we call xs_close() here? */
756 status
= xs_nospace(task
);
759 dprintk("RPC: sendmsg returned unrecognized error %d\n",
762 xs_tcp_shutdown(xprt
);
766 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
773 * xs_tcp_release_xprt - clean up after a tcp transmission
777 * This cleans up if an error causes us to abort the transmission of a request.
778 * In this case, the socket may need to be reset in order to avoid confusing
781 static void xs_tcp_release_xprt(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
783 struct rpc_rqst
*req
;
785 if (task
!= xprt
->snd_task
)
789 req
= task
->tk_rqstp
;
792 if (req
->rq_bytes_sent
== 0)
794 if (req
->rq_bytes_sent
== req
->rq_snd_buf
.len
)
796 set_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
798 xprt_release_xprt(xprt
, task
);
801 static void xs_save_old_callbacks(struct sock_xprt
*transport
, struct sock
*sk
)
803 transport
->old_data_ready
= sk
->sk_data_ready
;
804 transport
->old_state_change
= sk
->sk_state_change
;
805 transport
->old_write_space
= sk
->sk_write_space
;
808 static void xs_restore_old_callbacks(struct sock_xprt
*transport
, struct sock
*sk
)
810 sk
->sk_data_ready
= transport
->old_data_ready
;
811 sk
->sk_state_change
= transport
->old_state_change
;
812 sk
->sk_write_space
= transport
->old_write_space
;
815 static void xs_reset_transport(struct sock_xprt
*transport
)
817 struct socket
*sock
= transport
->sock
;
818 struct sock
*sk
= transport
->inet
;
823 transport
->srcport
= 0;
825 write_lock_bh(&sk
->sk_callback_lock
);
826 transport
->inet
= NULL
;
827 transport
->sock
= NULL
;
829 sk
->sk_user_data
= NULL
;
831 xs_restore_old_callbacks(transport
, sk
);
832 write_unlock_bh(&sk
->sk_callback_lock
);
836 trace_rpc_socket_close(&transport
->xprt
, sock
);
841 * xs_close - close a socket
844 * This is used when all requests are complete; ie, no DRC state remains
845 * on the server we want to save.
847 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
848 * xs_reset_transport() zeroing the socket from underneath a writer.
850 static void xs_close(struct rpc_xprt
*xprt
)
852 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
854 dprintk("RPC: xs_close xprt %p\n", xprt
);
856 cancel_delayed_work_sync(&transport
->connect_worker
);
858 xs_reset_transport(transport
);
859 xprt
->reestablish_timeout
= 0;
861 smp_mb__before_clear_bit();
862 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
863 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
864 clear_bit(XPRT_CLOSING
, &xprt
->state
);
865 smp_mb__after_clear_bit();
866 xprt_disconnect_done(xprt
);
869 static void xs_tcp_close(struct rpc_xprt
*xprt
)
871 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE
, &xprt
->state
))
874 xs_tcp_shutdown(xprt
);
877 static void xs_local_destroy(struct rpc_xprt
*xprt
)
880 xs_free_peer_addresses(xprt
);
882 module_put(THIS_MODULE
);
886 * xs_destroy - prepare to shutdown a transport
887 * @xprt: doomed transport
890 static void xs_destroy(struct rpc_xprt
*xprt
)
892 dprintk("RPC: xs_destroy xprt %p\n", xprt
);
894 xs_local_destroy(xprt
);
897 static inline struct rpc_xprt
*xprt_from_sock(struct sock
*sk
)
899 return (struct rpc_xprt
*) sk
->sk_user_data
;
902 static int xs_local_copy_to_xdr(struct xdr_buf
*xdr
, struct sk_buff
*skb
)
904 struct xdr_skb_reader desc
= {
906 .offset
= sizeof(rpc_fraghdr
),
907 .count
= skb
->len
- sizeof(rpc_fraghdr
),
910 if (xdr_partial_copy_from_skb(xdr
, 0, &desc
, xdr_skb_read_bits
) < 0)
918 * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
919 * @sk: socket with data to read
920 * @len: how much data to read
922 * Currently this assumes we can read the whole reply in a single gulp.
924 static void xs_local_data_ready(struct sock
*sk
, int len
)
926 struct rpc_task
*task
;
927 struct rpc_xprt
*xprt
;
928 struct rpc_rqst
*rovr
;
930 int err
, repsize
, copied
;
934 read_lock_bh(&sk
->sk_callback_lock
);
935 dprintk("RPC: %s...\n", __func__
);
936 xprt
= xprt_from_sock(sk
);
940 skb
= skb_recv_datagram(sk
, 0, 1, &err
);
944 repsize
= skb
->len
- sizeof(rpc_fraghdr
);
946 dprintk("RPC: impossible RPC reply size %d\n", repsize
);
950 /* Copy the XID from the skb... */
951 xp
= skb_header_pointer(skb
, sizeof(rpc_fraghdr
), sizeof(_xid
), &_xid
);
955 /* Look up and lock the request corresponding to the given XID */
956 spin_lock(&xprt
->transport_lock
);
957 rovr
= xprt_lookup_rqst(xprt
, *xp
);
960 task
= rovr
->rq_task
;
962 copied
= rovr
->rq_private_buf
.buflen
;
963 if (copied
> repsize
)
966 if (xs_local_copy_to_xdr(&rovr
->rq_private_buf
, skb
)) {
967 dprintk("RPC: sk_buff copy failed\n");
971 xprt_complete_rqst(task
, copied
);
974 spin_unlock(&xprt
->transport_lock
);
976 skb_free_datagram(sk
, skb
);
978 read_unlock_bh(&sk
->sk_callback_lock
);
982 * xs_udp_data_ready - "data ready" callback for UDP sockets
983 * @sk: socket with data to read
984 * @len: how much data to read
987 static void xs_udp_data_ready(struct sock
*sk
, int len
)
989 struct rpc_task
*task
;
990 struct rpc_xprt
*xprt
;
991 struct rpc_rqst
*rovr
;
993 int err
, repsize
, copied
;
997 read_lock_bh(&sk
->sk_callback_lock
);
998 dprintk("RPC: xs_udp_data_ready...\n");
999 if (!(xprt
= xprt_from_sock(sk
)))
1002 if ((skb
= skb_recv_datagram(sk
, 0, 1, &err
)) == NULL
)
1005 repsize
= skb
->len
- sizeof(struct udphdr
);
1007 dprintk("RPC: impossible RPC reply size %d!\n", repsize
);
1011 /* Copy the XID from the skb... */
1012 xp
= skb_header_pointer(skb
, sizeof(struct udphdr
),
1013 sizeof(_xid
), &_xid
);
1017 /* Look up and lock the request corresponding to the given XID */
1018 spin_lock(&xprt
->transport_lock
);
1019 rovr
= xprt_lookup_rqst(xprt
, *xp
);
1022 task
= rovr
->rq_task
;
1024 if ((copied
= rovr
->rq_private_buf
.buflen
) > repsize
)
1027 /* Suck it into the iovec, verify checksum if not done by hw. */
1028 if (csum_partial_copy_to_xdr(&rovr
->rq_private_buf
, skb
)) {
1029 UDPX_INC_STATS_BH(sk
, UDP_MIB_INERRORS
);
1033 UDPX_INC_STATS_BH(sk
, UDP_MIB_INDATAGRAMS
);
1035 xprt_adjust_cwnd(xprt
, task
, copied
);
1036 xprt_complete_rqst(task
, copied
);
1039 spin_unlock(&xprt
->transport_lock
);
1041 skb_free_datagram(sk
, skb
);
1043 read_unlock_bh(&sk
->sk_callback_lock
);
1047 * Helper function to force a TCP close if the server is sending
1048 * junk and/or it has put us in CLOSE_WAIT
1050 static void xs_tcp_force_close(struct rpc_xprt
*xprt
)
1052 set_bit(XPRT_CONNECTION_CLOSE
, &xprt
->state
);
1053 xprt_force_disconnect(xprt
);
1056 static inline void xs_tcp_read_fraghdr(struct rpc_xprt
*xprt
, struct xdr_skb_reader
*desc
)
1058 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1062 p
= ((char *) &transport
->tcp_fraghdr
) + transport
->tcp_offset
;
1063 len
= sizeof(transport
->tcp_fraghdr
) - transport
->tcp_offset
;
1064 used
= xdr_skb_read_bits(desc
, p
, len
);
1065 transport
->tcp_offset
+= used
;
1069 transport
->tcp_reclen
= ntohl(transport
->tcp_fraghdr
);
1070 if (transport
->tcp_reclen
& RPC_LAST_STREAM_FRAGMENT
)
1071 transport
->tcp_flags
|= TCP_RCV_LAST_FRAG
;
1073 transport
->tcp_flags
&= ~TCP_RCV_LAST_FRAG
;
1074 transport
->tcp_reclen
&= RPC_FRAGMENT_SIZE_MASK
;
1076 transport
->tcp_flags
&= ~TCP_RCV_COPY_FRAGHDR
;
1077 transport
->tcp_offset
= 0;
1079 /* Sanity check of the record length */
1080 if (unlikely(transport
->tcp_reclen
< 8)) {
1081 dprintk("RPC: invalid TCP record fragment length\n");
1082 xs_tcp_force_close(xprt
);
1085 dprintk("RPC: reading TCP record fragment of length %d\n",
1086 transport
->tcp_reclen
);
1089 static void xs_tcp_check_fraghdr(struct sock_xprt
*transport
)
1091 if (transport
->tcp_offset
== transport
->tcp_reclen
) {
1092 transport
->tcp_flags
|= TCP_RCV_COPY_FRAGHDR
;
1093 transport
->tcp_offset
= 0;
1094 if (transport
->tcp_flags
& TCP_RCV_LAST_FRAG
) {
1095 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1096 transport
->tcp_flags
|= TCP_RCV_COPY_XID
;
1097 transport
->tcp_copied
= 0;
1102 static inline void xs_tcp_read_xid(struct sock_xprt
*transport
, struct xdr_skb_reader
*desc
)
1107 len
= sizeof(transport
->tcp_xid
) - transport
->tcp_offset
;
1108 dprintk("RPC: reading XID (%Zu bytes)\n", len
);
1109 p
= ((char *) &transport
->tcp_xid
) + transport
->tcp_offset
;
1110 used
= xdr_skb_read_bits(desc
, p
, len
);
1111 transport
->tcp_offset
+= used
;
1114 transport
->tcp_flags
&= ~TCP_RCV_COPY_XID
;
1115 transport
->tcp_flags
|= TCP_RCV_READ_CALLDIR
;
1116 transport
->tcp_copied
= 4;
1117 dprintk("RPC: reading %s XID %08x\n",
1118 (transport
->tcp_flags
& TCP_RPC_REPLY
) ? "reply for"
1120 ntohl(transport
->tcp_xid
));
1121 xs_tcp_check_fraghdr(transport
);
1124 static inline void xs_tcp_read_calldir(struct sock_xprt
*transport
,
1125 struct xdr_skb_reader
*desc
)
1132 * We want transport->tcp_offset to be 8 at the end of this routine
1133 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1134 * When this function is called for the first time,
1135 * transport->tcp_offset is 4 (after having already read the xid).
1137 offset
= transport
->tcp_offset
- sizeof(transport
->tcp_xid
);
1138 len
= sizeof(transport
->tcp_calldir
) - offset
;
1139 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len
);
1140 p
= ((char *) &transport
->tcp_calldir
) + offset
;
1141 used
= xdr_skb_read_bits(desc
, p
, len
);
1142 transport
->tcp_offset
+= used
;
1145 transport
->tcp_flags
&= ~TCP_RCV_READ_CALLDIR
;
1147 * We don't yet have the XDR buffer, so we will write the calldir
1148 * out after we get the buffer from the 'struct rpc_rqst'
1150 switch (ntohl(transport
->tcp_calldir
)) {
1152 transport
->tcp_flags
|= TCP_RCV_COPY_CALLDIR
;
1153 transport
->tcp_flags
|= TCP_RCV_COPY_DATA
;
1154 transport
->tcp_flags
|= TCP_RPC_REPLY
;
1157 transport
->tcp_flags
|= TCP_RCV_COPY_CALLDIR
;
1158 transport
->tcp_flags
|= TCP_RCV_COPY_DATA
;
1159 transport
->tcp_flags
&= ~TCP_RPC_REPLY
;
1162 dprintk("RPC: invalid request message type\n");
1163 xs_tcp_force_close(&transport
->xprt
);
1165 xs_tcp_check_fraghdr(transport
);
1168 static inline void xs_tcp_read_common(struct rpc_xprt
*xprt
,
1169 struct xdr_skb_reader
*desc
,
1170 struct rpc_rqst
*req
)
1172 struct sock_xprt
*transport
=
1173 container_of(xprt
, struct sock_xprt
, xprt
);
1174 struct xdr_buf
*rcvbuf
;
1178 rcvbuf
= &req
->rq_private_buf
;
1180 if (transport
->tcp_flags
& TCP_RCV_COPY_CALLDIR
) {
1182 * Save the RPC direction in the XDR buffer
1184 memcpy(rcvbuf
->head
[0].iov_base
+ transport
->tcp_copied
,
1185 &transport
->tcp_calldir
,
1186 sizeof(transport
->tcp_calldir
));
1187 transport
->tcp_copied
+= sizeof(transport
->tcp_calldir
);
1188 transport
->tcp_flags
&= ~TCP_RCV_COPY_CALLDIR
;
1192 if (len
> transport
->tcp_reclen
- transport
->tcp_offset
) {
1193 struct xdr_skb_reader my_desc
;
1195 len
= transport
->tcp_reclen
- transport
->tcp_offset
;
1196 memcpy(&my_desc
, desc
, sizeof(my_desc
));
1197 my_desc
.count
= len
;
1198 r
= xdr_partial_copy_from_skb(rcvbuf
, transport
->tcp_copied
,
1199 &my_desc
, xdr_skb_read_bits
);
1203 r
= xdr_partial_copy_from_skb(rcvbuf
, transport
->tcp_copied
,
1204 desc
, xdr_skb_read_bits
);
1207 transport
->tcp_copied
+= r
;
1208 transport
->tcp_offset
+= r
;
1211 /* Error when copying to the receive buffer,
1212 * usually because we weren't able to allocate
1213 * additional buffer pages. All we can do now
1214 * is turn off TCP_RCV_COPY_DATA, so the request
1215 * will not receive any additional updates,
1217 * Any remaining data from this record will
1220 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1221 dprintk("RPC: XID %08x truncated request\n",
1222 ntohl(transport
->tcp_xid
));
1223 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1224 "tcp_offset = %u, tcp_reclen = %u\n",
1225 xprt
, transport
->tcp_copied
,
1226 transport
->tcp_offset
, transport
->tcp_reclen
);
1230 dprintk("RPC: XID %08x read %Zd bytes\n",
1231 ntohl(transport
->tcp_xid
), r
);
1232 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1233 "tcp_reclen = %u\n", xprt
, transport
->tcp_copied
,
1234 transport
->tcp_offset
, transport
->tcp_reclen
);
1236 if (transport
->tcp_copied
== req
->rq_private_buf
.buflen
)
1237 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1238 else if (transport
->tcp_offset
== transport
->tcp_reclen
) {
1239 if (transport
->tcp_flags
& TCP_RCV_LAST_FRAG
)
1240 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1245 * Finds the request corresponding to the RPC xid and invokes the common
1246 * tcp read code to read the data.
1248 static inline int xs_tcp_read_reply(struct rpc_xprt
*xprt
,
1249 struct xdr_skb_reader
*desc
)
1251 struct sock_xprt
*transport
=
1252 container_of(xprt
, struct sock_xprt
, xprt
);
1253 struct rpc_rqst
*req
;
1255 dprintk("RPC: read reply XID %08x\n", ntohl(transport
->tcp_xid
));
1257 /* Find and lock the request corresponding to this xid */
1258 spin_lock(&xprt
->transport_lock
);
1259 req
= xprt_lookup_rqst(xprt
, transport
->tcp_xid
);
1261 dprintk("RPC: XID %08x request not found!\n",
1262 ntohl(transport
->tcp_xid
));
1263 spin_unlock(&xprt
->transport_lock
);
1267 xs_tcp_read_common(xprt
, desc
, req
);
1269 if (!(transport
->tcp_flags
& TCP_RCV_COPY_DATA
))
1270 xprt_complete_rqst(req
->rq_task
, transport
->tcp_copied
);
1272 spin_unlock(&xprt
->transport_lock
);
1276 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1278 * Obtains an rpc_rqst previously allocated and invokes the common
1279 * tcp read code to read the data. The result is placed in the callback
1281 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1282 * connection and return -1.
1284 static inline int xs_tcp_read_callback(struct rpc_xprt
*xprt
,
1285 struct xdr_skb_reader
*desc
)
1287 struct sock_xprt
*transport
=
1288 container_of(xprt
, struct sock_xprt
, xprt
);
1289 struct rpc_rqst
*req
;
1291 req
= xprt_alloc_bc_request(xprt
);
1293 printk(KERN_WARNING
"Callback slot table overflowed\n");
1294 xprt_force_disconnect(xprt
);
1298 req
->rq_xid
= transport
->tcp_xid
;
1299 dprintk("RPC: read callback XID %08x\n", ntohl(req
->rq_xid
));
1300 xs_tcp_read_common(xprt
, desc
, req
);
1302 if (!(transport
->tcp_flags
& TCP_RCV_COPY_DATA
)) {
1303 struct svc_serv
*bc_serv
= xprt
->bc_serv
;
1306 * Add callback request to callback list. The callback
1307 * service sleeps on the sv_cb_waitq waiting for new
1308 * requests. Wake it up after adding enqueing the
1311 dprintk("RPC: add callback request to list\n");
1312 spin_lock(&bc_serv
->sv_cb_lock
);
1313 list_add(&req
->rq_bc_list
, &bc_serv
->sv_cb_list
);
1314 spin_unlock(&bc_serv
->sv_cb_lock
);
1315 wake_up(&bc_serv
->sv_cb_waitq
);
1318 req
->rq_private_buf
.len
= transport
->tcp_copied
;
1323 static inline int _xs_tcp_read_data(struct rpc_xprt
*xprt
,
1324 struct xdr_skb_reader
*desc
)
1326 struct sock_xprt
*transport
=
1327 container_of(xprt
, struct sock_xprt
, xprt
);
1329 return (transport
->tcp_flags
& TCP_RPC_REPLY
) ?
1330 xs_tcp_read_reply(xprt
, desc
) :
1331 xs_tcp_read_callback(xprt
, desc
);
1334 static inline int _xs_tcp_read_data(struct rpc_xprt
*xprt
,
1335 struct xdr_skb_reader
*desc
)
1337 return xs_tcp_read_reply(xprt
, desc
);
1339 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1342 * Read data off the transport. This can be either an RPC_CALL or an
1343 * RPC_REPLY. Relay the processing to helper functions.
1345 static void xs_tcp_read_data(struct rpc_xprt
*xprt
,
1346 struct xdr_skb_reader
*desc
)
1348 struct sock_xprt
*transport
=
1349 container_of(xprt
, struct sock_xprt
, xprt
);
1351 if (_xs_tcp_read_data(xprt
, desc
) == 0)
1352 xs_tcp_check_fraghdr(transport
);
1355 * The transport_lock protects the request handling.
1356 * There's no need to hold it to update the tcp_flags.
1358 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1362 static inline void xs_tcp_read_discard(struct sock_xprt
*transport
, struct xdr_skb_reader
*desc
)
1366 len
= transport
->tcp_reclen
- transport
->tcp_offset
;
1367 if (len
> desc
->count
)
1370 desc
->offset
+= len
;
1371 transport
->tcp_offset
+= len
;
1372 dprintk("RPC: discarded %Zu bytes\n", len
);
1373 xs_tcp_check_fraghdr(transport
);
1376 static int xs_tcp_data_recv(read_descriptor_t
*rd_desc
, struct sk_buff
*skb
, unsigned int offset
, size_t len
)
1378 struct rpc_xprt
*xprt
= rd_desc
->arg
.data
;
1379 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1380 struct xdr_skb_reader desc
= {
1386 dprintk("RPC: xs_tcp_data_recv started\n");
1388 /* Read in a new fragment marker if necessary */
1389 /* Can we ever really expect to get completely empty fragments? */
1390 if (transport
->tcp_flags
& TCP_RCV_COPY_FRAGHDR
) {
1391 xs_tcp_read_fraghdr(xprt
, &desc
);
1394 /* Read in the xid if necessary */
1395 if (transport
->tcp_flags
& TCP_RCV_COPY_XID
) {
1396 xs_tcp_read_xid(transport
, &desc
);
1399 /* Read in the call/reply flag */
1400 if (transport
->tcp_flags
& TCP_RCV_READ_CALLDIR
) {
1401 xs_tcp_read_calldir(transport
, &desc
);
1404 /* Read in the request data */
1405 if (transport
->tcp_flags
& TCP_RCV_COPY_DATA
) {
1406 xs_tcp_read_data(xprt
, &desc
);
1409 /* Skip over any trailing bytes on short reads */
1410 xs_tcp_read_discard(transport
, &desc
);
1411 } while (desc
.count
);
1412 dprintk("RPC: xs_tcp_data_recv done\n");
1413 return len
- desc
.count
;
1417 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1418 * @sk: socket with data to read
1419 * @bytes: how much data to read
1422 static void xs_tcp_data_ready(struct sock
*sk
, int bytes
)
1424 struct rpc_xprt
*xprt
;
1425 read_descriptor_t rd_desc
;
1428 dprintk("RPC: xs_tcp_data_ready...\n");
1430 read_lock_bh(&sk
->sk_callback_lock
);
1431 if (!(xprt
= xprt_from_sock(sk
)))
1433 /* Any data means we had a useful conversation, so
1434 * the we don't need to delay the next reconnect
1436 if (xprt
->reestablish_timeout
)
1437 xprt
->reestablish_timeout
= 0;
1439 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1440 rd_desc
.arg
.data
= xprt
;
1442 rd_desc
.count
= 65536;
1443 read
= tcp_read_sock(sk
, &rd_desc
, xs_tcp_data_recv
);
1446 read_unlock_bh(&sk
->sk_callback_lock
);
1450 * Do the equivalent of linger/linger2 handling for dealing with
1451 * broken servers that don't close the socket in a timely
1454 static void xs_tcp_schedule_linger_timeout(struct rpc_xprt
*xprt
,
1455 unsigned long timeout
)
1457 struct sock_xprt
*transport
;
1459 if (xprt_test_and_set_connecting(xprt
))
1461 set_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1462 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1463 queue_delayed_work(rpciod_workqueue
, &transport
->connect_worker
,
1467 static void xs_tcp_cancel_linger_timeout(struct rpc_xprt
*xprt
)
1469 struct sock_xprt
*transport
;
1471 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1473 if (!test_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
) ||
1474 !cancel_delayed_work(&transport
->connect_worker
))
1476 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1477 xprt_clear_connecting(xprt
);
1480 static void xs_sock_reset_connection_flags(struct rpc_xprt
*xprt
)
1482 smp_mb__before_clear_bit();
1483 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1484 clear_bit(XPRT_CONNECTION_CLOSE
, &xprt
->state
);
1485 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
1486 clear_bit(XPRT_CLOSING
, &xprt
->state
);
1487 smp_mb__after_clear_bit();
1490 static void xs_sock_mark_closed(struct rpc_xprt
*xprt
)
1492 xs_sock_reset_connection_flags(xprt
);
1493 /* Mark transport as closed and wake up all pending tasks */
1494 xprt_disconnect_done(xprt
);
1498 * xs_tcp_state_change - callback to handle TCP socket state changes
1499 * @sk: socket whose state has changed
1502 static void xs_tcp_state_change(struct sock
*sk
)
1504 struct rpc_xprt
*xprt
;
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 trace_rpc_socket_state_change(xprt
, sk
->sk_socket
);
1517 switch (sk
->sk_state
) {
1518 case TCP_ESTABLISHED
:
1519 spin_lock(&xprt
->transport_lock
);
1520 if (!xprt_test_and_set_connected(xprt
)) {
1521 struct sock_xprt
*transport
= container_of(xprt
,
1522 struct sock_xprt
, xprt
);
1524 /* Reset TCP record info */
1525 transport
->tcp_offset
= 0;
1526 transport
->tcp_reclen
= 0;
1527 transport
->tcp_copied
= 0;
1528 transport
->tcp_flags
=
1529 TCP_RCV_COPY_FRAGHDR
| TCP_RCV_COPY_XID
;
1531 xprt_wake_pending_tasks(xprt
, -EAGAIN
);
1533 spin_unlock(&xprt
->transport_lock
);
1536 /* The client initiated a shutdown of the socket */
1537 xprt
->connect_cookie
++;
1538 xprt
->reestablish_timeout
= 0;
1539 set_bit(XPRT_CLOSING
, &xprt
->state
);
1540 smp_mb__before_clear_bit();
1541 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1542 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
1543 smp_mb__after_clear_bit();
1544 xs_tcp_schedule_linger_timeout(xprt
, xs_tcp_fin_timeout
);
1546 case TCP_CLOSE_WAIT
:
1547 /* The server initiated a shutdown of the socket */
1548 xprt
->connect_cookie
++;
1549 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1550 xs_tcp_force_close(xprt
);
1553 * If the server closed down the connection, make sure that
1554 * we back off before reconnecting
1556 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
1557 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
1560 set_bit(XPRT_CLOSING
, &xprt
->state
);
1561 xs_tcp_schedule_linger_timeout(xprt
, xs_tcp_fin_timeout
);
1562 smp_mb__before_clear_bit();
1563 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1564 smp_mb__after_clear_bit();
1567 xs_tcp_cancel_linger_timeout(xprt
);
1568 xs_sock_mark_closed(xprt
);
1571 read_unlock_bh(&sk
->sk_callback_lock
);
1574 static void xs_write_space(struct sock
*sk
)
1576 struct socket
*sock
;
1577 struct rpc_xprt
*xprt
;
1579 if (unlikely(!(sock
= sk
->sk_socket
)))
1581 clear_bit(SOCK_NOSPACE
, &sock
->flags
);
1583 if (unlikely(!(xprt
= xprt_from_sock(sk
))))
1585 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
) == 0)
1588 xprt_write_space(xprt
);
1592 * xs_udp_write_space - callback invoked when socket buffer space
1594 * @sk: socket whose state has changed
1596 * Called when more output buffer space is available for this socket.
1597 * We try not to wake our writers until they can make "significant"
1598 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1599 * with a bunch of small requests.
1601 static void xs_udp_write_space(struct sock
*sk
)
1603 read_lock_bh(&sk
->sk_callback_lock
);
1605 /* from net/core/sock.c:sock_def_write_space */
1606 if (sock_writeable(sk
))
1609 read_unlock_bh(&sk
->sk_callback_lock
);
1613 * xs_tcp_write_space - callback invoked when socket buffer space
1615 * @sk: socket whose state has changed
1617 * Called when more output buffer space is available for this socket.
1618 * We try not to wake our writers until they can make "significant"
1619 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1620 * with a bunch of small requests.
1622 static void xs_tcp_write_space(struct sock
*sk
)
1624 read_lock_bh(&sk
->sk_callback_lock
);
1626 /* from net/core/stream.c:sk_stream_write_space */
1627 if (sk_stream_is_writeable(sk
))
1630 read_unlock_bh(&sk
->sk_callback_lock
);
1633 static void xs_udp_do_set_buffer_size(struct rpc_xprt
*xprt
)
1635 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1636 struct sock
*sk
= transport
->inet
;
1638 if (transport
->rcvsize
) {
1639 sk
->sk_userlocks
|= SOCK_RCVBUF_LOCK
;
1640 sk
->sk_rcvbuf
= transport
->rcvsize
* xprt
->max_reqs
* 2;
1642 if (transport
->sndsize
) {
1643 sk
->sk_userlocks
|= SOCK_SNDBUF_LOCK
;
1644 sk
->sk_sndbuf
= transport
->sndsize
* xprt
->max_reqs
* 2;
1645 sk
->sk_write_space(sk
);
1650 * xs_udp_set_buffer_size - set send and receive limits
1651 * @xprt: generic transport
1652 * @sndsize: requested size of send buffer, in bytes
1653 * @rcvsize: requested size of receive buffer, in bytes
1655 * Set socket send and receive buffer size limits.
1657 static void xs_udp_set_buffer_size(struct rpc_xprt
*xprt
, size_t sndsize
, size_t rcvsize
)
1659 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1661 transport
->sndsize
= 0;
1663 transport
->sndsize
= sndsize
+ 1024;
1664 transport
->rcvsize
= 0;
1666 transport
->rcvsize
= rcvsize
+ 1024;
1668 xs_udp_do_set_buffer_size(xprt
);
1672 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1673 * @task: task that timed out
1675 * Adjust the congestion window after a retransmit timeout has occurred.
1677 static void xs_udp_timer(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
1679 xprt_adjust_cwnd(xprt
, task
, -ETIMEDOUT
);
1682 static unsigned short xs_get_random_port(void)
1684 unsigned short range
= xprt_max_resvport
- xprt_min_resvport
;
1685 unsigned short rand
= (unsigned short) net_random() % range
;
1686 return rand
+ xprt_min_resvport
;
1690 * xs_set_port - reset the port number in the remote endpoint address
1691 * @xprt: generic transport
1692 * @port: new port number
1695 static void xs_set_port(struct rpc_xprt
*xprt
, unsigned short port
)
1697 dprintk("RPC: setting port for xprt %p to %u\n", xprt
, port
);
1699 rpc_set_port(xs_addr(xprt
), port
);
1700 xs_update_peer_port(xprt
);
1703 static unsigned short xs_get_srcport(struct sock_xprt
*transport
)
1705 unsigned short port
= transport
->srcport
;
1707 if (port
== 0 && transport
->xprt
.resvport
)
1708 port
= xs_get_random_port();
1712 static unsigned short xs_next_srcport(struct sock_xprt
*transport
, unsigned short port
)
1714 if (transport
->srcport
!= 0)
1715 transport
->srcport
= 0;
1716 if (!transport
->xprt
.resvport
)
1718 if (port
<= xprt_min_resvport
|| port
> xprt_max_resvport
)
1719 return xprt_max_resvport
;
1722 static int xs_bind(struct sock_xprt
*transport
, struct socket
*sock
)
1724 struct sockaddr_storage myaddr
;
1726 unsigned short port
= xs_get_srcport(transport
);
1727 unsigned short last
;
1729 memcpy(&myaddr
, &transport
->srcaddr
, transport
->xprt
.addrlen
);
1731 rpc_set_port((struct sockaddr
*)&myaddr
, port
);
1732 err
= kernel_bind(sock
, (struct sockaddr
*)&myaddr
,
1733 transport
->xprt
.addrlen
);
1737 transport
->srcport
= port
;
1741 port
= xs_next_srcport(transport
, port
);
1744 } while (err
== -EADDRINUSE
&& nloop
!= 2);
1746 if (myaddr
.ss_family
== AF_INET
)
1747 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__
,
1748 &((struct sockaddr_in
*)&myaddr
)->sin_addr
,
1749 port
, err
? "failed" : "ok", err
);
1751 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__
,
1752 &((struct sockaddr_in6
*)&myaddr
)->sin6_addr
,
1753 port
, err
? "failed" : "ok", err
);
1758 * We don't support autobind on AF_LOCAL sockets
1760 static void xs_local_rpcbind(struct rpc_task
*task
)
1763 xprt_set_bound(rcu_dereference(task
->tk_client
->cl_xprt
));
1767 static void xs_local_set_port(struct rpc_xprt
*xprt
, unsigned short port
)
1771 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1772 static struct lock_class_key xs_key
[2];
1773 static struct lock_class_key xs_slock_key
[2];
1775 static inline void xs_reclassify_socketu(struct socket
*sock
)
1777 struct sock
*sk
= sock
->sk
;
1779 sock_lock_init_class_and_name(sk
, "slock-AF_LOCAL-RPC",
1780 &xs_slock_key
[1], "sk_lock-AF_LOCAL-RPC", &xs_key
[1]);
1783 static inline void xs_reclassify_socket4(struct socket
*sock
)
1785 struct sock
*sk
= sock
->sk
;
1787 sock_lock_init_class_and_name(sk
, "slock-AF_INET-RPC",
1788 &xs_slock_key
[0], "sk_lock-AF_INET-RPC", &xs_key
[0]);
1791 static inline void xs_reclassify_socket6(struct socket
*sock
)
1793 struct sock
*sk
= sock
->sk
;
1795 sock_lock_init_class_and_name(sk
, "slock-AF_INET6-RPC",
1796 &xs_slock_key
[1], "sk_lock-AF_INET6-RPC", &xs_key
[1]);
1799 static inline void xs_reclassify_socket(int family
, struct socket
*sock
)
1801 WARN_ON_ONCE(sock_owned_by_user(sock
->sk
));
1802 if (sock_owned_by_user(sock
->sk
))
1807 xs_reclassify_socketu(sock
);
1810 xs_reclassify_socket4(sock
);
1813 xs_reclassify_socket6(sock
);
1818 static inline void xs_reclassify_socketu(struct socket
*sock
)
1822 static inline void xs_reclassify_socket4(struct socket
*sock
)
1826 static inline void xs_reclassify_socket6(struct socket
*sock
)
1830 static inline void xs_reclassify_socket(int family
, struct socket
*sock
)
1835 static void xs_dummy_setup_socket(struct work_struct
*work
)
1839 static struct socket
*xs_create_sock(struct rpc_xprt
*xprt
,
1840 struct sock_xprt
*transport
, int family
, int type
, int protocol
)
1842 struct socket
*sock
;
1845 err
= __sock_create(xprt
->xprt_net
, family
, type
, protocol
, &sock
, 1);
1847 dprintk("RPC: can't create %d transport socket (%d).\n",
1851 xs_reclassify_socket(family
, sock
);
1853 err
= xs_bind(transport
, sock
);
1861 return ERR_PTR(err
);
1864 static int xs_local_finish_connecting(struct rpc_xprt
*xprt
,
1865 struct socket
*sock
)
1867 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
,
1870 if (!transport
->inet
) {
1871 struct sock
*sk
= sock
->sk
;
1873 write_lock_bh(&sk
->sk_callback_lock
);
1875 xs_save_old_callbacks(transport
, sk
);
1877 sk
->sk_user_data
= xprt
;
1878 sk
->sk_data_ready
= xs_local_data_ready
;
1879 sk
->sk_write_space
= xs_udp_write_space
;
1880 sk
->sk_allocation
= GFP_ATOMIC
;
1882 xprt_clear_connected(xprt
);
1884 /* Reset to new socket */
1885 transport
->sock
= sock
;
1886 transport
->inet
= sk
;
1888 write_unlock_bh(&sk
->sk_callback_lock
);
1891 /* Tell the socket layer to start connecting... */
1892 xprt
->stat
.connect_count
++;
1893 xprt
->stat
.connect_start
= jiffies
;
1894 return kernel_connect(sock
, xs_addr(xprt
), xprt
->addrlen
, 0);
1898 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1899 * @xprt: RPC transport to connect
1900 * @transport: socket transport to connect
1901 * @create_sock: function to create a socket of the correct type
1903 static int xs_local_setup_socket(struct sock_xprt
*transport
)
1905 struct rpc_xprt
*xprt
= &transport
->xprt
;
1906 struct socket
*sock
;
1909 current
->flags
|= PF_FSTRANS
;
1911 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1912 status
= __sock_create(xprt
->xprt_net
, AF_LOCAL
,
1913 SOCK_STREAM
, 0, &sock
, 1);
1915 dprintk("RPC: can't create AF_LOCAL "
1916 "transport socket (%d).\n", -status
);
1919 xs_reclassify_socketu(sock
);
1921 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1922 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
1924 status
= xs_local_finish_connecting(xprt
, sock
);
1925 trace_rpc_socket_connect(xprt
, sock
, status
);
1928 dprintk("RPC: xprt %p connected to %s\n",
1929 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
1930 xprt_set_connected(xprt
);
1933 dprintk("RPC: xprt %p: socket %s does not exist\n",
1934 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
1937 dprintk("RPC: xprt %p: connection refused for %s\n",
1938 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
1941 printk(KERN_ERR
"%s: unhandled error (%d) connecting to %s\n",
1943 xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
1947 xprt_clear_connecting(xprt
);
1948 xprt_wake_pending_tasks(xprt
, status
);
1949 current
->flags
&= ~PF_FSTRANS
;
1953 static void xs_local_connect(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
1955 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1958 if (RPC_IS_ASYNC(task
)) {
1960 * We want the AF_LOCAL connect to be resolved in the
1961 * filesystem namespace of the process making the rpc
1962 * call. Thus we connect synchronously.
1964 * If we want to support asynchronous AF_LOCAL calls,
1965 * we'll need to figure out how to pass a namespace to
1968 rpc_exit(task
, -ENOTCONN
);
1971 ret
= xs_local_setup_socket(transport
);
1972 if (ret
&& !RPC_IS_SOFTCONN(task
))
1973 msleep_interruptible(15000);
1976 #ifdef CONFIG_SUNRPC_SWAP
1977 static void xs_set_memalloc(struct rpc_xprt
*xprt
)
1979 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
,
1983 sk_set_memalloc(transport
->inet
);
1987 * xs_swapper - Tag this transport as being used for swap.
1988 * @xprt: transport to tag
1989 * @enable: enable/disable
1992 int xs_swapper(struct rpc_xprt
*xprt
, int enable
)
1994 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
,
2000 xs_set_memalloc(xprt
);
2001 } else if (xprt
->swapper
) {
2003 sk_clear_memalloc(transport
->inet
);
2008 EXPORT_SYMBOL_GPL(xs_swapper
);
2010 static void xs_set_memalloc(struct rpc_xprt
*xprt
)
2015 static void xs_udp_finish_connecting(struct rpc_xprt
*xprt
, struct socket
*sock
)
2017 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2019 if (!transport
->inet
) {
2020 struct sock
*sk
= sock
->sk
;
2022 write_lock_bh(&sk
->sk_callback_lock
);
2024 xs_save_old_callbacks(transport
, sk
);
2026 sk
->sk_user_data
= xprt
;
2027 sk
->sk_data_ready
= xs_udp_data_ready
;
2028 sk
->sk_write_space
= xs_udp_write_space
;
2029 sk
->sk_no_check
= UDP_CSUM_NORCV
;
2030 sk
->sk_allocation
= GFP_ATOMIC
;
2032 xprt_set_connected(xprt
);
2034 /* Reset to new socket */
2035 transport
->sock
= sock
;
2036 transport
->inet
= sk
;
2038 xs_set_memalloc(xprt
);
2040 write_unlock_bh(&sk
->sk_callback_lock
);
2042 xs_udp_do_set_buffer_size(xprt
);
2045 static void xs_udp_setup_socket(struct work_struct
*work
)
2047 struct sock_xprt
*transport
=
2048 container_of(work
, struct sock_xprt
, connect_worker
.work
);
2049 struct rpc_xprt
*xprt
= &transport
->xprt
;
2050 struct socket
*sock
= transport
->sock
;
2053 current
->flags
|= PF_FSTRANS
;
2055 /* Start by resetting any existing state */
2056 xs_reset_transport(transport
);
2057 sock
= xs_create_sock(xprt
, transport
,
2058 xs_addr(xprt
)->sa_family
, SOCK_DGRAM
, IPPROTO_UDP
);
2062 dprintk("RPC: worker connecting xprt %p via %s to "
2063 "%s (port %s)\n", xprt
,
2064 xprt
->address_strings
[RPC_DISPLAY_PROTO
],
2065 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2066 xprt
->address_strings
[RPC_DISPLAY_PORT
]);
2068 xs_udp_finish_connecting(xprt
, sock
);
2069 trace_rpc_socket_connect(xprt
, sock
, 0);
2072 xprt_clear_connecting(xprt
);
2073 xprt_wake_pending_tasks(xprt
, status
);
2074 current
->flags
&= ~PF_FSTRANS
;
2078 * We need to preserve the port number so the reply cache on the server can
2079 * find our cached RPC replies when we get around to reconnecting.
2081 static void xs_abort_connection(struct sock_xprt
*transport
)
2084 struct sockaddr any
;
2086 dprintk("RPC: disconnecting xprt %p to reuse port\n", transport
);
2089 * Disconnect the transport socket by doing a connect operation
2090 * with AF_UNSPEC. This should return immediately...
2092 memset(&any
, 0, sizeof(any
));
2093 any
.sa_family
= AF_UNSPEC
;
2094 result
= kernel_connect(transport
->sock
, &any
, sizeof(any
), 0);
2095 trace_rpc_socket_reset_connection(&transport
->xprt
,
2096 transport
->sock
, result
);
2098 xs_sock_reset_connection_flags(&transport
->xprt
);
2099 dprintk("RPC: AF_UNSPEC connect return code %d\n", result
);
2102 static void xs_tcp_reuse_connection(struct sock_xprt
*transport
)
2104 unsigned int state
= transport
->inet
->sk_state
;
2106 if (state
== TCP_CLOSE
&& transport
->sock
->state
== SS_UNCONNECTED
) {
2107 /* we don't need to abort the connection if the socket
2108 * hasn't undergone a shutdown
2110 if (transport
->inet
->sk_shutdown
== 0)
2112 dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n",
2113 __func__
, transport
->inet
->sk_shutdown
);
2115 if ((1 << state
) & (TCPF_ESTABLISHED
|TCPF_SYN_SENT
)) {
2116 /* we don't need to abort the connection if the socket
2117 * hasn't undergone a shutdown
2119 if (transport
->inet
->sk_shutdown
== 0)
2121 dprintk("RPC: %s: ESTABLISHED/SYN_SENT "
2122 "sk_shutdown set to %d\n",
2123 __func__
, transport
->inet
->sk_shutdown
);
2125 xs_abort_connection(transport
);
2128 static int xs_tcp_finish_connecting(struct rpc_xprt
*xprt
, struct socket
*sock
)
2130 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2131 int ret
= -ENOTCONN
;
2133 if (!transport
->inet
) {
2134 struct sock
*sk
= sock
->sk
;
2136 write_lock_bh(&sk
->sk_callback_lock
);
2138 xs_save_old_callbacks(transport
, sk
);
2140 sk
->sk_user_data
= xprt
;
2141 sk
->sk_data_ready
= xs_tcp_data_ready
;
2142 sk
->sk_state_change
= xs_tcp_state_change
;
2143 sk
->sk_write_space
= xs_tcp_write_space
;
2144 sk
->sk_allocation
= GFP_ATOMIC
;
2146 /* socket options */
2147 sk
->sk_userlocks
|= SOCK_BINDPORT_LOCK
;
2148 sock_reset_flag(sk
, SOCK_LINGER
);
2149 tcp_sk(sk
)->linger2
= 0;
2150 tcp_sk(sk
)->nonagle
|= TCP_NAGLE_OFF
;
2152 xprt_clear_connected(xprt
);
2154 /* Reset to new socket */
2155 transport
->sock
= sock
;
2156 transport
->inet
= sk
;
2158 write_unlock_bh(&sk
->sk_callback_lock
);
2161 if (!xprt_bound(xprt
))
2164 xs_set_memalloc(xprt
);
2166 /* Tell the socket layer to start connecting... */
2167 xprt
->stat
.connect_count
++;
2168 xprt
->stat
.connect_start
= jiffies
;
2169 ret
= kernel_connect(sock
, xs_addr(xprt
), xprt
->addrlen
, O_NONBLOCK
);
2174 xprt
->connect_cookie
++;
2175 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
2176 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2183 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2184 * @xprt: RPC transport to connect
2185 * @transport: socket transport to connect
2186 * @create_sock: function to create a socket of the correct type
2188 * Invoked by a work queue tasklet.
2190 static void xs_tcp_setup_socket(struct work_struct
*work
)
2192 struct sock_xprt
*transport
=
2193 container_of(work
, struct sock_xprt
, connect_worker
.work
);
2194 struct socket
*sock
= transport
->sock
;
2195 struct rpc_xprt
*xprt
= &transport
->xprt
;
2198 current
->flags
|= PF_FSTRANS
;
2201 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
2202 sock
= xs_create_sock(xprt
, transport
,
2203 xs_addr(xprt
)->sa_family
, SOCK_STREAM
, IPPROTO_TCP
);
2205 status
= PTR_ERR(sock
);
2211 abort_and_exit
= test_and_clear_bit(XPRT_CONNECTION_ABORT
,
2213 /* "close" the socket, preserving the local port */
2214 xs_tcp_reuse_connection(transport
);
2220 dprintk("RPC: worker connecting xprt %p via %s to "
2221 "%s (port %s)\n", xprt
,
2222 xprt
->address_strings
[RPC_DISPLAY_PROTO
],
2223 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2224 xprt
->address_strings
[RPC_DISPLAY_PORT
]);
2226 status
= xs_tcp_finish_connecting(xprt
, sock
);
2227 trace_rpc_socket_connect(xprt
, sock
, status
);
2228 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2229 xprt
, -status
, xprt_connected(xprt
),
2230 sock
->sk
->sk_state
);
2233 printk("%s: connect returned unhandled error %d\n",
2235 case -EADDRNOTAVAIL
:
2236 /* We're probably in TIME_WAIT. Get rid of existing socket,
2239 xs_tcp_force_close(xprt
);
2244 xprt_clear_connecting(xprt
);
2245 current
->flags
&= ~PF_FSTRANS
;
2248 /* Happens, for instance, if the user specified a link
2249 * local IPv6 address without a scope-id.
2254 /* retry with existing socket, after a delay */
2260 xprt_clear_connecting(xprt
);
2261 xprt_wake_pending_tasks(xprt
, status
);
2262 current
->flags
&= ~PF_FSTRANS
;
2266 * xs_connect - connect a socket to a remote endpoint
2267 * @xprt: pointer to transport structure
2268 * @task: address of RPC task that manages state of connect request
2270 * TCP: If the remote end dropped the connection, delay reconnecting.
2272 * UDP socket connects are synchronous, but we use a work queue anyway
2273 * to guarantee that even unprivileged user processes can set up a
2274 * socket on a privileged port.
2276 * If a UDP socket connect fails, the delay behavior here prevents
2277 * retry floods (hard mounts).
2279 static void xs_connect(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
2281 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2283 if (transport
->sock
!= NULL
&& !RPC_IS_SOFTCONN(task
)) {
2284 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2286 xprt
, xprt
->reestablish_timeout
/ HZ
);
2287 queue_delayed_work(rpciod_workqueue
,
2288 &transport
->connect_worker
,
2289 xprt
->reestablish_timeout
);
2290 xprt
->reestablish_timeout
<<= 1;
2291 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
2292 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2293 if (xprt
->reestablish_timeout
> XS_TCP_MAX_REEST_TO
)
2294 xprt
->reestablish_timeout
= XS_TCP_MAX_REEST_TO
;
2296 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt
);
2297 queue_delayed_work(rpciod_workqueue
,
2298 &transport
->connect_worker
, 0);
2303 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2304 * @xprt: rpc_xprt struct containing statistics
2308 static void xs_local_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2312 if (xprt_connected(xprt
))
2313 idle_time
= (long)(jiffies
- xprt
->last_used
) / HZ
;
2315 seq_printf(seq
, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
2316 "%llu %llu %lu %llu %llu\n",
2317 xprt
->stat
.bind_count
,
2318 xprt
->stat
.connect_count
,
2319 xprt
->stat
.connect_time
,
2323 xprt
->stat
.bad_xids
,
2326 xprt
->stat
.max_slots
,
2327 xprt
->stat
.sending_u
,
2328 xprt
->stat
.pending_u
);
2332 * xs_udp_print_stats - display UDP socket-specifc stats
2333 * @xprt: rpc_xprt struct containing statistics
2337 static void xs_udp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2339 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2341 seq_printf(seq
, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2344 xprt
->stat
.bind_count
,
2347 xprt
->stat
.bad_xids
,
2350 xprt
->stat
.max_slots
,
2351 xprt
->stat
.sending_u
,
2352 xprt
->stat
.pending_u
);
2356 * xs_tcp_print_stats - display TCP socket-specifc stats
2357 * @xprt: rpc_xprt struct containing statistics
2361 static void xs_tcp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2363 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2366 if (xprt_connected(xprt
))
2367 idle_time
= (long)(jiffies
- xprt
->last_used
) / HZ
;
2369 seq_printf(seq
, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2370 "%llu %llu %lu %llu %llu\n",
2372 xprt
->stat
.bind_count
,
2373 xprt
->stat
.connect_count
,
2374 xprt
->stat
.connect_time
,
2378 xprt
->stat
.bad_xids
,
2381 xprt
->stat
.max_slots
,
2382 xprt
->stat
.sending_u
,
2383 xprt
->stat
.pending_u
);
2387 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2388 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2389 * to use the server side send routines.
2391 static void *bc_malloc(struct rpc_task
*task
, size_t size
)
2394 struct rpc_buffer
*buf
;
2396 WARN_ON_ONCE(size
> PAGE_SIZE
- sizeof(struct rpc_buffer
));
2397 if (size
> PAGE_SIZE
- sizeof(struct rpc_buffer
))
2400 page
= alloc_page(GFP_KERNEL
);
2404 buf
= page_address(page
);
2405 buf
->len
= PAGE_SIZE
;
2411 * Free the space allocated in the bc_alloc routine
2413 static void bc_free(void *buffer
)
2415 struct rpc_buffer
*buf
;
2420 buf
= container_of(buffer
, struct rpc_buffer
, data
);
2421 free_page((unsigned long)buf
);
2425 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2426 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2428 static int bc_sendto(struct rpc_rqst
*req
)
2431 struct xdr_buf
*xbufp
= &req
->rq_snd_buf
;
2432 struct rpc_xprt
*xprt
= req
->rq_xprt
;
2433 struct sock_xprt
*transport
=
2434 container_of(xprt
, struct sock_xprt
, xprt
);
2435 struct socket
*sock
= transport
->sock
;
2436 unsigned long headoff
;
2437 unsigned long tailoff
;
2439 xs_encode_stream_record_marker(xbufp
);
2441 tailoff
= (unsigned long)xbufp
->tail
[0].iov_base
& ~PAGE_MASK
;
2442 headoff
= (unsigned long)xbufp
->head
[0].iov_base
& ~PAGE_MASK
;
2443 len
= svc_send_common(sock
, xbufp
,
2444 virt_to_page(xbufp
->head
[0].iov_base
), headoff
,
2445 xbufp
->tail
[0].iov_base
, tailoff
);
2447 if (len
!= xbufp
->len
) {
2448 printk(KERN_NOTICE
"Error sending entire callback!\n");
2456 * The send routine. Borrows from svc_send
2458 static int bc_send_request(struct rpc_task
*task
)
2460 struct rpc_rqst
*req
= task
->tk_rqstp
;
2461 struct svc_xprt
*xprt
;
2464 dprintk("sending request with xid: %08x\n", ntohl(req
->rq_xid
));
2466 * Get the server socket associated with this callback xprt
2468 xprt
= req
->rq_xprt
->bc_xprt
;
2471 * Grab the mutex to serialize data as the connection is shared
2472 * with the fore channel
2474 if (!mutex_trylock(&xprt
->xpt_mutex
)) {
2475 rpc_sleep_on(&xprt
->xpt_bc_pending
, task
, NULL
);
2476 if (!mutex_trylock(&xprt
->xpt_mutex
))
2478 rpc_wake_up_queued_task(&xprt
->xpt_bc_pending
, task
);
2480 if (test_bit(XPT_DEAD
, &xprt
->xpt_flags
))
2483 len
= bc_sendto(req
);
2484 mutex_unlock(&xprt
->xpt_mutex
);
2493 * The close routine. Since this is client initiated, we do nothing
2496 static void bc_close(struct rpc_xprt
*xprt
)
2501 * The xprt destroy routine. Again, because this connection is client
2502 * initiated, we do nothing
2505 static void bc_destroy(struct rpc_xprt
*xprt
)
2509 static struct rpc_xprt_ops xs_local_ops
= {
2510 .reserve_xprt
= xprt_reserve_xprt
,
2511 .release_xprt
= xs_tcp_release_xprt
,
2512 .alloc_slot
= xprt_alloc_slot
,
2513 .rpcbind
= xs_local_rpcbind
,
2514 .set_port
= xs_local_set_port
,
2515 .connect
= xs_local_connect
,
2516 .buf_alloc
= rpc_malloc
,
2517 .buf_free
= rpc_free
,
2518 .send_request
= xs_local_send_request
,
2519 .set_retrans_timeout
= xprt_set_retrans_timeout_def
,
2521 .destroy
= xs_local_destroy
,
2522 .print_stats
= xs_local_print_stats
,
2525 static struct rpc_xprt_ops xs_udp_ops
= {
2526 .set_buffer_size
= xs_udp_set_buffer_size
,
2527 .reserve_xprt
= xprt_reserve_xprt_cong
,
2528 .release_xprt
= xprt_release_xprt_cong
,
2529 .alloc_slot
= xprt_alloc_slot
,
2530 .rpcbind
= rpcb_getport_async
,
2531 .set_port
= xs_set_port
,
2532 .connect
= xs_connect
,
2533 .buf_alloc
= rpc_malloc
,
2534 .buf_free
= rpc_free
,
2535 .send_request
= xs_udp_send_request
,
2536 .set_retrans_timeout
= xprt_set_retrans_timeout_rtt
,
2537 .timer
= xs_udp_timer
,
2538 .release_request
= xprt_release_rqst_cong
,
2540 .destroy
= xs_destroy
,
2541 .print_stats
= xs_udp_print_stats
,
2544 static struct rpc_xprt_ops xs_tcp_ops
= {
2545 .reserve_xprt
= xprt_reserve_xprt
,
2546 .release_xprt
= xs_tcp_release_xprt
,
2547 .alloc_slot
= xprt_lock_and_alloc_slot
,
2548 .rpcbind
= rpcb_getport_async
,
2549 .set_port
= xs_set_port
,
2550 .connect
= xs_connect
,
2551 .buf_alloc
= rpc_malloc
,
2552 .buf_free
= rpc_free
,
2553 .send_request
= xs_tcp_send_request
,
2554 .set_retrans_timeout
= xprt_set_retrans_timeout_def
,
2555 .close
= xs_tcp_close
,
2556 .destroy
= xs_destroy
,
2557 .print_stats
= xs_tcp_print_stats
,
2561 * The rpc_xprt_ops for the server backchannel
2564 static struct rpc_xprt_ops bc_tcp_ops
= {
2565 .reserve_xprt
= xprt_reserve_xprt
,
2566 .release_xprt
= xprt_release_xprt
,
2567 .alloc_slot
= xprt_alloc_slot
,
2568 .buf_alloc
= bc_malloc
,
2569 .buf_free
= bc_free
,
2570 .send_request
= bc_send_request
,
2571 .set_retrans_timeout
= xprt_set_retrans_timeout_def
,
2573 .destroy
= bc_destroy
,
2574 .print_stats
= xs_tcp_print_stats
,
2577 static int xs_init_anyaddr(const int family
, struct sockaddr
*sap
)
2579 static const struct sockaddr_in sin
= {
2580 .sin_family
= AF_INET
,
2581 .sin_addr
.s_addr
= htonl(INADDR_ANY
),
2583 static const struct sockaddr_in6 sin6
= {
2584 .sin6_family
= AF_INET6
,
2585 .sin6_addr
= IN6ADDR_ANY_INIT
,
2592 memcpy(sap
, &sin
, sizeof(sin
));
2595 memcpy(sap
, &sin6
, sizeof(sin6
));
2598 dprintk("RPC: %s: Bad address family\n", __func__
);
2599 return -EAFNOSUPPORT
;
2604 static struct rpc_xprt
*xs_setup_xprt(struct xprt_create
*args
,
2605 unsigned int slot_table_size
,
2606 unsigned int max_slot_table_size
)
2608 struct rpc_xprt
*xprt
;
2609 struct sock_xprt
*new;
2611 if (args
->addrlen
> sizeof(xprt
->addr
)) {
2612 dprintk("RPC: xs_setup_xprt: address too large\n");
2613 return ERR_PTR(-EBADF
);
2616 xprt
= xprt_alloc(args
->net
, sizeof(*new), slot_table_size
,
2617 max_slot_table_size
);
2619 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2621 return ERR_PTR(-ENOMEM
);
2624 new = container_of(xprt
, struct sock_xprt
, xprt
);
2625 memcpy(&xprt
->addr
, args
->dstaddr
, args
->addrlen
);
2626 xprt
->addrlen
= args
->addrlen
;
2628 memcpy(&new->srcaddr
, args
->srcaddr
, args
->addrlen
);
2631 err
= xs_init_anyaddr(args
->dstaddr
->sa_family
,
2632 (struct sockaddr
*)&new->srcaddr
);
2635 return ERR_PTR(err
);
2642 static const struct rpc_timeout xs_local_default_timeout
= {
2643 .to_initval
= 10 * HZ
,
2644 .to_maxval
= 10 * HZ
,
2649 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2650 * @args: rpc transport creation arguments
2652 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2654 static struct rpc_xprt
*xs_setup_local(struct xprt_create
*args
)
2656 struct sockaddr_un
*sun
= (struct sockaddr_un
*)args
->dstaddr
;
2657 struct sock_xprt
*transport
;
2658 struct rpc_xprt
*xprt
;
2659 struct rpc_xprt
*ret
;
2661 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
,
2662 xprt_max_tcp_slot_table_entries
);
2665 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2668 xprt
->tsh_size
= sizeof(rpc_fraghdr
) / sizeof(u32
);
2669 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
2671 xprt
->bind_timeout
= XS_BIND_TO
;
2672 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2673 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2675 xprt
->ops
= &xs_local_ops
;
2676 xprt
->timeout
= &xs_local_default_timeout
;
2678 INIT_DELAYED_WORK(&transport
->connect_worker
,
2679 xs_dummy_setup_socket
);
2681 switch (sun
->sun_family
) {
2683 if (sun
->sun_path
[0] != '/') {
2684 dprintk("RPC: bad AF_LOCAL address: %s\n",
2686 ret
= ERR_PTR(-EINVAL
);
2689 xprt_set_bound(xprt
);
2690 xs_format_peer_addresses(xprt
, "local", RPCBIND_NETID_LOCAL
);
2691 ret
= ERR_PTR(xs_local_setup_socket(transport
));
2696 ret
= ERR_PTR(-EAFNOSUPPORT
);
2700 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2701 xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
2703 if (try_module_get(THIS_MODULE
))
2705 ret
= ERR_PTR(-EINVAL
);
2711 static const struct rpc_timeout xs_udp_default_timeout
= {
2712 .to_initval
= 5 * HZ
,
2713 .to_maxval
= 30 * HZ
,
2714 .to_increment
= 5 * HZ
,
2719 * xs_setup_udp - Set up transport to use a UDP socket
2720 * @args: rpc transport creation arguments
2723 static struct rpc_xprt
*xs_setup_udp(struct xprt_create
*args
)
2725 struct sockaddr
*addr
= args
->dstaddr
;
2726 struct rpc_xprt
*xprt
;
2727 struct sock_xprt
*transport
;
2728 struct rpc_xprt
*ret
;
2730 xprt
= xs_setup_xprt(args
, xprt_udp_slot_table_entries
,
2731 xprt_udp_slot_table_entries
);
2734 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2736 xprt
->prot
= IPPROTO_UDP
;
2738 /* XXX: header size can vary due to auth type, IPv6, etc. */
2739 xprt
->max_payload
= (1U << 16) - (MAX_HEADER
<< 3);
2741 xprt
->bind_timeout
= XS_BIND_TO
;
2742 xprt
->reestablish_timeout
= XS_UDP_REEST_TO
;
2743 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2745 xprt
->ops
= &xs_udp_ops
;
2747 xprt
->timeout
= &xs_udp_default_timeout
;
2749 switch (addr
->sa_family
) {
2751 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
2752 xprt_set_bound(xprt
);
2754 INIT_DELAYED_WORK(&transport
->connect_worker
,
2755 xs_udp_setup_socket
);
2756 xs_format_peer_addresses(xprt
, "udp", RPCBIND_NETID_UDP
);
2759 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
2760 xprt_set_bound(xprt
);
2762 INIT_DELAYED_WORK(&transport
->connect_worker
,
2763 xs_udp_setup_socket
);
2764 xs_format_peer_addresses(xprt
, "udp", RPCBIND_NETID_UDP6
);
2767 ret
= ERR_PTR(-EAFNOSUPPORT
);
2771 if (xprt_bound(xprt
))
2772 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2773 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2774 xprt
->address_strings
[RPC_DISPLAY_PORT
],
2775 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2777 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2778 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2779 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2781 if (try_module_get(THIS_MODULE
))
2783 ret
= ERR_PTR(-EINVAL
);
2789 static const struct rpc_timeout xs_tcp_default_timeout
= {
2790 .to_initval
= 60 * HZ
,
2791 .to_maxval
= 60 * HZ
,
2796 * xs_setup_tcp - Set up transport to use a TCP socket
2797 * @args: rpc transport creation arguments
2800 static struct rpc_xprt
*xs_setup_tcp(struct xprt_create
*args
)
2802 struct sockaddr
*addr
= args
->dstaddr
;
2803 struct rpc_xprt
*xprt
;
2804 struct sock_xprt
*transport
;
2805 struct rpc_xprt
*ret
;
2806 unsigned int max_slot_table_size
= xprt_max_tcp_slot_table_entries
;
2808 if (args
->flags
& XPRT_CREATE_INFINITE_SLOTS
)
2809 max_slot_table_size
= RPC_MAX_SLOT_TABLE_LIMIT
;
2811 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
,
2812 max_slot_table_size
);
2815 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2817 xprt
->prot
= IPPROTO_TCP
;
2818 xprt
->tsh_size
= sizeof(rpc_fraghdr
) / sizeof(u32
);
2819 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
2821 xprt
->bind_timeout
= XS_BIND_TO
;
2822 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2823 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2825 xprt
->ops
= &xs_tcp_ops
;
2826 xprt
->timeout
= &xs_tcp_default_timeout
;
2828 switch (addr
->sa_family
) {
2830 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
2831 xprt_set_bound(xprt
);
2833 INIT_DELAYED_WORK(&transport
->connect_worker
,
2834 xs_tcp_setup_socket
);
2835 xs_format_peer_addresses(xprt
, "tcp", RPCBIND_NETID_TCP
);
2838 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
2839 xprt_set_bound(xprt
);
2841 INIT_DELAYED_WORK(&transport
->connect_worker
,
2842 xs_tcp_setup_socket
);
2843 xs_format_peer_addresses(xprt
, "tcp", RPCBIND_NETID_TCP6
);
2846 ret
= ERR_PTR(-EAFNOSUPPORT
);
2850 if (xprt_bound(xprt
))
2851 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2852 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2853 xprt
->address_strings
[RPC_DISPLAY_PORT
],
2854 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2856 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2857 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2858 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2861 if (try_module_get(THIS_MODULE
))
2863 ret
= ERR_PTR(-EINVAL
);
2870 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2871 * @args: rpc transport creation arguments
2874 static struct rpc_xprt
*xs_setup_bc_tcp(struct xprt_create
*args
)
2876 struct sockaddr
*addr
= args
->dstaddr
;
2877 struct rpc_xprt
*xprt
;
2878 struct sock_xprt
*transport
;
2879 struct svc_sock
*bc_sock
;
2880 struct rpc_xprt
*ret
;
2882 if (args
->bc_xprt
->xpt_bc_xprt
) {
2884 * This server connection already has a backchannel
2885 * export; we can't create a new one, as we wouldn't be
2886 * able to match replies based on xid any more. So,
2887 * reuse the already-existing one:
2889 return args
->bc_xprt
->xpt_bc_xprt
;
2891 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
,
2892 xprt_tcp_slot_table_entries
);
2895 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2897 xprt
->prot
= IPPROTO_TCP
;
2898 xprt
->tsh_size
= sizeof(rpc_fraghdr
) / sizeof(u32
);
2899 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
2900 xprt
->timeout
= &xs_tcp_default_timeout
;
2903 xprt_set_bound(xprt
);
2904 xprt
->bind_timeout
= 0;
2905 xprt
->reestablish_timeout
= 0;
2906 xprt
->idle_timeout
= 0;
2908 xprt
->ops
= &bc_tcp_ops
;
2910 switch (addr
->sa_family
) {
2912 xs_format_peer_addresses(xprt
, "tcp",
2916 xs_format_peer_addresses(xprt
, "tcp",
2917 RPCBIND_NETID_TCP6
);
2920 ret
= ERR_PTR(-EAFNOSUPPORT
);
2924 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2925 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2926 xprt
->address_strings
[RPC_DISPLAY_PORT
],
2927 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2930 * Once we've associated a backchannel xprt with a connection,
2931 * we want to keep it around as long as long as the connection
2932 * lasts, in case we need to start using it for a backchannel
2933 * again; this reference won't be dropped until bc_xprt is
2937 args
->bc_xprt
->xpt_bc_xprt
= xprt
;
2938 xprt
->bc_xprt
= args
->bc_xprt
;
2939 bc_sock
= container_of(args
->bc_xprt
, struct svc_sock
, sk_xprt
);
2940 transport
->sock
= bc_sock
->sk_sock
;
2941 transport
->inet
= bc_sock
->sk_sk
;
2944 * Since we don't want connections for the backchannel, we set
2945 * the xprt status to connected
2947 xprt_set_connected(xprt
);
2950 if (try_module_get(THIS_MODULE
))
2953 ret
= ERR_PTR(-EINVAL
);
2959 static struct xprt_class xs_local_transport
= {
2960 .list
= LIST_HEAD_INIT(xs_local_transport
.list
),
2961 .name
= "named UNIX socket",
2962 .owner
= THIS_MODULE
,
2963 .ident
= XPRT_TRANSPORT_LOCAL
,
2964 .setup
= xs_setup_local
,
2967 static struct xprt_class xs_udp_transport
= {
2968 .list
= LIST_HEAD_INIT(xs_udp_transport
.list
),
2970 .owner
= THIS_MODULE
,
2971 .ident
= XPRT_TRANSPORT_UDP
,
2972 .setup
= xs_setup_udp
,
2975 static struct xprt_class xs_tcp_transport
= {
2976 .list
= LIST_HEAD_INIT(xs_tcp_transport
.list
),
2978 .owner
= THIS_MODULE
,
2979 .ident
= XPRT_TRANSPORT_TCP
,
2980 .setup
= xs_setup_tcp
,
2983 static struct xprt_class xs_bc_tcp_transport
= {
2984 .list
= LIST_HEAD_INIT(xs_bc_tcp_transport
.list
),
2985 .name
= "tcp NFSv4.1 backchannel",
2986 .owner
= THIS_MODULE
,
2987 .ident
= XPRT_TRANSPORT_BC_TCP
,
2988 .setup
= xs_setup_bc_tcp
,
2992 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2995 int init_socket_xprt(void)
2998 if (!sunrpc_table_header
)
2999 sunrpc_table_header
= register_sysctl_table(sunrpc_table
);
3002 xprt_register_transport(&xs_local_transport
);
3003 xprt_register_transport(&xs_udp_transport
);
3004 xprt_register_transport(&xs_tcp_transport
);
3005 xprt_register_transport(&xs_bc_tcp_transport
);
3011 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3014 void cleanup_socket_xprt(void)
3017 if (sunrpc_table_header
) {
3018 unregister_sysctl_table(sunrpc_table_header
);
3019 sunrpc_table_header
= NULL
;
3023 xprt_unregister_transport(&xs_local_transport
);
3024 xprt_unregister_transport(&xs_udp_transport
);
3025 xprt_unregister_transport(&xs_tcp_transport
);
3026 xprt_unregister_transport(&xs_bc_tcp_transport
);
3029 static int param_set_uint_minmax(const char *val
,
3030 const struct kernel_param
*kp
,
3031 unsigned int min
, unsigned int max
)
3038 ret
= strict_strtoul(val
, 0, &num
);
3039 if (ret
== -EINVAL
|| num
< min
|| num
> max
)
3041 *((unsigned int *)kp
->arg
) = num
;
3045 static int param_set_portnr(const char *val
, const struct kernel_param
*kp
)
3047 return param_set_uint_minmax(val
, kp
,
3052 static struct kernel_param_ops param_ops_portnr
= {
3053 .set
= param_set_portnr
,
3054 .get
= param_get_uint
,
3057 #define param_check_portnr(name, p) \
3058 __param_check(name, p, unsigned int);
3060 module_param_named(min_resvport
, xprt_min_resvport
, portnr
, 0644);
3061 module_param_named(max_resvport
, xprt_max_resvport
, portnr
, 0644);
3063 static int param_set_slot_table_size(const char *val
,
3064 const struct kernel_param
*kp
)
3066 return param_set_uint_minmax(val
, kp
,
3068 RPC_MAX_SLOT_TABLE
);
3071 static struct kernel_param_ops param_ops_slot_table_size
= {
3072 .set
= param_set_slot_table_size
,
3073 .get
= param_get_uint
,
3076 #define param_check_slot_table_size(name, p) \
3077 __param_check(name, p, unsigned int);
3079 static int param_set_max_slot_table_size(const char *val
,
3080 const struct kernel_param
*kp
)
3082 return param_set_uint_minmax(val
, kp
,
3084 RPC_MAX_SLOT_TABLE_LIMIT
);
3087 static struct kernel_param_ops param_ops_max_slot_table_size
= {
3088 .set
= param_set_max_slot_table_size
,
3089 .get
= param_get_uint
,
3092 #define param_check_max_slot_table_size(name, p) \
3093 __param_check(name, p, unsigned int);
3095 module_param_named(tcp_slot_table_entries
, xprt_tcp_slot_table_entries
,
3096 slot_table_size
, 0644);
3097 module_param_named(tcp_max_slot_table_entries
, xprt_max_tcp_slot_table_entries
,
3098 max_slot_table_size
, 0644);
3099 module_param_named(udp_slot_table_entries
, xprt_udp_slot_table_entries
,
3100 slot_table_size
, 0644);