sch_generic.h 14 KB

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