1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __NET_SCHED_GENERIC_H 3 #define __NET_SCHED_GENERIC_H 4 5 #include <linux/netdevice.h> 6 #include <linux/types.h> 7 #include <linux/rcupdate.h> 8 #include <linux/pkt_sched.h> 9 #include <linux/pkt_cls.h> 10 #include <linux/percpu.h> 11 #include <linux/dynamic_queue_limits.h> 12 #include <linux/list.h> 13 #include <linux/refcount.h> 14 #include <linux/workqueue.h> 15 #include <net/gen_stats.h> 16 #include <net/rtnetlink.h> 17 18 struct Qdisc_ops; 19 struct qdisc_walker; 20 struct tcf_walker; 21 struct module; 22 23 struct qdisc_rate_table { 24 struct tc_ratespec rate; 25 u32 data[256]; 26 struct qdisc_rate_table *next; 27 int refcnt; 28 }; 29 30 enum qdisc_state_t { 31 __QDISC_STATE_SCHED, 32 __QDISC_STATE_DEACTIVATED, 33 }; 34 35 struct qdisc_size_table { 36 struct rcu_head rcu; 37 struct list_head list; 38 struct tc_sizespec szopts; 39 int refcnt; 40 u16 data[]; 41 }; 42 43 /* similar to sk_buff_head, but skb->prev pointer is undefined. */ 44 struct qdisc_skb_head { 45 struct sk_buff *head; 46 struct sk_buff *tail; 47 __u32 qlen; 48 spinlock_t lock; 49 }; 50 51 struct Qdisc { 52 int (*enqueue)(struct sk_buff *skb, 53 struct Qdisc *sch, 54 struct sk_buff **to_free); 55 struct sk_buff * (*dequeue)(struct Qdisc *sch); 56 unsigned int flags; 57 #define TCQ_F_BUILTIN 1 58 #define TCQ_F_INGRESS 2 59 #define TCQ_F_CAN_BYPASS 4 60 #define TCQ_F_MQROOT 8 61 #define TCQ_F_ONETXQUEUE 0x10 /* dequeue_skb() can assume all skbs are for 62 * q->dev_queue : It can test 63 * netif_xmit_frozen_or_stopped() before 64 * dequeueing next packet. 65 * Its true for MQ/MQPRIO slaves, or non 66 * multiqueue device. 67 */ 68 #define TCQ_F_WARN_NONWC (1 << 16) 69 #define TCQ_F_CPUSTATS 0x20 /* run using percpu statistics */ 70 #define TCQ_F_NOPARENT 0x40 /* root of its hierarchy : 71 * qdisc_tree_decrease_qlen() should stop. 72 */ 73 #define TCQ_F_INVISIBLE 0x80 /* invisible by default in dump */ 74 #define TCQ_F_NOLOCK 0x100 /* qdisc does not require locking */ 75 #define TCQ_F_OFFLOADED 0x200 /* qdisc is offloaded to HW */ 76 u32 limit; 77 const struct Qdisc_ops *ops; 78 struct qdisc_size_table __rcu *stab; 79 struct hlist_node hash; 80 u32 handle; 81 u32 parent; 82 83 struct netdev_queue *dev_queue; 84 85 struct net_rate_estimator __rcu *rate_est; 86 struct gnet_stats_basic_cpu __percpu *cpu_bstats; 87 struct gnet_stats_queue __percpu *cpu_qstats; 88 89 /* 90 * For performance sake on SMP, we put highly modified fields at the end 91 */ 92 struct sk_buff_head gso_skb ____cacheline_aligned_in_smp; 93 struct qdisc_skb_head q; 94 struct gnet_stats_basic_packed bstats; 95 seqcount_t running; 96 struct gnet_stats_queue qstats; 97 unsigned long state; 98 struct Qdisc *next_sched; 99 struct sk_buff_head skb_bad_txq; 100 int padded; 101 refcount_t refcnt; 102 103 spinlock_t busylock ____cacheline_aligned_in_smp; 104 }; 105 106 static inline void qdisc_refcount_inc(struct Qdisc *qdisc) 107 { 108 if (qdisc->flags & TCQ_F_BUILTIN) 109 return; 110 refcount_inc(&qdisc->refcnt); 111 } 112 113 static inline bool qdisc_is_running(const struct Qdisc *qdisc) 114 { 115 return (raw_read_seqcount(&qdisc->running) & 1) ? true : false; 116 } 117 118 static inline bool qdisc_run_begin(struct Qdisc *qdisc) 119 { 120 if (qdisc_is_running(qdisc)) 121 return false; 122 /* Variant of write_seqcount_begin() telling lockdep a trylock 123 * was attempted. 124 */ 125 raw_write_seqcount_begin(&qdisc->running); 126 seqcount_acquire(&qdisc->running.dep_map, 0, 1, _RET_IP_); 127 return true; 128 } 129 130 static inline void qdisc_run_end(struct Qdisc *qdisc) 131 { 132 write_seqcount_end(&qdisc->running); 133 } 134 135 static inline bool qdisc_may_bulk(const struct Qdisc *qdisc) 136 { 137 return qdisc->flags & TCQ_F_ONETXQUEUE; 138 } 139 140 static inline int qdisc_avail_bulklimit(const struct netdev_queue *txq) 141 { 142 #ifdef CONFIG_BQL 143 /* Non-BQL migrated drivers will return 0, too. */ 144 return dql_avail(&txq->dql); 145 #else 146 return 0; 147 #endif 148 } 149 150 struct Qdisc_class_ops { 151 /* Child qdisc manipulation */ 152 struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *); 153 int (*graft)(struct Qdisc *, unsigned long cl, 154 struct Qdisc *, struct Qdisc **, 155 struct netlink_ext_ack *extack); 156 struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl); 157 void (*qlen_notify)(struct Qdisc *, unsigned long); 158 159 /* Class manipulation routines */ 160 unsigned long (*find)(struct Qdisc *, u32 classid); 161 int (*change)(struct Qdisc *, u32, u32, 162 struct nlattr **, unsigned long *, 163 struct netlink_ext_ack *); 164 int (*delete)(struct Qdisc *, unsigned long); 165 void (*walk)(struct Qdisc *, struct qdisc_walker * arg); 166 167 /* Filter manipulation */ 168 struct tcf_block * (*tcf_block)(struct Qdisc *sch, 169 unsigned long arg, 170 struct netlink_ext_ack *extack); 171 unsigned long (*bind_tcf)(struct Qdisc *, unsigned long, 172 u32 classid); 173 void (*unbind_tcf)(struct Qdisc *, unsigned long); 174 175 /* rtnetlink specific */ 176 int (*dump)(struct Qdisc *, unsigned long, 177 struct sk_buff *skb, struct tcmsg*); 178 int (*dump_stats)(struct Qdisc *, unsigned long, 179 struct gnet_dump *); 180 }; 181 182 struct Qdisc_ops { 183 struct Qdisc_ops *next; 184 const struct Qdisc_class_ops *cl_ops; 185 char id[IFNAMSIZ]; 186 int priv_size; 187 unsigned int static_flags; 188 189 int (*enqueue)(struct sk_buff *skb, 190 struct Qdisc *sch, 191 struct sk_buff **to_free); 192 struct sk_buff * (*dequeue)(struct Qdisc *); 193 struct sk_buff * (*peek)(struct Qdisc *); 194 195 int (*init)(struct Qdisc *sch, struct nlattr *arg, 196 struct netlink_ext_ack *extack); 197 void (*reset)(struct Qdisc *); 198 void (*destroy)(struct Qdisc *); 199 int (*change)(struct Qdisc *sch, 200 struct nlattr *arg, 201 struct netlink_ext_ack *extack); 202 void (*attach)(struct Qdisc *sch); 203 204 int (*dump)(struct Qdisc *, struct sk_buff *); 205 int (*dump_stats)(struct Qdisc *, struct gnet_dump *); 206 207 void (*ingress_block_set)(struct Qdisc *sch, 208 u32 block_index); 209 void (*egress_block_set)(struct Qdisc *sch, 210 u32 block_index); 211 u32 (*ingress_block_get)(struct Qdisc *sch); 212 u32 (*egress_block_get)(struct Qdisc *sch); 213 214 struct module *owner; 215 }; 216 217 218 struct tcf_result { 219 union { 220 struct { 221 unsigned long class; 222 u32 classid; 223 }; 224 const struct tcf_proto *goto_tp; 225 }; 226 }; 227 228 struct tcf_proto_ops { 229 struct list_head head; 230 char kind[IFNAMSIZ]; 231 232 int (*classify)(struct sk_buff *, 233 const struct tcf_proto *, 234 struct tcf_result *); 235 int (*init)(struct tcf_proto*); 236 void (*destroy)(struct tcf_proto *tp, 237 struct netlink_ext_ack *extack); 238 239 void* (*get)(struct tcf_proto*, u32 handle); 240 int (*change)(struct net *net, struct sk_buff *, 241 struct tcf_proto*, unsigned long, 242 u32 handle, struct nlattr **, 243 void **, bool, 244 struct netlink_ext_ack *); 245 int (*delete)(struct tcf_proto *tp, void *arg, 246 bool *last, 247 struct netlink_ext_ack *); 248 void (*walk)(struct tcf_proto*, struct tcf_walker *arg); 249 void (*bind_class)(void *, u32, unsigned long); 250 251 /* rtnetlink specific */ 252 int (*dump)(struct net*, struct tcf_proto*, void *, 253 struct sk_buff *skb, struct tcmsg*); 254 255 struct module *owner; 256 }; 257 258 struct tcf_proto { 259 /* Fast access part */ 260 struct tcf_proto __rcu *next; 261 void __rcu *root; 262 int (*classify)(struct sk_buff *, 263 const struct tcf_proto *, 264 struct tcf_result *); 265 __be16 protocol; 266 267 /* All the rest */ 268 u32 prio; 269 void *data; 270 const struct tcf_proto_ops *ops; 271 struct tcf_chain *chain; 272 struct rcu_head rcu; 273 }; 274 275 struct qdisc_skb_cb { 276 unsigned int pkt_len; 277 u16 slave_dev_queue_mapping; 278 u16 tc_classid; 279 #define QDISC_CB_PRIV_LEN 20 280 unsigned char data[QDISC_CB_PRIV_LEN]; 281 }; 282 283 typedef void tcf_chain_head_change_t(struct tcf_proto *tp_head, void *priv); 284 285 struct tcf_chain { 286 struct tcf_proto __rcu *filter_chain; 287 struct list_head filter_chain_list; 288 struct list_head list; 289 struct tcf_block *block; 290 u32 index; /* chain index */ 291 unsigned int refcnt; 292 }; 293 294 struct tcf_block { 295 struct list_head chain_list; 296 u32 index; /* block index for shared blocks */ 297 unsigned int refcnt; 298 struct net *net; 299 struct Qdisc *q; 300 struct list_head cb_list; 301 struct list_head owner_list; 302 bool keep_dst; 303 unsigned int offloadcnt; /* Number of oddloaded filters */ 304 unsigned int nooffloaddevcnt; /* Number of devs unable to do offload */ 305 }; 306 307 static inline void tcf_block_offload_inc(struct tcf_block *block, u32 *flags) 308 { 309 if (*flags & TCA_CLS_FLAGS_IN_HW) 310 return; 311 *flags |= TCA_CLS_FLAGS_IN_HW; 312 block->offloadcnt++; 313 } 314 315 static inline void tcf_block_offload_dec(struct tcf_block *block, u32 *flags) 316 { 317 if (!(*flags & TCA_CLS_FLAGS_IN_HW)) 318 return; 319 *flags &= ~TCA_CLS_FLAGS_IN_HW; 320 block->offloadcnt--; 321 } 322 323 static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz) 324 { 325 struct qdisc_skb_cb *qcb; 326 327 BUILD_BUG_ON(sizeof(skb->cb) < offsetof(struct qdisc_skb_cb, data) + sz); 328 BUILD_BUG_ON(sizeof(qcb->data) < sz); 329 } 330 331 static inline int qdisc_qlen_cpu(const struct Qdisc *q) 332 { 333 return this_cpu_ptr(q->cpu_qstats)->qlen; 334 } 335 336 static inline int qdisc_qlen(const struct Qdisc *q) 337 { 338 return q->q.qlen; 339 } 340 341 static inline int qdisc_qlen_sum(const struct Qdisc *q) 342 { 343 __u32 qlen = 0; 344 int i; 345 346 if (q->flags & TCQ_F_NOLOCK) { 347 for_each_possible_cpu(i) 348 qlen += per_cpu_ptr(q->cpu_qstats, i)->qlen; 349 } else { 350 qlen = q->q.qlen; 351 } 352 353 return qlen; 354 } 355 356 static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb) 357 { 358 return (struct qdisc_skb_cb *)skb->cb; 359 } 360 361 static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc) 362 { 363 return &qdisc->q.lock; 364 } 365 366 static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc) 367 { 368 struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc); 369 370 return q; 371 } 372 373 static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc) 374 { 375 return qdisc->dev_queue->qdisc_sleeping; 376 } 377 378 /* The qdisc root lock is a mechanism by which to top level 379 * of a qdisc tree can be locked from any qdisc node in the 380 * forest. This allows changing the configuration of some 381 * aspect of the qdisc tree while blocking out asynchronous 382 * qdisc access in the packet processing paths. 383 * 384 * It is only legal to do this when the root will not change 385 * on us. Otherwise we'll potentially lock the wrong qdisc 386 * root. This is enforced by holding the RTNL semaphore, which 387 * all users of this lock accessor must do. 388 */ 389 static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc) 390 { 391 struct Qdisc *root = qdisc_root(qdisc); 392 393 ASSERT_RTNL(); 394 return qdisc_lock(root); 395 } 396 397 static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc) 398 { 399 struct Qdisc *root = qdisc_root_sleeping(qdisc); 400 401 ASSERT_RTNL(); 402 return qdisc_lock(root); 403 } 404 405 static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc) 406 { 407 struct Qdisc *root = qdisc_root_sleeping(qdisc); 408 409 ASSERT_RTNL(); 410 return &root->running; 411 } 412 413 static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc) 414 { 415 return qdisc->dev_queue->dev; 416 } 417 418 static inline void sch_tree_lock(const struct Qdisc *q) 419 { 420 spin_lock_bh(qdisc_root_sleeping_lock(q)); 421 } 422 423 static inline void sch_tree_unlock(const struct Qdisc *q) 424 { 425 spin_unlock_bh(qdisc_root_sleeping_lock(q)); 426 } 427 428 extern struct Qdisc noop_qdisc; 429 extern struct Qdisc_ops noop_qdisc_ops; 430 extern struct Qdisc_ops pfifo_fast_ops; 431 extern struct Qdisc_ops mq_qdisc_ops; 432 extern struct Qdisc_ops noqueue_qdisc_ops; 433 extern const struct Qdisc_ops *default_qdisc_ops; 434 static inline const struct Qdisc_ops * 435 get_default_qdisc_ops(const struct net_device *dev, int ntx) 436 { 437 return ntx < dev->real_num_tx_queues ? 438 default_qdisc_ops : &pfifo_fast_ops; 439 } 440 441 struct Qdisc_class_common { 442 u32 classid; 443 struct hlist_node hnode; 444 }; 445 446 struct Qdisc_class_hash { 447 struct hlist_head *hash; 448 unsigned int hashsize; 449 unsigned int hashmask; 450 unsigned int hashelems; 451 }; 452 453 static inline unsigned int qdisc_class_hash(u32 id, u32 mask) 454 { 455 id ^= id >> 8; 456 id ^= id >> 4; 457 return id & mask; 458 } 459 460 static inline struct Qdisc_class_common * 461 qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id) 462 { 463 struct Qdisc_class_common *cl; 464 unsigned int h; 465 466 if (!id) 467 return NULL; 468 469 h = qdisc_class_hash(id, hash->hashmask); 470 hlist_for_each_entry(cl, &hash->hash[h], hnode) { 471 if (cl->classid == id) 472 return cl; 473 } 474 return NULL; 475 } 476 477 static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid) 478 { 479 u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY; 480 481 return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL; 482 } 483 484 int qdisc_class_hash_init(struct Qdisc_class_hash *); 485 void qdisc_class_hash_insert(struct Qdisc_class_hash *, 486 struct Qdisc_class_common *); 487 void qdisc_class_hash_remove(struct Qdisc_class_hash *, 488 struct Qdisc_class_common *); 489 void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *); 490 void qdisc_class_hash_destroy(struct Qdisc_class_hash *); 491 492 void dev_init_scheduler(struct net_device *dev); 493 void dev_shutdown(struct net_device *dev); 494 void dev_activate(struct net_device *dev); 495 void dev_deactivate(struct net_device *dev); 496 void dev_deactivate_many(struct list_head *head); 497 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue, 498 struct Qdisc *qdisc); 499 void qdisc_reset(struct Qdisc *qdisc); 500 void qdisc_destroy(struct Qdisc *qdisc); 501 void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, unsigned int n, 502 unsigned int len); 503 struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue, 504 const struct Qdisc_ops *ops, 505 struct netlink_ext_ack *extack); 506 void qdisc_free(struct Qdisc *qdisc); 507 struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue, 508 const struct Qdisc_ops *ops, u32 parentid, 509 struct netlink_ext_ack *extack); 510 void __qdisc_calculate_pkt_len(struct sk_buff *skb, 511 const struct qdisc_size_table *stab); 512 int skb_do_redirect(struct sk_buff *); 513 514 static inline void skb_reset_tc(struct sk_buff *skb) 515 { 516 #ifdef CONFIG_NET_CLS_ACT 517 skb->tc_redirected = 0; 518 #endif 519 } 520 521 static inline bool skb_at_tc_ingress(const struct sk_buff *skb) 522 { 523 #ifdef CONFIG_NET_CLS_ACT 524 return skb->tc_at_ingress; 525 #else 526 return false; 527 #endif 528 } 529 530 static inline bool skb_skip_tc_classify(struct sk_buff *skb) 531 { 532 #ifdef CONFIG_NET_CLS_ACT 533 if (skb->tc_skip_classify) { 534 skb->tc_skip_classify = 0; 535 return true; 536 } 537 #endif 538 return false; 539 } 540 541 /* Reset all TX qdiscs greater then index of a device. */ 542 static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i) 543 { 544 struct Qdisc *qdisc; 545 546 for (; i < dev->num_tx_queues; i++) { 547 qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc); 548 if (qdisc) { 549 spin_lock_bh(qdisc_lock(qdisc)); 550 qdisc_reset(qdisc); 551 spin_unlock_bh(qdisc_lock(qdisc)); 552 } 553 } 554 } 555 556 static inline void qdisc_reset_all_tx(struct net_device *dev) 557 { 558 qdisc_reset_all_tx_gt(dev, 0); 559 } 560 561 /* Are all TX queues of the device empty? */ 562 static inline bool qdisc_all_tx_empty(const struct net_device *dev) 563 { 564 unsigned int i; 565 566 rcu_read_lock(); 567 for (i = 0; i < dev->num_tx_queues; i++) { 568 struct netdev_queue *txq = netdev_get_tx_queue(dev, i); 569 const struct Qdisc *q = rcu_dereference(txq->qdisc); 570 571 if (q->q.qlen) { 572 rcu_read_unlock(); 573 return false; 574 } 575 } 576 rcu_read_unlock(); 577 return true; 578 } 579 580 /* Are any of the TX qdiscs changing? */ 581 static inline bool qdisc_tx_changing(const struct net_device *dev) 582 { 583 unsigned int i; 584 585 for (i = 0; i < dev->num_tx_queues; i++) { 586 struct netdev_queue *txq = netdev_get_tx_queue(dev, i); 587 if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping) 588 return true; 589 } 590 return false; 591 } 592 593 /* Is the device using the noop qdisc on all queues? */ 594 static inline bool qdisc_tx_is_noop(const struct net_device *dev) 595 { 596 unsigned int i; 597 598 for (i = 0; i < dev->num_tx_queues; i++) { 599 struct netdev_queue *txq = netdev_get_tx_queue(dev, i); 600 if (rcu_access_pointer(txq->qdisc) != &noop_qdisc) 601 return false; 602 } 603 return true; 604 } 605 606 static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb) 607 { 608 return qdisc_skb_cb(skb)->pkt_len; 609 } 610 611 /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */ 612 enum net_xmit_qdisc_t { 613 __NET_XMIT_STOLEN = 0x00010000, 614 __NET_XMIT_BYPASS = 0x00020000, 615 }; 616 617 #ifdef CONFIG_NET_CLS_ACT 618 #define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1) 619 #else 620 #define net_xmit_drop_count(e) (1) 621 #endif 622 623 static inline void qdisc_calculate_pkt_len(struct sk_buff *skb, 624 const struct Qdisc *sch) 625 { 626 #ifdef CONFIG_NET_SCHED 627 struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab); 628 629 if (stab) 630 __qdisc_calculate_pkt_len(skb, stab); 631 #endif 632 } 633 634 static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch, 635 struct sk_buff **to_free) 636 { 637 qdisc_calculate_pkt_len(skb, sch); 638 return sch->enqueue(skb, sch, to_free); 639 } 640 641 static inline bool qdisc_is_percpu_stats(const struct Qdisc *q) 642 { 643 return q->flags & TCQ_F_CPUSTATS; 644 } 645 646 static inline void _bstats_update(struct gnet_stats_basic_packed *bstats, 647 __u64 bytes, __u32 packets) 648 { 649 bstats->bytes += bytes; 650 bstats->packets += packets; 651 } 652 653 static inline void bstats_update(struct gnet_stats_basic_packed *bstats, 654 const struct sk_buff *skb) 655 { 656 _bstats_update(bstats, 657 qdisc_pkt_len(skb), 658 skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1); 659 } 660 661 static inline void _bstats_cpu_update(struct gnet_stats_basic_cpu *bstats, 662 __u64 bytes, __u32 packets) 663 { 664 u64_stats_update_begin(&bstats->syncp); 665 _bstats_update(&bstats->bstats, bytes, packets); 666 u64_stats_update_end(&bstats->syncp); 667 } 668 669 static inline void bstats_cpu_update(struct gnet_stats_basic_cpu *bstats, 670 const struct sk_buff *skb) 671 { 672 u64_stats_update_begin(&bstats->syncp); 673 bstats_update(&bstats->bstats, skb); 674 u64_stats_update_end(&bstats->syncp); 675 } 676 677 static inline void qdisc_bstats_cpu_update(struct Qdisc *sch, 678 const struct sk_buff *skb) 679 { 680 bstats_cpu_update(this_cpu_ptr(sch->cpu_bstats), skb); 681 } 682 683 static inline void qdisc_bstats_update(struct Qdisc *sch, 684 const struct sk_buff *skb) 685 { 686 bstats_update(&sch->bstats, skb); 687 } 688 689 static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch, 690 const struct sk_buff *skb) 691 { 692 sch->qstats.backlog -= qdisc_pkt_len(skb); 693 } 694 695 static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch, 696 const struct sk_buff *skb) 697 { 698 this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb)); 699 } 700 701 static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch, 702 const struct sk_buff *skb) 703 { 704 sch->qstats.backlog += qdisc_pkt_len(skb); 705 } 706 707 static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch, 708 const struct sk_buff *skb) 709 { 710 this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb)); 711 } 712 713 static inline void qdisc_qstats_cpu_qlen_inc(struct Qdisc *sch) 714 { 715 this_cpu_inc(sch->cpu_qstats->qlen); 716 } 717 718 static inline void qdisc_qstats_cpu_qlen_dec(struct Qdisc *sch) 719 { 720 this_cpu_dec(sch->cpu_qstats->qlen); 721 } 722 723 static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch) 724 { 725 this_cpu_inc(sch->cpu_qstats->requeues); 726 } 727 728 static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count) 729 { 730 sch->qstats.drops += count; 731 } 732 733 static inline void qstats_drop_inc(struct gnet_stats_queue *qstats) 734 { 735 qstats->drops++; 736 } 737 738 static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats) 739 { 740 qstats->overlimits++; 741 } 742 743 static inline void qdisc_qstats_drop(struct Qdisc *sch) 744 { 745 qstats_drop_inc(&sch->qstats); 746 } 747 748 static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch) 749 { 750 this_cpu_inc(sch->cpu_qstats->drops); 751 } 752 753 static inline void qdisc_qstats_overlimit(struct Qdisc *sch) 754 { 755 sch->qstats.overlimits++; 756 } 757 758 static inline void qdisc_skb_head_init(struct qdisc_skb_head *qh) 759 { 760 qh->head = NULL; 761 qh->tail = NULL; 762 qh->qlen = 0; 763 } 764 765 static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch, 766 struct qdisc_skb_head *qh) 767 { 768 struct sk_buff *last = qh->tail; 769 770 if (last) { 771 skb->next = NULL; 772 last->next = skb; 773 qh->tail = skb; 774 } else { 775 qh->tail = skb; 776 qh->head = skb; 777 } 778 qh->qlen++; 779 qdisc_qstats_backlog_inc(sch, skb); 780 781 return NET_XMIT_SUCCESS; 782 } 783 784 static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch) 785 { 786 return __qdisc_enqueue_tail(skb, sch, &sch->q); 787 } 788 789 static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh) 790 { 791 struct sk_buff *skb = qh->head; 792 793 if (likely(skb != NULL)) { 794 qh->head = skb->next; 795 qh->qlen--; 796 if (qh->head == NULL) 797 qh->tail = NULL; 798 skb->next = NULL; 799 } 800 801 return skb; 802 } 803 804 static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch) 805 { 806 struct sk_buff *skb = __qdisc_dequeue_head(&sch->q); 807 808 if (likely(skb != NULL)) { 809 qdisc_qstats_backlog_dec(sch, skb); 810 qdisc_bstats_update(sch, skb); 811 } 812 813 return skb; 814 } 815 816 /* Instead of calling kfree_skb() while root qdisc lock is held, 817 * queue the skb for future freeing at end of __dev_xmit_skb() 818 */ 819 static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free) 820 { 821 skb->next = *to_free; 822 *to_free = skb; 823 } 824 825 static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch, 826 struct qdisc_skb_head *qh, 827 struct sk_buff **to_free) 828 { 829 struct sk_buff *skb = __qdisc_dequeue_head(qh); 830 831 if (likely(skb != NULL)) { 832 unsigned int len = qdisc_pkt_len(skb); 833 834 qdisc_qstats_backlog_dec(sch, skb); 835 __qdisc_drop(skb, to_free); 836 return len; 837 } 838 839 return 0; 840 } 841 842 static inline unsigned int qdisc_queue_drop_head(struct Qdisc *sch, 843 struct sk_buff **to_free) 844 { 845 return __qdisc_queue_drop_head(sch, &sch->q, to_free); 846 } 847 848 static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch) 849 { 850 const struct qdisc_skb_head *qh = &sch->q; 851 852 return qh->head; 853 } 854 855 /* generic pseudo peek method for non-work-conserving qdisc */ 856 static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch) 857 { 858 struct sk_buff *skb = skb_peek(&sch->gso_skb); 859 860 /* we can reuse ->gso_skb because peek isn't called for root qdiscs */ 861 if (!skb) { 862 skb = sch->dequeue(sch); 863 864 if (skb) { 865 __skb_queue_head(&sch->gso_skb, skb); 866 /* it's still part of the queue */ 867 qdisc_qstats_backlog_inc(sch, skb); 868 sch->q.qlen++; 869 } 870 } 871 872 return skb; 873 } 874 875 /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */ 876 static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch) 877 { 878 struct sk_buff *skb = skb_peek(&sch->gso_skb); 879 880 if (skb) { 881 skb = __skb_dequeue(&sch->gso_skb); 882 qdisc_qstats_backlog_dec(sch, skb); 883 sch->q.qlen--; 884 } else { 885 skb = sch->dequeue(sch); 886 } 887 888 return skb; 889 } 890 891 static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh) 892 { 893 /* 894 * We do not know the backlog in bytes of this list, it 895 * is up to the caller to correct it 896 */ 897 ASSERT_RTNL(); 898 if (qh->qlen) { 899 rtnl_kfree_skbs(qh->head, qh->tail); 900 901 qh->head = NULL; 902 qh->tail = NULL; 903 qh->qlen = 0; 904 } 905 } 906 907 static inline void qdisc_reset_queue(struct Qdisc *sch) 908 { 909 __qdisc_reset_queue(&sch->q); 910 sch->qstats.backlog = 0; 911 } 912 913 static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new, 914 struct Qdisc **pold) 915 { 916 struct Qdisc *old; 917 918 sch_tree_lock(sch); 919 old = *pold; 920 *pold = new; 921 if (old != NULL) { 922 unsigned int qlen = old->q.qlen; 923 unsigned int backlog = old->qstats.backlog; 924 925 qdisc_reset(old); 926 qdisc_tree_reduce_backlog(old, qlen, backlog); 927 } 928 sch_tree_unlock(sch); 929 930 return old; 931 } 932 933 static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch) 934 { 935 rtnl_kfree_skbs(skb, skb); 936 qdisc_qstats_drop(sch); 937 } 938 939 static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch, 940 struct sk_buff **to_free) 941 { 942 __qdisc_drop(skb, to_free); 943 qdisc_qstats_cpu_drop(sch); 944 945 return NET_XMIT_DROP; 946 } 947 948 static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch, 949 struct sk_buff **to_free) 950 { 951 __qdisc_drop(skb, to_free); 952 qdisc_qstats_drop(sch); 953 954 return NET_XMIT_DROP; 955 } 956 957 /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how 958 long it will take to send a packet given its size. 959 */ 960 static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen) 961 { 962 int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead; 963 if (slot < 0) 964 slot = 0; 965 slot >>= rtab->rate.cell_log; 966 if (slot > 255) 967 return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF]; 968 return rtab->data[slot]; 969 } 970 971 struct psched_ratecfg { 972 u64 rate_bytes_ps; /* bytes per second */ 973 u32 mult; 974 u16 overhead; 975 u8 linklayer; 976 u8 shift; 977 }; 978 979 static inline u64 psched_l2t_ns(const struct psched_ratecfg *r, 980 unsigned int len) 981 { 982 len += r->overhead; 983 984 if (unlikely(r->linklayer == TC_LINKLAYER_ATM)) 985 return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift; 986 987 return ((u64)len * r->mult) >> r->shift; 988 } 989 990 void psched_ratecfg_precompute(struct psched_ratecfg *r, 991 const struct tc_ratespec *conf, 992 u64 rate64); 993 994 static inline void psched_ratecfg_getrate(struct tc_ratespec *res, 995 const struct psched_ratecfg *r) 996 { 997 memset(res, 0, sizeof(*res)); 998 999 /* legacy struct tc_ratespec has a 32bit @rate field 1000 * Qdisc using 64bit rate should add new attributes 1001 * in order to maintain compatibility. 1002 */ 1003 res->rate = min_t(u64, r->rate_bytes_ps, ~0U); 1004 1005 res->overhead = r->overhead; 1006 res->linklayer = (r->linklayer & TC_LINKLAYER_MASK); 1007 } 1008 1009 /* Mini Qdisc serves for specific needs of ingress/clsact Qdisc. 1010 * The fast path only needs to access filter list and to update stats 1011 */ 1012 struct mini_Qdisc { 1013 struct tcf_proto *filter_list; 1014 struct gnet_stats_basic_cpu __percpu *cpu_bstats; 1015 struct gnet_stats_queue __percpu *cpu_qstats; 1016 struct rcu_head rcu; 1017 }; 1018 1019 static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq, 1020 const struct sk_buff *skb) 1021 { 1022 bstats_cpu_update(this_cpu_ptr(miniq->cpu_bstats), skb); 1023 } 1024 1025 static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq) 1026 { 1027 this_cpu_inc(miniq->cpu_qstats->drops); 1028 } 1029 1030 struct mini_Qdisc_pair { 1031 struct mini_Qdisc miniq1; 1032 struct mini_Qdisc miniq2; 1033 struct mini_Qdisc __rcu **p_miniq; 1034 }; 1035 1036 void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp, 1037 struct tcf_proto *tp_head); 1038 void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc, 1039 struct mini_Qdisc __rcu **p_miniq); 1040 1041 #endif 1042