Linux 3.12.28
[linux/fpc-iii.git] / net / sunrpc / xprtsock.c
blob1d034825fcc3cc5f21690b0888316109db49f5a9
1 /*
2 * linux/net/sunrpc/xprtsock.c
4 * Client-side transport implementation for sockets.
6 * TCP callback races fixes (C) 1998 Red Hat
7 * TCP send fixes (C) 1998 Red Hat
8 * TCP NFS related read + write fixes
9 * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
11 * Rewrite of larges part of the code in order to stabilize TCP stuff.
12 * Fix behaviour when socket buffer is full.
13 * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
15 * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
17 * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
18 * <gilles.quillard@bull.net>
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/capability.h>
26 #include <linux/pagemap.h>
27 #include <linux/errno.h>
28 #include <linux/socket.h>
29 #include <linux/in.h>
30 #include <linux/net.h>
31 #include <linux/mm.h>
32 #include <linux/un.h>
33 #include <linux/udp.h>
34 #include <linux/tcp.h>
35 #include <linux/sunrpc/clnt.h>
36 #include <linux/sunrpc/addr.h>
37 #include <linux/sunrpc/sched.h>
38 #include <linux/sunrpc/svcsock.h>
39 #include <linux/sunrpc/xprtsock.h>
40 #include <linux/file.h>
41 #ifdef CONFIG_SUNRPC_BACKCHANNEL
42 #include <linux/sunrpc/bc_xprt.h>
43 #endif
45 #include <net/sock.h>
46 #include <net/checksum.h>
47 #include <net/udp.h>
48 #include <net/tcp.h>
50 #include <trace/events/sunrpc.h>
52 #include "sunrpc.h"
54 static void xs_close(struct rpc_xprt *xprt);
57 * xprtsock tunables
59 static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
60 static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
61 static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
63 static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
64 static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
66 #define XS_TCP_LINGER_TO (15U * HZ)
67 static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
70 * We can register our own files under /proc/sys/sunrpc by
71 * calling register_sysctl_table() again. The files in that
72 * directory become the union of all files registered there.
74 * We simply need to make sure that we don't collide with
75 * someone else's file names!
78 #ifdef RPC_DEBUG
80 static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
81 static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
82 static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
83 static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
84 static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
86 static struct ctl_table_header *sunrpc_table_header;
89 * FIXME: changing the UDP slot table size should also resize the UDP
90 * socket buffers for existing UDP transports
92 static struct ctl_table xs_tunables_table[] = {
94 .procname = "udp_slot_table_entries",
95 .data = &xprt_udp_slot_table_entries,
96 .maxlen = sizeof(unsigned int),
97 .mode = 0644,
98 .proc_handler = proc_dointvec_minmax,
99 .extra1 = &min_slot_table_size,
100 .extra2 = &max_slot_table_size
103 .procname = "tcp_slot_table_entries",
104 .data = &xprt_tcp_slot_table_entries,
105 .maxlen = sizeof(unsigned int),
106 .mode = 0644,
107 .proc_handler = proc_dointvec_minmax,
108 .extra1 = &min_slot_table_size,
109 .extra2 = &max_slot_table_size
112 .procname = "tcp_max_slot_table_entries",
113 .data = &xprt_max_tcp_slot_table_entries,
114 .maxlen = sizeof(unsigned int),
115 .mode = 0644,
116 .proc_handler = proc_dointvec_minmax,
117 .extra1 = &min_slot_table_size,
118 .extra2 = &max_tcp_slot_table_limit
121 .procname = "min_resvport",
122 .data = &xprt_min_resvport,
123 .maxlen = sizeof(unsigned int),
124 .mode = 0644,
125 .proc_handler = proc_dointvec_minmax,
126 .extra1 = &xprt_min_resvport_limit,
127 .extra2 = &xprt_max_resvport_limit
130 .procname = "max_resvport",
131 .data = &xprt_max_resvport,
132 .maxlen = sizeof(unsigned int),
133 .mode = 0644,
134 .proc_handler = proc_dointvec_minmax,
135 .extra1 = &xprt_min_resvport_limit,
136 .extra2 = &xprt_max_resvport_limit
139 .procname = "tcp_fin_timeout",
140 .data = &xs_tcp_fin_timeout,
141 .maxlen = sizeof(xs_tcp_fin_timeout),
142 .mode = 0644,
143 .proc_handler = proc_dointvec_jiffies,
145 { },
148 static struct ctl_table sunrpc_table[] = {
150 .procname = "sunrpc",
151 .mode = 0555,
152 .child = xs_tunables_table
154 { },
157 #endif
160 * Wait duration for a reply from the RPC portmapper.
162 #define XS_BIND_TO (60U * HZ)
165 * Delay if a UDP socket connect error occurs. This is most likely some
166 * kind of resource problem on the local host.
168 #define XS_UDP_REEST_TO (2U * HZ)
171 * The reestablish timeout allows clients to delay for a bit before attempting
172 * to reconnect to a server that just dropped our connection.
174 * We implement an exponential backoff when trying to reestablish a TCP
175 * transport connection with the server. Some servers like to drop a TCP
176 * connection when they are overworked, so we start with a short timeout and
177 * increase over time if the server is down or not responding.
179 #define XS_TCP_INIT_REEST_TO (3U * HZ)
180 #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
183 * TCP idle timeout; client drops the transport socket if it is idle
184 * for this long. Note that we also timeout UDP sockets to prevent
185 * holding port numbers when there is no RPC traffic.
187 #define XS_IDLE_DISC_TO (5U * 60 * HZ)
189 #ifdef RPC_DEBUG
190 # undef RPC_DEBUG_DATA
191 # define RPCDBG_FACILITY RPCDBG_TRANS
192 #endif
194 #ifdef RPC_DEBUG_DATA
195 static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
197 u8 *buf = (u8 *) packet;
198 int j;
200 dprintk("RPC: %s\n", msg);
201 for (j = 0; j < count && j < 128; j += 4) {
202 if (!(j & 31)) {
203 if (j)
204 dprintk("\n");
205 dprintk("0x%04x ", j);
207 dprintk("%02x%02x%02x%02x ",
208 buf[j], buf[j+1], buf[j+2], buf[j+3]);
210 dprintk("\n");
212 #else
213 static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
215 /* NOP */
217 #endif
219 struct sock_xprt {
220 struct rpc_xprt xprt;
223 * Network layer
225 struct socket * sock;
226 struct sock * inet;
229 * State of TCP reply receive
231 __be32 tcp_fraghdr,
232 tcp_xid,
233 tcp_calldir;
235 u32 tcp_offset,
236 tcp_reclen;
238 unsigned long tcp_copied,
239 tcp_flags;
242 * Connection of transports
244 struct delayed_work connect_worker;
245 struct sockaddr_storage srcaddr;
246 unsigned short srcport;
249 * UDP socket buffer size parameters
251 size_t rcvsize,
252 sndsize;
255 * Saved socket callback addresses
257 void (*old_data_ready)(struct sock *, int);
258 void (*old_state_change)(struct sock *);
259 void (*old_write_space)(struct sock *);
263 * TCP receive state flags
265 #define TCP_RCV_LAST_FRAG (1UL << 0)
266 #define TCP_RCV_COPY_FRAGHDR (1UL << 1)
267 #define TCP_RCV_COPY_XID (1UL << 2)
268 #define TCP_RCV_COPY_DATA (1UL << 3)
269 #define TCP_RCV_READ_CALLDIR (1UL << 4)
270 #define TCP_RCV_COPY_CALLDIR (1UL << 5)
273 * TCP RPC flags
275 #define TCP_RPC_REPLY (1UL << 6)
277 static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
279 return (struct sockaddr *) &xprt->addr;
282 static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
284 return (struct sockaddr_un *) &xprt->addr;
287 static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
289 return (struct sockaddr_in *) &xprt->addr;
292 static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
294 return (struct sockaddr_in6 *) &xprt->addr;
297 static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
299 struct sockaddr *sap = xs_addr(xprt);
300 struct sockaddr_in6 *sin6;
301 struct sockaddr_in *sin;
302 struct sockaddr_un *sun;
303 char buf[128];
305 switch (sap->sa_family) {
306 case AF_LOCAL:
307 sun = xs_addr_un(xprt);
308 strlcpy(buf, sun->sun_path, sizeof(buf));
309 xprt->address_strings[RPC_DISPLAY_ADDR] =
310 kstrdup(buf, GFP_KERNEL);
311 break;
312 case AF_INET:
313 (void)rpc_ntop(sap, buf, sizeof(buf));
314 xprt->address_strings[RPC_DISPLAY_ADDR] =
315 kstrdup(buf, GFP_KERNEL);
316 sin = xs_addr_in(xprt);
317 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
318 break;
319 case AF_INET6:
320 (void)rpc_ntop(sap, buf, sizeof(buf));
321 xprt->address_strings[RPC_DISPLAY_ADDR] =
322 kstrdup(buf, GFP_KERNEL);
323 sin6 = xs_addr_in6(xprt);
324 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
325 break;
326 default:
327 BUG();
330 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
333 static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
335 struct sockaddr *sap = xs_addr(xprt);
336 char buf[128];
338 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
339 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
341 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
342 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
345 static void xs_format_peer_addresses(struct rpc_xprt *xprt,
346 const char *protocol,
347 const char *netid)
349 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
350 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
351 xs_format_common_peer_addresses(xprt);
352 xs_format_common_peer_ports(xprt);
355 static void xs_update_peer_port(struct rpc_xprt *xprt)
357 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
358 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
360 xs_format_common_peer_ports(xprt);
363 static void xs_free_peer_addresses(struct rpc_xprt *xprt)
365 unsigned int i;
367 for (i = 0; i < RPC_DISPLAY_MAX; i++)
368 switch (i) {
369 case RPC_DISPLAY_PROTO:
370 case RPC_DISPLAY_NETID:
371 continue;
372 default:
373 kfree(xprt->address_strings[i]);
377 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
379 static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
381 struct msghdr msg = {
382 .msg_name = addr,
383 .msg_namelen = addrlen,
384 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
386 struct kvec iov = {
387 .iov_base = vec->iov_base + base,
388 .iov_len = vec->iov_len - base,
391 if (iov.iov_len != 0)
392 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
393 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
396 static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more, bool zerocopy)
398 ssize_t (*do_sendpage)(struct socket *sock, struct page *page,
399 int offset, size_t size, int flags);
400 struct page **ppage;
401 unsigned int remainder;
402 int err, sent = 0;
404 remainder = xdr->page_len - base;
405 base += xdr->page_base;
406 ppage = xdr->pages + (base >> PAGE_SHIFT);
407 base &= ~PAGE_MASK;
408 do_sendpage = sock->ops->sendpage;
409 if (!zerocopy)
410 do_sendpage = sock_no_sendpage;
411 for(;;) {
412 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
413 int flags = XS_SENDMSG_FLAGS;
415 remainder -= len;
416 if (remainder != 0 || more)
417 flags |= MSG_MORE;
418 err = do_sendpage(sock, *ppage, base, len, flags);
419 if (remainder == 0 || err != len)
420 break;
421 sent += err;
422 ppage++;
423 base = 0;
425 if (sent == 0)
426 return err;
427 if (err > 0)
428 sent += err;
429 return sent;
433 * xs_sendpages - write pages directly to a socket
434 * @sock: socket to send on
435 * @addr: UDP only -- address of destination
436 * @addrlen: UDP only -- length of destination address
437 * @xdr: buffer containing this request
438 * @base: starting position in the buffer
439 * @zerocopy: true if it is safe to use sendpage()
442 static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, bool zerocopy)
444 unsigned int remainder = xdr->len - base;
445 int err, sent = 0;
447 if (unlikely(!sock))
448 return -ENOTSOCK;
450 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
451 if (base != 0) {
452 addr = NULL;
453 addrlen = 0;
456 if (base < xdr->head[0].iov_len || addr != NULL) {
457 unsigned int len = xdr->head[0].iov_len - base;
458 remainder -= len;
459 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
460 if (remainder == 0 || err != len)
461 goto out;
462 sent += err;
463 base = 0;
464 } else
465 base -= xdr->head[0].iov_len;
467 if (base < xdr->page_len) {
468 unsigned int len = xdr->page_len - base;
469 remainder -= len;
470 err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy);
471 if (remainder == 0 || err != len)
472 goto out;
473 sent += err;
474 base = 0;
475 } else
476 base -= xdr->page_len;
478 if (base >= xdr->tail[0].iov_len)
479 return sent;
480 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
481 out:
482 if (sent == 0)
483 return err;
484 if (err > 0)
485 sent += err;
486 return sent;
489 static void xs_nospace_callback(struct rpc_task *task)
491 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
493 transport->inet->sk_write_pending--;
494 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
498 * xs_nospace - place task on wait queue if transmit was incomplete
499 * @task: task to put to sleep
502 static int xs_nospace(struct rpc_task *task)
504 struct rpc_rqst *req = task->tk_rqstp;
505 struct rpc_xprt *xprt = req->rq_xprt;
506 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
507 struct sock *sk = transport->inet;
508 int ret = -EAGAIN;
510 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
511 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
512 req->rq_slen);
514 /* Protect against races with write_space */
515 spin_lock_bh(&xprt->transport_lock);
517 /* Don't race with disconnect */
518 if (xprt_connected(xprt)) {
519 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
521 * Notify TCP that we're limited by the application
522 * window size
524 set_bit(SOCK_NOSPACE, &transport->sock->flags);
525 sk->sk_write_pending++;
526 /* ...and wait for more buffer space */
527 xprt_wait_for_buffer_space(task, xs_nospace_callback);
529 } else {
530 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
531 ret = -ENOTCONN;
534 spin_unlock_bh(&xprt->transport_lock);
536 /* Race breaker in case memory is freed before above code is called */
537 sk->sk_write_space(sk);
538 return ret;
542 * Construct a stream transport record marker in @buf.
544 static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
546 u32 reclen = buf->len - sizeof(rpc_fraghdr);
547 rpc_fraghdr *base = buf->head[0].iov_base;
548 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
552 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
553 * @task: RPC task that manages the state of an RPC request
555 * Return values:
556 * 0: The request has been sent
557 * EAGAIN: The socket was blocked, please call again later to
558 * complete the request
559 * ENOTCONN: Caller needs to invoke connect logic then call again
560 * other: Some other error occured, the request was not sent
562 static int xs_local_send_request(struct rpc_task *task)
564 struct rpc_rqst *req = task->tk_rqstp;
565 struct rpc_xprt *xprt = req->rq_xprt;
566 struct sock_xprt *transport =
567 container_of(xprt, struct sock_xprt, xprt);
568 struct xdr_buf *xdr = &req->rq_snd_buf;
569 int status;
571 xs_encode_stream_record_marker(&req->rq_snd_buf);
573 xs_pktdump("packet data:",
574 req->rq_svec->iov_base, req->rq_svec->iov_len);
576 status = xs_sendpages(transport->sock, NULL, 0,
577 xdr, req->rq_bytes_sent, true);
578 dprintk("RPC: %s(%u) = %d\n",
579 __func__, xdr->len - req->rq_bytes_sent, status);
580 if (likely(status >= 0)) {
581 req->rq_bytes_sent += status;
582 req->rq_xmit_bytes_sent += status;
583 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
584 req->rq_bytes_sent = 0;
585 return 0;
587 status = -EAGAIN;
590 switch (status) {
591 case -EAGAIN:
592 status = xs_nospace(task);
593 break;
594 default:
595 dprintk("RPC: sendmsg returned unrecognized error %d\n",
596 -status);
597 case -EPIPE:
598 xs_close(xprt);
599 status = -ENOTCONN;
602 return status;
606 * xs_udp_send_request - write an RPC request to a UDP socket
607 * @task: address of RPC task that manages the state of an RPC request
609 * Return values:
610 * 0: The request has been sent
611 * EAGAIN: The socket was blocked, please call again later to
612 * complete the request
613 * ENOTCONN: Caller needs to invoke connect logic then call again
614 * other: Some other error occurred, the request was not sent
616 static int xs_udp_send_request(struct rpc_task *task)
618 struct rpc_rqst *req = task->tk_rqstp;
619 struct rpc_xprt *xprt = req->rq_xprt;
620 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
621 struct xdr_buf *xdr = &req->rq_snd_buf;
622 int status;
624 xs_pktdump("packet data:",
625 req->rq_svec->iov_base,
626 req->rq_svec->iov_len);
628 if (!xprt_bound(xprt))
629 return -ENOTCONN;
630 status = xs_sendpages(transport->sock,
631 xs_addr(xprt),
632 xprt->addrlen, xdr,
633 req->rq_bytes_sent, true);
635 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
636 xdr->len - req->rq_bytes_sent, status);
638 if (status >= 0) {
639 req->rq_xmit_bytes_sent += status;
640 if (status >= req->rq_slen)
641 return 0;
642 /* Still some bytes left; set up for a retry later. */
643 status = -EAGAIN;
646 switch (status) {
647 case -ENOTSOCK:
648 status = -ENOTCONN;
649 /* Should we call xs_close() here? */
650 break;
651 case -EAGAIN:
652 status = xs_nospace(task);
653 break;
654 default:
655 dprintk("RPC: sendmsg returned unrecognized error %d\n",
656 -status);
657 case -ENETUNREACH:
658 case -EPIPE:
659 case -ECONNREFUSED:
660 /* When the server has died, an ICMP port unreachable message
661 * prompts ECONNREFUSED. */
662 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
665 return status;
669 * xs_tcp_shutdown - gracefully shut down a TCP socket
670 * @xprt: transport
672 * Initiates a graceful shutdown of the TCP socket by calling the
673 * equivalent of shutdown(SHUT_WR);
675 static void xs_tcp_shutdown(struct rpc_xprt *xprt)
677 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
678 struct socket *sock = transport->sock;
680 if (sock != NULL) {
681 kernel_sock_shutdown(sock, SHUT_WR);
682 trace_rpc_socket_shutdown(xprt, sock);
687 * xs_tcp_send_request - write an RPC request to a TCP socket
688 * @task: address of RPC task that manages the state of an RPC request
690 * Return values:
691 * 0: The request has been sent
692 * EAGAIN: The socket was blocked, please call again later to
693 * complete the request
694 * ENOTCONN: Caller needs to invoke connect logic then call again
695 * other: Some other error occurred, the request was not sent
697 * XXX: In the case of soft timeouts, should we eventually give up
698 * if sendmsg is not able to make progress?
700 static int xs_tcp_send_request(struct rpc_task *task)
702 struct rpc_rqst *req = task->tk_rqstp;
703 struct rpc_xprt *xprt = req->rq_xprt;
704 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
705 struct xdr_buf *xdr = &req->rq_snd_buf;
706 bool zerocopy = true;
707 int status;
709 xs_encode_stream_record_marker(&req->rq_snd_buf);
711 xs_pktdump("packet data:",
712 req->rq_svec->iov_base,
713 req->rq_svec->iov_len);
714 /* Don't use zero copy if this is a resend. If the RPC call
715 * completes while the socket holds a reference to the pages,
716 * then we may end up resending corrupted data.
718 if (task->tk_flags & RPC_TASK_SENT)
719 zerocopy = false;
721 /* Continue transmitting the packet/record. We must be careful
722 * to cope with writespace callbacks arriving _after_ we have
723 * called sendmsg(). */
724 while (1) {
725 status = xs_sendpages(transport->sock,
726 NULL, 0, xdr, req->rq_bytes_sent,
727 zerocopy);
729 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
730 xdr->len - req->rq_bytes_sent, status);
732 if (unlikely(status < 0))
733 break;
735 /* If we've sent the entire packet, immediately
736 * reset the count of bytes sent. */
737 req->rq_bytes_sent += status;
738 req->rq_xmit_bytes_sent += status;
739 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
740 req->rq_bytes_sent = 0;
741 return 0;
744 if (status != 0)
745 continue;
746 status = -EAGAIN;
747 break;
750 switch (status) {
751 case -ENOTSOCK:
752 status = -ENOTCONN;
753 /* Should we call xs_close() here? */
754 break;
755 case -EAGAIN:
756 status = xs_nospace(task);
757 break;
758 default:
759 dprintk("RPC: sendmsg returned unrecognized error %d\n",
760 -status);
761 case -ECONNRESET:
762 xs_tcp_shutdown(xprt);
763 case -ECONNREFUSED:
764 case -ENOTCONN:
765 case -EPIPE:
766 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
769 return status;
773 * xs_tcp_release_xprt - clean up after a tcp transmission
774 * @xprt: transport
775 * @task: rpc task
777 * This cleans up if an error causes us to abort the transmission of a request.
778 * In this case, the socket may need to be reset in order to avoid confusing
779 * the server.
781 static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
783 struct rpc_rqst *req;
785 if (task != xprt->snd_task)
786 return;
787 if (task == NULL)
788 goto out_release;
789 req = task->tk_rqstp;
790 if (req == NULL)
791 goto out_release;
792 if (req->rq_bytes_sent == 0)
793 goto out_release;
794 if (req->rq_bytes_sent == req->rq_snd_buf.len)
795 goto out_release;
796 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
797 out_release:
798 xprt_release_xprt(xprt, task);
801 static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
803 transport->old_data_ready = sk->sk_data_ready;
804 transport->old_state_change = sk->sk_state_change;
805 transport->old_write_space = sk->sk_write_space;
808 static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
810 sk->sk_data_ready = transport->old_data_ready;
811 sk->sk_state_change = transport->old_state_change;
812 sk->sk_write_space = transport->old_write_space;
815 static void xs_reset_transport(struct sock_xprt *transport)
817 struct socket *sock = transport->sock;
818 struct sock *sk = transport->inet;
820 if (sk == NULL)
821 return;
823 transport->srcport = 0;
825 write_lock_bh(&sk->sk_callback_lock);
826 transport->inet = NULL;
827 transport->sock = NULL;
829 sk->sk_user_data = NULL;
831 xs_restore_old_callbacks(transport, sk);
832 write_unlock_bh(&sk->sk_callback_lock);
834 sk->sk_no_check = 0;
836 trace_rpc_socket_close(&transport->xprt, sock);
837 sock_release(sock);
841 * xs_close - close a socket
842 * @xprt: transport
844 * This is used when all requests are complete; ie, no DRC state remains
845 * on the server we want to save.
847 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
848 * xs_reset_transport() zeroing the socket from underneath a writer.
850 static void xs_close(struct rpc_xprt *xprt)
852 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
854 dprintk("RPC: xs_close xprt %p\n", xprt);
856 cancel_delayed_work_sync(&transport->connect_worker);
858 xs_reset_transport(transport);
859 xprt->reestablish_timeout = 0;
861 smp_mb__before_clear_bit();
862 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
863 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
864 clear_bit(XPRT_CLOSING, &xprt->state);
865 smp_mb__after_clear_bit();
866 xprt_disconnect_done(xprt);
869 static void xs_tcp_close(struct rpc_xprt *xprt)
871 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
872 xs_close(xprt);
873 else
874 xs_tcp_shutdown(xprt);
877 static void xs_local_destroy(struct rpc_xprt *xprt)
879 xs_close(xprt);
880 xs_free_peer_addresses(xprt);
881 xprt_free(xprt);
882 module_put(THIS_MODULE);
886 * xs_destroy - prepare to shutdown a transport
887 * @xprt: doomed transport
890 static void xs_destroy(struct rpc_xprt *xprt)
892 dprintk("RPC: xs_destroy xprt %p\n", xprt);
894 xs_local_destroy(xprt);
897 static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
899 return (struct rpc_xprt *) sk->sk_user_data;
902 static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
904 struct xdr_skb_reader desc = {
905 .skb = skb,
906 .offset = sizeof(rpc_fraghdr),
907 .count = skb->len - sizeof(rpc_fraghdr),
910 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
911 return -1;
912 if (desc.count)
913 return -1;
914 return 0;
918 * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
919 * @sk: socket with data to read
920 * @len: how much data to read
922 * Currently this assumes we can read the whole reply in a single gulp.
924 static void xs_local_data_ready(struct sock *sk, int len)
926 struct rpc_task *task;
927 struct rpc_xprt *xprt;
928 struct rpc_rqst *rovr;
929 struct sk_buff *skb;
930 int err, repsize, copied;
931 u32 _xid;
932 __be32 *xp;
934 read_lock_bh(&sk->sk_callback_lock);
935 dprintk("RPC: %s...\n", __func__);
936 xprt = xprt_from_sock(sk);
937 if (xprt == NULL)
938 goto out;
940 skb = skb_recv_datagram(sk, 0, 1, &err);
941 if (skb == NULL)
942 goto out;
944 repsize = skb->len - sizeof(rpc_fraghdr);
945 if (repsize < 4) {
946 dprintk("RPC: impossible RPC reply size %d\n", repsize);
947 goto dropit;
950 /* Copy the XID from the skb... */
951 xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
952 if (xp == NULL)
953 goto dropit;
955 /* Look up and lock the request corresponding to the given XID */
956 spin_lock(&xprt->transport_lock);
957 rovr = xprt_lookup_rqst(xprt, *xp);
958 if (!rovr)
959 goto out_unlock;
960 task = rovr->rq_task;
962 copied = rovr->rq_private_buf.buflen;
963 if (copied > repsize)
964 copied = repsize;
966 if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
967 dprintk("RPC: sk_buff copy failed\n");
968 goto out_unlock;
971 xprt_complete_rqst(task, copied);
973 out_unlock:
974 spin_unlock(&xprt->transport_lock);
975 dropit:
976 skb_free_datagram(sk, skb);
977 out:
978 read_unlock_bh(&sk->sk_callback_lock);
982 * xs_udp_data_ready - "data ready" callback for UDP sockets
983 * @sk: socket with data to read
984 * @len: how much data to read
987 static void xs_udp_data_ready(struct sock *sk, int len)
989 struct rpc_task *task;
990 struct rpc_xprt *xprt;
991 struct rpc_rqst *rovr;
992 struct sk_buff *skb;
993 int err, repsize, copied;
994 u32 _xid;
995 __be32 *xp;
997 read_lock_bh(&sk->sk_callback_lock);
998 dprintk("RPC: xs_udp_data_ready...\n");
999 if (!(xprt = xprt_from_sock(sk)))
1000 goto out;
1002 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
1003 goto out;
1005 repsize = skb->len - sizeof(struct udphdr);
1006 if (repsize < 4) {
1007 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
1008 goto dropit;
1011 /* Copy the XID from the skb... */
1012 xp = skb_header_pointer(skb, sizeof(struct udphdr),
1013 sizeof(_xid), &_xid);
1014 if (xp == NULL)
1015 goto dropit;
1017 /* Look up and lock the request corresponding to the given XID */
1018 spin_lock(&xprt->transport_lock);
1019 rovr = xprt_lookup_rqst(xprt, *xp);
1020 if (!rovr)
1021 goto out_unlock;
1022 task = rovr->rq_task;
1024 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1025 copied = repsize;
1027 /* Suck it into the iovec, verify checksum if not done by hw. */
1028 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1029 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
1030 goto out_unlock;
1033 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
1035 xprt_adjust_cwnd(xprt, task, copied);
1036 xprt_complete_rqst(task, copied);
1038 out_unlock:
1039 spin_unlock(&xprt->transport_lock);
1040 dropit:
1041 skb_free_datagram(sk, skb);
1042 out:
1043 read_unlock_bh(&sk->sk_callback_lock);
1047 * Helper function to force a TCP close if the server is sending
1048 * junk and/or it has put us in CLOSE_WAIT
1050 static void xs_tcp_force_close(struct rpc_xprt *xprt)
1052 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1053 xprt_force_disconnect(xprt);
1056 static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
1058 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1059 size_t len, used;
1060 char *p;
1062 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
1063 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
1064 used = xdr_skb_read_bits(desc, p, len);
1065 transport->tcp_offset += used;
1066 if (used != len)
1067 return;
1069 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
1070 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
1071 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
1072 else
1073 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
1074 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
1076 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
1077 transport->tcp_offset = 0;
1079 /* Sanity check of the record length */
1080 if (unlikely(transport->tcp_reclen < 8)) {
1081 dprintk("RPC: invalid TCP record fragment length\n");
1082 xs_tcp_force_close(xprt);
1083 return;
1085 dprintk("RPC: reading TCP record fragment of length %d\n",
1086 transport->tcp_reclen);
1089 static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
1091 if (transport->tcp_offset == transport->tcp_reclen) {
1092 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
1093 transport->tcp_offset = 0;
1094 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1095 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1096 transport->tcp_flags |= TCP_RCV_COPY_XID;
1097 transport->tcp_copied = 0;
1102 static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1104 size_t len, used;
1105 char *p;
1107 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
1108 dprintk("RPC: reading XID (%Zu bytes)\n", len);
1109 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
1110 used = xdr_skb_read_bits(desc, p, len);
1111 transport->tcp_offset += used;
1112 if (used != len)
1113 return;
1114 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
1115 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
1116 transport->tcp_copied = 4;
1117 dprintk("RPC: reading %s XID %08x\n",
1118 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1119 : "request with",
1120 ntohl(transport->tcp_xid));
1121 xs_tcp_check_fraghdr(transport);
1124 static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1125 struct xdr_skb_reader *desc)
1127 size_t len, used;
1128 u32 offset;
1129 char *p;
1132 * We want transport->tcp_offset to be 8 at the end of this routine
1133 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1134 * When this function is called for the first time,
1135 * transport->tcp_offset is 4 (after having already read the xid).
1137 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
1138 len = sizeof(transport->tcp_calldir) - offset;
1139 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
1140 p = ((char *) &transport->tcp_calldir) + offset;
1141 used = xdr_skb_read_bits(desc, p, len);
1142 transport->tcp_offset += used;
1143 if (used != len)
1144 return;
1145 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
1147 * We don't yet have the XDR buffer, so we will write the calldir
1148 * out after we get the buffer from the 'struct rpc_rqst'
1150 switch (ntohl(transport->tcp_calldir)) {
1151 case RPC_REPLY:
1152 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1153 transport->tcp_flags |= TCP_RCV_COPY_DATA;
1154 transport->tcp_flags |= TCP_RPC_REPLY;
1155 break;
1156 case RPC_CALL:
1157 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1158 transport->tcp_flags |= TCP_RCV_COPY_DATA;
1159 transport->tcp_flags &= ~TCP_RPC_REPLY;
1160 break;
1161 default:
1162 dprintk("RPC: invalid request message type\n");
1163 xs_tcp_force_close(&transport->xprt);
1165 xs_tcp_check_fraghdr(transport);
1168 static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1169 struct xdr_skb_reader *desc,
1170 struct rpc_rqst *req)
1172 struct sock_xprt *transport =
1173 container_of(xprt, struct sock_xprt, xprt);
1174 struct xdr_buf *rcvbuf;
1175 size_t len;
1176 ssize_t r;
1178 rcvbuf = &req->rq_private_buf;
1180 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1182 * Save the RPC direction in the XDR buffer
1184 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
1185 &transport->tcp_calldir,
1186 sizeof(transport->tcp_calldir));
1187 transport->tcp_copied += sizeof(transport->tcp_calldir);
1188 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
1191 len = desc->count;
1192 if (len > transport->tcp_reclen - transport->tcp_offset) {
1193 struct xdr_skb_reader my_desc;
1195 len = transport->tcp_reclen - transport->tcp_offset;
1196 memcpy(&my_desc, desc, sizeof(my_desc));
1197 my_desc.count = len;
1198 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
1199 &my_desc, xdr_skb_read_bits);
1200 desc->count -= r;
1201 desc->offset += r;
1202 } else
1203 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
1204 desc, xdr_skb_read_bits);
1206 if (r > 0) {
1207 transport->tcp_copied += r;
1208 transport->tcp_offset += r;
1210 if (r != len) {
1211 /* Error when copying to the receive buffer,
1212 * usually because we weren't able to allocate
1213 * additional buffer pages. All we can do now
1214 * is turn off TCP_RCV_COPY_DATA, so the request
1215 * will not receive any additional updates,
1216 * and time out.
1217 * Any remaining data from this record will
1218 * be discarded.
1220 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1221 dprintk("RPC: XID %08x truncated request\n",
1222 ntohl(transport->tcp_xid));
1223 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1224 "tcp_offset = %u, tcp_reclen = %u\n",
1225 xprt, transport->tcp_copied,
1226 transport->tcp_offset, transport->tcp_reclen);
1227 return;
1230 dprintk("RPC: XID %08x read %Zd bytes\n",
1231 ntohl(transport->tcp_xid), r);
1232 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1233 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1234 transport->tcp_offset, transport->tcp_reclen);
1236 if (transport->tcp_copied == req->rq_private_buf.buflen)
1237 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1238 else if (transport->tcp_offset == transport->tcp_reclen) {
1239 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1240 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1245 * Finds the request corresponding to the RPC xid and invokes the common
1246 * tcp read code to read the data.
1248 static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1249 struct xdr_skb_reader *desc)
1251 struct sock_xprt *transport =
1252 container_of(xprt, struct sock_xprt, xprt);
1253 struct rpc_rqst *req;
1255 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1257 /* Find and lock the request corresponding to this xid */
1258 spin_lock(&xprt->transport_lock);
1259 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1260 if (!req) {
1261 dprintk("RPC: XID %08x request not found!\n",
1262 ntohl(transport->tcp_xid));
1263 spin_unlock(&xprt->transport_lock);
1264 return -1;
1267 xs_tcp_read_common(xprt, desc, req);
1269 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
1270 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
1272 spin_unlock(&xprt->transport_lock);
1273 return 0;
1276 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1278 * Obtains an rpc_rqst previously allocated and invokes the common
1279 * tcp read code to read the data. The result is placed in the callback
1280 * queue.
1281 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1282 * connection and return -1.
1284 static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1285 struct xdr_skb_reader *desc)
1287 struct sock_xprt *transport =
1288 container_of(xprt, struct sock_xprt, xprt);
1289 struct rpc_rqst *req;
1291 req = xprt_alloc_bc_request(xprt);
1292 if (req == NULL) {
1293 printk(KERN_WARNING "Callback slot table overflowed\n");
1294 xprt_force_disconnect(xprt);
1295 return -1;
1298 req->rq_xid = transport->tcp_xid;
1299 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1300 xs_tcp_read_common(xprt, desc, req);
1302 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1303 struct svc_serv *bc_serv = xprt->bc_serv;
1306 * Add callback request to callback list. The callback
1307 * service sleeps on the sv_cb_waitq waiting for new
1308 * requests. Wake it up after adding enqueing the
1309 * request.
1311 dprintk("RPC: add callback request to list\n");
1312 spin_lock(&bc_serv->sv_cb_lock);
1313 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1314 spin_unlock(&bc_serv->sv_cb_lock);
1315 wake_up(&bc_serv->sv_cb_waitq);
1318 req->rq_private_buf.len = transport->tcp_copied;
1320 return 0;
1323 static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1324 struct xdr_skb_reader *desc)
1326 struct sock_xprt *transport =
1327 container_of(xprt, struct sock_xprt, xprt);
1329 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1330 xs_tcp_read_reply(xprt, desc) :
1331 xs_tcp_read_callback(xprt, desc);
1333 #else
1334 static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1335 struct xdr_skb_reader *desc)
1337 return xs_tcp_read_reply(xprt, desc);
1339 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1342 * Read data off the transport. This can be either an RPC_CALL or an
1343 * RPC_REPLY. Relay the processing to helper functions.
1345 static void xs_tcp_read_data(struct rpc_xprt *xprt,
1346 struct xdr_skb_reader *desc)
1348 struct sock_xprt *transport =
1349 container_of(xprt, struct sock_xprt, xprt);
1351 if (_xs_tcp_read_data(xprt, desc) == 0)
1352 xs_tcp_check_fraghdr(transport);
1353 else {
1355 * The transport_lock protects the request handling.
1356 * There's no need to hold it to update the tcp_flags.
1358 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1362 static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1364 size_t len;
1366 len = transport->tcp_reclen - transport->tcp_offset;
1367 if (len > desc->count)
1368 len = desc->count;
1369 desc->count -= len;
1370 desc->offset += len;
1371 transport->tcp_offset += len;
1372 dprintk("RPC: discarded %Zu bytes\n", len);
1373 xs_tcp_check_fraghdr(transport);
1376 static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
1378 struct rpc_xprt *xprt = rd_desc->arg.data;
1379 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1380 struct xdr_skb_reader desc = {
1381 .skb = skb,
1382 .offset = offset,
1383 .count = len,
1386 dprintk("RPC: xs_tcp_data_recv started\n");
1387 do {
1388 /* Read in a new fragment marker if necessary */
1389 /* Can we ever really expect to get completely empty fragments? */
1390 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
1391 xs_tcp_read_fraghdr(xprt, &desc);
1392 continue;
1394 /* Read in the xid if necessary */
1395 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
1396 xs_tcp_read_xid(transport, &desc);
1397 continue;
1399 /* Read in the call/reply flag */
1400 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
1401 xs_tcp_read_calldir(transport, &desc);
1402 continue;
1404 /* Read in the request data */
1405 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
1406 xs_tcp_read_data(xprt, &desc);
1407 continue;
1409 /* Skip over any trailing bytes on short reads */
1410 xs_tcp_read_discard(transport, &desc);
1411 } while (desc.count);
1412 dprintk("RPC: xs_tcp_data_recv done\n");
1413 return len - desc.count;
1417 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1418 * @sk: socket with data to read
1419 * @bytes: how much data to read
1422 static void xs_tcp_data_ready(struct sock *sk, int bytes)
1424 struct rpc_xprt *xprt;
1425 read_descriptor_t rd_desc;
1426 int read;
1428 dprintk("RPC: xs_tcp_data_ready...\n");
1430 read_lock_bh(&sk->sk_callback_lock);
1431 if (!(xprt = xprt_from_sock(sk)))
1432 goto out;
1433 /* Any data means we had a useful conversation, so
1434 * the we don't need to delay the next reconnect
1436 if (xprt->reestablish_timeout)
1437 xprt->reestablish_timeout = 0;
1439 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1440 rd_desc.arg.data = xprt;
1441 do {
1442 rd_desc.count = 65536;
1443 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1444 } while (read > 0);
1445 out:
1446 read_unlock_bh(&sk->sk_callback_lock);
1450 * Do the equivalent of linger/linger2 handling for dealing with
1451 * broken servers that don't close the socket in a timely
1452 * fashion
1454 static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1455 unsigned long timeout)
1457 struct sock_xprt *transport;
1459 if (xprt_test_and_set_connecting(xprt))
1460 return;
1461 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1462 transport = container_of(xprt, struct sock_xprt, xprt);
1463 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1464 timeout);
1467 static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1469 struct sock_xprt *transport;
1471 transport = container_of(xprt, struct sock_xprt, xprt);
1473 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1474 !cancel_delayed_work(&transport->connect_worker))
1475 return;
1476 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1477 xprt_clear_connecting(xprt);
1480 static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1482 smp_mb__before_clear_bit();
1483 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1484 clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1485 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1486 clear_bit(XPRT_CLOSING, &xprt->state);
1487 smp_mb__after_clear_bit();
1490 static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1492 xs_sock_reset_connection_flags(xprt);
1493 /* Mark transport as closed and wake up all pending tasks */
1494 xprt_disconnect_done(xprt);
1498 * xs_tcp_state_change - callback to handle TCP socket state changes
1499 * @sk: socket whose state has changed
1502 static void xs_tcp_state_change(struct sock *sk)
1504 struct rpc_xprt *xprt;
1506 read_lock_bh(&sk->sk_callback_lock);
1507 if (!(xprt = xprt_from_sock(sk)))
1508 goto out;
1509 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1510 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1511 sk->sk_state, xprt_connected(xprt),
1512 sock_flag(sk, SOCK_DEAD),
1513 sock_flag(sk, SOCK_ZAPPED),
1514 sk->sk_shutdown);
1516 trace_rpc_socket_state_change(xprt, sk->sk_socket);
1517 switch (sk->sk_state) {
1518 case TCP_ESTABLISHED:
1519 spin_lock(&xprt->transport_lock);
1520 if (!xprt_test_and_set_connected(xprt)) {
1521 struct sock_xprt *transport = container_of(xprt,
1522 struct sock_xprt, xprt);
1524 /* Reset TCP record info */
1525 transport->tcp_offset = 0;
1526 transport->tcp_reclen = 0;
1527 transport->tcp_copied = 0;
1528 transport->tcp_flags =
1529 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
1531 xprt_wake_pending_tasks(xprt, -EAGAIN);
1533 spin_unlock(&xprt->transport_lock);
1534 break;
1535 case TCP_FIN_WAIT1:
1536 /* The client initiated a shutdown of the socket */
1537 xprt->connect_cookie++;
1538 xprt->reestablish_timeout = 0;
1539 set_bit(XPRT_CLOSING, &xprt->state);
1540 smp_mb__before_clear_bit();
1541 clear_bit(XPRT_CONNECTED, &xprt->state);
1542 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1543 smp_mb__after_clear_bit();
1544 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1545 break;
1546 case TCP_CLOSE_WAIT:
1547 /* The server initiated a shutdown of the socket */
1548 xprt->connect_cookie++;
1549 clear_bit(XPRT_CONNECTED, &xprt->state);
1550 xs_tcp_force_close(xprt);
1551 case TCP_CLOSING:
1553 * If the server closed down the connection, make sure that
1554 * we back off before reconnecting
1556 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1557 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1558 break;
1559 case TCP_LAST_ACK:
1560 set_bit(XPRT_CLOSING, &xprt->state);
1561 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1562 smp_mb__before_clear_bit();
1563 clear_bit(XPRT_CONNECTED, &xprt->state);
1564 smp_mb__after_clear_bit();
1565 break;
1566 case TCP_CLOSE:
1567 xs_tcp_cancel_linger_timeout(xprt);
1568 xs_sock_mark_closed(xprt);
1570 out:
1571 read_unlock_bh(&sk->sk_callback_lock);
1574 static void xs_write_space(struct sock *sk)
1576 struct socket *sock;
1577 struct rpc_xprt *xprt;
1579 if (unlikely(!(sock = sk->sk_socket)))
1580 return;
1581 clear_bit(SOCK_NOSPACE, &sock->flags);
1583 if (unlikely(!(xprt = xprt_from_sock(sk))))
1584 return;
1585 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1586 return;
1588 xprt_write_space(xprt);
1592 * xs_udp_write_space - callback invoked when socket buffer space
1593 * becomes available
1594 * @sk: socket whose state has changed
1596 * Called when more output buffer space is available for this socket.
1597 * We try not to wake our writers until they can make "significant"
1598 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1599 * with a bunch of small requests.
1601 static void xs_udp_write_space(struct sock *sk)
1603 read_lock_bh(&sk->sk_callback_lock);
1605 /* from net/core/sock.c:sock_def_write_space */
1606 if (sock_writeable(sk))
1607 xs_write_space(sk);
1609 read_unlock_bh(&sk->sk_callback_lock);
1613 * xs_tcp_write_space - callback invoked when socket buffer space
1614 * becomes available
1615 * @sk: socket whose state has changed
1617 * Called when more output buffer space is available for this socket.
1618 * We try not to wake our writers until they can make "significant"
1619 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1620 * with a bunch of small requests.
1622 static void xs_tcp_write_space(struct sock *sk)
1624 read_lock_bh(&sk->sk_callback_lock);
1626 /* from net/core/stream.c:sk_stream_write_space */
1627 if (sk_stream_is_writeable(sk))
1628 xs_write_space(sk);
1630 read_unlock_bh(&sk->sk_callback_lock);
1633 static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1635 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1636 struct sock *sk = transport->inet;
1638 if (transport->rcvsize) {
1639 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
1640 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1642 if (transport->sndsize) {
1643 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
1644 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1645 sk->sk_write_space(sk);
1650 * xs_udp_set_buffer_size - set send and receive limits
1651 * @xprt: generic transport
1652 * @sndsize: requested size of send buffer, in bytes
1653 * @rcvsize: requested size of receive buffer, in bytes
1655 * Set socket send and receive buffer size limits.
1657 static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
1659 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1661 transport->sndsize = 0;
1662 if (sndsize)
1663 transport->sndsize = sndsize + 1024;
1664 transport->rcvsize = 0;
1665 if (rcvsize)
1666 transport->rcvsize = rcvsize + 1024;
1668 xs_udp_do_set_buffer_size(xprt);
1672 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1673 * @task: task that timed out
1675 * Adjust the congestion window after a retransmit timeout has occurred.
1677 static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
1679 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
1682 static unsigned short xs_get_random_port(void)
1684 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1685 unsigned short rand = (unsigned short) net_random() % range;
1686 return rand + xprt_min_resvport;
1690 * xs_set_port - reset the port number in the remote endpoint address
1691 * @xprt: generic transport
1692 * @port: new port number
1695 static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1697 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
1699 rpc_set_port(xs_addr(xprt), port);
1700 xs_update_peer_port(xprt);
1703 static unsigned short xs_get_srcport(struct sock_xprt *transport)
1705 unsigned short port = transport->srcport;
1707 if (port == 0 && transport->xprt.resvport)
1708 port = xs_get_random_port();
1709 return port;
1712 static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
1714 if (transport->srcport != 0)
1715 transport->srcport = 0;
1716 if (!transport->xprt.resvport)
1717 return 0;
1718 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1719 return xprt_max_resvport;
1720 return --port;
1722 static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1724 struct sockaddr_storage myaddr;
1725 int err, nloop = 0;
1726 unsigned short port = xs_get_srcport(transport);
1727 unsigned short last;
1729 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1730 do {
1731 rpc_set_port((struct sockaddr *)&myaddr, port);
1732 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1733 transport->xprt.addrlen);
1734 if (port == 0)
1735 break;
1736 if (err == 0) {
1737 transport->srcport = port;
1738 break;
1740 last = port;
1741 port = xs_next_srcport(transport, port);
1742 if (port > last)
1743 nloop++;
1744 } while (err == -EADDRINUSE && nloop != 2);
1746 if (myaddr.ss_family == AF_INET)
1747 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1748 &((struct sockaddr_in *)&myaddr)->sin_addr,
1749 port, err ? "failed" : "ok", err);
1750 else
1751 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1752 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1753 port, err ? "failed" : "ok", err);
1754 return err;
1758 * We don't support autobind on AF_LOCAL sockets
1760 static void xs_local_rpcbind(struct rpc_task *task)
1762 rcu_read_lock();
1763 xprt_set_bound(rcu_dereference(task->tk_client->cl_xprt));
1764 rcu_read_unlock();
1767 static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1771 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1772 static struct lock_class_key xs_key[2];
1773 static struct lock_class_key xs_slock_key[2];
1775 static inline void xs_reclassify_socketu(struct socket *sock)
1777 struct sock *sk = sock->sk;
1779 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1780 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1783 static inline void xs_reclassify_socket4(struct socket *sock)
1785 struct sock *sk = sock->sk;
1787 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1788 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1791 static inline void xs_reclassify_socket6(struct socket *sock)
1793 struct sock *sk = sock->sk;
1795 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1796 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1799 static inline void xs_reclassify_socket(int family, struct socket *sock)
1801 WARN_ON_ONCE(sock_owned_by_user(sock->sk));
1802 if (sock_owned_by_user(sock->sk))
1803 return;
1805 switch (family) {
1806 case AF_LOCAL:
1807 xs_reclassify_socketu(sock);
1808 break;
1809 case AF_INET:
1810 xs_reclassify_socket4(sock);
1811 break;
1812 case AF_INET6:
1813 xs_reclassify_socket6(sock);
1814 break;
1817 #else
1818 static inline void xs_reclassify_socketu(struct socket *sock)
1822 static inline void xs_reclassify_socket4(struct socket *sock)
1826 static inline void xs_reclassify_socket6(struct socket *sock)
1830 static inline void xs_reclassify_socket(int family, struct socket *sock)
1833 #endif
1835 static void xs_dummy_setup_socket(struct work_struct *work)
1839 static struct socket *xs_create_sock(struct rpc_xprt *xprt,
1840 struct sock_xprt *transport, int family, int type, int protocol)
1842 struct socket *sock;
1843 int err;
1845 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
1846 if (err < 0) {
1847 dprintk("RPC: can't create %d transport socket (%d).\n",
1848 protocol, -err);
1849 goto out;
1851 xs_reclassify_socket(family, sock);
1853 err = xs_bind(transport, sock);
1854 if (err) {
1855 sock_release(sock);
1856 goto out;
1859 return sock;
1860 out:
1861 return ERR_PTR(err);
1864 static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1865 struct socket *sock)
1867 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1868 xprt);
1870 if (!transport->inet) {
1871 struct sock *sk = sock->sk;
1873 write_lock_bh(&sk->sk_callback_lock);
1875 xs_save_old_callbacks(transport, sk);
1877 sk->sk_user_data = xprt;
1878 sk->sk_data_ready = xs_local_data_ready;
1879 sk->sk_write_space = xs_udp_write_space;
1880 sk->sk_allocation = GFP_ATOMIC;
1882 xprt_clear_connected(xprt);
1884 /* Reset to new socket */
1885 transport->sock = sock;
1886 transport->inet = sk;
1888 write_unlock_bh(&sk->sk_callback_lock);
1891 /* Tell the socket layer to start connecting... */
1892 xprt->stat.connect_count++;
1893 xprt->stat.connect_start = jiffies;
1894 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1898 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1899 * @xprt: RPC transport to connect
1900 * @transport: socket transport to connect
1901 * @create_sock: function to create a socket of the correct type
1903 static int xs_local_setup_socket(struct sock_xprt *transport)
1905 struct rpc_xprt *xprt = &transport->xprt;
1906 struct socket *sock;
1907 int status = -EIO;
1909 current->flags |= PF_FSTRANS;
1911 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1912 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1913 SOCK_STREAM, 0, &sock, 1);
1914 if (status < 0) {
1915 dprintk("RPC: can't create AF_LOCAL "
1916 "transport socket (%d).\n", -status);
1917 goto out;
1919 xs_reclassify_socketu(sock);
1921 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1922 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1924 status = xs_local_finish_connecting(xprt, sock);
1925 trace_rpc_socket_connect(xprt, sock, status);
1926 switch (status) {
1927 case 0:
1928 dprintk("RPC: xprt %p connected to %s\n",
1929 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1930 xprt_set_connected(xprt);
1931 break;
1932 case -ENOENT:
1933 dprintk("RPC: xprt %p: socket %s does not exist\n",
1934 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1935 break;
1936 case -ECONNREFUSED:
1937 dprintk("RPC: xprt %p: connection refused for %s\n",
1938 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1939 break;
1940 default:
1941 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1942 __func__, -status,
1943 xprt->address_strings[RPC_DISPLAY_ADDR]);
1946 out:
1947 xprt_clear_connecting(xprt);
1948 xprt_wake_pending_tasks(xprt, status);
1949 current->flags &= ~PF_FSTRANS;
1950 return status;
1953 static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
1955 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1956 int ret;
1958 if (RPC_IS_ASYNC(task)) {
1960 * We want the AF_LOCAL connect to be resolved in the
1961 * filesystem namespace of the process making the rpc
1962 * call. Thus we connect synchronously.
1964 * If we want to support asynchronous AF_LOCAL calls,
1965 * we'll need to figure out how to pass a namespace to
1966 * connect.
1968 rpc_exit(task, -ENOTCONN);
1969 return;
1971 ret = xs_local_setup_socket(transport);
1972 if (ret && !RPC_IS_SOFTCONN(task))
1973 msleep_interruptible(15000);
1976 #ifdef CONFIG_SUNRPC_SWAP
1977 static void xs_set_memalloc(struct rpc_xprt *xprt)
1979 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1980 xprt);
1982 if (xprt->swapper)
1983 sk_set_memalloc(transport->inet);
1987 * xs_swapper - Tag this transport as being used for swap.
1988 * @xprt: transport to tag
1989 * @enable: enable/disable
1992 int xs_swapper(struct rpc_xprt *xprt, int enable)
1994 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1995 xprt);
1996 int err = 0;
1998 if (enable) {
1999 xprt->swapper++;
2000 xs_set_memalloc(xprt);
2001 } else if (xprt->swapper) {
2002 xprt->swapper--;
2003 sk_clear_memalloc(transport->inet);
2006 return err;
2008 EXPORT_SYMBOL_GPL(xs_swapper);
2009 #else
2010 static void xs_set_memalloc(struct rpc_xprt *xprt)
2013 #endif
2015 static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2017 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2019 if (!transport->inet) {
2020 struct sock *sk = sock->sk;
2022 write_lock_bh(&sk->sk_callback_lock);
2024 xs_save_old_callbacks(transport, sk);
2026 sk->sk_user_data = xprt;
2027 sk->sk_data_ready = xs_udp_data_ready;
2028 sk->sk_write_space = xs_udp_write_space;
2029 sk->sk_no_check = UDP_CSUM_NORCV;
2030 sk->sk_allocation = GFP_ATOMIC;
2032 xprt_set_connected(xprt);
2034 /* Reset to new socket */
2035 transport->sock = sock;
2036 transport->inet = sk;
2038 xs_set_memalloc(xprt);
2040 write_unlock_bh(&sk->sk_callback_lock);
2042 xs_udp_do_set_buffer_size(xprt);
2045 static void xs_udp_setup_socket(struct work_struct *work)
2047 struct sock_xprt *transport =
2048 container_of(work, struct sock_xprt, connect_worker.work);
2049 struct rpc_xprt *xprt = &transport->xprt;
2050 struct socket *sock = transport->sock;
2051 int status = -EIO;
2053 current->flags |= PF_FSTRANS;
2055 /* Start by resetting any existing state */
2056 xs_reset_transport(transport);
2057 sock = xs_create_sock(xprt, transport,
2058 xs_addr(xprt)->sa_family, SOCK_DGRAM, IPPROTO_UDP);
2059 if (IS_ERR(sock))
2060 goto out;
2062 dprintk("RPC: worker connecting xprt %p via %s to "
2063 "%s (port %s)\n", xprt,
2064 xprt->address_strings[RPC_DISPLAY_PROTO],
2065 xprt->address_strings[RPC_DISPLAY_ADDR],
2066 xprt->address_strings[RPC_DISPLAY_PORT]);
2068 xs_udp_finish_connecting(xprt, sock);
2069 trace_rpc_socket_connect(xprt, sock, 0);
2070 status = 0;
2071 out:
2072 xprt_clear_connecting(xprt);
2073 xprt_wake_pending_tasks(xprt, status);
2074 current->flags &= ~PF_FSTRANS;
2078 * We need to preserve the port number so the reply cache on the server can
2079 * find our cached RPC replies when we get around to reconnecting.
2081 static void xs_abort_connection(struct sock_xprt *transport)
2083 int result;
2084 struct sockaddr any;
2086 dprintk("RPC: disconnecting xprt %p to reuse port\n", transport);
2089 * Disconnect the transport socket by doing a connect operation
2090 * with AF_UNSPEC. This should return immediately...
2092 memset(&any, 0, sizeof(any));
2093 any.sa_family = AF_UNSPEC;
2094 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
2095 trace_rpc_socket_reset_connection(&transport->xprt,
2096 transport->sock, result);
2097 if (!result)
2098 xs_sock_reset_connection_flags(&transport->xprt);
2099 dprintk("RPC: AF_UNSPEC connect return code %d\n", result);
2102 static void xs_tcp_reuse_connection(struct sock_xprt *transport)
2104 unsigned int state = transport->inet->sk_state;
2106 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) {
2107 /* we don't need to abort the connection if the socket
2108 * hasn't undergone a shutdown
2110 if (transport->inet->sk_shutdown == 0)
2111 return;
2112 dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n",
2113 __func__, transport->inet->sk_shutdown);
2115 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) {
2116 /* we don't need to abort the connection if the socket
2117 * hasn't undergone a shutdown
2119 if (transport->inet->sk_shutdown == 0)
2120 return;
2121 dprintk("RPC: %s: ESTABLISHED/SYN_SENT "
2122 "sk_shutdown set to %d\n",
2123 __func__, transport->inet->sk_shutdown);
2125 xs_abort_connection(transport);
2128 static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2130 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2131 int ret = -ENOTCONN;
2133 if (!transport->inet) {
2134 struct sock *sk = sock->sk;
2136 write_lock_bh(&sk->sk_callback_lock);
2138 xs_save_old_callbacks(transport, sk);
2140 sk->sk_user_data = xprt;
2141 sk->sk_data_ready = xs_tcp_data_ready;
2142 sk->sk_state_change = xs_tcp_state_change;
2143 sk->sk_write_space = xs_tcp_write_space;
2144 sk->sk_allocation = GFP_ATOMIC;
2146 /* socket options */
2147 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
2148 sock_reset_flag(sk, SOCK_LINGER);
2149 tcp_sk(sk)->linger2 = 0;
2150 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
2152 xprt_clear_connected(xprt);
2154 /* Reset to new socket */
2155 transport->sock = sock;
2156 transport->inet = sk;
2158 write_unlock_bh(&sk->sk_callback_lock);
2161 if (!xprt_bound(xprt))
2162 goto out;
2164 xs_set_memalloc(xprt);
2166 /* Tell the socket layer to start connecting... */
2167 xprt->stat.connect_count++;
2168 xprt->stat.connect_start = jiffies;
2169 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2170 switch (ret) {
2171 case 0:
2172 case -EINPROGRESS:
2173 /* SYN_SENT! */
2174 xprt->connect_cookie++;
2175 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2176 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2178 out:
2179 return ret;
2183 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2184 * @xprt: RPC transport to connect
2185 * @transport: socket transport to connect
2186 * @create_sock: function to create a socket of the correct type
2188 * Invoked by a work queue tasklet.
2190 static void xs_tcp_setup_socket(struct work_struct *work)
2192 struct sock_xprt *transport =
2193 container_of(work, struct sock_xprt, connect_worker.work);
2194 struct socket *sock = transport->sock;
2195 struct rpc_xprt *xprt = &transport->xprt;
2196 int status = -EIO;
2198 current->flags |= PF_FSTRANS;
2200 if (!sock) {
2201 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
2202 sock = xs_create_sock(xprt, transport,
2203 xs_addr(xprt)->sa_family, SOCK_STREAM, IPPROTO_TCP);
2204 if (IS_ERR(sock)) {
2205 status = PTR_ERR(sock);
2206 goto out;
2208 } else {
2209 int abort_and_exit;
2211 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
2212 &xprt->state);
2213 /* "close" the socket, preserving the local port */
2214 xs_tcp_reuse_connection(transport);
2216 if (abort_and_exit)
2217 goto out_eagain;
2220 dprintk("RPC: worker connecting xprt %p via %s to "
2221 "%s (port %s)\n", xprt,
2222 xprt->address_strings[RPC_DISPLAY_PROTO],
2223 xprt->address_strings[RPC_DISPLAY_ADDR],
2224 xprt->address_strings[RPC_DISPLAY_PORT]);
2226 status = xs_tcp_finish_connecting(xprt, sock);
2227 trace_rpc_socket_connect(xprt, sock, status);
2228 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2229 xprt, -status, xprt_connected(xprt),
2230 sock->sk->sk_state);
2231 switch (status) {
2232 default:
2233 printk("%s: connect returned unhandled error %d\n",
2234 __func__, status);
2235 case -EADDRNOTAVAIL:
2236 /* We're probably in TIME_WAIT. Get rid of existing socket,
2237 * and retry
2239 xs_tcp_force_close(xprt);
2240 break;
2241 case 0:
2242 case -EINPROGRESS:
2243 case -EALREADY:
2244 xprt_clear_connecting(xprt);
2245 current->flags &= ~PF_FSTRANS;
2246 return;
2247 case -EINVAL:
2248 /* Happens, for instance, if the user specified a link
2249 * local IPv6 address without a scope-id.
2251 case -ECONNREFUSED:
2252 case -ECONNRESET:
2253 case -ENETUNREACH:
2254 /* retry with existing socket, after a delay */
2255 goto out;
2257 out_eagain:
2258 status = -EAGAIN;
2259 out:
2260 xprt_clear_connecting(xprt);
2261 xprt_wake_pending_tasks(xprt, status);
2262 current->flags &= ~PF_FSTRANS;
2266 * xs_connect - connect a socket to a remote endpoint
2267 * @xprt: pointer to transport structure
2268 * @task: address of RPC task that manages state of connect request
2270 * TCP: If the remote end dropped the connection, delay reconnecting.
2272 * UDP socket connects are synchronous, but we use a work queue anyway
2273 * to guarantee that even unprivileged user processes can set up a
2274 * socket on a privileged port.
2276 * If a UDP socket connect fails, the delay behavior here prevents
2277 * retry floods (hard mounts).
2279 static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2281 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2283 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
2284 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2285 "seconds\n",
2286 xprt, xprt->reestablish_timeout / HZ);
2287 queue_delayed_work(rpciod_workqueue,
2288 &transport->connect_worker,
2289 xprt->reestablish_timeout);
2290 xprt->reestablish_timeout <<= 1;
2291 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2292 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2293 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2294 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
2295 } else {
2296 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
2297 queue_delayed_work(rpciod_workqueue,
2298 &transport->connect_worker, 0);
2303 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2304 * @xprt: rpc_xprt struct containing statistics
2305 * @seq: output file
2308 static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2310 long idle_time = 0;
2312 if (xprt_connected(xprt))
2313 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2315 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
2316 "%llu %llu %lu %llu %llu\n",
2317 xprt->stat.bind_count,
2318 xprt->stat.connect_count,
2319 xprt->stat.connect_time,
2320 idle_time,
2321 xprt->stat.sends,
2322 xprt->stat.recvs,
2323 xprt->stat.bad_xids,
2324 xprt->stat.req_u,
2325 xprt->stat.bklog_u,
2326 xprt->stat.max_slots,
2327 xprt->stat.sending_u,
2328 xprt->stat.pending_u);
2332 * xs_udp_print_stats - display UDP socket-specifc stats
2333 * @xprt: rpc_xprt struct containing statistics
2334 * @seq: output file
2337 static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2339 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2341 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2342 "%lu %llu %llu\n",
2343 transport->srcport,
2344 xprt->stat.bind_count,
2345 xprt->stat.sends,
2346 xprt->stat.recvs,
2347 xprt->stat.bad_xids,
2348 xprt->stat.req_u,
2349 xprt->stat.bklog_u,
2350 xprt->stat.max_slots,
2351 xprt->stat.sending_u,
2352 xprt->stat.pending_u);
2356 * xs_tcp_print_stats - display TCP socket-specifc stats
2357 * @xprt: rpc_xprt struct containing statistics
2358 * @seq: output file
2361 static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2363 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2364 long idle_time = 0;
2366 if (xprt_connected(xprt))
2367 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2369 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2370 "%llu %llu %lu %llu %llu\n",
2371 transport->srcport,
2372 xprt->stat.bind_count,
2373 xprt->stat.connect_count,
2374 xprt->stat.connect_time,
2375 idle_time,
2376 xprt->stat.sends,
2377 xprt->stat.recvs,
2378 xprt->stat.bad_xids,
2379 xprt->stat.req_u,
2380 xprt->stat.bklog_u,
2381 xprt->stat.max_slots,
2382 xprt->stat.sending_u,
2383 xprt->stat.pending_u);
2387 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2388 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2389 * to use the server side send routines.
2391 static void *bc_malloc(struct rpc_task *task, size_t size)
2393 struct page *page;
2394 struct rpc_buffer *buf;
2396 WARN_ON_ONCE(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2397 if (size > PAGE_SIZE - sizeof(struct rpc_buffer))
2398 return NULL;
2400 page = alloc_page(GFP_KERNEL);
2401 if (!page)
2402 return NULL;
2404 buf = page_address(page);
2405 buf->len = PAGE_SIZE;
2407 return buf->data;
2411 * Free the space allocated in the bc_alloc routine
2413 static void bc_free(void *buffer)
2415 struct rpc_buffer *buf;
2417 if (!buffer)
2418 return;
2420 buf = container_of(buffer, struct rpc_buffer, data);
2421 free_page((unsigned long)buf);
2425 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2426 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2428 static int bc_sendto(struct rpc_rqst *req)
2430 int len;
2431 struct xdr_buf *xbufp = &req->rq_snd_buf;
2432 struct rpc_xprt *xprt = req->rq_xprt;
2433 struct sock_xprt *transport =
2434 container_of(xprt, struct sock_xprt, xprt);
2435 struct socket *sock = transport->sock;
2436 unsigned long headoff;
2437 unsigned long tailoff;
2439 xs_encode_stream_record_marker(xbufp);
2441 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2442 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2443 len = svc_send_common(sock, xbufp,
2444 virt_to_page(xbufp->head[0].iov_base), headoff,
2445 xbufp->tail[0].iov_base, tailoff);
2447 if (len != xbufp->len) {
2448 printk(KERN_NOTICE "Error sending entire callback!\n");
2449 len = -EAGAIN;
2452 return len;
2456 * The send routine. Borrows from svc_send
2458 static int bc_send_request(struct rpc_task *task)
2460 struct rpc_rqst *req = task->tk_rqstp;
2461 struct svc_xprt *xprt;
2462 u32 len;
2464 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2466 * Get the server socket associated with this callback xprt
2468 xprt = req->rq_xprt->bc_xprt;
2471 * Grab the mutex to serialize data as the connection is shared
2472 * with the fore channel
2474 if (!mutex_trylock(&xprt->xpt_mutex)) {
2475 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2476 if (!mutex_trylock(&xprt->xpt_mutex))
2477 return -EAGAIN;
2478 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2480 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2481 len = -ENOTCONN;
2482 else
2483 len = bc_sendto(req);
2484 mutex_unlock(&xprt->xpt_mutex);
2486 if (len > 0)
2487 len = 0;
2489 return len;
2493 * The close routine. Since this is client initiated, we do nothing
2496 static void bc_close(struct rpc_xprt *xprt)
2501 * The xprt destroy routine. Again, because this connection is client
2502 * initiated, we do nothing
2505 static void bc_destroy(struct rpc_xprt *xprt)
2509 static struct rpc_xprt_ops xs_local_ops = {
2510 .reserve_xprt = xprt_reserve_xprt,
2511 .release_xprt = xs_tcp_release_xprt,
2512 .alloc_slot = xprt_alloc_slot,
2513 .rpcbind = xs_local_rpcbind,
2514 .set_port = xs_local_set_port,
2515 .connect = xs_local_connect,
2516 .buf_alloc = rpc_malloc,
2517 .buf_free = rpc_free,
2518 .send_request = xs_local_send_request,
2519 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2520 .close = xs_close,
2521 .destroy = xs_local_destroy,
2522 .print_stats = xs_local_print_stats,
2525 static struct rpc_xprt_ops xs_udp_ops = {
2526 .set_buffer_size = xs_udp_set_buffer_size,
2527 .reserve_xprt = xprt_reserve_xprt_cong,
2528 .release_xprt = xprt_release_xprt_cong,
2529 .alloc_slot = xprt_alloc_slot,
2530 .rpcbind = rpcb_getport_async,
2531 .set_port = xs_set_port,
2532 .connect = xs_connect,
2533 .buf_alloc = rpc_malloc,
2534 .buf_free = rpc_free,
2535 .send_request = xs_udp_send_request,
2536 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
2537 .timer = xs_udp_timer,
2538 .release_request = xprt_release_rqst_cong,
2539 .close = xs_close,
2540 .destroy = xs_destroy,
2541 .print_stats = xs_udp_print_stats,
2544 static struct rpc_xprt_ops xs_tcp_ops = {
2545 .reserve_xprt = xprt_reserve_xprt,
2546 .release_xprt = xs_tcp_release_xprt,
2547 .alloc_slot = xprt_lock_and_alloc_slot,
2548 .rpcbind = rpcb_getport_async,
2549 .set_port = xs_set_port,
2550 .connect = xs_connect,
2551 .buf_alloc = rpc_malloc,
2552 .buf_free = rpc_free,
2553 .send_request = xs_tcp_send_request,
2554 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2555 .close = xs_tcp_close,
2556 .destroy = xs_destroy,
2557 .print_stats = xs_tcp_print_stats,
2561 * The rpc_xprt_ops for the server backchannel
2564 static struct rpc_xprt_ops bc_tcp_ops = {
2565 .reserve_xprt = xprt_reserve_xprt,
2566 .release_xprt = xprt_release_xprt,
2567 .alloc_slot = xprt_alloc_slot,
2568 .buf_alloc = bc_malloc,
2569 .buf_free = bc_free,
2570 .send_request = bc_send_request,
2571 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2572 .close = bc_close,
2573 .destroy = bc_destroy,
2574 .print_stats = xs_tcp_print_stats,
2577 static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2579 static const struct sockaddr_in sin = {
2580 .sin_family = AF_INET,
2581 .sin_addr.s_addr = htonl(INADDR_ANY),
2583 static const struct sockaddr_in6 sin6 = {
2584 .sin6_family = AF_INET6,
2585 .sin6_addr = IN6ADDR_ANY_INIT,
2588 switch (family) {
2589 case AF_LOCAL:
2590 break;
2591 case AF_INET:
2592 memcpy(sap, &sin, sizeof(sin));
2593 break;
2594 case AF_INET6:
2595 memcpy(sap, &sin6, sizeof(sin6));
2596 break;
2597 default:
2598 dprintk("RPC: %s: Bad address family\n", __func__);
2599 return -EAFNOSUPPORT;
2601 return 0;
2604 static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2605 unsigned int slot_table_size,
2606 unsigned int max_slot_table_size)
2608 struct rpc_xprt *xprt;
2609 struct sock_xprt *new;
2611 if (args->addrlen > sizeof(xprt->addr)) {
2612 dprintk("RPC: xs_setup_xprt: address too large\n");
2613 return ERR_PTR(-EBADF);
2616 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2617 max_slot_table_size);
2618 if (xprt == NULL) {
2619 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2620 "rpc_xprt\n");
2621 return ERR_PTR(-ENOMEM);
2624 new = container_of(xprt, struct sock_xprt, xprt);
2625 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2626 xprt->addrlen = args->addrlen;
2627 if (args->srcaddr)
2628 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
2629 else {
2630 int err;
2631 err = xs_init_anyaddr(args->dstaddr->sa_family,
2632 (struct sockaddr *)&new->srcaddr);
2633 if (err != 0) {
2634 xprt_free(xprt);
2635 return ERR_PTR(err);
2639 return xprt;
2642 static const struct rpc_timeout xs_local_default_timeout = {
2643 .to_initval = 10 * HZ,
2644 .to_maxval = 10 * HZ,
2645 .to_retries = 2,
2649 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2650 * @args: rpc transport creation arguments
2652 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2654 static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2656 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2657 struct sock_xprt *transport;
2658 struct rpc_xprt *xprt;
2659 struct rpc_xprt *ret;
2661 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2662 xprt_max_tcp_slot_table_entries);
2663 if (IS_ERR(xprt))
2664 return xprt;
2665 transport = container_of(xprt, struct sock_xprt, xprt);
2667 xprt->prot = 0;
2668 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2669 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2671 xprt->bind_timeout = XS_BIND_TO;
2672 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2673 xprt->idle_timeout = XS_IDLE_DISC_TO;
2675 xprt->ops = &xs_local_ops;
2676 xprt->timeout = &xs_local_default_timeout;
2678 INIT_DELAYED_WORK(&transport->connect_worker,
2679 xs_dummy_setup_socket);
2681 switch (sun->sun_family) {
2682 case AF_LOCAL:
2683 if (sun->sun_path[0] != '/') {
2684 dprintk("RPC: bad AF_LOCAL address: %s\n",
2685 sun->sun_path);
2686 ret = ERR_PTR(-EINVAL);
2687 goto out_err;
2689 xprt_set_bound(xprt);
2690 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
2691 ret = ERR_PTR(xs_local_setup_socket(transport));
2692 if (ret)
2693 goto out_err;
2694 break;
2695 default:
2696 ret = ERR_PTR(-EAFNOSUPPORT);
2697 goto out_err;
2700 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2701 xprt->address_strings[RPC_DISPLAY_ADDR]);
2703 if (try_module_get(THIS_MODULE))
2704 return xprt;
2705 ret = ERR_PTR(-EINVAL);
2706 out_err:
2707 xprt_free(xprt);
2708 return ret;
2711 static const struct rpc_timeout xs_udp_default_timeout = {
2712 .to_initval = 5 * HZ,
2713 .to_maxval = 30 * HZ,
2714 .to_increment = 5 * HZ,
2715 .to_retries = 5,
2719 * xs_setup_udp - Set up transport to use a UDP socket
2720 * @args: rpc transport creation arguments
2723 static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2725 struct sockaddr *addr = args->dstaddr;
2726 struct rpc_xprt *xprt;
2727 struct sock_xprt *transport;
2728 struct rpc_xprt *ret;
2730 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2731 xprt_udp_slot_table_entries);
2732 if (IS_ERR(xprt))
2733 return xprt;
2734 transport = container_of(xprt, struct sock_xprt, xprt);
2736 xprt->prot = IPPROTO_UDP;
2737 xprt->tsh_size = 0;
2738 /* XXX: header size can vary due to auth type, IPv6, etc. */
2739 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2741 xprt->bind_timeout = XS_BIND_TO;
2742 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2743 xprt->idle_timeout = XS_IDLE_DISC_TO;
2745 xprt->ops = &xs_udp_ops;
2747 xprt->timeout = &xs_udp_default_timeout;
2749 switch (addr->sa_family) {
2750 case AF_INET:
2751 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2752 xprt_set_bound(xprt);
2754 INIT_DELAYED_WORK(&transport->connect_worker,
2755 xs_udp_setup_socket);
2756 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
2757 break;
2758 case AF_INET6:
2759 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2760 xprt_set_bound(xprt);
2762 INIT_DELAYED_WORK(&transport->connect_worker,
2763 xs_udp_setup_socket);
2764 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
2765 break;
2766 default:
2767 ret = ERR_PTR(-EAFNOSUPPORT);
2768 goto out_err;
2771 if (xprt_bound(xprt))
2772 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2773 xprt->address_strings[RPC_DISPLAY_ADDR],
2774 xprt->address_strings[RPC_DISPLAY_PORT],
2775 xprt->address_strings[RPC_DISPLAY_PROTO]);
2776 else
2777 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2778 xprt->address_strings[RPC_DISPLAY_ADDR],
2779 xprt->address_strings[RPC_DISPLAY_PROTO]);
2781 if (try_module_get(THIS_MODULE))
2782 return xprt;
2783 ret = ERR_PTR(-EINVAL);
2784 out_err:
2785 xprt_free(xprt);
2786 return ret;
2789 static const struct rpc_timeout xs_tcp_default_timeout = {
2790 .to_initval = 60 * HZ,
2791 .to_maxval = 60 * HZ,
2792 .to_retries = 2,
2796 * xs_setup_tcp - Set up transport to use a TCP socket
2797 * @args: rpc transport creation arguments
2800 static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2802 struct sockaddr *addr = args->dstaddr;
2803 struct rpc_xprt *xprt;
2804 struct sock_xprt *transport;
2805 struct rpc_xprt *ret;
2806 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2808 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2809 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
2811 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2812 max_slot_table_size);
2813 if (IS_ERR(xprt))
2814 return xprt;
2815 transport = container_of(xprt, struct sock_xprt, xprt);
2817 xprt->prot = IPPROTO_TCP;
2818 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2819 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2821 xprt->bind_timeout = XS_BIND_TO;
2822 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2823 xprt->idle_timeout = XS_IDLE_DISC_TO;
2825 xprt->ops = &xs_tcp_ops;
2826 xprt->timeout = &xs_tcp_default_timeout;
2828 switch (addr->sa_family) {
2829 case AF_INET:
2830 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2831 xprt_set_bound(xprt);
2833 INIT_DELAYED_WORK(&transport->connect_worker,
2834 xs_tcp_setup_socket);
2835 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
2836 break;
2837 case AF_INET6:
2838 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2839 xprt_set_bound(xprt);
2841 INIT_DELAYED_WORK(&transport->connect_worker,
2842 xs_tcp_setup_socket);
2843 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
2844 break;
2845 default:
2846 ret = ERR_PTR(-EAFNOSUPPORT);
2847 goto out_err;
2850 if (xprt_bound(xprt))
2851 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2852 xprt->address_strings[RPC_DISPLAY_ADDR],
2853 xprt->address_strings[RPC_DISPLAY_PORT],
2854 xprt->address_strings[RPC_DISPLAY_PROTO]);
2855 else
2856 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2857 xprt->address_strings[RPC_DISPLAY_ADDR],
2858 xprt->address_strings[RPC_DISPLAY_PROTO]);
2861 if (try_module_get(THIS_MODULE))
2862 return xprt;
2863 ret = ERR_PTR(-EINVAL);
2864 out_err:
2865 xprt_free(xprt);
2866 return ret;
2870 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2871 * @args: rpc transport creation arguments
2874 static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2876 struct sockaddr *addr = args->dstaddr;
2877 struct rpc_xprt *xprt;
2878 struct sock_xprt *transport;
2879 struct svc_sock *bc_sock;
2880 struct rpc_xprt *ret;
2882 if (args->bc_xprt->xpt_bc_xprt) {
2884 * This server connection already has a backchannel
2885 * export; we can't create a new one, as we wouldn't be
2886 * able to match replies based on xid any more. So,
2887 * reuse the already-existing one:
2889 return args->bc_xprt->xpt_bc_xprt;
2891 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2892 xprt_tcp_slot_table_entries);
2893 if (IS_ERR(xprt))
2894 return xprt;
2895 transport = container_of(xprt, struct sock_xprt, xprt);
2897 xprt->prot = IPPROTO_TCP;
2898 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2899 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2900 xprt->timeout = &xs_tcp_default_timeout;
2902 /* backchannel */
2903 xprt_set_bound(xprt);
2904 xprt->bind_timeout = 0;
2905 xprt->reestablish_timeout = 0;
2906 xprt->idle_timeout = 0;
2908 xprt->ops = &bc_tcp_ops;
2910 switch (addr->sa_family) {
2911 case AF_INET:
2912 xs_format_peer_addresses(xprt, "tcp",
2913 RPCBIND_NETID_TCP);
2914 break;
2915 case AF_INET6:
2916 xs_format_peer_addresses(xprt, "tcp",
2917 RPCBIND_NETID_TCP6);
2918 break;
2919 default:
2920 ret = ERR_PTR(-EAFNOSUPPORT);
2921 goto out_err;
2924 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2925 xprt->address_strings[RPC_DISPLAY_ADDR],
2926 xprt->address_strings[RPC_DISPLAY_PORT],
2927 xprt->address_strings[RPC_DISPLAY_PROTO]);
2930 * Once we've associated a backchannel xprt with a connection,
2931 * we want to keep it around as long as long as the connection
2932 * lasts, in case we need to start using it for a backchannel
2933 * again; this reference won't be dropped until bc_xprt is
2934 * destroyed.
2936 xprt_get(xprt);
2937 args->bc_xprt->xpt_bc_xprt = xprt;
2938 xprt->bc_xprt = args->bc_xprt;
2939 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2940 transport->sock = bc_sock->sk_sock;
2941 transport->inet = bc_sock->sk_sk;
2944 * Since we don't want connections for the backchannel, we set
2945 * the xprt status to connected
2947 xprt_set_connected(xprt);
2950 if (try_module_get(THIS_MODULE))
2951 return xprt;
2952 xprt_put(xprt);
2953 ret = ERR_PTR(-EINVAL);
2954 out_err:
2955 xprt_free(xprt);
2956 return ret;
2959 static struct xprt_class xs_local_transport = {
2960 .list = LIST_HEAD_INIT(xs_local_transport.list),
2961 .name = "named UNIX socket",
2962 .owner = THIS_MODULE,
2963 .ident = XPRT_TRANSPORT_LOCAL,
2964 .setup = xs_setup_local,
2967 static struct xprt_class xs_udp_transport = {
2968 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2969 .name = "udp",
2970 .owner = THIS_MODULE,
2971 .ident = XPRT_TRANSPORT_UDP,
2972 .setup = xs_setup_udp,
2975 static struct xprt_class xs_tcp_transport = {
2976 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2977 .name = "tcp",
2978 .owner = THIS_MODULE,
2979 .ident = XPRT_TRANSPORT_TCP,
2980 .setup = xs_setup_tcp,
2983 static struct xprt_class xs_bc_tcp_transport = {
2984 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2985 .name = "tcp NFSv4.1 backchannel",
2986 .owner = THIS_MODULE,
2987 .ident = XPRT_TRANSPORT_BC_TCP,
2988 .setup = xs_setup_bc_tcp,
2992 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2995 int init_socket_xprt(void)
2997 #ifdef RPC_DEBUG
2998 if (!sunrpc_table_header)
2999 sunrpc_table_header = register_sysctl_table(sunrpc_table);
3000 #endif
3002 xprt_register_transport(&xs_local_transport);
3003 xprt_register_transport(&xs_udp_transport);
3004 xprt_register_transport(&xs_tcp_transport);
3005 xprt_register_transport(&xs_bc_tcp_transport);
3007 return 0;
3011 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3014 void cleanup_socket_xprt(void)
3016 #ifdef RPC_DEBUG
3017 if (sunrpc_table_header) {
3018 unregister_sysctl_table(sunrpc_table_header);
3019 sunrpc_table_header = NULL;
3021 #endif
3023 xprt_unregister_transport(&xs_local_transport);
3024 xprt_unregister_transport(&xs_udp_transport);
3025 xprt_unregister_transport(&xs_tcp_transport);
3026 xprt_unregister_transport(&xs_bc_tcp_transport);
3029 static int param_set_uint_minmax(const char *val,
3030 const struct kernel_param *kp,
3031 unsigned int min, unsigned int max)
3033 unsigned long num;
3034 int ret;
3036 if (!val)
3037 return -EINVAL;
3038 ret = strict_strtoul(val, 0, &num);
3039 if (ret == -EINVAL || num < min || num > max)
3040 return -EINVAL;
3041 *((unsigned int *)kp->arg) = num;
3042 return 0;
3045 static int param_set_portnr(const char *val, const struct kernel_param *kp)
3047 return param_set_uint_minmax(val, kp,
3048 RPC_MIN_RESVPORT,
3049 RPC_MAX_RESVPORT);
3052 static struct kernel_param_ops param_ops_portnr = {
3053 .set = param_set_portnr,
3054 .get = param_get_uint,
3057 #define param_check_portnr(name, p) \
3058 __param_check(name, p, unsigned int);
3060 module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3061 module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3063 static int param_set_slot_table_size(const char *val,
3064 const struct kernel_param *kp)
3066 return param_set_uint_minmax(val, kp,
3067 RPC_MIN_SLOT_TABLE,
3068 RPC_MAX_SLOT_TABLE);
3071 static struct kernel_param_ops param_ops_slot_table_size = {
3072 .set = param_set_slot_table_size,
3073 .get = param_get_uint,
3076 #define param_check_slot_table_size(name, p) \
3077 __param_check(name, p, unsigned int);
3079 static int param_set_max_slot_table_size(const char *val,
3080 const struct kernel_param *kp)
3082 return param_set_uint_minmax(val, kp,
3083 RPC_MIN_SLOT_TABLE,
3084 RPC_MAX_SLOT_TABLE_LIMIT);
3087 static struct kernel_param_ops param_ops_max_slot_table_size = {
3088 .set = param_set_max_slot_table_size,
3089 .get = param_get_uint,
3092 #define param_check_max_slot_table_size(name, p) \
3093 __param_check(name, p, unsigned int);
3095 module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3096 slot_table_size, 0644);
3097 module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3098 max_slot_table_size, 0644);
3099 module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3100 slot_table_size, 0644);