1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * linux/include/linux/sunrpc/svc.h
4  *
5  * RPC server declarations.
6  *
7  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
8  */
9 
10 
11 #ifndef SUNRPC_SVC_H
12 #define SUNRPC_SVC_H
13 
14 #include <linux/in.h>
15 #include <linux/in6.h>
16 #include <linux/sunrpc/types.h>
17 #include <linux/sunrpc/xdr.h>
18 #include <linux/sunrpc/auth.h>
19 #include <linux/sunrpc/svcauth.h>
20 #include <linux/wait.h>
21 #include <linux/mm.h>
22 #include <linux/pagevec.h>
23 
24 /*
25  *
26  * RPC service thread pool.
27  *
28  * Pool of threads and temporary sockets.  Generally there is only
29  * a single one of these per RPC service, but on NUMA machines those
30  * services that can benefit from it (i.e. nfs but not lockd) will
31  * have one pool per NUMA node.  This optimisation reduces cross-
32  * node traffic on multi-node NUMA NFS servers.
33  */
34 struct svc_pool {
35 	unsigned int		sp_id;	    	/* pool id; also node id on NUMA */
36 	spinlock_t		sp_lock;	/* protects all fields */
37 	struct list_head	sp_sockets;	/* pending sockets */
38 	unsigned int		sp_nrthreads;	/* # of threads in pool */
39 	struct list_head	sp_all_threads;	/* all server threads */
40 
41 	/* statistics on pool operation */
42 	struct percpu_counter	sp_sockets_queued;
43 	struct percpu_counter	sp_threads_woken;
44 	struct percpu_counter	sp_threads_timedout;
45 
46 #define	SP_TASK_PENDING		(0)		/* still work to do even if no
47 						 * xprt is queued. */
48 #define SP_CONGESTED		(1)
49 	unsigned long		sp_flags;
50 } ____cacheline_aligned_in_smp;
51 
52 /*
53  * RPC service.
54  *
55  * An RPC service is a ``daemon,'' possibly multithreaded, which
56  * receives and processes incoming RPC messages.
57  * It has one or more transport sockets associated with it, and maintains
58  * a list of idle threads waiting for input.
59  *
60  * We currently do not support more than one RPC program per daemon.
61  */
62 struct svc_serv {
63 	struct svc_program *	sv_program;	/* RPC program */
64 	struct svc_stat *	sv_stats;	/* RPC statistics */
65 	spinlock_t		sv_lock;
66 	struct kref		sv_refcnt;
67 	unsigned int		sv_nrthreads;	/* # of server threads */
68 	unsigned int		sv_maxconn;	/* max connections allowed or
69 						 * '0' causing max to be based
70 						 * on number of threads. */
71 
72 	unsigned int		sv_max_payload;	/* datagram payload size */
73 	unsigned int		sv_max_mesg;	/* max_payload + 1 page for overheads */
74 	unsigned int		sv_xdrsize;	/* XDR buffer size */
75 	struct list_head	sv_permsocks;	/* all permanent sockets */
76 	struct list_head	sv_tempsocks;	/* all temporary sockets */
77 	int			sv_tmpcnt;	/* count of temporary sockets */
78 	struct timer_list	sv_temptimer;	/* timer for aging temporary sockets */
79 
80 	char *			sv_name;	/* service name */
81 
82 	unsigned int		sv_nrpools;	/* number of thread pools */
83 	struct svc_pool *	sv_pools;	/* array of thread pools */
84 	int			(*sv_threadfn)(void *data);
85 
86 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
87 	struct list_head	sv_cb_list;	/* queue for callback requests
88 						 * that arrive over the same
89 						 * connection */
90 	spinlock_t		sv_cb_lock;	/* protects the svc_cb_list */
91 	wait_queue_head_t	sv_cb_waitq;	/* sleep here if there are no
92 						 * entries in the svc_cb_list */
93 	bool			sv_bc_enabled;	/* service uses backchannel */
94 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
95 };
96 
97 /**
98  * svc_get() - increment reference count on a SUNRPC serv
99  * @serv:  the svc_serv to have count incremented
100  *
101  * Returns: the svc_serv that was passed in.
102  */
svc_get(struct svc_serv * serv)103 static inline struct svc_serv *svc_get(struct svc_serv *serv)
104 {
105 	kref_get(&serv->sv_refcnt);
106 	return serv;
107 }
108 
109 void svc_destroy(struct kref *);
110 
111 /**
112  * svc_put - decrement reference count on a SUNRPC serv
113  * @serv:  the svc_serv to have count decremented
114  *
115  * When the reference count reaches zero, svc_destroy()
116  * is called to clean up and free the serv.
117  */
svc_put(struct svc_serv * serv)118 static inline void svc_put(struct svc_serv *serv)
119 {
120 	kref_put(&serv->sv_refcnt, svc_destroy);
121 }
122 
123 /**
124  * svc_put_not_last - decrement non-final reference count on SUNRPC serv
125  * @serv:  the svc_serv to have count decremented
126  *
127  * Returns: %true is refcount was decremented.
128  *
129  * If the refcount is 1, it is not decremented and instead failure is reported.
130  */
svc_put_not_last(struct svc_serv * serv)131 static inline bool svc_put_not_last(struct svc_serv *serv)
132 {
133 	return refcount_dec_not_one(&serv->sv_refcnt.refcount);
134 }
135 
136 /*
137  * Maximum payload size supported by a kernel RPC server.
138  * This is use to determine the max number of pages nfsd is
139  * willing to return in a single READ operation.
140  *
141  * These happen to all be powers of 2, which is not strictly
142  * necessary but helps enforce the real limitation, which is
143  * that they should be multiples of PAGE_SIZE.
144  *
145  * For UDP transports, a block plus NFS,RPC, and UDP headers
146  * has to fit into the IP datagram limit of 64K.  The largest
147  * feasible number for all known page sizes is probably 48K,
148  * but we choose 32K here.  This is the same as the historical
149  * Linux limit; someone who cares more about NFS/UDP performance
150  * can test a larger number.
151  *
152  * For TCP transports we have more freedom.  A size of 1MB is
153  * chosen to match the client limit.  Other OSes are known to
154  * have larger limits, but those numbers are probably beyond
155  * the point of diminishing returns.
156  */
157 #define RPCSVC_MAXPAYLOAD	(1*1024*1024u)
158 #define RPCSVC_MAXPAYLOAD_TCP	RPCSVC_MAXPAYLOAD
159 #define RPCSVC_MAXPAYLOAD_UDP	(32*1024u)
160 
161 extern u32 svc_max_payload(const struct svc_rqst *rqstp);
162 
163 /*
164  * RPC Requsts and replies are stored in one or more pages.
165  * We maintain an array of pages for each server thread.
166  * Requests are copied into these pages as they arrive.  Remaining
167  * pages are available to write the reply into.
168  *
169  * Pages are sent using ->sendpage so each server thread needs to
170  * allocate more to replace those used in sending.  To help keep track
171  * of these pages we have a receive list where all pages initialy live,
172  * and a send list where pages are moved to when there are to be part
173  * of a reply.
174  *
175  * We use xdr_buf for holding responses as it fits well with NFS
176  * read responses (that have a header, and some data pages, and possibly
177  * a tail) and means we can share some client side routines.
178  *
179  * The xdr_buf.head kvec always points to the first page in the rq_*pages
180  * list.  The xdr_buf.pages pointer points to the second page on that
181  * list.  xdr_buf.tail points to the end of the first page.
182  * This assumes that the non-page part of an rpc reply will fit
183  * in a page - NFSd ensures this.  lockd also has no trouble.
184  *
185  * Each request/reply pair can have at most one "payload", plus two pages,
186  * one for the request, and one for the reply.
187  * We using ->sendfile to return read data, we might need one extra page
188  * if the request is not page-aligned.  So add another '1'.
189  */
190 #define RPCSVC_MAXPAGES		((RPCSVC_MAXPAYLOAD+PAGE_SIZE-1)/PAGE_SIZE \
191 				+ 2 + 1)
192 
193 /*
194  * The context of a single thread, including the request currently being
195  * processed.
196  */
197 struct svc_rqst {
198 	struct list_head	rq_all;		/* all threads list */
199 	struct rcu_head		rq_rcu_head;	/* for RCU deferred kfree */
200 	struct svc_xprt *	rq_xprt;	/* transport ptr */
201 
202 	struct sockaddr_storage	rq_addr;	/* peer address */
203 	size_t			rq_addrlen;
204 	struct sockaddr_storage	rq_daddr;	/* dest addr of request
205 						 *  - reply from here */
206 	size_t			rq_daddrlen;
207 
208 	struct svc_serv *	rq_server;	/* RPC service definition */
209 	struct svc_pool *	rq_pool;	/* thread pool */
210 	const struct svc_procedure *rq_procinfo;/* procedure info */
211 	struct auth_ops *	rq_authop;	/* authentication flavour */
212 	struct svc_cred		rq_cred;	/* auth info */
213 	void *			rq_xprt_ctxt;	/* transport specific context ptr */
214 	struct svc_deferred_req*rq_deferred;	/* deferred request we are replaying */
215 
216 	struct xdr_buf		rq_arg;
217 	struct xdr_stream	rq_arg_stream;
218 	struct xdr_stream	rq_res_stream;
219 	struct page		*rq_scratch_page;
220 	struct xdr_buf		rq_res;
221 	struct page		*rq_pages[RPCSVC_MAXPAGES + 1];
222 	struct page *		*rq_respages;	/* points into rq_pages */
223 	struct page *		*rq_next_page; /* next reply page to use */
224 	struct page *		*rq_page_end;  /* one past the last page */
225 
226 	struct pagevec		rq_pvec;
227 	struct kvec		rq_vec[RPCSVC_MAXPAGES]; /* generally useful.. */
228 	struct bio_vec		rq_bvec[RPCSVC_MAXPAGES];
229 
230 	__be32			rq_xid;		/* transmission id */
231 	u32			rq_prog;	/* program number */
232 	u32			rq_vers;	/* program version */
233 	u32			rq_proc;	/* procedure number */
234 	u32			rq_prot;	/* IP protocol */
235 	int			rq_cachetype;	/* catering to nfsd */
236 #define	RQ_SECURE	(0)			/* secure port */
237 #define	RQ_LOCAL	(1)			/* local request */
238 #define	RQ_USEDEFERRAL	(2)			/* use deferral */
239 #define	RQ_DROPME	(3)			/* drop current reply */
240 #define	RQ_SPLICE_OK	(4)			/* turned off in gss privacy
241 						 * to prevent encrypting page
242 						 * cache pages */
243 #define	RQ_VICTIM	(5)			/* about to be shut down */
244 #define	RQ_BUSY		(6)			/* request is busy */
245 #define	RQ_DATA		(7)			/* request has data */
246 	unsigned long		rq_flags;	/* flags field */
247 	ktime_t			rq_qtime;	/* enqueue time */
248 
249 	void *			rq_argp;	/* decoded arguments */
250 	void *			rq_resp;	/* xdr'd results */
251 	__be32			*rq_accept_statp;
252 	void *			rq_auth_data;	/* flavor-specific data */
253 	__be32			rq_auth_stat;	/* authentication status */
254 	int			rq_auth_slack;	/* extra space xdr code
255 						 * should leave in head
256 						 * for krb5i, krb5p.
257 						 */
258 	int			rq_reserved;	/* space on socket outq
259 						 * reserved for this request
260 						 */
261 	ktime_t			rq_stime;	/* start time */
262 
263 	struct cache_req	rq_chandle;	/* handle passed to caches for
264 						 * request delaying
265 						 */
266 	/* Catering to nfsd */
267 	struct auth_domain *	rq_client;	/* RPC peer info */
268 	struct auth_domain *	rq_gssclient;	/* "gss/"-style peer info */
269 	struct svc_cacherep *	rq_cacherep;	/* cache info */
270 	struct task_struct	*rq_task;	/* service thread */
271 	struct net		*rq_bc_net;	/* pointer to backchannel's
272 						 * net namespace
273 						 */
274 	void **			rq_lease_breaker; /* The v4 client breaking a lease */
275 };
276 
277 #define SVC_NET(rqst) (rqst->rq_xprt ? rqst->rq_xprt->xpt_net : rqst->rq_bc_net)
278 
279 /*
280  * Rigorous type checking on sockaddr type conversions
281  */
svc_addr_in(const struct svc_rqst * rqst)282 static inline struct sockaddr_in *svc_addr_in(const struct svc_rqst *rqst)
283 {
284 	return (struct sockaddr_in *) &rqst->rq_addr;
285 }
286 
svc_addr_in6(const struct svc_rqst * rqst)287 static inline struct sockaddr_in6 *svc_addr_in6(const struct svc_rqst *rqst)
288 {
289 	return (struct sockaddr_in6 *) &rqst->rq_addr;
290 }
291 
svc_addr(const struct svc_rqst * rqst)292 static inline struct sockaddr *svc_addr(const struct svc_rqst *rqst)
293 {
294 	return (struct sockaddr *) &rqst->rq_addr;
295 }
296 
svc_daddr_in(const struct svc_rqst * rqst)297 static inline struct sockaddr_in *svc_daddr_in(const struct svc_rqst *rqst)
298 {
299 	return (struct sockaddr_in *) &rqst->rq_daddr;
300 }
301 
svc_daddr_in6(const struct svc_rqst * rqst)302 static inline struct sockaddr_in6 *svc_daddr_in6(const struct svc_rqst *rqst)
303 {
304 	return (struct sockaddr_in6 *) &rqst->rq_daddr;
305 }
306 
svc_daddr(const struct svc_rqst * rqst)307 static inline struct sockaddr *svc_daddr(const struct svc_rqst *rqst)
308 {
309 	return (struct sockaddr *) &rqst->rq_daddr;
310 }
311 
svc_free_res_pages(struct svc_rqst * rqstp)312 static inline void svc_free_res_pages(struct svc_rqst *rqstp)
313 {
314 	while (rqstp->rq_next_page != rqstp->rq_respages) {
315 		struct page **pp = --rqstp->rq_next_page;
316 		if (*pp) {
317 			put_page(*pp);
318 			*pp = NULL;
319 		}
320 	}
321 }
322 
323 struct svc_deferred_req {
324 	u32			prot;	/* protocol (UDP or TCP) */
325 	struct svc_xprt		*xprt;
326 	struct sockaddr_storage	addr;	/* where reply must go */
327 	size_t			addrlen;
328 	struct sockaddr_storage	daddr;	/* where reply must come from */
329 	size_t			daddrlen;
330 	void			*xprt_ctxt;
331 	struct cache_deferred_req handle;
332 	int			argslen;
333 	__be32			args[];
334 };
335 
336 struct svc_process_info {
337 	union {
338 		int  (*dispatch)(struct svc_rqst *rqstp);
339 		struct {
340 			unsigned int lovers;
341 			unsigned int hivers;
342 		} mismatch;
343 	};
344 };
345 
346 /*
347  * List of RPC programs on the same transport endpoint
348  */
349 struct svc_program {
350 	struct svc_program *	pg_next;	/* other programs (same xprt) */
351 	u32			pg_prog;	/* program number */
352 	unsigned int		pg_lovers;	/* lowest version */
353 	unsigned int		pg_hivers;	/* highest version */
354 	unsigned int		pg_nvers;	/* number of versions */
355 	const struct svc_version **pg_vers;	/* version array */
356 	char *			pg_name;	/* service name */
357 	char *			pg_class;	/* class name: services sharing authentication */
358 	struct svc_stat *	pg_stats;	/* rpc statistics */
359 	int			(*pg_authenticate)(struct svc_rqst *);
360 	__be32			(*pg_init_request)(struct svc_rqst *,
361 						   const struct svc_program *,
362 						   struct svc_process_info *);
363 	int			(*pg_rpcbind_set)(struct net *net,
364 						  const struct svc_program *,
365 						  u32 version, int family,
366 						  unsigned short proto,
367 						  unsigned short port);
368 };
369 
370 /*
371  * RPC program version
372  */
373 struct svc_version {
374 	u32			vs_vers;	/* version number */
375 	u32			vs_nproc;	/* number of procedures */
376 	const struct svc_procedure *vs_proc;	/* per-procedure info */
377 	unsigned long __percpu	*vs_count;	/* call counts */
378 	u32			vs_xdrsize;	/* xdrsize needed for this version */
379 
380 	/* Don't register with rpcbind */
381 	bool			vs_hidden;
382 
383 	/* Don't care if the rpcbind registration fails */
384 	bool			vs_rpcb_optnl;
385 
386 	/* Need xprt with congestion control */
387 	bool			vs_need_cong_ctrl;
388 
389 	/* Dispatch function */
390 	int			(*vs_dispatch)(struct svc_rqst *rqstp);
391 };
392 
393 /*
394  * RPC procedure info
395  */
396 struct svc_procedure {
397 	/* process the request: */
398 	__be32			(*pc_func)(struct svc_rqst *);
399 	/* XDR decode args: */
400 	bool			(*pc_decode)(struct svc_rqst *rqstp,
401 					     struct xdr_stream *xdr);
402 	/* XDR encode result: */
403 	bool			(*pc_encode)(struct svc_rqst *rqstp,
404 					     struct xdr_stream *xdr);
405 	/* XDR free result: */
406 	void			(*pc_release)(struct svc_rqst *);
407 	unsigned int		pc_argsize;	/* argument struct size */
408 	unsigned int		pc_argzero;	/* how much of argument to clear */
409 	unsigned int		pc_ressize;	/* result struct size */
410 	unsigned int		pc_cachetype;	/* cache info (NFS) */
411 	unsigned int		pc_xdrressize;	/* maximum size of XDR reply */
412 	const char *		pc_name;	/* for display */
413 };
414 
415 /*
416  * Function prototypes.
417  */
418 int svc_rpcb_setup(struct svc_serv *serv, struct net *net);
419 void svc_rpcb_cleanup(struct svc_serv *serv, struct net *net);
420 int svc_bind(struct svc_serv *serv, struct net *net);
421 struct svc_serv *svc_create(struct svc_program *, unsigned int,
422 			    int (*threadfn)(void *data));
423 struct svc_rqst *svc_rqst_alloc(struct svc_serv *serv,
424 					struct svc_pool *pool, int node);
425 void		   svc_rqst_replace_page(struct svc_rqst *rqstp,
426 					 struct page *page);
427 void		   svc_rqst_free(struct svc_rqst *);
428 void		   svc_exit_thread(struct svc_rqst *);
429 struct svc_serv *  svc_create_pooled(struct svc_program *, unsigned int,
430 				     int (*threadfn)(void *data));
431 int		   svc_set_num_threads(struct svc_serv *, struct svc_pool *, int);
432 int		   svc_pool_stats_open(struct svc_serv *serv, struct file *file);
433 int		   svc_process(struct svc_rqst *);
434 int		   bc_svc_process(struct svc_serv *, struct rpc_rqst *,
435 			struct svc_rqst *);
436 int		   svc_register(const struct svc_serv *, struct net *, const int,
437 				const unsigned short, const unsigned short);
438 
439 void		   svc_wake_up(struct svc_serv *);
440 void		   svc_reserve(struct svc_rqst *rqstp, int space);
441 struct svc_pool   *svc_pool_for_cpu(struct svc_serv *serv);
442 char *		   svc_print_addr(struct svc_rqst *, char *, size_t);
443 const char *	   svc_proc_name(const struct svc_rqst *rqstp);
444 int		   svc_encode_result_payload(struct svc_rqst *rqstp,
445 					     unsigned int offset,
446 					     unsigned int length);
447 unsigned int	   svc_fill_write_vector(struct svc_rqst *rqstp,
448 					 struct xdr_buf *payload);
449 char		  *svc_fill_symlink_pathname(struct svc_rqst *rqstp,
450 					     struct kvec *first, void *p,
451 					     size_t total);
452 __be32		   svc_generic_init_request(struct svc_rqst *rqstp,
453 					    const struct svc_program *progp,
454 					    struct svc_process_info *procinfo);
455 int		   svc_generic_rpcbind_set(struct net *net,
456 					   const struct svc_program *progp,
457 					   u32 version, int family,
458 					   unsigned short proto,
459 					   unsigned short port);
460 int		   svc_rpcbind_set_version(struct net *net,
461 					   const struct svc_program *progp,
462 					   u32 version, int family,
463 					   unsigned short proto,
464 					   unsigned short port);
465 
466 #define	RPC_MAX_ADDRBUFLEN	(63U)
467 
468 /*
469  * When we want to reduce the size of the reserved space in the response
470  * buffer, we need to take into account the size of any checksum data that
471  * may be at the end of the packet. This is difficult to determine exactly
472  * for all cases without actually generating the checksum, so we just use a
473  * static value.
474  */
svc_reserve_auth(struct svc_rqst * rqstp,int space)475 static inline void svc_reserve_auth(struct svc_rqst *rqstp, int space)
476 {
477 	svc_reserve(rqstp, space + rqstp->rq_auth_slack);
478 }
479 
480 /**
481  * svcxdr_init_decode - Prepare an xdr_stream for Call decoding
482  * @rqstp: controlling server RPC transaction context
483  *
484  */
svcxdr_init_decode(struct svc_rqst * rqstp)485 static inline void svcxdr_init_decode(struct svc_rqst *rqstp)
486 {
487 	struct xdr_stream *xdr = &rqstp->rq_arg_stream;
488 	struct xdr_buf *buf = &rqstp->rq_arg;
489 	struct kvec *argv = buf->head;
490 
491 	WARN_ON(buf->len != buf->head->iov_len + buf->page_len + buf->tail->iov_len);
492 	buf->len = buf->head->iov_len + buf->page_len + buf->tail->iov_len;
493 
494 	xdr_init_decode(xdr, buf, argv->iov_base, NULL);
495 	xdr_set_scratch_page(xdr, rqstp->rq_scratch_page);
496 }
497 
498 /**
499  * svcxdr_init_encode - Prepare an xdr_stream for svc Reply encoding
500  * @rqstp: controlling server RPC transaction context
501  *
502  */
svcxdr_init_encode(struct svc_rqst * rqstp)503 static inline void svcxdr_init_encode(struct svc_rqst *rqstp)
504 {
505 	struct xdr_stream *xdr = &rqstp->rq_res_stream;
506 	struct xdr_buf *buf = &rqstp->rq_res;
507 	struct kvec *resv = buf->head;
508 
509 	xdr_reset_scratch_buffer(xdr);
510 
511 	xdr->buf = buf;
512 	xdr->iov = resv;
513 	xdr->p   = resv->iov_base + resv->iov_len;
514 	xdr->end = resv->iov_base + PAGE_SIZE;
515 	buf->len = resv->iov_len;
516 	xdr->page_ptr = buf->pages - 1;
517 	buf->buflen = PAGE_SIZE * (rqstp->rq_page_end - buf->pages);
518 	xdr->rqst = NULL;
519 }
520 
521 /**
522  * svcxdr_set_auth_slack -
523  * @rqstp: RPC transaction
524  * @slack: buffer space to reserve for the transaction's security flavor
525  *
526  * Set the request's slack space requirement, and set aside that much
527  * space in the rqstp's rq_res.head for use when the auth wraps the Reply.
528  */
svcxdr_set_auth_slack(struct svc_rqst * rqstp,int slack)529 static inline void svcxdr_set_auth_slack(struct svc_rqst *rqstp, int slack)
530 {
531 	struct xdr_stream *xdr = &rqstp->rq_res_stream;
532 	struct xdr_buf *buf = &rqstp->rq_res;
533 	struct kvec *resv = buf->head;
534 
535 	rqstp->rq_auth_slack = slack;
536 
537 	xdr->end -= XDR_QUADLEN(slack);
538 	buf->buflen -= rqstp->rq_auth_slack;
539 
540 	WARN_ON(xdr->iov != resv);
541 	WARN_ON(xdr->p > xdr->end);
542 }
543 
544 /**
545  * svcxdr_set_accept_stat - Reserve space for the accept_stat field
546  * @rqstp: RPC transaction context
547  *
548  * Return values:
549  *   %true: Success
550  *   %false: No response buffer space was available
551  */
svcxdr_set_accept_stat(struct svc_rqst * rqstp)552 static inline bool svcxdr_set_accept_stat(struct svc_rqst *rqstp)
553 {
554 	struct xdr_stream *xdr = &rqstp->rq_res_stream;
555 
556 	rqstp->rq_accept_statp = xdr_reserve_space(xdr, XDR_UNIT);
557 	if (unlikely(!rqstp->rq_accept_statp))
558 		return false;
559 	*rqstp->rq_accept_statp = rpc_success;
560 	return true;
561 }
562 
563 #endif /* SUNRPC_SVC_H */
564