2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 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 /* Configured maximum VLAN headers. */
57 extern int flow_vlan_limit
;
59 #define FLOW_U64_OFFSET(FIELD) \
60 (offsetof(struct flow, FIELD) / sizeof(uint64_t))
61 #define FLOW_U64_OFFREM(FIELD) \
62 (offsetof(struct flow, FIELD) % sizeof(uint64_t))
64 /* Number of 64-bit units spanned by a 'FIELD'. */
65 #define FLOW_U64_SIZE(FIELD) \
66 DIV_ROUND_UP(FLOW_U64_OFFREM(FIELD) + MEMBER_SIZEOF(struct flow, FIELD), \
69 void flow_extract(struct dp_packet
*, struct flow
*);
71 void flow_zero_wildcards(struct flow
*, const struct flow_wildcards
*);
72 void flow_unwildcard_tp_ports(const struct flow
*, struct flow_wildcards
*);
73 void flow_get_metadata(const struct flow
*, struct match
*flow_metadata
);
75 const char *ct_state_to_string(uint32_t state
);
76 uint32_t ct_state_from_string(const char *);
77 void flow_clear_conntrack(struct flow
*);
79 char *flow_to_string(const struct flow
*);
80 void format_flags(struct ds
*ds
, const char *(*bit_to_string
)(uint32_t),
81 uint32_t flags
, char del
);
82 void format_flags_masked(struct ds
*ds
, const char *name
,
83 const char *(*bit_to_string
)(uint32_t),
84 uint32_t flags
, uint32_t mask
, uint32_t max_mask
);
85 int parse_flags(const char *s
, const char *(*bit_to_string
)(uint32_t),
86 char end
, const char *field_name
, char **res_string
,
87 uint32_t *res_flags
, uint32_t allowed
, uint32_t *res_mask
);
89 void flow_format(struct ds
*, const struct flow
*);
90 void flow_print(FILE *, const struct flow
*);
91 static inline int flow_compare_3way(const struct flow
*, const struct flow
*);
92 static inline bool flow_equal(const struct flow
*, const struct flow
*);
93 static inline size_t flow_hash(const struct flow
*, uint32_t basis
);
95 void flow_set_dl_vlan(struct flow
*, ovs_be16 vid
);
96 void flow_fix_vlan_tpid(struct flow
*);
97 void flow_set_vlan_vid(struct flow
*, ovs_be16 vid
);
98 void flow_set_vlan_pcp(struct flow
*, uint8_t pcp
);
100 void flow_limit_vlans(int vlan_limit
);
101 int flow_count_vlan_headers(const struct flow
*);
102 void flow_skip_common_vlan_headers(const struct flow
*a
, int *p_an
,
103 const struct flow
*b
, int *p_bn
);
104 void flow_pop_vlan(struct flow
*, struct flow_wildcards
*);
105 void flow_push_vlan_uninit(struct flow
*, struct flow_wildcards
*);
107 int flow_count_mpls_labels(const struct flow
*, struct flow_wildcards
*);
108 int flow_count_common_mpls_labels(const struct flow
*a
, int an
,
109 const struct flow
*b
, int bn
,
110 struct flow_wildcards
*wc
);
111 void flow_push_mpls(struct flow
*, int n
, ovs_be16 mpls_eth_type
,
112 struct flow_wildcards
*, bool clear_flow_L3
);
113 bool flow_pop_mpls(struct flow
*, int n
, ovs_be16 eth_type
,
114 struct flow_wildcards
*);
115 void flow_set_mpls_label(struct flow
*, int idx
, ovs_be32 label
);
116 void flow_set_mpls_ttl(struct flow
*, int idx
, uint8_t ttl
);
117 void flow_set_mpls_tc(struct flow
*, int idx
, uint8_t tc
);
118 void flow_set_mpls_bos(struct flow
*, int idx
, uint8_t stack
);
119 void flow_set_mpls_lse(struct flow
*, int idx
, ovs_be32 lse
);
121 void flow_compose(struct dp_packet
*, const struct flow
*);
123 bool parse_ipv6_ext_hdrs(const void **datap
, size_t *sizep
, uint8_t *nw_proto
,
125 ovs_be16
parse_dl_type(const struct eth_header
*data_
, size_t size
);
127 static inline uint64_t
128 flow_get_xreg(const struct flow
*flow
, int idx
)
130 return ((uint64_t) flow
->regs
[idx
* 2] << 32) | flow
->regs
[idx
* 2 + 1];
134 flow_set_xreg(struct flow
*flow
, int idx
, uint64_t value
)
136 flow
->regs
[idx
* 2] = value
>> 32;
137 flow
->regs
[idx
* 2 + 1] = value
;
140 static inline ovs_u128
141 flow_get_xxreg(const struct flow
*flow
, int idx
)
145 value
.u64
.hi
= (uint64_t) flow
->regs
[idx
* 4] << 32;
146 value
.u64
.hi
|= flow
->regs
[idx
* 4 + 1];
147 value
.u64
.lo
= (uint64_t) flow
->regs
[idx
* 4 + 2] << 32;
148 value
.u64
.lo
|= flow
->regs
[idx
* 4 + 3];
154 flow_set_xxreg(struct flow
*flow
, int idx
, ovs_u128 value
)
156 flow
->regs
[idx
* 4] = value
.u64
.hi
>> 32;
157 flow
->regs
[idx
* 4 + 1] = value
.u64
.hi
;
158 flow
->regs
[idx
* 4 + 2] = value
.u64
.lo
>> 32;
159 flow
->regs
[idx
* 4 + 3] = value
.u64
.lo
;
163 flow_compare_3way(const struct flow
*a
, const struct flow
*b
)
165 return memcmp(a
, b
, sizeof *a
);
169 flow_equal(const struct flow
*a
, const struct flow
*b
)
171 return !flow_compare_3way(a
, b
);
175 flow_hash(const struct flow
*flow
, uint32_t basis
)
177 return hash_bytes64((const uint64_t *)flow
, sizeof *flow
, basis
);
180 static inline uint16_t
181 ofp_to_u16(ofp_port_t ofp_port
)
183 return (OVS_FORCE
uint16_t) ofp_port
;
186 static inline uint32_t
187 odp_to_u32(odp_port_t odp_port
)
189 return (OVS_FORCE
uint32_t) odp_port
;
192 static inline uint32_t
193 ofp11_to_u32(ofp11_port_t ofp11_port
)
195 return (OVS_FORCE
uint32_t) ofp11_port
;
198 static inline ofp_port_t
199 u16_to_ofp(uint16_t port
)
201 return OFP_PORT_C(port
);
204 static inline odp_port_t
205 u32_to_odp(uint32_t port
)
207 return ODP_PORT_C(port
);
210 static inline ofp11_port_t
211 u32_to_ofp11(uint32_t port
)
213 return OFP11_PORT_C(port
);
216 static inline uint32_t
217 hash_ofp_port(ofp_port_t ofp_port
)
219 return hash_int(ofp_to_u16(ofp_port
), 0);
222 static inline uint32_t
223 hash_odp_port(odp_port_t odp_port
)
225 return hash_int(odp_to_u32(odp_port
), 0);
228 uint32_t flow_hash_5tuple(const struct flow
*flow
, uint32_t basis
);
229 uint32_t flow_hash_symmetric_l4(const struct flow
*flow
, uint32_t basis
);
230 uint32_t flow_hash_symmetric_l3l4(const struct flow
*flow
, uint32_t basis
,
231 bool inc_udp_ports
);
233 /* Initialize a flow with random fields that matter for nx_hash_fields. */
234 void flow_random_hash_fields(struct flow
*);
235 void flow_mask_hash_fields(const struct flow
*, struct flow_wildcards
*,
236 enum nx_hash_fields
);
237 uint32_t flow_hash_fields(const struct flow
*, enum nx_hash_fields
,
239 const char *flow_hash_fields_to_str(enum nx_hash_fields
);
240 bool flow_hash_fields_valid(enum nx_hash_fields
);
242 uint32_t flow_hash_in_wildcards(const struct flow
*,
243 const struct flow_wildcards
*,
246 bool flow_equal_except(const struct flow
*a
, const struct flow
*b
,
247 const struct flow_wildcards
*);
249 /* Bitmap for flow values. For each 1-bit the corresponding flow value is
250 * explicitly specified, other values are zeroes.
252 * map_t must be wide enough to hold any member of struct flow. */
253 typedef unsigned long long map_t
;
254 #define MAP_T_BITS (sizeof(map_t) * CHAR_BIT)
255 #define MAP_1 (map_t)1
256 #define MAP_MAX TYPE_MAXIMUM(map_t)
258 #define MAP_IS_SET(MAP, IDX) ((MAP) & (MAP_1 << (IDX)))
260 /* Iterate through the indices of all 1-bits in 'MAP'. */
261 #define MAP_FOR_EACH_INDEX(IDX, MAP) \
262 ULLONG_FOR_EACH_1(IDX, MAP)
264 #define FLOWMAP_UNITS DIV_ROUND_UP(FLOW_U64S, MAP_T_BITS)
267 map_t bits
[FLOWMAP_UNITS
];
270 #define FLOWMAP_EMPTY_INITIALIZER { { 0 } }
272 static inline void flowmap_init(struct flowmap
*);
273 static inline bool flowmap_equal(struct flowmap
, struct flowmap
);
274 static inline bool flowmap_is_set(const struct flowmap
*, size_t idx
);
275 static inline bool flowmap_are_set(const struct flowmap
*, size_t idx
,
276 unsigned int n_bits
);
277 static inline void flowmap_set(struct flowmap
*, size_t idx
,
278 unsigned int n_bits
);
279 static inline void flowmap_clear(struct flowmap
*, size_t idx
,
280 unsigned int n_bits
);
281 static inline struct flowmap
flowmap_or(struct flowmap
, struct flowmap
);
282 static inline struct flowmap
flowmap_and(struct flowmap
, struct flowmap
);
283 static inline bool flowmap_is_empty(struct flowmap
);
284 static inline unsigned int flowmap_n_1bits(struct flowmap
);
286 #define FLOWMAP_HAS_FIELD(FM, FIELD) \
287 flowmap_are_set(FM, FLOW_U64_OFFSET(FIELD), FLOW_U64_SIZE(FIELD))
289 #define FLOWMAP_SET(FM, FIELD) \
290 flowmap_set(FM, FLOW_U64_OFFSET(FIELD), FLOW_U64_SIZE(FIELD))
292 #define FLOWMAP_SET__(FM, FIELD, SIZE) \
293 flowmap_set(FM, FLOW_U64_OFFSET(FIELD), \
294 DIV_ROUND_UP(SIZE, sizeof(uint64_t)))
296 /* XXX: Only works for full 64-bit units. */
297 #define FLOWMAP_CLEAR(FM, FIELD) \
298 BUILD_ASSERT_DECL(FLOW_U64_OFFREM(FIELD) == 0); \
299 BUILD_ASSERT_DECL(sizeof(((struct flow *)0)->FIELD) % sizeof(uint64_t) == 0); \
300 flowmap_clear(FM, FLOW_U64_OFFSET(FIELD), FLOW_U64_SIZE(FIELD))
302 /* Iterate through all units in 'FMAP'. */
303 #define FLOWMAP_FOR_EACH_UNIT(UNIT) \
304 for ((UNIT) = 0; (UNIT) < FLOWMAP_UNITS; (UNIT)++)
306 /* Iterate through all map units in 'FMAP'. */
307 #define FLOWMAP_FOR_EACH_MAP(MAP, FLOWMAP) \
308 for (size_t unit__ = 0; \
309 unit__ < FLOWMAP_UNITS && ((MAP) = (FLOWMAP).bits[unit__], true); \
313 static inline bool flowmap_next_index(struct flowmap_aux
*, size_t *idx
);
315 #define FLOWMAP_AUX_INITIALIZER(FLOWMAP) { .unit = 0, .map = (FLOWMAP) }
317 /* Iterate through all struct flow u64 indices specified by 'MAP'. This is a
318 * slower but easier version of the FLOWMAP_FOR_EACH_MAP() &
319 * MAP_FOR_EACH_INDEX() combination. */
320 #define FLOWMAP_FOR_EACH_INDEX(IDX, MAP) \
321 for (struct flowmap_aux aux__ = FLOWMAP_AUX_INITIALIZER(MAP); \
322 flowmap_next_index(&aux__, &(IDX));)
324 /* Flowmap inline implementations. */
326 flowmap_init(struct flowmap
*fm
)
328 memset(fm
, 0, sizeof *fm
);
332 flowmap_equal(struct flowmap a
, struct flowmap b
)
334 return !memcmp(&a
, &b
, sizeof a
);
338 flowmap_is_set(const struct flowmap
*fm
, size_t idx
)
340 return (fm
->bits
[idx
/ MAP_T_BITS
] & (MAP_1
<< (idx
% MAP_T_BITS
))) != 0;
343 /* Returns 'true' if any of the 'n_bits' bits starting at 'idx' are set in
344 * 'fm'. 'n_bits' can be at most MAP_T_BITS. */
346 flowmap_are_set(const struct flowmap
*fm
, size_t idx
, unsigned int n_bits
)
348 map_t n_bits_mask
= (MAP_1
<< n_bits
) - 1;
349 size_t unit
= idx
/ MAP_T_BITS
;
353 if (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 /* Check the remaining bits from the next unit. */
360 return fm
->bits
[unit
+ 1] & (n_bits_mask
>> (MAP_T_BITS
- idx
));
365 /* Set the 'n_bits' consecutive bits in 'fm', starting at bit 'idx'.
366 * 'n_bits' can be at most MAP_T_BITS. */
368 flowmap_set(struct flowmap
*fm
, size_t idx
, unsigned int n_bits
)
370 map_t n_bits_mask
= (MAP_1
<< n_bits
) - 1;
371 size_t unit
= idx
/ MAP_T_BITS
;
375 fm
->bits
[unit
] |= n_bits_mask
<< idx
;
376 /* The seemingly unnecessary bounds check on 'unit' is a workaround for a
377 * false-positive array out of bounds error by GCC 4.9. */
378 if (unit
+ 1 < FLOWMAP_UNITS
&& idx
+ n_bits
> MAP_T_BITS
) {
379 /* 'MAP_T_BITS - idx' bits were set on 'unit', set the remaining
380 * bits from the next unit. */
381 fm
->bits
[unit
+ 1] |= n_bits_mask
>> (MAP_T_BITS
- idx
);
385 /* Clears the 'n_bits' consecutive bits in 'fm', starting at bit 'idx'.
386 * 'n_bits' can be at most MAP_T_BITS. */
388 flowmap_clear(struct flowmap
*fm
, size_t idx
, unsigned int n_bits
)
390 map_t n_bits_mask
= (MAP_1
<< n_bits
) - 1;
391 size_t unit
= idx
/ MAP_T_BITS
;
395 fm
->bits
[unit
] &= ~(n_bits_mask
<< idx
);
396 /* The seemingly unnecessary bounds check on 'unit' is a workaround for a
397 * false-positive array out of bounds error by GCC 4.9. */
398 if (unit
+ 1 < FLOWMAP_UNITS
&& idx
+ n_bits
> MAP_T_BITS
) {
399 /* 'MAP_T_BITS - idx' bits were cleared on 'unit', clear the
400 * remaining bits from the next unit. */
401 fm
->bits
[unit
+ 1] &= ~(n_bits_mask
>> (MAP_T_BITS
- idx
));
405 /* OR the bits in the flowmaps. */
406 static inline struct flowmap
407 flowmap_or(struct flowmap a
, struct flowmap b
)
412 FLOWMAP_FOR_EACH_UNIT (unit
) {
413 map
.bits
[unit
] = a
.bits
[unit
] | b
.bits
[unit
];
418 /* AND the bits in the flowmaps. */
419 static inline struct flowmap
420 flowmap_and(struct flowmap a
, struct flowmap b
)
425 FLOWMAP_FOR_EACH_UNIT (unit
) {
426 map
.bits
[unit
] = a
.bits
[unit
] & b
.bits
[unit
];
432 flowmap_is_empty(struct flowmap fm
)
436 FLOWMAP_FOR_EACH_MAP (map
, fm
) {
444 static inline unsigned int
445 flowmap_n_1bits(struct flowmap fm
)
447 unsigned int n_1bits
= 0;
450 FLOWMAP_FOR_EACH_UNIT (unit
) {
451 n_1bits
+= count_1bits(fm
.bits
[unit
]);
462 flowmap_next_index(struct flowmap_aux
*aux
, size_t *idx
)
465 map_t
*map
= &aux
->map
.bits
[aux
->unit
];
467 *idx
= aux
->unit
* MAP_T_BITS
+ raw_ctz(*map
);
468 *map
= zero_rightmost_1bit(*map
);
471 if (++aux
->unit
>= FLOWMAP_UNITS
) {
478 /* Compressed flow. */
480 /* A sparse representation of a "struct flow".
482 * A "struct flow" is fairly large and tends to be mostly zeros. Sparse
483 * representation has two advantages. First, it saves memory and, more
484 * importantly, minimizes the number of accessed cache lines. Second, it saves
485 * time when the goal is to iterate over only the nonzero parts of the struct.
487 * The map member hold one bit for each uint64_t in a "struct flow". Each
488 * 0-bit indicates that the corresponding uint64_t is zero, each 1-bit that it
489 * *may* be nonzero (see below how this applies to minimasks).
491 * The values indicated by 'map' always follow the miniflow in memory. The
492 * user of the miniflow is responsible for always having enough storage after
493 * the struct miniflow corresponding to the number of 1-bits in maps.
495 * Elements in values array are allowed to be zero. This is useful for "struct
496 * minimatch", for which ensuring that the miniflow and minimask members have
497 * same maps allows optimization. This allowance applies only to a miniflow
498 * that is not a mask. That is, a minimask may NOT have zero elements in its
501 * A miniflow is always dynamically allocated so that the maps are followed by
502 * at least as many elements as there are 1-bits in maps. */
506 * uint64_t values[n];
507 * where 'n' is miniflow_n_values(miniflow). */
509 BUILD_ASSERT_DECL(sizeof(struct miniflow
) % sizeof(uint64_t) == 0);
511 #define MINIFLOW_VALUES_SIZE(COUNT) ((COUNT) * sizeof(uint64_t))
513 static inline uint64_t *miniflow_values(struct miniflow
*mf
)
515 return (uint64_t *)(mf
+ 1);
518 static inline const uint64_t *miniflow_get_values(const struct miniflow
*mf
)
520 return (const uint64_t *)(mf
+ 1);
525 /* The 'dst' must follow with buffer space for FLOW_U64S 64-bit units.
526 * 'dst->map' is ignored on input and set on output to indicate which fields
528 void miniflow_extract(struct dp_packet
*packet
, struct miniflow
*dst
);
529 void miniflow_map_init(struct miniflow
*, const struct flow
*);
530 void flow_wc_map(const struct flow
*, struct flowmap
*);
531 size_t miniflow_alloc(struct miniflow
*dsts
[], size_t n
,
532 const struct miniflow
*src
);
533 void miniflow_init(struct miniflow
*, const struct flow
*);
534 void miniflow_clone(struct miniflow
*, const struct miniflow
*,
536 struct miniflow
* miniflow_create(const struct flow
*);
538 void miniflow_expand(const struct miniflow
*, struct flow
*);
540 static inline uint64_t flow_u64_value(const struct flow
*flow
, size_t index
)
542 return ((uint64_t *)flow
)[index
];
545 static inline uint64_t *flow_u64_lvalue(struct flow
*flow
, size_t index
)
547 return &((uint64_t *)flow
)[index
];
551 miniflow_n_values(const struct miniflow
*flow
)
553 return flowmap_n_1bits(flow
->map
);
556 struct flow_for_each_in_maps_aux
{
557 const struct flow
*flow
;
558 struct flowmap_aux map_aux
;
562 flow_values_get_next_in_maps(struct flow_for_each_in_maps_aux
*aux
,
567 if (flowmap_next_index(&aux
->map_aux
, &idx
)) {
568 *value
= flow_u64_value(aux
->flow
, idx
);
574 /* Iterate through all flow u64 values specified by 'MAPS'. */
575 #define FLOW_FOR_EACH_IN_MAPS(VALUE, FLOW, MAPS) \
576 for (struct flow_for_each_in_maps_aux aux__ \
577 = { (FLOW), FLOWMAP_AUX_INITIALIZER(MAPS) }; \
578 flow_values_get_next_in_maps(&aux__, &(VALUE));)
580 struct mf_for_each_in_map_aux
{
581 size_t unit
; /* Current 64-bit unit of the flowmaps
583 struct flowmap fmap
; /* Remaining 1-bits corresponding to the
584 64-bit words in ‘values’ */
585 struct flowmap map
; /* Remaining 1-bits corresponding to the
586 64-bit words of interest. */
587 const uint64_t *values
; /* 64-bit words corresponding to the
588 1-bits in ‘fmap’. */
591 /* Get the data from ‘aux->values’ corresponding to the next lowest 1-bit
592 * in ‘aux->map’, given that ‘aux->values’ points to an array of 64-bit
593 * words corresponding to the 1-bits in ‘aux->fmap’, starting from the
596 * Returns ’true’ if the traversal is incomplete, ‘false’ otherwise.
597 * ‘aux’ is prepared for the next iteration after each call.
599 * This is used to traverse through, for example, the values in a miniflow
600 * representation of a flow key selected by non-zero 64-bit words in a
601 * corresponding subtable mask. */
603 mf_get_next_in_map(struct mf_for_each_in_map_aux
*aux
,
609 /* Skip empty map units. */
610 while (OVS_UNLIKELY(!*(map
= &aux
->map
.bits
[aux
->unit
]))) {
611 /* Skip remaining data in the current unit before advancing
613 aux
->values
+= count_1bits(aux
->fmap
.bits
[aux
->unit
]);
614 if (++aux
->unit
== FLOWMAP_UNITS
) {
619 rm1bit
= rightmost_1bit(*map
);
621 fmap
= &aux
->fmap
.bits
[aux
->unit
];
623 /* If the rightmost 1-bit found from the current unit in ‘aux->map’
624 * (‘rm1bit’) is also present in ‘aux->fmap’, store the corresponding
625 * value from ‘aux->values’ to ‘*value', otherwise store 0. */
626 if (OVS_LIKELY(*fmap
& rm1bit
)) {
627 /* Skip all 64-bit words in ‘values’ preceding the one corresponding
628 * to ‘rm1bit’. */
629 map_t trash
= *fmap
& (rm1bit
- 1);
631 /* Avoid resetting 'fmap' and calling count_1bits() when trash is
635 aux
->values
+= count_1bits(trash
);
638 *value
= *aux
->values
;
645 /* Iterate through miniflow u64 values specified by 'FLOWMAP'. */
646 #define MINIFLOW_FOR_EACH_IN_FLOWMAP(VALUE, FLOW, FLOWMAP) \
647 for (struct mf_for_each_in_map_aux aux__ = \
648 { 0, (FLOW)->map, (FLOWMAP), miniflow_get_values(FLOW) }; \
649 mf_get_next_in_map(&aux__, &(VALUE));)
651 /* This can be used when it is known that 'idx' is set in 'map'. */
652 static inline const uint64_t *
653 miniflow_values_get__(const uint64_t *values
, map_t map
, size_t idx
)
655 return values
+ count_1bits(map
& ((MAP_1
<< idx
) - 1));
658 /* This can be used when it is known that 'u64_idx' is set in
659 * the map of 'mf'. */
660 static inline const uint64_t *
661 miniflow_get__(const struct miniflow
*mf
, size_t idx
)
663 const uint64_t *values
= miniflow_get_values(mf
);
664 const map_t
*map
= mf
->map
.bits
;
666 while (idx
>= MAP_T_BITS
) {
668 values
+= count_1bits(*map
++);
670 return miniflow_values_get__(values
, *map
, idx
);
673 #define MINIFLOW_IN_MAP(MF, IDX) flowmap_is_set(&(MF)->map, IDX)
675 /* Get the value of the struct flow 'FIELD' as up to 8 byte wide integer type
676 * 'TYPE' from miniflow 'MF'. */
677 #define MINIFLOW_GET_TYPE(MF, TYPE, FIELD) \
678 (MINIFLOW_IN_MAP(MF, FLOW_U64_OFFSET(FIELD)) \
679 ? ((OVS_FORCE const TYPE *)miniflow_get__(MF, FLOW_U64_OFFSET(FIELD))) \
680 [FLOW_U64_OFFREM(FIELD) / sizeof(TYPE)] \
683 #define MINIFLOW_GET_U128(FLOW, FIELD) \
684 (ovs_u128) { .u64 = { \
685 (MINIFLOW_IN_MAP(FLOW, FLOW_U64_OFFSET(FIELD)) ? \
686 *miniflow_get__(FLOW, FLOW_U64_OFFSET(FIELD)) : 0), \
687 (MINIFLOW_IN_MAP(FLOW, FLOW_U64_OFFSET(FIELD) + 1) ? \
688 *miniflow_get__(FLOW, FLOW_U64_OFFSET(FIELD) + 1) : 0) } }
690 #define MINIFLOW_GET_U8(FLOW, FIELD) \
691 MINIFLOW_GET_TYPE(FLOW, uint8_t, FIELD)
692 #define MINIFLOW_GET_U16(FLOW, FIELD) \
693 MINIFLOW_GET_TYPE(FLOW, uint16_t, FIELD)
694 #define MINIFLOW_GET_BE16(FLOW, FIELD) \
695 MINIFLOW_GET_TYPE(FLOW, ovs_be16, FIELD)
696 #define MINIFLOW_GET_U32(FLOW, FIELD) \
697 MINIFLOW_GET_TYPE(FLOW, uint32_t, FIELD)
698 #define MINIFLOW_GET_BE32(FLOW, FIELD) \
699 MINIFLOW_GET_TYPE(FLOW, ovs_be32, FIELD)
700 #define MINIFLOW_GET_U64(FLOW, FIELD) \
701 MINIFLOW_GET_TYPE(FLOW, uint64_t, FIELD)
702 #define MINIFLOW_GET_BE64(FLOW, FIELD) \
703 MINIFLOW_GET_TYPE(FLOW, ovs_be64, FIELD)
705 static inline uint64_t miniflow_get(const struct miniflow
*,
706 unsigned int u64_ofs
);
707 static inline uint32_t miniflow_get_u32(const struct miniflow
*,
708 unsigned int u32_ofs
);
709 static inline ovs_be32
miniflow_get_be32(const struct miniflow
*,
710 unsigned int be32_ofs
);
711 static inline uint16_t miniflow_get_vid(const struct miniflow
*, size_t);
712 static inline uint16_t miniflow_get_tcp_flags(const struct miniflow
*);
713 static inline ovs_be64
miniflow_get_metadata(const struct miniflow
*);
715 bool miniflow_equal(const struct miniflow
*a
, const struct miniflow
*b
);
716 bool miniflow_equal_in_minimask(const struct miniflow
*a
,
717 const struct miniflow
*b
,
718 const struct minimask
*);
719 bool miniflow_equal_flow_in_minimask(const struct miniflow
*a
,
720 const struct flow
*b
,
721 const struct minimask
*);
722 uint32_t miniflow_hash_5tuple(const struct miniflow
*flow
, uint32_t basis
);
725 /* Compressed flow wildcards. */
727 /* A sparse representation of a "struct flow_wildcards".
729 * See the large comment on struct miniflow for details.
731 * Note: While miniflow can have zero data for a 1-bit in the map,
732 * a minimask may not! We rely on this in the implementation. */
734 struct miniflow masks
;
737 void minimask_init(struct minimask
*, const struct flow_wildcards
*);
738 struct minimask
* minimask_create(const struct flow_wildcards
*);
739 void minimask_combine(struct minimask
*dst
,
740 const struct minimask
*a
, const struct minimask
*b
,
741 uint64_t storage
[FLOW_U64S
]);
743 void minimask_expand(const struct minimask
*, struct flow_wildcards
*);
745 static inline uint32_t minimask_get_u32(const struct minimask
*,
746 unsigned int u32_ofs
);
747 static inline ovs_be32
minimask_get_be32(const struct minimask
*,
748 unsigned int be32_ofs
);
749 static inline uint16_t minimask_get_vid_mask(const struct minimask
*, size_t);
750 static inline ovs_be64
minimask_get_metadata_mask(const struct minimask
*);
752 bool minimask_equal(const struct minimask
*a
, const struct minimask
*b
);
753 bool minimask_has_extra(const struct minimask
*, const struct minimask
*);
756 /* Returns true if 'mask' matches every packet, false if 'mask' fixes any bits
759 minimask_is_catchall(const struct minimask
*mask
)
761 /* For every 1-bit in mask's map, the corresponding value is non-zero,
762 * so the only way the mask can not fix any bits or fields is for the
763 * map the be zero. */
764 return flowmap_is_empty(mask
->masks
.map
);
767 /* Returns the uint64_t that would be at byte offset '8 * u64_ofs' if 'flow'
768 * were expanded into a "struct flow". */
769 static inline uint64_t miniflow_get(const struct miniflow
*flow
,
770 unsigned int u64_ofs
)
772 return MINIFLOW_IN_MAP(flow
, u64_ofs
) ? *miniflow_get__(flow
, u64_ofs
) : 0;
775 static inline uint32_t miniflow_get_u32(const struct miniflow
*flow
,
776 unsigned int u32_ofs
)
778 uint64_t value
= miniflow_get(flow
, u32_ofs
/ 2);
781 return (u32_ofs
& 1) ? value
: value
>> 32;
783 return (u32_ofs
& 1) ? value
>> 32 : value
;
787 static inline ovs_be32
miniflow_get_be32(const struct miniflow
*flow
,
788 unsigned int be32_ofs
)
790 return (OVS_FORCE ovs_be32
)miniflow_get_u32(flow
, be32_ofs
);
793 /* Returns the VID within the vlan_tci member of the "struct flow" represented
795 static inline uint16_t
796 miniflow_get_vid(const struct miniflow
*flow
, size_t n
)
798 if (n
< FLOW_MAX_VLAN_HEADERS
) {
799 union flow_vlan_hdr hdr
= {
800 .qtag
= MINIFLOW_GET_BE32(flow
, vlans
[n
])
802 return vlan_tci_to_vid(hdr
.tci
);
807 /* Returns the uint32_t that would be at byte offset '4 * u32_ofs' if 'mask'
808 * were expanded into a "struct flow_wildcards". */
809 static inline uint32_t
810 minimask_get_u32(const struct minimask
*mask
, unsigned int u32_ofs
)
812 return miniflow_get_u32(&mask
->masks
, u32_ofs
);
815 static inline ovs_be32
816 minimask_get_be32(const struct minimask
*mask
, unsigned int be32_ofs
)
818 return (OVS_FORCE ovs_be32
)minimask_get_u32(mask
, be32_ofs
);
821 /* Returns the VID mask within the vlan_tci member of the "struct
822 * flow_wildcards" represented by 'mask'. */
823 static inline uint16_t
824 minimask_get_vid_mask(const struct minimask
*mask
, size_t n
)
826 return miniflow_get_vid(&mask
->masks
, n
);
829 /* Returns the value of the "tcp_flags" field in 'flow'. */
830 static inline uint16_t
831 miniflow_get_tcp_flags(const struct miniflow
*flow
)
833 return ntohs(MINIFLOW_GET_BE16(flow
, tcp_flags
));
836 /* Returns the value of the OpenFlow 1.1+ "metadata" field in 'flow'. */
837 static inline ovs_be64
838 miniflow_get_metadata(const struct miniflow
*flow
)
840 return MINIFLOW_GET_BE64(flow
, metadata
);
843 /* Returns the mask for the OpenFlow 1.1+ "metadata" field in 'mask'.
845 * The return value is all-1-bits if 'mask' matches on the whole value of the
846 * metadata field, all-0-bits if 'mask' entirely wildcards the metadata field,
847 * or some other value if the metadata field is partially matched, partially
849 static inline ovs_be64
850 minimask_get_metadata_mask(const struct minimask
*mask
)
852 return MINIFLOW_GET_BE64(&mask
->masks
, metadata
);
855 /* Perform a bitwise OR of miniflow 'src' flow data specified in 'subset' with
856 * the equivalent fields in 'dst', storing the result in 'dst'. 'subset' must
857 * be a subset of 'src's map. */
859 flow_union_with_miniflow_subset(struct flow
*dst
, const struct miniflow
*src
,
860 struct flowmap subset
)
862 uint64_t *dst_u64
= (uint64_t *) dst
;
863 const uint64_t *p
= miniflow_get_values(src
);
866 FLOWMAP_FOR_EACH_MAP (map
, subset
) {
869 MAP_FOR_EACH_INDEX(idx
, map
) {
870 dst_u64
[idx
] |= *p
++;
872 dst_u64
+= MAP_T_BITS
;
876 /* Perform a bitwise OR of miniflow 'src' flow data with the equivalent
877 * fields in 'dst', storing the result in 'dst'. */
879 flow_union_with_miniflow(struct flow
*dst
, const struct miniflow
*src
)
881 flow_union_with_miniflow_subset(dst
, src
, src
->map
);
884 static inline bool is_ct_valid(const struct flow
*flow
,
885 const struct flow_wildcards
*mask
,
886 struct flow_wildcards
*wc
)
888 /* Matches are checked with 'mask' and without 'wc'. */
890 /* Must match at least one of the bits that implies a valid
891 * conntrack entry, or an explicit not-invalid. */
892 return flow
->ct_state
& (CS_NEW
| CS_ESTABLISHED
| CS_RELATED
893 | CS_REPLY_DIR
| CS_SRC_NAT
| CS_DST_NAT
)
894 || (flow
->ct_state
& CS_TRACKED
895 && mask
->masks
.ct_state
& CS_INVALID
896 && !(flow
->ct_state
& CS_INVALID
));
898 /* Else we are checking a fully extracted flow, where valid CT state always
899 * has either 'new', 'established', or 'reply_dir' bit set. */
900 #define CS_VALID_MASK (CS_NEW | CS_ESTABLISHED | CS_REPLY_DIR)
902 wc
->masks
.ct_state
|= CS_VALID_MASK
;
904 return flow
->ct_state
& CS_VALID_MASK
;
908 pkt_metadata_from_flow(struct pkt_metadata
*md
, const struct flow
*flow
)
910 /* Update this function whenever struct flow changes. */
911 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 38);
913 md
->recirc_id
= flow
->recirc_id
;
914 md
->dp_hash
= flow
->dp_hash
;
915 flow_tnl_copy__(&md
->tunnel
, &flow
->tunnel
);
916 md
->skb_priority
= flow
->skb_priority
;
917 md
->pkt_mark
= flow
->pkt_mark
;
918 md
->in_port
= flow
->in_port
;
919 md
->ct_state
= flow
->ct_state
;
920 md
->ct_zone
= flow
->ct_zone
;
921 md
->ct_mark
= flow
->ct_mark
;
922 md
->ct_label
= flow
->ct_label
;
924 md
->ct_orig_tuple_ipv6
= false;
925 if (is_ct_valid(flow
, NULL
, NULL
)) {
926 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
927 md
->ct_orig_tuple
.ipv4
= (struct ovs_key_ct_tuple_ipv4
) {
934 } else if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
935 md
->ct_orig_tuple_ipv6
= true;
936 md
->ct_orig_tuple
.ipv6
= (struct ovs_key_ct_tuple_ipv6
) {
945 memset(&md
->ct_orig_tuple
, 0, sizeof md
->ct_orig_tuple
);
949 /* Often, during translation we need to read a value from a flow('FLOW') and
950 * unwildcard the corresponding bits in the wildcards('WC'). This macro makes
951 * it easier to do that. */
953 #define FLOW_WC_GET_AND_MASK_WC(FLOW, WC, FIELD) \
954 (((WC) ? WC_MASK_FIELD(WC, FIELD) : NULL), ((FLOW)->FIELD))
956 static inline bool is_vlan(const struct flow
*flow
,
957 struct flow_wildcards
*wc
)
960 WC_MASK_FIELD_MASK(wc
, vlans
[0].tci
, htons(VLAN_CFI
));
962 return (flow
->vlans
[0].tci
& htons(VLAN_CFI
)) != 0;
965 static inline bool is_ip_any(const struct flow
*flow
)
967 return dl_type_is_ip_any(flow
->dl_type
);
970 static inline bool is_ip_proto(const struct flow
*flow
, uint8_t ip_proto
,
971 struct flow_wildcards
*wc
)
973 if (is_ip_any(flow
)) {
975 WC_MASK_FIELD(wc
, nw_proto
);
977 return flow
->nw_proto
== ip_proto
;
982 static inline bool is_tcp(const struct flow
*flow
,
983 struct flow_wildcards
*wc
)
985 return is_ip_proto(flow
, IPPROTO_TCP
, wc
);
988 static inline bool is_udp(const struct flow
*flow
,
989 struct flow_wildcards
*wc
)
991 return is_ip_proto(flow
, IPPROTO_UDP
, wc
);
994 static inline bool is_sctp(const struct flow
*flow
,
995 struct flow_wildcards
*wc
)
997 return is_ip_proto(flow
, IPPROTO_SCTP
, wc
);
1000 static inline bool is_icmpv4(const struct flow
*flow
,
1001 struct flow_wildcards
*wc
)
1003 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
1005 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
1007 return flow
->nw_proto
== IPPROTO_ICMP
;
1012 static inline bool is_icmpv6(const struct flow
*flow
,
1013 struct flow_wildcards
*wc
)
1015 if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
1017 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
1019 return flow
->nw_proto
== IPPROTO_ICMPV6
;
1024 static inline bool is_nd(const struct flow
*flow
,
1025 struct flow_wildcards
*wc
)
1027 if (is_icmpv6(flow
, wc
)) {
1029 memset(&wc
->masks
.tp_dst
, 0xff, sizeof wc
->masks
.tp_dst
);
1031 if (flow
->tp_dst
!= htons(0)) {
1036 memset(&wc
->masks
.tp_src
, 0xff, sizeof wc
->masks
.tp_src
);
1038 return (flow
->tp_src
== htons(ND_NEIGHBOR_SOLICIT
) ||
1039 flow
->tp_src
== htons(ND_NEIGHBOR_ADVERT
));
1044 static inline bool is_igmp(const struct flow
*flow
, struct flow_wildcards
*wc
)
1046 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
1048 memset(&wc
->masks
.nw_proto
, 0xff, sizeof wc
->masks
.nw_proto
);
1050 return flow
->nw_proto
== IPPROTO_IGMP
;
1055 static inline bool is_mld(const struct flow
*flow
,
1056 struct flow_wildcards
*wc
)
1058 if (is_icmpv6(flow
, wc
)) {
1060 memset(&wc
->masks
.tp_src
, 0xff, sizeof wc
->masks
.tp_src
);
1062 return (flow
->tp_src
== htons(MLD_QUERY
)
1063 || flow
->tp_src
== htons(MLD_REPORT
)
1064 || flow
->tp_src
== htons(MLD_DONE
)
1065 || flow
->tp_src
== htons(MLD2_REPORT
));
1070 static inline bool is_mld_query(const struct flow
*flow
,
1071 struct flow_wildcards
*wc
)
1073 if (is_icmpv6(flow
, wc
)) {
1075 memset(&wc
->masks
.tp_src
, 0xff, sizeof wc
->masks
.tp_src
);
1077 return flow
->tp_src
== htons(MLD_QUERY
);
1082 static inline bool is_mld_report(const struct flow
*flow
,
1083 struct flow_wildcards
*wc
)
1085 return is_mld(flow
, wc
) && !is_mld_query(flow
, wc
);
1088 static inline bool is_stp(const struct flow
*flow
)
1090 return (eth_addr_equals(flow
->dl_dst
, eth_addr_stp
)
1091 && flow
->dl_type
== htons(FLOW_DL_TYPE_NONE
));