blob: dfef9621375eb176a6c07aaff5149b83dc63e12a [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
2 * net/sched/act_gact.c Generic actions
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
13#include <linux/types.h>
14#include <linux/kernel.h>
15#include <linux/string.h>
16#include <linux/errno.h>
17#include <linux/skbuff.h>
18#include <linux/rtnetlink.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <net/netlink.h>
22#include <net/pkt_sched.h>
23#include <linux/tc_act/tc_gact.h>
24#include <net/tc_act/tc_gact.h>
25
26static unsigned int gact_net_id;
27static struct tc_action_ops act_gact_ops;
28
29#ifdef CONFIG_GACT_PROB
30static int gact_net_rand(struct tcf_gact *gact)
31{
32 smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
33 if (prandom_u32() % gact->tcfg_pval)
34 return gact->tcf_action;
35 return gact->tcfg_paction;
36}
37
38static int gact_determ(struct tcf_gact *gact)
39{
40 u32 pack = atomic_inc_return(&gact->packets);
41
42 smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
43 if (pack % gact->tcfg_pval)
44 return gact->tcf_action;
45 return gact->tcfg_paction;
46}
47
48typedef int (*g_rand)(struct tcf_gact *gact);
49static g_rand gact_rand[MAX_RAND] = { NULL, gact_net_rand, gact_determ };
50#endif /* CONFIG_GACT_PROB */
51
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
57static int tcf_gact_init(struct net *net, struct nlattr *nla,
58 struct nlattr *est, struct tc_action **a,
59 int ovr, int bind, bool rtnl_held,
60 struct netlink_ext_ack *extack)
61{
62 struct tc_action_net *tn = net_generic(net, gact_net_id);
63 struct nlattr *tb[TCA_GACT_MAX + 1];
64 struct tc_gact *parm;
65 struct tcf_gact *gact;
66 int ret = 0;
67 u32 index;
68 int err;
69#ifdef CONFIG_GACT_PROB
70 struct tc_gact_p *p_parm = NULL;
71#endif
72
73 if (nla == NULL)
74 return -EINVAL;
75
76 err = nla_parse_nested(tb, TCA_GACT_MAX, nla, gact_policy, NULL);
77 if (err < 0)
78 return err;
79
80 if (tb[TCA_GACT_PARMS] == NULL)
81 return -EINVAL;
82 parm = nla_data(tb[TCA_GACT_PARMS]);
83 index = parm->index;
84
85#ifndef CONFIG_GACT_PROB
86 if (tb[TCA_GACT_PROB] != NULL)
87 return -EOPNOTSUPP;
88#else
89 if (tb[TCA_GACT_PROB]) {
90 p_parm = nla_data(tb[TCA_GACT_PROB]);
91 if (p_parm->ptype >= MAX_RAND)
92 return -EINVAL;
93 }
94#endif
95
96 err = tcf_idr_check_alloc(tn, &index, a, bind);
97 if (!err) {
98 ret = tcf_idr_create(tn, index, est, a,
99 &act_gact_ops, bind, true);
100 if (ret) {
101 tcf_idr_cleanup(tn, index);
102 return ret;
103 }
104 ret = ACT_P_CREATED;
105 } else if (err > 0) {
106 if (bind)/* dont override defaults */
107 return 0;
108 if (!ovr) {
109 tcf_idr_release(*a, bind);
110 return -EEXIST;
111 }
112 } else {
113 return err;
114 }
115
116 gact = to_gact(*a);
117
118 spin_lock_bh(&gact->tcf_lock);
119 gact->tcf_action = parm->action;
120#ifdef CONFIG_GACT_PROB
121 if (p_parm) {
122 gact->tcfg_paction = p_parm->paction;
123 gact->tcfg_pval = max_t(u16, 1, p_parm->pval);
124 /* Make sure tcfg_pval is written before tcfg_ptype
125 * coupled with smp_rmb() in gact_net_rand() & gact_determ()
126 */
127 smp_wmb();
128 gact->tcfg_ptype = p_parm->ptype;
129 }
130#endif
131 spin_unlock_bh(&gact->tcf_lock);
132
133 if (ret == ACT_P_CREATED)
134 tcf_idr_insert(tn, *a);
135 return ret;
136}
137
138static int tcf_gact_act(struct sk_buff *skb, const struct tc_action *a,
139 struct tcf_result *res)
140{
141 struct tcf_gact *gact = to_gact(a);
142 int action = READ_ONCE(gact->tcf_action);
143
144#ifdef CONFIG_GACT_PROB
145 {
146 u32 ptype = READ_ONCE(gact->tcfg_ptype);
147
148 if (ptype)
149 action = gact_rand[ptype](gact);
150 }
151#endif
152 bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), skb);
153 if (action == TC_ACT_SHOT)
154 qstats_drop_inc(this_cpu_ptr(gact->common.cpu_qstats));
155
156 tcf_lastuse_update(&gact->tcf_tm);
157
158 return action;
159}
160
161static void tcf_gact_stats_update(struct tc_action *a, u64 bytes, u32 packets,
162 u64 lastuse)
163{
164 struct tcf_gact *gact = to_gact(a);
165 int action = READ_ONCE(gact->tcf_action);
166 struct tcf_t *tm = &gact->tcf_tm;
167
168 _bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), bytes,
169 packets);
170 if (action == TC_ACT_SHOT)
171 this_cpu_ptr(gact->common.cpu_qstats)->drops += packets;
172
173 tm->lastuse = max_t(u64, tm->lastuse, lastuse);
174}
175
176static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a,
177 int bind, int ref)
178{
179 unsigned char *b = skb_tail_pointer(skb);
180 struct tcf_gact *gact = to_gact(a);
181 struct tc_gact opt = {
182 .index = gact->tcf_index,
183 .refcnt = refcount_read(&gact->tcf_refcnt) - ref,
184 .bindcnt = atomic_read(&gact->tcf_bindcnt) - bind,
185 };
186 struct tcf_t t;
187
188 spin_lock_bh(&gact->tcf_lock);
189 opt.action = gact->tcf_action;
190 if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt))
191 goto nla_put_failure;
192#ifdef CONFIG_GACT_PROB
193 if (gact->tcfg_ptype) {
194 struct tc_gact_p p_opt = {
195 .paction = gact->tcfg_paction,
196 .pval = gact->tcfg_pval,
197 .ptype = gact->tcfg_ptype,
198 };
199
200 if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt))
201 goto nla_put_failure;
202 }
203#endif
204 tcf_tm_dump(&t, &gact->tcf_tm);
205 if (nla_put_64bit(skb, TCA_GACT_TM, sizeof(t), &t, TCA_GACT_PAD))
206 goto nla_put_failure;
207 spin_unlock_bh(&gact->tcf_lock);
208
209 return skb->len;
210
211nla_put_failure:
212 spin_unlock_bh(&gact->tcf_lock);
213 nlmsg_trim(skb, b);
214 return -1;
215}
216
217static int tcf_gact_walker(struct net *net, struct sk_buff *skb,
218 struct netlink_callback *cb, int type,
219 const struct tc_action_ops *ops,
220 struct netlink_ext_ack *extack)
221{
222 struct tc_action_net *tn = net_generic(net, gact_net_id);
223
224 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
225}
226
227static int tcf_gact_search(struct net *net, struct tc_action **a, u32 index,
228 struct netlink_ext_ack *extack)
229{
230 struct tc_action_net *tn = net_generic(net, gact_net_id);
231
232 return tcf_idr_search(tn, a, index);
233}
234
235static size_t tcf_gact_get_fill_size(const struct tc_action *act)
236{
237 size_t sz = nla_total_size(sizeof(struct tc_gact)); /* TCA_GACT_PARMS */
238
239#ifdef CONFIG_GACT_PROB
240 if (to_gact(act)->tcfg_ptype)
241 /* TCA_GACT_PROB */
242 sz += nla_total_size(sizeof(struct tc_gact_p));
243#endif
244
245 return sz;
246}
247
248static struct tc_action_ops act_gact_ops = {
249 .kind = "gact",
250 .type = TCA_ACT_GACT,
251 .owner = THIS_MODULE,
252 .act = tcf_gact_act,
253 .stats_update = tcf_gact_stats_update,
254 .dump = tcf_gact_dump,
255 .init = tcf_gact_init,
256 .walk = tcf_gact_walker,
257 .lookup = tcf_gact_search,
258 .get_fill_size = tcf_gact_get_fill_size,
259 .size = sizeof(struct tcf_gact),
260};
261
262static __net_init int gact_init_net(struct net *net)
263{
264 struct tc_action_net *tn = net_generic(net, gact_net_id);
265
266 return tc_action_net_init(net, tn, &act_gact_ops);
267}
268
269static void __net_exit gact_exit_net(struct list_head *net_list)
270{
271 tc_action_net_exit(net_list, gact_net_id);
272}
273
274static struct pernet_operations gact_net_ops = {
275 .init = gact_init_net,
276 .exit_batch = gact_exit_net,
277 .id = &gact_net_id,
278 .size = sizeof(struct tc_action_net),
279};
280
281MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
282MODULE_DESCRIPTION("Generic Classifier actions");
283MODULE_LICENSE("GPL");
284
285static int __init gact_init_module(void)
286{
287#ifdef CONFIG_GACT_PROB
288 pr_info("GACT probability on\n");
289#else
290 pr_info("GACT probability NOT on\n");
291#endif
292
293 return tcf_register_action(&act_gact_ops, &gact_net_ops);
294}
295
296static void __exit gact_cleanup_module(void)
297{
298 tcf_unregister_action(&act_gact_ops, &gact_net_ops);
299}
300
301module_init(gact_init_module);
302module_exit(gact_cleanup_module);