2 * linux/net/sunrpc/xprtsock.c
4 * Client-side transport implementation for sockets.
6 * TCP callback races fixes (C) 1998 Red Hat Software <alan@redhat.com>
7 * TCP send fixes (C) 1998 Red Hat Software <alan@redhat.com>
8 * TCP NFS related read + write fixes
9 * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
11 * Rewrite of larges part of the code in order to stabilize TCP stuff.
12 * Fix behaviour when socket buffer is full.
13 * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
15 * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
18 #include <linux/types.h>
19 #include <linux/slab.h>
20 #include <linux/capability.h>
21 #include <linux/sched.h>
22 #include <linux/pagemap.h>
23 #include <linux/errno.h>
24 #include <linux/socket.h>
26 #include <linux/net.h>
28 #include <linux/udp.h>
29 #include <linux/tcp.h>
30 #include <linux/sunrpc/clnt.h>
31 #include <linux/sunrpc/sched.h>
32 #include <linux/file.h>
35 #include <net/checksum.h>
42 unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
43 unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
45 unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
46 unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
49 * How many times to try sending a request on a socket before waiting
50 * for the socket buffer to clear.
52 #define XS_SENDMSG_RETRY (10U)
55 * Time out for an RPC UDP socket connect. UDP socket connects are
56 * synchronous, but we set a timeout anyway in case of resource
57 * exhaustion on the local host.
59 #define XS_UDP_CONN_TO (5U * HZ)
62 * Wait duration for an RPC TCP connection to be established. Solaris
63 * NFS over TCP uses 60 seconds, for example, which is in line with how
64 * long a server takes to reboot.
66 #define XS_TCP_CONN_TO (60U * HZ)
69 * Wait duration for a reply from the RPC portmapper.
71 #define XS_BIND_TO (60U * HZ)
74 * Delay if a UDP socket connect error occurs. This is most likely some
75 * kind of resource problem on the local host.
77 #define XS_UDP_REEST_TO (2U * HZ)
80 * The reestablish timeout allows clients to delay for a bit before attempting
81 * to reconnect to a server that just dropped our connection.
83 * We implement an exponential backoff when trying to reestablish a TCP
84 * transport connection with the server. Some servers like to drop a TCP
85 * connection when they are overworked, so we start with a short timeout and
86 * increase over time if the server is down or not responding.
88 #define XS_TCP_INIT_REEST_TO (3U * HZ)
89 #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
92 * TCP idle timeout; client drops the transport socket if it is idle
93 * for this long. Note that we also timeout UDP sockets to prevent
94 * holding port numbers when there is no RPC traffic.
96 #define XS_IDLE_DISC_TO (5U * 60 * HZ)
99 # undef RPC_DEBUG_DATA
100 # define RPCDBG_FACILITY RPCDBG_TRANS
103 #ifdef RPC_DEBUG_DATA
104 static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
106 u8 *buf = (u8 *) packet;
109 dprintk("RPC: %s\n", msg);
110 for (j = 0; j < count && j < 128; j += 4) {
114 dprintk("0x%04x ", j);
116 dprintk("%02x%02x%02x%02x ",
117 buf[j], buf[j+1], buf[j+2], buf[j+3]);
122 static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
128 static void xs_format_peer_addresses(struct rpc_xprt *xprt)
130 struct sockaddr_in *addr = (struct sockaddr_in *) &xprt->addr;
133 buf = kzalloc(20, GFP_KERNEL);
135 snprintf(buf, 20, "%u.%u.%u.%u",
136 NIPQUAD(addr->sin_addr.s_addr));
138 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
140 buf = kzalloc(8, GFP_KERNEL);
142 snprintf(buf, 8, "%u",
143 ntohs(addr->sin_port));
145 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
147 if (xprt->prot == IPPROTO_UDP)
148 xprt->address_strings[RPC_DISPLAY_PROTO] = "udp";
150 xprt->address_strings[RPC_DISPLAY_PROTO] = "tcp";
152 buf = kzalloc(48, GFP_KERNEL);
154 snprintf(buf, 48, "addr=%u.%u.%u.%u port=%u proto=%s",
155 NIPQUAD(addr->sin_addr.s_addr),
156 ntohs(addr->sin_port),
157 xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
159 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
162 static void xs_free_peer_addresses(struct rpc_xprt *xprt)
164 kfree(xprt->address_strings[RPC_DISPLAY_ADDR]);
165 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
166 kfree(xprt->address_strings[RPC_DISPLAY_ALL]);
169 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
171 static inline int xs_send_head(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, unsigned int len)
174 .iov_base = xdr->head[0].iov_base + base,
175 .iov_len = len - base,
177 struct msghdr msg = {
179 .msg_namelen = addrlen,
180 .msg_flags = XS_SENDMSG_FLAGS,
184 msg.msg_flags |= MSG_MORE;
186 if (likely(iov.iov_len))
187 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
188 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
191 static int xs_send_tail(struct socket *sock, struct xdr_buf *xdr, unsigned int base, unsigned int len)
194 .iov_base = xdr->tail[0].iov_base + base,
195 .iov_len = len - base,
197 struct msghdr msg = {
198 .msg_flags = XS_SENDMSG_FLAGS,
201 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
205 * xs_sendpages - write pages directly to a socket
206 * @sock: socket to send on
207 * @addr: UDP only -- address of destination
208 * @addrlen: UDP only -- length of destination address
209 * @xdr: buffer containing this request
210 * @base: starting position in the buffer
213 static inline int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
215 struct page **ppage = xdr->pages;
216 unsigned int len, pglen = xdr->page_len;
222 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
224 len = xdr->head[0].iov_len;
225 if (base < len || (addr != NULL && base == 0)) {
226 err = xs_send_head(sock, addr, addrlen, xdr, base, len);
231 if (err != (len - base))
237 if (unlikely(pglen == 0))
239 if (unlikely(base >= pglen)) {
243 if (base || xdr->page_base) {
245 base += xdr->page_base;
246 ppage += base >> PAGE_CACHE_SHIFT;
247 base &= ~PAGE_CACHE_MASK;
251 int flags = XS_SENDMSG_FLAGS;
253 len = PAGE_CACHE_SIZE;
259 if (pglen != len || xdr->tail[0].iov_len != 0)
262 err = kernel_sendpage(sock, *ppage, base, len, flags);
271 } while ((pglen -= len) != 0);
273 len = xdr->tail[0].iov_len;
275 err = xs_send_tail(sock, xdr, base, len);
286 * xs_nospace - place task on wait queue if transmit was incomplete
287 * @task: task to put to sleep
290 static void xs_nospace(struct rpc_task *task)
292 struct rpc_rqst *req = task->tk_rqstp;
293 struct rpc_xprt *xprt = req->rq_xprt;
295 dprintk("RPC: %4d xmit incomplete (%u left of %u)\n",
296 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
299 if (test_bit(SOCK_ASYNC_NOSPACE, &xprt->sock->flags)) {
300 /* Protect against races with write_space */
301 spin_lock_bh(&xprt->transport_lock);
303 /* Don't race with disconnect */
304 if (!xprt_connected(xprt))
305 task->tk_status = -ENOTCONN;
306 else if (test_bit(SOCK_NOSPACE, &xprt->sock->flags))
307 xprt_wait_for_buffer_space(task);
309 spin_unlock_bh(&xprt->transport_lock);
311 /* Keep holding the socket if it is blocked */
312 rpc_delay(task, HZ>>4);
316 * xs_udp_send_request - write an RPC request to a UDP socket
317 * @task: address of RPC task that manages the state of an RPC request
320 * 0: The request has been sent
321 * EAGAIN: The socket was blocked, please call again later to
322 * complete the request
323 * ENOTCONN: Caller needs to invoke connect logic then call again
324 * other: Some other error occured, the request was not sent
326 static int xs_udp_send_request(struct rpc_task *task)
328 struct rpc_rqst *req = task->tk_rqstp;
329 struct rpc_xprt *xprt = req->rq_xprt;
330 struct xdr_buf *xdr = &req->rq_snd_buf;
333 xs_pktdump("packet data:",
334 req->rq_svec->iov_base,
335 req->rq_svec->iov_len);
337 req->rq_xtime = jiffies;
338 status = xs_sendpages(xprt->sock, (struct sockaddr *) &xprt->addr,
339 xprt->addrlen, xdr, req->rq_bytes_sent);
341 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
342 xdr->len - req->rq_bytes_sent, status);
344 if (likely(status >= (int) req->rq_slen))
347 /* Still some bytes left; set up for a retry later. */
355 /* When the server has died, an ICMP port unreachable message
356 * prompts ECONNREFUSED. */
362 dprintk("RPC: sendmsg returned unrecognized error %d\n",
370 static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
372 u32 reclen = buf->len - sizeof(rpc_fraghdr);
373 rpc_fraghdr *base = buf->head[0].iov_base;
374 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
378 * xs_tcp_send_request - write an RPC request to a TCP socket
379 * @task: address of RPC task that manages the state of an RPC request
382 * 0: The request has been sent
383 * EAGAIN: The socket was blocked, please call again later to
384 * complete the request
385 * ENOTCONN: Caller needs to invoke connect logic then call again
386 * other: Some other error occured, the request was not sent
388 * XXX: In the case of soft timeouts, should we eventually give up
389 * if sendmsg is not able to make progress?
391 static int xs_tcp_send_request(struct rpc_task *task)
393 struct rpc_rqst *req = task->tk_rqstp;
394 struct rpc_xprt *xprt = req->rq_xprt;
395 struct xdr_buf *xdr = &req->rq_snd_buf;
396 int status, retry = 0;
398 xs_encode_tcp_record_marker(&req->rq_snd_buf);
400 xs_pktdump("packet data:",
401 req->rq_svec->iov_base,
402 req->rq_svec->iov_len);
404 /* Continue transmitting the packet/record. We must be careful
405 * to cope with writespace callbacks arriving _after_ we have
406 * called sendmsg(). */
408 req->rq_xtime = jiffies;
409 status = xs_sendpages(xprt->sock, NULL, 0, xdr,
412 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
413 xdr->len - req->rq_bytes_sent, status);
415 if (unlikely(status < 0))
418 /* If we've sent the entire packet, immediately
419 * reset the count of bytes sent. */
420 req->rq_bytes_sent += status;
421 task->tk_bytes_sent += status;
422 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
423 req->rq_bytes_sent = 0;
428 if (retry++ > XS_SENDMSG_RETRY)
443 dprintk("RPC: sendmsg returned unrecognized error %d\n",
445 xprt_disconnect(xprt);
453 * xs_tcp_release_xprt - clean up after a tcp transmission
457 * This cleans up if an error causes us to abort the transmission of a request.
458 * In this case, the socket may need to be reset in order to avoid confusing
461 static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
463 struct rpc_rqst *req;
465 if (task != xprt->snd_task)
469 req = task->tk_rqstp;
470 if (req->rq_bytes_sent == 0)
472 if (req->rq_bytes_sent == req->rq_snd_buf.len)
474 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
476 xprt_release_xprt(xprt, task);
480 * xs_close - close a socket
483 * This is used when all requests are complete; ie, no DRC state remains
484 * on the server we want to save.
486 static void xs_close(struct rpc_xprt *xprt)
488 struct socket *sock = xprt->sock;
489 struct sock *sk = xprt->inet;
492 goto clear_close_wait;
494 dprintk("RPC: xs_close xprt %p\n", xprt);
496 write_lock_bh(&sk->sk_callback_lock);
500 sk->sk_user_data = NULL;
501 sk->sk_data_ready = xprt->old_data_ready;
502 sk->sk_state_change = xprt->old_state_change;
503 sk->sk_write_space = xprt->old_write_space;
504 write_unlock_bh(&sk->sk_callback_lock);
510 smp_mb__before_clear_bit();
511 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
512 smp_mb__after_clear_bit();
516 * xs_destroy - prepare to shutdown a transport
517 * @xprt: doomed transport
520 static void xs_destroy(struct rpc_xprt *xprt)
522 dprintk("RPC: xs_destroy xprt %p\n", xprt);
524 cancel_delayed_work(&xprt->connect_worker);
525 flush_scheduled_work();
527 xprt_disconnect(xprt);
529 xs_free_peer_addresses(xprt);
533 static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
535 return (struct rpc_xprt *) sk->sk_user_data;
539 * xs_udp_data_ready - "data ready" callback for UDP sockets
540 * @sk: socket with data to read
541 * @len: how much data to read
544 static void xs_udp_data_ready(struct sock *sk, int len)
546 struct rpc_task *task;
547 struct rpc_xprt *xprt;
548 struct rpc_rqst *rovr;
550 int err, repsize, copied;
554 read_lock(&sk->sk_callback_lock);
555 dprintk("RPC: xs_udp_data_ready...\n");
556 if (!(xprt = xprt_from_sock(sk)))
559 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
565 repsize = skb->len - sizeof(struct udphdr);
567 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
571 /* Copy the XID from the skb... */
572 xp = skb_header_pointer(skb, sizeof(struct udphdr),
573 sizeof(_xid), &_xid);
577 /* Look up and lock the request corresponding to the given XID */
578 spin_lock(&xprt->transport_lock);
579 rovr = xprt_lookup_rqst(xprt, *xp);
582 task = rovr->rq_task;
584 if ((copied = rovr->rq_private_buf.buflen) > repsize)
587 /* Suck it into the iovec, verify checksum if not done by hw. */
588 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb))
591 /* Something worked... */
592 dst_confirm(skb->dst);
594 xprt_adjust_cwnd(task, copied);
595 xprt_update_rtt(task);
596 xprt_complete_rqst(task, copied);
599 spin_unlock(&xprt->transport_lock);
601 skb_free_datagram(sk, skb);
603 read_unlock(&sk->sk_callback_lock);
606 static inline size_t xs_tcp_copy_data(skb_reader_t *desc, void *p, size_t len)
608 if (len > desc->count)
610 if (skb_copy_bits(desc->skb, desc->offset, p, len)) {
611 dprintk("RPC: failed to copy %zu bytes from skb. %zu bytes remain\n",
617 dprintk("RPC: copied %zu bytes from skb. %zu bytes remain\n",
622 static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, skb_reader_t *desc)
627 p = ((char *) &xprt->tcp_recm) + xprt->tcp_offset;
628 len = sizeof(xprt->tcp_recm) - xprt->tcp_offset;
629 used = xs_tcp_copy_data(desc, p, len);
630 xprt->tcp_offset += used;
634 xprt->tcp_reclen = ntohl(xprt->tcp_recm);
635 if (xprt->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
636 xprt->tcp_flags |= XPRT_LAST_FRAG;
638 xprt->tcp_flags &= ~XPRT_LAST_FRAG;
639 xprt->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
641 xprt->tcp_flags &= ~XPRT_COPY_RECM;
642 xprt->tcp_offset = 0;
644 /* Sanity check of the record length */
645 if (unlikely(xprt->tcp_reclen < 4)) {
646 dprintk("RPC: invalid TCP record fragment length\n");
647 xprt_disconnect(xprt);
650 dprintk("RPC: reading TCP record fragment of length %d\n",
654 static void xs_tcp_check_recm(struct rpc_xprt *xprt)
656 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u, tcp_flags = %lx\n",
657 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen, xprt->tcp_flags);
658 if (xprt->tcp_offset == xprt->tcp_reclen) {
659 xprt->tcp_flags |= XPRT_COPY_RECM;
660 xprt->tcp_offset = 0;
661 if (xprt->tcp_flags & XPRT_LAST_FRAG) {
662 xprt->tcp_flags &= ~XPRT_COPY_DATA;
663 xprt->tcp_flags |= XPRT_COPY_XID;
664 xprt->tcp_copied = 0;
669 static inline void xs_tcp_read_xid(struct rpc_xprt *xprt, skb_reader_t *desc)
674 len = sizeof(xprt->tcp_xid) - xprt->tcp_offset;
675 dprintk("RPC: reading XID (%Zu bytes)\n", len);
676 p = ((char *) &xprt->tcp_xid) + xprt->tcp_offset;
677 used = xs_tcp_copy_data(desc, p, len);
678 xprt->tcp_offset += used;
681 xprt->tcp_flags &= ~XPRT_COPY_XID;
682 xprt->tcp_flags |= XPRT_COPY_DATA;
683 xprt->tcp_copied = 4;
684 dprintk("RPC: reading reply for XID %08x\n",
685 ntohl(xprt->tcp_xid));
686 xs_tcp_check_recm(xprt);
689 static inline void xs_tcp_read_request(struct rpc_xprt *xprt, skb_reader_t *desc)
691 struct rpc_rqst *req;
692 struct xdr_buf *rcvbuf;
696 /* Find and lock the request corresponding to this xid */
697 spin_lock(&xprt->transport_lock);
698 req = xprt_lookup_rqst(xprt, xprt->tcp_xid);
700 xprt->tcp_flags &= ~XPRT_COPY_DATA;
701 dprintk("RPC: XID %08x request not found!\n",
702 ntohl(xprt->tcp_xid));
703 spin_unlock(&xprt->transport_lock);
707 rcvbuf = &req->rq_private_buf;
709 if (len > xprt->tcp_reclen - xprt->tcp_offset) {
710 skb_reader_t my_desc;
712 len = xprt->tcp_reclen - xprt->tcp_offset;
713 memcpy(&my_desc, desc, sizeof(my_desc));
715 r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied,
716 &my_desc, xs_tcp_copy_data);
720 r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied,
721 desc, xs_tcp_copy_data);
724 xprt->tcp_copied += r;
725 xprt->tcp_offset += r;
728 /* Error when copying to the receive buffer,
729 * usually because we weren't able to allocate
730 * additional buffer pages. All we can do now
731 * is turn off XPRT_COPY_DATA, so the request
732 * will not receive any additional updates,
734 * Any remaining data from this record will
737 xprt->tcp_flags &= ~XPRT_COPY_DATA;
738 dprintk("RPC: XID %08x truncated request\n",
739 ntohl(xprt->tcp_xid));
740 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
741 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen);
745 dprintk("RPC: XID %08x read %Zd bytes\n",
746 ntohl(xprt->tcp_xid), r);
747 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
748 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen);
750 if (xprt->tcp_copied == req->rq_private_buf.buflen)
751 xprt->tcp_flags &= ~XPRT_COPY_DATA;
752 else if (xprt->tcp_offset == xprt->tcp_reclen) {
753 if (xprt->tcp_flags & XPRT_LAST_FRAG)
754 xprt->tcp_flags &= ~XPRT_COPY_DATA;
758 if (!(xprt->tcp_flags & XPRT_COPY_DATA))
759 xprt_complete_rqst(req->rq_task, xprt->tcp_copied);
760 spin_unlock(&xprt->transport_lock);
761 xs_tcp_check_recm(xprt);
764 static inline void xs_tcp_read_discard(struct rpc_xprt *xprt, skb_reader_t *desc)
768 len = xprt->tcp_reclen - xprt->tcp_offset;
769 if (len > desc->count)
773 xprt->tcp_offset += len;
774 dprintk("RPC: discarded %Zu bytes\n", len);
775 xs_tcp_check_recm(xprt);
778 static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
780 struct rpc_xprt *xprt = rd_desc->arg.data;
781 skb_reader_t desc = {
788 dprintk("RPC: xs_tcp_data_recv started\n");
790 /* Read in a new fragment marker if necessary */
791 /* Can we ever really expect to get completely empty fragments? */
792 if (xprt->tcp_flags & XPRT_COPY_RECM) {
793 xs_tcp_read_fraghdr(xprt, &desc);
796 /* Read in the xid if necessary */
797 if (xprt->tcp_flags & XPRT_COPY_XID) {
798 xs_tcp_read_xid(xprt, &desc);
801 /* Read in the request data */
802 if (xprt->tcp_flags & XPRT_COPY_DATA) {
803 xs_tcp_read_request(xprt, &desc);
806 /* Skip over any trailing bytes on short reads */
807 xs_tcp_read_discard(xprt, &desc);
808 } while (desc.count);
809 dprintk("RPC: xs_tcp_data_recv done\n");
810 return len - desc.count;
814 * xs_tcp_data_ready - "data ready" callback for TCP sockets
815 * @sk: socket with data to read
816 * @bytes: how much data to read
819 static void xs_tcp_data_ready(struct sock *sk, int bytes)
821 struct rpc_xprt *xprt;
822 read_descriptor_t rd_desc;
824 read_lock(&sk->sk_callback_lock);
825 dprintk("RPC: xs_tcp_data_ready...\n");
826 if (!(xprt = xprt_from_sock(sk)))
831 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
832 rd_desc.arg.data = xprt;
833 rd_desc.count = 65536;
834 tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
836 read_unlock(&sk->sk_callback_lock);
840 * xs_tcp_state_change - callback to handle TCP socket state changes
841 * @sk: socket whose state has changed
844 static void xs_tcp_state_change(struct sock *sk)
846 struct rpc_xprt *xprt;
848 read_lock(&sk->sk_callback_lock);
849 if (!(xprt = xprt_from_sock(sk)))
851 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
852 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
853 sk->sk_state, xprt_connected(xprt),
854 sock_flag(sk, SOCK_DEAD),
855 sock_flag(sk, SOCK_ZAPPED));
857 switch (sk->sk_state) {
858 case TCP_ESTABLISHED:
859 spin_lock_bh(&xprt->transport_lock);
860 if (!xprt_test_and_set_connected(xprt)) {
861 /* Reset TCP record info */
862 xprt->tcp_offset = 0;
863 xprt->tcp_reclen = 0;
864 xprt->tcp_copied = 0;
865 xprt->tcp_flags = XPRT_COPY_RECM | XPRT_COPY_XID;
866 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
867 xprt_wake_pending_tasks(xprt, 0);
869 spin_unlock_bh(&xprt->transport_lock);
875 /* Try to schedule an autoclose RPC calls */
876 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
877 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
878 schedule_work(&xprt->task_cleanup);
880 xprt_disconnect(xprt);
883 read_unlock(&sk->sk_callback_lock);
887 * xs_udp_write_space - callback invoked when socket buffer space
889 * @sk: socket whose state has changed
891 * Called when more output buffer space is available for this socket.
892 * We try not to wake our writers until they can make "significant"
893 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
894 * with a bunch of small requests.
896 static void xs_udp_write_space(struct sock *sk)
898 read_lock(&sk->sk_callback_lock);
900 /* from net/core/sock.c:sock_def_write_space */
901 if (sock_writeable(sk)) {
903 struct rpc_xprt *xprt;
905 if (unlikely(!(sock = sk->sk_socket)))
907 if (unlikely(!(xprt = xprt_from_sock(sk))))
909 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
912 xprt_write_space(xprt);
916 read_unlock(&sk->sk_callback_lock);
920 * xs_tcp_write_space - callback invoked when socket buffer space
922 * @sk: socket whose state has changed
924 * Called when more output buffer space is available for this socket.
925 * We try not to wake our writers until they can make "significant"
926 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
927 * with a bunch of small requests.
929 static void xs_tcp_write_space(struct sock *sk)
931 read_lock(&sk->sk_callback_lock);
933 /* from net/core/stream.c:sk_stream_write_space */
934 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
936 struct rpc_xprt *xprt;
938 if (unlikely(!(sock = sk->sk_socket)))
940 if (unlikely(!(xprt = xprt_from_sock(sk))))
942 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
945 xprt_write_space(xprt);
949 read_unlock(&sk->sk_callback_lock);
952 static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
954 struct sock *sk = xprt->inet;
957 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
958 sk->sk_rcvbuf = xprt->rcvsize * xprt->max_reqs * 2;
961 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
962 sk->sk_sndbuf = xprt->sndsize * xprt->max_reqs * 2;
963 sk->sk_write_space(sk);
968 * xs_udp_set_buffer_size - set send and receive limits
969 * @xprt: generic transport
970 * @sndsize: requested size of send buffer, in bytes
971 * @rcvsize: requested size of receive buffer, in bytes
973 * Set socket send and receive buffer size limits.
975 static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
979 xprt->sndsize = sndsize + 1024;
982 xprt->rcvsize = rcvsize + 1024;
984 xs_udp_do_set_buffer_size(xprt);
988 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
989 * @task: task that timed out
991 * Adjust the congestion window after a retransmit timeout has occurred.
993 static void xs_udp_timer(struct rpc_task *task)
995 xprt_adjust_cwnd(task, -ETIMEDOUT);
998 static unsigned short xs_get_random_port(void)
1000 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1001 unsigned short rand = (unsigned short) net_random() % range;
1002 return rand + xprt_min_resvport;
1006 * xs_print_peer_address - format an IPv4 address for printing
1007 * @xprt: generic transport
1008 * @format: flags field indicating which parts of the address to render
1010 static char *xs_print_peer_address(struct rpc_xprt *xprt, enum rpc_display_format_t format)
1012 if (xprt->address_strings[format] != NULL)
1013 return xprt->address_strings[format];
1015 return "unprintable";
1019 * xs_set_port - reset the port number in the remote endpoint address
1020 * @xprt: generic transport
1021 * @port: new port number
1024 static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1026 struct sockaddr_in *sap = (struct sockaddr_in *) &xprt->addr;
1028 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
1030 sap->sin_port = htons(port);
1033 static int xs_bindresvport(struct rpc_xprt *xprt, struct socket *sock)
1035 struct sockaddr_in myaddr = {
1036 .sin_family = AF_INET,
1039 unsigned short port = xprt->port;
1042 myaddr.sin_port = htons(port);
1043 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1047 dprintk("RPC: xs_bindresvport bound to port %u\n",
1051 if (port <= xprt_min_resvport)
1052 port = xprt_max_resvport;
1055 } while (err == -EADDRINUSE && port != xprt->port);
1057 dprintk("RPC: can't bind to reserved port (%d).\n", -err);
1062 * xs_udp_connect_worker - set up a UDP socket
1063 * @args: RPC transport to connect
1065 * Invoked by a work queue tasklet.
1067 static void xs_udp_connect_worker(void *args)
1069 struct rpc_xprt *xprt = (struct rpc_xprt *) args;
1070 struct socket *sock = xprt->sock;
1071 int err, status = -EIO;
1073 if (xprt->shutdown || !xprt_bound(xprt))
1076 /* Start by resetting any existing state */
1079 if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
1080 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1084 if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) {
1089 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1090 xprt, xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1093 struct sock *sk = sock->sk;
1095 write_lock_bh(&sk->sk_callback_lock);
1097 sk->sk_user_data = xprt;
1098 xprt->old_data_ready = sk->sk_data_ready;
1099 xprt->old_state_change = sk->sk_state_change;
1100 xprt->old_write_space = sk->sk_write_space;
1101 sk->sk_data_ready = xs_udp_data_ready;
1102 sk->sk_write_space = xs_udp_write_space;
1103 sk->sk_no_check = UDP_CSUM_NORCV;
1104 sk->sk_allocation = GFP_ATOMIC;
1106 xprt_set_connected(xprt);
1108 /* Reset to new socket */
1112 write_unlock_bh(&sk->sk_callback_lock);
1114 xs_udp_do_set_buffer_size(xprt);
1117 xprt_wake_pending_tasks(xprt, status);
1118 xprt_clear_connecting(xprt);
1122 * We need to preserve the port number so the reply cache on the server can
1123 * find our cached RPC replies when we get around to reconnecting.
1125 static void xs_tcp_reuse_connection(struct rpc_xprt *xprt)
1128 struct socket *sock = xprt->sock;
1129 struct sockaddr any;
1131 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
1134 * Disconnect the transport socket by doing a connect operation
1135 * with AF_UNSPEC. This should return immediately...
1137 memset(&any, 0, sizeof(any));
1138 any.sa_family = AF_UNSPEC;
1139 result = kernel_connect(sock, &any, sizeof(any), 0);
1141 dprintk("RPC: AF_UNSPEC connect return code %d\n",
1146 * xs_tcp_connect_worker - connect a TCP socket to a remote endpoint
1147 * @args: RPC transport to connect
1149 * Invoked by a work queue tasklet.
1151 static void xs_tcp_connect_worker(void *args)
1153 struct rpc_xprt *xprt = (struct rpc_xprt *)args;
1154 struct socket *sock = xprt->sock;
1155 int err, status = -EIO;
1157 if (xprt->shutdown || !xprt_bound(xprt))
1161 /* start from scratch */
1162 if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
1163 dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
1167 if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) {
1172 /* "close" the socket, preserving the local port */
1173 xs_tcp_reuse_connection(xprt);
1175 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1176 xprt, xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1179 struct sock *sk = sock->sk;
1181 write_lock_bh(&sk->sk_callback_lock);
1183 sk->sk_user_data = xprt;
1184 xprt->old_data_ready = sk->sk_data_ready;
1185 xprt->old_state_change = sk->sk_state_change;
1186 xprt->old_write_space = sk->sk_write_space;
1187 sk->sk_data_ready = xs_tcp_data_ready;
1188 sk->sk_state_change = xs_tcp_state_change;
1189 sk->sk_write_space = xs_tcp_write_space;
1190 sk->sk_allocation = GFP_ATOMIC;
1192 /* socket options */
1193 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1194 sock_reset_flag(sk, SOCK_LINGER);
1195 tcp_sk(sk)->linger2 = 0;
1196 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
1198 xprt_clear_connected(xprt);
1200 /* Reset to new socket */
1204 write_unlock_bh(&sk->sk_callback_lock);
1207 /* Tell the socket layer to start connecting... */
1208 xprt->stat.connect_count++;
1209 xprt->stat.connect_start = jiffies;
1210 status = kernel_connect(sock, (struct sockaddr *) &xprt->addr,
1211 xprt->addrlen, O_NONBLOCK);
1212 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1213 xprt, -status, xprt_connected(xprt), sock->sk->sk_state);
1221 /* retry with existing socket, after a delay */
1224 /* get rid of existing socket, and retry */
1230 xprt_wake_pending_tasks(xprt, status);
1232 xprt_clear_connecting(xprt);
1236 * xs_connect - connect a socket to a remote endpoint
1237 * @task: address of RPC task that manages state of connect request
1239 * TCP: If the remote end dropped the connection, delay reconnecting.
1241 * UDP socket connects are synchronous, but we use a work queue anyway
1242 * to guarantee that even unprivileged user processes can set up a
1243 * socket on a privileged port.
1245 * If a UDP socket connect fails, the delay behavior here prevents
1246 * retry floods (hard mounts).
1248 static void xs_connect(struct rpc_task *task)
1250 struct rpc_xprt *xprt = task->tk_xprt;
1252 if (xprt_test_and_set_connecting(xprt))
1255 if (xprt->sock != NULL) {
1256 dprintk("RPC: xs_connect delayed xprt %p for %lu seconds\n",
1257 xprt, xprt->reestablish_timeout / HZ);
1258 schedule_delayed_work(&xprt->connect_worker,
1259 xprt->reestablish_timeout);
1260 xprt->reestablish_timeout <<= 1;
1261 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
1262 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
1264 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
1265 schedule_work(&xprt->connect_worker);
1267 /* flush_scheduled_work can sleep... */
1268 if (!RPC_IS_ASYNC(task))
1269 flush_scheduled_work();
1274 * xs_udp_print_stats - display UDP socket-specifc stats
1275 * @xprt: rpc_xprt struct containing statistics
1279 static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1281 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
1283 xprt->stat.bind_count,
1286 xprt->stat.bad_xids,
1288 xprt->stat.bklog_u);
1292 * xs_tcp_print_stats - display TCP socket-specifc stats
1293 * @xprt: rpc_xprt struct containing statistics
1297 static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1301 if (xprt_connected(xprt))
1302 idle_time = (long)(jiffies - xprt->last_used) / HZ;
1304 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
1306 xprt->stat.bind_count,
1307 xprt->stat.connect_count,
1308 xprt->stat.connect_time,
1312 xprt->stat.bad_xids,
1314 xprt->stat.bklog_u);
1317 static struct rpc_xprt_ops xs_udp_ops = {
1318 .set_buffer_size = xs_udp_set_buffer_size,
1319 .print_addr = xs_print_peer_address,
1320 .reserve_xprt = xprt_reserve_xprt_cong,
1321 .release_xprt = xprt_release_xprt_cong,
1322 .rpcbind = rpc_getport,
1323 .set_port = xs_set_port,
1324 .connect = xs_connect,
1325 .buf_alloc = rpc_malloc,
1326 .buf_free = rpc_free,
1327 .send_request = xs_udp_send_request,
1328 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
1329 .timer = xs_udp_timer,
1330 .release_request = xprt_release_rqst_cong,
1332 .destroy = xs_destroy,
1333 .print_stats = xs_udp_print_stats,
1336 static struct rpc_xprt_ops xs_tcp_ops = {
1337 .print_addr = xs_print_peer_address,
1338 .reserve_xprt = xprt_reserve_xprt,
1339 .release_xprt = xs_tcp_release_xprt,
1340 .rpcbind = rpc_getport,
1341 .set_port = xs_set_port,
1342 .connect = xs_connect,
1343 .buf_alloc = rpc_malloc,
1344 .buf_free = rpc_free,
1345 .send_request = xs_tcp_send_request,
1346 .set_retrans_timeout = xprt_set_retrans_timeout_def,
1348 .destroy = xs_destroy,
1349 .print_stats = xs_tcp_print_stats,
1353 * xs_setup_udp - Set up transport to use a UDP socket
1354 * @xprt: transport to set up
1355 * @to: timeout parameters
1358 int xs_setup_udp(struct rpc_xprt *xprt, struct rpc_timeout *to)
1360 size_t slot_table_size;
1361 struct sockaddr_in *addr = (struct sockaddr_in *) &xprt->addr;
1363 xprt->max_reqs = xprt_udp_slot_table_entries;
1364 slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]);
1365 xprt->slot = kzalloc(slot_table_size, GFP_KERNEL);
1366 if (xprt->slot == NULL)
1369 if (ntohs(addr->sin_port != 0))
1370 xprt_set_bound(xprt);
1371 xprt->port = xs_get_random_port();
1373 xprt->prot = IPPROTO_UDP;
1375 /* XXX: header size can vary due to auth type, IPv6, etc. */
1376 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
1378 INIT_WORK(&xprt->connect_worker, xs_udp_connect_worker, xprt);
1379 xprt->bind_timeout = XS_BIND_TO;
1380 xprt->connect_timeout = XS_UDP_CONN_TO;
1381 xprt->reestablish_timeout = XS_UDP_REEST_TO;
1382 xprt->idle_timeout = XS_IDLE_DISC_TO;
1384 xprt->ops = &xs_udp_ops;
1387 xprt->timeout = *to;
1389 xprt_set_timeout(&xprt->timeout, 5, 5 * HZ);
1391 xs_format_peer_addresses(xprt);
1392 dprintk("RPC: set up transport to address %s\n",
1393 xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1399 * xs_setup_tcp - Set up transport to use a TCP socket
1400 * @xprt: transport to set up
1401 * @to: timeout parameters
1404 int xs_setup_tcp(struct rpc_xprt *xprt, struct rpc_timeout *to)
1406 size_t slot_table_size;
1407 struct sockaddr_in *addr = (struct sockaddr_in *) &xprt->addr;
1409 xprt->max_reqs = xprt_tcp_slot_table_entries;
1410 slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]);
1411 xprt->slot = kzalloc(slot_table_size, GFP_KERNEL);
1412 if (xprt->slot == NULL)
1415 if (ntohs(addr->sin_port) != 0)
1416 xprt_set_bound(xprt);
1417 xprt->port = xs_get_random_port();
1419 xprt->prot = IPPROTO_TCP;
1420 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
1421 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
1423 INIT_WORK(&xprt->connect_worker, xs_tcp_connect_worker, xprt);
1424 xprt->bind_timeout = XS_BIND_TO;
1425 xprt->connect_timeout = XS_TCP_CONN_TO;
1426 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1427 xprt->idle_timeout = XS_IDLE_DISC_TO;
1429 xprt->ops = &xs_tcp_ops;
1432 xprt->timeout = *to;
1434 xprt_set_timeout(&xprt->timeout, 2, 60 * HZ);
1436 xs_format_peer_addresses(xprt);
1437 dprintk("RPC: set up transport to address %s\n",
1438 xs_print_peer_address(xprt, RPC_DISPLAY_ALL));