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