sch_generic.h 20 KB

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