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