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_arp {
36 struct flow_dissector_key_arp *key, *mask;
39 struct flow_match_ipv4_addrs {
40 struct flow_dissector_key_ipv4_addrs *key, *mask;
43 struct flow_match_ipv6_addrs {
44 struct flow_dissector_key_ipv6_addrs *key, *mask;
47 struct flow_match_ip {
48 struct flow_dissector_key_ip *key, *mask;
51 struct flow_match_ports {
52 struct flow_dissector_key_ports *key, *mask;
55 struct flow_match_ports_range {
56 struct flow_dissector_key_ports_range *key, *mask;
59 struct flow_match_icmp {
60 struct flow_dissector_key_icmp *key, *mask;
63 struct flow_match_tcp {
64 struct flow_dissector_key_tcp *key, *mask;
67 struct flow_match_mpls {
68 struct flow_dissector_key_mpls *key, *mask;
71 struct flow_match_enc_keyid {
72 struct flow_dissector_key_keyid *key, *mask;
75 struct flow_match_enc_opts {
76 struct flow_dissector_key_enc_opts *key, *mask;
79 struct flow_match_ct {
80 struct flow_dissector_key_ct *key, *mask;
83 struct flow_match_pppoe {
84 struct flow_dissector_key_pppoe *key, *mask;
87 struct flow_match_l2tpv3 {
88 struct flow_dissector_key_l2tpv3 *key, *mask;
93 void flow_rule_match_meta(const struct flow_rule *rule,
94 struct flow_match_meta *out);
95 void flow_rule_match_basic(const struct flow_rule *rule,
96 struct flow_match_basic *out);
97 void flow_rule_match_control(const struct flow_rule *rule,
98 struct flow_match_control *out);
99 void flow_rule_match_eth_addrs(const struct flow_rule *rule,
100 struct flow_match_eth_addrs *out);
101 void flow_rule_match_vlan(const struct flow_rule *rule,
102 struct flow_match_vlan *out);
103 void flow_rule_match_cvlan(const struct flow_rule *rule,
104 struct flow_match_vlan *out);
105 void flow_rule_match_arp(const struct flow_rule *rule,
106 struct flow_match_arp *out);
107 void flow_rule_match_ipv4_addrs(const struct flow_rule *rule,
108 struct flow_match_ipv4_addrs *out);
109 void flow_rule_match_ipv6_addrs(const struct flow_rule *rule,
110 struct flow_match_ipv6_addrs *out);
111 void flow_rule_match_ip(const struct flow_rule *rule,
112 struct flow_match_ip *out);
113 void flow_rule_match_ports(const struct flow_rule *rule,
114 struct flow_match_ports *out);
115 void flow_rule_match_ports_range(const struct flow_rule *rule,
116 struct flow_match_ports_range *out);
117 void flow_rule_match_tcp(const struct flow_rule *rule,
118 struct flow_match_tcp *out);
119 void flow_rule_match_icmp(const struct flow_rule *rule,
120 struct flow_match_icmp *out);
121 void flow_rule_match_mpls(const struct flow_rule *rule,
122 struct flow_match_mpls *out);
123 void flow_rule_match_enc_control(const struct flow_rule *rule,
124 struct flow_match_control *out);
125 void flow_rule_match_enc_ipv4_addrs(const struct flow_rule *rule,
126 struct flow_match_ipv4_addrs *out);
127 void flow_rule_match_enc_ipv6_addrs(const struct flow_rule *rule,
128 struct flow_match_ipv6_addrs *out);
129 void flow_rule_match_enc_ip(const struct flow_rule *rule,
130 struct flow_match_ip *out);
131 void flow_rule_match_enc_ports(const struct flow_rule *rule,
132 struct flow_match_ports *out);
133 void flow_rule_match_enc_keyid(const struct flow_rule *rule,
134 struct flow_match_enc_keyid *out);
135 void flow_rule_match_enc_opts(const struct flow_rule *rule,
136 struct flow_match_enc_opts *out);
137 void flow_rule_match_ct(const struct flow_rule *rule,
138 struct flow_match_ct *out);
139 void flow_rule_match_pppoe(const struct flow_rule *rule,
140 struct flow_match_pppoe *out);
141 void flow_rule_match_l2tpv3(const struct flow_rule *rule,
142 struct flow_match_l2tpv3 *out);
144 enum flow_action_id {
145 FLOW_ACTION_ACCEPT = 0,
149 FLOW_ACTION_REDIRECT,
151 FLOW_ACTION_REDIRECT_INGRESS,
152 FLOW_ACTION_MIRRED_INGRESS,
153 FLOW_ACTION_VLAN_PUSH,
154 FLOW_ACTION_VLAN_POP,
155 FLOW_ACTION_VLAN_MANGLE,
156 FLOW_ACTION_TUNNEL_ENCAP,
157 FLOW_ACTION_TUNNEL_DECAP,
163 FLOW_ACTION_PRIORITY,
164 FLOW_ACTION_RX_QUEUE_MAPPING,
170 FLOW_ACTION_CT_METADATA,
171 FLOW_ACTION_MPLS_PUSH,
172 FLOW_ACTION_MPLS_POP,
173 FLOW_ACTION_MPLS_MANGLE,
175 FLOW_ACTION_PPPOE_PUSH,
178 FLOW_ACTION_VLAN_PUSH_ETH,
179 FLOW_ACTION_VLAN_POP_ETH,
180 FLOW_ACTION_CONTINUE,
184 /* This is mirroring enum pedit_header_type definition for easy mapping between
185 * tc pedit action. Legacy TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK is mapped to
186 * FLOW_ACT_MANGLE_UNSPEC, which is supported by no driver.
188 enum flow_action_mangle_base {
189 FLOW_ACT_MANGLE_UNSPEC = 0,
190 FLOW_ACT_MANGLE_HDR_TYPE_ETH,
191 FLOW_ACT_MANGLE_HDR_TYPE_IP4,
192 FLOW_ACT_MANGLE_HDR_TYPE_IP6,
193 FLOW_ACT_MANGLE_HDR_TYPE_TCP,
194 FLOW_ACT_MANGLE_HDR_TYPE_UDP,
197 enum flow_action_hw_stats_bit {
198 FLOW_ACTION_HW_STATS_IMMEDIATE_BIT,
199 FLOW_ACTION_HW_STATS_DELAYED_BIT,
200 FLOW_ACTION_HW_STATS_DISABLED_BIT,
202 FLOW_ACTION_HW_STATS_NUM_BITS
205 enum flow_action_hw_stats {
206 FLOW_ACTION_HW_STATS_IMMEDIATE =
207 BIT(FLOW_ACTION_HW_STATS_IMMEDIATE_BIT),
208 FLOW_ACTION_HW_STATS_DELAYED = BIT(FLOW_ACTION_HW_STATS_DELAYED_BIT),
209 FLOW_ACTION_HW_STATS_ANY = FLOW_ACTION_HW_STATS_IMMEDIATE |
210 FLOW_ACTION_HW_STATS_DELAYED,
211 FLOW_ACTION_HW_STATS_DISABLED =
212 BIT(FLOW_ACTION_HW_STATS_DISABLED_BIT),
213 FLOW_ACTION_HW_STATS_DONT_CARE = BIT(FLOW_ACTION_HW_STATS_NUM_BITS) - 1,
216 typedef void (*action_destr)(void *priv);
218 struct flow_action_cookie {
223 struct flow_action_cookie *flow_action_cookie_create(void *data,
226 void flow_action_cookie_destroy(struct flow_action_cookie *cookie);
228 struct flow_action_entry {
229 enum flow_action_id id;
231 enum flow_action_hw_stats hw_stats;
232 action_destr destructor;
233 void *destructor_priv;
235 u32 chain_index; /* FLOW_ACTION_GOTO */
236 struct net_device *dev; /* FLOW_ACTION_REDIRECT */
237 struct { /* FLOW_ACTION_VLAN */
242 struct { /* FLOW_ACTION_VLAN_PUSH_ETH */
243 unsigned char dst[ETH_ALEN];
244 unsigned char src[ETH_ALEN];
246 struct { /* FLOW_ACTION_MANGLE */
247 /* FLOW_ACTION_ADD */
248 enum flow_action_mangle_base htype;
253 struct ip_tunnel_info *tunnel; /* FLOW_ACTION_TUNNEL_ENCAP */
254 u32 csum_flags; /* FLOW_ACTION_CSUM */
255 u32 mark; /* FLOW_ACTION_MARK */
256 u16 ptype; /* FLOW_ACTION_PTYPE */
257 u16 rx_queue; /* FLOW_ACTION_RX_QUEUE_MAPPING */
258 u32 priority; /* FLOW_ACTION_PRIORITY */
259 struct { /* FLOW_ACTION_QUEUE */
264 struct { /* FLOW_ACTION_SAMPLE */
265 struct psample_group *psample_group;
270 struct { /* FLOW_ACTION_POLICE */
273 u64 peakrate_bytes_ps;
280 enum flow_action_id act_id;
284 struct { /* FLOW_ACTION_CT */
287 struct nf_flowtable *flow_table;
290 unsigned long cookie;
295 struct { /* FLOW_ACTION_MPLS_PUSH */
302 struct { /* FLOW_ACTION_MPLS_POP */
305 struct { /* FLOW_ACTION_MPLS_MANGLE */
317 struct action_gate_entry *entries;
319 struct { /* FLOW_ACTION_PPPOE_PUSH */
323 struct flow_action_cookie *cookie; /* user defined action cookie */
327 unsigned int num_entries;
328 struct flow_action_entry entries[];
331 static inline bool flow_action_has_entries(const struct flow_action *action)
333 return action->num_entries;
337 * flow_offload_has_one_action() - check if exactly one action is present
338 * @action: tc filter flow offload action
340 * Returns true if exactly one action is present.
342 static inline bool flow_offload_has_one_action(const struct flow_action *action)
344 return action->num_entries == 1;
347 static inline bool flow_action_is_last_entry(const struct flow_action *action,
348 const struct flow_action_entry *entry)
350 return entry == &action->entries[action->num_entries - 1];
353 #define flow_action_for_each(__i, __act, __actions) \
354 for (__i = 0, __act = &(__actions)->entries[0]; \
355 __i < (__actions)->num_entries; \
356 __act = &(__actions)->entries[++__i])
359 flow_action_mixed_hw_stats_check(const struct flow_action *action,
360 struct netlink_ext_ack *extack)
362 const struct flow_action_entry *action_entry;
366 if (flow_offload_has_one_action(action))
369 flow_action_for_each(i, action_entry, action) {
370 if (i && action_entry->hw_stats != last_hw_stats) {
371 NL_SET_ERR_MSG_MOD(extack, "Mixing HW stats types for actions is not supported");
374 last_hw_stats = action_entry->hw_stats;
379 static inline const struct flow_action_entry *
380 flow_action_first_entry_get(const struct flow_action *action)
382 WARN_ON(!flow_action_has_entries(action));
383 return &action->entries[0];
387 __flow_action_hw_stats_check(const struct flow_action *action,
388 struct netlink_ext_ack *extack,
389 bool check_allow_bit,
390 enum flow_action_hw_stats_bit allow_bit)
392 const struct flow_action_entry *action_entry;
394 if (!flow_action_has_entries(action))
396 if (!flow_action_mixed_hw_stats_check(action, extack))
399 action_entry = flow_action_first_entry_get(action);
401 /* Zero is not a legal value for hw_stats, catch anyone passing it */
402 WARN_ON_ONCE(!action_entry->hw_stats);
404 if (!check_allow_bit &&
405 ~action_entry->hw_stats & FLOW_ACTION_HW_STATS_ANY) {
406 NL_SET_ERR_MSG_MOD(extack, "Driver supports only default HW stats type \"any\"");
408 } else if (check_allow_bit &&
409 !(action_entry->hw_stats & BIT(allow_bit))) {
410 NL_SET_ERR_MSG_MOD(extack, "Driver does not support selected HW stats type");
417 flow_action_hw_stats_check(const struct flow_action *action,
418 struct netlink_ext_ack *extack,
419 enum flow_action_hw_stats_bit allow_bit)
421 return __flow_action_hw_stats_check(action, extack, true, allow_bit);
425 flow_action_basic_hw_stats_check(const struct flow_action *action,
426 struct netlink_ext_ack *extack)
428 return __flow_action_hw_stats_check(action, extack, false, 0);
432 struct flow_match match;
433 struct flow_action action;
436 struct flow_rule *flow_rule_alloc(unsigned int num_actions);
438 static inline bool flow_rule_match_key(const struct flow_rule *rule,
439 enum flow_dissector_key_id key)
441 return dissector_uses_key(rule->match.dissector, key);
449 enum flow_action_hw_stats used_hw_stats;
450 bool used_hw_stats_valid;
453 static inline void flow_stats_update(struct flow_stats *flow_stats,
455 u64 drops, u64 lastused,
456 enum flow_action_hw_stats used_hw_stats)
458 flow_stats->pkts += pkts;
459 flow_stats->bytes += bytes;
460 flow_stats->drops += drops;
461 flow_stats->lastused = max_t(u64, flow_stats->lastused, lastused);
463 /* The driver should pass value with a maximum of one bit set.
464 * Passing FLOW_ACTION_HW_STATS_ANY is invalid.
466 WARN_ON(used_hw_stats == FLOW_ACTION_HW_STATS_ANY);
467 flow_stats->used_hw_stats |= used_hw_stats;
468 flow_stats->used_hw_stats_valid = true;
471 enum flow_block_command {
476 enum flow_block_binder_type {
477 FLOW_BLOCK_BINDER_TYPE_UNSPEC,
478 FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS,
479 FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS,
480 FLOW_BLOCK_BINDER_TYPE_RED_EARLY_DROP,
481 FLOW_BLOCK_BINDER_TYPE_RED_MARK,
485 struct list_head cb_list;
488 struct netlink_ext_ack;
490 struct flow_block_offload {
491 enum flow_block_command command;
492 enum flow_block_binder_type binder_type;
494 bool unlocked_driver_cb;
496 struct flow_block *block;
497 struct list_head cb_list;
498 struct list_head *driver_block_list;
499 struct netlink_ext_ack *extack;
501 struct list_head *cb_list_head;
505 typedef int flow_setup_cb_t(enum tc_setup_type type, void *type_data,
508 struct flow_block_cb;
510 struct flow_block_indr {
511 struct list_head list;
512 struct net_device *dev;
514 enum flow_block_binder_type binder_type;
517 void (*cleanup)(struct flow_block_cb *block_cb);
520 struct flow_block_cb {
521 struct list_head driver_list;
522 struct list_head list;
526 void (*release)(void *cb_priv);
527 struct flow_block_indr indr;
531 struct flow_block_cb *flow_block_cb_alloc(flow_setup_cb_t *cb,
532 void *cb_ident, void *cb_priv,
533 void (*release)(void *cb_priv));
534 struct flow_block_cb *flow_indr_block_cb_alloc(flow_setup_cb_t *cb,
535 void *cb_ident, void *cb_priv,
536 void (*release)(void *cb_priv),
537 struct flow_block_offload *bo,
538 struct net_device *dev,
539 struct Qdisc *sch, void *data,
541 void (*cleanup)(struct flow_block_cb *block_cb));
542 void flow_block_cb_free(struct flow_block_cb *block_cb);
544 struct flow_block_cb *flow_block_cb_lookup(struct flow_block *block,
545 flow_setup_cb_t *cb, void *cb_ident);
547 void *flow_block_cb_priv(struct flow_block_cb *block_cb);
548 void flow_block_cb_incref(struct flow_block_cb *block_cb);
549 unsigned int flow_block_cb_decref(struct flow_block_cb *block_cb);
551 static inline void flow_block_cb_add(struct flow_block_cb *block_cb,
552 struct flow_block_offload *offload)
554 list_add_tail(&block_cb->list, &offload->cb_list);
557 static inline void flow_block_cb_remove(struct flow_block_cb *block_cb,
558 struct flow_block_offload *offload)
560 list_move(&block_cb->list, &offload->cb_list);
563 static inline void flow_indr_block_cb_remove(struct flow_block_cb *block_cb,
564 struct flow_block_offload *offload)
566 list_del(&block_cb->indr.list);
567 list_move(&block_cb->list, &offload->cb_list);
570 bool flow_block_cb_is_busy(flow_setup_cb_t *cb, void *cb_ident,
571 struct list_head *driver_block_list);
573 int flow_block_cb_setup_simple(struct flow_block_offload *f,
574 struct list_head *driver_list,
576 void *cb_ident, void *cb_priv, bool ingress_only);
578 enum flow_cls_command {
582 FLOW_CLS_TMPLT_CREATE,
583 FLOW_CLS_TMPLT_DESTROY,
586 struct flow_cls_common_offload {
590 struct netlink_ext_ack *extack;
593 struct flow_cls_offload {
594 struct flow_cls_common_offload common;
595 enum flow_cls_command command;
596 unsigned long cookie;
597 struct flow_rule *rule;
598 struct flow_stats stats;
602 enum offload_act_command {
608 struct flow_offload_action {
609 struct netlink_ext_ack *extack; /* NULL in FLOW_ACT_STATS process*/
610 enum offload_act_command command;
611 enum flow_action_id id;
613 struct flow_stats stats;
614 struct flow_action action;
617 struct flow_offload_action *offload_action_alloc(unsigned int num_actions);
619 static inline struct flow_rule *
620 flow_cls_offload_flow_rule(struct flow_cls_offload *flow_cmd)
622 return flow_cmd->rule;
625 static inline void flow_block_init(struct flow_block *flow_block)
627 INIT_LIST_HEAD(&flow_block->cb_list);
630 typedef int flow_indr_block_bind_cb_t(struct net_device *dev, struct Qdisc *sch, void *cb_priv,
631 enum tc_setup_type type, void *type_data,
633 void (*cleanup)(struct flow_block_cb *block_cb));
635 int flow_indr_dev_register(flow_indr_block_bind_cb_t *cb, void *cb_priv);
636 void flow_indr_dev_unregister(flow_indr_block_bind_cb_t *cb, void *cb_priv,
637 void (*release)(void *cb_priv));
638 int flow_indr_dev_setup_offload(struct net_device *dev, struct Qdisc *sch,
639 enum tc_setup_type type, void *data,
640 struct flow_block_offload *bo,
641 void (*cleanup)(struct flow_block_cb *block_cb));
642 bool flow_indr_dev_exists(void);
644 #endif /* _NET_FLOW_OFFLOAD_H */