]> git.proxmox.com Git - mirror_ovs.git/blame - lib/flow.c
ofproto-dpif-upcall: Echo HASH attribute back to datapath.
[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
7dc18ae9 132#if (FLOW_WC_SEQ != 41)
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
BP
733 /* Add code to this function (or its callees) to extract new fields. */
734 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 41);
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 }
1110 if (OVS_LIKELY(dl_type == htons(ETH_TYPE_IP))) {
1111 const struct ip_header *nh = data;
1112 int ip_len;
1113 uint16_t tot_len;
1114
1115 if (OVS_UNLIKELY(!ipv4_sanity_check(nh, size, &ip_len, &tot_len))) {
1116 return 0;
1117 }
1118 dp_packet_set_l2_pad_size(packet, size - tot_len);
1119 packet->l3_ofs = (uint16_t)((char *)nh - frame);
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 }
1132 packet->l3_ofs = (uint16_t)((char *)nh - frame);
1133 data_pull(&data, &size, sizeof *nh);
1134
1135 plen = ntohs(nh->ip6_plen); /* Never pull padding. */
1136 dp_packet_set_l2_pad_size(packet, size - plen);
1137 size = plen;
523464ab 1138 const struct ovs_16aligned_ip6_frag *frag_hdr;
d0dd4908 1139 nw_proto = nh->ip6_nxt;
523464ab
DB
1140 if (!parse_ipv6_ext_hdrs__(&data, &size, &nw_proto, &nw_frag,
1141 &frag_hdr)) {
aab96ec4
YL
1142 return 0;
1143 }
aab96ec4
YL
1144 } else {
1145 return 0;
1146 }
1147
1148 packet->l4_ofs = (uint16_t)((char *)data - frame);
1149 if (!(nw_frag & FLOW_NW_FRAG_LATER) && nw_proto == IPPROTO_TCP &&
1150 size >= TCP_HEADER_LEN) {
1151 const struct tcp_header *tcp = data;
1152
1153 return TCP_FLAGS(tcp->tcp_ctl);
1154 }
1155
1156 return 0;
1157}
1158
993410fb
BP
1159/* For every bit of a field that is wildcarded in 'wildcards', sets the
1160 * corresponding bit in 'flow' to zero. */
1161void
1162flow_zero_wildcards(struct flow *flow, const struct flow_wildcards *wildcards)
1163{
d70e8c28
JR
1164 uint64_t *flow_u64 = (uint64_t *) flow;
1165 const uint64_t *wc_u64 = (const uint64_t *) &wildcards->masks;
659c2346 1166 size_t i;
993410fb 1167
d70e8c28
JR
1168 for (i = 0; i < FLOW_U64S; i++) {
1169 flow_u64[i] &= wc_u64[i];
26720e24 1170 }
993410fb
BP
1171}
1172
d8d9c698
EJ
1173void
1174flow_unwildcard_tp_ports(const struct flow *flow, struct flow_wildcards *wc)
1175{
1176 if (flow->nw_proto != IPPROTO_ICMP) {
1177 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
1178 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
1179 } else {
1180 wc->masks.tp_src = htons(0xff);
1181 wc->masks.tp_dst = htons(0xff);
1182 }
1183}
1184
50dcbd8e 1185/* Initializes 'flow_metadata' with the metadata found in 'flow'. */
5d6c3af0 1186void
50dcbd8e 1187flow_get_metadata(const struct flow *flow, struct match *flow_metadata)
5d6c3af0 1188{
50dcbd8e
JG
1189 int i;
1190
7dc18ae9 1191 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 41);
e9358af6 1192
50dcbd8e
JG
1193 match_init_catchall(flow_metadata);
1194 if (flow->tunnel.tun_id != htonll(0)) {
1195 match_set_tun_id(flow_metadata, flow->tunnel.tun_id);
1196 }
b666962b
JG
1197 if (flow->tunnel.flags & FLOW_TNL_PUB_F_MASK) {
1198 match_set_tun_flags(flow_metadata,
1199 flow->tunnel.flags & FLOW_TNL_PUB_F_MASK);
1200 }
ffe4c74f 1201 if (flow->tunnel.ip_src) {
50dcbd8e
JG
1202 match_set_tun_src(flow_metadata, flow->tunnel.ip_src);
1203 }
ffe4c74f 1204 if (flow->tunnel.ip_dst) {
50dcbd8e
JG
1205 match_set_tun_dst(flow_metadata, flow->tunnel.ip_dst);
1206 }
ffe4c74f
JB
1207 if (ipv6_addr_is_set(&flow->tunnel.ipv6_src)) {
1208 match_set_tun_ipv6_src(flow_metadata, &flow->tunnel.ipv6_src);
1209 }
1210 if (ipv6_addr_is_set(&flow->tunnel.ipv6_dst)) {
1211 match_set_tun_ipv6_dst(flow_metadata, &flow->tunnel.ipv6_dst);
1212 }
50dcbd8e
JG
1213 if (flow->tunnel.gbp_id != htons(0)) {
1214 match_set_tun_gbp_id(flow_metadata, flow->tunnel.gbp_id);
1215 }
1216 if (flow->tunnel.gbp_flags) {
1217 match_set_tun_gbp_flags(flow_metadata, flow->tunnel.gbp_flags);
1218 }
7dc18ae9
WT
1219 if (flow->tunnel.erspan_ver) {
1220 match_set_tun_erspan_ver(flow_metadata, flow->tunnel.erspan_ver);
1221 }
1222 if (flow->tunnel.erspan_idx) {
1223 match_set_tun_erspan_idx(flow_metadata, flow->tunnel.erspan_idx);
1224 }
1225 if (flow->tunnel.erspan_dir) {
1226 match_set_tun_erspan_dir(flow_metadata, flow->tunnel.erspan_dir);
1227 }
1228 if (flow->tunnel.erspan_hwid) {
1229 match_set_tun_erspan_hwid(flow_metadata, flow->tunnel.erspan_hwid);
1230 }
6728d578 1231 tun_metadata_get_fmd(&flow->tunnel, flow_metadata);
50dcbd8e
JG
1232 if (flow->metadata != htonll(0)) {
1233 match_set_metadata(flow_metadata, flow->metadata);
1234 }
1235
1236 for (i = 0; i < FLOW_N_REGS; i++) {
1237 if (flow->regs[i]) {
1238 match_set_reg(flow_metadata, i, flow->regs[i]);
1239 }
1240 }
1241
1242 if (flow->pkt_mark != 0) {
1243 match_set_pkt_mark(flow_metadata, flow->pkt_mark);
1244 }
1245
1246 match_set_in_port(flow_metadata, flow->in_port.ofp_port);
6a81043e
JS
1247 if (flow->packet_type != htonl(PT_ETH)) {
1248 match_set_packet_type(flow_metadata, flow->packet_type);
1249 }
1250
07659514
JS
1251 if (flow->ct_state != 0) {
1252 match_set_ct_state(flow_metadata, flow->ct_state);
7827edca
DA
1253 /* Match dl_type since it is required for the later interpretation of
1254 * the conntrack metadata. */
1255 match_set_dl_type(flow_metadata, flow->dl_type);
daf4d3c1
JR
1256 if (is_ct_valid(flow, NULL, NULL) && flow->ct_nw_proto != 0) {
1257 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1258 match_set_ct_nw_src(flow_metadata, flow->ct_nw_src);
1259 match_set_ct_nw_dst(flow_metadata, flow->ct_nw_dst);
1260 match_set_ct_nw_proto(flow_metadata, flow->ct_nw_proto);
1261 match_set_ct_tp_src(flow_metadata, flow->ct_tp_src);
1262 match_set_ct_tp_dst(flow_metadata, flow->ct_tp_dst);
1263 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1264 match_set_ct_ipv6_src(flow_metadata, &flow->ct_ipv6_src);
1265 match_set_ct_ipv6_dst(flow_metadata, &flow->ct_ipv6_dst);
1266 match_set_ct_nw_proto(flow_metadata, flow->ct_nw_proto);
1267 match_set_ct_tp_src(flow_metadata, flow->ct_tp_src);
1268 match_set_ct_tp_dst(flow_metadata, flow->ct_tp_dst);
1269 }
1270 }
07659514
JS
1271 }
1272 if (flow->ct_zone != 0) {
1273 match_set_ct_zone(flow_metadata, flow->ct_zone);
1274 }
8e53fe8c
JS
1275 if (flow->ct_mark != 0) {
1276 match_set_ct_mark(flow_metadata, flow->ct_mark);
1277 }
2ff8484b 1278 if (!ovs_u128_is_zero(flow->ct_label)) {
9daf2348
JS
1279 match_set_ct_label(flow_metadata, flow->ct_label);
1280 }
07659514
JS
1281}
1282
b02e6cf8
BP
1283const char *
1284ct_state_to_string(uint32_t state)
07659514
JS
1285{
1286 switch (state) {
fd6cd1bf
BP
1287#define CS_STATE(ENUM, INDEX, NAME) case CS_##ENUM: return NAME;
1288 CS_STATES
1289#undef CS_STATE
07659514
JS
1290 default:
1291 return NULL;
1292 }
5d6c3af0
EJ
1293}
1294
b02e6cf8
BP
1295uint32_t
1296ct_state_from_string(const char *s)
1297{
1298#define CS_STATE(ENUM, INDEX, NAME) \
1299 if (!strcmp(s, NAME)) { \
1300 return CS_##ENUM; \
1301 }
1302 CS_STATES
1303#undef CS_STATE
1304 return 0;
1305}
1306
b4293a33
YHW
1307/* Parses conntrack state from 'state_str'. If it is parsed successfully,
1308 * stores the parsed ct_state in 'ct_state', and returns true. Otherwise,
1309 * returns false, and reports error message in 'ds'. */
1310bool
1311parse_ct_state(const char *state_str, uint32_t default_state,
1312 uint32_t *ct_state, struct ds *ds)
1313{
1314 uint32_t state = default_state;
1315 char *state_s = xstrdup(state_str);
1316 char *save_ptr = NULL;
1317
1318 for (char *cs = strtok_r(state_s, ", ", &save_ptr); cs;
1319 cs = strtok_r(NULL, ", ", &save_ptr)) {
1320 uint32_t bit = ct_state_from_string(cs);
1321 if (!bit) {
1322 ds_put_format(ds, "%s: unknown connection tracking state flag",
1323 cs);
11e47653 1324 free(state_s);
b4293a33
YHW
1325 return false;
1326 }
1327 state |= bit;
1328 }
1329
1330 *ct_state = state;
1331 free(state_s);
1332
1333 return true;
1334}
1335
1336/* Checks the given conntrack state 'state' according to the constraints
1337 * listed in ovs-fields (7). Returns true if it is valid. Otherwise, returns
1338 * false, and reports error in 'ds'. */
1339bool
1340validate_ct_state(uint32_t state, struct ds *ds)
1341{
1342 bool valid_ct_state = true;
1343 struct ds d_str = DS_EMPTY_INITIALIZER;
1344
1345 format_flags(&d_str, ct_state_to_string, state, '|');
1346
1347 if (state && !(state & CS_TRACKED)) {
1348 ds_put_format(ds, "%s: invalid connection state: "
1349 "If \"trk\" is unset, no other flags are set\n",
1350 ds_cstr(&d_str));
1351 valid_ct_state = false;
1352 }
1353 if (state & CS_INVALID && state & ~(CS_TRACKED | CS_INVALID)) {
1354 ds_put_format(ds, "%s: invalid connection state: "
1355 "when \"inv\" is set, only \"trk\" may also be set\n",
1356 ds_cstr(&d_str));
1357 valid_ct_state = false;
1358 }
1359 if (state & CS_NEW && state & CS_ESTABLISHED) {
1360 ds_put_format(ds, "%s: invalid connection state: "
1361 "\"new\" and \"est\" are mutually exclusive\n",
1362 ds_cstr(&d_str));
1363 valid_ct_state = false;
1364 }
1365 if (state & CS_NEW && state & CS_REPLY_DIR) {
1366 ds_put_format(ds, "%s: invalid connection state: "
1367 "\"new\" and \"rpy\" are mutually exclusive\n",
1368 ds_cstr(&d_str));
1369 valid_ct_state = false;
1370 }
1371
1372 ds_destroy(&d_str);
1373 return valid_ct_state;
1374}
1375
6846e91e
BP
1376/* Clears the fields in 'flow' associated with connection tracking. */
1377void
1378flow_clear_conntrack(struct flow *flow)
1379{
1380 flow->ct_state = 0;
1381 flow->ct_zone = 0;
1382 flow->ct_mark = 0;
1383 flow->ct_label = OVS_U128_ZERO;
1384
1385 flow->ct_nw_proto = 0;
1386 flow->ct_tp_src = 0;
1387 flow->ct_tp_dst = 0;
1388 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1389 flow->ct_nw_src = 0;
1390 flow->ct_nw_dst = 0;
1391 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1392 memset(&flow->ct_ipv6_src, 0, sizeof flow->ct_ipv6_src);
1393 memset(&flow->ct_ipv6_dst, 0, sizeof flow->ct_ipv6_dst);
1394 }
1395}
1396
064af421 1397char *
50f96b10
BP
1398flow_to_string(const struct flow *flow,
1399 const struct ofputil_port_map *port_map)
064af421
BP
1400{
1401 struct ds ds = DS_EMPTY_INITIALIZER;
50f96b10 1402 flow_format(&ds, flow, port_map);
064af421
BP
1403 return ds_cstr(&ds);
1404}
1405
4fe3445a
PS
1406const char *
1407flow_tun_flag_to_string(uint32_t flags)
1408{
1409 switch (flags) {
1410 case FLOW_TNL_F_DONT_FRAGMENT:
1411 return "df";
1412 case FLOW_TNL_F_CSUM:
1413 return "csum";
1414 case FLOW_TNL_F_KEY:
1415 return "key";
94872594
JG
1416 case FLOW_TNL_F_OAM:
1417 return "oam";
4fe3445a
PS
1418 default:
1419 return NULL;
1420 }
1421}
1422
1423void
1424format_flags(struct ds *ds, const char *(*bit_to_string)(uint32_t),
1425 uint32_t flags, char del)
1426{
1427 uint32_t bad = 0;
1428
1429 if (!flags) {
8e4c1621 1430 ds_put_char(ds, '0');
4fe3445a
PS
1431 return;
1432 }
1433 while (flags) {
1434 uint32_t bit = rightmost_1bit(flags);
1435 const char *s;
1436
1437 s = bit_to_string(bit);
1438 if (s) {
1439 ds_put_format(ds, "%s%c", s, del);
1440 } else {
1441 bad |= bit;
1442 }
1443
1444 flags &= ~bit;
1445 }
1446
1447 if (bad) {
1448 ds_put_format(ds, "0x%"PRIx32"%c", bad, del);
1449 }
1450 ds_chomp(ds, del);
1451}
1452
61bf6666
JR
1453void
1454format_flags_masked(struct ds *ds, const char *name,
1455 const char *(*bit_to_string)(uint32_t), uint32_t flags,
8e4c1621 1456 uint32_t mask, uint32_t max_mask)
61bf6666
JR
1457{
1458 if (name) {
0deec6d2 1459 ds_put_format(ds, "%s%s=%s", colors.param, name, colors.end);
61bf6666 1460 }
8e4c1621
JG
1461
1462 if (mask == max_mask) {
1463 format_flags(ds, bit_to_string, flags, '|');
1464 return;
1465 }
1466
1467 if (!mask) {
1468 ds_put_cstr(ds, "0/0");
1469 return;
1470 }
1471
61bf6666
JR
1472 while (mask) {
1473 uint32_t bit = rightmost_1bit(mask);
1474 const char *s = bit_to_string(bit);
1475
1476 ds_put_format(ds, "%s%s", (flags & bit) ? "+" : "-",
1477 s ? s : "[Unknown]");
1478 mask &= ~bit;
1479 }
1480}
1481
3d4b2e6e
JS
1482static void
1483put_u16_masked(struct ds *s, uint16_t value, uint16_t mask)
1484{
1485 if (!mask) {
1486 ds_put_char(s, '*');
1487 } else {
1488 if (value > 9) {
1489 ds_put_format(s, "0x%"PRIx16, value);
1490 } else {
1491 ds_put_format(s, "%"PRIu16, value);
1492 }
1493
1494 if (mask != UINT16_MAX) {
1495 ds_put_format(s, "/0x%"PRIx16, mask);
1496 }
1497 }
1498}
1499
1500void
1501format_packet_type_masked(struct ds *s, ovs_be32 value, ovs_be32 mask)
1502{
1503 if (value == htonl(PT_ETH) && mask == OVS_BE32_MAX) {
1504 ds_put_cstr(s, "eth");
1505 } else {
1506 ds_put_cstr(s, "packet_type=(");
1507 put_u16_masked(s, pt_ns(value), pt_ns(mask));
1508 ds_put_char(s, ',');
1509 put_u16_masked(s, pt_ns_type(value), pt_ns_type(mask));
1510 ds_put_char(s, ')');
1511 }
1512}
1513
8e4c1621
JG
1514/* Scans a string 's' of flags to determine their numerical value and
1515 * returns the number of characters parsed using 'bit_to_string' to
1516 * lookup flag names. Scanning continues until the character 'end' is
1517 * reached.
1518 *
1519 * In the event of a failure, a negative error code will be returned. In
1520 * addition, if 'res_string' is non-NULL then a descriptive string will
1521 * be returned incorporating the identifying string 'field_name'. This
1522 * error string must be freed by the caller.
1523 *
1524 * Upon success, the flag values will be stored in 'res_flags' and
1525 * optionally 'res_mask', if it is non-NULL (if it is NULL then any masks
1526 * present in the original string will be considered an error). The
1527 * caller may restrict the acceptable set of values through the mask
1528 * 'allowed'. */
1529int
1530parse_flags(const char *s, const char *(*bit_to_string)(uint32_t),
1531 char end, const char *field_name, char **res_string,
1532 uint32_t *res_flags, uint32_t allowed, uint32_t *res_mask)
1533{
1534 uint32_t result = 0;
1535 int n;
1536
1537 /* Parse masked flags in numeric format? */
1538 if (res_mask && ovs_scan(s, "%"SCNi32"/%"SCNi32"%n",
1539 res_flags, res_mask, &n) && n > 0) {
1540 if (*res_flags & ~allowed || *res_mask & ~allowed) {
1541 goto unknown;
1542 }
1543 return n;
1544 }
1545
1546 n = 0;
1547
1548 if (res_mask && (*s == '+' || *s == '-')) {
1549 uint32_t flags = 0, mask = 0;
1550
1551 /* Parse masked flags. */
1552 while (s[0] != end) {
1553 bool set;
1554 uint32_t bit;
1555 size_t len;
1556
1557 if (s[0] == '+') {
1558 set = true;
1559 } else if (s[0] == '-') {
1560 set = false;
1561 } else {
1562 if (res_string) {
1563 *res_string = xasprintf("%s: %s must be preceded by '+' "
1564 "(for SET) or '-' (NOT SET)", s,
1565 field_name);
1566 }
1567 return -EINVAL;
1568 }
1569 s++;
1570 n++;
1571
1572 for (bit = 1; bit; bit <<= 1) {
1573 const char *fname = bit_to_string(bit);
1574
1575 if (!fname) {
1576 continue;
1577 }
1578
1579 len = strlen(fname);
1580 if (strncmp(s, fname, len) ||
1581 (s[len] != '+' && s[len] != '-' && s[len] != end)) {
1582 continue;
1583 }
1584
1585 if (mask & bit) {
1586 /* bit already set. */
1587 if (res_string) {
1588 *res_string = xasprintf("%s: Each %s flag can be "
1589 "specified only once", s,
1590 field_name);
1591 }
1592 return -EINVAL;
1593 }
1594 if (!(bit & allowed)) {
1595 goto unknown;
1596 }
1597 if (set) {
1598 flags |= bit;
1599 }
1600 mask |= bit;
1601 break;
1602 }
1603
1604 if (!bit) {
1605 goto unknown;
1606 }
1607 s += len;
1608 n += len;
1609 }
1610
1611 *res_flags = flags;
1612 *res_mask = mask;
1613 return n;
1614 }
1615
1616 /* Parse unmasked flags. If a flag is present, it is set, otherwise
1617 * it is not set. */
1618 while (s[n] != end) {
1619 unsigned long long int flags;
1620 uint32_t bit;
1621 int n0;
1622
1623 if (ovs_scan(&s[n], "%lli%n", &flags, &n0)) {
1624 if (flags & ~allowed) {
1625 goto unknown;
1626 }
1627 n += n0 + (s[n + n0] == '|');
1628 result |= flags;
1629 continue;
1630 }
1631
1632 for (bit = 1; bit; bit <<= 1) {
1633 const char *name = bit_to_string(bit);
1634 size_t len;
1635
1636 if (!name) {
1637 continue;
1638 }
1639
1640 len = strlen(name);
1641 if (!strncmp(s + n, name, len) &&
1642 (s[n + len] == '|' || s[n + len] == end)) {
1643 if (!(bit & allowed)) {
1644 goto unknown;
1645 }
1646 result |= bit;
1647 n += len + (s[n + len] == '|');
1648 break;
1649 }
1650 }
1651
1652 if (!bit) {
1653 goto unknown;
1654 }
1655 }
1656
1657 *res_flags = result;
1658 if (res_mask) {
1659 *res_mask = UINT32_MAX;
1660 }
1661 if (res_string) {
1662 *res_string = NULL;
1663 }
1664 return n;
1665
1666unknown:
1667 if (res_string) {
1668 *res_string = xasprintf("%s: unknown %s flag(s)", s, field_name);
1669 }
1670 return -EINVAL;
1671}
1672
064af421 1673void
50f96b10
BP
1674flow_format(struct ds *ds,
1675 const struct flow *flow, const struct ofputil_port_map *port_map)
064af421 1676{
aa6c9932 1677 struct match match;
78c9486d 1678 struct flow_wildcards *wc = &match.wc;
296e07ac 1679
aa6c9932 1680 match_wc_init(&match, flow);
78c9486d
JR
1681
1682 /* As this function is most often used for formatting a packet in a
1683 * packet-in message, skip formatting the packet context fields that are
e6d9ab56
JR
1684 * all-zeroes to make the print-out easier on the eyes. This means that a
1685 * missing context field implies a zero value for that field. This is
1686 * similar to OpenFlow encoding of these fields, as the specification
1687 * states that all-zeroes context fields should not be encoded in the
1688 * packet-in messages. */
1689 if (!flow->in_port.ofp_port) {
1690 WC_UNMASK_FIELD(wc, in_port);
1691 }
78c9486d
JR
1692 if (!flow->skb_priority) {
1693 WC_UNMASK_FIELD(wc, skb_priority);
1694 }
1695 if (!flow->pkt_mark) {
1696 WC_UNMASK_FIELD(wc, pkt_mark);
1697 }
1698 if (!flow->recirc_id) {
1699 WC_UNMASK_FIELD(wc, recirc_id);
1700 }
330de069
JR
1701 if (!flow->dp_hash) {
1702 WC_UNMASK_FIELD(wc, dp_hash);
1703 }
07659514
JS
1704 if (!flow->ct_state) {
1705 WC_UNMASK_FIELD(wc, ct_state);
1706 }
1707 if (!flow->ct_zone) {
1708 WC_UNMASK_FIELD(wc, ct_zone);
1709 }
8e53fe8c
JS
1710 if (!flow->ct_mark) {
1711 WC_UNMASK_FIELD(wc, ct_mark);
1712 }
2ff8484b 1713 if (ovs_u128_is_zero(flow->ct_label)) {
9daf2348
JS
1714 WC_UNMASK_FIELD(wc, ct_label);
1715 }
daf4d3c1
JR
1716 if (!is_ct_valid(flow, &match.wc, NULL) || !flow->ct_nw_proto) {
1717 WC_UNMASK_FIELD(wc, ct_nw_proto);
1718 WC_UNMASK_FIELD(wc, ct_tp_src);
1719 WC_UNMASK_FIELD(wc, ct_tp_dst);
1720 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1721 WC_UNMASK_FIELD(wc, ct_nw_src);
1722 WC_UNMASK_FIELD(wc, ct_nw_dst);
1723 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1724 WC_UNMASK_FIELD(wc, ct_ipv6_src);
1725 WC_UNMASK_FIELD(wc, ct_ipv6_dst);
1726 }
1727 }
78c9486d
JR
1728 for (int i = 0; i < FLOW_N_REGS; i++) {
1729 if (!flow->regs[i]) {
1730 WC_UNMASK_FIELD(wc, regs[i]);
1731 }
1732 }
1733 if (!flow->metadata) {
1734 WC_UNMASK_FIELD(wc, metadata);
1735 }
1736
50f96b10 1737 match_format(&match, port_map, ds, OFP_DEFAULT_PRIORITY);
064af421
BP
1738}
1739
1740void
50f96b10
BP
1741flow_print(FILE *stream,
1742 const struct flow *flow, const struct ofputil_port_map *port_map)
064af421 1743{
50f96b10 1744 char *s = flow_to_string(flow, port_map);
064af421
BP
1745 fputs(s, stream);
1746 free(s);
1747}
54363004
BP
1748\f
1749/* flow_wildcards functions. */
1750
d8ae4d67 1751/* Initializes 'wc' as a set of wildcards that matches every packet. */
54363004 1752void
d8ae4d67 1753flow_wildcards_init_catchall(struct flow_wildcards *wc)
54363004 1754{
659c2346 1755 memset(&wc->masks, 0, sizeof wc->masks);
54363004
BP
1756}
1757
78c9486d
JR
1758/* Converts a flow into flow wildcards. It sets the wildcard masks based on
1759 * the packet headers extracted to 'flow'. It will not set the mask for fields
1760 * that do not make sense for the packet type. OpenFlow-only metadata is
1761 * wildcarded, but other metadata is unconditionally exact-matched. */
f0fb825a
EG
1762void
1763flow_wildcards_init_for_packet(struct flow_wildcards *wc,
1764 const struct flow *flow)
78c9486d 1765{
cb1145d1
ZB
1766 ovs_be16 dl_type = OVS_BE16_MAX;
1767
78c9486d
JR
1768 memset(&wc->masks, 0x0, sizeof wc->masks);
1769
0de8783a 1770 /* Update this function whenever struct flow changes. */
7dc18ae9 1771 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 41);
0de8783a 1772
ffe4c74f 1773 if (flow_tnl_dst_is_set(&flow->tunnel)) {
78c9486d
JR
1774 if (flow->tunnel.flags & FLOW_TNL_F_KEY) {
1775 WC_MASK_FIELD(wc, tunnel.tun_id);
1776 }
1777 WC_MASK_FIELD(wc, tunnel.ip_src);
1778 WC_MASK_FIELD(wc, tunnel.ip_dst);
ffe4c74f
JB
1779 WC_MASK_FIELD(wc, tunnel.ipv6_src);
1780 WC_MASK_FIELD(wc, tunnel.ipv6_dst);
78c9486d
JR
1781 WC_MASK_FIELD(wc, tunnel.flags);
1782 WC_MASK_FIELD(wc, tunnel.ip_tos);
1783 WC_MASK_FIELD(wc, tunnel.ip_ttl);
1784 WC_MASK_FIELD(wc, tunnel.tp_src);
1785 WC_MASK_FIELD(wc, tunnel.tp_dst);
ac6073e3
MC
1786 WC_MASK_FIELD(wc, tunnel.gbp_id);
1787 WC_MASK_FIELD(wc, tunnel.gbp_flags);
7dc18ae9
WT
1788 WC_MASK_FIELD(wc, tunnel.erspan_ver);
1789 WC_MASK_FIELD(wc, tunnel.erspan_idx);
1790 WC_MASK_FIELD(wc, tunnel.erspan_dir);
1791 WC_MASK_FIELD(wc, tunnel.erspan_hwid);
9558d2a5 1792
6728d578
JG
1793 if (!(flow->tunnel.flags & FLOW_TNL_F_UDPIF)) {
1794 if (flow->tunnel.metadata.present.map) {
1795 wc->masks.tunnel.metadata.present.map =
1796 flow->tunnel.metadata.present.map;
1797 WC_MASK_FIELD(wc, tunnel.metadata.opts.u8);
8d8ab6c2 1798 WC_MASK_FIELD(wc, tunnel.metadata.tab);
6728d578
JG
1799 }
1800 } else {
1801 WC_MASK_FIELD(wc, tunnel.metadata.present.len);
1802 memset(wc->masks.tunnel.metadata.opts.gnv, 0xff,
1803 flow->tunnel.metadata.present.len);
9558d2a5 1804 }
78c9486d
JR
1805 } else if (flow->tunnel.tun_id) {
1806 WC_MASK_FIELD(wc, tunnel.tun_id);
1807 }
1808
18080541 1809 /* metadata, regs, and conj_id wildcarded. */
78c9486d
JR
1810
1811 WC_MASK_FIELD(wc, skb_priority);
1812 WC_MASK_FIELD(wc, pkt_mark);
07659514
JS
1813 WC_MASK_FIELD(wc, ct_state);
1814 WC_MASK_FIELD(wc, ct_zone);
8e53fe8c 1815 WC_MASK_FIELD(wc, ct_mark);
9daf2348 1816 WC_MASK_FIELD(wc, ct_label);
78c9486d
JR
1817 WC_MASK_FIELD(wc, recirc_id);
1818 WC_MASK_FIELD(wc, dp_hash);
1819 WC_MASK_FIELD(wc, in_port);
1820
c61f3870
BP
1821 /* actset_output wildcarded. */
1822
3d4b2e6e 1823 WC_MASK_FIELD(wc, packet_type);
cb1145d1
ZB
1824 if (flow->packet_type == htonl(PT_ETH)) {
1825 WC_MASK_FIELD(wc, dl_dst);
1826 WC_MASK_FIELD(wc, dl_src);
1827 WC_MASK_FIELD(wc, dl_type);
1828 /* No need to set mask of inner VLANs that don't exist. */
1829 for (int i = 0; i < FLOW_MAX_VLAN_HEADERS; i++) {
1830 /* Always show the first zero VLAN. */
1831 WC_MASK_FIELD(wc, vlans[i]);
1832 if (flow->vlans[i].tci == htons(0)) {
1833 break;
1834 }
f0fb825a 1835 }
cb1145d1
ZB
1836 dl_type = flow->dl_type;
1837 } else {
1838 dl_type = pt_ns_type_be(flow->packet_type);
f0fb825a 1839 }
78c9486d 1840
cb1145d1 1841 if (dl_type == htons(ETH_TYPE_IP)) {
78c9486d
JR
1842 WC_MASK_FIELD(wc, nw_src);
1843 WC_MASK_FIELD(wc, nw_dst);
daf4d3c1
JR
1844 WC_MASK_FIELD(wc, ct_nw_src);
1845 WC_MASK_FIELD(wc, ct_nw_dst);
cb1145d1 1846 } else if (dl_type == htons(ETH_TYPE_IPV6)) {
78c9486d
JR
1847 WC_MASK_FIELD(wc, ipv6_src);
1848 WC_MASK_FIELD(wc, ipv6_dst);
1849 WC_MASK_FIELD(wc, ipv6_label);
daf4d3c1
JR
1850 if (is_nd(flow, wc)) {
1851 WC_MASK_FIELD(wc, arp_sha);
1852 WC_MASK_FIELD(wc, arp_tha);
1853 WC_MASK_FIELD(wc, nd_target);
1854 } else {
1855 WC_MASK_FIELD(wc, ct_ipv6_src);
1856 WC_MASK_FIELD(wc, ct_ipv6_dst);
1857 }
cb1145d1
ZB
1858 } else if (dl_type == htons(ETH_TYPE_ARP) ||
1859 dl_type == htons(ETH_TYPE_RARP)) {
78c9486d
JR
1860 WC_MASK_FIELD(wc, nw_src);
1861 WC_MASK_FIELD(wc, nw_dst);
1862 WC_MASK_FIELD(wc, nw_proto);
1863 WC_MASK_FIELD(wc, arp_sha);
1864 WC_MASK_FIELD(wc, arp_tha);
1865 return;
cb1145d1 1866 } else if (eth_type_mpls(dl_type)) {
78c9486d
JR
1867 for (int i = 0; i < FLOW_MAX_MPLS_LABELS; i++) {
1868 WC_MASK_FIELD(wc, mpls_lse[i]);
1869 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
1870 break;
1871 }
1872 }
1873 return;
3d2fbd70
JS
1874 } else if (flow->dl_type == htons(ETH_TYPE_NSH)) {
1875 WC_MASK_FIELD(wc, nsh.flags);
17553f27 1876 WC_MASK_FIELD(wc, nsh.ttl);
3d2fbd70
JS
1877 WC_MASK_FIELD(wc, nsh.mdtype);
1878 WC_MASK_FIELD(wc, nsh.np);
17553f27 1879 WC_MASK_FIELD(wc, nsh.path_hdr);
f59cb331 1880 WC_MASK_FIELD(wc, nsh.context);
78c9486d
JR
1881 } else {
1882 return; /* Unknown ethertype. */
1883 }
1884
1885 /* IPv4 or IPv6. */
1886 WC_MASK_FIELD(wc, nw_frag);
1887 WC_MASK_FIELD(wc, nw_tos);
1888 WC_MASK_FIELD(wc, nw_ttl);
1889 WC_MASK_FIELD(wc, nw_proto);
daf4d3c1
JR
1890 WC_MASK_FIELD(wc, ct_nw_proto);
1891 WC_MASK_FIELD(wc, ct_tp_src);
1892 WC_MASK_FIELD(wc, ct_tp_dst);
78c9486d
JR
1893
1894 /* No transport layer header in later fragments. */
1895 if (!(flow->nw_frag & FLOW_NW_FRAG_LATER) &&
1896 (flow->nw_proto == IPPROTO_ICMP ||
1897 flow->nw_proto == IPPROTO_ICMPV6 ||
1898 flow->nw_proto == IPPROTO_TCP ||
1899 flow->nw_proto == IPPROTO_UDP ||
1900 flow->nw_proto == IPPROTO_SCTP ||
1901 flow->nw_proto == IPPROTO_IGMP)) {
1902 WC_MASK_FIELD(wc, tp_src);
1903 WC_MASK_FIELD(wc, tp_dst);
1904
1905 if (flow->nw_proto == IPPROTO_TCP) {
1906 WC_MASK_FIELD(wc, tcp_flags);
78c9486d
JR
1907 } else if (flow->nw_proto == IPPROTO_IGMP) {
1908 WC_MASK_FIELD(wc, igmp_group_ip4);
1909 }
1910 }
1911}
1912
0de8783a
JR
1913/* Return a map of possible fields for a packet of the same type as 'flow'.
1914 * Including extra bits in the returned mask is not wrong, it is just less
1915 * optimal.
1916 *
1917 * This is a less precise version of flow_wildcards_init_for_packet() above. */
361d808d 1918void
5fcff47b 1919flow_wc_map(const struct flow *flow, struct flowmap *map)
0de8783a
JR
1920{
1921 /* Update this function whenever struct flow changes. */
7dc18ae9 1922 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 41);
0de8783a 1923
5fcff47b
JR
1924 flowmap_init(map);
1925
ffe4c74f 1926 if (flow_tnl_dst_is_set(&flow->tunnel)) {
5fcff47b 1927 FLOWMAP_SET__(map, tunnel, offsetof(struct flow_tnl, metadata));
6728d578
JG
1928 if (!(flow->tunnel.flags & FLOW_TNL_F_UDPIF)) {
1929 if (flow->tunnel.metadata.present.map) {
5fcff47b 1930 FLOWMAP_SET(map, tunnel.metadata);
6728d578
JG
1931 }
1932 } else {
5fcff47b
JR
1933 FLOWMAP_SET(map, tunnel.metadata.present.len);
1934 FLOWMAP_SET__(map, tunnel.metadata.opts.gnv,
1935 flow->tunnel.metadata.present.len);
361d808d
JR
1936 }
1937 }
0de8783a
JR
1938
1939 /* Metadata fields that can appear on packet input. */
5fcff47b
JR
1940 FLOWMAP_SET(map, skb_priority);
1941 FLOWMAP_SET(map, pkt_mark);
1942 FLOWMAP_SET(map, recirc_id);
1943 FLOWMAP_SET(map, dp_hash);
1944 FLOWMAP_SET(map, in_port);
1945 FLOWMAP_SET(map, dl_dst);
1946 FLOWMAP_SET(map, dl_src);
1947 FLOWMAP_SET(map, dl_type);
f0fb825a 1948 FLOWMAP_SET(map, vlans);
07659514
JS
1949 FLOWMAP_SET(map, ct_state);
1950 FLOWMAP_SET(map, ct_zone);
8e53fe8c 1951 FLOWMAP_SET(map, ct_mark);
9daf2348 1952 FLOWMAP_SET(map, ct_label);
2482b0b0 1953 FLOWMAP_SET(map, packet_type);
0de8783a
JR
1954
1955 /* Ethertype-dependent fields. */
1956 if (OVS_LIKELY(flow->dl_type == htons(ETH_TYPE_IP))) {
5fcff47b
JR
1957 FLOWMAP_SET(map, nw_src);
1958 FLOWMAP_SET(map, nw_dst);
1959 FLOWMAP_SET(map, nw_proto);
1960 FLOWMAP_SET(map, nw_frag);
1961 FLOWMAP_SET(map, nw_tos);
1962 FLOWMAP_SET(map, nw_ttl);
8ecb3068
DDP
1963 FLOWMAP_SET(map, tp_src);
1964 FLOWMAP_SET(map, tp_dst);
daf4d3c1
JR
1965 FLOWMAP_SET(map, ct_nw_proto);
1966 FLOWMAP_SET(map, ct_nw_src);
1967 FLOWMAP_SET(map, ct_nw_dst);
1968 FLOWMAP_SET(map, ct_tp_src);
1969 FLOWMAP_SET(map, ct_tp_dst);
5fcff47b 1970
0de8783a 1971 if (OVS_UNLIKELY(flow->nw_proto == IPPROTO_IGMP)) {
5fcff47b 1972 FLOWMAP_SET(map, igmp_group_ip4);
0de8783a 1973 } else {
5fcff47b 1974 FLOWMAP_SET(map, tcp_flags);
0de8783a
JR
1975 }
1976 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
5fcff47b
JR
1977 FLOWMAP_SET(map, ipv6_src);
1978 FLOWMAP_SET(map, ipv6_dst);
1979 FLOWMAP_SET(map, ipv6_label);
1980 FLOWMAP_SET(map, nw_proto);
1981 FLOWMAP_SET(map, nw_frag);
1982 FLOWMAP_SET(map, nw_tos);
1983 FLOWMAP_SET(map, nw_ttl);
8ecb3068
DDP
1984 FLOWMAP_SET(map, tp_src);
1985 FLOWMAP_SET(map, tp_dst);
5fcff47b 1986
daf4d3c1 1987 if (OVS_UNLIKELY(is_nd(flow, NULL))) {
5fcff47b
JR
1988 FLOWMAP_SET(map, nd_target);
1989 FLOWMAP_SET(map, arp_sha);
1990 FLOWMAP_SET(map, arp_tha);
9b2b8497
VDA
1991 FLOWMAP_SET(map, tcp_flags);
1992 FLOWMAP_SET(map, igmp_group_ip4);
0de8783a 1993 } else {
daf4d3c1
JR
1994 FLOWMAP_SET(map, ct_nw_proto);
1995 FLOWMAP_SET(map, ct_ipv6_src);
1996 FLOWMAP_SET(map, ct_ipv6_dst);
1997 FLOWMAP_SET(map, ct_tp_src);
1998 FLOWMAP_SET(map, ct_tp_dst);
5fcff47b 1999 FLOWMAP_SET(map, tcp_flags);
0de8783a
JR
2000 }
2001 } else if (eth_type_mpls(flow->dl_type)) {
5fcff47b 2002 FLOWMAP_SET(map, mpls_lse);
0de8783a
JR
2003 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
2004 flow->dl_type == htons(ETH_TYPE_RARP)) {
5fcff47b
JR
2005 FLOWMAP_SET(map, nw_src);
2006 FLOWMAP_SET(map, nw_dst);
2007 FLOWMAP_SET(map, nw_proto);
2008 FLOWMAP_SET(map, arp_sha);
2009 FLOWMAP_SET(map, arp_tha);
3d2fbd70
JS
2010 } else if (flow->dl_type == htons(ETH_TYPE_NSH)) {
2011 FLOWMAP_SET(map, nsh.flags);
2012 FLOWMAP_SET(map, nsh.mdtype);
2013 FLOWMAP_SET(map, nsh.np);
17553f27 2014 FLOWMAP_SET(map, nsh.path_hdr);
f59cb331 2015 FLOWMAP_SET(map, nsh.context);
0de8783a 2016 }
0de8783a
JR
2017}
2018
c11c6faa
AZ
2019/* Clear the metadata and register wildcard masks. They are not packet
2020 * header fields. */
2021void
2022flow_wildcards_clear_non_packet_fields(struct flow_wildcards *wc)
2023{
0de8783a 2024 /* Update this function whenever struct flow changes. */
7dc18ae9 2025 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 41);
0de8783a 2026
c11c6faa
AZ
2027 memset(&wc->masks.metadata, 0, sizeof wc->masks.metadata);
2028 memset(&wc->masks.regs, 0, sizeof wc->masks.regs);
c61f3870 2029 wc->masks.actset_output = 0;
18080541 2030 wc->masks.conj_id = 0;
c11c6faa
AZ
2031}
2032
ecf1e7ac
BP
2033/* Returns true if 'wc' matches every packet, false if 'wc' fixes any bits or
2034 * fields. */
2035bool
2036flow_wildcards_is_catchall(const struct flow_wildcards *wc)
2037{
d70e8c28 2038 const uint64_t *wc_u64 = (const uint64_t *) &wc->masks;
659c2346 2039 size_t i;
ecf1e7ac 2040
d70e8c28
JR
2041 for (i = 0; i < FLOW_U64S; i++) {
2042 if (wc_u64[i]) {
ecf1e7ac
BP
2043 return false;
2044 }
2045 }
ecf1e7ac
BP
2046 return true;
2047}
2048
368eefac
EJ
2049/* Sets 'dst' as the bitwise AND of wildcards in 'src1' and 'src2'.
2050 * That is, a bit or a field is wildcarded in 'dst' if it is wildcarded
2051 * in 'src1' or 'src2' or both. */
b5d97350 2052void
368eefac
EJ
2053flow_wildcards_and(struct flow_wildcards *dst,
2054 const struct flow_wildcards *src1,
2055 const struct flow_wildcards *src2)
b5d97350 2056{
d70e8c28
JR
2057 uint64_t *dst_u64 = (uint64_t *) &dst->masks;
2058 const uint64_t *src1_u64 = (const uint64_t *) &src1->masks;
2059 const uint64_t *src2_u64 = (const uint64_t *) &src2->masks;
659c2346 2060 size_t i;
a79c50f3 2061
d70e8c28
JR
2062 for (i = 0; i < FLOW_U64S; i++) {
2063 dst_u64[i] = src1_u64[i] & src2_u64[i];
26720e24 2064 }
b5d97350
BP
2065}
2066
368eefac
EJ
2067/* Sets 'dst' as the bitwise OR of wildcards in 'src1' and 'src2'. That
2068 * is, a bit or a field is wildcarded in 'dst' if it is neither
2069 * wildcarded in 'src1' nor 'src2'. */
2070void
2071flow_wildcards_or(struct flow_wildcards *dst,
2072 const struct flow_wildcards *src1,
2073 const struct flow_wildcards *src2)
2074{
d70e8c28
JR
2075 uint64_t *dst_u64 = (uint64_t *) &dst->masks;
2076 const uint64_t *src1_u64 = (const uint64_t *) &src1->masks;
2077 const uint64_t *src2_u64 = (const uint64_t *) &src2->masks;
368eefac
EJ
2078 size_t i;
2079
d70e8c28
JR
2080 for (i = 0; i < FLOW_U64S; i++) {
2081 dst_u64[i] = src1_u64[i] | src2_u64[i];
368eefac
EJ
2082 }
2083}
2084
b5d97350
BP
2085/* Returns a hash of the wildcards in 'wc'. */
2086uint32_t
1006cda6 2087flow_wildcards_hash(const struct flow_wildcards *wc, uint32_t basis)
b5d97350 2088{
ac31c5af 2089 return flow_hash(&wc->masks, basis);
b5d97350
BP
2090}
2091
2092/* Returns true if 'a' and 'b' represent the same wildcards, false if they are
2093 * different. */
2094bool
2095flow_wildcards_equal(const struct flow_wildcards *a,
2096 const struct flow_wildcards *b)
2097{
659c2346 2098 return flow_equal(&a->masks, &b->masks);
b5d97350
BP
2099}
2100
2101/* Returns true if at least one bit or field is wildcarded in 'a' but not in
2102 * 'b', false otherwise. */
2103bool
2104flow_wildcards_has_extra(const struct flow_wildcards *a,
2105 const struct flow_wildcards *b)
2106{
d70e8c28
JR
2107 const uint64_t *a_u64 = (const uint64_t *) &a->masks;
2108 const uint64_t *b_u64 = (const uint64_t *) &b->masks;
659c2346 2109 size_t i;
a79c50f3 2110
d70e8c28
JR
2111 for (i = 0; i < FLOW_U64S; i++) {
2112 if ((a_u64[i] & b_u64[i]) != b_u64[i]) {
b6c9e612
BP
2113 return true;
2114 }
2115 }
659c2346
BP
2116 return false;
2117}
b6c9e612 2118
659c2346
BP
2119/* Returns true if 'a' and 'b' are equal, except that 0-bits (wildcarded bits)
2120 * in 'wc' do not need to be equal in 'a' and 'b'. */
2121bool
2122flow_equal_except(const struct flow *a, const struct flow *b,
2123 const struct flow_wildcards *wc)
2124{
d70e8c28
JR
2125 const uint64_t *a_u64 = (const uint64_t *) a;
2126 const uint64_t *b_u64 = (const uint64_t *) b;
2127 const uint64_t *wc_u64 = (const uint64_t *) &wc->masks;
659c2346 2128 size_t i;
d31f1109 2129
d70e8c28
JR
2130 for (i = 0; i < FLOW_U64S; i++) {
2131 if ((a_u64[i] ^ b_u64[i]) & wc_u64[i]) {
659c2346
BP
2132 return false;
2133 }
47284b1f 2134 }
659c2346 2135 return true;
b5d97350
BP
2136}
2137
b6c9e612
BP
2138/* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
2139 * (A 0-bit indicates a wildcard bit.) */
2140void
2141flow_wildcards_set_reg_mask(struct flow_wildcards *wc, int idx, uint32_t mask)
2142{
26720e24 2143 wc->masks.regs[idx] = mask;
b6c9e612 2144}
ff55ea1f 2145
79fe0f46
BP
2146/* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
2147 * (A 0-bit indicates a wildcard bit.) */
2148void
2149flow_wildcards_set_xreg_mask(struct flow_wildcards *wc, int idx, uint64_t mask)
2150{
2151 flow_set_xreg(&wc->masks, idx, mask);
2152}
2153
b23ada8e
JP
2154/* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
2155 * (A 0-bit indicates a wildcard bit.) */
2156void
2157flow_wildcards_set_xxreg_mask(struct flow_wildcards *wc, int idx,
2158 ovs_u128 mask)
2159{
2160 flow_set_xxreg(&wc->masks, idx, mask);
2161}
2162
28a560d9
JR
2163/* Calculates the 5-tuple hash from the given miniflow.
2164 * This returns the same value as flow_hash_5tuple for the corresponding
2165 * flow. */
4f150744
JR
2166uint32_t
2167miniflow_hash_5tuple(const struct miniflow *flow, uint32_t basis)
2168{
7dc18ae9 2169 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 41);
28a560d9 2170 uint32_t hash = basis;
4f150744 2171
28a560d9
JR
2172 if (flow) {
2173 ovs_be16 dl_type = MINIFLOW_GET_BE16(flow, dl_type);
362ad4ba 2174 uint8_t nw_proto;
28a560d9 2175
28a560d9 2176 if (dl_type == htons(ETH_TYPE_IPV6)) {
5fcff47b 2177 struct flowmap map = FLOWMAP_EMPTY_INITIALIZER;
d70e8c28 2178 uint64_t value;
4f150744 2179
5fcff47b
JR
2180 FLOWMAP_SET(&map, ipv6_src);
2181 FLOWMAP_SET(&map, ipv6_dst);
2182
2183 MINIFLOW_FOR_EACH_IN_FLOWMAP(value, flow, map) {
d70e8c28 2184 hash = hash_add64(hash, value);
28a560d9 2185 }
362ad4ba
DDP
2186 } else if (dl_type == htons(ETH_TYPE_IP)
2187 || dl_type == htons(ETH_TYPE_ARP)) {
d70e8c28
JR
2188 hash = hash_add(hash, MINIFLOW_GET_U32(flow, nw_src));
2189 hash = hash_add(hash, MINIFLOW_GET_U32(flow, nw_dst));
362ad4ba
DDP
2190 } else {
2191 goto out;
28a560d9 2192 }
362ad4ba
DDP
2193
2194 nw_proto = MINIFLOW_GET_U8(flow, nw_proto);
2195 hash = hash_add(hash, nw_proto);
2196 if (nw_proto != IPPROTO_TCP && nw_proto != IPPROTO_UDP
2197 && nw_proto != IPPROTO_SCTP && nw_proto != IPPROTO_ICMP
2198 && nw_proto != IPPROTO_ICMPV6) {
2199 goto out;
2200 }
2201
d70e8c28 2202 /* Add both ports at once. */
f825fdd4 2203 hash = hash_add(hash, (OVS_FORCE uint32_t) miniflow_get_ports(flow));
28a560d9 2204 }
362ad4ba
DDP
2205out:
2206 return hash_finish(hash, 42);
4f150744
JR
2207}
2208
268eca11
BP
2209ASSERT_SEQUENTIAL_SAME_WORD(tp_src, tp_dst);
2210ASSERT_SEQUENTIAL(ipv6_src, ipv6_dst);
4f150744 2211
63be20be
AW
2212/* Calculates the 5-tuple hash from the given flow. */
2213uint32_t
2214flow_hash_5tuple(const struct flow *flow, uint32_t basis)
2215{
7dc18ae9 2216 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 41);
28a560d9 2217 uint32_t hash = basis;
63be20be 2218
28a560d9 2219 if (flow) {
28a560d9
JR
2220
2221 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
d70e8c28
JR
2222 const uint64_t *flow_u64 = (const uint64_t *)flow;
2223 int ofs = offsetof(struct flow, ipv6_src) / 8;
2224 int end = ofs + 2 * sizeof flow->ipv6_src / 8;
63be20be 2225
d70e8c28
JR
2226 for (;ofs < end; ofs++) {
2227 hash = hash_add64(hash, flow_u64[ofs]);
28a560d9 2228 }
362ad4ba
DDP
2229 } else if (flow->dl_type == htons(ETH_TYPE_IP)
2230 || flow->dl_type == htons(ETH_TYPE_ARP)) {
33c6a1b9
JR
2231 hash = hash_add(hash, (OVS_FORCE uint32_t) flow->nw_src);
2232 hash = hash_add(hash, (OVS_FORCE uint32_t) flow->nw_dst);
362ad4ba
DDP
2233 } else {
2234 goto out;
28a560d9 2235 }
362ad4ba
DDP
2236
2237 hash = hash_add(hash, flow->nw_proto);
2238 if (flow->nw_proto != IPPROTO_TCP && flow->nw_proto != IPPROTO_UDP
2239 && flow->nw_proto != IPPROTO_SCTP && flow->nw_proto != IPPROTO_ICMP
2240 && flow->nw_proto != IPPROTO_ICMPV6) {
2241 goto out;
2242 }
2243
d70e8c28
JR
2244 /* Add both ports at once. */
2245 hash = hash_add(hash,
2246 ((const uint32_t *)flow)[offsetof(struct flow, tp_src)
2247 / sizeof(uint32_t)]);
28a560d9 2248 }
362ad4ba
DDP
2249out:
2250 return hash_finish(hash, 42); /* Arbitrary number. */
63be20be
AW
2251}
2252
ff55ea1f
EJ
2253/* Hashes 'flow' based on its L2 through L4 protocol information. */
2254uint32_t
2255flow_hash_symmetric_l4(const struct flow *flow, uint32_t basis)
2256{
2257 struct {
d31f1109
JP
2258 union {
2259 ovs_be32 ipv4_addr;
2260 struct in6_addr ipv6_addr;
2261 };
ff55ea1f
EJ
2262 ovs_be16 eth_type;
2263 ovs_be16 vlan_tci;
5b909cbb 2264 ovs_be16 tp_port;
74ff3298 2265 struct eth_addr eth_addr;
ff55ea1f
EJ
2266 uint8_t ip_proto;
2267 } fields;
2268
2269 int i;
2270
2271 memset(&fields, 0, sizeof fields);
74ff3298
JR
2272 for (i = 0; i < ARRAY_SIZE(fields.eth_addr.be16); i++) {
2273 fields.eth_addr.be16[i] = flow->dl_src.be16[i] ^ flow->dl_dst.be16[i];
ff55ea1f 2274 }
f0fb825a
EG
2275 for (i = 0; i < FLOW_MAX_VLAN_HEADERS; i++) {
2276 fields.vlan_tci ^= flow->vlans[i].tci & htons(VLAN_VID_MASK);
2277 }
ff55ea1f 2278 fields.eth_type = flow->dl_type;
3e3eda95
EJ
2279
2280 /* UDP source and destination port are not taken into account because they
2281 * will not necessarily be symmetric in a bidirectional flow. */
ff55ea1f 2282 if (fields.eth_type == htons(ETH_TYPE_IP)) {
d31f1109
JP
2283 fields.ipv4_addr = flow->nw_src ^ flow->nw_dst;
2284 fields.ip_proto = flow->nw_proto;
c6bcb685 2285 if (fields.ip_proto == IPPROTO_TCP || fields.ip_proto == IPPROTO_SCTP) {
5b909cbb 2286 fields.tp_port = flow->tp_src ^ flow->tp_dst;
d31f1109
JP
2287 }
2288 } else if (fields.eth_type == htons(ETH_TYPE_IPV6)) {
2289 const uint8_t *a = &flow->ipv6_src.s6_addr[0];
2290 const uint8_t *b = &flow->ipv6_dst.s6_addr[0];
2291 uint8_t *ipv6_addr = &fields.ipv6_addr.s6_addr[0];
2292
2293 for (i=0; i<16; i++) {
2294 ipv6_addr[i] = a[i] ^ b[i];
2295 }
ff55ea1f 2296 fields.ip_proto = flow->nw_proto;
c6bcb685 2297 if (fields.ip_proto == IPPROTO_TCP || fields.ip_proto == IPPROTO_SCTP) {
5b909cbb 2298 fields.tp_port = flow->tp_src ^ flow->tp_dst;
ff55ea1f 2299 }
ff55ea1f 2300 }
c49d1dd1 2301 return jhash_bytes(&fields, sizeof fields, basis);
ff55ea1f 2302}
520e9a2a 2303
6a0b0d3b
JS
2304/* Symmetrically Hashes non-IP 'flow' based on its L2 headers. */
2305uint32_t
2306flow_hash_symmetric_l2(const struct flow *flow, uint32_t basis)
2307{
2308 union {
2309 struct {
2310 ovs_be16 eth_type;
2311 ovs_be16 vlan_tci;
2312 struct eth_addr eth_addr;
2313 ovs_be16 pad;
2314 };
2315 uint32_t word[3];
2316 } fields;
2317
2318 uint32_t hash = basis;
2319 int i;
2320
2321 if (flow->packet_type != htonl(PT_ETH)) {
2322 /* Cannot hash non-Ethernet flows */
2323 return 0;
2324 }
2325
2326 for (i = 0; i < ARRAY_SIZE(fields.eth_addr.be16); i++) {
2327 fields.eth_addr.be16[i] =
2328 flow->dl_src.be16[i] ^ flow->dl_dst.be16[i];
2329 }
2330 fields.vlan_tci = 0;
2331 for (i = 0; i < FLOW_MAX_VLAN_HEADERS; i++) {
2332 fields.vlan_tci ^= flow->vlans[i].tci & htons(VLAN_VID_MASK);
2333 }
2334 fields.eth_type = flow->dl_type;
2335 fields.pad = 0;
2336
2337 hash = hash_add(hash, fields.word[0]);
2338 hash = hash_add(hash, fields.word[1]);
2339 hash = hash_add(hash, fields.word[2]);
2340 return hash_finish(hash, basis);
2341}
2342
4249b547
JB
2343/* Hashes 'flow' based on its L3 through L4 protocol information */
2344uint32_t
2345flow_hash_symmetric_l3l4(const struct flow *flow, uint32_t basis,
2346 bool inc_udp_ports)
2347{
2348 uint32_t hash = basis;
2349
2350 /* UDP source and destination port are also taken into account. */
2351 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2352 hash = hash_add(hash,
2353 (OVS_FORCE uint32_t) (flow->nw_src ^ flow->nw_dst));
2354 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2355 /* IPv6 addresses are 64-bit aligned inside struct flow. */
2356 const uint64_t *a = ALIGNED_CAST(uint64_t *, flow->ipv6_src.s6_addr);
2357 const uint64_t *b = ALIGNED_CAST(uint64_t *, flow->ipv6_dst.s6_addr);
2358
caaabd19 2359 for (int i = 0; i < sizeof flow->ipv6_src / sizeof *a; i++) {
4249b547
JB
2360 hash = hash_add64(hash, a[i] ^ b[i]);
2361 }
2362 } else {
6a0b0d3b
JS
2363 /* Revert to hashing L2 headers */
2364 return flow_hash_symmetric_l2(flow, basis);
4249b547 2365 }
4249b547 2366 hash = hash_add(hash, flow->nw_proto);
fb23ed47
VBJNU
2367 if (!(flow->nw_frag & FLOW_NW_FRAG_MASK)
2368 && (flow->nw_proto == IPPROTO_TCP || flow->nw_proto == IPPROTO_SCTP ||
2369 (inc_udp_ports && flow->nw_proto == IPPROTO_UDP))) {
4249b547
JB
2370 hash = hash_add(hash,
2371 (OVS_FORCE uint16_t) (flow->tp_src ^ flow->tp_dst));
2372 }
2373
2374 return hash_finish(hash, basis);
2375}
2376
84ddf96c
MX
2377/* Hashes 'flow' based on its nw_dst and nw_src for multipath. */
2378uint32_t
2379flow_hash_symmetric_l3(const struct flow *flow, uint32_t basis)
2380{
2381 struct {
2382 union {
2383 ovs_be32 ipv4_addr;
2384 struct in6_addr ipv6_addr;
2385 };
2386 ovs_be16 eth_type;
2387 } fields;
2388
2389 int i;
2390
2391 memset(&fields, 0, sizeof fields);
2392 fields.eth_type = flow->dl_type;
2393
2394 if (fields.eth_type == htons(ETH_TYPE_IP)) {
2395 fields.ipv4_addr = flow->nw_src ^ flow->nw_dst;
2396 } else if (fields.eth_type == htons(ETH_TYPE_IPV6)) {
2397 const uint8_t *a = &flow->ipv6_src.s6_addr[0];
2398 const uint8_t *b = &flow->ipv6_dst.s6_addr[0];
2399 uint8_t *ipv6_addr = &fields.ipv6_addr.s6_addr[0];
2400
2401 for (i = 0; i < 16; i++) {
2402 ipv6_addr[i] = a[i] ^ b[i];
2403 }
2404 }
2405 return jhash_bytes(&fields, sizeof fields, basis);
2406}
2407
94639963
JR
2408/* Initialize a flow with random fields that matter for nx_hash_fields. */
2409void
2410flow_random_hash_fields(struct flow *flow)
2411{
2412 uint16_t rnd = random_uint16();
f0fb825a 2413 int i;
94639963
JR
2414
2415 /* Initialize to all zeros. */
2416 memset(flow, 0, sizeof *flow);
2417
74ff3298
JR
2418 eth_addr_random(&flow->dl_src);
2419 eth_addr_random(&flow->dl_dst);
94639963 2420
f0fb825a
EG
2421 for (i = 0; i < FLOW_MAX_VLAN_HEADERS; i++) {
2422 uint16_t vlan = random_uint16() & VLAN_VID_MASK;
2423 flow->vlans[i].tpid = htons(ETH_TYPE_VLAN_8021Q);
2424 flow->vlans[i].tci = htons(vlan | VLAN_CFI);
2425 }
94639963
JR
2426
2427 /* Make most of the random flows IPv4, some IPv6, and rest random. */
2428 flow->dl_type = rnd < 0x8000 ? htons(ETH_TYPE_IP) :
2429 rnd < 0xc000 ? htons(ETH_TYPE_IPV6) : (OVS_FORCE ovs_be16)rnd;
2430
2431 if (dl_type_is_ip_any(flow->dl_type)) {
2432 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2433 flow->nw_src = (OVS_FORCE ovs_be32)random_uint32();
2434 flow->nw_dst = (OVS_FORCE ovs_be32)random_uint32();
2435 } else {
2436 random_bytes(&flow->ipv6_src, sizeof flow->ipv6_src);
2437 random_bytes(&flow->ipv6_dst, sizeof flow->ipv6_dst);
2438 }
2439 /* Make most of IP flows TCP, some UDP or SCTP, and rest random. */
2440 rnd = random_uint16();
2441 flow->nw_proto = rnd < 0x8000 ? IPPROTO_TCP :
2442 rnd < 0xc000 ? IPPROTO_UDP :
2443 rnd < 0xd000 ? IPPROTO_SCTP : (uint8_t)rnd;
2444 if (flow->nw_proto == IPPROTO_TCP ||
2445 flow->nw_proto == IPPROTO_UDP ||
2446 flow->nw_proto == IPPROTO_SCTP) {
2447 flow->tp_src = (OVS_FORCE ovs_be16)random_uint16();
2448 flow->tp_dst = (OVS_FORCE ovs_be16)random_uint16();
2449 }
2450 }
2451}
2452
bcd2633a
JP
2453/* Masks the fields in 'wc' that are used by the flow hash 'fields'. */
2454void
6cdd5145
JP
2455flow_mask_hash_fields(const struct flow *flow, struct flow_wildcards *wc,
2456 enum nx_hash_fields fields)
bcd2633a 2457{
f0fb825a 2458 int i;
bcd2633a
JP
2459 switch (fields) {
2460 case NX_HASH_FIELDS_ETH_SRC:
2461 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2462 break;
2463
2464 case NX_HASH_FIELDS_SYMMETRIC_L4:
2465 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
2466 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
6cdd5145
JP
2467 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2468 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2469 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
7f8a65ca 2470 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
6cdd5145
JP
2471 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
2472 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
2473 }
2474 if (is_ip_any(flow)) {
2475 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
cbbab701
VDA
2476 /* Unwildcard port only for non-UDP packets as udp port
2477 * numbers are not used in hash calculations.
2478 */
2479 if (flow->nw_proto != IPPROTO_UDP) {
2480 flow_unwildcard_tp_ports(flow, wc);
2481 }
6cdd5145 2482 }
f0fb825a
EG
2483 for (i = 0; i < FLOW_MAX_VLAN_HEADERS; i++) {
2484 wc->masks.vlans[i].tci |= htons(VLAN_VID_MASK | VLAN_CFI);
2485 }
bcd2633a 2486 break;
4249b547 2487 case NX_HASH_FIELDS_SYMMETRIC_L3L4_UDP:
fb23ed47
VBJNU
2488 if (is_ip_any(flow) && flow->nw_proto == IPPROTO_UDP
2489 && !(flow->nw_frag & FLOW_NW_FRAG_MASK)) {
4249b547
JB
2490 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
2491 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
2492 }
73c7216a 2493 /* fall through */
4249b547
JB
2494 case NX_HASH_FIELDS_SYMMETRIC_L3L4:
2495 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2496 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2497 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2498 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2499 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
2500 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
2501 } else {
2502 break; /* non-IP flow */
2503 }
4249b547 2504 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
fb23ed47
VBJNU
2505 if ((flow->nw_proto == IPPROTO_TCP || flow->nw_proto == IPPROTO_SCTP)
2506 && !(flow->nw_frag & FLOW_NW_FRAG_MASK)) {
4249b547
JB
2507 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
2508 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
2509 }
2510 break;
2511
417cfdb6 2512 case NX_HASH_FIELDS_NW_SRC:
2513 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2514 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2515 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2516 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
2517 }
2518 break;
2519
2520 case NX_HASH_FIELDS_NW_DST:
2521 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2522 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2523 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2524 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
2525 }
2526 break;
2527
84ddf96c
MX
2528 case NX_HASH_FIELDS_SYMMETRIC_L3:
2529 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2530 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
2531 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
2532 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2533 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
2534 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
2535 }
2536 break;
2537
bcd2633a 2538 default:
428b2edd 2539 OVS_NOT_REACHED();
bcd2633a
JP
2540 }
2541}
2542
520e9a2a
EJ
2543/* Hashes the portions of 'flow' designated by 'fields'. */
2544uint32_t
2545flow_hash_fields(const struct flow *flow, enum nx_hash_fields fields,
2546 uint16_t basis)
2547{
2548 switch (fields) {
2549
2550 case NX_HASH_FIELDS_ETH_SRC:
74ff3298 2551 return jhash_bytes(&flow->dl_src, sizeof flow->dl_src, basis);
520e9a2a
EJ
2552
2553 case NX_HASH_FIELDS_SYMMETRIC_L4:
2554 return flow_hash_symmetric_l4(flow, basis);
4249b547
JB
2555
2556 case NX_HASH_FIELDS_SYMMETRIC_L3L4:
2557 return flow_hash_symmetric_l3l4(flow, basis, false);
2558
2559 case NX_HASH_FIELDS_SYMMETRIC_L3L4_UDP:
2560 return flow_hash_symmetric_l3l4(flow, basis, true);
2561
417cfdb6 2562 case NX_HASH_FIELDS_NW_SRC:
2563 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2564 return jhash_bytes(&flow->nw_src, sizeof flow->nw_src, basis);
2565 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2566 return jhash_bytes(&flow->ipv6_src, sizeof flow->ipv6_src, basis);
2567 } else {
2568 return basis;
2569 }
2570
2571 case NX_HASH_FIELDS_NW_DST:
2572 if (flow->dl_type == htons(ETH_TYPE_IP)) {
2573 return jhash_bytes(&flow->nw_dst, sizeof flow->nw_dst, basis);
2574 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2575 return jhash_bytes(&flow->ipv6_dst, sizeof flow->ipv6_dst, basis);
2576 } else {
2577 return basis;
2578 }
2579
84ddf96c
MX
2580 case NX_HASH_FIELDS_SYMMETRIC_L3:
2581 return flow_hash_symmetric_l3(flow, basis);
520e9a2a
EJ
2582 }
2583
428b2edd 2584 OVS_NOT_REACHED();
520e9a2a
EJ
2585}
2586
2587/* Returns a string representation of 'fields'. */
2588const char *
2589flow_hash_fields_to_str(enum nx_hash_fields fields)
2590{
2591 switch (fields) {
2592 case NX_HASH_FIELDS_ETH_SRC: return "eth_src";
2593 case NX_HASH_FIELDS_SYMMETRIC_L4: return "symmetric_l4";
4249b547
JB
2594 case NX_HASH_FIELDS_SYMMETRIC_L3L4: return "symmetric_l3l4";
2595 case NX_HASH_FIELDS_SYMMETRIC_L3L4_UDP: return "symmetric_l3l4+udp";
417cfdb6 2596 case NX_HASH_FIELDS_NW_SRC: return "nw_src";
2597 case NX_HASH_FIELDS_NW_DST: return "nw_dst";
84ddf96c 2598 case NX_HASH_FIELDS_SYMMETRIC_L3: return "symmetric_l3";
520e9a2a
EJ
2599 default: return "<unknown>";
2600 }
2601}
2602
2603/* Returns true if the value of 'fields' is supported. Otherwise false. */
2604bool
2605flow_hash_fields_valid(enum nx_hash_fields fields)
2606{
2607 return fields == NX_HASH_FIELDS_ETH_SRC
4249b547
JB
2608 || fields == NX_HASH_FIELDS_SYMMETRIC_L4
2609 || fields == NX_HASH_FIELDS_SYMMETRIC_L3L4
417cfdb6 2610 || fields == NX_HASH_FIELDS_SYMMETRIC_L3L4_UDP
2611 || fields == NX_HASH_FIELDS_NW_SRC
84ddf96c
MX
2612 || fields == NX_HASH_FIELDS_NW_DST
2613 || fields == NX_HASH_FIELDS_SYMMETRIC_L3;
520e9a2a 2614}
8b3b8dd1 2615
368eefac
EJ
2616/* Returns a hash value for the bits of 'flow' that are active based on
2617 * 'wc', given 'basis'. */
2618uint32_t
2619flow_hash_in_wildcards(const struct flow *flow,
2620 const struct flow_wildcards *wc, uint32_t basis)
2621{
d70e8c28
JR
2622 const uint64_t *wc_u64 = (const uint64_t *) &wc->masks;
2623 const uint64_t *flow_u64 = (const uint64_t *) flow;
368eefac
EJ
2624 uint32_t hash;
2625 size_t i;
2626
2627 hash = basis;
d70e8c28
JR
2628 for (i = 0; i < FLOW_U64S; i++) {
2629 hash = hash_add64(hash, flow_u64[i] & wc_u64[i]);
368eefac 2630 }
d70e8c28 2631 return hash_finish(hash, 8 * FLOW_U64S);
368eefac
EJ
2632}
2633
3719455c
BP
2634/* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
2635 * OpenFlow 1.0 "dl_vlan" value:
2636 *
f0fb825a 2637 * - If it is in the range 0...4095, 'flow->vlans[0].tci' is set to match
3719455c
BP
2638 * that VLAN. Any existing PCP match is unchanged (it becomes 0 if
2639 * 'flow' previously matched packets without a VLAN header).
2640 *
2641 * - If it is OFP_VLAN_NONE, 'flow->vlan_tci' is set to match a packet
2642 * without a VLAN tag.
2643 *
2644 * - Other values of 'vid' should not be used. */
2645void
2f9366be 2646flow_set_dl_vlan(struct flow *flow, ovs_be16 vid, int id)
3719455c 2647{
0c436519 2648 if (vid == htons(OFP10_VLAN_NONE)) {
2f9366be 2649 flow->vlans[id].tci = htons(0);
3719455c
BP
2650 } else {
2651 vid &= htons(VLAN_VID_MASK);
2f9366be
JL
2652 flow->vlans[id].tci &= ~htons(VLAN_VID_MASK);
2653 flow->vlans[id].tci |= htons(VLAN_CFI) | vid;
f0fb825a
EG
2654 }
2655}
2656
2657/* Sets the VLAN header TPID, which must be either ETH_TYPE_VLAN_8021Q or
2658 * ETH_TYPE_VLAN_8021AD. */
2659void
2660flow_fix_vlan_tpid(struct flow *flow)
2661{
2662 if (flow->vlans[0].tpid == htons(0) && flow->vlans[0].tci != 0) {
2663 flow->vlans[0].tpid = htons(ETH_TYPE_VLAN_8021Q);
3719455c
BP
2664 }
2665}
2666
cc34bc8c
BP
2667/* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
2668 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
2669 * plus CFI). */
2670void
2671flow_set_vlan_vid(struct flow *flow, ovs_be16 vid)
2672{
2673 ovs_be16 mask = htons(VLAN_VID_MASK | VLAN_CFI);
f0fb825a
EG
2674 flow->vlans[0].tci &= ~mask;
2675 flow->vlans[0].tci |= vid & mask;
cc34bc8c
BP
2676}
2677
3719455c
BP
2678/* Sets the VLAN PCP that 'flow' matches to 'pcp', which should be in the
2679 * range 0...7.
2680 *
2681 * This function has no effect on the VLAN ID that 'flow' matches.
2682 *
2683 * After calling this function, 'flow' will not match packets without a VLAN
2684 * header. */
2685void
2f9366be 2686flow_set_vlan_pcp(struct flow *flow, uint8_t pcp, int id)
3719455c
BP
2687{
2688 pcp &= 0x07;
2f9366be
JL
2689 flow->vlans[id].tci &= ~htons(VLAN_PCP_MASK);
2690 flow->vlans[id].tci |= htons((pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
f0fb825a
EG
2691}
2692
2693/* Counts the number of VLAN headers. */
2694int
2695flow_count_vlan_headers(const struct flow *flow)
2696{
2697 int i;
2698
2699 for (i = 0; i < FLOW_MAX_VLAN_HEADERS; i++) {
2700 if (!(flow->vlans[i].tci & htons(VLAN_CFI))) {
2701 break;
2702 }
2703 }
2704 return i;
2705}
2706
2707/* Given '*p_an' and '*p_bn' pointing to one past the last VLAN header of
2708 * 'a' and 'b' respectively, skip common VLANs so that they point to the
2709 * first different VLAN counting from bottom. */
2710void
2711flow_skip_common_vlan_headers(const struct flow *a, int *p_an,
2712 const struct flow *b, int *p_bn)
2713{
2714 int an = *p_an, bn = *p_bn;
2715
2716 for (an--, bn--; an >= 0 && bn >= 0; an--, bn--) {
2717 if (a->vlans[an].qtag != b->vlans[bn].qtag) {
2718 break;
2719 }
2720 }
2721 *p_an = an;
2722 *p_bn = bn;
2723}
2724
2725void
2726flow_pop_vlan(struct flow *flow, struct flow_wildcards *wc)
2727{
2728 int n = flow_count_vlan_headers(flow);
68c744fd
BP
2729 if (n > 1) {
2730 if (wc) {
2731 memset(&wc->masks.vlans[1], 0xff,
2732 sizeof(union flow_vlan_hdr) * (n - 1));
2733 }
2734 memmove(&flow->vlans[0], &flow->vlans[1],
2735 sizeof(union flow_vlan_hdr) * (n - 1));
f0fb825a 2736 }
68c744fd
BP
2737 if (n > 0) {
2738 memset(&flow->vlans[n - 1], 0, sizeof(union flow_vlan_hdr));
f0fb825a 2739 }
f0fb825a
EG
2740}
2741
2742void
2743flow_push_vlan_uninit(struct flow *flow, struct flow_wildcards *wc)
2744{
2745 if (wc) {
2746 int n = flow_count_vlan_headers(flow);
6b6b508b
DB
2747 if (n) {
2748 memset(wc->masks.vlans, 0xff, sizeof(union flow_vlan_hdr) * n);
2749 }
f0fb825a
EG
2750 }
2751 memmove(&flow->vlans[1], &flow->vlans[0],
2752 sizeof(union flow_vlan_hdr) * (FLOW_MAX_VLAN_HEADERS - 1));
2753 memset(&flow->vlans[0], 0, sizeof(union flow_vlan_hdr));
3719455c
BP
2754}
2755
8bfd0fda
BP
2756/* Returns the number of MPLS LSEs present in 'flow'
2757 *
2758 * Returns 0 if the 'dl_type' of 'flow' is not an MPLS ethernet type.
2759 * Otherwise traverses 'flow''s MPLS label stack stopping at the
2760 * first entry that has the BoS bit set. If no such entry exists then
2761 * the maximum number of LSEs that can be stored in 'flow' is returned.
2762 */
2763int
2764flow_count_mpls_labels(const struct flow *flow, struct flow_wildcards *wc)
2765{
22d38fca 2766 /* dl_type is always masked. */
8bfd0fda
BP
2767 if (eth_type_mpls(flow->dl_type)) {
2768 int i;
5af43325 2769 int cnt;
8bfd0fda 2770
5af43325
PS
2771 cnt = 0;
2772 for (i = 0; i < FLOW_MAX_MPLS_LABELS; i++) {
8bfd0fda
BP
2773 if (wc) {
2774 wc->masks.mpls_lse[i] |= htonl(MPLS_BOS_MASK);
2775 }
2776 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
2777 return i + 1;
2778 }
5af43325
PS
2779 if (flow->mpls_lse[i]) {
2780 cnt++;
2781 }
8bfd0fda 2782 }
5af43325 2783 return cnt;
8bfd0fda
BP
2784 } else {
2785 return 0;
2786 }
2787}
2788
2789/* Returns the number consecutive of MPLS LSEs, starting at the
2790 * innermost LSE, that are common in 'a' and 'b'.
2791 *
2792 * 'an' must be flow_count_mpls_labels(a).
2793 * 'bn' must be flow_count_mpls_labels(b).
2794 */
2795int
2796flow_count_common_mpls_labels(const struct flow *a, int an,
2797 const struct flow *b, int bn,
2798 struct flow_wildcards *wc)
2799{
2800 int min_n = MIN(an, bn);
2801 if (min_n == 0) {
2802 return 0;
2803 } else {
2804 int common_n = 0;
2805 int a_last = an - 1;
2806 int b_last = bn - 1;
2807 int i;
2808
2809 for (i = 0; i < min_n; i++) {
2810 if (wc) {
2811 wc->masks.mpls_lse[a_last - i] = OVS_BE32_MAX;
2812 wc->masks.mpls_lse[b_last - i] = OVS_BE32_MAX;
2813 }
2814 if (a->mpls_lse[a_last - i] != b->mpls_lse[b_last - i]) {
2815 break;
2816 } else {
2817 common_n++;
2818 }
2819 }
2820
2821 return common_n;
2822 }
2823}
2824
2825/* Adds a new outermost MPLS label to 'flow' and changes 'flow''s Ethernet type
2826 * to 'mpls_eth_type', which must be an MPLS Ethertype.
2827 *
2828 * If the new label is the first MPLS label in 'flow', it is generated as;
2829 *
2830 * - label: 2, if 'flow' is IPv6, otherwise 0.
2831 *
2832 * - TTL: IPv4 or IPv6 TTL, if present and nonzero, otherwise 64.
2833 *
2834 * - TC: IPv4 or IPv6 TOS, if present, otherwise 0.
2835 *
2836 * - BoS: 1.
2837 *
22d38fca 2838 * If the new label is the second or later label MPLS label in 'flow', it is
8bfd0fda
BP
2839 * generated as;
2840 *
368fb7e6 2841 * - label: Copied from outer label.
8bfd0fda
BP
2842 *
2843 * - TTL: Copied from outer label.
2844 *
2845 * - TC: Copied from outer label.
2846 *
2847 * - BoS: 0.
2848 *
2849 * 'n' must be flow_count_mpls_labels(flow). 'n' must be less than
2850 * FLOW_MAX_MPLS_LABELS (because otherwise flow->mpls_lse[] would overflow).
2851 */
2852void
2853flow_push_mpls(struct flow *flow, int n, ovs_be16 mpls_eth_type,
742c0ac3 2854 struct flow_wildcards *wc, bool clear_flow_L3)
8bfd0fda
BP
2855{
2856 ovs_assert(eth_type_mpls(mpls_eth_type));
2857 ovs_assert(n < FLOW_MAX_MPLS_LABELS);
2858
8bfd0fda
BP
2859 if (n) {
2860 int i;
2861
22d38fca
JR
2862 if (wc) {
2863 memset(&wc->masks.mpls_lse, 0xff, sizeof *wc->masks.mpls_lse * n);
2864 }
8bfd0fda
BP
2865 for (i = n; i >= 1; i--) {
2866 flow->mpls_lse[i] = flow->mpls_lse[i - 1];
2867 }
22d38fca 2868 flow->mpls_lse[0] = (flow->mpls_lse[1] & htonl(~MPLS_BOS_MASK));
8bfd0fda
BP
2869 } else {
2870 int label = 0; /* IPv4 Explicit Null. */
2871 int tc = 0;
2872 int ttl = 64;
2873
2874 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
2875 label = 2;
2876 }
2877
2878 if (is_ip_any(flow)) {
2879 tc = (flow->nw_tos & IP_DSCP_MASK) >> 2;
22d38fca
JR
2880 if (wc) {
2881 wc->masks.nw_tos |= IP_DSCP_MASK;
2882 wc->masks.nw_ttl = 0xff;
2883 }
8bfd0fda
BP
2884
2885 if (flow->nw_ttl) {
2886 ttl = flow->nw_ttl;
2887 }
8bfd0fda
BP
2888 }
2889
2890 flow->mpls_lse[0] = set_mpls_lse_values(ttl, tc, 1, htonl(label));
2891
742c0ac3
JR
2892 if (clear_flow_L3) {
2893 /* Clear all L3 and L4 fields and dp_hash. */
7dc18ae9 2894 BUILD_ASSERT(FLOW_WC_SEQ == 41);
742c0ac3
JR
2895 memset((char *) flow + FLOW_SEGMENT_2_ENDS_AT, 0,
2896 sizeof(struct flow) - FLOW_SEGMENT_2_ENDS_AT);
2897 flow->dp_hash = 0;
2898 }
8bfd0fda
BP
2899 }
2900 flow->dl_type = mpls_eth_type;
2901}
2902
2903/* Tries to remove the outermost MPLS label from 'flow'. Returns true if
2904 * successful, false otherwise. On success, sets 'flow''s Ethernet type to
2905 * 'eth_type'.
2906 *
2907 * 'n' must be flow_count_mpls_labels(flow). */
2908bool
2909flow_pop_mpls(struct flow *flow, int n, ovs_be16 eth_type,
2910 struct flow_wildcards *wc)
2911{
2912 int i;
2913
2914 if (n == 0) {
2915 /* Nothing to pop. */
2916 return false;
22d38fca
JR
2917 } else if (n == FLOW_MAX_MPLS_LABELS) {
2918 if (wc) {
2919 wc->masks.mpls_lse[n - 1] |= htonl(MPLS_BOS_MASK);
2920 }
2921 if (!(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
2922 /* Can't pop because don't know what to fill in mpls_lse[n - 1]. */
2923 return false;
2924 }
8bfd0fda
BP
2925 }
2926
22d38fca
JR
2927 if (wc) {
2928 memset(&wc->masks.mpls_lse[1], 0xff,
2929 sizeof *wc->masks.mpls_lse * (n - 1));
2930 }
8bfd0fda
BP
2931 for (i = 1; i < n; i++) {
2932 flow->mpls_lse[i - 1] = flow->mpls_lse[i];
2933 }
2934 flow->mpls_lse[n - 1] = 0;
2935 flow->dl_type = eth_type;
2936 return true;
2937}
2938
b02475c5
SH
2939/* Sets the MPLS Label that 'flow' matches to 'label', which is interpreted
2940 * as an OpenFlow 1.1 "mpls_label" value. */
2941void
8bfd0fda 2942flow_set_mpls_label(struct flow *flow, int idx, ovs_be32 label)
b02475c5 2943{
8bfd0fda 2944 set_mpls_lse_label(&flow->mpls_lse[idx], label);
b02475c5
SH
2945}
2946
b676167a
SH
2947/* Sets the MPLS TTL that 'flow' matches to 'ttl', which should be in the
2948 * range 0...255. */
2949void
8bfd0fda 2950flow_set_mpls_ttl(struct flow *flow, int idx, uint8_t ttl)
b676167a 2951{
8bfd0fda 2952 set_mpls_lse_ttl(&flow->mpls_lse[idx], ttl);
b676167a
SH
2953}
2954
b02475c5
SH
2955/* Sets the MPLS TC that 'flow' matches to 'tc', which should be in the
2956 * range 0...7. */
2957void
8bfd0fda 2958flow_set_mpls_tc(struct flow *flow, int idx, uint8_t tc)
b02475c5 2959{
8bfd0fda 2960 set_mpls_lse_tc(&flow->mpls_lse[idx], tc);
b02475c5
SH
2961}
2962
2963/* Sets the MPLS BOS bit that 'flow' matches to which should be 0 or 1. */
2964void
8bfd0fda 2965flow_set_mpls_bos(struct flow *flow, int idx, uint8_t bos)
b02475c5 2966{
8bfd0fda 2967 set_mpls_lse_bos(&flow->mpls_lse[idx], bos);
b02475c5
SH
2968}
2969
8bfd0fda
BP
2970/* Sets the entire MPLS LSE. */
2971void
2972flow_set_mpls_lse(struct flow *flow, int idx, ovs_be32 lse)
2973{
2974 flow->mpls_lse[idx] = lse;
2975}
52105b67 2976
6f068379
BP
2977static void
2978flow_compose_l7(struct dp_packet *p, const void *l7, size_t l7_len)
2979{
2980 if (l7_len) {
2981 if (l7) {
2982 dp_packet_put(p, l7, l7_len);
2983 } else {
2984 uint8_t *payload = dp_packet_put_uninit(p, l7_len);
2985 for (size_t i = 0; i < l7_len; i++) {
2986 payload[i] = i;
2987 }
2988 }
2989 }
2990}
2991
437d0d22 2992static size_t
6f068379
BP
2993flow_compose_l4(struct dp_packet *p, const struct flow *flow,
2994 const void *l7, size_t l7_len)
52105b67 2995{
89225d65 2996 size_t orig_len = dp_packet_size(p);
437d0d22 2997
52105b67
JR
2998 if (!(flow->nw_frag & FLOW_NW_FRAG_ANY)
2999 || !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3000 if (flow->nw_proto == IPPROTO_TCP) {
89225d65 3001 struct tcp_header *tcp = dp_packet_put_zeros(p, sizeof *tcp);
52105b67
JR
3002 tcp->tcp_src = flow->tp_src;
3003 tcp->tcp_dst = flow->tp_dst;
3004 tcp->tcp_ctl = TCP_CTL(ntohs(flow->tcp_flags), 5);
6f068379
BP
3005 if (!(flow->tcp_flags & htons(TCP_SYN | TCP_FIN | TCP_RST))) {
3006 flow_compose_l7(p, l7, l7_len);
3007 }
52105b67 3008 } else if (flow->nw_proto == IPPROTO_UDP) {
89225d65 3009 struct udp_header *udp = dp_packet_put_zeros(p, sizeof *udp);
52105b67
JR
3010 udp->udp_src = flow->tp_src;
3011 udp->udp_dst = flow->tp_dst;
6f068379
BP
3012 udp->udp_len = htons(sizeof *udp + l7_len);
3013 flow_compose_l7(p, l7, l7_len);
52105b67 3014 } else if (flow->nw_proto == IPPROTO_SCTP) {
89225d65 3015 struct sctp_header *sctp = dp_packet_put_zeros(p, sizeof *sctp);
52105b67
JR
3016 sctp->sctp_src = flow->tp_src;
3017 sctp->sctp_dst = flow->tp_dst;
6f068379 3018 /* XXX Someone should figure out what L7 data to include. */
52105b67 3019 } else if (flow->nw_proto == IPPROTO_ICMP) {
89225d65 3020 struct icmp_header *icmp = dp_packet_put_zeros(p, sizeof *icmp);
52105b67
JR
3021 icmp->icmp_type = ntohs(flow->tp_src);
3022 icmp->icmp_code = ntohs(flow->tp_dst);
6f068379
BP
3023 if ((icmp->icmp_type == ICMP4_ECHO_REQUEST ||
3024 icmp->icmp_type == ICMP4_ECHO_REPLY)
3025 && icmp->icmp_code == 0) {
3026 flow_compose_l7(p, l7, l7_len);
3027 } else {
3028 /* XXX Add inner IP packet for e.g. destination unreachable? */
3029 }
0e612675 3030 } else if (flow->nw_proto == IPPROTO_IGMP) {
89225d65 3031 struct igmp_header *igmp = dp_packet_put_zeros(p, sizeof *igmp);
0e612675
FL
3032 igmp->igmp_type = ntohs(flow->tp_src);
3033 igmp->igmp_code = ntohs(flow->tp_dst);
3034 put_16aligned_be32(&igmp->group, flow->igmp_group_ip4);
52105b67 3035 } else if (flow->nw_proto == IPPROTO_ICMPV6) {
361a47d6
IM
3036 struct icmp6_data_header *icmp6;
3037
3038 icmp6 = dp_packet_put_zeros(p, sizeof *icmp6);
3039 icmp6->icmp6_base.icmp6_type = ntohs(flow->tp_src);
3040 icmp6->icmp6_base.icmp6_code = ntohs(flow->tp_dst);
3041 put_16aligned_be32(icmp6->icmp6_data.be32, flow->igmp_group_ip4);
3042
3043 if (icmp6->icmp6_base.icmp6_code == 0 &&
3044 (icmp6->icmp6_base.icmp6_type == ND_NEIGHBOR_SOLICIT ||
3045 icmp6->icmp6_base.icmp6_type == ND_NEIGHBOR_ADVERT)) {
52105b67 3046 struct in6_addr *nd_target;
86d46f3c 3047 struct ovs_nd_lla_opt *lla_opt;
52105b67 3048
cf62fa4c 3049 nd_target = dp_packet_put_zeros(p, sizeof *nd_target);
52105b67
JR
3050 *nd_target = flow->nd_target;
3051
3052 if (!eth_addr_is_zero(flow->arp_sha)) {
86d46f3c
ZKL
3053 lla_opt = dp_packet_put_zeros(p, 8);
3054 lla_opt->len = 1;
3055 lla_opt->type = ND_OPT_SOURCE_LINKADDR;
3056 lla_opt->mac = flow->arp_sha;
52105b67
JR
3057 }
3058 if (!eth_addr_is_zero(flow->arp_tha)) {
86d46f3c
ZKL
3059 lla_opt = dp_packet_put_zeros(p, 8);
3060 lla_opt->len = 1;
3061 lla_opt->type = ND_OPT_TARGET_LINKADDR;
3062 lla_opt->mac = flow->arp_tha;
52105b67 3063 }
361a47d6
IM
3064 } else if (icmp6->icmp6_base.icmp6_code == 0 &&
3065 (icmp6->icmp6_base.icmp6_type == ICMP6_ECHO_REQUEST ||
3066 icmp6->icmp6_base.icmp6_type == ICMP6_ECHO_REPLY)) {
6f068379
BP
3067 flow_compose_l7(p, l7, l7_len);
3068 } else {
3069 /* XXX Add inner IP packet for e.g. destination unreachable? */
52105b67 3070 }
52105b67
JR
3071 }
3072 }
89225d65
BP
3073
3074 return dp_packet_size(p) - orig_len;
52105b67
JR
3075}
3076
e839d01e
DDP
3077static void
3078flow_compose_l4_csum(struct dp_packet *p, const struct flow *flow,
3079 uint32_t pseudo_hdr_csum)
3080{
3081 size_t l4_len = (char *) dp_packet_tail(p) - (char *) dp_packet_l4(p);
3082
3083 if (!(flow->nw_frag & FLOW_NW_FRAG_ANY)
3084 || !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
3085 if (flow->nw_proto == IPPROTO_TCP) {
3086 struct tcp_header *tcp = dp_packet_l4(p);
3087
3476ce3a 3088 tcp->tcp_csum = 0;
e839d01e
DDP
3089 tcp->tcp_csum = csum_finish(csum_continue(pseudo_hdr_csum,
3090 tcp, l4_len));
3091 } else if (flow->nw_proto == IPPROTO_UDP) {
3092 struct udp_header *udp = dp_packet_l4(p);
3093
3476ce3a 3094 udp->udp_csum = 0;
e839d01e
DDP
3095 udp->udp_csum = csum_finish(csum_continue(pseudo_hdr_csum,
3096 udp, l4_len));
7a17a07d
LR
3097 if (!udp->udp_csum) {
3098 udp->udp_csum = htons(0xffff);
3099 }
e839d01e
DDP
3100 } else if (flow->nw_proto == IPPROTO_ICMP) {
3101 struct icmp_header *icmp = dp_packet_l4(p);
3102
3476ce3a 3103 icmp->icmp_csum = 0;
e839d01e
DDP
3104 icmp->icmp_csum = csum(icmp, l4_len);
3105 } else if (flow->nw_proto == IPPROTO_IGMP) {
3106 struct igmp_header *igmp = dp_packet_l4(p);
3107
3476ce3a 3108 igmp->igmp_csum = 0;
e839d01e
DDP
3109 igmp->igmp_csum = csum(igmp, l4_len);
3110 } else if (flow->nw_proto == IPPROTO_ICMPV6) {
361a47d6 3111 struct icmp6_data_header *icmp6 = dp_packet_l4(p);
e839d01e 3112
361a47d6
IM
3113 icmp6->icmp6_base.icmp6_cksum = 0;
3114 icmp6->icmp6_base.icmp6_cksum =
3115 csum_finish(csum_continue(pseudo_hdr_csum, icmp6, l4_len));
e839d01e
DDP
3116 }
3117 }
3118}
3119
bc0f5176
AZ
3120/* Increase the size of packet composed by 'flow_compose_minimal'
3121 * up to 'size' bytes. Fixes all the required packet headers like
3122 * ip/udp lengths and l3/l4 checksums.
3123 *
3124 * 'size' needs to be larger then the current packet size. */
6f068379 3125void
bc0f5176 3126packet_expand(struct dp_packet *p, const struct flow *flow, size_t size)
3476ce3a
IM
3127{
3128 size_t extra_size;
3129
bc0f5176 3130 ovs_assert(size > dp_packet_size(p));
3476ce3a
IM
3131
3132 extra_size = size - dp_packet_size(p);
3133 dp_packet_put_zeros(p, extra_size);
3134
3135 if (flow->dl_type == htons(FLOW_DL_TYPE_NONE)) {
3136 struct eth_header *eth = dp_packet_eth(p);
3137
3138 eth->eth_type = htons(dp_packet_size(p));
3476ce3a
IM
3139 } else if (dl_type_is_ip_any(flow->dl_type)) {
3140 uint32_t pseudo_hdr_csum;
3141 size_t l4_len = (char *) dp_packet_tail(p) - (char *) dp_packet_l4(p);
3142
3143 if (flow->dl_type == htons(ETH_TYPE_IP)) {
3144 struct ip_header *ip = dp_packet_l3(p);
3145
3146 ip->ip_tot_len = htons(p->l4_ofs - p->l3_ofs + l4_len);
3147 ip->ip_csum = 0;
3148 ip->ip_csum = csum(ip, sizeof *ip);
3149
3150 pseudo_hdr_csum = packet_csum_pseudoheader(ip);
3151 } else { /* ETH_TYPE_IPV6 */
3152 struct ovs_16aligned_ip6_hdr *nh = dp_packet_l3(p);
3153
3154 nh->ip6_plen = htons(l4_len);
3155 pseudo_hdr_csum = packet_csum_pseudoheader6(nh);
3156 }
3157
3158 if ((!(flow->nw_frag & FLOW_NW_FRAG_ANY)
3159 || !(flow->nw_frag & FLOW_NW_FRAG_LATER))
3160 && flow->nw_proto == IPPROTO_UDP) {
3161 struct udp_header *udp = dp_packet_l4(p);
3162
3163 udp->udp_len = htons(l4_len + extra_size);
3164 }
3165 flow_compose_l4_csum(p, flow, pseudo_hdr_csum);
3166 }
3167}
3168
3e24a894 3169/* Puts into 'p' a packet that flow_extract() would parse as having the given
8b3b8dd1
BP
3170 * 'flow'.
3171 *
3172 * (This is useful only for testing, obviously, and the packet isn't really
bc0f5176
AZ
3173 * valid. Lots of fields are just zeroed.)
3174 *
6f068379
BP
3175 * For packets whose protocols can encapsulate arbitrary L7 payloads, 'l7' and
3176 * 'l7_len' determine that payload:
3177 *
3178 * - If 'l7_len' is zero, no payload is included.
3179 *
3180 * - If 'l7_len' is nonzero and 'l7' is null, an arbitrary payload 'l7_len'
3181 * bytes long is included.
3182 *
3183 * - If 'l7_len' is nonzero and 'l7' is nonnull, the payload is copied
3184 * from 'l7'. */
3185void
3186flow_compose(struct dp_packet *p, const struct flow *flow,
3187 const void *l7, size_t l7_len)
8b3b8dd1 3188{
005bb872
BP
3189 /* Add code to this function (or its callees) for emitting new fields or
3190 * protocols. (This isn't essential, so it can be skipped for initial
3191 * testing.) */
3192 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 41);
3193
e839d01e 3194 uint32_t pseudo_hdr_csum;
437d0d22
JR
3195 size_t l4_len;
3196
52105b67 3197 /* eth_compose() sets l3 pointer and makes sure it is 32-bit aligned. */
cf62fa4c 3198 eth_compose(p, flow->dl_dst, flow->dl_src, ntohs(flow->dl_type), 0);
8b3b8dd1 3199 if (flow->dl_type == htons(FLOW_DL_TYPE_NONE)) {
2482b0b0 3200 struct eth_header *eth = dp_packet_eth(p);
cf62fa4c 3201 eth->eth_type = htons(dp_packet_size(p));
8b3b8dd1
BP
3202 return;
3203 }
3204
f0fb825a
EG
3205 for (int encaps = FLOW_MAX_VLAN_HEADERS - 1; encaps >= 0; encaps--) {
3206 if (flow->vlans[encaps].tci & htons(VLAN_CFI)) {
3207 eth_push_vlan(p, flow->vlans[encaps].tpid,
3208 flow->vlans[encaps].tci);
3209 }
8b3b8dd1
BP
3210 }
3211
cff78c88 3212 if (flow->dl_type == htons(ETH_TYPE_IP)) {
8b3b8dd1
BP
3213 struct ip_header *ip;
3214
cf62fa4c 3215 ip = dp_packet_put_zeros(p, sizeof *ip);
8b3b8dd1 3216 ip->ip_ihl_ver = IP_IHL_VER(5, 4);
eadef313 3217 ip->ip_tos = flow->nw_tos;
aabf5352 3218 ip->ip_ttl = flow->nw_ttl;
8b3b8dd1 3219 ip->ip_proto = flow->nw_proto;
7c457c33
BP
3220 put_16aligned_be32(&ip->ip_src, flow->nw_src);
3221 put_16aligned_be32(&ip->ip_dst, flow->nw_dst);
8b3b8dd1 3222
eadef313 3223 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
7257b535 3224 ip->ip_frag_off |= htons(IP_MORE_FRAGMENTS);
eadef313 3225 if (flow->nw_frag & FLOW_NW_FRAG_LATER) {
7257b535
BP
3226 ip->ip_frag_off |= htons(100);
3227 }
3228 }
df9b6612 3229
cf62fa4c 3230 dp_packet_set_l4(p, dp_packet_tail(p));
52105b67 3231
6f068379 3232 l4_len = flow_compose_l4(p, flow, l7, l7_len);
52105b67 3233
cf62fa4c
PS
3234 ip = dp_packet_l3(p);
3235 ip->ip_tot_len = htons(p->l4_ofs - p->l3_ofs + l4_len);
ece9c294 3236 /* Checksum has already been zeroed by put_zeros call. */
dc5a7ce7 3237 ip->ip_csum = csum(ip, sizeof *ip);
e839d01e
DDP
3238
3239 pseudo_hdr_csum = packet_csum_pseudoheader(ip);
3240 flow_compose_l4_csum(p, flow, pseudo_hdr_csum);
cff78c88 3241 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
52105b67
JR
3242 struct ovs_16aligned_ip6_hdr *nh;
3243
cf62fa4c 3244 nh = dp_packet_put_zeros(p, sizeof *nh);
52105b67
JR
3245 put_16aligned_be32(&nh->ip6_flow, htonl(6 << 28) |
3246 htonl(flow->nw_tos << 20) | flow->ipv6_label);
3247 nh->ip6_hlim = flow->nw_ttl;
3248 nh->ip6_nxt = flow->nw_proto;
3249
3250 memcpy(&nh->ip6_src, &flow->ipv6_src, sizeof(nh->ip6_src));
3251 memcpy(&nh->ip6_dst, &flow->ipv6_dst, sizeof(nh->ip6_dst));
3252
cf62fa4c 3253 dp_packet_set_l4(p, dp_packet_tail(p));
52105b67 3254
6f068379 3255 l4_len = flow_compose_l4(p, flow, l7, l7_len);
52105b67 3256
cf62fa4c 3257 nh = dp_packet_l3(p);
437d0d22 3258 nh->ip6_plen = htons(l4_len);
e839d01e
DDP
3259
3260 pseudo_hdr_csum = packet_csum_pseudoheader6(nh);
3261 flow_compose_l4_csum(p, flow, pseudo_hdr_csum);
cff78c88
SH
3262 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
3263 flow->dl_type == htons(ETH_TYPE_RARP)) {
8b3b8dd1
BP
3264 struct arp_eth_header *arp;
3265
cf62fa4c
PS
3266 arp = dp_packet_put_zeros(p, sizeof *arp);
3267 dp_packet_set_l3(p, arp);
8b3b8dd1
BP
3268 arp->ar_hrd = htons(1);
3269 arp->ar_pro = htons(ETH_TYPE_IP);
3270 arp->ar_hln = ETH_ADDR_LEN;
3271 arp->ar_pln = 4;
3272 arp->ar_op = htons(flow->nw_proto);
3273
3274 if (flow->nw_proto == ARP_OP_REQUEST ||
3275 flow->nw_proto == ARP_OP_REPLY) {
7c457c33
BP
3276 put_16aligned_be32(&arp->ar_spa, flow->nw_src);
3277 put_16aligned_be32(&arp->ar_tpa, flow->nw_dst);
74ff3298
JR
3278 arp->ar_sha = flow->arp_sha;
3279 arp->ar_tha = flow->arp_tha;
8b3b8dd1
BP
3280 }
3281 }
b02475c5
SH
3282
3283 if (eth_type_mpls(flow->dl_type)) {
8bfd0fda
BP
3284 int n;
3285
cf62fa4c 3286 p->l2_5_ofs = p->l3_ofs;
8bfd0fda
BP
3287 for (n = 1; n < FLOW_MAX_MPLS_LABELS; n++) {
3288 if (flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK)) {
3289 break;
3290 }
3291 }
3292 while (n > 0) {
cf62fa4c 3293 push_mpls(p, flow->dl_type, flow->mpls_lse[--n]);
8bfd0fda 3294 }
b02475c5 3295 }
8b3b8dd1 3296}
5cb7a798
BP
3297\f
3298/* Compressed flow. */
3299
df40c152 3300/* Completes an initialization of 'dst' as a miniflow copy of 'src' begun by
5fcff47b
JR
3301 * the caller. The caller must have already computed 'dst->map' properly to
3302 * indicate the significant uint64_t elements of 'src'.
13751fd8
JR
3303 *
3304 * Normally the significant elements are the ones that are non-zero. However,
3305 * when a miniflow is initialized from a (mini)mask, the values can be zeroes,
ceb3bd67
JR
3306 * so that the flow and mask always have the same maps. */
3307void
3308miniflow_init(struct miniflow *dst, const struct flow *src)
df40c152 3309{
09b0fa9c 3310 uint64_t *dst_u64 = miniflow_values(dst);
361d808d 3311 size_t idx;
df40c152 3312
5fcff47b
JR
3313 FLOWMAP_FOR_EACH_INDEX(idx, dst->map) {
3314 *dst_u64++ = flow_u64_value(src, idx);
df40c152
BP
3315 }
3316}
3317
361d808d 3318/* Initialize the maps of 'flow' from 'src'. */
ceb3bd67
JR
3319void
3320miniflow_map_init(struct miniflow *flow, const struct flow *src)
5cb7a798 3321{
ceb3bd67 3322 /* Initialize map, counting the number of nonzero elements. */
5fcff47b
JR
3323 flowmap_init(&flow->map);
3324 for (size_t i = 0; i < FLOW_U64S; i++) {
3325 if (flow_u64_value(src, i)) {
3326 flowmap_set(&flow->map, i, 1);
5cb7a798
BP
3327 }
3328 }
ceb3bd67 3329}
5cb7a798 3330
ceb3bd67
JR
3331/* Allocates 'n' count of miniflows, consecutive in memory, initializing the
3332 * map of each from 'src'.
3333 * Returns the size of the miniflow data. */
3334size_t
3335miniflow_alloc(struct miniflow *dsts[], size_t n, const struct miniflow *src)
3336{
361d808d
JR
3337 size_t n_values = miniflow_n_values(src);
3338 size_t data_size = MINIFLOW_VALUES_SIZE(n_values);
3339 struct miniflow *dst = xmalloc(n * (sizeof *src + data_size));
5fcff47b 3340 size_t i;
ceb3bd67
JR
3341
3342 COVERAGE_INC(miniflow_malloc);
3343
3344 for (i = 0; i < n; i++) {
361d808d 3345 *dst = *src; /* Copy maps. */
ceb3bd67 3346 dsts[i] = dst;
361d808d
JR
3347 dst += 1; /* Just past the maps. */
3348 dst = (struct miniflow *)((uint64_t *)dst + n_values); /* Skip data. */
ceb3bd67
JR
3349 }
3350 return data_size;
df40c152 3351}
5cb7a798 3352
ceb3bd67
JR
3353/* Returns a miniflow copy of 'src'. The caller must eventually free() the
3354 * returned miniflow. */
8fd47924 3355struct miniflow *
ceb3bd67 3356miniflow_create(const struct flow *src)
df40c152 3357{
ceb3bd67
JR
3358 struct miniflow tmp;
3359 struct miniflow *dst;
3360
3361 miniflow_map_init(&tmp, src);
3362
3363 miniflow_alloc(&dst, 1, &tmp);
3364 miniflow_init(dst, src);
3365 return dst;
5cb7a798
BP
3366}
3367
3016f3e4 3368/* Initializes 'dst' as a copy of 'src'. The caller must have allocated
8fd47924 3369 * 'dst' to have inline space for 'n_values' data in 'src'. */
3016f3e4 3370void
a851eb94
JR
3371miniflow_clone(struct miniflow *dst, const struct miniflow *src,
3372 size_t n_values)
3016f3e4 3373{
361d808d 3374 *dst = *src; /* Copy maps. */
09b0fa9c
JR
3375 memcpy(miniflow_values(dst), miniflow_get_values(src),
3376 MINIFLOW_VALUES_SIZE(n_values));
5cb7a798
BP
3377}
3378
3379/* Initializes 'dst' as a copy of 'src'. */
3380void
3381miniflow_expand(const struct miniflow *src, struct flow *dst)
3382{
ad77e3c5
EJ
3383 memset(dst, 0, sizeof *dst);
3384 flow_union_with_miniflow(dst, src);
5cb7a798
BP
3385}
3386
8fd47924 3387/* Returns true if 'a' and 'b' are equal miniflows, false otherwise. */
5cb7a798
BP
3388bool
3389miniflow_equal(const struct miniflow *a, const struct miniflow *b)
3390{
09b0fa9c
JR
3391 const uint64_t *ap = miniflow_get_values(a);
3392 const uint64_t *bp = miniflow_get_values(b);
5cb7a798 3393
5fcff47b
JR
3394 /* This is mostly called after a matching hash, so it is highly likely that
3395 * the maps are equal as well. */
3396 if (OVS_LIKELY(flowmap_equal(a->map, b->map))) {
361d808d 3397 return !memcmp(ap, bp, miniflow_n_values(a) * sizeof *ap);
080e28d0 3398 } else {
5fcff47b 3399 size_t idx;
df40c152 3400
5fcff47b
JR
3401 FLOWMAP_FOR_EACH_INDEX (idx, flowmap_or(a->map, b->map)) {
3402 if ((flowmap_is_set(&a->map, idx) ? *ap++ : 0)
3403 != (flowmap_is_set(&b->map, idx) ? *bp++ : 0)) {
080e28d0 3404 return false;
df40c152 3405 }
5cb7a798
BP
3406 }
3407 }
3408
df40c152 3409 return true;
5cb7a798
BP
3410}
3411
de4ad4a2
JR
3412/* Returns false if 'a' and 'b' differ at the places where there are 1-bits
3413 * in 'mask', true otherwise. */
5cb7a798
BP
3414bool
3415miniflow_equal_in_minimask(const struct miniflow *a, const struct miniflow *b,
3416 const struct minimask *mask)
3417{
09b0fa9c 3418 const uint64_t *p = miniflow_get_values(&mask->masks);
361d808d 3419 size_t idx;
5cb7a798 3420
5fcff47b 3421 FLOWMAP_FOR_EACH_INDEX(idx, mask->masks.map) {
1cea007c 3422 if ((miniflow_get(a, idx) ^ miniflow_get(b, idx)) & *p++) {
080e28d0 3423 return false;
5cb7a798
BP
3424 }
3425 }
3426
3427 return true;
3428}
3429
3430/* Returns true if 'a' and 'b' are equal at the places where there are 1-bits
3431 * in 'mask', false if they differ. */
3432bool
3433miniflow_equal_flow_in_minimask(const struct miniflow *a, const struct flow *b,
3434 const struct minimask *mask)
3435{
09b0fa9c 3436 const uint64_t *p = miniflow_get_values(&mask->masks);
361d808d 3437 size_t idx;
5cb7a798 3438
5fcff47b
JR
3439 FLOWMAP_FOR_EACH_INDEX(idx, mask->masks.map) {
3440 if ((miniflow_get(a, idx) ^ flow_u64_value(b, idx)) & *p++) {
080e28d0 3441 return false;
5cb7a798
BP
3442 }
3443 }
3444
3445 return true;
3446}
3447
5cb7a798 3448\f
ceb3bd67
JR
3449void
3450minimask_init(struct minimask *mask, const struct flow_wildcards *wc)
3451{
3452 miniflow_init(&mask->masks, &wc->masks);
3453}
3454
8fd47924
JR
3455/* Returns a minimask copy of 'wc'. The caller must eventually free the
3456 * returned minimask with free(). */
3457struct minimask *
3458minimask_create(const struct flow_wildcards *wc)
5cb7a798 3459{
8fd47924 3460 return (struct minimask *)miniflow_create(&wc->masks);
5cb7a798
BP
3461}
3462
3463/* Initializes 'dst_' as the bit-wise "and" of 'a_' and 'b_'.
3464 *
8fd47924
JR
3465 * The caller must provide room for FLOW_U64S "uint64_t"s in 'storage', which
3466 * must follow '*dst_' in memory, for use by 'dst_'. The caller must *not*
3467 * free 'dst_' free(). */
5cb7a798
BP
3468void
3469minimask_combine(struct minimask *dst_,
3470 const struct minimask *a_, const struct minimask *b_,
d70e8c28 3471 uint64_t storage[FLOW_U64S])
5cb7a798
BP
3472{
3473 struct miniflow *dst = &dst_->masks;
d70e8c28 3474 uint64_t *dst_values = storage;
5cb7a798
BP
3475 const struct miniflow *a = &a_->masks;
3476 const struct miniflow *b = &b_->masks;
361d808d 3477 size_t idx;
5cb7a798 3478
5fcff47b 3479 flowmap_init(&dst->map);
080e28d0 3480
5fcff47b 3481 FLOWMAP_FOR_EACH_INDEX(idx, flowmap_and(a->map, b->map)) {
361d808d 3482 /* Both 'a' and 'b' have non-zero data at 'idx'. */
5fcff47b 3483 uint64_t mask = *miniflow_get__(a, idx) & *miniflow_get__(b, idx);
361d808d
JR
3484
3485 if (mask) {
5fcff47b 3486 flowmap_set(&dst->map, idx, 1);
1cea007c 3487 *dst_values++ = mask;
5cb7a798
BP
3488 }
3489 }
3490}
3491
8fd47924 3492/* Initializes 'wc' as a copy of 'mask'. */
5cb7a798
BP
3493void
3494minimask_expand(const struct minimask *mask, struct flow_wildcards *wc)
3495{
3496 miniflow_expand(&mask->masks, &wc->masks);
3497}
3498
f4d335e9
JR
3499/* Returns true if 'a' and 'b' are the same flow mask, false otherwise.
3500 * Minimasks may not have zero data values, so for the minimasks to be the
3501 * same, they need to have the same map and the same data values. */
5cb7a798
BP
3502bool
3503minimask_equal(const struct minimask *a, const struct minimask *b)
3504{
2ed65055
BP
3505 /* At first glance, it might seem that this can be reasonably optimized
3506 * into a single memcmp() for the total size of the region. Such an
3507 * optimization will work OK with most implementations of memcmp() that
3508 * proceed from the start of the regions to be compared to the end in
3509 * reasonably sized chunks. However, memcmp() is not required to be
3510 * implemented that way, and an implementation that, for example, compares
3511 * all of the bytes in both regions without early exit when it finds a
3512 * difference, or one that compares, say, 64 bytes at a time, could access
3513 * an unmapped region of memory if minimasks 'a' and 'b' have different
3514 * lengths. By first checking that the maps are the same with the first
3515 * memcmp(), we verify that 'a' and 'b' have the same length and therefore
3516 * ensure that the second memcmp() is safe. */
3517 return (!memcmp(a, b, sizeof *a)
3518 && !memcmp(a + 1, b + 1,
3519 MINIFLOW_VALUES_SIZE(miniflow_n_values(&a->masks))));
5cb7a798
BP
3520}
3521
d4570fd8 3522/* Returns true if at least one bit matched by 'b' is wildcarded by 'a',
5cb7a798
BP
3523 * false otherwise. */
3524bool
d4570fd8 3525minimask_has_extra(const struct minimask *a, const struct minimask *b)
5cb7a798 3526{
09b0fa9c 3527 const uint64_t *bp = miniflow_get_values(&b->masks);
361d808d 3528 size_t idx;
5cb7a798 3529
5fcff47b 3530 FLOWMAP_FOR_EACH_INDEX(idx, b->masks.map) {
d70e8c28 3531 uint64_t b_u64 = *bp++;
5cb7a798 3532
d70e8c28
JR
3533 /* 'b_u64' is non-zero, check if the data in 'a' is either zero
3534 * or misses some of the bits in 'b_u64'. */
5fcff47b
JR
3535 if (!MINIFLOW_IN_MAP(&a->masks, idx)
3536 || ((*miniflow_get__(&a->masks, idx) & b_u64) != b_u64)) {
f4d335e9 3537 return true; /* 'a' wildcards some bits 'b' doesn't. */
5cb7a798
BP
3538 }
3539 }
3540
3541 return false;
3542}
f0fb825a
EG
3543
3544void
3545flow_limit_vlans(int vlan_limit)
3546{
3547 if (vlan_limit <= 0) {
3548 flow_vlan_limit = FLOW_MAX_VLAN_HEADERS;
3549 } else {
3550 flow_vlan_limit = MIN(vlan_limit, FLOW_MAX_VLAN_HEADERS);
3551 }
3552}
738c785f
SB
3553
3554struct netdev *
3555flow_get_tunnel_netdev(struct flow_tnl *tunnel)
3556{
3557 char iface[IFNAMSIZ];
3558 struct in6_addr ip6;
3559 struct in6_addr gw;
3560
3561 if (tunnel->ip_src) {
3562 in6_addr_set_mapped_ipv4(&ip6, tunnel->ip_src);
3563 } else if (ipv6_addr_is_set(&tunnel->ipv6_src)) {
3564 ip6 = tunnel->ipv6_src;
3565 } else {
3566 return NULL;
3567 }
3568
3569 if (!ovs_router_lookup(0, &ip6, iface, NULL, &gw)) {
3570 return NULL;
3571 }
3572
3573 return netdev_from_name(iface);
3574}