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_64((u64) 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_remove_hw_knode(struct tcf_proto *tp, u32 handle) 466 { 467 struct net_device *dev = tp->q->dev_queue->dev; 468 struct tc_cls_u32_offload cls_u32 = {}; 469 470 if (!tc_should_offload(dev, 0)) 471 return; 472 473 tc_cls_common_offload_init(&cls_u32.common, tp); 474 cls_u32.command = TC_CLSU32_DELETE_KNODE; 475 cls_u32.knode.handle = handle; 476 477 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSU32, &cls_u32); 478 } 479 480 static int u32_replace_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h, 481 u32 flags) 482 { 483 struct net_device *dev = tp->q->dev_queue->dev; 484 struct tc_cls_u32_offload cls_u32 = {}; 485 int err; 486 487 if (!tc_should_offload(dev, flags)) 488 return tc_skip_sw(flags) ? -EINVAL : 0; 489 490 tc_cls_common_offload_init(&cls_u32.common, tp); 491 cls_u32.command = TC_CLSU32_NEW_HNODE; 492 cls_u32.hnode.divisor = h->divisor; 493 cls_u32.hnode.handle = h->handle; 494 cls_u32.hnode.prio = h->prio; 495 496 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSU32, &cls_u32); 497 if (tc_skip_sw(flags)) 498 return err; 499 500 return 0; 501 } 502 503 static void u32_clear_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h) 504 { 505 struct net_device *dev = tp->q->dev_queue->dev; 506 struct tc_cls_u32_offload cls_u32 = {}; 507 508 if (!tc_should_offload(dev, 0)) 509 return; 510 511 tc_cls_common_offload_init(&cls_u32.common, tp); 512 cls_u32.command = TC_CLSU32_DELETE_HNODE; 513 cls_u32.hnode.divisor = h->divisor; 514 cls_u32.hnode.handle = h->handle; 515 cls_u32.hnode.prio = h->prio; 516 517 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSU32, &cls_u32); 518 } 519 520 static int u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n, 521 u32 flags) 522 { 523 struct net_device *dev = tp->q->dev_queue->dev; 524 struct tc_cls_u32_offload cls_u32 = {}; 525 int err; 526 527 if (!tc_should_offload(dev, flags)) 528 return tc_skip_sw(flags) ? -EINVAL : 0; 529 530 tc_cls_common_offload_init(&cls_u32.common, tp); 531 cls_u32.command = TC_CLSU32_REPLACE_KNODE; 532 cls_u32.knode.handle = n->handle; 533 cls_u32.knode.fshift = n->fshift; 534 #ifdef CONFIG_CLS_U32_MARK 535 cls_u32.knode.val = n->val; 536 cls_u32.knode.mask = n->mask; 537 #else 538 cls_u32.knode.val = 0; 539 cls_u32.knode.mask = 0; 540 #endif 541 cls_u32.knode.sel = &n->sel; 542 cls_u32.knode.exts = &n->exts; 543 if (n->ht_down) 544 cls_u32.knode.link_handle = n->ht_down->handle; 545 546 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSU32, &cls_u32); 547 548 if (!err) 549 n->flags |= TCA_CLS_FLAGS_IN_HW; 550 551 if (tc_skip_sw(flags)) 552 return err; 553 554 return 0; 555 } 556 557 static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht) 558 { 559 struct tc_u_knode *n; 560 unsigned int h; 561 562 for (h = 0; h <= ht->divisor; h++) { 563 while ((n = rtnl_dereference(ht->ht[h])) != NULL) { 564 RCU_INIT_POINTER(ht->ht[h], 565 rtnl_dereference(n->next)); 566 tcf_unbind_filter(tp, &n->res); 567 u32_remove_hw_knode(tp, n->handle); 568 idr_remove_ext(&ht->handle_idr, n->handle); 569 call_rcu(&n->rcu, u32_delete_key_freepf_rcu); 570 } 571 } 572 } 573 574 static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht) 575 { 576 struct tc_u_common *tp_c = tp->data; 577 struct tc_u_hnode __rcu **hn; 578 struct tc_u_hnode *phn; 579 580 WARN_ON(ht->refcnt); 581 582 u32_clear_hnode(tp, ht); 583 584 hn = &tp_c->hlist; 585 for (phn = rtnl_dereference(*hn); 586 phn; 587 hn = &phn->next, phn = rtnl_dereference(*hn)) { 588 if (phn == ht) { 589 u32_clear_hw_hnode(tp, ht); 590 idr_destroy(&ht->handle_idr); 591 idr_remove_ext(&tp_c->handle_idr, ht->handle); 592 RCU_INIT_POINTER(*hn, ht->next); 593 kfree_rcu(ht, rcu); 594 return 0; 595 } 596 } 597 598 return -ENOENT; 599 } 600 601 static bool ht_empty(struct tc_u_hnode *ht) 602 { 603 unsigned int h; 604 605 for (h = 0; h <= ht->divisor; h++) 606 if (rcu_access_pointer(ht->ht[h])) 607 return false; 608 609 return true; 610 } 611 612 static void u32_destroy(struct tcf_proto *tp) 613 { 614 struct tc_u_common *tp_c = tp->data; 615 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root); 616 617 WARN_ON(root_ht == NULL); 618 619 if (root_ht && --root_ht->refcnt == 0) 620 u32_destroy_hnode(tp, root_ht); 621 622 if (--tp_c->refcnt == 0) { 623 struct tc_u_hnode *ht; 624 625 hlist_del(&tp_c->hnode); 626 627 for (ht = rtnl_dereference(tp_c->hlist); 628 ht; 629 ht = rtnl_dereference(ht->next)) { 630 ht->refcnt--; 631 u32_clear_hnode(tp, ht); 632 } 633 634 while ((ht = rtnl_dereference(tp_c->hlist)) != NULL) { 635 RCU_INIT_POINTER(tp_c->hlist, ht->next); 636 kfree_rcu(ht, rcu); 637 } 638 639 idr_destroy(&tp_c->handle_idr); 640 kfree(tp_c); 641 } 642 643 tp->data = NULL; 644 } 645 646 static int u32_delete(struct tcf_proto *tp, void *arg, bool *last) 647 { 648 struct tc_u_hnode *ht = arg; 649 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root); 650 struct tc_u_common *tp_c = tp->data; 651 int ret = 0; 652 653 if (ht == NULL) 654 goto out; 655 656 if (TC_U32_KEY(ht->handle)) { 657 u32_remove_hw_knode(tp, ht->handle); 658 ret = u32_delete_key(tp, (struct tc_u_knode *)ht); 659 goto out; 660 } 661 662 if (root_ht == ht) 663 return -EINVAL; 664 665 if (ht->refcnt == 1) { 666 ht->refcnt--; 667 u32_destroy_hnode(tp, ht); 668 } else { 669 return -EBUSY; 670 } 671 672 out: 673 *last = true; 674 if (root_ht) { 675 if (root_ht->refcnt > 1) { 676 *last = false; 677 goto ret; 678 } 679 if (root_ht->refcnt == 1) { 680 if (!ht_empty(root_ht)) { 681 *last = false; 682 goto ret; 683 } 684 } 685 } 686 687 if (tp_c->refcnt > 1) { 688 *last = false; 689 goto ret; 690 } 691 692 if (tp_c->refcnt == 1) { 693 struct tc_u_hnode *ht; 694 695 for (ht = rtnl_dereference(tp_c->hlist); 696 ht; 697 ht = rtnl_dereference(ht->next)) 698 if (!ht_empty(ht)) { 699 *last = false; 700 break; 701 } 702 } 703 704 ret: 705 return ret; 706 } 707 708 static u32 gen_new_kid(struct tc_u_hnode *ht, u32 htid) 709 { 710 unsigned long idr_index; 711 u32 start = htid | 0x800; 712 u32 max = htid | 0xFFF; 713 u32 min = htid; 714 715 if (idr_alloc_ext(&ht->handle_idr, NULL, &idr_index, 716 start, max + 1, GFP_KERNEL)) { 717 if (idr_alloc_ext(&ht->handle_idr, NULL, &idr_index, 718 min + 1, max + 1, GFP_KERNEL)) 719 return max; 720 } 721 722 return (u32)idr_index; 723 } 724 725 static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = { 726 [TCA_U32_CLASSID] = { .type = NLA_U32 }, 727 [TCA_U32_HASH] = { .type = NLA_U32 }, 728 [TCA_U32_LINK] = { .type = NLA_U32 }, 729 [TCA_U32_DIVISOR] = { .type = NLA_U32 }, 730 [TCA_U32_SEL] = { .len = sizeof(struct tc_u32_sel) }, 731 [TCA_U32_INDEV] = { .type = NLA_STRING, .len = IFNAMSIZ }, 732 [TCA_U32_MARK] = { .len = sizeof(struct tc_u32_mark) }, 733 [TCA_U32_FLAGS] = { .type = NLA_U32 }, 734 }; 735 736 static int u32_set_parms(struct net *net, struct tcf_proto *tp, 737 unsigned long base, struct tc_u_hnode *ht, 738 struct tc_u_knode *n, struct nlattr **tb, 739 struct nlattr *est, bool ovr) 740 { 741 int err; 742 743 err = tcf_exts_validate(net, tp, tb, est, &n->exts, ovr); 744 if (err < 0) 745 return err; 746 747 if (tb[TCA_U32_LINK]) { 748 u32 handle = nla_get_u32(tb[TCA_U32_LINK]); 749 struct tc_u_hnode *ht_down = NULL, *ht_old; 750 751 if (TC_U32_KEY(handle)) 752 return -EINVAL; 753 754 if (handle) { 755 ht_down = u32_lookup_ht(ht->tp_c, handle); 756 757 if (ht_down == NULL) 758 return -EINVAL; 759 ht_down->refcnt++; 760 } 761 762 ht_old = rtnl_dereference(n->ht_down); 763 rcu_assign_pointer(n->ht_down, ht_down); 764 765 if (ht_old) 766 ht_old->refcnt--; 767 } 768 if (tb[TCA_U32_CLASSID]) { 769 n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]); 770 tcf_bind_filter(tp, &n->res, base); 771 } 772 773 #ifdef CONFIG_NET_CLS_IND 774 if (tb[TCA_U32_INDEV]) { 775 int ret; 776 ret = tcf_change_indev(net, tb[TCA_U32_INDEV]); 777 if (ret < 0) 778 return -EINVAL; 779 n->ifindex = ret; 780 } 781 #endif 782 return 0; 783 } 784 785 static void u32_replace_knode(struct tcf_proto *tp, struct tc_u_common *tp_c, 786 struct tc_u_knode *n) 787 { 788 struct tc_u_knode __rcu **ins; 789 struct tc_u_knode *pins; 790 struct tc_u_hnode *ht; 791 792 if (TC_U32_HTID(n->handle) == TC_U32_ROOT) 793 ht = rtnl_dereference(tp->root); 794 else 795 ht = u32_lookup_ht(tp_c, TC_U32_HTID(n->handle)); 796 797 ins = &ht->ht[TC_U32_HASH(n->handle)]; 798 799 /* The node must always exist for it to be replaced if this is not the 800 * case then something went very wrong elsewhere. 801 */ 802 for (pins = rtnl_dereference(*ins); ; 803 ins = &pins->next, pins = rtnl_dereference(*ins)) 804 if (pins->handle == n->handle) 805 break; 806 807 idr_replace_ext(&ht->handle_idr, n, n->handle); 808 RCU_INIT_POINTER(n->next, pins->next); 809 rcu_assign_pointer(*ins, n); 810 } 811 812 static struct tc_u_knode *u32_init_knode(struct tcf_proto *tp, 813 struct tc_u_knode *n) 814 { 815 struct tc_u_knode *new; 816 struct tc_u32_sel *s = &n->sel; 817 818 new = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), 819 GFP_KERNEL); 820 821 if (!new) 822 return NULL; 823 824 RCU_INIT_POINTER(new->next, n->next); 825 new->handle = n->handle; 826 RCU_INIT_POINTER(new->ht_up, n->ht_up); 827 828 #ifdef CONFIG_NET_CLS_IND 829 new->ifindex = n->ifindex; 830 #endif 831 new->fshift = n->fshift; 832 new->res = n->res; 833 new->flags = n->flags; 834 RCU_INIT_POINTER(new->ht_down, n->ht_down); 835 836 /* bump reference count as long as we hold pointer to structure */ 837 if (new->ht_down) 838 new->ht_down->refcnt++; 839 840 #ifdef CONFIG_CLS_U32_PERF 841 /* Statistics may be incremented by readers during update 842 * so we must keep them in tact. When the node is later destroyed 843 * a special destroy call must be made to not free the pf memory. 844 */ 845 new->pf = n->pf; 846 #endif 847 848 #ifdef CONFIG_CLS_U32_MARK 849 new->val = n->val; 850 new->mask = n->mask; 851 /* Similarly success statistics must be moved as pointers */ 852 new->pcpu_success = n->pcpu_success; 853 #endif 854 new->tp = tp; 855 memcpy(&new->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key)); 856 857 if (tcf_exts_init(&new->exts, TCA_U32_ACT, TCA_U32_POLICE)) { 858 kfree(new); 859 return NULL; 860 } 861 862 return new; 863 } 864 865 static int u32_change(struct net *net, struct sk_buff *in_skb, 866 struct tcf_proto *tp, unsigned long base, u32 handle, 867 struct nlattr **tca, void **arg, bool ovr) 868 { 869 struct tc_u_common *tp_c = tp->data; 870 struct tc_u_hnode *ht; 871 struct tc_u_knode *n; 872 struct tc_u32_sel *s; 873 struct nlattr *opt = tca[TCA_OPTIONS]; 874 struct nlattr *tb[TCA_U32_MAX + 1]; 875 u32 htid, flags = 0; 876 int err; 877 #ifdef CONFIG_CLS_U32_PERF 878 size_t size; 879 #endif 880 881 if (opt == NULL) 882 return handle ? -EINVAL : 0; 883 884 err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy, NULL); 885 if (err < 0) 886 return err; 887 888 if (tb[TCA_U32_FLAGS]) { 889 flags = nla_get_u32(tb[TCA_U32_FLAGS]); 890 if (!tc_flags_valid(flags)) 891 return -EINVAL; 892 } 893 894 n = *arg; 895 if (n) { 896 struct tc_u_knode *new; 897 898 if (TC_U32_KEY(n->handle) == 0) 899 return -EINVAL; 900 901 if (n->flags != flags) 902 return -EINVAL; 903 904 new = u32_init_knode(tp, n); 905 if (!new) 906 return -ENOMEM; 907 908 err = u32_set_parms(net, tp, base, 909 rtnl_dereference(n->ht_up), new, tb, 910 tca[TCA_RATE], ovr); 911 912 if (err) { 913 u32_destroy_key(tp, new, false); 914 return err; 915 } 916 917 err = u32_replace_hw_knode(tp, new, flags); 918 if (err) { 919 u32_destroy_key(tp, new, false); 920 return err; 921 } 922 923 if (!tc_in_hw(new->flags)) 924 new->flags |= TCA_CLS_FLAGS_NOT_IN_HW; 925 926 u32_replace_knode(tp, tp_c, new); 927 tcf_unbind_filter(tp, &n->res); 928 call_rcu(&n->rcu, u32_delete_key_rcu); 929 return 0; 930 } 931 932 if (tb[TCA_U32_DIVISOR]) { 933 unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]); 934 935 if (--divisor > 0x100) 936 return -EINVAL; 937 if (TC_U32_KEY(handle)) 938 return -EINVAL; 939 ht = kzalloc(sizeof(*ht) + divisor*sizeof(void *), GFP_KERNEL); 940 if (ht == NULL) 941 return -ENOBUFS; 942 if (handle == 0) { 943 handle = gen_new_htid(tp->data, ht); 944 if (handle == 0) { 945 kfree(ht); 946 return -ENOMEM; 947 } 948 } else { 949 err = idr_alloc_ext(&tp_c->handle_idr, ht, NULL, 950 handle, handle + 1, GFP_KERNEL); 951 if (err) { 952 kfree(ht); 953 return err; 954 } 955 } 956 ht->tp_c = tp_c; 957 ht->refcnt = 1; 958 ht->divisor = divisor; 959 ht->handle = handle; 960 ht->prio = tp->prio; 961 idr_init(&ht->handle_idr); 962 963 err = u32_replace_hw_hnode(tp, ht, flags); 964 if (err) { 965 idr_remove_ext(&tp_c->handle_idr, handle); 966 kfree(ht); 967 return err; 968 } 969 970 RCU_INIT_POINTER(ht->next, tp_c->hlist); 971 rcu_assign_pointer(tp_c->hlist, ht); 972 *arg = ht; 973 974 return 0; 975 } 976 977 if (tb[TCA_U32_HASH]) { 978 htid = nla_get_u32(tb[TCA_U32_HASH]); 979 if (TC_U32_HTID(htid) == TC_U32_ROOT) { 980 ht = rtnl_dereference(tp->root); 981 htid = ht->handle; 982 } else { 983 ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid)); 984 if (ht == NULL) 985 return -EINVAL; 986 } 987 } else { 988 ht = rtnl_dereference(tp->root); 989 htid = ht->handle; 990 } 991 992 if (ht->divisor < TC_U32_HASH(htid)) 993 return -EINVAL; 994 995 if (handle) { 996 if (TC_U32_HTID(handle) && TC_U32_HTID(handle^htid)) 997 return -EINVAL; 998 handle = htid | TC_U32_NODE(handle); 999 err = idr_alloc_ext(&ht->handle_idr, NULL, NULL, 1000 handle, handle + 1, 1001 GFP_KERNEL); 1002 if (err) 1003 return err; 1004 } else 1005 handle = gen_new_kid(ht, htid); 1006 1007 if (tb[TCA_U32_SEL] == NULL) { 1008 err = -EINVAL; 1009 goto erridr; 1010 } 1011 1012 s = nla_data(tb[TCA_U32_SEL]); 1013 1014 n = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL); 1015 if (n == NULL) { 1016 err = -ENOBUFS; 1017 goto erridr; 1018 } 1019 1020 #ifdef CONFIG_CLS_U32_PERF 1021 size = sizeof(struct tc_u32_pcnt) + s->nkeys * sizeof(u64); 1022 n->pf = __alloc_percpu(size, __alignof__(struct tc_u32_pcnt)); 1023 if (!n->pf) { 1024 err = -ENOBUFS; 1025 goto errfree; 1026 } 1027 #endif 1028 1029 memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key)); 1030 RCU_INIT_POINTER(n->ht_up, ht); 1031 n->handle = handle; 1032 n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0; 1033 n->flags = flags; 1034 n->tp = tp; 1035 1036 err = tcf_exts_init(&n->exts, TCA_U32_ACT, TCA_U32_POLICE); 1037 if (err < 0) 1038 goto errout; 1039 1040 #ifdef CONFIG_CLS_U32_MARK 1041 n->pcpu_success = alloc_percpu(u32); 1042 if (!n->pcpu_success) { 1043 err = -ENOMEM; 1044 goto errout; 1045 } 1046 1047 if (tb[TCA_U32_MARK]) { 1048 struct tc_u32_mark *mark; 1049 1050 mark = nla_data(tb[TCA_U32_MARK]); 1051 n->val = mark->val; 1052 n->mask = mark->mask; 1053 } 1054 #endif 1055 1056 err = u32_set_parms(net, tp, base, ht, n, tb, tca[TCA_RATE], ovr); 1057 if (err == 0) { 1058 struct tc_u_knode __rcu **ins; 1059 struct tc_u_knode *pins; 1060 1061 err = u32_replace_hw_knode(tp, n, flags); 1062 if (err) 1063 goto errhw; 1064 1065 if (!tc_in_hw(n->flags)) 1066 n->flags |= TCA_CLS_FLAGS_NOT_IN_HW; 1067 1068 ins = &ht->ht[TC_U32_HASH(handle)]; 1069 for (pins = rtnl_dereference(*ins); pins; 1070 ins = &pins->next, pins = rtnl_dereference(*ins)) 1071 if (TC_U32_NODE(handle) < TC_U32_NODE(pins->handle)) 1072 break; 1073 1074 RCU_INIT_POINTER(n->next, pins); 1075 rcu_assign_pointer(*ins, n); 1076 *arg = n; 1077 return 0; 1078 } 1079 1080 errhw: 1081 #ifdef CONFIG_CLS_U32_MARK 1082 free_percpu(n->pcpu_success); 1083 #endif 1084 1085 errout: 1086 tcf_exts_destroy(&n->exts); 1087 #ifdef CONFIG_CLS_U32_PERF 1088 errfree: 1089 free_percpu(n->pf); 1090 #endif 1091 kfree(n); 1092 erridr: 1093 idr_remove_ext(&ht->handle_idr, handle); 1094 return err; 1095 } 1096 1097 static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg) 1098 { 1099 struct tc_u_common *tp_c = tp->data; 1100 struct tc_u_hnode *ht; 1101 struct tc_u_knode *n; 1102 unsigned int h; 1103 1104 if (arg->stop) 1105 return; 1106 1107 for (ht = rtnl_dereference(tp_c->hlist); 1108 ht; 1109 ht = rtnl_dereference(ht->next)) { 1110 if (ht->prio != tp->prio) 1111 continue; 1112 if (arg->count >= arg->skip) { 1113 if (arg->fn(tp, ht, arg) < 0) { 1114 arg->stop = 1; 1115 return; 1116 } 1117 } 1118 arg->count++; 1119 for (h = 0; h <= ht->divisor; h++) { 1120 for (n = rtnl_dereference(ht->ht[h]); 1121 n; 1122 n = rtnl_dereference(n->next)) { 1123 if (arg->count < arg->skip) { 1124 arg->count++; 1125 continue; 1126 } 1127 if (arg->fn(tp, n, arg) < 0) { 1128 arg->stop = 1; 1129 return; 1130 } 1131 arg->count++; 1132 } 1133 } 1134 } 1135 } 1136 1137 static void u32_bind_class(void *fh, u32 classid, unsigned long cl) 1138 { 1139 struct tc_u_knode *n = fh; 1140 1141 if (n && n->res.classid == classid) 1142 n->res.class = cl; 1143 } 1144 1145 static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh, 1146 struct sk_buff *skb, struct tcmsg *t) 1147 { 1148 struct tc_u_knode *n = fh; 1149 struct tc_u_hnode *ht_up, *ht_down; 1150 struct nlattr *nest; 1151 1152 if (n == NULL) 1153 return skb->len; 1154 1155 t->tcm_handle = n->handle; 1156 1157 nest = nla_nest_start(skb, TCA_OPTIONS); 1158 if (nest == NULL) 1159 goto nla_put_failure; 1160 1161 if (TC_U32_KEY(n->handle) == 0) { 1162 struct tc_u_hnode *ht = fh; 1163 u32 divisor = ht->divisor + 1; 1164 1165 if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor)) 1166 goto nla_put_failure; 1167 } else { 1168 #ifdef CONFIG_CLS_U32_PERF 1169 struct tc_u32_pcnt *gpf; 1170 int cpu; 1171 #endif 1172 1173 if (nla_put(skb, TCA_U32_SEL, 1174 sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key), 1175 &n->sel)) 1176 goto nla_put_failure; 1177 1178 ht_up = rtnl_dereference(n->ht_up); 1179 if (ht_up) { 1180 u32 htid = n->handle & 0xFFFFF000; 1181 if (nla_put_u32(skb, TCA_U32_HASH, htid)) 1182 goto nla_put_failure; 1183 } 1184 if (n->res.classid && 1185 nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid)) 1186 goto nla_put_failure; 1187 1188 ht_down = rtnl_dereference(n->ht_down); 1189 if (ht_down && 1190 nla_put_u32(skb, TCA_U32_LINK, ht_down->handle)) 1191 goto nla_put_failure; 1192 1193 if (n->flags && nla_put_u32(skb, TCA_U32_FLAGS, n->flags)) 1194 goto nla_put_failure; 1195 1196 #ifdef CONFIG_CLS_U32_MARK 1197 if ((n->val || n->mask)) { 1198 struct tc_u32_mark mark = {.val = n->val, 1199 .mask = n->mask, 1200 .success = 0}; 1201 int cpum; 1202 1203 for_each_possible_cpu(cpum) { 1204 __u32 cnt = *per_cpu_ptr(n->pcpu_success, cpum); 1205 1206 mark.success += cnt; 1207 } 1208 1209 if (nla_put(skb, TCA_U32_MARK, sizeof(mark), &mark)) 1210 goto nla_put_failure; 1211 } 1212 #endif 1213 1214 if (tcf_exts_dump(skb, &n->exts) < 0) 1215 goto nla_put_failure; 1216 1217 #ifdef CONFIG_NET_CLS_IND 1218 if (n->ifindex) { 1219 struct net_device *dev; 1220 dev = __dev_get_by_index(net, n->ifindex); 1221 if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name)) 1222 goto nla_put_failure; 1223 } 1224 #endif 1225 #ifdef CONFIG_CLS_U32_PERF 1226 gpf = kzalloc(sizeof(struct tc_u32_pcnt) + 1227 n->sel.nkeys * sizeof(u64), 1228 GFP_KERNEL); 1229 if (!gpf) 1230 goto nla_put_failure; 1231 1232 for_each_possible_cpu(cpu) { 1233 int i; 1234 struct tc_u32_pcnt *pf = per_cpu_ptr(n->pf, cpu); 1235 1236 gpf->rcnt += pf->rcnt; 1237 gpf->rhit += pf->rhit; 1238 for (i = 0; i < n->sel.nkeys; i++) 1239 gpf->kcnts[i] += pf->kcnts[i]; 1240 } 1241 1242 if (nla_put_64bit(skb, TCA_U32_PCNT, 1243 sizeof(struct tc_u32_pcnt) + 1244 n->sel.nkeys * sizeof(u64), 1245 gpf, TCA_U32_PAD)) { 1246 kfree(gpf); 1247 goto nla_put_failure; 1248 } 1249 kfree(gpf); 1250 #endif 1251 } 1252 1253 nla_nest_end(skb, nest); 1254 1255 if (TC_U32_KEY(n->handle)) 1256 if (tcf_exts_dump_stats(skb, &n->exts) < 0) 1257 goto nla_put_failure; 1258 return skb->len; 1259 1260 nla_put_failure: 1261 nla_nest_cancel(skb, nest); 1262 return -1; 1263 } 1264 1265 static struct tcf_proto_ops cls_u32_ops __read_mostly = { 1266 .kind = "u32", 1267 .classify = u32_classify, 1268 .init = u32_init, 1269 .destroy = u32_destroy, 1270 .get = u32_get, 1271 .change = u32_change, 1272 .delete = u32_delete, 1273 .walk = u32_walk, 1274 .dump = u32_dump, 1275 .bind_class = u32_bind_class, 1276 .owner = THIS_MODULE, 1277 }; 1278 1279 static int __init init_u32(void) 1280 { 1281 int i, ret; 1282 1283 pr_info("u32 classifier\n"); 1284 #ifdef CONFIG_CLS_U32_PERF 1285 pr_info(" Performance counters on\n"); 1286 #endif 1287 #ifdef CONFIG_NET_CLS_IND 1288 pr_info(" input device check on\n"); 1289 #endif 1290 #ifdef CONFIG_NET_CLS_ACT 1291 pr_info(" Actions configured\n"); 1292 #endif 1293 tc_u_common_hash = kvmalloc_array(U32_HASH_SIZE, 1294 sizeof(struct hlist_head), 1295 GFP_KERNEL); 1296 if (!tc_u_common_hash) 1297 return -ENOMEM; 1298 1299 for (i = 0; i < U32_HASH_SIZE; i++) 1300 INIT_HLIST_HEAD(&tc_u_common_hash[i]); 1301 1302 ret = register_tcf_proto_ops(&cls_u32_ops); 1303 if (ret) 1304 kvfree(tc_u_common_hash); 1305 return ret; 1306 } 1307 1308 static void __exit exit_u32(void) 1309 { 1310 unregister_tcf_proto_ops(&cls_u32_ops); 1311 kvfree(tc_u_common_hash); 1312 } 1313 1314 module_init(init_u32) 1315 module_exit(exit_u32) 1316 MODULE_LICENSE("GPL"); 1317