1*4882a593Smuzhiyun #ifndef _NET_FLOW_OFFLOAD_H
2*4882a593Smuzhiyun #define _NET_FLOW_OFFLOAD_H
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun #include <linux/kernel.h>
5*4882a593Smuzhiyun #include <linux/list.h>
6*4882a593Smuzhiyun #include <linux/netlink.h>
7*4882a593Smuzhiyun #include <net/flow_dissector.h>
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun struct flow_match {
10*4882a593Smuzhiyun struct flow_dissector *dissector;
11*4882a593Smuzhiyun void *mask;
12*4882a593Smuzhiyun void *key;
13*4882a593Smuzhiyun };
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun struct flow_match_meta {
16*4882a593Smuzhiyun struct flow_dissector_key_meta *key, *mask;
17*4882a593Smuzhiyun };
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun struct flow_match_basic {
20*4882a593Smuzhiyun struct flow_dissector_key_basic *key, *mask;
21*4882a593Smuzhiyun };
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun struct flow_match_control {
24*4882a593Smuzhiyun struct flow_dissector_key_control *key, *mask;
25*4882a593Smuzhiyun };
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun struct flow_match_eth_addrs {
28*4882a593Smuzhiyun struct flow_dissector_key_eth_addrs *key, *mask;
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun struct flow_match_vlan {
32*4882a593Smuzhiyun struct flow_dissector_key_vlan *key, *mask;
33*4882a593Smuzhiyun };
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun struct flow_match_ipv4_addrs {
36*4882a593Smuzhiyun struct flow_dissector_key_ipv4_addrs *key, *mask;
37*4882a593Smuzhiyun };
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun struct flow_match_ipv6_addrs {
40*4882a593Smuzhiyun struct flow_dissector_key_ipv6_addrs *key, *mask;
41*4882a593Smuzhiyun };
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun struct flow_match_ip {
44*4882a593Smuzhiyun struct flow_dissector_key_ip *key, *mask;
45*4882a593Smuzhiyun };
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun struct flow_match_ports {
48*4882a593Smuzhiyun struct flow_dissector_key_ports *key, *mask;
49*4882a593Smuzhiyun };
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun struct flow_match_icmp {
52*4882a593Smuzhiyun struct flow_dissector_key_icmp *key, *mask;
53*4882a593Smuzhiyun };
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun struct flow_match_tcp {
56*4882a593Smuzhiyun struct flow_dissector_key_tcp *key, *mask;
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun struct flow_match_mpls {
60*4882a593Smuzhiyun struct flow_dissector_key_mpls *key, *mask;
61*4882a593Smuzhiyun };
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun struct flow_match_enc_keyid {
64*4882a593Smuzhiyun struct flow_dissector_key_keyid *key, *mask;
65*4882a593Smuzhiyun };
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun struct flow_match_enc_opts {
68*4882a593Smuzhiyun struct flow_dissector_key_enc_opts *key, *mask;
69*4882a593Smuzhiyun };
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun struct flow_match_ct {
72*4882a593Smuzhiyun struct flow_dissector_key_ct *key, *mask;
73*4882a593Smuzhiyun };
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun struct flow_rule;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun void flow_rule_match_meta(const struct flow_rule *rule,
78*4882a593Smuzhiyun struct flow_match_meta *out);
79*4882a593Smuzhiyun void flow_rule_match_basic(const struct flow_rule *rule,
80*4882a593Smuzhiyun struct flow_match_basic *out);
81*4882a593Smuzhiyun void flow_rule_match_control(const struct flow_rule *rule,
82*4882a593Smuzhiyun struct flow_match_control *out);
83*4882a593Smuzhiyun void flow_rule_match_eth_addrs(const struct flow_rule *rule,
84*4882a593Smuzhiyun struct flow_match_eth_addrs *out);
85*4882a593Smuzhiyun void flow_rule_match_vlan(const struct flow_rule *rule,
86*4882a593Smuzhiyun struct flow_match_vlan *out);
87*4882a593Smuzhiyun void flow_rule_match_cvlan(const struct flow_rule *rule,
88*4882a593Smuzhiyun struct flow_match_vlan *out);
89*4882a593Smuzhiyun void flow_rule_match_ipv4_addrs(const struct flow_rule *rule,
90*4882a593Smuzhiyun struct flow_match_ipv4_addrs *out);
91*4882a593Smuzhiyun void flow_rule_match_ipv6_addrs(const struct flow_rule *rule,
92*4882a593Smuzhiyun struct flow_match_ipv6_addrs *out);
93*4882a593Smuzhiyun void flow_rule_match_ip(const struct flow_rule *rule,
94*4882a593Smuzhiyun struct flow_match_ip *out);
95*4882a593Smuzhiyun void flow_rule_match_ports(const struct flow_rule *rule,
96*4882a593Smuzhiyun struct flow_match_ports *out);
97*4882a593Smuzhiyun void flow_rule_match_tcp(const struct flow_rule *rule,
98*4882a593Smuzhiyun struct flow_match_tcp *out);
99*4882a593Smuzhiyun void flow_rule_match_icmp(const struct flow_rule *rule,
100*4882a593Smuzhiyun struct flow_match_icmp *out);
101*4882a593Smuzhiyun void flow_rule_match_mpls(const struct flow_rule *rule,
102*4882a593Smuzhiyun struct flow_match_mpls *out);
103*4882a593Smuzhiyun void flow_rule_match_enc_control(const struct flow_rule *rule,
104*4882a593Smuzhiyun struct flow_match_control *out);
105*4882a593Smuzhiyun void flow_rule_match_enc_ipv4_addrs(const struct flow_rule *rule,
106*4882a593Smuzhiyun struct flow_match_ipv4_addrs *out);
107*4882a593Smuzhiyun void flow_rule_match_enc_ipv6_addrs(const struct flow_rule *rule,
108*4882a593Smuzhiyun struct flow_match_ipv6_addrs *out);
109*4882a593Smuzhiyun void flow_rule_match_enc_ip(const struct flow_rule *rule,
110*4882a593Smuzhiyun struct flow_match_ip *out);
111*4882a593Smuzhiyun void flow_rule_match_enc_ports(const struct flow_rule *rule,
112*4882a593Smuzhiyun struct flow_match_ports *out);
113*4882a593Smuzhiyun void flow_rule_match_enc_keyid(const struct flow_rule *rule,
114*4882a593Smuzhiyun struct flow_match_enc_keyid *out);
115*4882a593Smuzhiyun void flow_rule_match_enc_opts(const struct flow_rule *rule,
116*4882a593Smuzhiyun struct flow_match_enc_opts *out);
117*4882a593Smuzhiyun void flow_rule_match_ct(const struct flow_rule *rule,
118*4882a593Smuzhiyun struct flow_match_ct *out);
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun enum flow_action_id {
121*4882a593Smuzhiyun FLOW_ACTION_ACCEPT = 0,
122*4882a593Smuzhiyun FLOW_ACTION_DROP,
123*4882a593Smuzhiyun FLOW_ACTION_TRAP,
124*4882a593Smuzhiyun FLOW_ACTION_GOTO,
125*4882a593Smuzhiyun FLOW_ACTION_REDIRECT,
126*4882a593Smuzhiyun FLOW_ACTION_MIRRED,
127*4882a593Smuzhiyun FLOW_ACTION_REDIRECT_INGRESS,
128*4882a593Smuzhiyun FLOW_ACTION_MIRRED_INGRESS,
129*4882a593Smuzhiyun FLOW_ACTION_VLAN_PUSH,
130*4882a593Smuzhiyun FLOW_ACTION_VLAN_POP,
131*4882a593Smuzhiyun FLOW_ACTION_VLAN_MANGLE,
132*4882a593Smuzhiyun FLOW_ACTION_TUNNEL_ENCAP,
133*4882a593Smuzhiyun FLOW_ACTION_TUNNEL_DECAP,
134*4882a593Smuzhiyun FLOW_ACTION_MANGLE,
135*4882a593Smuzhiyun FLOW_ACTION_ADD,
136*4882a593Smuzhiyun FLOW_ACTION_CSUM,
137*4882a593Smuzhiyun FLOW_ACTION_MARK,
138*4882a593Smuzhiyun FLOW_ACTION_PTYPE,
139*4882a593Smuzhiyun FLOW_ACTION_PRIORITY,
140*4882a593Smuzhiyun FLOW_ACTION_WAKE,
141*4882a593Smuzhiyun FLOW_ACTION_QUEUE,
142*4882a593Smuzhiyun FLOW_ACTION_SAMPLE,
143*4882a593Smuzhiyun FLOW_ACTION_POLICE,
144*4882a593Smuzhiyun FLOW_ACTION_CT,
145*4882a593Smuzhiyun FLOW_ACTION_CT_METADATA,
146*4882a593Smuzhiyun FLOW_ACTION_MPLS_PUSH,
147*4882a593Smuzhiyun FLOW_ACTION_MPLS_POP,
148*4882a593Smuzhiyun FLOW_ACTION_MPLS_MANGLE,
149*4882a593Smuzhiyun FLOW_ACTION_GATE,
150*4882a593Smuzhiyun NUM_FLOW_ACTIONS,
151*4882a593Smuzhiyun };
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun /* This is mirroring enum pedit_header_type definition for easy mapping between
154*4882a593Smuzhiyun * tc pedit action. Legacy TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK is mapped to
155*4882a593Smuzhiyun * FLOW_ACT_MANGLE_UNSPEC, which is supported by no driver.
156*4882a593Smuzhiyun */
157*4882a593Smuzhiyun enum flow_action_mangle_base {
158*4882a593Smuzhiyun FLOW_ACT_MANGLE_UNSPEC = 0,
159*4882a593Smuzhiyun FLOW_ACT_MANGLE_HDR_TYPE_ETH,
160*4882a593Smuzhiyun FLOW_ACT_MANGLE_HDR_TYPE_IP4,
161*4882a593Smuzhiyun FLOW_ACT_MANGLE_HDR_TYPE_IP6,
162*4882a593Smuzhiyun FLOW_ACT_MANGLE_HDR_TYPE_TCP,
163*4882a593Smuzhiyun FLOW_ACT_MANGLE_HDR_TYPE_UDP,
164*4882a593Smuzhiyun };
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun enum flow_action_hw_stats_bit {
167*4882a593Smuzhiyun FLOW_ACTION_HW_STATS_IMMEDIATE_BIT,
168*4882a593Smuzhiyun FLOW_ACTION_HW_STATS_DELAYED_BIT,
169*4882a593Smuzhiyun FLOW_ACTION_HW_STATS_DISABLED_BIT,
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun FLOW_ACTION_HW_STATS_NUM_BITS
172*4882a593Smuzhiyun };
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun enum flow_action_hw_stats {
175*4882a593Smuzhiyun FLOW_ACTION_HW_STATS_IMMEDIATE =
176*4882a593Smuzhiyun BIT(FLOW_ACTION_HW_STATS_IMMEDIATE_BIT),
177*4882a593Smuzhiyun FLOW_ACTION_HW_STATS_DELAYED = BIT(FLOW_ACTION_HW_STATS_DELAYED_BIT),
178*4882a593Smuzhiyun FLOW_ACTION_HW_STATS_ANY = FLOW_ACTION_HW_STATS_IMMEDIATE |
179*4882a593Smuzhiyun FLOW_ACTION_HW_STATS_DELAYED,
180*4882a593Smuzhiyun FLOW_ACTION_HW_STATS_DISABLED =
181*4882a593Smuzhiyun BIT(FLOW_ACTION_HW_STATS_DISABLED_BIT),
182*4882a593Smuzhiyun FLOW_ACTION_HW_STATS_DONT_CARE = BIT(FLOW_ACTION_HW_STATS_NUM_BITS) - 1,
183*4882a593Smuzhiyun };
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun typedef void (*action_destr)(void *priv);
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun struct flow_action_cookie {
188*4882a593Smuzhiyun u32 cookie_len;
189*4882a593Smuzhiyun u8 cookie[];
190*4882a593Smuzhiyun };
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun struct flow_action_cookie *flow_action_cookie_create(void *data,
193*4882a593Smuzhiyun unsigned int len,
194*4882a593Smuzhiyun gfp_t gfp);
195*4882a593Smuzhiyun void flow_action_cookie_destroy(struct flow_action_cookie *cookie);
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun struct flow_action_entry {
198*4882a593Smuzhiyun enum flow_action_id id;
199*4882a593Smuzhiyun enum flow_action_hw_stats hw_stats;
200*4882a593Smuzhiyun action_destr destructor;
201*4882a593Smuzhiyun void *destructor_priv;
202*4882a593Smuzhiyun union {
203*4882a593Smuzhiyun u32 chain_index; /* FLOW_ACTION_GOTO */
204*4882a593Smuzhiyun struct net_device *dev; /* FLOW_ACTION_REDIRECT */
205*4882a593Smuzhiyun struct { /* FLOW_ACTION_VLAN */
206*4882a593Smuzhiyun u16 vid;
207*4882a593Smuzhiyun __be16 proto;
208*4882a593Smuzhiyun u8 prio;
209*4882a593Smuzhiyun } vlan;
210*4882a593Smuzhiyun struct { /* FLOW_ACTION_MANGLE */
211*4882a593Smuzhiyun /* FLOW_ACTION_ADD */
212*4882a593Smuzhiyun enum flow_action_mangle_base htype;
213*4882a593Smuzhiyun u32 offset;
214*4882a593Smuzhiyun u32 mask;
215*4882a593Smuzhiyun u32 val;
216*4882a593Smuzhiyun } mangle;
217*4882a593Smuzhiyun struct ip_tunnel_info *tunnel; /* FLOW_ACTION_TUNNEL_ENCAP */
218*4882a593Smuzhiyun u32 csum_flags; /* FLOW_ACTION_CSUM */
219*4882a593Smuzhiyun u32 mark; /* FLOW_ACTION_MARK */
220*4882a593Smuzhiyun u16 ptype; /* FLOW_ACTION_PTYPE */
221*4882a593Smuzhiyun u32 priority; /* FLOW_ACTION_PRIORITY */
222*4882a593Smuzhiyun struct { /* FLOW_ACTION_QUEUE */
223*4882a593Smuzhiyun u32 ctx;
224*4882a593Smuzhiyun u32 index;
225*4882a593Smuzhiyun u8 vf;
226*4882a593Smuzhiyun } queue;
227*4882a593Smuzhiyun struct { /* FLOW_ACTION_SAMPLE */
228*4882a593Smuzhiyun struct psample_group *psample_group;
229*4882a593Smuzhiyun u32 rate;
230*4882a593Smuzhiyun u32 trunc_size;
231*4882a593Smuzhiyun bool truncate;
232*4882a593Smuzhiyun } sample;
233*4882a593Smuzhiyun struct { /* FLOW_ACTION_POLICE */
234*4882a593Smuzhiyun u32 index;
235*4882a593Smuzhiyun u32 burst;
236*4882a593Smuzhiyun u64 rate_bytes_ps;
237*4882a593Smuzhiyun u32 mtu;
238*4882a593Smuzhiyun } police;
239*4882a593Smuzhiyun struct { /* FLOW_ACTION_CT */
240*4882a593Smuzhiyun int action;
241*4882a593Smuzhiyun u16 zone;
242*4882a593Smuzhiyun struct nf_flowtable *flow_table;
243*4882a593Smuzhiyun } ct;
244*4882a593Smuzhiyun struct {
245*4882a593Smuzhiyun unsigned long cookie;
246*4882a593Smuzhiyun u32 mark;
247*4882a593Smuzhiyun u32 labels[4];
248*4882a593Smuzhiyun } ct_metadata;
249*4882a593Smuzhiyun struct { /* FLOW_ACTION_MPLS_PUSH */
250*4882a593Smuzhiyun u32 label;
251*4882a593Smuzhiyun __be16 proto;
252*4882a593Smuzhiyun u8 tc;
253*4882a593Smuzhiyun u8 bos;
254*4882a593Smuzhiyun u8 ttl;
255*4882a593Smuzhiyun } mpls_push;
256*4882a593Smuzhiyun struct { /* FLOW_ACTION_MPLS_POP */
257*4882a593Smuzhiyun __be16 proto;
258*4882a593Smuzhiyun } mpls_pop;
259*4882a593Smuzhiyun struct { /* FLOW_ACTION_MPLS_MANGLE */
260*4882a593Smuzhiyun u32 label;
261*4882a593Smuzhiyun u8 tc;
262*4882a593Smuzhiyun u8 bos;
263*4882a593Smuzhiyun u8 ttl;
264*4882a593Smuzhiyun } mpls_mangle;
265*4882a593Smuzhiyun struct {
266*4882a593Smuzhiyun u32 index;
267*4882a593Smuzhiyun s32 prio;
268*4882a593Smuzhiyun u64 basetime;
269*4882a593Smuzhiyun u64 cycletime;
270*4882a593Smuzhiyun u64 cycletimeext;
271*4882a593Smuzhiyun u32 num_entries;
272*4882a593Smuzhiyun struct action_gate_entry *entries;
273*4882a593Smuzhiyun } gate;
274*4882a593Smuzhiyun };
275*4882a593Smuzhiyun struct flow_action_cookie *cookie; /* user defined action cookie */
276*4882a593Smuzhiyun };
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun struct flow_action {
279*4882a593Smuzhiyun unsigned int num_entries;
280*4882a593Smuzhiyun struct flow_action_entry entries[];
281*4882a593Smuzhiyun };
282*4882a593Smuzhiyun
flow_action_has_entries(const struct flow_action * action)283*4882a593Smuzhiyun static inline bool flow_action_has_entries(const struct flow_action *action)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun return action->num_entries;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun /**
289*4882a593Smuzhiyun * flow_action_has_one_action() - check if exactly one action is present
290*4882a593Smuzhiyun * @action: tc filter flow offload action
291*4882a593Smuzhiyun *
292*4882a593Smuzhiyun * Returns true if exactly one action is present.
293*4882a593Smuzhiyun */
flow_offload_has_one_action(const struct flow_action * action)294*4882a593Smuzhiyun static inline bool flow_offload_has_one_action(const struct flow_action *action)
295*4882a593Smuzhiyun {
296*4882a593Smuzhiyun return action->num_entries == 1;
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun #define flow_action_for_each(__i, __act, __actions) \
300*4882a593Smuzhiyun for (__i = 0, __act = &(__actions)->entries[0]; \
301*4882a593Smuzhiyun __i < (__actions)->num_entries; \
302*4882a593Smuzhiyun __act = &(__actions)->entries[++__i])
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun static inline bool
flow_action_mixed_hw_stats_check(const struct flow_action * action,struct netlink_ext_ack * extack)305*4882a593Smuzhiyun flow_action_mixed_hw_stats_check(const struct flow_action *action,
306*4882a593Smuzhiyun struct netlink_ext_ack *extack)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun const struct flow_action_entry *action_entry;
309*4882a593Smuzhiyun u8 last_hw_stats;
310*4882a593Smuzhiyun int i;
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun if (flow_offload_has_one_action(action))
313*4882a593Smuzhiyun return true;
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun flow_action_for_each(i, action_entry, action) {
316*4882a593Smuzhiyun if (i && action_entry->hw_stats != last_hw_stats) {
317*4882a593Smuzhiyun NL_SET_ERR_MSG_MOD(extack, "Mixing HW stats types for actions is not supported");
318*4882a593Smuzhiyun return false;
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun last_hw_stats = action_entry->hw_stats;
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun return true;
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun static inline const struct flow_action_entry *
flow_action_first_entry_get(const struct flow_action * action)326*4882a593Smuzhiyun flow_action_first_entry_get(const struct flow_action *action)
327*4882a593Smuzhiyun {
328*4882a593Smuzhiyun WARN_ON(!flow_action_has_entries(action));
329*4882a593Smuzhiyun return &action->entries[0];
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun static inline bool
__flow_action_hw_stats_check(const struct flow_action * action,struct netlink_ext_ack * extack,bool check_allow_bit,enum flow_action_hw_stats_bit allow_bit)333*4882a593Smuzhiyun __flow_action_hw_stats_check(const struct flow_action *action,
334*4882a593Smuzhiyun struct netlink_ext_ack *extack,
335*4882a593Smuzhiyun bool check_allow_bit,
336*4882a593Smuzhiyun enum flow_action_hw_stats_bit allow_bit)
337*4882a593Smuzhiyun {
338*4882a593Smuzhiyun const struct flow_action_entry *action_entry;
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun if (!flow_action_has_entries(action))
341*4882a593Smuzhiyun return true;
342*4882a593Smuzhiyun if (!flow_action_mixed_hw_stats_check(action, extack))
343*4882a593Smuzhiyun return false;
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun action_entry = flow_action_first_entry_get(action);
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun /* Zero is not a legal value for hw_stats, catch anyone passing it */
348*4882a593Smuzhiyun WARN_ON_ONCE(!action_entry->hw_stats);
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun if (!check_allow_bit &&
351*4882a593Smuzhiyun ~action_entry->hw_stats & FLOW_ACTION_HW_STATS_ANY) {
352*4882a593Smuzhiyun NL_SET_ERR_MSG_MOD(extack, "Driver supports only default HW stats type \"any\"");
353*4882a593Smuzhiyun return false;
354*4882a593Smuzhiyun } else if (check_allow_bit &&
355*4882a593Smuzhiyun !(action_entry->hw_stats & BIT(allow_bit))) {
356*4882a593Smuzhiyun NL_SET_ERR_MSG_MOD(extack, "Driver does not support selected HW stats type");
357*4882a593Smuzhiyun return false;
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun return true;
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun static inline bool
flow_action_hw_stats_check(const struct flow_action * action,struct netlink_ext_ack * extack,enum flow_action_hw_stats_bit allow_bit)363*4882a593Smuzhiyun flow_action_hw_stats_check(const struct flow_action *action,
364*4882a593Smuzhiyun struct netlink_ext_ack *extack,
365*4882a593Smuzhiyun enum flow_action_hw_stats_bit allow_bit)
366*4882a593Smuzhiyun {
367*4882a593Smuzhiyun return __flow_action_hw_stats_check(action, extack, true, allow_bit);
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun static inline bool
flow_action_basic_hw_stats_check(const struct flow_action * action,struct netlink_ext_ack * extack)371*4882a593Smuzhiyun flow_action_basic_hw_stats_check(const struct flow_action *action,
372*4882a593Smuzhiyun struct netlink_ext_ack *extack)
373*4882a593Smuzhiyun {
374*4882a593Smuzhiyun return __flow_action_hw_stats_check(action, extack, false, 0);
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun struct flow_rule {
378*4882a593Smuzhiyun struct flow_match match;
379*4882a593Smuzhiyun struct flow_action action;
380*4882a593Smuzhiyun };
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun struct flow_rule *flow_rule_alloc(unsigned int num_actions);
383*4882a593Smuzhiyun
flow_rule_match_key(const struct flow_rule * rule,enum flow_dissector_key_id key)384*4882a593Smuzhiyun static inline bool flow_rule_match_key(const struct flow_rule *rule,
385*4882a593Smuzhiyun enum flow_dissector_key_id key)
386*4882a593Smuzhiyun {
387*4882a593Smuzhiyun return dissector_uses_key(rule->match.dissector, key);
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun struct flow_stats {
391*4882a593Smuzhiyun u64 pkts;
392*4882a593Smuzhiyun u64 bytes;
393*4882a593Smuzhiyun u64 drops;
394*4882a593Smuzhiyun u64 lastused;
395*4882a593Smuzhiyun enum flow_action_hw_stats used_hw_stats;
396*4882a593Smuzhiyun bool used_hw_stats_valid;
397*4882a593Smuzhiyun };
398*4882a593Smuzhiyun
flow_stats_update(struct flow_stats * flow_stats,u64 bytes,u64 pkts,u64 drops,u64 lastused,enum flow_action_hw_stats used_hw_stats)399*4882a593Smuzhiyun static inline void flow_stats_update(struct flow_stats *flow_stats,
400*4882a593Smuzhiyun u64 bytes, u64 pkts,
401*4882a593Smuzhiyun u64 drops, u64 lastused,
402*4882a593Smuzhiyun enum flow_action_hw_stats used_hw_stats)
403*4882a593Smuzhiyun {
404*4882a593Smuzhiyun flow_stats->pkts += pkts;
405*4882a593Smuzhiyun flow_stats->bytes += bytes;
406*4882a593Smuzhiyun flow_stats->drops += drops;
407*4882a593Smuzhiyun flow_stats->lastused = max_t(u64, flow_stats->lastused, lastused);
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun /* The driver should pass value with a maximum of one bit set.
410*4882a593Smuzhiyun * Passing FLOW_ACTION_HW_STATS_ANY is invalid.
411*4882a593Smuzhiyun */
412*4882a593Smuzhiyun WARN_ON(used_hw_stats == FLOW_ACTION_HW_STATS_ANY);
413*4882a593Smuzhiyun flow_stats->used_hw_stats |= used_hw_stats;
414*4882a593Smuzhiyun flow_stats->used_hw_stats_valid = true;
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun enum flow_block_command {
418*4882a593Smuzhiyun FLOW_BLOCK_BIND,
419*4882a593Smuzhiyun FLOW_BLOCK_UNBIND,
420*4882a593Smuzhiyun };
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun enum flow_block_binder_type {
423*4882a593Smuzhiyun FLOW_BLOCK_BINDER_TYPE_UNSPEC,
424*4882a593Smuzhiyun FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS,
425*4882a593Smuzhiyun FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS,
426*4882a593Smuzhiyun FLOW_BLOCK_BINDER_TYPE_RED_EARLY_DROP,
427*4882a593Smuzhiyun FLOW_BLOCK_BINDER_TYPE_RED_MARK,
428*4882a593Smuzhiyun };
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun struct flow_block {
431*4882a593Smuzhiyun struct list_head cb_list;
432*4882a593Smuzhiyun };
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun struct netlink_ext_ack;
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun struct flow_block_offload {
437*4882a593Smuzhiyun enum flow_block_command command;
438*4882a593Smuzhiyun enum flow_block_binder_type binder_type;
439*4882a593Smuzhiyun bool block_shared;
440*4882a593Smuzhiyun bool unlocked_driver_cb;
441*4882a593Smuzhiyun struct net *net;
442*4882a593Smuzhiyun struct flow_block *block;
443*4882a593Smuzhiyun struct list_head cb_list;
444*4882a593Smuzhiyun struct list_head *driver_block_list;
445*4882a593Smuzhiyun struct netlink_ext_ack *extack;
446*4882a593Smuzhiyun struct Qdisc *sch;
447*4882a593Smuzhiyun struct list_head *cb_list_head;
448*4882a593Smuzhiyun };
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun enum tc_setup_type;
451*4882a593Smuzhiyun typedef int flow_setup_cb_t(enum tc_setup_type type, void *type_data,
452*4882a593Smuzhiyun void *cb_priv);
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun struct flow_block_cb;
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun struct flow_block_indr {
457*4882a593Smuzhiyun struct list_head list;
458*4882a593Smuzhiyun struct net_device *dev;
459*4882a593Smuzhiyun struct Qdisc *sch;
460*4882a593Smuzhiyun enum flow_block_binder_type binder_type;
461*4882a593Smuzhiyun void *data;
462*4882a593Smuzhiyun void *cb_priv;
463*4882a593Smuzhiyun void (*cleanup)(struct flow_block_cb *block_cb);
464*4882a593Smuzhiyun };
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun struct flow_block_cb {
467*4882a593Smuzhiyun struct list_head driver_list;
468*4882a593Smuzhiyun struct list_head list;
469*4882a593Smuzhiyun flow_setup_cb_t *cb;
470*4882a593Smuzhiyun void *cb_ident;
471*4882a593Smuzhiyun void *cb_priv;
472*4882a593Smuzhiyun void (*release)(void *cb_priv);
473*4882a593Smuzhiyun struct flow_block_indr indr;
474*4882a593Smuzhiyun unsigned int refcnt;
475*4882a593Smuzhiyun };
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun struct flow_block_cb *flow_block_cb_alloc(flow_setup_cb_t *cb,
478*4882a593Smuzhiyun void *cb_ident, void *cb_priv,
479*4882a593Smuzhiyun void (*release)(void *cb_priv));
480*4882a593Smuzhiyun struct flow_block_cb *flow_indr_block_cb_alloc(flow_setup_cb_t *cb,
481*4882a593Smuzhiyun void *cb_ident, void *cb_priv,
482*4882a593Smuzhiyun void (*release)(void *cb_priv),
483*4882a593Smuzhiyun struct flow_block_offload *bo,
484*4882a593Smuzhiyun struct net_device *dev,
485*4882a593Smuzhiyun struct Qdisc *sch, void *data,
486*4882a593Smuzhiyun void *indr_cb_priv,
487*4882a593Smuzhiyun void (*cleanup)(struct flow_block_cb *block_cb));
488*4882a593Smuzhiyun void flow_block_cb_free(struct flow_block_cb *block_cb);
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun struct flow_block_cb *flow_block_cb_lookup(struct flow_block *block,
491*4882a593Smuzhiyun flow_setup_cb_t *cb, void *cb_ident);
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun void *flow_block_cb_priv(struct flow_block_cb *block_cb);
494*4882a593Smuzhiyun void flow_block_cb_incref(struct flow_block_cb *block_cb);
495*4882a593Smuzhiyun unsigned int flow_block_cb_decref(struct flow_block_cb *block_cb);
496*4882a593Smuzhiyun
flow_block_cb_add(struct flow_block_cb * block_cb,struct flow_block_offload * offload)497*4882a593Smuzhiyun static inline void flow_block_cb_add(struct flow_block_cb *block_cb,
498*4882a593Smuzhiyun struct flow_block_offload *offload)
499*4882a593Smuzhiyun {
500*4882a593Smuzhiyun list_add_tail(&block_cb->list, &offload->cb_list);
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun
flow_block_cb_remove(struct flow_block_cb * block_cb,struct flow_block_offload * offload)503*4882a593Smuzhiyun static inline void flow_block_cb_remove(struct flow_block_cb *block_cb,
504*4882a593Smuzhiyun struct flow_block_offload *offload)
505*4882a593Smuzhiyun {
506*4882a593Smuzhiyun list_move(&block_cb->list, &offload->cb_list);
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun
flow_indr_block_cb_remove(struct flow_block_cb * block_cb,struct flow_block_offload * offload)509*4882a593Smuzhiyun static inline void flow_indr_block_cb_remove(struct flow_block_cb *block_cb,
510*4882a593Smuzhiyun struct flow_block_offload *offload)
511*4882a593Smuzhiyun {
512*4882a593Smuzhiyun list_del(&block_cb->indr.list);
513*4882a593Smuzhiyun list_move(&block_cb->list, &offload->cb_list);
514*4882a593Smuzhiyun }
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun bool flow_block_cb_is_busy(flow_setup_cb_t *cb, void *cb_ident,
517*4882a593Smuzhiyun struct list_head *driver_block_list);
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun int flow_block_cb_setup_simple(struct flow_block_offload *f,
520*4882a593Smuzhiyun struct list_head *driver_list,
521*4882a593Smuzhiyun flow_setup_cb_t *cb,
522*4882a593Smuzhiyun void *cb_ident, void *cb_priv, bool ingress_only);
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun enum flow_cls_command {
525*4882a593Smuzhiyun FLOW_CLS_REPLACE,
526*4882a593Smuzhiyun FLOW_CLS_DESTROY,
527*4882a593Smuzhiyun FLOW_CLS_STATS,
528*4882a593Smuzhiyun FLOW_CLS_TMPLT_CREATE,
529*4882a593Smuzhiyun FLOW_CLS_TMPLT_DESTROY,
530*4882a593Smuzhiyun };
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun struct flow_cls_common_offload {
533*4882a593Smuzhiyun u32 chain_index;
534*4882a593Smuzhiyun __be16 protocol;
535*4882a593Smuzhiyun u32 prio;
536*4882a593Smuzhiyun struct netlink_ext_ack *extack;
537*4882a593Smuzhiyun };
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun struct flow_cls_offload {
540*4882a593Smuzhiyun struct flow_cls_common_offload common;
541*4882a593Smuzhiyun enum flow_cls_command command;
542*4882a593Smuzhiyun unsigned long cookie;
543*4882a593Smuzhiyun struct flow_rule *rule;
544*4882a593Smuzhiyun struct flow_stats stats;
545*4882a593Smuzhiyun u32 classid;
546*4882a593Smuzhiyun };
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun static inline struct flow_rule *
flow_cls_offload_flow_rule(struct flow_cls_offload * flow_cmd)549*4882a593Smuzhiyun flow_cls_offload_flow_rule(struct flow_cls_offload *flow_cmd)
550*4882a593Smuzhiyun {
551*4882a593Smuzhiyun return flow_cmd->rule;
552*4882a593Smuzhiyun }
553*4882a593Smuzhiyun
flow_block_init(struct flow_block * flow_block)554*4882a593Smuzhiyun static inline void flow_block_init(struct flow_block *flow_block)
555*4882a593Smuzhiyun {
556*4882a593Smuzhiyun INIT_LIST_HEAD(&flow_block->cb_list);
557*4882a593Smuzhiyun }
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun typedef int flow_indr_block_bind_cb_t(struct net_device *dev, struct Qdisc *sch, void *cb_priv,
560*4882a593Smuzhiyun enum tc_setup_type type, void *type_data,
561*4882a593Smuzhiyun void *data,
562*4882a593Smuzhiyun void (*cleanup)(struct flow_block_cb *block_cb));
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun int flow_indr_dev_register(flow_indr_block_bind_cb_t *cb, void *cb_priv);
565*4882a593Smuzhiyun void flow_indr_dev_unregister(flow_indr_block_bind_cb_t *cb, void *cb_priv,
566*4882a593Smuzhiyun void (*release)(void *cb_priv));
567*4882a593Smuzhiyun int flow_indr_dev_setup_offload(struct net_device *dev, struct Qdisc *sch,
568*4882a593Smuzhiyun enum tc_setup_type type, void *data,
569*4882a593Smuzhiyun struct flow_block_offload *bo,
570*4882a593Smuzhiyun void (*cleanup)(struct flow_block_cb *block_cb));
571*4882a593Smuzhiyun bool flow_indr_dev_exists(void);
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun #endif /* _NET_FLOW_OFFLOAD_H */
574