blob: 21438d7dc47b85ea1ae6acf1b754e4f7c8dd1291 [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 Leverfbf76682006-12-05 16:35:54 -050049 * We can register our own files under /proc/sys/sunrpc by
50 * calling register_sysctl_table() again. The files in that
51 * directory become the union of all files registered there.
52 *
53 * We simply need to make sure that we don't collide with
54 * someone else's file names!
55 */
56
57#ifdef RPC_DEBUG
58
59static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
60static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
61static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
62static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
63
64static struct ctl_table_header *sunrpc_table_header;
65
66/*
67 * FIXME: changing the UDP slot table size should also resize the UDP
68 * socket buffers for existing UDP transports
69 */
70static ctl_table xs_tunables_table[] = {
71 {
72 .ctl_name = CTL_SLOTTABLE_UDP,
73 .procname = "udp_slot_table_entries",
74 .data = &xprt_udp_slot_table_entries,
75 .maxlen = sizeof(unsigned int),
76 .mode = 0644,
77 .proc_handler = &proc_dointvec_minmax,
78 .strategy = &sysctl_intvec,
79 .extra1 = &min_slot_table_size,
80 .extra2 = &max_slot_table_size
81 },
82 {
83 .ctl_name = CTL_SLOTTABLE_TCP,
84 .procname = "tcp_slot_table_entries",
85 .data = &xprt_tcp_slot_table_entries,
86 .maxlen = sizeof(unsigned int),
87 .mode = 0644,
88 .proc_handler = &proc_dointvec_minmax,
89 .strategy = &sysctl_intvec,
90 .extra1 = &min_slot_table_size,
91 .extra2 = &max_slot_table_size
92 },
93 {
94 .ctl_name = CTL_MIN_RESVPORT,
95 .procname = "min_resvport",
96 .data = &xprt_min_resvport,
97 .maxlen = sizeof(unsigned int),
98 .mode = 0644,
99 .proc_handler = &proc_dointvec_minmax,
100 .strategy = &sysctl_intvec,
101 .extra1 = &xprt_min_resvport_limit,
102 .extra2 = &xprt_max_resvport_limit
103 },
104 {
105 .ctl_name = CTL_MAX_RESVPORT,
106 .procname = "max_resvport",
107 .data = &xprt_max_resvport,
108 .maxlen = sizeof(unsigned int),
109 .mode = 0644,
110 .proc_handler = &proc_dointvec_minmax,
111 .strategy = &sysctl_intvec,
112 .extra1 = &xprt_min_resvport_limit,
113 .extra2 = &xprt_max_resvport_limit
114 },
115 {
116 .ctl_name = 0,
117 },
118};
119
120static ctl_table sunrpc_table[] = {
121 {
122 .ctl_name = CTL_SUNRPC,
123 .procname = "sunrpc",
124 .mode = 0555,
125 .child = xs_tunables_table
126 },
127 {
128 .ctl_name = 0,
129 },
130};
131
132#endif
133
134/*
Chuck Lever262965f2005-08-11 16:25:56 -0400135 * How many times to try sending a request on a socket before waiting
136 * for the socket buffer to clear.
137 */
138#define XS_SENDMSG_RETRY (10U)
139
Chuck Lever03bf4b72005-08-25 16:25:55 -0700140/*
141 * Time out for an RPC UDP socket connect. UDP socket connects are
142 * synchronous, but we set a timeout anyway in case of resource
143 * exhaustion on the local host.
144 */
145#define XS_UDP_CONN_TO (5U * HZ)
146
147/*
148 * Wait duration for an RPC TCP connection to be established. Solaris
149 * NFS over TCP uses 60 seconds, for example, which is in line with how
150 * long a server takes to reboot.
151 */
152#define XS_TCP_CONN_TO (60U * HZ)
153
154/*
155 * Wait duration for a reply from the RPC portmapper.
156 */
157#define XS_BIND_TO (60U * HZ)
158
159/*
160 * Delay if a UDP socket connect error occurs. This is most likely some
161 * kind of resource problem on the local host.
162 */
163#define XS_UDP_REEST_TO (2U * HZ)
164
165/*
166 * The reestablish timeout allows clients to delay for a bit before attempting
167 * to reconnect to a server that just dropped our connection.
168 *
169 * We implement an exponential backoff when trying to reestablish a TCP
170 * transport connection with the server. Some servers like to drop a TCP
171 * connection when they are overworked, so we start with a short timeout and
172 * increase over time if the server is down or not responding.
173 */
174#define XS_TCP_INIT_REEST_TO (3U * HZ)
175#define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
176
177/*
178 * TCP idle timeout; client drops the transport socket if it is idle
179 * for this long. Note that we also timeout UDP sockets to prevent
180 * holding port numbers when there is no RPC traffic.
181 */
182#define XS_IDLE_DISC_TO (5U * 60 * HZ)
183
Chuck Levera246b012005-08-11 16:25:23 -0400184#ifdef RPC_DEBUG
185# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400186# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400187#endif
188
Chuck Levera246b012005-08-11 16:25:23 -0400189#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400190static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400191{
Chuck Lever9903cd12005-08-11 16:25:26 -0400192 u8 *buf = (u8 *) packet;
193 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400194
195 dprintk("RPC: %s\n", msg);
196 for (j = 0; j < count && j < 128; j += 4) {
197 if (!(j & 31)) {
198 if (j)
199 dprintk("\n");
200 dprintk("0x%04x ", j);
201 }
202 dprintk("%02x%02x%02x%02x ",
203 buf[j], buf[j+1], buf[j+2], buf[j+3]);
204 }
205 dprintk("\n");
206}
207#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400208static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400209{
210 /* NOP */
211}
212#endif
213
Chuck Leverffc2e512006-12-05 16:35:11 -0500214struct sock_xprt {
215 struct rpc_xprt xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500216
217 /*
218 * Network layer
219 */
220 struct socket * sock;
221 struct sock * inet;
Chuck Lever51971132006-12-05 16:35:19 -0500222
223 /*
224 * State of TCP reply receive
225 */
226 __be32 tcp_fraghdr,
227 tcp_xid;
228
229 u32 tcp_offset,
230 tcp_reclen;
231
232 unsigned long tcp_copied,
233 tcp_flags;
Chuck Leverc8475462006-12-05 16:35:26 -0500234
235 /*
236 * Connection of transports
237 */
238 struct work_struct connect_worker;
239 unsigned short port;
Chuck Lever7c6e0662006-12-05 16:35:30 -0500240
241 /*
242 * UDP socket buffer size parameters
243 */
244 size_t rcvsize,
245 sndsize;
Chuck Lever314dfd72006-12-05 16:35:34 -0500246
247 /*
248 * Saved socket callback addresses
249 */
250 void (*old_data_ready)(struct sock *, int);
251 void (*old_state_change)(struct sock *);
252 void (*old_write_space)(struct sock *);
Chuck Leverffc2e512006-12-05 16:35:11 -0500253};
254
Chuck Levere136d092006-12-05 16:35:23 -0500255/*
256 * TCP receive state flags
257 */
258#define TCP_RCV_LAST_FRAG (1UL << 0)
259#define TCP_RCV_COPY_FRAGHDR (1UL << 1)
260#define TCP_RCV_COPY_XID (1UL << 2)
261#define TCP_RCV_COPY_DATA (1UL << 3)
262
Chuck Leveredb267a2006-08-22 20:06:18 -0400263static void xs_format_peer_addresses(struct rpc_xprt *xprt)
264{
265 struct sockaddr_in *addr = (struct sockaddr_in *) &xprt->addr;
266 char *buf;
267
268 buf = kzalloc(20, GFP_KERNEL);
269 if (buf) {
270 snprintf(buf, 20, "%u.%u.%u.%u",
271 NIPQUAD(addr->sin_addr.s_addr));
272 }
273 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
274
275 buf = kzalloc(8, GFP_KERNEL);
276 if (buf) {
277 snprintf(buf, 8, "%u",
278 ntohs(addr->sin_port));
279 }
280 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
281
282 if (xprt->prot == IPPROTO_UDP)
283 xprt->address_strings[RPC_DISPLAY_PROTO] = "udp";
284 else
285 xprt->address_strings[RPC_DISPLAY_PROTO] = "tcp";
286
287 buf = kzalloc(48, GFP_KERNEL);
288 if (buf) {
289 snprintf(buf, 48, "addr=%u.%u.%u.%u port=%u proto=%s",
290 NIPQUAD(addr->sin_addr.s_addr),
291 ntohs(addr->sin_port),
292 xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
293 }
294 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
295}
296
297static void xs_free_peer_addresses(struct rpc_xprt *xprt)
298{
299 kfree(xprt->address_strings[RPC_DISPLAY_ADDR]);
300 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
301 kfree(xprt->address_strings[RPC_DISPLAY_ALL]);
302}
303
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400304#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
305
Trond Myklebust24c56842006-10-17 15:06:22 -0400306static 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 -0400307{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400308 struct msghdr msg = {
309 .msg_name = addr,
310 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400311 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
312 };
313 struct kvec iov = {
314 .iov_base = vec->iov_base + base,
315 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400316 };
317
Trond Myklebust24c56842006-10-17 15:06:22 -0400318 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400319 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
320 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
321}
322
Trond Myklebust24c56842006-10-17 15:06:22 -0400323static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400324{
Trond Myklebust24c56842006-10-17 15:06:22 -0400325 struct page **ppage;
326 unsigned int remainder;
327 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400328
Trond Myklebust24c56842006-10-17 15:06:22 -0400329 remainder = xdr->page_len - base;
330 base += xdr->page_base;
331 ppage = xdr->pages + (base >> PAGE_SHIFT);
332 base &= ~PAGE_MASK;
333 for(;;) {
334 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
335 int flags = XS_SENDMSG_FLAGS;
336
337 remainder -= len;
338 if (remainder != 0 || more)
339 flags |= MSG_MORE;
340 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
341 if (remainder == 0 || err != len)
342 break;
343 sent += err;
344 ppage++;
345 base = 0;
346 }
347 if (sent == 0)
348 return err;
349 if (err > 0)
350 sent += err;
351 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400352}
353
Chuck Lever9903cd12005-08-11 16:25:26 -0400354/**
355 * xs_sendpages - write pages directly to a socket
356 * @sock: socket to send on
357 * @addr: UDP only -- address of destination
358 * @addrlen: UDP only -- length of destination address
359 * @xdr: buffer containing this request
360 * @base: starting position in the buffer
361 *
Chuck Levera246b012005-08-11 16:25:23 -0400362 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400363static 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 -0400364{
Trond Myklebust24c56842006-10-17 15:06:22 -0400365 unsigned int remainder = xdr->len - base;
366 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400367
Chuck Lever262965f2005-08-11 16:25:56 -0400368 if (unlikely(!sock))
369 return -ENOTCONN;
370
371 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400372 if (base != 0) {
373 addr = NULL;
374 addrlen = 0;
375 }
Chuck Lever262965f2005-08-11 16:25:56 -0400376
Trond Myklebust24c56842006-10-17 15:06:22 -0400377 if (base < xdr->head[0].iov_len || addr != NULL) {
378 unsigned int len = xdr->head[0].iov_len - base;
379 remainder -= len;
380 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
381 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400382 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400383 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400384 base = 0;
385 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400386 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400387
Trond Myklebust24c56842006-10-17 15:06:22 -0400388 if (base < xdr->page_len) {
389 unsigned int len = xdr->page_len - base;
390 remainder -= len;
391 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
392 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400393 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400394 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400395 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400396 } else
397 base -= xdr->page_len;
398
399 if (base >= xdr->tail[0].iov_len)
400 return sent;
401 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400402out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400403 if (sent == 0)
404 return err;
405 if (err > 0)
406 sent += err;
407 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400408}
409
Chuck Lever9903cd12005-08-11 16:25:26 -0400410/**
Chuck Lever262965f2005-08-11 16:25:56 -0400411 * xs_nospace - place task on wait queue if transmit was incomplete
412 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400413 *
Chuck Levera246b012005-08-11 16:25:23 -0400414 */
Chuck Lever262965f2005-08-11 16:25:56 -0400415static void xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400416{
Chuck Lever262965f2005-08-11 16:25:56 -0400417 struct rpc_rqst *req = task->tk_rqstp;
418 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500419 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400420
Chuck Lever262965f2005-08-11 16:25:56 -0400421 dprintk("RPC: %4d xmit incomplete (%u left of %u)\n",
422 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
423 req->rq_slen);
424
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500425 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
Chuck Lever262965f2005-08-11 16:25:56 -0400426 /* Protect against races with write_space */
427 spin_lock_bh(&xprt->transport_lock);
428
429 /* Don't race with disconnect */
430 if (!xprt_connected(xprt))
431 task->tk_status = -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500432 else if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
Chuck Lever262965f2005-08-11 16:25:56 -0400433 xprt_wait_for_buffer_space(task);
434
435 spin_unlock_bh(&xprt->transport_lock);
436 } else
437 /* Keep holding the socket if it is blocked */
438 rpc_delay(task, HZ>>4);
439}
440
441/**
442 * xs_udp_send_request - write an RPC request to a UDP socket
443 * @task: address of RPC task that manages the state of an RPC request
444 *
445 * Return values:
446 * 0: The request has been sent
447 * EAGAIN: The socket was blocked, please call again later to
448 * complete the request
449 * ENOTCONN: Caller needs to invoke connect logic then call again
450 * other: Some other error occured, the request was not sent
451 */
452static int xs_udp_send_request(struct rpc_task *task)
453{
454 struct rpc_rqst *req = task->tk_rqstp;
455 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500456 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400457 struct xdr_buf *xdr = &req->rq_snd_buf;
458 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400459
Chuck Lever9903cd12005-08-11 16:25:26 -0400460 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400461 req->rq_svec->iov_base,
462 req->rq_svec->iov_len);
463
Chuck Lever262965f2005-08-11 16:25:56 -0400464 req->rq_xtime = jiffies;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500465 status = xs_sendpages(transport->sock,
466 (struct sockaddr *) &xprt->addr,
467 xprt->addrlen, xdr,
468 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400469
Chuck Lever262965f2005-08-11 16:25:56 -0400470 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
471 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400472
Chuck Lever262965f2005-08-11 16:25:56 -0400473 if (likely(status >= (int) req->rq_slen))
474 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400475
Chuck Lever262965f2005-08-11 16:25:56 -0400476 /* Still some bytes left; set up for a retry later. */
477 if (status > 0)
478 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400479
Chuck Lever262965f2005-08-11 16:25:56 -0400480 switch (status) {
481 case -ENETUNREACH:
482 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400483 case -ECONNREFUSED:
484 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400485 * prompts ECONNREFUSED. */
Chuck Levera246b012005-08-11 16:25:23 -0400486 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400487 case -EAGAIN:
488 xs_nospace(task);
Chuck Levera246b012005-08-11 16:25:23 -0400489 break;
490 default:
Chuck Lever262965f2005-08-11 16:25:56 -0400491 dprintk("RPC: sendmsg returned unrecognized error %d\n",
492 -status);
Chuck Lever9903cd12005-08-11 16:25:26 -0400493 break;
Chuck Levera246b012005-08-11 16:25:23 -0400494 }
Chuck Lever262965f2005-08-11 16:25:56 -0400495
496 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400497}
498
Chuck Lever808012f2005-08-25 16:25:49 -0700499static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
500{
501 u32 reclen = buf->len - sizeof(rpc_fraghdr);
502 rpc_fraghdr *base = buf->head[0].iov_base;
503 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
504}
505
Chuck Lever9903cd12005-08-11 16:25:26 -0400506/**
Chuck Lever262965f2005-08-11 16:25:56 -0400507 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400508 * @task: address of RPC task that manages the state of an RPC request
509 *
510 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400511 * 0: The request has been sent
512 * EAGAIN: The socket was blocked, please call again later to
513 * complete the request
514 * ENOTCONN: Caller needs to invoke connect logic then call again
515 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400516 *
517 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400518 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400519 */
Chuck Lever262965f2005-08-11 16:25:56 -0400520static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400521{
522 struct rpc_rqst *req = task->tk_rqstp;
523 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500524 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400525 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Levera246b012005-08-11 16:25:23 -0400526 int status, retry = 0;
527
Chuck Lever808012f2005-08-25 16:25:49 -0700528 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400529
Chuck Lever262965f2005-08-11 16:25:56 -0400530 xs_pktdump("packet data:",
531 req->rq_svec->iov_base,
532 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400533
534 /* Continue transmitting the packet/record. We must be careful
535 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400536 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400537 while (1) {
538 req->rq_xtime = jiffies;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500539 status = xs_sendpages(transport->sock,
540 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400541
Chuck Lever262965f2005-08-11 16:25:56 -0400542 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
543 xdr->len - req->rq_bytes_sent, status);
544
545 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400546 break;
547
Chuck Lever262965f2005-08-11 16:25:56 -0400548 /* If we've sent the entire packet, immediately
549 * reset the count of bytes sent. */
550 req->rq_bytes_sent += status;
Chuck Leveref759a22006-03-20 13:44:17 -0500551 task->tk_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400552 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
553 req->rq_bytes_sent = 0;
554 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400555 }
556
Chuck Levera246b012005-08-11 16:25:23 -0400557 status = -EAGAIN;
Chuck Lever262965f2005-08-11 16:25:56 -0400558 if (retry++ > XS_SENDMSG_RETRY)
Chuck Levera246b012005-08-11 16:25:23 -0400559 break;
560 }
561
Chuck Lever262965f2005-08-11 16:25:56 -0400562 switch (status) {
563 case -EAGAIN:
564 xs_nospace(task);
565 break;
566 case -ECONNREFUSED:
567 case -ECONNRESET:
568 case -ENOTCONN:
569 case -EPIPE:
570 status = -ENOTCONN;
571 break;
572 default:
573 dprintk("RPC: sendmsg returned unrecognized error %d\n",
574 -status);
Chuck Lever43118c22005-08-25 16:25:49 -0700575 xprt_disconnect(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400576 break;
Chuck Levera246b012005-08-11 16:25:23 -0400577 }
Chuck Lever262965f2005-08-11 16:25:56 -0400578
Chuck Levera246b012005-08-11 16:25:23 -0400579 return status;
580}
581
Chuck Lever9903cd12005-08-11 16:25:26 -0400582/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400583 * xs_tcp_release_xprt - clean up after a tcp transmission
584 * @xprt: transport
585 * @task: rpc task
586 *
587 * This cleans up if an error causes us to abort the transmission of a request.
588 * In this case, the socket may need to be reset in order to avoid confusing
589 * the server.
590 */
591static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
592{
593 struct rpc_rqst *req;
594
595 if (task != xprt->snd_task)
596 return;
597 if (task == NULL)
598 goto out_release;
599 req = task->tk_rqstp;
600 if (req->rq_bytes_sent == 0)
601 goto out_release;
602 if (req->rq_bytes_sent == req->rq_snd_buf.len)
603 goto out_release;
604 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
605out_release:
606 xprt_release_xprt(xprt, task);
607}
608
609/**
Chuck Lever9903cd12005-08-11 16:25:26 -0400610 * xs_close - close a socket
611 * @xprt: transport
612 *
Chuck Lever3167e122005-08-25 16:25:55 -0700613 * This is used when all requests are complete; ie, no DRC state remains
614 * on the server we want to save.
Chuck Levera246b012005-08-11 16:25:23 -0400615 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400616static void xs_close(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400617{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500618 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
619 struct socket *sock = transport->sock;
620 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400621
622 if (!sk)
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100623 goto clear_close_wait;
Chuck Levera246b012005-08-11 16:25:23 -0400624
Chuck Lever9903cd12005-08-11 16:25:26 -0400625 dprintk("RPC: xs_close xprt %p\n", xprt);
626
Chuck Levera246b012005-08-11 16:25:23 -0400627 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500628 transport->inet = NULL;
629 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400630
Chuck Lever9903cd12005-08-11 16:25:26 -0400631 sk->sk_user_data = NULL;
Chuck Lever314dfd72006-12-05 16:35:34 -0500632 sk->sk_data_ready = transport->old_data_ready;
633 sk->sk_state_change = transport->old_state_change;
634 sk->sk_write_space = transport->old_write_space;
Chuck Levera246b012005-08-11 16:25:23 -0400635 write_unlock_bh(&sk->sk_callback_lock);
636
Chuck Lever9903cd12005-08-11 16:25:26 -0400637 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400638
639 sock_release(sock);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100640clear_close_wait:
641 smp_mb__before_clear_bit();
642 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
643 smp_mb__after_clear_bit();
Chuck Levera246b012005-08-11 16:25:23 -0400644}
645
Chuck Lever9903cd12005-08-11 16:25:26 -0400646/**
647 * xs_destroy - prepare to shutdown a transport
648 * @xprt: doomed transport
649 *
650 */
651static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400652{
Chuck Leverc8475462006-12-05 16:35:26 -0500653 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
654
Chuck Lever9903cd12005-08-11 16:25:26 -0400655 dprintk("RPC: xs_destroy xprt %p\n", xprt);
656
Chuck Leverc8475462006-12-05 16:35:26 -0500657 cancel_delayed_work(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400658 flush_scheduled_work();
659
660 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400661 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400662 xs_free_peer_addresses(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400663 kfree(xprt->slot);
Chuck Leverc8541ec2006-10-17 14:44:27 -0400664 kfree(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400665}
666
Chuck Lever9903cd12005-08-11 16:25:26 -0400667static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400668{
Chuck Lever9903cd12005-08-11 16:25:26 -0400669 return (struct rpc_xprt *) sk->sk_user_data;
670}
671
672/**
673 * xs_udp_data_ready - "data ready" callback for UDP sockets
674 * @sk: socket with data to read
675 * @len: how much data to read
676 *
677 */
678static void xs_udp_data_ready(struct sock *sk, int len)
679{
680 struct rpc_task *task;
681 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400682 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400683 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400684 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700685 u32 _xid;
686 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400687
688 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400689 dprintk("RPC: xs_udp_data_ready...\n");
690 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400691 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400692
693 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
694 goto out;
695
696 if (xprt->shutdown)
697 goto dropit;
698
699 repsize = skb->len - sizeof(struct udphdr);
700 if (repsize < 4) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400701 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400702 goto dropit;
703 }
704
705 /* Copy the XID from the skb... */
706 xp = skb_header_pointer(skb, sizeof(struct udphdr),
707 sizeof(_xid), &_xid);
708 if (xp == NULL)
709 goto dropit;
710
711 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400712 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400713 rovr = xprt_lookup_rqst(xprt, *xp);
714 if (!rovr)
715 goto out_unlock;
716 task = rovr->rq_task;
717
Chuck Levera246b012005-08-11 16:25:23 -0400718 if ((copied = rovr->rq_private_buf.buflen) > repsize)
719 copied = repsize;
720
721 /* Suck it into the iovec, verify checksum if not done by hw. */
722 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb))
723 goto out_unlock;
724
725 /* Something worked... */
726 dst_confirm(skb->dst);
727
Chuck Lever1570c1e2005-08-25 16:25:52 -0700728 xprt_adjust_cwnd(task, copied);
729 xprt_update_rtt(task);
730 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400731
732 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400733 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400734 dropit:
735 skb_free_datagram(sk, skb);
736 out:
737 read_unlock(&sk->sk_callback_lock);
738}
739
Chuck Leverdd456472006-12-05 16:35:44 -0500740static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400741{
Chuck Lever51971132006-12-05 16:35:19 -0500742 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400743 size_t len, used;
744 char *p;
745
Chuck Lever51971132006-12-05 16:35:19 -0500746 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
747 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500748 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500749 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400750 if (used != len)
751 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700752
Chuck Lever51971132006-12-05 16:35:19 -0500753 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
754 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -0500755 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -0400756 else
Chuck Levere136d092006-12-05 16:35:23 -0500757 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -0500758 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -0700759
Chuck Levere136d092006-12-05 16:35:23 -0500760 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500761 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700762
Chuck Levera246b012005-08-11 16:25:23 -0400763 /* Sanity check of the record length */
Chuck Lever51971132006-12-05 16:35:19 -0500764 if (unlikely(transport->tcp_reclen < 4)) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400765 dprintk("RPC: invalid TCP record fragment length\n");
Chuck Levera246b012005-08-11 16:25:23 -0400766 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400767 return;
Chuck Levera246b012005-08-11 16:25:23 -0400768 }
769 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -0500770 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400771}
772
Chuck Lever51971132006-12-05 16:35:19 -0500773static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400774{
Chuck Lever51971132006-12-05 16:35:19 -0500775 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500776 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500777 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -0500778 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
779 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
780 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -0500781 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400782 }
783 }
784}
785
Chuck Leverdd456472006-12-05 16:35:44 -0500786static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400787{
788 size_t len, used;
789 char *p;
790
Chuck Lever51971132006-12-05 16:35:19 -0500791 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -0400792 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500793 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9d292312006-12-05 16:35:41 -0500794 used = xdr_skb_read_bits(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500795 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400796 if (used != len)
797 return;
Chuck Levere136d092006-12-05 16:35:23 -0500798 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
799 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -0500800 transport->tcp_copied = 4;
Chuck Levera246b012005-08-11 16:25:23 -0400801 dprintk("RPC: reading reply for XID %08x\n",
Chuck Lever51971132006-12-05 16:35:19 -0500802 ntohl(transport->tcp_xid));
803 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400804}
805
Chuck Leverdd456472006-12-05 16:35:44 -0500806static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400807{
Chuck Lever51971132006-12-05 16:35:19 -0500808 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400809 struct rpc_rqst *req;
810 struct xdr_buf *rcvbuf;
811 size_t len;
812 ssize_t r;
813
814 /* Find and lock the request corresponding to this xid */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400815 spin_lock(&xprt->transport_lock);
Chuck Lever51971132006-12-05 16:35:19 -0500816 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
Chuck Levera246b012005-08-11 16:25:23 -0400817 if (!req) {
Chuck Levere136d092006-12-05 16:35:23 -0500818 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -0400819 dprintk("RPC: XID %08x request not found!\n",
Chuck Lever51971132006-12-05 16:35:19 -0500820 ntohl(transport->tcp_xid));
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400821 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400822 return;
823 }
824
825 rcvbuf = &req->rq_private_buf;
826 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -0500827 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Leverdd456472006-12-05 16:35:44 -0500828 struct xdr_skb_reader my_desc;
Chuck Levera246b012005-08-11 16:25:23 -0400829
Chuck Lever51971132006-12-05 16:35:19 -0500830 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -0400831 memcpy(&my_desc, desc, sizeof(my_desc));
832 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -0500833 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -0500834 &my_desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -0400835 desc->count -= r;
836 desc->offset += r;
837 } else
Chuck Lever51971132006-12-05 16:35:19 -0500838 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9d292312006-12-05 16:35:41 -0500839 desc, xdr_skb_read_bits);
Chuck Levera246b012005-08-11 16:25:23 -0400840
841 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -0500842 transport->tcp_copied += r;
843 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -0400844 }
845 if (r != len) {
846 /* Error when copying to the receive buffer,
847 * usually because we weren't able to allocate
848 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -0500849 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -0400850 * will not receive any additional updates,
851 * and time out.
852 * Any remaining data from this record will
853 * be discarded.
854 */
Chuck Levere136d092006-12-05 16:35:23 -0500855 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -0400856 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -0500857 ntohl(transport->tcp_xid));
Chuck Levera246b012005-08-11 16:25:23 -0400858 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
Chuck Lever51971132006-12-05 16:35:19 -0500859 xprt, transport->tcp_copied, transport->tcp_offset,
860 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400861 goto out;
862 }
863
864 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -0500865 ntohl(transport->tcp_xid), r);
Chuck Levera246b012005-08-11 16:25:23 -0400866 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
Chuck Lever51971132006-12-05 16:35:19 -0500867 xprt, transport->tcp_copied, transport->tcp_offset,
868 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400869
Chuck Lever51971132006-12-05 16:35:19 -0500870 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -0500871 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -0500872 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500873 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
874 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -0400875 }
876
877out:
Chuck Levere136d092006-12-05 16:35:23 -0500878 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -0500879 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400880 spin_unlock(&xprt->transport_lock);
Chuck Lever51971132006-12-05 16:35:19 -0500881 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400882}
883
Chuck Leverdd456472006-12-05 16:35:44 -0500884static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400885{
886 size_t len;
887
Chuck Lever51971132006-12-05 16:35:19 -0500888 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -0400889 if (len > desc->count)
890 len = desc->count;
891 desc->count -= len;
892 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -0500893 transport->tcp_offset += len;
Chuck Levera246b012005-08-11 16:25:23 -0400894 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500895 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400896}
897
Chuck Lever9903cd12005-08-11 16:25:26 -0400898static 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 -0400899{
900 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -0500901 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Leverdd456472006-12-05 16:35:44 -0500902 struct xdr_skb_reader desc = {
Chuck Levera246b012005-08-11 16:25:23 -0400903 .skb = skb,
904 .offset = offset,
905 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -0400906 };
Chuck Levera246b012005-08-11 16:25:23 -0400907
Chuck Lever9903cd12005-08-11 16:25:26 -0400908 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -0400909 do {
910 /* Read in a new fragment marker if necessary */
911 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -0500912 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400913 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400914 continue;
915 }
916 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -0500917 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -0500918 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400919 continue;
920 }
921 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -0500922 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400923 xs_tcp_read_request(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400924 continue;
925 }
926 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -0500927 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400928 } while (desc.count);
Chuck Lever9903cd12005-08-11 16:25:26 -0400929 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -0400930 return len - desc.count;
931}
932
Chuck Lever9903cd12005-08-11 16:25:26 -0400933/**
934 * xs_tcp_data_ready - "data ready" callback for TCP sockets
935 * @sk: socket with data to read
936 * @bytes: how much data to read
937 *
938 */
939static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -0400940{
941 struct rpc_xprt *xprt;
942 read_descriptor_t rd_desc;
943
944 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400945 dprintk("RPC: xs_tcp_data_ready...\n");
946 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400947 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400948 if (xprt->shutdown)
949 goto out;
950
Chuck Lever9903cd12005-08-11 16:25:26 -0400951 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -0400952 rd_desc.arg.data = xprt;
953 rd_desc.count = 65536;
Chuck Lever9903cd12005-08-11 16:25:26 -0400954 tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
Chuck Levera246b012005-08-11 16:25:23 -0400955out:
956 read_unlock(&sk->sk_callback_lock);
957}
958
Chuck Lever9903cd12005-08-11 16:25:26 -0400959/**
960 * xs_tcp_state_change - callback to handle TCP socket state changes
961 * @sk: socket whose state has changed
962 *
963 */
964static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400965{
Chuck Lever9903cd12005-08-11 16:25:26 -0400966 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400967
968 read_lock(&sk->sk_callback_lock);
969 if (!(xprt = xprt_from_sock(sk)))
970 goto out;
Chuck Lever9903cd12005-08-11 16:25:26 -0400971 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400972 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
973 sk->sk_state, xprt_connected(xprt),
974 sock_flag(sk, SOCK_DEAD),
975 sock_flag(sk, SOCK_ZAPPED));
976
977 switch (sk->sk_state) {
978 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400979 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400980 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -0500981 struct sock_xprt *transport = container_of(xprt,
982 struct sock_xprt, xprt);
983
Chuck Levera246b012005-08-11 16:25:23 -0400984 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -0500985 transport->tcp_offset = 0;
986 transport->tcp_reclen = 0;
987 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -0500988 transport->tcp_flags =
989 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -0500990
Chuck Lever03bf4b72005-08-25 16:25:55 -0700991 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever44fbac22005-08-11 16:25:44 -0400992 xprt_wake_pending_tasks(xprt, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400993 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400994 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400995 break;
996 case TCP_SYN_SENT:
997 case TCP_SYN_RECV:
998 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100999 case TCP_CLOSE_WAIT:
1000 /* Try to schedule an autoclose RPC calls */
1001 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
1002 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
1003 schedule_work(&xprt->task_cleanup);
Chuck Levera246b012005-08-11 16:25:23 -04001004 default:
1005 xprt_disconnect(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001006 }
1007 out:
1008 read_unlock(&sk->sk_callback_lock);
1009}
1010
Chuck Lever9903cd12005-08-11 16:25:26 -04001011/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001012 * xs_udp_write_space - callback invoked when socket buffer space
1013 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -04001014 * @sk: socket whose state has changed
1015 *
Chuck Levera246b012005-08-11 16:25:23 -04001016 * Called when more output buffer space is available for this socket.
1017 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001018 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -04001019 * with a bunch of small requests.
1020 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001021static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -04001022{
Chuck Levera246b012005-08-11 16:25:23 -04001023 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -04001024
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001025 /* from net/core/sock.c:sock_def_write_space */
1026 if (sock_writeable(sk)) {
1027 struct socket *sock;
1028 struct rpc_xprt *xprt;
1029
1030 if (unlikely(!(sock = sk->sk_socket)))
Chuck Levera246b012005-08-11 16:25:23 -04001031 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001032 if (unlikely(!(xprt = xprt_from_sock(sk))))
Chuck Levera246b012005-08-11 16:25:23 -04001033 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001034 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
1035 goto out;
1036
1037 xprt_write_space(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001038 }
1039
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001040 out:
1041 read_unlock(&sk->sk_callback_lock);
1042}
Chuck Levera246b012005-08-11 16:25:23 -04001043
Chuck Leverc7b2cae2005-08-11 16:25:50 -04001044/**
1045 * xs_tcp_write_space - callback invoked when socket buffer space
1046 * becomes available
1047 * @sk: socket whose state has changed
1048 *
1049 * Called when more output buffer space is available for this socket.
1050 * We try not to wake our writers until they can make "significant"
1051 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1052 * with a bunch of small requests.
1053 */
1054static void xs_tcp_write_space(struct sock *sk)
1055{
1056 read_lock(&sk->sk_callback_lock);
1057
1058 /* from net/core/stream.c:sk_stream_write_space */
1059 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
1060 struct socket *sock;
1061 struct rpc_xprt *xprt;
1062
1063 if (unlikely(!(sock = sk->sk_socket)))
1064 goto out;
1065 if (unlikely(!(xprt = xprt_from_sock(sk))))
1066 goto out;
1067 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
1068 goto out;
1069
1070 xprt_write_space(xprt);
1071 }
1072
1073 out:
Chuck Levera246b012005-08-11 16:25:23 -04001074 read_unlock(&sk->sk_callback_lock);
1075}
1076
Chuck Lever470056c2005-08-25 16:25:56 -07001077static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001078{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001079 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1080 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001081
Chuck Lever7c6e0662006-12-05 16:35:30 -05001082 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001083 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001084 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001085 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001086 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001087 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001088 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001089 sk->sk_write_space(sk);
1090 }
1091}
1092
Chuck Lever43118c22005-08-25 16:25:49 -07001093/**
Chuck Lever470056c2005-08-25 16:25:56 -07001094 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001095 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001096 * @sndsize: requested size of send buffer, in bytes
1097 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001098 *
Chuck Lever470056c2005-08-25 16:25:56 -07001099 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001100 */
Chuck Lever470056c2005-08-25 16:25:56 -07001101static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001102{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001103 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1104
1105 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001106 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001107 transport->sndsize = sndsize + 1024;
1108 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001109 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001110 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001111
1112 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001113}
1114
Chuck Lever46c0ee82005-08-25 16:25:52 -07001115/**
1116 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1117 * @task: task that timed out
1118 *
1119 * Adjust the congestion window after a retransmit timeout has occurred.
1120 */
1121static void xs_udp_timer(struct rpc_task *task)
1122{
1123 xprt_adjust_cwnd(task, -ETIMEDOUT);
1124}
1125
Chuck Leverb85d8802006-05-25 01:40:49 -04001126static unsigned short xs_get_random_port(void)
1127{
1128 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1129 unsigned short rand = (unsigned short) net_random() % range;
1130 return rand + xprt_min_resvport;
1131}
1132
Chuck Lever92200412006-01-03 09:55:51 +01001133/**
1134 * xs_set_port - reset the port number in the remote endpoint address
1135 * @xprt: generic transport
1136 * @port: new port number
1137 *
1138 */
1139static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1140{
Chuck Leverc4efcb12006-08-22 20:06:19 -04001141 struct sockaddr_in *sap = (struct sockaddr_in *) &xprt->addr;
1142
Chuck Lever92200412006-01-03 09:55:51 +01001143 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001144
1145 sap->sin_port = htons(port);
Chuck Lever92200412006-01-03 09:55:51 +01001146}
1147
Chuck Leverc8475462006-12-05 16:35:26 -05001148static int xs_bindresvport(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001149{
1150 struct sockaddr_in myaddr = {
1151 .sin_family = AF_INET,
1152 };
Chuck Lever529b33c2005-08-25 16:25:54 -07001153 int err;
Chuck Leverc8475462006-12-05 16:35:26 -05001154 unsigned short port = transport->port;
Chuck Levera246b012005-08-11 16:25:23 -04001155
Chuck Levera246b012005-08-11 16:25:23 -04001156 do {
1157 myaddr.sin_port = htons(port);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001158 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
Chuck Levera246b012005-08-11 16:25:23 -04001159 sizeof(myaddr));
1160 if (err == 0) {
Chuck Leverc8475462006-12-05 16:35:26 -05001161 transport->port = port;
Chuck Lever9903cd12005-08-11 16:25:26 -04001162 dprintk("RPC: xs_bindresvport bound to port %u\n",
1163 port);
Chuck Levera246b012005-08-11 16:25:23 -04001164 return 0;
1165 }
Chuck Lever529b33c2005-08-25 16:25:54 -07001166 if (port <= xprt_min_resvport)
1167 port = xprt_max_resvport;
1168 else
1169 port--;
Chuck Leverc8475462006-12-05 16:35:26 -05001170 } while (err == -EADDRINUSE && port != transport->port);
Chuck Levera246b012005-08-11 16:25:23 -04001171
Chuck Lever9903cd12005-08-11 16:25:26 -04001172 dprintk("RPC: can't bind to reserved port (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001173 return err;
1174}
1175
Chuck Lever9903cd12005-08-11 16:25:26 -04001176/**
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001177 * xs_udp_connect_worker - set up a UDP socket
Chuck Lever9903cd12005-08-11 16:25:26 -04001178 * @args: RPC transport to connect
1179 *
1180 * Invoked by a work queue tasklet.
Chuck Levera246b012005-08-11 16:25:23 -04001181 */
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001182static void xs_udp_connect_worker(void *args)
Chuck Levera246b012005-08-11 16:25:23 -04001183{
Chuck Leverc8475462006-12-05 16:35:26 -05001184 struct sock_xprt *transport = (struct sock_xprt *)args;
1185 struct rpc_xprt *xprt = &transport->xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001186 struct socket *sock = transport->sock;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001187 int err, status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04001188
Chuck Leverec739ef2006-08-22 20:06:15 -04001189 if (xprt->shutdown || !xprt_bound(xprt))
Chuck Levera246b012005-08-11 16:25:23 -04001190 goto out;
1191
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001192 /* Start by resetting any existing state */
Chuck Lever9903cd12005-08-11 16:25:26 -04001193 xs_close(xprt);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001194
1195 if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
1196 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001197 goto out;
1198 }
Chuck Levera246b012005-08-11 16:25:23 -04001199
Chuck Leverc8475462006-12-05 16:35:26 -05001200 if (xprt->resvport && xs_bindresvport(transport, sock) < 0) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001201 sock_release(sock);
1202 goto out;
1203 }
1204
Chuck Leveredb267a2006-08-22 20:06:18 -04001205 dprintk("RPC: worker connecting xprt %p to address: %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05001206 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04001207
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001208 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001209 struct sock *sk = sock->sk;
1210
1211 write_lock_bh(&sk->sk_callback_lock);
1212
1213 sk->sk_user_data = xprt;
Chuck Lever314dfd72006-12-05 16:35:34 -05001214 transport->old_data_ready = sk->sk_data_ready;
1215 transport->old_state_change = sk->sk_state_change;
1216 transport->old_write_space = sk->sk_write_space;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001217 sk->sk_data_ready = xs_udp_data_ready;
1218 sk->sk_write_space = xs_udp_write_space;
1219 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa72005-12-13 16:13:52 -05001220 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001221
1222 xprt_set_connected(xprt);
1223
1224 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001225 transport->sock = sock;
1226 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001227
1228 write_unlock_bh(&sk->sk_callback_lock);
1229 }
Chuck Lever470056c2005-08-25 16:25:56 -07001230 xs_udp_do_set_buffer_size(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001231 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001232out:
1233 xprt_wake_pending_tasks(xprt, status);
1234 xprt_clear_connecting(xprt);
1235}
1236
Chuck Lever3167e122005-08-25 16:25:55 -07001237/*
1238 * We need to preserve the port number so the reply cache on the server can
1239 * find our cached RPC replies when we get around to reconnecting.
1240 */
1241static void xs_tcp_reuse_connection(struct rpc_xprt *xprt)
1242{
1243 int result;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001244 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever3167e122005-08-25 16:25:55 -07001245 struct sockaddr any;
1246
1247 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
1248
1249 /*
1250 * Disconnect the transport socket by doing a connect operation
1251 * with AF_UNSPEC. This should return immediately...
1252 */
1253 memset(&any, 0, sizeof(any));
1254 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001255 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Chuck Lever3167e122005-08-25 16:25:55 -07001256 if (result)
1257 dprintk("RPC: AF_UNSPEC connect return code %d\n",
1258 result);
1259}
1260
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001261/**
1262 * xs_tcp_connect_worker - connect a TCP socket to a remote endpoint
1263 * @args: RPC transport to connect
1264 *
1265 * Invoked by a work queue tasklet.
1266 */
1267static void xs_tcp_connect_worker(void *args)
1268{
Chuck Leverc8475462006-12-05 16:35:26 -05001269 struct sock_xprt *transport = (struct sock_xprt *)args;
1270 struct rpc_xprt *xprt = &transport->xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001271 struct socket *sock = transport->sock;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001272 int err, status = -EIO;
1273
Chuck Leverec739ef2006-08-22 20:06:15 -04001274 if (xprt->shutdown || !xprt_bound(xprt))
Chuck Levera246b012005-08-11 16:25:23 -04001275 goto out;
1276
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001277 if (!sock) {
Chuck Lever3167e122005-08-25 16:25:55 -07001278 /* start from scratch */
1279 if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
1280 dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
1281 goto out;
1282 }
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001283
Chuck Leverc8475462006-12-05 16:35:26 -05001284 if (xprt->resvport && xs_bindresvport(transport, sock) < 0) {
Chuck Lever3167e122005-08-25 16:25:55 -07001285 sock_release(sock);
1286 goto out;
1287 }
1288 } else
1289 /* "close" the socket, preserving the local port */
1290 xs_tcp_reuse_connection(xprt);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001291
Chuck Leveredb267a2006-08-22 20:06:18 -04001292 dprintk("RPC: worker connecting xprt %p to address: %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05001293 xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04001294
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001295 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001296 struct sock *sk = sock->sk;
1297
1298 write_lock_bh(&sk->sk_callback_lock);
1299
1300 sk->sk_user_data = xprt;
Chuck Lever314dfd72006-12-05 16:35:34 -05001301 transport->old_data_ready = sk->sk_data_ready;
1302 transport->old_state_change = sk->sk_state_change;
1303 transport->old_write_space = sk->sk_write_space;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001304 sk->sk_data_ready = xs_tcp_data_ready;
1305 sk->sk_state_change = xs_tcp_state_change;
1306 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebustb079fa72005-12-13 16:13:52 -05001307 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07001308
1309 /* socket options */
1310 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1311 sock_reset_flag(sk, SOCK_LINGER);
1312 tcp_sk(sk)->linger2 = 0;
1313 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001314
1315 xprt_clear_connected(xprt);
1316
1317 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001318 transport->sock = sock;
1319 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001320
1321 write_unlock_bh(&sk->sk_callback_lock);
1322 }
1323
1324 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05001325 xprt->stat.connect_count++;
1326 xprt->stat.connect_start = jiffies;
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001327 status = kernel_connect(sock, (struct sockaddr *) &xprt->addr,
Chuck Leverc4efcb12006-08-22 20:06:19 -04001328 xprt->addrlen, O_NONBLOCK);
Chuck Levera246b012005-08-11 16:25:23 -04001329 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1330 xprt, -status, xprt_connected(xprt), sock->sk->sk_state);
1331 if (status < 0) {
1332 switch (status) {
1333 case -EINPROGRESS:
1334 case -EALREADY:
1335 goto out_clear;
Chuck Lever3167e122005-08-25 16:25:55 -07001336 case -ECONNREFUSED:
1337 case -ECONNRESET:
1338 /* retry with existing socket, after a delay */
1339 break;
1340 default:
1341 /* get rid of existing socket, and retry */
1342 xs_close(xprt);
1343 break;
Chuck Levera246b012005-08-11 16:25:23 -04001344 }
1345 }
1346out:
Chuck Lever44fbac22005-08-11 16:25:44 -04001347 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001348out_clear:
Chuck Lever2226feb2005-08-11 16:25:38 -04001349 xprt_clear_connecting(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001350}
1351
Chuck Lever9903cd12005-08-11 16:25:26 -04001352/**
1353 * xs_connect - connect a socket to a remote endpoint
1354 * @task: address of RPC task that manages state of connect request
1355 *
1356 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07001357 *
1358 * UDP socket connects are synchronous, but we use a work queue anyway
1359 * to guarantee that even unprivileged user processes can set up a
1360 * socket on a privileged port.
1361 *
1362 * If a UDP socket connect fails, the delay behavior here prevents
1363 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04001364 */
1365static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04001366{
1367 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001368 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001369
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001370 if (xprt_test_and_set_connecting(xprt))
1371 return;
1372
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001373 if (transport->sock != NULL) {
Chuck Lever03bf4b72005-08-25 16:25:55 -07001374 dprintk("RPC: xs_connect delayed xprt %p for %lu seconds\n",
1375 xprt, xprt->reestablish_timeout / HZ);
Chuck Leverc8475462006-12-05 16:35:26 -05001376 schedule_delayed_work(&transport->connect_worker,
Chuck Lever03bf4b72005-08-25 16:25:55 -07001377 xprt->reestablish_timeout);
1378 xprt->reestablish_timeout <<= 1;
1379 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
1380 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001381 } else {
1382 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Chuck Leverc8475462006-12-05 16:35:26 -05001383 schedule_work(&transport->connect_worker);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001384
1385 /* flush_scheduled_work can sleep... */
1386 if (!RPC_IS_ASYNC(task))
1387 flush_scheduled_work();
Chuck Levera246b012005-08-11 16:25:23 -04001388 }
1389}
1390
Chuck Lever262ca072006-03-20 13:44:16 -05001391/**
1392 * xs_udp_print_stats - display UDP socket-specifc stats
1393 * @xprt: rpc_xprt struct containing statistics
1394 * @seq: output file
1395 *
1396 */
1397static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1398{
Chuck Leverc8475462006-12-05 16:35:26 -05001399 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1400
Chuck Lever262ca072006-03-20 13:44:16 -05001401 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
Chuck Leverc8475462006-12-05 16:35:26 -05001402 transport->port,
Chuck Lever262ca072006-03-20 13:44:16 -05001403 xprt->stat.bind_count,
1404 xprt->stat.sends,
1405 xprt->stat.recvs,
1406 xprt->stat.bad_xids,
1407 xprt->stat.req_u,
1408 xprt->stat.bklog_u);
1409}
1410
1411/**
1412 * xs_tcp_print_stats - display TCP socket-specifc stats
1413 * @xprt: rpc_xprt struct containing statistics
1414 * @seq: output file
1415 *
1416 */
1417static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1418{
Chuck Leverc8475462006-12-05 16:35:26 -05001419 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05001420 long idle_time = 0;
1421
1422 if (xprt_connected(xprt))
1423 idle_time = (long)(jiffies - xprt->last_used) / HZ;
1424
1425 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
Chuck Leverc8475462006-12-05 16:35:26 -05001426 transport->port,
Chuck Lever262ca072006-03-20 13:44:16 -05001427 xprt->stat.bind_count,
1428 xprt->stat.connect_count,
1429 xprt->stat.connect_time,
1430 idle_time,
1431 xprt->stat.sends,
1432 xprt->stat.recvs,
1433 xprt->stat.bad_xids,
1434 xprt->stat.req_u,
1435 xprt->stat.bklog_u);
1436}
1437
Chuck Lever262965f2005-08-11 16:25:56 -04001438static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07001439 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07001440 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07001441 .release_xprt = xprt_release_xprt_cong,
Chuck Leverbbf7c1d2006-08-22 20:06:16 -04001442 .rpcbind = rpc_getport,
Chuck Lever92200412006-01-03 09:55:51 +01001443 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04001444 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01001445 .buf_alloc = rpc_malloc,
1446 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04001447 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001448 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07001449 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07001450 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04001451 .close = xs_close,
1452 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05001453 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04001454};
1455
1456static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07001457 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001458 .release_xprt = xs_tcp_release_xprt,
Chuck Leverbbf7c1d2006-08-22 20:06:16 -04001459 .rpcbind = rpc_getport,
Chuck Lever92200412006-01-03 09:55:51 +01001460 .set_port = xs_set_port,
Chuck Lever262965f2005-08-11 16:25:56 -04001461 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01001462 .buf_alloc = rpc_malloc,
1463 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04001464 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001465 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Chuck Lever9903cd12005-08-11 16:25:26 -04001466 .close = xs_close,
1467 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05001468 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04001469};
1470
Chuck Leverc8541ec2006-10-17 14:44:27 -04001471static struct rpc_xprt *xs_setup_xprt(struct sockaddr *addr, size_t addrlen, unsigned int slot_table_size)
1472{
1473 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05001474 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04001475
1476 if (addrlen > sizeof(xprt->addr)) {
1477 dprintk("RPC: xs_setup_xprt: address too large\n");
1478 return ERR_PTR(-EBADF);
1479 }
1480
Chuck Leverffc2e512006-12-05 16:35:11 -05001481 new = kzalloc(sizeof(*new), GFP_KERNEL);
1482 if (new == NULL) {
Chuck Leverc8541ec2006-10-17 14:44:27 -04001483 dprintk("RPC: xs_setup_xprt: couldn't allocate rpc_xprt\n");
1484 return ERR_PTR(-ENOMEM);
1485 }
Chuck Leverffc2e512006-12-05 16:35:11 -05001486 xprt = &new->xprt;
Chuck Leverc8541ec2006-10-17 14:44:27 -04001487
1488 xprt->max_reqs = slot_table_size;
1489 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
1490 if (xprt->slot == NULL) {
1491 kfree(xprt);
1492 dprintk("RPC: xs_setup_xprt: couldn't allocate slot table\n");
1493 return ERR_PTR(-ENOMEM);
1494 }
1495
1496 memcpy(&xprt->addr, addr, addrlen);
1497 xprt->addrlen = addrlen;
Chuck Leverc8475462006-12-05 16:35:26 -05001498 new->port = xs_get_random_port();
Chuck Leverc8541ec2006-10-17 14:44:27 -04001499
1500 return xprt;
1501}
1502
Chuck Lever9903cd12005-08-11 16:25:26 -04001503/**
1504 * xs_setup_udp - Set up transport to use a UDP socket
Chuck Leverc8541ec2006-10-17 14:44:27 -04001505 * @addr: address of remote server
1506 * @addrlen: length of address in bytes
Chuck Lever9903cd12005-08-11 16:25:26 -04001507 * @to: timeout parameters
1508 *
1509 */
Chuck Leverc8541ec2006-10-17 14:44:27 -04001510struct rpc_xprt *xs_setup_udp(struct sockaddr *addr, size_t addrlen, struct rpc_timeout *to)
Chuck Levera246b012005-08-11 16:25:23 -04001511{
Chuck Leverc8541ec2006-10-17 14:44:27 -04001512 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001513 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001514
Chuck Leverc8541ec2006-10-17 14:44:27 -04001515 xprt = xs_setup_xprt(addr, addrlen, xprt_udp_slot_table_entries);
1516 if (IS_ERR(xprt))
1517 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001518 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001519
Chuck Leverc8541ec2006-10-17 14:44:27 -04001520 if (ntohs(((struct sockaddr_in *)addr)->sin_port) != 0)
Chuck Leverec739ef2006-08-22 20:06:15 -04001521 xprt_set_bound(xprt);
Chuck Leverec739ef2006-08-22 20:06:15 -04001522
1523 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07001524 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001525 /* XXX: header size can vary due to auth type, IPv6, etc. */
1526 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
1527
Chuck Leverc8475462006-12-05 16:35:26 -05001528 INIT_WORK(&transport->connect_worker, xs_udp_connect_worker, transport);
Chuck Lever03bf4b72005-08-25 16:25:55 -07001529 xprt->bind_timeout = XS_BIND_TO;
1530 xprt->connect_timeout = XS_UDP_CONN_TO;
1531 xprt->reestablish_timeout = XS_UDP_REEST_TO;
1532 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04001533
Chuck Lever262965f2005-08-11 16:25:56 -04001534 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001535
1536 if (to)
1537 xprt->timeout = *to;
1538 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001539 xprt_set_timeout(&xprt->timeout, 5, 5 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001540
Chuck Leveredb267a2006-08-22 20:06:18 -04001541 xs_format_peer_addresses(xprt);
1542 dprintk("RPC: set up transport to address %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05001543 xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04001544
Chuck Leverc8541ec2006-10-17 14:44:27 -04001545 return xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001546}
1547
Chuck Lever9903cd12005-08-11 16:25:26 -04001548/**
1549 * xs_setup_tcp - Set up transport to use a TCP socket
Chuck Leverc8541ec2006-10-17 14:44:27 -04001550 * @addr: address of remote server
1551 * @addrlen: length of address in bytes
Chuck Lever9903cd12005-08-11 16:25:26 -04001552 * @to: timeout parameters
1553 *
1554 */
Chuck Leverc8541ec2006-10-17 14:44:27 -04001555struct rpc_xprt *xs_setup_tcp(struct sockaddr *addr, size_t addrlen, struct rpc_timeout *to)
Chuck Levera246b012005-08-11 16:25:23 -04001556{
Chuck Leverc8541ec2006-10-17 14:44:27 -04001557 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001558 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001559
Chuck Leverc8541ec2006-10-17 14:44:27 -04001560 xprt = xs_setup_xprt(addr, addrlen, xprt_tcp_slot_table_entries);
1561 if (IS_ERR(xprt))
1562 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001563 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001564
Chuck Leverc8541ec2006-10-17 14:44:27 -04001565 if (ntohs(((struct sockaddr_in *)addr)->sin_port) != 0)
Chuck Leverec739ef2006-08-22 20:06:15 -04001566 xprt_set_bound(xprt);
Chuck Leverec739ef2006-08-22 20:06:15 -04001567
1568 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07001569 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07001570 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04001571
Chuck Leverc8475462006-12-05 16:35:26 -05001572 INIT_WORK(&transport->connect_worker, xs_tcp_connect_worker, transport);
Chuck Lever03bf4b72005-08-25 16:25:55 -07001573 xprt->bind_timeout = XS_BIND_TO;
1574 xprt->connect_timeout = XS_TCP_CONN_TO;
1575 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1576 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04001577
Chuck Lever262965f2005-08-11 16:25:56 -04001578 xprt->ops = &xs_tcp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001579
1580 if (to)
1581 xprt->timeout = *to;
1582 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001583 xprt_set_timeout(&xprt->timeout, 2, 60 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001584
Chuck Leveredb267a2006-08-22 20:06:18 -04001585 xs_format_peer_addresses(xprt);
1586 dprintk("RPC: set up transport to address %s\n",
Chuck Lever7559c7a2006-12-05 16:35:37 -05001587 xprt->address_strings[RPC_DISPLAY_ALL]);
Chuck Leveredb267a2006-08-22 20:06:18 -04001588
Chuck Leverc8541ec2006-10-17 14:44:27 -04001589 return xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001590}
Chuck Lever282b32e2006-12-05 16:35:51 -05001591
1592/**
Chuck Leverfbf76682006-12-05 16:35:54 -05001593 * init_socket_xprt - set up xprtsock's sysctls
Chuck Lever282b32e2006-12-05 16:35:51 -05001594 *
1595 */
1596int init_socket_xprt(void)
1597{
Chuck Leverfbf76682006-12-05 16:35:54 -05001598#ifdef RPC_DEBUG
1599 if (!sunrpc_table_header) {
1600 sunrpc_table_header = register_sysctl_table(sunrpc_table, 1);
1601#ifdef CONFIG_PROC_FS
1602 if (sunrpc_table[0].de)
1603 sunrpc_table[0].de->owner = THIS_MODULE;
1604#endif
1605 }
1606#endif
1607
Chuck Lever282b32e2006-12-05 16:35:51 -05001608 return 0;
1609}
1610
1611/**
Chuck Leverfbf76682006-12-05 16:35:54 -05001612 * cleanup_socket_xprt - remove xprtsock's sysctls
Chuck Lever282b32e2006-12-05 16:35:51 -05001613 *
1614 */
1615void cleanup_socket_xprt(void)
1616{
Chuck Leverfbf76682006-12-05 16:35:54 -05001617#ifdef RPC_DEBUG
1618 if (sunrpc_table_header) {
1619 unregister_sysctl_table(sunrpc_table_header);
1620 sunrpc_table_header = NULL;
1621 }
1622#endif
Chuck Lever282b32e2006-12-05 16:35:51 -05001623}