blob: 17179aa4c2071eabcd53794d911bbea8bed6f856 [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;
218 ssize_t (*sendpage)(struct socket *, struct page *, int, size_t, int);
219
Chuck Lever262965f2005-08-11 16:25:56 -0400220 if (unlikely(!sock))
221 return -ENOTCONN;
222
223 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
224
Chuck Levera246b012005-08-11 16:25:23 -0400225 len = xdr->head[0].iov_len;
226 if (base < len || (addr != NULL && base == 0)) {
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400227 err = xs_send_head(sock, addr, addrlen, xdr, base, len);
Chuck Levera246b012005-08-11 16:25:23 -0400228 if (ret == 0)
229 ret = err;
230 else if (err > 0)
231 ret += err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400232 if (err != (len - base))
Chuck Levera246b012005-08-11 16:25:23 -0400233 goto out;
234 base = 0;
235 } else
236 base -= len;
237
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400238 if (unlikely(pglen == 0))
Chuck Levera246b012005-08-11 16:25:23 -0400239 goto copy_tail;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400240 if (unlikely(base >= pglen)) {
Chuck Levera246b012005-08-11 16:25:23 -0400241 base -= pglen;
242 goto copy_tail;
243 }
244 if (base || xdr->page_base) {
245 pglen -= base;
Chuck Lever9903cd12005-08-11 16:25:26 -0400246 base += xdr->page_base;
Chuck Levera246b012005-08-11 16:25:23 -0400247 ppage += base >> PAGE_CACHE_SHIFT;
248 base &= ~PAGE_CACHE_MASK;
249 }
250
251 sendpage = sock->ops->sendpage ? : sock_no_sendpage;
252 do {
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400253 int flags = XS_SENDMSG_FLAGS;
Chuck Levera246b012005-08-11 16:25:23 -0400254
255 len = PAGE_CACHE_SIZE;
256 if (base)
257 len -= base;
258 if (pglen < len)
259 len = pglen;
260
261 if (pglen != len || xdr->tail[0].iov_len != 0)
262 flags |= MSG_MORE;
263
264 /* Hmm... We might be dealing with highmem pages */
265 if (PageHighMem(*ppage))
266 sendpage = sock_no_sendpage;
267 err = sendpage(sock, *ppage, base, len, flags);
268 if (ret == 0)
269 ret = err;
270 else if (err > 0)
271 ret += err;
272 if (err != len)
273 goto out;
274 base = 0;
275 ppage++;
276 } while ((pglen -= len) != 0);
277copy_tail:
278 len = xdr->tail[0].iov_len;
279 if (base < len) {
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400280 err = xs_send_tail(sock, xdr, base, len);
Chuck Levera246b012005-08-11 16:25:23 -0400281 if (ret == 0)
282 ret = err;
283 else if (err > 0)
284 ret += err;
285 }
286out:
287 return ret;
288}
289
Chuck Lever9903cd12005-08-11 16:25:26 -0400290/**
Chuck Lever262965f2005-08-11 16:25:56 -0400291 * xs_nospace - place task on wait queue if transmit was incomplete
292 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400293 *
Chuck Levera246b012005-08-11 16:25:23 -0400294 */
Chuck Lever262965f2005-08-11 16:25:56 -0400295static void xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400296{
Chuck Lever262965f2005-08-11 16:25:56 -0400297 struct rpc_rqst *req = task->tk_rqstp;
298 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400299
Chuck Lever262965f2005-08-11 16:25:56 -0400300 dprintk("RPC: %4d xmit incomplete (%u left of %u)\n",
301 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
302 req->rq_slen);
303
304 if (test_bit(SOCK_ASYNC_NOSPACE, &xprt->sock->flags)) {
305 /* Protect against races with write_space */
306 spin_lock_bh(&xprt->transport_lock);
307
308 /* Don't race with disconnect */
309 if (!xprt_connected(xprt))
310 task->tk_status = -ENOTCONN;
311 else if (test_bit(SOCK_NOSPACE, &xprt->sock->flags))
312 xprt_wait_for_buffer_space(task);
313
314 spin_unlock_bh(&xprt->transport_lock);
315 } else
316 /* Keep holding the socket if it is blocked */
317 rpc_delay(task, HZ>>4);
318}
319
320/**
321 * xs_udp_send_request - write an RPC request to a UDP socket
322 * @task: address of RPC task that manages the state of an RPC request
323 *
324 * Return values:
325 * 0: The request has been sent
326 * EAGAIN: The socket was blocked, please call again later to
327 * complete the request
328 * ENOTCONN: Caller needs to invoke connect logic then call again
329 * other: Some other error occured, the request was not sent
330 */
331static int xs_udp_send_request(struct rpc_task *task)
332{
333 struct rpc_rqst *req = task->tk_rqstp;
334 struct rpc_xprt *xprt = req->rq_xprt;
335 struct xdr_buf *xdr = &req->rq_snd_buf;
336 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400337
Chuck Lever9903cd12005-08-11 16:25:26 -0400338 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400339 req->rq_svec->iov_base,
340 req->rq_svec->iov_len);
341
Chuck Lever262965f2005-08-11 16:25:56 -0400342 req->rq_xtime = jiffies;
343 status = xs_sendpages(xprt->sock, (struct sockaddr *) &xprt->addr,
Chuck Leverc4efcb12006-08-22 20:06:19 -0400344 xprt->addrlen, xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400345
Chuck Lever262965f2005-08-11 16:25:56 -0400346 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
347 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400348
Chuck Lever262965f2005-08-11 16:25:56 -0400349 if (likely(status >= (int) req->rq_slen))
350 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400351
Chuck Lever262965f2005-08-11 16:25:56 -0400352 /* Still some bytes left; set up for a retry later. */
353 if (status > 0)
354 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400355
Chuck Lever262965f2005-08-11 16:25:56 -0400356 switch (status) {
357 case -ENETUNREACH:
358 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400359 case -ECONNREFUSED:
360 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400361 * prompts ECONNREFUSED. */
Chuck Levera246b012005-08-11 16:25:23 -0400362 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400363 case -EAGAIN:
364 xs_nospace(task);
Chuck Levera246b012005-08-11 16:25:23 -0400365 break;
366 default:
Chuck Lever262965f2005-08-11 16:25:56 -0400367 dprintk("RPC: sendmsg returned unrecognized error %d\n",
368 -status);
Chuck Lever9903cd12005-08-11 16:25:26 -0400369 break;
Chuck Levera246b012005-08-11 16:25:23 -0400370 }
Chuck Lever262965f2005-08-11 16:25:56 -0400371
372 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400373}
374
Chuck Lever808012f2005-08-25 16:25:49 -0700375static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
376{
377 u32 reclen = buf->len - sizeof(rpc_fraghdr);
378 rpc_fraghdr *base = buf->head[0].iov_base;
379 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
380}
381
Chuck Lever9903cd12005-08-11 16:25:26 -0400382/**
Chuck Lever262965f2005-08-11 16:25:56 -0400383 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400384 * @task: address of RPC task that manages the state of an RPC request
385 *
386 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400387 * 0: The request has been sent
388 * EAGAIN: The socket was blocked, please call again later to
389 * complete the request
390 * ENOTCONN: Caller needs to invoke connect logic then call again
391 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400392 *
393 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400394 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400395 */
Chuck Lever262965f2005-08-11 16:25:56 -0400396static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400397{
398 struct rpc_rqst *req = task->tk_rqstp;
399 struct rpc_xprt *xprt = req->rq_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 Lever262965f2005-08-11 16:25:56 -0400414 status = xs_sendpages(xprt->sock, NULL, 0, xdr,
415 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 Lever9903cd12005-08-11 16:25:26 -0400493 struct socket *sock = xprt->sock;
494 struct sock *sk = xprt->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400495
496 if (!sk)
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100497 goto clear_close_wait;
Chuck Levera246b012005-08-11 16:25:23 -0400498
Chuck Lever9903cd12005-08-11 16:25:26 -0400499 dprintk("RPC: xs_close xprt %p\n", xprt);
500
Chuck Levera246b012005-08-11 16:25:23 -0400501 write_lock_bh(&sk->sk_callback_lock);
502 xprt->inet = NULL;
503 xprt->sock = NULL;
504
Chuck Lever9903cd12005-08-11 16:25:26 -0400505 sk->sk_user_data = NULL;
506 sk->sk_data_ready = xprt->old_data_ready;
Chuck Levera246b012005-08-11 16:25:23 -0400507 sk->sk_state_change = xprt->old_state_change;
Chuck Lever9903cd12005-08-11 16:25:26 -0400508 sk->sk_write_space = xprt->old_write_space;
Chuck Levera246b012005-08-11 16:25:23 -0400509 write_unlock_bh(&sk->sk_callback_lock);
510
Chuck Lever9903cd12005-08-11 16:25:26 -0400511 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400512
513 sock_release(sock);
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100514clear_close_wait:
515 smp_mb__before_clear_bit();
516 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
517 smp_mb__after_clear_bit();
Chuck Levera246b012005-08-11 16:25:23 -0400518}
519
Chuck Lever9903cd12005-08-11 16:25:26 -0400520/**
521 * xs_destroy - prepare to shutdown a transport
522 * @xprt: doomed transport
523 *
524 */
525static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400526{
Chuck Lever9903cd12005-08-11 16:25:26 -0400527 dprintk("RPC: xs_destroy xprt %p\n", xprt);
528
Chuck Lever55aa4f52005-08-11 16:25:47 -0400529 cancel_delayed_work(&xprt->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400530 flush_scheduled_work();
531
532 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400533 xs_close(xprt);
Chuck Leveredb267a2006-08-22 20:06:18 -0400534 xs_free_peer_addresses(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400535 kfree(xprt->slot);
536}
537
Chuck Lever9903cd12005-08-11 16:25:26 -0400538static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400539{
Chuck Lever9903cd12005-08-11 16:25:26 -0400540 return (struct rpc_xprt *) sk->sk_user_data;
541}
542
543/**
544 * xs_udp_data_ready - "data ready" callback for UDP sockets
545 * @sk: socket with data to read
546 * @len: how much data to read
547 *
548 */
549static void xs_udp_data_ready(struct sock *sk, int len)
550{
551 struct rpc_task *task;
552 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400553 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400554 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400555 int err, repsize, copied;
556 u32 _xid, *xp;
557
558 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400559 dprintk("RPC: xs_udp_data_ready...\n");
560 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400561 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400562
563 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
564 goto out;
565
566 if (xprt->shutdown)
567 goto dropit;
568
569 repsize = skb->len - sizeof(struct udphdr);
570 if (repsize < 4) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400571 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400572 goto dropit;
573 }
574
575 /* Copy the XID from the skb... */
576 xp = skb_header_pointer(skb, sizeof(struct udphdr),
577 sizeof(_xid), &_xid);
578 if (xp == NULL)
579 goto dropit;
580
581 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400582 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400583 rovr = xprt_lookup_rqst(xprt, *xp);
584 if (!rovr)
585 goto out_unlock;
586 task = rovr->rq_task;
587
Chuck Levera246b012005-08-11 16:25:23 -0400588 if ((copied = rovr->rq_private_buf.buflen) > repsize)
589 copied = repsize;
590
591 /* Suck it into the iovec, verify checksum if not done by hw. */
592 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb))
593 goto out_unlock;
594
595 /* Something worked... */
596 dst_confirm(skb->dst);
597
Chuck Lever1570c1e2005-08-25 16:25:52 -0700598 xprt_adjust_cwnd(task, copied);
599 xprt_update_rtt(task);
600 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400601
602 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400603 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400604 dropit:
605 skb_free_datagram(sk, skb);
606 out:
607 read_unlock(&sk->sk_callback_lock);
608}
609
Chuck Lever9903cd12005-08-11 16:25:26 -0400610static inline size_t xs_tcp_copy_data(skb_reader_t *desc, void *p, size_t len)
Chuck Levera246b012005-08-11 16:25:23 -0400611{
612 if (len > desc->count)
613 len = desc->count;
614 if (skb_copy_bits(desc->skb, desc->offset, p, len)) {
615 dprintk("RPC: failed to copy %zu bytes from skb. %zu bytes remain\n",
616 len, desc->count);
617 return 0;
618 }
619 desc->offset += len;
620 desc->count -= len;
621 dprintk("RPC: copied %zu bytes from skb. %zu bytes remain\n",
622 len, desc->count);
623 return len;
624}
625
Chuck Lever9903cd12005-08-11 16:25:26 -0400626static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400627{
628 size_t len, used;
629 char *p;
630
631 p = ((char *) &xprt->tcp_recm) + xprt->tcp_offset;
632 len = sizeof(xprt->tcp_recm) - xprt->tcp_offset;
Chuck Lever9903cd12005-08-11 16:25:26 -0400633 used = xs_tcp_copy_data(desc, p, len);
Chuck Levera246b012005-08-11 16:25:23 -0400634 xprt->tcp_offset += used;
635 if (used != len)
636 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700637
Chuck Levera246b012005-08-11 16:25:23 -0400638 xprt->tcp_reclen = ntohl(xprt->tcp_recm);
Chuck Lever808012f2005-08-25 16:25:49 -0700639 if (xprt->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levera246b012005-08-11 16:25:23 -0400640 xprt->tcp_flags |= XPRT_LAST_FRAG;
641 else
642 xprt->tcp_flags &= ~XPRT_LAST_FRAG;
Chuck Lever808012f2005-08-25 16:25:49 -0700643 xprt->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
644
Chuck Levera246b012005-08-11 16:25:23 -0400645 xprt->tcp_flags &= ~XPRT_COPY_RECM;
646 xprt->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700647
Chuck Levera246b012005-08-11 16:25:23 -0400648 /* Sanity check of the record length */
Chuck Lever808012f2005-08-25 16:25:49 -0700649 if (unlikely(xprt->tcp_reclen < 4)) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400650 dprintk("RPC: invalid TCP record fragment length\n");
Chuck Levera246b012005-08-11 16:25:23 -0400651 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400652 return;
Chuck Levera246b012005-08-11 16:25:23 -0400653 }
654 dprintk("RPC: reading TCP record fragment of length %d\n",
655 xprt->tcp_reclen);
656}
657
Chuck Lever9903cd12005-08-11 16:25:26 -0400658static void xs_tcp_check_recm(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400659{
660 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u, tcp_flags = %lx\n",
661 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen, xprt->tcp_flags);
662 if (xprt->tcp_offset == xprt->tcp_reclen) {
663 xprt->tcp_flags |= XPRT_COPY_RECM;
664 xprt->tcp_offset = 0;
665 if (xprt->tcp_flags & XPRT_LAST_FRAG) {
666 xprt->tcp_flags &= ~XPRT_COPY_DATA;
667 xprt->tcp_flags |= XPRT_COPY_XID;
668 xprt->tcp_copied = 0;
669 }
670 }
671}
672
Chuck Lever9903cd12005-08-11 16:25:26 -0400673static inline void xs_tcp_read_xid(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400674{
675 size_t len, used;
676 char *p;
677
678 len = sizeof(xprt->tcp_xid) - xprt->tcp_offset;
679 dprintk("RPC: reading XID (%Zu bytes)\n", len);
680 p = ((char *) &xprt->tcp_xid) + xprt->tcp_offset;
Chuck Lever9903cd12005-08-11 16:25:26 -0400681 used = xs_tcp_copy_data(desc, p, len);
Chuck Levera246b012005-08-11 16:25:23 -0400682 xprt->tcp_offset += used;
683 if (used != len)
684 return;
685 xprt->tcp_flags &= ~XPRT_COPY_XID;
686 xprt->tcp_flags |= XPRT_COPY_DATA;
687 xprt->tcp_copied = 4;
688 dprintk("RPC: reading reply for XID %08x\n",
689 ntohl(xprt->tcp_xid));
Chuck Lever9903cd12005-08-11 16:25:26 -0400690 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400691}
692
Chuck Lever9903cd12005-08-11 16:25:26 -0400693static inline void xs_tcp_read_request(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400694{
695 struct rpc_rqst *req;
696 struct xdr_buf *rcvbuf;
697 size_t len;
698 ssize_t r;
699
700 /* Find and lock the request corresponding to this xid */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400701 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400702 req = xprt_lookup_rqst(xprt, xprt->tcp_xid);
703 if (!req) {
704 xprt->tcp_flags &= ~XPRT_COPY_DATA;
705 dprintk("RPC: XID %08x request not found!\n",
706 ntohl(xprt->tcp_xid));
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400707 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400708 return;
709 }
710
711 rcvbuf = &req->rq_private_buf;
712 len = desc->count;
713 if (len > xprt->tcp_reclen - xprt->tcp_offset) {
714 skb_reader_t my_desc;
715
716 len = xprt->tcp_reclen - xprt->tcp_offset;
717 memcpy(&my_desc, desc, sizeof(my_desc));
718 my_desc.count = len;
719 r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied,
Chuck Lever9903cd12005-08-11 16:25:26 -0400720 &my_desc, xs_tcp_copy_data);
Chuck Levera246b012005-08-11 16:25:23 -0400721 desc->count -= r;
722 desc->offset += r;
723 } else
724 r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied,
Chuck Lever9903cd12005-08-11 16:25:26 -0400725 desc, xs_tcp_copy_data);
Chuck Levera246b012005-08-11 16:25:23 -0400726
727 if (r > 0) {
728 xprt->tcp_copied += r;
729 xprt->tcp_offset += r;
730 }
731 if (r != len) {
732 /* Error when copying to the receive buffer,
733 * usually because we weren't able to allocate
734 * additional buffer pages. All we can do now
735 * is turn off XPRT_COPY_DATA, so the request
736 * will not receive any additional updates,
737 * and time out.
738 * Any remaining data from this record will
739 * be discarded.
740 */
741 xprt->tcp_flags &= ~XPRT_COPY_DATA;
742 dprintk("RPC: XID %08x truncated request\n",
743 ntohl(xprt->tcp_xid));
744 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
745 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen);
746 goto out;
747 }
748
749 dprintk("RPC: XID %08x read %Zd bytes\n",
750 ntohl(xprt->tcp_xid), r);
751 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
752 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen);
753
754 if (xprt->tcp_copied == req->rq_private_buf.buflen)
755 xprt->tcp_flags &= ~XPRT_COPY_DATA;
756 else if (xprt->tcp_offset == xprt->tcp_reclen) {
757 if (xprt->tcp_flags & XPRT_LAST_FRAG)
758 xprt->tcp_flags &= ~XPRT_COPY_DATA;
759 }
760
761out:
Chuck Lever1570c1e2005-08-25 16:25:52 -0700762 if (!(xprt->tcp_flags & XPRT_COPY_DATA))
763 xprt_complete_rqst(req->rq_task, xprt->tcp_copied);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400764 spin_unlock(&xprt->transport_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400765 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400766}
767
Chuck Lever9903cd12005-08-11 16:25:26 -0400768static inline void xs_tcp_read_discard(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400769{
770 size_t len;
771
772 len = xprt->tcp_reclen - xprt->tcp_offset;
773 if (len > desc->count)
774 len = desc->count;
775 desc->count -= len;
776 desc->offset += len;
777 xprt->tcp_offset += len;
778 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever9903cd12005-08-11 16:25:26 -0400779 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400780}
781
Chuck Lever9903cd12005-08-11 16:25:26 -0400782static 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 -0400783{
784 struct rpc_xprt *xprt = rd_desc->arg.data;
785 skb_reader_t desc = {
786 .skb = skb,
787 .offset = offset,
788 .count = len,
789 .csum = 0
Chuck Lever9903cd12005-08-11 16:25:26 -0400790 };
Chuck Levera246b012005-08-11 16:25:23 -0400791
Chuck Lever9903cd12005-08-11 16:25:26 -0400792 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -0400793 do {
794 /* Read in a new fragment marker if necessary */
795 /* Can we ever really expect to get completely empty fragments? */
796 if (xprt->tcp_flags & XPRT_COPY_RECM) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400797 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400798 continue;
799 }
800 /* Read in the xid if necessary */
801 if (xprt->tcp_flags & XPRT_COPY_XID) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400802 xs_tcp_read_xid(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400803 continue;
804 }
805 /* Read in the request data */
806 if (xprt->tcp_flags & XPRT_COPY_DATA) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400807 xs_tcp_read_request(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400808 continue;
809 }
810 /* Skip over any trailing bytes on short reads */
Chuck Lever9903cd12005-08-11 16:25:26 -0400811 xs_tcp_read_discard(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400812 } while (desc.count);
Chuck Lever9903cd12005-08-11 16:25:26 -0400813 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -0400814 return len - desc.count;
815}
816
Chuck Lever9903cd12005-08-11 16:25:26 -0400817/**
818 * xs_tcp_data_ready - "data ready" callback for TCP sockets
819 * @sk: socket with data to read
820 * @bytes: how much data to read
821 *
822 */
823static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -0400824{
825 struct rpc_xprt *xprt;
826 read_descriptor_t rd_desc;
827
828 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400829 dprintk("RPC: xs_tcp_data_ready...\n");
830 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400831 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400832 if (xprt->shutdown)
833 goto out;
834
Chuck Lever9903cd12005-08-11 16:25:26 -0400835 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -0400836 rd_desc.arg.data = xprt;
837 rd_desc.count = 65536;
Chuck Lever9903cd12005-08-11 16:25:26 -0400838 tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
Chuck Levera246b012005-08-11 16:25:23 -0400839out:
840 read_unlock(&sk->sk_callback_lock);
841}
842
Chuck Lever9903cd12005-08-11 16:25:26 -0400843/**
844 * xs_tcp_state_change - callback to handle TCP socket state changes
845 * @sk: socket whose state has changed
846 *
847 */
848static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400849{
Chuck Lever9903cd12005-08-11 16:25:26 -0400850 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400851
852 read_lock(&sk->sk_callback_lock);
853 if (!(xprt = xprt_from_sock(sk)))
854 goto out;
Chuck Lever9903cd12005-08-11 16:25:26 -0400855 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400856 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
857 sk->sk_state, xprt_connected(xprt),
858 sock_flag(sk, SOCK_DEAD),
859 sock_flag(sk, SOCK_ZAPPED));
860
861 switch (sk->sk_state) {
862 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400863 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400864 if (!xprt_test_and_set_connected(xprt)) {
865 /* Reset TCP record info */
866 xprt->tcp_offset = 0;
867 xprt->tcp_reclen = 0;
868 xprt->tcp_copied = 0;
869 xprt->tcp_flags = XPRT_COPY_RECM | XPRT_COPY_XID;
Chuck Lever03bf4b72005-08-25 16:25:55 -0700870 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever44fbac22005-08-11 16:25:44 -0400871 xprt_wake_pending_tasks(xprt, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400872 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400873 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400874 break;
875 case TCP_SYN_SENT:
876 case TCP_SYN_RECV:
877 break;
Trond Myklebust632e3bd2006-01-03 09:55:55 +0100878 case TCP_CLOSE_WAIT:
879 /* Try to schedule an autoclose RPC calls */
880 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
881 if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
882 schedule_work(&xprt->task_cleanup);
Chuck Levera246b012005-08-11 16:25:23 -0400883 default:
884 xprt_disconnect(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400885 }
886 out:
887 read_unlock(&sk->sk_callback_lock);
888}
889
Chuck Lever9903cd12005-08-11 16:25:26 -0400890/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400891 * xs_udp_write_space - callback invoked when socket buffer space
892 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -0400893 * @sk: socket whose state has changed
894 *
Chuck Levera246b012005-08-11 16:25:23 -0400895 * Called when more output buffer space is available for this socket.
896 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400897 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -0400898 * with a bunch of small requests.
899 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400900static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400901{
Chuck Levera246b012005-08-11 16:25:23 -0400902 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400903
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400904 /* from net/core/sock.c:sock_def_write_space */
905 if (sock_writeable(sk)) {
906 struct socket *sock;
907 struct rpc_xprt *xprt;
908
909 if (unlikely(!(sock = sk->sk_socket)))
Chuck Levera246b012005-08-11 16:25:23 -0400910 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400911 if (unlikely(!(xprt = xprt_from_sock(sk))))
Chuck Levera246b012005-08-11 16:25:23 -0400912 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400913 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
914 goto out;
915
916 xprt_write_space(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400917 }
918
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400919 out:
920 read_unlock(&sk->sk_callback_lock);
921}
Chuck Levera246b012005-08-11 16:25:23 -0400922
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400923/**
924 * xs_tcp_write_space - callback invoked when socket buffer space
925 * becomes available
926 * @sk: socket whose state has changed
927 *
928 * Called when more output buffer space is available for this socket.
929 * We try not to wake our writers until they can make "significant"
930 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
931 * with a bunch of small requests.
932 */
933static void xs_tcp_write_space(struct sock *sk)
934{
935 read_lock(&sk->sk_callback_lock);
936
937 /* from net/core/stream.c:sk_stream_write_space */
938 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
939 struct socket *sock;
940 struct rpc_xprt *xprt;
941
942 if (unlikely(!(sock = sk->sk_socket)))
943 goto out;
944 if (unlikely(!(xprt = xprt_from_sock(sk))))
945 goto out;
946 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
947 goto out;
948
949 xprt_write_space(xprt);
950 }
951
952 out:
Chuck Levera246b012005-08-11 16:25:23 -0400953 read_unlock(&sk->sk_callback_lock);
954}
955
Chuck Lever470056c2005-08-25 16:25:56 -0700956static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400957{
958 struct sock *sk = xprt->inet;
959
Chuck Levera246b012005-08-11 16:25:23 -0400960 if (xprt->rcvsize) {
961 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
962 sk->sk_rcvbuf = xprt->rcvsize * xprt->max_reqs * 2;
963 }
964 if (xprt->sndsize) {
965 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
966 sk->sk_sndbuf = xprt->sndsize * xprt->max_reqs * 2;
967 sk->sk_write_space(sk);
968 }
969}
970
Chuck Lever43118c22005-08-25 16:25:49 -0700971/**
Chuck Lever470056c2005-08-25 16:25:56 -0700972 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -0700973 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -0700974 * @sndsize: requested size of send buffer, in bytes
975 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -0700976 *
Chuck Lever470056c2005-08-25 16:25:56 -0700977 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -0700978 */
Chuck Lever470056c2005-08-25 16:25:56 -0700979static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -0700980{
Chuck Lever470056c2005-08-25 16:25:56 -0700981 xprt->sndsize = 0;
982 if (sndsize)
983 xprt->sndsize = sndsize + 1024;
984 xprt->rcvsize = 0;
985 if (rcvsize)
986 xprt->rcvsize = rcvsize + 1024;
987
988 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -0700989}
990
Chuck Lever46c0ee82005-08-25 16:25:52 -0700991/**
992 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
993 * @task: task that timed out
994 *
995 * Adjust the congestion window after a retransmit timeout has occurred.
996 */
997static void xs_udp_timer(struct rpc_task *task)
998{
999 xprt_adjust_cwnd(task, -ETIMEDOUT);
1000}
1001
Chuck Leverb85d8802006-05-25 01:40:49 -04001002static unsigned short xs_get_random_port(void)
1003{
1004 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1005 unsigned short rand = (unsigned short) net_random() % range;
1006 return rand + xprt_min_resvport;
1007}
1008
Chuck Lever92200412006-01-03 09:55:51 +01001009/**
Chuck Leveredb267a2006-08-22 20:06:18 -04001010 * xs_print_peer_address - format an IPv4 address for printing
1011 * @xprt: generic transport
1012 * @format: flags field indicating which parts of the address to render
1013 */
1014static char *xs_print_peer_address(struct rpc_xprt *xprt, enum rpc_display_format_t format)
1015{
1016 if (xprt->address_strings[format] != NULL)
1017 return xprt->address_strings[format];
1018 else
1019 return "unprintable";
1020}
1021
1022/**
Chuck Lever92200412006-01-03 09:55:51 +01001023 * xs_set_port - reset the port number in the remote endpoint address
1024 * @xprt: generic transport
1025 * @port: new port number
1026 *
1027 */
1028static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1029{
Chuck Leverc4efcb12006-08-22 20:06:19 -04001030 struct sockaddr_in *sap = (struct sockaddr_in *) &xprt->addr;
1031
Chuck Lever92200412006-01-03 09:55:51 +01001032 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
Chuck Leverc4efcb12006-08-22 20:06:19 -04001033
1034 sap->sin_port = htons(port);
Chuck Lever92200412006-01-03 09:55:51 +01001035}
1036
Chuck Lever9903cd12005-08-11 16:25:26 -04001037static int xs_bindresvport(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -04001038{
1039 struct sockaddr_in myaddr = {
1040 .sin_family = AF_INET,
1041 };
Chuck Lever529b33c2005-08-25 16:25:54 -07001042 int err;
1043 unsigned short port = xprt->port;
Chuck Levera246b012005-08-11 16:25:23 -04001044
Chuck Levera246b012005-08-11 16:25:23 -04001045 do {
1046 myaddr.sin_port = htons(port);
1047 err = sock->ops->bind(sock, (struct sockaddr *) &myaddr,
1048 sizeof(myaddr));
1049 if (err == 0) {
1050 xprt->port = port;
Chuck Lever9903cd12005-08-11 16:25:26 -04001051 dprintk("RPC: xs_bindresvport bound to port %u\n",
1052 port);
Chuck Levera246b012005-08-11 16:25:23 -04001053 return 0;
1054 }
Chuck Lever529b33c2005-08-25 16:25:54 -07001055 if (port <= xprt_min_resvport)
1056 port = xprt_max_resvport;
1057 else
1058 port--;
Chuck Levera246b012005-08-11 16:25:23 -04001059 } while (err == -EADDRINUSE && port != xprt->port);
1060
Chuck Lever9903cd12005-08-11 16:25:26 -04001061 dprintk("RPC: can't bind to reserved port (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001062 return err;
1063}
1064
Chuck Lever9903cd12005-08-11 16:25:26 -04001065/**
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001066 * xs_udp_connect_worker - set up a UDP socket
Chuck Lever9903cd12005-08-11 16:25:26 -04001067 * @args: RPC transport to connect
1068 *
1069 * Invoked by a work queue tasklet.
Chuck Levera246b012005-08-11 16:25:23 -04001070 */
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001071static void xs_udp_connect_worker(void *args)
Chuck Levera246b012005-08-11 16:25:23 -04001072{
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001073 struct rpc_xprt *xprt = (struct rpc_xprt *) args;
Chuck Levera246b012005-08-11 16:25:23 -04001074 struct socket *sock = xprt->sock;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001075 int err, status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -04001076
Chuck Leverec739ef2006-08-22 20:06:15 -04001077 if (xprt->shutdown || !xprt_bound(xprt))
Chuck Levera246b012005-08-11 16:25:23 -04001078 goto out;
1079
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001080 /* Start by resetting any existing state */
Chuck Lever9903cd12005-08-11 16:25:26 -04001081 xs_close(xprt);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001082
1083 if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
1084 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -04001085 goto out;
1086 }
Chuck Levera246b012005-08-11 16:25:23 -04001087
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001088 if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) {
1089 sock_release(sock);
1090 goto out;
1091 }
1092
Chuck Leveredb267a2006-08-22 20:06:18 -04001093 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1094 xprt, xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1095
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001096 if (!xprt->inet) {
1097 struct sock *sk = sock->sk;
1098
1099 write_lock_bh(&sk->sk_callback_lock);
1100
1101 sk->sk_user_data = xprt;
1102 xprt->old_data_ready = sk->sk_data_ready;
1103 xprt->old_state_change = sk->sk_state_change;
1104 xprt->old_write_space = sk->sk_write_space;
1105 sk->sk_data_ready = xs_udp_data_ready;
1106 sk->sk_write_space = xs_udp_write_space;
1107 sk->sk_no_check = UDP_CSUM_NORCV;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001108 sk->sk_allocation = GFP_ATOMIC;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001109
1110 xprt_set_connected(xprt);
1111
1112 /* Reset to new socket */
1113 xprt->sock = sock;
1114 xprt->inet = sk;
1115
1116 write_unlock_bh(&sk->sk_callback_lock);
1117 }
Chuck Lever470056c2005-08-25 16:25:56 -07001118 xs_udp_do_set_buffer_size(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001119 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001120out:
1121 xprt_wake_pending_tasks(xprt, status);
1122 xprt_clear_connecting(xprt);
1123}
1124
Chuck Lever3167e122005-08-25 16:25:55 -07001125/*
1126 * We need to preserve the port number so the reply cache on the server can
1127 * find our cached RPC replies when we get around to reconnecting.
1128 */
1129static void xs_tcp_reuse_connection(struct rpc_xprt *xprt)
1130{
1131 int result;
1132 struct socket *sock = xprt->sock;
1133 struct sockaddr any;
1134
1135 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
1136
1137 /*
1138 * Disconnect the transport socket by doing a connect operation
1139 * with AF_UNSPEC. This should return immediately...
1140 */
1141 memset(&any, 0, sizeof(any));
1142 any.sa_family = AF_UNSPEC;
1143 result = sock->ops->connect(sock, &any, sizeof(any), 0);
1144 if (result)
1145 dprintk("RPC: AF_UNSPEC connect return code %d\n",
1146 result);
1147}
1148
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001149/**
1150 * xs_tcp_connect_worker - connect a TCP socket to a remote endpoint
1151 * @args: RPC transport to connect
1152 *
1153 * Invoked by a work queue tasklet.
1154 */
1155static void xs_tcp_connect_worker(void *args)
1156{
1157 struct rpc_xprt *xprt = (struct rpc_xprt *)args;
1158 struct socket *sock = xprt->sock;
1159 int err, status = -EIO;
1160
Chuck Leverec739ef2006-08-22 20:06:15 -04001161 if (xprt->shutdown || !xprt_bound(xprt))
Chuck Levera246b012005-08-11 16:25:23 -04001162 goto out;
1163
Chuck Lever3167e122005-08-25 16:25:55 -07001164 if (!xprt->sock) {
1165 /* start from scratch */
1166 if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
1167 dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
1168 goto out;
1169 }
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001170
Chuck Lever3167e122005-08-25 16:25:55 -07001171 if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) {
1172 sock_release(sock);
1173 goto out;
1174 }
1175 } else
1176 /* "close" the socket, preserving the local port */
1177 xs_tcp_reuse_connection(xprt);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001178
Chuck Leveredb267a2006-08-22 20:06:18 -04001179 dprintk("RPC: worker connecting xprt %p to address: %s\n",
1180 xprt, xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1181
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001182 if (!xprt->inet) {
1183 struct sock *sk = sock->sk;
1184
1185 write_lock_bh(&sk->sk_callback_lock);
1186
1187 sk->sk_user_data = xprt;
1188 xprt->old_data_ready = sk->sk_data_ready;
1189 xprt->old_state_change = sk->sk_state_change;
1190 xprt->old_write_space = sk->sk_write_space;
1191 sk->sk_data_ready = xs_tcp_data_ready;
1192 sk->sk_state_change = xs_tcp_state_change;
1193 sk->sk_write_space = xs_tcp_write_space;
Trond Myklebustb079fa7b2005-12-13 16:13:52 -05001194 sk->sk_allocation = GFP_ATOMIC;
Chuck Lever3167e122005-08-25 16:25:55 -07001195
1196 /* socket options */
1197 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1198 sock_reset_flag(sk, SOCK_LINGER);
1199 tcp_sk(sk)->linger2 = 0;
1200 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001201
1202 xprt_clear_connected(xprt);
1203
1204 /* Reset to new socket */
1205 xprt->sock = sock;
1206 xprt->inet = sk;
1207
1208 write_unlock_bh(&sk->sk_callback_lock);
1209 }
1210
1211 /* Tell the socket layer to start connecting... */
Chuck Lever262ca072006-03-20 13:44:16 -05001212 xprt->stat.connect_count++;
1213 xprt->stat.connect_start = jiffies;
Chuck Levera246b012005-08-11 16:25:23 -04001214 status = sock->ops->connect(sock, (struct sockaddr *) &xprt->addr,
Chuck Leverc4efcb12006-08-22 20:06:19 -04001215 xprt->addrlen, O_NONBLOCK);
Chuck Levera246b012005-08-11 16:25:23 -04001216 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1217 xprt, -status, xprt_connected(xprt), sock->sk->sk_state);
1218 if (status < 0) {
1219 switch (status) {
1220 case -EINPROGRESS:
1221 case -EALREADY:
1222 goto out_clear;
Chuck Lever3167e122005-08-25 16:25:55 -07001223 case -ECONNREFUSED:
1224 case -ECONNRESET:
1225 /* retry with existing socket, after a delay */
1226 break;
1227 default:
1228 /* get rid of existing socket, and retry */
1229 xs_close(xprt);
1230 break;
Chuck Levera246b012005-08-11 16:25:23 -04001231 }
1232 }
1233out:
Chuck Lever44fbac22005-08-11 16:25:44 -04001234 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001235out_clear:
Chuck Lever2226feb2005-08-11 16:25:38 -04001236 xprt_clear_connecting(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001237}
1238
Chuck Lever9903cd12005-08-11 16:25:26 -04001239/**
1240 * xs_connect - connect a socket to a remote endpoint
1241 * @task: address of RPC task that manages state of connect request
1242 *
1243 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07001244 *
1245 * UDP socket connects are synchronous, but we use a work queue anyway
1246 * to guarantee that even unprivileged user processes can set up a
1247 * socket on a privileged port.
1248 *
1249 * If a UDP socket connect fails, the delay behavior here prevents
1250 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04001251 */
1252static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04001253{
1254 struct rpc_xprt *xprt = task->tk_xprt;
1255
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001256 if (xprt_test_and_set_connecting(xprt))
1257 return;
1258
1259 if (xprt->sock != NULL) {
Chuck Lever03bf4b72005-08-25 16:25:55 -07001260 dprintk("RPC: xs_connect delayed xprt %p for %lu seconds\n",
1261 xprt, xprt->reestablish_timeout / HZ);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001262 schedule_delayed_work(&xprt->connect_worker,
Chuck Lever03bf4b72005-08-25 16:25:55 -07001263 xprt->reestablish_timeout);
1264 xprt->reestablish_timeout <<= 1;
1265 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
1266 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001267 } else {
1268 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
1269 schedule_work(&xprt->connect_worker);
1270
1271 /* flush_scheduled_work can sleep... */
1272 if (!RPC_IS_ASYNC(task))
1273 flush_scheduled_work();
Chuck Levera246b012005-08-11 16:25:23 -04001274 }
1275}
1276
Chuck Lever262ca072006-03-20 13:44:16 -05001277/**
1278 * xs_udp_print_stats - display UDP socket-specifc stats
1279 * @xprt: rpc_xprt struct containing statistics
1280 * @seq: output file
1281 *
1282 */
1283static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1284{
1285 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
1286 xprt->port,
1287 xprt->stat.bind_count,
1288 xprt->stat.sends,
1289 xprt->stat.recvs,
1290 xprt->stat.bad_xids,
1291 xprt->stat.req_u,
1292 xprt->stat.bklog_u);
1293}
1294
1295/**
1296 * xs_tcp_print_stats - display TCP socket-specifc stats
1297 * @xprt: rpc_xprt struct containing statistics
1298 * @seq: output file
1299 *
1300 */
1301static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1302{
1303 long idle_time = 0;
1304
1305 if (xprt_connected(xprt))
1306 idle_time = (long)(jiffies - xprt->last_used) / HZ;
1307
1308 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
1309 xprt->port,
1310 xprt->stat.bind_count,
1311 xprt->stat.connect_count,
1312 xprt->stat.connect_time,
1313 idle_time,
1314 xprt->stat.sends,
1315 xprt->stat.recvs,
1316 xprt->stat.bad_xids,
1317 xprt->stat.req_u,
1318 xprt->stat.bklog_u);
1319}
1320
Chuck Lever262965f2005-08-11 16:25:56 -04001321static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07001322 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Leveredb267a2006-08-22 20:06:18 -04001323 .print_addr = xs_print_peer_address,
Chuck Lever12a80462005-08-25 16:25:51 -07001324 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07001325 .release_xprt = xprt_release_xprt_cong,
Chuck Leverbbf7c1d2006-08-22 20:06:16 -04001326 .rpcbind = rpc_getport,
Chuck Lever92200412006-01-03 09:55:51 +01001327 .set_port = xs_set_port,
Chuck Lever9903cd12005-08-11 16:25:26 -04001328 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01001329 .buf_alloc = rpc_malloc,
1330 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04001331 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001332 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07001333 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07001334 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04001335 .close = xs_close,
1336 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05001337 .print_stats = xs_udp_print_stats,
Chuck Lever262965f2005-08-11 16:25:56 -04001338};
1339
1340static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Leveredb267a2006-08-22 20:06:18 -04001341 .print_addr = xs_print_peer_address,
Chuck Lever12a80462005-08-25 16:25:51 -07001342 .reserve_xprt = xprt_reserve_xprt,
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001343 .release_xprt = xs_tcp_release_xprt,
Chuck Leverbbf7c1d2006-08-22 20:06:16 -04001344 .rpcbind = rpc_getport,
Chuck Lever92200412006-01-03 09:55:51 +01001345 .set_port = xs_set_port,
Chuck Lever262965f2005-08-11 16:25:56 -04001346 .connect = xs_connect,
Chuck Lever02107142006-01-03 09:55:49 +01001347 .buf_alloc = rpc_malloc,
1348 .buf_free = rpc_free,
Chuck Lever262965f2005-08-11 16:25:56 -04001349 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001350 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Chuck Lever9903cd12005-08-11 16:25:26 -04001351 .close = xs_close,
1352 .destroy = xs_destroy,
Chuck Lever262ca072006-03-20 13:44:16 -05001353 .print_stats = xs_tcp_print_stats,
Chuck Levera246b012005-08-11 16:25:23 -04001354};
1355
Chuck Lever9903cd12005-08-11 16:25:26 -04001356/**
1357 * xs_setup_udp - Set up transport to use a UDP socket
1358 * @xprt: transport to set up
1359 * @to: timeout parameters
1360 *
1361 */
Chuck Levera246b012005-08-11 16:25:23 -04001362int xs_setup_udp(struct rpc_xprt *xprt, struct rpc_timeout *to)
1363{
1364 size_t slot_table_size;
Chuck Leverc4efcb12006-08-22 20:06:19 -04001365 struct sockaddr_in *addr = (struct sockaddr_in *) &xprt->addr;
Chuck Levera246b012005-08-11 16:25:23 -04001366
Chuck Levera246b012005-08-11 16:25:23 -04001367 xprt->max_reqs = xprt_udp_slot_table_entries;
1368 slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]);
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001369 xprt->slot = kzalloc(slot_table_size, GFP_KERNEL);
Chuck Levera246b012005-08-11 16:25:23 -04001370 if (xprt->slot == NULL)
1371 return -ENOMEM;
Chuck Levera246b012005-08-11 16:25:23 -04001372
Chuck Leverc4efcb12006-08-22 20:06:19 -04001373 if (ntohs(addr->sin_port != 0))
Chuck Leverec739ef2006-08-22 20:06:15 -04001374 xprt_set_bound(xprt);
Chuck Leverb85d8802006-05-25 01:40:49 -04001375 xprt->port = xs_get_random_port();
Chuck Leverec739ef2006-08-22 20:06:15 -04001376
1377 xprt->prot = IPPROTO_UDP;
Chuck Lever808012f2005-08-25 16:25:49 -07001378 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001379 xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0;
1380 /* XXX: header size can vary due to auth type, IPv6, etc. */
1381 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
1382
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001383 INIT_WORK(&xprt->connect_worker, xs_udp_connect_worker, xprt);
Chuck Lever03bf4b72005-08-25 16:25:55 -07001384 xprt->bind_timeout = XS_BIND_TO;
1385 xprt->connect_timeout = XS_UDP_CONN_TO;
1386 xprt->reestablish_timeout = XS_UDP_REEST_TO;
1387 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04001388
Chuck Lever262965f2005-08-11 16:25:56 -04001389 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001390
1391 if (to)
1392 xprt->timeout = *to;
1393 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001394 xprt_set_timeout(&xprt->timeout, 5, 5 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001395
Chuck Leveredb267a2006-08-22 20:06:18 -04001396 xs_format_peer_addresses(xprt);
1397 dprintk("RPC: set up transport to address %s\n",
1398 xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1399
Chuck Levera246b012005-08-11 16:25:23 -04001400 return 0;
1401}
1402
Chuck Lever9903cd12005-08-11 16:25:26 -04001403/**
1404 * xs_setup_tcp - Set up transport to use a TCP socket
1405 * @xprt: transport to set up
1406 * @to: timeout parameters
1407 *
1408 */
Chuck Levera246b012005-08-11 16:25:23 -04001409int xs_setup_tcp(struct rpc_xprt *xprt, struct rpc_timeout *to)
1410{
1411 size_t slot_table_size;
Chuck Leverc4efcb12006-08-22 20:06:19 -04001412 struct sockaddr_in *addr = (struct sockaddr_in *) &xprt->addr;
Chuck Levera246b012005-08-11 16:25:23 -04001413
Chuck Levera246b012005-08-11 16:25:23 -04001414 xprt->max_reqs = xprt_tcp_slot_table_entries;
1415 slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]);
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001416 xprt->slot = kzalloc(slot_table_size, GFP_KERNEL);
Chuck Levera246b012005-08-11 16:25:23 -04001417 if (xprt->slot == NULL)
1418 return -ENOMEM;
Chuck Levera246b012005-08-11 16:25:23 -04001419
Chuck Leverc4efcb12006-08-22 20:06:19 -04001420 if (ntohs(addr->sin_port) != 0)
Chuck Leverec739ef2006-08-22 20:06:15 -04001421 xprt_set_bound(xprt);
Chuck Leverb85d8802006-05-25 01:40:49 -04001422 xprt->port = xs_get_random_port();
Chuck Leverec739ef2006-08-22 20:06:15 -04001423
1424 xprt->prot = IPPROTO_TCP;
Chuck Lever808012f2005-08-25 16:25:49 -07001425 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Levera246b012005-08-11 16:25:23 -04001426 xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0;
Chuck Lever808012f2005-08-25 16:25:49 -07001427 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04001428
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001429 INIT_WORK(&xprt->connect_worker, xs_tcp_connect_worker, xprt);
Chuck Lever03bf4b72005-08-25 16:25:55 -07001430 xprt->bind_timeout = XS_BIND_TO;
1431 xprt->connect_timeout = XS_TCP_CONN_TO;
1432 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1433 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04001434
Chuck Lever262965f2005-08-11 16:25:56 -04001435 xprt->ops = &xs_tcp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001436
1437 if (to)
1438 xprt->timeout = *to;
1439 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001440 xprt_set_timeout(&xprt->timeout, 2, 60 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001441
Chuck Leveredb267a2006-08-22 20:06:18 -04001442 xs_format_peer_addresses(xprt);
1443 dprintk("RPC: set up transport to address %s\n",
1444 xs_print_peer_address(xprt, RPC_DISPLAY_ALL));
1445
Chuck Levera246b012005-08-11 16:25:23 -04001446 return 0;
1447}