sch_generic.h 14 KB

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