]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - net/sched/act_police.c
[NET_SCHED]: Convert classifiers from rtnetlink to new netlink API
[thirdparty/kernel/stable.git] / net / sched / act_police.c
CommitLineData
1da177e4
LT
1/*
2 * net/sched/police.c Input police filter.
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 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 * J Hadi Salim (action changes)
11 */
12
1da177e4
LT
13#include <linux/module.h>
14#include <linux/types.h>
15#include <linux/kernel.h>
1da177e4 16#include <linux/string.h>
1da177e4 17#include <linux/errno.h>
1da177e4 18#include <linux/skbuff.h>
1da177e4
LT
19#include <linux/rtnetlink.h>
20#include <linux/init.h>
1da177e4 21#include <net/act_api.h>
dc5fc579 22#include <net/netlink.h>
1da177e4 23
e9bef55d
JDB
24#define L2T(p,L) qdisc_l2t((p)->tcfp_R_tab, L)
25#define L2T_P(p,L) qdisc_l2t((p)->tcfp_P_tab, L)
1da177e4 26
e9ce1cd3
DM
27#define POL_TAB_MASK 15
28static struct tcf_common *tcf_police_ht[POL_TAB_MASK + 1];
29static u32 police_idx_gen;
30static DEFINE_RWLOCK(police_lock);
1da177e4 31
e9ce1cd3
DM
32static struct tcf_hashinfo police_hash_info = {
33 .htab = tcf_police_ht,
34 .hmask = POL_TAB_MASK,
35 .lock = &police_lock,
36};
1da177e4 37
1e9b3d53
PM
38/* old policer structure from before tc actions */
39struct tc_police_compat
40{
41 u32 index;
42 int action;
43 u32 limit;
44 u32 burst;
45 u32 mtu;
46 struct tc_ratespec rate;
47 struct tc_ratespec peakrate;
48};
49
e9ce1cd3 50/* Each policer is serialized by its individual spinlock */
1da177e4 51
83b950c8 52static int tcf_act_police_walker(struct sk_buff *skb, struct netlink_callback *cb,
10297b99 53 int type, struct tc_action *a)
1da177e4 54{
e9ce1cd3 55 struct tcf_common *p;
1da177e4
LT
56 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
57 struct rtattr *r;
58
e1e992e5 59 read_lock_bh(&police_lock);
1da177e4
LT
60
61 s_i = cb->args[0];
62
e9ce1cd3
DM
63 for (i = 0; i < (POL_TAB_MASK + 1); i++) {
64 p = tcf_police_ht[tcf_hash(i, POL_TAB_MASK)];
1da177e4 65
e9ce1cd3 66 for (; p; p = p->tcfc_next) {
1da177e4
LT
67 index++;
68 if (index < s_i)
69 continue;
70 a->priv = p;
71 a->order = index;
27a884dc 72 r = (struct rtattr *)skb_tail_pointer(skb);
1da177e4
LT
73 RTA_PUT(skb, a->order, 0, NULL);
74 if (type == RTM_DELACTION)
75 err = tcf_action_dump_1(skb, a, 0, 1);
76 else
77 err = tcf_action_dump_1(skb, a, 0, 0);
78 if (err < 0) {
79 index--;
dc5fc579 80 nlmsg_trim(skb, r);
1da177e4
LT
81 goto done;
82 }
27a884dc 83 r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
1da177e4
LT
84 n_i++;
85 }
86 }
87done:
e1e992e5 88 read_unlock_bh(&police_lock);
1da177e4
LT
89 if (n_i)
90 cb->args[0] += n_i;
91 return n_i;
92
93rtattr_failure:
dc5fc579 94 nlmsg_trim(skb, r);
1da177e4
LT
95 goto done;
96}
1da177e4 97
c3bc7cff 98static void tcf_police_destroy(struct tcf_police *p)
1da177e4 99{
e9ce1cd3
DM
100 unsigned int h = tcf_hash(p->tcf_index, POL_TAB_MASK);
101 struct tcf_common **p1p;
10297b99 102
e9ce1cd3
DM
103 for (p1p = &tcf_police_ht[h]; *p1p; p1p = &(*p1p)->tcfc_next) {
104 if (*p1p == &p->common) {
1da177e4 105 write_lock_bh(&police_lock);
e9ce1cd3 106 *p1p = p->tcf_next;
1da177e4 107 write_unlock_bh(&police_lock);
e9ce1cd3
DM
108 gen_kill_estimator(&p->tcf_bstats,
109 &p->tcf_rate_est);
e9ce1cd3
DM
110 if (p->tcfp_R_tab)
111 qdisc_put_rtab(p->tcfp_R_tab);
112 if (p->tcfp_P_tab)
113 qdisc_put_rtab(p->tcfp_P_tab);
1da177e4
LT
114 kfree(p);
115 return;
116 }
117 }
118 BUG_TRAP(0);
119}
120
1da177e4 121static int tcf_act_police_locate(struct rtattr *rta, struct rtattr *est,
10297b99 122 struct tc_action *a, int ovr, int bind)
1da177e4
LT
123{
124 unsigned h;
125 int ret = 0, err;
126 struct rtattr *tb[TCA_POLICE_MAX];
127 struct tc_police *parm;
e9ce1cd3 128 struct tcf_police *police;
1da177e4 129 struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
1e9b3d53 130 int size;
1da177e4
LT
131
132 if (rta == NULL || rtattr_parse_nested(tb, TCA_POLICE_MAX, rta) < 0)
133 return -EINVAL;
134
1e9b3d53
PM
135 if (tb[TCA_POLICE_TBF-1] == NULL)
136 return -EINVAL;
137 size = RTA_PAYLOAD(tb[TCA_POLICE_TBF-1]);
138 if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
1da177e4
LT
139 return -EINVAL;
140 parm = RTA_DATA(tb[TCA_POLICE_TBF-1]);
141
142 if (tb[TCA_POLICE_RESULT-1] != NULL &&
143 RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
144 return -EINVAL;
145 if (tb[TCA_POLICE_RESULT-1] != NULL &&
146 RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32))
147 return -EINVAL;
148
e9ce1cd3
DM
149 if (parm->index) {
150 struct tcf_common *pc;
151
152 pc = tcf_hash_lookup(parm->index, &police_hash_info);
153 if (pc != NULL) {
154 a->priv = pc;
155 police = to_police(pc);
156 if (bind) {
157 police->tcf_bindcnt += 1;
158 police->tcf_refcnt += 1;
159 }
160 if (ovr)
161 goto override;
162 return ret;
1da177e4 163 }
1da177e4
LT
164 }
165
e9ce1cd3
DM
166 police = kzalloc(sizeof(*police), GFP_KERNEL);
167 if (police == NULL)
1da177e4 168 return -ENOMEM;
1da177e4 169 ret = ACT_P_CREATED;
e9ce1cd3
DM
170 police->tcf_refcnt = 1;
171 spin_lock_init(&police->tcf_lock);
1da177e4 172 if (bind)
e9ce1cd3 173 police->tcf_bindcnt = 1;
1da177e4
LT
174override:
175 if (parm->rate.rate) {
176 err = -ENOMEM;
1e90474c 177 R_tab = qdisc_get_rtab(&parm->rate, (struct nlattr *)tb[TCA_POLICE_RATE-1]);
1da177e4
LT
178 if (R_tab == NULL)
179 goto failure;
180 if (parm->peakrate.rate) {
181 P_tab = qdisc_get_rtab(&parm->peakrate,
1e90474c 182 (struct nlattr *)tb[TCA_POLICE_PEAKRATE-1]);
e9ce1cd3 183 if (P_tab == NULL) {
1da177e4
LT
184 qdisc_put_rtab(R_tab);
185 goto failure;
186 }
187 }
188 }
189 /* No failure allowed after this point */
e9ce1cd3 190 spin_lock_bh(&police->tcf_lock);
1da177e4 191 if (R_tab != NULL) {
e9ce1cd3
DM
192 qdisc_put_rtab(police->tcfp_R_tab);
193 police->tcfp_R_tab = R_tab;
1da177e4
LT
194 }
195 if (P_tab != NULL) {
e9ce1cd3
DM
196 qdisc_put_rtab(police->tcfp_P_tab);
197 police->tcfp_P_tab = P_tab;
1da177e4
LT
198 }
199
200 if (tb[TCA_POLICE_RESULT-1])
e9ce1cd3
DM
201 police->tcfp_result = *(u32*)RTA_DATA(tb[TCA_POLICE_RESULT-1]);
202 police->tcfp_toks = police->tcfp_burst = parm->burst;
203 police->tcfp_mtu = parm->mtu;
204 if (police->tcfp_mtu == 0) {
205 police->tcfp_mtu = ~0;
206 if (police->tcfp_R_tab)
207 police->tcfp_mtu = 255<<police->tcfp_R_tab->rate.cell_log;
1da177e4 208 }
e9ce1cd3
DM
209 if (police->tcfp_P_tab)
210 police->tcfp_ptoks = L2T_P(police, police->tcfp_mtu);
211 police->tcf_action = parm->action;
1da177e4 212
1da177e4 213 if (tb[TCA_POLICE_AVRATE-1])
e9ce1cd3
DM
214 police->tcfp_ewma_rate =
215 *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]);
1da177e4 216 if (est)
e9ce1cd3
DM
217 gen_replace_estimator(&police->tcf_bstats,
218 &police->tcf_rate_est,
1e90474c 219 &police->tcf_lock, (struct nlattr *)est);
1da177e4 220
e9ce1cd3 221 spin_unlock_bh(&police->tcf_lock);
1da177e4
LT
222 if (ret != ACT_P_CREATED)
223 return ret;
224
3bebcda2 225 police->tcfp_t_c = psched_get_time();
e9ce1cd3
DM
226 police->tcf_index = parm->index ? parm->index :
227 tcf_hash_new_index(&police_idx_gen, &police_hash_info);
228 h = tcf_hash(police->tcf_index, POL_TAB_MASK);
1da177e4 229 write_lock_bh(&police_lock);
e9ce1cd3
DM
230 police->tcf_next = tcf_police_ht[h];
231 tcf_police_ht[h] = &police->common;
1da177e4
LT
232 write_unlock_bh(&police_lock);
233
e9ce1cd3 234 a->priv = police;
1da177e4
LT
235 return ret;
236
237failure:
238 if (ret == ACT_P_CREATED)
e9ce1cd3 239 kfree(police);
1da177e4
LT
240 return err;
241}
242
243static int tcf_act_police_cleanup(struct tc_action *a, int bind)
244{
e9ce1cd3 245 struct tcf_police *p = a->priv;
c3bc7cff 246 int ret = 0;
1da177e4 247
c3bc7cff
PM
248 if (p != NULL) {
249 if (bind)
250 p->tcf_bindcnt--;
251
252 p->tcf_refcnt--;
253 if (p->tcf_refcnt <= 0 && !p->tcf_bindcnt) {
254 tcf_police_destroy(p);
255 ret = 1;
256 }
257 }
258 return ret;
1da177e4
LT
259}
260
f43c5a0d 261static int tcf_act_police(struct sk_buff *skb, struct tc_action *a,
10297b99 262 struct tcf_result *res)
1da177e4 263{
e9ce1cd3 264 struct tcf_police *police = a->priv;
1da177e4 265 psched_time_t now;
1da177e4
LT
266 long toks;
267 long ptoks = 0;
268
e9ce1cd3 269 spin_lock(&police->tcf_lock);
1da177e4 270
e9ce1cd3
DM
271 police->tcf_bstats.bytes += skb->len;
272 police->tcf_bstats.packets++;
1da177e4 273
e9ce1cd3
DM
274 if (police->tcfp_ewma_rate &&
275 police->tcf_rate_est.bps >= police->tcfp_ewma_rate) {
276 police->tcf_qstats.overlimits++;
277 spin_unlock(&police->tcf_lock);
278 return police->tcf_action;
1da177e4 279 }
1da177e4 280
e9ce1cd3
DM
281 if (skb->len <= police->tcfp_mtu) {
282 if (police->tcfp_R_tab == NULL) {
283 spin_unlock(&police->tcf_lock);
284 return police->tcfp_result;
1da177e4
LT
285 }
286
3bebcda2 287 now = psched_get_time();
03cc45c0
PM
288 toks = psched_tdiff_bounded(now, police->tcfp_t_c,
289 police->tcfp_burst);
e9ce1cd3
DM
290 if (police->tcfp_P_tab) {
291 ptoks = toks + police->tcfp_ptoks;
292 if (ptoks > (long)L2T_P(police, police->tcfp_mtu))
293 ptoks = (long)L2T_P(police, police->tcfp_mtu);
294 ptoks -= L2T_P(police, skb->len);
1da177e4 295 }
e9ce1cd3
DM
296 toks += police->tcfp_toks;
297 if (toks > (long)police->tcfp_burst)
298 toks = police->tcfp_burst;
299 toks -= L2T(police, skb->len);
1da177e4 300 if ((toks|ptoks) >= 0) {
e9ce1cd3
DM
301 police->tcfp_t_c = now;
302 police->tcfp_toks = toks;
303 police->tcfp_ptoks = ptoks;
304 spin_unlock(&police->tcf_lock);
305 return police->tcfp_result;
1da177e4
LT
306 }
307 }
308
e9ce1cd3
DM
309 police->tcf_qstats.overlimits++;
310 spin_unlock(&police->tcf_lock);
311 return police->tcf_action;
1da177e4
LT
312}
313
314static int
315tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
316{
27a884dc 317 unsigned char *b = skb_tail_pointer(skb);
e9ce1cd3 318 struct tcf_police *police = a->priv;
1da177e4 319 struct tc_police opt;
e9ce1cd3
DM
320
321 opt.index = police->tcf_index;
322 opt.action = police->tcf_action;
323 opt.mtu = police->tcfp_mtu;
324 opt.burst = police->tcfp_burst;
325 opt.refcnt = police->tcf_refcnt - ref;
326 opt.bindcnt = police->tcf_bindcnt - bind;
327 if (police->tcfp_R_tab)
328 opt.rate = police->tcfp_R_tab->rate;
1da177e4
LT
329 else
330 memset(&opt.rate, 0, sizeof(opt.rate));
e9ce1cd3
DM
331 if (police->tcfp_P_tab)
332 opt.peakrate = police->tcfp_P_tab->rate;
1da177e4
LT
333 else
334 memset(&opt.peakrate, 0, sizeof(opt.peakrate));
335 RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt);
e9ce1cd3
DM
336 if (police->tcfp_result)
337 RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int),
338 &police->tcfp_result);
e9ce1cd3
DM
339 if (police->tcfp_ewma_rate)
340 RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &police->tcfp_ewma_rate);
1da177e4
LT
341 return skb->len;
342
343rtattr_failure:
dc5fc579 344 nlmsg_trim(skb, b);
1da177e4
LT
345 return -1;
346}
347
348MODULE_AUTHOR("Alexey Kuznetsov");
349MODULE_DESCRIPTION("Policing actions");
350MODULE_LICENSE("GPL");
351
352static struct tc_action_ops act_police_ops = {
353 .kind = "police",
e9ce1cd3 354 .hinfo = &police_hash_info,
1da177e4
LT
355 .type = TCA_ID_POLICE,
356 .capab = TCA_CAP_NONE,
357 .owner = THIS_MODULE,
358 .act = tcf_act_police,
359 .dump = tcf_act_police_dump,
360 .cleanup = tcf_act_police_cleanup,
e9ce1cd3 361 .lookup = tcf_hash_search,
1da177e4 362 .init = tcf_act_police_locate,
83b950c8 363 .walk = tcf_act_police_walker
1da177e4
LT
364};
365
366static int __init
367police_init_module(void)
368{
369 return tcf_register_action(&act_police_ops);
370}
371
372static void __exit
373police_cleanup_module(void)
374{
375 tcf_unregister_action(&act_police_ops);
376}
377
378module_init(police_init_module);
379module_exit(police_cleanup_module);