]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - net/sched/act_simple.c
net: Move all TC actions identifiers to one place
[mirror_ubuntu-eoan-kernel.git] / net / sched / act_simple.c
CommitLineData
db753079 1/*
0c6965dd 2 * net/sched/act_simple.c Simple example of an action
db753079
JHS
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 *
fa1b1cff 9 * Authors: Jamal Hadi Salim (2005-8)
db753079
JHS
10 *
11 */
12
cbdbf00a 13#include <linux/module.h>
5a0e3ad6 14#include <linux/slab.h>
cbdbf00a 15#include <linux/init.h>
db753079 16#include <linux/kernel.h>
db753079
JHS
17#include <linux/skbuff.h>
18#include <linux/rtnetlink.h>
dc5fc579 19#include <net/netlink.h>
db753079
JHS
20#include <net/pkt_sched.h>
21
db753079
JHS
22#include <linux/tc_act/tc_defact.h>
23#include <net/tc_act/tc_defact.h>
24
c7d03a00 25static unsigned int simp_net_id;
a85a970a 26static struct tc_action_ops act_simp_ops;
ddf97ccd 27
fa1b1cff 28#define SIMP_MAX_DATA 32
798de374
JHS
29static int tcf_simp_act(struct sk_buff *skb, const struct tc_action *a,
30 struct tcf_result *res)
db753079 31{
a85a970a 32 struct tcf_defact *d = to_defact(a);
db753079 33
e9ce1cd3 34 spin_lock(&d->tcf_lock);
9c4a4e48 35 tcf_lastuse_update(&d->tcf_tm);
bfe0d029 36 bstats_update(&d->tcf_bstats, skb);
db753079 37
10297b99
YH
38 /* print policy string followed by _ then packet count
39 * Example if this was the 3rd packet and the string was "hello"
40 * then it would look like "hello_3" (without quotes)
cc7ec456 41 */
6ff9c364 42 pr_info("simple: %s_%d\n",
e9ce1cd3
DM
43 (char *)d->tcfd_defdata, d->tcf_bstats.packets);
44 spin_unlock(&d->tcf_lock);
45 return d->tcf_action;
46}
47
9a63b255 48static void tcf_simp_release(struct tc_action *a)
e9ce1cd3 49{
86062033 50 struct tcf_defact *d = to_defact(a);
a5b5c958 51 kfree(d->tcfd_defdata);
e9ce1cd3
DM
52}
53
8d499533 54static int alloc_defdata(struct tcf_defact *d, const struct nlattr *defdata)
e9ce1cd3 55{
0eff683f 56 d->tcfd_defdata = kzalloc(SIMP_MAX_DATA, GFP_KERNEL);
e9ce1cd3
DM
57 if (unlikely(!d->tcfd_defdata))
58 return -ENOMEM;
8d499533 59 nla_strlcpy(d->tcfd_defdata, defdata, SIMP_MAX_DATA);
e9ce1cd3
DM
60 return 0;
61}
62
8d499533 63static void reset_policy(struct tcf_defact *d, const struct nlattr *defdata,
9d1045ad 64 struct tc_defact *p)
e9ce1cd3 65{
9d1045ad
JHS
66 spin_lock_bh(&d->tcf_lock);
67 d->tcf_action = p->action;
68 memset(d->tcfd_defdata, 0, SIMP_MAX_DATA);
8d499533 69 nla_strlcpy(d->tcfd_defdata, defdata, SIMP_MAX_DATA);
9d1045ad 70 spin_unlock_bh(&d->tcf_lock);
e9ce1cd3
DM
71}
72
53b2bf3f
PM
73static const struct nla_policy simple_policy[TCA_DEF_MAX + 1] = {
74 [TCA_DEF_PARMS] = { .len = sizeof(struct tc_defact) },
fa1b1cff 75 [TCA_DEF_DATA] = { .type = NLA_STRING, .len = SIMP_MAX_DATA },
53b2bf3f
PM
76};
77
c1b52739 78static int tcf_simp_init(struct net *net, struct nlattr *nla,
a85a970a 79 struct nlattr *est, struct tc_action **a,
789871bb
VB
80 int ovr, int bind, bool rtnl_held,
81 struct netlink_ext_ack *extack)
e9ce1cd3 82{
ddf97ccd 83 struct tc_action_net *tn = net_generic(net, simp_net_id);
7ba699c6 84 struct nlattr *tb[TCA_DEF_MAX + 1];
e9ce1cd3
DM
85 struct tc_defact *parm;
86 struct tcf_defact *d;
b2313077
WC
87 bool exists = false;
88 int ret = 0, err;
e9ce1cd3 89
cee63723 90 if (nla == NULL)
e9ce1cd3
DM
91 return -EINVAL;
92
fceb6435 93 err = nla_parse_nested(tb, TCA_DEF_MAX, nla, simple_policy, NULL);
cee63723
PM
94 if (err < 0)
95 return err;
96
53b2bf3f 97 if (tb[TCA_DEF_PARMS] == NULL)
e9ce1cd3
DM
98 return -EINVAL;
99
fa1b1cff 100 parm = nla_data(tb[TCA_DEF_PARMS]);
0190c1d4
VB
101 err = tcf_idr_check_alloc(tn, &parm->index, a, bind);
102 if (err < 0)
103 return err;
104 exists = err;
0e5538ab
JHS
105 if (exists && bind)
106 return 0;
107
108 if (tb[TCA_DEF_DATA] == NULL) {
109 if (exists)
65a206c0 110 tcf_idr_release(*a, bind);
0190c1d4
VB
111 else
112 tcf_idr_cleanup(tn, parm->index);
0e5538ab
JHS
113 return -EINVAL;
114 }
115
0e5538ab 116 if (!exists) {
65a206c0
CM
117 ret = tcf_idr_create(tn, parm->index, est, a,
118 &act_simp_ops, bind, false);
0190c1d4
VB
119 if (ret) {
120 tcf_idr_cleanup(tn, parm->index);
86062033 121 return ret;
0190c1d4 122 }
e9ce1cd3 123
a85a970a 124 d = to_defact(*a);
8d499533 125 ret = alloc_defdata(d, tb[TCA_DEF_DATA]);
e9ce1cd3 126 if (ret < 0) {
60e10b3a 127 tcf_idr_release(*a, bind);
e9ce1cd3
DM
128 return ret;
129 }
9d1045ad 130 d->tcf_action = parm->action;
e9ce1cd3
DM
131 ret = ACT_P_CREATED;
132 } else {
a85a970a 133 d = to_defact(*a);
1a29321e 134
4e8ddd7f
VB
135 if (!ovr) {
136 tcf_idr_release(*a, bind);
e9ce1cd3 137 return -EEXIST;
4e8ddd7f 138 }
1a29321e 139
8d499533 140 reset_policy(d, tb[TCA_DEF_DATA], parm);
e9ce1cd3
DM
141 }
142
e9ce1cd3 143 if (ret == ACT_P_CREATED)
65a206c0 144 tcf_idr_insert(tn, *a);
e9ce1cd3
DM
145 return ret;
146}
147
cc7ec456
ED
148static int tcf_simp_dump(struct sk_buff *skb, struct tc_action *a,
149 int bind, int ref)
e9ce1cd3 150{
27a884dc 151 unsigned char *b = skb_tail_pointer(skb);
a85a970a 152 struct tcf_defact *d = to_defact(a);
1c40be12
ED
153 struct tc_defact opt = {
154 .index = d->tcf_index,
036bb443
VB
155 .refcnt = refcount_read(&d->tcf_refcnt) - ref,
156 .bindcnt = atomic_read(&d->tcf_bindcnt) - bind,
1c40be12 157 };
e9ce1cd3
DM
158 struct tcf_t t;
159
5e48180e
VB
160 spin_lock_bh(&d->tcf_lock);
161 opt.action = d->tcf_action;
1b34ec43
DM
162 if (nla_put(skb, TCA_DEF_PARMS, sizeof(opt), &opt) ||
163 nla_put_string(skb, TCA_DEF_DATA, d->tcfd_defdata))
164 goto nla_put_failure;
48d8ee16
JHS
165
166 tcf_tm_dump(&t, &d->tcf_tm);
9854518e 167 if (nla_put_64bit(skb, TCA_DEF_TM, sizeof(t), &t, TCA_DEF_PAD))
1b34ec43 168 goto nla_put_failure;
5e48180e
VB
169 spin_unlock_bh(&d->tcf_lock);
170
e9ce1cd3
DM
171 return skb->len;
172
7ba699c6 173nla_put_failure:
5e48180e 174 spin_unlock_bh(&d->tcf_lock);
dc5fc579 175 nlmsg_trim(skb, b);
e9ce1cd3 176 return -1;
db753079
JHS
177}
178
ddf97ccd
WC
179static int tcf_simp_walker(struct net *net, struct sk_buff *skb,
180 struct netlink_callback *cb, int type,
41780105
AA
181 const struct tc_action_ops *ops,
182 struct netlink_ext_ack *extack)
ddf97ccd
WC
183{
184 struct tc_action_net *tn = net_generic(net, simp_net_id);
185
b3620145 186 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
ddf97ccd
WC
187}
188
f061b48c 189static int tcf_simp_search(struct net *net, struct tc_action **a, u32 index)
ddf97ccd
WC
190{
191 struct tc_action_net *tn = net_generic(net, simp_net_id);
192
65a206c0 193 return tcf_idr_search(tn, a, index);
ddf97ccd
WC
194}
195
db753079 196static struct tc_action_ops act_simp_ops = {
e9ce1cd3 197 .kind = "simple",
e9ce1cd3 198 .type = TCA_ACT_SIMP,
e9ce1cd3 199 .owner = THIS_MODULE,
798de374 200 .act = tcf_simp_act,
e9ce1cd3 201 .dump = tcf_simp_dump,
86062033 202 .cleanup = tcf_simp_release,
e9ce1cd3 203 .init = tcf_simp_init,
ddf97ccd
WC
204 .walk = tcf_simp_walker,
205 .lookup = tcf_simp_search,
a85a970a 206 .size = sizeof(struct tcf_defact),
ddf97ccd
WC
207};
208
209static __net_init int simp_init_net(struct net *net)
210{
211 struct tc_action_net *tn = net_generic(net, simp_net_id);
212
c7e460ce 213 return tc_action_net_init(tn, &act_simp_ops);
ddf97ccd
WC
214}
215
039af9c6 216static void __net_exit simp_exit_net(struct list_head *net_list)
ddf97ccd 217{
039af9c6 218 tc_action_net_exit(net_list, simp_net_id);
ddf97ccd
WC
219}
220
221static struct pernet_operations simp_net_ops = {
222 .init = simp_init_net,
039af9c6 223 .exit_batch = simp_exit_net,
ddf97ccd
WC
224 .id = &simp_net_id,
225 .size = sizeof(struct tc_action_net),
db753079
JHS
226};
227
228MODULE_AUTHOR("Jamal Hadi Salim(2005)");
229MODULE_DESCRIPTION("Simple example action");
230MODULE_LICENSE("GPL");
231
232static int __init simp_init_module(void)
233{
ddf97ccd 234 int ret = tcf_register_action(&act_simp_ops, &simp_net_ops);
db753079 235 if (!ret)
6ff9c364 236 pr_info("Simple TC action Loaded\n");
db753079
JHS
237 return ret;
238}
239
240static void __exit simp_cleanup_module(void)
241{
ddf97ccd 242 tcf_unregister_action(&act_simp_ops, &simp_net_ops);
db753079
JHS
243}
244
245module_init(simp_init_module);
246module_exit(simp_cleanup_module);