blob: 88b51c4ecb8b0e81218833eea61ed386e804afed [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/net/sunrpc/svcsock.c
3 *
4 * These are the RPC server socket internals.
5 *
6 * The server scheduling algorithm does not always distribute the load
7 * evenly when servicing a single client. May need to modify the
8 * svc_sock_enqueue procedure...
9 *
10 * TCP support is largely untested and may be a little slow. The problem
11 * is that we currently do two separate recvfrom's, one for the 4-byte
12 * record length, and the second for the actual record. This could possibly
13 * be improved by always reading a minimum size of around 100 bytes and
14 * tucking any superfluous bytes away in a temporary store. Still, that
15 * leaves write requests out in the rain. An alternative may be to peek at
16 * the first skb in the queue, and if it matches the next TCP sequence
17 * number, to extract the record marker. Yuck.
18 *
19 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
20 */
21
22#include <linux/sched.h>
23#include <linux/errno.h>
24#include <linux/fcntl.h>
25#include <linux/net.h>
26#include <linux/in.h>
27#include <linux/inet.h>
28#include <linux/udp.h>
Andrew Morton91483c42005-08-09 20:20:07 -070029#include <linux/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/unistd.h>
31#include <linux/slab.h>
32#include <linux/netdevice.h>
33#include <linux/skbuff.h>
NeilBrownb41b66d2006-10-02 02:17:48 -070034#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <net/sock.h>
36#include <net/checksum.h>
37#include <net/ip.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070038#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/uaccess.h>
40#include <asm/ioctls.h>
41
42#include <linux/sunrpc/types.h>
43#include <linux/sunrpc/xdr.h>
44#include <linux/sunrpc/svcsock.h>
45#include <linux/sunrpc/stats.h>
46
47/* SMP locking strategy:
48 *
49 * svc_serv->sv_lock protects most stuff for that service.
Greg Banks1a68d952006-10-02 02:17:55 -070050 * svc_sock->sk_defer_lock protects the svc_sock->sk_deferred list
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 *
52 * Some flags can be set to certain values at any time
53 * providing that certain rules are followed:
54 *
55 * SK_BUSY can be set to 0 at any time.
56 * svc_sock_enqueue must be called afterwards
57 * SK_CONN, SK_DATA, can be set or cleared at any time.
58 * after a set, svc_sock_enqueue must be called.
59 * after a clear, the socket must be read/accepted
60 * if this succeeds, it must be set again.
61 * SK_CLOSE can set at any time. It is never cleared.
62 *
63 */
64
65#define RPCDBG_FACILITY RPCDBG_SVCSOCK
66
67
68static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
69 int *errp, int pmap_reg);
70static void svc_udp_data_ready(struct sock *, int);
71static int svc_udp_recvfrom(struct svc_rqst *);
72static int svc_udp_sendto(struct svc_rqst *);
73
74static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk);
75static int svc_deferred_recv(struct svc_rqst *rqstp);
76static struct cache_deferred_req *svc_defer(struct cache_req *req);
77
Greg Banks36bdfc82006-10-02 02:17:54 -070078/* apparently the "standard" is that clients close
79 * idle connections after 5 minutes, servers after
80 * 6 minutes
81 * http://www.connectathon.org/talks96/nfstcp.pdf
82 */
83static int svc_conn_age_period = 6*60;
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/*
86 * Queue up an idle server thread. Must have serv->sv_lock held.
87 * Note: this is really a stack rather than a queue, so that we only
88 * use as many different threads as we need, and the rest don't polute
89 * the cache.
90 */
91static inline void
92svc_serv_enqueue(struct svc_serv *serv, struct svc_rqst *rqstp)
93{
94 list_add(&rqstp->rq_list, &serv->sv_threads);
95}
96
97/*
98 * Dequeue an nfsd thread. Must have serv->sv_lock held.
99 */
100static inline void
101svc_serv_dequeue(struct svc_serv *serv, struct svc_rqst *rqstp)
102{
103 list_del(&rqstp->rq_list);
104}
105
106/*
107 * Release an skbuff after use
108 */
109static inline void
110svc_release_skb(struct svc_rqst *rqstp)
111{
112 struct sk_buff *skb = rqstp->rq_skbuff;
113 struct svc_deferred_req *dr = rqstp->rq_deferred;
114
115 if (skb) {
116 rqstp->rq_skbuff = NULL;
117
118 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
119 skb_free_datagram(rqstp->rq_sock->sk_sk, skb);
120 }
121 if (dr) {
122 rqstp->rq_deferred = NULL;
123 kfree(dr);
124 }
125}
126
127/*
128 * Any space to write?
129 */
130static inline unsigned long
131svc_sock_wspace(struct svc_sock *svsk)
132{
133 int wspace;
134
135 if (svsk->sk_sock->type == SOCK_STREAM)
136 wspace = sk_stream_wspace(svsk->sk_sk);
137 else
138 wspace = sock_wspace(svsk->sk_sk);
139
140 return wspace;
141}
142
143/*
144 * Queue up a socket with data pending. If there are idle nfsd
145 * processes, wake 'em up.
146 *
147 */
148static void
149svc_sock_enqueue(struct svc_sock *svsk)
150{
151 struct svc_serv *serv = svsk->sk_server;
152 struct svc_rqst *rqstp;
153
154 if (!(svsk->sk_flags &
155 ( (1<<SK_CONN)|(1<<SK_DATA)|(1<<SK_CLOSE)|(1<<SK_DEFERRED)) ))
156 return;
157 if (test_bit(SK_DEAD, &svsk->sk_flags))
158 return;
159
160 spin_lock_bh(&serv->sv_lock);
161
162 if (!list_empty(&serv->sv_threads) &&
163 !list_empty(&serv->sv_sockets))
164 printk(KERN_ERR
165 "svc_sock_enqueue: threads and sockets both waiting??\n");
166
167 if (test_bit(SK_DEAD, &svsk->sk_flags)) {
168 /* Don't enqueue dead sockets */
169 dprintk("svc: socket %p is dead, not enqueued\n", svsk->sk_sk);
170 goto out_unlock;
171 }
172
173 if (test_bit(SK_BUSY, &svsk->sk_flags)) {
174 /* Don't enqueue socket while daemon is receiving */
175 dprintk("svc: socket %p busy, not enqueued\n", svsk->sk_sk);
176 goto out_unlock;
177 }
178
179 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
Greg Banks5685f0f2006-10-02 02:17:56 -0700180 if (((atomic_read(&svsk->sk_reserved) + serv->sv_bufsz)*2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 > svc_sock_wspace(svsk))
182 && !test_bit(SK_CLOSE, &svsk->sk_flags)
183 && !test_bit(SK_CONN, &svsk->sk_flags)) {
184 /* Don't enqueue while not enough space for reply */
185 dprintk("svc: socket %p no space, %d*2 > %ld, not enqueued\n",
Greg Banks5685f0f2006-10-02 02:17:56 -0700186 svsk->sk_sk, atomic_read(&svsk->sk_reserved)+serv->sv_bufsz,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 svc_sock_wspace(svsk));
188 goto out_unlock;
189 }
190 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
191
192 /* Mark socket as busy. It will remain in this state until the
193 * server has processed all pending data and put the socket back
194 * on the idle list.
195 */
196 set_bit(SK_BUSY, &svsk->sk_flags);
197
198 if (!list_empty(&serv->sv_threads)) {
199 rqstp = list_entry(serv->sv_threads.next,
200 struct svc_rqst,
201 rq_list);
202 dprintk("svc: socket %p served by daemon %p\n",
203 svsk->sk_sk, rqstp);
204 svc_serv_dequeue(serv, rqstp);
205 if (rqstp->rq_sock)
206 printk(KERN_ERR
207 "svc_sock_enqueue: server %p, rq_sock=%p!\n",
208 rqstp, rqstp->rq_sock);
209 rqstp->rq_sock = svsk;
Greg Banksc45c3572006-10-02 02:17:54 -0700210 atomic_inc(&svsk->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 rqstp->rq_reserved = serv->sv_bufsz;
Greg Banks5685f0f2006-10-02 02:17:56 -0700212 atomic_add(rqstp->rq_reserved, &svsk->sk_reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 wake_up(&rqstp->rq_wait);
214 } else {
215 dprintk("svc: socket %p put into queue\n", svsk->sk_sk);
216 list_add_tail(&svsk->sk_ready, &serv->sv_sockets);
217 }
218
219out_unlock:
220 spin_unlock_bh(&serv->sv_lock);
221}
222
223/*
224 * Dequeue the first socket. Must be called with the serv->sv_lock held.
225 */
226static inline struct svc_sock *
227svc_sock_dequeue(struct svc_serv *serv)
228{
229 struct svc_sock *svsk;
230
231 if (list_empty(&serv->sv_sockets))
232 return NULL;
233
234 svsk = list_entry(serv->sv_sockets.next,
235 struct svc_sock, sk_ready);
236 list_del_init(&svsk->sk_ready);
237
238 dprintk("svc: socket %p dequeued, inuse=%d\n",
Greg Banksc45c3572006-10-02 02:17:54 -0700239 svsk->sk_sk, atomic_read(&svsk->sk_inuse));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 return svsk;
242}
243
244/*
245 * Having read something from a socket, check whether it
246 * needs to be re-enqueued.
247 * Note: SK_DATA only gets cleared when a read-attempt finds
248 * no (or insufficient) data.
249 */
250static inline void
251svc_sock_received(struct svc_sock *svsk)
252{
253 clear_bit(SK_BUSY, &svsk->sk_flags);
254 svc_sock_enqueue(svsk);
255}
256
257
258/**
259 * svc_reserve - change the space reserved for the reply to a request.
260 * @rqstp: The request in question
261 * @space: new max space to reserve
262 *
263 * Each request reserves some space on the output queue of the socket
264 * to make sure the reply fits. This function reduces that reserved
265 * space to be the amount of space used already, plus @space.
266 *
267 */
268void svc_reserve(struct svc_rqst *rqstp, int space)
269{
270 space += rqstp->rq_res.head[0].iov_len;
271
272 if (space < rqstp->rq_reserved) {
273 struct svc_sock *svsk = rqstp->rq_sock;
Greg Banks5685f0f2006-10-02 02:17:56 -0700274 atomic_sub((rqstp->rq_reserved - space), &svsk->sk_reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 rqstp->rq_reserved = space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 svc_sock_enqueue(svsk);
278 }
279}
280
281/*
282 * Release a socket after use.
283 */
284static inline void
285svc_sock_put(struct svc_sock *svsk)
286{
Greg Banksc45c3572006-10-02 02:17:54 -0700287 if (atomic_dec_and_test(&svsk->sk_inuse) && test_bit(SK_DEAD, &svsk->sk_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 dprintk("svc: releasing dead socket\n");
289 sock_release(svsk->sk_sock);
290 kfree(svsk);
291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294static void
295svc_sock_release(struct svc_rqst *rqstp)
296{
297 struct svc_sock *svsk = rqstp->rq_sock;
298
299 svc_release_skb(rqstp);
300
301 svc_free_allpages(rqstp);
302 rqstp->rq_res.page_len = 0;
303 rqstp->rq_res.page_base = 0;
304
305
306 /* Reset response buffer and release
307 * the reservation.
308 * But first, check that enough space was reserved
309 * for the reply, otherwise we have a bug!
310 */
311 if ((rqstp->rq_res.len) > rqstp->rq_reserved)
312 printk(KERN_ERR "RPC request reserved %d but used %d\n",
313 rqstp->rq_reserved,
314 rqstp->rq_res.len);
315
316 rqstp->rq_res.head[0].iov_len = 0;
317 svc_reserve(rqstp, 0);
318 rqstp->rq_sock = NULL;
319
320 svc_sock_put(svsk);
321}
322
323/*
324 * External function to wake up a server waiting for data
325 */
326void
327svc_wake_up(struct svc_serv *serv)
328{
329 struct svc_rqst *rqstp;
330
331 spin_lock_bh(&serv->sv_lock);
332 if (!list_empty(&serv->sv_threads)) {
333 rqstp = list_entry(serv->sv_threads.next,
334 struct svc_rqst,
335 rq_list);
336 dprintk("svc: daemon %p woken up.\n", rqstp);
337 /*
338 svc_serv_dequeue(serv, rqstp);
339 rqstp->rq_sock = NULL;
340 */
341 wake_up(&rqstp->rq_wait);
342 }
343 spin_unlock_bh(&serv->sv_lock);
344}
345
346/*
347 * Generic sendto routine
348 */
349static int
350svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
351{
352 struct svc_sock *svsk = rqstp->rq_sock;
353 struct socket *sock = svsk->sk_sock;
354 int slen;
355 char buffer[CMSG_SPACE(sizeof(struct in_pktinfo))];
356 struct cmsghdr *cmh = (struct cmsghdr *)buffer;
357 struct in_pktinfo *pki = (struct in_pktinfo *)CMSG_DATA(cmh);
358 int len = 0;
359 int result;
360 int size;
361 struct page **ppage = xdr->pages;
362 size_t base = xdr->page_base;
363 unsigned int pglen = xdr->page_len;
364 unsigned int flags = MSG_MORE;
365
366 slen = xdr->len;
367
368 if (rqstp->rq_prot == IPPROTO_UDP) {
369 /* set the source and destination */
370 struct msghdr msg;
371 msg.msg_name = &rqstp->rq_addr;
372 msg.msg_namelen = sizeof(rqstp->rq_addr);
373 msg.msg_iov = NULL;
374 msg.msg_iovlen = 0;
375 msg.msg_flags = MSG_MORE;
376
377 msg.msg_control = cmh;
378 msg.msg_controllen = sizeof(buffer);
379 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
380 cmh->cmsg_level = SOL_IP;
381 cmh->cmsg_type = IP_PKTINFO;
382 pki->ipi_ifindex = 0;
383 pki->ipi_spec_dst.s_addr = rqstp->rq_daddr;
384
385 if (sock_sendmsg(sock, &msg, 0) < 0)
386 goto out;
387 }
388
389 /* send head */
390 if (slen == xdr->head[0].iov_len)
391 flags = 0;
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700392 len = kernel_sendpage(sock, rqstp->rq_respages[0], 0, xdr->head[0].iov_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (len != xdr->head[0].iov_len)
394 goto out;
395 slen -= xdr->head[0].iov_len;
396 if (slen == 0)
397 goto out;
398
399 /* send page data */
400 size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
401 while (pglen > 0) {
402 if (slen == size)
403 flags = 0;
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700404 result = kernel_sendpage(sock, *ppage, base, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (result > 0)
406 len += result;
407 if (result != size)
408 goto out;
409 slen -= size;
410 pglen -= size;
411 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
412 base = 0;
413 ppage++;
414 }
415 /* send tail */
416 if (xdr->tail[0].iov_len) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700417 result = kernel_sendpage(sock, rqstp->rq_respages[rqstp->rq_restailpage],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 ((unsigned long)xdr->tail[0].iov_base)& (PAGE_SIZE-1),
419 xdr->tail[0].iov_len, 0);
420
421 if (result > 0)
422 len += result;
423 }
424out:
425 dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %x)\n",
426 rqstp->rq_sock, xdr->head[0].iov_base, xdr->head[0].iov_len, xdr->len, len,
427 rqstp->rq_addr.sin_addr.s_addr);
428
429 return len;
430}
431
432/*
NeilBrown80212d52006-10-02 02:17:47 -0700433 * Report socket names for nfsdfs
434 */
435static int one_sock_name(char *buf, struct svc_sock *svsk)
436{
437 int len;
438
439 switch(svsk->sk_sk->sk_family) {
440 case AF_INET:
441 len = sprintf(buf, "ipv4 %s %u.%u.%u.%u %d\n",
442 svsk->sk_sk->sk_protocol==IPPROTO_UDP?
443 "udp" : "tcp",
444 NIPQUAD(inet_sk(svsk->sk_sk)->rcv_saddr),
445 inet_sk(svsk->sk_sk)->num);
446 break;
447 default:
448 len = sprintf(buf, "*unknown-%d*\n",
449 svsk->sk_sk->sk_family);
450 }
451 return len;
452}
453
454int
NeilBrownb41b66d2006-10-02 02:17:48 -0700455svc_sock_names(char *buf, struct svc_serv *serv, char *toclose)
NeilBrown80212d52006-10-02 02:17:47 -0700456{
NeilBrownb41b66d2006-10-02 02:17:48 -0700457 struct svc_sock *svsk, *closesk = NULL;
NeilBrown80212d52006-10-02 02:17:47 -0700458 int len = 0;
459
460 if (!serv)
461 return 0;
462 spin_lock(&serv->sv_lock);
463 list_for_each_entry(svsk, &serv->sv_permsocks, sk_list) {
464 int onelen = one_sock_name(buf+len, svsk);
NeilBrownb41b66d2006-10-02 02:17:48 -0700465 if (toclose && strcmp(toclose, buf+len) == 0)
466 closesk = svsk;
467 else
468 len += onelen;
NeilBrown80212d52006-10-02 02:17:47 -0700469 }
470 spin_unlock(&serv->sv_lock);
NeilBrownb41b66d2006-10-02 02:17:48 -0700471 if (closesk)
472 svc_delete_socket(closesk);
NeilBrown80212d52006-10-02 02:17:47 -0700473 return len;
474}
475EXPORT_SYMBOL(svc_sock_names);
476
477/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 * Check input queue length
479 */
480static int
481svc_recv_available(struct svc_sock *svsk)
482{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 struct socket *sock = svsk->sk_sock;
484 int avail, err;
485
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700486 err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 return (err >= 0)? avail : err;
489}
490
491/*
492 * Generic recvfrom routine.
493 */
494static int
495svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr, int buflen)
496{
497 struct msghdr msg;
498 struct socket *sock;
499 int len, alen;
500
501 rqstp->rq_addrlen = sizeof(rqstp->rq_addr);
502 sock = rqstp->rq_sock->sk_sock;
503
504 msg.msg_name = &rqstp->rq_addr;
505 msg.msg_namelen = sizeof(rqstp->rq_addr);
506 msg.msg_control = NULL;
507 msg.msg_controllen = 0;
508
509 msg.msg_flags = MSG_DONTWAIT;
510
511 len = kernel_recvmsg(sock, &msg, iov, nr, buflen, MSG_DONTWAIT);
512
513 /* sock_recvmsg doesn't fill in the name/namelen, so we must..
514 * possibly we should cache this in the svc_sock structure
515 * at accept time. FIXME
516 */
517 alen = sizeof(rqstp->rq_addr);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700518 kernel_getpeername(sock, (struct sockaddr *)&rqstp->rq_addr, &alen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
521 rqstp->rq_sock, iov[0].iov_base, iov[0].iov_len, len);
522
523 return len;
524}
525
526/*
527 * Set socket snd and rcv buffer lengths
528 */
529static inline void
530svc_sock_setbufsize(struct socket *sock, unsigned int snd, unsigned int rcv)
531{
532#if 0
533 mm_segment_t oldfs;
534 oldfs = get_fs(); set_fs(KERNEL_DS);
535 sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
536 (char*)&snd, sizeof(snd));
537 sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
538 (char*)&rcv, sizeof(rcv));
539#else
540 /* sock_setsockopt limits use to sysctl_?mem_max,
541 * which isn't acceptable. Until that is made conditional
542 * on not having CAP_SYS_RESOURCE or similar, we go direct...
543 * DaveM said I could!
544 */
545 lock_sock(sock->sk);
546 sock->sk->sk_sndbuf = snd * 2;
547 sock->sk->sk_rcvbuf = rcv * 2;
548 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
549 release_sock(sock->sk);
550#endif
551}
552/*
553 * INET callback when data has been received on the socket.
554 */
555static void
556svc_udp_data_ready(struct sock *sk, int count)
557{
Neil Brown939bb7e2005-09-13 01:25:39 -0700558 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Neil Brown939bb7e2005-09-13 01:25:39 -0700560 if (svsk) {
561 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
562 svsk, sk, count, test_bit(SK_BUSY, &svsk->sk_flags));
563 set_bit(SK_DATA, &svsk->sk_flags);
564 svc_sock_enqueue(svsk);
565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
567 wake_up_interruptible(sk->sk_sleep);
568}
569
570/*
571 * INET callback when space is newly available on the socket.
572 */
573static void
574svc_write_space(struct sock *sk)
575{
576 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
577
578 if (svsk) {
579 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
580 svsk, sk, test_bit(SK_BUSY, &svsk->sk_flags));
581 svc_sock_enqueue(svsk);
582 }
583
584 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) {
Neil Brown939bb7e2005-09-13 01:25:39 -0700585 dprintk("RPC svc_write_space: someone sleeping on %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 svsk);
587 wake_up_interruptible(sk->sk_sleep);
588 }
589}
590
591/*
592 * Receive a datagram from a UDP socket.
593 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594static int
595svc_udp_recvfrom(struct svc_rqst *rqstp)
596{
597 struct svc_sock *svsk = rqstp->rq_sock;
598 struct svc_serv *serv = svsk->sk_server;
599 struct sk_buff *skb;
600 int err, len;
601
602 if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
603 /* udp sockets need large rcvbuf as all pending
604 * requests are still in that buffer. sndbuf must
605 * also be large enough that there is enough space
606 * for one reply per thread.
607 */
608 svc_sock_setbufsize(svsk->sk_sock,
609 (serv->sv_nrthreads+3) * serv->sv_bufsz,
610 (serv->sv_nrthreads+3) * serv->sv_bufsz);
611
612 if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
613 svc_sock_received(svsk);
614 return svc_deferred_recv(rqstp);
615 }
616
617 clear_bit(SK_DATA, &svsk->sk_flags);
618 while ((skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err)) == NULL) {
619 if (err == -EAGAIN) {
620 svc_sock_received(svsk);
621 return err;
622 }
623 /* possibly an icmp error */
624 dprintk("svc: recvfrom returned error %d\n", -err);
625 }
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700626 if (skb->tstamp.off_sec == 0) {
627 struct timeval tv;
628
629 tv.tv_sec = xtime.tv_sec;
Andrew Morton4bcde032005-10-26 01:59:03 -0700630 tv.tv_usec = xtime.tv_nsec / NSEC_PER_USEC;
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700631 skb_set_timestamp(skb, &tv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /* Don't enable netstamp, sunrpc doesn't
633 need that much accuracy */
634 }
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700635 skb_get_timestamp(skb, &svsk->sk_sk->sk_stamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 set_bit(SK_DATA, &svsk->sk_flags); /* there may be more data... */
637
638 /*
639 * Maybe more packets - kick another thread ASAP.
640 */
641 svc_sock_received(svsk);
642
643 len = skb->len - sizeof(struct udphdr);
644 rqstp->rq_arg.len = len;
645
646 rqstp->rq_prot = IPPROTO_UDP;
647
648 /* Get sender address */
649 rqstp->rq_addr.sin_family = AF_INET;
650 rqstp->rq_addr.sin_port = skb->h.uh->source;
651 rqstp->rq_addr.sin_addr.s_addr = skb->nh.iph->saddr;
652 rqstp->rq_daddr = skb->nh.iph->daddr;
653
654 if (skb_is_nonlinear(skb)) {
655 /* we have to copy */
656 local_bh_disable();
657 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
658 local_bh_enable();
659 /* checksum error */
660 skb_free_datagram(svsk->sk_sk, skb);
661 return 0;
662 }
663 local_bh_enable();
664 skb_free_datagram(svsk->sk_sk, skb);
665 } else {
666 /* we can use it in-place */
667 rqstp->rq_arg.head[0].iov_base = skb->data + sizeof(struct udphdr);
668 rqstp->rq_arg.head[0].iov_len = len;
Herbert Xufb286bb2005-11-10 13:01:24 -0800669 if (skb_checksum_complete(skb)) {
670 skb_free_datagram(svsk->sk_sk, skb);
671 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673 rqstp->rq_skbuff = skb;
674 }
675
676 rqstp->rq_arg.page_base = 0;
677 if (len <= rqstp->rq_arg.head[0].iov_len) {
678 rqstp->rq_arg.head[0].iov_len = len;
679 rqstp->rq_arg.page_len = 0;
680 } else {
681 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
682 rqstp->rq_argused += (rqstp->rq_arg.page_len + PAGE_SIZE - 1)/ PAGE_SIZE;
683 }
684
685 if (serv->sv_stats)
686 serv->sv_stats->netudpcnt++;
687
688 return len;
689}
690
691static int
692svc_udp_sendto(struct svc_rqst *rqstp)
693{
694 int error;
695
696 error = svc_sendto(rqstp, &rqstp->rq_res);
697 if (error == -ECONNREFUSED)
698 /* ICMP error on earlier request. */
699 error = svc_sendto(rqstp, &rqstp->rq_res);
700
701 return error;
702}
703
704static void
705svc_udp_init(struct svc_sock *svsk)
706{
707 svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
708 svsk->sk_sk->sk_write_space = svc_write_space;
709 svsk->sk_recvfrom = svc_udp_recvfrom;
710 svsk->sk_sendto = svc_udp_sendto;
711
712 /* initialise setting must have enough space to
713 * receive and respond to one request.
714 * svc_udp_recvfrom will re-adjust if necessary
715 */
716 svc_sock_setbufsize(svsk->sk_sock,
717 3 * svsk->sk_server->sv_bufsz,
718 3 * svsk->sk_server->sv_bufsz);
719
720 set_bit(SK_DATA, &svsk->sk_flags); /* might have come in before data_ready set up */
721 set_bit(SK_CHNGBUF, &svsk->sk_flags);
722}
723
724/*
725 * A data_ready event on a listening socket means there's a connection
726 * pending. Do not use state_change as a substitute for it.
727 */
728static void
729svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
730{
Neil Brown939bb7e2005-09-13 01:25:39 -0700731 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 dprintk("svc: socket %p TCP (listen) state change %d\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700734 sk, sk->sk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Neil Brown939bb7e2005-09-13 01:25:39 -0700736 /*
737 * This callback may called twice when a new connection
738 * is established as a child socket inherits everything
739 * from a parent LISTEN socket.
740 * 1) data_ready method of the parent socket will be called
741 * when one of child sockets become ESTABLISHED.
742 * 2) data_ready method of the child socket may be called
743 * when it receives data before the socket is accepted.
744 * In case of 2, we should ignore it silently.
745 */
746 if (sk->sk_state == TCP_LISTEN) {
747 if (svsk) {
748 set_bit(SK_CONN, &svsk->sk_flags);
749 svc_sock_enqueue(svsk);
750 } else
751 printk("svc: socket %p: no user data\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Neil Brown939bb7e2005-09-13 01:25:39 -0700753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
755 wake_up_interruptible_all(sk->sk_sleep);
756}
757
758/*
759 * A state change on a connected socket means it's dying or dead.
760 */
761static void
762svc_tcp_state_change(struct sock *sk)
763{
Neil Brown939bb7e2005-09-13 01:25:39 -0700764 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700767 sk, sk->sk_state, sk->sk_user_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Neil Brown939bb7e2005-09-13 01:25:39 -0700769 if (!svsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 printk("svc: socket %p: no user data\n", sk);
Neil Brown939bb7e2005-09-13 01:25:39 -0700771 else {
772 set_bit(SK_CLOSE, &svsk->sk_flags);
773 svc_sock_enqueue(svsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
776 wake_up_interruptible_all(sk->sk_sleep);
777}
778
779static void
780svc_tcp_data_ready(struct sock *sk, int count)
781{
Neil Brown939bb7e2005-09-13 01:25:39 -0700782 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700785 sk, sk->sk_user_data);
786 if (svsk) {
787 set_bit(SK_DATA, &svsk->sk_flags);
788 svc_sock_enqueue(svsk);
789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
791 wake_up_interruptible(sk->sk_sleep);
792}
793
794/*
795 * Accept a TCP connection
796 */
797static void
798svc_tcp_accept(struct svc_sock *svsk)
799{
800 struct sockaddr_in sin;
801 struct svc_serv *serv = svsk->sk_server;
802 struct socket *sock = svsk->sk_sock;
803 struct socket *newsock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 struct svc_sock *newsvsk;
805 int err, slen;
806
807 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
808 if (!sock)
809 return;
810
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700811 clear_bit(SK_CONN, &svsk->sk_flags);
812 err = kernel_accept(sock, &newsock, O_NONBLOCK);
813 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (err == -ENOMEM)
815 printk(KERN_WARNING "%s: no more sockets!\n",
816 serv->sv_name);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700817 else if (err != -EAGAIN && net_ratelimit())
818 printk(KERN_WARNING "%s: accept failed (err %d)!\n",
819 serv->sv_name, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return;
821 }
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 set_bit(SK_CONN, &svsk->sk_flags);
824 svc_sock_enqueue(svsk);
825
826 slen = sizeof(sin);
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700827 err = kernel_getpeername(newsock, (struct sockaddr *) &sin, &slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (err < 0) {
829 if (net_ratelimit())
830 printk(KERN_WARNING "%s: peername failed (err %d)!\n",
831 serv->sv_name, -err);
832 goto failed; /* aborted connection or whatever */
833 }
834
835 /* Ideally, we would want to reject connections from unauthorized
836 * hosts here, but when we get encription, the IP of the host won't
837 * tell us anything. For now just warn about unpriv connections.
838 */
839 if (ntohs(sin.sin_port) >= 1024) {
840 dprintk(KERN_WARNING
841 "%s: connect from unprivileged port: %u.%u.%u.%u:%d\n",
842 serv->sv_name,
843 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
844 }
845
846 dprintk("%s: connect from %u.%u.%u.%u:%04x\n", serv->sv_name,
847 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
848
849 /* make sure that a write doesn't block forever when
850 * low on memory
851 */
852 newsock->sk->sk_sndtimeo = HZ*30;
853
854 if (!(newsvsk = svc_setup_socket(serv, newsock, &err, 0)))
855 goto failed;
856
857
858 /* make sure that we don't have too many active connections.
859 * If we have, something must be dropped.
860 *
861 * There's no point in trying to do random drop here for
862 * DoS prevention. The NFS clients does 1 reconnect in 15
863 * seconds. An attacker can easily beat that.
864 *
865 * The only somewhat efficient mechanism would be if drop
866 * old connections from the same IP first. But right now
867 * we don't even record the client IP in svc_sock.
868 */
869 if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) {
870 struct svc_sock *svsk = NULL;
871 spin_lock_bh(&serv->sv_lock);
872 if (!list_empty(&serv->sv_tempsocks)) {
873 if (net_ratelimit()) {
874 /* Try to help the admin */
875 printk(KERN_NOTICE "%s: too many open TCP "
876 "sockets, consider increasing the "
877 "number of nfsd threads\n",
878 serv->sv_name);
879 printk(KERN_NOTICE "%s: last TCP connect from "
880 "%u.%u.%u.%u:%d\n",
881 serv->sv_name,
882 NIPQUAD(sin.sin_addr.s_addr),
883 ntohs(sin.sin_port));
884 }
885 /*
886 * Always select the oldest socket. It's not fair,
887 * but so is life
888 */
889 svsk = list_entry(serv->sv_tempsocks.prev,
890 struct svc_sock,
891 sk_list);
892 set_bit(SK_CLOSE, &svsk->sk_flags);
Greg Banksc45c3572006-10-02 02:17:54 -0700893 atomic_inc(&svsk->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895 spin_unlock_bh(&serv->sv_lock);
896
897 if (svsk) {
898 svc_sock_enqueue(svsk);
899 svc_sock_put(svsk);
900 }
901
902 }
903
904 if (serv->sv_stats)
905 serv->sv_stats->nettcpconn++;
906
907 return;
908
909failed:
910 sock_release(newsock);
911 return;
912}
913
914/*
915 * Receive data from a TCP socket.
916 */
917static int
918svc_tcp_recvfrom(struct svc_rqst *rqstp)
919{
920 struct svc_sock *svsk = rqstp->rq_sock;
921 struct svc_serv *serv = svsk->sk_server;
922 int len;
923 struct kvec vec[RPCSVC_MAXPAGES];
924 int pnum, vlen;
925
926 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
927 svsk, test_bit(SK_DATA, &svsk->sk_flags),
928 test_bit(SK_CONN, &svsk->sk_flags),
929 test_bit(SK_CLOSE, &svsk->sk_flags));
930
931 if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
932 svc_sock_received(svsk);
933 return svc_deferred_recv(rqstp);
934 }
935
936 if (test_bit(SK_CLOSE, &svsk->sk_flags)) {
937 svc_delete_socket(svsk);
938 return 0;
939 }
940
941 if (test_bit(SK_CONN, &svsk->sk_flags)) {
942 svc_tcp_accept(svsk);
943 svc_sock_received(svsk);
944 return 0;
945 }
946
947 if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
948 /* sndbuf needs to have room for one request
949 * per thread, otherwise we can stall even when the
950 * network isn't a bottleneck.
951 * rcvbuf just needs to be able to hold a few requests.
952 * Normally they will be removed from the queue
953 * as soon a a complete request arrives.
954 */
955 svc_sock_setbufsize(svsk->sk_sock,
956 (serv->sv_nrthreads+3) * serv->sv_bufsz,
957 3 * serv->sv_bufsz);
958
959 clear_bit(SK_DATA, &svsk->sk_flags);
960
961 /* Receive data. If we haven't got the record length yet, get
962 * the next four bytes. Otherwise try to gobble up as much as
963 * possible up to the complete record length.
964 */
965 if (svsk->sk_tcplen < 4) {
966 unsigned long want = 4 - svsk->sk_tcplen;
967 struct kvec iov;
968
969 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
970 iov.iov_len = want;
971 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
972 goto error;
973 svsk->sk_tcplen += len;
974
975 if (len < want) {
976 dprintk("svc: short recvfrom while reading record length (%d of %lu)\n",
977 len, want);
978 svc_sock_received(svsk);
979 return -EAGAIN; /* record header not complete */
980 }
981
982 svsk->sk_reclen = ntohl(svsk->sk_reclen);
983 if (!(svsk->sk_reclen & 0x80000000)) {
984 /* FIXME: technically, a record can be fragmented,
985 * and non-terminal fragments will not have the top
986 * bit set in the fragment length header.
987 * But apparently no known nfs clients send fragmented
988 * records. */
989 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx (non-terminal)\n",
990 (unsigned long) svsk->sk_reclen);
991 goto err_delete;
992 }
993 svsk->sk_reclen &= 0x7fffffff;
994 dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
995 if (svsk->sk_reclen > serv->sv_bufsz) {
996 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx (large)\n",
997 (unsigned long) svsk->sk_reclen);
998 goto err_delete;
999 }
1000 }
1001
1002 /* Check whether enough data is available */
1003 len = svc_recv_available(svsk);
1004 if (len < 0)
1005 goto error;
1006
1007 if (len < svsk->sk_reclen) {
1008 dprintk("svc: incomplete TCP record (%d of %d)\n",
1009 len, svsk->sk_reclen);
1010 svc_sock_received(svsk);
1011 return -EAGAIN; /* record not complete */
1012 }
1013 len = svsk->sk_reclen;
1014 set_bit(SK_DATA, &svsk->sk_flags);
1015
1016 vec[0] = rqstp->rq_arg.head[0];
1017 vlen = PAGE_SIZE;
1018 pnum = 1;
1019 while (vlen < len) {
1020 vec[pnum].iov_base = page_address(rqstp->rq_argpages[rqstp->rq_argused++]);
1021 vec[pnum].iov_len = PAGE_SIZE;
1022 pnum++;
1023 vlen += PAGE_SIZE;
1024 }
1025
1026 /* Now receive data */
1027 len = svc_recvfrom(rqstp, vec, pnum, len);
1028 if (len < 0)
1029 goto error;
1030
1031 dprintk("svc: TCP complete record (%d bytes)\n", len);
1032 rqstp->rq_arg.len = len;
1033 rqstp->rq_arg.page_base = 0;
1034 if (len <= rqstp->rq_arg.head[0].iov_len) {
1035 rqstp->rq_arg.head[0].iov_len = len;
1036 rqstp->rq_arg.page_len = 0;
1037 } else {
1038 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
1039 }
1040
1041 rqstp->rq_skbuff = NULL;
1042 rqstp->rq_prot = IPPROTO_TCP;
1043
1044 /* Reset TCP read info */
1045 svsk->sk_reclen = 0;
1046 svsk->sk_tcplen = 0;
1047
1048 svc_sock_received(svsk);
1049 if (serv->sv_stats)
1050 serv->sv_stats->nettcpcnt++;
1051
1052 return len;
1053
1054 err_delete:
1055 svc_delete_socket(svsk);
1056 return -EAGAIN;
1057
1058 error:
1059 if (len == -EAGAIN) {
1060 dprintk("RPC: TCP recvfrom got EAGAIN\n");
1061 svc_sock_received(svsk);
1062 } else {
1063 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1064 svsk->sk_server->sv_name, -len);
Olaf Kirch93fbf1a2006-01-06 00:19:56 -08001065 goto err_delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067
1068 return len;
1069}
1070
1071/*
1072 * Send out data on TCP socket.
1073 */
1074static int
1075svc_tcp_sendto(struct svc_rqst *rqstp)
1076{
1077 struct xdr_buf *xbufp = &rqstp->rq_res;
1078 int sent;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001079 __be32 reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 /* Set up the first element of the reply kvec.
1082 * Any other kvecs that may be in use have been taken
1083 * care of by the server implementation itself.
1084 */
1085 reclen = htonl(0x80000000|((xbufp->len ) - 4));
1086 memcpy(xbufp->head[0].iov_base, &reclen, 4);
1087
1088 if (test_bit(SK_DEAD, &rqstp->rq_sock->sk_flags))
1089 return -ENOTCONN;
1090
1091 sent = svc_sendto(rqstp, &rqstp->rq_res);
1092 if (sent != xbufp->len) {
1093 printk(KERN_NOTICE "rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
1094 rqstp->rq_sock->sk_server->sv_name,
1095 (sent<0)?"got error":"sent only",
1096 sent, xbufp->len);
1097 svc_delete_socket(rqstp->rq_sock);
1098 sent = -EAGAIN;
1099 }
1100 return sent;
1101}
1102
1103static void
1104svc_tcp_init(struct svc_sock *svsk)
1105{
1106 struct sock *sk = svsk->sk_sk;
1107 struct tcp_sock *tp = tcp_sk(sk);
1108
1109 svsk->sk_recvfrom = svc_tcp_recvfrom;
1110 svsk->sk_sendto = svc_tcp_sendto;
1111
1112 if (sk->sk_state == TCP_LISTEN) {
1113 dprintk("setting up TCP socket for listening\n");
1114 sk->sk_data_ready = svc_tcp_listen_data_ready;
1115 set_bit(SK_CONN, &svsk->sk_flags);
1116 } else {
1117 dprintk("setting up TCP socket for reading\n");
1118 sk->sk_state_change = svc_tcp_state_change;
1119 sk->sk_data_ready = svc_tcp_data_ready;
1120 sk->sk_write_space = svc_write_space;
1121
1122 svsk->sk_reclen = 0;
1123 svsk->sk_tcplen = 0;
1124
1125 tp->nonagle = 1; /* disable Nagle's algorithm */
1126
1127 /* initialise setting must have enough space to
1128 * receive and respond to one request.
1129 * svc_tcp_recvfrom will re-adjust if necessary
1130 */
1131 svc_sock_setbufsize(svsk->sk_sock,
1132 3 * svsk->sk_server->sv_bufsz,
1133 3 * svsk->sk_server->sv_bufsz);
1134
1135 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1136 set_bit(SK_DATA, &svsk->sk_flags);
1137 if (sk->sk_state != TCP_ESTABLISHED)
1138 set_bit(SK_CLOSE, &svsk->sk_flags);
1139 }
1140}
1141
1142void
1143svc_sock_update_bufs(struct svc_serv *serv)
1144{
1145 /*
1146 * The number of server threads has changed. Update
1147 * rcvbuf and sndbuf accordingly on all sockets
1148 */
1149 struct list_head *le;
1150
1151 spin_lock_bh(&serv->sv_lock);
1152 list_for_each(le, &serv->sv_permsocks) {
1153 struct svc_sock *svsk =
1154 list_entry(le, struct svc_sock, sk_list);
1155 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1156 }
1157 list_for_each(le, &serv->sv_tempsocks) {
1158 struct svc_sock *svsk =
1159 list_entry(le, struct svc_sock, sk_list);
1160 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1161 }
1162 spin_unlock_bh(&serv->sv_lock);
1163}
1164
1165/*
1166 * Receive the next request on any socket.
1167 */
1168int
NeilBrown6fb2b472006-10-02 02:17:50 -07001169svc_recv(struct svc_rqst *rqstp, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 struct svc_sock *svsk =NULL;
NeilBrown6fb2b472006-10-02 02:17:50 -07001172 struct svc_serv *serv = rqstp->rq_server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 int len;
1174 int pages;
1175 struct xdr_buf *arg;
1176 DECLARE_WAITQUEUE(wait, current);
1177
1178 dprintk("svc: server %p waiting for data (to = %ld)\n",
1179 rqstp, timeout);
1180
1181 if (rqstp->rq_sock)
1182 printk(KERN_ERR
1183 "svc_recv: service %p, socket not NULL!\n",
1184 rqstp);
1185 if (waitqueue_active(&rqstp->rq_wait))
1186 printk(KERN_ERR
1187 "svc_recv: service %p, wait queue active!\n",
1188 rqstp);
1189
1190 /* Initialize the buffers */
1191 /* first reclaim pages that were moved to response list */
1192 svc_pushback_allpages(rqstp);
1193
1194 /* now allocate needed pages. If we get a failure, sleep briefly */
1195 pages = 2 + (serv->sv_bufsz + PAGE_SIZE -1) / PAGE_SIZE;
1196 while (rqstp->rq_arghi < pages) {
1197 struct page *p = alloc_page(GFP_KERNEL);
1198 if (!p) {
Nishanth Aravamudan121caf52005-09-12 14:15:34 -07001199 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 continue;
1201 }
1202 rqstp->rq_argpages[rqstp->rq_arghi++] = p;
1203 }
1204
1205 /* Make arg->head point to first page and arg->pages point to rest */
1206 arg = &rqstp->rq_arg;
1207 arg->head[0].iov_base = page_address(rqstp->rq_argpages[0]);
1208 arg->head[0].iov_len = PAGE_SIZE;
1209 rqstp->rq_argused = 1;
1210 arg->pages = rqstp->rq_argpages + 1;
1211 arg->page_base = 0;
1212 /* save at least one page for response */
1213 arg->page_len = (pages-2)*PAGE_SIZE;
1214 arg->len = (pages-1)*PAGE_SIZE;
1215 arg->tail[0].iov_len = 0;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001216
1217 try_to_freeze();
NeilBrown1887b932005-11-15 00:09:10 -08001218 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (signalled())
1220 return -EINTR;
1221
1222 spin_lock_bh(&serv->sv_lock);
Greg Banks36bdfc82006-10-02 02:17:54 -07001223 if ((svsk = svc_sock_dequeue(serv)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 rqstp->rq_sock = svsk;
Greg Banksc45c3572006-10-02 02:17:54 -07001225 atomic_inc(&svsk->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 rqstp->rq_reserved = serv->sv_bufsz;
Greg Banks5685f0f2006-10-02 02:17:56 -07001227 atomic_add(rqstp->rq_reserved, &svsk->sk_reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 } else {
1229 /* No data pending. Go to sleep */
1230 svc_serv_enqueue(serv, rqstp);
1231
1232 /*
1233 * We have to be able to interrupt this wait
1234 * to bring down the daemons ...
1235 */
1236 set_current_state(TASK_INTERRUPTIBLE);
1237 add_wait_queue(&rqstp->rq_wait, &wait);
1238 spin_unlock_bh(&serv->sv_lock);
1239
1240 schedule_timeout(timeout);
1241
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001242 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 spin_lock_bh(&serv->sv_lock);
1245 remove_wait_queue(&rqstp->rq_wait, &wait);
1246
1247 if (!(svsk = rqstp->rq_sock)) {
1248 svc_serv_dequeue(serv, rqstp);
1249 spin_unlock_bh(&serv->sv_lock);
1250 dprintk("svc: server %p, no data yet\n", rqstp);
1251 return signalled()? -EINTR : -EAGAIN;
1252 }
1253 }
1254 spin_unlock_bh(&serv->sv_lock);
1255
1256 dprintk("svc: server %p, socket %p, inuse=%d\n",
Greg Banksc45c3572006-10-02 02:17:54 -07001257 rqstp, svsk, atomic_read(&svsk->sk_inuse));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 len = svsk->sk_recvfrom(rqstp);
1259 dprintk("svc: got len=%d\n", len);
1260
1261 /* No data, incomplete (TCP) read, or accept() */
1262 if (len == 0 || len == -EAGAIN) {
1263 rqstp->rq_res.len = 0;
1264 svc_sock_release(rqstp);
1265 return -EAGAIN;
1266 }
1267 svsk->sk_lastrecv = get_seconds();
Greg Banks36bdfc82006-10-02 02:17:54 -07001268 clear_bit(SK_OLD, &svsk->sk_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 rqstp->rq_secure = ntohs(rqstp->rq_addr.sin_port) < 1024;
1271 rqstp->rq_chandle.defer = svc_defer;
1272
1273 if (serv->sv_stats)
1274 serv->sv_stats->netcnt++;
1275 return len;
1276}
1277
1278/*
1279 * Drop request
1280 */
1281void
1282svc_drop(struct svc_rqst *rqstp)
1283{
1284 dprintk("svc: socket %p dropped request\n", rqstp->rq_sock);
1285 svc_sock_release(rqstp);
1286}
1287
1288/*
1289 * Return reply to client.
1290 */
1291int
1292svc_send(struct svc_rqst *rqstp)
1293{
1294 struct svc_sock *svsk;
1295 int len;
1296 struct xdr_buf *xb;
1297
1298 if ((svsk = rqstp->rq_sock) == NULL) {
1299 printk(KERN_WARNING "NULL socket pointer in %s:%d\n",
1300 __FILE__, __LINE__);
1301 return -EFAULT;
1302 }
1303
1304 /* release the receive skb before sending the reply */
1305 svc_release_skb(rqstp);
1306
1307 /* calculate over-all length */
1308 xb = & rqstp->rq_res;
1309 xb->len = xb->head[0].iov_len +
1310 xb->page_len +
1311 xb->tail[0].iov_len;
1312
Ingo Molnar57b47a52006-03-20 22:35:41 -08001313 /* Grab svsk->sk_mutex to serialize outgoing data. */
1314 mutex_lock(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (test_bit(SK_DEAD, &svsk->sk_flags))
1316 len = -ENOTCONN;
1317 else
1318 len = svsk->sk_sendto(rqstp);
Ingo Molnar57b47a52006-03-20 22:35:41 -08001319 mutex_unlock(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 svc_sock_release(rqstp);
1321
1322 if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
1323 return 0;
1324 return len;
1325}
1326
1327/*
Greg Banks36bdfc82006-10-02 02:17:54 -07001328 * Timer function to close old temporary sockets, using
1329 * a mark-and-sweep algorithm.
1330 */
1331static void
1332svc_age_temp_sockets(unsigned long closure)
1333{
1334 struct svc_serv *serv = (struct svc_serv *)closure;
1335 struct svc_sock *svsk;
1336 struct list_head *le, *next;
1337 LIST_HEAD(to_be_aged);
1338
1339 dprintk("svc_age_temp_sockets\n");
1340
1341 if (!spin_trylock_bh(&serv->sv_lock)) {
1342 /* busy, try again 1 sec later */
1343 dprintk("svc_age_temp_sockets: busy\n");
1344 mod_timer(&serv->sv_temptimer, jiffies + HZ);
1345 return;
1346 }
1347
1348 list_for_each_safe(le, next, &serv->sv_tempsocks) {
1349 svsk = list_entry(le, struct svc_sock, sk_list);
1350
1351 if (!test_and_set_bit(SK_OLD, &svsk->sk_flags))
1352 continue;
Greg Banksc45c3572006-10-02 02:17:54 -07001353 if (atomic_read(&svsk->sk_inuse) || test_bit(SK_BUSY, &svsk->sk_flags))
Greg Banks36bdfc82006-10-02 02:17:54 -07001354 continue;
Greg Banksc45c3572006-10-02 02:17:54 -07001355 atomic_inc(&svsk->sk_inuse);
Greg Banks36bdfc82006-10-02 02:17:54 -07001356 list_move(le, &to_be_aged);
1357 set_bit(SK_CLOSE, &svsk->sk_flags);
1358 set_bit(SK_DETACHED, &svsk->sk_flags);
1359 }
1360 spin_unlock_bh(&serv->sv_lock);
1361
1362 while (!list_empty(&to_be_aged)) {
1363 le = to_be_aged.next;
1364 /* fiddling the sk_list node is safe 'cos we're SK_DETACHED */
1365 list_del_init(le);
1366 svsk = list_entry(le, struct svc_sock, sk_list);
1367
1368 dprintk("queuing svsk %p for closing, %lu seconds old\n",
1369 svsk, get_seconds() - svsk->sk_lastrecv);
1370
1371 /* a thread will dequeue and close it soon */
1372 svc_sock_enqueue(svsk);
1373 svc_sock_put(svsk);
1374 }
1375
1376 mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
1377}
1378
1379/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 * Initialize socket for RPC use and create svc_sock struct
1381 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1382 */
1383static struct svc_sock *
1384svc_setup_socket(struct svc_serv *serv, struct socket *sock,
1385 int *errp, int pmap_register)
1386{
1387 struct svc_sock *svsk;
1388 struct sock *inet;
1389
1390 dprintk("svc: svc_setup_socket %p\n", sock);
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001391 if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 *errp = -ENOMEM;
1393 return NULL;
1394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 inet = sock->sk;
1397
1398 /* Register socket with portmapper */
1399 if (*errp >= 0 && pmap_register)
1400 *errp = svc_register(serv, inet->sk_protocol,
1401 ntohs(inet_sk(inet)->sport));
1402
1403 if (*errp < 0) {
1404 kfree(svsk);
1405 return NULL;
1406 }
1407
1408 set_bit(SK_BUSY, &svsk->sk_flags);
1409 inet->sk_user_data = svsk;
1410 svsk->sk_sock = sock;
1411 svsk->sk_sk = inet;
1412 svsk->sk_ostate = inet->sk_state_change;
1413 svsk->sk_odata = inet->sk_data_ready;
1414 svsk->sk_owspace = inet->sk_write_space;
1415 svsk->sk_server = serv;
Greg Banksc45c3572006-10-02 02:17:54 -07001416 atomic_set(&svsk->sk_inuse, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 svsk->sk_lastrecv = get_seconds();
Greg Banks1a68d952006-10-02 02:17:55 -07001418 spin_lock_init(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 INIT_LIST_HEAD(&svsk->sk_deferred);
1420 INIT_LIST_HEAD(&svsk->sk_ready);
Ingo Molnar57b47a52006-03-20 22:35:41 -08001421 mutex_init(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 /* Initialize the socket */
1424 if (sock->type == SOCK_DGRAM)
1425 svc_udp_init(svsk);
1426 else
1427 svc_tcp_init(svsk);
1428
1429 spin_lock_bh(&serv->sv_lock);
1430 if (!pmap_register) {
1431 set_bit(SK_TEMP, &svsk->sk_flags);
1432 list_add(&svsk->sk_list, &serv->sv_tempsocks);
1433 serv->sv_tmpcnt++;
Greg Banks36bdfc82006-10-02 02:17:54 -07001434 if (serv->sv_temptimer.function == NULL) {
1435 /* setup timer to age temp sockets */
1436 setup_timer(&serv->sv_temptimer, svc_age_temp_sockets,
1437 (unsigned long)serv);
1438 mod_timer(&serv->sv_temptimer,
1439 jiffies + svc_conn_age_period * HZ);
1440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 } else {
1442 clear_bit(SK_TEMP, &svsk->sk_flags);
1443 list_add(&svsk->sk_list, &serv->sv_permsocks);
1444 }
1445 spin_unlock_bh(&serv->sv_lock);
1446
1447 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1448 svsk, svsk->sk_sk);
1449
1450 clear_bit(SK_BUSY, &svsk->sk_flags);
1451 svc_sock_enqueue(svsk);
1452 return svsk;
1453}
1454
NeilBrownb41b66d2006-10-02 02:17:48 -07001455int svc_addsock(struct svc_serv *serv,
1456 int fd,
1457 char *name_return,
1458 int *proto)
1459{
1460 int err = 0;
1461 struct socket *so = sockfd_lookup(fd, &err);
1462 struct svc_sock *svsk = NULL;
1463
1464 if (!so)
1465 return err;
1466 if (so->sk->sk_family != AF_INET)
1467 err = -EAFNOSUPPORT;
1468 else if (so->sk->sk_protocol != IPPROTO_TCP &&
1469 so->sk->sk_protocol != IPPROTO_UDP)
1470 err = -EPROTONOSUPPORT;
1471 else if (so->state > SS_UNCONNECTED)
1472 err = -EISCONN;
1473 else {
1474 svsk = svc_setup_socket(serv, so, &err, 1);
1475 if (svsk)
1476 err = 0;
1477 }
1478 if (err) {
1479 sockfd_put(so);
1480 return err;
1481 }
1482 if (proto) *proto = so->sk->sk_protocol;
1483 return one_sock_name(name_return, svsk);
1484}
1485EXPORT_SYMBOL_GPL(svc_addsock);
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487/*
1488 * Create socket for RPC service.
1489 */
1490static int
1491svc_create_socket(struct svc_serv *serv, int protocol, struct sockaddr_in *sin)
1492{
1493 struct svc_sock *svsk;
1494 struct socket *sock;
1495 int error;
1496 int type;
1497
1498 dprintk("svc: svc_create_socket(%s, %d, %u.%u.%u.%u:%d)\n",
1499 serv->sv_program->pg_name, protocol,
1500 NIPQUAD(sin->sin_addr.s_addr),
1501 ntohs(sin->sin_port));
1502
1503 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1504 printk(KERN_WARNING "svc: only UDP and TCP "
1505 "sockets supported\n");
1506 return -EINVAL;
1507 }
1508 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1509
1510 if ((error = sock_create_kern(PF_INET, type, protocol, &sock)) < 0)
1511 return error;
1512
Eric Sesterhenn18114742006-09-28 14:37:07 -07001513 if (type == SOCK_STREAM)
1514 sock->sk->sk_reuse = 1; /* allow address reuse */
1515 error = kernel_bind(sock, (struct sockaddr *) sin,
1516 sizeof(*sin));
1517 if (error < 0)
1518 goto bummer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 if (protocol == IPPROTO_TCP) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001521 if ((error = kernel_listen(sock, 64)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 goto bummer;
1523 }
1524
1525 if ((svsk = svc_setup_socket(serv, sock, &error, 1)) != NULL)
1526 return 0;
1527
1528bummer:
1529 dprintk("svc: svc_create_socket error = %d\n", -error);
1530 sock_release(sock);
1531 return error;
1532}
1533
1534/*
1535 * Remove a dead socket
1536 */
1537void
1538svc_delete_socket(struct svc_sock *svsk)
1539{
1540 struct svc_serv *serv;
1541 struct sock *sk;
1542
1543 dprintk("svc: svc_delete_socket(%p)\n", svsk);
1544
1545 serv = svsk->sk_server;
1546 sk = svsk->sk_sk;
1547
1548 sk->sk_state_change = svsk->sk_ostate;
1549 sk->sk_data_ready = svsk->sk_odata;
1550 sk->sk_write_space = svsk->sk_owspace;
1551
1552 spin_lock_bh(&serv->sv_lock);
1553
Greg Banks36bdfc82006-10-02 02:17:54 -07001554 if (!test_and_set_bit(SK_DETACHED, &svsk->sk_flags))
1555 list_del_init(&svsk->sk_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 list_del_init(&svsk->sk_ready);
1557 if (!test_and_set_bit(SK_DEAD, &svsk->sk_flags))
1558 if (test_bit(SK_TEMP, &svsk->sk_flags))
1559 serv->sv_tmpcnt--;
1560
Greg Banksc45c3572006-10-02 02:17:54 -07001561 if (!atomic_read(&svsk->sk_inuse)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 spin_unlock_bh(&serv->sv_lock);
NeilBrownb41b66d2006-10-02 02:17:48 -07001563 if (svsk->sk_sock->file)
1564 sockfd_put(svsk->sk_sock);
1565 else
1566 sock_release(svsk->sk_sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 kfree(svsk);
1568 } else {
1569 spin_unlock_bh(&serv->sv_lock);
1570 dprintk(KERN_NOTICE "svc: server socket destroy delayed\n");
1571 /* svsk->sk_server = NULL; */
1572 }
1573}
1574
1575/*
1576 * Make a socket for nfsd and lockd
1577 */
1578int
1579svc_makesock(struct svc_serv *serv, int protocol, unsigned short port)
1580{
1581 struct sockaddr_in sin;
1582
1583 dprintk("svc: creating socket proto = %d\n", protocol);
1584 sin.sin_family = AF_INET;
1585 sin.sin_addr.s_addr = INADDR_ANY;
1586 sin.sin_port = htons(port);
1587 return svc_create_socket(serv, protocol, &sin);
1588}
1589
1590/*
1591 * Handle defer and revisit of requests
1592 */
1593
1594static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
1595{
1596 struct svc_deferred_req *dr = container_of(dreq, struct svc_deferred_req, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 struct svc_sock *svsk;
1598
1599 if (too_many) {
1600 svc_sock_put(dr->svsk);
1601 kfree(dr);
1602 return;
1603 }
1604 dprintk("revisit queued\n");
1605 svsk = dr->svsk;
1606 dr->svsk = NULL;
Greg Banks1a68d952006-10-02 02:17:55 -07001607 spin_lock_bh(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 list_add(&dr->handle.recent, &svsk->sk_deferred);
Greg Banks1a68d952006-10-02 02:17:55 -07001609 spin_unlock_bh(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 set_bit(SK_DEFERRED, &svsk->sk_flags);
1611 svc_sock_enqueue(svsk);
1612 svc_sock_put(svsk);
1613}
1614
1615static struct cache_deferred_req *
1616svc_defer(struct cache_req *req)
1617{
1618 struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
1619 int size = sizeof(struct svc_deferred_req) + (rqstp->rq_arg.len);
1620 struct svc_deferred_req *dr;
1621
1622 if (rqstp->rq_arg.page_len)
1623 return NULL; /* if more than a page, give up FIXME */
1624 if (rqstp->rq_deferred) {
1625 dr = rqstp->rq_deferred;
1626 rqstp->rq_deferred = NULL;
1627 } else {
1628 int skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
1629 /* FIXME maybe discard if size too large */
1630 dr = kmalloc(size, GFP_KERNEL);
1631 if (dr == NULL)
1632 return NULL;
1633
1634 dr->handle.owner = rqstp->rq_server;
1635 dr->prot = rqstp->rq_prot;
1636 dr->addr = rqstp->rq_addr;
J. Bruce Fields1918e342006-01-18 17:43:16 -08001637 dr->daddr = rqstp->rq_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 dr->argslen = rqstp->rq_arg.len >> 2;
1639 memcpy(dr->args, rqstp->rq_arg.head[0].iov_base-skip, dr->argslen<<2);
1640 }
Greg Banksc45c3572006-10-02 02:17:54 -07001641 atomic_inc(&rqstp->rq_sock->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 dr->svsk = rqstp->rq_sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 dr->handle.revisit = svc_revisit;
1645 return &dr->handle;
1646}
1647
1648/*
1649 * recv data from a deferred request into an active one
1650 */
1651static int svc_deferred_recv(struct svc_rqst *rqstp)
1652{
1653 struct svc_deferred_req *dr = rqstp->rq_deferred;
1654
1655 rqstp->rq_arg.head[0].iov_base = dr->args;
1656 rqstp->rq_arg.head[0].iov_len = dr->argslen<<2;
1657 rqstp->rq_arg.page_len = 0;
1658 rqstp->rq_arg.len = dr->argslen<<2;
1659 rqstp->rq_prot = dr->prot;
1660 rqstp->rq_addr = dr->addr;
J. Bruce Fields1918e342006-01-18 17:43:16 -08001661 rqstp->rq_daddr = dr->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 return dr->argslen<<2;
1663}
1664
1665
1666static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk)
1667{
1668 struct svc_deferred_req *dr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 if (!test_bit(SK_DEFERRED, &svsk->sk_flags))
1671 return NULL;
Greg Banks1a68d952006-10-02 02:17:55 -07001672 spin_lock_bh(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 clear_bit(SK_DEFERRED, &svsk->sk_flags);
1674 if (!list_empty(&svsk->sk_deferred)) {
1675 dr = list_entry(svsk->sk_deferred.next,
1676 struct svc_deferred_req,
1677 handle.recent);
1678 list_del_init(&dr->handle.recent);
1679 set_bit(SK_DEFERRED, &svsk->sk_flags);
1680 }
Greg Banks1a68d952006-10-02 02:17:55 -07001681 spin_unlock_bh(&svsk->sk_defer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 return dr;
1683}