]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/sched/act_gact.c
Merge tag 'dmaengine-4.3-rc1' of git://git.infradead.org/users/vkoul/slave-dma
[mirror_ubuntu-bionic-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
e9ce1cd3 26#define GACT_TAB_MASK 15
1da177e4
LT
27
28#ifdef CONFIG_GACT_PROB
e9ce1cd3 29static int gact_net_rand(struct tcf_gact *gact)
1da177e4 30{
cef5ecf9
ED
31 smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
32 if (prandom_u32() % gact->tcfg_pval)
e9ce1cd3
DM
33 return gact->tcf_action;
34 return gact->tcfg_paction;
1da177e4
LT
35}
36
e9ce1cd3 37static int gact_determ(struct tcf_gact *gact)
1da177e4 38{
cc6510a9
ED
39 u32 pack = atomic_inc_return(&gact->packets);
40
cef5ecf9 41 smp_rmb(); /* coupled with smp_wmb() in tcf_gact_init() */
cc6510a9 42 if (pack % gact->tcfg_pval)
e9ce1cd3
DM
43 return gact->tcf_action;
44 return gact->tcfg_paction;
1da177e4
LT
45}
46
e9ce1cd3 47typedef int (*g_rand)(struct tcf_gact *gact);
cc7ec456 48static g_rand gact_rand[MAX_RAND] = { NULL, gact_net_rand, gact_determ };
e9ce1cd3 49#endif /* CONFIG_GACT_PROB */
1da177e4 50
53b2bf3f
PM
51static const struct nla_policy gact_policy[TCA_GACT_MAX + 1] = {
52 [TCA_GACT_PARMS] = { .len = sizeof(struct tc_gact) },
53 [TCA_GACT_PROB] = { .len = sizeof(struct tc_gact_p) },
54};
55
c1b52739
BL
56static int tcf_gact_init(struct net *net, struct nlattr *nla,
57 struct nlattr *est, struct tc_action *a,
58 int ovr, int bind)
1da177e4 59{
7ba699c6 60 struct nlattr *tb[TCA_GACT_MAX + 1];
1da177e4 61 struct tc_gact *parm;
e9ce1cd3 62 struct tcf_gact *gact;
1da177e4 63 int ret = 0;
cee63723 64 int err;
696ecdc1
HS
65#ifdef CONFIG_GACT_PROB
66 struct tc_gact_p *p_parm = NULL;
67#endif
1da177e4 68
cee63723 69 if (nla == NULL)
1da177e4
LT
70 return -EINVAL;
71
53b2bf3f 72 err = nla_parse_nested(tb, TCA_GACT_MAX, nla, gact_policy);
cee63723
PM
73 if (err < 0)
74 return err;
75
53b2bf3f 76 if (tb[TCA_GACT_PARMS] == NULL)
1da177e4 77 return -EINVAL;
7ba699c6 78 parm = nla_data(tb[TCA_GACT_PARMS]);
1da177e4 79
53b2bf3f 80#ifndef CONFIG_GACT_PROB
7ba699c6 81 if (tb[TCA_GACT_PROB] != NULL)
1da177e4 82 return -EOPNOTSUPP;
696ecdc1
HS
83#else
84 if (tb[TCA_GACT_PROB]) {
85 p_parm = nla_data(tb[TCA_GACT_PROB]);
86 if (p_parm->ptype >= MAX_RAND)
87 return -EINVAL;
88 }
1da177e4
LT
89#endif
90
86062033 91 if (!tcf_hash_check(parm->index, a, bind)) {
519c818e 92 ret = tcf_hash_create(parm->index, est, a, sizeof(*gact),
56e5d1ca 93 bind, true);
86062033
WC
94 if (ret)
95 return ret;
1da177e4
LT
96 ret = ACT_P_CREATED;
97 } else {
1a29321e
JHS
98 if (bind)/* dont override defaults */
99 return 0;
86062033 100 tcf_hash_release(a, bind);
1a29321e 101 if (!ovr)
1da177e4 102 return -EEXIST;
1da177e4
LT
103 }
104
86062033 105 gact = to_gact(a);
e9ce1cd3 106
56e5d1ca 107 ASSERT_RTNL();
e9ce1cd3 108 gact->tcf_action = parm->action;
1da177e4 109#ifdef CONFIG_GACT_PROB
696ecdc1 110 if (p_parm) {
e9ce1cd3 111 gact->tcfg_paction = p_parm->paction;
cef5ecf9
ED
112 gact->tcfg_pval = max_t(u16, 1, p_parm->pval);
113 /* Make sure tcfg_pval is written before tcfg_ptype
114 * coupled with smp_rmb() in gact_net_rand() & gact_determ()
115 */
116 smp_wmb();
e9ce1cd3 117 gact->tcfg_ptype = p_parm->ptype;
1da177e4
LT
118 }
119#endif
1da177e4 120 if (ret == ACT_P_CREATED)
86062033 121 tcf_hash_insert(a);
1da177e4
LT
122 return ret;
123}
124
dc7f9f6e
ED
125static int tcf_gact(struct sk_buff *skb, const struct tc_action *a,
126 struct tcf_result *res)
1da177e4 127{
e9ce1cd3 128 struct tcf_gact *gact = a->priv;
56e5d1ca 129 int action = READ_ONCE(gact->tcf_action);
1da177e4 130
1da177e4 131#ifdef CONFIG_GACT_PROB
8f2ae965
ED
132 {
133 u32 ptype = READ_ONCE(gact->tcfg_ptype);
134
135 if (ptype)
136 action = gact_rand[ptype](gact);
137 }
1da177e4 138#endif
56e5d1ca 139 bstats_cpu_update(this_cpu_ptr(gact->common.cpu_bstats), skb);
1da177e4 140 if (action == TC_ACT_SHOT)
56e5d1ca
ED
141 qstats_drop_inc(this_cpu_ptr(gact->common.cpu_qstats));
142
143 tcf_lastuse_update(&gact->tcf_tm);
1da177e4
LT
144
145 return action;
146}
147
e9ce1cd3 148static int tcf_gact_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
1da177e4 149{
27a884dc 150 unsigned char *b = skb_tail_pointer(skb);
e9ce1cd3 151 struct tcf_gact *gact = a->priv;
1c40be12
ED
152 struct tc_gact opt = {
153 .index = gact->tcf_index,
154 .refcnt = gact->tcf_refcnt - ref,
155 .bindcnt = gact->tcf_bindcnt - bind,
156 .action = gact->tcf_action,
157 };
1da177e4
LT
158 struct tcf_t t;
159
1b34ec43
DM
160 if (nla_put(skb, TCA_GACT_PARMS, sizeof(opt), &opt))
161 goto nla_put_failure;
1da177e4 162#ifdef CONFIG_GACT_PROB
e9ce1cd3 163 if (gact->tcfg_ptype) {
1c40be12
ED
164 struct tc_gact_p p_opt = {
165 .paction = gact->tcfg_paction,
166 .pval = gact->tcfg_pval,
167 .ptype = gact->tcfg_ptype,
168 };
169
1b34ec43
DM
170 if (nla_put(skb, TCA_GACT_PROB, sizeof(p_opt), &p_opt))
171 goto nla_put_failure;
1da177e4
LT
172 }
173#endif
e9ce1cd3
DM
174 t.install = jiffies_to_clock_t(jiffies - gact->tcf_tm.install);
175 t.lastuse = jiffies_to_clock_t(jiffies - gact->tcf_tm.lastuse);
176 t.expires = jiffies_to_clock_t(gact->tcf_tm.expires);
1b34ec43
DM
177 if (nla_put(skb, TCA_GACT_TM, sizeof(t), &t))
178 goto nla_put_failure;
1da177e4
LT
179 return skb->len;
180
7ba699c6 181nla_put_failure:
dc5fc579 182 nlmsg_trim(skb, b);
1da177e4
LT
183 return -1;
184}
185
186static struct tc_action_ops act_gact_ops = {
187 .kind = "gact",
188 .type = TCA_ACT_GACT,
1da177e4
LT
189 .owner = THIS_MODULE,
190 .act = tcf_gact,
191 .dump = tcf_gact_dump,
1da177e4 192 .init = tcf_gact_init,
1da177e4
LT
193};
194
195MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
196MODULE_DESCRIPTION("Generic Classifier actions");
197MODULE_LICENSE("GPL");
198
e9ce1cd3 199static int __init gact_init_module(void)
1da177e4
LT
200{
201#ifdef CONFIG_GACT_PROB
cc7ec456 202 pr_info("GACT probability on\n");
1da177e4 203#else
cc7ec456 204 pr_info("GACT probability NOT on\n");
1da177e4 205#endif
4f1e9d89 206 return tcf_register_action(&act_gact_ops, GACT_TAB_MASK);
1da177e4
LT
207}
208
e9ce1cd3 209static void __exit gact_cleanup_module(void)
1da177e4
LT
210{
211 tcf_unregister_action(&act_gact_ops);
212}
213
214module_init(gact_init_module);
215module_exit(gact_cleanup_module);