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