blob: 0a51fd46a848d03d81a57b81383d0309d7f4fa30 [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 Leverc556b752005-11-01 12:24:48 -050039 * xprtsock tunables
40 */
41unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
42unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
43
44unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
45unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
46
47/*
Chuck Lever262965f2005-08-11 16:25:56 -040048 * How many times to try sending a request on a socket before waiting
49 * for the socket buffer to clear.
50 */
51#define XS_SENDMSG_RETRY (10U)
52
Chuck Lever03bf4b72005-08-25 16:25:55 -070053/*
54 * Time out for an RPC UDP socket connect. UDP socket connects are
55 * synchronous, but we set a timeout anyway in case of resource
56 * exhaustion on the local host.
57 */
58#define XS_UDP_CONN_TO (5U * HZ)
59
60/*
61 * Wait duration for an RPC TCP connection to be established. Solaris
62 * NFS over TCP uses 60 seconds, for example, which is in line with how
63 * long a server takes to reboot.
64 */
65#define XS_TCP_CONN_TO (60U * HZ)
66
67/*
68 * Wait duration for a reply from the RPC portmapper.
69 */
70#define XS_BIND_TO (60U * HZ)
71
72/*
73 * Delay if a UDP socket connect error occurs. This is most likely some
74 * kind of resource problem on the local host.
75 */
76#define XS_UDP_REEST_TO (2U * HZ)
77
78/*
79 * The reestablish timeout allows clients to delay for a bit before attempting
80 * to reconnect to a server that just dropped our connection.
81 *
82 * We implement an exponential backoff when trying to reestablish a TCP
83 * transport connection with the server. Some servers like to drop a TCP
84 * connection when they are overworked, so we start with a short timeout and
85 * increase over time if the server is down or not responding.
86 */
87#define XS_TCP_INIT_REEST_TO (3U * HZ)
88#define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
89
90/*
91 * TCP idle timeout; client drops the transport socket if it is idle
92 * for this long. Note that we also timeout UDP sockets to prevent
93 * holding port numbers when there is no RPC traffic.
94 */
95#define XS_IDLE_DISC_TO (5U * 60 * HZ)
96
Chuck Levera246b012005-08-11 16:25:23 -040097#ifdef RPC_DEBUG
98# undef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -040099# define RPCDBG_FACILITY RPCDBG_TRANS
Chuck Levera246b012005-08-11 16:25:23 -0400100#endif
101
Chuck Levera246b012005-08-11 16:25:23 -0400102#ifdef RPC_DEBUG_DATA
Chuck Lever9903cd12005-08-11 16:25:26 -0400103static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400104{
Chuck Lever9903cd12005-08-11 16:25:26 -0400105 u8 *buf = (u8 *) packet;
106 int j;
Chuck Levera246b012005-08-11 16:25:23 -0400107
108 dprintk("RPC: %s\n", msg);
109 for (j = 0; j < count && j < 128; j += 4) {
110 if (!(j & 31)) {
111 if (j)
112 dprintk("\n");
113 dprintk("0x%04x ", j);
114 }
115 dprintk("%02x%02x%02x%02x ",
116 buf[j], buf[j+1], buf[j+2], buf[j+3]);
117 }
118 dprintk("\n");
119}
120#else
Chuck Lever9903cd12005-08-11 16:25:26 -0400121static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
Chuck Levera246b012005-08-11 16:25:23 -0400122{
123 /* NOP */
124}
125#endif
126
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400127#define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
128
129static inline int xs_send_head(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, unsigned int len)
130{
131 struct kvec iov = {
132 .iov_base = xdr->head[0].iov_base + base,
133 .iov_len = len - base,
134 };
135 struct msghdr msg = {
136 .msg_name = addr,
137 .msg_namelen = addrlen,
138 .msg_flags = XS_SENDMSG_FLAGS,
139 };
140
141 if (xdr->len > len)
142 msg.msg_flags |= MSG_MORE;
143
144 if (likely(iov.iov_len))
145 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
146 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
147}
148
149static int xs_send_tail(struct socket *sock, struct xdr_buf *xdr, unsigned int base, unsigned int len)
150{
151 struct kvec iov = {
152 .iov_base = xdr->tail[0].iov_base + base,
153 .iov_len = len - base,
154 };
155 struct msghdr msg = {
156 .msg_flags = XS_SENDMSG_FLAGS,
157 };
158
159 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
160}
161
Chuck Lever9903cd12005-08-11 16:25:26 -0400162/**
163 * xs_sendpages - write pages directly to a socket
164 * @sock: socket to send on
165 * @addr: UDP only -- address of destination
166 * @addrlen: UDP only -- length of destination address
167 * @xdr: buffer containing this request
168 * @base: starting position in the buffer
169 *
Chuck Levera246b012005-08-11 16:25:23 -0400170 */
Chuck Lever262965f2005-08-11 16:25:56 -0400171static 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 -0400172{
173 struct page **ppage = xdr->pages;
174 unsigned int len, pglen = xdr->page_len;
175 int err, ret = 0;
176 ssize_t (*sendpage)(struct socket *, struct page *, int, size_t, int);
177
Chuck Lever262965f2005-08-11 16:25:56 -0400178 if (unlikely(!sock))
179 return -ENOTCONN;
180
181 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
182
Chuck Levera246b012005-08-11 16:25:23 -0400183 len = xdr->head[0].iov_len;
184 if (base < len || (addr != NULL && base == 0)) {
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400185 err = xs_send_head(sock, addr, addrlen, 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;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400190 if (err != (len - base))
Chuck Levera246b012005-08-11 16:25:23 -0400191 goto out;
192 base = 0;
193 } else
194 base -= len;
195
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400196 if (unlikely(pglen == 0))
Chuck Levera246b012005-08-11 16:25:23 -0400197 goto copy_tail;
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400198 if (unlikely(base >= pglen)) {
Chuck Levera246b012005-08-11 16:25:23 -0400199 base -= pglen;
200 goto copy_tail;
201 }
202 if (base || xdr->page_base) {
203 pglen -= base;
Chuck Lever9903cd12005-08-11 16:25:26 -0400204 base += xdr->page_base;
Chuck Levera246b012005-08-11 16:25:23 -0400205 ppage += base >> PAGE_CACHE_SHIFT;
206 base &= ~PAGE_CACHE_MASK;
207 }
208
209 sendpage = sock->ops->sendpage ? : sock_no_sendpage;
210 do {
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400211 int flags = XS_SENDMSG_FLAGS;
Chuck Levera246b012005-08-11 16:25:23 -0400212
213 len = PAGE_CACHE_SIZE;
214 if (base)
215 len -= base;
216 if (pglen < len)
217 len = pglen;
218
219 if (pglen != len || xdr->tail[0].iov_len != 0)
220 flags |= MSG_MORE;
221
222 /* Hmm... We might be dealing with highmem pages */
223 if (PageHighMem(*ppage))
224 sendpage = sock_no_sendpage;
225 err = sendpage(sock, *ppage, base, len, flags);
226 if (ret == 0)
227 ret = err;
228 else if (err > 0)
229 ret += err;
230 if (err != len)
231 goto out;
232 base = 0;
233 ppage++;
234 } while ((pglen -= len) != 0);
235copy_tail:
236 len = xdr->tail[0].iov_len;
237 if (base < len) {
Chuck Leverb4b5cc82005-08-11 16:25:29 -0400238 err = xs_send_tail(sock, xdr, base, len);
Chuck Levera246b012005-08-11 16:25:23 -0400239 if (ret == 0)
240 ret = err;
241 else if (err > 0)
242 ret += err;
243 }
244out:
245 return ret;
246}
247
Chuck Lever9903cd12005-08-11 16:25:26 -0400248/**
Chuck Lever262965f2005-08-11 16:25:56 -0400249 * xs_nospace - place task on wait queue if transmit was incomplete
250 * @task: task to put to sleep
Chuck Lever9903cd12005-08-11 16:25:26 -0400251 *
Chuck Levera246b012005-08-11 16:25:23 -0400252 */
Chuck Lever262965f2005-08-11 16:25:56 -0400253static void xs_nospace(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400254{
Chuck Lever262965f2005-08-11 16:25:56 -0400255 struct rpc_rqst *req = task->tk_rqstp;
256 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400257
Chuck Lever262965f2005-08-11 16:25:56 -0400258 dprintk("RPC: %4d xmit incomplete (%u left of %u)\n",
259 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
260 req->rq_slen);
261
262 if (test_bit(SOCK_ASYNC_NOSPACE, &xprt->sock->flags)) {
263 /* Protect against races with write_space */
264 spin_lock_bh(&xprt->transport_lock);
265
266 /* Don't race with disconnect */
267 if (!xprt_connected(xprt))
268 task->tk_status = -ENOTCONN;
269 else if (test_bit(SOCK_NOSPACE, &xprt->sock->flags))
270 xprt_wait_for_buffer_space(task);
271
272 spin_unlock_bh(&xprt->transport_lock);
273 } else
274 /* Keep holding the socket if it is blocked */
275 rpc_delay(task, HZ>>4);
276}
277
278/**
279 * xs_udp_send_request - write an RPC request to a UDP socket
280 * @task: address of RPC task that manages the state of an RPC request
281 *
282 * Return values:
283 * 0: The request has been sent
284 * EAGAIN: The socket was blocked, please call again later to
285 * complete the request
286 * ENOTCONN: Caller needs to invoke connect logic then call again
287 * other: Some other error occured, the request was not sent
288 */
289static int xs_udp_send_request(struct rpc_task *task)
290{
291 struct rpc_rqst *req = task->tk_rqstp;
292 struct rpc_xprt *xprt = req->rq_xprt;
293 struct xdr_buf *xdr = &req->rq_snd_buf;
294 int status;
Chuck Levera246b012005-08-11 16:25:23 -0400295
Chuck Lever9903cd12005-08-11 16:25:26 -0400296 xs_pktdump("packet data:",
Chuck Levera246b012005-08-11 16:25:23 -0400297 req->rq_svec->iov_base,
298 req->rq_svec->iov_len);
299
Chuck Lever262965f2005-08-11 16:25:56 -0400300 req->rq_xtime = jiffies;
301 status = xs_sendpages(xprt->sock, (struct sockaddr *) &xprt->addr,
302 sizeof(xprt->addr), xdr, req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400303
Chuck Lever262965f2005-08-11 16:25:56 -0400304 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
305 xdr->len - req->rq_bytes_sent, status);
Chuck Levera246b012005-08-11 16:25:23 -0400306
Chuck Lever262965f2005-08-11 16:25:56 -0400307 if (likely(status >= (int) req->rq_slen))
308 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400309
Chuck Lever262965f2005-08-11 16:25:56 -0400310 /* Still some bytes left; set up for a retry later. */
311 if (status > 0)
312 status = -EAGAIN;
Chuck Levera246b012005-08-11 16:25:23 -0400313
Chuck Lever262965f2005-08-11 16:25:56 -0400314 switch (status) {
315 case -ENETUNREACH:
316 case -EPIPE:
Chuck Levera246b012005-08-11 16:25:23 -0400317 case -ECONNREFUSED:
318 /* When the server has died, an ICMP port unreachable message
Chuck Lever9903cd12005-08-11 16:25:26 -0400319 * prompts ECONNREFUSED. */
Chuck Levera246b012005-08-11 16:25:23 -0400320 break;
Chuck Lever262965f2005-08-11 16:25:56 -0400321 case -EAGAIN:
322 xs_nospace(task);
Chuck Levera246b012005-08-11 16:25:23 -0400323 break;
324 default:
Chuck Lever262965f2005-08-11 16:25:56 -0400325 dprintk("RPC: sendmsg returned unrecognized error %d\n",
326 -status);
Chuck Lever9903cd12005-08-11 16:25:26 -0400327 break;
Chuck Levera246b012005-08-11 16:25:23 -0400328 }
Chuck Lever262965f2005-08-11 16:25:56 -0400329
330 return status;
Chuck Levera246b012005-08-11 16:25:23 -0400331}
332
Chuck Lever808012f2005-08-25 16:25:49 -0700333static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
334{
335 u32 reclen = buf->len - sizeof(rpc_fraghdr);
336 rpc_fraghdr *base = buf->head[0].iov_base;
337 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
338}
339
Chuck Lever9903cd12005-08-11 16:25:26 -0400340/**
Chuck Lever262965f2005-08-11 16:25:56 -0400341 * xs_tcp_send_request - write an RPC request to a TCP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400342 * @task: address of RPC task that manages the state of an RPC request
343 *
344 * Return values:
Chuck Lever262965f2005-08-11 16:25:56 -0400345 * 0: The request has been sent
346 * EAGAIN: The socket was blocked, please call again later to
347 * complete the request
348 * ENOTCONN: Caller needs to invoke connect logic then call again
349 * other: Some other error occured, the request was not sent
Chuck Lever9903cd12005-08-11 16:25:26 -0400350 *
351 * XXX: In the case of soft timeouts, should we eventually give up
Chuck Lever262965f2005-08-11 16:25:56 -0400352 * if sendmsg is not able to make progress?
Chuck Lever9903cd12005-08-11 16:25:26 -0400353 */
Chuck Lever262965f2005-08-11 16:25:56 -0400354static int xs_tcp_send_request(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -0400355{
356 struct rpc_rqst *req = task->tk_rqstp;
357 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Lever262965f2005-08-11 16:25:56 -0400358 struct xdr_buf *xdr = &req->rq_snd_buf;
Chuck Levera246b012005-08-11 16:25:23 -0400359 int status, retry = 0;
360
Chuck Lever808012f2005-08-25 16:25:49 -0700361 xs_encode_tcp_record_marker(&req->rq_snd_buf);
Chuck Levera246b012005-08-11 16:25:23 -0400362
Chuck Lever262965f2005-08-11 16:25:56 -0400363 xs_pktdump("packet data:",
364 req->rq_svec->iov_base,
365 req->rq_svec->iov_len);
Chuck Levera246b012005-08-11 16:25:23 -0400366
367 /* Continue transmitting the packet/record. We must be careful
368 * to cope with writespace callbacks arriving _after_ we have
Chuck Lever262965f2005-08-11 16:25:56 -0400369 * called sendmsg(). */
Chuck Levera246b012005-08-11 16:25:23 -0400370 while (1) {
371 req->rq_xtime = jiffies;
Chuck Lever262965f2005-08-11 16:25:56 -0400372 status = xs_sendpages(xprt->sock, NULL, 0, xdr,
373 req->rq_bytes_sent);
Chuck Levera246b012005-08-11 16:25:23 -0400374
Chuck Lever262965f2005-08-11 16:25:56 -0400375 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
376 xdr->len - req->rq_bytes_sent, status);
377
378 if (unlikely(status < 0))
Chuck Levera246b012005-08-11 16:25:23 -0400379 break;
380
Chuck Lever262965f2005-08-11 16:25:56 -0400381 /* If we've sent the entire packet, immediately
382 * reset the count of bytes sent. */
383 req->rq_bytes_sent += status;
384 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
385 req->rq_bytes_sent = 0;
386 return 0;
Chuck Levera246b012005-08-11 16:25:23 -0400387 }
388
Chuck Levera246b012005-08-11 16:25:23 -0400389 status = -EAGAIN;
Chuck Lever262965f2005-08-11 16:25:56 -0400390 if (retry++ > XS_SENDMSG_RETRY)
Chuck Levera246b012005-08-11 16:25:23 -0400391 break;
392 }
393
Chuck Lever262965f2005-08-11 16:25:56 -0400394 switch (status) {
395 case -EAGAIN:
396 xs_nospace(task);
397 break;
398 case -ECONNREFUSED:
399 case -ECONNRESET:
400 case -ENOTCONN:
401 case -EPIPE:
402 status = -ENOTCONN;
403 break;
404 default:
405 dprintk("RPC: sendmsg returned unrecognized error %d\n",
406 -status);
Chuck Lever43118c22005-08-25 16:25:49 -0700407 xprt_disconnect(xprt);
Chuck Lever262965f2005-08-11 16:25:56 -0400408 break;
Chuck Levera246b012005-08-11 16:25:23 -0400409 }
Chuck Lever262965f2005-08-11 16:25:56 -0400410
Chuck Levera246b012005-08-11 16:25:23 -0400411 return status;
412}
413
Chuck Lever9903cd12005-08-11 16:25:26 -0400414/**
415 * xs_close - close a socket
416 * @xprt: transport
417 *
Chuck Lever3167e122005-08-25 16:25:55 -0700418 * This is used when all requests are complete; ie, no DRC state remains
419 * on the server we want to save.
Chuck Levera246b012005-08-11 16:25:23 -0400420 */
Chuck Lever9903cd12005-08-11 16:25:26 -0400421static void xs_close(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400422{
Chuck Lever9903cd12005-08-11 16:25:26 -0400423 struct socket *sock = xprt->sock;
424 struct sock *sk = xprt->inet;
Chuck Levera246b012005-08-11 16:25:23 -0400425
426 if (!sk)
427 return;
428
Chuck Lever9903cd12005-08-11 16:25:26 -0400429 dprintk("RPC: xs_close xprt %p\n", xprt);
430
Chuck Levera246b012005-08-11 16:25:23 -0400431 write_lock_bh(&sk->sk_callback_lock);
432 xprt->inet = NULL;
433 xprt->sock = NULL;
434
Chuck Lever9903cd12005-08-11 16:25:26 -0400435 sk->sk_user_data = NULL;
436 sk->sk_data_ready = xprt->old_data_ready;
Chuck Levera246b012005-08-11 16:25:23 -0400437 sk->sk_state_change = xprt->old_state_change;
Chuck Lever9903cd12005-08-11 16:25:26 -0400438 sk->sk_write_space = xprt->old_write_space;
Chuck Levera246b012005-08-11 16:25:23 -0400439 write_unlock_bh(&sk->sk_callback_lock);
440
Chuck Lever9903cd12005-08-11 16:25:26 -0400441 sk->sk_no_check = 0;
Chuck Levera246b012005-08-11 16:25:23 -0400442
443 sock_release(sock);
444}
445
Chuck Lever9903cd12005-08-11 16:25:26 -0400446/**
447 * xs_destroy - prepare to shutdown a transport
448 * @xprt: doomed transport
449 *
450 */
451static void xs_destroy(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400452{
Chuck Lever9903cd12005-08-11 16:25:26 -0400453 dprintk("RPC: xs_destroy xprt %p\n", xprt);
454
Chuck Lever55aa4f52005-08-11 16:25:47 -0400455 cancel_delayed_work(&xprt->connect_worker);
Chuck Levera246b012005-08-11 16:25:23 -0400456 flush_scheduled_work();
457
458 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400459 xs_close(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400460 kfree(xprt->slot);
461}
462
Chuck Lever9903cd12005-08-11 16:25:26 -0400463static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400464{
Chuck Lever9903cd12005-08-11 16:25:26 -0400465 return (struct rpc_xprt *) sk->sk_user_data;
466}
467
468/**
469 * xs_udp_data_ready - "data ready" callback for UDP sockets
470 * @sk: socket with data to read
471 * @len: how much data to read
472 *
473 */
474static void xs_udp_data_ready(struct sock *sk, int len)
475{
476 struct rpc_task *task;
477 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400478 struct rpc_rqst *rovr;
Chuck Lever9903cd12005-08-11 16:25:26 -0400479 struct sk_buff *skb;
Chuck Levera246b012005-08-11 16:25:23 -0400480 int err, repsize, copied;
481 u32 _xid, *xp;
482
483 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400484 dprintk("RPC: xs_udp_data_ready...\n");
485 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400486 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400487
488 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
489 goto out;
490
491 if (xprt->shutdown)
492 goto dropit;
493
494 repsize = skb->len - sizeof(struct udphdr);
495 if (repsize < 4) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400496 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
Chuck Levera246b012005-08-11 16:25:23 -0400497 goto dropit;
498 }
499
500 /* Copy the XID from the skb... */
501 xp = skb_header_pointer(skb, sizeof(struct udphdr),
502 sizeof(_xid), &_xid);
503 if (xp == NULL)
504 goto dropit;
505
506 /* Look up and lock the request corresponding to the given XID */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400507 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400508 rovr = xprt_lookup_rqst(xprt, *xp);
509 if (!rovr)
510 goto out_unlock;
511 task = rovr->rq_task;
512
Chuck Levera246b012005-08-11 16:25:23 -0400513 if ((copied = rovr->rq_private_buf.buflen) > repsize)
514 copied = repsize;
515
516 /* Suck it into the iovec, verify checksum if not done by hw. */
517 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb))
518 goto out_unlock;
519
520 /* Something worked... */
521 dst_confirm(skb->dst);
522
Chuck Lever1570c1e2005-08-25 16:25:52 -0700523 xprt_adjust_cwnd(task, copied);
524 xprt_update_rtt(task);
525 xprt_complete_rqst(task, copied);
Chuck Levera246b012005-08-11 16:25:23 -0400526
527 out_unlock:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400528 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400529 dropit:
530 skb_free_datagram(sk, skb);
531 out:
532 read_unlock(&sk->sk_callback_lock);
533}
534
Chuck Lever9903cd12005-08-11 16:25:26 -0400535static inline size_t xs_tcp_copy_data(skb_reader_t *desc, void *p, size_t len)
Chuck Levera246b012005-08-11 16:25:23 -0400536{
537 if (len > desc->count)
538 len = desc->count;
539 if (skb_copy_bits(desc->skb, desc->offset, p, len)) {
540 dprintk("RPC: failed to copy %zu bytes from skb. %zu bytes remain\n",
541 len, desc->count);
542 return 0;
543 }
544 desc->offset += len;
545 desc->count -= len;
546 dprintk("RPC: copied %zu bytes from skb. %zu bytes remain\n",
547 len, desc->count);
548 return len;
549}
550
Chuck Lever9903cd12005-08-11 16:25:26 -0400551static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400552{
553 size_t len, used;
554 char *p;
555
556 p = ((char *) &xprt->tcp_recm) + xprt->tcp_offset;
557 len = sizeof(xprt->tcp_recm) - xprt->tcp_offset;
Chuck Lever9903cd12005-08-11 16:25:26 -0400558 used = xs_tcp_copy_data(desc, p, len);
Chuck Levera246b012005-08-11 16:25:23 -0400559 xprt->tcp_offset += used;
560 if (used != len)
561 return;
Chuck Lever808012f2005-08-25 16:25:49 -0700562
Chuck Levera246b012005-08-11 16:25:23 -0400563 xprt->tcp_reclen = ntohl(xprt->tcp_recm);
Chuck Lever808012f2005-08-25 16:25:49 -0700564 if (xprt->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
Chuck Levera246b012005-08-11 16:25:23 -0400565 xprt->tcp_flags |= XPRT_LAST_FRAG;
566 else
567 xprt->tcp_flags &= ~XPRT_LAST_FRAG;
Chuck Lever808012f2005-08-25 16:25:49 -0700568 xprt->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
569
Chuck Levera246b012005-08-11 16:25:23 -0400570 xprt->tcp_flags &= ~XPRT_COPY_RECM;
571 xprt->tcp_offset = 0;
Chuck Lever808012f2005-08-25 16:25:49 -0700572
Chuck Levera246b012005-08-11 16:25:23 -0400573 /* Sanity check of the record length */
Chuck Lever808012f2005-08-25 16:25:49 -0700574 if (unlikely(xprt->tcp_reclen < 4)) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400575 dprintk("RPC: invalid TCP record fragment length\n");
Chuck Levera246b012005-08-11 16:25:23 -0400576 xprt_disconnect(xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400577 return;
Chuck Levera246b012005-08-11 16:25:23 -0400578 }
579 dprintk("RPC: reading TCP record fragment of length %d\n",
580 xprt->tcp_reclen);
581}
582
Chuck Lever9903cd12005-08-11 16:25:26 -0400583static void xs_tcp_check_recm(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400584{
585 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u, tcp_flags = %lx\n",
586 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen, xprt->tcp_flags);
587 if (xprt->tcp_offset == xprt->tcp_reclen) {
588 xprt->tcp_flags |= XPRT_COPY_RECM;
589 xprt->tcp_offset = 0;
590 if (xprt->tcp_flags & XPRT_LAST_FRAG) {
591 xprt->tcp_flags &= ~XPRT_COPY_DATA;
592 xprt->tcp_flags |= XPRT_COPY_XID;
593 xprt->tcp_copied = 0;
594 }
595 }
596}
597
Chuck Lever9903cd12005-08-11 16:25:26 -0400598static inline void xs_tcp_read_xid(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400599{
600 size_t len, used;
601 char *p;
602
603 len = sizeof(xprt->tcp_xid) - xprt->tcp_offset;
604 dprintk("RPC: reading XID (%Zu bytes)\n", len);
605 p = ((char *) &xprt->tcp_xid) + xprt->tcp_offset;
Chuck Lever9903cd12005-08-11 16:25:26 -0400606 used = xs_tcp_copy_data(desc, p, len);
Chuck Levera246b012005-08-11 16:25:23 -0400607 xprt->tcp_offset += used;
608 if (used != len)
609 return;
610 xprt->tcp_flags &= ~XPRT_COPY_XID;
611 xprt->tcp_flags |= XPRT_COPY_DATA;
612 xprt->tcp_copied = 4;
613 dprintk("RPC: reading reply for XID %08x\n",
614 ntohl(xprt->tcp_xid));
Chuck Lever9903cd12005-08-11 16:25:26 -0400615 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400616}
617
Chuck Lever9903cd12005-08-11 16:25:26 -0400618static inline void xs_tcp_read_request(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400619{
620 struct rpc_rqst *req;
621 struct xdr_buf *rcvbuf;
622 size_t len;
623 ssize_t r;
624
625 /* Find and lock the request corresponding to this xid */
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400626 spin_lock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400627 req = xprt_lookup_rqst(xprt, xprt->tcp_xid);
628 if (!req) {
629 xprt->tcp_flags &= ~XPRT_COPY_DATA;
630 dprintk("RPC: XID %08x request not found!\n",
631 ntohl(xprt->tcp_xid));
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400632 spin_unlock(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400633 return;
634 }
635
636 rcvbuf = &req->rq_private_buf;
637 len = desc->count;
638 if (len > xprt->tcp_reclen - xprt->tcp_offset) {
639 skb_reader_t my_desc;
640
641 len = xprt->tcp_reclen - xprt->tcp_offset;
642 memcpy(&my_desc, desc, sizeof(my_desc));
643 my_desc.count = len;
644 r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied,
Chuck Lever9903cd12005-08-11 16:25:26 -0400645 &my_desc, xs_tcp_copy_data);
Chuck Levera246b012005-08-11 16:25:23 -0400646 desc->count -= r;
647 desc->offset += r;
648 } else
649 r = xdr_partial_copy_from_skb(rcvbuf, xprt->tcp_copied,
Chuck Lever9903cd12005-08-11 16:25:26 -0400650 desc, xs_tcp_copy_data);
Chuck Levera246b012005-08-11 16:25:23 -0400651
652 if (r > 0) {
653 xprt->tcp_copied += r;
654 xprt->tcp_offset += r;
655 }
656 if (r != len) {
657 /* Error when copying to the receive buffer,
658 * usually because we weren't able to allocate
659 * additional buffer pages. All we can do now
660 * is turn off XPRT_COPY_DATA, so the request
661 * will not receive any additional updates,
662 * and time out.
663 * Any remaining data from this record will
664 * be discarded.
665 */
666 xprt->tcp_flags &= ~XPRT_COPY_DATA;
667 dprintk("RPC: XID %08x truncated request\n",
668 ntohl(xprt->tcp_xid));
669 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
670 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen);
671 goto out;
672 }
673
674 dprintk("RPC: XID %08x read %Zd bytes\n",
675 ntohl(xprt->tcp_xid), r);
676 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, tcp_reclen = %u\n",
677 xprt, xprt->tcp_copied, xprt->tcp_offset, xprt->tcp_reclen);
678
679 if (xprt->tcp_copied == req->rq_private_buf.buflen)
680 xprt->tcp_flags &= ~XPRT_COPY_DATA;
681 else if (xprt->tcp_offset == xprt->tcp_reclen) {
682 if (xprt->tcp_flags & XPRT_LAST_FRAG)
683 xprt->tcp_flags &= ~XPRT_COPY_DATA;
684 }
685
686out:
Chuck Lever1570c1e2005-08-25 16:25:52 -0700687 if (!(xprt->tcp_flags & XPRT_COPY_DATA))
688 xprt_complete_rqst(req->rq_task, xprt->tcp_copied);
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400689 spin_unlock(&xprt->transport_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400690 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400691}
692
Chuck Lever9903cd12005-08-11 16:25:26 -0400693static inline void xs_tcp_read_discard(struct rpc_xprt *xprt, skb_reader_t *desc)
Chuck Levera246b012005-08-11 16:25:23 -0400694{
695 size_t len;
696
697 len = xprt->tcp_reclen - xprt->tcp_offset;
698 if (len > desc->count)
699 len = desc->count;
700 desc->count -= len;
701 desc->offset += len;
702 xprt->tcp_offset += len;
703 dprintk("RPC: discarded %Zu bytes\n", len);
Chuck Lever9903cd12005-08-11 16:25:26 -0400704 xs_tcp_check_recm(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400705}
706
Chuck Lever9903cd12005-08-11 16:25:26 -0400707static 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 -0400708{
709 struct rpc_xprt *xprt = rd_desc->arg.data;
710 skb_reader_t desc = {
711 .skb = skb,
712 .offset = offset,
713 .count = len,
714 .csum = 0
Chuck Lever9903cd12005-08-11 16:25:26 -0400715 };
Chuck Levera246b012005-08-11 16:25:23 -0400716
Chuck Lever9903cd12005-08-11 16:25:26 -0400717 dprintk("RPC: xs_tcp_data_recv started\n");
Chuck Levera246b012005-08-11 16:25:23 -0400718 do {
719 /* Read in a new fragment marker if necessary */
720 /* Can we ever really expect to get completely empty fragments? */
721 if (xprt->tcp_flags & XPRT_COPY_RECM) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400722 xs_tcp_read_fraghdr(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400723 continue;
724 }
725 /* Read in the xid if necessary */
726 if (xprt->tcp_flags & XPRT_COPY_XID) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400727 xs_tcp_read_xid(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400728 continue;
729 }
730 /* Read in the request data */
731 if (xprt->tcp_flags & XPRT_COPY_DATA) {
Chuck Lever9903cd12005-08-11 16:25:26 -0400732 xs_tcp_read_request(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400733 continue;
734 }
735 /* Skip over any trailing bytes on short reads */
Chuck Lever9903cd12005-08-11 16:25:26 -0400736 xs_tcp_read_discard(xprt, &desc);
Chuck Levera246b012005-08-11 16:25:23 -0400737 } while (desc.count);
Chuck Lever9903cd12005-08-11 16:25:26 -0400738 dprintk("RPC: xs_tcp_data_recv done\n");
Chuck Levera246b012005-08-11 16:25:23 -0400739 return len - desc.count;
740}
741
Chuck Lever9903cd12005-08-11 16:25:26 -0400742/**
743 * xs_tcp_data_ready - "data ready" callback for TCP sockets
744 * @sk: socket with data to read
745 * @bytes: how much data to read
746 *
747 */
748static void xs_tcp_data_ready(struct sock *sk, int bytes)
Chuck Levera246b012005-08-11 16:25:23 -0400749{
750 struct rpc_xprt *xprt;
751 read_descriptor_t rd_desc;
752
753 read_lock(&sk->sk_callback_lock);
Chuck Lever9903cd12005-08-11 16:25:26 -0400754 dprintk("RPC: xs_tcp_data_ready...\n");
755 if (!(xprt = xprt_from_sock(sk)))
Chuck Levera246b012005-08-11 16:25:23 -0400756 goto out;
Chuck Levera246b012005-08-11 16:25:23 -0400757 if (xprt->shutdown)
758 goto out;
759
Chuck Lever9903cd12005-08-11 16:25:26 -0400760 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
Chuck Levera246b012005-08-11 16:25:23 -0400761 rd_desc.arg.data = xprt;
762 rd_desc.count = 65536;
Chuck Lever9903cd12005-08-11 16:25:26 -0400763 tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
Chuck Levera246b012005-08-11 16:25:23 -0400764out:
765 read_unlock(&sk->sk_callback_lock);
766}
767
Chuck Lever9903cd12005-08-11 16:25:26 -0400768/**
769 * xs_tcp_state_change - callback to handle TCP socket state changes
770 * @sk: socket whose state has changed
771 *
772 */
773static void xs_tcp_state_change(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400774{
Chuck Lever9903cd12005-08-11 16:25:26 -0400775 struct rpc_xprt *xprt;
Chuck Levera246b012005-08-11 16:25:23 -0400776
777 read_lock(&sk->sk_callback_lock);
778 if (!(xprt = xprt_from_sock(sk)))
779 goto out;
Chuck Lever9903cd12005-08-11 16:25:26 -0400780 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400781 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
782 sk->sk_state, xprt_connected(xprt),
783 sock_flag(sk, SOCK_DEAD),
784 sock_flag(sk, SOCK_ZAPPED));
785
786 switch (sk->sk_state) {
787 case TCP_ESTABLISHED:
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400788 spin_lock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400789 if (!xprt_test_and_set_connected(xprt)) {
790 /* Reset TCP record info */
791 xprt->tcp_offset = 0;
792 xprt->tcp_reclen = 0;
793 xprt->tcp_copied = 0;
794 xprt->tcp_flags = XPRT_COPY_RECM | XPRT_COPY_XID;
Chuck Lever03bf4b72005-08-25 16:25:55 -0700795 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
Chuck Lever44fbac22005-08-11 16:25:44 -0400796 xprt_wake_pending_tasks(xprt, 0);
Chuck Levera246b012005-08-11 16:25:23 -0400797 }
Chuck Lever4a0f8c02005-08-11 16:25:32 -0400798 spin_unlock_bh(&xprt->transport_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400799 break;
800 case TCP_SYN_SENT:
801 case TCP_SYN_RECV:
802 break;
803 default:
804 xprt_disconnect(xprt);
805 break;
806 }
807 out:
808 read_unlock(&sk->sk_callback_lock);
809}
810
Chuck Lever9903cd12005-08-11 16:25:26 -0400811/**
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400812 * xs_udp_write_space - callback invoked when socket buffer space
813 * becomes available
Chuck Lever9903cd12005-08-11 16:25:26 -0400814 * @sk: socket whose state has changed
815 *
Chuck Levera246b012005-08-11 16:25:23 -0400816 * Called when more output buffer space is available for this socket.
817 * We try not to wake our writers until they can make "significant"
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400818 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
Chuck Levera246b012005-08-11 16:25:23 -0400819 * with a bunch of small requests.
820 */
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400821static void xs_udp_write_space(struct sock *sk)
Chuck Levera246b012005-08-11 16:25:23 -0400822{
Chuck Levera246b012005-08-11 16:25:23 -0400823 read_lock(&sk->sk_callback_lock);
Chuck Levera246b012005-08-11 16:25:23 -0400824
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400825 /* from net/core/sock.c:sock_def_write_space */
826 if (sock_writeable(sk)) {
827 struct socket *sock;
828 struct rpc_xprt *xprt;
829
830 if (unlikely(!(sock = sk->sk_socket)))
Chuck Levera246b012005-08-11 16:25:23 -0400831 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400832 if (unlikely(!(xprt = xprt_from_sock(sk))))
Chuck Levera246b012005-08-11 16:25:23 -0400833 goto out;
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400834 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
835 goto out;
836
837 xprt_write_space(xprt);
Chuck Levera246b012005-08-11 16:25:23 -0400838 }
839
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400840 out:
841 read_unlock(&sk->sk_callback_lock);
842}
Chuck Levera246b012005-08-11 16:25:23 -0400843
Chuck Leverc7b2cae2005-08-11 16:25:50 -0400844/**
845 * xs_tcp_write_space - callback invoked when socket buffer space
846 * becomes available
847 * @sk: socket whose state has changed
848 *
849 * Called when more output buffer space is available for this socket.
850 * We try not to wake our writers until they can make "significant"
851 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
852 * with a bunch of small requests.
853 */
854static void xs_tcp_write_space(struct sock *sk)
855{
856 read_lock(&sk->sk_callback_lock);
857
858 /* from net/core/stream.c:sk_stream_write_space */
859 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
860 struct socket *sock;
861 struct rpc_xprt *xprt;
862
863 if (unlikely(!(sock = sk->sk_socket)))
864 goto out;
865 if (unlikely(!(xprt = xprt_from_sock(sk))))
866 goto out;
867 if (unlikely(!test_and_clear_bit(SOCK_NOSPACE, &sock->flags)))
868 goto out;
869
870 xprt_write_space(xprt);
871 }
872
873 out:
Chuck Levera246b012005-08-11 16:25:23 -0400874 read_unlock(&sk->sk_callback_lock);
875}
876
Chuck Lever470056c2005-08-25 16:25:56 -0700877static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
Chuck Levera246b012005-08-11 16:25:23 -0400878{
879 struct sock *sk = xprt->inet;
880
Chuck Levera246b012005-08-11 16:25:23 -0400881 if (xprt->rcvsize) {
882 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
883 sk->sk_rcvbuf = xprt->rcvsize * xprt->max_reqs * 2;
884 }
885 if (xprt->sndsize) {
886 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
887 sk->sk_sndbuf = xprt->sndsize * xprt->max_reqs * 2;
888 sk->sk_write_space(sk);
889 }
890}
891
Chuck Lever43118c22005-08-25 16:25:49 -0700892/**
Chuck Lever470056c2005-08-25 16:25:56 -0700893 * xs_udp_set_buffer_size - set send and receive limits
Chuck Lever43118c22005-08-25 16:25:49 -0700894 * @xprt: generic transport
Chuck Lever470056c2005-08-25 16:25:56 -0700895 * @sndsize: requested size of send buffer, in bytes
896 * @rcvsize: requested size of receive buffer, in bytes
Chuck Lever43118c22005-08-25 16:25:49 -0700897 *
Chuck Lever470056c2005-08-25 16:25:56 -0700898 * Set socket send and receive buffer size limits.
Chuck Lever43118c22005-08-25 16:25:49 -0700899 */
Chuck Lever470056c2005-08-25 16:25:56 -0700900static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
Chuck Lever43118c22005-08-25 16:25:49 -0700901{
Chuck Lever470056c2005-08-25 16:25:56 -0700902 xprt->sndsize = 0;
903 if (sndsize)
904 xprt->sndsize = sndsize + 1024;
905 xprt->rcvsize = 0;
906 if (rcvsize)
907 xprt->rcvsize = rcvsize + 1024;
908
909 xs_udp_do_set_buffer_size(xprt);
Chuck Lever43118c22005-08-25 16:25:49 -0700910}
911
Chuck Lever46c0ee82005-08-25 16:25:52 -0700912/**
913 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
914 * @task: task that timed out
915 *
916 * Adjust the congestion window after a retransmit timeout has occurred.
917 */
918static void xs_udp_timer(struct rpc_task *task)
919{
920 xprt_adjust_cwnd(task, -ETIMEDOUT);
921}
922
Chuck Lever9903cd12005-08-11 16:25:26 -0400923static int xs_bindresvport(struct rpc_xprt *xprt, struct socket *sock)
Chuck Levera246b012005-08-11 16:25:23 -0400924{
925 struct sockaddr_in myaddr = {
926 .sin_family = AF_INET,
927 };
Chuck Lever529b33c2005-08-25 16:25:54 -0700928 int err;
929 unsigned short port = xprt->port;
Chuck Levera246b012005-08-11 16:25:23 -0400930
Chuck Levera246b012005-08-11 16:25:23 -0400931 do {
932 myaddr.sin_port = htons(port);
933 err = sock->ops->bind(sock, (struct sockaddr *) &myaddr,
934 sizeof(myaddr));
935 if (err == 0) {
936 xprt->port = port;
Chuck Lever9903cd12005-08-11 16:25:26 -0400937 dprintk("RPC: xs_bindresvport bound to port %u\n",
938 port);
Chuck Levera246b012005-08-11 16:25:23 -0400939 return 0;
940 }
Chuck Lever529b33c2005-08-25 16:25:54 -0700941 if (port <= xprt_min_resvport)
942 port = xprt_max_resvport;
943 else
944 port--;
Chuck Levera246b012005-08-11 16:25:23 -0400945 } while (err == -EADDRINUSE && port != xprt->port);
946
Chuck Lever9903cd12005-08-11 16:25:26 -0400947 dprintk("RPC: can't bind to reserved port (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -0400948 return err;
949}
950
Chuck Lever9903cd12005-08-11 16:25:26 -0400951/**
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400952 * xs_udp_connect_worker - set up a UDP socket
Chuck Lever9903cd12005-08-11 16:25:26 -0400953 * @args: RPC transport to connect
954 *
955 * Invoked by a work queue tasklet.
Chuck Levera246b012005-08-11 16:25:23 -0400956 */
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400957static void xs_udp_connect_worker(void *args)
Chuck Levera246b012005-08-11 16:25:23 -0400958{
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400959 struct rpc_xprt *xprt = (struct rpc_xprt *) args;
Chuck Levera246b012005-08-11 16:25:23 -0400960 struct socket *sock = xprt->sock;
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400961 int err, status = -EIO;
Chuck Levera246b012005-08-11 16:25:23 -0400962
963 if (xprt->shutdown || xprt->addr.sin_port == 0)
964 goto out;
965
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400966 dprintk("RPC: xs_udp_connect_worker for xprt %p\n", xprt);
Chuck Lever9903cd12005-08-11 16:25:26 -0400967
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400968 /* Start by resetting any existing state */
Chuck Lever9903cd12005-08-11 16:25:26 -0400969 xs_close(xprt);
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400970
971 if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
972 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
Chuck Levera246b012005-08-11 16:25:23 -0400973 goto out;
974 }
Chuck Levera246b012005-08-11 16:25:23 -0400975
Chuck Leverb0d93ad2005-08-11 16:25:53 -0400976 if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) {
977 sock_release(sock);
978 goto out;
979 }
980
981 if (!xprt->inet) {
982 struct sock *sk = sock->sk;
983
984 write_lock_bh(&sk->sk_callback_lock);
985
986 sk->sk_user_data = xprt;
987 xprt->old_data_ready = sk->sk_data_ready;
988 xprt->old_state_change = sk->sk_state_change;
989 xprt->old_write_space = sk->sk_write_space;
990 sk->sk_data_ready = xs_udp_data_ready;
991 sk->sk_write_space = xs_udp_write_space;
992 sk->sk_no_check = UDP_CSUM_NORCV;
993
994 xprt_set_connected(xprt);
995
996 /* Reset to new socket */
997 xprt->sock = sock;
998 xprt->inet = sk;
999
1000 write_unlock_bh(&sk->sk_callback_lock);
1001 }
Chuck Lever470056c2005-08-25 16:25:56 -07001002 xs_udp_do_set_buffer_size(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001003 status = 0;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001004out:
1005 xprt_wake_pending_tasks(xprt, status);
1006 xprt_clear_connecting(xprt);
1007}
1008
Chuck Lever3167e122005-08-25 16:25:55 -07001009/*
1010 * We need to preserve the port number so the reply cache on the server can
1011 * find our cached RPC replies when we get around to reconnecting.
1012 */
1013static void xs_tcp_reuse_connection(struct rpc_xprt *xprt)
1014{
1015 int result;
1016 struct socket *sock = xprt->sock;
1017 struct sockaddr any;
1018
1019 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
1020
1021 /*
1022 * Disconnect the transport socket by doing a connect operation
1023 * with AF_UNSPEC. This should return immediately...
1024 */
1025 memset(&any, 0, sizeof(any));
1026 any.sa_family = AF_UNSPEC;
1027 result = sock->ops->connect(sock, &any, sizeof(any), 0);
1028 if (result)
1029 dprintk("RPC: AF_UNSPEC connect return code %d\n",
1030 result);
1031}
1032
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001033/**
1034 * xs_tcp_connect_worker - connect a TCP socket to a remote endpoint
1035 * @args: RPC transport to connect
1036 *
1037 * Invoked by a work queue tasklet.
1038 */
1039static void xs_tcp_connect_worker(void *args)
1040{
1041 struct rpc_xprt *xprt = (struct rpc_xprt *)args;
1042 struct socket *sock = xprt->sock;
1043 int err, status = -EIO;
1044
1045 if (xprt->shutdown || xprt->addr.sin_port == 0)
Chuck Levera246b012005-08-11 16:25:23 -04001046 goto out;
1047
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001048 dprintk("RPC: xs_tcp_connect_worker for xprt %p\n", xprt);
1049
Chuck Lever3167e122005-08-25 16:25:55 -07001050 if (!xprt->sock) {
1051 /* start from scratch */
1052 if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
1053 dprintk("RPC: can't create TCP transport socket (%d).\n", -err);
1054 goto out;
1055 }
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001056
Chuck Lever3167e122005-08-25 16:25:55 -07001057 if (xprt->resvport && xs_bindresvport(xprt, sock) < 0) {
1058 sock_release(sock);
1059 goto out;
1060 }
1061 } else
1062 /* "close" the socket, preserving the local port */
1063 xs_tcp_reuse_connection(xprt);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001064
1065 if (!xprt->inet) {
1066 struct sock *sk = sock->sk;
1067
1068 write_lock_bh(&sk->sk_callback_lock);
1069
1070 sk->sk_user_data = xprt;
1071 xprt->old_data_ready = sk->sk_data_ready;
1072 xprt->old_state_change = sk->sk_state_change;
1073 xprt->old_write_space = sk->sk_write_space;
1074 sk->sk_data_ready = xs_tcp_data_ready;
1075 sk->sk_state_change = xs_tcp_state_change;
1076 sk->sk_write_space = xs_tcp_write_space;
Chuck Lever3167e122005-08-25 16:25:55 -07001077
1078 /* socket options */
1079 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1080 sock_reset_flag(sk, SOCK_LINGER);
1081 tcp_sk(sk)->linger2 = 0;
1082 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001083
1084 xprt_clear_connected(xprt);
1085
1086 /* Reset to new socket */
1087 xprt->sock = sock;
1088 xprt->inet = sk;
1089
1090 write_unlock_bh(&sk->sk_callback_lock);
1091 }
1092
1093 /* Tell the socket layer to start connecting... */
Chuck Levera246b012005-08-11 16:25:23 -04001094 status = sock->ops->connect(sock, (struct sockaddr *) &xprt->addr,
1095 sizeof(xprt->addr), O_NONBLOCK);
1096 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1097 xprt, -status, xprt_connected(xprt), sock->sk->sk_state);
1098 if (status < 0) {
1099 switch (status) {
1100 case -EINPROGRESS:
1101 case -EALREADY:
1102 goto out_clear;
Chuck Lever3167e122005-08-25 16:25:55 -07001103 case -ECONNREFUSED:
1104 case -ECONNRESET:
1105 /* retry with existing socket, after a delay */
1106 break;
1107 default:
1108 /* get rid of existing socket, and retry */
1109 xs_close(xprt);
1110 break;
Chuck Levera246b012005-08-11 16:25:23 -04001111 }
1112 }
1113out:
Chuck Lever44fbac22005-08-11 16:25:44 -04001114 xprt_wake_pending_tasks(xprt, status);
Chuck Levera246b012005-08-11 16:25:23 -04001115out_clear:
Chuck Lever2226feb2005-08-11 16:25:38 -04001116 xprt_clear_connecting(xprt);
Chuck Levera246b012005-08-11 16:25:23 -04001117}
1118
Chuck Lever9903cd12005-08-11 16:25:26 -04001119/**
1120 * xs_connect - connect a socket to a remote endpoint
1121 * @task: address of RPC task that manages state of connect request
1122 *
1123 * TCP: If the remote end dropped the connection, delay reconnecting.
Chuck Lever03bf4b72005-08-25 16:25:55 -07001124 *
1125 * UDP socket connects are synchronous, but we use a work queue anyway
1126 * to guarantee that even unprivileged user processes can set up a
1127 * socket on a privileged port.
1128 *
1129 * If a UDP socket connect fails, the delay behavior here prevents
1130 * retry floods (hard mounts).
Chuck Lever9903cd12005-08-11 16:25:26 -04001131 */
1132static void xs_connect(struct rpc_task *task)
Chuck Levera246b012005-08-11 16:25:23 -04001133{
1134 struct rpc_xprt *xprt = task->tk_xprt;
1135
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001136 if (xprt_test_and_set_connecting(xprt))
1137 return;
1138
1139 if (xprt->sock != NULL) {
Chuck Lever03bf4b72005-08-25 16:25:55 -07001140 dprintk("RPC: xs_connect delayed xprt %p for %lu seconds\n",
1141 xprt, xprt->reestablish_timeout / HZ);
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001142 schedule_delayed_work(&xprt->connect_worker,
Chuck Lever03bf4b72005-08-25 16:25:55 -07001143 xprt->reestablish_timeout);
1144 xprt->reestablish_timeout <<= 1;
1145 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
1146 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001147 } else {
1148 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
1149 schedule_work(&xprt->connect_worker);
1150
1151 /* flush_scheduled_work can sleep... */
1152 if (!RPC_IS_ASYNC(task))
1153 flush_scheduled_work();
Chuck Levera246b012005-08-11 16:25:23 -04001154 }
1155}
1156
Chuck Lever262965f2005-08-11 16:25:56 -04001157static struct rpc_xprt_ops xs_udp_ops = {
Chuck Lever43118c22005-08-25 16:25:49 -07001158 .set_buffer_size = xs_udp_set_buffer_size,
Chuck Lever12a80462005-08-25 16:25:51 -07001159 .reserve_xprt = xprt_reserve_xprt_cong,
Chuck Lever49e9a892005-08-25 16:25:51 -07001160 .release_xprt = xprt_release_xprt_cong,
Chuck Lever9903cd12005-08-11 16:25:26 -04001161 .connect = xs_connect,
Chuck Lever262965f2005-08-11 16:25:56 -04001162 .send_request = xs_udp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001163 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
Chuck Lever46c0ee82005-08-25 16:25:52 -07001164 .timer = xs_udp_timer,
Chuck Levera58dd392005-08-25 16:25:53 -07001165 .release_request = xprt_release_rqst_cong,
Chuck Lever262965f2005-08-11 16:25:56 -04001166 .close = xs_close,
1167 .destroy = xs_destroy,
1168};
1169
1170static struct rpc_xprt_ops xs_tcp_ops = {
Chuck Lever12a80462005-08-25 16:25:51 -07001171 .reserve_xprt = xprt_reserve_xprt,
Chuck Lever49e9a892005-08-25 16:25:51 -07001172 .release_xprt = xprt_release_xprt,
Chuck Lever262965f2005-08-11 16:25:56 -04001173 .connect = xs_connect,
1174 .send_request = xs_tcp_send_request,
Chuck Leverfe3aca22005-08-25 16:25:50 -07001175 .set_retrans_timeout = xprt_set_retrans_timeout_def,
Chuck Lever9903cd12005-08-11 16:25:26 -04001176 .close = xs_close,
1177 .destroy = xs_destroy,
Chuck Levera246b012005-08-11 16:25:23 -04001178};
1179
Chuck Lever9903cd12005-08-11 16:25:26 -04001180/**
1181 * xs_setup_udp - Set up transport to use a UDP socket
1182 * @xprt: transport to set up
1183 * @to: timeout parameters
1184 *
1185 */
Chuck Levera246b012005-08-11 16:25:23 -04001186int xs_setup_udp(struct rpc_xprt *xprt, struct rpc_timeout *to)
1187{
1188 size_t slot_table_size;
1189
1190 dprintk("RPC: setting up udp-ipv4 transport...\n");
1191
1192 xprt->max_reqs = xprt_udp_slot_table_entries;
1193 slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]);
1194 xprt->slot = kmalloc(slot_table_size, GFP_KERNEL);
1195 if (xprt->slot == NULL)
1196 return -ENOMEM;
1197 memset(xprt->slot, 0, slot_table_size);
1198
1199 xprt->prot = IPPROTO_UDP;
Chuck Lever529b33c2005-08-25 16:25:54 -07001200 xprt->port = xprt_max_resvport;
Chuck Lever808012f2005-08-25 16:25:49 -07001201 xprt->tsh_size = 0;
Chuck Levera246b012005-08-11 16:25:23 -04001202 xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0;
1203 /* XXX: header size can vary due to auth type, IPv6, etc. */
1204 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
1205
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001206 INIT_WORK(&xprt->connect_worker, xs_udp_connect_worker, xprt);
Chuck Lever03bf4b72005-08-25 16:25:55 -07001207 xprt->bind_timeout = XS_BIND_TO;
1208 xprt->connect_timeout = XS_UDP_CONN_TO;
1209 xprt->reestablish_timeout = XS_UDP_REEST_TO;
1210 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04001211
Chuck Lever262965f2005-08-11 16:25:56 -04001212 xprt->ops = &xs_udp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001213
1214 if (to)
1215 xprt->timeout = *to;
1216 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001217 xprt_set_timeout(&xprt->timeout, 5, 5 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001218
1219 return 0;
1220}
1221
Chuck Lever9903cd12005-08-11 16:25:26 -04001222/**
1223 * xs_setup_tcp - Set up transport to use a TCP socket
1224 * @xprt: transport to set up
1225 * @to: timeout parameters
1226 *
1227 */
Chuck Levera246b012005-08-11 16:25:23 -04001228int xs_setup_tcp(struct rpc_xprt *xprt, struct rpc_timeout *to)
1229{
1230 size_t slot_table_size;
1231
1232 dprintk("RPC: setting up tcp-ipv4 transport...\n");
1233
1234 xprt->max_reqs = xprt_tcp_slot_table_entries;
1235 slot_table_size = xprt->max_reqs * sizeof(xprt->slot[0]);
1236 xprt->slot = kmalloc(slot_table_size, GFP_KERNEL);
1237 if (xprt->slot == NULL)
1238 return -ENOMEM;
1239 memset(xprt->slot, 0, slot_table_size);
1240
1241 xprt->prot = IPPROTO_TCP;
Chuck Lever529b33c2005-08-25 16:25:54 -07001242 xprt->port = xprt_max_resvport;
Chuck Lever808012f2005-08-25 16:25:49 -07001243 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
Chuck Levera246b012005-08-11 16:25:23 -04001244 xprt->resvport = capable(CAP_NET_BIND_SERVICE) ? 1 : 0;
Chuck Lever808012f2005-08-25 16:25:49 -07001245 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
Chuck Levera246b012005-08-11 16:25:23 -04001246
Chuck Leverb0d93ad2005-08-11 16:25:53 -04001247 INIT_WORK(&xprt->connect_worker, xs_tcp_connect_worker, xprt);
Chuck Lever03bf4b72005-08-25 16:25:55 -07001248 xprt->bind_timeout = XS_BIND_TO;
1249 xprt->connect_timeout = XS_TCP_CONN_TO;
1250 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1251 xprt->idle_timeout = XS_IDLE_DISC_TO;
Chuck Levera246b012005-08-11 16:25:23 -04001252
Chuck Lever262965f2005-08-11 16:25:56 -04001253 xprt->ops = &xs_tcp_ops;
Chuck Levera246b012005-08-11 16:25:23 -04001254
1255 if (to)
1256 xprt->timeout = *to;
1257 else
Chuck Lever9903cd12005-08-11 16:25:26 -04001258 xprt_set_timeout(&xprt->timeout, 2, 60 * HZ);
Chuck Levera246b012005-08-11 16:25:23 -04001259
1260 return 0;
1261}