]> git.proxmox.com Git - ovs.git/blame - lib/flow.c
userspace: Introduce packet_type in OF 1.5 packet-out
[ovs.git] / lib / flow.c
CommitLineData
064af421 1/*
fd6cd1bf 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2017 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#include <config.h>
17#include <sys/types.h>
18#include "flow.h"
d31f1109 19#include <errno.h>
064af421 20#include <inttypes.h>
5cb7a798 21#include <limits.h>
064af421 22#include <netinet/in.h>
d31f1109
JP
23#include <netinet/icmp6.h>
24#include <netinet/ip6.h>
5cb7a798 25#include <stdint.h>
064af421
BP
26#include <stdlib.h>
27#include <string.h>
10a24935 28#include "byte-order.h"
0deec6d2 29#include "colors.h"
064af421 30#include "coverage.h"
dc5a7ce7 31#include "csum.h"
3e8a2ad1 32#include "openvswitch/dynamic-string.h"
064af421 33#include "hash.h"
c49d1dd1 34#include "jhash.h"
e29747e4 35#include "openvswitch/match.h"
cf62fa4c 36#include "dp-packet.h"
064af421 37#include "openflow/openflow.h"
064af421 38#include "packets.h"
b5e7e61a 39#include "odp-util.h"
94639963 40#include "random.h"
176aaa65 41#include "unaligned.h"
ee89ea7b 42#include "util.h"
064af421 43
d76f09ea 44COVERAGE_DEFINE(flow_extract);
5cb7a798 45COVERAGE_DEFINE(miniflow_malloc);
d76f09ea 46
d70e8c28
JR
47/* U64 indices for segmented flow classification. */
48const uint8_t flow_segment_u64s[4] = {
49 FLOW_SEGMENT_1_ENDS_AT / sizeof(uint64_t),
50 FLOW_SEGMENT_2_ENDS_AT / sizeof(uint64_t),
51 FLOW_SEGMENT_3_ENDS_AT / sizeof(uint64_t),
52 FLOW_U64S
476f36e8
JR
53};
54
f0fb825a
EG
55int flow_vlan_limit = FLOW_MAX_VLAN_HEADERS;
56
268eca11
BP
57/* Asserts that field 'f1' follows immediately after 'f0' in struct flow,
58 * without any intervening padding. */
59#define ASSERT_SEQUENTIAL(f0, f1) \
60 BUILD_ASSERT_DECL(offsetof(struct flow, f0) \
61 + MEMBER_SIZEOF(struct flow, f0) \
62 == offsetof(struct flow, f1))
63
64/* Asserts that fields 'f0' and 'f1' are in the same 32-bit aligned word within
65 * struct flow. */
66#define ASSERT_SAME_WORD(f0, f1) \
67 BUILD_ASSERT_DECL(offsetof(struct flow, f0) / 4 \
68 == offsetof(struct flow, f1) / 4)
69
70/* Asserts that 'f0' and 'f1' are both sequential and within the same 32-bit
71 * aligned word in struct flow. */
72#define ASSERT_SEQUENTIAL_SAME_WORD(f0, f1) \
73 ASSERT_SEQUENTIAL(f0, f1); \
74 ASSERT_SAME_WORD(f0, f1)
75
419681da
JR
76/* miniflow_extract() assumes the following to be true to optimize the
77 * extraction process. */
268eca11
BP
78ASSERT_SEQUENTIAL_SAME_WORD(nw_frag, nw_tos);
79ASSERT_SEQUENTIAL_SAME_WORD(nw_tos, nw_ttl);
80ASSERT_SEQUENTIAL_SAME_WORD(nw_ttl, nw_proto);
419681da 81
d70e8c28
JR
82/* TCP flags in the middle of a BE64, zeroes in the other half. */
83BUILD_ASSERT_DECL(offsetof(struct flow, tcp_flags) % 8 == 4);
84
419681da
JR
85#if WORDS_BIGENDIAN
86#define TCP_FLAGS_BE32(tcp_ctl) ((OVS_FORCE ovs_be32)TCP_FLAGS_BE16(tcp_ctl) \
87 << 16)
88#else
89#define TCP_FLAGS_BE32(tcp_ctl) ((OVS_FORCE ovs_be32)TCP_FLAGS_BE16(tcp_ctl))
90#endif
91
268eca11 92ASSERT_SEQUENTIAL_SAME_WORD(tp_src, tp_dst);
419681da
JR
93
94/* Removes 'size' bytes from the head end of '*datap', of size '*sizep', which
95 * must contain at least 'size' bytes of data. Returns the first byte of data
96 * removed. */
97static inline const void *
4c0e587c 98data_pull(const void **datap, size_t *sizep, size_t size)
a26ef517 99{
4c0e587c 100 const char *data = *datap;
419681da
JR
101 *datap = data + size;
102 *sizep -= size;
103 return data;
a26ef517
JP
104}
105
419681da
JR
106/* If '*datap' has at least 'size' bytes of data, removes that many bytes from
107 * the head end of '*datap' and returns the first byte removed. Otherwise,
108 * returns a null pointer without modifying '*datap'. */
109static inline const void *
4c0e587c 110data_try_pull(const void **datap, size_t *sizep, size_t size)
064af421 111{
419681da 112 return OVS_LIKELY(*sizep >= size) ? data_pull(datap, sizep, size) : NULL;
064af421
BP
113}
114
419681da
JR
115/* Context for pushing data to a miniflow. */
116struct mf_ctx {
5fcff47b 117 struct flowmap map;
d70e8c28
JR
118 uint64_t *data;
119 uint64_t * const end;
419681da 120};
064af421 121
419681da
JR
122/* miniflow_push_* macros allow filling in a miniflow data values in order.
123 * Assertions are needed only when the layout of the struct flow is modified.
124 * 'ofs' is a compile-time constant, which allows most of the code be optimized
694ffecc 125 * away. Some GCC versions gave warnings on ALWAYS_INLINE, so these are
419681da
JR
126 * defined as macros. */
127
2482b0b0 128#if (FLOW_WC_SEQ != 39)
419681da 129#define MINIFLOW_ASSERT(X) ovs_assert(X)
dce96af8
DDP
130BUILD_MESSAGE("FLOW_WC_SEQ changed: miniflow_extract() will have runtime "
131 "assertions enabled. Consider updating FLOW_WC_SEQ after "
132 "testing")
419681da
JR
133#else
134#define MINIFLOW_ASSERT(X)
135#endif
136
5fcff47b
JR
137/* True if 'IDX' and higher bits are not set. */
138#define ASSERT_FLOWMAP_NOT_SET(FM, IDX) \
c2581ccf 139{ \
5fcff47b 140 MINIFLOW_ASSERT(!((FM)->bits[(IDX) / MAP_T_BITS] & \
e807a1a6 141 (MAP_MAX << ((IDX) % MAP_T_BITS)))); \
5fcff47b
JR
142 for (size_t i = (IDX) / MAP_T_BITS + 1; i < FLOWMAP_UNITS; i++) { \
143 MINIFLOW_ASSERT(!(FM)->bits[i]); \
c2581ccf
JR
144 } \
145}
361d808d 146
5fcff47b
JR
147#define miniflow_set_map(MF, OFS) \
148 { \
149 ASSERT_FLOWMAP_NOT_SET(&MF.map, (OFS)); \
150 flowmap_set(&MF.map, (OFS), 1); \
c2581ccf 151}
361d808d 152
e807a1a6
SH
153#define miniflow_assert_in_map(MF, OFS) \
154 MINIFLOW_ASSERT(flowmap_is_set(&MF.map, (OFS))); \
5fcff47b
JR
155 ASSERT_FLOWMAP_NOT_SET(&MF.map, (OFS) + 1)
156
157#define miniflow_push_uint64_(MF, OFS, VALUE) \
158{ \
159 MINIFLOW_ASSERT(MF.data < MF.end && (OFS) % 8 == 0); \
160 *MF.data++ = VALUE; \
161 miniflow_set_map(MF, OFS / 8); \
d31f1109
JP
162}
163
5fcff47b 164#define miniflow_push_be64_(MF, OFS, VALUE) \
d70e8c28 165 miniflow_push_uint64_(MF, OFS, (OVS_FORCE uint64_t)(VALUE))
419681da 166
5fcff47b
JR
167#define miniflow_push_uint32_(MF, OFS, VALUE) \
168 { \
169 MINIFLOW_ASSERT(MF.data < MF.end); \
170 \
171 if ((OFS) % 8 == 0) { \
172 miniflow_set_map(MF, OFS / 8); \
173 *(uint32_t *)MF.data = VALUE; \
174 } else if ((OFS) % 8 == 4) { \
175 miniflow_assert_in_map(MF, OFS / 8); \
176 *((uint32_t *)MF.data + 1) = VALUE; \
177 MF.data++; \
178 } \
d70e8c28
JR
179}
180
181#define miniflow_push_be32_(MF, OFS, VALUE) \
182 miniflow_push_uint32_(MF, OFS, (OVS_FORCE uint32_t)(VALUE))
183
5fcff47b
JR
184#define miniflow_push_uint16_(MF, OFS, VALUE) \
185{ \
186 MINIFLOW_ASSERT(MF.data < MF.end); \
187 \
188 if ((OFS) % 8 == 0) { \
189 miniflow_set_map(MF, OFS / 8); \
190 *(uint16_t *)MF.data = VALUE; \
191 } else if ((OFS) % 8 == 2) { \
192 miniflow_assert_in_map(MF, OFS / 8); \
193 *((uint16_t *)MF.data + 1) = VALUE; \
194 } else if ((OFS) % 8 == 4) { \
195 miniflow_assert_in_map(MF, OFS / 8); \
196 *((uint16_t *)MF.data + 2) = VALUE; \
197 } else if ((OFS) % 8 == 6) { \
198 miniflow_assert_in_map(MF, OFS / 8); \
199 *((uint16_t *)MF.data + 3) = VALUE; \
200 MF.data++; \
201 } \
202}
203
1dcf9ac7
SH
204#define miniflow_push_uint8_(MF, OFS, VALUE) \
205{ \
206 MINIFLOW_ASSERT(MF.data < MF.end); \
207 \
208 if ((OFS) % 8 == 0) { \
209 miniflow_set_map(MF, OFS / 8); \
210 *(uint8_t *)MF.data = VALUE; \
211 } else if ((OFS) % 8 == 7) { \
212 miniflow_assert_in_map(MF, OFS / 8); \
213 *((uint8_t *)MF.data + 7) = VALUE; \
214 MF.data++; \
215 } else { \
216 miniflow_assert_in_map(MF, OFS / 8); \
217 *((uint8_t *)MF.data + ((OFS) % 8)) = VALUE; \
218 } \
219}
220
5fcff47b
JR
221#define miniflow_pad_to_64_(MF, OFS) \
222{ \
223 MINIFLOW_ASSERT((OFS) % 8 != 0); \
224 miniflow_assert_in_map(MF, OFS / 8); \
225 \
226 memset((uint8_t *)MF.data + (OFS) % 8, 0, 8 - (OFS) % 8); \
227 MF.data++; \
d70e8c28
JR
228}
229
1589ee5a
SH
230#define miniflow_pad_from_64_(MF, OFS) \
231{ \
232 MINIFLOW_ASSERT(MF.data < MF.end); \
233 \
234 MINIFLOW_ASSERT((OFS) % 8 != 0); \
235 miniflow_set_map(MF, OFS / 8); \
236 \
237 memset((uint8_t *)MF.data, 0, (OFS) % 8); \
238}
239
d70e8c28 240#define miniflow_push_be16_(MF, OFS, VALUE) \
419681da
JR
241 miniflow_push_uint16_(MF, OFS, (OVS_FORCE uint16_t)VALUE);
242
1dcf9ac7
SH
243#define miniflow_push_be8_(MF, OFS, VALUE) \
244 miniflow_push_uint8_(MF, OFS, (OVS_FORCE uint8_t)VALUE);
245
5fcff47b
JR
246#define miniflow_set_maps(MF, OFS, N_WORDS) \
247{ \
248 size_t ofs = (OFS); \
249 size_t n_words = (N_WORDS); \
250 \
251 MINIFLOW_ASSERT(n_words && MF.data + n_words <= MF.end); \
252 ASSERT_FLOWMAP_NOT_SET(&MF.map, ofs); \
253 flowmap_set(&MF.map, ofs, n_words); \
361d808d
JR
254}
255
419681da
JR
256/* Data at 'valuep' may be unaligned. */
257#define miniflow_push_words_(MF, OFS, VALUEP, N_WORDS) \
258{ \
361d808d
JR
259 MINIFLOW_ASSERT((OFS) % 8 == 0); \
260 miniflow_set_maps(MF, (OFS) / 8, (N_WORDS)); \
261 memcpy(MF.data, (VALUEP), (N_WORDS) * sizeof *MF.data); \
262 MF.data += (N_WORDS); \
064af421
BP
263}
264
d70e8c28
JR
265/* Push 32-bit words padded to 64-bits. */
266#define miniflow_push_words_32_(MF, OFS, VALUEP, N_WORDS) \
267{ \
361d808d 268 miniflow_set_maps(MF, (OFS) / 8, DIV_ROUND_UP(N_WORDS, 2)); \
d70e8c28
JR
269 memcpy(MF.data, (VALUEP), (N_WORDS) * sizeof(uint32_t)); \
270 MF.data += DIV_ROUND_UP(N_WORDS, 2); \
d70e8c28
JR
271 if ((N_WORDS) & 1) { \
272 *((uint32_t *)MF.data - 1) = 0; \
273 } \
274}
50f06e16 275
d70e8c28
JR
276/* Data at 'valuep' may be unaligned. */
277/* MACs start 64-aligned, and must be followed by other data or padding. */
278#define miniflow_push_macs_(MF, OFS, VALUEP) \
279{ \
361d808d 280 miniflow_set_maps(MF, (OFS) / 8, 2); \
d70e8c28
JR
281 memcpy(MF.data, (VALUEP), 2 * ETH_ADDR_LEN); \
282 MF.data += 1; /* First word only. */ \
d70e8c28 283}
50f06e16 284
d70e8c28
JR
285#define miniflow_push_uint32(MF, FIELD, VALUE) \
286 miniflow_push_uint32_(MF, offsetof(struct flow, FIELD), VALUE)
50f06e16 287
d70e8c28
JR
288#define miniflow_push_be32(MF, FIELD, VALUE) \
289 miniflow_push_be32_(MF, offsetof(struct flow, FIELD), VALUE)
50f06e16 290
d70e8c28 291#define miniflow_push_uint16(MF, FIELD, VALUE) \
419681da 292 miniflow_push_uint16_(MF, offsetof(struct flow, FIELD), VALUE)
9e69bc5f 293
d70e8c28 294#define miniflow_push_be16(MF, FIELD, VALUE) \
419681da 295 miniflow_push_be16_(MF, offsetof(struct flow, FIELD), VALUE)
9e69bc5f 296
1dcf9ac7
SH
297#define miniflow_push_uint8(MF, FIELD, VALUE) \
298 miniflow_push_uint8_(MF, offsetof(struct flow, FIELD), VALUE)
299
d70e8c28 300#define miniflow_pad_to_64(MF, FIELD) \
06f41fc4 301 miniflow_pad_to_64_(MF, OFFSETOFEND(struct flow, FIELD))
d70e8c28 302
1589ee5a
SH
303#define miniflow_pad_from_64(MF, FIELD) \
304 miniflow_pad_from_64_(MF, offsetof(struct flow, FIELD))
305
419681da
JR
306#define miniflow_push_words(MF, FIELD, VALUEP, N_WORDS) \
307 miniflow_push_words_(MF, offsetof(struct flow, FIELD), VALUEP, N_WORDS)
064af421 308
d70e8c28
JR
309#define miniflow_push_words_32(MF, FIELD, VALUEP, N_WORDS) \
310 miniflow_push_words_32_(MF, offsetof(struct flow, FIELD), VALUEP, N_WORDS)
311
312#define miniflow_push_macs(MF, FIELD, VALUEP) \
313 miniflow_push_macs_(MF, offsetof(struct flow, FIELD), VALUEP)
314
daf4d3c1
JR
315/* Return the pointer to the miniflow data when called BEFORE the corresponding
316 * push. */
317#define miniflow_pointer(MF, FIELD) \
318 (void *)((uint8_t *)MF.data + ((offsetof(struct flow, FIELD)) % 8))
319
419681da
JR
320/* Pulls the MPLS headers at '*datap' and returns the count of them. */
321static inline int
4c0e587c 322parse_mpls(const void **datap, size_t *sizep)
d31f1109 323{
419681da
JR
324 const struct mpls_hdr *mh;
325 int count = 0;
d31f1109 326
419681da
JR
327 while ((mh = data_try_pull(datap, sizep, sizeof *mh))) {
328 count++;
329 if (mh->mpls_lse.lo & htons(1 << MPLS_BOS_SHIFT)) {
d31f1109
JP
330 break;
331 }
419681da 332 }
ba8561c6 333 return MIN(count, FLOW_MAX_MPLS_LABELS);
419681da 334}
d31f1109 335
f0fb825a
EG
336/* passed vlan_hdrs arg must be at least size FLOW_MAX_VLAN_HEADERS. */
337static inline ALWAYS_INLINE size_t
338parse_vlan(const void **datap, size_t *sizep, union flow_vlan_hdr *vlan_hdrs)
419681da 339{
f0fb825a 340 const ovs_be16 *eth_type;
d31f1109 341
f0fb825a 342 memset(vlan_hdrs, 0, sizeof(union flow_vlan_hdr) * FLOW_MAX_VLAN_HEADERS);
419681da 343 data_pull(datap, sizep, ETH_ADDR_LEN * 2);
d31f1109 344
f0fb825a
EG
345 eth_type = *datap;
346
347 size_t n;
348 for (n = 0; eth_type_vlan(*eth_type) && n < flow_vlan_limit; n++) {
349 if (OVS_UNLIKELY(*sizep < sizeof(ovs_be32) + sizeof(ovs_be16))) {
350 break;
d31f1109 351 }
f0fb825a
EG
352
353 const ovs_16aligned_be32 *qp = data_pull(datap, sizep, sizeof *qp);
354 vlan_hdrs[n].qtag = get_16aligned_be32(qp);
355 vlan_hdrs[n].tci |= htons(VLAN_CFI);
356 eth_type = *datap;
d31f1109 357 }
f0fb825a 358 return n;
d31f1109
JP
359}
360
206b60d4 361static inline ALWAYS_INLINE ovs_be16
4c0e587c 362parse_ethertype(const void **datap, size_t *sizep)
88366484 363{
419681da
JR
364 const struct llc_snap_header *llc;
365 ovs_be16 proto;
5a51b2cd 366
419681da
JR
367 proto = *(ovs_be16 *) data_pull(datap, sizep, sizeof proto);
368 if (OVS_LIKELY(ntohs(proto) >= ETH_TYPE_MIN)) {
369 return proto;
88366484 370 }
5a51b2cd 371
419681da
JR
372 if (OVS_UNLIKELY(*sizep < sizeof *llc)) {
373 return htons(FLOW_DL_TYPE_NONE);
88366484 374 }
5a51b2cd 375
419681da
JR
376 llc = *datap;
377 if (OVS_UNLIKELY(llc->llc.llc_dsap != LLC_DSAP_SNAP
378 || llc->llc.llc_ssap != LLC_SSAP_SNAP
379 || llc->llc.llc_cntl != LLC_CNTL_SNAP
380 || memcmp(llc->snap.snap_org, SNAP_ORG_ETHERNET,
381 sizeof llc->snap.snap_org))) {
382 return htons(FLOW_DL_TYPE_NONE);
c6bcb685 383 }
c6bcb685 384
419681da 385 data_pull(datap, sizep, sizeof *llc);
685a51a5 386
419681da
JR
387 if (OVS_LIKELY(ntohs(llc->snap.snap_type) >= ETH_TYPE_MIN)) {
388 return llc->snap.snap_type;
685a51a5
JP
389 }
390
419681da
JR
391 return htons(FLOW_DL_TYPE_NONE);
392}
685a51a5 393
daf4d3c1
JR
394/* Returns 'true' if the packet is an ND packet. In that case the '*nd_target'
395 * and 'arp_buf[]' are filled in. If the packet is not an ND pacet, 'false' is
396 * returned and no values are filled in on '*nd_target' or 'arp_buf[]'. */
397static inline bool
4c0e587c 398parse_icmpv6(const void **datap, size_t *sizep, const struct icmp6_hdr *icmp,
419681da 399 const struct in6_addr **nd_target,
74ff3298 400 struct eth_addr arp_buf[2])
419681da 401{
daf4d3c1
JR
402 if (icmp->icmp6_code != 0 ||
403 (icmp->icmp6_type != ND_NEIGHBOR_SOLICIT &&
404 icmp->icmp6_type != ND_NEIGHBOR_ADVERT)) {
405 return false;
406 }
685a51a5 407
daf4d3c1
JR
408 arp_buf[0] = eth_addr_zero;
409 arp_buf[1] = eth_addr_zero;
410 *nd_target = data_try_pull(datap, sizep, sizeof **nd_target);
411 if (OVS_UNLIKELY(!*nd_target)) {
412 return true;
413 }
685a51a5 414
daf4d3c1
JR
415 while (*sizep >= 8) {
416 /* The minimum size of an option is 8 bytes, which also is
417 * the size of Ethernet link-layer options. */
86d46f3c
ZKL
418 const struct ovs_nd_lla_opt *lla_opt = *datap;
419 int opt_len = lla_opt->len * ND_LLA_OPT_LEN;
88366484 420
daf4d3c1
JR
421 if (!opt_len || opt_len > *sizep) {
422 return true;
423 }
685a51a5 424
daf4d3c1
JR
425 /* Store the link layer address if the appropriate option is
426 * provided. It is considered an error if the same link
427 * layer option is specified twice. */
86d46f3c 428 if (lla_opt->type == ND_OPT_SOURCE_LINKADDR && opt_len == 8) {
daf4d3c1 429 if (OVS_LIKELY(eth_addr_is_zero(arp_buf[0]))) {
86d46f3c 430 arp_buf[0] = lla_opt->mac;
daf4d3c1
JR
431 } else {
432 goto invalid;
685a51a5 433 }
86d46f3c 434 } else if (lla_opt->type == ND_OPT_TARGET_LINKADDR && opt_len == 8) {
daf4d3c1 435 if (OVS_LIKELY(eth_addr_is_zero(arp_buf[1]))) {
86d46f3c 436 arp_buf[1] = lla_opt->mac;
daf4d3c1
JR
437 } else {
438 goto invalid;
685a51a5 439 }
685a51a5 440 }
685a51a5 441
daf4d3c1
JR
442 if (OVS_UNLIKELY(!data_try_pull(datap, sizep, opt_len))) {
443 return true;
444 }
445 }
446 return true;
685a51a5
JP
447
448invalid:
cc5dba2f 449 *nd_target = NULL;
74ff3298
JR
450 arp_buf[0] = eth_addr_zero;
451 arp_buf[1] = eth_addr_zero;
daf4d3c1 452 return true;
685a51a5
JP
453}
454
94a81e40
DDP
455static inline bool
456parse_ipv6_ext_hdrs__(const void **datap, size_t *sizep, uint8_t *nw_proto,
457 uint8_t *nw_frag)
458{
459 while (1) {
460 if (OVS_LIKELY((*nw_proto != IPPROTO_HOPOPTS)
461 && (*nw_proto != IPPROTO_ROUTING)
462 && (*nw_proto != IPPROTO_DSTOPTS)
463 && (*nw_proto != IPPROTO_AH)
464 && (*nw_proto != IPPROTO_FRAGMENT))) {
465 /* It's either a terminal header (e.g., TCP, UDP) or one we
466 * don't understand. In either case, we're done with the
467 * packet, so use it to fill in 'nw_proto'. */
468 return true;
469 }
470
471 /* We only verify that at least 8 bytes of the next header are
472 * available, but many of these headers are longer. Ensure that
473 * accesses within the extension header are within those first 8
474 * bytes. All extension headers are required to be at least 8
475 * bytes. */
476 if (OVS_UNLIKELY(*sizep < 8)) {
477 return false;
478 }
479
480 if ((*nw_proto == IPPROTO_HOPOPTS)
481 || (*nw_proto == IPPROTO_ROUTING)
482 || (*nw_proto == IPPROTO_DSTOPTS)) {
483 /* These headers, while different, have the fields we care
484 * about in the same location and with the same
485 * interpretation. */
486 const struct ip6_ext *ext_hdr = *datap;
487 *nw_proto = ext_hdr->ip6e_nxt;
488 if (OVS_UNLIKELY(!data_try_pull(datap, sizep,
489 (ext_hdr->ip6e_len + 1) * 8))) {
490 return false;
491 }
492 } else if (*nw_proto == IPPROTO_AH) {
493 /* A standard AH definition isn't available, but the fields
494 * we care about are in the same location as the generic
495 * option header--only the header length is calculated
496 * differently. */
497 const struct ip6_ext *ext_hdr = *datap;
498 *nw_proto = ext_hdr->ip6e_nxt;
499 if (OVS_UNLIKELY(!data_try_pull(datap, sizep,
500 (ext_hdr->ip6e_len + 2) * 4))) {
501 return false;
502 }
503 } else if (*nw_proto == IPPROTO_FRAGMENT) {
504 const struct ovs_16aligned_ip6_frag *frag_hdr = *datap;
505
506 *nw_proto = frag_hdr->ip6f_nxt;
507 if (!data_try_pull(datap, sizep, sizeof *frag_hdr)) {
508 return false;
509 }
510
511 /* We only process the first fragment. */
512 if (frag_hdr->ip6f_offlg != htons(0)) {
513 *nw_frag = FLOW_NW_FRAG_ANY;
514 if ((frag_hdr->ip6f_offlg & IP6F_OFF_MASK) != htons(0)) {
515 *nw_frag |= FLOW_NW_FRAG_LATER;
516 *nw_proto = IPPROTO_FRAGMENT;
517 return true;
518 }
519 }
520 }
521 }
522}
523
524bool
525parse_ipv6_ext_hdrs(const void **datap, size_t *sizep, uint8_t *nw_proto,
526 uint8_t *nw_frag)
527{
528 return parse_ipv6_ext_hdrs__(datap, sizep, nw_proto, nw_frag);
529}
530
2482b0b0
JS
531/* Initializes 'flow' members from 'packet' and 'md', taking the packet type
532 * into account.
deedf7e7 533 *
2482b0b0 534 * Initializes the layer offsets as follows:
ca78c6b6 535 *
2482b0b0
JS
536 * - packet->l2_5_ofs to the
537 * * the start of the MPLS shim header. Can be zero, if the
538 * packet is of type (OFPHTN_ETHERTYPE, ETH_TYPE_MPLS).
539 * * UINT16_MAX when there is no MPLS shim header.
ca78c6b6 540 *
2482b0b0
JS
541 * - packet->l3_ofs is set to
542 * * zero if the packet_type is in name space OFPHTN_ETHERTYPE
543 * and there is no MPLS shim header.
544 * * just past the Ethernet header, or just past the vlan_header if
545 * one is present, to the first byte of the payload of the
546 * Ethernet frame if the packet type is Ethernet and there is
547 * no MPLS shim header.
548 * * just past the MPLS label stack to the first byte of the MPLS
549 * payload if there is at least one MPLS shim header.
550 * * UINT16_MAX if the packet type is Ethernet and the frame is
551 * too short to contain an Ethernet header.
ca78c6b6 552 *
2482b0b0
JS
553 * - packet->l4_ofs is set to just past the IPv4 or IPv6 header, if one is
554 * present and the packet has at least the content used for the fields
555 * of interest for the flow, otherwise UINT16_MAX.
ca78c6b6 556 */
7257b535 557void
cf62fa4c 558flow_extract(struct dp_packet *packet, struct flow *flow)
064af421 559{
27bbe15d
JR
560 struct {
561 struct miniflow mf;
d70e8c28 562 uint64_t buf[FLOW_U64S];
27bbe15d 563 } m;
064af421
BP
564
565 COVERAGE_INC(flow_extract);
566
cf62fa4c 567 miniflow_extract(packet, &m.mf);
27bbe15d 568 miniflow_expand(&m.mf, flow);
419681da 569}
296e07ac 570
27bbe15d 571/* Caller is responsible for initializing 'dst' with enough storage for
d70e8c28 572 * FLOW_U64S * 8 bytes. */
419681da 573void
cf62fa4c 574miniflow_extract(struct dp_packet *packet, struct miniflow *dst)
419681da 575{
cf62fa4c 576 const struct pkt_metadata *md = &packet->md;
4c0e587c 577 const void *data = dp_packet_data(packet);
cf62fa4c 578 size_t size = dp_packet_size(packet);
2482b0b0 579 ovs_be32 packet_type = packet->packet_type;
09b0fa9c 580 uint64_t *values = miniflow_values(dst);
5fcff47b
JR
581 struct mf_ctx mf = { FLOWMAP_EMPTY_INITIALIZER, values,
582 values + FLOW_U64S };
2482b0b0
JS
583 const char *frame;
584 ovs_be16 dl_type = OVS_BE16_MAX;
419681da 585 uint8_t nw_frag, nw_tos, nw_ttl, nw_proto;
daf4d3c1
JR
586 uint8_t *ct_nw_proto_p = NULL;
587 ovs_be16 ct_tp_src = 0, ct_tp_dst = 0;
419681da
JR
588
589 /* Metadata. */
ffe4c74f 590 if (flow_tnl_dst_is_set(&md->tunnel)) {
cf62fa4c 591 miniflow_push_words(mf, tunnel, &md->tunnel,
9ad11dbe
JG
592 offsetof(struct flow_tnl, metadata) /
593 sizeof(uint64_t));
6728d578
JG
594
595 if (!(md->tunnel.flags & FLOW_TNL_F_UDPIF)) {
596 if (md->tunnel.metadata.present.map) {
597 miniflow_push_words(mf, tunnel.metadata, &md->tunnel.metadata,
598 sizeof md->tunnel.metadata /
599 sizeof(uint64_t));
600 }
601 } else {
602 if (md->tunnel.metadata.present.len) {
603 miniflow_push_words(mf, tunnel.metadata.present,
604 &md->tunnel.metadata.present, 1);
605 miniflow_push_words(mf, tunnel.metadata.opts.gnv,
606 md->tunnel.metadata.opts.gnv,
607 DIV_ROUND_UP(md->tunnel.metadata.present.len,
608 sizeof(uint64_t)));
609 }
9ad11dbe 610 }
cf62fa4c
PS
611 }
612 if (md->skb_priority || md->pkt_mark) {
613 miniflow_push_uint32(mf, skb_priority, md->skb_priority);
614 miniflow_push_uint32(mf, pkt_mark, md->pkt_mark);
615 }
616 miniflow_push_uint32(mf, dp_hash, md->dp_hash);
617 miniflow_push_uint32(mf, in_port, odp_to_u32(md->in_port.odp_port));
6cf5c521 618 if (md->ct_state) {
cf62fa4c 619 miniflow_push_uint32(mf, recirc_id, md->recirc_id);
2a28ccc8 620 miniflow_push_uint8(mf, ct_state, md->ct_state);
daf4d3c1
JR
621 ct_nw_proto_p = miniflow_pointer(mf, ct_nw_proto);
622 miniflow_push_uint8(mf, ct_nw_proto, 0);
07659514 623 miniflow_push_uint16(mf, ct_zone, md->ct_zone);
6cf5c521
DB
624 } else if (md->recirc_id) {
625 miniflow_push_uint32(mf, recirc_id, md->recirc_id);
626 miniflow_pad_to_64(mf, recirc_id);
296e07ac 627 }
064af421 628
8e53fe8c
JS
629 if (md->ct_state) {
630 miniflow_push_uint32(mf, ct_mark, md->ct_mark);
2482b0b0 631 miniflow_push_be32(mf, packet_type, packet_type);
9daf2348 632
2ff8484b 633 if (!ovs_u128_is_zero(md->ct_label)) {
9daf2348
JS
634 miniflow_push_words(mf, ct_label, &md->ct_label,
635 sizeof md->ct_label / sizeof(uint64_t));
636 }
2482b0b0
JS
637 } else {
638 miniflow_pad_from_64(mf, packet_type);
639 miniflow_push_be32(mf, packet_type, packet_type);
8e53fe8c
JS
640 }
641
419681da 642 /* Initialize packet's layer pointer and offsets. */
2482b0b0 643 frame = data;
82eb5b0a 644 dp_packet_reset_offsets(packet);
064af421 645
2482b0b0
JS
646 if (packet_type == htonl(PT_ETH)) {
647 /* Must have full Ethernet header to proceed. */
648 if (OVS_UNLIKELY(size < sizeof(struct eth_header))) {
649 goto out;
650 } else {
651 /* Link layer. */
652 ASSERT_SEQUENTIAL(dl_dst, dl_src);
653 miniflow_push_macs(mf, dl_dst, data);
654
655 /* VLAN */
656 union flow_vlan_hdr vlans[FLOW_MAX_VLAN_HEADERS];
657 size_t num_vlans = parse_vlan(&data, &size, vlans);
658
659 dl_type = parse_ethertype(&data, &size);
660 miniflow_push_be16(mf, dl_type, dl_type);
661 miniflow_pad_to_64(mf, dl_type);
662 if (num_vlans > 0) {
663 miniflow_push_words_32(mf, vlans, vlans, num_vlans);
664 }
f0fb825a 665
2482b0b0
JS
666 }
667 } else {
668 /* Take dl_type from packet_type. */
669 dl_type = pt_ns_type_be(packet_type);
670 miniflow_pad_from_64(mf, dl_type);
419681da 671 miniflow_push_be16(mf, dl_type, dl_type);
2482b0b0 672 /* Do not push vlan_tci, pad instead */
f0fb825a 673 miniflow_pad_to_64(mf, dl_type);
50f06e16 674 }
50f06e16 675
419681da
JR
676 /* Parse mpls. */
677 if (OVS_UNLIKELY(eth_type_mpls(dl_type))) {
678 int count;
679 const void *mpls = data;
680
2482b0b0 681 packet->l2_5_ofs = (char *)data - frame;
419681da 682 count = parse_mpls(&data, &size);
d70e8c28 683 miniflow_push_words_32(mf, mpls_lse, mpls, count);
b02475c5
SH
684 }
685
ad128cc1 686 /* Network layer. */
2482b0b0 687 packet->l3_ofs = (char *)data - frame;
419681da
JR
688
689 nw_frag = 0;
690 if (OVS_LIKELY(dl_type == htons(ETH_TYPE_IP))) {
691 const struct ip_header *nh = data;
692 int ip_len;
fa8d9001 693 uint16_t tot_len;
419681da
JR
694
695 if (OVS_UNLIKELY(size < IP_HEADER_LEN)) {
696 goto out;
697 }
698 ip_len = IP_IHL(nh->ip_ihl_ver) * 4;
699
700 if (OVS_UNLIKELY(ip_len < IP_HEADER_LEN)) {
701 goto out;
702 }
fa8d9001
JR
703 if (OVS_UNLIKELY(size < ip_len)) {
704 goto out;
705 }
706 tot_len = ntohs(nh->ip_tot_len);
5b2cdc3f 707 if (OVS_UNLIKELY(tot_len > size || ip_len > tot_len)) {
fa8d9001
JR
708 goto out;
709 }
710 if (OVS_UNLIKELY(size - tot_len > UINT8_MAX)) {
711 goto out;
712 }
cf62fa4c 713 dp_packet_set_l2_pad_size(packet, size - tot_len);
fa8d9001 714 size = tot_len; /* Never pull padding. */
419681da
JR
715
716 /* Push both source and destination address at once. */
d70e8c28 717 miniflow_push_words(mf, nw_src, &nh->ip_src, 1);
daf4d3c1
JR
718 if (ct_nw_proto_p && !md->ct_orig_tuple_ipv6) {
719 *ct_nw_proto_p = md->ct_orig_tuple.ipv4.ipv4_proto;
720 if (*ct_nw_proto_p) {
721 miniflow_push_words(mf, ct_nw_src,
722 &md->ct_orig_tuple.ipv4.ipv4_src, 1);
723 ct_tp_src = md->ct_orig_tuple.ipv4.src_port;
724 ct_tp_dst = md->ct_orig_tuple.ipv4.dst_port;
725 }
726 }
d70e8c28
JR
727
728 miniflow_push_be32(mf, ipv6_label, 0); /* Padding for IPv4. */
419681da
JR
729
730 nw_tos = nh->ip_tos;
731 nw_ttl = nh->ip_ttl;
732 nw_proto = nh->ip_proto;
733 if (OVS_UNLIKELY(IP_IS_FRAGMENT(nh->ip_frag_off))) {
734 nw_frag = FLOW_NW_FRAG_ANY;
735 if (nh->ip_frag_off & htons(IP_FRAG_OFF_MASK)) {
736 nw_frag |= FLOW_NW_FRAG_LATER;
737 }
738 }
419681da 739 data_pull(&data, &size, ip_len);
419681da
JR
740 } else if (dl_type == htons(ETH_TYPE_IPV6)) {
741 const struct ovs_16aligned_ip6_hdr *nh;
742 ovs_be32 tc_flow;
fa8d9001 743 uint16_t plen;
419681da
JR
744
745 if (OVS_UNLIKELY(size < sizeof *nh)) {
746 goto out;
747 }
748 nh = data_pull(&data, &size, sizeof *nh);
749
fa8d9001
JR
750 plen = ntohs(nh->ip6_plen);
751 if (OVS_UNLIKELY(plen > size)) {
752 goto out;
753 }
754 /* Jumbo Payload option not supported yet. */
755 if (OVS_UNLIKELY(size - plen > UINT8_MAX)) {
756 goto out;
757 }
cf62fa4c 758 dp_packet_set_l2_pad_size(packet, size - plen);
fa8d9001
JR
759 size = plen; /* Never pull padding. */
760
419681da 761 miniflow_push_words(mf, ipv6_src, &nh->ip6_src,
d70e8c28 762 sizeof nh->ip6_src / 8);
419681da 763 miniflow_push_words(mf, ipv6_dst, &nh->ip6_dst,
d70e8c28 764 sizeof nh->ip6_dst / 8);
daf4d3c1
JR
765 if (ct_nw_proto_p && md->ct_orig_tuple_ipv6) {
766 *ct_nw_proto_p = md->ct_orig_tuple.ipv6.ipv6_proto;
767 if (*ct_nw_proto_p) {
768 miniflow_push_words(mf, ct_ipv6_src,
769 &md->ct_orig_tuple.ipv6.ipv6_src,
770 2 *
771 sizeof md->ct_orig_tuple.ipv6.ipv6_src / 8);
772 ct_tp_src = md->ct_orig_tuple.ipv6.src_port;
773 ct_tp_dst = md->ct_orig_tuple.ipv6.dst_port;
774 }
775 }
419681da
JR
776
777 tc_flow = get_16aligned_be32(&nh->ip6_flow);
778 {
779 ovs_be32 label = tc_flow & htonl(IPV6_LABEL_MASK);
d70e8c28 780 miniflow_push_be32(mf, ipv6_label, label);
419681da
JR
781 }
782
783 nw_tos = ntohl(tc_flow) >> 20;
784 nw_ttl = nh->ip6_hlim;
785 nw_proto = nh->ip6_nxt;
786
94a81e40
DDP
787 if (!parse_ipv6_ext_hdrs__(&data, &size, &nw_proto, &nw_frag)) {
788 goto out;
50f06e16 789 }
419681da
JR
790 } else {
791 if (dl_type == htons(ETH_TYPE_ARP) ||
792 dl_type == htons(ETH_TYPE_RARP)) {
74ff3298 793 struct eth_addr arp_buf[2];
419681da
JR
794 const struct arp_eth_header *arp = (const struct arp_eth_header *)
795 data_try_pull(&data, &size, ARP_ETH_HEADER_LEN);
796
797 if (OVS_LIKELY(arp) && OVS_LIKELY(arp->ar_hrd == htons(1))
798 && OVS_LIKELY(arp->ar_pro == htons(ETH_TYPE_IP))
799 && OVS_LIKELY(arp->ar_hln == ETH_ADDR_LEN)
800 && OVS_LIKELY(arp->ar_pln == 4)) {
d70e8c28
JR
801 miniflow_push_be32(mf, nw_src,
802 get_16aligned_be32(&arp->ar_spa));
803 miniflow_push_be32(mf, nw_dst,
804 get_16aligned_be32(&arp->ar_tpa));
419681da
JR
805
806 /* We only match on the lower 8 bits of the opcode. */
807 if (OVS_LIKELY(ntohs(arp->ar_op) <= 0xff)) {
d70e8c28 808 miniflow_push_be32(mf, ipv6_label, 0); /* Pad with ARP. */
419681da
JR
809 miniflow_push_be32(mf, nw_frag, htonl(ntohs(arp->ar_op)));
810 }
d31f1109 811
419681da 812 /* Must be adjacent. */
268eca11 813 ASSERT_SEQUENTIAL(arp_sha, arp_tha);
419681da 814
74ff3298
JR
815 arp_buf[0] = arp->ar_sha;
816 arp_buf[1] = arp->ar_tha;
d70e8c28 817 miniflow_push_macs(mf, arp_sha, arp_buf);
06f41fc4 818 miniflow_pad_to_64(mf, arp_tha);
419681da 819 }
d31f1109 820 }
419681da
JR
821 goto out;
822 }
823
2482b0b0 824 packet->l4_ofs = (char *)data - frame;
419681da 825 miniflow_push_be32(mf, nw_frag,
a13784ba 826 bytes_to_be32(nw_frag, nw_tos, nw_ttl, nw_proto));
419681da
JR
827
828 if (OVS_LIKELY(!(nw_frag & FLOW_NW_FRAG_LATER))) {
829 if (OVS_LIKELY(nw_proto == IPPROTO_TCP)) {
830 if (OVS_LIKELY(size >= TCP_HEADER_LEN)) {
831 const struct tcp_header *tcp = data;
832
74ff3298 833 miniflow_push_be32(mf, arp_tha.ea[2], 0);
419681da
JR
834 miniflow_push_be32(mf, tcp_flags,
835 TCP_FLAGS_BE32(tcp->tcp_ctl));
115f2481
JR
836 miniflow_push_be16(mf, tp_src, tcp->tcp_src);
837 miniflow_push_be16(mf, tp_dst, tcp->tcp_dst);
daf4d3c1
JR
838 miniflow_push_be16(mf, ct_tp_src, ct_tp_src);
839 miniflow_push_be16(mf, ct_tp_dst, ct_tp_dst);
419681da
JR
840 }
841 } else if (OVS_LIKELY(nw_proto == IPPROTO_UDP)) {
842 if (OVS_LIKELY(size >= UDP_HEADER_LEN)) {
843 const struct udp_header *udp = data;
844
115f2481
JR
845 miniflow_push_be16(mf, tp_src, udp->udp_src);
846 miniflow_push_be16(mf, tp_dst, udp->udp_dst);
daf4d3c1
JR
847 miniflow_push_be16(mf, ct_tp_src, ct_tp_src);
848 miniflow_push_be16(mf, ct_tp_dst, ct_tp_dst);
064af421 849 }
419681da
JR
850 } else if (OVS_LIKELY(nw_proto == IPPROTO_SCTP)) {
851 if (OVS_LIKELY(size >= SCTP_HEADER_LEN)) {
852 const struct sctp_header *sctp = data;
a26ef517 853
115f2481
JR
854 miniflow_push_be16(mf, tp_src, sctp->sctp_src);
855 miniflow_push_be16(mf, tp_dst, sctp->sctp_dst);
daf4d3c1
JR
856 miniflow_push_be16(mf, ct_tp_src, ct_tp_src);
857 miniflow_push_be16(mf, ct_tp_dst, ct_tp_dst);
419681da
JR
858 }
859 } else if (OVS_LIKELY(nw_proto == IPPROTO_ICMP)) {
860 if (OVS_LIKELY(size >= ICMP_HEADER_LEN)) {
861 const struct icmp_header *icmp = data;
862
863 miniflow_push_be16(mf, tp_src, htons(icmp->icmp_type));
864 miniflow_push_be16(mf, tp_dst, htons(icmp->icmp_code));
daf4d3c1
JR
865 miniflow_push_be16(mf, ct_tp_src, ct_tp_src);
866 miniflow_push_be16(mf, ct_tp_dst, ct_tp_dst);
419681da 867 }
0e612675
FL
868 } else if (OVS_LIKELY(nw_proto == IPPROTO_IGMP)) {
869 if (OVS_LIKELY(size >= IGMP_HEADER_LEN)) {
870 const struct igmp_header *igmp = data;
871
872 miniflow_push_be16(mf, tp_src, htons(igmp->igmp_type));
873 miniflow_push_be16(mf, tp_dst, htons(igmp->igmp_code));
daf4d3c1
JR
874 miniflow_push_be16(mf, ct_tp_src, ct_tp_src);
875 miniflow_push_be16(mf, ct_tp_dst, ct_tp_dst);
0e612675
FL
876 miniflow_push_be32(mf, igmp_group_ip4,
877 get_16aligned_be32(&igmp->group));
daf4d3c1 878 miniflow_pad_to_64(mf, igmp_group_ip4);
0e612675 879 }
419681da
JR
880 } else if (OVS_LIKELY(nw_proto == IPPROTO_ICMPV6)) {
881 if (OVS_LIKELY(size >= sizeof(struct icmp6_hdr))) {
daf4d3c1
JR
882 const struct in6_addr *nd_target;
883 struct eth_addr arp_buf[2];
419681da
JR
884 const struct icmp6_hdr *icmp = data_pull(&data, &size,
885 sizeof *icmp);
daf4d3c1
JR
886 if (parse_icmpv6(&data, &size, icmp, &nd_target, arp_buf)) {
887 if (nd_target) {
888 miniflow_push_words(mf, nd_target, nd_target,
889 sizeof *nd_target / sizeof(uint64_t));
890 }
891 miniflow_push_macs(mf, arp_sha, arp_buf);
892 miniflow_pad_to_64(mf, arp_tha);
893 miniflow_push_be16(mf, tp_src, htons(icmp->icmp6_type));
894 miniflow_push_be16(mf, tp_dst, htons(icmp->icmp6_code));
895 miniflow_pad_to_64(mf, tp_dst);
896 } else {
897 /* ICMPv6 but not ND. */
898 miniflow_push_be16(mf, tp_src, htons(icmp->icmp6_type));
899 miniflow_push_be16(mf, tp_dst, htons(icmp->icmp6_code));
900 miniflow_push_be16(mf, ct_tp_src, ct_tp_src);
901 miniflow_push_be16(mf, ct_tp_dst, ct_tp_dst);
419681da
JR
902 }
903 }
064af421
BP
904 }
905 }
419681da 906 out:
5fcff47b 907 dst->map = mf.map;
064af421
BP
908}
909
206b60d4
DDP
910ovs_be16
911parse_dl_type(const struct eth_header *data_, size_t size)
912{
913 const void *data = data_;
f0fb825a 914 union flow_vlan_hdr vlans[FLOW_MAX_VLAN_HEADERS];
206b60d4 915
f0fb825a 916 parse_vlan(&data, &size, vlans);
206b60d4
DDP
917
918 return parse_ethertype(&data, &size);
919}
920
993410fb
BP
921/* For every bit of a field that is wildcarded in 'wildcards', sets the
922 * corresponding bit in 'flow' to zero. */
923void
924flow_zero_wildcards(struct flow *flow, const struct flow_wildcards *wildcards)
925{
d70e8c28
JR
926 uint64_t *flow_u64 = (uint64_t *) flow;
927 const uint64_t *wc_u64 = (const uint64_t *) &wildcards->masks;
659c2346 928 size_t i;
993410fb 929
d70e8c28
JR
930 for (i = 0; i < FLOW_U64S; i++) {
931 flow_u64[i] &= wc_u64[i];
26720e24 932 }
993410fb
BP
933}
934
d8d9c698
EJ
935void
936flow_unwildcard_tp_ports(const struct flow *flow, struct flow_wildcards *wc)
937{
938 if (flow->nw_proto != IPPROTO_ICMP) {
939 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
940 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
941 } else {
942 wc->masks.tp_src = htons(0xff);
943 wc->masks.tp_dst = htons(0xff);
944 }
945}
946
50dcbd8e 947/* Initializes 'flow_metadata' with the metadata found in 'flow'. */
5d6c3af0 948void
50dcbd8e 949flow_get_metadata(const struct flow *flow, struct match *flow_metadata)
5d6c3af0 950{
50dcbd8e
JG
951 int i;
952
2482b0b0 953 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 39);
e9358af6 954
50dcbd8e
JG
955 match_init_catchall(flow_metadata);
956 if (flow->tunnel.tun_id != htonll(0)) {
957 match_set_tun_id(flow_metadata, flow->tunnel.tun_id);
958 }
b666962b
JG
959 if (flow->tunnel.flags & FLOW_TNL_PUB_F_MASK) {
960 match_set_tun_flags(flow_metadata,
961 flow->tunnel.flags & FLOW_TNL_PUB_F_MASK);
962 }
ffe4c74f 963 if (flow->tunnel.ip_src) {
50dcbd8e
JG
964 match_set_tun_src(flow_metadata, flow->tunnel.ip_src);
965 }
ffe4c74f 966 if (flow->tunnel.ip_dst) {
50dcbd8e
JG
967 match_set_tun_dst(flow_metadata, flow->tunnel.ip_dst);
968 }
ffe4c74f
JB
969 if (ipv6_addr_is_set(&flow->tunnel.ipv6_src)) {
970 match_set_tun_ipv6_src(flow_metadata, &flow->tunnel.ipv6_src);
971 }
972 if (ipv6_addr_is_set(&flow->tunnel.ipv6_dst)) {
973 match_set_tun_ipv6_dst(flow_metadata, &flow->tunnel.ipv6_dst);
974 }
50dcbd8e
JG
975 if (flow->tunnel.gbp_id != htons(0)) {
976 match_set_tun_gbp_id(flow_metadata, flow->tunnel.gbp_id);
977 }
978 if (flow->tunnel.gbp_flags) {
979 match_set_tun_gbp_flags(flow_metadata, flow->tunnel.gbp_flags);
980 }
6728d578 981 tun_metadata_get_fmd(&flow->tunnel, flow_metadata);
50dcbd8e
JG
982 if (flow->metadata != htonll(0)) {
983 match_set_metadata(flow_metadata, flow->metadata);
984 }
985
986 for (i = 0; i < FLOW_N_REGS; i++) {
987 if (flow->regs[i]) {
988 match_set_reg(flow_metadata, i, flow->regs[i]);
989 }
990 }
991
992 if (flow->pkt_mark != 0) {
993 match_set_pkt_mark(flow_metadata, flow->pkt_mark);
994 }
995
996 match_set_in_port(flow_metadata, flow->in_port.ofp_port);
6a81043e
JS
997 if (flow->packet_type != htonl(PT_ETH)) {
998 match_set_packet_type(flow_metadata, flow->packet_type);
999 }
1000
07659514
JS
1001 if (flow->ct_state != 0) {
1002 match_set_ct_state(flow_metadata, flow->ct_state);
daf4d3c1
JR
1003 if (is_ct_valid(flow, NULL, NULL) && flow->ct_nw_proto != 0) {
1004 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1005 match_set_ct_nw_src(flow_metadata, flow->ct_nw_src);
1006 match_set_ct_nw_dst(flow_metadata, flow->ct_nw_dst);
1007 match_set_ct_nw_proto(flow_metadata, flow->ct_nw_proto);
1008 match_set_ct_tp_src(flow_metadata, flow->ct_tp_src);
1009 match_set_ct_tp_dst(flow_metadata, flow->ct_tp_dst);
1010 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1011 match_set_ct_ipv6_src(flow_metadata, &flow->ct_ipv6_src);
1012 match_set_ct_ipv6_dst(flow_metadata, &flow->ct_ipv6_dst);
1013 match_set_ct_nw_proto(flow_metadata, flow->ct_nw_proto);
1014 match_set_ct_tp_src(flow_metadata, flow->ct_tp_src);
1015 match_set_ct_tp_dst(flow_metadata, flow->ct_tp_dst);
1016 }
1017 }
07659514
JS
1018 }
1019 if (flow->ct_zone != 0) {
1020 match_set_ct_zone(flow_metadata, flow->ct_zone);
1021 }
8e53fe8c
JS
1022 if (flow->ct_mark != 0) {
1023 match_set_ct_mark(flow_metadata, flow->ct_mark);
1024 }
2ff8484b 1025 if (!ovs_u128_is_zero(flow->ct_label)) {
9daf2348
JS
1026 match_set_ct_label(flow_metadata, flow->ct_label);
1027 }
07659514
JS
1028}
1029
b02e6cf8
BP
1030const char *
1031ct_state_to_string(uint32_t state)
07659514
JS
1032{
1033 switch (state) {
fd6cd1bf
BP
1034#define CS_STATE(ENUM, INDEX, NAME) case CS_##ENUM: return NAME;
1035 CS_STATES
1036#undef CS_STATE
07659514
JS
1037 default:
1038 return NULL;
1039 }
5d6c3af0
EJ
1040}
1041
b02e6cf8
BP
1042uint32_t
1043ct_state_from_string(const char *s)
1044{
1045#define CS_STATE(ENUM, INDEX, NAME) \
1046 if (!strcmp(s, NAME)) { \
1047 return CS_##ENUM; \
1048 }
1049 CS_STATES
1050#undef CS_STATE
1051 return 0;
1052}
1053
6846e91e
BP
1054/* Clears the fields in 'flow' associated with connection tracking. */
1055void
1056flow_clear_conntrack(struct flow *flow)
1057{
1058 flow->ct_state = 0;
1059 flow->ct_zone = 0;
1060 flow->ct_mark = 0;
1061 flow->ct_label = OVS_U128_ZERO;
1062
1063 flow->ct_nw_proto = 0;
1064 flow->ct_tp_src = 0;
1065 flow->ct_tp_dst = 0;
1066 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1067 flow->ct_nw_src = 0;
1068 flow->ct_nw_dst = 0;
1069 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1070 memset(&flow->ct_ipv6_src, 0, sizeof flow->ct_ipv6_src);
1071 memset(&flow->ct_ipv6_dst, 0, sizeof flow->ct_ipv6_dst);
1072 }
1073}
1074
064af421 1075char *
50f96b10
BP
1076flow_to_string(const struct flow *flow,
1077 const struct ofputil_port_map *port_map)
064af421
BP
1078{
1079 struct ds ds = DS_EMPTY_INITIALIZER;
50f96b10 1080 flow_format(&ds, flow, port_map);
064af421
BP
1081 return ds_cstr(&ds);
1082}
1083
4fe3445a
PS
1084const char *
1085flow_tun_flag_to_string(uint32_t flags)
1086{
1087 switch (flags) {
1088 case FLOW_TNL_F_DONT_FRAGMENT:
1089 return "df";
1090 case FLOW_TNL_F_CSUM:
1091 return "csum";
1092 case FLOW_TNL_F_KEY:
1093 return "key";
94872594
JG
1094 case FLOW_TNL_F_OAM:
1095 return "oam";
4fe3445a
PS
1096 default:
1097 return NULL;
1098 }
1099}
1100
1101void
1102format_flags(struct ds *ds, const char *(*bit_to_string)(uint32_t),
1103 uint32_t flags, char del)
1104{
1105 uint32_t bad = 0;
1106
1107 if (!flags) {
8e4c1621 1108 ds_put_char(ds, '0');
4fe3445a
PS
1109 return;
1110 }
1111 while (flags) {
1112 uint32_t bit = rightmost_1bit(flags);
1113 const char *s;
1114
1115 s = bit_to_string(bit);
1116 if (s) {
1117 ds_put_format(ds, "%s%c", s, del);
1118 } else {
1119 bad |= bit;
1120 }
1121
1122 flags &= ~bit;
1123 }
1124
1125 if (bad) {
1126 ds_put_format(ds, "0x%"PRIx32"%c", bad, del);
1127 }
1128 ds_chomp(ds, del);
1129}
1130
61bf6666
JR
1131void
1132format_flags_masked(struct ds *ds, const char *name,
1133 const char *(*bit_to_string)(uint32_t), uint32_t flags,
8e4c1621 1134 uint32_t mask, uint32_t max_mask)
61bf6666
JR
1135{
1136 if (name) {
0deec6d2 1137 ds_put_format(ds, "%s%s=%s", colors.param, name, colors.end);
61bf6666 1138 }
8e4c1621
JG
1139
1140 if (mask == max_mask) {
1141 format_flags(ds, bit_to_string, flags, '|');
1142 return;
1143 }
1144
1145 if (!mask) {
1146 ds_put_cstr(ds, "0/0");
1147 return;
1148 }
1149
61bf6666
JR
1150 while (mask) {
1151 uint32_t bit = rightmost_1bit(mask);
1152 const char *s = bit_to_string(bit);
1153
1154 ds_put_format(ds, "%s%s", (flags & bit) ? "+" : "-",
1155 s ? s : "[Unknown]");
1156 mask &= ~bit;
1157 }
1158}
1159
3d4b2e6e
JS
1160static void
1161put_u16_masked(struct ds *s, uint16_t value, uint16_t mask)
1162{
1163 if (!mask) {
1164 ds_put_char(s, '*');
1165 } else {
1166 if (value > 9) {
1167 ds_put_format(s, "0x%"PRIx16, value);
1168 } else {
1169 ds_put_format(s, "%"PRIu16, value);
1170 }
1171
1172 if (mask != UINT16_MAX) {
1173 ds_put_format(s, "/0x%"PRIx16, mask);
1174 }
1175 }
1176}
1177
1178void
1179format_packet_type_masked(struct ds *s, ovs_be32 value, ovs_be32 mask)
1180{
1181 if (value == htonl(PT_ETH) && mask == OVS_BE32_MAX) {
1182 ds_put_cstr(s, "eth");
1183 } else {
1184 ds_put_cstr(s, "packet_type=(");
1185 put_u16_masked(s, pt_ns(value), pt_ns(mask));
1186 ds_put_char(s, ',');
1187 put_u16_masked(s, pt_ns_type(value), pt_ns_type(mask));
1188 ds_put_char(s, ')');
1189 }
1190}
1191
8e4c1621
JG
1192/* Scans a string 's' of flags to determine their numerical value and
1193 * returns the number of characters parsed using 'bit_to_string' to
1194 * lookup flag names. Scanning continues until the character 'end' is
1195 * reached.
1196 *
1197 * In the event of a failure, a negative error code will be returned. In
1198 * addition, if 'res_string' is non-NULL then a descriptive string will
1199 * be returned incorporating the identifying string 'field_name'. This
1200 * error string must be freed by the caller.
1201 *
1202 * Upon success, the flag values will be stored in 'res_flags' and
1203 * optionally 'res_mask', if it is non-NULL (if it is NULL then any masks
1204 * present in the original string will be considered an error). The
1205 * caller may restrict the acceptable set of values through the mask
1206 * 'allowed'. */
1207int
1208parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
1209 char end, const char *field_name, char **res_string,
1210 uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask)
1211{
1212 uint32_t result = 0;
1213 int n;
1214
1215 /* Parse masked flags in numeric format? */
1216 if (res_mask && ovs_scan(s, "%"SCNi32"/%"SCNi32"%n",
1217 res_flags, res_mask, &n) && n > 0) {
1218 if (*res_flags & ~allowed || *res_mask & ~allowed) {
1219 goto unknown;
1220 }
1221 return n;
1222 }
1223
1224 n = 0;
1225
1226 if (res_mask && (*s == '+' || *s == '-')) {
1227 uint32_t flags = 0, mask = 0;
1228
1229 /* Parse masked flags. */
1230 while (s[0] != end) {
1231 bool set;
1232 uint32_t bit;
1233 size_t len;
1234
1235 if (s[0] == '+') {
1236 set = true;
1237 } else if (s[0] == '-') {
1238 set = false;
1239 } else {
1240 if (res_string) {
1241 *res_string = xasprintf("%s: %s must be preceded by '+' "
1242 "(for SET) or '-' (NOT SET)", s,
1243 field_name);
1244 }
1245 return -EINVAL;
1246 }
1247 s++;
1248 n++;
1249
1250 for (bit = 1; bit; bit <<= 1) {
1251 const char *fname = bit_to_string(bit);
1252
1253 if (!fname) {
1254 continue;
1255 }
1256
1257 len = strlen(fname);
1258 if (strncmp(s, fname, len) ||
1259 (s[len] != '+' && s[len] != '-' && s[len] != end)) {
1260 continue;
1261 }
1262
1263 if (mask & bit) {
1264 /* bit already set. */
1265 if (res_string) {
1266 *res_string = xasprintf("%s: Each %s flag can be "
1267 "specified only once", s,
1268 field_name);
1269 }
1270 return -EINVAL;
1271 }
1272 if (!(bit & allowed)) {
1273 goto unknown;
1274 }
1275 if (set) {
1276 flags |= bit;
1277 }
1278 mask |= bit;
1279 break;
1280 }
1281
1282 if (!bit) {
1283 goto unknown;
1284 }
1285 s += len;
1286 n += len;
1287 }
1288
1289 *res_flags = flags;
1290 *res_mask = mask;
1291 return n;
1292 }
1293
1294 /* Parse unmasked flags. If a flag is present, it is set, otherwise
1295 * it is not set. */
1296 while (s[n] != end) {
1297 unsigned long long int flags;
1298 uint32_t bit;
1299 int n0;
1300
1301 if (ovs_scan(&s[n], "%lli%n", &flags, &n0)) {
1302 if (flags & ~allowed) {
1303 goto unknown;
1304 }
1305 n += n0 + (s[n + n0] == '|');
1306 result |= flags;
1307 continue;
1308 }
1309
1310 for (bit = 1; bit; bit <<= 1) {
1311 const char *name = bit_to_string(bit);
1312 size_t len;
1313
1314 if (!name) {
1315 continue;
1316 }
1317
1318 len = strlen(name);
1319 if (!strncmp(s + n, name, len) &&
1320 (s[n + len] == '|' || s[n + len] == end)) {
1321 if (!(bit & allowed)) {
1322 goto unknown;
1323 }
1324 result |= bit;
1325 n += len + (s[n + len] == '|');
1326 break;
1327 }
1328 }
1329
1330 if (!bit) {
1331 goto unknown;
1332 }
1333 }
1334
1335 *res_flags = result;
1336 if (res_mask) {
1337 *res_mask = UINT32_MAX;
1338 }
1339 if (res_string) {
1340 *res_string = NULL;
1341 }
1342 return n;
1343
1344unknown:
1345 if (res_string) {
1346 *res_string = xasprintf("%s: unknown %s flag(s)", s, field_name);
1347 }
1348 return -EINVAL;
1349}
1350
064af421 1351void
50f96b10
BP
1352flow_format(struct ds *ds,
1353 const struct flow *flow, const struct ofputil_port_map *port_map)
064af421 1354{
aa6c9932 1355 struct match match;
78c9486d 1356 struct flow_wildcards *wc = &match.wc;
296e07ac 1357
aa6c9932 1358 match_wc_init(&match, flow);
78c9486d
JR
1359
1360 /* As this function is most often used for formatting a packet in a
1361 * packet-in message, skip formatting the packet context fields that are
e6d9ab56
JR
1362 * all-zeroes to make the print-out easier on the eyes. This means that a
1363 * missing context field implies a zero value for that field. This is
1364 * similar to OpenFlow encoding of these fields, as the specification
1365 * states that all-zeroes context fields should not be encoded in the
1366 * packet-in messages. */
1367 if (!flow->in_port.ofp_port) {
1368 WC_UNMASK_FIELD(wc, in_port);
1369 }
78c9486d
JR
1370 if (!flow->skb_priority) {
1371 WC_UNMASK_FIELD(wc, skb_priority);
1372 }
1373 if (!flow->pkt_mark) {
1374 WC_UNMASK_FIELD(wc, pkt_mark);
1375 }
1376 if (!flow->recirc_id) {
1377 WC_UNMASK_FIELD(wc, recirc_id);
1378 }
330de069
JR
1379 if (!flow->dp_hash) {
1380 WC_UNMASK_FIELD(wc, dp_hash);
1381 }
07659514
JS
1382 if (!flow->ct_state) {
1383 WC_UNMASK_FIELD(wc, ct_state);
1384 }
1385 if (!flow->ct_zone) {
1386 WC_UNMASK_FIELD(wc, ct_zone);
1387 }
8e53fe8c
JS
1388 if (!flow->ct_mark) {
1389 WC_UNMASK_FIELD(wc, ct_mark);
1390 }
2ff8484b 1391 if (ovs_u128_is_zero(flow->ct_label)) {
9daf2348
JS
1392 WC_UNMASK_FIELD(wc, ct_label);
1393 }
daf4d3c1
JR
1394 if (!is_ct_valid(flow, &match.wc, NULL) || !flow->ct_nw_proto) {
1395 WC_UNMASK_FIELD(wc, ct_nw_proto);
1396 WC_UNMASK_FIELD(wc, ct_tp_src);
1397 WC_UNMASK_FIELD(wc, ct_tp_dst);
1398 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1399 WC_UNMASK_FIELD(wc, ct_nw_src);
1400 WC_UNMASK_FIELD(wc, ct_nw_dst);
1401 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1402 WC_UNMASK_FIELD(wc, ct_ipv6_src);
1403 WC_UNMASK_FIELD(wc, ct_ipv6_dst);
1404 }
1405 }
78c9486d
JR
1406 for (int i = 0; i < FLOW_N_REGS; i++) {
1407 if (!flow->regs[i]) {
1408 WC_UNMASK_FIELD(wc, regs[i]);
1409 }
1410 }
1411 if (!flow->metadata) {
1412 WC_UNMASK_FIELD(wc, metadata);
1413 }
1414
50f96b10 1415 match_format(&match, port_map, ds, OFP_DEFAULT_PRIORITY);
064af421
BP
1416}
1417
1418void
50f96b10
BP
1419flow_print(FILE *stream,
1420 const struct flow *flow, const struct ofputil_port_map *port_map)
064af421 1421{
50f96b10 1422 char *s = flow_to_string(flow, port_map);
064af421
BP
1423 fputs(s, stream);
1424 free(s);
1425}
54363004
BP
1426\f
1427/* flow_wildcards functions. */
1428
d8ae4d67 1429/* Initializes 'wc' as a set of wildcards that matches every packet. */
54363004 1430void
d8ae4d67 1431flow_wildcards_init_catchall(struct flow_wildcards *wc)
54363004 1432{
659c2346 1433 memset(&wc->masks, 0, sizeof wc->masks);
54363004
BP
1434}
1435
78c9486d
JR
1436/* Converts a flow into flow wildcards. It sets the wildcard masks based on
1437 * the packet headers extracted to 'flow'. It will not set the mask for fields
1438 * that do not make sense for the packet type. OpenFlow-only metadata is
1439 * wildcarded, but other metadata is unconditionally exact-matched. */
f0fb825a
EG
1440void
1441flow_wildcards_init_for_packet(struct flow_wildcards *wc,
1442 const struct flow *flow)
78c9486d 1443{
cb1145d1
ZB
1444 ovs_be16 dl_type = OVS_BE16_MAX;
1445
78c9486d
JR
1446 memset(&wc->masks, 0x0, sizeof wc->masks);
1447
0de8783a 1448 /* Update this function whenever struct flow changes. */
2482b0b0 1449 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 39);
0de8783a 1450
ffe4c74f 1451 if (flow_tnl_dst_is_set(&flow->tunnel)) {
78c9486d
JR
1452 if (flow->tunnel.flags & FLOW_TNL_F_KEY) {
1453 WC_MASK_FIELD(wc, tunnel.tun_id);
1454 }
1455 WC_MASK_FIELD(wc, tunnel.ip_src);
1456 WC_MASK_FIELD(wc, tunnel.ip_dst);
ffe4c74f
JB
1457 WC_MASK_FIELD(wc, tunnel.ipv6_src);
1458 WC_MASK_FIELD(wc, tunnel.ipv6_dst);
78c9486d
JR
1459 WC_MASK_FIELD(wc, tunnel.flags);
1460 WC_MASK_FIELD(wc, tunnel.ip_tos);
1461 WC_MASK_FIELD(wc, tunnel.ip_ttl);
1462 WC_MASK_FIELD(wc, tunnel.tp_src);
1463 WC_MASK_FIELD(wc, tunnel.tp_dst);
ac6073e3
MC
1464 WC_MASK_FIELD(wc, tunnel.gbp_id);
1465 WC_MASK_FIELD(wc, tunnel.gbp_flags);
9558d2a5 1466
6728d578
JG
1467 if (!(flow->tunnel.flags & FLOW_TNL_F_UDPIF)) {
1468 if (flow->tunnel.metadata.present.map) {
1469 wc->masks.tunnel.metadata.present.map =
1470 flow->tunnel.metadata.present.map;
1471 WC_MASK_FIELD(wc, tunnel.metadata.opts.u8);
8d8ab6c2 1472 WC_MASK_FIELD(wc, tunnel.metadata.tab);
6728d578
JG
1473 }
1474 } else {
1475 WC_MASK_FIELD(wc, tunnel.metadata.present.len);
1476 memset(wc->masks.tunnel.metadata.opts.gnv, 0xff,
1477 flow->tunnel.metadata.present.len);
9558d2a5 1478 }
78c9486d
JR
1479 } else if (flow->tunnel.tun_id) {
1480 WC_MASK_FIELD(wc, tunnel.tun_id);
1481 }
1482
18080541 1483 /* metadata, regs, and conj_id wildcarded. */
78c9486d
JR
1484
1485 WC_MASK_FIELD(wc, skb_priority);
1486 WC_MASK_FIELD(wc, pkt_mark);
07659514
JS
1487 WC_MASK_FIELD(wc, ct_state);
1488 WC_MASK_FIELD(wc, ct_zone);
8e53fe8c 1489 WC_MASK_FIELD(wc, ct_mark);
9daf2348 1490 WC_MASK_FIELD(wc, ct_label);
78c9486d
JR
1491 WC_MASK_FIELD(wc, recirc_id);
1492 WC_MASK_FIELD(wc, dp_hash);
1493 WC_MASK_FIELD(wc, in_port);
1494
c61f3870
BP
1495 /* actset_output wildcarded. */
1496
3d4b2e6e 1497 WC_MASK_FIELD(wc, packet_type);
cb1145d1
ZB
1498 if (flow->packet_type == htonl(PT_ETH)) {
1499 WC_MASK_FIELD(wc, dl_dst);
1500 WC_MASK_FIELD(wc, dl_src);
1501 WC_MASK_FIELD(wc, dl_type);
1502 /* No need to set mask of inner VLANs that don't exist. */
1503 for (int i = 0; i < FLOW_MAX_VLAN_HEADERS; i++) {
1504 /* Always show the first zero VLAN. */
1505 WC_MASK_FIELD(wc, vlans[i]);
1506 if (flow->vlans[i].tci == htons(0)) {
1507 break;
1508 }
f0fb825a 1509 }
cb1145d1
ZB
1510 dl_type = flow->dl_type;
1511 } else {
1512 dl_type = pt_ns_type_be(flow->packet_type);
f0fb825a 1513 }
78c9486d 1514
cb1145d1 1515 if (dl_type == htons(ETH_TYPE_IP)) {
78c9486d
JR
1516 WC_MASK_FIELD(wc, nw_src);
1517 WC_MASK_FIELD(wc, nw_dst);
daf4d3c1
JR
1518 WC_MASK_FIELD(wc, ct_nw_src);
1519 WC_MASK_FIELD(wc, ct_nw_dst);
cb1145d1 1520 } else if (dl_type == htons(ETH_TYPE_IPV6)) {
78c9486d
JR
1521 WC_MASK_FIELD(wc, ipv6_src);
1522 WC_MASK_FIELD(wc, ipv6_dst);
1523 WC_MASK_FIELD(wc, ipv6_label);
daf4d3c1
JR
1524 if (is_nd(flow, wc)) {
1525 WC_MASK_FIELD(wc, arp_sha);
1526 WC_MASK_FIELD(wc, arp_tha);
1527 WC_MASK_FIELD(wc, nd_target);
1528 } else {
1529 WC_MASK_FIELD(wc, ct_ipv6_src);
1530 WC_MASK_FIELD(wc, ct_ipv6_dst);
1531 }
cb1145d1
ZB
1532 } else if (dl_type == htons(ETH_TYPE_ARP) ||
1533 dl_type == htons(ETH_TYPE_RARP)) {
78c9486d
JR
1534 WC_MASK_FIELD(wc, nw_src);
1535 WC_MASK_FIELD(wc, nw_dst);
1536 WC_MASK_FIELD(wc, nw_proto);
1537 WC_MASK_FIELD(wc, arp_sha);
1538 WC_MASK_FIELD(wc, arp_tha);
1539 return;
cb1145d1 1540 } else if (eth_type_mpls(dl_type)) {
78c9486d
JR
1541 for (int i = 0; i < FLOW_MAX_MPLS_LABELS; i++) {
1542 WC_MASK_FIELD(wc, mpls_lse[i]);
1543 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
1544 break;
1545 }
1546 }
1547 return;
1548 } else {
1549 return; /* Unknown ethertype. */
1550 }
1551
1552 /* IPv4 or IPv6. */
1553 WC_MASK_FIELD(wc, nw_frag);
1554 WC_MASK_FIELD(wc, nw_tos);
1555 WC_MASK_FIELD(wc, nw_ttl);
1556 WC_MASK_FIELD(wc, nw_proto);
daf4d3c1
JR
1557 WC_MASK_FIELD(wc, ct_nw_proto);
1558 WC_MASK_FIELD(wc, ct_tp_src);
1559 WC_MASK_FIELD(wc, ct_tp_dst);
78c9486d
JR
1560
1561 /* No transport layer header in later fragments. */
1562 if (!(flow->nw_frag & FLOW_NW_FRAG_LATER) &&
1563 (flow->nw_proto == IPPROTO_ICMP ||
1564 flow->nw_proto == IPPROTO_ICMPV6 ||
1565 flow->nw_proto == IPPROTO_TCP ||
1566 flow->nw_proto == IPPROTO_UDP ||
1567 flow->nw_proto == IPPROTO_SCTP ||
1568 flow->nw_proto == IPPROTO_IGMP)) {
1569 WC_MASK_FIELD(wc, tp_src);
1570 WC_MASK_FIELD(wc, tp_dst);
1571
1572 if (flow->nw_proto == IPPROTO_TCP) {
1573 WC_MASK_FIELD(wc, tcp_flags);
78c9486d
JR
1574 } else if (flow->nw_proto == IPPROTO_IGMP) {
1575 WC_MASK_FIELD(wc, igmp_group_ip4);
1576 }
1577 }
1578}
1579
0de8783a
JR
1580/* Return a map of possible fields for a packet of the same type as 'flow'.
1581 * Including extra bits in the returned mask is not wrong, it is just less
1582 * optimal.
1583 *
1584 * This is a less precise version of flow_wildcards_init_for_packet() above. */
361d808d 1585void
5fcff47b 1586flow_wc_map(const struct flow *flow, struct flowmap *map)
0de8783a
JR
1587{
1588 /* Update this function whenever struct flow changes. */
2482b0b0 1589 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 39);
0de8783a 1590
5fcff47b
JR
1591 flowmap_init(map);
1592
ffe4c74f 1593 if (flow_tnl_dst_is_set(&flow->tunnel)) {
5fcff47b 1594 FLOWMAP_SET__(map, tunnel, offsetof(struct flow_tnl, metadata));
6728d578
JG
1595 if (!(flow->tunnel.flags & FLOW_TNL_F_UDPIF)) {
1596 if (flow->tunnel.metadata.present.map) {
5fcff47b 1597 FLOWMAP_SET(map, tunnel.metadata);
6728d578
JG
1598 }
1599 } else {
5fcff47b
JR
1600 FLOWMAP_SET(map, tunnel.metadata.present.len);
1601 FLOWMAP_SET__(map, tunnel.metadata.opts.gnv,
1602 flow->tunnel.metadata.present.len);
361d808d
JR
1603 }
1604 }
0de8783a
JR
1605
1606 /* Metadata fields that can appear on packet input. */
5fcff47b
JR
1607 FLOWMAP_SET(map, skb_priority);
1608 FLOWMAP_SET(map, pkt_mark);
1609 FLOWMAP_SET(map, recirc_id);
1610 FLOWMAP_SET(map, dp_hash);
1611 FLOWMAP_SET(map, in_port);
1612 FLOWMAP_SET(map, dl_dst);
1613 FLOWMAP_SET(map, dl_src);
1614 FLOWMAP_SET(map, dl_type);
f0fb825a 1615 FLOWMAP_SET(map, vlans);
07659514
JS
1616 FLOWMAP_SET(map, ct_state);
1617 FLOWMAP_SET(map, ct_zone);
8e53fe8c 1618 FLOWMAP_SET(map, ct_mark);
9daf2348 1619 FLOWMAP_SET(map, ct_label);
2482b0b0 1620 FLOWMAP_SET(map, packet_type);
0de8783a
JR
1621
1622 /* Ethertype-dependent fields. */
1623 if (OVS_LIKELY(flow->dl_type == htons(ETH_TYPE_IP))) {
5fcff47b
JR
1624 FLOWMAP_SET(map, nw_src);
1625 FLOWMAP_SET(map, nw_dst);
1626 FLOWMAP_SET(map, nw_proto);
1627 FLOWMAP_SET(map, nw_frag);
1628 FLOWMAP_SET(map, nw_tos);
1629 FLOWMAP_SET(map, nw_ttl);
8ecb3068
DDP
1630 FLOWMAP_SET(map, tp_src);
1631 FLOWMAP_SET(map, tp_dst);
daf4d3c1
JR
1632 FLOWMAP_SET(map, ct_nw_proto);
1633 FLOWMAP_SET(map, ct_nw_src);
1634 FLOWMAP_SET(map, ct_nw_dst);
1635 FLOWMAP_SET(map, ct_tp_src);
1636 FLOWMAP_SET(map, ct_tp_dst);
5fcff47b 1637
0de8783a 1638 if (OVS_UNLIKELY(flow->nw_proto == IPPROTO_IGMP)) {
5fcff47b 1639 FLOWMAP_SET(map, igmp_group_ip4);
0de8783a 1640 } else {
5fcff47b 1641 FLOWMAP_SET(map, tcp_flags);
0de8783a
JR
1642 }
1643 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
5fcff47b
JR
1644 FLOWMAP_SET(map, ipv6_src);
1645 FLOWMAP_SET(map, ipv6_dst);
1646 FLOWMAP_SET(map, ipv6_label);
1647 FLOWMAP_SET(map, nw_proto);
1648 FLOWMAP_SET(map, nw_frag);
1649 FLOWMAP_SET(map, nw_tos);
1650 FLOWMAP_SET(map, nw_ttl);
8ecb3068
DDP
1651 FLOWMAP_SET(map, tp_src);
1652 FLOWMAP_SET(map, tp_dst);
5fcff47b 1653
daf4d3c1 1654 if (OVS_UNLIKELY(is_nd(flow, NULL))) {
5fcff47b
JR
1655 FLOWMAP_SET(map, nd_target);
1656 FLOWMAP_SET(map, arp_sha);
1657 FLOWMAP_SET(map, arp_tha);
0de8783a 1658 } else {
daf4d3c1
JR
1659 FLOWMAP_SET(map, ct_nw_proto);
1660 FLOWMAP_SET(map, ct_ipv6_src);
1661 FLOWMAP_SET(map, ct_ipv6_dst);
1662 FLOWMAP_SET(map, ct_tp_src);
1663 FLOWMAP_SET(map, ct_tp_dst);
5fcff47b 1664 FLOWMAP_SET(map, tcp_flags);
0de8783a
JR
1665 }
1666 } else if (eth_type_mpls(flow->dl_type)) {
5fcff47b 1667 FLOWMAP_SET(map, mpls_lse);
0de8783a
JR
1668 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
1669 flow->dl_type == htons(ETH_TYPE_RARP)) {
5fcff47b
JR
1670 FLOWMAP_SET(map, nw_src);
1671 FLOWMAP_SET(map, nw_dst);
1672 FLOWMAP_SET(map, nw_proto);
1673 FLOWMAP_SET(map, arp_sha);
1674 FLOWMAP_SET(map, arp_tha);
0de8783a 1675 }
0de8783a
JR
1676}
1677
c11c6faa
AZ
1678/* Clear the metadata and register wildcard masks. They are not packet
1679 * header fields. */
1680void
1681flow_wildcards_clear_non_packet_fields(struct flow_wildcards *wc)
1682{
0de8783a 1683 /* Update this function whenever struct flow changes. */
2482b0b0 1684 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 39);
0de8783a 1685
c11c6faa
AZ
1686 memset(&wc->masks.metadata, 0, sizeof wc->masks.metadata);
1687 memset(&wc->masks.regs, 0, sizeof wc->masks.regs);
c61f3870 1688 wc->masks.actset_output = 0;
18080541 1689 wc->masks.conj_id = 0;
c11c6faa
AZ
1690}
1691
ecf1e7ac
BP
1692/* Returns true if 'wc' matches every packet, false if 'wc' fixes any bits or
1693 * fields. */
1694bool
1695flow_wildcards_is_catchall(const struct flow_wildcards *wc)
1696{
d70e8c28 1697 const uint64_t *wc_u64 = (const uint64_t *) &wc->masks;
659c2346 1698 size_t i;
ecf1e7ac 1699
d70e8c28
JR
1700 for (i = 0; i < FLOW_U64S; i++) {
1701 if (wc_u64[i]) {
ecf1e7ac
BP
1702 return false;
1703 }
1704 }
ecf1e7ac
BP
1705 return true;
1706}
1707
368eefac
EJ
1708/* Sets 'dst' as the bitwise AND of wildcards in 'src1' and 'src2'.
1709 * That is, a bit or a field is wildcarded in 'dst' if it is wildcarded
1710 * in 'src1' or 'src2' or both. */
b5d97350 1711void
368eefac
EJ
1712flow_wildcards_and(struct flow_wildcards *dst,
1713 const struct flow_wildcards *src1,
1714 const struct flow_wildcards *src2)
b5d97350 1715{
d70e8c28
JR
1716 uint64_t *dst_u64 = (uint64_t *) &dst->masks;
1717 const uint64_t *src1_u64 = (const uint64_t *) &src1->masks;
1718 const uint64_t *src2_u64 = (const uint64_t *) &src2->masks;
659c2346 1719 size_t i;
a79c50f3 1720
d70e8c28
JR
1721 for (i = 0; i < FLOW_U64S; i++) {
1722 dst_u64[i] = src1_u64[i] & src2_u64[i];
26720e24 1723 }
b5d97350
BP
1724}
1725
368eefac
EJ
1726/* Sets 'dst' as the bitwise OR of wildcards in 'src1' and 'src2'. That
1727 * is, a bit or a field is wildcarded in 'dst' if it is neither
1728 * wildcarded in 'src1' nor 'src2'. */
1729void
1730flow_wildcards_or(struct flow_wildcards *dst,
1731 const struct flow_wildcards *src1,
1732 const struct flow_wildcards *src2)
1733{
d70e8c28
JR
1734 uint64_t *dst_u64 = (uint64_t *) &dst->masks;
1735 const uint64_t *src1_u64 = (const uint64_t *) &src1->masks;
1736 const uint64_t *src2_u64 = (const uint64_t *) &src2->masks;
368eefac
EJ
1737 size_t i;
1738
d70e8c28
JR
1739 for (i = 0; i < FLOW_U64S; i++) {
1740 dst_u64[i] = src1_u64[i] | src2_u64[i];
368eefac
EJ
1741 }
1742}
1743
b5d97350
BP
1744/* Returns a hash of the wildcards in 'wc'. */
1745uint32_t
1006cda6 1746flow_wildcards_hash(const struct flow_wildcards *wc, uint32_t basis)
b5d97350 1747{
ac31c5af 1748 return flow_hash(&wc->masks, basis);
b5d97350
BP
1749}
1750
1751/* Returns true if 'a' and 'b' represent the same wildcards, false if they are
1752 * different. */
1753bool
1754flow_wildcards_equal(const struct flow_wildcards *a,
1755 const struct flow_wildcards *b)
1756{
659c2346 1757 return flow_equal(&a->masks, &b->masks);
b5d97350
BP
1758}
1759
1760/* Returns true if at least one bit or field is wildcarded in 'a' but not in
1761 * 'b', false otherwise. */
1762bool
1763flow_wildcards_has_extra(const struct flow_wildcards *a,
1764 const struct flow_wildcards *b)
1765{
d70e8c28
JR
1766 const uint64_t *a_u64 = (const uint64_t *) &a->masks;
1767 const uint64_t *b_u64 = (const uint64_t *) &b->masks;
659c2346 1768 size_t i;
a79c50f3 1769
d70e8c28
JR
1770 for (i = 0; i < FLOW_U64S; i++) {
1771 if ((a_u64[i] & b_u64[i]) != b_u64[i]) {
b6c9e612
BP
1772 return true;
1773 }
1774 }
659c2346
BP
1775 return false;
1776}
b6c9e612 1777
659c2346
BP
1778/* Returns true if 'a' and 'b' are equal, except that 0-bits (wildcarded bits)
1779 * in 'wc' do not need to be equal in 'a' and 'b'. */
1780bool
1781flow_equal_except(const struct flow *a, const struct flow *b,
1782 const struct flow_wildcards *wc)
1783{
d70e8c28
JR
1784 const uint64_t *a_u64 = (const uint64_t *) a;
1785 const uint64_t *b_u64 = (const uint64_t *) b;
1786 const uint64_t *wc_u64 = (const uint64_t *) &wc->masks;
659c2346 1787 size_t i;
d31f1109 1788
d70e8c28
JR
1789 for (i = 0; i < FLOW_U64S; i++) {
1790 if ((a_u64[i] ^ b_u64[i]) & wc_u64[i]) {
659c2346
BP
1791 return false;
1792 }
47284b1f 1793 }
659c2346 1794 return true;
b5d97350
BP
1795}
1796
b6c9e612
BP
1797/* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
1798 * (A 0-bit indicates a wildcard bit.) */
1799void
1800flow_wildcards_set_reg_mask(struct flow_wildcards *wc, int idx, uint32_t mask)
1801{
26720e24 1802 wc->masks.regs[idx] = mask;
b6c9e612 1803}
ff55ea1f 1804
79fe0f46
BP
1805/* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
1806 * (A 0-bit indicates a wildcard bit.) */
1807void
1808flow_wildcards_set_xreg_mask(struct flow_wildcards *wc, int idx, uint64_t mask)
1809{
1810 flow_set_xreg(&wc->masks, idx, mask);
1811}
1812
b23ada8e
JP
1813/* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
1814 * (A 0-bit indicates a wildcard bit.) */
1815void
1816flow_wildcards_set_xxreg_mask(struct flow_wildcards *wc, int idx,
1817 ovs_u128 mask)
1818{
1819 flow_set_xxreg(&wc->masks, idx, mask);
1820}
1821
28a560d9
JR
1822/* Calculates the 5-tuple hash from the given miniflow.
1823 * This returns the same value as flow_hash_5tuple for the corresponding
1824 * flow. */
4f150744
JR
1825uint32_t
1826miniflow_hash_5tuple(const struct miniflow *flow, uint32_t basis)
1827{
2482b0b0 1828 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 39);
28a560d9 1829 uint32_t hash = basis;
4f150744 1830
28a560d9
JR
1831 if (flow) {
1832 ovs_be16 dl_type = MINIFLOW_GET_BE16(flow, dl_type);
362ad4ba 1833 uint8_t nw_proto;
28a560d9 1834
28a560d9 1835 if (dl_type == htons(ETH_TYPE_IPV6)) {
5fcff47b 1836 struct flowmap map = FLOWMAP_EMPTY_INITIALIZER;
d70e8c28 1837 uint64_t value;
4f150744 1838
5fcff47b
JR
1839 FLOWMAP_SET(&map, ipv6_src);
1840 FLOWMAP_SET(&map, ipv6_dst);
1841
1842 MINIFLOW_FOR_EACH_IN_FLOWMAP(value, flow, map) {
d70e8c28 1843 hash = hash_add64(hash, value);
28a560d9 1844 }
362ad4ba
DDP
1845 } else if (dl_type == htons(ETH_TYPE_IP)
1846 || dl_type == htons(ETH_TYPE_ARP)) {
d70e8c28
JR
1847 hash = hash_add(hash, MINIFLOW_GET_U32(flow, nw_src));
1848 hash = hash_add(hash, MINIFLOW_GET_U32(flow, nw_dst));
362ad4ba
DDP
1849 } else {
1850 goto out;
28a560d9 1851 }
362ad4ba
DDP
1852
1853 nw_proto = MINIFLOW_GET_U8(flow, nw_proto);
1854 hash = hash_add(hash, nw_proto);
1855 if (nw_proto != IPPROTO_TCP && nw_proto != IPPROTO_UDP
1856 && nw_proto != IPPROTO_SCTP && nw_proto != IPPROTO_ICMP
1857 && nw_proto != IPPROTO_ICMPV6) {
1858 goto out;
1859 }
1860
d70e8c28
JR
1861 /* Add both ports at once. */
1862 hash = hash_add(hash, MINIFLOW_GET_U32(flow, tp_src));
28a560d9 1863 }
362ad4ba
DDP
1864out:
1865 return hash_finish(hash, 42);
4f150744
JR
1866}
1867
268eca11
BP
1868ASSERT_SEQUENTIAL_SAME_WORD(tp_src, tp_dst);
1869ASSERT_SEQUENTIAL(ipv6_src, ipv6_dst);
4f150744 1870
63be20be
AW
1871/* Calculates the 5-tuple hash from the given flow. */
1872uint32_t
1873flow_hash_5tuple(const struct flow *flow, uint32_t basis)
1874{
2482b0b0 1875 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 39);
28a560d9 1876 uint32_t hash = basis;
63be20be 1877
28a560d9 1878 if (flow) {
28a560d9
JR
1879
1880 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
d70e8c28
JR
1881 const uint64_t *flow_u64 = (const uint64_t *)flow;
1882 int ofs = offsetof(struct flow, ipv6_src) / 8;
1883 int end = ofs + 2 * sizeof flow->ipv6_src / 8;
63be20be 1884
d70e8c28
JR
1885 for (;ofs < end; ofs++) {
1886 hash = hash_add64(hash, flow_u64[ofs]);
28a560d9 1887 }
362ad4ba
DDP
1888 } else if (flow->dl_type == htons(ETH_TYPE_IP)
1889 || flow->dl_type == htons(ETH_TYPE_ARP)) {
33c6a1b9
JR
1890 hash = hash_add(hash, (OVS_FORCE uint32_t) flow->nw_src);
1891 hash = hash_add(hash, (OVS_FORCE uint32_t) flow->nw_dst);
362ad4ba
DDP
1892 } else {
1893 goto out;
28a560d9 1894 }
362ad4ba
DDP
1895
1896 hash = hash_add(hash, flow->nw_proto);
1897 if (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
1898 && flow->nw_proto != IPPROTO_SCTP && flow->nw_proto != IPPROTO_ICMP
1899 && flow->nw_proto != IPPROTO_ICMPV6) {
1900 goto out;
1901 }
1902
d70e8c28
JR
1903 /* Add both ports at once. */
1904 hash = hash_add(hash,
1905 ((const uint32_t *)flow)[offsetof(struct flow, tp_src)
1906 / sizeof(uint32_t)]);
28a560d9 1907 }
362ad4ba
DDP
1908out:
1909 return hash_finish(hash, 42); /* Arbitrary number. */
63be20be
AW
1910}
1911
ff55ea1f
EJ
1912/* Hashes 'flow' based on its L2 through L4 protocol information. */
1913uint32_t
1914flow_hash_symmetric_l4(const struct flow *flow, uint32_t basis)
1915{
1916 struct {
d31f1109
JP
1917 union {
1918 ovs_be32 ipv4_addr;
1919 struct in6_addr ipv6_addr;
1920 };
ff55ea1f
EJ
1921 ovs_be16 eth_type;
1922 ovs_be16 vlan_tci;
5b909cbb 1923 ovs_be16 tp_port;
74ff3298 1924 struct eth_addr eth_addr;
ff55ea1f
EJ
1925 uint8_t ip_proto;
1926 } fields;
1927
1928 int i;
1929
1930 memset(&fields, 0, sizeof fields);
74ff3298
JR
1931 for (i = 0; i < ARRAY_SIZE(fields.eth_addr.be16); i++) {
1932 fields.eth_addr.be16[i] = flow->dl_src.be16[i] ^ flow->dl_dst.be16[i];
ff55ea1f 1933 }
f0fb825a
EG
1934 for (i = 0; i < FLOW_MAX_VLAN_HEADERS; i++) {
1935 fields.vlan_tci ^= flow->vlans[i].tci & htons(VLAN_VID_MASK);
1936 }
ff55ea1f 1937 fields.eth_type = flow->dl_type;
3e3eda95
EJ
1938
1939 /* UDP source and destination port are not taken into account because they
1940 * will not necessarily be symmetric in a bidirectional flow. */
ff55ea1f 1941 if (fields.eth_type == htons(ETH_TYPE_IP)) {
d31f1109
JP
1942 fields.ipv4_addr = flow->nw_src ^ flow->nw_dst;
1943 fields.ip_proto = flow->nw_proto;
c6bcb685 1944 if (fields.ip_proto == IPPROTO_TCP || fields.ip_proto == IPPROTO_SCTP) {
5b909cbb 1945 fields.tp_port = flow->tp_src ^ flow->tp_dst;
d31f1109
JP
1946 }
1947 } else if (fields.eth_type == htons(ETH_TYPE_IPV6)) {
1948 const uint8_t *a = &flow->ipv6_src.s6_addr[0];
1949 const uint8_t *b = &flow->ipv6_dst.s6_addr[0];
1950 uint8_t *ipv6_addr = &fields.ipv6_addr.s6_addr[0];
1951
1952 for (i=0; i<16; i++) {
1953 ipv6_addr[i] = a[i] ^ b[i];
1954 }
ff55ea1f 1955 fields.ip_proto = flow->nw_proto;
c6bcb685 1956 if (fields.ip_proto == IPPROTO_TCP || fields.ip_proto == IPPROTO_SCTP) {
5b909cbb 1957 fields.tp_port = flow->tp_src ^ flow->tp_dst;
ff55ea1f 1958 }
ff55ea1f 1959 }
c49d1dd1 1960 return jhash_bytes(&fields, sizeof fields, basis);
ff55ea1f 1961}
520e9a2a 1962
4249b547
JB
1963/* Hashes 'flow' based on its L3 through L4 protocol information */
1964uint32_t
1965flow_hash_symmetric_l3l4(const struct flow *flow, uint32_t basis,
1966 bool inc_udp_ports)
1967{
1968 uint32_t hash = basis;
1969
1970 /* UDP source and destination port are also taken into account. */
1971 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1972 hash = hash_add(hash,
1973 (OVS_FORCE uint32_t) (flow->nw_src ^ flow->nw_dst));
1974 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1975 /* IPv6 addresses are 64-bit aligned inside struct flow. */
1976 const uint64_t *a = ALIGNED_CAST(uint64_t *, flow->ipv6_src.s6_addr);
1977 const uint64_t *b = ALIGNED_CAST(uint64_t *, flow->ipv6_dst.s6_addr);
1978
caaabd19 1979 for (int i = 0; i < sizeof flow->ipv6_src / sizeof *a; i++) {
4249b547
JB
1980 hash = hash_add64(hash, a[i] ^ b[i]);
1981 }
1982 } else {
1983 /* Cannot hash non-IP flows */
1984 return 0;
1985 }
1986
1987 hash = hash_add(hash, flow->nw_proto);
1988 if (flow->nw_proto == IPPROTO_TCP || flow->nw_proto == IPPROTO_SCTP ||
1989 (inc_udp_ports && flow->nw_proto == IPPROTO_UDP)) {
1990 hash = hash_add(hash,
1991 (OVS_FORCE uint16_t) (flow->tp_src ^ flow->tp_dst));
1992 }
1993
1994 return hash_finish(hash, basis);
1995}
1996
94639963
JR
1997/* Initialize a flow with random fields that matter for nx_hash_fields. */
1998void
1999flow_random_hash_fields(struct flow *flow)
2000{
2001 uint16_t rnd = random_uint16();
f0fb825a 2002 int i;
94639963
JR
2003
2004 /* Initialize to all zeros. */
2005 memset(flow, 0, sizeof *flow);
2006
74ff3298
JR
2007 eth_addr_random(&flow->dl_src);
2008 eth_addr_random(&flow->dl_dst);
94639963 2009
f0fb825a
EG
2010 for (i = 0; i < FLOW_MAX_VLAN_HEADERS; i++) {
2011 uint16_t vlan = random_uint16() & VLAN_VID_MASK;
2012 flow->vlans[i].tpid = htons(ETH_TYPE_VLAN_8021Q);
2013 flow->vlans[i].tci = htons(vlan | VLAN_CFI);
2014 }
94639963
JR
2015
2016 /* Make most of the random flows IPv4, some IPv6, and rest random. */
2017 flow->dl_type = rnd < 0x8000 ? htons(ETH_TYPE_IP) :
2018 rnd < 0xc000 ? htons(ETH_TYPE_IPV6) : (OVS_FORCE ovs_be16)rnd;
2019
2020 if (dl_type_is_ip_any(flow->dl_type)) {
2021 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2022 flow->nw_src = (OVS_FORCE ovs_be32)random_uint32();
2023 flow->nw_dst = (OVS_FORCE ovs_be32)random_uint32();
2024 } else {
2025 random_bytes(&flow->ipv6_src, sizeof flow->ipv6_src);
2026 random_bytes(&flow->ipv6_dst, sizeof flow->ipv6_dst);
2027 }
2028 /* Make most of IP flows TCP, some UDP or SCTP, and rest random. */
2029 rnd = random_uint16();
2030 flow->nw_proto = rnd < 0x8000 ? IPPROTO_TCP :
2031 rnd < 0xc000 ? IPPROTO_UDP :
2032 rnd < 0xd000 ? IPPROTO_SCTP : (uint8_t)rnd;
2033 if (flow->nw_proto == IPPROTO_TCP ||
2034 flow->nw_proto == IPPROTO_UDP ||
2035 flow->nw_proto == IPPROTO_SCTP) {
2036 flow->tp_src = (OVS_FORCE ovs_be16)random_uint16();
2037 flow->tp_dst = (OVS_FORCE ovs_be16)random_uint16();
2038 }
2039 }
2040}
2041
bcd2633a
JP
2042/* Masks the fields in 'wc' that are used by the flow hash 'fields'. */
2043void
6cdd5145
JP
2044flow_mask_hash_fields(const struct flow *flow, struct flow_wildcards *wc,
2045 enum nx_hash_fields fields)
bcd2633a 2046{
f0fb825a 2047 int i;
bcd2633a
JP
2048 switch (fields) {
2049 case NX_HASH_FIELDS_ETH_SRC:
2050 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2051 break;
2052
2053 case NX_HASH_FIELDS_SYMMETRIC_L4:
2054 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2055 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
6cdd5145
JP
2056 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2057 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2058 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
7f8a65ca 2059 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
6cdd5145
JP
2060 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
2061 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
2062 }
2063 if (is_ip_any(flow)) {
2064 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
d8d9c698 2065 flow_unwildcard_tp_ports(flow, wc);
6cdd5145 2066 }
f0fb825a
EG
2067 for (i = 0; i < FLOW_MAX_VLAN_HEADERS; i++) {
2068 wc->masks.vlans[i].tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2069 }
bcd2633a
JP
2070 break;
2071
4249b547
JB
2072 case NX_HASH_FIELDS_SYMMETRIC_L3L4_UDP:
2073 if (is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP) {
2074 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
2075 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
2076 }
2077 /* no break */
2078 case NX_HASH_FIELDS_SYMMETRIC_L3L4:
2079 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2080 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2081 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2082 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2083 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
2084 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
2085 } else {
2086 break; /* non-IP flow */
2087 }
2088
2089 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
2090 if (flow->nw_proto == IPPROTO_TCP || flow->nw_proto == IPPROTO_SCTP) {
2091 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
2092 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
2093 }
2094 break;
2095
417cfdb6 2096 case NX_HASH_FIELDS_NW_SRC:
2097 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2098 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2099 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2100 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
2101 }
2102 break;
2103
2104 case NX_HASH_FIELDS_NW_DST:
2105 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2106 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2107 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2108 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
2109 }
2110 break;
2111
bcd2633a 2112 default:
428b2edd 2113 OVS_NOT_REACHED();
bcd2633a
JP
2114 }
2115}
2116
520e9a2a
EJ
2117/* Hashes the portions of 'flow' designated by 'fields'. */
2118uint32_t
2119flow_hash_fields(const struct flow *flow, enum nx_hash_fields fields,
2120 uint16_t basis)
2121{
2122 switch (fields) {
2123
2124 case NX_HASH_FIELDS_ETH_SRC:
74ff3298 2125 return jhash_bytes(&flow->dl_src, sizeof flow->dl_src, basis);
520e9a2a
EJ
2126
2127 case NX_HASH_FIELDS_SYMMETRIC_L4:
2128 return flow_hash_symmetric_l4(flow, basis);
4249b547
JB
2129
2130 case NX_HASH_FIELDS_SYMMETRIC_L3L4:
2131 return flow_hash_symmetric_l3l4(flow, basis, false);
2132
2133 case NX_HASH_FIELDS_SYMMETRIC_L3L4_UDP:
2134 return flow_hash_symmetric_l3l4(flow, basis, true);
2135
417cfdb6 2136 case NX_HASH_FIELDS_NW_SRC:
2137 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2138 return jhash_bytes(&flow->nw_src, sizeof flow->nw_src, basis);
2139 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2140 return jhash_bytes(&flow->ipv6_src, sizeof flow->ipv6_src, basis);
2141 } else {
2142 return basis;
2143 }
2144
2145 case NX_HASH_FIELDS_NW_DST:
2146 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2147 return jhash_bytes(&flow->nw_dst, sizeof flow->nw_dst, basis);
2148 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2149 return jhash_bytes(&flow->ipv6_dst, sizeof flow->ipv6_dst, basis);
2150 } else {
2151 return basis;
2152 }
2153
520e9a2a
EJ
2154 }
2155
428b2edd 2156 OVS_NOT_REACHED();
520e9a2a
EJ
2157}
2158
2159/* Returns a string representation of 'fields'. */
2160const char *
2161flow_hash_fields_to_str(enum nx_hash_fields fields)
2162{
2163 switch (fields) {
2164 case NX_HASH_FIELDS_ETH_SRC: return "eth_src";
2165 case NX_HASH_FIELDS_SYMMETRIC_L4: return "symmetric_l4";
4249b547
JB
2166 case NX_HASH_FIELDS_SYMMETRIC_L3L4: return "symmetric_l3l4";
2167 case NX_HASH_FIELDS_SYMMETRIC_L3L4_UDP: return "symmetric_l3l4+udp";
417cfdb6 2168 case NX_HASH_FIELDS_NW_SRC: return "nw_src";
2169 case NX_HASH_FIELDS_NW_DST: return "nw_dst";
520e9a2a
EJ
2170 default: return "<unknown>";
2171 }
2172}
2173
2174/* Returns true if the value of 'fields' is supported. Otherwise false. */
2175bool
2176flow_hash_fields_valid(enum nx_hash_fields fields)
2177{
2178 return fields == NX_HASH_FIELDS_ETH_SRC
4249b547
JB
2179 || fields == NX_HASH_FIELDS_SYMMETRIC_L4
2180 || fields == NX_HASH_FIELDS_SYMMETRIC_L3L4
417cfdb6 2181 || fields == NX_HASH_FIELDS_SYMMETRIC_L3L4_UDP
2182 || fields == NX_HASH_FIELDS_NW_SRC
2183 || fields == NX_HASH_FIELDS_NW_DST;
520e9a2a 2184}
8b3b8dd1 2185
368eefac
EJ
2186/* Returns a hash value for the bits of 'flow' that are active based on
2187 * 'wc', given 'basis'. */
2188uint32_t
2189flow_hash_in_wildcards(const struct flow *flow,
2190 const struct flow_wildcards *wc, uint32_t basis)
2191{
d70e8c28
JR
2192 const uint64_t *wc_u64 = (const uint64_t *) &wc->masks;
2193 const uint64_t *flow_u64 = (const uint64_t *) flow;
368eefac
EJ
2194 uint32_t hash;
2195 size_t i;
2196
2197 hash = basis;
d70e8c28
JR
2198 for (i = 0; i < FLOW_U64S; i++) {
2199 hash = hash_add64(hash, flow_u64[i] & wc_u64[i]);
368eefac 2200 }
d70e8c28 2201 return hash_finish(hash, 8 * FLOW_U64S);
368eefac
EJ
2202}
2203
3719455c
BP
2204/* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
2205 * OpenFlow 1.0 "dl_vlan" value:
2206 *
f0fb825a 2207 * - If it is in the range 0...4095, 'flow->vlans[0].tci' is set to match
3719455c
BP
2208 * that VLAN. Any existing PCP match is unchanged (it becomes 0 if
2209 * 'flow' previously matched packets without a VLAN header).
2210 *
2211 * - If it is OFP_VLAN_NONE, 'flow->vlan_tci' is set to match a packet
2212 * without a VLAN tag.
2213 *
2214 * - Other values of 'vid' should not be used. */
2215void
fb0451d9 2216flow_set_dl_vlan(struct flow *flow, ovs_be16 vid)
3719455c 2217{
0c436519 2218 if (vid == htons(OFP10_VLAN_NONE)) {
f0fb825a 2219 flow->vlans[0].tci = htons(0);
3719455c
BP
2220 } else {
2221 vid &= htons(VLAN_VID_MASK);
f0fb825a
EG
2222 flow->vlans[0].tci &= ~htons(VLAN_VID_MASK);
2223 flow->vlans[0].tci |= htons(VLAN_CFI) | vid;
2224 }
2225}
2226
2227/* Sets the VLAN header TPID, which must be either ETH_TYPE_VLAN_8021Q or
2228 * ETH_TYPE_VLAN_8021AD. */
2229void
2230flow_fix_vlan_tpid(struct flow *flow)
2231{
2232 if (flow->vlans[0].tpid == htons(0) && flow->vlans[0].tci != 0) {
2233 flow->vlans[0].tpid = htons(ETH_TYPE_VLAN_8021Q);
3719455c
BP
2234 }
2235}
2236
cc34bc8c
BP
2237/* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
2238 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
2239 * plus CFI). */
2240void
2241flow_set_vlan_vid(struct flow *flow, ovs_be16 vid)
2242{
2243 ovs_be16 mask = htons(VLAN_VID_MASK | VLAN_CFI);
f0fb825a
EG
2244 flow->vlans[0].tci &= ~mask;
2245 flow->vlans[0].tci |= vid & mask;
cc34bc8c
BP
2246}
2247
3719455c
BP
2248/* Sets the VLAN PCP that 'flow' matches to 'pcp', which should be in the
2249 * range 0...7.
2250 *
2251 * This function has no effect on the VLAN ID that 'flow' matches.
2252 *
2253 * After calling this function, 'flow' will not match packets without a VLAN
2254 * header. */
2255void
2256flow_set_vlan_pcp(struct flow *flow, uint8_t pcp)
2257{
2258 pcp &= 0x07;
f0fb825a
EG
2259 flow->vlans[0].tci &= ~htons(VLAN_PCP_MASK);
2260 flow->vlans[0].tci |= htons((pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
2261}
2262
2263/* Counts the number of VLAN headers. */
2264int
2265flow_count_vlan_headers(const struct flow *flow)
2266{
2267 int i;
2268
2269 for (i = 0; i < FLOW_MAX_VLAN_HEADERS; i++) {
2270 if (!(flow->vlans[i].tci & htons(VLAN_CFI))) {
2271 break;
2272 }
2273 }
2274 return i;
2275}
2276
2277/* Given '*p_an' and '*p_bn' pointing to one past the last VLAN header of
2278 * 'a' and 'b' respectively, skip common VLANs so that they point to the
2279 * first different VLAN counting from bottom. */
2280void
2281flow_skip_common_vlan_headers(const struct flow *a, int *p_an,
2282 const struct flow *b, int *p_bn)
2283{
2284 int an = *p_an, bn = *p_bn;
2285
2286 for (an--, bn--; an >= 0 && bn >= 0; an--, bn--) {
2287 if (a->vlans[an].qtag != b->vlans[bn].qtag) {
2288 break;
2289 }
2290 }
2291 *p_an = an;
2292 *p_bn = bn;
2293}
2294
2295void
2296flow_pop_vlan(struct flow *flow, struct flow_wildcards *wc)
2297{
2298 int n = flow_count_vlan_headers(flow);
68c744fd
BP
2299 if (n > 1) {
2300 if (wc) {
2301 memset(&wc->masks.vlans[1], 0xff,
2302 sizeof(union flow_vlan_hdr) * (n - 1));
2303 }
2304 memmove(&flow->vlans[0], &flow->vlans[1],
2305 sizeof(union flow_vlan_hdr) * (n - 1));
f0fb825a 2306 }
68c744fd
BP
2307 if (n > 0) {
2308 memset(&flow->vlans[n - 1], 0, sizeof(union flow_vlan_hdr));
f0fb825a 2309 }
f0fb825a
EG
2310}
2311
2312void
2313flow_push_vlan_uninit(struct flow *flow, struct flow_wildcards *wc)
2314{
2315 if (wc) {
2316 int n = flow_count_vlan_headers(flow);
6b6b508b
DB
2317 if (n) {
2318 memset(wc->masks.vlans, 0xff, sizeof(union flow_vlan_hdr) * n);
2319 }
f0fb825a
EG
2320 }
2321 memmove(&flow->vlans[1], &flow->vlans[0],
2322 sizeof(union flow_vlan_hdr) * (FLOW_MAX_VLAN_HEADERS - 1));
2323 memset(&flow->vlans[0], 0, sizeof(union flow_vlan_hdr));
3719455c
BP
2324}
2325
8bfd0fda
BP
2326/* Returns the number of MPLS LSEs present in 'flow'
2327 *
2328 * Returns 0 if the 'dl_type' of 'flow' is not an MPLS ethernet type.
2329 * Otherwise traverses 'flow''s MPLS label stack stopping at the
2330 * first entry that has the BoS bit set. If no such entry exists then
2331 * the maximum number of LSEs that can be stored in 'flow' is returned.
2332 */
2333int
2334flow_count_mpls_labels(const struct flow *flow, struct flow_wildcards *wc)
2335{
22d38fca 2336 /* dl_type is always masked. */
8bfd0fda
BP
2337 if (eth_type_mpls(flow->dl_type)) {
2338 int i;
5af43325 2339 int cnt;
8bfd0fda 2340
5af43325
PS
2341 cnt = 0;
2342 for (i = 0; i < FLOW_MAX_MPLS_LABELS; i++) {
8bfd0fda
BP
2343 if (wc) {
2344 wc->masks.mpls_lse[i] |= htonl(MPLS_BOS_MASK);
2345 }
2346 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
2347 return i + 1;
2348 }
5af43325
PS
2349 if (flow->mpls_lse[i]) {
2350 cnt++;
2351 }
8bfd0fda 2352 }
5af43325 2353 return cnt;
8bfd0fda
BP
2354 } else {
2355 return 0;
2356 }
2357}
2358
2359/* Returns the number consecutive of MPLS LSEs, starting at the
2360 * innermost LSE, that are common in 'a' and 'b'.
2361 *
2362 * 'an' must be flow_count_mpls_labels(a).
2363 * 'bn' must be flow_count_mpls_labels(b).
2364 */
2365int
2366flow_count_common_mpls_labels(const struct flow *a, int an,
2367 const struct flow *b, int bn,
2368 struct flow_wildcards *wc)
2369{
2370 int min_n = MIN(an, bn);
2371 if (min_n == 0) {
2372 return 0;
2373 } else {
2374 int common_n = 0;
2375 int a_last = an - 1;
2376 int b_last = bn - 1;
2377 int i;
2378
2379 for (i = 0; i < min_n; i++) {
2380 if (wc) {
2381 wc->masks.mpls_lse[a_last - i] = OVS_BE32_MAX;
2382 wc->masks.mpls_lse[b_last - i] = OVS_BE32_MAX;
2383 }
2384 if (a->mpls_lse[a_last - i] != b->mpls_lse[b_last - i]) {
2385 break;
2386 } else {
2387 common_n++;
2388 }
2389 }
2390
2391 return common_n;
2392 }
2393}
2394
2395/* Adds a new outermost MPLS label to 'flow' and changes 'flow''s Ethernet type
2396 * to 'mpls_eth_type', which must be an MPLS Ethertype.
2397 *
2398 * If the new label is the first MPLS label in 'flow', it is generated as;
2399 *
2400 * - label: 2, if 'flow' is IPv6, otherwise 0.
2401 *
2402 * - TTL: IPv4 or IPv6 TTL, if present and nonzero, otherwise 64.
2403 *
2404 * - TC: IPv4 or IPv6 TOS, if present, otherwise 0.
2405 *
2406 * - BoS: 1.
2407 *
22d38fca 2408 * If the new label is the second or later label MPLS label in 'flow', it is
8bfd0fda
BP
2409 * generated as;
2410 *
368fb7e6 2411 * - label: Copied from outer label.
8bfd0fda
BP
2412 *
2413 * - TTL: Copied from outer label.
2414 *
2415 * - TC: Copied from outer label.
2416 *
2417 * - BoS: 0.
2418 *
2419 * 'n' must be flow_count_mpls_labels(flow). 'n' must be less than
2420 * FLOW_MAX_MPLS_LABELS (because otherwise flow->mpls_lse[] would overflow).
2421 */
2422void
2423flow_push_mpls(struct flow *flow, int n, ovs_be16 mpls_eth_type,
742c0ac3 2424 struct flow_wildcards *wc, bool clear_flow_L3)
8bfd0fda
BP
2425{
2426 ovs_assert(eth_type_mpls(mpls_eth_type));
2427 ovs_assert(n < FLOW_MAX_MPLS_LABELS);
2428
8bfd0fda
BP
2429 if (n) {
2430 int i;
2431
22d38fca
JR
2432 if (wc) {
2433 memset(&wc->masks.mpls_lse, 0xff, sizeof *wc->masks.mpls_lse * n);
2434 }
8bfd0fda
BP
2435 for (i = n; i >= 1; i--) {
2436 flow->mpls_lse[i] = flow->mpls_lse[i - 1];
2437 }
22d38fca 2438 flow->mpls_lse[0] = (flow->mpls_lse[1] & htonl(~MPLS_BOS_MASK));
8bfd0fda
BP
2439 } else {
2440 int label = 0; /* IPv4 Explicit Null. */
2441 int tc = 0;
2442 int ttl = 64;
2443
2444 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2445 label = 2;
2446 }
2447
2448 if (is_ip_any(flow)) {
2449 tc = (flow->nw_tos & IP_DSCP_MASK) >> 2;
22d38fca
JR
2450 if (wc) {
2451 wc->masks.nw_tos |= IP_DSCP_MASK;
2452 wc->masks.nw_ttl = 0xff;
2453 }
8bfd0fda
BP
2454
2455 if (flow->nw_ttl) {
2456 ttl = flow->nw_ttl;
2457 }
8bfd0fda
BP
2458 }
2459
2460 flow->mpls_lse[0] = set_mpls_lse_values(ttl, tc, 1, htonl(label));
2461
742c0ac3
JR
2462 if (clear_flow_L3) {
2463 /* Clear all L3 and L4 fields and dp_hash. */
2482b0b0 2464 BUILD_ASSERT(FLOW_WC_SEQ == 39);
742c0ac3
JR
2465 memset((char *) flow + FLOW_SEGMENT_2_ENDS_AT, 0,
2466 sizeof(struct flow) - FLOW_SEGMENT_2_ENDS_AT);
2467 flow->dp_hash = 0;
2468 }
8bfd0fda
BP
2469 }
2470 flow->dl_type = mpls_eth_type;
2471}
2472
2473/* Tries to remove the outermost MPLS label from 'flow'. Returns true if
2474 * successful, false otherwise. On success, sets 'flow''s Ethernet type to
2475 * 'eth_type'.
2476 *
2477 * 'n' must be flow_count_mpls_labels(flow). */
2478bool
2479flow_pop_mpls(struct flow *flow, int n, ovs_be16 eth_type,
2480 struct flow_wildcards *wc)
2481{
2482 int i;
2483
2484 if (n == 0) {
2485 /* Nothing to pop. */
2486 return false;
22d38fca
JR
2487 } else if (n == FLOW_MAX_MPLS_LABELS) {
2488 if (wc) {
2489 wc->masks.mpls_lse[n - 1] |= htonl(MPLS_BOS_MASK);
2490 }
2491 if (!(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
2492 /* Can't pop because don't know what to fill in mpls_lse[n - 1]. */
2493 return false;
2494 }
8bfd0fda
BP
2495 }
2496
22d38fca
JR
2497 if (wc) {
2498 memset(&wc->masks.mpls_lse[1], 0xff,
2499 sizeof *wc->masks.mpls_lse * (n - 1));
2500 }
8bfd0fda
BP
2501 for (i = 1; i < n; i++) {
2502 flow->mpls_lse[i - 1] = flow->mpls_lse[i];
2503 }
2504 flow->mpls_lse[n - 1] = 0;
2505 flow->dl_type = eth_type;
2506 return true;
2507}
2508
b02475c5
SH
2509/* Sets the MPLS Label that 'flow' matches to 'label', which is interpreted
2510 * as an OpenFlow 1.1 "mpls_label" value. */
2511void
8bfd0fda 2512flow_set_mpls_label(struct flow *flow, int idx, ovs_be32 label)
b02475c5 2513{
8bfd0fda 2514 set_mpls_lse_label(&flow->mpls_lse[idx], label);
b02475c5
SH
2515}
2516
b676167a
SH
2517/* Sets the MPLS TTL that 'flow' matches to 'ttl', which should be in the
2518 * range 0...255. */
2519void
8bfd0fda 2520flow_set_mpls_ttl(struct flow *flow, int idx, uint8_t ttl)
b676167a 2521{
8bfd0fda 2522 set_mpls_lse_ttl(&flow->mpls_lse[idx], ttl);
b676167a
SH
2523}
2524
b02475c5
SH
2525/* Sets the MPLS TC that 'flow' matches to 'tc', which should be in the
2526 * range 0...7. */
2527void
8bfd0fda 2528flow_set_mpls_tc(struct flow *flow, int idx, uint8_t tc)
b02475c5 2529{
8bfd0fda 2530 set_mpls_lse_tc(&flow->mpls_lse[idx], tc);
b02475c5
SH
2531}
2532
2533/* Sets the MPLS BOS bit that 'flow' matches to which should be 0 or 1. */
2534void
8bfd0fda 2535flow_set_mpls_bos(struct flow *flow, int idx, uint8_t bos)
b02475c5 2536{
8bfd0fda 2537 set_mpls_lse_bos(&flow->mpls_lse[idx], bos);
b02475c5
SH
2538}
2539
8bfd0fda
BP
2540/* Sets the entire MPLS LSE. */
2541void
2542flow_set_mpls_lse(struct flow *flow, int idx, ovs_be32 lse)
2543{
2544 flow->mpls_lse[idx] = lse;
2545}
52105b67 2546
437d0d22 2547static size_t
cf62fa4c 2548flow_compose_l4(struct dp_packet *p, const struct flow *flow)
52105b67 2549{
437d0d22
JR
2550 size_t l4_len = 0;
2551
52105b67
JR
2552 if (!(flow->nw_frag & FLOW_NW_FRAG_ANY)
2553 || !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2554 if (flow->nw_proto == IPPROTO_TCP) {
2555 struct tcp_header *tcp;
2556
437d0d22 2557 l4_len = sizeof *tcp;
cf62fa4c 2558 tcp = dp_packet_put_zeros(p, l4_len);
52105b67
JR
2559 tcp->tcp_src = flow->tp_src;
2560 tcp->tcp_dst = flow->tp_dst;
2561 tcp->tcp_ctl = TCP_CTL(ntohs(flow->tcp_flags), 5);
52105b67
JR
2562 } else if (flow->nw_proto == IPPROTO_UDP) {
2563 struct udp_header *udp;
2564
437d0d22 2565 l4_len = sizeof *udp;
cf62fa4c 2566 udp = dp_packet_put_zeros(p, l4_len);
52105b67
JR
2567 udp->udp_src = flow->tp_src;
2568 udp->udp_dst = flow->tp_dst;
e839d01e 2569 udp->udp_len = htons(l4_len);
52105b67
JR
2570 } else if (flow->nw_proto == IPPROTO_SCTP) {
2571 struct sctp_header *sctp;
2572
437d0d22 2573 l4_len = sizeof *sctp;
cf62fa4c 2574 sctp = dp_packet_put_zeros(p, l4_len);
52105b67
JR
2575 sctp->sctp_src = flow->tp_src;
2576 sctp->sctp_dst = flow->tp_dst;
52105b67
JR
2577 } else if (flow->nw_proto == IPPROTO_ICMP) {
2578 struct icmp_header *icmp;
2579
437d0d22 2580 l4_len = sizeof *icmp;
cf62fa4c 2581 icmp = dp_packet_put_zeros(p, l4_len);
52105b67
JR
2582 icmp->icmp_type = ntohs(flow->tp_src);
2583 icmp->icmp_code = ntohs(flow->tp_dst);
0e612675
FL
2584 } else if (flow->nw_proto == IPPROTO_IGMP) {
2585 struct igmp_header *igmp;
2586
2587 l4_len = sizeof *igmp;
cf62fa4c 2588 igmp = dp_packet_put_zeros(p, l4_len);
0e612675
FL
2589 igmp->igmp_type = ntohs(flow->tp_src);
2590 igmp->igmp_code = ntohs(flow->tp_dst);
2591 put_16aligned_be32(&igmp->group, flow->igmp_group_ip4);
52105b67
JR
2592 } else if (flow->nw_proto == IPPROTO_ICMPV6) {
2593 struct icmp6_hdr *icmp;
2594
437d0d22 2595 l4_len = sizeof *icmp;
cf62fa4c 2596 icmp = dp_packet_put_zeros(p, l4_len);
52105b67
JR
2597 icmp->icmp6_type = ntohs(flow->tp_src);
2598 icmp->icmp6_code = ntohs(flow->tp_dst);
2599
2600 if (icmp->icmp6_code == 0 &&
2601 (icmp->icmp6_type == ND_NEIGHBOR_SOLICIT ||
2602 icmp->icmp6_type == ND_NEIGHBOR_ADVERT)) {
2603 struct in6_addr *nd_target;
86d46f3c 2604 struct ovs_nd_lla_opt *lla_opt;
52105b67 2605
437d0d22 2606 l4_len += sizeof *nd_target;
cf62fa4c 2607 nd_target = dp_packet_put_zeros(p, sizeof *nd_target);
52105b67
JR
2608 *nd_target = flow->nd_target;
2609
2610 if (!eth_addr_is_zero(flow->arp_sha)) {
437d0d22 2611 l4_len += 8;
86d46f3c
ZKL
2612 lla_opt = dp_packet_put_zeros(p, 8);
2613 lla_opt->len = 1;
2614 lla_opt->type = ND_OPT_SOURCE_LINKADDR;
2615 lla_opt->mac = flow->arp_sha;
52105b67
JR
2616 }
2617 if (!eth_addr_is_zero(flow->arp_tha)) {
437d0d22 2618 l4_len += 8;
86d46f3c
ZKL
2619 lla_opt = dp_packet_put_zeros(p, 8);
2620 lla_opt->len = 1;
2621 lla_opt->type = ND_OPT_TARGET_LINKADDR;
2622 lla_opt->mac = flow->arp_tha;
52105b67
JR
2623 }
2624 }
52105b67
JR
2625 }
2626 }
437d0d22 2627 return l4_len;
52105b67
JR
2628}
2629
e839d01e
DDP
2630static void
2631flow_compose_l4_csum(struct dp_packet *p, const struct flow *flow,
2632 uint32_t pseudo_hdr_csum)
2633{
2634 size_t l4_len = (char *) dp_packet_tail(p) - (char *) dp_packet_l4(p);
2635
2636 if (!(flow->nw_frag & FLOW_NW_FRAG_ANY)
2637 || !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
2638 if (flow->nw_proto == IPPROTO_TCP) {
2639 struct tcp_header *tcp = dp_packet_l4(p);
2640
2641 /* Checksum has already been zeroed by put_zeros call in
2642 * flow_compose_l4(). */
2643 tcp->tcp_csum = csum_finish(csum_continue(pseudo_hdr_csum,
2644 tcp, l4_len));
2645 } else if (flow->nw_proto == IPPROTO_UDP) {
2646 struct udp_header *udp = dp_packet_l4(p);
2647
2648 /* Checksum has already been zeroed by put_zeros call in
2649 * flow_compose_l4(). */
2650 udp->udp_csum = csum_finish(csum_continue(pseudo_hdr_csum,
2651 udp, l4_len));
2652 } else if (flow->nw_proto == IPPROTO_ICMP) {
2653 struct icmp_header *icmp = dp_packet_l4(p);
2654
2655 /* Checksum has already been zeroed by put_zeros call in
2656 * flow_compose_l4(). */
2657 icmp->icmp_csum = csum(icmp, l4_len);
2658 } else if (flow->nw_proto == IPPROTO_IGMP) {
2659 struct igmp_header *igmp = dp_packet_l4(p);
2660
2661 /* Checksum has already been zeroed by put_zeros call in
2662 * flow_compose_l4(). */
2663 igmp->igmp_csum = csum(igmp, l4_len);
2664 } else if (flow->nw_proto == IPPROTO_ICMPV6) {
2665 struct icmp6_hdr *icmp = dp_packet_l4(p);
2666
2667 /* Checksum has already been zeroed by put_zeros call in
2668 * flow_compose_l4(). */
2669 icmp->icmp6_cksum = (OVS_FORCE uint16_t)
2670 csum_finish(csum_continue(pseudo_hdr_csum, icmp, l4_len));
2671 }
2672 }
2673}
2674
3e24a894 2675/* Puts into 'p' a packet that flow_extract() would parse as having the given
8b3b8dd1
BP
2676 * 'flow'.
2677 *
2678 * (This is useful only for testing, obviously, and the packet isn't really
e839d01e 2679 * valid. Lots of fields are just zeroed.) */
8b3b8dd1 2680void
cf62fa4c 2681flow_compose(struct dp_packet *p, const struct flow *flow)
8b3b8dd1 2682{
e839d01e 2683 uint32_t pseudo_hdr_csum;
437d0d22
JR
2684 size_t l4_len;
2685
52105b67 2686 /* eth_compose() sets l3 pointer and makes sure it is 32-bit aligned. */
cf62fa4c 2687 eth_compose(p, flow->dl_dst, flow->dl_src, ntohs(flow->dl_type), 0);
8b3b8dd1 2688 if (flow->dl_type == htons(FLOW_DL_TYPE_NONE)) {
2482b0b0 2689 struct eth_header *eth = dp_packet_eth(p);
cf62fa4c 2690 eth->eth_type = htons(dp_packet_size(p));
8b3b8dd1
BP
2691 return;
2692 }
2693
f0fb825a
EG
2694 for (int encaps = FLOW_MAX_VLAN_HEADERS - 1; encaps >= 0; encaps--) {
2695 if (flow->vlans[encaps].tci & htons(VLAN_CFI)) {
2696 eth_push_vlan(p, flow->vlans[encaps].tpid,
2697 flow->vlans[encaps].tci);
2698 }
8b3b8dd1
BP
2699 }
2700
cff78c88 2701 if (flow->dl_type == htons(ETH_TYPE_IP)) {
8b3b8dd1
BP
2702 struct ip_header *ip;
2703
cf62fa4c 2704 ip = dp_packet_put_zeros(p, sizeof *ip);
8b3b8dd1 2705 ip->ip_ihl_ver = IP_IHL_VER(5, 4);
eadef313 2706 ip->ip_tos = flow->nw_tos;
aabf5352 2707 ip->ip_ttl = flow->nw_ttl;
8b3b8dd1 2708 ip->ip_proto = flow->nw_proto;
7c457c33
BP
2709 put_16aligned_be32(&ip->ip_src, flow->nw_src);
2710 put_16aligned_be32(&ip->ip_dst, flow->nw_dst);
8b3b8dd1 2711
eadef313 2712 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
7257b535 2713 ip->ip_frag_off |= htons(IP_MORE_FRAGMENTS);
eadef313 2714 if (flow->nw_frag & FLOW_NW_FRAG_LATER) {
7257b535
BP
2715 ip->ip_frag_off |= htons(100);
2716 }
2717 }
df9b6612 2718
cf62fa4c 2719 dp_packet_set_l4(p, dp_packet_tail(p));
52105b67 2720
cf62fa4c 2721 l4_len = flow_compose_l4(p, flow);
52105b67 2722
cf62fa4c
PS
2723 ip = dp_packet_l3(p);
2724 ip->ip_tot_len = htons(p->l4_ofs - p->l3_ofs + l4_len);
ece9c294 2725 /* Checksum has already been zeroed by put_zeros call. */
dc5a7ce7 2726 ip->ip_csum = csum(ip, sizeof *ip);
e839d01e
DDP
2727
2728 pseudo_hdr_csum = packet_csum_pseudoheader(ip);
2729 flow_compose_l4_csum(p, flow, pseudo_hdr_csum);
cff78c88 2730 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
52105b67
JR
2731 struct ovs_16aligned_ip6_hdr *nh;
2732
cf62fa4c 2733 nh = dp_packet_put_zeros(p, sizeof *nh);
52105b67
JR
2734 put_16aligned_be32(&nh->ip6_flow, htonl(6 << 28) |
2735 htonl(flow->nw_tos << 20) | flow->ipv6_label);
2736 nh->ip6_hlim = flow->nw_ttl;
2737 nh->ip6_nxt = flow->nw_proto;
2738
2739 memcpy(&nh->ip6_src, &flow->ipv6_src, sizeof(nh->ip6_src));
2740 memcpy(&nh->ip6_dst, &flow->ipv6_dst, sizeof(nh->ip6_dst));
2741
cf62fa4c 2742 dp_packet_set_l4(p, dp_packet_tail(p));
52105b67 2743
cf62fa4c 2744 l4_len = flow_compose_l4(p, flow);
52105b67 2745
cf62fa4c 2746 nh = dp_packet_l3(p);
437d0d22 2747 nh->ip6_plen = htons(l4_len);
e839d01e
DDP
2748
2749 pseudo_hdr_csum = packet_csum_pseudoheader6(nh);
2750 flow_compose_l4_csum(p, flow, pseudo_hdr_csum);
cff78c88
SH
2751 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
2752 flow->dl_type == htons(ETH_TYPE_RARP)) {
8b3b8dd1
BP
2753 struct arp_eth_header *arp;
2754
cf62fa4c
PS
2755 arp = dp_packet_put_zeros(p, sizeof *arp);
2756 dp_packet_set_l3(p, arp);
8b3b8dd1
BP
2757 arp->ar_hrd = htons(1);
2758 arp->ar_pro = htons(ETH_TYPE_IP);
2759 arp->ar_hln = ETH_ADDR_LEN;
2760 arp->ar_pln = 4;
2761 arp->ar_op = htons(flow->nw_proto);
2762
2763 if (flow->nw_proto == ARP_OP_REQUEST ||
2764 flow->nw_proto == ARP_OP_REPLY) {
7c457c33
BP
2765 put_16aligned_be32(&arp->ar_spa, flow->nw_src);
2766 put_16aligned_be32(&arp->ar_tpa, flow->nw_dst);
74ff3298
JR
2767 arp->ar_sha = flow->arp_sha;
2768 arp->ar_tha = flow->arp_tha;
8b3b8dd1
BP
2769 }
2770 }
b02475c5
SH
2771
2772 if (eth_type_mpls(flow->dl_type)) {
8bfd0fda
BP
2773 int n;
2774
cf62fa4c 2775 p->l2_5_ofs = p->l3_ofs;
8bfd0fda
BP
2776 for (n = 1; n < FLOW_MAX_MPLS_LABELS; n++) {
2777 if (flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK)) {
2778 break;
2779 }
2780 }
2781 while (n > 0) {
cf62fa4c 2782 push_mpls(p, flow->dl_type, flow->mpls_lse[--n]);
8bfd0fda 2783 }
b02475c5 2784 }
8b3b8dd1 2785}
5cb7a798
BP
2786\f
2787/* Compressed flow. */
2788
df40c152 2789/* Completes an initialization of 'dst' as a miniflow copy of 'src' begun by
5fcff47b
JR
2790 * the caller. The caller must have already computed 'dst->map' properly to
2791 * indicate the significant uint64_t elements of 'src'.
13751fd8
JR
2792 *
2793 * Normally the significant elements are the ones that are non-zero. However,
2794 * when a miniflow is initialized from a (mini)mask, the values can be zeroes,
ceb3bd67
JR
2795 * so that the flow and mask always have the same maps. */
2796void
2797miniflow_init(struct miniflow *dst, const struct flow *src)
df40c152 2798{
09b0fa9c 2799 uint64_t *dst_u64 = miniflow_values(dst);
361d808d 2800 size_t idx;
df40c152 2801
5fcff47b
JR
2802 FLOWMAP_FOR_EACH_INDEX(idx, dst->map) {
2803 *dst_u64++ = flow_u64_value(src, idx);
df40c152
BP
2804 }
2805}
2806
361d808d 2807/* Initialize the maps of 'flow' from 'src'. */
ceb3bd67
JR
2808void
2809miniflow_map_init(struct miniflow *flow, const struct flow *src)
5cb7a798 2810{
ceb3bd67 2811 /* Initialize map, counting the number of nonzero elements. */
5fcff47b
JR
2812 flowmap_init(&flow->map);
2813 for (size_t i = 0; i < FLOW_U64S; i++) {
2814 if (flow_u64_value(src, i)) {
2815 flowmap_set(&flow->map, i, 1);
5cb7a798
BP
2816 }
2817 }
ceb3bd67 2818}
5cb7a798 2819
ceb3bd67
JR
2820/* Allocates 'n' count of miniflows, consecutive in memory, initializing the
2821 * map of each from 'src'.
2822 * Returns the size of the miniflow data. */
2823size_t
2824miniflow_alloc(struct miniflow *dsts[], size_t n, const struct miniflow *src)
2825{
361d808d
JR
2826 size_t n_values = miniflow_n_values(src);
2827 size_t data_size = MINIFLOW_VALUES_SIZE(n_values);
2828 struct miniflow *dst = xmalloc(n * (sizeof *src + data_size));
5fcff47b 2829 size_t i;
ceb3bd67
JR
2830
2831 COVERAGE_INC(miniflow_malloc);
2832
2833 for (i = 0; i < n; i++) {
361d808d 2834 *dst = *src; /* Copy maps. */
ceb3bd67 2835 dsts[i] = dst;
361d808d
JR
2836 dst += 1; /* Just past the maps. */
2837 dst = (struct miniflow *)((uint64_t *)dst + n_values); /* Skip data. */
ceb3bd67
JR
2838 }
2839 return data_size;
df40c152 2840}
5cb7a798 2841
ceb3bd67
JR
2842/* Returns a miniflow copy of 'src'. The caller must eventually free() the
2843 * returned miniflow. */
8fd47924 2844struct miniflow *
ceb3bd67 2845miniflow_create(const struct flow *src)
df40c152 2846{
ceb3bd67
JR
2847 struct miniflow tmp;
2848 struct miniflow *dst;
2849
2850 miniflow_map_init(&tmp, src);
2851
2852 miniflow_alloc(&dst, 1, &tmp);
2853 miniflow_init(dst, src);
2854 return dst;
5cb7a798
BP
2855}
2856
3016f3e4 2857/* Initializes 'dst' as a copy of 'src'. The caller must have allocated
8fd47924 2858 * 'dst' to have inline space for 'n_values' data in 'src'. */
3016f3e4 2859void
a851eb94
JR
2860miniflow_clone(struct miniflow *dst, const struct miniflow *src,
2861 size_t n_values)
3016f3e4 2862{
361d808d 2863 *dst = *src; /* Copy maps. */
09b0fa9c
JR
2864 memcpy(miniflow_values(dst), miniflow_get_values(src),
2865 MINIFLOW_VALUES_SIZE(n_values));
5cb7a798
BP
2866}
2867
2868/* Initializes 'dst' as a copy of 'src'. */
2869void
2870miniflow_expand(const struct miniflow *src, struct flow *dst)
2871{
ad77e3c5
EJ
2872 memset(dst, 0, sizeof *dst);
2873 flow_union_with_miniflow(dst, src);
5cb7a798
BP
2874}
2875
8fd47924 2876/* Returns true if 'a' and 'b' are equal miniflows, false otherwise. */
5cb7a798
BP
2877bool
2878miniflow_equal(const struct miniflow *a, const struct miniflow *b)
2879{
09b0fa9c
JR
2880 const uint64_t *ap = miniflow_get_values(a);
2881 const uint64_t *bp = miniflow_get_values(b);
5cb7a798 2882
5fcff47b
JR
2883 /* This is mostly called after a matching hash, so it is highly likely that
2884 * the maps are equal as well. */
2885 if (OVS_LIKELY(flowmap_equal(a->map, b->map))) {
361d808d 2886 return !memcmp(ap, bp, miniflow_n_values(a) * sizeof *ap);
080e28d0 2887 } else {
5fcff47b 2888 size_t idx;
df40c152 2889
5fcff47b
JR
2890 FLOWMAP_FOR_EACH_INDEX (idx, flowmap_or(a->map, b->map)) {
2891 if ((flowmap_is_set(&a->map, idx) ? *ap++ : 0)
2892 != (flowmap_is_set(&b->map, idx) ? *bp++ : 0)) {
080e28d0 2893 return false;
df40c152 2894 }
5cb7a798
BP
2895 }
2896 }
2897
df40c152 2898 return true;
5cb7a798
BP
2899}
2900
de4ad4a2
JR
2901/* Returns false if 'a' and 'b' differ at the places where there are 1-bits
2902 * in 'mask', true otherwise. */
5cb7a798
BP
2903bool
2904miniflow_equal_in_minimask(const struct miniflow *a, const struct miniflow *b,
2905 const struct minimask *mask)
2906{
09b0fa9c 2907 const uint64_t *p = miniflow_get_values(&mask->masks);
361d808d 2908 size_t idx;
5cb7a798 2909
5fcff47b 2910 FLOWMAP_FOR_EACH_INDEX(idx, mask->masks.map) {
1cea007c 2911 if ((miniflow_get(a, idx) ^ miniflow_get(b, idx)) & *p++) {
080e28d0 2912 return false;
5cb7a798
BP
2913 }
2914 }
2915
2916 return true;
2917}
2918
2919/* Returns true if 'a' and 'b' are equal at the places where there are 1-bits
2920 * in 'mask', false if they differ. */
2921bool
2922miniflow_equal_flow_in_minimask(const struct miniflow *a, const struct flow *b,
2923 const struct minimask *mask)
2924{
09b0fa9c 2925 const uint64_t *p = miniflow_get_values(&mask->masks);
361d808d 2926 size_t idx;
5cb7a798 2927
5fcff47b
JR
2928 FLOWMAP_FOR_EACH_INDEX(idx, mask->masks.map) {
2929 if ((miniflow_get(a, idx) ^ flow_u64_value(b, idx)) & *p++) {
080e28d0 2930 return false;
5cb7a798
BP
2931 }
2932 }
2933
2934 return true;
2935}
2936
5cb7a798 2937\f
ceb3bd67
JR
2938void
2939minimask_init(struct minimask *mask, const struct flow_wildcards *wc)
2940{
2941 miniflow_init(&mask->masks, &wc->masks);
2942}
2943
8fd47924
JR
2944/* Returns a minimask copy of 'wc'. The caller must eventually free the
2945 * returned minimask with free(). */
2946struct minimask *
2947minimask_create(const struct flow_wildcards *wc)
5cb7a798 2948{
8fd47924 2949 return (struct minimask *)miniflow_create(&wc->masks);
5cb7a798
BP
2950}
2951
2952/* Initializes 'dst_' as the bit-wise "and" of 'a_' and 'b_'.
2953 *
8fd47924
JR
2954 * The caller must provide room for FLOW_U64S "uint64_t"s in 'storage', which
2955 * must follow '*dst_' in memory, for use by 'dst_'. The caller must *not*
2956 * free 'dst_' free(). */
5cb7a798
BP
2957void
2958minimask_combine(struct minimask *dst_,
2959 const struct minimask *a_, const struct minimask *b_,
d70e8c28 2960 uint64_t storage[FLOW_U64S])
5cb7a798
BP
2961{
2962 struct miniflow *dst = &dst_->masks;
d70e8c28 2963 uint64_t *dst_values = storage;
5cb7a798
BP
2964 const struct miniflow *a = &a_->masks;
2965 const struct miniflow *b = &b_->masks;
361d808d 2966 size_t idx;
5cb7a798 2967
5fcff47b 2968 flowmap_init(&dst->map);
080e28d0 2969
5fcff47b 2970 FLOWMAP_FOR_EACH_INDEX(idx, flowmap_and(a->map, b->map)) {
361d808d 2971 /* Both 'a' and 'b' have non-zero data at 'idx'. */
5fcff47b 2972 uint64_t mask = *miniflow_get__(a, idx) & *miniflow_get__(b, idx);
361d808d
JR
2973
2974 if (mask) {
5fcff47b 2975 flowmap_set(&dst->map, idx, 1);
1cea007c 2976 *dst_values++ = mask;
5cb7a798
BP
2977 }
2978 }
2979}
2980
8fd47924 2981/* Initializes 'wc' as a copy of 'mask'. */
5cb7a798
BP
2982void
2983minimask_expand(const struct minimask *mask, struct flow_wildcards *wc)
2984{
2985 miniflow_expand(&mask->masks, &wc->masks);
2986}
2987
f4d335e9
JR
2988/* Returns true if 'a' and 'b' are the same flow mask, false otherwise.
2989 * Minimasks may not have zero data values, so for the minimasks to be the
2990 * same, they need to have the same map and the same data values. */
5cb7a798
BP
2991bool
2992minimask_equal(const struct minimask *a, const struct minimask *b)
2993{
5fcff47b
JR
2994 return !memcmp(a, b, sizeof *a
2995 + MINIFLOW_VALUES_SIZE(miniflow_n_values(&a->masks)));
5cb7a798
BP
2996}
2997
d4570fd8 2998/* Returns true if at least one bit matched by 'b' is wildcarded by 'a',
5cb7a798
BP
2999 * false otherwise. */
3000bool
d4570fd8 3001minimask_has_extra(const struct minimask *a, const struct minimask *b)
5cb7a798 3002{
09b0fa9c 3003 const uint64_t *bp = miniflow_get_values(&b->masks);
361d808d 3004 size_t idx;
5cb7a798 3005
5fcff47b 3006 FLOWMAP_FOR_EACH_INDEX(idx, b->masks.map) {
d70e8c28 3007 uint64_t b_u64 = *bp++;
5cb7a798 3008
d70e8c28
JR
3009 /* 'b_u64' is non-zero, check if the data in 'a' is either zero
3010 * or misses some of the bits in 'b_u64'. */
5fcff47b
JR
3011 if (!MINIFLOW_IN_MAP(&a->masks, idx)
3012 || ((*miniflow_get__(&a->masks, idx) & b_u64) != b_u64)) {
f4d335e9 3013 return true; /* 'a' wildcards some bits 'b' doesn't. */
5cb7a798
BP
3014 }
3015 }
3016
3017 return false;
3018}
f0fb825a
EG
3019
3020void
3021flow_limit_vlans(int vlan_limit)
3022{
3023 if (vlan_limit <= 0) {
3024 flow_vlan_limit = FLOW_MAX_VLAN_HEADERS;
3025 } else {
3026 flow_vlan_limit = MIN(vlan_limit, FLOW_MAX_VLAN_HEADERS);
3027 }
3028}