blob: fc4fbe8ea346d2554cbf5bd87b079ddd84d3deed [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
463 dprintk("RPC: %4d received reply\n", task->tk_pid);
464
465 if ((copied = rovr->rq_private_buf.buflen) > repsize)
466 copied = repsize;
467
468 /* Suck it into the iovec, verify checksum if not done by hw. */
469 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb))
470 goto out_unlock;
471
472 /* Something worked... */
473 dst_confirm(skb->dst);
474
475 xprt_complete_rqst(xprt, rovr, copied);
476
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:
637 if (!(xprt->tcp_flags & XPRT_COPY_DATA)) {
638 dprintk("RPC: %4d received reply complete\n",
639 req->rq_task->tk_pid);
640 xprt_complete_rqst(xprt, req, xprt->tcp_copied);
641 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400642 spin_unlock(&xprt->transport_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400643 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400644}
645
Chuck Lever9903cd12005-08-11 16:25:26 -0400646static inline void xs_tcp_read_discard(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400647{
648 size_t len;
649
650 len = xprt->tcp_reclen - xprt->tcp_offset;
651 if (len > desc->count)
652 len = desc->count;
653 desc->count -= len;
654 desc->offset += len;
655 xprt->tcp_offset += len;
656 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever9903cd12005-08-11 16:25:26 -0400657 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400658}
659
Chuck Lever9903cd12005-08-11 16:25:26 -0400660static 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 -0400661{
662 struct rpc_xprt *xprt = rd_desc->arg.data;
663 skb_reader_t desc = {
664 .skb = skb,
665 .offset = offset,
666 .count = len,
667 .csum = 0
Chuck Lever9903cd12005-08-11 16:25:26 -0400668 };
Chuck Levera246b012005-08-11 16:25:23 -0400669
Chuck Lever9903cd12005-08-11 16:25:26 -0400670 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -0400671 do {
672 /* Read in a new fragment marker if necessary */
673 /* Can we ever really expect to get completely empty fragments? */
674 if (xprt->tcp_flags & XPRT_COPY_RECM) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400675 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400676 continue;
677 }
678 /* Read in the xid if necessary */
679 if (xprt->tcp_flags & XPRT_COPY_XID) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400680 xs_tcp_read_xid(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400681 continue;
682 }
683 /* Read in the request data */
684 if (xprt->tcp_flags & XPRT_COPY_DATA) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400685 xs_tcp_read_request(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400686 continue;
687 }
688 /* Skip over any trailing bytes on short reads */
Chuck Lever9903cd12005-08-11 16:25:26 -0400689 xs_tcp_read_discard(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400690 } while (desc.count);
Chuck Lever9903cd12005-08-11 16:25:26 -0400691 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -0400692 return len - desc.count;
693}
694
Chuck Lever9903cd12005-08-11 16:25:26 -0400695/**
696 * xs_tcp_data_ready - "data ready" callback for TCP sockets
697 * @sk: socket with data to read
698 * @bytes: how much data to read
699 *
700 */
701static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -0400702{
703 struct rpc_xprt *xprt;
704 read_descriptor_t rd_desc;
705
706 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400707 dprintk("RPC: xs_tcp_data_ready...\n");
708 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400709 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400710 if (xprt->shutdown)
711 goto out;
712
Chuck Lever9903cd12005-08-11 16:25:26 -0400713 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -0400714 rd_desc.arg.data = xprt;
715 rd_desc.count = 65536;
Chuck Lever9903cd12005-08-11 16:25:26 -0400716 tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
Chuck Levera246b012005-08-11 16:25:23 -0400717out:
718 read_unlock(&sk->sk_callback_lock);
719}
720
Chuck Lever9903cd12005-08-11 16:25:26 -0400721/**
722 * xs_tcp_state_change - callback to handle TCP socket state changes
723 * @sk: socket whose state has changed
724 *
725 */
726static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400727{
Chuck Lever9903cd12005-08-11 16:25:26 -0400728 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400729
730 read_lock(&sk->sk_callback_lock);
731 if (!(xprt = xprt_from_sock(sk)))
732 goto out;
Chuck Lever9903cd12005-08-11 16:25:26 -0400733 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400734 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
735 sk->sk_state, xprt_connected(xprt),
736 sock_flag(sk, SOCK_DEAD),
737 sock_flag(sk, SOCK_ZAPPED));
738
739 switch (sk->sk_state) {
740 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400741 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400742 if (!xprt_test_and_set_connected(xprt)) {
743 /* Reset TCP record info */
744 xprt->tcp_offset = 0;
745 xprt->tcp_reclen = 0;
746 xprt->tcp_copied = 0;
747 xprt->tcp_flags = XPRT_COPY_RECM | XPRT_COPY_XID;
Chuck Lever44fbac22005-08-11 16:25:44 -0400748 xprt_wake_pending_tasks(xprt, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400749 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400750 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400751 break;
752 case TCP_SYN_SENT:
753 case TCP_SYN_RECV:
754 break;
755 default:
756 xprt_disconnect(xprt);
757 break;
758 }
759 out:
760 read_unlock(&sk->sk_callback_lock);
761}
762
Chuck Lever9903cd12005-08-11 16:25:26 -0400763/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400764 * xs_udp_write_space - callback invoked when socket buffer space
765 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -0400766 * @sk: socket whose state has changed
767 *
Chuck Levera246b012005-08-11 16:25:23 -0400768 * Called when more output buffer space is available for this socket.
769 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400770 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -0400771 * with a bunch of small requests.
772 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400773static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400774{
Chuck Levera246b012005-08-11 16:25:23 -0400775 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400776
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400777 /* from net/core/sock.c:sock_def_write_space */
778 if (sock_writeable(sk)) {
779 struct socket *sock;
780 struct rpc_xprt *xprt;
781
782 if (unlikely(!(sock = sk->sk_socket)))
Chuck Levera246b012005-08-11 16:25:23 -0400783 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400784 if (unlikely(!(xprt = xprt_from_sock(sk))))
Chuck Levera246b012005-08-11 16:25:23 -0400785 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400786 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
787 goto out;
788
789 xprt_write_space(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400790 }
791
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400792 out:
793 read_unlock(&sk->sk_callback_lock);
794}
Chuck Levera246b012005-08-11 16:25:23 -0400795
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400796/**
797 * xs_tcp_write_space - callback invoked when socket buffer space
798 * becomes available
799 * @sk: socket whose state has changed
800 *
801 * Called when more output buffer space is available for this socket.
802 * We try not to wake our writers until they can make "significant"
803 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
804 * with a bunch of small requests.
805 */
806static void xs_tcp_write_space(struct sock *sk)
807{
808 read_lock(&sk->sk_callback_lock);
809
810 /* from net/core/stream.c:sk_stream_write_space */
811 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
812 struct socket *sock;
813 struct rpc_xprt *xprt;
814
815 if (unlikely(!(sock = sk->sk_socket)))
816 goto out;
817 if (unlikely(!(xprt = xprt_from_sock(sk))))
818 goto out;
819 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
820 goto out;
821
822 xprt_write_space(xprt);
823 }
824
825 out:
Chuck Levera246b012005-08-11 16:25:23 -0400826 read_unlock(&sk->sk_callback_lock);
827}
828
Chuck Lever9903cd12005-08-11 16:25:26 -0400829/**
Chuck Lever43118c22005-08-25 16:25:49 -0700830 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever9903cd12005-08-11 16:25:26 -0400831 * @xprt: generic transport
832 *
833 * Set socket send and receive limits based on the
834 * sndsize and rcvsize fields in the generic transport
Chuck Lever43118c22005-08-25 16:25:49 -0700835 * structure.
Chuck Levera246b012005-08-11 16:25:23 -0400836 */
Chuck Lever43118c22005-08-25 16:25:49 -0700837static void xs_udp_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400838{
839 struct sock *sk = xprt->inet;
840
Chuck Levera246b012005-08-11 16:25:23 -0400841 if (xprt->rcvsize) {
842 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
843 sk->sk_rcvbuf = xprt->rcvsize * xprt->max_reqs * 2;
844 }
845 if (xprt->sndsize) {
846 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
847 sk->sk_sndbuf = xprt->sndsize * xprt->max_reqs * 2;
848 sk->sk_write_space(sk);
849 }
850}
851
Chuck Lever43118c22005-08-25 16:25:49 -0700852/**
853 * xs_tcp_set_buffer_size - set send and receive limits
854 * @xprt: generic transport
855 *
856 * Nothing to do for TCP.
857 */
858static void xs_tcp_set_buffer_size(struct rpc_xprt *xprt)
859{
860 return;
861}
862
Chuck Lever9903cd12005-08-11 16:25:26 -0400863static int xs_bindresvport(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -0400864{
865 struct sockaddr_in myaddr = {
866 .sin_family = AF_INET,
867 };
Chuck Lever9903cd12005-08-11 16:25:26 -0400868 int err, port;
Chuck Levera246b012005-08-11 16:25:23 -0400869
870 /* Were we already bound to a given port? Try to reuse it */
871 port = xprt->port;
872 do {
873 myaddr.sin_port = htons(port);
874 err = sock->ops->bind(sock, (struct sockaddr *) &myaddr,
875 sizeof(myaddr));
876 if (err == 0) {
877 xprt->port = port;
Chuck Lever9903cd12005-08-11 16:25:26 -0400878 dprintk("RPC: xs_bindresvport bound to port %u\n",
879 port);
Chuck Levera246b012005-08-11 16:25:23 -0400880 return 0;
881 }
882 if (--port == 0)
Chuck Lever9903cd12005-08-11 16:25:26 -0400883 port = XS_MAX_RESVPORT;
Chuck Levera246b012005-08-11 16:25:23 -0400884 } while (err == -EADDRINUSE && port != xprt->port);
885
Chuck Lever9903cd12005-08-11 16:25:26 -0400886 dprintk("RPC: can't bind to reserved port (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -0400887 return err;
888}
889
Chuck Lever9903cd12005-08-11 16:25:26 -0400890/**
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400891 * xs_udp_connect_worker - set up a UDP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400892 * @args: RPC transport to connect
893 *
894 * Invoked by a work queue tasklet.
Chuck Levera246b012005-08-11 16:25:23 -0400895 */
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400896static void xs_udp_connect_worker(void *args)
Chuck Levera246b012005-08-11 16:25:23 -0400897{
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400898 struct rpc_xprt *xprt = (struct rpc_xprt *) args;
Chuck Levera246b012005-08-11 16:25:23 -0400899 struct socket *sock = xprt->sock;
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400900 int err, status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -0400901
902 if (xprt->shutdown || xprt->addr.sin_port == 0)
903 goto out;
904
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400905 dprintk("RPC: xs_udp_connect_worker for xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400906
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400907 /* Start by resetting any existing state */
Chuck Lever9903cd12005-08-11 16:25:26 -0400908 xs_close(xprt);
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400909
910 if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
911 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -0400912 goto out;
913 }
Chuck Levera246b012005-08-11 16:25:23 -0400914
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400915 if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) {
916 sock_release(sock);
917 goto out;
918 }
919
920 if (!xprt->inet) {
921 struct sock *sk = sock->sk;
922
923 write_lock_bh(&sk->sk_callback_lock);
924
925 sk->sk_user_data = xprt;
926 xprt->old_data_ready = sk->sk_data_ready;
927 xprt->old_state_change = sk->sk_state_change;
928 xprt->old_write_space = sk->sk_write_space;
929 sk->sk_data_ready = xs_udp_data_ready;
930 sk->sk_write_space = xs_udp_write_space;
931 sk->sk_no_check = UDP_CSUM_NORCV;
932
933 xprt_set_connected(xprt);
934
935 /* Reset to new socket */
936 xprt->sock = sock;
937 xprt->inet = sk;
938
939 write_unlock_bh(&sk->sk_callback_lock);
940 }
Chuck Lever43118c22005-08-25 16:25:49 -0700941 xs_udp_set_buffer_size(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400942 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400943out:
944 xprt_wake_pending_tasks(xprt, status);
945 xprt_clear_connecting(xprt);
946}
947
948/**
949 * xs_tcp_connect_worker - connect a TCP socket to a remote endpoint
950 * @args: RPC transport to connect
951 *
952 * Invoked by a work queue tasklet.
953 */
954static void xs_tcp_connect_worker(void *args)
955{
956 struct rpc_xprt *xprt = (struct rpc_xprt *)args;
957 struct socket *sock = xprt->sock;
958 int err, status = -EIO;
959
960 if (xprt->shutdown || xprt->addr.sin_port == 0)
Chuck Levera246b012005-08-11 16:25:23 -0400961 goto out;
962
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400963 dprintk("RPC: xs_tcp_connect_worker for xprt %p\n", xprt);
964
965 /* Start by resetting any existing socket state */
966 xs_close(xprt);
967
968 if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
969 dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
970 goto out;
971 }
972
973 if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) {
974 sock_release(sock);
975 goto out;
976 }
977
978 if (!xprt->inet) {
979 struct sock *sk = sock->sk;
980
981 write_lock_bh(&sk->sk_callback_lock);
982
983 sk->sk_user_data = xprt;
984 xprt->old_data_ready = sk->sk_data_ready;
985 xprt->old_state_change = sk->sk_state_change;
986 xprt->old_write_space = sk->sk_write_space;
987 sk->sk_data_ready = xs_tcp_data_ready;
988 sk->sk_state_change = xs_tcp_state_change;
989 sk->sk_write_space = xs_tcp_write_space;
990 tcp_sk(sk)->nonagle = 1;
991
992 xprt_clear_connected(xprt);
993
994 /* Reset to new socket */
995 xprt->sock = sock;
996 xprt->inet = sk;
997
998 write_unlock_bh(&sk->sk_callback_lock);
999 }
1000
1001 /* Tell the socket layer to start connecting... */
Chuck Levera246b012005-08-11 16:25:23 -04001002 status = sock->ops->connect(sock, (struct sockaddr *) &xprt->addr,
1003 sizeof(xprt->addr), O_NONBLOCK);
1004 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1005 xprt, -status, xprt_connected(xprt), sock->sk->sk_state);
1006 if (status < 0) {
1007 switch (status) {
1008 case -EINPROGRESS:
1009 case -EALREADY:
1010 goto out_clear;
1011 }
1012 }
1013out:
Chuck Lever44fbac22005-08-11 16:25:44 -04001014 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001015out_clear:
Chuck Lever2226feb2005-08-11 16:25:38 -04001016 xprt_clear_connecting(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001017}
1018
Chuck Lever9903cd12005-08-11 16:25:26 -04001019/**
1020 * xs_connect - connect a socket to a remote endpoint
1021 * @task: address of RPC task that manages state of connect request
1022 *
1023 * TCP: If the remote end dropped the connection, delay reconnecting.
1024 */
1025static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04001026{
1027 struct rpc_xprt *xprt = task->tk_xprt;
1028
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001029 if (xprt_test_and_set_connecting(xprt))
1030 return;
1031
1032 if (xprt->sock != NULL) {
1033 dprintk("RPC: xs_connect delayed xprt %p\n", xprt);
1034 schedule_delayed_work(&xprt->connect_worker,
Chuck Levera246b012005-08-11 16:25:23 -04001035 RPC_REESTABLISH_TIMEOUT);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001036 } else {
1037 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
1038 schedule_work(&xprt->connect_worker);
1039
1040 /* flush_scheduled_work can sleep... */
1041 if (!RPC_IS_ASYNC(task))
1042 flush_scheduled_work();
Chuck Levera246b012005-08-11 16:25:23 -04001043 }
1044}
1045
Chuck Lever262965f2005-08-11 16:25:56 -04001046static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07001047 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07001048 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever9903cd12005-08-11 16:25:26 -04001049 .connect = xs_connect,
Chuck Lever262965f2005-08-11 16:25:56 -04001050 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001051 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever262965f2005-08-11 16:25:56 -04001052 .close = xs_close,
1053 .destroy = xs_destroy,
1054};
1055
1056static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07001057 .set_buffer_size = xs_tcp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07001058 .reserve_xprt = xprt_reserve_xprt,
Chuck Lever262965f2005-08-11 16:25:56 -04001059 .connect = xs_connect,
1060 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001061 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Chuck Lever9903cd12005-08-11 16:25:26 -04001062 .close = xs_close,
1063 .destroy = xs_destroy,
Chuck Levera246b012005-08-11 16:25:23 -04001064};
1065
1066extern unsigned int xprt_udp_slot_table_entries;
1067extern unsigned int xprt_tcp_slot_table_entries;
1068
Chuck Lever9903cd12005-08-11 16:25:26 -04001069/**
1070 * xs_setup_udp - Set up transport to use a UDP socket
1071 * @xprt: transport to set up
1072 * @to: timeout parameters
1073 *
1074 */
Chuck Levera246b012005-08-11 16:25:23 -04001075int xs_setup_udp(struct rpc_xprt *xprt, struct rpc_timeout *to)
1076{
1077 size_t slot_table_size;
1078
1079 dprintk("RPC: setting up udp-ipv4 transport...\n");
1080
1081 xprt->max_reqs = xprt_udp_slot_table_entries;
1082 slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]);
1083 xprt->slot = kmalloc(slot_table_size, GFP_KERNEL);
1084 if (xprt->slot == NULL)
1085 return -ENOMEM;
1086 memset(xprt->slot, 0, slot_table_size);
1087
1088 xprt->prot = IPPROTO_UDP;
Chuck Lever9903cd12005-08-11 16:25:26 -04001089 xprt->port = XS_MAX_RESVPORT;
Chuck Lever808012f2005-08-25 16:25:49 -07001090 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001091 xprt->nocong = 0;
1092 xprt->cwnd = RPC_INITCWND;
1093 xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0;
1094 /* XXX: header size can vary due to auth type, IPv6, etc. */
1095 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
1096
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001097 INIT_WORK(&xprt->connect_worker, xs_udp_connect_worker, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001098
Chuck Lever262965f2005-08-11 16:25:56 -04001099 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001100
1101 if (to)
1102 xprt->timeout = *to;
1103 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001104 xprt_set_timeout(&xprt->timeout, 5, 5 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001105
1106 return 0;
1107}
1108
Chuck Lever9903cd12005-08-11 16:25:26 -04001109/**
1110 * xs_setup_tcp - Set up transport to use a TCP socket
1111 * @xprt: transport to set up
1112 * @to: timeout parameters
1113 *
1114 */
Chuck Levera246b012005-08-11 16:25:23 -04001115int xs_setup_tcp(struct rpc_xprt *xprt, struct rpc_timeout *to)
1116{
1117 size_t slot_table_size;
1118
1119 dprintk("RPC: setting up tcp-ipv4 transport...\n");
1120
1121 xprt->max_reqs = xprt_tcp_slot_table_entries;
1122 slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]);
1123 xprt->slot = kmalloc(slot_table_size, GFP_KERNEL);
1124 if (xprt->slot == NULL)
1125 return -ENOMEM;
1126 memset(xprt->slot, 0, slot_table_size);
1127
1128 xprt->prot = IPPROTO_TCP;
Chuck Lever9903cd12005-08-11 16:25:26 -04001129 xprt->port = XS_MAX_RESVPORT;
Chuck Lever808012f2005-08-25 16:25:49 -07001130 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Levera246b012005-08-11 16:25:23 -04001131 xprt->nocong = 1;
1132 xprt->cwnd = RPC_MAXCWND(xprt);
1133 xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0;
Chuck Lever808012f2005-08-25 16:25:49 -07001134 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04001135
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001136 INIT_WORK(&xprt->connect_worker, xs_tcp_connect_worker, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001137
Chuck Lever262965f2005-08-11 16:25:56 -04001138 xprt->ops = &xs_tcp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001139
1140 if (to)
1141 xprt->timeout = *to;
1142 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001143 xprt_set_timeout(&xprt->timeout, 2, 60 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001144
1145 return 0;
1146}