blob: 0f455fd1820c26724a70ca374ddf6e945e598f75 [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 Leverffc2e512006-12-05 16:35:11 -0500136};
137
Chuck Leveredb267a2006-08-22 20:06:18 -0400138static void xs_format_peer_addresses(struct rpc_xprt *xprt)
139{
140 struct sockaddr_in *addr = (struct sockaddr_in *) &xprt->addr;
141 char *buf;
142
143 buf = kzalloc(20, GFP_KERNEL);
144 if (buf) {
145 snprintf(buf, 20, "%u.%u.%u.%u",
146 NIPQUAD(addr->sin_addr.s_addr));
147 }
148 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
149
150 buf = kzalloc(8, GFP_KERNEL);
151 if (buf) {
152 snprintf(buf, 8, "%u",
153 ntohs(addr->sin_port));
154 }
155 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
156
157 if (xprt->prot == IPPROTO_UDP)
158 xprt->address_strings[RPC_DISPLAY_PROTO] = "udp";
159 else
160 xprt->address_strings[RPC_DISPLAY_PROTO] = "tcp";
161
162 buf = kzalloc(48, GFP_KERNEL);
163 if (buf) {
164 snprintf(buf, 48, "addr=%u.%u.%u.%u port=%u proto=%s",
165 NIPQUAD(addr->sin_addr.s_addr),
166 ntohs(addr->sin_port),
167 xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
168 }
169 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
170}
171
172static void xs_free_peer_addresses(struct rpc_xprt *xprt)
173{
174 kfree(xprt->address_strings[RPC_DISPLAY_ADDR]);
175 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
176 kfree(xprt->address_strings[RPC_DISPLAY_ALL]);
177}
178
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400179#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
180
Trond Myklebust24c56842006-10-17 15:06:22 -0400181static 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 -0400182{
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400183 struct msghdr msg = {
184 .msg_name = addr,
185 .msg_namelen = addrlen,
Trond Myklebust24c56842006-10-17 15:06:22 -0400186 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
187 };
188 struct kvec iov = {
189 .iov_base = vec->iov_base + base,
190 .iov_len = vec->iov_len - base,
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400191 };
192
Trond Myklebust24c56842006-10-17 15:06:22 -0400193 if (iov.iov_len != 0)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400194 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
195 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
196}
197
Trond Myklebust24c56842006-10-17 15:06:22 -0400198static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400199{
Trond Myklebust24c56842006-10-17 15:06:22 -0400200 struct page **ppage;
201 unsigned int remainder;
202 int err, sent = 0;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400203
Trond Myklebust24c56842006-10-17 15:06:22 -0400204 remainder = xdr->page_len - base;
205 base += xdr->page_base;
206 ppage = xdr->pages + (base >> PAGE_SHIFT);
207 base &= ~PAGE_MASK;
208 for(;;) {
209 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
210 int flags = XS_SENDMSG_FLAGS;
211
212 remainder -= len;
213 if (remainder != 0 || more)
214 flags |= MSG_MORE;
215 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
216 if (remainder == 0 || err != len)
217 break;
218 sent += err;
219 ppage++;
220 base = 0;
221 }
222 if (sent == 0)
223 return err;
224 if (err > 0)
225 sent += err;
226 return sent;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400227}
228
Chuck Lever9903cd12005-08-11 16:25:26 -0400229/**
230 * xs_sendpages - write pages directly to a socket
231 * @sock: socket to send on
232 * @addr: UDP only -- address of destination
233 * @addrlen: UDP only -- length of destination address
234 * @xdr: buffer containing this request
235 * @base: starting position in the buffer
236 *
Chuck Levera246b012005-08-11 16:25:23 -0400237 */
Trond Myklebust24c56842006-10-17 15:06:22 -0400238static 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 -0400239{
Trond Myklebust24c56842006-10-17 15:06:22 -0400240 unsigned int remainder = xdr->len - base;
241 int err, sent = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400242
Chuck Lever262965f2005-08-11 16:25:56 -0400243 if (unlikely(!sock))
244 return -ENOTCONN;
245
246 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Trond Myklebust24c56842006-10-17 15:06:22 -0400247 if (base != 0) {
248 addr = NULL;
249 addrlen = 0;
250 }
Chuck Lever262965f2005-08-11 16:25:56 -0400251
Trond Myklebust24c56842006-10-17 15:06:22 -0400252 if (base < xdr->head[0].iov_len || addr != NULL) {
253 unsigned int len = xdr->head[0].iov_len - base;
254 remainder -= len;
255 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
256 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400257 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400258 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400259 base = 0;
260 } else
Trond Myklebust24c56842006-10-17 15:06:22 -0400261 base -= xdr->head[0].iov_len;
Chuck Levera246b012005-08-11 16:25:23 -0400262
Trond Myklebust24c56842006-10-17 15:06:22 -0400263 if (base < xdr->page_len) {
264 unsigned int len = xdr->page_len - base;
265 remainder -= len;
266 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
267 if (remainder == 0 || err != len)
Chuck Levera246b012005-08-11 16:25:23 -0400268 goto out;
Trond Myklebust24c56842006-10-17 15:06:22 -0400269 sent += err;
Chuck Levera246b012005-08-11 16:25:23 -0400270 base = 0;
Trond Myklebust24c56842006-10-17 15:06:22 -0400271 } else
272 base -= xdr->page_len;
273
274 if (base >= xdr->tail[0].iov_len)
275 return sent;
276 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400277out:
Trond Myklebust24c56842006-10-17 15:06:22 -0400278 if (sent == 0)
279 return err;
280 if (err > 0)
281 sent += err;
282 return sent;
Chuck Levera246b012005-08-11 16:25:23 -0400283}
284
Chuck Lever9903cd12005-08-11 16:25:26 -0400285/**
Chuck Lever262965f2005-08-11 16:25:56 -0400286 * xs_nospace - place task on wait queue if transmit was incomplete
287 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400288 *
Chuck Levera246b012005-08-11 16:25:23 -0400289 */
Chuck Lever262965f2005-08-11 16:25:56 -0400290static void xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400291{
Chuck Lever262965f2005-08-11 16:25:56 -0400292 struct rpc_rqst *req = task->tk_rqstp;
293 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500294 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400295
Chuck Lever262965f2005-08-11 16:25:56 -0400296 dprintk("RPC: %4d xmit incomplete (%u left of %u)\n",
297 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
298 req->rq_slen);
299
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500300 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
Chuck Lever262965f2005-08-11 16:25:56 -0400301 /* Protect against races with write_space */
302 spin_lock_bh(&xprt->transport_lock);
303
304 /* Don't race with disconnect */
305 if (!xprt_connected(xprt))
306 task->tk_status = -ENOTCONN;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500307 else if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
Chuck Lever262965f2005-08-11 16:25:56 -0400308 xprt_wait_for_buffer_space(task);
309
310 spin_unlock_bh(&xprt->transport_lock);
311 } else
312 /* Keep holding the socket if it is blocked */
313 rpc_delay(task, HZ>>4);
314}
315
316/**
317 * xs_udp_send_request - write an RPC request to a UDP socket
318 * @task: address of RPC task that manages the state of an RPC request
319 *
320 * Return values:
321 * 0: The request has been sent
322 * EAGAIN: The socket was blocked, please call again later to
323 * complete the request
324 * ENOTCONN: Caller needs to invoke connect logic then call again
325 * other: Some other error occured, the request was not sent
326 */
327static int xs_udp_send_request(struct rpc_task *task)
328{
329 struct rpc_rqst *req = task->tk_rqstp;
330 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500331 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400332 struct xdr_buf *xdr = &req->rq_snd_buf;
333 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400334
Chuck Lever9903cd12005-08-11 16:25:26 -0400335 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400336 req->rq_svec->iov_base,
337 req->rq_svec->iov_len);
338
Chuck Lever262965f2005-08-11 16:25:56 -0400339 req->rq_xtime = jiffies;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500340 status = xs_sendpages(transport->sock,
341 (struct sockaddr *) &xprt->addr,
342 xprt->addrlen, xdr,
343 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400344
Chuck Lever262965f2005-08-11 16:25:56 -0400345 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
346 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400347
Chuck Lever262965f2005-08-11 16:25:56 -0400348 if (likely(status >= (int) req->rq_slen))
349 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400350
Chuck Lever262965f2005-08-11 16:25:56 -0400351 /* Still some bytes left; set up for a retry later. */
352 if (status > 0)
353 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400354
Chuck Lever262965f2005-08-11 16:25:56 -0400355 switch (status) {
356 case -ENETUNREACH:
357 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400358 case -ECONNREFUSED:
359 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400360 * prompts ECONNREFUSED. */
Chuck Levera246b012005-08-11 16:25:23 -0400361 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400362 case -EAGAIN:
363 xs_nospace(task);
Chuck Levera246b012005-08-11 16:25:23 -0400364 break;
365 default:
Chuck Lever262965f2005-08-11 16:25:56 -0400366 dprintk("RPC: sendmsg returned unrecognized error %d\n",
367 -status);
Chuck Lever9903cd12005-08-11 16:25:26 -0400368 break;
Chuck Levera246b012005-08-11 16:25:23 -0400369 }
Chuck Lever262965f2005-08-11 16:25:56 -0400370
371 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400372}
373
Chuck Lever808012f2005-08-25 16:25:49 -0700374static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
375{
376 u32 reclen = buf->len - sizeof(rpc_fraghdr);
377 rpc_fraghdr *base = buf->head[0].iov_base;
378 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
379}
380
Chuck Lever9903cd12005-08-11 16:25:26 -0400381/**
Chuck Lever262965f2005-08-11 16:25:56 -0400382 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400383 * @task: address of RPC task that manages the state of an RPC request
384 *
385 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400386 * 0: The request has been sent
387 * EAGAIN: The socket was blocked, please call again later to
388 * complete the request
389 * ENOTCONN: Caller needs to invoke connect logic then call again
390 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400391 *
392 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400393 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400394 */
Chuck Lever262965f2005-08-11 16:25:56 -0400395static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400396{
397 struct rpc_rqst *req = task->tk_rqstp;
398 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500399 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400400 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Levera246b012005-08-11 16:25:23 -0400401 int status, retry = 0;
402
Chuck Lever808012f2005-08-25 16:25:49 -0700403 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400404
Chuck Lever262965f2005-08-11 16:25:56 -0400405 xs_pktdump("packet data:",
406 req->rq_svec->iov_base,
407 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400408
409 /* Continue transmitting the packet/record. We must be careful
410 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400411 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400412 while (1) {
413 req->rq_xtime = jiffies;
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500414 status = xs_sendpages(transport->sock,
415 NULL, 0, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400416
Chuck Lever262965f2005-08-11 16:25:56 -0400417 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
418 xdr->len - req->rq_bytes_sent, status);
419
420 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400421 break;
422
Chuck Lever262965f2005-08-11 16:25:56 -0400423 /* If we've sent the entire packet, immediately
424 * reset the count of bytes sent. */
425 req->rq_bytes_sent += status;
Chuck Leveref759a22006-03-20 13:44:17 -0500426 task->tk_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400427 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
428 req->rq_bytes_sent = 0;
429 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400430 }
431
Chuck Levera246b012005-08-11 16:25:23 -0400432 status = -EAGAIN;
Chuck Lever262965f2005-08-11 16:25:56 -0400433 if (retry++ > XS_SENDMSG_RETRY)
Chuck Levera246b012005-08-11 16:25:23 -0400434 break;
435 }
436
Chuck Lever262965f2005-08-11 16:25:56 -0400437 switch (status) {
438 case -EAGAIN:
439 xs_nospace(task);
440 break;
441 case -ECONNREFUSED:
442 case -ECONNRESET:
443 case -ENOTCONN:
444 case -EPIPE:
445 status = -ENOTCONN;
446 break;
447 default:
448 dprintk("RPC: sendmsg returned unrecognized error %d\n",
449 -status);
Chuck Lever43118c22005-08-25 16:25:49 -0700450 xprt_disconnect(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400451 break;
Chuck Levera246b012005-08-11 16:25:23 -0400452 }
Chuck Lever262965f2005-08-11 16:25:56 -0400453
Chuck Levera246b012005-08-11 16:25:23 -0400454 return status;
455}
456
Chuck Lever9903cd12005-08-11 16:25:26 -0400457/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400458 * xs_tcp_release_xprt - clean up after a tcp transmission
459 * @xprt: transport
460 * @task: rpc task
461 *
462 * This cleans up if an error causes us to abort the transmission of a request.
463 * In this case, the socket may need to be reset in order to avoid confusing
464 * the server.
465 */
466static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
467{
468 struct rpc_rqst *req;
469
470 if (task != xprt->snd_task)
471 return;
472 if (task == NULL)
473 goto out_release;
474 req = task->tk_rqstp;
475 if (req->rq_bytes_sent == 0)
476 goto out_release;
477 if (req->rq_bytes_sent == req->rq_snd_buf.len)
478 goto out_release;
479 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
480out_release:
481 xprt_release_xprt(xprt, task);
482}
483
484/**
Chuck Lever9903cd12005-08-11 16:25:26 -0400485 * xs_close - close a socket
486 * @xprt: transport
487 *
Chuck Lever3167e122005-08-25 16:25:55 -0700488 * This is used when all requests are complete; ie, no DRC state remains
489 * on the server we want to save.
Chuck Levera246b012005-08-11 16:25:23 -0400490 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400491static void xs_close(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400492{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500493 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
494 struct socket *sock = transport->sock;
495 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400496
497 if (!sk)
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100498 goto clear_close_wait;
Chuck Levera246b012005-08-11 16:25:23 -0400499
Chuck Lever9903cd12005-08-11 16:25:26 -0400500 dprintk("RPC: xs_close xprt %p\n", xprt);
501
Chuck Levera246b012005-08-11 16:25:23 -0400502 write_lock_bh(&sk->sk_callback_lock);
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500503 transport->inet = NULL;
504 transport->sock = NULL;
Chuck Levera246b012005-08-11 16:25:23 -0400505
Chuck Lever9903cd12005-08-11 16:25:26 -0400506 sk->sk_user_data = NULL;
507 sk->sk_data_ready = xprt->old_data_ready;
Chuck Levera246b012005-08-11 16:25:23 -0400508 sk->sk_state_change = xprt->old_state_change;
Chuck Lever9903cd12005-08-11 16:25:26 -0400509 sk->sk_write_space = xprt->old_write_space;
Chuck Levera246b012005-08-11 16:25:23 -0400510 write_unlock_bh(&sk->sk_callback_lock);
511
Chuck Lever9903cd12005-08-11 16:25:26 -0400512 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400513
514 sock_release(sock);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100515clear_close_wait:
516 smp_mb__before_clear_bit();
517 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
518 smp_mb__after_clear_bit();
Chuck Levera246b012005-08-11 16:25:23 -0400519}
520
Chuck Lever9903cd12005-08-11 16:25:26 -0400521/**
522 * xs_destroy - prepare to shutdown a transport
523 * @xprt: doomed transport
524 *
525 */
526static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400527{
Chuck Lever9903cd12005-08-11 16:25:26 -0400528 dprintk("RPC: xs_destroy xprt %p\n", xprt);
529
Chuck Lever55aa4f52005-08-11 16:25:47 -0400530 cancel_delayed_work(&xprt->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400531 flush_scheduled_work();
532
533 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400534 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400535 xs_free_peer_addresses(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400536 kfree(xprt->slot);
Chuck Leverc8541ec2006-10-17 14:44:27 -0400537 kfree(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400538}
539
Chuck Lever9903cd12005-08-11 16:25:26 -0400540static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400541{
Chuck Lever9903cd12005-08-11 16:25:26 -0400542 return (struct rpc_xprt *) sk->sk_user_data;
543}
544
545/**
546 * xs_udp_data_ready - "data ready" callback for UDP sockets
547 * @sk: socket with data to read
548 * @len: how much data to read
549 *
550 */
551static void xs_udp_data_ready(struct sock *sk, int len)
552{
553 struct rpc_task *task;
554 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400555 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400556 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400557 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700558 u32 _xid;
559 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400560
561 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400562 dprintk("RPC: xs_udp_data_ready...\n");
563 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400564 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400565
566 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
567 goto out;
568
569 if (xprt->shutdown)
570 goto dropit;
571
572 repsize = skb->len - sizeof(struct udphdr);
573 if (repsize < 4) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400574 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400575 goto dropit;
576 }
577
578 /* Copy the XID from the skb... */
579 xp = skb_header_pointer(skb, sizeof(struct udphdr),
580 sizeof(_xid), &_xid);
581 if (xp == NULL)
582 goto dropit;
583
584 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400585 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400586 rovr = xprt_lookup_rqst(xprt, *xp);
587 if (!rovr)
588 goto out_unlock;
589 task = rovr->rq_task;
590
Chuck Levera246b012005-08-11 16:25:23 -0400591 if ((copied = rovr->rq_private_buf.buflen) > repsize)
592 copied = repsize;
593
594 /* Suck it into the iovec, verify checksum if not done by hw. */
595 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb))
596 goto out_unlock;
597
598 /* Something worked... */
599 dst_confirm(skb->dst);
600
Chuck Lever1570c1e2005-08-25 16:25:52 -0700601 xprt_adjust_cwnd(task, copied);
602 xprt_update_rtt(task);
603 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400604
605 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400606 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400607 dropit:
608 skb_free_datagram(sk, skb);
609 out:
610 read_unlock(&sk->sk_callback_lock);
611}
612
Chuck Lever9903cd12005-08-11 16:25:26 -0400613static inline size_t xs_tcp_copy_data(skb_reader_t *desc, void *p, size_t len)
Chuck Levera246b012005-08-11 16:25:23 -0400614{
615 if (len > desc->count)
616 len = desc->count;
617 if (skb_copy_bits(desc->skb, desc->offset, p, len)) {
618 dprintk("RPC: failed to copy %zu bytes from skb. %zu bytes remain\n",
619 len, desc->count);
620 return 0;
621 }
622 desc->offset += len;
623 desc->count -= len;
624 dprintk("RPC: copied %zu bytes from skb. %zu bytes remain\n",
625 len, desc->count);
626 return len;
627}
628
Chuck Lever9903cd12005-08-11 16:25:26 -0400629static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400630{
631 size_t len, used;
632 char *p;
633
634 p = ((char *) &xprt->tcp_recm) + xprt->tcp_offset;
635 len = sizeof(xprt->tcp_recm) - xprt->tcp_offset;
Chuck Lever9903cd12005-08-11 16:25:26 -0400636 used = xs_tcp_copy_data(desc, p, len);
Chuck Levera246b012005-08-11 16:25:23 -0400637 xprt->tcp_offset += used;
638 if (used != len)
639 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700640
Chuck Levera246b012005-08-11 16:25:23 -0400641 xprt->tcp_reclen = ntohl(xprt->tcp_recm);
Chuck Lever808012f2005-08-25 16:25:49 -0700642 if (xprt->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levera246b012005-08-11 16:25:23 -0400643 xprt->tcp_flags |= XPRT_LAST_FRAG;
644 else
645 xprt->tcp_flags &= ~XPRT_LAST_FRAG;
Chuck Lever808012f2005-08-25 16:25:49 -0700646 xprt->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
647
Chuck Levera246b012005-08-11 16:25:23 -0400648 xprt->tcp_flags &= ~XPRT_COPY_RECM;
649 xprt->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700650
Chuck Levera246b012005-08-11 16:25:23 -0400651 /* Sanity check of the record length */
Chuck Lever808012f2005-08-25 16:25:49 -0700652 if (unlikely(xprt->tcp_reclen < 4)) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400653 dprintk("RPC: invalid TCP record fragment length\n");
Chuck Levera246b012005-08-11 16:25:23 -0400654 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400655 return;
Chuck Levera246b012005-08-11 16:25:23 -0400656 }
657 dprintk("RPC: reading TCP record fragment of length %d\n",
658 xprt->tcp_reclen);
659}
660
Chuck Lever9903cd12005-08-11 16:25:26 -0400661static void xs_tcp_check_recm(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400662{
663 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u, tcp_flags = %lx\n",
664 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen, xprt->tcp_flags);
665 if (xprt->tcp_offset == xprt->tcp_reclen) {
666 xprt->tcp_flags |= XPRT_COPY_RECM;
667 xprt->tcp_offset = 0;
668 if (xprt->tcp_flags & XPRT_LAST_FRAG) {
669 xprt->tcp_flags &= ~XPRT_COPY_DATA;
670 xprt->tcp_flags |= XPRT_COPY_XID;
671 xprt->tcp_copied = 0;
672 }
673 }
674}
675
Chuck Lever9903cd12005-08-11 16:25:26 -0400676static inline void xs_tcp_read_xid(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400677{
678 size_t len, used;
679 char *p;
680
681 len = sizeof(xprt->tcp_xid) - xprt->tcp_offset;
682 dprintk("RPC: reading XID (%Zu bytes)\n", len);
683 p = ((char *) &xprt->tcp_xid) + xprt->tcp_offset;
Chuck Lever9903cd12005-08-11 16:25:26 -0400684 used = xs_tcp_copy_data(desc, p, len);
Chuck Levera246b012005-08-11 16:25:23 -0400685 xprt->tcp_offset += used;
686 if (used != len)
687 return;
688 xprt->tcp_flags &= ~XPRT_COPY_XID;
689 xprt->tcp_flags |= XPRT_COPY_DATA;
690 xprt->tcp_copied = 4;
691 dprintk("RPC: reading reply for XID %08x\n",
692 ntohl(xprt->tcp_xid));
Chuck Lever9903cd12005-08-11 16:25:26 -0400693 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400694}
695
Chuck Lever9903cd12005-08-11 16:25:26 -0400696static inline void xs_tcp_read_request(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400697{
698 struct rpc_rqst *req;
699 struct xdr_buf *rcvbuf;
700 size_t len;
701 ssize_t r;
702
703 /* Find and lock the request corresponding to this xid */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400704 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400705 req = xprt_lookup_rqst(xprt, xprt->tcp_xid);
706 if (!req) {
707 xprt->tcp_flags &= ~XPRT_COPY_DATA;
708 dprintk("RPC: XID %08x request not found!\n",
709 ntohl(xprt->tcp_xid));
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400710 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400711 return;
712 }
713
714 rcvbuf = &req->rq_private_buf;
715 len = desc->count;
716 if (len > xprt->tcp_reclen - xprt->tcp_offset) {
717 skb_reader_t my_desc;
718
719 len = xprt->tcp_reclen - xprt->tcp_offset;
720 memcpy(&my_desc, desc, sizeof(my_desc));
721 my_desc.count = len;
722 r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied,
Chuck Lever9903cd12005-08-11 16:25:26 -0400723 &my_desc, xs_tcp_copy_data);
Chuck Levera246b012005-08-11 16:25:23 -0400724 desc->count -= r;
725 desc->offset += r;
726 } else
727 r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied,
Chuck Lever9903cd12005-08-11 16:25:26 -0400728 desc, xs_tcp_copy_data);
Chuck Levera246b012005-08-11 16:25:23 -0400729
730 if (r > 0) {
731 xprt->tcp_copied += r;
732 xprt->tcp_offset += r;
733 }
734 if (r != len) {
735 /* Error when copying to the receive buffer,
736 * usually because we weren't able to allocate
737 * additional buffer pages. All we can do now
738 * is turn off XPRT_COPY_DATA, so the request
739 * will not receive any additional updates,
740 * and time out.
741 * Any remaining data from this record will
742 * be discarded.
743 */
744 xprt->tcp_flags &= ~XPRT_COPY_DATA;
745 dprintk("RPC: XID %08x truncated request\n",
746 ntohl(xprt->tcp_xid));
747 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
748 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen);
749 goto out;
750 }
751
752 dprintk("RPC: XID %08x read %Zd bytes\n",
753 ntohl(xprt->tcp_xid), r);
754 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
755 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen);
756
757 if (xprt->tcp_copied == req->rq_private_buf.buflen)
758 xprt->tcp_flags &= ~XPRT_COPY_DATA;
759 else if (xprt->tcp_offset == xprt->tcp_reclen) {
760 if (xprt->tcp_flags & XPRT_LAST_FRAG)
761 xprt->tcp_flags &= ~XPRT_COPY_DATA;
762 }
763
764out:
Chuck Lever1570c1e2005-08-25 16:25:52 -0700765 if (!(xprt->tcp_flags & XPRT_COPY_DATA))
766 xprt_complete_rqst(req->rq_task, xprt->tcp_copied);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400767 spin_unlock(&xprt->transport_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400768 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400769}
770
Chuck Lever9903cd12005-08-11 16:25:26 -0400771static inline void xs_tcp_read_discard(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400772{
773 size_t len;
774
775 len = xprt->tcp_reclen - xprt->tcp_offset;
776 if (len > desc->count)
777 len = desc->count;
778 desc->count -= len;
779 desc->offset += len;
780 xprt->tcp_offset += len;
781 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever9903cd12005-08-11 16:25:26 -0400782 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400783}
784
Chuck Lever9903cd12005-08-11 16:25:26 -0400785static 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 -0400786{
787 struct rpc_xprt *xprt = rd_desc->arg.data;
788 skb_reader_t desc = {
789 .skb = skb,
790 .offset = offset,
791 .count = len,
792 .csum = 0
Chuck Lever9903cd12005-08-11 16:25:26 -0400793 };
Chuck Levera246b012005-08-11 16:25:23 -0400794
Chuck Lever9903cd12005-08-11 16:25:26 -0400795 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -0400796 do {
797 /* Read in a new fragment marker if necessary */
798 /* Can we ever really expect to get completely empty fragments? */
799 if (xprt->tcp_flags & XPRT_COPY_RECM) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400800 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400801 continue;
802 }
803 /* Read in the xid if necessary */
804 if (xprt->tcp_flags & XPRT_COPY_XID) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400805 xs_tcp_read_xid(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400806 continue;
807 }
808 /* Read in the request data */
809 if (xprt->tcp_flags & XPRT_COPY_DATA) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400810 xs_tcp_read_request(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400811 continue;
812 }
813 /* Skip over any trailing bytes on short reads */
Chuck Lever9903cd12005-08-11 16:25:26 -0400814 xs_tcp_read_discard(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400815 } while (desc.count);
Chuck Lever9903cd12005-08-11 16:25:26 -0400816 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -0400817 return len - desc.count;
818}
819
Chuck Lever9903cd12005-08-11 16:25:26 -0400820/**
821 * xs_tcp_data_ready - "data ready" callback for TCP sockets
822 * @sk: socket with data to read
823 * @bytes: how much data to read
824 *
825 */
826static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -0400827{
828 struct rpc_xprt *xprt;
829 read_descriptor_t rd_desc;
830
831 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400832 dprintk("RPC: xs_tcp_data_ready...\n");
833 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400834 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400835 if (xprt->shutdown)
836 goto out;
837
Chuck Lever9903cd12005-08-11 16:25:26 -0400838 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -0400839 rd_desc.arg.data = xprt;
840 rd_desc.count = 65536;
Chuck Lever9903cd12005-08-11 16:25:26 -0400841 tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
Chuck Levera246b012005-08-11 16:25:23 -0400842out:
843 read_unlock(&sk->sk_callback_lock);
844}
845
Chuck Lever9903cd12005-08-11 16:25:26 -0400846/**
847 * xs_tcp_state_change - callback to handle TCP socket state changes
848 * @sk: socket whose state has changed
849 *
850 */
851static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400852{
Chuck Lever9903cd12005-08-11 16:25:26 -0400853 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400854
855 read_lock(&sk->sk_callback_lock);
856 if (!(xprt = xprt_from_sock(sk)))
857 goto out;
Chuck Lever9903cd12005-08-11 16:25:26 -0400858 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400859 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
860 sk->sk_state, xprt_connected(xprt),
861 sock_flag(sk, SOCK_DEAD),
862 sock_flag(sk, SOCK_ZAPPED));
863
864 switch (sk->sk_state) {
865 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400866 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400867 if (!xprt_test_and_set_connected(xprt)) {
868 /* Reset TCP record info */
869 xprt->tcp_offset = 0;
870 xprt->tcp_reclen = 0;
871 xprt->tcp_copied = 0;
872 xprt->tcp_flags = XPRT_COPY_RECM | XPRT_COPY_XID;
Chuck Lever03bf4b72005-08-25 16:25:55 -0700873 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever44fbac22005-08-11 16:25:44 -0400874 xprt_wake_pending_tasks(xprt, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400875 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400876 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400877 break;
878 case TCP_SYN_SENT:
879 case TCP_SYN_RECV:
880 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100881 case TCP_CLOSE_WAIT:
882 /* Try to schedule an autoclose RPC calls */
883 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
884 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
885 schedule_work(&xprt->task_cleanup);
Chuck Levera246b012005-08-11 16:25:23 -0400886 default:
887 xprt_disconnect(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400888 }
889 out:
890 read_unlock(&sk->sk_callback_lock);
891}
892
Chuck Lever9903cd12005-08-11 16:25:26 -0400893/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400894 * xs_udp_write_space - callback invoked when socket buffer space
895 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -0400896 * @sk: socket whose state has changed
897 *
Chuck Levera246b012005-08-11 16:25:23 -0400898 * Called when more output buffer space is available for this socket.
899 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400900 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -0400901 * with a bunch of small requests.
902 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400903static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400904{
Chuck Levera246b012005-08-11 16:25:23 -0400905 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400906
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400907 /* from net/core/sock.c:sock_def_write_space */
908 if (sock_writeable(sk)) {
909 struct socket *sock;
910 struct rpc_xprt *xprt;
911
912 if (unlikely(!(sock = sk->sk_socket)))
Chuck Levera246b012005-08-11 16:25:23 -0400913 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400914 if (unlikely(!(xprt = xprt_from_sock(sk))))
Chuck Levera246b012005-08-11 16:25:23 -0400915 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400916 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
917 goto out;
918
919 xprt_write_space(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400920 }
921
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400922 out:
923 read_unlock(&sk->sk_callback_lock);
924}
Chuck Levera246b012005-08-11 16:25:23 -0400925
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400926/**
927 * xs_tcp_write_space - callback invoked when socket buffer space
928 * becomes available
929 * @sk: socket whose state has changed
930 *
931 * Called when more output buffer space is available for this socket.
932 * We try not to wake our writers until they can make "significant"
933 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
934 * with a bunch of small requests.
935 */
936static void xs_tcp_write_space(struct sock *sk)
937{
938 read_lock(&sk->sk_callback_lock);
939
940 /* from net/core/stream.c:sk_stream_write_space */
941 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
942 struct socket *sock;
943 struct rpc_xprt *xprt;
944
945 if (unlikely(!(sock = sk->sk_socket)))
946 goto out;
947 if (unlikely(!(xprt = xprt_from_sock(sk))))
948 goto out;
949 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
950 goto out;
951
952 xprt_write_space(xprt);
953 }
954
955 out:
Chuck Levera246b012005-08-11 16:25:23 -0400956 read_unlock(&sk->sk_callback_lock);
957}
958
Chuck Lever470056c2005-08-25 16:25:56 -0700959static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400960{
Chuck Leveree0ac0c2006-12-05 16:35:15 -0500961 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
962 struct sock *sk = transport->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400963
Chuck Levera246b012005-08-11 16:25:23 -0400964 if (xprt->rcvsize) {
965 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
966 sk->sk_rcvbuf = xprt->rcvsize * xprt->max_reqs * 2;
967 }
968 if (xprt->sndsize) {
969 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
970 sk->sk_sndbuf = xprt->sndsize * xprt->max_reqs * 2;
971 sk->sk_write_space(sk);
972 }
973}
974
Chuck Lever43118c22005-08-25 16:25:49 -0700975/**
Chuck Lever470056c2005-08-25 16:25:56 -0700976 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -0700977 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -0700978 * @sndsize: requested size of send buffer, in bytes
979 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -0700980 *
Chuck Lever470056c2005-08-25 16:25:56 -0700981 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -0700982 */
Chuck Lever470056c2005-08-25 16:25:56 -0700983static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -0700984{
Chuck Lever470056c2005-08-25 16:25:56 -0700985 xprt->sndsize = 0;
986 if (sndsize)
987 xprt->sndsize = sndsize + 1024;
988 xprt->rcvsize = 0;
989 if (rcvsize)
990 xprt->rcvsize = rcvsize + 1024;
991
992 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -0700993}
994
Chuck Lever46c0ee82005-08-25 16:25:52 -0700995/**
996 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
997 * @task: task that timed out
998 *
999 * Adjust the congestion window after a retransmit timeout has occurred.
1000 */
1001static void xs_udp_timer(struct rpc_task *task)
1002{
1003 xprt_adjust_cwnd(task, -ETIMEDOUT);
1004}
1005
Chuck Leverb85d8802006-05-25 01:40:49 -04001006static unsigned short xs_get_random_port(void)
1007{
1008 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1009 unsigned short rand = (unsigned short) net_random() % range;
1010 return rand + xprt_min_resvport;
1011}
1012
Chuck Lever92200412006-01-03 09:55:51 +01001013/**
Chuck Leveredb267a2006-08-22 20:06:18 -04001014 * xs_print_peer_address - format an IPv4 address for printing
1015 * @xprt: generic transport
1016 * @format: flags field indicating which parts of the address to render
1017 */
1018static char *xs_print_peer_address(struct rpc_xprt *xprt, enum rpc_display_format_t format)
1019{
1020 if (xprt->address_strings[format] != NULL)
1021 return xprt->address_strings[format];
1022 else
1023 return "unprintable";
1024}
1025
1026/**
Chuck Lever92200412006-01-03 09:55:51 +01001027 * xs_set_port - reset the port number in the remote endpoint address
1028 * @xprt: generic transport
1029 * @port: new port number
1030 *
1031 */
1032static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1033{
Chuck Leverc4efcb12006-08-22 20:06:19 -04001034 struct sockaddr_in *sap = (struct sockaddr_in *) &xprt->addr;
1035
Chuck Lever92200412006-01-03 09:55:51 +01001036 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001037
1038 sap->sin_port = htons(port);
Chuck Lever92200412006-01-03 09:55:51 +01001039}
1040
Chuck Lever9903cd12005-08-11 16:25:26 -04001041static int xs_bindresvport(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001042{
1043 struct sockaddr_in myaddr = {
1044 .sin_family = AF_INET,
1045 };
Chuck Lever529b33c2005-08-25 16:25:54 -07001046 int err;
1047 unsigned short port = xprt->port;
Chuck Levera246b012005-08-11 16:25:23 -04001048
Chuck Levera246b012005-08-11 16:25:23 -04001049 do {
1050 myaddr.sin_port = htons(port);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001051 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
Chuck Levera246b012005-08-11 16:25:23 -04001052 sizeof(myaddr));
1053 if (err == 0) {
1054 xprt->port = port;
Chuck Lever9903cd12005-08-11 16:25:26 -04001055 dprintk("RPC: xs_bindresvport bound to port %u\n",
1056 port);
Chuck Levera246b012005-08-11 16:25:23 -04001057 return 0;
1058 }
Chuck Lever529b33c2005-08-25 16:25:54 -07001059 if (port <= xprt_min_resvport)
1060 port = xprt_max_resvport;
1061 else
1062 port--;
Chuck Levera246b012005-08-11 16:25:23 -04001063 } while (err == -EADDRINUSE && port != xprt->port);
1064
Chuck Lever9903cd12005-08-11 16:25:26 -04001065 dprintk("RPC: can't bind to reserved port (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001066 return err;
1067}
1068
Chuck Lever9903cd12005-08-11 16:25:26 -04001069/**
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001070 * xs_udp_connect_worker - set up a UDP socket
Chuck Lever9903cd12005-08-11 16:25:26 -04001071 * @args: RPC transport to connect
1072 *
1073 * Invoked by a work queue tasklet.
Chuck Levera246b012005-08-11 16:25:23 -04001074 */
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001075static void xs_udp_connect_worker(void *args)
Chuck Levera246b012005-08-11 16:25:23 -04001076{
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001077 struct rpc_xprt *xprt = (struct rpc_xprt *) args;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001078 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1079 struct socket *sock = transport->sock;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001080 int err, status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04001081
Chuck Leverec739ef2006-08-22 20:06:15 -04001082 if (xprt->shutdown || !xprt_bound(xprt))
Chuck Levera246b012005-08-11 16:25:23 -04001083 goto out;
1084
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001085 /* Start by resetting any existing state */
Chuck Lever9903cd12005-08-11 16:25:26 -04001086 xs_close(xprt);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001087
1088 if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
1089 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001090 goto out;
1091 }
Chuck Levera246b012005-08-11 16:25:23 -04001092
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001093 if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) {
1094 sock_release(sock);
1095 goto out;
1096 }
1097
Chuck Leveredb267a2006-08-22 20:06:18 -04001098 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1099 xprt, xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1100
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001101 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001102 struct sock *sk = sock->sk;
1103
1104 write_lock_bh(&sk->sk_callback_lock);
1105
1106 sk->sk_user_data = xprt;
1107 xprt->old_data_ready = sk->sk_data_ready;
1108 xprt->old_state_change = sk->sk_state_change;
1109 xprt->old_write_space = sk->sk_write_space;
1110 sk->sk_data_ready = xs_udp_data_ready;
1111 sk->sk_write_space = xs_udp_write_space;
1112 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa72005-12-13 16:13:52 -05001113 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001114
1115 xprt_set_connected(xprt);
1116
1117 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001118 transport->sock = sock;
1119 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001120
1121 write_unlock_bh(&sk->sk_callback_lock);
1122 }
Chuck Lever470056c2005-08-25 16:25:56 -07001123 xs_udp_do_set_buffer_size(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001124 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001125out:
1126 xprt_wake_pending_tasks(xprt, status);
1127 xprt_clear_connecting(xprt);
1128}
1129
Chuck Lever3167e122005-08-25 16:25:55 -07001130/*
1131 * We need to preserve the port number so the reply cache on the server can
1132 * find our cached RPC replies when we get around to reconnecting.
1133 */
1134static void xs_tcp_reuse_connection(struct rpc_xprt *xprt)
1135{
1136 int result;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001137 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Lever3167e122005-08-25 16:25:55 -07001138 struct sockaddr any;
1139
1140 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
1141
1142 /*
1143 * Disconnect the transport socket by doing a connect operation
1144 * with AF_UNSPEC. This should return immediately...
1145 */
1146 memset(&any, 0, sizeof(any));
1147 any.sa_family = AF_UNSPEC;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001148 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
Chuck Lever3167e122005-08-25 16:25:55 -07001149 if (result)
1150 dprintk("RPC: AF_UNSPEC connect return code %d\n",
1151 result);
1152}
1153
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001154/**
1155 * xs_tcp_connect_worker - connect a TCP socket to a remote endpoint
1156 * @args: RPC transport to connect
1157 *
1158 * Invoked by a work queue tasklet.
1159 */
1160static void xs_tcp_connect_worker(void *args)
1161{
1162 struct rpc_xprt *xprt = (struct rpc_xprt *)args;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001163 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1164 struct socket *sock = transport->sock;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001165 int err, status = -EIO;
1166
Chuck Leverec739ef2006-08-22 20:06:15 -04001167 if (xprt->shutdown || !xprt_bound(xprt))
Chuck Levera246b012005-08-11 16:25:23 -04001168 goto out;
1169
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001170 if (!sock) {
Chuck Lever3167e122005-08-25 16:25:55 -07001171 /* start from scratch */
1172 if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
1173 dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
1174 goto out;
1175 }
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001176
Chuck Lever3167e122005-08-25 16:25:55 -07001177 if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) {
1178 sock_release(sock);
1179 goto out;
1180 }
1181 } else
1182 /* "close" the socket, preserving the local port */
1183 xs_tcp_reuse_connection(xprt);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001184
Chuck Leveredb267a2006-08-22 20:06:18 -04001185 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1186 xprt, xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1187
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001188 if (!transport->inet) {
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001189 struct sock *sk = sock->sk;
1190
1191 write_lock_bh(&sk->sk_callback_lock);
1192
1193 sk->sk_user_data = xprt;
1194 xprt->old_data_ready = sk->sk_data_ready;
1195 xprt->old_state_change = sk->sk_state_change;
1196 xprt->old_write_space = sk->sk_write_space;
1197 sk->sk_data_ready = xs_tcp_data_ready;
1198 sk->sk_state_change = xs_tcp_state_change;
1199 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebustb079fa72005-12-13 16:13:52 -05001200 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07001201
1202 /* socket options */
1203 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1204 sock_reset_flag(sk, SOCK_LINGER);
1205 tcp_sk(sk)->linger2 = 0;
1206 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001207
1208 xprt_clear_connected(xprt);
1209
1210 /* Reset to new socket */
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001211 transport->sock = sock;
1212 transport->inet = sk;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001213
1214 write_unlock_bh(&sk->sk_callback_lock);
1215 }
1216
1217 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05001218 xprt->stat.connect_count++;
1219 xprt->stat.connect_start = jiffies;
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001220 status = kernel_connect(sock, (struct sockaddr *) &xprt->addr,
Chuck Leverc4efcb12006-08-22 20:06:19 -04001221 xprt->addrlen, O_NONBLOCK);
Chuck Levera246b012005-08-11 16:25:23 -04001222 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1223 xprt, -status, xprt_connected(xprt), sock->sk->sk_state);
1224 if (status < 0) {
1225 switch (status) {
1226 case -EINPROGRESS:
1227 case -EALREADY:
1228 goto out_clear;
Chuck Lever3167e122005-08-25 16:25:55 -07001229 case -ECONNREFUSED:
1230 case -ECONNRESET:
1231 /* retry with existing socket, after a delay */
1232 break;
1233 default:
1234 /* get rid of existing socket, and retry */
1235 xs_close(xprt);
1236 break;
Chuck Levera246b012005-08-11 16:25:23 -04001237 }
1238 }
1239out:
Chuck Lever44fbac22005-08-11 16:25:44 -04001240 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001241out_clear:
Chuck Lever2226feb2005-08-11 16:25:38 -04001242 xprt_clear_connecting(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001243}
1244
Chuck Lever9903cd12005-08-11 16:25:26 -04001245/**
1246 * xs_connect - connect a socket to a remote endpoint
1247 * @task: address of RPC task that manages state of connect request
1248 *
1249 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07001250 *
1251 * UDP socket connects are synchronous, but we use a work queue anyway
1252 * to guarantee that even unprivileged user processes can set up a
1253 * socket on a privileged port.
1254 *
1255 * If a UDP socket connect fails, the delay behavior here prevents
1256 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04001257 */
1258static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04001259{
1260 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001261 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001262
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001263 if (xprt_test_and_set_connecting(xprt))
1264 return;
1265
Chuck Leveree0ac0c2006-12-05 16:35:15 -05001266 if (transport->sock != NULL) {
Chuck Lever03bf4b72005-08-25 16:25:55 -07001267 dprintk("RPC: xs_connect delayed xprt %p for %lu seconds\n",
1268 xprt, xprt->reestablish_timeout / HZ);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001269 schedule_delayed_work(&xprt->connect_worker,
Chuck Lever03bf4b72005-08-25 16:25:55 -07001270 xprt->reestablish_timeout);
1271 xprt->reestablish_timeout <<= 1;
1272 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
1273 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001274 } else {
1275 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
1276 schedule_work(&xprt->connect_worker);
1277
1278 /* flush_scheduled_work can sleep... */
1279 if (!RPC_IS_ASYNC(task))
1280 flush_scheduled_work();
Chuck Levera246b012005-08-11 16:25:23 -04001281 }
1282}
1283
Chuck Lever262ca072006-03-20 13:44:16 -05001284/**
1285 * xs_udp_print_stats - display UDP socket-specifc stats
1286 * @xprt: rpc_xprt struct containing statistics
1287 * @seq: output file
1288 *
1289 */
1290static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1291{
1292 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
1293 xprt->port,
1294 xprt->stat.bind_count,
1295 xprt->stat.sends,
1296 xprt->stat.recvs,
1297 xprt->stat.bad_xids,
1298 xprt->stat.req_u,
1299 xprt->stat.bklog_u);
1300}
1301
1302/**
1303 * xs_tcp_print_stats - display TCP socket-specifc stats
1304 * @xprt: rpc_xprt struct containing statistics
1305 * @seq: output file
1306 *
1307 */
1308static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1309{
1310 long idle_time = 0;
1311
1312 if (xprt_connected(xprt))
1313 idle_time = (long)(jiffies - xprt->last_used) / HZ;
1314
1315 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
1316 xprt->port,
1317 xprt->stat.bind_count,
1318 xprt->stat.connect_count,
1319 xprt->stat.connect_time,
1320 idle_time,
1321 xprt->stat.sends,
1322 xprt->stat.recvs,
1323 xprt->stat.bad_xids,
1324 xprt->stat.req_u,
1325 xprt->stat.bklog_u);
1326}
1327
Chuck Lever262965f2005-08-11 16:25:56 -04001328static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07001329 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Leveredb267a2006-08-22 20:06:18 -04001330 .print_addr = xs_print_peer_address,
Chuck Lever12a80462005-08-25 16:25:51 -07001331 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07001332 .release_xprt = xprt_release_xprt_cong,
Chuck Leverbbf7c1d2006-08-22 20:06:16 -04001333 .rpcbind = rpc_getport,
Chuck Lever92200412006-01-03 09:55:51 +01001334 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04001335 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01001336 .buf_alloc = rpc_malloc,
1337 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04001338 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001339 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07001340 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07001341 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04001342 .close = xs_close,
1343 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05001344 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04001345};
1346
1347static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Leveredb267a2006-08-22 20:06:18 -04001348 .print_addr = xs_print_peer_address,
Chuck Lever12a80462005-08-25 16:25:51 -07001349 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001350 .release_xprt = xs_tcp_release_xprt,
Chuck Leverbbf7c1d2006-08-22 20:06:16 -04001351 .rpcbind = rpc_getport,
Chuck Lever92200412006-01-03 09:55:51 +01001352 .set_port = xs_set_port,
Chuck Lever262965f2005-08-11 16:25:56 -04001353 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01001354 .buf_alloc = rpc_malloc,
1355 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04001356 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001357 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Chuck Lever9903cd12005-08-11 16:25:26 -04001358 .close = xs_close,
1359 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05001360 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04001361};
1362
Chuck Leverc8541ec2006-10-17 14:44:27 -04001363static struct rpc_xprt *xs_setup_xprt(struct sockaddr *addr, size_t addrlen, unsigned int slot_table_size)
1364{
1365 struct rpc_xprt *xprt;
Chuck Leverffc2e512006-12-05 16:35:11 -05001366 struct sock_xprt *new;
Chuck Leverc8541ec2006-10-17 14:44:27 -04001367
1368 if (addrlen > sizeof(xprt->addr)) {
1369 dprintk("RPC: xs_setup_xprt: address too large\n");
1370 return ERR_PTR(-EBADF);
1371 }
1372
Chuck Leverffc2e512006-12-05 16:35:11 -05001373 new = kzalloc(sizeof(*new), GFP_KERNEL);
1374 if (new == NULL) {
Chuck Leverc8541ec2006-10-17 14:44:27 -04001375 dprintk("RPC: xs_setup_xprt: couldn't allocate rpc_xprt\n");
1376 return ERR_PTR(-ENOMEM);
1377 }
Chuck Leverffc2e512006-12-05 16:35:11 -05001378 xprt = &new->xprt;
Chuck Leverc8541ec2006-10-17 14:44:27 -04001379
1380 xprt->max_reqs = slot_table_size;
1381 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
1382 if (xprt->slot == NULL) {
1383 kfree(xprt);
1384 dprintk("RPC: xs_setup_xprt: couldn't allocate slot table\n");
1385 return ERR_PTR(-ENOMEM);
1386 }
1387
1388 memcpy(&xprt->addr, addr, addrlen);
1389 xprt->addrlen = addrlen;
1390 xprt->port = xs_get_random_port();
1391
1392 return xprt;
1393}
1394
Chuck Lever9903cd12005-08-11 16:25:26 -04001395/**
1396 * xs_setup_udp - Set up transport to use a UDP socket
Chuck Leverc8541ec2006-10-17 14:44:27 -04001397 * @addr: address of remote server
1398 * @addrlen: length of address in bytes
Chuck Lever9903cd12005-08-11 16:25:26 -04001399 * @to: timeout parameters
1400 *
1401 */
Chuck Leverc8541ec2006-10-17 14:44:27 -04001402struct rpc_xprt *xs_setup_udp(struct sockaddr *addr, size_t addrlen, struct rpc_timeout *to)
Chuck Levera246b012005-08-11 16:25:23 -04001403{
Chuck Leverc8541ec2006-10-17 14:44:27 -04001404 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001405
Chuck Leverc8541ec2006-10-17 14:44:27 -04001406 xprt = xs_setup_xprt(addr, addrlen, xprt_udp_slot_table_entries);
1407 if (IS_ERR(xprt))
1408 return xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001409
Chuck Leverc8541ec2006-10-17 14:44:27 -04001410 if (ntohs(((struct sockaddr_in *)addr)->sin_port) != 0)
Chuck Leverec739ef2006-08-22 20:06:15 -04001411 xprt_set_bound(xprt);
Chuck Leverec739ef2006-08-22 20:06:15 -04001412
1413 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07001414 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001415 /* XXX: header size can vary due to auth type, IPv6, etc. */
1416 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
1417
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001418 INIT_WORK(&xprt->connect_worker, xs_udp_connect_worker, xprt);
Chuck Lever03bf4b72005-08-25 16:25:55 -07001419 xprt->bind_timeout = XS_BIND_TO;
1420 xprt->connect_timeout = XS_UDP_CONN_TO;
1421 xprt->reestablish_timeout = XS_UDP_REEST_TO;
1422 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04001423
Chuck Lever262965f2005-08-11 16:25:56 -04001424 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001425
1426 if (to)
1427 xprt->timeout = *to;
1428 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001429 xprt_set_timeout(&xprt->timeout, 5, 5 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001430
Chuck Leveredb267a2006-08-22 20:06:18 -04001431 xs_format_peer_addresses(xprt);
1432 dprintk("RPC: set up transport to address %s\n",
1433 xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1434
Chuck Leverc8541ec2006-10-17 14:44:27 -04001435 return xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001436}
1437
Chuck Lever9903cd12005-08-11 16:25:26 -04001438/**
1439 * xs_setup_tcp - Set up transport to use a TCP socket
Chuck Leverc8541ec2006-10-17 14:44:27 -04001440 * @addr: address of remote server
1441 * @addrlen: length of address in bytes
Chuck Lever9903cd12005-08-11 16:25:26 -04001442 * @to: timeout parameters
1443 *
1444 */
Chuck Leverc8541ec2006-10-17 14:44:27 -04001445struct rpc_xprt *xs_setup_tcp(struct sockaddr *addr, size_t addrlen, struct rpc_timeout *to)
Chuck Levera246b012005-08-11 16:25:23 -04001446{
Chuck Leverc8541ec2006-10-17 14:44:27 -04001447 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001448
Chuck Leverc8541ec2006-10-17 14:44:27 -04001449 xprt = xs_setup_xprt(addr, addrlen, xprt_tcp_slot_table_entries);
1450 if (IS_ERR(xprt))
1451 return xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001452
Chuck Leverc8541ec2006-10-17 14:44:27 -04001453 if (ntohs(((struct sockaddr_in *)addr)->sin_port) != 0)
Chuck Leverec739ef2006-08-22 20:06:15 -04001454 xprt_set_bound(xprt);
Chuck Leverec739ef2006-08-22 20:06:15 -04001455
1456 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07001457 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07001458 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04001459
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001460 INIT_WORK(&xprt->connect_worker, xs_tcp_connect_worker, xprt);
Chuck Lever03bf4b72005-08-25 16:25:55 -07001461 xprt->bind_timeout = XS_BIND_TO;
1462 xprt->connect_timeout = XS_TCP_CONN_TO;
1463 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1464 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04001465
Chuck Lever262965f2005-08-11 16:25:56 -04001466 xprt->ops = &xs_tcp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001467
1468 if (to)
1469 xprt->timeout = *to;
1470 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001471 xprt_set_timeout(&xprt->timeout, 2, 60 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001472
Chuck Leveredb267a2006-08-22 20:06:18 -04001473 xs_format_peer_addresses(xprt);
1474 dprintk("RPC: set up transport to address %s\n",
1475 xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1476
Chuck Leverc8541ec2006-10-17 14:44:27 -04001477 return xprt;
Chuck Levera246b012005-08-11 16:25:23 -04001478}