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/file.h>
34 #include <net/checksum.h>
41 unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
42 unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
44 unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
45 unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
48 * How many times to try sending a request on a socket before waiting
49 * for the socket buffer to clear.
51 #define XS_SENDMSG_RETRY (10U)
54 * Time out for an RPC UDP socket connect. UDP socket connects are
55 * synchronous, but we set a timeout anyway in case of resource
56 * exhaustion on the local host.
58 #define XS_UDP_CONN_TO (5U * HZ)
61 * Wait duration for an RPC TCP connection to be established. Solaris
62 * NFS over TCP uses 60 seconds, for example, which is in line with how
63 * long a server takes to reboot.
65 #define XS_TCP_CONN_TO (60U * HZ)
68 * Wait duration for a reply from the RPC portmapper.
70 #define XS_BIND_TO (60U * HZ)
73 * Delay if a UDP socket connect error occurs. This is most likely some
74 * kind of resource problem on the local host.
76 #define XS_UDP_REEST_TO (2U * HZ)
79 * The reestablish timeout allows clients to delay for a bit before attempting
80 * to reconnect to a server that just dropped our connection.
82 * We implement an exponential backoff when trying to reestablish a TCP
83 * transport connection with the server. Some servers like to drop a TCP
84 * connection when they are overworked, so we start with a short timeout and
85 * increase over time if the server is down or not responding.
87 #define XS_TCP_INIT_REEST_TO (3U * HZ)
88 #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
91 * TCP idle timeout; client drops the transport socket if it is idle
92 * for this long. Note that we also timeout UDP sockets to prevent
93 * holding port numbers when there is no RPC traffic.
95 #define XS_IDLE_DISC_TO (5U * 60 * HZ)
98 # undef RPC_DEBUG_DATA
99 # define RPCDBG_FACILITY RPCDBG_TRANS
102 #ifdef RPC_DEBUG_DATA
103 static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
105 u8 *buf = (u8 *) packet;
108 dprintk("RPC: %s\n", msg);
109 for (j = 0; j < count && j < 128; j += 4) {
113 dprintk("0x%04x ", j);
115 dprintk("%02x%02x%02x%02x ",
116 buf[j], buf[j+1], buf[j+2], buf[j+3]);
121 static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
127 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
129 static inline int xs_send_head(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, unsigned int len)
132 .iov_base = xdr->head[0].iov_base + base,
133 .iov_len = len - base,
135 struct msghdr msg = {
137 .msg_namelen = addrlen,
138 .msg_flags = XS_SENDMSG_FLAGS,
142 msg.msg_flags |= MSG_MORE;
144 if (likely(iov.iov_len))
145 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
146 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
149 static int xs_send_tail(struct socket *sock, struct xdr_buf *xdr, unsigned int base, unsigned int len)
152 .iov_base = xdr->tail[0].iov_base + base,
153 .iov_len = len - base,
155 struct msghdr msg = {
156 .msg_flags = XS_SENDMSG_FLAGS,
159 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
163 * xs_sendpages - write pages directly to a socket
164 * @sock: socket to send on
165 * @addr: UDP only -- address of destination
166 * @addrlen: UDP only -- length of destination address
167 * @xdr: buffer containing this request
168 * @base: starting position in the buffer
171 static inline int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
173 struct page **ppage = xdr->pages;
174 unsigned int len, pglen = xdr->page_len;
176 ssize_t (*sendpage)(struct socket *, struct page *, int, size_t, int);
181 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
183 len = xdr->head[0].iov_len;
184 if (base < len || (addr != NULL && base == 0)) {
185 err = xs_send_head(sock, addr, addrlen, xdr, base, len);
190 if (err != (len - base))
196 if (unlikely(pglen == 0))
198 if (unlikely(base >= pglen)) {
202 if (base || xdr->page_base) {
204 base += xdr->page_base;
205 ppage += base >> PAGE_CACHE_SHIFT;
206 base &= ~PAGE_CACHE_MASK;
209 sendpage = sock->ops->sendpage ? : sock_no_sendpage;
211 int flags = XS_SENDMSG_FLAGS;
213 len = PAGE_CACHE_SIZE;
219 if (pglen != len || xdr->tail[0].iov_len != 0)
222 /* Hmm... We might be dealing with highmem pages */
223 if (PageHighMem(*ppage))
224 sendpage = sock_no_sendpage;
225 err = sendpage(sock, *ppage, base, len, flags);
234 } while ((pglen -= len) != 0);
236 len = xdr->tail[0].iov_len;
238 err = xs_send_tail(sock, xdr, base, len);
249 * xs_nospace - place task on wait queue if transmit was incomplete
250 * @task: task to put to sleep
253 static void xs_nospace(struct rpc_task *task)
255 struct rpc_rqst *req = task->tk_rqstp;
256 struct rpc_xprt *xprt = req->rq_xprt;
258 dprintk("RPC: %4d xmit incomplete (%u left of %u)\n",
259 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
262 if (test_bit(SOCK_ASYNC_NOSPACE, &xprt->sock->flags)) {
263 /* Protect against races with write_space */
264 spin_lock_bh(&xprt->transport_lock);
266 /* Don't race with disconnect */
267 if (!xprt_connected(xprt))
268 task->tk_status = -ENOTCONN;
269 else if (test_bit(SOCK_NOSPACE, &xprt->sock->flags))
270 xprt_wait_for_buffer_space(task);
272 spin_unlock_bh(&xprt->transport_lock);
274 /* Keep holding the socket if it is blocked */
275 rpc_delay(task, HZ>>4);
279 * xs_udp_send_request - write an RPC request to a UDP socket
280 * @task: address of RPC task that manages the state of an RPC request
283 * 0: The request has been sent
284 * EAGAIN: The socket was blocked, please call again later to
285 * complete the request
286 * ENOTCONN: Caller needs to invoke connect logic then call again
287 * other: Some other error occured, the request was not sent
289 static int xs_udp_send_request(struct rpc_task *task)
291 struct rpc_rqst *req = task->tk_rqstp;
292 struct rpc_xprt *xprt = req->rq_xprt;
293 struct xdr_buf *xdr = &req->rq_snd_buf;
296 xs_pktdump("packet data:",
297 req->rq_svec->iov_base,
298 req->rq_svec->iov_len);
300 req->rq_xtime = jiffies;
301 status = xs_sendpages(xprt->sock, (struct sockaddr *) &xprt->addr,
302 sizeof(xprt->addr), xdr, req->rq_bytes_sent);
304 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
305 xdr->len - req->rq_bytes_sent, status);
307 if (likely(status >= (int) req->rq_slen))
310 /* Still some bytes left; set up for a retry later. */
318 /* When the server has died, an ICMP port unreachable message
319 * prompts ECONNREFUSED. */
325 dprintk("RPC: sendmsg returned unrecognized error %d\n",
333 static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
335 u32 reclen = buf->len - sizeof(rpc_fraghdr);
336 rpc_fraghdr *base = buf->head[0].iov_base;
337 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
341 * xs_tcp_send_request - write an RPC request to a TCP socket
342 * @task: address of RPC task that manages the state of an RPC request
345 * 0: The request has been sent
346 * EAGAIN: The socket was blocked, please call again later to
347 * complete the request
348 * ENOTCONN: Caller needs to invoke connect logic then call again
349 * other: Some other error occured, the request was not sent
351 * XXX: In the case of soft timeouts, should we eventually give up
352 * if sendmsg is not able to make progress?
354 static int xs_tcp_send_request(struct rpc_task *task)
356 struct rpc_rqst *req = task->tk_rqstp;
357 struct rpc_xprt *xprt = req->rq_xprt;
358 struct xdr_buf *xdr = &req->rq_snd_buf;
359 int status, retry = 0;
361 xs_encode_tcp_record_marker(&req->rq_snd_buf);
363 xs_pktdump("packet data:",
364 req->rq_svec->iov_base,
365 req->rq_svec->iov_len);
367 /* Continue transmitting the packet/record. We must be careful
368 * to cope with writespace callbacks arriving _after_ we have
369 * called sendmsg(). */
371 req->rq_xtime = jiffies;
372 status = xs_sendpages(xprt->sock, NULL, 0, xdr,
375 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
376 xdr->len - req->rq_bytes_sent, status);
378 if (unlikely(status < 0))
381 /* If we've sent the entire packet, immediately
382 * reset the count of bytes sent. */
383 req->rq_bytes_sent += status;
384 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
385 req->rq_bytes_sent = 0;
390 if (retry++ > XS_SENDMSG_RETRY)
405 dprintk("RPC: sendmsg returned unrecognized error %d\n",
407 xprt_disconnect(xprt);
415 * xs_close - close a socket
418 * This is used when all requests are complete; ie, no DRC state remains
419 * on the server we want to save.
421 static void xs_close(struct rpc_xprt *xprt)
423 struct socket *sock = xprt->sock;
424 struct sock *sk = xprt->inet;
429 dprintk("RPC: xs_close xprt %p\n", xprt);
431 write_lock_bh(&sk->sk_callback_lock);
435 sk->sk_user_data = NULL;
436 sk->sk_data_ready = xprt->old_data_ready;
437 sk->sk_state_change = xprt->old_state_change;
438 sk->sk_write_space = xprt->old_write_space;
439 write_unlock_bh(&sk->sk_callback_lock);
447 * xs_destroy - prepare to shutdown a transport
448 * @xprt: doomed transport
451 static void xs_destroy(struct rpc_xprt *xprt)
453 dprintk("RPC: xs_destroy xprt %p\n", xprt);
455 cancel_delayed_work(&xprt->connect_worker);
456 flush_scheduled_work();
458 xprt_disconnect(xprt);
463 static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
465 return (struct rpc_xprt *) sk->sk_user_data;
469 * xs_udp_data_ready - "data ready" callback for UDP sockets
470 * @sk: socket with data to read
471 * @len: how much data to read
474 static void xs_udp_data_ready(struct sock *sk, int len)
476 struct rpc_task *task;
477 struct rpc_xprt *xprt;
478 struct rpc_rqst *rovr;
480 int err, repsize, copied;
483 read_lock(&sk->sk_callback_lock);
484 dprintk("RPC: xs_udp_data_ready...\n");
485 if (!(xprt = xprt_from_sock(sk)))
488 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
494 repsize = skb->len - sizeof(struct udphdr);
496 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
500 /* Copy the XID from the skb... */
501 xp = skb_header_pointer(skb, sizeof(struct udphdr),
502 sizeof(_xid), &_xid);
506 /* Look up and lock the request corresponding to the given XID */
507 spin_lock(&xprt->transport_lock);
508 rovr = xprt_lookup_rqst(xprt, *xp);
511 task = rovr->rq_task;
513 if ((copied = rovr->rq_private_buf.buflen) > repsize)
516 /* Suck it into the iovec, verify checksum if not done by hw. */
517 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb))
520 /* Something worked... */
521 dst_confirm(skb->dst);
523 xprt_adjust_cwnd(task, copied);
524 xprt_update_rtt(task);
525 xprt_complete_rqst(task, copied);
528 spin_unlock(&xprt->transport_lock);
530 skb_free_datagram(sk, skb);
532 read_unlock(&sk->sk_callback_lock);
535 static inline size_t xs_tcp_copy_data(skb_reader_t *desc, void *p, size_t len)
537 if (len > desc->count)
539 if (skb_copy_bits(desc->skb, desc->offset, p, len)) {
540 dprintk("RPC: failed to copy %zu bytes from skb. %zu bytes remain\n",
546 dprintk("RPC: copied %zu bytes from skb. %zu bytes remain\n",
551 static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, skb_reader_t *desc)
556 p = ((char *) &xprt->tcp_recm) + xprt->tcp_offset;
557 len = sizeof(xprt->tcp_recm) - xprt->tcp_offset;
558 used = xs_tcp_copy_data(desc, p, len);
559 xprt->tcp_offset += used;
563 xprt->tcp_reclen = ntohl(xprt->tcp_recm);
564 if (xprt->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
565 xprt->tcp_flags |= XPRT_LAST_FRAG;
567 xprt->tcp_flags &= ~XPRT_LAST_FRAG;
568 xprt->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
570 xprt->tcp_flags &= ~XPRT_COPY_RECM;
571 xprt->tcp_offset = 0;
573 /* Sanity check of the record length */
574 if (unlikely(xprt->tcp_reclen < 4)) {
575 dprintk("RPC: invalid TCP record fragment length\n");
576 xprt_disconnect(xprt);
579 dprintk("RPC: reading TCP record fragment of length %d\n",
583 static void xs_tcp_check_recm(struct rpc_xprt *xprt)
585 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u, tcp_flags = %lx\n",
586 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen, xprt->tcp_flags);
587 if (xprt->tcp_offset == xprt->tcp_reclen) {
588 xprt->tcp_flags |= XPRT_COPY_RECM;
589 xprt->tcp_offset = 0;
590 if (xprt->tcp_flags & XPRT_LAST_FRAG) {
591 xprt->tcp_flags &= ~XPRT_COPY_DATA;
592 xprt->tcp_flags |= XPRT_COPY_XID;
593 xprt->tcp_copied = 0;
598 static inline void xs_tcp_read_xid(struct rpc_xprt *xprt, skb_reader_t *desc)
603 len = sizeof(xprt->tcp_xid) - xprt->tcp_offset;
604 dprintk("RPC: reading XID (%Zu bytes)\n", len);
605 p = ((char *) &xprt->tcp_xid) + xprt->tcp_offset;
606 used = xs_tcp_copy_data(desc, p, len);
607 xprt->tcp_offset += used;
610 xprt->tcp_flags &= ~XPRT_COPY_XID;
611 xprt->tcp_flags |= XPRT_COPY_DATA;
612 xprt->tcp_copied = 4;
613 dprintk("RPC: reading reply for XID %08x\n",
614 ntohl(xprt->tcp_xid));
615 xs_tcp_check_recm(xprt);
618 static inline void xs_tcp_read_request(struct rpc_xprt *xprt, skb_reader_t *desc)
620 struct rpc_rqst *req;
621 struct xdr_buf *rcvbuf;
625 /* Find and lock the request corresponding to this xid */
626 spin_lock(&xprt->transport_lock);
627 req = xprt_lookup_rqst(xprt, xprt->tcp_xid);
629 xprt->tcp_flags &= ~XPRT_COPY_DATA;
630 dprintk("RPC: XID %08x request not found!\n",
631 ntohl(xprt->tcp_xid));
632 spin_unlock(&xprt->transport_lock);
636 rcvbuf = &req->rq_private_buf;
638 if (len > xprt->tcp_reclen - xprt->tcp_offset) {
639 skb_reader_t my_desc;
641 len = xprt->tcp_reclen - xprt->tcp_offset;
642 memcpy(&my_desc, desc, sizeof(my_desc));
644 r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied,
645 &my_desc, xs_tcp_copy_data);
649 r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied,
650 desc, xs_tcp_copy_data);
653 xprt->tcp_copied += r;
654 xprt->tcp_offset += r;
657 /* Error when copying to the receive buffer,
658 * usually because we weren't able to allocate
659 * additional buffer pages. All we can do now
660 * is turn off XPRT_COPY_DATA, so the request
661 * will not receive any additional updates,
663 * Any remaining data from this record will
666 xprt->tcp_flags &= ~XPRT_COPY_DATA;
667 dprintk("RPC: XID %08x truncated request\n",
668 ntohl(xprt->tcp_xid));
669 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
670 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen);
674 dprintk("RPC: XID %08x read %Zd bytes\n",
675 ntohl(xprt->tcp_xid), r);
676 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
677 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen);
679 if (xprt->tcp_copied == req->rq_private_buf.buflen)
680 xprt->tcp_flags &= ~XPRT_COPY_DATA;
681 else if (xprt->tcp_offset == xprt->tcp_reclen) {
682 if (xprt->tcp_flags & XPRT_LAST_FRAG)
683 xprt->tcp_flags &= ~XPRT_COPY_DATA;
687 if (!(xprt->tcp_flags & XPRT_COPY_DATA))
688 xprt_complete_rqst(req->rq_task, xprt->tcp_copied);
689 spin_unlock(&xprt->transport_lock);
690 xs_tcp_check_recm(xprt);
693 static inline void xs_tcp_read_discard(struct rpc_xprt *xprt, skb_reader_t *desc)
697 len = xprt->tcp_reclen - xprt->tcp_offset;
698 if (len > desc->count)
702 xprt->tcp_offset += len;
703 dprintk("RPC: discarded %Zu bytes\n", len);
704 xs_tcp_check_recm(xprt);
707 static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
709 struct rpc_xprt *xprt = rd_desc->arg.data;
710 skb_reader_t desc = {
717 dprintk("RPC: xs_tcp_data_recv started\n");
719 /* Read in a new fragment marker if necessary */
720 /* Can we ever really expect to get completely empty fragments? */
721 if (xprt->tcp_flags & XPRT_COPY_RECM) {
722 xs_tcp_read_fraghdr(xprt, &desc);
725 /* Read in the xid if necessary */
726 if (xprt->tcp_flags & XPRT_COPY_XID) {
727 xs_tcp_read_xid(xprt, &desc);
730 /* Read in the request data */
731 if (xprt->tcp_flags & XPRT_COPY_DATA) {
732 xs_tcp_read_request(xprt, &desc);
735 /* Skip over any trailing bytes on short reads */
736 xs_tcp_read_discard(xprt, &desc);
737 } while (desc.count);
738 dprintk("RPC: xs_tcp_data_recv done\n");
739 return len - desc.count;
743 * xs_tcp_data_ready - "data ready" callback for TCP sockets
744 * @sk: socket with data to read
745 * @bytes: how much data to read
748 static void xs_tcp_data_ready(struct sock *sk, int bytes)
750 struct rpc_xprt *xprt;
751 read_descriptor_t rd_desc;
753 read_lock(&sk->sk_callback_lock);
754 dprintk("RPC: xs_tcp_data_ready...\n");
755 if (!(xprt = xprt_from_sock(sk)))
760 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
761 rd_desc.arg.data = xprt;
762 rd_desc.count = 65536;
763 tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
765 read_unlock(&sk->sk_callback_lock);
769 * xs_tcp_state_change - callback to handle TCP socket state changes
770 * @sk: socket whose state has changed
773 static void xs_tcp_state_change(struct sock *sk)
775 struct rpc_xprt *xprt;
777 read_lock(&sk->sk_callback_lock);
778 if (!(xprt = xprt_from_sock(sk)))
780 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
781 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
782 sk->sk_state, xprt_connected(xprt),
783 sock_flag(sk, SOCK_DEAD),
784 sock_flag(sk, SOCK_ZAPPED));
786 switch (sk->sk_state) {
787 case TCP_ESTABLISHED:
788 spin_lock_bh(&xprt->transport_lock);
789 if (!xprt_test_and_set_connected(xprt)) {
790 /* Reset TCP record info */
791 xprt->tcp_offset = 0;
792 xprt->tcp_reclen = 0;
793 xprt->tcp_copied = 0;
794 xprt->tcp_flags = XPRT_COPY_RECM | XPRT_COPY_XID;
795 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
796 xprt_wake_pending_tasks(xprt, 0);
798 spin_unlock_bh(&xprt->transport_lock);
804 xprt_disconnect(xprt);
808 read_unlock(&sk->sk_callback_lock);
812 * xs_udp_write_space - callback invoked when socket buffer space
814 * @sk: socket whose state has changed
816 * Called when more output buffer space is available for this socket.
817 * We try not to wake our writers until they can make "significant"
818 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
819 * with a bunch of small requests.
821 static void xs_udp_write_space(struct sock *sk)
823 read_lock(&sk->sk_callback_lock);
825 /* from net/core/sock.c:sock_def_write_space */
826 if (sock_writeable(sk)) {
828 struct rpc_xprt *xprt;
830 if (unlikely(!(sock = sk->sk_socket)))
832 if (unlikely(!(xprt = xprt_from_sock(sk))))
834 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
837 xprt_write_space(xprt);
841 read_unlock(&sk->sk_callback_lock);
845 * xs_tcp_write_space - callback invoked when socket buffer space
847 * @sk: socket whose state has changed
849 * Called when more output buffer space is available for this socket.
850 * We try not to wake our writers until they can make "significant"
851 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
852 * with a bunch of small requests.
854 static void xs_tcp_write_space(struct sock *sk)
856 read_lock(&sk->sk_callback_lock);
858 /* from net/core/stream.c:sk_stream_write_space */
859 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
861 struct rpc_xprt *xprt;
863 if (unlikely(!(sock = sk->sk_socket)))
865 if (unlikely(!(xprt = xprt_from_sock(sk))))
867 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
870 xprt_write_space(xprt);
874 read_unlock(&sk->sk_callback_lock);
877 static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
879 struct sock *sk = xprt->inet;
882 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
883 sk->sk_rcvbuf = xprt->rcvsize * xprt->max_reqs * 2;
886 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
887 sk->sk_sndbuf = xprt->sndsize * xprt->max_reqs * 2;
888 sk->sk_write_space(sk);
893 * xs_udp_set_buffer_size - set send and receive limits
894 * @xprt: generic transport
895 * @sndsize: requested size of send buffer, in bytes
896 * @rcvsize: requested size of receive buffer, in bytes
898 * Set socket send and receive buffer size limits.
900 static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
904 xprt->sndsize = sndsize + 1024;
907 xprt->rcvsize = rcvsize + 1024;
909 xs_udp_do_set_buffer_size(xprt);
913 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
914 * @task: task that timed out
916 * Adjust the congestion window after a retransmit timeout has occurred.
918 static void xs_udp_timer(struct rpc_task *task)
920 xprt_adjust_cwnd(task, -ETIMEDOUT);
923 static int xs_bindresvport(struct rpc_xprt *xprt, struct socket *sock)
925 struct sockaddr_in myaddr = {
926 .sin_family = AF_INET,
929 unsigned short port = xprt->port;
932 myaddr.sin_port = htons(port);
933 err = sock->ops->bind(sock, (struct sockaddr *) &myaddr,
937 dprintk("RPC: xs_bindresvport bound to port %u\n",
941 if (port <= xprt_min_resvport)
942 port = xprt_max_resvport;
945 } while (err == -EADDRINUSE && port != xprt->port);
947 dprintk("RPC: can't bind to reserved port (%d).\n", -err);
952 * xs_udp_connect_worker - set up a UDP socket
953 * @args: RPC transport to connect
955 * Invoked by a work queue tasklet.
957 static void xs_udp_connect_worker(void *args)
959 struct rpc_xprt *xprt = (struct rpc_xprt *) args;
960 struct socket *sock = xprt->sock;
961 int err, status = -EIO;
963 if (xprt->shutdown || xprt->addr.sin_port == 0)
966 dprintk("RPC: xs_udp_connect_worker for xprt %p\n", xprt);
968 /* Start by resetting any existing state */
971 if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
972 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
976 if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) {
982 struct sock *sk = sock->sk;
984 write_lock_bh(&sk->sk_callback_lock);
986 sk->sk_user_data = xprt;
987 xprt->old_data_ready = sk->sk_data_ready;
988 xprt->old_state_change = sk->sk_state_change;
989 xprt->old_write_space = sk->sk_write_space;
990 sk->sk_data_ready = xs_udp_data_ready;
991 sk->sk_write_space = xs_udp_write_space;
992 sk->sk_no_check = UDP_CSUM_NORCV;
993 sk->sk_allocation = GFP_ATOMIC;
995 xprt_set_connected(xprt);
997 /* Reset to new socket */
1001 write_unlock_bh(&sk->sk_callback_lock);
1003 xs_udp_do_set_buffer_size(xprt);
1006 xprt_wake_pending_tasks(xprt, status);
1007 xprt_clear_connecting(xprt);
1011 * We need to preserve the port number so the reply cache on the server can
1012 * find our cached RPC replies when we get around to reconnecting.
1014 static void xs_tcp_reuse_connection(struct rpc_xprt *xprt)
1017 struct socket *sock = xprt->sock;
1018 struct sockaddr any;
1020 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
1023 * Disconnect the transport socket by doing a connect operation
1024 * with AF_UNSPEC. This should return immediately...
1026 memset(&any, 0, sizeof(any));
1027 any.sa_family = AF_UNSPEC;
1028 result = sock->ops->connect(sock, &any, sizeof(any), 0);
1030 dprintk("RPC: AF_UNSPEC connect return code %d\n",
1035 * xs_tcp_connect_worker - connect a TCP socket to a remote endpoint
1036 * @args: RPC transport to connect
1038 * Invoked by a work queue tasklet.
1040 static void xs_tcp_connect_worker(void *args)
1042 struct rpc_xprt *xprt = (struct rpc_xprt *)args;
1043 struct socket *sock = xprt->sock;
1044 int err, status = -EIO;
1046 if (xprt->shutdown || xprt->addr.sin_port == 0)
1049 dprintk("RPC: xs_tcp_connect_worker for xprt %p\n", xprt);
1052 /* start from scratch */
1053 if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
1054 dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
1058 if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) {
1063 /* "close" the socket, preserving the local port */
1064 xs_tcp_reuse_connection(xprt);
1067 struct sock *sk = sock->sk;
1069 write_lock_bh(&sk->sk_callback_lock);
1071 sk->sk_user_data = xprt;
1072 xprt->old_data_ready = sk->sk_data_ready;
1073 xprt->old_state_change = sk->sk_state_change;
1074 xprt->old_write_space = sk->sk_write_space;
1075 sk->sk_data_ready = xs_tcp_data_ready;
1076 sk->sk_state_change = xs_tcp_state_change;
1077 sk->sk_write_space = xs_tcp_write_space;
1078 sk->sk_allocation = GFP_ATOMIC;
1080 /* socket options */
1081 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1082 sock_reset_flag(sk, SOCK_LINGER);
1083 tcp_sk(sk)->linger2 = 0;
1084 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
1086 xprt_clear_connected(xprt);
1088 /* Reset to new socket */
1092 write_unlock_bh(&sk->sk_callback_lock);
1095 /* Tell the socket layer to start connecting... */
1096 status = sock->ops->connect(sock, (struct sockaddr *) &xprt->addr,
1097 sizeof(xprt->addr), O_NONBLOCK);
1098 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1099 xprt, -status, xprt_connected(xprt), sock->sk->sk_state);
1107 /* retry with existing socket, after a delay */
1110 /* get rid of existing socket, and retry */
1116 xprt_wake_pending_tasks(xprt, status);
1118 xprt_clear_connecting(xprt);
1122 * xs_connect - connect a socket to a remote endpoint
1123 * @task: address of RPC task that manages state of connect request
1125 * TCP: If the remote end dropped the connection, delay reconnecting.
1127 * UDP socket connects are synchronous, but we use a work queue anyway
1128 * to guarantee that even unprivileged user processes can set up a
1129 * socket on a privileged port.
1131 * If a UDP socket connect fails, the delay behavior here prevents
1132 * retry floods (hard mounts).
1134 static void xs_connect(struct rpc_task *task)
1136 struct rpc_xprt *xprt = task->tk_xprt;
1138 if (xprt_test_and_set_connecting(xprt))
1141 if (xprt->sock != NULL) {
1142 dprintk("RPC: xs_connect delayed xprt %p for %lu seconds\n",
1143 xprt, xprt->reestablish_timeout / HZ);
1144 schedule_delayed_work(&xprt->connect_worker,
1145 xprt->reestablish_timeout);
1146 xprt->reestablish_timeout <<= 1;
1147 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
1148 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
1150 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
1151 schedule_work(&xprt->connect_worker);
1153 /* flush_scheduled_work can sleep... */
1154 if (!RPC_IS_ASYNC(task))
1155 flush_scheduled_work();
1159 static struct rpc_xprt_ops xs_udp_ops = {
1160 .set_buffer_size = xs_udp_set_buffer_size,
1161 .reserve_xprt = xprt_reserve_xprt_cong,
1162 .release_xprt = xprt_release_xprt_cong,
1163 .connect = xs_connect,
1164 .send_request = xs_udp_send_request,
1165 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
1166 .timer = xs_udp_timer,
1167 .release_request = xprt_release_rqst_cong,
1169 .destroy = xs_destroy,
1172 static struct rpc_xprt_ops xs_tcp_ops = {
1173 .reserve_xprt = xprt_reserve_xprt,
1174 .release_xprt = xprt_release_xprt,
1175 .connect = xs_connect,
1176 .send_request = xs_tcp_send_request,
1177 .set_retrans_timeout = xprt_set_retrans_timeout_def,
1179 .destroy = xs_destroy,
1183 * xs_setup_udp - Set up transport to use a UDP socket
1184 * @xprt: transport to set up
1185 * @to: timeout parameters
1188 int xs_setup_udp(struct rpc_xprt *xprt, struct rpc_timeout *to)
1190 size_t slot_table_size;
1192 dprintk("RPC: setting up udp-ipv4 transport...\n");
1194 xprt->max_reqs = xprt_udp_slot_table_entries;
1195 slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]);
1196 xprt->slot = kmalloc(slot_table_size, GFP_KERNEL);
1197 if (xprt->slot == NULL)
1199 memset(xprt->slot, 0, slot_table_size);
1201 xprt->prot = IPPROTO_UDP;
1202 xprt->port = xprt_max_resvport;
1204 xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0;
1205 /* XXX: header size can vary due to auth type, IPv6, etc. */
1206 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
1208 INIT_WORK(&xprt->connect_worker, xs_udp_connect_worker, xprt);
1209 xprt->bind_timeout = XS_BIND_TO;
1210 xprt->connect_timeout = XS_UDP_CONN_TO;
1211 xprt->reestablish_timeout = XS_UDP_REEST_TO;
1212 xprt->idle_timeout = XS_IDLE_DISC_TO;
1214 xprt->ops = &xs_udp_ops;
1217 xprt->timeout = *to;
1219 xprt_set_timeout(&xprt->timeout, 5, 5 * HZ);
1225 * xs_setup_tcp - Set up transport to use a TCP socket
1226 * @xprt: transport to set up
1227 * @to: timeout parameters
1230 int xs_setup_tcp(struct rpc_xprt *xprt, struct rpc_timeout *to)
1232 size_t slot_table_size;
1234 dprintk("RPC: setting up tcp-ipv4 transport...\n");
1236 xprt->max_reqs = xprt_tcp_slot_table_entries;
1237 slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]);
1238 xprt->slot = kmalloc(slot_table_size, GFP_KERNEL);
1239 if (xprt->slot == NULL)
1241 memset(xprt->slot, 0, slot_table_size);
1243 xprt->prot = IPPROTO_TCP;
1244 xprt->port = xprt_max_resvport;
1245 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
1246 xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0;
1247 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
1249 INIT_WORK(&xprt->connect_worker, xs_tcp_connect_worker, xprt);
1250 xprt->bind_timeout = XS_BIND_TO;
1251 xprt->connect_timeout = XS_TCP_CONN_TO;
1252 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1253 xprt->idle_timeout = XS_IDLE_DISC_TO;
1255 xprt->ops = &xs_tcp_ops;
1258 xprt->timeout = *to;
1260 xprt_set_timeout(&xprt->timeout, 2, 60 * HZ);