blob: 6c2f5dcea416855c9775fd2aacefbafe7b6d05f5 [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 Lever262965f2005-08-11 16:25:56 -040043/*
44 * How many times to try sending a request on a socket before waiting
45 * for the socket buffer to clear.
46 */
47#define XS_SENDMSG_RETRY (10U)
48
Chuck Levera246b012005-08-11 16:25:23 -040049#ifdef RPC_DEBUG
50# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -040051# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -040052#endif
53
Chuck Levera246b012005-08-11 16:25:23 -040054#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -040055static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -040056{
Chuck Lever9903cd12005-08-11 16:25:26 -040057 u8 *buf = (u8 *) packet;
58 int j;
Chuck Levera246b012005-08-11 16:25:23 -040059
60 dprintk("RPC: %s\n", msg);
61 for (j = 0; j < count && j < 128; j += 4) {
62 if (!(j & 31)) {
63 if (j)
64 dprintk("\n");
65 dprintk("0x%04x ", j);
66 }
67 dprintk("%02x%02x%02x%02x ",
68 buf[j], buf[j+1], buf[j+2], buf[j+3]);
69 }
70 dprintk("\n");
71}
72#else
Chuck Lever9903cd12005-08-11 16:25:26 -040073static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -040074{
75 /* NOP */
76}
77#endif
78
Chuck Leverb4b5cc82005-08-11 16:25:29 -040079#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
80
81static inline int xs_send_head(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, unsigned int len)
82{
83 struct kvec iov = {
84 .iov_base = xdr->head[0].iov_base + base,
85 .iov_len = len - base,
86 };
87 struct msghdr msg = {
88 .msg_name = addr,
89 .msg_namelen = addrlen,
90 .msg_flags = XS_SENDMSG_FLAGS,
91 };
92
93 if (xdr->len > len)
94 msg.msg_flags |= MSG_MORE;
95
96 if (likely(iov.iov_len))
97 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
98 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
99}
100
101static int xs_send_tail(struct socket *sock, struct xdr_buf *xdr, unsigned int base, unsigned int len)
102{
103 struct kvec iov = {
104 .iov_base = xdr->tail[0].iov_base + base,
105 .iov_len = len - base,
106 };
107 struct msghdr msg = {
108 .msg_flags = XS_SENDMSG_FLAGS,
109 };
110
111 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
112}
113
Chuck Lever9903cd12005-08-11 16:25:26 -0400114/**
115 * xs_sendpages - write pages directly to a socket
116 * @sock: socket to send on
117 * @addr: UDP only -- address of destination
118 * @addrlen: UDP only -- length of destination address
119 * @xdr: buffer containing this request
120 * @base: starting position in the buffer
121 *
Chuck Levera246b012005-08-11 16:25:23 -0400122 */
Chuck Lever262965f2005-08-11 16:25:56 -0400123static 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 -0400124{
125 struct page **ppage = xdr->pages;
126 unsigned int len, pglen = xdr->page_len;
127 int err, ret = 0;
128 ssize_t (*sendpage)(struct socket *, struct page *, int, size_t, int);
129
Chuck Lever262965f2005-08-11 16:25:56 -0400130 if (unlikely(!sock))
131 return -ENOTCONN;
132
133 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
134
Chuck Levera246b012005-08-11 16:25:23 -0400135 len = xdr->head[0].iov_len;
136 if (base < len || (addr != NULL && base == 0)) {
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400137 err = xs_send_head(sock, addr, addrlen, xdr, base, len);
Chuck Levera246b012005-08-11 16:25:23 -0400138 if (ret == 0)
139 ret = err;
140 else if (err > 0)
141 ret += err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400142 if (err != (len - base))
Chuck Levera246b012005-08-11 16:25:23 -0400143 goto out;
144 base = 0;
145 } else
146 base -= len;
147
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400148 if (unlikely(pglen == 0))
Chuck Levera246b012005-08-11 16:25:23 -0400149 goto copy_tail;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400150 if (unlikely(base >= pglen)) {
Chuck Levera246b012005-08-11 16:25:23 -0400151 base -= pglen;
152 goto copy_tail;
153 }
154 if (base || xdr->page_base) {
155 pglen -= base;
Chuck Lever9903cd12005-08-11 16:25:26 -0400156 base += xdr->page_base;
Chuck Levera246b012005-08-11 16:25:23 -0400157 ppage += base >> PAGE_CACHE_SHIFT;
158 base &= ~PAGE_CACHE_MASK;
159 }
160
161 sendpage = sock->ops->sendpage ? : sock_no_sendpage;
162 do {
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400163 int flags = XS_SENDMSG_FLAGS;
Chuck Levera246b012005-08-11 16:25:23 -0400164
165 len = PAGE_CACHE_SIZE;
166 if (base)
167 len -= base;
168 if (pglen < len)
169 len = pglen;
170
171 if (pglen != len || xdr->tail[0].iov_len != 0)
172 flags |= MSG_MORE;
173
174 /* Hmm... We might be dealing with highmem pages */
175 if (PageHighMem(*ppage))
176 sendpage = sock_no_sendpage;
177 err = sendpage(sock, *ppage, base, len, flags);
178 if (ret == 0)
179 ret = err;
180 else if (err > 0)
181 ret += err;
182 if (err != len)
183 goto out;
184 base = 0;
185 ppage++;
186 } while ((pglen -= len) != 0);
187copy_tail:
188 len = xdr->tail[0].iov_len;
189 if (base < len) {
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400190 err = xs_send_tail(sock, xdr, base, len);
Chuck Levera246b012005-08-11 16:25:23 -0400191 if (ret == 0)
192 ret = err;
193 else if (err > 0)
194 ret += err;
195 }
196out:
197 return ret;
198}
199
Chuck Lever9903cd12005-08-11 16:25:26 -0400200/**
Chuck Lever262965f2005-08-11 16:25:56 -0400201 * xs_nospace - place task on wait queue if transmit was incomplete
202 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400203 *
Chuck Levera246b012005-08-11 16:25:23 -0400204 */
Chuck Lever262965f2005-08-11 16:25:56 -0400205static void xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400206{
Chuck Lever262965f2005-08-11 16:25:56 -0400207 struct rpc_rqst *req = task->tk_rqstp;
208 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400209
Chuck Lever262965f2005-08-11 16:25:56 -0400210 dprintk("RPC: %4d xmit incomplete (%u left of %u)\n",
211 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
212 req->rq_slen);
213
214 if (test_bit(SOCK_ASYNC_NOSPACE, &xprt->sock->flags)) {
215 /* Protect against races with write_space */
216 spin_lock_bh(&xprt->transport_lock);
217
218 /* Don't race with disconnect */
219 if (!xprt_connected(xprt))
220 task->tk_status = -ENOTCONN;
221 else if (test_bit(SOCK_NOSPACE, &xprt->sock->flags))
222 xprt_wait_for_buffer_space(task);
223
224 spin_unlock_bh(&xprt->transport_lock);
225 } else
226 /* Keep holding the socket if it is blocked */
227 rpc_delay(task, HZ>>4);
228}
229
230/**
231 * xs_udp_send_request - write an RPC request to a UDP socket
232 * @task: address of RPC task that manages the state of an RPC request
233 *
234 * Return values:
235 * 0: The request has been sent
236 * EAGAIN: The socket was blocked, please call again later to
237 * complete the request
238 * ENOTCONN: Caller needs to invoke connect logic then call again
239 * other: Some other error occured, the request was not sent
240 */
241static int xs_udp_send_request(struct rpc_task *task)
242{
243 struct rpc_rqst *req = task->tk_rqstp;
244 struct rpc_xprt *xprt = req->rq_xprt;
245 struct xdr_buf *xdr = &req->rq_snd_buf;
246 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400247
Chuck Lever9903cd12005-08-11 16:25:26 -0400248 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400249 req->rq_svec->iov_base,
250 req->rq_svec->iov_len);
251
Chuck Lever262965f2005-08-11 16:25:56 -0400252 req->rq_xtime = jiffies;
253 status = xs_sendpages(xprt->sock, (struct sockaddr *) &xprt->addr,
254 sizeof(xprt->addr), xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400255
Chuck Lever262965f2005-08-11 16:25:56 -0400256 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
257 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400258
Chuck Lever262965f2005-08-11 16:25:56 -0400259 if (likely(status >= (int) req->rq_slen))
260 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400261
Chuck Lever262965f2005-08-11 16:25:56 -0400262 /* Still some bytes left; set up for a retry later. */
263 if (status > 0)
264 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400265
Chuck Lever262965f2005-08-11 16:25:56 -0400266 switch (status) {
267 case -ENETUNREACH:
268 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400269 case -ECONNREFUSED:
270 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400271 * prompts ECONNREFUSED. */
Chuck Levera246b012005-08-11 16:25:23 -0400272 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400273 case -EAGAIN:
274 xs_nospace(task);
Chuck Levera246b012005-08-11 16:25:23 -0400275 break;
276 default:
Chuck Lever262965f2005-08-11 16:25:56 -0400277 dprintk("RPC: sendmsg returned unrecognized error %d\n",
278 -status);
Chuck Lever9903cd12005-08-11 16:25:26 -0400279 break;
Chuck Levera246b012005-08-11 16:25:23 -0400280 }
Chuck Lever262965f2005-08-11 16:25:56 -0400281
282 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400283}
284
Chuck Lever808012f2005-08-25 16:25:49 -0700285static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
286{
287 u32 reclen = buf->len - sizeof(rpc_fraghdr);
288 rpc_fraghdr *base = buf->head[0].iov_base;
289 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
290}
291
Chuck Lever9903cd12005-08-11 16:25:26 -0400292/**
Chuck Lever262965f2005-08-11 16:25:56 -0400293 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400294 * @task: address of RPC task that manages the state of an RPC request
295 *
296 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400297 * 0: The request has been sent
298 * EAGAIN: The socket was blocked, please call again later to
299 * complete the request
300 * ENOTCONN: Caller needs to invoke connect logic then call again
301 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400302 *
303 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400304 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400305 */
Chuck Lever262965f2005-08-11 16:25:56 -0400306static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400307{
308 struct rpc_rqst *req = task->tk_rqstp;
309 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Lever262965f2005-08-11 16:25:56 -0400310 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Levera246b012005-08-11 16:25:23 -0400311 int status, retry = 0;
312
Chuck Lever808012f2005-08-25 16:25:49 -0700313 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400314
Chuck Lever262965f2005-08-11 16:25:56 -0400315 xs_pktdump("packet data:",
316 req->rq_svec->iov_base,
317 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400318
319 /* Continue transmitting the packet/record. We must be careful
320 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400321 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400322 while (1) {
323 req->rq_xtime = jiffies;
Chuck Lever262965f2005-08-11 16:25:56 -0400324 status = xs_sendpages(xprt->sock, NULL, 0, xdr,
325 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400326
Chuck Lever262965f2005-08-11 16:25:56 -0400327 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
328 xdr->len - req->rq_bytes_sent, status);
329
330 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400331 break;
332
Chuck Lever262965f2005-08-11 16:25:56 -0400333 /* If we've sent the entire packet, immediately
334 * reset the count of bytes sent. */
335 req->rq_bytes_sent += status;
336 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
337 req->rq_bytes_sent = 0;
338 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400339 }
340
Chuck Levera246b012005-08-11 16:25:23 -0400341 status = -EAGAIN;
Chuck Lever262965f2005-08-11 16:25:56 -0400342 if (retry++ > XS_SENDMSG_RETRY)
Chuck Levera246b012005-08-11 16:25:23 -0400343 break;
344 }
345
Chuck Lever262965f2005-08-11 16:25:56 -0400346 switch (status) {
347 case -EAGAIN:
348 xs_nospace(task);
349 break;
350 case -ECONNREFUSED:
351 case -ECONNRESET:
352 case -ENOTCONN:
353 case -EPIPE:
354 status = -ENOTCONN;
355 break;
356 default:
357 dprintk("RPC: sendmsg returned unrecognized error %d\n",
358 -status);
Chuck Lever43118c22005-08-25 16:25:49 -0700359 xprt_disconnect(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400360 break;
Chuck Levera246b012005-08-11 16:25:23 -0400361 }
Chuck Lever262965f2005-08-11 16:25:56 -0400362
Chuck Levera246b012005-08-11 16:25:23 -0400363 return status;
364}
365
Chuck Lever9903cd12005-08-11 16:25:26 -0400366/**
367 * xs_close - close a socket
368 * @xprt: transport
369 *
Chuck Levera246b012005-08-11 16:25:23 -0400370 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400371static void xs_close(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400372{
Chuck Lever9903cd12005-08-11 16:25:26 -0400373 struct socket *sock = xprt->sock;
374 struct sock *sk = xprt->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400375
376 if (!sk)
377 return;
378
Chuck Lever9903cd12005-08-11 16:25:26 -0400379 dprintk("RPC: xs_close xprt %p\n", xprt);
380
Chuck Levera246b012005-08-11 16:25:23 -0400381 write_lock_bh(&sk->sk_callback_lock);
382 xprt->inet = NULL;
383 xprt->sock = NULL;
384
Chuck Lever9903cd12005-08-11 16:25:26 -0400385 sk->sk_user_data = NULL;
386 sk->sk_data_ready = xprt->old_data_ready;
Chuck Levera246b012005-08-11 16:25:23 -0400387 sk->sk_state_change = xprt->old_state_change;
Chuck Lever9903cd12005-08-11 16:25:26 -0400388 sk->sk_write_space = xprt->old_write_space;
Chuck Levera246b012005-08-11 16:25:23 -0400389 write_unlock_bh(&sk->sk_callback_lock);
390
Chuck Lever9903cd12005-08-11 16:25:26 -0400391 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400392
393 sock_release(sock);
394}
395
Chuck Lever9903cd12005-08-11 16:25:26 -0400396/**
397 * xs_destroy - prepare to shutdown a transport
398 * @xprt: doomed transport
399 *
400 */
401static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400402{
Chuck Lever9903cd12005-08-11 16:25:26 -0400403 dprintk("RPC: xs_destroy xprt %p\n", xprt);
404
Chuck Lever55aa4f52005-08-11 16:25:47 -0400405 cancel_delayed_work(&xprt->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400406 flush_scheduled_work();
407
408 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400409 xs_close(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400410 kfree(xprt->slot);
411}
412
Chuck Lever9903cd12005-08-11 16:25:26 -0400413static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400414{
Chuck Lever9903cd12005-08-11 16:25:26 -0400415 return (struct rpc_xprt *) sk->sk_user_data;
416}
417
418/**
419 * xs_udp_data_ready - "data ready" callback for UDP sockets
420 * @sk: socket with data to read
421 * @len: how much data to read
422 *
423 */
424static void xs_udp_data_ready(struct sock *sk, int len)
425{
426 struct rpc_task *task;
427 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400428 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400429 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400430 int err, repsize, copied;
431 u32 _xid, *xp;
432
433 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400434 dprintk("RPC: xs_udp_data_ready...\n");
435 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400436 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400437
438 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
439 goto out;
440
441 if (xprt->shutdown)
442 goto dropit;
443
444 repsize = skb->len - sizeof(struct udphdr);
445 if (repsize < 4) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400446 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400447 goto dropit;
448 }
449
450 /* Copy the XID from the skb... */
451 xp = skb_header_pointer(skb, sizeof(struct udphdr),
452 sizeof(_xid), &_xid);
453 if (xp == NULL)
454 goto dropit;
455
456 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400457 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400458 rovr = xprt_lookup_rqst(xprt, *xp);
459 if (!rovr)
460 goto out_unlock;
461 task = rovr->rq_task;
462
Chuck Levera246b012005-08-11 16:25:23 -0400463 if ((copied = rovr->rq_private_buf.buflen) > repsize)
464 copied = repsize;
465
466 /* Suck it into the iovec, verify checksum if not done by hw. */
467 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb))
468 goto out_unlock;
469
470 /* Something worked... */
471 dst_confirm(skb->dst);
472
Chuck Lever1570c1e2005-08-25 16:25:52 -0700473 xprt_adjust_cwnd(task, copied);
474 xprt_update_rtt(task);
475 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400476
477 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400478 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400479 dropit:
480 skb_free_datagram(sk, skb);
481 out:
482 read_unlock(&sk->sk_callback_lock);
483}
484
Chuck Lever9903cd12005-08-11 16:25:26 -0400485static inline size_t xs_tcp_copy_data(skb_reader_t *desc, void *p, size_t len)
Chuck Levera246b012005-08-11 16:25:23 -0400486{
487 if (len > desc->count)
488 len = desc->count;
489 if (skb_copy_bits(desc->skb, desc->offset, p, len)) {
490 dprintk("RPC: failed to copy %zu bytes from skb. %zu bytes remain\n",
491 len, desc->count);
492 return 0;
493 }
494 desc->offset += len;
495 desc->count -= len;
496 dprintk("RPC: copied %zu bytes from skb. %zu bytes remain\n",
497 len, desc->count);
498 return len;
499}
500
Chuck Lever9903cd12005-08-11 16:25:26 -0400501static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400502{
503 size_t len, used;
504 char *p;
505
506 p = ((char *) &xprt->tcp_recm) + xprt->tcp_offset;
507 len = sizeof(xprt->tcp_recm) - xprt->tcp_offset;
Chuck Lever9903cd12005-08-11 16:25:26 -0400508 used = xs_tcp_copy_data(desc, p, len);
Chuck Levera246b012005-08-11 16:25:23 -0400509 xprt->tcp_offset += used;
510 if (used != len)
511 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700512
Chuck Levera246b012005-08-11 16:25:23 -0400513 xprt->tcp_reclen = ntohl(xprt->tcp_recm);
Chuck Lever808012f2005-08-25 16:25:49 -0700514 if (xprt->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levera246b012005-08-11 16:25:23 -0400515 xprt->tcp_flags |= XPRT_LAST_FRAG;
516 else
517 xprt->tcp_flags &= ~XPRT_LAST_FRAG;
Chuck Lever808012f2005-08-25 16:25:49 -0700518 xprt->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
519
Chuck Levera246b012005-08-11 16:25:23 -0400520 xprt->tcp_flags &= ~XPRT_COPY_RECM;
521 xprt->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700522
Chuck Levera246b012005-08-11 16:25:23 -0400523 /* Sanity check of the record length */
Chuck Lever808012f2005-08-25 16:25:49 -0700524 if (unlikely(xprt->tcp_reclen < 4)) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400525 dprintk("RPC: invalid TCP record fragment length\n");
Chuck Levera246b012005-08-11 16:25:23 -0400526 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400527 return;
Chuck Levera246b012005-08-11 16:25:23 -0400528 }
529 dprintk("RPC: reading TCP record fragment of length %d\n",
530 xprt->tcp_reclen);
531}
532
Chuck Lever9903cd12005-08-11 16:25:26 -0400533static void xs_tcp_check_recm(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400534{
535 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u, tcp_flags = %lx\n",
536 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen, xprt->tcp_flags);
537 if (xprt->tcp_offset == xprt->tcp_reclen) {
538 xprt->tcp_flags |= XPRT_COPY_RECM;
539 xprt->tcp_offset = 0;
540 if (xprt->tcp_flags & XPRT_LAST_FRAG) {
541 xprt->tcp_flags &= ~XPRT_COPY_DATA;
542 xprt->tcp_flags |= XPRT_COPY_XID;
543 xprt->tcp_copied = 0;
544 }
545 }
546}
547
Chuck Lever9903cd12005-08-11 16:25:26 -0400548static inline void xs_tcp_read_xid(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400549{
550 size_t len, used;
551 char *p;
552
553 len = sizeof(xprt->tcp_xid) - xprt->tcp_offset;
554 dprintk("RPC: reading XID (%Zu bytes)\n", len);
555 p = ((char *) &xprt->tcp_xid) + xprt->tcp_offset;
Chuck Lever9903cd12005-08-11 16:25:26 -0400556 used = xs_tcp_copy_data(desc, p, len);
Chuck Levera246b012005-08-11 16:25:23 -0400557 xprt->tcp_offset += used;
558 if (used != len)
559 return;
560 xprt->tcp_flags &= ~XPRT_COPY_XID;
561 xprt->tcp_flags |= XPRT_COPY_DATA;
562 xprt->tcp_copied = 4;
563 dprintk("RPC: reading reply for XID %08x\n",
564 ntohl(xprt->tcp_xid));
Chuck Lever9903cd12005-08-11 16:25:26 -0400565 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400566}
567
Chuck Lever9903cd12005-08-11 16:25:26 -0400568static inline void xs_tcp_read_request(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400569{
570 struct rpc_rqst *req;
571 struct xdr_buf *rcvbuf;
572 size_t len;
573 ssize_t r;
574
575 /* Find and lock the request corresponding to this xid */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400576 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400577 req = xprt_lookup_rqst(xprt, xprt->tcp_xid);
578 if (!req) {
579 xprt->tcp_flags &= ~XPRT_COPY_DATA;
580 dprintk("RPC: XID %08x request not found!\n",
581 ntohl(xprt->tcp_xid));
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400582 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400583 return;
584 }
585
586 rcvbuf = &req->rq_private_buf;
587 len = desc->count;
588 if (len > xprt->tcp_reclen - xprt->tcp_offset) {
589 skb_reader_t my_desc;
590
591 len = xprt->tcp_reclen - xprt->tcp_offset;
592 memcpy(&my_desc, desc, sizeof(my_desc));
593 my_desc.count = len;
594 r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied,
Chuck Lever9903cd12005-08-11 16:25:26 -0400595 &my_desc, xs_tcp_copy_data);
Chuck Levera246b012005-08-11 16:25:23 -0400596 desc->count -= r;
597 desc->offset += r;
598 } else
599 r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied,
Chuck Lever9903cd12005-08-11 16:25:26 -0400600 desc, xs_tcp_copy_data);
Chuck Levera246b012005-08-11 16:25:23 -0400601
602 if (r > 0) {
603 xprt->tcp_copied += r;
604 xprt->tcp_offset += r;
605 }
606 if (r != len) {
607 /* Error when copying to the receive buffer,
608 * usually because we weren't able to allocate
609 * additional buffer pages. All we can do now
610 * is turn off XPRT_COPY_DATA, so the request
611 * will not receive any additional updates,
612 * and time out.
613 * Any remaining data from this record will
614 * be discarded.
615 */
616 xprt->tcp_flags &= ~XPRT_COPY_DATA;
617 dprintk("RPC: XID %08x truncated request\n",
618 ntohl(xprt->tcp_xid));
619 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
620 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen);
621 goto out;
622 }
623
624 dprintk("RPC: XID %08x read %Zd bytes\n",
625 ntohl(xprt->tcp_xid), r);
626 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
627 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen);
628
629 if (xprt->tcp_copied == req->rq_private_buf.buflen)
630 xprt->tcp_flags &= ~XPRT_COPY_DATA;
631 else if (xprt->tcp_offset == xprt->tcp_reclen) {
632 if (xprt->tcp_flags & XPRT_LAST_FRAG)
633 xprt->tcp_flags &= ~XPRT_COPY_DATA;
634 }
635
636out:
Chuck Lever1570c1e2005-08-25 16:25:52 -0700637 if (!(xprt->tcp_flags & XPRT_COPY_DATA))
638 xprt_complete_rqst(req->rq_task, xprt->tcp_copied);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400639 spin_unlock(&xprt->transport_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400640 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400641}
642
Chuck Lever9903cd12005-08-11 16:25:26 -0400643static inline void xs_tcp_read_discard(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400644{
645 size_t len;
646
647 len = xprt->tcp_reclen - xprt->tcp_offset;
648 if (len > desc->count)
649 len = desc->count;
650 desc->count -= len;
651 desc->offset += len;
652 xprt->tcp_offset += len;
653 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever9903cd12005-08-11 16:25:26 -0400654 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400655}
656
Chuck Lever9903cd12005-08-11 16:25:26 -0400657static 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 -0400658{
659 struct rpc_xprt *xprt = rd_desc->arg.data;
660 skb_reader_t desc = {
661 .skb = skb,
662 .offset = offset,
663 .count = len,
664 .csum = 0
Chuck Lever9903cd12005-08-11 16:25:26 -0400665 };
Chuck Levera246b012005-08-11 16:25:23 -0400666
Chuck Lever9903cd12005-08-11 16:25:26 -0400667 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -0400668 do {
669 /* Read in a new fragment marker if necessary */
670 /* Can we ever really expect to get completely empty fragments? */
671 if (xprt->tcp_flags & XPRT_COPY_RECM) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400672 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400673 continue;
674 }
675 /* Read in the xid if necessary */
676 if (xprt->tcp_flags & XPRT_COPY_XID) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400677 xs_tcp_read_xid(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400678 continue;
679 }
680 /* Read in the request data */
681 if (xprt->tcp_flags & XPRT_COPY_DATA) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400682 xs_tcp_read_request(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400683 continue;
684 }
685 /* Skip over any trailing bytes on short reads */
Chuck Lever9903cd12005-08-11 16:25:26 -0400686 xs_tcp_read_discard(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400687 } while (desc.count);
Chuck Lever9903cd12005-08-11 16:25:26 -0400688 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -0400689 return len - desc.count;
690}
691
Chuck Lever9903cd12005-08-11 16:25:26 -0400692/**
693 * xs_tcp_data_ready - "data ready" callback for TCP sockets
694 * @sk: socket with data to read
695 * @bytes: how much data to read
696 *
697 */
698static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -0400699{
700 struct rpc_xprt *xprt;
701 read_descriptor_t rd_desc;
702
703 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400704 dprintk("RPC: xs_tcp_data_ready...\n");
705 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400706 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400707 if (xprt->shutdown)
708 goto out;
709
Chuck Lever9903cd12005-08-11 16:25:26 -0400710 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -0400711 rd_desc.arg.data = xprt;
712 rd_desc.count = 65536;
Chuck Lever9903cd12005-08-11 16:25:26 -0400713 tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
Chuck Levera246b012005-08-11 16:25:23 -0400714out:
715 read_unlock(&sk->sk_callback_lock);
716}
717
Chuck Lever9903cd12005-08-11 16:25:26 -0400718/**
719 * xs_tcp_state_change - callback to handle TCP socket state changes
720 * @sk: socket whose state has changed
721 *
722 */
723static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400724{
Chuck Lever9903cd12005-08-11 16:25:26 -0400725 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400726
727 read_lock(&sk->sk_callback_lock);
728 if (!(xprt = xprt_from_sock(sk)))
729 goto out;
Chuck Lever9903cd12005-08-11 16:25:26 -0400730 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400731 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
732 sk->sk_state, xprt_connected(xprt),
733 sock_flag(sk, SOCK_DEAD),
734 sock_flag(sk, SOCK_ZAPPED));
735
736 switch (sk->sk_state) {
737 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400738 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400739 if (!xprt_test_and_set_connected(xprt)) {
740 /* Reset TCP record info */
741 xprt->tcp_offset = 0;
742 xprt->tcp_reclen = 0;
743 xprt->tcp_copied = 0;
744 xprt->tcp_flags = XPRT_COPY_RECM | XPRT_COPY_XID;
Chuck Lever44fbac22005-08-11 16:25:44 -0400745 xprt_wake_pending_tasks(xprt, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400746 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400747 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400748 break;
749 case TCP_SYN_SENT:
750 case TCP_SYN_RECV:
751 break;
752 default:
753 xprt_disconnect(xprt);
754 break;
755 }
756 out:
757 read_unlock(&sk->sk_callback_lock);
758}
759
Chuck Lever9903cd12005-08-11 16:25:26 -0400760/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400761 * xs_udp_write_space - callback invoked when socket buffer space
762 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -0400763 * @sk: socket whose state has changed
764 *
Chuck Levera246b012005-08-11 16:25:23 -0400765 * Called when more output buffer space is available for this socket.
766 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400767 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -0400768 * with a bunch of small requests.
769 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400770static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400771{
Chuck Levera246b012005-08-11 16:25:23 -0400772 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400773
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400774 /* from net/core/sock.c:sock_def_write_space */
775 if (sock_writeable(sk)) {
776 struct socket *sock;
777 struct rpc_xprt *xprt;
778
779 if (unlikely(!(sock = sk->sk_socket)))
Chuck Levera246b012005-08-11 16:25:23 -0400780 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400781 if (unlikely(!(xprt = xprt_from_sock(sk))))
Chuck Levera246b012005-08-11 16:25:23 -0400782 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400783 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
784 goto out;
785
786 xprt_write_space(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400787 }
788
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400789 out:
790 read_unlock(&sk->sk_callback_lock);
791}
Chuck Levera246b012005-08-11 16:25:23 -0400792
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400793/**
794 * xs_tcp_write_space - callback invoked when socket buffer space
795 * becomes available
796 * @sk: socket whose state has changed
797 *
798 * Called when more output buffer space is available for this socket.
799 * We try not to wake our writers until they can make "significant"
800 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
801 * with a bunch of small requests.
802 */
803static void xs_tcp_write_space(struct sock *sk)
804{
805 read_lock(&sk->sk_callback_lock);
806
807 /* from net/core/stream.c:sk_stream_write_space */
808 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
809 struct socket *sock;
810 struct rpc_xprt *xprt;
811
812 if (unlikely(!(sock = sk->sk_socket)))
813 goto out;
814 if (unlikely(!(xprt = xprt_from_sock(sk))))
815 goto out;
816 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
817 goto out;
818
819 xprt_write_space(xprt);
820 }
821
822 out:
Chuck Levera246b012005-08-11 16:25:23 -0400823 read_unlock(&sk->sk_callback_lock);
824}
825
Chuck Lever9903cd12005-08-11 16:25:26 -0400826/**
Chuck Lever43118c22005-08-25 16:25:49 -0700827 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever9903cd12005-08-11 16:25:26 -0400828 * @xprt: generic transport
829 *
830 * Set socket send and receive limits based on the
831 * sndsize and rcvsize fields in the generic transport
Chuck Lever43118c22005-08-25 16:25:49 -0700832 * structure.
Chuck Levera246b012005-08-11 16:25:23 -0400833 */
Chuck Lever43118c22005-08-25 16:25:49 -0700834static void xs_udp_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400835{
836 struct sock *sk = xprt->inet;
837
Chuck Levera246b012005-08-11 16:25:23 -0400838 if (xprt->rcvsize) {
839 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
840 sk->sk_rcvbuf = xprt->rcvsize * xprt->max_reqs * 2;
841 }
842 if (xprt->sndsize) {
843 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
844 sk->sk_sndbuf = xprt->sndsize * xprt->max_reqs * 2;
845 sk->sk_write_space(sk);
846 }
847}
848
Chuck Lever43118c22005-08-25 16:25:49 -0700849/**
850 * xs_tcp_set_buffer_size - set send and receive limits
851 * @xprt: generic transport
852 *
853 * Nothing to do for TCP.
854 */
855static void xs_tcp_set_buffer_size(struct rpc_xprt *xprt)
856{
857 return;
858}
859
Chuck Lever46c0ee82005-08-25 16:25:52 -0700860/**
861 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
862 * @task: task that timed out
863 *
864 * Adjust the congestion window after a retransmit timeout has occurred.
865 */
866static void xs_udp_timer(struct rpc_task *task)
867{
868 xprt_adjust_cwnd(task, -ETIMEDOUT);
869}
870
Chuck Lever9903cd12005-08-11 16:25:26 -0400871static int xs_bindresvport(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -0400872{
873 struct sockaddr_in myaddr = {
874 .sin_family = AF_INET,
875 };
Chuck Lever9903cd12005-08-11 16:25:26 -0400876 int err, port;
Chuck Levera246b012005-08-11 16:25:23 -0400877
878 /* Were we already bound to a given port? Try to reuse it */
879 port = xprt->port;
880 do {
881 myaddr.sin_port = htons(port);
882 err = sock->ops->bind(sock, (struct sockaddr *) &myaddr,
883 sizeof(myaddr));
884 if (err == 0) {
885 xprt->port = port;
Chuck Lever9903cd12005-08-11 16:25:26 -0400886 dprintk("RPC: xs_bindresvport bound to port %u\n",
887 port);
Chuck Levera246b012005-08-11 16:25:23 -0400888 return 0;
889 }
890 if (--port == 0)
Chuck Lever9903cd12005-08-11 16:25:26 -0400891 port = XS_MAX_RESVPORT;
Chuck Levera246b012005-08-11 16:25:23 -0400892 } while (err == -EADDRINUSE && port != xprt->port);
893
Chuck Lever9903cd12005-08-11 16:25:26 -0400894 dprintk("RPC: can't bind to reserved port (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -0400895 return err;
896}
897
Chuck Lever9903cd12005-08-11 16:25:26 -0400898/**
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400899 * xs_udp_connect_worker - set up a UDP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400900 * @args: RPC transport to connect
901 *
902 * Invoked by a work queue tasklet.
Chuck Levera246b012005-08-11 16:25:23 -0400903 */
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400904static void xs_udp_connect_worker(void *args)
Chuck Levera246b012005-08-11 16:25:23 -0400905{
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400906 struct rpc_xprt *xprt = (struct rpc_xprt *) args;
Chuck Levera246b012005-08-11 16:25:23 -0400907 struct socket *sock = xprt->sock;
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400908 int err, status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -0400909
910 if (xprt->shutdown || xprt->addr.sin_port == 0)
911 goto out;
912
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400913 dprintk("RPC: xs_udp_connect_worker for xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400914
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400915 /* Start by resetting any existing state */
Chuck Lever9903cd12005-08-11 16:25:26 -0400916 xs_close(xprt);
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400917
918 if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
919 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -0400920 goto out;
921 }
Chuck Levera246b012005-08-11 16:25:23 -0400922
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400923 if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) {
924 sock_release(sock);
925 goto out;
926 }
927
928 if (!xprt->inet) {
929 struct sock *sk = sock->sk;
930
931 write_lock_bh(&sk->sk_callback_lock);
932
933 sk->sk_user_data = xprt;
934 xprt->old_data_ready = sk->sk_data_ready;
935 xprt->old_state_change = sk->sk_state_change;
936 xprt->old_write_space = sk->sk_write_space;
937 sk->sk_data_ready = xs_udp_data_ready;
938 sk->sk_write_space = xs_udp_write_space;
939 sk->sk_no_check = UDP_CSUM_NORCV;
940
941 xprt_set_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 }
Chuck Lever43118c22005-08-25 16:25:49 -0700949 xs_udp_set_buffer_size(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400950 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400951out:
952 xprt_wake_pending_tasks(xprt, status);
953 xprt_clear_connecting(xprt);
954}
955
956/**
957 * xs_tcp_connect_worker - connect a TCP socket to a remote endpoint
958 * @args: RPC transport to connect
959 *
960 * Invoked by a work queue tasklet.
961 */
962static void xs_tcp_connect_worker(void *args)
963{
964 struct rpc_xprt *xprt = (struct rpc_xprt *)args;
965 struct socket *sock = xprt->sock;
966 int err, status = -EIO;
967
968 if (xprt->shutdown || xprt->addr.sin_port == 0)
Chuck Levera246b012005-08-11 16:25:23 -0400969 goto out;
970
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400971 dprintk("RPC: xs_tcp_connect_worker for xprt %p\n", xprt);
972
973 /* Start by resetting any existing socket state */
974 xs_close(xprt);
975
976 if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
977 dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
978 goto out;
979 }
980
981 if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) {
982 sock_release(sock);
983 goto out;
984 }
985
986 if (!xprt->inet) {
987 struct sock *sk = sock->sk;
988
989 write_lock_bh(&sk->sk_callback_lock);
990
991 sk->sk_user_data = xprt;
992 xprt->old_data_ready = sk->sk_data_ready;
993 xprt->old_state_change = sk->sk_state_change;
994 xprt->old_write_space = sk->sk_write_space;
995 sk->sk_data_ready = xs_tcp_data_ready;
996 sk->sk_state_change = xs_tcp_state_change;
997 sk->sk_write_space = xs_tcp_write_space;
998 tcp_sk(sk)->nonagle = 1;
999
1000 xprt_clear_connected(xprt);
1001
1002 /* Reset to new socket */
1003 xprt->sock = sock;
1004 xprt->inet = sk;
1005
1006 write_unlock_bh(&sk->sk_callback_lock);
1007 }
1008
1009 /* Tell the socket layer to start connecting... */
Chuck Levera246b012005-08-11 16:25:23 -04001010 status = sock->ops->connect(sock, (struct sockaddr *) &xprt->addr,
1011 sizeof(xprt->addr), O_NONBLOCK);
1012 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1013 xprt, -status, xprt_connected(xprt), sock->sk->sk_state);
1014 if (status < 0) {
1015 switch (status) {
1016 case -EINPROGRESS:
1017 case -EALREADY:
1018 goto out_clear;
1019 }
1020 }
1021out:
Chuck Lever44fbac22005-08-11 16:25:44 -04001022 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001023out_clear:
Chuck Lever2226feb2005-08-11 16:25:38 -04001024 xprt_clear_connecting(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001025}
1026
Chuck Lever9903cd12005-08-11 16:25:26 -04001027/**
1028 * xs_connect - connect a socket to a remote endpoint
1029 * @task: address of RPC task that manages state of connect request
1030 *
1031 * TCP: If the remote end dropped the connection, delay reconnecting.
1032 */
1033static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04001034{
1035 struct rpc_xprt *xprt = task->tk_xprt;
1036
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001037 if (xprt_test_and_set_connecting(xprt))
1038 return;
1039
1040 if (xprt->sock != NULL) {
1041 dprintk("RPC: xs_connect delayed xprt %p\n", xprt);
1042 schedule_delayed_work(&xprt->connect_worker,
Chuck Levera246b012005-08-11 16:25:23 -04001043 RPC_REESTABLISH_TIMEOUT);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001044 } else {
1045 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
1046 schedule_work(&xprt->connect_worker);
1047
1048 /* flush_scheduled_work can sleep... */
1049 if (!RPC_IS_ASYNC(task))
1050 flush_scheduled_work();
Chuck Levera246b012005-08-11 16:25:23 -04001051 }
1052}
1053
Chuck Lever262965f2005-08-11 16:25:56 -04001054static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07001055 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07001056 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07001057 .release_xprt = xprt_release_xprt_cong,
Chuck Lever9903cd12005-08-11 16:25:26 -04001058 .connect = xs_connect,
Chuck Lever262965f2005-08-11 16:25:56 -04001059 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001060 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07001061 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07001062 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04001063 .close = xs_close,
1064 .destroy = xs_destroy,
1065};
1066
1067static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07001068 .set_buffer_size = xs_tcp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07001069 .reserve_xprt = xprt_reserve_xprt,
Chuck Lever49e9a892005-08-25 16:25:51 -07001070 .release_xprt = xprt_release_xprt,
Chuck Lever262965f2005-08-11 16:25:56 -04001071 .connect = xs_connect,
1072 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001073 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Chuck Lever9903cd12005-08-11 16:25:26 -04001074 .close = xs_close,
1075 .destroy = xs_destroy,
Chuck Levera246b012005-08-11 16:25:23 -04001076};
1077
1078extern unsigned int xprt_udp_slot_table_entries;
1079extern unsigned int xprt_tcp_slot_table_entries;
1080
Chuck Lever9903cd12005-08-11 16:25:26 -04001081/**
1082 * xs_setup_udp - Set up transport to use a UDP socket
1083 * @xprt: transport to set up
1084 * @to: timeout parameters
1085 *
1086 */
Chuck Levera246b012005-08-11 16:25:23 -04001087int xs_setup_udp(struct rpc_xprt *xprt, struct rpc_timeout *to)
1088{
1089 size_t slot_table_size;
1090
1091 dprintk("RPC: setting up udp-ipv4 transport...\n");
1092
1093 xprt->max_reqs = xprt_udp_slot_table_entries;
1094 slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]);
1095 xprt->slot = kmalloc(slot_table_size, GFP_KERNEL);
1096 if (xprt->slot == NULL)
1097 return -ENOMEM;
1098 memset(xprt->slot, 0, slot_table_size);
1099
1100 xprt->prot = IPPROTO_UDP;
Chuck Lever9903cd12005-08-11 16:25:26 -04001101 xprt->port = XS_MAX_RESVPORT;
Chuck Lever808012f2005-08-25 16:25:49 -07001102 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001103 xprt->nocong = 0;
1104 xprt->cwnd = RPC_INITCWND;
1105 xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0;
1106 /* XXX: header size can vary due to auth type, IPv6, etc. */
1107 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
1108
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001109 INIT_WORK(&xprt->connect_worker, xs_udp_connect_worker, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001110
Chuck Lever262965f2005-08-11 16:25:56 -04001111 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001112
1113 if (to)
1114 xprt->timeout = *to;
1115 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001116 xprt_set_timeout(&xprt->timeout, 5, 5 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001117
1118 return 0;
1119}
1120
Chuck Lever9903cd12005-08-11 16:25:26 -04001121/**
1122 * xs_setup_tcp - Set up transport to use a TCP socket
1123 * @xprt: transport to set up
1124 * @to: timeout parameters
1125 *
1126 */
Chuck Levera246b012005-08-11 16:25:23 -04001127int xs_setup_tcp(struct rpc_xprt *xprt, struct rpc_timeout *to)
1128{
1129 size_t slot_table_size;
1130
1131 dprintk("RPC: setting up tcp-ipv4 transport...\n");
1132
1133 xprt->max_reqs = xprt_tcp_slot_table_entries;
1134 slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]);
1135 xprt->slot = kmalloc(slot_table_size, GFP_KERNEL);
1136 if (xprt->slot == NULL)
1137 return -ENOMEM;
1138 memset(xprt->slot, 0, slot_table_size);
1139
1140 xprt->prot = IPPROTO_TCP;
Chuck Lever9903cd12005-08-11 16:25:26 -04001141 xprt->port = XS_MAX_RESVPORT;
Chuck Lever808012f2005-08-25 16:25:49 -07001142 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Levera246b012005-08-11 16:25:23 -04001143 xprt->nocong = 1;
1144 xprt->cwnd = RPC_MAXCWND(xprt);
1145 xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0;
Chuck Lever808012f2005-08-25 16:25:49 -07001146 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04001147
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001148 INIT_WORK(&xprt->connect_worker, xs_tcp_connect_worker, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001149
Chuck Lever262965f2005-08-11 16:25:56 -04001150 xprt->ops = &xs_tcp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001151
1152 if (to)
1153 xprt->timeout = *to;
1154 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001155 xprt_set_timeout(&xprt->timeout, 2, 60 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001156
1157 return 0;
1158}