sch_generic.h 13 KB

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