2 * linux/net/sunrpc/svc.c
4 * High-level RPC service routines
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
8 * Multiple threads pools and NUMAisation
9 * Copyright (c) 2006 Silicon Graphics, Inc.
10 * by Greg Banks <gnb@melbourne.sgi.com>
13 #include <linux/linkage.h>
14 #include <linux/sched/signal.h>
15 #include <linux/errno.h>
16 #include <linux/net.h>
19 #include <linux/interrupt.h>
20 #include <linux/module.h>
21 #include <linux/kthread.h>
22 #include <linux/slab.h>
24 #include <linux/sunrpc/types.h>
25 #include <linux/sunrpc/xdr.h>
26 #include <linux/sunrpc/stats.h>
27 #include <linux/sunrpc/svcsock.h>
28 #include <linux/sunrpc/clnt.h>
29 #include <linux/sunrpc/bc_xprt.h>
31 #include <trace/events/sunrpc.h>
33 #define RPCDBG_FACILITY RPCDBG_SVCDSP
35 static void svc_unregister(const struct svc_serv
*serv
, struct net
*net
);
37 void svc_tcp_prep_reply_hdr(struct svc_rqst
*rqstp
);
39 #define svc_serv_is_pooled(serv) ((serv)->sv_ops->svo_function)
41 #define SVC_POOL_DEFAULT SVC_POOL_GLOBAL
44 * Structure for mapping cpus to pools and vice versa.
45 * Setup once during sunrpc initialisation.
47 struct svc_pool_map svc_pool_map
= {
48 .mode
= SVC_POOL_DEFAULT
50 EXPORT_SYMBOL_GPL(svc_pool_map
);
52 static DEFINE_MUTEX(svc_pool_map_mutex
);/* protects svc_pool_map.count only */
55 param_set_pool_mode(const char *val
, const struct kernel_param
*kp
)
57 int *ip
= (int *)kp
->arg
;
58 struct svc_pool_map
*m
= &svc_pool_map
;
61 mutex_lock(&svc_pool_map_mutex
);
68 if (!strncmp(val
, "auto", 4))
70 else if (!strncmp(val
, "global", 6))
71 *ip
= SVC_POOL_GLOBAL
;
72 else if (!strncmp(val
, "percpu", 6))
73 *ip
= SVC_POOL_PERCPU
;
74 else if (!strncmp(val
, "pernode", 7))
75 *ip
= SVC_POOL_PERNODE
;
80 mutex_unlock(&svc_pool_map_mutex
);
85 param_get_pool_mode(char *buf
, const struct kernel_param
*kp
)
87 int *ip
= (int *)kp
->arg
;
92 return strlcpy(buf
, "auto", 20);
94 return strlcpy(buf
, "global", 20);
96 return strlcpy(buf
, "percpu", 20);
97 case SVC_POOL_PERNODE
:
98 return strlcpy(buf
, "pernode", 20);
100 return sprintf(buf
, "%d", *ip
);
104 module_param_call(pool_mode
, param_set_pool_mode
, param_get_pool_mode
,
105 &svc_pool_map
.mode
, 0644);
108 * Detect best pool mapping mode heuristically,
109 * according to the machine's topology.
112 svc_pool_map_choose_mode(void)
116 if (nr_online_nodes
> 1) {
118 * Actually have multiple NUMA nodes,
119 * so split pools on NUMA node boundaries
121 return SVC_POOL_PERNODE
;
124 node
= first_online_node
;
125 if (nr_cpus_node(node
) > 2) {
127 * Non-trivial SMP, or CONFIG_NUMA on
128 * non-NUMA hardware, e.g. with a generic
129 * x86_64 kernel on Xeons. In this case we
130 * want to divide the pools on cpu boundaries.
132 return SVC_POOL_PERCPU
;
135 /* default: one global pool */
136 return SVC_POOL_GLOBAL
;
140 * Allocate the to_pool[] and pool_to[] arrays.
141 * Returns 0 on success or an errno.
144 svc_pool_map_alloc_arrays(struct svc_pool_map
*m
, unsigned int maxpools
)
146 m
->to_pool
= kcalloc(maxpools
, sizeof(unsigned int), GFP_KERNEL
);
149 m
->pool_to
= kcalloc(maxpools
, sizeof(unsigned int), GFP_KERNEL
);
163 * Initialise the pool map for SVC_POOL_PERCPU mode.
164 * Returns number of pools or <0 on error.
167 svc_pool_map_init_percpu(struct svc_pool_map
*m
)
169 unsigned int maxpools
= nr_cpu_ids
;
170 unsigned int pidx
= 0;
174 err
= svc_pool_map_alloc_arrays(m
, maxpools
);
178 for_each_online_cpu(cpu
) {
179 BUG_ON(pidx
>= maxpools
);
180 m
->to_pool
[cpu
] = pidx
;
181 m
->pool_to
[pidx
] = cpu
;
184 /* cpus brought online later all get mapped to pool0, sorry */
191 * Initialise the pool map for SVC_POOL_PERNODE mode.
192 * Returns number of pools or <0 on error.
195 svc_pool_map_init_pernode(struct svc_pool_map
*m
)
197 unsigned int maxpools
= nr_node_ids
;
198 unsigned int pidx
= 0;
202 err
= svc_pool_map_alloc_arrays(m
, maxpools
);
206 for_each_node_with_cpus(node
) {
207 /* some architectures (e.g. SN2) have cpuless nodes */
208 BUG_ON(pidx
> maxpools
);
209 m
->to_pool
[node
] = pidx
;
210 m
->pool_to
[pidx
] = node
;
213 /* nodes brought online later all get mapped to pool0, sorry */
220 * Add a reference to the global map of cpus to pools (and
221 * vice versa). Initialise the map if we're the first user.
222 * Returns the number of pools.
225 svc_pool_map_get(void)
227 struct svc_pool_map
*m
= &svc_pool_map
;
230 mutex_lock(&svc_pool_map_mutex
);
233 mutex_unlock(&svc_pool_map_mutex
);
237 if (m
->mode
== SVC_POOL_AUTO
)
238 m
->mode
= svc_pool_map_choose_mode();
241 case SVC_POOL_PERCPU
:
242 npools
= svc_pool_map_init_percpu(m
);
244 case SVC_POOL_PERNODE
:
245 npools
= svc_pool_map_init_pernode(m
);
250 /* default, or memory allocation failure */
252 m
->mode
= SVC_POOL_GLOBAL
;
256 mutex_unlock(&svc_pool_map_mutex
);
259 EXPORT_SYMBOL_GPL(svc_pool_map_get
);
262 * Drop a reference to the global map of cpus to pools.
263 * When the last reference is dropped, the map data is
264 * freed; this allows the sysadmin to change the pool
265 * mode using the pool_mode module option without
266 * rebooting or re-loading sunrpc.ko.
269 svc_pool_map_put(void)
271 struct svc_pool_map
*m
= &svc_pool_map
;
273 mutex_lock(&svc_pool_map_mutex
);
283 mutex_unlock(&svc_pool_map_mutex
);
285 EXPORT_SYMBOL_GPL(svc_pool_map_put
);
287 static int svc_pool_map_get_node(unsigned int pidx
)
289 const struct svc_pool_map
*m
= &svc_pool_map
;
292 if (m
->mode
== SVC_POOL_PERCPU
)
293 return cpu_to_node(m
->pool_to
[pidx
]);
294 if (m
->mode
== SVC_POOL_PERNODE
)
295 return m
->pool_to
[pidx
];
300 * Set the given thread's cpus_allowed mask so that it
301 * will only run on cpus in the given pool.
304 svc_pool_map_set_cpumask(struct task_struct
*task
, unsigned int pidx
)
306 struct svc_pool_map
*m
= &svc_pool_map
;
307 unsigned int node
= m
->pool_to
[pidx
];
310 * The caller checks for sv_nrpools > 1, which
311 * implies that we've been initialized.
313 WARN_ON_ONCE(m
->count
== 0);
318 case SVC_POOL_PERCPU
:
320 set_cpus_allowed_ptr(task
, cpumask_of(node
));
323 case SVC_POOL_PERNODE
:
325 set_cpus_allowed_ptr(task
, cpumask_of_node(node
));
332 * Use the mapping mode to choose a pool for a given CPU.
333 * Used when enqueueing an incoming RPC. Always returns
334 * a non-NULL pool pointer.
337 svc_pool_for_cpu(struct svc_serv
*serv
, int cpu
)
339 struct svc_pool_map
*m
= &svc_pool_map
;
340 unsigned int pidx
= 0;
343 * An uninitialised map happens in a pure client when
344 * lockd is brought up, so silently treat it the
345 * same as SVC_POOL_GLOBAL.
347 if (svc_serv_is_pooled(serv
)) {
349 case SVC_POOL_PERCPU
:
350 pidx
= m
->to_pool
[cpu
];
352 case SVC_POOL_PERNODE
:
353 pidx
= m
->to_pool
[cpu_to_node(cpu
)];
357 return &serv
->sv_pools
[pidx
% serv
->sv_nrpools
];
360 int svc_rpcb_setup(struct svc_serv
*serv
, struct net
*net
)
364 err
= rpcb_create_local(net
);
368 /* Remove any stale portmap registrations */
369 svc_unregister(serv
, net
);
372 EXPORT_SYMBOL_GPL(svc_rpcb_setup
);
374 void svc_rpcb_cleanup(struct svc_serv
*serv
, struct net
*net
)
376 svc_unregister(serv
, net
);
379 EXPORT_SYMBOL_GPL(svc_rpcb_cleanup
);
381 static int svc_uses_rpcbind(struct svc_serv
*serv
)
383 struct svc_program
*progp
;
386 for (progp
= serv
->sv_program
; progp
; progp
= progp
->pg_next
) {
387 for (i
= 0; i
< progp
->pg_nvers
; i
++) {
388 if (progp
->pg_vers
[i
] == NULL
)
390 if (!progp
->pg_vers
[i
]->vs_hidden
)
398 int svc_bind(struct svc_serv
*serv
, struct net
*net
)
400 if (!svc_uses_rpcbind(serv
))
402 return svc_rpcb_setup(serv
, net
);
404 EXPORT_SYMBOL_GPL(svc_bind
);
406 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
408 __svc_init_bc(struct svc_serv
*serv
)
410 INIT_LIST_HEAD(&serv
->sv_cb_list
);
411 spin_lock_init(&serv
->sv_cb_lock
);
412 init_waitqueue_head(&serv
->sv_cb_waitq
);
416 __svc_init_bc(struct svc_serv
*serv
)
422 * Create an RPC service
424 static struct svc_serv
*
425 __svc_create(struct svc_program
*prog
, unsigned int bufsize
, int npools
,
426 const struct svc_serv_ops
*ops
)
428 struct svc_serv
*serv
;
430 unsigned int xdrsize
;
433 if (!(serv
= kzalloc(sizeof(*serv
), GFP_KERNEL
)))
435 serv
->sv_name
= prog
->pg_name
;
436 serv
->sv_program
= prog
;
437 serv
->sv_nrthreads
= 1;
438 serv
->sv_stats
= prog
->pg_stats
;
439 if (bufsize
> RPCSVC_MAXPAYLOAD
)
440 bufsize
= RPCSVC_MAXPAYLOAD
;
441 serv
->sv_max_payload
= bufsize
? bufsize
: 4096;
442 serv
->sv_max_mesg
= roundup(serv
->sv_max_payload
+ PAGE_SIZE
, PAGE_SIZE
);
446 prog
->pg_lovers
= prog
->pg_nvers
-1;
447 for (vers
=0; vers
<prog
->pg_nvers
; vers
++)
448 if (prog
->pg_vers
[vers
]) {
449 prog
->pg_hivers
= vers
;
450 if (prog
->pg_lovers
> vers
)
451 prog
->pg_lovers
= vers
;
452 if (prog
->pg_vers
[vers
]->vs_xdrsize
> xdrsize
)
453 xdrsize
= prog
->pg_vers
[vers
]->vs_xdrsize
;
455 prog
= prog
->pg_next
;
457 serv
->sv_xdrsize
= xdrsize
;
458 INIT_LIST_HEAD(&serv
->sv_tempsocks
);
459 INIT_LIST_HEAD(&serv
->sv_permsocks
);
460 timer_setup(&serv
->sv_temptimer
, NULL
, 0);
461 spin_lock_init(&serv
->sv_lock
);
465 serv
->sv_nrpools
= npools
;
467 kcalloc(serv
->sv_nrpools
, sizeof(struct svc_pool
),
469 if (!serv
->sv_pools
) {
474 for (i
= 0; i
< serv
->sv_nrpools
; i
++) {
475 struct svc_pool
*pool
= &serv
->sv_pools
[i
];
477 dprintk("svc: initialising pool %u for %s\n",
481 INIT_LIST_HEAD(&pool
->sp_sockets
);
482 INIT_LIST_HEAD(&pool
->sp_all_threads
);
483 spin_lock_init(&pool
->sp_lock
);
490 svc_create(struct svc_program
*prog
, unsigned int bufsize
,
491 const struct svc_serv_ops
*ops
)
493 return __svc_create(prog
, bufsize
, /*npools*/1, ops
);
495 EXPORT_SYMBOL_GPL(svc_create
);
498 svc_create_pooled(struct svc_program
*prog
, unsigned int bufsize
,
499 const struct svc_serv_ops
*ops
)
501 struct svc_serv
*serv
;
502 unsigned int npools
= svc_pool_map_get();
504 serv
= __svc_create(prog
, bufsize
, npools
, ops
);
512 EXPORT_SYMBOL_GPL(svc_create_pooled
);
514 void svc_shutdown_net(struct svc_serv
*serv
, struct net
*net
)
516 svc_close_net(serv
, net
);
518 if (serv
->sv_ops
->svo_shutdown
)
519 serv
->sv_ops
->svo_shutdown(serv
, net
);
521 EXPORT_SYMBOL_GPL(svc_shutdown_net
);
524 * Destroy an RPC service. Should be called with appropriate locking to
525 * protect the sv_nrthreads, sv_permsocks and sv_tempsocks.
528 svc_destroy(struct svc_serv
*serv
)
530 dprintk("svc: svc_destroy(%s, %d)\n",
531 serv
->sv_program
->pg_name
,
534 if (serv
->sv_nrthreads
) {
535 if (--(serv
->sv_nrthreads
) != 0) {
536 svc_sock_update_bufs(serv
);
540 printk("svc_destroy: no threads for serv=%p!\n", serv
);
542 del_timer_sync(&serv
->sv_temptimer
);
545 * The last user is gone and thus all sockets have to be destroyed to
546 * the point. Check this.
548 BUG_ON(!list_empty(&serv
->sv_permsocks
));
549 BUG_ON(!list_empty(&serv
->sv_tempsocks
));
551 cache_clean_deferred(serv
);
553 if (svc_serv_is_pooled(serv
))
556 kfree(serv
->sv_pools
);
559 EXPORT_SYMBOL_GPL(svc_destroy
);
562 * Allocate an RPC server's buffer space.
563 * We allocate pages and place them in rq_argpages.
566 svc_init_buffer(struct svc_rqst
*rqstp
, unsigned int size
, int node
)
568 unsigned int pages
, arghi
;
570 /* bc_xprt uses fore channel allocated buffers */
571 if (svc_is_backchannel(rqstp
))
574 pages
= size
/ PAGE_SIZE
+ 1; /* extra page as we hold both request and reply.
575 * We assume one is at most one page
578 WARN_ON_ONCE(pages
> RPCSVC_MAXPAGES
);
579 if (pages
> RPCSVC_MAXPAGES
)
580 pages
= RPCSVC_MAXPAGES
;
582 struct page
*p
= alloc_pages_node(node
, GFP_KERNEL
, 0);
585 rqstp
->rq_pages
[arghi
++] = p
;
592 * Release an RPC server buffer
595 svc_release_buffer(struct svc_rqst
*rqstp
)
599 for (i
= 0; i
< ARRAY_SIZE(rqstp
->rq_pages
); i
++)
600 if (rqstp
->rq_pages
[i
])
601 put_page(rqstp
->rq_pages
[i
]);
605 svc_rqst_alloc(struct svc_serv
*serv
, struct svc_pool
*pool
, int node
)
607 struct svc_rqst
*rqstp
;
609 rqstp
= kzalloc_node(sizeof(*rqstp
), GFP_KERNEL
, node
);
613 __set_bit(RQ_BUSY
, &rqstp
->rq_flags
);
614 spin_lock_init(&rqstp
->rq_lock
);
615 rqstp
->rq_server
= serv
;
616 rqstp
->rq_pool
= pool
;
618 rqstp
->rq_argp
= kmalloc_node(serv
->sv_xdrsize
, GFP_KERNEL
, node
);
622 rqstp
->rq_resp
= kmalloc_node(serv
->sv_xdrsize
, GFP_KERNEL
, node
);
626 if (!svc_init_buffer(rqstp
, serv
->sv_max_mesg
, node
))
631 svc_rqst_free(rqstp
);
634 EXPORT_SYMBOL_GPL(svc_rqst_alloc
);
637 svc_prepare_thread(struct svc_serv
*serv
, struct svc_pool
*pool
, int node
)
639 struct svc_rqst
*rqstp
;
641 rqstp
= svc_rqst_alloc(serv
, pool
, node
);
643 return ERR_PTR(-ENOMEM
);
645 serv
->sv_nrthreads
++;
646 spin_lock_bh(&pool
->sp_lock
);
647 pool
->sp_nrthreads
++;
648 list_add_rcu(&rqstp
->rq_all
, &pool
->sp_all_threads
);
649 spin_unlock_bh(&pool
->sp_lock
);
652 EXPORT_SYMBOL_GPL(svc_prepare_thread
);
655 * Choose a pool in which to create a new thread, for svc_set_num_threads
657 static inline struct svc_pool
*
658 choose_pool(struct svc_serv
*serv
, struct svc_pool
*pool
, unsigned int *state
)
663 return &serv
->sv_pools
[(*state
)++ % serv
->sv_nrpools
];
667 * Choose a thread to kill, for svc_set_num_threads
669 static inline struct task_struct
*
670 choose_victim(struct svc_serv
*serv
, struct svc_pool
*pool
, unsigned int *state
)
673 struct task_struct
*task
= NULL
;
676 spin_lock_bh(&pool
->sp_lock
);
678 /* choose a pool in round-robin fashion */
679 for (i
= 0; i
< serv
->sv_nrpools
; i
++) {
680 pool
= &serv
->sv_pools
[--(*state
) % serv
->sv_nrpools
];
681 spin_lock_bh(&pool
->sp_lock
);
682 if (!list_empty(&pool
->sp_all_threads
))
684 spin_unlock_bh(&pool
->sp_lock
);
690 if (!list_empty(&pool
->sp_all_threads
)) {
691 struct svc_rqst
*rqstp
;
694 * Remove from the pool->sp_all_threads list
695 * so we don't try to kill it again.
697 rqstp
= list_entry(pool
->sp_all_threads
.next
, struct svc_rqst
, rq_all
);
698 set_bit(RQ_VICTIM
, &rqstp
->rq_flags
);
699 list_del_rcu(&rqstp
->rq_all
);
700 task
= rqstp
->rq_task
;
702 spin_unlock_bh(&pool
->sp_lock
);
707 /* create new threads */
709 svc_start_kthreads(struct svc_serv
*serv
, struct svc_pool
*pool
, int nrservs
)
711 struct svc_rqst
*rqstp
;
712 struct task_struct
*task
;
713 struct svc_pool
*chosen_pool
;
714 unsigned int state
= serv
->sv_nrthreads
-1;
719 chosen_pool
= choose_pool(serv
, pool
, &state
);
721 node
= svc_pool_map_get_node(chosen_pool
->sp_id
);
722 rqstp
= svc_prepare_thread(serv
, chosen_pool
, node
);
724 return PTR_ERR(rqstp
);
726 __module_get(serv
->sv_ops
->svo_module
);
727 task
= kthread_create_on_node(serv
->sv_ops
->svo_function
, rqstp
,
728 node
, "%s", serv
->sv_name
);
730 module_put(serv
->sv_ops
->svo_module
);
731 svc_exit_thread(rqstp
);
732 return PTR_ERR(task
);
735 rqstp
->rq_task
= task
;
736 if (serv
->sv_nrpools
> 1)
737 svc_pool_map_set_cpumask(task
, chosen_pool
->sp_id
);
739 svc_sock_update_bufs(serv
);
740 wake_up_process(task
);
741 } while (nrservs
> 0);
747 /* destroy old threads */
749 svc_signal_kthreads(struct svc_serv
*serv
, struct svc_pool
*pool
, int nrservs
)
751 struct task_struct
*task
;
752 unsigned int state
= serv
->sv_nrthreads
-1;
754 /* destroy old threads */
756 task
= choose_victim(serv
, pool
, &state
);
759 send_sig(SIGINT
, task
, 1);
761 } while (nrservs
< 0);
767 * Create or destroy enough new threads to make the number
768 * of threads the given number. If `pool' is non-NULL, applies
769 * only to threads in that pool, otherwise round-robins between
770 * all pools. Caller must ensure that mutual exclusion between this and
771 * server startup or shutdown.
773 * Destroying threads relies on the service threads filling in
774 * rqstp->rq_task, which only the nfs ones do. Assumes the serv
775 * has been created using svc_create_pooled().
777 * Based on code that used to be in nfsd_svc() but tweaked
781 svc_set_num_threads(struct svc_serv
*serv
, struct svc_pool
*pool
, int nrservs
)
784 /* The -1 assumes caller has done a svc_get() */
785 nrservs
-= (serv
->sv_nrthreads
-1);
787 spin_lock_bh(&pool
->sp_lock
);
788 nrservs
-= pool
->sp_nrthreads
;
789 spin_unlock_bh(&pool
->sp_lock
);
793 return svc_start_kthreads(serv
, pool
, nrservs
);
795 return svc_signal_kthreads(serv
, pool
, nrservs
);
798 EXPORT_SYMBOL_GPL(svc_set_num_threads
);
800 /* destroy old threads */
802 svc_stop_kthreads(struct svc_serv
*serv
, struct svc_pool
*pool
, int nrservs
)
804 struct task_struct
*task
;
805 unsigned int state
= serv
->sv_nrthreads
-1;
807 /* destroy old threads */
809 task
= choose_victim(serv
, pool
, &state
);
814 } while (nrservs
< 0);
819 svc_set_num_threads_sync(struct svc_serv
*serv
, struct svc_pool
*pool
, int nrservs
)
822 /* The -1 assumes caller has done a svc_get() */
823 nrservs
-= (serv
->sv_nrthreads
-1);
825 spin_lock_bh(&pool
->sp_lock
);
826 nrservs
-= pool
->sp_nrthreads
;
827 spin_unlock_bh(&pool
->sp_lock
);
831 return svc_start_kthreads(serv
, pool
, nrservs
);
833 return svc_stop_kthreads(serv
, pool
, nrservs
);
836 EXPORT_SYMBOL_GPL(svc_set_num_threads_sync
);
839 * Called from a server thread as it's exiting. Caller must hold the "service
840 * mutex" for the service.
843 svc_rqst_free(struct svc_rqst
*rqstp
)
845 svc_release_buffer(rqstp
);
846 kfree(rqstp
->rq_resp
);
847 kfree(rqstp
->rq_argp
);
848 kfree(rqstp
->rq_auth_data
);
849 kfree_rcu(rqstp
, rq_rcu_head
);
851 EXPORT_SYMBOL_GPL(svc_rqst_free
);
854 svc_exit_thread(struct svc_rqst
*rqstp
)
856 struct svc_serv
*serv
= rqstp
->rq_server
;
857 struct svc_pool
*pool
= rqstp
->rq_pool
;
859 spin_lock_bh(&pool
->sp_lock
);
860 pool
->sp_nrthreads
--;
861 if (!test_and_set_bit(RQ_VICTIM
, &rqstp
->rq_flags
))
862 list_del_rcu(&rqstp
->rq_all
);
863 spin_unlock_bh(&pool
->sp_lock
);
865 svc_rqst_free(rqstp
);
867 /* Release the server */
871 EXPORT_SYMBOL_GPL(svc_exit_thread
);
874 * Register an "inet" protocol family netid with the local
875 * rpcbind daemon via an rpcbind v4 SET request.
877 * No netconfig infrastructure is available in the kernel, so
878 * we map IP_ protocol numbers to netids by hand.
880 * Returns zero on success; a negative errno value is returned
881 * if any error occurs.
883 static int __svc_rpcb_register4(struct net
*net
, const u32 program
,
885 const unsigned short protocol
,
886 const unsigned short port
)
888 const struct sockaddr_in sin
= {
889 .sin_family
= AF_INET
,
890 .sin_addr
.s_addr
= htonl(INADDR_ANY
),
891 .sin_port
= htons(port
),
898 netid
= RPCBIND_NETID_UDP
;
901 netid
= RPCBIND_NETID_TCP
;
907 error
= rpcb_v4_register(net
, program
, version
,
908 (const struct sockaddr
*)&sin
, netid
);
911 * User space didn't support rpcbind v4, so retry this
912 * registration request with the legacy rpcbind v2 protocol.
914 if (error
== -EPROTONOSUPPORT
)
915 error
= rpcb_register(net
, program
, version
, protocol
, port
);
920 #if IS_ENABLED(CONFIG_IPV6)
922 * Register an "inet6" protocol family netid with the local
923 * rpcbind daemon via an rpcbind v4 SET request.
925 * No netconfig infrastructure is available in the kernel, so
926 * we map IP_ protocol numbers to netids by hand.
928 * Returns zero on success; a negative errno value is returned
929 * if any error occurs.
931 static int __svc_rpcb_register6(struct net
*net
, const u32 program
,
933 const unsigned short protocol
,
934 const unsigned short port
)
936 const struct sockaddr_in6 sin6
= {
937 .sin6_family
= AF_INET6
,
938 .sin6_addr
= IN6ADDR_ANY_INIT
,
939 .sin6_port
= htons(port
),
946 netid
= RPCBIND_NETID_UDP6
;
949 netid
= RPCBIND_NETID_TCP6
;
955 error
= rpcb_v4_register(net
, program
, version
,
956 (const struct sockaddr
*)&sin6
, netid
);
959 * User space didn't support rpcbind version 4, so we won't
960 * use a PF_INET6 listener.
962 if (error
== -EPROTONOSUPPORT
)
963 error
= -EAFNOSUPPORT
;
967 #endif /* IS_ENABLED(CONFIG_IPV6) */
970 * Register a kernel RPC service via rpcbind version 4.
972 * Returns zero on success; a negative errno value is returned
973 * if any error occurs.
975 static int __svc_register(struct net
*net
, const char *progname
,
976 const u32 program
, const u32 version
,
978 const unsigned short protocol
,
979 const unsigned short port
)
981 int error
= -EAFNOSUPPORT
;
985 error
= __svc_rpcb_register4(net
, program
, version
,
988 #if IS_ENABLED(CONFIG_IPV6)
990 error
= __svc_rpcb_register6(net
, program
, version
,
999 * svc_register - register an RPC service with the local portmapper
1000 * @serv: svc_serv struct for the service to register
1001 * @net: net namespace for the service to register
1002 * @family: protocol family of service's listener socket
1003 * @proto: transport protocol number to advertise
1004 * @port: port to advertise
1006 * Service is registered for any address in the passed-in protocol family
1008 int svc_register(const struct svc_serv
*serv
, struct net
*net
,
1009 const int family
, const unsigned short proto
,
1010 const unsigned short port
)
1012 struct svc_program
*progp
;
1013 const struct svc_version
*vers
;
1017 WARN_ON_ONCE(proto
== 0 && port
== 0);
1018 if (proto
== 0 && port
== 0)
1021 for (progp
= serv
->sv_program
; progp
; progp
= progp
->pg_next
) {
1022 for (i
= 0; i
< progp
->pg_nvers
; i
++) {
1023 vers
= progp
->pg_vers
[i
];
1027 dprintk("svc: svc_register(%sv%d, %s, %u, %u)%s\n",
1030 proto
== IPPROTO_UDP
? "udp" : "tcp",
1034 " (but not telling portmap)" : "");
1036 if (vers
->vs_hidden
)
1040 * Don't register a UDP port if we need congestion
1043 if (vers
->vs_need_cong_ctrl
&& proto
== IPPROTO_UDP
)
1046 error
= __svc_register(net
, progp
->pg_name
, progp
->pg_prog
,
1047 i
, family
, proto
, port
);
1049 if (vers
->vs_rpcb_optnl
) {
1055 printk(KERN_WARNING
"svc: failed to register "
1056 "%sv%u RPC service (errno %d).\n",
1057 progp
->pg_name
, i
, -error
);
1067 * If user space is running rpcbind, it should take the v4 UNSET
1068 * and clear everything for this [program, version]. If user space
1069 * is running portmap, it will reject the v4 UNSET, but won't have
1070 * any "inet6" entries anyway. So a PMAP_UNSET should be sufficient
1071 * in this case to clear all existing entries for [program, version].
1073 static void __svc_unregister(struct net
*net
, const u32 program
, const u32 version
,
1074 const char *progname
)
1078 error
= rpcb_v4_register(net
, program
, version
, NULL
, "");
1081 * User space didn't support rpcbind v4, so retry this
1082 * request with the legacy rpcbind v2 protocol.
1084 if (error
== -EPROTONOSUPPORT
)
1085 error
= rpcb_register(net
, program
, version
, 0, 0);
1087 dprintk("svc: %s(%sv%u), error %d\n",
1088 __func__
, progname
, version
, error
);
1092 * All netids, bind addresses and ports registered for [program, version]
1093 * are removed from the local rpcbind database (if the service is not
1094 * hidden) to make way for a new instance of the service.
1096 * The result of unregistration is reported via dprintk for those who want
1097 * verification of the result, but is otherwise not important.
1099 static void svc_unregister(const struct svc_serv
*serv
, struct net
*net
)
1101 struct svc_program
*progp
;
1102 unsigned long flags
;
1105 clear_thread_flag(TIF_SIGPENDING
);
1107 for (progp
= serv
->sv_program
; progp
; progp
= progp
->pg_next
) {
1108 for (i
= 0; i
< progp
->pg_nvers
; i
++) {
1109 if (progp
->pg_vers
[i
] == NULL
)
1111 if (progp
->pg_vers
[i
]->vs_hidden
)
1114 dprintk("svc: attempting to unregister %sv%u\n",
1116 __svc_unregister(net
, progp
->pg_prog
, i
, progp
->pg_name
);
1120 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
1121 recalc_sigpending();
1122 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
1126 * dprintk the given error with the address of the client that caused it.
1128 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
1129 static __printf(2, 3)
1130 void svc_printk(struct svc_rqst
*rqstp
, const char *fmt
, ...)
1132 struct va_format vaf
;
1134 char buf
[RPC_MAX_ADDRBUFLEN
];
1136 va_start(args
, fmt
);
1141 dprintk("svc: %s: %pV", svc_print_addr(rqstp
, buf
, sizeof(buf
)), &vaf
);
1146 static __printf(2,3) void svc_printk(struct svc_rqst
*rqstp
, const char *fmt
, ...) {}
1150 * Common routine for processing the RPC request.
1153 svc_process_common(struct svc_rqst
*rqstp
, struct kvec
*argv
, struct kvec
*resv
)
1155 struct svc_program
*progp
;
1156 const struct svc_version
*versp
= NULL
; /* compiler food */
1157 const struct svc_procedure
*procp
= NULL
;
1158 struct svc_serv
*serv
= rqstp
->rq_server
;
1160 u32 prog
, vers
, proc
;
1161 __be32 auth_stat
, rpc_stat
;
1163 __be32
*reply_statp
;
1165 rpc_stat
= rpc_success
;
1167 if (argv
->iov_len
< 6*4)
1170 /* Will be turned off by GSS integrity and privacy services */
1171 set_bit(RQ_SPLICE_OK
, &rqstp
->rq_flags
);
1172 /* Will be turned off only when NFSv4 Sessions are used */
1173 set_bit(RQ_USEDEFERRAL
, &rqstp
->rq_flags
);
1174 clear_bit(RQ_DROPME
, &rqstp
->rq_flags
);
1176 /* Setup reply header */
1177 if (rqstp
->rq_prot
== IPPROTO_TCP
)
1178 svc_tcp_prep_reply_hdr(rqstp
);
1180 svc_putu32(resv
, rqstp
->rq_xid
);
1182 vers
= svc_getnl(argv
);
1184 /* First words of reply: */
1185 svc_putnl(resv
, 1); /* REPLY */
1187 if (vers
!= 2) /* RPC version number */
1190 /* Save position in case we later decide to reject: */
1191 reply_statp
= resv
->iov_base
+ resv
->iov_len
;
1193 svc_putnl(resv
, 0); /* ACCEPT */
1195 rqstp
->rq_prog
= prog
= svc_getnl(argv
); /* program number */
1196 rqstp
->rq_vers
= vers
= svc_getnl(argv
); /* version number */
1197 rqstp
->rq_proc
= proc
= svc_getnl(argv
); /* procedure number */
1199 for (progp
= serv
->sv_program
; progp
; progp
= progp
->pg_next
)
1200 if (prog
== progp
->pg_prog
)
1204 * Decode auth data, and add verifier to reply buffer.
1205 * We do this before anything else in order to get a decent
1208 auth_res
= svc_authenticate(rqstp
, &auth_stat
);
1209 /* Also give the program a chance to reject this call: */
1210 if (auth_res
== SVC_OK
&& progp
) {
1211 auth_stat
= rpc_autherr_badcred
;
1212 auth_res
= progp
->pg_authenticate(rqstp
);
1220 rpc_stat
= rpc_system_err
;
1235 if (vers
>= progp
->pg_nvers
||
1236 !(versp
= progp
->pg_vers
[vers
]))
1240 * Some protocol versions (namely NFSv4) require some form of
1241 * congestion control. (See RFC 7530 section 3.1 paragraph 2)
1242 * In other words, UDP is not allowed. We mark those when setting
1243 * up the svc_xprt, and verify that here.
1245 * The spec is not very clear about what error should be returned
1246 * when someone tries to access a server that is listening on UDP
1247 * for lower versions. RPC_PROG_MISMATCH seems to be the closest
1250 if (versp
->vs_need_cong_ctrl
&& rqstp
->rq_xprt
&&
1251 !test_bit(XPT_CONG_CTRL
, &rqstp
->rq_xprt
->xpt_flags
))
1254 procp
= versp
->vs_proc
+ proc
;
1255 if (proc
>= versp
->vs_nproc
|| !procp
->pc_func
)
1257 rqstp
->rq_procinfo
= procp
;
1259 /* Syntactic check complete */
1260 serv
->sv_stats
->rpccnt
++;
1262 /* Build the reply header. */
1263 statp
= resv
->iov_base
+resv
->iov_len
;
1264 svc_putnl(resv
, RPC_SUCCESS
);
1266 /* Bump per-procedure stats counter */
1267 versp
->vs_count
[proc
]++;
1269 /* Initialize storage for argp and resp */
1270 memset(rqstp
->rq_argp
, 0, procp
->pc_argsize
);
1271 memset(rqstp
->rq_resp
, 0, procp
->pc_ressize
);
1273 /* un-reserve some of the out-queue now that we have a
1274 * better idea of reply size
1276 if (procp
->pc_xdrressize
)
1277 svc_reserve_auth(rqstp
, procp
->pc_xdrressize
<<2);
1279 /* Call the function that processes the request. */
1280 if (!versp
->vs_dispatch
) {
1283 * XXX: why do we ignore the return value?
1285 if (procp
->pc_decode
&&
1286 !procp
->pc_decode(rqstp
, argv
->iov_base
))
1289 *statp
= procp
->pc_func(rqstp
);
1292 if (*statp
== rpc_drop_reply
||
1293 test_bit(RQ_DROPME
, &rqstp
->rq_flags
)) {
1294 if (procp
->pc_release
)
1295 procp
->pc_release(rqstp
);
1298 if (*statp
== rpc_autherr_badcred
) {
1299 if (procp
->pc_release
)
1300 procp
->pc_release(rqstp
);
1303 if (*statp
== rpc_success
&& procp
->pc_encode
&&
1304 !procp
->pc_encode(rqstp
, resv
->iov_base
+ resv
->iov_len
)) {
1305 dprintk("svc: failed to encode reply\n");
1306 /* serv->sv_stats->rpcsystemerr++; */
1307 *statp
= rpc_system_err
;
1310 dprintk("svc: calling dispatcher\n");
1311 if (!versp
->vs_dispatch(rqstp
, statp
)) {
1312 /* Release reply info */
1313 if (procp
->pc_release
)
1314 procp
->pc_release(rqstp
);
1319 /* Check RPC status result */
1320 if (*statp
!= rpc_success
)
1321 resv
->iov_len
= ((void*)statp
) - resv
->iov_base
+ 4;
1323 /* Release reply info */
1324 if (procp
->pc_release
)
1325 procp
->pc_release(rqstp
);
1327 if (procp
->pc_encode
== NULL
)
1331 if (svc_authorise(rqstp
))
1333 return 1; /* Caller can now send it */
1336 svc_authorise(rqstp
); /* doesn't hurt to call this twice */
1337 dprintk("svc: svc_process dropit\n");
1341 if (rqstp
->rq_xprt
&& test_bit(XPT_TEMP
, &rqstp
->rq_xprt
->xpt_flags
))
1342 svc_close_xprt(rqstp
->rq_xprt
);
1343 dprintk("svc: svc_process close\n");
1347 svc_printk(rqstp
, "short len %zd, dropping request\n",
1352 serv
->sv_stats
->rpcbadfmt
++;
1353 svc_putnl(resv
, 1); /* REJECT */
1354 svc_putnl(resv
, 0); /* RPC_MISMATCH */
1355 svc_putnl(resv
, 2); /* Only RPCv2 supported */
1360 dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat
));
1361 serv
->sv_stats
->rpcbadauth
++;
1362 /* Restore write pointer to location of accept status: */
1363 xdr_ressize_check(rqstp
, reply_statp
);
1364 svc_putnl(resv
, 1); /* REJECT */
1365 svc_putnl(resv
, 1); /* AUTH_ERROR */
1366 svc_putnl(resv
, ntohl(auth_stat
)); /* status */
1370 dprintk("svc: unknown program %d\n", prog
);
1371 serv
->sv_stats
->rpcbadfmt
++;
1372 svc_putnl(resv
, RPC_PROG_UNAVAIL
);
1376 svc_printk(rqstp
, "unknown version (%d for prog %d, %s)\n",
1377 vers
, prog
, progp
->pg_name
);
1379 serv
->sv_stats
->rpcbadfmt
++;
1380 svc_putnl(resv
, RPC_PROG_MISMATCH
);
1381 svc_putnl(resv
, progp
->pg_lovers
);
1382 svc_putnl(resv
, progp
->pg_hivers
);
1386 svc_printk(rqstp
, "unknown procedure (%d)\n", proc
);
1388 serv
->sv_stats
->rpcbadfmt
++;
1389 svc_putnl(resv
, RPC_PROC_UNAVAIL
);
1393 svc_printk(rqstp
, "failed to decode args\n");
1395 rpc_stat
= rpc_garbage_args
;
1397 serv
->sv_stats
->rpcbadfmt
++;
1398 svc_putnl(resv
, ntohl(rpc_stat
));
1403 * Process the RPC request.
1406 svc_process(struct svc_rqst
*rqstp
)
1408 struct kvec
*argv
= &rqstp
->rq_arg
.head
[0];
1409 struct kvec
*resv
= &rqstp
->rq_res
.head
[0];
1410 struct svc_serv
*serv
= rqstp
->rq_server
;
1414 * Setup response xdr_buf.
1415 * Initially it has just one page
1417 rqstp
->rq_next_page
= &rqstp
->rq_respages
[1];
1418 resv
->iov_base
= page_address(rqstp
->rq_respages
[0]);
1420 rqstp
->rq_res
.pages
= rqstp
->rq_respages
+ 1;
1421 rqstp
->rq_res
.len
= 0;
1422 rqstp
->rq_res
.page_base
= 0;
1423 rqstp
->rq_res
.page_len
= 0;
1424 rqstp
->rq_res
.buflen
= PAGE_SIZE
;
1425 rqstp
->rq_res
.tail
[0].iov_base
= NULL
;
1426 rqstp
->rq_res
.tail
[0].iov_len
= 0;
1428 dir
= svc_getnl(argv
);
1430 /* direction != CALL */
1431 svc_printk(rqstp
, "bad direction %d, dropping request\n", dir
);
1432 serv
->sv_stats
->rpcbadfmt
++;
1436 /* Returns 1 for send, 0 for drop */
1437 if (likely(svc_process_common(rqstp
, argv
, resv
))) {
1438 int ret
= svc_send(rqstp
);
1440 trace_svc_process(rqstp
, ret
);
1444 trace_svc_process(rqstp
, 0);
1448 EXPORT_SYMBOL_GPL(svc_process
);
1450 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1452 * Process a backchannel RPC request that arrived over an existing
1453 * outbound connection
1456 bc_svc_process(struct svc_serv
*serv
, struct rpc_rqst
*req
,
1457 struct svc_rqst
*rqstp
)
1459 struct kvec
*argv
= &rqstp
->rq_arg
.head
[0];
1460 struct kvec
*resv
= &rqstp
->rq_res
.head
[0];
1461 struct rpc_task
*task
;
1465 dprintk("svc: %s(%p)\n", __func__
, req
);
1467 /* Build the svc_rqst used by the common processing routine */
1468 rqstp
->rq_xid
= req
->rq_xid
;
1469 rqstp
->rq_prot
= req
->rq_xprt
->prot
;
1470 rqstp
->rq_server
= serv
;
1471 rqstp
->rq_bc_net
= req
->rq_xprt
->xprt_net
;
1473 rqstp
->rq_addrlen
= sizeof(req
->rq_xprt
->addr
);
1474 memcpy(&rqstp
->rq_addr
, &req
->rq_xprt
->addr
, rqstp
->rq_addrlen
);
1475 memcpy(&rqstp
->rq_arg
, &req
->rq_rcv_buf
, sizeof(rqstp
->rq_arg
));
1476 memcpy(&rqstp
->rq_res
, &req
->rq_snd_buf
, sizeof(rqstp
->rq_res
));
1478 /* Adjust the argument buffer length */
1479 rqstp
->rq_arg
.len
= req
->rq_private_buf
.len
;
1480 if (rqstp
->rq_arg
.len
<= rqstp
->rq_arg
.head
[0].iov_len
) {
1481 rqstp
->rq_arg
.head
[0].iov_len
= rqstp
->rq_arg
.len
;
1482 rqstp
->rq_arg
.page_len
= 0;
1483 } else if (rqstp
->rq_arg
.len
<= rqstp
->rq_arg
.head
[0].iov_len
+
1484 rqstp
->rq_arg
.page_len
)
1485 rqstp
->rq_arg
.page_len
= rqstp
->rq_arg
.len
-
1486 rqstp
->rq_arg
.head
[0].iov_len
;
1488 rqstp
->rq_arg
.len
= rqstp
->rq_arg
.head
[0].iov_len
+
1489 rqstp
->rq_arg
.page_len
;
1491 /* reset result send buffer "put" position */
1495 * Skip the next two words because they've already been
1496 * processed in the transport
1498 svc_getu32(argv
); /* XID */
1499 svc_getnl(argv
); /* CALLDIR */
1501 /* Parse and execute the bc call */
1502 proc_error
= svc_process_common(rqstp
, argv
, resv
);
1504 atomic_inc(&req
->rq_xprt
->bc_free_slots
);
1506 /* Processing error: drop the request */
1507 xprt_free_bc_request(req
);
1511 /* Finally, send the reply synchronously */
1512 memcpy(&req
->rq_snd_buf
, &rqstp
->rq_res
, sizeof(req
->rq_snd_buf
));
1513 task
= rpc_run_bc_task(req
);
1515 error
= PTR_ERR(task
);
1519 WARN_ON_ONCE(atomic_read(&task
->tk_count
) != 1);
1520 error
= task
->tk_status
;
1524 dprintk("svc: %s(), error=%d\n", __func__
, error
);
1527 EXPORT_SYMBOL_GPL(bc_svc_process
);
1528 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1531 * Return (transport-specific) limit on the rpc payload.
1533 u32
svc_max_payload(const struct svc_rqst
*rqstp
)
1535 u32 max
= rqstp
->rq_xprt
->xpt_class
->xcl_max_payload
;
1537 if (rqstp
->rq_server
->sv_max_payload
< max
)
1538 max
= rqstp
->rq_server
->sv_max_payload
;
1541 EXPORT_SYMBOL_GPL(svc_max_payload
);