sch_generic.h 14 KB

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