blob: 5b8c54eb7a6b8a6e35d7f14e41f49898b585b456 [file] [log] [blame]
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +01001#ifndef _NET_FLOW_OFFLOAD_H
2#define _NET_FLOW_OFFLOAD_H
3
Edward Creefa859992019-05-31 22:47:21 +01004#include <linux/kernel.h>
Pablo Neira Ayusoa7323312019-07-19 18:20:15 +02005#include <linux/list.h>
Jiri Pirko319a1d12020-03-07 12:40:13 +01006#include <linux/netlink.h>
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +01007#include <net/flow_dissector.h>
8
9struct flow_match {
10 struct flow_dissector *dissector;
11 void *mask;
12 void *key;
13};
14
Jiri Pirko9558a832019-06-19 09:41:04 +030015struct flow_match_meta {
16 struct flow_dissector_key_meta *key, *mask;
17};
18
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +010019struct flow_match_basic {
20 struct flow_dissector_key_basic *key, *mask;
21};
22
23struct flow_match_control {
24 struct flow_dissector_key_control *key, *mask;
25};
26
27struct flow_match_eth_addrs {
28 struct flow_dissector_key_eth_addrs *key, *mask;
29};
30
31struct flow_match_vlan {
32 struct flow_dissector_key_vlan *key, *mask;
33};
34
35struct flow_match_ipv4_addrs {
36 struct flow_dissector_key_ipv4_addrs *key, *mask;
37};
38
39struct flow_match_ipv6_addrs {
40 struct flow_dissector_key_ipv6_addrs *key, *mask;
41};
42
43struct flow_match_ip {
44 struct flow_dissector_key_ip *key, *mask;
45};
46
47struct flow_match_ports {
48 struct flow_dissector_key_ports *key, *mask;
49};
50
51struct flow_match_icmp {
52 struct flow_dissector_key_icmp *key, *mask;
53};
54
55struct flow_match_tcp {
56 struct flow_dissector_key_tcp *key, *mask;
57};
58
59struct flow_match_mpls {
60 struct flow_dissector_key_mpls *key, *mask;
61};
62
63struct flow_match_enc_keyid {
64 struct flow_dissector_key_keyid *key, *mask;
65};
66
67struct flow_match_enc_opts {
68 struct flow_dissector_key_enc_opts *key, *mask;
69};
70
Paul Blakeyee1c45e2020-03-12 12:23:13 +020071struct flow_match_ct {
72 struct flow_dissector_key_ct *key, *mask;
73};
74
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +010075struct flow_rule;
76
Jiri Pirko9558a832019-06-19 09:41:04 +030077void flow_rule_match_meta(const struct flow_rule *rule,
78 struct flow_match_meta *out);
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +010079void flow_rule_match_basic(const struct flow_rule *rule,
80 struct flow_match_basic *out);
81void flow_rule_match_control(const struct flow_rule *rule,
82 struct flow_match_control *out);
83void flow_rule_match_eth_addrs(const struct flow_rule *rule,
84 struct flow_match_eth_addrs *out);
85void flow_rule_match_vlan(const struct flow_rule *rule,
86 struct flow_match_vlan *out);
Edward Creebae9ed62019-05-14 21:18:12 +010087void flow_rule_match_cvlan(const struct flow_rule *rule,
88 struct flow_match_vlan *out);
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +010089void flow_rule_match_ipv4_addrs(const struct flow_rule *rule,
90 struct flow_match_ipv4_addrs *out);
91void flow_rule_match_ipv6_addrs(const struct flow_rule *rule,
92 struct flow_match_ipv6_addrs *out);
93void flow_rule_match_ip(const struct flow_rule *rule,
94 struct flow_match_ip *out);
95void flow_rule_match_ports(const struct flow_rule *rule,
96 struct flow_match_ports *out);
97void flow_rule_match_tcp(const struct flow_rule *rule,
98 struct flow_match_tcp *out);
99void flow_rule_match_icmp(const struct flow_rule *rule,
100 struct flow_match_icmp *out);
101void flow_rule_match_mpls(const struct flow_rule *rule,
102 struct flow_match_mpls *out);
103void flow_rule_match_enc_control(const struct flow_rule *rule,
104 struct flow_match_control *out);
105void flow_rule_match_enc_ipv4_addrs(const struct flow_rule *rule,
106 struct flow_match_ipv4_addrs *out);
107void flow_rule_match_enc_ipv6_addrs(const struct flow_rule *rule,
108 struct flow_match_ipv6_addrs *out);
109void flow_rule_match_enc_ip(const struct flow_rule *rule,
110 struct flow_match_ip *out);
111void flow_rule_match_enc_ports(const struct flow_rule *rule,
112 struct flow_match_ports *out);
113void flow_rule_match_enc_keyid(const struct flow_rule *rule,
114 struct flow_match_enc_keyid *out);
115void flow_rule_match_enc_opts(const struct flow_rule *rule,
116 struct flow_match_enc_opts *out);
Paul Blakeyee1c45e2020-03-12 12:23:13 +0200117void flow_rule_match_ct(const struct flow_rule *rule,
118 struct flow_match_ct *out);
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +0100119
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +0100120enum flow_action_id {
121 FLOW_ACTION_ACCEPT = 0,
122 FLOW_ACTION_DROP,
123 FLOW_ACTION_TRAP,
124 FLOW_ACTION_GOTO,
125 FLOW_ACTION_REDIRECT,
126 FLOW_ACTION_MIRRED,
John Hurley48e584a2019-08-04 16:09:06 +0100127 FLOW_ACTION_REDIRECT_INGRESS,
128 FLOW_ACTION_MIRRED_INGRESS,
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +0100129 FLOW_ACTION_VLAN_PUSH,
130 FLOW_ACTION_VLAN_POP,
131 FLOW_ACTION_VLAN_MANGLE,
132 FLOW_ACTION_TUNNEL_ENCAP,
133 FLOW_ACTION_TUNNEL_DECAP,
134 FLOW_ACTION_MANGLE,
135 FLOW_ACTION_ADD,
136 FLOW_ACTION_CSUM,
137 FLOW_ACTION_MARK,
John Hurleyfb1b7752019-08-04 16:09:04 +0100138 FLOW_ACTION_PTYPE,
Petr Machata2ce12412020-03-19 15:47:21 +0200139 FLOW_ACTION_PRIORITY,
Pablo Neira Ayuso8bec2832019-02-02 12:50:50 +0100140 FLOW_ACTION_WAKE,
141 FLOW_ACTION_QUEUE,
Pieter Jansen van Vuurena7a7be62019-05-04 04:46:16 -0700142 FLOW_ACTION_SAMPLE,
Pieter Jansen van Vuuren8c8cfc62019-05-04 04:46:22 -0700143 FLOW_ACTION_POLICE,
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300144 FLOW_ACTION_CT,
Paul Blakey9c26ba92020-03-12 12:23:06 +0200145 FLOW_ACTION_CT_METADATA,
John Hurley6749d5902019-07-23 15:33:59 +0100146 FLOW_ACTION_MPLS_PUSH,
147 FLOW_ACTION_MPLS_POP,
148 FLOW_ACTION_MPLS_MANGLE,
Po Liud29bdd62020-05-01 08:53:16 +0800149 FLOW_ACTION_GATE,
Pablo Neira Ayuso563ae552021-03-24 02:30:49 +0100150 FLOW_ACTION_PPPOE_PUSH,
Dmytro Linkin7a978752019-06-27 10:55:02 +0000151 NUM_FLOW_ACTIONS,
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +0100152};
153
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +0100154/* This is mirroring enum pedit_header_type definition for easy mapping between
155 * tc pedit action. Legacy TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK is mapped to
156 * FLOW_ACT_MANGLE_UNSPEC, which is supported by no driver.
157 */
158enum flow_action_mangle_base {
159 FLOW_ACT_MANGLE_UNSPEC = 0,
160 FLOW_ACT_MANGLE_HDR_TYPE_ETH,
161 FLOW_ACT_MANGLE_HDR_TYPE_IP4,
162 FLOW_ACT_MANGLE_HDR_TYPE_IP6,
163 FLOW_ACT_MANGLE_HDR_TYPE_TCP,
164 FLOW_ACT_MANGLE_HDR_TYPE_UDP,
165};
166
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -0700167enum flow_action_hw_stats_bit {
Jakub Kicinski53eca1f2020-03-16 18:42:11 -0700168 FLOW_ACTION_HW_STATS_IMMEDIATE_BIT,
169 FLOW_ACTION_HW_STATS_DELAYED_BIT,
Pablo Neira Ayuso16f80362020-05-06 20:34:50 +0200170 FLOW_ACTION_HW_STATS_DISABLED_BIT,
Edward Cree060b6382020-05-20 19:18:10 +0100171
172 FLOW_ACTION_HW_STATS_NUM_BITS
Jiri Pirko42d5fe52020-03-10 16:49:08 +0100173};
174
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -0700175enum flow_action_hw_stats {
Jakub Kicinski53eca1f2020-03-16 18:42:11 -0700176 FLOW_ACTION_HW_STATS_IMMEDIATE =
177 BIT(FLOW_ACTION_HW_STATS_IMMEDIATE_BIT),
178 FLOW_ACTION_HW_STATS_DELAYED = BIT(FLOW_ACTION_HW_STATS_DELAYED_BIT),
179 FLOW_ACTION_HW_STATS_ANY = FLOW_ACTION_HW_STATS_IMMEDIATE |
180 FLOW_ACTION_HW_STATS_DELAYED,
Pablo Neira Ayuso16f80362020-05-06 20:34:50 +0200181 FLOW_ACTION_HW_STATS_DISABLED =
182 BIT(FLOW_ACTION_HW_STATS_DISABLED_BIT),
Edward Cree060b6382020-05-20 19:18:10 +0100183 FLOW_ACTION_HW_STATS_DONT_CARE = BIT(FLOW_ACTION_HW_STATS_NUM_BITS) - 1,
Jiri Pirko42d5fe52020-03-10 16:49:08 +0100184};
Jiri Pirko25149212020-03-07 12:40:11 +0100185
Vlad Buslov11589582019-09-13 18:28:39 +0300186typedef void (*action_destr)(void *priv);
187
Jiri Pirko20084952020-02-25 11:45:18 +0100188struct flow_action_cookie {
189 u32 cookie_len;
190 u8 cookie[];
191};
192
193struct flow_action_cookie *flow_action_cookie_create(void *data,
194 unsigned int len,
195 gfp_t gfp);
196void flow_action_cookie_destroy(struct flow_action_cookie *cookie);
197
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +0100198struct flow_action_entry {
199 enum flow_action_id id;
Baowen Zheng5a995902021-12-17 19:16:19 +0100200 u32 hw_index;
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -0700201 enum flow_action_hw_stats hw_stats;
Vlad Buslov11589582019-09-13 18:28:39 +0300202 action_destr destructor;
203 void *destructor_priv;
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +0100204 union {
205 u32 chain_index; /* FLOW_ACTION_GOTO */
206 struct net_device *dev; /* FLOW_ACTION_REDIRECT */
207 struct { /* FLOW_ACTION_VLAN */
208 u16 vid;
209 __be16 proto;
210 u8 prio;
211 } vlan;
Petr Machata1f40be62020-03-26 16:01:09 +0200212 struct { /* FLOW_ACTION_MANGLE */
213 /* FLOW_ACTION_ADD */
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +0100214 enum flow_action_mangle_base htype;
215 u32 offset;
216 u32 mask;
217 u32 val;
218 } mangle;
Vlad Buslov11589582019-09-13 18:28:39 +0300219 struct ip_tunnel_info *tunnel; /* FLOW_ACTION_TUNNEL_ENCAP */
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +0100220 u32 csum_flags; /* FLOW_ACTION_CSUM */
221 u32 mark; /* FLOW_ACTION_MARK */
John Hurleyfb1b7752019-08-04 16:09:04 +0100222 u16 ptype; /* FLOW_ACTION_PTYPE */
Petr Machata2ce12412020-03-19 15:47:21 +0200223 u32 priority; /* FLOW_ACTION_PRIORITY */
Pablo Neira Ayuso8bec2832019-02-02 12:50:50 +0100224 struct { /* FLOW_ACTION_QUEUE */
225 u32 ctx;
226 u32 index;
227 u8 vf;
228 } queue;
Pieter Jansen van Vuurena7a7be62019-05-04 04:46:16 -0700229 struct { /* FLOW_ACTION_SAMPLE */
230 struct psample_group *psample_group;
231 u32 rate;
232 u32 trunc_size;
233 bool truncate;
234 } sample;
Pieter Jansen van Vuuren8c8cfc62019-05-04 04:46:22 -0700235 struct { /* FLOW_ACTION_POLICE */
Po Liu5f035af2020-06-29 14:54:16 +0800236 u32 burst;
Pieter Jansen van Vuuren8c8cfc62019-05-04 04:46:22 -0700237 u64 rate_bytes_ps;
Xingfeng Hu25660152021-03-12 15:08:29 +0100238 u64 burst_pkt;
239 u64 rate_pkt_ps;
Po Liu19e528d2020-06-24 17:36:28 +0800240 u32 mtu;
Pieter Jansen van Vuuren8c8cfc62019-05-04 04:46:22 -0700241 } police;
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300242 struct { /* FLOW_ACTION_CT */
243 int action;
244 u16 zone;
Paul Blakeyedd58612020-03-12 12:23:09 +0200245 struct nf_flowtable *flow_table;
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300246 } ct;
Paul Blakey9c26ba92020-03-12 12:23:06 +0200247 struct {
Paul Blakey30b0cf92020-03-12 12:23:07 +0200248 unsigned long cookie;
Paul Blakey9c26ba92020-03-12 12:23:06 +0200249 u32 mark;
250 u32 labels[4];
Paul Blakey941eff52021-01-27 16:32:46 +0200251 bool orig_dir;
Paul Blakey9c26ba92020-03-12 12:23:06 +0200252 } ct_metadata;
John Hurley6749d5902019-07-23 15:33:59 +0100253 struct { /* FLOW_ACTION_MPLS_PUSH */
254 u32 label;
255 __be16 proto;
256 u8 tc;
257 u8 bos;
258 u8 ttl;
259 } mpls_push;
260 struct { /* FLOW_ACTION_MPLS_POP */
261 __be16 proto;
262 } mpls_pop;
263 struct { /* FLOW_ACTION_MPLS_MANGLE */
264 u32 label;
265 u8 tc;
266 u8 bos;
267 u8 ttl;
268 } mpls_mangle;
Po Liud29bdd62020-05-01 08:53:16 +0800269 struct {
Po Liud29bdd62020-05-01 08:53:16 +0800270 s32 prio;
271 u64 basetime;
272 u64 cycletime;
273 u64 cycletimeext;
274 u32 num_entries;
275 struct action_gate_entry *entries;
276 } gate;
Pablo Neira Ayuso563ae552021-03-24 02:30:49 +0100277 struct { /* FLOW_ACTION_PPPOE_PUSH */
278 u16 sid;
279 } pppoe;
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +0100280 };
Jiri Pirko20084952020-02-25 11:45:18 +0100281 struct flow_action_cookie *cookie; /* user defined action cookie */
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +0100282};
283
284struct flow_action {
285 unsigned int num_entries;
Gustavo A. R. Silva8661b6e2020-02-28 19:02:54 -0600286 struct flow_action_entry entries[];
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +0100287};
288
289static inline bool flow_action_has_entries(const struct flow_action *action)
290{
291 return action->num_entries;
292}
293
Pieter Jansen van Vuurenab79af32019-05-04 04:46:18 -0700294/**
Bijie Xuc87a4c52021-08-03 11:40:18 +0200295 * flow_offload_has_one_action() - check if exactly one action is present
Pieter Jansen van Vuurenab79af32019-05-04 04:46:18 -0700296 * @action: tc filter flow offload action
297 *
298 * Returns true if exactly one action is present.
299 */
300static inline bool flow_offload_has_one_action(const struct flow_action *action)
301{
302 return action->num_entries == 1;
303}
304
Jiri Pirko62751b62020-03-10 11:11:57 +0100305#define flow_action_for_each(__i, __act, __actions) \
306 for (__i = 0, __act = &(__actions)->entries[0]; \
307 __i < (__actions)->num_entries; \
308 __act = &(__actions)->entries[++__i])
309
Jiri Pirko319a1d12020-03-07 12:40:13 +0100310static inline bool
Jakub Kicinski53eca1f2020-03-16 18:42:11 -0700311flow_action_mixed_hw_stats_check(const struct flow_action *action,
312 struct netlink_ext_ack *extack)
Jiri Pirko319a1d12020-03-07 12:40:13 +0100313{
314 const struct flow_action_entry *action_entry;
Kees Cook3f649ab2020-06-03 13:09:38 -0700315 u8 last_hw_stats;
Jiri Pirko319a1d12020-03-07 12:40:13 +0100316 int i;
317
318 if (flow_offload_has_one_action(action))
319 return true;
320
Ido Schimmelfa05bdb2021-08-19 13:58:42 +0300321 flow_action_for_each(i, action_entry, action) {
322 if (i && action_entry->hw_stats != last_hw_stats) {
323 NL_SET_ERR_MSG_MOD(extack, "Mixing HW stats types for actions is not supported");
324 return false;
Jiri Pirko319a1d12020-03-07 12:40:13 +0100325 }
Ido Schimmelfa05bdb2021-08-19 13:58:42 +0300326 last_hw_stats = action_entry->hw_stats;
Jiri Pirko319a1d12020-03-07 12:40:13 +0100327 }
328 return true;
329}
330
331static inline const struct flow_action_entry *
332flow_action_first_entry_get(const struct flow_action *action)
333{
334 WARN_ON(!flow_action_has_entries(action));
335 return &action->entries[0];
336}
337
338static inline bool
Jakub Kicinski53eca1f2020-03-16 18:42:11 -0700339__flow_action_hw_stats_check(const struct flow_action *action,
340 struct netlink_ext_ack *extack,
341 bool check_allow_bit,
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -0700342 enum flow_action_hw_stats_bit allow_bit)
Jiri Pirko319a1d12020-03-07 12:40:13 +0100343{
344 const struct flow_action_entry *action_entry;
345
346 if (!flow_action_has_entries(action))
347 return true;
Jakub Kicinski53eca1f2020-03-16 18:42:11 -0700348 if (!flow_action_mixed_hw_stats_check(action, extack))
Jiri Pirko319a1d12020-03-07 12:40:13 +0100349 return false;
Pablo Neira Ayuso16f80362020-05-06 20:34:50 +0200350
Jiri Pirko319a1d12020-03-07 12:40:13 +0100351 action_entry = flow_action_first_entry_get(action);
Edward Cree060b6382020-05-20 19:18:10 +0100352
353 /* Zero is not a legal value for hw_stats, catch anyone passing it */
354 WARN_ON_ONCE(!action_entry->hw_stats);
Pablo Neira Ayuso16f80362020-05-06 20:34:50 +0200355
Jiri Pirkoa16fa282020-03-10 16:49:09 +0100356 if (!check_allow_bit &&
Edward Cree060b6382020-05-20 19:18:10 +0100357 ~action_entry->hw_stats & FLOW_ACTION_HW_STATS_ANY) {
Jiri Pirko319a1d12020-03-07 12:40:13 +0100358 NL_SET_ERR_MSG_MOD(extack, "Driver supports only default HW stats type \"any\"");
359 return false;
Jiri Pirkoa16fa282020-03-10 16:49:09 +0100360 } else if (check_allow_bit &&
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -0700361 !(action_entry->hw_stats & BIT(allow_bit))) {
Jiri Pirko319a1d12020-03-07 12:40:13 +0100362 NL_SET_ERR_MSG_MOD(extack, "Driver does not support selected HW stats type");
363 return false;
364 }
365 return true;
366}
367
368static inline bool
Jakub Kicinski53eca1f2020-03-16 18:42:11 -0700369flow_action_hw_stats_check(const struct flow_action *action,
370 struct netlink_ext_ack *extack,
Jakub Kicinski0dfb2d82020-03-19 16:26:23 -0700371 enum flow_action_hw_stats_bit allow_bit)
Jiri Pirkoa16fa282020-03-10 16:49:09 +0100372{
Jakub Kicinski53eca1f2020-03-16 18:42:11 -0700373 return __flow_action_hw_stats_check(action, extack, true, allow_bit);
Jiri Pirkoa16fa282020-03-10 16:49:09 +0100374}
375
376static inline bool
Jakub Kicinski53eca1f2020-03-16 18:42:11 -0700377flow_action_basic_hw_stats_check(const struct flow_action *action,
378 struct netlink_ext_ack *extack)
Jiri Pirko319a1d12020-03-07 12:40:13 +0100379{
Jakub Kicinski53eca1f2020-03-16 18:42:11 -0700380 return __flow_action_hw_stats_check(action, extack, false, 0);
Jiri Pirko319a1d12020-03-07 12:40:13 +0100381}
382
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +0100383struct flow_rule {
384 struct flow_match match;
385 struct flow_action action;
386};
387
388struct flow_rule *flow_rule_alloc(unsigned int num_actions);
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +0100389
390static inline bool flow_rule_match_key(const struct flow_rule *rule,
391 enum flow_dissector_key_id key)
392{
393 return dissector_uses_key(rule->match.dissector, key);
394}
395
Pablo Neira Ayuso3b1903e2019-02-02 12:50:47 +0100396struct flow_stats {
397 u64 pkts;
398 u64 bytes;
Po Liu4b61d3e2020-06-19 14:01:07 +0800399 u64 drops;
Pablo Neira Ayuso3b1903e2019-02-02 12:50:47 +0100400 u64 lastused;
Jiri Pirko93a129e2020-03-28 16:37:43 +0100401 enum flow_action_hw_stats used_hw_stats;
402 bool used_hw_stats_valid;
Pablo Neira Ayuso3b1903e2019-02-02 12:50:47 +0100403};
404
405static inline void flow_stats_update(struct flow_stats *flow_stats,
Po Liu4b61d3e2020-06-19 14:01:07 +0800406 u64 bytes, u64 pkts,
407 u64 drops, u64 lastused,
Jiri Pirko93a129e2020-03-28 16:37:43 +0100408 enum flow_action_hw_stats used_hw_stats)
Pablo Neira Ayuso3b1903e2019-02-02 12:50:47 +0100409{
John Hurley9f9dc492019-02-13 00:23:52 +0000410 flow_stats->pkts += pkts;
411 flow_stats->bytes += bytes;
Po Liu4b61d3e2020-06-19 14:01:07 +0800412 flow_stats->drops += drops;
John Hurley9f9dc492019-02-13 00:23:52 +0000413 flow_stats->lastused = max_t(u64, flow_stats->lastused, lastused);
Jiri Pirko93a129e2020-03-28 16:37:43 +0100414
415 /* The driver should pass value with a maximum of one bit set.
416 * Passing FLOW_ACTION_HW_STATS_ANY is invalid.
417 */
418 WARN_ON(used_hw_stats == FLOW_ACTION_HW_STATS_ANY);
419 flow_stats->used_hw_stats |= used_hw_stats;
420 flow_stats->used_hw_stats_valid = true;
Pablo Neira Ayuso3b1903e2019-02-02 12:50:47 +0100421}
422
Pablo Neira Ayuso4e95bc22019-07-09 22:55:39 +0200423enum flow_block_command {
Pablo Neira Ayuso9c0e1892019-07-09 22:55:40 +0200424 FLOW_BLOCK_BIND,
425 FLOW_BLOCK_UNBIND,
Pablo Neira Ayuso4e95bc22019-07-09 22:55:39 +0200426};
427
428enum flow_block_binder_type {
Pablo Neira Ayuso32f8c402019-07-09 22:55:41 +0200429 FLOW_BLOCK_BINDER_TYPE_UNSPEC,
430 FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS,
431 FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS,
Petr Machataaee9caa2020-06-27 01:45:28 +0300432 FLOW_BLOCK_BINDER_TYPE_RED_EARLY_DROP,
433 FLOW_BLOCK_BINDER_TYPE_RED_MARK,
Pablo Neira Ayuso4e95bc22019-07-09 22:55:39 +0200434};
435
Pablo Neira Ayuso14bfb132019-07-19 18:20:16 +0200436struct flow_block {
437 struct list_head cb_list;
438};
439
Pablo Neira Ayuso4e95bc22019-07-09 22:55:39 +0200440struct netlink_ext_ack;
441
442struct flow_block_offload {
443 enum flow_block_command command;
444 enum flow_block_binder_type binder_type;
Pablo Neira Ayuso955bcb62019-07-09 22:55:46 +0200445 bool block_shared;
Vlad Buslovc9f14472019-08-26 16:45:01 +0300446 bool unlocked_driver_cb;
Pablo Neira Ayusoda3eeb92019-07-09 22:55:43 +0200447 struct net *net;
Pablo Neira Ayuso14bfb132019-07-19 18:20:16 +0200448 struct flow_block *block;
Pablo Neira Ayusoda3eeb92019-07-09 22:55:43 +0200449 struct list_head cb_list;
Pablo Neira Ayuso4e95bc22019-07-09 22:55:39 +0200450 struct list_head *driver_block_list;
451 struct netlink_ext_ack *extack;
Petr Machatac40f4e52020-07-11 00:55:03 +0300452 struct Qdisc *sch;
Eli Cohen74fc4f82021-08-17 20:05:18 +0300453 struct list_head *cb_list_head;
Pablo Neira Ayuso4e95bc22019-07-09 22:55:39 +0200454};
455
Pablo Neira Ayusoa7323312019-07-19 18:20:15 +0200456enum tc_setup_type;
457typedef int flow_setup_cb_t(enum tc_setup_type type, void *type_data,
458 void *cb_priv);
459
Pablo Neira Ayuso1fac52d2020-05-29 02:25:35 +0200460struct flow_block_cb;
461
462struct flow_block_indr {
463 struct list_head list;
464 struct net_device *dev;
Petr Machatac40f4e52020-07-11 00:55:03 +0300465 struct Qdisc *sch;
Pablo Neira Ayuso1fac52d2020-05-29 02:25:35 +0200466 enum flow_block_binder_type binder_type;
467 void *data;
wenxua1db2172020-06-18 20:49:10 +0800468 void *cb_priv;
Pablo Neira Ayuso1fac52d2020-05-29 02:25:35 +0200469 void (*cleanup)(struct flow_block_cb *block_cb);
470};
471
Pablo Neira Ayusod63db302019-07-09 22:55:42 +0200472struct flow_block_cb {
Pablo Neira Ayusoda3eeb92019-07-09 22:55:43 +0200473 struct list_head driver_list;
Pablo Neira Ayusod63db302019-07-09 22:55:42 +0200474 struct list_head list;
Pablo Neira Ayusoa7323312019-07-19 18:20:15 +0200475 flow_setup_cb_t *cb;
Pablo Neira Ayusod63db302019-07-09 22:55:42 +0200476 void *cb_ident;
477 void *cb_priv;
478 void (*release)(void *cb_priv);
Pablo Neira Ayuso1fac52d2020-05-29 02:25:35 +0200479 struct flow_block_indr indr;
Pablo Neira Ayusod63db302019-07-09 22:55:42 +0200480 unsigned int refcnt;
481};
482
Pablo Neira Ayusoa7323312019-07-19 18:20:15 +0200483struct flow_block_cb *flow_block_cb_alloc(flow_setup_cb_t *cb,
Pablo Neira Ayusod63db302019-07-09 22:55:42 +0200484 void *cb_ident, void *cb_priv,
485 void (*release)(void *cb_priv));
wenxu26f2eb22020-06-18 20:49:08 +0800486struct flow_block_cb *flow_indr_block_cb_alloc(flow_setup_cb_t *cb,
487 void *cb_ident, void *cb_priv,
488 void (*release)(void *cb_priv),
489 struct flow_block_offload *bo,
Petr Machatac40f4e52020-07-11 00:55:03 +0300490 struct net_device *dev,
491 struct Qdisc *sch, void *data,
wenxua1db2172020-06-18 20:49:10 +0800492 void *indr_cb_priv,
wenxu26f2eb22020-06-18 20:49:08 +0800493 void (*cleanup)(struct flow_block_cb *block_cb));
Pablo Neira Ayusod63db302019-07-09 22:55:42 +0200494void flow_block_cb_free(struct flow_block_cb *block_cb);
495
Pablo Neira Ayuso14bfb132019-07-19 18:20:16 +0200496struct flow_block_cb *flow_block_cb_lookup(struct flow_block *block,
Pablo Neira Ayusoa7323312019-07-19 18:20:15 +0200497 flow_setup_cb_t *cb, void *cb_ident);
Pablo Neira Ayusoda3eeb92019-07-09 22:55:43 +0200498
Pablo Neira Ayuso67bd0d52019-07-09 22:55:44 +0200499void *flow_block_cb_priv(struct flow_block_cb *block_cb);
500void flow_block_cb_incref(struct flow_block_cb *block_cb);
501unsigned int flow_block_cb_decref(struct flow_block_cb *block_cb);
502
Pablo Neira Ayusoda3eeb92019-07-09 22:55:43 +0200503static inline void flow_block_cb_add(struct flow_block_cb *block_cb,
504 struct flow_block_offload *offload)
505{
506 list_add_tail(&block_cb->list, &offload->cb_list);
507}
508
509static inline void flow_block_cb_remove(struct flow_block_cb *block_cb,
510 struct flow_block_offload *offload)
511{
512 list_move(&block_cb->list, &offload->cb_list);
513}
514
wenxu26f2eb22020-06-18 20:49:08 +0800515static inline void flow_indr_block_cb_remove(struct flow_block_cb *block_cb,
516 struct flow_block_offload *offload)
517{
518 list_del(&block_cb->indr.list);
519 list_move(&block_cb->list, &offload->cb_list);
520}
521
Pablo Neira Ayusoa7323312019-07-19 18:20:15 +0200522bool flow_block_cb_is_busy(flow_setup_cb_t *cb, void *cb_ident,
Pablo Neira Ayuso0d4fd022019-07-09 22:55:48 +0200523 struct list_head *driver_block_list);
524
Pablo Neira Ayuso4e95bc22019-07-09 22:55:39 +0200525int flow_block_cb_setup_simple(struct flow_block_offload *f,
Pablo Neira Ayusoa7323312019-07-19 18:20:15 +0200526 struct list_head *driver_list,
527 flow_setup_cb_t *cb,
Pablo Neira Ayuso4e95bc22019-07-09 22:55:39 +0200528 void *cb_ident, void *cb_priv, bool ingress_only);
529
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +0200530enum flow_cls_command {
531 FLOW_CLS_REPLACE,
532 FLOW_CLS_DESTROY,
533 FLOW_CLS_STATS,
534 FLOW_CLS_TMPLT_CREATE,
535 FLOW_CLS_TMPLT_DESTROY,
536};
537
538struct flow_cls_common_offload {
539 u32 chain_index;
540 __be16 protocol;
541 u32 prio;
542 struct netlink_ext_ack *extack;
543};
544
545struct flow_cls_offload {
546 struct flow_cls_common_offload common;
547 enum flow_cls_command command;
548 unsigned long cookie;
549 struct flow_rule *rule;
550 struct flow_stats stats;
551 u32 classid;
552};
553
Baowen Zheng8cbfe932021-12-17 19:16:22 +0100554enum offload_act_command {
555 FLOW_ACT_REPLACE,
556 FLOW_ACT_DESTROY,
557 FLOW_ACT_STATS,
558};
559
560struct flow_offload_action {
561 struct netlink_ext_ack *extack; /* NULL in FLOW_ACT_STATS process*/
562 enum offload_act_command command;
563 enum flow_action_id id;
564 u32 index;
565 struct flow_stats stats;
566 struct flow_action action;
567};
568
569struct flow_offload_action *offload_action_alloc(unsigned int num_actions);
570
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +0200571static inline struct flow_rule *
572flow_cls_offload_flow_rule(struct flow_cls_offload *flow_cmd)
573{
574 return flow_cmd->rule;
575}
576
Pablo Neira Ayuso14bfb132019-07-19 18:20:16 +0200577static inline void flow_block_init(struct flow_block *flow_block)
578{
579 INIT_LIST_HEAD(&flow_block->cb_list);
580}
581
Petr Machatac40f4e52020-07-11 00:55:03 +0300582typedef int flow_indr_block_bind_cb_t(struct net_device *dev, struct Qdisc *sch, void *cb_priv,
wenxu66f1939a2020-06-18 20:49:09 +0800583 enum tc_setup_type type, void *type_data,
584 void *data,
585 void (*cleanup)(struct flow_block_cb *block_cb));
wenxu4e481902019-08-07 09:13:52 +0800586
Pablo Neira Ayuso1fac52d2020-05-29 02:25:35 +0200587int flow_indr_dev_register(flow_indr_block_bind_cb_t *cb, void *cb_priv);
588void flow_indr_dev_unregister(flow_indr_block_bind_cb_t *cb, void *cb_priv,
wenxua1db2172020-06-18 20:49:10 +0800589 void (*release)(void *cb_priv));
Petr Machatac40f4e52020-07-11 00:55:03 +0300590int flow_indr_dev_setup_offload(struct net_device *dev, struct Qdisc *sch,
Pablo Neira Ayuso1fac52d2020-05-29 02:25:35 +0200591 enum tc_setup_type type, void *data,
592 struct flow_block_offload *bo,
593 void (*cleanup)(struct flow_block_cb *block_cb));
594
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +0100595#endif /* _NET_FLOW_OFFLOAD_H */