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>
8 #include <linux/rhashtable.h>
11 struct flow_dissector
*dissector
;
16 struct flow_match_meta
{
17 struct flow_dissector_key_meta
*key
, *mask
;
20 struct flow_match_basic
{
21 struct flow_dissector_key_basic
*key
, *mask
;
24 struct flow_match_control
{
25 struct flow_dissector_key_control
*key
, *mask
;
28 struct flow_match_eth_addrs
{
29 struct flow_dissector_key_eth_addrs
*key
, *mask
;
32 struct flow_match_vlan
{
33 struct flow_dissector_key_vlan
*key
, *mask
;
36 struct flow_match_ipv4_addrs
{
37 struct flow_dissector_key_ipv4_addrs
*key
, *mask
;
40 struct flow_match_ipv6_addrs
{
41 struct flow_dissector_key_ipv6_addrs
*key
, *mask
;
44 struct flow_match_ip
{
45 struct flow_dissector_key_ip
*key
, *mask
;
48 struct flow_match_ports
{
49 struct flow_dissector_key_ports
*key
, *mask
;
52 struct flow_match_icmp
{
53 struct flow_dissector_key_icmp
*key
, *mask
;
56 struct flow_match_tcp
{
57 struct flow_dissector_key_tcp
*key
, *mask
;
60 struct flow_match_mpls
{
61 struct flow_dissector_key_mpls
*key
, *mask
;
64 struct flow_match_enc_keyid
{
65 struct flow_dissector_key_keyid
*key
, *mask
;
68 struct flow_match_enc_opts
{
69 struct flow_dissector_key_enc_opts
*key
, *mask
;
72 struct flow_match_ct
{
73 struct flow_dissector_key_ct
*key
, *mask
;
78 void flow_rule_match_meta(const struct flow_rule
*rule
,
79 struct flow_match_meta
*out
);
80 void flow_rule_match_basic(const struct flow_rule
*rule
,
81 struct flow_match_basic
*out
);
82 void flow_rule_match_control(const struct flow_rule
*rule
,
83 struct flow_match_control
*out
);
84 void flow_rule_match_eth_addrs(const struct flow_rule
*rule
,
85 struct flow_match_eth_addrs
*out
);
86 void flow_rule_match_vlan(const struct flow_rule
*rule
,
87 struct flow_match_vlan
*out
);
88 void flow_rule_match_cvlan(const struct flow_rule
*rule
,
89 struct flow_match_vlan
*out
);
90 void flow_rule_match_ipv4_addrs(const struct flow_rule
*rule
,
91 struct flow_match_ipv4_addrs
*out
);
92 void flow_rule_match_ipv6_addrs(const struct flow_rule
*rule
,
93 struct flow_match_ipv6_addrs
*out
);
94 void flow_rule_match_ip(const struct flow_rule
*rule
,
95 struct flow_match_ip
*out
);
96 void flow_rule_match_ports(const struct flow_rule
*rule
,
97 struct flow_match_ports
*out
);
98 void flow_rule_match_tcp(const struct flow_rule
*rule
,
99 struct flow_match_tcp
*out
);
100 void flow_rule_match_icmp(const struct flow_rule
*rule
,
101 struct flow_match_icmp
*out
);
102 void flow_rule_match_mpls(const struct flow_rule
*rule
,
103 struct flow_match_mpls
*out
);
104 void flow_rule_match_enc_control(const struct flow_rule
*rule
,
105 struct flow_match_control
*out
);
106 void flow_rule_match_enc_ipv4_addrs(const struct flow_rule
*rule
,
107 struct flow_match_ipv4_addrs
*out
);
108 void flow_rule_match_enc_ipv6_addrs(const struct flow_rule
*rule
,
109 struct flow_match_ipv6_addrs
*out
);
110 void flow_rule_match_enc_ip(const struct flow_rule
*rule
,
111 struct flow_match_ip
*out
);
112 void flow_rule_match_enc_ports(const struct flow_rule
*rule
,
113 struct flow_match_ports
*out
);
114 void flow_rule_match_enc_keyid(const struct flow_rule
*rule
,
115 struct flow_match_enc_keyid
*out
);
116 void flow_rule_match_enc_opts(const struct flow_rule
*rule
,
117 struct flow_match_enc_opts
*out
);
118 void flow_rule_match_ct(const struct flow_rule
*rule
,
119 struct flow_match_ct
*out
);
121 enum flow_action_id
{
122 FLOW_ACTION_ACCEPT
= 0,
126 FLOW_ACTION_REDIRECT
,
128 FLOW_ACTION_REDIRECT_INGRESS
,
129 FLOW_ACTION_MIRRED_INGRESS
,
130 FLOW_ACTION_VLAN_PUSH
,
131 FLOW_ACTION_VLAN_POP
,
132 FLOW_ACTION_VLAN_MANGLE
,
133 FLOW_ACTION_TUNNEL_ENCAP
,
134 FLOW_ACTION_TUNNEL_DECAP
,
140 FLOW_ACTION_PRIORITY
,
146 FLOW_ACTION_CT_METADATA
,
147 FLOW_ACTION_MPLS_PUSH
,
148 FLOW_ACTION_MPLS_POP
,
149 FLOW_ACTION_MPLS_MANGLE
,
153 /* This is mirroring enum pedit_header_type definition for easy mapping between
154 * tc pedit action. Legacy TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK is mapped to
155 * FLOW_ACT_MANGLE_UNSPEC, which is supported by no driver.
157 enum flow_action_mangle_base
{
158 FLOW_ACT_MANGLE_UNSPEC
= 0,
159 FLOW_ACT_MANGLE_HDR_TYPE_ETH
,
160 FLOW_ACT_MANGLE_HDR_TYPE_IP4
,
161 FLOW_ACT_MANGLE_HDR_TYPE_IP6
,
162 FLOW_ACT_MANGLE_HDR_TYPE_TCP
,
163 FLOW_ACT_MANGLE_HDR_TYPE_UDP
,
166 enum flow_action_hw_stats_bit
{
167 FLOW_ACTION_HW_STATS_IMMEDIATE_BIT
,
168 FLOW_ACTION_HW_STATS_DELAYED_BIT
,
171 enum flow_action_hw_stats
{
172 FLOW_ACTION_HW_STATS_DISABLED
= 0,
173 FLOW_ACTION_HW_STATS_IMMEDIATE
=
174 BIT(FLOW_ACTION_HW_STATS_IMMEDIATE_BIT
),
175 FLOW_ACTION_HW_STATS_DELAYED
= BIT(FLOW_ACTION_HW_STATS_DELAYED_BIT
),
176 FLOW_ACTION_HW_STATS_ANY
= FLOW_ACTION_HW_STATS_IMMEDIATE
|
177 FLOW_ACTION_HW_STATS_DELAYED
,
180 typedef void (*action_destr
)(void *priv
);
182 struct flow_action_cookie
{
187 struct flow_action_cookie
*flow_action_cookie_create(void *data
,
190 void flow_action_cookie_destroy(struct flow_action_cookie
*cookie
);
192 struct flow_action_entry
{
193 enum flow_action_id id
;
194 enum flow_action_hw_stats hw_stats
;
195 action_destr destructor
;
196 void *destructor_priv
;
198 u32 chain_index
; /* FLOW_ACTION_GOTO */
199 struct net_device
*dev
; /* FLOW_ACTION_REDIRECT */
200 struct { /* FLOW_ACTION_VLAN */
205 struct { /* FLOW_ACTION_MANGLE */
206 /* FLOW_ACTION_ADD */
207 enum flow_action_mangle_base htype
;
212 struct ip_tunnel_info
*tunnel
; /* FLOW_ACTION_TUNNEL_ENCAP */
213 u32 csum_flags
; /* FLOW_ACTION_CSUM */
214 u32 mark
; /* FLOW_ACTION_MARK */
215 u16 ptype
; /* FLOW_ACTION_PTYPE */
216 u32 priority
; /* FLOW_ACTION_PRIORITY */
217 struct { /* FLOW_ACTION_QUEUE */
222 struct { /* FLOW_ACTION_SAMPLE */
223 struct psample_group
*psample_group
;
228 struct { /* FLOW_ACTION_POLICE */
232 struct { /* FLOW_ACTION_CT */
235 struct nf_flowtable
*flow_table
;
238 unsigned long cookie
;
242 struct { /* FLOW_ACTION_MPLS_PUSH */
249 struct { /* FLOW_ACTION_MPLS_POP */
252 struct { /* FLOW_ACTION_MPLS_MANGLE */
259 struct flow_action_cookie
*cookie
; /* user defined action cookie */
263 unsigned int num_entries
;
264 struct flow_action_entry entries
[];
267 static inline bool flow_action_has_entries(const struct flow_action
*action
)
269 return action
->num_entries
;
273 * flow_action_has_one_action() - check if exactly one action is present
274 * @action: tc filter flow offload action
276 * Returns true if exactly one action is present.
278 static inline bool flow_offload_has_one_action(const struct flow_action
*action
)
280 return action
->num_entries
== 1;
283 #define flow_action_for_each(__i, __act, __actions) \
284 for (__i = 0, __act = &(__actions)->entries[0]; \
285 __i < (__actions)->num_entries; \
286 __act = &(__actions)->entries[++__i])
289 flow_action_mixed_hw_stats_check(const struct flow_action
*action
,
290 struct netlink_ext_ack
*extack
)
292 const struct flow_action_entry
*action_entry
;
293 u8
uninitialized_var(last_hw_stats
);
296 if (flow_offload_has_one_action(action
))
299 flow_action_for_each(i
, action_entry
, action
) {
300 if (i
&& action_entry
->hw_stats
!= last_hw_stats
) {
301 NL_SET_ERR_MSG_MOD(extack
, "Mixing HW stats types for actions is not supported");
304 last_hw_stats
= action_entry
->hw_stats
;
309 static inline const struct flow_action_entry
*
310 flow_action_first_entry_get(const struct flow_action
*action
)
312 WARN_ON(!flow_action_has_entries(action
));
313 return &action
->entries
[0];
317 __flow_action_hw_stats_check(const struct flow_action
*action
,
318 struct netlink_ext_ack
*extack
,
319 bool check_allow_bit
,
320 enum flow_action_hw_stats_bit allow_bit
)
322 const struct flow_action_entry
*action_entry
;
324 if (!flow_action_has_entries(action
))
326 if (!flow_action_mixed_hw_stats_check(action
, extack
))
328 action_entry
= flow_action_first_entry_get(action
);
329 if (!check_allow_bit
&&
330 action_entry
->hw_stats
!= FLOW_ACTION_HW_STATS_ANY
) {
331 NL_SET_ERR_MSG_MOD(extack
, "Driver supports only default HW stats type \"any\"");
333 } else if (check_allow_bit
&&
334 !(action_entry
->hw_stats
& BIT(allow_bit
))) {
335 NL_SET_ERR_MSG_MOD(extack
, "Driver does not support selected HW stats type");
342 flow_action_hw_stats_check(const struct flow_action
*action
,
343 struct netlink_ext_ack
*extack
,
344 enum flow_action_hw_stats_bit allow_bit
)
346 return __flow_action_hw_stats_check(action
, extack
, true, allow_bit
);
350 flow_action_basic_hw_stats_check(const struct flow_action
*action
,
351 struct netlink_ext_ack
*extack
)
353 return __flow_action_hw_stats_check(action
, extack
, false, 0);
357 struct flow_match match
;
358 struct flow_action action
;
361 struct flow_rule
*flow_rule_alloc(unsigned int num_actions
);
363 static inline bool flow_rule_match_key(const struct flow_rule
*rule
,
364 enum flow_dissector_key_id key
)
366 return dissector_uses_key(rule
->match
.dissector
, key
);
373 enum flow_action_hw_stats used_hw_stats
;
374 bool used_hw_stats_valid
;
377 static inline void flow_stats_update(struct flow_stats
*flow_stats
,
378 u64 bytes
, u64 pkts
, u64 lastused
,
379 enum flow_action_hw_stats used_hw_stats
)
381 flow_stats
->pkts
+= pkts
;
382 flow_stats
->bytes
+= bytes
;
383 flow_stats
->lastused
= max_t(u64
, flow_stats
->lastused
, lastused
);
385 /* The driver should pass value with a maximum of one bit set.
386 * Passing FLOW_ACTION_HW_STATS_ANY is invalid.
388 WARN_ON(used_hw_stats
== FLOW_ACTION_HW_STATS_ANY
);
389 flow_stats
->used_hw_stats
|= used_hw_stats
;
390 flow_stats
->used_hw_stats_valid
= true;
393 enum flow_block_command
{
398 enum flow_block_binder_type
{
399 FLOW_BLOCK_BINDER_TYPE_UNSPEC
,
400 FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS
,
401 FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS
,
405 struct list_head cb_list
;
408 struct netlink_ext_ack
;
410 struct flow_block_offload
{
411 enum flow_block_command command
;
412 enum flow_block_binder_type binder_type
;
414 bool unlocked_driver_cb
;
416 struct flow_block
*block
;
417 struct list_head cb_list
;
418 struct list_head
*driver_block_list
;
419 struct netlink_ext_ack
*extack
;
423 typedef int flow_setup_cb_t(enum tc_setup_type type
, void *type_data
,
426 struct flow_block_cb
{
427 struct list_head driver_list
;
428 struct list_head list
;
432 void (*release
)(void *cb_priv
);
436 struct flow_block_cb
*flow_block_cb_alloc(flow_setup_cb_t
*cb
,
437 void *cb_ident
, void *cb_priv
,
438 void (*release
)(void *cb_priv
));
439 void flow_block_cb_free(struct flow_block_cb
*block_cb
);
441 struct flow_block_cb
*flow_block_cb_lookup(struct flow_block
*block
,
442 flow_setup_cb_t
*cb
, void *cb_ident
);
444 void *flow_block_cb_priv(struct flow_block_cb
*block_cb
);
445 void flow_block_cb_incref(struct flow_block_cb
*block_cb
);
446 unsigned int flow_block_cb_decref(struct flow_block_cb
*block_cb
);
448 static inline void flow_block_cb_add(struct flow_block_cb
*block_cb
,
449 struct flow_block_offload
*offload
)
451 list_add_tail(&block_cb
->list
, &offload
->cb_list
);
454 static inline void flow_block_cb_remove(struct flow_block_cb
*block_cb
,
455 struct flow_block_offload
*offload
)
457 list_move(&block_cb
->list
, &offload
->cb_list
);
460 bool flow_block_cb_is_busy(flow_setup_cb_t
*cb
, void *cb_ident
,
461 struct list_head
*driver_block_list
);
463 int flow_block_cb_setup_simple(struct flow_block_offload
*f
,
464 struct list_head
*driver_list
,
466 void *cb_ident
, void *cb_priv
, bool ingress_only
);
468 enum flow_cls_command
{
472 FLOW_CLS_TMPLT_CREATE
,
473 FLOW_CLS_TMPLT_DESTROY
,
476 struct flow_cls_common_offload
{
480 struct netlink_ext_ack
*extack
;
483 struct flow_cls_offload
{
484 struct flow_cls_common_offload common
;
485 enum flow_cls_command command
;
486 unsigned long cookie
;
487 struct flow_rule
*rule
;
488 struct flow_stats stats
;
492 static inline struct flow_rule
*
493 flow_cls_offload_flow_rule(struct flow_cls_offload
*flow_cmd
)
495 return flow_cmd
->rule
;
498 static inline void flow_block_init(struct flow_block
*flow_block
)
500 INIT_LIST_HEAD(&flow_block
->cb_list
);
503 typedef int flow_indr_block_bind_cb_t(struct net_device
*dev
, void *cb_priv
,
504 enum tc_setup_type type
, void *type_data
);
506 typedef void flow_indr_block_cmd_t(struct net_device
*dev
,
507 flow_indr_block_bind_cb_t
*cb
, void *cb_priv
,
508 enum flow_block_command command
);
510 struct flow_indr_block_entry
{
511 flow_indr_block_cmd_t
*cb
;
512 struct list_head list
;
515 void flow_indr_add_block_cb(struct flow_indr_block_entry
*entry
);
517 void flow_indr_del_block_cb(struct flow_indr_block_entry
*entry
);
519 int __flow_indr_block_cb_register(struct net_device
*dev
, void *cb_priv
,
520 flow_indr_block_bind_cb_t
*cb
,
523 void __flow_indr_block_cb_unregister(struct net_device
*dev
,
524 flow_indr_block_bind_cb_t
*cb
,
527 int flow_indr_block_cb_register(struct net_device
*dev
, void *cb_priv
,
528 flow_indr_block_bind_cb_t
*cb
, void *cb_ident
);
530 void flow_indr_block_cb_unregister(struct net_device
*dev
,
531 flow_indr_block_bind_cb_t
*cb
,
534 void flow_indr_block_call(struct net_device
*dev
,
535 struct flow_block_offload
*bo
,
536 enum flow_block_command command
,
537 enum tc_setup_type type
);
539 #endif /* _NET_FLOW_OFFLOAD_H */