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 | |
| 13 | #include <asm/uaccess.h> |
| 14 | #include <asm/system.h> |
| 15 | #include <linux/bitops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/types.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/sched.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/socket.h> |
| 23 | #include <linux/sockios.h> |
| 24 | #include <linux/in.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/netdevice.h> |
| 28 | #include <linux/skbuff.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/rtnetlink.h> |
| 31 | #include <linux/init.h> |
| 32 | #include <net/sock.h> |
| 33 | #include <net/act_api.h> |
| 34 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 35 | #define L2T(p,L) ((p)->tcfp_R_tab->data[(L)>>(p)->tcfp_R_tab->rate.cell_log]) |
| 36 | #define L2T_P(p,L) ((p)->tcfp_P_tab->data[(L)>>(p)->tcfp_P_tab->rate.cell_log]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 38 | #define POL_TAB_MASK 15 |
| 39 | static struct tcf_common *tcf_police_ht[POL_TAB_MASK + 1]; |
| 40 | static u32 police_idx_gen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | static DEFINE_RWLOCK(police_lock); |
| 42 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 43 | static struct tcf_hashinfo police_hash_info = { |
| 44 | .htab = tcf_police_ht, |
| 45 | .hmask = POL_TAB_MASK, |
| 46 | .lock = &police_lock, |
| 47 | }; |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | /* Each policer is serialized by its individual spinlock */ |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #ifdef CONFIG_NET_CLS_ACT |
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, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | int type, struct tc_action *a) |
| 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 | |
| 59 | read_lock(&police_lock); |
| 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; |
| 72 | r = (struct rtattr*) skb->tail; |
| 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--; |
| 80 | skb_trim(skb, (u8*)r - skb->data); |
| 81 | goto done; |
| 82 | } |
| 83 | r->rta_len = skb->tail - (u8*)r; |
| 84 | n_i++; |
| 85 | } |
| 86 | } |
| 87 | done: |
| 88 | read_unlock(&police_lock); |
| 89 | if (n_i) |
| 90 | cb->args[0] += n_i; |
| 91 | return n_i; |
| 92 | |
| 93 | rtattr_failure: |
| 94 | skb_trim(skb, (u8*)r - skb->data); |
| 95 | goto done; |
| 96 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | #endif |
| 98 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | void tcf_police_destroy(struct tcf_police *p) |
| 100 | { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 101 | unsigned int h = tcf_hash(p->tcf_index, POL_TAB_MASK); |
| 102 | struct tcf_common **p1p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 104 | for (p1p = &tcf_police_ht[h]; *p1p; p1p = &(*p1p)->tcfc_next) { |
| 105 | if (*p1p == &p->common) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | write_lock_bh(&police_lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 107 | *p1p = p->tcf_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | write_unlock_bh(&police_lock); |
| 109 | #ifdef CONFIG_NET_ESTIMATOR |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 110 | gen_kill_estimator(&p->tcf_bstats, |
| 111 | &p->tcf_rate_est); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | #endif |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 113 | if (p->tcfp_R_tab) |
| 114 | qdisc_put_rtab(p->tcfp_R_tab); |
| 115 | if (p->tcfp_P_tab) |
| 116 | qdisc_put_rtab(p->tcfp_P_tab); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | kfree(p); |
| 118 | return; |
| 119 | } |
| 120 | } |
| 121 | BUG_TRAP(0); |
| 122 | } |
| 123 | |
| 124 | #ifdef CONFIG_NET_CLS_ACT |
| 125 | static int tcf_act_police_locate(struct rtattr *rta, struct rtattr *est, |
| 126 | struct tc_action *a, int ovr, int bind) |
| 127 | { |
| 128 | unsigned h; |
| 129 | int ret = 0, err; |
| 130 | struct rtattr *tb[TCA_POLICE_MAX]; |
| 131 | struct tc_police *parm; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 132 | struct tcf_police *police; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | struct qdisc_rate_table *R_tab = NULL, *P_tab = NULL; |
| 134 | |
| 135 | if (rta == NULL || rtattr_parse_nested(tb, TCA_POLICE_MAX, rta) < 0) |
| 136 | return -EINVAL; |
| 137 | |
| 138 | if (tb[TCA_POLICE_TBF-1] == NULL || |
| 139 | RTA_PAYLOAD(tb[TCA_POLICE_TBF-1]) != sizeof(*parm)) |
| 140 | return -EINVAL; |
| 141 | parm = RTA_DATA(tb[TCA_POLICE_TBF-1]); |
| 142 | |
| 143 | if (tb[TCA_POLICE_RESULT-1] != NULL && |
| 144 | RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32)) |
| 145 | return -EINVAL; |
| 146 | if (tb[TCA_POLICE_RESULT-1] != NULL && |
| 147 | RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32)) |
| 148 | return -EINVAL; |
| 149 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 150 | if (parm->index) { |
| 151 | struct tcf_common *pc; |
| 152 | |
| 153 | pc = tcf_hash_lookup(parm->index, &police_hash_info); |
| 154 | if (pc != NULL) { |
| 155 | a->priv = pc; |
| 156 | police = to_police(pc); |
| 157 | if (bind) { |
| 158 | police->tcf_bindcnt += 1; |
| 159 | police->tcf_refcnt += 1; |
| 160 | } |
| 161 | if (ovr) |
| 162 | goto override; |
| 163 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 167 | police = kzalloc(sizeof(*police), GFP_KERNEL); |
| 168 | if (police == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | ret = ACT_P_CREATED; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 171 | police->tcf_refcnt = 1; |
| 172 | spin_lock_init(&police->tcf_lock); |
| 173 | police->tcf_stats_lock = &police->tcf_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | if (bind) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 175 | police->tcf_bindcnt = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | override: |
| 177 | if (parm->rate.rate) { |
| 178 | err = -ENOMEM; |
| 179 | R_tab = qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1]); |
| 180 | if (R_tab == NULL) |
| 181 | goto failure; |
| 182 | if (parm->peakrate.rate) { |
| 183 | P_tab = qdisc_get_rtab(&parm->peakrate, |
| 184 | tb[TCA_POLICE_PEAKRATE-1]); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 185 | if (P_tab == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | qdisc_put_rtab(R_tab); |
| 187 | goto failure; |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | /* No failure allowed after this point */ |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 192 | spin_lock_bh(&police->tcf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | if (R_tab != NULL) { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 194 | qdisc_put_rtab(police->tcfp_R_tab); |
| 195 | police->tcfp_R_tab = R_tab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | if (P_tab != NULL) { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 198 | qdisc_put_rtab(police->tcfp_P_tab); |
| 199 | police->tcfp_P_tab = P_tab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | if (tb[TCA_POLICE_RESULT-1]) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 203 | police->tcfp_result = *(u32*)RTA_DATA(tb[TCA_POLICE_RESULT-1]); |
| 204 | police->tcfp_toks = police->tcfp_burst = parm->burst; |
| 205 | police->tcfp_mtu = parm->mtu; |
| 206 | if (police->tcfp_mtu == 0) { |
| 207 | police->tcfp_mtu = ~0; |
| 208 | if (police->tcfp_R_tab) |
| 209 | police->tcfp_mtu = 255<<police->tcfp_R_tab->rate.cell_log; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 211 | if (police->tcfp_P_tab) |
| 212 | police->tcfp_ptoks = L2T_P(police, police->tcfp_mtu); |
| 213 | police->tcf_action = parm->action; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
| 215 | #ifdef CONFIG_NET_ESTIMATOR |
| 216 | if (tb[TCA_POLICE_AVRATE-1]) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 217 | police->tcfp_ewma_rate = |
| 218 | *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | if (est) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 220 | gen_replace_estimator(&police->tcf_bstats, |
| 221 | &police->tcf_rate_est, |
| 222 | police->tcf_stats_lock, est); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | #endif |
| 224 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 225 | spin_unlock_bh(&police->tcf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | if (ret != ACT_P_CREATED) |
| 227 | return ret; |
| 228 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 229 | PSCHED_GET_TIME(police->tcfp_t_c); |
| 230 | police->tcf_index = parm->index ? parm->index : |
| 231 | tcf_hash_new_index(&police_idx_gen, &police_hash_info); |
| 232 | h = tcf_hash(police->tcf_index, POL_TAB_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | write_lock_bh(&police_lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 234 | police->tcf_next = tcf_police_ht[h]; |
| 235 | tcf_police_ht[h] = &police->common; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | write_unlock_bh(&police_lock); |
| 237 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 238 | a->priv = police; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | return ret; |
| 240 | |
| 241 | failure: |
| 242 | if (ret == ACT_P_CREATED) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 243 | kfree(police); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | return err; |
| 245 | } |
| 246 | |
| 247 | static int tcf_act_police_cleanup(struct tc_action *a, int bind) |
| 248 | { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 249 | struct tcf_police *p = a->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
| 251 | if (p != NULL) |
| 252 | return tcf_police_release(p, bind); |
| 253 | return 0; |
| 254 | } |
| 255 | |
Patrick McHardy | f43c5a0 | 2006-01-08 22:15:34 -0800 | [diff] [blame] | 256 | static int tcf_act_police(struct sk_buff *skb, struct tc_action *a, |
Patrick McHardy | abc3bc5 | 2005-08-09 19:25:56 -0700 | [diff] [blame] | 257 | struct tcf_result *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 259 | struct tcf_police *police = a->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | psched_time_t now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | long toks; |
| 262 | long ptoks = 0; |
| 263 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 264 | spin_lock(&police->tcf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 266 | police->tcf_bstats.bytes += skb->len; |
| 267 | police->tcf_bstats.packets++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
| 269 | #ifdef CONFIG_NET_ESTIMATOR |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 270 | if (police->tcfp_ewma_rate && |
| 271 | police->tcf_rate_est.bps >= police->tcfp_ewma_rate) { |
| 272 | police->tcf_qstats.overlimits++; |
| 273 | spin_unlock(&police->tcf_lock); |
| 274 | return police->tcf_action; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | } |
| 276 | #endif |
| 277 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 278 | if (skb->len <= police->tcfp_mtu) { |
| 279 | if (police->tcfp_R_tab == NULL) { |
| 280 | spin_unlock(&police->tcf_lock); |
| 281 | return police->tcfp_result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | PSCHED_GET_TIME(now); |
| 285 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 286 | toks = PSCHED_TDIFF_SAFE(now, police->tcfp_t_c, |
| 287 | police->tcfp_burst); |
| 288 | if (police->tcfp_P_tab) { |
| 289 | ptoks = toks + police->tcfp_ptoks; |
| 290 | if (ptoks > (long)L2T_P(police, police->tcfp_mtu)) |
| 291 | ptoks = (long)L2T_P(police, police->tcfp_mtu); |
| 292 | ptoks -= L2T_P(police, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | } |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 294 | toks += police->tcfp_toks; |
| 295 | if (toks > (long)police->tcfp_burst) |
| 296 | toks = police->tcfp_burst; |
| 297 | toks -= L2T(police, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | if ((toks|ptoks) >= 0) { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 299 | police->tcfp_t_c = now; |
| 300 | police->tcfp_toks = toks; |
| 301 | police->tcfp_ptoks = ptoks; |
| 302 | spin_unlock(&police->tcf_lock); |
| 303 | return police->tcfp_result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 307 | police->tcf_qstats.overlimits++; |
| 308 | spin_unlock(&police->tcf_lock); |
| 309 | return police->tcf_action; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static int |
| 313 | tcf_act_police_dump(struct sk_buff *skb, struct tc_action *a, int bind, int ref) |
| 314 | { |
| 315 | unsigned char *b = skb->tail; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 316 | struct tcf_police *police = a->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | struct tc_police opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 319 | opt.index = police->tcf_index; |
| 320 | opt.action = police->tcf_action; |
| 321 | opt.mtu = police->tcfp_mtu; |
| 322 | opt.burst = police->tcfp_burst; |
| 323 | opt.refcnt = police->tcf_refcnt - ref; |
| 324 | opt.bindcnt = police->tcf_bindcnt - bind; |
| 325 | if (police->tcfp_R_tab) |
| 326 | opt.rate = police->tcfp_R_tab->rate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | else |
| 328 | memset(&opt.rate, 0, sizeof(opt.rate)); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 329 | if (police->tcfp_P_tab) |
| 330 | opt.peakrate = police->tcfp_P_tab->rate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | else |
| 332 | memset(&opt.peakrate, 0, sizeof(opt.peakrate)); |
| 333 | RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 334 | if (police->tcfp_result) |
| 335 | RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int), |
| 336 | &police->tcfp_result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | #ifdef CONFIG_NET_ESTIMATOR |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 338 | if (police->tcfp_ewma_rate) |
| 339 | RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &police->tcfp_ewma_rate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | #endif |
| 341 | return skb->len; |
| 342 | |
| 343 | rtattr_failure: |
| 344 | skb_trim(skb, b - skb->data); |
| 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); |
| 380 | |
Patrick McHardy | 31bd06e | 2006-01-08 22:16:25 -0800 | [diff] [blame] | 381 | #else /* CONFIG_NET_CLS_ACT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 383 | static struct tcf_common *tcf_police_lookup(u32 index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 385 | struct tcf_hashinfo *hinfo = &police_hash_info; |
| 386 | struct tcf_common *p; |
| 387 | |
| 388 | read_lock(hinfo->lock); |
| 389 | for (p = hinfo->htab[tcf_hash(index, hinfo->hmask)]; p; |
| 390 | p = p->tcfc_next) { |
| 391 | if (p->tcfc_index == index) |
| 392 | break; |
| 393 | } |
| 394 | read_unlock(hinfo->lock); |
| 395 | |
| 396 | return p; |
| 397 | } |
| 398 | |
| 399 | static u32 tcf_police_new_index(void) |
| 400 | { |
| 401 | u32 *idx_gen = &police_idx_gen; |
| 402 | u32 val = *idx_gen; |
| 403 | |
| 404 | do { |
| 405 | if (++val == 0) |
| 406 | val = 1; |
| 407 | } while (tcf_police_lookup(val)); |
| 408 | |
| 409 | return (*idx_gen = val); |
| 410 | } |
| 411 | |
| 412 | struct tcf_police *tcf_police_locate(struct rtattr *rta, struct rtattr *est) |
| 413 | { |
| 414 | unsigned int h; |
| 415 | struct tcf_police *police; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | struct rtattr *tb[TCA_POLICE_MAX]; |
| 417 | struct tc_police *parm; |
| 418 | |
| 419 | if (rtattr_parse_nested(tb, TCA_POLICE_MAX, rta) < 0) |
| 420 | return NULL; |
| 421 | |
| 422 | if (tb[TCA_POLICE_TBF-1] == NULL || |
| 423 | RTA_PAYLOAD(tb[TCA_POLICE_TBF-1]) != sizeof(*parm)) |
| 424 | return NULL; |
| 425 | |
| 426 | parm = RTA_DATA(tb[TCA_POLICE_TBF-1]); |
| 427 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 428 | if (parm->index) { |
| 429 | struct tcf_common *pc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 431 | pc = tcf_police_lookup(parm->index); |
| 432 | if (pc) { |
| 433 | police = to_police(pc); |
| 434 | police->tcf_refcnt++; |
| 435 | return police; |
| 436 | } |
| 437 | } |
| 438 | police = kzalloc(sizeof(*police), GFP_KERNEL); |
| 439 | if (unlikely(!police)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | return NULL; |
| 441 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 442 | police->tcf_refcnt = 1; |
| 443 | spin_lock_init(&police->tcf_lock); |
| 444 | police->tcf_stats_lock = &police->tcf_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | if (parm->rate.rate) { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 446 | police->tcfp_R_tab = |
| 447 | qdisc_get_rtab(&parm->rate, tb[TCA_POLICE_RATE-1]); |
| 448 | if (police->tcfp_R_tab == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | goto failure; |
| 450 | if (parm->peakrate.rate) { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 451 | police->tcfp_P_tab = |
| 452 | qdisc_get_rtab(&parm->peakrate, |
| 453 | tb[TCA_POLICE_PEAKRATE-1]); |
| 454 | if (police->tcfp_P_tab == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | goto failure; |
| 456 | } |
| 457 | } |
| 458 | if (tb[TCA_POLICE_RESULT-1]) { |
| 459 | if (RTA_PAYLOAD(tb[TCA_POLICE_RESULT-1]) != sizeof(u32)) |
| 460 | goto failure; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 461 | police->tcfp_result = *(u32*)RTA_DATA(tb[TCA_POLICE_RESULT-1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | } |
| 463 | #ifdef CONFIG_NET_ESTIMATOR |
| 464 | if (tb[TCA_POLICE_AVRATE-1]) { |
| 465 | if (RTA_PAYLOAD(tb[TCA_POLICE_AVRATE-1]) != sizeof(u32)) |
| 466 | goto failure; |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 467 | police->tcfp_ewma_rate = |
| 468 | *(u32*)RTA_DATA(tb[TCA_POLICE_AVRATE-1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } |
| 470 | #endif |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 471 | police->tcfp_toks = police->tcfp_burst = parm->burst; |
| 472 | police->tcfp_mtu = parm->mtu; |
| 473 | if (police->tcfp_mtu == 0) { |
| 474 | police->tcfp_mtu = ~0; |
| 475 | if (police->tcfp_R_tab) |
| 476 | police->tcfp_mtu = 255<<police->tcfp_R_tab->rate.cell_log; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 478 | if (police->tcfp_P_tab) |
| 479 | police->tcfp_ptoks = L2T_P(police, police->tcfp_mtu); |
| 480 | PSCHED_GET_TIME(police->tcfp_t_c); |
| 481 | police->tcf_index = parm->index ? parm->index : |
| 482 | tcf_police_new_index(); |
| 483 | police->tcf_action = parm->action; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | #ifdef CONFIG_NET_ESTIMATOR |
| 485 | if (est) |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 486 | gen_new_estimator(&police->tcf_bstats, &police->tcf_rate_est, |
| 487 | police->tcf_stats_lock, est); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | #endif |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 489 | h = tcf_hash(police->tcf_index, POL_TAB_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | write_lock_bh(&police_lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 491 | police->tcf_next = tcf_police_ht[h]; |
| 492 | tcf_police_ht[h] = &police->common; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | write_unlock_bh(&police_lock); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 494 | return police; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
| 496 | failure: |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 497 | if (police->tcfp_R_tab) |
| 498 | qdisc_put_rtab(police->tcfp_R_tab); |
| 499 | kfree(police); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | return NULL; |
| 501 | } |
| 502 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 503 | int tcf_police(struct sk_buff *skb, struct tcf_police *police) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | { |
| 505 | psched_time_t now; |
| 506 | long toks; |
| 507 | long ptoks = 0; |
| 508 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 509 | spin_lock(&police->tcf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 511 | police->tcf_bstats.bytes += skb->len; |
| 512 | police->tcf_bstats.packets++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
| 514 | #ifdef CONFIG_NET_ESTIMATOR |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 515 | if (police->tcfp_ewma_rate && |
| 516 | police->tcf_rate_est.bps >= police->tcfp_ewma_rate) { |
| 517 | police->tcf_qstats.overlimits++; |
| 518 | spin_unlock(&police->tcf_lock); |
| 519 | return police->tcf_action; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | } |
| 521 | #endif |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 522 | if (skb->len <= police->tcfp_mtu) { |
| 523 | if (police->tcfp_R_tab == NULL) { |
| 524 | spin_unlock(&police->tcf_lock); |
| 525 | return police->tcfp_result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | PSCHED_GET_TIME(now); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 529 | toks = PSCHED_TDIFF_SAFE(now, police->tcfp_t_c, |
| 530 | police->tcfp_burst); |
| 531 | if (police->tcfp_P_tab) { |
| 532 | ptoks = toks + police->tcfp_ptoks; |
| 533 | if (ptoks > (long)L2T_P(police, police->tcfp_mtu)) |
| 534 | ptoks = (long)L2T_P(police, police->tcfp_mtu); |
| 535 | ptoks -= L2T_P(police, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | } |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 537 | toks += police->tcfp_toks; |
| 538 | if (toks > (long)police->tcfp_burst) |
| 539 | toks = police->tcfp_burst; |
| 540 | toks -= L2T(police, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | if ((toks|ptoks) >= 0) { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 542 | police->tcfp_t_c = now; |
| 543 | police->tcfp_toks = toks; |
| 544 | police->tcfp_ptoks = ptoks; |
| 545 | spin_unlock(&police->tcf_lock); |
| 546 | return police->tcfp_result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 550 | police->tcf_qstats.overlimits++; |
| 551 | spin_unlock(&police->tcf_lock); |
| 552 | return police->tcf_action; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | } |
Patrick McHardy | 31bd06e | 2006-01-08 22:16:25 -0800 | [diff] [blame] | 554 | EXPORT_SYMBOL(tcf_police); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 556 | int tcf_police_dump(struct sk_buff *skb, struct tcf_police *police) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 558 | unsigned char *b = skb->tail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | struct tc_police opt; |
| 560 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 561 | opt.index = police->tcf_index; |
| 562 | opt.action = police->tcf_action; |
| 563 | opt.mtu = police->tcfp_mtu; |
| 564 | opt.burst = police->tcfp_burst; |
| 565 | if (police->tcfp_R_tab) |
| 566 | opt.rate = police->tcfp_R_tab->rate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | else |
| 568 | memset(&opt.rate, 0, sizeof(opt.rate)); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 569 | if (police->tcfp_P_tab) |
| 570 | opt.peakrate = police->tcfp_P_tab->rate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | else |
| 572 | memset(&opt.peakrate, 0, sizeof(opt.peakrate)); |
| 573 | RTA_PUT(skb, TCA_POLICE_TBF, sizeof(opt), &opt); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 574 | if (police->tcfp_result) |
| 575 | RTA_PUT(skb, TCA_POLICE_RESULT, sizeof(int), |
| 576 | &police->tcfp_result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | #ifdef CONFIG_NET_ESTIMATOR |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 578 | if (police->tcfp_ewma_rate) |
| 579 | RTA_PUT(skb, TCA_POLICE_AVRATE, 4, &police->tcfp_ewma_rate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | #endif |
| 581 | return skb->len; |
| 582 | |
| 583 | rtattr_failure: |
| 584 | skb_trim(skb, b - skb->data); |
| 585 | return -1; |
| 586 | } |
| 587 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 588 | int tcf_police_dump_stats(struct sk_buff *skb, struct tcf_police *police) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | { |
| 590 | struct gnet_dump d; |
| 591 | |
| 592 | if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS, |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 593 | TCA_XSTATS, police->tcf_stats_lock, |
| 594 | &d) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | goto errout; |
| 596 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 597 | if (gnet_stats_copy_basic(&d, &police->tcf_bstats) < 0 || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | #ifdef CONFIG_NET_ESTIMATOR |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 599 | gnet_stats_copy_rate_est(&d, &police->tcf_rate_est) < 0 || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | #endif |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 601 | gnet_stats_copy_queue(&d, &police->tcf_qstats) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | goto errout; |
| 603 | |
| 604 | if (gnet_stats_finish_copy(&d) < 0) |
| 605 | goto errout; |
| 606 | |
| 607 | return 0; |
| 608 | |
| 609 | errout: |
| 610 | return -1; |
| 611 | } |
| 612 | |
Patrick McHardy | 31bd06e | 2006-01-08 22:16:25 -0800 | [diff] [blame] | 613 | #endif /* CONFIG_NET_CLS_ACT */ |