13 #include <linux/linkage.h>
14 #include <linux/sched.h>
15 #include <linux/errno.h>
16 #include <linux/net.h>
20 #include <linux/module.h>
22 #include <linux/slab.h>
32 #define RPCDBG_FACILITY RPCDBG_SVCDSP
34 static void svc_unregister(
const struct svc_serv *serv,
struct net *
net);
36 #define svc_serv_is_pooled(serv) ((serv)->sv_function)
48 #define SVC_POOL_DEFAULT SVC_POOL_GLOBAL
54 static struct svc_pool_map {
60 unsigned int *pool_to;
61 unsigned int *to_pool;
71 int *
ip = (
int *)kp->
arg;
72 struct svc_pool_map *
m = &svc_pool_map;
84 else if (!
strncmp(val,
"global", 6))
86 else if (!
strncmp(val,
"percpu", 6))
88 else if (!
strncmp(val,
"pernode", 7))
101 int *ip = (
int *)kp->
arg;
106 return strlcpy(buf,
"auto", 20);
108 return strlcpy(buf,
"global", 20);
110 return strlcpy(buf,
"percpu", 20);
112 return strlcpy(buf,
"pernode", 20);
114 return sprintf(buf,
"%d", *ip);
119 &svc_pool_map.mode, 0644);
126 svc_pool_map_choose_mode(
void)
158 svc_pool_map_alloc_arrays(
struct svc_pool_map *
m,
unsigned int maxpools)
160 m->to_pool = kcalloc(maxpools,
sizeof(
unsigned int),
GFP_KERNEL);
163 m->pool_to = kcalloc(maxpools,
sizeof(
unsigned int),
GFP_KERNEL);
181 svc_pool_map_init_percpu(
struct svc_pool_map *m)
184 unsigned int pidx = 0;
188 err = svc_pool_map_alloc_arrays(m, maxpools);
194 m->to_pool[
cpu] = pidx;
195 m->pool_to[pidx] =
cpu;
209 svc_pool_map_init_pernode(
struct svc_pool_map *m)
212 unsigned int pidx = 0;
216 err = svc_pool_map_alloc_arrays(m, maxpools);
223 m->to_pool[
node] = pidx;
224 m->pool_to[pidx] =
node;
239 svc_pool_map_get(
void)
241 struct svc_pool_map *m = &svc_pool_map;
252 m->mode = svc_pool_map_choose_mode();
256 npools = svc_pool_map_init_percpu(m);
259 npools = svc_pool_map_init_pernode(m);
283 svc_pool_map_put(
void)
285 struct svc_pool_map *m = &svc_pool_map;
301 static int svc_pool_map_get_node(
unsigned int pidx)
303 const struct svc_pool_map *m = &svc_pool_map;
309 return m->pool_to[pidx];
320 struct svc_pool_map *m = &svc_pool_map;
321 unsigned int node = m->pool_to[pidx];
351 struct svc_pool_map *m = &svc_pool_map;
352 unsigned int pidx = 0;
362 pidx = m->to_pool[
cpu];
381 svc_unregister(serv, net);
388 svc_unregister(serv, net);
393 static int svc_uses_rpcbind(
struct svc_serv *serv)
399 for (i = 0; i < progp->
pg_nvers; i++) {
402 if (progp->
pg_vers[i]->vs_hidden == 0)
412 if (!svc_uses_rpcbind(serv))
427 unsigned int xdrsize;
430 if (!(serv = kzalloc(
sizeof(*serv),
GFP_KERNEL)))
444 for (vers=0; vers<prog->
pg_nvers ; vers++)
449 if (prog->
pg_vers[vers]->vs_xdrsize > xdrsize)
450 xdrsize = prog->
pg_vers[vers]->vs_xdrsize;
472 dprintk(
"svc: initialising pool %u for %s\n",
482 if (svc_uses_rpcbind(serv) && (!serv->
sv_shutdown))
490 void (*shutdown)(
struct svc_serv *serv,
struct net *net))
492 return __svc_create(prog, bufsize, 1,
shutdown);
498 void (*shutdown)(
struct svc_serv *serv,
struct net *net),
502 unsigned int npools = svc_pool_map_get();
504 serv = __svc_create(prog, bufsize, npools,
shutdown);
540 dprintk(
"svc: svc_destroy(%s, %d)\n",
550 printk(
"svc_destroy: no threads for serv=%p!\n", serv);
576 svc_init_buffer(
struct svc_rqst *rqstp,
unsigned int size,
int node)
578 unsigned int pages, arghi;
581 if (svc_is_backchannel(rqstp))
603 svc_release_buffer(
struct svc_rqst *rqstp)
617 rqstp = kzalloc_node(
sizeof(*rqstp),
GFP_KERNEL, node);
627 spin_unlock_bh(&pool->
sp_lock);
639 if (!svc_init_buffer(rqstp, serv->
sv_max_mesg, node))
666 choose_victim(
struct svc_serv *serv,
struct svc_pool *pool,
unsigned int *state)
680 spin_unlock_bh(&pool->
sp_lock);
694 list_del_init(&rqstp->
rq_all);
697 spin_unlock_bh(&pool->
sp_lock);
732 spin_unlock_bh(&pool->
sp_lock);
736 while (nrservs > 0) {
738 chosen_pool = choose_pool(serv, pool, &state);
740 node = svc_pool_map_get_node(chosen_pool->
sp_id);
743 error = PTR_ERR(rqstp);
751 error = PTR_ERR(task);
759 svc_pool_map_set_cpumask(task, chosen_pool->
sp_id);
765 while (nrservs < 0 &&
766 (task = choose_victim(serv, pool, &state)) !=
NULL) {
785 svc_release_buffer(rqstp);
793 spin_unlock_bh(&pool->
sp_lock);
813 static int __svc_rpcb_register4(
struct net *net,
const u32 program,
816 const unsigned short port)
821 .sin_port =
htons(port),
828 netid = RPCBIND_NETID_UDP;
831 netid = RPCBIND_NETID_TCP;
838 (
const struct sockaddr *)&sin, netid);
845 error =
rpcb_register(net, program, version, protocol, port);
850 #if IS_ENABLED(CONFIG_IPV6)
861 static int __svc_rpcb_register6(
struct net *net,
const u32 program,
863 const unsigned short protocol,
864 const unsigned short port)
869 .sin6_port =
htons(port),
876 netid = RPCBIND_NETID_UDP6;
879 netid = RPCBIND_NETID_TCP6;
886 (
const struct sockaddr *)&sin6, netid);
905 static int __svc_register(
struct net *net,
const char *progname,
906 const u32 program,
const u32 version,
908 const unsigned short protocol,
909 const unsigned short port)
915 error = __svc_rpcb_register4(net, program, version,
918 #if IS_ENABLED(CONFIG_IPV6)
920 error = __svc_rpcb_register6(net, program, version,
927 "service (errno %d).\n", progname, version, -error);
942 const int family,
const unsigned short proto,
943 const unsigned short port)
949 BUG_ON(proto == 0 && port == 0);
952 for (i = 0; i < progp->
pg_nvers; i++) {
956 dprintk(
"svc: svc_register(%sv%d, %s, %u, %u)%s\n",
963 " (but not telling portmap)" :
"");
965 if (progp->
pg_vers[i]->vs_hidden)
969 i, family, proto, port);
985 static void __svc_unregister(
struct net *net,
const u32 program,
const u32 version,
986 const char *progname)
999 dprintk(
"svc: %s(%sv%u), error %d\n",
1000 __func__, progname, version, error);
1011 static void svc_unregister(
const struct svc_serv *serv,
struct net *net)
1014 unsigned long flags;
1020 for (i = 0; i < progp->
pg_nvers; i++) {
1023 if (progp->
pg_vers[i]->vs_hidden)
1026 dprintk(
"svc: attempting to unregister %sv%u\n",
1034 spin_unlock_irqrestore(&
current->sighand->siglock, flags);
1070 u32 prog, vers, proc;
1075 rpc_stat = rpc_success;
1087 rqstp->
rq_xprt->xpt_ops->xpo_prep_reply_hdr(rqstp);
1089 svc_putu32(resv, rqstp->
rq_xid);
1091 vers = svc_getnl(argv);
1104 rqstp->
rq_prog = prog = svc_getnl(argv);
1105 rqstp->
rq_vers = vers = svc_getnl(argv);
1106 rqstp->
rq_proc = proc = svc_getnl(argv);
1121 if (auth_res == SVC_OK && progp) {
1122 auth_stat = rpc_autherr_badcred;
1131 rpc_stat = rpc_system_err;
1148 !(versp = progp->
pg_vers[vers]))
1151 procp = versp->
vs_proc + proc;
1161 svc_putnl(resv, RPC_SUCCESS);
1191 if (*statp == rpc_success &&
1194 dprintk(
"svc: failed to encode reply\n");
1196 *statp = rpc_system_err;
1199 dprintk(
"svc: calling dispatcher\n");
1209 if (*statp != rpc_success)
1226 dprintk(
"svc: svc_process dropit\n");
1230 svc_printk(rqstp,
"short len %Zd, dropping request\n",
1244 dprintk(
"svc: authentication failed (%d)\n",
ntohl(auth_stat));
1247 xdr_ressize_check(rqstp, reply_statp);
1250 svc_putnl(resv,
ntohl(auth_stat));
1254 dprintk(
"svc: unknown program %d\n", prog);
1256 svc_putnl(resv, RPC_PROG_UNAVAIL);
1260 svc_printk(rqstp,
"unknown version (%d for prog %d, %s)\n",
1264 svc_putnl(resv, RPC_PROG_MISMATCH);
1270 svc_printk(rqstp,
"unknown procedure (%d)\n", proc);
1273 svc_putnl(resv, RPC_PROC_UNAVAIL);
1277 svc_printk(rqstp,
"failed to decode args\n");
1279 rpc_stat = rpc_garbage_args;
1282 svc_putnl(resv,
ntohl(rpc_stat));
1307 rqstp->
rq_res.page_base = 0;
1308 rqstp->
rq_res.page_len = 0;
1311 rqstp->
rq_res.tail[0].iov_len = 0;
1313 rqstp->
rq_xid = svc_getu32(argv);
1315 dir = svc_getnl(argv);
1318 svc_printk(rqstp,
"bad direction %d, dropping request\n", dir);
1325 if (svc_process_common(rqstp, argv, resv))
1333 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1346 rqstp->
rq_xprt = serv->sv_bc_xprt;
1347 rqstp->
rq_xid = req->rq_xid;
1348 rqstp->
rq_prot = req->rq_xprt->prot;
1351 rqstp->
rq_addrlen =
sizeof(req->rq_xprt->addr);
1372 if (svc_process_common(rqstp, argv, resv)) {
1374 sizeof(req->rq_snd_buf));
1392 if (rqstp->
rq_server->sv_max_payload < max)