blob: f91529787b9bc885bffc66ed8a26a7831b53a844 [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>
31#include <linux/file.h>
32
33#include <net/sock.h>
34#include <net/checksum.h>
35#include <net/udp.h>
36#include <net/tcp.h>
37
Chuck Lever9903cd12005-08-11 16:25:26 -040038/*
39 * Maximum port number to use when requesting a reserved port.
40 */
41#define XS_MAX_RESVPORT (800U)
42
Chuck Levera246b012005-08-11 16:25:23 -040043#ifdef RPC_DEBUG
44# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -040045# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -040046#endif
47
Chuck Levera246b012005-08-11 16:25:23 -040048#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -040049static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -040050{
Chuck Lever9903cd12005-08-11 16:25:26 -040051 u8 *buf = (u8 *) packet;
52 int j;
Chuck Levera246b012005-08-11 16:25:23 -040053
54 dprintk("RPC: %s\n", msg);
55 for (j = 0; j < count && j < 128; j += 4) {
56 if (!(j & 31)) {
57 if (j)
58 dprintk("\n");
59 dprintk("0x%04x ", j);
60 }
61 dprintk("%02x%02x%02x%02x ",
62 buf[j], buf[j+1], buf[j+2], buf[j+3]);
63 }
64 dprintk("\n");
65}
66#else
Chuck Lever9903cd12005-08-11 16:25:26 -040067static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -040068{
69 /* NOP */
70}
71#endif
72
Chuck Leverb4b5cc82005-08-11 16:25:29 -040073#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
74
75static inline int xs_send_head(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, unsigned int len)
76{
77 struct kvec iov = {
78 .iov_base = xdr->head[0].iov_base + base,
79 .iov_len = len - base,
80 };
81 struct msghdr msg = {
82 .msg_name = addr,
83 .msg_namelen = addrlen,
84 .msg_flags = XS_SENDMSG_FLAGS,
85 };
86
87 if (xdr->len > len)
88 msg.msg_flags |= MSG_MORE;
89
90 if (likely(iov.iov_len))
91 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
92 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
93}
94
95static int xs_send_tail(struct socket *sock, struct xdr_buf *xdr, unsigned int base, unsigned int len)
96{
97 struct kvec iov = {
98 .iov_base = xdr->tail[0].iov_base + base,
99 .iov_len = len - base,
100 };
101 struct msghdr msg = {
102 .msg_flags = XS_SENDMSG_FLAGS,
103 };
104
105 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
106}
107
Chuck Lever9903cd12005-08-11 16:25:26 -0400108/**
109 * xs_sendpages - write pages directly to a socket
110 * @sock: socket to send on
111 * @addr: UDP only -- address of destination
112 * @addrlen: UDP only -- length of destination address
113 * @xdr: buffer containing this request
114 * @base: starting position in the buffer
115 *
Chuck Levera246b012005-08-11 16:25:23 -0400116 */
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400117static 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 -0400118{
119 struct page **ppage = xdr->pages;
120 unsigned int len, pglen = xdr->page_len;
121 int err, ret = 0;
122 ssize_t (*sendpage)(struct socket *, struct page *, int, size_t, int);
123
124 len = xdr->head[0].iov_len;
125 if (base < len || (addr != NULL && base == 0)) {
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400126 err = xs_send_head(sock, addr, addrlen, xdr, base, len);
Chuck Levera246b012005-08-11 16:25:23 -0400127 if (ret == 0)
128 ret = err;
129 else if (err > 0)
130 ret += err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400131 if (err != (len - base))
Chuck Levera246b012005-08-11 16:25:23 -0400132 goto out;
133 base = 0;
134 } else
135 base -= len;
136
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400137 if (unlikely(pglen == 0))
Chuck Levera246b012005-08-11 16:25:23 -0400138 goto copy_tail;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400139 if (unlikely(base >= pglen)) {
Chuck Levera246b012005-08-11 16:25:23 -0400140 base -= pglen;
141 goto copy_tail;
142 }
143 if (base || xdr->page_base) {
144 pglen -= base;
Chuck Lever9903cd12005-08-11 16:25:26 -0400145 base += xdr->page_base;
Chuck Levera246b012005-08-11 16:25:23 -0400146 ppage += base >> PAGE_CACHE_SHIFT;
147 base &= ~PAGE_CACHE_MASK;
148 }
149
150 sendpage = sock->ops->sendpage ? : sock_no_sendpage;
151 do {
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400152 int flags = XS_SENDMSG_FLAGS;
Chuck Levera246b012005-08-11 16:25:23 -0400153
154 len = PAGE_CACHE_SIZE;
155 if (base)
156 len -= base;
157 if (pglen < len)
158 len = pglen;
159
160 if (pglen != len || xdr->tail[0].iov_len != 0)
161 flags |= MSG_MORE;
162
163 /* Hmm... We might be dealing with highmem pages */
164 if (PageHighMem(*ppage))
165 sendpage = sock_no_sendpage;
166 err = sendpage(sock, *ppage, base, len, flags);
167 if (ret == 0)
168 ret = err;
169 else if (err > 0)
170 ret += err;
171 if (err != len)
172 goto out;
173 base = 0;
174 ppage++;
175 } while ((pglen -= len) != 0);
176copy_tail:
177 len = xdr->tail[0].iov_len;
178 if (base < len) {
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400179 err = xs_send_tail(sock, xdr, base, len);
Chuck Levera246b012005-08-11 16:25:23 -0400180 if (ret == 0)
181 ret = err;
182 else if (err > 0)
183 ret += err;
184 }
185out:
186 return ret;
187}
188
Chuck Lever9903cd12005-08-11 16:25:26 -0400189/**
190 * xs_sendmsg - write an RPC request to a socket
191 * @xprt: generic transport
192 * @req: the RPC request to write
193 *
Chuck Levera246b012005-08-11 16:25:23 -0400194 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400195static int xs_sendmsg(struct rpc_xprt *xprt, struct rpc_rqst *req)
Chuck Levera246b012005-08-11 16:25:23 -0400196{
Chuck Lever9903cd12005-08-11 16:25:26 -0400197 struct socket *sock = xprt->sock;
198 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Levera246b012005-08-11 16:25:23 -0400199 struct sockaddr *addr = NULL;
200 int addrlen = 0;
Chuck Lever9903cd12005-08-11 16:25:26 -0400201 unsigned int skip;
202 int result;
Chuck Levera246b012005-08-11 16:25:23 -0400203
204 if (!sock)
205 return -ENOTCONN;
206
Chuck Lever9903cd12005-08-11 16:25:26 -0400207 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400208 req->rq_svec->iov_base,
209 req->rq_svec->iov_len);
210
211 /* For UDP, we need to provide an address */
212 if (!xprt->stream) {
213 addr = (struct sockaddr *) &xprt->addr;
214 addrlen = sizeof(xprt->addr);
215 }
Chuck Lever9903cd12005-08-11 16:25:26 -0400216 /* Don't repeat bytes */
Chuck Levera246b012005-08-11 16:25:23 -0400217 skip = req->rq_bytes_sent;
218
219 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400220 result = xs_sendpages(sock, addr, addrlen, xdr, skip);
Chuck Levera246b012005-08-11 16:25:23 -0400221
Chuck Lever9903cd12005-08-11 16:25:26 -0400222 dprintk("RPC: xs_sendmsg(%d) = %d\n", xdr->len - skip, result);
Chuck Levera246b012005-08-11 16:25:23 -0400223
224 if (result >= 0)
225 return result;
226
227 switch (result) {
228 case -ECONNREFUSED:
229 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400230 * prompts ECONNREFUSED. */
Chuck Levera246b012005-08-11 16:25:23 -0400231 case -EAGAIN:
232 break;
233 case -ECONNRESET:
234 case -ENOTCONN:
235 case -EPIPE:
236 /* connection broken */
237 if (xprt->stream)
238 result = -ENOTCONN;
239 break;
240 default:
Chuck Lever9903cd12005-08-11 16:25:26 -0400241 break;
Chuck Levera246b012005-08-11 16:25:23 -0400242 }
243 return result;
244}
245
Chuck Lever9903cd12005-08-11 16:25:26 -0400246/**
247 * xs_send_request - write an RPC request to a socket
248 * @task: address of RPC task that manages the state of an RPC request
249 *
250 * Return values:
251 * 0: The request has been sent
252 * EAGAIN: The socket was blocked, please call again later to
253 * complete the request
254 * other: Some other error occured, the request was not sent
255 *
256 * XXX: In the case of soft timeouts, should we eventually give up
257 * if the socket is not able to make progress?
258 */
259static int xs_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400260{
261 struct rpc_rqst *req = task->tk_rqstp;
262 struct rpc_xprt *xprt = req->rq_xprt;
263 int status, retry = 0;
264
265 /* set up everything as needed. */
266 /* Write the record marker */
267 if (xprt->stream) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400268 u32 *marker = req->rq_svec[0].iov_base;
Chuck Levera246b012005-08-11 16:25:23 -0400269
270 *marker = htonl(0x80000000|(req->rq_slen-sizeof(*marker)));
271 }
272
273 /* Continue transmitting the packet/record. We must be careful
274 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever9903cd12005-08-11 16:25:26 -0400275 * called sendmsg().
Chuck Levera246b012005-08-11 16:25:23 -0400276 */
277 while (1) {
278 req->rq_xtime = jiffies;
Chuck Lever9903cd12005-08-11 16:25:26 -0400279 status = xs_sendmsg(xprt, req);
Chuck Levera246b012005-08-11 16:25:23 -0400280
281 if (status < 0)
282 break;
283
284 if (xprt->stream) {
285 req->rq_bytes_sent += status;
286
287 /* If we've sent the entire packet, immediately
288 * reset the count of bytes sent. */
289 if (req->rq_bytes_sent >= req->rq_slen) {
290 req->rq_bytes_sent = 0;
291 return 0;
292 }
293 } else {
294 if (status >= req->rq_slen)
295 return 0;
296 status = -EAGAIN;
297 break;
298 }
299
300 dprintk("RPC: %4d xmit incomplete (%d left of %d)\n",
301 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
302 req->rq_slen);
303
304 status = -EAGAIN;
305 if (retry++ > 50)
306 break;
307 }
308
309 if (status == -EAGAIN) {
310 if (test_bit(SOCK_ASYNC_NOSPACE, &xprt->sock->flags)) {
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400311 /* Protect against races with write_space */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400312 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400313 /* Don't race with disconnect */
314 if (!xprt_connected(xprt))
315 task->tk_status = -ENOTCONN;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400316 else if (test_bit(SOCK_NOSPACE, &xprt->sock->flags))
317 xprt_wait_for_buffer_space(task);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400318 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400319 return status;
320 }
321 /* Keep holding the socket if it is blocked */
322 rpc_delay(task, HZ>>4);
323 }
324 return status;
325}
326
Chuck Lever9903cd12005-08-11 16:25:26 -0400327/**
328 * xs_close - close a socket
329 * @xprt: transport
330 *
Chuck Levera246b012005-08-11 16:25:23 -0400331 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400332static void xs_close(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400333{
Chuck Lever9903cd12005-08-11 16:25:26 -0400334 struct socket *sock = xprt->sock;
335 struct sock *sk = xprt->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400336
337 if (!sk)
338 return;
339
Chuck Lever9903cd12005-08-11 16:25:26 -0400340 dprintk("RPC: xs_close xprt %p\n", xprt);
341
Chuck Levera246b012005-08-11 16:25:23 -0400342 write_lock_bh(&sk->sk_callback_lock);
343 xprt->inet = NULL;
344 xprt->sock = NULL;
345
Chuck Lever9903cd12005-08-11 16:25:26 -0400346 sk->sk_user_data = NULL;
347 sk->sk_data_ready = xprt->old_data_ready;
Chuck Levera246b012005-08-11 16:25:23 -0400348 sk->sk_state_change = xprt->old_state_change;
Chuck Lever9903cd12005-08-11 16:25:26 -0400349 sk->sk_write_space = xprt->old_write_space;
Chuck Levera246b012005-08-11 16:25:23 -0400350 write_unlock_bh(&sk->sk_callback_lock);
351
Chuck Lever9903cd12005-08-11 16:25:26 -0400352 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400353
354 sock_release(sock);
355}
356
Chuck Lever9903cd12005-08-11 16:25:26 -0400357/**
358 * xs_destroy - prepare to shutdown a transport
359 * @xprt: doomed transport
360 *
361 */
362static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400363{
Chuck Lever9903cd12005-08-11 16:25:26 -0400364 dprintk("RPC: xs_destroy xprt %p\n", xprt);
365
Chuck Lever55aa4f52005-08-11 16:25:47 -0400366 cancel_delayed_work(&xprt->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400367 flush_scheduled_work();
368
369 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400370 xs_close(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400371 kfree(xprt->slot);
372}
373
Chuck Lever9903cd12005-08-11 16:25:26 -0400374static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400375{
Chuck Lever9903cd12005-08-11 16:25:26 -0400376 return (struct rpc_xprt *) sk->sk_user_data;
377}
378
379/**
380 * xs_udp_data_ready - "data ready" callback for UDP sockets
381 * @sk: socket with data to read
382 * @len: how much data to read
383 *
384 */
385static void xs_udp_data_ready(struct sock *sk, int len)
386{
387 struct rpc_task *task;
388 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400389 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400390 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400391 int err, repsize, copied;
392 u32 _xid, *xp;
393
394 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400395 dprintk("RPC: xs_udp_data_ready...\n");
396 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400397 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400398
399 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
400 goto out;
401
402 if (xprt->shutdown)
403 goto dropit;
404
405 repsize = skb->len - sizeof(struct udphdr);
406 if (repsize < 4) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400407 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400408 goto dropit;
409 }
410
411 /* Copy the XID from the skb... */
412 xp = skb_header_pointer(skb, sizeof(struct udphdr),
413 sizeof(_xid), &_xid);
414 if (xp == NULL)
415 goto dropit;
416
417 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400418 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400419 rovr = xprt_lookup_rqst(xprt, *xp);
420 if (!rovr)
421 goto out_unlock;
422 task = rovr->rq_task;
423
424 dprintk("RPC: %4d received reply\n", task->tk_pid);
425
426 if ((copied = rovr->rq_private_buf.buflen) > repsize)
427 copied = repsize;
428
429 /* Suck it into the iovec, verify checksum if not done by hw. */
430 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb))
431 goto out_unlock;
432
433 /* Something worked... */
434 dst_confirm(skb->dst);
435
436 xprt_complete_rqst(xprt, rovr, copied);
437
438 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400439 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400440 dropit:
441 skb_free_datagram(sk, skb);
442 out:
443 read_unlock(&sk->sk_callback_lock);
444}
445
Chuck Lever9903cd12005-08-11 16:25:26 -0400446static inline size_t xs_tcp_copy_data(skb_reader_t *desc, void *p, size_t len)
Chuck Levera246b012005-08-11 16:25:23 -0400447{
448 if (len > desc->count)
449 len = desc->count;
450 if (skb_copy_bits(desc->skb, desc->offset, p, len)) {
451 dprintk("RPC: failed to copy %zu bytes from skb. %zu bytes remain\n",
452 len, desc->count);
453 return 0;
454 }
455 desc->offset += len;
456 desc->count -= len;
457 dprintk("RPC: copied %zu bytes from skb. %zu bytes remain\n",
458 len, desc->count);
459 return len;
460}
461
Chuck Lever9903cd12005-08-11 16:25:26 -0400462static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400463{
464 size_t len, used;
465 char *p;
466
467 p = ((char *) &xprt->tcp_recm) + xprt->tcp_offset;
468 len = sizeof(xprt->tcp_recm) - xprt->tcp_offset;
Chuck Lever9903cd12005-08-11 16:25:26 -0400469 used = xs_tcp_copy_data(desc, p, len);
Chuck Levera246b012005-08-11 16:25:23 -0400470 xprt->tcp_offset += used;
471 if (used != len)
472 return;
473 xprt->tcp_reclen = ntohl(xprt->tcp_recm);
474 if (xprt->tcp_reclen & 0x80000000)
475 xprt->tcp_flags |= XPRT_LAST_FRAG;
476 else
477 xprt->tcp_flags &= ~XPRT_LAST_FRAG;
478 xprt->tcp_reclen &= 0x7fffffff;
479 xprt->tcp_flags &= ~XPRT_COPY_RECM;
480 xprt->tcp_offset = 0;
481 /* Sanity check of the record length */
482 if (xprt->tcp_reclen < 4) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400483 dprintk("RPC: invalid TCP record fragment length\n");
Chuck Levera246b012005-08-11 16:25:23 -0400484 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400485 return;
Chuck Levera246b012005-08-11 16:25:23 -0400486 }
487 dprintk("RPC: reading TCP record fragment of length %d\n",
488 xprt->tcp_reclen);
489}
490
Chuck Lever9903cd12005-08-11 16:25:26 -0400491static void xs_tcp_check_recm(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400492{
493 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u, tcp_flags = %lx\n",
494 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen, xprt->tcp_flags);
495 if (xprt->tcp_offset == xprt->tcp_reclen) {
496 xprt->tcp_flags |= XPRT_COPY_RECM;
497 xprt->tcp_offset = 0;
498 if (xprt->tcp_flags & XPRT_LAST_FRAG) {
499 xprt->tcp_flags &= ~XPRT_COPY_DATA;
500 xprt->tcp_flags |= XPRT_COPY_XID;
501 xprt->tcp_copied = 0;
502 }
503 }
504}
505
Chuck Lever9903cd12005-08-11 16:25:26 -0400506static inline void xs_tcp_read_xid(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400507{
508 size_t len, used;
509 char *p;
510
511 len = sizeof(xprt->tcp_xid) - xprt->tcp_offset;
512 dprintk("RPC: reading XID (%Zu bytes)\n", len);
513 p = ((char *) &xprt->tcp_xid) + xprt->tcp_offset;
Chuck Lever9903cd12005-08-11 16:25:26 -0400514 used = xs_tcp_copy_data(desc, p, len);
Chuck Levera246b012005-08-11 16:25:23 -0400515 xprt->tcp_offset += used;
516 if (used != len)
517 return;
518 xprt->tcp_flags &= ~XPRT_COPY_XID;
519 xprt->tcp_flags |= XPRT_COPY_DATA;
520 xprt->tcp_copied = 4;
521 dprintk("RPC: reading reply for XID %08x\n",
522 ntohl(xprt->tcp_xid));
Chuck Lever9903cd12005-08-11 16:25:26 -0400523 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400524}
525
Chuck Lever9903cd12005-08-11 16:25:26 -0400526static inline void xs_tcp_read_request(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400527{
528 struct rpc_rqst *req;
529 struct xdr_buf *rcvbuf;
530 size_t len;
531 ssize_t r;
532
533 /* Find and lock the request corresponding to this xid */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400534 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400535 req = xprt_lookup_rqst(xprt, xprt->tcp_xid);
536 if (!req) {
537 xprt->tcp_flags &= ~XPRT_COPY_DATA;
538 dprintk("RPC: XID %08x request not found!\n",
539 ntohl(xprt->tcp_xid));
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400540 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400541 return;
542 }
543
544 rcvbuf = &req->rq_private_buf;
545 len = desc->count;
546 if (len > xprt->tcp_reclen - xprt->tcp_offset) {
547 skb_reader_t my_desc;
548
549 len = xprt->tcp_reclen - xprt->tcp_offset;
550 memcpy(&my_desc, desc, sizeof(my_desc));
551 my_desc.count = len;
552 r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied,
Chuck Lever9903cd12005-08-11 16:25:26 -0400553 &my_desc, xs_tcp_copy_data);
Chuck Levera246b012005-08-11 16:25:23 -0400554 desc->count -= r;
555 desc->offset += r;
556 } else
557 r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied,
Chuck Lever9903cd12005-08-11 16:25:26 -0400558 desc, xs_tcp_copy_data);
Chuck Levera246b012005-08-11 16:25:23 -0400559
560 if (r > 0) {
561 xprt->tcp_copied += r;
562 xprt->tcp_offset += r;
563 }
564 if (r != len) {
565 /* Error when copying to the receive buffer,
566 * usually because we weren't able to allocate
567 * additional buffer pages. All we can do now
568 * is turn off XPRT_COPY_DATA, so the request
569 * will not receive any additional updates,
570 * and time out.
571 * Any remaining data from this record will
572 * be discarded.
573 */
574 xprt->tcp_flags &= ~XPRT_COPY_DATA;
575 dprintk("RPC: XID %08x truncated request\n",
576 ntohl(xprt->tcp_xid));
577 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
578 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen);
579 goto out;
580 }
581
582 dprintk("RPC: XID %08x read %Zd bytes\n",
583 ntohl(xprt->tcp_xid), r);
584 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
585 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen);
586
587 if (xprt->tcp_copied == req->rq_private_buf.buflen)
588 xprt->tcp_flags &= ~XPRT_COPY_DATA;
589 else if (xprt->tcp_offset == xprt->tcp_reclen) {
590 if (xprt->tcp_flags & XPRT_LAST_FRAG)
591 xprt->tcp_flags &= ~XPRT_COPY_DATA;
592 }
593
594out:
595 if (!(xprt->tcp_flags & XPRT_COPY_DATA)) {
596 dprintk("RPC: %4d received reply complete\n",
597 req->rq_task->tk_pid);
598 xprt_complete_rqst(xprt, req, xprt->tcp_copied);
599 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400600 spin_unlock(&xprt->transport_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400601 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400602}
603
Chuck Lever9903cd12005-08-11 16:25:26 -0400604static inline void xs_tcp_read_discard(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400605{
606 size_t len;
607
608 len = xprt->tcp_reclen - xprt->tcp_offset;
609 if (len > desc->count)
610 len = desc->count;
611 desc->count -= len;
612 desc->offset += len;
613 xprt->tcp_offset += len;
614 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever9903cd12005-08-11 16:25:26 -0400615 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400616}
617
Chuck Lever9903cd12005-08-11 16:25:26 -0400618static 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 -0400619{
620 struct rpc_xprt *xprt = rd_desc->arg.data;
621 skb_reader_t desc = {
622 .skb = skb,
623 .offset = offset,
624 .count = len,
625 .csum = 0
Chuck Lever9903cd12005-08-11 16:25:26 -0400626 };
Chuck Levera246b012005-08-11 16:25:23 -0400627
Chuck Lever9903cd12005-08-11 16:25:26 -0400628 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -0400629 do {
630 /* Read in a new fragment marker if necessary */
631 /* Can we ever really expect to get completely empty fragments? */
632 if (xprt->tcp_flags & XPRT_COPY_RECM) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400633 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400634 continue;
635 }
636 /* Read in the xid if necessary */
637 if (xprt->tcp_flags & XPRT_COPY_XID) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400638 xs_tcp_read_xid(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400639 continue;
640 }
641 /* Read in the request data */
642 if (xprt->tcp_flags & XPRT_COPY_DATA) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400643 xs_tcp_read_request(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400644 continue;
645 }
646 /* Skip over any trailing bytes on short reads */
Chuck Lever9903cd12005-08-11 16:25:26 -0400647 xs_tcp_read_discard(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400648 } while (desc.count);
Chuck Lever9903cd12005-08-11 16:25:26 -0400649 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -0400650 return len - desc.count;
651}
652
Chuck Lever9903cd12005-08-11 16:25:26 -0400653/**
654 * xs_tcp_data_ready - "data ready" callback for TCP sockets
655 * @sk: socket with data to read
656 * @bytes: how much data to read
657 *
658 */
659static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -0400660{
661 struct rpc_xprt *xprt;
662 read_descriptor_t rd_desc;
663
664 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400665 dprintk("RPC: xs_tcp_data_ready...\n");
666 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400667 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400668 if (xprt->shutdown)
669 goto out;
670
Chuck Lever9903cd12005-08-11 16:25:26 -0400671 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -0400672 rd_desc.arg.data = xprt;
673 rd_desc.count = 65536;
Chuck Lever9903cd12005-08-11 16:25:26 -0400674 tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
Chuck Levera246b012005-08-11 16:25:23 -0400675out:
676 read_unlock(&sk->sk_callback_lock);
677}
678
Chuck Lever9903cd12005-08-11 16:25:26 -0400679/**
680 * xs_tcp_state_change - callback to handle TCP socket state changes
681 * @sk: socket whose state has changed
682 *
683 */
684static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400685{
Chuck Lever9903cd12005-08-11 16:25:26 -0400686 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400687
688 read_lock(&sk->sk_callback_lock);
689 if (!(xprt = xprt_from_sock(sk)))
690 goto out;
Chuck Lever9903cd12005-08-11 16:25:26 -0400691 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400692 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
693 sk->sk_state, xprt_connected(xprt),
694 sock_flag(sk, SOCK_DEAD),
695 sock_flag(sk, SOCK_ZAPPED));
696
697 switch (sk->sk_state) {
698 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400699 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400700 if (!xprt_test_and_set_connected(xprt)) {
701 /* Reset TCP record info */
702 xprt->tcp_offset = 0;
703 xprt->tcp_reclen = 0;
704 xprt->tcp_copied = 0;
705 xprt->tcp_flags = XPRT_COPY_RECM | XPRT_COPY_XID;
Chuck Lever44fbac22005-08-11 16:25:44 -0400706 xprt_wake_pending_tasks(xprt, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400707 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400708 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400709 break;
710 case TCP_SYN_SENT:
711 case TCP_SYN_RECV:
712 break;
713 default:
714 xprt_disconnect(xprt);
715 break;
716 }
717 out:
718 read_unlock(&sk->sk_callback_lock);
719}
720
Chuck Lever9903cd12005-08-11 16:25:26 -0400721/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400722 * xs_udp_write_space - callback invoked when socket buffer space
723 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -0400724 * @sk: socket whose state has changed
725 *
Chuck Levera246b012005-08-11 16:25:23 -0400726 * Called when more output buffer space is available for this socket.
727 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400728 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -0400729 * with a bunch of small requests.
730 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400731static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400732{
Chuck Levera246b012005-08-11 16:25:23 -0400733 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400734
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400735 /* from net/core/sock.c:sock_def_write_space */
736 if (sock_writeable(sk)) {
737 struct socket *sock;
738 struct rpc_xprt *xprt;
739
740 if (unlikely(!(sock = sk->sk_socket)))
Chuck Levera246b012005-08-11 16:25:23 -0400741 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400742 if (unlikely(!(xprt = xprt_from_sock(sk))))
Chuck Levera246b012005-08-11 16:25:23 -0400743 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400744 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
745 goto out;
746
747 xprt_write_space(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400748 }
749
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400750 out:
751 read_unlock(&sk->sk_callback_lock);
752}
Chuck Levera246b012005-08-11 16:25:23 -0400753
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400754/**
755 * xs_tcp_write_space - callback invoked when socket buffer space
756 * becomes available
757 * @sk: socket whose state has changed
758 *
759 * Called when more output buffer space is available for this socket.
760 * We try not to wake our writers until they can make "significant"
761 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
762 * with a bunch of small requests.
763 */
764static void xs_tcp_write_space(struct sock *sk)
765{
766 read_lock(&sk->sk_callback_lock);
767
768 /* from net/core/stream.c:sk_stream_write_space */
769 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
770 struct socket *sock;
771 struct rpc_xprt *xprt;
772
773 if (unlikely(!(sock = sk->sk_socket)))
774 goto out;
775 if (unlikely(!(xprt = xprt_from_sock(sk))))
776 goto out;
777 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
778 goto out;
779
780 xprt_write_space(xprt);
781 }
782
783 out:
Chuck Levera246b012005-08-11 16:25:23 -0400784 read_unlock(&sk->sk_callback_lock);
785}
786
Chuck Lever9903cd12005-08-11 16:25:26 -0400787/**
788 * xs_set_buffer_size - set send and receive limits
789 * @xprt: generic transport
790 *
791 * Set socket send and receive limits based on the
792 * sndsize and rcvsize fields in the generic transport
793 * structure. This applies only to UDP sockets.
Chuck Levera246b012005-08-11 16:25:23 -0400794 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400795static void xs_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400796{
797 struct sock *sk = xprt->inet;
798
799 if (xprt->stream)
800 return;
801 if (xprt->rcvsize) {
802 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
803 sk->sk_rcvbuf = xprt->rcvsize * xprt->max_reqs * 2;
804 }
805 if (xprt->sndsize) {
806 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
807 sk->sk_sndbuf = xprt->sndsize * xprt->max_reqs * 2;
808 sk->sk_write_space(sk);
809 }
810}
811
Chuck Lever9903cd12005-08-11 16:25:26 -0400812static int xs_bindresvport(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -0400813{
814 struct sockaddr_in myaddr = {
815 .sin_family = AF_INET,
816 };
Chuck Lever9903cd12005-08-11 16:25:26 -0400817 int err, port;
Chuck Levera246b012005-08-11 16:25:23 -0400818
819 /* Were we already bound to a given port? Try to reuse it */
820 port = xprt->port;
821 do {
822 myaddr.sin_port = htons(port);
823 err = sock->ops->bind(sock, (struct sockaddr *) &myaddr,
824 sizeof(myaddr));
825 if (err == 0) {
826 xprt->port = port;
Chuck Lever9903cd12005-08-11 16:25:26 -0400827 dprintk("RPC: xs_bindresvport bound to port %u\n",
828 port);
Chuck Levera246b012005-08-11 16:25:23 -0400829 return 0;
830 }
831 if (--port == 0)
Chuck Lever9903cd12005-08-11 16:25:26 -0400832 port = XS_MAX_RESVPORT;
Chuck Levera246b012005-08-11 16:25:23 -0400833 } while (err == -EADDRINUSE && port != xprt->port);
834
Chuck Lever9903cd12005-08-11 16:25:26 -0400835 dprintk("RPC: can't bind to reserved port (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -0400836 return err;
837}
838
Chuck Lever9903cd12005-08-11 16:25:26 -0400839/**
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400840 * xs_udp_connect_worker - set up a UDP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400841 * @args: RPC transport to connect
842 *
843 * Invoked by a work queue tasklet.
Chuck Levera246b012005-08-11 16:25:23 -0400844 */
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400845static void xs_udp_connect_worker(void *args)
Chuck Levera246b012005-08-11 16:25:23 -0400846{
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400847 struct rpc_xprt *xprt = (struct rpc_xprt *) args;
Chuck Levera246b012005-08-11 16:25:23 -0400848 struct socket *sock = xprt->sock;
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400849 int err, status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -0400850
851 if (xprt->shutdown || xprt->addr.sin_port == 0)
852 goto out;
853
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400854 dprintk("RPC: xs_udp_connect_worker for xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400855
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400856 /* Start by resetting any existing state */
Chuck Lever9903cd12005-08-11 16:25:26 -0400857 xs_close(xprt);
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400858
859 if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
860 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -0400861 goto out;
862 }
Chuck Levera246b012005-08-11 16:25:23 -0400863
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400864 if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) {
865 sock_release(sock);
866 goto out;
867 }
868
869 if (!xprt->inet) {
870 struct sock *sk = sock->sk;
871
872 write_lock_bh(&sk->sk_callback_lock);
873
874 sk->sk_user_data = xprt;
875 xprt->old_data_ready = sk->sk_data_ready;
876 xprt->old_state_change = sk->sk_state_change;
877 xprt->old_write_space = sk->sk_write_space;
878 sk->sk_data_ready = xs_udp_data_ready;
879 sk->sk_write_space = xs_udp_write_space;
880 sk->sk_no_check = UDP_CSUM_NORCV;
881
882 xprt_set_connected(xprt);
883
884 /* Reset to new socket */
885 xprt->sock = sock;
886 xprt->inet = sk;
887
888 write_unlock_bh(&sk->sk_callback_lock);
889 }
890 xs_set_buffer_size(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400891 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400892out:
893 xprt_wake_pending_tasks(xprt, status);
894 xprt_clear_connecting(xprt);
895}
896
897/**
898 * xs_tcp_connect_worker - connect a TCP socket to a remote endpoint
899 * @args: RPC transport to connect
900 *
901 * Invoked by a work queue tasklet.
902 */
903static void xs_tcp_connect_worker(void *args)
904{
905 struct rpc_xprt *xprt = (struct rpc_xprt *)args;
906 struct socket *sock = xprt->sock;
907 int err, status = -EIO;
908
909 if (xprt->shutdown || xprt->addr.sin_port == 0)
Chuck Levera246b012005-08-11 16:25:23 -0400910 goto out;
911
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400912 dprintk("RPC: xs_tcp_connect_worker for xprt %p\n", xprt);
913
914 /* Start by resetting any existing socket state */
915 xs_close(xprt);
916
917 if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
918 dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
919 goto out;
920 }
921
922 if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) {
923 sock_release(sock);
924 goto out;
925 }
926
927 if (!xprt->inet) {
928 struct sock *sk = sock->sk;
929
930 write_lock_bh(&sk->sk_callback_lock);
931
932 sk->sk_user_data = xprt;
933 xprt->old_data_ready = sk->sk_data_ready;
934 xprt->old_state_change = sk->sk_state_change;
935 xprt->old_write_space = sk->sk_write_space;
936 sk->sk_data_ready = xs_tcp_data_ready;
937 sk->sk_state_change = xs_tcp_state_change;
938 sk->sk_write_space = xs_tcp_write_space;
939 tcp_sk(sk)->nonagle = 1;
940
941 xprt_clear_connected(xprt);
942
943 /* Reset to new socket */
944 xprt->sock = sock;
945 xprt->inet = sk;
946
947 write_unlock_bh(&sk->sk_callback_lock);
948 }
949
950 /* Tell the socket layer to start connecting... */
Chuck Levera246b012005-08-11 16:25:23 -0400951 status = sock->ops->connect(sock, (struct sockaddr *) &xprt->addr,
952 sizeof(xprt->addr), O_NONBLOCK);
953 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
954 xprt, -status, xprt_connected(xprt), sock->sk->sk_state);
955 if (status < 0) {
956 switch (status) {
957 case -EINPROGRESS:
958 case -EALREADY:
959 goto out_clear;
960 }
961 }
962out:
Chuck Lever44fbac22005-08-11 16:25:44 -0400963 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -0400964out_clear:
Chuck Lever2226feb2005-08-11 16:25:38 -0400965 xprt_clear_connecting(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400966}
967
Chuck Lever9903cd12005-08-11 16:25:26 -0400968/**
969 * xs_connect - connect a socket to a remote endpoint
970 * @task: address of RPC task that manages state of connect request
971 *
972 * TCP: If the remote end dropped the connection, delay reconnecting.
973 */
974static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400975{
976 struct rpc_xprt *xprt = task->tk_xprt;
977
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400978 if (xprt_test_and_set_connecting(xprt))
979 return;
980
981 if (xprt->sock != NULL) {
982 dprintk("RPC: xs_connect delayed xprt %p\n", xprt);
983 schedule_delayed_work(&xprt->connect_worker,
Chuck Levera246b012005-08-11 16:25:23 -0400984 RPC_REESTABLISH_TIMEOUT);
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400985 } else {
986 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
987 schedule_work(&xprt->connect_worker);
988
989 /* flush_scheduled_work can sleep... */
990 if (!RPC_IS_ASYNC(task))
991 flush_scheduled_work();
Chuck Levera246b012005-08-11 16:25:23 -0400992 }
993}
994
Chuck Lever9903cd12005-08-11 16:25:26 -0400995static struct rpc_xprt_ops xs_ops = {
996 .set_buffer_size = xs_set_buffer_size,
997 .connect = xs_connect,
998 .send_request = xs_send_request,
999 .close = xs_close,
1000 .destroy = xs_destroy,
Chuck Levera246b012005-08-11 16:25:23 -04001001};
1002
1003extern unsigned int xprt_udp_slot_table_entries;
1004extern unsigned int xprt_tcp_slot_table_entries;
1005
Chuck Lever9903cd12005-08-11 16:25:26 -04001006/**
1007 * xs_setup_udp - Set up transport to use a UDP socket
1008 * @xprt: transport to set up
1009 * @to: timeout parameters
1010 *
1011 */
Chuck Levera246b012005-08-11 16:25:23 -04001012int xs_setup_udp(struct rpc_xprt *xprt, struct rpc_timeout *to)
1013{
1014 size_t slot_table_size;
1015
1016 dprintk("RPC: setting up udp-ipv4 transport...\n");
1017
1018 xprt->max_reqs = xprt_udp_slot_table_entries;
1019 slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]);
1020 xprt->slot = kmalloc(slot_table_size, GFP_KERNEL);
1021 if (xprt->slot == NULL)
1022 return -ENOMEM;
1023 memset(xprt->slot, 0, slot_table_size);
1024
1025 xprt->prot = IPPROTO_UDP;
Chuck Lever9903cd12005-08-11 16:25:26 -04001026 xprt->port = XS_MAX_RESVPORT;
Chuck Levera246b012005-08-11 16:25:23 -04001027 xprt->stream = 0;
1028 xprt->nocong = 0;
1029 xprt->cwnd = RPC_INITCWND;
1030 xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0;
1031 /* XXX: header size can vary due to auth type, IPv6, etc. */
1032 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
1033
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001034 INIT_WORK(&xprt->connect_worker, xs_udp_connect_worker, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001035
Chuck Lever9903cd12005-08-11 16:25:26 -04001036 xprt->ops = &xs_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001037
1038 if (to)
1039 xprt->timeout = *to;
1040 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001041 xprt_set_timeout(&xprt->timeout, 5, 5 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001042
1043 return 0;
1044}
1045
Chuck Lever9903cd12005-08-11 16:25:26 -04001046/**
1047 * xs_setup_tcp - Set up transport to use a TCP socket
1048 * @xprt: transport to set up
1049 * @to: timeout parameters
1050 *
1051 */
Chuck Levera246b012005-08-11 16:25:23 -04001052int xs_setup_tcp(struct rpc_xprt *xprt, struct rpc_timeout *to)
1053{
1054 size_t slot_table_size;
1055
1056 dprintk("RPC: setting up tcp-ipv4 transport...\n");
1057
1058 xprt->max_reqs = xprt_tcp_slot_table_entries;
1059 slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]);
1060 xprt->slot = kmalloc(slot_table_size, GFP_KERNEL);
1061 if (xprt->slot == NULL)
1062 return -ENOMEM;
1063 memset(xprt->slot, 0, slot_table_size);
1064
1065 xprt->prot = IPPROTO_TCP;
Chuck Lever9903cd12005-08-11 16:25:26 -04001066 xprt->port = XS_MAX_RESVPORT;
Chuck Levera246b012005-08-11 16:25:23 -04001067 xprt->stream = 1;
1068 xprt->nocong = 1;
1069 xprt->cwnd = RPC_MAXCWND(xprt);
1070 xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0;
1071 xprt->max_payload = (1U << 31) - 1;
1072
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001073 INIT_WORK(&xprt->connect_worker, xs_tcp_connect_worker, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001074
Chuck Lever9903cd12005-08-11 16:25:26 -04001075 xprt->ops = &xs_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001076
1077 if (to)
1078 xprt->timeout = *to;
1079 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001080 xprt_set_timeout(&xprt->timeout, 2, 60 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001081
1082 return 0;
1083}