blob: 757fc91ef25d8621e9af5f1e162bda754b90f0fa [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 Leveredb267a2006-08-22 20:06:18 -0400128static void xs_format_peer_addresses(struct rpc_xprt *xprt)
129{
130 struct sockaddr_in *addr = (struct sockaddr_in *) &xprt->addr;
131 char *buf;
132
133 buf = kzalloc(20, GFP_KERNEL);
134 if (buf) {
135 snprintf(buf, 20, "%u.%u.%u.%u",
136 NIPQUAD(addr->sin_addr.s_addr));
137 }
138 xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
139
140 buf = kzalloc(8, GFP_KERNEL);
141 if (buf) {
142 snprintf(buf, 8, "%u",
143 ntohs(addr->sin_port));
144 }
145 xprt->address_strings[RPC_DISPLAY_PORT] = buf;
146
147 if (xprt->prot == IPPROTO_UDP)
148 xprt->address_strings[RPC_DISPLAY_PROTO] = "udp";
149 else
150 xprt->address_strings[RPC_DISPLAY_PROTO] = "tcp";
151
152 buf = kzalloc(48, GFP_KERNEL);
153 if (buf) {
154 snprintf(buf, 48, "addr=%u.%u.%u.%u port=%u proto=%s",
155 NIPQUAD(addr->sin_addr.s_addr),
156 ntohs(addr->sin_port),
157 xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
158 }
159 xprt->address_strings[RPC_DISPLAY_ALL] = buf;
160}
161
162static void xs_free_peer_addresses(struct rpc_xprt *xprt)
163{
164 kfree(xprt->address_strings[RPC_DISPLAY_ADDR]);
165 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
166 kfree(xprt->address_strings[RPC_DISPLAY_ALL]);
167}
168
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400169#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
170
171static inline int xs_send_head(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, unsigned int len)
172{
173 struct kvec iov = {
174 .iov_base = xdr->head[0].iov_base + base,
175 .iov_len = len - base,
176 };
177 struct msghdr msg = {
178 .msg_name = addr,
179 .msg_namelen = addrlen,
180 .msg_flags = XS_SENDMSG_FLAGS,
181 };
182
183 if (xdr->len > len)
184 msg.msg_flags |= MSG_MORE;
185
186 if (likely(iov.iov_len))
187 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
188 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
189}
190
191static int xs_send_tail(struct socket *sock, struct xdr_buf *xdr, unsigned int base, unsigned int len)
192{
193 struct kvec iov = {
194 .iov_base = xdr->tail[0].iov_base + base,
195 .iov_len = len - base,
196 };
197 struct msghdr msg = {
198 .msg_flags = XS_SENDMSG_FLAGS,
199 };
200
201 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
202}
203
Chuck Lever9903cd12005-08-11 16:25:26 -0400204/**
205 * xs_sendpages - write pages directly to a socket
206 * @sock: socket to send on
207 * @addr: UDP only -- address of destination
208 * @addrlen: UDP only -- length of destination address
209 * @xdr: buffer containing this request
210 * @base: starting position in the buffer
211 *
Chuck Levera246b012005-08-11 16:25:23 -0400212 */
Chuck Lever262965f2005-08-11 16:25:56 -0400213static inline 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 -0400214{
215 struct page **ppage = xdr->pages;
216 unsigned int len, pglen = xdr->page_len;
217 int err, ret = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400218
Chuck Lever262965f2005-08-11 16:25:56 -0400219 if (unlikely(!sock))
220 return -ENOTCONN;
221
222 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
223
Chuck Levera246b012005-08-11 16:25:23 -0400224 len = xdr->head[0].iov_len;
225 if (base < len || (addr != NULL && base == 0)) {
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400226 err = xs_send_head(sock, addr, addrlen, xdr, base, len);
Chuck Levera246b012005-08-11 16:25:23 -0400227 if (ret == 0)
228 ret = err;
229 else if (err > 0)
230 ret += err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400231 if (err != (len - base))
Chuck Levera246b012005-08-11 16:25:23 -0400232 goto out;
233 base = 0;
234 } else
235 base -= len;
236
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400237 if (unlikely(pglen == 0))
Chuck Levera246b012005-08-11 16:25:23 -0400238 goto copy_tail;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400239 if (unlikely(base >= pglen)) {
Chuck Levera246b012005-08-11 16:25:23 -0400240 base -= pglen;
241 goto copy_tail;
242 }
243 if (base || xdr->page_base) {
244 pglen -= base;
Chuck Lever9903cd12005-08-11 16:25:26 -0400245 base += xdr->page_base;
Chuck Levera246b012005-08-11 16:25:23 -0400246 ppage += base >> PAGE_CACHE_SHIFT;
247 base &= ~PAGE_CACHE_MASK;
248 }
249
Chuck Levera246b012005-08-11 16:25:23 -0400250 do {
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400251 int flags = XS_SENDMSG_FLAGS;
Chuck Levera246b012005-08-11 16:25:23 -0400252
253 len = PAGE_CACHE_SIZE;
254 if (base)
255 len -= base;
256 if (pglen < len)
257 len = pglen;
258
259 if (pglen != len || xdr->tail[0].iov_len != 0)
260 flags |= MSG_MORE;
261
Sridhar Samudrala53fad3c2006-08-09 17:03:17 -0700262 err = kernel_sendpage(sock, *ppage, base, len, flags);
Chuck Levera246b012005-08-11 16:25:23 -0400263 if (ret == 0)
264 ret = err;
265 else if (err > 0)
266 ret += err;
267 if (err != len)
268 goto out;
269 base = 0;
270 ppage++;
271 } while ((pglen -= len) != 0);
272copy_tail:
273 len = xdr->tail[0].iov_len;
274 if (base < len) {
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400275 err = xs_send_tail(sock, xdr, base, len);
Chuck Levera246b012005-08-11 16:25:23 -0400276 if (ret == 0)
277 ret = err;
278 else if (err > 0)
279 ret += err;
280 }
281out:
282 return ret;
283}
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 Levera246b012005-08-11 16:25:23 -0400294
Chuck Lever262965f2005-08-11 16:25:56 -0400295 dprintk("RPC: %4d xmit incomplete (%u left of %u)\n",
296 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
297 req->rq_slen);
298
299 if (test_bit(SOCK_ASYNC_NOSPACE, &xprt->sock->flags)) {
300 /* Protect against races with write_space */
301 spin_lock_bh(&xprt->transport_lock);
302
303 /* Don't race with disconnect */
304 if (!xprt_connected(xprt))
305 task->tk_status = -ENOTCONN;
306 else if (test_bit(SOCK_NOSPACE, &xprt->sock->flags))
307 xprt_wait_for_buffer_space(task);
308
309 spin_unlock_bh(&xprt->transport_lock);
310 } else
311 /* Keep holding the socket if it is blocked */
312 rpc_delay(task, HZ>>4);
313}
314
315/**
316 * xs_udp_send_request - write an RPC request to a UDP socket
317 * @task: address of RPC task that manages the state of an RPC request
318 *
319 * Return values:
320 * 0: The request has been sent
321 * EAGAIN: The socket was blocked, please call again later to
322 * complete the request
323 * ENOTCONN: Caller needs to invoke connect logic then call again
324 * other: Some other error occured, the request was not sent
325 */
326static int xs_udp_send_request(struct rpc_task *task)
327{
328 struct rpc_rqst *req = task->tk_rqstp;
329 struct rpc_xprt *xprt = req->rq_xprt;
330 struct xdr_buf *xdr = &req->rq_snd_buf;
331 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400332
Chuck Lever9903cd12005-08-11 16:25:26 -0400333 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400334 req->rq_svec->iov_base,
335 req->rq_svec->iov_len);
336
Chuck Lever262965f2005-08-11 16:25:56 -0400337 req->rq_xtime = jiffies;
338 status = xs_sendpages(xprt->sock, (struct sockaddr *) &xprt->addr,
Chuck Leverc4efcb12006-08-22 20:06:19 -0400339 xprt->addrlen, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400340
Chuck Lever262965f2005-08-11 16:25:56 -0400341 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
342 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400343
Chuck Lever262965f2005-08-11 16:25:56 -0400344 if (likely(status >= (int) req->rq_slen))
345 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400346
Chuck Lever262965f2005-08-11 16:25:56 -0400347 /* Still some bytes left; set up for a retry later. */
348 if (status > 0)
349 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400350
Chuck Lever262965f2005-08-11 16:25:56 -0400351 switch (status) {
352 case -ENETUNREACH:
353 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400354 case -ECONNREFUSED:
355 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400356 * prompts ECONNREFUSED. */
Chuck Levera246b012005-08-11 16:25:23 -0400357 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400358 case -EAGAIN:
359 xs_nospace(task);
Chuck Levera246b012005-08-11 16:25:23 -0400360 break;
361 default:
Chuck Lever262965f2005-08-11 16:25:56 -0400362 dprintk("RPC: sendmsg returned unrecognized error %d\n",
363 -status);
Chuck Lever9903cd12005-08-11 16:25:26 -0400364 break;
Chuck Levera246b012005-08-11 16:25:23 -0400365 }
Chuck Lever262965f2005-08-11 16:25:56 -0400366
367 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400368}
369
Chuck Lever808012f2005-08-25 16:25:49 -0700370static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
371{
372 u32 reclen = buf->len - sizeof(rpc_fraghdr);
373 rpc_fraghdr *base = buf->head[0].iov_base;
374 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
375}
376
Chuck Lever9903cd12005-08-11 16:25:26 -0400377/**
Chuck Lever262965f2005-08-11 16:25:56 -0400378 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400379 * @task: address of RPC task that manages the state of an RPC request
380 *
381 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400382 * 0: The request has been sent
383 * EAGAIN: The socket was blocked, please call again later to
384 * complete the request
385 * ENOTCONN: Caller needs to invoke connect logic then call again
386 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400387 *
388 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400389 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400390 */
Chuck Lever262965f2005-08-11 16:25:56 -0400391static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400392{
393 struct rpc_rqst *req = task->tk_rqstp;
394 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Lever262965f2005-08-11 16:25:56 -0400395 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Levera246b012005-08-11 16:25:23 -0400396 int status, retry = 0;
397
Chuck Lever808012f2005-08-25 16:25:49 -0700398 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400399
Chuck Lever262965f2005-08-11 16:25:56 -0400400 xs_pktdump("packet data:",
401 req->rq_svec->iov_base,
402 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400403
404 /* Continue transmitting the packet/record. We must be careful
405 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400406 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400407 while (1) {
408 req->rq_xtime = jiffies;
Chuck Lever262965f2005-08-11 16:25:56 -0400409 status = xs_sendpages(xprt->sock, NULL, 0, xdr,
410 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400411
Chuck Lever262965f2005-08-11 16:25:56 -0400412 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
413 xdr->len - req->rq_bytes_sent, status);
414
415 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400416 break;
417
Chuck Lever262965f2005-08-11 16:25:56 -0400418 /* If we've sent the entire packet, immediately
419 * reset the count of bytes sent. */
420 req->rq_bytes_sent += status;
Chuck Leveref759a22006-03-20 13:44:17 -0500421 task->tk_bytes_sent += status;
Chuck Lever262965f2005-08-11 16:25:56 -0400422 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
423 req->rq_bytes_sent = 0;
424 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400425 }
426
Chuck Levera246b012005-08-11 16:25:23 -0400427 status = -EAGAIN;
Chuck Lever262965f2005-08-11 16:25:56 -0400428 if (retry++ > XS_SENDMSG_RETRY)
Chuck Levera246b012005-08-11 16:25:23 -0400429 break;
430 }
431
Chuck Lever262965f2005-08-11 16:25:56 -0400432 switch (status) {
433 case -EAGAIN:
434 xs_nospace(task);
435 break;
436 case -ECONNREFUSED:
437 case -ECONNRESET:
438 case -ENOTCONN:
439 case -EPIPE:
440 status = -ENOTCONN;
441 break;
442 default:
443 dprintk("RPC: sendmsg returned unrecognized error %d\n",
444 -status);
Chuck Lever43118c22005-08-25 16:25:49 -0700445 xprt_disconnect(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400446 break;
Chuck Levera246b012005-08-11 16:25:23 -0400447 }
Chuck Lever262965f2005-08-11 16:25:56 -0400448
Chuck Levera246b012005-08-11 16:25:23 -0400449 return status;
450}
451
Chuck Lever9903cd12005-08-11 16:25:26 -0400452/**
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400453 * xs_tcp_release_xprt - clean up after a tcp transmission
454 * @xprt: transport
455 * @task: rpc task
456 *
457 * This cleans up if an error causes us to abort the transmission of a request.
458 * In this case, the socket may need to be reset in order to avoid confusing
459 * the server.
460 */
461static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
462{
463 struct rpc_rqst *req;
464
465 if (task != xprt->snd_task)
466 return;
467 if (task == NULL)
468 goto out_release;
469 req = task->tk_rqstp;
470 if (req->rq_bytes_sent == 0)
471 goto out_release;
472 if (req->rq_bytes_sent == req->rq_snd_buf.len)
473 goto out_release;
474 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
475out_release:
476 xprt_release_xprt(xprt, task);
477}
478
479/**
Chuck Lever9903cd12005-08-11 16:25:26 -0400480 * xs_close - close a socket
481 * @xprt: transport
482 *
Chuck Lever3167e122005-08-25 16:25:55 -0700483 * This is used when all requests are complete; ie, no DRC state remains
484 * on the server we want to save.
Chuck Levera246b012005-08-11 16:25:23 -0400485 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400486static void xs_close(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400487{
Chuck Lever9903cd12005-08-11 16:25:26 -0400488 struct socket *sock = xprt->sock;
489 struct sock *sk = xprt->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400490
491 if (!sk)
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100492 goto clear_close_wait;
Chuck Levera246b012005-08-11 16:25:23 -0400493
Chuck Lever9903cd12005-08-11 16:25:26 -0400494 dprintk("RPC: xs_close xprt %p\n", xprt);
495
Chuck Levera246b012005-08-11 16:25:23 -0400496 write_lock_bh(&sk->sk_callback_lock);
497 xprt->inet = NULL;
498 xprt->sock = NULL;
499
Chuck Lever9903cd12005-08-11 16:25:26 -0400500 sk->sk_user_data = NULL;
501 sk->sk_data_ready = xprt->old_data_ready;
Chuck Levera246b012005-08-11 16:25:23 -0400502 sk->sk_state_change = xprt->old_state_change;
Chuck Lever9903cd12005-08-11 16:25:26 -0400503 sk->sk_write_space = xprt->old_write_space;
Chuck Levera246b012005-08-11 16:25:23 -0400504 write_unlock_bh(&sk->sk_callback_lock);
505
Chuck Lever9903cd12005-08-11 16:25:26 -0400506 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400507
508 sock_release(sock);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100509clear_close_wait:
510 smp_mb__before_clear_bit();
511 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
512 smp_mb__after_clear_bit();
Chuck Levera246b012005-08-11 16:25:23 -0400513}
514
Chuck Lever9903cd12005-08-11 16:25:26 -0400515/**
516 * xs_destroy - prepare to shutdown a transport
517 * @xprt: doomed transport
518 *
519 */
520static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400521{
Chuck Lever9903cd12005-08-11 16:25:26 -0400522 dprintk("RPC: xs_destroy xprt %p\n", xprt);
523
Chuck Lever55aa4f52005-08-11 16:25:47 -0400524 cancel_delayed_work(&xprt->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400525 flush_scheduled_work();
526
527 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400528 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400529 xs_free_peer_addresses(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400530 kfree(xprt->slot);
531}
532
Chuck Lever9903cd12005-08-11 16:25:26 -0400533static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400534{
Chuck Lever9903cd12005-08-11 16:25:26 -0400535 return (struct rpc_xprt *) sk->sk_user_data;
536}
537
538/**
539 * xs_udp_data_ready - "data ready" callback for UDP sockets
540 * @sk: socket with data to read
541 * @len: how much data to read
542 *
543 */
544static void xs_udp_data_ready(struct sock *sk, int len)
545{
546 struct rpc_task *task;
547 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400548 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400549 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400550 int err, repsize, copied;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700551 u32 _xid;
552 __be32 *xp;
Chuck Levera246b012005-08-11 16:25:23 -0400553
554 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400555 dprintk("RPC: xs_udp_data_ready...\n");
556 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400557 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400558
559 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
560 goto out;
561
562 if (xprt->shutdown)
563 goto dropit;
564
565 repsize = skb->len - sizeof(struct udphdr);
566 if (repsize < 4) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400567 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400568 goto dropit;
569 }
570
571 /* Copy the XID from the skb... */
572 xp = skb_header_pointer(skb, sizeof(struct udphdr),
573 sizeof(_xid), &_xid);
574 if (xp == NULL)
575 goto dropit;
576
577 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400578 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400579 rovr = xprt_lookup_rqst(xprt, *xp);
580 if (!rovr)
581 goto out_unlock;
582 task = rovr->rq_task;
583
Chuck Levera246b012005-08-11 16:25:23 -0400584 if ((copied = rovr->rq_private_buf.buflen) > repsize)
585 copied = repsize;
586
587 /* Suck it into the iovec, verify checksum if not done by hw. */
588 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb))
589 goto out_unlock;
590
591 /* Something worked... */
592 dst_confirm(skb->dst);
593
Chuck Lever1570c1e2005-08-25 16:25:52 -0700594 xprt_adjust_cwnd(task, copied);
595 xprt_update_rtt(task);
596 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400597
598 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400599 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400600 dropit:
601 skb_free_datagram(sk, skb);
602 out:
603 read_unlock(&sk->sk_callback_lock);
604}
605
Chuck Lever9903cd12005-08-11 16:25:26 -0400606static inline size_t xs_tcp_copy_data(skb_reader_t *desc, void *p, size_t len)
Chuck Levera246b012005-08-11 16:25:23 -0400607{
608 if (len > desc->count)
609 len = desc->count;
610 if (skb_copy_bits(desc->skb, desc->offset, p, len)) {
611 dprintk("RPC: failed to copy %zu bytes from skb. %zu bytes remain\n",
612 len, desc->count);
613 return 0;
614 }
615 desc->offset += len;
616 desc->count -= len;
617 dprintk("RPC: copied %zu bytes from skb. %zu bytes remain\n",
618 len, desc->count);
619 return len;
620}
621
Chuck Lever9903cd12005-08-11 16:25:26 -0400622static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400623{
624 size_t len, used;
625 char *p;
626
627 p = ((char *) &xprt->tcp_recm) + xprt->tcp_offset;
628 len = sizeof(xprt->tcp_recm) - xprt->tcp_offset;
Chuck Lever9903cd12005-08-11 16:25:26 -0400629 used = xs_tcp_copy_data(desc, p, len);
Chuck Levera246b012005-08-11 16:25:23 -0400630 xprt->tcp_offset += used;
631 if (used != len)
632 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700633
Chuck Levera246b012005-08-11 16:25:23 -0400634 xprt->tcp_reclen = ntohl(xprt->tcp_recm);
Chuck Lever808012f2005-08-25 16:25:49 -0700635 if (xprt->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levera246b012005-08-11 16:25:23 -0400636 xprt->tcp_flags |= XPRT_LAST_FRAG;
637 else
638 xprt->tcp_flags &= ~XPRT_LAST_FRAG;
Chuck Lever808012f2005-08-25 16:25:49 -0700639 xprt->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
640
Chuck Levera246b012005-08-11 16:25:23 -0400641 xprt->tcp_flags &= ~XPRT_COPY_RECM;
642 xprt->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700643
Chuck Levera246b012005-08-11 16:25:23 -0400644 /* Sanity check of the record length */
Chuck Lever808012f2005-08-25 16:25:49 -0700645 if (unlikely(xprt->tcp_reclen < 4)) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400646 dprintk("RPC: invalid TCP record fragment length\n");
Chuck Levera246b012005-08-11 16:25:23 -0400647 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400648 return;
Chuck Levera246b012005-08-11 16:25:23 -0400649 }
650 dprintk("RPC: reading TCP record fragment of length %d\n",
651 xprt->tcp_reclen);
652}
653
Chuck Lever9903cd12005-08-11 16:25:26 -0400654static void xs_tcp_check_recm(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400655{
656 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u, tcp_flags = %lx\n",
657 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen, xprt->tcp_flags);
658 if (xprt->tcp_offset == xprt->tcp_reclen) {
659 xprt->tcp_flags |= XPRT_COPY_RECM;
660 xprt->tcp_offset = 0;
661 if (xprt->tcp_flags & XPRT_LAST_FRAG) {
662 xprt->tcp_flags &= ~XPRT_COPY_DATA;
663 xprt->tcp_flags |= XPRT_COPY_XID;
664 xprt->tcp_copied = 0;
665 }
666 }
667}
668
Chuck Lever9903cd12005-08-11 16:25:26 -0400669static inline void xs_tcp_read_xid(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400670{
671 size_t len, used;
672 char *p;
673
674 len = sizeof(xprt->tcp_xid) - xprt->tcp_offset;
675 dprintk("RPC: reading XID (%Zu bytes)\n", len);
676 p = ((char *) &xprt->tcp_xid) + xprt->tcp_offset;
Chuck Lever9903cd12005-08-11 16:25:26 -0400677 used = xs_tcp_copy_data(desc, p, len);
Chuck Levera246b012005-08-11 16:25:23 -0400678 xprt->tcp_offset += used;
679 if (used != len)
680 return;
681 xprt->tcp_flags &= ~XPRT_COPY_XID;
682 xprt->tcp_flags |= XPRT_COPY_DATA;
683 xprt->tcp_copied = 4;
684 dprintk("RPC: reading reply for XID %08x\n",
685 ntohl(xprt->tcp_xid));
Chuck Lever9903cd12005-08-11 16:25:26 -0400686 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400687}
688
Chuck Lever9903cd12005-08-11 16:25:26 -0400689static inline void xs_tcp_read_request(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400690{
691 struct rpc_rqst *req;
692 struct xdr_buf *rcvbuf;
693 size_t len;
694 ssize_t r;
695
696 /* Find and lock the request corresponding to this xid */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400697 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400698 req = xprt_lookup_rqst(xprt, xprt->tcp_xid);
699 if (!req) {
700 xprt->tcp_flags &= ~XPRT_COPY_DATA;
701 dprintk("RPC: XID %08x request not found!\n",
702 ntohl(xprt->tcp_xid));
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400703 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400704 return;
705 }
706
707 rcvbuf = &req->rq_private_buf;
708 len = desc->count;
709 if (len > xprt->tcp_reclen - xprt->tcp_offset) {
710 skb_reader_t my_desc;
711
712 len = xprt->tcp_reclen - xprt->tcp_offset;
713 memcpy(&my_desc, desc, sizeof(my_desc));
714 my_desc.count = len;
715 r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied,
Chuck Lever9903cd12005-08-11 16:25:26 -0400716 &my_desc, xs_tcp_copy_data);
Chuck Levera246b012005-08-11 16:25:23 -0400717 desc->count -= r;
718 desc->offset += r;
719 } else
720 r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied,
Chuck Lever9903cd12005-08-11 16:25:26 -0400721 desc, xs_tcp_copy_data);
Chuck Levera246b012005-08-11 16:25:23 -0400722
723 if (r > 0) {
724 xprt->tcp_copied += r;
725 xprt->tcp_offset += r;
726 }
727 if (r != len) {
728 /* Error when copying to the receive buffer,
729 * usually because we weren't able to allocate
730 * additional buffer pages. All we can do now
731 * is turn off XPRT_COPY_DATA, so the request
732 * will not receive any additional updates,
733 * and time out.
734 * Any remaining data from this record will
735 * be discarded.
736 */
737 xprt->tcp_flags &= ~XPRT_COPY_DATA;
738 dprintk("RPC: XID %08x truncated request\n",
739 ntohl(xprt->tcp_xid));
740 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
741 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen);
742 goto out;
743 }
744
745 dprintk("RPC: XID %08x read %Zd bytes\n",
746 ntohl(xprt->tcp_xid), r);
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
750 if (xprt->tcp_copied == req->rq_private_buf.buflen)
751 xprt->tcp_flags &= ~XPRT_COPY_DATA;
752 else if (xprt->tcp_offset == xprt->tcp_reclen) {
753 if (xprt->tcp_flags & XPRT_LAST_FRAG)
754 xprt->tcp_flags &= ~XPRT_COPY_DATA;
755 }
756
757out:
Chuck Lever1570c1e2005-08-25 16:25:52 -0700758 if (!(xprt->tcp_flags & XPRT_COPY_DATA))
759 xprt_complete_rqst(req->rq_task, xprt->tcp_copied);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400760 spin_unlock(&xprt->transport_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400761 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400762}
763
Chuck Lever9903cd12005-08-11 16:25:26 -0400764static inline void xs_tcp_read_discard(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400765{
766 size_t len;
767
768 len = xprt->tcp_reclen - xprt->tcp_offset;
769 if (len > desc->count)
770 len = desc->count;
771 desc->count -= len;
772 desc->offset += len;
773 xprt->tcp_offset += len;
774 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever9903cd12005-08-11 16:25:26 -0400775 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400776}
777
Chuck Lever9903cd12005-08-11 16:25:26 -0400778static 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 -0400779{
780 struct rpc_xprt *xprt = rd_desc->arg.data;
781 skb_reader_t desc = {
782 .skb = skb,
783 .offset = offset,
784 .count = len,
785 .csum = 0
Chuck Lever9903cd12005-08-11 16:25:26 -0400786 };
Chuck Levera246b012005-08-11 16:25:23 -0400787
Chuck Lever9903cd12005-08-11 16:25:26 -0400788 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -0400789 do {
790 /* Read in a new fragment marker if necessary */
791 /* Can we ever really expect to get completely empty fragments? */
792 if (xprt->tcp_flags & XPRT_COPY_RECM) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400793 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400794 continue;
795 }
796 /* Read in the xid if necessary */
797 if (xprt->tcp_flags & XPRT_COPY_XID) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400798 xs_tcp_read_xid(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400799 continue;
800 }
801 /* Read in the request data */
802 if (xprt->tcp_flags & XPRT_COPY_DATA) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400803 xs_tcp_read_request(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400804 continue;
805 }
806 /* Skip over any trailing bytes on short reads */
Chuck Lever9903cd12005-08-11 16:25:26 -0400807 xs_tcp_read_discard(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400808 } while (desc.count);
Chuck Lever9903cd12005-08-11 16:25:26 -0400809 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -0400810 return len - desc.count;
811}
812
Chuck Lever9903cd12005-08-11 16:25:26 -0400813/**
814 * xs_tcp_data_ready - "data ready" callback for TCP sockets
815 * @sk: socket with data to read
816 * @bytes: how much data to read
817 *
818 */
819static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -0400820{
821 struct rpc_xprt *xprt;
822 read_descriptor_t rd_desc;
823
824 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400825 dprintk("RPC: xs_tcp_data_ready...\n");
826 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400827 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400828 if (xprt->shutdown)
829 goto out;
830
Chuck Lever9903cd12005-08-11 16:25:26 -0400831 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -0400832 rd_desc.arg.data = xprt;
833 rd_desc.count = 65536;
Chuck Lever9903cd12005-08-11 16:25:26 -0400834 tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
Chuck Levera246b012005-08-11 16:25:23 -0400835out:
836 read_unlock(&sk->sk_callback_lock);
837}
838
Chuck Lever9903cd12005-08-11 16:25:26 -0400839/**
840 * xs_tcp_state_change - callback to handle TCP socket state changes
841 * @sk: socket whose state has changed
842 *
843 */
844static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400845{
Chuck Lever9903cd12005-08-11 16:25:26 -0400846 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400847
848 read_lock(&sk->sk_callback_lock);
849 if (!(xprt = xprt_from_sock(sk)))
850 goto out;
Chuck Lever9903cd12005-08-11 16:25:26 -0400851 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400852 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
853 sk->sk_state, xprt_connected(xprt),
854 sock_flag(sk, SOCK_DEAD),
855 sock_flag(sk, SOCK_ZAPPED));
856
857 switch (sk->sk_state) {
858 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400859 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400860 if (!xprt_test_and_set_connected(xprt)) {
861 /* Reset TCP record info */
862 xprt->tcp_offset = 0;
863 xprt->tcp_reclen = 0;
864 xprt->tcp_copied = 0;
865 xprt->tcp_flags = XPRT_COPY_RECM | XPRT_COPY_XID;
Chuck Lever03bf4b72005-08-25 16:25:55 -0700866 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever44fbac22005-08-11 16:25:44 -0400867 xprt_wake_pending_tasks(xprt, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400868 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400869 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400870 break;
871 case TCP_SYN_SENT:
872 case TCP_SYN_RECV:
873 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100874 case TCP_CLOSE_WAIT:
875 /* Try to schedule an autoclose RPC calls */
876 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
877 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
878 schedule_work(&xprt->task_cleanup);
Chuck Levera246b012005-08-11 16:25:23 -0400879 default:
880 xprt_disconnect(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400881 }
882 out:
883 read_unlock(&sk->sk_callback_lock);
884}
885
Chuck Lever9903cd12005-08-11 16:25:26 -0400886/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400887 * xs_udp_write_space - callback invoked when socket buffer space
888 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -0400889 * @sk: socket whose state has changed
890 *
Chuck Levera246b012005-08-11 16:25:23 -0400891 * Called when more output buffer space is available for this socket.
892 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400893 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -0400894 * with a bunch of small requests.
895 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400896static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400897{
Chuck Levera246b012005-08-11 16:25:23 -0400898 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400899
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400900 /* from net/core/sock.c:sock_def_write_space */
901 if (sock_writeable(sk)) {
902 struct socket *sock;
903 struct rpc_xprt *xprt;
904
905 if (unlikely(!(sock = sk->sk_socket)))
Chuck Levera246b012005-08-11 16:25:23 -0400906 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400907 if (unlikely(!(xprt = xprt_from_sock(sk))))
Chuck Levera246b012005-08-11 16:25:23 -0400908 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400909 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
910 goto out;
911
912 xprt_write_space(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400913 }
914
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400915 out:
916 read_unlock(&sk->sk_callback_lock);
917}
Chuck Levera246b012005-08-11 16:25:23 -0400918
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400919/**
920 * xs_tcp_write_space - callback invoked when socket buffer space
921 * becomes available
922 * @sk: socket whose state has changed
923 *
924 * Called when more output buffer space is available for this socket.
925 * We try not to wake our writers until they can make "significant"
926 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
927 * with a bunch of small requests.
928 */
929static void xs_tcp_write_space(struct sock *sk)
930{
931 read_lock(&sk->sk_callback_lock);
932
933 /* from net/core/stream.c:sk_stream_write_space */
934 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
935 struct socket *sock;
936 struct rpc_xprt *xprt;
937
938 if (unlikely(!(sock = sk->sk_socket)))
939 goto out;
940 if (unlikely(!(xprt = xprt_from_sock(sk))))
941 goto out;
942 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
943 goto out;
944
945 xprt_write_space(xprt);
946 }
947
948 out:
Chuck Levera246b012005-08-11 16:25:23 -0400949 read_unlock(&sk->sk_callback_lock);
950}
951
Chuck Lever470056c2005-08-25 16:25:56 -0700952static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400953{
954 struct sock *sk = xprt->inet;
955
Chuck Levera246b012005-08-11 16:25:23 -0400956 if (xprt->rcvsize) {
957 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
958 sk->sk_rcvbuf = xprt->rcvsize * xprt->max_reqs * 2;
959 }
960 if (xprt->sndsize) {
961 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
962 sk->sk_sndbuf = xprt->sndsize * xprt->max_reqs * 2;
963 sk->sk_write_space(sk);
964 }
965}
966
Chuck Lever43118c22005-08-25 16:25:49 -0700967/**
Chuck Lever470056c2005-08-25 16:25:56 -0700968 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -0700969 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -0700970 * @sndsize: requested size of send buffer, in bytes
971 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -0700972 *
Chuck Lever470056c2005-08-25 16:25:56 -0700973 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -0700974 */
Chuck Lever470056c2005-08-25 16:25:56 -0700975static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -0700976{
Chuck Lever470056c2005-08-25 16:25:56 -0700977 xprt->sndsize = 0;
978 if (sndsize)
979 xprt->sndsize = sndsize + 1024;
980 xprt->rcvsize = 0;
981 if (rcvsize)
982 xprt->rcvsize = rcvsize + 1024;
983
984 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -0700985}
986
Chuck Lever46c0ee82005-08-25 16:25:52 -0700987/**
988 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
989 * @task: task that timed out
990 *
991 * Adjust the congestion window after a retransmit timeout has occurred.
992 */
993static void xs_udp_timer(struct rpc_task *task)
994{
995 xprt_adjust_cwnd(task, -ETIMEDOUT);
996}
997
Chuck Leverb85d8802006-05-25 01:40:49 -0400998static unsigned short xs_get_random_port(void)
999{
1000 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1001 unsigned short rand = (unsigned short) net_random() % range;
1002 return rand + xprt_min_resvport;
1003}
1004
Chuck Lever92200412006-01-03 09:55:51 +01001005/**
Chuck Leveredb267a2006-08-22 20:06:18 -04001006 * xs_print_peer_address - format an IPv4 address for printing
1007 * @xprt: generic transport
1008 * @format: flags field indicating which parts of the address to render
1009 */
1010static char *xs_print_peer_address(struct rpc_xprt *xprt, enum rpc_display_format_t format)
1011{
1012 if (xprt->address_strings[format] != NULL)
1013 return xprt->address_strings[format];
1014 else
1015 return "unprintable";
1016}
1017
1018/**
Chuck Lever92200412006-01-03 09:55:51 +01001019 * xs_set_port - reset the port number in the remote endpoint address
1020 * @xprt: generic transport
1021 * @port: new port number
1022 *
1023 */
1024static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1025{
Chuck Leverc4efcb12006-08-22 20:06:19 -04001026 struct sockaddr_in *sap = (struct sockaddr_in *) &xprt->addr;
1027
Chuck Lever92200412006-01-03 09:55:51 +01001028 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001029
1030 sap->sin_port = htons(port);
Chuck Lever92200412006-01-03 09:55:51 +01001031}
1032
Chuck Lever9903cd12005-08-11 16:25:26 -04001033static int xs_bindresvport(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001034{
1035 struct sockaddr_in myaddr = {
1036 .sin_family = AF_INET,
1037 };
Chuck Lever529b33c2005-08-25 16:25:54 -07001038 int err;
1039 unsigned short port = xprt->port;
Chuck Levera246b012005-08-11 16:25:23 -04001040
Chuck Levera246b012005-08-11 16:25:23 -04001041 do {
1042 myaddr.sin_port = htons(port);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001043 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
Chuck Levera246b012005-08-11 16:25:23 -04001044 sizeof(myaddr));
1045 if (err == 0) {
1046 xprt->port = port;
Chuck Lever9903cd12005-08-11 16:25:26 -04001047 dprintk("RPC: xs_bindresvport bound to port %u\n",
1048 port);
Chuck Levera246b012005-08-11 16:25:23 -04001049 return 0;
1050 }
Chuck Lever529b33c2005-08-25 16:25:54 -07001051 if (port <= xprt_min_resvport)
1052 port = xprt_max_resvport;
1053 else
1054 port--;
Chuck Levera246b012005-08-11 16:25:23 -04001055 } while (err == -EADDRINUSE && port != xprt->port);
1056
Chuck Lever9903cd12005-08-11 16:25:26 -04001057 dprintk("RPC: can't bind to reserved port (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001058 return err;
1059}
1060
Chuck Lever9903cd12005-08-11 16:25:26 -04001061/**
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001062 * xs_udp_connect_worker - set up a UDP socket
Chuck Lever9903cd12005-08-11 16:25:26 -04001063 * @args: RPC transport to connect
1064 *
1065 * Invoked by a work queue tasklet.
Chuck Levera246b012005-08-11 16:25:23 -04001066 */
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001067static void xs_udp_connect_worker(void *args)
Chuck Levera246b012005-08-11 16:25:23 -04001068{
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001069 struct rpc_xprt *xprt = (struct rpc_xprt *) args;
Chuck Levera246b012005-08-11 16:25:23 -04001070 struct socket *sock = xprt->sock;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001071 int err, status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04001072
Chuck Leverec739ef2006-08-22 20:06:15 -04001073 if (xprt->shutdown || !xprt_bound(xprt))
Chuck Levera246b012005-08-11 16:25:23 -04001074 goto out;
1075
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001076 /* Start by resetting any existing state */
Chuck Lever9903cd12005-08-11 16:25:26 -04001077 xs_close(xprt);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001078
1079 if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
1080 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001081 goto out;
1082 }
Chuck Levera246b012005-08-11 16:25:23 -04001083
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001084 if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) {
1085 sock_release(sock);
1086 goto out;
1087 }
1088
Chuck Leveredb267a2006-08-22 20:06:18 -04001089 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1090 xprt, xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1091
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001092 if (!xprt->inet) {
1093 struct sock *sk = sock->sk;
1094
1095 write_lock_bh(&sk->sk_callback_lock);
1096
1097 sk->sk_user_data = xprt;
1098 xprt->old_data_ready = sk->sk_data_ready;
1099 xprt->old_state_change = sk->sk_state_change;
1100 xprt->old_write_space = sk->sk_write_space;
1101 sk->sk_data_ready = xs_udp_data_ready;
1102 sk->sk_write_space = xs_udp_write_space;
1103 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001104 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001105
1106 xprt_set_connected(xprt);
1107
1108 /* Reset to new socket */
1109 xprt->sock = sock;
1110 xprt->inet = sk;
1111
1112 write_unlock_bh(&sk->sk_callback_lock);
1113 }
Chuck Lever470056c2005-08-25 16:25:56 -07001114 xs_udp_do_set_buffer_size(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001115 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001116out:
1117 xprt_wake_pending_tasks(xprt, status);
1118 xprt_clear_connecting(xprt);
1119}
1120
Chuck Lever3167e122005-08-25 16:25:55 -07001121/*
1122 * We need to preserve the port number so the reply cache on the server can
1123 * find our cached RPC replies when we get around to reconnecting.
1124 */
1125static void xs_tcp_reuse_connection(struct rpc_xprt *xprt)
1126{
1127 int result;
1128 struct socket *sock = xprt->sock;
1129 struct sockaddr any;
1130
1131 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
1132
1133 /*
1134 * Disconnect the transport socket by doing a connect operation
1135 * with AF_UNSPEC. This should return immediately...
1136 */
1137 memset(&any, 0, sizeof(any));
1138 any.sa_family = AF_UNSPEC;
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001139 result = kernel_connect(sock, &any, sizeof(any), 0);
Chuck Lever3167e122005-08-25 16:25:55 -07001140 if (result)
1141 dprintk("RPC: AF_UNSPEC connect return code %d\n",
1142 result);
1143}
1144
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001145/**
1146 * xs_tcp_connect_worker - connect a TCP socket to a remote endpoint
1147 * @args: RPC transport to connect
1148 *
1149 * Invoked by a work queue tasklet.
1150 */
1151static void xs_tcp_connect_worker(void *args)
1152{
1153 struct rpc_xprt *xprt = (struct rpc_xprt *)args;
1154 struct socket *sock = xprt->sock;
1155 int err, status = -EIO;
1156
Chuck Leverec739ef2006-08-22 20:06:15 -04001157 if (xprt->shutdown || !xprt_bound(xprt))
Chuck Levera246b012005-08-11 16:25:23 -04001158 goto out;
1159
Chuck Lever3167e122005-08-25 16:25:55 -07001160 if (!xprt->sock) {
1161 /* start from scratch */
1162 if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
1163 dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
1164 goto out;
1165 }
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001166
Chuck Lever3167e122005-08-25 16:25:55 -07001167 if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) {
1168 sock_release(sock);
1169 goto out;
1170 }
1171 } else
1172 /* "close" the socket, preserving the local port */
1173 xs_tcp_reuse_connection(xprt);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001174
Chuck Leveredb267a2006-08-22 20:06:18 -04001175 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1176 xprt, xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1177
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001178 if (!xprt->inet) {
1179 struct sock *sk = sock->sk;
1180
1181 write_lock_bh(&sk->sk_callback_lock);
1182
1183 sk->sk_user_data = xprt;
1184 xprt->old_data_ready = sk->sk_data_ready;
1185 xprt->old_state_change = sk->sk_state_change;
1186 xprt->old_write_space = sk->sk_write_space;
1187 sk->sk_data_ready = xs_tcp_data_ready;
1188 sk->sk_state_change = xs_tcp_state_change;
1189 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001190 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07001191
1192 /* socket options */
1193 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1194 sock_reset_flag(sk, SOCK_LINGER);
1195 tcp_sk(sk)->linger2 = 0;
1196 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001197
1198 xprt_clear_connected(xprt);
1199
1200 /* Reset to new socket */
1201 xprt->sock = sock;
1202 xprt->inet = sk;
1203
1204 write_unlock_bh(&sk->sk_callback_lock);
1205 }
1206
1207 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05001208 xprt->stat.connect_count++;
1209 xprt->stat.connect_start = jiffies;
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001210 status = kernel_connect(sock, (struct sockaddr *) &xprt->addr,
Chuck Leverc4efcb12006-08-22 20:06:19 -04001211 xprt->addrlen, O_NONBLOCK);
Chuck Levera246b012005-08-11 16:25:23 -04001212 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1213 xprt, -status, xprt_connected(xprt), sock->sk->sk_state);
1214 if (status < 0) {
1215 switch (status) {
1216 case -EINPROGRESS:
1217 case -EALREADY:
1218 goto out_clear;
Chuck Lever3167e122005-08-25 16:25:55 -07001219 case -ECONNREFUSED:
1220 case -ECONNRESET:
1221 /* retry with existing socket, after a delay */
1222 break;
1223 default:
1224 /* get rid of existing socket, and retry */
1225 xs_close(xprt);
1226 break;
Chuck Levera246b012005-08-11 16:25:23 -04001227 }
1228 }
1229out:
Chuck Lever44fbac22005-08-11 16:25:44 -04001230 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001231out_clear:
Chuck Lever2226feb2005-08-11 16:25:38 -04001232 xprt_clear_connecting(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001233}
1234
Chuck Lever9903cd12005-08-11 16:25:26 -04001235/**
1236 * xs_connect - connect a socket to a remote endpoint
1237 * @task: address of RPC task that manages state of connect request
1238 *
1239 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07001240 *
1241 * UDP socket connects are synchronous, but we use a work queue anyway
1242 * to guarantee that even unprivileged user processes can set up a
1243 * socket on a privileged port.
1244 *
1245 * If a UDP socket connect fails, the delay behavior here prevents
1246 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04001247 */
1248static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04001249{
1250 struct rpc_xprt *xprt = task->tk_xprt;
1251
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001252 if (xprt_test_and_set_connecting(xprt))
1253 return;
1254
1255 if (xprt->sock != NULL) {
Chuck Lever03bf4b72005-08-25 16:25:55 -07001256 dprintk("RPC: xs_connect delayed xprt %p for %lu seconds\n",
1257 xprt, xprt->reestablish_timeout / HZ);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001258 schedule_delayed_work(&xprt->connect_worker,
Chuck Lever03bf4b72005-08-25 16:25:55 -07001259 xprt->reestablish_timeout);
1260 xprt->reestablish_timeout <<= 1;
1261 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
1262 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001263 } else {
1264 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
1265 schedule_work(&xprt->connect_worker);
1266
1267 /* flush_scheduled_work can sleep... */
1268 if (!RPC_IS_ASYNC(task))
1269 flush_scheduled_work();
Chuck Levera246b012005-08-11 16:25:23 -04001270 }
1271}
1272
Chuck Lever262ca072006-03-20 13:44:16 -05001273/**
1274 * xs_udp_print_stats - display UDP socket-specifc stats
1275 * @xprt: rpc_xprt struct containing statistics
1276 * @seq: output file
1277 *
1278 */
1279static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1280{
1281 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
1282 xprt->port,
1283 xprt->stat.bind_count,
1284 xprt->stat.sends,
1285 xprt->stat.recvs,
1286 xprt->stat.bad_xids,
1287 xprt->stat.req_u,
1288 xprt->stat.bklog_u);
1289}
1290
1291/**
1292 * xs_tcp_print_stats - display TCP socket-specifc stats
1293 * @xprt: rpc_xprt struct containing statistics
1294 * @seq: output file
1295 *
1296 */
1297static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1298{
1299 long idle_time = 0;
1300
1301 if (xprt_connected(xprt))
1302 idle_time = (long)(jiffies - xprt->last_used) / HZ;
1303
1304 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
1305 xprt->port,
1306 xprt->stat.bind_count,
1307 xprt->stat.connect_count,
1308 xprt->stat.connect_time,
1309 idle_time,
1310 xprt->stat.sends,
1311 xprt->stat.recvs,
1312 xprt->stat.bad_xids,
1313 xprt->stat.req_u,
1314 xprt->stat.bklog_u);
1315}
1316
Chuck Lever262965f2005-08-11 16:25:56 -04001317static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07001318 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Leveredb267a2006-08-22 20:06:18 -04001319 .print_addr = xs_print_peer_address,
Chuck Lever12a80462005-08-25 16:25:51 -07001320 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07001321 .release_xprt = xprt_release_xprt_cong,
Chuck Leverbbf7c1d2006-08-22 20:06:16 -04001322 .rpcbind = rpc_getport,
Chuck Lever92200412006-01-03 09:55:51 +01001323 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04001324 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01001325 .buf_alloc = rpc_malloc,
1326 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04001327 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001328 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07001329 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07001330 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04001331 .close = xs_close,
1332 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05001333 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04001334};
1335
1336static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Leveredb267a2006-08-22 20:06:18 -04001337 .print_addr = xs_print_peer_address,
Chuck Lever12a80462005-08-25 16:25:51 -07001338 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001339 .release_xprt = xs_tcp_release_xprt,
Chuck Leverbbf7c1d2006-08-22 20:06:16 -04001340 .rpcbind = rpc_getport,
Chuck Lever92200412006-01-03 09:55:51 +01001341 .set_port = xs_set_port,
Chuck Lever262965f2005-08-11 16:25:56 -04001342 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01001343 .buf_alloc = rpc_malloc,
1344 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04001345 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001346 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Chuck Lever9903cd12005-08-11 16:25:26 -04001347 .close = xs_close,
1348 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05001349 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04001350};
1351
Chuck Lever9903cd12005-08-11 16:25:26 -04001352/**
1353 * xs_setup_udp - Set up transport to use a UDP socket
1354 * @xprt: transport to set up
1355 * @to: timeout parameters
1356 *
1357 */
Chuck Levera246b012005-08-11 16:25:23 -04001358int xs_setup_udp(struct rpc_xprt *xprt, struct rpc_timeout *to)
1359{
1360 size_t slot_table_size;
Chuck Leverc4efcb12006-08-22 20:06:19 -04001361 struct sockaddr_in *addr = (struct sockaddr_in *) &xprt->addr;
Chuck Levera246b012005-08-11 16:25:23 -04001362
1363 xprt->max_reqs = xprt_udp_slot_table_entries;
1364 slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]);
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001365 xprt->slot = kzalloc(slot_table_size, GFP_KERNEL);
Chuck Levera246b012005-08-11 16:25:23 -04001366 if (xprt->slot == NULL)
1367 return -ENOMEM;
Chuck Levera246b012005-08-11 16:25:23 -04001368
Chuck Leverb7766da2006-10-19 23:28:44 -07001369 if (ntohs(addr->sin_port) != 0)
Chuck Leverec739ef2006-08-22 20:06:15 -04001370 xprt_set_bound(xprt);
Chuck Leverb85d8802006-05-25 01:40:49 -04001371 xprt->port = xs_get_random_port();
Chuck Leverec739ef2006-08-22 20:06:15 -04001372
1373 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07001374 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001375 /* XXX: header size can vary due to auth type, IPv6, etc. */
1376 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
1377
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001378 INIT_WORK(&xprt->connect_worker, xs_udp_connect_worker, xprt);
Chuck Lever03bf4b72005-08-25 16:25:55 -07001379 xprt->bind_timeout = XS_BIND_TO;
1380 xprt->connect_timeout = XS_UDP_CONN_TO;
1381 xprt->reestablish_timeout = XS_UDP_REEST_TO;
1382 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04001383
Chuck Lever262965f2005-08-11 16:25:56 -04001384 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001385
1386 if (to)
1387 xprt->timeout = *to;
1388 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001389 xprt_set_timeout(&xprt->timeout, 5, 5 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001390
Chuck Leveredb267a2006-08-22 20:06:18 -04001391 xs_format_peer_addresses(xprt);
1392 dprintk("RPC: set up transport to address %s\n",
1393 xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1394
Chuck Levera246b012005-08-11 16:25:23 -04001395 return 0;
1396}
1397
Chuck Lever9903cd12005-08-11 16:25:26 -04001398/**
1399 * xs_setup_tcp - Set up transport to use a TCP socket
1400 * @xprt: transport to set up
1401 * @to: timeout parameters
1402 *
1403 */
Chuck Levera246b012005-08-11 16:25:23 -04001404int xs_setup_tcp(struct rpc_xprt *xprt, struct rpc_timeout *to)
1405{
1406 size_t slot_table_size;
Chuck Leverc4efcb12006-08-22 20:06:19 -04001407 struct sockaddr_in *addr = (struct sockaddr_in *) &xprt->addr;
Chuck Levera246b012005-08-11 16:25:23 -04001408
1409 xprt->max_reqs = xprt_tcp_slot_table_entries;
1410 slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]);
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001411 xprt->slot = kzalloc(slot_table_size, GFP_KERNEL);
Chuck Levera246b012005-08-11 16:25:23 -04001412 if (xprt->slot == NULL)
1413 return -ENOMEM;
Chuck Levera246b012005-08-11 16:25:23 -04001414
Chuck Leverc4efcb12006-08-22 20:06:19 -04001415 if (ntohs(addr->sin_port) != 0)
Chuck Leverec739ef2006-08-22 20:06:15 -04001416 xprt_set_bound(xprt);
Chuck Leverb85d8802006-05-25 01:40:49 -04001417 xprt->port = xs_get_random_port();
Chuck Leverec739ef2006-08-22 20:06:15 -04001418
1419 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07001420 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Lever808012f2005-08-25 16:25:49 -07001421 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04001422
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001423 INIT_WORK(&xprt->connect_worker, xs_tcp_connect_worker, xprt);
Chuck Lever03bf4b72005-08-25 16:25:55 -07001424 xprt->bind_timeout = XS_BIND_TO;
1425 xprt->connect_timeout = XS_TCP_CONN_TO;
1426 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1427 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04001428
Chuck Lever262965f2005-08-11 16:25:56 -04001429 xprt->ops = &xs_tcp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001430
1431 if (to)
1432 xprt->timeout = *to;
1433 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001434 xprt_set_timeout(&xprt->timeout, 2, 60 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001435
Chuck Leveredb267a2006-08-22 20:06:18 -04001436 xs_format_peer_addresses(xprt);
1437 dprintk("RPC: set up transport to address %s\n",
1438 xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1439
Chuck Levera246b012005-08-11 16:25:23 -04001440 return 0;
1441}