2 * linux/net/sunrpc/xprtsock.c
4 * Client-side transport implementation for sockets.
6 * TCP callback races fixes (C) 1998 Red Hat
7 * TCP send fixes (C) 1998 Red Hat
8 * TCP NFS related read + write fixes
9 * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
11 * Rewrite of larges part of the code in order to stabilize TCP stuff.
12 * Fix behaviour when socket buffer is full.
13 * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
15 * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
17 * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
18 * <gilles.quillard@bull.net>
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/capability.h>
26 #include <linux/pagemap.h>
27 #include <linux/errno.h>
28 #include <linux/socket.h>
30 #include <linux/net.h>
33 #include <linux/udp.h>
34 #include <linux/tcp.h>
35 #include <linux/sunrpc/clnt.h>
36 #include <linux/sunrpc/addr.h>
37 #include <linux/sunrpc/sched.h>
38 #include <linux/sunrpc/svcsock.h>
39 #include <linux/sunrpc/xprtsock.h>
40 #include <linux/file.h>
41 #ifdef CONFIG_SUNRPC_BACKCHANNEL
42 #include <linux/sunrpc/bc_xprt.h>
46 #include <net/checksum.h>
50 #include <trace/events/sunrpc.h>
54 static void xs_close(struct rpc_xprt
*xprt
);
59 static unsigned int xprt_udp_slot_table_entries
= RPC_DEF_SLOT_TABLE
;
60 static unsigned int xprt_tcp_slot_table_entries
= RPC_MIN_SLOT_TABLE
;
61 static unsigned int xprt_max_tcp_slot_table_entries
= RPC_MAX_SLOT_TABLE
;
63 static unsigned int xprt_min_resvport
= RPC_DEF_MIN_RESVPORT
;
64 static unsigned int xprt_max_resvport
= RPC_DEF_MAX_RESVPORT
;
66 #define XS_TCP_LINGER_TO (15U * HZ)
67 static unsigned int xs_tcp_fin_timeout __read_mostly
= XS_TCP_LINGER_TO
;
70 * We can register our own files under /proc/sys/sunrpc by
71 * calling register_sysctl_table() again. The files in that
72 * directory become the union of all files registered there.
74 * We simply need to make sure that we don't collide with
75 * someone else's file names!
80 static unsigned int min_slot_table_size
= RPC_MIN_SLOT_TABLE
;
81 static unsigned int max_slot_table_size
= RPC_MAX_SLOT_TABLE
;
82 static unsigned int max_tcp_slot_table_limit
= RPC_MAX_SLOT_TABLE_LIMIT
;
83 static unsigned int xprt_min_resvport_limit
= RPC_MIN_RESVPORT
;
84 static unsigned int xprt_max_resvport_limit
= RPC_MAX_RESVPORT
;
86 static struct ctl_table_header
*sunrpc_table_header
;
89 * FIXME: changing the UDP slot table size should also resize the UDP
90 * socket buffers for existing UDP transports
92 static struct ctl_table xs_tunables_table
[] = {
94 .procname
= "udp_slot_table_entries",
95 .data
= &xprt_udp_slot_table_entries
,
96 .maxlen
= sizeof(unsigned int),
98 .proc_handler
= proc_dointvec_minmax
,
99 .extra1
= &min_slot_table_size
,
100 .extra2
= &max_slot_table_size
103 .procname
= "tcp_slot_table_entries",
104 .data
= &xprt_tcp_slot_table_entries
,
105 .maxlen
= sizeof(unsigned int),
107 .proc_handler
= proc_dointvec_minmax
,
108 .extra1
= &min_slot_table_size
,
109 .extra2
= &max_slot_table_size
112 .procname
= "tcp_max_slot_table_entries",
113 .data
= &xprt_max_tcp_slot_table_entries
,
114 .maxlen
= sizeof(unsigned int),
116 .proc_handler
= proc_dointvec_minmax
,
117 .extra1
= &min_slot_table_size
,
118 .extra2
= &max_tcp_slot_table_limit
121 .procname
= "min_resvport",
122 .data
= &xprt_min_resvport
,
123 .maxlen
= sizeof(unsigned int),
125 .proc_handler
= proc_dointvec_minmax
,
126 .extra1
= &xprt_min_resvport_limit
,
127 .extra2
= &xprt_max_resvport_limit
130 .procname
= "max_resvport",
131 .data
= &xprt_max_resvport
,
132 .maxlen
= sizeof(unsigned int),
134 .proc_handler
= proc_dointvec_minmax
,
135 .extra1
= &xprt_min_resvport_limit
,
136 .extra2
= &xprt_max_resvport_limit
139 .procname
= "tcp_fin_timeout",
140 .data
= &xs_tcp_fin_timeout
,
141 .maxlen
= sizeof(xs_tcp_fin_timeout
),
143 .proc_handler
= proc_dointvec_jiffies
,
148 static struct ctl_table sunrpc_table
[] = {
150 .procname
= "sunrpc",
152 .child
= xs_tunables_table
160 * Wait duration for a reply from the RPC portmapper.
162 #define XS_BIND_TO (60U * HZ)
165 * Delay if a UDP socket connect error occurs. This is most likely some
166 * kind of resource problem on the local host.
168 #define XS_UDP_REEST_TO (2U * HZ)
171 * The reestablish timeout allows clients to delay for a bit before attempting
172 * to reconnect to a server that just dropped our connection.
174 * We implement an exponential backoff when trying to reestablish a TCP
175 * transport connection with the server. Some servers like to drop a TCP
176 * connection when they are overworked, so we start with a short timeout and
177 * increase over time if the server is down or not responding.
179 #define XS_TCP_INIT_REEST_TO (3U * HZ)
180 #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
183 * TCP idle timeout; client drops the transport socket if it is idle
184 * for this long. Note that we also timeout UDP sockets to prevent
185 * holding port numbers when there is no RPC traffic.
187 #define XS_IDLE_DISC_TO (5U * 60 * HZ)
190 # undef RPC_DEBUG_DATA
191 # define RPCDBG_FACILITY RPCDBG_TRANS
194 #ifdef RPC_DEBUG_DATA
195 static void xs_pktdump(char *msg
, u32
*packet
, unsigned int count
)
197 u8
*buf
= (u8
*) packet
;
200 dprintk("RPC: %s\n", msg
);
201 for (j
= 0; j
< count
&& j
< 128; j
+= 4) {
205 dprintk("0x%04x ", j
);
207 dprintk("%02x%02x%02x%02x ",
208 buf
[j
], buf
[j
+1], buf
[j
+2], buf
[j
+3]);
213 static inline void xs_pktdump(char *msg
, u32
*packet
, unsigned int count
)
220 struct rpc_xprt xprt
;
225 struct socket
* sock
;
229 * State of TCP reply receive
238 unsigned long tcp_copied
,
242 * Connection of transports
244 struct delayed_work connect_worker
;
245 struct sockaddr_storage srcaddr
;
246 unsigned short srcport
;
249 * UDP socket buffer size parameters
255 * Saved socket callback addresses
257 void (*old_data_ready
)(struct sock
*);
258 void (*old_state_change
)(struct sock
*);
259 void (*old_write_space
)(struct sock
*);
260 void (*old_error_report
)(struct sock
*);
264 * TCP receive state flags
266 #define TCP_RCV_LAST_FRAG (1UL << 0)
267 #define TCP_RCV_COPY_FRAGHDR (1UL << 1)
268 #define TCP_RCV_COPY_XID (1UL << 2)
269 #define TCP_RCV_COPY_DATA (1UL << 3)
270 #define TCP_RCV_READ_CALLDIR (1UL << 4)
271 #define TCP_RCV_COPY_CALLDIR (1UL << 5)
276 #define TCP_RPC_REPLY (1UL << 6)
278 static inline struct rpc_xprt
*xprt_from_sock(struct sock
*sk
)
280 return (struct rpc_xprt
*) sk
->sk_user_data
;
283 static inline struct sockaddr
*xs_addr(struct rpc_xprt
*xprt
)
285 return (struct sockaddr
*) &xprt
->addr
;
288 static inline struct sockaddr_un
*xs_addr_un(struct rpc_xprt
*xprt
)
290 return (struct sockaddr_un
*) &xprt
->addr
;
293 static inline struct sockaddr_in
*xs_addr_in(struct rpc_xprt
*xprt
)
295 return (struct sockaddr_in
*) &xprt
->addr
;
298 static inline struct sockaddr_in6
*xs_addr_in6(struct rpc_xprt
*xprt
)
300 return (struct sockaddr_in6
*) &xprt
->addr
;
303 static void xs_format_common_peer_addresses(struct rpc_xprt
*xprt
)
305 struct sockaddr
*sap
= xs_addr(xprt
);
306 struct sockaddr_in6
*sin6
;
307 struct sockaddr_in
*sin
;
308 struct sockaddr_un
*sun
;
311 switch (sap
->sa_family
) {
313 sun
= xs_addr_un(xprt
);
314 strlcpy(buf
, sun
->sun_path
, sizeof(buf
));
315 xprt
->address_strings
[RPC_DISPLAY_ADDR
] =
316 kstrdup(buf
, GFP_KERNEL
);
319 (void)rpc_ntop(sap
, buf
, sizeof(buf
));
320 xprt
->address_strings
[RPC_DISPLAY_ADDR
] =
321 kstrdup(buf
, GFP_KERNEL
);
322 sin
= xs_addr_in(xprt
);
323 snprintf(buf
, sizeof(buf
), "%08x", ntohl(sin
->sin_addr
.s_addr
));
326 (void)rpc_ntop(sap
, buf
, sizeof(buf
));
327 xprt
->address_strings
[RPC_DISPLAY_ADDR
] =
328 kstrdup(buf
, GFP_KERNEL
);
329 sin6
= xs_addr_in6(xprt
);
330 snprintf(buf
, sizeof(buf
), "%pi6", &sin6
->sin6_addr
);
336 xprt
->address_strings
[RPC_DISPLAY_HEX_ADDR
] = kstrdup(buf
, GFP_KERNEL
);
339 static void xs_format_common_peer_ports(struct rpc_xprt
*xprt
)
341 struct sockaddr
*sap
= xs_addr(xprt
);
344 snprintf(buf
, sizeof(buf
), "%u", rpc_get_port(sap
));
345 xprt
->address_strings
[RPC_DISPLAY_PORT
] = kstrdup(buf
, GFP_KERNEL
);
347 snprintf(buf
, sizeof(buf
), "%4hx", rpc_get_port(sap
));
348 xprt
->address_strings
[RPC_DISPLAY_HEX_PORT
] = kstrdup(buf
, GFP_KERNEL
);
351 static void xs_format_peer_addresses(struct rpc_xprt
*xprt
,
352 const char *protocol
,
355 xprt
->address_strings
[RPC_DISPLAY_PROTO
] = protocol
;
356 xprt
->address_strings
[RPC_DISPLAY_NETID
] = netid
;
357 xs_format_common_peer_addresses(xprt
);
358 xs_format_common_peer_ports(xprt
);
361 static void xs_update_peer_port(struct rpc_xprt
*xprt
)
363 kfree(xprt
->address_strings
[RPC_DISPLAY_HEX_PORT
]);
364 kfree(xprt
->address_strings
[RPC_DISPLAY_PORT
]);
366 xs_format_common_peer_ports(xprt
);
369 static void xs_free_peer_addresses(struct rpc_xprt
*xprt
)
373 for (i
= 0; i
< RPC_DISPLAY_MAX
; i
++)
375 case RPC_DISPLAY_PROTO
:
376 case RPC_DISPLAY_NETID
:
379 kfree(xprt
->address_strings
[i
]);
383 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
385 static int xs_send_kvec(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
, struct kvec
*vec
, unsigned int base
, int more
)
387 struct msghdr msg
= {
389 .msg_namelen
= addrlen
,
390 .msg_flags
= XS_SENDMSG_FLAGS
| (more
? MSG_MORE
: 0),
393 .iov_base
= vec
->iov_base
+ base
,
394 .iov_len
= vec
->iov_len
- base
,
397 if (iov
.iov_len
!= 0)
398 return kernel_sendmsg(sock
, &msg
, &iov
, 1, iov
.iov_len
);
399 return kernel_sendmsg(sock
, &msg
, NULL
, 0, 0);
402 static int xs_send_pagedata(struct socket
*sock
, struct xdr_buf
*xdr
, unsigned int base
, int more
, bool zerocopy
)
404 ssize_t (*do_sendpage
)(struct socket
*sock
, struct page
*page
,
405 int offset
, size_t size
, int flags
);
407 unsigned int remainder
;
410 remainder
= xdr
->page_len
- base
;
411 base
+= xdr
->page_base
;
412 ppage
= xdr
->pages
+ (base
>> PAGE_SHIFT
);
414 do_sendpage
= sock
->ops
->sendpage
;
416 do_sendpage
= sock_no_sendpage
;
418 unsigned int len
= min_t(unsigned int, PAGE_SIZE
- base
, remainder
);
419 int flags
= XS_SENDMSG_FLAGS
;
422 if (remainder
!= 0 || more
)
424 err
= do_sendpage(sock
, *ppage
, base
, len
, flags
);
425 if (remainder
== 0 || err
!= len
)
439 * xs_sendpages - write pages directly to a socket
440 * @sock: socket to send on
441 * @addr: UDP only -- address of destination
442 * @addrlen: UDP only -- length of destination address
443 * @xdr: buffer containing this request
444 * @base: starting position in the buffer
445 * @zerocopy: true if it is safe to use sendpage()
448 static int xs_sendpages(struct socket
*sock
, struct sockaddr
*addr
, int addrlen
, struct xdr_buf
*xdr
, unsigned int base
, bool zerocopy
)
450 unsigned int remainder
= xdr
->len
- base
;
456 clear_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
);
462 if (base
< xdr
->head
[0].iov_len
|| addr
!= NULL
) {
463 unsigned int len
= xdr
->head
[0].iov_len
- base
;
465 err
= xs_send_kvec(sock
, addr
, addrlen
, &xdr
->head
[0], base
, remainder
!= 0);
466 if (remainder
== 0 || err
!= len
)
471 base
-= xdr
->head
[0].iov_len
;
473 if (base
< xdr
->page_len
) {
474 unsigned int len
= xdr
->page_len
- base
;
476 err
= xs_send_pagedata(sock
, xdr
, base
, remainder
!= 0, zerocopy
);
477 if (remainder
== 0 || err
!= len
)
482 base
-= xdr
->page_len
;
484 if (base
>= xdr
->tail
[0].iov_len
)
486 err
= xs_send_kvec(sock
, NULL
, 0, &xdr
->tail
[0], base
, 0);
495 static void xs_nospace_callback(struct rpc_task
*task
)
497 struct sock_xprt
*transport
= container_of(task
->tk_rqstp
->rq_xprt
, struct sock_xprt
, xprt
);
499 transport
->inet
->sk_write_pending
--;
500 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
504 * xs_nospace - place task on wait queue if transmit was incomplete
505 * @task: task to put to sleep
508 static int xs_nospace(struct rpc_task
*task
)
510 struct rpc_rqst
*req
= task
->tk_rqstp
;
511 struct rpc_xprt
*xprt
= req
->rq_xprt
;
512 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
513 struct sock
*sk
= transport
->inet
;
516 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
517 task
->tk_pid
, req
->rq_slen
- req
->rq_bytes_sent
,
520 /* Protect against races with write_space */
521 spin_lock_bh(&xprt
->transport_lock
);
523 /* Don't race with disconnect */
524 if (xprt_connected(xprt
)) {
525 if (test_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
)) {
527 * Notify TCP that we're limited by the application
530 set_bit(SOCK_NOSPACE
, &transport
->sock
->flags
);
531 sk
->sk_write_pending
++;
532 /* ...and wait for more buffer space */
533 xprt_wait_for_buffer_space(task
, xs_nospace_callback
);
536 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
540 spin_unlock_bh(&xprt
->transport_lock
);
542 /* Race breaker in case memory is freed before above code is called */
543 sk
->sk_write_space(sk
);
548 * Construct a stream transport record marker in @buf.
550 static inline void xs_encode_stream_record_marker(struct xdr_buf
*buf
)
552 u32 reclen
= buf
->len
- sizeof(rpc_fraghdr
);
553 rpc_fraghdr
*base
= buf
->head
[0].iov_base
;
554 *base
= cpu_to_be32(RPC_LAST_STREAM_FRAGMENT
| reclen
);
558 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
559 * @task: 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_local_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
=
573 container_of(xprt
, struct sock_xprt
, xprt
);
574 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
577 xs_encode_stream_record_marker(&req
->rq_snd_buf
);
579 xs_pktdump("packet data:",
580 req
->rq_svec
->iov_base
, req
->rq_svec
->iov_len
);
582 status
= xs_sendpages(transport
->sock
, NULL
, 0,
583 xdr
, req
->rq_bytes_sent
, true);
584 dprintk("RPC: %s(%u) = %d\n",
585 __func__
, xdr
->len
- req
->rq_bytes_sent
, status
);
586 if (likely(status
>= 0)) {
587 req
->rq_bytes_sent
+= status
;
588 req
->rq_xmit_bytes_sent
+= status
;
589 if (likely(req
->rq_bytes_sent
>= req
->rq_slen
)) {
590 req
->rq_bytes_sent
= 0;
598 status
= xs_nospace(task
);
601 dprintk("RPC: sendmsg returned unrecognized error %d\n",
612 * xs_udp_send_request - write an RPC request to a UDP socket
613 * @task: address of RPC task that manages the state of an RPC request
616 * 0: The request has been sent
617 * EAGAIN: The socket was blocked, please call again later to
618 * complete the request
619 * ENOTCONN: Caller needs to invoke connect logic then call again
620 * other: Some other error occurred, the request was not sent
622 static int xs_udp_send_request(struct rpc_task
*task
)
624 struct rpc_rqst
*req
= task
->tk_rqstp
;
625 struct rpc_xprt
*xprt
= req
->rq_xprt
;
626 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
627 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
630 xs_pktdump("packet data:",
631 req
->rq_svec
->iov_base
,
632 req
->rq_svec
->iov_len
);
634 if (!xprt_bound(xprt
))
636 status
= xs_sendpages(transport
->sock
,
639 req
->rq_bytes_sent
, true);
641 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
642 xdr
->len
- req
->rq_bytes_sent
, status
);
645 req
->rq_xmit_bytes_sent
+= status
;
646 if (status
>= req
->rq_slen
)
648 /* Still some bytes left; set up for a retry later. */
655 /* Should we call xs_close() here? */
658 status
= xs_nospace(task
);
661 dprintk("RPC: sendmsg returned unrecognized error %d\n",
666 /* When the server has died, an ICMP port unreachable message
667 * prompts ECONNREFUSED. */
668 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
675 * xs_tcp_shutdown - gracefully shut down a TCP socket
678 * Initiates a graceful shutdown of the TCP socket by calling the
679 * equivalent of shutdown(SHUT_WR);
681 static void xs_tcp_shutdown(struct rpc_xprt
*xprt
)
683 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
684 struct socket
*sock
= transport
->sock
;
687 kernel_sock_shutdown(sock
, SHUT_WR
);
688 trace_rpc_socket_shutdown(xprt
, sock
);
693 * xs_tcp_send_request - write an RPC request to a TCP socket
694 * @task: address of RPC task that manages the state of an RPC request
697 * 0: The request has been sent
698 * EAGAIN: The socket was blocked, please call again later to
699 * complete the request
700 * ENOTCONN: Caller needs to invoke connect logic then call again
701 * other: Some other error occurred, the request was not sent
703 * XXX: In the case of soft timeouts, should we eventually give up
704 * if sendmsg is not able to make progress?
706 static int xs_tcp_send_request(struct rpc_task
*task
)
708 struct rpc_rqst
*req
= task
->tk_rqstp
;
709 struct rpc_xprt
*xprt
= req
->rq_xprt
;
710 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
711 struct xdr_buf
*xdr
= &req
->rq_snd_buf
;
712 bool zerocopy
= true;
715 xs_encode_stream_record_marker(&req
->rq_snd_buf
);
717 xs_pktdump("packet data:",
718 req
->rq_svec
->iov_base
,
719 req
->rq_svec
->iov_len
);
720 /* Don't use zero copy if this is a resend. If the RPC call
721 * completes while the socket holds a reference to the pages,
722 * then we may end up resending corrupted data.
724 if (task
->tk_flags
& RPC_TASK_SENT
)
727 /* Continue transmitting the packet/record. We must be careful
728 * to cope with writespace callbacks arriving _after_ we have
729 * called sendmsg(). */
731 status
= xs_sendpages(transport
->sock
,
732 NULL
, 0, xdr
, req
->rq_bytes_sent
,
735 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
736 xdr
->len
- req
->rq_bytes_sent
, status
);
738 if (unlikely(status
< 0))
741 /* If we've sent the entire packet, immediately
742 * reset the count of bytes sent. */
743 req
->rq_bytes_sent
+= status
;
744 req
->rq_xmit_bytes_sent
+= status
;
745 if (likely(req
->rq_bytes_sent
>= req
->rq_slen
)) {
746 req
->rq_bytes_sent
= 0;
759 /* Should we call xs_close() here? */
762 status
= xs_nospace(task
);
765 dprintk("RPC: sendmsg returned unrecognized error %d\n",
768 xs_tcp_shutdown(xprt
);
772 clear_bit(SOCK_ASYNC_NOSPACE
, &transport
->sock
->flags
);
779 * xs_tcp_release_xprt - clean up after a tcp transmission
783 * This cleans up if an error causes us to abort the transmission of a request.
784 * In this case, the socket may need to be reset in order to avoid confusing
787 static void xs_tcp_release_xprt(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
789 struct rpc_rqst
*req
;
791 if (task
!= xprt
->snd_task
)
795 req
= task
->tk_rqstp
;
798 if (req
->rq_bytes_sent
== 0)
800 if (req
->rq_bytes_sent
== req
->rq_snd_buf
.len
)
802 set_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
804 xprt_release_xprt(xprt
, task
);
807 static void xs_save_old_callbacks(struct sock_xprt
*transport
, struct sock
*sk
)
809 transport
->old_data_ready
= sk
->sk_data_ready
;
810 transport
->old_state_change
= sk
->sk_state_change
;
811 transport
->old_write_space
= sk
->sk_write_space
;
812 transport
->old_error_report
= sk
->sk_error_report
;
815 static void xs_restore_old_callbacks(struct sock_xprt
*transport
, struct sock
*sk
)
817 sk
->sk_data_ready
= transport
->old_data_ready
;
818 sk
->sk_state_change
= transport
->old_state_change
;
819 sk
->sk_write_space
= transport
->old_write_space
;
820 sk
->sk_error_report
= transport
->old_error_report
;
824 * xs_error_report - callback to handle TCP socket state errors
827 * Note: we don't call sock_error() since there may be a rpc_task
828 * using the socket, and so we don't want to clear sk->sk_err.
830 static void xs_error_report(struct sock
*sk
)
832 struct rpc_xprt
*xprt
;
835 read_lock_bh(&sk
->sk_callback_lock
);
836 if (!(xprt
= xprt_from_sock(sk
)))
842 dprintk("RPC: xs_error_report client %p, error=%d...\n",
844 trace_rpc_socket_error(xprt
, sk
->sk_socket
, err
);
845 if (test_bit(XPRT_CONNECTION_REUSE
, &xprt
->state
))
847 xprt_wake_pending_tasks(xprt
, err
);
849 read_unlock_bh(&sk
->sk_callback_lock
);
852 static void xs_reset_transport(struct sock_xprt
*transport
)
854 struct socket
*sock
= transport
->sock
;
855 struct sock
*sk
= transport
->inet
;
860 transport
->srcport
= 0;
862 write_lock_bh(&sk
->sk_callback_lock
);
863 transport
->inet
= NULL
;
864 transport
->sock
= NULL
;
866 sk
->sk_user_data
= NULL
;
868 xs_restore_old_callbacks(transport
, sk
);
869 write_unlock_bh(&sk
->sk_callback_lock
);
871 trace_rpc_socket_close(&transport
->xprt
, sock
);
876 * xs_close - close a socket
879 * This is used when all requests are complete; ie, no DRC state remains
880 * on the server we want to save.
882 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
883 * xs_reset_transport() zeroing the socket from underneath a writer.
885 static void xs_close(struct rpc_xprt
*xprt
)
887 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
889 dprintk("RPC: xs_close xprt %p\n", xprt
);
891 cancel_delayed_work_sync(&transport
->connect_worker
);
893 xs_reset_transport(transport
);
894 xprt
->reestablish_timeout
= 0;
896 smp_mb__before_atomic();
897 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
898 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
899 clear_bit(XPRT_CLOSING
, &xprt
->state
);
900 smp_mb__after_atomic();
901 xprt_disconnect_done(xprt
);
904 static void xs_tcp_close(struct rpc_xprt
*xprt
)
906 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE
, &xprt
->state
))
909 xs_tcp_shutdown(xprt
);
912 static void xs_xprt_free(struct rpc_xprt
*xprt
)
914 xs_free_peer_addresses(xprt
);
919 * xs_destroy - prepare to shutdown a transport
920 * @xprt: doomed transport
923 static void xs_destroy(struct rpc_xprt
*xprt
)
925 dprintk("RPC: xs_destroy xprt %p\n", xprt
);
929 module_put(THIS_MODULE
);
932 static int xs_local_copy_to_xdr(struct xdr_buf
*xdr
, struct sk_buff
*skb
)
934 struct xdr_skb_reader desc
= {
936 .offset
= sizeof(rpc_fraghdr
),
937 .count
= skb
->len
- sizeof(rpc_fraghdr
),
940 if (xdr_partial_copy_from_skb(xdr
, 0, &desc
, xdr_skb_read_bits
) < 0)
948 * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
949 * @sk: socket with data to read
950 * @len: how much data to read
952 * Currently this assumes we can read the whole reply in a single gulp.
954 static void xs_local_data_ready(struct sock
*sk
)
956 struct rpc_task
*task
;
957 struct rpc_xprt
*xprt
;
958 struct rpc_rqst
*rovr
;
960 int err
, repsize
, copied
;
964 read_lock_bh(&sk
->sk_callback_lock
);
965 dprintk("RPC: %s...\n", __func__
);
966 xprt
= xprt_from_sock(sk
);
970 skb
= skb_recv_datagram(sk
, 0, 1, &err
);
974 repsize
= skb
->len
- sizeof(rpc_fraghdr
);
976 dprintk("RPC: impossible RPC reply size %d\n", repsize
);
980 /* Copy the XID from the skb... */
981 xp
= skb_header_pointer(skb
, sizeof(rpc_fraghdr
), sizeof(_xid
), &_xid
);
985 /* Look up and lock the request corresponding to the given XID */
986 spin_lock(&xprt
->transport_lock
);
987 rovr
= xprt_lookup_rqst(xprt
, *xp
);
990 task
= rovr
->rq_task
;
992 copied
= rovr
->rq_private_buf
.buflen
;
993 if (copied
> repsize
)
996 if (xs_local_copy_to_xdr(&rovr
->rq_private_buf
, skb
)) {
997 dprintk("RPC: sk_buff copy failed\n");
1001 xprt_complete_rqst(task
, copied
);
1004 spin_unlock(&xprt
->transport_lock
);
1006 skb_free_datagram(sk
, skb
);
1008 read_unlock_bh(&sk
->sk_callback_lock
);
1012 * xs_udp_data_ready - "data ready" callback for UDP sockets
1013 * @sk: socket with data to read
1014 * @len: how much data to read
1017 static void xs_udp_data_ready(struct sock
*sk
)
1019 struct rpc_task
*task
;
1020 struct rpc_xprt
*xprt
;
1021 struct rpc_rqst
*rovr
;
1022 struct sk_buff
*skb
;
1023 int err
, repsize
, copied
;
1027 read_lock_bh(&sk
->sk_callback_lock
);
1028 dprintk("RPC: xs_udp_data_ready...\n");
1029 if (!(xprt
= xprt_from_sock(sk
)))
1032 if ((skb
= skb_recv_datagram(sk
, 0, 1, &err
)) == NULL
)
1035 repsize
= skb
->len
- sizeof(struct udphdr
);
1037 dprintk("RPC: impossible RPC reply size %d!\n", repsize
);
1041 /* Copy the XID from the skb... */
1042 xp
= skb_header_pointer(skb
, sizeof(struct udphdr
),
1043 sizeof(_xid
), &_xid
);
1047 /* Look up and lock the request corresponding to the given XID */
1048 spin_lock(&xprt
->transport_lock
);
1049 rovr
= xprt_lookup_rqst(xprt
, *xp
);
1052 task
= rovr
->rq_task
;
1054 if ((copied
= rovr
->rq_private_buf
.buflen
) > repsize
)
1057 /* Suck it into the iovec, verify checksum if not done by hw. */
1058 if (csum_partial_copy_to_xdr(&rovr
->rq_private_buf
, skb
)) {
1059 UDPX_INC_STATS_BH(sk
, UDP_MIB_INERRORS
);
1063 UDPX_INC_STATS_BH(sk
, UDP_MIB_INDATAGRAMS
);
1065 xprt_adjust_cwnd(xprt
, task
, copied
);
1066 xprt_complete_rqst(task
, copied
);
1069 spin_unlock(&xprt
->transport_lock
);
1071 skb_free_datagram(sk
, skb
);
1073 read_unlock_bh(&sk
->sk_callback_lock
);
1077 * Helper function to force a TCP close if the server is sending
1078 * junk and/or it has put us in CLOSE_WAIT
1080 static void xs_tcp_force_close(struct rpc_xprt
*xprt
)
1082 set_bit(XPRT_CONNECTION_CLOSE
, &xprt
->state
);
1083 xprt_force_disconnect(xprt
);
1086 static inline void xs_tcp_read_fraghdr(struct rpc_xprt
*xprt
, struct xdr_skb_reader
*desc
)
1088 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1092 p
= ((char *) &transport
->tcp_fraghdr
) + transport
->tcp_offset
;
1093 len
= sizeof(transport
->tcp_fraghdr
) - transport
->tcp_offset
;
1094 used
= xdr_skb_read_bits(desc
, p
, len
);
1095 transport
->tcp_offset
+= used
;
1099 transport
->tcp_reclen
= ntohl(transport
->tcp_fraghdr
);
1100 if (transport
->tcp_reclen
& RPC_LAST_STREAM_FRAGMENT
)
1101 transport
->tcp_flags
|= TCP_RCV_LAST_FRAG
;
1103 transport
->tcp_flags
&= ~TCP_RCV_LAST_FRAG
;
1104 transport
->tcp_reclen
&= RPC_FRAGMENT_SIZE_MASK
;
1106 transport
->tcp_flags
&= ~TCP_RCV_COPY_FRAGHDR
;
1107 transport
->tcp_offset
= 0;
1109 /* Sanity check of the record length */
1110 if (unlikely(transport
->tcp_reclen
< 8)) {
1111 dprintk("RPC: invalid TCP record fragment length\n");
1112 xs_tcp_force_close(xprt
);
1115 dprintk("RPC: reading TCP record fragment of length %d\n",
1116 transport
->tcp_reclen
);
1119 static void xs_tcp_check_fraghdr(struct sock_xprt
*transport
)
1121 if (transport
->tcp_offset
== transport
->tcp_reclen
) {
1122 transport
->tcp_flags
|= TCP_RCV_COPY_FRAGHDR
;
1123 transport
->tcp_offset
= 0;
1124 if (transport
->tcp_flags
& TCP_RCV_LAST_FRAG
) {
1125 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1126 transport
->tcp_flags
|= TCP_RCV_COPY_XID
;
1127 transport
->tcp_copied
= 0;
1132 static inline void xs_tcp_read_xid(struct sock_xprt
*transport
, struct xdr_skb_reader
*desc
)
1137 len
= sizeof(transport
->tcp_xid
) - transport
->tcp_offset
;
1138 dprintk("RPC: reading XID (%Zu bytes)\n", len
);
1139 p
= ((char *) &transport
->tcp_xid
) + transport
->tcp_offset
;
1140 used
= xdr_skb_read_bits(desc
, p
, len
);
1141 transport
->tcp_offset
+= used
;
1144 transport
->tcp_flags
&= ~TCP_RCV_COPY_XID
;
1145 transport
->tcp_flags
|= TCP_RCV_READ_CALLDIR
;
1146 transport
->tcp_copied
= 4;
1147 dprintk("RPC: reading %s XID %08x\n",
1148 (transport
->tcp_flags
& TCP_RPC_REPLY
) ? "reply for"
1150 ntohl(transport
->tcp_xid
));
1151 xs_tcp_check_fraghdr(transport
);
1154 static inline void xs_tcp_read_calldir(struct sock_xprt
*transport
,
1155 struct xdr_skb_reader
*desc
)
1162 * We want transport->tcp_offset to be 8 at the end of this routine
1163 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1164 * When this function is called for the first time,
1165 * transport->tcp_offset is 4 (after having already read the xid).
1167 offset
= transport
->tcp_offset
- sizeof(transport
->tcp_xid
);
1168 len
= sizeof(transport
->tcp_calldir
) - offset
;
1169 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len
);
1170 p
= ((char *) &transport
->tcp_calldir
) + offset
;
1171 used
= xdr_skb_read_bits(desc
, p
, len
);
1172 transport
->tcp_offset
+= used
;
1175 transport
->tcp_flags
&= ~TCP_RCV_READ_CALLDIR
;
1177 * We don't yet have the XDR buffer, so we will write the calldir
1178 * out after we get the buffer from the 'struct rpc_rqst'
1180 switch (ntohl(transport
->tcp_calldir
)) {
1182 transport
->tcp_flags
|= TCP_RCV_COPY_CALLDIR
;
1183 transport
->tcp_flags
|= TCP_RCV_COPY_DATA
;
1184 transport
->tcp_flags
|= TCP_RPC_REPLY
;
1187 transport
->tcp_flags
|= TCP_RCV_COPY_CALLDIR
;
1188 transport
->tcp_flags
|= TCP_RCV_COPY_DATA
;
1189 transport
->tcp_flags
&= ~TCP_RPC_REPLY
;
1192 dprintk("RPC: invalid request message type\n");
1193 xs_tcp_force_close(&transport
->xprt
);
1195 xs_tcp_check_fraghdr(transport
);
1198 static inline void xs_tcp_read_common(struct rpc_xprt
*xprt
,
1199 struct xdr_skb_reader
*desc
,
1200 struct rpc_rqst
*req
)
1202 struct sock_xprt
*transport
=
1203 container_of(xprt
, struct sock_xprt
, xprt
);
1204 struct xdr_buf
*rcvbuf
;
1208 rcvbuf
= &req
->rq_private_buf
;
1210 if (transport
->tcp_flags
& TCP_RCV_COPY_CALLDIR
) {
1212 * Save the RPC direction in the XDR buffer
1214 memcpy(rcvbuf
->head
[0].iov_base
+ transport
->tcp_copied
,
1215 &transport
->tcp_calldir
,
1216 sizeof(transport
->tcp_calldir
));
1217 transport
->tcp_copied
+= sizeof(transport
->tcp_calldir
);
1218 transport
->tcp_flags
&= ~TCP_RCV_COPY_CALLDIR
;
1222 if (len
> transport
->tcp_reclen
- transport
->tcp_offset
) {
1223 struct xdr_skb_reader my_desc
;
1225 len
= transport
->tcp_reclen
- transport
->tcp_offset
;
1226 memcpy(&my_desc
, desc
, sizeof(my_desc
));
1227 my_desc
.count
= len
;
1228 r
= xdr_partial_copy_from_skb(rcvbuf
, transport
->tcp_copied
,
1229 &my_desc
, xdr_skb_read_bits
);
1233 r
= xdr_partial_copy_from_skb(rcvbuf
, transport
->tcp_copied
,
1234 desc
, xdr_skb_read_bits
);
1237 transport
->tcp_copied
+= r
;
1238 transport
->tcp_offset
+= r
;
1241 /* Error when copying to the receive buffer,
1242 * usually because we weren't able to allocate
1243 * additional buffer pages. All we can do now
1244 * is turn off TCP_RCV_COPY_DATA, so the request
1245 * will not receive any additional updates,
1247 * Any remaining data from this record will
1250 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1251 dprintk("RPC: XID %08x truncated request\n",
1252 ntohl(transport
->tcp_xid
));
1253 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1254 "tcp_offset = %u, tcp_reclen = %u\n",
1255 xprt
, transport
->tcp_copied
,
1256 transport
->tcp_offset
, transport
->tcp_reclen
);
1260 dprintk("RPC: XID %08x read %Zd bytes\n",
1261 ntohl(transport
->tcp_xid
), r
);
1262 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1263 "tcp_reclen = %u\n", xprt
, transport
->tcp_copied
,
1264 transport
->tcp_offset
, transport
->tcp_reclen
);
1266 if (transport
->tcp_copied
== req
->rq_private_buf
.buflen
)
1267 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1268 else if (transport
->tcp_offset
== transport
->tcp_reclen
) {
1269 if (transport
->tcp_flags
& TCP_RCV_LAST_FRAG
)
1270 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1275 * Finds the request corresponding to the RPC xid and invokes the common
1276 * tcp read code to read the data.
1278 static inline int xs_tcp_read_reply(struct rpc_xprt
*xprt
,
1279 struct xdr_skb_reader
*desc
)
1281 struct sock_xprt
*transport
=
1282 container_of(xprt
, struct sock_xprt
, xprt
);
1283 struct rpc_rqst
*req
;
1285 dprintk("RPC: read reply XID %08x\n", ntohl(transport
->tcp_xid
));
1287 /* Find and lock the request corresponding to this xid */
1288 spin_lock(&xprt
->transport_lock
);
1289 req
= xprt_lookup_rqst(xprt
, transport
->tcp_xid
);
1291 dprintk("RPC: XID %08x request not found!\n",
1292 ntohl(transport
->tcp_xid
));
1293 spin_unlock(&xprt
->transport_lock
);
1297 xs_tcp_read_common(xprt
, desc
, req
);
1299 if (!(transport
->tcp_flags
& TCP_RCV_COPY_DATA
))
1300 xprt_complete_rqst(req
->rq_task
, transport
->tcp_copied
);
1302 spin_unlock(&xprt
->transport_lock
);
1306 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1308 * Obtains an rpc_rqst previously allocated and invokes the common
1309 * tcp read code to read the data. The result is placed in the callback
1311 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1312 * connection and return -1.
1314 static int xs_tcp_read_callback(struct rpc_xprt
*xprt
,
1315 struct xdr_skb_reader
*desc
)
1317 struct sock_xprt
*transport
=
1318 container_of(xprt
, struct sock_xprt
, xprt
);
1319 struct rpc_rqst
*req
;
1321 /* Look up and lock the request corresponding to the given XID */
1322 spin_lock(&xprt
->transport_lock
);
1323 req
= xprt_lookup_bc_request(xprt
, transport
->tcp_xid
);
1325 spin_unlock(&xprt
->transport_lock
);
1326 printk(KERN_WARNING
"Callback slot table overflowed\n");
1327 xprt_force_disconnect(xprt
);
1331 dprintk("RPC: read callback XID %08x\n", ntohl(req
->rq_xid
));
1332 xs_tcp_read_common(xprt
, desc
, req
);
1334 if (!(transport
->tcp_flags
& TCP_RCV_COPY_DATA
))
1335 xprt_complete_bc_request(req
, transport
->tcp_copied
);
1336 spin_unlock(&xprt
->transport_lock
);
1341 static inline int _xs_tcp_read_data(struct rpc_xprt
*xprt
,
1342 struct xdr_skb_reader
*desc
)
1344 struct sock_xprt
*transport
=
1345 container_of(xprt
, struct sock_xprt
, xprt
);
1347 return (transport
->tcp_flags
& TCP_RPC_REPLY
) ?
1348 xs_tcp_read_reply(xprt
, desc
) :
1349 xs_tcp_read_callback(xprt
, desc
);
1352 static inline int _xs_tcp_read_data(struct rpc_xprt
*xprt
,
1353 struct xdr_skb_reader
*desc
)
1355 return xs_tcp_read_reply(xprt
, desc
);
1357 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1360 * Read data off the transport. This can be either an RPC_CALL or an
1361 * RPC_REPLY. Relay the processing to helper functions.
1363 static void xs_tcp_read_data(struct rpc_xprt
*xprt
,
1364 struct xdr_skb_reader
*desc
)
1366 struct sock_xprt
*transport
=
1367 container_of(xprt
, struct sock_xprt
, xprt
);
1369 if (_xs_tcp_read_data(xprt
, desc
) == 0)
1370 xs_tcp_check_fraghdr(transport
);
1373 * The transport_lock protects the request handling.
1374 * There's no need to hold it to update the tcp_flags.
1376 transport
->tcp_flags
&= ~TCP_RCV_COPY_DATA
;
1380 static inline void xs_tcp_read_discard(struct sock_xprt
*transport
, struct xdr_skb_reader
*desc
)
1384 len
= transport
->tcp_reclen
- transport
->tcp_offset
;
1385 if (len
> desc
->count
)
1388 desc
->offset
+= len
;
1389 transport
->tcp_offset
+= len
;
1390 dprintk("RPC: discarded %Zu bytes\n", len
);
1391 xs_tcp_check_fraghdr(transport
);
1394 static int xs_tcp_data_recv(read_descriptor_t
*rd_desc
, struct sk_buff
*skb
, unsigned int offset
, size_t len
)
1396 struct rpc_xprt
*xprt
= rd_desc
->arg
.data
;
1397 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1398 struct xdr_skb_reader desc
= {
1404 dprintk("RPC: xs_tcp_data_recv started\n");
1406 /* Read in a new fragment marker if necessary */
1407 /* Can we ever really expect to get completely empty fragments? */
1408 if (transport
->tcp_flags
& TCP_RCV_COPY_FRAGHDR
) {
1409 xs_tcp_read_fraghdr(xprt
, &desc
);
1412 /* Read in the xid if necessary */
1413 if (transport
->tcp_flags
& TCP_RCV_COPY_XID
) {
1414 xs_tcp_read_xid(transport
, &desc
);
1417 /* Read in the call/reply flag */
1418 if (transport
->tcp_flags
& TCP_RCV_READ_CALLDIR
) {
1419 xs_tcp_read_calldir(transport
, &desc
);
1422 /* Read in the request data */
1423 if (transport
->tcp_flags
& TCP_RCV_COPY_DATA
) {
1424 xs_tcp_read_data(xprt
, &desc
);
1427 /* Skip over any trailing bytes on short reads */
1428 xs_tcp_read_discard(transport
, &desc
);
1429 } while (desc
.count
);
1430 dprintk("RPC: xs_tcp_data_recv done\n");
1431 return len
- desc
.count
;
1435 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1436 * @sk: socket with data to read
1437 * @bytes: how much data to read
1440 static void xs_tcp_data_ready(struct sock
*sk
)
1442 struct rpc_xprt
*xprt
;
1443 read_descriptor_t rd_desc
;
1446 dprintk("RPC: xs_tcp_data_ready...\n");
1448 read_lock_bh(&sk
->sk_callback_lock
);
1449 if (!(xprt
= xprt_from_sock(sk
)))
1451 /* Any data means we had a useful conversation, so
1452 * the we don't need to delay the next reconnect
1454 if (xprt
->reestablish_timeout
)
1455 xprt
->reestablish_timeout
= 0;
1457 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1458 rd_desc
.arg
.data
= xprt
;
1460 rd_desc
.count
= 65536;
1461 read
= tcp_read_sock(sk
, &rd_desc
, xs_tcp_data_recv
);
1464 read_unlock_bh(&sk
->sk_callback_lock
);
1468 * Do the equivalent of linger/linger2 handling for dealing with
1469 * broken servers that don't close the socket in a timely
1472 static void xs_tcp_schedule_linger_timeout(struct rpc_xprt
*xprt
,
1473 unsigned long timeout
)
1475 struct sock_xprt
*transport
;
1477 if (xprt_test_and_set_connecting(xprt
))
1479 set_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1480 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1481 queue_delayed_work(rpciod_workqueue
, &transport
->connect_worker
,
1485 static void xs_tcp_cancel_linger_timeout(struct rpc_xprt
*xprt
)
1487 struct sock_xprt
*transport
;
1489 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1491 if (!test_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
) ||
1492 !cancel_delayed_work(&transport
->connect_worker
))
1494 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1495 xprt_clear_connecting(xprt
);
1498 static void xs_sock_reset_connection_flags(struct rpc_xprt
*xprt
)
1500 smp_mb__before_atomic();
1501 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1502 clear_bit(XPRT_CONNECTION_CLOSE
, &xprt
->state
);
1503 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
1504 clear_bit(XPRT_CLOSING
, &xprt
->state
);
1505 smp_mb__after_atomic();
1508 static void xs_sock_mark_closed(struct rpc_xprt
*xprt
)
1510 xs_sock_reset_connection_flags(xprt
);
1511 /* Mark transport as closed and wake up all pending tasks */
1512 xprt_disconnect_done(xprt
);
1516 * xs_tcp_state_change - callback to handle TCP socket state changes
1517 * @sk: socket whose state has changed
1520 static void xs_tcp_state_change(struct sock
*sk
)
1522 struct rpc_xprt
*xprt
;
1524 read_lock_bh(&sk
->sk_callback_lock
);
1525 if (!(xprt
= xprt_from_sock(sk
)))
1527 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt
);
1528 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1529 sk
->sk_state
, xprt_connected(xprt
),
1530 sock_flag(sk
, SOCK_DEAD
),
1531 sock_flag(sk
, SOCK_ZAPPED
),
1534 trace_rpc_socket_state_change(xprt
, sk
->sk_socket
);
1535 switch (sk
->sk_state
) {
1536 case TCP_ESTABLISHED
:
1537 spin_lock(&xprt
->transport_lock
);
1538 if (!xprt_test_and_set_connected(xprt
)) {
1539 struct sock_xprt
*transport
= container_of(xprt
,
1540 struct sock_xprt
, xprt
);
1542 /* Reset TCP record info */
1543 transport
->tcp_offset
= 0;
1544 transport
->tcp_reclen
= 0;
1545 transport
->tcp_copied
= 0;
1546 transport
->tcp_flags
=
1547 TCP_RCV_COPY_FRAGHDR
| TCP_RCV_COPY_XID
;
1548 xprt
->connect_cookie
++;
1550 xprt_wake_pending_tasks(xprt
, -EAGAIN
);
1552 spin_unlock(&xprt
->transport_lock
);
1555 /* The client initiated a shutdown of the socket */
1556 xprt
->connect_cookie
++;
1557 xprt
->reestablish_timeout
= 0;
1558 set_bit(XPRT_CLOSING
, &xprt
->state
);
1559 smp_mb__before_atomic();
1560 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1561 clear_bit(XPRT_CLOSE_WAIT
, &xprt
->state
);
1562 smp_mb__after_atomic();
1563 xs_tcp_schedule_linger_timeout(xprt
, xs_tcp_fin_timeout
);
1565 case TCP_CLOSE_WAIT
:
1566 /* The server initiated a shutdown of the socket */
1567 xprt
->connect_cookie
++;
1568 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1569 xs_tcp_force_close(xprt
);
1572 * If the server closed down the connection, make sure that
1573 * we back off before reconnecting
1575 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
1576 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
1579 set_bit(XPRT_CLOSING
, &xprt
->state
);
1580 xs_tcp_schedule_linger_timeout(xprt
, xs_tcp_fin_timeout
);
1581 smp_mb__before_atomic();
1582 clear_bit(XPRT_CONNECTED
, &xprt
->state
);
1583 smp_mb__after_atomic();
1586 xs_tcp_cancel_linger_timeout(xprt
);
1587 xs_sock_mark_closed(xprt
);
1590 read_unlock_bh(&sk
->sk_callback_lock
);
1593 static void xs_write_space(struct sock
*sk
)
1595 struct socket
*sock
;
1596 struct rpc_xprt
*xprt
;
1598 if (unlikely(!(sock
= sk
->sk_socket
)))
1600 clear_bit(SOCK_NOSPACE
, &sock
->flags
);
1602 if (unlikely(!(xprt
= xprt_from_sock(sk
))))
1604 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
) == 0)
1607 xprt_write_space(xprt
);
1611 * xs_udp_write_space - callback invoked when socket buffer space
1613 * @sk: socket whose state has changed
1615 * Called when more output buffer space is available for this socket.
1616 * We try not to wake our writers until they can make "significant"
1617 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1618 * with a bunch of small requests.
1620 static void xs_udp_write_space(struct sock
*sk
)
1622 read_lock_bh(&sk
->sk_callback_lock
);
1624 /* from net/core/sock.c:sock_def_write_space */
1625 if (sock_writeable(sk
))
1628 read_unlock_bh(&sk
->sk_callback_lock
);
1632 * xs_tcp_write_space - callback invoked when socket buffer space
1634 * @sk: socket whose state has changed
1636 * Called when more output buffer space is available for this socket.
1637 * We try not to wake our writers until they can make "significant"
1638 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1639 * with a bunch of small requests.
1641 static void xs_tcp_write_space(struct sock
*sk
)
1643 read_lock_bh(&sk
->sk_callback_lock
);
1645 /* from net/core/stream.c:sk_stream_write_space */
1646 if (sk_stream_is_writeable(sk
))
1649 read_unlock_bh(&sk
->sk_callback_lock
);
1652 static void xs_udp_do_set_buffer_size(struct rpc_xprt
*xprt
)
1654 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1655 struct sock
*sk
= transport
->inet
;
1657 if (transport
->rcvsize
) {
1658 sk
->sk_userlocks
|= SOCK_RCVBUF_LOCK
;
1659 sk
->sk_rcvbuf
= transport
->rcvsize
* xprt
->max_reqs
* 2;
1661 if (transport
->sndsize
) {
1662 sk
->sk_userlocks
|= SOCK_SNDBUF_LOCK
;
1663 sk
->sk_sndbuf
= transport
->sndsize
* xprt
->max_reqs
* 2;
1664 sk
->sk_write_space(sk
);
1669 * xs_udp_set_buffer_size - set send and receive limits
1670 * @xprt: generic transport
1671 * @sndsize: requested size of send buffer, in bytes
1672 * @rcvsize: requested size of receive buffer, in bytes
1674 * Set socket send and receive buffer size limits.
1676 static void xs_udp_set_buffer_size(struct rpc_xprt
*xprt
, size_t sndsize
, size_t rcvsize
)
1678 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1680 transport
->sndsize
= 0;
1682 transport
->sndsize
= sndsize
+ 1024;
1683 transport
->rcvsize
= 0;
1685 transport
->rcvsize
= rcvsize
+ 1024;
1687 xs_udp_do_set_buffer_size(xprt
);
1691 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1692 * @task: task that timed out
1694 * Adjust the congestion window after a retransmit timeout has occurred.
1696 static void xs_udp_timer(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
1698 xprt_adjust_cwnd(xprt
, task
, -ETIMEDOUT
);
1701 static unsigned short xs_get_random_port(void)
1703 unsigned short range
= xprt_max_resvport
- xprt_min_resvport
;
1704 unsigned short rand
= (unsigned short) prandom_u32() % range
;
1705 return rand
+ xprt_min_resvport
;
1709 * xs_set_port - reset the port number in the remote endpoint address
1710 * @xprt: generic transport
1711 * @port: new port number
1714 static void xs_set_port(struct rpc_xprt
*xprt
, unsigned short port
)
1716 dprintk("RPC: setting port for xprt %p to %u\n", xprt
, port
);
1718 rpc_set_port(xs_addr(xprt
), port
);
1719 xs_update_peer_port(xprt
);
1722 static unsigned short xs_get_srcport(struct sock_xprt
*transport
)
1724 unsigned short port
= transport
->srcport
;
1726 if (port
== 0 && transport
->xprt
.resvport
)
1727 port
= xs_get_random_port();
1731 static unsigned short xs_next_srcport(struct sock_xprt
*transport
, unsigned short port
)
1733 if (transport
->srcport
!= 0)
1734 transport
->srcport
= 0;
1735 if (!transport
->xprt
.resvport
)
1737 if (port
<= xprt_min_resvport
|| port
> xprt_max_resvport
)
1738 return xprt_max_resvport
;
1741 static int xs_bind(struct sock_xprt
*transport
, struct socket
*sock
)
1743 struct sockaddr_storage myaddr
;
1745 unsigned short port
= xs_get_srcport(transport
);
1746 unsigned short last
;
1748 memcpy(&myaddr
, &transport
->srcaddr
, transport
->xprt
.addrlen
);
1750 rpc_set_port((struct sockaddr
*)&myaddr
, port
);
1751 err
= kernel_bind(sock
, (struct sockaddr
*)&myaddr
,
1752 transport
->xprt
.addrlen
);
1756 transport
->srcport
= port
;
1760 port
= xs_next_srcport(transport
, port
);
1763 } while (err
== -EADDRINUSE
&& nloop
!= 2);
1765 if (myaddr
.ss_family
== AF_INET
)
1766 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__
,
1767 &((struct sockaddr_in
*)&myaddr
)->sin_addr
,
1768 port
, err
? "failed" : "ok", err
);
1770 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__
,
1771 &((struct sockaddr_in6
*)&myaddr
)->sin6_addr
,
1772 port
, err
? "failed" : "ok", err
);
1777 * We don't support autobind on AF_LOCAL sockets
1779 static void xs_local_rpcbind(struct rpc_task
*task
)
1782 xprt_set_bound(rcu_dereference(task
->tk_client
->cl_xprt
));
1786 static void xs_local_set_port(struct rpc_xprt
*xprt
, unsigned short port
)
1790 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1791 static struct lock_class_key xs_key
[2];
1792 static struct lock_class_key xs_slock_key
[2];
1794 static inline void xs_reclassify_socketu(struct socket
*sock
)
1796 struct sock
*sk
= sock
->sk
;
1798 sock_lock_init_class_and_name(sk
, "slock-AF_LOCAL-RPC",
1799 &xs_slock_key
[1], "sk_lock-AF_LOCAL-RPC", &xs_key
[1]);
1802 static inline void xs_reclassify_socket4(struct socket
*sock
)
1804 struct sock
*sk
= sock
->sk
;
1806 sock_lock_init_class_and_name(sk
, "slock-AF_INET-RPC",
1807 &xs_slock_key
[0], "sk_lock-AF_INET-RPC", &xs_key
[0]);
1810 static inline void xs_reclassify_socket6(struct socket
*sock
)
1812 struct sock
*sk
= sock
->sk
;
1814 sock_lock_init_class_and_name(sk
, "slock-AF_INET6-RPC",
1815 &xs_slock_key
[1], "sk_lock-AF_INET6-RPC", &xs_key
[1]);
1818 static inline void xs_reclassify_socket(int family
, struct socket
*sock
)
1820 WARN_ON_ONCE(sock_owned_by_user(sock
->sk
));
1821 if (sock_owned_by_user(sock
->sk
))
1826 xs_reclassify_socketu(sock
);
1829 xs_reclassify_socket4(sock
);
1832 xs_reclassify_socket6(sock
);
1837 static inline void xs_reclassify_socketu(struct socket
*sock
)
1841 static inline void xs_reclassify_socket4(struct socket
*sock
)
1845 static inline void xs_reclassify_socket6(struct socket
*sock
)
1849 static inline void xs_reclassify_socket(int family
, struct socket
*sock
)
1854 static void xs_dummy_setup_socket(struct work_struct
*work
)
1858 static struct socket
*xs_create_sock(struct rpc_xprt
*xprt
,
1859 struct sock_xprt
*transport
, int family
, int type
, int protocol
)
1861 struct socket
*sock
;
1864 err
= __sock_create(xprt
->xprt_net
, family
, type
, protocol
, &sock
, 1);
1866 dprintk("RPC: can't create %d transport socket (%d).\n",
1870 xs_reclassify_socket(family
, sock
);
1872 err
= xs_bind(transport
, sock
);
1880 return ERR_PTR(err
);
1883 static int xs_local_finish_connecting(struct rpc_xprt
*xprt
,
1884 struct socket
*sock
)
1886 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
,
1889 if (!transport
->inet
) {
1890 struct sock
*sk
= sock
->sk
;
1892 write_lock_bh(&sk
->sk_callback_lock
);
1894 xs_save_old_callbacks(transport
, sk
);
1896 sk
->sk_user_data
= xprt
;
1897 sk
->sk_data_ready
= xs_local_data_ready
;
1898 sk
->sk_write_space
= xs_udp_write_space
;
1899 sk
->sk_error_report
= xs_error_report
;
1900 sk
->sk_allocation
= GFP_ATOMIC
;
1902 xprt_clear_connected(xprt
);
1904 /* Reset to new socket */
1905 transport
->sock
= sock
;
1906 transport
->inet
= sk
;
1908 write_unlock_bh(&sk
->sk_callback_lock
);
1911 /* Tell the socket layer to start connecting... */
1912 xprt
->stat
.connect_count
++;
1913 xprt
->stat
.connect_start
= jiffies
;
1914 return kernel_connect(sock
, xs_addr(xprt
), xprt
->addrlen
, 0);
1918 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1919 * @xprt: RPC transport to connect
1920 * @transport: socket transport to connect
1921 * @create_sock: function to create a socket of the correct type
1923 static int xs_local_setup_socket(struct sock_xprt
*transport
)
1925 struct rpc_xprt
*xprt
= &transport
->xprt
;
1926 struct socket
*sock
;
1929 current
->flags
|= PF_FSTRANS
;
1931 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
1932 status
= __sock_create(xprt
->xprt_net
, AF_LOCAL
,
1933 SOCK_STREAM
, 0, &sock
, 1);
1935 dprintk("RPC: can't create AF_LOCAL "
1936 "transport socket (%d).\n", -status
);
1939 xs_reclassify_socketu(sock
);
1941 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1942 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
1944 status
= xs_local_finish_connecting(xprt
, sock
);
1945 trace_rpc_socket_connect(xprt
, sock
, status
);
1948 dprintk("RPC: xprt %p connected to %s\n",
1949 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
1950 xprt_set_connected(xprt
);
1953 dprintk("RPC: xprt %p: socket %s does not exist\n",
1954 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
1957 dprintk("RPC: xprt %p: connection refused for %s\n",
1958 xprt
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
1961 printk(KERN_ERR
"%s: unhandled error (%d) connecting to %s\n",
1963 xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
1967 xprt_clear_connecting(xprt
);
1968 xprt_wake_pending_tasks(xprt
, status
);
1969 current
->flags
&= ~PF_FSTRANS
;
1973 static void xs_local_connect(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
1975 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
1978 if (RPC_IS_ASYNC(task
)) {
1980 * We want the AF_LOCAL connect to be resolved in the
1981 * filesystem namespace of the process making the rpc
1982 * call. Thus we connect synchronously.
1984 * If we want to support asynchronous AF_LOCAL calls,
1985 * we'll need to figure out how to pass a namespace to
1988 rpc_exit(task
, -ENOTCONN
);
1991 ret
= xs_local_setup_socket(transport
);
1992 if (ret
&& !RPC_IS_SOFTCONN(task
))
1993 msleep_interruptible(15000);
1996 #ifdef CONFIG_SUNRPC_SWAP
1997 static void xs_set_memalloc(struct rpc_xprt
*xprt
)
1999 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
,
2003 sk_set_memalloc(transport
->inet
);
2007 * xs_swapper - Tag this transport as being used for swap.
2008 * @xprt: transport to tag
2009 * @enable: enable/disable
2012 int xs_swapper(struct rpc_xprt
*xprt
, int enable
)
2014 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
,
2020 xs_set_memalloc(xprt
);
2021 } else if (xprt
->swapper
) {
2023 sk_clear_memalloc(transport
->inet
);
2028 EXPORT_SYMBOL_GPL(xs_swapper
);
2030 static void xs_set_memalloc(struct rpc_xprt
*xprt
)
2035 static void xs_udp_finish_connecting(struct rpc_xprt
*xprt
, struct socket
*sock
)
2037 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2039 if (!transport
->inet
) {
2040 struct sock
*sk
= sock
->sk
;
2042 write_lock_bh(&sk
->sk_callback_lock
);
2044 xs_save_old_callbacks(transport
, sk
);
2046 sk
->sk_user_data
= xprt
;
2047 sk
->sk_data_ready
= xs_udp_data_ready
;
2048 sk
->sk_write_space
= xs_udp_write_space
;
2049 sk
->sk_allocation
= GFP_ATOMIC
;
2051 xprt_set_connected(xprt
);
2053 /* Reset to new socket */
2054 transport
->sock
= sock
;
2055 transport
->inet
= sk
;
2057 xs_set_memalloc(xprt
);
2059 write_unlock_bh(&sk
->sk_callback_lock
);
2061 xs_udp_do_set_buffer_size(xprt
);
2064 static void xs_udp_setup_socket(struct work_struct
*work
)
2066 struct sock_xprt
*transport
=
2067 container_of(work
, struct sock_xprt
, connect_worker
.work
);
2068 struct rpc_xprt
*xprt
= &transport
->xprt
;
2069 struct socket
*sock
= transport
->sock
;
2072 current
->flags
|= PF_FSTRANS
;
2074 /* Start by resetting any existing state */
2075 xs_reset_transport(transport
);
2076 sock
= xs_create_sock(xprt
, transport
,
2077 xs_addr(xprt
)->sa_family
, SOCK_DGRAM
, IPPROTO_UDP
);
2081 dprintk("RPC: worker connecting xprt %p via %s to "
2082 "%s (port %s)\n", xprt
,
2083 xprt
->address_strings
[RPC_DISPLAY_PROTO
],
2084 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2085 xprt
->address_strings
[RPC_DISPLAY_PORT
]);
2087 xs_udp_finish_connecting(xprt
, sock
);
2088 trace_rpc_socket_connect(xprt
, sock
, 0);
2091 xprt_clear_connecting(xprt
);
2092 xprt_wake_pending_tasks(xprt
, status
);
2093 current
->flags
&= ~PF_FSTRANS
;
2097 * We need to preserve the port number so the reply cache on the server can
2098 * find our cached RPC replies when we get around to reconnecting.
2100 static void xs_abort_connection(struct sock_xprt
*transport
)
2103 struct sockaddr any
;
2105 dprintk("RPC: disconnecting xprt %p to reuse port\n", transport
);
2108 * Disconnect the transport socket by doing a connect operation
2109 * with AF_UNSPEC. This should return immediately...
2111 memset(&any
, 0, sizeof(any
));
2112 any
.sa_family
= AF_UNSPEC
;
2113 result
= kernel_connect(transport
->sock
, &any
, sizeof(any
), 0);
2114 trace_rpc_socket_reset_connection(&transport
->xprt
,
2115 transport
->sock
, result
);
2117 xs_sock_reset_connection_flags(&transport
->xprt
);
2118 dprintk("RPC: AF_UNSPEC connect return code %d\n", result
);
2121 static void xs_tcp_reuse_connection(struct sock_xprt
*transport
)
2123 unsigned int state
= transport
->inet
->sk_state
;
2125 if (state
== TCP_CLOSE
&& transport
->sock
->state
== SS_UNCONNECTED
) {
2126 /* we don't need to abort the connection if the socket
2127 * hasn't undergone a shutdown
2129 if (transport
->inet
->sk_shutdown
== 0)
2131 dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n",
2132 __func__
, transport
->inet
->sk_shutdown
);
2134 if ((1 << state
) & (TCPF_ESTABLISHED
|TCPF_SYN_SENT
)) {
2135 /* we don't need to abort the connection if the socket
2136 * hasn't undergone a shutdown
2138 if (transport
->inet
->sk_shutdown
== 0)
2140 dprintk("RPC: %s: ESTABLISHED/SYN_SENT "
2141 "sk_shutdown set to %d\n",
2142 __func__
, transport
->inet
->sk_shutdown
);
2144 xs_abort_connection(transport
);
2147 static int xs_tcp_finish_connecting(struct rpc_xprt
*xprt
, struct socket
*sock
)
2149 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2150 int ret
= -ENOTCONN
;
2152 if (!transport
->inet
) {
2153 struct sock
*sk
= sock
->sk
;
2154 unsigned int keepidle
= xprt
->timeout
->to_initval
/ HZ
;
2155 unsigned int keepcnt
= xprt
->timeout
->to_retries
+ 1;
2156 unsigned int opt_on
= 1;
2158 /* TCP Keepalive options */
2159 kernel_setsockopt(sock
, SOL_SOCKET
, SO_KEEPALIVE
,
2160 (char *)&opt_on
, sizeof(opt_on
));
2161 kernel_setsockopt(sock
, SOL_TCP
, TCP_KEEPIDLE
,
2162 (char *)&keepidle
, sizeof(keepidle
));
2163 kernel_setsockopt(sock
, SOL_TCP
, TCP_KEEPINTVL
,
2164 (char *)&keepidle
, sizeof(keepidle
));
2165 kernel_setsockopt(sock
, SOL_TCP
, TCP_KEEPCNT
,
2166 (char *)&keepcnt
, sizeof(keepcnt
));
2168 write_lock_bh(&sk
->sk_callback_lock
);
2170 xs_save_old_callbacks(transport
, sk
);
2172 sk
->sk_user_data
= xprt
;
2173 sk
->sk_data_ready
= xs_tcp_data_ready
;
2174 sk
->sk_state_change
= xs_tcp_state_change
;
2175 sk
->sk_write_space
= xs_tcp_write_space
;
2176 sk
->sk_error_report
= xs_error_report
;
2177 sk
->sk_allocation
= GFP_ATOMIC
;
2179 /* socket options */
2180 sk
->sk_userlocks
|= SOCK_BINDPORT_LOCK
;
2181 sock_reset_flag(sk
, SOCK_LINGER
);
2182 tcp_sk(sk
)->linger2
= 0;
2183 tcp_sk(sk
)->nonagle
|= TCP_NAGLE_OFF
;
2185 xprt_clear_connected(xprt
);
2187 /* Reset to new socket */
2188 transport
->sock
= sock
;
2189 transport
->inet
= sk
;
2191 write_unlock_bh(&sk
->sk_callback_lock
);
2194 if (!xprt_bound(xprt
))
2197 xs_set_memalloc(xprt
);
2199 /* Tell the socket layer to start connecting... */
2200 xprt
->stat
.connect_count
++;
2201 xprt
->stat
.connect_start
= jiffies
;
2202 ret
= kernel_connect(sock
, xs_addr(xprt
), xprt
->addrlen
, O_NONBLOCK
);
2207 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
2208 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2215 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2216 * @xprt: RPC transport to connect
2217 * @transport: socket transport to connect
2218 * @create_sock: function to create a socket of the correct type
2220 * Invoked by a work queue tasklet.
2222 static void xs_tcp_setup_socket(struct work_struct
*work
)
2224 struct sock_xprt
*transport
=
2225 container_of(work
, struct sock_xprt
, connect_worker
.work
);
2226 struct socket
*sock
= transport
->sock
;
2227 struct rpc_xprt
*xprt
= &transport
->xprt
;
2230 current
->flags
|= PF_FSTRANS
;
2233 clear_bit(XPRT_CONNECTION_ABORT
, &xprt
->state
);
2234 sock
= xs_create_sock(xprt
, transport
,
2235 xs_addr(xprt
)->sa_family
, SOCK_STREAM
, IPPROTO_TCP
);
2237 status
= PTR_ERR(sock
);
2243 abort_and_exit
= test_and_clear_bit(XPRT_CONNECTION_ABORT
,
2245 /* "close" the socket, preserving the local port */
2246 set_bit(XPRT_CONNECTION_REUSE
, &xprt
->state
);
2247 xs_tcp_reuse_connection(transport
);
2248 clear_bit(XPRT_CONNECTION_REUSE
, &xprt
->state
);
2254 dprintk("RPC: worker connecting xprt %p via %s to "
2255 "%s (port %s)\n", xprt
,
2256 xprt
->address_strings
[RPC_DISPLAY_PROTO
],
2257 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2258 xprt
->address_strings
[RPC_DISPLAY_PORT
]);
2260 status
= xs_tcp_finish_connecting(xprt
, sock
);
2261 trace_rpc_socket_connect(xprt
, sock
, status
);
2262 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2263 xprt
, -status
, xprt_connected(xprt
),
2264 sock
->sk
->sk_state
);
2267 printk("%s: connect returned unhandled error %d\n",
2269 case -EADDRNOTAVAIL
:
2270 /* We're probably in TIME_WAIT. Get rid of existing socket,
2273 xs_tcp_force_close(xprt
);
2278 xprt_clear_connecting(xprt
);
2279 current
->flags
&= ~PF_FSTRANS
;
2282 /* Happens, for instance, if the user specified a link
2283 * local IPv6 address without a scope-id.
2288 /* retry with existing socket, after a delay */
2294 xprt_clear_connecting(xprt
);
2295 xprt_wake_pending_tasks(xprt
, status
);
2296 current
->flags
&= ~PF_FSTRANS
;
2300 * xs_connect - connect a socket to a remote endpoint
2301 * @xprt: pointer to transport structure
2302 * @task: address of RPC task that manages state of connect request
2304 * TCP: If the remote end dropped the connection, delay reconnecting.
2306 * UDP socket connects are synchronous, but we use a work queue anyway
2307 * to guarantee that even unprivileged user processes can set up a
2308 * socket on a privileged port.
2310 * If a UDP socket connect fails, the delay behavior here prevents
2311 * retry floods (hard mounts).
2313 static void xs_connect(struct rpc_xprt
*xprt
, struct rpc_task
*task
)
2315 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2317 if (transport
->sock
!= NULL
&& !RPC_IS_SOFTCONN(task
)) {
2318 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2320 xprt
, xprt
->reestablish_timeout
/ HZ
);
2321 queue_delayed_work(rpciod_workqueue
,
2322 &transport
->connect_worker
,
2323 xprt
->reestablish_timeout
);
2324 xprt
->reestablish_timeout
<<= 1;
2325 if (xprt
->reestablish_timeout
< XS_TCP_INIT_REEST_TO
)
2326 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2327 if (xprt
->reestablish_timeout
> XS_TCP_MAX_REEST_TO
)
2328 xprt
->reestablish_timeout
= XS_TCP_MAX_REEST_TO
;
2330 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt
);
2331 queue_delayed_work(rpciod_workqueue
,
2332 &transport
->connect_worker
, 0);
2337 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2338 * @xprt: rpc_xprt struct containing statistics
2342 static void xs_local_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2346 if (xprt_connected(xprt
))
2347 idle_time
= (long)(jiffies
- xprt
->last_used
) / HZ
;
2349 seq_printf(seq
, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
2350 "%llu %llu %lu %llu %llu\n",
2351 xprt
->stat
.bind_count
,
2352 xprt
->stat
.connect_count
,
2353 xprt
->stat
.connect_time
,
2357 xprt
->stat
.bad_xids
,
2360 xprt
->stat
.max_slots
,
2361 xprt
->stat
.sending_u
,
2362 xprt
->stat
.pending_u
);
2366 * xs_udp_print_stats - display UDP socket-specifc stats
2367 * @xprt: rpc_xprt struct containing statistics
2371 static void xs_udp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2373 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2375 seq_printf(seq
, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2378 xprt
->stat
.bind_count
,
2381 xprt
->stat
.bad_xids
,
2384 xprt
->stat
.max_slots
,
2385 xprt
->stat
.sending_u
,
2386 xprt
->stat
.pending_u
);
2390 * xs_tcp_print_stats - display TCP socket-specifc stats
2391 * @xprt: rpc_xprt struct containing statistics
2395 static void xs_tcp_print_stats(struct rpc_xprt
*xprt
, struct seq_file
*seq
)
2397 struct sock_xprt
*transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2400 if (xprt_connected(xprt
))
2401 idle_time
= (long)(jiffies
- xprt
->last_used
) / HZ
;
2403 seq_printf(seq
, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2404 "%llu %llu %lu %llu %llu\n",
2406 xprt
->stat
.bind_count
,
2407 xprt
->stat
.connect_count
,
2408 xprt
->stat
.connect_time
,
2412 xprt
->stat
.bad_xids
,
2415 xprt
->stat
.max_slots
,
2416 xprt
->stat
.sending_u
,
2417 xprt
->stat
.pending_u
);
2421 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2422 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2423 * to use the server side send routines.
2425 static void *bc_malloc(struct rpc_task
*task
, size_t size
)
2428 struct rpc_buffer
*buf
;
2430 WARN_ON_ONCE(size
> PAGE_SIZE
- sizeof(struct rpc_buffer
));
2431 if (size
> PAGE_SIZE
- sizeof(struct rpc_buffer
))
2434 page
= alloc_page(GFP_KERNEL
);
2438 buf
= page_address(page
);
2439 buf
->len
= PAGE_SIZE
;
2445 * Free the space allocated in the bc_alloc routine
2447 static void bc_free(void *buffer
)
2449 struct rpc_buffer
*buf
;
2454 buf
= container_of(buffer
, struct rpc_buffer
, data
);
2455 free_page((unsigned long)buf
);
2459 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2460 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2462 static int bc_sendto(struct rpc_rqst
*req
)
2465 struct xdr_buf
*xbufp
= &req
->rq_snd_buf
;
2466 struct rpc_xprt
*xprt
= req
->rq_xprt
;
2467 struct sock_xprt
*transport
=
2468 container_of(xprt
, struct sock_xprt
, xprt
);
2469 struct socket
*sock
= transport
->sock
;
2470 unsigned long headoff
;
2471 unsigned long tailoff
;
2473 xs_encode_stream_record_marker(xbufp
);
2475 tailoff
= (unsigned long)xbufp
->tail
[0].iov_base
& ~PAGE_MASK
;
2476 headoff
= (unsigned long)xbufp
->head
[0].iov_base
& ~PAGE_MASK
;
2477 len
= svc_send_common(sock
, xbufp
,
2478 virt_to_page(xbufp
->head
[0].iov_base
), headoff
,
2479 xbufp
->tail
[0].iov_base
, tailoff
);
2481 if (len
!= xbufp
->len
) {
2482 printk(KERN_NOTICE
"Error sending entire callback!\n");
2490 * The send routine. Borrows from svc_send
2492 static int bc_send_request(struct rpc_task
*task
)
2494 struct rpc_rqst
*req
= task
->tk_rqstp
;
2495 struct svc_xprt
*xprt
;
2498 dprintk("sending request with xid: %08x\n", ntohl(req
->rq_xid
));
2500 * Get the server socket associated with this callback xprt
2502 xprt
= req
->rq_xprt
->bc_xprt
;
2505 * Grab the mutex to serialize data as the connection is shared
2506 * with the fore channel
2508 if (!mutex_trylock(&xprt
->xpt_mutex
)) {
2509 rpc_sleep_on(&xprt
->xpt_bc_pending
, task
, NULL
);
2510 if (!mutex_trylock(&xprt
->xpt_mutex
))
2512 rpc_wake_up_queued_task(&xprt
->xpt_bc_pending
, task
);
2514 if (test_bit(XPT_DEAD
, &xprt
->xpt_flags
))
2517 len
= bc_sendto(req
);
2518 mutex_unlock(&xprt
->xpt_mutex
);
2527 * The close routine. Since this is client initiated, we do nothing
2530 static void bc_close(struct rpc_xprt
*xprt
)
2535 * The xprt destroy routine. Again, because this connection is client
2536 * initiated, we do nothing
2539 static void bc_destroy(struct rpc_xprt
*xprt
)
2541 dprintk("RPC: bc_destroy xprt %p\n", xprt
);
2544 module_put(THIS_MODULE
);
2547 static struct rpc_xprt_ops xs_local_ops
= {
2548 .reserve_xprt
= xprt_reserve_xprt
,
2549 .release_xprt
= xs_tcp_release_xprt
,
2550 .alloc_slot
= xprt_alloc_slot
,
2551 .rpcbind
= xs_local_rpcbind
,
2552 .set_port
= xs_local_set_port
,
2553 .connect
= xs_local_connect
,
2554 .buf_alloc
= rpc_malloc
,
2555 .buf_free
= rpc_free
,
2556 .send_request
= xs_local_send_request
,
2557 .set_retrans_timeout
= xprt_set_retrans_timeout_def
,
2559 .destroy
= xs_destroy
,
2560 .print_stats
= xs_local_print_stats
,
2563 static struct rpc_xprt_ops xs_udp_ops
= {
2564 .set_buffer_size
= xs_udp_set_buffer_size
,
2565 .reserve_xprt
= xprt_reserve_xprt_cong
,
2566 .release_xprt
= xprt_release_xprt_cong
,
2567 .alloc_slot
= xprt_alloc_slot
,
2568 .rpcbind
= rpcb_getport_async
,
2569 .set_port
= xs_set_port
,
2570 .connect
= xs_connect
,
2571 .buf_alloc
= rpc_malloc
,
2572 .buf_free
= rpc_free
,
2573 .send_request
= xs_udp_send_request
,
2574 .set_retrans_timeout
= xprt_set_retrans_timeout_rtt
,
2575 .timer
= xs_udp_timer
,
2576 .release_request
= xprt_release_rqst_cong
,
2578 .destroy
= xs_destroy
,
2579 .print_stats
= xs_udp_print_stats
,
2582 static struct rpc_xprt_ops xs_tcp_ops
= {
2583 .reserve_xprt
= xprt_reserve_xprt
,
2584 .release_xprt
= xs_tcp_release_xprt
,
2585 .alloc_slot
= xprt_lock_and_alloc_slot
,
2586 .rpcbind
= rpcb_getport_async
,
2587 .set_port
= xs_set_port
,
2588 .connect
= xs_connect
,
2589 .buf_alloc
= rpc_malloc
,
2590 .buf_free
= rpc_free
,
2591 .send_request
= xs_tcp_send_request
,
2592 .set_retrans_timeout
= xprt_set_retrans_timeout_def
,
2593 .close
= xs_tcp_close
,
2594 .destroy
= xs_destroy
,
2595 .print_stats
= xs_tcp_print_stats
,
2599 * The rpc_xprt_ops for the server backchannel
2602 static struct rpc_xprt_ops bc_tcp_ops
= {
2603 .reserve_xprt
= xprt_reserve_xprt
,
2604 .release_xprt
= xprt_release_xprt
,
2605 .alloc_slot
= xprt_alloc_slot
,
2606 .buf_alloc
= bc_malloc
,
2607 .buf_free
= bc_free
,
2608 .send_request
= bc_send_request
,
2609 .set_retrans_timeout
= xprt_set_retrans_timeout_def
,
2611 .destroy
= bc_destroy
,
2612 .print_stats
= xs_tcp_print_stats
,
2615 static int xs_init_anyaddr(const int family
, struct sockaddr
*sap
)
2617 static const struct sockaddr_in sin
= {
2618 .sin_family
= AF_INET
,
2619 .sin_addr
.s_addr
= htonl(INADDR_ANY
),
2621 static const struct sockaddr_in6 sin6
= {
2622 .sin6_family
= AF_INET6
,
2623 .sin6_addr
= IN6ADDR_ANY_INIT
,
2630 memcpy(sap
, &sin
, sizeof(sin
));
2633 memcpy(sap
, &sin6
, sizeof(sin6
));
2636 dprintk("RPC: %s: Bad address family\n", __func__
);
2637 return -EAFNOSUPPORT
;
2642 static struct rpc_xprt
*xs_setup_xprt(struct xprt_create
*args
,
2643 unsigned int slot_table_size
,
2644 unsigned int max_slot_table_size
)
2646 struct rpc_xprt
*xprt
;
2647 struct sock_xprt
*new;
2649 if (args
->addrlen
> sizeof(xprt
->addr
)) {
2650 dprintk("RPC: xs_setup_xprt: address too large\n");
2651 return ERR_PTR(-EBADF
);
2654 xprt
= xprt_alloc(args
->net
, sizeof(*new), slot_table_size
,
2655 max_slot_table_size
);
2657 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2659 return ERR_PTR(-ENOMEM
);
2662 new = container_of(xprt
, struct sock_xprt
, xprt
);
2663 memcpy(&xprt
->addr
, args
->dstaddr
, args
->addrlen
);
2664 xprt
->addrlen
= args
->addrlen
;
2666 memcpy(&new->srcaddr
, args
->srcaddr
, args
->addrlen
);
2669 err
= xs_init_anyaddr(args
->dstaddr
->sa_family
,
2670 (struct sockaddr
*)&new->srcaddr
);
2673 return ERR_PTR(err
);
2680 static const struct rpc_timeout xs_local_default_timeout
= {
2681 .to_initval
= 10 * HZ
,
2682 .to_maxval
= 10 * HZ
,
2687 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2688 * @args: rpc transport creation arguments
2690 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2692 static struct rpc_xprt
*xs_setup_local(struct xprt_create
*args
)
2694 struct sockaddr_un
*sun
= (struct sockaddr_un
*)args
->dstaddr
;
2695 struct sock_xprt
*transport
;
2696 struct rpc_xprt
*xprt
;
2697 struct rpc_xprt
*ret
;
2699 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
,
2700 xprt_max_tcp_slot_table_entries
);
2703 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2706 xprt
->tsh_size
= sizeof(rpc_fraghdr
) / sizeof(u32
);
2707 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
2709 xprt
->bind_timeout
= XS_BIND_TO
;
2710 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2711 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2713 xprt
->ops
= &xs_local_ops
;
2714 xprt
->timeout
= &xs_local_default_timeout
;
2716 INIT_DELAYED_WORK(&transport
->connect_worker
,
2717 xs_dummy_setup_socket
);
2719 switch (sun
->sun_family
) {
2721 if (sun
->sun_path
[0] != '/') {
2722 dprintk("RPC: bad AF_LOCAL address: %s\n",
2724 ret
= ERR_PTR(-EINVAL
);
2727 xprt_set_bound(xprt
);
2728 xs_format_peer_addresses(xprt
, "local", RPCBIND_NETID_LOCAL
);
2729 ret
= ERR_PTR(xs_local_setup_socket(transport
));
2734 ret
= ERR_PTR(-EAFNOSUPPORT
);
2738 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2739 xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
2741 if (try_module_get(THIS_MODULE
))
2743 ret
= ERR_PTR(-EINVAL
);
2749 static const struct rpc_timeout xs_udp_default_timeout
= {
2750 .to_initval
= 5 * HZ
,
2751 .to_maxval
= 30 * HZ
,
2752 .to_increment
= 5 * HZ
,
2757 * xs_setup_udp - Set up transport to use a UDP socket
2758 * @args: rpc transport creation arguments
2761 static struct rpc_xprt
*xs_setup_udp(struct xprt_create
*args
)
2763 struct sockaddr
*addr
= args
->dstaddr
;
2764 struct rpc_xprt
*xprt
;
2765 struct sock_xprt
*transport
;
2766 struct rpc_xprt
*ret
;
2768 xprt
= xs_setup_xprt(args
, xprt_udp_slot_table_entries
,
2769 xprt_udp_slot_table_entries
);
2772 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2774 xprt
->prot
= IPPROTO_UDP
;
2776 /* XXX: header size can vary due to auth type, IPv6, etc. */
2777 xprt
->max_payload
= (1U << 16) - (MAX_HEADER
<< 3);
2779 xprt
->bind_timeout
= XS_BIND_TO
;
2780 xprt
->reestablish_timeout
= XS_UDP_REEST_TO
;
2781 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2783 xprt
->ops
= &xs_udp_ops
;
2785 xprt
->timeout
= &xs_udp_default_timeout
;
2787 switch (addr
->sa_family
) {
2789 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
2790 xprt_set_bound(xprt
);
2792 INIT_DELAYED_WORK(&transport
->connect_worker
,
2793 xs_udp_setup_socket
);
2794 xs_format_peer_addresses(xprt
, "udp", RPCBIND_NETID_UDP
);
2797 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
2798 xprt_set_bound(xprt
);
2800 INIT_DELAYED_WORK(&transport
->connect_worker
,
2801 xs_udp_setup_socket
);
2802 xs_format_peer_addresses(xprt
, "udp", RPCBIND_NETID_UDP6
);
2805 ret
= ERR_PTR(-EAFNOSUPPORT
);
2809 if (xprt_bound(xprt
))
2810 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2811 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2812 xprt
->address_strings
[RPC_DISPLAY_PORT
],
2813 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2815 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2816 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2817 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2819 if (try_module_get(THIS_MODULE
))
2821 ret
= ERR_PTR(-EINVAL
);
2827 static const struct rpc_timeout xs_tcp_default_timeout
= {
2828 .to_initval
= 60 * HZ
,
2829 .to_maxval
= 60 * HZ
,
2834 * xs_setup_tcp - Set up transport to use a TCP socket
2835 * @args: rpc transport creation arguments
2838 static struct rpc_xprt
*xs_setup_tcp(struct xprt_create
*args
)
2840 struct sockaddr
*addr
= args
->dstaddr
;
2841 struct rpc_xprt
*xprt
;
2842 struct sock_xprt
*transport
;
2843 struct rpc_xprt
*ret
;
2844 unsigned int max_slot_table_size
= xprt_max_tcp_slot_table_entries
;
2846 if (args
->flags
& XPRT_CREATE_INFINITE_SLOTS
)
2847 max_slot_table_size
= RPC_MAX_SLOT_TABLE_LIMIT
;
2849 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
,
2850 max_slot_table_size
);
2853 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2855 xprt
->prot
= IPPROTO_TCP
;
2856 xprt
->tsh_size
= sizeof(rpc_fraghdr
) / sizeof(u32
);
2857 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
2859 xprt
->bind_timeout
= XS_BIND_TO
;
2860 xprt
->reestablish_timeout
= XS_TCP_INIT_REEST_TO
;
2861 xprt
->idle_timeout
= XS_IDLE_DISC_TO
;
2863 xprt
->ops
= &xs_tcp_ops
;
2864 xprt
->timeout
= &xs_tcp_default_timeout
;
2866 switch (addr
->sa_family
) {
2868 if (((struct sockaddr_in
*)addr
)->sin_port
!= htons(0))
2869 xprt_set_bound(xprt
);
2871 INIT_DELAYED_WORK(&transport
->connect_worker
,
2872 xs_tcp_setup_socket
);
2873 xs_format_peer_addresses(xprt
, "tcp", RPCBIND_NETID_TCP
);
2876 if (((struct sockaddr_in6
*)addr
)->sin6_port
!= htons(0))
2877 xprt_set_bound(xprt
);
2879 INIT_DELAYED_WORK(&transport
->connect_worker
,
2880 xs_tcp_setup_socket
);
2881 xs_format_peer_addresses(xprt
, "tcp", RPCBIND_NETID_TCP6
);
2884 ret
= ERR_PTR(-EAFNOSUPPORT
);
2888 if (xprt_bound(xprt
))
2889 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2890 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2891 xprt
->address_strings
[RPC_DISPLAY_PORT
],
2892 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2894 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2895 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2896 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2898 if (try_module_get(THIS_MODULE
))
2900 ret
= ERR_PTR(-EINVAL
);
2907 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2908 * @args: rpc transport creation arguments
2911 static struct rpc_xprt
*xs_setup_bc_tcp(struct xprt_create
*args
)
2913 struct sockaddr
*addr
= args
->dstaddr
;
2914 struct rpc_xprt
*xprt
;
2915 struct sock_xprt
*transport
;
2916 struct svc_sock
*bc_sock
;
2917 struct rpc_xprt
*ret
;
2919 xprt
= xs_setup_xprt(args
, xprt_tcp_slot_table_entries
,
2920 xprt_tcp_slot_table_entries
);
2923 transport
= container_of(xprt
, struct sock_xprt
, xprt
);
2925 xprt
->prot
= IPPROTO_TCP
;
2926 xprt
->tsh_size
= sizeof(rpc_fraghdr
) / sizeof(u32
);
2927 xprt
->max_payload
= RPC_MAX_FRAGMENT_SIZE
;
2928 xprt
->timeout
= &xs_tcp_default_timeout
;
2931 xprt_set_bound(xprt
);
2932 xprt
->bind_timeout
= 0;
2933 xprt
->reestablish_timeout
= 0;
2934 xprt
->idle_timeout
= 0;
2936 xprt
->ops
= &bc_tcp_ops
;
2938 switch (addr
->sa_family
) {
2940 xs_format_peer_addresses(xprt
, "tcp",
2944 xs_format_peer_addresses(xprt
, "tcp",
2945 RPCBIND_NETID_TCP6
);
2948 ret
= ERR_PTR(-EAFNOSUPPORT
);
2952 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2953 xprt
->address_strings
[RPC_DISPLAY_ADDR
],
2954 xprt
->address_strings
[RPC_DISPLAY_PORT
],
2955 xprt
->address_strings
[RPC_DISPLAY_PROTO
]);
2958 * Once we've associated a backchannel xprt with a connection,
2959 * we want to keep it around as long as the connection lasts,
2960 * in case we need to start using it for a backchannel again;
2961 * this reference won't be dropped until bc_xprt is destroyed.
2964 args
->bc_xprt
->xpt_bc_xprt
= xprt
;
2965 xprt
->bc_xprt
= args
->bc_xprt
;
2966 bc_sock
= container_of(args
->bc_xprt
, struct svc_sock
, sk_xprt
);
2967 transport
->sock
= bc_sock
->sk_sock
;
2968 transport
->inet
= bc_sock
->sk_sk
;
2971 * Since we don't want connections for the backchannel, we set
2972 * the xprt status to connected
2974 xprt_set_connected(xprt
);
2976 if (try_module_get(THIS_MODULE
))
2979 args
->bc_xprt
->xpt_bc_xprt
= NULL
;
2981 ret
= ERR_PTR(-EINVAL
);
2987 static struct xprt_class xs_local_transport
= {
2988 .list
= LIST_HEAD_INIT(xs_local_transport
.list
),
2989 .name
= "named UNIX socket",
2990 .owner
= THIS_MODULE
,
2991 .ident
= XPRT_TRANSPORT_LOCAL
,
2992 .setup
= xs_setup_local
,
2995 static struct xprt_class xs_udp_transport
= {
2996 .list
= LIST_HEAD_INIT(xs_udp_transport
.list
),
2998 .owner
= THIS_MODULE
,
2999 .ident
= XPRT_TRANSPORT_UDP
,
3000 .setup
= xs_setup_udp
,
3003 static struct xprt_class xs_tcp_transport
= {
3004 .list
= LIST_HEAD_INIT(xs_tcp_transport
.list
),
3006 .owner
= THIS_MODULE
,
3007 .ident
= XPRT_TRANSPORT_TCP
,
3008 .setup
= xs_setup_tcp
,
3011 static struct xprt_class xs_bc_tcp_transport
= {
3012 .list
= LIST_HEAD_INIT(xs_bc_tcp_transport
.list
),
3013 .name
= "tcp NFSv4.1 backchannel",
3014 .owner
= THIS_MODULE
,
3015 .ident
= XPRT_TRANSPORT_BC_TCP
,
3016 .setup
= xs_setup_bc_tcp
,
3020 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
3023 int init_socket_xprt(void)
3026 if (!sunrpc_table_header
)
3027 sunrpc_table_header
= register_sysctl_table(sunrpc_table
);
3030 xprt_register_transport(&xs_local_transport
);
3031 xprt_register_transport(&xs_udp_transport
);
3032 xprt_register_transport(&xs_tcp_transport
);
3033 xprt_register_transport(&xs_bc_tcp_transport
);
3039 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3042 void cleanup_socket_xprt(void)
3045 if (sunrpc_table_header
) {
3046 unregister_sysctl_table(sunrpc_table_header
);
3047 sunrpc_table_header
= NULL
;
3051 xprt_unregister_transport(&xs_local_transport
);
3052 xprt_unregister_transport(&xs_udp_transport
);
3053 xprt_unregister_transport(&xs_tcp_transport
);
3054 xprt_unregister_transport(&xs_bc_tcp_transport
);
3057 static int param_set_uint_minmax(const char *val
,
3058 const struct kernel_param
*kp
,
3059 unsigned int min
, unsigned int max
)
3066 ret
= strict_strtoul(val
, 0, &num
);
3067 if (ret
== -EINVAL
|| num
< min
|| num
> max
)
3069 *((unsigned int *)kp
->arg
) = num
;
3073 static int param_set_portnr(const char *val
, const struct kernel_param
*kp
)
3075 return param_set_uint_minmax(val
, kp
,
3080 static struct kernel_param_ops param_ops_portnr
= {
3081 .set
= param_set_portnr
,
3082 .get
= param_get_uint
,
3085 #define param_check_portnr(name, p) \
3086 __param_check(name, p, unsigned int);
3088 module_param_named(min_resvport
, xprt_min_resvport
, portnr
, 0644);
3089 module_param_named(max_resvport
, xprt_max_resvport
, portnr
, 0644);
3091 static int param_set_slot_table_size(const char *val
,
3092 const struct kernel_param
*kp
)
3094 return param_set_uint_minmax(val
, kp
,
3096 RPC_MAX_SLOT_TABLE
);
3099 static struct kernel_param_ops param_ops_slot_table_size
= {
3100 .set
= param_set_slot_table_size
,
3101 .get
= param_get_uint
,
3104 #define param_check_slot_table_size(name, p) \
3105 __param_check(name, p, unsigned int);
3107 static int param_set_max_slot_table_size(const char *val
,
3108 const struct kernel_param
*kp
)
3110 return param_set_uint_minmax(val
, kp
,
3112 RPC_MAX_SLOT_TABLE_LIMIT
);
3115 static struct kernel_param_ops param_ops_max_slot_table_size
= {
3116 .set
= param_set_max_slot_table_size
,
3117 .get
= param_get_uint
,
3120 #define param_check_max_slot_table_size(name, p) \
3121 __param_check(name, p, unsigned int);
3123 module_param_named(tcp_slot_table_entries
, xprt_tcp_slot_table_entries
,
3124 slot_table_size
, 0644);
3125 module_param_named(tcp_max_slot_table_entries
, xprt_max_tcp_slot_table_entries
,
3126 max_slot_table_size
, 0644);
3127 module_param_named(udp_slot_table_entries
, xprt_udp_slot_table_entries
,
3128 slot_table_size
, 0644);