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