2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 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>
22 #include <netinet/in.h>
23 #include <netinet/icmp6.h>
24 #include <netinet/ip6.h>
28 #include "byte-order.h"
31 #include "dynamic-string.h"
36 #include "openflow/openflow.h"
40 #include "unaligned.h"
42 COVERAGE_DEFINE(flow_extract);
43 COVERAGE_DEFINE(miniflow_malloc);
45 /* U32 indices for segmented flow classification. */
46 const uint8_t flow_segment_u32s[4] = {
47 FLOW_SEGMENT_1_ENDS_AT / 4,
48 FLOW_SEGMENT_2_ENDS_AT / 4,
49 FLOW_SEGMENT_3_ENDS_AT / 4,
53 /* miniflow_extract() assumes the following to be true to optimize the
54 * extraction process. */
55 BUILD_ASSERT_DECL(offsetof(struct flow, dl_type) + 2
56 == offsetof(struct flow, vlan_tci) &&
57 offsetof(struct flow, dl_type) / 4
58 == offsetof(struct flow, vlan_tci) / 4 );
60 BUILD_ASSERT_DECL(offsetof(struct flow, nw_frag) + 3
61 == offsetof(struct flow, nw_proto) &&
62 offsetof(struct flow, nw_tos) + 2
63 == offsetof(struct flow, nw_proto) &&
64 offsetof(struct flow, nw_ttl) + 1
65 == offsetof(struct flow, nw_proto) &&
66 offsetof(struct flow, nw_frag) / 4
67 == offsetof(struct flow, nw_tos) / 4 &&
68 offsetof(struct flow, nw_ttl) / 4
69 == offsetof(struct flow, nw_tos) / 4 &&
70 offsetof(struct flow, nw_proto) / 4
71 == offsetof(struct flow, nw_tos) / 4);
73 /* TCP flags in the first half of a BE32, zeroes in the other half. */
74 BUILD_ASSERT_DECL(offsetof(struct flow, tcp_flags) + 2
75 == offsetof(struct flow, pad) &&
76 offsetof(struct flow, tcp_flags) / 4
77 == offsetof(struct flow, pad) / 4);
79 #define TCP_FLAGS_BE32(tcp_ctl) ((OVS_FORCE ovs_be32)TCP_FLAGS_BE16(tcp_ctl) \
82 #define TCP_FLAGS_BE32(tcp_ctl) ((OVS_FORCE ovs_be32)TCP_FLAGS_BE16(tcp_ctl))
85 BUILD_ASSERT_DECL(offsetof(struct flow, tp_src) + 2
86 == offsetof(struct flow, tp_dst) &&
87 offsetof(struct flow, tp_src) / 4
88 == offsetof(struct flow, tp_dst) / 4);
90 /* Removes 'size' bytes from the head end of '*datap', of size '*sizep', which
91 * must contain at least 'size' bytes of data. Returns the first byte of data
93 static inline const void *
94 data_pull(void **datap, size_t *sizep, size_t size)
96 char *data = (char *)*datap;
102 /* If '*datap' has at least 'size' bytes of data, removes that many bytes from
103 * the head end of '*datap' and returns the first byte removed. Otherwise,
104 * returns a null pointer without modifying '*datap'. */
105 static inline const void *
106 data_try_pull(void **datap, size_t *sizep, size_t size)
108 return OVS_LIKELY(*sizep >= size) ? data_pull(datap, sizep, size) : NULL;
111 /* Context for pushing data to a miniflow. */
115 uint32_t * const end;
118 /* miniflow_push_* macros allow filling in a miniflow data values in order.
119 * Assertions are needed only when the layout of the struct flow is modified.
120 * 'ofs' is a compile-time constant, which allows most of the code be optimized
121 * away. Some GCC versions gave warnigns on ALWAYS_INLINE, so these are
122 * defined as macros. */
124 #if (FLOW_WC_SEQ != 26)
125 #define MINIFLOW_ASSERT(X) ovs_assert(X)
127 #define MINIFLOW_ASSERT(X)
130 #define miniflow_push_uint32_(MF, OFS, VALUE) \
132 MINIFLOW_ASSERT(MF.data < MF.end && (OFS) % 4 == 0 \
133 && !(MF.map & (UINT64_MAX << (OFS) / 4))); \
134 *MF.data++ = VALUE; \
135 MF.map |= UINT64_C(1) << (OFS) / 4; \
138 #define miniflow_push_be32_(MF, OFS, VALUE) \
139 miniflow_push_uint32_(MF, OFS, (OVS_FORCE uint32_t)(VALUE))
141 #define miniflow_push_uint16_(MF, OFS, VALUE) \
143 MINIFLOW_ASSERT(MF.data < MF.end && \
144 (((OFS) % 4 == 0 && !(MF.map & (UINT64_MAX << (OFS) / 4))) \
145 || ((OFS) % 4 == 2 && MF.map & (UINT64_C(1) << (OFS) / 4) \
146 && !(MF.map & (UINT64_MAX << ((OFS) / 4 + 1)))))); \
148 if ((OFS) % 4 == 0) { \
149 *(uint16_t *)MF.data = VALUE; \
150 MF.map |= UINT64_C(1) << (OFS) / 4; \
151 } else if ((OFS) % 4 == 2) { \
152 *((uint16_t *)MF.data + 1) = VALUE; \
157 #define miniflow_push_be16_(MF, OFS, VALUE) \
158 miniflow_push_uint16_(MF, OFS, (OVS_FORCE uint16_t)VALUE);
160 /* Data at 'valuep' may be unaligned. */
161 #define miniflow_push_words_(MF, OFS, VALUEP, N_WORDS) \
163 int ofs32 = (OFS) / 4; \
165 MINIFLOW_ASSERT(MF.data + (N_WORDS) <= MF.end && (OFS) % 4 == 0 \
166 && !(MF.map & (UINT64_MAX << ofs32))); \
168 memcpy(MF.data, (VALUEP), (N_WORDS) * sizeof *MF.data); \
169 MF.data += (N_WORDS); \
170 MF.map |= ((UINT64_MAX >> (64 - (N_WORDS))) << ofs32); \
173 #define miniflow_push_uint32(MF, FIELD, VALUE) \
174 miniflow_push_uint32_(MF, offsetof(struct flow, FIELD), VALUE)
176 #define miniflow_push_be32(MF, FIELD, VALUE) \
177 miniflow_push_be32_(MF, offsetof(struct flow, FIELD), VALUE)
179 #define miniflow_push_uint32_check(MF, FIELD, VALUE) \
180 { if (OVS_LIKELY(VALUE)) { \
181 miniflow_push_uint32_(MF, offsetof(struct flow, FIELD), VALUE); \
185 #define miniflow_push_be32_check(MF, FIELD, VALUE) \
186 { if (OVS_LIKELY(VALUE)) { \
187 miniflow_push_be32_(MF, offsetof(struct flow, FIELD), VALUE); \
191 #define miniflow_push_uint16(MF, FIELD, VALUE) \
192 miniflow_push_uint16_(MF, offsetof(struct flow, FIELD), VALUE)
194 #define miniflow_push_be16(MF, FIELD, VALUE) \
195 miniflow_push_be16_(MF, offsetof(struct flow, FIELD), VALUE)
197 #define miniflow_push_words(MF, FIELD, VALUEP, N_WORDS) \
198 miniflow_push_words_(MF, offsetof(struct flow, FIELD), VALUEP, N_WORDS)
200 /* Pulls the MPLS headers at '*datap' and returns the count of them. */
202 parse_mpls(void **datap, size_t *sizep)
204 const struct mpls_hdr *mh;
207 while ((mh = data_try_pull(datap, sizep, sizeof *mh))) {
209 if (mh->mpls_lse.lo & htons(1 << MPLS_BOS_SHIFT)) {
213 return MAX(count, FLOW_MAX_MPLS_LABELS);
216 static inline ovs_be16
217 parse_vlan(void **datap, size_t *sizep)
219 const struct eth_header *eth = *datap;
222 ovs_be16 eth_type; /* ETH_TYPE_VLAN */
226 data_pull(datap, sizep, ETH_ADDR_LEN * 2);
228 if (eth->eth_type == htons(ETH_TYPE_VLAN)) {
229 if (OVS_LIKELY(*sizep
230 >= sizeof(struct qtag_prefix) + sizeof(ovs_be16))) {
231 const struct qtag_prefix *qp = data_pull(datap, sizep, sizeof *qp);
232 return qp->tci | htons(VLAN_CFI);
238 static inline ovs_be16
239 parse_ethertype(void **datap, size_t *sizep)
241 const struct llc_snap_header *llc;
244 proto = *(ovs_be16 *) data_pull(datap, sizep, sizeof proto);
245 if (OVS_LIKELY(ntohs(proto) >= ETH_TYPE_MIN)) {
249 if (OVS_UNLIKELY(*sizep < sizeof *llc)) {
250 return htons(FLOW_DL_TYPE_NONE);
254 if (OVS_UNLIKELY(llc->llc.llc_dsap != LLC_DSAP_SNAP
255 || llc->llc.llc_ssap != LLC_SSAP_SNAP
256 || llc->llc.llc_cntl != LLC_CNTL_SNAP
257 || memcmp(llc->snap.snap_org, SNAP_ORG_ETHERNET,
258 sizeof llc->snap.snap_org))) {
259 return htons(FLOW_DL_TYPE_NONE);
262 data_pull(datap, sizep, sizeof *llc);
264 if (OVS_LIKELY(ntohs(llc->snap.snap_type) >= ETH_TYPE_MIN)) {
265 return llc->snap.snap_type;
268 return htons(FLOW_DL_TYPE_NONE);
272 parse_icmpv6(void **datap, size_t *sizep, const struct icmp6_hdr *icmp,
273 const struct in6_addr **nd_target,
274 uint8_t arp_buf[2][ETH_ADDR_LEN])
276 if (icmp->icmp6_code == 0 &&
277 (icmp->icmp6_type == ND_NEIGHBOR_SOLICIT ||
278 icmp->icmp6_type == ND_NEIGHBOR_ADVERT)) {
280 *nd_target = data_try_pull(datap, sizep, sizeof **nd_target);
281 if (OVS_UNLIKELY(!*nd_target)) {
285 while (*sizep >= 8) {
286 /* The minimum size of an option is 8 bytes, which also is
287 * the size of Ethernet link-layer options. */
288 const struct nd_opt_hdr *nd_opt = *datap;
289 int opt_len = nd_opt->nd_opt_len * 8;
291 if (!opt_len || opt_len > *sizep) {
295 /* Store the link layer address if the appropriate option is
296 * provided. It is considered an error if the same link
297 * layer option is specified twice. */
298 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LINKADDR
300 if (OVS_LIKELY(eth_addr_is_zero(arp_buf[0]))) {
301 memcpy(arp_buf[0], nd_opt + 1, ETH_ADDR_LEN);
305 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LINKADDR
307 if (OVS_LIKELY(eth_addr_is_zero(arp_buf[1]))) {
308 memcpy(arp_buf[1], nd_opt + 1, ETH_ADDR_LEN);
314 if (OVS_UNLIKELY(!data_try_pull(datap, sizep, opt_len))) {
326 /* Initializes 'flow' members from 'packet' and 'md'
328 * Initializes 'packet' header l2 pointer to the start of the Ethernet
329 * header, and the layer offsets as follows:
331 * - packet->l2_5_ofs to the start of the MPLS shim header, or UINT16_MAX
332 * when there is no MPLS shim header.
334 * - packet->l3_ofs to just past the Ethernet header, or just past the
335 * vlan_header if one is present, to the first byte of the payload of the
336 * Ethernet frame. UINT16_MAX if the frame is too short to contain an
339 * - packet->l4_ofs to just past the IPv4 header, if one is present and
340 * has at least the content used for the fields of interest for the flow,
341 * otherwise UINT16_MAX.
344 flow_extract(struct ofpbuf *packet, const struct pkt_metadata *md,
349 uint32_t buf[FLOW_U32S];
352 COVERAGE_INC(flow_extract);
354 miniflow_initialize(&m.mf, m.buf);
355 miniflow_extract(packet, md, &m.mf);
356 miniflow_expand(&m.mf, flow);
359 /* Caller is responsible for initializing 'dst' with enough storage for
360 * FLOW_U32S * 4 bytes. */
362 miniflow_extract(struct ofpbuf *packet, const struct pkt_metadata *md,
363 struct miniflow *dst)
365 void *data = ofpbuf_data(packet);
366 size_t size = ofpbuf_size(packet);
367 uint32_t *values = miniflow_values(dst);
368 struct mf_ctx mf = { 0, values, values + FLOW_U32S };
371 uint8_t nw_frag, nw_tos, nw_ttl, nw_proto;
375 if (md->tunnel.ip_dst) {
376 miniflow_push_words(mf, tunnel, &md->tunnel,
377 sizeof md->tunnel / 4);
379 miniflow_push_uint32_check(mf, skb_priority, md->skb_priority);
380 miniflow_push_uint32_check(mf, pkt_mark, md->pkt_mark);
381 miniflow_push_uint32_check(mf, recirc_id, md->recirc_id);
382 miniflow_push_uint32(mf, in_port, odp_to_u32(md->in_port.odp_port));
385 /* Initialize packet's layer pointer and offsets. */
387 ofpbuf_set_frame(packet, data);
389 /* Must have full Ethernet header to proceed. */
390 if (OVS_UNLIKELY(size < sizeof(struct eth_header))) {
396 BUILD_ASSERT(offsetof(struct flow, dl_dst) + 6
397 == offsetof(struct flow, dl_src));
398 miniflow_push_words(mf, dl_dst, data, ETH_ADDR_LEN * 2 / 4);
399 /* dl_type, vlan_tci. */
400 vlan_tci = parse_vlan(&data, &size);
401 dl_type = parse_ethertype(&data, &size);
402 miniflow_push_be16(mf, dl_type, dl_type);
403 miniflow_push_be16(mf, vlan_tci, vlan_tci);
407 if (OVS_UNLIKELY(eth_type_mpls(dl_type))) {
409 const void *mpls = data;
411 packet->l2_5_ofs = (char *)data - l2;
412 count = parse_mpls(&data, &size);
413 miniflow_push_words(mf, mpls_lse, mpls, count);
417 packet->l3_ofs = (char *)data - l2;
420 if (OVS_LIKELY(dl_type == htons(ETH_TYPE_IP))) {
421 const struct ip_header *nh = data;
424 if (OVS_UNLIKELY(size < IP_HEADER_LEN)) {
427 ip_len = IP_IHL(nh->ip_ihl_ver) * 4;
429 if (OVS_UNLIKELY(ip_len < IP_HEADER_LEN)) {
433 /* Push both source and destination address at once. */
434 miniflow_push_words(mf, nw_src, &nh->ip_src, 2);
438 nw_proto = nh->ip_proto;
439 if (OVS_UNLIKELY(IP_IS_FRAGMENT(nh->ip_frag_off))) {
440 nw_frag = FLOW_NW_FRAG_ANY;
441 if (nh->ip_frag_off & htons(IP_FRAG_OFF_MASK)) {
442 nw_frag |= FLOW_NW_FRAG_LATER;
445 if (OVS_UNLIKELY(size < ip_len)) {
448 data_pull(&data, &size, ip_len);
450 } else if (dl_type == htons(ETH_TYPE_IPV6)) {
451 const struct ovs_16aligned_ip6_hdr *nh;
454 if (OVS_UNLIKELY(size < sizeof *nh)) {
457 nh = data_pull(&data, &size, sizeof *nh);
459 miniflow_push_words(mf, ipv6_src, &nh->ip6_src,
460 sizeof nh->ip6_src / 4);
461 miniflow_push_words(mf, ipv6_dst, &nh->ip6_dst,
462 sizeof nh->ip6_dst / 4);
464 tc_flow = get_16aligned_be32(&nh->ip6_flow);
466 ovs_be32 label = tc_flow & htonl(IPV6_LABEL_MASK);
467 miniflow_push_be32_check(mf, ipv6_label, label);
470 nw_tos = ntohl(tc_flow) >> 20;
471 nw_ttl = nh->ip6_hlim;
472 nw_proto = nh->ip6_nxt;
475 if (OVS_LIKELY((nw_proto != IPPROTO_HOPOPTS)
476 && (nw_proto != IPPROTO_ROUTING)
477 && (nw_proto != IPPROTO_DSTOPTS)
478 && (nw_proto != IPPROTO_AH)
479 && (nw_proto != IPPROTO_FRAGMENT))) {
480 /* It's either a terminal header (e.g., TCP, UDP) or one we
481 * don't understand. In either case, we're done with the
482 * packet, so use it to fill in 'nw_proto'. */
486 /* We only verify that at least 8 bytes of the next header are
487 * available, but many of these headers are longer. Ensure that
488 * accesses within the extension header are within those first 8
489 * bytes. All extension headers are required to be at least 8
491 if (OVS_UNLIKELY(size < 8)) {
495 if ((nw_proto == IPPROTO_HOPOPTS)
496 || (nw_proto == IPPROTO_ROUTING)
497 || (nw_proto == IPPROTO_DSTOPTS)) {
498 /* These headers, while different, have the fields we care
499 * about in the same location and with the same
501 const struct ip6_ext *ext_hdr = data;
502 nw_proto = ext_hdr->ip6e_nxt;
503 if (OVS_UNLIKELY(!data_try_pull(&data, &size,
504 (ext_hdr->ip6e_len + 1) * 8))) {
507 } else if (nw_proto == IPPROTO_AH) {
508 /* A standard AH definition isn't available, but the fields
509 * we care about are in the same location as the generic
510 * option header--only the header length is calculated
512 const struct ip6_ext *ext_hdr = data;
513 nw_proto = ext_hdr->ip6e_nxt;
514 if (OVS_UNLIKELY(!data_try_pull(&data, &size,
515 (ext_hdr->ip6e_len + 2) * 4))) {
518 } else if (nw_proto == IPPROTO_FRAGMENT) {
519 const struct ovs_16aligned_ip6_frag *frag_hdr = data;
521 nw_proto = frag_hdr->ip6f_nxt;
522 if (!data_try_pull(&data, &size, sizeof *frag_hdr)) {
526 /* We only process the first fragment. */
527 if (frag_hdr->ip6f_offlg != htons(0)) {
528 nw_frag = FLOW_NW_FRAG_ANY;
529 if ((frag_hdr->ip6f_offlg & IP6F_OFF_MASK) != htons(0)) {
530 nw_frag |= FLOW_NW_FRAG_LATER;
531 nw_proto = IPPROTO_FRAGMENT;
538 if (dl_type == htons(ETH_TYPE_ARP) ||
539 dl_type == htons(ETH_TYPE_RARP)) {
540 uint8_t arp_buf[2][ETH_ADDR_LEN];
541 const struct arp_eth_header *arp = (const struct arp_eth_header *)
542 data_try_pull(&data, &size, ARP_ETH_HEADER_LEN);
544 if (OVS_LIKELY(arp) && OVS_LIKELY(arp->ar_hrd == htons(1))
545 && OVS_LIKELY(arp->ar_pro == htons(ETH_TYPE_IP))
546 && OVS_LIKELY(arp->ar_hln == ETH_ADDR_LEN)
547 && OVS_LIKELY(arp->ar_pln == 4)) {
548 miniflow_push_words(mf, nw_src, &arp->ar_spa, 1);
549 miniflow_push_words(mf, nw_dst, &arp->ar_tpa, 1);
551 /* We only match on the lower 8 bits of the opcode. */
552 if (OVS_LIKELY(ntohs(arp->ar_op) <= 0xff)) {
553 miniflow_push_be32(mf, nw_frag, htonl(ntohs(arp->ar_op)));
556 /* Must be adjacent. */
557 BUILD_ASSERT(offsetof(struct flow, arp_sha) + 6
558 == offsetof(struct flow, arp_tha));
560 memcpy(arp_buf[0], arp->ar_sha, ETH_ADDR_LEN);
561 memcpy(arp_buf[1], arp->ar_tha, ETH_ADDR_LEN);
562 miniflow_push_words(mf, arp_sha, arp_buf,
563 ETH_ADDR_LEN * 2 / 4);
569 packet->l4_ofs = (char *)data - l2;
570 miniflow_push_be32(mf, nw_frag,
571 BYTES_TO_BE32(nw_frag, nw_tos, nw_ttl, nw_proto));
573 if (OVS_LIKELY(!(nw_frag & FLOW_NW_FRAG_LATER))) {
574 if (OVS_LIKELY(nw_proto == IPPROTO_TCP)) {
575 if (OVS_LIKELY(size >= TCP_HEADER_LEN)) {
576 const struct tcp_header *tcp = data;
578 miniflow_push_be32(mf, tcp_flags,
579 TCP_FLAGS_BE32(tcp->tcp_ctl));
580 miniflow_push_words(mf, tp_src, &tcp->tcp_src, 1);
582 } else if (OVS_LIKELY(nw_proto == IPPROTO_UDP)) {
583 if (OVS_LIKELY(size >= UDP_HEADER_LEN)) {
584 const struct udp_header *udp = data;
586 miniflow_push_words(mf, tp_src, &udp->udp_src, 1);
588 } else if (OVS_LIKELY(nw_proto == IPPROTO_SCTP)) {
589 if (OVS_LIKELY(size >= SCTP_HEADER_LEN)) {
590 const struct sctp_header *sctp = data;
592 miniflow_push_words(mf, tp_src, &sctp->sctp_src, 1);
594 } else if (OVS_LIKELY(nw_proto == IPPROTO_ICMP)) {
595 if (OVS_LIKELY(size >= ICMP_HEADER_LEN)) {
596 const struct icmp_header *icmp = data;
598 miniflow_push_be16(mf, tp_src, htons(icmp->icmp_type));
599 miniflow_push_be16(mf, tp_dst, htons(icmp->icmp_code));
601 } else if (OVS_LIKELY(nw_proto == IPPROTO_IGMP)) {
602 if (OVS_LIKELY(size >= IGMP_HEADER_LEN)) {
603 const struct igmp_header *igmp = data;
605 miniflow_push_be16(mf, tp_src, htons(igmp->igmp_type));
606 miniflow_push_be16(mf, tp_dst, htons(igmp->igmp_code));
607 miniflow_push_be32(mf, igmp_group_ip4,
608 get_16aligned_be32(&igmp->group));
610 } else if (OVS_LIKELY(nw_proto == IPPROTO_ICMPV6)) {
611 if (OVS_LIKELY(size >= sizeof(struct icmp6_hdr))) {
612 const struct in6_addr *nd_target = NULL;
613 uint8_t arp_buf[2][ETH_ADDR_LEN];
614 const struct icmp6_hdr *icmp = data_pull(&data, &size,
616 memset(arp_buf, 0, sizeof arp_buf);
617 if (OVS_LIKELY(parse_icmpv6(&data, &size, icmp, &nd_target,
619 miniflow_push_words(mf, arp_sha, arp_buf,
620 ETH_ADDR_LEN * 2 / 4);
622 miniflow_push_words(mf, nd_target, nd_target,
623 sizeof *nd_target / 4);
625 miniflow_push_be16(mf, tp_src, htons(icmp->icmp6_type));
626 miniflow_push_be16(mf, tp_dst, htons(icmp->icmp6_code));
632 miniflow_push_uint32_check(mf, dp_hash, md->dp_hash);
638 /* For every bit of a field that is wildcarded in 'wildcards', sets the
639 * corresponding bit in 'flow' to zero. */
641 flow_zero_wildcards(struct flow *flow, const struct flow_wildcards *wildcards)
643 uint32_t *flow_u32 = (uint32_t *) flow;
644 const uint32_t *wc_u32 = (const uint32_t *) &wildcards->masks;
647 for (i = 0; i < FLOW_U32S; i++) {
648 flow_u32[i] &= wc_u32[i];
653 flow_unwildcard_tp_ports(const struct flow *flow, struct flow_wildcards *wc)
655 if (flow->nw_proto != IPPROTO_ICMP) {
656 memset(&wc->masks.tp_src, 0xff, sizeof wc->masks.tp_src);
657 memset(&wc->masks.tp_dst, 0xff, sizeof wc->masks.tp_dst);
659 wc->masks.tp_src = htons(0xff);
660 wc->masks.tp_dst = htons(0xff);
664 /* Initializes 'fmd' with the metadata found in 'flow'. */
666 flow_get_metadata(const struct flow *flow, struct flow_metadata *fmd)
668 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 27);
670 fmd->dp_hash = flow->dp_hash;
671 fmd->recirc_id = flow->recirc_id;
672 fmd->tun_id = flow->tunnel.tun_id;
673 fmd->tun_src = flow->tunnel.ip_src;
674 fmd->tun_dst = flow->tunnel.ip_dst;
675 fmd->metadata = flow->metadata;
676 memcpy(fmd->regs, flow->regs, sizeof fmd->regs);
677 fmd->pkt_mark = flow->pkt_mark;
678 fmd->in_port = flow->in_port.ofp_port;
682 flow_to_string(const struct flow *flow)
684 struct ds ds = DS_EMPTY_INITIALIZER;
685 flow_format(&ds, flow);
690 flow_tun_flag_to_string(uint32_t flags)
693 case FLOW_TNL_F_DONT_FRAGMENT:
695 case FLOW_TNL_F_CSUM:
707 format_flags(struct ds *ds, const char *(*bit_to_string)(uint32_t),
708 uint32_t flags, char del)
716 uint32_t bit = rightmost_1bit(flags);
719 s = bit_to_string(bit);
721 ds_put_format(ds, "%s%c", s, del);
730 ds_put_format(ds, "0x%"PRIx32"%c", bad, del);
736 format_flags_masked(struct ds *ds, const char *name,
737 const char *(*bit_to_string)(uint32_t), uint32_t flags,
741 ds_put_format(ds, "%s=", name);
744 uint32_t bit = rightmost_1bit(mask);
745 const char *s = bit_to_string(bit);
747 ds_put_format(ds, "%s%s", (flags & bit) ? "+" : "-",
748 s ? s : "[Unknown]");
754 flow_format(struct ds *ds, const struct flow *flow)
758 match_wc_init(&match, flow);
759 match_format(&match, ds, OFP_DEFAULT_PRIORITY);
763 flow_print(FILE *stream, const struct flow *flow)
765 char *s = flow_to_string(flow);
770 /* flow_wildcards functions. */
772 /* Initializes 'wc' as a set of wildcards that matches every packet. */
774 flow_wildcards_init_catchall(struct flow_wildcards *wc)
776 memset(&wc->masks, 0, sizeof wc->masks);
779 /* Clear the metadata and register wildcard masks. They are not packet
782 flow_wildcards_clear_non_packet_fields(struct flow_wildcards *wc)
784 memset(&wc->masks.metadata, 0, sizeof wc->masks.metadata);
785 memset(&wc->masks.regs, 0, sizeof wc->masks.regs);
788 /* Returns true if 'wc' matches every packet, false if 'wc' fixes any bits or
791 flow_wildcards_is_catchall(const struct flow_wildcards *wc)
793 const uint32_t *wc_u32 = (const uint32_t *) &wc->masks;
796 for (i = 0; i < FLOW_U32S; i++) {
804 /* Sets 'dst' as the bitwise AND of wildcards in 'src1' and 'src2'.
805 * That is, a bit or a field is wildcarded in 'dst' if it is wildcarded
806 * in 'src1' or 'src2' or both. */
808 flow_wildcards_and(struct flow_wildcards *dst,
809 const struct flow_wildcards *src1,
810 const struct flow_wildcards *src2)
812 uint32_t *dst_u32 = (uint32_t *) &dst->masks;
813 const uint32_t *src1_u32 = (const uint32_t *) &src1->masks;
814 const uint32_t *src2_u32 = (const uint32_t *) &src2->masks;
817 for (i = 0; i < FLOW_U32S; i++) {
818 dst_u32[i] = src1_u32[i] & src2_u32[i];
822 /* Sets 'dst' as the bitwise OR of wildcards in 'src1' and 'src2'. That
823 * is, a bit or a field is wildcarded in 'dst' if it is neither
824 * wildcarded in 'src1' nor 'src2'. */
826 flow_wildcards_or(struct flow_wildcards *dst,
827 const struct flow_wildcards *src1,
828 const struct flow_wildcards *src2)
830 uint32_t *dst_u32 = (uint32_t *) &dst->masks;
831 const uint32_t *src1_u32 = (const uint32_t *) &src1->masks;
832 const uint32_t *src2_u32 = (const uint32_t *) &src2->masks;
835 for (i = 0; i < FLOW_U32S; i++) {
836 dst_u32[i] = src1_u32[i] | src2_u32[i];
840 /* Returns a hash of the wildcards in 'wc'. */
842 flow_wildcards_hash(const struct flow_wildcards *wc, uint32_t basis)
844 return flow_hash(&wc->masks, basis);
847 /* Returns true if 'a' and 'b' represent the same wildcards, false if they are
850 flow_wildcards_equal(const struct flow_wildcards *a,
851 const struct flow_wildcards *b)
853 return flow_equal(&a->masks, &b->masks);
856 /* Returns true if at least one bit or field is wildcarded in 'a' but not in
857 * 'b', false otherwise. */
859 flow_wildcards_has_extra(const struct flow_wildcards *a,
860 const struct flow_wildcards *b)
862 const uint32_t *a_u32 = (const uint32_t *) &a->masks;
863 const uint32_t *b_u32 = (const uint32_t *) &b->masks;
866 for (i = 0; i < FLOW_U32S; i++) {
867 if ((a_u32[i] & b_u32[i]) != b_u32[i]) {
874 /* Returns true if 'a' and 'b' are equal, except that 0-bits (wildcarded bits)
875 * in 'wc' do not need to be equal in 'a' and 'b'. */
877 flow_equal_except(const struct flow *a, const struct flow *b,
878 const struct flow_wildcards *wc)
880 const uint32_t *a_u32 = (const uint32_t *) a;
881 const uint32_t *b_u32 = (const uint32_t *) b;
882 const uint32_t *wc_u32 = (const uint32_t *) &wc->masks;
885 for (i = 0; i < FLOW_U32S; i++) {
886 if ((a_u32[i] ^ b_u32[i]) & wc_u32[i]) {
893 /* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
894 * (A 0-bit indicates a wildcard bit.) */
896 flow_wildcards_set_reg_mask(struct flow_wildcards *wc, int idx, uint32_t mask)
898 wc->masks.regs[idx] = mask;
901 /* Calculates the 5-tuple hash from the given miniflow.
902 * This returns the same value as flow_hash_5tuple for the corresponding
905 miniflow_hash_5tuple(const struct miniflow *flow, uint32_t basis)
907 uint32_t hash = basis;
910 ovs_be16 dl_type = MINIFLOW_GET_BE16(flow, dl_type);
912 hash = hash_add(hash, MINIFLOW_GET_U8(flow, nw_proto));
914 /* Separate loops for better optimization. */
915 if (dl_type == htons(ETH_TYPE_IPV6)) {
916 uint64_t map = MINIFLOW_MAP(ipv6_src) | MINIFLOW_MAP(ipv6_dst)
917 | MINIFLOW_MAP(tp_src); /* Covers both ports */
920 MINIFLOW_FOR_EACH_IN_MAP(value, flow, map) {
921 hash = hash_add(hash, value);
924 uint64_t map = MINIFLOW_MAP(nw_src) | MINIFLOW_MAP(nw_dst)
925 | MINIFLOW_MAP(tp_src); /* Covers both ports */
928 MINIFLOW_FOR_EACH_IN_MAP(value, flow, map) {
929 hash = hash_add(hash, value);
932 hash = hash_finish(hash, 42); /* Arbitrary number. */
937 BUILD_ASSERT_DECL(offsetof(struct flow, tp_src) + 2
938 == offsetof(struct flow, tp_dst) &&
939 offsetof(struct flow, tp_src) / 4
940 == offsetof(struct flow, tp_dst) / 4);
941 BUILD_ASSERT_DECL(offsetof(struct flow, ipv6_src) + 16
942 == offsetof(struct flow, ipv6_dst));
944 /* Calculates the 5-tuple hash from the given flow. */
946 flow_hash_5tuple(const struct flow *flow, uint32_t basis)
948 uint32_t hash = basis;
951 const uint32_t *flow_u32 = (const uint32_t *)flow;
953 hash = hash_add(hash, flow->nw_proto);
955 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
956 int ofs = offsetof(struct flow, ipv6_src) / 4;
957 int end = ofs + 2 * sizeof flow->ipv6_src / 4;
960 hash = hash_add(hash, flow_u32[ofs++]);
963 hash = hash_add(hash, (OVS_FORCE uint32_t) flow->nw_src);
964 hash = hash_add(hash, (OVS_FORCE uint32_t) flow->nw_dst);
966 hash = hash_add(hash, flow_u32[offsetof(struct flow, tp_src) / 4]);
968 hash = hash_finish(hash, 42); /* Arbitrary number. */
973 /* Hashes 'flow' based on its L2 through L4 protocol information. */
975 flow_hash_symmetric_l4(const struct flow *flow, uint32_t basis)
980 struct in6_addr ipv6_addr;
985 uint8_t eth_addr[ETH_ADDR_LEN];
991 memset(&fields, 0, sizeof fields);
992 for (i = 0; i < ETH_ADDR_LEN; i++) {
993 fields.eth_addr[i] = flow->dl_src[i] ^ flow->dl_dst[i];
995 fields.vlan_tci = flow->vlan_tci & htons(VLAN_VID_MASK);
996 fields.eth_type = flow->dl_type;
998 /* UDP source and destination port are not taken into account because they
999 * will not necessarily be symmetric in a bidirectional flow. */
1000 if (fields.eth_type == htons(ETH_TYPE_IP)) {
1001 fields.ipv4_addr = flow->nw_src ^ flow->nw_dst;
1002 fields.ip_proto = flow->nw_proto;
1003 if (fields.ip_proto == IPPROTO_TCP || fields.ip_proto == IPPROTO_SCTP) {
1004 fields.tp_port = flow->tp_src ^ flow->tp_dst;
1006 } else if (fields.eth_type == htons(ETH_TYPE_IPV6)) {
1007 const uint8_t *a = &flow->ipv6_src.s6_addr[0];
1008 const uint8_t *b = &flow->ipv6_dst.s6_addr[0];
1009 uint8_t *ipv6_addr = &fields.ipv6_addr.s6_addr[0];
1011 for (i=0; i<16; i++) {
1012 ipv6_addr[i] = a[i] ^ b[i];
1014 fields.ip_proto = flow->nw_proto;
1015 if (fields.ip_proto == IPPROTO_TCP || fields.ip_proto == IPPROTO_SCTP) {
1016 fields.tp_port = flow->tp_src ^ flow->tp_dst;
1019 return jhash_bytes(&fields, sizeof fields, basis);
1022 /* Initialize a flow with random fields that matter for nx_hash_fields. */
1024 flow_random_hash_fields(struct flow *flow)
1026 uint16_t rnd = random_uint16();
1028 /* Initialize to all zeros. */
1029 memset(flow, 0, sizeof *flow);
1031 eth_addr_random(flow->dl_src);
1032 eth_addr_random(flow->dl_dst);
1034 flow->vlan_tci = (OVS_FORCE ovs_be16) (random_uint16() & VLAN_VID_MASK);
1036 /* Make most of the random flows IPv4, some IPv6, and rest random. */
1037 flow->dl_type = rnd < 0x8000 ? htons(ETH_TYPE_IP) :
1038 rnd < 0xc000 ? htons(ETH_TYPE_IPV6) : (OVS_FORCE ovs_be16)rnd;
1040 if (dl_type_is_ip_any(flow->dl_type)) {
1041 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1042 flow->nw_src = (OVS_FORCE ovs_be32)random_uint32();
1043 flow->nw_dst = (OVS_FORCE ovs_be32)random_uint32();
1045 random_bytes(&flow->ipv6_src, sizeof flow->ipv6_src);
1046 random_bytes(&flow->ipv6_dst, sizeof flow->ipv6_dst);
1048 /* Make most of IP flows TCP, some UDP or SCTP, and rest random. */
1049 rnd = random_uint16();
1050 flow->nw_proto = rnd < 0x8000 ? IPPROTO_TCP :
1051 rnd < 0xc000 ? IPPROTO_UDP :
1052 rnd < 0xd000 ? IPPROTO_SCTP : (uint8_t)rnd;
1053 if (flow->nw_proto == IPPROTO_TCP ||
1054 flow->nw_proto == IPPROTO_UDP ||
1055 flow->nw_proto == IPPROTO_SCTP) {
1056 flow->tp_src = (OVS_FORCE ovs_be16)random_uint16();
1057 flow->tp_dst = (OVS_FORCE ovs_be16)random_uint16();
1062 /* Masks the fields in 'wc' that are used by the flow hash 'fields'. */
1064 flow_mask_hash_fields(const struct flow *flow, struct flow_wildcards *wc,
1065 enum nx_hash_fields fields)
1068 case NX_HASH_FIELDS_ETH_SRC:
1069 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
1072 case NX_HASH_FIELDS_SYMMETRIC_L4:
1073 memset(&wc->masks.dl_src, 0xff, sizeof wc->masks.dl_src);
1074 memset(&wc->masks.dl_dst, 0xff, sizeof wc->masks.dl_dst);
1075 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1076 memset(&wc->masks.nw_src, 0xff, sizeof wc->masks.nw_src);
1077 memset(&wc->masks.nw_dst, 0xff, sizeof wc->masks.nw_dst);
1078 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1079 memset(&wc->masks.ipv6_src, 0xff, sizeof wc->masks.ipv6_src);
1080 memset(&wc->masks.ipv6_dst, 0xff, sizeof wc->masks.ipv6_dst);
1082 if (is_ip_any(flow)) {
1083 memset(&wc->masks.nw_proto, 0xff, sizeof wc->masks.nw_proto);
1084 flow_unwildcard_tp_ports(flow, wc);
1086 wc->masks.vlan_tci |= htons(VLAN_VID_MASK | VLAN_CFI);
1094 /* Hashes the portions of 'flow' designated by 'fields'. */
1096 flow_hash_fields(const struct flow *flow, enum nx_hash_fields fields,
1101 case NX_HASH_FIELDS_ETH_SRC:
1102 return jhash_bytes(flow->dl_src, sizeof flow->dl_src, basis);
1104 case NX_HASH_FIELDS_SYMMETRIC_L4:
1105 return flow_hash_symmetric_l4(flow, basis);
1111 /* Returns a string representation of 'fields'. */
1113 flow_hash_fields_to_str(enum nx_hash_fields fields)
1116 case NX_HASH_FIELDS_ETH_SRC: return "eth_src";
1117 case NX_HASH_FIELDS_SYMMETRIC_L4: return "symmetric_l4";
1118 default: return "<unknown>";
1122 /* Returns true if the value of 'fields' is supported. Otherwise false. */
1124 flow_hash_fields_valid(enum nx_hash_fields fields)
1126 return fields == NX_HASH_FIELDS_ETH_SRC
1127 || fields == NX_HASH_FIELDS_SYMMETRIC_L4;
1130 /* Returns a hash value for the bits of 'flow' that are active based on
1131 * 'wc', given 'basis'. */
1133 flow_hash_in_wildcards(const struct flow *flow,
1134 const struct flow_wildcards *wc, uint32_t basis)
1136 const uint32_t *wc_u32 = (const uint32_t *) &wc->masks;
1137 const uint32_t *flow_u32 = (const uint32_t *) flow;
1142 for (i = 0; i < FLOW_U32S; i++) {
1143 hash = hash_add(hash, flow_u32[i] & wc_u32[i]);
1145 return hash_finish(hash, 4 * FLOW_U32S);
1148 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
1149 * OpenFlow 1.0 "dl_vlan" value:
1151 * - If it is in the range 0...4095, 'flow->vlan_tci' is set to match
1152 * that VLAN. Any existing PCP match is unchanged (it becomes 0 if
1153 * 'flow' previously matched packets without a VLAN header).
1155 * - If it is OFP_VLAN_NONE, 'flow->vlan_tci' is set to match a packet
1156 * without a VLAN tag.
1158 * - Other values of 'vid' should not be used. */
1160 flow_set_dl_vlan(struct flow *flow, ovs_be16 vid)
1162 if (vid == htons(OFP10_VLAN_NONE)) {
1163 flow->vlan_tci = htons(0);
1165 vid &= htons(VLAN_VID_MASK);
1166 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
1167 flow->vlan_tci |= htons(VLAN_CFI) | vid;
1171 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
1172 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
1175 flow_set_vlan_vid(struct flow *flow, ovs_be16 vid)
1177 ovs_be16 mask = htons(VLAN_VID_MASK | VLAN_CFI);
1178 flow->vlan_tci &= ~mask;
1179 flow->vlan_tci |= vid & mask;
1182 /* Sets the VLAN PCP that 'flow' matches to 'pcp', which should be in the
1185 * This function has no effect on the VLAN ID that 'flow' matches.
1187 * After calling this function, 'flow' will not match packets without a VLAN
1190 flow_set_vlan_pcp(struct flow *flow, uint8_t pcp)
1193 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
1194 flow->vlan_tci |= htons((pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
1197 /* Returns the number of MPLS LSEs present in 'flow'
1199 * Returns 0 if the 'dl_type' of 'flow' is not an MPLS ethernet type.
1200 * Otherwise traverses 'flow''s MPLS label stack stopping at the
1201 * first entry that has the BoS bit set. If no such entry exists then
1202 * the maximum number of LSEs that can be stored in 'flow' is returned.
1205 flow_count_mpls_labels(const struct flow *flow, struct flow_wildcards *wc)
1208 wc->masks.dl_type = OVS_BE16_MAX;
1210 if (eth_type_mpls(flow->dl_type)) {
1212 int len = FLOW_MAX_MPLS_LABELS;
1214 for (i = 0; i < len; i++) {
1216 wc->masks.mpls_lse[i] |= htonl(MPLS_BOS_MASK);
1218 if (flow->mpls_lse[i] & htonl(MPLS_BOS_MASK)) {
1229 /* Returns the number consecutive of MPLS LSEs, starting at the
1230 * innermost LSE, that are common in 'a' and 'b'.
1232 * 'an' must be flow_count_mpls_labels(a).
1233 * 'bn' must be flow_count_mpls_labels(b).
1236 flow_count_common_mpls_labels(const struct flow *a, int an,
1237 const struct flow *b, int bn,
1238 struct flow_wildcards *wc)
1240 int min_n = MIN(an, bn);
1245 int a_last = an - 1;
1246 int b_last = bn - 1;
1249 for (i = 0; i < min_n; i++) {
1251 wc->masks.mpls_lse[a_last - i] = OVS_BE32_MAX;
1252 wc->masks.mpls_lse[b_last - i] = OVS_BE32_MAX;
1254 if (a->mpls_lse[a_last - i] != b->mpls_lse[b_last - i]) {
1265 /* Adds a new outermost MPLS label to 'flow' and changes 'flow''s Ethernet type
1266 * to 'mpls_eth_type', which must be an MPLS Ethertype.
1268 * If the new label is the first MPLS label in 'flow', it is generated as;
1270 * - label: 2, if 'flow' is IPv6, otherwise 0.
1272 * - TTL: IPv4 or IPv6 TTL, if present and nonzero, otherwise 64.
1274 * - TC: IPv4 or IPv6 TOS, if present, otherwise 0.
1278 * If the new label is the second or label MPLS label in 'flow', it is
1281 * - label: Copied from outer label.
1283 * - TTL: Copied from outer label.
1285 * - TC: Copied from outer label.
1289 * 'n' must be flow_count_mpls_labels(flow). 'n' must be less than
1290 * FLOW_MAX_MPLS_LABELS (because otherwise flow->mpls_lse[] would overflow).
1293 flow_push_mpls(struct flow *flow, int n, ovs_be16 mpls_eth_type,
1294 struct flow_wildcards *wc)
1296 ovs_assert(eth_type_mpls(mpls_eth_type));
1297 ovs_assert(n < FLOW_MAX_MPLS_LABELS);
1299 memset(wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1303 for (i = n; i >= 1; i--) {
1304 flow->mpls_lse[i] = flow->mpls_lse[i - 1];
1306 flow->mpls_lse[0] = (flow->mpls_lse[1]
1307 & htonl(~MPLS_BOS_MASK));
1309 int label = 0; /* IPv4 Explicit Null. */
1313 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1317 if (is_ip_any(flow)) {
1318 tc = (flow->nw_tos & IP_DSCP_MASK) >> 2;
1319 wc->masks.nw_tos |= IP_DSCP_MASK;
1324 wc->masks.nw_ttl = 0xff;
1327 flow->mpls_lse[0] = set_mpls_lse_values(ttl, tc, 1, htonl(label));
1329 /* Clear all L3 and L4 fields. */
1330 BUILD_ASSERT(FLOW_WC_SEQ == 27);
1331 memset((char *) flow + FLOW_SEGMENT_2_ENDS_AT, 0,
1332 sizeof(struct flow) - FLOW_SEGMENT_2_ENDS_AT);
1334 flow->dl_type = mpls_eth_type;
1337 /* Tries to remove the outermost MPLS label from 'flow'. Returns true if
1338 * successful, false otherwise. On success, sets 'flow''s Ethernet type to
1341 * 'n' must be flow_count_mpls_labels(flow). */
1343 flow_pop_mpls(struct flow *flow, int n, ovs_be16 eth_type,
1344 struct flow_wildcards *wc)
1349 /* Nothing to pop. */
1351 } else if (n == FLOW_MAX_MPLS_LABELS
1352 && !(flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK))) {
1353 /* Can't pop because we don't know what to fill in mpls_lse[n - 1]. */
1357 memset(wc->masks.mpls_lse, 0xff, sizeof wc->masks.mpls_lse);
1358 for (i = 1; i < n; i++) {
1359 flow->mpls_lse[i - 1] = flow->mpls_lse[i];
1361 flow->mpls_lse[n - 1] = 0;
1362 flow->dl_type = eth_type;
1366 /* Sets the MPLS Label that 'flow' matches to 'label', which is interpreted
1367 * as an OpenFlow 1.1 "mpls_label" value. */
1369 flow_set_mpls_label(struct flow *flow, int idx, ovs_be32 label)
1371 set_mpls_lse_label(&flow->mpls_lse[idx], label);
1374 /* Sets the MPLS TTL that 'flow' matches to 'ttl', which should be in the
1377 flow_set_mpls_ttl(struct flow *flow, int idx, uint8_t ttl)
1379 set_mpls_lse_ttl(&flow->mpls_lse[idx], ttl);
1382 /* Sets the MPLS TC that 'flow' matches to 'tc', which should be in the
1385 flow_set_mpls_tc(struct flow *flow, int idx, uint8_t tc)
1387 set_mpls_lse_tc(&flow->mpls_lse[idx], tc);
1390 /* Sets the MPLS BOS bit that 'flow' matches to which should be 0 or 1. */
1392 flow_set_mpls_bos(struct flow *flow, int idx, uint8_t bos)
1394 set_mpls_lse_bos(&flow->mpls_lse[idx], bos);
1397 /* Sets the entire MPLS LSE. */
1399 flow_set_mpls_lse(struct flow *flow, int idx, ovs_be32 lse)
1401 flow->mpls_lse[idx] = lse;
1405 flow_compose_l4(struct ofpbuf *b, const struct flow *flow)
1409 if (!(flow->nw_frag & FLOW_NW_FRAG_ANY)
1410 || !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1411 if (flow->nw_proto == IPPROTO_TCP) {
1412 struct tcp_header *tcp;
1414 l4_len = sizeof *tcp;
1415 tcp = ofpbuf_put_zeros(b, l4_len);
1416 tcp->tcp_src = flow->tp_src;
1417 tcp->tcp_dst = flow->tp_dst;
1418 tcp->tcp_ctl = TCP_CTL(ntohs(flow->tcp_flags), 5);
1419 } else if (flow->nw_proto == IPPROTO_UDP) {
1420 struct udp_header *udp;
1422 l4_len = sizeof *udp;
1423 udp = ofpbuf_put_zeros(b, l4_len);
1424 udp->udp_src = flow->tp_src;
1425 udp->udp_dst = flow->tp_dst;
1426 } else if (flow->nw_proto == IPPROTO_SCTP) {
1427 struct sctp_header *sctp;
1429 l4_len = sizeof *sctp;
1430 sctp = ofpbuf_put_zeros(b, l4_len);
1431 sctp->sctp_src = flow->tp_src;
1432 sctp->sctp_dst = flow->tp_dst;
1433 } else if (flow->nw_proto == IPPROTO_ICMP) {
1434 struct icmp_header *icmp;
1436 l4_len = sizeof *icmp;
1437 icmp = ofpbuf_put_zeros(b, l4_len);
1438 icmp->icmp_type = ntohs(flow->tp_src);
1439 icmp->icmp_code = ntohs(flow->tp_dst);
1440 icmp->icmp_csum = csum(icmp, ICMP_HEADER_LEN);
1441 } else if (flow->nw_proto == IPPROTO_IGMP) {
1442 struct igmp_header *igmp;
1444 l4_len = sizeof *igmp;
1445 igmp = ofpbuf_put_zeros(b, l4_len);
1446 igmp->igmp_type = ntohs(flow->tp_src);
1447 igmp->igmp_code = ntohs(flow->tp_dst);
1448 put_16aligned_be32(&igmp->group, flow->igmp_group_ip4);
1449 igmp->igmp_csum = csum(igmp, IGMP_HEADER_LEN);
1450 } else if (flow->nw_proto == IPPROTO_ICMPV6) {
1451 struct icmp6_hdr *icmp;
1453 l4_len = sizeof *icmp;
1454 icmp = ofpbuf_put_zeros(b, l4_len);
1455 icmp->icmp6_type = ntohs(flow->tp_src);
1456 icmp->icmp6_code = ntohs(flow->tp_dst);
1458 if (icmp->icmp6_code == 0 &&
1459 (icmp->icmp6_type == ND_NEIGHBOR_SOLICIT ||
1460 icmp->icmp6_type == ND_NEIGHBOR_ADVERT)) {
1461 struct in6_addr *nd_target;
1462 struct nd_opt_hdr *nd_opt;
1464 l4_len += sizeof *nd_target;
1465 nd_target = ofpbuf_put_zeros(b, sizeof *nd_target);
1466 *nd_target = flow->nd_target;
1468 if (!eth_addr_is_zero(flow->arp_sha)) {
1470 nd_opt = ofpbuf_put_zeros(b, 8);
1471 nd_opt->nd_opt_len = 1;
1472 nd_opt->nd_opt_type = ND_OPT_SOURCE_LINKADDR;
1473 memcpy(nd_opt + 1, flow->arp_sha, ETH_ADDR_LEN);
1475 if (!eth_addr_is_zero(flow->arp_tha)) {
1477 nd_opt = ofpbuf_put_zeros(b, 8);
1478 nd_opt->nd_opt_len = 1;
1479 nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
1480 memcpy(nd_opt + 1, flow->arp_tha, ETH_ADDR_LEN);
1483 icmp->icmp6_cksum = (OVS_FORCE uint16_t)
1484 csum(icmp, (char *)ofpbuf_tail(b) - (char *)icmp);
1490 /* Puts into 'b' a packet that flow_extract() would parse as having the given
1493 * (This is useful only for testing, obviously, and the packet isn't really
1494 * valid. It hasn't got some checksums filled in, for one, and lots of fields
1495 * are just zeroed.) */
1497 flow_compose(struct ofpbuf *b, const struct flow *flow)
1501 /* eth_compose() sets l3 pointer and makes sure it is 32-bit aligned. */
1502 eth_compose(b, flow->dl_dst, flow->dl_src, ntohs(flow->dl_type), 0);
1503 if (flow->dl_type == htons(FLOW_DL_TYPE_NONE)) {
1504 struct eth_header *eth = ofpbuf_l2(b);
1505 eth->eth_type = htons(ofpbuf_size(b));
1509 if (flow->vlan_tci & htons(VLAN_CFI)) {
1510 eth_push_vlan(b, htons(ETH_TYPE_VLAN), flow->vlan_tci);
1513 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1514 struct ip_header *ip;
1516 ip = ofpbuf_put_zeros(b, sizeof *ip);
1517 ip->ip_ihl_ver = IP_IHL_VER(5, 4);
1518 ip->ip_tos = flow->nw_tos;
1519 ip->ip_ttl = flow->nw_ttl;
1520 ip->ip_proto = flow->nw_proto;
1521 put_16aligned_be32(&ip->ip_src, flow->nw_src);
1522 put_16aligned_be32(&ip->ip_dst, flow->nw_dst);
1524 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
1525 ip->ip_frag_off |= htons(IP_MORE_FRAGMENTS);
1526 if (flow->nw_frag & FLOW_NW_FRAG_LATER) {
1527 ip->ip_frag_off |= htons(100);
1531 ofpbuf_set_l4(b, ofpbuf_tail(b));
1533 l4_len = flow_compose_l4(b, flow);
1536 ip->ip_tot_len = htons(b->l4_ofs - b->l3_ofs + l4_len);
1537 ip->ip_csum = csum(ip, sizeof *ip);
1538 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1539 struct ovs_16aligned_ip6_hdr *nh;
1541 nh = ofpbuf_put_zeros(b, sizeof *nh);
1542 put_16aligned_be32(&nh->ip6_flow, htonl(6 << 28) |
1543 htonl(flow->nw_tos << 20) | flow->ipv6_label);
1544 nh->ip6_hlim = flow->nw_ttl;
1545 nh->ip6_nxt = flow->nw_proto;
1547 memcpy(&nh->ip6_src, &flow->ipv6_src, sizeof(nh->ip6_src));
1548 memcpy(&nh->ip6_dst, &flow->ipv6_dst, sizeof(nh->ip6_dst));
1550 ofpbuf_set_l4(b, ofpbuf_tail(b));
1552 l4_len = flow_compose_l4(b, flow);
1555 nh->ip6_plen = htons(l4_len);
1556 } else if (flow->dl_type == htons(ETH_TYPE_ARP) ||
1557 flow->dl_type == htons(ETH_TYPE_RARP)) {
1558 struct arp_eth_header *arp;
1560 arp = ofpbuf_put_zeros(b, sizeof *arp);
1561 ofpbuf_set_l3(b, arp);
1562 arp->ar_hrd = htons(1);
1563 arp->ar_pro = htons(ETH_TYPE_IP);
1564 arp->ar_hln = ETH_ADDR_LEN;
1566 arp->ar_op = htons(flow->nw_proto);
1568 if (flow->nw_proto == ARP_OP_REQUEST ||
1569 flow->nw_proto == ARP_OP_REPLY) {
1570 put_16aligned_be32(&arp->ar_spa, flow->nw_src);
1571 put_16aligned_be32(&arp->ar_tpa, flow->nw_dst);
1572 memcpy(arp->ar_sha, flow->arp_sha, ETH_ADDR_LEN);
1573 memcpy(arp->ar_tha, flow->arp_tha, ETH_ADDR_LEN);
1577 if (eth_type_mpls(flow->dl_type)) {
1580 b->l2_5_ofs = b->l3_ofs;
1581 for (n = 1; n < FLOW_MAX_MPLS_LABELS; n++) {
1582 if (flow->mpls_lse[n - 1] & htonl(MPLS_BOS_MASK)) {
1587 push_mpls(b, flow->dl_type, flow->mpls_lse[--n]);
1592 /* Compressed flow. */
1595 miniflow_n_values(const struct miniflow *flow)
1597 return count_1bits(flow->map);
1601 miniflow_alloc_values(struct miniflow *flow, int n)
1603 int size = MINIFLOW_VALUES_SIZE(n);
1605 if (size <= sizeof flow->inline_values) {
1606 flow->values_inline = true;
1607 return flow->inline_values;
1609 COVERAGE_INC(miniflow_malloc);
1610 flow->values_inline = false;
1611 flow->offline_values = xmalloc(size);
1612 return flow->offline_values;
1616 /* Completes an initialization of 'dst' as a miniflow copy of 'src' begun by
1617 * the caller. The caller must have already initialized 'dst->map' properly
1618 * to indicate the significant uint32_t elements of 'src'. 'n' must be the
1619 * number of 1-bits in 'dst->map'.
1621 * Normally the significant elements are the ones that are non-zero. However,
1622 * when a miniflow is initialized from a (mini)mask, the values can be zeroes,
1623 * so that the flow and mask always have the same maps.
1625 * This function initializes values (either inline if possible or with
1626 * malloc() otherwise) and copies the uint32_t elements of 'src' indicated by
1627 * 'dst->map' into it. */
1629 miniflow_init__(struct miniflow *dst, const struct flow *src, int n)
1631 const uint32_t *src_u32 = (const uint32_t *) src;
1632 uint32_t *dst_u32 = miniflow_alloc_values(dst, n);
1635 for (map = dst->map; map; map = zero_rightmost_1bit(map)) {
1636 *dst_u32++ = src_u32[raw_ctz(map)];
1640 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1641 * with miniflow_destroy().
1642 * Always allocates offline storage. */
1644 miniflow_init(struct miniflow *dst, const struct flow *src)
1646 const uint32_t *src_u32 = (const uint32_t *) src;
1650 /* Initialize dst->map, counting the number of nonzero elements. */
1654 for (i = 0; i < FLOW_U32S; i++) {
1656 dst->map |= UINT64_C(1) << i;
1661 miniflow_init__(dst, src, n);
1664 /* Initializes 'dst' as a copy of 'src', using 'mask->map' as 'dst''s map. The
1665 * caller must eventually free 'dst' with miniflow_destroy(). */
1667 miniflow_init_with_minimask(struct miniflow *dst, const struct flow *src,
1668 const struct minimask *mask)
1670 dst->map = mask->masks.map;
1671 miniflow_init__(dst, src, miniflow_n_values(dst));
1674 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1675 * with miniflow_destroy(). */
1677 miniflow_clone(struct miniflow *dst, const struct miniflow *src)
1679 int size = MINIFLOW_VALUES_SIZE(miniflow_n_values(src));
1682 dst->map = src->map;
1683 if (size <= sizeof dst->inline_values) {
1684 dst->values_inline = true;
1685 values = dst->inline_values;
1687 dst->values_inline = false;
1688 COVERAGE_INC(miniflow_malloc);
1689 dst->offline_values = xmalloc(size);
1690 values = dst->offline_values;
1692 memcpy(values, miniflow_get_values(src), size);
1695 /* Initializes 'dst' as a copy of 'src'. The caller must have allocated
1696 * 'dst' to have inline space all data in 'src'. */
1698 miniflow_clone_inline(struct miniflow *dst, const struct miniflow *src,
1701 dst->values_inline = true;
1702 dst->map = src->map;
1703 memcpy(dst->inline_values, miniflow_get_values(src),
1704 MINIFLOW_VALUES_SIZE(n_values));
1707 /* Initializes 'dst' with the data in 'src', destroying 'src'.
1708 * The caller must eventually free 'dst' with miniflow_destroy().
1709 * 'dst' must be regularly sized miniflow, but 'src' can have
1710 * larger than default inline values. */
1712 miniflow_move(struct miniflow *dst, struct miniflow *src)
1714 int size = MINIFLOW_VALUES_SIZE(miniflow_n_values(src));
1716 dst->map = src->map;
1717 if (size <= sizeof dst->inline_values) {
1718 dst->values_inline = true;
1719 memcpy(dst->inline_values, miniflow_get_values(src), size);
1720 miniflow_destroy(src);
1721 } else if (src->values_inline) {
1722 dst->values_inline = false;
1723 COVERAGE_INC(miniflow_malloc);
1724 dst->offline_values = xmalloc(size);
1725 memcpy(dst->offline_values, src->inline_values, size);
1727 dst->values_inline = false;
1728 dst->offline_values = src->offline_values;
1732 /* Frees any memory owned by 'flow'. Does not free the storage in which 'flow'
1733 * itself resides; the caller is responsible for that. */
1735 miniflow_destroy(struct miniflow *flow)
1737 if (!flow->values_inline) {
1738 free(flow->offline_values);
1742 /* Initializes 'dst' as a copy of 'src'. */
1744 miniflow_expand(const struct miniflow *src, struct flow *dst)
1746 memset(dst, 0, sizeof *dst);
1747 flow_union_with_miniflow(dst, src);
1750 /* Returns the uint32_t that would be at byte offset '4 * u32_ofs' if 'flow'
1751 * were expanded into a "struct flow". */
1753 miniflow_get(const struct miniflow *flow, unsigned int u32_ofs)
1755 return (flow->map & UINT64_C(1) << u32_ofs)
1756 ? *(miniflow_get_u32_values(flow) +
1757 count_1bits(flow->map & ((UINT64_C(1) << u32_ofs) - 1)))
1761 /* Returns true if 'a' and 'b' are the same flow, false otherwise. */
1763 miniflow_equal(const struct miniflow *a, const struct miniflow *b)
1765 const uint32_t *ap = miniflow_get_u32_values(a);
1766 const uint32_t *bp = miniflow_get_u32_values(b);
1767 const uint64_t a_map = a->map;
1768 const uint64_t b_map = b->map;
1770 if (OVS_LIKELY(a_map == b_map)) {
1771 int count = miniflow_n_values(a);
1773 return !memcmp(ap, bp, count * sizeof *ap);
1777 for (map = a_map | b_map; map; map = zero_rightmost_1bit(map)) {
1778 uint64_t bit = rightmost_1bit(map);
1779 uint64_t a_value = a_map & bit ? *ap++ : 0;
1780 uint64_t b_value = b_map & bit ? *bp++ : 0;
1782 if (a_value != b_value) {
1791 /* Returns true if 'a' and 'b' are equal at the places where there are 1-bits
1792 * in 'mask', false if they differ. */
1794 miniflow_equal_in_minimask(const struct miniflow *a, const struct miniflow *b,
1795 const struct minimask *mask)
1797 const uint32_t *p = miniflow_get_u32_values(&mask->masks);
1800 for (map = mask->masks.map; map; map = zero_rightmost_1bit(map)) {
1801 int ofs = raw_ctz(map);
1803 if ((miniflow_get(a, ofs) ^ miniflow_get(b, ofs)) & *p++) {
1811 /* Returns true if 'a' and 'b' are equal at the places where there are 1-bits
1812 * in 'mask', false if they differ. */
1814 miniflow_equal_flow_in_minimask(const struct miniflow *a, const struct flow *b,
1815 const struct minimask *mask)
1817 const uint32_t *b_u32 = (const uint32_t *) b;
1818 const uint32_t *p = miniflow_get_u32_values(&mask->masks);
1821 for (map = mask->masks.map; map; map = zero_rightmost_1bit(map)) {
1822 int ofs = raw_ctz(map);
1824 if ((miniflow_get(a, ofs) ^ b_u32[ofs]) & *p++) {
1833 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1834 * with minimask_destroy(). */
1836 minimask_init(struct minimask *mask, const struct flow_wildcards *wc)
1838 miniflow_init(&mask->masks, &wc->masks);
1841 /* Initializes 'dst' as a copy of 'src'. The caller must eventually free 'dst'
1842 * with minimask_destroy(). */
1844 minimask_clone(struct minimask *dst, const struct minimask *src)
1846 miniflow_clone(&dst->masks, &src->masks);
1849 /* Initializes 'dst' with the data in 'src', destroying 'src'.
1850 * The caller must eventually free 'dst' with minimask_destroy(). */
1852 minimask_move(struct minimask *dst, struct minimask *src)
1854 miniflow_move(&dst->masks, &src->masks);
1857 /* Initializes 'dst_' as the bit-wise "and" of 'a_' and 'b_'.
1859 * The caller must provide room for FLOW_U32S "uint32_t"s in 'storage', for use
1860 * by 'dst_'. The caller must *not* free 'dst_' with minimask_destroy(). */
1862 minimask_combine(struct minimask *dst_,
1863 const struct minimask *a_, const struct minimask *b_,
1864 uint32_t storage[FLOW_U32S])
1866 struct miniflow *dst = &dst_->masks;
1867 uint32_t *dst_values = storage;
1868 const struct miniflow *a = &a_->masks;
1869 const struct miniflow *b = &b_->masks;
1873 dst->values_inline = false;
1874 dst->offline_values = storage;
1877 for (map = a->map & b->map; map; map = zero_rightmost_1bit(map)) {
1878 int ofs = raw_ctz(map);
1879 uint32_t mask = miniflow_get(a, ofs) & miniflow_get(b, ofs);
1882 dst->map |= rightmost_1bit(map);
1883 dst_values[n++] = mask;
1888 /* Frees any memory owned by 'mask'. Does not free the storage in which 'mask'
1889 * itself resides; the caller is responsible for that. */
1891 minimask_destroy(struct minimask *mask)
1893 miniflow_destroy(&mask->masks);
1896 /* Initializes 'dst' as a copy of 'src'. */
1898 minimask_expand(const struct minimask *mask, struct flow_wildcards *wc)
1900 miniflow_expand(&mask->masks, &wc->masks);
1903 /* Returns the uint32_t that would be at byte offset '4 * u32_ofs' if 'mask'
1904 * were expanded into a "struct flow_wildcards". */
1906 minimask_get(const struct minimask *mask, unsigned int u32_ofs)
1908 return miniflow_get(&mask->masks, u32_ofs);
1911 /* Returns true if 'a' and 'b' are the same flow mask, false otherwise. */
1913 minimask_equal(const struct minimask *a, const struct minimask *b)
1915 return miniflow_equal(&a->masks, &b->masks);
1918 /* Returns true if at least one bit matched by 'b' is wildcarded by 'a',
1919 * false otherwise. */
1921 minimask_has_extra(const struct minimask *a, const struct minimask *b)
1923 const uint32_t *p = miniflow_get_u32_values(&b->masks);
1926 for (map = b->masks.map; map; map = zero_rightmost_1bit(map)) {
1927 uint32_t a_u32 = minimask_get(a, raw_ctz(map));
1928 uint32_t b_u32 = *p++;
1930 if ((a_u32 & b_u32) != b_u32) {