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