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/slab.h>
23 #include <linux/module.h>
24 #include <linux/capability.h>
25 #include <linux/pagemap.h>
26 #include <linux/errno.h>
27 #include <linux/socket.h>
29 #include <linux/net.h>
31 #include <linux/udp.h>
32 #include <linux/tcp.h>
33 #include <linux/sunrpc/clnt.h>
34 #include <linux/sunrpc/sched.h>
35 #include <linux/sunrpc/xprtsock.h>
36 #include <linux/file.h>
37 #ifdef CONFIG_NFS_V4_1
38 #include <linux/sunrpc/bc_xprt.h>
42 #include <net/checksum.h>
49 unsigned int xprt_udp_slot_table_entries
= RPC_DEF_SLOT_TABLE
;
50 unsigned int xprt_tcp_slot_table_entries
= RPC_DEF_SLOT_TABLE
;
52 unsigned int xprt_min_resvport
= RPC_DEF_MIN_RESVPORT
;
53 unsigned int xprt_max_resvport
= RPC_DEF_MAX_RESVPORT
;
55 #define XS_TCP_LINGER_TO (15U * HZ)
56 static unsigned int xs_tcp_fin_timeout __read_mostly
= XS_TCP_LINGER_TO
;
59 * We can register our own files under /proc/sys/sunrpc by
60 * calling register_sysctl_table() again. The files in that
61 * directory become the union of all files registered there.
63 * We simply need to make sure that we don't collide with
64 * someone else's file names!
69 static unsigned int min_slot_table_size
= RPC_MIN_SLOT_TABLE
;
70 static unsigned int max_slot_table_size
= RPC_MAX_SLOT_TABLE
;
71 static unsigned int xprt_min_resvport_limit
= RPC_MIN_RESVPORT
;
72 static unsigned int xprt_max_resvport_limit
= RPC_MAX_RESVPORT
;
74 static struct ctl_table_header
*sunrpc_table_header
;
77 * FIXME: changing the UDP slot table size should also resize the UDP
78 * socket buffers for existing UDP transports
80 static ctl_table xs_tunables_table
[] = {
82 .ctl_name
= CTL_SLOTTABLE_UDP
,
83 .procname
= "udp_slot_table_entries",
84 .data
= &xprt_udp_slot_table_entries
,
85 .maxlen
= sizeof(unsigned int),
87 .proc_handler
= &proc_dointvec_minmax
,
88 .strategy
= &sysctl_intvec
,
89 .extra1
= &min_slot_table_size
,
90 .extra2
= &max_slot_table_size
93 .ctl_name
= CTL_SLOTTABLE_TCP
,
94 .procname
= "tcp_slot_table_entries",
95 .data
= &xprt_tcp_slot_table_entries
,
96 .maxlen
= sizeof(unsigned int),
98 .proc_handler
= &proc_dointvec_minmax
,
99 .strategy
= &sysctl_intvec
,
100 .extra1
= &min_slot_table_size
,
101 .extra2
= &max_slot_table_size
104 .ctl_name
= CTL_MIN_RESVPORT
,
105 .procname
= "min_resvport",
106 .data
= &xprt_min_resvport
,
107 .maxlen
= sizeof(unsigned int),
109 .proc_handler
= &proc_dointvec_minmax
,
110 .strategy
= &sysctl_intvec
,
111 .extra1
= &xprt_min_resvport_limit
,
112 .extra2
= &xprt_max_resvport_limit
115 .ctl_name
= CTL_MAX_RESVPORT
,
116 .procname
= "max_resvport",
117 .data
= &xprt_max_resvport
,
118 .maxlen
= sizeof(unsigned int),
120 .proc_handler
= &proc_dointvec_minmax
,
121 .strategy
= &sysctl_intvec
,
122 .extra1
= &xprt_min_resvport_limit
,
123 .extra2
= &xprt_max_resvport_limit
126 .procname
= "tcp_fin_timeout",
127 .data
= &xs_tcp_fin_timeout
,
128 .maxlen
= sizeof(xs_tcp_fin_timeout
),
130 .proc_handler
= &proc_dointvec_jiffies
,
131 .strategy
= sysctl_jiffies
138 static ctl_table sunrpc_table
[] = {
140 .ctl_name
= CTL_SUNRPC
,
141 .procname
= "sunrpc",
143 .child
= xs_tunables_table
153 * Time out for an RPC UDP socket connect. UDP socket connects are
154 * synchronous, but we set a timeout anyway in case of resource
155 * exhaustion on the local host.
157 #define XS_UDP_CONN_TO (5U * HZ)
160 * Wait duration for an RPC TCP connection to be established. Solaris
161 * NFS over TCP uses 60 seconds, for example, which is in line with how
162 * long a server takes to reboot.
164 #define XS_TCP_CONN_TO (60U * HZ)
167 * Wait duration for a reply from the RPC portmapper.
169 #define XS_BIND_TO (60U * HZ)
172 * Delay if a UDP socket connect error occurs. This is most likely some
173 * kind of resource problem on the local host.
175 #define XS_UDP_REEST_TO (2U * HZ)
178 * The reestablish timeout allows clients to delay for a bit before attempting
179 * to reconnect to a server that just dropped our connection.
181 * We implement an exponential backoff when trying to reestablish a TCP
182 * transport connection with the server. Some servers like to drop a TCP
183 * connection when they are overworked, so we start with a short timeout and
184 * increase over time if the server is down or not responding.
186 #define XS_TCP_INIT_REEST_TO (3U * HZ)
187 #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
190 * TCP idle timeout; client drops the transport socket if it is idle
191 * for this long. Note that we also timeout UDP sockets to prevent
192 * holding port numbers when there is no RPC traffic.
194 #define XS_IDLE_DISC_TO (5U * 60 * HZ)
197 # undef RPC_DEBUG_DATA
198 # define RPCDBG_FACILITY RPCDBG_TRANS
201 #ifdef RPC_DEBUG_DATA
202 static void xs_pktdump(char *msg
, u32
*packet
, unsigned int count
)
204 u8
*buf
= (u8
*) packet
;
207 dprintk("RPC: %s\n", msg
);
208 for (j
= 0; j
< count
&& j
< 128; j
+= 4) {
212 dprintk("0x%04x ", j
);
214 dprintk("%02x%02x%02x%02x ",
215 buf
[j
], buf
[j
+1], buf
[j
+2], buf
[j
+3]);
220 static inline void xs_pktdump(char *msg
, u32
*packet
, unsigned int count
)
227 struct rpc_xprt xprt
;
232 struct socket
* sock
;
236 * State of TCP reply receive
244 unsigned long tcp_copied
,
248 * Connection of transports
250 struct delayed_work connect_worker
;
251 struct sockaddr_storage addr
;
255 * UDP socket buffer size parameters
261 * Saved socket callback addresses
263 void (*old_data_ready
)(struct sock
*, int);
264 void (*old_state_change
)(struct sock
*);
265 void (*old_write_space
)(struct sock
*);
266 void (*old_error_report
)(struct sock
*);
270 * TCP receive state flags
272 #define TCP_RCV_LAST_FRAG (1UL << 0)
273 #define TCP_RCV_COPY_FRAGHDR (1UL << 1)
274 #define TCP_RCV_COPY_XID (1UL << 2)
275 #define TCP_RCV_COPY_DATA (1UL << 3)
276 #define TCP_RCV_READ_CALLDIR (1UL << 4)
277 #define TCP_RCV_COPY_CALLDIR (1UL << 5)
282 #define TCP_RPC_REPLY (1UL << 6)
284 static inline struct sockaddr
*xs_addr(struct rpc_xprt
*xprt
)
286 return (struct sockaddr
*) &xprt
->addr
;
289 static inline struct sockaddr_in
*xs_addr_in(struct rpc_xprt
*xprt
)
291 return (struct sockaddr_in
*) &xprt
->addr
;
294 static inline struct sockaddr_in6
*xs_addr_in6(struct rpc_xprt
*xprt
)
296 return (struct sockaddr_in6
*) &xprt
->addr
;
299 static void xs_format_ipv4_peer_addresses(struct rpc_xprt
*xprt
,
300 const char *protocol
,
303 struct sockaddr_in
*addr
= xs_addr_in(xprt
);
306 buf
= kzalloc(20, GFP_KERNEL
);
308 snprintf(buf
, 20, "%pI4", &addr
->sin_addr
.s_addr
);
310 xprt
->address_strings
[RPC_DISPLAY_ADDR
] = buf
;
312 buf
= kzalloc(8, GFP_KERNEL
);
314 snprintf(buf
, 8, "%u",
315 ntohs(addr
->sin_port
));
317 xprt
->address_strings
[RPC_DISPLAY_PORT
] = buf
;
319 xprt
->address_strings
[RPC_DISPLAY_PROTO
] = protocol
;
321 buf
= kzalloc(48, GFP_KERNEL
);
323 snprintf(buf
, 48, "addr=%pI4 port=%u proto=%s",
324 &addr
->sin_addr
.s_addr
,
325 ntohs(addr
->sin_port
),
328 xprt
->address_strings
[RPC_DISPLAY_ALL
] = buf
;
330 buf
= kzalloc(10, GFP_KERNEL
);
332 snprintf(buf
, 10, "%02x%02x%02x%02x",
333 NIPQUAD(addr
->sin_addr
.s_addr
));
335 xprt
->address_strings
[RPC_DISPLAY_HEX_ADDR
] = buf
;
337 buf
= kzalloc(8, GFP_KERNEL
);
339 snprintf(buf
, 8, "%4hx",
340 ntohs(addr
->sin_port
));
342 xprt
->address_strings
[RPC_DISPLAY_HEX_PORT
] = buf
;
344 buf
= kzalloc(30, GFP_KERNEL
);
346 snprintf(buf
, 30, "%pI4.%u.%u",
347 &addr
->sin_addr
.s_addr
,
348 ntohs(addr
->sin_port
) >> 8,
349 ntohs(addr
->sin_port
) & 0xff);
351 xprt
->address_strings
[RPC_DISPLAY_UNIVERSAL_ADDR
] = buf
;
353 xprt
->address_strings
[RPC_DISPLAY_NETID
] = netid
;
356 static void xs_format_ipv6_peer_addresses(struct rpc_xprt
*xprt
,
357 const char *protocol
,
360 struct sockaddr_in6
*addr
= xs_addr_in6(xprt
);
363 buf
= kzalloc(40, GFP_KERNEL
);
365 snprintf(buf
, 40, "%pI6",&addr
->sin6_addr
);
367 xprt
->address_strings
[RPC_DISPLAY_ADDR
] = buf
;
369 buf
= kzalloc(8, GFP_KERNEL
);
371 snprintf(buf
, 8, "%u",
372 ntohs(addr
->sin6_port
));
374 xprt
->address_strings
[RPC_DISPLAY_PORT
] = buf
;
376 xprt
->address_strings
[RPC_DISPLAY_PROTO
] = protocol
;
378 buf
= kzalloc(64, GFP_KERNEL
);
380 snprintf(buf
, 64, "addr=%pI6 port=%u proto=%s",
382 ntohs(addr
->sin6_port
),
385 xprt
->address_strings
[RPC_DISPLAY_ALL
] = buf
;
387 buf
= kzalloc(36, GFP_KERNEL
);
389 snprintf(buf
, 36, "%pi6", &addr
->sin6_addr
);
391 xprt
->address_strings
[RPC_DISPLAY_HEX_ADDR
] = buf
;
393 buf
= kzalloc(8, GFP_KERNEL
);
395 snprintf(buf
, 8, "%4hx",
396 ntohs(addr
->sin6_port
));
398 xprt
->address_strings
[RPC_DISPLAY_HEX_PORT
] = buf
;
400 buf
= kzalloc(50, GFP_KERNEL
);
402 snprintf(buf
, 50, "%pI6.%u.%u",
404 ntohs(addr
->sin6_port
) >> 8,
405 ntohs(addr
->sin6_port
) & 0xff);
407 xprt
->address_strings
[RPC_DISPLAY_UNIVERSAL_ADDR
] = buf
;
409 xprt
->address_strings
[RPC_DISPLAY_NETID
] = netid
;
412 static void xs_free_peer_addresses(struct rpc_xprt
*xprt
)
416 for (i
= 0; i
< RPC_DISPLAY_MAX
; i
++)
418 case RPC_DISPLAY_PROTO
:
419 case RPC_DISPLAY_NETID
:
422 kfree(xprt
->address_strings
[i
]);
426 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
428 static int xs_send_kvec(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
, struct kvec
*vec
, unsigned int base
, int more
)
430 struct msghdr msg
= {
432 .msg_namelen
= addrlen
,
433 .msg_flags
= XS_SENDMSG_FLAGS
| (more
? MSG_MORE
: 0),
436 .iov_base
= vec
->iov_base
+ base
,
437 .iov_len
= vec
->iov_len
- base
,
440 if (iov
.iov_len
!= 0)
441 return kernel_sendmsg(sock
, &msg
, &iov
, 1, iov
.iov_len
);
442 return kernel_sendmsg(sock
, &msg
, NULL
, 0, 0);
445 static int xs_send_pagedata(struct socket
*sock
, struct xdr_buf
*xdr
, unsigned int base
, int more
)
448 unsigned int remainder
;
451 remainder
= xdr
->page_len
- base
;
452 base
+= xdr
->page_base
;
453 ppage
= xdr
->pages
+ (base
>> PAGE_SHIFT
);
456 unsigned int len
= min_t(unsigned int, PAGE_SIZE
- base
, remainder
);
457 int flags
= XS_SENDMSG_FLAGS
;
460 if (remainder
!= 0 || more
)
462 err
= sock
->ops
->sendpage(sock
, *ppage
, base
, len
, flags
);
463 if (remainder
== 0 || err
!= len
)
477 * xs_sendpages - write pages directly to a socket
478 * @sock: socket to send on
479 * @addr: UDP only -- address of destination
480 * @addrlen: UDP only -- length of destination address
481 * @xdr: buffer containing this request
482 * @base: starting position in the buffer
485 static int xs_sendpages(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
, struct xdr_buf
*xdr
, unsigned int base
)
487 unsigned int remainder
= xdr
->len
- base
;
493 clear_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
);
499 if (base
< xdr
->head
[0].iov_len
|| addr
!= NULL
) {
500 unsigned int len
= xdr
->head
[0].iov_len
- base
;
502 err
= xs_send_kvec(sock
, addr
, addrlen
, &xdr
->head
[0], base
, remainder
!= 0);
503 if (remainder
== 0 || err
!= len
)
508 base
-= xdr
->head
[0].iov_len
;
510 if (base
< xdr
->page_len
) {
511 unsigned int len
= xdr
->page_len
- base
;
513 err
= xs_send_pagedata(sock
, xdr
, base
, remainder
!= 0);
514 if (remainder
== 0 || err
!= len
)
519 base
-= xdr
->page_len
;
521 if (base
>= xdr
->tail
[0].iov_len
)
523 err
= xs_send_kvec(sock
, NULL
, 0, &xdr
->tail
[0], base
, 0);
532 static void xs_nospace_callback(struct rpc_task
*task
)
534 struct sock_xprt
*transport
= container_of(task
->tk_rqstp
->rq_xprt
, struct sock_xprt
, xprt
);
536 transport
->inet
->sk_write_pending
--;
537 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
541 * xs_nospace - place task on wait queue if transmit was incomplete
542 * @task: task to put to sleep
545 static int xs_nospace(struct rpc_task
*task
)
547 struct rpc_rqst
*req
= task
->tk_rqstp
;
548 struct rpc_xprt
*xprt
= req
->rq_xprt
;
549 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
552 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
553 task
->tk_pid
, req
->rq_slen
- req
->rq_bytes_sent
,
556 /* Protect against races with write_space */
557 spin_lock_bh(&xprt
->transport_lock
);
559 /* Don't race with disconnect */
560 if (xprt_connected(xprt
)) {
561 if (test_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
)) {
564 * Notify TCP that we're limited by the application
567 set_bit(SOCK_NOSPACE
, &transport
->sock
->flags
);
568 transport
->inet
->sk_write_pending
++;
569 /* ...and wait for more buffer space */
570 xprt_wait_for_buffer_space(task
, xs_nospace_callback
);
573 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
577 spin_unlock_bh(&xprt
->transport_lock
);
582 * xs_udp_send_request - write an RPC request to a UDP socket
583 * @task: address of RPC task that manages the state of an RPC request
586 * 0: The request has been sent
587 * EAGAIN: The socket was blocked, please call again later to
588 * complete the request
589 * ENOTCONN: Caller needs to invoke connect logic then call again
590 * other: Some other error occured, the request was not sent
592 static int xs_udp_send_request(struct rpc_task
*task
)
594 struct rpc_rqst
*req
= task
->tk_rqstp
;
595 struct rpc_xprt
*xprt
= req
->rq_xprt
;
596 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
597 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
600 xs_pktdump("packet data:",
601 req
->rq_svec
->iov_base
,
602 req
->rq_svec
->iov_len
);
604 if (!xprt_bound(xprt
))
606 status
= xs_sendpages(transport
->sock
,
611 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
612 xdr
->len
- req
->rq_bytes_sent
, status
);
615 task
->tk_bytes_sent
+= status
;
616 if (status
>= req
->rq_slen
)
618 /* Still some bytes left; set up for a retry later. */
621 if (!transport
->sock
)
627 /* Should we call xs_close() here? */
630 status
= xs_nospace(task
);
633 dprintk("RPC: sendmsg returned unrecognized error %d\n",
638 /* When the server has died, an ICMP port unreachable message
639 * prompts ECONNREFUSED. */
640 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
647 * xs_tcp_shutdown - gracefully shut down a TCP socket
650 * Initiates a graceful shutdown of the TCP socket by calling the
651 * equivalent of shutdown(SHUT_WR);
653 static void xs_tcp_shutdown(struct rpc_xprt
*xprt
)
655 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
656 struct socket
*sock
= transport
->sock
;
659 kernel_sock_shutdown(sock
, SHUT_WR
);
662 static inline void xs_encode_tcp_record_marker(struct xdr_buf
*buf
)
664 u32 reclen
= buf
->len
- sizeof(rpc_fraghdr
);
665 rpc_fraghdr
*base
= buf
->head
[0].iov_base
;
666 *base
= htonl(RPC_LAST_STREAM_FRAGMENT
| reclen
);
670 * xs_tcp_send_request - write an RPC request to a TCP socket
671 * @task: address of RPC task that manages the state of an RPC request
674 * 0: The request has been sent
675 * EAGAIN: The socket was blocked, please call again later to
676 * complete the request
677 * ENOTCONN: Caller needs to invoke connect logic then call again
678 * other: Some other error occured, the request was not sent
680 * XXX: In the case of soft timeouts, should we eventually give up
681 * if sendmsg is not able to make progress?
683 static int xs_tcp_send_request(struct rpc_task
*task
)
685 struct rpc_rqst
*req
= task
->tk_rqstp
;
686 struct rpc_xprt
*xprt
= req
->rq_xprt
;
687 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
688 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
691 xs_encode_tcp_record_marker(&req
->rq_snd_buf
);
693 xs_pktdump("packet data:",
694 req
->rq_svec
->iov_base
,
695 req
->rq_svec
->iov_len
);
697 /* Continue transmitting the packet/record. We must be careful
698 * to cope with writespace callbacks arriving _after_ we have
699 * called sendmsg(). */
701 status
= xs_sendpages(transport
->sock
,
702 NULL
, 0, xdr
, req
->rq_bytes_sent
);
704 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
705 xdr
->len
- req
->rq_bytes_sent
, status
);
707 if (unlikely(status
< 0))
710 /* If we've sent the entire packet, immediately
711 * reset the count of bytes sent. */
712 req
->rq_bytes_sent
+= status
;
713 task
->tk_bytes_sent
+= status
;
714 if (likely(req
->rq_bytes_sent
>= req
->rq_slen
)) {
715 req
->rq_bytes_sent
= 0;
724 if (!transport
->sock
)
730 /* Should we call xs_close() here? */
733 status
= xs_nospace(task
);
736 dprintk("RPC: sendmsg returned unrecognized error %d\n",
740 xs_tcp_shutdown(xprt
);
743 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
750 * xs_tcp_release_xprt - clean up after a tcp transmission
754 * This cleans up if an error causes us to abort the transmission of a request.
755 * In this case, the socket may need to be reset in order to avoid confusing
758 static void xs_tcp_release_xprt(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
760 struct rpc_rqst
*req
;
762 if (task
!= xprt
->snd_task
)
766 req
= task
->tk_rqstp
;
767 if (req
->rq_bytes_sent
== 0)
769 if (req
->rq_bytes_sent
== req
->rq_snd_buf
.len
)
771 set_bit(XPRT_CLOSE_WAIT
, &task
->tk_xprt
->state
);
773 xprt_release_xprt(xprt
, task
);
776 static void xs_save_old_callbacks(struct sock_xprt
*transport
, struct sock
*sk
)
778 transport
->old_data_ready
= sk
->sk_data_ready
;
779 transport
->old_state_change
= sk
->sk_state_change
;
780 transport
->old_write_space
= sk
->sk_write_space
;
781 transport
->old_error_report
= sk
->sk_error_report
;
784 static void xs_restore_old_callbacks(struct sock_xprt
*transport
, struct sock
*sk
)
786 sk
->sk_data_ready
= transport
->old_data_ready
;
787 sk
->sk_state_change
= transport
->old_state_change
;
788 sk
->sk_write_space
= transport
->old_write_space
;
789 sk
->sk_error_report
= transport
->old_error_report
;
792 static void xs_reset_transport(struct sock_xprt
*transport
)
794 struct socket
*sock
= transport
->sock
;
795 struct sock
*sk
= transport
->inet
;
800 write_lock_bh(&sk
->sk_callback_lock
);
801 transport
->inet
= NULL
;
802 transport
->sock
= NULL
;
804 sk
->sk_user_data
= NULL
;
806 xs_restore_old_callbacks(transport
, sk
);
807 write_unlock_bh(&sk
->sk_callback_lock
);
815 * xs_close - close a socket
818 * This is used when all requests are complete; ie, no DRC state remains
819 * on the server we want to save.
821 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
822 * xs_reset_transport() zeroing the socket from underneath a writer.
824 static void xs_close(struct rpc_xprt
*xprt
)
826 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
828 dprintk("RPC: xs_close xprt %p\n", xprt
);
830 xs_reset_transport(transport
);
832 smp_mb__before_clear_bit();
833 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
834 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
835 clear_bit(XPRT_CLOSING
, &xprt
->state
);
836 smp_mb__after_clear_bit();
837 xprt_disconnect_done(xprt
);
840 static void xs_tcp_close(struct rpc_xprt
*xprt
)
842 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE
, &xprt
->state
))
845 xs_tcp_shutdown(xprt
);
849 * xs_destroy - prepare to shutdown a transport
850 * @xprt: doomed transport
853 static void xs_destroy(struct rpc_xprt
*xprt
)
855 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
857 dprintk("RPC: xs_destroy xprt %p\n", xprt
);
859 cancel_rearming_delayed_work(&transport
->connect_worker
);
862 xs_free_peer_addresses(xprt
);
865 module_put(THIS_MODULE
);
868 static inline struct rpc_xprt
*xprt_from_sock(struct sock
*sk
)
870 return (struct rpc_xprt
*) sk
->sk_user_data
;
874 * xs_udp_data_ready - "data ready" callback for UDP sockets
875 * @sk: socket with data to read
876 * @len: how much data to read
879 static void xs_udp_data_ready(struct sock
*sk
, int len
)
881 struct rpc_task
*task
;
882 struct rpc_xprt
*xprt
;
883 struct rpc_rqst
*rovr
;
885 int err
, repsize
, copied
;
889 read_lock(&sk
->sk_callback_lock
);
890 dprintk("RPC: xs_udp_data_ready...\n");
891 if (!(xprt
= xprt_from_sock(sk
)))
894 if ((skb
= skb_recv_datagram(sk
, 0, 1, &err
)) == NULL
)
900 repsize
= skb
->len
- sizeof(struct udphdr
);
902 dprintk("RPC: impossible RPC reply size %d!\n", repsize
);
906 /* Copy the XID from the skb... */
907 xp
= skb_header_pointer(skb
, sizeof(struct udphdr
),
908 sizeof(_xid
), &_xid
);
912 /* Look up and lock the request corresponding to the given XID */
913 spin_lock(&xprt
->transport_lock
);
914 rovr
= xprt_lookup_rqst(xprt
, *xp
);
917 task
= rovr
->rq_task
;
919 if ((copied
= rovr
->rq_private_buf
.buflen
) > repsize
)
922 /* Suck it into the iovec, verify checksum if not done by hw. */
923 if (csum_partial_copy_to_xdr(&rovr
->rq_private_buf
, skb
)) {
924 UDPX_INC_STATS_BH(sk
, UDP_MIB_INERRORS
);
928 UDPX_INC_STATS_BH(sk
, UDP_MIB_INDATAGRAMS
);
930 /* Something worked... */
931 dst_confirm(skb_dst(skb
));
933 xprt_adjust_cwnd(task
, copied
);
934 xprt_update_rtt(task
);
935 xprt_complete_rqst(task
, copied
);
938 spin_unlock(&xprt
->transport_lock
);
940 skb_free_datagram(sk
, skb
);
942 read_unlock(&sk
->sk_callback_lock
);
945 static inline void xs_tcp_read_fraghdr(struct rpc_xprt
*xprt
, struct xdr_skb_reader
*desc
)
947 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
951 p
= ((char *) &transport
->tcp_fraghdr
) + transport
->tcp_offset
;
952 len
= sizeof(transport
->tcp_fraghdr
) - transport
->tcp_offset
;
953 used
= xdr_skb_read_bits(desc
, p
, len
);
954 transport
->tcp_offset
+= used
;
958 transport
->tcp_reclen
= ntohl(transport
->tcp_fraghdr
);
959 if (transport
->tcp_reclen
& RPC_LAST_STREAM_FRAGMENT
)
960 transport
->tcp_flags
|= TCP_RCV_LAST_FRAG
;
962 transport
->tcp_flags
&= ~TCP_RCV_LAST_FRAG
;
963 transport
->tcp_reclen
&= RPC_FRAGMENT_SIZE_MASK
;
965 transport
->tcp_flags
&= ~TCP_RCV_COPY_FRAGHDR
;
966 transport
->tcp_offset
= 0;
968 /* Sanity check of the record length */
969 if (unlikely(transport
->tcp_reclen
< 8)) {
970 dprintk("RPC: invalid TCP record fragment length\n");
971 xprt_force_disconnect(xprt
);
974 dprintk("RPC: reading TCP record fragment of length %d\n",
975 transport
->tcp_reclen
);
978 static void xs_tcp_check_fraghdr(struct sock_xprt
*transport
)
980 if (transport
->tcp_offset
== transport
->tcp_reclen
) {
981 transport
->tcp_flags
|= TCP_RCV_COPY_FRAGHDR
;
982 transport
->tcp_offset
= 0;
983 if (transport
->tcp_flags
& TCP_RCV_LAST_FRAG
) {
984 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
985 transport
->tcp_flags
|= TCP_RCV_COPY_XID
;
986 transport
->tcp_copied
= 0;
991 static inline void xs_tcp_read_xid(struct sock_xprt
*transport
, struct xdr_skb_reader
*desc
)
996 len
= sizeof(transport
->tcp_xid
) - transport
->tcp_offset
;
997 dprintk("RPC: reading XID (%Zu bytes)\n", len
);
998 p
= ((char *) &transport
->tcp_xid
) + transport
->tcp_offset
;
999 used
= xdr_skb_read_bits(desc
, p
, len
);
1000 transport
->tcp_offset
+= used
;
1003 transport
->tcp_flags
&= ~TCP_RCV_COPY_XID
;
1004 transport
->tcp_flags
|= TCP_RCV_READ_CALLDIR
;
1005 transport
->tcp_copied
= 4;
1006 dprintk("RPC: reading %s XID %08x\n",
1007 (transport
->tcp_flags
& TCP_RPC_REPLY
) ? "reply for"
1009 ntohl(transport
->tcp_xid
));
1010 xs_tcp_check_fraghdr(transport
);
1013 static inline void xs_tcp_read_calldir(struct sock_xprt
*transport
,
1014 struct xdr_skb_reader
*desc
)
1021 * We want transport->tcp_offset to be 8 at the end of this routine
1022 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1023 * When this function is called for the first time,
1024 * transport->tcp_offset is 4 (after having already read the xid).
1026 offset
= transport
->tcp_offset
- sizeof(transport
->tcp_xid
);
1027 len
= sizeof(calldir
) - offset
;
1028 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len
);
1029 used
= xdr_skb_read_bits(desc
, &calldir
, len
);
1030 transport
->tcp_offset
+= used
;
1033 transport
->tcp_flags
&= ~TCP_RCV_READ_CALLDIR
;
1034 transport
->tcp_flags
|= TCP_RCV_COPY_CALLDIR
;
1035 transport
->tcp_flags
|= TCP_RCV_COPY_DATA
;
1037 * We don't yet have the XDR buffer, so we will write the calldir
1038 * out after we get the buffer from the 'struct rpc_rqst'
1040 if (ntohl(calldir
) == RPC_REPLY
)
1041 transport
->tcp_flags
|= TCP_RPC_REPLY
;
1043 transport
->tcp_flags
&= ~TCP_RPC_REPLY
;
1044 dprintk("RPC: reading %s CALL/REPLY flag %08x\n",
1045 (transport
->tcp_flags
& TCP_RPC_REPLY
) ?
1046 "reply for" : "request with", calldir
);
1047 xs_tcp_check_fraghdr(transport
);
1050 static inline void xs_tcp_read_common(struct rpc_xprt
*xprt
,
1051 struct xdr_skb_reader
*desc
,
1052 struct rpc_rqst
*req
)
1054 struct sock_xprt
*transport
=
1055 container_of(xprt
, struct sock_xprt
, xprt
);
1056 struct xdr_buf
*rcvbuf
;
1060 rcvbuf
= &req
->rq_private_buf
;
1062 if (transport
->tcp_flags
& TCP_RCV_COPY_CALLDIR
) {
1064 * Save the RPC direction in the XDR buffer
1066 __be32 calldir
= transport
->tcp_flags
& TCP_RPC_REPLY
?
1067 htonl(RPC_REPLY
) : 0;
1069 memcpy(rcvbuf
->head
[0].iov_base
+ transport
->tcp_copied
,
1070 &calldir
, sizeof(calldir
));
1071 transport
->tcp_copied
+= sizeof(calldir
);
1072 transport
->tcp_flags
&= ~TCP_RCV_COPY_CALLDIR
;
1076 if (len
> transport
->tcp_reclen
- transport
->tcp_offset
) {
1077 struct xdr_skb_reader my_desc
;
1079 len
= transport
->tcp_reclen
- transport
->tcp_offset
;
1080 memcpy(&my_desc
, desc
, sizeof(my_desc
));
1081 my_desc
.count
= len
;
1082 r
= xdr_partial_copy_from_skb(rcvbuf
, transport
->tcp_copied
,
1083 &my_desc
, xdr_skb_read_bits
);
1087 r
= xdr_partial_copy_from_skb(rcvbuf
, transport
->tcp_copied
,
1088 desc
, xdr_skb_read_bits
);
1091 transport
->tcp_copied
+= r
;
1092 transport
->tcp_offset
+= r
;
1095 /* Error when copying to the receive buffer,
1096 * usually because we weren't able to allocate
1097 * additional buffer pages. All we can do now
1098 * is turn off TCP_RCV_COPY_DATA, so the request
1099 * will not receive any additional updates,
1101 * Any remaining data from this record will
1104 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1105 dprintk("RPC: XID %08x truncated request\n",
1106 ntohl(transport
->tcp_xid
));
1107 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1108 "tcp_offset = %u, tcp_reclen = %u\n",
1109 xprt
, transport
->tcp_copied
,
1110 transport
->tcp_offset
, transport
->tcp_reclen
);
1114 dprintk("RPC: XID %08x read %Zd bytes\n",
1115 ntohl(transport
->tcp_xid
), r
);
1116 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1117 "tcp_reclen = %u\n", xprt
, transport
->tcp_copied
,
1118 transport
->tcp_offset
, transport
->tcp_reclen
);
1120 if (transport
->tcp_copied
== req
->rq_private_buf
.buflen
)
1121 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1122 else if (transport
->tcp_offset
== transport
->tcp_reclen
) {
1123 if (transport
->tcp_flags
& TCP_RCV_LAST_FRAG
)
1124 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1131 * Finds the request corresponding to the RPC xid and invokes the common
1132 * tcp read code to read the data.
1134 static inline int xs_tcp_read_reply(struct rpc_xprt
*xprt
,
1135 struct xdr_skb_reader
*desc
)
1137 struct sock_xprt
*transport
=
1138 container_of(xprt
, struct sock_xprt
, xprt
);
1139 struct rpc_rqst
*req
;
1141 dprintk("RPC: read reply XID %08x\n", ntohl(transport
->tcp_xid
));
1143 /* Find and lock the request corresponding to this xid */
1144 spin_lock(&xprt
->transport_lock
);
1145 req
= xprt_lookup_rqst(xprt
, transport
->tcp_xid
);
1147 dprintk("RPC: XID %08x request not found!\n",
1148 ntohl(transport
->tcp_xid
));
1149 spin_unlock(&xprt
->transport_lock
);
1153 xs_tcp_read_common(xprt
, desc
, req
);
1155 if (!(transport
->tcp_flags
& TCP_RCV_COPY_DATA
))
1156 xprt_complete_rqst(req
->rq_task
, transport
->tcp_copied
);
1158 spin_unlock(&xprt
->transport_lock
);
1162 #if defined(CONFIG_NFS_V4_1)
1164 * Obtains an rpc_rqst previously allocated and invokes the common
1165 * tcp read code to read the data. The result is placed in the callback
1167 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1168 * connection and return -1.
1170 static inline int xs_tcp_read_callback(struct rpc_xprt
*xprt
,
1171 struct xdr_skb_reader
*desc
)
1173 struct sock_xprt
*transport
=
1174 container_of(xprt
, struct sock_xprt
, xprt
);
1175 struct rpc_rqst
*req
;
1177 req
= xprt_alloc_bc_request(xprt
);
1179 printk(KERN_WARNING
"Callback slot table overflowed\n");
1180 xprt_force_disconnect(xprt
);
1184 req
->rq_xid
= transport
->tcp_xid
;
1185 dprintk("RPC: read callback XID %08x\n", ntohl(req
->rq_xid
));
1186 xs_tcp_read_common(xprt
, desc
, req
);
1188 if (!(transport
->tcp_flags
& TCP_RCV_COPY_DATA
)) {
1189 struct svc_serv
*bc_serv
= xprt
->bc_serv
;
1192 * Add callback request to callback list. The callback
1193 * service sleeps on the sv_cb_waitq waiting for new
1194 * requests. Wake it up after adding enqueing the
1197 dprintk("RPC: add callback request to list\n");
1198 spin_lock(&bc_serv
->sv_cb_lock
);
1199 list_add(&req
->rq_bc_list
, &bc_serv
->sv_cb_list
);
1200 spin_unlock(&bc_serv
->sv_cb_lock
);
1201 wake_up(&bc_serv
->sv_cb_waitq
);
1204 req
->rq_private_buf
.len
= transport
->tcp_copied
;
1209 static inline int _xs_tcp_read_data(struct rpc_xprt
*xprt
,
1210 struct xdr_skb_reader
*desc
)
1212 struct sock_xprt
*transport
=
1213 container_of(xprt
, struct sock_xprt
, xprt
);
1215 return (transport
->tcp_flags
& TCP_RPC_REPLY
) ?
1216 xs_tcp_read_reply(xprt
, desc
) :
1217 xs_tcp_read_callback(xprt
, desc
);
1220 static inline int _xs_tcp_read_data(struct rpc_xprt
*xprt
,
1221 struct xdr_skb_reader
*desc
)
1223 return xs_tcp_read_reply(xprt
, desc
);
1225 #endif /* CONFIG_NFS_V4_1 */
1228 * Read data off the transport. This can be either an RPC_CALL or an
1229 * RPC_REPLY. Relay the processing to helper functions.
1231 static void xs_tcp_read_data(struct rpc_xprt
*xprt
,
1232 struct xdr_skb_reader
*desc
)
1234 struct sock_xprt
*transport
=
1235 container_of(xprt
, struct sock_xprt
, xprt
);
1237 if (_xs_tcp_read_data(xprt
, desc
) == 0)
1238 xs_tcp_check_fraghdr(transport
);
1241 * The transport_lock protects the request handling.
1242 * There's no need to hold it to update the tcp_flags.
1244 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1248 static inline void xs_tcp_read_discard(struct sock_xprt
*transport
, struct xdr_skb_reader
*desc
)
1252 len
= transport
->tcp_reclen
- transport
->tcp_offset
;
1253 if (len
> desc
->count
)
1256 desc
->offset
+= len
;
1257 transport
->tcp_offset
+= len
;
1258 dprintk("RPC: discarded %Zu bytes\n", len
);
1259 xs_tcp_check_fraghdr(transport
);
1262 static int xs_tcp_data_recv(read_descriptor_t
*rd_desc
, struct sk_buff
*skb
, unsigned int offset
, size_t len
)
1264 struct rpc_xprt
*xprt
= rd_desc
->arg
.data
;
1265 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1266 struct xdr_skb_reader desc
= {
1272 dprintk("RPC: xs_tcp_data_recv started\n");
1274 /* Read in a new fragment marker if necessary */
1275 /* Can we ever really expect to get completely empty fragments? */
1276 if (transport
->tcp_flags
& TCP_RCV_COPY_FRAGHDR
) {
1277 xs_tcp_read_fraghdr(xprt
, &desc
);
1280 /* Read in the xid if necessary */
1281 if (transport
->tcp_flags
& TCP_RCV_COPY_XID
) {
1282 xs_tcp_read_xid(transport
, &desc
);
1285 /* Read in the call/reply flag */
1286 if (transport
->tcp_flags
& TCP_RCV_READ_CALLDIR
) {
1287 xs_tcp_read_calldir(transport
, &desc
);
1290 /* Read in the request data */
1291 if (transport
->tcp_flags
& TCP_RCV_COPY_DATA
) {
1292 xs_tcp_read_data(xprt
, &desc
);
1295 /* Skip over any trailing bytes on short reads */
1296 xs_tcp_read_discard(transport
, &desc
);
1297 } while (desc
.count
);
1298 dprintk("RPC: xs_tcp_data_recv done\n");
1299 return len
- desc
.count
;
1303 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1304 * @sk: socket with data to read
1305 * @bytes: how much data to read
1308 static void xs_tcp_data_ready(struct sock
*sk
, int bytes
)
1310 struct rpc_xprt
*xprt
;
1311 read_descriptor_t rd_desc
;
1314 dprintk("RPC: xs_tcp_data_ready...\n");
1316 read_lock(&sk
->sk_callback_lock
);
1317 if (!(xprt
= xprt_from_sock(sk
)))
1322 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1323 rd_desc
.arg
.data
= xprt
;
1325 rd_desc
.count
= 65536;
1326 read
= tcp_read_sock(sk
, &rd_desc
, xs_tcp_data_recv
);
1329 read_unlock(&sk
->sk_callback_lock
);
1333 * Do the equivalent of linger/linger2 handling for dealing with
1334 * broken servers that don't close the socket in a timely
1337 static void xs_tcp_schedule_linger_timeout(struct rpc_xprt
*xprt
,
1338 unsigned long timeout
)
1340 struct sock_xprt
*transport
;
1342 if (xprt_test_and_set_connecting(xprt
))
1344 set_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1345 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1346 queue_delayed_work(rpciod_workqueue
, &transport
->connect_worker
,
1350 static void xs_tcp_cancel_linger_timeout(struct rpc_xprt
*xprt
)
1352 struct sock_xprt
*transport
;
1354 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1356 if (!test_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
) ||
1357 !cancel_delayed_work(&transport
->connect_worker
))
1359 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1360 xprt_clear_connecting(xprt
);
1363 static void xs_sock_mark_closed(struct rpc_xprt
*xprt
)
1365 smp_mb__before_clear_bit();
1366 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
1367 clear_bit(XPRT_CLOSING
, &xprt
->state
);
1368 smp_mb__after_clear_bit();
1369 /* Mark transport as closed and wake up all pending tasks */
1370 xprt_disconnect_done(xprt
);
1374 * xs_tcp_state_change - callback to handle TCP socket state changes
1375 * @sk: socket whose state has changed
1378 static void xs_tcp_state_change(struct sock
*sk
)
1380 struct rpc_xprt
*xprt
;
1382 read_lock(&sk
->sk_callback_lock
);
1383 if (!(xprt
= xprt_from_sock(sk
)))
1385 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt
);
1386 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1387 sk
->sk_state
, xprt_connected(xprt
),
1388 sock_flag(sk
, SOCK_DEAD
),
1389 sock_flag(sk
, SOCK_ZAPPED
));
1391 switch (sk
->sk_state
) {
1392 case TCP_ESTABLISHED
:
1393 spin_lock_bh(&xprt
->transport_lock
);
1394 if (!xprt_test_and_set_connected(xprt
)) {
1395 struct sock_xprt
*transport
= container_of(xprt
,
1396 struct sock_xprt
, xprt
);
1398 /* Reset TCP record info */
1399 transport
->tcp_offset
= 0;
1400 transport
->tcp_reclen
= 0;
1401 transport
->tcp_copied
= 0;
1402 transport
->tcp_flags
=
1403 TCP_RCV_COPY_FRAGHDR
| TCP_RCV_COPY_XID
;
1405 xprt_wake_pending_tasks(xprt
, -EAGAIN
);
1407 spin_unlock_bh(&xprt
->transport_lock
);
1410 /* The client initiated a shutdown of the socket */
1411 xprt
->connect_cookie
++;
1412 xprt
->reestablish_timeout
= 0;
1413 set_bit(XPRT_CLOSING
, &xprt
->state
);
1414 smp_mb__before_clear_bit();
1415 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1416 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
1417 smp_mb__after_clear_bit();
1418 xs_tcp_schedule_linger_timeout(xprt
, xs_tcp_fin_timeout
);
1420 case TCP_CLOSE_WAIT
:
1421 /* The server initiated a shutdown of the socket */
1422 xprt_force_disconnect(xprt
);
1424 xprt
->connect_cookie
++;
1427 * If the server closed down the connection, make sure that
1428 * we back off before reconnecting
1430 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
1431 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
1434 set_bit(XPRT_CLOSING
, &xprt
->state
);
1435 xs_tcp_schedule_linger_timeout(xprt
, xs_tcp_fin_timeout
);
1436 smp_mb__before_clear_bit();
1437 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1438 smp_mb__after_clear_bit();
1441 xs_tcp_cancel_linger_timeout(xprt
);
1442 xs_sock_mark_closed(xprt
);
1445 read_unlock(&sk
->sk_callback_lock
);
1449 * xs_error_report - callback mainly for catching socket errors
1452 static void xs_error_report(struct sock
*sk
)
1454 struct rpc_xprt
*xprt
;
1456 read_lock(&sk
->sk_callback_lock
);
1457 if (!(xprt
= xprt_from_sock(sk
)))
1459 dprintk("RPC: %s client %p...\n"
1461 __func__
, xprt
, sk
->sk_err
);
1462 xprt_wake_pending_tasks(xprt
, -EAGAIN
);
1464 read_unlock(&sk
->sk_callback_lock
);
1467 static void xs_write_space(struct sock
*sk
)
1469 struct socket
*sock
;
1470 struct rpc_xprt
*xprt
;
1472 if (unlikely(!(sock
= sk
->sk_socket
)))
1474 clear_bit(SOCK_NOSPACE
, &sock
->flags
);
1476 if (unlikely(!(xprt
= xprt_from_sock(sk
))))
1478 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
) == 0)
1481 xprt_write_space(xprt
);
1485 * xs_udp_write_space - callback invoked when socket buffer space
1487 * @sk: socket whose state has changed
1489 * Called when more output buffer space is available for this socket.
1490 * We try not to wake our writers until they can make "significant"
1491 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1492 * with a bunch of small requests.
1494 static void xs_udp_write_space(struct sock
*sk
)
1496 read_lock(&sk
->sk_callback_lock
);
1498 /* from net/core/sock.c:sock_def_write_space */
1499 if (sock_writeable(sk
))
1502 read_unlock(&sk
->sk_callback_lock
);
1506 * xs_tcp_write_space - callback invoked when socket buffer space
1508 * @sk: socket whose state has changed
1510 * Called when more output buffer space is available for this socket.
1511 * We try not to wake our writers until they can make "significant"
1512 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1513 * with a bunch of small requests.
1515 static void xs_tcp_write_space(struct sock
*sk
)
1517 read_lock(&sk
->sk_callback_lock
);
1519 /* from net/core/stream.c:sk_stream_write_space */
1520 if (sk_stream_wspace(sk
) >= sk_stream_min_wspace(sk
))
1523 read_unlock(&sk
->sk_callback_lock
);
1526 static void xs_udp_do_set_buffer_size(struct rpc_xprt
*xprt
)
1528 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1529 struct sock
*sk
= transport
->inet
;
1531 if (transport
->rcvsize
) {
1532 sk
->sk_userlocks
|= SOCK_RCVBUF_LOCK
;
1533 sk
->sk_rcvbuf
= transport
->rcvsize
* xprt
->max_reqs
* 2;
1535 if (transport
->sndsize
) {
1536 sk
->sk_userlocks
|= SOCK_SNDBUF_LOCK
;
1537 sk
->sk_sndbuf
= transport
->sndsize
* xprt
->max_reqs
* 2;
1538 sk
->sk_write_space(sk
);
1543 * xs_udp_set_buffer_size - set send and receive limits
1544 * @xprt: generic transport
1545 * @sndsize: requested size of send buffer, in bytes
1546 * @rcvsize: requested size of receive buffer, in bytes
1548 * Set socket send and receive buffer size limits.
1550 static void xs_udp_set_buffer_size(struct rpc_xprt
*xprt
, size_t sndsize
, size_t rcvsize
)
1552 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1554 transport
->sndsize
= 0;
1556 transport
->sndsize
= sndsize
+ 1024;
1557 transport
->rcvsize
= 0;
1559 transport
->rcvsize
= rcvsize
+ 1024;
1561 xs_udp_do_set_buffer_size(xprt
);
1565 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1566 * @task: task that timed out
1568 * Adjust the congestion window after a retransmit timeout has occurred.
1570 static void xs_udp_timer(struct rpc_task
*task
)
1572 xprt_adjust_cwnd(task
, -ETIMEDOUT
);
1575 static unsigned short xs_get_random_port(void)
1577 unsigned short range
= xprt_max_resvport
- xprt_min_resvport
;
1578 unsigned short rand
= (unsigned short) net_random() % range
;
1579 return rand
+ xprt_min_resvport
;
1583 * xs_set_port - reset the port number in the remote endpoint address
1584 * @xprt: generic transport
1585 * @port: new port number
1588 static void xs_set_port(struct rpc_xprt
*xprt
, unsigned short port
)
1590 struct sockaddr
*addr
= xs_addr(xprt
);
1592 dprintk("RPC: setting port for xprt %p to %u\n", xprt
, port
);
1594 switch (addr
->sa_family
) {
1596 ((struct sockaddr_in
*)addr
)->sin_port
= htons(port
);
1599 ((struct sockaddr_in6
*)addr
)->sin6_port
= htons(port
);
1606 static unsigned short xs_get_srcport(struct sock_xprt
*transport
, struct socket
*sock
)
1608 unsigned short port
= transport
->port
;
1610 if (port
== 0 && transport
->xprt
.resvport
)
1611 port
= xs_get_random_port();
1615 static unsigned short xs_next_srcport(struct sock_xprt
*transport
, struct socket
*sock
, unsigned short port
)
1617 if (transport
->port
!= 0)
1618 transport
->port
= 0;
1619 if (!transport
->xprt
.resvport
)
1621 if (port
<= xprt_min_resvport
|| port
> xprt_max_resvport
)
1622 return xprt_max_resvport
;
1626 static int xs_bind4(struct sock_xprt
*transport
, struct socket
*sock
)
1628 struct sockaddr_in myaddr
= {
1629 .sin_family
= AF_INET
,
1631 struct sockaddr_in
*sa
;
1633 unsigned short port
= xs_get_srcport(transport
, sock
);
1634 unsigned short last
;
1636 sa
= (struct sockaddr_in
*)&transport
->addr
;
1637 myaddr
.sin_addr
= sa
->sin_addr
;
1639 myaddr
.sin_port
= htons(port
);
1640 err
= kernel_bind(sock
, (struct sockaddr
*) &myaddr
,
1645 transport
->port
= port
;
1649 port
= xs_next_srcport(transport
, sock
, port
);
1652 } while (err
== -EADDRINUSE
&& nloop
!= 2);
1653 dprintk("RPC: %s %pI4:%u: %s (%d)\n",
1654 __func__
, &myaddr
.sin_addr
,
1655 port
, err
? "failed" : "ok", err
);
1659 static int xs_bind6(struct sock_xprt
*transport
, struct socket
*sock
)
1661 struct sockaddr_in6 myaddr
= {
1662 .sin6_family
= AF_INET6
,
1664 struct sockaddr_in6
*sa
;
1666 unsigned short port
= xs_get_srcport(transport
, sock
);
1667 unsigned short last
;
1669 sa
= (struct sockaddr_in6
*)&transport
->addr
;
1670 myaddr
.sin6_addr
= sa
->sin6_addr
;
1672 myaddr
.sin6_port
= htons(port
);
1673 err
= kernel_bind(sock
, (struct sockaddr
*) &myaddr
,
1678 transport
->port
= port
;
1682 port
= xs_next_srcport(transport
, sock
, port
);
1685 } while (err
== -EADDRINUSE
&& nloop
!= 2);
1686 dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n",
1687 &myaddr
.sin6_addr
, port
, err
? "failed" : "ok", err
);
1691 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1692 static struct lock_class_key xs_key
[2];
1693 static struct lock_class_key xs_slock_key
[2];
1695 static inline void xs_reclassify_socket4(struct socket
*sock
)
1697 struct sock
*sk
= sock
->sk
;
1699 BUG_ON(sock_owned_by_user(sk
));
1700 sock_lock_init_class_and_name(sk
, "slock-AF_INET-RPC",
1701 &xs_slock_key
[0], "sk_lock-AF_INET-RPC", &xs_key
[0]);
1704 static inline void xs_reclassify_socket6(struct socket
*sock
)
1706 struct sock
*sk
= sock
->sk
;
1708 BUG_ON(sock_owned_by_user(sk
));
1709 sock_lock_init_class_and_name(sk
, "slock-AF_INET6-RPC",
1710 &xs_slock_key
[1], "sk_lock-AF_INET6-RPC", &xs_key
[1]);
1713 static inline void xs_reclassify_socket4(struct socket
*sock
)
1717 static inline void xs_reclassify_socket6(struct socket
*sock
)
1722 static void xs_udp_finish_connecting(struct rpc_xprt
*xprt
, struct socket
*sock
)
1724 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1726 if (!transport
->inet
) {
1727 struct sock
*sk
= sock
->sk
;
1729 write_lock_bh(&sk
->sk_callback_lock
);
1731 xs_save_old_callbacks(transport
, sk
);
1733 sk
->sk_user_data
= xprt
;
1734 sk
->sk_data_ready
= xs_udp_data_ready
;
1735 sk
->sk_write_space
= xs_udp_write_space
;
1736 sk
->sk_error_report
= xs_error_report
;
1737 sk
->sk_no_check
= UDP_CSUM_NORCV
;
1738 sk
->sk_allocation
= GFP_ATOMIC
;
1740 xprt_set_connected(xprt
);
1742 /* Reset to new socket */
1743 transport
->sock
= sock
;
1744 transport
->inet
= sk
;
1746 write_unlock_bh(&sk
->sk_callback_lock
);
1748 xs_udp_do_set_buffer_size(xprt
);
1752 * xs_udp_connect_worker4 - set up a UDP socket
1753 * @work: RPC transport to connect
1755 * Invoked by a work queue tasklet.
1757 static void xs_udp_connect_worker4(struct work_struct
*work
)
1759 struct sock_xprt
*transport
=
1760 container_of(work
, struct sock_xprt
, connect_worker
.work
);
1761 struct rpc_xprt
*xprt
= &transport
->xprt
;
1762 struct socket
*sock
= transport
->sock
;
1763 int err
, status
= -EIO
;
1768 /* Start by resetting any existing state */
1769 xs_reset_transport(transport
);
1771 err
= sock_create_kern(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
);
1773 dprintk("RPC: can't create UDP transport socket (%d).\n", -err
);
1776 xs_reclassify_socket4(sock
);
1778 if (xs_bind4(transport
, sock
)) {
1783 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1784 xprt
, xprt
->address_strings
[RPC_DISPLAY_ALL
]);
1786 xs_udp_finish_connecting(xprt
, sock
);
1789 xprt_clear_connecting(xprt
);
1790 xprt_wake_pending_tasks(xprt
, status
);
1794 * xs_udp_connect_worker6 - set up a UDP socket
1795 * @work: RPC transport to connect
1797 * Invoked by a work queue tasklet.
1799 static void xs_udp_connect_worker6(struct work_struct
*work
)
1801 struct sock_xprt
*transport
=
1802 container_of(work
, struct sock_xprt
, connect_worker
.work
);
1803 struct rpc_xprt
*xprt
= &transport
->xprt
;
1804 struct socket
*sock
= transport
->sock
;
1805 int err
, status
= -EIO
;
1810 /* Start by resetting any existing state */
1811 xs_reset_transport(transport
);
1813 err
= sock_create_kern(PF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
);
1815 dprintk("RPC: can't create UDP transport socket (%d).\n", -err
);
1818 xs_reclassify_socket6(sock
);
1820 if (xs_bind6(transport
, sock
) < 0) {
1825 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1826 xprt
, xprt
->address_strings
[RPC_DISPLAY_ALL
]);
1828 xs_udp_finish_connecting(xprt
, sock
);
1831 xprt_clear_connecting(xprt
);
1832 xprt_wake_pending_tasks(xprt
, status
);
1836 * We need to preserve the port number so the reply cache on the server can
1837 * find our cached RPC replies when we get around to reconnecting.
1839 static void xs_abort_connection(struct rpc_xprt
*xprt
, struct sock_xprt
*transport
)
1842 struct sockaddr any
;
1844 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt
);
1847 * Disconnect the transport socket by doing a connect operation
1848 * with AF_UNSPEC. This should return immediately...
1850 memset(&any
, 0, sizeof(any
));
1851 any
.sa_family
= AF_UNSPEC
;
1852 result
= kernel_connect(transport
->sock
, &any
, sizeof(any
), 0);
1854 xs_sock_mark_closed(xprt
);
1856 dprintk("RPC: AF_UNSPEC connect return code %d\n",
1860 static void xs_tcp_reuse_connection(struct rpc_xprt
*xprt
, struct sock_xprt
*transport
)
1862 unsigned int state
= transport
->inet
->sk_state
;
1864 if (state
== TCP_CLOSE
&& transport
->sock
->state
== SS_UNCONNECTED
)
1866 if ((1 << state
) & (TCPF_ESTABLISHED
|TCPF_SYN_SENT
))
1868 xs_abort_connection(xprt
, transport
);
1871 static int xs_tcp_finish_connecting(struct rpc_xprt
*xprt
, struct socket
*sock
)
1873 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1875 if (!transport
->inet
) {
1876 struct sock
*sk
= sock
->sk
;
1878 write_lock_bh(&sk
->sk_callback_lock
);
1880 xs_save_old_callbacks(transport
, sk
);
1882 sk
->sk_user_data
= xprt
;
1883 sk
->sk_data_ready
= xs_tcp_data_ready
;
1884 sk
->sk_state_change
= xs_tcp_state_change
;
1885 sk
->sk_write_space
= xs_tcp_write_space
;
1886 sk
->sk_error_report
= xs_error_report
;
1887 sk
->sk_allocation
= GFP_ATOMIC
;
1889 /* socket options */
1890 sk
->sk_userlocks
|= SOCK_BINDPORT_LOCK
;
1891 sock_reset_flag(sk
, SOCK_LINGER
);
1892 tcp_sk(sk
)->linger2
= 0;
1893 tcp_sk(sk
)->nonagle
|= TCP_NAGLE_OFF
;
1895 xprt_clear_connected(xprt
);
1897 /* Reset to new socket */
1898 transport
->sock
= sock
;
1899 transport
->inet
= sk
;
1901 write_unlock_bh(&sk
->sk_callback_lock
);
1904 if (!xprt_bound(xprt
))
1907 /* Tell the socket layer to start connecting... */
1908 xprt
->stat
.connect_count
++;
1909 xprt
->stat
.connect_start
= jiffies
;
1910 return kernel_connect(sock
, xs_addr(xprt
), xprt
->addrlen
, O_NONBLOCK
);
1914 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1915 * @xprt: RPC transport to connect
1916 * @transport: socket transport to connect
1917 * @create_sock: function to create a socket of the correct type
1919 * Invoked by a work queue tasklet.
1921 static void xs_tcp_setup_socket(struct rpc_xprt
*xprt
,
1922 struct sock_xprt
*transport
,
1923 struct socket
*(*create_sock
)(struct rpc_xprt
*,
1924 struct sock_xprt
*))
1926 struct socket
*sock
= transport
->sock
;
1933 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1934 sock
= create_sock(xprt
, transport
);
1936 status
= PTR_ERR(sock
);
1942 abort_and_exit
= test_and_clear_bit(XPRT_CONNECTION_ABORT
,
1944 /* "close" the socket, preserving the local port */
1945 xs_tcp_reuse_connection(xprt
, transport
);
1951 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1952 xprt
, xprt
->address_strings
[RPC_DISPLAY_ALL
]);
1954 status
= xs_tcp_finish_connecting(xprt
, sock
);
1955 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1956 xprt
, -status
, xprt_connected(xprt
),
1957 sock
->sk
->sk_state
);
1960 printk("%s: connect returned unhandled error %d\n",
1962 case -EADDRNOTAVAIL
:
1963 /* We're probably in TIME_WAIT. Get rid of existing socket,
1966 set_bit(XPRT_CONNECTION_CLOSE
, &xprt
->state
);
1967 xprt_force_disconnect(xprt
);
1972 /* retry with existing socket, after a delay */
1976 xprt_clear_connecting(xprt
);
1982 xprt_clear_connecting(xprt
);
1983 xprt_wake_pending_tasks(xprt
, status
);
1986 static struct socket
*xs_create_tcp_sock4(struct rpc_xprt
*xprt
,
1987 struct sock_xprt
*transport
)
1989 struct socket
*sock
;
1992 /* start from scratch */
1993 err
= sock_create_kern(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
, &sock
);
1995 dprintk("RPC: can't create TCP transport socket (%d).\n",
1999 xs_reclassify_socket4(sock
);
2001 if (xs_bind4(transport
, sock
) < 0) {
2007 return ERR_PTR(-EIO
);
2011 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
2012 * @work: RPC transport to connect
2014 * Invoked by a work queue tasklet.
2016 static void xs_tcp_connect_worker4(struct work_struct
*work
)
2018 struct sock_xprt
*transport
=
2019 container_of(work
, struct sock_xprt
, connect_worker
.work
);
2020 struct rpc_xprt
*xprt
= &transport
->xprt
;
2022 xs_tcp_setup_socket(xprt
, transport
, xs_create_tcp_sock4
);
2025 static struct socket
*xs_create_tcp_sock6(struct rpc_xprt
*xprt
,
2026 struct sock_xprt
*transport
)
2028 struct socket
*sock
;
2031 /* start from scratch */
2032 err
= sock_create_kern(PF_INET6
, SOCK_STREAM
, IPPROTO_TCP
, &sock
);
2034 dprintk("RPC: can't create TCP transport socket (%d).\n",
2038 xs_reclassify_socket6(sock
);
2040 if (xs_bind6(transport
, sock
) < 0) {
2046 return ERR_PTR(-EIO
);
2050 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
2051 * @work: RPC transport to connect
2053 * Invoked by a work queue tasklet.
2055 static void xs_tcp_connect_worker6(struct work_struct
*work
)
2057 struct sock_xprt
*transport
=
2058 container_of(work
, struct sock_xprt
, connect_worker
.work
);
2059 struct rpc_xprt
*xprt
= &transport
->xprt
;
2061 xs_tcp_setup_socket(xprt
, transport
, xs_create_tcp_sock6
);
2065 * xs_connect - connect a socket to a remote endpoint
2066 * @task: address of RPC task that manages state of connect request
2068 * TCP: If the remote end dropped the connection, delay reconnecting.
2070 * UDP socket connects are synchronous, but we use a work queue anyway
2071 * to guarantee that even unprivileged user processes can set up a
2072 * socket on a privileged port.
2074 * If a UDP socket connect fails, the delay behavior here prevents
2075 * retry floods (hard mounts).
2077 static void xs_connect(struct rpc_task
*task
)
2079 struct rpc_xprt
*xprt
= task
->tk_xprt
;
2080 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2082 if (xprt_test_and_set_connecting(xprt
))
2085 if (transport
->sock
!= NULL
) {
2086 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2088 xprt
, xprt
->reestablish_timeout
/ HZ
);
2089 queue_delayed_work(rpciod_workqueue
,
2090 &transport
->connect_worker
,
2091 xprt
->reestablish_timeout
);
2092 xprt
->reestablish_timeout
<<= 1;
2093 if (xprt
->reestablish_timeout
> XS_TCP_MAX_REEST_TO
)
2094 xprt
->reestablish_timeout
= XS_TCP_MAX_REEST_TO
;
2096 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt
);
2097 queue_delayed_work(rpciod_workqueue
,
2098 &transport
->connect_worker
, 0);
2102 static void xs_tcp_connect(struct rpc_task
*task
)
2104 struct rpc_xprt
*xprt
= task
->tk_xprt
;
2106 /* Exit if we need to wait for socket shutdown to complete */
2107 if (test_bit(XPRT_CLOSING
, &xprt
->state
))
2113 * xs_udp_print_stats - display UDP socket-specifc stats
2114 * @xprt: rpc_xprt struct containing statistics
2118 static void xs_udp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2120 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2122 seq_printf(seq
, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
2124 xprt
->stat
.bind_count
,
2127 xprt
->stat
.bad_xids
,
2129 xprt
->stat
.bklog_u
);
2133 * xs_tcp_print_stats - display TCP socket-specifc stats
2134 * @xprt: rpc_xprt struct containing statistics
2138 static void xs_tcp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2140 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2143 if (xprt_connected(xprt
))
2144 idle_time
= (long)(jiffies
- xprt
->last_used
) / HZ
;
2146 seq_printf(seq
, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
2148 xprt
->stat
.bind_count
,
2149 xprt
->stat
.connect_count
,
2150 xprt
->stat
.connect_time
,
2154 xprt
->stat
.bad_xids
,
2156 xprt
->stat
.bklog_u
);
2159 static struct rpc_xprt_ops xs_udp_ops
= {
2160 .set_buffer_size
= xs_udp_set_buffer_size
,
2161 .reserve_xprt
= xprt_reserve_xprt_cong
,
2162 .release_xprt
= xprt_release_xprt_cong
,
2163 .rpcbind
= rpcb_getport_async
,
2164 .set_port
= xs_set_port
,
2165 .connect
= xs_connect
,
2166 .buf_alloc
= rpc_malloc
,
2167 .buf_free
= rpc_free
,
2168 .send_request
= xs_udp_send_request
,
2169 .set_retrans_timeout
= xprt_set_retrans_timeout_rtt
,
2170 .timer
= xs_udp_timer
,
2171 .release_request
= xprt_release_rqst_cong
,
2173 .destroy
= xs_destroy
,
2174 .print_stats
= xs_udp_print_stats
,
2177 static struct rpc_xprt_ops xs_tcp_ops
= {
2178 .reserve_xprt
= xprt_reserve_xprt
,
2179 .release_xprt
= xs_tcp_release_xprt
,
2180 .rpcbind
= rpcb_getport_async
,
2181 .set_port
= xs_set_port
,
2182 .connect
= xs_tcp_connect
,
2183 .buf_alloc
= rpc_malloc
,
2184 .buf_free
= rpc_free
,
2185 .send_request
= xs_tcp_send_request
,
2186 .set_retrans_timeout
= xprt_set_retrans_timeout_def
,
2187 #if defined(CONFIG_NFS_V4_1)
2188 .release_request
= bc_release_request
,
2189 #endif /* CONFIG_NFS_V4_1 */
2190 .close
= xs_tcp_close
,
2191 .destroy
= xs_destroy
,
2192 .print_stats
= xs_tcp_print_stats
,
2195 static struct rpc_xprt
*xs_setup_xprt(struct xprt_create
*args
,
2196 unsigned int slot_table_size
)
2198 struct rpc_xprt
*xprt
;
2199 struct sock_xprt
*new;
2201 if (args
->addrlen
> sizeof(xprt
->addr
)) {
2202 dprintk("RPC: xs_setup_xprt: address too large\n");
2203 return ERR_PTR(-EBADF
);
2206 new = kzalloc(sizeof(*new), GFP_KERNEL
);
2208 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2210 return ERR_PTR(-ENOMEM
);
2214 xprt
->max_reqs
= slot_table_size
;
2215 xprt
->slot
= kcalloc(xprt
->max_reqs
, sizeof(struct rpc_rqst
), GFP_KERNEL
);
2216 if (xprt
->slot
== NULL
) {
2218 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
2220 return ERR_PTR(-ENOMEM
);
2223 memcpy(&xprt
->addr
, args
->dstaddr
, args
->addrlen
);
2224 xprt
->addrlen
= args
->addrlen
;
2226 memcpy(&new->addr
, args
->srcaddr
, args
->addrlen
);
2231 static const struct rpc_timeout xs_udp_default_timeout
= {
2232 .to_initval
= 5 * HZ
,
2233 .to_maxval
= 30 * HZ
,
2234 .to_increment
= 5 * HZ
,
2239 * xs_setup_udp - Set up transport to use a UDP socket
2240 * @args: rpc transport creation arguments
2243 static struct rpc_xprt
*xs_setup_udp(struct xprt_create
*args
)
2245 struct sockaddr
*addr
= args
->dstaddr
;
2246 struct rpc_xprt
*xprt
;
2247 struct sock_xprt
*transport
;
2249 xprt
= xs_setup_xprt(args
, xprt_udp_slot_table_entries
);
2252 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2254 xprt
->prot
= IPPROTO_UDP
;
2256 /* XXX: header size can vary due to auth type, IPv6, etc. */
2257 xprt
->max_payload
= (1U << 16) - (MAX_HEADER
<< 3);
2259 xprt
->bind_timeout
= XS_BIND_TO
;
2260 xprt
->connect_timeout
= XS_UDP_CONN_TO
;
2261 xprt
->reestablish_timeout
= XS_UDP_REEST_TO
;
2262 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2264 xprt
->ops
= &xs_udp_ops
;
2266 xprt
->timeout
= &xs_udp_default_timeout
;
2268 switch (addr
->sa_family
) {
2270 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
2271 xprt_set_bound(xprt
);
2273 INIT_DELAYED_WORK(&transport
->connect_worker
,
2274 xs_udp_connect_worker4
);
2275 xs_format_ipv4_peer_addresses(xprt
, "udp", RPCBIND_NETID_UDP
);
2278 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
2279 xprt_set_bound(xprt
);
2281 INIT_DELAYED_WORK(&transport
->connect_worker
,
2282 xs_udp_connect_worker6
);
2283 xs_format_ipv6_peer_addresses(xprt
, "udp", RPCBIND_NETID_UDP6
);
2287 return ERR_PTR(-EAFNOSUPPORT
);
2290 dprintk("RPC: set up transport to address %s\n",
2291 xprt
->address_strings
[RPC_DISPLAY_ALL
]);
2293 if (try_module_get(THIS_MODULE
))
2298 return ERR_PTR(-EINVAL
);
2301 static const struct rpc_timeout xs_tcp_default_timeout
= {
2302 .to_initval
= 60 * HZ
,
2303 .to_maxval
= 60 * HZ
,
2308 * xs_setup_tcp - Set up transport to use a TCP socket
2309 * @args: rpc transport creation arguments
2312 static struct rpc_xprt
*xs_setup_tcp(struct xprt_create
*args
)
2314 struct sockaddr
*addr
= args
->dstaddr
;
2315 struct rpc_xprt
*xprt
;
2316 struct sock_xprt
*transport
;
2318 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
);
2321 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2323 xprt
->prot
= IPPROTO_TCP
;
2324 xprt
->tsh_size
= sizeof(rpc_fraghdr
) / sizeof(u32
);
2325 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
2327 xprt
->bind_timeout
= XS_BIND_TO
;
2328 xprt
->connect_timeout
= XS_TCP_CONN_TO
;
2329 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2330 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2332 xprt
->ops
= &xs_tcp_ops
;
2333 xprt
->timeout
= &xs_tcp_default_timeout
;
2335 switch (addr
->sa_family
) {
2337 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
2338 xprt_set_bound(xprt
);
2340 INIT_DELAYED_WORK(&transport
->connect_worker
, xs_tcp_connect_worker4
);
2341 xs_format_ipv4_peer_addresses(xprt
, "tcp", RPCBIND_NETID_TCP
);
2344 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
2345 xprt_set_bound(xprt
);
2347 INIT_DELAYED_WORK(&transport
->connect_worker
, xs_tcp_connect_worker6
);
2348 xs_format_ipv6_peer_addresses(xprt
, "tcp", RPCBIND_NETID_TCP6
);
2352 return ERR_PTR(-EAFNOSUPPORT
);
2355 dprintk("RPC: set up transport to address %s\n",
2356 xprt
->address_strings
[RPC_DISPLAY_ALL
]);
2358 if (try_module_get(THIS_MODULE
))
2363 return ERR_PTR(-EINVAL
);
2366 static struct xprt_class xs_udp_transport
= {
2367 .list
= LIST_HEAD_INIT(xs_udp_transport
.list
),
2369 .owner
= THIS_MODULE
,
2370 .ident
= IPPROTO_UDP
,
2371 .setup
= xs_setup_udp
,
2374 static struct xprt_class xs_tcp_transport
= {
2375 .list
= LIST_HEAD_INIT(xs_tcp_transport
.list
),
2377 .owner
= THIS_MODULE
,
2378 .ident
= IPPROTO_TCP
,
2379 .setup
= xs_setup_tcp
,
2383 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2386 int init_socket_xprt(void)
2389 if (!sunrpc_table_header
)
2390 sunrpc_table_header
= register_sysctl_table(sunrpc_table
);
2393 xprt_register_transport(&xs_udp_transport
);
2394 xprt_register_transport(&xs_tcp_transport
);
2400 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
2403 void cleanup_socket_xprt(void)
2406 if (sunrpc_table_header
) {
2407 unregister_sysctl_table(sunrpc_table_header
);
2408 sunrpc_table_header
= NULL
;
2412 xprt_unregister_transport(&xs_udp_transport
);
2413 xprt_unregister_transport(&xs_tcp_transport
);