sch_generic.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. int (*requeue)(struct sk_buff *, 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 struct net_device *qdisc_dev(struct Qdisc *qdisc)
  186. {
  187. return qdisc->dev_queue->dev;
  188. }
  189. static inline void sch_tree_lock(struct Qdisc *q)
  190. {
  191. spin_lock_bh(qdisc_root_lock(q));
  192. }
  193. static inline void sch_tree_unlock(struct Qdisc *q)
  194. {
  195. spin_unlock_bh(qdisc_root_lock(q));
  196. }
  197. #define tcf_tree_lock(tp) sch_tree_lock((tp)->q)
  198. #define tcf_tree_unlock(tp) sch_tree_unlock((tp)->q)
  199. extern struct Qdisc noop_qdisc;
  200. extern struct Qdisc_ops noop_qdisc_ops;
  201. struct Qdisc_class_common
  202. {
  203. u32 classid;
  204. struct hlist_node hnode;
  205. };
  206. struct Qdisc_class_hash
  207. {
  208. struct hlist_head *hash;
  209. unsigned int hashsize;
  210. unsigned int hashmask;
  211. unsigned int hashelems;
  212. };
  213. static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
  214. {
  215. id ^= id >> 8;
  216. id ^= id >> 4;
  217. return id & mask;
  218. }
  219. static inline struct Qdisc_class_common *
  220. qdisc_class_find(struct Qdisc_class_hash *hash, u32 id)
  221. {
  222. struct Qdisc_class_common *cl;
  223. struct hlist_node *n;
  224. unsigned int h;
  225. h = qdisc_class_hash(id, hash->hashmask);
  226. hlist_for_each_entry(cl, n, &hash->hash[h], hnode) {
  227. if (cl->classid == id)
  228. return cl;
  229. }
  230. return NULL;
  231. }
  232. extern int qdisc_class_hash_init(struct Qdisc_class_hash *);
  233. extern void qdisc_class_hash_insert(struct Qdisc_class_hash *, struct Qdisc_class_common *);
  234. extern void qdisc_class_hash_remove(struct Qdisc_class_hash *, struct Qdisc_class_common *);
  235. extern void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
  236. extern void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
  237. extern void dev_init_scheduler(struct net_device *dev);
  238. extern void dev_shutdown(struct net_device *dev);
  239. extern void dev_activate(struct net_device *dev);
  240. extern void dev_deactivate(struct net_device *dev);
  241. extern void qdisc_reset(struct Qdisc *qdisc);
  242. extern void qdisc_destroy(struct Qdisc *qdisc);
  243. extern void qdisc_tree_decrease_qlen(struct Qdisc *qdisc, unsigned int n);
  244. extern struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
  245. struct Qdisc_ops *ops);
  246. extern struct Qdisc *qdisc_create_dflt(struct net_device *dev,
  247. struct netdev_queue *dev_queue,
  248. struct Qdisc_ops *ops, u32 parentid);
  249. extern void qdisc_calculate_pkt_len(struct sk_buff *skb,
  250. struct qdisc_size_table *stab);
  251. extern void tcf_destroy(struct tcf_proto *tp);
  252. extern void tcf_destroy_chain(struct tcf_proto **fl);
  253. /* Reset all TX qdiscs of a device. */
  254. static inline void qdisc_reset_all_tx(struct net_device *dev)
  255. {
  256. unsigned int i;
  257. for (i = 0; i < dev->num_tx_queues; i++)
  258. qdisc_reset(netdev_get_tx_queue(dev, i)->qdisc);
  259. }
  260. /* Are all TX queues of the device empty? */
  261. static inline bool qdisc_all_tx_empty(const struct net_device *dev)
  262. {
  263. unsigned int i;
  264. for (i = 0; i < dev->num_tx_queues; i++) {
  265. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  266. const struct Qdisc *q = txq->qdisc;
  267. if (q->q.qlen)
  268. return false;
  269. }
  270. return true;
  271. }
  272. /* Are any of the TX qdiscs changing? */
  273. static inline bool qdisc_tx_changing(struct net_device *dev)
  274. {
  275. unsigned int i;
  276. for (i = 0; i < dev->num_tx_queues; i++) {
  277. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  278. if (txq->qdisc != txq->qdisc_sleeping)
  279. return true;
  280. }
  281. return false;
  282. }
  283. /* Is the device using the noop qdisc on all queues? */
  284. static inline bool qdisc_tx_is_noop(const struct net_device *dev)
  285. {
  286. unsigned int i;
  287. for (i = 0; i < dev->num_tx_queues; i++) {
  288. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  289. if (txq->qdisc != &noop_qdisc)
  290. return false;
  291. }
  292. return true;
  293. }
  294. static inline unsigned int qdisc_pkt_len(struct sk_buff *skb)
  295. {
  296. return qdisc_skb_cb(skb)->pkt_len;
  297. }
  298. /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
  299. enum net_xmit_qdisc_t {
  300. __NET_XMIT_STOLEN = 0x00010000,
  301. __NET_XMIT_BYPASS = 0x00020000,
  302. };
  303. #ifdef CONFIG_NET_CLS_ACT
  304. #define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
  305. #else
  306. #define net_xmit_drop_count(e) (1)
  307. #endif
  308. static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  309. {
  310. #ifdef CONFIG_NET_SCHED
  311. if (sch->stab)
  312. qdisc_calculate_pkt_len(skb, sch->stab);
  313. #endif
  314. return sch->enqueue(skb, sch);
  315. }
  316. static inline int qdisc_enqueue_root(struct sk_buff *skb, struct Qdisc *sch)
  317. {
  318. qdisc_skb_cb(skb)->pkt_len = skb->len;
  319. return qdisc_enqueue(skb, sch) & NET_XMIT_MASK;
  320. }
  321. static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
  322. struct sk_buff_head *list)
  323. {
  324. __skb_queue_tail(list, skb);
  325. sch->qstats.backlog += qdisc_pkt_len(skb);
  326. sch->bstats.bytes += qdisc_pkt_len(skb);
  327. sch->bstats.packets++;
  328. return NET_XMIT_SUCCESS;
  329. }
  330. static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
  331. {
  332. return __qdisc_enqueue_tail(skb, sch, &sch->q);
  333. }
  334. static inline struct sk_buff *__qdisc_dequeue_head(struct Qdisc *sch,
  335. struct sk_buff_head *list)
  336. {
  337. struct sk_buff *skb = __skb_dequeue(list);
  338. if (likely(skb != NULL))
  339. sch->qstats.backlog -= qdisc_pkt_len(skb);
  340. return skb;
  341. }
  342. static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
  343. {
  344. return __qdisc_dequeue_head(sch, &sch->q);
  345. }
  346. static inline struct sk_buff *__qdisc_dequeue_tail(struct Qdisc *sch,
  347. struct sk_buff_head *list)
  348. {
  349. struct sk_buff *skb = __skb_dequeue_tail(list);
  350. if (likely(skb != NULL))
  351. sch->qstats.backlog -= qdisc_pkt_len(skb);
  352. return skb;
  353. }
  354. static inline struct sk_buff *qdisc_dequeue_tail(struct Qdisc *sch)
  355. {
  356. return __qdisc_dequeue_tail(sch, &sch->q);
  357. }
  358. static inline int __qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch,
  359. struct sk_buff_head *list)
  360. {
  361. __skb_queue_head(list, skb);
  362. sch->qstats.backlog += qdisc_pkt_len(skb);
  363. sch->qstats.requeues++;
  364. return NET_XMIT_SUCCESS;
  365. }
  366. static inline int qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch)
  367. {
  368. return __qdisc_requeue(skb, sch, &sch->q);
  369. }
  370. static inline void __qdisc_reset_queue(struct Qdisc *sch,
  371. struct sk_buff_head *list)
  372. {
  373. /*
  374. * We do not know the backlog in bytes of this list, it
  375. * is up to the caller to correct it
  376. */
  377. __skb_queue_purge(list);
  378. }
  379. static inline void qdisc_reset_queue(struct Qdisc *sch)
  380. {
  381. __qdisc_reset_queue(sch, &sch->q);
  382. sch->qstats.backlog = 0;
  383. }
  384. static inline unsigned int __qdisc_queue_drop(struct Qdisc *sch,
  385. struct sk_buff_head *list)
  386. {
  387. struct sk_buff *skb = __qdisc_dequeue_tail(sch, list);
  388. if (likely(skb != NULL)) {
  389. unsigned int len = qdisc_pkt_len(skb);
  390. kfree_skb(skb);
  391. return len;
  392. }
  393. return 0;
  394. }
  395. static inline unsigned int qdisc_queue_drop(struct Qdisc *sch)
  396. {
  397. return __qdisc_queue_drop(sch, &sch->q);
  398. }
  399. static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
  400. {
  401. kfree_skb(skb);
  402. sch->qstats.drops++;
  403. return NET_XMIT_DROP;
  404. }
  405. static inline int qdisc_reshape_fail(struct sk_buff *skb, struct Qdisc *sch)
  406. {
  407. sch->qstats.drops++;
  408. #ifdef CONFIG_NET_CLS_ACT
  409. if (sch->reshape_fail == NULL || sch->reshape_fail(skb, sch))
  410. goto drop;
  411. return NET_XMIT_SUCCESS;
  412. drop:
  413. #endif
  414. kfree_skb(skb);
  415. return NET_XMIT_DROP;
  416. }
  417. /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
  418. long it will take to send a packet given its size.
  419. */
  420. static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
  421. {
  422. int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
  423. if (slot < 0)
  424. slot = 0;
  425. slot >>= rtab->rate.cell_log;
  426. if (slot > 255)
  427. return (rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF]);
  428. return rtab->data[slot];
  429. }
  430. #ifdef CONFIG_NET_CLS_ACT
  431. static inline struct sk_buff *skb_act_clone(struct sk_buff *skb, gfp_t gfp_mask)
  432. {
  433. struct sk_buff *n = skb_clone(skb, gfp_mask);
  434. if (n) {
  435. n->tc_verd = SET_TC_VERD(n->tc_verd, 0);
  436. n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
  437. n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
  438. }
  439. return n;
  440. }
  441. #endif
  442. #endif