]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
1da177e4 LT |
2 | #ifndef __NET_SCHED_GENERIC_H |
3 | #define __NET_SCHED_GENERIC_H | |
4 | ||
1da177e4 LT |
5 | #include <linux/netdevice.h> |
6 | #include <linux/types.h> | |
7 | #include <linux/rcupdate.h> | |
1da177e4 LT |
8 | #include <linux/pkt_sched.h> |
9 | #include <linux/pkt_cls.h> | |
22e0f8b9 | 10 | #include <linux/percpu.h> |
5772e9a3 | 11 | #include <linux/dynamic_queue_limits.h> |
5bc17018 | 12 | #include <linux/list.h> |
7b936405 | 13 | #include <linux/refcount.h> |
7aa0045d | 14 | #include <linux/workqueue.h> |
c266f64d | 15 | #include <linux/mutex.h> |
1da177e4 | 16 | #include <net/gen_stats.h> |
be577ddc | 17 | #include <net/rtnetlink.h> |
1da177e4 LT |
18 | |
19 | struct Qdisc_ops; | |
20 | struct qdisc_walker; | |
21 | struct tcf_walker; | |
22 | struct module; | |
d58e468b | 23 | struct bpf_flow_keys; |
1da177e4 | 24 | |
e56185c7 JH |
25 | typedef int tc_setup_cb_t(enum tc_setup_type type, |
26 | void *type_data, void *cb_priv); | |
27 | ||
7f76fa36 JH |
28 | typedef int tc_indr_block_bind_cb_t(struct net_device *dev, void *cb_priv, |
29 | enum tc_setup_type type, void *type_data); | |
30 | ||
fd2c3ef7 | 31 | struct qdisc_rate_table { |
1da177e4 LT |
32 | struct tc_ratespec rate; |
33 | u32 data[256]; | |
34 | struct qdisc_rate_table *next; | |
35 | int refcnt; | |
36 | }; | |
37 | ||
fd2c3ef7 | 38 | enum qdisc_state_t { |
37437bb2 | 39 | __QDISC_STATE_SCHED, |
a9312ae8 | 40 | __QDISC_STATE_DEACTIVATED, |
e2627c8c DM |
41 | }; |
42 | ||
175f9c1b | 43 | struct qdisc_size_table { |
a2da570d | 44 | struct rcu_head rcu; |
175f9c1b JK |
45 | struct list_head list; |
46 | struct tc_sizespec szopts; | |
47 | int refcnt; | |
48 | u16 data[]; | |
49 | }; | |
50 | ||
48da34b7 FW |
51 | /* similar to sk_buff_head, but skb->prev pointer is undefined. */ |
52 | struct qdisc_skb_head { | |
53 | struct sk_buff *head; | |
54 | struct sk_buff *tail; | |
55 | __u32 qlen; | |
56 | spinlock_t lock; | |
57 | }; | |
58 | ||
fd2c3ef7 | 59 | struct Qdisc { |
520ac30f ED |
60 | int (*enqueue)(struct sk_buff *skb, |
61 | struct Qdisc *sch, | |
62 | struct sk_buff **to_free); | |
63 | struct sk_buff * (*dequeue)(struct Qdisc *sch); | |
05bdd2f1 | 64 | unsigned int flags; |
b00355db | 65 | #define TCQ_F_BUILTIN 1 |
fd245a4a ED |
66 | #define TCQ_F_INGRESS 2 |
67 | #define TCQ_F_CAN_BYPASS 4 | |
68 | #define TCQ_F_MQROOT 8 | |
1abbe139 ED |
69 | #define TCQ_F_ONETXQUEUE 0x10 /* dequeue_skb() can assume all skbs are for |
70 | * q->dev_queue : It can test | |
71 | * netif_xmit_frozen_or_stopped() before | |
72 | * dequeueing next packet. | |
73 | * Its true for MQ/MQPRIO slaves, or non | |
74 | * multiqueue device. | |
75 | */ | |
b00355db | 76 | #define TCQ_F_WARN_NONWC (1 << 16) |
22e0f8b9 | 77 | #define TCQ_F_CPUSTATS 0x20 /* run using percpu statistics */ |
4eaf3b84 ED |
78 | #define TCQ_F_NOPARENT 0x40 /* root of its hierarchy : |
79 | * qdisc_tree_decrease_qlen() should stop. | |
80 | */ | |
49b49971 | 81 | #define TCQ_F_INVISIBLE 0x80 /* invisible by default in dump */ |
6b3ba914 | 82 | #define TCQ_F_NOLOCK 0x100 /* qdisc does not require locking */ |
7a4fa291 | 83 | #define TCQ_F_OFFLOADED 0x200 /* qdisc is offloaded to HW */ |
45203a3b | 84 | u32 limit; |
05bdd2f1 | 85 | const struct Qdisc_ops *ops; |
a2da570d | 86 | struct qdisc_size_table __rcu *stab; |
59cc1f61 | 87 | struct hlist_node hash; |
1da177e4 LT |
88 | u32 handle; |
89 | u32 parent; | |
72b25a91 | 90 | |
5e140dfc | 91 | struct netdev_queue *dev_queue; |
5e140dfc | 92 | |
1c0d32fd | 93 | struct net_rate_estimator __rcu *rate_est; |
0d32ef8c ED |
94 | struct gnet_stats_basic_cpu __percpu *cpu_bstats; |
95 | struct gnet_stats_queue __percpu *cpu_qstats; | |
e9be0e99 PA |
96 | int padded; |
97 | refcount_t refcnt; | |
0d32ef8c | 98 | |
5e140dfc ED |
99 | /* |
100 | * For performance sake on SMP, we put highly modified fields at the end | |
101 | */ | |
a53851e2 | 102 | struct sk_buff_head gso_skb ____cacheline_aligned_in_smp; |
48da34b7 | 103 | struct qdisc_skb_head q; |
0d32ef8c | 104 | struct gnet_stats_basic_packed bstats; |
f9eb8aea | 105 | seqcount_t running; |
0d32ef8c | 106 | struct gnet_stats_queue qstats; |
4d202a0d ED |
107 | unsigned long state; |
108 | struct Qdisc *next_sched; | |
70e57d5e | 109 | struct sk_buff_head skb_bad_txq; |
45203a3b ED |
110 | |
111 | spinlock_t busylock ____cacheline_aligned_in_smp; | |
96009c7d | 112 | spinlock_t seqlock; |
3a7d0d07 | 113 | struct rcu_head rcu; |
1da177e4 LT |
114 | }; |
115 | ||
551143d8 ED |
116 | static inline void qdisc_refcount_inc(struct Qdisc *qdisc) |
117 | { | |
118 | if (qdisc->flags & TCQ_F_BUILTIN) | |
119 | return; | |
120 | refcount_inc(&qdisc->refcnt); | |
121 | } | |
122 | ||
9d7e82ce VB |
123 | /* Intended to be used by unlocked users, when concurrent qdisc release is |
124 | * possible. | |
125 | */ | |
126 | ||
127 | static inline struct Qdisc *qdisc_refcount_inc_nz(struct Qdisc *qdisc) | |
128 | { | |
129 | if (qdisc->flags & TCQ_F_BUILTIN) | |
130 | return qdisc; | |
131 | if (refcount_inc_not_zero(&qdisc->refcnt)) | |
132 | return qdisc; | |
133 | return NULL; | |
134 | } | |
135 | ||
96009c7d | 136 | static inline bool qdisc_is_running(struct Qdisc *qdisc) |
bc135b23 | 137 | { |
32f7b44d | 138 | if (qdisc->flags & TCQ_F_NOLOCK) |
96009c7d | 139 | return spin_is_locked(&qdisc->seqlock); |
f9eb8aea | 140 | return (raw_read_seqcount(&qdisc->running) & 1) ? true : false; |
bc135b23 ED |
141 | } |
142 | ||
143 | static inline bool qdisc_run_begin(struct Qdisc *qdisc) | |
144 | { | |
32f7b44d | 145 | if (qdisc->flags & TCQ_F_NOLOCK) { |
96009c7d | 146 | if (!spin_trylock(&qdisc->seqlock)) |
32f7b44d PA |
147 | return false; |
148 | } else if (qdisc_is_running(qdisc)) { | |
fd245a4a | 149 | return false; |
32f7b44d | 150 | } |
52fbb290 ED |
151 | /* Variant of write_seqcount_begin() telling lockdep a trylock |
152 | * was attempted. | |
153 | */ | |
154 | raw_write_seqcount_begin(&qdisc->running); | |
155 | seqcount_acquire(&qdisc->running.dep_map, 0, 1, _RET_IP_); | |
fd245a4a | 156 | return true; |
bc135b23 ED |
157 | } |
158 | ||
159 | static inline void qdisc_run_end(struct Qdisc *qdisc) | |
160 | { | |
f9eb8aea | 161 | write_seqcount_end(&qdisc->running); |
32f7b44d | 162 | if (qdisc->flags & TCQ_F_NOLOCK) |
96009c7d | 163 | spin_unlock(&qdisc->seqlock); |
fd245a4a ED |
164 | } |
165 | ||
5772e9a3 JDB |
166 | static inline bool qdisc_may_bulk(const struct Qdisc *qdisc) |
167 | { | |
168 | return qdisc->flags & TCQ_F_ONETXQUEUE; | |
169 | } | |
170 | ||
171 | static inline int qdisc_avail_bulklimit(const struct netdev_queue *txq) | |
172 | { | |
173 | #ifdef CONFIG_BQL | |
174 | /* Non-BQL migrated drivers will return 0, too. */ | |
175 | return dql_avail(&txq->dql); | |
176 | #else | |
177 | return 0; | |
178 | #endif | |
179 | } | |
180 | ||
fd2c3ef7 | 181 | struct Qdisc_class_ops { |
1da177e4 | 182 | /* Child qdisc manipulation */ |
926e61b7 | 183 | struct netdev_queue * (*select_queue)(struct Qdisc *, struct tcmsg *); |
1da177e4 | 184 | int (*graft)(struct Qdisc *, unsigned long cl, |
653d6fd6 AA |
185 | struct Qdisc *, struct Qdisc **, |
186 | struct netlink_ext_ack *extack); | |
1da177e4 | 187 | struct Qdisc * (*leaf)(struct Qdisc *, unsigned long cl); |
43effa1e | 188 | void (*qlen_notify)(struct Qdisc *, unsigned long); |
1da177e4 LT |
189 | |
190 | /* Class manipulation routines */ | |
143976ce | 191 | unsigned long (*find)(struct Qdisc *, u32 classid); |
1da177e4 | 192 | int (*change)(struct Qdisc *, u32, u32, |
793d81d6 AA |
193 | struct nlattr **, unsigned long *, |
194 | struct netlink_ext_ack *); | |
1da177e4 LT |
195 | int (*delete)(struct Qdisc *, unsigned long); |
196 | void (*walk)(struct Qdisc *, struct qdisc_walker * arg); | |
197 | ||
198 | /* Filter manipulation */ | |
0ac4bd68 | 199 | struct tcf_block * (*tcf_block)(struct Qdisc *sch, |
cbaacc4e AA |
200 | unsigned long arg, |
201 | struct netlink_ext_ack *extack); | |
1da177e4 LT |
202 | unsigned long (*bind_tcf)(struct Qdisc *, unsigned long, |
203 | u32 classid); | |
204 | void (*unbind_tcf)(struct Qdisc *, unsigned long); | |
205 | ||
206 | /* rtnetlink specific */ | |
207 | int (*dump)(struct Qdisc *, unsigned long, | |
208 | struct sk_buff *skb, struct tcmsg*); | |
209 | int (*dump_stats)(struct Qdisc *, unsigned long, | |
210 | struct gnet_dump *); | |
211 | }; | |
212 | ||
fd2c3ef7 | 213 | struct Qdisc_ops { |
1da177e4 | 214 | struct Qdisc_ops *next; |
20fea08b | 215 | const struct Qdisc_class_ops *cl_ops; |
1da177e4 LT |
216 | char id[IFNAMSIZ]; |
217 | int priv_size; | |
d59f5ffa | 218 | unsigned int static_flags; |
1da177e4 | 219 | |
520ac30f ED |
220 | int (*enqueue)(struct sk_buff *skb, |
221 | struct Qdisc *sch, | |
222 | struct sk_buff **to_free); | |
1da177e4 | 223 | struct sk_buff * (*dequeue)(struct Qdisc *); |
90d841fd | 224 | struct sk_buff * (*peek)(struct Qdisc *); |
1da177e4 | 225 | |
e63d7dfd AA |
226 | int (*init)(struct Qdisc *sch, struct nlattr *arg, |
227 | struct netlink_ext_ack *extack); | |
1da177e4 LT |
228 | void (*reset)(struct Qdisc *); |
229 | void (*destroy)(struct Qdisc *); | |
0ac4bd68 | 230 | int (*change)(struct Qdisc *sch, |
2030721c AA |
231 | struct nlattr *arg, |
232 | struct netlink_ext_ack *extack); | |
0ac4bd68 | 233 | void (*attach)(struct Qdisc *sch); |
48bfd55e | 234 | int (*change_tx_queue_len)(struct Qdisc *, unsigned int); |
1da177e4 LT |
235 | |
236 | int (*dump)(struct Qdisc *, struct sk_buff *); | |
237 | int (*dump_stats)(struct Qdisc *, struct gnet_dump *); | |
238 | ||
d47a6b0e JP |
239 | void (*ingress_block_set)(struct Qdisc *sch, |
240 | u32 block_index); | |
241 | void (*egress_block_set)(struct Qdisc *sch, | |
242 | u32 block_index); | |
243 | u32 (*ingress_block_get)(struct Qdisc *sch); | |
244 | u32 (*egress_block_get)(struct Qdisc *sch); | |
245 | ||
1da177e4 LT |
246 | struct module *owner; |
247 | }; | |
248 | ||
249 | ||
fd2c3ef7 | 250 | struct tcf_result { |
db50514f JP |
251 | union { |
252 | struct { | |
253 | unsigned long class; | |
254 | u32 classid; | |
255 | }; | |
256 | const struct tcf_proto *goto_tp; | |
cd11b164 PA |
257 | |
258 | /* used by the TC_ACT_REINSERT action */ | |
259 | struct { | |
260 | bool ingress; | |
261 | struct gnet_stats_queue *qstats; | |
262 | }; | |
db50514f | 263 | }; |
1da177e4 LT |
264 | }; |
265 | ||
9f407f17 JP |
266 | struct tcf_chain; |
267 | ||
fd2c3ef7 | 268 | struct tcf_proto_ops { |
36272874 | 269 | struct list_head head; |
1da177e4 LT |
270 | char kind[IFNAMSIZ]; |
271 | ||
dc7f9f6e ED |
272 | int (*classify)(struct sk_buff *, |
273 | const struct tcf_proto *, | |
274 | struct tcf_result *); | |
1da177e4 | 275 | int (*init)(struct tcf_proto*); |
715df5ec JK |
276 | void (*destroy)(struct tcf_proto *tp, |
277 | struct netlink_ext_ack *extack); | |
1da177e4 | 278 | |
8113c095 | 279 | void* (*get)(struct tcf_proto*, u32 handle); |
c1b52739 | 280 | int (*change)(struct net *net, struct sk_buff *, |
af4c6641 | 281 | struct tcf_proto*, unsigned long, |
add93b61 | 282 | u32 handle, struct nlattr **, |
7306db38 AA |
283 | void **, bool, |
284 | struct netlink_ext_ack *); | |
8865fdd4 | 285 | int (*delete)(struct tcf_proto *tp, void *arg, |
571acf21 AA |
286 | bool *last, |
287 | struct netlink_ext_ack *); | |
1da177e4 | 288 | void (*walk)(struct tcf_proto*, struct tcf_walker *arg); |
e56185c7 JH |
289 | int (*reoffload)(struct tcf_proto *tp, bool add, |
290 | tc_setup_cb_t *cb, void *cb_priv, | |
291 | struct netlink_ext_ack *extack); | |
07d79fc7 | 292 | void (*bind_class)(void *, u32, unsigned long); |
9f407f17 JP |
293 | void * (*tmplt_create)(struct net *net, |
294 | struct tcf_chain *chain, | |
295 | struct nlattr **tca, | |
296 | struct netlink_ext_ack *extack); | |
297 | void (*tmplt_destroy)(void *tmplt_priv); | |
1da177e4 LT |
298 | |
299 | /* rtnetlink specific */ | |
8113c095 | 300 | int (*dump)(struct net*, struct tcf_proto*, void *, |
1da177e4 | 301 | struct sk_buff *skb, struct tcmsg*); |
9f407f17 JP |
302 | int (*tmplt_dump)(struct sk_buff *skb, |
303 | struct net *net, | |
304 | void *tmplt_priv); | |
1da177e4 LT |
305 | |
306 | struct module *owner; | |
307 | }; | |
308 | ||
fd2c3ef7 | 309 | struct tcf_proto { |
1da177e4 | 310 | /* Fast access part */ |
25d8c0d5 JF |
311 | struct tcf_proto __rcu *next; |
312 | void __rcu *root; | |
7fd4b288 PA |
313 | |
314 | /* called under RCU BH lock*/ | |
dc7f9f6e ED |
315 | int (*classify)(struct sk_buff *, |
316 | const struct tcf_proto *, | |
317 | struct tcf_result *); | |
66c6f529 | 318 | __be16 protocol; |
1da177e4 LT |
319 | |
320 | /* All the rest */ | |
321 | u32 prio; | |
1da177e4 | 322 | void *data; |
dc7f9f6e | 323 | const struct tcf_proto_ops *ops; |
5bc17018 | 324 | struct tcf_chain *chain; |
4dbfa766 | 325 | refcount_t refcnt; |
25d8c0d5 | 326 | struct rcu_head rcu; |
1da177e4 LT |
327 | }; |
328 | ||
175f9c1b | 329 | struct qdisc_skb_cb { |
d58e468b PP |
330 | union { |
331 | struct { | |
332 | unsigned int pkt_len; | |
333 | u16 slave_dev_queue_mapping; | |
334 | u16 tc_classid; | |
335 | }; | |
336 | struct bpf_flow_keys *flow_keys; | |
337 | }; | |
25711786 ED |
338 | #define QDISC_CB_PRIV_LEN 20 |
339 | unsigned char data[QDISC_CB_PRIV_LEN]; | |
175f9c1b JK |
340 | }; |
341 | ||
c7eb7d72 JP |
342 | typedef void tcf_chain_head_change_t(struct tcf_proto *tp_head, void *priv); |
343 | ||
2190d1d0 | 344 | struct tcf_chain { |
ed76f5ed VB |
345 | /* Protects filter_chain. */ |
346 | struct mutex filter_chain_lock; | |
2190d1d0 | 347 | struct tcf_proto __rcu *filter_chain; |
5bc17018 JP |
348 | struct list_head list; |
349 | struct tcf_block *block; | |
350 | u32 index; /* chain index */ | |
351 | unsigned int refcnt; | |
1f3ed383 | 352 | unsigned int action_refcnt; |
32a4f5ec | 353 | bool explicitly_created; |
9f407f17 JP |
354 | const struct tcf_proto_ops *tmplt_ops; |
355 | void *tmplt_priv; | |
6529eaba JP |
356 | }; |
357 | ||
2190d1d0 | 358 | struct tcf_block { |
c266f64d VB |
359 | /* Lock protects tcf_block and lifetime-management data of chains |
360 | * attached to the block (refcnt, action_refcnt, explicitly_created). | |
361 | */ | |
362 | struct mutex lock; | |
5bc17018 | 363 | struct list_head chain_list; |
48617387 | 364 | u32 index; /* block index for shared blocks */ |
cfebd7e2 | 365 | refcount_t refcnt; |
855319be | 366 | struct net *net; |
69d78ef2 | 367 | struct Qdisc *q; |
acb67442 | 368 | struct list_head cb_list; |
f36fe1c4 JP |
369 | struct list_head owner_list; |
370 | bool keep_dst; | |
caa72601 JP |
371 | unsigned int offloadcnt; /* Number of oddloaded filters */ |
372 | unsigned int nooffloaddevcnt; /* Number of devs unable to do offload */ | |
f71e0ca4 JP |
373 | struct { |
374 | struct tcf_chain *chain; | |
375 | struct list_head filter_chain_list; | |
376 | } chain0; | |
0607e439 | 377 | struct rcu_head rcu; |
2190d1d0 JP |
378 | }; |
379 | ||
ed76f5ed VB |
380 | #ifdef CONFIG_PROVE_LOCKING |
381 | static inline bool lockdep_tcf_chain_is_locked(struct tcf_chain *chain) | |
382 | { | |
383 | return lockdep_is_held(&chain->filter_chain_lock); | |
384 | } | |
385 | #else | |
386 | static inline bool lockdep_tcf_chain_is_locked(struct tcf_block *chain) | |
387 | { | |
388 | return true; | |
389 | } | |
390 | #endif /* #ifdef CONFIG_PROVE_LOCKING */ | |
391 | ||
392 | #define tcf_chain_dereference(p, chain) \ | |
393 | rcu_dereference_protected(p, lockdep_tcf_chain_is_locked(chain)) | |
394 | ||
caa72601 JP |
395 | static inline void tcf_block_offload_inc(struct tcf_block *block, u32 *flags) |
396 | { | |
397 | if (*flags & TCA_CLS_FLAGS_IN_HW) | |
398 | return; | |
399 | *flags |= TCA_CLS_FLAGS_IN_HW; | |
400 | block->offloadcnt++; | |
401 | } | |
402 | ||
403 | static inline void tcf_block_offload_dec(struct tcf_block *block, u32 *flags) | |
404 | { | |
405 | if (!(*flags & TCA_CLS_FLAGS_IN_HW)) | |
406 | return; | |
407 | *flags &= ~TCA_CLS_FLAGS_IN_HW; | |
408 | block->offloadcnt--; | |
409 | } | |
410 | ||
31533cba | 411 | static inline void |
86c55361 | 412 | tc_cls_offload_cnt_update(struct tcf_block *block, u32 *cnt, |
31533cba JH |
413 | u32 *flags, bool add) |
414 | { | |
415 | if (add) { | |
416 | if (!*cnt) | |
417 | tcf_block_offload_inc(block, flags); | |
418 | (*cnt)++; | |
419 | } else { | |
420 | (*cnt)--; | |
421 | if (!*cnt) | |
422 | tcf_block_offload_dec(block, flags); | |
423 | } | |
424 | } | |
425 | ||
16bda13d DM |
426 | static inline void qdisc_cb_private_validate(const struct sk_buff *skb, int sz) |
427 | { | |
428 | struct qdisc_skb_cb *qcb; | |
5ee31c68 ED |
429 | |
430 | BUILD_BUG_ON(sizeof(skb->cb) < offsetof(struct qdisc_skb_cb, data) + sz); | |
16bda13d DM |
431 | BUILD_BUG_ON(sizeof(qcb->data) < sz); |
432 | } | |
433 | ||
7e66016f JF |
434 | static inline int qdisc_qlen_cpu(const struct Qdisc *q) |
435 | { | |
436 | return this_cpu_ptr(q->cpu_qstats)->qlen; | |
437 | } | |
438 | ||
05bdd2f1 | 439 | static inline int qdisc_qlen(const struct Qdisc *q) |
bbd8a0d3 KK |
440 | { |
441 | return q->q.qlen; | |
442 | } | |
443 | ||
7e66016f JF |
444 | static inline int qdisc_qlen_sum(const struct Qdisc *q) |
445 | { | |
6172abc1 | 446 | __u32 qlen = q->qstats.qlen; |
7e66016f JF |
447 | int i; |
448 | ||
449 | if (q->flags & TCQ_F_NOLOCK) { | |
450 | for_each_possible_cpu(i) | |
451 | qlen += per_cpu_ptr(q->cpu_qstats, i)->qlen; | |
452 | } else { | |
6172abc1 | 453 | qlen += q->q.qlen; |
7e66016f JF |
454 | } |
455 | ||
456 | return qlen; | |
457 | } | |
458 | ||
bfe0d029 | 459 | static inline struct qdisc_skb_cb *qdisc_skb_cb(const struct sk_buff *skb) |
175f9c1b JK |
460 | { |
461 | return (struct qdisc_skb_cb *)skb->cb; | |
462 | } | |
463 | ||
83874000 DM |
464 | static inline spinlock_t *qdisc_lock(struct Qdisc *qdisc) |
465 | { | |
466 | return &qdisc->q.lock; | |
467 | } | |
468 | ||
05bdd2f1 | 469 | static inline struct Qdisc *qdisc_root(const struct Qdisc *qdisc) |
7698b4fc | 470 | { |
46e5da40 JF |
471 | struct Qdisc *q = rcu_dereference_rtnl(qdisc->dev_queue->qdisc); |
472 | ||
473 | return q; | |
7698b4fc DM |
474 | } |
475 | ||
05bdd2f1 | 476 | static inline struct Qdisc *qdisc_root_sleeping(const struct Qdisc *qdisc) |
2540e051 JP |
477 | { |
478 | return qdisc->dev_queue->qdisc_sleeping; | |
479 | } | |
480 | ||
7e43f112 DM |
481 | /* The qdisc root lock is a mechanism by which to top level |
482 | * of a qdisc tree can be locked from any qdisc node in the | |
483 | * forest. This allows changing the configuration of some | |
484 | * aspect of the qdisc tree while blocking out asynchronous | |
485 | * qdisc access in the packet processing paths. | |
486 | * | |
487 | * It is only legal to do this when the root will not change | |
488 | * on us. Otherwise we'll potentially lock the wrong qdisc | |
489 | * root. This is enforced by holding the RTNL semaphore, which | |
490 | * all users of this lock accessor must do. | |
491 | */ | |
05bdd2f1 | 492 | static inline spinlock_t *qdisc_root_lock(const struct Qdisc *qdisc) |
7698b4fc DM |
493 | { |
494 | struct Qdisc *root = qdisc_root(qdisc); | |
495 | ||
7e43f112 | 496 | ASSERT_RTNL(); |
83874000 | 497 | return qdisc_lock(root); |
7698b4fc DM |
498 | } |
499 | ||
05bdd2f1 | 500 | static inline spinlock_t *qdisc_root_sleeping_lock(const struct Qdisc *qdisc) |
f6f9b93f JP |
501 | { |
502 | struct Qdisc *root = qdisc_root_sleeping(qdisc); | |
503 | ||
504 | ASSERT_RTNL(); | |
505 | return qdisc_lock(root); | |
506 | } | |
507 | ||
edb09eb1 ED |
508 | static inline seqcount_t *qdisc_root_sleeping_running(const struct Qdisc *qdisc) |
509 | { | |
510 | struct Qdisc *root = qdisc_root_sleeping(qdisc); | |
511 | ||
512 | ASSERT_RTNL(); | |
513 | return &root->running; | |
514 | } | |
515 | ||
05bdd2f1 | 516 | static inline struct net_device *qdisc_dev(const struct Qdisc *qdisc) |
5ce2d488 DM |
517 | { |
518 | return qdisc->dev_queue->dev; | |
519 | } | |
1da177e4 | 520 | |
05bdd2f1 | 521 | static inline void sch_tree_lock(const struct Qdisc *q) |
78a5b30b | 522 | { |
fe439dd0 | 523 | spin_lock_bh(qdisc_root_sleeping_lock(q)); |
78a5b30b DM |
524 | } |
525 | ||
05bdd2f1 | 526 | static inline void sch_tree_unlock(const struct Qdisc *q) |
78a5b30b | 527 | { |
fe439dd0 | 528 | spin_unlock_bh(qdisc_root_sleeping_lock(q)); |
78a5b30b DM |
529 | } |
530 | ||
e41a33e6 TG |
531 | extern struct Qdisc noop_qdisc; |
532 | extern struct Qdisc_ops noop_qdisc_ops; | |
6ec1c69a DM |
533 | extern struct Qdisc_ops pfifo_fast_ops; |
534 | extern struct Qdisc_ops mq_qdisc_ops; | |
d66d6c31 | 535 | extern struct Qdisc_ops noqueue_qdisc_ops; |
6da7c8fc | 536 | extern const struct Qdisc_ops *default_qdisc_ops; |
1f27cde3 ED |
537 | static inline const struct Qdisc_ops * |
538 | get_default_qdisc_ops(const struct net_device *dev, int ntx) | |
539 | { | |
540 | return ntx < dev->real_num_tx_queues ? | |
541 | default_qdisc_ops : &pfifo_fast_ops; | |
542 | } | |
e41a33e6 | 543 | |
fd2c3ef7 | 544 | struct Qdisc_class_common { |
6fe1c7a5 PM |
545 | u32 classid; |
546 | struct hlist_node hnode; | |
547 | }; | |
548 | ||
fd2c3ef7 | 549 | struct Qdisc_class_hash { |
6fe1c7a5 PM |
550 | struct hlist_head *hash; |
551 | unsigned int hashsize; | |
552 | unsigned int hashmask; | |
553 | unsigned int hashelems; | |
554 | }; | |
555 | ||
556 | static inline unsigned int qdisc_class_hash(u32 id, u32 mask) | |
557 | { | |
558 | id ^= id >> 8; | |
559 | id ^= id >> 4; | |
560 | return id & mask; | |
561 | } | |
562 | ||
563 | static inline struct Qdisc_class_common * | |
05bdd2f1 | 564 | qdisc_class_find(const struct Qdisc_class_hash *hash, u32 id) |
6fe1c7a5 PM |
565 | { |
566 | struct Qdisc_class_common *cl; | |
6fe1c7a5 PM |
567 | unsigned int h; |
568 | ||
7d3f0cd4 GF |
569 | if (!id) |
570 | return NULL; | |
571 | ||
6fe1c7a5 | 572 | h = qdisc_class_hash(id, hash->hashmask); |
b67bfe0d | 573 | hlist_for_each_entry(cl, &hash->hash[h], hnode) { |
6fe1c7a5 PM |
574 | if (cl->classid == id) |
575 | return cl; | |
576 | } | |
577 | return NULL; | |
578 | } | |
579 | ||
384c181e AN |
580 | static inline int tc_classid_to_hwtc(struct net_device *dev, u32 classid) |
581 | { | |
582 | u32 hwtc = TC_H_MIN(classid) - TC_H_MIN_PRIORITY; | |
583 | ||
584 | return (hwtc < netdev_get_num_tc(dev)) ? hwtc : -EINVAL; | |
585 | } | |
586 | ||
5c15257f JP |
587 | int qdisc_class_hash_init(struct Qdisc_class_hash *); |
588 | void qdisc_class_hash_insert(struct Qdisc_class_hash *, | |
589 | struct Qdisc_class_common *); | |
590 | void qdisc_class_hash_remove(struct Qdisc_class_hash *, | |
591 | struct Qdisc_class_common *); | |
592 | void qdisc_class_hash_grow(struct Qdisc *, struct Qdisc_class_hash *); | |
593 | void qdisc_class_hash_destroy(struct Qdisc_class_hash *); | |
594 | ||
48bfd55e | 595 | int dev_qdisc_change_tx_queue_len(struct net_device *dev); |
5c15257f JP |
596 | void dev_init_scheduler(struct net_device *dev); |
597 | void dev_shutdown(struct net_device *dev); | |
598 | void dev_activate(struct net_device *dev); | |
599 | void dev_deactivate(struct net_device *dev); | |
600 | void dev_deactivate_many(struct list_head *head); | |
601 | struct Qdisc *dev_graft_qdisc(struct netdev_queue *dev_queue, | |
602 | struct Qdisc *qdisc); | |
603 | void qdisc_reset(struct Qdisc *qdisc); | |
86bd446b | 604 | void qdisc_put(struct Qdisc *qdisc); |
3a7d0d07 | 605 | void qdisc_put_unlocked(struct Qdisc *qdisc); |
5f2939d9 | 606 | void qdisc_tree_reduce_backlog(struct Qdisc *qdisc, int n, int len); |
b592843c JK |
607 | #ifdef CONFIG_NET_SCHED |
608 | int qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type, | |
609 | void *type_data); | |
bfaee911 JK |
610 | void qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch, |
611 | struct Qdisc *new, struct Qdisc *old, | |
612 | enum tc_setup_type type, void *type_data, | |
613 | struct netlink_ext_ack *extack); | |
b592843c JK |
614 | #else |
615 | static inline int | |
616 | qdisc_offload_dump_helper(struct Qdisc *q, enum tc_setup_type type, | |
617 | void *type_data) | |
618 | { | |
619 | q->flags &= ~TCQ_F_OFFLOADED; | |
620 | return 0; | |
621 | } | |
bfaee911 JK |
622 | |
623 | static inline void | |
624 | qdisc_offload_graft_helper(struct net_device *dev, struct Qdisc *sch, | |
625 | struct Qdisc *new, struct Qdisc *old, | |
626 | enum tc_setup_type type, void *type_data, | |
627 | struct netlink_ext_ack *extack) | |
628 | { | |
629 | } | |
b592843c | 630 | #endif |
5c15257f | 631 | struct Qdisc *qdisc_alloc(struct netdev_queue *dev_queue, |
d0bd684d AA |
632 | const struct Qdisc_ops *ops, |
633 | struct netlink_ext_ack *extack); | |
81d947e2 | 634 | void qdisc_free(struct Qdisc *qdisc); |
5c15257f | 635 | struct Qdisc *qdisc_create_dflt(struct netdev_queue *dev_queue, |
a38a9882 AA |
636 | const struct Qdisc_ops *ops, u32 parentid, |
637 | struct netlink_ext_ack *extack); | |
5c15257f JP |
638 | void __qdisc_calculate_pkt_len(struct sk_buff *skb, |
639 | const struct qdisc_size_table *stab); | |
27b29f63 | 640 | int skb_do_redirect(struct sk_buff *); |
1da177e4 | 641 | |
a5135bcf WB |
642 | static inline void skb_reset_tc(struct sk_buff *skb) |
643 | { | |
644 | #ifdef CONFIG_NET_CLS_ACT | |
bc31c905 | 645 | skb->tc_redirected = 0; |
a5135bcf WB |
646 | #endif |
647 | } | |
648 | ||
cd11b164 PA |
649 | static inline bool skb_is_tc_redirected(const struct sk_buff *skb) |
650 | { | |
651 | #ifdef CONFIG_NET_CLS_ACT | |
652 | return skb->tc_redirected; | |
653 | #else | |
654 | return false; | |
655 | #endif | |
656 | } | |
657 | ||
fdc5432a DB |
658 | static inline bool skb_at_tc_ingress(const struct sk_buff *skb) |
659 | { | |
660 | #ifdef CONFIG_NET_CLS_ACT | |
8dc07fdb | 661 | return skb->tc_at_ingress; |
fdc5432a DB |
662 | #else |
663 | return false; | |
664 | #endif | |
665 | } | |
666 | ||
e7246e12 WB |
667 | static inline bool skb_skip_tc_classify(struct sk_buff *skb) |
668 | { | |
669 | #ifdef CONFIG_NET_CLS_ACT | |
670 | if (skb->tc_skip_classify) { | |
671 | skb->tc_skip_classify = 0; | |
672 | return true; | |
673 | } | |
674 | #endif | |
675 | return false; | |
676 | } | |
677 | ||
3a053b1a | 678 | /* Reset all TX qdiscs greater than index of a device. */ |
f0796d5c | 679 | static inline void qdisc_reset_all_tx_gt(struct net_device *dev, unsigned int i) |
5aa70995 | 680 | { |
4ef6acff JF |
681 | struct Qdisc *qdisc; |
682 | ||
f0796d5c | 683 | for (; i < dev->num_tx_queues; i++) { |
46e5da40 | 684 | qdisc = rtnl_dereference(netdev_get_tx_queue(dev, i)->qdisc); |
4ef6acff JF |
685 | if (qdisc) { |
686 | spin_lock_bh(qdisc_lock(qdisc)); | |
687 | qdisc_reset(qdisc); | |
688 | spin_unlock_bh(qdisc_lock(qdisc)); | |
689 | } | |
690 | } | |
5aa70995 DM |
691 | } |
692 | ||
5aa70995 DM |
693 | static inline void qdisc_reset_all_tx(struct net_device *dev) |
694 | { | |
f0796d5c | 695 | qdisc_reset_all_tx_gt(dev, 0); |
5aa70995 DM |
696 | } |
697 | ||
3e745dd6 DM |
698 | /* Are all TX queues of the device empty? */ |
699 | static inline bool qdisc_all_tx_empty(const struct net_device *dev) | |
700 | { | |
e8a0464c | 701 | unsigned int i; |
46e5da40 JF |
702 | |
703 | rcu_read_lock(); | |
e8a0464c DM |
704 | for (i = 0; i < dev->num_tx_queues; i++) { |
705 | struct netdev_queue *txq = netdev_get_tx_queue(dev, i); | |
46e5da40 | 706 | const struct Qdisc *q = rcu_dereference(txq->qdisc); |
3e745dd6 | 707 | |
46e5da40 JF |
708 | if (q->q.qlen) { |
709 | rcu_read_unlock(); | |
e8a0464c | 710 | return false; |
46e5da40 | 711 | } |
e8a0464c | 712 | } |
46e5da40 | 713 | rcu_read_unlock(); |
e8a0464c | 714 | return true; |
3e745dd6 DM |
715 | } |
716 | ||
6fa9864b | 717 | /* Are any of the TX qdiscs changing? */ |
05bdd2f1 | 718 | static inline bool qdisc_tx_changing(const struct net_device *dev) |
6fa9864b | 719 | { |
e8a0464c | 720 | unsigned int i; |
46e5da40 | 721 | |
e8a0464c DM |
722 | for (i = 0; i < dev->num_tx_queues; i++) { |
723 | struct netdev_queue *txq = netdev_get_tx_queue(dev, i); | |
46e5da40 | 724 | if (rcu_access_pointer(txq->qdisc) != txq->qdisc_sleeping) |
e8a0464c DM |
725 | return true; |
726 | } | |
727 | return false; | |
6fa9864b DM |
728 | } |
729 | ||
e8a0464c | 730 | /* Is the device using the noop qdisc on all queues? */ |
05297949 DM |
731 | static inline bool qdisc_tx_is_noop(const struct net_device *dev) |
732 | { | |
e8a0464c | 733 | unsigned int i; |
46e5da40 | 734 | |
e8a0464c DM |
735 | for (i = 0; i < dev->num_tx_queues; i++) { |
736 | struct netdev_queue *txq = netdev_get_tx_queue(dev, i); | |
46e5da40 | 737 | if (rcu_access_pointer(txq->qdisc) != &noop_qdisc) |
e8a0464c DM |
738 | return false; |
739 | } | |
740 | return true; | |
05297949 DM |
741 | } |
742 | ||
bfe0d029 | 743 | static inline unsigned int qdisc_pkt_len(const struct sk_buff *skb) |
0abf77e5 | 744 | { |
175f9c1b | 745 | return qdisc_skb_cb(skb)->pkt_len; |
0abf77e5 JK |
746 | } |
747 | ||
c27f339a | 748 | /* additional qdisc xmit flags (NET_XMIT_MASK in linux/netdevice.h) */ |
378a2f09 JP |
749 | enum net_xmit_qdisc_t { |
750 | __NET_XMIT_STOLEN = 0x00010000, | |
c27f339a | 751 | __NET_XMIT_BYPASS = 0x00020000, |
378a2f09 JP |
752 | }; |
753 | ||
c27f339a | 754 | #ifdef CONFIG_NET_CLS_ACT |
378a2f09 | 755 | #define net_xmit_drop_count(e) ((e) & __NET_XMIT_STOLEN ? 0 : 1) |
378a2f09 JP |
756 | #else |
757 | #define net_xmit_drop_count(e) (1) | |
758 | #endif | |
759 | ||
a2da570d ED |
760 | static inline void qdisc_calculate_pkt_len(struct sk_buff *skb, |
761 | const struct Qdisc *sch) | |
5f86173b | 762 | { |
3a682fbd | 763 | #ifdef CONFIG_NET_SCHED |
a2da570d ED |
764 | struct qdisc_size_table *stab = rcu_dereference_bh(sch->stab); |
765 | ||
766 | if (stab) | |
767 | __qdisc_calculate_pkt_len(skb, stab); | |
3a682fbd | 768 | #endif |
a2da570d ED |
769 | } |
770 | ||
520ac30f ED |
771 | static inline int qdisc_enqueue(struct sk_buff *skb, struct Qdisc *sch, |
772 | struct sk_buff **to_free) | |
a2da570d ED |
773 | { |
774 | qdisc_calculate_pkt_len(skb, sch); | |
520ac30f | 775 | return sch->enqueue(skb, sch, to_free); |
5f86173b JK |
776 | } |
777 | ||
22e0f8b9 JF |
778 | static inline bool qdisc_is_percpu_stats(const struct Qdisc *q) |
779 | { | |
780 | return q->flags & TCQ_F_CPUSTATS; | |
781 | } | |
bfe0d029 | 782 | |
38040702 AV |
783 | static inline void _bstats_update(struct gnet_stats_basic_packed *bstats, |
784 | __u64 bytes, __u32 packets) | |
785 | { | |
786 | bstats->bytes += bytes; | |
787 | bstats->packets += packets; | |
788 | } | |
789 | ||
bfe0d029 ED |
790 | static inline void bstats_update(struct gnet_stats_basic_packed *bstats, |
791 | const struct sk_buff *skb) | |
792 | { | |
38040702 AV |
793 | _bstats_update(bstats, |
794 | qdisc_pkt_len(skb), | |
795 | skb_is_gso(skb) ? skb_shinfo(skb)->gso_segs : 1); | |
796 | } | |
797 | ||
798 | static inline void _bstats_cpu_update(struct gnet_stats_basic_cpu *bstats, | |
799 | __u64 bytes, __u32 packets) | |
800 | { | |
801 | u64_stats_update_begin(&bstats->syncp); | |
802 | _bstats_update(&bstats->bstats, bytes, packets); | |
803 | u64_stats_update_end(&bstats->syncp); | |
bfe0d029 ED |
804 | } |
805 | ||
24ea591d ED |
806 | static inline void bstats_cpu_update(struct gnet_stats_basic_cpu *bstats, |
807 | const struct sk_buff *skb) | |
22e0f8b9 | 808 | { |
22e0f8b9 JF |
809 | u64_stats_update_begin(&bstats->syncp); |
810 | bstats_update(&bstats->bstats, skb); | |
811 | u64_stats_update_end(&bstats->syncp); | |
812 | } | |
813 | ||
24ea591d ED |
814 | static inline void qdisc_bstats_cpu_update(struct Qdisc *sch, |
815 | const struct sk_buff *skb) | |
816 | { | |
817 | bstats_cpu_update(this_cpu_ptr(sch->cpu_bstats), skb); | |
818 | } | |
819 | ||
bfe0d029 ED |
820 | static inline void qdisc_bstats_update(struct Qdisc *sch, |
821 | const struct sk_buff *skb) | |
bbd8a0d3 | 822 | { |
bfe0d029 | 823 | bstats_update(&sch->bstats, skb); |
bbd8a0d3 KK |
824 | } |
825 | ||
25331d6c JF |
826 | static inline void qdisc_qstats_backlog_dec(struct Qdisc *sch, |
827 | const struct sk_buff *skb) | |
828 | { | |
829 | sch->qstats.backlog -= qdisc_pkt_len(skb); | |
830 | } | |
831 | ||
40bd0362 JF |
832 | static inline void qdisc_qstats_cpu_backlog_dec(struct Qdisc *sch, |
833 | const struct sk_buff *skb) | |
834 | { | |
835 | this_cpu_sub(sch->cpu_qstats->backlog, qdisc_pkt_len(skb)); | |
836 | } | |
837 | ||
25331d6c JF |
838 | static inline void qdisc_qstats_backlog_inc(struct Qdisc *sch, |
839 | const struct sk_buff *skb) | |
840 | { | |
841 | sch->qstats.backlog += qdisc_pkt_len(skb); | |
842 | } | |
843 | ||
40bd0362 JF |
844 | static inline void qdisc_qstats_cpu_backlog_inc(struct Qdisc *sch, |
845 | const struct sk_buff *skb) | |
846 | { | |
847 | this_cpu_add(sch->cpu_qstats->backlog, qdisc_pkt_len(skb)); | |
848 | } | |
849 | ||
850 | static inline void qdisc_qstats_cpu_qlen_inc(struct Qdisc *sch) | |
851 | { | |
852 | this_cpu_inc(sch->cpu_qstats->qlen); | |
853 | } | |
854 | ||
855 | static inline void qdisc_qstats_cpu_qlen_dec(struct Qdisc *sch) | |
856 | { | |
857 | this_cpu_dec(sch->cpu_qstats->qlen); | |
858 | } | |
859 | ||
860 | static inline void qdisc_qstats_cpu_requeues_inc(struct Qdisc *sch) | |
861 | { | |
862 | this_cpu_inc(sch->cpu_qstats->requeues); | |
863 | } | |
864 | ||
25331d6c JF |
865 | static inline void __qdisc_qstats_drop(struct Qdisc *sch, int count) |
866 | { | |
867 | sch->qstats.drops += count; | |
868 | } | |
869 | ||
24ea591d | 870 | static inline void qstats_drop_inc(struct gnet_stats_queue *qstats) |
25331d6c | 871 | { |
24ea591d | 872 | qstats->drops++; |
25331d6c JF |
873 | } |
874 | ||
24ea591d | 875 | static inline void qstats_overlimit_inc(struct gnet_stats_queue *qstats) |
b0ab6f92 | 876 | { |
24ea591d ED |
877 | qstats->overlimits++; |
878 | } | |
b0ab6f92 | 879 | |
24ea591d ED |
880 | static inline void qdisc_qstats_drop(struct Qdisc *sch) |
881 | { | |
882 | qstats_drop_inc(&sch->qstats); | |
883 | } | |
884 | ||
885 | static inline void qdisc_qstats_cpu_drop(struct Qdisc *sch) | |
886 | { | |
eb60a8dd | 887 | this_cpu_inc(sch->cpu_qstats->drops); |
b0ab6f92 JF |
888 | } |
889 | ||
25331d6c JF |
890 | static inline void qdisc_qstats_overlimit(struct Qdisc *sch) |
891 | { | |
892 | sch->qstats.overlimits++; | |
893 | } | |
894 | ||
48da34b7 FW |
895 | static inline void qdisc_skb_head_init(struct qdisc_skb_head *qh) |
896 | { | |
897 | qh->head = NULL; | |
898 | qh->tail = NULL; | |
899 | qh->qlen = 0; | |
900 | } | |
901 | ||
aea890b8 DM |
902 | static inline void __qdisc_enqueue_tail(struct sk_buff *skb, |
903 | struct qdisc_skb_head *qh) | |
9972b25d | 904 | { |
48da34b7 FW |
905 | struct sk_buff *last = qh->tail; |
906 | ||
907 | if (last) { | |
908 | skb->next = NULL; | |
909 | last->next = skb; | |
910 | qh->tail = skb; | |
911 | } else { | |
912 | qh->tail = skb; | |
913 | qh->head = skb; | |
914 | } | |
915 | qh->qlen++; | |
9972b25d TG |
916 | } |
917 | ||
918 | static inline int qdisc_enqueue_tail(struct sk_buff *skb, struct Qdisc *sch) | |
919 | { | |
aea890b8 DM |
920 | __qdisc_enqueue_tail(skb, &sch->q); |
921 | qdisc_qstats_backlog_inc(sch, skb); | |
922 | return NET_XMIT_SUCCESS; | |
9972b25d TG |
923 | } |
924 | ||
59697730 DM |
925 | static inline void __qdisc_enqueue_head(struct sk_buff *skb, |
926 | struct qdisc_skb_head *qh) | |
927 | { | |
928 | skb->next = qh->head; | |
929 | ||
930 | if (!qh->head) | |
931 | qh->tail = skb; | |
932 | qh->head = skb; | |
933 | qh->qlen++; | |
934 | } | |
935 | ||
48da34b7 | 936 | static inline struct sk_buff *__qdisc_dequeue_head(struct qdisc_skb_head *qh) |
9972b25d | 937 | { |
48da34b7 FW |
938 | struct sk_buff *skb = qh->head; |
939 | ||
940 | if (likely(skb != NULL)) { | |
941 | qh->head = skb->next; | |
942 | qh->qlen--; | |
943 | if (qh->head == NULL) | |
944 | qh->tail = NULL; | |
945 | skb->next = NULL; | |
946 | } | |
9972b25d | 947 | |
ec323368 FW |
948 | return skb; |
949 | } | |
950 | ||
951 | static inline struct sk_buff *qdisc_dequeue_head(struct Qdisc *sch) | |
952 | { | |
953 | struct sk_buff *skb = __qdisc_dequeue_head(&sch->q); | |
954 | ||
9190b3b3 | 955 | if (likely(skb != NULL)) { |
25331d6c | 956 | qdisc_qstats_backlog_dec(sch, skb); |
9190b3b3 ED |
957 | qdisc_bstats_update(sch, skb); |
958 | } | |
9972b25d TG |
959 | |
960 | return skb; | |
961 | } | |
962 | ||
520ac30f ED |
963 | /* Instead of calling kfree_skb() while root qdisc lock is held, |
964 | * queue the skb for future freeing at end of __dev_xmit_skb() | |
965 | */ | |
966 | static inline void __qdisc_drop(struct sk_buff *skb, struct sk_buff **to_free) | |
967 | { | |
968 | skb->next = *to_free; | |
969 | *to_free = skb; | |
970 | } | |
971 | ||
35d889d1 AK |
972 | static inline void __qdisc_drop_all(struct sk_buff *skb, |
973 | struct sk_buff **to_free) | |
974 | { | |
975 | if (skb->prev) | |
976 | skb->prev->next = *to_free; | |
977 | else | |
978 | skb->next = *to_free; | |
979 | *to_free = skb; | |
980 | } | |
981 | ||
57dbb2d8 | 982 | static inline unsigned int __qdisc_queue_drop_head(struct Qdisc *sch, |
48da34b7 | 983 | struct qdisc_skb_head *qh, |
520ac30f | 984 | struct sk_buff **to_free) |
57dbb2d8 | 985 | { |
48da34b7 | 986 | struct sk_buff *skb = __qdisc_dequeue_head(qh); |
57dbb2d8 HPP |
987 | |
988 | if (likely(skb != NULL)) { | |
989 | unsigned int len = qdisc_pkt_len(skb); | |
520ac30f | 990 | |
25331d6c | 991 | qdisc_qstats_backlog_dec(sch, skb); |
520ac30f | 992 | __qdisc_drop(skb, to_free); |
57dbb2d8 HPP |
993 | return len; |
994 | } | |
995 | ||
996 | return 0; | |
997 | } | |
998 | ||
520ac30f ED |
999 | static inline unsigned int qdisc_queue_drop_head(struct Qdisc *sch, |
1000 | struct sk_buff **to_free) | |
57dbb2d8 | 1001 | { |
520ac30f | 1002 | return __qdisc_queue_drop_head(sch, &sch->q, to_free); |
9972b25d TG |
1003 | } |
1004 | ||
48a8f519 PM |
1005 | static inline struct sk_buff *qdisc_peek_head(struct Qdisc *sch) |
1006 | { | |
48da34b7 FW |
1007 | const struct qdisc_skb_head *qh = &sch->q; |
1008 | ||
1009 | return qh->head; | |
48a8f519 PM |
1010 | } |
1011 | ||
77be155c JP |
1012 | /* generic pseudo peek method for non-work-conserving qdisc */ |
1013 | static inline struct sk_buff *qdisc_peek_dequeued(struct Qdisc *sch) | |
1014 | { | |
a53851e2 JF |
1015 | struct sk_buff *skb = skb_peek(&sch->gso_skb); |
1016 | ||
77be155c | 1017 | /* we can reuse ->gso_skb because peek isn't called for root qdiscs */ |
a53851e2 JF |
1018 | if (!skb) { |
1019 | skb = sch->dequeue(sch); | |
1020 | ||
1021 | if (skb) { | |
1022 | __skb_queue_head(&sch->gso_skb, skb); | |
61c9eaf9 | 1023 | /* it's still part of the queue */ |
a53851e2 | 1024 | qdisc_qstats_backlog_inc(sch, skb); |
61c9eaf9 | 1025 | sch->q.qlen++; |
a27758ff | 1026 | } |
61c9eaf9 | 1027 | } |
77be155c | 1028 | |
a53851e2 | 1029 | return skb; |
77be155c JP |
1030 | } |
1031 | ||
1032 | /* use instead of qdisc->dequeue() for all qdiscs queried with ->peek() */ | |
1033 | static inline struct sk_buff *qdisc_dequeue_peeked(struct Qdisc *sch) | |
1034 | { | |
a53851e2 | 1035 | struct sk_buff *skb = skb_peek(&sch->gso_skb); |
77be155c | 1036 | |
61c9eaf9 | 1037 | if (skb) { |
a53851e2 | 1038 | skb = __skb_dequeue(&sch->gso_skb); |
a27758ff | 1039 | qdisc_qstats_backlog_dec(sch, skb); |
61c9eaf9 JP |
1040 | sch->q.qlen--; |
1041 | } else { | |
77be155c | 1042 | skb = sch->dequeue(sch); |
61c9eaf9 | 1043 | } |
77be155c JP |
1044 | |
1045 | return skb; | |
1046 | } | |
1047 | ||
48da34b7 | 1048 | static inline void __qdisc_reset_queue(struct qdisc_skb_head *qh) |
9972b25d TG |
1049 | { |
1050 | /* | |
1051 | * We do not know the backlog in bytes of this list, it | |
1052 | * is up to the caller to correct it | |
1053 | */ | |
48da34b7 FW |
1054 | ASSERT_RTNL(); |
1055 | if (qh->qlen) { | |
1056 | rtnl_kfree_skbs(qh->head, qh->tail); | |
1057 | ||
1058 | qh->head = NULL; | |
1059 | qh->tail = NULL; | |
1060 | qh->qlen = 0; | |
1b5c5493 | 1061 | } |
9972b25d TG |
1062 | } |
1063 | ||
1064 | static inline void qdisc_reset_queue(struct Qdisc *sch) | |
1065 | { | |
1b5c5493 | 1066 | __qdisc_reset_queue(&sch->q); |
9972b25d TG |
1067 | sch->qstats.backlog = 0; |
1068 | } | |
1069 | ||
86a7996c WC |
1070 | static inline struct Qdisc *qdisc_replace(struct Qdisc *sch, struct Qdisc *new, |
1071 | struct Qdisc **pold) | |
1072 | { | |
1073 | struct Qdisc *old; | |
1074 | ||
1075 | sch_tree_lock(sch); | |
1076 | old = *pold; | |
1077 | *pold = new; | |
1078 | if (old != NULL) { | |
68a66d14 KK |
1079 | unsigned int qlen = old->q.qlen; |
1080 | unsigned int backlog = old->qstats.backlog; | |
1081 | ||
86a7996c | 1082 | qdisc_reset(old); |
68a66d14 | 1083 | qdisc_tree_reduce_backlog(old, qlen, backlog); |
86a7996c WC |
1084 | } |
1085 | sch_tree_unlock(sch); | |
1086 | ||
1087 | return old; | |
1088 | } | |
1089 | ||
1b5c5493 ED |
1090 | static inline void rtnl_qdisc_drop(struct sk_buff *skb, struct Qdisc *sch) |
1091 | { | |
1092 | rtnl_kfree_skbs(skb, skb); | |
1093 | qdisc_qstats_drop(sch); | |
1094 | } | |
1095 | ||
40bd0362 JF |
1096 | static inline int qdisc_drop_cpu(struct sk_buff *skb, struct Qdisc *sch, |
1097 | struct sk_buff **to_free) | |
1098 | { | |
1099 | __qdisc_drop(skb, to_free); | |
1100 | qdisc_qstats_cpu_drop(sch); | |
1101 | ||
1102 | return NET_XMIT_DROP; | |
1103 | } | |
520ac30f ED |
1104 | |
1105 | static inline int qdisc_drop(struct sk_buff *skb, struct Qdisc *sch, | |
1106 | struct sk_buff **to_free) | |
9972b25d | 1107 | { |
520ac30f | 1108 | __qdisc_drop(skb, to_free); |
25331d6c | 1109 | qdisc_qstats_drop(sch); |
9972b25d TG |
1110 | |
1111 | return NET_XMIT_DROP; | |
1112 | } | |
1113 | ||
35d889d1 AK |
1114 | static inline int qdisc_drop_all(struct sk_buff *skb, struct Qdisc *sch, |
1115 | struct sk_buff **to_free) | |
1116 | { | |
1117 | __qdisc_drop_all(skb, to_free); | |
1118 | qdisc_qstats_drop(sch); | |
1119 | ||
1120 | return NET_XMIT_DROP; | |
1121 | } | |
1122 | ||
e9bef55d JDB |
1123 | /* Length to Time (L2T) lookup in a qdisc_rate_table, to determine how |
1124 | long it will take to send a packet given its size. | |
1125 | */ | |
1126 | static inline u32 qdisc_l2t(struct qdisc_rate_table* rtab, unsigned int pktlen) | |
1127 | { | |
e08b0998 JDB |
1128 | int slot = pktlen + rtab->rate.cell_align + rtab->rate.overhead; |
1129 | if (slot < 0) | |
1130 | slot = 0; | |
e9bef55d JDB |
1131 | slot >>= rtab->rate.cell_log; |
1132 | if (slot > 255) | |
a02cec21 | 1133 | return rtab->data[255]*(slot >> 8) + rtab->data[slot & 0xFF]; |
e9bef55d JDB |
1134 | return rtab->data[slot]; |
1135 | } | |
1136 | ||
292f1c7f | 1137 | struct psched_ratecfg { |
130d3d68 | 1138 | u64 rate_bytes_ps; /* bytes per second */ |
01cb71d2 ED |
1139 | u32 mult; |
1140 | u16 overhead; | |
8a8e3d84 | 1141 | u8 linklayer; |
01cb71d2 | 1142 | u8 shift; |
292f1c7f JP |
1143 | }; |
1144 | ||
1145 | static inline u64 psched_l2t_ns(const struct psched_ratecfg *r, | |
1146 | unsigned int len) | |
1147 | { | |
8a8e3d84 JDB |
1148 | len += r->overhead; |
1149 | ||
1150 | if (unlikely(r->linklayer == TC_LINKLAYER_ATM)) | |
1151 | return ((u64)(DIV_ROUND_UP(len,48)*53) * r->mult) >> r->shift; | |
1152 | ||
1153 | return ((u64)len * r->mult) >> r->shift; | |
292f1c7f JP |
1154 | } |
1155 | ||
5c15257f | 1156 | void psched_ratecfg_precompute(struct psched_ratecfg *r, |
3e1e3aae ED |
1157 | const struct tc_ratespec *conf, |
1158 | u64 rate64); | |
292f1c7f | 1159 | |
01cb71d2 ED |
1160 | static inline void psched_ratecfg_getrate(struct tc_ratespec *res, |
1161 | const struct psched_ratecfg *r) | |
292f1c7f | 1162 | { |
01cb71d2 | 1163 | memset(res, 0, sizeof(*res)); |
3e1e3aae ED |
1164 | |
1165 | /* legacy struct tc_ratespec has a 32bit @rate field | |
1166 | * Qdisc using 64bit rate should add new attributes | |
1167 | * in order to maintain compatibility. | |
1168 | */ | |
1169 | res->rate = min_t(u64, r->rate_bytes_ps, ~0U); | |
1170 | ||
01cb71d2 | 1171 | res->overhead = r->overhead; |
8a8e3d84 | 1172 | res->linklayer = (r->linklayer & TC_LINKLAYER_MASK); |
292f1c7f JP |
1173 | } |
1174 | ||
46209401 JP |
1175 | /* Mini Qdisc serves for specific needs of ingress/clsact Qdisc. |
1176 | * The fast path only needs to access filter list and to update stats | |
1177 | */ | |
1178 | struct mini_Qdisc { | |
1179 | struct tcf_proto *filter_list; | |
1180 | struct gnet_stats_basic_cpu __percpu *cpu_bstats; | |
1181 | struct gnet_stats_queue __percpu *cpu_qstats; | |
1182 | struct rcu_head rcu; | |
1183 | }; | |
1184 | ||
1185 | static inline void mini_qdisc_bstats_cpu_update(struct mini_Qdisc *miniq, | |
1186 | const struct sk_buff *skb) | |
1187 | { | |
1188 | bstats_cpu_update(this_cpu_ptr(miniq->cpu_bstats), skb); | |
1189 | } | |
1190 | ||
1191 | static inline void mini_qdisc_qstats_cpu_drop(struct mini_Qdisc *miniq) | |
1192 | { | |
1193 | this_cpu_inc(miniq->cpu_qstats->drops); | |
1194 | } | |
1195 | ||
1196 | struct mini_Qdisc_pair { | |
1197 | struct mini_Qdisc miniq1; | |
1198 | struct mini_Qdisc miniq2; | |
1199 | struct mini_Qdisc __rcu **p_miniq; | |
1200 | }; | |
1201 | ||
1202 | void mini_qdisc_pair_swap(struct mini_Qdisc_pair *miniqp, | |
1203 | struct tcf_proto *tp_head); | |
1204 | void mini_qdisc_pair_init(struct mini_Qdisc_pair *miniqp, struct Qdisc *qdisc, | |
1205 | struct mini_Qdisc __rcu **p_miniq); | |
1206 | ||
cd11b164 PA |
1207 | static inline void skb_tc_reinsert(struct sk_buff *skb, struct tcf_result *res) |
1208 | { | |
1209 | struct gnet_stats_queue *stats = res->qstats; | |
1210 | int ret; | |
1211 | ||
1212 | if (res->ingress) | |
1213 | ret = netif_receive_skb(skb); | |
1214 | else | |
1215 | ret = dev_queue_xmit(skb); | |
1216 | if (ret && stats) | |
1217 | qstats_overlimit_inc(res->qstats); | |
1218 | } | |
1219 | ||
1da177e4 | 1220 | #endif |