]> git.proxmox.com Git - ovs.git/blob - lib/flow.h
flow: New function is_nd().
[ovs.git] / lib / flow.h
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
3 *
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:
7 *
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.
15 */
16 #ifndef FLOW_H
17 #define FLOW_H 1
18
19 #include <sys/types.h>
20 #include <netinet/in.h>
21 #include <netinet/icmp6.h>
22 #include <stdbool.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include "bitmap.h"
26 #include "byte-order.h"
27 #include "openvswitch/compiler.h"
28 #include "openflow/nicira-ext.h"
29 #include "openflow/openflow.h"
30 #include "openvswitch/flow.h"
31 #include "packets.h"
32 #include "hash.h"
33 #include "util.h"
34
35 struct dpif_flow_stats;
36 struct ds;
37 struct flow_wildcards;
38 struct minimask;
39 struct dp_packet;
40 struct pkt_metadata;
41 struct match;
42
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. */
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) \
52 )
53
54 extern const uint8_t flow_segment_u64s[];
55
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
66 void flow_extract(struct dp_packet *, struct flow *);
67
68 void flow_zero_wildcards(struct flow *, const struct flow_wildcards *);
69 void flow_unwildcard_tp_ports(const struct flow *, struct flow_wildcards *);
70 void flow_get_metadata(const struct flow *, struct match *flow_metadata);
71
72 const char *ct_state_to_string(uint32_t state);
73 char *flow_to_string(const struct flow *);
74 void format_flags(struct ds *ds, const char *(*bit_to_string)(uint32_t),
75 uint32_t flags, char del);
76 void format_flags_masked(struct ds *ds, const char *name,
77 const char *(*bit_to_string)(uint32_t),
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);
82
83 void flow_format(struct ds *, const struct flow *);
84 void flow_print(FILE *, const struct flow *);
85 static inline int flow_compare_3way(const struct flow *, const struct flow *);
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);
88
89 void flow_set_dl_vlan(struct flow *, ovs_be16 vid);
90 void flow_set_vlan_vid(struct flow *, ovs_be16 vid);
91 void flow_set_vlan_pcp(struct flow *, uint8_t pcp);
92
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,
98 struct flow_wildcards *);
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);
106
107 void flow_compose(struct dp_packet *, const struct flow *);
108
109 static inline uint64_t
110 flow_get_xreg(const struct flow *flow, int idx)
111 {
112 return ((uint64_t) flow->regs[idx * 2] << 32) | flow->regs[idx * 2 + 1];
113 }
114
115 static inline void
116 flow_set_xreg(struct flow *flow, int idx, uint64_t value)
117 {
118 flow->regs[idx * 2] = value >> 32;
119 flow->regs[idx * 2 + 1] = value;
120 }
121
122 static inline int
123 flow_compare_3way(const struct flow *a, const struct flow *b)
124 {
125 return memcmp(a, b, sizeof *a);
126 }
127
128 static inline bool
129 flow_equal(const struct flow *a, const struct flow *b)
130 {
131 return !flow_compare_3way(a, b);
132 }
133
134 static inline size_t
135 flow_hash(const struct flow *flow, uint32_t basis)
136 {
137 return hash_bytes64((const uint64_t *)flow, sizeof *flow, basis);
138 }
139
140 static inline uint16_t
141 ofp_to_u16(ofp_port_t ofp_port)
142 {
143 return (OVS_FORCE uint16_t) ofp_port;
144 }
145
146 static inline uint32_t
147 odp_to_u32(odp_port_t odp_port)
148 {
149 return (OVS_FORCE uint32_t) odp_port;
150 }
151
152 static inline uint32_t
153 ofp11_to_u32(ofp11_port_t ofp11_port)
154 {
155 return (OVS_FORCE uint32_t) ofp11_port;
156 }
157
158 static inline ofp_port_t
159 u16_to_ofp(uint16_t port)
160 {
161 return OFP_PORT_C(port);
162 }
163
164 static inline odp_port_t
165 u32_to_odp(uint32_t port)
166 {
167 return ODP_PORT_C(port);
168 }
169
170 static inline ofp11_port_t
171 u32_to_ofp11(uint32_t port)
172 {
173 return OFP11_PORT_C(port);
174 }
175
176 static inline uint32_t
177 hash_ofp_port(ofp_port_t ofp_port)
178 {
179 return hash_int(ofp_to_u16(ofp_port), 0);
180 }
181
182 static inline uint32_t
183 hash_odp_port(odp_port_t odp_port)
184 {
185 return hash_int(odp_to_u32(odp_port), 0);
186 }
187 \f
188 uint32_t flow_hash_5tuple(const struct flow *flow, uint32_t basis);
189 uint32_t flow_hash_symmetric_l4(const struct flow *flow, uint32_t basis);
190 uint32_t flow_hash_symmetric_l3l4(const struct flow *flow, uint32_t basis,
191 bool inc_udp_ports );
192
193 /* Initialize a flow with random fields that matter for nx_hash_fields. */
194 void flow_random_hash_fields(struct flow *);
195 void flow_mask_hash_fields(const struct flow *, struct flow_wildcards *,
196 enum nx_hash_fields);
197 uint32_t flow_hash_fields(const struct flow *, enum nx_hash_fields,
198 uint16_t basis);
199 const char *flow_hash_fields_to_str(enum nx_hash_fields);
200 bool flow_hash_fields_valid(enum nx_hash_fields);
201
202 uint32_t flow_hash_in_wildcards(const struct flow *,
203 const struct flow_wildcards *,
204 uint32_t basis);
205
206 bool flow_equal_except(const struct flow *a, const struct flow *b,
207 const struct flow_wildcards *);
208 \f
209 /* Bitmap for flow values. For each 1-bit the corresponding flow value is
210 * explicitly specified, other values are zeroes.
211 *
212 * map_t must be wide enough to hold any member of struct flow. */
213 typedef unsigned long long map_t;
214 #define MAP_T_BITS (sizeof(map_t) * CHAR_BIT)
215 #define MAP_1 (map_t)1
216 #define MAP_MAX TYPE_MAXIMUM(map_t)
217
218 #define MAP_IS_SET(MAP, IDX) ((MAP) & (MAP_1 << (IDX)))
219
220 /* Iterate through the indices of all 1-bits in 'MAP'. */
221 #define MAP_FOR_EACH_INDEX(IDX, MAP) \
222 ULLONG_FOR_EACH_1(IDX, MAP)
223
224 #define FLOWMAP_UNITS DIV_ROUND_UP(FLOW_U64S, MAP_T_BITS)
225
226 struct flowmap {
227 map_t bits[FLOWMAP_UNITS];
228 };
229
230 #define FLOWMAP_EMPTY_INITIALIZER { { 0 } }
231
232 static inline void flowmap_init(struct flowmap *);
233 static inline bool flowmap_equal(struct flowmap, struct flowmap);
234 static inline bool flowmap_is_set(const struct flowmap *, size_t idx);
235 static inline bool flowmap_are_set(const struct flowmap *, size_t idx,
236 unsigned int n_bits);
237 static inline void flowmap_set(struct flowmap *, size_t idx,
238 unsigned int n_bits);
239 static inline void flowmap_clear(struct flowmap *, size_t idx,
240 unsigned int n_bits);
241 static inline struct flowmap flowmap_or(struct flowmap, struct flowmap);
242 static inline struct flowmap flowmap_and(struct flowmap, struct flowmap);
243 static inline bool flowmap_is_empty(struct flowmap);
244 static inline unsigned int flowmap_n_1bits(struct flowmap);
245
246 #define FLOWMAP_HAS_FIELD(FM, FIELD) \
247 flowmap_are_set(FM, FLOW_U64_OFFSET(FIELD), FLOW_U64_SIZE(FIELD))
248
249 #define FLOWMAP_SET(FM, FIELD) \
250 flowmap_set(FM, FLOW_U64_OFFSET(FIELD), FLOW_U64_SIZE(FIELD))
251
252 #define FLOWMAP_SET__(FM, FIELD, SIZE) \
253 flowmap_set(FM, FLOW_U64_OFFSET(FIELD), \
254 DIV_ROUND_UP(SIZE, sizeof(uint64_t)))
255
256 /* XXX: Only works for full 64-bit units. */
257 #define FLOWMAP_CLEAR(FM, FIELD) \
258 BUILD_ASSERT_DECL(FLOW_U64_OFFREM(FIELD) == 0); \
259 BUILD_ASSERT_DECL(sizeof(((struct flow *)0)->FIELD) % sizeof(uint64_t) == 0); \
260 flowmap_clear(FM, FLOW_U64_OFFSET(FIELD), FLOW_U64_SIZE(FIELD))
261
262 /* Iterate through all units in 'FMAP'. */
263 #define FLOWMAP_FOR_EACH_UNIT(UNIT) \
264 for ((UNIT) = 0; (UNIT) < FLOWMAP_UNITS; (UNIT)++)
265
266 /* Iterate through all map units in 'FMAP'. */
267 #define FLOWMAP_FOR_EACH_MAP(MAP, FLOWMAP) \
268 for (size_t unit__ = 0; \
269 unit__ < FLOWMAP_UNITS && ((MAP) = (FLOWMAP).bits[unit__], true); \
270 unit__++)
271
272 struct flowmap_aux;
273 static inline bool flowmap_next_index(struct flowmap_aux *, size_t *idx);
274
275 #define FLOWMAP_AUX_INITIALIZER(FLOWMAP) { .unit = 0, .map = (FLOWMAP) }
276
277 /* Iterate through all struct flow u64 indices specified by 'MAP'. This is a
278 * slower but easier version of the FLOWMAP_FOR_EACH_MAP() &
279 * MAP_FOR_EACH_INDEX() combination. */
280 #define FLOWMAP_FOR_EACH_INDEX(IDX, MAP) \
281 for (struct flowmap_aux aux__ = FLOWMAP_AUX_INITIALIZER(MAP); \
282 flowmap_next_index(&aux__, &(IDX));)
283
284 /* Flowmap inline implementations. */
285 static inline void
286 flowmap_init(struct flowmap *fm)
287 {
288 memset(fm, 0, sizeof *fm);
289 }
290
291 static inline bool
292 flowmap_equal(struct flowmap a, struct flowmap b)
293 {
294 return !memcmp(&a, &b, sizeof a);
295 }
296
297 static inline bool
298 flowmap_is_set(const struct flowmap *fm, size_t idx)
299 {
300 return (fm->bits[idx / MAP_T_BITS] & (MAP_1 << (idx % MAP_T_BITS))) != 0;
301 }
302
303 /* Returns 'true' if any of the 'n_bits' bits starting at 'idx' are set in
304 * 'fm'. 'n_bits' can be at most MAP_T_BITS. */
305 static inline bool
306 flowmap_are_set(const struct flowmap *fm, size_t idx, unsigned int n_bits)
307 {
308 map_t n_bits_mask = (MAP_1 << n_bits) - 1;
309 size_t unit = idx / MAP_T_BITS;
310
311 idx %= MAP_T_BITS;
312
313 if (fm->bits[unit] & (n_bits_mask << idx)) {
314 return true;
315 }
316 /* The seemingly unnecessary bounds check on 'unit' is a workaround for a
317 * false-positive array out of bounds error by GCC 4.9. */
318 if (unit + 1 < FLOWMAP_UNITS && idx + n_bits > MAP_T_BITS) {
319 /* Check the remaining bits from the next unit. */
320 return fm->bits[unit + 1] & (n_bits_mask >> (MAP_T_BITS - idx));
321 }
322 return false;
323 }
324
325 /* Set the 'n_bits' consecutive bits in 'fm', starting at bit 'idx'.
326 * 'n_bits' can be at most MAP_T_BITS. */
327 static inline void
328 flowmap_set(struct flowmap *fm, size_t idx, unsigned int n_bits)
329 {
330 map_t n_bits_mask = (MAP_1 << n_bits) - 1;
331 size_t unit = idx / MAP_T_BITS;
332
333 idx %= MAP_T_BITS;
334
335 fm->bits[unit] |= n_bits_mask << idx;
336 /* The seemingly unnecessary bounds check on 'unit' is a workaround for a
337 * false-positive array out of bounds error by GCC 4.9. */
338 if (unit + 1 < FLOWMAP_UNITS && idx + n_bits > MAP_T_BITS) {
339 /* 'MAP_T_BITS - idx' bits were set on 'unit', set the remaining
340 * bits from the next unit. */
341 fm->bits[unit + 1] |= n_bits_mask >> (MAP_T_BITS - idx);
342 }
343 }
344
345 /* Clears the 'n_bits' consecutive bits in 'fm', starting at bit 'idx'.
346 * 'n_bits' can be at most MAP_T_BITS. */
347 static inline void
348 flowmap_clear(struct flowmap *fm, size_t idx, unsigned int n_bits)
349 {
350 map_t n_bits_mask = (MAP_1 << n_bits) - 1;
351 size_t unit = idx / MAP_T_BITS;
352
353 idx %= MAP_T_BITS;
354
355 fm->bits[unit] &= ~(n_bits_mask << idx);
356 /* The seemingly unnecessary bounds check on 'unit' is a workaround for a
357 * false-positive array out of bounds error by GCC 4.9. */
358 if (unit + 1 < FLOWMAP_UNITS && idx + n_bits > MAP_T_BITS) {
359 /* 'MAP_T_BITS - idx' bits were cleared on 'unit', clear the
360 * remaining bits from the next unit. */
361 fm->bits[unit + 1] &= ~(n_bits_mask >> (MAP_T_BITS - idx));
362 }
363 }
364
365 /* OR the bits in the flowmaps. */
366 static inline struct flowmap
367 flowmap_or(struct flowmap a, struct flowmap b)
368 {
369 struct flowmap map;
370 size_t unit;
371
372 FLOWMAP_FOR_EACH_UNIT (unit) {
373 map.bits[unit] = a.bits[unit] | b.bits[unit];
374 }
375 return map;
376 }
377
378 /* AND the bits in the flowmaps. */
379 static inline struct flowmap
380 flowmap_and(struct flowmap a, struct flowmap b)
381 {
382 struct flowmap map;
383 size_t unit;
384
385 FLOWMAP_FOR_EACH_UNIT (unit) {
386 map.bits[unit] = a.bits[unit] & b.bits[unit];
387 }
388 return map;
389 }
390
391 static inline bool
392 flowmap_is_empty(struct flowmap fm)
393 {
394 map_t map;
395
396 FLOWMAP_FOR_EACH_MAP (map, fm) {
397 if (map) {
398 return false;
399 }
400 }
401 return true;
402 }
403
404 static inline unsigned int
405 flowmap_n_1bits(struct flowmap fm)
406 {
407 unsigned int n_1bits = 0;
408 size_t unit;
409
410 FLOWMAP_FOR_EACH_UNIT (unit) {
411 n_1bits += count_1bits(fm.bits[unit]);
412 }
413 return n_1bits;
414 }
415
416 struct flowmap_aux {
417 size_t unit;
418 struct flowmap map;
419 };
420
421 static inline bool
422 flowmap_next_index(struct flowmap_aux *aux, size_t *idx)
423 {
424 for (;;) {
425 map_t *map = &aux->map.bits[aux->unit];
426 if (*map) {
427 *idx = aux->unit * MAP_T_BITS + raw_ctz(*map);
428 *map = zero_rightmost_1bit(*map);
429 return true;
430 }
431 if (++aux->unit >= FLOWMAP_UNITS) {
432 return false;
433 }
434 }
435 }
436
437 \f
438 /* Compressed flow. */
439
440 /* A sparse representation of a "struct flow".
441 *
442 * A "struct flow" is fairly large and tends to be mostly zeros. Sparse
443 * representation has two advantages. First, it saves memory and, more
444 * importantly, minimizes the number of accessed cache lines. Second, it saves
445 * time when the goal is to iterate over only the nonzero parts of the struct.
446 *
447 * The map member hold one bit for each uint64_t in a "struct flow". Each
448 * 0-bit indicates that the corresponding uint64_t is zero, each 1-bit that it
449 * *may* be nonzero (see below how this applies to minimasks).
450 *
451 * The values indicated by 'map' always follow the miniflow in memory. The
452 * user of the miniflow is responsible for always having enough storage after
453 * the struct miniflow corresponding to the number of 1-bits in maps.
454 *
455 * Elements in values array are allowed to be zero. This is useful for "struct
456 * minimatch", for which ensuring that the miniflow and minimask members have
457 * same maps allows optimization. This allowance applies only to a miniflow
458 * that is not a mask. That is, a minimask may NOT have zero elements in its
459 * values.
460 *
461 * A miniflow is always dynamically allocated so that the maps are followed by
462 * at least as many elements as there are 1-bits in maps. */
463 struct miniflow {
464 struct flowmap map;
465 /* Followed by:
466 * uint64_t values[n];
467 * where 'n' is miniflow_n_values(miniflow). */
468 };
469 BUILD_ASSERT_DECL(sizeof(struct miniflow) % sizeof(uint64_t) == 0);
470
471 #define MINIFLOW_VALUES_SIZE(COUNT) ((COUNT) * sizeof(uint64_t))
472
473 static inline uint64_t *miniflow_values(struct miniflow *mf)
474 {
475 return (uint64_t *)(mf + 1);
476 }
477
478 static inline const uint64_t *miniflow_get_values(const struct miniflow *mf)
479 {
480 return (const uint64_t *)(mf + 1);
481 }
482
483 struct pkt_metadata;
484
485 /* The 'dst' must follow with buffer space for FLOW_U64S 64-bit units.
486 * 'dst->map' is ignored on input and set on output to indicate which fields
487 * were extracted. */
488 void miniflow_extract(struct dp_packet *packet, struct miniflow *dst);
489 void miniflow_map_init(struct miniflow *, const struct flow *);
490 void flow_wc_map(const struct flow *, struct flowmap *);
491 size_t miniflow_alloc(struct miniflow *dsts[], size_t n,
492 const struct miniflow *src);
493 void miniflow_init(struct miniflow *, const struct flow *);
494 void miniflow_clone(struct miniflow *, const struct miniflow *,
495 size_t n_values);
496 struct miniflow * miniflow_create(const struct flow *);
497
498 void miniflow_expand(const struct miniflow *, struct flow *);
499
500 static inline uint64_t flow_u64_value(const struct flow *flow, size_t index)
501 {
502 return ((uint64_t *)flow)[index];
503 }
504
505 static inline uint64_t *flow_u64_lvalue(struct flow *flow, size_t index)
506 {
507 return &((uint64_t *)flow)[index];
508 }
509
510 static inline size_t
511 miniflow_n_values(const struct miniflow *flow)
512 {
513 return flowmap_n_1bits(flow->map);
514 }
515
516 struct flow_for_each_in_maps_aux {
517 const struct flow *flow;
518 struct flowmap_aux map_aux;
519 };
520
521 static inline bool
522 flow_values_get_next_in_maps(struct flow_for_each_in_maps_aux *aux,
523 uint64_t *value)
524 {
525 size_t idx;
526
527 if (flowmap_next_index(&aux->map_aux, &idx)) {
528 *value = flow_u64_value(aux->flow, idx);
529 return true;
530 }
531 return false;
532 }
533
534 /* Iterate through all flow u64 values specified by 'MAPS'. */
535 #define FLOW_FOR_EACH_IN_MAPS(VALUE, FLOW, MAPS) \
536 for (struct flow_for_each_in_maps_aux aux__ \
537 = { (FLOW), FLOWMAP_AUX_INITIALIZER(MAPS) }; \
538 flow_values_get_next_in_maps(&aux__, &(VALUE));)
539
540 struct mf_for_each_in_map_aux {
541 size_t unit;
542 struct flowmap fmap;
543 struct flowmap map;
544 const uint64_t *values;
545 };
546
547 static inline bool
548 mf_get_next_in_map(struct mf_for_each_in_map_aux *aux,
549 uint64_t *value)
550 {
551 map_t *map, *fmap;
552 map_t rm1bit;
553
554 while (OVS_UNLIKELY(!*(map = &aux->map.bits[aux->unit]))) {
555 /* Skip remaining data in the previous unit. */
556 aux->values += count_1bits(aux->fmap.bits[aux->unit]);
557 if (++aux->unit == FLOWMAP_UNITS) {
558 return false;
559 }
560 }
561
562 rm1bit = rightmost_1bit(*map);
563 *map -= rm1bit;
564 fmap = &aux->fmap.bits[aux->unit];
565
566 if (OVS_LIKELY(*fmap & rm1bit)) {
567 map_t trash = *fmap & (rm1bit - 1);
568
569 *fmap -= trash;
570 /* count_1bits() is fast for systems where speed matters (e.g.,
571 * DPDK), so we don't try avoid using it.
572 * Advance 'aux->values' to point to the value for 'rm1bit'. */
573 aux->values += count_1bits(trash);
574
575 *value = *aux->values;
576 } else {
577 *value = 0;
578 }
579 return true;
580 }
581
582 /* Iterate through miniflow u64 values specified by 'FLOWMAP'. */
583 #define MINIFLOW_FOR_EACH_IN_FLOWMAP(VALUE, FLOW, FLOWMAP) \
584 for (struct mf_for_each_in_map_aux aux__ = \
585 { 0, (FLOW)->map, (FLOWMAP), miniflow_get_values(FLOW) }; \
586 mf_get_next_in_map(&aux__, &(VALUE));)
587
588 /* This can be used when it is known that 'idx' is set in 'map'. */
589 static inline const uint64_t *
590 miniflow_values_get__(const uint64_t *values, map_t map, size_t idx)
591 {
592 return values + count_1bits(map & ((MAP_1 << idx) - 1));
593 }
594
595 /* This can be used when it is known that 'u64_idx' is set in
596 * the map of 'mf'. */
597 static inline const uint64_t *
598 miniflow_get__(const struct miniflow *mf, size_t idx)
599 {
600 const uint64_t *values = miniflow_get_values(mf);
601 const map_t *map = mf->map.bits;
602
603 while (idx >= MAP_T_BITS) {
604 idx -= MAP_T_BITS;
605 values += count_1bits(*map++);
606 }
607 return miniflow_values_get__(values, *map, idx);
608 }
609
610 #define MINIFLOW_IN_MAP(MF, IDX) flowmap_is_set(&(MF)->map, IDX)
611
612 /* Get the value of the struct flow 'FIELD' as up to 8 byte wide integer type
613 * 'TYPE' from miniflow 'MF'. */
614 #define MINIFLOW_GET_TYPE(MF, TYPE, FIELD) \
615 (MINIFLOW_IN_MAP(MF, FLOW_U64_OFFSET(FIELD)) \
616 ? ((OVS_FORCE const TYPE *)miniflow_get__(MF, FLOW_U64_OFFSET(FIELD))) \
617 [FLOW_U64_OFFREM(FIELD) / sizeof(TYPE)] \
618 : 0)
619
620 #define MINIFLOW_GET_U128(FLOW, FIELD) \
621 (ovs_u128) { .u64 = { \
622 (MINIFLOW_IN_MAP(FLOW, FLOW_U64_OFFSET(FIELD)) ? \
623 *miniflow_get__(FLOW, FLOW_U64_OFFSET(FIELD)) : 0), \
624 (MINIFLOW_IN_MAP(FLOW, FLOW_U64_OFFSET(FIELD) + 1) ? \
625 *miniflow_get__(FLOW, FLOW_U64_OFFSET(FIELD) + 1) : 0) } }
626
627 #define MINIFLOW_GET_U8(FLOW, FIELD) \
628 MINIFLOW_GET_TYPE(FLOW, uint8_t, FIELD)
629 #define MINIFLOW_GET_U16(FLOW, FIELD) \
630 MINIFLOW_GET_TYPE(FLOW, uint16_t, FIELD)
631 #define MINIFLOW_GET_BE16(FLOW, FIELD) \
632 MINIFLOW_GET_TYPE(FLOW, ovs_be16, FIELD)
633 #define MINIFLOW_GET_U32(FLOW, FIELD) \
634 MINIFLOW_GET_TYPE(FLOW, uint32_t, FIELD)
635 #define MINIFLOW_GET_BE32(FLOW, FIELD) \
636 MINIFLOW_GET_TYPE(FLOW, ovs_be32, FIELD)
637 #define MINIFLOW_GET_U64(FLOW, FIELD) \
638 MINIFLOW_GET_TYPE(FLOW, uint64_t, FIELD)
639 #define MINIFLOW_GET_BE64(FLOW, FIELD) \
640 MINIFLOW_GET_TYPE(FLOW, ovs_be64, FIELD)
641
642 static inline uint64_t miniflow_get(const struct miniflow *,
643 unsigned int u64_ofs);
644 static inline uint32_t miniflow_get_u32(const struct miniflow *,
645 unsigned int u32_ofs);
646 static inline ovs_be32 miniflow_get_be32(const struct miniflow *,
647 unsigned int be32_ofs);
648 static inline uint16_t miniflow_get_vid(const struct miniflow *);
649 static inline uint16_t miniflow_get_tcp_flags(const struct miniflow *);
650 static inline ovs_be64 miniflow_get_metadata(const struct miniflow *);
651
652 bool miniflow_equal(const struct miniflow *a, const struct miniflow *b);
653 bool miniflow_equal_in_minimask(const struct miniflow *a,
654 const struct miniflow *b,
655 const struct minimask *);
656 bool miniflow_equal_flow_in_minimask(const struct miniflow *a,
657 const struct flow *b,
658 const struct minimask *);
659 uint32_t miniflow_hash_5tuple(const struct miniflow *flow, uint32_t basis);
660
661 \f
662 /* Compressed flow wildcards. */
663
664 /* A sparse representation of a "struct flow_wildcards".
665 *
666 * See the large comment on struct miniflow for details.
667 *
668 * Note: While miniflow can have zero data for a 1-bit in the map,
669 * a minimask may not! We rely on this in the implementation. */
670 struct minimask {
671 struct miniflow masks;
672 };
673
674 void minimask_init(struct minimask *, const struct flow_wildcards *);
675 struct minimask * minimask_create(const struct flow_wildcards *);
676 void minimask_combine(struct minimask *dst,
677 const struct minimask *a, const struct minimask *b,
678 uint64_t storage[FLOW_U64S]);
679
680 void minimask_expand(const struct minimask *, struct flow_wildcards *);
681
682 static inline uint32_t minimask_get_u32(const struct minimask *,
683 unsigned int u32_ofs);
684 static inline ovs_be32 minimask_get_be32(const struct minimask *,
685 unsigned int be32_ofs);
686 static inline uint16_t minimask_get_vid_mask(const struct minimask *);
687 static inline ovs_be64 minimask_get_metadata_mask(const struct minimask *);
688
689 bool minimask_equal(const struct minimask *a, const struct minimask *b);
690 bool minimask_has_extra(const struct minimask *, const struct minimask *);
691
692 \f
693 /* Returns true if 'mask' matches every packet, false if 'mask' fixes any bits
694 * or fields. */
695 static inline bool
696 minimask_is_catchall(const struct minimask *mask)
697 {
698 /* For every 1-bit in mask's map, the corresponding value is non-zero,
699 * so the only way the mask can not fix any bits or fields is for the
700 * map the be zero. */
701 return flowmap_is_empty(mask->masks.map);
702 }
703
704 /* Returns the uint64_t that would be at byte offset '8 * u64_ofs' if 'flow'
705 * were expanded into a "struct flow". */
706 static inline uint64_t miniflow_get(const struct miniflow *flow,
707 unsigned int u64_ofs)
708 {
709 return MINIFLOW_IN_MAP(flow, u64_ofs) ? *miniflow_get__(flow, u64_ofs) : 0;
710 }
711
712 static inline uint32_t miniflow_get_u32(const struct miniflow *flow,
713 unsigned int u32_ofs)
714 {
715 uint64_t value = miniflow_get(flow, u32_ofs / 2);
716
717 #if WORDS_BIGENDIAN
718 return (u32_ofs & 1) ? value : value >> 32;
719 #else
720 return (u32_ofs & 1) ? value >> 32 : value;
721 #endif
722 }
723
724 static inline ovs_be32 miniflow_get_be32(const struct miniflow *flow,
725 unsigned int be32_ofs)
726 {
727 return (OVS_FORCE ovs_be32)miniflow_get_u32(flow, be32_ofs);
728 }
729
730 /* Returns the VID within the vlan_tci member of the "struct flow" represented
731 * by 'flow'. */
732 static inline uint16_t
733 miniflow_get_vid(const struct miniflow *flow)
734 {
735 ovs_be16 tci = MINIFLOW_GET_BE16(flow, vlan_tci);
736 return vlan_tci_to_vid(tci);
737 }
738
739 /* Returns the uint32_t that would be at byte offset '4 * u32_ofs' if 'mask'
740 * were expanded into a "struct flow_wildcards". */
741 static inline uint32_t
742 minimask_get_u32(const struct minimask *mask, unsigned int u32_ofs)
743 {
744 return miniflow_get_u32(&mask->masks, u32_ofs);
745 }
746
747 static inline ovs_be32
748 minimask_get_be32(const struct minimask *mask, unsigned int be32_ofs)
749 {
750 return (OVS_FORCE ovs_be32)minimask_get_u32(mask, be32_ofs);
751 }
752
753 /* Returns the VID mask within the vlan_tci member of the "struct
754 * flow_wildcards" represented by 'mask'. */
755 static inline uint16_t
756 minimask_get_vid_mask(const struct minimask *mask)
757 {
758 return miniflow_get_vid(&mask->masks);
759 }
760
761 /* Returns the value of the "tcp_flags" field in 'flow'. */
762 static inline uint16_t
763 miniflow_get_tcp_flags(const struct miniflow *flow)
764 {
765 return ntohs(MINIFLOW_GET_BE16(flow, tcp_flags));
766 }
767
768 /* Returns the value of the OpenFlow 1.1+ "metadata" field in 'flow'. */
769 static inline ovs_be64
770 miniflow_get_metadata(const struct miniflow *flow)
771 {
772 return MINIFLOW_GET_BE64(flow, metadata);
773 }
774
775 /* Returns the mask for the OpenFlow 1.1+ "metadata" field in 'mask'.
776 *
777 * The return value is all-1-bits if 'mask' matches on the whole value of the
778 * metadata field, all-0-bits if 'mask' entirely wildcards the metadata field,
779 * or some other value if the metadata field is partially matched, partially
780 * wildcarded. */
781 static inline ovs_be64
782 minimask_get_metadata_mask(const struct minimask *mask)
783 {
784 return MINIFLOW_GET_BE64(&mask->masks, metadata);
785 }
786
787 /* Perform a bitwise OR of miniflow 'src' flow data specified in 'subset' with
788 * the equivalent fields in 'dst', storing the result in 'dst'. 'subset' must
789 * be a subset of 'src's map. */
790 static inline void
791 flow_union_with_miniflow_subset(struct flow *dst, const struct miniflow *src,
792 struct flowmap subset)
793 {
794 uint64_t *dst_u64 = (uint64_t *) dst;
795 const uint64_t *p = miniflow_get_values(src);
796 map_t map;
797
798 FLOWMAP_FOR_EACH_MAP (map, subset) {
799 size_t idx;
800
801 MAP_FOR_EACH_INDEX(idx, map) {
802 dst_u64[idx] |= *p++;
803 }
804 dst_u64 += MAP_T_BITS;
805 }
806 }
807
808 /* Perform a bitwise OR of miniflow 'src' flow data with the equivalent
809 * fields in 'dst', storing the result in 'dst'. */
810 static inline void
811 flow_union_with_miniflow(struct flow *dst, const struct miniflow *src)
812 {
813 flow_union_with_miniflow_subset(dst, src, src->map);
814 }
815
816 static inline void
817 pkt_metadata_from_flow(struct pkt_metadata *md, const struct flow *flow)
818 {
819 md->recirc_id = flow->recirc_id;
820 md->dp_hash = flow->dp_hash;
821 flow_tnl_copy__(&md->tunnel, &flow->tunnel);
822 md->skb_priority = flow->skb_priority;
823 md->pkt_mark = flow->pkt_mark;
824 md->in_port = flow->in_port;
825 md->ct_state = flow->ct_state;
826 md->ct_zone = flow->ct_zone;
827 md->ct_mark = flow->ct_mark;
828 md->ct_label = flow->ct_label;
829 }
830
831 static inline bool is_ip_any(const struct flow *flow)
832 {
833 return dl_type_is_ip_any(flow->dl_type);
834 }
835
836 static inline bool is_icmpv4(const struct flow *flow,
837 struct flow_wildcards *wc)
838 {
839 if (flow->dl_type == htons(ETH_TYPE_IP)) {
840 if (wc) {
841 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
842 }
843 return flow->nw_proto == IPPROTO_ICMP;
844 }
845 return false;
846 }
847
848 static inline bool is_icmpv6(const struct flow *flow,
849 struct flow_wildcards *wc)
850 {
851 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
852 if (wc) {
853 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
854 }
855 return flow->nw_proto == IPPROTO_ICMPV6;
856 }
857 return false;
858 }
859
860 static inline bool is_nd(const struct flow *flow,
861 struct flow_wildcards *wc)
862 {
863 if (is_icmpv6(flow, wc)) {
864 if (wc) {
865 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
866 }
867 if (flow->tp_dst != htons(0)) {
868 return false;
869 }
870
871 if (wc) {
872 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
873 }
874 return (flow->tp_src == htons(ND_NEIGHBOR_SOLICIT) ||
875 flow->tp_src == htons(ND_NEIGHBOR_ADVERT));
876 }
877 return false;
878 }
879
880 static inline bool is_igmp(const struct flow *flow, struct flow_wildcards *wc)
881 {
882 if (flow->dl_type == htons(ETH_TYPE_IP)) {
883 if (wc) {
884 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
885 }
886 return flow->nw_proto == IPPROTO_IGMP;
887 }
888 return false;
889 }
890
891 static inline bool is_mld(const struct flow *flow,
892 struct flow_wildcards *wc)
893 {
894 if (is_icmpv6(flow, wc)) {
895 if (wc) {
896 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
897 }
898 return (flow->tp_src == htons(MLD_QUERY)
899 || flow->tp_src == htons(MLD_REPORT)
900 || flow->tp_src == htons(MLD_DONE)
901 || flow->tp_src == htons(MLD2_REPORT));
902 }
903 return false;
904 }
905
906 static inline bool is_mld_query(const struct flow *flow,
907 struct flow_wildcards *wc)
908 {
909 if (is_icmpv6(flow, wc)) {
910 if (wc) {
911 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
912 }
913 return flow->tp_src == htons(MLD_QUERY);
914 }
915 return false;
916 }
917
918 static inline bool is_mld_report(const struct flow *flow,
919 struct flow_wildcards *wc)
920 {
921 return is_mld(flow, wc) && !is_mld_query(flow, wc);
922 }
923
924 static inline bool is_stp(const struct flow *flow)
925 {
926 return (eth_addr_equals(flow->dl_dst, eth_addr_stp)
927 && flow->dl_type == htons(FLOW_DL_TYPE_NONE));
928 }
929
930 #endif /* flow.h */