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