sch_generic.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. struct tc_ratespec rate;
  17. u32 data[256];
  18. struct qdisc_rate_table *next;
  19. int refcnt;
  20. };
  21. enum qdisc_state_t {
  22. __QDISC_STATE_RUNNING,
  23. __QDISC_STATE_SCHED,
  24. __QDISC_STATE_DEACTIVATED,
  25. };
  26. struct qdisc_size_table {
  27. struct list_head list;
  28. struct tc_sizespec szopts;
  29. int refcnt;
  30. u16 data[];
  31. };
  32. struct Qdisc {
  33. int (*enqueue)(struct sk_buff *skb, struct Qdisc *dev);
  34. struct sk_buff * (*dequeue)(struct Qdisc *dev);
  35. unsigned flags;
  36. #define TCQ_F_BUILTIN 1
  37. #define TCQ_F_THROTTLED 2
  38. #define TCQ_F_INGRESS 4
  39. #define TCQ_F_CAN_BYPASS 8
  40. #define TCQ_F_MQROOT 16
  41. #define TCQ_F_WARN_NONWC (1 << 16)
  42. int padded;
  43. struct Qdisc_ops *ops;
  44. struct qdisc_size_table *stab;
  45. struct list_head list;
  46. u32 handle;
  47. u32 parent;
  48. atomic_t refcnt;
  49. struct gnet_stats_rate_est rate_est;
  50. int (*reshape_fail)(struct sk_buff *skb,
  51. struct Qdisc *q);
  52. void *u32_node;
  53. /* This field is deprecated, but it is still used by CBQ
  54. * and it will live until better solution will be invented.
  55. */
  56. struct Qdisc *__parent;
  57. struct netdev_queue *dev_queue;
  58. struct Qdisc *next_sched;
  59. struct sk_buff *gso_skb;
  60. /*
  61. * For performance sake on SMP, we put highly modified fields at the end
  62. */
  63. unsigned long state;
  64. struct sk_buff_head q;
  65. struct gnet_stats_basic_packed bstats;
  66. struct gnet_stats_queue qstats;
  67. };
  68. struct Qdisc_class_ops {
  69. /* Child qdisc manipulation */
  70. struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *);
  71. int (*graft)(struct Qdisc *, unsigned long cl,
  72. struct Qdisc *, struct Qdisc **);
  73. struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl);
  74. void (*qlen_notify)(struct Qdisc *, unsigned long);
  75. /* Class manipulation routines */
  76. unsigned long (*get)(struct Qdisc *, u32 classid);
  77. void (*put)(struct Qdisc *, unsigned long);
  78. int (*change)(struct Qdisc *, u32, u32,
  79. struct nlattr **, unsigned long *);
  80. int (*delete)(struct Qdisc *, unsigned long);
  81. void (*walk)(struct Qdisc *, struct qdisc_walker * arg);
  82. /* Filter manipulation */
  83. struct tcf_proto ** (*tcf_chain)(struct Qdisc *, unsigned long);
  84. unsigned long (*bind_tcf)(struct Qdisc *, unsigned long,
  85. u32 classid);
  86. void (*unbind_tcf)(struct Qdisc *, unsigned long);
  87. /* rtnetlink specific */
  88. int (*dump)(struct Qdisc *, unsigned long,
  89. struct sk_buff *skb, struct tcmsg*);
  90. int (*dump_stats)(struct Qdisc *, unsigned long,
  91. struct gnet_dump *);
  92. };
  93. struct Qdisc_ops {
  94. struct Qdisc_ops *next;
  95. const struct Qdisc_class_ops *cl_ops;
  96. char id[IFNAMSIZ];
  97. int priv_size;
  98. int (*enqueue)(struct sk_buff *, struct Qdisc *);
  99. struct sk_buff * (*dequeue)(struct Qdisc *);
  100. struct sk_buff * (*peek)(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. void (*attach)(struct Qdisc *);
  107. int (*dump)(struct Qdisc *, struct sk_buff *);
  108. int (*dump_stats)(struct Qdisc *, struct gnet_dump *);
  109. struct module *owner;
  110. };
  111. struct tcf_result {
  112. unsigned long class;
  113. u32 classid;
  114. };
  115. struct tcf_proto_ops {
  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. /* 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 int qdisc_qlen(struct Qdisc *q)
  153. {
  154. return q->q.qlen;
  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. extern struct Qdisc_ops pfifo_fast_ops;
  212. extern struct Qdisc_ops mq_qdisc_ops;
  213. struct Qdisc_class_common {
  214. u32 classid;
  215. struct hlist_node hnode;
  216. };
  217. struct Qdisc_class_hash {
  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 struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue,
  252. struct Qdisc *qdisc);
  253. extern void qdisc_reset(struct Qdisc *qdisc);
  254. extern void qdisc_destroy(struct Qdisc *qdisc);
  255. extern void qdisc_tree_decrease_qlen(struct Qdisc *qdisc, unsigned int n);
  256. extern struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue,
  257. struct Qdisc_ops *ops);
  258. extern struct Qdisc *qdisc_create_dflt(struct net_device *dev,
  259. struct netdev_queue *dev_queue,
  260. struct Qdisc_ops *ops, u32 parentid);
  261. extern void qdisc_calculate_pkt_len(struct sk_buff *skb,
  262. struct qdisc_size_table *stab);
  263. extern void tcf_destroy(struct tcf_proto *tp);
  264. extern void tcf_destroy_chain(struct tcf_proto **fl);
  265. /* Reset all TX qdiscs of a device. */
  266. static inline void qdisc_reset_all_tx(struct net_device *dev)
  267. {
  268. unsigned int i;
  269. for (i = 0; i < dev->num_tx_queues; i++)
  270. qdisc_reset(netdev_get_tx_queue(dev, i)->qdisc);
  271. }
  272. /* Are all TX queues of the device empty? */
  273. static inline bool qdisc_all_tx_empty(const 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. const struct Qdisc *q = txq->qdisc;
  279. if (q->q.qlen)
  280. return false;
  281. }
  282. return true;
  283. }
  284. /* Are any of the TX qdiscs changing? */
  285. static inline bool qdisc_tx_changing(struct net_device *dev)
  286. {
  287. unsigned int i;
  288. for (i = 0; i < dev->num_tx_queues; i++) {
  289. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  290. if (txq->qdisc != txq->qdisc_sleeping)
  291. return true;
  292. }
  293. return false;
  294. }
  295. /* Is the device using the noop qdisc on all queues? */
  296. static inline bool qdisc_tx_is_noop(const struct net_device *dev)
  297. {
  298. unsigned int i;
  299. for (i = 0; i < dev->num_tx_queues; i++) {
  300. struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
  301. if (txq->qdisc != &noop_qdisc)
  302. return false;
  303. }
  304. return true;
  305. }
  306. static inline unsigned int qdisc_pkt_len(struct sk_buff *skb)
  307. {
  308. return qdisc_skb_cb(skb)->pkt_len;
  309. }
  310. /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */
  311. enum net_xmit_qdisc_t {
  312. __NET_XMIT_STOLEN = 0x00010000,
  313. __NET_XMIT_BYPASS = 0x00020000,
  314. };
  315. #ifdef CONFIG_NET_CLS_ACT
  316. #define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1)
  317. #else
  318. #define net_xmit_drop_count(e) (1)
  319. #endif
  320. static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch)
  321. {
  322. #ifdef CONFIG_NET_SCHED
  323. if (sch->stab)
  324. qdisc_calculate_pkt_len(skb, sch->stab);
  325. #endif
  326. return sch->enqueue(skb, sch);
  327. }
  328. static inline int qdisc_enqueue_root(struct sk_buff *skb, struct Qdisc *sch)
  329. {
  330. qdisc_skb_cb(skb)->pkt_len = skb->len;
  331. return qdisc_enqueue(skb, sch) & NET_XMIT_MASK;
  332. }
  333. static inline void __qdisc_update_bstats(struct Qdisc *sch, unsigned int len)
  334. {
  335. sch->bstats.bytes += len;
  336. sch->bstats.packets++;
  337. }
  338. static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch,
  339. struct sk_buff_head *list)
  340. {
  341. __skb_queue_tail(list, skb);
  342. sch->qstats.backlog += qdisc_pkt_len(skb);
  343. __qdisc_update_bstats(sch, qdisc_pkt_len(skb));
  344. return NET_XMIT_SUCCESS;
  345. }
  346. static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch)
  347. {
  348. return __qdisc_enqueue_tail(skb, sch, &sch->q);
  349. }
  350. static inline struct sk_buff *__qdisc_dequeue_head(struct Qdisc *sch,
  351. struct sk_buff_head *list)
  352. {
  353. struct sk_buff *skb = __skb_dequeue(list);
  354. if (likely(skb != NULL))
  355. sch->qstats.backlog -= qdisc_pkt_len(skb);
  356. return skb;
  357. }
  358. static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch)
  359. {
  360. return __qdisc_dequeue_head(sch, &sch->q);
  361. }
  362. static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch,
  363. struct sk_buff_head *list)
  364. {
  365. struct sk_buff *skb = __qdisc_dequeue_head(sch, list);
  366. if (likely(skb != NULL)) {
  367. unsigned int len = qdisc_pkt_len(skb);
  368. kfree_skb(skb);
  369. return len;
  370. }
  371. return 0;
  372. }
  373. static inline unsigned int qdisc_queue_drop_head(struct Qdisc *sch)
  374. {
  375. return __qdisc_queue_drop_head(sch, &sch->q);
  376. }
  377. static inline struct sk_buff *__qdisc_dequeue_tail(struct Qdisc *sch,
  378. struct sk_buff_head *list)
  379. {
  380. struct sk_buff *skb = __skb_dequeue_tail(list);
  381. if (likely(skb != NULL))
  382. sch->qstats.backlog -= qdisc_pkt_len(skb);
  383. return skb;
  384. }
  385. static inline struct sk_buff *qdisc_dequeue_tail(struct Qdisc *sch)
  386. {
  387. return __qdisc_dequeue_tail(sch, &sch->q);
  388. }
  389. static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch)
  390. {
  391. return skb_peek(&sch->q);
  392. }
  393. /* generic pseudo peek method for non-work-conserving qdisc */
  394. static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch)
  395. {
  396. /* we can reuse ->gso_skb because peek isn't called for root qdiscs */
  397. if (!sch->gso_skb) {
  398. sch->gso_skb = sch->dequeue(sch);
  399. if (sch->gso_skb)
  400. /* it's still part of the queue */
  401. sch->q.qlen++;
  402. }
  403. return sch->gso_skb;
  404. }
  405. /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */
  406. static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch)
  407. {
  408. struct sk_buff *skb = sch->gso_skb;
  409. if (skb) {
  410. sch->gso_skb = NULL;
  411. sch->q.qlen--;
  412. } else {
  413. skb = sch->dequeue(sch);
  414. }
  415. return skb;
  416. }
  417. static inline void __qdisc_reset_queue(struct Qdisc *sch,
  418. struct sk_buff_head *list)
  419. {
  420. /*
  421. * We do not know the backlog in bytes of this list, it
  422. * is up to the caller to correct it
  423. */
  424. __skb_queue_purge(list);
  425. }
  426. static inline void qdisc_reset_queue(struct Qdisc *sch)
  427. {
  428. __qdisc_reset_queue(sch, &sch->q);
  429. sch->qstats.backlog = 0;
  430. }
  431. static inline unsigned int __qdisc_queue_drop(struct Qdisc *sch,
  432. struct sk_buff_head *list)
  433. {
  434. struct sk_buff *skb = __qdisc_dequeue_tail(sch, list);
  435. if (likely(skb != NULL)) {
  436. unsigned int len = qdisc_pkt_len(skb);
  437. kfree_skb(skb);
  438. return len;
  439. }
  440. return 0;
  441. }
  442. static inline unsigned int qdisc_queue_drop(struct Qdisc *sch)
  443. {
  444. return __qdisc_queue_drop(sch, &sch->q);
  445. }
  446. static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch)
  447. {
  448. kfree_skb(skb);
  449. sch->qstats.drops++;
  450. return NET_XMIT_DROP;
  451. }
  452. static inline int qdisc_reshape_fail(struct sk_buff *skb, struct Qdisc *sch)
  453. {
  454. sch->qstats.drops++;
  455. #ifdef CONFIG_NET_CLS_ACT
  456. if (sch->reshape_fail == NULL || sch->reshape_fail(skb, sch))
  457. goto drop;
  458. return NET_XMIT_SUCCESS;
  459. drop:
  460. #endif
  461. kfree_skb(skb);
  462. return NET_XMIT_DROP;
  463. }
  464. /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how
  465. long it will take to send a packet given its size.
  466. */
  467. static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen)
  468. {
  469. int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead;
  470. if (slot < 0)
  471. slot = 0;
  472. slot >>= rtab->rate.cell_log;
  473. if (slot > 255)
  474. return (rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF]);
  475. return rtab->data[slot];
  476. }
  477. #ifdef CONFIG_NET_CLS_ACT
  478. static inline struct sk_buff *skb_act_clone(struct sk_buff *skb, gfp_t gfp_mask)
  479. {
  480. struct sk_buff *n = skb_clone(skb, gfp_mask);
  481. if (n) {
  482. n->tc_verd = SET_TC_VERD(n->tc_verd, 0);
  483. n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd);
  484. n->tc_verd = CLR_TC_MUNGED(n->tc_verd);
  485. }
  486. return n;
  487. }
  488. #endif
  489. #endif