2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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>
27 #include "byte-order.h"
29 #include "dynamic-string.h"
32 #include "openflow/openflow.h"
34 #include "unaligned.h"
37 VLOG_DEFINE_THIS_MODULE(flow);
39 COVERAGE_DEFINE(flow_extract);
41 static struct arp_eth_header *
42 pull_arp(struct ofpbuf *packet)
44 return ofpbuf_try_pull(packet, ARP_ETH_HEADER_LEN);
47 static struct ip_header *
48 pull_ip(struct ofpbuf *packet)
50 if (packet->size >= IP_HEADER_LEN) {
51 struct ip_header *ip = packet->data;
52 int ip_len = IP_IHL(ip->ip_ihl_ver) * 4;
53 if (ip_len >= IP_HEADER_LEN && packet->size >= ip_len) {
54 return ofpbuf_pull(packet, ip_len);
60 static struct tcp_header *
61 pull_tcp(struct ofpbuf *packet)
63 if (packet->size >= TCP_HEADER_LEN) {
64 struct tcp_header *tcp = packet->data;
65 int tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4;
66 if (tcp_len >= TCP_HEADER_LEN && packet->size >= tcp_len) {
67 return ofpbuf_pull(packet, tcp_len);
73 static struct udp_header *
74 pull_udp(struct ofpbuf *packet)
76 return ofpbuf_try_pull(packet, UDP_HEADER_LEN);
79 static struct icmp_header *
80 pull_icmp(struct ofpbuf *packet)
82 return ofpbuf_try_pull(packet, ICMP_HEADER_LEN);
85 static struct icmp6_hdr *
86 pull_icmpv6(struct ofpbuf *packet)
88 return ofpbuf_try_pull(packet, sizeof(struct icmp6_hdr));
92 parse_vlan(struct ofpbuf *b, struct flow *flow)
95 ovs_be16 eth_type; /* ETH_TYPE_VLAN */
99 if (b->size >= sizeof(struct qtag_prefix) + sizeof(ovs_be16)) {
100 struct qtag_prefix *qp = ofpbuf_pull(b, sizeof *qp);
101 flow->vlan_tci = qp->tci | htons(VLAN_CFI);
106 parse_ethertype(struct ofpbuf *b)
108 struct llc_snap_header *llc;
111 proto = *(ovs_be16 *) ofpbuf_pull(b, sizeof proto);
112 if (ntohs(proto) >= ETH_TYPE_MIN) {
116 if (b->size < sizeof *llc) {
117 return htons(FLOW_DL_TYPE_NONE);
121 if (llc->llc.llc_dsap != LLC_DSAP_SNAP
122 || llc->llc.llc_ssap != LLC_SSAP_SNAP
123 || llc->llc.llc_cntl != LLC_CNTL_SNAP
124 || memcmp(llc->snap.snap_org, SNAP_ORG_ETHERNET,
125 sizeof llc->snap.snap_org)) {
126 return htons(FLOW_DL_TYPE_NONE);
129 ofpbuf_pull(b, sizeof *llc);
130 return llc->snap.snap_type;
134 parse_ipv6(struct ofpbuf *packet, struct flow *flow)
136 const struct ip6_hdr *nh;
140 nh = ofpbuf_try_pull(packet, sizeof *nh);
145 nexthdr = nh->ip6_nxt;
147 flow->ipv6_src = nh->ip6_src;
148 flow->ipv6_dst = nh->ip6_dst;
150 tc_flow = get_unaligned_be32(&nh->ip6_flow);
151 flow->nw_tos = ntohl(tc_flow) >> 4;
152 flow->ipv6_label = tc_flow & htonl(IPV6_LABEL_MASK);
153 flow->nw_ttl = nh->ip6_hlim;
154 flow->nw_proto = IPPROTO_NONE;
157 if ((nexthdr != IPPROTO_HOPOPTS)
158 && (nexthdr != IPPROTO_ROUTING)
159 && (nexthdr != IPPROTO_DSTOPTS)
160 && (nexthdr != IPPROTO_AH)
161 && (nexthdr != IPPROTO_FRAGMENT)) {
162 /* It's either a terminal header (e.g., TCP, UDP) or one we
163 * don't understand. In either case, we're done with the
164 * packet, so use it to fill in 'nw_proto'. */
168 /* We only verify that at least 8 bytes of the next header are
169 * available, but many of these headers are longer. Ensure that
170 * accesses within the extension header are within those first 8
171 * bytes. All extension headers are required to be at least 8
173 if (packet->size < 8) {
177 if ((nexthdr == IPPROTO_HOPOPTS)
178 || (nexthdr == IPPROTO_ROUTING)
179 || (nexthdr == IPPROTO_DSTOPTS)) {
180 /* These headers, while different, have the fields we care about
181 * in the same location and with the same interpretation. */
182 const struct ip6_ext *ext_hdr = (struct ip6_ext *)packet->data;
183 nexthdr = ext_hdr->ip6e_nxt;
184 if (!ofpbuf_try_pull(packet, (ext_hdr->ip6e_len + 1) * 8)) {
187 } else if (nexthdr == IPPROTO_AH) {
188 /* A standard AH definition isn't available, but the fields
189 * we care about are in the same location as the generic
190 * option header--only the header length is calculated
192 const struct ip6_ext *ext_hdr = (struct ip6_ext *)packet->data;
193 nexthdr = ext_hdr->ip6e_nxt;
194 if (!ofpbuf_try_pull(packet, (ext_hdr->ip6e_len + 2) * 4)) {
197 } else if (nexthdr == IPPROTO_FRAGMENT) {
198 const struct ip6_frag *frag_hdr = (struct ip6_frag *)packet->data;
200 nexthdr = frag_hdr->ip6f_nxt;
201 if (!ofpbuf_try_pull(packet, sizeof *frag_hdr)) {
205 /* We only process the first fragment. */
206 flow->nw_frag = FLOW_NW_FRAG_ANY;
207 if ((frag_hdr->ip6f_offlg & IP6F_OFF_MASK) != htons(0)) {
208 flow->nw_frag |= FLOW_NW_FRAG_LATER;
209 nexthdr = IPPROTO_FRAGMENT;
215 flow->nw_proto = nexthdr;
220 parse_tcp(struct ofpbuf *packet, struct ofpbuf *b, struct flow *flow)
222 const struct tcp_header *tcp = pull_tcp(b);
224 flow->tp_src = tcp->tcp_src;
225 flow->tp_dst = tcp->tcp_dst;
226 packet->l7 = b->data;
231 parse_udp(struct ofpbuf *packet, struct ofpbuf *b, struct flow *flow)
233 const struct udp_header *udp = pull_udp(b);
235 flow->tp_src = udp->udp_src;
236 flow->tp_dst = udp->udp_dst;
237 packet->l7 = b->data;
242 parse_icmpv6(struct ofpbuf *b, struct flow *flow)
244 const struct icmp6_hdr *icmp = pull_icmpv6(b);
250 /* The ICMPv6 type and code fields use the 16-bit transport port
251 * fields, so we need to store them in 16-bit network byte order. */
252 flow->tp_src = htons(icmp->icmp6_type);
253 flow->tp_dst = htons(icmp->icmp6_code);
255 if (icmp->icmp6_code == 0 &&
256 (icmp->icmp6_type == ND_NEIGHBOR_SOLICIT ||
257 icmp->icmp6_type == ND_NEIGHBOR_ADVERT)) {
258 const struct in6_addr *nd_target;
260 nd_target = ofpbuf_try_pull(b, sizeof *nd_target);
264 flow->nd_target = *nd_target;
266 while (b->size >= 8) {
267 /* The minimum size of an option is 8 bytes, which also is
268 * the size of Ethernet link-layer options. */
269 const struct nd_opt_hdr *nd_opt = b->data;
270 int opt_len = nd_opt->nd_opt_len * 8;
272 if (!opt_len || opt_len > b->size) {
276 /* Store the link layer address if the appropriate option is
277 * provided. It is considered an error if the same link
278 * layer option is specified twice. */
279 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LINKADDR
281 if (eth_addr_is_zero(flow->arp_sha)) {
282 memcpy(flow->arp_sha, nd_opt + 1, ETH_ADDR_LEN);
286 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LINKADDR
288 if (eth_addr_is_zero(flow->arp_tha)) {
289 memcpy(flow->arp_tha, nd_opt + 1, ETH_ADDR_LEN);
295 if (!ofpbuf_try_pull(b, opt_len)) {
304 memset(&flow->nd_target, 0, sizeof(flow->nd_target));
305 memset(flow->arp_sha, 0, sizeof(flow->arp_sha));
306 memset(flow->arp_tha, 0, sizeof(flow->arp_tha));
312 /* Initializes 'flow' members from 'packet', 'tun_id', and 'ofp_in_port'.
313 * Initializes 'packet' header pointers as follows:
315 * - packet->l2 to the start of the Ethernet header.
317 * - packet->l3 to just past the Ethernet header, or just past the
318 * vlan_header if one is present, to the first byte of the payload of the
321 * - packet->l4 to just past the IPv4 header, if one is present and has a
322 * correct length, and otherwise NULL.
324 * - packet->l7 to just past the TCP or UDP or ICMP header, if one is
325 * present and has a correct length, and otherwise NULL.
328 flow_extract(struct ofpbuf *packet, uint32_t priority, ovs_be64 tun_id,
329 uint16_t ofp_in_port, struct flow *flow)
331 struct ofpbuf b = *packet;
332 struct eth_header *eth;
334 COVERAGE_INC(flow_extract);
336 memset(flow, 0, sizeof *flow);
337 flow->tun_id = tun_id;
338 flow->in_port = ofp_in_port;
339 flow->priority = priority;
346 if (b.size < sizeof *eth) {
352 memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
353 memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
355 /* dl_type, vlan_tci. */
356 ofpbuf_pull(&b, ETH_ADDR_LEN * 2);
357 if (eth->eth_type == htons(ETH_TYPE_VLAN)) {
358 parse_vlan(&b, flow);
360 flow->dl_type = parse_ethertype(&b);
364 if (flow->dl_type == htons(ETH_TYPE_IP)) {
365 const struct ip_header *nh = pull_ip(&b);
369 flow->nw_src = get_unaligned_be32(&nh->ip_src);
370 flow->nw_dst = get_unaligned_be32(&nh->ip_dst);
371 flow->nw_proto = nh->ip_proto;
373 flow->nw_tos = nh->ip_tos;
374 if (IP_IS_FRAGMENT(nh->ip_frag_off)) {
375 flow->nw_frag = FLOW_NW_FRAG_ANY;
376 if (nh->ip_frag_off & htons(IP_FRAG_OFF_MASK)) {
377 flow->nw_frag |= FLOW_NW_FRAG_LATER;
380 flow->nw_ttl = nh->ip_ttl;
382 if (!(nh->ip_frag_off & htons(IP_FRAG_OFF_MASK))) {
383 if (flow->nw_proto == IPPROTO_TCP) {
384 parse_tcp(packet, &b, flow);
385 } else if (flow->nw_proto == IPPROTO_UDP) {
386 parse_udp(packet, &b, flow);
387 } else if (flow->nw_proto == IPPROTO_ICMP) {
388 const struct icmp_header *icmp = pull_icmp(&b);
390 flow->tp_src = htons(icmp->icmp_type);
391 flow->tp_dst = htons(icmp->icmp_code);
397 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
398 if (parse_ipv6(&b, flow)) {
403 if (flow->nw_proto == IPPROTO_TCP) {
404 parse_tcp(packet, &b, flow);
405 } else if (flow->nw_proto == IPPROTO_UDP) {
406 parse_udp(packet, &b, flow);
407 } else if (flow->nw_proto == IPPROTO_ICMPV6) {
408 if (parse_icmpv6(&b, flow)) {
412 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
413 const struct arp_eth_header *arp = pull_arp(&b);
414 if (arp && arp->ar_hrd == htons(1)
415 && arp->ar_pro == htons(ETH_TYPE_IP)
416 && arp->ar_hln == ETH_ADDR_LEN
417 && arp->ar_pln == 4) {
418 /* We only match on the lower 8 bits of the opcode. */
419 if (ntohs(arp->ar_op) <= 0xff) {
420 flow->nw_proto = ntohs(arp->ar_op);
423 if ((flow->nw_proto == ARP_OP_REQUEST)
424 || (flow->nw_proto == ARP_OP_REPLY)) {
425 flow->nw_src = arp->ar_spa;
426 flow->nw_dst = arp->ar_tpa;
427 memcpy(flow->arp_sha, arp->ar_sha, ETH_ADDR_LEN);
428 memcpy(flow->arp_tha, arp->ar_tha, ETH_ADDR_LEN);
434 /* For every bit of a field that is wildcarded in 'wildcards', sets the
435 * corresponding bit in 'flow' to zero. */
437 flow_zero_wildcards(struct flow *flow, const struct flow_wildcards *wildcards)
439 const flow_wildcards_t wc = wildcards->wildcards;
442 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 6);
444 for (i = 0; i < FLOW_N_REGS; i++) {
445 flow->regs[i] &= wildcards->reg_masks[i];
447 flow->tun_id &= wildcards->tun_id_mask;
448 flow->nw_src &= wildcards->nw_src_mask;
449 flow->nw_dst &= wildcards->nw_dst_mask;
450 if (wc & FWW_IN_PORT) {
453 flow->vlan_tci &= wildcards->vlan_tci_mask;
454 if (wc & FWW_DL_TYPE) {
457 if (wc & FWW_TP_SRC) {
460 if (wc & FWW_TP_DST) {
463 if (wc & FWW_DL_SRC) {
464 memset(flow->dl_src, 0, sizeof flow->dl_src);
466 if (wc & FWW_DL_DST) {
467 flow->dl_dst[0] &= 0x01;
468 memset(&flow->dl_dst[1], 0, 5);
470 if (wc & FWW_ETH_MCAST) {
471 flow->dl_dst[0] &= 0xfe;
473 if (wc & FWW_NW_PROTO) {
476 if (wc & FWW_IPV6_LABEL) {
477 flow->ipv6_label = htonl(0);
479 flow->nw_tos &= wildcards->nw_tos_mask;
480 if (wc & FWW_NW_TTL) {
483 flow->nw_frag &= wildcards->nw_frag_mask;
484 if (wc & FWW_ARP_SHA) {
485 memset(flow->arp_sha, 0, sizeof flow->arp_sha);
487 if (wc & FWW_ARP_THA) {
488 memset(flow->arp_tha, 0, sizeof flow->arp_tha);
490 flow->ipv6_src = ipv6_addr_bitand(&flow->ipv6_src,
491 &wildcards->ipv6_src_mask);
492 flow->ipv6_dst = ipv6_addr_bitand(&flow->ipv6_dst,
493 &wildcards->ipv6_dst_mask);
494 if (wc & FWW_ND_TARGET) {
495 memset(&flow->nd_target, 0, sizeof flow->nd_target);
501 flow_to_string(const struct flow *flow)
503 struct ds ds = DS_EMPTY_INITIALIZER;
504 flow_format(&ds, flow);
509 flow_format(struct ds *ds, const struct flow *flow)
511 ds_put_format(ds, "priority%"PRIu32
515 ntohll(flow->tun_id),
518 ds_put_format(ds, ":tci(");
519 if (flow->vlan_tci) {
520 ds_put_format(ds, "vlan%"PRIu16",pcp%d",
521 vlan_tci_to_vid(flow->vlan_tci),
522 vlan_tci_to_pcp(flow->vlan_tci));
524 ds_put_char(ds, '0');
526 ds_put_format(ds, ") mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT
528 ETH_ADDR_ARGS(flow->dl_src),
529 ETH_ADDR_ARGS(flow->dl_dst),
530 ntohs(flow->dl_type));
532 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
533 ds_put_format(ds, " label%#"PRIx32" proto%"PRIu8" tos%#"PRIx8
535 ntohl(flow->ipv6_label), flow->nw_proto,
536 flow->nw_tos, flow->nw_ttl);
537 print_ipv6_addr(ds, &flow->ipv6_src);
538 ds_put_cstr(ds, "->");
539 print_ipv6_addr(ds, &flow->ipv6_dst);
542 ds_put_format(ds, " proto%"PRIu8" tos%#"PRIx8" ttl%"PRIu8
543 " ip"IP_FMT"->"IP_FMT,
544 flow->nw_proto, flow->nw_tos, flow->nw_ttl,
545 IP_ARGS(&flow->nw_src), IP_ARGS(&flow->nw_dst));
548 ds_put_format(ds, " frag(%s)",
549 flow->nw_frag == FLOW_NW_FRAG_ANY ? "first"
550 : flow->nw_frag == (FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER)
551 ? "later" : "<error>");
553 if (flow->tp_src || flow->tp_dst) {
554 ds_put_format(ds, " port%"PRIu16"->%"PRIu16,
555 ntohs(flow->tp_src), ntohs(flow->tp_dst));
557 if (!eth_addr_is_zero(flow->arp_sha) || !eth_addr_is_zero(flow->arp_tha)) {
558 ds_put_format(ds, " arp_ha"ETH_ADDR_FMT"->"ETH_ADDR_FMT,
559 ETH_ADDR_ARGS(flow->arp_sha),
560 ETH_ADDR_ARGS(flow->arp_tha));
565 flow_print(FILE *stream, const struct flow *flow)
567 char *s = flow_to_string(flow);
572 /* flow_wildcards functions. */
574 /* Initializes 'wc' as a set of wildcards that matches every packet. */
576 flow_wildcards_init_catchall(struct flow_wildcards *wc)
578 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 6);
580 wc->wildcards = FWW_ALL;
581 wc->tun_id_mask = htonll(0);
582 wc->nw_src_mask = htonl(0);
583 wc->nw_dst_mask = htonl(0);
584 wc->ipv6_src_mask = in6addr_any;
585 wc->ipv6_dst_mask = in6addr_any;
586 memset(wc->reg_masks, 0, sizeof wc->reg_masks);
587 wc->vlan_tci_mask = htons(0);
589 wc->nw_frag_mask = 0;
590 memset(wc->zeros, 0, sizeof wc->zeros);
593 /* Initializes 'wc' as an exact-match set of wildcards; that is, 'wc' does not
594 * wildcard any bits or fields. */
596 flow_wildcards_init_exact(struct flow_wildcards *wc)
598 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 6);
601 wc->tun_id_mask = htonll(UINT64_MAX);
602 wc->nw_src_mask = htonl(UINT32_MAX);
603 wc->nw_dst_mask = htonl(UINT32_MAX);
604 wc->ipv6_src_mask = in6addr_exact;
605 wc->ipv6_dst_mask = in6addr_exact;
606 memset(wc->reg_masks, 0xff, sizeof wc->reg_masks);
607 wc->vlan_tci_mask = htons(UINT16_MAX);
608 wc->nw_tos_mask = UINT8_MAX;
609 wc->nw_frag_mask = UINT8_MAX;
610 memset(wc->zeros, 0, sizeof wc->zeros);
613 /* Returns true if 'wc' is exact-match, false if 'wc' wildcards any bits or
616 flow_wildcards_is_exact(const struct flow_wildcards *wc)
620 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 6);
623 || wc->tun_id_mask != htonll(UINT64_MAX)
624 || wc->nw_src_mask != htonl(UINT32_MAX)
625 || wc->nw_dst_mask != htonl(UINT32_MAX)
626 || wc->vlan_tci_mask != htons(UINT16_MAX)
627 || !ipv6_mask_is_exact(&wc->ipv6_src_mask)
628 || !ipv6_mask_is_exact(&wc->ipv6_dst_mask)
629 || wc->nw_tos_mask != UINT8_MAX
630 || wc->nw_frag_mask != UINT8_MAX) {
634 for (i = 0; i < FLOW_N_REGS; i++) {
635 if (wc->reg_masks[i] != UINT32_MAX) {
643 /* Returns true if 'wc' matches every packet, false if 'wc' fixes any bits or
646 flow_wildcards_is_catchall(const struct flow_wildcards *wc)
650 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 6);
652 if (wc->wildcards != FWW_ALL
653 || wc->tun_id_mask != htonll(0)
654 || wc->nw_src_mask != htonl(0)
655 || wc->nw_dst_mask != htonl(0)
656 || wc->vlan_tci_mask != htons(0)
657 || !ipv6_mask_is_any(&wc->ipv6_src_mask)
658 || !ipv6_mask_is_any(&wc->ipv6_dst_mask)
659 || wc->nw_tos_mask != 0
660 || wc->nw_frag_mask != 0) {
664 for (i = 0; i < FLOW_N_REGS; i++) {
665 if (wc->reg_masks[i] != 0) {
673 /* Initializes 'dst' as the combination of wildcards in 'src1' and 'src2'.
674 * That is, a bit or a field is wildcarded in 'dst' if it is wildcarded in
675 * 'src1' or 'src2' or both. */
677 flow_wildcards_combine(struct flow_wildcards *dst,
678 const struct flow_wildcards *src1,
679 const struct flow_wildcards *src2)
683 dst->wildcards = src1->wildcards | src2->wildcards;
684 dst->tun_id_mask = src1->tun_id_mask & src2->tun_id_mask;
685 dst->nw_src_mask = src1->nw_src_mask & src2->nw_src_mask;
686 dst->nw_dst_mask = src1->nw_dst_mask & src2->nw_dst_mask;
687 dst->ipv6_src_mask = ipv6_addr_bitand(&src1->ipv6_src_mask,
688 &src2->ipv6_src_mask);
689 dst->ipv6_dst_mask = ipv6_addr_bitand(&src1->ipv6_dst_mask,
690 &src2->ipv6_dst_mask);
691 for (i = 0; i < FLOW_N_REGS; i++) {
692 dst->reg_masks[i] = src1->reg_masks[i] & src2->reg_masks[i];
694 dst->vlan_tci_mask = src1->vlan_tci_mask & src2->vlan_tci_mask;
697 /* Returns a hash of the wildcards in 'wc'. */
699 flow_wildcards_hash(const struct flow_wildcards *wc, uint32_t basis)
701 /* If you change struct flow_wildcards and thereby trigger this
702 * assertion, please check that the new struct flow_wildcards has no holes
703 * in it before you update the assertion. */
704 BUILD_ASSERT_DECL(sizeof *wc == 60 + FLOW_N_REGS * 4);
705 return hash_bytes(wc, sizeof *wc, basis);
708 /* Returns true if 'a' and 'b' represent the same wildcards, false if they are
711 flow_wildcards_equal(const struct flow_wildcards *a,
712 const struct flow_wildcards *b)
716 if (a->wildcards != b->wildcards
717 || a->tun_id_mask != b->tun_id_mask
718 || a->nw_src_mask != b->nw_src_mask
719 || a->nw_dst_mask != b->nw_dst_mask
720 || a->vlan_tci_mask != b->vlan_tci_mask
721 || !ipv6_addr_equals(&a->ipv6_src_mask, &b->ipv6_src_mask)
722 || !ipv6_addr_equals(&a->ipv6_dst_mask, &b->ipv6_dst_mask)) {
726 for (i = 0; i < FLOW_N_REGS; i++) {
727 if (a->reg_masks[i] != b->reg_masks[i]) {
735 /* Returns true if at least one bit or field is wildcarded in 'a' but not in
736 * 'b', false otherwise. */
738 flow_wildcards_has_extra(const struct flow_wildcards *a,
739 const struct flow_wildcards *b)
742 struct in6_addr ipv6_masked;
744 for (i = 0; i < FLOW_N_REGS; i++) {
745 if ((a->reg_masks[i] & b->reg_masks[i]) != b->reg_masks[i]) {
750 ipv6_masked = ipv6_addr_bitand(&a->ipv6_src_mask, &b->ipv6_src_mask);
751 if (!ipv6_addr_equals(&ipv6_masked, &b->ipv6_src_mask)) {
755 ipv6_masked = ipv6_addr_bitand(&a->ipv6_dst_mask, &b->ipv6_dst_mask);
756 if (!ipv6_addr_equals(&ipv6_masked, &b->ipv6_dst_mask)) {
760 return (a->wildcards & ~b->wildcards
761 || (a->tun_id_mask & b->tun_id_mask) != b->tun_id_mask
762 || (a->nw_src_mask & b->nw_src_mask) != b->nw_src_mask
763 || (a->nw_dst_mask & b->nw_dst_mask) != b->nw_dst_mask
764 || (a->vlan_tci_mask & b->vlan_tci_mask) != b->vlan_tci_mask);
767 /* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
768 * (A 0-bit indicates a wildcard bit.) */
770 flow_wildcards_set_reg_mask(struct flow_wildcards *wc, int idx, uint32_t mask)
772 wc->reg_masks[idx] = mask;
775 /* Returns the wildcard bitmask for the Ethernet destination address
776 * that 'wc' specifies. The bitmask has a 0 in each bit that is wildcarded
777 * and a 1 in each bit that must match. */
779 flow_wildcards_to_dl_dst_mask(flow_wildcards_t wc)
781 static const uint8_t no_wild[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
782 static const uint8_t addr_wild[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
783 static const uint8_t mcast_wild[] = {0xfe, 0xff, 0xff, 0xff, 0xff, 0xff};
784 static const uint8_t all_wild[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
786 switch (wc & (FWW_DL_DST | FWW_ETH_MCAST)) {
787 case 0: return no_wild;
788 case FWW_DL_DST: return addr_wild;
789 case FWW_ETH_MCAST: return mcast_wild;
790 case FWW_DL_DST | FWW_ETH_MCAST: return all_wild;
795 /* Returns true if 'mask' is a valid wildcard bitmask for the Ethernet
796 * destination address. Valid bitmasks are either all-bits-0 or all-bits-1,
797 * except that the multicast bit may differ from the rest of the bits. So,
798 * there are four possible valid bitmasks:
800 * - 00:00:00:00:00:00
801 * - 01:00:00:00:00:00
802 * - fe:ff:ff:ff:ff:ff
803 * - ff:ff:ff:ff:ff:ff
805 * All other bitmasks are invalid. */
807 flow_wildcards_is_dl_dst_mask_valid(const uint8_t mask[ETH_ADDR_LEN])
812 return (mask[1] | mask[2] | mask[3] | mask[4] | mask[5]) == 0x00;
816 return (mask[1] & mask[2] & mask[3] & mask[4] & mask[5]) == 0xff;
823 /* Returns 'wc' with the FWW_DL_DST and FWW_ETH_MCAST bits modified
824 * appropriately to match 'mask'.
826 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
827 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
829 flow_wildcards_set_dl_dst_mask(flow_wildcards_t wc,
830 const uint8_t mask[ETH_ADDR_LEN])
832 assert(flow_wildcards_is_dl_dst_mask_valid(mask));
836 return wc | FWW_DL_DST | FWW_ETH_MCAST;
839 return (wc | FWW_DL_DST) & ~FWW_ETH_MCAST;
842 return (wc & ~FWW_DL_DST) | FWW_ETH_MCAST;
845 return wc & ~(FWW_DL_DST | FWW_ETH_MCAST);
852 /* Hashes 'flow' based on its L2 through L4 protocol information. */
854 flow_hash_symmetric_l4(const struct flow *flow, uint32_t basis)
859 struct in6_addr ipv6_addr;
864 uint8_t eth_addr[ETH_ADDR_LEN];
870 memset(&fields, 0, sizeof fields);
871 for (i = 0; i < ETH_ADDR_LEN; i++) {
872 fields.eth_addr[i] = flow->dl_src[i] ^ flow->dl_dst[i];
874 fields.vlan_tci = flow->vlan_tci & htons(VLAN_VID_MASK);
875 fields.eth_type = flow->dl_type;
877 /* UDP source and destination port are not taken into account because they
878 * will not necessarily be symmetric in a bidirectional flow. */
879 if (fields.eth_type == htons(ETH_TYPE_IP)) {
880 fields.ipv4_addr = flow->nw_src ^ flow->nw_dst;
881 fields.ip_proto = flow->nw_proto;
882 if (fields.ip_proto == IPPROTO_TCP) {
883 fields.tp_addr = flow->tp_src ^ flow->tp_dst;
885 } else if (fields.eth_type == htons(ETH_TYPE_IPV6)) {
886 const uint8_t *a = &flow->ipv6_src.s6_addr[0];
887 const uint8_t *b = &flow->ipv6_dst.s6_addr[0];
888 uint8_t *ipv6_addr = &fields.ipv6_addr.s6_addr[0];
890 for (i=0; i<16; i++) {
891 ipv6_addr[i] = a[i] ^ b[i];
893 fields.ip_proto = flow->nw_proto;
894 if (fields.ip_proto == IPPROTO_TCP) {
895 fields.tp_addr = flow->tp_src ^ flow->tp_dst;
898 return hash_bytes(&fields, sizeof fields, basis);
901 /* Hashes the portions of 'flow' designated by 'fields'. */
903 flow_hash_fields(const struct flow *flow, enum nx_hash_fields fields,
908 case NX_HASH_FIELDS_ETH_SRC:
909 return hash_bytes(flow->dl_src, sizeof flow->dl_src, basis);
911 case NX_HASH_FIELDS_SYMMETRIC_L4:
912 return flow_hash_symmetric_l4(flow, basis);
918 /* Returns a string representation of 'fields'. */
920 flow_hash_fields_to_str(enum nx_hash_fields fields)
923 case NX_HASH_FIELDS_ETH_SRC: return "eth_src";
924 case NX_HASH_FIELDS_SYMMETRIC_L4: return "symmetric_l4";
925 default: return "<unknown>";
929 /* Returns true if the value of 'fields' is supported. Otherwise false. */
931 flow_hash_fields_valid(enum nx_hash_fields fields)
933 return fields == NX_HASH_FIELDS_ETH_SRC
934 || fields == NX_HASH_FIELDS_SYMMETRIC_L4;
937 /* Puts into 'b' a packet that flow_extract() would parse as having the given
940 * (This is useful only for testing, obviously, and the packet isn't really
941 * valid. It hasn't got any checksums filled in, for one, and lots of fields
942 * are just zeroed.) */
944 flow_compose(struct ofpbuf *b, const struct flow *flow)
946 eth_compose(b, flow->dl_dst, flow->dl_src, ntohs(flow->dl_type), 0);
947 if (flow->dl_type == htons(FLOW_DL_TYPE_NONE)) {
948 struct eth_header *eth = b->l2;
949 eth->eth_type = htons(b->size);
953 if (flow->vlan_tci & htons(VLAN_CFI)) {
954 eth_push_vlan(b, flow->vlan_tci & ~htons(VLAN_CFI));
957 if (flow->dl_type == htons(ETH_TYPE_IP)) {
958 struct ip_header *ip;
960 b->l3 = ip = ofpbuf_put_zeros(b, sizeof *ip);
961 ip->ip_ihl_ver = IP_IHL_VER(5, 4);
962 ip->ip_tos = flow->nw_tos;
963 ip->ip_proto = flow->nw_proto;
964 ip->ip_src = flow->nw_src;
965 ip->ip_dst = flow->nw_dst;
967 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
968 ip->ip_frag_off |= htons(IP_MORE_FRAGMENTS);
969 if (flow->nw_frag & FLOW_NW_FRAG_LATER) {
970 ip->ip_frag_off |= htons(100);
973 if (!(flow->nw_frag & FLOW_NW_FRAG_ANY)
974 || !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
975 if (flow->nw_proto == IPPROTO_TCP) {
976 struct tcp_header *tcp;
978 b->l4 = tcp = ofpbuf_put_zeros(b, sizeof *tcp);
979 tcp->tcp_src = flow->tp_src;
980 tcp->tcp_dst = flow->tp_dst;
981 } else if (flow->nw_proto == IPPROTO_UDP) {
982 struct udp_header *udp;
984 b->l4 = udp = ofpbuf_put_zeros(b, sizeof *udp);
985 udp->udp_src = flow->tp_src;
986 udp->udp_dst = flow->tp_dst;
987 } else if (flow->nw_proto == IPPROTO_ICMP) {
988 struct icmp_header *icmp;
990 b->l4 = icmp = ofpbuf_put_zeros(b, sizeof *icmp);
991 icmp->icmp_type = ntohs(flow->tp_src);
992 icmp->icmp_code = ntohs(flow->tp_dst);
995 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
997 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
998 struct arp_eth_header *arp;
1000 b->l3 = arp = ofpbuf_put_zeros(b, sizeof *arp);
1001 arp->ar_hrd = htons(1);
1002 arp->ar_pro = htons(ETH_TYPE_IP);
1003 arp->ar_hln = ETH_ADDR_LEN;
1005 arp->ar_op = htons(flow->nw_proto);
1007 if (flow->nw_proto == ARP_OP_REQUEST ||
1008 flow->nw_proto == ARP_OP_REPLY) {
1009 arp->ar_spa = flow->nw_src;
1010 arp->ar_tpa = flow->nw_dst;
1011 memcpy(arp->ar_sha, flow->arp_sha, ETH_ADDR_LEN);
1012 memcpy(arp->ar_tha, flow->arp_tha, ETH_ADDR_LEN);