blob: cb0ed9beb22726b92ef623d4831a73c902244035 [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 Banksa7455442006-10-02 02:17:59 -070020/*
21 * This is the RPC server thread function prototype
22 */
23typedef void (*svc_thread_fn)(struct svc_rqst *);
Greg Banks3262c812006-10-02 02:17:58 -070024
25/*
26 *
27 * RPC service thread pool.
28 *
29 * Pool of threads and temporary sockets. Generally there is only
30 * a single one of these per RPC service, but on NUMA machines those
31 * services that can benefit from it (i.e. nfs but not lockd) will
32 * have one pool per NUMA node. This optimisation reduces cross-
33 * node traffic on multi-node NUMA NFS servers.
34 */
35struct svc_pool {
36 unsigned int sp_id; /* pool id; also node id on NUMA */
37 spinlock_t sp_lock; /* protects all fields */
38 struct list_head sp_threads; /* idle server threads */
39 struct list_head sp_sockets; /* pending sockets */
40 unsigned int sp_nrthreads; /* # of threads in pool */
Greg Banksa7455442006-10-02 02:17:59 -070041 struct list_head sp_all_threads; /* all server threads */
Greg Banks3262c812006-10-02 02:17:58 -070042} ____cacheline_aligned_in_smp;
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * RPC service.
46 *
47 * An RPC service is a ``daemon,'' possibly multithreaded, which
48 * receives and processes incoming RPC messages.
49 * It has one or more transport sockets associated with it, and maintains
50 * a list of idle threads waiting for input.
51 *
52 * We currently do not support more than one RPC program per daemon.
53 */
54struct svc_serv {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 struct svc_program * sv_program; /* RPC program */
56 struct svc_stat * sv_stats; /* RPC statistics */
57 spinlock_t sv_lock;
58 unsigned int sv_nrthreads; /* # of server threads */
59 unsigned int sv_bufsz; /* datagram buffer size */
60 unsigned int sv_xdrsize; /* XDR buffer size */
61
62 struct list_head sv_permsocks; /* all permanent sockets */
63 struct list_head sv_tempsocks; /* all temporary sockets */
64 int sv_tmpcnt; /* count of temporary sockets */
Greg Banks36bdfc82006-10-02 02:17:54 -070065 struct timer_list sv_temptimer; /* timer for aging temporary sockets */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 char * sv_name; /* service name */
NeilBrownbc591cc2006-10-02 02:17:44 -070068
Greg Banks3262c812006-10-02 02:17:58 -070069 unsigned int sv_nrpools; /* number of thread pools */
70 struct svc_pool * sv_pools; /* array of thread pools */
71
NeilBrownbc591cc2006-10-02 02:17:44 -070072 void (*sv_shutdown)(struct svc_serv *serv);
73 /* Callback to use when last thread
74 * exits.
75 */
Greg Banksa7455442006-10-02 02:17:59 -070076
77 struct module * sv_module; /* optional module to count when
78 * adding threads */
79 svc_thread_fn sv_function; /* main function for threads */
80 int sv_kill_signal; /* signal to kill threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081};
82
83/*
Greg Banks9a24ab52006-10-02 02:17:58 -070084 * We use sv_nrthreads as a reference count. svc_destroy() drops
85 * this refcount, so we need to bump it up around operations that
86 * change the number of threads. Horrible, but there it is.
87 * Should be called with the BKL held.
88 */
89static inline void svc_get(struct svc_serv *serv)
90{
91 serv->sv_nrthreads++;
92}
93
94/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * Maximum payload size supported by a kernel RPC server.
96 * This is use to determine the max number of pages nfsd is
97 * willing to return in a single READ operation.
98 */
99#define RPCSVC_MAXPAYLOAD (64*1024u)
100
101/*
102 * RPC Requsts and replies are stored in one or more pages.
103 * We maintain an array of pages for each server thread.
104 * Requests are copied into these pages as they arrive. Remaining
105 * pages are available to write the reply into.
106 *
107 * Pages are sent using ->sendpage so each server thread needs to
108 * allocate more to replace those used in sending. To help keep track
109 * of these pages we have a receive list where all pages initialy live,
110 * and a send list where pages are moved to when there are to be part
111 * of a reply.
112 *
113 * We use xdr_buf for holding responses as it fits well with NFS
114 * read responses (that have a header, and some data pages, and possibly
115 * a tail) and means we can share some client side routines.
116 *
117 * The xdr_buf.head kvec always points to the first page in the rq_*pages
118 * list. The xdr_buf.pages pointer points to the second page on that
119 * list. xdr_buf.tail points to the end of the first page.
120 * This assumes that the non-page part of an rpc reply will fit
121 * in a page - NFSd ensures this. lockd also has no trouble.
122 *
123 * Each request/reply pair can have at most one "payload", plus two pages,
124 * one for the request, and one for the reply.
125 */
126#define RPCSVC_MAXPAGES ((RPCSVC_MAXPAYLOAD+PAGE_SIZE-1)/PAGE_SIZE + 2)
127
Alexey Dobriyan76994312006-09-26 22:28:46 -0700128static inline u32 svc_getnl(struct kvec *iov)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Alexey Dobriyan76994312006-09-26 22:28:46 -0700130 __be32 val, *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 vp = iov->iov_base;
132 val = *vp++;
133 iov->iov_base = (void*)vp;
Alexey Dobriyan76994312006-09-26 22:28:46 -0700134 iov->iov_len -= sizeof(__be32);
135 return ntohl(val);
136}
137
138static inline void svc_putnl(struct kvec *iov, u32 val)
139{
140 __be32 *vp = iov->iov_base + iov->iov_len;
141 *vp = htonl(val);
142 iov->iov_len += sizeof(__be32);
143}
144
145static inline __be32 svc_getu32(struct kvec *iov)
146{
147 __be32 val, *vp;
148 vp = iov->iov_base;
149 val = *vp++;
150 iov->iov_base = (void*)vp;
151 iov->iov_len -= sizeof(__be32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return val;
153}
154
155static inline void svc_ungetu32(struct kvec *iov)
156{
Alexey Dobriyan76994312006-09-26 22:28:46 -0700157 __be32 *vp = (__be32 *)iov->iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 iov->iov_base = (void *)(vp - 1);
159 iov->iov_len += sizeof(*vp);
160}
161
Alexey Dobriyan76994312006-09-26 22:28:46 -0700162static inline void svc_putu32(struct kvec *iov, __be32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Alexey Dobriyan76994312006-09-26 22:28:46 -0700164 __be32 *vp = iov->iov_base + iov->iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 *vp = val;
Alexey Dobriyan76994312006-09-26 22:28:46 -0700166 iov->iov_len += sizeof(__be32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169
170/*
171 * The context of a single thread, including the request currently being
172 * processed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 */
174struct svc_rqst {
175 struct list_head rq_list; /* idle list */
Greg Banksa7455442006-10-02 02:17:59 -0700176 struct list_head rq_all; /* all threads list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 struct svc_sock * rq_sock; /* socket */
178 struct sockaddr_in rq_addr; /* peer address */
179 int rq_addrlen;
180
181 struct svc_serv * rq_server; /* RPC service definition */
Greg Banks3262c812006-10-02 02:17:58 -0700182 struct svc_pool * rq_pool; /* thread pool */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 struct svc_procedure * rq_procinfo; /* procedure info */
184 struct auth_ops * rq_authop; /* authentication flavour */
185 struct svc_cred rq_cred; /* auth info */
186 struct sk_buff * rq_skbuff; /* fast recv inet buffer */
187 struct svc_deferred_req*rq_deferred; /* deferred request we are replaying */
188
189 struct xdr_buf rq_arg;
190 struct xdr_buf rq_res;
NeilBrown44524352006-10-04 02:15:46 -0700191 struct page * rq_pages[RPCSVC_MAXPAGES];
192 struct page * *rq_respages; /* points into rq_pages */
193 int rq_resused; /* number of pages used for result */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
NeilBrown3cc03b12006-10-04 02:15:47 -0700195 struct kvec rq_vec[RPCSVC_MAXPAGES]; /* generally useful.. */
196
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700197 __be32 rq_xid; /* transmission id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 u32 rq_prog; /* program number */
199 u32 rq_vers; /* program version */
200 u32 rq_proc; /* procedure number */
201 u32 rq_prot; /* IP protocol */
202 unsigned short
203 rq_secure : 1; /* secure port */
204
205
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700206 __be32 rq_daddr; /* dest addr of request - reply from here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 void * rq_argp; /* decoded arguments */
209 void * rq_resp; /* xdr'd results */
210 void * rq_auth_data; /* flavor-specific data */
211
212 int rq_reserved; /* space on socket outq
213 * reserved for this request
214 */
215
216 struct cache_req rq_chandle; /* handle passed to caches for
217 * request delaying
218 */
219 /* Catering to nfsd */
220 struct auth_domain * rq_client; /* RPC peer info */
221 struct svc_cacherep * rq_cacherep; /* cache info */
222 struct knfsd_fh * rq_reffh; /* Referrence filehandle, used to
223 * determine what device number
224 * to report (real or virtual)
225 */
J. Bruce Fields5c04c462006-06-30 01:56:19 -0700226 int rq_sendfile_ok; /* turned off in gss privacy
227 * to prevent encrypting page
228 * cache pages */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 wait_queue_head_t rq_wait; /* synchronization */
Greg Banksa7455442006-10-02 02:17:59 -0700230 struct task_struct *rq_task; /* service thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231};
232
233/*
234 * Check buffer bounds after decoding arguments
235 */
236static inline int
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700237xdr_argsize_check(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 char *cp = (char *)p;
240 struct kvec *vec = &rqstp->rq_arg.head[0];
NeilBrown0ba75362005-11-07 01:00:26 -0800241 return cp >= (char*)vec->iov_base
242 && cp <= (char*)vec->iov_base + vec->iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245static inline int
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700246xdr_ressize_check(struct svc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 struct kvec *vec = &rqstp->rq_res.head[0];
249 char *cp = (char*)p;
250
251 vec->iov_len = cp - (char*)vec->iov_base;
252
253 return vec->iov_len <= PAGE_SIZE;
254}
255
NeilBrown44524352006-10-04 02:15:46 -0700256static inline void svc_free_res_pages(struct svc_rqst *rqstp)
Andreas Gruenbachera257cdd2005-06-22 17:16:26 +0000257{
NeilBrown44524352006-10-04 02:15:46 -0700258 while (rqstp->rq_resused) {
259 struct page **pp = (rqstp->rq_respages +
260 --rqstp->rq_resused);
261 if (*pp) {
262 put_page(*pp);
263 *pp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265 }
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268struct svc_deferred_req {
269 u32 prot; /* protocol (UDP or TCP) */
270 struct sockaddr_in addr;
271 struct svc_sock *svsk; /* where reply must go */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700272 __be32 daddr; /* where reply must come from */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 struct cache_deferred_req handle;
274 int argslen;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700275 __be32 args[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276};
277
278/*
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +0000279 * List of RPC programs on the same transport endpoint
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 */
281struct svc_program {
Andreas Gruenbacher9ba02632005-06-22 17:16:24 +0000282 struct svc_program * pg_next; /* other programs (same xprt) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 u32 pg_prog; /* program number */
284 unsigned int pg_lovers; /* lowest version */
285 unsigned int pg_hivers; /* lowest version */
286 unsigned int pg_nvers; /* number of versions */
287 struct svc_version ** pg_vers; /* version array */
288 char * pg_name; /* service name */
289 char * pg_class; /* class name: services sharing authentication */
290 struct svc_stat * pg_stats; /* rpc statistics */
291 int (*pg_authenticate)(struct svc_rqst *);
292};
293
294/*
295 * RPC program version
296 */
297struct svc_version {
298 u32 vs_vers; /* version number */
299 u32 vs_nproc; /* number of procedures */
300 struct svc_procedure * vs_proc; /* per-procedure info */
301 u32 vs_xdrsize; /* xdrsize needed for this version */
302
303 /* Override dispatch function (e.g. when caching replies).
304 * A return value of 0 means drop the request.
305 * vs_dispatch == NULL means use default dispatcher.
306 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700307 int (*vs_dispatch)(struct svc_rqst *, __be32 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308};
309
310/*
311 * RPC procedure info
312 */
313typedef int (*svc_procfunc)(struct svc_rqst *, void *argp, void *resp);
314struct svc_procedure {
315 svc_procfunc pc_func; /* process the request */
316 kxdrproc_t pc_decode; /* XDR decode args */
317 kxdrproc_t pc_encode; /* XDR encode result */
318 kxdrproc_t pc_release; /* XDR free result */
319 unsigned int pc_argsize; /* argument struct size */
320 unsigned int pc_ressize; /* result struct size */
321 unsigned int pc_count; /* call count */
322 unsigned int pc_cachetype; /* cache info (NFS) */
323 unsigned int pc_xdrressize; /* maximum size of XDR reply */
324};
325
326/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 * Function prototypes.
328 */
NeilBrownbc591cc2006-10-02 02:17:44 -0700329struct svc_serv * svc_create(struct svc_program *, unsigned int,
330 void (*shutdown)(struct svc_serv*));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331int svc_create_thread(svc_thread_fn, struct svc_serv *);
332void svc_exit_thread(struct svc_rqst *);
Greg Banksa7455442006-10-02 02:17:59 -0700333struct svc_serv * svc_create_pooled(struct svc_program *, unsigned int,
334 void (*shutdown)(struct svc_serv*),
335 svc_thread_fn, int sig, struct module *);
336int svc_set_num_threads(struct svc_serv *, struct svc_pool *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337void svc_destroy(struct svc_serv *);
NeilBrown6fb2b472006-10-02 02:17:50 -0700338int svc_process(struct svc_rqst *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339int svc_register(struct svc_serv *, int, unsigned short);
340void svc_wake_up(struct svc_serv *);
341void svc_reserve(struct svc_rqst *rqstp, int space);
Greg Banksbfd24162006-10-02 02:18:01 -0700342struct svc_pool * svc_pool_for_cpu(struct svc_serv *serv, int cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344#endif /* SUNRPC_SVC_H */