blob: b804381d5bee433e381d0eda14eb37b0840724dc [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 Lever7c6e0662006-12-05 16:35:30 -0500154
155 /*
156 * UDP socket buffer size parameters
157 */
158 size_t rcvsize,
159 sndsize;
Chuck Leverffc2e512006-12-05 16:35:11 -0500160};
161
Chuck Levere136d092006-12-05 16:35:23 -0500162/*
163 * TCP receive state flags
164 */
165#define TCP_RCV_LAST_FRAG (1UL << 0)
166#define TCP_RCV_COPY_FRAGHDR (1UL << 1)
167#define TCP_RCV_COPY_XID (1UL << 2)
168#define TCP_RCV_COPY_DATA (1UL << 3)
169
Chuck Leveredb267a2006-08-22 20:06:18 -0400170static void xs_format_peer_addresses(struct rpc_xprt *xprt)
171{
172 struct sockaddr_in *addr = (struct sockaddr_in *) &xprt->addr;
173 char *buf;
174
175 buf = kzalloc(20, GFP_KERNEL);
176 if (buf) {
177 snprintf(buf, 20, "%u.%u.%u.%u",
178 NIPQUAD(addr->sin_addr.s_addr));
179 }
180 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
181
182 buf = kzalloc(8, GFP_KERNEL);
183 if (buf) {
184 snprintf(buf, 8, "%u",
185 ntohs(addr->sin_port));
186 }
187 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
188
189 if (xprt->prot == IPPROTO_UDP)
190 xprt->address_strings[RPC_DISPLAY_PROTO] = "udp";
191 else
192 xprt->address_strings[RPC_DISPLAY_PROTO] = "tcp";
193
194 buf = kzalloc(48, GFP_KERNEL);
195 if (buf) {
196 snprintf(buf, 48, "addr=%u.%u.%u.%u port=%u proto=%s",
197 NIPQUAD(addr->sin_addr.s_addr),
198 ntohs(addr->sin_port),
199 xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
200 }
201 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
202}
203
204static void xs_free_peer_addresses(struct rpc_xprt *xprt)
205{
206 kfree(xprt->address_strings[RPC_DISPLAY_ADDR]);
207 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
208 kfree(xprt->address_strings[RPC_DISPLAY_ALL]);
209}
210
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400211#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
212
Trond Myklebust24c56842006-10-17 15:06:22 -0400213static 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 -0400214{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400215 struct msghdr msg = {
216 .msg_name = addr,
217 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400218 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
219 };
220 struct kvec iov = {
221 .iov_base = vec->iov_base + base,
222 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400223 };
224
Trond Myklebust24c56842006-10-17 15:06:22 -0400225 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400226 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
227 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
228}
229
Trond Myklebust24c56842006-10-17 15:06:22 -0400230static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400231{
Trond Myklebust24c56842006-10-17 15:06:22 -0400232 struct page **ppage;
233 unsigned int remainder;
234 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400235
Trond Myklebust24c56842006-10-17 15:06:22 -0400236 remainder = xdr->page_len - base;
237 base += xdr->page_base;
238 ppage = xdr->pages + (base >> PAGE_SHIFT);
239 base &= ~PAGE_MASK;
240 for(;;) {
241 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
242 int flags = XS_SENDMSG_FLAGS;
243
244 remainder -= len;
245 if (remainder != 0 || more)
246 flags |= MSG_MORE;
247 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
248 if (remainder == 0 || err != len)
249 break;
250 sent += err;
251 ppage++;
252 base = 0;
253 }
254 if (sent == 0)
255 return err;
256 if (err > 0)
257 sent += err;
258 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400259}
260
Chuck Lever9903cd12005-08-11 16:25:26 -0400261/**
262 * xs_sendpages - write pages directly to a socket
263 * @sock: socket to send on
264 * @addr: UDP only -- address of destination
265 * @addrlen: UDP only -- length of destination address
266 * @xdr: buffer containing this request
267 * @base: starting position in the buffer
268 *
Chuck Levera246b012005-08-11 16:25:23 -0400269 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400270static 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 -0400271{
Trond Myklebust24c56842006-10-17 15:06:22 -0400272 unsigned int remainder = xdr->len - base;
273 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400274
Chuck Lever262965f2005-08-11 16:25:56 -0400275 if (unlikely(!sock))
276 return -ENOTCONN;
277
278 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400279 if (base != 0) {
280 addr = NULL;
281 addrlen = 0;
282 }
Chuck Lever262965f2005-08-11 16:25:56 -0400283
Trond Myklebust24c56842006-10-17 15:06:22 -0400284 if (base < xdr->head[0].iov_len || addr != NULL) {
285 unsigned int len = xdr->head[0].iov_len - base;
286 remainder -= len;
287 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
288 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400289 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400290 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400291 base = 0;
292 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400293 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400294
Trond Myklebust24c56842006-10-17 15:06:22 -0400295 if (base < xdr->page_len) {
296 unsigned int len = xdr->page_len - base;
297 remainder -= len;
298 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
299 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400300 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400301 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400302 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400303 } else
304 base -= xdr->page_len;
305
306 if (base >= xdr->tail[0].iov_len)
307 return sent;
308 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400309out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400310 if (sent == 0)
311 return err;
312 if (err > 0)
313 sent += err;
314 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400315}
316
Chuck Lever9903cd12005-08-11 16:25:26 -0400317/**
Chuck Lever262965f2005-08-11 16:25:56 -0400318 * xs_nospace - place task on wait queue if transmit was incomplete
319 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400320 *
Chuck Levera246b012005-08-11 16:25:23 -0400321 */
Chuck Lever262965f2005-08-11 16:25:56 -0400322static void xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400323{
Chuck Lever262965f2005-08-11 16:25:56 -0400324 struct rpc_rqst *req = task->tk_rqstp;
325 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500326 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400327
Chuck Lever262965f2005-08-11 16:25:56 -0400328 dprintk("RPC: %4d xmit incomplete (%u left of %u)\n",
329 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
330 req->rq_slen);
331
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500332 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
Chuck Lever262965f2005-08-11 16:25:56 -0400333 /* Protect against races with write_space */
334 spin_lock_bh(&xprt->transport_lock);
335
336 /* Don't race with disconnect */
337 if (!xprt_connected(xprt))
338 task->tk_status = -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500339 else if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
Chuck Lever262965f2005-08-11 16:25:56 -0400340 xprt_wait_for_buffer_space(task);
341
342 spin_unlock_bh(&xprt->transport_lock);
343 } else
344 /* Keep holding the socket if it is blocked */
345 rpc_delay(task, HZ>>4);
346}
347
348/**
349 * xs_udp_send_request - write an RPC request to a UDP socket
350 * @task: address of RPC task that manages the state of an RPC request
351 *
352 * Return values:
353 * 0: The request has been sent
354 * EAGAIN: The socket was blocked, please call again later to
355 * complete the request
356 * ENOTCONN: Caller needs to invoke connect logic then call again
357 * other: Some other error occured, the request was not sent
358 */
359static int xs_udp_send_request(struct rpc_task *task)
360{
361 struct rpc_rqst *req = task->tk_rqstp;
362 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500363 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400364 struct xdr_buf *xdr = &req->rq_snd_buf;
365 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400366
Chuck Lever9903cd12005-08-11 16:25:26 -0400367 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400368 req->rq_svec->iov_base,
369 req->rq_svec->iov_len);
370
Chuck Lever262965f2005-08-11 16:25:56 -0400371 req->rq_xtime = jiffies;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500372 status = xs_sendpages(transport->sock,
373 (struct sockaddr *) &xprt->addr,
374 xprt->addrlen, xdr,
375 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400376
Chuck Lever262965f2005-08-11 16:25:56 -0400377 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
378 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400379
Chuck Lever262965f2005-08-11 16:25:56 -0400380 if (likely(status >= (int) req->rq_slen))
381 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400382
Chuck Lever262965f2005-08-11 16:25:56 -0400383 /* Still some bytes left; set up for a retry later. */
384 if (status > 0)
385 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400386
Chuck Lever262965f2005-08-11 16:25:56 -0400387 switch (status) {
388 case -ENETUNREACH:
389 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400390 case -ECONNREFUSED:
391 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400392 * prompts ECONNREFUSED. */
Chuck Levera246b012005-08-11 16:25:23 -0400393 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400394 case -EAGAIN:
395 xs_nospace(task);
Chuck Levera246b012005-08-11 16:25:23 -0400396 break;
397 default:
Chuck Lever262965f2005-08-11 16:25:56 -0400398 dprintk("RPC: sendmsg returned unrecognized error %d\n",
399 -status);
Chuck Lever9903cd12005-08-11 16:25:26 -0400400 break;
Chuck Levera246b012005-08-11 16:25:23 -0400401 }
Chuck Lever262965f2005-08-11 16:25:56 -0400402
403 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400404}
405
Chuck Lever808012f2005-08-25 16:25:49 -0700406static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
407{
408 u32 reclen = buf->len - sizeof(rpc_fraghdr);
409 rpc_fraghdr *base = buf->head[0].iov_base;
410 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
411}
412
Chuck Lever9903cd12005-08-11 16:25:26 -0400413/**
Chuck Lever262965f2005-08-11 16:25:56 -0400414 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400415 * @task: address of RPC task that manages the state of an RPC request
416 *
417 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400418 * 0: The request has been sent
419 * EAGAIN: The socket was blocked, please call again later to
420 * complete the request
421 * ENOTCONN: Caller needs to invoke connect logic then call again
422 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400423 *
424 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400425 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400426 */
Chuck Lever262965f2005-08-11 16:25:56 -0400427static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400428{
429 struct rpc_rqst *req = task->tk_rqstp;
430 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500431 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400432 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Levera246b012005-08-11 16:25:23 -0400433 int status, retry = 0;
434
Chuck Lever808012f2005-08-25 16:25:49 -0700435 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400436
Chuck Lever262965f2005-08-11 16:25:56 -0400437 xs_pktdump("packet data:",
438 req->rq_svec->iov_base,
439 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400440
441 /* Continue transmitting the packet/record. We must be careful
442 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400443 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400444 while (1) {
445 req->rq_xtime = jiffies;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500446 status = xs_sendpages(transport->sock,
447 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400448
Chuck Lever262965f2005-08-11 16:25:56 -0400449 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
450 xdr->len - req->rq_bytes_sent, status);
451
452 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400453 break;
454
Chuck Lever262965f2005-08-11 16:25:56 -0400455 /* If we've sent the entire packet, immediately
456 * reset the count of bytes sent. */
457 req->rq_bytes_sent += status;
Chuck Leveref759a22006-03-20 13:44:17 -0500458 task->tk_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400459 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
460 req->rq_bytes_sent = 0;
461 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400462 }
463
Chuck Levera246b012005-08-11 16:25:23 -0400464 status = -EAGAIN;
Chuck Lever262965f2005-08-11 16:25:56 -0400465 if (retry++ > XS_SENDMSG_RETRY)
Chuck Levera246b012005-08-11 16:25:23 -0400466 break;
467 }
468
Chuck Lever262965f2005-08-11 16:25:56 -0400469 switch (status) {
470 case -EAGAIN:
471 xs_nospace(task);
472 break;
473 case -ECONNREFUSED:
474 case -ECONNRESET:
475 case -ENOTCONN:
476 case -EPIPE:
477 status = -ENOTCONN;
478 break;
479 default:
480 dprintk("RPC: sendmsg returned unrecognized error %d\n",
481 -status);
Chuck Lever43118c22005-08-25 16:25:49 -0700482 xprt_disconnect(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400483 break;
Chuck Levera246b012005-08-11 16:25:23 -0400484 }
Chuck Lever262965f2005-08-11 16:25:56 -0400485
Chuck Levera246b012005-08-11 16:25:23 -0400486 return status;
487}
488
Chuck Lever9903cd12005-08-11 16:25:26 -0400489/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400490 * xs_tcp_release_xprt - clean up after a tcp transmission
491 * @xprt: transport
492 * @task: rpc task
493 *
494 * This cleans up if an error causes us to abort the transmission of a request.
495 * In this case, the socket may need to be reset in order to avoid confusing
496 * the server.
497 */
498static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
499{
500 struct rpc_rqst *req;
501
502 if (task != xprt->snd_task)
503 return;
504 if (task == NULL)
505 goto out_release;
506 req = task->tk_rqstp;
507 if (req->rq_bytes_sent == 0)
508 goto out_release;
509 if (req->rq_bytes_sent == req->rq_snd_buf.len)
510 goto out_release;
511 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
512out_release:
513 xprt_release_xprt(xprt, task);
514}
515
516/**
Chuck Lever9903cd12005-08-11 16:25:26 -0400517 * xs_close - close a socket
518 * @xprt: transport
519 *
Chuck Lever3167e122005-08-25 16:25:55 -0700520 * This is used when all requests are complete; ie, no DRC state remains
521 * on the server we want to save.
Chuck Levera246b012005-08-11 16:25:23 -0400522 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400523static void xs_close(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400524{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500525 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
526 struct socket *sock = transport->sock;
527 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400528
529 if (!sk)
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100530 goto clear_close_wait;
Chuck Levera246b012005-08-11 16:25:23 -0400531
Chuck Lever9903cd12005-08-11 16:25:26 -0400532 dprintk("RPC: xs_close xprt %p\n", xprt);
533
Chuck Levera246b012005-08-11 16:25:23 -0400534 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500535 transport->inet = NULL;
536 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400537
Chuck Lever9903cd12005-08-11 16:25:26 -0400538 sk->sk_user_data = NULL;
539 sk->sk_data_ready = xprt->old_data_ready;
Chuck Levera246b012005-08-11 16:25:23 -0400540 sk->sk_state_change = xprt->old_state_change;
Chuck Lever9903cd12005-08-11 16:25:26 -0400541 sk->sk_write_space = xprt->old_write_space;
Chuck Levera246b012005-08-11 16:25:23 -0400542 write_unlock_bh(&sk->sk_callback_lock);
543
Chuck Lever9903cd12005-08-11 16:25:26 -0400544 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400545
546 sock_release(sock);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100547clear_close_wait:
548 smp_mb__before_clear_bit();
549 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
550 smp_mb__after_clear_bit();
Chuck Levera246b012005-08-11 16:25:23 -0400551}
552
Chuck Lever9903cd12005-08-11 16:25:26 -0400553/**
554 * xs_destroy - prepare to shutdown a transport
555 * @xprt: doomed transport
556 *
557 */
558static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400559{
Chuck Leverc8475462006-12-05 16:35:26 -0500560 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
561
Chuck Lever9903cd12005-08-11 16:25:26 -0400562 dprintk("RPC: xs_destroy xprt %p\n", xprt);
563
Chuck Leverc8475462006-12-05 16:35:26 -0500564 cancel_delayed_work(&transport->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400565 flush_scheduled_work();
566
567 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400568 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400569 xs_free_peer_addresses(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400570 kfree(xprt->slot);
Chuck Leverc8541ec2006-10-17 14:44:27 -0400571 kfree(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400572}
573
Chuck Lever9903cd12005-08-11 16:25:26 -0400574static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400575{
Chuck Lever9903cd12005-08-11 16:25:26 -0400576 return (struct rpc_xprt *) sk->sk_user_data;
577}
578
579/**
580 * xs_udp_data_ready - "data ready" callback for UDP sockets
581 * @sk: socket with data to read
582 * @len: how much data to read
583 *
584 */
585static void xs_udp_data_ready(struct sock *sk, int len)
586{
587 struct rpc_task *task;
588 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400589 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400590 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400591 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700592 u32 _xid;
593 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400594
595 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400596 dprintk("RPC: xs_udp_data_ready...\n");
597 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400598 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400599
600 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
601 goto out;
602
603 if (xprt->shutdown)
604 goto dropit;
605
606 repsize = skb->len - sizeof(struct udphdr);
607 if (repsize < 4) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400608 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400609 goto dropit;
610 }
611
612 /* Copy the XID from the skb... */
613 xp = skb_header_pointer(skb, sizeof(struct udphdr),
614 sizeof(_xid), &_xid);
615 if (xp == NULL)
616 goto dropit;
617
618 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400619 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400620 rovr = xprt_lookup_rqst(xprt, *xp);
621 if (!rovr)
622 goto out_unlock;
623 task = rovr->rq_task;
624
Chuck Levera246b012005-08-11 16:25:23 -0400625 if ((copied = rovr->rq_private_buf.buflen) > repsize)
626 copied = repsize;
627
628 /* Suck it into the iovec, verify checksum if not done by hw. */
629 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb))
630 goto out_unlock;
631
632 /* Something worked... */
633 dst_confirm(skb->dst);
634
Chuck Lever1570c1e2005-08-25 16:25:52 -0700635 xprt_adjust_cwnd(task, copied);
636 xprt_update_rtt(task);
637 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400638
639 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400640 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400641 dropit:
642 skb_free_datagram(sk, skb);
643 out:
644 read_unlock(&sk->sk_callback_lock);
645}
646
Chuck Lever9903cd12005-08-11 16:25:26 -0400647static inline size_t xs_tcp_copy_data(skb_reader_t *desc, void *p, size_t len)
Chuck Levera246b012005-08-11 16:25:23 -0400648{
649 if (len > desc->count)
650 len = desc->count;
651 if (skb_copy_bits(desc->skb, desc->offset, p, len)) {
652 dprintk("RPC: failed to copy %zu bytes from skb. %zu bytes remain\n",
653 len, desc->count);
654 return 0;
655 }
656 desc->offset += len;
657 desc->count -= len;
658 dprintk("RPC: copied %zu bytes from skb. %zu bytes remain\n",
659 len, desc->count);
660 return len;
661}
662
Chuck Lever9903cd12005-08-11 16:25:26 -0400663static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400664{
Chuck Lever51971132006-12-05 16:35:19 -0500665 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400666 size_t len, used;
667 char *p;
668
Chuck Lever51971132006-12-05 16:35:19 -0500669 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
670 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
Chuck Lever9903cd12005-08-11 16:25:26 -0400671 used = xs_tcp_copy_data(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500672 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400673 if (used != len)
674 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700675
Chuck Lever51971132006-12-05 16:35:19 -0500676 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
677 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levere136d092006-12-05 16:35:23 -0500678 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
Chuck Levera246b012005-08-11 16:25:23 -0400679 else
Chuck Levere136d092006-12-05 16:35:23 -0500680 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
Chuck Lever51971132006-12-05 16:35:19 -0500681 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
Chuck Lever808012f2005-08-25 16:25:49 -0700682
Chuck Levere136d092006-12-05 16:35:23 -0500683 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500684 transport->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700685
Chuck Levera246b012005-08-11 16:25:23 -0400686 /* Sanity check of the record length */
Chuck Lever51971132006-12-05 16:35:19 -0500687 if (unlikely(transport->tcp_reclen < 4)) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400688 dprintk("RPC: invalid TCP record fragment length\n");
Chuck Levera246b012005-08-11 16:25:23 -0400689 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400690 return;
Chuck Levera246b012005-08-11 16:25:23 -0400691 }
692 dprintk("RPC: reading TCP record fragment of length %d\n",
Chuck Lever51971132006-12-05 16:35:19 -0500693 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400694}
695
Chuck Lever51971132006-12-05 16:35:19 -0500696static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
Chuck Levera246b012005-08-11 16:25:23 -0400697{
Chuck Lever51971132006-12-05 16:35:19 -0500698 if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500699 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
Chuck Lever51971132006-12-05 16:35:19 -0500700 transport->tcp_offset = 0;
Chuck Levere136d092006-12-05 16:35:23 -0500701 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
702 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
703 transport->tcp_flags |= TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -0500704 transport->tcp_copied = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400705 }
706 }
707}
708
Chuck Lever51971132006-12-05 16:35:19 -0500709static inline void xs_tcp_read_xid(struct sock_xprt *transport, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400710{
711 size_t len, used;
712 char *p;
713
Chuck Lever51971132006-12-05 16:35:19 -0500714 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -0400715 dprintk("RPC: reading XID (%Zu bytes)\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500716 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
Chuck Lever9903cd12005-08-11 16:25:26 -0400717 used = xs_tcp_copy_data(desc, p, len);
Chuck Lever51971132006-12-05 16:35:19 -0500718 transport->tcp_offset += used;
Chuck Levera246b012005-08-11 16:25:23 -0400719 if (used != len)
720 return;
Chuck Levere136d092006-12-05 16:35:23 -0500721 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
722 transport->tcp_flags |= TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -0500723 transport->tcp_copied = 4;
Chuck Levera246b012005-08-11 16:25:23 -0400724 dprintk("RPC: reading reply for XID %08x\n",
Chuck Lever51971132006-12-05 16:35:19 -0500725 ntohl(transport->tcp_xid));
726 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400727}
728
Chuck Lever9903cd12005-08-11 16:25:26 -0400729static inline void xs_tcp_read_request(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400730{
Chuck Lever51971132006-12-05 16:35:19 -0500731 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400732 struct rpc_rqst *req;
733 struct xdr_buf *rcvbuf;
734 size_t len;
735 ssize_t r;
736
737 /* Find and lock the request corresponding to this xid */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400738 spin_lock(&xprt->transport_lock);
Chuck Lever51971132006-12-05 16:35:19 -0500739 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
Chuck Levera246b012005-08-11 16:25:23 -0400740 if (!req) {
Chuck Levere136d092006-12-05 16:35:23 -0500741 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -0400742 dprintk("RPC: XID %08x request not found!\n",
Chuck Lever51971132006-12-05 16:35:19 -0500743 ntohl(transport->tcp_xid));
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400744 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400745 return;
746 }
747
748 rcvbuf = &req->rq_private_buf;
749 len = desc->count;
Chuck Lever51971132006-12-05 16:35:19 -0500750 if (len > transport->tcp_reclen - transport->tcp_offset) {
Chuck Levera246b012005-08-11 16:25:23 -0400751 skb_reader_t my_desc;
752
Chuck Lever51971132006-12-05 16:35:19 -0500753 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -0400754 memcpy(&my_desc, desc, sizeof(my_desc));
755 my_desc.count = len;
Chuck Lever51971132006-12-05 16:35:19 -0500756 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9903cd12005-08-11 16:25:26 -0400757 &my_desc, xs_tcp_copy_data);
Chuck Levera246b012005-08-11 16:25:23 -0400758 desc->count -= r;
759 desc->offset += r;
760 } else
Chuck Lever51971132006-12-05 16:35:19 -0500761 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
Chuck Lever9903cd12005-08-11 16:25:26 -0400762 desc, xs_tcp_copy_data);
Chuck Levera246b012005-08-11 16:25:23 -0400763
764 if (r > 0) {
Chuck Lever51971132006-12-05 16:35:19 -0500765 transport->tcp_copied += r;
766 transport->tcp_offset += r;
Chuck Levera246b012005-08-11 16:25:23 -0400767 }
768 if (r != len) {
769 /* Error when copying to the receive buffer,
770 * usually because we weren't able to allocate
771 * additional buffer pages. All we can do now
Chuck Levere136d092006-12-05 16:35:23 -0500772 * is turn off TCP_RCV_COPY_DATA, so the request
Chuck Levera246b012005-08-11 16:25:23 -0400773 * will not receive any additional updates,
774 * and time out.
775 * Any remaining data from this record will
776 * be discarded.
777 */
Chuck Levere136d092006-12-05 16:35:23 -0500778 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -0400779 dprintk("RPC: XID %08x truncated request\n",
Chuck Lever51971132006-12-05 16:35:19 -0500780 ntohl(transport->tcp_xid));
Chuck Levera246b012005-08-11 16:25:23 -0400781 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
Chuck Lever51971132006-12-05 16:35:19 -0500782 xprt, transport->tcp_copied, transport->tcp_offset,
783 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400784 goto out;
785 }
786
787 dprintk("RPC: XID %08x read %Zd bytes\n",
Chuck Lever51971132006-12-05 16:35:19 -0500788 ntohl(transport->tcp_xid), r);
Chuck Levera246b012005-08-11 16:25:23 -0400789 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
Chuck Lever51971132006-12-05 16:35:19 -0500790 xprt, transport->tcp_copied, transport->tcp_offset,
791 transport->tcp_reclen);
Chuck Levera246b012005-08-11 16:25:23 -0400792
Chuck Lever51971132006-12-05 16:35:19 -0500793 if (transport->tcp_copied == req->rq_private_buf.buflen)
Chuck Levere136d092006-12-05 16:35:23 -0500794 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Lever51971132006-12-05 16:35:19 -0500795 else if (transport->tcp_offset == transport->tcp_reclen) {
Chuck Levere136d092006-12-05 16:35:23 -0500796 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
797 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
Chuck Levera246b012005-08-11 16:25:23 -0400798 }
799
800out:
Chuck Levere136d092006-12-05 16:35:23 -0500801 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
Chuck Lever51971132006-12-05 16:35:19 -0500802 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400803 spin_unlock(&xprt->transport_lock);
Chuck Lever51971132006-12-05 16:35:19 -0500804 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400805}
806
Chuck Lever51971132006-12-05 16:35:19 -0500807static inline void xs_tcp_read_discard(struct sock_xprt *transport, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400808{
809 size_t len;
810
Chuck Lever51971132006-12-05 16:35:19 -0500811 len = transport->tcp_reclen - transport->tcp_offset;
Chuck Levera246b012005-08-11 16:25:23 -0400812 if (len > desc->count)
813 len = desc->count;
814 desc->count -= len;
815 desc->offset += len;
Chuck Lever51971132006-12-05 16:35:19 -0500816 transport->tcp_offset += len;
Chuck Levera246b012005-08-11 16:25:23 -0400817 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever51971132006-12-05 16:35:19 -0500818 xs_tcp_check_fraghdr(transport);
Chuck Levera246b012005-08-11 16:25:23 -0400819}
820
Chuck Lever9903cd12005-08-11 16:25:26 -0400821static 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 -0400822{
823 struct rpc_xprt *xprt = rd_desc->arg.data;
Chuck Lever51971132006-12-05 16:35:19 -0500824 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400825 skb_reader_t desc = {
826 .skb = skb,
827 .offset = offset,
828 .count = len,
Chuck Lever9903cd12005-08-11 16:25:26 -0400829 };
Chuck Levera246b012005-08-11 16:25:23 -0400830
Chuck Lever9903cd12005-08-11 16:25:26 -0400831 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -0400832 do {
833 /* Read in a new fragment marker if necessary */
834 /* Can we ever really expect to get completely empty fragments? */
Chuck Levere136d092006-12-05 16:35:23 -0500835 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400836 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400837 continue;
838 }
839 /* Read in the xid if necessary */
Chuck Levere136d092006-12-05 16:35:23 -0500840 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
Chuck Lever51971132006-12-05 16:35:19 -0500841 xs_tcp_read_xid(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400842 continue;
843 }
844 /* Read in the request data */
Chuck Levere136d092006-12-05 16:35:23 -0500845 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400846 xs_tcp_read_request(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400847 continue;
848 }
849 /* Skip over any trailing bytes on short reads */
Chuck Lever51971132006-12-05 16:35:19 -0500850 xs_tcp_read_discard(transport, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400851 } while (desc.count);
Chuck Lever9903cd12005-08-11 16:25:26 -0400852 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -0400853 return len - desc.count;
854}
855
Chuck Lever9903cd12005-08-11 16:25:26 -0400856/**
857 * xs_tcp_data_ready - "data ready" callback for TCP sockets
858 * @sk: socket with data to read
859 * @bytes: how much data to read
860 *
861 */
862static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -0400863{
864 struct rpc_xprt *xprt;
865 read_descriptor_t rd_desc;
866
867 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400868 dprintk("RPC: xs_tcp_data_ready...\n");
869 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400870 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400871 if (xprt->shutdown)
872 goto out;
873
Chuck Lever9903cd12005-08-11 16:25:26 -0400874 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -0400875 rd_desc.arg.data = xprt;
876 rd_desc.count = 65536;
Chuck Lever9903cd12005-08-11 16:25:26 -0400877 tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
Chuck Levera246b012005-08-11 16:25:23 -0400878out:
879 read_unlock(&sk->sk_callback_lock);
880}
881
Chuck Lever9903cd12005-08-11 16:25:26 -0400882/**
883 * xs_tcp_state_change - callback to handle TCP socket state changes
884 * @sk: socket whose state has changed
885 *
886 */
887static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400888{
Chuck Lever9903cd12005-08-11 16:25:26 -0400889 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400890
891 read_lock(&sk->sk_callback_lock);
892 if (!(xprt = xprt_from_sock(sk)))
893 goto out;
Chuck Lever9903cd12005-08-11 16:25:26 -0400894 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400895 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
896 sk->sk_state, xprt_connected(xprt),
897 sock_flag(sk, SOCK_DEAD),
898 sock_flag(sk, SOCK_ZAPPED));
899
900 switch (sk->sk_state) {
901 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400902 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400903 if (!xprt_test_and_set_connected(xprt)) {
Chuck Lever51971132006-12-05 16:35:19 -0500904 struct sock_xprt *transport = container_of(xprt,
905 struct sock_xprt, xprt);
906
Chuck Levera246b012005-08-11 16:25:23 -0400907 /* Reset TCP record info */
Chuck Lever51971132006-12-05 16:35:19 -0500908 transport->tcp_offset = 0;
909 transport->tcp_reclen = 0;
910 transport->tcp_copied = 0;
Chuck Levere136d092006-12-05 16:35:23 -0500911 transport->tcp_flags =
912 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
Chuck Lever51971132006-12-05 16:35:19 -0500913
Chuck Lever03bf4b72005-08-25 16:25:55 -0700914 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever44fbac22005-08-11 16:25:44 -0400915 xprt_wake_pending_tasks(xprt, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400916 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400917 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400918 break;
919 case TCP_SYN_SENT:
920 case TCP_SYN_RECV:
921 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100922 case TCP_CLOSE_WAIT:
923 /* Try to schedule an autoclose RPC calls */
924 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
925 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
926 schedule_work(&xprt->task_cleanup);
Chuck Levera246b012005-08-11 16:25:23 -0400927 default:
928 xprt_disconnect(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400929 }
930 out:
931 read_unlock(&sk->sk_callback_lock);
932}
933
Chuck Lever9903cd12005-08-11 16:25:26 -0400934/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400935 * xs_udp_write_space - callback invoked when socket buffer space
936 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -0400937 * @sk: socket whose state has changed
938 *
Chuck Levera246b012005-08-11 16:25:23 -0400939 * Called when more output buffer space is available for this socket.
940 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400941 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -0400942 * with a bunch of small requests.
943 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400944static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400945{
Chuck Levera246b012005-08-11 16:25:23 -0400946 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400947
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400948 /* from net/core/sock.c:sock_def_write_space */
949 if (sock_writeable(sk)) {
950 struct socket *sock;
951 struct rpc_xprt *xprt;
952
953 if (unlikely(!(sock = sk->sk_socket)))
Chuck Levera246b012005-08-11 16:25:23 -0400954 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400955 if (unlikely(!(xprt = xprt_from_sock(sk))))
Chuck Levera246b012005-08-11 16:25:23 -0400956 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400957 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
958 goto out;
959
960 xprt_write_space(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400961 }
962
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400963 out:
964 read_unlock(&sk->sk_callback_lock);
965}
Chuck Levera246b012005-08-11 16:25:23 -0400966
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400967/**
968 * xs_tcp_write_space - callback invoked when socket buffer space
969 * becomes available
970 * @sk: socket whose state has changed
971 *
972 * Called when more output buffer space is available for this socket.
973 * We try not to wake our writers until they can make "significant"
974 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
975 * with a bunch of small requests.
976 */
977static void xs_tcp_write_space(struct sock *sk)
978{
979 read_lock(&sk->sk_callback_lock);
980
981 /* from net/core/stream.c:sk_stream_write_space */
982 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
983 struct socket *sock;
984 struct rpc_xprt *xprt;
985
986 if (unlikely(!(sock = sk->sk_socket)))
987 goto out;
988 if (unlikely(!(xprt = xprt_from_sock(sk))))
989 goto out;
990 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
991 goto out;
992
993 xprt_write_space(xprt);
994 }
995
996 out:
Chuck Levera246b012005-08-11 16:25:23 -0400997 read_unlock(&sk->sk_callback_lock);
998}
999
Chuck Lever470056c2005-08-25 16:25:56 -07001000static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -04001001{
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001002 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1003 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -04001004
Chuck Lever7c6e0662006-12-05 16:35:30 -05001005 if (transport->rcvsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001006 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001007 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001008 }
Chuck Lever7c6e0662006-12-05 16:35:30 -05001009 if (transport->sndsize) {
Chuck Levera246b012005-08-11 16:25:23 -04001010 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
Chuck Lever7c6e0662006-12-05 16:35:30 -05001011 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
Chuck Levera246b012005-08-11 16:25:23 -04001012 sk->sk_write_space(sk);
1013 }
1014}
1015
Chuck Lever43118c22005-08-25 16:25:49 -07001016/**
Chuck Lever470056c2005-08-25 16:25:56 -07001017 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -07001018 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -07001019 * @sndsize: requested size of send buffer, in bytes
1020 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -07001021 *
Chuck Lever470056c2005-08-25 16:25:56 -07001022 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -07001023 */
Chuck Lever470056c2005-08-25 16:25:56 -07001024static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -07001025{
Chuck Lever7c6e0662006-12-05 16:35:30 -05001026 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1027
1028 transport->sndsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001029 if (sndsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001030 transport->sndsize = sndsize + 1024;
1031 transport->rcvsize = 0;
Chuck Lever470056c2005-08-25 16:25:56 -07001032 if (rcvsize)
Chuck Lever7c6e0662006-12-05 16:35:30 -05001033 transport->rcvsize = rcvsize + 1024;
Chuck Lever470056c2005-08-25 16:25:56 -07001034
1035 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -07001036}
1037
Chuck Lever46c0ee82005-08-25 16:25:52 -07001038/**
1039 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1040 * @task: task that timed out
1041 *
1042 * Adjust the congestion window after a retransmit timeout has occurred.
1043 */
1044static void xs_udp_timer(struct rpc_task *task)
1045{
1046 xprt_adjust_cwnd(task, -ETIMEDOUT);
1047}
1048
Chuck Leverb85d8802006-05-25 01:40:49 -04001049static unsigned short xs_get_random_port(void)
1050{
1051 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1052 unsigned short rand = (unsigned short) net_random() % range;
1053 return rand + xprt_min_resvport;
1054}
1055
Chuck Lever92200412006-01-03 09:55:51 +01001056/**
Chuck Leveredb267a2006-08-22 20:06:18 -04001057 * xs_print_peer_address - format an IPv4 address for printing
1058 * @xprt: generic transport
1059 * @format: flags field indicating which parts of the address to render
1060 */
1061static char *xs_print_peer_address(struct rpc_xprt *xprt, enum rpc_display_format_t format)
1062{
1063 if (xprt->address_strings[format] != NULL)
1064 return xprt->address_strings[format];
1065 else
1066 return "unprintable";
1067}
1068
1069/**
Chuck Lever92200412006-01-03 09:55:51 +01001070 * xs_set_port - reset the port number in the remote endpoint address
1071 * @xprt: generic transport
1072 * @port: new port number
1073 *
1074 */
1075static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1076{
Chuck Leverc4efcb12006-08-22 20:06:19 -04001077 struct sockaddr_in *sap = (struct sockaddr_in *) &xprt->addr;
1078
Chuck Lever92200412006-01-03 09:55:51 +01001079 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001080
1081 sap->sin_port = htons(port);
Chuck Lever92200412006-01-03 09:55:51 +01001082}
1083
Chuck Leverc8475462006-12-05 16:35:26 -05001084static int xs_bindresvport(struct sock_xprt *transport, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001085{
1086 struct sockaddr_in myaddr = {
1087 .sin_family = AF_INET,
1088 };
Chuck Lever529b33c2005-08-25 16:25:54 -07001089 int err;
Chuck Leverc8475462006-12-05 16:35:26 -05001090 unsigned short port = transport->port;
Chuck Levera246b012005-08-11 16:25:23 -04001091
Chuck Levera246b012005-08-11 16:25:23 -04001092 do {
1093 myaddr.sin_port = htons(port);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001094 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
Chuck Levera246b012005-08-11 16:25:23 -04001095 sizeof(myaddr));
1096 if (err == 0) {
Chuck Leverc8475462006-12-05 16:35:26 -05001097 transport->port = port;
Chuck Lever9903cd12005-08-11 16:25:26 -04001098 dprintk("RPC: xs_bindresvport bound to port %u\n",
1099 port);
Chuck Levera246b012005-08-11 16:25:23 -04001100 return 0;
1101 }
Chuck Lever529b33c2005-08-25 16:25:54 -07001102 if (port <= xprt_min_resvport)
1103 port = xprt_max_resvport;
1104 else
1105 port--;
Chuck Leverc8475462006-12-05 16:35:26 -05001106 } while (err == -EADDRINUSE && port != transport->port);
Chuck Levera246b012005-08-11 16:25:23 -04001107
Chuck Lever9903cd12005-08-11 16:25:26 -04001108 dprintk("RPC: can't bind to reserved port (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001109 return err;
1110}
1111
Chuck Lever9903cd12005-08-11 16:25:26 -04001112/**
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001113 * xs_udp_connect_worker - set up a UDP socket
Chuck Lever9903cd12005-08-11 16:25:26 -04001114 * @args: RPC transport to connect
1115 *
1116 * Invoked by a work queue tasklet.
Chuck Levera246b012005-08-11 16:25:23 -04001117 */
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001118static void xs_udp_connect_worker(void *args)
Chuck Levera246b012005-08-11 16:25:23 -04001119{
Chuck Leverc8475462006-12-05 16:35:26 -05001120 struct sock_xprt *transport = (struct sock_xprt *)args;
1121 struct rpc_xprt *xprt = &transport->xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001122 struct socket *sock = transport->sock;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001123 int err, status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04001124
Chuck Leverec739ef2006-08-22 20:06:15 -04001125 if (xprt->shutdown || !xprt_bound(xprt))
Chuck Levera246b012005-08-11 16:25:23 -04001126 goto out;
1127
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001128 /* Start by resetting any existing state */
Chuck Lever9903cd12005-08-11 16:25:26 -04001129 xs_close(xprt);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001130
1131 if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
1132 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001133 goto out;
1134 }
Chuck Levera246b012005-08-11 16:25:23 -04001135
Chuck Leverc8475462006-12-05 16:35:26 -05001136 if (xprt->resvport && xs_bindresvport(transport, sock) < 0) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001137 sock_release(sock);
1138 goto out;
1139 }
1140
Chuck Leveredb267a2006-08-22 20:06:18 -04001141 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1142 xprt, xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1143
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001144 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001145 struct sock *sk = sock->sk;
1146
1147 write_lock_bh(&sk->sk_callback_lock);
1148
1149 sk->sk_user_data = xprt;
1150 xprt->old_data_ready = sk->sk_data_ready;
1151 xprt->old_state_change = sk->sk_state_change;
1152 xprt->old_write_space = sk->sk_write_space;
1153 sk->sk_data_ready = xs_udp_data_ready;
1154 sk->sk_write_space = xs_udp_write_space;
1155 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001156 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001157
1158 xprt_set_connected(xprt);
1159
1160 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001161 transport->sock = sock;
1162 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001163
1164 write_unlock_bh(&sk->sk_callback_lock);
1165 }
Chuck Lever470056c2005-08-25 16:25:56 -07001166 xs_udp_do_set_buffer_size(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001167 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001168out:
1169 xprt_wake_pending_tasks(xprt, status);
1170 xprt_clear_connecting(xprt);
1171}
1172
Chuck Lever3167e122005-08-25 16:25:55 -07001173/*
1174 * We need to preserve the port number so the reply cache on the server can
1175 * find our cached RPC replies when we get around to reconnecting.
1176 */
1177static void xs_tcp_reuse_connection(struct rpc_xprt *xprt)
1178{
1179 int result;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001180 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever3167e122005-08-25 16:25:55 -07001181 struct sockaddr any;
1182
1183 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
1184
1185 /*
1186 * Disconnect the transport socket by doing a connect operation
1187 * with AF_UNSPEC. This should return immediately...
1188 */
1189 memset(&any, 0, sizeof(any));
1190 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001191 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Chuck Lever3167e122005-08-25 16:25:55 -07001192 if (result)
1193 dprintk("RPC: AF_UNSPEC connect return code %d\n",
1194 result);
1195}
1196
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001197/**
1198 * xs_tcp_connect_worker - connect a TCP socket to a remote endpoint
1199 * @args: RPC transport to connect
1200 *
1201 * Invoked by a work queue tasklet.
1202 */
1203static void xs_tcp_connect_worker(void *args)
1204{
Chuck Leverc8475462006-12-05 16:35:26 -05001205 struct sock_xprt *transport = (struct sock_xprt *)args;
1206 struct rpc_xprt *xprt = &transport->xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001207 struct socket *sock = transport->sock;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001208 int err, status = -EIO;
1209
Chuck Leverec739ef2006-08-22 20:06:15 -04001210 if (xprt->shutdown || !xprt_bound(xprt))
Chuck Levera246b012005-08-11 16:25:23 -04001211 goto out;
1212
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001213 if (!sock) {
Chuck Lever3167e122005-08-25 16:25:55 -07001214 /* start from scratch */
1215 if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
1216 dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
1217 goto out;
1218 }
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001219
Chuck Leverc8475462006-12-05 16:35:26 -05001220 if (xprt->resvport && xs_bindresvport(transport, sock) < 0) {
Chuck Lever3167e122005-08-25 16:25:55 -07001221 sock_release(sock);
1222 goto out;
1223 }
1224 } else
1225 /* "close" the socket, preserving the local port */
1226 xs_tcp_reuse_connection(xprt);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001227
Chuck Leveredb267a2006-08-22 20:06:18 -04001228 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1229 xprt, xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1230
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001231 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001232 struct sock *sk = sock->sk;
1233
1234 write_lock_bh(&sk->sk_callback_lock);
1235
1236 sk->sk_user_data = xprt;
1237 xprt->old_data_ready = sk->sk_data_ready;
1238 xprt->old_state_change = sk->sk_state_change;
1239 xprt->old_write_space = sk->sk_write_space;
1240 sk->sk_data_ready = xs_tcp_data_ready;
1241 sk->sk_state_change = xs_tcp_state_change;
1242 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001243 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07001244
1245 /* socket options */
1246 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1247 sock_reset_flag(sk, SOCK_LINGER);
1248 tcp_sk(sk)->linger2 = 0;
1249 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001250
1251 xprt_clear_connected(xprt);
1252
1253 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001254 transport->sock = sock;
1255 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001256
1257 write_unlock_bh(&sk->sk_callback_lock);
1258 }
1259
1260 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05001261 xprt->stat.connect_count++;
1262 xprt->stat.connect_start = jiffies;
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001263 status = kernel_connect(sock, (struct sockaddr *) &xprt->addr,
Chuck Leverc4efcb12006-08-22 20:06:19 -04001264 xprt->addrlen, O_NONBLOCK);
Chuck Levera246b012005-08-11 16:25:23 -04001265 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1266 xprt, -status, xprt_connected(xprt), sock->sk->sk_state);
1267 if (status < 0) {
1268 switch (status) {
1269 case -EINPROGRESS:
1270 case -EALREADY:
1271 goto out_clear;
Chuck Lever3167e122005-08-25 16:25:55 -07001272 case -ECONNREFUSED:
1273 case -ECONNRESET:
1274 /* retry with existing socket, after a delay */
1275 break;
1276 default:
1277 /* get rid of existing socket, and retry */
1278 xs_close(xprt);
1279 break;
Chuck Levera246b012005-08-11 16:25:23 -04001280 }
1281 }
1282out:
Chuck Lever44fbac22005-08-11 16:25:44 -04001283 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001284out_clear:
Chuck Lever2226feb2005-08-11 16:25:38 -04001285 xprt_clear_connecting(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001286}
1287
Chuck Lever9903cd12005-08-11 16:25:26 -04001288/**
1289 * xs_connect - connect a socket to a remote endpoint
1290 * @task: address of RPC task that manages state of connect request
1291 *
1292 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07001293 *
1294 * UDP socket connects are synchronous, but we use a work queue anyway
1295 * to guarantee that even unprivileged user processes can set up a
1296 * socket on a privileged port.
1297 *
1298 * If a UDP socket connect fails, the delay behavior here prevents
1299 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04001300 */
1301static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04001302{
1303 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001304 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001305
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001306 if (xprt_test_and_set_connecting(xprt))
1307 return;
1308
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001309 if (transport->sock != NULL) {
Chuck Lever03bf4b72005-08-25 16:25:55 -07001310 dprintk("RPC: xs_connect delayed xprt %p for %lu seconds\n",
1311 xprt, xprt->reestablish_timeout / HZ);
Chuck Leverc8475462006-12-05 16:35:26 -05001312 schedule_delayed_work(&transport->connect_worker,
Chuck Lever03bf4b72005-08-25 16:25:55 -07001313 xprt->reestablish_timeout);
1314 xprt->reestablish_timeout <<= 1;
1315 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
1316 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001317 } else {
1318 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
Chuck Leverc8475462006-12-05 16:35:26 -05001319 schedule_work(&transport->connect_worker);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001320
1321 /* flush_scheduled_work can sleep... */
1322 if (!RPC_IS_ASYNC(task))
1323 flush_scheduled_work();
Chuck Levera246b012005-08-11 16:25:23 -04001324 }
1325}
1326
Chuck Lever262ca072006-03-20 13:44:16 -05001327/**
1328 * xs_udp_print_stats - display UDP socket-specifc stats
1329 * @xprt: rpc_xprt struct containing statistics
1330 * @seq: output file
1331 *
1332 */
1333static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1334{
Chuck Leverc8475462006-12-05 16:35:26 -05001335 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1336
Chuck Lever262ca072006-03-20 13:44:16 -05001337 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
Chuck Leverc8475462006-12-05 16:35:26 -05001338 transport->port,
Chuck Lever262ca072006-03-20 13:44:16 -05001339 xprt->stat.bind_count,
1340 xprt->stat.sends,
1341 xprt->stat.recvs,
1342 xprt->stat.bad_xids,
1343 xprt->stat.req_u,
1344 xprt->stat.bklog_u);
1345}
1346
1347/**
1348 * xs_tcp_print_stats - display TCP socket-specifc stats
1349 * @xprt: rpc_xprt struct containing statistics
1350 * @seq: output file
1351 *
1352 */
1353static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1354{
Chuck Leverc8475462006-12-05 16:35:26 -05001355 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262ca072006-03-20 13:44:16 -05001356 long idle_time = 0;
1357
1358 if (xprt_connected(xprt))
1359 idle_time = (long)(jiffies - xprt->last_used) / HZ;
1360
1361 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
Chuck Leverc8475462006-12-05 16:35:26 -05001362 transport->port,
Chuck Lever262ca072006-03-20 13:44:16 -05001363 xprt->stat.bind_count,
1364 xprt->stat.connect_count,
1365 xprt->stat.connect_time,
1366 idle_time,
1367 xprt->stat.sends,
1368 xprt->stat.recvs,
1369 xprt->stat.bad_xids,
1370 xprt->stat.req_u,
1371 xprt->stat.bklog_u);
1372}
1373
Chuck Lever262965f2005-08-11 16:25:56 -04001374static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07001375 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Leveredb267a2006-08-22 20:06:18 -04001376 .print_addr = xs_print_peer_address,
Chuck Lever12a80462005-08-25 16:25:51 -07001377 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07001378 .release_xprt = xprt_release_xprt_cong,
Chuck Leverbbf7c1d2006-08-22 20:06:16 -04001379 .rpcbind = rpc_getport,
Chuck Lever92200412006-01-03 09:55:51 +01001380 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04001381 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01001382 .buf_alloc = rpc_malloc,
1383 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04001384 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001385 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07001386 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07001387 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04001388 .close = xs_close,
1389 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05001390 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04001391};
1392
1393static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Leveredb267a2006-08-22 20:06:18 -04001394 .print_addr = xs_print_peer_address,
Chuck Lever12a80462005-08-25 16:25:51 -07001395 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001396 .release_xprt = xs_tcp_release_xprt,
Chuck Leverbbf7c1d2006-08-22 20:06:16 -04001397 .rpcbind = rpc_getport,
Chuck Lever92200412006-01-03 09:55:51 +01001398 .set_port = xs_set_port,
Chuck Lever262965f2005-08-11 16:25:56 -04001399 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01001400 .buf_alloc = rpc_malloc,
1401 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04001402 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001403 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Chuck Lever9903cd12005-08-11 16:25:26 -04001404 .close = xs_close,
1405 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05001406 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04001407};
1408
Chuck Leverc8541ec2006-10-17 14:44:27 -04001409static struct rpc_xprt *xs_setup_xprt(struct sockaddr *addr, size_t addrlen, unsigned int slot_table_size)
1410{
1411 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05001412 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04001413
1414 if (addrlen > sizeof(xprt->addr)) {
1415 dprintk("RPC: xs_setup_xprt: address too large\n");
1416 return ERR_PTR(-EBADF);
1417 }
1418
Chuck Leverffc2e512006-12-05 16:35:11 -05001419 new = kzalloc(sizeof(*new), GFP_KERNEL);
1420 if (new == NULL) {
Chuck Leverc8541ec2006-10-17 14:44:27 -04001421 dprintk("RPC: xs_setup_xprt: couldn't allocate rpc_xprt\n");
1422 return ERR_PTR(-ENOMEM);
1423 }
Chuck Leverffc2e512006-12-05 16:35:11 -05001424 xprt = &new->xprt;
Chuck Leverc8541ec2006-10-17 14:44:27 -04001425
1426 xprt->max_reqs = slot_table_size;
1427 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
1428 if (xprt->slot == NULL) {
1429 kfree(xprt);
1430 dprintk("RPC: xs_setup_xprt: couldn't allocate slot table\n");
1431 return ERR_PTR(-ENOMEM);
1432 }
1433
1434 memcpy(&xprt->addr, addr, addrlen);
1435 xprt->addrlen = addrlen;
Chuck Leverc8475462006-12-05 16:35:26 -05001436 new->port = xs_get_random_port();
Chuck Leverc8541ec2006-10-17 14:44:27 -04001437
1438 return xprt;
1439}
1440
Chuck Lever9903cd12005-08-11 16:25:26 -04001441/**
1442 * xs_setup_udp - Set up transport to use a UDP socket
Chuck Leverc8541ec2006-10-17 14:44:27 -04001443 * @addr: address of remote server
1444 * @addrlen: length of address in bytes
Chuck Lever9903cd12005-08-11 16:25:26 -04001445 * @to: timeout parameters
1446 *
1447 */
Chuck Leverc8541ec2006-10-17 14:44:27 -04001448struct rpc_xprt *xs_setup_udp(struct sockaddr *addr, size_t addrlen, struct rpc_timeout *to)
Chuck Levera246b012005-08-11 16:25:23 -04001449{
Chuck Leverc8541ec2006-10-17 14:44:27 -04001450 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001451 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001452
Chuck Leverc8541ec2006-10-17 14:44:27 -04001453 xprt = xs_setup_xprt(addr, addrlen, xprt_udp_slot_table_entries);
1454 if (IS_ERR(xprt))
1455 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001456 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001457
Chuck Leverc8541ec2006-10-17 14:44:27 -04001458 if (ntohs(((struct sockaddr_in *)addr)->sin_port) != 0)
Chuck Leverec739ef2006-08-22 20:06:15 -04001459 xprt_set_bound(xprt);
Chuck Leverec739ef2006-08-22 20:06:15 -04001460
1461 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07001462 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001463 /* XXX: header size can vary due to auth type, IPv6, etc. */
1464 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
1465
Chuck Leverc8475462006-12-05 16:35:26 -05001466 INIT_WORK(&transport->connect_worker, xs_udp_connect_worker, transport);
Chuck Lever03bf4b72005-08-25 16:25:55 -07001467 xprt->bind_timeout = XS_BIND_TO;
1468 xprt->connect_timeout = XS_UDP_CONN_TO;
1469 xprt->reestablish_timeout = XS_UDP_REEST_TO;
1470 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04001471
Chuck Lever262965f2005-08-11 16:25:56 -04001472 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001473
1474 if (to)
1475 xprt->timeout = *to;
1476 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001477 xprt_set_timeout(&xprt->timeout, 5, 5 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001478
Chuck Leveredb267a2006-08-22 20:06:18 -04001479 xs_format_peer_addresses(xprt);
1480 dprintk("RPC: set up transport to address %s\n",
1481 xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1482
Chuck Leverc8541ec2006-10-17 14:44:27 -04001483 return xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001484}
1485
Chuck Lever9903cd12005-08-11 16:25:26 -04001486/**
1487 * xs_setup_tcp - Set up transport to use a TCP socket
Chuck Leverc8541ec2006-10-17 14:44:27 -04001488 * @addr: address of remote server
1489 * @addrlen: length of address in bytes
Chuck Lever9903cd12005-08-11 16:25:26 -04001490 * @to: timeout parameters
1491 *
1492 */
Chuck Leverc8541ec2006-10-17 14:44:27 -04001493struct rpc_xprt *xs_setup_tcp(struct sockaddr *addr, size_t addrlen, struct rpc_timeout *to)
Chuck Levera246b012005-08-11 16:25:23 -04001494{
Chuck Leverc8541ec2006-10-17 14:44:27 -04001495 struct rpc_xprt *xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001496 struct sock_xprt *transport;
Chuck Levera246b012005-08-11 16:25:23 -04001497
Chuck Leverc8541ec2006-10-17 14:44:27 -04001498 xprt = xs_setup_xprt(addr, addrlen, xprt_tcp_slot_table_entries);
1499 if (IS_ERR(xprt))
1500 return xprt;
Chuck Leverc8475462006-12-05 16:35:26 -05001501 transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001502
Chuck Leverc8541ec2006-10-17 14:44:27 -04001503 if (ntohs(((struct sockaddr_in *)addr)->sin_port) != 0)
Chuck Leverec739ef2006-08-22 20:06:15 -04001504 xprt_set_bound(xprt);
Chuck Leverec739ef2006-08-22 20:06:15 -04001505
1506 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07001507 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07001508 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04001509
Chuck Leverc8475462006-12-05 16:35:26 -05001510 INIT_WORK(&transport->connect_worker, xs_tcp_connect_worker, transport);
Chuck Lever03bf4b72005-08-25 16:25:55 -07001511 xprt->bind_timeout = XS_BIND_TO;
1512 xprt->connect_timeout = XS_TCP_CONN_TO;
1513 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1514 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04001515
Chuck Lever262965f2005-08-11 16:25:56 -04001516 xprt->ops = &xs_tcp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001517
1518 if (to)
1519 xprt->timeout = *to;
1520 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001521 xprt_set_timeout(&xprt->timeout, 2, 60 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001522
Chuck Leveredb267a2006-08-22 20:06:18 -04001523 xs_format_peer_addresses(xprt);
1524 dprintk("RPC: set up transport to address %s\n",
1525 xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1526
Chuck Leverc8541ec2006-10-17 14:44:27 -04001527 return xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001528}