blob: 54d8e7bc23411d51764686133f40517a3a38cdd2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/include/linux/sunrpc/svc.h
3 *
4 * RPC server declarations.
5 *
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
7 */
8
9
10#ifndef SUNRPC_SVC_H
11#define SUNRPC_SVC_H
12
13#include <linux/in.h>
14#include <linux/sunrpc/types.h>
15#include <linux/sunrpc/xdr.h>
16#include <linux/sunrpc/svcauth.h>
17#include <linux/wait.h>
18#include <linux/mm.h>
19
Greg Banks3262c812006-10-02 02:17:58 -070020
21/*
22 *
23 * RPC service thread pool.
24 *
25 * Pool of threads and temporary sockets. Generally there is only
26 * a single one of these per RPC service, but on NUMA machines those
27 * services that can benefit from it (i.e. nfs but not lockd) will
28 * have one pool per NUMA node. This optimisation reduces cross-
29 * node traffic on multi-node NUMA NFS servers.
30 */
31struct svc_pool {
32 unsigned int sp_id; /* pool id; also node id on NUMA */
33 spinlock_t sp_lock; /* protects all fields */
34 struct list_head sp_threads; /* idle server threads */
35 struct list_head sp_sockets; /* pending sockets */
36 unsigned int sp_nrthreads; /* # of threads in pool */
37} ____cacheline_aligned_in_smp;
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*
40 * RPC service.
41 *
42 * An RPC service is a ``daemon,'' possibly multithreaded, which
43 * receives and processes incoming RPC messages.
44 * It has one or more transport sockets associated with it, and maintains
45 * a list of idle threads waiting for input.
46 *
47 * We currently do not support more than one RPC program per daemon.
48 */
49struct svc_serv {
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 struct svc_program * sv_program; /* RPC program */
51 struct svc_stat * sv_stats; /* RPC statistics */
52 spinlock_t sv_lock;
53 unsigned int sv_nrthreads; /* # of server threads */
54 unsigned int sv_bufsz; /* datagram buffer size */
55 unsigned int sv_xdrsize; /* XDR buffer size */
56
57 struct list_head sv_permsocks; /* all permanent sockets */
58 struct list_head sv_tempsocks; /* all temporary sockets */
59 int sv_tmpcnt; /* count of temporary sockets */
Greg Banks36bdfc82006-10-02 02:17:54 -070060 struct timer_list sv_temptimer; /* timer for aging temporary sockets */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 char * sv_name; /* service name */
NeilBrownbc591cc2006-10-02 02:17:44 -070063
Greg Banks3262c812006-10-02 02:17:58 -070064 unsigned int sv_nrpools; /* number of thread pools */
65 struct svc_pool * sv_pools; /* array of thread pools */
66
NeilBrownbc591cc2006-10-02 02:17:44 -070067 void (*sv_shutdown)(struct svc_serv *serv);
68 /* Callback to use when last thread
69 * exits.
70 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071};
72
73/*
Greg Banks9a24ab52006-10-02 02:17:58 -070074 * We use sv_nrthreads as a reference count. svc_destroy() drops
75 * this refcount, so we need to bump it up around operations that
76 * change the number of threads. Horrible, but there it is.
77 * Should be called with the BKL held.
78 */
79static inline void svc_get(struct svc_serv *serv)
80{
81 serv->sv_nrthreads++;
82}
83
84/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * Maximum payload size supported by a kernel RPC server.
86 * This is use to determine the max number of pages nfsd is
87 * willing to return in a single READ operation.
88 */
89#define RPCSVC_MAXPAYLOAD (64*1024u)
90
91/*
92 * RPC Requsts and replies are stored in one or more pages.
93 * We maintain an array of pages for each server thread.
94 * Requests are copied into these pages as they arrive. Remaining
95 * pages are available to write the reply into.
96 *
97 * Pages are sent using ->sendpage so each server thread needs to
98 * allocate more to replace those used in sending. To help keep track
99 * of these pages we have a receive list where all pages initialy live,
100 * and a send list where pages are moved to when there are to be part
101 * of a reply.
102 *
103 * We use xdr_buf for holding responses as it fits well with NFS
104 * read responses (that have a header, and some data pages, and possibly
105 * a tail) and means we can share some client side routines.
106 *
107 * The xdr_buf.head kvec always points to the first page in the rq_*pages
108 * list. The xdr_buf.pages pointer points to the second page on that
109 * list. xdr_buf.tail points to the end of the first page.
110 * This assumes that the non-page part of an rpc reply will fit
111 * in a page - NFSd ensures this. lockd also has no trouble.
112 *
113 * Each request/reply pair can have at most one "payload", plus two pages,
114 * one for the request, and one for the reply.
115 */
116#define RPCSVC_MAXPAGES ((RPCSVC_MAXPAYLOAD+PAGE_SIZE-1)/PAGE_SIZE + 2)
117
Alexey Dobriyan76994312006-09-26 22:28:46 -0700118static inline u32 svc_getnl(struct kvec *iov)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Alexey Dobriyan76994312006-09-26 22:28:46 -0700120 __be32 val, *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 vp = iov->iov_base;
122 val = *vp++;
123 iov->iov_base = (void*)vp;
Alexey Dobriyan76994312006-09-26 22:28:46 -0700124 iov->iov_len -= sizeof(__be32);
125 return ntohl(val);
126}
127
128static inline void svc_putnl(struct kvec *iov, u32 val)
129{
130 __be32 *vp = iov->iov_base + iov->iov_len;
131 *vp = htonl(val);
132 iov->iov_len += sizeof(__be32);
133}
134
135static inline __be32 svc_getu32(struct kvec *iov)
136{
137 __be32 val, *vp;
138 vp = iov->iov_base;
139 val = *vp++;
140 iov->iov_base = (void*)vp;
141 iov->iov_len -= sizeof(__be32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return val;
143}
144
145static inline void svc_ungetu32(struct kvec *iov)
146{
Alexey Dobriyan76994312006-09-26 22:28:46 -0700147 __be32 *vp = (__be32 *)iov->iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 iov->iov_base = (void *)(vp - 1);
149 iov->iov_len += sizeof(*vp);
150}
151
Alexey Dobriyan76994312006-09-26 22:28:46 -0700152static inline void svc_putu32(struct kvec *iov, __be32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Alexey Dobriyan76994312006-09-26 22:28:46 -0700154 __be32 *vp = iov->iov_base + iov->iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 *vp = val;
Alexey Dobriyan76994312006-09-26 22:28:46 -0700156 iov->iov_len += sizeof(__be32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159
160/*
161 * The context of a single thread, including the request currently being
162 * processed.
163 * NOTE: First two items must be prev/next.
164 */
165struct svc_rqst {
166 struct list_head rq_list; /* idle list */
167 struct svc_sock * rq_sock; /* socket */
168 struct sockaddr_in rq_addr; /* peer address */
169 int rq_addrlen;
170
171 struct svc_serv * rq_server; /* RPC service definition */
Greg Banks3262c812006-10-02 02:17:58 -0700172 struct svc_pool * rq_pool; /* thread pool */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 struct svc_procedure * rq_procinfo; /* procedure info */
174 struct auth_ops * rq_authop; /* authentication flavour */
175 struct svc_cred rq_cred; /* auth info */
176 struct sk_buff * rq_skbuff; /* fast recv inet buffer */
177 struct svc_deferred_req*rq_deferred; /* deferred request we are replaying */
178
179 struct xdr_buf rq_arg;
180 struct xdr_buf rq_res;
181 struct page * rq_argpages[RPCSVC_MAXPAGES];
182 struct page * rq_respages[RPCSVC_MAXPAGES];
183 int rq_restailpage;
184 short rq_argused; /* pages used for argument */
185 short rq_arghi; /* pages available in argument page list */
186 short rq_resused; /* pages used for result */
187
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700188 __be32 rq_xid; /* transmission id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 u32 rq_prog; /* program number */
190 u32 rq_vers; /* program version */
191 u32 rq_proc; /* procedure number */
192 u32 rq_prot; /* IP protocol */
193 unsigned short
194 rq_secure : 1; /* secure port */
195
196
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700197 __be32 rq_daddr; /* dest addr of request - reply from here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 void * rq_argp; /* decoded arguments */
200 void * rq_resp; /* xdr'd results */
201 void * rq_auth_data; /* flavor-specific data */
202
203 int rq_reserved; /* space on socket outq
204 * reserved for this request
205 */
206
207 struct cache_req rq_chandle; /* handle passed to caches for
208 * request delaying
209 */
210 /* Catering to nfsd */
211 struct auth_domain * rq_client; /* RPC peer info */
212 struct svc_cacherep * rq_cacherep; /* cache info */
213 struct knfsd_fh * rq_reffh; /* Referrence filehandle, used to
214 * determine what device number
215 * to report (real or virtual)
216 */
J. Bruce Fields5c04c462006-06-30 01:56:19 -0700217 int rq_sendfile_ok; /* turned off in gss privacy
218 * to prevent encrypting page
219 * cache pages */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 wait_queue_head_t rq_wait; /* synchronization */
221};
222
223/*
224 * Check buffer bounds after decoding arguments
225 */
226static inline int
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700227xdr_argsize_check(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 char *cp = (char *)p;
230 struct kvec *vec = &rqstp->rq_arg.head[0];
NeilBrown0ba75362005-11-07 01:00:26 -0800231 return cp >= (char*)vec->iov_base
232 && cp <= (char*)vec->iov_base + vec->iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235static inline int
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700236xdr_ressize_check(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 struct kvec *vec = &rqstp->rq_res.head[0];
239 char *cp = (char*)p;
240
241 vec->iov_len = cp - (char*)vec->iov_base;
242
243 return vec->iov_len <= PAGE_SIZE;
244}
245
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000246static inline struct page *
247svc_take_res_page(struct svc_rqst *rqstp)
248{
249 if (rqstp->rq_arghi <= rqstp->rq_argused)
250 return NULL;
251 rqstp->rq_arghi--;
252 rqstp->rq_respages[rqstp->rq_resused] =
253 rqstp->rq_argpages[rqstp->rq_arghi];
254 return rqstp->rq_respages[rqstp->rq_resused++];
255}
256
J. Bruce Fields6f54e2d2006-04-10 22:55:36 -0700257static inline void svc_take_page(struct svc_rqst *rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
J. Bruce Fields6f54e2d2006-04-10 22:55:36 -0700259 if (rqstp->rq_arghi <= rqstp->rq_argused) {
260 WARN_ON(1);
261 return;
262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 rqstp->rq_arghi--;
264 rqstp->rq_respages[rqstp->rq_resused] =
265 rqstp->rq_argpages[rqstp->rq_arghi];
266 rqstp->rq_resused++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269static inline void svc_pushback_allpages(struct svc_rqst *rqstp)
270{
271 while (rqstp->rq_resused) {
272 if (rqstp->rq_respages[--rqstp->rq_resused] == NULL)
273 continue;
274 rqstp->rq_argpages[rqstp->rq_arghi++] =
275 rqstp->rq_respages[rqstp->rq_resused];
276 rqstp->rq_respages[rqstp->rq_resused] = NULL;
277 }
278}
279
280static inline void svc_pushback_unused_pages(struct svc_rqst *rqstp)
281{
282 while (rqstp->rq_resused &&
283 rqstp->rq_res.pages != &rqstp->rq_respages[rqstp->rq_resused]) {
284
285 if (rqstp->rq_respages[--rqstp->rq_resused] != NULL) {
286 rqstp->rq_argpages[rqstp->rq_arghi++] =
287 rqstp->rq_respages[rqstp->rq_resused];
288 rqstp->rq_respages[rqstp->rq_resused] = NULL;
289 }
290 }
291}
292
293static inline void svc_free_allpages(struct svc_rqst *rqstp)
294{
295 while (rqstp->rq_resused) {
296 if (rqstp->rq_respages[--rqstp->rq_resused] == NULL)
297 continue;
298 put_page(rqstp->rq_respages[rqstp->rq_resused]);
299 rqstp->rq_respages[rqstp->rq_resused] = NULL;
300 }
301}
302
303struct svc_deferred_req {
304 u32 prot; /* protocol (UDP or TCP) */
305 struct sockaddr_in addr;
306 struct svc_sock *svsk; /* where reply must go */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700307 __be32 daddr; /* where reply must come from */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct cache_deferred_req handle;
309 int argslen;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700310 __be32 args[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311};
312
313/*
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +0000314 * List of RPC programs on the same transport endpoint
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 */
316struct svc_program {
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +0000317 struct svc_program * pg_next; /* other programs (same xprt) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 u32 pg_prog; /* program number */
319 unsigned int pg_lovers; /* lowest version */
320 unsigned int pg_hivers; /* lowest version */
321 unsigned int pg_nvers; /* number of versions */
322 struct svc_version ** pg_vers; /* version array */
323 char * pg_name; /* service name */
324 char * pg_class; /* class name: services sharing authentication */
325 struct svc_stat * pg_stats; /* rpc statistics */
326 int (*pg_authenticate)(struct svc_rqst *);
327};
328
329/*
330 * RPC program version
331 */
332struct svc_version {
333 u32 vs_vers; /* version number */
334 u32 vs_nproc; /* number of procedures */
335 struct svc_procedure * vs_proc; /* per-procedure info */
336 u32 vs_xdrsize; /* xdrsize needed for this version */
337
338 /* Override dispatch function (e.g. when caching replies).
339 * A return value of 0 means drop the request.
340 * vs_dispatch == NULL means use default dispatcher.
341 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700342 int (*vs_dispatch)(struct svc_rqst *, __be32 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343};
344
345/*
346 * RPC procedure info
347 */
348typedef int (*svc_procfunc)(struct svc_rqst *, void *argp, void *resp);
349struct svc_procedure {
350 svc_procfunc pc_func; /* process the request */
351 kxdrproc_t pc_decode; /* XDR decode args */
352 kxdrproc_t pc_encode; /* XDR encode result */
353 kxdrproc_t pc_release; /* XDR free result */
354 unsigned int pc_argsize; /* argument struct size */
355 unsigned int pc_ressize; /* result struct size */
356 unsigned int pc_count; /* call count */
357 unsigned int pc_cachetype; /* cache info (NFS) */
358 unsigned int pc_xdrressize; /* maximum size of XDR reply */
359};
360
361/*
362 * This is the RPC server thread function prototype
363 */
364typedef void (*svc_thread_fn)(struct svc_rqst *);
365
366/*
367 * Function prototypes.
368 */
NeilBrownbc591cc2006-10-02 02:17:44 -0700369struct svc_serv * svc_create(struct svc_program *, unsigned int,
370 void (*shutdown)(struct svc_serv*));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371int svc_create_thread(svc_thread_fn, struct svc_serv *);
372void svc_exit_thread(struct svc_rqst *);
373void svc_destroy(struct svc_serv *);
NeilBrown6fb2b472006-10-02 02:17:50 -0700374int svc_process(struct svc_rqst *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375int svc_register(struct svc_serv *, int, unsigned short);
376void svc_wake_up(struct svc_serv *);
377void svc_reserve(struct svc_rqst *rqstp, int space);
378
379#endif /* SUNRPC_SVC_H */