]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - net/sched/act_gact.c
Merge tag 'batadv-next-for-davem-20180919' of git://git.open-mesh.org/linux-merge
[mirror_ubuntu-eoan-kernel.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
0190c1d4
VB
94 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
95 if (!err) {
65a206c0
CM
96 ret = tcf_idr_create(tn, parm->index, est, a,
97 &act_gact_ops, bind, true);
0190c1d4
VB
98 if (ret) {
99 tcf_idr_cleanup(tn, parm->index);
86062033 100 return ret;
0190c1d4 101 }
1da177e4 102 ret = ACT_P_CREATED;
0190c1d4 103 } else if (err > 0) {
1a29321e
JHS
104 if (bind)/* dont override defaults */
105 return 0;
4e8ddd7f
VB
106 if (!ovr) {
107 tcf_idr_release(*a, bind);
1da177e4 108 return -EEXIST;
4e8ddd7f 109 }
0190c1d4
VB
110 } else {
111 return err;
1da177e4
LT
112 }
113
a85a970a 114 gact = to_gact(*a);
e9ce1cd3 115
653cd284 116 spin_lock_bh(&gact->tcf_lock);
e9ce1cd3 117 gact->tcf_action = parm->action;
1da177e4 118#ifdef CONFIG_GACT_PROB
696ecdc1 119 if (p_parm) {
e9ce1cd3 120 gact->tcfg_paction = p_parm->paction;
cef5ecf9
ED
121 gact->tcfg_pval = max_t(u16, 1, p_parm->pval);
122 /* Make sure tcfg_pval is written before tcfg_ptype
123 * coupled with smp_rmb() in gact_net_rand() & gact_determ()
124 */
125 smp_wmb();
e9ce1cd3 126 gact->tcfg_ptype = p_parm->ptype;
1da177e4
LT
127 }
128#endif
653cd284 129 spin_unlock_bh(&gact->tcf_lock);
e8917f43 130
1da177e4 131 if (ret == ACT_P_CREATED)
65a206c0 132 tcf_idr_insert(tn, *a);
1da177e4
LT
133 return ret;
134}
135
1740005e
JHS
136static int tcf_gact_act(struct sk_buff *skb, const struct tc_action *a,
137 struct tcf_result *res)
1da177e4 138{
a85a970a 139 struct tcf_gact *gact = to_gact(a);
56e5d1ca 140 int action = READ_ONCE(gact->tcf_action);
1da177e4 141
1da177e4 142#ifdef CONFIG_GACT_PROB
8f2ae965
ED
143 {
144 u32 ptype = READ_ONCE(gact->tcfg_ptype);
145
146 if (ptype)
147 action = gact_rand[ptype](gact);
148 }
1da177e4 149#endif
56e5d1ca 150 bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), skb);
1da177e4 151 if (action == TC_ACT_SHOT)
56e5d1ca
ED
152 qstats_drop_inc(this_cpu_ptr(gact->common.cpu_qstats));
153
154 tcf_lastuse_update(&gact->tcf_tm);
1da177e4
LT
155
156 return action;
157}
158
9fea47d9
AV
159static void tcf_gact_stats_update(struct tc_action *a, u64 bytes, u32 packets,
160 u64 lastuse)
161{
a85a970a 162 struct tcf_gact *gact = to_gact(a);
9fea47d9
AV
163 int action = READ_ONCE(gact->tcf_action);
164 struct tcf_t *tm = &gact->tcf_tm;
165
5a7a5555
JHS
166 _bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), bytes,
167 packets);
9fea47d9
AV
168 if (action == TC_ACT_SHOT)
169 this_cpu_ptr(gact->common.cpu_qstats)->drops += packets;
170
3bb23421 171 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
9fea47d9
AV
172}
173
0b0f43fe
JHS
174static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a,
175 int bind, int ref)
1da177e4 176{
27a884dc 177 unsigned char *b = skb_tail_pointer(skb);
a85a970a 178 struct tcf_gact *gact = to_gact(a);
1c40be12
ED
179 struct tc_gact opt = {
180 .index = gact->tcf_index,
036bb443
VB
181 .refcnt = refcount_read(&gact->tcf_refcnt) - ref,
182 .bindcnt = atomic_read(&gact->tcf_bindcnt) - bind,
1c40be12 183 };
1da177e4
LT
184 struct tcf_t t;
185
653cd284 186 spin_lock_bh(&gact->tcf_lock);
e8917f43 187 opt.action = gact->tcf_action;
1b34ec43
DM
188 if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt))
189 goto nla_put_failure;
1da177e4 190#ifdef CONFIG_GACT_PROB
e9ce1cd3 191 if (gact->tcfg_ptype) {
1c40be12
ED
192 struct tc_gact_p p_opt = {
193 .paction = gact->tcfg_paction,
194 .pval = gact->tcfg_pval,
195 .ptype = gact->tcfg_ptype,
196 };
197
1b34ec43
DM
198 if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt))
199 goto nla_put_failure;
1da177e4
LT
200 }
201#endif
48d8ee16 202 tcf_tm_dump(&t, &gact->tcf_tm);
9854518e 203 if (nla_put_64bit(skb, TCA_GACT_TM, sizeof(t), &t, TCA_GACT_PAD))
1b34ec43 204 goto nla_put_failure;
653cd284 205 spin_unlock_bh(&gact->tcf_lock);
e8917f43 206
1da177e4
LT
207 return skb->len;
208
7ba699c6 209nla_put_failure:
653cd284 210 spin_unlock_bh(&gact->tcf_lock);
dc5fc579 211 nlmsg_trim(skb, b);
1da177e4
LT
212 return -1;
213}
214
ddf97ccd
WC
215static int tcf_gact_walker(struct net *net, struct sk_buff *skb,
216 struct netlink_callback *cb, int type,
41780105
AA
217 const struct tc_action_ops *ops,
218 struct netlink_ext_ack *extack)
ddf97ccd
WC
219{
220 struct tc_action_net *tn = net_generic(net, gact_net_id);
221
b3620145 222 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
ddf97ccd
WC
223}
224
f061b48c 225static int tcf_gact_search(struct net *net, struct tc_action **a, u32 index)
ddf97ccd
WC
226{
227 struct tc_action_net *tn = net_generic(net, gact_net_id);
228
65a206c0 229 return tcf_idr_search(tn, a, index);
ddf97ccd
WC
230}
231
9c5c9c57
RM
232static size_t tcf_gact_get_fill_size(const struct tc_action *act)
233{
234 size_t sz = nla_total_size(sizeof(struct tc_gact)); /* TCA_GACT_PARMS */
235
236#ifdef CONFIG_GACT_PROB
237 if (to_gact(act)->tcfg_ptype)
238 /* TCA_GACT_PROB */
239 sz += nla_total_size(sizeof(struct tc_gact_p));
240#endif
241
242 return sz;
243}
244
1da177e4
LT
245static struct tc_action_ops act_gact_ops = {
246 .kind = "gact",
247 .type = TCA_ACT_GACT,
1da177e4 248 .owner = THIS_MODULE,
1740005e 249 .act = tcf_gact_act,
9fea47d9 250 .stats_update = tcf_gact_stats_update,
1da177e4 251 .dump = tcf_gact_dump,
1da177e4 252 .init = tcf_gact_init,
ddf97ccd
WC
253 .walk = tcf_gact_walker,
254 .lookup = tcf_gact_search,
9c5c9c57 255 .get_fill_size = tcf_gact_get_fill_size,
a85a970a 256 .size = sizeof(struct tcf_gact),
ddf97ccd
WC
257};
258
259static __net_init int gact_init_net(struct net *net)
260{
261 struct tc_action_net *tn = net_generic(net, gact_net_id);
262
c7e460ce 263 return tc_action_net_init(tn, &act_gact_ops);
ddf97ccd
WC
264}
265
039af9c6 266static void __net_exit gact_exit_net(struct list_head *net_list)
ddf97ccd 267{
039af9c6 268 tc_action_net_exit(net_list, gact_net_id);
ddf97ccd
WC
269}
270
271static struct pernet_operations gact_net_ops = {
272 .init = gact_init_net,
039af9c6 273 .exit_batch = gact_exit_net,
ddf97ccd
WC
274 .id = &gact_net_id,
275 .size = sizeof(struct tc_action_net),
1da177e4
LT
276};
277
278MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
279MODULE_DESCRIPTION("Generic Classifier actions");
280MODULE_LICENSE("GPL");
281
e9ce1cd3 282static int __init gact_init_module(void)
1da177e4
LT
283{
284#ifdef CONFIG_GACT_PROB
cc7ec456 285 pr_info("GACT probability on\n");
1da177e4 286#else
cc7ec456 287 pr_info("GACT probability NOT on\n");
1da177e4 288#endif
ddf97ccd
WC
289
290 return tcf_register_action(&act_gact_ops, &gact_net_ops);
1da177e4
LT
291}
292
e9ce1cd3 293static void __exit gact_cleanup_module(void)
1da177e4 294{
ddf97ccd 295 tcf_unregister_action(&act_gact_ops, &gact_net_ops);
1da177e4
LT
296}
297
298module_init(gact_init_module);
299module_exit(gact_cleanup_module);