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;
79 struct flow_match_pppoe {
80 struct flow_dissector_key_pppoe *key, *mask;
83 struct flow_match_l2tpv3 {
84 struct flow_dissector_key_l2tpv3 *key, *mask;
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);
138 enum flow_action_id {
139 FLOW_ACTION_ACCEPT = 0,
143 FLOW_ACTION_REDIRECT,
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,
157 FLOW_ACTION_PRIORITY,
163 FLOW_ACTION_CT_METADATA,
164 FLOW_ACTION_MPLS_PUSH,
165 FLOW_ACTION_MPLS_POP,
166 FLOW_ACTION_MPLS_MANGLE,
168 FLOW_ACTION_PPPOE_PUSH,
171 FLOW_ACTION_VLAN_PUSH_ETH,
172 FLOW_ACTION_VLAN_POP_ETH,
173 FLOW_ACTION_CONTINUE,
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.
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,
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,
195 FLOW_ACTION_HW_STATS_NUM_BITS
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,
209 typedef void (*action_destr)(void *priv);
211 struct flow_action_cookie {
216 struct flow_action_cookie *flow_action_cookie_create(void *data,
219 void flow_action_cookie_destroy(struct flow_action_cookie *cookie);
221 struct flow_action_entry {
222 enum flow_action_id id;
224 enum flow_action_hw_stats hw_stats;
225 action_destr destructor;
226 void *destructor_priv;
228 u32 chain_index; /* FLOW_ACTION_GOTO */
229 struct net_device *dev; /* FLOW_ACTION_REDIRECT */
230 struct { /* FLOW_ACTION_VLAN */
235 struct { /* FLOW_ACTION_VLAN_PUSH_ETH */
236 unsigned char dst[ETH_ALEN];
237 unsigned char src[ETH_ALEN];
239 struct { /* FLOW_ACTION_MANGLE */
240 /* FLOW_ACTION_ADD */
241 enum flow_action_mangle_base htype;
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 */
256 struct { /* FLOW_ACTION_SAMPLE */
257 struct psample_group *psample_group;
262 struct { /* FLOW_ACTION_POLICE */
265 u64 peakrate_bytes_ps;
272 enum flow_action_id act_id;
276 struct { /* FLOW_ACTION_CT */
279 struct nf_flowtable *flow_table;
282 unsigned long cookie;
287 struct { /* FLOW_ACTION_MPLS_PUSH */
294 struct { /* FLOW_ACTION_MPLS_POP */
297 struct { /* FLOW_ACTION_MPLS_MANGLE */
309 struct action_gate_entry *entries;
311 struct { /* FLOW_ACTION_PPPOE_PUSH */
315 struct flow_action_cookie *cookie; /* user defined action cookie */
319 unsigned int num_entries;
320 struct flow_action_entry entries[];
323 static inline bool flow_action_has_entries(const struct flow_action *action)
325 return action->num_entries;
329 * flow_offload_has_one_action() - check if exactly one action is present
330 * @action: tc filter flow offload action
332 * Returns true if exactly one action is present.
334 static inline bool flow_offload_has_one_action(const struct flow_action *action)
336 return action->num_entries == 1;
339 static inline bool flow_action_is_last_entry(const struct flow_action *action,
340 const struct flow_action_entry *entry)
342 return entry == &action->entries[action->num_entries - 1];
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])
351 flow_action_mixed_hw_stats_check(const struct flow_action *action,
352 struct netlink_ext_ack *extack)
354 const struct flow_action_entry *action_entry;
358 if (flow_offload_has_one_action(action))
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");
366 last_hw_stats = action_entry->hw_stats;
371 static inline const struct flow_action_entry *
372 flow_action_first_entry_get(const struct flow_action *action)
374 WARN_ON(!flow_action_has_entries(action));
375 return &action->entries[0];
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)
384 const struct flow_action_entry *action_entry;
386 if (!flow_action_has_entries(action))
388 if (!flow_action_mixed_hw_stats_check(action, extack))
391 action_entry = flow_action_first_entry_get(action);
393 /* Zero is not a legal value for hw_stats, catch anyone passing it */
394 WARN_ON_ONCE(!action_entry->hw_stats);
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\"");
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");
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)
413 return __flow_action_hw_stats_check(action, extack, true, allow_bit);
417 flow_action_basic_hw_stats_check(const struct flow_action *action,
418 struct netlink_ext_ack *extack)
420 return __flow_action_hw_stats_check(action, extack, false, 0);
424 struct flow_match match;
425 struct flow_action action;
428 struct flow_rule *flow_rule_alloc(unsigned int num_actions);
430 static inline bool flow_rule_match_key(const struct flow_rule *rule,
431 enum flow_dissector_key_id key)
433 return dissector_uses_key(rule->match.dissector, key);
441 enum flow_action_hw_stats used_hw_stats;
442 bool used_hw_stats_valid;
445 static inline void flow_stats_update(struct flow_stats *flow_stats,
447 u64 drops, u64 lastused,
448 enum flow_action_hw_stats used_hw_stats)
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);
455 /* The driver should pass value with a maximum of one bit set.
456 * Passing FLOW_ACTION_HW_STATS_ANY is invalid.
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;
463 enum flow_block_command {
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,
477 struct list_head cb_list;
480 struct netlink_ext_ack;
482 struct flow_block_offload {
483 enum flow_block_command command;
484 enum flow_block_binder_type binder_type;
486 bool unlocked_driver_cb;
488 struct flow_block *block;
489 struct list_head cb_list;
490 struct list_head *driver_block_list;
491 struct netlink_ext_ack *extack;
493 struct list_head *cb_list_head;
497 typedef int flow_setup_cb_t(enum tc_setup_type type, void *type_data,
500 struct flow_block_cb;
502 struct flow_block_indr {
503 struct list_head list;
504 struct net_device *dev;
506 enum flow_block_binder_type binder_type;
509 void (*cleanup)(struct flow_block_cb *block_cb);
512 struct flow_block_cb {
513 struct list_head driver_list;
514 struct list_head list;
518 void (*release)(void *cb_priv);
519 struct flow_block_indr indr;
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,
533 void (*cleanup)(struct flow_block_cb *block_cb));
534 void flow_block_cb_free(struct flow_block_cb *block_cb);
536 struct flow_block_cb *flow_block_cb_lookup(struct flow_block *block,
537 flow_setup_cb_t *cb, void *cb_ident);
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);
543 static inline void flow_block_cb_add(struct flow_block_cb *block_cb,
544 struct flow_block_offload *offload)
546 list_add_tail(&block_cb->list, &offload->cb_list);
549 static inline void flow_block_cb_remove(struct flow_block_cb *block_cb,
550 struct flow_block_offload *offload)
552 list_move(&block_cb->list, &offload->cb_list);
555 static inline void flow_indr_block_cb_remove(struct flow_block_cb *block_cb,
556 struct flow_block_offload *offload)
558 list_del(&block_cb->indr.list);
559 list_move(&block_cb->list, &offload->cb_list);
562 bool flow_block_cb_is_busy(flow_setup_cb_t *cb, void *cb_ident,
563 struct list_head *driver_block_list);
565 int flow_block_cb_setup_simple(struct flow_block_offload *f,
566 struct list_head *driver_list,
568 void *cb_ident, void *cb_priv, bool ingress_only);
570 enum flow_cls_command {
574 FLOW_CLS_TMPLT_CREATE,
575 FLOW_CLS_TMPLT_DESTROY,
578 struct flow_cls_common_offload {
582 struct netlink_ext_ack *extack;
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;
594 enum offload_act_command {
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;
605 struct flow_stats stats;
606 struct flow_action action;
609 struct flow_offload_action *offload_action_alloc(unsigned int num_actions);
611 static inline struct flow_rule *
612 flow_cls_offload_flow_rule(struct flow_cls_offload *flow_cmd)
614 return flow_cmd->rule;
617 static inline void flow_block_init(struct flow_block *flow_block)
619 INIT_LIST_HEAD(&flow_block->cb_list);
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,
625 void (*cleanup)(struct flow_block_cb *block_cb));
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);
636 #endif /* _NET_FLOW_OFFLOAD_H */