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