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/svcsock.h>
36 #include <linux/sunrpc/xprtsock.h>
37 #include <linux/file.h>
38 #ifdef CONFIG_NFS_V4_1
39 #include <linux/sunrpc/bc_xprt.h>
43 #include <net/checksum.h>
51 unsigned int xprt_udp_slot_table_entries
= RPC_DEF_SLOT_TABLE
;
52 unsigned int xprt_tcp_slot_table_entries
= RPC_DEF_SLOT_TABLE
;
54 unsigned int xprt_min_resvport
= RPC_DEF_MIN_RESVPORT
;
55 unsigned int xprt_max_resvport
= RPC_DEF_MAX_RESVPORT
;
57 #define XS_TCP_LINGER_TO (15U * HZ)
58 static unsigned int xs_tcp_fin_timeout __read_mostly
= XS_TCP_LINGER_TO
;
61 * We can register our own files under /proc/sys/sunrpc by
62 * calling register_sysctl_table() again. The files in that
63 * directory become the union of all files registered there.
65 * We simply need to make sure that we don't collide with
66 * someone else's file names!
71 static unsigned int min_slot_table_size
= RPC_MIN_SLOT_TABLE
;
72 static unsigned int max_slot_table_size
= RPC_MAX_SLOT_TABLE
;
73 static unsigned int xprt_min_resvport_limit
= RPC_MIN_RESVPORT
;
74 static unsigned int xprt_max_resvport_limit
= RPC_MAX_RESVPORT
;
76 static struct ctl_table_header
*sunrpc_table_header
;
79 * FIXME: changing the UDP slot table size should also resize the UDP
80 * socket buffers for existing UDP transports
82 static ctl_table xs_tunables_table
[] = {
84 .procname
= "udp_slot_table_entries",
85 .data
= &xprt_udp_slot_table_entries
,
86 .maxlen
= sizeof(unsigned int),
88 .proc_handler
= proc_dointvec_minmax
,
89 .extra1
= &min_slot_table_size
,
90 .extra2
= &max_slot_table_size
93 .procname
= "tcp_slot_table_entries",
94 .data
= &xprt_tcp_slot_table_entries
,
95 .maxlen
= sizeof(unsigned int),
97 .proc_handler
= proc_dointvec_minmax
,
98 .extra1
= &min_slot_table_size
,
99 .extra2
= &max_slot_table_size
102 .procname
= "min_resvport",
103 .data
= &xprt_min_resvport
,
104 .maxlen
= sizeof(unsigned int),
106 .proc_handler
= proc_dointvec_minmax
,
107 .extra1
= &xprt_min_resvport_limit
,
108 .extra2
= &xprt_max_resvport_limit
111 .procname
= "max_resvport",
112 .data
= &xprt_max_resvport
,
113 .maxlen
= sizeof(unsigned int),
115 .proc_handler
= proc_dointvec_minmax
,
116 .extra1
= &xprt_min_resvport_limit
,
117 .extra2
= &xprt_max_resvport_limit
120 .procname
= "tcp_fin_timeout",
121 .data
= &xs_tcp_fin_timeout
,
122 .maxlen
= sizeof(xs_tcp_fin_timeout
),
124 .proc_handler
= proc_dointvec_jiffies
,
129 static ctl_table sunrpc_table
[] = {
131 .procname
= "sunrpc",
133 .child
= xs_tunables_table
141 * Time out for an RPC UDP socket connect. UDP socket connects are
142 * synchronous, but we set a timeout anyway in case of resource
143 * exhaustion on the local host.
145 #define XS_UDP_CONN_TO (5U * HZ)
148 * Wait duration for an RPC TCP connection to be established. Solaris
149 * NFS over TCP uses 60 seconds, for example, which is in line with how
150 * long a server takes to reboot.
152 #define XS_TCP_CONN_TO (60U * HZ)
155 * Wait duration for a reply from the RPC portmapper.
157 #define XS_BIND_TO (60U * HZ)
160 * Delay if a UDP socket connect error occurs. This is most likely some
161 * kind of resource problem on the local host.
163 #define XS_UDP_REEST_TO (2U * HZ)
166 * The reestablish timeout allows clients to delay for a bit before attempting
167 * to reconnect to a server that just dropped our connection.
169 * We implement an exponential backoff when trying to reestablish a TCP
170 * transport connection with the server. Some servers like to drop a TCP
171 * connection when they are overworked, so we start with a short timeout and
172 * increase over time if the server is down or not responding.
174 #define XS_TCP_INIT_REEST_TO (3U * HZ)
175 #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
178 * TCP idle timeout; client drops the transport socket if it is idle
179 * for this long. Note that we also timeout UDP sockets to prevent
180 * holding port numbers when there is no RPC traffic.
182 #define XS_IDLE_DISC_TO (5U * 60 * HZ)
185 # undef RPC_DEBUG_DATA
186 # define RPCDBG_FACILITY RPCDBG_TRANS
189 #ifdef RPC_DEBUG_DATA
190 static void xs_pktdump(char *msg
, u32
*packet
, unsigned int count
)
192 u8
*buf
= (u8
*) packet
;
195 dprintk("RPC: %s\n", msg
);
196 for (j
= 0; j
< count
&& j
< 128; j
+= 4) {
200 dprintk("0x%04x ", j
);
202 dprintk("%02x%02x%02x%02x ",
203 buf
[j
], buf
[j
+1], buf
[j
+2], buf
[j
+3]);
208 static inline void xs_pktdump(char *msg
, u32
*packet
, unsigned int count
)
215 struct rpc_xprt xprt
;
220 struct socket
* sock
;
224 * State of TCP reply receive
232 unsigned long tcp_copied
,
236 * Connection of transports
238 struct delayed_work connect_worker
;
239 struct sockaddr_storage srcaddr
;
240 unsigned short srcport
;
243 * UDP socket buffer size parameters
249 * Saved socket callback addresses
251 void (*old_data_ready
)(struct sock
*, int);
252 void (*old_state_change
)(struct sock
*);
253 void (*old_write_space
)(struct sock
*);
254 void (*old_error_report
)(struct sock
*);
258 * TCP receive state flags
260 #define TCP_RCV_LAST_FRAG (1UL << 0)
261 #define TCP_RCV_COPY_FRAGHDR (1UL << 1)
262 #define TCP_RCV_COPY_XID (1UL << 2)
263 #define TCP_RCV_COPY_DATA (1UL << 3)
264 #define TCP_RCV_READ_CALLDIR (1UL << 4)
265 #define TCP_RCV_COPY_CALLDIR (1UL << 5)
270 #define TCP_RPC_REPLY (1UL << 6)
272 static inline struct sockaddr
*xs_addr(struct rpc_xprt
*xprt
)
274 return (struct sockaddr
*) &xprt
->addr
;
277 static inline struct sockaddr_in
*xs_addr_in(struct rpc_xprt
*xprt
)
279 return (struct sockaddr_in
*) &xprt
->addr
;
282 static inline struct sockaddr_in6
*xs_addr_in6(struct rpc_xprt
*xprt
)
284 return (struct sockaddr_in6
*) &xprt
->addr
;
287 static void xs_format_common_peer_addresses(struct rpc_xprt
*xprt
)
289 struct sockaddr
*sap
= xs_addr(xprt
);
290 struct sockaddr_in6
*sin6
;
291 struct sockaddr_in
*sin
;
294 (void)rpc_ntop(sap
, buf
, sizeof(buf
));
295 xprt
->address_strings
[RPC_DISPLAY_ADDR
] = kstrdup(buf
, GFP_KERNEL
);
297 switch (sap
->sa_family
) {
299 sin
= xs_addr_in(xprt
);
300 (void)snprintf(buf
, sizeof(buf
), "%02x%02x%02x%02x",
301 NIPQUAD(sin
->sin_addr
.s_addr
));
304 sin6
= xs_addr_in6(xprt
);
305 (void)snprintf(buf
, sizeof(buf
), "%pi6", &sin6
->sin6_addr
);
310 xprt
->address_strings
[RPC_DISPLAY_HEX_ADDR
] = kstrdup(buf
, GFP_KERNEL
);
313 static void xs_format_common_peer_ports(struct rpc_xprt
*xprt
)
315 struct sockaddr
*sap
= xs_addr(xprt
);
318 (void)snprintf(buf
, sizeof(buf
), "%u", rpc_get_port(sap
));
319 xprt
->address_strings
[RPC_DISPLAY_PORT
] = kstrdup(buf
, GFP_KERNEL
);
321 (void)snprintf(buf
, sizeof(buf
), "%4hx", rpc_get_port(sap
));
322 xprt
->address_strings
[RPC_DISPLAY_HEX_PORT
] = kstrdup(buf
, GFP_KERNEL
);
325 static void xs_format_peer_addresses(struct rpc_xprt
*xprt
,
326 const char *protocol
,
329 xprt
->address_strings
[RPC_DISPLAY_PROTO
] = protocol
;
330 xprt
->address_strings
[RPC_DISPLAY_NETID
] = netid
;
331 xs_format_common_peer_addresses(xprt
);
332 xs_format_common_peer_ports(xprt
);
335 static void xs_update_peer_port(struct rpc_xprt
*xprt
)
337 kfree(xprt
->address_strings
[RPC_DISPLAY_HEX_PORT
]);
338 kfree(xprt
->address_strings
[RPC_DISPLAY_PORT
]);
340 xs_format_common_peer_ports(xprt
);
343 static void xs_free_peer_addresses(struct rpc_xprt
*xprt
)
347 for (i
= 0; i
< RPC_DISPLAY_MAX
; i
++)
349 case RPC_DISPLAY_PROTO
:
350 case RPC_DISPLAY_NETID
:
353 kfree(xprt
->address_strings
[i
]);
357 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
359 static int xs_send_kvec(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
, struct kvec
*vec
, unsigned int base
, int more
)
361 struct msghdr msg
= {
363 .msg_namelen
= addrlen
,
364 .msg_flags
= XS_SENDMSG_FLAGS
| (more
? MSG_MORE
: 0),
367 .iov_base
= vec
->iov_base
+ base
,
368 .iov_len
= vec
->iov_len
- base
,
371 if (iov
.iov_len
!= 0)
372 return kernel_sendmsg(sock
, &msg
, &iov
, 1, iov
.iov_len
);
373 return kernel_sendmsg(sock
, &msg
, NULL
, 0, 0);
376 static int xs_send_pagedata(struct socket
*sock
, struct xdr_buf
*xdr
, unsigned int base
, int more
)
379 unsigned int remainder
;
382 remainder
= xdr
->page_len
- base
;
383 base
+= xdr
->page_base
;
384 ppage
= xdr
->pages
+ (base
>> PAGE_SHIFT
);
387 unsigned int len
= min_t(unsigned int, PAGE_SIZE
- base
, remainder
);
388 int flags
= XS_SENDMSG_FLAGS
;
391 if (remainder
!= 0 || more
)
393 err
= sock
->ops
->sendpage(sock
, *ppage
, base
, len
, flags
);
394 if (remainder
== 0 || err
!= len
)
408 * xs_sendpages - write pages directly to a socket
409 * @sock: socket to send on
410 * @addr: UDP only -- address of destination
411 * @addrlen: UDP only -- length of destination address
412 * @xdr: buffer containing this request
413 * @base: starting position in the buffer
416 static int xs_sendpages(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
, struct xdr_buf
*xdr
, unsigned int base
)
418 unsigned int remainder
= xdr
->len
- base
;
424 clear_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
);
430 if (base
< xdr
->head
[0].iov_len
|| addr
!= NULL
) {
431 unsigned int len
= xdr
->head
[0].iov_len
- base
;
433 err
= xs_send_kvec(sock
, addr
, addrlen
, &xdr
->head
[0], base
, remainder
!= 0);
434 if (remainder
== 0 || err
!= len
)
439 base
-= xdr
->head
[0].iov_len
;
441 if (base
< xdr
->page_len
) {
442 unsigned int len
= xdr
->page_len
- base
;
444 err
= xs_send_pagedata(sock
, xdr
, base
, remainder
!= 0);
445 if (remainder
== 0 || err
!= len
)
450 base
-= xdr
->page_len
;
452 if (base
>= xdr
->tail
[0].iov_len
)
454 err
= xs_send_kvec(sock
, NULL
, 0, &xdr
->tail
[0], base
, 0);
463 static void xs_nospace_callback(struct rpc_task
*task
)
465 struct sock_xprt
*transport
= container_of(task
->tk_rqstp
->rq_xprt
, struct sock_xprt
, xprt
);
467 transport
->inet
->sk_write_pending
--;
468 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
472 * xs_nospace - place task on wait queue if transmit was incomplete
473 * @task: task to put to sleep
476 static int xs_nospace(struct rpc_task
*task
)
478 struct rpc_rqst
*req
= task
->tk_rqstp
;
479 struct rpc_xprt
*xprt
= req
->rq_xprt
;
480 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
483 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
484 task
->tk_pid
, req
->rq_slen
- req
->rq_bytes_sent
,
487 /* Protect against races with write_space */
488 spin_lock_bh(&xprt
->transport_lock
);
490 /* Don't race with disconnect */
491 if (xprt_connected(xprt
)) {
492 if (test_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
)) {
495 * Notify TCP that we're limited by the application
498 set_bit(SOCK_NOSPACE
, &transport
->sock
->flags
);
499 transport
->inet
->sk_write_pending
++;
500 /* ...and wait for more buffer space */
501 xprt_wait_for_buffer_space(task
, xs_nospace_callback
);
504 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
508 spin_unlock_bh(&xprt
->transport_lock
);
513 * xs_udp_send_request - write an RPC request to a UDP socket
514 * @task: address of RPC task that manages the state of an RPC request
517 * 0: The request has been sent
518 * EAGAIN: The socket was blocked, please call again later to
519 * complete the request
520 * ENOTCONN: Caller needs to invoke connect logic then call again
521 * other: Some other error occured, the request was not sent
523 static int xs_udp_send_request(struct rpc_task
*task
)
525 struct rpc_rqst
*req
= task
->tk_rqstp
;
526 struct rpc_xprt
*xprt
= req
->rq_xprt
;
527 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
528 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
531 xs_pktdump("packet data:",
532 req
->rq_svec
->iov_base
,
533 req
->rq_svec
->iov_len
);
535 if (!xprt_bound(xprt
))
537 status
= xs_sendpages(transport
->sock
,
542 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
543 xdr
->len
- req
->rq_bytes_sent
, status
);
546 task
->tk_bytes_sent
+= status
;
547 if (status
>= req
->rq_slen
)
549 /* Still some bytes left; set up for a retry later. */
552 if (!transport
->sock
)
558 /* Should we call xs_close() here? */
561 status
= xs_nospace(task
);
564 dprintk("RPC: sendmsg returned unrecognized error %d\n",
569 /* When the server has died, an ICMP port unreachable message
570 * prompts ECONNREFUSED. */
571 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
578 * xs_tcp_shutdown - gracefully shut down a TCP socket
581 * Initiates a graceful shutdown of the TCP socket by calling the
582 * equivalent of shutdown(SHUT_WR);
584 static void xs_tcp_shutdown(struct rpc_xprt
*xprt
)
586 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
587 struct socket
*sock
= transport
->sock
;
590 kernel_sock_shutdown(sock
, SHUT_WR
);
593 static inline void xs_encode_tcp_record_marker(struct xdr_buf
*buf
)
595 u32 reclen
= buf
->len
- sizeof(rpc_fraghdr
);
596 rpc_fraghdr
*base
= buf
->head
[0].iov_base
;
597 *base
= htonl(RPC_LAST_STREAM_FRAGMENT
| reclen
);
601 * xs_tcp_send_request - write an RPC request to a TCP socket
602 * @task: address of RPC task that manages the state of an RPC request
605 * 0: The request has been sent
606 * EAGAIN: The socket was blocked, please call again later to
607 * complete the request
608 * ENOTCONN: Caller needs to invoke connect logic then call again
609 * other: Some other error occured, the request was not sent
611 * XXX: In the case of soft timeouts, should we eventually give up
612 * if sendmsg is not able to make progress?
614 static int xs_tcp_send_request(struct rpc_task
*task
)
616 struct rpc_rqst
*req
= task
->tk_rqstp
;
617 struct rpc_xprt
*xprt
= req
->rq_xprt
;
618 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
619 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
622 xs_encode_tcp_record_marker(&req
->rq_snd_buf
);
624 xs_pktdump("packet data:",
625 req
->rq_svec
->iov_base
,
626 req
->rq_svec
->iov_len
);
628 /* Continue transmitting the packet/record. We must be careful
629 * to cope with writespace callbacks arriving _after_ we have
630 * called sendmsg(). */
632 status
= xs_sendpages(transport
->sock
,
633 NULL
, 0, xdr
, req
->rq_bytes_sent
);
635 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
636 xdr
->len
- req
->rq_bytes_sent
, status
);
638 if (unlikely(status
< 0))
641 /* If we've sent the entire packet, immediately
642 * reset the count of bytes sent. */
643 req
->rq_bytes_sent
+= status
;
644 task
->tk_bytes_sent
+= status
;
645 if (likely(req
->rq_bytes_sent
>= req
->rq_slen
)) {
646 req
->rq_bytes_sent
= 0;
655 if (!transport
->sock
)
661 /* Should we call xs_close() here? */
664 status
= xs_nospace(task
);
667 dprintk("RPC: sendmsg returned unrecognized error %d\n",
671 xs_tcp_shutdown(xprt
);
674 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
681 * xs_tcp_release_xprt - clean up after a tcp transmission
685 * This cleans up if an error causes us to abort the transmission of a request.
686 * In this case, the socket may need to be reset in order to avoid confusing
689 static void xs_tcp_release_xprt(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
691 struct rpc_rqst
*req
;
693 if (task
!= xprt
->snd_task
)
697 req
= task
->tk_rqstp
;
698 if (req
->rq_bytes_sent
== 0)
700 if (req
->rq_bytes_sent
== req
->rq_snd_buf
.len
)
702 set_bit(XPRT_CLOSE_WAIT
, &task
->tk_xprt
->state
);
704 xprt_release_xprt(xprt
, task
);
707 static void xs_save_old_callbacks(struct sock_xprt
*transport
, struct sock
*sk
)
709 transport
->old_data_ready
= sk
->sk_data_ready
;
710 transport
->old_state_change
= sk
->sk_state_change
;
711 transport
->old_write_space
= sk
->sk_write_space
;
712 transport
->old_error_report
= sk
->sk_error_report
;
715 static void xs_restore_old_callbacks(struct sock_xprt
*transport
, struct sock
*sk
)
717 sk
->sk_data_ready
= transport
->old_data_ready
;
718 sk
->sk_state_change
= transport
->old_state_change
;
719 sk
->sk_write_space
= transport
->old_write_space
;
720 sk
->sk_error_report
= transport
->old_error_report
;
723 static void xs_reset_transport(struct sock_xprt
*transport
)
725 struct socket
*sock
= transport
->sock
;
726 struct sock
*sk
= transport
->inet
;
731 write_lock_bh(&sk
->sk_callback_lock
);
732 transport
->inet
= NULL
;
733 transport
->sock
= NULL
;
735 sk
->sk_user_data
= NULL
;
737 xs_restore_old_callbacks(transport
, sk
);
738 write_unlock_bh(&sk
->sk_callback_lock
);
746 * xs_close - close a socket
749 * This is used when all requests are complete; ie, no DRC state remains
750 * on the server we want to save.
752 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
753 * xs_reset_transport() zeroing the socket from underneath a writer.
755 static void xs_close(struct rpc_xprt
*xprt
)
757 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
759 dprintk("RPC: xs_close xprt %p\n", xprt
);
761 xs_reset_transport(transport
);
762 xprt
->reestablish_timeout
= 0;
764 smp_mb__before_clear_bit();
765 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
766 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
767 clear_bit(XPRT_CLOSING
, &xprt
->state
);
768 smp_mb__after_clear_bit();
769 xprt_disconnect_done(xprt
);
772 static void xs_tcp_close(struct rpc_xprt
*xprt
)
774 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE
, &xprt
->state
))
777 xs_tcp_shutdown(xprt
);
781 * xs_destroy - prepare to shutdown a transport
782 * @xprt: doomed transport
785 static void xs_destroy(struct rpc_xprt
*xprt
)
787 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
789 dprintk("RPC: xs_destroy xprt %p\n", xprt
);
791 cancel_rearming_delayed_work(&transport
->connect_worker
);
794 xs_free_peer_addresses(xprt
);
797 module_put(THIS_MODULE
);
800 static inline struct rpc_xprt
*xprt_from_sock(struct sock
*sk
)
802 return (struct rpc_xprt
*) sk
->sk_user_data
;
806 * xs_udp_data_ready - "data ready" callback for UDP sockets
807 * @sk: socket with data to read
808 * @len: how much data to read
811 static void xs_udp_data_ready(struct sock
*sk
, int len
)
813 struct rpc_task
*task
;
814 struct rpc_xprt
*xprt
;
815 struct rpc_rqst
*rovr
;
817 int err
, repsize
, copied
;
821 read_lock(&sk
->sk_callback_lock
);
822 dprintk("RPC: xs_udp_data_ready...\n");
823 if (!(xprt
= xprt_from_sock(sk
)))
826 if ((skb
= skb_recv_datagram(sk
, 0, 1, &err
)) == NULL
)
832 repsize
= skb
->len
- sizeof(struct udphdr
);
834 dprintk("RPC: impossible RPC reply size %d!\n", repsize
);
838 /* Copy the XID from the skb... */
839 xp
= skb_header_pointer(skb
, sizeof(struct udphdr
),
840 sizeof(_xid
), &_xid
);
844 /* Look up and lock the request corresponding to the given XID */
845 spin_lock(&xprt
->transport_lock
);
846 rovr
= xprt_lookup_rqst(xprt
, *xp
);
849 task
= rovr
->rq_task
;
851 if ((copied
= rovr
->rq_private_buf
.buflen
) > repsize
)
854 /* Suck it into the iovec, verify checksum if not done by hw. */
855 if (csum_partial_copy_to_xdr(&rovr
->rq_private_buf
, skb
)) {
856 UDPX_INC_STATS_BH(sk
, UDP_MIB_INERRORS
);
860 UDPX_INC_STATS_BH(sk
, UDP_MIB_INDATAGRAMS
);
862 /* Something worked... */
863 dst_confirm(skb_dst(skb
));
865 xprt_adjust_cwnd(task
, copied
);
866 xprt_update_rtt(task
);
867 xprt_complete_rqst(task
, copied
);
870 spin_unlock(&xprt
->transport_lock
);
872 skb_free_datagram(sk
, skb
);
874 read_unlock(&sk
->sk_callback_lock
);
877 static inline void xs_tcp_read_fraghdr(struct rpc_xprt
*xprt
, struct xdr_skb_reader
*desc
)
879 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
883 p
= ((char *) &transport
->tcp_fraghdr
) + transport
->tcp_offset
;
884 len
= sizeof(transport
->tcp_fraghdr
) - transport
->tcp_offset
;
885 used
= xdr_skb_read_bits(desc
, p
, len
);
886 transport
->tcp_offset
+= used
;
890 transport
->tcp_reclen
= ntohl(transport
->tcp_fraghdr
);
891 if (transport
->tcp_reclen
& RPC_LAST_STREAM_FRAGMENT
)
892 transport
->tcp_flags
|= TCP_RCV_LAST_FRAG
;
894 transport
->tcp_flags
&= ~TCP_RCV_LAST_FRAG
;
895 transport
->tcp_reclen
&= RPC_FRAGMENT_SIZE_MASK
;
897 transport
->tcp_flags
&= ~TCP_RCV_COPY_FRAGHDR
;
898 transport
->tcp_offset
= 0;
900 /* Sanity check of the record length */
901 if (unlikely(transport
->tcp_reclen
< 8)) {
902 dprintk("RPC: invalid TCP record fragment length\n");
903 xprt_force_disconnect(xprt
);
906 dprintk("RPC: reading TCP record fragment of length %d\n",
907 transport
->tcp_reclen
);
910 static void xs_tcp_check_fraghdr(struct sock_xprt
*transport
)
912 if (transport
->tcp_offset
== transport
->tcp_reclen
) {
913 transport
->tcp_flags
|= TCP_RCV_COPY_FRAGHDR
;
914 transport
->tcp_offset
= 0;
915 if (transport
->tcp_flags
& TCP_RCV_LAST_FRAG
) {
916 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
917 transport
->tcp_flags
|= TCP_RCV_COPY_XID
;
918 transport
->tcp_copied
= 0;
923 static inline void xs_tcp_read_xid(struct sock_xprt
*transport
, struct xdr_skb_reader
*desc
)
928 len
= sizeof(transport
->tcp_xid
) - transport
->tcp_offset
;
929 dprintk("RPC: reading XID (%Zu bytes)\n", len
);
930 p
= ((char *) &transport
->tcp_xid
) + transport
->tcp_offset
;
931 used
= xdr_skb_read_bits(desc
, p
, len
);
932 transport
->tcp_offset
+= used
;
935 transport
->tcp_flags
&= ~TCP_RCV_COPY_XID
;
936 transport
->tcp_flags
|= TCP_RCV_READ_CALLDIR
;
937 transport
->tcp_copied
= 4;
938 dprintk("RPC: reading %s XID %08x\n",
939 (transport
->tcp_flags
& TCP_RPC_REPLY
) ? "reply for"
941 ntohl(transport
->tcp_xid
));
942 xs_tcp_check_fraghdr(transport
);
945 static inline void xs_tcp_read_calldir(struct sock_xprt
*transport
,
946 struct xdr_skb_reader
*desc
)
953 * We want transport->tcp_offset to be 8 at the end of this routine
954 * (4 bytes for the xid and 4 bytes for the call/reply flag).
955 * When this function is called for the first time,
956 * transport->tcp_offset is 4 (after having already read the xid).
958 offset
= transport
->tcp_offset
- sizeof(transport
->tcp_xid
);
959 len
= sizeof(calldir
) - offset
;
960 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len
);
961 used
= xdr_skb_read_bits(desc
, &calldir
, len
);
962 transport
->tcp_offset
+= used
;
965 transport
->tcp_flags
&= ~TCP_RCV_READ_CALLDIR
;
966 transport
->tcp_flags
|= TCP_RCV_COPY_CALLDIR
;
967 transport
->tcp_flags
|= TCP_RCV_COPY_DATA
;
969 * We don't yet have the XDR buffer, so we will write the calldir
970 * out after we get the buffer from the 'struct rpc_rqst'
972 if (ntohl(calldir
) == RPC_REPLY
)
973 transport
->tcp_flags
|= TCP_RPC_REPLY
;
975 transport
->tcp_flags
&= ~TCP_RPC_REPLY
;
976 dprintk("RPC: reading %s CALL/REPLY flag %08x\n",
977 (transport
->tcp_flags
& TCP_RPC_REPLY
) ?
978 "reply for" : "request with", calldir
);
979 xs_tcp_check_fraghdr(transport
);
982 static inline void xs_tcp_read_common(struct rpc_xprt
*xprt
,
983 struct xdr_skb_reader
*desc
,
984 struct rpc_rqst
*req
)
986 struct sock_xprt
*transport
=
987 container_of(xprt
, struct sock_xprt
, xprt
);
988 struct xdr_buf
*rcvbuf
;
992 rcvbuf
= &req
->rq_private_buf
;
994 if (transport
->tcp_flags
& TCP_RCV_COPY_CALLDIR
) {
996 * Save the RPC direction in the XDR buffer
998 __be32 calldir
= transport
->tcp_flags
& TCP_RPC_REPLY
?
999 htonl(RPC_REPLY
) : 0;
1001 memcpy(rcvbuf
->head
[0].iov_base
+ transport
->tcp_copied
,
1002 &calldir
, sizeof(calldir
));
1003 transport
->tcp_copied
+= sizeof(calldir
);
1004 transport
->tcp_flags
&= ~TCP_RCV_COPY_CALLDIR
;
1008 if (len
> transport
->tcp_reclen
- transport
->tcp_offset
) {
1009 struct xdr_skb_reader my_desc
;
1011 len
= transport
->tcp_reclen
- transport
->tcp_offset
;
1012 memcpy(&my_desc
, desc
, sizeof(my_desc
));
1013 my_desc
.count
= len
;
1014 r
= xdr_partial_copy_from_skb(rcvbuf
, transport
->tcp_copied
,
1015 &my_desc
, xdr_skb_read_bits
);
1019 r
= xdr_partial_copy_from_skb(rcvbuf
, transport
->tcp_copied
,
1020 desc
, xdr_skb_read_bits
);
1023 transport
->tcp_copied
+= r
;
1024 transport
->tcp_offset
+= r
;
1027 /* Error when copying to the receive buffer,
1028 * usually because we weren't able to allocate
1029 * additional buffer pages. All we can do now
1030 * is turn off TCP_RCV_COPY_DATA, so the request
1031 * will not receive any additional updates,
1033 * Any remaining data from this record will
1036 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1037 dprintk("RPC: XID %08x truncated request\n",
1038 ntohl(transport
->tcp_xid
));
1039 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1040 "tcp_offset = %u, tcp_reclen = %u\n",
1041 xprt
, transport
->tcp_copied
,
1042 transport
->tcp_offset
, transport
->tcp_reclen
);
1046 dprintk("RPC: XID %08x read %Zd bytes\n",
1047 ntohl(transport
->tcp_xid
), r
);
1048 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1049 "tcp_reclen = %u\n", xprt
, transport
->tcp_copied
,
1050 transport
->tcp_offset
, transport
->tcp_reclen
);
1052 if (transport
->tcp_copied
== req
->rq_private_buf
.buflen
)
1053 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1054 else if (transport
->tcp_offset
== transport
->tcp_reclen
) {
1055 if (transport
->tcp_flags
& TCP_RCV_LAST_FRAG
)
1056 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1063 * Finds the request corresponding to the RPC xid and invokes the common
1064 * tcp read code to read the data.
1066 static inline int xs_tcp_read_reply(struct rpc_xprt
*xprt
,
1067 struct xdr_skb_reader
*desc
)
1069 struct sock_xprt
*transport
=
1070 container_of(xprt
, struct sock_xprt
, xprt
);
1071 struct rpc_rqst
*req
;
1073 dprintk("RPC: read reply XID %08x\n", ntohl(transport
->tcp_xid
));
1075 /* Find and lock the request corresponding to this xid */
1076 spin_lock(&xprt
->transport_lock
);
1077 req
= xprt_lookup_rqst(xprt
, transport
->tcp_xid
);
1079 dprintk("RPC: XID %08x request not found!\n",
1080 ntohl(transport
->tcp_xid
));
1081 spin_unlock(&xprt
->transport_lock
);
1085 xs_tcp_read_common(xprt
, desc
, req
);
1087 if (!(transport
->tcp_flags
& TCP_RCV_COPY_DATA
))
1088 xprt_complete_rqst(req
->rq_task
, transport
->tcp_copied
);
1090 spin_unlock(&xprt
->transport_lock
);
1094 #if defined(CONFIG_NFS_V4_1)
1096 * Obtains an rpc_rqst previously allocated and invokes the common
1097 * tcp read code to read the data. The result is placed in the callback
1099 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1100 * connection and return -1.
1102 static inline int xs_tcp_read_callback(struct rpc_xprt
*xprt
,
1103 struct xdr_skb_reader
*desc
)
1105 struct sock_xprt
*transport
=
1106 container_of(xprt
, struct sock_xprt
, xprt
);
1107 struct rpc_rqst
*req
;
1109 req
= xprt_alloc_bc_request(xprt
);
1111 printk(KERN_WARNING
"Callback slot table overflowed\n");
1112 xprt_force_disconnect(xprt
);
1116 req
->rq_xid
= transport
->tcp_xid
;
1117 dprintk("RPC: read callback XID %08x\n", ntohl(req
->rq_xid
));
1118 xs_tcp_read_common(xprt
, desc
, req
);
1120 if (!(transport
->tcp_flags
& TCP_RCV_COPY_DATA
)) {
1121 struct svc_serv
*bc_serv
= xprt
->bc_serv
;
1124 * Add callback request to callback list. The callback
1125 * service sleeps on the sv_cb_waitq waiting for new
1126 * requests. Wake it up after adding enqueing the
1129 dprintk("RPC: add callback request to list\n");
1130 spin_lock(&bc_serv
->sv_cb_lock
);
1131 list_add(&req
->rq_bc_list
, &bc_serv
->sv_cb_list
);
1132 spin_unlock(&bc_serv
->sv_cb_lock
);
1133 wake_up(&bc_serv
->sv_cb_waitq
);
1136 req
->rq_private_buf
.len
= transport
->tcp_copied
;
1141 static inline int _xs_tcp_read_data(struct rpc_xprt
*xprt
,
1142 struct xdr_skb_reader
*desc
)
1144 struct sock_xprt
*transport
=
1145 container_of(xprt
, struct sock_xprt
, xprt
);
1147 return (transport
->tcp_flags
& TCP_RPC_REPLY
) ?
1148 xs_tcp_read_reply(xprt
, desc
) :
1149 xs_tcp_read_callback(xprt
, desc
);
1152 static inline int _xs_tcp_read_data(struct rpc_xprt
*xprt
,
1153 struct xdr_skb_reader
*desc
)
1155 return xs_tcp_read_reply(xprt
, desc
);
1157 #endif /* CONFIG_NFS_V4_1 */
1160 * Read data off the transport. This can be either an RPC_CALL or an
1161 * RPC_REPLY. Relay the processing to helper functions.
1163 static void xs_tcp_read_data(struct rpc_xprt
*xprt
,
1164 struct xdr_skb_reader
*desc
)
1166 struct sock_xprt
*transport
=
1167 container_of(xprt
, struct sock_xprt
, xprt
);
1169 if (_xs_tcp_read_data(xprt
, desc
) == 0)
1170 xs_tcp_check_fraghdr(transport
);
1173 * The transport_lock protects the request handling.
1174 * There's no need to hold it to update the tcp_flags.
1176 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1180 static inline void xs_tcp_read_discard(struct sock_xprt
*transport
, struct xdr_skb_reader
*desc
)
1184 len
= transport
->tcp_reclen
- transport
->tcp_offset
;
1185 if (len
> desc
->count
)
1188 desc
->offset
+= len
;
1189 transport
->tcp_offset
+= len
;
1190 dprintk("RPC: discarded %Zu bytes\n", len
);
1191 xs_tcp_check_fraghdr(transport
);
1194 static int xs_tcp_data_recv(read_descriptor_t
*rd_desc
, struct sk_buff
*skb
, unsigned int offset
, size_t len
)
1196 struct rpc_xprt
*xprt
= rd_desc
->arg
.data
;
1197 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1198 struct xdr_skb_reader desc
= {
1204 dprintk("RPC: xs_tcp_data_recv started\n");
1206 /* Read in a new fragment marker if necessary */
1207 /* Can we ever really expect to get completely empty fragments? */
1208 if (transport
->tcp_flags
& TCP_RCV_COPY_FRAGHDR
) {
1209 xs_tcp_read_fraghdr(xprt
, &desc
);
1212 /* Read in the xid if necessary */
1213 if (transport
->tcp_flags
& TCP_RCV_COPY_XID
) {
1214 xs_tcp_read_xid(transport
, &desc
);
1217 /* Read in the call/reply flag */
1218 if (transport
->tcp_flags
& TCP_RCV_READ_CALLDIR
) {
1219 xs_tcp_read_calldir(transport
, &desc
);
1222 /* Read in the request data */
1223 if (transport
->tcp_flags
& TCP_RCV_COPY_DATA
) {
1224 xs_tcp_read_data(xprt
, &desc
);
1227 /* Skip over any trailing bytes on short reads */
1228 xs_tcp_read_discard(transport
, &desc
);
1229 } while (desc
.count
);
1230 dprintk("RPC: xs_tcp_data_recv done\n");
1231 return len
- desc
.count
;
1235 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1236 * @sk: socket with data to read
1237 * @bytes: how much data to read
1240 static void xs_tcp_data_ready(struct sock
*sk
, int bytes
)
1242 struct rpc_xprt
*xprt
;
1243 read_descriptor_t rd_desc
;
1246 dprintk("RPC: xs_tcp_data_ready...\n");
1248 read_lock(&sk
->sk_callback_lock
);
1249 if (!(xprt
= xprt_from_sock(sk
)))
1254 /* Any data means we had a useful conversation, so
1255 * the we don't need to delay the next reconnect
1257 if (xprt
->reestablish_timeout
)
1258 xprt
->reestablish_timeout
= 0;
1260 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1261 rd_desc
.arg
.data
= xprt
;
1263 rd_desc
.count
= 65536;
1264 read
= tcp_read_sock(sk
, &rd_desc
, xs_tcp_data_recv
);
1267 read_unlock(&sk
->sk_callback_lock
);
1271 * Do the equivalent of linger/linger2 handling for dealing with
1272 * broken servers that don't close the socket in a timely
1275 static void xs_tcp_schedule_linger_timeout(struct rpc_xprt
*xprt
,
1276 unsigned long timeout
)
1278 struct sock_xprt
*transport
;
1280 if (xprt_test_and_set_connecting(xprt
))
1282 set_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1283 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1284 queue_delayed_work(rpciod_workqueue
, &transport
->connect_worker
,
1288 static void xs_tcp_cancel_linger_timeout(struct rpc_xprt
*xprt
)
1290 struct sock_xprt
*transport
;
1292 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1294 if (!test_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
) ||
1295 !cancel_delayed_work(&transport
->connect_worker
))
1297 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1298 xprt_clear_connecting(xprt
);
1301 static void xs_sock_mark_closed(struct rpc_xprt
*xprt
)
1303 smp_mb__before_clear_bit();
1304 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
1305 clear_bit(XPRT_CLOSING
, &xprt
->state
);
1306 smp_mb__after_clear_bit();
1307 /* Mark transport as closed and wake up all pending tasks */
1308 xprt_disconnect_done(xprt
);
1312 * xs_tcp_state_change - callback to handle TCP socket state changes
1313 * @sk: socket whose state has changed
1316 static void xs_tcp_state_change(struct sock
*sk
)
1318 struct rpc_xprt
*xprt
;
1320 read_lock(&sk
->sk_callback_lock
);
1321 if (!(xprt
= xprt_from_sock(sk
)))
1323 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt
);
1324 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1325 sk
->sk_state
, xprt_connected(xprt
),
1326 sock_flag(sk
, SOCK_DEAD
),
1327 sock_flag(sk
, SOCK_ZAPPED
));
1329 switch (sk
->sk_state
) {
1330 case TCP_ESTABLISHED
:
1331 spin_lock_bh(&xprt
->transport_lock
);
1332 if (!xprt_test_and_set_connected(xprt
)) {
1333 struct sock_xprt
*transport
= container_of(xprt
,
1334 struct sock_xprt
, xprt
);
1336 /* Reset TCP record info */
1337 transport
->tcp_offset
= 0;
1338 transport
->tcp_reclen
= 0;
1339 transport
->tcp_copied
= 0;
1340 transport
->tcp_flags
=
1341 TCP_RCV_COPY_FRAGHDR
| TCP_RCV_COPY_XID
;
1343 xprt_wake_pending_tasks(xprt
, -EAGAIN
);
1345 spin_unlock_bh(&xprt
->transport_lock
);
1348 /* The client initiated a shutdown of the socket */
1349 xprt
->connect_cookie
++;
1350 xprt
->reestablish_timeout
= 0;
1351 set_bit(XPRT_CLOSING
, &xprt
->state
);
1352 smp_mb__before_clear_bit();
1353 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1354 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
1355 smp_mb__after_clear_bit();
1356 xs_tcp_schedule_linger_timeout(xprt
, xs_tcp_fin_timeout
);
1358 case TCP_CLOSE_WAIT
:
1359 /* The server initiated a shutdown of the socket */
1360 xprt_force_disconnect(xprt
);
1362 xprt
->connect_cookie
++;
1365 * If the server closed down the connection, make sure that
1366 * we back off before reconnecting
1368 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
1369 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
1372 set_bit(XPRT_CLOSING
, &xprt
->state
);
1373 xs_tcp_schedule_linger_timeout(xprt
, xs_tcp_fin_timeout
);
1374 smp_mb__before_clear_bit();
1375 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1376 smp_mb__after_clear_bit();
1379 xs_tcp_cancel_linger_timeout(xprt
);
1380 xs_sock_mark_closed(xprt
);
1383 read_unlock(&sk
->sk_callback_lock
);
1387 * xs_error_report - callback mainly for catching socket errors
1390 static void xs_error_report(struct sock
*sk
)
1392 struct rpc_xprt
*xprt
;
1394 read_lock(&sk
->sk_callback_lock
);
1395 if (!(xprt
= xprt_from_sock(sk
)))
1397 dprintk("RPC: %s client %p...\n"
1399 __func__
, xprt
, sk
->sk_err
);
1400 xprt_wake_pending_tasks(xprt
, -EAGAIN
);
1402 read_unlock(&sk
->sk_callback_lock
);
1405 static void xs_write_space(struct sock
*sk
)
1407 struct socket
*sock
;
1408 struct rpc_xprt
*xprt
;
1410 if (unlikely(!(sock
= sk
->sk_socket
)))
1412 clear_bit(SOCK_NOSPACE
, &sock
->flags
);
1414 if (unlikely(!(xprt
= xprt_from_sock(sk
))))
1416 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
) == 0)
1419 xprt_write_space(xprt
);
1423 * xs_udp_write_space - callback invoked when socket buffer space
1425 * @sk: socket whose state has changed
1427 * Called when more output buffer space is available for this socket.
1428 * We try not to wake our writers until they can make "significant"
1429 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1430 * with a bunch of small requests.
1432 static void xs_udp_write_space(struct sock
*sk
)
1434 read_lock(&sk
->sk_callback_lock
);
1436 /* from net/core/sock.c:sock_def_write_space */
1437 if (sock_writeable(sk
))
1440 read_unlock(&sk
->sk_callback_lock
);
1444 * xs_tcp_write_space - callback invoked when socket buffer space
1446 * @sk: socket whose state has changed
1448 * Called when more output buffer space is available for this socket.
1449 * We try not to wake our writers until they can make "significant"
1450 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1451 * with a bunch of small requests.
1453 static void xs_tcp_write_space(struct sock
*sk
)
1455 read_lock(&sk
->sk_callback_lock
);
1457 /* from net/core/stream.c:sk_stream_write_space */
1458 if (sk_stream_wspace(sk
) >= sk_stream_min_wspace(sk
))
1461 read_unlock(&sk
->sk_callback_lock
);
1464 static void xs_udp_do_set_buffer_size(struct rpc_xprt
*xprt
)
1466 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1467 struct sock
*sk
= transport
->inet
;
1469 if (transport
->rcvsize
) {
1470 sk
->sk_userlocks
|= SOCK_RCVBUF_LOCK
;
1471 sk
->sk_rcvbuf
= transport
->rcvsize
* xprt
->max_reqs
* 2;
1473 if (transport
->sndsize
) {
1474 sk
->sk_userlocks
|= SOCK_SNDBUF_LOCK
;
1475 sk
->sk_sndbuf
= transport
->sndsize
* xprt
->max_reqs
* 2;
1476 sk
->sk_write_space(sk
);
1481 * xs_udp_set_buffer_size - set send and receive limits
1482 * @xprt: generic transport
1483 * @sndsize: requested size of send buffer, in bytes
1484 * @rcvsize: requested size of receive buffer, in bytes
1486 * Set socket send and receive buffer size limits.
1488 static void xs_udp_set_buffer_size(struct rpc_xprt
*xprt
, size_t sndsize
, size_t rcvsize
)
1490 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1492 transport
->sndsize
= 0;
1494 transport
->sndsize
= sndsize
+ 1024;
1495 transport
->rcvsize
= 0;
1497 transport
->rcvsize
= rcvsize
+ 1024;
1499 xs_udp_do_set_buffer_size(xprt
);
1503 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1504 * @task: task that timed out
1506 * Adjust the congestion window after a retransmit timeout has occurred.
1508 static void xs_udp_timer(struct rpc_task
*task
)
1510 xprt_adjust_cwnd(task
, -ETIMEDOUT
);
1513 static unsigned short xs_get_random_port(void)
1515 unsigned short range
= xprt_max_resvport
- xprt_min_resvport
;
1516 unsigned short rand
= (unsigned short) net_random() % range
;
1517 return rand
+ xprt_min_resvport
;
1521 * xs_set_port - reset the port number in the remote endpoint address
1522 * @xprt: generic transport
1523 * @port: new port number
1526 static void xs_set_port(struct rpc_xprt
*xprt
, unsigned short port
)
1528 dprintk("RPC: setting port for xprt %p to %u\n", xprt
, port
);
1530 rpc_set_port(xs_addr(xprt
), port
);
1531 xs_update_peer_port(xprt
);
1534 static unsigned short xs_get_srcport(struct sock_xprt
*transport
, struct socket
*sock
)
1536 unsigned short port
= transport
->srcport
;
1538 if (port
== 0 && transport
->xprt
.resvport
)
1539 port
= xs_get_random_port();
1543 static unsigned short xs_next_srcport(struct sock_xprt
*transport
, struct socket
*sock
, unsigned short port
)
1545 if (transport
->srcport
!= 0)
1546 transport
->srcport
= 0;
1547 if (!transport
->xprt
.resvport
)
1549 if (port
<= xprt_min_resvport
|| port
> xprt_max_resvport
)
1550 return xprt_max_resvport
;
1554 static int xs_bind4(struct sock_xprt
*transport
, struct socket
*sock
)
1556 struct sockaddr_in myaddr
= {
1557 .sin_family
= AF_INET
,
1559 struct sockaddr_in
*sa
;
1561 unsigned short port
= xs_get_srcport(transport
, sock
);
1562 unsigned short last
;
1564 sa
= (struct sockaddr_in
*)&transport
->srcaddr
;
1565 myaddr
.sin_addr
= sa
->sin_addr
;
1567 myaddr
.sin_port
= htons(port
);
1568 err
= kernel_bind(sock
, (struct sockaddr
*) &myaddr
,
1573 transport
->srcport
= port
;
1577 port
= xs_next_srcport(transport
, sock
, port
);
1580 } while (err
== -EADDRINUSE
&& nloop
!= 2);
1581 dprintk("RPC: %s %pI4:%u: %s (%d)\n",
1582 __func__
, &myaddr
.sin_addr
,
1583 port
, err
? "failed" : "ok", err
);
1587 static int xs_bind6(struct sock_xprt
*transport
, struct socket
*sock
)
1589 struct sockaddr_in6 myaddr
= {
1590 .sin6_family
= AF_INET6
,
1592 struct sockaddr_in6
*sa
;
1594 unsigned short port
= xs_get_srcport(transport
, sock
);
1595 unsigned short last
;
1597 sa
= (struct sockaddr_in6
*)&transport
->srcaddr
;
1598 myaddr
.sin6_addr
= sa
->sin6_addr
;
1600 myaddr
.sin6_port
= htons(port
);
1601 err
= kernel_bind(sock
, (struct sockaddr
*) &myaddr
,
1606 transport
->srcport
= port
;
1610 port
= xs_next_srcport(transport
, sock
, port
);
1613 } while (err
== -EADDRINUSE
&& nloop
!= 2);
1614 dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n",
1615 &myaddr
.sin6_addr
, port
, err
? "failed" : "ok", err
);
1619 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1620 static struct lock_class_key xs_key
[2];
1621 static struct lock_class_key xs_slock_key
[2];
1623 static inline void xs_reclassify_socket4(struct socket
*sock
)
1625 struct sock
*sk
= sock
->sk
;
1627 BUG_ON(sock_owned_by_user(sk
));
1628 sock_lock_init_class_and_name(sk
, "slock-AF_INET-RPC",
1629 &xs_slock_key
[0], "sk_lock-AF_INET-RPC", &xs_key
[0]);
1632 static inline void xs_reclassify_socket6(struct socket
*sock
)
1634 struct sock
*sk
= sock
->sk
;
1636 BUG_ON(sock_owned_by_user(sk
));
1637 sock_lock_init_class_and_name(sk
, "slock-AF_INET6-RPC",
1638 &xs_slock_key
[1], "sk_lock-AF_INET6-RPC", &xs_key
[1]);
1641 static inline void xs_reclassify_socket4(struct socket
*sock
)
1645 static inline void xs_reclassify_socket6(struct socket
*sock
)
1650 static void xs_udp_finish_connecting(struct rpc_xprt
*xprt
, struct socket
*sock
)
1652 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1654 if (!transport
->inet
) {
1655 struct sock
*sk
= sock
->sk
;
1657 write_lock_bh(&sk
->sk_callback_lock
);
1659 xs_save_old_callbacks(transport
, sk
);
1661 sk
->sk_user_data
= xprt
;
1662 sk
->sk_data_ready
= xs_udp_data_ready
;
1663 sk
->sk_write_space
= xs_udp_write_space
;
1664 sk
->sk_error_report
= xs_error_report
;
1665 sk
->sk_no_check
= UDP_CSUM_NORCV
;
1666 sk
->sk_allocation
= GFP_ATOMIC
;
1668 xprt_set_connected(xprt
);
1670 /* Reset to new socket */
1671 transport
->sock
= sock
;
1672 transport
->inet
= sk
;
1674 write_unlock_bh(&sk
->sk_callback_lock
);
1676 xs_udp_do_set_buffer_size(xprt
);
1680 * xs_udp_connect_worker4 - set up a UDP socket
1681 * @work: RPC transport to connect
1683 * Invoked by a work queue tasklet.
1685 static void xs_udp_connect_worker4(struct work_struct
*work
)
1687 struct sock_xprt
*transport
=
1688 container_of(work
, struct sock_xprt
, connect_worker
.work
);
1689 struct rpc_xprt
*xprt
= &transport
->xprt
;
1690 struct socket
*sock
= transport
->sock
;
1691 int err
, status
= -EIO
;
1696 /* Start by resetting any existing state */
1697 xs_reset_transport(transport
);
1699 err
= sock_create_kern(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
);
1701 dprintk("RPC: can't create UDP transport socket (%d).\n", -err
);
1704 xs_reclassify_socket4(sock
);
1706 if (xs_bind4(transport
, sock
)) {
1711 dprintk("RPC: worker connecting xprt %p via %s to "
1712 "%s (port %s)\n", xprt
,
1713 xprt
->address_strings
[RPC_DISPLAY_PROTO
],
1714 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
1715 xprt
->address_strings
[RPC_DISPLAY_PORT
]);
1717 xs_udp_finish_connecting(xprt
, sock
);
1720 xprt_clear_connecting(xprt
);
1721 xprt_wake_pending_tasks(xprt
, status
);
1725 * xs_udp_connect_worker6 - set up a UDP socket
1726 * @work: RPC transport to connect
1728 * Invoked by a work queue tasklet.
1730 static void xs_udp_connect_worker6(struct work_struct
*work
)
1732 struct sock_xprt
*transport
=
1733 container_of(work
, struct sock_xprt
, connect_worker
.work
);
1734 struct rpc_xprt
*xprt
= &transport
->xprt
;
1735 struct socket
*sock
= transport
->sock
;
1736 int err
, status
= -EIO
;
1741 /* Start by resetting any existing state */
1742 xs_reset_transport(transport
);
1744 err
= sock_create_kern(PF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
);
1746 dprintk("RPC: can't create UDP transport socket (%d).\n", -err
);
1749 xs_reclassify_socket6(sock
);
1751 if (xs_bind6(transport
, sock
) < 0) {
1756 dprintk("RPC: worker connecting xprt %p via %s to "
1757 "%s (port %s)\n", xprt
,
1758 xprt
->address_strings
[RPC_DISPLAY_PROTO
],
1759 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
1760 xprt
->address_strings
[RPC_DISPLAY_PORT
]);
1762 xs_udp_finish_connecting(xprt
, sock
);
1765 xprt_clear_connecting(xprt
);
1766 xprt_wake_pending_tasks(xprt
, status
);
1770 * We need to preserve the port number so the reply cache on the server can
1771 * find our cached RPC replies when we get around to reconnecting.
1773 static void xs_abort_connection(struct rpc_xprt
*xprt
, struct sock_xprt
*transport
)
1776 struct sockaddr any
;
1778 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt
);
1781 * Disconnect the transport socket by doing a connect operation
1782 * with AF_UNSPEC. This should return immediately...
1784 memset(&any
, 0, sizeof(any
));
1785 any
.sa_family
= AF_UNSPEC
;
1786 result
= kernel_connect(transport
->sock
, &any
, sizeof(any
), 0);
1788 xs_sock_mark_closed(xprt
);
1790 dprintk("RPC: AF_UNSPEC connect return code %d\n",
1794 static void xs_tcp_reuse_connection(struct rpc_xprt
*xprt
, struct sock_xprt
*transport
)
1796 unsigned int state
= transport
->inet
->sk_state
;
1798 if (state
== TCP_CLOSE
&& transport
->sock
->state
== SS_UNCONNECTED
)
1800 if ((1 << state
) & (TCPF_ESTABLISHED
|TCPF_SYN_SENT
))
1802 xs_abort_connection(xprt
, transport
);
1805 static int xs_tcp_finish_connecting(struct rpc_xprt
*xprt
, struct socket
*sock
)
1807 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1809 if (!transport
->inet
) {
1810 struct sock
*sk
= sock
->sk
;
1812 write_lock_bh(&sk
->sk_callback_lock
);
1814 xs_save_old_callbacks(transport
, sk
);
1816 sk
->sk_user_data
= xprt
;
1817 sk
->sk_data_ready
= xs_tcp_data_ready
;
1818 sk
->sk_state_change
= xs_tcp_state_change
;
1819 sk
->sk_write_space
= xs_tcp_write_space
;
1820 sk
->sk_error_report
= xs_error_report
;
1821 sk
->sk_allocation
= GFP_ATOMIC
;
1823 /* socket options */
1824 sk
->sk_userlocks
|= SOCK_BINDPORT_LOCK
;
1825 sock_reset_flag(sk
, SOCK_LINGER
);
1826 tcp_sk(sk
)->linger2
= 0;
1827 tcp_sk(sk
)->nonagle
|= TCP_NAGLE_OFF
;
1829 xprt_clear_connected(xprt
);
1831 /* Reset to new socket */
1832 transport
->sock
= sock
;
1833 transport
->inet
= sk
;
1835 write_unlock_bh(&sk
->sk_callback_lock
);
1838 if (!xprt_bound(xprt
))
1841 /* Tell the socket layer to start connecting... */
1842 xprt
->stat
.connect_count
++;
1843 xprt
->stat
.connect_start
= jiffies
;
1844 return kernel_connect(sock
, xs_addr(xprt
), xprt
->addrlen
, O_NONBLOCK
);
1848 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1849 * @xprt: RPC transport to connect
1850 * @transport: socket transport to connect
1851 * @create_sock: function to create a socket of the correct type
1853 * Invoked by a work queue tasklet.
1855 static void xs_tcp_setup_socket(struct rpc_xprt
*xprt
,
1856 struct sock_xprt
*transport
,
1857 struct socket
*(*create_sock
)(struct rpc_xprt
*,
1858 struct sock_xprt
*))
1860 struct socket
*sock
= transport
->sock
;
1867 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1868 sock
= create_sock(xprt
, transport
);
1870 status
= PTR_ERR(sock
);
1876 abort_and_exit
= test_and_clear_bit(XPRT_CONNECTION_ABORT
,
1878 /* "close" the socket, preserving the local port */
1879 xs_tcp_reuse_connection(xprt
, transport
);
1885 dprintk("RPC: worker connecting xprt %p via %s to "
1886 "%s (port %s)\n", xprt
,
1887 xprt
->address_strings
[RPC_DISPLAY_PROTO
],
1888 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
1889 xprt
->address_strings
[RPC_DISPLAY_PORT
]);
1891 status
= xs_tcp_finish_connecting(xprt
, sock
);
1892 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1893 xprt
, -status
, xprt_connected(xprt
),
1894 sock
->sk
->sk_state
);
1897 printk("%s: connect returned unhandled error %d\n",
1899 case -EADDRNOTAVAIL
:
1900 /* We're probably in TIME_WAIT. Get rid of existing socket,
1903 set_bit(XPRT_CONNECTION_CLOSE
, &xprt
->state
);
1904 xprt_force_disconnect(xprt
);
1909 /* retry with existing socket, after a delay */
1913 xprt_clear_connecting(xprt
);
1919 xprt_clear_connecting(xprt
);
1920 xprt_wake_pending_tasks(xprt
, status
);
1923 static struct socket
*xs_create_tcp_sock4(struct rpc_xprt
*xprt
,
1924 struct sock_xprt
*transport
)
1926 struct socket
*sock
;
1929 /* start from scratch */
1930 err
= sock_create_kern(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
, &sock
);
1932 dprintk("RPC: can't create TCP transport socket (%d).\n",
1936 xs_reclassify_socket4(sock
);
1938 if (xs_bind4(transport
, sock
) < 0) {
1944 return ERR_PTR(-EIO
);
1948 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1949 * @work: RPC transport to connect
1951 * Invoked by a work queue tasklet.
1953 static void xs_tcp_connect_worker4(struct work_struct
*work
)
1955 struct sock_xprt
*transport
=
1956 container_of(work
, struct sock_xprt
, connect_worker
.work
);
1957 struct rpc_xprt
*xprt
= &transport
->xprt
;
1959 xs_tcp_setup_socket(xprt
, transport
, xs_create_tcp_sock4
);
1962 static struct socket
*xs_create_tcp_sock6(struct rpc_xprt
*xprt
,
1963 struct sock_xprt
*transport
)
1965 struct socket
*sock
;
1968 /* start from scratch */
1969 err
= sock_create_kern(PF_INET6
, SOCK_STREAM
, IPPROTO_TCP
, &sock
);
1971 dprintk("RPC: can't create TCP transport socket (%d).\n",
1975 xs_reclassify_socket6(sock
);
1977 if (xs_bind6(transport
, sock
) < 0) {
1983 return ERR_PTR(-EIO
);
1987 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
1988 * @work: RPC transport to connect
1990 * Invoked by a work queue tasklet.
1992 static void xs_tcp_connect_worker6(struct work_struct
*work
)
1994 struct sock_xprt
*transport
=
1995 container_of(work
, struct sock_xprt
, connect_worker
.work
);
1996 struct rpc_xprt
*xprt
= &transport
->xprt
;
1998 xs_tcp_setup_socket(xprt
, transport
, xs_create_tcp_sock6
);
2002 * xs_connect - connect a socket to a remote endpoint
2003 * @task: address of RPC task that manages state of connect request
2005 * TCP: If the remote end dropped the connection, delay reconnecting.
2007 * UDP socket connects are synchronous, but we use a work queue anyway
2008 * to guarantee that even unprivileged user processes can set up a
2009 * socket on a privileged port.
2011 * If a UDP socket connect fails, the delay behavior here prevents
2012 * retry floods (hard mounts).
2014 static void xs_connect(struct rpc_task
*task
)
2016 struct rpc_xprt
*xprt
= task
->tk_xprt
;
2017 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2019 if (xprt_test_and_set_connecting(xprt
))
2022 if (transport
->sock
!= NULL
&& !RPC_IS_SOFTCONN(task
)) {
2023 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2025 xprt
, xprt
->reestablish_timeout
/ HZ
);
2026 queue_delayed_work(rpciod_workqueue
,
2027 &transport
->connect_worker
,
2028 xprt
->reestablish_timeout
);
2029 xprt
->reestablish_timeout
<<= 1;
2030 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
2031 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2032 if (xprt
->reestablish_timeout
> XS_TCP_MAX_REEST_TO
)
2033 xprt
->reestablish_timeout
= XS_TCP_MAX_REEST_TO
;
2035 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt
);
2036 queue_delayed_work(rpciod_workqueue
,
2037 &transport
->connect_worker
, 0);
2041 static void xs_tcp_connect(struct rpc_task
*task
)
2043 struct rpc_xprt
*xprt
= task
->tk_xprt
;
2045 /* Exit if we need to wait for socket shutdown to complete */
2046 if (test_bit(XPRT_CLOSING
, &xprt
->state
))
2052 * xs_udp_print_stats - display UDP socket-specifc stats
2053 * @xprt: rpc_xprt struct containing statistics
2057 static void xs_udp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2059 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2061 seq_printf(seq
, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
2063 xprt
->stat
.bind_count
,
2066 xprt
->stat
.bad_xids
,
2068 xprt
->stat
.bklog_u
);
2072 * xs_tcp_print_stats - display TCP socket-specifc stats
2073 * @xprt: rpc_xprt struct containing statistics
2077 static void xs_tcp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2079 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2082 if (xprt_connected(xprt
))
2083 idle_time
= (long)(jiffies
- xprt
->last_used
) / HZ
;
2085 seq_printf(seq
, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
2087 xprt
->stat
.bind_count
,
2088 xprt
->stat
.connect_count
,
2089 xprt
->stat
.connect_time
,
2093 xprt
->stat
.bad_xids
,
2095 xprt
->stat
.bklog_u
);
2099 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2100 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2101 * to use the server side send routines.
2103 void *bc_malloc(struct rpc_task
*task
, size_t size
)
2106 struct rpc_buffer
*buf
;
2108 BUG_ON(size
> PAGE_SIZE
- sizeof(struct rpc_buffer
));
2109 page
= alloc_page(GFP_KERNEL
);
2114 buf
= page_address(page
);
2115 buf
->len
= PAGE_SIZE
;
2121 * Free the space allocated in the bc_alloc routine
2123 void bc_free(void *buffer
)
2125 struct rpc_buffer
*buf
;
2130 buf
= container_of(buffer
, struct rpc_buffer
, data
);
2131 free_page((unsigned long)buf
);
2135 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2136 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2138 static int bc_sendto(struct rpc_rqst
*req
)
2141 struct xdr_buf
*xbufp
= &req
->rq_snd_buf
;
2142 struct rpc_xprt
*xprt
= req
->rq_xprt
;
2143 struct sock_xprt
*transport
=
2144 container_of(xprt
, struct sock_xprt
, xprt
);
2145 struct socket
*sock
= transport
->sock
;
2146 unsigned long headoff
;
2147 unsigned long tailoff
;
2150 * Set up the rpc header and record marker stuff
2152 xs_encode_tcp_record_marker(xbufp
);
2154 tailoff
= (unsigned long)xbufp
->tail
[0].iov_base
& ~PAGE_MASK
;
2155 headoff
= (unsigned long)xbufp
->head
[0].iov_base
& ~PAGE_MASK
;
2156 len
= svc_send_common(sock
, xbufp
,
2157 virt_to_page(xbufp
->head
[0].iov_base
), headoff
,
2158 xbufp
->tail
[0].iov_base
, tailoff
);
2160 if (len
!= xbufp
->len
) {
2161 printk(KERN_NOTICE
"Error sending entire callback!\n");
2169 * The send routine. Borrows from svc_send
2171 static int bc_send_request(struct rpc_task
*task
)
2173 struct rpc_rqst
*req
= task
->tk_rqstp
;
2174 struct svc_xprt
*xprt
;
2175 struct svc_sock
*svsk
;
2178 dprintk("sending request with xid: %08x\n", ntohl(req
->rq_xid
));
2180 * Get the server socket associated with this callback xprt
2182 xprt
= req
->rq_xprt
->bc_xprt
;
2183 svsk
= container_of(xprt
, struct svc_sock
, sk_xprt
);
2186 * Grab the mutex to serialize data as the connection is shared
2187 * with the fore channel
2189 if (!mutex_trylock(&xprt
->xpt_mutex
)) {
2190 rpc_sleep_on(&xprt
->xpt_bc_pending
, task
, NULL
);
2191 if (!mutex_trylock(&xprt
->xpt_mutex
))
2193 rpc_wake_up_queued_task(&xprt
->xpt_bc_pending
, task
);
2195 if (test_bit(XPT_DEAD
, &xprt
->xpt_flags
))
2198 len
= bc_sendto(req
);
2199 mutex_unlock(&xprt
->xpt_mutex
);
2208 * The close routine. Since this is client initiated, we do nothing
2211 static void bc_close(struct rpc_xprt
*xprt
)
2217 * The xprt destroy routine. Again, because this connection is client
2218 * initiated, we do nothing
2221 static void bc_destroy(struct rpc_xprt
*xprt
)
2226 static struct rpc_xprt_ops xs_udp_ops
= {
2227 .set_buffer_size
= xs_udp_set_buffer_size
,
2228 .reserve_xprt
= xprt_reserve_xprt_cong
,
2229 .release_xprt
= xprt_release_xprt_cong
,
2230 .rpcbind
= rpcb_getport_async
,
2231 .set_port
= xs_set_port
,
2232 .connect
= xs_connect
,
2233 .buf_alloc
= rpc_malloc
,
2234 .buf_free
= rpc_free
,
2235 .send_request
= xs_udp_send_request
,
2236 .set_retrans_timeout
= xprt_set_retrans_timeout_rtt
,
2237 .timer
= xs_udp_timer
,
2238 .release_request
= xprt_release_rqst_cong
,
2240 .destroy
= xs_destroy
,
2241 .print_stats
= xs_udp_print_stats
,
2244 static struct rpc_xprt_ops xs_tcp_ops
= {
2245 .reserve_xprt
= xprt_reserve_xprt
,
2246 .release_xprt
= xs_tcp_release_xprt
,
2247 .rpcbind
= rpcb_getport_async
,
2248 .set_port
= xs_set_port
,
2249 .connect
= xs_tcp_connect
,
2250 .buf_alloc
= rpc_malloc
,
2251 .buf_free
= rpc_free
,
2252 .send_request
= xs_tcp_send_request
,
2253 .set_retrans_timeout
= xprt_set_retrans_timeout_def
,
2254 #if defined(CONFIG_NFS_V4_1)
2255 .release_request
= bc_release_request
,
2256 #endif /* CONFIG_NFS_V4_1 */
2257 .close
= xs_tcp_close
,
2258 .destroy
= xs_destroy
,
2259 .print_stats
= xs_tcp_print_stats
,
2263 * The rpc_xprt_ops for the server backchannel
2266 static struct rpc_xprt_ops bc_tcp_ops
= {
2267 .reserve_xprt
= xprt_reserve_xprt
,
2268 .release_xprt
= xprt_release_xprt
,
2269 .buf_alloc
= bc_malloc
,
2270 .buf_free
= bc_free
,
2271 .send_request
= bc_send_request
,
2272 .set_retrans_timeout
= xprt_set_retrans_timeout_def
,
2274 .destroy
= bc_destroy
,
2275 .print_stats
= xs_tcp_print_stats
,
2278 static struct rpc_xprt
*xs_setup_xprt(struct xprt_create
*args
,
2279 unsigned int slot_table_size
)
2281 struct rpc_xprt
*xprt
;
2282 struct sock_xprt
*new;
2284 if (args
->addrlen
> sizeof(xprt
->addr
)) {
2285 dprintk("RPC: xs_setup_xprt: address too large\n");
2286 return ERR_PTR(-EBADF
);
2289 new = kzalloc(sizeof(*new), GFP_KERNEL
);
2291 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2293 return ERR_PTR(-ENOMEM
);
2297 xprt
->max_reqs
= slot_table_size
;
2298 xprt
->slot
= kcalloc(xprt
->max_reqs
, sizeof(struct rpc_rqst
), GFP_KERNEL
);
2299 if (xprt
->slot
== NULL
) {
2301 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
2303 return ERR_PTR(-ENOMEM
);
2306 memcpy(&xprt
->addr
, args
->dstaddr
, args
->addrlen
);
2307 xprt
->addrlen
= args
->addrlen
;
2309 memcpy(&new->srcaddr
, args
->srcaddr
, args
->addrlen
);
2314 static const struct rpc_timeout xs_udp_default_timeout
= {
2315 .to_initval
= 5 * HZ
,
2316 .to_maxval
= 30 * HZ
,
2317 .to_increment
= 5 * HZ
,
2322 * xs_setup_udp - Set up transport to use a UDP socket
2323 * @args: rpc transport creation arguments
2326 static struct rpc_xprt
*xs_setup_udp(struct xprt_create
*args
)
2328 struct sockaddr
*addr
= args
->dstaddr
;
2329 struct rpc_xprt
*xprt
;
2330 struct sock_xprt
*transport
;
2332 xprt
= xs_setup_xprt(args
, xprt_udp_slot_table_entries
);
2335 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2337 xprt
->prot
= IPPROTO_UDP
;
2339 /* XXX: header size can vary due to auth type, IPv6, etc. */
2340 xprt
->max_payload
= (1U << 16) - (MAX_HEADER
<< 3);
2342 xprt
->bind_timeout
= XS_BIND_TO
;
2343 xprt
->connect_timeout
= XS_UDP_CONN_TO
;
2344 xprt
->reestablish_timeout
= XS_UDP_REEST_TO
;
2345 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2347 xprt
->ops
= &xs_udp_ops
;
2349 xprt
->timeout
= &xs_udp_default_timeout
;
2351 switch (addr
->sa_family
) {
2353 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
2354 xprt_set_bound(xprt
);
2356 INIT_DELAYED_WORK(&transport
->connect_worker
,
2357 xs_udp_connect_worker4
);
2358 xs_format_peer_addresses(xprt
, "udp", RPCBIND_NETID_UDP
);
2361 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
2362 xprt_set_bound(xprt
);
2364 INIT_DELAYED_WORK(&transport
->connect_worker
,
2365 xs_udp_connect_worker6
);
2366 xs_format_peer_addresses(xprt
, "udp", RPCBIND_NETID_UDP6
);
2370 return ERR_PTR(-EAFNOSUPPORT
);
2373 if (xprt_bound(xprt
))
2374 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2375 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2376 xprt
->address_strings
[RPC_DISPLAY_PORT
],
2377 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2379 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2380 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2381 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2383 if (try_module_get(THIS_MODULE
))
2388 return ERR_PTR(-EINVAL
);
2391 static const struct rpc_timeout xs_tcp_default_timeout
= {
2392 .to_initval
= 60 * HZ
,
2393 .to_maxval
= 60 * HZ
,
2398 * xs_setup_tcp - Set up transport to use a TCP socket
2399 * @args: rpc transport creation arguments
2402 static struct rpc_xprt
*xs_setup_tcp(struct xprt_create
*args
)
2404 struct sockaddr
*addr
= args
->dstaddr
;
2405 struct rpc_xprt
*xprt
;
2406 struct sock_xprt
*transport
;
2408 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
);
2411 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2413 xprt
->prot
= IPPROTO_TCP
;
2414 xprt
->tsh_size
= sizeof(rpc_fraghdr
) / sizeof(u32
);
2415 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
2417 xprt
->bind_timeout
= XS_BIND_TO
;
2418 xprt
->connect_timeout
= XS_TCP_CONN_TO
;
2419 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2420 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2422 xprt
->ops
= &xs_tcp_ops
;
2423 xprt
->timeout
= &xs_tcp_default_timeout
;
2425 switch (addr
->sa_family
) {
2427 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
2428 xprt_set_bound(xprt
);
2430 INIT_DELAYED_WORK(&transport
->connect_worker
,
2431 xs_tcp_connect_worker4
);
2432 xs_format_peer_addresses(xprt
, "tcp", RPCBIND_NETID_TCP
);
2435 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
2436 xprt_set_bound(xprt
);
2438 INIT_DELAYED_WORK(&transport
->connect_worker
,
2439 xs_tcp_connect_worker6
);
2440 xs_format_peer_addresses(xprt
, "tcp", RPCBIND_NETID_TCP6
);
2444 return ERR_PTR(-EAFNOSUPPORT
);
2447 if (xprt_bound(xprt
))
2448 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2449 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2450 xprt
->address_strings
[RPC_DISPLAY_PORT
],
2451 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2453 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2454 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2455 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2458 if (try_module_get(THIS_MODULE
))
2463 return ERR_PTR(-EINVAL
);
2467 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2468 * @args: rpc transport creation arguments
2471 static struct rpc_xprt
*xs_setup_bc_tcp(struct xprt_create
*args
)
2473 struct sockaddr
*addr
= args
->dstaddr
;
2474 struct rpc_xprt
*xprt
;
2475 struct sock_xprt
*transport
;
2476 struct svc_sock
*bc_sock
;
2481 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
);
2484 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2486 xprt
->prot
= IPPROTO_TCP
;
2487 xprt
->tsh_size
= sizeof(rpc_fraghdr
) / sizeof(u32
);
2488 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
2489 xprt
->timeout
= &xs_tcp_default_timeout
;
2492 xprt_set_bound(xprt
);
2493 xprt
->bind_timeout
= 0;
2494 xprt
->connect_timeout
= 0;
2495 xprt
->reestablish_timeout
= 0;
2496 xprt
->idle_timeout
= 0;
2499 * The backchannel uses the same socket connection as the
2502 xprt
->bc_xprt
= args
->bc_xprt
;
2503 bc_sock
= container_of(args
->bc_xprt
, struct svc_sock
, sk_xprt
);
2504 bc_sock
->sk_bc_xprt
= xprt
;
2505 transport
->sock
= bc_sock
->sk_sock
;
2506 transport
->inet
= bc_sock
->sk_sk
;
2508 xprt
->ops
= &bc_tcp_ops
;
2510 switch (addr
->sa_family
) {
2512 xs_format_peer_addresses(xprt
, "tcp",
2516 xs_format_peer_addresses(xprt
, "tcp",
2517 RPCBIND_NETID_TCP6
);
2521 return ERR_PTR(-EAFNOSUPPORT
);
2524 if (xprt_bound(xprt
))
2525 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2526 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2527 xprt
->address_strings
[RPC_DISPLAY_PORT
],
2528 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2530 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2531 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2532 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2535 * Since we don't want connections for the backchannel, we set
2536 * the xprt status to connected
2538 xprt_set_connected(xprt
);
2541 if (try_module_get(THIS_MODULE
))
2545 return ERR_PTR(-EINVAL
);
2548 static struct xprt_class xs_udp_transport
= {
2549 .list
= LIST_HEAD_INIT(xs_udp_transport
.list
),
2551 .owner
= THIS_MODULE
,
2552 .ident
= XPRT_TRANSPORT_UDP
,
2553 .setup
= xs_setup_udp
,
2556 static struct xprt_class xs_tcp_transport
= {
2557 .list
= LIST_HEAD_INIT(xs_tcp_transport
.list
),
2559 .owner
= THIS_MODULE
,
2560 .ident
= XPRT_TRANSPORT_TCP
,
2561 .setup
= xs_setup_tcp
,
2564 static struct xprt_class xs_bc_tcp_transport
= {
2565 .list
= LIST_HEAD_INIT(xs_bc_tcp_transport
.list
),
2566 .name
= "tcp NFSv4.1 backchannel",
2567 .owner
= THIS_MODULE
,
2568 .ident
= XPRT_TRANSPORT_BC_TCP
,
2569 .setup
= xs_setup_bc_tcp
,
2573 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2576 int init_socket_xprt(void)
2579 if (!sunrpc_table_header
)
2580 sunrpc_table_header
= register_sysctl_table(sunrpc_table
);
2583 xprt_register_transport(&xs_udp_transport
);
2584 xprt_register_transport(&xs_tcp_transport
);
2585 xprt_register_transport(&xs_bc_tcp_transport
);
2591 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
2594 void cleanup_socket_xprt(void)
2597 if (sunrpc_table_header
) {
2598 unregister_sysctl_table(sunrpc_table_header
);
2599 sunrpc_table_header
= NULL
;
2603 xprt_unregister_transport(&xs_udp_transport
);
2604 xprt_unregister_transport(&xs_tcp_transport
);
2605 xprt_unregister_transport(&xs_bc_tcp_transport
);
2608 static int param_set_uint_minmax(const char *val
, struct kernel_param
*kp
,
2609 unsigned int min
, unsigned int max
)
2616 ret
= strict_strtoul(val
, 0, &num
);
2617 if (ret
== -EINVAL
|| num
< min
|| num
> max
)
2619 *((unsigned int *)kp
->arg
) = num
;
2623 static int param_set_portnr(const char *val
, struct kernel_param
*kp
)
2625 return param_set_uint_minmax(val
, kp
,
2630 static int param_get_portnr(char *buffer
, struct kernel_param
*kp
)
2632 return param_get_uint(buffer
, kp
);
2634 #define param_check_portnr(name, p) \
2635 __param_check(name, p, unsigned int);
2637 module_param_named(min_resvport
, xprt_min_resvport
, portnr
, 0644);
2638 module_param_named(max_resvport
, xprt_max_resvport
, portnr
, 0644);
2640 static int param_set_slot_table_size(const char *val
, struct kernel_param
*kp
)
2642 return param_set_uint_minmax(val
, kp
,
2644 RPC_MAX_SLOT_TABLE
);
2647 static int param_get_slot_table_size(char *buffer
, struct kernel_param
*kp
)
2649 return param_get_uint(buffer
, kp
);
2651 #define param_check_slot_table_size(name, p) \
2652 __param_check(name, p, unsigned int);
2654 module_param_named(tcp_slot_table_entries
, xprt_tcp_slot_table_entries
,
2655 slot_table_size
, 0644);
2656 module_param_named(udp_slot_table_entries
, xprt_udp_slot_table_entries
,
2657 slot_table_size
, 0644);