sch_generic.h 13 KB

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