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