]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - include/net/pkt_cls.h
net: flow_offload: rename TCF_BLOCK_BINDER_TYPE_* to FLOW_BLOCK_BINDER_TYPE_*
[mirror_ubuntu-hirsute-kernel.git] / include / net / pkt_cls.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2#ifndef __NET_PKT_CLS_H
3#define __NET_PKT_CLS_H
4
5#include <linux/pkt_cls.h>
7aa0045d 6#include <linux/workqueue.h>
1da177e4
LT
7#include <net/sch_generic.h>
8#include <net/act_api.h>
8f256622 9#include <net/flow_offload.h>
a5148626 10#include <net/net_namespace.h>
1da177e4 11
cd11b164 12/* TC action not accessible from user space */
720f22fe 13#define TC_ACT_CONSUMED (TC_ACT_VALUE_MAX + 1)
cd11b164 14
1da177e4
LT
15/* Basic packet classifier frontend definitions. */
16
fd2c3ef7 17struct tcf_walker {
1da177e4
LT
18 int stop;
19 int skip;
20 int count;
6676d5e4 21 bool nonempty;
01683a14 22 unsigned long cookie;
8113c095 23 int (*fn)(struct tcf_proto *, void *node, struct tcf_walker *);
1da177e4
LT
24};
25
5c15257f
JP
26int register_tcf_proto_ops(struct tcf_proto_ops *ops);
27int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
1da177e4 28
4e95bc26 29#define tc_block_offload flow_block_offload
8c4083b3
JP
30
31struct tcf_block_ext_info {
32f8c409 32 enum flow_block_binder_type binder_type;
c7eb7d72
JP
33 tcf_chain_head_change_t *chain_head_change;
34 void *chain_head_change_priv;
48617387 35 u32 block_index;
8c4083b3
JP
36};
37
acb67442 38struct tcf_block_cb;
aaa908ff 39bool tcf_queue_work(struct rcu_work *rwork, work_func_t func);
acb67442 40
8ae70032 41#ifdef CONFIG_NET_CLS
1f3ed383
JP
42struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block,
43 u32 chain_index);
1f3ed383 44void tcf_chain_put_by_act(struct tcf_chain *chain);
bbf73830
VB
45struct tcf_chain *tcf_get_next_chain(struct tcf_block *block,
46 struct tcf_chain *chain);
fe2923af 47struct tcf_proto *tcf_get_next_proto(struct tcf_chain *chain,
12db03b6 48 struct tcf_proto *tp, bool rtnl_held);
f36fe1c4 49void tcf_block_netif_keep_dst(struct tcf_block *block);
6529eaba 50int tcf_block_get(struct tcf_block **p_block,
8d1a77f9
AA
51 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
52 struct netlink_ext_ack *extack);
c7eb7d72 53int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
8d1a77f9
AA
54 struct tcf_block_ext_info *ei,
55 struct netlink_ext_ack *extack);
6529eaba 56void tcf_block_put(struct tcf_block *block);
c7eb7d72 57void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
8c4083b3 58 struct tcf_block_ext_info *ei);
44186460 59
48617387
JP
60static inline bool tcf_block_shared(struct tcf_block *block)
61{
62 return block->index;
63}
64
44186460
JP
65static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
66{
48617387 67 WARN_ON(tcf_block_shared(block));
44186460
JP
68 return block->q;
69}
70
acb67442
JP
71void *tcf_block_cb_priv(struct tcf_block_cb *block_cb);
72struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
73 tc_setup_cb_t *cb, void *cb_ident);
74void tcf_block_cb_incref(struct tcf_block_cb *block_cb);
75unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb);
76struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
77 tc_setup_cb_t *cb, void *cb_ident,
60513bd8
JH
78 void *cb_priv,
79 struct netlink_ext_ack *extack);
acb67442
JP
80int tcf_block_cb_register(struct tcf_block *block,
81 tc_setup_cb_t *cb, void *cb_ident,
60513bd8 82 void *cb_priv, struct netlink_ext_ack *extack);
32636742
JH
83void __tcf_block_cb_unregister(struct tcf_block *block,
84 struct tcf_block_cb *block_cb);
acb67442
JP
85void tcf_block_cb_unregister(struct tcf_block *block,
86 tc_setup_cb_t *cb, void *cb_ident);
7f76fa36
JH
87int __tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
88 tc_indr_block_bind_cb_t *cb, void *cb_ident);
89int tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
90 tc_indr_block_bind_cb_t *cb, void *cb_ident);
91void __tc_indr_block_cb_unregister(struct net_device *dev,
92 tc_indr_block_bind_cb_t *cb, void *cb_ident);
93void tc_indr_block_cb_unregister(struct net_device *dev,
94 tc_indr_block_bind_cb_t *cb, void *cb_ident);
acb67442 95
87d83093
JP
96int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
97 struct tcf_result *res, bool compat_mode);
98
8ae70032 99#else
88c44a52
PJV
100static inline bool tcf_block_shared(struct tcf_block *block)
101{
102 return false;
103}
104
6529eaba
JP
105static inline
106int tcf_block_get(struct tcf_block **p_block,
3c149091
SM
107 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
108 struct netlink_ext_ack *extack)
6529eaba
JP
109{
110 return 0;
111}
112
8c4083b3 113static inline
c7eb7d72 114int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
33c30a8b
QM
115 struct tcf_block_ext_info *ei,
116 struct netlink_ext_ack *extack)
8c4083b3
JP
117{
118 return 0;
119}
120
6529eaba 121static inline void tcf_block_put(struct tcf_block *block)
8ae70032
JP
122{
123}
87d83093 124
8c4083b3 125static inline
c7eb7d72 126void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
8c4083b3
JP
127 struct tcf_block_ext_info *ei)
128{
129}
130
44186460
JP
131static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
132{
133 return NULL;
134}
135
acb67442
JP
136static inline
137int tc_setup_cb_block_register(struct tcf_block *block, tc_setup_cb_t *cb,
138 void *cb_priv)
139{
140 return 0;
141}
142
143static inline
144void tc_setup_cb_block_unregister(struct tcf_block *block, tc_setup_cb_t *cb,
145 void *cb_priv)
146{
147}
148
149static inline
150void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
151{
152 return NULL;
153}
154
155static inline
156struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
157 tc_setup_cb_t *cb, void *cb_ident)
158{
159 return NULL;
160}
161
162static inline
163void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
164{
165}
166
167static inline
168unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
169{
170 return 0;
171}
172
173static inline
174struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
175 tc_setup_cb_t *cb, void *cb_ident,
60513bd8
JH
176 void *cb_priv,
177 struct netlink_ext_ack *extack)
acb67442
JP
178{
179 return NULL;
180}
181
182static inline
183int tcf_block_cb_register(struct tcf_block *block,
184 tc_setup_cb_t *cb, void *cb_ident,
60513bd8 185 void *cb_priv, struct netlink_ext_ack *extack)
acb67442
JP
186{
187 return 0;
188}
189
190static inline
32636742
JH
191void __tcf_block_cb_unregister(struct tcf_block *block,
192 struct tcf_block_cb *block_cb)
acb67442
JP
193{
194}
195
196static inline
197void tcf_block_cb_unregister(struct tcf_block *block,
198 tc_setup_cb_t *cb, void *cb_ident)
199{
200}
201
7f76fa36
JH
202static inline
203int __tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
204 tc_indr_block_bind_cb_t *cb, void *cb_ident)
205{
206 return 0;
207}
208
209static inline
210int tc_indr_block_cb_register(struct net_device *dev, void *cb_priv,
211 tc_indr_block_bind_cb_t *cb, void *cb_ident)
212{
213 return 0;
214}
215
216static inline
217void __tc_indr_block_cb_unregister(struct net_device *dev,
218 tc_indr_block_bind_cb_t *cb, void *cb_ident)
219{
220}
221
222static inline
223void tc_indr_block_cb_unregister(struct net_device *dev,
224 tc_indr_block_bind_cb_t *cb, void *cb_ident)
225{
226}
227
87d83093
JP
228static inline int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
229 struct tcf_result *res, bool compat_mode)
230{
231 return TC_ACT_UNSPEC;
232}
8ae70032 233#endif
cf1facda 234
1da177e4
LT
235static inline unsigned long
236__cls_set_class(unsigned long *clp, unsigned long cl)
237{
a0efb80c 238 return xchg(clp, cl);
1da177e4
LT
239}
240
241static inline unsigned long
34e3759c 242cls_set_class(struct Qdisc *q, unsigned long *clp, unsigned long cl)
1da177e4
LT
243{
244 unsigned long old_cl;
34e3759c
JP
245
246 sch_tree_lock(q);
1da177e4 247 old_cl = __cls_set_class(clp, cl);
34e3759c 248 sch_tree_unlock(q);
1da177e4
LT
249 return old_cl;
250}
251
252static inline void
253tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
254{
34e3759c 255 struct Qdisc *q = tp->chain->block->q;
1da177e4
LT
256 unsigned long cl;
257
34e3759c
JP
258 /* Check q as it is not set for shared blocks. In that case,
259 * setting class is not supported.
260 */
261 if (!q)
262 return;
263 cl = q->ops->cl_ops->bind_tcf(q, base, r->classid);
264 cl = cls_set_class(q, &r->class, cl);
1da177e4 265 if (cl)
34e3759c 266 q->ops->cl_ops->unbind_tcf(q, cl);
1da177e4
LT
267}
268
269static inline void
270tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
271{
34e3759c 272 struct Qdisc *q = tp->chain->block->q;
1da177e4
LT
273 unsigned long cl;
274
34e3759c
JP
275 if (!q)
276 return;
1da177e4 277 if ((cl = __cls_set_class(&r->class, 0)) != 0)
34e3759c 278 q->ops->cl_ops->unbind_tcf(q, cl);
1da177e4
LT
279}
280
fd2c3ef7 281struct tcf_exts {
1da177e4 282#ifdef CONFIG_NET_CLS_ACT
33be6271 283 __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
22dc13c8
WC
284 int nr_actions;
285 struct tc_action **actions;
e4b95c41 286 struct net *net;
1da177e4 287#endif
5da57f42
WC
288 /* Map to export classifier specific extension TLV types to the
289 * generic extensions API. Unsupported extensions must be set to 0.
290 */
1da177e4
LT
291 int action;
292 int police;
293};
294
14215108
CW
295static inline int tcf_exts_init(struct tcf_exts *exts, struct net *net,
296 int action, int police)
33be6271
WC
297{
298#ifdef CONFIG_NET_CLS_ACT
5da57f42 299 exts->type = 0;
22dc13c8 300 exts->nr_actions = 0;
14215108 301 exts->net = net;
22dc13c8
WC
302 exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
303 GFP_KERNEL);
b9a24bb7
WC
304 if (!exts->actions)
305 return -ENOMEM;
33be6271 306#endif
5da57f42
WC
307 exts->action = action;
308 exts->police = police;
b9a24bb7 309 return 0;
33be6271
WC
310}
311
e4b95c41
CW
312/* Return false if the netns is being destroyed in cleanup_net(). Callers
313 * need to do cleanup synchronously in this case, otherwise may race with
314 * tc_action_net_exit(). Return true for other cases.
315 */
316static inline bool tcf_exts_get_net(struct tcf_exts *exts)
317{
318#ifdef CONFIG_NET_CLS_ACT
319 exts->net = maybe_get_net(exts->net);
320 return exts->net != NULL;
321#else
322 return true;
323#endif
324}
325
326static inline void tcf_exts_put_net(struct tcf_exts *exts)
327{
328#ifdef CONFIG_NET_CLS_ACT
329 if (exts->net)
330 put_net(exts->net);
331#endif
332}
333
22dc13c8 334#ifdef CONFIG_NET_CLS_ACT
244cd96a
CW
335#define tcf_exts_for_each_action(i, a, exts) \
336 for (i = 0; i < TCA_ACT_MAX_PRIO && ((a) = (exts)->actions[i]); i++)
337#else
338#define tcf_exts_for_each_action(i, a, exts) \
191672ca 339 for (; 0; (void)(i), (void)(a), (void)(exts))
22dc13c8 340#endif
22dc13c8 341
d897a638
JK
342static inline void
343tcf_exts_stats_update(const struct tcf_exts *exts,
344 u64 bytes, u64 packets, u64 lastuse)
345{
346#ifdef CONFIG_NET_CLS_ACT
347 int i;
348
349 preempt_disable();
350
351 for (i = 0; i < exts->nr_actions; i++) {
352 struct tc_action *a = exts->actions[i];
353
28169aba 354 tcf_action_stats_update(a, bytes, packets, lastuse, true);
d897a638
JK
355 }
356
357 preempt_enable();
358#endif
359}
360
3bcc0cec
JP
361/**
362 * tcf_exts_has_actions - check if at least one action is present
363 * @exts: tc filter extensions handle
364 *
365 * Returns true if at least one action is present.
366 */
367static inline bool tcf_exts_has_actions(struct tcf_exts *exts)
368{
2734437e 369#ifdef CONFIG_NET_CLS_ACT
3bcc0cec
JP
370 return exts->nr_actions;
371#else
372 return false;
373#endif
374}
2734437e 375
af69afc5
JP
376/**
377 * tcf_exts_exec - execute tc filter extensions
378 * @skb: socket buffer
379 * @exts: tc filter extensions handle
380 * @res: desired result
381 *
af089e70 382 * Executes all configured extensions. Returns TC_ACT_OK on a normal execution,
af69afc5
JP
383 * a negative number if the filter must be considered unmatched or
384 * a positive action code (TC_ACT_*) which must be returned to the
385 * underlying layer.
386 */
387static inline int
388tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
389 struct tcf_result *res)
390{
391#ifdef CONFIG_NET_CLS_ACT
ec1a9cca 392 return tcf_action_exec(skb, exts->actions, exts->nr_actions, res);
af69afc5 393#endif
af089e70 394 return TC_ACT_OK;
af69afc5
JP
395}
396
5c15257f
JP
397int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
398 struct nlattr **tb, struct nlattr *rate_tlv,
ec6743a1 399 struct tcf_exts *exts, bool ovr, bool rtnl_held,
50a56190 400 struct netlink_ext_ack *extack);
18d0264f 401void tcf_exts_destroy(struct tcf_exts *exts);
9b0d4446 402void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src);
5da57f42
WC
403int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
404int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
1da177e4
LT
405
406/**
407 * struct tcf_pkt_info - packet information
408 */
fd2c3ef7 409struct tcf_pkt_info {
1da177e4
LT
410 unsigned char * ptr;
411 int nexthdr;
412};
413
414#ifdef CONFIG_NET_EMATCH
415
416struct tcf_ematch_ops;
417
418/**
419 * struct tcf_ematch - extended match (ematch)
420 *
421 * @matchid: identifier to allow userspace to reidentify a match
422 * @flags: flags specifying attributes and the relation to other matches
423 * @ops: the operations lookup table of the corresponding ematch module
424 * @datalen: length of the ematch specific configuration data
425 * @data: ematch specific data
426 */
fd2c3ef7 427struct tcf_ematch {
1da177e4
LT
428 struct tcf_ematch_ops * ops;
429 unsigned long data;
430 unsigned int datalen;
431 u16 matchid;
432 u16 flags;
82a470f1 433 struct net *net;
1da177e4
LT
434};
435
436static inline int tcf_em_is_container(struct tcf_ematch *em)
437{
438 return !em->ops;
439}
440
441static inline int tcf_em_is_simple(struct tcf_ematch *em)
442{
443 return em->flags & TCF_EM_SIMPLE;
444}
445
446static inline int tcf_em_is_inverted(struct tcf_ematch *em)
447{
448 return em->flags & TCF_EM_INVERT;
449}
450
451static inline int tcf_em_last_match(struct tcf_ematch *em)
452{
453 return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
454}
455
456static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
457{
458 if (tcf_em_last_match(em))
459 return 1;
460
461 if (result == 0 && em->flags & TCF_EM_REL_AND)
462 return 1;
463
464 if (result != 0 && em->flags & TCF_EM_REL_OR)
465 return 1;
466
467 return 0;
468}
469
470/**
471 * struct tcf_ematch_tree - ematch tree handle
472 *
473 * @hdr: ematch tree header supplied by userspace
474 * @matches: array of ematches
475 */
fd2c3ef7 476struct tcf_ematch_tree {
1da177e4
LT
477 struct tcf_ematch_tree_hdr hdr;
478 struct tcf_ematch * matches;
479
480};
481
482/**
483 * struct tcf_ematch_ops - ematch module operations
484 *
485 * @kind: identifier (kind) of this ematch module
486 * @datalen: length of expected configuration data (optional)
487 * @change: called during validation (optional)
488 * @match: called during ematch tree evaluation, must return 1/0
489 * @destroy: called during destroyage (optional)
490 * @dump: called during dumping process (optional)
491 * @owner: owner, must be set to THIS_MODULE
492 * @link: link to previous/next ematch module (internal use)
493 */
fd2c3ef7 494struct tcf_ematch_ops {
1da177e4
LT
495 int kind;
496 int datalen;
82a470f1 497 int (*change)(struct net *net, void *,
1da177e4
LT
498 int, struct tcf_ematch *);
499 int (*match)(struct sk_buff *, struct tcf_ematch *,
500 struct tcf_pkt_info *);
82a470f1 501 void (*destroy)(struct tcf_ematch *);
1da177e4
LT
502 int (*dump)(struct sk_buff *, struct tcf_ematch *);
503 struct module *owner;
504 struct list_head link;
505};
506
5c15257f
JP
507int tcf_em_register(struct tcf_ematch_ops *);
508void tcf_em_unregister(struct tcf_ematch_ops *);
509int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
510 struct tcf_ematch_tree *);
82a470f1 511void tcf_em_tree_destroy(struct tcf_ematch_tree *);
5c15257f
JP
512int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
513int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
514 struct tcf_pkt_info *);
1da177e4 515
1da177e4
LT
516/**
517 * tcf_em_tree_match - evaulate an ematch tree
518 *
519 * @skb: socket buffer of the packet in question
520 * @tree: ematch tree to be used for evaluation
521 * @info: packet information examined by classifier
522 *
523 * This function matches @skb against the ematch tree in @tree by going
524 * through all ematches respecting their logic relations returning
525 * as soon as the result is obvious.
526 *
527 * Returns 1 if the ematch tree as-one matches, no ematches are configured
528 * or ematch is not enabled in the kernel, otherwise 0 is returned.
529 */
530static inline int tcf_em_tree_match(struct sk_buff *skb,
531 struct tcf_ematch_tree *tree,
532 struct tcf_pkt_info *info)
533{
534 if (tree->hdr.nmatches)
535 return __tcf_em_tree_match(skb, tree, info);
536 else
537 return 1;
538}
539
db3d99c0
PM
540#define MODULE_ALIAS_TCF_EMATCH(kind) MODULE_ALIAS("ematch-kind-" __stringify(kind))
541
1da177e4
LT
542#else /* CONFIG_NET_EMATCH */
543
fd2c3ef7 544struct tcf_ematch_tree {
1da177e4
LT
545};
546
547#define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
82a470f1 548#define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
1da177e4 549#define tcf_em_tree_dump(skb, t, tlv) (0)
1da177e4
LT
550#define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
551
552#endif /* CONFIG_NET_EMATCH */
553
554static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
555{
556 switch (layer) {
557 case TCF_LAYER_LINK:
d3303a65 558 return skb_mac_header(skb);
1da177e4 559 case TCF_LAYER_NETWORK:
d56f90a7 560 return skb_network_header(skb);
1da177e4 561 case TCF_LAYER_TRANSPORT:
9c70220b 562 return skb_transport_header(skb);
1da177e4
LT
563 }
564
565 return NULL;
566}
567
eddc9ec5
ACM
568static inline int tcf_valid_offset(const struct sk_buff *skb,
569 const unsigned char *ptr, const int len)
1da177e4 570{
da521b2c
DM
571 return likely((ptr + len) <= skb_tail_pointer(skb) &&
572 ptr >= skb->head &&
573 (ptr <= (ptr + len)));
1da177e4
LT
574}
575
1da177e4 576static inline int
1057c55f
AA
577tcf_change_indev(struct net *net, struct nlattr *indev_tlv,
578 struct netlink_ext_ack *extack)
1da177e4 579{
2519a602
WC
580 char indev[IFNAMSIZ];
581 struct net_device *dev;
582
1057c55f
AA
583 if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ) {
584 NL_SET_ERR_MSG(extack, "Interface name too long");
1da177e4 585 return -EINVAL;
1057c55f 586 }
2519a602
WC
587 dev = __dev_get_by_name(net, indev);
588 if (!dev)
589 return -ENODEV;
590 return dev->ifindex;
1da177e4
LT
591}
592
2519a602
WC
593static inline bool
594tcf_match_indev(struct sk_buff *skb, int ifindex)
1da177e4 595{
2519a602
WC
596 if (!ifindex)
597 return true;
598 if (!skb->skb_iif)
599 return false;
600 return ifindex == skb->skb_iif;
1da177e4 601}
1da177e4 602
3a7b6861
PNA
603int tc_setup_flow_action(struct flow_action *flow_action,
604 const struct tcf_exts *exts);
aeb3fecd
CW
605int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
606 void *type_data, bool err_stop);
e3ab786b 607unsigned int tcf_exts_num_actions(struct tcf_exts *exts);
717503b9 608
5fd9fc4e 609struct tc_cls_common_offload {
5fd9fc4e
JP
610 u32 chain_index;
611 __be16 protocol;
d7c1c8d2 612 u32 prio;
8f0b425a 613 struct netlink_ext_ack *extack;
5fd9fc4e
JP
614};
615
a1b7c5fd
JF
616struct tc_cls_u32_knode {
617 struct tcf_exts *exts;
068ceb35 618 struct tcf_result *res;
e014860e 619 struct tc_u32_sel *sel;
a1b7c5fd
JF
620 u32 handle;
621 u32 val;
622 u32 mask;
623 u32 link_handle;
e014860e 624 u8 fshift;
a1b7c5fd
JF
625};
626
627struct tc_cls_u32_hnode {
628 u32 handle;
629 u32 prio;
630 unsigned int divisor;
631};
632
633enum tc_clsu32_command {
634 TC_CLSU32_NEW_KNODE,
635 TC_CLSU32_REPLACE_KNODE,
636 TC_CLSU32_DELETE_KNODE,
637 TC_CLSU32_NEW_HNODE,
638 TC_CLSU32_REPLACE_HNODE,
639 TC_CLSU32_DELETE_HNODE,
640};
641
642struct tc_cls_u32_offload {
5fd9fc4e 643 struct tc_cls_common_offload common;
a1b7c5fd
JF
644 /* knode values */
645 enum tc_clsu32_command command;
646 union {
647 struct tc_cls_u32_knode knode;
648 struct tc_cls_u32_hnode hnode;
649 };
650};
651
7b06e8ae 652static inline bool tc_can_offload(const struct net_device *dev)
6843e7a2 653{
70b5aee4 654 return dev->features & NETIF_F_HW_TC;
6843e7a2
JF
655}
656
f9eda14f
QM
657static inline bool tc_can_offload_extack(const struct net_device *dev,
658 struct netlink_ext_ack *extack)
659{
660 bool can = tc_can_offload(dev);
661
662 if (!can)
663 NL_SET_ERR_MSG(extack, "TC offload is disabled on net device");
664
665 return can;
666}
667
878db9f0
JK
668static inline bool
669tc_cls_can_offload_and_chain0(const struct net_device *dev,
670 struct tc_cls_common_offload *common)
671{
672 if (!tc_can_offload_extack(dev, common->extack))
673 return false;
674 if (common->chain_index) {
675 NL_SET_ERR_MSG(common->extack,
676 "Driver supports only offload of chain 0");
677 return false;
678 }
679 return true;
680}
681
55330f05
HHZ
682static inline bool tc_skip_hw(u32 flags)
683{
684 return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
685}
686
d34e3e18
SS
687static inline bool tc_skip_sw(u32 flags)
688{
689 return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
690}
691
692/* SKIP_HW and SKIP_SW are mutually exclusive flags. */
693static inline bool tc_flags_valid(u32 flags)
694{
81c7288b
MRL
695 if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW |
696 TCA_CLS_FLAGS_VERBOSE))
d34e3e18
SS
697 return false;
698
81c7288b 699 flags &= TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW;
d34e3e18
SS
700 if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
701 return false;
702
703 return true;
704}
705
e696028a
OG
706static inline bool tc_in_hw(u32 flags)
707{
708 return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
709}
710
34832e1c
JK
711static inline void
712tc_cls_common_offload_init(struct tc_cls_common_offload *cls_common,
713 const struct tcf_proto *tp, u32 flags,
714 struct netlink_ext_ack *extack)
715{
716 cls_common->chain_index = tp->chain->index;
717 cls_common->protocol = tp->protocol;
718 cls_common->prio = tp->prio;
81c7288b 719 if (tc_skip_sw(flags) || flags & TCA_CLS_FLAGS_VERBOSE)
34832e1c
JK
720 cls_common->extack = extack;
721}
722
5b33f488
AV
723enum tc_fl_command {
724 TC_CLSFLOWER_REPLACE,
725 TC_CLSFLOWER_DESTROY,
10cbc684 726 TC_CLSFLOWER_STATS,
34738452
JP
727 TC_CLSFLOWER_TMPLT_CREATE,
728 TC_CLSFLOWER_TMPLT_DESTROY,
5b33f488
AV
729};
730
731struct tc_cls_flower_offload {
5fd9fc4e 732 struct tc_cls_common_offload common;
5b33f488 733 enum tc_fl_command command;
8208d21b 734 unsigned long cookie;
8f256622 735 struct flow_rule *rule;
3b1903ef 736 struct flow_stats stats;
384c181e 737 u32 classid;
5b33f488
AV
738};
739
8f256622
PNA
740static inline struct flow_rule *
741tc_cls_flower_offload_flow_rule(struct tc_cls_flower_offload *tc_flow_cmd)
742{
743 return tc_flow_cmd->rule;
744}
745
b87f7936
YG
746enum tc_matchall_command {
747 TC_CLSMATCHALL_REPLACE,
748 TC_CLSMATCHALL_DESTROY,
b7fe4ab8 749 TC_CLSMATCHALL_STATS,
b87f7936
YG
750};
751
752struct tc_cls_matchall_offload {
5fd9fc4e 753 struct tc_cls_common_offload common;
b87f7936 754 enum tc_matchall_command command;
f00cbf19 755 struct flow_rule *rule;
b7fe4ab8 756 struct flow_stats stats;
b87f7936
YG
757 unsigned long cookie;
758};
759
332ae8e2 760enum tc_clsbpf_command {
102740bd 761 TC_CLSBPF_OFFLOAD,
68d64063 762 TC_CLSBPF_STATS,
332ae8e2
JK
763};
764
765struct tc_cls_bpf_offload {
5fd9fc4e 766 struct tc_cls_common_offload common;
332ae8e2
JK
767 enum tc_clsbpf_command command;
768 struct tcf_exts *exts;
769 struct bpf_prog *prog;
102740bd 770 struct bpf_prog *oldprog;
332ae8e2
JK
771 const char *name;
772 bool exts_integrated;
773};
774
4e8b86c0
AN
775struct tc_mqprio_qopt_offload {
776 /* struct tc_mqprio_qopt must always be the first element */
777 struct tc_mqprio_qopt qopt;
778 u16 mode;
779 u16 shaper;
780 u32 flags;
781 u64 min_rate[TC_QOPT_MAX_QUEUE];
782 u64 max_rate[TC_QOPT_MAX_QUEUE];
783};
1045ba77
JHS
784
785/* This structure holds cookie structure that is passed from user
786 * to the kernel for actions and classifiers
787 */
788struct tc_cookie {
789 u8 *data;
790 u32 len;
eec94fdb 791 struct rcu_head rcu;
1045ba77 792};
602f3baf 793
f34b4aac
NF
794struct tc_qopt_offload_stats {
795 struct gnet_stats_basic_packed *bstats;
796 struct gnet_stats_queue *qstats;
797};
798
f971b132
JK
799enum tc_mq_command {
800 TC_MQ_CREATE,
801 TC_MQ_DESTROY,
47c669a4 802 TC_MQ_STATS,
d577a3d2
JK
803 TC_MQ_GRAFT,
804};
805
806struct tc_mq_opt_offload_graft_params {
807 unsigned long queue;
808 u32 child_handle;
f971b132
JK
809};
810
811struct tc_mq_qopt_offload {
812 enum tc_mq_command command;
813 u32 handle;
d577a3d2
JK
814 union {
815 struct tc_qopt_offload_stats stats;
816 struct tc_mq_opt_offload_graft_params graft_params;
817 };
f971b132
JK
818};
819
602f3baf
NF
820enum tc_red_command {
821 TC_RED_REPLACE,
822 TC_RED_DESTROY,
823 TC_RED_STATS,
824 TC_RED_XSTATS,
bf2a752b 825 TC_RED_GRAFT,
602f3baf
NF
826};
827
828struct tc_red_qopt_offload_params {
829 u32 min;
830 u32 max;
831 u32 probability;
c0b7490b 832 u32 limit;
602f3baf 833 bool is_ecn;
190852a5 834 bool is_harddrop;
416ef9b1 835 struct gnet_stats_queue *qstats;
602f3baf 836};
602f3baf
NF
837
838struct tc_red_qopt_offload {
839 enum tc_red_command command;
840 u32 handle;
841 u32 parent;
842 union {
843 struct tc_red_qopt_offload_params set;
f34b4aac 844 struct tc_qopt_offload_stats stats;
602f3baf 845 struct red_stats *xstats;
bf2a752b 846 u32 child_handle;
602f3baf
NF
847 };
848};
849
890d8d23
JK
850enum tc_gred_command {
851 TC_GRED_REPLACE,
852 TC_GRED_DESTROY,
e49efd52 853 TC_GRED_STATS,
890d8d23
JK
854};
855
856struct tc_gred_vq_qopt_offload_params {
857 bool present;
858 u32 limit;
859 u32 prio;
860 u32 min;
861 u32 max;
862 bool is_ecn;
863 bool is_harddrop;
864 u32 probability;
865 /* Only need backlog, see struct tc_prio_qopt_offload_params */
866 u32 *backlog;
867};
868
869struct tc_gred_qopt_offload_params {
870 bool grio_on;
871 bool wred_on;
872 unsigned int dp_cnt;
873 unsigned int dp_def;
874 struct gnet_stats_queue *qstats;
875 struct tc_gred_vq_qopt_offload_params tab[MAX_DPs];
876};
877
e49efd52
JK
878struct tc_gred_qopt_offload_stats {
879 struct gnet_stats_basic_packed bstats[MAX_DPs];
880 struct gnet_stats_queue qstats[MAX_DPs];
881 struct red_stats *xstats[MAX_DPs];
882};
883
890d8d23
JK
884struct tc_gred_qopt_offload {
885 enum tc_gred_command command;
886 u32 handle;
887 u32 parent;
888 union {
889 struct tc_gred_qopt_offload_params set;
e49efd52 890 struct tc_gred_qopt_offload_stats stats;
890d8d23
JK
891 };
892};
893
7fdb61b4
NF
894enum tc_prio_command {
895 TC_PRIO_REPLACE,
896 TC_PRIO_DESTROY,
897 TC_PRIO_STATS,
b9c7a7ac 898 TC_PRIO_GRAFT,
7fdb61b4
NF
899};
900
901struct tc_prio_qopt_offload_params {
902 int bands;
903 u8 priomap[TC_PRIO_MAX + 1];
904 /* In case that a prio qdisc is offloaded and now is changed to a
905 * non-offloadedable config, it needs to update the backlog & qlen
906 * values to negate the HW backlog & qlen values (and only them).
907 */
908 struct gnet_stats_queue *qstats;
909};
910
b9c7a7ac
NF
911struct tc_prio_qopt_offload_graft_params {
912 u8 band;
913 u32 child_handle;
914};
915
7fdb61b4
NF
916struct tc_prio_qopt_offload {
917 enum tc_prio_command command;
918 u32 handle;
919 u32 parent;
920 union {
921 struct tc_prio_qopt_offload_params replace_params;
922 struct tc_qopt_offload_stats stats;
b9c7a7ac 923 struct tc_prio_qopt_offload_graft_params graft_params;
7fdb61b4
NF
924 };
925};
b9c7a7ac 926
98b0e5f6
JK
927enum tc_root_command {
928 TC_ROOT_GRAFT,
929};
930
931struct tc_root_qopt_offload {
932 enum tc_root_command command;
933 u32 handle;
934 bool ingress;
935};
936
1da177e4 937#endif