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 unsigned long cookie;
233 enum flow_action_hw_stats hw_stats;
234 action_destr destructor;
235 void *destructor_priv;
237 u32 chain_index; /* FLOW_ACTION_GOTO */
238 struct net_device *dev; /* FLOW_ACTION_REDIRECT */
239 struct { /* FLOW_ACTION_VLAN */
244 struct { /* FLOW_ACTION_VLAN_PUSH_ETH */
245 unsigned char dst[ETH_ALEN];
246 unsigned char src[ETH_ALEN];
248 struct { /* FLOW_ACTION_MANGLE */
249 /* FLOW_ACTION_ADD */
250 enum flow_action_mangle_base htype;
255 struct ip_tunnel_info *tunnel; /* FLOW_ACTION_TUNNEL_ENCAP */
256 u32 csum_flags; /* FLOW_ACTION_CSUM */
257 u32 mark; /* FLOW_ACTION_MARK */
258 u16 ptype; /* FLOW_ACTION_PTYPE */
259 u16 rx_queue; /* FLOW_ACTION_RX_QUEUE_MAPPING */
260 u32 priority; /* FLOW_ACTION_PRIORITY */
261 struct { /* FLOW_ACTION_QUEUE */
266 struct { /* FLOW_ACTION_SAMPLE */
267 struct psample_group *psample_group;
272 struct { /* FLOW_ACTION_POLICE */
275 u64 peakrate_bytes_ps;
282 enum flow_action_id act_id;
286 struct { /* FLOW_ACTION_CT */
289 struct nf_flowtable *flow_table;
292 unsigned long cookie;
297 struct { /* FLOW_ACTION_MPLS_PUSH */
304 struct { /* FLOW_ACTION_MPLS_POP */
307 struct { /* FLOW_ACTION_MPLS_MANGLE */
319 struct action_gate_entry *entries;
321 struct { /* FLOW_ACTION_PPPOE_PUSH */
325 struct flow_action_cookie *user_cookie; /* user defined action cookie */
329 unsigned int num_entries;
330 struct flow_action_entry entries[];
333 static inline bool flow_action_has_entries(const struct flow_action *action)
335 return action->num_entries;
339 * flow_offload_has_one_action() - check if exactly one action is present
340 * @action: tc filter flow offload action
342 * Returns true if exactly one action is present.
344 static inline bool flow_offload_has_one_action(const struct flow_action *action)
346 return action->num_entries == 1;
349 static inline bool flow_action_is_last_entry(const struct flow_action *action,
350 const struct flow_action_entry *entry)
352 return entry == &action->entries[action->num_entries - 1];
355 #define flow_action_for_each(__i, __act, __actions) \
356 for (__i = 0, __act = &(__actions)->entries[0]; \
357 __i < (__actions)->num_entries; \
358 __act = &(__actions)->entries[++__i])
361 flow_action_mixed_hw_stats_check(const struct flow_action *action,
362 struct netlink_ext_ack *extack)
364 const struct flow_action_entry *action_entry;
368 if (flow_offload_has_one_action(action))
371 flow_action_for_each(i, action_entry, action) {
372 if (i && action_entry->hw_stats != last_hw_stats) {
373 NL_SET_ERR_MSG_MOD(extack, "Mixing HW stats types for actions is not supported");
376 last_hw_stats = action_entry->hw_stats;
381 static inline const struct flow_action_entry *
382 flow_action_first_entry_get(const struct flow_action *action)
384 WARN_ON(!flow_action_has_entries(action));
385 return &action->entries[0];
389 __flow_action_hw_stats_check(const struct flow_action *action,
390 struct netlink_ext_ack *extack,
391 bool check_allow_bit,
392 enum flow_action_hw_stats_bit allow_bit)
394 const struct flow_action_entry *action_entry;
396 if (!flow_action_has_entries(action))
398 if (!flow_action_mixed_hw_stats_check(action, extack))
401 action_entry = flow_action_first_entry_get(action);
403 /* Zero is not a legal value for hw_stats, catch anyone passing it */
404 WARN_ON_ONCE(!action_entry->hw_stats);
406 if (!check_allow_bit &&
407 ~action_entry->hw_stats & FLOW_ACTION_HW_STATS_ANY) {
408 NL_SET_ERR_MSG_MOD(extack, "Driver supports only default HW stats type \"any\"");
410 } else if (check_allow_bit &&
411 !(action_entry->hw_stats & BIT(allow_bit))) {
412 NL_SET_ERR_MSG_MOD(extack, "Driver does not support selected HW stats type");
419 flow_action_hw_stats_check(const struct flow_action *action,
420 struct netlink_ext_ack *extack,
421 enum flow_action_hw_stats_bit allow_bit)
423 return __flow_action_hw_stats_check(action, extack, true, allow_bit);
427 flow_action_basic_hw_stats_check(const struct flow_action *action,
428 struct netlink_ext_ack *extack)
430 return __flow_action_hw_stats_check(action, extack, false, 0);
434 struct flow_match match;
435 struct flow_action action;
438 struct flow_rule *flow_rule_alloc(unsigned int num_actions);
440 static inline bool flow_rule_match_key(const struct flow_rule *rule,
441 enum flow_dissector_key_id key)
443 return dissector_uses_key(rule->match.dissector, key);
451 enum flow_action_hw_stats used_hw_stats;
452 bool used_hw_stats_valid;
455 static inline void flow_stats_update(struct flow_stats *flow_stats,
457 u64 drops, u64 lastused,
458 enum flow_action_hw_stats used_hw_stats)
460 flow_stats->pkts += pkts;
461 flow_stats->bytes += bytes;
462 flow_stats->drops += drops;
463 flow_stats->lastused = max_t(u64, flow_stats->lastused, lastused);
465 /* The driver should pass value with a maximum of one bit set.
466 * Passing FLOW_ACTION_HW_STATS_ANY is invalid.
468 WARN_ON(used_hw_stats == FLOW_ACTION_HW_STATS_ANY);
469 flow_stats->used_hw_stats |= used_hw_stats;
470 flow_stats->used_hw_stats_valid = true;
473 enum flow_block_command {
478 enum flow_block_binder_type {
479 FLOW_BLOCK_BINDER_TYPE_UNSPEC,
480 FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS,
481 FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS,
482 FLOW_BLOCK_BINDER_TYPE_RED_EARLY_DROP,
483 FLOW_BLOCK_BINDER_TYPE_RED_MARK,
487 struct list_head cb_list;
490 struct netlink_ext_ack;
492 struct flow_block_offload {
493 enum flow_block_command command;
494 enum flow_block_binder_type binder_type;
496 bool unlocked_driver_cb;
498 struct flow_block *block;
499 struct list_head cb_list;
500 struct list_head *driver_block_list;
501 struct netlink_ext_ack *extack;
503 struct list_head *cb_list_head;
507 typedef int flow_setup_cb_t(enum tc_setup_type type, void *type_data,
510 struct flow_block_cb;
512 struct flow_block_indr {
513 struct list_head list;
514 struct net_device *dev;
516 enum flow_block_binder_type binder_type;
519 void (*cleanup)(struct flow_block_cb *block_cb);
522 struct flow_block_cb {
523 struct list_head driver_list;
524 struct list_head list;
528 void (*release)(void *cb_priv);
529 struct flow_block_indr indr;
533 struct flow_block_cb *flow_block_cb_alloc(flow_setup_cb_t *cb,
534 void *cb_ident, void *cb_priv,
535 void (*release)(void *cb_priv));
536 struct flow_block_cb *flow_indr_block_cb_alloc(flow_setup_cb_t *cb,
537 void *cb_ident, void *cb_priv,
538 void (*release)(void *cb_priv),
539 struct flow_block_offload *bo,
540 struct net_device *dev,
541 struct Qdisc *sch, void *data,
543 void (*cleanup)(struct flow_block_cb *block_cb));
544 void flow_block_cb_free(struct flow_block_cb *block_cb);
546 struct flow_block_cb *flow_block_cb_lookup(struct flow_block *block,
547 flow_setup_cb_t *cb, void *cb_ident);
549 void *flow_block_cb_priv(struct flow_block_cb *block_cb);
550 void flow_block_cb_incref(struct flow_block_cb *block_cb);
551 unsigned int flow_block_cb_decref(struct flow_block_cb *block_cb);
553 static inline void flow_block_cb_add(struct flow_block_cb *block_cb,
554 struct flow_block_offload *offload)
556 list_add_tail(&block_cb->list, &offload->cb_list);
559 static inline void flow_block_cb_remove(struct flow_block_cb *block_cb,
560 struct flow_block_offload *offload)
562 list_move(&block_cb->list, &offload->cb_list);
565 static inline void flow_indr_block_cb_remove(struct flow_block_cb *block_cb,
566 struct flow_block_offload *offload)
568 list_del(&block_cb->indr.list);
569 list_move(&block_cb->list, &offload->cb_list);
572 bool flow_block_cb_is_busy(flow_setup_cb_t *cb, void *cb_ident,
573 struct list_head *driver_block_list);
575 int flow_block_cb_setup_simple(struct flow_block_offload *f,
576 struct list_head *driver_list,
578 void *cb_ident, void *cb_priv, bool ingress_only);
580 enum flow_cls_command {
584 FLOW_CLS_TMPLT_CREATE,
585 FLOW_CLS_TMPLT_DESTROY,
588 struct flow_cls_common_offload {
592 struct netlink_ext_ack *extack;
595 struct flow_cls_offload {
596 struct flow_cls_common_offload common;
597 enum flow_cls_command command;
599 unsigned long cookie;
600 struct flow_rule *rule;
601 struct flow_stats stats;
605 enum offload_act_command {
611 struct flow_offload_action {
612 struct netlink_ext_ack *extack; /* NULL in FLOW_ACT_STATS process*/
613 enum offload_act_command command;
614 enum flow_action_id id;
616 unsigned long cookie;
617 struct flow_stats stats;
618 struct flow_action action;
621 struct flow_offload_action *offload_action_alloc(unsigned int num_actions);
623 static inline struct flow_rule *
624 flow_cls_offload_flow_rule(struct flow_cls_offload *flow_cmd)
626 return flow_cmd->rule;
629 static inline void flow_block_init(struct flow_block *flow_block)
631 INIT_LIST_HEAD(&flow_block->cb_list);
634 typedef int flow_indr_block_bind_cb_t(struct net_device *dev, struct Qdisc *sch, void *cb_priv,
635 enum tc_setup_type type, void *type_data,
637 void (*cleanup)(struct flow_block_cb *block_cb));
639 int flow_indr_dev_register(flow_indr_block_bind_cb_t *cb, void *cb_priv);
640 void flow_indr_dev_unregister(flow_indr_block_bind_cb_t *cb, void *cb_priv,
641 void (*release)(void *cb_priv));
642 int flow_indr_dev_setup_offload(struct net_device *dev, struct Qdisc *sch,
643 enum tc_setup_type type, void *data,
644 struct flow_block_offload *bo,
645 void (*cleanup)(struct flow_block_cb *block_cb));
646 bool flow_indr_dev_exists(void);
648 #endif /* _NET_FLOW_OFFLOAD_H */