sch_generic.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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/module.h>
  7. #include <linux/pkt_sched.h>
  8. #include <linux/pkt_cls.h>
  9. #include <net/gen_stats.h>
  10. #include <net/rtnetlink.h>
  11. struct Qdisc_ops;
  12. struct qdisc_walker;
  13. struct tcf_walker;
  14. struct module;
  15. struct qdisc_rate_table
  16. {
  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. {
  24. __QDISC_STATE_RUNNING,
  25. __QDISC_STATE_SCHED,
  26. __QDISC_STATE_DEACTIVATED,
  27. };
  28. struct qdisc_size_table {
  29. struct list_head list;
  30. struct tc_sizespec szopts;
  31. int refcnt;
  32. u16 data[];
  33. };
  34. struct Qdisc
  35. {
  36. int (*enqueue)(struct sk_buff *skb, struct Qdisc *dev);
  37. struct sk_buff * (*dequeue)(struct Qdisc *dev);
  38. unsigned flags;
  39. #define TCQ_F_BUILTIN 1
  40. #define TCQ_F_THROTTLED 2
  41. #define TCQ_F_INGRESS 4
  42. int padded;
  43. struct Qdisc_ops *ops;
  44. struct qdisc_size_table *stab;
  45. u32 handle;
  46. u32 parent;
  47. atomic_t refcnt;
  48. unsigned long state;
  49. struct sk_buff *gso_skb;
  50. struct sk_buff_head q;
  51. struct netdev_queue *dev_queue;
  52. struct Qdisc *next_sched;
  53. struct list_head list;
  54. struct gnet_stats_basic bstats;
  55. struct gnet_stats_queue qstats;
  56. struct gnet_stats_rate_est rate_est;
  57. int (*reshape_fail)(struct sk_buff *skb,
  58. struct Qdisc *q);
  59. void *u32_node;
  60. /* This field is deprecated, but it is still used by CBQ
  61. * and it will live until better solution will be invented.
  62. */
  63. struct Qdisc *__parent;
  64. };
  65. struct Qdisc_class_ops
  66. {
  67. /* Child qdisc manipulation */
  68. int (*graft)(struct Qdisc *, unsigned long cl,
  69. struct Qdisc *, struct Qdisc **);
  70. struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
  71. void (*qlen_notify)(struct Qdisc *, unsigned long);
  72. /* Class manipulation routines */
  73. unsigned long (*get)(struct Qdisc *, u32 classid);
  74. void (*put)(struct Qdisc *, unsigned long);
  75. int (*change)(struct Qdisc *, u32, u32,
  76. struct nlattr **, unsigned long *);
  77. int (*delete)(struct Qdisc *, unsigned long);
  78. void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
  79. /* Filter manipulation */
  80. struct tcf_proto ** (*tcf_chain)(struct Qdisc *, unsigned long);
  81. unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
  82. u32 classid);
  83. void (*unbind_tcf)(struct Qdisc *, unsigned long);
  84. /* rtnetlink specific */
  85. int (*dump)(struct Qdisc *, unsigned long,
  86. struct sk_buff *skb, struct tcmsg*);
  87. int (*dump_stats)(struct Qdisc *, unsigned long,
  88. struct gnet_dump *);
  89. };
  90. struct Qdisc_ops
  91. {
  92. struct Qdisc_ops *next;
  93. const struct Qdisc_class_ops *cl_ops;
  94. char id[IFNAMSIZ];
  95. int priv_size;
  96. int (*enqueue)(struct sk_buff *, struct Qdisc *);
  97. struct sk_buff * (*dequeue)(struct Qdisc *);
  98. struct sk_buff * (*peek)(struct Qdisc *);
  99. unsigned int (*drop)(struct Qdisc *);
  100. int (*init)(struct Qdisc *, struct nlattr *arg);
  101. void (*reset)(struct Qdisc *);
  102. void (*destroy)(struct Qdisc *);
  103. int (*change)(struct Qdisc *, struct nlattr *arg);
  104. int (*dump)(struct Qdisc *, struct sk_buff *);
  105. int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
  106. struct module *owner;
  107. };
  108. struct tcf_result
  109. {
  110. unsigned long class;
  111. u32 classid;
  112. };
  113. struct tcf_proto_ops
  114. {
  115. struct tcf_proto_ops *next;
  116. char kind[IFNAMSIZ];
  117. int (*classify)(struct sk_buff*, struct tcf_proto*,
  118. struct tcf_result *);
  119. int (*init)(struct tcf_proto*);
  120. void (*destroy)(struct tcf_proto*);
  121. unsigned long (*get)(struct tcf_proto*, u32 handle);
  122. void (*put)(struct tcf_proto*, unsigned long);
  123. int (*change)(struct tcf_proto*, unsigned long,
  124. u32 handle, struct nlattr **,
  125. unsigned long *);
  126. int (*delete)(struct tcf_proto*, unsigned long);
  127. void (*walk)(struct tcf_proto*, struct tcf_walker *arg);
  128. /* rtnetlink specific */
  129. int (*dump)(struct tcf_proto*, unsigned long,
  130. struct sk_buff *skb, struct tcmsg*);
  131. struct module *owner;
  132. };
  133. struct tcf_proto
  134. {
  135. /* Fast access part */
  136. struct tcf_proto *next;
  137. void *root;
  138. int (*classify)(struct sk_buff*, struct tcf_proto*,
  139. struct tcf_result *);
  140. __be16 protocol;
  141. /* All the rest */
  142. u32 prio;
  143. u32 classid;
  144. struct Qdisc *q;
  145. void *data;
  146. struct tcf_proto_ops *ops;
  147. };
  148. struct qdisc_skb_cb {
  149. unsigned int pkt_len;
  150. char data[];
  151. };
  152. static inline struct qdisc_skb_cb *qdisc_skb_cb(struct sk_buff *skb)
  153. {
  154. return (struct qdisc_skb_cb *)skb->cb;
  155. }
  156. static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc)
  157. {
  158. return &qdisc->q.lock;
  159. }
  160. static inline struct Qdisc *qdisc_root(struct Qdisc *qdisc)
  161. {
  162. return qdisc->dev_queue->qdisc;
  163. }
  164. static inline struct Qdisc *qdisc_root_sleeping(struct Qdisc *qdisc)
  165. {
  166. return qdisc->dev_queue->qdisc_sleeping;
  167. }
  168. /* The qdisc root lock is a mechanism by which to top level
  169. * of a qdisc tree can be locked from any qdisc node in the
  170. * forest. This allows changing the configuration of some
  171. * aspect of the qdisc tree while blocking out asynchronous
  172. * qdisc access in the packet processing paths.
  173. *
  174. * It is only legal to do this when the root will not change
  175. * on us. Otherwise we'll potentially lock the wrong qdisc
  176. * root. This is enforced by holding the RTNL semaphore, which
  177. * all users of this lock accessor must do.
  178. */
  179. static inline spinlock_t *qdisc_root_lock(struct Qdisc *qdisc)
  180. {
  181. struct Qdisc *root = qdisc_root(qdisc);
  182. ASSERT_RTNL();
  183. return qdisc_lock(root);
  184. }
  185. static inline spinlock_t *qdisc_root_sleeping_lock(struct Qdisc *qdisc)
  186. {
  187. struct Qdisc *root = qdisc_root_sleeping(qdisc);
  188. ASSERT_RTNL();
  189. return qdisc_lock(root);
  190. }
  191. static inline struct net_device *qdisc_dev(struct Qdisc *qdisc)
  192. {
  193. return qdisc->dev_queue->dev;
  194. }
  195. static inline void sch_tree_lock(struct Qdisc *q)
  196. {
  197. spin_lock_bh(qdisc_root_sleeping_lock(q));
  198. }
  199. static inline void sch_tree_unlock(struct Qdisc *q)
  200. {
  201. spin_unlock_bh(qdisc_root_sleeping_lock(q));
  202. }
  203. #define tcf_tree_lock(tp) sch_tree_lock((tp)->q)
  204. #define tcf_tree_unlock(tp) sch_tree_unlock((tp)->q)
  205. extern struct Qdisc noop_qdisc;
  206. extern struct Qdisc_ops noop_qdisc_ops;
  207. struct Qdisc_class_common
  208. {
  209. u32 classid;
  210. struct hlist_node hnode;
  211. };
  212. struct Qdisc_class_hash
  213. {
  214. struct hlist_head *hash;
  215. unsigned int hashsize;
  216. unsigned int hashmask;
  217. unsigned int hashelems;
  218. };
  219. static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
  220. {
  221. id ^= id >> 8;
  222. id ^= id >> 4;
  223. return id & mask;
  224. }
  225. static inline struct Qdisc_class_common *
  226. qdisc_class_find(struct Qdisc_class_hash *hash, u32 id)
  227. {
  228. struct Qdisc_class_common *cl;
  229. struct hlist_node *n;
  230. unsigned int h;
  231. h = qdisc_class_hash(id, hash->hashmask);
  232. hlist_for_each_entry(cl, n, &hash->hash[h], hnode) {
  233. if (cl->classid == id)
  234. return cl;
  235. }
  236. return NULL;
  237. }
  238. extern int qdisc_class_hash_init(struct Qdisc_class_hash *);
  239. extern void qdisc_class_hash_insert(struct Qdisc_class_hash *, struct Qdisc_class_common *);
  240. extern void qdisc_class_hash_remove(struct Qdisc_class_hash *, struct Qdisc_class_common *);
  241. extern void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
  242. extern void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
  243. extern void dev_init_scheduler(struct net_device *dev);
  244. extern void dev_shutdown(struct net_device *dev);
  245. extern void dev_activate(struct net_device *dev);
  246. extern void dev_deactivate(struct net_device *dev);
  247. extern void qdisc_reset(struct Qdisc *qdisc);
  248. extern void qdisc_destroy(struct Qdisc *qdisc);
  249. extern void qdisc_tree_decrease_qlen(struct Qdisc *qdisc, unsigned int n);
  250. extern struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
  251. struct Qdisc_ops *ops);
  252. extern struct Qdisc *qdisc_create_dflt(struct net_device *dev,
  253. struct netdev_queue *dev_queue,
  254. struct Qdisc_ops *ops, u32 parentid);
  255. extern void qdisc_calculate_pkt_len(struct sk_buff *skb,
  256. struct qdisc_size_table *stab);
  257. extern void tcf_destroy(struct tcf_proto *tp);
  258. extern void tcf_destroy_chain(struct tcf_proto **fl);
  259. /* Reset all TX qdiscs of a device. */
  260. static inline void qdisc_reset_all_tx(struct net_device *dev)
  261. {
  262. unsigned int i;
  263. for (i = 0; i < dev->num_tx_queues; i++)
  264. qdisc_reset(netdev_get_tx_queue(dev, i)->qdisc);
  265. }
  266. /* Are all TX queues of the device empty? */
  267. static inline bool qdisc_all_tx_empty(const struct net_device *dev)
  268. {
  269. unsigned int i;
  270. for (i = 0; i < dev->num_tx_queues; i++) {
  271. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  272. const struct Qdisc *q = txq->qdisc;
  273. if (q->q.qlen)
  274. return false;
  275. }
  276. return true;
  277. }
  278. /* Are any of the TX qdiscs changing? */
  279. static inline bool qdisc_tx_changing(struct net_device *dev)
  280. {
  281. unsigned int i;
  282. for (i = 0; i < dev->num_tx_queues; i++) {
  283. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  284. if (txq->qdisc != txq->qdisc_sleeping)
  285. return true;
  286. }
  287. return false;
  288. }
  289. /* Is the device using the noop qdisc on all queues? */
  290. static inline bool qdisc_tx_is_noop(const struct net_device *dev)
  291. {
  292. unsigned int i;
  293. for (i = 0; i < dev->num_tx_queues; i++) {
  294. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  295. if (txq->qdisc != &noop_qdisc)
  296. return false;
  297. }
  298. return true;
  299. }
  300. static inline unsigned int qdisc_pkt_len(struct sk_buff *skb)
  301. {
  302. return qdisc_skb_cb(skb)->pkt_len;
  303. }
  304. /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
  305. enum net_xmit_qdisc_t {
  306. __NET_XMIT_STOLEN = 0x00010000,
  307. __NET_XMIT_BYPASS = 0x00020000,
  308. };
  309. #ifdef CONFIG_NET_CLS_ACT
  310. #define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
  311. #else
  312. #define net_xmit_drop_count(e) (1)
  313. #endif
  314. static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  315. {
  316. #ifdef CONFIG_NET_SCHED
  317. if (sch->stab)
  318. qdisc_calculate_pkt_len(skb, sch->stab);
  319. #endif
  320. return sch->enqueue(skb, sch);
  321. }
  322. static inline int qdisc_enqueue_root(struct sk_buff *skb, struct Qdisc *sch)
  323. {
  324. qdisc_skb_cb(skb)->pkt_len = skb->len;
  325. return qdisc_enqueue(skb, sch) & NET_XMIT_MASK;
  326. }
  327. static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
  328. struct sk_buff_head *list)
  329. {
  330. __skb_queue_tail(list, skb);
  331. sch->qstats.backlog += qdisc_pkt_len(skb);
  332. sch->bstats.bytes += qdisc_pkt_len(skb);
  333. sch->bstats.packets++;
  334. return NET_XMIT_SUCCESS;
  335. }
  336. static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
  337. {
  338. return __qdisc_enqueue_tail(skb, sch, &sch->q);
  339. }
  340. static inline struct sk_buff *__qdisc_dequeue_head(struct Qdisc *sch,
  341. struct sk_buff_head *list)
  342. {
  343. struct sk_buff *skb = __skb_dequeue(list);
  344. if (likely(skb != NULL))
  345. sch->qstats.backlog -= qdisc_pkt_len(skb);
  346. return skb;
  347. }
  348. static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
  349. {
  350. return __qdisc_dequeue_head(sch, &sch->q);
  351. }
  352. static inline struct sk_buff *__qdisc_dequeue_tail(struct Qdisc *sch,
  353. struct sk_buff_head *list)
  354. {
  355. struct sk_buff *skb = __skb_dequeue_tail(list);
  356. if (likely(skb != NULL))
  357. sch->qstats.backlog -= qdisc_pkt_len(skb);
  358. return skb;
  359. }
  360. static inline struct sk_buff *qdisc_dequeue_tail(struct Qdisc *sch)
  361. {
  362. return __qdisc_dequeue_tail(sch, &sch->q);
  363. }
  364. static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
  365. {
  366. return skb_peek(&sch->q);
  367. }
  368. /* generic pseudo peek method for non-work-conserving qdisc */
  369. static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
  370. {
  371. /* we can reuse ->gso_skb because peek isn't called for root qdiscs */
  372. if (!sch->gso_skb) {
  373. sch->gso_skb = sch->dequeue(sch);
  374. if (sch->gso_skb)
  375. /* it's still part of the queue */
  376. sch->q.qlen++;
  377. }
  378. return sch->gso_skb;
  379. }
  380. /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
  381. static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
  382. {
  383. struct sk_buff *skb = sch->gso_skb;
  384. if (skb) {
  385. sch->gso_skb = NULL;
  386. sch->q.qlen--;
  387. } else {
  388. skb = sch->dequeue(sch);
  389. }
  390. return skb;
  391. }
  392. static inline void __qdisc_reset_queue(struct Qdisc *sch,
  393. struct sk_buff_head *list)
  394. {
  395. /*
  396. * We do not know the backlog in bytes of this list, it
  397. * is up to the caller to correct it
  398. */
  399. __skb_queue_purge(list);
  400. }
  401. static inline void qdisc_reset_queue(struct Qdisc *sch)
  402. {
  403. __qdisc_reset_queue(sch, &sch->q);
  404. sch->qstats.backlog = 0;
  405. }
  406. static inline unsigned int __qdisc_queue_drop(struct Qdisc *sch,
  407. struct sk_buff_head *list)
  408. {
  409. struct sk_buff *skb = __qdisc_dequeue_tail(sch, list);
  410. if (likely(skb != NULL)) {
  411. unsigned int len = qdisc_pkt_len(skb);
  412. kfree_skb(skb);
  413. return len;
  414. }
  415. return 0;
  416. }
  417. static inline unsigned int qdisc_queue_drop(struct Qdisc *sch)
  418. {
  419. return __qdisc_queue_drop(sch, &sch->q);
  420. }
  421. static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
  422. {
  423. kfree_skb(skb);
  424. sch->qstats.drops++;
  425. return NET_XMIT_DROP;
  426. }
  427. static inline int qdisc_reshape_fail(struct sk_buff *skb, struct Qdisc *sch)
  428. {
  429. sch->qstats.drops++;
  430. #ifdef CONFIG_NET_CLS_ACT
  431. if (sch->reshape_fail == NULL || sch->reshape_fail(skb, sch))
  432. goto drop;
  433. return NET_XMIT_SUCCESS;
  434. drop:
  435. #endif
  436. kfree_skb(skb);
  437. return NET_XMIT_DROP;
  438. }
  439. /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
  440. long it will take to send a packet given its size.
  441. */
  442. static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
  443. {
  444. int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
  445. if (slot < 0)
  446. slot = 0;
  447. slot >>= rtab->rate.cell_log;
  448. if (slot > 255)
  449. return (rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF]);
  450. return rtab->data[slot];
  451. }
  452. #ifdef CONFIG_NET_CLS_ACT
  453. static inline struct sk_buff *skb_act_clone(struct sk_buff *skb, gfp_t gfp_mask)
  454. {
  455. struct sk_buff *n = skb_clone(skb, gfp_mask);
  456. if (n) {
  457. n->tc_verd = SET_TC_VERD(n->tc_verd, 0);
  458. n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
  459. n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
  460. }
  461. return n;
  462. }
  463. #endif
  464. #endif