]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
0a96a21b | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
064af421 | 7 | * |
a14bc59f BP |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
064af421 BP |
15 | */ |
16 | #ifndef FLOW_H | |
17 | #define FLOW_H 1 | |
18 | ||
7f3adc00 | 19 | #include <sys/types.h> |
064af421 | 20 | #include <netinet/in.h> |
c17fcc0a | 21 | #include <netinet/icmp6.h> |
064af421 BP |
22 | #include <stdbool.h> |
23 | #include <stdint.h> | |
24 | #include <string.h> | |
5fcff47b | 25 | #include "bitmap.h" |
c906cedf | 26 | #include "byte-order.h" |
1182038f | 27 | #include "openvswitch/compiler.h" |
659586ef | 28 | #include "openflow/nicira-ext.h" |
064af421 | 29 | #include "openflow/openflow.h" |
1182038f | 30 | #include "openvswitch/flow.h" |
b7807e4f | 31 | #include "packets.h" |
064af421 | 32 | #include "hash.h" |
064af421 BP |
33 | #include "util.h" |
34 | ||
c97fb132 | 35 | struct dpif_flow_stats; |
064af421 | 36 | struct ds; |
b63f2ea7 | 37 | struct flow_wildcards; |
5cb7a798 | 38 | struct minimask; |
cf62fa4c | 39 | struct dp_packet; |
b5e7e61a | 40 | struct pkt_metadata; |
50dcbd8e | 41 | struct match; |
064af421 | 42 | |
154374a7 DDP |
43 | /* Some flow fields are mutually exclusive or only appear within the flow |
44 | * pipeline. IPv6 headers are bigger than IPv4 and MPLS, and IPv6 ND packets | |
45 | * are bigger than TCP,UDP and IGMP packets. */ | |
d70e8c28 JR |
46 | #define FLOW_MAX_PACKET_U64S (FLOW_U64S \ |
47 | /* Unused in datapath */ - FLOW_U64_SIZE(regs) \ | |
48 | - FLOW_U64_SIZE(metadata) \ | |
49 | /* L2.5/3 */ - FLOW_U64_SIZE(nw_src) /* incl. nw_dst */ \ | |
50 | - FLOW_U64_SIZE(mpls_lse) \ | |
51 | /* L4 */ - FLOW_U64_SIZE(tp_src) \ | |
154374a7 DDP |
52 | ) |
53 | ||
d70e8c28 | 54 | extern const uint8_t flow_segment_u64s[]; |
14608a15 | 55 | |
5fcff47b JR |
56 | #define FLOW_U64_OFFSET(FIELD) \ |
57 | (offsetof(struct flow, FIELD) / sizeof(uint64_t)) | |
58 | #define FLOW_U64_OFFREM(FIELD) \ | |
59 | (offsetof(struct flow, FIELD) % sizeof(uint64_t)) | |
60 | ||
61 | /* Number of 64-bit units spanned by a 'FIELD'. */ | |
62 | #define FLOW_U64_SIZE(FIELD) \ | |
63 | DIV_ROUND_UP(FLOW_U64_OFFREM(FIELD) + MEMBER_SIZEOF(struct flow, FIELD), \ | |
64 | sizeof(uint64_t)) | |
65 | ||
cf62fa4c | 66 | void flow_extract(struct dp_packet *, struct flow *); |
b02475c5 | 67 | |
993410fb | 68 | void flow_zero_wildcards(struct flow *, const struct flow_wildcards *); |
d8d9c698 | 69 | void flow_unwildcard_tp_ports(const struct flow *, struct flow_wildcards *); |
50dcbd8e | 70 | void flow_get_metadata(const struct flow *, struct match *flow_metadata); |
993410fb | 71 | |
07659514 | 72 | const char *ct_state_to_string(uint32_t state); |
ae412e7d | 73 | char *flow_to_string(const struct flow *); |
4fe3445a PS |
74 | void format_flags(struct ds *ds, const char *(*bit_to_string)(uint32_t), |
75 | uint32_t flags, char del); | |
61bf6666 JR |
76 | void format_flags_masked(struct ds *ds, const char *name, |
77 | const char *(*bit_to_string)(uint32_t), | |
8e4c1621 JG |
78 | uint32_t flags, uint32_t mask, uint32_t max_mask); |
79 | int parse_flags(const char *s, const char *(*bit_to_string)(uint32_t), | |
80 | char end, const char *field_name, char **res_string, | |
81 | uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask); | |
4fe3445a | 82 | |
ae412e7d BP |
83 | void flow_format(struct ds *, const struct flow *); |
84 | void flow_print(FILE *, const struct flow *); | |
79049a24 | 85 | static inline int flow_compare_3way(const struct flow *, const struct flow *); |
ae412e7d BP |
86 | static inline bool flow_equal(const struct flow *, const struct flow *); |
87 | static inline size_t flow_hash(const struct flow *, uint32_t basis); | |
064af421 | 88 | |
fb0451d9 | 89 | void flow_set_dl_vlan(struct flow *, ovs_be16 vid); |
cc34bc8c | 90 | void flow_set_vlan_vid(struct flow *, ovs_be16 vid); |
3719455c BP |
91 | void flow_set_vlan_pcp(struct flow *, uint8_t pcp); |
92 | ||
8bfd0fda BP |
93 | int flow_count_mpls_labels(const struct flow *, struct flow_wildcards *); |
94 | int flow_count_common_mpls_labels(const struct flow *a, int an, | |
95 | const struct flow *b, int bn, | |
96 | struct flow_wildcards *wc); | |
97 | void flow_push_mpls(struct flow *, int n, ovs_be16 mpls_eth_type, | |
742c0ac3 | 98 | struct flow_wildcards *, bool clear_flow_L3); |
8bfd0fda BP |
99 | bool flow_pop_mpls(struct flow *, int n, ovs_be16 eth_type, |
100 | struct flow_wildcards *); | |
101 | void flow_set_mpls_label(struct flow *, int idx, ovs_be32 label); | |
102 | void flow_set_mpls_ttl(struct flow *, int idx, uint8_t ttl); | |
103 | void flow_set_mpls_tc(struct flow *, int idx, uint8_t tc); | |
104 | void flow_set_mpls_bos(struct flow *, int idx, uint8_t stack); | |
105 | void flow_set_mpls_lse(struct flow *, int idx, ovs_be32 lse); | |
b02475c5 | 106 | |
cf62fa4c | 107 | void flow_compose(struct dp_packet *, const struct flow *); |
8b3b8dd1 | 108 | |
94a81e40 DDP |
109 | bool parse_ipv6_ext_hdrs(const void **datap, size_t *sizep, uint8_t *nw_proto, |
110 | uint8_t *nw_frag); | |
206b60d4 | 111 | ovs_be16 parse_dl_type(const struct eth_header *data_, size_t size); |
94a81e40 | 112 | |
79fe0f46 BP |
113 | static inline uint64_t |
114 | flow_get_xreg(const struct flow *flow, int idx) | |
115 | { | |
116 | return ((uint64_t) flow->regs[idx * 2] << 32) | flow->regs[idx * 2 + 1]; | |
117 | } | |
118 | ||
119 | static inline void | |
120 | flow_set_xreg(struct flow *flow, int idx, uint64_t value) | |
121 | { | |
122 | flow->regs[idx * 2] = value >> 32; | |
123 | flow->regs[idx * 2 + 1] = value; | |
124 | } | |
125 | ||
b23ada8e JP |
126 | static inline ovs_u128 |
127 | flow_get_xxreg(const struct flow *flow, int idx) | |
128 | { | |
129 | ovs_u128 value; | |
130 | ||
131 | value.u64.hi = (uint64_t) flow->regs[idx * 4] << 32; | |
132 | value.u64.hi |= flow->regs[idx * 4 + 1]; | |
133 | value.u64.lo = (uint64_t) flow->regs[idx * 4 + 2] << 32; | |
134 | value.u64.lo |= flow->regs[idx * 4 + 3]; | |
135 | ||
136 | return value; | |
137 | } | |
138 | ||
139 | static inline void | |
140 | flow_set_xxreg(struct flow *flow, int idx, ovs_u128 value) | |
141 | { | |
142 | flow->regs[idx * 4] = value.u64.hi >> 32; | |
143 | flow->regs[idx * 4 + 1] = value.u64.hi; | |
144 | flow->regs[idx * 4 + 2] = value.u64.lo >> 32; | |
145 | flow->regs[idx * 4 + 3] = value.u64.lo; | |
146 | } | |
147 | ||
064af421 | 148 | static inline int |
79049a24 | 149 | flow_compare_3way(const struct flow *a, const struct flow *b) |
064af421 | 150 | { |
16c6d0c3 | 151 | return memcmp(a, b, sizeof *a); |
064af421 BP |
152 | } |
153 | ||
154 | static inline bool | |
ae412e7d | 155 | flow_equal(const struct flow *a, const struct flow *b) |
064af421 | 156 | { |
79049a24 | 157 | return !flow_compare_3way(a, b); |
064af421 BP |
158 | } |
159 | ||
160 | static inline size_t | |
ae412e7d | 161 | flow_hash(const struct flow *flow, uint32_t basis) |
064af421 | 162 | { |
0a96a21b | 163 | return hash_bytes64((const uint64_t *)flow, sizeof *flow, basis); |
064af421 | 164 | } |
5cb7a798 | 165 | |
4e022ec0 AW |
166 | static inline uint16_t |
167 | ofp_to_u16(ofp_port_t ofp_port) | |
168 | { | |
169 | return (OVS_FORCE uint16_t) ofp_port; | |
170 | } | |
171 | ||
172 | static inline uint32_t | |
173 | odp_to_u32(odp_port_t odp_port) | |
174 | { | |
175 | return (OVS_FORCE uint32_t) odp_port; | |
176 | } | |
177 | ||
178 | static inline uint32_t | |
179 | ofp11_to_u32(ofp11_port_t ofp11_port) | |
180 | { | |
181 | return (OVS_FORCE uint32_t) ofp11_port; | |
182 | } | |
183 | ||
184 | static inline ofp_port_t | |
185 | u16_to_ofp(uint16_t port) | |
186 | { | |
187 | return OFP_PORT_C(port); | |
188 | } | |
189 | ||
190 | static inline odp_port_t | |
191 | u32_to_odp(uint32_t port) | |
192 | { | |
193 | return ODP_PORT_C(port); | |
194 | } | |
195 | ||
196 | static inline ofp11_port_t | |
197 | u32_to_ofp11(uint32_t port) | |
198 | { | |
199 | return OFP11_PORT_C(port); | |
200 | } | |
201 | ||
f9c0c3ec EJ |
202 | static inline uint32_t |
203 | hash_ofp_port(ofp_port_t ofp_port) | |
204 | { | |
205 | return hash_int(ofp_to_u16(ofp_port), 0); | |
206 | } | |
207 | ||
208 | static inline uint32_t | |
209 | hash_odp_port(odp_port_t odp_port) | |
210 | { | |
211 | return hash_int(odp_to_u32(odp_port), 0); | |
212 | } | |
659c2346 | 213 | \f |
63be20be | 214 | uint32_t flow_hash_5tuple(const struct flow *flow, uint32_t basis); |
ff55ea1f | 215 | uint32_t flow_hash_symmetric_l4(const struct flow *flow, uint32_t basis); |
4249b547 JB |
216 | uint32_t flow_hash_symmetric_l3l4(const struct flow *flow, uint32_t basis, |
217 | bool inc_udp_ports ); | |
b5d97350 | 218 | |
94639963 JR |
219 | /* Initialize a flow with random fields that matter for nx_hash_fields. */ |
220 | void flow_random_hash_fields(struct flow *); | |
6cdd5145 JP |
221 | void flow_mask_hash_fields(const struct flow *, struct flow_wildcards *, |
222 | enum nx_hash_fields); | |
520e9a2a EJ |
223 | uint32_t flow_hash_fields(const struct flow *, enum nx_hash_fields, |
224 | uint16_t basis); | |
225 | const char *flow_hash_fields_to_str(enum nx_hash_fields); | |
226 | bool flow_hash_fields_valid(enum nx_hash_fields); | |
db7f8281 | 227 | |
368eefac EJ |
228 | uint32_t flow_hash_in_wildcards(const struct flow *, |
229 | const struct flow_wildcards *, | |
230 | uint32_t basis); | |
231 | ||
659c2346 BP |
232 | bool flow_equal_except(const struct flow *a, const struct flow *b, |
233 | const struct flow_wildcards *); | |
5cb7a798 | 234 | \f |
5fcff47b JR |
235 | /* Bitmap for flow values. For each 1-bit the corresponding flow value is |
236 | * explicitly specified, other values are zeroes. | |
237 | * | |
238 | * map_t must be wide enough to hold any member of struct flow. */ | |
239 | typedef unsigned long long map_t; | |
240 | #define MAP_T_BITS (sizeof(map_t) * CHAR_BIT) | |
241 | #define MAP_1 (map_t)1 | |
242 | #define MAP_MAX TYPE_MAXIMUM(map_t) | |
243 | ||
244 | #define MAP_IS_SET(MAP, IDX) ((MAP) & (MAP_1 << (IDX))) | |
245 | ||
246 | /* Iterate through the indices of all 1-bits in 'MAP'. */ | |
247 | #define MAP_FOR_EACH_INDEX(IDX, MAP) \ | |
248 | ULLONG_FOR_EACH_1(IDX, MAP) | |
249 | ||
250 | #define FLOWMAP_UNITS DIV_ROUND_UP(FLOW_U64S, MAP_T_BITS) | |
251 | ||
252 | struct flowmap { | |
253 | map_t bits[FLOWMAP_UNITS]; | |
254 | }; | |
255 | ||
256 | #define FLOWMAP_EMPTY_INITIALIZER { { 0 } } | |
257 | ||
258 | static inline void flowmap_init(struct flowmap *); | |
259 | static inline bool flowmap_equal(struct flowmap, struct flowmap); | |
260 | static inline bool flowmap_is_set(const struct flowmap *, size_t idx); | |
261 | static inline bool flowmap_are_set(const struct flowmap *, size_t idx, | |
262 | unsigned int n_bits); | |
263 | static inline void flowmap_set(struct flowmap *, size_t idx, | |
264 | unsigned int n_bits); | |
265 | static inline void flowmap_clear(struct flowmap *, size_t idx, | |
266 | unsigned int n_bits); | |
267 | static inline struct flowmap flowmap_or(struct flowmap, struct flowmap); | |
268 | static inline struct flowmap flowmap_and(struct flowmap, struct flowmap); | |
269 | static inline bool flowmap_is_empty(struct flowmap); | |
270 | static inline unsigned int flowmap_n_1bits(struct flowmap); | |
271 | ||
272 | #define FLOWMAP_HAS_FIELD(FM, FIELD) \ | |
273 | flowmap_are_set(FM, FLOW_U64_OFFSET(FIELD), FLOW_U64_SIZE(FIELD)) | |
274 | ||
275 | #define FLOWMAP_SET(FM, FIELD) \ | |
276 | flowmap_set(FM, FLOW_U64_OFFSET(FIELD), FLOW_U64_SIZE(FIELD)) | |
277 | ||
278 | #define FLOWMAP_SET__(FM, FIELD, SIZE) \ | |
279 | flowmap_set(FM, FLOW_U64_OFFSET(FIELD), \ | |
280 | DIV_ROUND_UP(SIZE, sizeof(uint64_t))) | |
281 | ||
282 | /* XXX: Only works for full 64-bit units. */ | |
283 | #define FLOWMAP_CLEAR(FM, FIELD) \ | |
284 | BUILD_ASSERT_DECL(FLOW_U64_OFFREM(FIELD) == 0); \ | |
285 | BUILD_ASSERT_DECL(sizeof(((struct flow *)0)->FIELD) % sizeof(uint64_t) == 0); \ | |
286 | flowmap_clear(FM, FLOW_U64_OFFSET(FIELD), FLOW_U64_SIZE(FIELD)) | |
287 | ||
288 | /* Iterate through all units in 'FMAP'. */ | |
289 | #define FLOWMAP_FOR_EACH_UNIT(UNIT) \ | |
290 | for ((UNIT) = 0; (UNIT) < FLOWMAP_UNITS; (UNIT)++) | |
291 | ||
292 | /* Iterate through all map units in 'FMAP'. */ | |
293 | #define FLOWMAP_FOR_EACH_MAP(MAP, FLOWMAP) \ | |
294 | for (size_t unit__ = 0; \ | |
295 | unit__ < FLOWMAP_UNITS && ((MAP) = (FLOWMAP).bits[unit__], true); \ | |
296 | unit__++) | |
297 | ||
298 | struct flowmap_aux; | |
299 | static inline bool flowmap_next_index(struct flowmap_aux *, size_t *idx); | |
300 | ||
301 | #define FLOWMAP_AUX_INITIALIZER(FLOWMAP) { .unit = 0, .map = (FLOWMAP) } | |
302 | ||
303 | /* Iterate through all struct flow u64 indices specified by 'MAP'. This is a | |
304 | * slower but easier version of the FLOWMAP_FOR_EACH_MAP() & | |
305 | * MAP_FOR_EACH_INDEX() combination. */ | |
306 | #define FLOWMAP_FOR_EACH_INDEX(IDX, MAP) \ | |
307 | for (struct flowmap_aux aux__ = FLOWMAP_AUX_INITIALIZER(MAP); \ | |
308 | flowmap_next_index(&aux__, &(IDX));) | |
309 | ||
310 | /* Flowmap inline implementations. */ | |
311 | static inline void | |
312 | flowmap_init(struct flowmap *fm) | |
313 | { | |
314 | memset(fm, 0, sizeof *fm); | |
315 | } | |
316 | ||
317 | static inline bool | |
318 | flowmap_equal(struct flowmap a, struct flowmap b) | |
319 | { | |
320 | return !memcmp(&a, &b, sizeof a); | |
321 | } | |
322 | ||
323 | static inline bool | |
324 | flowmap_is_set(const struct flowmap *fm, size_t idx) | |
325 | { | |
326 | return (fm->bits[idx / MAP_T_BITS] & (MAP_1 << (idx % MAP_T_BITS))) != 0; | |
327 | } | |
328 | ||
329 | /* Returns 'true' if any of the 'n_bits' bits starting at 'idx' are set in | |
330 | * 'fm'. 'n_bits' can be at most MAP_T_BITS. */ | |
331 | static inline bool | |
332 | flowmap_are_set(const struct flowmap *fm, size_t idx, unsigned int n_bits) | |
333 | { | |
334 | map_t n_bits_mask = (MAP_1 << n_bits) - 1; | |
335 | size_t unit = idx / MAP_T_BITS; | |
336 | ||
337 | idx %= MAP_T_BITS; | |
5cb7a798 | 338 | |
5fcff47b JR |
339 | if (fm->bits[unit] & (n_bits_mask << idx)) { |
340 | return true; | |
341 | } | |
5ecf765c JR |
342 | /* The seemingly unnecessary bounds check on 'unit' is a workaround for a |
343 | * false-positive array out of bounds error by GCC 4.9. */ | |
344 | if (unit + 1 < FLOWMAP_UNITS && idx + n_bits > MAP_T_BITS) { | |
5fcff47b JR |
345 | /* Check the remaining bits from the next unit. */ |
346 | return fm->bits[unit + 1] & (n_bits_mask >> (MAP_T_BITS - idx)); | |
347 | } | |
348 | return false; | |
349 | } | |
361d808d | 350 | |
5fcff47b JR |
351 | /* Set the 'n_bits' consecutive bits in 'fm', starting at bit 'idx'. |
352 | * 'n_bits' can be at most MAP_T_BITS. */ | |
353 | static inline void | |
354 | flowmap_set(struct flowmap *fm, size_t idx, unsigned int n_bits) | |
355 | { | |
356 | map_t n_bits_mask = (MAP_1 << n_bits) - 1; | |
357 | size_t unit = idx / MAP_T_BITS; | |
358 | ||
359 | idx %= MAP_T_BITS; | |
360 | ||
361 | fm->bits[unit] |= n_bits_mask << idx; | |
5ecf765c JR |
362 | /* The seemingly unnecessary bounds check on 'unit' is a workaround for a |
363 | * false-positive array out of bounds error by GCC 4.9. */ | |
364 | if (unit + 1 < FLOWMAP_UNITS && idx + n_bits > MAP_T_BITS) { | |
5fcff47b JR |
365 | /* 'MAP_T_BITS - idx' bits were set on 'unit', set the remaining |
366 | * bits from the next unit. */ | |
367 | fm->bits[unit + 1] |= n_bits_mask >> (MAP_T_BITS - idx); | |
368 | } | |
369 | } | |
370 | ||
371 | /* Clears the 'n_bits' consecutive bits in 'fm', starting at bit 'idx'. | |
372 | * 'n_bits' can be at most MAP_T_BITS. */ | |
373 | static inline void | |
374 | flowmap_clear(struct flowmap *fm, size_t idx, unsigned int n_bits) | |
375 | { | |
376 | map_t n_bits_mask = (MAP_1 << n_bits) - 1; | |
377 | size_t unit = idx / MAP_T_BITS; | |
378 | ||
379 | idx %= MAP_T_BITS; | |
380 | ||
381 | fm->bits[unit] &= ~(n_bits_mask << idx); | |
5ecf765c JR |
382 | /* The seemingly unnecessary bounds check on 'unit' is a workaround for a |
383 | * false-positive array out of bounds error by GCC 4.9. */ | |
384 | if (unit + 1 < FLOWMAP_UNITS && idx + n_bits > MAP_T_BITS) { | |
5fcff47b JR |
385 | /* 'MAP_T_BITS - idx' bits were cleared on 'unit', clear the |
386 | * remaining bits from the next unit. */ | |
387 | fm->bits[unit + 1] &= ~(n_bits_mask >> (MAP_T_BITS - idx)); | |
388 | } | |
389 | } | |
390 | ||
391 | /* OR the bits in the flowmaps. */ | |
392 | static inline struct flowmap | |
aba5d6dd | 393 | flowmap_or(struct flowmap a, struct flowmap b) |
5fcff47b JR |
394 | { |
395 | struct flowmap map; | |
396 | size_t unit; | |
397 | ||
398 | FLOWMAP_FOR_EACH_UNIT (unit) { | |
399 | map.bits[unit] = a.bits[unit] | b.bits[unit]; | |
400 | } | |
401 | return map; | |
402 | } | |
403 | ||
404 | /* AND the bits in the flowmaps. */ | |
405 | static inline struct flowmap | |
aba5d6dd | 406 | flowmap_and(struct flowmap a, struct flowmap b) |
5fcff47b JR |
407 | { |
408 | struct flowmap map; | |
409 | size_t unit; | |
410 | ||
411 | FLOWMAP_FOR_EACH_UNIT (unit) { | |
412 | map.bits[unit] = a.bits[unit] & b.bits[unit]; | |
413 | } | |
414 | return map; | |
415 | } | |
416 | ||
417 | static inline bool | |
aba5d6dd | 418 | flowmap_is_empty(struct flowmap fm) |
5fcff47b JR |
419 | { |
420 | map_t map; | |
421 | ||
422 | FLOWMAP_FOR_EACH_MAP (map, fm) { | |
423 | if (map) { | |
424 | return false; | |
425 | } | |
426 | } | |
427 | return true; | |
428 | } | |
429 | ||
430 | static inline unsigned int | |
431 | flowmap_n_1bits(struct flowmap fm) | |
432 | { | |
433 | unsigned int n_1bits = 0; | |
434 | size_t unit; | |
435 | ||
436 | FLOWMAP_FOR_EACH_UNIT (unit) { | |
437 | n_1bits += count_1bits(fm.bits[unit]); | |
438 | } | |
439 | return n_1bits; | |
440 | } | |
441 | ||
442 | struct flowmap_aux { | |
443 | size_t unit; | |
444 | struct flowmap map; | |
445 | }; | |
446 | ||
447 | static inline bool | |
448 | flowmap_next_index(struct flowmap_aux *aux, size_t *idx) | |
449 | { | |
450 | for (;;) { | |
451 | map_t *map = &aux->map.bits[aux->unit]; | |
452 | if (*map) { | |
453 | *idx = aux->unit * MAP_T_BITS + raw_ctz(*map); | |
454 | *map = zero_rightmost_1bit(*map); | |
455 | return true; | |
456 | } | |
457 | if (++aux->unit >= FLOWMAP_UNITS) { | |
458 | return false; | |
459 | } | |
460 | } | |
461 | } | |
462 | ||
463 | \f | |
464 | /* Compressed flow. */ | |
5cb7a798 BP |
465 | |
466 | /* A sparse representation of a "struct flow". | |
467 | * | |
468 | * A "struct flow" is fairly large and tends to be mostly zeros. Sparse | |
8fd47924 JR |
469 | * representation has two advantages. First, it saves memory and, more |
470 | * importantly, minimizes the number of accessed cache lines. Second, it saves | |
471 | * time when the goal is to iterate over only the nonzero parts of the struct. | |
5cb7a798 | 472 | * |
5fcff47b | 473 | * The map member hold one bit for each uint64_t in a "struct flow". Each |
d70e8c28 | 474 | * 0-bit indicates that the corresponding uint64_t is zero, each 1-bit that it |
27bbe15d | 475 | * *may* be nonzero (see below how this applies to minimasks). |
5cb7a798 | 476 | * |
5fcff47b JR |
477 | * The values indicated by 'map' always follow the miniflow in memory. The |
478 | * user of the miniflow is responsible for always having enough storage after | |
479 | * the struct miniflow corresponding to the number of 1-bits in maps. | |
361d808d | 480 | * |
27bbe15d | 481 | * Elements in values array are allowed to be zero. This is useful for "struct |
df40c152 | 482 | * minimatch", for which ensuring that the miniflow and minimask members have |
361d808d | 483 | * same maps allows optimization. This allowance applies only to a miniflow |
09b0fa9c | 484 | * that is not a mask. That is, a minimask may NOT have zero elements in its |
361d808d | 485 | * values. |
8fd47924 | 486 | * |
361d808d JR |
487 | * A miniflow is always dynamically allocated so that the maps are followed by |
488 | * at least as many elements as there are 1-bits in maps. */ | |
5cb7a798 | 489 | struct miniflow { |
5fcff47b | 490 | struct flowmap map; |
361d808d JR |
491 | /* Followed by: |
492 | * uint64_t values[n]; | |
493 | * where 'n' is miniflow_n_values(miniflow). */ | |
5cb7a798 | 494 | }; |
5fcff47b | 495 | BUILD_ASSERT_DECL(sizeof(struct miniflow) % sizeof(uint64_t) == 0); |
5cb7a798 | 496 | |
d70e8c28 | 497 | #define MINIFLOW_VALUES_SIZE(COUNT) ((COUNT) * sizeof(uint64_t)) |
3016f3e4 | 498 | |
09b0fa9c JR |
499 | static inline uint64_t *miniflow_values(struct miniflow *mf) |
500 | { | |
501 | return (uint64_t *)(mf + 1); | |
502 | } | |
503 | ||
504 | static inline const uint64_t *miniflow_get_values(const struct miniflow *mf) | |
505 | { | |
506 | return (const uint64_t *)(mf + 1); | |
507 | } | |
508 | ||
419681da JR |
509 | struct pkt_metadata; |
510 | ||
8fd47924 JR |
511 | /* The 'dst' must follow with buffer space for FLOW_U64S 64-bit units. |
512 | * 'dst->map' is ignored on input and set on output to indicate which fields | |
513 | * were extracted. */ | |
cf62fa4c | 514 | void miniflow_extract(struct dp_packet *packet, struct miniflow *dst); |
ceb3bd67 | 515 | void miniflow_map_init(struct miniflow *, const struct flow *); |
5fcff47b | 516 | void flow_wc_map(const struct flow *, struct flowmap *); |
ceb3bd67 JR |
517 | size_t miniflow_alloc(struct miniflow *dsts[], size_t n, |
518 | const struct miniflow *src); | |
519 | void miniflow_init(struct miniflow *, const struct flow *); | |
a851eb94 JR |
520 | void miniflow_clone(struct miniflow *, const struct miniflow *, |
521 | size_t n_values); | |
8fd47924 | 522 | struct miniflow * miniflow_create(const struct flow *); |
5cb7a798 BP |
523 | |
524 | void miniflow_expand(const struct miniflow *, struct flow *); | |
525 | ||
d70e8c28 | 526 | static inline uint64_t flow_u64_value(const struct flow *flow, size_t index) |
a64759f0 | 527 | { |
361d808d | 528 | return ((uint64_t *)flow)[index]; |
a64759f0 JR |
529 | } |
530 | ||
d70e8c28 | 531 | static inline uint64_t *flow_u64_lvalue(struct flow *flow, size_t index) |
a64759f0 | 532 | { |
361d808d JR |
533 | return &((uint64_t *)flow)[index]; |
534 | } | |
535 | ||
536 | static inline size_t | |
537 | miniflow_n_values(const struct miniflow *flow) | |
538 | { | |
5fcff47b | 539 | return flowmap_n_1bits(flow->map); |
361d808d JR |
540 | } |
541 | ||
542 | struct flow_for_each_in_maps_aux { | |
5fcff47b JR |
543 | const struct flow *flow; |
544 | struct flowmap_aux map_aux; | |
361d808d JR |
545 | }; |
546 | ||
08feeb75 | 547 | static inline bool |
361d808d JR |
548 | flow_values_get_next_in_maps(struct flow_for_each_in_maps_aux *aux, |
549 | uint64_t *value) | |
3016f3e4 | 550 | { |
5fcff47b JR |
551 | size_t idx; |
552 | ||
553 | if (flowmap_next_index(&aux->map_aux, &idx)) { | |
554 | *value = flow_u64_value(aux->flow, idx); | |
3016f3e4 JR |
555 | return true; |
556 | } | |
557 | return false; | |
558 | } | |
559 | ||
5fcff47b | 560 | /* Iterate through all flow u64 values specified by 'MAPS'. */ |
361d808d JR |
561 | #define FLOW_FOR_EACH_IN_MAPS(VALUE, FLOW, MAPS) \ |
562 | for (struct flow_for_each_in_maps_aux aux__ \ | |
5fcff47b | 563 | = { (FLOW), FLOWMAP_AUX_INITIALIZER(MAPS) }; \ |
361d808d | 564 | flow_values_get_next_in_maps(&aux__, &(VALUE));) |
3016f3e4 | 565 | |
1f2e8d2d | 566 | struct mf_for_each_in_map_aux { |
d8a304b4 BB |
567 | size_t unit; /* Current 64-bit unit of the flowmaps |
568 | being processed. */ | |
569 | struct flowmap fmap; /* Remaining 1-bits corresponding to the | |
570 | 64-bit words in ‘values’ */ | |
571 | struct flowmap map; /* Remaining 1-bits corresponding to the | |
572 | 64-bit words of interest. */ | |
573 | const uint64_t *values; /* 64-bit words corresponding to the | |
574 | 1-bits in ‘fmap’. */ | |
1f2e8d2d | 575 | }; |
2abf78ff | 576 | |
d8a304b4 BB |
577 | /* Get the data from ‘aux->values’ corresponding to the next lowest 1-bit |
578 | * in ‘aux->map’, given that ‘aux->values’ points to an array of 64-bit | |
579 | * words corresponding to the 1-bits in ‘aux->fmap’, starting from the | |
580 | * rightmost 1-bit. | |
581 | * | |
582 | * Returns ’true’ if the traversal is incomplete, ‘false’ otherwise. | |
583 | * ‘aux’ is prepared for the next iteration after each call. | |
584 | * | |
585 | * This is used to traverse through, for example, the values in a miniflow | |
586 | * representation of a flow key selected by non-zero 64-bit words in a | |
587 | * corresponding subtable mask. */ | |
1f2e8d2d | 588 | static inline bool |
361d808d JR |
589 | mf_get_next_in_map(struct mf_for_each_in_map_aux *aux, |
590 | uint64_t *value) | |
1f2e8d2d | 591 | { |
5fcff47b JR |
592 | map_t *map, *fmap; |
593 | map_t rm1bit; | |
594 | ||
d8a304b4 | 595 | /* Skip empty map units. */ |
5fcff47b | 596 | while (OVS_UNLIKELY(!*(map = &aux->map.bits[aux->unit]))) { |
d8a304b4 BB |
597 | /* Skip remaining data in the current unit before advancing |
598 | * to the next. */ | |
5fcff47b JR |
599 | aux->values += count_1bits(aux->fmap.bits[aux->unit]); |
600 | if (++aux->unit == FLOWMAP_UNITS) { | |
601 | return false; | |
602 | } | |
603 | } | |
361d808d | 604 | |
5fcff47b JR |
605 | rm1bit = rightmost_1bit(*map); |
606 | *map -= rm1bit; | |
607 | fmap = &aux->fmap.bits[aux->unit]; | |
1f2e8d2d | 608 | |
d8a304b4 BB |
609 | /* If the rightmost 1-bit found from the current unit in ‘aux->map’ |
610 | * (‘rm1bit’) is also present in ‘aux->fmap’, store the corresponding | |
611 | * value from ‘aux->values’ to ‘*value', otherwise store 0. */ | |
5fcff47b | 612 | if (OVS_LIKELY(*fmap & rm1bit)) { |
d8a304b4 BB |
613 | /* Skip all 64-bit words in ‘values’ preceding the one corresponding |
614 | * to ‘rm1bit’. */ | |
5fcff47b | 615 | map_t trash = *fmap & (rm1bit - 1); |
361d808d | 616 | |
22ccf9c2 BB |
617 | /* Avoid resetting 'fmap' and calling count_1bits() when trash is |
618 | * zero. */ | |
619 | if (trash) { | |
620 | *fmap -= trash; | |
621 | aux->values += count_1bits(trash); | |
622 | } | |
361d808d | 623 | |
5fcff47b JR |
624 | *value = *aux->values; |
625 | } else { | |
626 | *value = 0; | |
2abf78ff | 627 | } |
5fcff47b | 628 | return true; |
2abf78ff JR |
629 | } |
630 | ||
5fcff47b JR |
631 | /* Iterate through miniflow u64 values specified by 'FLOWMAP'. */ |
632 | #define MINIFLOW_FOR_EACH_IN_FLOWMAP(VALUE, FLOW, FLOWMAP) \ | |
361d808d | 633 | for (struct mf_for_each_in_map_aux aux__ = \ |
5fcff47b | 634 | { 0, (FLOW)->map, (FLOWMAP), miniflow_get_values(FLOW) }; \ |
361d808d | 635 | mf_get_next_in_map(&aux__, &(VALUE));) |
2abf78ff | 636 | |
5fcff47b | 637 | /* This can be used when it is known that 'idx' is set in 'map'. */ |
361d808d | 638 | static inline const uint64_t * |
5fcff47b | 639 | miniflow_values_get__(const uint64_t *values, map_t map, size_t idx) |
f4d335e9 | 640 | { |
5fcff47b | 641 | return values + count_1bits(map & ((MAP_1 << idx) - 1)); |
f4d335e9 JR |
642 | } |
643 | ||
d70e8c28 | 644 | /* This can be used when it is known that 'u64_idx' is set in |
03690ebb | 645 | * the map of 'mf'. */ |
361d808d | 646 | static inline const uint64_t * |
5fcff47b JR |
647 | miniflow_get__(const struct miniflow *mf, size_t idx) |
648 | { | |
649 | const uint64_t *values = miniflow_get_values(mf); | |
650 | const map_t *map = mf->map.bits; | |
651 | ||
652 | while (idx >= MAP_T_BITS) { | |
653 | idx -= MAP_T_BITS; | |
654 | values += count_1bits(*map++); | |
655 | } | |
656 | return miniflow_values_get__(values, *map, idx); | |
657 | } | |
658 | ||
659 | #define MINIFLOW_IN_MAP(MF, IDX) flowmap_is_set(&(MF)->map, IDX) | |
660 | ||
661 | /* Get the value of the struct flow 'FIELD' as up to 8 byte wide integer type | |
662 | * 'TYPE' from miniflow 'MF'. */ | |
663 | #define MINIFLOW_GET_TYPE(MF, TYPE, FIELD) \ | |
664 | (MINIFLOW_IN_MAP(MF, FLOW_U64_OFFSET(FIELD)) \ | |
665 | ? ((OVS_FORCE const TYPE *)miniflow_get__(MF, FLOW_U64_OFFSET(FIELD))) \ | |
666 | [FLOW_U64_OFFREM(FIELD) / sizeof(TYPE)] \ | |
361d808d | 667 | : 0) |
28a560d9 | 668 | |
efba5ae4 JS |
669 | #define MINIFLOW_GET_U128(FLOW, FIELD) \ |
670 | (ovs_u128) { .u64 = { \ | |
671 | (MINIFLOW_IN_MAP(FLOW, FLOW_U64_OFFSET(FIELD)) ? \ | |
672 | *miniflow_get__(FLOW, FLOW_U64_OFFSET(FIELD)) : 0), \ | |
673 | (MINIFLOW_IN_MAP(FLOW, FLOW_U64_OFFSET(FIELD) + 1) ? \ | |
674 | *miniflow_get__(FLOW, FLOW_U64_OFFSET(FIELD) + 1) : 0) } } | |
9daf2348 | 675 | |
5fcff47b JR |
676 | #define MINIFLOW_GET_U8(FLOW, FIELD) \ |
677 | MINIFLOW_GET_TYPE(FLOW, uint8_t, FIELD) | |
678 | #define MINIFLOW_GET_U16(FLOW, FIELD) \ | |
679 | MINIFLOW_GET_TYPE(FLOW, uint16_t, FIELD) | |
680 | #define MINIFLOW_GET_BE16(FLOW, FIELD) \ | |
681 | MINIFLOW_GET_TYPE(FLOW, ovs_be16, FIELD) | |
682 | #define MINIFLOW_GET_U32(FLOW, FIELD) \ | |
683 | MINIFLOW_GET_TYPE(FLOW, uint32_t, FIELD) | |
684 | #define MINIFLOW_GET_BE32(FLOW, FIELD) \ | |
685 | MINIFLOW_GET_TYPE(FLOW, ovs_be32, FIELD) | |
686 | #define MINIFLOW_GET_U64(FLOW, FIELD) \ | |
687 | MINIFLOW_GET_TYPE(FLOW, uint64_t, FIELD) | |
688 | #define MINIFLOW_GET_BE64(FLOW, FIELD) \ | |
689 | MINIFLOW_GET_TYPE(FLOW, ovs_be64, FIELD) | |
d70e8c28 JR |
690 | |
691 | static inline uint64_t miniflow_get(const struct miniflow *, | |
692 | unsigned int u64_ofs); | |
693 | static inline uint32_t miniflow_get_u32(const struct miniflow *, | |
694 | unsigned int u32_ofs); | |
695 | static inline ovs_be32 miniflow_get_be32(const struct miniflow *, | |
696 | unsigned int be32_ofs); | |
b7807e4f JR |
697 | static inline uint16_t miniflow_get_vid(const struct miniflow *); |
698 | static inline uint16_t miniflow_get_tcp_flags(const struct miniflow *); | |
c906cedf | 699 | static inline ovs_be64 miniflow_get_metadata(const struct miniflow *); |
5cb7a798 BP |
700 | |
701 | bool miniflow_equal(const struct miniflow *a, const struct miniflow *b); | |
702 | bool miniflow_equal_in_minimask(const struct miniflow *a, | |
703 | const struct miniflow *b, | |
704 | const struct minimask *); | |
705 | bool miniflow_equal_flow_in_minimask(const struct miniflow *a, | |
706 | const struct flow *b, | |
707 | const struct minimask *); | |
4f150744 | 708 | uint32_t miniflow_hash_5tuple(const struct miniflow *flow, uint32_t basis); |
83916319 | 709 | |
5cb7a798 BP |
710 | \f |
711 | /* Compressed flow wildcards. */ | |
712 | ||
713 | /* A sparse representation of a "struct flow_wildcards". | |
714 | * | |
83916319 JR |
715 | * See the large comment on struct miniflow for details. |
716 | * | |
717 | * Note: While miniflow can have zero data for a 1-bit in the map, | |
718 | * a minimask may not! We rely on this in the implementation. */ | |
5cb7a798 BP |
719 | struct minimask { |
720 | struct miniflow masks; | |
721 | }; | |
722 | ||
ceb3bd67 | 723 | void minimask_init(struct minimask *, const struct flow_wildcards *); |
8fd47924 | 724 | struct minimask * minimask_create(const struct flow_wildcards *); |
5cb7a798 BP |
725 | void minimask_combine(struct minimask *dst, |
726 | const struct minimask *a, const struct minimask *b, | |
d70e8c28 | 727 | uint64_t storage[FLOW_U64S]); |
5cb7a798 BP |
728 | |
729 | void minimask_expand(const struct minimask *, struct flow_wildcards *); | |
730 | ||
d70e8c28 JR |
731 | static inline uint32_t minimask_get_u32(const struct minimask *, |
732 | unsigned int u32_ofs); | |
733 | static inline ovs_be32 minimask_get_be32(const struct minimask *, | |
734 | unsigned int be32_ofs); | |
b7807e4f | 735 | static inline uint16_t minimask_get_vid_mask(const struct minimask *); |
c906cedf | 736 | static inline ovs_be64 minimask_get_metadata_mask(const struct minimask *); |
5cb7a798 BP |
737 | |
738 | bool minimask_equal(const struct minimask *a, const struct minimask *b); | |
5cb7a798 | 739 | bool minimask_has_extra(const struct minimask *, const struct minimask *); |
d4570fd8 | 740 | |
27bbe15d | 741 | \f |
d4570fd8 JR |
742 | /* Returns true if 'mask' matches every packet, false if 'mask' fixes any bits |
743 | * or fields. */ | |
744 | static inline bool | |
745 | minimask_is_catchall(const struct minimask *mask) | |
746 | { | |
747 | /* For every 1-bit in mask's map, the corresponding value is non-zero, | |
748 | * so the only way the mask can not fix any bits or fields is for the | |
749 | * map the be zero. */ | |
5fcff47b | 750 | return flowmap_is_empty(mask->masks.map); |
d4570fd8 | 751 | } |
3d91d909 | 752 | |
d70e8c28 JR |
753 | /* Returns the uint64_t that would be at byte offset '8 * u64_ofs' if 'flow' |
754 | * were expanded into a "struct flow". */ | |
755 | static inline uint64_t miniflow_get(const struct miniflow *flow, | |
756 | unsigned int u64_ofs) | |
757 | { | |
5fcff47b | 758 | return MINIFLOW_IN_MAP(flow, u64_ofs) ? *miniflow_get__(flow, u64_ofs) : 0; |
d70e8c28 JR |
759 | } |
760 | ||
761 | static inline uint32_t miniflow_get_u32(const struct miniflow *flow, | |
762 | unsigned int u32_ofs) | |
763 | { | |
764 | uint64_t value = miniflow_get(flow, u32_ofs / 2); | |
765 | ||
766 | #if WORDS_BIGENDIAN | |
767 | return (u32_ofs & 1) ? value : value >> 32; | |
768 | #else | |
769 | return (u32_ofs & 1) ? value >> 32 : value; | |
770 | #endif | |
771 | } | |
772 | ||
773 | static inline ovs_be32 miniflow_get_be32(const struct miniflow *flow, | |
774 | unsigned int be32_ofs) | |
775 | { | |
776 | return (OVS_FORCE ovs_be32)miniflow_get_u32(flow, be32_ofs); | |
777 | } | |
778 | ||
b7807e4f JR |
779 | /* Returns the VID within the vlan_tci member of the "struct flow" represented |
780 | * by 'flow'. */ | |
781 | static inline uint16_t | |
782 | miniflow_get_vid(const struct miniflow *flow) | |
783 | { | |
28a560d9 | 784 | ovs_be16 tci = MINIFLOW_GET_BE16(flow, vlan_tci); |
b7807e4f JR |
785 | return vlan_tci_to_vid(tci); |
786 | } | |
787 | ||
d70e8c28 JR |
788 | /* Returns the uint32_t that would be at byte offset '4 * u32_ofs' if 'mask' |
789 | * were expanded into a "struct flow_wildcards". */ | |
790 | static inline uint32_t | |
791 | minimask_get_u32(const struct minimask *mask, unsigned int u32_ofs) | |
792 | { | |
793 | return miniflow_get_u32(&mask->masks, u32_ofs); | |
794 | } | |
795 | ||
796 | static inline ovs_be32 | |
797 | minimask_get_be32(const struct minimask *mask, unsigned int be32_ofs) | |
798 | { | |
799 | return (OVS_FORCE ovs_be32)minimask_get_u32(mask, be32_ofs); | |
800 | } | |
801 | ||
b7807e4f JR |
802 | /* Returns the VID mask within the vlan_tci member of the "struct |
803 | * flow_wildcards" represented by 'mask'. */ | |
804 | static inline uint16_t | |
805 | minimask_get_vid_mask(const struct minimask *mask) | |
806 | { | |
807 | return miniflow_get_vid(&mask->masks); | |
808 | } | |
809 | ||
810 | /* Returns the value of the "tcp_flags" field in 'flow'. */ | |
811 | static inline uint16_t | |
812 | miniflow_get_tcp_flags(const struct miniflow *flow) | |
813 | { | |
28a560d9 | 814 | return ntohs(MINIFLOW_GET_BE16(flow, tcp_flags)); |
b7807e4f JR |
815 | } |
816 | ||
c906cedf BP |
817 | /* Returns the value of the OpenFlow 1.1+ "metadata" field in 'flow'. */ |
818 | static inline ovs_be64 | |
819 | miniflow_get_metadata(const struct miniflow *flow) | |
820 | { | |
d70e8c28 | 821 | return MINIFLOW_GET_BE64(flow, metadata); |
c906cedf BP |
822 | } |
823 | ||
824 | /* Returns the mask for the OpenFlow 1.1+ "metadata" field in 'mask'. | |
825 | * | |
826 | * The return value is all-1-bits if 'mask' matches on the whole value of the | |
827 | * metadata field, all-0-bits if 'mask' entirely wildcards the metadata field, | |
828 | * or some other value if the metadata field is partially matched, partially | |
829 | * wildcarded. */ | |
830 | static inline ovs_be64 | |
831 | minimask_get_metadata_mask(const struct minimask *mask) | |
832 | { | |
d70e8c28 | 833 | return MINIFLOW_GET_BE64(&mask->masks, metadata); |
c906cedf | 834 | } |
659c2346 | 835 | |
5fcff47b JR |
836 | /* Perform a bitwise OR of miniflow 'src' flow data specified in 'subset' with |
837 | * the equivalent fields in 'dst', storing the result in 'dst'. 'subset' must | |
838 | * be a subset of 'src's map. */ | |
3d91d909 | 839 | static inline void |
5fcff47b JR |
840 | flow_union_with_miniflow_subset(struct flow *dst, const struct miniflow *src, |
841 | struct flowmap subset) | |
3d91d909 | 842 | { |
d70e8c28 | 843 | uint64_t *dst_u64 = (uint64_t *) dst; |
09b0fa9c | 844 | const uint64_t *p = miniflow_get_values(src); |
5fcff47b | 845 | map_t map; |
3d91d909 | 846 | |
5fcff47b JR |
847 | FLOWMAP_FOR_EACH_MAP (map, subset) { |
848 | size_t idx; | |
849 | ||
850 | MAP_FOR_EACH_INDEX(idx, map) { | |
851 | dst_u64[idx] |= *p++; | |
852 | } | |
853 | dst_u64 += MAP_T_BITS; | |
3d91d909 JR |
854 | } |
855 | } | |
856 | ||
5fcff47b JR |
857 | /* Perform a bitwise OR of miniflow 'src' flow data with the equivalent |
858 | * fields in 'dst', storing the result in 'dst'. */ | |
859 | static inline void | |
860 | flow_union_with_miniflow(struct flow *dst, const struct miniflow *src) | |
861 | { | |
862 | flow_union_with_miniflow_subset(dst, src, src->map); | |
863 | } | |
864 | ||
cf62fa4c PS |
865 | static inline void |
866 | pkt_metadata_from_flow(struct pkt_metadata *md, const struct flow *flow) | |
867 | { | |
868 | md->recirc_id = flow->recirc_id; | |
869 | md->dp_hash = flow->dp_hash; | |
59781952 | 870 | flow_tnl_copy__(&md->tunnel, &flow->tunnel); |
cf62fa4c PS |
871 | md->skb_priority = flow->skb_priority; |
872 | md->pkt_mark = flow->pkt_mark; | |
873 | md->in_port = flow->in_port; | |
07659514 JS |
874 | md->ct_state = flow->ct_state; |
875 | md->ct_zone = flow->ct_zone; | |
8e53fe8c | 876 | md->ct_mark = flow->ct_mark; |
9daf2348 | 877 | md->ct_label = flow->ct_label; |
b7807e4f JR |
878 | } |
879 | ||
05bb9148 DDP |
880 | /* Often, during translation we need to read a value from a flow('FLOW') and |
881 | * unwildcard the corresponding bits in the wildcards('WC'). This macro makes | |
882 | * it easier to do that. */ | |
883 | ||
884 | #define FLOW_WC_GET_AND_MASK_WC(FLOW, WC, FIELD) \ | |
885 | (((WC) ? WC_MASK_FIELD(WC, FIELD) : NULL), ((FLOW)->FIELD)) | |
886 | ||
aff49b8c JR |
887 | static inline bool is_vlan(const struct flow *flow, |
888 | struct flow_wildcards *wc) | |
889 | { | |
890 | if (wc) { | |
891 | WC_MASK_FIELD_MASK(wc, vlan_tci, htons(VLAN_CFI)); | |
892 | } | |
893 | return (flow->vlan_tci & htons(VLAN_CFI)) != 0; | |
894 | } | |
895 | ||
b7807e4f JR |
896 | static inline bool is_ip_any(const struct flow *flow) |
897 | { | |
898 | return dl_type_is_ip_any(flow->dl_type); | |
899 | } | |
900 | ||
aff49b8c JR |
901 | static inline bool is_ip_proto(const struct flow *flow, uint8_t ip_proto, |
902 | struct flow_wildcards *wc) | |
903 | { | |
904 | if (is_ip_any(flow)) { | |
905 | if (wc) { | |
906 | WC_MASK_FIELD(wc, nw_proto); | |
907 | } | |
908 | return flow->nw_proto == ip_proto; | |
909 | } | |
910 | return false; | |
911 | } | |
912 | ||
913 | static inline bool is_tcp(const struct flow *flow, | |
914 | struct flow_wildcards *wc) | |
915 | { | |
916 | return is_ip_proto(flow, IPPROTO_TCP, wc); | |
917 | } | |
918 | ||
919 | static inline bool is_udp(const struct flow *flow, | |
920 | struct flow_wildcards *wc) | |
921 | { | |
922 | return is_ip_proto(flow, IPPROTO_UDP, wc); | |
923 | } | |
924 | ||
925 | static inline bool is_sctp(const struct flow *flow, | |
926 | struct flow_wildcards *wc) | |
927 | { | |
928 | return is_ip_proto(flow, IPPROTO_SCTP, wc); | |
929 | } | |
930 | ||
a75636c8 BP |
931 | static inline bool is_icmpv4(const struct flow *flow, |
932 | struct flow_wildcards *wc) | |
b7807e4f | 933 | { |
a75636c8 BP |
934 | if (flow->dl_type == htons(ETH_TYPE_IP)) { |
935 | if (wc) { | |
936 | memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto); | |
937 | } | |
938 | return flow->nw_proto == IPPROTO_ICMP; | |
939 | } | |
940 | return false; | |
b7807e4f JR |
941 | } |
942 | ||
a75636c8 BP |
943 | static inline bool is_icmpv6(const struct flow *flow, |
944 | struct flow_wildcards *wc) | |
b7807e4f | 945 | { |
a75636c8 BP |
946 | if (flow->dl_type == htons(ETH_TYPE_IPV6)) { |
947 | if (wc) { | |
948 | memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto); | |
949 | } | |
950 | return flow->nw_proto == IPPROTO_ICMPV6; | |
951 | } | |
952 | return false; | |
b7807e4f JR |
953 | } |
954 | ||
c17fcc0a BP |
955 | static inline bool is_nd(const struct flow *flow, |
956 | struct flow_wildcards *wc) | |
957 | { | |
958 | if (is_icmpv6(flow, wc)) { | |
959 | if (wc) { | |
960 | memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst); | |
961 | } | |
962 | if (flow->tp_dst != htons(0)) { | |
963 | return false; | |
964 | } | |
965 | ||
966 | if (wc) { | |
967 | memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src); | |
968 | } | |
969 | return (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) || | |
970 | flow->tp_src == htons(ND_NEIGHBOR_ADVERT)); | |
971 | } | |
972 | return false; | |
973 | } | |
974 | ||
a75636c8 | 975 | static inline bool is_igmp(const struct flow *flow, struct flow_wildcards *wc) |
06994f87 | 976 | { |
a75636c8 BP |
977 | if (flow->dl_type == htons(ETH_TYPE_IP)) { |
978 | if (wc) { | |
979 | memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto); | |
980 | } | |
981 | return flow->nw_proto == IPPROTO_IGMP; | |
982 | } | |
983 | return false; | |
06994f87 TLSC |
984 | } |
985 | ||
a75636c8 BP |
986 | static inline bool is_mld(const struct flow *flow, |
987 | struct flow_wildcards *wc) | |
06994f87 | 988 | { |
a75636c8 BP |
989 | if (is_icmpv6(flow, wc)) { |
990 | if (wc) { | |
991 | memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src); | |
992 | } | |
993 | return (flow->tp_src == htons(MLD_QUERY) | |
994 | || flow->tp_src == htons(MLD_REPORT) | |
995 | || flow->tp_src == htons(MLD_DONE) | |
996 | || flow->tp_src == htons(MLD2_REPORT)); | |
997 | } | |
998 | return false; | |
06994f87 TLSC |
999 | } |
1000 | ||
a75636c8 BP |
1001 | static inline bool is_mld_query(const struct flow *flow, |
1002 | struct flow_wildcards *wc) | |
06994f87 | 1003 | { |
a75636c8 BP |
1004 | if (is_icmpv6(flow, wc)) { |
1005 | if (wc) { | |
1006 | memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src); | |
1007 | } | |
1008 | return flow->tp_src == htons(MLD_QUERY); | |
1009 | } | |
1010 | return false; | |
06994f87 TLSC |
1011 | } |
1012 | ||
a75636c8 BP |
1013 | static inline bool is_mld_report(const struct flow *flow, |
1014 | struct flow_wildcards *wc) | |
06994f87 | 1015 | { |
a75636c8 | 1016 | return is_mld(flow, wc) && !is_mld_query(flow, wc); |
06994f87 TLSC |
1017 | } |
1018 | ||
bbbca389 PK |
1019 | static inline bool is_stp(const struct flow *flow) |
1020 | { | |
1021 | return (eth_addr_equals(flow->dl_dst, eth_addr_stp) | |
1022 | && flow->dl_type == htons(FLOW_DL_TYPE_NONE)); | |
1023 | } | |
1024 | ||
064af421 | 1025 | #endif /* flow.h */ |