1 #ifndef _NET_FLOW_OFFLOAD_H
2 #define _NET_FLOW_OFFLOAD_H
4 #include <linux/kernel.h>
5 #include <linux/list.h>
6 #include <linux/netlink.h>
7 #include <net/flow_dissector.h>
10 struct flow_dissector *dissector;
15 struct flow_match_meta {
16 struct flow_dissector_key_meta *key, *mask;
19 struct flow_match_basic {
20 struct flow_dissector_key_basic *key, *mask;
23 struct flow_match_control {
24 struct flow_dissector_key_control *key, *mask;
27 struct flow_match_eth_addrs {
28 struct flow_dissector_key_eth_addrs *key, *mask;
31 struct flow_match_vlan {
32 struct flow_dissector_key_vlan *key, *mask;
35 struct flow_match_ipv4_addrs {
36 struct flow_dissector_key_ipv4_addrs *key, *mask;
39 struct flow_match_ipv6_addrs {
40 struct flow_dissector_key_ipv6_addrs *key, *mask;
43 struct flow_match_ip {
44 struct flow_dissector_key_ip *key, *mask;
47 struct flow_match_ports {
48 struct flow_dissector_key_ports *key, *mask;
51 struct flow_match_ports_range {
52 struct flow_dissector_key_ports_range *key, *mask;
55 struct flow_match_icmp {
56 struct flow_dissector_key_icmp *key, *mask;
59 struct flow_match_tcp {
60 struct flow_dissector_key_tcp *key, *mask;
63 struct flow_match_mpls {
64 struct flow_dissector_key_mpls *key, *mask;
67 struct flow_match_enc_keyid {
68 struct flow_dissector_key_keyid *key, *mask;
71 struct flow_match_enc_opts {
72 struct flow_dissector_key_enc_opts *key, *mask;
75 struct flow_match_ct {
76 struct flow_dissector_key_ct *key, *mask;
81 void flow_rule_match_meta(const struct flow_rule *rule,
82 struct flow_match_meta *out);
83 void flow_rule_match_basic(const struct flow_rule *rule,
84 struct flow_match_basic *out);
85 void flow_rule_match_control(const struct flow_rule *rule,
86 struct flow_match_control *out);
87 void flow_rule_match_eth_addrs(const struct flow_rule *rule,
88 struct flow_match_eth_addrs *out);
89 void flow_rule_match_vlan(const struct flow_rule *rule,
90 struct flow_match_vlan *out);
91 void flow_rule_match_cvlan(const struct flow_rule *rule,
92 struct flow_match_vlan *out);
93 void flow_rule_match_ipv4_addrs(const struct flow_rule *rule,
94 struct flow_match_ipv4_addrs *out);
95 void flow_rule_match_ipv6_addrs(const struct flow_rule *rule,
96 struct flow_match_ipv6_addrs *out);
97 void flow_rule_match_ip(const struct flow_rule *rule,
98 struct flow_match_ip *out);
99 void flow_rule_match_ports(const struct flow_rule *rule,
100 struct flow_match_ports *out);
101 void flow_rule_match_ports_range(const struct flow_rule *rule,
102 struct flow_match_ports_range *out);
103 void flow_rule_match_tcp(const struct flow_rule *rule,
104 struct flow_match_tcp *out);
105 void flow_rule_match_icmp(const struct flow_rule *rule,
106 struct flow_match_icmp *out);
107 void flow_rule_match_mpls(const struct flow_rule *rule,
108 struct flow_match_mpls *out);
109 void flow_rule_match_enc_control(const struct flow_rule *rule,
110 struct flow_match_control *out);
111 void flow_rule_match_enc_ipv4_addrs(const struct flow_rule *rule,
112 struct flow_match_ipv4_addrs *out);
113 void flow_rule_match_enc_ipv6_addrs(const struct flow_rule *rule,
114 struct flow_match_ipv6_addrs *out);
115 void flow_rule_match_enc_ip(const struct flow_rule *rule,
116 struct flow_match_ip *out);
117 void flow_rule_match_enc_ports(const struct flow_rule *rule,
118 struct flow_match_ports *out);
119 void flow_rule_match_enc_keyid(const struct flow_rule *rule,
120 struct flow_match_enc_keyid *out);
121 void flow_rule_match_enc_opts(const struct flow_rule *rule,
122 struct flow_match_enc_opts *out);
123 void flow_rule_match_ct(const struct flow_rule *rule,
124 struct flow_match_ct *out);
126 enum flow_action_id {
127 FLOW_ACTION_ACCEPT = 0,
131 FLOW_ACTION_REDIRECT,
133 FLOW_ACTION_REDIRECT_INGRESS,
134 FLOW_ACTION_MIRRED_INGRESS,
135 FLOW_ACTION_VLAN_PUSH,
136 FLOW_ACTION_VLAN_POP,
137 FLOW_ACTION_VLAN_MANGLE,
138 FLOW_ACTION_TUNNEL_ENCAP,
139 FLOW_ACTION_TUNNEL_DECAP,
145 FLOW_ACTION_PRIORITY,
151 FLOW_ACTION_CT_METADATA,
152 FLOW_ACTION_MPLS_PUSH,
153 FLOW_ACTION_MPLS_POP,
154 FLOW_ACTION_MPLS_MANGLE,
156 FLOW_ACTION_PPPOE_PUSH,
159 FLOW_ACTION_VLAN_PUSH_ETH,
160 FLOW_ACTION_VLAN_POP_ETH,
161 FLOW_ACTION_CONTINUE,
165 /* This is mirroring enum pedit_header_type definition for easy mapping between
166 * tc pedit action. Legacy TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK is mapped to
167 * FLOW_ACT_MANGLE_UNSPEC, which is supported by no driver.
169 enum flow_action_mangle_base {
170 FLOW_ACT_MANGLE_UNSPEC = 0,
171 FLOW_ACT_MANGLE_HDR_TYPE_ETH,
172 FLOW_ACT_MANGLE_HDR_TYPE_IP4,
173 FLOW_ACT_MANGLE_HDR_TYPE_IP6,
174 FLOW_ACT_MANGLE_HDR_TYPE_TCP,
175 FLOW_ACT_MANGLE_HDR_TYPE_UDP,
178 enum flow_action_hw_stats_bit {
179 FLOW_ACTION_HW_STATS_IMMEDIATE_BIT,
180 FLOW_ACTION_HW_STATS_DELAYED_BIT,
181 FLOW_ACTION_HW_STATS_DISABLED_BIT,
183 FLOW_ACTION_HW_STATS_NUM_BITS
186 enum flow_action_hw_stats {
187 FLOW_ACTION_HW_STATS_IMMEDIATE =
188 BIT(FLOW_ACTION_HW_STATS_IMMEDIATE_BIT),
189 FLOW_ACTION_HW_STATS_DELAYED = BIT(FLOW_ACTION_HW_STATS_DELAYED_BIT),
190 FLOW_ACTION_HW_STATS_ANY = FLOW_ACTION_HW_STATS_IMMEDIATE |
191 FLOW_ACTION_HW_STATS_DELAYED,
192 FLOW_ACTION_HW_STATS_DISABLED =
193 BIT(FLOW_ACTION_HW_STATS_DISABLED_BIT),
194 FLOW_ACTION_HW_STATS_DONT_CARE = BIT(FLOW_ACTION_HW_STATS_NUM_BITS) - 1,
197 typedef void (*action_destr)(void *priv);
199 struct flow_action_cookie {
204 struct flow_action_cookie *flow_action_cookie_create(void *data,
207 void flow_action_cookie_destroy(struct flow_action_cookie *cookie);
209 struct flow_action_entry {
210 enum flow_action_id id;
212 enum flow_action_hw_stats hw_stats;
213 action_destr destructor;
214 void *destructor_priv;
216 u32 chain_index; /* FLOW_ACTION_GOTO */
217 struct net_device *dev; /* FLOW_ACTION_REDIRECT */
218 struct { /* FLOW_ACTION_VLAN */
223 struct { /* FLOW_ACTION_VLAN_PUSH_ETH */
224 unsigned char dst[ETH_ALEN];
225 unsigned char src[ETH_ALEN];
227 struct { /* FLOW_ACTION_MANGLE */
228 /* FLOW_ACTION_ADD */
229 enum flow_action_mangle_base htype;
234 struct ip_tunnel_info *tunnel; /* FLOW_ACTION_TUNNEL_ENCAP */
235 u32 csum_flags; /* FLOW_ACTION_CSUM */
236 u32 mark; /* FLOW_ACTION_MARK */
237 u16 ptype; /* FLOW_ACTION_PTYPE */
238 u32 priority; /* FLOW_ACTION_PRIORITY */
239 struct { /* FLOW_ACTION_QUEUE */
244 struct { /* FLOW_ACTION_SAMPLE */
245 struct psample_group *psample_group;
250 struct { /* FLOW_ACTION_POLICE */
253 u64 peakrate_bytes_ps;
260 enum flow_action_id act_id;
264 struct { /* FLOW_ACTION_CT */
267 struct nf_flowtable *flow_table;
270 unsigned long cookie;
275 struct { /* FLOW_ACTION_MPLS_PUSH */
282 struct { /* FLOW_ACTION_MPLS_POP */
285 struct { /* FLOW_ACTION_MPLS_MANGLE */
297 struct action_gate_entry *entries;
299 struct { /* FLOW_ACTION_PPPOE_PUSH */
303 struct flow_action_cookie *cookie; /* user defined action cookie */
307 unsigned int num_entries;
308 struct flow_action_entry entries[];
311 static inline bool flow_action_has_entries(const struct flow_action *action)
313 return action->num_entries;
317 * flow_offload_has_one_action() - check if exactly one action is present
318 * @action: tc filter flow offload action
320 * Returns true if exactly one action is present.
322 static inline bool flow_offload_has_one_action(const struct flow_action *action)
324 return action->num_entries == 1;
327 static inline bool flow_action_is_last_entry(const struct flow_action *action,
328 const struct flow_action_entry *entry)
330 return entry == &action->entries[action->num_entries - 1];
333 #define flow_action_for_each(__i, __act, __actions) \
334 for (__i = 0, __act = &(__actions)->entries[0]; \
335 __i < (__actions)->num_entries; \
336 __act = &(__actions)->entries[++__i])
339 flow_action_mixed_hw_stats_check(const struct flow_action *action,
340 struct netlink_ext_ack *extack)
342 const struct flow_action_entry *action_entry;
346 if (flow_offload_has_one_action(action))
349 flow_action_for_each(i, action_entry, action) {
350 if (i && action_entry->hw_stats != last_hw_stats) {
351 NL_SET_ERR_MSG_MOD(extack, "Mixing HW stats types for actions is not supported");
354 last_hw_stats = action_entry->hw_stats;
359 static inline const struct flow_action_entry *
360 flow_action_first_entry_get(const struct flow_action *action)
362 WARN_ON(!flow_action_has_entries(action));
363 return &action->entries[0];
367 __flow_action_hw_stats_check(const struct flow_action *action,
368 struct netlink_ext_ack *extack,
369 bool check_allow_bit,
370 enum flow_action_hw_stats_bit allow_bit)
372 const struct flow_action_entry *action_entry;
374 if (!flow_action_has_entries(action))
376 if (!flow_action_mixed_hw_stats_check(action, extack))
379 action_entry = flow_action_first_entry_get(action);
381 /* Zero is not a legal value for hw_stats, catch anyone passing it */
382 WARN_ON_ONCE(!action_entry->hw_stats);
384 if (!check_allow_bit &&
385 ~action_entry->hw_stats & FLOW_ACTION_HW_STATS_ANY) {
386 NL_SET_ERR_MSG_MOD(extack, "Driver supports only default HW stats type \"any\"");
388 } else if (check_allow_bit &&
389 !(action_entry->hw_stats & BIT(allow_bit))) {
390 NL_SET_ERR_MSG_MOD(extack, "Driver does not support selected HW stats type");
397 flow_action_hw_stats_check(const struct flow_action *action,
398 struct netlink_ext_ack *extack,
399 enum flow_action_hw_stats_bit allow_bit)
401 return __flow_action_hw_stats_check(action, extack, true, allow_bit);
405 flow_action_basic_hw_stats_check(const struct flow_action *action,
406 struct netlink_ext_ack *extack)
408 return __flow_action_hw_stats_check(action, extack, false, 0);
412 struct flow_match match;
413 struct flow_action action;
416 struct flow_rule *flow_rule_alloc(unsigned int num_actions);
418 static inline bool flow_rule_match_key(const struct flow_rule *rule,
419 enum flow_dissector_key_id key)
421 return dissector_uses_key(rule->match.dissector, key);
429 enum flow_action_hw_stats used_hw_stats;
430 bool used_hw_stats_valid;
433 static inline void flow_stats_update(struct flow_stats *flow_stats,
435 u64 drops, u64 lastused,
436 enum flow_action_hw_stats used_hw_stats)
438 flow_stats->pkts += pkts;
439 flow_stats->bytes += bytes;
440 flow_stats->drops += drops;
441 flow_stats->lastused = max_t(u64, flow_stats->lastused, lastused);
443 /* The driver should pass value with a maximum of one bit set.
444 * Passing FLOW_ACTION_HW_STATS_ANY is invalid.
446 WARN_ON(used_hw_stats == FLOW_ACTION_HW_STATS_ANY);
447 flow_stats->used_hw_stats |= used_hw_stats;
448 flow_stats->used_hw_stats_valid = true;
451 enum flow_block_command {
456 enum flow_block_binder_type {
457 FLOW_BLOCK_BINDER_TYPE_UNSPEC,
458 FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS,
459 FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS,
460 FLOW_BLOCK_BINDER_TYPE_RED_EARLY_DROP,
461 FLOW_BLOCK_BINDER_TYPE_RED_MARK,
465 struct list_head cb_list;
468 struct netlink_ext_ack;
470 struct flow_block_offload {
471 enum flow_block_command command;
472 enum flow_block_binder_type binder_type;
474 bool unlocked_driver_cb;
476 struct flow_block *block;
477 struct list_head cb_list;
478 struct list_head *driver_block_list;
479 struct netlink_ext_ack *extack;
481 struct list_head *cb_list_head;
485 typedef int flow_setup_cb_t(enum tc_setup_type type, void *type_data,
488 struct flow_block_cb;
490 struct flow_block_indr {
491 struct list_head list;
492 struct net_device *dev;
494 enum flow_block_binder_type binder_type;
497 void (*cleanup)(struct flow_block_cb *block_cb);
500 struct flow_block_cb {
501 struct list_head driver_list;
502 struct list_head list;
506 void (*release)(void *cb_priv);
507 struct flow_block_indr indr;
511 struct flow_block_cb *flow_block_cb_alloc(flow_setup_cb_t *cb,
512 void *cb_ident, void *cb_priv,
513 void (*release)(void *cb_priv));
514 struct flow_block_cb *flow_indr_block_cb_alloc(flow_setup_cb_t *cb,
515 void *cb_ident, void *cb_priv,
516 void (*release)(void *cb_priv),
517 struct flow_block_offload *bo,
518 struct net_device *dev,
519 struct Qdisc *sch, void *data,
521 void (*cleanup)(struct flow_block_cb *block_cb));
522 void flow_block_cb_free(struct flow_block_cb *block_cb);
524 struct flow_block_cb *flow_block_cb_lookup(struct flow_block *block,
525 flow_setup_cb_t *cb, void *cb_ident);
527 void *flow_block_cb_priv(struct flow_block_cb *block_cb);
528 void flow_block_cb_incref(struct flow_block_cb *block_cb);
529 unsigned int flow_block_cb_decref(struct flow_block_cb *block_cb);
531 static inline void flow_block_cb_add(struct flow_block_cb *block_cb,
532 struct flow_block_offload *offload)
534 list_add_tail(&block_cb->list, &offload->cb_list);
537 static inline void flow_block_cb_remove(struct flow_block_cb *block_cb,
538 struct flow_block_offload *offload)
540 list_move(&block_cb->list, &offload->cb_list);
543 static inline void flow_indr_block_cb_remove(struct flow_block_cb *block_cb,
544 struct flow_block_offload *offload)
546 list_del(&block_cb->indr.list);
547 list_move(&block_cb->list, &offload->cb_list);
550 bool flow_block_cb_is_busy(flow_setup_cb_t *cb, void *cb_ident,
551 struct list_head *driver_block_list);
553 int flow_block_cb_setup_simple(struct flow_block_offload *f,
554 struct list_head *driver_list,
556 void *cb_ident, void *cb_priv, bool ingress_only);
558 enum flow_cls_command {
562 FLOW_CLS_TMPLT_CREATE,
563 FLOW_CLS_TMPLT_DESTROY,
566 struct flow_cls_common_offload {
570 struct netlink_ext_ack *extack;
573 struct flow_cls_offload {
574 struct flow_cls_common_offload common;
575 enum flow_cls_command command;
576 unsigned long cookie;
577 struct flow_rule *rule;
578 struct flow_stats stats;
582 enum offload_act_command {
588 struct flow_offload_action {
589 struct netlink_ext_ack *extack; /* NULL in FLOW_ACT_STATS process*/
590 enum offload_act_command command;
591 enum flow_action_id id;
593 struct flow_stats stats;
594 struct flow_action action;
597 struct flow_offload_action *offload_action_alloc(unsigned int num_actions);
599 static inline struct flow_rule *
600 flow_cls_offload_flow_rule(struct flow_cls_offload *flow_cmd)
602 return flow_cmd->rule;
605 static inline void flow_block_init(struct flow_block *flow_block)
607 INIT_LIST_HEAD(&flow_block->cb_list);
610 typedef int flow_indr_block_bind_cb_t(struct net_device *dev, struct Qdisc *sch, void *cb_priv,
611 enum tc_setup_type type, void *type_data,
613 void (*cleanup)(struct flow_block_cb *block_cb));
615 int flow_indr_dev_register(flow_indr_block_bind_cb_t *cb, void *cb_priv);
616 void flow_indr_dev_unregister(flow_indr_block_bind_cb_t *cb, void *cb_priv,
617 void (*release)(void *cb_priv));
618 int flow_indr_dev_setup_offload(struct net_device *dev, struct Qdisc *sch,
619 enum tc_setup_type type, void *data,
620 struct flow_block_offload *bo,
621 void (*cleanup)(struct flow_block_cb *block_cb));
622 bool flow_indr_dev_exists(void);
624 #endif /* _NET_FLOW_OFFLOAD_H */