xref: /linux/include/net/flow_offload.h (revision 31d166642c7c601c65eccf0ff2e0afe9a0538be2)
1 #ifndef _NET_FLOW_OFFLOAD_H
2 #define _NET_FLOW_OFFLOAD_H
3 
4 #include <linux/kernel.h>
5 #include <net/flow_dissector.h>
6 
7 struct flow_match {
8 	struct flow_dissector	*dissector;
9 	void			*mask;
10 	void			*key;
11 };
12 
13 struct flow_match_meta {
14 	struct flow_dissector_key_meta *key, *mask;
15 };
16 
17 struct flow_match_basic {
18 	struct flow_dissector_key_basic *key, *mask;
19 };
20 
21 struct flow_match_control {
22 	struct flow_dissector_key_control *key, *mask;
23 };
24 
25 struct flow_match_eth_addrs {
26 	struct flow_dissector_key_eth_addrs *key, *mask;
27 };
28 
29 struct flow_match_vlan {
30 	struct flow_dissector_key_vlan *key, *mask;
31 };
32 
33 struct flow_match_ipv4_addrs {
34 	struct flow_dissector_key_ipv4_addrs *key, *mask;
35 };
36 
37 struct flow_match_ipv6_addrs {
38 	struct flow_dissector_key_ipv6_addrs *key, *mask;
39 };
40 
41 struct flow_match_ip {
42 	struct flow_dissector_key_ip *key, *mask;
43 };
44 
45 struct flow_match_ports {
46 	struct flow_dissector_key_ports *key, *mask;
47 };
48 
49 struct flow_match_icmp {
50 	struct flow_dissector_key_icmp *key, *mask;
51 };
52 
53 struct flow_match_tcp {
54 	struct flow_dissector_key_tcp *key, *mask;
55 };
56 
57 struct flow_match_mpls {
58 	struct flow_dissector_key_mpls *key, *mask;
59 };
60 
61 struct flow_match_enc_keyid {
62 	struct flow_dissector_key_keyid *key, *mask;
63 };
64 
65 struct flow_match_enc_opts {
66 	struct flow_dissector_key_enc_opts *key, *mask;
67 };
68 
69 struct flow_rule;
70 
71 void flow_rule_match_meta(const struct flow_rule *rule,
72 			  struct flow_match_meta *out);
73 void flow_rule_match_basic(const struct flow_rule *rule,
74 			   struct flow_match_basic *out);
75 void flow_rule_match_control(const struct flow_rule *rule,
76 			     struct flow_match_control *out);
77 void flow_rule_match_eth_addrs(const struct flow_rule *rule,
78 			       struct flow_match_eth_addrs *out);
79 void flow_rule_match_vlan(const struct flow_rule *rule,
80 			  struct flow_match_vlan *out);
81 void flow_rule_match_cvlan(const struct flow_rule *rule,
82 			   struct flow_match_vlan *out);
83 void flow_rule_match_ipv4_addrs(const struct flow_rule *rule,
84 				struct flow_match_ipv4_addrs *out);
85 void flow_rule_match_ipv6_addrs(const struct flow_rule *rule,
86 				struct flow_match_ipv6_addrs *out);
87 void flow_rule_match_ip(const struct flow_rule *rule,
88 			struct flow_match_ip *out);
89 void flow_rule_match_ports(const struct flow_rule *rule,
90 			   struct flow_match_ports *out);
91 void flow_rule_match_tcp(const struct flow_rule *rule,
92 			 struct flow_match_tcp *out);
93 void flow_rule_match_icmp(const struct flow_rule *rule,
94 			  struct flow_match_icmp *out);
95 void flow_rule_match_mpls(const struct flow_rule *rule,
96 			  struct flow_match_mpls *out);
97 void flow_rule_match_enc_control(const struct flow_rule *rule,
98 				 struct flow_match_control *out);
99 void flow_rule_match_enc_ipv4_addrs(const struct flow_rule *rule,
100 				    struct flow_match_ipv4_addrs *out);
101 void flow_rule_match_enc_ipv6_addrs(const struct flow_rule *rule,
102 				    struct flow_match_ipv6_addrs *out);
103 void flow_rule_match_enc_ip(const struct flow_rule *rule,
104 			    struct flow_match_ip *out);
105 void flow_rule_match_enc_ports(const struct flow_rule *rule,
106 			       struct flow_match_ports *out);
107 void flow_rule_match_enc_keyid(const struct flow_rule *rule,
108 			       struct flow_match_enc_keyid *out);
109 void flow_rule_match_enc_opts(const struct flow_rule *rule,
110 			      struct flow_match_enc_opts *out);
111 
112 enum flow_action_id {
113 	FLOW_ACTION_ACCEPT		= 0,
114 	FLOW_ACTION_DROP,
115 	FLOW_ACTION_TRAP,
116 	FLOW_ACTION_GOTO,
117 	FLOW_ACTION_REDIRECT,
118 	FLOW_ACTION_MIRRED,
119 	FLOW_ACTION_VLAN_PUSH,
120 	FLOW_ACTION_VLAN_POP,
121 	FLOW_ACTION_VLAN_MANGLE,
122 	FLOW_ACTION_TUNNEL_ENCAP,
123 	FLOW_ACTION_TUNNEL_DECAP,
124 	FLOW_ACTION_MANGLE,
125 	FLOW_ACTION_ADD,
126 	FLOW_ACTION_CSUM,
127 	FLOW_ACTION_MARK,
128 	FLOW_ACTION_WAKE,
129 	FLOW_ACTION_QUEUE,
130 	FLOW_ACTION_SAMPLE,
131 	FLOW_ACTION_POLICE,
132 };
133 
134 /* This is mirroring enum pedit_header_type definition for easy mapping between
135  * tc pedit action. Legacy TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK is mapped to
136  * FLOW_ACT_MANGLE_UNSPEC, which is supported by no driver.
137  */
138 enum flow_action_mangle_base {
139 	FLOW_ACT_MANGLE_UNSPEC		= 0,
140 	FLOW_ACT_MANGLE_HDR_TYPE_ETH,
141 	FLOW_ACT_MANGLE_HDR_TYPE_IP4,
142 	FLOW_ACT_MANGLE_HDR_TYPE_IP6,
143 	FLOW_ACT_MANGLE_HDR_TYPE_TCP,
144 	FLOW_ACT_MANGLE_HDR_TYPE_UDP,
145 };
146 
147 struct flow_action_entry {
148 	enum flow_action_id		id;
149 	union {
150 		u32			chain_index;	/* FLOW_ACTION_GOTO */
151 		struct net_device	*dev;		/* FLOW_ACTION_REDIRECT */
152 		struct {				/* FLOW_ACTION_VLAN */
153 			u16		vid;
154 			__be16		proto;
155 			u8		prio;
156 		} vlan;
157 		struct {				/* FLOW_ACTION_PACKET_EDIT */
158 			enum flow_action_mangle_base htype;
159 			u32		offset;
160 			u32		mask;
161 			u32		val;
162 		} mangle;
163 		const struct ip_tunnel_info *tunnel;	/* FLOW_ACTION_TUNNEL_ENCAP */
164 		u32			csum_flags;	/* FLOW_ACTION_CSUM */
165 		u32			mark;		/* FLOW_ACTION_MARK */
166 		struct {				/* FLOW_ACTION_QUEUE */
167 			u32		ctx;
168 			u32		index;
169 			u8		vf;
170 		} queue;
171 		struct {				/* FLOW_ACTION_SAMPLE */
172 			struct psample_group	*psample_group;
173 			u32			rate;
174 			u32			trunc_size;
175 			bool			truncate;
176 		} sample;
177 		struct {				/* FLOW_ACTION_POLICE */
178 			s64			burst;
179 			u64			rate_bytes_ps;
180 		} police;
181 	};
182 };
183 
184 struct flow_action {
185 	unsigned int			num_entries;
186 	struct flow_action_entry 	entries[0];
187 };
188 
189 static inline bool flow_action_has_entries(const struct flow_action *action)
190 {
191 	return action->num_entries;
192 }
193 
194 /**
195  * flow_action_has_one_action() - check if exactly one action is present
196  * @action: tc filter flow offload action
197  *
198  * Returns true if exactly one action is present.
199  */
200 static inline bool flow_offload_has_one_action(const struct flow_action *action)
201 {
202 	return action->num_entries == 1;
203 }
204 
205 #define flow_action_for_each(__i, __act, __actions)			\
206         for (__i = 0, __act = &(__actions)->entries[0]; __i < (__actions)->num_entries; __act = &(__actions)->entries[++__i])
207 
208 struct flow_rule {
209 	struct flow_match	match;
210 	struct flow_action	action;
211 };
212 
213 struct flow_rule *flow_rule_alloc(unsigned int num_actions);
214 
215 static inline bool flow_rule_match_key(const struct flow_rule *rule,
216 				       enum flow_dissector_key_id key)
217 {
218 	return dissector_uses_key(rule->match.dissector, key);
219 }
220 
221 struct flow_stats {
222 	u64	pkts;
223 	u64	bytes;
224 	u64	lastused;
225 };
226 
227 static inline void flow_stats_update(struct flow_stats *flow_stats,
228 				     u64 bytes, u64 pkts, u64 lastused)
229 {
230 	flow_stats->pkts	+= pkts;
231 	flow_stats->bytes	+= bytes;
232 	flow_stats->lastused	= max_t(u64, flow_stats->lastused, lastused);
233 }
234 
235 #endif /* _NET_FLOW_OFFLOAD_H */
236