]> Pileus Git - ~andy/linux/blob - net/sched/act_police.c
a719fdff575eaa0917f06ceec76237c50f78175e
[~andy/linux] / net / sched / act_police.c
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
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <linux/errno.h>
18 #include <linux/skbuff.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/init.h>
21 #include <linux/slab.h>
22 #include <net/act_api.h>
23 #include <net/netlink.h>
24
25 struct tcf_police {
26         struct tcf_common       common;
27         int                     tcfp_result;
28         u32                     tcfp_ewma_rate;
29         s64                     tcfp_burst;
30         u32                     tcfp_mtu;
31         s64                     tcfp_toks;
32         s64                     tcfp_ptoks;
33         s64                     tcfp_mtu_ptoks;
34         s64                     tcfp_t_c;
35         struct psched_ratecfg   rate;
36         bool                    rate_present;
37         struct psched_ratecfg   peak;
38         bool                    peak_present;
39 };
40 #define to_police(pc)   \
41         container_of(pc, struct tcf_police, common)
42
43 #define POL_TAB_MASK     15
44 static struct tcf_hashinfo police_hash_info;
45
46 /* old policer structure from before tc actions */
47 struct tc_police_compat {
48         u32                     index;
49         int                     action;
50         u32                     limit;
51         u32                     burst;
52         u32                     mtu;
53         struct tc_ratespec      rate;
54         struct tc_ratespec      peakrate;
55 };
56
57 /* Each policer is serialized by its individual spinlock */
58
59 static int tcf_act_police_walker(struct sk_buff *skb, struct netlink_callback *cb,
60                               int type, struct tc_action *a)
61 {
62         struct hlist_head *head;
63         struct tcf_common *p;
64         int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
65         struct nlattr *nest;
66
67         spin_lock_bh(&police_hash_info.lock);
68
69         s_i = cb->args[0];
70
71         for (i = 0; i < (POL_TAB_MASK + 1); i++) {
72                 head = &police_hash_info.htab[tcf_hash(i, POL_TAB_MASK)];
73
74                 hlist_for_each_entry_rcu(p, head, tcfc_head) {
75                         index++;
76                         if (index < s_i)
77                                 continue;
78                         a->priv = p;
79                         a->order = index;
80                         nest = nla_nest_start(skb, a->order);
81                         if (nest == NULL)
82                                 goto nla_put_failure;
83                         if (type == RTM_DELACTION)
84                                 err = tcf_action_dump_1(skb, a, 0, 1);
85                         else
86                                 err = tcf_action_dump_1(skb, a, 0, 0);
87                         if (err < 0) {
88                                 index--;
89                                 nla_nest_cancel(skb, nest);
90                                 goto done;
91                         }
92                         nla_nest_end(skb, nest);
93                         n_i++;
94                 }
95         }
96 done:
97         spin_unlock_bh(&police_hash_info.lock);
98         if (n_i)
99                 cb->args[0] += n_i;
100         return n_i;
101
102 nla_put_failure:
103         nla_nest_cancel(skb, nest);
104         goto done;
105 }
106
107 static void tcf_police_destroy(struct tcf_police *p)
108 {
109         spin_lock_bh(&police_hash_info.lock);
110         hlist_del(&p->tcf_head);
111         spin_unlock_bh(&police_hash_info.lock);
112         gen_kill_estimator(&p->tcf_bstats,
113                            &p->tcf_rate_est);
114         /*
115          * gen_estimator est_timer() might access p->tcf_lock
116          * or bstats, wait a RCU grace period before freeing p
117          */
118         kfree_rcu(p, tcf_rcu);
119 }
120
121 static const struct nla_policy police_policy[TCA_POLICE_MAX + 1] = {
122         [TCA_POLICE_RATE]       = { .len = TC_RTAB_SIZE },
123         [TCA_POLICE_PEAKRATE]   = { .len = TC_RTAB_SIZE },
124         [TCA_POLICE_AVRATE]     = { .type = NLA_U32 },
125         [TCA_POLICE_RESULT]     = { .type = NLA_U32 },
126 };
127
128 static int tcf_act_police_locate(struct net *net, struct nlattr *nla,
129                                  struct nlattr *est, struct tc_action *a,
130                                  int ovr, int bind)
131 {
132         unsigned int h;
133         int ret = 0, err;
134         struct nlattr *tb[TCA_POLICE_MAX + 1];
135         struct tc_police *parm;
136         struct tcf_police *police;
137         struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
138         int size;
139
140         if (nla == NULL)
141                 return -EINVAL;
142
143         err = nla_parse_nested(tb, TCA_POLICE_MAX, nla, police_policy);
144         if (err < 0)
145                 return err;
146
147         if (tb[TCA_POLICE_TBF] == NULL)
148                 return -EINVAL;
149         size = nla_len(tb[TCA_POLICE_TBF]);
150         if (size != sizeof(*parm) && size != sizeof(struct tc_police_compat))
151                 return -EINVAL;
152         parm = nla_data(tb[TCA_POLICE_TBF]);
153
154         if (parm->index) {
155                 struct tcf_common *pc;
156
157                 pc = tcf_hash_lookup(parm->index, &police_hash_info);
158                 if (pc != NULL) {
159                         a->priv = pc;
160                         police = to_police(pc);
161                         if (bind) {
162                                 police->tcf_bindcnt += 1;
163                                 police->tcf_refcnt += 1;
164                                 return 0;
165                         }
166                         if (ovr)
167                                 goto override;
168                         /* not replacing */
169                         return -EEXIST;
170                 }
171         }
172
173         police = kzalloc(sizeof(*police), GFP_KERNEL);
174         if (police == NULL)
175                 return -ENOMEM;
176         ret = ACT_P_CREATED;
177         police->tcf_refcnt = 1;
178         spin_lock_init(&police->tcf_lock);
179         if (bind)
180                 police->tcf_bindcnt = 1;
181 override:
182         if (parm->rate.rate) {
183                 err = -ENOMEM;
184                 R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE]);
185                 if (R_tab == NULL)
186                         goto failure;
187
188                 if (parm->peakrate.rate) {
189                         P_tab = qdisc_get_rtab(&parm->peakrate,
190                                                tb[TCA_POLICE_PEAKRATE]);
191                         if (P_tab == NULL)
192                                 goto failure;
193                 }
194         }
195
196         spin_lock_bh(&police->tcf_lock);
197         if (est) {
198                 err = gen_replace_estimator(&police->tcf_bstats,
199                                             &police->tcf_rate_est,
200                                             &police->tcf_lock, est);
201                 if (err)
202                         goto failure_unlock;
203         } else if (tb[TCA_POLICE_AVRATE] &&
204                    (ret == ACT_P_CREATED ||
205                     !gen_estimator_active(&police->tcf_bstats,
206                                           &police->tcf_rate_est))) {
207                 err = -EINVAL;
208                 goto failure_unlock;
209         }
210
211         /* No failure allowed after this point */
212         police->tcfp_mtu = parm->mtu;
213         if (police->tcfp_mtu == 0) {
214                 police->tcfp_mtu = ~0;
215                 if (R_tab)
216                         police->tcfp_mtu = 255 << R_tab->rate.cell_log;
217         }
218         if (R_tab) {
219                 police->rate_present = true;
220                 psched_ratecfg_precompute(&police->rate, &R_tab->rate, 0);
221                 qdisc_put_rtab(R_tab);
222         } else {
223                 police->rate_present = false;
224         }
225         if (P_tab) {
226                 police->peak_present = true;
227                 psched_ratecfg_precompute(&police->peak, &P_tab->rate, 0);
228                 qdisc_put_rtab(P_tab);
229         } else {
230                 police->peak_present = false;
231         }
232
233         if (tb[TCA_POLICE_RESULT])
234                 police->tcfp_result = nla_get_u32(tb[TCA_POLICE_RESULT]);
235         police->tcfp_burst = PSCHED_TICKS2NS(parm->burst);
236         police->tcfp_toks = police->tcfp_burst;
237         if (police->peak_present) {
238                 police->tcfp_mtu_ptoks = (s64) psched_l2t_ns(&police->peak,
239                                                              police->tcfp_mtu);
240                 police->tcfp_ptoks = police->tcfp_mtu_ptoks;
241         }
242         police->tcf_action = parm->action;
243
244         if (tb[TCA_POLICE_AVRATE])
245                 police->tcfp_ewma_rate = nla_get_u32(tb[TCA_POLICE_AVRATE]);
246
247         spin_unlock_bh(&police->tcf_lock);
248         if (ret != ACT_P_CREATED)
249                 return ret;
250
251         police->tcfp_t_c = ktime_to_ns(ktime_get());
252         police->tcf_index = parm->index ? parm->index :
253                 tcf_hash_new_index(&police_hash_info);
254         h = tcf_hash(police->tcf_index, POL_TAB_MASK);
255         spin_lock_bh(&police_hash_info.lock);
256         hlist_add_head(&police->tcf_head, &police_hash_info.htab[h]);
257         spin_unlock_bh(&police_hash_info.lock);
258
259         a->priv = police;
260         return ret;
261
262 failure_unlock:
263         spin_unlock_bh(&police->tcf_lock);
264 failure:
265         qdisc_put_rtab(P_tab);
266         qdisc_put_rtab(R_tab);
267         if (ret == ACT_P_CREATED)
268                 kfree(police);
269         return err;
270 }
271
272 static int tcf_act_police_cleanup(struct tc_action *a, int bind)
273 {
274         struct tcf_police *p = a->priv;
275         int ret = 0;
276
277         if (p != NULL) {
278                 if (bind)
279                         p->tcf_bindcnt--;
280
281                 p->tcf_refcnt--;
282                 if (p->tcf_refcnt <= 0 && !p->tcf_bindcnt) {
283                         tcf_police_destroy(p);
284                         ret = 1;
285                 }
286         }
287         return ret;
288 }
289
290 static int tcf_act_police(struct sk_buff *skb, const struct tc_action *a,
291                           struct tcf_result *res)
292 {
293         struct tcf_police *police = a->priv;
294         s64 now;
295         s64 toks;
296         s64 ptoks = 0;
297
298         spin_lock(&police->tcf_lock);
299
300         bstats_update(&police->tcf_bstats, skb);
301
302         if (police->tcfp_ewma_rate &&
303             police->tcf_rate_est.bps >= police->tcfp_ewma_rate) {
304                 police->tcf_qstats.overlimits++;
305                 if (police->tcf_action == TC_ACT_SHOT)
306                         police->tcf_qstats.drops++;
307                 spin_unlock(&police->tcf_lock);
308                 return police->tcf_action;
309         }
310
311         if (qdisc_pkt_len(skb) <= police->tcfp_mtu) {
312                 if (!police->rate_present) {
313                         spin_unlock(&police->tcf_lock);
314                         return police->tcfp_result;
315                 }
316
317                 now = ktime_to_ns(ktime_get());
318                 toks = min_t(s64, now - police->tcfp_t_c,
319                              police->tcfp_burst);
320                 if (police->peak_present) {
321                         ptoks = toks + police->tcfp_ptoks;
322                         if (ptoks > police->tcfp_mtu_ptoks)
323                                 ptoks = police->tcfp_mtu_ptoks;
324                         ptoks -= (s64) psched_l2t_ns(&police->peak,
325                                                      qdisc_pkt_len(skb));
326                 }
327                 toks += police->tcfp_toks;
328                 if (toks > police->tcfp_burst)
329                         toks = police->tcfp_burst;
330                 toks -= (s64) psched_l2t_ns(&police->rate, qdisc_pkt_len(skb));
331                 if ((toks|ptoks) >= 0) {
332                         police->tcfp_t_c = now;
333                         police->tcfp_toks = toks;
334                         police->tcfp_ptoks = ptoks;
335                         spin_unlock(&police->tcf_lock);
336                         return police->tcfp_result;
337                 }
338         }
339
340         police->tcf_qstats.overlimits++;
341         if (police->tcf_action == TC_ACT_SHOT)
342                 police->tcf_qstats.drops++;
343         spin_unlock(&police->tcf_lock);
344         return police->tcf_action;
345 }
346
347 static int
348 tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
349 {
350         unsigned char *b = skb_tail_pointer(skb);
351         struct tcf_police *police = a->priv;
352         struct tc_police opt = {
353                 .index = police->tcf_index,
354                 .action = police->tcf_action,
355                 .mtu = police->tcfp_mtu,
356                 .burst = PSCHED_NS2TICKS(police->tcfp_burst),
357                 .refcnt = police->tcf_refcnt - ref,
358                 .bindcnt = police->tcf_bindcnt - bind,
359         };
360
361         if (police->rate_present)
362                 psched_ratecfg_getrate(&opt.rate, &police->rate);
363         if (police->peak_present)
364                 psched_ratecfg_getrate(&opt.peakrate, &police->peak);
365         if (nla_put(skb, TCA_POLICE_TBF, sizeof(opt), &opt))
366                 goto nla_put_failure;
367         if (police->tcfp_result &&
368             nla_put_u32(skb, TCA_POLICE_RESULT, police->tcfp_result))
369                 goto nla_put_failure;
370         if (police->tcfp_ewma_rate &&
371             nla_put_u32(skb, TCA_POLICE_AVRATE, police->tcfp_ewma_rate))
372                 goto nla_put_failure;
373         return skb->len;
374
375 nla_put_failure:
376         nlmsg_trim(skb, b);
377         return -1;
378 }
379
380 MODULE_AUTHOR("Alexey Kuznetsov");
381 MODULE_DESCRIPTION("Policing actions");
382 MODULE_LICENSE("GPL");
383
384 static struct tc_action_ops act_police_ops = {
385         .kind           =       "police",
386         .hinfo          =       &police_hash_info,
387         .type           =       TCA_ID_POLICE,
388         .capab          =       TCA_CAP_NONE,
389         .owner          =       THIS_MODULE,
390         .act            =       tcf_act_police,
391         .dump           =       tcf_act_police_dump,
392         .cleanup        =       tcf_act_police_cleanup,
393         .init           =       tcf_act_police_locate,
394         .walk           =       tcf_act_police_walker
395 };
396
397 static int __init
398 police_init_module(void)
399 {
400         int err = tcf_hashinfo_init(&police_hash_info, POL_TAB_MASK);
401         if (err)
402                 return err;
403         err = tcf_register_action(&act_police_ops);
404         if (err)
405                 tcf_hashinfo_destroy(&police_hash_info);
406         return err;
407 }
408
409 static void __exit
410 police_cleanup_module(void)
411 {
412         tcf_hashinfo_destroy(&police_hash_info);
413         tcf_unregister_action(&act_police_ops);
414 }
415
416 module_init(police_init_module);
417 module_exit(police_cleanup_module);