2 * linux/net/sunrpc/xprtsock.c
4 * Client-side transport implementation for sockets.
6 * TCP callback races fixes (C) 1998 Red Hat Software <alan@redhat.com>
7 * TCP send fixes (C) 1998 Red Hat Software <alan@redhat.com>
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/file.h>
38 #include <net/checksum.h>
45 unsigned int xprt_udp_slot_table_entries
= RPC_DEF_SLOT_TABLE
;
46 unsigned int xprt_tcp_slot_table_entries
= RPC_DEF_SLOT_TABLE
;
48 unsigned int xprt_min_resvport
= RPC_DEF_MIN_RESVPORT
;
49 unsigned int xprt_max_resvport
= RPC_DEF_MAX_RESVPORT
;
52 * We can register our own files under /proc/sys/sunrpc by
53 * calling register_sysctl_table() again. The files in that
54 * directory become the union of all files registered there.
56 * We simply need to make sure that we don't collide with
57 * someone else's file names!
62 static unsigned int min_slot_table_size
= RPC_MIN_SLOT_TABLE
;
63 static unsigned int max_slot_table_size
= RPC_MAX_SLOT_TABLE
;
64 static unsigned int xprt_min_resvport_limit
= RPC_MIN_RESVPORT
;
65 static unsigned int xprt_max_resvport_limit
= RPC_MAX_RESVPORT
;
67 static struct ctl_table_header
*sunrpc_table_header
;
70 * FIXME: changing the UDP slot table size should also resize the UDP
71 * socket buffers for existing UDP transports
73 static ctl_table xs_tunables_table
[] = {
75 .ctl_name
= CTL_SLOTTABLE_UDP
,
76 .procname
= "udp_slot_table_entries",
77 .data
= &xprt_udp_slot_table_entries
,
78 .maxlen
= sizeof(unsigned int),
80 .proc_handler
= &proc_dointvec_minmax
,
81 .strategy
= &sysctl_intvec
,
82 .extra1
= &min_slot_table_size
,
83 .extra2
= &max_slot_table_size
86 .ctl_name
= CTL_SLOTTABLE_TCP
,
87 .procname
= "tcp_slot_table_entries",
88 .data
= &xprt_tcp_slot_table_entries
,
89 .maxlen
= sizeof(unsigned int),
91 .proc_handler
= &proc_dointvec_minmax
,
92 .strategy
= &sysctl_intvec
,
93 .extra1
= &min_slot_table_size
,
94 .extra2
= &max_slot_table_size
97 .ctl_name
= CTL_MIN_RESVPORT
,
98 .procname
= "min_resvport",
99 .data
= &xprt_min_resvport
,
100 .maxlen
= sizeof(unsigned int),
102 .proc_handler
= &proc_dointvec_minmax
,
103 .strategy
= &sysctl_intvec
,
104 .extra1
= &xprt_min_resvport_limit
,
105 .extra2
= &xprt_max_resvport_limit
108 .ctl_name
= CTL_MAX_RESVPORT
,
109 .procname
= "max_resvport",
110 .data
= &xprt_max_resvport
,
111 .maxlen
= sizeof(unsigned int),
113 .proc_handler
= &proc_dointvec_minmax
,
114 .strategy
= &sysctl_intvec
,
115 .extra1
= &xprt_min_resvport_limit
,
116 .extra2
= &xprt_max_resvport_limit
123 static ctl_table sunrpc_table
[] = {
125 .ctl_name
= CTL_SUNRPC
,
126 .procname
= "sunrpc",
128 .child
= xs_tunables_table
138 * How many times to try sending a request on a socket before waiting
139 * for the socket buffer to clear.
141 #define XS_SENDMSG_RETRY (10U)
144 * Time out for an RPC UDP socket connect. UDP socket connects are
145 * synchronous, but we set a timeout anyway in case of resource
146 * exhaustion on the local host.
148 #define XS_UDP_CONN_TO (5U * HZ)
151 * Wait duration for an RPC TCP connection to be established. Solaris
152 * NFS over TCP uses 60 seconds, for example, which is in line with how
153 * long a server takes to reboot.
155 #define XS_TCP_CONN_TO (60U * HZ)
158 * Wait duration for a reply from the RPC portmapper.
160 #define XS_BIND_TO (60U * HZ)
163 * Delay if a UDP socket connect error occurs. This is most likely some
164 * kind of resource problem on the local host.
166 #define XS_UDP_REEST_TO (2U * HZ)
169 * The reestablish timeout allows clients to delay for a bit before attempting
170 * to reconnect to a server that just dropped our connection.
172 * We implement an exponential backoff when trying to reestablish a TCP
173 * transport connection with the server. Some servers like to drop a TCP
174 * connection when they are overworked, so we start with a short timeout and
175 * increase over time if the server is down or not responding.
177 #define XS_TCP_INIT_REEST_TO (3U * HZ)
178 #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
181 * TCP idle timeout; client drops the transport socket if it is idle
182 * for this long. Note that we also timeout UDP sockets to prevent
183 * holding port numbers when there is no RPC traffic.
185 #define XS_IDLE_DISC_TO (5U * 60 * HZ)
188 # undef RPC_DEBUG_DATA
189 # define RPCDBG_FACILITY RPCDBG_TRANS
192 #ifdef RPC_DEBUG_DATA
193 static void xs_pktdump(char *msg
, u32
*packet
, unsigned int count
)
195 u8
*buf
= (u8
*) packet
;
198 dprintk("RPC: %s\n", msg
);
199 for (j
= 0; j
< count
&& j
< 128; j
+= 4) {
203 dprintk("0x%04x ", j
);
205 dprintk("%02x%02x%02x%02x ",
206 buf
[j
], buf
[j
+1], buf
[j
+2], buf
[j
+3]);
211 static inline void xs_pktdump(char *msg
, u32
*packet
, unsigned int count
)
218 struct rpc_xprt xprt
;
223 struct socket
* sock
;
227 * State of TCP reply receive
235 unsigned long tcp_copied
,
239 * Connection of transports
241 struct delayed_work connect_worker
;
242 struct sockaddr_storage addr
;
246 * UDP socket buffer size parameters
252 * Saved socket callback addresses
254 void (*old_data_ready
)(struct sock
*, int);
255 void (*old_state_change
)(struct sock
*);
256 void (*old_write_space
)(struct sock
*);
260 * TCP receive state flags
262 #define TCP_RCV_LAST_FRAG (1UL << 0)
263 #define TCP_RCV_COPY_FRAGHDR (1UL << 1)
264 #define TCP_RCV_COPY_XID (1UL << 2)
265 #define TCP_RCV_COPY_DATA (1UL << 3)
267 static inline struct sockaddr
*xs_addr(struct rpc_xprt
*xprt
)
269 return (struct sockaddr
*) &xprt
->addr
;
272 static inline struct sockaddr_in
*xs_addr_in(struct rpc_xprt
*xprt
)
274 return (struct sockaddr_in
*) &xprt
->addr
;
277 static inline struct sockaddr_in6
*xs_addr_in6(struct rpc_xprt
*xprt
)
279 return (struct sockaddr_in6
*) &xprt
->addr
;
282 static void xs_format_ipv4_peer_addresses(struct rpc_xprt
*xprt
)
284 struct sockaddr_in
*addr
= xs_addr_in(xprt
);
287 buf
= kzalloc(20, GFP_KERNEL
);
289 snprintf(buf
, 20, NIPQUAD_FMT
,
290 NIPQUAD(addr
->sin_addr
.s_addr
));
292 xprt
->address_strings
[RPC_DISPLAY_ADDR
] = buf
;
294 buf
= kzalloc(8, GFP_KERNEL
);
296 snprintf(buf
, 8, "%u",
297 ntohs(addr
->sin_port
));
299 xprt
->address_strings
[RPC_DISPLAY_PORT
] = buf
;
301 buf
= kzalloc(8, GFP_KERNEL
);
303 if (xprt
->prot
== IPPROTO_UDP
)
304 snprintf(buf
, 8, "udp");
306 snprintf(buf
, 8, "tcp");
308 xprt
->address_strings
[RPC_DISPLAY_PROTO
] = buf
;
310 buf
= kzalloc(48, GFP_KERNEL
);
312 snprintf(buf
, 48, "addr="NIPQUAD_FMT
" port=%u proto=%s",
313 NIPQUAD(addr
->sin_addr
.s_addr
),
314 ntohs(addr
->sin_port
),
315 xprt
->prot
== IPPROTO_UDP
? "udp" : "tcp");
317 xprt
->address_strings
[RPC_DISPLAY_ALL
] = buf
;
319 buf
= kzalloc(10, GFP_KERNEL
);
321 snprintf(buf
, 10, "%02x%02x%02x%02x",
322 NIPQUAD(addr
->sin_addr
.s_addr
));
324 xprt
->address_strings
[RPC_DISPLAY_HEX_ADDR
] = buf
;
326 buf
= kzalloc(8, GFP_KERNEL
);
328 snprintf(buf
, 8, "%4hx",
329 ntohs(addr
->sin_port
));
331 xprt
->address_strings
[RPC_DISPLAY_HEX_PORT
] = buf
;
333 buf
= kzalloc(30, GFP_KERNEL
);
335 snprintf(buf
, 30, NIPQUAD_FMT
".%u.%u",
336 NIPQUAD(addr
->sin_addr
.s_addr
),
337 ntohs(addr
->sin_port
) >> 8,
338 ntohs(addr
->sin_port
) & 0xff);
340 xprt
->address_strings
[RPC_DISPLAY_UNIVERSAL_ADDR
] = buf
;
342 xprt
->address_strings
[RPC_DISPLAY_NETID
] =
343 kstrdup(xprt
->prot
== IPPROTO_UDP
?
344 RPCBIND_NETID_UDP
: RPCBIND_NETID_TCP
, GFP_KERNEL
);
347 static void xs_format_ipv6_peer_addresses(struct rpc_xprt
*xprt
)
349 struct sockaddr_in6
*addr
= xs_addr_in6(xprt
);
352 buf
= kzalloc(40, GFP_KERNEL
);
354 snprintf(buf
, 40, NIP6_FMT
,
355 NIP6(addr
->sin6_addr
));
357 xprt
->address_strings
[RPC_DISPLAY_ADDR
] = buf
;
359 buf
= kzalloc(8, GFP_KERNEL
);
361 snprintf(buf
, 8, "%u",
362 ntohs(addr
->sin6_port
));
364 xprt
->address_strings
[RPC_DISPLAY_PORT
] = buf
;
366 buf
= kzalloc(8, GFP_KERNEL
);
368 if (xprt
->prot
== IPPROTO_UDP
)
369 snprintf(buf
, 8, "udp");
371 snprintf(buf
, 8, "tcp");
373 xprt
->address_strings
[RPC_DISPLAY_PROTO
] = buf
;
375 buf
= kzalloc(64, GFP_KERNEL
);
377 snprintf(buf
, 64, "addr="NIP6_FMT
" port=%u proto=%s",
378 NIP6(addr
->sin6_addr
),
379 ntohs(addr
->sin6_port
),
380 xprt
->prot
== IPPROTO_UDP
? "udp" : "tcp");
382 xprt
->address_strings
[RPC_DISPLAY_ALL
] = buf
;
384 buf
= kzalloc(36, GFP_KERNEL
);
386 snprintf(buf
, 36, NIP6_SEQFMT
,
387 NIP6(addr
->sin6_addr
));
389 xprt
->address_strings
[RPC_DISPLAY_HEX_ADDR
] = buf
;
391 buf
= kzalloc(8, GFP_KERNEL
);
393 snprintf(buf
, 8, "%4hx",
394 ntohs(addr
->sin6_port
));
396 xprt
->address_strings
[RPC_DISPLAY_HEX_PORT
] = buf
;
398 buf
= kzalloc(50, GFP_KERNEL
);
400 snprintf(buf
, 50, NIP6_FMT
".%u.%u",
401 NIP6(addr
->sin6_addr
),
402 ntohs(addr
->sin6_port
) >> 8,
403 ntohs(addr
->sin6_port
) & 0xff);
405 xprt
->address_strings
[RPC_DISPLAY_UNIVERSAL_ADDR
] = buf
;
407 xprt
->address_strings
[RPC_DISPLAY_NETID
] =
408 kstrdup(xprt
->prot
== IPPROTO_UDP
?
409 RPCBIND_NETID_UDP6
: RPCBIND_NETID_TCP6
, GFP_KERNEL
);
412 static void xs_free_peer_addresses(struct rpc_xprt
*xprt
)
416 for (i
= 0; i
< RPC_DISPLAY_MAX
; i
++)
417 kfree(xprt
->address_strings
[i
]);
420 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
422 static int xs_send_kvec(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
, struct kvec
*vec
, unsigned int base
, int more
)
424 struct msghdr msg
= {
426 .msg_namelen
= addrlen
,
427 .msg_flags
= XS_SENDMSG_FLAGS
| (more
? MSG_MORE
: 0),
430 .iov_base
= vec
->iov_base
+ base
,
431 .iov_len
= vec
->iov_len
- base
,
434 if (iov
.iov_len
!= 0)
435 return kernel_sendmsg(sock
, &msg
, &iov
, 1, iov
.iov_len
);
436 return kernel_sendmsg(sock
, &msg
, NULL
, 0, 0);
439 static int xs_send_pagedata(struct socket
*sock
, struct xdr_buf
*xdr
, unsigned int base
, int more
)
442 unsigned int remainder
;
445 remainder
= xdr
->page_len
- base
;
446 base
+= xdr
->page_base
;
447 ppage
= xdr
->pages
+ (base
>> PAGE_SHIFT
);
450 unsigned int len
= min_t(unsigned int, PAGE_SIZE
- base
, remainder
);
451 int flags
= XS_SENDMSG_FLAGS
;
454 if (remainder
!= 0 || more
)
456 err
= sock
->ops
->sendpage(sock
, *ppage
, base
, len
, flags
);
457 if (remainder
== 0 || err
!= len
)
471 * xs_sendpages - write pages directly to a socket
472 * @sock: socket to send on
473 * @addr: UDP only -- address of destination
474 * @addrlen: UDP only -- length of destination address
475 * @xdr: buffer containing this request
476 * @base: starting position in the buffer
479 static int xs_sendpages(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
, struct xdr_buf
*xdr
, unsigned int base
)
481 unsigned int remainder
= xdr
->len
- base
;
487 clear_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
);
493 if (base
< xdr
->head
[0].iov_len
|| addr
!= NULL
) {
494 unsigned int len
= xdr
->head
[0].iov_len
- base
;
496 err
= xs_send_kvec(sock
, addr
, addrlen
, &xdr
->head
[0], base
, remainder
!= 0);
497 if (remainder
== 0 || err
!= len
)
502 base
-= xdr
->head
[0].iov_len
;
504 if (base
< xdr
->page_len
) {
505 unsigned int len
= xdr
->page_len
- base
;
507 err
= xs_send_pagedata(sock
, xdr
, base
, remainder
!= 0);
508 if (remainder
== 0 || err
!= len
)
513 base
-= xdr
->page_len
;
515 if (base
>= xdr
->tail
[0].iov_len
)
517 err
= xs_send_kvec(sock
, NULL
, 0, &xdr
->tail
[0], base
, 0);
527 * xs_nospace - place task on wait queue if transmit was incomplete
528 * @task: task to put to sleep
531 static void xs_nospace(struct rpc_task
*task
)
533 struct rpc_rqst
*req
= task
->tk_rqstp
;
534 struct rpc_xprt
*xprt
= req
->rq_xprt
;
535 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
537 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
538 task
->tk_pid
, req
->rq_slen
- req
->rq_bytes_sent
,
541 if (test_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
)) {
542 /* Protect against races with write_space */
543 spin_lock_bh(&xprt
->transport_lock
);
545 /* Don't race with disconnect */
546 if (!xprt_connected(xprt
))
547 task
->tk_status
= -ENOTCONN
;
548 else if (test_bit(SOCK_NOSPACE
, &transport
->sock
->flags
))
549 xprt_wait_for_buffer_space(task
);
551 spin_unlock_bh(&xprt
->transport_lock
);
553 /* Keep holding the socket if it is blocked */
554 rpc_delay(task
, HZ
>>4);
558 * xs_udp_send_request - write an RPC request to a UDP socket
559 * @task: address of RPC task that manages the state of an RPC request
562 * 0: The request has been sent
563 * EAGAIN: The socket was blocked, please call again later to
564 * complete the request
565 * ENOTCONN: Caller needs to invoke connect logic then call again
566 * other: Some other error occured, the request was not sent
568 static int xs_udp_send_request(struct rpc_task
*task
)
570 struct rpc_rqst
*req
= task
->tk_rqstp
;
571 struct rpc_xprt
*xprt
= req
->rq_xprt
;
572 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
573 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
576 xs_pktdump("packet data:",
577 req
->rq_svec
->iov_base
,
578 req
->rq_svec
->iov_len
);
580 req
->rq_xtime
= jiffies
;
581 status
= xs_sendpages(transport
->sock
,
586 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
587 xdr
->len
- req
->rq_bytes_sent
, status
);
589 if (likely(status
>= (int) req
->rq_slen
))
592 /* Still some bytes left; set up for a retry later. */
600 /* When the server has died, an ICMP port unreachable message
601 * prompts ECONNREFUSED. */
607 dprintk("RPC: sendmsg returned unrecognized error %d\n",
615 static inline void xs_encode_tcp_record_marker(struct xdr_buf
*buf
)
617 u32 reclen
= buf
->len
- sizeof(rpc_fraghdr
);
618 rpc_fraghdr
*base
= buf
->head
[0].iov_base
;
619 *base
= htonl(RPC_LAST_STREAM_FRAGMENT
| reclen
);
623 * xs_tcp_send_request - write an RPC request to a TCP socket
624 * @task: address of RPC task that manages the state of an RPC request
627 * 0: The request has been sent
628 * EAGAIN: The socket was blocked, please call again later to
629 * complete the request
630 * ENOTCONN: Caller needs to invoke connect logic then call again
631 * other: Some other error occured, the request was not sent
633 * XXX: In the case of soft timeouts, should we eventually give up
634 * if sendmsg is not able to make progress?
636 static int xs_tcp_send_request(struct rpc_task
*task
)
638 struct rpc_rqst
*req
= task
->tk_rqstp
;
639 struct rpc_xprt
*xprt
= req
->rq_xprt
;
640 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
641 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
643 unsigned int retry
= 0;
645 xs_encode_tcp_record_marker(&req
->rq_snd_buf
);
647 xs_pktdump("packet data:",
648 req
->rq_svec
->iov_base
,
649 req
->rq_svec
->iov_len
);
651 /* Continue transmitting the packet/record. We must be careful
652 * to cope with writespace callbacks arriving _after_ we have
653 * called sendmsg(). */
655 req
->rq_xtime
= jiffies
;
656 status
= xs_sendpages(transport
->sock
,
657 NULL
, 0, xdr
, req
->rq_bytes_sent
);
659 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
660 xdr
->len
- req
->rq_bytes_sent
, status
);
662 if (unlikely(status
< 0))
665 /* If we've sent the entire packet, immediately
666 * reset the count of bytes sent. */
667 req
->rq_bytes_sent
+= status
;
668 task
->tk_bytes_sent
+= status
;
669 if (likely(req
->rq_bytes_sent
>= req
->rq_slen
)) {
670 req
->rq_bytes_sent
= 0;
675 if (retry
++ > XS_SENDMSG_RETRY
)
690 dprintk("RPC: sendmsg returned unrecognized error %d\n",
692 xprt_disconnect(xprt
);
700 * xs_tcp_release_xprt - clean up after a tcp transmission
704 * This cleans up if an error causes us to abort the transmission of a request.
705 * In this case, the socket may need to be reset in order to avoid confusing
708 static void xs_tcp_release_xprt(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
710 struct rpc_rqst
*req
;
712 if (task
!= xprt
->snd_task
)
716 req
= task
->tk_rqstp
;
717 if (req
->rq_bytes_sent
== 0)
719 if (req
->rq_bytes_sent
== req
->rq_snd_buf
.len
)
721 set_bit(XPRT_CLOSE_WAIT
, &task
->tk_xprt
->state
);
723 xprt_release_xprt(xprt
, task
);
727 * xs_close - close a socket
730 * This is used when all requests are complete; ie, no DRC state remains
731 * on the server we want to save.
733 static void xs_close(struct rpc_xprt
*xprt
)
735 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
736 struct socket
*sock
= transport
->sock
;
737 struct sock
*sk
= transport
->inet
;
740 goto clear_close_wait
;
742 dprintk("RPC: xs_close xprt %p\n", xprt
);
744 write_lock_bh(&sk
->sk_callback_lock
);
745 transport
->inet
= NULL
;
746 transport
->sock
= NULL
;
748 sk
->sk_user_data
= NULL
;
749 sk
->sk_data_ready
= transport
->old_data_ready
;
750 sk
->sk_state_change
= transport
->old_state_change
;
751 sk
->sk_write_space
= transport
->old_write_space
;
752 write_unlock_bh(&sk
->sk_callback_lock
);
758 smp_mb__before_clear_bit();
759 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
760 smp_mb__after_clear_bit();
764 * xs_destroy - prepare to shutdown a transport
765 * @xprt: doomed transport
768 static void xs_destroy(struct rpc_xprt
*xprt
)
770 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
772 dprintk("RPC: xs_destroy xprt %p\n", xprt
);
774 cancel_rearming_delayed_work(&transport
->connect_worker
);
776 xprt_disconnect(xprt
);
778 xs_free_peer_addresses(xprt
);
781 module_put(THIS_MODULE
);
784 static inline struct rpc_xprt
*xprt_from_sock(struct sock
*sk
)
786 return (struct rpc_xprt
*) sk
->sk_user_data
;
790 * xs_udp_data_ready - "data ready" callback for UDP sockets
791 * @sk: socket with data to read
792 * @len: how much data to read
795 static void xs_udp_data_ready(struct sock
*sk
, int len
)
797 struct rpc_task
*task
;
798 struct rpc_xprt
*xprt
;
799 struct rpc_rqst
*rovr
;
801 int err
, repsize
, copied
;
805 read_lock(&sk
->sk_callback_lock
);
806 dprintk("RPC: xs_udp_data_ready...\n");
807 if (!(xprt
= xprt_from_sock(sk
)))
810 if ((skb
= skb_recv_datagram(sk
, 0, 1, &err
)) == NULL
)
816 repsize
= skb
->len
- sizeof(struct udphdr
);
818 dprintk("RPC: impossible RPC reply size %d!\n", repsize
);
822 /* Copy the XID from the skb... */
823 xp
= skb_header_pointer(skb
, sizeof(struct udphdr
),
824 sizeof(_xid
), &_xid
);
828 /* Look up and lock the request corresponding to the given XID */
829 spin_lock(&xprt
->transport_lock
);
830 rovr
= xprt_lookup_rqst(xprt
, *xp
);
833 task
= rovr
->rq_task
;
835 if ((copied
= rovr
->rq_private_buf
.buflen
) > repsize
)
838 /* Suck it into the iovec, verify checksum if not done by hw. */
839 if (csum_partial_copy_to_xdr(&rovr
->rq_private_buf
, skb
))
842 /* Something worked... */
843 dst_confirm(skb
->dst
);
845 xprt_adjust_cwnd(task
, copied
);
846 xprt_update_rtt(task
);
847 xprt_complete_rqst(task
, copied
);
850 spin_unlock(&xprt
->transport_lock
);
852 skb_free_datagram(sk
, skb
);
854 read_unlock(&sk
->sk_callback_lock
);
857 static inline void xs_tcp_read_fraghdr(struct rpc_xprt
*xprt
, struct xdr_skb_reader
*desc
)
859 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
863 p
= ((char *) &transport
->tcp_fraghdr
) + transport
->tcp_offset
;
864 len
= sizeof(transport
->tcp_fraghdr
) - transport
->tcp_offset
;
865 used
= xdr_skb_read_bits(desc
, p
, len
);
866 transport
->tcp_offset
+= used
;
870 transport
->tcp_reclen
= ntohl(transport
->tcp_fraghdr
);
871 if (transport
->tcp_reclen
& RPC_LAST_STREAM_FRAGMENT
)
872 transport
->tcp_flags
|= TCP_RCV_LAST_FRAG
;
874 transport
->tcp_flags
&= ~TCP_RCV_LAST_FRAG
;
875 transport
->tcp_reclen
&= RPC_FRAGMENT_SIZE_MASK
;
877 transport
->tcp_flags
&= ~TCP_RCV_COPY_FRAGHDR
;
878 transport
->tcp_offset
= 0;
880 /* Sanity check of the record length */
881 if (unlikely(transport
->tcp_reclen
< 4)) {
882 dprintk("RPC: invalid TCP record fragment length\n");
883 xprt_disconnect(xprt
);
886 dprintk("RPC: reading TCP record fragment of length %d\n",
887 transport
->tcp_reclen
);
890 static void xs_tcp_check_fraghdr(struct sock_xprt
*transport
)
892 if (transport
->tcp_offset
== transport
->tcp_reclen
) {
893 transport
->tcp_flags
|= TCP_RCV_COPY_FRAGHDR
;
894 transport
->tcp_offset
= 0;
895 if (transport
->tcp_flags
& TCP_RCV_LAST_FRAG
) {
896 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
897 transport
->tcp_flags
|= TCP_RCV_COPY_XID
;
898 transport
->tcp_copied
= 0;
903 static inline void xs_tcp_read_xid(struct sock_xprt
*transport
, struct xdr_skb_reader
*desc
)
908 len
= sizeof(transport
->tcp_xid
) - transport
->tcp_offset
;
909 dprintk("RPC: reading XID (%Zu bytes)\n", len
);
910 p
= ((char *) &transport
->tcp_xid
) + transport
->tcp_offset
;
911 used
= xdr_skb_read_bits(desc
, p
, len
);
912 transport
->tcp_offset
+= used
;
915 transport
->tcp_flags
&= ~TCP_RCV_COPY_XID
;
916 transport
->tcp_flags
|= TCP_RCV_COPY_DATA
;
917 transport
->tcp_copied
= 4;
918 dprintk("RPC: reading reply for XID %08x\n",
919 ntohl(transport
->tcp_xid
));
920 xs_tcp_check_fraghdr(transport
);
923 static inline void xs_tcp_read_request(struct rpc_xprt
*xprt
, struct xdr_skb_reader
*desc
)
925 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
926 struct rpc_rqst
*req
;
927 struct xdr_buf
*rcvbuf
;
931 /* Find and lock the request corresponding to this xid */
932 spin_lock(&xprt
->transport_lock
);
933 req
= xprt_lookup_rqst(xprt
, transport
->tcp_xid
);
935 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
936 dprintk("RPC: XID %08x request not found!\n",
937 ntohl(transport
->tcp_xid
));
938 spin_unlock(&xprt
->transport_lock
);
942 rcvbuf
= &req
->rq_private_buf
;
944 if (len
> transport
->tcp_reclen
- transport
->tcp_offset
) {
945 struct xdr_skb_reader my_desc
;
947 len
= transport
->tcp_reclen
- transport
->tcp_offset
;
948 memcpy(&my_desc
, desc
, sizeof(my_desc
));
950 r
= xdr_partial_copy_from_skb(rcvbuf
, transport
->tcp_copied
,
951 &my_desc
, xdr_skb_read_bits
);
955 r
= xdr_partial_copy_from_skb(rcvbuf
, transport
->tcp_copied
,
956 desc
, xdr_skb_read_bits
);
959 transport
->tcp_copied
+= r
;
960 transport
->tcp_offset
+= r
;
963 /* Error when copying to the receive buffer,
964 * usually because we weren't able to allocate
965 * additional buffer pages. All we can do now
966 * is turn off TCP_RCV_COPY_DATA, so the request
967 * will not receive any additional updates,
969 * Any remaining data from this record will
972 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
973 dprintk("RPC: XID %08x truncated request\n",
974 ntohl(transport
->tcp_xid
));
975 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
976 "tcp_offset = %u, tcp_reclen = %u\n",
977 xprt
, transport
->tcp_copied
,
978 transport
->tcp_offset
, transport
->tcp_reclen
);
982 dprintk("RPC: XID %08x read %Zd bytes\n",
983 ntohl(transport
->tcp_xid
), r
);
984 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
985 "tcp_reclen = %u\n", xprt
, transport
->tcp_copied
,
986 transport
->tcp_offset
, transport
->tcp_reclen
);
988 if (transport
->tcp_copied
== req
->rq_private_buf
.buflen
)
989 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
990 else if (transport
->tcp_offset
== transport
->tcp_reclen
) {
991 if (transport
->tcp_flags
& TCP_RCV_LAST_FRAG
)
992 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
996 if (!(transport
->tcp_flags
& TCP_RCV_COPY_DATA
))
997 xprt_complete_rqst(req
->rq_task
, transport
->tcp_copied
);
998 spin_unlock(&xprt
->transport_lock
);
999 xs_tcp_check_fraghdr(transport
);
1002 static inline void xs_tcp_read_discard(struct sock_xprt
*transport
, struct xdr_skb_reader
*desc
)
1006 len
= transport
->tcp_reclen
- transport
->tcp_offset
;
1007 if (len
> desc
->count
)
1010 desc
->offset
+= len
;
1011 transport
->tcp_offset
+= len
;
1012 dprintk("RPC: discarded %Zu bytes\n", len
);
1013 xs_tcp_check_fraghdr(transport
);
1016 static int xs_tcp_data_recv(read_descriptor_t
*rd_desc
, struct sk_buff
*skb
, unsigned int offset
, size_t len
)
1018 struct rpc_xprt
*xprt
= rd_desc
->arg
.data
;
1019 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1020 struct xdr_skb_reader desc
= {
1026 dprintk("RPC: xs_tcp_data_recv started\n");
1028 /* Read in a new fragment marker if necessary */
1029 /* Can we ever really expect to get completely empty fragments? */
1030 if (transport
->tcp_flags
& TCP_RCV_COPY_FRAGHDR
) {
1031 xs_tcp_read_fraghdr(xprt
, &desc
);
1034 /* Read in the xid if necessary */
1035 if (transport
->tcp_flags
& TCP_RCV_COPY_XID
) {
1036 xs_tcp_read_xid(transport
, &desc
);
1039 /* Read in the request data */
1040 if (transport
->tcp_flags
& TCP_RCV_COPY_DATA
) {
1041 xs_tcp_read_request(xprt
, &desc
);
1044 /* Skip over any trailing bytes on short reads */
1045 xs_tcp_read_discard(transport
, &desc
);
1046 } while (desc
.count
);
1047 dprintk("RPC: xs_tcp_data_recv done\n");
1048 return len
- desc
.count
;
1052 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1053 * @sk: socket with data to read
1054 * @bytes: how much data to read
1057 static void xs_tcp_data_ready(struct sock
*sk
, int bytes
)
1059 struct rpc_xprt
*xprt
;
1060 read_descriptor_t rd_desc
;
1062 dprintk("RPC: xs_tcp_data_ready...\n");
1064 read_lock(&sk
->sk_callback_lock
);
1065 if (!(xprt
= xprt_from_sock(sk
)))
1070 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1071 rd_desc
.arg
.data
= xprt
;
1072 rd_desc
.count
= 65536;
1073 tcp_read_sock(sk
, &rd_desc
, xs_tcp_data_recv
);
1075 read_unlock(&sk
->sk_callback_lock
);
1079 * xs_tcp_state_change - callback to handle TCP socket state changes
1080 * @sk: socket whose state has changed
1083 static void xs_tcp_state_change(struct sock
*sk
)
1085 struct rpc_xprt
*xprt
;
1087 read_lock(&sk
->sk_callback_lock
);
1088 if (!(xprt
= xprt_from_sock(sk
)))
1090 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt
);
1091 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1092 sk
->sk_state
, xprt_connected(xprt
),
1093 sock_flag(sk
, SOCK_DEAD
),
1094 sock_flag(sk
, SOCK_ZAPPED
));
1096 switch (sk
->sk_state
) {
1097 case TCP_ESTABLISHED
:
1098 spin_lock_bh(&xprt
->transport_lock
);
1099 if (!xprt_test_and_set_connected(xprt
)) {
1100 struct sock_xprt
*transport
= container_of(xprt
,
1101 struct sock_xprt
, xprt
);
1103 /* Reset TCP record info */
1104 transport
->tcp_offset
= 0;
1105 transport
->tcp_reclen
= 0;
1106 transport
->tcp_copied
= 0;
1107 transport
->tcp_flags
=
1108 TCP_RCV_COPY_FRAGHDR
| TCP_RCV_COPY_XID
;
1110 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
1111 xprt_wake_pending_tasks(xprt
, 0);
1113 spin_unlock_bh(&xprt
->transport_lock
);
1118 case TCP_CLOSE_WAIT
:
1119 /* Try to schedule an autoclose RPC calls */
1120 set_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
1121 if (test_and_set_bit(XPRT_LOCKED
, &xprt
->state
) == 0)
1122 queue_work(rpciod_workqueue
, &xprt
->task_cleanup
);
1124 xprt_disconnect(xprt
);
1127 read_unlock(&sk
->sk_callback_lock
);
1131 * xs_udp_write_space - callback invoked when socket buffer space
1133 * @sk: socket whose state has changed
1135 * Called when more output buffer space is available for this socket.
1136 * We try not to wake our writers until they can make "significant"
1137 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1138 * with a bunch of small requests.
1140 static void xs_udp_write_space(struct sock
*sk
)
1142 read_lock(&sk
->sk_callback_lock
);
1144 /* from net/core/sock.c:sock_def_write_space */
1145 if (sock_writeable(sk
)) {
1146 struct socket
*sock
;
1147 struct rpc_xprt
*xprt
;
1149 if (unlikely(!(sock
= sk
->sk_socket
)))
1151 if (unlikely(!(xprt
= xprt_from_sock(sk
))))
1153 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE
, &sock
->flags
)))
1156 xprt_write_space(xprt
);
1160 read_unlock(&sk
->sk_callback_lock
);
1164 * xs_tcp_write_space - callback invoked when socket buffer space
1166 * @sk: socket whose state has changed
1168 * Called when more output buffer space is available for this socket.
1169 * We try not to wake our writers until they can make "significant"
1170 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1171 * with a bunch of small requests.
1173 static void xs_tcp_write_space(struct sock
*sk
)
1175 read_lock(&sk
->sk_callback_lock
);
1177 /* from net/core/stream.c:sk_stream_write_space */
1178 if (sk_stream_wspace(sk
) >= sk_stream_min_wspace(sk
)) {
1179 struct socket
*sock
;
1180 struct rpc_xprt
*xprt
;
1182 if (unlikely(!(sock
= sk
->sk_socket
)))
1184 if (unlikely(!(xprt
= xprt_from_sock(sk
))))
1186 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE
, &sock
->flags
)))
1189 xprt_write_space(xprt
);
1193 read_unlock(&sk
->sk_callback_lock
);
1196 static void xs_udp_do_set_buffer_size(struct rpc_xprt
*xprt
)
1198 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1199 struct sock
*sk
= transport
->inet
;
1201 if (transport
->rcvsize
) {
1202 sk
->sk_userlocks
|= SOCK_RCVBUF_LOCK
;
1203 sk
->sk_rcvbuf
= transport
->rcvsize
* xprt
->max_reqs
* 2;
1205 if (transport
->sndsize
) {
1206 sk
->sk_userlocks
|= SOCK_SNDBUF_LOCK
;
1207 sk
->sk_sndbuf
= transport
->sndsize
* xprt
->max_reqs
* 2;
1208 sk
->sk_write_space(sk
);
1213 * xs_udp_set_buffer_size - set send and receive limits
1214 * @xprt: generic transport
1215 * @sndsize: requested size of send buffer, in bytes
1216 * @rcvsize: requested size of receive buffer, in bytes
1218 * Set socket send and receive buffer size limits.
1220 static void xs_udp_set_buffer_size(struct rpc_xprt
*xprt
, size_t sndsize
, size_t rcvsize
)
1222 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1224 transport
->sndsize
= 0;
1226 transport
->sndsize
= sndsize
+ 1024;
1227 transport
->rcvsize
= 0;
1229 transport
->rcvsize
= rcvsize
+ 1024;
1231 xs_udp_do_set_buffer_size(xprt
);
1235 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1236 * @task: task that timed out
1238 * Adjust the congestion window after a retransmit timeout has occurred.
1240 static void xs_udp_timer(struct rpc_task
*task
)
1242 xprt_adjust_cwnd(task
, -ETIMEDOUT
);
1245 static unsigned short xs_get_random_port(void)
1247 unsigned short range
= xprt_max_resvport
- xprt_min_resvport
;
1248 unsigned short rand
= (unsigned short) net_random() % range
;
1249 return rand
+ xprt_min_resvport
;
1253 * xs_set_port - reset the port number in the remote endpoint address
1254 * @xprt: generic transport
1255 * @port: new port number
1258 static void xs_set_port(struct rpc_xprt
*xprt
, unsigned short port
)
1260 struct sockaddr
*addr
= xs_addr(xprt
);
1262 dprintk("RPC: setting port for xprt %p to %u\n", xprt
, port
);
1264 switch (addr
->sa_family
) {
1266 ((struct sockaddr_in
*)addr
)->sin_port
= htons(port
);
1269 ((struct sockaddr_in6
*)addr
)->sin6_port
= htons(port
);
1276 static int xs_bind4(struct sock_xprt
*transport
, struct socket
*sock
)
1278 struct sockaddr_in myaddr
= {
1279 .sin_family
= AF_INET
,
1281 struct sockaddr_in
*sa
;
1283 unsigned short port
= transport
->port
;
1285 if (!transport
->xprt
.resvport
)
1287 sa
= (struct sockaddr_in
*)&transport
->addr
;
1288 myaddr
.sin_addr
= sa
->sin_addr
;
1290 myaddr
.sin_port
= htons(port
);
1291 err
= kernel_bind(sock
, (struct sockaddr
*) &myaddr
,
1293 if (!transport
->xprt
.resvport
)
1296 transport
->port
= port
;
1299 if (port
<= xprt_min_resvport
)
1300 port
= xprt_max_resvport
;
1303 } while (err
== -EADDRINUSE
&& port
!= transport
->port
);
1304 dprintk("RPC: %s "NIPQUAD_FMT
":%u: %s (%d)\n",
1305 __FUNCTION__
, NIPQUAD(myaddr
.sin_addr
),
1306 port
, err
? "failed" : "ok", err
);
1310 static int xs_bind6(struct sock_xprt
*transport
, struct socket
*sock
)
1312 struct sockaddr_in6 myaddr
= {
1313 .sin6_family
= AF_INET6
,
1315 struct sockaddr_in6
*sa
;
1317 unsigned short port
= transport
->port
;
1319 if (!transport
->xprt
.resvport
)
1321 sa
= (struct sockaddr_in6
*)&transport
->addr
;
1322 myaddr
.sin6_addr
= sa
->sin6_addr
;
1324 myaddr
.sin6_port
= htons(port
);
1325 err
= kernel_bind(sock
, (struct sockaddr
*) &myaddr
,
1327 if (!transport
->xprt
.resvport
)
1330 transport
->port
= port
;
1333 if (port
<= xprt_min_resvport
)
1334 port
= xprt_max_resvport
;
1337 } while (err
== -EADDRINUSE
&& port
!= transport
->port
);
1338 dprintk("RPC: xs_bind6 "NIP6_FMT
":%u: %s (%d)\n",
1339 NIP6(myaddr
.sin6_addr
), port
, err
? "failed" : "ok", err
);
1343 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1344 static struct lock_class_key xs_key
[2];
1345 static struct lock_class_key xs_slock_key
[2];
1347 static inline void xs_reclassify_socket4(struct socket
*sock
)
1349 struct sock
*sk
= sock
->sk
;
1351 BUG_ON(sk
->sk_lock
.owner
!= NULL
);
1352 sock_lock_init_class_and_name(sk
, "slock-AF_INET-RPC",
1353 &xs_slock_key
[0], "sk_lock-AF_INET-RPC", &xs_key
[0]);
1356 static inline void xs_reclassify_socket6(struct socket
*sock
)
1358 struct sock
*sk
= sock
->sk
;
1360 BUG_ON(sk
->sk_lock
.owner
!= NULL
);
1361 sock_lock_init_class_and_name(sk
, "slock-AF_INET6-RPC",
1362 &xs_slock_key
[1], "sk_lock-AF_INET6-RPC", &xs_key
[1]);
1365 static inline void xs_reclassify_socket4(struct socket
*sock
)
1369 static inline void xs_reclassify_socket6(struct socket
*sock
)
1374 static void xs_udp_finish_connecting(struct rpc_xprt
*xprt
, struct socket
*sock
)
1376 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1378 if (!transport
->inet
) {
1379 struct sock
*sk
= sock
->sk
;
1381 write_lock_bh(&sk
->sk_callback_lock
);
1383 sk
->sk_user_data
= xprt
;
1384 transport
->old_data_ready
= sk
->sk_data_ready
;
1385 transport
->old_state_change
= sk
->sk_state_change
;
1386 transport
->old_write_space
= sk
->sk_write_space
;
1387 sk
->sk_data_ready
= xs_udp_data_ready
;
1388 sk
->sk_write_space
= xs_udp_write_space
;
1389 sk
->sk_no_check
= UDP_CSUM_NORCV
;
1390 sk
->sk_allocation
= GFP_ATOMIC
;
1392 xprt_set_connected(xprt
);
1394 /* Reset to new socket */
1395 transport
->sock
= sock
;
1396 transport
->inet
= sk
;
1398 write_unlock_bh(&sk
->sk_callback_lock
);
1400 xs_udp_do_set_buffer_size(xprt
);
1404 * xs_udp_connect_worker4 - set up a UDP socket
1405 * @work: RPC transport to connect
1407 * Invoked by a work queue tasklet.
1409 static void xs_udp_connect_worker4(struct work_struct
*work
)
1411 struct sock_xprt
*transport
=
1412 container_of(work
, struct sock_xprt
, connect_worker
.work
);
1413 struct rpc_xprt
*xprt
= &transport
->xprt
;
1414 struct socket
*sock
= transport
->sock
;
1415 int err
, status
= -EIO
;
1417 if (xprt
->shutdown
|| !xprt_bound(xprt
))
1420 /* Start by resetting any existing state */
1423 if ((err
= sock_create_kern(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
)) < 0) {
1424 dprintk("RPC: can't create UDP transport socket (%d).\n", -err
);
1427 xs_reclassify_socket4(sock
);
1429 if (xs_bind4(transport
, sock
)) {
1434 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1435 xprt
, xprt
->address_strings
[RPC_DISPLAY_ALL
]);
1437 xs_udp_finish_connecting(xprt
, sock
);
1440 xprt_wake_pending_tasks(xprt
, status
);
1441 xprt_clear_connecting(xprt
);
1445 * xs_udp_connect_worker6 - set up a UDP socket
1446 * @work: RPC transport to connect
1448 * Invoked by a work queue tasklet.
1450 static void xs_udp_connect_worker6(struct work_struct
*work
)
1452 struct sock_xprt
*transport
=
1453 container_of(work
, struct sock_xprt
, connect_worker
.work
);
1454 struct rpc_xprt
*xprt
= &transport
->xprt
;
1455 struct socket
*sock
= transport
->sock
;
1456 int err
, status
= -EIO
;
1458 if (xprt
->shutdown
|| !xprt_bound(xprt
))
1461 /* Start by resetting any existing state */
1464 if ((err
= sock_create_kern(PF_INET6
, SOCK_DGRAM
, IPPROTO_UDP
, &sock
)) < 0) {
1465 dprintk("RPC: can't create UDP transport socket (%d).\n", -err
);
1468 xs_reclassify_socket6(sock
);
1470 if (xs_bind6(transport
, sock
) < 0) {
1475 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1476 xprt
, xprt
->address_strings
[RPC_DISPLAY_ALL
]);
1478 xs_udp_finish_connecting(xprt
, sock
);
1481 xprt_wake_pending_tasks(xprt
, status
);
1482 xprt_clear_connecting(xprt
);
1486 * We need to preserve the port number so the reply cache on the server can
1487 * find our cached RPC replies when we get around to reconnecting.
1489 static void xs_tcp_reuse_connection(struct rpc_xprt
*xprt
)
1492 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1493 struct sockaddr any
;
1495 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt
);
1498 * Disconnect the transport socket by doing a connect operation
1499 * with AF_UNSPEC. This should return immediately...
1501 memset(&any
, 0, sizeof(any
));
1502 any
.sa_family
= AF_UNSPEC
;
1503 result
= kernel_connect(transport
->sock
, &any
, sizeof(any
), 0);
1505 dprintk("RPC: AF_UNSPEC connect return code %d\n",
1509 static int xs_tcp_finish_connecting(struct rpc_xprt
*xprt
, struct socket
*sock
)
1511 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1513 if (!transport
->inet
) {
1514 struct sock
*sk
= sock
->sk
;
1516 write_lock_bh(&sk
->sk_callback_lock
);
1518 sk
->sk_user_data
= xprt
;
1519 transport
->old_data_ready
= sk
->sk_data_ready
;
1520 transport
->old_state_change
= sk
->sk_state_change
;
1521 transport
->old_write_space
= sk
->sk_write_space
;
1522 sk
->sk_data_ready
= xs_tcp_data_ready
;
1523 sk
->sk_state_change
= xs_tcp_state_change
;
1524 sk
->sk_write_space
= xs_tcp_write_space
;
1525 sk
->sk_allocation
= GFP_ATOMIC
;
1527 /* socket options */
1528 sk
->sk_userlocks
|= SOCK_BINDPORT_LOCK
;
1529 sock_reset_flag(sk
, SOCK_LINGER
);
1530 tcp_sk(sk
)->linger2
= 0;
1531 tcp_sk(sk
)->nonagle
|= TCP_NAGLE_OFF
;
1533 xprt_clear_connected(xprt
);
1535 /* Reset to new socket */
1536 transport
->sock
= sock
;
1537 transport
->inet
= sk
;
1539 write_unlock_bh(&sk
->sk_callback_lock
);
1542 /* Tell the socket layer to start connecting... */
1543 xprt
->stat
.connect_count
++;
1544 xprt
->stat
.connect_start
= jiffies
;
1545 return kernel_connect(sock
, xs_addr(xprt
), xprt
->addrlen
, O_NONBLOCK
);
1549 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1550 * @work: RPC transport to connect
1552 * Invoked by a work queue tasklet.
1554 static void xs_tcp_connect_worker4(struct work_struct
*work
)
1556 struct sock_xprt
*transport
=
1557 container_of(work
, struct sock_xprt
, connect_worker
.work
);
1558 struct rpc_xprt
*xprt
= &transport
->xprt
;
1559 struct socket
*sock
= transport
->sock
;
1560 int err
, status
= -EIO
;
1562 if (xprt
->shutdown
|| !xprt_bound(xprt
))
1566 /* start from scratch */
1567 if ((err
= sock_create_kern(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
, &sock
)) < 0) {
1568 dprintk("RPC: can't create TCP transport socket (%d).\n", -err
);
1571 xs_reclassify_socket4(sock
);
1573 if (xs_bind4(transport
, sock
) < 0) {
1578 /* "close" the socket, preserving the local port */
1579 xs_tcp_reuse_connection(xprt
);
1581 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1582 xprt
, xprt
->address_strings
[RPC_DISPLAY_ALL
]);
1584 status
= xs_tcp_finish_connecting(xprt
, sock
);
1585 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1586 xprt
, -status
, xprt_connected(xprt
),
1587 sock
->sk
->sk_state
);
1595 /* retry with existing socket, after a delay */
1598 /* get rid of existing socket, and retry */
1604 xprt_wake_pending_tasks(xprt
, status
);
1606 xprt_clear_connecting(xprt
);
1610 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
1611 * @work: RPC transport to connect
1613 * Invoked by a work queue tasklet.
1615 static void xs_tcp_connect_worker6(struct work_struct
*work
)
1617 struct sock_xprt
*transport
=
1618 container_of(work
, struct sock_xprt
, connect_worker
.work
);
1619 struct rpc_xprt
*xprt
= &transport
->xprt
;
1620 struct socket
*sock
= transport
->sock
;
1621 int err
, status
= -EIO
;
1623 if (xprt
->shutdown
|| !xprt_bound(xprt
))
1627 /* start from scratch */
1628 if ((err
= sock_create_kern(PF_INET6
, SOCK_STREAM
, IPPROTO_TCP
, &sock
)) < 0) {
1629 dprintk("RPC: can't create TCP transport socket (%d).\n", -err
);
1632 xs_reclassify_socket6(sock
);
1634 if (xs_bind6(transport
, sock
) < 0) {
1639 /* "close" the socket, preserving the local port */
1640 xs_tcp_reuse_connection(xprt
);
1642 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1643 xprt
, xprt
->address_strings
[RPC_DISPLAY_ALL
]);
1645 status
= xs_tcp_finish_connecting(xprt
, sock
);
1646 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1647 xprt
, -status
, xprt_connected(xprt
), sock
->sk
->sk_state
);
1655 /* retry with existing socket, after a delay */
1658 /* get rid of existing socket, and retry */
1664 xprt_wake_pending_tasks(xprt
, status
);
1666 xprt_clear_connecting(xprt
);
1670 * xs_connect - connect a socket to a remote endpoint
1671 * @task: address of RPC task that manages state of connect request
1673 * TCP: If the remote end dropped the connection, delay reconnecting.
1675 * UDP socket connects are synchronous, but we use a work queue anyway
1676 * to guarantee that even unprivileged user processes can set up a
1677 * socket on a privileged port.
1679 * If a UDP socket connect fails, the delay behavior here prevents
1680 * retry floods (hard mounts).
1682 static void xs_connect(struct rpc_task
*task
)
1684 struct rpc_xprt
*xprt
= task
->tk_xprt
;
1685 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1687 if (xprt_test_and_set_connecting(xprt
))
1690 if (transport
->sock
!= NULL
) {
1691 dprintk("RPC: xs_connect delayed xprt %p for %lu "
1693 xprt
, xprt
->reestablish_timeout
/ HZ
);
1694 queue_delayed_work(rpciod_workqueue
,
1695 &transport
->connect_worker
,
1696 xprt
->reestablish_timeout
);
1697 xprt
->reestablish_timeout
<<= 1;
1698 if (xprt
->reestablish_timeout
> XS_TCP_MAX_REEST_TO
)
1699 xprt
->reestablish_timeout
= XS_TCP_MAX_REEST_TO
;
1701 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt
);
1702 queue_delayed_work(rpciod_workqueue
,
1703 &transport
->connect_worker
, 0);
1708 * xs_udp_print_stats - display UDP socket-specifc stats
1709 * @xprt: rpc_xprt struct containing statistics
1713 static void xs_udp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
1715 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1717 seq_printf(seq
, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
1719 xprt
->stat
.bind_count
,
1722 xprt
->stat
.bad_xids
,
1724 xprt
->stat
.bklog_u
);
1728 * xs_tcp_print_stats - display TCP socket-specifc stats
1729 * @xprt: rpc_xprt struct containing statistics
1733 static void xs_tcp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
1735 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1738 if (xprt_connected(xprt
))
1739 idle_time
= (long)(jiffies
- xprt
->last_used
) / HZ
;
1741 seq_printf(seq
, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
1743 xprt
->stat
.bind_count
,
1744 xprt
->stat
.connect_count
,
1745 xprt
->stat
.connect_time
,
1749 xprt
->stat
.bad_xids
,
1751 xprt
->stat
.bklog_u
);
1754 static struct rpc_xprt_ops xs_udp_ops
= {
1755 .set_buffer_size
= xs_udp_set_buffer_size
,
1756 .reserve_xprt
= xprt_reserve_xprt_cong
,
1757 .release_xprt
= xprt_release_xprt_cong
,
1758 .rpcbind
= rpcb_getport_async
,
1759 .set_port
= xs_set_port
,
1760 .connect
= xs_connect
,
1761 .buf_alloc
= rpc_malloc
,
1762 .buf_free
= rpc_free
,
1763 .send_request
= xs_udp_send_request
,
1764 .set_retrans_timeout
= xprt_set_retrans_timeout_rtt
,
1765 .timer
= xs_udp_timer
,
1766 .release_request
= xprt_release_rqst_cong
,
1768 .destroy
= xs_destroy
,
1769 .print_stats
= xs_udp_print_stats
,
1772 static struct rpc_xprt_ops xs_tcp_ops
= {
1773 .reserve_xprt
= xprt_reserve_xprt
,
1774 .release_xprt
= xs_tcp_release_xprt
,
1775 .rpcbind
= rpcb_getport_async
,
1776 .set_port
= xs_set_port
,
1777 .connect
= xs_connect
,
1778 .buf_alloc
= rpc_malloc
,
1779 .buf_free
= rpc_free
,
1780 .send_request
= xs_tcp_send_request
,
1781 .set_retrans_timeout
= xprt_set_retrans_timeout_def
,
1783 .destroy
= xs_destroy
,
1784 .print_stats
= xs_tcp_print_stats
,
1787 static struct rpc_xprt
*xs_setup_xprt(struct xprt_create
*args
,
1788 unsigned int slot_table_size
)
1790 struct rpc_xprt
*xprt
;
1791 struct sock_xprt
*new;
1793 if (args
->addrlen
> sizeof(xprt
->addr
)) {
1794 dprintk("RPC: xs_setup_xprt: address too large\n");
1795 return ERR_PTR(-EBADF
);
1798 new = kzalloc(sizeof(*new), GFP_KERNEL
);
1800 dprintk("RPC: xs_setup_xprt: couldn't allocate "
1802 return ERR_PTR(-ENOMEM
);
1806 xprt
->max_reqs
= slot_table_size
;
1807 xprt
->slot
= kcalloc(xprt
->max_reqs
, sizeof(struct rpc_rqst
), GFP_KERNEL
);
1808 if (xprt
->slot
== NULL
) {
1810 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
1812 return ERR_PTR(-ENOMEM
);
1815 memcpy(&xprt
->addr
, args
->dstaddr
, args
->addrlen
);
1816 xprt
->addrlen
= args
->addrlen
;
1818 memcpy(&new->addr
, args
->srcaddr
, args
->addrlen
);
1819 new->port
= xs_get_random_port();
1825 * xs_setup_udp - Set up transport to use a UDP socket
1826 * @args: rpc transport creation arguments
1829 struct rpc_xprt
*xs_setup_udp(struct xprt_create
*args
)
1831 struct sockaddr
*addr
= args
->dstaddr
;
1832 struct rpc_xprt
*xprt
;
1833 struct sock_xprt
*transport
;
1835 xprt
= xs_setup_xprt(args
, xprt_udp_slot_table_entries
);
1838 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1840 xprt
->prot
= IPPROTO_UDP
;
1842 /* XXX: header size can vary due to auth type, IPv6, etc. */
1843 xprt
->max_payload
= (1U << 16) - (MAX_HEADER
<< 3);
1845 xprt
->bind_timeout
= XS_BIND_TO
;
1846 xprt
->connect_timeout
= XS_UDP_CONN_TO
;
1847 xprt
->reestablish_timeout
= XS_UDP_REEST_TO
;
1848 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
1850 xprt
->ops
= &xs_udp_ops
;
1853 xprt
->timeout
= *args
->timeout
;
1855 xprt_set_timeout(&xprt
->timeout
, 5, 5 * HZ
);
1857 switch (addr
->sa_family
) {
1859 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
1860 xprt_set_bound(xprt
);
1862 INIT_DELAYED_WORK(&transport
->connect_worker
,
1863 xs_udp_connect_worker4
);
1864 xs_format_ipv4_peer_addresses(xprt
);
1867 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
1868 xprt_set_bound(xprt
);
1870 INIT_DELAYED_WORK(&transport
->connect_worker
,
1871 xs_udp_connect_worker6
);
1872 xs_format_ipv6_peer_addresses(xprt
);
1876 return ERR_PTR(-EAFNOSUPPORT
);
1879 dprintk("RPC: set up transport to address %s\n",
1880 xprt
->address_strings
[RPC_DISPLAY_ALL
]);
1882 if (try_module_get(THIS_MODULE
))
1887 return ERR_PTR(-EINVAL
);
1891 * xs_setup_tcp - Set up transport to use a TCP socket
1892 * @args: rpc transport creation arguments
1895 struct rpc_xprt
*xs_setup_tcp(struct xprt_create
*args
)
1897 struct sockaddr
*addr
= args
->dstaddr
;
1898 struct rpc_xprt
*xprt
;
1899 struct sock_xprt
*transport
;
1901 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
);
1904 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1906 xprt
->prot
= IPPROTO_TCP
;
1907 xprt
->tsh_size
= sizeof(rpc_fraghdr
) / sizeof(u32
);
1908 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
1910 xprt
->bind_timeout
= XS_BIND_TO
;
1911 xprt
->connect_timeout
= XS_TCP_CONN_TO
;
1912 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
1913 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
1915 xprt
->ops
= &xs_tcp_ops
;
1918 xprt
->timeout
= *args
->timeout
;
1920 xprt_set_timeout(&xprt
->timeout
, 2, 60 * HZ
);
1922 switch (addr
->sa_family
) {
1924 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
1925 xprt_set_bound(xprt
);
1927 INIT_DELAYED_WORK(&transport
->connect_worker
, xs_tcp_connect_worker4
);
1928 xs_format_ipv4_peer_addresses(xprt
);
1931 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
1932 xprt_set_bound(xprt
);
1934 INIT_DELAYED_WORK(&transport
->connect_worker
, xs_tcp_connect_worker6
);
1935 xs_format_ipv6_peer_addresses(xprt
);
1939 return ERR_PTR(-EAFNOSUPPORT
);
1942 dprintk("RPC: set up transport to address %s\n",
1943 xprt
->address_strings
[RPC_DISPLAY_ALL
]);
1945 if (try_module_get(THIS_MODULE
))
1950 return ERR_PTR(-EINVAL
);
1953 static struct xprt_class xs_udp_transport
= {
1954 .list
= LIST_HEAD_INIT(xs_udp_transport
.list
),
1956 .owner
= THIS_MODULE
,
1958 .protocol
= IPPROTO_UDP
,
1959 .setup
= xs_setup_udp
,
1962 static struct xprt_class xs_tcp_transport
= {
1963 .list
= LIST_HEAD_INIT(xs_tcp_transport
.list
),
1965 .owner
= THIS_MODULE
,
1967 .protocol
= IPPROTO_TCP
,
1968 .setup
= xs_setup_tcp
,
1972 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
1975 int init_socket_xprt(void)
1978 if (!sunrpc_table_header
)
1979 sunrpc_table_header
= register_sysctl_table(sunrpc_table
);
1982 xprt_register_transport(&xs_udp_transport
);
1983 xprt_register_transport(&xs_tcp_transport
);
1989 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
1992 void cleanup_socket_xprt(void)
1995 if (sunrpc_table_header
) {
1996 unregister_sysctl_table(sunrpc_table_header
);
1997 sunrpc_table_header
= NULL
;
2001 xprt_unregister_transport(&xs_udp_transport
);
2002 xprt_unregister_transport(&xs_tcp_transport
);