]> Git Repo - linux.git/blame - net/sched/act_gact.c
net: sched: don't release reference on action overwrite
[linux.git] / net / sched / act_gact.c
CommitLineData
1da177e4 1/*
0c6965dd 2 * net/sched/act_gact.c Generic actions
1da177e4
LT
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * copyright Jamal Hadi Salim (2002-4)
10 *
11 */
12
1da177e4
LT
13#include <linux/types.h>
14#include <linux/kernel.h>
1da177e4 15#include <linux/string.h>
1da177e4 16#include <linux/errno.h>
1da177e4
LT
17#include <linux/skbuff.h>
18#include <linux/rtnetlink.h>
19#include <linux/module.h>
20#include <linux/init.h>
dc5fc579 21#include <net/netlink.h>
1da177e4
LT
22#include <net/pkt_sched.h>
23#include <linux/tc_act/tc_gact.h>
24#include <net/tc_act/tc_gact.h>
25
c7d03a00 26static unsigned int gact_net_id;
a85a970a 27static struct tc_action_ops act_gact_ops;
ddf97ccd 28
1da177e4 29#ifdef CONFIG_GACT_PROB
e9ce1cd3 30static int gact_net_rand(struct tcf_gact *gact)
1da177e4 31{
cef5ecf9
ED
32 smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
33 if (prandom_u32() % gact->tcfg_pval)
e9ce1cd3
DM
34 return gact->tcf_action;
35 return gact->tcfg_paction;
1da177e4
LT
36}
37
e9ce1cd3 38static int gact_determ(struct tcf_gact *gact)
1da177e4 39{
cc6510a9
ED
40 u32 pack = atomic_inc_return(&gact->packets);
41
cef5ecf9 42 smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
cc6510a9 43 if (pack % gact->tcfg_pval)
e9ce1cd3
DM
44 return gact->tcf_action;
45 return gact->tcfg_paction;
1da177e4
LT
46}
47
e9ce1cd3 48typedef int (*g_rand)(struct tcf_gact *gact);
cc7ec456 49static g_rand gact_rand[MAX_RAND] = { NULL, gact_net_rand, gact_determ };
e9ce1cd3 50#endif /* CONFIG_GACT_PROB */
1da177e4 51
53b2bf3f
PM
52static const struct nla_policy gact_policy[TCA_GACT_MAX + 1] = {
53 [TCA_GACT_PARMS] = { .len = sizeof(struct tc_gact) },
54 [TCA_GACT_PROB] = { .len = sizeof(struct tc_gact_p) },
55};
56
c1b52739 57static int tcf_gact_init(struct net *net, struct nlattr *nla,
a85a970a 58 struct nlattr *est, struct tc_action **a,
789871bb
VB
59 int ovr, int bind, bool rtnl_held,
60 struct netlink_ext_ack *extack)
1da177e4 61{
ddf97ccd 62 struct tc_action_net *tn = net_generic(net, gact_net_id);
7ba699c6 63 struct nlattr *tb[TCA_GACT_MAX + 1];
1da177e4 64 struct tc_gact *parm;
e9ce1cd3 65 struct tcf_gact *gact;
1da177e4 66 int ret = 0;
cee63723 67 int err;
696ecdc1
HS
68#ifdef CONFIG_GACT_PROB
69 struct tc_gact_p *p_parm = NULL;
70#endif
1da177e4 71
cee63723 72 if (nla == NULL)
1da177e4
LT
73 return -EINVAL;
74
fceb6435 75 err = nla_parse_nested(tb, TCA_GACT_MAX, nla, gact_policy, NULL);
cee63723
PM
76 if (err < 0)
77 return err;
78
53b2bf3f 79 if (tb[TCA_GACT_PARMS] == NULL)
1da177e4 80 return -EINVAL;
7ba699c6 81 parm = nla_data(tb[TCA_GACT_PARMS]);
1da177e4 82
53b2bf3f 83#ifndef CONFIG_GACT_PROB
7ba699c6 84 if (tb[TCA_GACT_PROB] != NULL)
1da177e4 85 return -EOPNOTSUPP;
696ecdc1
HS
86#else
87 if (tb[TCA_GACT_PROB]) {
88 p_parm = nla_data(tb[TCA_GACT_PROB]);
89 if (p_parm->ptype >= MAX_RAND)
90 return -EINVAL;
91 }
1da177e4
LT
92#endif
93
65a206c0
CM
94 if (!tcf_idr_check(tn, parm->index, a, bind)) {
95 ret = tcf_idr_create(tn, parm->index, est, a,
96 &act_gact_ops, bind, true);
86062033
WC
97 if (ret)
98 return ret;
1da177e4
LT
99 ret = ACT_P_CREATED;
100 } else {
1a29321e
JHS
101 if (bind)/* dont override defaults */
102 return 0;
4e8ddd7f
VB
103 if (!ovr) {
104 tcf_idr_release(*a, bind);
1da177e4 105 return -EEXIST;
4e8ddd7f 106 }
1da177e4
LT
107 }
108
a85a970a 109 gact = to_gact(*a);
e9ce1cd3 110
56e5d1ca 111 ASSERT_RTNL();
e9ce1cd3 112 gact->tcf_action = parm->action;
1da177e4 113#ifdef CONFIG_GACT_PROB
696ecdc1 114 if (p_parm) {
e9ce1cd3 115 gact->tcfg_paction = p_parm->paction;
cef5ecf9
ED
116 gact->tcfg_pval = max_t(u16, 1, p_parm->pval);
117 /* Make sure tcfg_pval is written before tcfg_ptype
118 * coupled with smp_rmb() in gact_net_rand() & gact_determ()
119 */
120 smp_wmb();
e9ce1cd3 121 gact->tcfg_ptype = p_parm->ptype;
1da177e4
LT
122 }
123#endif
1da177e4 124 if (ret == ACT_P_CREATED)
65a206c0 125 tcf_idr_insert(tn, *a);
1da177e4
LT
126 return ret;
127}
128
dc7f9f6e
ED
129static int tcf_gact(struct sk_buff *skb, const struct tc_action *a,
130 struct tcf_result *res)
1da177e4 131{
a85a970a 132 struct tcf_gact *gact = to_gact(a);
56e5d1ca 133 int action = READ_ONCE(gact->tcf_action);
1da177e4 134
1da177e4 135#ifdef CONFIG_GACT_PROB
8f2ae965
ED
136 {
137 u32 ptype = READ_ONCE(gact->tcfg_ptype);
138
139 if (ptype)
140 action = gact_rand[ptype](gact);
141 }
1da177e4 142#endif
56e5d1ca 143 bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), skb);
1da177e4 144 if (action == TC_ACT_SHOT)
56e5d1ca
ED
145 qstats_drop_inc(this_cpu_ptr(gact->common.cpu_qstats));
146
147 tcf_lastuse_update(&gact->tcf_tm);
1da177e4
LT
148
149 return action;
150}
151
9fea47d9
AV
152static void tcf_gact_stats_update(struct tc_action *a, u64 bytes, u32 packets,
153 u64 lastuse)
154{
a85a970a 155 struct tcf_gact *gact = to_gact(a);
9fea47d9
AV
156 int action = READ_ONCE(gact->tcf_action);
157 struct tcf_t *tm = &gact->tcf_tm;
158
5a7a5555
JHS
159 _bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), bytes,
160 packets);
9fea47d9
AV
161 if (action == TC_ACT_SHOT)
162 this_cpu_ptr(gact->common.cpu_qstats)->drops += packets;
163
3bb23421 164 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
9fea47d9
AV
165}
166
0b0f43fe
JHS
167static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a,
168 int bind, int ref)
1da177e4 169{
27a884dc 170 unsigned char *b = skb_tail_pointer(skb);
a85a970a 171 struct tcf_gact *gact = to_gact(a);
1c40be12
ED
172 struct tc_gact opt = {
173 .index = gact->tcf_index,
036bb443
VB
174 .refcnt = refcount_read(&gact->tcf_refcnt) - ref,
175 .bindcnt = atomic_read(&gact->tcf_bindcnt) - bind,
1c40be12
ED
176 .action = gact->tcf_action,
177 };
1da177e4
LT
178 struct tcf_t t;
179
1b34ec43
DM
180 if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt))
181 goto nla_put_failure;
1da177e4 182#ifdef CONFIG_GACT_PROB
e9ce1cd3 183 if (gact->tcfg_ptype) {
1c40be12
ED
184 struct tc_gact_p p_opt = {
185 .paction = gact->tcfg_paction,
186 .pval = gact->tcfg_pval,
187 .ptype = gact->tcfg_ptype,
188 };
189
1b34ec43
DM
190 if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt))
191 goto nla_put_failure;
1da177e4
LT
192 }
193#endif
48d8ee16 194 tcf_tm_dump(&t, &gact->tcf_tm);
9854518e 195 if (nla_put_64bit(skb, TCA_GACT_TM, sizeof(t), &t, TCA_GACT_PAD))
1b34ec43 196 goto nla_put_failure;
1da177e4
LT
197 return skb->len;
198
7ba699c6 199nla_put_failure:
dc5fc579 200 nlmsg_trim(skb, b);
1da177e4
LT
201 return -1;
202}
203
ddf97ccd
WC
204static int tcf_gact_walker(struct net *net, struct sk_buff *skb,
205 struct netlink_callback *cb, int type,
41780105
AA
206 const struct tc_action_ops *ops,
207 struct netlink_ext_ack *extack)
ddf97ccd
WC
208{
209 struct tc_action_net *tn = net_generic(net, gact_net_id);
210
b3620145 211 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
ddf97ccd
WC
212}
213
331a9295
AA
214static int tcf_gact_search(struct net *net, struct tc_action **a, u32 index,
215 struct netlink_ext_ack *extack)
ddf97ccd
WC
216{
217 struct tc_action_net *tn = net_generic(net, gact_net_id);
218
65a206c0 219 return tcf_idr_search(tn, a, index);
ddf97ccd
WC
220}
221
9c5c9c57
RM
222static size_t tcf_gact_get_fill_size(const struct tc_action *act)
223{
224 size_t sz = nla_total_size(sizeof(struct tc_gact)); /* TCA_GACT_PARMS */
225
226#ifdef CONFIG_GACT_PROB
227 if (to_gact(act)->tcfg_ptype)
228 /* TCA_GACT_PROB */
229 sz += nla_total_size(sizeof(struct tc_gact_p));
230#endif
231
232 return sz;
233}
234
b409074e
VB
235static int tcf_gact_delete(struct net *net, u32 index)
236{
237 struct tc_action_net *tn = net_generic(net, gact_net_id);
238
239 return tcf_idr_delete_index(tn, index);
240}
241
1da177e4
LT
242static struct tc_action_ops act_gact_ops = {
243 .kind = "gact",
244 .type = TCA_ACT_GACT,
1da177e4
LT
245 .owner = THIS_MODULE,
246 .act = tcf_gact,
9fea47d9 247 .stats_update = tcf_gact_stats_update,
1da177e4 248 .dump = tcf_gact_dump,
1da177e4 249 .init = tcf_gact_init,
ddf97ccd
WC
250 .walk = tcf_gact_walker,
251 .lookup = tcf_gact_search,
9c5c9c57 252 .get_fill_size = tcf_gact_get_fill_size,
b409074e 253 .delete = tcf_gact_delete,
a85a970a 254 .size = sizeof(struct tcf_gact),
ddf97ccd
WC
255};
256
257static __net_init int gact_init_net(struct net *net)
258{
259 struct tc_action_net *tn = net_generic(net, gact_net_id);
260
c7e460ce 261 return tc_action_net_init(tn, &act_gact_ops);
ddf97ccd
WC
262}
263
039af9c6 264static void __net_exit gact_exit_net(struct list_head *net_list)
ddf97ccd 265{
039af9c6 266 tc_action_net_exit(net_list, gact_net_id);
ddf97ccd
WC
267}
268
269static struct pernet_operations gact_net_ops = {
270 .init = gact_init_net,
039af9c6 271 .exit_batch = gact_exit_net,
ddf97ccd
WC
272 .id = &gact_net_id,
273 .size = sizeof(struct tc_action_net),
1da177e4
LT
274};
275
276MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
277MODULE_DESCRIPTION("Generic Classifier actions");
278MODULE_LICENSE("GPL");
279
e9ce1cd3 280static int __init gact_init_module(void)
1da177e4
LT
281{
282#ifdef CONFIG_GACT_PROB
cc7ec456 283 pr_info("GACT probability on\n");
1da177e4 284#else
cc7ec456 285 pr_info("GACT probability NOT on\n");
1da177e4 286#endif
ddf97ccd
WC
287
288 return tcf_register_action(&act_gact_ops, &gact_net_ops);
1da177e4
LT
289}
290
e9ce1cd3 291static void __exit gact_cleanup_module(void)
1da177e4 292{
ddf97ccd 293 tcf_unregister_action(&act_gact_ops, &gact_net_ops);
1da177e4
LT
294}
295
296module_init(gact_init_module);
297module_exit(gact_cleanup_module);
This page took 1.053258 seconds and 4 git commands to generate.