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