blob: 86c567f531f35bb6e93b62feb4a02e17e45efb26 [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>
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +01006#include <net/flow_dissector.h>
wenxu4e481902019-08-07 09:13:52 +08007#include <linux/rhashtable.h>
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +01008
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
71struct flow_rule;
72
Jiri Pirko9558a832019-06-19 09:41:04 +030073void flow_rule_match_meta(const struct flow_rule *rule,
74 struct flow_match_meta *out);
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +010075void flow_rule_match_basic(const struct flow_rule *rule,
76 struct flow_match_basic *out);
77void flow_rule_match_control(const struct flow_rule *rule,
78 struct flow_match_control *out);
79void flow_rule_match_eth_addrs(const struct flow_rule *rule,
80 struct flow_match_eth_addrs *out);
81void flow_rule_match_vlan(const struct flow_rule *rule,
82 struct flow_match_vlan *out);
Edward Creebae9ed62019-05-14 21:18:12 +010083void flow_rule_match_cvlan(const struct flow_rule *rule,
84 struct flow_match_vlan *out);
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +010085void flow_rule_match_ipv4_addrs(const struct flow_rule *rule,
86 struct flow_match_ipv4_addrs *out);
87void flow_rule_match_ipv6_addrs(const struct flow_rule *rule,
88 struct flow_match_ipv6_addrs *out);
89void flow_rule_match_ip(const struct flow_rule *rule,
90 struct flow_match_ip *out);
91void flow_rule_match_ports(const struct flow_rule *rule,
92 struct flow_match_ports *out);
93void flow_rule_match_tcp(const struct flow_rule *rule,
94 struct flow_match_tcp *out);
95void flow_rule_match_icmp(const struct flow_rule *rule,
96 struct flow_match_icmp *out);
97void flow_rule_match_mpls(const struct flow_rule *rule,
98 struct flow_match_mpls *out);
99void flow_rule_match_enc_control(const struct flow_rule *rule,
100 struct flow_match_control *out);
101void flow_rule_match_enc_ipv4_addrs(const struct flow_rule *rule,
102 struct flow_match_ipv4_addrs *out);
103void flow_rule_match_enc_ipv6_addrs(const struct flow_rule *rule,
104 struct flow_match_ipv6_addrs *out);
105void flow_rule_match_enc_ip(const struct flow_rule *rule,
106 struct flow_match_ip *out);
107void flow_rule_match_enc_ports(const struct flow_rule *rule,
108 struct flow_match_ports *out);
109void flow_rule_match_enc_keyid(const struct flow_rule *rule,
110 struct flow_match_enc_keyid *out);
111void flow_rule_match_enc_opts(const struct flow_rule *rule,
112 struct flow_match_enc_opts *out);
113
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +0100114enum flow_action_id {
115 FLOW_ACTION_ACCEPT = 0,
116 FLOW_ACTION_DROP,
117 FLOW_ACTION_TRAP,
118 FLOW_ACTION_GOTO,
119 FLOW_ACTION_REDIRECT,
120 FLOW_ACTION_MIRRED,
John Hurley48e584a2019-08-04 16:09:06 +0100121 FLOW_ACTION_REDIRECT_INGRESS,
122 FLOW_ACTION_MIRRED_INGRESS,
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +0100123 FLOW_ACTION_VLAN_PUSH,
124 FLOW_ACTION_VLAN_POP,
125 FLOW_ACTION_VLAN_MANGLE,
126 FLOW_ACTION_TUNNEL_ENCAP,
127 FLOW_ACTION_TUNNEL_DECAP,
128 FLOW_ACTION_MANGLE,
129 FLOW_ACTION_ADD,
130 FLOW_ACTION_CSUM,
131 FLOW_ACTION_MARK,
John Hurleyfb1b7752019-08-04 16:09:04 +0100132 FLOW_ACTION_PTYPE,
Pablo Neira Ayuso8bec2832019-02-02 12:50:50 +0100133 FLOW_ACTION_WAKE,
134 FLOW_ACTION_QUEUE,
Pieter Jansen van Vuurena7a7be62019-05-04 04:46:16 -0700135 FLOW_ACTION_SAMPLE,
Pieter Jansen van Vuuren8c8cfc62019-05-04 04:46:22 -0700136 FLOW_ACTION_POLICE,
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300137 FLOW_ACTION_CT,
John Hurley6749d5902019-07-23 15:33:59 +0100138 FLOW_ACTION_MPLS_PUSH,
139 FLOW_ACTION_MPLS_POP,
140 FLOW_ACTION_MPLS_MANGLE,
Dmytro Linkin7a978752019-06-27 10:55:02 +0000141 NUM_FLOW_ACTIONS,
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +0100142};
143
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +0100144/* This is mirroring enum pedit_header_type definition for easy mapping between
145 * tc pedit action. Legacy TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK is mapped to
146 * FLOW_ACT_MANGLE_UNSPEC, which is supported by no driver.
147 */
148enum flow_action_mangle_base {
149 FLOW_ACT_MANGLE_UNSPEC = 0,
150 FLOW_ACT_MANGLE_HDR_TYPE_ETH,
151 FLOW_ACT_MANGLE_HDR_TYPE_IP4,
152 FLOW_ACT_MANGLE_HDR_TYPE_IP6,
153 FLOW_ACT_MANGLE_HDR_TYPE_TCP,
154 FLOW_ACT_MANGLE_HDR_TYPE_UDP,
155};
156
Vlad Buslov11589582019-09-13 18:28:39 +0300157typedef void (*action_destr)(void *priv);
158
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +0100159struct flow_action_entry {
160 enum flow_action_id id;
Vlad Buslov11589582019-09-13 18:28:39 +0300161 action_destr destructor;
162 void *destructor_priv;
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +0100163 union {
164 u32 chain_index; /* FLOW_ACTION_GOTO */
165 struct net_device *dev; /* FLOW_ACTION_REDIRECT */
166 struct { /* FLOW_ACTION_VLAN */
167 u16 vid;
168 __be16 proto;
169 u8 prio;
170 } vlan;
171 struct { /* FLOW_ACTION_PACKET_EDIT */
172 enum flow_action_mangle_base htype;
173 u32 offset;
174 u32 mask;
175 u32 val;
176 } mangle;
Vlad Buslov11589582019-09-13 18:28:39 +0300177 struct ip_tunnel_info *tunnel; /* FLOW_ACTION_TUNNEL_ENCAP */
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +0100178 u32 csum_flags; /* FLOW_ACTION_CSUM */
179 u32 mark; /* FLOW_ACTION_MARK */
John Hurleyfb1b7752019-08-04 16:09:04 +0100180 u16 ptype; /* FLOW_ACTION_PTYPE */
Pablo Neira Ayuso8bec2832019-02-02 12:50:50 +0100181 struct { /* FLOW_ACTION_QUEUE */
182 u32 ctx;
183 u32 index;
184 u8 vf;
185 } queue;
Pieter Jansen van Vuurena7a7be62019-05-04 04:46:16 -0700186 struct { /* FLOW_ACTION_SAMPLE */
187 struct psample_group *psample_group;
188 u32 rate;
189 u32 trunc_size;
190 bool truncate;
191 } sample;
Pieter Jansen van Vuuren8c8cfc62019-05-04 04:46:22 -0700192 struct { /* FLOW_ACTION_POLICE */
193 s64 burst;
194 u64 rate_bytes_ps;
195 } police;
Paul Blakeyb57dc7c2019-07-09 10:30:48 +0300196 struct { /* FLOW_ACTION_CT */
197 int action;
198 u16 zone;
199 } ct;
John Hurley6749d5902019-07-23 15:33:59 +0100200 struct { /* FLOW_ACTION_MPLS_PUSH */
201 u32 label;
202 __be16 proto;
203 u8 tc;
204 u8 bos;
205 u8 ttl;
206 } mpls_push;
207 struct { /* FLOW_ACTION_MPLS_POP */
208 __be16 proto;
209 } mpls_pop;
210 struct { /* FLOW_ACTION_MPLS_MANGLE */
211 u32 label;
212 u8 tc;
213 u8 bos;
214 u8 ttl;
215 } mpls_mangle;
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +0100216 };
217};
218
219struct flow_action {
220 unsigned int num_entries;
221 struct flow_action_entry entries[0];
222};
223
224static inline bool flow_action_has_entries(const struct flow_action *action)
225{
226 return action->num_entries;
227}
228
Pieter Jansen van Vuurenab79af32019-05-04 04:46:18 -0700229/**
230 * flow_action_has_one_action() - check if exactly one action is present
231 * @action: tc filter flow offload action
232 *
233 * Returns true if exactly one action is present.
234 */
235static inline bool flow_offload_has_one_action(const struct flow_action *action)
236{
237 return action->num_entries == 1;
238}
239
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +0100240#define flow_action_for_each(__i, __act, __actions) \
Eli Britstein6663cf82019-02-11 09:52:59 +0200241 for (__i = 0, __act = &(__actions)->entries[0]; __i < (__actions)->num_entries; __act = &(__actions)->entries[++__i])
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +0100242
243struct flow_rule {
244 struct flow_match match;
245 struct flow_action action;
246};
247
248struct flow_rule *flow_rule_alloc(unsigned int num_actions);
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +0100249
250static inline bool flow_rule_match_key(const struct flow_rule *rule,
251 enum flow_dissector_key_id key)
252{
253 return dissector_uses_key(rule->match.dissector, key);
254}
255
Pablo Neira Ayuso3b1903e2019-02-02 12:50:47 +0100256struct flow_stats {
257 u64 pkts;
258 u64 bytes;
259 u64 lastused;
260};
261
262static inline void flow_stats_update(struct flow_stats *flow_stats,
263 u64 bytes, u64 pkts, u64 lastused)
264{
John Hurley9f9dc492019-02-13 00:23:52 +0000265 flow_stats->pkts += pkts;
266 flow_stats->bytes += bytes;
267 flow_stats->lastused = max_t(u64, flow_stats->lastused, lastused);
Pablo Neira Ayuso3b1903e2019-02-02 12:50:47 +0100268}
269
Pablo Neira Ayuso4e95bc22019-07-09 22:55:39 +0200270enum flow_block_command {
Pablo Neira Ayuso9c0e1892019-07-09 22:55:40 +0200271 FLOW_BLOCK_BIND,
272 FLOW_BLOCK_UNBIND,
Pablo Neira Ayuso4e95bc22019-07-09 22:55:39 +0200273};
274
275enum flow_block_binder_type {
Pablo Neira Ayuso32f8c402019-07-09 22:55:41 +0200276 FLOW_BLOCK_BINDER_TYPE_UNSPEC,
277 FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS,
278 FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS,
Pablo Neira Ayuso4e95bc22019-07-09 22:55:39 +0200279};
280
Pablo Neira Ayuso14bfb132019-07-19 18:20:16 +0200281struct flow_block {
282 struct list_head cb_list;
283};
284
Pablo Neira Ayuso4e95bc22019-07-09 22:55:39 +0200285struct netlink_ext_ack;
286
287struct flow_block_offload {
288 enum flow_block_command command;
289 enum flow_block_binder_type binder_type;
Pablo Neira Ayuso955bcb62019-07-09 22:55:46 +0200290 bool block_shared;
Vlad Buslovc9f14472019-08-26 16:45:01 +0300291 bool unlocked_driver_cb;
Pablo Neira Ayusoda3eeb92019-07-09 22:55:43 +0200292 struct net *net;
Pablo Neira Ayuso14bfb132019-07-19 18:20:16 +0200293 struct flow_block *block;
Pablo Neira Ayusoda3eeb92019-07-09 22:55:43 +0200294 struct list_head cb_list;
Pablo Neira Ayuso4e95bc22019-07-09 22:55:39 +0200295 struct list_head *driver_block_list;
296 struct netlink_ext_ack *extack;
297};
298
Pablo Neira Ayusoa7323312019-07-19 18:20:15 +0200299enum tc_setup_type;
300typedef int flow_setup_cb_t(enum tc_setup_type type, void *type_data,
301 void *cb_priv);
302
Pablo Neira Ayusod63db302019-07-09 22:55:42 +0200303struct flow_block_cb {
Pablo Neira Ayusoda3eeb92019-07-09 22:55:43 +0200304 struct list_head driver_list;
Pablo Neira Ayusod63db302019-07-09 22:55:42 +0200305 struct list_head list;
Pablo Neira Ayusoa7323312019-07-19 18:20:15 +0200306 flow_setup_cb_t *cb;
Pablo Neira Ayusod63db302019-07-09 22:55:42 +0200307 void *cb_ident;
308 void *cb_priv;
309 void (*release)(void *cb_priv);
310 unsigned int refcnt;
311};
312
Pablo Neira Ayusoa7323312019-07-19 18:20:15 +0200313struct flow_block_cb *flow_block_cb_alloc(flow_setup_cb_t *cb,
Pablo Neira Ayusod63db302019-07-09 22:55:42 +0200314 void *cb_ident, void *cb_priv,
315 void (*release)(void *cb_priv));
316void flow_block_cb_free(struct flow_block_cb *block_cb);
317
Pablo Neira Ayuso14bfb132019-07-19 18:20:16 +0200318struct flow_block_cb *flow_block_cb_lookup(struct flow_block *block,
Pablo Neira Ayusoa7323312019-07-19 18:20:15 +0200319 flow_setup_cb_t *cb, void *cb_ident);
Pablo Neira Ayusoda3eeb92019-07-09 22:55:43 +0200320
Pablo Neira Ayuso67bd0d52019-07-09 22:55:44 +0200321void *flow_block_cb_priv(struct flow_block_cb *block_cb);
322void flow_block_cb_incref(struct flow_block_cb *block_cb);
323unsigned int flow_block_cb_decref(struct flow_block_cb *block_cb);
324
Pablo Neira Ayusoda3eeb92019-07-09 22:55:43 +0200325static inline void flow_block_cb_add(struct flow_block_cb *block_cb,
326 struct flow_block_offload *offload)
327{
328 list_add_tail(&block_cb->list, &offload->cb_list);
329}
330
331static inline void flow_block_cb_remove(struct flow_block_cb *block_cb,
332 struct flow_block_offload *offload)
333{
334 list_move(&block_cb->list, &offload->cb_list);
335}
336
Pablo Neira Ayusoa7323312019-07-19 18:20:15 +0200337bool flow_block_cb_is_busy(flow_setup_cb_t *cb, void *cb_ident,
Pablo Neira Ayuso0d4fd022019-07-09 22:55:48 +0200338 struct list_head *driver_block_list);
339
Pablo Neira Ayuso4e95bc22019-07-09 22:55:39 +0200340int flow_block_cb_setup_simple(struct flow_block_offload *f,
Pablo Neira Ayusoa7323312019-07-19 18:20:15 +0200341 struct list_head *driver_list,
342 flow_setup_cb_t *cb,
Pablo Neira Ayuso4e95bc22019-07-09 22:55:39 +0200343 void *cb_ident, void *cb_priv, bool ingress_only);
344
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +0200345enum flow_cls_command {
346 FLOW_CLS_REPLACE,
347 FLOW_CLS_DESTROY,
348 FLOW_CLS_STATS,
349 FLOW_CLS_TMPLT_CREATE,
350 FLOW_CLS_TMPLT_DESTROY,
351};
352
353struct flow_cls_common_offload {
354 u32 chain_index;
355 __be16 protocol;
356 u32 prio;
357 struct netlink_ext_ack *extack;
358};
359
360struct flow_cls_offload {
361 struct flow_cls_common_offload common;
362 enum flow_cls_command command;
363 unsigned long cookie;
364 struct flow_rule *rule;
365 struct flow_stats stats;
366 u32 classid;
367};
368
369static inline struct flow_rule *
370flow_cls_offload_flow_rule(struct flow_cls_offload *flow_cmd)
371{
372 return flow_cmd->rule;
373}
374
Pablo Neira Ayuso14bfb132019-07-19 18:20:16 +0200375static inline void flow_block_init(struct flow_block *flow_block)
376{
377 INIT_LIST_HEAD(&flow_block->cb_list);
378}
379
wenxu4e481902019-08-07 09:13:52 +0800380typedef int flow_indr_block_bind_cb_t(struct net_device *dev, void *cb_priv,
381 enum tc_setup_type type, void *type_data);
382
383typedef void flow_indr_block_ing_cmd_t(struct net_device *dev,
384 flow_indr_block_bind_cb_t *cb,
385 void *cb_priv,
386 enum flow_block_command command);
387
wenxu1150ab02019-08-07 09:13:53 +0800388struct flow_indr_block_ing_entry {
389 flow_indr_block_ing_cmd_t *cb;
390 struct list_head list;
391};
392
393void flow_indr_add_block_ing_cb(struct flow_indr_block_ing_entry *entry);
394
395void flow_indr_del_block_ing_cb(struct flow_indr_block_ing_entry *entry);
396
wenxu4e481902019-08-07 09:13:52 +0800397int __flow_indr_block_cb_register(struct net_device *dev, void *cb_priv,
398 flow_indr_block_bind_cb_t *cb,
399 void *cb_ident);
400
401void __flow_indr_block_cb_unregister(struct net_device *dev,
402 flow_indr_block_bind_cb_t *cb,
403 void *cb_ident);
404
405int flow_indr_block_cb_register(struct net_device *dev, void *cb_priv,
406 flow_indr_block_bind_cb_t *cb, void *cb_ident);
407
408void flow_indr_block_cb_unregister(struct net_device *dev,
409 flow_indr_block_bind_cb_t *cb,
410 void *cb_ident);
411
412void flow_indr_block_call(struct net_device *dev,
wenxu4e481902019-08-07 09:13:52 +0800413 struct flow_block_offload *bo,
414 enum flow_block_command command);
415
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +0100416#endif /* _NET_FLOW_OFFLOAD_H */