blob: a73e3e6d87ea378f3f72b36b69d3ca3266061949 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/types.h>
15#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/rtnetlink.h>
20#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <net/act_api.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070022#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Jesper Dangaard Brouere9bef552007-09-12 16:35:24 +020024#define L2T(p,L) qdisc_l2t((p)->tcfp_R_tab, L)
25#define L2T_P(p,L) qdisc_l2t((p)->tcfp_P_tab, L)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
David S. Millere9ce1cd2006-08-21 23:54:55 -070027#define POL_TAB_MASK 15
28static struct tcf_common *tcf_police_ht[POL_TAB_MASK + 1];
29static u32 police_idx_gen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static DEFINE_RWLOCK(police_lock);
31
David S. Millere9ce1cd2006-08-21 23:54:55 -070032static struct tcf_hashinfo police_hash_info = {
33 .htab = tcf_police_ht,
34 .hmask = POL_TAB_MASK,
35 .lock = &police_lock,
36};
37
Patrick McHardy1e9b3d52006-11-30 19:54:05 -080038/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/* Each policer is serialized by its individual spinlock */
51
Jamal Hadi Salim83b950c2006-04-06 22:24:22 -070052static int tcf_act_police_walker(struct sk_buff *skb, struct netlink_callback *cb,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +090053 int type, struct tc_action *a)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
David S. Millere9ce1cd2006-08-21 23:54:55 -070055 struct tcf_common *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 int err = 0, index = -1, i = 0, s_i = 0, n_i = 0;
57 struct rtattr *r;
58
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +020059 read_lock_bh(&police_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 s_i = cb->args[0];
62
David S. Millere9ce1cd2006-08-21 23:54:55 -070063 for (i = 0; i < (POL_TAB_MASK + 1); i++) {
64 p = tcf_police_ht[tcf_hash(i, POL_TAB_MASK)];
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
David S. Millere9ce1cd2006-08-21 23:54:55 -070066 for (; p; p = p->tcfc_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 index++;
68 if (index < s_i)
69 continue;
70 a->priv = p;
71 a->order = index;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070072 r = (struct rtattr *)skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 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--;
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070080 nlmsg_trim(skb, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 goto done;
82 }
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070083 r->rta_len = skb_tail_pointer(skb) - (u8 *)r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 n_i++;
85 }
86 }
87done:
Jamal Hadi Salime1e992e2007-09-12 16:32:59 +020088 read_unlock_bh(&police_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 if (n_i)
90 cb->args[0] += n_i;
91 return n_i;
92
93rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070094 nlmsg_trim(skb, r);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 goto done;
96}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Patrick McHardyc3bc7cf2007-07-15 00:03:05 -070098static void tcf_police_destroy(struct tcf_police *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
David S. Millere9ce1cd2006-08-21 23:54:55 -0700100 unsigned int h = tcf_hash(p->tcf_index, POL_TAB_MASK);
101 struct tcf_common **p1p;
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900102
David S. Millere9ce1cd2006-08-21 23:54:55 -0700103 for (p1p = &tcf_police_ht[h]; *p1p; p1p = &(*p1p)->tcfc_next) {
104 if (*p1p == &p->common) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 write_lock_bh(&police_lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700106 *p1p = p->tcf_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 write_unlock_bh(&police_lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700108 gen_kill_estimator(&p->tcf_bstats,
109 &p->tcf_rate_est);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700110 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 kfree(p);
115 return;
116 }
117 }
118 BUG_TRAP(0);
119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static int tcf_act_police_locate(struct rtattr *rta, struct rtattr *est,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900122 struct tc_action *a, int ovr, int bind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 unsigned h;
125 int ret = 0, err;
126 struct rtattr *tb[TCA_POLICE_MAX];
127 struct tc_police *parm;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700128 struct tcf_police *police;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL;
Patrick McHardy1e9b3d52006-11-30 19:54:05 -0800130 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 if (rta == NULL || rtattr_parse_nested(tb, TCA_POLICE_MAX, rta) < 0)
133 return -EINVAL;
134
Patrick McHardy1e9b3d52006-11-30 19:54:05 -0800135 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))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 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
David S. Millere9ce1cd2006-08-21 23:54:55 -0700149 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165
David S. Millere9ce1cd2006-08-21 23:54:55 -0700166 police = kzalloc(sizeof(*police), GFP_KERNEL);
167 if (police == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 ret = ACT_P_CREATED;
David S. Millere9ce1cd2006-08-21 23:54:55 -0700170 police->tcf_refcnt = 1;
171 spin_lock_init(&police->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (bind)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700173 police->tcf_bindcnt = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174override:
175 if (parm->rate.rate) {
176 err = -ENOMEM;
177 R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1]);
178 if (R_tab == NULL)
179 goto failure;
180 if (parm->peakrate.rate) {
181 P_tab = qdisc_get_rtab(&parm->peakrate,
182 tb[TCA_POLICE_PEAKRATE-1]);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700183 if (P_tab == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 qdisc_put_rtab(R_tab);
185 goto failure;
186 }
187 }
188 }
189 /* No failure allowed after this point */
David S. Millere9ce1cd2006-08-21 23:54:55 -0700190 spin_lock_bh(&police->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (R_tab != NULL) {
David S. Millere9ce1cd2006-08-21 23:54:55 -0700192 qdisc_put_rtab(police->tcfp_R_tab);
193 police->tcfp_R_tab = R_tab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195 if (P_tab != NULL) {
David S. Millere9ce1cd2006-08-21 23:54:55 -0700196 qdisc_put_rtab(police->tcfp_P_tab);
197 police->tcfp_P_tab = P_tab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199
200 if (tb[TCA_POLICE_RESULT-1])
David S. Millere9ce1cd2006-08-21 23:54:55 -0700201 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
David S. Millere9ce1cd2006-08-21 23:54:55 -0700209 if (police->tcfp_P_tab)
210 police->tcfp_ptoks = L2T_P(police, police->tcfp_mtu);
211 police->tcf_action = parm->action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (tb[TCA_POLICE_AVRATE-1])
David S. Millere9ce1cd2006-08-21 23:54:55 -0700214 police->tcfp_ewma_rate =
215 *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (est)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700217 gen_replace_estimator(&police->tcf_bstats,
218 &police->tcf_rate_est,
Patrick McHardy4bdf3992007-07-02 22:47:37 -0700219 &police->tcf_lock, est);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
David S. Millere9ce1cd2006-08-21 23:54:55 -0700221 spin_unlock_bh(&police->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (ret != ACT_P_CREATED)
223 return ret;
224
Patrick McHardy3bebcda2007-03-23 11:29:25 -0700225 police->tcfp_t_c = psched_get_time();
David S. Millere9ce1cd2006-08-21 23:54:55 -0700226 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 write_lock_bh(&police_lock);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700230 police->tcf_next = tcf_police_ht[h];
231 tcf_police_ht[h] = &police->common;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 write_unlock_bh(&police_lock);
233
David S. Millere9ce1cd2006-08-21 23:54:55 -0700234 a->priv = police;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return ret;
236
237failure:
238 if (ret == ACT_P_CREATED)
David S. Millere9ce1cd2006-08-21 23:54:55 -0700239 kfree(police);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return err;
241}
242
243static int tcf_act_police_cleanup(struct tc_action *a, int bind)
244{
David S. Millere9ce1cd2006-08-21 23:54:55 -0700245 struct tcf_police *p = a->priv;
Patrick McHardyc3bc7cf2007-07-15 00:03:05 -0700246 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Patrick McHardyc3bc7cf2007-07-15 00:03:05 -0700248 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Patrick McHardyf43c5a02006-01-08 22:15:34 -0800261static int tcf_act_police(struct sk_buff *skb, struct tc_action *a,
YOSHIFUJI Hideaki10297b92007-02-09 23:25:16 +0900262 struct tcf_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
David S. Millere9ce1cd2006-08-21 23:54:55 -0700264 struct tcf_police *police = a->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 psched_time_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 long toks;
267 long ptoks = 0;
268
David S. Millere9ce1cd2006-08-21 23:54:55 -0700269 spin_lock(&police->tcf_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
David S. Millere9ce1cd2006-08-21 23:54:55 -0700271 police->tcf_bstats.bytes += skb->len;
272 police->tcf_bstats.packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
David S. Millere9ce1cd2006-08-21 23:54:55 -0700274 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
David S. Millere9ce1cd2006-08-21 23:54:55 -0700281 if (skb->len <= police->tcfp_mtu) {
282 if (police->tcfp_R_tab == NULL) {
283 spin_unlock(&police->tcf_lock);
284 return police->tcfp_result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
Patrick McHardy3bebcda2007-03-23 11:29:25 -0700287 now = psched_get_time();
Patrick McHardy03cc45c2007-03-23 11:29:11 -0700288 toks = psched_tdiff_bounded(now, police->tcfp_t_c,
289 police->tcfp_burst);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700290 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
David S. Millere9ce1cd2006-08-21 23:54:55 -0700296 toks += police->tcfp_toks;
297 if (toks > (long)police->tcfp_burst)
298 toks = police->tcfp_burst;
299 toks -= L2T(police, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if ((toks|ptoks) >= 0) {
David S. Millere9ce1cd2006-08-21 23:54:55 -0700301 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307 }
308
David S. Millere9ce1cd2006-08-21 23:54:55 -0700309 police->tcf_qstats.overlimits++;
310 spin_unlock(&police->tcf_lock);
311 return police->tcf_action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314static int
315tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
316{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700317 unsigned char *b = skb_tail_pointer(skb);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700318 struct tcf_police *police = a->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 struct tc_police opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
David S. Millere9ce1cd2006-08-21 23:54:55 -0700321 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 else
330 memset(&opt.rate, 0, sizeof(opt.rate));
David S. Millere9ce1cd2006-08-21 23:54:55 -0700331 if (police->tcfp_P_tab)
332 opt.peakrate = police->tcfp_P_tab->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 else
334 memset(&opt.peakrate, 0, sizeof(opt.peakrate));
335 RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700336 if (police->tcfp_result)
337 RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int),
338 &police->tcfp_result);
David S. Millere9ce1cd2006-08-21 23:54:55 -0700339 if (police->tcfp_ewma_rate)
340 RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &police->tcfp_ewma_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return skb->len;
342
343rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700344 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 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",
David S. Millere9ce1cd2006-08-21 23:54:55 -0700354 .hinfo = &police_hash_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 .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,
David S. Millere9ce1cd2006-08-21 23:54:55 -0700361 .lookup = tcf_hash_search,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 .init = tcf_act_police_locate,
Jamal Hadi Salim83b950c2006-04-06 22:24:22 -0700363 .walk = tcf_act_police_walker
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364};
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);