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 33 #include <linux/module.h> 34 #include <linux/slab.h> 35 #include <linux/types.h> 36 #include <linux/kernel.h> 37 #include <linux/string.h> 38 #include <linux/errno.h> 39 #include <linux/percpu.h> 40 #include <linux/rtnetlink.h> 41 #include <linux/skbuff.h> 42 #include <linux/bitmap.h> 43 #include <linux/netdevice.h> 44 #include <linux/hash.h> 45 #include <net/netlink.h> 46 #include <net/act_api.h> 47 #include <net/pkt_cls.h> 48 #include <linux/netdevice.h> 49 #include <linux/idr.h> 50 51 struct tc_u_knode { 52 struct tc_u_knode __rcu *next; 53 u32 handle; 54 struct tc_u_hnode __rcu *ht_up; 55 struct tcf_exts exts; 56 #ifdef CONFIG_NET_CLS_IND 57 int ifindex; 58 #endif 59 u8 fshift; 60 struct tcf_result res; 61 struct tc_u_hnode __rcu *ht_down; 62 #ifdef CONFIG_CLS_U32_PERF 63 struct tc_u32_pcnt __percpu *pf; 64 #endif 65 u32 flags; 66 #ifdef CONFIG_CLS_U32_MARK 67 u32 val; 68 u32 mask; 69 u32 __percpu *pcpu_success; 70 #endif 71 struct tcf_proto *tp; 72 struct rcu_head rcu; 73 /* The 'sel' field MUST be the last field in structure to allow for 74 * tc_u32_keys allocated at end of structure. 75 */ 76 struct tc_u32_sel sel; 77 }; 78 79 struct tc_u_hnode { 80 struct tc_u_hnode __rcu *next; 81 u32 handle; 82 u32 prio; 83 struct tc_u_common *tp_c; 84 int refcnt; 85 unsigned int divisor; 86 struct idr handle_idr; 87 struct rcu_head rcu; 88 /* The 'ht' field MUST be the last field in structure to allow for 89 * more entries allocated at end of structure. 90 */ 91 struct tc_u_knode __rcu *ht[1]; 92 }; 93 94 struct tc_u_common { 95 struct tc_u_hnode __rcu *hlist; 96 struct tcf_block *block; 97 int refcnt; 98 struct idr handle_idr; 99 struct hlist_node hnode; 100 struct rcu_head rcu; 101 }; 102 103 static inline unsigned int u32_hash_fold(__be32 key, 104 const struct tc_u32_sel *sel, 105 u8 fshift) 106 { 107 unsigned int h = ntohl(key & sel->hmask) >> fshift; 108 109 return h; 110 } 111 112 static int u32_classify(struct sk_buff *skb, const struct tcf_proto *tp, 113 struct tcf_result *res) 114 { 115 struct { 116 struct tc_u_knode *knode; 117 unsigned int off; 118 } stack[TC_U32_MAXDEPTH]; 119 120 struct tc_u_hnode *ht = rcu_dereference_bh(tp->root); 121 unsigned int off = skb_network_offset(skb); 122 struct tc_u_knode *n; 123 int sdepth = 0; 124 int off2 = 0; 125 int sel = 0; 126 #ifdef CONFIG_CLS_U32_PERF 127 int j; 128 #endif 129 int i, r; 130 131 next_ht: 132 n = rcu_dereference_bh(ht->ht[sel]); 133 134 next_knode: 135 if (n) { 136 struct tc_u32_key *key = n->sel.keys; 137 138 #ifdef CONFIG_CLS_U32_PERF 139 __this_cpu_inc(n->pf->rcnt); 140 j = 0; 141 #endif 142 143 if (tc_skip_sw(n->flags)) { 144 n = rcu_dereference_bh(n->next); 145 goto next_knode; 146 } 147 148 #ifdef CONFIG_CLS_U32_MARK 149 if ((skb->mark & n->mask) != n->val) { 150 n = rcu_dereference_bh(n->next); 151 goto next_knode; 152 } else { 153 __this_cpu_inc(*n->pcpu_success); 154 } 155 #endif 156 157 for (i = n->sel.nkeys; i > 0; i--, key++) { 158 int toff = off + key->off + (off2 & key->offmask); 159 __be32 *data, hdata; 160 161 if (skb_headroom(skb) + toff > INT_MAX) 162 goto out; 163 164 data = skb_header_pointer(skb, toff, 4, &hdata); 165 if (!data) 166 goto out; 167 if ((*data ^ key->val) & key->mask) { 168 n = rcu_dereference_bh(n->next); 169 goto next_knode; 170 } 171 #ifdef CONFIG_CLS_U32_PERF 172 __this_cpu_inc(n->pf->kcnts[j]); 173 j++; 174 #endif 175 } 176 177 ht = rcu_dereference_bh(n->ht_down); 178 if (!ht) { 179 check_terminal: 180 if (n->sel.flags & TC_U32_TERMINAL) { 181 182 *res = n->res; 183 #ifdef CONFIG_NET_CLS_IND 184 if (!tcf_match_indev(skb, n->ifindex)) { 185 n = rcu_dereference_bh(n->next); 186 goto next_knode; 187 } 188 #endif 189 #ifdef CONFIG_CLS_U32_PERF 190 __this_cpu_inc(n->pf->rhit); 191 #endif 192 r = tcf_exts_exec(skb, &n->exts, res); 193 if (r < 0) { 194 n = rcu_dereference_bh(n->next); 195 goto next_knode; 196 } 197 198 return r; 199 } 200 n = rcu_dereference_bh(n->next); 201 goto next_knode; 202 } 203 204 /* PUSH */ 205 if (sdepth >= TC_U32_MAXDEPTH) 206 goto deadloop; 207 stack[sdepth].knode = n; 208 stack[sdepth].off = off; 209 sdepth++; 210 211 ht = rcu_dereference_bh(n->ht_down); 212 sel = 0; 213 if (ht->divisor) { 214 __be32 *data, hdata; 215 216 data = skb_header_pointer(skb, off + n->sel.hoff, 4, 217 &hdata); 218 if (!data) 219 goto out; 220 sel = ht->divisor & u32_hash_fold(*data, &n->sel, 221 n->fshift); 222 } 223 if (!(n->sel.flags & (TC_U32_VAROFFSET | TC_U32_OFFSET | TC_U32_EAT))) 224 goto next_ht; 225 226 if (n->sel.flags & (TC_U32_OFFSET | TC_U32_VAROFFSET)) { 227 off2 = n->sel.off + 3; 228 if (n->sel.flags & TC_U32_VAROFFSET) { 229 __be16 *data, hdata; 230 231 data = skb_header_pointer(skb, 232 off + n->sel.offoff, 233 2, &hdata); 234 if (!data) 235 goto out; 236 off2 += ntohs(n->sel.offmask & *data) >> 237 n->sel.offshift; 238 } 239 off2 &= ~3; 240 } 241 if (n->sel.flags & TC_U32_EAT) { 242 off += off2; 243 off2 = 0; 244 } 245 246 if (off < skb->len) 247 goto next_ht; 248 } 249 250 /* POP */ 251 if (sdepth--) { 252 n = stack[sdepth].knode; 253 ht = rcu_dereference_bh(n->ht_up); 254 off = stack[sdepth].off; 255 goto check_terminal; 256 } 257 out: 258 return -1; 259 260 deadloop: 261 net_warn_ratelimited("cls_u32: dead loop\n"); 262 return -1; 263 } 264 265 static struct tc_u_hnode *u32_lookup_ht(struct tc_u_common *tp_c, u32 handle) 266 { 267 struct tc_u_hnode *ht; 268 269 for (ht = rtnl_dereference(tp_c->hlist); 270 ht; 271 ht = rtnl_dereference(ht->next)) 272 if (ht->handle == handle) 273 break; 274 275 return ht; 276 } 277 278 static struct tc_u_knode *u32_lookup_key(struct tc_u_hnode *ht, u32 handle) 279 { 280 unsigned int sel; 281 struct tc_u_knode *n = NULL; 282 283 sel = TC_U32_HASH(handle); 284 if (sel > ht->divisor) 285 goto out; 286 287 for (n = rtnl_dereference(ht->ht[sel]); 288 n; 289 n = rtnl_dereference(n->next)) 290 if (n->handle == handle) 291 break; 292 out: 293 return n; 294 } 295 296 297 static void *u32_get(struct tcf_proto *tp, u32 handle) 298 { 299 struct tc_u_hnode *ht; 300 struct tc_u_common *tp_c = tp->data; 301 302 if (TC_U32_HTID(handle) == TC_U32_ROOT) 303 ht = rtnl_dereference(tp->root); 304 else 305 ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle)); 306 307 if (!ht) 308 return NULL; 309 310 if (TC_U32_KEY(handle) == 0) 311 return ht; 312 313 return u32_lookup_key(ht, handle); 314 } 315 316 static u32 gen_new_htid(struct tc_u_common *tp_c, struct tc_u_hnode *ptr) 317 { 318 unsigned long idr_index; 319 int err; 320 321 /* This is only used inside rtnl lock it is safe to increment 322 * without read _copy_ update semantics 323 */ 324 err = idr_alloc_ext(&tp_c->handle_idr, ptr, &idr_index, 325 1, 0x7FF, GFP_KERNEL); 326 if (err) 327 return 0; 328 return (u32)(idr_index | 0x800) << 20; 329 } 330 331 static struct hlist_head *tc_u_common_hash; 332 333 #define U32_HASH_SHIFT 10 334 #define U32_HASH_SIZE (1 << U32_HASH_SHIFT) 335 336 static unsigned int tc_u_hash(const struct tcf_proto *tp) 337 { 338 return hash_ptr(tp->chain->block, U32_HASH_SHIFT); 339 } 340 341 static struct tc_u_common *tc_u_common_find(const struct tcf_proto *tp) 342 { 343 struct tc_u_common *tc; 344 unsigned int h; 345 346 h = tc_u_hash(tp); 347 hlist_for_each_entry(tc, &tc_u_common_hash[h], hnode) { 348 if (tc->block == tp->chain->block) 349 return tc; 350 } 351 return NULL; 352 } 353 354 static int u32_init(struct tcf_proto *tp) 355 { 356 struct tc_u_hnode *root_ht; 357 struct tc_u_common *tp_c; 358 unsigned int h; 359 360 tp_c = tc_u_common_find(tp); 361 362 root_ht = kzalloc(sizeof(*root_ht), GFP_KERNEL); 363 if (root_ht == NULL) 364 return -ENOBUFS; 365 366 root_ht->refcnt++; 367 root_ht->handle = tp_c ? gen_new_htid(tp_c, root_ht) : 0x80000000; 368 root_ht->prio = tp->prio; 369 idr_init(&root_ht->handle_idr); 370 371 if (tp_c == NULL) { 372 tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL); 373 if (tp_c == NULL) { 374 kfree(root_ht); 375 return -ENOBUFS; 376 } 377 tp_c->block = tp->chain->block; 378 INIT_HLIST_NODE(&tp_c->hnode); 379 idr_init(&tp_c->handle_idr); 380 381 h = tc_u_hash(tp); 382 hlist_add_head(&tp_c->hnode, &tc_u_common_hash[h]); 383 } 384 385 tp_c->refcnt++; 386 RCU_INIT_POINTER(root_ht->next, tp_c->hlist); 387 rcu_assign_pointer(tp_c->hlist, root_ht); 388 root_ht->tp_c = tp_c; 389 390 rcu_assign_pointer(tp->root, root_ht); 391 tp->data = tp_c; 392 return 0; 393 } 394 395 static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n, 396 bool free_pf) 397 { 398 tcf_exts_destroy(&n->exts); 399 if (n->ht_down) 400 n->ht_down->refcnt--; 401 #ifdef CONFIG_CLS_U32_PERF 402 if (free_pf) 403 free_percpu(n->pf); 404 #endif 405 #ifdef CONFIG_CLS_U32_MARK 406 if (free_pf) 407 free_percpu(n->pcpu_success); 408 #endif 409 kfree(n); 410 return 0; 411 } 412 413 /* u32_delete_key_rcu should be called when free'ing a copied 414 * version of a tc_u_knode obtained from u32_init_knode(). When 415 * copies are obtained from u32_init_knode() the statistics are 416 * shared between the old and new copies to allow readers to 417 * continue to update the statistics during the copy. To support 418 * this the u32_delete_key_rcu variant does not free the percpu 419 * statistics. 420 */ 421 static void u32_delete_key_rcu(struct rcu_head *rcu) 422 { 423 struct tc_u_knode *key = container_of(rcu, struct tc_u_knode, rcu); 424 425 u32_destroy_key(key->tp, key, false); 426 } 427 428 /* u32_delete_key_freepf_rcu is the rcu callback variant 429 * that free's the entire structure including the statistics 430 * percpu variables. Only use this if the key is not a copy 431 * returned by u32_init_knode(). See u32_delete_key_rcu() 432 * for the variant that should be used with keys return from 433 * u32_init_knode() 434 */ 435 static void u32_delete_key_freepf_rcu(struct rcu_head *rcu) 436 { 437 struct tc_u_knode *key = container_of(rcu, struct tc_u_knode, rcu); 438 439 u32_destroy_key(key->tp, key, true); 440 } 441 442 static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode *key) 443 { 444 struct tc_u_knode __rcu **kp; 445 struct tc_u_knode *pkp; 446 struct tc_u_hnode *ht = rtnl_dereference(key->ht_up); 447 448 if (ht) { 449 kp = &ht->ht[TC_U32_HASH(key->handle)]; 450 for (pkp = rtnl_dereference(*kp); pkp; 451 kp = &pkp->next, pkp = rtnl_dereference(*kp)) { 452 if (pkp == key) { 453 RCU_INIT_POINTER(*kp, key->next); 454 455 tcf_unbind_filter(tp, &key->res); 456 call_rcu(&key->rcu, u32_delete_key_freepf_rcu); 457 return 0; 458 } 459 } 460 } 461 WARN_ON(1); 462 return 0; 463 } 464 465 static void u32_clear_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h) 466 { 467 struct tcf_block *block = tp->chain->block; 468 struct tc_cls_u32_offload cls_u32 = {}; 469 470 tc_cls_common_offload_init(&cls_u32.common, tp); 471 cls_u32.command = TC_CLSU32_DELETE_HNODE; 472 cls_u32.hnode.divisor = h->divisor; 473 cls_u32.hnode.handle = h->handle; 474 cls_u32.hnode.prio = h->prio; 475 476 tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, false); 477 } 478 479 static int u32_replace_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h, 480 u32 flags) 481 { 482 struct tcf_block *block = tp->chain->block; 483 struct tc_cls_u32_offload cls_u32 = {}; 484 bool skip_sw = tc_skip_sw(flags); 485 bool offloaded = false; 486 int err; 487 488 tc_cls_common_offload_init(&cls_u32.common, tp); 489 cls_u32.command = TC_CLSU32_NEW_HNODE; 490 cls_u32.hnode.divisor = h->divisor; 491 cls_u32.hnode.handle = h->handle; 492 cls_u32.hnode.prio = h->prio; 493 494 err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, skip_sw); 495 if (err < 0) { 496 u32_clear_hw_hnode(tp, h); 497 return err; 498 } else if (err > 0) { 499 offloaded = true; 500 } 501 502 if (skip_sw && !offloaded) 503 return -EINVAL; 504 505 return 0; 506 } 507 508 static void u32_remove_hw_knode(struct tcf_proto *tp, u32 handle) 509 { 510 struct tcf_block *block = tp->chain->block; 511 struct tc_cls_u32_offload cls_u32 = {}; 512 513 tc_cls_common_offload_init(&cls_u32.common, tp); 514 cls_u32.command = TC_CLSU32_DELETE_KNODE; 515 cls_u32.knode.handle = handle; 516 517 tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, false); 518 } 519 520 static int u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n, 521 u32 flags) 522 { 523 struct tcf_block *block = tp->chain->block; 524 struct tc_cls_u32_offload cls_u32 = {}; 525 bool skip_sw = tc_skip_sw(flags); 526 int err; 527 528 tc_cls_common_offload_init(&cls_u32.common, tp); 529 cls_u32.command = TC_CLSU32_REPLACE_KNODE; 530 cls_u32.knode.handle = n->handle; 531 cls_u32.knode.fshift = n->fshift; 532 #ifdef CONFIG_CLS_U32_MARK 533 cls_u32.knode.val = n->val; 534 cls_u32.knode.mask = n->mask; 535 #else 536 cls_u32.knode.val = 0; 537 cls_u32.knode.mask = 0; 538 #endif 539 cls_u32.knode.sel = &n->sel; 540 cls_u32.knode.exts = &n->exts; 541 if (n->ht_down) 542 cls_u32.knode.link_handle = n->ht_down->handle; 543 544 err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, skip_sw); 545 if (err < 0) { 546 u32_remove_hw_knode(tp, n->handle); 547 return err; 548 } else if (err > 0) { 549 n->flags |= TCA_CLS_FLAGS_IN_HW; 550 } 551 552 if (skip_sw && !(n->flags && TCA_CLS_FLAGS_IN_HW)) 553 return -EINVAL; 554 555 return 0; 556 } 557 558 static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht) 559 { 560 struct tc_u_knode *n; 561 unsigned int h; 562 563 for (h = 0; h <= ht->divisor; h++) { 564 while ((n = rtnl_dereference(ht->ht[h])) != NULL) { 565 RCU_INIT_POINTER(ht->ht[h], 566 rtnl_dereference(n->next)); 567 tcf_unbind_filter(tp, &n->res); 568 u32_remove_hw_knode(tp, n->handle); 569 idr_remove_ext(&ht->handle_idr, n->handle); 570 call_rcu(&n->rcu, u32_delete_key_freepf_rcu); 571 } 572 } 573 } 574 575 static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht) 576 { 577 struct tc_u_common *tp_c = tp->data; 578 struct tc_u_hnode __rcu **hn; 579 struct tc_u_hnode *phn; 580 581 WARN_ON(ht->refcnt); 582 583 u32_clear_hnode(tp, ht); 584 585 hn = &tp_c->hlist; 586 for (phn = rtnl_dereference(*hn); 587 phn; 588 hn = &phn->next, phn = rtnl_dereference(*hn)) { 589 if (phn == ht) { 590 u32_clear_hw_hnode(tp, ht); 591 idr_destroy(&ht->handle_idr); 592 idr_remove_ext(&tp_c->handle_idr, ht->handle); 593 RCU_INIT_POINTER(*hn, ht->next); 594 kfree_rcu(ht, rcu); 595 return 0; 596 } 597 } 598 599 return -ENOENT; 600 } 601 602 static bool ht_empty(struct tc_u_hnode *ht) 603 { 604 unsigned int h; 605 606 for (h = 0; h <= ht->divisor; h++) 607 if (rcu_access_pointer(ht->ht[h])) 608 return false; 609 610 return true; 611 } 612 613 static void u32_destroy(struct tcf_proto *tp) 614 { 615 struct tc_u_common *tp_c = tp->data; 616 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root); 617 618 WARN_ON(root_ht == NULL); 619 620 if (root_ht && --root_ht->refcnt == 0) 621 u32_destroy_hnode(tp, root_ht); 622 623 if (--tp_c->refcnt == 0) { 624 struct tc_u_hnode *ht; 625 626 hlist_del(&tp_c->hnode); 627 628 for (ht = rtnl_dereference(tp_c->hlist); 629 ht; 630 ht = rtnl_dereference(ht->next)) { 631 ht->refcnt--; 632 u32_clear_hnode(tp, ht); 633 } 634 635 while ((ht = rtnl_dereference(tp_c->hlist)) != NULL) { 636 RCU_INIT_POINTER(tp_c->hlist, ht->next); 637 kfree_rcu(ht, rcu); 638 } 639 640 idr_destroy(&tp_c->handle_idr); 641 kfree(tp_c); 642 } 643 644 tp->data = NULL; 645 } 646 647 static int u32_delete(struct tcf_proto *tp, void *arg, bool *last) 648 { 649 struct tc_u_hnode *ht = arg; 650 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root); 651 struct tc_u_common *tp_c = tp->data; 652 int ret = 0; 653 654 if (ht == NULL) 655 goto out; 656 657 if (TC_U32_KEY(ht->handle)) { 658 u32_remove_hw_knode(tp, ht->handle); 659 ret = u32_delete_key(tp, (struct tc_u_knode *)ht); 660 goto out; 661 } 662 663 if (root_ht == ht) 664 return -EINVAL; 665 666 if (ht->refcnt == 1) { 667 ht->refcnt--; 668 u32_destroy_hnode(tp, ht); 669 } else { 670 return -EBUSY; 671 } 672 673 out: 674 *last = true; 675 if (root_ht) { 676 if (root_ht->refcnt > 1) { 677 *last = false; 678 goto ret; 679 } 680 if (root_ht->refcnt == 1) { 681 if (!ht_empty(root_ht)) { 682 *last = false; 683 goto ret; 684 } 685 } 686 } 687 688 if (tp_c->refcnt > 1) { 689 *last = false; 690 goto ret; 691 } 692 693 if (tp_c->refcnt == 1) { 694 struct tc_u_hnode *ht; 695 696 for (ht = rtnl_dereference(tp_c->hlist); 697 ht; 698 ht = rtnl_dereference(ht->next)) 699 if (!ht_empty(ht)) { 700 *last = false; 701 break; 702 } 703 } 704 705 ret: 706 return ret; 707 } 708 709 static u32 gen_new_kid(struct tc_u_hnode *ht, u32 htid) 710 { 711 unsigned long idr_index; 712 u32 start = htid | 0x800; 713 u32 max = htid | 0xFFF; 714 u32 min = htid; 715 716 if (idr_alloc_ext(&ht->handle_idr, NULL, &idr_index, 717 start, max + 1, GFP_KERNEL)) { 718 if (idr_alloc_ext(&ht->handle_idr, NULL, &idr_index, 719 min + 1, max + 1, GFP_KERNEL)) 720 return max; 721 } 722 723 return (u32)idr_index; 724 } 725 726 static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = { 727 [TCA_U32_CLASSID] = { .type = NLA_U32 }, 728 [TCA_U32_HASH] = { .type = NLA_U32 }, 729 [TCA_U32_LINK] = { .type = NLA_U32 }, 730 [TCA_U32_DIVISOR] = { .type = NLA_U32 }, 731 [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) }, 732 [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ }, 733 [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) }, 734 [TCA_U32_FLAGS] = { .type = NLA_U32 }, 735 }; 736 737 static int u32_set_parms(struct net *net, struct tcf_proto *tp, 738 unsigned long base, struct tc_u_hnode *ht, 739 struct tc_u_knode *n, struct nlattr **tb, 740 struct nlattr *est, bool ovr) 741 { 742 int err; 743 744 err = tcf_exts_validate(net, tp, tb, est, &n->exts, ovr); 745 if (err < 0) 746 return err; 747 748 if (tb[TCA_U32_LINK]) { 749 u32 handle = nla_get_u32(tb[TCA_U32_LINK]); 750 struct tc_u_hnode *ht_down = NULL, *ht_old; 751 752 if (TC_U32_KEY(handle)) 753 return -EINVAL; 754 755 if (handle) { 756 ht_down = u32_lookup_ht(ht->tp_c, handle); 757 758 if (ht_down == NULL) 759 return -EINVAL; 760 ht_down->refcnt++; 761 } 762 763 ht_old = rtnl_dereference(n->ht_down); 764 rcu_assign_pointer(n->ht_down, ht_down); 765 766 if (ht_old) 767 ht_old->refcnt--; 768 } 769 if (tb[TCA_U32_CLASSID]) { 770 n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]); 771 tcf_bind_filter(tp, &n->res, base); 772 } 773 774 #ifdef CONFIG_NET_CLS_IND 775 if (tb[TCA_U32_INDEV]) { 776 int ret; 777 ret = tcf_change_indev(net, tb[TCA_U32_INDEV]); 778 if (ret < 0) 779 return -EINVAL; 780 n->ifindex = ret; 781 } 782 #endif 783 return 0; 784 } 785 786 static void u32_replace_knode(struct tcf_proto *tp, struct tc_u_common *tp_c, 787 struct tc_u_knode *n) 788 { 789 struct tc_u_knode __rcu **ins; 790 struct tc_u_knode *pins; 791 struct tc_u_hnode *ht; 792 793 if (TC_U32_HTID(n->handle) == TC_U32_ROOT) 794 ht = rtnl_dereference(tp->root); 795 else 796 ht = u32_lookup_ht(tp_c, TC_U32_HTID(n->handle)); 797 798 ins = &ht->ht[TC_U32_HASH(n->handle)]; 799 800 /* The node must always exist for it to be replaced if this is not the 801 * case then something went very wrong elsewhere. 802 */ 803 for (pins = rtnl_dereference(*ins); ; 804 ins = &pins->next, pins = rtnl_dereference(*ins)) 805 if (pins->handle == n->handle) 806 break; 807 808 idr_replace_ext(&ht->handle_idr, n, n->handle); 809 RCU_INIT_POINTER(n->next, pins->next); 810 rcu_assign_pointer(*ins, n); 811 } 812 813 static struct tc_u_knode *u32_init_knode(struct tcf_proto *tp, 814 struct tc_u_knode *n) 815 { 816 struct tc_u_knode *new; 817 struct tc_u32_sel *s = &n->sel; 818 819 new = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), 820 GFP_KERNEL); 821 822 if (!new) 823 return NULL; 824 825 RCU_INIT_POINTER(new->next, n->next); 826 new->handle = n->handle; 827 RCU_INIT_POINTER(new->ht_up, n->ht_up); 828 829 #ifdef CONFIG_NET_CLS_IND 830 new->ifindex = n->ifindex; 831 #endif 832 new->fshift = n->fshift; 833 new->res = n->res; 834 new->flags = n->flags; 835 RCU_INIT_POINTER(new->ht_down, n->ht_down); 836 837 /* bump reference count as long as we hold pointer to structure */ 838 if (new->ht_down) 839 new->ht_down->refcnt++; 840 841 #ifdef CONFIG_CLS_U32_PERF 842 /* Statistics may be incremented by readers during update 843 * so we must keep them in tact. When the node is later destroyed 844 * a special destroy call must be made to not free the pf memory. 845 */ 846 new->pf = n->pf; 847 #endif 848 849 #ifdef CONFIG_CLS_U32_MARK 850 new->val = n->val; 851 new->mask = n->mask; 852 /* Similarly success statistics must be moved as pointers */ 853 new->pcpu_success = n->pcpu_success; 854 #endif 855 new->tp = tp; 856 memcpy(&new->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key)); 857 858 if (tcf_exts_init(&new->exts, TCA_U32_ACT, TCA_U32_POLICE)) { 859 kfree(new); 860 return NULL; 861 } 862 863 return new; 864 } 865 866 static int u32_change(struct net *net, struct sk_buff *in_skb, 867 struct tcf_proto *tp, unsigned long base, u32 handle, 868 struct nlattr **tca, void **arg, bool ovr) 869 { 870 struct tc_u_common *tp_c = tp->data; 871 struct tc_u_hnode *ht; 872 struct tc_u_knode *n; 873 struct tc_u32_sel *s; 874 struct nlattr *opt = tca[TCA_OPTIONS]; 875 struct nlattr *tb[TCA_U32_MAX + 1]; 876 u32 htid, flags = 0; 877 int err; 878 #ifdef CONFIG_CLS_U32_PERF 879 size_t size; 880 #endif 881 882 if (opt == NULL) 883 return handle ? -EINVAL : 0; 884 885 err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy, NULL); 886 if (err < 0) 887 return err; 888 889 if (tb[TCA_U32_FLAGS]) { 890 flags = nla_get_u32(tb[TCA_U32_FLAGS]); 891 if (!tc_flags_valid(flags)) 892 return -EINVAL; 893 } 894 895 n = *arg; 896 if (n) { 897 struct tc_u_knode *new; 898 899 if (TC_U32_KEY(n->handle) == 0) 900 return -EINVAL; 901 902 if (n->flags != flags) 903 return -EINVAL; 904 905 new = u32_init_knode(tp, n); 906 if (!new) 907 return -ENOMEM; 908 909 err = u32_set_parms(net, tp, base, 910 rtnl_dereference(n->ht_up), new, tb, 911 tca[TCA_RATE], ovr); 912 913 if (err) { 914 u32_destroy_key(tp, new, false); 915 return err; 916 } 917 918 err = u32_replace_hw_knode(tp, new, flags); 919 if (err) { 920 u32_destroy_key(tp, new, false); 921 return err; 922 } 923 924 if (!tc_in_hw(new->flags)) 925 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW; 926 927 u32_replace_knode(tp, tp_c, new); 928 tcf_unbind_filter(tp, &n->res); 929 call_rcu(&n->rcu, u32_delete_key_rcu); 930 return 0; 931 } 932 933 if (tb[TCA_U32_DIVISOR]) { 934 unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]); 935 936 if (--divisor > 0x100) 937 return -EINVAL; 938 if (TC_U32_KEY(handle)) 939 return -EINVAL; 940 ht = kzalloc(sizeof(*ht) + divisor*sizeof(void *), GFP_KERNEL); 941 if (ht == NULL) 942 return -ENOBUFS; 943 if (handle == 0) { 944 handle = gen_new_htid(tp->data, ht); 945 if (handle == 0) { 946 kfree(ht); 947 return -ENOMEM; 948 } 949 } else { 950 err = idr_alloc_ext(&tp_c->handle_idr, ht, NULL, 951 handle, handle + 1, GFP_KERNEL); 952 if (err) { 953 kfree(ht); 954 return err; 955 } 956 } 957 ht->tp_c = tp_c; 958 ht->refcnt = 1; 959 ht->divisor = divisor; 960 ht->handle = handle; 961 ht->prio = tp->prio; 962 idr_init(&ht->handle_idr); 963 964 err = u32_replace_hw_hnode(tp, ht, flags); 965 if (err) { 966 idr_remove_ext(&tp_c->handle_idr, handle); 967 kfree(ht); 968 return err; 969 } 970 971 RCU_INIT_POINTER(ht->next, tp_c->hlist); 972 rcu_assign_pointer(tp_c->hlist, ht); 973 *arg = ht; 974 975 return 0; 976 } 977 978 if (tb[TCA_U32_HASH]) { 979 htid = nla_get_u32(tb[TCA_U32_HASH]); 980 if (TC_U32_HTID(htid) == TC_U32_ROOT) { 981 ht = rtnl_dereference(tp->root); 982 htid = ht->handle; 983 } else { 984 ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid)); 985 if (ht == NULL) 986 return -EINVAL; 987 } 988 } else { 989 ht = rtnl_dereference(tp->root); 990 htid = ht->handle; 991 } 992 993 if (ht->divisor < TC_U32_HASH(htid)) 994 return -EINVAL; 995 996 if (handle) { 997 if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid)) 998 return -EINVAL; 999 handle = htid | TC_U32_NODE(handle); 1000 err = idr_alloc_ext(&ht->handle_idr, NULL, NULL, 1001 handle, handle + 1, 1002 GFP_KERNEL); 1003 if (err) 1004 return err; 1005 } else 1006 handle = gen_new_kid(ht, htid); 1007 1008 if (tb[TCA_U32_SEL] == NULL) { 1009 err = -EINVAL; 1010 goto erridr; 1011 } 1012 1013 s = nla_data(tb[TCA_U32_SEL]); 1014 1015 n = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL); 1016 if (n == NULL) { 1017 err = -ENOBUFS; 1018 goto erridr; 1019 } 1020 1021 #ifdef CONFIG_CLS_U32_PERF 1022 size = sizeof(struct tc_u32_pcnt) + s->nkeys * sizeof(u64); 1023 n->pf = __alloc_percpu(size, __alignof__(struct tc_u32_pcnt)); 1024 if (!n->pf) { 1025 err = -ENOBUFS; 1026 goto errfree; 1027 } 1028 #endif 1029 1030 memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key)); 1031 RCU_INIT_POINTER(n->ht_up, ht); 1032 n->handle = handle; 1033 n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0; 1034 n->flags = flags; 1035 n->tp = tp; 1036 1037 err = tcf_exts_init(&n->exts, TCA_U32_ACT, TCA_U32_POLICE); 1038 if (err < 0) 1039 goto errout; 1040 1041 #ifdef CONFIG_CLS_U32_MARK 1042 n->pcpu_success = alloc_percpu(u32); 1043 if (!n->pcpu_success) { 1044 err = -ENOMEM; 1045 goto errout; 1046 } 1047 1048 if (tb[TCA_U32_MARK]) { 1049 struct tc_u32_mark *mark; 1050 1051 mark = nla_data(tb[TCA_U32_MARK]); 1052 n->val = mark->val; 1053 n->mask = mark->mask; 1054 } 1055 #endif 1056 1057 err = u32_set_parms(net, tp, base, ht, n, tb, tca[TCA_RATE], ovr); 1058 if (err == 0) { 1059 struct tc_u_knode __rcu **ins; 1060 struct tc_u_knode *pins; 1061 1062 err = u32_replace_hw_knode(tp, n, flags); 1063 if (err) 1064 goto errhw; 1065 1066 if (!tc_in_hw(n->flags)) 1067 n->flags |= TCA_CLS_FLAGS_NOT_IN_HW; 1068 1069 ins = &ht->ht[TC_U32_HASH(handle)]; 1070 for (pins = rtnl_dereference(*ins); pins; 1071 ins = &pins->next, pins = rtnl_dereference(*ins)) 1072 if (TC_U32_NODE(handle) < TC_U32_NODE(pins->handle)) 1073 break; 1074 1075 RCU_INIT_POINTER(n->next, pins); 1076 rcu_assign_pointer(*ins, n); 1077 *arg = n; 1078 return 0; 1079 } 1080 1081 errhw: 1082 #ifdef CONFIG_CLS_U32_MARK 1083 free_percpu(n->pcpu_success); 1084 #endif 1085 1086 errout: 1087 tcf_exts_destroy(&n->exts); 1088 #ifdef CONFIG_CLS_U32_PERF 1089 errfree: 1090 free_percpu(n->pf); 1091 #endif 1092 kfree(n); 1093 erridr: 1094 idr_remove_ext(&ht->handle_idr, handle); 1095 return err; 1096 } 1097 1098 static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg) 1099 { 1100 struct tc_u_common *tp_c = tp->data; 1101 struct tc_u_hnode *ht; 1102 struct tc_u_knode *n; 1103 unsigned int h; 1104 1105 if (arg->stop) 1106 return; 1107 1108 for (ht = rtnl_dereference(tp_c->hlist); 1109 ht; 1110 ht = rtnl_dereference(ht->next)) { 1111 if (ht->prio != tp->prio) 1112 continue; 1113 if (arg->count >= arg->skip) { 1114 if (arg->fn(tp, ht, arg) < 0) { 1115 arg->stop = 1; 1116 return; 1117 } 1118 } 1119 arg->count++; 1120 for (h = 0; h <= ht->divisor; h++) { 1121 for (n = rtnl_dereference(ht->ht[h]); 1122 n; 1123 n = rtnl_dereference(n->next)) { 1124 if (arg->count < arg->skip) { 1125 arg->count++; 1126 continue; 1127 } 1128 if (arg->fn(tp, n, arg) < 0) { 1129 arg->stop = 1; 1130 return; 1131 } 1132 arg->count++; 1133 } 1134 } 1135 } 1136 } 1137 1138 static void u32_bind_class(void *fh, u32 classid, unsigned long cl) 1139 { 1140 struct tc_u_knode *n = fh; 1141 1142 if (n && n->res.classid == classid) 1143 n->res.class = cl; 1144 } 1145 1146 static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh, 1147 struct sk_buff *skb, struct tcmsg *t) 1148 { 1149 struct tc_u_knode *n = fh; 1150 struct tc_u_hnode *ht_up, *ht_down; 1151 struct nlattr *nest; 1152 1153 if (n == NULL) 1154 return skb->len; 1155 1156 t->tcm_handle = n->handle; 1157 1158 nest = nla_nest_start(skb, TCA_OPTIONS); 1159 if (nest == NULL) 1160 goto nla_put_failure; 1161 1162 if (TC_U32_KEY(n->handle) == 0) { 1163 struct tc_u_hnode *ht = fh; 1164 u32 divisor = ht->divisor + 1; 1165 1166 if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor)) 1167 goto nla_put_failure; 1168 } else { 1169 #ifdef CONFIG_CLS_U32_PERF 1170 struct tc_u32_pcnt *gpf; 1171 int cpu; 1172 #endif 1173 1174 if (nla_put(skb, TCA_U32_SEL, 1175 sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key), 1176 &n->sel)) 1177 goto nla_put_failure; 1178 1179 ht_up = rtnl_dereference(n->ht_up); 1180 if (ht_up) { 1181 u32 htid = n->handle & 0xFFFFF000; 1182 if (nla_put_u32(skb, TCA_U32_HASH, htid)) 1183 goto nla_put_failure; 1184 } 1185 if (n->res.classid && 1186 nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid)) 1187 goto nla_put_failure; 1188 1189 ht_down = rtnl_dereference(n->ht_down); 1190 if (ht_down && 1191 nla_put_u32(skb, TCA_U32_LINK, ht_down->handle)) 1192 goto nla_put_failure; 1193 1194 if (n->flags && nla_put_u32(skb, TCA_U32_FLAGS, n->flags)) 1195 goto nla_put_failure; 1196 1197 #ifdef CONFIG_CLS_U32_MARK 1198 if ((n->val || n->mask)) { 1199 struct tc_u32_mark mark = {.val = n->val, 1200 .mask = n->mask, 1201 .success = 0}; 1202 int cpum; 1203 1204 for_each_possible_cpu(cpum) { 1205 __u32 cnt = *per_cpu_ptr(n->pcpu_success, cpum); 1206 1207 mark.success += cnt; 1208 } 1209 1210 if (nla_put(skb, TCA_U32_MARK, sizeof(mark), &mark)) 1211 goto nla_put_failure; 1212 } 1213 #endif 1214 1215 if (tcf_exts_dump(skb, &n->exts) < 0) 1216 goto nla_put_failure; 1217 1218 #ifdef CONFIG_NET_CLS_IND 1219 if (n->ifindex) { 1220 struct net_device *dev; 1221 dev = __dev_get_by_index(net, n->ifindex); 1222 if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name)) 1223 goto nla_put_failure; 1224 } 1225 #endif 1226 #ifdef CONFIG_CLS_U32_PERF 1227 gpf = kzalloc(sizeof(struct tc_u32_pcnt) + 1228 n->sel.nkeys * sizeof(u64), 1229 GFP_KERNEL); 1230 if (!gpf) 1231 goto nla_put_failure; 1232 1233 for_each_possible_cpu(cpu) { 1234 int i; 1235 struct tc_u32_pcnt *pf = per_cpu_ptr(n->pf, cpu); 1236 1237 gpf->rcnt += pf->rcnt; 1238 gpf->rhit += pf->rhit; 1239 for (i = 0; i < n->sel.nkeys; i++) 1240 gpf->kcnts[i] += pf->kcnts[i]; 1241 } 1242 1243 if (nla_put_64bit(skb, TCA_U32_PCNT, 1244 sizeof(struct tc_u32_pcnt) + 1245 n->sel.nkeys * sizeof(u64), 1246 gpf, TCA_U32_PAD)) { 1247 kfree(gpf); 1248 goto nla_put_failure; 1249 } 1250 kfree(gpf); 1251 #endif 1252 } 1253 1254 nla_nest_end(skb, nest); 1255 1256 if (TC_U32_KEY(n->handle)) 1257 if (tcf_exts_dump_stats(skb, &n->exts) < 0) 1258 goto nla_put_failure; 1259 return skb->len; 1260 1261 nla_put_failure: 1262 nla_nest_cancel(skb, nest); 1263 return -1; 1264 } 1265 1266 static struct tcf_proto_ops cls_u32_ops __read_mostly = { 1267 .kind = "u32", 1268 .classify = u32_classify, 1269 .init = u32_init, 1270 .destroy = u32_destroy, 1271 .get = u32_get, 1272 .change = u32_change, 1273 .delete = u32_delete, 1274 .walk = u32_walk, 1275 .dump = u32_dump, 1276 .bind_class = u32_bind_class, 1277 .owner = THIS_MODULE, 1278 }; 1279 1280 static int __init init_u32(void) 1281 { 1282 int i, ret; 1283 1284 pr_info("u32 classifier\n"); 1285 #ifdef CONFIG_CLS_U32_PERF 1286 pr_info(" Performance counters on\n"); 1287 #endif 1288 #ifdef CONFIG_NET_CLS_IND 1289 pr_info(" input device check on\n"); 1290 #endif 1291 #ifdef CONFIG_NET_CLS_ACT 1292 pr_info(" Actions configured\n"); 1293 #endif 1294 tc_u_common_hash = kvmalloc_array(U32_HASH_SIZE, 1295 sizeof(struct hlist_head), 1296 GFP_KERNEL); 1297 if (!tc_u_common_hash) 1298 return -ENOMEM; 1299 1300 for (i = 0; i < U32_HASH_SIZE; i++) 1301 INIT_HLIST_HEAD(&tc_u_common_hash[i]); 1302 1303 ret = register_tcf_proto_ops(&cls_u32_ops); 1304 if (ret) 1305 kvfree(tc_u_common_hash); 1306 return ret; 1307 } 1308 1309 static void __exit exit_u32(void) 1310 { 1311 unregister_tcf_proto_ops(&cls_u32_ops); 1312 kvfree(tc_u_common_hash); 1313 } 1314 1315 module_init(init_u32) 1316 module_exit(exit_u32) 1317 MODULE_LICENSE("GPL"); 1318