blob: 6e9dc8f96495c2f2f1d0e0f2fa63fe06eb790622 [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
Ilpo Järvinen172589c2007-08-28 15:50:33 -070022#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/sched.h>
24#include <linux/errno.h>
25#include <linux/fcntl.h>
26#include <linux/net.h>
27#include <linux/in.h>
28#include <linux/inet.h>
29#include <linux/udp.h>
Andrew Morton91483c42005-08-09 20:20:07 -070030#include <linux/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/unistd.h>
32#include <linux/slab.h>
33#include <linux/netdevice.h>
34#include <linux/skbuff.h>
NeilBrownb41b66d2006-10-02 02:17:48 -070035#include <linux/file.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080036#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <net/sock.h>
38#include <net/checksum.h>
39#include <net/ip.h>
Chuck Leverb92503b2007-02-12 00:53:36 -080040#include <net/ipv6.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070041#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/uaccess.h>
43#include <asm/ioctls.h>
44
45#include <linux/sunrpc/types.h>
Chuck Leverad06e4b2007-02-12 00:53:32 -080046#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/sunrpc/xdr.h>
48#include <linux/sunrpc/svcsock.h>
49#include <linux/sunrpc/stats.h>
50
51/* SMP locking strategy:
52 *
Greg Banks3262c812006-10-02 02:17:58 -070053 * svc_pool->sp_lock protects most of the fields of that pool.
54 * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
55 * when both need to be taken (rare), svc_serv->sv_lock is first.
56 * BKL protects svc_serv->sv_nrthread.
NeilBrown7ac1bea2007-05-09 02:34:48 -070057 * svc_sock->sk_lock protects the svc_sock->sk_deferred list
58 * and the ->sk_info_authunix cache.
Greg Banksc081a0c2006-10-02 02:17:57 -070059 * svc_sock->sk_flags.SK_BUSY prevents a svc_sock being enqueued multiply.
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 *
61 * Some flags can be set to certain values at any time
62 * providing that certain rules are followed:
63 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * SK_CONN, SK_DATA, can be set or cleared at any time.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -080065 * after a set, svc_sock_enqueue must be called.
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * after a clear, the socket must be read/accepted
67 * if this succeeds, it must be set again.
68 * SK_CLOSE can set at any time. It is never cleared.
NeilBrownaaf68cf2007-02-08 14:20:30 -080069 * sk_inuse contains a bias of '1' until SK_DEAD is set.
70 * so when sk_inuse hits zero, we know the socket is dead
71 * and no-one is using it.
72 * SK_DEAD can only be set while SK_BUSY is held which ensures
73 * no other thread will be using the socket or will try to
74 * set SK_DEAD.
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 *
76 */
77
Tom Tucker360d8732007-12-30 21:07:17 -060078#define RPCDBG_FACILITY RPCDBG_SVCXPRT
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80
81static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *,
Chuck Lever6b174332007-02-12 00:53:28 -080082 int *errp, int flags);
NeilBrownaaf68cf2007-02-08 14:20:30 -080083static void svc_delete_socket(struct svc_sock *svsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static void svc_udp_data_ready(struct sock *, int);
85static int svc_udp_recvfrom(struct svc_rqst *);
86static int svc_udp_sendto(struct svc_rqst *);
NeilBrowncda1fd42007-03-06 01:42:22 -080087static void svc_close_socket(struct svc_sock *svsk);
Tom Tucker755ccea2007-12-30 21:07:27 -060088static void svc_sock_detach(struct svc_xprt *);
89static void svc_sock_free(struct svc_xprt *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk);
92static int svc_deferred_recv(struct svc_rqst *rqstp);
93static struct cache_deferred_req *svc_defer(struct cache_req *req);
94
Greg Banks36bdfc82006-10-02 02:17:54 -070095/* apparently the "standard" is that clients close
96 * idle connections after 5 minutes, servers after
97 * 6 minutes
98 * http://www.connectathon.org/talks96/nfstcp.pdf
99 */
100static int svc_conn_age_period = 6*60;
101
Peter Zijlstraed075362006-12-06 20:35:24 -0800102#ifdef CONFIG_DEBUG_LOCK_ALLOC
103static struct lock_class_key svc_key[2];
104static struct lock_class_key svc_slock_key[2];
105
106static inline void svc_reclassify_socket(struct socket *sock)
107{
108 struct sock *sk = sock->sk;
John Heffner02b3d342007-09-12 10:42:12 +0200109 BUG_ON(sock_owned_by_user(sk));
Peter Zijlstraed075362006-12-06 20:35:24 -0800110 switch (sk->sk_family) {
111 case AF_INET:
112 sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD",
113 &svc_slock_key[0], "sk_lock-AF_INET-NFSD", &svc_key[0]);
114 break;
115
116 case AF_INET6:
117 sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD",
118 &svc_slock_key[1], "sk_lock-AF_INET6-NFSD", &svc_key[1]);
119 break;
120
121 default:
122 BUG();
123 }
124}
125#else
126static inline void svc_reclassify_socket(struct socket *sock)
127{
128}
129#endif
130
Chuck Leverad06e4b2007-02-12 00:53:32 -0800131static char *__svc_print_addr(struct sockaddr *addr, char *buf, size_t len)
132{
133 switch (addr->sa_family) {
134 case AF_INET:
135 snprintf(buf, len, "%u.%u.%u.%u, port=%u",
136 NIPQUAD(((struct sockaddr_in *) addr)->sin_addr),
Al Viro582ee432007-07-26 17:33:39 +0100137 ntohs(((struct sockaddr_in *) addr)->sin_port));
Chuck Leverad06e4b2007-02-12 00:53:32 -0800138 break;
NeilBrown5a05ed72007-03-06 01:42:22 -0800139
Chuck Leverad06e4b2007-02-12 00:53:32 -0800140 case AF_INET6:
141 snprintf(buf, len, "%x:%x:%x:%x:%x:%x:%x:%x, port=%u",
142 NIP6(((struct sockaddr_in6 *) addr)->sin6_addr),
Al Viro582ee432007-07-26 17:33:39 +0100143 ntohs(((struct sockaddr_in6 *) addr)->sin6_port));
Chuck Leverad06e4b2007-02-12 00:53:32 -0800144 break;
NeilBrown5a05ed72007-03-06 01:42:22 -0800145
Chuck Leverad06e4b2007-02-12 00:53:32 -0800146 default:
147 snprintf(buf, len, "unknown address type: %d", addr->sa_family);
148 break;
149 }
150 return buf;
151}
152
153/**
154 * svc_print_addr - Format rq_addr field for printing
155 * @rqstp: svc_rqst struct containing address to print
156 * @buf: target buffer for formatted address
157 * @len: length of target buffer
158 *
159 */
160char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
161{
Chuck Lever27459f02007-02-12 00:53:34 -0800162 return __svc_print_addr(svc_addr(rqstp), buf, len);
Chuck Leverad06e4b2007-02-12 00:53:32 -0800163}
164EXPORT_SYMBOL_GPL(svc_print_addr);
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/*
Greg Banks3262c812006-10-02 02:17:58 -0700167 * Queue up an idle server thread. Must have pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * Note: this is really a stack rather than a queue, so that we only
Greg Banks3262c812006-10-02 02:17:58 -0700169 * use as many different threads as we need, and the rest don't pollute
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * the cache.
171 */
172static inline void
Greg Banks3262c812006-10-02 02:17:58 -0700173svc_thread_enqueue(struct svc_pool *pool, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Greg Banks3262c812006-10-02 02:17:58 -0700175 list_add(&rqstp->rq_list, &pool->sp_threads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178/*
Greg Banks3262c812006-10-02 02:17:58 -0700179 * Dequeue an nfsd thread. Must have pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 */
181static inline void
Greg Banks3262c812006-10-02 02:17:58 -0700182svc_thread_dequeue(struct svc_pool *pool, struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 list_del(&rqstp->rq_list);
185}
186
187/*
188 * Release an skbuff after use
189 */
Tom Tucker5148bf42007-12-30 21:07:25 -0600190static void svc_release_skb(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Tom Tucker5148bf42007-12-30 21:07:25 -0600192 struct sk_buff *skb = rqstp->rq_xprt_ctxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct svc_deferred_req *dr = rqstp->rq_deferred;
194
195 if (skb) {
Tom Tucker5148bf42007-12-30 21:07:25 -0600196 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 dprintk("svc: service %p, releasing skb %p\n", rqstp, skb);
199 skb_free_datagram(rqstp->rq_sock->sk_sk, skb);
200 }
201 if (dr) {
202 rqstp->rq_deferred = NULL;
203 kfree(dr);
204 }
205}
206
207/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 * Queue up a socket with data pending. If there are idle nfsd
209 * processes, wake 'em up.
210 *
211 */
212static void
213svc_sock_enqueue(struct svc_sock *svsk)
214{
215 struct svc_serv *serv = svsk->sk_server;
Greg Banksbfd24162006-10-02 02:18:01 -0700216 struct svc_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 struct svc_rqst *rqstp;
Greg Banksbfd24162006-10-02 02:18:01 -0700218 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 if (!(svsk->sk_flags &
221 ( (1<<SK_CONN)|(1<<SK_DATA)|(1<<SK_CLOSE)|(1<<SK_DEFERRED)) ))
222 return;
223 if (test_bit(SK_DEAD, &svsk->sk_flags))
224 return;
225
Greg Banksbfd24162006-10-02 02:18:01 -0700226 cpu = get_cpu();
227 pool = svc_pool_for_cpu(svsk->sk_server, cpu);
228 put_cpu();
229
Greg Banks3262c812006-10-02 02:17:58 -0700230 spin_lock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Greg Banks3262c812006-10-02 02:17:58 -0700232 if (!list_empty(&pool->sp_threads) &&
233 !list_empty(&pool->sp_sockets))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 printk(KERN_ERR
235 "svc_sock_enqueue: threads and sockets both waiting??\n");
236
237 if (test_bit(SK_DEAD, &svsk->sk_flags)) {
238 /* Don't enqueue dead sockets */
239 dprintk("svc: socket %p is dead, not enqueued\n", svsk->sk_sk);
240 goto out_unlock;
241 }
242
Greg Banksc081a0c2006-10-02 02:17:57 -0700243 /* Mark socket as busy. It will remain in this state until the
244 * server has processed all pending data and put the socket back
245 * on the idle list. We update SK_BUSY atomically because
246 * it also guards against trying to enqueue the svc_sock twice.
247 */
248 if (test_and_set_bit(SK_BUSY, &svsk->sk_flags)) {
249 /* Don't enqueue socket while already enqueued */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 dprintk("svc: socket %p busy, not enqueued\n", svsk->sk_sk);
251 goto out_unlock;
252 }
Greg Banks3262c812006-10-02 02:17:58 -0700253 BUG_ON(svsk->sk_pool != NULL);
254 svsk->sk_pool = pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Tom Tucker323bee32007-12-30 21:07:31 -0600256 /* Handle pending connection */
257 if (test_bit(SK_CONN, &svsk->sk_flags))
258 goto process;
259
260 /* Handle close in-progress */
261 if (test_bit(SK_CLOSE, &svsk->sk_flags))
262 goto process;
263
264 /* Check if we have space to reply to a request */
265 if (!svsk->sk_xprt.xpt_ops->xpo_has_wspace(&svsk->sk_xprt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 /* Don't enqueue while not enough space for reply */
Tom Tucker323bee32007-12-30 21:07:31 -0600267 dprintk("svc: no write space, socket %p not enqueued\n", svsk);
Greg Banks3262c812006-10-02 02:17:58 -0700268 svsk->sk_pool = NULL;
Greg Banksc081a0c2006-10-02 02:17:57 -0700269 clear_bit(SK_BUSY, &svsk->sk_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 goto out_unlock;
271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Tom Tucker323bee32007-12-30 21:07:31 -0600273 process:
Greg Banks3262c812006-10-02 02:17:58 -0700274 if (!list_empty(&pool->sp_threads)) {
275 rqstp = list_entry(pool->sp_threads.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 struct svc_rqst,
277 rq_list);
278 dprintk("svc: socket %p served by daemon %p\n",
279 svsk->sk_sk, rqstp);
Greg Banks3262c812006-10-02 02:17:58 -0700280 svc_thread_dequeue(pool, rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (rqstp->rq_sock)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800282 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 "svc_sock_enqueue: server %p, rq_sock=%p!\n",
284 rqstp, rqstp->rq_sock);
285 rqstp->rq_sock = svsk;
Greg Banksc45c3572006-10-02 02:17:54 -0700286 atomic_inc(&svsk->sk_inuse);
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700287 rqstp->rq_reserved = serv->sv_max_mesg;
Greg Banks5685f0f2006-10-02 02:17:56 -0700288 atomic_add(rqstp->rq_reserved, &svsk->sk_reserved);
Greg Banks3262c812006-10-02 02:17:58 -0700289 BUG_ON(svsk->sk_pool != pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 wake_up(&rqstp->rq_wait);
291 } else {
292 dprintk("svc: socket %p put into queue\n", svsk->sk_sk);
Greg Banks3262c812006-10-02 02:17:58 -0700293 list_add_tail(&svsk->sk_ready, &pool->sp_sockets);
294 BUG_ON(svsk->sk_pool != pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296
297out_unlock:
Greg Banks3262c812006-10-02 02:17:58 -0700298 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301/*
Greg Banks3262c812006-10-02 02:17:58 -0700302 * Dequeue the first socket. Must be called with the pool->sp_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 */
304static inline struct svc_sock *
Greg Banks3262c812006-10-02 02:17:58 -0700305svc_sock_dequeue(struct svc_pool *pool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 struct svc_sock *svsk;
308
Greg Banks3262c812006-10-02 02:17:58 -0700309 if (list_empty(&pool->sp_sockets))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return NULL;
311
Greg Banks3262c812006-10-02 02:17:58 -0700312 svsk = list_entry(pool->sp_sockets.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 struct svc_sock, sk_ready);
314 list_del_init(&svsk->sk_ready);
315
316 dprintk("svc: socket %p dequeued, inuse=%d\n",
Greg Banksc45c3572006-10-02 02:17:54 -0700317 svsk->sk_sk, atomic_read(&svsk->sk_inuse));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 return svsk;
320}
321
322/*
323 * Having read something from a socket, check whether it
324 * needs to be re-enqueued.
325 * Note: SK_DATA only gets cleared when a read-attempt finds
326 * no (or insufficient) data.
327 */
328static inline void
329svc_sock_received(struct svc_sock *svsk)
330{
Greg Banks3262c812006-10-02 02:17:58 -0700331 svsk->sk_pool = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 clear_bit(SK_BUSY, &svsk->sk_flags);
333 svc_sock_enqueue(svsk);
334}
335
336
337/**
338 * svc_reserve - change the space reserved for the reply to a request.
339 * @rqstp: The request in question
340 * @space: new max space to reserve
341 *
342 * Each request reserves some space on the output queue of the socket
343 * to make sure the reply fits. This function reduces that reserved
344 * space to be the amount of space used already, plus @space.
345 *
346 */
347void svc_reserve(struct svc_rqst *rqstp, int space)
348{
349 space += rqstp->rq_res.head[0].iov_len;
350
351 if (space < rqstp->rq_reserved) {
352 struct svc_sock *svsk = rqstp->rq_sock;
Greg Banks5685f0f2006-10-02 02:17:56 -0700353 atomic_sub((rqstp->rq_reserved - space), &svsk->sk_reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 rqstp->rq_reserved = space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 svc_sock_enqueue(svsk);
357 }
358}
359
360/*
361 * Release a socket after use.
362 */
363static inline void
364svc_sock_put(struct svc_sock *svsk)
365{
NeilBrownaaf68cf2007-02-08 14:20:30 -0800366 if (atomic_dec_and_test(&svsk->sk_inuse)) {
Tom Tucker755ccea2007-12-30 21:07:27 -0600367 BUG_ON(!test_bit(SK_DEAD, &svsk->sk_flags));
368 svsk->sk_xprt.xpt_ops->xpo_free(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372static void
373svc_sock_release(struct svc_rqst *rqstp)
374{
375 struct svc_sock *svsk = rqstp->rq_sock;
376
Tom Tucker5148bf42007-12-30 21:07:25 -0600377 rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
NeilBrown44524352006-10-04 02:15:46 -0700379 svc_free_res_pages(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 rqstp->rq_res.page_len = 0;
381 rqstp->rq_res.page_base = 0;
382
383
384 /* Reset response buffer and release
385 * the reservation.
386 * But first, check that enough space was reserved
387 * for the reply, otherwise we have a bug!
388 */
389 if ((rqstp->rq_res.len) > rqstp->rq_reserved)
390 printk(KERN_ERR "RPC request reserved %d but used %d\n",
391 rqstp->rq_reserved,
392 rqstp->rq_res.len);
393
394 rqstp->rq_res.head[0].iov_len = 0;
395 svc_reserve(rqstp, 0);
396 rqstp->rq_sock = NULL;
397
398 svc_sock_put(svsk);
399}
400
401/*
402 * External function to wake up a server waiting for data
Greg Banks3262c812006-10-02 02:17:58 -0700403 * This really only makes sense for services like lockd
404 * which have exactly one thread anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 */
406void
407svc_wake_up(struct svc_serv *serv)
408{
409 struct svc_rqst *rqstp;
Greg Banks3262c812006-10-02 02:17:58 -0700410 unsigned int i;
411 struct svc_pool *pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Greg Banks3262c812006-10-02 02:17:58 -0700413 for (i = 0; i < serv->sv_nrpools; i++) {
414 pool = &serv->sv_pools[i];
415
416 spin_lock_bh(&pool->sp_lock);
417 if (!list_empty(&pool->sp_threads)) {
418 rqstp = list_entry(pool->sp_threads.next,
419 struct svc_rqst,
420 rq_list);
421 dprintk("svc: daemon %p woken up.\n", rqstp);
422 /*
423 svc_thread_dequeue(pool, rqstp);
424 rqstp->rq_sock = NULL;
425 */
426 wake_up(&rqstp->rq_wait);
427 }
428 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Chuck Leverb92503b2007-02-12 00:53:36 -0800432union svc_pktinfo_u {
433 struct in_pktinfo pkti;
Chuck Leverb92503b2007-02-12 00:53:36 -0800434 struct in6_pktinfo pkti6;
Chuck Leverb92503b2007-02-12 00:53:36 -0800435};
David S. Millerbc375ea2007-04-12 13:35:59 -0700436#define SVC_PKTINFO_SPACE \
437 CMSG_SPACE(sizeof(union svc_pktinfo_u))
Chuck Leverb92503b2007-02-12 00:53:36 -0800438
439static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh)
440{
441 switch (rqstp->rq_sock->sk_sk->sk_family) {
442 case AF_INET: {
443 struct in_pktinfo *pki = CMSG_DATA(cmh);
444
445 cmh->cmsg_level = SOL_IP;
446 cmh->cmsg_type = IP_PKTINFO;
447 pki->ipi_ifindex = 0;
448 pki->ipi_spec_dst.s_addr = rqstp->rq_daddr.addr.s_addr;
449 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
450 }
451 break;
NeilBrown5a05ed72007-03-06 01:42:22 -0800452
Chuck Leverb92503b2007-02-12 00:53:36 -0800453 case AF_INET6: {
454 struct in6_pktinfo *pki = CMSG_DATA(cmh);
455
456 cmh->cmsg_level = SOL_IPV6;
457 cmh->cmsg_type = IPV6_PKTINFO;
458 pki->ipi6_ifindex = 0;
459 ipv6_addr_copy(&pki->ipi6_addr,
460 &rqstp->rq_daddr.addr6);
461 cmh->cmsg_len = CMSG_LEN(sizeof(*pki));
462 }
463 break;
Chuck Leverb92503b2007-02-12 00:53:36 -0800464 }
465 return;
466}
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/*
469 * Generic sendto routine
470 */
471static int
472svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
473{
474 struct svc_sock *svsk = rqstp->rq_sock;
475 struct socket *sock = svsk->sk_sock;
476 int slen;
David S. Millerbc375ea2007-04-12 13:35:59 -0700477 union {
478 struct cmsghdr hdr;
479 long all[SVC_PKTINFO_SPACE / sizeof(long)];
480 } buffer;
481 struct cmsghdr *cmh = &buffer.hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 int len = 0;
483 int result;
484 int size;
485 struct page **ppage = xdr->pages;
486 size_t base = xdr->page_base;
487 unsigned int pglen = xdr->page_len;
488 unsigned int flags = MSG_MORE;
Chuck Leverad06e4b2007-02-12 00:53:32 -0800489 char buf[RPC_MAX_ADDRBUFLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 slen = xdr->len;
492
493 if (rqstp->rq_prot == IPPROTO_UDP) {
Chuck Leverb92503b2007-02-12 00:53:36 -0800494 struct msghdr msg = {
495 .msg_name = &rqstp->rq_addr,
496 .msg_namelen = rqstp->rq_addrlen,
497 .msg_control = cmh,
498 .msg_controllen = sizeof(buffer),
499 .msg_flags = MSG_MORE,
500 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Chuck Leverb92503b2007-02-12 00:53:36 -0800502 svc_set_cmsg_data(rqstp, cmh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 if (sock_sendmsg(sock, &msg, 0) < 0)
505 goto out;
506 }
507
508 /* send head */
509 if (slen == xdr->head[0].iov_len)
510 flags = 0;
NeilBrown44524352006-10-04 02:15:46 -0700511 len = kernel_sendpage(sock, rqstp->rq_respages[0], 0,
512 xdr->head[0].iov_len, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (len != xdr->head[0].iov_len)
514 goto out;
515 slen -= xdr->head[0].iov_len;
516 if (slen == 0)
517 goto out;
518
519 /* send page data */
520 size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen;
521 while (pglen > 0) {
522 if (slen == size)
523 flags = 0;
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700524 result = kernel_sendpage(sock, *ppage, base, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (result > 0)
526 len += result;
527 if (result != size)
528 goto out;
529 slen -= size;
530 pglen -= size;
531 size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen;
532 base = 0;
533 ppage++;
534 }
535 /* send tail */
536 if (xdr->tail[0].iov_len) {
NeilBrown44524352006-10-04 02:15:46 -0700537 result = kernel_sendpage(sock, rqstp->rq_respages[0],
538 ((unsigned long)xdr->tail[0].iov_base)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800539 & (PAGE_SIZE-1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 xdr->tail[0].iov_len, 0);
541
542 if (result > 0)
543 len += result;
544 }
545out:
Chuck Leverad06e4b2007-02-12 00:53:32 -0800546 dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n",
547 rqstp->rq_sock, xdr->head[0].iov_base, xdr->head[0].iov_len,
548 xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 return len;
551}
552
553/*
NeilBrown80212d52006-10-02 02:17:47 -0700554 * Report socket names for nfsdfs
555 */
556static int one_sock_name(char *buf, struct svc_sock *svsk)
557{
558 int len;
559
560 switch(svsk->sk_sk->sk_family) {
561 case AF_INET:
562 len = sprintf(buf, "ipv4 %s %u.%u.%u.%u %d\n",
563 svsk->sk_sk->sk_protocol==IPPROTO_UDP?
564 "udp" : "tcp",
565 NIPQUAD(inet_sk(svsk->sk_sk)->rcv_saddr),
566 inet_sk(svsk->sk_sk)->num);
567 break;
568 default:
569 len = sprintf(buf, "*unknown-%d*\n",
570 svsk->sk_sk->sk_family);
571 }
572 return len;
573}
574
575int
NeilBrownb41b66d2006-10-02 02:17:48 -0700576svc_sock_names(char *buf, struct svc_serv *serv, char *toclose)
NeilBrown80212d52006-10-02 02:17:47 -0700577{
NeilBrownb41b66d2006-10-02 02:17:48 -0700578 struct svc_sock *svsk, *closesk = NULL;
NeilBrown80212d52006-10-02 02:17:47 -0700579 int len = 0;
580
581 if (!serv)
582 return 0;
NeilBrownaaf68cf2007-02-08 14:20:30 -0800583 spin_lock_bh(&serv->sv_lock);
NeilBrown80212d52006-10-02 02:17:47 -0700584 list_for_each_entry(svsk, &serv->sv_permsocks, sk_list) {
585 int onelen = one_sock_name(buf+len, svsk);
NeilBrownb41b66d2006-10-02 02:17:48 -0700586 if (toclose && strcmp(toclose, buf+len) == 0)
587 closesk = svsk;
588 else
589 len += onelen;
NeilBrown80212d52006-10-02 02:17:47 -0700590 }
NeilBrownaaf68cf2007-02-08 14:20:30 -0800591 spin_unlock_bh(&serv->sv_lock);
NeilBrownb41b66d2006-10-02 02:17:48 -0700592 if (closesk)
NeilBrown5680c442006-10-04 02:15:45 -0700593 /* Should unregister with portmap, but you cannot
594 * unregister just one protocol...
595 */
NeilBrownaaf68cf2007-02-08 14:20:30 -0800596 svc_close_socket(closesk);
NeilBrown37a03472006-10-04 02:15:44 -0700597 else if (toclose)
598 return -ENOENT;
NeilBrown80212d52006-10-02 02:17:47 -0700599 return len;
600}
601EXPORT_SYMBOL(svc_sock_names);
602
603/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 * Check input queue length
605 */
606static int
607svc_recv_available(struct svc_sock *svsk)
608{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 struct socket *sock = svsk->sk_sock;
610 int avail, err;
611
Sridhar Samudralae6242e92006-08-07 20:58:01 -0700612 err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 return (err >= 0)? avail : err;
615}
616
617/*
618 * Generic recvfrom routine.
619 */
620static int
621svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr, int buflen)
622{
Chuck Lever067d7812007-02-12 00:53:30 -0800623 struct svc_sock *svsk = rqstp->rq_sock;
Chuck Lever1ba95102007-02-12 00:53:31 -0800624 struct msghdr msg = {
625 .msg_flags = MSG_DONTWAIT,
626 };
Frank van Maarseveena9747692007-07-09 22:21:39 +0200627 struct sockaddr *sin;
Chuck Lever1ba95102007-02-12 00:53:31 -0800628 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Chuck Lever1ba95102007-02-12 00:53:31 -0800630 len = kernel_recvmsg(svsk->sk_sock, &msg, iov, nr, buflen,
631 msg.msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 /* sock_recvmsg doesn't fill in the name/namelen, so we must..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 */
Chuck Lever067d7812007-02-12 00:53:30 -0800635 memcpy(&rqstp->rq_addr, &svsk->sk_remote, svsk->sk_remotelen);
636 rqstp->rq_addrlen = svsk->sk_remotelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Frank van Maarseveena9747692007-07-09 22:21:39 +0200638 /* Destination address in request is needed for binding the
639 * source address in RPC callbacks later.
640 */
641 sin = (struct sockaddr *)&svsk->sk_local;
642 switch (sin->sa_family) {
643 case AF_INET:
644 rqstp->rq_daddr.addr = ((struct sockaddr_in *)sin)->sin_addr;
645 break;
646 case AF_INET6:
647 rqstp->rq_daddr.addr6 = ((struct sockaddr_in6 *)sin)->sin6_addr;
648 break;
649 }
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n",
Chuck Lever1ba95102007-02-12 00:53:31 -0800652 svsk, iov[0].iov_base, iov[0].iov_len, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 return len;
655}
656
657/*
658 * Set socket snd and rcv buffer lengths
659 */
660static inline void
661svc_sock_setbufsize(struct socket *sock, unsigned int snd, unsigned int rcv)
662{
663#if 0
664 mm_segment_t oldfs;
665 oldfs = get_fs(); set_fs(KERNEL_DS);
666 sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
667 (char*)&snd, sizeof(snd));
668 sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
669 (char*)&rcv, sizeof(rcv));
670#else
671 /* sock_setsockopt limits use to sysctl_?mem_max,
672 * which isn't acceptable. Until that is made conditional
673 * on not having CAP_SYS_RESOURCE or similar, we go direct...
674 * DaveM said I could!
675 */
676 lock_sock(sock->sk);
677 sock->sk->sk_sndbuf = snd * 2;
678 sock->sk->sk_rcvbuf = rcv * 2;
679 sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK;
680 release_sock(sock->sk);
681#endif
682}
683/*
684 * INET callback when data has been received on the socket.
685 */
686static void
687svc_udp_data_ready(struct sock *sk, int count)
688{
Neil Brown939bb7e2005-09-13 01:25:39 -0700689 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Neil Brown939bb7e2005-09-13 01:25:39 -0700691 if (svsk) {
692 dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n",
693 svsk, sk, count, test_bit(SK_BUSY, &svsk->sk_flags));
694 set_bit(SK_DATA, &svsk->sk_flags);
695 svc_sock_enqueue(svsk);
696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
698 wake_up_interruptible(sk->sk_sleep);
699}
700
701/*
702 * INET callback when space is newly available on the socket.
703 */
704static void
705svc_write_space(struct sock *sk)
706{
707 struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data);
708
709 if (svsk) {
710 dprintk("svc: socket %p(inet %p), write_space busy=%d\n",
711 svsk, sk, test_bit(SK_BUSY, &svsk->sk_flags));
712 svc_sock_enqueue(svsk);
713 }
714
715 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) {
Neil Brown939bb7e2005-09-13 01:25:39 -0700716 dprintk("RPC svc_write_space: someone sleeping on %p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 svsk);
718 wake_up_interruptible(sk->sk_sleep);
719 }
720}
721
NeilBrown7a37f572007-03-06 01:42:21 -0800722static inline void svc_udp_get_dest_address(struct svc_rqst *rqstp,
723 struct cmsghdr *cmh)
Chuck Lever95756482007-02-12 00:53:38 -0800724{
725 switch (rqstp->rq_sock->sk_sk->sk_family) {
726 case AF_INET: {
NeilBrown7a37f572007-03-06 01:42:21 -0800727 struct in_pktinfo *pki = CMSG_DATA(cmh);
728 rqstp->rq_daddr.addr.s_addr = pki->ipi_spec_dst.s_addr;
Chuck Lever95756482007-02-12 00:53:38 -0800729 break;
NeilBrown7a37f572007-03-06 01:42:21 -0800730 }
Chuck Lever95756482007-02-12 00:53:38 -0800731 case AF_INET6: {
NeilBrown7a37f572007-03-06 01:42:21 -0800732 struct in6_pktinfo *pki = CMSG_DATA(cmh);
733 ipv6_addr_copy(&rqstp->rq_daddr.addr6, &pki->ipi6_addr);
Chuck Lever95756482007-02-12 00:53:38 -0800734 break;
NeilBrown7a37f572007-03-06 01:42:21 -0800735 }
Chuck Lever95756482007-02-12 00:53:38 -0800736 }
Chuck Lever95756482007-02-12 00:53:38 -0800737}
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739/*
740 * Receive a datagram from a UDP socket.
741 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742static int
743svc_udp_recvfrom(struct svc_rqst *rqstp)
744{
745 struct svc_sock *svsk = rqstp->rq_sock;
746 struct svc_serv *serv = svsk->sk_server;
747 struct sk_buff *skb;
David S. Millerbc375ea2007-04-12 13:35:59 -0700748 union {
749 struct cmsghdr hdr;
750 long all[SVC_PKTINFO_SPACE / sizeof(long)];
751 } buffer;
752 struct cmsghdr *cmh = &buffer.hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 int err, len;
NeilBrown7a37f572007-03-06 01:42:21 -0800754 struct msghdr msg = {
755 .msg_name = svc_addr(rqstp),
756 .msg_control = cmh,
757 .msg_controllen = sizeof(buffer),
758 .msg_flags = MSG_DONTWAIT,
759 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
762 /* udp sockets need large rcvbuf as all pending
763 * requests are still in that buffer. sndbuf must
764 * also be large enough that there is enough space
Greg Banks3262c812006-10-02 02:17:58 -0700765 * for one reply per thread. We count all threads
766 * rather than threads in a particular pool, which
767 * provides an upper bound on the number of threads
768 * which will access the socket.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 */
770 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700771 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
772 (serv->sv_nrthreads+3) * serv->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
775 svc_sock_received(svsk);
776 return svc_deferred_recv(rqstp);
777 }
778
779 clear_bit(SK_DATA, &svsk->sk_flags);
NeilBrown05ed6902007-05-09 02:34:55 -0700780 skb = NULL;
781 err = kernel_recvmsg(svsk->sk_sock, &msg, NULL,
782 0, 0, MSG_PEEK | MSG_DONTWAIT);
783 if (err >= 0)
784 skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err);
785
786 if (skb == NULL) {
787 if (err != -EAGAIN) {
788 /* possibly an icmp error */
789 dprintk("svc: recvfrom returned error %d\n", -err);
790 set_bit(SK_DATA, &svsk->sk_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
NeilBrown05ed6902007-05-09 02:34:55 -0700792 svc_sock_received(svsk);
793 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
NeilBrown7a37f572007-03-06 01:42:21 -0800795 rqstp->rq_addrlen = sizeof(rqstp->rq_addr);
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700796 if (skb->tstamp.tv64 == 0) {
797 skb->tstamp = ktime_get_real();
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800798 /* Don't enable netstamp, sunrpc doesn't
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 need that much accuracy */
800 }
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700801 svsk->sk_sk->sk_stamp = skb->tstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 set_bit(SK_DATA, &svsk->sk_flags); /* there may be more data... */
803
804 /*
805 * Maybe more packets - kick another thread ASAP.
806 */
807 svc_sock_received(svsk);
808
809 len = skb->len - sizeof(struct udphdr);
810 rqstp->rq_arg.len = len;
811
Chuck Lever95756482007-02-12 00:53:38 -0800812 rqstp->rq_prot = IPPROTO_UDP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
NeilBrown7a37f572007-03-06 01:42:21 -0800814 if (cmh->cmsg_level != IPPROTO_IP ||
815 cmh->cmsg_type != IP_PKTINFO) {
816 if (net_ratelimit())
817 printk("rpcsvc: received unknown control message:"
818 "%d/%d\n",
819 cmh->cmsg_level, cmh->cmsg_type);
820 skb_free_datagram(svsk->sk_sk, skb);
821 return 0;
822 }
823 svc_udp_get_dest_address(rqstp, cmh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 if (skb_is_nonlinear(skb)) {
826 /* we have to copy */
827 local_bh_disable();
828 if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) {
829 local_bh_enable();
830 /* checksum error */
831 skb_free_datagram(svsk->sk_sk, skb);
832 return 0;
833 }
834 local_bh_enable();
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800835 skb_free_datagram(svsk->sk_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 } else {
837 /* we can use it in-place */
838 rqstp->rq_arg.head[0].iov_base = skb->data + sizeof(struct udphdr);
839 rqstp->rq_arg.head[0].iov_len = len;
Herbert Xufb286bb2005-11-10 13:01:24 -0800840 if (skb_checksum_complete(skb)) {
841 skb_free_datagram(svsk->sk_sk, skb);
842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
Tom Tucker5148bf42007-12-30 21:07:25 -0600844 rqstp->rq_xprt_ctxt = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846
847 rqstp->rq_arg.page_base = 0;
848 if (len <= rqstp->rq_arg.head[0].iov_len) {
849 rqstp->rq_arg.head[0].iov_len = len;
850 rqstp->rq_arg.page_len = 0;
NeilBrown44524352006-10-04 02:15:46 -0700851 rqstp->rq_respages = rqstp->rq_pages+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 } else {
853 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
NeilBrown44524352006-10-04 02:15:46 -0700854 rqstp->rq_respages = rqstp->rq_pages + 1 +
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700855 DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857
858 if (serv->sv_stats)
859 serv->sv_stats->netudpcnt++;
860
861 return len;
862}
863
864static int
865svc_udp_sendto(struct svc_rqst *rqstp)
866{
867 int error;
868
869 error = svc_sendto(rqstp, &rqstp->rq_res);
870 if (error == -ECONNREFUSED)
871 /* ICMP error on earlier request. */
872 error = svc_sendto(rqstp, &rqstp->rq_res);
873
874 return error;
875}
876
Tom Tuckere831fe62007-12-30 21:07:29 -0600877static void svc_udp_prep_reply_hdr(struct svc_rqst *rqstp)
878{
879}
880
Tom Tucker323bee32007-12-30 21:07:31 -0600881static int svc_udp_has_wspace(struct svc_xprt *xprt)
882{
883 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
884 struct svc_serv *serv = svsk->sk_server;
885 unsigned long required;
886
887 /*
888 * Set the SOCK_NOSPACE flag before checking the available
889 * sock space.
890 */
891 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
892 required = atomic_read(&svsk->sk_reserved) + serv->sv_max_mesg;
893 if (required*2 > sock_wspace(svsk->sk_sk))
894 return 0;
895 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
896 return 1;
897}
898
Tom Tucker38a417c2007-12-30 21:07:36 -0600899static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt)
900{
901 BUG();
902 return NULL;
903}
904
Tom Tucker360d8732007-12-30 21:07:17 -0600905static struct svc_xprt_ops svc_udp_ops = {
Tom Tucker5d137992007-12-30 21:07:23 -0600906 .xpo_recvfrom = svc_udp_recvfrom,
907 .xpo_sendto = svc_udp_sendto,
Tom Tucker5148bf42007-12-30 21:07:25 -0600908 .xpo_release_rqst = svc_release_skb,
Tom Tucker755ccea2007-12-30 21:07:27 -0600909 .xpo_detach = svc_sock_detach,
910 .xpo_free = svc_sock_free,
Tom Tuckere831fe62007-12-30 21:07:29 -0600911 .xpo_prep_reply_hdr = svc_udp_prep_reply_hdr,
Tom Tucker323bee32007-12-30 21:07:31 -0600912 .xpo_has_wspace = svc_udp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -0600913 .xpo_accept = svc_udp_accept,
Tom Tucker360d8732007-12-30 21:07:17 -0600914};
915
916static struct svc_xprt_class svc_udp_class = {
917 .xcl_name = "udp",
918 .xcl_ops = &svc_udp_ops,
Tom Tucker49023152007-12-30 21:07:21 -0600919 .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP,
Tom Tucker360d8732007-12-30 21:07:17 -0600920};
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922static void
923svc_udp_init(struct svc_sock *svsk)
924{
NeilBrown7a37f572007-03-06 01:42:21 -0800925 int one = 1;
926 mm_segment_t oldfs;
927
Tom Tucker360d8732007-12-30 21:07:17 -0600928 svc_xprt_init(&svc_udp_class, &svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 svsk->sk_sk->sk_data_ready = svc_udp_data_ready;
930 svsk->sk_sk->sk_write_space = svc_write_space;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800933 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 * svc_udp_recvfrom will re-adjust if necessary
935 */
936 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -0700937 3 * svsk->sk_server->sv_max_mesg,
938 3 * svsk->sk_server->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 set_bit(SK_DATA, &svsk->sk_flags); /* might have come in before data_ready set up */
941 set_bit(SK_CHNGBUF, &svsk->sk_flags);
NeilBrown7a37f572007-03-06 01:42:21 -0800942
943 oldfs = get_fs();
944 set_fs(KERNEL_DS);
945 /* make sure we get destination address info */
946 svsk->sk_sock->ops->setsockopt(svsk->sk_sock, IPPROTO_IP, IP_PKTINFO,
947 (char __user *)&one, sizeof(one));
948 set_fs(oldfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
951/*
952 * A data_ready event on a listening socket means there's a connection
953 * pending. Do not use state_change as a substitute for it.
954 */
955static void
956svc_tcp_listen_data_ready(struct sock *sk, int count_unused)
957{
Neil Brown939bb7e2005-09-13 01:25:39 -0700958 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 dprintk("svc: socket %p TCP (listen) state change %d\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700961 sk, sk->sk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Neil Brown939bb7e2005-09-13 01:25:39 -0700963 /*
964 * This callback may called twice when a new connection
965 * is established as a child socket inherits everything
966 * from a parent LISTEN socket.
967 * 1) data_ready method of the parent socket will be called
968 * when one of child sockets become ESTABLISHED.
969 * 2) data_ready method of the child socket may be called
970 * when it receives data before the socket is accepted.
971 * In case of 2, we should ignore it silently.
972 */
973 if (sk->sk_state == TCP_LISTEN) {
974 if (svsk) {
975 set_bit(SK_CONN, &svsk->sk_flags);
976 svc_sock_enqueue(svsk);
977 } else
978 printk("svc: socket %p: no user data\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
Neil Brown939bb7e2005-09-13 01:25:39 -0700980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
982 wake_up_interruptible_all(sk->sk_sleep);
983}
984
985/*
986 * A state change on a connected socket means it's dying or dead.
987 */
988static void
989svc_tcp_state_change(struct sock *sk)
990{
Neil Brown939bb7e2005-09-13 01:25:39 -0700991 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -0700994 sk, sk->sk_state, sk->sk_user_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Neil Brown939bb7e2005-09-13 01:25:39 -0700996 if (!svsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 printk("svc: socket %p: no user data\n", sk);
Neil Brown939bb7e2005-09-13 01:25:39 -0700998 else {
999 set_bit(SK_CLOSE, &svsk->sk_flags);
1000 svc_sock_enqueue(svsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
1003 wake_up_interruptible_all(sk->sk_sleep);
1004}
1005
1006static void
1007svc_tcp_data_ready(struct sock *sk, int count)
1008{
Neil Brown939bb7e2005-09-13 01:25:39 -07001009 struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 dprintk("svc: socket %p TCP data ready (svsk %p)\n",
Neil Brown939bb7e2005-09-13 01:25:39 -07001012 sk, sk->sk_user_data);
1013 if (svsk) {
1014 set_bit(SK_DATA, &svsk->sk_flags);
1015 svc_sock_enqueue(svsk);
1016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
1018 wake_up_interruptible(sk->sk_sleep);
1019}
1020
Chuck Leverbcdb81a2007-02-12 00:53:37 -08001021static inline int svc_port_is_privileged(struct sockaddr *sin)
1022{
1023 switch (sin->sa_family) {
1024 case AF_INET:
1025 return ntohs(((struct sockaddr_in *)sin)->sin_port)
1026 < PROT_SOCK;
Chuck Leverbcdb81a2007-02-12 00:53:37 -08001027 case AF_INET6:
1028 return ntohs(((struct sockaddr_in6 *)sin)->sin6_port)
1029 < PROT_SOCK;
Chuck Leverbcdb81a2007-02-12 00:53:37 -08001030 default:
1031 return 0;
1032 }
1033}
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035/*
1036 * Accept a TCP connection
1037 */
Tom Tucker38a417c2007-12-30 21:07:36 -06001038static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Tom Tucker38a417c2007-12-30 21:07:36 -06001040 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001041 struct sockaddr_storage addr;
1042 struct sockaddr *sin = (struct sockaddr *) &addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 struct svc_serv *serv = svsk->sk_server;
1044 struct socket *sock = svsk->sk_sock;
1045 struct socket *newsock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 struct svc_sock *newsvsk;
1047 int err, slen;
Chuck Leverad06e4b2007-02-12 00:53:32 -08001048 char buf[RPC_MAX_ADDRBUFLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 dprintk("svc: tcp_accept %p sock %p\n", svsk, sock);
1051 if (!sock)
Tom Tucker38a417c2007-12-30 21:07:36 -06001052 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001054 clear_bit(SK_CONN, &svsk->sk_flags);
1055 err = kernel_accept(sock, &newsock, O_NONBLOCK);
1056 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (err == -ENOMEM)
1058 printk(KERN_WARNING "%s: no more sockets!\n",
1059 serv->sv_name);
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001060 else if (err != -EAGAIN && net_ratelimit())
1061 printk(KERN_WARNING "%s: accept failed (err %d)!\n",
1062 serv->sv_name, -err);
Tom Tucker38a417c2007-12-30 21:07:36 -06001063 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 set_bit(SK_CONN, &svsk->sk_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001068 err = kernel_getpeername(newsock, sin, &slen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (err < 0) {
1070 if (net_ratelimit())
1071 printk(KERN_WARNING "%s: peername failed (err %d)!\n",
1072 serv->sv_name, -err);
1073 goto failed; /* aborted connection or whatever */
1074 }
1075
1076 /* Ideally, we would want to reject connections from unauthorized
Chuck Leverad06e4b2007-02-12 00:53:32 -08001077 * hosts here, but when we get encryption, the IP of the host won't
1078 * tell us anything. For now just warn about unpriv connections.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 */
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001080 if (!svc_port_is_privileged(sin)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 dprintk(KERN_WARNING
Chuck Leverad06e4b2007-02-12 00:53:32 -08001082 "%s: connect from unprivileged port: %s\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001083 serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001084 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
Chuck Leverad06e4b2007-02-12 00:53:32 -08001086 dprintk("%s: connect from %s\n", serv->sv_name,
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001087 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 /* make sure that a write doesn't block forever when
1090 * low on memory
1091 */
1092 newsock->sk->sk_sndtimeo = HZ*30;
1093
Chuck Lever6b174332007-02-12 00:53:28 -08001094 if (!(newsvsk = svc_setup_socket(serv, newsock, &err,
1095 (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 goto failed;
akpm@linux-foundation.orgcdd88b92007-02-12 00:53:38 -08001097 memcpy(&newsvsk->sk_remote, sin, slen);
Chuck Lever067d7812007-02-12 00:53:30 -08001098 newsvsk->sk_remotelen = slen;
Frank van Maarseveena9747692007-07-09 22:21:39 +02001099 err = kernel_getsockname(newsock, sin, &slen);
1100 if (unlikely(err < 0)) {
1101 dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err);
1102 slen = offsetof(struct sockaddr, sa_data);
1103 }
1104 memcpy(&newsvsk->sk_local, sin, slen);
Chuck Lever067d7812007-02-12 00:53:30 -08001105
NeilBrowne79eff12007-02-12 00:53:30 -08001106 svc_sock_received(newsvsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Tom Tuckerf9f3cc42007-12-30 21:07:40 -06001108 if (serv->sv_stats)
1109 serv->sv_stats->nettcpconn++;
1110
1111 return &newsvsk->sk_xprt;
1112
1113failed:
1114 sock_release(newsock);
1115 return NULL;
1116}
1117
1118/*
1119 * Make sure that we don't have too many active connections. If we
1120 * have, something must be dropped.
1121 *
1122 * There's no point in trying to do random drop here for DoS
1123 * prevention. The NFS clients does 1 reconnect in 15 seconds. An
1124 * attacker can easily beat that.
1125 *
1126 * The only somewhat efficient mechanism would be if drop old
1127 * connections from the same IP first. But right now we don't even
1128 * record the client IP in svc_sock.
1129 */
1130static void svc_check_conn_limits(struct svc_serv *serv)
1131{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) {
1133 struct svc_sock *svsk = NULL;
1134 spin_lock_bh(&serv->sv_lock);
1135 if (!list_empty(&serv->sv_tempsocks)) {
1136 if (net_ratelimit()) {
1137 /* Try to help the admin */
1138 printk(KERN_NOTICE "%s: too many open TCP "
Tom Tuckerf9f3cc42007-12-30 21:07:40 -06001139 "sockets, consider increasing the "
1140 "number of nfsd threads\n",
1141 serv->sv_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143 /*
1144 * Always select the oldest socket. It's not fair,
1145 * but so is life
1146 */
1147 svsk = list_entry(serv->sv_tempsocks.prev,
1148 struct svc_sock,
1149 sk_list);
1150 set_bit(SK_CLOSE, &svsk->sk_flags);
Greg Banksc45c3572006-10-02 02:17:54 -07001151 atomic_inc(&svsk->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
1153 spin_unlock_bh(&serv->sv_lock);
1154
1155 if (svsk) {
1156 svc_sock_enqueue(svsk);
1157 svc_sock_put(svsk);
1158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
1162/*
1163 * Receive data from a TCP socket.
1164 */
1165static int
1166svc_tcp_recvfrom(struct svc_rqst *rqstp)
1167{
1168 struct svc_sock *svsk = rqstp->rq_sock;
1169 struct svc_serv *serv = svsk->sk_server;
1170 int len;
NeilBrown3cc03b12006-10-04 02:15:47 -07001171 struct kvec *vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 int pnum, vlen;
1173
1174 dprintk("svc: tcp_recv %p data %d conn %d close %d\n",
1175 svsk, test_bit(SK_DATA, &svsk->sk_flags),
1176 test_bit(SK_CONN, &svsk->sk_flags),
1177 test_bit(SK_CLOSE, &svsk->sk_flags));
1178
1179 if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) {
1180 svc_sock_received(svsk);
1181 return svc_deferred_recv(rqstp);
1182 }
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (test_and_clear_bit(SK_CHNGBUF, &svsk->sk_flags))
1185 /* sndbuf needs to have room for one request
1186 * per thread, otherwise we can stall even when the
1187 * network isn't a bottleneck.
Greg Banks3262c812006-10-02 02:17:58 -07001188 *
1189 * We count all threads rather than threads in a
1190 * particular pool, which provides an upper bound
1191 * on the number of threads which will access the socket.
1192 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 * rcvbuf just needs to be able to hold a few requests.
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001194 * Normally they will be removed from the queue
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 * as soon a a complete request arrives.
1196 */
1197 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001198 (serv->sv_nrthreads+3) * serv->sv_max_mesg,
1199 3 * serv->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 clear_bit(SK_DATA, &svsk->sk_flags);
1202
1203 /* Receive data. If we haven't got the record length yet, get
1204 * the next four bytes. Otherwise try to gobble up as much as
1205 * possible up to the complete record length.
1206 */
1207 if (svsk->sk_tcplen < 4) {
1208 unsigned long want = 4 - svsk->sk_tcplen;
1209 struct kvec iov;
1210
1211 iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen;
1212 iov.iov_len = want;
1213 if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0)
1214 goto error;
1215 svsk->sk_tcplen += len;
1216
1217 if (len < want) {
1218 dprintk("svc: short recvfrom while reading record length (%d of %lu)\n",
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001219 len, want);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 svc_sock_received(svsk);
1221 return -EAGAIN; /* record header not complete */
1222 }
1223
1224 svsk->sk_reclen = ntohl(svsk->sk_reclen);
1225 if (!(svsk->sk_reclen & 0x80000000)) {
1226 /* FIXME: technically, a record can be fragmented,
1227 * and non-terminal fragments will not have the top
1228 * bit set in the fragment length header.
1229 * But apparently no known nfs clients send fragmented
1230 * records. */
NeilBrown34e9a632007-01-29 13:19:52 -08001231 if (net_ratelimit())
1232 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
1233 " (non-terminal)\n",
1234 (unsigned long) svsk->sk_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 goto err_delete;
1236 }
1237 svsk->sk_reclen &= 0x7fffffff;
1238 dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen);
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001239 if (svsk->sk_reclen > serv->sv_max_mesg) {
NeilBrown34e9a632007-01-29 13:19:52 -08001240 if (net_ratelimit())
1241 printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx"
1242 " (large)\n",
1243 (unsigned long) svsk->sk_reclen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 goto err_delete;
1245 }
1246 }
1247
1248 /* Check whether enough data is available */
1249 len = svc_recv_available(svsk);
1250 if (len < 0)
1251 goto error;
1252
1253 if (len < svsk->sk_reclen) {
1254 dprintk("svc: incomplete TCP record (%d of %d)\n",
1255 len, svsk->sk_reclen);
1256 svc_sock_received(svsk);
1257 return -EAGAIN; /* record not complete */
1258 }
1259 len = svsk->sk_reclen;
1260 set_bit(SK_DATA, &svsk->sk_flags);
1261
NeilBrown3cc03b12006-10-04 02:15:47 -07001262 vec = rqstp->rq_vec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 vec[0] = rqstp->rq_arg.head[0];
1264 vlen = PAGE_SIZE;
1265 pnum = 1;
1266 while (vlen < len) {
NeilBrown44524352006-10-04 02:15:46 -07001267 vec[pnum].iov_base = page_address(rqstp->rq_pages[pnum]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 vec[pnum].iov_len = PAGE_SIZE;
1269 pnum++;
1270 vlen += PAGE_SIZE;
1271 }
NeilBrown44524352006-10-04 02:15:46 -07001272 rqstp->rq_respages = &rqstp->rq_pages[pnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 /* Now receive data */
1275 len = svc_recvfrom(rqstp, vec, pnum, len);
1276 if (len < 0)
1277 goto error;
1278
1279 dprintk("svc: TCP complete record (%d bytes)\n", len);
1280 rqstp->rq_arg.len = len;
1281 rqstp->rq_arg.page_base = 0;
1282 if (len <= rqstp->rq_arg.head[0].iov_len) {
1283 rqstp->rq_arg.head[0].iov_len = len;
1284 rqstp->rq_arg.page_len = 0;
1285 } else {
1286 rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len;
1287 }
1288
Tom Tucker5148bf42007-12-30 21:07:25 -06001289 rqstp->rq_xprt_ctxt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 rqstp->rq_prot = IPPROTO_TCP;
1291
1292 /* Reset TCP read info */
1293 svsk->sk_reclen = 0;
1294 svsk->sk_tcplen = 0;
1295
1296 svc_sock_received(svsk);
1297 if (serv->sv_stats)
1298 serv->sv_stats->nettcpcnt++;
1299
1300 return len;
1301
1302 err_delete:
Tom Tuckerd7979ae2007-12-30 21:07:34 -06001303 set_bit(SK_CLOSE, &svsk->sk_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return -EAGAIN;
1305
1306 error:
1307 if (len == -EAGAIN) {
1308 dprintk("RPC: TCP recvfrom got EAGAIN\n");
1309 svc_sock_received(svsk);
1310 } else {
1311 printk(KERN_NOTICE "%s: recvfrom returned errno %d\n",
1312 svsk->sk_server->sv_name, -len);
Olaf Kirch93fbf1a2006-01-06 00:19:56 -08001313 goto err_delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
1315
1316 return len;
1317}
1318
1319/*
1320 * Send out data on TCP socket.
1321 */
1322static int
1323svc_tcp_sendto(struct svc_rqst *rqstp)
1324{
1325 struct xdr_buf *xbufp = &rqstp->rq_res;
1326 int sent;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001327 __be32 reclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 /* Set up the first element of the reply kvec.
1330 * Any other kvecs that may be in use have been taken
1331 * care of by the server implementation itself.
1332 */
1333 reclen = htonl(0x80000000|((xbufp->len ) - 4));
1334 memcpy(xbufp->head[0].iov_base, &reclen, 4);
1335
1336 if (test_bit(SK_DEAD, &rqstp->rq_sock->sk_flags))
1337 return -ENOTCONN;
1338
1339 sent = svc_sendto(rqstp, &rqstp->rq_res);
1340 if (sent != xbufp->len) {
1341 printk(KERN_NOTICE "rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n",
1342 rqstp->rq_sock->sk_server->sv_name,
1343 (sent<0)?"got error":"sent only",
1344 sent, xbufp->len);
NeilBrownaaf68cf2007-02-08 14:20:30 -08001345 set_bit(SK_CLOSE, &rqstp->rq_sock->sk_flags);
1346 svc_sock_enqueue(rqstp->rq_sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 sent = -EAGAIN;
1348 }
1349 return sent;
1350}
1351
Tom Tuckere831fe62007-12-30 21:07:29 -06001352/*
1353 * Setup response header. TCP has a 4B record length field.
1354 */
1355static void svc_tcp_prep_reply_hdr(struct svc_rqst *rqstp)
1356{
1357 struct kvec *resv = &rqstp->rq_res.head[0];
1358
1359 /* tcp needs a space for the record length... */
1360 svc_putnl(resv, 0);
1361}
1362
Tom Tucker323bee32007-12-30 21:07:31 -06001363static int svc_tcp_has_wspace(struct svc_xprt *xprt)
1364{
1365 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1366 struct svc_serv *serv = svsk->sk_server;
1367 int required;
1368 int wspace;
1369
1370 /*
1371 * Set the SOCK_NOSPACE flag before checking the available
1372 * sock space.
1373 */
1374 set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
1375 required = atomic_read(&svsk->sk_reserved) + serv->sv_max_mesg;
1376 wspace = sk_stream_wspace(svsk->sk_sk);
1377
1378 if (wspace < sk_stream_min_wspace(svsk->sk_sk))
1379 return 0;
1380 if (required * 2 > wspace)
1381 return 0;
1382
1383 clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags);
1384 return 1;
1385}
1386
Tom Tucker360d8732007-12-30 21:07:17 -06001387static struct svc_xprt_ops svc_tcp_ops = {
Tom Tucker5d137992007-12-30 21:07:23 -06001388 .xpo_recvfrom = svc_tcp_recvfrom,
1389 .xpo_sendto = svc_tcp_sendto,
Tom Tucker5148bf42007-12-30 21:07:25 -06001390 .xpo_release_rqst = svc_release_skb,
Tom Tucker755ccea2007-12-30 21:07:27 -06001391 .xpo_detach = svc_sock_detach,
1392 .xpo_free = svc_sock_free,
Tom Tuckere831fe62007-12-30 21:07:29 -06001393 .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr,
Tom Tucker323bee32007-12-30 21:07:31 -06001394 .xpo_has_wspace = svc_tcp_has_wspace,
Tom Tucker38a417c2007-12-30 21:07:36 -06001395 .xpo_accept = svc_tcp_accept,
Tom Tucker360d8732007-12-30 21:07:17 -06001396};
1397
1398static struct svc_xprt_class svc_tcp_class = {
1399 .xcl_name = "tcp",
1400 .xcl_ops = &svc_tcp_ops,
Tom Tucker49023152007-12-30 21:07:21 -06001401 .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP,
Tom Tucker360d8732007-12-30 21:07:17 -06001402};
1403
1404void svc_init_xprt_sock(void)
1405{
1406 svc_reg_xprt_class(&svc_tcp_class);
1407 svc_reg_xprt_class(&svc_udp_class);
1408}
1409
1410void svc_cleanup_xprt_sock(void)
1411{
1412 svc_unreg_xprt_class(&svc_tcp_class);
1413 svc_unreg_xprt_class(&svc_udp_class);
1414}
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416static void
1417svc_tcp_init(struct svc_sock *svsk)
1418{
1419 struct sock *sk = svsk->sk_sk;
1420 struct tcp_sock *tp = tcp_sk(sk);
1421
Tom Tucker360d8732007-12-30 21:07:17 -06001422 svc_xprt_init(&svc_tcp_class, &svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 if (sk->sk_state == TCP_LISTEN) {
1425 dprintk("setting up TCP socket for listening\n");
Tom Tucker38a417c2007-12-30 21:07:36 -06001426 set_bit(SK_LISTENER, &svsk->sk_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 sk->sk_data_ready = svc_tcp_listen_data_ready;
1428 set_bit(SK_CONN, &svsk->sk_flags);
1429 } else {
1430 dprintk("setting up TCP socket for reading\n");
1431 sk->sk_state_change = svc_tcp_state_change;
1432 sk->sk_data_ready = svc_tcp_data_ready;
1433 sk->sk_write_space = svc_write_space;
1434
1435 svsk->sk_reclen = 0;
1436 svsk->sk_tcplen = 0;
1437
1438 tp->nonagle = 1; /* disable Nagle's algorithm */
1439
1440 /* initialise setting must have enough space to
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001441 * receive and respond to one request.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 * svc_tcp_recvfrom will re-adjust if necessary
1443 */
1444 svc_sock_setbufsize(svsk->sk_sock,
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001445 3 * svsk->sk_server->sv_max_mesg,
1446 3 * svsk->sk_server->sv_max_mesg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1449 set_bit(SK_DATA, &svsk->sk_flags);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001450 if (sk->sk_state != TCP_ESTABLISHED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 set_bit(SK_CLOSE, &svsk->sk_flags);
1452 }
1453}
1454
1455void
1456svc_sock_update_bufs(struct svc_serv *serv)
1457{
1458 /*
1459 * The number of server threads has changed. Update
1460 * rcvbuf and sndbuf accordingly on all sockets
1461 */
1462 struct list_head *le;
1463
1464 spin_lock_bh(&serv->sv_lock);
1465 list_for_each(le, &serv->sv_permsocks) {
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001466 struct svc_sock *svsk =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 list_entry(le, struct svc_sock, sk_list);
1468 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1469 }
1470 list_for_each(le, &serv->sv_tempsocks) {
1471 struct svc_sock *svsk =
1472 list_entry(le, struct svc_sock, sk_list);
1473 set_bit(SK_CHNGBUF, &svsk->sk_flags);
1474 }
1475 spin_unlock_bh(&serv->sv_lock);
1476}
1477
1478/*
Greg Banks3262c812006-10-02 02:17:58 -07001479 * Receive the next request on any socket. This code is carefully
1480 * organised not to touch any cachelines in the shared svc_serv
1481 * structure, only cachelines in the local svc_pool.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 */
1483int
NeilBrown6fb2b472006-10-02 02:17:50 -07001484svc_recv(struct svc_rqst *rqstp, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
Chuck Lever27459f02007-02-12 00:53:34 -08001486 struct svc_sock *svsk = NULL;
NeilBrown6fb2b472006-10-02 02:17:50 -07001487 struct svc_serv *serv = rqstp->rq_server;
Greg Banks3262c812006-10-02 02:17:58 -07001488 struct svc_pool *pool = rqstp->rq_pool;
NeilBrown44524352006-10-04 02:15:46 -07001489 int len, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 int pages;
1491 struct xdr_buf *arg;
1492 DECLARE_WAITQUEUE(wait, current);
1493
1494 dprintk("svc: server %p waiting for data (to = %ld)\n",
1495 rqstp, timeout);
1496
1497 if (rqstp->rq_sock)
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001498 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 "svc_recv: service %p, socket not NULL!\n",
1500 rqstp);
1501 if (waitqueue_active(&rqstp->rq_wait))
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001502 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 "svc_recv: service %p, wait queue active!\n",
1504 rqstp);
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 /* now allocate needed pages. If we get a failure, sleep briefly */
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001508 pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
NeilBrown44524352006-10-04 02:15:46 -07001509 for (i=0; i < pages ; i++)
1510 while (rqstp->rq_pages[i] == NULL) {
1511 struct page *p = alloc_page(GFP_KERNEL);
1512 if (!p)
1513 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1514 rqstp->rq_pages[i] = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
NeilBrown250f3912007-01-26 00:56:59 -08001516 rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
1517 BUG_ON(pages >= RPCSVC_MAXPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 /* Make arg->head point to first page and arg->pages point to rest */
1520 arg = &rqstp->rq_arg;
NeilBrown44524352006-10-04 02:15:46 -07001521 arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 arg->head[0].iov_len = PAGE_SIZE;
NeilBrown44524352006-10-04 02:15:46 -07001523 arg->pages = rqstp->rq_pages + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 arg->page_base = 0;
1525 /* save at least one page for response */
1526 arg->page_len = (pages-2)*PAGE_SIZE;
1527 arg->len = (pages-1)*PAGE_SIZE;
1528 arg->tail[0].iov_len = 0;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001529
1530 try_to_freeze();
NeilBrown1887b932005-11-15 00:09:10 -08001531 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (signalled())
1533 return -EINTR;
1534
Greg Banks3262c812006-10-02 02:17:58 -07001535 spin_lock_bh(&pool->sp_lock);
1536 if ((svsk = svc_sock_dequeue(pool)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 rqstp->rq_sock = svsk;
Greg Banksc45c3572006-10-02 02:17:54 -07001538 atomic_inc(&svsk->sk_inuse);
NeilBrownc6b0a9f2006-10-06 00:44:05 -07001539 rqstp->rq_reserved = serv->sv_max_mesg;
Greg Banks5685f0f2006-10-02 02:17:56 -07001540 atomic_add(rqstp->rq_reserved, &svsk->sk_reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 } else {
1542 /* No data pending. Go to sleep */
Greg Banks3262c812006-10-02 02:17:58 -07001543 svc_thread_enqueue(pool, rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 /*
1546 * We have to be able to interrupt this wait
1547 * to bring down the daemons ...
1548 */
1549 set_current_state(TASK_INTERRUPTIBLE);
1550 add_wait_queue(&rqstp->rq_wait, &wait);
Greg Banks3262c812006-10-02 02:17:58 -07001551 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 schedule_timeout(timeout);
1554
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001555 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Greg Banks3262c812006-10-02 02:17:58 -07001557 spin_lock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 remove_wait_queue(&rqstp->rq_wait, &wait);
1559
1560 if (!(svsk = rqstp->rq_sock)) {
Greg Banks3262c812006-10-02 02:17:58 -07001561 svc_thread_dequeue(pool, rqstp);
1562 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 dprintk("svc: server %p, no data yet\n", rqstp);
1564 return signalled()? -EINTR : -EAGAIN;
1565 }
1566 }
Greg Banks3262c812006-10-02 02:17:58 -07001567 spin_unlock_bh(&pool->sp_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Tom Tuckerd7979ae2007-12-30 21:07:34 -06001569 len = 0;
1570 if (test_bit(SK_CLOSE, &svsk->sk_flags)) {
1571 dprintk("svc_recv: found SK_CLOSE\n");
1572 svc_delete_socket(svsk);
Tom Tucker38a417c2007-12-30 21:07:36 -06001573 } else if (test_bit(SK_LISTENER, &svsk->sk_flags)) {
1574 struct svc_xprt *newxpt;
1575 newxpt = svsk->sk_xprt.xpt_ops->xpo_accept(&svsk->sk_xprt);
Tom Tuckerf9f3cc42007-12-30 21:07:40 -06001576 if (newxpt)
1577 svc_check_conn_limits(svsk->sk_server);
Tom Tucker38a417c2007-12-30 21:07:36 -06001578 svc_sock_received(svsk);
Tom Tuckerd7979ae2007-12-30 21:07:34 -06001579 } else {
1580 dprintk("svc: server %p, pool %u, socket %p, inuse=%d\n",
1581 rqstp, pool->sp_id, svsk, atomic_read(&svsk->sk_inuse));
1582 len = svsk->sk_xprt.xpt_ops->xpo_recvfrom(rqstp);
1583 dprintk("svc: got len=%d\n", len);
1584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 /* No data, incomplete (TCP) read, or accept() */
1587 if (len == 0 || len == -EAGAIN) {
1588 rqstp->rq_res.len = 0;
1589 svc_sock_release(rqstp);
1590 return -EAGAIN;
1591 }
1592 svsk->sk_lastrecv = get_seconds();
Greg Banks36bdfc82006-10-02 02:17:54 -07001593 clear_bit(SK_OLD, &svsk->sk_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Chuck Leverbcdb81a2007-02-12 00:53:37 -08001595 rqstp->rq_secure = svc_port_is_privileged(svc_addr(rqstp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 rqstp->rq_chandle.defer = svc_defer;
1597
1598 if (serv->sv_stats)
1599 serv->sv_stats->netcnt++;
1600 return len;
1601}
1602
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001603/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 * Drop request
1605 */
1606void
1607svc_drop(struct svc_rqst *rqstp)
1608{
1609 dprintk("svc: socket %p dropped request\n", rqstp->rq_sock);
1610 svc_sock_release(rqstp);
1611}
1612
1613/*
1614 * Return reply to client.
1615 */
1616int
1617svc_send(struct svc_rqst *rqstp)
1618{
1619 struct svc_sock *svsk;
1620 int len;
1621 struct xdr_buf *xb;
1622
1623 if ((svsk = rqstp->rq_sock) == NULL) {
1624 printk(KERN_WARNING "NULL socket pointer in %s:%d\n",
1625 __FILE__, __LINE__);
1626 return -EFAULT;
1627 }
1628
1629 /* release the receive skb before sending the reply */
Tom Tucker5148bf42007-12-30 21:07:25 -06001630 rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 /* calculate over-all length */
1633 xb = & rqstp->rq_res;
1634 xb->len = xb->head[0].iov_len +
1635 xb->page_len +
1636 xb->tail[0].iov_len;
1637
Ingo Molnar57b47a52006-03-20 22:35:41 -08001638 /* Grab svsk->sk_mutex to serialize outgoing data. */
1639 mutex_lock(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (test_bit(SK_DEAD, &svsk->sk_flags))
1641 len = -ENOTCONN;
1642 else
Tom Tucker5d137992007-12-30 21:07:23 -06001643 len = svsk->sk_xprt.xpt_ops->xpo_sendto(rqstp);
Ingo Molnar57b47a52006-03-20 22:35:41 -08001644 mutex_unlock(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 svc_sock_release(rqstp);
1646
1647 if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
1648 return 0;
1649 return len;
1650}
1651
1652/*
Greg Banks36bdfc82006-10-02 02:17:54 -07001653 * Timer function to close old temporary sockets, using
1654 * a mark-and-sweep algorithm.
1655 */
1656static void
1657svc_age_temp_sockets(unsigned long closure)
1658{
1659 struct svc_serv *serv = (struct svc_serv *)closure;
1660 struct svc_sock *svsk;
1661 struct list_head *le, *next;
1662 LIST_HEAD(to_be_aged);
1663
1664 dprintk("svc_age_temp_sockets\n");
1665
1666 if (!spin_trylock_bh(&serv->sv_lock)) {
1667 /* busy, try again 1 sec later */
1668 dprintk("svc_age_temp_sockets: busy\n");
1669 mod_timer(&serv->sv_temptimer, jiffies + HZ);
1670 return;
1671 }
1672
1673 list_for_each_safe(le, next, &serv->sv_tempsocks) {
1674 svsk = list_entry(le, struct svc_sock, sk_list);
1675
1676 if (!test_and_set_bit(SK_OLD, &svsk->sk_flags))
1677 continue;
Neil Brown7a1fa062007-09-14 10:28:08 -04001678 if (atomic_read(&svsk->sk_inuse) > 1 || test_bit(SK_BUSY, &svsk->sk_flags))
Greg Banks36bdfc82006-10-02 02:17:54 -07001679 continue;
Greg Banksc45c3572006-10-02 02:17:54 -07001680 atomic_inc(&svsk->sk_inuse);
Greg Banks36bdfc82006-10-02 02:17:54 -07001681 list_move(le, &to_be_aged);
1682 set_bit(SK_CLOSE, &svsk->sk_flags);
1683 set_bit(SK_DETACHED, &svsk->sk_flags);
1684 }
1685 spin_unlock_bh(&serv->sv_lock);
1686
1687 while (!list_empty(&to_be_aged)) {
1688 le = to_be_aged.next;
1689 /* fiddling the sk_list node is safe 'cos we're SK_DETACHED */
1690 list_del_init(le);
1691 svsk = list_entry(le, struct svc_sock, sk_list);
1692
1693 dprintk("queuing svsk %p for closing, %lu seconds old\n",
1694 svsk, get_seconds() - svsk->sk_lastrecv);
1695
1696 /* a thread will dequeue and close it soon */
1697 svc_sock_enqueue(svsk);
1698 svc_sock_put(svsk);
1699 }
1700
1701 mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
1702}
1703
1704/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 * Initialize socket for RPC use and create svc_sock struct
1706 * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF.
1707 */
Chuck Lever6b174332007-02-12 00:53:28 -08001708static struct svc_sock *svc_setup_socket(struct svc_serv *serv,
1709 struct socket *sock,
1710 int *errp, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
1712 struct svc_sock *svsk;
1713 struct sock *inet;
Chuck Lever6b174332007-02-12 00:53:28 -08001714 int pmap_register = !(flags & SVC_SOCK_ANONYMOUS);
1715 int is_temporary = flags & SVC_SOCK_TEMPORARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 dprintk("svc: svc_setup_socket %p\n", sock);
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001718 if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 *errp = -ENOMEM;
1720 return NULL;
1721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723 inet = sock->sk;
1724
1725 /* Register socket with portmapper */
1726 if (*errp >= 0 && pmap_register)
1727 *errp = svc_register(serv, inet->sk_protocol,
1728 ntohs(inet_sk(inet)->sport));
1729
1730 if (*errp < 0) {
1731 kfree(svsk);
1732 return NULL;
1733 }
1734
1735 set_bit(SK_BUSY, &svsk->sk_flags);
1736 inet->sk_user_data = svsk;
1737 svsk->sk_sock = sock;
1738 svsk->sk_sk = inet;
1739 svsk->sk_ostate = inet->sk_state_change;
1740 svsk->sk_odata = inet->sk_data_ready;
1741 svsk->sk_owspace = inet->sk_write_space;
1742 svsk->sk_server = serv;
NeilBrownaaf68cf2007-02-08 14:20:30 -08001743 atomic_set(&svsk->sk_inuse, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 svsk->sk_lastrecv = get_seconds();
NeilBrown7ac1bea2007-05-09 02:34:48 -07001745 spin_lock_init(&svsk->sk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 INIT_LIST_HEAD(&svsk->sk_deferred);
1747 INIT_LIST_HEAD(&svsk->sk_ready);
Ingo Molnar57b47a52006-03-20 22:35:41 -08001748 mutex_init(&svsk->sk_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 /* Initialize the socket */
1751 if (sock->type == SOCK_DGRAM)
1752 svc_udp_init(svsk);
1753 else
1754 svc_tcp_init(svsk);
1755
1756 spin_lock_bh(&serv->sv_lock);
Chuck Lever6b174332007-02-12 00:53:28 -08001757 if (is_temporary) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 set_bit(SK_TEMP, &svsk->sk_flags);
1759 list_add(&svsk->sk_list, &serv->sv_tempsocks);
1760 serv->sv_tmpcnt++;
Greg Banks36bdfc82006-10-02 02:17:54 -07001761 if (serv->sv_temptimer.function == NULL) {
1762 /* setup timer to age temp sockets */
1763 setup_timer(&serv->sv_temptimer, svc_age_temp_sockets,
1764 (unsigned long)serv);
1765 mod_timer(&serv->sv_temptimer,
1766 jiffies + svc_conn_age_period * HZ);
1767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 } else {
1769 clear_bit(SK_TEMP, &svsk->sk_flags);
1770 list_add(&svsk->sk_list, &serv->sv_permsocks);
1771 }
1772 spin_unlock_bh(&serv->sv_lock);
1773
1774 dprintk("svc: svc_setup_socket created %p (inet %p)\n",
1775 svsk, svsk->sk_sk);
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return svsk;
1778}
1779
NeilBrownb41b66d2006-10-02 02:17:48 -07001780int svc_addsock(struct svc_serv *serv,
1781 int fd,
1782 char *name_return,
1783 int *proto)
1784{
1785 int err = 0;
1786 struct socket *so = sockfd_lookup(fd, &err);
1787 struct svc_sock *svsk = NULL;
1788
1789 if (!so)
1790 return err;
1791 if (so->sk->sk_family != AF_INET)
1792 err = -EAFNOSUPPORT;
1793 else if (so->sk->sk_protocol != IPPROTO_TCP &&
1794 so->sk->sk_protocol != IPPROTO_UDP)
1795 err = -EPROTONOSUPPORT;
1796 else if (so->state > SS_UNCONNECTED)
1797 err = -EISCONN;
1798 else {
Chuck Lever6b174332007-02-12 00:53:28 -08001799 svsk = svc_setup_socket(serv, so, &err, SVC_SOCK_DEFAULTS);
NeilBrowne79eff12007-02-12 00:53:30 -08001800 if (svsk) {
1801 svc_sock_received(svsk);
NeilBrownb41b66d2006-10-02 02:17:48 -07001802 err = 0;
NeilBrowne79eff12007-02-12 00:53:30 -08001803 }
NeilBrownb41b66d2006-10-02 02:17:48 -07001804 }
1805 if (err) {
1806 sockfd_put(so);
1807 return err;
1808 }
1809 if (proto) *proto = so->sk->sk_protocol;
1810 return one_sock_name(name_return, svsk);
1811}
1812EXPORT_SYMBOL_GPL(svc_addsock);
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814/*
1815 * Create socket for RPC service.
1816 */
Chuck Lever6b174332007-02-12 00:53:28 -08001817static int svc_create_socket(struct svc_serv *serv, int protocol,
Chuck Lever77f1f672007-02-12 00:53:39 -08001818 struct sockaddr *sin, int len, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
1820 struct svc_sock *svsk;
1821 struct socket *sock;
1822 int error;
1823 int type;
Chuck Leverad06e4b2007-02-12 00:53:32 -08001824 char buf[RPC_MAX_ADDRBUFLEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Chuck Leverad06e4b2007-02-12 00:53:32 -08001826 dprintk("svc: svc_create_socket(%s, %d, %s)\n",
1827 serv->sv_program->pg_name, protocol,
Chuck Lever77f1f672007-02-12 00:53:39 -08001828 __svc_print_addr(sin, buf, sizeof(buf)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) {
1831 printk(KERN_WARNING "svc: only UDP and TCP "
1832 "sockets supported\n");
1833 return -EINVAL;
1834 }
1835 type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM;
1836
Chuck Lever77f1f672007-02-12 00:53:39 -08001837 error = sock_create_kern(sin->sa_family, type, protocol, &sock);
1838 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return error;
1840
Peter Zijlstraed075362006-12-06 20:35:24 -08001841 svc_reclassify_socket(sock);
1842
Eric Sesterhenn18114742006-09-28 14:37:07 -07001843 if (type == SOCK_STREAM)
Chuck Lever77f1f672007-02-12 00:53:39 -08001844 sock->sk->sk_reuse = 1; /* allow address reuse */
1845 error = kernel_bind(sock, sin, len);
Eric Sesterhenn18114742006-09-28 14:37:07 -07001846 if (error < 0)
1847 goto bummer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 if (protocol == IPPROTO_TCP) {
Sridhar Samudralae6242e92006-08-07 20:58:01 -07001850 if ((error = kernel_listen(sock, 64)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 goto bummer;
1852 }
1853
NeilBrowne79eff12007-02-12 00:53:30 -08001854 if ((svsk = svc_setup_socket(serv, sock, &error, flags)) != NULL) {
1855 svc_sock_received(svsk);
Chuck Lever6b174332007-02-12 00:53:28 -08001856 return ntohs(inet_sk(svsk->sk_sk)->sport);
NeilBrowne79eff12007-02-12 00:53:30 -08001857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859bummer:
1860 dprintk("svc: svc_create_socket error = %d\n", -error);
1861 sock_release(sock);
1862 return error;
1863}
1864
1865/*
Tom Tucker755ccea2007-12-30 21:07:27 -06001866 * Detach the svc_sock from the socket so that no
1867 * more callbacks occur.
1868 */
1869static void svc_sock_detach(struct svc_xprt *xprt)
1870{
1871 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1872 struct sock *sk = svsk->sk_sk;
1873
1874 dprintk("svc: svc_sock_detach(%p)\n", svsk);
1875
1876 /* put back the old socket callbacks */
1877 sk->sk_state_change = svsk->sk_ostate;
1878 sk->sk_data_ready = svsk->sk_odata;
1879 sk->sk_write_space = svsk->sk_owspace;
1880}
1881
1882/*
1883 * Free the svc_sock's socket resources and the svc_sock itself.
1884 */
1885static void svc_sock_free(struct svc_xprt *xprt)
1886{
1887 struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt);
1888 dprintk("svc: svc_sock_free(%p)\n", svsk);
1889
1890 if (svsk->sk_info_authunix != NULL)
1891 svcauth_unix_info_release(svsk->sk_info_authunix);
1892 if (svsk->sk_sock->file)
1893 sockfd_put(svsk->sk_sock);
1894 else
1895 sock_release(svsk->sk_sock);
1896 kfree(svsk);
1897}
1898
1899/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 * Remove a dead socket
1901 */
NeilBrownaaf68cf2007-02-08 14:20:30 -08001902static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903svc_delete_socket(struct svc_sock *svsk)
1904{
1905 struct svc_serv *serv;
1906 struct sock *sk;
1907
1908 dprintk("svc: svc_delete_socket(%p)\n", svsk);
1909
1910 serv = svsk->sk_server;
1911 sk = svsk->sk_sk;
1912
Tom Tucker755ccea2007-12-30 21:07:27 -06001913 svsk->sk_xprt.xpt_ops->xpo_detach(&svsk->sk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 spin_lock_bh(&serv->sv_lock);
1916
Greg Banks36bdfc82006-10-02 02:17:54 -07001917 if (!test_and_set_bit(SK_DETACHED, &svsk->sk_flags))
1918 list_del_init(&svsk->sk_list);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001919 /*
Greg Banks3262c812006-10-02 02:17:58 -07001920 * We used to delete the svc_sock from whichever list
1921 * it's sk_ready node was on, but we don't actually
1922 * need to. This is because the only time we're called
1923 * while still attached to a queue, the queue itself
1924 * is about to be destroyed (in svc_destroy).
1925 */
NeilBrownaaf68cf2007-02-08 14:20:30 -08001926 if (!test_and_set_bit(SK_DEAD, &svsk->sk_flags)) {
1927 BUG_ON(atomic_read(&svsk->sk_inuse)<2);
1928 atomic_dec(&svsk->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 if (test_bit(SK_TEMP, &svsk->sk_flags))
1930 serv->sv_tmpcnt--;
NeilBrownaaf68cf2007-02-08 14:20:30 -08001931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Neil Brownd6740df2006-10-29 22:46:45 -08001933 spin_unlock_bh(&serv->sv_lock);
NeilBrownaaf68cf2007-02-08 14:20:30 -08001934}
1935
NeilBrowncda1fd42007-03-06 01:42:22 -08001936static void svc_close_socket(struct svc_sock *svsk)
NeilBrownaaf68cf2007-02-08 14:20:30 -08001937{
1938 set_bit(SK_CLOSE, &svsk->sk_flags);
1939 if (test_and_set_bit(SK_BUSY, &svsk->sk_flags))
1940 /* someone else will have to effect the close */
1941 return;
1942
1943 atomic_inc(&svsk->sk_inuse);
1944 svc_delete_socket(svsk);
1945 clear_bit(SK_BUSY, &svsk->sk_flags);
Neil Brownd6740df2006-10-29 22:46:45 -08001946 svc_sock_put(svsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947}
1948
NeilBrowncda1fd42007-03-06 01:42:22 -08001949void svc_force_close_socket(struct svc_sock *svsk)
1950{
1951 set_bit(SK_CLOSE, &svsk->sk_flags);
1952 if (test_bit(SK_BUSY, &svsk->sk_flags)) {
1953 /* Waiting to be processed, but no threads left,
1954 * So just remove it from the waiting list
1955 */
1956 list_del_init(&svsk->sk_ready);
1957 clear_bit(SK_BUSY, &svsk->sk_flags);
1958 }
1959 svc_close_socket(svsk);
1960}
1961
Chuck Lever6b174332007-02-12 00:53:28 -08001962/**
1963 * svc_makesock - Make a socket for nfsd and lockd
1964 * @serv: RPC server structure
1965 * @protocol: transport protocol to use
1966 * @port: port to use
Chuck Lever482fb942007-02-12 00:53:29 -08001967 * @flags: requested socket characteristics
Chuck Lever6b174332007-02-12 00:53:28 -08001968 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 */
Chuck Lever482fb942007-02-12 00:53:29 -08001970int svc_makesock(struct svc_serv *serv, int protocol, unsigned short port,
1971 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
Chuck Lever6b174332007-02-12 00:53:28 -08001973 struct sockaddr_in sin = {
1974 .sin_family = AF_INET,
1975 .sin_addr.s_addr = INADDR_ANY,
1976 .sin_port = htons(port),
1977 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 dprintk("svc: creating socket proto = %d\n", protocol);
Chuck Lever77f1f672007-02-12 00:53:39 -08001980 return svc_create_socket(serv, protocol, (struct sockaddr *) &sin,
1981 sizeof(sin), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
1984/*
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08001985 * Handle defer and revisit of requests
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 */
1987
1988static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
1989{
1990 struct svc_deferred_req *dr = container_of(dreq, struct svc_deferred_req, handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 struct svc_sock *svsk;
1992
1993 if (too_many) {
1994 svc_sock_put(dr->svsk);
1995 kfree(dr);
1996 return;
1997 }
1998 dprintk("revisit queued\n");
1999 svsk = dr->svsk;
2000 dr->svsk = NULL;
NeilBrown7ac1bea2007-05-09 02:34:48 -07002001 spin_lock(&svsk->sk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 list_add(&dr->handle.recent, &svsk->sk_deferred);
NeilBrown7ac1bea2007-05-09 02:34:48 -07002003 spin_unlock(&svsk->sk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 set_bit(SK_DEFERRED, &svsk->sk_flags);
2005 svc_sock_enqueue(svsk);
2006 svc_sock_put(svsk);
2007}
2008
2009static struct cache_deferred_req *
2010svc_defer(struct cache_req *req)
2011{
2012 struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
2013 int size = sizeof(struct svc_deferred_req) + (rqstp->rq_arg.len);
2014 struct svc_deferred_req *dr;
2015
2016 if (rqstp->rq_arg.page_len)
2017 return NULL; /* if more than a page, give up FIXME */
2018 if (rqstp->rq_deferred) {
2019 dr = rqstp->rq_deferred;
2020 rqstp->rq_deferred = NULL;
2021 } else {
2022 int skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
2023 /* FIXME maybe discard if size too large */
2024 dr = kmalloc(size, GFP_KERNEL);
2025 if (dr == NULL)
2026 return NULL;
2027
2028 dr->handle.owner = rqstp->rq_server;
2029 dr->prot = rqstp->rq_prot;
Chuck Lever24422222007-02-12 00:53:33 -08002030 memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
2031 dr->addrlen = rqstp->rq_addrlen;
J. Bruce Fields1918e342006-01-18 17:43:16 -08002032 dr->daddr = rqstp->rq_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 dr->argslen = rqstp->rq_arg.len >> 2;
2034 memcpy(dr->args, rqstp->rq_arg.head[0].iov_base-skip, dr->argslen<<2);
2035 }
Greg Banksc45c3572006-10-02 02:17:54 -07002036 atomic_inc(&rqstp->rq_sock->sk_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 dr->svsk = rqstp->rq_sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 dr->handle.revisit = svc_revisit;
2040 return &dr->handle;
2041}
2042
2043/*
2044 * recv data from a deferred request into an active one
2045 */
2046static int svc_deferred_recv(struct svc_rqst *rqstp)
2047{
2048 struct svc_deferred_req *dr = rqstp->rq_deferred;
2049
2050 rqstp->rq_arg.head[0].iov_base = dr->args;
2051 rqstp->rq_arg.head[0].iov_len = dr->argslen<<2;
2052 rqstp->rq_arg.page_len = 0;
2053 rqstp->rq_arg.len = dr->argslen<<2;
2054 rqstp->rq_prot = dr->prot;
Chuck Lever24422222007-02-12 00:53:33 -08002055 memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
2056 rqstp->rq_addrlen = dr->addrlen;
J. Bruce Fields1918e342006-01-18 17:43:16 -08002057 rqstp->rq_daddr = dr->daddr;
NeilBrown44524352006-10-04 02:15:46 -07002058 rqstp->rq_respages = rqstp->rq_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 return dr->argslen<<2;
2060}
2061
2062
2063static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk)
2064{
2065 struct svc_deferred_req *dr = NULL;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08002066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if (!test_bit(SK_DEFERRED, &svsk->sk_flags))
2068 return NULL;
NeilBrown7ac1bea2007-05-09 02:34:48 -07002069 spin_lock(&svsk->sk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 clear_bit(SK_DEFERRED, &svsk->sk_flags);
2071 if (!list_empty(&svsk->sk_deferred)) {
2072 dr = list_entry(svsk->sk_deferred.next,
2073 struct svc_deferred_req,
2074 handle.recent);
2075 list_del_init(&dr->handle.recent);
2076 set_bit(SK_DEFERRED, &svsk->sk_flags);
2077 }
NeilBrown7ac1bea2007-05-09 02:34:48 -07002078 spin_unlock(&svsk->sk_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return dr;
2080}