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"
30 #include "dynamic-string.h"
33 #include "openflow/openflow.h"
35 #include "unaligned.h"
38 VLOG_DEFINE_THIS_MODULE(flow);
40 COVERAGE_DEFINE(flow_extract);
42 static struct arp_eth_header *
43 pull_arp(struct ofpbuf *packet)
45 return ofpbuf_try_pull(packet, ARP_ETH_HEADER_LEN);
48 static struct ip_header *
49 pull_ip(struct ofpbuf *packet)
51 if (packet->size >= IP_HEADER_LEN) {
52 struct ip_header *ip = packet->data;
53 int ip_len = IP_IHL(ip->ip_ihl_ver) * 4;
54 if (ip_len >= IP_HEADER_LEN && packet->size >= ip_len) {
55 return ofpbuf_pull(packet, ip_len);
61 static struct tcp_header *
62 pull_tcp(struct ofpbuf *packet)
64 if (packet->size >= TCP_HEADER_LEN) {
65 struct tcp_header *tcp = packet->data;
66 int tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4;
67 if (tcp_len >= TCP_HEADER_LEN && packet->size >= tcp_len) {
68 return ofpbuf_pull(packet, tcp_len);
74 static struct udp_header *
75 pull_udp(struct ofpbuf *packet)
77 return ofpbuf_try_pull(packet, UDP_HEADER_LEN);
80 static struct icmp_header *
81 pull_icmp(struct ofpbuf *packet)
83 return ofpbuf_try_pull(packet, ICMP_HEADER_LEN);
86 static struct icmp6_hdr *
87 pull_icmpv6(struct ofpbuf *packet)
89 return ofpbuf_try_pull(packet, sizeof(struct icmp6_hdr));
93 parse_vlan(struct ofpbuf *b, struct flow *flow)
96 ovs_be16 eth_type; /* ETH_TYPE_VLAN */
100 if (b->size >= sizeof(struct qtag_prefix) + sizeof(ovs_be16)) {
101 struct qtag_prefix *qp = ofpbuf_pull(b, sizeof *qp);
102 flow->vlan_tci = qp->tci | htons(VLAN_CFI);
107 parse_ethertype(struct ofpbuf *b)
109 struct llc_snap_header *llc;
112 proto = *(ovs_be16 *) ofpbuf_pull(b, sizeof proto);
113 if (ntohs(proto) >= ETH_TYPE_MIN) {
117 if (b->size < sizeof *llc) {
118 return htons(FLOW_DL_TYPE_NONE);
122 if (llc->llc.llc_dsap != LLC_DSAP_SNAP
123 || llc->llc.llc_ssap != LLC_SSAP_SNAP
124 || llc->llc.llc_cntl != LLC_CNTL_SNAP
125 || memcmp(llc->snap.snap_org, SNAP_ORG_ETHERNET,
126 sizeof llc->snap.snap_org)) {
127 return htons(FLOW_DL_TYPE_NONE);
130 ofpbuf_pull(b, sizeof *llc);
131 return llc->snap.snap_type;
135 parse_ipv6(struct ofpbuf *packet, struct flow *flow)
137 const struct ip6_hdr *nh;
141 nh = ofpbuf_try_pull(packet, sizeof *nh);
146 nexthdr = nh->ip6_nxt;
148 flow->ipv6_src = nh->ip6_src;
149 flow->ipv6_dst = nh->ip6_dst;
151 tc_flow = get_unaligned_be32(&nh->ip6_flow);
152 flow->nw_tos = (ntohl(tc_flow) >> 4) & IP_DSCP_MASK;
153 flow->nw_proto = IPPROTO_NONE;
156 if ((nexthdr != IPPROTO_HOPOPTS)
157 && (nexthdr != IPPROTO_ROUTING)
158 && (nexthdr != IPPROTO_DSTOPTS)
159 && (nexthdr != IPPROTO_AH)
160 && (nexthdr != IPPROTO_FRAGMENT)) {
161 /* It's either a terminal header (e.g., TCP, UDP) or one we
162 * don't understand. In either case, we're done with the
163 * packet, so use it to fill in 'nw_proto'. */
167 /* We only verify that at least 8 bytes of the next header are
168 * available, but many of these headers are longer. Ensure that
169 * accesses within the extension header are within those first 8
170 * bytes. All extension headers are required to be at least 8
172 if (packet->size < 8) {
176 if ((nexthdr == IPPROTO_HOPOPTS)
177 || (nexthdr == IPPROTO_ROUTING)
178 || (nexthdr == IPPROTO_DSTOPTS)) {
179 /* These headers, while different, have the fields we care about
180 * in the same location and with the same interpretation. */
181 const struct ip6_ext *ext_hdr = (struct ip6_ext *)packet->data;
182 nexthdr = ext_hdr->ip6e_nxt;
183 if (!ofpbuf_try_pull(packet, (ext_hdr->ip6e_len + 1) * 8)) {
186 } else if (nexthdr == IPPROTO_AH) {
187 /* A standard AH definition isn't available, but the fields
188 * we care about are in the same location as the generic
189 * option header--only the header length is calculated
191 const struct ip6_ext *ext_hdr = (struct ip6_ext *)packet->data;
192 nexthdr = ext_hdr->ip6e_nxt;
193 if (!ofpbuf_try_pull(packet, (ext_hdr->ip6e_len + 2) * 4)) {
196 } else if (nexthdr == IPPROTO_FRAGMENT) {
197 const struct ip6_frag *frag_hdr = (struct ip6_frag *)packet->data;
199 nexthdr = frag_hdr->ip6f_nxt;
200 if (!ofpbuf_try_pull(packet, sizeof *frag_hdr)) {
204 /* We only process the first fragment. */
205 if ((frag_hdr->ip6f_offlg & IP6F_OFF_MASK) != htons(0)) {
206 nexthdr = IPPROTO_FRAGMENT;
212 flow->nw_proto = nexthdr;
217 parse_tcp(struct ofpbuf *packet, struct ofpbuf *b, struct flow *flow)
219 const struct tcp_header *tcp = pull_tcp(b);
221 flow->tp_src = tcp->tcp_src;
222 flow->tp_dst = tcp->tcp_dst;
223 packet->l7 = b->data;
228 parse_udp(struct ofpbuf *packet, struct ofpbuf *b, struct flow *flow)
230 const struct udp_header *udp = pull_udp(b);
232 flow->tp_src = udp->udp_src;
233 flow->tp_dst = udp->udp_dst;
234 packet->l7 = b->data;
239 parse_icmpv6(struct ofpbuf *b, struct flow *flow)
241 const struct icmp6_hdr *icmp = pull_icmpv6(b);
247 /* The ICMPv6 type and code fields use the 16-bit transport port
248 * fields, so we need to store them in 16-bit network byte order. */
249 flow->icmp_type = htons(icmp->icmp6_type);
250 flow->icmp_code = htons(icmp->icmp6_code);
252 if (icmp->icmp6_code == 0 &&
253 (icmp->icmp6_type == ND_NEIGHBOR_SOLICIT ||
254 icmp->icmp6_type == ND_NEIGHBOR_ADVERT)) {
255 const struct in6_addr *nd_target;
257 nd_target = ofpbuf_try_pull(b, sizeof *nd_target);
261 flow->nd_target = *nd_target;
263 while (b->size >= 8) {
264 /* The minimum size of an option is 8 bytes, which also is
265 * the size of Ethernet link-layer options. */
266 const struct nd_opt_hdr *nd_opt = b->data;
267 int opt_len = nd_opt->nd_opt_len * 8;
269 if (!opt_len || opt_len > b->size) {
273 /* Store the link layer address if the appropriate option is
274 * provided. It is considered an error if the same link
275 * layer option is specified twice. */
276 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LINKADDR
278 if (eth_addr_is_zero(flow->arp_sha)) {
279 memcpy(flow->arp_sha, nd_opt + 1, ETH_ADDR_LEN);
283 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LINKADDR
285 if (eth_addr_is_zero(flow->arp_tha)) {
286 memcpy(flow->arp_tha, nd_opt + 1, ETH_ADDR_LEN);
292 if (!ofpbuf_try_pull(b, opt_len)) {
301 memset(&flow->nd_target, 0, sizeof(flow->nd_target));
302 memset(flow->arp_sha, 0, sizeof(flow->arp_sha));
303 memset(flow->arp_tha, 0, sizeof(flow->arp_tha));
309 /* Initializes 'flow' members from 'packet', 'tun_id', and 'ofp_in_port'.
310 * Initializes 'packet' header pointers as follows:
312 * - packet->l2 to the start of the Ethernet header.
314 * - packet->l3 to just past the Ethernet header, or just past the
315 * vlan_header if one is present, to the first byte of the payload of the
318 * - packet->l4 to just past the IPv4 header, if one is present and has a
319 * correct length, and otherwise NULL.
321 * - packet->l7 to just past the TCP or UDP or ICMP header, if one is
322 * present and has a correct length, and otherwise NULL.
325 flow_extract(struct ofpbuf *packet, ovs_be64 tun_id, uint16_t ofp_in_port,
328 struct ofpbuf b = *packet;
329 struct eth_header *eth;
332 COVERAGE_INC(flow_extract);
334 memset(flow, 0, sizeof *flow);
335 flow->tun_id = tun_id;
336 flow->in_port = ofp_in_port;
343 if (b.size < sizeof *eth) {
349 memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
350 memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
352 /* dl_type, vlan_tci. */
353 ofpbuf_pull(&b, ETH_ADDR_LEN * 2);
354 if (eth->eth_type == htons(ETH_TYPE_VLAN)) {
355 parse_vlan(&b, flow);
357 flow->dl_type = parse_ethertype(&b);
361 if (flow->dl_type == htons(ETH_TYPE_IP)) {
362 const struct ip_header *nh = pull_ip(&b);
364 flow->nw_src = get_unaligned_be32(&nh->ip_src);
365 flow->nw_dst = get_unaligned_be32(&nh->ip_dst);
366 flow->nw_tos = nh->ip_tos & IP_DSCP_MASK;
367 flow->nw_proto = nh->ip_proto;
369 if (!IP_IS_FRAGMENT(nh->ip_frag_off)) {
370 if (flow->nw_proto == IPPROTO_TCP) {
371 parse_tcp(packet, &b, flow);
372 } else if (flow->nw_proto == IPPROTO_UDP) {
373 parse_udp(packet, &b, flow);
374 } else if (flow->nw_proto == IPPROTO_ICMP) {
375 const struct icmp_header *icmp = pull_icmp(&b);
377 flow->icmp_type = htons(icmp->icmp_type);
378 flow->icmp_code = htons(icmp->icmp_code);
386 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
388 retval = parse_ipv6(&b, flow);
394 if (flow->nw_proto == IPPROTO_TCP) {
395 parse_tcp(packet, &b, flow);
396 } else if (flow->nw_proto == IPPROTO_UDP) {
397 parse_udp(packet, &b, flow);
398 } else if (flow->nw_proto == IPPROTO_ICMPV6) {
399 if (parse_icmpv6(&b, flow)) {
403 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
404 const struct arp_eth_header *arp = pull_arp(&b);
405 if (arp && arp->ar_hrd == htons(1)
406 && arp->ar_pro == htons(ETH_TYPE_IP)
407 && arp->ar_hln == ETH_ADDR_LEN
408 && arp->ar_pln == 4) {
409 /* We only match on the lower 8 bits of the opcode. */
410 if (ntohs(arp->ar_op) <= 0xff) {
411 flow->nw_proto = ntohs(arp->ar_op);
414 if ((flow->nw_proto == ARP_OP_REQUEST)
415 || (flow->nw_proto == ARP_OP_REPLY)) {
416 flow->nw_src = arp->ar_spa;
417 flow->nw_dst = arp->ar_tpa;
418 memcpy(flow->arp_sha, arp->ar_sha, ETH_ADDR_LEN);
419 memcpy(flow->arp_tha, arp->ar_tha, ETH_ADDR_LEN);
427 /* Extracts the flow stats for a packet. The 'flow' and 'packet'
428 * arguments must have been initialized through a call to flow_extract().
431 flow_extract_stats(const struct flow *flow, struct ofpbuf *packet,
432 struct dpif_flow_stats *stats)
434 memset(stats, 0, sizeof(*stats));
436 if ((flow->dl_type == htons(ETH_TYPE_IP)) && packet->l4) {
437 if ((flow->nw_proto == IPPROTO_TCP) && packet->l7) {
438 struct tcp_header *tcp = packet->l4;
439 stats->tcp_flags = TCP_FLAGS(tcp->tcp_ctl);
443 stats->n_bytes = packet->size;
444 stats->n_packets = 1;
447 /* For every bit of a field that is wildcarded in 'wildcards', sets the
448 * corresponding bit in 'flow' to zero. */
450 flow_zero_wildcards(struct flow *flow, const struct flow_wildcards *wildcards)
452 const flow_wildcards_t wc = wildcards->wildcards;
455 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 1);
457 for (i = 0; i < FLOW_N_REGS; i++) {
458 flow->regs[i] &= wildcards->reg_masks[i];
460 flow->tun_id &= wildcards->tun_id_mask;
461 flow->nw_src &= wildcards->nw_src_mask;
462 flow->nw_dst &= wildcards->nw_dst_mask;
463 if (wc & FWW_IN_PORT) {
466 flow->vlan_tci &= wildcards->vlan_tci_mask;
467 if (wc & FWW_DL_TYPE) {
470 if (wc & FWW_TP_SRC) {
473 if (wc & FWW_TP_DST) {
476 if (wc & FWW_DL_SRC) {
477 memset(flow->dl_src, 0, sizeof flow->dl_src);
479 if (wc & FWW_DL_DST) {
480 flow->dl_dst[0] &= 0x01;
481 memset(&flow->dl_dst[1], 0, 5);
483 if (wc & FWW_ETH_MCAST) {
484 flow->dl_dst[0] &= 0xfe;
486 if (wc & FWW_NW_PROTO) {
489 if (wc & FWW_NW_TOS) {
492 if (wc & FWW_ARP_SHA) {
493 memset(flow->arp_sha, 0, sizeof flow->arp_sha);
495 if (wc & FWW_ARP_THA) {
496 memset(flow->arp_tha, 0, sizeof flow->arp_tha);
498 flow->ipv6_src = ipv6_addr_bitand(&flow->ipv6_src,
499 &wildcards->ipv6_src_mask);
500 flow->ipv6_dst = ipv6_addr_bitand(&flow->ipv6_dst,
501 &wildcards->ipv6_dst_mask);
502 if (wc & FWW_ND_TARGET) {
503 memset(&flow->nd_target, 0, sizeof flow->nd_target);
508 flow_to_string(const struct flow *flow)
510 struct ds ds = DS_EMPTY_INITIALIZER;
511 flow_format(&ds, flow);
516 flow_format(struct ds *ds, const struct flow *flow)
518 ds_put_format(ds, "tunnel%#"PRIx64":in_port%04"PRIx16":tci(",
519 ntohll(flow->tun_id), flow->in_port);
520 if (flow->vlan_tci) {
521 ds_put_format(ds, "vlan%"PRIu16",pcp%d",
522 vlan_tci_to_vid(flow->vlan_tci),
523 vlan_tci_to_pcp(flow->vlan_tci));
525 ds_put_char(ds, '0');
527 ds_put_format(ds, ") mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT
529 ETH_ADDR_ARGS(flow->dl_src),
530 ETH_ADDR_ARGS(flow->dl_dst),
531 ntohs(flow->dl_type));
533 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
534 ds_put_format(ds, " proto%"PRIu8" tos%"PRIu8" ipv6",
535 flow->nw_proto, flow->nw_tos);
536 print_ipv6_addr(ds, &flow->ipv6_src);
537 ds_put_cstr(ds, "->");
538 print_ipv6_addr(ds, &flow->ipv6_dst);
541 ds_put_format(ds, " proto%"PRIu8
543 " ip"IP_FMT"->"IP_FMT,
546 IP_ARGS(&flow->nw_src),
547 IP_ARGS(&flow->nw_dst));
549 if (flow->tp_src || flow->tp_dst) {
550 ds_put_format(ds, " port%"PRIu16"->%"PRIu16,
551 ntohs(flow->tp_src), ntohs(flow->tp_dst));
553 if (!eth_addr_is_zero(flow->arp_sha) || !eth_addr_is_zero(flow->arp_tha)) {
554 ds_put_format(ds, " arp_ha"ETH_ADDR_FMT"->"ETH_ADDR_FMT,
555 ETH_ADDR_ARGS(flow->arp_sha),
556 ETH_ADDR_ARGS(flow->arp_tha));
561 flow_print(FILE *stream, const struct flow *flow)
563 char *s = flow_to_string(flow);
568 /* flow_wildcards functions. */
570 /* Initializes 'wc' as a set of wildcards that matches every packet. */
572 flow_wildcards_init_catchall(struct flow_wildcards *wc)
574 wc->wildcards = FWW_ALL;
575 wc->tun_id_mask = htonll(0);
576 wc->nw_src_mask = htonl(0);
577 wc->nw_dst_mask = htonl(0);
578 wc->ipv6_src_mask = in6addr_any;
579 wc->ipv6_dst_mask = in6addr_any;
580 memset(wc->reg_masks, 0, sizeof wc->reg_masks);
581 wc->vlan_tci_mask = htons(0);
585 /* Initializes 'wc' as an exact-match set of wildcards; that is, 'wc' does not
586 * wildcard any bits or fields. */
588 flow_wildcards_init_exact(struct flow_wildcards *wc)
591 wc->tun_id_mask = htonll(UINT64_MAX);
592 wc->nw_src_mask = htonl(UINT32_MAX);
593 wc->nw_dst_mask = htonl(UINT32_MAX);
594 wc->ipv6_src_mask = in6addr_exact;
595 wc->ipv6_dst_mask = in6addr_exact;
596 memset(wc->reg_masks, 0xff, sizeof wc->reg_masks);
597 wc->vlan_tci_mask = htons(UINT16_MAX);
601 /* Returns true if 'wc' is exact-match, false if 'wc' wildcards any bits or
604 flow_wildcards_is_exact(const struct flow_wildcards *wc)
608 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 1);
611 || wc->tun_id_mask != htonll(UINT64_MAX)
612 || wc->nw_src_mask != htonl(UINT32_MAX)
613 || wc->nw_dst_mask != htonl(UINT32_MAX)
614 || wc->vlan_tci_mask != htons(UINT16_MAX)
615 || !ipv6_mask_is_exact(&wc->ipv6_src_mask)
616 || !ipv6_mask_is_exact(&wc->ipv6_dst_mask)) {
620 for (i = 0; i < FLOW_N_REGS; i++) {
621 if (wc->reg_masks[i] != UINT32_MAX) {
629 /* Returns true if 'wc' matches every packet, false if 'wc' fixes any bits or
632 flow_wildcards_is_catchall(const struct flow_wildcards *wc)
636 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 1);
638 if (wc->wildcards != FWW_ALL
639 || wc->tun_id_mask != htonll(0)
640 || wc->nw_src_mask != htonl(0)
641 || wc->nw_dst_mask != htonl(0)
642 || wc->vlan_tci_mask != htons(0)
643 || !ipv6_mask_is_any(&wc->ipv6_src_mask)
644 || !ipv6_mask_is_any(&wc->ipv6_dst_mask)) {
648 for (i = 0; i < FLOW_N_REGS; i++) {
649 if (wc->reg_masks[i] != 0) {
657 /* Initializes 'dst' as the combination of wildcards in 'src1' and 'src2'.
658 * That is, a bit or a field is wildcarded in 'dst' if it is wildcarded in
659 * 'src1' or 'src2' or both. */
661 flow_wildcards_combine(struct flow_wildcards *dst,
662 const struct flow_wildcards *src1,
663 const struct flow_wildcards *src2)
667 dst->wildcards = src1->wildcards | src2->wildcards;
668 dst->tun_id_mask = src1->tun_id_mask & src2->tun_id_mask;
669 dst->nw_src_mask = src1->nw_src_mask & src2->nw_src_mask;
670 dst->nw_dst_mask = src1->nw_dst_mask & src2->nw_dst_mask;
671 dst->ipv6_src_mask = ipv6_addr_bitand(&src1->ipv6_src_mask,
672 &src2->ipv6_src_mask);
673 dst->ipv6_dst_mask = ipv6_addr_bitand(&src1->ipv6_dst_mask,
674 &src2->ipv6_dst_mask);
675 for (i = 0; i < FLOW_N_REGS; i++) {
676 dst->reg_masks[i] = src1->reg_masks[i] & src2->reg_masks[i];
678 dst->vlan_tci_mask = src1->vlan_tci_mask & src2->vlan_tci_mask;
681 /* Returns a hash of the wildcards in 'wc'. */
683 flow_wildcards_hash(const struct flow_wildcards *wc, uint32_t basis)
685 /* If you change struct flow_wildcards and thereby trigger this
686 * assertion, please check that the new struct flow_wildcards has no holes
687 * in it before you update the assertion. */
688 BUILD_ASSERT_DECL(sizeof *wc == 56 + FLOW_N_REGS * 4);
689 return hash_bytes(wc, sizeof *wc, basis);
692 /* Returns true if 'a' and 'b' represent the same wildcards, false if they are
695 flow_wildcards_equal(const struct flow_wildcards *a,
696 const struct flow_wildcards *b)
700 if (a->wildcards != b->wildcards
701 || a->tun_id_mask != b->tun_id_mask
702 || a->nw_src_mask != b->nw_src_mask
703 || a->nw_dst_mask != b->nw_dst_mask
704 || a->vlan_tci_mask != b->vlan_tci_mask
705 || !ipv6_addr_equals(&a->ipv6_src_mask, &b->ipv6_src_mask)
706 || !ipv6_addr_equals(&a->ipv6_dst_mask, &b->ipv6_dst_mask)) {
710 for (i = 0; i < FLOW_N_REGS; i++) {
711 if (a->reg_masks[i] != b->reg_masks[i]) {
719 /* Returns true if at least one bit or field is wildcarded in 'a' but not in
720 * 'b', false otherwise. */
722 flow_wildcards_has_extra(const struct flow_wildcards *a,
723 const struct flow_wildcards *b)
726 struct in6_addr ipv6_masked;
728 for (i = 0; i < FLOW_N_REGS; i++) {
729 if ((a->reg_masks[i] & b->reg_masks[i]) != b->reg_masks[i]) {
734 ipv6_masked = ipv6_addr_bitand(&a->ipv6_src_mask, &b->ipv6_src_mask);
735 if (!ipv6_addr_equals(&ipv6_masked, &b->ipv6_src_mask)) {
739 ipv6_masked = ipv6_addr_bitand(&a->ipv6_dst_mask, &b->ipv6_dst_mask);
740 if (!ipv6_addr_equals(&ipv6_masked, &b->ipv6_dst_mask)) {
744 return (a->wildcards & ~b->wildcards
745 || (a->tun_id_mask & b->tun_id_mask) != b->tun_id_mask
746 || (a->nw_src_mask & b->nw_src_mask) != b->nw_src_mask
747 || (a->nw_dst_mask & b->nw_dst_mask) != b->nw_dst_mask
748 || (a->vlan_tci_mask & b->vlan_tci_mask) != b->vlan_tci_mask);
752 set_nw_mask(ovs_be32 *maskp, ovs_be32 mask)
754 if (ip_is_cidr(mask)) {
762 /* Sets the IP (or ARP) source wildcard mask to CIDR 'mask' (consisting of N
763 * high-order 1-bit and 32-N low-order 0-bits). Returns true if successful,
764 * false if 'mask' is not a CIDR mask. */
766 flow_wildcards_set_nw_src_mask(struct flow_wildcards *wc, ovs_be32 mask)
768 return set_nw_mask(&wc->nw_src_mask, mask);
771 /* Sets the IP (or ARP) destination wildcard mask to CIDR 'mask' (consisting of
772 * N high-order 1-bit and 32-N low-order 0-bits). Returns true if successful,
773 * false if 'mask' is not a CIDR mask. */
775 flow_wildcards_set_nw_dst_mask(struct flow_wildcards *wc, ovs_be32 mask)
777 return set_nw_mask(&wc->nw_dst_mask, mask);
781 set_ipv6_mask(struct in6_addr *maskp, const struct in6_addr *mask)
783 if (ipv6_is_cidr(mask)) {
791 /* Sets the IPv6 source wildcard mask to CIDR 'mask' (consisting of N
792 * high-order 1-bit and 128-N low-order 0-bits). Returns true if successful,
793 * false if 'mask' is not a CIDR mask. */
795 flow_wildcards_set_ipv6_src_mask(struct flow_wildcards *wc,
796 const struct in6_addr *mask)
798 return set_ipv6_mask(&wc->ipv6_src_mask, mask);
801 /* Sets the IPv6 destination wildcard mask to CIDR 'mask' (consisting of
802 * N high-order 1-bit and 128-N low-order 0-bits). Returns true if
803 * successful, false if 'mask' is not a CIDR mask. */
805 flow_wildcards_set_ipv6_dst_mask(struct flow_wildcards *wc,
806 const struct in6_addr *mask)
808 return set_ipv6_mask(&wc->ipv6_dst_mask, mask);
811 /* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
812 * (A 0-bit indicates a wildcard bit.) */
814 flow_wildcards_set_reg_mask(struct flow_wildcards *wc, int idx, uint32_t mask)
816 wc->reg_masks[idx] = mask;
819 /* Returns the wildcard bitmask for the Ethernet destination address
820 * that 'wc' specifies. The bitmask has a 0 in each bit that is wildcarded
821 * and a 1 in each bit that must match. */
823 flow_wildcards_to_dl_dst_mask(flow_wildcards_t wc)
825 static const uint8_t no_wild[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
826 static const uint8_t addr_wild[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
827 static const uint8_t mcast_wild[] = {0xfe, 0xff, 0xff, 0xff, 0xff, 0xff};
828 static const uint8_t all_wild[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
830 switch (wc & (FWW_DL_DST | FWW_ETH_MCAST)) {
831 case 0: return no_wild;
832 case FWW_DL_DST: return addr_wild;
833 case FWW_ETH_MCAST: return mcast_wild;
834 case FWW_DL_DST | FWW_ETH_MCAST: return all_wild;
839 /* Returns true if 'mask' is a valid wildcard bitmask for the Ethernet
840 * destination address. Valid bitmasks are either all-bits-0 or all-bits-1,
841 * except that the multicast bit may differ from the rest of the bits. So,
842 * there are four possible valid bitmasks:
844 * - 00:00:00:00:00:00
845 * - 01:00:00:00:00:00
846 * - fe:ff:ff:ff:ff:ff
847 * - ff:ff:ff:ff:ff:ff
849 * All other bitmasks are invalid. */
851 flow_wildcards_is_dl_dst_mask_valid(const uint8_t mask[ETH_ADDR_LEN])
856 return (mask[1] | mask[2] | mask[3] | mask[4] | mask[5]) == 0x00;
860 return (mask[1] & mask[2] & mask[3] & mask[4] & mask[5]) == 0xff;
867 /* Returns 'wc' with the FWW_DL_DST and FWW_ETH_MCAST bits modified
868 * appropriately to match 'mask'.
870 * This function will assert-fail if 'mask' is invalid. Only 'mask' values
871 * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
873 flow_wildcards_set_dl_dst_mask(flow_wildcards_t wc,
874 const uint8_t mask[ETH_ADDR_LEN])
876 assert(flow_wildcards_is_dl_dst_mask_valid(mask));
880 return wc | FWW_DL_DST | FWW_ETH_MCAST;
883 return (wc | FWW_DL_DST) & ~FWW_ETH_MCAST;
886 return (wc & ~FWW_DL_DST) | FWW_ETH_MCAST;
889 return wc & ~(FWW_DL_DST | FWW_ETH_MCAST);
896 /* Hashes 'flow' based on its L2 through L4 protocol information. */
898 flow_hash_symmetric_l4(const struct flow *flow, uint32_t basis)
903 struct in6_addr ipv6_addr;
908 uint8_t eth_addr[ETH_ADDR_LEN];
914 memset(&fields, 0, sizeof fields);
915 for (i = 0; i < ETH_ADDR_LEN; i++) {
916 fields.eth_addr[i] = flow->dl_src[i] ^ flow->dl_dst[i];
918 fields.vlan_tci = flow->vlan_tci & htons(VLAN_VID_MASK);
919 fields.eth_type = flow->dl_type;
921 /* UDP source and destination port are not taken into account because they
922 * will not necessarily be symmetric in a bidirectional flow. */
923 if (fields.eth_type == htons(ETH_TYPE_IP)) {
924 fields.ipv4_addr = flow->nw_src ^ flow->nw_dst;
925 fields.ip_proto = flow->nw_proto;
926 if (fields.ip_proto == IPPROTO_TCP) {
927 fields.tp_addr = flow->tp_src ^ flow->tp_dst;
929 } else if (fields.eth_type == htons(ETH_TYPE_IPV6)) {
930 const uint8_t *a = &flow->ipv6_src.s6_addr[0];
931 const uint8_t *b = &flow->ipv6_dst.s6_addr[0];
932 uint8_t *ipv6_addr = &fields.ipv6_addr.s6_addr[0];
934 for (i=0; i<16; i++) {
935 ipv6_addr[i] = a[i] ^ b[i];
937 fields.ip_proto = flow->nw_proto;
938 if (fields.ip_proto == IPPROTO_TCP) {
939 fields.tp_addr = flow->tp_src ^ flow->tp_dst;
942 return hash_bytes(&fields, sizeof fields, basis);
945 /* Hashes the portions of 'flow' designated by 'fields'. */
947 flow_hash_fields(const struct flow *flow, enum nx_hash_fields fields,
952 case NX_HASH_FIELDS_ETH_SRC:
953 return hash_bytes(flow->dl_src, sizeof flow->dl_src, basis);
955 case NX_HASH_FIELDS_SYMMETRIC_L4:
956 return flow_hash_symmetric_l4(flow, basis);
962 /* Returns a string representation of 'fields'. */
964 flow_hash_fields_to_str(enum nx_hash_fields fields)
967 case NX_HASH_FIELDS_ETH_SRC: return "eth_src";
968 case NX_HASH_FIELDS_SYMMETRIC_L4: return "symmetric_l4";
969 default: return "<unknown>";
973 /* Returns true if the value of 'fields' is supported. Otherwise false. */
975 flow_hash_fields_valid(enum nx_hash_fields fields)
977 return fields == NX_HASH_FIELDS_ETH_SRC
978 || fields == NX_HASH_FIELDS_SYMMETRIC_L4;
981 /* Puts into 'b' a packet that flow_extract() would parse as having the given
984 * (This is useful only for testing, obviously, and the packet isn't really
985 * valid. It hasn't got any checksums filled in, for one, and lots of fields
986 * are just zeroed.) */
988 flow_compose(struct ofpbuf *b, const struct flow *flow)
990 eth_compose(b, flow->dl_dst, flow->dl_src, ntohs(flow->dl_type), 0);
991 if (flow->dl_type == htons(FLOW_DL_TYPE_NONE)) {
992 struct eth_header *eth = b->l2;
993 eth->eth_type = htons(b->size);
997 if (flow->vlan_tci & htons(VLAN_CFI)) {
998 eth_push_vlan(b, flow->vlan_tci & ~htons(VLAN_CFI));
1001 if (flow->dl_type == htons(ETH_TYPE_IP)) {
1002 struct ip_header *ip;
1004 b->l3 = ip = ofpbuf_put_zeros(b, sizeof *ip);
1005 ip->ip_ihl_ver = IP_IHL_VER(5, 4);
1006 ip->ip_tos = flow->nw_tos;
1007 ip->ip_proto = flow->nw_proto;
1008 ip->ip_src = flow->nw_src;
1009 ip->ip_dst = flow->nw_dst;
1011 if (flow->nw_proto == IPPROTO_TCP) {
1012 struct tcp_header *tcp;
1014 b->l4 = tcp = ofpbuf_put_zeros(b, sizeof *tcp);
1015 tcp->tcp_src = flow->tp_src;
1016 tcp->tcp_dst = flow->tp_dst;
1017 } else if (flow->nw_proto == IPPROTO_UDP) {
1018 struct udp_header *udp;
1020 b->l4 = udp = ofpbuf_put_zeros(b, sizeof *udp);
1021 udp->udp_src = flow->tp_src;
1022 udp->udp_dst = flow->tp_dst;
1023 } else if (flow->nw_proto == IPPROTO_ICMP) {
1024 struct icmp_header *icmp;
1026 b->l4 = icmp = ofpbuf_put_zeros(b, sizeof *icmp);
1027 icmp->icmp_type = ntohs(flow->tp_src);
1028 icmp->icmp_code = ntohs(flow->tp_dst);
1030 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1032 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
1033 struct arp_eth_header *arp;
1035 b->l3 = arp = ofpbuf_put_zeros(b, sizeof *arp);
1036 arp->ar_hrd = htons(1);
1037 arp->ar_pro = htons(ETH_TYPE_IP);
1038 arp->ar_hln = ETH_ADDR_LEN;
1040 arp->ar_op = htons(flow->nw_proto);
1042 if (flow->nw_proto == ARP_OP_REQUEST ||
1043 flow->nw_proto == ARP_OP_REPLY) {
1044 arp->ar_spa = flow->nw_src;
1045 arp->ar_tpa = flow->nw_dst;
1046 memcpy(arp->ar_sha, flow->arp_sha, ETH_ADDR_LEN);
1047 memcpy(arp->ar_tha, flow->arp_tha, ETH_ADDR_LEN);