Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | #ifndef __NET_SCHED_GENERIC_H |
2 | #define __NET_SCHED_GENERIC_H | |
3 | ||
1da177e4 LT |
4 | #include <linux/netdevice.h> |
5 | #include <linux/types.h> | |
6 | #include <linux/rcupdate.h> | |
1da177e4 LT |
7 | #include <linux/pkt_sched.h> |
8 | #include <linux/pkt_cls.h> | |
22e0f8b9 | 9 | #include <linux/percpu.h> |
5772e9a3 | 10 | #include <linux/dynamic_queue_limits.h> |
1da177e4 | 11 | #include <net/gen_stats.h> |
be577ddc | 12 | #include <net/rtnetlink.h> |
1da177e4 LT |
13 | |
14 | struct Qdisc_ops; | |
15 | struct qdisc_walker; | |
16 | struct tcf_walker; | |
17 | struct module; | |
18 | ||
fd2c3ef7 | 19 | struct qdisc_rate_table { |
1da177e4 LT |
20 | struct tc_ratespec rate; |
21 | u32 data[256]; | |
22 | struct qdisc_rate_table *next; | |
23 | int refcnt; | |
24 | }; | |
25 | ||
fd2c3ef7 | 26 | enum qdisc_state_t { |
37437bb2 | 27 | __QDISC_STATE_SCHED, |
a9312ae8 | 28 | __QDISC_STATE_DEACTIVATED, |
e2627c8c DM |
29 | }; |
30 | ||
175f9c1b | 31 | struct qdisc_size_table { |
a2da570d | 32 | struct rcu_head rcu; |
175f9c1b JK |
33 | struct list_head list; |
34 | struct tc_sizespec szopts; | |
35 | int refcnt; | |
36 | u16 data[]; | |
37 | }; | |
38 | ||
fd2c3ef7 | 39 | struct Qdisc { |
520ac30f ED |
40 | int (*enqueue)(struct sk_buff *skb, |
41 | struct Qdisc *sch, | |
42 | struct sk_buff **to_free); | |
43 | struct sk_buff * (*dequeue)(struct Qdisc *sch); | |
05bdd2f1 | 44 | unsigned int flags; |
b00355db | 45 | #define TCQ_F_BUILTIN 1 |
fd245a4a ED |
46 | #define TCQ_F_INGRESS 2 |
47 | #define TCQ_F_CAN_BYPASS 4 | |
48 | #define TCQ_F_MQROOT 8 | |
1abbe139 ED |
49 | #define TCQ_F_ONETXQUEUE 0x10 /* dequeue_skb() can assume all skbs are for |
50 | * q->dev_queue : It can test | |
51 | * netif_xmit_frozen_or_stopped() before | |
52 | * dequeueing next packet. | |
53 | * Its true for MQ/MQPRIO slaves, or non | |
54 | * multiqueue device. | |
55 | */ | |
b00355db | 56 | #define TCQ_F_WARN_NONWC (1 << 16) |
22e0f8b9 | 57 | #define TCQ_F_CPUSTATS 0x20 /* run using percpu statistics */ |
4eaf3b84 ED |
58 | #define TCQ_F_NOPARENT 0x40 /* root of its hierarchy : |
59 | * qdisc_tree_decrease_qlen() should stop. | |
60 | */ | |
45203a3b | 61 | u32 limit; |
05bdd2f1 | 62 | const struct Qdisc_ops *ops; |
a2da570d | 63 | struct qdisc_size_table __rcu *stab; |
5e140dfc | 64 | struct list_head list; |
1da177e4 LT |
65 | u32 handle; |
66 | u32 parent; | |
72b25a91 DM |
67 | void *u32_node; |
68 | ||
5e140dfc | 69 | struct netdev_queue *dev_queue; |
5e140dfc | 70 | |
45203a3b | 71 | struct gnet_stats_rate_est64 rate_est; |
0d32ef8c ED |
72 | struct gnet_stats_basic_cpu __percpu *cpu_bstats; |
73 | struct gnet_stats_queue __percpu *cpu_qstats; | |
74 | ||
5e140dfc ED |
75 | /* |
76 | * For performance sake on SMP, we put highly modified fields at the end | |
77 | */ | |
c8945043 FW |
78 | struct Qdisc *next_sched ____cacheline_aligned_in_smp; |
79 | struct sk_buff *gso_skb; | |
5e140dfc ED |
80 | unsigned long state; |
81 | struct sk_buff_head q; | |
0d32ef8c | 82 | struct gnet_stats_basic_packed bstats; |
f9eb8aea | 83 | seqcount_t running; |
0d32ef8c | 84 | struct gnet_stats_queue qstats; |
79640a4c | 85 | struct rcu_head rcu_head; |
45203a3b ED |
86 | int padded; |
87 | atomic_t refcnt; | |
88 | ||
89 | spinlock_t busylock ____cacheline_aligned_in_smp; | |
1da177e4 LT |
90 | }; |
91 | ||
fd245a4a | 92 | static inline bool qdisc_is_running(const struct Qdisc *qdisc) |
bc135b23 | 93 | { |
f9eb8aea | 94 | return (raw_read_seqcount(&qdisc->running) & 1) ? true : false; |
bc135b23 ED |
95 | } |
96 | ||
97 | static inline bool qdisc_run_begin(struct Qdisc *qdisc) | |
98 | { | |
fd245a4a ED |
99 | if (qdisc_is_running(qdisc)) |
100 | return false; | |
52fbb290 ED |
101 | /* Variant of write_seqcount_begin() telling lockdep a trylock |
102 | * was attempted. | |
103 | */ | |
104 | raw_write_seqcount_begin(&qdisc->running); | |
105 | seqcount_acquire(&qdisc->running.dep_map, 0, 1, _RET_IP_); | |
fd245a4a | 106 | return true; |
bc135b23 ED |
107 | } |
108 | ||
109 | static inline void qdisc_run_end(struct Qdisc *qdisc) | |
110 | { | |
f9eb8aea | 111 | write_seqcount_end(&qdisc->running); |
fd245a4a ED |
112 | } |
113 | ||
5772e9a3 JDB |
114 | static inline bool qdisc_may_bulk(const struct Qdisc *qdisc) |
115 | { | |
116 | return qdisc->flags & TCQ_F_ONETXQUEUE; | |
117 | } | |
118 | ||
119 | static inline int qdisc_avail_bulklimit(const struct netdev_queue *txq) | |
120 | { | |
121 | #ifdef CONFIG_BQL | |
122 | /* Non-BQL migrated drivers will return 0, too. */ | |
123 | return dql_avail(&txq->dql); | |
124 | #else | |
125 | return 0; | |
126 | #endif | |
127 | } | |
128 | ||
fd2c3ef7 | 129 | struct Qdisc_class_ops { |
1da177e4 | 130 | /* Child qdisc manipulation */ |
926e61b7 | 131 | struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *); |
1da177e4 LT |
132 | int (*graft)(struct Qdisc *, unsigned long cl, |
133 | struct Qdisc *, struct Qdisc **); | |
134 | struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl); | |
43effa1e | 135 | void (*qlen_notify)(struct Qdisc *, unsigned long); |
1da177e4 LT |
136 | |
137 | /* Class manipulation routines */ | |
138 | unsigned long (*get)(struct Qdisc *, u32 classid); | |
139 | void (*put)(struct Qdisc *, unsigned long); | |
140 | int (*change)(struct Qdisc *, u32, u32, | |
1e90474c | 141 | struct nlattr **, unsigned long *); |
1da177e4 LT |
142 | int (*delete)(struct Qdisc *, unsigned long); |
143 | void (*walk)(struct Qdisc *, struct qdisc_walker * arg); | |
144 | ||
145 | /* Filter manipulation */ | |
25d8c0d5 | 146 | struct tcf_proto __rcu ** (*tcf_chain)(struct Qdisc *, unsigned long); |
92c075db | 147 | bool (*tcf_cl_offload)(u32 classid); |
1da177e4 LT |
148 | unsigned long (*bind_tcf)(struct Qdisc *, unsigned long, |
149 | u32 classid); | |
150 | void (*unbind_tcf)(struct Qdisc *, unsigned long); | |
151 | ||
152 | /* rtnetlink specific */ | |
153 | int (*dump)(struct Qdisc *, unsigned long, | |
154 | struct sk_buff *skb, struct tcmsg*); | |
155 | int (*dump_stats)(struct Qdisc *, unsigned long, | |
156 | struct gnet_dump *); | |
157 | }; | |
158 | ||
fd2c3ef7 | 159 | struct Qdisc_ops { |
1da177e4 | 160 | struct Qdisc_ops *next; |
20fea08b | 161 | const struct Qdisc_class_ops *cl_ops; |
1da177e4 LT |
162 | char id[IFNAMSIZ]; |
163 | int priv_size; | |
164 | ||
520ac30f ED |
165 | int (*enqueue)(struct sk_buff *skb, |
166 | struct Qdisc *sch, | |
167 | struct sk_buff **to_free); | |
1da177e4 | 168 | struct sk_buff * (*dequeue)(struct Qdisc *); |
90d841fd | 169 | struct sk_buff * (*peek)(struct Qdisc *); |
1da177e4 | 170 | |
1e90474c | 171 | int (*init)(struct Qdisc *, struct nlattr *arg); |
1da177e4 LT |
172 | void (*reset)(struct Qdisc *); |
173 | void (*destroy)(struct Qdisc *); | |
1e90474c | 174 | int (*change)(struct Qdisc *, struct nlattr *arg); |
6ec1c69a | 175 | void (*attach)(struct Qdisc *); |
1da177e4 LT |
176 | |
177 | int (*dump)(struct Qdisc *, struct sk_buff *); | |
178 | int (*dump_stats)(struct Qdisc *, struct gnet_dump *); | |
179 | ||
180 | struct module *owner; | |
181 | }; | |
182 | ||
183 | ||
fd2c3ef7 | 184 | struct tcf_result { |
1da177e4 LT |
185 | unsigned long class; |
186 | u32 classid; | |
187 | }; | |
188 | ||
fd2c3ef7 | 189 | struct tcf_proto_ops { |
36272874 | 190 | struct list_head head; |
1da177e4 LT |
191 | char kind[IFNAMSIZ]; |
192 | ||
dc7f9f6e ED |
193 | int (*classify)(struct sk_buff *, |
194 | const struct tcf_proto *, | |
195 | struct tcf_result *); | |
1da177e4 | 196 | int (*init)(struct tcf_proto*); |
1e052be6 | 197 | bool (*destroy)(struct tcf_proto*, bool); |
1da177e4 LT |
198 | |
199 | unsigned long (*get)(struct tcf_proto*, u32 handle); | |
c1b52739 | 200 | int (*change)(struct net *net, struct sk_buff *, |
af4c6641 | 201 | struct tcf_proto*, unsigned long, |
add93b61 | 202 | u32 handle, struct nlattr **, |
2f7ef2f8 | 203 | unsigned long *, bool); |
1da177e4 LT |
204 | int (*delete)(struct tcf_proto*, unsigned long); |
205 | void (*walk)(struct tcf_proto*, struct tcf_walker *arg); | |
206 | ||
207 | /* rtnetlink specific */ | |
832d1d5b | 208 | int (*dump)(struct net*, struct tcf_proto*, unsigned long, |
1da177e4 LT |
209 | struct sk_buff *skb, struct tcmsg*); |
210 | ||
211 | struct module *owner; | |
212 | }; | |
213 | ||
fd2c3ef7 | 214 | struct tcf_proto { |
1da177e4 | 215 | /* Fast access part */ |
25d8c0d5 JF |
216 | struct tcf_proto __rcu *next; |
217 | void __rcu *root; | |
dc7f9f6e ED |
218 | int (*classify)(struct sk_buff *, |
219 | const struct tcf_proto *, | |
220 | struct tcf_result *); | |
66c6f529 | 221 | __be16 protocol; |
1da177e4 LT |
222 | |
223 | /* All the rest */ | |
224 | u32 prio; | |
225 | u32 classid; | |
226 | struct Qdisc *q; | |
227 | void *data; | |
dc7f9f6e | 228 | const struct tcf_proto_ops *ops; |
25d8c0d5 | 229 | struct rcu_head rcu; |
1da177e4 LT |
230 | }; |
231 | ||
175f9c1b JK |
232 | struct qdisc_skb_cb { |
233 | unsigned int pkt_len; | |
df4ab5b3 | 234 | u16 slave_dev_queue_mapping; |
045efa82 | 235 | u16 tc_classid; |
25711786 ED |
236 | #define QDISC_CB_PRIV_LEN 20 |
237 | unsigned char data[QDISC_CB_PRIV_LEN]; | |
175f9c1b JK |
238 | }; |
239 | ||
16bda13d DM |
240 | static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz) |
241 | { | |
242 | struct qdisc_skb_cb *qcb; | |
5ee31c68 ED |
243 | |
244 | BUILD_BUG_ON(sizeof(skb->cb) < offsetof(struct qdisc_skb_cb, data) + sz); | |
16bda13d DM |
245 | BUILD_BUG_ON(sizeof(qcb->data) < sz); |
246 | } | |
247 | ||
05bdd2f1 | 248 | static inline int qdisc_qlen(const struct Qdisc *q) |
bbd8a0d3 KK |
249 | { |
250 | return q->q.qlen; | |
251 | } | |
252 | ||
bfe0d029 | 253 | static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb) |
175f9c1b JK |
254 | { |
255 | return (struct qdisc_skb_cb *)skb->cb; | |
256 | } | |
257 | ||
83874000 DM |
258 | static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc) |
259 | { | |
260 | return &qdisc->q.lock; | |
261 | } | |
262 | ||
05bdd2f1 | 263 | static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc) |
7698b4fc | 264 | { |
46e5da40 JF |
265 | struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc); |
266 | ||
267 | return q; | |
7698b4fc DM |
268 | } |
269 | ||
05bdd2f1 | 270 | static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc) |
2540e051 JP |
271 | { |
272 | return qdisc->dev_queue->qdisc_sleeping; | |
273 | } | |
274 | ||
7e43f112 DM |
275 | /* The qdisc root lock is a mechanism by which to top level |
276 | * of a qdisc tree can be locked from any qdisc node in the | |
277 | * forest. This allows changing the configuration of some | |
278 | * aspect of the qdisc tree while blocking out asynchronous | |
279 | * qdisc access in the packet processing paths. | |
280 | * | |
281 | * It is only legal to do this when the root will not change | |
282 | * on us. Otherwise we'll potentially lock the wrong qdisc | |
283 | * root. This is enforced by holding the RTNL semaphore, which | |
284 | * all users of this lock accessor must do. | |
285 | */ | |
05bdd2f1 | 286 | static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc) |
7698b4fc DM |
287 | { |
288 | struct Qdisc *root = qdisc_root(qdisc); | |
289 | ||
7e43f112 | 290 | ASSERT_RTNL(); |
83874000 | 291 | return qdisc_lock(root); |
7698b4fc DM |
292 | } |
293 | ||
05bdd2f1 | 294 | static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc) |
f6f9b93f JP |
295 | { |
296 | struct Qdisc *root = qdisc_root_sleeping(qdisc); | |
297 | ||
298 | ASSERT_RTNL(); | |
299 | return qdisc_lock(root); | |
300 | } | |
301 | ||
edb09eb1 ED |
302 | static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc) |
303 | { | |
304 | struct Qdisc *root = qdisc_root_sleeping(qdisc); | |
305 | ||
306 | ASSERT_RTNL(); | |
307 | return &root->running; | |
308 | } | |
309 | ||
05bdd2f1 | 310 | static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc) |
5ce2d488 DM |
311 | { |
312 | return qdisc->dev_queue->dev; | |
313 | } | |
1da177e4 | 314 | |
05bdd2f1 | 315 | static inline void sch_tree_lock(const struct Qdisc *q) |
78a5b30b | 316 | { |
fe439dd0 | 317 | spin_lock_bh(qdisc_root_sleeping_lock(q)); |
78a5b30b DM |
318 | } |
319 | ||
05bdd2f1 | 320 | static inline void sch_tree_unlock(const struct Qdisc *q) |
78a5b30b | 321 | { |
fe439dd0 | 322 | spin_unlock_bh(qdisc_root_sleeping_lock(q)); |
78a5b30b DM |
323 | } |
324 | ||
325 | #define tcf_tree_lock(tp) sch_tree_lock((tp)->q) | |
326 | #define tcf_tree_unlock(tp) sch_tree_unlock((tp)->q) | |
1da177e4 | 327 | |
e41a33e6 TG |
328 | extern struct Qdisc noop_qdisc; |
329 | extern struct Qdisc_ops noop_qdisc_ops; | |
6ec1c69a DM |
330 | extern struct Qdisc_ops pfifo_fast_ops; |
331 | extern struct Qdisc_ops mq_qdisc_ops; | |
d66d6c31 | 332 | extern struct Qdisc_ops noqueue_qdisc_ops; |
6da7c8fc | 333 | extern const struct Qdisc_ops *default_qdisc_ops; |
1f27cde3 ED |
334 | static inline const struct Qdisc_ops * |
335 | get_default_qdisc_ops(const struct net_device *dev, int ntx) | |
336 | { | |
337 | return ntx < dev->real_num_tx_queues ? | |
338 | default_qdisc_ops : &pfifo_fast_ops; | |
339 | } | |
e41a33e6 | 340 | |
fd2c3ef7 | 341 | struct Qdisc_class_common { |
6fe1c7a5 PM |
342 | u32 classid; |
343 | struct hlist_node hnode; | |
344 | }; | |
345 | ||
fd2c3ef7 | 346 | struct Qdisc_class_hash { |
6fe1c7a5 PM |
347 | struct hlist_head *hash; |
348 | unsigned int hashsize; | |
349 | unsigned int hashmask; | |
350 | unsigned int hashelems; | |
351 | }; | |
352 | ||
353 | static inline unsigned int qdisc_class_hash(u32 id, u32 mask) | |
354 | { | |
355 | id ^= id >> 8; | |
356 | id ^= id >> 4; | |
357 | return id & mask; | |
358 | } | |
359 | ||
360 | static inline struct Qdisc_class_common * | |
05bdd2f1 | 361 | qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id) |
6fe1c7a5 PM |
362 | { |
363 | struct Qdisc_class_common *cl; | |
6fe1c7a5 PM |
364 | unsigned int h; |
365 | ||
366 | h = qdisc_class_hash(id, hash->hashmask); | |
b67bfe0d | 367 | hlist_for_each_entry(cl, &hash->hash[h], hnode) { |
6fe1c7a5 PM |
368 | if (cl->classid == id) |
369 | return cl; | |
370 | } | |
371 | return NULL; | |
372 | } | |
373 | ||
5c15257f JP |
374 | int qdisc_class_hash_init(struct Qdisc_class_hash *); |
375 | void qdisc_class_hash_insert(struct Qdisc_class_hash *, | |
376 | struct Qdisc_class_common *); | |
377 | void qdisc_class_hash_remove(struct Qdisc_class_hash *, | |
378 | struct Qdisc_class_common *); | |
379 | void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *); | |
380 | void qdisc_class_hash_destroy(struct Qdisc_class_hash *); | |
381 | ||
382 | void dev_init_scheduler(struct net_device *dev); | |
383 | void dev_shutdown(struct net_device *dev); | |
384 | void dev_activate(struct net_device *dev); | |
385 | void dev_deactivate(struct net_device *dev); | |
386 | void dev_deactivate_many(struct list_head *head); | |
387 | struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue, | |
388 | struct Qdisc *qdisc); | |
389 | void qdisc_reset(struct Qdisc *qdisc); | |
390 | void qdisc_destroy(struct Qdisc *qdisc); | |
2ccccf5f WC |
391 | void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, unsigned int n, |
392 | unsigned int len); | |
5c15257f | 393 | struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue, |
d2a7f269 | 394 | const struct Qdisc_ops *ops); |
5c15257f | 395 | struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue, |
d2a7f269 | 396 | const struct Qdisc_ops *ops, u32 parentid); |
5c15257f JP |
397 | void __qdisc_calculate_pkt_len(struct sk_buff *skb, |
398 | const struct qdisc_size_table *stab); | |
1e052be6 | 399 | bool tcf_destroy(struct tcf_proto *tp, bool force); |
25d8c0d5 | 400 | void tcf_destroy_chain(struct tcf_proto __rcu **fl); |
27b29f63 | 401 | int skb_do_redirect(struct sk_buff *); |
1da177e4 | 402 | |
fdc5432a DB |
403 | static inline bool skb_at_tc_ingress(const struct sk_buff *skb) |
404 | { | |
405 | #ifdef CONFIG_NET_CLS_ACT | |
406 | return G_TC_AT(skb->tc_verd) & AT_INGRESS; | |
407 | #else | |
408 | return false; | |
409 | #endif | |
410 | } | |
411 | ||
f0796d5c JF |
412 | /* Reset all TX qdiscs greater then index of a device. */ |
413 | static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i) | |
5aa70995 | 414 | { |
4ef6acff JF |
415 | struct Qdisc *qdisc; |
416 | ||
f0796d5c | 417 | for (; i < dev->num_tx_queues; i++) { |
46e5da40 | 418 | qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc); |
4ef6acff JF |
419 | if (qdisc) { |
420 | spin_lock_bh(qdisc_lock(qdisc)); | |
421 | qdisc_reset(qdisc); | |
422 | spin_unlock_bh(qdisc_lock(qdisc)); | |
423 | } | |
424 | } | |
5aa70995 DM |
425 | } |
426 | ||
5aa70995 DM |
427 | static inline void qdisc_reset_all_tx(struct net_device *dev) |
428 | { | |
f0796d5c | 429 | qdisc_reset_all_tx_gt(dev, 0); |
5aa70995 DM |
430 | } |
431 | ||
3e745dd6 DM |
432 | /* Are all TX queues of the device empty? */ |
433 | static inline bool qdisc_all_tx_empty(const struct net_device *dev) | |
434 | { | |
e8a0464c | 435 | unsigned int i; |
46e5da40 JF |
436 | |
437 | rcu_read_lock(); | |
e8a0464c DM |
438 | for (i = 0; i < dev->num_tx_queues; i++) { |
439 | struct netdev_queue *txq = netdev_get_tx_queue(dev, i); | |
46e5da40 | 440 | const struct Qdisc *q = rcu_dereference(txq->qdisc); |
3e745dd6 | 441 | |
46e5da40 JF |
442 | if (q->q.qlen) { |
443 | rcu_read_unlock(); | |
e8a0464c | 444 | return false; |
46e5da40 | 445 | } |
e8a0464c | 446 | } |
46e5da40 | 447 | rcu_read_unlock(); |
e8a0464c | 448 | return true; |
3e745dd6 DM |
449 | } |
450 | ||
6fa9864b | 451 | /* Are any of the TX qdiscs changing? */ |
05bdd2f1 | 452 | static inline bool qdisc_tx_changing(const struct net_device *dev) |
6fa9864b | 453 | { |
e8a0464c | 454 | unsigned int i; |
46e5da40 | 455 | |
e8a0464c DM |
456 | for (i = 0; i < dev->num_tx_queues; i++) { |
457 | struct netdev_queue *txq = netdev_get_tx_queue(dev, i); | |
46e5da40 | 458 | if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping) |
e8a0464c DM |
459 | return true; |
460 | } | |
461 | return false; | |
6fa9864b DM |
462 | } |
463 | ||
e8a0464c | 464 | /* Is the device using the noop qdisc on all queues? */ |
05297949 DM |
465 | static inline bool qdisc_tx_is_noop(const struct net_device *dev) |
466 | { | |
e8a0464c | 467 | unsigned int i; |
46e5da40 | 468 | |
e8a0464c DM |
469 | for (i = 0; i < dev->num_tx_queues; i++) { |
470 | struct netdev_queue *txq = netdev_get_tx_queue(dev, i); | |
46e5da40 | 471 | if (rcu_access_pointer(txq->qdisc) != &noop_qdisc) |
e8a0464c DM |
472 | return false; |
473 | } | |
474 | return true; | |
05297949 DM |
475 | } |
476 | ||
bfe0d029 | 477 | static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb) |
0abf77e5 | 478 | { |
175f9c1b | 479 | return qdisc_skb_cb(skb)->pkt_len; |
0abf77e5 JK |
480 | } |
481 | ||
c27f339a | 482 | /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */ |
378a2f09 JP |
483 | enum net_xmit_qdisc_t { |
484 | __NET_XMIT_STOLEN = 0x00010000, | |
c27f339a | 485 | __NET_XMIT_BYPASS = 0x00020000, |
378a2f09 JP |
486 | }; |
487 | ||
c27f339a | 488 | #ifdef CONFIG_NET_CLS_ACT |
378a2f09 | 489 | #define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1) |
378a2f09 JP |
490 | #else |
491 | #define net_xmit_drop_count(e) (1) | |
492 | #endif | |
493 | ||
a2da570d ED |
494 | static inline void qdisc_calculate_pkt_len(struct sk_buff *skb, |
495 | const struct Qdisc *sch) | |
5f86173b | 496 | { |
3a682fbd | 497 | #ifdef CONFIG_NET_SCHED |
a2da570d ED |
498 | struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab); |
499 | ||
500 | if (stab) | |
501 | __qdisc_calculate_pkt_len(skb, stab); | |
3a682fbd | 502 | #endif |
a2da570d ED |
503 | } |
504 | ||
520ac30f ED |
505 | static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch, |
506 | struct sk_buff **to_free) | |
a2da570d ED |
507 | { |
508 | qdisc_calculate_pkt_len(skb, sch); | |
520ac30f | 509 | return sch->enqueue(skb, sch, to_free); |
5f86173b JK |
510 | } |
511 | ||
22e0f8b9 JF |
512 | static inline bool qdisc_is_percpu_stats(const struct Qdisc *q) |
513 | { | |
514 | return q->flags & TCQ_F_CPUSTATS; | |
515 | } | |
bfe0d029 | 516 | |
38040702 AV |
517 | static inline void _bstats_update(struct gnet_stats_basic_packed *bstats, |
518 | __u64 bytes, __u32 packets) | |
519 | { | |
520 | bstats->bytes += bytes; | |
521 | bstats->packets += packets; | |
522 | } | |
523 | ||
bfe0d029 ED |
524 | static inline void bstats_update(struct gnet_stats_basic_packed *bstats, |
525 | const struct sk_buff *skb) | |
526 | { | |
38040702 AV |
527 | _bstats_update(bstats, |
528 | qdisc_pkt_len(skb), | |
529 | skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1); | |
530 | } | |
531 | ||
532 | static inline void _bstats_cpu_update(struct gnet_stats_basic_cpu *bstats, | |
533 | __u64 bytes, __u32 packets) | |
534 | { | |
535 | u64_stats_update_begin(&bstats->syncp); | |
536 | _bstats_update(&bstats->bstats, bytes, packets); | |
537 | u64_stats_update_end(&bstats->syncp); | |
bfe0d029 ED |
538 | } |
539 | ||
24ea591d ED |
540 | static inline void bstats_cpu_update(struct gnet_stats_basic_cpu *bstats, |
541 | const struct sk_buff *skb) | |
22e0f8b9 | 542 | { |
22e0f8b9 JF |
543 | u64_stats_update_begin(&bstats->syncp); |
544 | bstats_update(&bstats->bstats, skb); | |
545 | u64_stats_update_end(&bstats->syncp); | |
546 | } | |
547 | ||
24ea591d ED |
548 | static inline void qdisc_bstats_cpu_update(struct Qdisc *sch, |
549 | const struct sk_buff *skb) | |
550 | { | |
551 | bstats_cpu_update(this_cpu_ptr(sch->cpu_bstats), skb); | |
552 | } | |
553 | ||
bfe0d029 ED |
554 | static inline void qdisc_bstats_update(struct Qdisc *sch, |
555 | const struct sk_buff *skb) | |
bbd8a0d3 | 556 | { |
bfe0d029 | 557 | bstats_update(&sch->bstats, skb); |
bbd8a0d3 KK |
558 | } |
559 | ||
25331d6c JF |
560 | static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch, |
561 | const struct sk_buff *skb) | |
562 | { | |
563 | sch->qstats.backlog -= qdisc_pkt_len(skb); | |
564 | } | |
565 | ||
566 | static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch, | |
567 | const struct sk_buff *skb) | |
568 | { | |
569 | sch->qstats.backlog += qdisc_pkt_len(skb); | |
570 | } | |
571 | ||
572 | static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count) | |
573 | { | |
574 | sch->qstats.drops += count; | |
575 | } | |
576 | ||
24ea591d | 577 | static inline void qstats_drop_inc(struct gnet_stats_queue *qstats) |
25331d6c | 578 | { |
24ea591d | 579 | qstats->drops++; |
25331d6c JF |
580 | } |
581 | ||
24ea591d | 582 | static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats) |
b0ab6f92 | 583 | { |
24ea591d ED |
584 | qstats->overlimits++; |
585 | } | |
b0ab6f92 | 586 | |
24ea591d ED |
587 | static inline void qdisc_qstats_drop(struct Qdisc *sch) |
588 | { | |
589 | qstats_drop_inc(&sch->qstats); | |
590 | } | |
591 | ||
592 | static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch) | |
593 | { | |
594 | qstats_drop_inc(this_cpu_ptr(sch->cpu_qstats)); | |
b0ab6f92 JF |
595 | } |
596 | ||
25331d6c JF |
597 | static inline void qdisc_qstats_overlimit(struct Qdisc *sch) |
598 | { | |
599 | sch->qstats.overlimits++; | |
600 | } | |
601 | ||
9972b25d TG |
602 | static inline int __qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch, |
603 | struct sk_buff_head *list) | |
604 | { | |
605 | __skb_queue_tail(list, skb); | |
25331d6c | 606 | qdisc_qstats_backlog_inc(sch, skb); |
9972b25d TG |
607 | |
608 | return NET_XMIT_SUCCESS; | |
609 | } | |
610 | ||
611 | static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch) | |
612 | { | |
613 | return __qdisc_enqueue_tail(skb, sch, &sch->q); | |
614 | } | |
615 | ||
616 | static inline struct sk_buff *__qdisc_dequeue_head(struct Qdisc *sch, | |
617 | struct sk_buff_head *list) | |
618 | { | |
619 | struct sk_buff *skb = __skb_dequeue(list); | |
620 | ||
9190b3b3 | 621 | if (likely(skb != NULL)) { |
25331d6c | 622 | qdisc_qstats_backlog_dec(sch, skb); |
9190b3b3 ED |
623 | qdisc_bstats_update(sch, skb); |
624 | } | |
9972b25d TG |
625 | |
626 | return skb; | |
627 | } | |
628 | ||
629 | static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch) | |
630 | { | |
631 | return __qdisc_dequeue_head(sch, &sch->q); | |
57dbb2d8 HPP |
632 | } |
633 | ||
520ac30f ED |
634 | /* Instead of calling kfree_skb() while root qdisc lock is held, |
635 | * queue the skb for future freeing at end of __dev_xmit_skb() | |
636 | */ | |
637 | static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free) | |
638 | { | |
639 | skb->next = *to_free; | |
640 | *to_free = skb; | |
641 | } | |
642 | ||
57dbb2d8 | 643 | static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch, |
520ac30f ED |
644 | struct sk_buff_head *list, |
645 | struct sk_buff **to_free) | |
57dbb2d8 | 646 | { |
9190b3b3 | 647 | struct sk_buff *skb = __skb_dequeue(list); |
57dbb2d8 HPP |
648 | |
649 | if (likely(skb != NULL)) { | |
650 | unsigned int len = qdisc_pkt_len(skb); | |
520ac30f | 651 | |
25331d6c | 652 | qdisc_qstats_backlog_dec(sch, skb); |
520ac30f | 653 | __qdisc_drop(skb, to_free); |
57dbb2d8 HPP |
654 | return len; |
655 | } | |
656 | ||
657 | return 0; | |
658 | } | |
659 | ||
520ac30f ED |
660 | static inline unsigned int qdisc_queue_drop_head(struct Qdisc *sch, |
661 | struct sk_buff **to_free) | |
57dbb2d8 | 662 | { |
520ac30f | 663 | return __qdisc_queue_drop_head(sch, &sch->q, to_free); |
9972b25d TG |
664 | } |
665 | ||
48a8f519 PM |
666 | static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch) |
667 | { | |
668 | return skb_peek(&sch->q); | |
669 | } | |
670 | ||
77be155c JP |
671 | /* generic pseudo peek method for non-work-conserving qdisc */ |
672 | static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch) | |
673 | { | |
674 | /* we can reuse ->gso_skb because peek isn't called for root qdiscs */ | |
61c9eaf9 | 675 | if (!sch->gso_skb) { |
77be155c | 676 | sch->gso_skb = sch->dequeue(sch); |
a27758ff | 677 | if (sch->gso_skb) { |
61c9eaf9 | 678 | /* it's still part of the queue */ |
a27758ff | 679 | qdisc_qstats_backlog_inc(sch, sch->gso_skb); |
61c9eaf9 | 680 | sch->q.qlen++; |
a27758ff | 681 | } |
61c9eaf9 | 682 | } |
77be155c JP |
683 | |
684 | return sch->gso_skb; | |
685 | } | |
686 | ||
687 | /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */ | |
688 | static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch) | |
689 | { | |
690 | struct sk_buff *skb = sch->gso_skb; | |
691 | ||
61c9eaf9 | 692 | if (skb) { |
77be155c | 693 | sch->gso_skb = NULL; |
a27758ff | 694 | qdisc_qstats_backlog_dec(sch, skb); |
61c9eaf9 JP |
695 | sch->q.qlen--; |
696 | } else { | |
77be155c | 697 | skb = sch->dequeue(sch); |
61c9eaf9 | 698 | } |
77be155c JP |
699 | |
700 | return skb; | |
701 | } | |
702 | ||
1b5c5493 | 703 | static inline void __qdisc_reset_queue(struct sk_buff_head *list) |
9972b25d TG |
704 | { |
705 | /* | |
706 | * We do not know the backlog in bytes of this list, it | |
707 | * is up to the caller to correct it | |
708 | */ | |
1b5c5493 ED |
709 | if (!skb_queue_empty(list)) { |
710 | rtnl_kfree_skbs(list->next, list->prev); | |
711 | __skb_queue_head_init(list); | |
712 | } | |
9972b25d TG |
713 | } |
714 | ||
715 | static inline void qdisc_reset_queue(struct Qdisc *sch) | |
716 | { | |
1b5c5493 | 717 | __qdisc_reset_queue(&sch->q); |
9972b25d TG |
718 | sch->qstats.backlog = 0; |
719 | } | |
720 | ||
86a7996c WC |
721 | static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new, |
722 | struct Qdisc **pold) | |
723 | { | |
724 | struct Qdisc *old; | |
725 | ||
726 | sch_tree_lock(sch); | |
727 | old = *pold; | |
728 | *pold = new; | |
729 | if (old != NULL) { | |
2ccccf5f | 730 | qdisc_tree_reduce_backlog(old, old->q.qlen, old->qstats.backlog); |
86a7996c WC |
731 | qdisc_reset(old); |
732 | } | |
733 | sch_tree_unlock(sch); | |
734 | ||
735 | return old; | |
736 | } | |
737 | ||
1b5c5493 ED |
738 | static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch) |
739 | { | |
740 | rtnl_kfree_skbs(skb, skb); | |
741 | qdisc_qstats_drop(sch); | |
742 | } | |
743 | ||
520ac30f ED |
744 | |
745 | static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch, | |
746 | struct sk_buff **to_free) | |
9972b25d | 747 | { |
520ac30f | 748 | __qdisc_drop(skb, to_free); |
25331d6c | 749 | qdisc_qstats_drop(sch); |
9972b25d TG |
750 | |
751 | return NET_XMIT_DROP; | |
752 | } | |
753 | ||
e9bef55d JDB |
754 | /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how |
755 | long it will take to send a packet given its size. | |
756 | */ | |
757 | static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen) | |
758 | { | |
e08b0998 JDB |
759 | int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead; |
760 | if (slot < 0) | |
761 | slot = 0; | |
e9bef55d JDB |
762 | slot >>= rtab->rate.cell_log; |
763 | if (slot > 255) | |
a02cec21 | 764 | return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF]; |
e9bef55d JDB |
765 | return rtab->data[slot]; |
766 | } | |
767 | ||
292f1c7f | 768 | struct psched_ratecfg { |
130d3d68 | 769 | u64 rate_bytes_ps; /* bytes per second */ |
01cb71d2 ED |
770 | u32 mult; |
771 | u16 overhead; | |
8a8e3d84 | 772 | u8 linklayer; |
01cb71d2 | 773 | u8 shift; |
292f1c7f JP |
774 | }; |
775 | ||
776 | static inline u64 psched_l2t_ns(const struct psched_ratecfg *r, | |
777 | unsigned int len) | |
778 | { | |
8a8e3d84 JDB |
779 | len += r->overhead; |
780 | ||
781 | if (unlikely(r->linklayer == TC_LINKLAYER_ATM)) | |
782 | return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift; | |
783 | ||
784 | return ((u64)len * r->mult) >> r->shift; | |
292f1c7f JP |
785 | } |
786 | ||
5c15257f | 787 | void psched_ratecfg_precompute(struct psched_ratecfg *r, |
3e1e3aae ED |
788 | const struct tc_ratespec *conf, |
789 | u64 rate64); | |
292f1c7f | 790 | |
01cb71d2 ED |
791 | static inline void psched_ratecfg_getrate(struct tc_ratespec *res, |
792 | const struct psched_ratecfg *r) | |
292f1c7f | 793 | { |
01cb71d2 | 794 | memset(res, 0, sizeof(*res)); |
3e1e3aae ED |
795 | |
796 | /* legacy struct tc_ratespec has a 32bit @rate field | |
797 | * Qdisc using 64bit rate should add new attributes | |
798 | * in order to maintain compatibility. | |
799 | */ | |
800 | res->rate = min_t(u64, r->rate_bytes_ps, ~0U); | |
801 | ||
01cb71d2 | 802 | res->overhead = r->overhead; |
8a8e3d84 | 803 | res->linklayer = (r->linklayer & TC_LINKLAYER_MASK); |
292f1c7f JP |
804 | } |
805 | ||
1da177e4 | 806 | #endif |