sch_generic.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. };
  27. struct qdisc_size_table {
  28. struct list_head list;
  29. struct tc_sizespec szopts;
  30. int refcnt;
  31. u16 data[];
  32. };
  33. struct Qdisc
  34. {
  35. int (*enqueue)(struct sk_buff *skb, struct Qdisc *dev);
  36. struct sk_buff * (*dequeue)(struct Qdisc *dev);
  37. unsigned flags;
  38. #define TCQ_F_BUILTIN 1
  39. #define TCQ_F_THROTTLED 2
  40. #define TCQ_F_INGRESS 4
  41. int padded;
  42. struct Qdisc_ops *ops;
  43. struct qdisc_size_table *stab;
  44. u32 handle;
  45. u32 parent;
  46. atomic_t refcnt;
  47. unsigned long state;
  48. struct sk_buff *gso_skb;
  49. struct sk_buff_head q;
  50. struct netdev_queue *dev_queue;
  51. struct Qdisc *next_sched;
  52. struct list_head list;
  53. struct gnet_stats_basic bstats;
  54. struct gnet_stats_queue qstats;
  55. struct gnet_stats_rate_est rate_est;
  56. struct rcu_head q_rcu;
  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. /* The qdisc root lock is a mechanism by which to top level
  165. * of a qdisc tree can be locked from any qdisc node in the
  166. * forest. This allows changing the configuration of some
  167. * aspect of the qdisc tree while blocking out asynchronous
  168. * qdisc access in the packet processing paths.
  169. *
  170. * It is only legal to do this when the root will not change
  171. * on us. Otherwise we'll potentially lock the wrong qdisc
  172. * root. This is enforced by holding the RTNL semaphore, which
  173. * all users of this lock accessor must do.
  174. */
  175. static inline spinlock_t *qdisc_root_lock(struct Qdisc *qdisc)
  176. {
  177. struct Qdisc *root = qdisc_root(qdisc);
  178. ASSERT_RTNL();
  179. return qdisc_lock(root);
  180. }
  181. static inline struct net_device *qdisc_dev(struct Qdisc *qdisc)
  182. {
  183. return qdisc->dev_queue->dev;
  184. }
  185. static inline void sch_tree_lock(struct Qdisc *q)
  186. {
  187. spin_lock_bh(qdisc_root_lock(q));
  188. }
  189. static inline void sch_tree_unlock(struct Qdisc *q)
  190. {
  191. spin_unlock_bh(qdisc_root_lock(q));
  192. }
  193. #define tcf_tree_lock(tp) sch_tree_lock((tp)->q)
  194. #define tcf_tree_unlock(tp) sch_tree_unlock((tp)->q)
  195. extern struct Qdisc noop_qdisc;
  196. extern struct Qdisc_ops noop_qdisc_ops;
  197. struct Qdisc_class_common
  198. {
  199. u32 classid;
  200. struct hlist_node hnode;
  201. };
  202. struct Qdisc_class_hash
  203. {
  204. struct hlist_head *hash;
  205. unsigned int hashsize;
  206. unsigned int hashmask;
  207. unsigned int hashelems;
  208. };
  209. static inline unsigned int qdisc_class_hash(u32 id, u32 mask)
  210. {
  211. id ^= id >> 8;
  212. id ^= id >> 4;
  213. return id & mask;
  214. }
  215. static inline struct Qdisc_class_common *
  216. qdisc_class_find(struct Qdisc_class_hash *hash, u32 id)
  217. {
  218. struct Qdisc_class_common *cl;
  219. struct hlist_node *n;
  220. unsigned int h;
  221. h = qdisc_class_hash(id, hash->hashmask);
  222. hlist_for_each_entry(cl, n, &hash->hash[h], hnode) {
  223. if (cl->classid == id)
  224. return cl;
  225. }
  226. return NULL;
  227. }
  228. extern int qdisc_class_hash_init(struct Qdisc_class_hash *);
  229. extern void qdisc_class_hash_insert(struct Qdisc_class_hash *, struct Qdisc_class_common *);
  230. extern void qdisc_class_hash_remove(struct Qdisc_class_hash *, struct Qdisc_class_common *);
  231. extern void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *);
  232. extern void qdisc_class_hash_destroy(struct Qdisc_class_hash *);
  233. extern void dev_init_scheduler(struct net_device *dev);
  234. extern void dev_shutdown(struct net_device *dev);
  235. extern void dev_activate(struct net_device *dev);
  236. extern void dev_deactivate(struct net_device *dev);
  237. extern void qdisc_reset(struct Qdisc *qdisc);
  238. extern void qdisc_destroy(struct Qdisc *qdisc);
  239. extern void qdisc_tree_decrease_qlen(struct Qdisc *qdisc, unsigned int n);
  240. extern struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
  241. struct Qdisc_ops *ops);
  242. extern struct Qdisc *qdisc_create_dflt(struct net_device *dev,
  243. struct netdev_queue *dev_queue,
  244. struct Qdisc_ops *ops, u32 parentid);
  245. extern void qdisc_calculate_pkt_len(struct sk_buff *skb,
  246. struct qdisc_size_table *stab);
  247. extern void tcf_destroy(struct tcf_proto *tp);
  248. extern void tcf_destroy_chain(struct tcf_proto **fl);
  249. /* Reset all TX qdiscs of a device. */
  250. static inline void qdisc_reset_all_tx(struct net_device *dev)
  251. {
  252. unsigned int i;
  253. for (i = 0; i < dev->num_tx_queues; i++)
  254. qdisc_reset(netdev_get_tx_queue(dev, i)->qdisc);
  255. }
  256. /* Are all TX queues of the device empty? */
  257. static inline bool qdisc_all_tx_empty(const struct net_device *dev)
  258. {
  259. unsigned int i;
  260. for (i = 0; i < dev->num_tx_queues; i++) {
  261. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  262. const struct Qdisc *q = txq->qdisc;
  263. if (q->q.qlen)
  264. return false;
  265. }
  266. return true;
  267. }
  268. /* Are any of the TX qdiscs changing? */
  269. static inline bool qdisc_tx_changing(struct net_device *dev)
  270. {
  271. unsigned int i;
  272. for (i = 0; i < dev->num_tx_queues; i++) {
  273. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  274. if (txq->qdisc != txq->qdisc_sleeping)
  275. return true;
  276. }
  277. return false;
  278. }
  279. /* Is the device using the noop qdisc on all queues? */
  280. static inline bool qdisc_tx_is_noop(const 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 != &noop_qdisc)
  286. return false;
  287. }
  288. return true;
  289. }
  290. static inline unsigned int qdisc_pkt_len(struct sk_buff *skb)
  291. {
  292. return qdisc_skb_cb(skb)->pkt_len;
  293. }
  294. /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
  295. enum net_xmit_qdisc_t {
  296. __NET_XMIT_STOLEN = 0x00010000,
  297. __NET_XMIT_BYPASS = 0x00020000,
  298. };
  299. #ifdef CONFIG_NET_CLS_ACT
  300. #define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
  301. #else
  302. #define net_xmit_drop_count(e) (1)
  303. #endif
  304. static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  305. {
  306. #ifdef CONFIG_NET_SCHED
  307. if (sch->stab)
  308. qdisc_calculate_pkt_len(skb, sch->stab);
  309. #endif
  310. return sch->enqueue(skb, sch);
  311. }
  312. static inline int qdisc_enqueue_root(struct sk_buff *skb, struct Qdisc *sch)
  313. {
  314. qdisc_skb_cb(skb)->pkt_len = skb->len;
  315. return qdisc_enqueue(skb, sch) & NET_XMIT_MASK;
  316. }
  317. static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
  318. struct sk_buff_head *list)
  319. {
  320. __skb_queue_tail(list, skb);
  321. sch->qstats.backlog += qdisc_pkt_len(skb);
  322. sch->bstats.bytes += qdisc_pkt_len(skb);
  323. sch->bstats.packets++;
  324. return NET_XMIT_SUCCESS;
  325. }
  326. static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
  327. {
  328. return __qdisc_enqueue_tail(skb, sch, &sch->q);
  329. }
  330. static inline struct sk_buff *__qdisc_dequeue_head(struct Qdisc *sch,
  331. struct sk_buff_head *list)
  332. {
  333. struct sk_buff *skb = __skb_dequeue(list);
  334. if (likely(skb != NULL))
  335. sch->qstats.backlog -= qdisc_pkt_len(skb);
  336. return skb;
  337. }
  338. static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
  339. {
  340. return __qdisc_dequeue_head(sch, &sch->q);
  341. }
  342. static inline struct sk_buff *__qdisc_dequeue_tail(struct Qdisc *sch,
  343. struct sk_buff_head *list)
  344. {
  345. struct sk_buff *skb = __skb_dequeue_tail(list);
  346. if (likely(skb != NULL))
  347. sch->qstats.backlog -= qdisc_pkt_len(skb);
  348. return skb;
  349. }
  350. static inline struct sk_buff *qdisc_dequeue_tail(struct Qdisc *sch)
  351. {
  352. return __qdisc_dequeue_tail(sch, &sch->q);
  353. }
  354. static inline int __qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch,
  355. struct sk_buff_head *list)
  356. {
  357. __skb_queue_head(list, skb);
  358. sch->qstats.backlog += qdisc_pkt_len(skb);
  359. sch->qstats.requeues++;
  360. return NET_XMIT_SUCCESS;
  361. }
  362. static inline int qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch)
  363. {
  364. return __qdisc_requeue(skb, sch, &sch->q);
  365. }
  366. static inline void __qdisc_reset_queue(struct Qdisc *sch,
  367. struct sk_buff_head *list)
  368. {
  369. /*
  370. * We do not know the backlog in bytes of this list, it
  371. * is up to the caller to correct it
  372. */
  373. __skb_queue_purge(list);
  374. }
  375. static inline void qdisc_reset_queue(struct Qdisc *sch)
  376. {
  377. __qdisc_reset_queue(sch, &sch->q);
  378. sch->qstats.backlog = 0;
  379. }
  380. static inline unsigned int __qdisc_queue_drop(struct Qdisc *sch,
  381. struct sk_buff_head *list)
  382. {
  383. struct sk_buff *skb = __qdisc_dequeue_tail(sch, list);
  384. if (likely(skb != NULL)) {
  385. unsigned int len = qdisc_pkt_len(skb);
  386. kfree_skb(skb);
  387. return len;
  388. }
  389. return 0;
  390. }
  391. static inline unsigned int qdisc_queue_drop(struct Qdisc *sch)
  392. {
  393. return __qdisc_queue_drop(sch, &sch->q);
  394. }
  395. static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
  396. {
  397. kfree_skb(skb);
  398. sch->qstats.drops++;
  399. return NET_XMIT_DROP;
  400. }
  401. static inline int qdisc_reshape_fail(struct sk_buff *skb, struct Qdisc *sch)
  402. {
  403. sch->qstats.drops++;
  404. #ifdef CONFIG_NET_CLS_ACT
  405. if (sch->reshape_fail == NULL || sch->reshape_fail(skb, sch))
  406. goto drop;
  407. return NET_XMIT_SUCCESS;
  408. drop:
  409. #endif
  410. kfree_skb(skb);
  411. return NET_XMIT_DROP;
  412. }
  413. /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
  414. long it will take to send a packet given its size.
  415. */
  416. static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
  417. {
  418. int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
  419. if (slot < 0)
  420. slot = 0;
  421. slot >>= rtab->rate.cell_log;
  422. if (slot > 255)
  423. return (rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF]);
  424. return rtab->data[slot];
  425. }
  426. #ifdef CONFIG_NET_CLS_ACT
  427. static inline struct sk_buff *skb_act_clone(struct sk_buff *skb, gfp_t gfp_mask)
  428. {
  429. struct sk_buff *n = skb_clone(skb, gfp_mask);
  430. if (n) {
  431. n->tc_verd = SET_TC_VERD(n->tc_verd, 0);
  432. n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
  433. n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
  434. }
  435. return n;
  436. }
  437. #endif
  438. #endif