Merge tag 'hyperv-fixes-signed-20221125' of git://git.kernel.org/pub/scm/linux/kernel...
[platform/kernel/linux-starfive.git] / include / net / flow_offload.h
1 #ifndef _NET_FLOW_OFFLOAD_H
2 #define _NET_FLOW_OFFLOAD_H
3
4 #include <linux/kernel.h>
5 #include <linux/list.h>
6 #include <linux/netlink.h>
7 #include <net/flow_dissector.h>
8
9 struct flow_match {
10         struct flow_dissector   *dissector;
11         void                    *mask;
12         void                    *key;
13 };
14
15 struct flow_match_meta {
16         struct flow_dissector_key_meta *key, *mask;
17 };
18
19 struct flow_match_basic {
20         struct flow_dissector_key_basic *key, *mask;
21 };
22
23 struct flow_match_control {
24         struct flow_dissector_key_control *key, *mask;
25 };
26
27 struct flow_match_eth_addrs {
28         struct flow_dissector_key_eth_addrs *key, *mask;
29 };
30
31 struct flow_match_vlan {
32         struct flow_dissector_key_vlan *key, *mask;
33 };
34
35 struct flow_match_ipv4_addrs {
36         struct flow_dissector_key_ipv4_addrs *key, *mask;
37 };
38
39 struct flow_match_ipv6_addrs {
40         struct flow_dissector_key_ipv6_addrs *key, *mask;
41 };
42
43 struct flow_match_ip {
44         struct flow_dissector_key_ip *key, *mask;
45 };
46
47 struct flow_match_ports {
48         struct flow_dissector_key_ports *key, *mask;
49 };
50
51 struct flow_match_ports_range {
52         struct flow_dissector_key_ports_range *key, *mask;
53 };
54
55 struct flow_match_icmp {
56         struct flow_dissector_key_icmp *key, *mask;
57 };
58
59 struct flow_match_tcp {
60         struct flow_dissector_key_tcp *key, *mask;
61 };
62
63 struct flow_match_mpls {
64         struct flow_dissector_key_mpls *key, *mask;
65 };
66
67 struct flow_match_enc_keyid {
68         struct flow_dissector_key_keyid *key, *mask;
69 };
70
71 struct flow_match_enc_opts {
72         struct flow_dissector_key_enc_opts *key, *mask;
73 };
74
75 struct flow_match_ct {
76         struct flow_dissector_key_ct *key, *mask;
77 };
78
79 struct flow_match_pppoe {
80         struct flow_dissector_key_pppoe *key, *mask;
81 };
82
83 struct flow_match_l2tpv3 {
84         struct flow_dissector_key_l2tpv3 *key, *mask;
85 };
86
87 struct flow_rule;
88
89 void flow_rule_match_meta(const struct flow_rule *rule,
90                           struct flow_match_meta *out);
91 void flow_rule_match_basic(const struct flow_rule *rule,
92                            struct flow_match_basic *out);
93 void flow_rule_match_control(const struct flow_rule *rule,
94                              struct flow_match_control *out);
95 void flow_rule_match_eth_addrs(const struct flow_rule *rule,
96                                struct flow_match_eth_addrs *out);
97 void flow_rule_match_vlan(const struct flow_rule *rule,
98                           struct flow_match_vlan *out);
99 void flow_rule_match_cvlan(const struct flow_rule *rule,
100                            struct flow_match_vlan *out);
101 void flow_rule_match_ipv4_addrs(const struct flow_rule *rule,
102                                 struct flow_match_ipv4_addrs *out);
103 void flow_rule_match_ipv6_addrs(const struct flow_rule *rule,
104                                 struct flow_match_ipv6_addrs *out);
105 void flow_rule_match_ip(const struct flow_rule *rule,
106                         struct flow_match_ip *out);
107 void flow_rule_match_ports(const struct flow_rule *rule,
108                            struct flow_match_ports *out);
109 void flow_rule_match_ports_range(const struct flow_rule *rule,
110                                  struct flow_match_ports_range *out);
111 void flow_rule_match_tcp(const struct flow_rule *rule,
112                          struct flow_match_tcp *out);
113 void flow_rule_match_icmp(const struct flow_rule *rule,
114                           struct flow_match_icmp *out);
115 void flow_rule_match_mpls(const struct flow_rule *rule,
116                           struct flow_match_mpls *out);
117 void flow_rule_match_enc_control(const struct flow_rule *rule,
118                                  struct flow_match_control *out);
119 void flow_rule_match_enc_ipv4_addrs(const struct flow_rule *rule,
120                                     struct flow_match_ipv4_addrs *out);
121 void flow_rule_match_enc_ipv6_addrs(const struct flow_rule *rule,
122                                     struct flow_match_ipv6_addrs *out);
123 void flow_rule_match_enc_ip(const struct flow_rule *rule,
124                             struct flow_match_ip *out);
125 void flow_rule_match_enc_ports(const struct flow_rule *rule,
126                                struct flow_match_ports *out);
127 void flow_rule_match_enc_keyid(const struct flow_rule *rule,
128                                struct flow_match_enc_keyid *out);
129 void flow_rule_match_enc_opts(const struct flow_rule *rule,
130                               struct flow_match_enc_opts *out);
131 void flow_rule_match_ct(const struct flow_rule *rule,
132                         struct flow_match_ct *out);
133 void flow_rule_match_pppoe(const struct flow_rule *rule,
134                            struct flow_match_pppoe *out);
135 void flow_rule_match_l2tpv3(const struct flow_rule *rule,
136                             struct flow_match_l2tpv3 *out);
137
138 enum flow_action_id {
139         FLOW_ACTION_ACCEPT              = 0,
140         FLOW_ACTION_DROP,
141         FLOW_ACTION_TRAP,
142         FLOW_ACTION_GOTO,
143         FLOW_ACTION_REDIRECT,
144         FLOW_ACTION_MIRRED,
145         FLOW_ACTION_REDIRECT_INGRESS,
146         FLOW_ACTION_MIRRED_INGRESS,
147         FLOW_ACTION_VLAN_PUSH,
148         FLOW_ACTION_VLAN_POP,
149         FLOW_ACTION_VLAN_MANGLE,
150         FLOW_ACTION_TUNNEL_ENCAP,
151         FLOW_ACTION_TUNNEL_DECAP,
152         FLOW_ACTION_MANGLE,
153         FLOW_ACTION_ADD,
154         FLOW_ACTION_CSUM,
155         FLOW_ACTION_MARK,
156         FLOW_ACTION_PTYPE,
157         FLOW_ACTION_PRIORITY,
158         FLOW_ACTION_WAKE,
159         FLOW_ACTION_QUEUE,
160         FLOW_ACTION_SAMPLE,
161         FLOW_ACTION_POLICE,
162         FLOW_ACTION_CT,
163         FLOW_ACTION_CT_METADATA,
164         FLOW_ACTION_MPLS_PUSH,
165         FLOW_ACTION_MPLS_POP,
166         FLOW_ACTION_MPLS_MANGLE,
167         FLOW_ACTION_GATE,
168         FLOW_ACTION_PPPOE_PUSH,
169         FLOW_ACTION_JUMP,
170         FLOW_ACTION_PIPE,
171         FLOW_ACTION_VLAN_PUSH_ETH,
172         FLOW_ACTION_VLAN_POP_ETH,
173         FLOW_ACTION_CONTINUE,
174         NUM_FLOW_ACTIONS,
175 };
176
177 /* This is mirroring enum pedit_header_type definition for easy mapping between
178  * tc pedit action. Legacy TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK is mapped to
179  * FLOW_ACT_MANGLE_UNSPEC, which is supported by no driver.
180  */
181 enum flow_action_mangle_base {
182         FLOW_ACT_MANGLE_UNSPEC          = 0,
183         FLOW_ACT_MANGLE_HDR_TYPE_ETH,
184         FLOW_ACT_MANGLE_HDR_TYPE_IP4,
185         FLOW_ACT_MANGLE_HDR_TYPE_IP6,
186         FLOW_ACT_MANGLE_HDR_TYPE_TCP,
187         FLOW_ACT_MANGLE_HDR_TYPE_UDP,
188 };
189
190 enum flow_action_hw_stats_bit {
191         FLOW_ACTION_HW_STATS_IMMEDIATE_BIT,
192         FLOW_ACTION_HW_STATS_DELAYED_BIT,
193         FLOW_ACTION_HW_STATS_DISABLED_BIT,
194
195         FLOW_ACTION_HW_STATS_NUM_BITS
196 };
197
198 enum flow_action_hw_stats {
199         FLOW_ACTION_HW_STATS_IMMEDIATE =
200                 BIT(FLOW_ACTION_HW_STATS_IMMEDIATE_BIT),
201         FLOW_ACTION_HW_STATS_DELAYED = BIT(FLOW_ACTION_HW_STATS_DELAYED_BIT),
202         FLOW_ACTION_HW_STATS_ANY = FLOW_ACTION_HW_STATS_IMMEDIATE |
203                                    FLOW_ACTION_HW_STATS_DELAYED,
204         FLOW_ACTION_HW_STATS_DISABLED =
205                 BIT(FLOW_ACTION_HW_STATS_DISABLED_BIT),
206         FLOW_ACTION_HW_STATS_DONT_CARE = BIT(FLOW_ACTION_HW_STATS_NUM_BITS) - 1,
207 };
208
209 typedef void (*action_destr)(void *priv);
210
211 struct flow_action_cookie {
212         u32 cookie_len;
213         u8 cookie[];
214 };
215
216 struct flow_action_cookie *flow_action_cookie_create(void *data,
217                                                      unsigned int len,
218                                                      gfp_t gfp);
219 void flow_action_cookie_destroy(struct flow_action_cookie *cookie);
220
221 struct flow_action_entry {
222         enum flow_action_id             id;
223         u32                             hw_index;
224         enum flow_action_hw_stats       hw_stats;
225         action_destr                    destructor;
226         void                            *destructor_priv;
227         union {
228                 u32                     chain_index;    /* FLOW_ACTION_GOTO */
229                 struct net_device       *dev;           /* FLOW_ACTION_REDIRECT */
230                 struct {                                /* FLOW_ACTION_VLAN */
231                         u16             vid;
232                         __be16          proto;
233                         u8              prio;
234                 } vlan;
235                 struct {                                /* FLOW_ACTION_VLAN_PUSH_ETH */
236                         unsigned char dst[ETH_ALEN];
237                         unsigned char src[ETH_ALEN];
238                 } vlan_push_eth;
239                 struct {                                /* FLOW_ACTION_MANGLE */
240                                                         /* FLOW_ACTION_ADD */
241                         enum flow_action_mangle_base htype;
242                         u32             offset;
243                         u32             mask;
244                         u32             val;
245                 } mangle;
246                 struct ip_tunnel_info   *tunnel;        /* FLOW_ACTION_TUNNEL_ENCAP */
247                 u32                     csum_flags;     /* FLOW_ACTION_CSUM */
248                 u32                     mark;           /* FLOW_ACTION_MARK */
249                 u16                     ptype;          /* FLOW_ACTION_PTYPE */
250                 u32                     priority;       /* FLOW_ACTION_PRIORITY */
251                 struct {                                /* FLOW_ACTION_QUEUE */
252                         u32             ctx;
253                         u32             index;
254                         u8              vf;
255                 } queue;
256                 struct {                                /* FLOW_ACTION_SAMPLE */
257                         struct psample_group    *psample_group;
258                         u32                     rate;
259                         u32                     trunc_size;
260                         bool                    truncate;
261                 } sample;
262                 struct {                                /* FLOW_ACTION_POLICE */
263                         u32                     burst;
264                         u64                     rate_bytes_ps;
265                         u64                     peakrate_bytes_ps;
266                         u32                     avrate;
267                         u16                     overhead;
268                         u64                     burst_pkt;
269                         u64                     rate_pkt_ps;
270                         u32                     mtu;
271                         struct {
272                                 enum flow_action_id     act_id;
273                                 u32                     extval;
274                         } exceed, notexceed;
275                 } police;
276                 struct {                                /* FLOW_ACTION_CT */
277                         int action;
278                         u16 zone;
279                         struct nf_flowtable *flow_table;
280                 } ct;
281                 struct {
282                         unsigned long cookie;
283                         u32 mark;
284                         u32 labels[4];
285                         bool orig_dir;
286                 } ct_metadata;
287                 struct {                                /* FLOW_ACTION_MPLS_PUSH */
288                         u32             label;
289                         __be16          proto;
290                         u8              tc;
291                         u8              bos;
292                         u8              ttl;
293                 } mpls_push;
294                 struct {                                /* FLOW_ACTION_MPLS_POP */
295                         __be16          proto;
296                 } mpls_pop;
297                 struct {                                /* FLOW_ACTION_MPLS_MANGLE */
298                         u32             label;
299                         u8              tc;
300                         u8              bos;
301                         u8              ttl;
302                 } mpls_mangle;
303                 struct {
304                         s32             prio;
305                         u64             basetime;
306                         u64             cycletime;
307                         u64             cycletimeext;
308                         u32             num_entries;
309                         struct action_gate_entry *entries;
310                 } gate;
311                 struct {                                /* FLOW_ACTION_PPPOE_PUSH */
312                         u16             sid;
313                 } pppoe;
314         };
315         struct flow_action_cookie *cookie; /* user defined action cookie */
316 };
317
318 struct flow_action {
319         unsigned int                    num_entries;
320         struct flow_action_entry        entries[];
321 };
322
323 static inline bool flow_action_has_entries(const struct flow_action *action)
324 {
325         return action->num_entries;
326 }
327
328 /**
329  * flow_offload_has_one_action() - check if exactly one action is present
330  * @action: tc filter flow offload action
331  *
332  * Returns true if exactly one action is present.
333  */
334 static inline bool flow_offload_has_one_action(const struct flow_action *action)
335 {
336         return action->num_entries == 1;
337 }
338
339 static inline bool flow_action_is_last_entry(const struct flow_action *action,
340                                              const struct flow_action_entry *entry)
341 {
342         return entry == &action->entries[action->num_entries - 1];
343 }
344
345 #define flow_action_for_each(__i, __act, __actions)                     \
346         for (__i = 0, __act = &(__actions)->entries[0];                 \
347              __i < (__actions)->num_entries;                            \
348              __act = &(__actions)->entries[++__i])
349
350 static inline bool
351 flow_action_mixed_hw_stats_check(const struct flow_action *action,
352                                  struct netlink_ext_ack *extack)
353 {
354         const struct flow_action_entry *action_entry;
355         u8 last_hw_stats;
356         int i;
357
358         if (flow_offload_has_one_action(action))
359                 return true;
360
361         flow_action_for_each(i, action_entry, action) {
362                 if (i && action_entry->hw_stats != last_hw_stats) {
363                         NL_SET_ERR_MSG_MOD(extack, "Mixing HW stats types for actions is not supported");
364                         return false;
365                 }
366                 last_hw_stats = action_entry->hw_stats;
367         }
368         return true;
369 }
370
371 static inline const struct flow_action_entry *
372 flow_action_first_entry_get(const struct flow_action *action)
373 {
374         WARN_ON(!flow_action_has_entries(action));
375         return &action->entries[0];
376 }
377
378 static inline bool
379 __flow_action_hw_stats_check(const struct flow_action *action,
380                              struct netlink_ext_ack *extack,
381                              bool check_allow_bit,
382                              enum flow_action_hw_stats_bit allow_bit)
383 {
384         const struct flow_action_entry *action_entry;
385
386         if (!flow_action_has_entries(action))
387                 return true;
388         if (!flow_action_mixed_hw_stats_check(action, extack))
389                 return false;
390
391         action_entry = flow_action_first_entry_get(action);
392
393         /* Zero is not a legal value for hw_stats, catch anyone passing it */
394         WARN_ON_ONCE(!action_entry->hw_stats);
395
396         if (!check_allow_bit &&
397             ~action_entry->hw_stats & FLOW_ACTION_HW_STATS_ANY) {
398                 NL_SET_ERR_MSG_MOD(extack, "Driver supports only default HW stats type \"any\"");
399                 return false;
400         } else if (check_allow_bit &&
401                    !(action_entry->hw_stats & BIT(allow_bit))) {
402                 NL_SET_ERR_MSG_MOD(extack, "Driver does not support selected HW stats type");
403                 return false;
404         }
405         return true;
406 }
407
408 static inline bool
409 flow_action_hw_stats_check(const struct flow_action *action,
410                            struct netlink_ext_ack *extack,
411                            enum flow_action_hw_stats_bit allow_bit)
412 {
413         return __flow_action_hw_stats_check(action, extack, true, allow_bit);
414 }
415
416 static inline bool
417 flow_action_basic_hw_stats_check(const struct flow_action *action,
418                                  struct netlink_ext_ack *extack)
419 {
420         return __flow_action_hw_stats_check(action, extack, false, 0);
421 }
422
423 struct flow_rule {
424         struct flow_match       match;
425         struct flow_action      action;
426 };
427
428 struct flow_rule *flow_rule_alloc(unsigned int num_actions);
429
430 static inline bool flow_rule_match_key(const struct flow_rule *rule,
431                                        enum flow_dissector_key_id key)
432 {
433         return dissector_uses_key(rule->match.dissector, key);
434 }
435
436 struct flow_stats {
437         u64     pkts;
438         u64     bytes;
439         u64     drops;
440         u64     lastused;
441         enum flow_action_hw_stats used_hw_stats;
442         bool used_hw_stats_valid;
443 };
444
445 static inline void flow_stats_update(struct flow_stats *flow_stats,
446                                      u64 bytes, u64 pkts,
447                                      u64 drops, u64 lastused,
448                                      enum flow_action_hw_stats used_hw_stats)
449 {
450         flow_stats->pkts        += pkts;
451         flow_stats->bytes       += bytes;
452         flow_stats->drops       += drops;
453         flow_stats->lastused    = max_t(u64, flow_stats->lastused, lastused);
454
455         /* The driver should pass value with a maximum of one bit set.
456          * Passing FLOW_ACTION_HW_STATS_ANY is invalid.
457          */
458         WARN_ON(used_hw_stats == FLOW_ACTION_HW_STATS_ANY);
459         flow_stats->used_hw_stats |= used_hw_stats;
460         flow_stats->used_hw_stats_valid = true;
461 }
462
463 enum flow_block_command {
464         FLOW_BLOCK_BIND,
465         FLOW_BLOCK_UNBIND,
466 };
467
468 enum flow_block_binder_type {
469         FLOW_BLOCK_BINDER_TYPE_UNSPEC,
470         FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS,
471         FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS,
472         FLOW_BLOCK_BINDER_TYPE_RED_EARLY_DROP,
473         FLOW_BLOCK_BINDER_TYPE_RED_MARK,
474 };
475
476 struct flow_block {
477         struct list_head cb_list;
478 };
479
480 struct netlink_ext_ack;
481
482 struct flow_block_offload {
483         enum flow_block_command command;
484         enum flow_block_binder_type binder_type;
485         bool block_shared;
486         bool unlocked_driver_cb;
487         struct net *net;
488         struct flow_block *block;
489         struct list_head cb_list;
490         struct list_head *driver_block_list;
491         struct netlink_ext_ack *extack;
492         struct Qdisc *sch;
493         struct list_head *cb_list_head;
494 };
495
496 enum tc_setup_type;
497 typedef int flow_setup_cb_t(enum tc_setup_type type, void *type_data,
498                             void *cb_priv);
499
500 struct flow_block_cb;
501
502 struct flow_block_indr {
503         struct list_head                list;
504         struct net_device               *dev;
505         struct Qdisc                    *sch;
506         enum flow_block_binder_type     binder_type;
507         void                            *data;
508         void                            *cb_priv;
509         void                            (*cleanup)(struct flow_block_cb *block_cb);
510 };
511
512 struct flow_block_cb {
513         struct list_head        driver_list;
514         struct list_head        list;
515         flow_setup_cb_t         *cb;
516         void                    *cb_ident;
517         void                    *cb_priv;
518         void                    (*release)(void *cb_priv);
519         struct flow_block_indr  indr;
520         unsigned int            refcnt;
521 };
522
523 struct flow_block_cb *flow_block_cb_alloc(flow_setup_cb_t *cb,
524                                           void *cb_ident, void *cb_priv,
525                                           void (*release)(void *cb_priv));
526 struct flow_block_cb *flow_indr_block_cb_alloc(flow_setup_cb_t *cb,
527                                                void *cb_ident, void *cb_priv,
528                                                void (*release)(void *cb_priv),
529                                                struct flow_block_offload *bo,
530                                                struct net_device *dev,
531                                                struct Qdisc *sch, void *data,
532                                                void *indr_cb_priv,
533                                                void (*cleanup)(struct flow_block_cb *block_cb));
534 void flow_block_cb_free(struct flow_block_cb *block_cb);
535
536 struct flow_block_cb *flow_block_cb_lookup(struct flow_block *block,
537                                            flow_setup_cb_t *cb, void *cb_ident);
538
539 void *flow_block_cb_priv(struct flow_block_cb *block_cb);
540 void flow_block_cb_incref(struct flow_block_cb *block_cb);
541 unsigned int flow_block_cb_decref(struct flow_block_cb *block_cb);
542
543 static inline void flow_block_cb_add(struct flow_block_cb *block_cb,
544                                      struct flow_block_offload *offload)
545 {
546         list_add_tail(&block_cb->list, &offload->cb_list);
547 }
548
549 static inline void flow_block_cb_remove(struct flow_block_cb *block_cb,
550                                         struct flow_block_offload *offload)
551 {
552         list_move(&block_cb->list, &offload->cb_list);
553 }
554
555 static inline void flow_indr_block_cb_remove(struct flow_block_cb *block_cb,
556                                              struct flow_block_offload *offload)
557 {
558         list_del(&block_cb->indr.list);
559         list_move(&block_cb->list, &offload->cb_list);
560 }
561
562 bool flow_block_cb_is_busy(flow_setup_cb_t *cb, void *cb_ident,
563                            struct list_head *driver_block_list);
564
565 int flow_block_cb_setup_simple(struct flow_block_offload *f,
566                                struct list_head *driver_list,
567                                flow_setup_cb_t *cb,
568                                void *cb_ident, void *cb_priv, bool ingress_only);
569
570 enum flow_cls_command {
571         FLOW_CLS_REPLACE,
572         FLOW_CLS_DESTROY,
573         FLOW_CLS_STATS,
574         FLOW_CLS_TMPLT_CREATE,
575         FLOW_CLS_TMPLT_DESTROY,
576 };
577
578 struct flow_cls_common_offload {
579         u32 chain_index;
580         __be16 protocol;
581         u32 prio;
582         struct netlink_ext_ack *extack;
583 };
584
585 struct flow_cls_offload {
586         struct flow_cls_common_offload common;
587         enum flow_cls_command command;
588         unsigned long cookie;
589         struct flow_rule *rule;
590         struct flow_stats stats;
591         u32 classid;
592 };
593
594 enum offload_act_command  {
595         FLOW_ACT_REPLACE,
596         FLOW_ACT_DESTROY,
597         FLOW_ACT_STATS,
598 };
599
600 struct flow_offload_action {
601         struct netlink_ext_ack *extack; /* NULL in FLOW_ACT_STATS process*/
602         enum offload_act_command  command;
603         enum flow_action_id id;
604         u32 index;
605         struct flow_stats stats;
606         struct flow_action action;
607 };
608
609 struct flow_offload_action *offload_action_alloc(unsigned int num_actions);
610
611 static inline struct flow_rule *
612 flow_cls_offload_flow_rule(struct flow_cls_offload *flow_cmd)
613 {
614         return flow_cmd->rule;
615 }
616
617 static inline void flow_block_init(struct flow_block *flow_block)
618 {
619         INIT_LIST_HEAD(&flow_block->cb_list);
620 }
621
622 typedef int flow_indr_block_bind_cb_t(struct net_device *dev, struct Qdisc *sch, void *cb_priv,
623                                       enum tc_setup_type type, void *type_data,
624                                       void *data,
625                                       void (*cleanup)(struct flow_block_cb *block_cb));
626
627 int flow_indr_dev_register(flow_indr_block_bind_cb_t *cb, void *cb_priv);
628 void flow_indr_dev_unregister(flow_indr_block_bind_cb_t *cb, void *cb_priv,
629                               void (*release)(void *cb_priv));
630 int flow_indr_dev_setup_offload(struct net_device *dev, struct Qdisc *sch,
631                                 enum tc_setup_type type, void *data,
632                                 struct flow_block_offload *bo,
633                                 void (*cleanup)(struct flow_block_cb *block_cb));
634 bool flow_indr_dev_exists(void);
635
636 #endif /* _NET_FLOW_OFFLOAD_H */