Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * net/sched/cls_u32.c Ugly (or Universal) 32bit key Packet Classifier. |
| 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 | * |
| 11 | * The filters are packed to hash tables of key nodes |
| 12 | * with a set of 32bit key/mask pairs at every node. |
| 13 | * Nodes reference next level hash tables etc. |
| 14 | * |
| 15 | * This scheme is the best universal classifier I managed to |
| 16 | * invent; it is not super-fast, but it is not slow (provided you |
| 17 | * program it correctly), and general enough. And its relative |
| 18 | * speed grows as the number of rules becomes larger. |
| 19 | * |
| 20 | * It seems that it represents the best middle point between |
| 21 | * speed and manageability both by human and by machine. |
| 22 | * |
| 23 | * It is especially useful for link sharing combined with QoS; |
| 24 | * pure RSVP doesn't need such a general approach and can use |
| 25 | * much simpler (and faster) schemes, sort of cls_rsvp.c. |
| 26 | * |
| 27 | * JHS: We should remove the CONFIG_NET_CLS_IND from here |
| 28 | * eventually when the meta match extension is made available |
| 29 | * |
| 30 | * nfmark match added by Catalin(ux aka Dino) BOIE <catab at umbrella.ro> |
| 31 | */ |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/types.h> |
| 36 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/rtnetlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/skbuff.h> |
Cong Wang | 7801db8 | 2014-07-17 17:34:53 -0700 | [diff] [blame] | 41 | #include <linux/bitmap.h> |
Patrick McHardy | 0ba4805 | 2007-07-02 22:49:07 -0700 | [diff] [blame] | 42 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <net/act_api.h> |
| 44 | #include <net/pkt_cls.h> |
| 45 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 46 | struct tc_u_knode { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | struct tc_u_knode *next; |
| 48 | u32 handle; |
| 49 | struct tc_u_hnode *ht_up; |
| 50 | struct tcf_exts exts; |
| 51 | #ifdef CONFIG_NET_CLS_IND |
WANG Cong | 2519a60 | 2014-01-09 16:14:02 -0800 | [diff] [blame] | 52 | int ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #endif |
| 54 | u8 fshift; |
| 55 | struct tcf_result res; |
| 56 | struct tc_u_hnode *ht_down; |
| 57 | #ifdef CONFIG_CLS_U32_PERF |
| 58 | struct tc_u32_pcnt *pf; |
| 59 | #endif |
| 60 | #ifdef CONFIG_CLS_U32_MARK |
| 61 | struct tc_u32_mark mark; |
| 62 | #endif |
| 63 | struct tc_u32_sel sel; |
| 64 | }; |
| 65 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 66 | struct tc_u_hnode { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | struct tc_u_hnode *next; |
| 68 | u32 handle; |
| 69 | u32 prio; |
| 70 | struct tc_u_common *tp_c; |
| 71 | int refcnt; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 72 | unsigned int divisor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | struct tc_u_knode *ht[1]; |
| 74 | }; |
| 75 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 76 | struct tc_u_common { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | struct tc_u_hnode *hlist; |
| 78 | struct Qdisc *q; |
| 79 | int refcnt; |
| 80 | u32 hgenerator; |
| 81 | }; |
| 82 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 83 | static inline unsigned int u32_hash_fold(__be32 key, |
| 84 | const struct tc_u32_sel *sel, |
| 85 | u8 fshift) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 87 | unsigned int h = ntohl(key & sel->hmask) >> fshift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
| 89 | return h; |
| 90 | } |
| 91 | |
Eric Dumazet | dc7f9f6 | 2011-07-05 23:25:42 +0000 | [diff] [blame] | 92 | static int u32_classify(struct sk_buff *skb, const struct tcf_proto *tp, struct tcf_result *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
| 94 | struct { |
| 95 | struct tc_u_knode *knode; |
Changli Gao | fbc2e7d | 2010-06-02 07:32:42 -0700 | [diff] [blame] | 96 | unsigned int off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } stack[TC_U32_MAXDEPTH]; |
| 98 | |
WANG Cong | a8701a6 | 2014-01-09 16:14:03 -0800 | [diff] [blame] | 99 | struct tc_u_hnode *ht = tp->root; |
Changli Gao | fbc2e7d | 2010-06-02 07:32:42 -0700 | [diff] [blame] | 100 | unsigned int off = skb_network_offset(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | struct tc_u_knode *n; |
| 102 | int sdepth = 0; |
| 103 | int off2 = 0; |
| 104 | int sel = 0; |
| 105 | #ifdef CONFIG_CLS_U32_PERF |
| 106 | int j; |
| 107 | #endif |
| 108 | int i, r; |
| 109 | |
| 110 | next_ht: |
| 111 | n = ht->ht[sel]; |
| 112 | |
| 113 | next_knode: |
| 114 | if (n) { |
| 115 | struct tc_u32_key *key = n->sel.keys; |
| 116 | |
| 117 | #ifdef CONFIG_CLS_U32_PERF |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 118 | n->pf->rcnt += 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | j = 0; |
| 120 | #endif |
| 121 | |
| 122 | #ifdef CONFIG_CLS_U32_MARK |
Thomas Graf | 82e91ff | 2006-11-09 15:19:14 -0800 | [diff] [blame] | 123 | if ((skb->mark & n->mark.mask) != n->mark.val) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | n = n->next; |
| 125 | goto next_knode; |
| 126 | } else { |
| 127 | n->mark.success++; |
| 128 | } |
| 129 | #endif |
| 130 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 131 | for (i = n->sel.nkeys; i > 0; i--, key++) { |
stephen hemminger | 66d50d2 | 2010-08-02 13:44:13 +0000 | [diff] [blame] | 132 | int toff = off + key->off + (off2 & key->offmask); |
stephen hemminger | 86fce3b | 2011-02-20 16:14:23 +0000 | [diff] [blame] | 133 | __be32 *data, hdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Dan Carpenter | 4e18b3e | 2010-10-04 02:28:36 +0000 | [diff] [blame] | 135 | if (skb_headroom(skb) + toff > INT_MAX) |
stephen hemminger | 66d50d2 | 2010-08-02 13:44:13 +0000 | [diff] [blame] | 136 | goto out; |
| 137 | |
stephen hemminger | 86fce3b | 2011-02-20 16:14:23 +0000 | [diff] [blame] | 138 | data = skb_header_pointer(skb, toff, 4, &hdata); |
Changli Gao | fbc2e7d | 2010-06-02 07:32:42 -0700 | [diff] [blame] | 139 | if (!data) |
| 140 | goto out; |
| 141 | if ((*data ^ key->val) & key->mask) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | n = n->next; |
| 143 | goto next_knode; |
| 144 | } |
| 145 | #ifdef CONFIG_CLS_U32_PERF |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 146 | n->pf->kcnts[j] += 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | j++; |
| 148 | #endif |
| 149 | } |
| 150 | if (n->ht_down == NULL) { |
| 151 | check_terminal: |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 152 | if (n->sel.flags & TC_U32_TERMINAL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 154 | *res = n->res; |
| 155 | #ifdef CONFIG_NET_CLS_IND |
WANG Cong | 2519a60 | 2014-01-09 16:14:02 -0800 | [diff] [blame] | 156 | if (!tcf_match_indev(skb, n->ifindex)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | n = n->next; |
| 158 | goto next_knode; |
| 159 | } |
| 160 | #endif |
| 161 | #ifdef CONFIG_CLS_U32_PERF |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 162 | n->pf->rhit += 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | #endif |
| 164 | r = tcf_exts_exec(skb, &n->exts, res); |
| 165 | if (r < 0) { |
| 166 | n = n->next; |
| 167 | goto next_knode; |
| 168 | } |
| 169 | |
| 170 | return r; |
| 171 | } |
| 172 | n = n->next; |
| 173 | goto next_knode; |
| 174 | } |
| 175 | |
| 176 | /* PUSH */ |
| 177 | if (sdepth >= TC_U32_MAXDEPTH) |
| 178 | goto deadloop; |
| 179 | stack[sdepth].knode = n; |
Changli Gao | fbc2e7d | 2010-06-02 07:32:42 -0700 | [diff] [blame] | 180 | stack[sdepth].off = off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | sdepth++; |
| 182 | |
| 183 | ht = n->ht_down; |
| 184 | sel = 0; |
Changli Gao | fbc2e7d | 2010-06-02 07:32:42 -0700 | [diff] [blame] | 185 | if (ht->divisor) { |
stephen hemminger | 86fce3b | 2011-02-20 16:14:23 +0000 | [diff] [blame] | 186 | __be32 *data, hdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Changli Gao | fbc2e7d | 2010-06-02 07:32:42 -0700 | [diff] [blame] | 188 | data = skb_header_pointer(skb, off + n->sel.hoff, 4, |
stephen hemminger | 86fce3b | 2011-02-20 16:14:23 +0000 | [diff] [blame] | 189 | &hdata); |
Changli Gao | fbc2e7d | 2010-06-02 07:32:42 -0700 | [diff] [blame] | 190 | if (!data) |
| 191 | goto out; |
| 192 | sel = ht->divisor & u32_hash_fold(*data, &n->sel, |
| 193 | n->fshift); |
| 194 | } |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 195 | if (!(n->sel.flags & (TC_U32_VAROFFSET | TC_U32_OFFSET | TC_U32_EAT))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | goto next_ht; |
| 197 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 198 | if (n->sel.flags & (TC_U32_OFFSET | TC_U32_VAROFFSET)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | off2 = n->sel.off + 3; |
Changli Gao | fbc2e7d | 2010-06-02 07:32:42 -0700 | [diff] [blame] | 200 | if (n->sel.flags & TC_U32_VAROFFSET) { |
stephen hemminger | 86fce3b | 2011-02-20 16:14:23 +0000 | [diff] [blame] | 201 | __be16 *data, hdata; |
Changli Gao | fbc2e7d | 2010-06-02 07:32:42 -0700 | [diff] [blame] | 202 | |
| 203 | data = skb_header_pointer(skb, |
| 204 | off + n->sel.offoff, |
stephen hemminger | 86fce3b | 2011-02-20 16:14:23 +0000 | [diff] [blame] | 205 | 2, &hdata); |
Changli Gao | fbc2e7d | 2010-06-02 07:32:42 -0700 | [diff] [blame] | 206 | if (!data) |
| 207 | goto out; |
| 208 | off2 += ntohs(n->sel.offmask & *data) >> |
| 209 | n->sel.offshift; |
| 210 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | off2 &= ~3; |
| 212 | } |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 213 | if (n->sel.flags & TC_U32_EAT) { |
Changli Gao | fbc2e7d | 2010-06-02 07:32:42 -0700 | [diff] [blame] | 214 | off += off2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | off2 = 0; |
| 216 | } |
| 217 | |
Changli Gao | fbc2e7d | 2010-06-02 07:32:42 -0700 | [diff] [blame] | 218 | if (off < skb->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | goto next_ht; |
| 220 | } |
| 221 | |
| 222 | /* POP */ |
| 223 | if (sdepth--) { |
| 224 | n = stack[sdepth].knode; |
| 225 | ht = n->ht_up; |
Changli Gao | fbc2e7d | 2010-06-02 07:32:42 -0700 | [diff] [blame] | 226 | off = stack[sdepth].off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | goto check_terminal; |
| 228 | } |
Changli Gao | fbc2e7d | 2010-06-02 07:32:42 -0700 | [diff] [blame] | 229 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | return -1; |
| 231 | |
| 232 | deadloop: |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 233 | net_warn_ratelimited("cls_u32: dead loop\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | return -1; |
| 235 | } |
| 236 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 237 | static struct tc_u_hnode * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | u32_lookup_ht(struct tc_u_common *tp_c, u32 handle) |
| 239 | { |
| 240 | struct tc_u_hnode *ht; |
| 241 | |
| 242 | for (ht = tp_c->hlist; ht; ht = ht->next) |
| 243 | if (ht->handle == handle) |
| 244 | break; |
| 245 | |
| 246 | return ht; |
| 247 | } |
| 248 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 249 | static struct tc_u_knode * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | u32_lookup_key(struct tc_u_hnode *ht, u32 handle) |
| 251 | { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 252 | unsigned int sel; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | struct tc_u_knode *n = NULL; |
| 254 | |
| 255 | sel = TC_U32_HASH(handle); |
| 256 | if (sel > ht->divisor) |
| 257 | goto out; |
| 258 | |
| 259 | for (n = ht->ht[sel]; n; n = n->next) |
| 260 | if (n->handle == handle) |
| 261 | break; |
| 262 | out: |
| 263 | return n; |
| 264 | } |
| 265 | |
| 266 | |
| 267 | static unsigned long u32_get(struct tcf_proto *tp, u32 handle) |
| 268 | { |
| 269 | struct tc_u_hnode *ht; |
| 270 | struct tc_u_common *tp_c = tp->data; |
| 271 | |
| 272 | if (TC_U32_HTID(handle) == TC_U32_ROOT) |
| 273 | ht = tp->root; |
| 274 | else |
| 275 | ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle)); |
| 276 | |
| 277 | if (!ht) |
| 278 | return 0; |
| 279 | |
| 280 | if (TC_U32_KEY(handle) == 0) |
| 281 | return (unsigned long)ht; |
| 282 | |
| 283 | return (unsigned long)u32_lookup_key(ht, handle); |
| 284 | } |
| 285 | |
| 286 | static void u32_put(struct tcf_proto *tp, unsigned long f) |
| 287 | { |
| 288 | } |
| 289 | |
| 290 | static u32 gen_new_htid(struct tc_u_common *tp_c) |
| 291 | { |
| 292 | int i = 0x800; |
| 293 | |
| 294 | do { |
| 295 | if (++tp_c->hgenerator == 0x7FF) |
| 296 | tp_c->hgenerator = 1; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 297 | } while (--i > 0 && u32_lookup_ht(tp_c, (tp_c->hgenerator|0x800)<<20)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
| 299 | return i > 0 ? (tp_c->hgenerator|0x800)<<20 : 0; |
| 300 | } |
| 301 | |
| 302 | static int u32_init(struct tcf_proto *tp) |
| 303 | { |
| 304 | struct tc_u_hnode *root_ht; |
| 305 | struct tc_u_common *tp_c; |
| 306 | |
David S. Miller | 72b25a9 | 2008-07-18 20:54:17 -0700 | [diff] [blame] | 307 | tp_c = tp->q->u32_node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 309 | root_ht = kzalloc(sizeof(*root_ht), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | if (root_ht == NULL) |
| 311 | return -ENOBUFS; |
| 312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | root_ht->divisor = 0; |
| 314 | root_ht->refcnt++; |
| 315 | root_ht->handle = tp_c ? gen_new_htid(tp_c) : 0x80000000; |
| 316 | root_ht->prio = tp->prio; |
| 317 | |
| 318 | if (tp_c == NULL) { |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 319 | tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | if (tp_c == NULL) { |
| 321 | kfree(root_ht); |
| 322 | return -ENOBUFS; |
| 323 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | tp_c->q = tp->q; |
David S. Miller | 72b25a9 | 2008-07-18 20:54:17 -0700 | [diff] [blame] | 325 | tp->q->u32_node = tp_c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | tp_c->refcnt++; |
| 329 | root_ht->next = tp_c->hlist; |
| 330 | tp_c->hlist = root_ht; |
| 331 | root_ht->tp_c = tp_c; |
| 332 | |
| 333 | tp->root = root_ht; |
| 334 | tp->data = tp_c; |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n) |
| 339 | { |
| 340 | tcf_unbind_filter(tp, &n->res); |
| 341 | tcf_exts_destroy(tp, &n->exts); |
| 342 | if (n->ht_down) |
| 343 | n->ht_down->refcnt--; |
| 344 | #ifdef CONFIG_CLS_U32_PERF |
Patrick McHardy | 1ae39a4 | 2006-03-23 01:16:48 -0800 | [diff] [blame] | 345 | kfree(n->pf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | #endif |
| 347 | kfree(n); |
| 348 | return 0; |
| 349 | } |
| 350 | |
Yang Yingliang | 82d567c | 2013-12-10 20:55:31 +0800 | [diff] [blame] | 351 | static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | { |
| 353 | struct tc_u_knode **kp; |
| 354 | struct tc_u_hnode *ht = key->ht_up; |
| 355 | |
| 356 | if (ht) { |
| 357 | for (kp = &ht->ht[TC_U32_HASH(key->handle)]; *kp; kp = &(*kp)->next) { |
| 358 | if (*kp == key) { |
| 359 | tcf_tree_lock(tp); |
| 360 | *kp = key->next; |
| 361 | tcf_tree_unlock(tp); |
| 362 | |
| 363 | u32_destroy_key(tp, key); |
| 364 | return 0; |
| 365 | } |
| 366 | } |
| 367 | } |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 368 | WARN_ON(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht) |
| 373 | { |
| 374 | struct tc_u_knode *n; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 375 | unsigned int h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 377 | for (h = 0; h <= ht->divisor; h++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | while ((n = ht->ht[h]) != NULL) { |
| 379 | ht->ht[h] = n->next; |
| 380 | |
| 381 | u32_destroy_key(tp, n); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht) |
| 387 | { |
| 388 | struct tc_u_common *tp_c = tp->data; |
| 389 | struct tc_u_hnode **hn; |
| 390 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 391 | WARN_ON(ht->refcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
| 393 | u32_clear_hnode(tp, ht); |
| 394 | |
| 395 | for (hn = &tp_c->hlist; *hn; hn = &(*hn)->next) { |
| 396 | if (*hn == ht) { |
| 397 | *hn = ht->next; |
| 398 | kfree(ht); |
| 399 | return 0; |
| 400 | } |
| 401 | } |
| 402 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 403 | WARN_ON(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | return -ENOENT; |
| 405 | } |
| 406 | |
| 407 | static void u32_destroy(struct tcf_proto *tp) |
| 408 | { |
| 409 | struct tc_u_common *tp_c = tp->data; |
Patrick McHardy | 47a1a1d | 2008-11-19 08:03:09 +0000 | [diff] [blame] | 410 | struct tc_u_hnode *root_ht = tp->root; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 412 | WARN_ON(root_ht == NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
| 414 | if (root_ht && --root_ht->refcnt == 0) |
| 415 | u32_destroy_hnode(tp, root_ht); |
| 416 | |
| 417 | if (--tp_c->refcnt == 0) { |
| 418 | struct tc_u_hnode *ht; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
David S. Miller | 72b25a9 | 2008-07-18 20:54:17 -0700 | [diff] [blame] | 420 | tp->q->u32_node = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
Jarek Poplawski | e56cfad | 2008-04-12 18:37:13 -0700 | [diff] [blame] | 422 | for (ht = tp_c->hlist; ht; ht = ht->next) { |
| 423 | ht->refcnt--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | u32_clear_hnode(tp, ht); |
Jarek Poplawski | e56cfad | 2008-04-12 18:37:13 -0700 | [diff] [blame] | 425 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
| 427 | while ((ht = tp_c->hlist) != NULL) { |
| 428 | tp_c->hlist = ht->next; |
| 429 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 430 | WARN_ON(ht->refcnt != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
| 432 | kfree(ht); |
Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 433 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
| 435 | kfree(tp_c); |
| 436 | } |
| 437 | |
| 438 | tp->data = NULL; |
| 439 | } |
| 440 | |
| 441 | static int u32_delete(struct tcf_proto *tp, unsigned long arg) |
| 442 | { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 443 | struct tc_u_hnode *ht = (struct tc_u_hnode *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
| 445 | if (ht == NULL) |
| 446 | return 0; |
| 447 | |
| 448 | if (TC_U32_KEY(ht->handle)) |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 449 | return u32_delete_key(tp, (struct tc_u_knode *)ht); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
| 451 | if (tp->root == ht) |
| 452 | return -EINVAL; |
| 453 | |
Jarek Poplawski | e56cfad | 2008-04-12 18:37:13 -0700 | [diff] [blame] | 454 | if (ht->refcnt == 1) { |
| 455 | ht->refcnt--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | u32_destroy_hnode(tp, ht); |
Jarek Poplawski | e56cfad | 2008-04-12 18:37:13 -0700 | [diff] [blame] | 457 | } else { |
| 458 | return -EBUSY; |
| 459 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
| 461 | return 0; |
| 462 | } |
| 463 | |
Cong Wang | 7801db8 | 2014-07-17 17:34:53 -0700 | [diff] [blame] | 464 | #define NR_U32_NODE (1<<12) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | static u32 gen_new_kid(struct tc_u_hnode *ht, u32 handle) |
| 466 | { |
| 467 | struct tc_u_knode *n; |
Cong Wang | 7801db8 | 2014-07-17 17:34:53 -0700 | [diff] [blame] | 468 | unsigned long i; |
| 469 | unsigned long *bitmap = kzalloc(BITS_TO_LONGS(NR_U32_NODE) * sizeof(unsigned long), |
| 470 | GFP_KERNEL); |
| 471 | if (!bitmap) |
| 472 | return handle | 0xFFF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 474 | for (n = ht->ht[TC_U32_HASH(handle)]; n; n = n->next) |
Cong Wang | 7801db8 | 2014-07-17 17:34:53 -0700 | [diff] [blame] | 475 | set_bit(TC_U32_NODE(n->handle), bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
Cong Wang | 7801db8 | 2014-07-17 17:34:53 -0700 | [diff] [blame] | 477 | i = find_next_zero_bit(bitmap, NR_U32_NODE, 0x800); |
| 478 | if (i >= NR_U32_NODE) |
| 479 | i = find_next_zero_bit(bitmap, NR_U32_NODE, 1); |
| 480 | |
| 481 | kfree(bitmap); |
| 482 | return handle | (i >= NR_U32_NODE ? 0xFFF : i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Patrick McHardy | 6fa8c01 | 2008-01-23 20:36:12 -0800 | [diff] [blame] | 485 | static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = { |
| 486 | [TCA_U32_CLASSID] = { .type = NLA_U32 }, |
| 487 | [TCA_U32_HASH] = { .type = NLA_U32 }, |
| 488 | [TCA_U32_LINK] = { .type = NLA_U32 }, |
| 489 | [TCA_U32_DIVISOR] = { .type = NLA_U32 }, |
| 490 | [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) }, |
| 491 | [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ }, |
| 492 | [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) }, |
| 493 | }; |
| 494 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 495 | static int u32_set_parms(struct net *net, struct tcf_proto *tp, |
| 496 | unsigned long base, struct tc_u_hnode *ht, |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 497 | struct tc_u_knode *n, struct nlattr **tb, |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 498 | struct nlattr *est, bool ovr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | { |
| 500 | int err; |
| 501 | struct tcf_exts e; |
| 502 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 503 | tcf_exts_init(&e, TCA_U32_ACT, TCA_U32_POLICE); |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 504 | err = tcf_exts_validate(net, tp, tb, est, &e, ovr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | if (err < 0) |
| 506 | return err; |
| 507 | |
| 508 | err = -EINVAL; |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 509 | if (tb[TCA_U32_LINK]) { |
Patrick McHardy | 1587bac | 2008-01-23 20:35:03 -0800 | [diff] [blame] | 510 | u32 handle = nla_get_u32(tb[TCA_U32_LINK]); |
Patrick McHardy | 47a1a1d | 2008-11-19 08:03:09 +0000 | [diff] [blame] | 511 | struct tc_u_hnode *ht_down = NULL, *ht_old; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | |
| 513 | if (TC_U32_KEY(handle)) |
| 514 | goto errout; |
| 515 | |
| 516 | if (handle) { |
| 517 | ht_down = u32_lookup_ht(ht->tp_c, handle); |
| 518 | |
| 519 | if (ht_down == NULL) |
| 520 | goto errout; |
| 521 | ht_down->refcnt++; |
| 522 | } |
| 523 | |
| 524 | tcf_tree_lock(tp); |
Patrick McHardy | 47a1a1d | 2008-11-19 08:03:09 +0000 | [diff] [blame] | 525 | ht_old = n->ht_down; |
| 526 | n->ht_down = ht_down; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | tcf_tree_unlock(tp); |
| 528 | |
Patrick McHardy | 47a1a1d | 2008-11-19 08:03:09 +0000 | [diff] [blame] | 529 | if (ht_old) |
| 530 | ht_old->refcnt--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | } |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 532 | if (tb[TCA_U32_CLASSID]) { |
Patrick McHardy | 1587bac | 2008-01-23 20:35:03 -0800 | [diff] [blame] | 533 | n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | tcf_bind_filter(tp, &n->res, base); |
| 535 | } |
| 536 | |
| 537 | #ifdef CONFIG_NET_CLS_IND |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 538 | if (tb[TCA_U32_INDEV]) { |
WANG Cong | 2519a60 | 2014-01-09 16:14:02 -0800 | [diff] [blame] | 539 | int ret; |
| 540 | ret = tcf_change_indev(net, tb[TCA_U32_INDEV]); |
| 541 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | goto errout; |
WANG Cong | 2519a60 | 2014-01-09 16:14:02 -0800 | [diff] [blame] | 543 | n->ifindex = ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } |
| 545 | #endif |
| 546 | tcf_exts_change(tp, &n->exts, &e); |
| 547 | |
| 548 | return 0; |
| 549 | errout: |
| 550 | tcf_exts_destroy(tp, &e); |
| 551 | return err; |
| 552 | } |
| 553 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 554 | static int u32_change(struct net *net, struct sk_buff *in_skb, |
Eric W. Biederman | af4c664 | 2012-05-25 13:42:45 -0600 | [diff] [blame] | 555 | struct tcf_proto *tp, unsigned long base, u32 handle, |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 556 | struct nlattr **tca, |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 557 | unsigned long *arg, bool ovr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | { |
| 559 | struct tc_u_common *tp_c = tp->data; |
| 560 | struct tc_u_hnode *ht; |
| 561 | struct tc_u_knode *n; |
| 562 | struct tc_u32_sel *s; |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 563 | struct nlattr *opt = tca[TCA_OPTIONS]; |
| 564 | struct nlattr *tb[TCA_U32_MAX + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | u32 htid; |
| 566 | int err; |
| 567 | |
| 568 | if (opt == NULL) |
| 569 | return handle ? -EINVAL : 0; |
| 570 | |
Patrick McHardy | 6fa8c01 | 2008-01-23 20:36:12 -0800 | [diff] [blame] | 571 | err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 572 | if (err < 0) |
| 573 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 575 | n = (struct tc_u_knode *)*arg; |
| 576 | if (n) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | if (TC_U32_KEY(n->handle) == 0) |
| 578 | return -EINVAL; |
| 579 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 580 | return u32_set_parms(net, tp, base, n->ht_up, n, tb, |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 581 | tca[TCA_RATE], ovr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 584 | if (tb[TCA_U32_DIVISOR]) { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 585 | unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
| 587 | if (--divisor > 0x100) |
| 588 | return -EINVAL; |
| 589 | if (TC_U32_KEY(handle)) |
| 590 | return -EINVAL; |
| 591 | if (handle == 0) { |
| 592 | handle = gen_new_htid(tp->data); |
| 593 | if (handle == 0) |
| 594 | return -ENOMEM; |
| 595 | } |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 596 | ht = kzalloc(sizeof(*ht) + divisor*sizeof(void *), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | if (ht == NULL) |
| 598 | return -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | ht->tp_c = tp_c; |
Jarek Poplawski | e56cfad | 2008-04-12 18:37:13 -0700 | [diff] [blame] | 600 | ht->refcnt = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | ht->divisor = divisor; |
| 602 | ht->handle = handle; |
| 603 | ht->prio = tp->prio; |
| 604 | ht->next = tp_c->hlist; |
| 605 | tp_c->hlist = ht; |
| 606 | *arg = (unsigned long)ht; |
| 607 | return 0; |
| 608 | } |
| 609 | |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 610 | if (tb[TCA_U32_HASH]) { |
Patrick McHardy | 1587bac | 2008-01-23 20:35:03 -0800 | [diff] [blame] | 611 | htid = nla_get_u32(tb[TCA_U32_HASH]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | if (TC_U32_HTID(htid) == TC_U32_ROOT) { |
| 613 | ht = tp->root; |
| 614 | htid = ht->handle; |
| 615 | } else { |
| 616 | ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid)); |
| 617 | if (ht == NULL) |
| 618 | return -EINVAL; |
| 619 | } |
| 620 | } else { |
| 621 | ht = tp->root; |
| 622 | htid = ht->handle; |
| 623 | } |
| 624 | |
| 625 | if (ht->divisor < TC_U32_HASH(htid)) |
| 626 | return -EINVAL; |
| 627 | |
| 628 | if (handle) { |
| 629 | if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid)) |
| 630 | return -EINVAL; |
| 631 | handle = htid | TC_U32_NODE(handle); |
| 632 | } else |
| 633 | handle = gen_new_kid(ht, htid); |
| 634 | |
Patrick McHardy | 6fa8c01 | 2008-01-23 20:36:12 -0800 | [diff] [blame] | 635 | if (tb[TCA_U32_SEL] == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | return -EINVAL; |
| 637 | |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 638 | s = nla_data(tb[TCA_U32_SEL]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 640 | n = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | if (n == NULL) |
| 642 | return -ENOBUFS; |
| 643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | #ifdef CONFIG_CLS_U32_PERF |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 645 | n->pf = kzalloc(sizeof(struct tc_u32_pcnt) + s->nkeys*sizeof(u64), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | if (n->pf == NULL) { |
| 647 | kfree(n); |
| 648 | return -ENOBUFS; |
| 649 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | #endif |
| 651 | |
| 652 | memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key)); |
| 653 | n->ht_up = ht; |
| 654 | n->handle = handle; |
Radu Rendec | b226801 | 2007-11-10 21:54:50 -0800 | [diff] [blame] | 655 | n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0; |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 656 | tcf_exts_init(&n->exts, TCA_U32_ACT, TCA_U32_POLICE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | |
| 658 | #ifdef CONFIG_CLS_U32_MARK |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 659 | if (tb[TCA_U32_MARK]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | struct tc_u32_mark *mark; |
| 661 | |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 662 | mark = nla_data(tb[TCA_U32_MARK]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | memcpy(&n->mark, mark, sizeof(struct tc_u32_mark)); |
| 664 | n->mark.success = 0; |
| 665 | } |
| 666 | #endif |
| 667 | |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 668 | err = u32_set_parms(net, tp, base, ht, n, tb, tca[TCA_RATE], ovr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | if (err == 0) { |
| 670 | struct tc_u_knode **ins; |
| 671 | for (ins = &ht->ht[TC_U32_HASH(handle)]; *ins; ins = &(*ins)->next) |
| 672 | if (TC_U32_NODE(handle) < TC_U32_NODE((*ins)->handle)) |
| 673 | break; |
| 674 | |
| 675 | n->next = *ins; |
Jarek Poplawski | 6f57321 | 2009-01-05 18:14:19 -0800 | [diff] [blame] | 676 | tcf_tree_lock(tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | *ins = n; |
Jarek Poplawski | 6f57321 | 2009-01-05 18:14:19 -0800 | [diff] [blame] | 678 | tcf_tree_unlock(tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | |
| 680 | *arg = (unsigned long)n; |
| 681 | return 0; |
| 682 | } |
| 683 | #ifdef CONFIG_CLS_U32_PERF |
Patrick McHardy | 1ae39a4 | 2006-03-23 01:16:48 -0800 | [diff] [blame] | 684 | kfree(n->pf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | #endif |
| 686 | kfree(n); |
| 687 | return err; |
| 688 | } |
| 689 | |
| 690 | static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg) |
| 691 | { |
| 692 | struct tc_u_common *tp_c = tp->data; |
| 693 | struct tc_u_hnode *ht; |
| 694 | struct tc_u_knode *n; |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 695 | unsigned int h; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | |
| 697 | if (arg->stop) |
| 698 | return; |
| 699 | |
| 700 | for (ht = tp_c->hlist; ht; ht = ht->next) { |
| 701 | if (ht->prio != tp->prio) |
| 702 | continue; |
| 703 | if (arg->count >= arg->skip) { |
| 704 | if (arg->fn(tp, (unsigned long)ht, arg) < 0) { |
| 705 | arg->stop = 1; |
| 706 | return; |
| 707 | } |
| 708 | } |
| 709 | arg->count++; |
| 710 | for (h = 0; h <= ht->divisor; h++) { |
| 711 | for (n = ht->ht[h]; n; n = n->next) { |
| 712 | if (arg->count < arg->skip) { |
| 713 | arg->count++; |
| 714 | continue; |
| 715 | } |
| 716 | if (arg->fn(tp, (unsigned long)n, arg) < 0) { |
| 717 | arg->stop = 1; |
| 718 | return; |
| 719 | } |
| 720 | arg->count++; |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
WANG Cong | 832d1d5 | 2014-01-09 16:14:01 -0800 | [diff] [blame] | 726 | static int u32_dump(struct net *net, struct tcf_proto *tp, unsigned long fh, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | struct sk_buff *skb, struct tcmsg *t) |
| 728 | { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 729 | struct tc_u_knode *n = (struct tc_u_knode *)fh; |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 730 | struct nlattr *nest; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | |
| 732 | if (n == NULL) |
| 733 | return skb->len; |
| 734 | |
| 735 | t->tcm_handle = n->handle; |
| 736 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 737 | nest = nla_nest_start(skb, TCA_OPTIONS); |
| 738 | if (nest == NULL) |
| 739 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | |
| 741 | if (TC_U32_KEY(n->handle) == 0) { |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 742 | struct tc_u_hnode *ht = (struct tc_u_hnode *)fh; |
| 743 | u32 divisor = ht->divisor + 1; |
| 744 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 745 | if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor)) |
| 746 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | } else { |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 748 | if (nla_put(skb, TCA_U32_SEL, |
| 749 | sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key), |
| 750 | &n->sel)) |
| 751 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | if (n->ht_up) { |
| 753 | u32 htid = n->handle & 0xFFFFF000; |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 754 | if (nla_put_u32(skb, TCA_U32_HASH, htid)) |
| 755 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 757 | if (n->res.classid && |
| 758 | nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid)) |
| 759 | goto nla_put_failure; |
| 760 | if (n->ht_down && |
| 761 | nla_put_u32(skb, TCA_U32_LINK, n->ht_down->handle)) |
| 762 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | |
| 764 | #ifdef CONFIG_CLS_U32_MARK |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 765 | if ((n->mark.val || n->mark.mask) && |
| 766 | nla_put(skb, TCA_U32_MARK, sizeof(n->mark), &n->mark)) |
| 767 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | #endif |
| 769 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 770 | if (tcf_exts_dump(skb, &n->exts) < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 771 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | |
| 773 | #ifdef CONFIG_NET_CLS_IND |
WANG Cong | 2519a60 | 2014-01-09 16:14:02 -0800 | [diff] [blame] | 774 | if (n->ifindex) { |
| 775 | struct net_device *dev; |
| 776 | dev = __dev_get_by_index(net, n->ifindex); |
| 777 | if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name)) |
| 778 | goto nla_put_failure; |
| 779 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | #endif |
| 781 | #ifdef CONFIG_CLS_U32_PERF |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 782 | if (nla_put(skb, TCA_U32_PCNT, |
| 783 | sizeof(struct tc_u32_pcnt) + n->sel.nkeys*sizeof(u64), |
| 784 | n->pf)) |
| 785 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | #endif |
| 787 | } |
| 788 | |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 789 | nla_nest_end(skb, nest); |
| 790 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | if (TC_U32_KEY(n->handle)) |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 792 | if (tcf_exts_dump_stats(skb, &n->exts) < 0) |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 793 | goto nla_put_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | return skb->len; |
| 795 | |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 796 | nla_put_failure: |
Patrick McHardy | 4b3550ef | 2008-01-23 20:34:11 -0800 | [diff] [blame] | 797 | nla_nest_cancel(skb, nest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | return -1; |
| 799 | } |
| 800 | |
Patrick McHardy | 2eb9d75 | 2008-01-22 22:10:42 -0800 | [diff] [blame] | 801 | static struct tcf_proto_ops cls_u32_ops __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | .kind = "u32", |
| 803 | .classify = u32_classify, |
| 804 | .init = u32_init, |
| 805 | .destroy = u32_destroy, |
| 806 | .get = u32_get, |
| 807 | .put = u32_put, |
| 808 | .change = u32_change, |
| 809 | .delete = u32_delete, |
| 810 | .walk = u32_walk, |
| 811 | .dump = u32_dump, |
| 812 | .owner = THIS_MODULE, |
| 813 | }; |
| 814 | |
| 815 | static int __init init_u32(void) |
| 816 | { |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 817 | pr_info("u32 classifier\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | #ifdef CONFIG_CLS_U32_PERF |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 819 | pr_info(" Performance counters on\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | #ifdef CONFIG_NET_CLS_IND |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 822 | pr_info(" input device check on\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | #endif |
| 824 | #ifdef CONFIG_NET_CLS_ACT |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 825 | pr_info(" Actions configured\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | #endif |
| 827 | return register_tcf_proto_ops(&cls_u32_ops); |
| 828 | } |
| 829 | |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 830 | static void __exit exit_u32(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | { |
| 832 | unregister_tcf_proto_ops(&cls_u32_ops); |
| 833 | } |
| 834 | |
| 835 | module_init(init_u32) |
| 836 | module_exit(exit_u32) |
| 837 | MODULE_LICENSE("GPL"); |