sch_generic.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __NET_SCHED_GENERIC_H
  3. #define __NET_SCHED_GENERIC_H
  4. #include <linux/netdevice.h>
  5. #include <linux/types.h>
  6. #include <linux/rcupdate.h>
  7. #include <linux/pkt_sched.h>
  8. #include <linux/pkt_cls.h>
  9. #include <linux/percpu.h>
  10. #include <linux/dynamic_queue_limits.h>
  11. #include <linux/list.h>
  12. #include <linux/refcount.h>
  13. #include <linux/workqueue.h>
  14. #include <net/gen_stats.h>
  15. #include <net/rtnetlink.h>
  16. struct Qdisc_ops;
  17. struct qdisc_walker;
  18. struct tcf_walker;
  19. struct module;
  20. struct qdisc_rate_table {
  21. struct tc_ratespec rate;
  22. u32 data[256];
  23. struct qdisc_rate_table *next;
  24. int refcnt;
  25. };
  26. enum qdisc_state_t {
  27. __QDISC_STATE_SCHED,
  28. __QDISC_STATE_DEACTIVATED,
  29. };
  30. struct qdisc_size_table {
  31. struct rcu_head rcu;
  32. struct list_head list;
  33. struct tc_sizespec szopts;
  34. int refcnt;
  35. u16 data[];
  36. };
  37. /* similar to sk_buff_head, but skb->prev pointer is undefined. */
  38. struct qdisc_skb_head {
  39. struct sk_buff *head;
  40. struct sk_buff *tail;
  41. __u32 qlen;
  42. spinlock_t lock;
  43. };
  44. struct Qdisc {
  45. int (*enqueue)(struct sk_buff *skb,
  46. struct Qdisc *sch,
  47. struct sk_buff **to_free);
  48. struct sk_buff * (*dequeue)(struct Qdisc *sch);
  49. unsigned int flags;
  50. #define TCQ_F_BUILTIN 1
  51. #define TCQ_F_INGRESS 2
  52. #define TCQ_F_CAN_BYPASS 4
  53. #define TCQ_F_MQROOT 8
  54. #define TCQ_F_ONETXQUEUE 0x10 /* dequeue_skb() can assume all skbs are for
  55. * q->dev_queue : It can test
  56. * netif_xmit_frozen_or_stopped() before
  57. * dequeueing next packet.
  58. * Its true for MQ/MQPRIO slaves, or non
  59. * multiqueue device.
  60. */
  61. #define TCQ_F_WARN_NONWC (1 << 16)
  62. #define TCQ_F_CPUSTATS 0x20 /* run using percpu statistics */
  63. #define TCQ_F_NOPARENT 0x40 /* root of its hierarchy :
  64. * qdisc_tree_decrease_qlen() should stop.
  65. */
  66. #define TCQ_F_INVISIBLE 0x80 /* invisible by default in dump */
  67. u32 limit;
  68. const struct Qdisc_ops *ops;
  69. struct qdisc_size_table __rcu *stab;
  70. struct hlist_node hash;
  71. u32 handle;
  72. u32 parent;
  73. struct netdev_queue *dev_queue;
  74. struct net_rate_estimator __rcu *rate_est;
  75. struct gnet_stats_basic_cpu __percpu *cpu_bstats;
  76. struct gnet_stats_queue __percpu *cpu_qstats;
  77. /*
  78. * For performance sake on SMP, we put highly modified fields at the end
  79. */
  80. struct sk_buff *gso_skb ____cacheline_aligned_in_smp;
  81. struct qdisc_skb_head q;
  82. struct gnet_stats_basic_packed bstats;
  83. seqcount_t running;
  84. struct gnet_stats_queue qstats;
  85. unsigned long state;
  86. struct Qdisc *next_sched;
  87. struct sk_buff *skb_bad_txq;
  88. struct rcu_head rcu_head;
  89. int padded;
  90. refcount_t refcnt;
  91. spinlock_t busylock ____cacheline_aligned_in_smp;
  92. };
  93. static inline void qdisc_refcount_inc(struct Qdisc *qdisc)
  94. {
  95. if (qdisc->flags & TCQ_F_BUILTIN)
  96. return;
  97. refcount_inc(&qdisc->refcnt);
  98. }
  99. static inline bool qdisc_is_running(const struct Qdisc *qdisc)
  100. {
  101. return (raw_read_seqcount(&qdisc->running) & 1) ? true : false;
  102. }
  103. static inline bool qdisc_run_begin(struct Qdisc *qdisc)
  104. {
  105. if (qdisc_is_running(qdisc))
  106. return false;
  107. /* Variant of write_seqcount_begin() telling lockdep a trylock
  108. * was attempted.
  109. */
  110. raw_write_seqcount_begin(&qdisc->running);
  111. seqcount_acquire(&qdisc->running.dep_map, 0, 1, _RET_IP_);
  112. return true;
  113. }
  114. static inline void qdisc_run_end(struct Qdisc *qdisc)
  115. {
  116. write_seqcount_end(&qdisc->running);
  117. }
  118. static inline bool qdisc_may_bulk(const struct Qdisc *qdisc)
  119. {
  120. return qdisc->flags & TCQ_F_ONETXQUEUE;
  121. }
  122. static inline int qdisc_avail_bulklimit(const struct netdev_queue *txq)
  123. {
  124. #ifdef CONFIG_BQL
  125. /* Non-BQL migrated drivers will return 0, too. */
  126. return dql_avail(&txq->dql);
  127. #else
  128. return 0;
  129. #endif
  130. }
  131. struct Qdisc_class_ops {
  132. /* Child qdisc manipulation */
  133. struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *);
  134. int (*graft)(struct Qdisc *, unsigned long cl,
  135. struct Qdisc *, struct Qdisc **);
  136. struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
  137. void (*qlen_notify)(struct Qdisc *, unsigned long);
  138. /* Class manipulation routines */
  139. unsigned long (*find)(struct Qdisc *, u32 classid);
  140. int (*change)(struct Qdisc *, u32, u32,
  141. struct nlattr **, unsigned long *);
  142. int (*delete)(struct Qdisc *, unsigned long);
  143. void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
  144. /* Filter manipulation */
  145. struct tcf_block * (*tcf_block)(struct Qdisc *, unsigned long);
  146. unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
  147. u32 classid);
  148. void (*unbind_tcf)(struct Qdisc *, unsigned long);
  149. /* rtnetlink specific */
  150. int (*dump)(struct Qdisc *, unsigned long,
  151. struct sk_buff *skb, struct tcmsg*);
  152. int (*dump_stats)(struct Qdisc *, unsigned long,
  153. struct gnet_dump *);
  154. };
  155. struct Qdisc_ops {
  156. struct Qdisc_ops *next;
  157. const struct Qdisc_class_ops *cl_ops;
  158. char id[IFNAMSIZ];
  159. int priv_size;
  160. int (*enqueue)(struct sk_buff *skb,
  161. struct Qdisc *sch,
  162. struct sk_buff **to_free);
  163. struct sk_buff * (*dequeue)(struct Qdisc *);
  164. struct sk_buff * (*peek)(struct Qdisc *);
  165. int (*init)(struct Qdisc *, struct nlattr *arg);
  166. void (*reset)(struct Qdisc *);
  167. void (*destroy)(struct Qdisc *);
  168. int (*change)(struct Qdisc *, struct nlattr *arg);
  169. void (*attach)(struct Qdisc *);
  170. int (*dump)(struct Qdisc *, struct sk_buff *);
  171. int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
  172. struct module *owner;
  173. };
  174. struct tcf_result {
  175. union {
  176. struct {
  177. unsigned long class;
  178. u32 classid;
  179. };
  180. const struct tcf_proto *goto_tp;
  181. };
  182. };
  183. struct tcf_proto_ops {
  184. struct list_head head;
  185. char kind[IFNAMSIZ];
  186. int (*classify)(struct sk_buff *,
  187. const struct tcf_proto *,
  188. struct tcf_result *);
  189. int (*init)(struct tcf_proto*);
  190. void (*destroy)(struct tcf_proto*);
  191. void* (*get)(struct tcf_proto*, u32 handle);
  192. int (*change)(struct net *net, struct sk_buff *,
  193. struct tcf_proto*, unsigned long,
  194. u32 handle, struct nlattr **,
  195. void **, bool);
  196. int (*delete)(struct tcf_proto*, void *, bool*);
  197. void (*walk)(struct tcf_proto*, struct tcf_walker *arg);
  198. void (*bind_class)(void *, u32, unsigned long);
  199. /* rtnetlink specific */
  200. int (*dump)(struct net*, struct tcf_proto*, void *,
  201. struct sk_buff *skb, struct tcmsg*);
  202. struct module *owner;
  203. };
  204. struct tcf_proto {
  205. /* Fast access part */
  206. struct tcf_proto __rcu *next;
  207. void __rcu *root;
  208. int (*classify)(struct sk_buff *,
  209. const struct tcf_proto *,
  210. struct tcf_result *);
  211. __be16 protocol;
  212. /* All the rest */
  213. u32 prio;
  214. u32 classid;
  215. struct Qdisc *q;
  216. void *data;
  217. const struct tcf_proto_ops *ops;
  218. struct tcf_chain *chain;
  219. struct rcu_head rcu;
  220. };
  221. struct qdisc_skb_cb {
  222. unsigned int pkt_len;
  223. u16 slave_dev_queue_mapping;
  224. u16 tc_classid;
  225. #define QDISC_CB_PRIV_LEN 20
  226. unsigned char data[QDISC_CB_PRIV_LEN];
  227. };
  228. struct tcf_chain {
  229. struct tcf_proto __rcu *filter_chain;
  230. struct tcf_proto __rcu **p_filter_chain;
  231. struct list_head list;
  232. struct tcf_block *block;
  233. u32 index; /* chain index */
  234. unsigned int refcnt;
  235. };
  236. struct tcf_block {
  237. struct list_head chain_list;
  238. };
  239. static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
  240. {
  241. struct qdisc_skb_cb *qcb;
  242. BUILD_BUG_ON(sizeof(skb->cb) < offsetof(struct qdisc_skb_cb, data) + sz);
  243. BUILD_BUG_ON(sizeof(qcb->data) < sz);
  244. }
  245. static inline int qdisc_qlen(const struct Qdisc *q)
  246. {
  247. return q->q.qlen;
  248. }
  249. static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
  250. {
  251. return (struct qdisc_skb_cb *)skb->cb;
  252. }
  253. static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
  254. {
  255. return &qdisc->q.lock;
  256. }
  257. static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc)
  258. {
  259. struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc);
  260. return q;
  261. }
  262. static inline struct Qdisc *qdisc_root_bh(const struct Qdisc *qdisc)
  263. {
  264. return rcu_dereference_bh(qdisc->dev_queue->qdisc);
  265. }
  266. static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc)
  267. {
  268. return qdisc->dev_queue->qdisc_sleeping;
  269. }
  270. /* The qdisc root lock is a mechanism by which to top level
  271. * of a qdisc tree can be locked from any qdisc node in the
  272. * forest. This allows changing the configuration of some
  273. * aspect of the qdisc tree while blocking out asynchronous
  274. * qdisc access in the packet processing paths.
  275. *
  276. * It is only legal to do this when the root will not change
  277. * on us. Otherwise we'll potentially lock the wrong qdisc
  278. * root. This is enforced by holding the RTNL semaphore, which
  279. * all users of this lock accessor must do.
  280. */
  281. static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc)
  282. {
  283. struct Qdisc *root = qdisc_root(qdisc);
  284. ASSERT_RTNL();
  285. return qdisc_lock(root);
  286. }
  287. static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
  288. {
  289. struct Qdisc *root = qdisc_root_sleeping(qdisc);
  290. ASSERT_RTNL();
  291. return qdisc_lock(root);
  292. }
  293. static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc)
  294. {
  295. struct Qdisc *root = qdisc_root_sleeping(qdisc);
  296. ASSERT_RTNL();
  297. return &root->running;
  298. }
  299. static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
  300. {
  301. return qdisc->dev_queue->dev;
  302. }
  303. static inline void sch_tree_lock(const struct Qdisc *q)
  304. {
  305. spin_lock_bh(qdisc_root_sleeping_lock(q));
  306. }
  307. static inline void sch_tree_unlock(const struct Qdisc *q)
  308. {
  309. spin_unlock_bh(qdisc_root_sleeping_lock(q));
  310. }
  311. #define tcf_tree_lock(tp) sch_tree_lock((tp)->q)
  312. #define tcf_tree_unlock(tp) sch_tree_unlock((tp)->q)
  313. extern struct Qdisc noop_qdisc;
  314. extern struct Qdisc_ops noop_qdisc_ops;
  315. extern struct Qdisc_ops pfifo_fast_ops;
  316. extern struct Qdisc_ops mq_qdisc_ops;
  317. extern struct Qdisc_ops noqueue_qdisc_ops;
  318. extern const struct Qdisc_ops *default_qdisc_ops;
  319. static inline const struct Qdisc_ops *
  320. get_default_qdisc_ops(const struct net_device *dev, int ntx)
  321. {
  322. return ntx < dev->real_num_tx_queues ?
  323. default_qdisc_ops : &pfifo_fast_ops;
  324. }
  325. struct Qdisc_class_common {
  326. u32 classid;
  327. struct hlist_node hnode;
  328. };
  329. struct Qdisc_class_hash {
  330. struct hlist_head *hash;
  331. unsigned int hashsize;
  332. unsigned int hashmask;
  333. unsigned int hashelems;
  334. };
  335. static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
  336. {
  337. id ^= id >> 8;
  338. id ^= id >> 4;
  339. return id & mask;
  340. }
  341. static inline struct Qdisc_class_common *
  342. qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id)
  343. {
  344. struct Qdisc_class_common *cl;
  345. unsigned int h;
  346. if (!id)
  347. return NULL;
  348. h = qdisc_class_hash(id, hash->hashmask);
  349. hlist_for_each_entry(cl, &hash->hash[h], hnode) {
  350. if (cl->classid == id)
  351. return cl;
  352. }
  353. return NULL;
  354. }
  355. int qdisc_class_hash_init(struct Qdisc_class_hash *);
  356. void qdisc_class_hash_insert(struct Qdisc_class_hash *,
  357. struct Qdisc_class_common *);
  358. void qdisc_class_hash_remove(struct Qdisc_class_hash *,
  359. struct Qdisc_class_common *);
  360. void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
  361. void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
  362. void dev_init_scheduler(struct net_device *dev);
  363. void dev_shutdown(struct net_device *dev);
  364. void dev_activate(struct net_device *dev);
  365. void dev_deactivate(struct net_device *dev);
  366. void dev_deactivate_many(struct list_head *head);
  367. struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
  368. struct Qdisc *qdisc);
  369. void qdisc_reset(struct Qdisc *qdisc);
  370. void qdisc_destroy(struct Qdisc *qdisc);
  371. void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, unsigned int n,
  372. unsigned int len);
  373. struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
  374. const struct Qdisc_ops *ops);
  375. struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
  376. const struct Qdisc_ops *ops, u32 parentid);
  377. void __qdisc_calculate_pkt_len(struct sk_buff *skb,
  378. const struct qdisc_size_table *stab);
  379. int skb_do_redirect(struct sk_buff *);
  380. static inline void skb_reset_tc(struct sk_buff *skb)
  381. {
  382. #ifdef CONFIG_NET_CLS_ACT
  383. skb->tc_redirected = 0;
  384. #endif
  385. }
  386. static inline bool skb_at_tc_ingress(const struct sk_buff *skb)
  387. {
  388. #ifdef CONFIG_NET_CLS_ACT
  389. return skb->tc_at_ingress;
  390. #else
  391. return false;
  392. #endif
  393. }
  394. static inline bool skb_skip_tc_classify(struct sk_buff *skb)
  395. {
  396. #ifdef CONFIG_NET_CLS_ACT
  397. if (skb->tc_skip_classify) {
  398. skb->tc_skip_classify = 0;
  399. return true;
  400. }
  401. #endif
  402. return false;
  403. }
  404. /* Reset all TX qdiscs greater then index of a device. */
  405. static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
  406. {
  407. struct Qdisc *qdisc;
  408. for (; i < dev->num_tx_queues; i++) {
  409. qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
  410. if (qdisc) {
  411. spin_lock_bh(qdisc_lock(qdisc));
  412. qdisc_reset(qdisc);
  413. spin_unlock_bh(qdisc_lock(qdisc));
  414. }
  415. }
  416. }
  417. static inline void qdisc_reset_all_tx(struct net_device *dev)
  418. {
  419. qdisc_reset_all_tx_gt(dev, 0);
  420. }
  421. /* Are all TX queues of the device empty? */
  422. static inline bool qdisc_all_tx_empty(const struct net_device *dev)
  423. {
  424. unsigned int i;
  425. rcu_read_lock();
  426. for (i = 0; i < dev->num_tx_queues; i++) {
  427. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  428. const struct Qdisc *q = rcu_dereference(txq->qdisc);
  429. if (q->q.qlen) {
  430. rcu_read_unlock();
  431. return false;
  432. }
  433. }
  434. rcu_read_unlock();
  435. return true;
  436. }
  437. /* Are any of the TX qdiscs changing? */
  438. static inline bool qdisc_tx_changing(const struct net_device *dev)
  439. {
  440. unsigned int i;
  441. for (i = 0; i < dev->num_tx_queues; i++) {
  442. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  443. if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping)
  444. return true;
  445. }
  446. return false;
  447. }
  448. /* Is the device using the noop qdisc on all queues? */
  449. static inline bool qdisc_tx_is_noop(const struct net_device *dev)
  450. {
  451. unsigned int i;
  452. for (i = 0; i < dev->num_tx_queues; i++) {
  453. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  454. if (rcu_access_pointer(txq->qdisc) != &noop_qdisc)
  455. return false;
  456. }
  457. return true;
  458. }
  459. static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
  460. {
  461. return qdisc_skb_cb(skb)->pkt_len;
  462. }
  463. /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
  464. enum net_xmit_qdisc_t {
  465. __NET_XMIT_STOLEN = 0x00010000,
  466. __NET_XMIT_BYPASS = 0x00020000,
  467. };
  468. #ifdef CONFIG_NET_CLS_ACT
  469. #define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
  470. #else
  471. #define net_xmit_drop_count(e) (1)
  472. #endif
  473. static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
  474. const struct Qdisc *sch)
  475. {
  476. #ifdef CONFIG_NET_SCHED
  477. struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
  478. if (stab)
  479. __qdisc_calculate_pkt_len(skb, stab);
  480. #endif
  481. }
  482. static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  483. struct sk_buff **to_free)
  484. {
  485. qdisc_calculate_pkt_len(skb, sch);
  486. return sch->enqueue(skb, sch, to_free);
  487. }
  488. static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
  489. {
  490. return q->flags & TCQ_F_CPUSTATS;
  491. }
  492. static inline void _bstats_update(struct gnet_stats_basic_packed *bstats,
  493. __u64 bytes, __u32 packets)
  494. {
  495. bstats->bytes += bytes;
  496. bstats->packets += packets;
  497. }
  498. static inline void bstats_update(struct gnet_stats_basic_packed *bstats,
  499. const struct sk_buff *skb)
  500. {
  501. _bstats_update(bstats,
  502. qdisc_pkt_len(skb),
  503. skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1);
  504. }
  505. static inline void _bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
  506. __u64 bytes, __u32 packets)
  507. {
  508. u64_stats_update_begin(&bstats->syncp);
  509. _bstats_update(&bstats->bstats, bytes, packets);
  510. u64_stats_update_end(&bstats->syncp);
  511. }
  512. static inline void bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
  513. const struct sk_buff *skb)
  514. {
  515. u64_stats_update_begin(&bstats->syncp);
  516. bstats_update(&bstats->bstats, skb);
  517. u64_stats_update_end(&bstats->syncp);
  518. }
  519. static inline void qdisc_bstats_cpu_update(struct Qdisc *sch,
  520. const struct sk_buff *skb)
  521. {
  522. bstats_cpu_update(this_cpu_ptr(sch->cpu_bstats), skb);
  523. }
  524. static inline void qdisc_bstats_update(struct Qdisc *sch,
  525. const struct sk_buff *skb)
  526. {
  527. bstats_update(&sch->bstats, skb);
  528. }
  529. static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
  530. const struct sk_buff *skb)
  531. {
  532. sch->qstats.backlog -= qdisc_pkt_len(skb);
  533. }
  534. static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch,
  535. const struct sk_buff *skb)
  536. {
  537. sch->qstats.backlog += qdisc_pkt_len(skb);
  538. }
  539. static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count)
  540. {
  541. sch->qstats.drops += count;
  542. }
  543. static inline void qstats_drop_inc(struct gnet_stats_queue *qstats)
  544. {
  545. qstats->drops++;
  546. }
  547. static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats)
  548. {
  549. qstats->overlimits++;
  550. }
  551. static inline void qdisc_qstats_drop(struct Qdisc *sch)
  552. {
  553. qstats_drop_inc(&sch->qstats);
  554. }
  555. static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch)
  556. {
  557. this_cpu_inc(sch->cpu_qstats->drops);
  558. }
  559. static inline void qdisc_qstats_overlimit(struct Qdisc *sch)
  560. {
  561. sch->qstats.overlimits++;
  562. }
  563. static inline void qdisc_skb_head_init(struct qdisc_skb_head *qh)
  564. {
  565. qh->head = NULL;
  566. qh->tail = NULL;
  567. qh->qlen = 0;
  568. }
  569. static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
  570. struct qdisc_skb_head *qh)
  571. {
  572. struct sk_buff *last = qh->tail;
  573. if (last) {
  574. skb->next = NULL;
  575. last->next = skb;
  576. qh->tail = skb;
  577. } else {
  578. qh->tail = skb;
  579. qh->head = skb;
  580. }
  581. qh->qlen++;
  582. qdisc_qstats_backlog_inc(sch, skb);
  583. return NET_XMIT_SUCCESS;
  584. }
  585. static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
  586. {
  587. return __qdisc_enqueue_tail(skb, sch, &sch->q);
  588. }
  589. static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh)
  590. {
  591. struct sk_buff *skb = qh->head;
  592. if (likely(skb != NULL)) {
  593. qh->head = skb->next;
  594. qh->qlen--;
  595. if (qh->head == NULL)
  596. qh->tail = NULL;
  597. skb->next = NULL;
  598. }
  599. return skb;
  600. }
  601. static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
  602. {
  603. struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
  604. if (likely(skb != NULL)) {
  605. qdisc_qstats_backlog_dec(sch, skb);
  606. qdisc_bstats_update(sch, skb);
  607. }
  608. return skb;
  609. }
  610. /* Instead of calling kfree_skb() while root qdisc lock is held,
  611. * queue the skb for future freeing at end of __dev_xmit_skb()
  612. */
  613. static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free)
  614. {
  615. skb->next = *to_free;
  616. *to_free = skb;
  617. }
  618. static inline void __qdisc_drop_all(struct sk_buff *skb,
  619. struct sk_buff **to_free)
  620. {
  621. if (skb->prev)
  622. skb->prev->next = *to_free;
  623. else
  624. skb->next = *to_free;
  625. *to_free = skb;
  626. }
  627. static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
  628. struct qdisc_skb_head *qh,
  629. struct sk_buff **to_free)
  630. {
  631. struct sk_buff *skb = __qdisc_dequeue_head(qh);
  632. if (likely(skb != NULL)) {
  633. unsigned int len = qdisc_pkt_len(skb);
  634. qdisc_qstats_backlog_dec(sch, skb);
  635. __qdisc_drop(skb, to_free);
  636. return len;
  637. }
  638. return 0;
  639. }
  640. static inline unsigned int qdisc_queue_drop_head(struct Qdisc *sch,
  641. struct sk_buff **to_free)
  642. {
  643. return __qdisc_queue_drop_head(sch, &sch->q, to_free);
  644. }
  645. static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
  646. {
  647. const struct qdisc_skb_head *qh = &sch->q;
  648. return qh->head;
  649. }
  650. /* generic pseudo peek method for non-work-conserving qdisc */
  651. static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
  652. {
  653. /* we can reuse ->gso_skb because peek isn't called for root qdiscs */
  654. if (!sch->gso_skb) {
  655. sch->gso_skb = sch->dequeue(sch);
  656. if (sch->gso_skb) {
  657. /* it's still part of the queue */
  658. qdisc_qstats_backlog_inc(sch, sch->gso_skb);
  659. sch->q.qlen++;
  660. }
  661. }
  662. return sch->gso_skb;
  663. }
  664. /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
  665. static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
  666. {
  667. struct sk_buff *skb = sch->gso_skb;
  668. if (skb) {
  669. sch->gso_skb = NULL;
  670. qdisc_qstats_backlog_dec(sch, skb);
  671. sch->q.qlen--;
  672. } else {
  673. skb = sch->dequeue(sch);
  674. }
  675. return skb;
  676. }
  677. static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh)
  678. {
  679. /*
  680. * We do not know the backlog in bytes of this list, it
  681. * is up to the caller to correct it
  682. */
  683. ASSERT_RTNL();
  684. if (qh->qlen) {
  685. rtnl_kfree_skbs(qh->head, qh->tail);
  686. qh->head = NULL;
  687. qh->tail = NULL;
  688. qh->qlen = 0;
  689. }
  690. }
  691. static inline void qdisc_reset_queue(struct Qdisc *sch)
  692. {
  693. __qdisc_reset_queue(&sch->q);
  694. sch->qstats.backlog = 0;
  695. }
  696. static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
  697. struct Qdisc **pold)
  698. {
  699. struct Qdisc *old;
  700. sch_tree_lock(sch);
  701. old = *pold;
  702. *pold = new;
  703. if (old != NULL) {
  704. unsigned int qlen = old->q.qlen;
  705. unsigned int backlog = old->qstats.backlog;
  706. qdisc_reset(old);
  707. qdisc_tree_reduce_backlog(old, qlen, backlog);
  708. }
  709. sch_tree_unlock(sch);
  710. return old;
  711. }
  712. static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
  713. {
  714. rtnl_kfree_skbs(skb, skb);
  715. qdisc_qstats_drop(sch);
  716. }
  717. static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch,
  718. struct sk_buff **to_free)
  719. {
  720. __qdisc_drop(skb, to_free);
  721. qdisc_qstats_drop(sch);
  722. return NET_XMIT_DROP;
  723. }
  724. static inline int qdisc_drop_all(struct sk_buff *skb, struct Qdisc *sch,
  725. struct sk_buff **to_free)
  726. {
  727. __qdisc_drop_all(skb, to_free);
  728. qdisc_qstats_drop(sch);
  729. return NET_XMIT_DROP;
  730. }
  731. /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
  732. long it will take to send a packet given its size.
  733. */
  734. static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
  735. {
  736. int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
  737. if (slot < 0)
  738. slot = 0;
  739. slot >>= rtab->rate.cell_log;
  740. if (slot > 255)
  741. return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF];
  742. return rtab->data[slot];
  743. }
  744. struct psched_ratecfg {
  745. u64 rate_bytes_ps; /* bytes per second */
  746. u32 mult;
  747. u16 overhead;
  748. u8 linklayer;
  749. u8 shift;
  750. };
  751. static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
  752. unsigned int len)
  753. {
  754. len += r->overhead;
  755. if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
  756. return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
  757. return ((u64)len * r->mult) >> r->shift;
  758. }
  759. void psched_ratecfg_precompute(struct psched_ratecfg *r,
  760. const struct tc_ratespec *conf,
  761. u64 rate64);
  762. static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
  763. const struct psched_ratecfg *r)
  764. {
  765. memset(res, 0, sizeof(*res));
  766. /* legacy struct tc_ratespec has a 32bit @rate field
  767. * Qdisc using 64bit rate should add new attributes
  768. * in order to maintain compatibility.
  769. */
  770. res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
  771. res->overhead = r->overhead;
  772. res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
  773. }
  774. #endif