Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/rtnetlink.h> |
| 20 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <net/act_api.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 22 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Jesper Dangaard Brouer | e9bef55 | 2007-09-12 16:35:24 +0200 | [diff] [blame^] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 27 | #define POL_TAB_MASK 15 |
| 28 | static struct tcf_common *tcf_police_ht[POL_TAB_MASK + 1]; |
| 29 | static u32 police_idx_gen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | static DEFINE_RWLOCK(police_lock); |
| 31 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 32 | static struct tcf_hashinfo police_hash_info = { |
| 33 | .htab = tcf_police_ht, |
| 34 | .hmask = POL_TAB_MASK, |
| 35 | .lock = &police_lock, |
| 36 | }; |
| 37 | |
Patrick McHardy | 1e9b3d5 | 2006-11-30 19:54:05 -0800 | [diff] [blame] | 38 | /* old policer structure from before tc actions */ |
| 39 | struct 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | /* Each policer is serialized by its individual spinlock */ |
| 51 | |
Jamal Hadi Salim | 83b950c | 2006-04-06 22:24:22 -0700 | [diff] [blame] | 52 | static int tcf_act_police_walker(struct sk_buff *skb, struct netlink_callback *cb, |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 53 | int type, struct tc_action *a) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 55 | struct tcf_common *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | int err = 0, index = -1, i = 0, s_i = 0, n_i = 0; |
| 57 | struct rtattr *r; |
| 58 | |
Jamal Hadi Salim | e1e992e | 2007-09-12 16:32:59 +0200 | [diff] [blame] | 59 | read_lock_bh(&police_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | s_i = cb->args[0]; |
| 62 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 63 | for (i = 0; i < (POL_TAB_MASK + 1); i++) { |
| 64 | p = tcf_police_ht[tcf_hash(i, POL_TAB_MASK)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 66 | for (; p; p = p->tcfc_next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | index++; |
| 68 | if (index < s_i) |
| 69 | continue; |
| 70 | a->priv = p; |
| 71 | a->order = index; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 72 | r = (struct rtattr *)skb_tail_pointer(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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--; |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 80 | nlmsg_trim(skb, r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | goto done; |
| 82 | } |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 83 | r->rta_len = skb_tail_pointer(skb) - (u8 *)r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | n_i++; |
| 85 | } |
| 86 | } |
| 87 | done: |
Jamal Hadi Salim | e1e992e | 2007-09-12 16:32:59 +0200 | [diff] [blame] | 88 | read_unlock_bh(&police_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | if (n_i) |
| 90 | cb->args[0] += n_i; |
| 91 | return n_i; |
| 92 | |
| 93 | rtattr_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 94 | nlmsg_trim(skb, r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | goto done; |
| 96 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Patrick McHardy | c3bc7cf | 2007-07-15 00:03:05 -0700 | [diff] [blame] | 98 | static void tcf_police_destroy(struct tcf_police *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 100 | unsigned int h = tcf_hash(p->tcf_index, POL_TAB_MASK); |
| 101 | struct tcf_common **p1p; |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 102 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 103 | for (p1p = &tcf_police_ht[h]; *p1p; p1p = &(*p1p)->tcfc_next) { |
| 104 | if (*p1p == &p->common) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | write_lock_bh(&police_lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 106 | *p1p = p->tcf_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | write_unlock_bh(&police_lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 108 | gen_kill_estimator(&p->tcf_bstats, |
| 109 | &p->tcf_rate_est); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | kfree(p); |
| 115 | return; |
| 116 | } |
| 117 | } |
| 118 | BUG_TRAP(0); |
| 119 | } |
| 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | static int tcf_act_police_locate(struct rtattr *rta, struct rtattr *est, |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 122 | struct tc_action *a, int ovr, int bind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
| 124 | unsigned h; |
| 125 | int ret = 0, err; |
| 126 | struct rtattr *tb[TCA_POLICE_MAX]; |
| 127 | struct tc_police *parm; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 128 | struct tcf_police *police; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL; |
Patrick McHardy | 1e9b3d5 | 2006-11-30 19:54:05 -0800 | [diff] [blame] | 130 | int size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
| 132 | if (rta == NULL || rtattr_parse_nested(tb, TCA_POLICE_MAX, rta) < 0) |
| 133 | return -EINVAL; |
| 134 | |
Patrick McHardy | 1e9b3d5 | 2006-11-30 19:54:05 -0800 | [diff] [blame] | 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)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 166 | police = kzalloc(sizeof(*police), GFP_KERNEL); |
| 167 | if (police == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | ret = ACT_P_CREATED; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 170 | police->tcf_refcnt = 1; |
| 171 | spin_lock_init(&police->tcf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | if (bind) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 173 | police->tcf_bindcnt = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | override: |
| 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. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 183 | if (P_tab == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | qdisc_put_rtab(R_tab); |
| 185 | goto failure; |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | /* No failure allowed after this point */ |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 190 | spin_lock_bh(&police->tcf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | if (R_tab != NULL) { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 192 | qdisc_put_rtab(police->tcfp_R_tab); |
| 193 | police->tcfp_R_tab = R_tab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | if (P_tab != NULL) { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 196 | qdisc_put_rtab(police->tcfp_P_tab); |
| 197 | police->tcfp_P_tab = P_tab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | if (tb[TCA_POLICE_RESULT-1]) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 209 | if (police->tcfp_P_tab) |
| 210 | police->tcfp_ptoks = L2T_P(police, police->tcfp_mtu); |
| 211 | police->tcf_action = parm->action; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | if (tb[TCA_POLICE_AVRATE-1]) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 214 | police->tcfp_ewma_rate = |
| 215 | *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | if (est) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 217 | gen_replace_estimator(&police->tcf_bstats, |
| 218 | &police->tcf_rate_est, |
Patrick McHardy | 4bdf399 | 2007-07-02 22:47:37 -0700 | [diff] [blame] | 219 | &police->tcf_lock, est); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 221 | spin_unlock_bh(&police->tcf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | if (ret != ACT_P_CREATED) |
| 223 | return ret; |
| 224 | |
Patrick McHardy | 3bebcda | 2007-03-23 11:29:25 -0700 | [diff] [blame] | 225 | police->tcfp_t_c = psched_get_time(); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | write_lock_bh(&police_lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 230 | police->tcf_next = tcf_police_ht[h]; |
| 231 | tcf_police_ht[h] = &police->common; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | write_unlock_bh(&police_lock); |
| 233 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 234 | a->priv = police; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | return ret; |
| 236 | |
| 237 | failure: |
| 238 | if (ret == ACT_P_CREATED) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 239 | kfree(police); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | return err; |
| 241 | } |
| 242 | |
| 243 | static int tcf_act_police_cleanup(struct tc_action *a, int bind) |
| 244 | { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 245 | struct tcf_police *p = a->priv; |
Patrick McHardy | c3bc7cf | 2007-07-15 00:03:05 -0700 | [diff] [blame] | 246 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Patrick McHardy | c3bc7cf | 2007-07-15 00:03:05 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Patrick McHardy | f43c5a0 | 2006-01-08 22:15:34 -0800 | [diff] [blame] | 261 | static int tcf_act_police(struct sk_buff *skb, struct tc_action *a, |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 262 | struct tcf_result *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 264 | struct tcf_police *police = a->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | psched_time_t now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | long toks; |
| 267 | long ptoks = 0; |
| 268 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 269 | spin_lock(&police->tcf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 271 | police->tcf_bstats.bytes += skb->len; |
| 272 | police->tcf_bstats.packets++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Patrick McHardy | 3bebcda | 2007-03-23 11:29:25 -0700 | [diff] [blame] | 287 | now = psched_get_time(); |
Patrick McHardy | 03cc45c | 2007-03-23 11:29:11 -0700 | [diff] [blame] | 288 | toks = psched_tdiff_bounded(now, police->tcfp_t_c, |
| 289 | police->tcfp_burst); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 296 | toks += police->tcfp_toks; |
| 297 | if (toks > (long)police->tcfp_burst) |
| 298 | toks = police->tcfp_burst; |
| 299 | toks -= L2T(police, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | if ((toks|ptoks) >= 0) { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 309 | police->tcf_qstats.overlimits++; |
| 310 | spin_unlock(&police->tcf_lock); |
| 311 | return police->tcf_action; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static int |
| 315 | tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref) |
| 316 | { |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 317 | unsigned char *b = skb_tail_pointer(skb); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 318 | struct tcf_police *police = a->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | struct tc_police opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | else |
| 330 | memset(&opt.rate, 0, sizeof(opt.rate)); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 331 | if (police->tcfp_P_tab) |
| 332 | opt.peakrate = police->tcfp_P_tab->rate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | else |
| 334 | memset(&opt.peakrate, 0, sizeof(opt.peakrate)); |
| 335 | RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 336 | if (police->tcfp_result) |
| 337 | RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int), |
| 338 | &police->tcfp_result); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 339 | if (police->tcfp_ewma_rate) |
| 340 | RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &police->tcfp_ewma_rate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | return skb->len; |
| 342 | |
| 343 | rtattr_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 344 | nlmsg_trim(skb, b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | return -1; |
| 346 | } |
| 347 | |
| 348 | MODULE_AUTHOR("Alexey Kuznetsov"); |
| 349 | MODULE_DESCRIPTION("Policing actions"); |
| 350 | MODULE_LICENSE("GPL"); |
| 351 | |
| 352 | static struct tc_action_ops act_police_ops = { |
| 353 | .kind = "police", |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 354 | .hinfo = &police_hash_info, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 361 | .lookup = tcf_hash_search, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | .init = tcf_act_police_locate, |
Jamal Hadi Salim | 83b950c | 2006-04-06 22:24:22 -0700 | [diff] [blame] | 363 | .walk = tcf_act_police_walker |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | }; |
| 365 | |
| 366 | static int __init |
| 367 | police_init_module(void) |
| 368 | { |
| 369 | return tcf_register_action(&act_police_ops); |
| 370 | } |
| 371 | |
| 372 | static void __exit |
| 373 | police_cleanup_module(void) |
| 374 | { |
| 375 | tcf_unregister_action(&act_police_ops); |
| 376 | } |
| 377 | |
| 378 | module_init(police_init_module); |
| 379 | module_exit(police_cleanup_module); |