sch_generic.h 24 KB

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