blob: 88ac71fcd335bdcd8717cc04fd22802f8b635cc3 [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/*
Chuck Lever262965f2005-08-11 16:25:56 -040039 * How many times to try sending a request on a socket before waiting
40 * for the socket buffer to clear.
41 */
42#define XS_SENDMSG_RETRY (10U)
43
Chuck Levera246b012005-08-11 16:25:23 -040044#ifdef RPC_DEBUG
45# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -040046# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -040047#endif
48
Chuck Levera246b012005-08-11 16:25:23 -040049#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -040050static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -040051{
Chuck Lever9903cd12005-08-11 16:25:26 -040052 u8 *buf = (u8 *) packet;
53 int j;
Chuck Levera246b012005-08-11 16:25:23 -040054
55 dprintk("RPC: %s\n", msg);
56 for (j = 0; j < count && j < 128; j += 4) {
57 if (!(j & 31)) {
58 if (j)
59 dprintk("\n");
60 dprintk("0x%04x ", j);
61 }
62 dprintk("%02x%02x%02x%02x ",
63 buf[j], buf[j+1], buf[j+2], buf[j+3]);
64 }
65 dprintk("\n");
66}
67#else
Chuck Lever9903cd12005-08-11 16:25:26 -040068static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -040069{
70 /* NOP */
71}
72#endif
73
Chuck Leverb4b5cc82005-08-11 16:25:29 -040074#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
75
76static inline int xs_send_head(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, unsigned int len)
77{
78 struct kvec iov = {
79 .iov_base = xdr->head[0].iov_base + base,
80 .iov_len = len - base,
81 };
82 struct msghdr msg = {
83 .msg_name = addr,
84 .msg_namelen = addrlen,
85 .msg_flags = XS_SENDMSG_FLAGS,
86 };
87
88 if (xdr->len > len)
89 msg.msg_flags |= MSG_MORE;
90
91 if (likely(iov.iov_len))
92 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
93 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
94}
95
96static int xs_send_tail(struct socket *sock, struct xdr_buf *xdr, unsigned int base, unsigned int len)
97{
98 struct kvec iov = {
99 .iov_base = xdr->tail[0].iov_base + base,
100 .iov_len = len - base,
101 };
102 struct msghdr msg = {
103 .msg_flags = XS_SENDMSG_FLAGS,
104 };
105
106 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
107}
108
Chuck Lever9903cd12005-08-11 16:25:26 -0400109/**
110 * xs_sendpages - write pages directly to a socket
111 * @sock: socket to send on
112 * @addr: UDP only -- address of destination
113 * @addrlen: UDP only -- length of destination address
114 * @xdr: buffer containing this request
115 * @base: starting position in the buffer
116 *
Chuck Levera246b012005-08-11 16:25:23 -0400117 */
Chuck Lever262965f2005-08-11 16:25:56 -0400118static 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 -0400119{
120 struct page **ppage = xdr->pages;
121 unsigned int len, pglen = xdr->page_len;
122 int err, ret = 0;
123 ssize_t (*sendpage)(struct socket *, struct page *, int, size_t, int);
124
Chuck Lever262965f2005-08-11 16:25:56 -0400125 if (unlikely(!sock))
126 return -ENOTCONN;
127
128 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
129
Chuck Levera246b012005-08-11 16:25:23 -0400130 len = xdr->head[0].iov_len;
131 if (base < len || (addr != NULL && base == 0)) {
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400132 err = xs_send_head(sock, addr, addrlen, xdr, base, len);
Chuck Levera246b012005-08-11 16:25:23 -0400133 if (ret == 0)
134 ret = err;
135 else if (err > 0)
136 ret += err;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400137 if (err != (len - base))
Chuck Levera246b012005-08-11 16:25:23 -0400138 goto out;
139 base = 0;
140 } else
141 base -= len;
142
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400143 if (unlikely(pglen == 0))
Chuck Levera246b012005-08-11 16:25:23 -0400144 goto copy_tail;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400145 if (unlikely(base >= pglen)) {
Chuck Levera246b012005-08-11 16:25:23 -0400146 base -= pglen;
147 goto copy_tail;
148 }
149 if (base || xdr->page_base) {
150 pglen -= base;
Chuck Lever9903cd12005-08-11 16:25:26 -0400151 base += xdr->page_base;
Chuck Levera246b012005-08-11 16:25:23 -0400152 ppage += base >> PAGE_CACHE_SHIFT;
153 base &= ~PAGE_CACHE_MASK;
154 }
155
156 sendpage = sock->ops->sendpage ? : sock_no_sendpage;
157 do {
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400158 int flags = XS_SENDMSG_FLAGS;
Chuck Levera246b012005-08-11 16:25:23 -0400159
160 len = PAGE_CACHE_SIZE;
161 if (base)
162 len -= base;
163 if (pglen < len)
164 len = pglen;
165
166 if (pglen != len || xdr->tail[0].iov_len != 0)
167 flags |= MSG_MORE;
168
169 /* Hmm... We might be dealing with highmem pages */
170 if (PageHighMem(*ppage))
171 sendpage = sock_no_sendpage;
172 err = sendpage(sock, *ppage, base, len, flags);
173 if (ret == 0)
174 ret = err;
175 else if (err > 0)
176 ret += err;
177 if (err != len)
178 goto out;
179 base = 0;
180 ppage++;
181 } while ((pglen -= len) != 0);
182copy_tail:
183 len = xdr->tail[0].iov_len;
184 if (base < len) {
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400185 err = xs_send_tail(sock, xdr, base, len);
Chuck Levera246b012005-08-11 16:25:23 -0400186 if (ret == 0)
187 ret = err;
188 else if (err > 0)
189 ret += err;
190 }
191out:
192 return ret;
193}
194
Chuck Lever9903cd12005-08-11 16:25:26 -0400195/**
Chuck Lever262965f2005-08-11 16:25:56 -0400196 * xs_nospace - place task on wait queue if transmit was incomplete
197 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400198 *
Chuck Levera246b012005-08-11 16:25:23 -0400199 */
Chuck Lever262965f2005-08-11 16:25:56 -0400200static void xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400201{
Chuck Lever262965f2005-08-11 16:25:56 -0400202 struct rpc_rqst *req = task->tk_rqstp;
203 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400204
Chuck Lever262965f2005-08-11 16:25:56 -0400205 dprintk("RPC: %4d xmit incomplete (%u left of %u)\n",
206 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
207 req->rq_slen);
208
209 if (test_bit(SOCK_ASYNC_NOSPACE, &xprt->sock->flags)) {
210 /* Protect against races with write_space */
211 spin_lock_bh(&xprt->transport_lock);
212
213 /* Don't race with disconnect */
214 if (!xprt_connected(xprt))
215 task->tk_status = -ENOTCONN;
216 else if (test_bit(SOCK_NOSPACE, &xprt->sock->flags))
217 xprt_wait_for_buffer_space(task);
218
219 spin_unlock_bh(&xprt->transport_lock);
220 } else
221 /* Keep holding the socket if it is blocked */
222 rpc_delay(task, HZ>>4);
223}
224
225/**
226 * xs_udp_send_request - write an RPC request to a UDP socket
227 * @task: address of RPC task that manages the state of an RPC request
228 *
229 * Return values:
230 * 0: The request has been sent
231 * EAGAIN: The socket was blocked, please call again later to
232 * complete the request
233 * ENOTCONN: Caller needs to invoke connect logic then call again
234 * other: Some other error occured, the request was not sent
235 */
236static int xs_udp_send_request(struct rpc_task *task)
237{
238 struct rpc_rqst *req = task->tk_rqstp;
239 struct rpc_xprt *xprt = req->rq_xprt;
240 struct xdr_buf *xdr = &req->rq_snd_buf;
241 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400242
Chuck Lever9903cd12005-08-11 16:25:26 -0400243 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400244 req->rq_svec->iov_base,
245 req->rq_svec->iov_len);
246
Chuck Lever262965f2005-08-11 16:25:56 -0400247 req->rq_xtime = jiffies;
248 status = xs_sendpages(xprt->sock, (struct sockaddr *) &xprt->addr,
249 sizeof(xprt->addr), xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400250
Chuck Lever262965f2005-08-11 16:25:56 -0400251 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
252 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400253
Chuck Lever262965f2005-08-11 16:25:56 -0400254 if (likely(status >= (int) req->rq_slen))
255 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400256
Chuck Lever262965f2005-08-11 16:25:56 -0400257 /* Still some bytes left; set up for a retry later. */
258 if (status > 0)
259 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400260
Chuck Lever262965f2005-08-11 16:25:56 -0400261 switch (status) {
262 case -ENETUNREACH:
263 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400264 case -ECONNREFUSED:
265 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400266 * prompts ECONNREFUSED. */
Chuck Levera246b012005-08-11 16:25:23 -0400267 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400268 case -EAGAIN:
269 xs_nospace(task);
Chuck Levera246b012005-08-11 16:25:23 -0400270 break;
271 default:
Chuck Lever262965f2005-08-11 16:25:56 -0400272 dprintk("RPC: sendmsg returned unrecognized error %d\n",
273 -status);
Chuck Lever9903cd12005-08-11 16:25:26 -0400274 break;
Chuck Levera246b012005-08-11 16:25:23 -0400275 }
Chuck Lever262965f2005-08-11 16:25:56 -0400276
277 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400278}
279
Chuck Lever808012f2005-08-25 16:25:49 -0700280static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
281{
282 u32 reclen = buf->len - sizeof(rpc_fraghdr);
283 rpc_fraghdr *base = buf->head[0].iov_base;
284 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
285}
286
Chuck Lever9903cd12005-08-11 16:25:26 -0400287/**
Chuck Lever262965f2005-08-11 16:25:56 -0400288 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400289 * @task: address of RPC task that manages the state of an RPC request
290 *
291 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400292 * 0: The request has been sent
293 * EAGAIN: The socket was blocked, please call again later to
294 * complete the request
295 * ENOTCONN: Caller needs to invoke connect logic then call again
296 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400297 *
298 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400299 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400300 */
Chuck Lever262965f2005-08-11 16:25:56 -0400301static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400302{
303 struct rpc_rqst *req = task->tk_rqstp;
304 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Lever262965f2005-08-11 16:25:56 -0400305 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Levera246b012005-08-11 16:25:23 -0400306 int status, retry = 0;
307
Chuck Lever808012f2005-08-25 16:25:49 -0700308 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400309
Chuck Lever262965f2005-08-11 16:25:56 -0400310 xs_pktdump("packet data:",
311 req->rq_svec->iov_base,
312 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400313
314 /* Continue transmitting the packet/record. We must be careful
315 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400316 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400317 while (1) {
318 req->rq_xtime = jiffies;
Chuck Lever262965f2005-08-11 16:25:56 -0400319 status = xs_sendpages(xprt->sock, NULL, 0, xdr,
320 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400321
Chuck Lever262965f2005-08-11 16:25:56 -0400322 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
323 xdr->len - req->rq_bytes_sent, status);
324
325 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400326 break;
327
Chuck Lever262965f2005-08-11 16:25:56 -0400328 /* If we've sent the entire packet, immediately
329 * reset the count of bytes sent. */
330 req->rq_bytes_sent += status;
331 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
332 req->rq_bytes_sent = 0;
333 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400334 }
335
Chuck Levera246b012005-08-11 16:25:23 -0400336 status = -EAGAIN;
Chuck Lever262965f2005-08-11 16:25:56 -0400337 if (retry++ > XS_SENDMSG_RETRY)
Chuck Levera246b012005-08-11 16:25:23 -0400338 break;
339 }
340
Chuck Lever262965f2005-08-11 16:25:56 -0400341 switch (status) {
342 case -EAGAIN:
343 xs_nospace(task);
344 break;
345 case -ECONNREFUSED:
346 case -ECONNRESET:
347 case -ENOTCONN:
348 case -EPIPE:
349 status = -ENOTCONN;
350 break;
351 default:
352 dprintk("RPC: sendmsg returned unrecognized error %d\n",
353 -status);
Chuck Lever43118c22005-08-25 16:25:49 -0700354 xprt_disconnect(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400355 break;
Chuck Levera246b012005-08-11 16:25:23 -0400356 }
Chuck Lever262965f2005-08-11 16:25:56 -0400357
Chuck Levera246b012005-08-11 16:25:23 -0400358 return status;
359}
360
Chuck Lever9903cd12005-08-11 16:25:26 -0400361/**
362 * xs_close - close a socket
363 * @xprt: transport
364 *
Chuck Lever3167e122005-08-25 16:25:55 -0700365 * This is used when all requests are complete; ie, no DRC state remains
366 * on the server we want to save.
Chuck Levera246b012005-08-11 16:25:23 -0400367 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400368static void xs_close(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400369{
Chuck Lever9903cd12005-08-11 16:25:26 -0400370 struct socket *sock = xprt->sock;
371 struct sock *sk = xprt->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400372
373 if (!sk)
374 return;
375
Chuck Lever9903cd12005-08-11 16:25:26 -0400376 dprintk("RPC: xs_close xprt %p\n", xprt);
377
Chuck Levera246b012005-08-11 16:25:23 -0400378 write_lock_bh(&sk->sk_callback_lock);
379 xprt->inet = NULL;
380 xprt->sock = NULL;
381
Chuck Lever9903cd12005-08-11 16:25:26 -0400382 sk->sk_user_data = NULL;
383 sk->sk_data_ready = xprt->old_data_ready;
Chuck Levera246b012005-08-11 16:25:23 -0400384 sk->sk_state_change = xprt->old_state_change;
Chuck Lever9903cd12005-08-11 16:25:26 -0400385 sk->sk_write_space = xprt->old_write_space;
Chuck Levera246b012005-08-11 16:25:23 -0400386 write_unlock_bh(&sk->sk_callback_lock);
387
Chuck Lever9903cd12005-08-11 16:25:26 -0400388 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400389
390 sock_release(sock);
391}
392
Chuck Lever9903cd12005-08-11 16:25:26 -0400393/**
394 * xs_destroy - prepare to shutdown a transport
395 * @xprt: doomed transport
396 *
397 */
398static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400399{
Chuck Lever9903cd12005-08-11 16:25:26 -0400400 dprintk("RPC: xs_destroy xprt %p\n", xprt);
401
Chuck Lever55aa4f52005-08-11 16:25:47 -0400402 cancel_delayed_work(&xprt->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400403 flush_scheduled_work();
404
405 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400406 xs_close(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400407 kfree(xprt->slot);
408}
409
Chuck Lever9903cd12005-08-11 16:25:26 -0400410static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400411{
Chuck Lever9903cd12005-08-11 16:25:26 -0400412 return (struct rpc_xprt *) sk->sk_user_data;
413}
414
415/**
416 * xs_udp_data_ready - "data ready" callback for UDP sockets
417 * @sk: socket with data to read
418 * @len: how much data to read
419 *
420 */
421static void xs_udp_data_ready(struct sock *sk, int len)
422{
423 struct rpc_task *task;
424 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400425 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400426 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400427 int err, repsize, copied;
428 u32 _xid, *xp;
429
430 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400431 dprintk("RPC: xs_udp_data_ready...\n");
432 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400433 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400434
435 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
436 goto out;
437
438 if (xprt->shutdown)
439 goto dropit;
440
441 repsize = skb->len - sizeof(struct udphdr);
442 if (repsize < 4) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400443 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400444 goto dropit;
445 }
446
447 /* Copy the XID from the skb... */
448 xp = skb_header_pointer(skb, sizeof(struct udphdr),
449 sizeof(_xid), &_xid);
450 if (xp == NULL)
451 goto dropit;
452
453 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400454 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400455 rovr = xprt_lookup_rqst(xprt, *xp);
456 if (!rovr)
457 goto out_unlock;
458 task = rovr->rq_task;
459
Chuck Levera246b012005-08-11 16:25:23 -0400460 if ((copied = rovr->rq_private_buf.buflen) > repsize)
461 copied = repsize;
462
463 /* Suck it into the iovec, verify checksum if not done by hw. */
464 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb))
465 goto out_unlock;
466
467 /* Something worked... */
468 dst_confirm(skb->dst);
469
Chuck Lever1570c1e2005-08-25 16:25:52 -0700470 xprt_adjust_cwnd(task, copied);
471 xprt_update_rtt(task);
472 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400473
474 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400475 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400476 dropit:
477 skb_free_datagram(sk, skb);
478 out:
479 read_unlock(&sk->sk_callback_lock);
480}
481
Chuck Lever9903cd12005-08-11 16:25:26 -0400482static inline size_t xs_tcp_copy_data(skb_reader_t *desc, void *p, size_t len)
Chuck Levera246b012005-08-11 16:25:23 -0400483{
484 if (len > desc->count)
485 len = desc->count;
486 if (skb_copy_bits(desc->skb, desc->offset, p, len)) {
487 dprintk("RPC: failed to copy %zu bytes from skb. %zu bytes remain\n",
488 len, desc->count);
489 return 0;
490 }
491 desc->offset += len;
492 desc->count -= len;
493 dprintk("RPC: copied %zu bytes from skb. %zu bytes remain\n",
494 len, desc->count);
495 return len;
496}
497
Chuck Lever9903cd12005-08-11 16:25:26 -0400498static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400499{
500 size_t len, used;
501 char *p;
502
503 p = ((char *) &xprt->tcp_recm) + xprt->tcp_offset;
504 len = sizeof(xprt->tcp_recm) - xprt->tcp_offset;
Chuck Lever9903cd12005-08-11 16:25:26 -0400505 used = xs_tcp_copy_data(desc, p, len);
Chuck Levera246b012005-08-11 16:25:23 -0400506 xprt->tcp_offset += used;
507 if (used != len)
508 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700509
Chuck Levera246b012005-08-11 16:25:23 -0400510 xprt->tcp_reclen = ntohl(xprt->tcp_recm);
Chuck Lever808012f2005-08-25 16:25:49 -0700511 if (xprt->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levera246b012005-08-11 16:25:23 -0400512 xprt->tcp_flags |= XPRT_LAST_FRAG;
513 else
514 xprt->tcp_flags &= ~XPRT_LAST_FRAG;
Chuck Lever808012f2005-08-25 16:25:49 -0700515 xprt->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
516
Chuck Levera246b012005-08-11 16:25:23 -0400517 xprt->tcp_flags &= ~XPRT_COPY_RECM;
518 xprt->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700519
Chuck Levera246b012005-08-11 16:25:23 -0400520 /* Sanity check of the record length */
Chuck Lever808012f2005-08-25 16:25:49 -0700521 if (unlikely(xprt->tcp_reclen < 4)) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400522 dprintk("RPC: invalid TCP record fragment length\n");
Chuck Levera246b012005-08-11 16:25:23 -0400523 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400524 return;
Chuck Levera246b012005-08-11 16:25:23 -0400525 }
526 dprintk("RPC: reading TCP record fragment of length %d\n",
527 xprt->tcp_reclen);
528}
529
Chuck Lever9903cd12005-08-11 16:25:26 -0400530static void xs_tcp_check_recm(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400531{
532 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u, tcp_flags = %lx\n",
533 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen, xprt->tcp_flags);
534 if (xprt->tcp_offset == xprt->tcp_reclen) {
535 xprt->tcp_flags |= XPRT_COPY_RECM;
536 xprt->tcp_offset = 0;
537 if (xprt->tcp_flags & XPRT_LAST_FRAG) {
538 xprt->tcp_flags &= ~XPRT_COPY_DATA;
539 xprt->tcp_flags |= XPRT_COPY_XID;
540 xprt->tcp_copied = 0;
541 }
542 }
543}
544
Chuck Lever9903cd12005-08-11 16:25:26 -0400545static inline void xs_tcp_read_xid(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400546{
547 size_t len, used;
548 char *p;
549
550 len = sizeof(xprt->tcp_xid) - xprt->tcp_offset;
551 dprintk("RPC: reading XID (%Zu bytes)\n", len);
552 p = ((char *) &xprt->tcp_xid) + xprt->tcp_offset;
Chuck Lever9903cd12005-08-11 16:25:26 -0400553 used = xs_tcp_copy_data(desc, p, len);
Chuck Levera246b012005-08-11 16:25:23 -0400554 xprt->tcp_offset += used;
555 if (used != len)
556 return;
557 xprt->tcp_flags &= ~XPRT_COPY_XID;
558 xprt->tcp_flags |= XPRT_COPY_DATA;
559 xprt->tcp_copied = 4;
560 dprintk("RPC: reading reply for XID %08x\n",
561 ntohl(xprt->tcp_xid));
Chuck Lever9903cd12005-08-11 16:25:26 -0400562 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400563}
564
Chuck Lever9903cd12005-08-11 16:25:26 -0400565static inline void xs_tcp_read_request(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400566{
567 struct rpc_rqst *req;
568 struct xdr_buf *rcvbuf;
569 size_t len;
570 ssize_t r;
571
572 /* Find and lock the request corresponding to this xid */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400573 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400574 req = xprt_lookup_rqst(xprt, xprt->tcp_xid);
575 if (!req) {
576 xprt->tcp_flags &= ~XPRT_COPY_DATA;
577 dprintk("RPC: XID %08x request not found!\n",
578 ntohl(xprt->tcp_xid));
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400579 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400580 return;
581 }
582
583 rcvbuf = &req->rq_private_buf;
584 len = desc->count;
585 if (len > xprt->tcp_reclen - xprt->tcp_offset) {
586 skb_reader_t my_desc;
587
588 len = xprt->tcp_reclen - xprt->tcp_offset;
589 memcpy(&my_desc, desc, sizeof(my_desc));
590 my_desc.count = len;
591 r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied,
Chuck Lever9903cd12005-08-11 16:25:26 -0400592 &my_desc, xs_tcp_copy_data);
Chuck Levera246b012005-08-11 16:25:23 -0400593 desc->count -= r;
594 desc->offset += r;
595 } else
596 r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied,
Chuck Lever9903cd12005-08-11 16:25:26 -0400597 desc, xs_tcp_copy_data);
Chuck Levera246b012005-08-11 16:25:23 -0400598
599 if (r > 0) {
600 xprt->tcp_copied += r;
601 xprt->tcp_offset += r;
602 }
603 if (r != len) {
604 /* Error when copying to the receive buffer,
605 * usually because we weren't able to allocate
606 * additional buffer pages. All we can do now
607 * is turn off XPRT_COPY_DATA, so the request
608 * will not receive any additional updates,
609 * and time out.
610 * Any remaining data from this record will
611 * be discarded.
612 */
613 xprt->tcp_flags &= ~XPRT_COPY_DATA;
614 dprintk("RPC: XID %08x truncated request\n",
615 ntohl(xprt->tcp_xid));
616 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
617 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen);
618 goto out;
619 }
620
621 dprintk("RPC: XID %08x read %Zd bytes\n",
622 ntohl(xprt->tcp_xid), r);
623 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
624 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen);
625
626 if (xprt->tcp_copied == req->rq_private_buf.buflen)
627 xprt->tcp_flags &= ~XPRT_COPY_DATA;
628 else if (xprt->tcp_offset == xprt->tcp_reclen) {
629 if (xprt->tcp_flags & XPRT_LAST_FRAG)
630 xprt->tcp_flags &= ~XPRT_COPY_DATA;
631 }
632
633out:
Chuck Lever1570c1e2005-08-25 16:25:52 -0700634 if (!(xprt->tcp_flags & XPRT_COPY_DATA))
635 xprt_complete_rqst(req->rq_task, xprt->tcp_copied);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400636 spin_unlock(&xprt->transport_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400637 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400638}
639
Chuck Lever9903cd12005-08-11 16:25:26 -0400640static inline void xs_tcp_read_discard(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400641{
642 size_t len;
643
644 len = xprt->tcp_reclen - xprt->tcp_offset;
645 if (len > desc->count)
646 len = desc->count;
647 desc->count -= len;
648 desc->offset += len;
649 xprt->tcp_offset += len;
650 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever9903cd12005-08-11 16:25:26 -0400651 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400652}
653
Chuck Lever9903cd12005-08-11 16:25:26 -0400654static 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 -0400655{
656 struct rpc_xprt *xprt = rd_desc->arg.data;
657 skb_reader_t desc = {
658 .skb = skb,
659 .offset = offset,
660 .count = len,
661 .csum = 0
Chuck Lever9903cd12005-08-11 16:25:26 -0400662 };
Chuck Levera246b012005-08-11 16:25:23 -0400663
Chuck Lever9903cd12005-08-11 16:25:26 -0400664 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -0400665 do {
666 /* Read in a new fragment marker if necessary */
667 /* Can we ever really expect to get completely empty fragments? */
668 if (xprt->tcp_flags & XPRT_COPY_RECM) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400669 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400670 continue;
671 }
672 /* Read in the xid if necessary */
673 if (xprt->tcp_flags & XPRT_COPY_XID) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400674 xs_tcp_read_xid(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400675 continue;
676 }
677 /* Read in the request data */
678 if (xprt->tcp_flags & XPRT_COPY_DATA) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400679 xs_tcp_read_request(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400680 continue;
681 }
682 /* Skip over any trailing bytes on short reads */
Chuck Lever9903cd12005-08-11 16:25:26 -0400683 xs_tcp_read_discard(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400684 } while (desc.count);
Chuck Lever9903cd12005-08-11 16:25:26 -0400685 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -0400686 return len - desc.count;
687}
688
Chuck Lever9903cd12005-08-11 16:25:26 -0400689/**
690 * xs_tcp_data_ready - "data ready" callback for TCP sockets
691 * @sk: socket with data to read
692 * @bytes: how much data to read
693 *
694 */
695static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -0400696{
697 struct rpc_xprt *xprt;
698 read_descriptor_t rd_desc;
699
700 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400701 dprintk("RPC: xs_tcp_data_ready...\n");
702 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400703 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400704 if (xprt->shutdown)
705 goto out;
706
Chuck Lever9903cd12005-08-11 16:25:26 -0400707 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -0400708 rd_desc.arg.data = xprt;
709 rd_desc.count = 65536;
Chuck Lever9903cd12005-08-11 16:25:26 -0400710 tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
Chuck Levera246b012005-08-11 16:25:23 -0400711out:
712 read_unlock(&sk->sk_callback_lock);
713}
714
Chuck Lever9903cd12005-08-11 16:25:26 -0400715/**
716 * xs_tcp_state_change - callback to handle TCP socket state changes
717 * @sk: socket whose state has changed
718 *
719 */
720static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400721{
Chuck Lever9903cd12005-08-11 16:25:26 -0400722 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400723
724 read_lock(&sk->sk_callback_lock);
725 if (!(xprt = xprt_from_sock(sk)))
726 goto out;
Chuck Lever9903cd12005-08-11 16:25:26 -0400727 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400728 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
729 sk->sk_state, xprt_connected(xprt),
730 sock_flag(sk, SOCK_DEAD),
731 sock_flag(sk, SOCK_ZAPPED));
732
733 switch (sk->sk_state) {
734 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400735 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400736 if (!xprt_test_and_set_connected(xprt)) {
737 /* Reset TCP record info */
738 xprt->tcp_offset = 0;
739 xprt->tcp_reclen = 0;
740 xprt->tcp_copied = 0;
741 xprt->tcp_flags = XPRT_COPY_RECM | XPRT_COPY_XID;
Chuck Lever44fbac22005-08-11 16:25:44 -0400742 xprt_wake_pending_tasks(xprt, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400743 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400744 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400745 break;
746 case TCP_SYN_SENT:
747 case TCP_SYN_RECV:
748 break;
749 default:
750 xprt_disconnect(xprt);
751 break;
752 }
753 out:
754 read_unlock(&sk->sk_callback_lock);
755}
756
Chuck Lever9903cd12005-08-11 16:25:26 -0400757/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400758 * xs_udp_write_space - callback invoked when socket buffer space
759 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -0400760 * @sk: socket whose state has changed
761 *
Chuck Levera246b012005-08-11 16:25:23 -0400762 * Called when more output buffer space is available for this socket.
763 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400764 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -0400765 * with a bunch of small requests.
766 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400767static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400768{
Chuck Levera246b012005-08-11 16:25:23 -0400769 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400770
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400771 /* from net/core/sock.c:sock_def_write_space */
772 if (sock_writeable(sk)) {
773 struct socket *sock;
774 struct rpc_xprt *xprt;
775
776 if (unlikely(!(sock = sk->sk_socket)))
Chuck Levera246b012005-08-11 16:25:23 -0400777 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400778 if (unlikely(!(xprt = xprt_from_sock(sk))))
Chuck Levera246b012005-08-11 16:25:23 -0400779 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400780 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
781 goto out;
782
783 xprt_write_space(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400784 }
785
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400786 out:
787 read_unlock(&sk->sk_callback_lock);
788}
Chuck Levera246b012005-08-11 16:25:23 -0400789
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400790/**
791 * xs_tcp_write_space - callback invoked when socket buffer space
792 * becomes available
793 * @sk: socket whose state has changed
794 *
795 * Called when more output buffer space is available for this socket.
796 * We try not to wake our writers until they can make "significant"
797 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
798 * with a bunch of small requests.
799 */
800static void xs_tcp_write_space(struct sock *sk)
801{
802 read_lock(&sk->sk_callback_lock);
803
804 /* from net/core/stream.c:sk_stream_write_space */
805 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
806 struct socket *sock;
807 struct rpc_xprt *xprt;
808
809 if (unlikely(!(sock = sk->sk_socket)))
810 goto out;
811 if (unlikely(!(xprt = xprt_from_sock(sk))))
812 goto out;
813 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
814 goto out;
815
816 xprt_write_space(xprt);
817 }
818
819 out:
Chuck Levera246b012005-08-11 16:25:23 -0400820 read_unlock(&sk->sk_callback_lock);
821}
822
Chuck Lever9903cd12005-08-11 16:25:26 -0400823/**
Chuck Lever43118c22005-08-25 16:25:49 -0700824 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever9903cd12005-08-11 16:25:26 -0400825 * @xprt: generic transport
826 *
827 * Set socket send and receive limits based on the
828 * sndsize and rcvsize fields in the generic transport
Chuck Lever43118c22005-08-25 16:25:49 -0700829 * structure.
Chuck Levera246b012005-08-11 16:25:23 -0400830 */
Chuck Lever43118c22005-08-25 16:25:49 -0700831static void xs_udp_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400832{
833 struct sock *sk = xprt->inet;
834
Chuck Levera246b012005-08-11 16:25:23 -0400835 if (xprt->rcvsize) {
836 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
837 sk->sk_rcvbuf = xprt->rcvsize * xprt->max_reqs * 2;
838 }
839 if (xprt->sndsize) {
840 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
841 sk->sk_sndbuf = xprt->sndsize * xprt->max_reqs * 2;
842 sk->sk_write_space(sk);
843 }
844}
845
Chuck Lever43118c22005-08-25 16:25:49 -0700846/**
847 * xs_tcp_set_buffer_size - set send and receive limits
848 * @xprt: generic transport
849 *
850 * Nothing to do for TCP.
851 */
852static void xs_tcp_set_buffer_size(struct rpc_xprt *xprt)
853{
854 return;
855}
856
Chuck Lever46c0ee82005-08-25 16:25:52 -0700857/**
858 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
859 * @task: task that timed out
860 *
861 * Adjust the congestion window after a retransmit timeout has occurred.
862 */
863static void xs_udp_timer(struct rpc_task *task)
864{
865 xprt_adjust_cwnd(task, -ETIMEDOUT);
866}
867
Chuck Lever9903cd12005-08-11 16:25:26 -0400868static int xs_bindresvport(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -0400869{
870 struct sockaddr_in myaddr = {
871 .sin_family = AF_INET,
872 };
Chuck Lever529b33c2005-08-25 16:25:54 -0700873 int err;
874 unsigned short port = xprt->port;
Chuck Levera246b012005-08-11 16:25:23 -0400875
Chuck Levera246b012005-08-11 16:25:23 -0400876 do {
877 myaddr.sin_port = htons(port);
878 err = sock->ops->bind(sock, (struct sockaddr *) &myaddr,
879 sizeof(myaddr));
880 if (err == 0) {
881 xprt->port = port;
Chuck Lever9903cd12005-08-11 16:25:26 -0400882 dprintk("RPC: xs_bindresvport bound to port %u\n",
883 port);
Chuck Levera246b012005-08-11 16:25:23 -0400884 return 0;
885 }
Chuck Lever529b33c2005-08-25 16:25:54 -0700886 if (port <= xprt_min_resvport)
887 port = xprt_max_resvport;
888 else
889 port--;
Chuck Levera246b012005-08-11 16:25:23 -0400890 } while (err == -EADDRINUSE && port != xprt->port);
891
Chuck Lever9903cd12005-08-11 16:25:26 -0400892 dprintk("RPC: can't bind to reserved port (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -0400893 return err;
894}
895
Chuck Lever9903cd12005-08-11 16:25:26 -0400896/**
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400897 * xs_udp_connect_worker - set up a UDP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400898 * @args: RPC transport to connect
899 *
900 * Invoked by a work queue tasklet.
Chuck Levera246b012005-08-11 16:25:23 -0400901 */
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400902static void xs_udp_connect_worker(void *args)
Chuck Levera246b012005-08-11 16:25:23 -0400903{
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400904 struct rpc_xprt *xprt = (struct rpc_xprt *) args;
Chuck Levera246b012005-08-11 16:25:23 -0400905 struct socket *sock = xprt->sock;
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400906 int err, status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -0400907
908 if (xprt->shutdown || xprt->addr.sin_port == 0)
909 goto out;
910
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400911 dprintk("RPC: xs_udp_connect_worker for xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400912
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400913 /* Start by resetting any existing state */
Chuck Lever9903cd12005-08-11 16:25:26 -0400914 xs_close(xprt);
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400915
916 if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
917 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -0400918 goto out;
919 }
Chuck Levera246b012005-08-11 16:25:23 -0400920
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400921 if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) {
922 sock_release(sock);
923 goto out;
924 }
925
926 if (!xprt->inet) {
927 struct sock *sk = sock->sk;
928
929 write_lock_bh(&sk->sk_callback_lock);
930
931 sk->sk_user_data = xprt;
932 xprt->old_data_ready = sk->sk_data_ready;
933 xprt->old_state_change = sk->sk_state_change;
934 xprt->old_write_space = sk->sk_write_space;
935 sk->sk_data_ready = xs_udp_data_ready;
936 sk->sk_write_space = xs_udp_write_space;
937 sk->sk_no_check = UDP_CSUM_NORCV;
938
939 xprt_set_connected(xprt);
940
941 /* Reset to new socket */
942 xprt->sock = sock;
943 xprt->inet = sk;
944
945 write_unlock_bh(&sk->sk_callback_lock);
946 }
Chuck Lever43118c22005-08-25 16:25:49 -0700947 xs_udp_set_buffer_size(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400948 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400949out:
950 xprt_wake_pending_tasks(xprt, status);
951 xprt_clear_connecting(xprt);
952}
953
Chuck Lever3167e122005-08-25 16:25:55 -0700954/*
955 * We need to preserve the port number so the reply cache on the server can
956 * find our cached RPC replies when we get around to reconnecting.
957 */
958static void xs_tcp_reuse_connection(struct rpc_xprt *xprt)
959{
960 int result;
961 struct socket *sock = xprt->sock;
962 struct sockaddr any;
963
964 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
965
966 /*
967 * Disconnect the transport socket by doing a connect operation
968 * with AF_UNSPEC. This should return immediately...
969 */
970 memset(&any, 0, sizeof(any));
971 any.sa_family = AF_UNSPEC;
972 result = sock->ops->connect(sock, &any, sizeof(any), 0);
973 if (result)
974 dprintk("RPC: AF_UNSPEC connect return code %d\n",
975 result);
976}
977
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400978/**
979 * xs_tcp_connect_worker - connect a TCP socket to a remote endpoint
980 * @args: RPC transport to connect
981 *
982 * Invoked by a work queue tasklet.
983 */
984static void xs_tcp_connect_worker(void *args)
985{
986 struct rpc_xprt *xprt = (struct rpc_xprt *)args;
987 struct socket *sock = xprt->sock;
988 int err, status = -EIO;
989
990 if (xprt->shutdown || xprt->addr.sin_port == 0)
Chuck Levera246b012005-08-11 16:25:23 -0400991 goto out;
992
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400993 dprintk("RPC: xs_tcp_connect_worker for xprt %p\n", xprt);
994
Chuck Lever3167e122005-08-25 16:25:55 -0700995 if (!xprt->sock) {
996 /* start from scratch */
997 if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
998 dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
999 goto out;
1000 }
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001001
Chuck Lever3167e122005-08-25 16:25:55 -07001002 if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) {
1003 sock_release(sock);
1004 goto out;
1005 }
1006 } else
1007 /* "close" the socket, preserving the local port */
1008 xs_tcp_reuse_connection(xprt);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001009
1010 if (!xprt->inet) {
1011 struct sock *sk = sock->sk;
1012
1013 write_lock_bh(&sk->sk_callback_lock);
1014
1015 sk->sk_user_data = xprt;
1016 xprt->old_data_ready = sk->sk_data_ready;
1017 xprt->old_state_change = sk->sk_state_change;
1018 xprt->old_write_space = sk->sk_write_space;
1019 sk->sk_data_ready = xs_tcp_data_ready;
1020 sk->sk_state_change = xs_tcp_state_change;
1021 sk->sk_write_space = xs_tcp_write_space;
Chuck Lever3167e122005-08-25 16:25:55 -07001022
1023 /* socket options */
1024 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1025 sock_reset_flag(sk, SOCK_LINGER);
1026 tcp_sk(sk)->linger2 = 0;
1027 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001028
1029 xprt_clear_connected(xprt);
1030
1031 /* Reset to new socket */
1032 xprt->sock = sock;
1033 xprt->inet = sk;
1034
1035 write_unlock_bh(&sk->sk_callback_lock);
1036 }
1037
1038 /* Tell the socket layer to start connecting... */
Chuck Levera246b012005-08-11 16:25:23 -04001039 status = sock->ops->connect(sock, (struct sockaddr *) &xprt->addr,
1040 sizeof(xprt->addr), O_NONBLOCK);
1041 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1042 xprt, -status, xprt_connected(xprt), sock->sk->sk_state);
1043 if (status < 0) {
1044 switch (status) {
1045 case -EINPROGRESS:
1046 case -EALREADY:
1047 goto out_clear;
Chuck Lever3167e122005-08-25 16:25:55 -07001048 case -ECONNREFUSED:
1049 case -ECONNRESET:
1050 /* retry with existing socket, after a delay */
1051 break;
1052 default:
1053 /* get rid of existing socket, and retry */
1054 xs_close(xprt);
1055 break;
Chuck Levera246b012005-08-11 16:25:23 -04001056 }
1057 }
1058out:
Chuck Lever44fbac22005-08-11 16:25:44 -04001059 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001060out_clear:
Chuck Lever2226feb2005-08-11 16:25:38 -04001061 xprt_clear_connecting(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001062}
1063
Chuck Lever9903cd12005-08-11 16:25:26 -04001064/**
1065 * xs_connect - connect a socket to a remote endpoint
1066 * @task: address of RPC task that manages state of connect request
1067 *
1068 * TCP: If the remote end dropped the connection, delay reconnecting.
1069 */
1070static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04001071{
1072 struct rpc_xprt *xprt = task->tk_xprt;
1073
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001074 if (xprt_test_and_set_connecting(xprt))
1075 return;
1076
1077 if (xprt->sock != NULL) {
1078 dprintk("RPC: xs_connect delayed xprt %p\n", xprt);
1079 schedule_delayed_work(&xprt->connect_worker,
Chuck Levera246b012005-08-11 16:25:23 -04001080 RPC_REESTABLISH_TIMEOUT);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001081 } else {
1082 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
1083 schedule_work(&xprt->connect_worker);
1084
1085 /* flush_scheduled_work can sleep... */
1086 if (!RPC_IS_ASYNC(task))
1087 flush_scheduled_work();
Chuck Levera246b012005-08-11 16:25:23 -04001088 }
1089}
1090
Chuck Lever262965f2005-08-11 16:25:56 -04001091static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07001092 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07001093 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07001094 .release_xprt = xprt_release_xprt_cong,
Chuck Lever9903cd12005-08-11 16:25:26 -04001095 .connect = xs_connect,
Chuck Lever262965f2005-08-11 16:25:56 -04001096 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001097 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07001098 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07001099 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04001100 .close = xs_close,
1101 .destroy = xs_destroy,
1102};
1103
1104static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07001105 .set_buffer_size = xs_tcp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07001106 .reserve_xprt = xprt_reserve_xprt,
Chuck Lever49e9a892005-08-25 16:25:51 -07001107 .release_xprt = xprt_release_xprt,
Chuck Lever262965f2005-08-11 16:25:56 -04001108 .connect = xs_connect,
1109 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001110 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Chuck Lever9903cd12005-08-11 16:25:26 -04001111 .close = xs_close,
1112 .destroy = xs_destroy,
Chuck Levera246b012005-08-11 16:25:23 -04001113};
1114
Chuck Lever9903cd12005-08-11 16:25:26 -04001115/**
1116 * xs_setup_udp - Set up transport to use a UDP socket
1117 * @xprt: transport to set up
1118 * @to: timeout parameters
1119 *
1120 */
Chuck Levera246b012005-08-11 16:25:23 -04001121int xs_setup_udp(struct rpc_xprt *xprt, struct rpc_timeout *to)
1122{
1123 size_t slot_table_size;
1124
1125 dprintk("RPC: setting up udp-ipv4 transport...\n");
1126
1127 xprt->max_reqs = xprt_udp_slot_table_entries;
1128 slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]);
1129 xprt->slot = kmalloc(slot_table_size, GFP_KERNEL);
1130 if (xprt->slot == NULL)
1131 return -ENOMEM;
1132 memset(xprt->slot, 0, slot_table_size);
1133
1134 xprt->prot = IPPROTO_UDP;
Chuck Lever529b33c2005-08-25 16:25:54 -07001135 xprt->port = xprt_max_resvport;
Chuck Lever808012f2005-08-25 16:25:49 -07001136 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001137 xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0;
1138 /* XXX: header size can vary due to auth type, IPv6, etc. */
1139 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
1140
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001141 INIT_WORK(&xprt->connect_worker, xs_udp_connect_worker, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001142
Chuck Lever262965f2005-08-11 16:25:56 -04001143 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001144
1145 if (to)
1146 xprt->timeout = *to;
1147 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001148 xprt_set_timeout(&xprt->timeout, 5, 5 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001149
1150 return 0;
1151}
1152
Chuck Lever9903cd12005-08-11 16:25:26 -04001153/**
1154 * xs_setup_tcp - Set up transport to use a TCP socket
1155 * @xprt: transport to set up
1156 * @to: timeout parameters
1157 *
1158 */
Chuck Levera246b012005-08-11 16:25:23 -04001159int xs_setup_tcp(struct rpc_xprt *xprt, struct rpc_timeout *to)
1160{
1161 size_t slot_table_size;
1162
1163 dprintk("RPC: setting up tcp-ipv4 transport...\n");
1164
1165 xprt->max_reqs = xprt_tcp_slot_table_entries;
1166 slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]);
1167 xprt->slot = kmalloc(slot_table_size, GFP_KERNEL);
1168 if (xprt->slot == NULL)
1169 return -ENOMEM;
1170 memset(xprt->slot, 0, slot_table_size);
1171
1172 xprt->prot = IPPROTO_TCP;
Chuck Lever529b33c2005-08-25 16:25:54 -07001173 xprt->port = xprt_max_resvport;
Chuck Lever808012f2005-08-25 16:25:49 -07001174 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Levera246b012005-08-11 16:25:23 -04001175 xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0;
Chuck Lever808012f2005-08-25 16:25:49 -07001176 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04001177
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001178 INIT_WORK(&xprt->connect_worker, xs_tcp_connect_worker, xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001179
Chuck Lever262965f2005-08-11 16:25:56 -04001180 xprt->ops = &xs_tcp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001181
1182 if (to)
1183 xprt->timeout = *to;
1184 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001185 xprt_set_timeout(&xprt->timeout, 2, 60 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001186
1187 return 0;
1188}