1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * net/sched/sch_api.c Packet scheduler API. 4 * 5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 6 * 7 * Fixes: 8 * 9 * Rani Assaf <rani@magic.metawire.com> :980802: JIFFIES and CPU clock sources are repaired. 10 * Eduardo J. Blanco <ejbs@netlabs.com.uy> :990222: kmod support 11 * Jamal Hadi Salim <hadi@nortelnetworks.com>: 990601: ingress support 12 */ 13 14 #include <linux/module.h> 15 #include <linux/types.h> 16 #include <linux/kernel.h> 17 #include <linux/string.h> 18 #include <linux/errno.h> 19 #include <linux/skbuff.h> 20 #include <linux/init.h> 21 #include <linux/proc_fs.h> 22 #include <linux/seq_file.h> 23 #include <linux/kmod.h> 24 #include <linux/list.h> 25 #include <linux/hrtimer.h> 26 #include <linux/slab.h> 27 #include <linux/hashtable.h> 28 #include <linux/bpf.h> 29 30 #include <net/netdev_lock.h> 31 #include <net/net_namespace.h> 32 #include <net/sock.h> 33 #include <net/netlink.h> 34 #include <net/pkt_sched.h> 35 #include <net/pkt_cls.h> 36 #include <net/tc_wrapper.h> 37 38 #include <trace/events/qdisc.h> 39 40 /* 41 42 Short review. 43 ------------- 44 45 This file consists of two interrelated parts: 46 47 1. queueing disciplines manager frontend. 48 2. traffic classes manager frontend. 49 50 Generally, queueing discipline ("qdisc") is a black box, 51 which is able to enqueue packets and to dequeue them (when 52 device is ready to send something) in order and at times 53 determined by algorithm hidden in it. 54 55 qdisc's are divided to two categories: 56 - "queues", which have no internal structure visible from outside. 57 - "schedulers", which split all the packets to "traffic classes", 58 using "packet classifiers" (look at cls_api.c) 59 60 In turn, classes may have child qdiscs (as rule, queues) 61 attached to them etc. etc. etc. 62 63 The goal of the routines in this file is to translate 64 information supplied by user in the form of handles 65 to more intelligible for kernel form, to make some sanity 66 checks and part of work, which is common to all qdiscs 67 and to provide rtnetlink notifications. 68 69 All real intelligent work is done inside qdisc modules. 70 71 72 73 Every discipline has two major routines: enqueue and dequeue. 74 75 ---dequeue 76 77 dequeue usually returns a skb to send. It is allowed to return NULL, 78 but it does not mean that queue is empty, it just means that 79 discipline does not want to send anything this time. 80 Queue is really empty if q->q.qlen == 0. 81 For complicated disciplines with multiple queues q->q is not 82 real packet queue, but however q->q.qlen must be valid. 83 84 ---enqueue 85 86 enqueue returns 0, if packet was enqueued successfully. 87 If packet (this one or another one) was dropped, it returns 88 not zero error code. 89 NET_XMIT_DROP - this packet dropped 90 Expected action: do not backoff, but wait until queue will clear. 91 NET_XMIT_CN - probably this packet enqueued, but another one dropped. 92 Expected action: backoff or ignore 93 94 Auxiliary routines: 95 96 ---peek 97 98 like dequeue but without removing a packet from the queue 99 100 ---reset 101 102 returns qdisc to initial state: purge all buffers, clear all 103 timers, counters (except for statistics) etc. 104 105 ---init 106 107 initializes newly created qdisc. 108 109 ---destroy 110 111 destroys resources allocated by init and during lifetime of qdisc. 112 113 ---change 114 115 changes qdisc parameters. 116 */ 117 118 /* Protects list of registered TC modules. It is pure SMP lock. */ 119 static DEFINE_RWLOCK(qdisc_mod_lock); 120 121 122 /************************************************ 123 * Queueing disciplines manipulation. * 124 ************************************************/ 125 126 127 /* The list of all installed queueing disciplines. */ 128 129 static struct Qdisc_ops *qdisc_base; 130 131 /* Register/unregister queueing discipline */ 132 133 int register_qdisc(struct Qdisc_ops *qops) 134 { 135 struct Qdisc_ops *q, **qp; 136 int rc = -EEXIST; 137 138 write_lock(&qdisc_mod_lock); 139 for (qp = &qdisc_base; (q = *qp) != NULL; qp = &q->next) 140 if (!strcmp(qops->id, q->id)) 141 goto out; 142 143 if (qops->enqueue == NULL) 144 qops->enqueue = noop_qdisc_ops.enqueue; 145 if (qops->peek == NULL) { 146 if (qops->dequeue == NULL) 147 qops->peek = noop_qdisc_ops.peek; 148 else 149 goto out_einval; 150 } 151 if (qops->dequeue == NULL) 152 qops->dequeue = noop_qdisc_ops.dequeue; 153 154 if (qops->cl_ops) { 155 const struct Qdisc_class_ops *cops = qops->cl_ops; 156 157 if (!(cops->find && cops->walk && cops->leaf)) 158 goto out_einval; 159 160 if (cops->tcf_block && !(cops->bind_tcf && cops->unbind_tcf)) 161 goto out_einval; 162 } 163 164 qops->next = NULL; 165 *qp = qops; 166 rc = 0; 167 out: 168 write_unlock(&qdisc_mod_lock); 169 return rc; 170 171 out_einval: 172 rc = -EINVAL; 173 goto out; 174 } 175 EXPORT_SYMBOL(register_qdisc); 176 177 void unregister_qdisc(struct Qdisc_ops *qops) 178 { 179 struct Qdisc_ops *q, **qp; 180 int err = -ENOENT; 181 182 write_lock(&qdisc_mod_lock); 183 for (qp = &qdisc_base; (q = *qp) != NULL; qp = &q->next) 184 if (q == qops) 185 break; 186 if (q) { 187 *qp = q->next; 188 q->next = NULL; 189 err = 0; 190 } 191 write_unlock(&qdisc_mod_lock); 192 193 WARN(err, "unregister qdisc(%s) failed\n", qops->id); 194 } 195 EXPORT_SYMBOL(unregister_qdisc); 196 197 /* Get default qdisc if not otherwise specified */ 198 void qdisc_get_default(char *name, size_t len) 199 { 200 read_lock(&qdisc_mod_lock); 201 strscpy(name, default_qdisc_ops->id, len); 202 read_unlock(&qdisc_mod_lock); 203 } 204 205 static struct Qdisc_ops *qdisc_lookup_default(const char *name) 206 { 207 struct Qdisc_ops *q = NULL; 208 209 for (q = qdisc_base; q; q = q->next) { 210 if (!strcmp(name, q->id)) { 211 if (!bpf_try_module_get(q, q->owner)) 212 q = NULL; 213 break; 214 } 215 } 216 217 return q; 218 } 219 220 /* Set new default qdisc to use */ 221 int qdisc_set_default(const char *name) 222 { 223 const struct Qdisc_ops *ops; 224 225 if (!capable(CAP_NET_ADMIN)) 226 return -EPERM; 227 228 write_lock(&qdisc_mod_lock); 229 ops = qdisc_lookup_default(name); 230 if (!ops) { 231 /* Not found, drop lock and try to load module */ 232 write_unlock(&qdisc_mod_lock); 233 request_module(NET_SCH_ALIAS_PREFIX "%s", name); 234 write_lock(&qdisc_mod_lock); 235 236 ops = qdisc_lookup_default(name); 237 } 238 239 if (ops) { 240 /* Set new default */ 241 bpf_module_put(default_qdisc_ops, default_qdisc_ops->owner); 242 default_qdisc_ops = ops; 243 } 244 write_unlock(&qdisc_mod_lock); 245 246 return ops ? 0 : -ENOENT; 247 } 248 249 #ifdef CONFIG_NET_SCH_DEFAULT 250 /* Set default value from kernel config */ 251 static int __init sch_default_qdisc(void) 252 { 253 return qdisc_set_default(CONFIG_DEFAULT_NET_SCH); 254 } 255 late_initcall(sch_default_qdisc); 256 #endif 257 258 /* We know handle. Find qdisc among all qdisc's attached to device 259 * (root qdisc, all its children, children of children etc.) 260 * Note: caller either uses rtnl or rcu_read_lock() 261 */ 262 263 static struct Qdisc *qdisc_match_from_root(struct Qdisc *root, u32 handle) 264 { 265 struct Qdisc *q; 266 267 if (!qdisc_dev(root)) 268 return (root->handle == handle ? root : NULL); 269 270 if (!(root->flags & TCQ_F_BUILTIN) && 271 root->handle == handle) 272 return root; 273 274 hash_for_each_possible_rcu(qdisc_dev(root)->qdisc_hash, q, hash, handle, 275 lockdep_rtnl_is_held()) { 276 if (q->handle == handle) 277 return q; 278 } 279 return NULL; 280 } 281 282 void qdisc_hash_add(struct Qdisc *q, bool invisible) 283 { 284 if ((q->parent != TC_H_ROOT) && !(q->flags & TCQ_F_INGRESS)) { 285 ASSERT_RTNL(); 286 hash_add_rcu(qdisc_dev(q)->qdisc_hash, &q->hash, q->handle); 287 if (invisible) 288 q->flags |= TCQ_F_INVISIBLE; 289 } 290 } 291 EXPORT_SYMBOL(qdisc_hash_add); 292 293 void qdisc_hash_del(struct Qdisc *q) 294 { 295 if ((q->parent != TC_H_ROOT) && !(q->flags & TCQ_F_INGRESS)) { 296 ASSERT_RTNL(); 297 hash_del_rcu(&q->hash); 298 } 299 } 300 EXPORT_SYMBOL(qdisc_hash_del); 301 302 struct Qdisc *qdisc_lookup(struct net_device *dev, u32 handle) 303 { 304 struct Qdisc *q; 305 306 if (!handle) 307 return NULL; 308 q = qdisc_match_from_root(rtnl_dereference(dev->qdisc), handle); 309 if (q) 310 goto out; 311 312 if (dev_ingress_queue(dev)) 313 q = qdisc_match_from_root( 314 rtnl_dereference(dev_ingress_queue(dev)->qdisc_sleeping), 315 handle); 316 out: 317 return q; 318 } 319 320 struct Qdisc *qdisc_lookup_rcu(struct net_device *dev, u32 handle) 321 { 322 struct netdev_queue *nq; 323 struct Qdisc *q; 324 325 if (!handle) 326 return NULL; 327 q = qdisc_match_from_root(rcu_dereference(dev->qdisc), handle); 328 if (q) 329 goto out; 330 331 nq = dev_ingress_queue_rcu(dev); 332 if (nq) 333 q = qdisc_match_from_root(rcu_dereference(nq->qdisc_sleeping), 334 handle); 335 out: 336 return q; 337 } 338 339 static struct Qdisc *qdisc_leaf(struct Qdisc *p, u32 classid, 340 struct netlink_ext_ack *extack) 341 { 342 unsigned long cl; 343 const struct Qdisc_class_ops *cops = p->ops->cl_ops; 344 345 if (cops == NULL) { 346 NL_SET_ERR_MSG(extack, "Parent qdisc is not classful"); 347 return ERR_PTR(-EOPNOTSUPP); 348 } 349 cl = cops->find(p, classid); 350 351 if (cl == 0) { 352 NL_SET_ERR_MSG(extack, "Specified class not found"); 353 return ERR_PTR(-ENOENT); 354 } 355 return cops->leaf(p, cl); 356 } 357 358 /* Find queueing discipline by name */ 359 360 static struct Qdisc_ops *qdisc_lookup_ops(struct nlattr *kind) 361 { 362 struct Qdisc_ops *q = NULL; 363 364 if (kind) { 365 read_lock(&qdisc_mod_lock); 366 for (q = qdisc_base; q; q = q->next) { 367 if (nla_strcmp(kind, q->id) == 0) { 368 if (!bpf_try_module_get(q, q->owner)) 369 q = NULL; 370 break; 371 } 372 } 373 read_unlock(&qdisc_mod_lock); 374 } 375 return q; 376 } 377 378 /* The linklayer setting were not transferred from iproute2, in older 379 * versions, and the rate tables lookup systems have been dropped in 380 * the kernel. To keep backward compatible with older iproute2 tc 381 * utils, we detect the linklayer setting by detecting if the rate 382 * table were modified. 383 * 384 * For linklayer ATM table entries, the rate table will be aligned to 385 * 48 bytes, thus some table entries will contain the same value. The 386 * mpu (min packet unit) is also encoded into the old rate table, thus 387 * starting from the mpu, we find low and high table entries for 388 * mapping this cell. If these entries contain the same value, when 389 * the rate tables have been modified for linklayer ATM. 390 * 391 * This is done by rounding mpu to the nearest 48 bytes cell/entry, 392 * and then roundup to the next cell, calc the table entry one below, 393 * and compare. 394 */ 395 static __u8 __detect_linklayer(struct tc_ratespec *r, __u32 *rtab) 396 { 397 int low = roundup(r->mpu, 48); 398 int high = roundup(low+1, 48); 399 int cell_low = low >> r->cell_log; 400 int cell_high = (high >> r->cell_log) - 1; 401 402 /* rtab is too inaccurate at rates > 100Mbit/s */ 403 if ((r->rate > (100000000/8)) || (rtab[0] == 0)) { 404 pr_debug("TC linklayer: Giving up ATM detection\n"); 405 return TC_LINKLAYER_ETHERNET; 406 } 407 408 if ((cell_high > cell_low) && (cell_high < 256) 409 && (rtab[cell_low] == rtab[cell_high])) { 410 pr_debug("TC linklayer: Detected ATM, low(%d)=high(%d)=%u\n", 411 cell_low, cell_high, rtab[cell_high]); 412 return TC_LINKLAYER_ATM; 413 } 414 return TC_LINKLAYER_ETHERNET; 415 } 416 417 static struct qdisc_rate_table *qdisc_rtab_list; 418 static DEFINE_SPINLOCK(qdisc_rtab_lock); 419 420 struct qdisc_rate_table *qdisc_get_rtab(struct tc_ratespec *r, 421 struct nlattr *tab, 422 struct netlink_ext_ack *extack) 423 { 424 struct qdisc_rate_table *rtab, *new_rtab; 425 426 if (tab == NULL || r->rate == 0 || 427 r->cell_log == 0 || r->cell_log >= 32 || 428 nla_len(tab) != TC_RTAB_SIZE) { 429 NL_SET_ERR_MSG(extack, "Invalid rate table parameters for searching"); 430 return NULL; 431 } 432 433 new_rtab = kmalloc_obj(*new_rtab); 434 435 spin_lock(&qdisc_rtab_lock); 436 for (rtab = qdisc_rtab_list; rtab; rtab = rtab->next) { 437 if (!memcmp(&rtab->rate, r, sizeof(struct tc_ratespec)) && 438 !memcmp(&rtab->data, nla_data(tab), TC_RTAB_SIZE)) { 439 rtab->refcnt++; 440 spin_unlock(&qdisc_rtab_lock); 441 kfree(new_rtab); 442 return rtab; 443 } 444 } 445 446 rtab = new_rtab; 447 if (rtab) { 448 rtab->rate = *r; 449 rtab->refcnt = 1; 450 memcpy(rtab->data, nla_data(tab), TC_RTAB_SIZE); 451 if (r->linklayer == TC_LINKLAYER_UNAWARE) 452 r->linklayer = __detect_linklayer(r, rtab->data); 453 rtab->next = qdisc_rtab_list; 454 qdisc_rtab_list = rtab; 455 } else { 456 NL_SET_ERR_MSG(extack, "Failed to allocate new qdisc rate table"); 457 } 458 spin_unlock(&qdisc_rtab_lock); 459 return rtab; 460 } 461 EXPORT_SYMBOL(qdisc_get_rtab); 462 463 void qdisc_put_rtab(struct qdisc_rate_table *tab) 464 { 465 struct qdisc_rate_table *rtab, **rtabp; 466 467 if (!tab) 468 return; 469 470 spin_lock(&qdisc_rtab_lock); 471 if (--tab->refcnt) { 472 spin_unlock(&qdisc_rtab_lock); 473 return; 474 } 475 476 for (rtabp = &qdisc_rtab_list; 477 (rtab = *rtabp) != NULL; 478 rtabp = &rtab->next) { 479 if (rtab == tab) { 480 *rtabp = rtab->next; 481 break; 482 } 483 } 484 spin_unlock(&qdisc_rtab_lock); 485 kfree(tab); 486 } 487 EXPORT_SYMBOL(qdisc_put_rtab); 488 489 static LIST_HEAD(qdisc_stab_list); 490 491 static const struct nla_policy stab_policy[TCA_STAB_MAX + 1] = { 492 [TCA_STAB_BASE] = { .len = sizeof(struct tc_sizespec) }, 493 [TCA_STAB_DATA] = { .type = NLA_BINARY }, 494 }; 495 496 static struct qdisc_size_table *qdisc_get_stab(struct nlattr *opt, 497 struct netlink_ext_ack *extack) 498 { 499 struct nlattr *tb[TCA_STAB_MAX + 1]; 500 struct qdisc_size_table *stab; 501 struct tc_sizespec *s; 502 unsigned int tsize = 0; 503 u16 *tab = NULL; 504 int err; 505 506 err = nla_parse_nested_deprecated(tb, TCA_STAB_MAX, opt, stab_policy, 507 extack); 508 if (err < 0) 509 return ERR_PTR(err); 510 if (!tb[TCA_STAB_BASE]) { 511 NL_SET_ERR_MSG(extack, "Size table base attribute is missing"); 512 return ERR_PTR(-EINVAL); 513 } 514 515 s = nla_data(tb[TCA_STAB_BASE]); 516 517 if (s->tsize > 0) { 518 if (!tb[TCA_STAB_DATA]) { 519 NL_SET_ERR_MSG(extack, "Size table data attribute is missing"); 520 return ERR_PTR(-EINVAL); 521 } 522 tab = nla_data(tb[TCA_STAB_DATA]); 523 tsize = nla_len(tb[TCA_STAB_DATA]) / sizeof(u16); 524 } 525 526 if (tsize != s->tsize || (!tab && tsize > 0)) { 527 NL_SET_ERR_MSG(extack, "Invalid size of size table"); 528 return ERR_PTR(-EINVAL); 529 } 530 531 list_for_each_entry(stab, &qdisc_stab_list, list) { 532 if (memcmp(&stab->szopts, s, sizeof(*s))) 533 continue; 534 if (tsize > 0 && 535 memcmp(stab->data, tab, flex_array_size(stab, data, tsize))) 536 continue; 537 stab->refcnt++; 538 return stab; 539 } 540 541 if (s->size_log > STAB_SIZE_LOG_MAX || 542 s->cell_log > STAB_SIZE_LOG_MAX) { 543 NL_SET_ERR_MSG(extack, "Invalid logarithmic size of size table"); 544 return ERR_PTR(-EINVAL); 545 } 546 547 stab = kmalloc_flex(*stab, data, tsize); 548 if (!stab) 549 return ERR_PTR(-ENOMEM); 550 551 stab->refcnt = 1; 552 stab->szopts = *s; 553 if (tsize > 0) 554 memcpy(stab->data, tab, flex_array_size(stab, data, tsize)); 555 556 list_add_tail(&stab->list, &qdisc_stab_list); 557 558 return stab; 559 } 560 561 void qdisc_put_stab(struct qdisc_size_table *tab) 562 { 563 if (!tab) 564 return; 565 566 if (--tab->refcnt == 0) { 567 list_del(&tab->list); 568 kfree_rcu(tab, rcu); 569 } 570 } 571 EXPORT_SYMBOL(qdisc_put_stab); 572 573 static int qdisc_dump_stab(struct sk_buff *skb, struct qdisc_size_table *stab) 574 { 575 struct nlattr *nest; 576 577 nest = nla_nest_start_noflag(skb, TCA_STAB); 578 if (nest == NULL) 579 goto nla_put_failure; 580 if (nla_put(skb, TCA_STAB_BASE, sizeof(stab->szopts), &stab->szopts)) 581 goto nla_put_failure; 582 nla_nest_end(skb, nest); 583 584 return skb->len; 585 586 nla_put_failure: 587 return -1; 588 } 589 590 void __qdisc_calculate_pkt_len(struct sk_buff *skb, 591 const struct qdisc_size_table *stab) 592 { 593 int pkt_len, slot; 594 595 pkt_len = skb->len + stab->szopts.overhead; 596 if (unlikely(!stab->szopts.tsize)) 597 goto out; 598 599 slot = pkt_len + stab->szopts.cell_align; 600 if (unlikely(slot < 0)) 601 slot = 0; 602 603 slot >>= stab->szopts.cell_log; 604 if (likely(slot < stab->szopts.tsize)) 605 pkt_len = stab->data[slot]; 606 else 607 pkt_len = stab->data[stab->szopts.tsize - 1] * 608 (slot / stab->szopts.tsize) + 609 stab->data[slot % stab->szopts.tsize]; 610 611 pkt_len <<= stab->szopts.size_log; 612 out: 613 /* A size table can inflate qdisc_pkt_len() beyond any real packet 614 * (via overhead, the data table, or size_log); cap it so deficit 615 * schedulers such as DRR/ETS terminate their refill loops. 616 */ 617 pkt_len = clamp_t(int, pkt_len, 1, QDISC_PKT_LEN_MAX); 618 qdisc_skb_cb(skb)->pkt_len = pkt_len; 619 } 620 621 static enum hrtimer_restart qdisc_watchdog(struct hrtimer *timer) 622 { 623 struct qdisc_watchdog *wd = container_of(timer, struct qdisc_watchdog, 624 timer); 625 626 rcu_read_lock(); 627 __netif_schedule(qdisc_root(wd->qdisc)); 628 rcu_read_unlock(); 629 630 return HRTIMER_NORESTART; 631 } 632 633 void qdisc_watchdog_init_clockid(struct qdisc_watchdog *wd, struct Qdisc *qdisc, 634 clockid_t clockid) 635 { 636 hrtimer_setup(&wd->timer, qdisc_watchdog, clockid, HRTIMER_MODE_ABS_PINNED); 637 wd->qdisc = qdisc; 638 } 639 EXPORT_SYMBOL(qdisc_watchdog_init_clockid); 640 641 void qdisc_watchdog_init(struct qdisc_watchdog *wd, struct Qdisc *qdisc) 642 { 643 qdisc_watchdog_init_clockid(wd, qdisc, CLOCK_MONOTONIC); 644 } 645 EXPORT_SYMBOL(qdisc_watchdog_init); 646 647 void qdisc_watchdog_schedule_range_ns(struct qdisc_watchdog *wd, u64 expires, 648 u64 delta_ns) 649 { 650 bool deactivated; 651 652 rcu_read_lock(); 653 deactivated = test_bit(__QDISC_STATE_DEACTIVATED, 654 &qdisc_root_sleeping(wd->qdisc)->state); 655 rcu_read_unlock(); 656 if (deactivated) 657 return; 658 659 if (hrtimer_is_queued(&wd->timer)) { 660 u64 softexpires; 661 662 softexpires = ktime_to_ns(hrtimer_get_softexpires(&wd->timer)); 663 /* If timer is already set in [expires, expires + delta_ns], 664 * do not reprogram it. 665 */ 666 if (softexpires - expires <= delta_ns) 667 return; 668 } 669 670 hrtimer_start_range_ns(&wd->timer, 671 ns_to_ktime(expires), 672 delta_ns, 673 HRTIMER_MODE_ABS_PINNED); 674 } 675 EXPORT_SYMBOL(qdisc_watchdog_schedule_range_ns); 676 677 void qdisc_watchdog_cancel(struct qdisc_watchdog *wd) 678 { 679 hrtimer_cancel(&wd->timer); 680 } 681 EXPORT_SYMBOL(qdisc_watchdog_cancel); 682 683 static struct hlist_head *qdisc_class_hash_alloc(unsigned int n) 684 { 685 struct hlist_head *h; 686 unsigned int i; 687 688 h = kvmalloc_objs(struct hlist_head, n); 689 690 if (h != NULL) { 691 for (i = 0; i < n; i++) 692 INIT_HLIST_HEAD(&h[i]); 693 } 694 return h; 695 } 696 697 void qdisc_class_hash_grow(struct Qdisc *sch, struct Qdisc_class_hash *clhash) 698 { 699 struct Qdisc_class_common *cl; 700 struct hlist_node *next; 701 struct hlist_head *nhash, *ohash; 702 unsigned int nsize, nmask, osize; 703 unsigned int i, h; 704 705 /* Rehash when load factor exceeds 0.75 */ 706 if (clhash->hashelems * 4 <= clhash->hashsize * 3) 707 return; 708 nsize = clhash->hashsize * 2; 709 nmask = nsize - 1; 710 nhash = qdisc_class_hash_alloc(nsize); 711 if (nhash == NULL) 712 return; 713 714 ohash = clhash->hash; 715 osize = clhash->hashsize; 716 717 sch_tree_lock(sch); 718 for (i = 0; i < osize; i++) { 719 hlist_for_each_entry_safe(cl, next, &ohash[i], hnode) { 720 h = qdisc_class_hash(cl->classid, nmask); 721 hlist_add_head(&cl->hnode, &nhash[h]); 722 } 723 } 724 clhash->hash = nhash; 725 clhash->hashsize = nsize; 726 clhash->hashmask = nmask; 727 sch_tree_unlock(sch); 728 729 kvfree(ohash); 730 } 731 EXPORT_SYMBOL(qdisc_class_hash_grow); 732 733 int qdisc_class_hash_init(struct Qdisc_class_hash *clhash) 734 { 735 unsigned int size = 4; 736 737 clhash->hash = qdisc_class_hash_alloc(size); 738 if (!clhash->hash) 739 return -ENOMEM; 740 clhash->hashsize = size; 741 clhash->hashmask = size - 1; 742 clhash->hashelems = 0; 743 return 0; 744 } 745 EXPORT_SYMBOL(qdisc_class_hash_init); 746 747 void qdisc_class_hash_destroy(struct Qdisc_class_hash *clhash) 748 { 749 kvfree(clhash->hash); 750 } 751 EXPORT_SYMBOL(qdisc_class_hash_destroy); 752 753 void qdisc_class_hash_insert(struct Qdisc_class_hash *clhash, 754 struct Qdisc_class_common *cl) 755 { 756 unsigned int h; 757 758 INIT_HLIST_NODE(&cl->hnode); 759 h = qdisc_class_hash(cl->classid, clhash->hashmask); 760 hlist_add_head(&cl->hnode, &clhash->hash[h]); 761 clhash->hashelems++; 762 } 763 EXPORT_SYMBOL(qdisc_class_hash_insert); 764 765 void qdisc_class_hash_remove(struct Qdisc_class_hash *clhash, 766 struct Qdisc_class_common *cl) 767 { 768 hlist_del(&cl->hnode); 769 clhash->hashelems--; 770 } 771 EXPORT_SYMBOL(qdisc_class_hash_remove); 772 773 /* Allocate an unique handle from space managed by kernel 774 * Possible range is [8000-FFFF]:0000 (0x8000 values) 775 */ 776 static u32 qdisc_alloc_handle(struct net_device *dev) 777 { 778 int i = 0x8000; 779 static u32 autohandle = TC_H_MAKE(0x80000000U, 0); 780 781 do { 782 autohandle += TC_H_MAKE(0x10000U, 0); 783 if (autohandle == TC_H_MAKE(TC_H_ROOT, 0)) 784 autohandle = TC_H_MAKE(0x80000000U, 0); 785 if (!qdisc_lookup(dev, autohandle)) 786 return autohandle; 787 cond_resched(); 788 } while (--i > 0); 789 790 return 0; 791 } 792 793 void qdisc_tree_reduce_backlog(struct Qdisc *sch, int n, int len) 794 { 795 const struct Qdisc_class_ops *cops; 796 unsigned long cl; 797 u32 parentid; 798 bool notify; 799 int drops; 800 801 drops = max_t(int, n, 0); 802 rcu_read_lock(); 803 while ((parentid = sch->parent)) { 804 if (parentid == TC_H_ROOT) 805 break; 806 807 if (sch->flags & TCQ_F_NOPARENT) 808 break; 809 /* Notify parent qdisc only if child qdisc becomes empty. */ 810 notify = !sch->q.qlen; 811 /* TODO: perform the search on a per txq basis */ 812 sch = qdisc_lookup_rcu(qdisc_dev(sch), TC_H_MAJ(parentid)); 813 if (sch == NULL) { 814 WARN_ON_ONCE(parentid != TC_H_ROOT); 815 break; 816 } 817 cops = sch->ops->cl_ops; 818 if (notify && cops->qlen_notify) { 819 /* Note that qlen_notify must be idempotent as it may get called 820 * multiple times. 821 */ 822 cl = cops->find(sch, parentid); 823 cops->qlen_notify(sch, cl); 824 } 825 WRITE_ONCE(sch->q.qlen, sch->q.qlen - n); 826 qstats_backlog_sub(sch, len); 827 __qdisc_qstats_drop(sch, drops); 828 } 829 rcu_read_unlock(); 830 } 831 EXPORT_SYMBOL(qdisc_tree_reduce_backlog); 832 833 int qdisc_offload_dump_helper(struct Qdisc *sch, enum tc_setup_type type, 834 void *type_data) 835 { 836 struct net_device *dev = qdisc_dev(sch); 837 int err; 838 839 sch->flags &= ~TCQ_F_OFFLOADED; 840 if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc) 841 return 0; 842 843 err = dev->netdev_ops->ndo_setup_tc(dev, type, type_data); 844 if (err == -EOPNOTSUPP) 845 return 0; 846 847 if (!err) 848 sch->flags |= TCQ_F_OFFLOADED; 849 850 return err; 851 } 852 EXPORT_SYMBOL(qdisc_offload_dump_helper); 853 854 void qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch, 855 struct Qdisc *new, struct Qdisc *old, 856 enum tc_setup_type type, void *type_data, 857 struct netlink_ext_ack *extack) 858 { 859 bool any_qdisc_is_offloaded; 860 int err; 861 862 if (!tc_can_offload(dev) || !dev->netdev_ops->ndo_setup_tc) 863 return; 864 865 err = dev->netdev_ops->ndo_setup_tc(dev, type, type_data); 866 867 /* Don't report error if the graft is part of destroy operation. */ 868 if (!err || !new || new == &noop_qdisc) 869 return; 870 871 /* Don't report error if the parent, the old child and the new 872 * one are not offloaded. 873 */ 874 any_qdisc_is_offloaded = new->flags & TCQ_F_OFFLOADED; 875 any_qdisc_is_offloaded |= sch && sch->flags & TCQ_F_OFFLOADED; 876 any_qdisc_is_offloaded |= old && old->flags & TCQ_F_OFFLOADED; 877 878 if (any_qdisc_is_offloaded) 879 NL_SET_ERR_MSG_WEAK(extack, "Offloading graft operation failed."); 880 } 881 EXPORT_SYMBOL(qdisc_offload_graft_helper); 882 883 void qdisc_offload_query_caps(struct net_device *dev, 884 enum tc_setup_type type, 885 void *caps, size_t caps_len) 886 { 887 const struct net_device_ops *ops = dev->netdev_ops; 888 struct tc_query_caps_base base = { 889 .type = type, 890 .caps = caps, 891 }; 892 893 memset(caps, 0, caps_len); 894 895 if (ops->ndo_setup_tc) 896 ops->ndo_setup_tc(dev, TC_QUERY_CAPS, &base); 897 } 898 EXPORT_SYMBOL(qdisc_offload_query_caps); 899 900 static void qdisc_offload_graft_root(struct net_device *dev, 901 struct Qdisc *new, struct Qdisc *old, 902 struct netlink_ext_ack *extack) 903 { 904 struct tc_root_qopt_offload graft_offload = { 905 .command = TC_ROOT_GRAFT, 906 .handle = new ? new->handle : 0, 907 .ingress = (new && new->flags & TCQ_F_INGRESS) || 908 (old && old->flags & TCQ_F_INGRESS), 909 }; 910 911 qdisc_offload_graft_helper(dev, NULL, new, old, 912 TC_SETUP_ROOT_QDISC, &graft_offload, extack); 913 } 914 915 static int tc_fill_qdisc(struct sk_buff *skb, struct Qdisc *q, u32 clid, 916 u32 portid, u32 seq, u16 flags, int event, 917 struct netlink_ext_ack *extack) 918 { 919 struct gnet_stats_basic_sync __percpu *cpu_bstats = NULL; 920 struct gnet_stats_queue __percpu *cpu_qstats = NULL; 921 struct tcmsg *tcm; 922 struct nlmsghdr *nlh; 923 unsigned char *b = skb_tail_pointer(skb); 924 struct gnet_dump d; 925 struct qdisc_size_table *stab; 926 u32 block_index; 927 __u32 qlen; 928 929 cond_resched(); 930 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); 931 if (!nlh) 932 goto out_nlmsg_trim; 933 tcm = nlmsg_data(nlh); 934 tcm->tcm_family = AF_UNSPEC; 935 tcm->tcm__pad1 = 0; 936 tcm->tcm__pad2 = 0; 937 tcm->tcm_ifindex = qdisc_dev(q)->ifindex; 938 tcm->tcm_parent = clid; 939 tcm->tcm_handle = q->handle; 940 tcm->tcm_info = refcount_read(&q->refcnt); 941 if (nla_put_string(skb, TCA_KIND, q->ops->id)) 942 goto nla_put_failure; 943 if (q->ops->ingress_block_get) { 944 block_index = q->ops->ingress_block_get(q); 945 if (block_index && 946 nla_put_u32(skb, TCA_INGRESS_BLOCK, block_index)) 947 goto nla_put_failure; 948 } 949 if (q->ops->egress_block_get) { 950 block_index = q->ops->egress_block_get(q); 951 if (block_index && 952 nla_put_u32(skb, TCA_EGRESS_BLOCK, block_index)) 953 goto nla_put_failure; 954 } 955 if (q->ops->dump && q->ops->dump(q, skb) < 0) 956 goto nla_put_failure; 957 if (nla_put_u8(skb, TCA_HW_OFFLOAD, !!(q->flags & TCQ_F_OFFLOADED))) 958 goto nla_put_failure; 959 qlen = qdisc_qlen_sum(q); 960 961 stab = rtnl_dereference(q->stab); 962 if (stab && qdisc_dump_stab(skb, stab) < 0) 963 goto nla_put_failure; 964 965 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS, TCA_XSTATS, 966 NULL, &d, TCA_PAD) < 0) 967 goto nla_put_failure; 968 969 if (q->ops->dump_stats && q->ops->dump_stats(q, &d) < 0) 970 goto nla_put_failure; 971 972 if (qdisc_is_percpu_stats(q)) { 973 cpu_bstats = q->cpu_bstats; 974 cpu_qstats = q->cpu_qstats; 975 } 976 977 if (gnet_stats_copy_basic(&d, cpu_bstats, &q->bstats, true) < 0 || 978 gnet_stats_copy_rate_est(&d, &q->rate_est) < 0 || 979 gnet_stats_copy_queue(&d, cpu_qstats, &q->qstats, qlen) < 0) 980 goto nla_put_failure; 981 982 if (gnet_stats_finish_copy(&d) < 0) 983 goto nla_put_failure; 984 985 if (extack && extack->_msg && 986 nla_put_string(skb, TCA_EXT_WARN_MSG, extack->_msg)) 987 goto out_nlmsg_trim; 988 989 nlh->nlmsg_len = skb_tail_pointer(skb) - b; 990 991 return skb->len; 992 993 out_nlmsg_trim: 994 nla_put_failure: 995 nlmsg_trim(skb, b); 996 return -EMSGSIZE; 997 } 998 999 static bool tc_qdisc_dump_ignore(struct Qdisc *q, bool dump_invisible, 1000 const struct tcmsg *tcm) 1001 { 1002 if (q->flags & TCQ_F_BUILTIN) 1003 return true; 1004 if ((q->flags & TCQ_F_INVISIBLE) && !dump_invisible) 1005 return true; 1006 if (tcm) { 1007 if (tcm->tcm_handle && tcm->tcm_handle != q->handle) 1008 return true; 1009 } 1010 return false; 1011 } 1012 1013 static int qdisc_get_notify(struct net *net, struct sk_buff *oskb, 1014 struct nlmsghdr *n, u32 clid, struct Qdisc *q, 1015 struct netlink_ext_ack *extack) 1016 { 1017 struct sk_buff *skb; 1018 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; 1019 1020 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 1021 if (!skb) 1022 return -ENOBUFS; 1023 1024 if (!tc_qdisc_dump_ignore(q, false, NULL)) { 1025 if (tc_fill_qdisc(skb, q, clid, portid, n->nlmsg_seq, 0, 1026 RTM_NEWQDISC, extack) < 0) 1027 goto err_out; 1028 } 1029 1030 if (skb->len) 1031 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, 1032 n->nlmsg_flags & NLM_F_ECHO); 1033 1034 err_out: 1035 kfree_skb(skb); 1036 return -EINVAL; 1037 } 1038 1039 static int qdisc_notify(struct net *net, struct sk_buff *oskb, 1040 struct nlmsghdr *n, u32 clid, 1041 struct Qdisc *old, struct Qdisc *new, 1042 struct netlink_ext_ack *extack) 1043 { 1044 struct sk_buff *skb; 1045 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; 1046 1047 if (!rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC)) 1048 return 0; 1049 1050 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 1051 if (!skb) 1052 return -ENOBUFS; 1053 1054 if (old && !tc_qdisc_dump_ignore(old, false, NULL)) { 1055 if (tc_fill_qdisc(skb, old, clid, portid, n->nlmsg_seq, 1056 0, RTM_DELQDISC, extack) < 0) 1057 goto err_out; 1058 } 1059 if (new && !tc_qdisc_dump_ignore(new, false, NULL)) { 1060 if (tc_fill_qdisc(skb, new, clid, portid, n->nlmsg_seq, 1061 old ? NLM_F_REPLACE : 0, RTM_NEWQDISC, extack) < 0) 1062 goto err_out; 1063 } 1064 1065 if (skb->len) 1066 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, 1067 n->nlmsg_flags & NLM_F_ECHO); 1068 1069 err_out: 1070 kfree_skb(skb); 1071 return -EINVAL; 1072 } 1073 1074 static void notify_and_destroy(struct net *net, struct sk_buff *skb, 1075 struct nlmsghdr *n, u32 clid, 1076 struct Qdisc *old, struct Qdisc *new, 1077 struct netlink_ext_ack *extack) 1078 { 1079 if (new || old) 1080 qdisc_notify(net, skb, n, clid, old, new, extack); 1081 1082 if (old) 1083 qdisc_put(old); 1084 } 1085 1086 static void qdisc_clear_nolock(struct Qdisc *sch) 1087 { 1088 sch->flags &= ~TCQ_F_NOLOCK; 1089 if (!(sch->flags & TCQ_F_CPUSTATS)) 1090 return; 1091 1092 free_percpu(sch->cpu_bstats); 1093 free_percpu(sch->cpu_qstats); 1094 sch->cpu_bstats = NULL; 1095 sch->cpu_qstats = NULL; 1096 sch->flags &= ~TCQ_F_CPUSTATS; 1097 } 1098 1099 /* Graft qdisc "new" to class "classid" of qdisc "parent" or 1100 * to device "dev". 1101 * 1102 * When appropriate send a netlink notification using 'skb' 1103 * and "n". 1104 * 1105 * On success, destroy old qdisc. 1106 */ 1107 1108 static int qdisc_graft(struct net_device *dev, struct Qdisc *parent, 1109 struct sk_buff *skb, struct nlmsghdr *n, u32 classid, 1110 struct Qdisc *new, struct Qdisc *old, 1111 struct netlink_ext_ack *extack) 1112 { 1113 struct Qdisc *q = old; 1114 struct net *net = dev_net(dev); 1115 1116 if (parent == NULL) { 1117 unsigned int i, num_q, ingress; 1118 struct netdev_queue *dev_queue; 1119 1120 if (new) 1121 new->depth = 0; 1122 1123 ingress = 0; 1124 num_q = dev->num_tx_queues; 1125 if ((q && q->flags & TCQ_F_INGRESS) || 1126 (new && new->flags & TCQ_F_INGRESS)) { 1127 ingress = 1; 1128 dev_queue = dev_ingress_queue(dev); 1129 if (!dev_queue) { 1130 NL_SET_ERR_MSG(extack, "Device does not have an ingress queue"); 1131 return -ENOENT; 1132 } 1133 1134 q = rtnl_dereference(dev_queue->qdisc_sleeping); 1135 1136 /* This is the counterpart of that qdisc_refcount_inc_nz() call in 1137 * __tcf_qdisc_find() for filter requests. 1138 */ 1139 if (!qdisc_refcount_dec_if_one(q)) { 1140 NL_SET_ERR_MSG(extack, 1141 "Current ingress or clsact Qdisc has ongoing filter requests"); 1142 return -EBUSY; 1143 } 1144 } 1145 1146 if (dev->flags & IFF_UP) 1147 dev_deactivate(dev, false); 1148 1149 qdisc_offload_graft_root(dev, new, old, extack); 1150 1151 if (new && new->ops->attach && !ingress) 1152 goto skip; 1153 1154 if (!ingress) { 1155 for (i = 0; i < num_q; i++) { 1156 dev_queue = netdev_get_tx_queue(dev, i); 1157 old = dev_graft_qdisc(dev_queue, new); 1158 1159 if (new && i > 0) 1160 qdisc_refcount_inc(new); 1161 qdisc_put(old); 1162 } 1163 } else { 1164 old = dev_graft_qdisc(dev_queue, NULL); 1165 1166 /* {ingress,clsact}_destroy() @old before grafting @new to avoid 1167 * unprotected concurrent accesses to net_device::miniq_{in,e}gress 1168 * pointer(s) in mini_qdisc_pair_swap(). 1169 */ 1170 qdisc_notify(net, skb, n, classid, old, new, extack); 1171 qdisc_destroy(old); 1172 1173 dev_graft_qdisc(dev_queue, new); 1174 } 1175 1176 skip: 1177 if (!ingress) { 1178 old = rtnl_dereference(dev->qdisc); 1179 if (new && !new->ops->attach) 1180 qdisc_refcount_inc(new); 1181 rcu_assign_pointer(dev->qdisc, new ? : &noop_qdisc); 1182 1183 notify_and_destroy(net, skb, n, classid, old, new, extack); 1184 1185 if (new && new->ops->attach) 1186 new->ops->attach(new); 1187 } 1188 1189 if (dev->flags & IFF_UP) 1190 dev_activate(dev); 1191 } else { 1192 const struct Qdisc_class_ops *cops = parent->ops->cl_ops; 1193 unsigned long cl; 1194 int err; 1195 1196 /* Only support running class lockless if parent is lockless */ 1197 if (new && (new->flags & TCQ_F_NOLOCK) && !(parent->flags & TCQ_F_NOLOCK)) 1198 qdisc_clear_nolock(new); 1199 1200 if (!cops || !cops->graft) 1201 return -EOPNOTSUPP; 1202 1203 cl = cops->find(parent, classid); 1204 if (!cl) { 1205 NL_SET_ERR_MSG(extack, "Specified class not found"); 1206 return -ENOENT; 1207 } 1208 1209 if (new && new->ops == &noqueue_qdisc_ops) { 1210 NL_SET_ERR_MSG(extack, "Cannot assign noqueue to a class"); 1211 return -EINVAL; 1212 } 1213 1214 if (new && 1215 !(parent->flags & TCQ_F_MQROOT) && 1216 rcu_access_pointer(new->stab)) { 1217 NL_SET_ERR_MSG(extack, "STAB not supported on a non root"); 1218 return -EINVAL; 1219 } 1220 if (new && parent->depth >= 7) { 1221 NL_SET_ERR_MSG(extack, "Qdisc hierarchy is too deep"); 1222 return -E2BIG; 1223 } 1224 err = cops->graft(parent, cl, new, &old, extack); 1225 if (err) 1226 return err; 1227 if (new) 1228 new->depth = parent->depth + 1; 1229 notify_and_destroy(net, skb, n, classid, old, new, extack); 1230 } 1231 return 0; 1232 } 1233 1234 static int qdisc_block_indexes_set(struct Qdisc *sch, struct nlattr **tca, 1235 struct netlink_ext_ack *extack) 1236 { 1237 u32 block_index; 1238 1239 if (tca[TCA_INGRESS_BLOCK]) { 1240 block_index = nla_get_u32(tca[TCA_INGRESS_BLOCK]); 1241 1242 if (!block_index) { 1243 NL_SET_ERR_MSG(extack, "Ingress block index cannot be 0"); 1244 return -EINVAL; 1245 } 1246 if (!sch->ops->ingress_block_set) { 1247 NL_SET_ERR_MSG(extack, "Ingress block sharing is not supported"); 1248 return -EOPNOTSUPP; 1249 } 1250 sch->ops->ingress_block_set(sch, block_index); 1251 } 1252 if (tca[TCA_EGRESS_BLOCK]) { 1253 block_index = nla_get_u32(tca[TCA_EGRESS_BLOCK]); 1254 1255 if (!block_index) { 1256 NL_SET_ERR_MSG(extack, "Egress block index cannot be 0"); 1257 return -EINVAL; 1258 } 1259 if (!sch->ops->egress_block_set) { 1260 NL_SET_ERR_MSG(extack, "Egress block sharing is not supported"); 1261 return -EOPNOTSUPP; 1262 } 1263 sch->ops->egress_block_set(sch, block_index); 1264 } 1265 return 0; 1266 } 1267 1268 /* 1269 Allocate and initialize new qdisc. 1270 1271 Parameters are passed via opt. 1272 */ 1273 1274 static struct Qdisc *qdisc_create(struct net_device *dev, 1275 struct netdev_queue *dev_queue, 1276 u32 parent, u32 handle, 1277 struct nlattr **tca, int *errp, 1278 struct netlink_ext_ack *extack) 1279 { 1280 int err; 1281 struct nlattr *kind = tca[TCA_KIND]; 1282 struct Qdisc *sch; 1283 struct Qdisc_ops *ops; 1284 struct qdisc_size_table *stab; 1285 1286 ops = qdisc_lookup_ops(kind); 1287 if (!ops) { 1288 err = -ENOENT; 1289 NL_SET_ERR_MSG(extack, "Specified qdisc kind is unknown"); 1290 goto err_out; 1291 } 1292 1293 sch = qdisc_alloc(dev_queue, ops, extack); 1294 if (IS_ERR(sch)) { 1295 err = PTR_ERR(sch); 1296 goto err_out2; 1297 } 1298 1299 sch->parent = parent; 1300 1301 if (handle == TC_H_INGRESS) { 1302 if (!(sch->flags & TCQ_F_INGRESS)) { 1303 NL_SET_ERR_MSG(extack, 1304 "Specified parent ID is reserved for ingress and clsact Qdiscs"); 1305 err = -EINVAL; 1306 goto err_out3; 1307 } 1308 handle = TC_H_MAKE(TC_H_INGRESS, 0); 1309 } else { 1310 if (handle == 0) { 1311 handle = qdisc_alloc_handle(dev); 1312 if (handle == 0) { 1313 NL_SET_ERR_MSG(extack, "Maximum number of qdisc handles was exceeded"); 1314 err = -ENOSPC; 1315 goto err_out3; 1316 } 1317 } 1318 if (!netif_is_multiqueue(dev)) 1319 sch->flags |= TCQ_F_ONETXQUEUE; 1320 } 1321 1322 sch->handle = handle; 1323 1324 /* This exist to keep backward compatible with a userspace 1325 * loophole, what allowed userspace to get IFF_NO_QUEUE 1326 * facility on older kernels by setting tx_queue_len=0 (prior 1327 * to qdisc init), and then forgot to reinit tx_queue_len 1328 * before again attaching a qdisc. 1329 */ 1330 if ((dev->priv_flags & IFF_NO_QUEUE) && (dev->tx_queue_len == 0)) { 1331 WRITE_ONCE(dev->tx_queue_len, DEFAULT_TX_QUEUE_LEN); 1332 netdev_info(dev, "Caught tx_queue_len zero misconfig\n"); 1333 } 1334 1335 err = qdisc_block_indexes_set(sch, tca, extack); 1336 if (err) 1337 goto err_out3; 1338 1339 if (tca[TCA_STAB]) { 1340 stab = qdisc_get_stab(tca[TCA_STAB], extack); 1341 if (IS_ERR(stab)) { 1342 err = PTR_ERR(stab); 1343 goto err_out3; 1344 } 1345 rcu_assign_pointer(sch->stab, stab); 1346 } 1347 1348 if (ops->init) { 1349 err = ops->init(sch, tca[TCA_OPTIONS], extack); 1350 if (err != 0) 1351 goto err_out4; 1352 } 1353 1354 if (tca[TCA_RATE]) { 1355 err = -EOPNOTSUPP; 1356 if (sch->flags & TCQ_F_MQROOT) { 1357 NL_SET_ERR_MSG(extack, "Cannot attach rate estimator to a multi-queue root qdisc"); 1358 goto err_out4; 1359 } 1360 1361 err = gen_new_estimator(&sch->bstats, 1362 sch->cpu_bstats, 1363 &sch->rate_est, 1364 NULL, 1365 true, 1366 tca[TCA_RATE]); 1367 if (err) { 1368 NL_SET_ERR_MSG(extack, "Failed to generate new estimator"); 1369 goto err_out4; 1370 } 1371 } 1372 1373 qdisc_hash_add(sch, false); 1374 trace_qdisc_create(ops, dev, parent); 1375 1376 return sch; 1377 1378 err_out4: 1379 /* Even if ops->init() failed, we call ops->destroy() 1380 * like qdisc_create_dflt(). 1381 */ 1382 if (ops->destroy) 1383 ops->destroy(sch); 1384 qdisc_put_stab(rtnl_dereference(sch->stab)); 1385 err_out3: 1386 qdisc_lock_uninit(sch, ops); 1387 netdev_put(dev, &sch->dev_tracker); 1388 qdisc_free(sch); 1389 err_out2: 1390 bpf_module_put(ops, ops->owner); 1391 err_out: 1392 *errp = err; 1393 return NULL; 1394 } 1395 1396 static int qdisc_change(struct Qdisc *sch, struct nlattr **tca, 1397 struct netlink_ext_ack *extack) 1398 { 1399 struct qdisc_size_table *ostab, *stab = NULL; 1400 int err = 0; 1401 1402 if (tca[TCA_OPTIONS]) { 1403 if (!sch->ops->change) { 1404 NL_SET_ERR_MSG(extack, "Change operation not supported by specified qdisc"); 1405 return -EINVAL; 1406 } 1407 if (tca[TCA_INGRESS_BLOCK] || tca[TCA_EGRESS_BLOCK]) { 1408 NL_SET_ERR_MSG(extack, "Change of blocks is not supported"); 1409 return -EOPNOTSUPP; 1410 } 1411 err = sch->ops->change(sch, tca[TCA_OPTIONS], extack); 1412 if (err) 1413 return err; 1414 } 1415 1416 if (tca[TCA_STAB]) { 1417 stab = qdisc_get_stab(tca[TCA_STAB], extack); 1418 if (IS_ERR(stab)) 1419 return PTR_ERR(stab); 1420 } 1421 1422 ostab = rtnl_dereference(sch->stab); 1423 rcu_assign_pointer(sch->stab, stab); 1424 qdisc_put_stab(ostab); 1425 1426 if (tca[TCA_RATE]) { 1427 /* NB: ignores errors from replace_estimator 1428 because change can't be undone. */ 1429 if (sch->flags & TCQ_F_MQROOT) 1430 goto out; 1431 gen_replace_estimator(&sch->bstats, 1432 sch->cpu_bstats, 1433 &sch->rate_est, 1434 NULL, 1435 true, 1436 tca[TCA_RATE]); 1437 } 1438 out: 1439 return 0; 1440 } 1441 1442 struct check_loop_arg { 1443 struct qdisc_walker w; 1444 struct Qdisc *p; 1445 int depth; 1446 }; 1447 1448 static int check_loop_fn(struct Qdisc *q, unsigned long cl, 1449 struct qdisc_walker *w); 1450 1451 static int check_loop(struct Qdisc *q, struct Qdisc *p, int depth) 1452 { 1453 struct check_loop_arg arg; 1454 1455 if (q->ops->cl_ops == NULL) 1456 return 0; 1457 1458 arg.w.stop = arg.w.skip = arg.w.count = 0; 1459 arg.w.fn = check_loop_fn; 1460 arg.depth = depth; 1461 arg.p = p; 1462 q->ops->cl_ops->walk(q, &arg.w); 1463 return arg.w.stop ? -ELOOP : 0; 1464 } 1465 1466 static int 1467 check_loop_fn(struct Qdisc *q, unsigned long cl, struct qdisc_walker *w) 1468 { 1469 struct Qdisc *leaf; 1470 const struct Qdisc_class_ops *cops = q->ops->cl_ops; 1471 struct check_loop_arg *arg = (struct check_loop_arg *)w; 1472 1473 leaf = cops->leaf(q, cl); 1474 if (leaf) { 1475 if (leaf == arg->p || arg->depth > 7) 1476 return -ELOOP; 1477 return check_loop(leaf, arg->p, arg->depth + 1); 1478 } 1479 return 0; 1480 } 1481 1482 const struct nla_policy rtm_tca_policy[TCA_MAX + 1] = { 1483 [TCA_KIND] = { .type = NLA_STRING }, 1484 [TCA_RATE] = { .type = NLA_BINARY, 1485 .len = sizeof(struct tc_estimator) }, 1486 [TCA_STAB] = { .type = NLA_NESTED }, 1487 [TCA_DUMP_INVISIBLE] = { .type = NLA_FLAG }, 1488 [TCA_CHAIN] = { .type = NLA_U32 }, 1489 [TCA_INGRESS_BLOCK] = { .type = NLA_U32 }, 1490 [TCA_EGRESS_BLOCK] = { .type = NLA_U32 }, 1491 }; 1492 1493 /* 1494 * Delete/get qdisc. 1495 */ 1496 1497 static int __tc_get_qdisc(struct sk_buff *skb, struct nlmsghdr *n, 1498 struct netlink_ext_ack *extack, 1499 struct net_device *dev, 1500 struct nlattr *tca[TCA_MAX + 1], 1501 struct tcmsg *tcm) 1502 { 1503 struct net *net = sock_net(skb->sk); 1504 struct Qdisc *q = NULL; 1505 struct Qdisc *p = NULL; 1506 u32 clid; 1507 int err; 1508 1509 clid = tcm->tcm_parent; 1510 if (clid) { 1511 if (clid != TC_H_ROOT) { 1512 if (TC_H_MAJ(clid) != TC_H_MAJ(TC_H_INGRESS)) { 1513 p = qdisc_lookup(dev, TC_H_MAJ(clid)); 1514 if (!p) { 1515 NL_SET_ERR_MSG(extack, "Failed to find qdisc with specified classid"); 1516 return -ENOENT; 1517 } 1518 q = qdisc_leaf(p, clid, extack); 1519 } else if (dev_ingress_queue(dev)) { 1520 q = rtnl_dereference(dev_ingress_queue(dev)->qdisc_sleeping); 1521 } 1522 } else { 1523 q = rtnl_dereference(dev->qdisc); 1524 } 1525 if (!q) { 1526 NL_SET_ERR_MSG(extack, "Cannot find specified qdisc on specified device"); 1527 return -ENOENT; 1528 } 1529 if (IS_ERR(q)) 1530 return PTR_ERR(q); 1531 1532 if (tcm->tcm_handle && q->handle != tcm->tcm_handle) { 1533 NL_SET_ERR_MSG(extack, "Invalid handle"); 1534 return -EINVAL; 1535 } 1536 } else { 1537 q = qdisc_lookup(dev, tcm->tcm_handle); 1538 if (!q) { 1539 NL_SET_ERR_MSG(extack, "Failed to find qdisc with specified handle"); 1540 return -ENOENT; 1541 } 1542 } 1543 1544 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id)) { 1545 NL_SET_ERR_MSG(extack, "Invalid qdisc name: must match existing qdisc"); 1546 return -EINVAL; 1547 } 1548 1549 if (n->nlmsg_type == RTM_DELQDISC) { 1550 if (!clid) { 1551 NL_SET_ERR_MSG(extack, "Classid cannot be zero"); 1552 return -EINVAL; 1553 } 1554 if (q->handle == 0) { 1555 NL_SET_ERR_MSG(extack, "Cannot delete qdisc with handle of zero"); 1556 return -ENOENT; 1557 } 1558 err = qdisc_graft(dev, p, skb, n, clid, NULL, q, extack); 1559 if (err != 0) 1560 return err; 1561 } else { 1562 qdisc_get_notify(net, skb, n, clid, q, NULL); 1563 } 1564 return 0; 1565 } 1566 1567 static int tc_get_qdisc(struct sk_buff *skb, struct nlmsghdr *n, 1568 struct netlink_ext_ack *extack) 1569 { 1570 struct net *net = sock_net(skb->sk); 1571 struct tcmsg *tcm = nlmsg_data(n); 1572 struct nlattr *tca[TCA_MAX + 1]; 1573 struct net_device *dev; 1574 int err; 1575 1576 err = nlmsg_parse_deprecated(n, sizeof(*tcm), tca, TCA_MAX, 1577 rtm_tca_policy, extack); 1578 if (err < 0) 1579 return err; 1580 1581 dev = __dev_get_by_index(net, tcm->tcm_ifindex); 1582 if (!dev) 1583 return -ENODEV; 1584 1585 netdev_lock_ops(dev); 1586 err = __tc_get_qdisc(skb, n, extack, dev, tca, tcm); 1587 netdev_unlock_ops(dev); 1588 1589 return err; 1590 } 1591 1592 static bool req_create_or_replace(struct nlmsghdr *n) 1593 { 1594 return (n->nlmsg_flags & NLM_F_CREATE && 1595 n->nlmsg_flags & NLM_F_REPLACE); 1596 } 1597 1598 static bool req_create_exclusive(struct nlmsghdr *n) 1599 { 1600 return (n->nlmsg_flags & NLM_F_CREATE && 1601 n->nlmsg_flags & NLM_F_EXCL); 1602 } 1603 1604 static bool req_change(struct nlmsghdr *n) 1605 { 1606 return (!(n->nlmsg_flags & NLM_F_CREATE) && 1607 !(n->nlmsg_flags & NLM_F_REPLACE) && 1608 !(n->nlmsg_flags & NLM_F_EXCL)); 1609 } 1610 1611 static int __tc_modify_qdisc(struct sk_buff *skb, struct nlmsghdr *n, 1612 struct netlink_ext_ack *extack, 1613 struct net_device *dev, 1614 struct nlattr *tca[TCA_MAX + 1], 1615 struct tcmsg *tcm) 1616 { 1617 struct Qdisc *q = NULL; 1618 struct Qdisc *p = NULL; 1619 u32 clid; 1620 int err; 1621 1622 clid = tcm->tcm_parent; 1623 1624 if (clid) { 1625 if (clid != TC_H_ROOT) { 1626 if (clid != TC_H_INGRESS) { 1627 p = qdisc_lookup(dev, TC_H_MAJ(clid)); 1628 if (!p) { 1629 NL_SET_ERR_MSG(extack, "Failed to find specified qdisc"); 1630 return -ENOENT; 1631 } 1632 if (p->flags & TCQ_F_INGRESS) { 1633 NL_SET_ERR_MSG(extack, 1634 "Cannot add children to ingress/clsact qdisc"); 1635 return -EOPNOTSUPP; 1636 } 1637 q = qdisc_leaf(p, clid, extack); 1638 if (IS_ERR(q)) 1639 return PTR_ERR(q); 1640 } else if (dev_ingress_queue_create(dev)) { 1641 q = rtnl_dereference(dev_ingress_queue(dev)->qdisc_sleeping); 1642 } 1643 } else { 1644 q = rtnl_dereference(dev->qdisc); 1645 } 1646 1647 /* It may be default qdisc, ignore it */ 1648 if (q && q->handle == 0) 1649 q = NULL; 1650 1651 if (!q || !tcm->tcm_handle || q->handle != tcm->tcm_handle) { 1652 if (tcm->tcm_handle) { 1653 if (q && !(n->nlmsg_flags & NLM_F_REPLACE)) { 1654 NL_SET_ERR_MSG(extack, "NLM_F_REPLACE needed to override"); 1655 return -EEXIST; 1656 } 1657 if (TC_H_MIN(tcm->tcm_handle)) { 1658 NL_SET_ERR_MSG(extack, "Invalid minor handle"); 1659 return -EINVAL; 1660 } 1661 q = qdisc_lookup(dev, tcm->tcm_handle); 1662 if (!q) 1663 goto create_n_graft; 1664 if (q->parent != tcm->tcm_parent) { 1665 NL_SET_ERR_MSG(extack, "Cannot move an existing qdisc to a different parent"); 1666 return -EINVAL; 1667 } 1668 if (n->nlmsg_flags & NLM_F_EXCL) { 1669 NL_SET_ERR_MSG(extack, "Exclusivity flag on, cannot override"); 1670 return -EEXIST; 1671 } 1672 if (tca[TCA_KIND] && 1673 nla_strcmp(tca[TCA_KIND], q->ops->id)) { 1674 NL_SET_ERR_MSG(extack, "Invalid qdisc name: must match existing qdisc"); 1675 return -EINVAL; 1676 } 1677 if (q->flags & TCQ_F_INGRESS) { 1678 NL_SET_ERR_MSG(extack, 1679 "Cannot regraft ingress or clsact Qdiscs"); 1680 return -EINVAL; 1681 } 1682 if (q == p || 1683 (p && check_loop(q, p, 0))) { 1684 NL_SET_ERR_MSG(extack, "Qdisc parent/child loop detected"); 1685 return -ELOOP; 1686 } 1687 if (clid == TC_H_INGRESS) { 1688 NL_SET_ERR_MSG(extack, "Ingress cannot graft directly"); 1689 return -EINVAL; 1690 } 1691 qdisc_refcount_inc(q); 1692 goto graft; 1693 } else { 1694 if (!q) 1695 goto create_n_graft; 1696 1697 /* This magic test requires explanation. 1698 * 1699 * We know, that some child q is already 1700 * attached to this parent and have choice: 1701 * 1) change it or 2) create/graft new one. 1702 * If the requested qdisc kind is different 1703 * than the existing one, then we choose graft. 1704 * If they are the same then this is "change" 1705 * operation - just let it fallthrough.. 1706 * 1707 * 1. We are allowed to create/graft only 1708 * if the request is explicitly stating 1709 * "please create if it doesn't exist". 1710 * 1711 * 2. If the request is to exclusive create 1712 * then the qdisc tcm_handle is not expected 1713 * to exist, so that we choose create/graft too. 1714 * 1715 * 3. The last case is when no flags are set. 1716 * This will happen when for example tc 1717 * utility issues a "change" command. 1718 * Alas, it is sort of hole in API, we 1719 * cannot decide what to do unambiguously. 1720 * For now we select create/graft. 1721 */ 1722 if (tca[TCA_KIND] && 1723 nla_strcmp(tca[TCA_KIND], q->ops->id)) { 1724 if (req_create_or_replace(n) || 1725 req_create_exclusive(n)) 1726 goto create_n_graft; 1727 else if (req_change(n)) 1728 goto create_n_graft2; 1729 } 1730 } 1731 } 1732 } else { 1733 if (!tcm->tcm_handle) { 1734 NL_SET_ERR_MSG(extack, "Handle cannot be zero"); 1735 return -EINVAL; 1736 } 1737 q = qdisc_lookup(dev, tcm->tcm_handle); 1738 } 1739 1740 /* Change qdisc parameters */ 1741 if (!q) { 1742 NL_SET_ERR_MSG(extack, "Specified qdisc not found"); 1743 return -ENOENT; 1744 } 1745 if (n->nlmsg_flags & NLM_F_EXCL) { 1746 NL_SET_ERR_MSG(extack, "Exclusivity flag on, cannot modify"); 1747 return -EEXIST; 1748 } 1749 if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], q->ops->id)) { 1750 NL_SET_ERR_MSG(extack, "Invalid qdisc name: must match existing qdisc"); 1751 return -EINVAL; 1752 } 1753 err = qdisc_change(q, tca, extack); 1754 if (err == 0) 1755 qdisc_notify(sock_net(skb->sk), skb, n, clid, NULL, q, extack); 1756 return err; 1757 1758 create_n_graft: 1759 if (!(n->nlmsg_flags & NLM_F_CREATE)) { 1760 NL_SET_ERR_MSG(extack, "Qdisc not found. To create specify NLM_F_CREATE flag"); 1761 return -ENOENT; 1762 } 1763 create_n_graft2: 1764 if (clid == TC_H_INGRESS) { 1765 if (dev_ingress_queue(dev)) { 1766 q = qdisc_create(dev, dev_ingress_queue(dev), 1767 tcm->tcm_parent, tcm->tcm_parent, 1768 tca, &err, extack); 1769 } else { 1770 NL_SET_ERR_MSG(extack, "Cannot find ingress queue for specified device"); 1771 err = -ENOENT; 1772 } 1773 } else { 1774 struct netdev_queue *dev_queue; 1775 1776 if (p && p->ops->cl_ops && p->ops->cl_ops->select_queue) 1777 dev_queue = p->ops->cl_ops->select_queue(p, tcm); 1778 else if (p) 1779 dev_queue = p->dev_queue; 1780 else 1781 dev_queue = netdev_get_tx_queue(dev, 0); 1782 1783 q = qdisc_create(dev, dev_queue, 1784 tcm->tcm_parent, tcm->tcm_handle, 1785 tca, &err, extack); 1786 } 1787 if (!q) 1788 return err; 1789 1790 graft: 1791 err = qdisc_graft(dev, p, skb, n, clid, q, NULL, extack); 1792 if (err) { 1793 if (q) 1794 qdisc_put(q); 1795 return err; 1796 } 1797 1798 return 0; 1799 } 1800 1801 static void request_qdisc_module(struct nlattr *kind) 1802 { 1803 struct Qdisc_ops *ops; 1804 char name[IFNAMSIZ]; 1805 1806 if (!kind) 1807 return; 1808 1809 ops = qdisc_lookup_ops(kind); 1810 if (ops) { 1811 bpf_module_put(ops, ops->owner); 1812 return; 1813 } 1814 1815 if (nla_strscpy(name, kind, IFNAMSIZ) >= 0) { 1816 rtnl_unlock(); 1817 request_module(NET_SCH_ALIAS_PREFIX "%s", name); 1818 rtnl_lock(); 1819 } 1820 } 1821 1822 /* 1823 * Create/change qdisc. 1824 */ 1825 static int tc_modify_qdisc(struct sk_buff *skb, struct nlmsghdr *n, 1826 struct netlink_ext_ack *extack) 1827 { 1828 struct net *net = sock_net(skb->sk); 1829 struct nlattr *tca[TCA_MAX + 1]; 1830 struct net_device *dev; 1831 struct tcmsg *tcm; 1832 int err; 1833 1834 err = nlmsg_parse_deprecated(n, sizeof(*tcm), tca, TCA_MAX, 1835 rtm_tca_policy, extack); 1836 if (err < 0) 1837 return err; 1838 1839 request_qdisc_module(tca[TCA_KIND]); 1840 1841 tcm = nlmsg_data(n); 1842 dev = __dev_get_by_index(net, tcm->tcm_ifindex); 1843 if (!dev) 1844 return -ENODEV; 1845 1846 netdev_lock_ops(dev); 1847 err = __tc_modify_qdisc(skb, n, extack, dev, tca, tcm); 1848 netdev_unlock_ops(dev); 1849 1850 return err; 1851 } 1852 1853 static int tc_dump_qdisc_root(struct Qdisc *root, struct sk_buff *skb, 1854 struct netlink_callback *cb, 1855 int *q_idx_p, int s_q_idx, bool recur, 1856 bool dump_invisible) 1857 { 1858 const struct nlmsghdr *nlh = cb->nlh; 1859 int ret = 0, q_idx = *q_idx_p; 1860 const struct tcmsg *tcm; 1861 struct Qdisc *q; 1862 int b; 1863 1864 if (!root) 1865 return 0; 1866 1867 tcm = nlmsg_data(nlh); 1868 q = root; 1869 if (q_idx < s_q_idx) { 1870 q_idx++; 1871 } else { 1872 if (!tc_qdisc_dump_ignore(q, dump_invisible, tcm)) 1873 ret = tc_fill_qdisc(skb, q, q->parent, 1874 NETLINK_CB(cb->skb).portid, 1875 nlh->nlmsg_seq, NLM_F_MULTI, 1876 RTM_NEWQDISC, NULL); 1877 if (ret < 0) 1878 goto out; 1879 q_idx++; 1880 } 1881 1882 /* If dumping singletons, there is no qdisc_dev(root) and the singleton 1883 * itself has already been dumped. 1884 * 1885 * If we've already dumped the top-level (ingress) qdisc above and the global 1886 * qdisc hashtable, we don't want to hit it again 1887 */ 1888 if (!qdisc_dev(root) || !recur) 1889 goto out; 1890 1891 hash_for_each(qdisc_dev(root)->qdisc_hash, b, q, hash) { 1892 if (q_idx < s_q_idx) { 1893 q_idx++; 1894 continue; 1895 } 1896 if (!tc_qdisc_dump_ignore(q, dump_invisible, tcm)) 1897 ret = tc_fill_qdisc(skb, q, q->parent, 1898 NETLINK_CB(cb->skb).portid, 1899 nlh->nlmsg_seq, NLM_F_MULTI, 1900 RTM_NEWQDISC, NULL); 1901 if (ret < 0) 1902 goto out; 1903 q_idx++; 1904 } 1905 1906 out: 1907 *q_idx_p = q_idx; 1908 return ret; 1909 } 1910 1911 static int tc_dump_qdisc(struct sk_buff *skb, struct netlink_callback *cb) 1912 { 1913 const struct nlmsghdr *nlh = cb->nlh; 1914 struct net *net = sock_net(skb->sk); 1915 struct nlattr *tca[TCA_MAX + 1]; 1916 struct { 1917 unsigned long ifindex; 1918 int q_idx; 1919 } *ctx = (void *)cb->ctx; 1920 const struct tcmsg *tcm; 1921 struct net_device *dev; 1922 int s_q_idx, q_idx; 1923 int err; 1924 1925 ASSERT_RTNL(); 1926 1927 err = nlmsg_parse_deprecated(nlh, sizeof(struct tcmsg), tca, TCA_MAX, 1928 rtm_tca_policy, cb->extack); 1929 if (err < 0) 1930 return err; 1931 tcm = nlmsg_data(nlh); 1932 if (tcm->tcm_ifindex && !ctx->ifindex) 1933 ctx->ifindex = tcm->tcm_ifindex; 1934 1935 s_q_idx = ctx->q_idx; 1936 1937 for_each_netdev_dump(net, dev, ctx->ifindex) { 1938 struct netdev_queue *dev_queue; 1939 struct Qdisc *q; 1940 1941 if (tcm->tcm_ifindex && ctx->ifindex != tcm->tcm_ifindex) 1942 break; 1943 1944 q_idx = 0; 1945 1946 netdev_lock_ops(dev); 1947 q = rtnl_dereference(dev->qdisc); 1948 err = tc_dump_qdisc_root(q, skb, cb, &q_idx, s_q_idx, 1949 true, tca[TCA_DUMP_INVISIBLE]); 1950 if (err < 0) 1951 goto error_unlock; 1952 1953 dev_queue = dev_ingress_queue(dev); 1954 if (dev_queue) { 1955 q = rtnl_dereference(dev_queue->qdisc_sleeping); 1956 err = tc_dump_qdisc_root(q, skb, cb, &q_idx, s_q_idx, 1957 false, tca[TCA_DUMP_INVISIBLE]); 1958 if (err < 0) 1959 goto error_unlock; 1960 } 1961 netdev_unlock_ops(dev); 1962 s_q_idx = 0; 1963 } 1964 return skb->len; 1965 1966 error_unlock: 1967 netdev_unlock_ops(dev); 1968 ctx->q_idx = q_idx; 1969 1970 return err; 1971 } 1972 1973 1974 1975 /************************************************ 1976 * Traffic classes manipulation. * 1977 ************************************************/ 1978 1979 static int tc_fill_tclass(struct sk_buff *skb, struct Qdisc *q, 1980 unsigned long cl, u32 portid, u32 seq, u16 flags, 1981 int event, struct netlink_ext_ack *extack) 1982 { 1983 struct tcmsg *tcm; 1984 struct nlmsghdr *nlh; 1985 unsigned char *b = skb_tail_pointer(skb); 1986 struct gnet_dump d; 1987 const struct Qdisc_class_ops *cl_ops = q->ops->cl_ops; 1988 1989 cond_resched(); 1990 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*tcm), flags); 1991 if (!nlh) 1992 goto out_nlmsg_trim; 1993 tcm = nlmsg_data(nlh); 1994 tcm->tcm_family = AF_UNSPEC; 1995 tcm->tcm__pad1 = 0; 1996 tcm->tcm__pad2 = 0; 1997 tcm->tcm_ifindex = qdisc_dev(q)->ifindex; 1998 tcm->tcm_parent = q->handle; 1999 tcm->tcm_handle = q->handle; 2000 tcm->tcm_info = 0; 2001 if (nla_put_string(skb, TCA_KIND, q->ops->id)) 2002 goto nla_put_failure; 2003 if (cl_ops->dump && cl_ops->dump(q, cl, skb, tcm) < 0) 2004 goto nla_put_failure; 2005 2006 if (gnet_stats_start_copy_compat(skb, TCA_STATS2, TCA_STATS, TCA_XSTATS, 2007 NULL, &d, TCA_PAD) < 0) 2008 goto nla_put_failure; 2009 2010 if (cl_ops->dump_stats && cl_ops->dump_stats(q, cl, &d) < 0) 2011 goto nla_put_failure; 2012 2013 if (gnet_stats_finish_copy(&d) < 0) 2014 goto nla_put_failure; 2015 2016 if (extack && extack->_msg && 2017 nla_put_string(skb, TCA_EXT_WARN_MSG, extack->_msg)) 2018 goto out_nlmsg_trim; 2019 2020 nlh->nlmsg_len = skb_tail_pointer(skb) - b; 2021 2022 return skb->len; 2023 2024 out_nlmsg_trim: 2025 nla_put_failure: 2026 nlmsg_trim(skb, b); 2027 return -EMSGSIZE; 2028 } 2029 2030 static int tclass_notify(struct net *net, struct sk_buff *oskb, 2031 struct nlmsghdr *n, struct Qdisc *q, 2032 unsigned long cl, int event, struct netlink_ext_ack *extack) 2033 { 2034 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; 2035 struct sk_buff *skb; 2036 int ret; 2037 2038 if (!rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC)) 2039 return 0; 2040 2041 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 2042 if (!skb) 2043 return -ENOBUFS; 2044 2045 ret = tc_fill_tclass(skb, q, cl, portid, n->nlmsg_seq, 0, event, extack); 2046 if (ret < 0) { 2047 kfree_skb(skb); 2048 return ret; 2049 } 2050 2051 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, 2052 n->nlmsg_flags & NLM_F_ECHO); 2053 } 2054 2055 static int tclass_get_notify(struct net *net, struct sk_buff *oskb, 2056 struct nlmsghdr *n, struct Qdisc *q, 2057 unsigned long cl, struct netlink_ext_ack *extack) 2058 { 2059 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; 2060 struct sk_buff *skb; 2061 int ret; 2062 2063 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 2064 if (!skb) 2065 return -ENOBUFS; 2066 2067 ret = tc_fill_tclass(skb, q, cl, portid, n->nlmsg_seq, 0, 2068 RTM_NEWTCLASS, extack); 2069 if (ret < 0) { 2070 kfree_skb(skb); 2071 return ret; 2072 } 2073 2074 return rtnetlink_send(skb, net, portid, RTNLGRP_TC, 2075 n->nlmsg_flags & NLM_F_ECHO); 2076 } 2077 2078 static int tclass_del_notify(struct net *net, 2079 const struct Qdisc_class_ops *cops, 2080 struct sk_buff *oskb, struct nlmsghdr *n, 2081 struct Qdisc *q, unsigned long cl, 2082 struct netlink_ext_ack *extack) 2083 { 2084 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; 2085 struct sk_buff *skb = NULL; 2086 int err = 0; 2087 2088 if (!cops->delete) 2089 return -EOPNOTSUPP; 2090 2091 if (rtnl_notify_needed(net, n->nlmsg_flags, RTNLGRP_TC)) { 2092 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 2093 if (!skb) 2094 return -ENOBUFS; 2095 2096 err = tc_fill_tclass(skb, q, cl, portid, n->nlmsg_seq, 0, 2097 RTM_DELTCLASS, extack); 2098 if (err < 0) { 2099 kfree_skb(skb); 2100 return err; 2101 } 2102 } 2103 2104 err = cops->delete(q, cl, extack); 2105 if (err) { 2106 kfree_skb(skb); 2107 return err; 2108 } 2109 2110 err = rtnetlink_maybe_send(skb, net, portid, RTNLGRP_TC, 2111 n->nlmsg_flags & NLM_F_ECHO); 2112 return err; 2113 } 2114 2115 #ifdef CONFIG_NET_CLS 2116 2117 struct tcf_bind_args { 2118 struct tcf_walker w; 2119 unsigned long base; 2120 unsigned long cl; 2121 u32 classid; 2122 }; 2123 2124 static int tcf_node_bind(struct tcf_proto *tp, void *n, struct tcf_walker *arg) 2125 { 2126 struct tcf_bind_args *a = (void *)arg; 2127 2128 if (n && tp->ops->bind_class) { 2129 struct Qdisc *q = tcf_block_q(tp->chain->block); 2130 2131 sch_tree_lock(q); 2132 tp->ops->bind_class(n, a->classid, a->cl, q, a->base); 2133 sch_tree_unlock(q); 2134 } 2135 return 0; 2136 } 2137 2138 struct tc_bind_class_args { 2139 struct qdisc_walker w; 2140 unsigned long new_cl; 2141 u32 portid; 2142 u32 clid; 2143 }; 2144 2145 static int tc_bind_class_walker(struct Qdisc *q, unsigned long cl, 2146 struct qdisc_walker *w) 2147 { 2148 struct tc_bind_class_args *a = (struct tc_bind_class_args *)w; 2149 const struct Qdisc_class_ops *cops = q->ops->cl_ops; 2150 struct tcf_block *block; 2151 struct tcf_chain *chain; 2152 2153 block = cops->tcf_block(q, cl, NULL); 2154 if (!block) 2155 return 0; 2156 for (chain = tcf_get_next_chain(block, NULL); 2157 chain; 2158 chain = tcf_get_next_chain(block, chain)) { 2159 struct tcf_proto *tp; 2160 2161 for (tp = tcf_get_next_proto(chain, NULL); 2162 tp; tp = tcf_get_next_proto(chain, tp)) { 2163 struct tcf_bind_args arg = {}; 2164 2165 arg.w.fn = tcf_node_bind; 2166 arg.classid = a->clid; 2167 arg.base = cl; 2168 arg.cl = a->new_cl; 2169 tp->ops->walk(tp, &arg.w, true); 2170 } 2171 } 2172 2173 return 0; 2174 } 2175 2176 static void tc_bind_tclass(struct Qdisc *q, u32 portid, u32 clid, 2177 unsigned long new_cl) 2178 { 2179 const struct Qdisc_class_ops *cops = q->ops->cl_ops; 2180 struct tc_bind_class_args args = {}; 2181 2182 if (!cops->tcf_block) 2183 return; 2184 args.portid = portid; 2185 args.clid = clid; 2186 args.new_cl = new_cl; 2187 args.w.fn = tc_bind_class_walker; 2188 q->ops->cl_ops->walk(q, &args.w); 2189 } 2190 2191 #else 2192 2193 static void tc_bind_tclass(struct Qdisc *q, u32 portid, u32 clid, 2194 unsigned long new_cl) 2195 { 2196 } 2197 2198 #endif 2199 2200 static int __tc_ctl_tclass(struct sk_buff *skb, struct nlmsghdr *n, 2201 struct netlink_ext_ack *extack, 2202 struct net_device *dev, 2203 struct nlattr *tca[TCA_MAX + 1], 2204 struct tcmsg *tcm) 2205 { 2206 struct net *net = sock_net(skb->sk); 2207 const struct Qdisc_class_ops *cops; 2208 struct Qdisc *q = NULL; 2209 unsigned long cl = 0; 2210 unsigned long new_cl; 2211 u32 portid; 2212 u32 clid; 2213 u32 qid; 2214 int err; 2215 2216 /* 2217 parent == TC_H_UNSPEC - unspecified parent. 2218 parent == TC_H_ROOT - class is root, which has no parent. 2219 parent == X:0 - parent is root class. 2220 parent == X:Y - parent is a node in hierarchy. 2221 parent == 0:Y - parent is X:Y, where X:0 is qdisc. 2222 2223 handle == 0:0 - generate handle from kernel pool. 2224 handle == 0:Y - class is X:Y, where X:0 is qdisc. 2225 handle == X:Y - clear. 2226 handle == X:0 - root class. 2227 */ 2228 2229 /* Step 1. Determine qdisc handle X:0 */ 2230 2231 portid = tcm->tcm_parent; 2232 clid = tcm->tcm_handle; 2233 qid = TC_H_MAJ(clid); 2234 2235 if (portid != TC_H_ROOT) { 2236 u32 qid1 = TC_H_MAJ(portid); 2237 2238 if (qid && qid1) { 2239 /* If both majors are known, they must be identical. */ 2240 if (qid != qid1) 2241 return -EINVAL; 2242 } else if (qid1) { 2243 qid = qid1; 2244 } else if (qid == 0) 2245 qid = rtnl_dereference(dev->qdisc)->handle; 2246 2247 /* Now qid is genuine qdisc handle consistent 2248 * both with parent and child. 2249 * 2250 * TC_H_MAJ(portid) still may be unspecified, complete it now. 2251 */ 2252 if (portid) 2253 portid = TC_H_MAKE(qid, portid); 2254 } else { 2255 if (qid == 0) 2256 qid = rtnl_dereference(dev->qdisc)->handle; 2257 } 2258 2259 /* OK. Locate qdisc */ 2260 q = qdisc_lookup(dev, qid); 2261 if (!q) 2262 return -ENOENT; 2263 2264 /* An check that it supports classes */ 2265 cops = q->ops->cl_ops; 2266 if (cops == NULL) 2267 return -EINVAL; 2268 2269 /* Now try to get class */ 2270 if (clid == 0) { 2271 if (portid == TC_H_ROOT) 2272 clid = qid; 2273 } else 2274 clid = TC_H_MAKE(qid, clid); 2275 2276 if (clid) 2277 cl = cops->find(q, clid); 2278 2279 if (cl == 0) { 2280 err = -ENOENT; 2281 if (n->nlmsg_type != RTM_NEWTCLASS || 2282 !(n->nlmsg_flags & NLM_F_CREATE)) 2283 goto out; 2284 } else { 2285 switch (n->nlmsg_type) { 2286 case RTM_NEWTCLASS: 2287 err = -EEXIST; 2288 if (n->nlmsg_flags & NLM_F_EXCL) 2289 goto out; 2290 break; 2291 case RTM_DELTCLASS: 2292 err = tclass_del_notify(net, cops, skb, n, q, cl, extack); 2293 /* Unbind the class with flilters with 0 */ 2294 tc_bind_tclass(q, portid, clid, 0); 2295 goto out; 2296 case RTM_GETTCLASS: 2297 err = tclass_get_notify(net, skb, n, q, cl, extack); 2298 goto out; 2299 default: 2300 err = -EINVAL; 2301 goto out; 2302 } 2303 } 2304 2305 if (tca[TCA_INGRESS_BLOCK] || tca[TCA_EGRESS_BLOCK]) { 2306 NL_SET_ERR_MSG(extack, "Shared blocks are not supported for classes"); 2307 return -EOPNOTSUPP; 2308 } 2309 2310 /* Prevent creation of traffic classes with classid TC_H_ROOT */ 2311 if (clid == TC_H_ROOT) { 2312 NL_SET_ERR_MSG(extack, "Cannot create traffic class with classid TC_H_ROOT"); 2313 return -EINVAL; 2314 } 2315 2316 new_cl = cl; 2317 err = -EOPNOTSUPP; 2318 if (cops->change) 2319 err = cops->change(q, clid, portid, tca, &new_cl, extack); 2320 if (err == 0) { 2321 tclass_notify(net, skb, n, q, new_cl, RTM_NEWTCLASS, extack); 2322 /* We just create a new class, need to do reverse binding. */ 2323 if (cl != new_cl) 2324 tc_bind_tclass(q, portid, clid, new_cl); 2325 } 2326 out: 2327 return err; 2328 } 2329 2330 static int tc_ctl_tclass(struct sk_buff *skb, struct nlmsghdr *n, 2331 struct netlink_ext_ack *extack) 2332 { 2333 struct net *net = sock_net(skb->sk); 2334 struct tcmsg *tcm = nlmsg_data(n); 2335 struct nlattr *tca[TCA_MAX + 1]; 2336 struct net_device *dev; 2337 int err; 2338 2339 err = nlmsg_parse_deprecated(n, sizeof(*tcm), tca, TCA_MAX, 2340 rtm_tca_policy, extack); 2341 if (err < 0) 2342 return err; 2343 2344 dev = __dev_get_by_index(net, tcm->tcm_ifindex); 2345 if (!dev) 2346 return -ENODEV; 2347 2348 netdev_lock_ops(dev); 2349 err = __tc_ctl_tclass(skb, n, extack, dev, tca, tcm); 2350 netdev_unlock_ops(dev); 2351 2352 return err; 2353 } 2354 2355 struct qdisc_dump_args { 2356 struct qdisc_walker w; 2357 struct sk_buff *skb; 2358 struct netlink_callback *cb; 2359 }; 2360 2361 static int qdisc_class_dump(struct Qdisc *q, unsigned long cl, 2362 struct qdisc_walker *arg) 2363 { 2364 struct qdisc_dump_args *a = (struct qdisc_dump_args *)arg; 2365 2366 return tc_fill_tclass(a->skb, q, cl, NETLINK_CB(a->cb->skb).portid, 2367 a->cb->nlh->nlmsg_seq, NLM_F_MULTI, 2368 RTM_NEWTCLASS, NULL); 2369 } 2370 2371 static int tc_dump_tclass_qdisc(struct Qdisc *q, struct sk_buff *skb, 2372 struct tcmsg *tcm, struct netlink_callback *cb, 2373 int *t_p, int s_t) 2374 { 2375 struct qdisc_dump_args arg; 2376 2377 if (tc_qdisc_dump_ignore(q, false, NULL) || 2378 *t_p < s_t || !q->ops->cl_ops || 2379 (tcm->tcm_parent && 2380 TC_H_MAJ(tcm->tcm_parent) != q->handle)) { 2381 (*t_p)++; 2382 return 0; 2383 } 2384 if (*t_p > s_t) 2385 memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0])); 2386 arg.w.fn = qdisc_class_dump; 2387 arg.skb = skb; 2388 arg.cb = cb; 2389 arg.w.stop = 0; 2390 arg.w.skip = cb->args[1]; 2391 arg.w.count = 0; 2392 q->ops->cl_ops->walk(q, &arg.w); 2393 cb->args[1] = arg.w.count; 2394 if (arg.w.stop) 2395 return -1; 2396 (*t_p)++; 2397 return 0; 2398 } 2399 2400 static int tc_dump_tclass_root(struct Qdisc *root, struct sk_buff *skb, 2401 struct tcmsg *tcm, struct netlink_callback *cb, 2402 int *t_p, int s_t, bool recur) 2403 { 2404 struct Qdisc *q; 2405 int b; 2406 2407 if (!root) 2408 return 0; 2409 2410 if (tc_dump_tclass_qdisc(root, skb, tcm, cb, t_p, s_t) < 0) 2411 return -1; 2412 2413 if (!qdisc_dev(root) || !recur) 2414 return 0; 2415 2416 if (tcm->tcm_parent) { 2417 q = qdisc_match_from_root(root, TC_H_MAJ(tcm->tcm_parent)); 2418 if (q && q != root && 2419 tc_dump_tclass_qdisc(q, skb, tcm, cb, t_p, s_t) < 0) 2420 return -1; 2421 return 0; 2422 } 2423 hash_for_each(qdisc_dev(root)->qdisc_hash, b, q, hash) { 2424 if (tc_dump_tclass_qdisc(q, skb, tcm, cb, t_p, s_t) < 0) 2425 return -1; 2426 } 2427 2428 return 0; 2429 } 2430 2431 static int __tc_dump_tclass(struct sk_buff *skb, struct netlink_callback *cb, 2432 struct tcmsg *tcm, struct net_device *dev) 2433 { 2434 struct netdev_queue *dev_queue; 2435 int t, s_t; 2436 2437 s_t = cb->args[0]; 2438 t = 0; 2439 2440 if (tc_dump_tclass_root(rtnl_dereference(dev->qdisc), 2441 skb, tcm, cb, &t, s_t, true) < 0) 2442 goto done; 2443 2444 dev_queue = dev_ingress_queue(dev); 2445 if (dev_queue && 2446 tc_dump_tclass_root(rtnl_dereference(dev_queue->qdisc_sleeping), 2447 skb, tcm, cb, &t, s_t, false) < 0) 2448 goto done; 2449 2450 done: 2451 cb->args[0] = t; 2452 2453 return skb->len; 2454 } 2455 2456 static int tc_dump_tclass(struct sk_buff *skb, struct netlink_callback *cb) 2457 { 2458 struct tcmsg *tcm = nlmsg_data(cb->nlh); 2459 struct net *net = sock_net(skb->sk); 2460 struct net_device *dev; 2461 int err; 2462 2463 if (nlmsg_len(cb->nlh) < sizeof(*tcm)) 2464 return 0; 2465 2466 dev = dev_get_by_index(net, tcm->tcm_ifindex); 2467 if (!dev) 2468 return 0; 2469 2470 netdev_lock_ops(dev); 2471 err = __tc_dump_tclass(skb, cb, tcm, dev); 2472 netdev_unlock_ops(dev); 2473 2474 dev_put(dev); 2475 2476 return err; 2477 } 2478 2479 #ifdef CONFIG_PROC_FS 2480 static int psched_show(struct seq_file *seq, void *v) 2481 { 2482 seq_printf(seq, "%08x %08x %08x %08x\n", 2483 (u32)NSEC_PER_USEC, (u32)PSCHED_TICKS2NS(1), 2484 1000000, 2485 (u32)NSEC_PER_SEC / hrtimer_resolution); 2486 2487 return 0; 2488 } 2489 2490 static int __net_init psched_net_init(struct net *net) 2491 { 2492 struct proc_dir_entry *e; 2493 2494 e = proc_create_single("psched", 0, net->proc_net, psched_show); 2495 if (e == NULL) 2496 return -ENOMEM; 2497 2498 return 0; 2499 } 2500 2501 static void __net_exit psched_net_exit(struct net *net) 2502 { 2503 remove_proc_entry("psched", net->proc_net); 2504 } 2505 #else 2506 static int __net_init psched_net_init(struct net *net) 2507 { 2508 return 0; 2509 } 2510 2511 static void __net_exit psched_net_exit(struct net *net) 2512 { 2513 } 2514 #endif 2515 2516 static struct pernet_operations psched_net_ops = { 2517 .init = psched_net_init, 2518 .exit = psched_net_exit, 2519 }; 2520 2521 #if IS_ENABLED(CONFIG_MITIGATION_RETPOLINE) 2522 DEFINE_STATIC_KEY_FALSE(tc_skip_wrapper_act); 2523 DEFINE_STATIC_KEY_FALSE(tc_skip_wrapper_cls); 2524 #endif 2525 2526 static const struct rtnl_msg_handler psched_rtnl_msg_handlers[] __initconst = { 2527 {.msgtype = RTM_NEWQDISC, .doit = tc_modify_qdisc}, 2528 {.msgtype = RTM_DELQDISC, .doit = tc_get_qdisc}, 2529 {.msgtype = RTM_GETQDISC, .doit = tc_get_qdisc, 2530 .dumpit = tc_dump_qdisc}, 2531 {.msgtype = RTM_NEWTCLASS, .doit = tc_ctl_tclass}, 2532 {.msgtype = RTM_DELTCLASS, .doit = tc_ctl_tclass}, 2533 {.msgtype = RTM_GETTCLASS, .doit = tc_ctl_tclass, 2534 .dumpit = tc_dump_tclass}, 2535 }; 2536 2537 static int __init pktsched_init(void) 2538 { 2539 int err; 2540 2541 err = register_pernet_subsys(&psched_net_ops); 2542 if (err) { 2543 pr_err("pktsched_init: " 2544 "cannot initialize per netns operations\n"); 2545 return err; 2546 } 2547 2548 register_qdisc(&pfifo_fast_ops); 2549 register_qdisc(&pfifo_qdisc_ops); 2550 register_qdisc(&bfifo_qdisc_ops); 2551 register_qdisc(&pfifo_head_drop_qdisc_ops); 2552 register_qdisc(&mq_qdisc_ops); 2553 register_qdisc(&noqueue_qdisc_ops); 2554 2555 rtnl_register_many(psched_rtnl_msg_handlers); 2556 2557 tc_wrapper_init(); 2558 2559 return 0; 2560 } 2561 2562 subsys_initcall(pktsched_init); 2563