2 * linux/net/sunrpc/xprtsock.c
4 * Client-side transport implementation for sockets.
6 * TCP callback races fixes (C) 1998 Red Hat
7 * TCP send fixes (C) 1998 Red Hat
8 * TCP NFS related read + write fixes
9 * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
11 * Rewrite of larges part of the code in order to stabilize TCP stuff.
12 * Fix behaviour when socket buffer is full.
13 * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
15 * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
17 * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
18 * <gilles.quillard@bull.net>
21 #include <linux/types.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <linux/capability.h>
25 #include <linux/pagemap.h>
26 #include <linux/errno.h>
27 #include <linux/socket.h>
29 #include <linux/net.h>
31 #include <linux/udp.h>
32 #include <linux/tcp.h>
33 #include <linux/sunrpc/clnt.h>
34 #include <linux/sunrpc/sched.h>
35 #include <linux/sunrpc/xprtsock.h>
36 #include <linux/file.h>
39 #include <net/checksum.h>
46 unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
47 unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
49 unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
50 unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
52 #define XS_TCP_LINGER_TO (15U * HZ)
53 static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
56 * We can register our own files under /proc/sys/sunrpc by
57 * calling register_sysctl_table() again. The files in that
58 * directory become the union of all files registered there.
60 * We simply need to make sure that we don't collide with
61 * someone else's file names!
66 static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
67 static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
68 static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
69 static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
71 static struct ctl_table_header *sunrpc_table_header;
74 * FIXME: changing the UDP slot table size should also resize the UDP
75 * socket buffers for existing UDP transports
77 static ctl_table xs_tunables_table[] = {
79 .ctl_name = CTL_SLOTTABLE_UDP,
80 .procname = "udp_slot_table_entries",
81 .data = &xprt_udp_slot_table_entries,
82 .maxlen = sizeof(unsigned int),
84 .proc_handler = &proc_dointvec_minmax,
85 .strategy = &sysctl_intvec,
86 .extra1 = &min_slot_table_size,
87 .extra2 = &max_slot_table_size
90 .ctl_name = CTL_SLOTTABLE_TCP,
91 .procname = "tcp_slot_table_entries",
92 .data = &xprt_tcp_slot_table_entries,
93 .maxlen = sizeof(unsigned int),
95 .proc_handler = &proc_dointvec_minmax,
96 .strategy = &sysctl_intvec,
97 .extra1 = &min_slot_table_size,
98 .extra2 = &max_slot_table_size
101 .ctl_name = CTL_MIN_RESVPORT,
102 .procname = "min_resvport",
103 .data = &xprt_min_resvport,
104 .maxlen = sizeof(unsigned int),
106 .proc_handler = &proc_dointvec_minmax,
107 .strategy = &sysctl_intvec,
108 .extra1 = &xprt_min_resvport_limit,
109 .extra2 = &xprt_max_resvport_limit
112 .ctl_name = CTL_MAX_RESVPORT,
113 .procname = "max_resvport",
114 .data = &xprt_max_resvport,
115 .maxlen = sizeof(unsigned int),
117 .proc_handler = &proc_dointvec_minmax,
118 .strategy = &sysctl_intvec,
119 .extra1 = &xprt_min_resvport_limit,
120 .extra2 = &xprt_max_resvport_limit
123 .procname = "tcp_fin_timeout",
124 .data = &xs_tcp_fin_timeout,
125 .maxlen = sizeof(xs_tcp_fin_timeout),
127 .proc_handler = &proc_dointvec_jiffies,
128 .strategy = sysctl_jiffies
135 static ctl_table sunrpc_table[] = {
137 .ctl_name = CTL_SUNRPC,
138 .procname = "sunrpc",
140 .child = xs_tunables_table
150 * Time out for an RPC UDP socket connect. UDP socket connects are
151 * synchronous, but we set a timeout anyway in case of resource
152 * exhaustion on the local host.
154 #define XS_UDP_CONN_TO (5U * HZ)
157 * Wait duration for an RPC TCP connection to be established. Solaris
158 * NFS over TCP uses 60 seconds, for example, which is in line with how
159 * long a server takes to reboot.
161 #define XS_TCP_CONN_TO (60U * HZ)
164 * Wait duration for a reply from the RPC portmapper.
166 #define XS_BIND_TO (60U * HZ)
169 * Delay if a UDP socket connect error occurs. This is most likely some
170 * kind of resource problem on the local host.
172 #define XS_UDP_REEST_TO (2U * HZ)
175 * The reestablish timeout allows clients to delay for a bit before attempting
176 * to reconnect to a server that just dropped our connection.
178 * We implement an exponential backoff when trying to reestablish a TCP
179 * transport connection with the server. Some servers like to drop a TCP
180 * connection when they are overworked, so we start with a short timeout and
181 * increase over time if the server is down or not responding.
183 #define XS_TCP_INIT_REEST_TO (3U * HZ)
184 #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
187 * TCP idle timeout; client drops the transport socket if it is idle
188 * for this long. Note that we also timeout UDP sockets to prevent
189 * holding port numbers when there is no RPC traffic.
191 #define XS_IDLE_DISC_TO (5U * 60 * HZ)
194 # undef RPC_DEBUG_DATA
195 # define RPCDBG_FACILITY RPCDBG_TRANS
198 #ifdef RPC_DEBUG_DATA
199 static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
201 u8 *buf = (u8 *) packet;
204 dprintk("RPC: %s\n", msg);
205 for (j = 0; j < count && j < 128; j += 4) {
209 dprintk("0x%04x ", j);
211 dprintk("%02x%02x%02x%02x ",
212 buf[j], buf[j+1], buf[j+2], buf[j+3]);
217 static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
224 struct rpc_xprt xprt;
229 struct socket * sock;
233 * State of TCP reply receive
241 unsigned long tcp_copied,
245 * Connection of transports
247 struct delayed_work connect_worker;
248 struct sockaddr_storage addr;
252 * UDP socket buffer size parameters
258 * Saved socket callback addresses
260 void (*old_data_ready)(struct sock *, int);
261 void (*old_state_change)(struct sock *);
262 void (*old_write_space)(struct sock *);
263 void (*old_error_report)(struct sock *);
267 * TCP receive state flags
269 #define TCP_RCV_LAST_FRAG (1UL << 0)
270 #define TCP_RCV_COPY_FRAGHDR (1UL << 1)
271 #define TCP_RCV_COPY_XID (1UL << 2)
272 #define TCP_RCV_COPY_DATA (1UL << 3)
274 static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
276 return (struct sockaddr *) &xprt->addr;
279 static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
281 return (struct sockaddr_in *) &xprt->addr;
284 static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
286 return (struct sockaddr_in6 *) &xprt->addr;
289 static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt,
290 const char *protocol,
293 struct sockaddr_in *addr = xs_addr_in(xprt);
296 buf = kzalloc(20, GFP_KERNEL);
298 snprintf(buf, 20, "%pI4", &addr->sin_addr.s_addr);
300 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
302 buf = kzalloc(8, GFP_KERNEL);
304 snprintf(buf, 8, "%u",
305 ntohs(addr->sin_port));
307 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
309 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
311 buf = kzalloc(48, GFP_KERNEL);
313 snprintf(buf, 48, "addr=%pI4 port=%u proto=%s",
314 &addr->sin_addr.s_addr,
315 ntohs(addr->sin_port),
318 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
320 buf = kzalloc(10, GFP_KERNEL);
322 snprintf(buf, 10, "%02x%02x%02x%02x",
323 NIPQUAD(addr->sin_addr.s_addr));
325 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
327 buf = kzalloc(8, GFP_KERNEL);
329 snprintf(buf, 8, "%4hx",
330 ntohs(addr->sin_port));
332 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
334 buf = kzalloc(30, GFP_KERNEL);
336 snprintf(buf, 30, "%pI4.%u.%u",
337 &addr->sin_addr.s_addr,
338 ntohs(addr->sin_port) >> 8,
339 ntohs(addr->sin_port) & 0xff);
341 xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
343 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
346 static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt,
347 const char *protocol,
350 struct sockaddr_in6 *addr = xs_addr_in6(xprt);
353 buf = kzalloc(40, GFP_KERNEL);
355 snprintf(buf, 40, "%pI6",&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 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
368 buf = kzalloc(64, GFP_KERNEL);
370 snprintf(buf, 64, "addr=%pI6 port=%u proto=%s",
372 ntohs(addr->sin6_port),
375 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
377 buf = kzalloc(36, GFP_KERNEL);
379 snprintf(buf, 36, "%pi6", &addr->sin6_addr);
381 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
383 buf = kzalloc(8, GFP_KERNEL);
385 snprintf(buf, 8, "%4hx",
386 ntohs(addr->sin6_port));
388 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
390 buf = kzalloc(50, GFP_KERNEL);
392 snprintf(buf, 50, "%pI6.%u.%u",
394 ntohs(addr->sin6_port) >> 8,
395 ntohs(addr->sin6_port) & 0xff);
397 xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
399 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
402 static void xs_free_peer_addresses(struct rpc_xprt *xprt)
406 for (i = 0; i < RPC_DISPLAY_MAX; i++)
408 case RPC_DISPLAY_PROTO:
409 case RPC_DISPLAY_NETID:
412 kfree(xprt->address_strings[i]);
416 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
418 static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
420 struct msghdr msg = {
422 .msg_namelen = addrlen,
423 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
426 .iov_base = vec->iov_base + base,
427 .iov_len = vec->iov_len - base,
430 if (iov.iov_len != 0)
431 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
432 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
435 static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
438 unsigned int remainder;
441 remainder = xdr->page_len - base;
442 base += xdr->page_base;
443 ppage = xdr->pages + (base >> PAGE_SHIFT);
446 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
447 int flags = XS_SENDMSG_FLAGS;
450 if (remainder != 0 || more)
452 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
453 if (remainder == 0 || err != len)
467 * xs_sendpages - write pages directly to a socket
468 * @sock: socket to send on
469 * @addr: UDP only -- address of destination
470 * @addrlen: UDP only -- length of destination address
471 * @xdr: buffer containing this request
472 * @base: starting position in the buffer
475 static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
477 unsigned int remainder = xdr->len - base;
483 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
489 if (base < xdr->head[0].iov_len || addr != NULL) {
490 unsigned int len = xdr->head[0].iov_len - base;
492 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
493 if (remainder == 0 || err != len)
498 base -= xdr->head[0].iov_len;
500 if (base < xdr->page_len) {
501 unsigned int len = xdr->page_len - base;
503 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
504 if (remainder == 0 || err != len)
509 base -= xdr->page_len;
511 if (base >= xdr->tail[0].iov_len)
513 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
522 static void xs_nospace_callback(struct rpc_task *task)
524 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
526 transport->inet->sk_write_pending--;
527 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
531 * xs_nospace - place task on wait queue if transmit was incomplete
532 * @task: task to put to sleep
535 static int xs_nospace(struct rpc_task *task)
537 struct rpc_rqst *req = task->tk_rqstp;
538 struct rpc_xprt *xprt = req->rq_xprt;
539 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
542 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
543 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
546 /* Protect against races with write_space */
547 spin_lock_bh(&xprt->transport_lock);
549 /* Don't race with disconnect */
550 if (xprt_connected(xprt)) {
551 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
554 * Notify TCP that we're limited by the application
557 set_bit(SOCK_NOSPACE, &transport->sock->flags);
558 transport->inet->sk_write_pending++;
559 /* ...and wait for more buffer space */
560 xprt_wait_for_buffer_space(task, xs_nospace_callback);
563 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
567 spin_unlock_bh(&xprt->transport_lock);
572 * xs_udp_send_request - write an RPC request to a UDP socket
573 * @task: address of RPC task that manages the state of an RPC request
576 * 0: The request has been sent
577 * EAGAIN: The socket was blocked, please call again later to
578 * complete the request
579 * ENOTCONN: Caller needs to invoke connect logic then call again
580 * other: Some other error occured, the request was not sent
582 static int xs_udp_send_request(struct rpc_task *task)
584 struct rpc_rqst *req = task->tk_rqstp;
585 struct rpc_xprt *xprt = req->rq_xprt;
586 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
587 struct xdr_buf *xdr = &req->rq_snd_buf;
590 xs_pktdump("packet data:",
591 req->rq_svec->iov_base,
592 req->rq_svec->iov_len);
594 if (!xprt_bound(xprt))
596 status = xs_sendpages(transport->sock,
601 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
602 xdr->len - req->rq_bytes_sent, status);
605 task->tk_bytes_sent += status;
606 if (status >= req->rq_slen)
608 /* Still some bytes left; set up for a retry later. */
611 if (!transport->sock)
617 /* Should we call xs_close() here? */
620 status = xs_nospace(task);
623 dprintk("RPC: sendmsg returned unrecognized error %d\n",
628 /* When the server has died, an ICMP port unreachable message
629 * prompts ECONNREFUSED. */
630 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
637 * xs_tcp_shutdown - gracefully shut down a TCP socket
640 * Initiates a graceful shutdown of the TCP socket by calling the
641 * equivalent of shutdown(SHUT_WR);
643 static void xs_tcp_shutdown(struct rpc_xprt *xprt)
645 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
646 struct socket *sock = transport->sock;
649 kernel_sock_shutdown(sock, SHUT_WR);
652 static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
654 u32 reclen = buf->len - sizeof(rpc_fraghdr);
655 rpc_fraghdr *base = buf->head[0].iov_base;
656 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
660 * xs_tcp_send_request - write an RPC request to a TCP socket
661 * @task: address of RPC task that manages the state of an RPC request
664 * 0: The request has been sent
665 * EAGAIN: The socket was blocked, please call again later to
666 * complete the request
667 * ENOTCONN: Caller needs to invoke connect logic then call again
668 * other: Some other error occured, the request was not sent
670 * XXX: In the case of soft timeouts, should we eventually give up
671 * if sendmsg is not able to make progress?
673 static int xs_tcp_send_request(struct rpc_task *task)
675 struct rpc_rqst *req = task->tk_rqstp;
676 struct rpc_xprt *xprt = req->rq_xprt;
677 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
678 struct xdr_buf *xdr = &req->rq_snd_buf;
681 xs_encode_tcp_record_marker(&req->rq_snd_buf);
683 xs_pktdump("packet data:",
684 req->rq_svec->iov_base,
685 req->rq_svec->iov_len);
687 /* Continue transmitting the packet/record. We must be careful
688 * to cope with writespace callbacks arriving _after_ we have
689 * called sendmsg(). */
691 status = xs_sendpages(transport->sock,
692 NULL, 0, xdr, req->rq_bytes_sent);
694 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
695 xdr->len - req->rq_bytes_sent, status);
697 if (unlikely(status < 0))
700 /* If we've sent the entire packet, immediately
701 * reset the count of bytes sent. */
702 req->rq_bytes_sent += status;
703 task->tk_bytes_sent += status;
704 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
705 req->rq_bytes_sent = 0;
714 if (!transport->sock)
720 /* Should we call xs_close() here? */
723 status = xs_nospace(task);
726 dprintk("RPC: sendmsg returned unrecognized error %d\n",
730 xs_tcp_shutdown(xprt);
733 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
740 * xs_tcp_release_xprt - clean up after a tcp transmission
744 * This cleans up if an error causes us to abort the transmission of a request.
745 * In this case, the socket may need to be reset in order to avoid confusing
748 static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
750 struct rpc_rqst *req;
752 if (task != xprt->snd_task)
756 req = task->tk_rqstp;
757 if (req->rq_bytes_sent == 0)
759 if (req->rq_bytes_sent == req->rq_snd_buf.len)
761 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
763 xprt_release_xprt(xprt, task);
766 static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
768 transport->old_data_ready = sk->sk_data_ready;
769 transport->old_state_change = sk->sk_state_change;
770 transport->old_write_space = sk->sk_write_space;
771 transport->old_error_report = sk->sk_error_report;
774 static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
776 sk->sk_data_ready = transport->old_data_ready;
777 sk->sk_state_change = transport->old_state_change;
778 sk->sk_write_space = transport->old_write_space;
779 sk->sk_error_report = transport->old_error_report;
782 static void xs_reset_transport(struct sock_xprt *transport)
784 struct socket *sock = transport->sock;
785 struct sock *sk = transport->inet;
790 write_lock_bh(&sk->sk_callback_lock);
791 transport->inet = NULL;
792 transport->sock = NULL;
794 sk->sk_user_data = NULL;
796 xs_restore_old_callbacks(transport, sk);
797 write_unlock_bh(&sk->sk_callback_lock);
805 * xs_close - close a socket
808 * This is used when all requests are complete; ie, no DRC state remains
809 * on the server we want to save.
811 static void xs_close(struct rpc_xprt *xprt)
813 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
815 dprintk("RPC: xs_close xprt %p\n", xprt);
817 xs_reset_transport(transport);
819 smp_mb__before_clear_bit();
820 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
821 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
822 clear_bit(XPRT_CLOSING, &xprt->state);
823 smp_mb__after_clear_bit();
824 xprt_disconnect_done(xprt);
828 * xs_destroy - prepare to shutdown a transport
829 * @xprt: doomed transport
832 static void xs_destroy(struct rpc_xprt *xprt)
834 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
836 dprintk("RPC: xs_destroy xprt %p\n", xprt);
838 cancel_rearming_delayed_work(&transport->connect_worker);
841 xs_free_peer_addresses(xprt);
844 module_put(THIS_MODULE);
847 static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
849 return (struct rpc_xprt *) sk->sk_user_data;
853 * xs_udp_data_ready - "data ready" callback for UDP sockets
854 * @sk: socket with data to read
855 * @len: how much data to read
858 static void xs_udp_data_ready(struct sock *sk, int len)
860 struct rpc_task *task;
861 struct rpc_xprt *xprt;
862 struct rpc_rqst *rovr;
864 int err, repsize, copied;
868 read_lock(&sk->sk_callback_lock);
869 dprintk("RPC: xs_udp_data_ready...\n");
870 if (!(xprt = xprt_from_sock(sk)))
873 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
879 repsize = skb->len - sizeof(struct udphdr);
881 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
885 /* Copy the XID from the skb... */
886 xp = skb_header_pointer(skb, sizeof(struct udphdr),
887 sizeof(_xid), &_xid);
891 /* Look up and lock the request corresponding to the given XID */
892 spin_lock(&xprt->transport_lock);
893 rovr = xprt_lookup_rqst(xprt, *xp);
896 task = rovr->rq_task;
898 if ((copied = rovr->rq_private_buf.buflen) > repsize)
901 /* Suck it into the iovec, verify checksum if not done by hw. */
902 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
903 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
907 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
909 /* Something worked... */
910 dst_confirm(skb->dst);
912 xprt_adjust_cwnd(task, copied);
913 xprt_update_rtt(task);
914 xprt_complete_rqst(task, copied);
917 spin_unlock(&xprt->transport_lock);
919 skb_free_datagram(sk, skb);
921 read_unlock(&sk->sk_callback_lock);
924 static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
926 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
930 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
931 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
932 used = xdr_skb_read_bits(desc, p, len);
933 transport->tcp_offset += used;
937 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
938 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
939 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
941 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
942 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
944 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
945 transport->tcp_offset = 0;
947 /* Sanity check of the record length */
948 if (unlikely(transport->tcp_reclen < 4)) {
949 dprintk("RPC: invalid TCP record fragment length\n");
950 xprt_force_disconnect(xprt);
953 dprintk("RPC: reading TCP record fragment of length %d\n",
954 transport->tcp_reclen);
957 static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
959 if (transport->tcp_offset == transport->tcp_reclen) {
960 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
961 transport->tcp_offset = 0;
962 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
963 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
964 transport->tcp_flags |= TCP_RCV_COPY_XID;
965 transport->tcp_copied = 0;
970 static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
975 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
976 dprintk("RPC: reading XID (%Zu bytes)\n", len);
977 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
978 used = xdr_skb_read_bits(desc, p, len);
979 transport->tcp_offset += used;
982 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
983 transport->tcp_flags |= TCP_RCV_COPY_DATA;
984 transport->tcp_copied = 4;
985 dprintk("RPC: reading reply for XID %08x\n",
986 ntohl(transport->tcp_xid));
987 xs_tcp_check_fraghdr(transport);
990 static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
992 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
993 struct rpc_rqst *req;
994 struct xdr_buf *rcvbuf;
998 /* Find and lock the request corresponding to this xid */
999 spin_lock(&xprt->transport_lock);
1000 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1002 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1003 dprintk("RPC: XID %08x request not found!\n",
1004 ntohl(transport->tcp_xid));
1005 spin_unlock(&xprt->transport_lock);
1009 rcvbuf = &req->rq_private_buf;
1011 if (len > transport->tcp_reclen - transport->tcp_offset) {
1012 struct xdr_skb_reader my_desc;
1014 len = transport->tcp_reclen - transport->tcp_offset;
1015 memcpy(&my_desc, desc, sizeof(my_desc));
1016 my_desc.count = len;
1017 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
1018 &my_desc, xdr_skb_read_bits);
1022 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
1023 desc, xdr_skb_read_bits);
1026 transport->tcp_copied += r;
1027 transport->tcp_offset += r;
1030 /* Error when copying to the receive buffer,
1031 * usually because we weren't able to allocate
1032 * additional buffer pages. All we can do now
1033 * is turn off TCP_RCV_COPY_DATA, so the request
1034 * will not receive any additional updates,
1036 * Any remaining data from this record will
1039 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1040 dprintk("RPC: XID %08x truncated request\n",
1041 ntohl(transport->tcp_xid));
1042 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1043 "tcp_offset = %u, tcp_reclen = %u\n",
1044 xprt, transport->tcp_copied,
1045 transport->tcp_offset, transport->tcp_reclen);
1049 dprintk("RPC: XID %08x read %Zd bytes\n",
1050 ntohl(transport->tcp_xid), r);
1051 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1052 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1053 transport->tcp_offset, transport->tcp_reclen);
1055 if (transport->tcp_copied == req->rq_private_buf.buflen)
1056 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1057 else if (transport->tcp_offset == transport->tcp_reclen) {
1058 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1059 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1063 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
1064 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
1065 spin_unlock(&xprt->transport_lock);
1066 xs_tcp_check_fraghdr(transport);
1069 static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1073 len = transport->tcp_reclen - transport->tcp_offset;
1074 if (len > desc->count)
1077 desc->offset += len;
1078 transport->tcp_offset += len;
1079 dprintk("RPC: discarded %Zu bytes\n", len);
1080 xs_tcp_check_fraghdr(transport);
1083 static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
1085 struct rpc_xprt *xprt = rd_desc->arg.data;
1086 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1087 struct xdr_skb_reader desc = {
1093 dprintk("RPC: xs_tcp_data_recv started\n");
1095 /* Read in a new fragment marker if necessary */
1096 /* Can we ever really expect to get completely empty fragments? */
1097 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
1098 xs_tcp_read_fraghdr(xprt, &desc);
1101 /* Read in the xid if necessary */
1102 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
1103 xs_tcp_read_xid(transport, &desc);
1106 /* Read in the request data */
1107 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
1108 xs_tcp_read_request(xprt, &desc);
1111 /* Skip over any trailing bytes on short reads */
1112 xs_tcp_read_discard(transport, &desc);
1113 } while (desc.count);
1114 dprintk("RPC: xs_tcp_data_recv done\n");
1115 return len - desc.count;
1119 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1120 * @sk: socket with data to read
1121 * @bytes: how much data to read
1124 static void xs_tcp_data_ready(struct sock *sk, int bytes)
1126 struct rpc_xprt *xprt;
1127 read_descriptor_t rd_desc;
1130 dprintk("RPC: xs_tcp_data_ready...\n");
1132 read_lock(&sk->sk_callback_lock);
1133 if (!(xprt = xprt_from_sock(sk)))
1138 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1139 rd_desc.arg.data = xprt;
1141 rd_desc.count = 65536;
1142 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1145 read_unlock(&sk->sk_callback_lock);
1149 * Do the equivalent of linger/linger2 handling for dealing with
1150 * broken servers that don't close the socket in a timely
1153 static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1154 unsigned long timeout)
1156 struct sock_xprt *transport;
1158 if (xprt_test_and_set_connecting(xprt))
1160 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1161 transport = container_of(xprt, struct sock_xprt, xprt);
1162 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1166 static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1168 struct sock_xprt *transport;
1170 transport = container_of(xprt, struct sock_xprt, xprt);
1172 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1173 !cancel_delayed_work(&transport->connect_worker))
1175 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1176 xprt_clear_connecting(xprt);
1179 static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1181 smp_mb__before_clear_bit();
1182 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1183 clear_bit(XPRT_CLOSING, &xprt->state);
1184 smp_mb__after_clear_bit();
1185 /* Mark transport as closed and wake up all pending tasks */
1186 xprt_disconnect_done(xprt);
1190 * xs_tcp_state_change - callback to handle TCP socket state changes
1191 * @sk: socket whose state has changed
1194 static void xs_tcp_state_change(struct sock *sk)
1196 struct rpc_xprt *xprt;
1198 read_lock(&sk->sk_callback_lock);
1199 if (!(xprt = xprt_from_sock(sk)))
1201 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1202 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1203 sk->sk_state, xprt_connected(xprt),
1204 sock_flag(sk, SOCK_DEAD),
1205 sock_flag(sk, SOCK_ZAPPED));
1207 switch (sk->sk_state) {
1208 case TCP_ESTABLISHED:
1209 spin_lock_bh(&xprt->transport_lock);
1210 if (!xprt_test_and_set_connected(xprt)) {
1211 struct sock_xprt *transport = container_of(xprt,
1212 struct sock_xprt, xprt);
1214 /* Reset TCP record info */
1215 transport->tcp_offset = 0;
1216 transport->tcp_reclen = 0;
1217 transport->tcp_copied = 0;
1218 transport->tcp_flags =
1219 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
1221 xprt_wake_pending_tasks(xprt, -EAGAIN);
1223 spin_unlock_bh(&xprt->transport_lock);
1226 /* The client initiated a shutdown of the socket */
1227 xprt->connect_cookie++;
1228 xprt->reestablish_timeout = 0;
1229 set_bit(XPRT_CLOSING, &xprt->state);
1230 smp_mb__before_clear_bit();
1231 clear_bit(XPRT_CONNECTED, &xprt->state);
1232 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1233 smp_mb__after_clear_bit();
1234 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1236 case TCP_CLOSE_WAIT:
1237 /* The server initiated a shutdown of the socket */
1238 xprt_force_disconnect(xprt);
1240 xprt->connect_cookie++;
1243 * If the server closed down the connection, make sure that
1244 * we back off before reconnecting
1246 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1247 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1250 set_bit(XPRT_CLOSING, &xprt->state);
1251 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1252 smp_mb__before_clear_bit();
1253 clear_bit(XPRT_CONNECTED, &xprt->state);
1254 smp_mb__after_clear_bit();
1257 xs_tcp_cancel_linger_timeout(xprt);
1258 xs_sock_mark_closed(xprt);
1261 read_unlock(&sk->sk_callback_lock);
1265 * xs_error_report - callback mainly for catching socket errors
1268 static void xs_error_report(struct sock *sk)
1270 struct rpc_xprt *xprt;
1272 read_lock(&sk->sk_callback_lock);
1273 if (!(xprt = xprt_from_sock(sk)))
1275 dprintk("RPC: %s client %p...\n"
1277 __func__, xprt, sk->sk_err);
1278 xprt_wake_pending_tasks(xprt, -EAGAIN);
1280 read_unlock(&sk->sk_callback_lock);
1283 static void xs_write_space(struct sock *sk)
1285 struct socket *sock;
1286 struct rpc_xprt *xprt;
1288 if (unlikely(!(sock = sk->sk_socket)))
1290 clear_bit(SOCK_NOSPACE, &sock->flags);
1292 if (unlikely(!(xprt = xprt_from_sock(sk))))
1294 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1297 xprt_write_space(xprt);
1301 * xs_udp_write_space - callback invoked when socket buffer space
1303 * @sk: socket whose state has changed
1305 * Called when more output buffer space is available for this socket.
1306 * We try not to wake our writers until they can make "significant"
1307 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1308 * with a bunch of small requests.
1310 static void xs_udp_write_space(struct sock *sk)
1312 read_lock(&sk->sk_callback_lock);
1314 /* from net/core/sock.c:sock_def_write_space */
1315 if (sock_writeable(sk))
1318 read_unlock(&sk->sk_callback_lock);
1322 * xs_tcp_write_space - callback invoked when socket buffer space
1324 * @sk: socket whose state has changed
1326 * Called when more output buffer space is available for this socket.
1327 * We try not to wake our writers until they can make "significant"
1328 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1329 * with a bunch of small requests.
1331 static void xs_tcp_write_space(struct sock *sk)
1333 read_lock(&sk->sk_callback_lock);
1335 /* from net/core/stream.c:sk_stream_write_space */
1336 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1339 read_unlock(&sk->sk_callback_lock);
1342 static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1344 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1345 struct sock *sk = transport->inet;
1347 if (transport->rcvsize) {
1348 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
1349 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1351 if (transport->sndsize) {
1352 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
1353 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1354 sk->sk_write_space(sk);
1359 * xs_udp_set_buffer_size - set send and receive limits
1360 * @xprt: generic transport
1361 * @sndsize: requested size of send buffer, in bytes
1362 * @rcvsize: requested size of receive buffer, in bytes
1364 * Set socket send and receive buffer size limits.
1366 static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
1368 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1370 transport->sndsize = 0;
1372 transport->sndsize = sndsize + 1024;
1373 transport->rcvsize = 0;
1375 transport->rcvsize = rcvsize + 1024;
1377 xs_udp_do_set_buffer_size(xprt);
1381 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1382 * @task: task that timed out
1384 * Adjust the congestion window after a retransmit timeout has occurred.
1386 static void xs_udp_timer(struct rpc_task *task)
1388 xprt_adjust_cwnd(task, -ETIMEDOUT);
1391 static unsigned short xs_get_random_port(void)
1393 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1394 unsigned short rand = (unsigned short) net_random() % range;
1395 return rand + xprt_min_resvport;
1399 * xs_set_port - reset the port number in the remote endpoint address
1400 * @xprt: generic transport
1401 * @port: new port number
1404 static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1406 struct sockaddr *addr = xs_addr(xprt);
1408 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
1410 switch (addr->sa_family) {
1412 ((struct sockaddr_in *)addr)->sin_port = htons(port);
1415 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
1422 static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
1424 unsigned short port = transport->port;
1426 if (port == 0 && transport->xprt.resvport)
1427 port = xs_get_random_port();
1431 static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
1433 if (transport->port != 0)
1434 transport->port = 0;
1435 if (!transport->xprt.resvport)
1437 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1438 return xprt_max_resvport;
1442 static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
1444 struct sockaddr_in myaddr = {
1445 .sin_family = AF_INET,
1447 struct sockaddr_in *sa;
1449 unsigned short port = xs_get_srcport(transport, sock);
1450 unsigned short last;
1452 sa = (struct sockaddr_in *)&transport->addr;
1453 myaddr.sin_addr = sa->sin_addr;
1455 myaddr.sin_port = htons(port);
1456 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1461 transport->port = port;
1465 port = xs_next_srcport(transport, sock, port);
1468 } while (err == -EADDRINUSE && nloop != 2);
1469 dprintk("RPC: %s %pI4:%u: %s (%d)\n",
1470 __func__, &myaddr.sin_addr,
1471 port, err ? "failed" : "ok", err);
1475 static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
1477 struct sockaddr_in6 myaddr = {
1478 .sin6_family = AF_INET6,
1480 struct sockaddr_in6 *sa;
1482 unsigned short port = xs_get_srcport(transport, sock);
1483 unsigned short last;
1485 sa = (struct sockaddr_in6 *)&transport->addr;
1486 myaddr.sin6_addr = sa->sin6_addr;
1488 myaddr.sin6_port = htons(port);
1489 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1494 transport->port = port;
1498 port = xs_next_srcport(transport, sock, port);
1501 } while (err == -EADDRINUSE && nloop != 2);
1502 dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n",
1503 &myaddr.sin6_addr, port, err ? "failed" : "ok", err);
1507 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1508 static struct lock_class_key xs_key[2];
1509 static struct lock_class_key xs_slock_key[2];
1511 static inline void xs_reclassify_socket4(struct socket *sock)
1513 struct sock *sk = sock->sk;
1515 BUG_ON(sock_owned_by_user(sk));
1516 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1517 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1520 static inline void xs_reclassify_socket6(struct socket *sock)
1522 struct sock *sk = sock->sk;
1524 BUG_ON(sock_owned_by_user(sk));
1525 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1526 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1529 static inline void xs_reclassify_socket4(struct socket *sock)
1533 static inline void xs_reclassify_socket6(struct socket *sock)
1538 static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1540 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1542 if (!transport->inet) {
1543 struct sock *sk = sock->sk;
1545 write_lock_bh(&sk->sk_callback_lock);
1547 xs_save_old_callbacks(transport, sk);
1549 sk->sk_user_data = xprt;
1550 sk->sk_data_ready = xs_udp_data_ready;
1551 sk->sk_write_space = xs_udp_write_space;
1552 sk->sk_error_report = xs_error_report;
1553 sk->sk_no_check = UDP_CSUM_NORCV;
1554 sk->sk_allocation = GFP_ATOMIC;
1556 xprt_set_connected(xprt);
1558 /* Reset to new socket */
1559 transport->sock = sock;
1560 transport->inet = sk;
1562 write_unlock_bh(&sk->sk_callback_lock);
1564 xs_udp_do_set_buffer_size(xprt);
1568 * xs_udp_connect_worker4 - set up a UDP socket
1569 * @work: RPC transport to connect
1571 * Invoked by a work queue tasklet.
1573 static void xs_udp_connect_worker4(struct work_struct *work)
1575 struct sock_xprt *transport =
1576 container_of(work, struct sock_xprt, connect_worker.work);
1577 struct rpc_xprt *xprt = &transport->xprt;
1578 struct socket *sock = transport->sock;
1579 int err, status = -EIO;
1584 /* Start by resetting any existing state */
1585 xs_reset_transport(transport);
1587 err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1589 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1592 xs_reclassify_socket4(sock);
1594 if (xs_bind4(transport, sock)) {
1599 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1600 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1602 xs_udp_finish_connecting(xprt, sock);
1605 xprt_clear_connecting(xprt);
1606 xprt_wake_pending_tasks(xprt, status);
1610 * xs_udp_connect_worker6 - set up a UDP socket
1611 * @work: RPC transport to connect
1613 * Invoked by a work queue tasklet.
1615 static void xs_udp_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;
1626 /* Start by resetting any existing state */
1627 xs_reset_transport(transport);
1629 err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1631 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1634 xs_reclassify_socket6(sock);
1636 if (xs_bind6(transport, sock) < 0) {
1641 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1642 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1644 xs_udp_finish_connecting(xprt, sock);
1647 xprt_clear_connecting(xprt);
1648 xprt_wake_pending_tasks(xprt, status);
1652 * We need to preserve the port number so the reply cache on the server can
1653 * find our cached RPC replies when we get around to reconnecting.
1655 static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
1658 struct sockaddr any;
1660 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
1663 * Disconnect the transport socket by doing a connect operation
1664 * with AF_UNSPEC. This should return immediately...
1666 memset(&any, 0, sizeof(any));
1667 any.sa_family = AF_UNSPEC;
1668 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
1670 xs_sock_mark_closed(xprt);
1672 dprintk("RPC: AF_UNSPEC connect return code %d\n",
1676 static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
1678 unsigned int state = transport->inet->sk_state;
1680 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED)
1682 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT))
1684 xs_abort_connection(xprt, transport);
1687 static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1689 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1691 if (!transport->inet) {
1692 struct sock *sk = sock->sk;
1694 write_lock_bh(&sk->sk_callback_lock);
1696 xs_save_old_callbacks(transport, sk);
1698 sk->sk_user_data = xprt;
1699 sk->sk_data_ready = xs_tcp_data_ready;
1700 sk->sk_state_change = xs_tcp_state_change;
1701 sk->sk_write_space = xs_tcp_write_space;
1702 sk->sk_error_report = xs_error_report;
1703 sk->sk_allocation = GFP_ATOMIC;
1705 /* socket options */
1706 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1707 sock_reset_flag(sk, SOCK_LINGER);
1708 tcp_sk(sk)->linger2 = 0;
1709 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
1711 xprt_clear_connected(xprt);
1713 /* Reset to new socket */
1714 transport->sock = sock;
1715 transport->inet = sk;
1717 write_unlock_bh(&sk->sk_callback_lock);
1720 if (!xprt_bound(xprt))
1723 /* Tell the socket layer to start connecting... */
1724 xprt->stat.connect_count++;
1725 xprt->stat.connect_start = jiffies;
1726 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
1730 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1731 * @xprt: RPC transport to connect
1732 * @transport: socket transport to connect
1733 * @create_sock: function to create a socket of the correct type
1735 * Invoked by a work queue tasklet.
1737 static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1738 struct sock_xprt *transport,
1739 struct socket *(*create_sock)(struct rpc_xprt *,
1740 struct sock_xprt *))
1742 struct socket *sock = transport->sock;
1749 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1750 sock = create_sock(xprt, transport);
1752 status = PTR_ERR(sock);
1758 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
1760 /* "close" the socket, preserving the local port */
1761 xs_tcp_reuse_connection(xprt, transport);
1767 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1768 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1770 status = xs_tcp_finish_connecting(xprt, sock);
1771 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1772 xprt, -status, xprt_connected(xprt),
1773 sock->sk->sk_state);
1778 /* retry with existing socket, after a delay */
1782 xprt_clear_connecting(xprt);
1785 /* get rid of existing socket, and retry */
1786 xs_tcp_shutdown(xprt);
1787 printk("%s: connect returned unhandled error %d\n",
1792 xprt_clear_connecting(xprt);
1793 xprt_wake_pending_tasks(xprt, status);
1796 static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1797 struct sock_xprt *transport)
1799 struct socket *sock;
1802 /* start from scratch */
1803 err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1805 dprintk("RPC: can't create TCP transport socket (%d).\n",
1809 xs_reclassify_socket4(sock);
1811 if (xs_bind4(transport, sock) < 0) {
1817 return ERR_PTR(-EIO);
1821 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1822 * @work: RPC transport to connect
1824 * Invoked by a work queue tasklet.
1826 static void xs_tcp_connect_worker4(struct work_struct *work)
1828 struct sock_xprt *transport =
1829 container_of(work, struct sock_xprt, connect_worker.work);
1830 struct rpc_xprt *xprt = &transport->xprt;
1832 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
1835 static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1836 struct sock_xprt *transport)
1838 struct socket *sock;
1841 /* start from scratch */
1842 err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
1844 dprintk("RPC: can't create TCP transport socket (%d).\n",
1848 xs_reclassify_socket6(sock);
1850 if (xs_bind6(transport, sock) < 0) {
1856 return ERR_PTR(-EIO);
1860 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
1861 * @work: RPC transport to connect
1863 * Invoked by a work queue tasklet.
1865 static void xs_tcp_connect_worker6(struct work_struct *work)
1867 struct sock_xprt *transport =
1868 container_of(work, struct sock_xprt, connect_worker.work);
1869 struct rpc_xprt *xprt = &transport->xprt;
1871 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
1875 * xs_connect - connect a socket to a remote endpoint
1876 * @task: address of RPC task that manages state of connect request
1878 * TCP: If the remote end dropped the connection, delay reconnecting.
1880 * UDP socket connects are synchronous, but we use a work queue anyway
1881 * to guarantee that even unprivileged user processes can set up a
1882 * socket on a privileged port.
1884 * If a UDP socket connect fails, the delay behavior here prevents
1885 * retry floods (hard mounts).
1887 static void xs_connect(struct rpc_task *task)
1889 struct rpc_xprt *xprt = task->tk_xprt;
1890 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1892 if (xprt_test_and_set_connecting(xprt))
1895 if (transport->sock != NULL) {
1896 dprintk("RPC: xs_connect delayed xprt %p for %lu "
1898 xprt, xprt->reestablish_timeout / HZ);
1899 queue_delayed_work(rpciod_workqueue,
1900 &transport->connect_worker,
1901 xprt->reestablish_timeout);
1902 xprt->reestablish_timeout <<= 1;
1903 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
1904 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
1906 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
1907 queue_delayed_work(rpciod_workqueue,
1908 &transport->connect_worker, 0);
1912 static void xs_tcp_connect(struct rpc_task *task)
1914 struct rpc_xprt *xprt = task->tk_xprt;
1916 /* Exit if we need to wait for socket shutdown to complete */
1917 if (test_bit(XPRT_CLOSING, &xprt->state))
1923 * xs_udp_print_stats - display UDP socket-specifc stats
1924 * @xprt: rpc_xprt struct containing statistics
1928 static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1930 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1932 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
1934 xprt->stat.bind_count,
1937 xprt->stat.bad_xids,
1939 xprt->stat.bklog_u);
1943 * xs_tcp_print_stats - display TCP socket-specifc stats
1944 * @xprt: rpc_xprt struct containing statistics
1948 static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1950 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1953 if (xprt_connected(xprt))
1954 idle_time = (long)(jiffies - xprt->last_used) / HZ;
1956 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
1958 xprt->stat.bind_count,
1959 xprt->stat.connect_count,
1960 xprt->stat.connect_time,
1964 xprt->stat.bad_xids,
1966 xprt->stat.bklog_u);
1969 static struct rpc_xprt_ops xs_udp_ops = {
1970 .set_buffer_size = xs_udp_set_buffer_size,
1971 .reserve_xprt = xprt_reserve_xprt_cong,
1972 .release_xprt = xprt_release_xprt_cong,
1973 .rpcbind = rpcb_getport_async,
1974 .set_port = xs_set_port,
1975 .connect = xs_connect,
1976 .buf_alloc = rpc_malloc,
1977 .buf_free = rpc_free,
1978 .send_request = xs_udp_send_request,
1979 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
1980 .timer = xs_udp_timer,
1981 .release_request = xprt_release_rqst_cong,
1983 .destroy = xs_destroy,
1984 .print_stats = xs_udp_print_stats,
1987 static struct rpc_xprt_ops xs_tcp_ops = {
1988 .reserve_xprt = xprt_reserve_xprt,
1989 .release_xprt = xs_tcp_release_xprt,
1990 .rpcbind = rpcb_getport_async,
1991 .set_port = xs_set_port,
1992 .connect = xs_tcp_connect,
1993 .buf_alloc = rpc_malloc,
1994 .buf_free = rpc_free,
1995 .send_request = xs_tcp_send_request,
1996 .set_retrans_timeout = xprt_set_retrans_timeout_def,
1997 .close = xs_tcp_shutdown,
1998 .destroy = xs_destroy,
1999 .print_stats = xs_tcp_print_stats,
2002 static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2003 unsigned int slot_table_size)
2005 struct rpc_xprt *xprt;
2006 struct sock_xprt *new;
2008 if (args->addrlen > sizeof(xprt->addr)) {
2009 dprintk("RPC: xs_setup_xprt: address too large\n");
2010 return ERR_PTR(-EBADF);
2013 new = kzalloc(sizeof(*new), GFP_KERNEL);
2015 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2017 return ERR_PTR(-ENOMEM);
2021 xprt->max_reqs = slot_table_size;
2022 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2023 if (xprt->slot == NULL) {
2025 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
2027 return ERR_PTR(-ENOMEM);
2030 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2031 xprt->addrlen = args->addrlen;
2033 memcpy(&new->addr, args->srcaddr, args->addrlen);
2038 static const struct rpc_timeout xs_udp_default_timeout = {
2039 .to_initval = 5 * HZ,
2040 .to_maxval = 30 * HZ,
2041 .to_increment = 5 * HZ,
2046 * xs_setup_udp - Set up transport to use a UDP socket
2047 * @args: rpc transport creation arguments
2050 static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2052 struct sockaddr *addr = args->dstaddr;
2053 struct rpc_xprt *xprt;
2054 struct sock_xprt *transport;
2056 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
2059 transport = container_of(xprt, struct sock_xprt, xprt);
2061 xprt->prot = IPPROTO_UDP;
2063 /* XXX: header size can vary due to auth type, IPv6, etc. */
2064 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2066 xprt->bind_timeout = XS_BIND_TO;
2067 xprt->connect_timeout = XS_UDP_CONN_TO;
2068 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2069 xprt->idle_timeout = XS_IDLE_DISC_TO;
2071 xprt->ops = &xs_udp_ops;
2073 xprt->timeout = &xs_udp_default_timeout;
2075 switch (addr->sa_family) {
2077 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2078 xprt_set_bound(xprt);
2080 INIT_DELAYED_WORK(&transport->connect_worker,
2081 xs_udp_connect_worker4);
2082 xs_format_ipv4_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
2085 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2086 xprt_set_bound(xprt);
2088 INIT_DELAYED_WORK(&transport->connect_worker,
2089 xs_udp_connect_worker6);
2090 xs_format_ipv6_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
2094 return ERR_PTR(-EAFNOSUPPORT);
2097 dprintk("RPC: set up transport to address %s\n",
2098 xprt->address_strings[RPC_DISPLAY_ALL]);
2100 if (try_module_get(THIS_MODULE))
2105 return ERR_PTR(-EINVAL);
2108 static const struct rpc_timeout xs_tcp_default_timeout = {
2109 .to_initval = 60 * HZ,
2110 .to_maxval = 60 * HZ,
2115 * xs_setup_tcp - Set up transport to use a TCP socket
2116 * @args: rpc transport creation arguments
2119 static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2121 struct sockaddr *addr = args->dstaddr;
2122 struct rpc_xprt *xprt;
2123 struct sock_xprt *transport;
2125 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2128 transport = container_of(xprt, struct sock_xprt, xprt);
2130 xprt->prot = IPPROTO_TCP;
2131 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2132 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2134 xprt->bind_timeout = XS_BIND_TO;
2135 xprt->connect_timeout = XS_TCP_CONN_TO;
2136 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2137 xprt->idle_timeout = XS_IDLE_DISC_TO;
2139 xprt->ops = &xs_tcp_ops;
2140 xprt->timeout = &xs_tcp_default_timeout;
2142 switch (addr->sa_family) {
2144 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2145 xprt_set_bound(xprt);
2147 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4);
2148 xs_format_ipv4_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
2151 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2152 xprt_set_bound(xprt);
2154 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6);
2155 xs_format_ipv6_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
2159 return ERR_PTR(-EAFNOSUPPORT);
2162 dprintk("RPC: set up transport to address %s\n",
2163 xprt->address_strings[RPC_DISPLAY_ALL]);
2165 if (try_module_get(THIS_MODULE))
2170 return ERR_PTR(-EINVAL);
2173 static struct xprt_class xs_udp_transport = {
2174 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2176 .owner = THIS_MODULE,
2177 .ident = IPPROTO_UDP,
2178 .setup = xs_setup_udp,
2181 static struct xprt_class xs_tcp_transport = {
2182 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2184 .owner = THIS_MODULE,
2185 .ident = IPPROTO_TCP,
2186 .setup = xs_setup_tcp,
2190 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2193 int init_socket_xprt(void)
2196 if (!sunrpc_table_header)
2197 sunrpc_table_header = register_sysctl_table(sunrpc_table);
2200 xprt_register_transport(&xs_udp_transport);
2201 xprt_register_transport(&xs_tcp_transport);
2207 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
2210 void cleanup_socket_xprt(void)
2213 if (sunrpc_table_header) {
2214 unregister_sysctl_table(sunrpc_table_header);
2215 sunrpc_table_header = NULL;
2219 xprt_unregister_transport(&xs_udp_transport);
2220 xprt_unregister_transport(&xs_tcp_transport);