2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <sys/types.h>
20 #include <netinet/in.h>
21 #include <netinet/icmp6.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"
35 struct dpif_flow_stats
;
37 struct flow_wildcards
;
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) \
54 extern const uint8_t flow_segment_u64s
[];
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))
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), \
66 void flow_extract(struct dp_packet
*, struct flow
*);
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
);
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
);
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
);
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
);
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
);
107 void flow_compose(struct dp_packet
*, const struct flow
*);
109 static inline uint64_t
110 flow_get_xreg(const struct flow
*flow
, int idx
)
112 return ((uint64_t) flow
->regs
[idx
* 2] << 32) | flow
->regs
[idx
* 2 + 1];
116 flow_set_xreg(struct flow
*flow
, int idx
, uint64_t value
)
118 flow
->regs
[idx
* 2] = value
>> 32;
119 flow
->regs
[idx
* 2 + 1] = value
;
123 flow_compare_3way(const struct flow
*a
, const struct flow
*b
)
125 return memcmp(a
, b
, sizeof *a
);
129 flow_equal(const struct flow
*a
, const struct flow
*b
)
131 return !flow_compare_3way(a
, b
);
135 flow_hash(const struct flow
*flow
, uint32_t basis
)
137 return hash_bytes64((const uint64_t *)flow
, sizeof *flow
, basis
);
140 static inline uint16_t
141 ofp_to_u16(ofp_port_t ofp_port
)
143 return (OVS_FORCE
uint16_t) ofp_port
;
146 static inline uint32_t
147 odp_to_u32(odp_port_t odp_port
)
149 return (OVS_FORCE
uint32_t) odp_port
;
152 static inline uint32_t
153 ofp11_to_u32(ofp11_port_t ofp11_port
)
155 return (OVS_FORCE
uint32_t) ofp11_port
;
158 static inline ofp_port_t
159 u16_to_ofp(uint16_t port
)
161 return OFP_PORT_C(port
);
164 static inline odp_port_t
165 u32_to_odp(uint32_t port
)
167 return ODP_PORT_C(port
);
170 static inline ofp11_port_t
171 u32_to_ofp11(uint32_t port
)
173 return OFP11_PORT_C(port
);
176 static inline uint32_t
177 hash_ofp_port(ofp_port_t ofp_port
)
179 return hash_int(ofp_to_u16(ofp_port
), 0);
182 static inline uint32_t
183 hash_odp_port(odp_port_t odp_port
)
185 return hash_int(odp_to_u32(odp_port
), 0);
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
);
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
,
199 const char *flow_hash_fields_to_str(enum nx_hash_fields
);
200 bool flow_hash_fields_valid(enum nx_hash_fields
);
202 uint32_t flow_hash_in_wildcards(const struct flow
*,
203 const struct flow_wildcards
*,
206 bool flow_equal_except(const struct flow
*a
, const struct flow
*b
,
207 const struct flow_wildcards
*);
209 /* Bitmap for flow values. For each 1-bit the corresponding flow value is
210 * explicitly specified, other values are zeroes.
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)
218 #define MAP_IS_SET(MAP, IDX) ((MAP) & (MAP_1 << (IDX)))
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)
224 #define FLOWMAP_UNITS DIV_ROUND_UP(FLOW_U64S, MAP_T_BITS)
227 map_t bits
[FLOWMAP_UNITS
];
230 #define FLOWMAP_EMPTY_INITIALIZER { { 0 } }
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
);
246 #define FLOWMAP_HAS_FIELD(FM, FIELD) \
247 flowmap_are_set(FM, FLOW_U64_OFFSET(FIELD), FLOW_U64_SIZE(FIELD))
249 #define FLOWMAP_SET(FM, FIELD) \
250 flowmap_set(FM, FLOW_U64_OFFSET(FIELD), FLOW_U64_SIZE(FIELD))
252 #define FLOWMAP_SET__(FM, FIELD, SIZE) \
253 flowmap_set(FM, FLOW_U64_OFFSET(FIELD), \
254 DIV_ROUND_UP(SIZE, sizeof(uint64_t)))
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))
262 /* Iterate through all units in 'FMAP'. */
263 #define FLOWMAP_FOR_EACH_UNIT(UNIT) \
264 for ((UNIT) = 0; (UNIT) < FLOWMAP_UNITS; (UNIT)++)
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); \
273 static inline bool flowmap_next_index(struct flowmap_aux
*, size_t *idx
);
275 #define FLOWMAP_AUX_INITIALIZER(FLOWMAP) { .unit = 0, .map = (FLOWMAP) }
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));)
284 /* Flowmap inline implementations. */
286 flowmap_init(struct flowmap
*fm
)
288 memset(fm
, 0, sizeof *fm
);
292 flowmap_equal(struct flowmap a
, struct flowmap b
)
294 return !memcmp(&a
, &b
, sizeof a
);
298 flowmap_is_set(const struct flowmap
*fm
, size_t idx
)
300 return (fm
->bits
[idx
/ MAP_T_BITS
] & (MAP_1
<< (idx
% MAP_T_BITS
))) != 0;
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. */
306 flowmap_are_set(const struct flowmap
*fm
, size_t idx
, unsigned int n_bits
)
308 map_t n_bits_mask
= (MAP_1
<< n_bits
) - 1;
309 size_t unit
= idx
/ MAP_T_BITS
;
313 if (fm
->bits
[unit
] & (n_bits_mask
<< idx
)) {
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
));
325 /* Set the 'n_bits' consecutive bits in 'fm', starting at bit 'idx'.
326 * 'n_bits' can be at most MAP_T_BITS. */
328 flowmap_set(struct flowmap
*fm
, size_t idx
, unsigned int n_bits
)
330 map_t n_bits_mask
= (MAP_1
<< n_bits
) - 1;
331 size_t unit
= idx
/ MAP_T_BITS
;
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
);
345 /* Clears the 'n_bits' consecutive bits in 'fm', starting at bit 'idx'.
346 * 'n_bits' can be at most MAP_T_BITS. */
348 flowmap_clear(struct flowmap
*fm
, size_t idx
, unsigned int n_bits
)
350 map_t n_bits_mask
= (MAP_1
<< n_bits
) - 1;
351 size_t unit
= idx
/ MAP_T_BITS
;
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
));
365 /* OR the bits in the flowmaps. */
366 static inline struct flowmap
367 flowmap_or(struct flowmap a
, struct flowmap b
)
372 FLOWMAP_FOR_EACH_UNIT (unit
) {
373 map
.bits
[unit
] = a
.bits
[unit
] | b
.bits
[unit
];
378 /* AND the bits in the flowmaps. */
379 static inline struct flowmap
380 flowmap_and(struct flowmap a
, struct flowmap b
)
385 FLOWMAP_FOR_EACH_UNIT (unit
) {
386 map
.bits
[unit
] = a
.bits
[unit
] & b
.bits
[unit
];
392 flowmap_is_empty(struct flowmap fm
)
396 FLOWMAP_FOR_EACH_MAP (map
, fm
) {
404 static inline unsigned int
405 flowmap_n_1bits(struct flowmap fm
)
407 unsigned int n_1bits
= 0;
410 FLOWMAP_FOR_EACH_UNIT (unit
) {
411 n_1bits
+= count_1bits(fm
.bits
[unit
]);
422 flowmap_next_index(struct flowmap_aux
*aux
, size_t *idx
)
425 map_t
*map
= &aux
->map
.bits
[aux
->unit
];
427 *idx
= aux
->unit
* MAP_T_BITS
+ raw_ctz(*map
);
428 *map
= zero_rightmost_1bit(*map
);
431 if (++aux
->unit
>= FLOWMAP_UNITS
) {
438 /* Compressed flow. */
440 /* A sparse representation of a "struct flow".
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.
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).
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.
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
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. */
466 * uint64_t values[n];
467 * where 'n' is miniflow_n_values(miniflow). */
469 BUILD_ASSERT_DECL(sizeof(struct miniflow
) % sizeof(uint64_t) == 0);
471 #define MINIFLOW_VALUES_SIZE(COUNT) ((COUNT) * sizeof(uint64_t))
473 static inline uint64_t *miniflow_values(struct miniflow
*mf
)
475 return (uint64_t *)(mf
+ 1);
478 static inline const uint64_t *miniflow_get_values(const struct miniflow
*mf
)
480 return (const uint64_t *)(mf
+ 1);
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
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
*,
496 struct miniflow
* miniflow_create(const struct flow
*);
498 void miniflow_expand(const struct miniflow
*, struct flow
*);
500 static inline uint64_t flow_u64_value(const struct flow
*flow
, size_t index
)
502 return ((uint64_t *)flow
)[index
];
505 static inline uint64_t *flow_u64_lvalue(struct flow
*flow
, size_t index
)
507 return &((uint64_t *)flow
)[index
];
511 miniflow_n_values(const struct miniflow
*flow
)
513 return flowmap_n_1bits(flow
->map
);
516 struct flow_for_each_in_maps_aux
{
517 const struct flow
*flow
;
518 struct flowmap_aux map_aux
;
522 flow_values_get_next_in_maps(struct flow_for_each_in_maps_aux
*aux
,
527 if (flowmap_next_index(&aux
->map_aux
, &idx
)) {
528 *value
= flow_u64_value(aux
->flow
, idx
);
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));)
540 struct mf_for_each_in_map_aux
{
544 const uint64_t *values
;
548 mf_get_next_in_map(struct mf_for_each_in_map_aux
*aux
,
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
) {
562 rm1bit
= rightmost_1bit(*map
);
564 fmap
= &aux
->fmap
.bits
[aux
->unit
];
566 if (OVS_LIKELY(*fmap
& rm1bit
)) {
567 map_t trash
= *fmap
& (rm1bit
- 1);
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
);
575 *value
= *aux
->values
;
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));)
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
)
592 return values
+ count_1bits(map
& ((MAP_1
<< idx
) - 1));
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
)
600 const uint64_t *values
= miniflow_get_values(mf
);
601 const map_t
*map
= mf
->map
.bits
;
603 while (idx
>= MAP_T_BITS
) {
605 values
+= count_1bits(*map
++);
607 return miniflow_values_get__(values
, *map
, idx
);
610 #define MINIFLOW_IN_MAP(MF, IDX) flowmap_is_set(&(MF)->map, IDX)
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)] \
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) } }
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)
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
*);
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
);
662 /* Compressed flow wildcards. */
664 /* A sparse representation of a "struct flow_wildcards".
666 * See the large comment on struct miniflow for details.
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. */
671 struct miniflow masks
;
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
]);
680 void minimask_expand(const struct minimask
*, struct flow_wildcards
*);
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
*);
689 bool minimask_equal(const struct minimask
*a
, const struct minimask
*b
);
690 bool minimask_has_extra(const struct minimask
*, const struct minimask
*);
693 /* Returns true if 'mask' matches every packet, false if 'mask' fixes any bits
696 minimask_is_catchall(const struct minimask
*mask
)
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
);
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
)
709 return MINIFLOW_IN_MAP(flow
, u64_ofs
) ? *miniflow_get__(flow
, u64_ofs
) : 0;
712 static inline uint32_t miniflow_get_u32(const struct miniflow
*flow
,
713 unsigned int u32_ofs
)
715 uint64_t value
= miniflow_get(flow
, u32_ofs
/ 2);
718 return (u32_ofs
& 1) ? value
: value
>> 32;
720 return (u32_ofs
& 1) ? value
>> 32 : value
;
724 static inline ovs_be32
miniflow_get_be32(const struct miniflow
*flow
,
725 unsigned int be32_ofs
)
727 return (OVS_FORCE ovs_be32
)miniflow_get_u32(flow
, be32_ofs
);
730 /* Returns the VID within the vlan_tci member of the "struct flow" represented
732 static inline uint16_t
733 miniflow_get_vid(const struct miniflow
*flow
)
735 ovs_be16 tci
= MINIFLOW_GET_BE16(flow
, vlan_tci
);
736 return vlan_tci_to_vid(tci
);
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
)
744 return miniflow_get_u32(&mask
->masks
, u32_ofs
);
747 static inline ovs_be32
748 minimask_get_be32(const struct minimask
*mask
, unsigned int be32_ofs
)
750 return (OVS_FORCE ovs_be32
)minimask_get_u32(mask
, be32_ofs
);
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
)
758 return miniflow_get_vid(&mask
->masks
);
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
)
765 return ntohs(MINIFLOW_GET_BE16(flow
, tcp_flags
));
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
)
772 return MINIFLOW_GET_BE64(flow
, metadata
);
775 /* Returns the mask for the OpenFlow 1.1+ "metadata" field in 'mask'.
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
781 static inline ovs_be64
782 minimask_get_metadata_mask(const struct minimask
*mask
)
784 return MINIFLOW_GET_BE64(&mask
->masks
, metadata
);
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. */
791 flow_union_with_miniflow_subset(struct flow
*dst
, const struct miniflow
*src
,
792 struct flowmap subset
)
794 uint64_t *dst_u64
= (uint64_t *) dst
;
795 const uint64_t *p
= miniflow_get_values(src
);
798 FLOWMAP_FOR_EACH_MAP (map
, subset
) {
801 MAP_FOR_EACH_INDEX(idx
, map
) {
802 dst_u64
[idx
] |= *p
++;
804 dst_u64
+= MAP_T_BITS
;
808 /* Perform a bitwise OR of miniflow 'src' flow data with the equivalent
809 * fields in 'dst', storing the result in 'dst'. */
811 flow_union_with_miniflow(struct flow
*dst
, const struct miniflow
*src
)
813 flow_union_with_miniflow_subset(dst
, src
, src
->map
);
817 pkt_metadata_from_flow(struct pkt_metadata
*md
, const struct flow
*flow
)
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
;
831 static inline bool is_ip_any(const struct flow
*flow
)
833 return dl_type_is_ip_any(flow
->dl_type
);
836 static inline bool is_icmpv4(const struct flow
*flow
,
837 struct flow_wildcards
*wc
)
839 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
841 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
843 return flow
->nw_proto
== IPPROTO_ICMP
;
848 static inline bool is_icmpv6(const struct flow
*flow
,
849 struct flow_wildcards
*wc
)
851 if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
853 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
855 return flow
->nw_proto
== IPPROTO_ICMPV6
;
860 static inline bool is_nd(const struct flow
*flow
,
861 struct flow_wildcards
*wc
)
863 if (is_icmpv6(flow
, wc
)) {
865 memset(&wc
->masks
.tp_dst
, 0xff, sizeof wc
->masks
.tp_dst
);
867 if (flow
->tp_dst
!= htons(0)) {
872 memset(&wc
->masks
.tp_src
, 0xff, sizeof wc
->masks
.tp_src
);
874 return (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
) ||
875 flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
));
880 static inline bool is_igmp(const struct flow
*flow
, struct flow_wildcards
*wc
)
882 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
884 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
886 return flow
->nw_proto
== IPPROTO_IGMP
;
891 static inline bool is_mld(const struct flow
*flow
,
892 struct flow_wildcards
*wc
)
894 if (is_icmpv6(flow
, wc
)) {
896 memset(&wc
->masks
.tp_src
, 0xff, sizeof wc
->masks
.tp_src
);
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
));
906 static inline bool is_mld_query(const struct flow
*flow
,
907 struct flow_wildcards
*wc
)
909 if (is_icmpv6(flow
, wc
)) {
911 memset(&wc
->masks
.tp_src
, 0xff, sizeof wc
->masks
.tp_src
);
913 return flow
->tp_src
== htons(MLD_QUERY
);
918 static inline bool is_mld_report(const struct flow
*flow
,
919 struct flow_wildcards
*wc
)
921 return is_mld(flow
, wc
) && !is_mld_query(flow
, wc
);
924 static inline bool is_stp(const struct flow
*flow
)
926 return (eth_addr_equals(flow
->dl_dst
, eth_addr_stp
)
927 && flow
->dl_type
== htons(FLOW_DL_TYPE_NONE
));