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