Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef __NET_SCHED_GENERIC_H |
| 2 | #define __NET_SCHED_GENERIC_H |
| 3 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | #include <linux/netdevice.h> |
| 5 | #include <linux/types.h> |
| 6 | #include <linux/rcupdate.h> |
| 7 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/pkt_sched.h> |
| 9 | #include <linux/pkt_cls.h> |
| 10 | #include <net/gen_stats.h> |
Thomas Graf | be577dd | 2007-03-22 11:55:50 -0700 | [diff] [blame] | 11 | #include <net/rtnetlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
| 13 | struct Qdisc_ops; |
| 14 | struct qdisc_walker; |
| 15 | struct tcf_walker; |
| 16 | struct module; |
| 17 | |
| 18 | struct qdisc_rate_table |
| 19 | { |
| 20 | struct tc_ratespec rate; |
| 21 | u32 data[256]; |
| 22 | struct qdisc_rate_table *next; |
| 23 | int refcnt; |
| 24 | }; |
| 25 | |
| 26 | struct Qdisc |
| 27 | { |
| 28 | int (*enqueue)(struct sk_buff *skb, struct Qdisc *dev); |
| 29 | struct sk_buff * (*dequeue)(struct Qdisc *dev); |
| 30 | unsigned flags; |
| 31 | #define TCQ_F_BUILTIN 1 |
| 32 | #define TCQ_F_THROTTLED 2 |
| 33 | #define TCQ_F_INGRESS 4 |
| 34 | int padded; |
| 35 | struct Qdisc_ops *ops; |
| 36 | u32 handle; |
| 37 | u32 parent; |
| 38 | atomic_t refcnt; |
| 39 | struct sk_buff_head q; |
David S. Miller | bb949fb | 2008-07-08 16:55:56 -0700 | [diff] [blame^] | 40 | struct netdev_queue *dev_queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | struct net_device *dev; |
| 42 | struct list_head list; |
| 43 | |
| 44 | struct gnet_stats_basic bstats; |
| 45 | struct gnet_stats_queue qstats; |
| 46 | struct gnet_stats_rate_est rate_est; |
| 47 | spinlock_t *stats_lock; |
| 48 | struct rcu_head q_rcu; |
| 49 | int (*reshape_fail)(struct sk_buff *skb, |
| 50 | struct Qdisc *q); |
| 51 | |
| 52 | /* This field is deprecated, but it is still used by CBQ |
| 53 | * and it will live until better solution will be invented. |
| 54 | */ |
| 55 | struct Qdisc *__parent; |
| 56 | }; |
| 57 | |
| 58 | struct Qdisc_class_ops |
| 59 | { |
| 60 | /* Child qdisc manipulation */ |
| 61 | int (*graft)(struct Qdisc *, unsigned long cl, |
| 62 | struct Qdisc *, struct Qdisc **); |
| 63 | struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl); |
Patrick McHardy | 43effa1 | 2006-11-29 17:35:48 -0800 | [diff] [blame] | 64 | void (*qlen_notify)(struct Qdisc *, unsigned long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | /* Class manipulation routines */ |
| 67 | unsigned long (*get)(struct Qdisc *, u32 classid); |
| 68 | void (*put)(struct Qdisc *, unsigned long); |
| 69 | int (*change)(struct Qdisc *, u32, u32, |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 70 | struct nlattr **, unsigned long *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | int (*delete)(struct Qdisc *, unsigned long); |
| 72 | void (*walk)(struct Qdisc *, struct qdisc_walker * arg); |
| 73 | |
| 74 | /* Filter manipulation */ |
| 75 | struct tcf_proto ** (*tcf_chain)(struct Qdisc *, unsigned long); |
| 76 | unsigned long (*bind_tcf)(struct Qdisc *, unsigned long, |
| 77 | u32 classid); |
| 78 | void (*unbind_tcf)(struct Qdisc *, unsigned long); |
| 79 | |
| 80 | /* rtnetlink specific */ |
| 81 | int (*dump)(struct Qdisc *, unsigned long, |
| 82 | struct sk_buff *skb, struct tcmsg*); |
| 83 | int (*dump_stats)(struct Qdisc *, unsigned long, |
| 84 | struct gnet_dump *); |
| 85 | }; |
| 86 | |
| 87 | struct Qdisc_ops |
| 88 | { |
| 89 | struct Qdisc_ops *next; |
Eric Dumazet | 20fea08 | 2007-11-14 01:44:41 -0800 | [diff] [blame] | 90 | const struct Qdisc_class_ops *cl_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | char id[IFNAMSIZ]; |
| 92 | int priv_size; |
| 93 | |
| 94 | int (*enqueue)(struct sk_buff *, struct Qdisc *); |
| 95 | struct sk_buff * (*dequeue)(struct Qdisc *); |
| 96 | int (*requeue)(struct sk_buff *, struct Qdisc *); |
| 97 | unsigned int (*drop)(struct Qdisc *); |
| 98 | |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 99 | int (*init)(struct Qdisc *, struct nlattr *arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | void (*reset)(struct Qdisc *); |
| 101 | void (*destroy)(struct Qdisc *); |
Patrick McHardy | 1e90474 | 2008-01-22 22:11:17 -0800 | [diff] [blame] | 102 | int (*change)(struct Qdisc *, struct nlattr *arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
| 104 | int (*dump)(struct Qdisc *, struct sk_buff *); |
| 105 | int (*dump_stats)(struct Qdisc *, struct gnet_dump *); |
| 106 | |
| 107 | struct module *owner; |
| 108 | }; |
| 109 | |
| 110 | |
| 111 | struct tcf_result |
| 112 | { |
| 113 | unsigned long class; |
| 114 | u32 classid; |
| 115 | }; |
| 116 | |
| 117 | struct tcf_proto_ops |
| 118 | { |
| 119 | struct tcf_proto_ops *next; |
| 120 | char kind[IFNAMSIZ]; |
| 121 | |
| 122 | int (*classify)(struct sk_buff*, struct tcf_proto*, |
| 123 | struct tcf_result *); |
| 124 | int (*init)(struct tcf_proto*); |
| 125 | void (*destroy)(struct tcf_proto*); |
| 126 | |
| 127 | unsigned long (*get)(struct tcf_proto*, u32 handle); |
| 128 | void (*put)(struct tcf_proto*, unsigned long); |
| 129 | int (*change)(struct tcf_proto*, unsigned long, |
Patrick McHardy | add93b6 | 2008-01-22 22:11:33 -0800 | [diff] [blame] | 130 | u32 handle, struct nlattr **, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | unsigned long *); |
| 132 | int (*delete)(struct tcf_proto*, unsigned long); |
| 133 | void (*walk)(struct tcf_proto*, struct tcf_walker *arg); |
| 134 | |
| 135 | /* rtnetlink specific */ |
| 136 | int (*dump)(struct tcf_proto*, unsigned long, |
| 137 | struct sk_buff *skb, struct tcmsg*); |
| 138 | |
| 139 | struct module *owner; |
| 140 | }; |
| 141 | |
| 142 | struct tcf_proto |
| 143 | { |
| 144 | /* Fast access part */ |
| 145 | struct tcf_proto *next; |
| 146 | void *root; |
| 147 | int (*classify)(struct sk_buff*, struct tcf_proto*, |
| 148 | struct tcf_result *); |
Al Viro | 66c6f52 | 2006-11-20 18:07:51 -0800 | [diff] [blame] | 149 | __be16 protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
| 151 | /* All the rest */ |
| 152 | u32 prio; |
| 153 | u32 classid; |
| 154 | struct Qdisc *q; |
| 155 | void *data; |
| 156 | struct tcf_proto_ops *ops; |
| 157 | }; |
| 158 | |
| 159 | |
| 160 | extern void qdisc_lock_tree(struct net_device *dev); |
| 161 | extern void qdisc_unlock_tree(struct net_device *dev); |
| 162 | |
| 163 | #define sch_tree_lock(q) qdisc_lock_tree((q)->dev) |
| 164 | #define sch_tree_unlock(q) qdisc_unlock_tree((q)->dev) |
| 165 | #define tcf_tree_lock(tp) qdisc_lock_tree((tp)->q->dev) |
| 166 | #define tcf_tree_unlock(tp) qdisc_unlock_tree((tp)->q->dev) |
| 167 | |
Thomas Graf | e41a33e | 2005-07-05 14:14:30 -0700 | [diff] [blame] | 168 | extern struct Qdisc noop_qdisc; |
| 169 | extern struct Qdisc_ops noop_qdisc_ops; |
| 170 | |
Patrick McHardy | 6fe1c7a | 2008-07-05 23:21:31 -0700 | [diff] [blame] | 171 | struct Qdisc_class_common |
| 172 | { |
| 173 | u32 classid; |
| 174 | struct hlist_node hnode; |
| 175 | }; |
| 176 | |
| 177 | struct Qdisc_class_hash |
| 178 | { |
| 179 | struct hlist_head *hash; |
| 180 | unsigned int hashsize; |
| 181 | unsigned int hashmask; |
| 182 | unsigned int hashelems; |
| 183 | }; |
| 184 | |
| 185 | static inline unsigned int qdisc_class_hash(u32 id, u32 mask) |
| 186 | { |
| 187 | id ^= id >> 8; |
| 188 | id ^= id >> 4; |
| 189 | return id & mask; |
| 190 | } |
| 191 | |
| 192 | static inline struct Qdisc_class_common * |
| 193 | qdisc_class_find(struct Qdisc_class_hash *hash, u32 id) |
| 194 | { |
| 195 | struct Qdisc_class_common *cl; |
| 196 | struct hlist_node *n; |
| 197 | unsigned int h; |
| 198 | |
| 199 | h = qdisc_class_hash(id, hash->hashmask); |
| 200 | hlist_for_each_entry(cl, n, &hash->hash[h], hnode) { |
| 201 | if (cl->classid == id) |
| 202 | return cl; |
| 203 | } |
| 204 | return NULL; |
| 205 | } |
| 206 | |
| 207 | extern int qdisc_class_hash_init(struct Qdisc_class_hash *); |
| 208 | extern void qdisc_class_hash_insert(struct Qdisc_class_hash *, struct Qdisc_class_common *); |
| 209 | extern void qdisc_class_hash_remove(struct Qdisc_class_hash *, struct Qdisc_class_common *); |
| 210 | extern void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *); |
| 211 | extern void qdisc_class_hash_destroy(struct Qdisc_class_hash *); |
| 212 | |
Thomas Graf | e41a33e | 2005-07-05 14:14:30 -0700 | [diff] [blame] | 213 | extern void dev_init_scheduler(struct net_device *dev); |
| 214 | extern void dev_shutdown(struct net_device *dev); |
| 215 | extern void dev_activate(struct net_device *dev); |
| 216 | extern void dev_deactivate(struct net_device *dev); |
| 217 | extern void qdisc_reset(struct Qdisc *qdisc); |
| 218 | extern void qdisc_destroy(struct Qdisc *qdisc); |
Patrick McHardy | 43effa1 | 2006-11-29 17:35:48 -0800 | [diff] [blame] | 219 | extern void qdisc_tree_decrease_qlen(struct Qdisc *qdisc, unsigned int n); |
David S. Miller | bb949fb | 2008-07-08 16:55:56 -0700 | [diff] [blame^] | 220 | extern struct Qdisc *qdisc_alloc(struct net_device *dev, |
| 221 | struct netdev_queue *dev_queue, |
| 222 | struct Qdisc_ops *ops); |
Thomas Graf | e41a33e | 2005-07-05 14:14:30 -0700 | [diff] [blame] | 223 | extern struct Qdisc *qdisc_create_dflt(struct net_device *dev, |
David S. Miller | bb949fb | 2008-07-08 16:55:56 -0700 | [diff] [blame^] | 224 | struct netdev_queue *dev_queue, |
Patrick McHardy | 9f9afec | 2006-11-29 17:35:18 -0800 | [diff] [blame] | 225 | struct Qdisc_ops *ops, u32 parentid); |
Patrick McHardy | a48b5a6 | 2007-03-23 11:29:43 -0700 | [diff] [blame] | 226 | extern void tcf_destroy(struct tcf_proto *tp); |
Patrick McHardy | ff31ab5 | 2008-07-01 19:52:38 -0700 | [diff] [blame] | 227 | extern void tcf_destroy_chain(struct tcf_proto **fl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Thomas Graf | 9972b25 | 2005-06-18 22:57:26 -0700 | [diff] [blame] | 229 | static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch, |
| 230 | struct sk_buff_head *list) |
| 231 | { |
| 232 | __skb_queue_tail(list, skb); |
| 233 | sch->qstats.backlog += skb->len; |
| 234 | sch->bstats.bytes += skb->len; |
| 235 | sch->bstats.packets++; |
| 236 | |
| 237 | return NET_XMIT_SUCCESS; |
| 238 | } |
| 239 | |
| 240 | static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch) |
| 241 | { |
| 242 | return __qdisc_enqueue_tail(skb, sch, &sch->q); |
| 243 | } |
| 244 | |
| 245 | static inline struct sk_buff *__qdisc_dequeue_head(struct Qdisc *sch, |
| 246 | struct sk_buff_head *list) |
| 247 | { |
| 248 | struct sk_buff *skb = __skb_dequeue(list); |
| 249 | |
| 250 | if (likely(skb != NULL)) |
| 251 | sch->qstats.backlog -= skb->len; |
| 252 | |
| 253 | return skb; |
| 254 | } |
| 255 | |
| 256 | static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch) |
| 257 | { |
| 258 | return __qdisc_dequeue_head(sch, &sch->q); |
| 259 | } |
| 260 | |
| 261 | static inline struct sk_buff *__qdisc_dequeue_tail(struct Qdisc *sch, |
| 262 | struct sk_buff_head *list) |
| 263 | { |
| 264 | struct sk_buff *skb = __skb_dequeue_tail(list); |
| 265 | |
| 266 | if (likely(skb != NULL)) |
| 267 | sch->qstats.backlog -= skb->len; |
| 268 | |
| 269 | return skb; |
| 270 | } |
| 271 | |
| 272 | static inline struct sk_buff *qdisc_dequeue_tail(struct Qdisc *sch) |
| 273 | { |
| 274 | return __qdisc_dequeue_tail(sch, &sch->q); |
| 275 | } |
| 276 | |
| 277 | static inline int __qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch, |
| 278 | struct sk_buff_head *list) |
| 279 | { |
| 280 | __skb_queue_head(list, skb); |
| 281 | sch->qstats.backlog += skb->len; |
| 282 | sch->qstats.requeues++; |
| 283 | |
| 284 | return NET_XMIT_SUCCESS; |
| 285 | } |
| 286 | |
| 287 | static inline int qdisc_requeue(struct sk_buff *skb, struct Qdisc *sch) |
| 288 | { |
| 289 | return __qdisc_requeue(skb, sch, &sch->q); |
| 290 | } |
| 291 | |
| 292 | static inline void __qdisc_reset_queue(struct Qdisc *sch, |
| 293 | struct sk_buff_head *list) |
| 294 | { |
| 295 | /* |
| 296 | * We do not know the backlog in bytes of this list, it |
| 297 | * is up to the caller to correct it |
| 298 | */ |
| 299 | skb_queue_purge(list); |
| 300 | } |
| 301 | |
| 302 | static inline void qdisc_reset_queue(struct Qdisc *sch) |
| 303 | { |
| 304 | __qdisc_reset_queue(sch, &sch->q); |
| 305 | sch->qstats.backlog = 0; |
| 306 | } |
| 307 | |
| 308 | static inline unsigned int __qdisc_queue_drop(struct Qdisc *sch, |
| 309 | struct sk_buff_head *list) |
| 310 | { |
| 311 | struct sk_buff *skb = __qdisc_dequeue_tail(sch, list); |
| 312 | |
| 313 | if (likely(skb != NULL)) { |
| 314 | unsigned int len = skb->len; |
| 315 | kfree_skb(skb); |
| 316 | return len; |
| 317 | } |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | static inline unsigned int qdisc_queue_drop(struct Qdisc *sch) |
| 323 | { |
| 324 | return __qdisc_queue_drop(sch, &sch->q); |
| 325 | } |
| 326 | |
| 327 | static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch) |
| 328 | { |
| 329 | kfree_skb(skb); |
| 330 | sch->qstats.drops++; |
| 331 | |
| 332 | return NET_XMIT_DROP; |
| 333 | } |
| 334 | |
| 335 | static inline int qdisc_reshape_fail(struct sk_buff *skb, struct Qdisc *sch) |
| 336 | { |
| 337 | sch->qstats.drops++; |
| 338 | |
Patrick McHardy | c3bc7cf | 2007-07-15 00:03:05 -0700 | [diff] [blame] | 339 | #ifdef CONFIG_NET_CLS_ACT |
Thomas Graf | 9972b25 | 2005-06-18 22:57:26 -0700 | [diff] [blame] | 340 | if (sch->reshape_fail == NULL || sch->reshape_fail(skb, sch)) |
| 341 | goto drop; |
| 342 | |
| 343 | return NET_XMIT_SUCCESS; |
| 344 | |
| 345 | drop: |
| 346 | #endif |
| 347 | kfree_skb(skb); |
| 348 | return NET_XMIT_DROP; |
| 349 | } |
| 350 | |
Jesper Dangaard Brouer | e9bef55 | 2007-09-12 16:35:24 +0200 | [diff] [blame] | 351 | /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how |
| 352 | long it will take to send a packet given its size. |
| 353 | */ |
| 354 | static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen) |
| 355 | { |
Jesper Dangaard Brouer | e08b099 | 2007-09-12 16:36:28 +0200 | [diff] [blame] | 356 | int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead; |
| 357 | if (slot < 0) |
| 358 | slot = 0; |
Jesper Dangaard Brouer | e9bef55 | 2007-09-12 16:35:24 +0200 | [diff] [blame] | 359 | slot >>= rtab->rate.cell_log; |
| 360 | if (slot > 255) |
| 361 | return (rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF]); |
| 362 | return rtab->data[slot]; |
| 363 | } |
| 364 | |
Jamal Hadi Salim | 12da81d | 2007-10-26 02:47:23 -0700 | [diff] [blame] | 365 | #ifdef CONFIG_NET_CLS_ACT |
| 366 | static inline struct sk_buff *skb_act_clone(struct sk_buff *skb, gfp_t gfp_mask) |
| 367 | { |
| 368 | struct sk_buff *n = skb_clone(skb, gfp_mask); |
| 369 | |
| 370 | if (n) { |
| 371 | n->tc_verd = SET_TC_VERD(n->tc_verd, 0); |
| 372 | n->tc_verd = CLR_TC_OK2MUNGE(n->tc_verd); |
| 373 | n->tc_verd = CLR_TC_MUNGED(n->tc_verd); |
Jamal Hadi Salim | 12da81d | 2007-10-26 02:47:23 -0700 | [diff] [blame] | 374 | } |
| 375 | return n; |
| 376 | } |
| 377 | #endif |
| 378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | #endif |