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