blob: 4797a4608c07028ac73c05c6a5122058c51ef3cd [file] [log] [blame]
Chuck Levera246b012005-08-11 16:25:23 -04001/*
2 * linux/net/sunrpc/xprtsock.c
3 *
4 * Client-side transport implementation for sockets.
5 *
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>
10 *
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>
Chuck Lever55aa4f52005-08-11 16:25:47 -040014 *
15 * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
Chuck Levera246b012005-08-11 16:25:23 -040016 */
17
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>
25#include <linux/in.h>
26#include <linux/net.h>
27#include <linux/mm.h>
28#include <linux/udp.h>
29#include <linux/tcp.h>
30#include <linux/sunrpc/clnt.h>
Chuck Lever02107142006-01-03 09:55:49 +010031#include <linux/sunrpc/sched.h>
Chuck Levera246b012005-08-11 16:25:23 -040032#include <linux/file.h>
33
34#include <net/sock.h>
35#include <net/checksum.h>
36#include <net/udp.h>
37#include <net/tcp.h>
38
Chuck Lever9903cd12005-08-11 16:25:26 -040039/*
Chuck Leverc556b752005-11-01 12:24:48 -050040 * xprtsock tunables
41 */
42unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
43unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
44
45unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
46unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
47
48/*
Chuck Lever262965f2005-08-11 16:25:56 -040049 * How many times to try sending a request on a socket before waiting
50 * for the socket buffer to clear.
51 */
52#define XS_SENDMSG_RETRY (10U)
53
Chuck Lever03bf4b72005-08-25 16:25:55 -070054/*
55 * Time out for an RPC UDP socket connect. UDP socket connects are
56 * synchronous, but we set a timeout anyway in case of resource
57 * exhaustion on the local host.
58 */
59#define XS_UDP_CONN_TO (5U * HZ)
60
61/*
62 * Wait duration for an RPC TCP connection to be established. Solaris
63 * NFS over TCP uses 60 seconds, for example, which is in line with how
64 * long a server takes to reboot.
65 */
66#define XS_TCP_CONN_TO (60U * HZ)
67
68/*
69 * Wait duration for a reply from the RPC portmapper.
70 */
71#define XS_BIND_TO (60U * HZ)
72
73/*
74 * Delay if a UDP socket connect error occurs. This is most likely some
75 * kind of resource problem on the local host.
76 */
77#define XS_UDP_REEST_TO (2U * HZ)
78
79/*
80 * The reestablish timeout allows clients to delay for a bit before attempting
81 * to reconnect to a server that just dropped our connection.
82 *
83 * We implement an exponential backoff when trying to reestablish a TCP
84 * transport connection with the server. Some servers like to drop a TCP
85 * connection when they are overworked, so we start with a short timeout and
86 * increase over time if the server is down or not responding.
87 */
88#define XS_TCP_INIT_REEST_TO (3U * HZ)
89#define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
90
91/*
92 * TCP idle timeout; client drops the transport socket if it is idle
93 * for this long. Note that we also timeout UDP sockets to prevent
94 * holding port numbers when there is no RPC traffic.
95 */
96#define XS_IDLE_DISC_TO (5U * 60 * HZ)
97
Chuck Levera246b012005-08-11 16:25:23 -040098#ifdef RPC_DEBUG
99# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400100# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400101#endif
102
Chuck Levera246b012005-08-11 16:25:23 -0400103#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400104static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400105{
Chuck Lever9903cd12005-08-11 16:25:26 -0400106 u8 *buf = (u8 *) packet;
107 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400108
109 dprintk("RPC: %s\n", msg);
110 for (j = 0; j < count && j < 128; j += 4) {
111 if (!(j & 31)) {
112 if (j)
113 dprintk("\n");
114 dprintk("0x%04x ", j);
115 }
116 dprintk("%02x%02x%02x%02x ",
117 buf[j], buf[j+1], buf[j+2], buf[j+3]);
118 }
119 dprintk("\n");
120}
121#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400122static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400123{
124 /* NOP */
125}
126#endif
127
Chuck Leverffc2e512006-12-05 16:35:11 -0500128struct sock_xprt {
129 struct rpc_xprt xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500130
131 /*
132 * Network layer
133 */
134 struct socket * sock;
135 struct sock * inet;
Chuck Lever51971132006-12-05 16:35:19 -0500136
137 /*
138 * State of TCP reply receive
139 */
140 __be32 tcp_fraghdr,
141 tcp_xid;
142
143 u32 tcp_offset,
144 tcp_reclen;
145
146 unsigned long tcp_copied,
147 tcp_flags;
Chuck Leverc8475462006-12-05 16:35:26 -0500148
149 /*
150 * Connection of transports
151 */
152 struct work_struct connect_worker;
153 unsigned short port;
Chuck Leverffc2e512006-12-05 16:35:11 -0500154};
155
Chuck Levere136d092006-12-05 16:35:23 -0500156/*
157 * TCP receive state flags
158 */
159#define TCP_RCV_LAST_FRAG (1UL << 0)
160#define TCP_RCV_COPY_FRAGHDR (1UL << 1)
161#define TCP_RCV_COPY_XID (1UL << 2)
162#define TCP_RCV_COPY_DATA (1UL << 3)
163
Chuck Leveredb267a2006-08-22 20:06:18 -0400164static void xs_format_peer_addresses(struct rpc_xprt *xprt)
165{
166 struct sockaddr_in *addr = (struct sockaddr_in *) &xprt->addr;
167 char *buf;
168
169 buf = kzalloc(20, GFP_KERNEL);
170 if (buf) {
171 snprintf(buf, 20, "%u.%u.%u.%u",
172 NIPQUAD(addr->sin_addr.s_addr));
173 }
174 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
175
176 buf = kzalloc(8, GFP_KERNEL);
177 if (buf) {
178 snprintf(buf, 8, "%u",
179 ntohs(addr->sin_port));
180 }
181 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
182
183 if (xprt->prot == IPPROTO_UDP)
184 xprt->address_strings[RPC_DISPLAY_PROTO] = "udp";
185 else
186 xprt->address_strings[RPC_DISPLAY_PROTO] = "tcp";
187
188 buf = kzalloc(48, GFP_KERNEL);
189 if (buf) {
190 snprintf(buf, 48, "addr=%u.%u.%u.%u port=%u proto=%s",
191 NIPQUAD(addr->sin_addr.s_addr),
192 ntohs(addr->sin_port),
193 xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
194 }
195 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
196}
197
198static void xs_free_peer_addresses(struct rpc_xprt *xprt)
199{
200 kfree(xprt->address_strings[RPC_DISPLAY_ADDR]);
201 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
202 kfree(xprt->address_strings[RPC_DISPLAY_ALL]);
203}
204
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400205#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
206
Trond Myklebust24c56842006-10-17 15:06:22 -0400207static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400208{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400209 struct msghdr msg = {
210 .msg_name = addr,
211 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400212 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
213 };
214 struct kvec iov = {
215 .iov_base = vec->iov_base + base,
216 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400217 };
218
Trond Myklebust24c56842006-10-17 15:06:22 -0400219 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400220 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
221 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
222}
223
Trond Myklebust24c56842006-10-17 15:06:22 -0400224static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400225{
Trond Myklebust24c56842006-10-17 15:06:22 -0400226 struct page **ppage;
227 unsigned int remainder;
228 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400229
Trond Myklebust24c56842006-10-17 15:06:22 -0400230 remainder = xdr->page_len - base;
231 base += xdr->page_base;
232 ppage = xdr->pages + (base >> PAGE_SHIFT);
233 base &= ~PAGE_MASK;
234 for(;;) {
235 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
236 int flags = XS_SENDMSG_FLAGS;
237
238 remainder -= len;
239 if (remainder != 0 || more)
240 flags |= MSG_MORE;
241 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
242 if (remainder == 0 || err != len)
243 break;
244 sent += err;
245 ppage++;
246 base = 0;
247 }
248 if (sent == 0)
249 return err;
250 if (err > 0)
251 sent += err;
252 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400253}
254
Chuck Lever9903cd12005-08-11 16:25:26 -0400255/**
256 * xs_sendpages - write pages directly to a socket
257 * @sock: socket to send on
258 * @addr: UDP only -- address of destination
259 * @addrlen: UDP only -- length of destination address
260 * @xdr: buffer containing this request
261 * @base: starting position in the buffer
262 *
Chuck Levera246b012005-08-11 16:25:23 -0400263 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400264static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
Chuck Levera246b012005-08-11 16:25:23 -0400265{
Trond Myklebust24c56842006-10-17 15:06:22 -0400266 unsigned int remainder = xdr->len - base;
267 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400268
Chuck Lever262965f2005-08-11 16:25:56 -0400269 if (unlikely(!sock))
270 return -ENOTCONN;
271
272 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400273 if (base != 0) {
274 addr = NULL;
275 addrlen = 0;
276 }
Chuck Lever262965f2005-08-11 16:25:56 -0400277
Trond Myklebust24c56842006-10-17 15:06:22 -0400278 if (base < xdr->head[0].iov_len || addr != NULL) {
279 unsigned int len = xdr->head[0].iov_len - base;
280 remainder -= len;
281 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
282 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400283 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400284 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400285 base = 0;
286 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400287 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400288
Trond Myklebust24c56842006-10-17 15:06:22 -0400289 if (base < xdr->page_len) {
290 unsigned int len = xdr->page_len - base;
291 remainder -= len;
292 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
293 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400294 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400295 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400296 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400297 } else
298 base -= xdr->page_len;
299
300 if (base >= xdr->tail[0].iov_len)
301 return sent;
302 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400303out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400304 if (sent == 0)
305 return err;
306 if (err > 0)
307 sent += err;
308 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400309}
310
Chuck Lever9903cd12005-08-11 16:25:26 -0400311/**
Chuck Lever262965f2005-08-11 16:25:56 -0400312 * xs_nospace - place task on wait queue if transmit was incomplete
313 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400314 *
Chuck Levera246b012005-08-11 16:25:23 -0400315 */
Chuck Lever262965f2005-08-11 16:25:56 -0400316static void xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400317{
Chuck Lever262965f2005-08-11 16:25:56 -0400318 struct rpc_rqst *req = task->tk_rqstp;
319 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500320 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400321
Chuck Lever262965f2005-08-11 16:25:56 -0400322 dprintk("RPC: %4d xmit incomplete (%u left of %u)\n",
323 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
324 req->rq_slen);
325
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500326 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
Chuck Lever262965f2005-08-11 16:25:56 -0400327 /* Protect against races with write_space */
328 spin_lock_bh(&xprt->transport_lock);
329
330 /* Don't race with disconnect */
331 if (!xprt_connected(xprt))
332 task->tk_status = -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500333 else if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
Chuck Lever262965f2005-08-11 16:25:56 -0400334 xprt_wait_for_buffer_space(task);
335
336 spin_unlock_bh(&xprt->transport_lock);
337 } else
338 /* Keep holding the socket if it is blocked */
339 rpc_delay(task, HZ>>4);
340}
341
342/**
343 * xs_udp_send_request - write an RPC request to a UDP socket
344 * @task: address of RPC task that manages the state of an RPC request
345 *
346 * Return values:
347 * 0: The request has been sent
348 * EAGAIN: The socket was blocked, please call again later to
349 * complete the request
350 * ENOTCONN: Caller needs to invoke connect logic then call again
351 * other: Some other error occured, the request was not sent
352 */
353static int xs_udp_send_request(struct rpc_task *task)
354{
355 struct rpc_rqst *req = task->tk_rqstp;
356 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500357 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400358 struct xdr_buf *xdr = &req->rq_snd_buf;
359 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400360
Chuck Lever9903cd12005-08-11 16:25:26 -0400361 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400362 req->rq_svec->iov_base,
363 req->rq_svec->iov_len);
364
Chuck Lever262965f2005-08-11 16:25:56 -0400365 req->rq_xtime = jiffies;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500366 status = xs_sendpages(transport->sock,
367 (struct sockaddr *) &xprt->addr,
368 xprt->addrlen, xdr,
369 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400370
Chuck Lever262965f2005-08-11 16:25:56 -0400371 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
372 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400373
Chuck Lever262965f2005-08-11 16:25:56 -0400374 if (likely(status >= (int) req->rq_slen))
375 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400376
Chuck Lever262965f2005-08-11 16:25:56 -0400377 /* Still some bytes left; set up for a retry later. */
378 if (status > 0)
379 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400380
Chuck Lever262965f2005-08-11 16:25:56 -0400381 switch (status) {
382 case -ENETUNREACH:
383 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400384 case -ECONNREFUSED:
385 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400386 * prompts ECONNREFUSED. */
Chuck Levera246b012005-08-11 16:25:23 -0400387 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400388 case -EAGAIN:
389 xs_nospace(task);
Chuck Levera246b012005-08-11 16:25:23 -0400390 break;
391 default:
Chuck Lever262965f2005-08-11 16:25:56 -0400392 dprintk("RPC: sendmsg returned unrecognized error %d\n",
393 -status);
Chuck Lever9903cd12005-08-11 16:25:26 -0400394 break;
Chuck Levera246b012005-08-11 16:25:23 -0400395 }
Chuck Lever262965f2005-08-11 16:25:56 -0400396
397 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400398}
399
Chuck Lever808012f2005-08-25 16:25:49 -0700400static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
401{
402 u32 reclen = buf->len - sizeof(rpc_fraghdr);
403 rpc_fraghdr *base = buf->head[0].iov_base;
404 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
405}
406
Chuck Lever9903cd12005-08-11 16:25:26 -0400407/**
Chuck Lever262965f2005-08-11 16:25:56 -0400408 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400409 * @task: address of RPC task that manages the state of an RPC request
410 *
411 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400412 * 0: The request has been sent
413 * EAGAIN: The socket was blocked, please call again later to
414 * complete the request
415 * ENOTCONN: Caller needs to invoke connect logic then call again
416 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400417 *
418 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400419 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400420 */
Chuck Lever262965f2005-08-11 16:25:56 -0400421static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400422{
423 struct rpc_rqst *req = task->tk_rqstp;
424 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500425 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400426 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Levera246b012005-08-11 16:25:23 -0400427 int status, retry = 0;
428
Chuck Lever808012f2005-08-25 16:25:49 -0700429 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400430
Chuck Lever262965f2005-08-11 16:25:56 -0400431 xs_pktdump("packet data:",
432 req->rq_svec->iov_base,
433 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400434
435 /* Continue transmitting the packet/record. We must be careful
436 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400437 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400438 while (1) {
439 req->rq_xtime = jiffies;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500440 status = xs_sendpages(transport->sock,
441 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400442
Chuck Lever262965f2005-08-11 16:25:56 -0400443 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
444 xdr->len - req->rq_bytes_sent, status);
445
446 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400447 break;
448
Chuck Lever262965f2005-08-11 16:25:56 -0400449 /* If we've sent the entire packet, immediately
450 * reset the count of bytes sent. */
451 req->rq_bytes_sent += status;
Chuck Leveref759a22006-03-20 13:44:17 -0500452 task->tk_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400453 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
454 req->rq_bytes_sent = 0;
455 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400456 }
457
Chuck Levera246b012005-08-11 16:25:23 -0400458 status = -EAGAIN;
Chuck Lever262965f2005-08-11 16:25:56 -0400459 if (retry++ > XS_SENDMSG_RETRY)
Chuck Levera246b012005-08-11 16:25:23 -0400460 break;
461 }
462
Chuck Lever262965f2005-08-11 16:25:56 -0400463 switch (status) {
464 case -EAGAIN:
465 xs_nospace(task);
466 break;
467 case -ECONNREFUSED:
468 case -ECONNRESET:
469 case -ENOTCONN:
470 case -EPIPE:
471 status = -ENOTCONN;
472 break;
473 default:
474 dprintk("RPC: sendmsg returned unrecognized error %d\n",
475 -status);
Chuck Lever43118c22005-08-25 16:25:49 -0700476 xprt_disconnect(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400477 break;
Chuck Levera246b012005-08-11 16:25:23 -0400478 }
Chuck Lever262965f2005-08-11 16:25:56 -0400479
Chuck Levera246b012005-08-11 16:25:23 -0400480 return status;
481}
482
Chuck Lever9903cd12005-08-11 16:25:26 -0400483/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400484 * xs_tcp_release_xprt - clean up after a tcp transmission
485 * @xprt: transport
486 * @task: rpc task
487 *
488 * This cleans up if an error causes us to abort the transmission of a request.
489 * In this case, the socket may need to be reset in order to avoid confusing
490 * the server.
491 */
492static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
493{
494 struct rpc_rqst *req;
495
496 if (task != xprt->snd_task)
497 return;
498 if (task == NULL)
499 goto out_release;
500 req = task->tk_rqstp;
501 if (req->rq_bytes_sent == 0)
502 goto out_release;
503 if (req->rq_bytes_sent == req->rq_snd_buf.len)
504 goto out_release;
505 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
506out_release:
507 xprt_release_xprt(xprt, task);
508}
509
510/**
Chuck Lever9903cd12005-08-11 16:25:26 -0400511 * xs_close - close a socket
512 * @xprt: transport
513 *
Chuck Lever3167e122005-08-25 16:25:55 -0700514 * This is used when all requests are complete; ie, no DRC state remains
515 * on the server we want to save.
Chuck Levera246b012005-08-11 16:25:23 -0400516 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400517static void xs_close(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400518{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500519 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
520 struct socket *sock = transport->sock;
521 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400522
523 if (!sk)
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100524 goto clear_close_wait;
Chuck Levera246b012005-08-11 16:25:23 -0400525
Chuck Lever9903cd12005-08-11 16:25:26 -0400526 dprintk("RPC: xs_close xprt %p\n", xprt);
527
Chuck Levera246b012005-08-11 16:25:23 -0400528 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500529 transport->inet = NULL;
530 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400531
Chuck Lever9903cd12005-08-11 16:25:26 -0400532 sk->sk_user_data = NULL;
533 sk->sk_data_ready = xprt->old_data_ready;
Chuck Levera246b012005-08-11 16:25:23 -0400534 sk->sk_state_change = xprt->old_state_change;
Chuck Lever9903cd12005-08-11 16:25:26 -0400535 sk->sk_write_space = xprt->old_write_space;
Chuck Levera246b012005-08-11 16:25:23 -0400536 write_unlock_bh(&sk->sk_callback_lock);
537
Chuck Lever9903cd12005-08-11 16:25:26 -0400538 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400539
540 sock_release(sock);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100541clear_close_wait:
542 smp_mb__before_clear_bit();
543 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
544 smp_mb__after_clear_bit();
Chuck Levera246b012005-08-11 16:25:23 -0400545}
546
Chuck Lever9903cd12005-08-11 16:25:26 -0400547/**
548 * xs_destroy - prepare to shutdown a transport
549 * @xprt: doomed transport
550 *
551 */
552static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400553{
Chuck Leverc8475462006-12-05 16:35:26 -0500554 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
555
Chuck Lever9903cd12005-08-11 16:25:26 -0400556 dprintk("RPC: xs_destroy xprt %p\n", xprt);
557
Chuck Leverc8475462006-12-05 16:35:26 -0500558 cancel_delayed_work(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400559 flush_scheduled_work();
560
561 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400562 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400563 xs_free_peer_addresses(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400564 kfree(xprt->slot);
Chuck Leverc8541ec2006-10-17 14:44:27 -0400565 kfree(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400566}
567
Chuck Lever9903cd12005-08-11 16:25:26 -0400568static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400569{
Chuck Lever9903cd12005-08-11 16:25:26 -0400570 return (struct rpc_xprt *) sk->sk_user_data;
571}
572
573/**
574 * xs_udp_data_ready - "data ready" callback for UDP sockets
575 * @sk: socket with data to read
576 * @len: how much data to read
577 *
578 */
579static void xs_udp_data_ready(struct sock *sk, int len)
580{
581 struct rpc_task *task;
582 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400583 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400584 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400585 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700586 u32 _xid;
587 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400588
589 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400590 dprintk("RPC: xs_udp_data_ready...\n");
591 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400592 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400593
594 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
595 goto out;
596
597 if (xprt->shutdown)
598 goto dropit;
599
600 repsize = skb->len - sizeof(struct udphdr);
601 if (repsize < 4) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400602 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400603 goto dropit;
604 }
605
606 /* Copy the XID from the skb... */
607 xp = skb_header_pointer(skb, sizeof(struct udphdr),
608 sizeof(_xid), &_xid);
609 if (xp == NULL)
610 goto dropit;
611
612 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400613 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400614 rovr = xprt_lookup_rqst(xprt, *xp);
615 if (!rovr)
616 goto out_unlock;
617 task = rovr->rq_task;
618
Chuck Levera246b012005-08-11 16:25:23 -0400619 if ((copied = rovr->rq_private_buf.buflen) > repsize)
620 copied = repsize;
621
622 /* Suck it into the iovec, verify checksum if not done by hw. */
623 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb))
624 goto out_unlock;
625
626 /* Something worked... */
627 dst_confirm(skb->dst);
628
Chuck Lever1570c1e2005-08-25 16:25:52 -0700629 xprt_adjust_cwnd(task, copied);
630 xprt_update_rtt(task);
631 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400632
633 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400634 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400635 dropit:
636 skb_free_datagram(sk, skb);
637 out:
638 read_unlock(&sk->sk_callback_lock);
639}
640
Chuck Lever9903cd12005-08-11 16:25:26 -0400641static inline size_t xs_tcp_copy_data(skb_reader_t *desc, void *p, size_t len)
Chuck Levera246b012005-08-11 16:25:23 -0400642{
643 if (len > desc->count)
644 len = desc->count;
645 if (skb_copy_bits(desc->skb, desc->offset, p, len)) {
646 dprintk("RPC: failed to copy %zu bytes from skb. %zu bytes remain\n",
647 len, desc->count);
648 return 0;
649 }
650 desc->offset += len;
651 desc->count -= len;
652 dprintk("RPC: copied %zu bytes from skb. %zu bytes remain\n",
653 len, desc->count);
654 return len;
655}
656
Chuck Lever9903cd12005-08-11 16:25:26 -0400657static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400658{
Chuck Lever51971132006-12-05 16:35:19 -0500659 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400660 size_t len, used;
661 char *p;
662
Chuck Lever51971132006-12-05 16:35:19 -0500663 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
664 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9903cd12005-08-11 16:25:26 -0400665 used = xs_tcp_copy_data(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500666 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400667 if (used != len)
668 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700669
Chuck Lever51971132006-12-05 16:35:19 -0500670 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
671 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -0500672 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -0400673 else
Chuck Levere136d092006-12-05 16:35:23 -0500674 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -0500675 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -0700676
Chuck Levere136d092006-12-05 16:35:23 -0500677 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500678 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700679
Chuck Levera246b012005-08-11 16:25:23 -0400680 /* Sanity check of the record length */
Chuck Lever51971132006-12-05 16:35:19 -0500681 if (unlikely(transport->tcp_reclen < 4)) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400682 dprintk("RPC: invalid TCP record fragment length\n");
Chuck Levera246b012005-08-11 16:25:23 -0400683 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400684 return;
Chuck Levera246b012005-08-11 16:25:23 -0400685 }
686 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -0500687 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400688}
689
Chuck Lever51971132006-12-05 16:35:19 -0500690static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400691{
Chuck Lever51971132006-12-05 16:35:19 -0500692 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500693 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500694 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -0500695 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
696 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
697 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -0500698 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400699 }
700 }
701}
702
Chuck Lever51971132006-12-05 16:35:19 -0500703static inline void xs_tcp_read_xid(struct sock_xprt *transport, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400704{
705 size_t len, used;
706 char *p;
707
Chuck Lever51971132006-12-05 16:35:19 -0500708 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -0400709 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500710 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9903cd12005-08-11 16:25:26 -0400711 used = xs_tcp_copy_data(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500712 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400713 if (used != len)
714 return;
Chuck Levere136d092006-12-05 16:35:23 -0500715 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
716 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -0500717 transport->tcp_copied = 4;
Chuck Levera246b012005-08-11 16:25:23 -0400718 dprintk("RPC: reading reply for XID %08x\n",
Chuck Lever51971132006-12-05 16:35:19 -0500719 ntohl(transport->tcp_xid));
720 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400721}
722
Chuck Lever9903cd12005-08-11 16:25:26 -0400723static inline void xs_tcp_read_request(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400724{
Chuck Lever51971132006-12-05 16:35:19 -0500725 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400726 struct rpc_rqst *req;
727 struct xdr_buf *rcvbuf;
728 size_t len;
729 ssize_t r;
730
731 /* Find and lock the request corresponding to this xid */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400732 spin_lock(&xprt->transport_lock);
Chuck Lever51971132006-12-05 16:35:19 -0500733 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
Chuck Levera246b012005-08-11 16:25:23 -0400734 if (!req) {
Chuck Levere136d092006-12-05 16:35:23 -0500735 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -0400736 dprintk("RPC: XID %08x request not found!\n",
Chuck Lever51971132006-12-05 16:35:19 -0500737 ntohl(transport->tcp_xid));
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400738 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400739 return;
740 }
741
742 rcvbuf = &req->rq_private_buf;
743 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -0500744 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Levera246b012005-08-11 16:25:23 -0400745 skb_reader_t my_desc;
746
Chuck Lever51971132006-12-05 16:35:19 -0500747 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -0400748 memcpy(&my_desc, desc, sizeof(my_desc));
749 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -0500750 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9903cd12005-08-11 16:25:26 -0400751 &my_desc, xs_tcp_copy_data);
Chuck Levera246b012005-08-11 16:25:23 -0400752 desc->count -= r;
753 desc->offset += r;
754 } else
Chuck Lever51971132006-12-05 16:35:19 -0500755 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9903cd12005-08-11 16:25:26 -0400756 desc, xs_tcp_copy_data);
Chuck Levera246b012005-08-11 16:25:23 -0400757
758 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -0500759 transport->tcp_copied += r;
760 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -0400761 }
762 if (r != len) {
763 /* Error when copying to the receive buffer,
764 * usually because we weren't able to allocate
765 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -0500766 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -0400767 * will not receive any additional updates,
768 * and time out.
769 * Any remaining data from this record will
770 * be discarded.
771 */
Chuck Levere136d092006-12-05 16:35:23 -0500772 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -0400773 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -0500774 ntohl(transport->tcp_xid));
Chuck Levera246b012005-08-11 16:25:23 -0400775 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
Chuck Lever51971132006-12-05 16:35:19 -0500776 xprt, transport->tcp_copied, transport->tcp_offset,
777 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400778 goto out;
779 }
780
781 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -0500782 ntohl(transport->tcp_xid), r);
Chuck Levera246b012005-08-11 16:25:23 -0400783 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
Chuck Lever51971132006-12-05 16:35:19 -0500784 xprt, transport->tcp_copied, transport->tcp_offset,
785 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400786
Chuck Lever51971132006-12-05 16:35:19 -0500787 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -0500788 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -0500789 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500790 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
791 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -0400792 }
793
794out:
Chuck Levere136d092006-12-05 16:35:23 -0500795 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -0500796 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400797 spin_unlock(&xprt->transport_lock);
Chuck Lever51971132006-12-05 16:35:19 -0500798 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400799}
800
Chuck Lever51971132006-12-05 16:35:19 -0500801static inline void xs_tcp_read_discard(struct sock_xprt *transport, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400802{
803 size_t len;
804
Chuck Lever51971132006-12-05 16:35:19 -0500805 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -0400806 if (len > desc->count)
807 len = desc->count;
808 desc->count -= len;
809 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -0500810 transport->tcp_offset += len;
Chuck Levera246b012005-08-11 16:25:23 -0400811 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500812 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400813}
814
Chuck Lever9903cd12005-08-11 16:25:26 -0400815static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
Chuck Levera246b012005-08-11 16:25:23 -0400816{
817 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -0500818 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400819 skb_reader_t desc = {
820 .skb = skb,
821 .offset = offset,
822 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -0400823 };
Chuck Levera246b012005-08-11 16:25:23 -0400824
Chuck Lever9903cd12005-08-11 16:25:26 -0400825 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -0400826 do {
827 /* Read in a new fragment marker if necessary */
828 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -0500829 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400830 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400831 continue;
832 }
833 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -0500834 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -0500835 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400836 continue;
837 }
838 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -0500839 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400840 xs_tcp_read_request(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400841 continue;
842 }
843 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -0500844 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400845 } while (desc.count);
Chuck Lever9903cd12005-08-11 16:25:26 -0400846 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -0400847 return len - desc.count;
848}
849
Chuck Lever9903cd12005-08-11 16:25:26 -0400850/**
851 * xs_tcp_data_ready - "data ready" callback for TCP sockets
852 * @sk: socket with data to read
853 * @bytes: how much data to read
854 *
855 */
856static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -0400857{
858 struct rpc_xprt *xprt;
859 read_descriptor_t rd_desc;
860
861 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400862 dprintk("RPC: xs_tcp_data_ready...\n");
863 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400864 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400865 if (xprt->shutdown)
866 goto out;
867
Chuck Lever9903cd12005-08-11 16:25:26 -0400868 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -0400869 rd_desc.arg.data = xprt;
870 rd_desc.count = 65536;
Chuck Lever9903cd12005-08-11 16:25:26 -0400871 tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
Chuck Levera246b012005-08-11 16:25:23 -0400872out:
873 read_unlock(&sk->sk_callback_lock);
874}
875
Chuck Lever9903cd12005-08-11 16:25:26 -0400876/**
877 * xs_tcp_state_change - callback to handle TCP socket state changes
878 * @sk: socket whose state has changed
879 *
880 */
881static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400882{
Chuck Lever9903cd12005-08-11 16:25:26 -0400883 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400884
885 read_lock(&sk->sk_callback_lock);
886 if (!(xprt = xprt_from_sock(sk)))
887 goto out;
Chuck Lever9903cd12005-08-11 16:25:26 -0400888 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400889 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
890 sk->sk_state, xprt_connected(xprt),
891 sock_flag(sk, SOCK_DEAD),
892 sock_flag(sk, SOCK_ZAPPED));
893
894 switch (sk->sk_state) {
895 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400896 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400897 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -0500898 struct sock_xprt *transport = container_of(xprt,
899 struct sock_xprt, xprt);
900
Chuck Levera246b012005-08-11 16:25:23 -0400901 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -0500902 transport->tcp_offset = 0;
903 transport->tcp_reclen = 0;
904 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -0500905 transport->tcp_flags =
906 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -0500907
Chuck Lever03bf4b72005-08-25 16:25:55 -0700908 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever44fbac22005-08-11 16:25:44 -0400909 xprt_wake_pending_tasks(xprt, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400910 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400911 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400912 break;
913 case TCP_SYN_SENT:
914 case TCP_SYN_RECV:
915 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100916 case TCP_CLOSE_WAIT:
917 /* Try to schedule an autoclose RPC calls */
918 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
919 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
920 schedule_work(&xprt->task_cleanup);
Chuck Levera246b012005-08-11 16:25:23 -0400921 default:
922 xprt_disconnect(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400923 }
924 out:
925 read_unlock(&sk->sk_callback_lock);
926}
927
Chuck Lever9903cd12005-08-11 16:25:26 -0400928/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400929 * xs_udp_write_space - callback invoked when socket buffer space
930 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -0400931 * @sk: socket whose state has changed
932 *
Chuck Levera246b012005-08-11 16:25:23 -0400933 * Called when more output buffer space is available for this socket.
934 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400935 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -0400936 * with a bunch of small requests.
937 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400938static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400939{
Chuck Levera246b012005-08-11 16:25:23 -0400940 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400941
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400942 /* from net/core/sock.c:sock_def_write_space */
943 if (sock_writeable(sk)) {
944 struct socket *sock;
945 struct rpc_xprt *xprt;
946
947 if (unlikely(!(sock = sk->sk_socket)))
Chuck Levera246b012005-08-11 16:25:23 -0400948 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400949 if (unlikely(!(xprt = xprt_from_sock(sk))))
Chuck Levera246b012005-08-11 16:25:23 -0400950 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400951 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
952 goto out;
953
954 xprt_write_space(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400955 }
956
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400957 out:
958 read_unlock(&sk->sk_callback_lock);
959}
Chuck Levera246b012005-08-11 16:25:23 -0400960
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400961/**
962 * xs_tcp_write_space - callback invoked when socket buffer space
963 * becomes available
964 * @sk: socket whose state has changed
965 *
966 * Called when more output buffer space is available for this socket.
967 * We try not to wake our writers until they can make "significant"
968 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
969 * with a bunch of small requests.
970 */
971static void xs_tcp_write_space(struct sock *sk)
972{
973 read_lock(&sk->sk_callback_lock);
974
975 /* from net/core/stream.c:sk_stream_write_space */
976 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
977 struct socket *sock;
978 struct rpc_xprt *xprt;
979
980 if (unlikely(!(sock = sk->sk_socket)))
981 goto out;
982 if (unlikely(!(xprt = xprt_from_sock(sk))))
983 goto out;
984 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
985 goto out;
986
987 xprt_write_space(xprt);
988 }
989
990 out:
Chuck Levera246b012005-08-11 16:25:23 -0400991 read_unlock(&sk->sk_callback_lock);
992}
993
Chuck Lever470056c2005-08-25 16:25:56 -0700994static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400995{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500996 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
997 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400998
Chuck Levera246b012005-08-11 16:25:23 -0400999 if (xprt->rcvsize) {
1000 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
1001 sk->sk_rcvbuf = xprt->rcvsize * xprt->max_reqs * 2;
1002 }
1003 if (xprt->sndsize) {
1004 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
1005 sk->sk_sndbuf = xprt->sndsize * xprt->max_reqs * 2;
1006 sk->sk_write_space(sk);
1007 }
1008}
1009
Chuck Lever43118c22005-08-25 16:25:49 -07001010/**
Chuck Lever470056c2005-08-25 16:25:56 -07001011 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001012 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001013 * @sndsize: requested size of send buffer, in bytes
1014 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001015 *
Chuck Lever470056c2005-08-25 16:25:56 -07001016 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001017 */
Chuck Lever470056c2005-08-25 16:25:56 -07001018static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001019{
Chuck Lever470056c2005-08-25 16:25:56 -07001020 xprt->sndsize = 0;
1021 if (sndsize)
1022 xprt->sndsize = sndsize + 1024;
1023 xprt->rcvsize = 0;
1024 if (rcvsize)
1025 xprt->rcvsize = rcvsize + 1024;
1026
1027 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001028}
1029
Chuck Lever46c0ee82005-08-25 16:25:52 -07001030/**
1031 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1032 * @task: task that timed out
1033 *
1034 * Adjust the congestion window after a retransmit timeout has occurred.
1035 */
1036static void xs_udp_timer(struct rpc_task *task)
1037{
1038 xprt_adjust_cwnd(task, -ETIMEDOUT);
1039}
1040
Chuck Leverb85d8802006-05-25 01:40:49 -04001041static unsigned short xs_get_random_port(void)
1042{
1043 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1044 unsigned short rand = (unsigned short) net_random() % range;
1045 return rand + xprt_min_resvport;
1046}
1047
Chuck Lever92200412006-01-03 09:55:51 +01001048/**
Chuck Leveredb267a2006-08-22 20:06:18 -04001049 * xs_print_peer_address - format an IPv4 address for printing
1050 * @xprt: generic transport
1051 * @format: flags field indicating which parts of the address to render
1052 */
1053static char *xs_print_peer_address(struct rpc_xprt *xprt, enum rpc_display_format_t format)
1054{
1055 if (xprt->address_strings[format] != NULL)
1056 return xprt->address_strings[format];
1057 else
1058 return "unprintable";
1059}
1060
1061/**
Chuck Lever92200412006-01-03 09:55:51 +01001062 * xs_set_port - reset the port number in the remote endpoint address
1063 * @xprt: generic transport
1064 * @port: new port number
1065 *
1066 */
1067static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1068{
Chuck Leverc4efcb12006-08-22 20:06:19 -04001069 struct sockaddr_in *sap = (struct sockaddr_in *) &xprt->addr;
1070
Chuck Lever92200412006-01-03 09:55:51 +01001071 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001072
1073 sap->sin_port = htons(port);
Chuck Lever92200412006-01-03 09:55:51 +01001074}
1075
Chuck Leverc8475462006-12-05 16:35:26 -05001076static int xs_bindresvport(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001077{
1078 struct sockaddr_in myaddr = {
1079 .sin_family = AF_INET,
1080 };
Chuck Lever529b33c2005-08-25 16:25:54 -07001081 int err;
Chuck Leverc8475462006-12-05 16:35:26 -05001082 unsigned short port = transport->port;
Chuck Levera246b012005-08-11 16:25:23 -04001083
Chuck Levera246b012005-08-11 16:25:23 -04001084 do {
1085 myaddr.sin_port = htons(port);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001086 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
Chuck Levera246b012005-08-11 16:25:23 -04001087 sizeof(myaddr));
1088 if (err == 0) {
Chuck Leverc8475462006-12-05 16:35:26 -05001089 transport->port = port;
Chuck Lever9903cd12005-08-11 16:25:26 -04001090 dprintk("RPC: xs_bindresvport bound to port %u\n",
1091 port);
Chuck Levera246b012005-08-11 16:25:23 -04001092 return 0;
1093 }
Chuck Lever529b33c2005-08-25 16:25:54 -07001094 if (port <= xprt_min_resvport)
1095 port = xprt_max_resvport;
1096 else
1097 port--;
Chuck Leverc8475462006-12-05 16:35:26 -05001098 } while (err == -EADDRINUSE && port != transport->port);
Chuck Levera246b012005-08-11 16:25:23 -04001099
Chuck Lever9903cd12005-08-11 16:25:26 -04001100 dprintk("RPC: can't bind to reserved port (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001101 return err;
1102}
1103
Chuck Lever9903cd12005-08-11 16:25:26 -04001104/**
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001105 * xs_udp_connect_worker - set up a UDP socket
Chuck Lever9903cd12005-08-11 16:25:26 -04001106 * @args: RPC transport to connect
1107 *
1108 * Invoked by a work queue tasklet.
Chuck Levera246b012005-08-11 16:25:23 -04001109 */
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001110static void xs_udp_connect_worker(void *args)
Chuck Levera246b012005-08-11 16:25:23 -04001111{
Chuck Leverc8475462006-12-05 16:35:26 -05001112 struct sock_xprt *transport = (struct sock_xprt *)args;
1113 struct rpc_xprt *xprt = &transport->xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001114 struct socket *sock = transport->sock;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001115 int err, status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04001116
Chuck Leverec739ef2006-08-22 20:06:15 -04001117 if (xprt->shutdown || !xprt_bound(xprt))
Chuck Levera246b012005-08-11 16:25:23 -04001118 goto out;
1119
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001120 /* Start by resetting any existing state */
Chuck Lever9903cd12005-08-11 16:25:26 -04001121 xs_close(xprt);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001122
1123 if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
1124 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001125 goto out;
1126 }
Chuck Levera246b012005-08-11 16:25:23 -04001127
Chuck Leverc8475462006-12-05 16:35:26 -05001128 if (xprt->resvport && xs_bindresvport(transport, sock) < 0) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001129 sock_release(sock);
1130 goto out;
1131 }
1132
Chuck Leveredb267a2006-08-22 20:06:18 -04001133 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1134 xprt, xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1135
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001136 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001137 struct sock *sk = sock->sk;
1138
1139 write_lock_bh(&sk->sk_callback_lock);
1140
1141 sk->sk_user_data = xprt;
1142 xprt->old_data_ready = sk->sk_data_ready;
1143 xprt->old_state_change = sk->sk_state_change;
1144 xprt->old_write_space = sk->sk_write_space;
1145 sk->sk_data_ready = xs_udp_data_ready;
1146 sk->sk_write_space = xs_udp_write_space;
1147 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa72005-12-13 16:13:52 -05001148 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001149
1150 xprt_set_connected(xprt);
1151
1152 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001153 transport->sock = sock;
1154 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001155
1156 write_unlock_bh(&sk->sk_callback_lock);
1157 }
Chuck Lever470056c2005-08-25 16:25:56 -07001158 xs_udp_do_set_buffer_size(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001159 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001160out:
1161 xprt_wake_pending_tasks(xprt, status);
1162 xprt_clear_connecting(xprt);
1163}
1164
Chuck Lever3167e122005-08-25 16:25:55 -07001165/*
1166 * We need to preserve the port number so the reply cache on the server can
1167 * find our cached RPC replies when we get around to reconnecting.
1168 */
1169static void xs_tcp_reuse_connection(struct rpc_xprt *xprt)
1170{
1171 int result;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001172 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever3167e122005-08-25 16:25:55 -07001173 struct sockaddr any;
1174
1175 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
1176
1177 /*
1178 * Disconnect the transport socket by doing a connect operation
1179 * with AF_UNSPEC. This should return immediately...
1180 */
1181 memset(&any, 0, sizeof(any));
1182 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001183 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Chuck Lever3167e122005-08-25 16:25:55 -07001184 if (result)
1185 dprintk("RPC: AF_UNSPEC connect return code %d\n",
1186 result);
1187}
1188
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001189/**
1190 * xs_tcp_connect_worker - connect a TCP socket to a remote endpoint
1191 * @args: RPC transport to connect
1192 *
1193 * Invoked by a work queue tasklet.
1194 */
1195static void xs_tcp_connect_worker(void *args)
1196{
Chuck Leverc8475462006-12-05 16:35:26 -05001197 struct sock_xprt *transport = (struct sock_xprt *)args;
1198 struct rpc_xprt *xprt = &transport->xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001199 struct socket *sock = transport->sock;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001200 int err, status = -EIO;
1201
Chuck Leverec739ef2006-08-22 20:06:15 -04001202 if (xprt->shutdown || !xprt_bound(xprt))
Chuck Levera246b012005-08-11 16:25:23 -04001203 goto out;
1204
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001205 if (!sock) {
Chuck Lever3167e122005-08-25 16:25:55 -07001206 /* start from scratch */
1207 if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
1208 dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
1209 goto out;
1210 }
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001211
Chuck Leverc8475462006-12-05 16:35:26 -05001212 if (xprt->resvport && xs_bindresvport(transport, sock) < 0) {
Chuck Lever3167e122005-08-25 16:25:55 -07001213 sock_release(sock);
1214 goto out;
1215 }
1216 } else
1217 /* "close" the socket, preserving the local port */
1218 xs_tcp_reuse_connection(xprt);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001219
Chuck Leveredb267a2006-08-22 20:06:18 -04001220 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1221 xprt, xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1222
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001223 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001224 struct sock *sk = sock->sk;
1225
1226 write_lock_bh(&sk->sk_callback_lock);
1227
1228 sk->sk_user_data = xprt;
1229 xprt->old_data_ready = sk->sk_data_ready;
1230 xprt->old_state_change = sk->sk_state_change;
1231 xprt->old_write_space = sk->sk_write_space;
1232 sk->sk_data_ready = xs_tcp_data_ready;
1233 sk->sk_state_change = xs_tcp_state_change;
1234 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebustb079fa72005-12-13 16:13:52 -05001235 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07001236
1237 /* socket options */
1238 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1239 sock_reset_flag(sk, SOCK_LINGER);
1240 tcp_sk(sk)->linger2 = 0;
1241 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001242
1243 xprt_clear_connected(xprt);
1244
1245 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001246 transport->sock = sock;
1247 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001248
1249 write_unlock_bh(&sk->sk_callback_lock);
1250 }
1251
1252 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05001253 xprt->stat.connect_count++;
1254 xprt->stat.connect_start = jiffies;
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001255 status = kernel_connect(sock, (struct sockaddr *) &xprt->addr,
Chuck Leverc4efcb12006-08-22 20:06:19 -04001256 xprt->addrlen, O_NONBLOCK);
Chuck Levera246b012005-08-11 16:25:23 -04001257 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1258 xprt, -status, xprt_connected(xprt), sock->sk->sk_state);
1259 if (status < 0) {
1260 switch (status) {
1261 case -EINPROGRESS:
1262 case -EALREADY:
1263 goto out_clear;
Chuck Lever3167e122005-08-25 16:25:55 -07001264 case -ECONNREFUSED:
1265 case -ECONNRESET:
1266 /* retry with existing socket, after a delay */
1267 break;
1268 default:
1269 /* get rid of existing socket, and retry */
1270 xs_close(xprt);
1271 break;
Chuck Levera246b012005-08-11 16:25:23 -04001272 }
1273 }
1274out:
Chuck Lever44fbac22005-08-11 16:25:44 -04001275 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001276out_clear:
Chuck Lever2226feb2005-08-11 16:25:38 -04001277 xprt_clear_connecting(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001278}
1279
Chuck Lever9903cd12005-08-11 16:25:26 -04001280/**
1281 * xs_connect - connect a socket to a remote endpoint
1282 * @task: address of RPC task that manages state of connect request
1283 *
1284 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07001285 *
1286 * UDP socket connects are synchronous, but we use a work queue anyway
1287 * to guarantee that even unprivileged user processes can set up a
1288 * socket on a privileged port.
1289 *
1290 * If a UDP socket connect fails, the delay behavior here prevents
1291 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04001292 */
1293static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04001294{
1295 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001296 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001297
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001298 if (xprt_test_and_set_connecting(xprt))
1299 return;
1300
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001301 if (transport->sock != NULL) {
Chuck Lever03bf4b72005-08-25 16:25:55 -07001302 dprintk("RPC: xs_connect delayed xprt %p for %lu seconds\n",
1303 xprt, xprt->reestablish_timeout / HZ);
Chuck Leverc8475462006-12-05 16:35:26 -05001304 schedule_delayed_work(&transport->connect_worker,
Chuck Lever03bf4b72005-08-25 16:25:55 -07001305 xprt->reestablish_timeout);
1306 xprt->reestablish_timeout <<= 1;
1307 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
1308 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001309 } else {
1310 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Chuck Leverc8475462006-12-05 16:35:26 -05001311 schedule_work(&transport->connect_worker);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001312
1313 /* flush_scheduled_work can sleep... */
1314 if (!RPC_IS_ASYNC(task))
1315 flush_scheduled_work();
Chuck Levera246b012005-08-11 16:25:23 -04001316 }
1317}
1318
Chuck Lever262ca072006-03-20 13:44:16 -05001319/**
1320 * xs_udp_print_stats - display UDP socket-specifc stats
1321 * @xprt: rpc_xprt struct containing statistics
1322 * @seq: output file
1323 *
1324 */
1325static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1326{
Chuck Leverc8475462006-12-05 16:35:26 -05001327 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1328
Chuck Lever262ca072006-03-20 13:44:16 -05001329 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
Chuck Leverc8475462006-12-05 16:35:26 -05001330 transport->port,
Chuck Lever262ca072006-03-20 13:44:16 -05001331 xprt->stat.bind_count,
1332 xprt->stat.sends,
1333 xprt->stat.recvs,
1334 xprt->stat.bad_xids,
1335 xprt->stat.req_u,
1336 xprt->stat.bklog_u);
1337}
1338
1339/**
1340 * xs_tcp_print_stats - display TCP socket-specifc stats
1341 * @xprt: rpc_xprt struct containing statistics
1342 * @seq: output file
1343 *
1344 */
1345static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1346{
Chuck Leverc8475462006-12-05 16:35:26 -05001347 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05001348 long idle_time = 0;
1349
1350 if (xprt_connected(xprt))
1351 idle_time = (long)(jiffies - xprt->last_used) / HZ;
1352
1353 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
Chuck Leverc8475462006-12-05 16:35:26 -05001354 transport->port,
Chuck Lever262ca072006-03-20 13:44:16 -05001355 xprt->stat.bind_count,
1356 xprt->stat.connect_count,
1357 xprt->stat.connect_time,
1358 idle_time,
1359 xprt->stat.sends,
1360 xprt->stat.recvs,
1361 xprt->stat.bad_xids,
1362 xprt->stat.req_u,
1363 xprt->stat.bklog_u);
1364}
1365
Chuck Lever262965f2005-08-11 16:25:56 -04001366static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07001367 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Leveredb267a2006-08-22 20:06:18 -04001368 .print_addr = xs_print_peer_address,
Chuck Lever12a80462005-08-25 16:25:51 -07001369 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07001370 .release_xprt = xprt_release_xprt_cong,
Chuck Leverbbf7c1d2006-08-22 20:06:16 -04001371 .rpcbind = rpc_getport,
Chuck Lever92200412006-01-03 09:55:51 +01001372 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04001373 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01001374 .buf_alloc = rpc_malloc,
1375 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04001376 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001377 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07001378 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07001379 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04001380 .close = xs_close,
1381 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05001382 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04001383};
1384
1385static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Leveredb267a2006-08-22 20:06:18 -04001386 .print_addr = xs_print_peer_address,
Chuck Lever12a80462005-08-25 16:25:51 -07001387 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001388 .release_xprt = xs_tcp_release_xprt,
Chuck Leverbbf7c1d2006-08-22 20:06:16 -04001389 .rpcbind = rpc_getport,
Chuck Lever92200412006-01-03 09:55:51 +01001390 .set_port = xs_set_port,
Chuck Lever262965f2005-08-11 16:25:56 -04001391 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01001392 .buf_alloc = rpc_malloc,
1393 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04001394 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001395 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Chuck Lever9903cd12005-08-11 16:25:26 -04001396 .close = xs_close,
1397 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05001398 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04001399};
1400
Chuck Leverc8541ec2006-10-17 14:44:27 -04001401static struct rpc_xprt *xs_setup_xprt(struct sockaddr *addr, size_t addrlen, unsigned int slot_table_size)
1402{
1403 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05001404 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04001405
1406 if (addrlen > sizeof(xprt->addr)) {
1407 dprintk("RPC: xs_setup_xprt: address too large\n");
1408 return ERR_PTR(-EBADF);
1409 }
1410
Chuck Leverffc2e512006-12-05 16:35:11 -05001411 new = kzalloc(sizeof(*new), GFP_KERNEL);
1412 if (new == NULL) {
Chuck Leverc8541ec2006-10-17 14:44:27 -04001413 dprintk("RPC: xs_setup_xprt: couldn't allocate rpc_xprt\n");
1414 return ERR_PTR(-ENOMEM);
1415 }
Chuck Leverffc2e512006-12-05 16:35:11 -05001416 xprt = &new->xprt;
Chuck Leverc8541ec2006-10-17 14:44:27 -04001417
1418 xprt->max_reqs = slot_table_size;
1419 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
1420 if (xprt->slot == NULL) {
1421 kfree(xprt);
1422 dprintk("RPC: xs_setup_xprt: couldn't allocate slot table\n");
1423 return ERR_PTR(-ENOMEM);
1424 }
1425
1426 memcpy(&xprt->addr, addr, addrlen);
1427 xprt->addrlen = addrlen;
Chuck Leverc8475462006-12-05 16:35:26 -05001428 new->port = xs_get_random_port();
Chuck Leverc8541ec2006-10-17 14:44:27 -04001429
1430 return xprt;
1431}
1432
Chuck Lever9903cd12005-08-11 16:25:26 -04001433/**
1434 * xs_setup_udp - Set up transport to use a UDP socket
Chuck Leverc8541ec2006-10-17 14:44:27 -04001435 * @addr: address of remote server
1436 * @addrlen: length of address in bytes
Chuck Lever9903cd12005-08-11 16:25:26 -04001437 * @to: timeout parameters
1438 *
1439 */
Chuck Leverc8541ec2006-10-17 14:44:27 -04001440struct rpc_xprt *xs_setup_udp(struct sockaddr *addr, size_t addrlen, struct rpc_timeout *to)
Chuck Levera246b012005-08-11 16:25:23 -04001441{
Chuck Leverc8541ec2006-10-17 14:44:27 -04001442 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001443 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001444
Chuck Leverc8541ec2006-10-17 14:44:27 -04001445 xprt = xs_setup_xprt(addr, addrlen, xprt_udp_slot_table_entries);
1446 if (IS_ERR(xprt))
1447 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001448 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001449
Chuck Leverc8541ec2006-10-17 14:44:27 -04001450 if (ntohs(((struct sockaddr_in *)addr)->sin_port) != 0)
Chuck Leverec739ef2006-08-22 20:06:15 -04001451 xprt_set_bound(xprt);
Chuck Leverec739ef2006-08-22 20:06:15 -04001452
1453 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07001454 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001455 /* XXX: header size can vary due to auth type, IPv6, etc. */
1456 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
1457
Chuck Leverc8475462006-12-05 16:35:26 -05001458 INIT_WORK(&transport->connect_worker, xs_udp_connect_worker, transport);
Chuck Lever03bf4b72005-08-25 16:25:55 -07001459 xprt->bind_timeout = XS_BIND_TO;
1460 xprt->connect_timeout = XS_UDP_CONN_TO;
1461 xprt->reestablish_timeout = XS_UDP_REEST_TO;
1462 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04001463
Chuck Lever262965f2005-08-11 16:25:56 -04001464 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001465
1466 if (to)
1467 xprt->timeout = *to;
1468 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001469 xprt_set_timeout(&xprt->timeout, 5, 5 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001470
Chuck Leveredb267a2006-08-22 20:06:18 -04001471 xs_format_peer_addresses(xprt);
1472 dprintk("RPC: set up transport to address %s\n",
1473 xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1474
Chuck Leverc8541ec2006-10-17 14:44:27 -04001475 return xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001476}
1477
Chuck Lever9903cd12005-08-11 16:25:26 -04001478/**
1479 * xs_setup_tcp - Set up transport to use a TCP socket
Chuck Leverc8541ec2006-10-17 14:44:27 -04001480 * @addr: address of remote server
1481 * @addrlen: length of address in bytes
Chuck Lever9903cd12005-08-11 16:25:26 -04001482 * @to: timeout parameters
1483 *
1484 */
Chuck Leverc8541ec2006-10-17 14:44:27 -04001485struct rpc_xprt *xs_setup_tcp(struct sockaddr *addr, size_t addrlen, struct rpc_timeout *to)
Chuck Levera246b012005-08-11 16:25:23 -04001486{
Chuck Leverc8541ec2006-10-17 14:44:27 -04001487 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001488 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001489
Chuck Leverc8541ec2006-10-17 14:44:27 -04001490 xprt = xs_setup_xprt(addr, addrlen, xprt_tcp_slot_table_entries);
1491 if (IS_ERR(xprt))
1492 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001493 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001494
Chuck Leverc8541ec2006-10-17 14:44:27 -04001495 if (ntohs(((struct sockaddr_in *)addr)->sin_port) != 0)
Chuck Leverec739ef2006-08-22 20:06:15 -04001496 xprt_set_bound(xprt);
Chuck Leverec739ef2006-08-22 20:06:15 -04001497
1498 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07001499 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07001500 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04001501
Chuck Leverc8475462006-12-05 16:35:26 -05001502 INIT_WORK(&transport->connect_worker, xs_tcp_connect_worker, transport);
Chuck Lever03bf4b72005-08-25 16:25:55 -07001503 xprt->bind_timeout = XS_BIND_TO;
1504 xprt->connect_timeout = XS_TCP_CONN_TO;
1505 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1506 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04001507
Chuck Lever262965f2005-08-11 16:25:56 -04001508 xprt->ops = &xs_tcp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001509
1510 if (to)
1511 xprt->timeout = *to;
1512 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001513 xprt_set_timeout(&xprt->timeout, 2, 60 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001514
Chuck Leveredb267a2006-08-22 20:06:18 -04001515 xs_format_peer_addresses(xprt);
1516 dprintk("RPC: set up transport to address %s\n",
1517 xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1518
Chuck Leverc8541ec2006-10-17 14:44:27 -04001519 return xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001520}