sch_generic.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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. #define TCQ_F_NOLOCK 0x100 /* qdisc does not require locking */
  68. u32 limit;
  69. const struct Qdisc_ops *ops;
  70. struct qdisc_size_table __rcu *stab;
  71. struct hlist_node hash;
  72. u32 handle;
  73. u32 parent;
  74. struct netdev_queue *dev_queue;
  75. struct net_rate_estimator __rcu *rate_est;
  76. struct gnet_stats_basic_cpu __percpu *cpu_bstats;
  77. struct gnet_stats_queue __percpu *cpu_qstats;
  78. /*
  79. * For performance sake on SMP, we put highly modified fields at the end
  80. */
  81. struct sk_buff_head gso_skb ____cacheline_aligned_in_smp;
  82. struct qdisc_skb_head q;
  83. struct gnet_stats_basic_packed bstats;
  84. seqcount_t running;
  85. struct gnet_stats_queue qstats;
  86. unsigned long state;
  87. struct Qdisc *next_sched;
  88. struct sk_buff_head skb_bad_txq;
  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 *sch,
  146. unsigned long arg);
  147. unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
  148. u32 classid);
  149. void (*unbind_tcf)(struct Qdisc *, unsigned long);
  150. /* rtnetlink specific */
  151. int (*dump)(struct Qdisc *, unsigned long,
  152. struct sk_buff *skb, struct tcmsg*);
  153. int (*dump_stats)(struct Qdisc *, unsigned long,
  154. struct gnet_dump *);
  155. };
  156. struct Qdisc_ops {
  157. struct Qdisc_ops *next;
  158. const struct Qdisc_class_ops *cl_ops;
  159. char id[IFNAMSIZ];
  160. int priv_size;
  161. unsigned int static_flags;
  162. int (*enqueue)(struct sk_buff *skb,
  163. struct Qdisc *sch,
  164. struct sk_buff **to_free);
  165. struct sk_buff * (*dequeue)(struct Qdisc *);
  166. struct sk_buff * (*peek)(struct Qdisc *);
  167. int (*init)(struct Qdisc *sch, struct nlattr *arg);
  168. void (*reset)(struct Qdisc *);
  169. void (*destroy)(struct Qdisc *);
  170. int (*change)(struct Qdisc *sch,
  171. struct nlattr *arg);
  172. void (*attach)(struct Qdisc *sch);
  173. int (*dump)(struct Qdisc *, struct sk_buff *);
  174. int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
  175. struct module *owner;
  176. };
  177. struct tcf_result {
  178. union {
  179. struct {
  180. unsigned long class;
  181. u32 classid;
  182. };
  183. const struct tcf_proto *goto_tp;
  184. };
  185. };
  186. struct tcf_proto_ops {
  187. struct list_head head;
  188. char kind[IFNAMSIZ];
  189. int (*classify)(struct sk_buff *,
  190. const struct tcf_proto *,
  191. struct tcf_result *);
  192. int (*init)(struct tcf_proto*);
  193. void (*destroy)(struct tcf_proto*);
  194. void* (*get)(struct tcf_proto*, u32 handle);
  195. int (*change)(struct net *net, struct sk_buff *,
  196. struct tcf_proto*, unsigned long,
  197. u32 handle, struct nlattr **,
  198. void **, bool);
  199. int (*delete)(struct tcf_proto*, void *, bool*);
  200. void (*walk)(struct tcf_proto*, struct tcf_walker *arg);
  201. void (*bind_class)(void *, u32, unsigned long);
  202. /* rtnetlink specific */
  203. int (*dump)(struct net*, struct tcf_proto*, void *,
  204. struct sk_buff *skb, struct tcmsg*);
  205. struct module *owner;
  206. };
  207. struct tcf_proto {
  208. /* Fast access part */
  209. struct tcf_proto __rcu *next;
  210. void __rcu *root;
  211. int (*classify)(struct sk_buff *,
  212. const struct tcf_proto *,
  213. struct tcf_result *);
  214. __be16 protocol;
  215. /* All the rest */
  216. u32 prio;
  217. u32 classid;
  218. struct Qdisc *q;
  219. void *data;
  220. const struct tcf_proto_ops *ops;
  221. struct tcf_chain *chain;
  222. struct rcu_head rcu;
  223. };
  224. struct qdisc_skb_cb {
  225. unsigned int pkt_len;
  226. u16 slave_dev_queue_mapping;
  227. u16 tc_classid;
  228. #define QDISC_CB_PRIV_LEN 20
  229. unsigned char data[QDISC_CB_PRIV_LEN];
  230. };
  231. typedef void tcf_chain_head_change_t(struct tcf_proto *tp_head, void *priv);
  232. struct tcf_chain {
  233. struct tcf_proto __rcu *filter_chain;
  234. tcf_chain_head_change_t *chain_head_change;
  235. void *chain_head_change_priv;
  236. struct list_head list;
  237. struct tcf_block *block;
  238. u32 index; /* chain index */
  239. unsigned int refcnt;
  240. };
  241. struct tcf_block {
  242. struct list_head chain_list;
  243. struct net *net;
  244. struct Qdisc *q;
  245. struct list_head cb_list;
  246. };
  247. static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz)
  248. {
  249. struct qdisc_skb_cb *qcb;
  250. BUILD_BUG_ON(sizeof(skb->cb) < offsetof(struct qdisc_skb_cb, data) + sz);
  251. BUILD_BUG_ON(sizeof(qcb->data) < sz);
  252. }
  253. static inline int qdisc_qlen(const struct Qdisc *q)
  254. {
  255. return q->q.qlen;
  256. }
  257. static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb)
  258. {
  259. return (struct qdisc_skb_cb *)skb->cb;
  260. }
  261. static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
  262. {
  263. return &qdisc->q.lock;
  264. }
  265. static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc)
  266. {
  267. struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc);
  268. return q;
  269. }
  270. static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc)
  271. {
  272. return qdisc->dev_queue->qdisc_sleeping;
  273. }
  274. /* The qdisc root lock is a mechanism by which to top level
  275. * of a qdisc tree can be locked from any qdisc node in the
  276. * forest. This allows changing the configuration of some
  277. * aspect of the qdisc tree while blocking out asynchronous
  278. * qdisc access in the packet processing paths.
  279. *
  280. * It is only legal to do this when the root will not change
  281. * on us. Otherwise we'll potentially lock the wrong qdisc
  282. * root. This is enforced by holding the RTNL semaphore, which
  283. * all users of this lock accessor must do.
  284. */
  285. static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc)
  286. {
  287. struct Qdisc *root = qdisc_root(qdisc);
  288. ASSERT_RTNL();
  289. return qdisc_lock(root);
  290. }
  291. static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc)
  292. {
  293. struct Qdisc *root = qdisc_root_sleeping(qdisc);
  294. ASSERT_RTNL();
  295. return qdisc_lock(root);
  296. }
  297. static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc)
  298. {
  299. struct Qdisc *root = qdisc_root_sleeping(qdisc);
  300. ASSERT_RTNL();
  301. return &root->running;
  302. }
  303. static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc)
  304. {
  305. return qdisc->dev_queue->dev;
  306. }
  307. static inline void sch_tree_lock(const struct Qdisc *q)
  308. {
  309. spin_lock_bh(qdisc_root_sleeping_lock(q));
  310. }
  311. static inline void sch_tree_unlock(const struct Qdisc *q)
  312. {
  313. spin_unlock_bh(qdisc_root_sleeping_lock(q));
  314. }
  315. extern struct Qdisc noop_qdisc;
  316. extern struct Qdisc_ops noop_qdisc_ops;
  317. extern struct Qdisc_ops pfifo_fast_ops;
  318. extern struct Qdisc_ops mq_qdisc_ops;
  319. extern struct Qdisc_ops noqueue_qdisc_ops;
  320. extern const struct Qdisc_ops *default_qdisc_ops;
  321. static inline const struct Qdisc_ops *
  322. get_default_qdisc_ops(const struct net_device *dev, int ntx)
  323. {
  324. return ntx < dev->real_num_tx_queues ?
  325. default_qdisc_ops : &pfifo_fast_ops;
  326. }
  327. struct Qdisc_class_common {
  328. u32 classid;
  329. struct hlist_node hnode;
  330. };
  331. struct Qdisc_class_hash {
  332. struct hlist_head *hash;
  333. unsigned int hashsize;
  334. unsigned int hashmask;
  335. unsigned int hashelems;
  336. };
  337. static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
  338. {
  339. id ^= id >> 8;
  340. id ^= id >> 4;
  341. return id & mask;
  342. }
  343. static inline struct Qdisc_class_common *
  344. qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id)
  345. {
  346. struct Qdisc_class_common *cl;
  347. unsigned int h;
  348. if (!id)
  349. return NULL;
  350. h = qdisc_class_hash(id, hash->hashmask);
  351. hlist_for_each_entry(cl, &hash->hash[h], hnode) {
  352. if (cl->classid == id)
  353. return cl;
  354. }
  355. return NULL;
  356. }
  357. static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid)
  358. {
  359. u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY;
  360. return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL;
  361. }
  362. int qdisc_class_hash_init(struct Qdisc_class_hash *);
  363. void qdisc_class_hash_insert(struct Qdisc_class_hash *,
  364. struct Qdisc_class_common *);
  365. void qdisc_class_hash_remove(struct Qdisc_class_hash *,
  366. struct Qdisc_class_common *);
  367. void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
  368. void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
  369. void dev_init_scheduler(struct net_device *dev);
  370. void dev_shutdown(struct net_device *dev);
  371. void dev_activate(struct net_device *dev);
  372. void dev_deactivate(struct net_device *dev);
  373. void dev_deactivate_many(struct list_head *head);
  374. struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
  375. struct Qdisc *qdisc);
  376. void qdisc_reset(struct Qdisc *qdisc);
  377. void qdisc_destroy(struct Qdisc *qdisc);
  378. void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, unsigned int n,
  379. unsigned int len);
  380. struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
  381. const struct Qdisc_ops *ops);
  382. struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue,
  383. const struct Qdisc_ops *ops, u32 parentid);
  384. void __qdisc_calculate_pkt_len(struct sk_buff *skb,
  385. const struct qdisc_size_table *stab);
  386. int skb_do_redirect(struct sk_buff *);
  387. static inline void skb_reset_tc(struct sk_buff *skb)
  388. {
  389. #ifdef CONFIG_NET_CLS_ACT
  390. skb->tc_redirected = 0;
  391. #endif
  392. }
  393. static inline bool skb_at_tc_ingress(const struct sk_buff *skb)
  394. {
  395. #ifdef CONFIG_NET_CLS_ACT
  396. return skb->tc_at_ingress;
  397. #else
  398. return false;
  399. #endif
  400. }
  401. static inline bool skb_skip_tc_classify(struct sk_buff *skb)
  402. {
  403. #ifdef CONFIG_NET_CLS_ACT
  404. if (skb->tc_skip_classify) {
  405. skb->tc_skip_classify = 0;
  406. return true;
  407. }
  408. #endif
  409. return false;
  410. }
  411. /* Reset all TX qdiscs greater then index of a device. */
  412. static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i)
  413. {
  414. struct Qdisc *qdisc;
  415. for (; i < dev->num_tx_queues; i++) {
  416. qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc);
  417. if (qdisc) {
  418. spin_lock_bh(qdisc_lock(qdisc));
  419. qdisc_reset(qdisc);
  420. spin_unlock_bh(qdisc_lock(qdisc));
  421. }
  422. }
  423. }
  424. static inline void qdisc_reset_all_tx(struct net_device *dev)
  425. {
  426. qdisc_reset_all_tx_gt(dev, 0);
  427. }
  428. /* Are all TX queues of the device empty? */
  429. static inline bool qdisc_all_tx_empty(const struct net_device *dev)
  430. {
  431. unsigned int i;
  432. rcu_read_lock();
  433. for (i = 0; i < dev->num_tx_queues; i++) {
  434. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  435. const struct Qdisc *q = rcu_dereference(txq->qdisc);
  436. if (q->q.qlen) {
  437. rcu_read_unlock();
  438. return false;
  439. }
  440. }
  441. rcu_read_unlock();
  442. return true;
  443. }
  444. /* Are any of the TX qdiscs changing? */
  445. static inline bool qdisc_tx_changing(const struct net_device *dev)
  446. {
  447. unsigned int i;
  448. for (i = 0; i < dev->num_tx_queues; i++) {
  449. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  450. if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping)
  451. return true;
  452. }
  453. return false;
  454. }
  455. /* Is the device using the noop qdisc on all queues? */
  456. static inline bool qdisc_tx_is_noop(const struct net_device *dev)
  457. {
  458. unsigned int i;
  459. for (i = 0; i < dev->num_tx_queues; i++) {
  460. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  461. if (rcu_access_pointer(txq->qdisc) != &noop_qdisc)
  462. return false;
  463. }
  464. return true;
  465. }
  466. static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb)
  467. {
  468. return qdisc_skb_cb(skb)->pkt_len;
  469. }
  470. /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
  471. enum net_xmit_qdisc_t {
  472. __NET_XMIT_STOLEN = 0x00010000,
  473. __NET_XMIT_BYPASS = 0x00020000,
  474. };
  475. #ifdef CONFIG_NET_CLS_ACT
  476. #define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
  477. #else
  478. #define net_xmit_drop_count(e) (1)
  479. #endif
  480. static inline void qdisc_calculate_pkt_len(struct sk_buff *skb,
  481. const struct Qdisc *sch)
  482. {
  483. #ifdef CONFIG_NET_SCHED
  484. struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab);
  485. if (stab)
  486. __qdisc_calculate_pkt_len(skb, stab);
  487. #endif
  488. }
  489. static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch,
  490. struct sk_buff **to_free)
  491. {
  492. qdisc_calculate_pkt_len(skb, sch);
  493. return sch->enqueue(skb, sch, to_free);
  494. }
  495. static inline bool qdisc_is_percpu_stats(const struct Qdisc *q)
  496. {
  497. return q->flags & TCQ_F_CPUSTATS;
  498. }
  499. static inline void _bstats_update(struct gnet_stats_basic_packed *bstats,
  500. __u64 bytes, __u32 packets)
  501. {
  502. bstats->bytes += bytes;
  503. bstats->packets += packets;
  504. }
  505. static inline void bstats_update(struct gnet_stats_basic_packed *bstats,
  506. const struct sk_buff *skb)
  507. {
  508. _bstats_update(bstats,
  509. qdisc_pkt_len(skb),
  510. skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1);
  511. }
  512. static inline void _bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
  513. __u64 bytes, __u32 packets)
  514. {
  515. u64_stats_update_begin(&bstats->syncp);
  516. _bstats_update(&bstats->bstats, bytes, packets);
  517. u64_stats_update_end(&bstats->syncp);
  518. }
  519. static inline void bstats_cpu_update(struct gnet_stats_basic_cpu *bstats,
  520. const struct sk_buff *skb)
  521. {
  522. u64_stats_update_begin(&bstats->syncp);
  523. bstats_update(&bstats->bstats, skb);
  524. u64_stats_update_end(&bstats->syncp);
  525. }
  526. static inline void qdisc_bstats_cpu_update(struct Qdisc *sch,
  527. const struct sk_buff *skb)
  528. {
  529. bstats_cpu_update(this_cpu_ptr(sch->cpu_bstats), skb);
  530. }
  531. static inline void qdisc_bstats_update(struct Qdisc *sch,
  532. const struct sk_buff *skb)
  533. {
  534. bstats_update(&sch->bstats, skb);
  535. }
  536. static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch,
  537. const struct sk_buff *skb)
  538. {
  539. sch->qstats.backlog -= qdisc_pkt_len(skb);
  540. }
  541. static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch,
  542. const struct sk_buff *skb)
  543. {
  544. this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
  545. }
  546. static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch,
  547. const struct sk_buff *skb)
  548. {
  549. sch->qstats.backlog += qdisc_pkt_len(skb);
  550. }
  551. static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch,
  552. const struct sk_buff *skb)
  553. {
  554. this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb));
  555. }
  556. static inline void qdisc_qstats_cpu_qlen_inc(struct Qdisc *sch)
  557. {
  558. this_cpu_inc(sch->cpu_qstats->qlen);
  559. }
  560. static inline void qdisc_qstats_cpu_qlen_dec(struct Qdisc *sch)
  561. {
  562. this_cpu_dec(sch->cpu_qstats->qlen);
  563. }
  564. static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch)
  565. {
  566. this_cpu_inc(sch->cpu_qstats->requeues);
  567. }
  568. static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count)
  569. {
  570. sch->qstats.drops += count;
  571. }
  572. static inline void qstats_drop_inc(struct gnet_stats_queue *qstats)
  573. {
  574. qstats->drops++;
  575. }
  576. static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats)
  577. {
  578. qstats->overlimits++;
  579. }
  580. static inline void qdisc_qstats_drop(struct Qdisc *sch)
  581. {
  582. qstats_drop_inc(&sch->qstats);
  583. }
  584. static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch)
  585. {
  586. this_cpu_inc(sch->cpu_qstats->drops);
  587. }
  588. static inline void qdisc_qstats_overlimit(struct Qdisc *sch)
  589. {
  590. sch->qstats.overlimits++;
  591. }
  592. static inline void qdisc_skb_head_init(struct qdisc_skb_head *qh)
  593. {
  594. qh->head = NULL;
  595. qh->tail = NULL;
  596. qh->qlen = 0;
  597. }
  598. static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
  599. struct qdisc_skb_head *qh)
  600. {
  601. struct sk_buff *last = qh->tail;
  602. if (last) {
  603. skb->next = NULL;
  604. last->next = skb;
  605. qh->tail = skb;
  606. } else {
  607. qh->tail = skb;
  608. qh->head = skb;
  609. }
  610. qh->qlen++;
  611. qdisc_qstats_backlog_inc(sch, skb);
  612. return NET_XMIT_SUCCESS;
  613. }
  614. static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
  615. {
  616. return __qdisc_enqueue_tail(skb, sch, &sch->q);
  617. }
  618. static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh)
  619. {
  620. struct sk_buff *skb = qh->head;
  621. if (likely(skb != NULL)) {
  622. qh->head = skb->next;
  623. qh->qlen--;
  624. if (qh->head == NULL)
  625. qh->tail = NULL;
  626. skb->next = NULL;
  627. }
  628. return skb;
  629. }
  630. static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
  631. {
  632. struct sk_buff *skb = __qdisc_dequeue_head(&sch->q);
  633. if (likely(skb != NULL)) {
  634. qdisc_qstats_backlog_dec(sch, skb);
  635. qdisc_bstats_update(sch, skb);
  636. }
  637. return skb;
  638. }
  639. /* Instead of calling kfree_skb() while root qdisc lock is held,
  640. * queue the skb for future freeing at end of __dev_xmit_skb()
  641. */
  642. static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free)
  643. {
  644. skb->next = *to_free;
  645. *to_free = skb;
  646. }
  647. static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
  648. struct qdisc_skb_head *qh,
  649. struct sk_buff **to_free)
  650. {
  651. struct sk_buff *skb = __qdisc_dequeue_head(qh);
  652. if (likely(skb != NULL)) {
  653. unsigned int len = qdisc_pkt_len(skb);
  654. qdisc_qstats_backlog_dec(sch, skb);
  655. __qdisc_drop(skb, to_free);
  656. return len;
  657. }
  658. return 0;
  659. }
  660. static inline unsigned int qdisc_queue_drop_head(struct Qdisc *sch,
  661. struct sk_buff **to_free)
  662. {
  663. return __qdisc_queue_drop_head(sch, &sch->q, to_free);
  664. }
  665. static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
  666. {
  667. const struct qdisc_skb_head *qh = &sch->q;
  668. return qh->head;
  669. }
  670. /* generic pseudo peek method for non-work-conserving qdisc */
  671. static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
  672. {
  673. struct sk_buff *skb = skb_peek(&sch->gso_skb);
  674. /* we can reuse ->gso_skb because peek isn't called for root qdiscs */
  675. if (!skb) {
  676. skb = sch->dequeue(sch);
  677. if (skb) {
  678. __skb_queue_head(&sch->gso_skb, skb);
  679. /* it's still part of the queue */
  680. qdisc_qstats_backlog_inc(sch, skb);
  681. sch->q.qlen++;
  682. }
  683. }
  684. return skb;
  685. }
  686. /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
  687. static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
  688. {
  689. struct sk_buff *skb = skb_peek(&sch->gso_skb);
  690. if (skb) {
  691. skb = __skb_dequeue(&sch->gso_skb);
  692. qdisc_qstats_backlog_dec(sch, skb);
  693. sch->q.qlen--;
  694. } else {
  695. skb = sch->dequeue(sch);
  696. }
  697. return skb;
  698. }
  699. static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh)
  700. {
  701. /*
  702. * We do not know the backlog in bytes of this list, it
  703. * is up to the caller to correct it
  704. */
  705. ASSERT_RTNL();
  706. if (qh->qlen) {
  707. rtnl_kfree_skbs(qh->head, qh->tail);
  708. qh->head = NULL;
  709. qh->tail = NULL;
  710. qh->qlen = 0;
  711. }
  712. }
  713. static inline void qdisc_reset_queue(struct Qdisc *sch)
  714. {
  715. __qdisc_reset_queue(&sch->q);
  716. sch->qstats.backlog = 0;
  717. }
  718. static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new,
  719. struct Qdisc **pold)
  720. {
  721. struct Qdisc *old;
  722. sch_tree_lock(sch);
  723. old = *pold;
  724. *pold = new;
  725. if (old != NULL) {
  726. unsigned int qlen = old->q.qlen;
  727. unsigned int backlog = old->qstats.backlog;
  728. qdisc_reset(old);
  729. qdisc_tree_reduce_backlog(old, qlen, backlog);
  730. }
  731. sch_tree_unlock(sch);
  732. return old;
  733. }
  734. static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
  735. {
  736. rtnl_kfree_skbs(skb, skb);
  737. qdisc_qstats_drop(sch);
  738. }
  739. static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch,
  740. struct sk_buff **to_free)
  741. {
  742. __qdisc_drop(skb, to_free);
  743. qdisc_qstats_cpu_drop(sch);
  744. return NET_XMIT_DROP;
  745. }
  746. static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch,
  747. struct sk_buff **to_free)
  748. {
  749. __qdisc_drop(skb, to_free);
  750. qdisc_qstats_drop(sch);
  751. return NET_XMIT_DROP;
  752. }
  753. /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
  754. long it will take to send a packet given its size.
  755. */
  756. static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
  757. {
  758. int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
  759. if (slot < 0)
  760. slot = 0;
  761. slot >>= rtab->rate.cell_log;
  762. if (slot > 255)
  763. return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF];
  764. return rtab->data[slot];
  765. }
  766. struct psched_ratecfg {
  767. u64 rate_bytes_ps; /* bytes per second */
  768. u32 mult;
  769. u16 overhead;
  770. u8 linklayer;
  771. u8 shift;
  772. };
  773. static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,
  774. unsigned int len)
  775. {
  776. len += r->overhead;
  777. if (unlikely(r->linklayer == TC_LINKLAYER_ATM))
  778. return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift;
  779. return ((u64)len * r->mult) >> r->shift;
  780. }
  781. void psched_ratecfg_precompute(struct psched_ratecfg *r,
  782. const struct tc_ratespec *conf,
  783. u64 rate64);
  784. static inline void psched_ratecfg_getrate(struct tc_ratespec *res,
  785. const struct psched_ratecfg *r)
  786. {
  787. memset(res, 0, sizeof(*res));
  788. /* legacy struct tc_ratespec has a 32bit @rate field
  789. * Qdisc using 64bit rate should add new attributes
  790. * in order to maintain compatibility.
  791. */
  792. res->rate = min_t(u64, r->rate_bytes_ps, ~0U);
  793. res->overhead = r->overhead;
  794. res->linklayer = (r->linklayer & TC_LINKLAYER_MASK);
  795. }
  796. /* Mini Qdisc serves for specific needs of ingress/clsact Qdisc.
  797. * The fast path only needs to access filter list and to update stats
  798. */
  799. struct mini_Qdisc {
  800. struct tcf_proto *filter_list;
  801. struct gnet_stats_basic_cpu __percpu *cpu_bstats;
  802. struct gnet_stats_queue __percpu *cpu_qstats;
  803. struct rcu_head rcu;
  804. };
  805. static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq,
  806. const struct sk_buff *skb)
  807. {
  808. bstats_cpu_update(this_cpu_ptr(miniq->cpu_bstats), skb);
  809. }
  810. static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq)
  811. {
  812. this_cpu_inc(miniq->cpu_qstats->drops);
  813. }
  814. struct mini_Qdisc_pair {
  815. struct mini_Qdisc miniq1;
  816. struct mini_Qdisc miniq2;
  817. struct mini_Qdisc __rcu **p_miniq;
  818. };
  819. void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp,
  820. struct tcf_proto *tp_head);
  821. void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc,
  822. struct mini_Qdisc __rcu **p_miniq);
  823. #endif