2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2017 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <netinet/icmp6.h>
25 #include <netinet/ip6.h>
29 #include "byte-order.h"
33 #include "openvswitch/dynamic-string.h"
36 #include "openvswitch/match.h"
37 #include "dp-packet.h"
38 #include "openflow/openflow.h"
42 #include "unaligned.h"
44 #include "openvswitch/nsh.h"
45 #include "ovs-router.h"
46 #include "lib/netdev-provider.h"
48 COVERAGE_DEFINE(flow_extract
);
49 COVERAGE_DEFINE(miniflow_malloc
);
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),
59 int flow_vlan_limit
= FLOW_MAX_VLAN_HEADERS
;
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))
68 /* Asserts that fields 'f0' and 'f1' are in the same 32-bit aligned word within
70 #define ASSERT_SAME_WORD(f0, f1) \
71 BUILD_ASSERT_DECL(offsetof(struct flow, f0) / 4 \
72 == offsetof(struct flow, f1) / 4)
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)
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
);
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);
90 #define TCP_FLAGS_BE32(tcp_ctl) ((OVS_FORCE ovs_be32)TCP_FLAGS_BE16(tcp_ctl) \
93 #define TCP_FLAGS_BE32(tcp_ctl) ((OVS_FORCE ovs_be32)TCP_FLAGS_BE16(tcp_ctl))
96 ASSERT_SEQUENTIAL_SAME_WORD(tp_src
, tp_dst
);
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
101 static inline const void *
102 data_pull(const void **datap
, size_t *sizep
, size_t size
)
104 const char *data
= *datap
;
105 *datap
= data
+ size
;
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
)
116 return OVS_LIKELY(*sizep
>= size
) ? data_pull(datap
, sizep
, size
) : NULL
;
119 /* Context for pushing data to a miniflow. */
123 uint64_t * const end
;
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. */
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 "
138 #define MINIFLOW_ASSERT(X)
141 /* True if 'IDX' and higher bits are not set. */
142 #define ASSERT_FLOWMAP_NOT_SET(FM, IDX) \
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]); \
151 #define miniflow_set_map(MF, OFS) \
153 ASSERT_FLOWMAP_NOT_SET(&MF.map, (OFS)); \
154 flowmap_set(&MF.map, (OFS), 1); \
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)
161 #define miniflow_push_uint64_(MF, OFS, VALUE) \
163 MINIFLOW_ASSERT(MF.data < MF.end && (OFS) % 8 == 0); \
164 *MF.data++ = VALUE; \
165 miniflow_set_map(MF, OFS / 8); \
168 #define miniflow_push_be64_(MF, OFS, VALUE) \
169 miniflow_push_uint64_(MF, OFS, (OVS_FORCE uint64_t)(VALUE))
171 #define miniflow_push_uint32_(MF, OFS, VALUE) \
173 MINIFLOW_ASSERT(MF.data < MF.end); \
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; \
185 #define miniflow_push_be32_(MF, OFS, VALUE) \
186 miniflow_push_uint32_(MF, OFS, (OVS_FORCE uint32_t)(VALUE))
188 #define miniflow_push_uint16_(MF, OFS, VALUE) \
190 MINIFLOW_ASSERT(MF.data < MF.end); \
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; \
208 #define miniflow_push_uint8_(MF, OFS, VALUE) \
210 MINIFLOW_ASSERT(MF.data < MF.end); \
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; \
220 miniflow_assert_in_map(MF, OFS / 8); \
221 *((uint8_t *)MF.data + ((OFS) % 8)) = VALUE; \
225 #define miniflow_pad_to_64_(MF, OFS) \
227 MINIFLOW_ASSERT((OFS) % 8 != 0); \
228 miniflow_assert_in_map(MF, OFS / 8); \
230 memset((uint8_t *)MF.data + (OFS) % 8, 0, 8 - (OFS) % 8); \
234 #define miniflow_pad_from_64_(MF, OFS) \
236 MINIFLOW_ASSERT(MF.data < MF.end); \
238 MINIFLOW_ASSERT((OFS) % 8 != 0); \
239 miniflow_set_map(MF, OFS / 8); \
241 memset((uint8_t *)MF.data, 0, (OFS) % 8); \
244 #define miniflow_push_be16_(MF, OFS, VALUE) \
245 miniflow_push_uint16_(MF, OFS, (OVS_FORCE uint16_t)VALUE);
247 #define miniflow_push_be8_(MF, OFS, VALUE) \
248 miniflow_push_uint8_(MF, OFS, (OVS_FORCE uint8_t)VALUE);
250 #define miniflow_set_maps(MF, OFS, N_WORDS) \
252 size_t ofs = (OFS); \
253 size_t n_words = (N_WORDS); \
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); \
260 /* Data at 'valuep' may be unaligned. */
261 #define miniflow_push_words_(MF, OFS, VALUEP, N_WORDS) \
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); \
269 /* Push 32-bit words padded to 64-bits. */
270 #define miniflow_push_words_32_(MF, OFS, VALUEP, N_WORDS) \
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; \
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) \
284 miniflow_set_maps(MF, (OFS) / 8, 2); \
285 memcpy(MF.data, (VALUEP), 2 * ETH_ADDR_LEN); \
286 MF.data += 1; /* First word only. */ \
289 #define miniflow_push_uint32(MF, FIELD, VALUE) \
290 miniflow_push_uint32_(MF, offsetof(struct flow, FIELD), VALUE)
292 #define miniflow_push_be32(MF, FIELD, VALUE) \
293 miniflow_push_be32_(MF, offsetof(struct flow, FIELD), VALUE)
295 #define miniflow_push_uint16(MF, FIELD, VALUE) \
296 miniflow_push_uint16_(MF, offsetof(struct flow, FIELD), VALUE)
298 #define miniflow_push_be16(MF, FIELD, VALUE) \
299 miniflow_push_be16_(MF, offsetof(struct flow, FIELD), VALUE)
301 #define miniflow_push_uint8(MF, FIELD, VALUE) \
302 miniflow_push_uint8_(MF, offsetof(struct flow, FIELD), VALUE)
304 #define miniflow_pad_to_64(MF, FIELD) \
305 miniflow_pad_to_64_(MF, OFFSETOFEND(struct flow, FIELD))
307 #define miniflow_pad_from_64(MF, FIELD) \
308 miniflow_pad_from_64_(MF, offsetof(struct flow, FIELD))
310 #define miniflow_push_words(MF, FIELD, VALUEP, N_WORDS) \
311 miniflow_push_words_(MF, offsetof(struct flow, FIELD), VALUEP, N_WORDS)
313 #define miniflow_push_words_32(MF, FIELD, VALUEP, N_WORDS) \
314 miniflow_push_words_32_(MF, offsetof(struct flow, FIELD), VALUEP, N_WORDS)
316 #define miniflow_push_macs(MF, FIELD, VALUEP) \
317 miniflow_push_macs_(MF, offsetof(struct flow, FIELD), VALUEP)
319 /* Return the pointer to the miniflow data when called BEFORE the corresponding
321 #define miniflow_pointer(MF, FIELD) \
322 (void *)((uint8_t *)MF.data + ((offsetof(struct flow, FIELD)) % 8))
324 /* Pulls the MPLS headers at '*datap' and returns the count of them. */
326 parse_mpls(const void **datap
, size_t *sizep
)
328 const struct mpls_hdr
*mh
;
331 while ((mh
= data_try_pull(datap
, sizep
, sizeof *mh
))) {
333 if (mh
->mpls_lse
.lo
& htons(1 << MPLS_BOS_SHIFT
)) {
337 return MIN(count
, FLOW_MAX_MPLS_LABELS
);
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
)
344 const ovs_be16
*eth_type
;
346 memset(vlan_hdrs
, 0, sizeof(union flow_vlan_hdr
) * FLOW_MAX_VLAN_HEADERS
);
347 data_pull(datap
, sizep
, ETH_ADDR_LEN
* 2);
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
))) {
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
);
365 static inline ALWAYS_INLINE ovs_be16
366 parse_ethertype(const void **datap
, size_t *sizep
)
368 const struct llc_snap_header
*llc
;
371 proto
= *(ovs_be16
*) data_pull(datap
, sizep
, sizeof proto
);
372 if (OVS_LIKELY(ntohs(proto
) >= ETH_TYPE_MIN
)) {
376 if (OVS_UNLIKELY(*sizep
< sizeof *llc
)) {
377 return htons(FLOW_DL_TYPE_NONE
);
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
);
389 data_pull(datap
, sizep
, sizeof *llc
);
391 if (OVS_LIKELY(ntohs(llc
->snap
.snap_type
) >= ETH_TYPE_MIN
)) {
392 return llc
->snap
.snap_type
;
395 return htons(FLOW_DL_TYPE_NONE
);
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[]'. */
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])
406 if (icmp
->icmp6_code
!= 0 ||
407 (icmp
->icmp6_type
!= ND_NEIGHBOR_SOLICIT
&&
408 icmp
->icmp6_type
!= ND_NEIGHBOR_ADVERT
)) {
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
)) {
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
;
425 if (!opt_len
|| opt_len
> *sizep
) {
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
;
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
;
446 if (OVS_UNLIKELY(!data_try_pull(datap
, sizep
, opt_len
))) {
454 arp_buf
[0] = eth_addr_zero
;
455 arp_buf
[1] = eth_addr_zero
;
460 parse_ipv6_ext_hdrs__(const void **datap
, size_t *sizep
, uint8_t *nw_proto
,
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'. */
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
480 if (OVS_UNLIKELY(*sizep
< 8)) {
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
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))) {
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
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))) {
507 } else if (*nw_proto
== IPPROTO_FRAGMENT
) {
508 const struct ovs_16aligned_ip6_frag
*frag_hdr
= *datap
;
510 *nw_proto
= frag_hdr
->ip6f_nxt
;
511 if (!data_try_pull(datap
, sizep
, sizeof *frag_hdr
)) {
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
;
529 parse_ipv6_ext_hdrs(const void **datap
, size_t *sizep
, uint8_t *nw_proto
,
532 return parse_ipv6_ext_hdrs__(datap
, sizep
, nw_proto
, nw_frag
);
536 parse_nsh(const void **datap
, size_t *sizep
, struct ovs_key_nsh
*key
)
538 const struct nsh_hdr
*nsh
= (const struct nsh_hdr
*) *datap
;
539 uint8_t version
, length
, flags
, ttl
;
541 /* Check if it is long enough for NSH header, doesn't support
544 if (OVS_UNLIKELY(*sizep
< NSH_BASE_HDR_LEN
)) {
548 version
= nsh_get_ver(nsh
);
549 flags
= nsh_get_flags(nsh
);
550 length
= nsh_hdr_len(nsh
);
551 ttl
= nsh_get_ttl(nsh
);
553 if (OVS_UNLIKELY(length
> *sizep
|| version
!= 0)) {
559 key
->mdtype
= nsh
->md_type
;
560 key
->np
= nsh
->next_proto
;
561 key
->path_hdr
= nsh_get_path_hdr(nsh
);
563 switch (key
->mdtype
) {
565 if (length
!= NSH_M_TYPE1_LEN
) {
568 for (size_t i
= 0; i
< 4; i
++) {
569 key
->context
[i
] = get_16aligned_be32(&nsh
->md1
.context
[i
]);
573 /* Don't support MD type 2 metedata parsing yet */
574 if (length
< NSH_BASE_HDR_LEN
) {
578 memset(key
->context
, 0, sizeof(key
->context
));
581 /* We don't parse other context headers yet. */
582 memset(key
->context
, 0, sizeof(key
->context
));
586 data_pull(datap
, sizep
, length
);
591 /* Initializes 'flow' members from 'packet' and 'md', taking the packet type
594 * Initializes the layer offsets as follows:
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.
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.
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.
618 flow_extract(struct dp_packet
*packet
, struct flow
*flow
)
622 uint64_t buf
[FLOW_U64S
];
625 COVERAGE_INC(flow_extract
);
627 miniflow_extract(packet
, &m
.mf
);
628 miniflow_expand(&m
.mf
, flow
);
632 ipv4_sanity_check(const struct ip_header
*nh
, size_t size
,
633 int *ip_lenp
, uint16_t *tot_lenp
)
638 if (OVS_UNLIKELY(size
< IP_HEADER_LEN
)) {
641 ip_len
= IP_IHL(nh
->ip_ihl_ver
) * 4;
643 if (OVS_UNLIKELY(ip_len
< IP_HEADER_LEN
|| size
< ip_len
)) {
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
)) {
659 static inline uint8_t
660 ipv4_get_nw_frag(const struct ip_header
*nh
)
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
;
675 ipv6_sanity_check(const struct ovs_16aligned_ip6_hdr
*nh
, size_t size
)
679 if (OVS_UNLIKELY(size
< sizeof *nh
)) {
683 plen
= ntohs(nh
->ip6_plen
);
684 if (OVS_UNLIKELY(plen
+ IPV6_HEADER_LEN
> size
)) {
687 /* Jumbo Payload option not supported yet. */
688 if (OVS_UNLIKELY(size
- plen
> UINT8_MAX
)) {
695 /* Caller is responsible for initializing 'dst' with enough storage for
696 * FLOW_U64S * 8 bytes. */
698 miniflow_extract(struct dp_packet
*packet
, struct miniflow
*dst
)
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
};
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;
714 if (flow_tnl_dst_is_set(&md
->tunnel
)) {
715 miniflow_push_words(mf
, tunnel
, &md
->tunnel
,
716 offsetof(struct flow_tnl
, metadata
) /
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
/
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
,
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
);
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
));
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
);
754 miniflow_push_uint32(mf
, ct_mark
, md
->ct_mark
);
755 miniflow_push_be32(mf
, packet_type
, packet_type
);
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));
762 miniflow_pad_from_64(mf
, packet_type
);
763 miniflow_push_be32(mf
, packet_type
, packet_type
);
766 /* Initialize packet's layer pointer and offsets. */
768 dp_packet_reset_offsets(packet
);
770 if (packet_type
== htonl(PT_ETH
)) {
771 /* Must have full Ethernet header to proceed. */
772 if (OVS_UNLIKELY(size
< sizeof(struct eth_header
))) {
776 ASSERT_SEQUENTIAL(dl_dst
, dl_src
);
777 miniflow_push_macs(mf
, dl_dst
, data
);
780 union flow_vlan_hdr vlans
[FLOW_MAX_VLAN_HEADERS
];
781 size_t num_vlans
= parse_vlan(&data
, &size
, vlans
);
783 dl_type
= parse_ethertype(&data
, &size
);
784 miniflow_push_be16(mf
, dl_type
, dl_type
);
785 miniflow_pad_to_64(mf
, dl_type
);
787 miniflow_push_words_32(mf
, vlans
, vlans
, num_vlans
);
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
);
801 if (OVS_UNLIKELY(eth_type_mpls(dl_type
))) {
803 const void *mpls
= data
;
805 packet
->l2_5_ofs
= (char *)data
- frame
;
806 count
= parse_mpls(&data
, &size
);
807 miniflow_push_words_32(mf
, mpls_lse
, mpls
, count
);
811 packet
->l3_ofs
= (char *)data
- frame
;
814 if (OVS_LIKELY(dl_type
== htons(ETH_TYPE_IP
))) {
815 const struct ip_header
*nh
= data
;
819 if (OVS_UNLIKELY(!ipv4_sanity_check(nh
, size
, &ip_len
, &tot_len
))) {
822 dp_packet_set_l2_pad_size(packet
, size
- tot_len
);
823 size
= tot_len
; /* Never pull padding. */
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
;
837 miniflow_push_be32(mf
, ipv6_label
, 0); /* Padding for IPv4. */
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
;
849 if (OVS_UNLIKELY(!ipv6_sanity_check(nh
, size
))) {
852 data_pull(&data
, &size
, sizeof *nh
);
854 plen
= ntohs(nh
->ip6_plen
);
855 dp_packet_set_l2_pad_size(packet
, size
- plen
);
856 size
= plen
; /* Never pull padding. */
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
,
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
;
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
;
879 if (!parse_ipv6_ext_hdrs__(&data
, &size
, &nw_proto
, &nw_frag
)) {
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
);
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
);
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
));
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
)));
910 /* Must be adjacent. */
911 ASSERT_SEQUENTIAL(arp_sha
, arp_tha
);
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
);
918 } else if (dl_type
== htons(ETH_TYPE_NSH
)) {
919 struct ovs_key_nsh nsh
;
921 if (OVS_LIKELY(parse_nsh(&data
, &size
, &nsh
))) {
922 miniflow_push_words(mf
, nsh
, &nsh
,
923 sizeof(struct ovs_key_nsh
) /
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
));
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
;
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
);
947 } else if (OVS_LIKELY(nw_proto
== IPPROTO_UDP
)) {
948 if (OVS_LIKELY(size
>= UDP_HEADER_LEN
)) {
949 const struct udp_header
*udp
= data
;
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
);
956 } else if (OVS_LIKELY(nw_proto
== IPPROTO_SCTP
)) {
957 if (OVS_LIKELY(size
>= SCTP_HEADER_LEN
)) {
958 const struct sctp_header
*sctp
= data
;
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
);
965 } else if (OVS_LIKELY(nw_proto
== IPPROTO_ICMP
)) {
966 if (OVS_LIKELY(size
>= ICMP_HEADER_LEN
)) {
967 const struct icmp_header
*icmp
= data
;
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
);
974 } else if (OVS_LIKELY(nw_proto
== IPPROTO_IGMP
)) {
975 if (OVS_LIKELY(size
>= IGMP_HEADER_LEN
)) {
976 const struct igmp_header
*igmp
= data
;
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
);
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
,
992 if (parse_icmpv6(&data
, &size
, icmp
, &nd_target
, arp_buf
)) {
994 miniflow_push_words(mf
, nd_target
, nd_target
,
995 sizeof *nd_target
/ sizeof(uint64_t));
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
);
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
);
1017 parse_dl_type(const struct eth_header
*data_
, size_t size
)
1019 const void *data
= data_
;
1020 union flow_vlan_hdr vlans
[FLOW_MAX_VLAN_HEADERS
];
1022 parse_vlan(&data
, &size
, vlans
);
1024 return parse_ethertype(&data
, &size
);
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.
1030 * The caller must ensure that 'packet' is at least ETH_HEADER_LEN bytes
1033 parse_tcp_flags(struct dp_packet
*packet
)
1035 const void *data
= dp_packet_data(packet
);
1036 const char *frame
= (const char *)data
;
1037 size_t size
= dp_packet_size(packet
);
1039 uint8_t nw_frag
= 0, nw_proto
= 0;
1041 if (packet
->packet_type
!= htonl(PT_ETH
)) {
1045 dp_packet_reset_offsets(packet
);
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
;
1052 if (OVS_LIKELY(dl_type
== htons(ETH_TYPE_IP
))) {
1053 const struct ip_header
*nh
= data
;
1057 if (OVS_UNLIKELY(!ipv4_sanity_check(nh
, size
, &ip_len
, &tot_len
))) {
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
);
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
;
1071 if (OVS_UNLIKELY(!ipv6_sanity_check(nh
, size
))) {
1074 packet
->l3_ofs
= (uint16_t)((char *)nh
- frame
);
1075 data_pull(&data
, &size
, sizeof *nh
);
1077 plen
= ntohs(nh
->ip6_plen
); /* Never pull padding. */
1078 dp_packet_set_l2_pad_size(packet
, size
- plen
);
1080 if (!parse_ipv6_ext_hdrs__(&data
, &size
, &nw_proto
, &nw_frag
)) {
1083 nw_proto
= nh
->ip6_nxt
;
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
;
1093 return TCP_FLAGS(tcp
->tcp_ctl
);
1099 /* For every bit of a field that is wildcarded in 'wildcards', sets the
1100 * corresponding bit in 'flow' to zero. */
1102 flow_zero_wildcards(struct flow
*flow
, const struct flow_wildcards
*wildcards
)
1104 uint64_t *flow_u64
= (uint64_t *) flow
;
1105 const uint64_t *wc_u64
= (const uint64_t *) &wildcards
->masks
;
1108 for (i
= 0; i
< FLOW_U64S
; i
++) {
1109 flow_u64
[i
] &= wc_u64
[i
];
1114 flow_unwildcard_tp_ports(const struct flow
*flow
, struct flow_wildcards
*wc
)
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
);
1120 wc
->masks
.tp_src
= htons(0xff);
1121 wc
->masks
.tp_dst
= htons(0xff);
1125 /* Initializes 'flow_metadata' with the metadata found in 'flow'. */
1127 flow_get_metadata(const struct flow
*flow
, struct match
*flow_metadata
)
1131 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 41);
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
);
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
);
1141 if (flow
->tunnel
.ip_src
) {
1142 match_set_tun_src(flow_metadata
, flow
->tunnel
.ip_src
);
1144 if (flow
->tunnel
.ip_dst
) {
1145 match_set_tun_dst(flow_metadata
, flow
->tunnel
.ip_dst
);
1147 if (ipv6_addr_is_set(&flow
->tunnel
.ipv6_src
)) {
1148 match_set_tun_ipv6_src(flow_metadata
, &flow
->tunnel
.ipv6_src
);
1150 if (ipv6_addr_is_set(&flow
->tunnel
.ipv6_dst
)) {
1151 match_set_tun_ipv6_dst(flow_metadata
, &flow
->tunnel
.ipv6_dst
);
1153 if (flow
->tunnel
.gbp_id
!= htons(0)) {
1154 match_set_tun_gbp_id(flow_metadata
, flow
->tunnel
.gbp_id
);
1156 if (flow
->tunnel
.gbp_flags
) {
1157 match_set_tun_gbp_flags(flow_metadata
, flow
->tunnel
.gbp_flags
);
1159 if (flow
->tunnel
.erspan_ver
) {
1160 match_set_tun_erspan_ver(flow_metadata
, flow
->tunnel
.erspan_ver
);
1162 if (flow
->tunnel
.erspan_idx
) {
1163 match_set_tun_erspan_idx(flow_metadata
, flow
->tunnel
.erspan_idx
);
1165 if (flow
->tunnel
.erspan_dir
) {
1166 match_set_tun_erspan_dir(flow_metadata
, flow
->tunnel
.erspan_dir
);
1168 if (flow
->tunnel
.erspan_hwid
) {
1169 match_set_tun_erspan_hwid(flow_metadata
, flow
->tunnel
.erspan_hwid
);
1171 tun_metadata_get_fmd(&flow
->tunnel
, flow_metadata
);
1172 if (flow
->metadata
!= htonll(0)) {
1173 match_set_metadata(flow_metadata
, flow
->metadata
);
1176 for (i
= 0; i
< FLOW_N_REGS
; i
++) {
1177 if (flow
->regs
[i
]) {
1178 match_set_reg(flow_metadata
, i
, flow
->regs
[i
]);
1182 if (flow
->pkt_mark
!= 0) {
1183 match_set_pkt_mark(flow_metadata
, flow
->pkt_mark
);
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
);
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
);
1212 if (flow
->ct_zone
!= 0) {
1213 match_set_ct_zone(flow_metadata
, flow
->ct_zone
);
1215 if (flow
->ct_mark
!= 0) {
1216 match_set_ct_mark(flow_metadata
, flow
->ct_mark
);
1218 if (!ovs_u128_is_zero(flow
->ct_label
)) {
1219 match_set_ct_label(flow_metadata
, flow
->ct_label
);
1224 ct_state_to_string(uint32_t state
)
1227 #define CS_STATE(ENUM, INDEX, NAME) case CS_##ENUM: return NAME;
1236 ct_state_from_string(const char *s
)
1238 #define CS_STATE(ENUM, INDEX, NAME) \
1239 if (!strcmp(s, NAME)) { \
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'. */
1251 parse_ct_state(const char *state_str
, uint32_t default_state
,
1252 uint32_t *ct_state
, struct ds
*ds
)
1254 uint32_t state
= default_state
;
1255 char *state_s
= xstrdup(state_str
);
1256 char *save_ptr
= NULL
;
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
);
1262 ds_put_format(ds
, "%s: unknown connection tracking state flag",
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'. */
1279 validate_ct_state(uint32_t state
, struct ds
*ds
)
1281 bool valid_ct_state
= true;
1282 struct ds d_str
= DS_EMPTY_INITIALIZER
;
1284 format_flags(&d_str
, ct_state_to_string
, state
, '|');
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",
1290 valid_ct_state
= false;
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",
1296 valid_ct_state
= false;
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",
1302 valid_ct_state
= false;
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",
1308 valid_ct_state
= false;
1312 return valid_ct_state
;
1315 /* Clears the fields in 'flow' associated with connection tracking. */
1317 flow_clear_conntrack(struct flow
*flow
)
1322 flow
->ct_label
= OVS_U128_ZERO
;
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
);
1337 flow_to_string(const struct flow
*flow
,
1338 const struct ofputil_port_map
*port_map
)
1340 struct ds ds
= DS_EMPTY_INITIALIZER
;
1341 flow_format(&ds
, flow
, port_map
);
1342 return ds_cstr(&ds
);
1346 flow_tun_flag_to_string(uint32_t flags
)
1349 case FLOW_TNL_F_DONT_FRAGMENT
:
1351 case FLOW_TNL_F_CSUM
:
1353 case FLOW_TNL_F_KEY
:
1355 case FLOW_TNL_F_OAM
:
1363 format_flags(struct ds
*ds
, const char *(*bit_to_string
)(uint32_t),
1364 uint32_t flags
, char del
)
1369 ds_put_char(ds
, '0');
1373 uint32_t bit
= rightmost_1bit(flags
);
1376 s
= bit_to_string(bit
);
1378 ds_put_format(ds
, "%s%c", s
, del
);
1387 ds_put_format(ds
, "0x%"PRIx32
"%c", bad
, del
);
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
)
1398 ds_put_format(ds
, "%s%s=%s", colors
.param
, name
, colors
.end
);
1401 if (mask
== max_mask
) {
1402 format_flags(ds
, bit_to_string
, flags
, '|');
1407 ds_put_cstr(ds
, "0/0");
1412 uint32_t bit
= rightmost_1bit(mask
);
1413 const char *s
= bit_to_string(bit
);
1415 ds_put_format(ds
, "%s%s", (flags
& bit
) ? "+" : "-",
1416 s
? s
: "[Unknown]");
1422 put_u16_masked(struct ds
*s
, uint16_t value
, uint16_t mask
)
1425 ds_put_char(s
, '*');
1428 ds_put_format(s
, "0x%"PRIx16
, value
);
1430 ds_put_format(s
, "%"PRIu16
, value
);
1433 if (mask
!= UINT16_MAX
) {
1434 ds_put_format(s
, "/0x%"PRIx16
, mask
);
1440 format_packet_type_masked(struct ds
*s
, ovs_be32 value
, ovs_be32 mask
)
1442 if (value
== htonl(PT_ETH
) && mask
== OVS_BE32_MAX
) {
1443 ds_put_cstr(s
, "eth");
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
, ')');
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
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.
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
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
)
1473 uint32_t result
= 0;
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
) {
1487 if (res_mask
&& (*s
== '+' || *s
== '-')) {
1488 uint32_t flags
= 0, mask
= 0;
1490 /* Parse masked flags. */
1491 while (s
[0] != end
) {
1498 } else if (s
[0] == '-') {
1502 *res_string
= xasprintf("%s: %s must be preceded by '+' "
1503 "(for SET) or '-' (NOT SET)", s
,
1511 for (bit
= 1; bit
; bit
<<= 1) {
1512 const char *fname
= bit_to_string(bit
);
1518 len
= strlen(fname
);
1519 if (strncmp(s
, fname
, len
) ||
1520 (s
[len
] != '+' && s
[len
] != '-' && s
[len
] != end
)) {
1525 /* bit already set. */
1527 *res_string
= xasprintf("%s: Each %s flag can be "
1528 "specified only once", s
,
1533 if (!(bit
& allowed
)) {
1555 /* Parse unmasked flags. If a flag is present, it is set, otherwise
1557 while (s
[n
] != end
) {
1558 unsigned long long int flags
;
1562 if (ovs_scan(&s
[n
], "%lli%n", &flags
, &n0
)) {
1563 if (flags
& ~allowed
) {
1566 n
+= n0
+ (s
[n
+ n0
] == '|');
1571 for (bit
= 1; bit
; bit
<<= 1) {
1572 const char *name
= bit_to_string(bit
);
1580 if (!strncmp(s
+ n
, name
, len
) &&
1581 (s
[n
+ len
] == '|' || s
[n
+ len
] == end
)) {
1582 if (!(bit
& allowed
)) {
1586 n
+= len
+ (s
[n
+ len
] == '|');
1596 *res_flags
= result
;
1598 *res_mask
= UINT32_MAX
;
1607 *res_string
= xasprintf("%s: unknown %s flag(s)", s
, field_name
);
1613 flow_format(struct ds
*ds
,
1614 const struct flow
*flow
, const struct ofputil_port_map
*port_map
)
1617 struct flow_wildcards
*wc
= &match
.wc
;
1619 match_wc_init(&match
, flow
);
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
);
1631 if (!flow
->skb_priority
) {
1632 WC_UNMASK_FIELD(wc
, skb_priority
);
1634 if (!flow
->pkt_mark
) {
1635 WC_UNMASK_FIELD(wc
, pkt_mark
);
1637 if (!flow
->recirc_id
) {
1638 WC_UNMASK_FIELD(wc
, recirc_id
);
1640 if (!flow
->dp_hash
) {
1641 WC_UNMASK_FIELD(wc
, dp_hash
);
1643 if (!flow
->ct_state
) {
1644 WC_UNMASK_FIELD(wc
, ct_state
);
1646 if (!flow
->ct_zone
) {
1647 WC_UNMASK_FIELD(wc
, ct_zone
);
1649 if (!flow
->ct_mark
) {
1650 WC_UNMASK_FIELD(wc
, ct_mark
);
1652 if (ovs_u128_is_zero(flow
->ct_label
)) {
1653 WC_UNMASK_FIELD(wc
, ct_label
);
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
);
1667 for (int i
= 0; i
< FLOW_N_REGS
; i
++) {
1668 if (!flow
->regs
[i
]) {
1669 WC_UNMASK_FIELD(wc
, regs
[i
]);
1672 if (!flow
->metadata
) {
1673 WC_UNMASK_FIELD(wc
, metadata
);
1676 match_format(&match
, port_map
, ds
, OFP_DEFAULT_PRIORITY
);
1680 flow_print(FILE *stream
,
1681 const struct flow
*flow
, const struct ofputil_port_map
*port_map
)
1683 char *s
= flow_to_string(flow
, port_map
);
1688 /* flow_wildcards functions. */
1690 /* Initializes 'wc' as a set of wildcards that matches every packet. */
1692 flow_wildcards_init_catchall(struct flow_wildcards
*wc
)
1694 memset(&wc
->masks
, 0, sizeof wc
->masks
);
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. */
1702 flow_wildcards_init_for_packet(struct flow_wildcards
*wc
,
1703 const struct flow
*flow
)
1705 ovs_be16 dl_type
= OVS_BE16_MAX
;
1707 memset(&wc
->masks
, 0x0, sizeof wc
->masks
);
1709 /* Update this function whenever struct flow changes. */
1710 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 41);
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
);
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
);
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
);
1740 WC_MASK_FIELD(wc
, tunnel
.metadata
.present
.len
);
1741 memset(wc
->masks
.tunnel
.metadata
.opts
.gnv
, 0xff,
1742 flow
->tunnel
.metadata
.present
.len
);
1744 } else if (flow
->tunnel
.tun_id
) {
1745 WC_MASK_FIELD(wc
, tunnel
.tun_id
);
1748 /* metadata, regs, and conj_id wildcarded. */
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
);
1760 /* actset_output wildcarded. */
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)) {
1775 dl_type
= flow
->dl_type
;
1777 dl_type
= pt_ns_type_be(flow
->packet_type
);
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
);
1794 WC_MASK_FIELD(wc
, ct_ipv6_src
);
1795 WC_MASK_FIELD(wc
, ct_ipv6_dst
);
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
);
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
)) {
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
);
1821 return; /* Unknown ethertype. */
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
);
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
);
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
);
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
1856 * This is a less precise version of flow_wildcards_init_for_packet() above. */
1858 flow_wc_map(const struct flow
*flow
, struct flowmap
*map
)
1860 /* Update this function whenever struct flow changes. */
1861 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 41);
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
);
1872 FLOWMAP_SET(map
, tunnel
.metadata
.present
.len
);
1873 FLOWMAP_SET__(map
, tunnel
.metadata
.opts
.gnv
,
1874 flow
->tunnel
.metadata
.present
.len
);
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
);
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
);
1910 if (OVS_UNLIKELY(flow
->nw_proto
== IPPROTO_IGMP
)) {
1911 FLOWMAP_SET(map
, igmp_group_ip4
);
1913 FLOWMAP_SET(map
, tcp_flags
);
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
);
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
);
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
);
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
);
1956 /* Clear the metadata and register wildcard masks. They are not packet
1959 flow_wildcards_clear_non_packet_fields(struct flow_wildcards
*wc
)
1961 /* Update this function whenever struct flow changes. */
1962 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 41);
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;
1970 /* Returns true if 'wc' matches every packet, false if 'wc' fixes any bits or
1973 flow_wildcards_is_catchall(const struct flow_wildcards
*wc
)
1975 const uint64_t *wc_u64
= (const uint64_t *) &wc
->masks
;
1978 for (i
= 0; i
< FLOW_U64S
; i
++) {
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. */
1990 flow_wildcards_and(struct flow_wildcards
*dst
,
1991 const struct flow_wildcards
*src1
,
1992 const struct flow_wildcards
*src2
)
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
;
1999 for (i
= 0; i
< FLOW_U64S
; i
++) {
2000 dst_u64
[i
] = src1_u64
[i
] & src2_u64
[i
];
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'. */
2008 flow_wildcards_or(struct flow_wildcards
*dst
,
2009 const struct flow_wildcards
*src1
,
2010 const struct flow_wildcards
*src2
)
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
;
2017 for (i
= 0; i
< FLOW_U64S
; i
++) {
2018 dst_u64
[i
] = src1_u64
[i
] | src2_u64
[i
];
2022 /* Returns a hash of the wildcards in 'wc'. */
2024 flow_wildcards_hash(const struct flow_wildcards
*wc
, uint32_t basis
)
2026 return flow_hash(&wc
->masks
, basis
);
2029 /* Returns true if 'a' and 'b' represent the same wildcards, false if they are
2032 flow_wildcards_equal(const struct flow_wildcards
*a
,
2033 const struct flow_wildcards
*b
)
2035 return flow_equal(&a
->masks
, &b
->masks
);
2038 /* Returns true if at least one bit or field is wildcarded in 'a' but not in
2039 * 'b', false otherwise. */
2041 flow_wildcards_has_extra(const struct flow_wildcards
*a
,
2042 const struct flow_wildcards
*b
)
2044 const uint64_t *a_u64
= (const uint64_t *) &a
->masks
;
2045 const uint64_t *b_u64
= (const uint64_t *) &b
->masks
;
2048 for (i
= 0; i
< FLOW_U64S
; i
++) {
2049 if ((a_u64
[i
] & b_u64
[i
]) != b_u64
[i
]) {
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'. */
2059 flow_equal_except(const struct flow
*a
, const struct flow
*b
,
2060 const struct flow_wildcards
*wc
)
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
;
2067 for (i
= 0; i
< FLOW_U64S
; i
++) {
2068 if ((a_u64
[i
] ^ b_u64
[i
]) & wc_u64
[i
]) {
2075 /* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
2076 * (A 0-bit indicates a wildcard bit.) */
2078 flow_wildcards_set_reg_mask(struct flow_wildcards
*wc
, int idx
, uint32_t mask
)
2080 wc
->masks
.regs
[idx
] = mask
;
2083 /* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
2084 * (A 0-bit indicates a wildcard bit.) */
2086 flow_wildcards_set_xreg_mask(struct flow_wildcards
*wc
, int idx
, uint64_t mask
)
2088 flow_set_xreg(&wc
->masks
, idx
, mask
);
2091 /* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
2092 * (A 0-bit indicates a wildcard bit.) */
2094 flow_wildcards_set_xxreg_mask(struct flow_wildcards
*wc
, int idx
,
2097 flow_set_xxreg(&wc
->masks
, idx
, mask
);
2100 /* Calculates the 5-tuple hash from the given miniflow.
2101 * This returns the same value as flow_hash_5tuple for the corresponding
2104 miniflow_hash_5tuple(const struct miniflow
*flow
, uint32_t basis
)
2106 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 41);
2107 uint32_t hash
= basis
;
2110 ovs_be16 dl_type
= MINIFLOW_GET_BE16(flow
, dl_type
);
2113 if (dl_type
== htons(ETH_TYPE_IPV6
)) {
2114 struct flowmap map
= FLOWMAP_EMPTY_INITIALIZER
;
2117 FLOWMAP_SET(&map
, ipv6_src
);
2118 FLOWMAP_SET(&map
, ipv6_dst
);
2120 MINIFLOW_FOR_EACH_IN_FLOWMAP(value
, flow
, map
) {
2121 hash
= hash_add64(hash
, value
);
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
));
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
) {
2139 /* Add both ports at once. */
2140 hash
= hash_add(hash
, (OVS_FORCE
uint32_t) miniflow_get_ports(flow
));
2143 return hash_finish(hash
, 42);
2146 ASSERT_SEQUENTIAL_SAME_WORD(tp_src
, tp_dst
);
2147 ASSERT_SEQUENTIAL(ipv6_src
, ipv6_dst
);
2149 /* Calculates the 5-tuple hash from the given flow. */
2151 flow_hash_5tuple(const struct flow
*flow
, uint32_t basis
)
2153 BUILD_ASSERT_DECL(FLOW_WC_SEQ
== 41);
2154 uint32_t hash
= basis
;
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;
2163 for (;ofs
< end
; ofs
++) {
2164 hash
= hash_add64(hash
, flow_u64
[ofs
]);
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
);
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
) {
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)]);
2187 return hash_finish(hash
, 42); /* Arbitrary number. */
2190 /* Hashes 'flow' based on its L2 through L4 protocol information. */
2192 flow_hash_symmetric_l4(const struct flow
*flow
, uint32_t basis
)
2197 struct in6_addr ipv6_addr
;
2202 struct eth_addr eth_addr
;
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
];
2212 for (i
= 0; i
< FLOW_MAX_VLAN_HEADERS
; i
++) {
2213 fields
.vlan_tci
^= flow
->vlans
[i
].tci
& htons(VLAN_VID_MASK
);
2215 fields
.eth_type
= flow
->dl_type
;
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
;
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];
2230 for (i
=0; i
<16; i
++) {
2231 ipv6_addr
[i
] = a
[i
] ^ b
[i
];
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
;
2238 return jhash_bytes(&fields
, sizeof fields
, basis
);
2241 /* Symmetrically Hashes non-IP 'flow' based on its L2 headers. */
2243 flow_hash_symmetric_l2(const struct flow
*flow
, uint32_t basis
)
2249 struct eth_addr eth_addr
;
2255 uint32_t hash
= basis
;
2258 if (flow
->packet_type
!= htonl(PT_ETH
)) {
2259 /* Cannot hash non-Ethernet flows */
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
];
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
);
2271 fields
.eth_type
= flow
->dl_type
;
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
);
2280 /* Hashes 'flow' based on its L3 through L4 protocol information */
2282 flow_hash_symmetric_l3l4(const struct flow
*flow
, uint32_t basis
,
2285 uint32_t hash
= basis
;
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
);
2296 for (int i
= 0; i
< sizeof flow
->ipv6_src
/ sizeof *a
; i
++) {
2297 hash
= hash_add64(hash
, a
[i
] ^ b
[i
]);
2300 /* Revert to hashing L2 headers */
2301 return flow_hash_symmetric_l2(flow
, basis
);
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
));
2311 return hash_finish(hash
, basis
);
2314 /* Hashes 'flow' based on its nw_dst and nw_src for multipath. */
2316 flow_hash_symmetric_l3(const struct flow
*flow
, uint32_t basis
)
2321 struct in6_addr ipv6_addr
;
2328 memset(&fields
, 0, sizeof fields
);
2329 fields
.eth_type
= flow
->dl_type
;
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];
2338 for (i
= 0; i
< 16; i
++) {
2339 ipv6_addr
[i
] = a
[i
] ^ b
[i
];
2342 return jhash_bytes(&fields
, sizeof fields
, basis
);
2345 /* Initialize a flow with random fields that matter for nx_hash_fields. */
2347 flow_random_hash_fields(struct flow
*flow
)
2349 uint16_t rnd
= random_uint16();
2352 /* Initialize to all zeros. */
2353 memset(flow
, 0, sizeof *flow
);
2355 eth_addr_random(&flow
->dl_src
);
2356 eth_addr_random(&flow
->dl_dst
);
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
);
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
;
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();
2373 random_bytes(&flow
->ipv6_src
, sizeof flow
->ipv6_src
);
2374 random_bytes(&flow
->ipv6_dst
, sizeof flow
->ipv6_dst
);
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();
2390 /* Masks the fields in 'wc' that are used by the flow hash 'fields'. */
2392 flow_mask_hash_fields(const struct flow
*flow
, struct flow_wildcards
*wc
,
2393 enum nx_hash_fields fields
)
2397 case NX_HASH_FIELDS_ETH_SRC
:
2398 memset(&wc
->masks
.dl_src
, 0xff, sizeof wc
->masks
.dl_src
);
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
);
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
);
2415 for (i
= 0; i
< FLOW_MAX_VLAN_HEADERS
; i
++) {
2416 wc
->masks
.vlans
[i
].tci
|= htons(VLAN_VID_MASK
| VLAN_CFI
);
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
);
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
);
2434 break; /* non-IP flow */
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
);
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
);
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
);
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
);
2475 /* Hashes the portions of 'flow' designated by 'fields'. */
2477 flow_hash_fields(const struct flow
*flow
, enum nx_hash_fields fields
,
2482 case NX_HASH_FIELDS_ETH_SRC
:
2483 return jhash_bytes(&flow
->dl_src
, sizeof flow
->dl_src
, basis
);
2485 case NX_HASH_FIELDS_SYMMETRIC_L4
:
2486 return flow_hash_symmetric_l4(flow
, basis
);
2488 case NX_HASH_FIELDS_SYMMETRIC_L3L4
:
2489 return flow_hash_symmetric_l3l4(flow
, basis
, false);
2491 case NX_HASH_FIELDS_SYMMETRIC_L3L4_UDP
:
2492 return flow_hash_symmetric_l3l4(flow
, basis
, true);
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
);
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
);
2512 case NX_HASH_FIELDS_SYMMETRIC_L3
:
2513 return flow_hash_symmetric_l3(flow
, basis
);
2519 /* Returns a string representation of 'fields'. */
2521 flow_hash_fields_to_str(enum nx_hash_fields 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>";
2535 /* Returns true if the value of 'fields' is supported. Otherwise false. */
2537 flow_hash_fields_valid(enum nx_hash_fields fields
)
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
;
2548 /* Returns a hash value for the bits of 'flow' that are active based on
2549 * 'wc', given 'basis'. */
2551 flow_hash_in_wildcards(const struct flow
*flow
,
2552 const struct flow_wildcards
*wc
, uint32_t basis
)
2554 const uint64_t *wc_u64
= (const uint64_t *) &wc
->masks
;
2555 const uint64_t *flow_u64
= (const uint64_t *) flow
;
2560 for (i
= 0; i
< FLOW_U64S
; i
++) {
2561 hash
= hash_add64(hash
, flow_u64
[i
] & wc_u64
[i
]);
2563 return hash_finish(hash
, 8 * FLOW_U64S
);
2566 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
2567 * OpenFlow 1.0 "dl_vlan" value:
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).
2573 * - If it is OFP_VLAN_NONE, 'flow->vlan_tci' is set to match a packet
2574 * without a VLAN tag.
2576 * - Other values of 'vid' should not be used. */
2578 flow_set_dl_vlan(struct flow
*flow
, ovs_be16 vid
, int id
)
2580 if (vid
== htons(OFP10_VLAN_NONE
)) {
2581 flow
->vlans
[id
].tci
= htons(0);
2583 vid
&= htons(VLAN_VID_MASK
);
2584 flow
->vlans
[id
].tci
&= ~htons(VLAN_VID_MASK
);
2585 flow
->vlans
[id
].tci
|= htons(VLAN_CFI
) | vid
;
2589 /* Sets the VLAN header TPID, which must be either ETH_TYPE_VLAN_8021Q or
2590 * ETH_TYPE_VLAN_8021AD. */
2592 flow_fix_vlan_tpid(struct flow
*flow
)
2594 if (flow
->vlans
[0].tpid
== htons(0) && flow
->vlans
[0].tci
!= 0) {
2595 flow
->vlans
[0].tpid
= htons(ETH_TYPE_VLAN_8021Q
);
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
2603 flow_set_vlan_vid(struct flow
*flow
, ovs_be16 vid
)
2605 ovs_be16 mask
= htons(VLAN_VID_MASK
| VLAN_CFI
);
2606 flow
->vlans
[0].tci
&= ~mask
;
2607 flow
->vlans
[0].tci
|= vid
& mask
;
2610 /* Sets the VLAN PCP that 'flow' matches to 'pcp', which should be in the
2613 * This function has no effect on the VLAN ID that 'flow' matches.
2615 * After calling this function, 'flow' will not match packets without a VLAN
2618 flow_set_vlan_pcp(struct flow
*flow
, uint8_t pcp
, int id
)
2621 flow
->vlans
[id
].tci
&= ~htons(VLAN_PCP_MASK
);
2622 flow
->vlans
[id
].tci
|= htons((pcp
<< VLAN_PCP_SHIFT
) | VLAN_CFI
);
2625 /* Counts the number of VLAN headers. */
2627 flow_count_vlan_headers(const struct flow
*flow
)
2631 for (i
= 0; i
< FLOW_MAX_VLAN_HEADERS
; i
++) {
2632 if (!(flow
->vlans
[i
].tci
& htons(VLAN_CFI
))) {
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. */
2643 flow_skip_common_vlan_headers(const struct flow
*a
, int *p_an
,
2644 const struct flow
*b
, int *p_bn
)
2646 int an
= *p_an
, bn
= *p_bn
;
2648 for (an
--, bn
--; an
>= 0 && bn
>= 0; an
--, bn
--) {
2649 if (a
->vlans
[an
].qtag
!= b
->vlans
[bn
].qtag
) {
2658 flow_pop_vlan(struct flow
*flow
, struct flow_wildcards
*wc
)
2660 int n
= flow_count_vlan_headers(flow
);
2663 memset(&wc
->masks
.vlans
[1], 0xff,
2664 sizeof(union flow_vlan_hdr
) * (n
- 1));
2666 memmove(&flow
->vlans
[0], &flow
->vlans
[1],
2667 sizeof(union flow_vlan_hdr
) * (n
- 1));
2670 memset(&flow
->vlans
[n
- 1], 0, sizeof(union flow_vlan_hdr
));
2675 flow_push_vlan_uninit(struct flow
*flow
, struct flow_wildcards
*wc
)
2678 int n
= flow_count_vlan_headers(flow
);
2680 memset(wc
->masks
.vlans
, 0xff, sizeof(union flow_vlan_hdr
) * n
);
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
));
2688 /* Returns the number of MPLS LSEs present in 'flow'
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.
2696 flow_count_mpls_labels(const struct flow
*flow
, struct flow_wildcards
*wc
)
2698 /* dl_type is always masked. */
2699 if (eth_type_mpls(flow
->dl_type
)) {
2704 for (i
= 0; i
< FLOW_MAX_MPLS_LABELS
; i
++) {
2706 wc
->masks
.mpls_lse
[i
] |= htonl(MPLS_BOS_MASK
);
2708 if (flow
->mpls_lse
[i
] & htonl(MPLS_BOS_MASK
)) {
2711 if (flow
->mpls_lse
[i
]) {
2721 /* Returns the number consecutive of MPLS LSEs, starting at the
2722 * innermost LSE, that are common in 'a' and 'b'.
2724 * 'an' must be flow_count_mpls_labels(a).
2725 * 'bn' must be flow_count_mpls_labels(b).
2728 flow_count_common_mpls_labels(const struct flow
*a
, int an
,
2729 const struct flow
*b
, int bn
,
2730 struct flow_wildcards
*wc
)
2732 int min_n
= MIN(an
, bn
);
2737 int a_last
= an
- 1;
2738 int b_last
= bn
- 1;
2741 for (i
= 0; i
< min_n
; i
++) {
2743 wc
->masks
.mpls_lse
[a_last
- i
] = OVS_BE32_MAX
;
2744 wc
->masks
.mpls_lse
[b_last
- i
] = OVS_BE32_MAX
;
2746 if (a
->mpls_lse
[a_last
- i
] != b
->mpls_lse
[b_last
- i
]) {
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.
2760 * If the new label is the first MPLS label in 'flow', it is generated as;
2762 * - label: 2, if 'flow' is IPv6, otherwise 0.
2764 * - TTL: IPv4 or IPv6 TTL, if present and nonzero, otherwise 64.
2766 * - TC: IPv4 or IPv6 TOS, if present, otherwise 0.
2770 * If the new label is the second or later label MPLS label in 'flow', it is
2773 * - label: Copied from outer label.
2775 * - TTL: Copied from outer label.
2777 * - TC: Copied from outer label.
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).
2785 flow_push_mpls(struct flow
*flow
, int n
, ovs_be16 mpls_eth_type
,
2786 struct flow_wildcards
*wc
, bool clear_flow_L3
)
2788 ovs_assert(eth_type_mpls(mpls_eth_type
));
2789 ovs_assert(n
< FLOW_MAX_MPLS_LABELS
);
2795 memset(&wc
->masks
.mpls_lse
, 0xff, sizeof *wc
->masks
.mpls_lse
* n
);
2797 for (i
= n
; i
>= 1; i
--) {
2798 flow
->mpls_lse
[i
] = flow
->mpls_lse
[i
- 1];
2800 flow
->mpls_lse
[0] = (flow
->mpls_lse
[1] & htonl(~MPLS_BOS_MASK
));
2802 int label
= 0; /* IPv4 Explicit Null. */
2806 if (flow
->dl_type
== htons(ETH_TYPE_IPV6
)) {
2810 if (is_ip_any(flow
)) {
2811 tc
= (flow
->nw_tos
& IP_DSCP_MASK
) >> 2;
2813 wc
->masks
.nw_tos
|= IP_DSCP_MASK
;
2814 wc
->masks
.nw_ttl
= 0xff;
2822 flow
->mpls_lse
[0] = set_mpls_lse_values(ttl
, tc
, 1, htonl(label
));
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
);
2832 flow
->dl_type
= mpls_eth_type
;
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
2839 * 'n' must be flow_count_mpls_labels(flow). */
2841 flow_pop_mpls(struct flow
*flow
, int n
, ovs_be16 eth_type
,
2842 struct flow_wildcards
*wc
)
2847 /* Nothing to pop. */
2849 } else if (n
== FLOW_MAX_MPLS_LABELS
) {
2851 wc
->masks
.mpls_lse
[n
- 1] |= htonl(MPLS_BOS_MASK
);
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]. */
2860 memset(&wc
->masks
.mpls_lse
[1], 0xff,
2861 sizeof *wc
->masks
.mpls_lse
* (n
- 1));
2863 for (i
= 1; i
< n
; i
++) {
2864 flow
->mpls_lse
[i
- 1] = flow
->mpls_lse
[i
];
2866 flow
->mpls_lse
[n
- 1] = 0;
2867 flow
->dl_type
= eth_type
;
2871 /* Sets the MPLS Label that 'flow' matches to 'label', which is interpreted
2872 * as an OpenFlow 1.1 "mpls_label" value. */
2874 flow_set_mpls_label(struct flow
*flow
, int idx
, ovs_be32 label
)
2876 set_mpls_lse_label(&flow
->mpls_lse
[idx
], label
);
2879 /* Sets the MPLS TTL that 'flow' matches to 'ttl', which should be in the
2882 flow_set_mpls_ttl(struct flow
*flow
, int idx
, uint8_t ttl
)
2884 set_mpls_lse_ttl(&flow
->mpls_lse
[idx
], ttl
);
2887 /* Sets the MPLS TC that 'flow' matches to 'tc', which should be in the
2890 flow_set_mpls_tc(struct flow
*flow
, int idx
, uint8_t tc
)
2892 set_mpls_lse_tc(&flow
->mpls_lse
[idx
], tc
);
2895 /* Sets the MPLS BOS bit that 'flow' matches to which should be 0 or 1. */
2897 flow_set_mpls_bos(struct flow
*flow
, int idx
, uint8_t bos
)
2899 set_mpls_lse_bos(&flow
->mpls_lse
[idx
], bos
);
2902 /* Sets the entire MPLS LSE. */
2904 flow_set_mpls_lse(struct flow
*flow
, int idx
, ovs_be32 lse
)
2906 flow
->mpls_lse
[idx
] = lse
;
2910 flow_compose_l7(struct dp_packet
*p
, const void *l7
, size_t l7_len
)
2914 dp_packet_put(p
, l7
, l7_len
);
2916 uint8_t *payload
= dp_packet_put_uninit(p
, l7_len
);
2917 for (size_t i
= 0; i
< l7_len
; i
++) {
2925 flow_compose_l4(struct dp_packet
*p
, const struct flow
*flow
,
2926 const void *l7
, size_t l7_len
)
2928 size_t orig_len
= dp_packet_size(p
);
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
);
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
);
2960 /* XXX Add inner IP packet for e.g. destination unreachable? */
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
);
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
;
2978 nd_target
= dp_packet_put_zeros(p
, sizeof *nd_target
);
2979 *nd_target
= flow
->nd_target
;
2981 if (!eth_addr_is_zero(flow
->arp_sha
)) {
2982 lla_opt
= dp_packet_put_zeros(p
, 8);
2984 lla_opt
->type
= ND_OPT_SOURCE_LINKADDR
;
2985 lla_opt
->mac
= flow
->arp_sha
;
2987 if (!eth_addr_is_zero(flow
->arp_tha
)) {
2988 lla_opt
= dp_packet_put_zeros(p
, 8);
2990 lla_opt
->type
= ND_OPT_TARGET_LINKADDR
;
2991 lla_opt
->mac
= flow
->arp_tha
;
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
);
2998 /* XXX Add inner IP packet for e.g. destination unreachable? */
3003 return dp_packet_size(p
) - orig_len
;
3007 flow_compose_l4_csum(struct dp_packet
*p
, const struct flow
*flow
,
3008 uint32_t pseudo_hdr_csum
)
3010 size_t l4_len
= (char *) dp_packet_tail(p
) - (char *) dp_packet_l4(p
);
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
);
3018 tcp
->tcp_csum
= csum_finish(csum_continue(pseudo_hdr_csum
,
3020 } else if (flow
->nw_proto
== IPPROTO_UDP
) {
3021 struct udp_header
*udp
= dp_packet_l4(p
);
3024 udp
->udp_csum
= csum_finish(csum_continue(pseudo_hdr_csum
,
3026 } else if (flow
->nw_proto
== IPPROTO_ICMP
) {
3027 struct icmp_header
*icmp
= dp_packet_l4(p
);
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
);
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
);
3039 icmp
->icmp6_cksum
= 0;
3040 icmp
->icmp6_cksum
= (OVS_FORCE
uint16_t)
3041 csum_finish(csum_continue(pseudo_hdr_csum
, icmp
, l4_len
));
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.
3050 * 'size' needs to be larger then the current packet size. */
3052 packet_expand(struct dp_packet
*p
, const struct flow
*flow
, size_t size
)
3056 ovs_assert(size
> dp_packet_size(p
));
3058 extra_size
= size
- dp_packet_size(p
);
3059 dp_packet_put_zeros(p
, extra_size
);
3061 if (flow
->dl_type
== htons(FLOW_DL_TYPE_NONE
)) {
3062 struct eth_header
*eth
= dp_packet_eth(p
);
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
);
3069 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
3070 struct ip_header
*ip
= dp_packet_l3(p
);
3072 ip
->ip_tot_len
= htons(p
->l4_ofs
- p
->l3_ofs
+ l4_len
);
3074 ip
->ip_csum
= csum(ip
, sizeof *ip
);
3076 pseudo_hdr_csum
= packet_csum_pseudoheader(ip
);
3077 } else { /* ETH_TYPE_IPV6 */
3078 struct ovs_16aligned_ip6_hdr
*nh
= dp_packet_l3(p
);
3080 nh
->ip6_plen
= htons(l4_len
);
3081 pseudo_hdr_csum
= packet_csum_pseudoheader6(nh
);
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
);
3089 udp
->udp_len
= htons(l4_len
+ extra_size
);
3091 flow_compose_l4_csum(p
, flow
, pseudo_hdr_csum
);
3095 /* Puts into 'p' a packet that flow_extract() would parse as having the given
3098 * (This is useful only for testing, obviously, and the packet isn't really
3099 * valid. Lots of fields are just zeroed.)
3101 * For packets whose protocols can encapsulate arbitrary L7 payloads, 'l7' and
3102 * 'l7_len' determine that payload:
3104 * - If 'l7_len' is zero, no payload is included.
3106 * - If 'l7_len' is nonzero and 'l7' is null, an arbitrary payload 'l7_len'
3107 * bytes long is included.
3109 * - If 'l7_len' is nonzero and 'l7' is nonnull, the payload is copied
3112 flow_compose(struct dp_packet
*p
, const struct flow
*flow
,
3113 const void *l7
, size_t l7_len
)
3115 uint32_t pseudo_hdr_csum
;
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
));
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
);
3133 if (flow
->dl_type
== htons(ETH_TYPE_IP
)) {
3134 struct ip_header
*ip
;
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
);
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);
3151 dp_packet_set_l4(p
, dp_packet_tail(p
));
3153 l4_len
= flow_compose_l4(p
, flow
, l7
, l7_len
);
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
);
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
;
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
;
3171 memcpy(&nh
->ip6_src
, &flow
->ipv6_src
, sizeof(nh
->ip6_src
));
3172 memcpy(&nh
->ip6_dst
, &flow
->ipv6_dst
, sizeof(nh
->ip6_dst
));
3174 dp_packet_set_l4(p
, dp_packet_tail(p
));
3176 l4_len
= flow_compose_l4(p
, flow
, l7
, l7_len
);
3178 nh
= dp_packet_l3(p
);
3179 nh
->ip6_plen
= htons(l4_len
);
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
;
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
;
3193 arp
->ar_op
= htons(flow
->nw_proto
);
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
;
3204 if (eth_type_mpls(flow
->dl_type
)) {
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
)) {
3214 push_mpls(p
, flow
->dl_type
, flow
->mpls_lse
[--n
]);
3219 /* Compressed flow. */
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'.
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. */
3229 miniflow_init(struct miniflow
*dst
, const struct flow
*src
)
3231 uint64_t *dst_u64
= miniflow_values(dst
);
3234 FLOWMAP_FOR_EACH_INDEX(idx
, dst
->map
) {
3235 *dst_u64
++ = flow_u64_value(src
, idx
);
3239 /* Initialize the maps of 'flow' from 'src'. */
3241 miniflow_map_init(struct miniflow
*flow
, const struct flow
*src
)
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);
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. */
3256 miniflow_alloc(struct miniflow
*dsts
[], size_t n
, const struct miniflow
*src
)
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
));
3263 COVERAGE_INC(miniflow_malloc
);
3265 for (i
= 0; i
< n
; i
++) {
3266 *dst
= *src
; /* Copy maps. */
3268 dst
+= 1; /* Just past the maps. */
3269 dst
= (struct miniflow
*)((uint64_t *)dst
+ n_values
); /* Skip data. */
3274 /* Returns a miniflow copy of 'src'. The caller must eventually free() the
3275 * returned miniflow. */
3277 miniflow_create(const struct flow
*src
)
3279 struct miniflow tmp
;
3280 struct miniflow
*dst
;
3282 miniflow_map_init(&tmp
, src
);
3284 miniflow_alloc(&dst
, 1, &tmp
);
3285 miniflow_init(dst
, src
);
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'. */
3292 miniflow_clone(struct miniflow
*dst
, const struct miniflow
*src
,
3295 *dst
= *src
; /* Copy maps. */
3296 memcpy(miniflow_values(dst
), miniflow_get_values(src
),
3297 MINIFLOW_VALUES_SIZE(n_values
));
3300 /* Initializes 'dst' as a copy of 'src'. */
3302 miniflow_expand(const struct miniflow
*src
, struct flow
*dst
)
3304 memset(dst
, 0, sizeof *dst
);
3305 flow_union_with_miniflow(dst
, src
);
3308 /* Returns true if 'a' and 'b' are equal miniflows, false otherwise. */
3310 miniflow_equal(const struct miniflow
*a
, const struct miniflow
*b
)
3312 const uint64_t *ap
= miniflow_get_values(a
);
3313 const uint64_t *bp
= miniflow_get_values(b
);
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
);
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)) {
3333 /* Returns false if 'a' and 'b' differ at the places where there are 1-bits
3334 * in 'mask', true otherwise. */
3336 miniflow_equal_in_minimask(const struct miniflow
*a
, const struct miniflow
*b
,
3337 const struct minimask
*mask
)
3339 const uint64_t *p
= miniflow_get_values(&mask
->masks
);
3342 FLOWMAP_FOR_EACH_INDEX(idx
, mask
->masks
.map
) {
3343 if ((miniflow_get(a
, idx
) ^ miniflow_get(b
, idx
)) & *p
++) {
3351 /* Returns true if 'a' and 'b' are equal at the places where there are 1-bits
3352 * in 'mask', false if they differ. */
3354 miniflow_equal_flow_in_minimask(const struct miniflow
*a
, const struct flow
*b
,
3355 const struct minimask
*mask
)
3357 const uint64_t *p
= miniflow_get_values(&mask
->masks
);
3360 FLOWMAP_FOR_EACH_INDEX(idx
, mask
->masks
.map
) {
3361 if ((miniflow_get(a
, idx
) ^ flow_u64_value(b
, idx
)) & *p
++) {
3371 minimask_init(struct minimask
*mask
, const struct flow_wildcards
*wc
)
3373 miniflow_init(&mask
->masks
, &wc
->masks
);
3376 /* Returns a minimask copy of 'wc'. The caller must eventually free the
3377 * returned minimask with free(). */
3379 minimask_create(const struct flow_wildcards
*wc
)
3381 return (struct minimask
*)miniflow_create(&wc
->masks
);
3384 /* Initializes 'dst_' as the bit-wise "and" of 'a_' and 'b_'.
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(). */
3390 minimask_combine(struct minimask
*dst_
,
3391 const struct minimask
*a_
, const struct minimask
*b_
,
3392 uint64_t storage
[FLOW_U64S
])
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
;
3400 flowmap_init(&dst
->map
);
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
);
3407 flowmap_set(&dst
->map
, idx
, 1);
3408 *dst_values
++ = mask
;
3413 /* Initializes 'wc' as a copy of 'mask'. */
3415 minimask_expand(const struct minimask
*mask
, struct flow_wildcards
*wc
)
3417 miniflow_expand(&mask
->masks
, &wc
->masks
);
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. */
3424 minimask_equal(const struct minimask
*a
, const struct minimask
*b
)
3426 return !memcmp(a
, b
, sizeof *a
3427 + MINIFLOW_VALUES_SIZE(miniflow_n_values(&a
->masks
)));
3430 /* Returns true if at least one bit matched by 'b' is wildcarded by 'a',
3431 * false otherwise. */
3433 minimask_has_extra(const struct minimask
*a
, const struct minimask
*b
)
3435 const uint64_t *bp
= miniflow_get_values(&b
->masks
);
3438 FLOWMAP_FOR_EACH_INDEX(idx
, b
->masks
.map
) {
3439 uint64_t b_u64
= *bp
++;
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. */
3453 flow_limit_vlans(int vlan_limit
)
3455 if (vlan_limit
<= 0) {
3456 flow_vlan_limit
= FLOW_MAX_VLAN_HEADERS
;
3458 flow_vlan_limit
= MIN(vlan_limit
, FLOW_MAX_VLAN_HEADERS
);
3463 flow_get_tunnel_netdev(struct flow_tnl
*tunnel
)
3465 char iface
[IFNAMSIZ
];
3466 struct in6_addr ip6
;
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
;
3477 if (!ovs_router_lookup(0, &ip6
, iface
, NULL
, &gw
)) {
3481 return netdev_from_name(iface
);