sch_generic.h 14 KB

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