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