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 /* 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 */ 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 */ 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 */ 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 Requests 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 ->sendmsg with MSG_SPLICE_PAGES so each server thread 170 * needs to 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, and a 172 * send list where pages are moved to when there are to be part of a reply. 173 * 174 * We use xdr_buf for holding responses as it fits well with NFS 175 * read responses (that have a header, and some data pages, and possibly 176 * a tail) and means we can share some client side routines. 177 * 178 * The xdr_buf.head kvec always points to the first page in the rq_*pages 179 * list. The xdr_buf.pages pointer points to the second page on that 180 * list. xdr_buf.tail points to the end of the first page. 181 * This assumes that the non-page part of an rpc reply will fit 182 * in a page - NFSd ensures this. lockd also has no trouble. 183 * 184 * Each request/reply pair can have at most one "payload", plus two pages, 185 * one for the request, and one for the reply. 186 * We using ->sendfile to return read data, we might need one extra page 187 * if the request is not page-aligned. So add another '1'. 188 */ 189 #define RPCSVC_MAXPAGES ((RPCSVC_MAXPAYLOAD+PAGE_SIZE-1)/PAGE_SIZE \ 190 + 2 + 1) 191 192 /* 193 * The context of a single thread, including the request currently being 194 * processed. 195 */ 196 struct svc_rqst { 197 struct list_head rq_all; /* all threads list */ 198 struct rcu_head rq_rcu_head; /* for RCU deferred kfree */ 199 struct svc_xprt * rq_xprt; /* transport ptr */ 200 201 struct sockaddr_storage rq_addr; /* peer address */ 202 size_t rq_addrlen; 203 struct sockaddr_storage rq_daddr; /* dest addr of request 204 * - reply from here */ 205 size_t rq_daddrlen; 206 207 struct svc_serv * rq_server; /* RPC service definition */ 208 struct svc_pool * rq_pool; /* thread pool */ 209 const struct svc_procedure *rq_procinfo;/* procedure info */ 210 struct auth_ops * rq_authop; /* authentication flavour */ 211 struct svc_cred rq_cred; /* auth info */ 212 void * rq_xprt_ctxt; /* transport specific context ptr */ 213 struct svc_deferred_req*rq_deferred; /* deferred request we are replaying */ 214 215 struct xdr_buf rq_arg; 216 struct xdr_stream rq_arg_stream; 217 struct xdr_stream rq_res_stream; 218 struct page *rq_scratch_page; 219 struct xdr_buf rq_res; 220 struct page *rq_pages[RPCSVC_MAXPAGES + 1]; 221 struct page * *rq_respages; /* points into rq_pages */ 222 struct page * *rq_next_page; /* next reply page to use */ 223 struct page * *rq_page_end; /* one past the last page */ 224 225 struct folio_batch rq_fbatch; 226 struct kvec rq_vec[RPCSVC_MAXPAGES]; /* generally useful.. */ 227 struct bio_vec rq_bvec[RPCSVC_MAXPAGES]; 228 229 __be32 rq_xid; /* transmission id */ 230 u32 rq_prog; /* program number */ 231 u32 rq_vers; /* program version */ 232 u32 rq_proc; /* procedure number */ 233 u32 rq_prot; /* IP protocol */ 234 int rq_cachetype; /* catering to nfsd */ 235 #define RQ_SECURE (0) /* secure port */ 236 #define RQ_LOCAL (1) /* local request */ 237 #define RQ_USEDEFERRAL (2) /* use deferral */ 238 #define RQ_DROPME (3) /* drop current reply */ 239 #define RQ_SPLICE_OK (4) /* turned off in gss privacy 240 * to prevent encrypting page 241 * cache pages */ 242 #define RQ_VICTIM (5) /* about to be shut down */ 243 #define RQ_BUSY (6) /* request is busy */ 244 #define RQ_DATA (7) /* request has data */ 245 unsigned long rq_flags; /* flags field */ 246 ktime_t rq_qtime; /* enqueue time */ 247 248 void * rq_argp; /* decoded arguments */ 249 void * rq_resp; /* xdr'd results */ 250 __be32 *rq_accept_statp; 251 void * rq_auth_data; /* flavor-specific data */ 252 __be32 rq_auth_stat; /* authentication status */ 253 int rq_auth_slack; /* extra space xdr code 254 * should leave in head 255 * for krb5i, krb5p. 256 */ 257 int rq_reserved; /* space on socket outq 258 * reserved for this request 259 */ 260 ktime_t rq_stime; /* start time */ 261 262 struct cache_req rq_chandle; /* handle passed to caches for 263 * request delaying 264 */ 265 /* Catering to nfsd */ 266 struct auth_domain * rq_client; /* RPC peer info */ 267 struct auth_domain * rq_gssclient; /* "gss/"-style peer info */ 268 struct svc_cacherep * rq_cacherep; /* cache info */ 269 struct task_struct *rq_task; /* service thread */ 270 struct net *rq_bc_net; /* pointer to backchannel's 271 * net namespace 272 */ 273 void ** rq_lease_breaker; /* The v4 client breaking a lease */ 274 }; 275 276 #define SVC_NET(rqst) (rqst->rq_xprt ? rqst->rq_xprt->xpt_net : rqst->rq_bc_net) 277 278 /* 279 * Rigorous type checking on sockaddr type conversions 280 */ 281 static inline struct sockaddr_in *svc_addr_in(const struct svc_rqst *rqst) 282 { 283 return (struct sockaddr_in *) &rqst->rq_addr; 284 } 285 286 static inline struct sockaddr_in6 *svc_addr_in6(const struct svc_rqst *rqst) 287 { 288 return (struct sockaddr_in6 *) &rqst->rq_addr; 289 } 290 291 static inline struct sockaddr *svc_addr(const struct svc_rqst *rqst) 292 { 293 return (struct sockaddr *) &rqst->rq_addr; 294 } 295 296 static inline struct sockaddr_in *svc_daddr_in(const struct svc_rqst *rqst) 297 { 298 return (struct sockaddr_in *) &rqst->rq_daddr; 299 } 300 301 static inline struct sockaddr_in6 *svc_daddr_in6(const struct svc_rqst *rqst) 302 { 303 return (struct sockaddr_in6 *) &rqst->rq_daddr; 304 } 305 306 static inline struct sockaddr *svc_daddr(const struct svc_rqst *rqst) 307 { 308 return (struct sockaddr *) &rqst->rq_daddr; 309 } 310 311 struct svc_deferred_req { 312 u32 prot; /* protocol (UDP or TCP) */ 313 struct svc_xprt *xprt; 314 struct sockaddr_storage addr; /* where reply must go */ 315 size_t addrlen; 316 struct sockaddr_storage daddr; /* where reply must come from */ 317 size_t daddrlen; 318 void *xprt_ctxt; 319 struct cache_deferred_req handle; 320 int argslen; 321 __be32 args[]; 322 }; 323 324 struct svc_process_info { 325 union { 326 int (*dispatch)(struct svc_rqst *rqstp); 327 struct { 328 unsigned int lovers; 329 unsigned int hivers; 330 } mismatch; 331 }; 332 }; 333 334 /* 335 * List of RPC programs on the same transport endpoint 336 */ 337 struct svc_program { 338 struct svc_program * pg_next; /* other programs (same xprt) */ 339 u32 pg_prog; /* program number */ 340 unsigned int pg_lovers; /* lowest version */ 341 unsigned int pg_hivers; /* highest version */ 342 unsigned int pg_nvers; /* number of versions */ 343 const struct svc_version **pg_vers; /* version array */ 344 char * pg_name; /* service name */ 345 char * pg_class; /* class name: services sharing authentication */ 346 struct svc_stat * pg_stats; /* rpc statistics */ 347 int (*pg_authenticate)(struct svc_rqst *); 348 __be32 (*pg_init_request)(struct svc_rqst *, 349 const struct svc_program *, 350 struct svc_process_info *); 351 int (*pg_rpcbind_set)(struct net *net, 352 const struct svc_program *, 353 u32 version, int family, 354 unsigned short proto, 355 unsigned short port); 356 }; 357 358 /* 359 * RPC program version 360 */ 361 struct svc_version { 362 u32 vs_vers; /* version number */ 363 u32 vs_nproc; /* number of procedures */ 364 const struct svc_procedure *vs_proc; /* per-procedure info */ 365 unsigned long __percpu *vs_count; /* call counts */ 366 u32 vs_xdrsize; /* xdrsize needed for this version */ 367 368 /* Don't register with rpcbind */ 369 bool vs_hidden; 370 371 /* Don't care if the rpcbind registration fails */ 372 bool vs_rpcb_optnl; 373 374 /* Need xprt with congestion control */ 375 bool vs_need_cong_ctrl; 376 377 /* Dispatch function */ 378 int (*vs_dispatch)(struct svc_rqst *rqstp); 379 }; 380 381 /* 382 * RPC procedure info 383 */ 384 struct svc_procedure { 385 /* process the request: */ 386 __be32 (*pc_func)(struct svc_rqst *); 387 /* XDR decode args: */ 388 bool (*pc_decode)(struct svc_rqst *rqstp, 389 struct xdr_stream *xdr); 390 /* XDR encode result: */ 391 bool (*pc_encode)(struct svc_rqst *rqstp, 392 struct xdr_stream *xdr); 393 /* XDR free result: */ 394 void (*pc_release)(struct svc_rqst *); 395 unsigned int pc_argsize; /* argument struct size */ 396 unsigned int pc_argzero; /* how much of argument to clear */ 397 unsigned int pc_ressize; /* result struct size */ 398 unsigned int pc_cachetype; /* cache info (NFS) */ 399 unsigned int pc_xdrressize; /* maximum size of XDR reply */ 400 const char * pc_name; /* for display */ 401 }; 402 403 /* 404 * Function prototypes. 405 */ 406 int svc_rpcb_setup(struct svc_serv *serv, struct net *net); 407 void svc_rpcb_cleanup(struct svc_serv *serv, struct net *net); 408 int svc_bind(struct svc_serv *serv, struct net *net); 409 struct svc_serv *svc_create(struct svc_program *, unsigned int, 410 int (*threadfn)(void *data)); 411 struct svc_rqst *svc_rqst_alloc(struct svc_serv *serv, 412 struct svc_pool *pool, int node); 413 bool svc_rqst_replace_page(struct svc_rqst *rqstp, 414 struct page *page); 415 void svc_rqst_release_pages(struct svc_rqst *rqstp); 416 void svc_rqst_free(struct svc_rqst *); 417 void svc_exit_thread(struct svc_rqst *); 418 struct svc_serv * svc_create_pooled(struct svc_program *, unsigned int, 419 int (*threadfn)(void *data)); 420 int svc_set_num_threads(struct svc_serv *, struct svc_pool *, int); 421 int svc_pool_stats_open(struct svc_serv *serv, struct file *file); 422 void svc_process(struct svc_rqst *rqstp); 423 int bc_svc_process(struct svc_serv *, struct rpc_rqst *, 424 struct svc_rqst *); 425 int svc_register(const struct svc_serv *, struct net *, const int, 426 const unsigned short, const unsigned short); 427 428 void svc_wake_up(struct svc_serv *); 429 void svc_reserve(struct svc_rqst *rqstp, int space); 430 struct svc_pool *svc_pool_for_cpu(struct svc_serv *serv); 431 char * svc_print_addr(struct svc_rqst *, char *, size_t); 432 const char * svc_proc_name(const struct svc_rqst *rqstp); 433 int svc_encode_result_payload(struct svc_rqst *rqstp, 434 unsigned int offset, 435 unsigned int length); 436 unsigned int svc_fill_write_vector(struct svc_rqst *rqstp, 437 struct xdr_buf *payload); 438 char *svc_fill_symlink_pathname(struct svc_rqst *rqstp, 439 struct kvec *first, void *p, 440 size_t total); 441 __be32 svc_generic_init_request(struct svc_rqst *rqstp, 442 const struct svc_program *progp, 443 struct svc_process_info *procinfo); 444 int svc_generic_rpcbind_set(struct net *net, 445 const struct svc_program *progp, 446 u32 version, int family, 447 unsigned short proto, 448 unsigned short port); 449 int svc_rpcbind_set_version(struct net *net, 450 const struct svc_program *progp, 451 u32 version, int family, 452 unsigned short proto, 453 unsigned short port); 454 455 #define RPC_MAX_ADDRBUFLEN (63U) 456 457 /* 458 * When we want to reduce the size of the reserved space in the response 459 * buffer, we need to take into account the size of any checksum data that 460 * may be at the end of the packet. This is difficult to determine exactly 461 * for all cases without actually generating the checksum, so we just use a 462 * static value. 463 */ 464 static inline void svc_reserve_auth(struct svc_rqst *rqstp, int space) 465 { 466 svc_reserve(rqstp, space + rqstp->rq_auth_slack); 467 } 468 469 /** 470 * svcxdr_init_decode - Prepare an xdr_stream for Call decoding 471 * @rqstp: controlling server RPC transaction context 472 * 473 */ 474 static inline void svcxdr_init_decode(struct svc_rqst *rqstp) 475 { 476 struct xdr_stream *xdr = &rqstp->rq_arg_stream; 477 struct xdr_buf *buf = &rqstp->rq_arg; 478 struct kvec *argv = buf->head; 479 480 WARN_ON(buf->len != buf->head->iov_len + buf->page_len + buf->tail->iov_len); 481 buf->len = buf->head->iov_len + buf->page_len + buf->tail->iov_len; 482 483 xdr_init_decode(xdr, buf, argv->iov_base, NULL); 484 xdr_set_scratch_page(xdr, rqstp->rq_scratch_page); 485 } 486 487 /** 488 * svcxdr_init_encode - Prepare an xdr_stream for svc Reply encoding 489 * @rqstp: controlling server RPC transaction context 490 * 491 */ 492 static inline void svcxdr_init_encode(struct svc_rqst *rqstp) 493 { 494 struct xdr_stream *xdr = &rqstp->rq_res_stream; 495 struct xdr_buf *buf = &rqstp->rq_res; 496 struct kvec *resv = buf->head; 497 498 xdr_reset_scratch_buffer(xdr); 499 500 xdr->buf = buf; 501 xdr->iov = resv; 502 xdr->p = resv->iov_base + resv->iov_len; 503 xdr->end = resv->iov_base + PAGE_SIZE; 504 buf->len = resv->iov_len; 505 xdr->page_ptr = buf->pages - 1; 506 buf->buflen = PAGE_SIZE * (rqstp->rq_page_end - buf->pages); 507 xdr->rqst = NULL; 508 } 509 510 /** 511 * svcxdr_encode_opaque_pages - Insert pages into an xdr_stream 512 * @xdr: xdr_stream to be updated 513 * @pages: array of pages to insert 514 * @base: starting offset of first data byte in @pages 515 * @len: number of data bytes in @pages to insert 516 * 517 * After the @pages are added, the tail iovec is instantiated pointing 518 * to end of the head buffer, and the stream is set up to encode 519 * subsequent items into the tail. 520 */ 521 static inline void svcxdr_encode_opaque_pages(struct svc_rqst *rqstp, 522 struct xdr_stream *xdr, 523 struct page **pages, 524 unsigned int base, 525 unsigned int len) 526 { 527 xdr_write_pages(xdr, pages, base, len); 528 xdr->page_ptr = rqstp->rq_next_page - 1; 529 } 530 531 /** 532 * svcxdr_set_auth_slack - 533 * @rqstp: RPC transaction 534 * @slack: buffer space to reserve for the transaction's security flavor 535 * 536 * Set the request's slack space requirement, and set aside that much 537 * space in the rqstp's rq_res.head for use when the auth wraps the Reply. 538 */ 539 static inline void svcxdr_set_auth_slack(struct svc_rqst *rqstp, int slack) 540 { 541 struct xdr_stream *xdr = &rqstp->rq_res_stream; 542 struct xdr_buf *buf = &rqstp->rq_res; 543 struct kvec *resv = buf->head; 544 545 rqstp->rq_auth_slack = slack; 546 547 xdr->end -= XDR_QUADLEN(slack); 548 buf->buflen -= rqstp->rq_auth_slack; 549 550 WARN_ON(xdr->iov != resv); 551 WARN_ON(xdr->p > xdr->end); 552 } 553 554 /** 555 * svcxdr_set_accept_stat - Reserve space for the accept_stat field 556 * @rqstp: RPC transaction context 557 * 558 * Return values: 559 * %true: Success 560 * %false: No response buffer space was available 561 */ 562 static inline bool svcxdr_set_accept_stat(struct svc_rqst *rqstp) 563 { 564 struct xdr_stream *xdr = &rqstp->rq_res_stream; 565 566 rqstp->rq_accept_statp = xdr_reserve_space(xdr, XDR_UNIT); 567 if (unlikely(!rqstp->rq_accept_statp)) 568 return false; 569 *rqstp->rq_accept_statp = rpc_success; 570 return true; 571 } 572 573 #endif /* SUNRPC_SVC_H */ 574