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_icmp {
52 struct flow_dissector_key_icmp *key, *mask;
55 struct flow_match_tcp {
56 struct flow_dissector_key_tcp *key, *mask;
59 struct flow_match_mpls {
60 struct flow_dissector_key_mpls *key, *mask;
63 struct flow_match_enc_keyid {
64 struct flow_dissector_key_keyid *key, *mask;
67 struct flow_match_enc_opts {
68 struct flow_dissector_key_enc_opts *key, *mask;
71 struct flow_match_ct {
72 struct flow_dissector_key_ct *key, *mask;
77 void flow_rule_match_meta(const struct flow_rule *rule,
78 struct flow_match_meta *out);
79 void flow_rule_match_basic(const struct flow_rule *rule,
80 struct flow_match_basic *out);
81 void flow_rule_match_control(const struct flow_rule *rule,
82 struct flow_match_control *out);
83 void flow_rule_match_eth_addrs(const struct flow_rule *rule,
84 struct flow_match_eth_addrs *out);
85 void flow_rule_match_vlan(const struct flow_rule *rule,
86 struct flow_match_vlan *out);
87 void flow_rule_match_cvlan(const struct flow_rule *rule,
88 struct flow_match_vlan *out);
89 void flow_rule_match_ipv4_addrs(const struct flow_rule *rule,
90 struct flow_match_ipv4_addrs *out);
91 void flow_rule_match_ipv6_addrs(const struct flow_rule *rule,
92 struct flow_match_ipv6_addrs *out);
93 void flow_rule_match_ip(const struct flow_rule *rule,
94 struct flow_match_ip *out);
95 void flow_rule_match_ports(const struct flow_rule *rule,
96 struct flow_match_ports *out);
97 void flow_rule_match_tcp(const struct flow_rule *rule,
98 struct flow_match_tcp *out);
99 void flow_rule_match_icmp(const struct flow_rule *rule,
100 struct flow_match_icmp *out);
101 void flow_rule_match_mpls(const struct flow_rule *rule,
102 struct flow_match_mpls *out);
103 void flow_rule_match_enc_control(const struct flow_rule *rule,
104 struct flow_match_control *out);
105 void flow_rule_match_enc_ipv4_addrs(const struct flow_rule *rule,
106 struct flow_match_ipv4_addrs *out);
107 void flow_rule_match_enc_ipv6_addrs(const struct flow_rule *rule,
108 struct flow_match_ipv6_addrs *out);
109 void flow_rule_match_enc_ip(const struct flow_rule *rule,
110 struct flow_match_ip *out);
111 void flow_rule_match_enc_ports(const struct flow_rule *rule,
112 struct flow_match_ports *out);
113 void flow_rule_match_enc_keyid(const struct flow_rule *rule,
114 struct flow_match_enc_keyid *out);
115 void flow_rule_match_enc_opts(const struct flow_rule *rule,
116 struct flow_match_enc_opts *out);
117 void flow_rule_match_ct(const struct flow_rule *rule,
118 struct flow_match_ct *out);
120 enum flow_action_id {
121 FLOW_ACTION_ACCEPT = 0,
125 FLOW_ACTION_REDIRECT,
127 FLOW_ACTION_REDIRECT_INGRESS,
128 FLOW_ACTION_MIRRED_INGRESS,
129 FLOW_ACTION_VLAN_PUSH,
130 FLOW_ACTION_VLAN_POP,
131 FLOW_ACTION_VLAN_MANGLE,
132 FLOW_ACTION_TUNNEL_ENCAP,
133 FLOW_ACTION_TUNNEL_DECAP,
139 FLOW_ACTION_PRIORITY,
145 FLOW_ACTION_CT_METADATA,
146 FLOW_ACTION_MPLS_PUSH,
147 FLOW_ACTION_MPLS_POP,
148 FLOW_ACTION_MPLS_MANGLE,
150 FLOW_ACTION_PPPOE_PUSH,
153 FLOW_ACTION_VLAN_PUSH_ETH,
154 FLOW_ACTION_VLAN_POP_ETH,
155 FLOW_ACTION_CONTINUE,
159 /* This is mirroring enum pedit_header_type definition for easy mapping between
160 * tc pedit action. Legacy TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK is mapped to
161 * FLOW_ACT_MANGLE_UNSPEC, which is supported by no driver.
163 enum flow_action_mangle_base {
164 FLOW_ACT_MANGLE_UNSPEC = 0,
165 FLOW_ACT_MANGLE_HDR_TYPE_ETH,
166 FLOW_ACT_MANGLE_HDR_TYPE_IP4,
167 FLOW_ACT_MANGLE_HDR_TYPE_IP6,
168 FLOW_ACT_MANGLE_HDR_TYPE_TCP,
169 FLOW_ACT_MANGLE_HDR_TYPE_UDP,
172 enum flow_action_hw_stats_bit {
173 FLOW_ACTION_HW_STATS_IMMEDIATE_BIT,
174 FLOW_ACTION_HW_STATS_DELAYED_BIT,
175 FLOW_ACTION_HW_STATS_DISABLED_BIT,
177 FLOW_ACTION_HW_STATS_NUM_BITS
180 enum flow_action_hw_stats {
181 FLOW_ACTION_HW_STATS_IMMEDIATE =
182 BIT(FLOW_ACTION_HW_STATS_IMMEDIATE_BIT),
183 FLOW_ACTION_HW_STATS_DELAYED = BIT(FLOW_ACTION_HW_STATS_DELAYED_BIT),
184 FLOW_ACTION_HW_STATS_ANY = FLOW_ACTION_HW_STATS_IMMEDIATE |
185 FLOW_ACTION_HW_STATS_DELAYED,
186 FLOW_ACTION_HW_STATS_DISABLED =
187 BIT(FLOW_ACTION_HW_STATS_DISABLED_BIT),
188 FLOW_ACTION_HW_STATS_DONT_CARE = BIT(FLOW_ACTION_HW_STATS_NUM_BITS) - 1,
191 typedef void (*action_destr)(void *priv);
193 struct flow_action_cookie {
198 struct flow_action_cookie *flow_action_cookie_create(void *data,
201 void flow_action_cookie_destroy(struct flow_action_cookie *cookie);
203 struct flow_action_entry {
204 enum flow_action_id id;
206 enum flow_action_hw_stats hw_stats;
207 action_destr destructor;
208 void *destructor_priv;
210 u32 chain_index; /* FLOW_ACTION_GOTO */
211 struct net_device *dev; /* FLOW_ACTION_REDIRECT */
212 struct { /* FLOW_ACTION_VLAN */
217 struct { /* FLOW_ACTION_VLAN_PUSH_ETH */
218 unsigned char dst[ETH_ALEN];
219 unsigned char src[ETH_ALEN];
221 struct { /* FLOW_ACTION_MANGLE */
222 /* FLOW_ACTION_ADD */
223 enum flow_action_mangle_base htype;
228 struct ip_tunnel_info *tunnel; /* FLOW_ACTION_TUNNEL_ENCAP */
229 u32 csum_flags; /* FLOW_ACTION_CSUM */
230 u32 mark; /* FLOW_ACTION_MARK */
231 u16 ptype; /* FLOW_ACTION_PTYPE */
232 u32 priority; /* FLOW_ACTION_PRIORITY */
233 struct { /* FLOW_ACTION_QUEUE */
238 struct { /* FLOW_ACTION_SAMPLE */
239 struct psample_group *psample_group;
244 struct { /* FLOW_ACTION_POLICE */
247 u64 peakrate_bytes_ps;
254 enum flow_action_id act_id;
258 struct { /* FLOW_ACTION_CT */
261 struct nf_flowtable *flow_table;
264 unsigned long cookie;
269 struct { /* FLOW_ACTION_MPLS_PUSH */
276 struct { /* FLOW_ACTION_MPLS_POP */
279 struct { /* FLOW_ACTION_MPLS_MANGLE */
291 struct action_gate_entry *entries;
293 struct { /* FLOW_ACTION_PPPOE_PUSH */
297 struct flow_action_cookie *cookie; /* user defined action cookie */
301 unsigned int num_entries;
302 struct flow_action_entry entries[];
305 static inline bool flow_action_has_entries(const struct flow_action *action)
307 return action->num_entries;
311 * flow_offload_has_one_action() - check if exactly one action is present
312 * @action: tc filter flow offload action
314 * Returns true if exactly one action is present.
316 static inline bool flow_offload_has_one_action(const struct flow_action *action)
318 return action->num_entries == 1;
321 static inline bool flow_action_is_last_entry(const struct flow_action *action,
322 const struct flow_action_entry *entry)
324 return entry == &action->entries[action->num_entries - 1];
327 #define flow_action_for_each(__i, __act, __actions) \
328 for (__i = 0, __act = &(__actions)->entries[0]; \
329 __i < (__actions)->num_entries; \
330 __act = &(__actions)->entries[++__i])
333 flow_action_mixed_hw_stats_check(const struct flow_action *action,
334 struct netlink_ext_ack *extack)
336 const struct flow_action_entry *action_entry;
340 if (flow_offload_has_one_action(action))
343 flow_action_for_each(i, action_entry, action) {
344 if (i && action_entry->hw_stats != last_hw_stats) {
345 NL_SET_ERR_MSG_MOD(extack, "Mixing HW stats types for actions is not supported");
348 last_hw_stats = action_entry->hw_stats;
353 static inline const struct flow_action_entry *
354 flow_action_first_entry_get(const struct flow_action *action)
356 WARN_ON(!flow_action_has_entries(action));
357 return &action->entries[0];
361 __flow_action_hw_stats_check(const struct flow_action *action,
362 struct netlink_ext_ack *extack,
363 bool check_allow_bit,
364 enum flow_action_hw_stats_bit allow_bit)
366 const struct flow_action_entry *action_entry;
368 if (!flow_action_has_entries(action))
370 if (!flow_action_mixed_hw_stats_check(action, extack))
373 action_entry = flow_action_first_entry_get(action);
375 /* Zero is not a legal value for hw_stats, catch anyone passing it */
376 WARN_ON_ONCE(!action_entry->hw_stats);
378 if (!check_allow_bit &&
379 ~action_entry->hw_stats & FLOW_ACTION_HW_STATS_ANY) {
380 NL_SET_ERR_MSG_MOD(extack, "Driver supports only default HW stats type \"any\"");
382 } else if (check_allow_bit &&
383 !(action_entry->hw_stats & BIT(allow_bit))) {
384 NL_SET_ERR_MSG_MOD(extack, "Driver does not support selected HW stats type");
391 flow_action_hw_stats_check(const struct flow_action *action,
392 struct netlink_ext_ack *extack,
393 enum flow_action_hw_stats_bit allow_bit)
395 return __flow_action_hw_stats_check(action, extack, true, allow_bit);
399 flow_action_basic_hw_stats_check(const struct flow_action *action,
400 struct netlink_ext_ack *extack)
402 return __flow_action_hw_stats_check(action, extack, false, 0);
406 struct flow_match match;
407 struct flow_action action;
410 struct flow_rule *flow_rule_alloc(unsigned int num_actions);
412 static inline bool flow_rule_match_key(const struct flow_rule *rule,
413 enum flow_dissector_key_id key)
415 return dissector_uses_key(rule->match.dissector, key);
423 enum flow_action_hw_stats used_hw_stats;
424 bool used_hw_stats_valid;
427 static inline void flow_stats_update(struct flow_stats *flow_stats,
429 u64 drops, u64 lastused,
430 enum flow_action_hw_stats used_hw_stats)
432 flow_stats->pkts += pkts;
433 flow_stats->bytes += bytes;
434 flow_stats->drops += drops;
435 flow_stats->lastused = max_t(u64, flow_stats->lastused, lastused);
437 /* The driver should pass value with a maximum of one bit set.
438 * Passing FLOW_ACTION_HW_STATS_ANY is invalid.
440 WARN_ON(used_hw_stats == FLOW_ACTION_HW_STATS_ANY);
441 flow_stats->used_hw_stats |= used_hw_stats;
442 flow_stats->used_hw_stats_valid = true;
445 enum flow_block_command {
450 enum flow_block_binder_type {
451 FLOW_BLOCK_BINDER_TYPE_UNSPEC,
452 FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS,
453 FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS,
454 FLOW_BLOCK_BINDER_TYPE_RED_EARLY_DROP,
455 FLOW_BLOCK_BINDER_TYPE_RED_MARK,
459 struct list_head cb_list;
462 struct netlink_ext_ack;
464 struct flow_block_offload {
465 enum flow_block_command command;
466 enum flow_block_binder_type binder_type;
468 bool unlocked_driver_cb;
470 struct flow_block *block;
471 struct list_head cb_list;
472 struct list_head *driver_block_list;
473 struct netlink_ext_ack *extack;
475 struct list_head *cb_list_head;
479 typedef int flow_setup_cb_t(enum tc_setup_type type, void *type_data,
482 struct flow_block_cb;
484 struct flow_block_indr {
485 struct list_head list;
486 struct net_device *dev;
488 enum flow_block_binder_type binder_type;
491 void (*cleanup)(struct flow_block_cb *block_cb);
494 struct flow_block_cb {
495 struct list_head driver_list;
496 struct list_head list;
500 void (*release)(void *cb_priv);
501 struct flow_block_indr indr;
505 struct flow_block_cb *flow_block_cb_alloc(flow_setup_cb_t *cb,
506 void *cb_ident, void *cb_priv,
507 void (*release)(void *cb_priv));
508 struct flow_block_cb *flow_indr_block_cb_alloc(flow_setup_cb_t *cb,
509 void *cb_ident, void *cb_priv,
510 void (*release)(void *cb_priv),
511 struct flow_block_offload *bo,
512 struct net_device *dev,
513 struct Qdisc *sch, void *data,
515 void (*cleanup)(struct flow_block_cb *block_cb));
516 void flow_block_cb_free(struct flow_block_cb *block_cb);
518 struct flow_block_cb *flow_block_cb_lookup(struct flow_block *block,
519 flow_setup_cb_t *cb, void *cb_ident);
521 void *flow_block_cb_priv(struct flow_block_cb *block_cb);
522 void flow_block_cb_incref(struct flow_block_cb *block_cb);
523 unsigned int flow_block_cb_decref(struct flow_block_cb *block_cb);
525 static inline void flow_block_cb_add(struct flow_block_cb *block_cb,
526 struct flow_block_offload *offload)
528 list_add_tail(&block_cb->list, &offload->cb_list);
531 static inline void flow_block_cb_remove(struct flow_block_cb *block_cb,
532 struct flow_block_offload *offload)
534 list_move(&block_cb->list, &offload->cb_list);
537 static inline void flow_indr_block_cb_remove(struct flow_block_cb *block_cb,
538 struct flow_block_offload *offload)
540 list_del(&block_cb->indr.list);
541 list_move(&block_cb->list, &offload->cb_list);
544 bool flow_block_cb_is_busy(flow_setup_cb_t *cb, void *cb_ident,
545 struct list_head *driver_block_list);
547 int flow_block_cb_setup_simple(struct flow_block_offload *f,
548 struct list_head *driver_list,
550 void *cb_ident, void *cb_priv, bool ingress_only);
552 enum flow_cls_command {
556 FLOW_CLS_TMPLT_CREATE,
557 FLOW_CLS_TMPLT_DESTROY,
560 struct flow_cls_common_offload {
564 struct netlink_ext_ack *extack;
567 struct flow_cls_offload {
568 struct flow_cls_common_offload common;
569 enum flow_cls_command command;
570 unsigned long cookie;
571 struct flow_rule *rule;
572 struct flow_stats stats;
576 enum offload_act_command {
582 struct flow_offload_action {
583 struct netlink_ext_ack *extack; /* NULL in FLOW_ACT_STATS process*/
584 enum offload_act_command command;
585 enum flow_action_id id;
587 struct flow_stats stats;
588 struct flow_action action;
591 struct flow_offload_action *offload_action_alloc(unsigned int num_actions);
593 static inline struct flow_rule *
594 flow_cls_offload_flow_rule(struct flow_cls_offload *flow_cmd)
596 return flow_cmd->rule;
599 static inline void flow_block_init(struct flow_block *flow_block)
601 INIT_LIST_HEAD(&flow_block->cb_list);
604 typedef int flow_indr_block_bind_cb_t(struct net_device *dev, struct Qdisc *sch, void *cb_priv,
605 enum tc_setup_type type, void *type_data,
607 void (*cleanup)(struct flow_block_cb *block_cb));
609 int flow_indr_dev_register(flow_indr_block_bind_cb_t *cb, void *cb_priv);
610 void flow_indr_dev_unregister(flow_indr_block_bind_cb_t *cb, void *cb_priv,
611 void (*release)(void *cb_priv));
612 int flow_indr_dev_setup_offload(struct net_device *dev, struct Qdisc *sch,
613 enum tc_setup_type type, void *data,
614 struct flow_block_offload *bo,
615 void (*cleanup)(struct flow_block_cb *block_cb));
616 bool flow_indr_dev_exists(void);
618 #endif /* _NET_FLOW_OFFLOAD_H */