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