sch_generic.h 21 KB

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