2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 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>
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) >> 20;
153 flow->ipv6_label = tc_flow & htonl(IPV6_LABEL_MASK);
154 flow->nw_ttl = nh->ip6_hlim;
155 flow->nw_proto = IPPROTO_NONE;
158 if ((nexthdr != IPPROTO_HOPOPTS)
159 && (nexthdr != IPPROTO_ROUTING)
160 && (nexthdr != IPPROTO_DSTOPTS)
161 && (nexthdr != IPPROTO_AH)
162 && (nexthdr != IPPROTO_FRAGMENT)) {
163 /* It's either a terminal header (e.g., TCP, UDP) or one we
164 * don't understand. In either case, we're done with the
165 * packet, so use it to fill in 'nw_proto'. */
169 /* We only verify that at least 8 bytes of the next header are
170 * available, but many of these headers are longer. Ensure that
171 * accesses within the extension header are within those first 8
172 * bytes. All extension headers are required to be at least 8
174 if (packet->size < 8) {
178 if ((nexthdr == IPPROTO_HOPOPTS)
179 || (nexthdr == IPPROTO_ROUTING)
180 || (nexthdr == IPPROTO_DSTOPTS)) {
181 /* These headers, while different, have the fields we care about
182 * in the same location and with the same interpretation. */
183 const struct ip6_ext *ext_hdr = packet->data;
184 nexthdr = ext_hdr->ip6e_nxt;
185 if (!ofpbuf_try_pull(packet, (ext_hdr->ip6e_len + 1) * 8)) {
188 } else if (nexthdr == IPPROTO_AH) {
189 /* A standard AH definition isn't available, but the fields
190 * we care about are in the same location as the generic
191 * option header--only the header length is calculated
193 const struct ip6_ext *ext_hdr = packet->data;
194 nexthdr = ext_hdr->ip6e_nxt;
195 if (!ofpbuf_try_pull(packet, (ext_hdr->ip6e_len + 2) * 4)) {
198 } else if (nexthdr == IPPROTO_FRAGMENT) {
199 const struct ip6_frag *frag_hdr = packet->data;
201 nexthdr = frag_hdr->ip6f_nxt;
202 if (!ofpbuf_try_pull(packet, sizeof *frag_hdr)) {
206 /* We only process the first fragment. */
207 if (frag_hdr->ip6f_offlg != htons(0)) {
208 if ((frag_hdr->ip6f_offlg & IP6F_OFF_MASK) == htons(0)) {
209 flow->nw_frag = FLOW_NW_FRAG_ANY;
211 flow->nw_frag |= FLOW_NW_FRAG_LATER;
212 nexthdr = IPPROTO_FRAGMENT;
219 flow->nw_proto = nexthdr;
224 parse_tcp(struct ofpbuf *packet, struct ofpbuf *b, struct flow *flow)
226 const struct tcp_header *tcp = pull_tcp(b);
228 flow->tp_src = tcp->tcp_src;
229 flow->tp_dst = tcp->tcp_dst;
230 packet->l7 = b->data;
235 parse_udp(struct ofpbuf *packet, struct ofpbuf *b, struct flow *flow)
237 const struct udp_header *udp = pull_udp(b);
239 flow->tp_src = udp->udp_src;
240 flow->tp_dst = udp->udp_dst;
241 packet->l7 = b->data;
246 parse_icmpv6(struct ofpbuf *b, struct flow *flow)
248 const struct icmp6_hdr *icmp = pull_icmpv6(b);
254 /* The ICMPv6 type and code fields use the 16-bit transport port
255 * fields, so we need to store them in 16-bit network byte order. */
256 flow->tp_src = htons(icmp->icmp6_type);
257 flow->tp_dst = htons(icmp->icmp6_code);
259 if (icmp->icmp6_code == 0 &&
260 (icmp->icmp6_type == ND_NEIGHBOR_SOLICIT ||
261 icmp->icmp6_type == ND_NEIGHBOR_ADVERT)) {
262 const struct in6_addr *nd_target;
264 nd_target = ofpbuf_try_pull(b, sizeof *nd_target);
268 flow->nd_target = *nd_target;
270 while (b->size >= 8) {
271 /* The minimum size of an option is 8 bytes, which also is
272 * the size of Ethernet link-layer options. */
273 const struct nd_opt_hdr *nd_opt = b->data;
274 int opt_len = nd_opt->nd_opt_len * 8;
276 if (!opt_len || opt_len > b->size) {
280 /* Store the link layer address if the appropriate option is
281 * provided. It is considered an error if the same link
282 * layer option is specified twice. */
283 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LINKADDR
285 if (eth_addr_is_zero(flow->arp_sha)) {
286 memcpy(flow->arp_sha, nd_opt + 1, ETH_ADDR_LEN);
290 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LINKADDR
292 if (eth_addr_is_zero(flow->arp_tha)) {
293 memcpy(flow->arp_tha, nd_opt + 1, ETH_ADDR_LEN);
299 if (!ofpbuf_try_pull(b, opt_len)) {
308 memset(&flow->nd_target, 0, sizeof(flow->nd_target));
309 memset(flow->arp_sha, 0, sizeof(flow->arp_sha));
310 memset(flow->arp_tha, 0, sizeof(flow->arp_tha));
316 /* Initializes 'flow' members from 'packet', 'skb_priority', 'tun_id', and
319 * Initializes 'packet' header pointers as follows:
321 * - packet->l2 to the start of the Ethernet header.
323 * - packet->l3 to just past the Ethernet header, or just past the
324 * vlan_header if one is present, to the first byte of the payload of the
327 * - packet->l4 to just past the IPv4 header, if one is present and has a
328 * correct length, and otherwise NULL.
330 * - packet->l7 to just past the TCP or UDP or ICMP header, if one is
331 * present and has a correct length, and otherwise NULL.
334 flow_extract(struct ofpbuf *packet, uint32_t skb_priority, ovs_be64 tun_id,
335 uint16_t ofp_in_port, struct flow *flow)
337 struct ofpbuf b = *packet;
338 struct eth_header *eth;
340 COVERAGE_INC(flow_extract);
342 memset(flow, 0, sizeof *flow);
343 flow->tun_id = tun_id;
344 flow->in_port = ofp_in_port;
345 flow->skb_priority = skb_priority;
352 if (b.size < sizeof *eth) {
358 memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
359 memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
361 /* dl_type, vlan_tci. */
362 ofpbuf_pull(&b, ETH_ADDR_LEN * 2);
363 if (eth->eth_type == htons(ETH_TYPE_VLAN)) {
364 parse_vlan(&b, flow);
366 flow->dl_type = parse_ethertype(&b);
370 if (flow->dl_type == htons(ETH_TYPE_IP)) {
371 const struct ip_header *nh = pull_ip(&b);
375 flow->nw_src = get_unaligned_be32(&nh->ip_src);
376 flow->nw_dst = get_unaligned_be32(&nh->ip_dst);
377 flow->nw_proto = nh->ip_proto;
379 flow->nw_tos = nh->ip_tos;
380 if (IP_IS_FRAGMENT(nh->ip_frag_off)) {
381 flow->nw_frag = FLOW_NW_FRAG_ANY;
382 if (nh->ip_frag_off & htons(IP_FRAG_OFF_MASK)) {
383 flow->nw_frag |= FLOW_NW_FRAG_LATER;
386 flow->nw_ttl = nh->ip_ttl;
388 if (!(nh->ip_frag_off & htons(IP_FRAG_OFF_MASK))) {
389 if (flow->nw_proto == IPPROTO_TCP) {
390 parse_tcp(packet, &b, flow);
391 } else if (flow->nw_proto == IPPROTO_UDP) {
392 parse_udp(packet, &b, flow);
393 } else if (flow->nw_proto == IPPROTO_ICMP) {
394 const struct icmp_header *icmp = pull_icmp(&b);
396 flow->tp_src = htons(icmp->icmp_type);
397 flow->tp_dst = htons(icmp->icmp_code);
403 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
404 if (parse_ipv6(&b, flow)) {
409 if (flow->nw_proto == IPPROTO_TCP) {
410 parse_tcp(packet, &b, flow);
411 } else if (flow->nw_proto == IPPROTO_UDP) {
412 parse_udp(packet, &b, flow);
413 } else if (flow->nw_proto == IPPROTO_ICMPV6) {
414 if (parse_icmpv6(&b, flow)) {
418 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
419 const struct arp_eth_header *arp = pull_arp(&b);
420 if (arp && arp->ar_hrd == htons(1)
421 && arp->ar_pro == htons(ETH_TYPE_IP)
422 && arp->ar_hln == ETH_ADDR_LEN
423 && arp->ar_pln == 4) {
424 /* We only match on the lower 8 bits of the opcode. */
425 if (ntohs(arp->ar_op) <= 0xff) {
426 flow->nw_proto = ntohs(arp->ar_op);
429 if ((flow->nw_proto == ARP_OP_REQUEST)
430 || (flow->nw_proto == ARP_OP_REPLY)) {
431 flow->nw_src = arp->ar_spa;
432 flow->nw_dst = arp->ar_tpa;
433 memcpy(flow->arp_sha, arp->ar_sha, ETH_ADDR_LEN);
434 memcpy(flow->arp_tha, arp->ar_tha, ETH_ADDR_LEN);
440 /* For every bit of a field that is wildcarded in 'wildcards', sets the
441 * corresponding bit in 'flow' to zero. */
443 flow_zero_wildcards(struct flow *flow, const struct flow_wildcards *wildcards)
447 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 17);
449 for (i = 0; i < FLOW_N_REGS; i++) {
450 flow->regs[i] &= wildcards->masks.regs[i];
452 flow->tun_id &= wildcards->masks.tun_id;
453 flow->metadata &= wildcards->masks.metadata;
454 flow->nw_src &= wildcards->masks.nw_src;
455 flow->nw_dst &= wildcards->masks.nw_dst;
456 flow->in_port &= wildcards->masks.in_port;
457 flow->vlan_tci &= wildcards->masks.vlan_tci;
458 flow->dl_type &= wildcards->masks.dl_type;
459 flow->tp_src &= wildcards->masks.tp_src;
460 flow->tp_dst &= wildcards->masks.tp_dst;
461 eth_addr_bitand(flow->dl_src, wildcards->masks.dl_src, flow->dl_src);
462 eth_addr_bitand(flow->dl_dst, wildcards->masks.dl_dst, flow->dl_dst);
463 flow->ipv6_label &= wildcards->masks.ipv6_label;
464 flow->nw_proto &= wildcards->masks.nw_proto;
465 flow->nw_tos &= wildcards->masks.nw_tos;
466 flow->nw_ttl &= wildcards->masks.nw_ttl;
467 flow->nw_frag &= wildcards->masks.nw_frag;
468 eth_addr_bitand(flow->arp_sha, wildcards->masks.arp_sha, flow->arp_sha);
469 eth_addr_bitand(flow->arp_tha, wildcards->masks.arp_tha, flow->arp_tha);
470 flow->ipv6_src = ipv6_addr_bitand(&flow->ipv6_src,
471 &wildcards->masks.ipv6_src);
472 flow->ipv6_dst = ipv6_addr_bitand(&flow->ipv6_dst,
473 &wildcards->masks.ipv6_dst);
474 flow->nd_target = ipv6_addr_bitand(&flow->nd_target,
475 &wildcards->masks.nd_target);
476 flow->skb_priority = 0;
479 /* Initializes 'fmd' with the metadata found in 'flow'. */
481 flow_get_metadata(const struct flow *flow, struct flow_metadata *fmd)
483 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 17);
485 fmd->tun_id = flow->tun_id;
486 fmd->metadata = flow->metadata;
487 memcpy(fmd->regs, flow->regs, sizeof fmd->regs);
488 fmd->in_port = flow->in_port;
492 flow_to_string(const struct flow *flow)
494 struct ds ds = DS_EMPTY_INITIALIZER;
495 flow_format(&ds, flow);
500 flow_format(struct ds *ds, const struct flow *flow)
502 ds_put_format(ds, "priority:%"PRIu32
505 ",in_port:%04"PRIx16,
507 ntohll(flow->tun_id),
508 ntohll(flow->metadata),
511 ds_put_format(ds, ",tci(");
512 if (flow->vlan_tci) {
513 ds_put_format(ds, "vlan:%"PRIu16",pcp:%d",
514 vlan_tci_to_vid(flow->vlan_tci),
515 vlan_tci_to_pcp(flow->vlan_tci));
517 ds_put_char(ds, '0');
519 ds_put_format(ds, ") mac("ETH_ADDR_FMT"->"ETH_ADDR_FMT
521 ETH_ADDR_ARGS(flow->dl_src),
522 ETH_ADDR_ARGS(flow->dl_dst),
523 ntohs(flow->dl_type));
525 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
526 ds_put_format(ds, " label:%#"PRIx32" proto:%"PRIu8" tos:%#"PRIx8
527 " ttl:%"PRIu8" ipv6(",
528 ntohl(flow->ipv6_label), flow->nw_proto,
529 flow->nw_tos, flow->nw_ttl);
530 print_ipv6_addr(ds, &flow->ipv6_src);
531 ds_put_cstr(ds, "->");
532 print_ipv6_addr(ds, &flow->ipv6_dst);
533 ds_put_char(ds, ')');
535 ds_put_format(ds, " proto:%"PRIu8" tos:%#"PRIx8" ttl:%"PRIu8
536 " ip("IP_FMT"->"IP_FMT")",
537 flow->nw_proto, flow->nw_tos, flow->nw_ttl,
538 IP_ARGS(&flow->nw_src), IP_ARGS(&flow->nw_dst));
541 ds_put_format(ds, " frag(%s)",
542 flow->nw_frag == FLOW_NW_FRAG_ANY ? "first"
543 : flow->nw_frag == (FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER)
544 ? "later" : "<error>");
546 if (flow->tp_src || flow->tp_dst) {
547 ds_put_format(ds, " port(%"PRIu16"->%"PRIu16")",
548 ntohs(flow->tp_src), ntohs(flow->tp_dst));
550 if (!eth_addr_is_zero(flow->arp_sha) || !eth_addr_is_zero(flow->arp_tha)) {
551 ds_put_format(ds, " arp_ha("ETH_ADDR_FMT"->"ETH_ADDR_FMT")",
552 ETH_ADDR_ARGS(flow->arp_sha),
553 ETH_ADDR_ARGS(flow->arp_tha));
558 flow_print(FILE *stream, const struct flow *flow)
560 char *s = flow_to_string(flow);
565 /* flow_wildcards functions. */
567 /* Initializes 'wc' as a set of wildcards that matches every packet. */
569 flow_wildcards_init_catchall(struct flow_wildcards *wc)
571 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 17);
573 wc->masks.tun_id = htonll(0);
574 wc->masks.nw_src = htonl(0);
575 wc->masks.nw_dst = htonl(0);
576 wc->masks.ipv6_src = in6addr_any;
577 wc->masks.ipv6_dst = in6addr_any;
578 wc->masks.ipv6_label = htonl(0);
579 wc->masks.nd_target = in6addr_any;
580 memset(wc->masks.regs, 0, sizeof wc->masks.regs);
581 wc->masks.metadata = htonll(0);
582 wc->masks.in_port = 0;
583 wc->masks.vlan_tci = htons(0);
584 wc->masks.nw_frag = 0;
585 wc->masks.dl_type = htons(0);
586 wc->masks.tp_src = htons(0);
587 wc->masks.tp_dst = htons(0);
588 memset(wc->masks.dl_src, 0, ETH_ADDR_LEN);
589 memset(wc->masks.dl_dst, 0, ETH_ADDR_LEN);
590 memset(wc->masks.arp_sha, 0, ETH_ADDR_LEN);
591 memset(wc->masks.arp_tha, 0, ETH_ADDR_LEN);
592 wc->masks.nw_proto = 0;
593 wc->masks.nw_tos = 0;
594 wc->masks.nw_ttl = 0;
595 memset(wc->masks.zeros, 0, sizeof wc->masks.zeros);
598 /* Initializes 'wc' as an exact-match set of wildcards; that is, 'wc' does not
599 * wildcard any bits or fields. */
601 flow_wildcards_init_exact(struct flow_wildcards *wc)
603 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 17);
605 wc->masks.tun_id = htonll(UINT64_MAX);
606 wc->masks.nw_src = htonl(UINT32_MAX);
607 wc->masks.nw_dst = htonl(UINT32_MAX);
608 wc->masks.ipv6_src = in6addr_exact;
609 wc->masks.ipv6_dst = in6addr_exact;
610 wc->masks.ipv6_label = htonl(UINT32_MAX);
611 wc->masks.nd_target = in6addr_exact;
612 memset(wc->masks.regs, 0xff, sizeof wc->masks.regs);
613 wc->masks.metadata = htonll(UINT64_MAX);
614 wc->masks.in_port = UINT16_MAX;
615 wc->masks.vlan_tci = htons(UINT16_MAX);
616 wc->masks.nw_frag = UINT8_MAX;
617 wc->masks.dl_type = htons(UINT16_MAX);
618 wc->masks.tp_src = htons(UINT16_MAX);
619 wc->masks.tp_dst = htons(UINT16_MAX);
620 memset(wc->masks.dl_src, 0xff, ETH_ADDR_LEN);
621 memset(wc->masks.dl_dst, 0xff, ETH_ADDR_LEN);
622 memset(wc->masks.arp_sha, 0xff, ETH_ADDR_LEN);
623 memset(wc->masks.arp_tha, 0xff, ETH_ADDR_LEN);
624 wc->masks.nw_proto = UINT8_MAX;
625 wc->masks.nw_tos = UINT8_MAX;
626 wc->masks.nw_ttl = UINT8_MAX;
627 memset(wc->masks.zeros, 0, sizeof wc->masks.zeros);
630 /* Returns true if 'wc' matches every packet, false if 'wc' fixes any bits or
633 flow_wildcards_is_catchall(const struct flow_wildcards *wc)
637 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 17);
639 if (wc->masks.tun_id != htonll(0)
640 || wc->masks.nw_src != htonl(0)
641 || wc->masks.nw_dst != htonl(0)
642 || wc->masks.tp_src != htons(0)
643 || wc->masks.tp_dst != htons(0)
644 || wc->masks.in_port != 0
645 || wc->masks.vlan_tci != htons(0)
646 || wc->masks.metadata != htonll(0)
647 || wc->masks.dl_type != htons(0)
648 || !eth_addr_is_zero(wc->masks.dl_src)
649 || !eth_addr_is_zero(wc->masks.dl_dst)
650 || !eth_addr_is_zero(wc->masks.arp_sha)
651 || !eth_addr_is_zero(wc->masks.arp_tha)
652 || !ipv6_mask_is_any(&wc->masks.ipv6_src)
653 || !ipv6_mask_is_any(&wc->masks.ipv6_dst)
654 || wc->masks.ipv6_label != htonl(0)
655 || !ipv6_mask_is_any(&wc->masks.nd_target)
656 || wc->masks.nw_proto != 0
657 || wc->masks.nw_frag != 0
658 || wc->masks.nw_tos != 0
659 || wc->masks.nw_ttl != 0) {
663 for (i = 0; i < FLOW_N_REGS; i++) {
664 if (wc->masks.regs[i] != 0) {
672 /* Initializes 'dst' as the combination of wildcards in 'src1' and 'src2'.
673 * That is, a bit or a field is wildcarded in 'dst' if it is wildcarded in
674 * 'src1' or 'src2' or both. */
676 flow_wildcards_combine(struct flow_wildcards *dst,
677 const struct flow_wildcards *src1,
678 const struct flow_wildcards *src2)
682 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 17);
684 dst->masks.tun_id = src1->masks.tun_id & src2->masks.tun_id;
685 dst->masks.nw_src = src1->masks.nw_src & src2->masks.nw_src;
686 dst->masks.nw_dst = src1->masks.nw_dst & src2->masks.nw_dst;
687 dst->masks.ipv6_src = ipv6_addr_bitand(&src1->masks.ipv6_src,
688 &src2->masks.ipv6_src);
689 dst->masks.ipv6_dst = ipv6_addr_bitand(&src1->masks.ipv6_dst,
690 &src2->masks.ipv6_dst);
691 dst->masks.ipv6_label = src1->masks.ipv6_label & src2->masks.ipv6_label;
692 dst->masks.nd_target = ipv6_addr_bitand(&src1->masks.nd_target,
693 &src2->masks.nd_target);
694 for (i = 0; i < FLOW_N_REGS; i++) {
695 dst->masks.regs[i] = src1->masks.regs[i] & src2->masks.regs[i];
697 dst->masks.metadata = src1->masks.metadata & src2->masks.metadata;
698 dst->masks.in_port = src1->masks.in_port & src2->masks.in_port;
699 dst->masks.vlan_tci = src1->masks.vlan_tci & src2->masks.vlan_tci;
700 dst->masks.dl_type = src1->masks.dl_type & src2->masks.dl_type;
701 dst->masks.tp_src = src1->masks.tp_src & src2->masks.tp_src;
702 dst->masks.tp_dst = src1->masks.tp_dst & src2->masks.tp_dst;
703 dst->masks.nw_frag = src1->masks.nw_frag & src2->masks.nw_frag;
704 eth_addr_bitand(src1->masks.dl_src, src2->masks.dl_src, dst->masks.dl_src);
705 eth_addr_bitand(src1->masks.dl_dst, src2->masks.dl_dst, dst->masks.dl_dst);
706 eth_addr_bitand(src1->masks.arp_sha, src2->masks.arp_sha,
708 eth_addr_bitand(src1->masks.arp_tha, src2->masks.arp_tha,
710 dst->masks.nw_proto = src1->masks.nw_proto & src2->masks.nw_proto;
711 dst->masks.nw_tos = src1->masks.nw_tos & src2->masks.nw_tos;
712 dst->masks.nw_ttl = src1->masks.nw_ttl & src2->masks.nw_ttl;
715 /* Returns a hash of the wildcards in 'wc'. */
717 flow_wildcards_hash(const struct flow_wildcards *wc, uint32_t basis)
719 return hash_words((const uint32_t *) wc, sizeof *wc / 4, basis);
722 /* Returns true if 'a' and 'b' represent the same wildcards, false if they are
725 flow_wildcards_equal(const struct flow_wildcards *a,
726 const struct flow_wildcards *b)
730 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 17);
732 if (a->masks.tun_id != b->masks.tun_id
733 || a->masks.nw_src != b->masks.nw_src
734 || a->masks.nw_dst != b->masks.nw_dst
735 || a->masks.in_port != b->masks.in_port
736 || a->masks.vlan_tci != b->masks.vlan_tci
737 || a->masks.metadata != b->masks.metadata
738 || a->masks.dl_type != b->masks.dl_type
739 || !ipv6_addr_equals(&a->masks.ipv6_src, &b->masks.ipv6_src)
740 || !ipv6_addr_equals(&a->masks.ipv6_dst, &b->masks.ipv6_dst)
741 || a->masks.ipv6_label != b->masks.ipv6_label
742 || !ipv6_addr_equals(&a->masks.nd_target, &b->masks.nd_target)
743 || a->masks.tp_src != b->masks.tp_src
744 || a->masks.tp_dst != b->masks.tp_dst
745 || a->masks.nw_frag != b->masks.nw_frag
746 || !eth_addr_equals(a->masks.dl_src, b->masks.dl_src)
747 || !eth_addr_equals(a->masks.dl_dst, b->masks.dl_dst)
748 || !eth_addr_equals(a->masks.arp_sha, b->masks.arp_sha)
749 || !eth_addr_equals(a->masks.arp_tha, b->masks.arp_tha)
750 || a->masks.nw_proto != b->masks.nw_proto
751 || a->masks.nw_tos != b->masks.nw_tos
752 || a->masks.nw_ttl != b->masks.nw_ttl) {
756 for (i = 0; i < FLOW_N_REGS; i++) {
757 if (a->masks.regs[i] != b->masks.regs[i]) {
765 /* Returns true if at least one bit or field is wildcarded in 'a' but not in
766 * 'b', false otherwise. */
768 flow_wildcards_has_extra(const struct flow_wildcards *a,
769 const struct flow_wildcards *b)
772 uint8_t eth_masked[ETH_ADDR_LEN];
773 struct in6_addr ipv6_masked;
775 BUILD_ASSERT_DECL(FLOW_WC_SEQ == 17);
777 for (i = 0; i < FLOW_N_REGS; i++) {
778 if ((a->masks.regs[i] & b->masks.regs[i]) != b->masks.regs[i]) {
783 eth_addr_bitand(a->masks.dl_src, b->masks.dl_src, eth_masked);
784 if (!eth_addr_equals(eth_masked, b->masks.dl_src)) {
788 eth_addr_bitand(a->masks.dl_dst, b->masks.dl_dst, eth_masked);
789 if (!eth_addr_equals(eth_masked, b->masks.dl_dst)) {
793 eth_addr_bitand(a->masks.arp_sha, b->masks.arp_sha, eth_masked);
794 if (!eth_addr_equals(eth_masked, b->masks.arp_sha)) {
798 eth_addr_bitand(a->masks.arp_tha, b->masks.arp_tha, eth_masked);
799 if (!eth_addr_equals(eth_masked, b->masks.arp_tha)) {
803 ipv6_masked = ipv6_addr_bitand(&a->masks.ipv6_src, &b->masks.ipv6_src);
804 if (!ipv6_addr_equals(&ipv6_masked, &b->masks.ipv6_src)) {
808 ipv6_masked = ipv6_addr_bitand(&a->masks.ipv6_dst, &b->masks.ipv6_dst);
809 if (!ipv6_addr_equals(&ipv6_masked, &b->masks.ipv6_dst)) {
813 ipv6_masked = ipv6_addr_bitand(&a->masks.nd_target, &b->masks.nd_target);
814 if (!ipv6_addr_equals(&ipv6_masked, &b->masks.nd_target)) {
818 return ((a->masks.tun_id & b->masks.tun_id) != b->masks.tun_id
819 || (a->masks.nw_src & b->masks.nw_src) != b->masks.nw_src
820 || (a->masks.nw_dst & b->masks.nw_dst) != b->masks.nw_dst
821 || ((a->masks.ipv6_label & b->masks.ipv6_label)
822 != b->masks.ipv6_label)
823 || (a->masks.in_port & b->masks.in_port) != b->masks.in_port
824 || (a->masks.vlan_tci & b->masks.vlan_tci) != b->masks.vlan_tci
825 || (a->masks.metadata & b->masks.metadata) != b->masks.metadata
826 || (a->masks.dl_type & b->masks.dl_type) != b->masks.dl_type
827 || (a->masks.dl_type & b->masks.dl_type) != b->masks.dl_type
828 || (a->masks.tp_src & b->masks.tp_src) != b->masks.tp_src
829 || (a->masks.tp_dst & b->masks.tp_dst) != b->masks.tp_dst
830 || (a->masks.nw_proto & b->masks.nw_proto) != b->masks.nw_proto
831 || (a->masks.nw_frag & b->masks.nw_frag) != b->masks.nw_frag
832 || (a->masks.nw_tos & b->masks.nw_tos) != b->masks.nw_tos
833 || (a->masks.nw_ttl & b->masks.nw_ttl) != b->masks.nw_ttl);
836 /* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
837 * (A 0-bit indicates a wildcard bit.) */
839 flow_wildcards_set_reg_mask(struct flow_wildcards *wc, int idx, uint32_t mask)
841 wc->masks.regs[idx] = mask;
844 /* Hashes 'flow' based on its L2 through L4 protocol information. */
846 flow_hash_symmetric_l4(const struct flow *flow, uint32_t basis)
851 struct in6_addr ipv6_addr;
856 uint8_t eth_addr[ETH_ADDR_LEN];
862 memset(&fields, 0, sizeof fields);
863 for (i = 0; i < ETH_ADDR_LEN; i++) {
864 fields.eth_addr[i] = flow->dl_src[i] ^ flow->dl_dst[i];
866 fields.vlan_tci = flow->vlan_tci & htons(VLAN_VID_MASK);
867 fields.eth_type = flow->dl_type;
869 /* UDP source and destination port are not taken into account because they
870 * will not necessarily be symmetric in a bidirectional flow. */
871 if (fields.eth_type == htons(ETH_TYPE_IP)) {
872 fields.ipv4_addr = flow->nw_src ^ flow->nw_dst;
873 fields.ip_proto = flow->nw_proto;
874 if (fields.ip_proto == IPPROTO_TCP) {
875 fields.tp_port = flow->tp_src ^ flow->tp_dst;
877 } else if (fields.eth_type == htons(ETH_TYPE_IPV6)) {
878 const uint8_t *a = &flow->ipv6_src.s6_addr[0];
879 const uint8_t *b = &flow->ipv6_dst.s6_addr[0];
880 uint8_t *ipv6_addr = &fields.ipv6_addr.s6_addr[0];
882 for (i=0; i<16; i++) {
883 ipv6_addr[i] = a[i] ^ b[i];
885 fields.ip_proto = flow->nw_proto;
886 if (fields.ip_proto == IPPROTO_TCP) {
887 fields.tp_port = flow->tp_src ^ flow->tp_dst;
890 return hash_bytes(&fields, sizeof fields, basis);
893 /* Hashes the portions of 'flow' designated by 'fields'. */
895 flow_hash_fields(const struct flow *flow, enum nx_hash_fields fields,
900 case NX_HASH_FIELDS_ETH_SRC:
901 return hash_bytes(flow->dl_src, sizeof flow->dl_src, basis);
903 case NX_HASH_FIELDS_SYMMETRIC_L4:
904 return flow_hash_symmetric_l4(flow, basis);
910 /* Returns a string representation of 'fields'. */
912 flow_hash_fields_to_str(enum nx_hash_fields fields)
915 case NX_HASH_FIELDS_ETH_SRC: return "eth_src";
916 case NX_HASH_FIELDS_SYMMETRIC_L4: return "symmetric_l4";
917 default: return "<unknown>";
921 /* Returns true if the value of 'fields' is supported. Otherwise false. */
923 flow_hash_fields_valid(enum nx_hash_fields fields)
925 return fields == NX_HASH_FIELDS_ETH_SRC
926 || fields == NX_HASH_FIELDS_SYMMETRIC_L4;
929 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
930 * OpenFlow 1.0 "dl_vlan" value:
932 * - If it is in the range 0...4095, 'flow->vlan_tci' is set to match
933 * that VLAN. Any existing PCP match is unchanged (it becomes 0 if
934 * 'flow' previously matched packets without a VLAN header).
936 * - If it is OFP_VLAN_NONE, 'flow->vlan_tci' is set to match a packet
937 * without a VLAN tag.
939 * - Other values of 'vid' should not be used. */
941 flow_set_dl_vlan(struct flow *flow, ovs_be16 vid)
943 if (vid == htons(OFP10_VLAN_NONE)) {
944 flow->vlan_tci = htons(0);
946 vid &= htons(VLAN_VID_MASK);
947 flow->vlan_tci &= ~htons(VLAN_VID_MASK);
948 flow->vlan_tci |= htons(VLAN_CFI) | vid;
952 /* Sets the VLAN VID that 'flow' matches to 'vid', which is interpreted as an
953 * OpenFlow 1.2 "vlan_vid" value, that is, the low 13 bits of 'vlan_tci' (VID
956 flow_set_vlan_vid(struct flow *flow, ovs_be16 vid)
958 ovs_be16 mask = htons(VLAN_VID_MASK | VLAN_CFI);
959 flow->vlan_tci &= ~mask;
960 flow->vlan_tci |= vid & mask;
963 /* Sets the VLAN PCP that 'flow' matches to 'pcp', which should be in the
966 * This function has no effect on the VLAN ID that 'flow' matches.
968 * After calling this function, 'flow' will not match packets without a VLAN
971 flow_set_vlan_pcp(struct flow *flow, uint8_t pcp)
974 flow->vlan_tci &= ~htons(VLAN_PCP_MASK);
975 flow->vlan_tci |= htons((pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
978 /* Puts into 'b' a packet that flow_extract() would parse as having the given
981 * (This is useful only for testing, obviously, and the packet isn't really
982 * valid. It hasn't got some checksums filled in, for one, and lots of fields
983 * are just zeroed.) */
985 flow_compose(struct ofpbuf *b, const struct flow *flow)
987 eth_compose(b, flow->dl_dst, flow->dl_src, ntohs(flow->dl_type), 0);
988 if (flow->dl_type == htons(FLOW_DL_TYPE_NONE)) {
989 struct eth_header *eth = b->l2;
990 eth->eth_type = htons(b->size);
994 if (flow->vlan_tci & htons(VLAN_CFI)) {
995 eth_push_vlan(b, flow->vlan_tci);
998 if (flow->dl_type == htons(ETH_TYPE_IP)) {
999 struct ip_header *ip;
1001 b->l3 = ip = ofpbuf_put_zeros(b, sizeof *ip);
1002 ip->ip_ihl_ver = IP_IHL_VER(5, 4);
1003 ip->ip_tos = flow->nw_tos;
1004 ip->ip_proto = flow->nw_proto;
1005 ip->ip_src = flow->nw_src;
1006 ip->ip_dst = flow->nw_dst;
1008 if (flow->nw_frag & FLOW_NW_FRAG_ANY) {
1009 ip->ip_frag_off |= htons(IP_MORE_FRAGMENTS);
1010 if (flow->nw_frag & FLOW_NW_FRAG_LATER) {
1011 ip->ip_frag_off |= htons(100);
1014 if (!(flow->nw_frag & FLOW_NW_FRAG_ANY)
1015 || !(flow->nw_frag & FLOW_NW_FRAG_LATER)) {
1016 if (flow->nw_proto == IPPROTO_TCP) {
1017 struct tcp_header *tcp;
1019 b->l4 = tcp = ofpbuf_put_zeros(b, sizeof *tcp);
1020 tcp->tcp_src = flow->tp_src;
1021 tcp->tcp_dst = flow->tp_dst;
1022 tcp->tcp_ctl = TCP_CTL(0, 5);
1023 } else if (flow->nw_proto == IPPROTO_UDP) {
1024 struct udp_header *udp;
1026 b->l4 = udp = ofpbuf_put_zeros(b, sizeof *udp);
1027 udp->udp_src = flow->tp_src;
1028 udp->udp_dst = flow->tp_dst;
1029 } else if (flow->nw_proto == IPPROTO_ICMP) {
1030 struct icmp_header *icmp;
1032 b->l4 = icmp = ofpbuf_put_zeros(b, sizeof *icmp);
1033 icmp->icmp_type = ntohs(flow->tp_src);
1034 icmp->icmp_code = ntohs(flow->tp_dst);
1035 icmp->icmp_csum = csum(icmp, ICMP_HEADER_LEN);
1040 ip->ip_tot_len = htons((uint8_t *) b->data + b->size
1041 - (uint8_t *) b->l3);
1042 ip->ip_csum = csum(ip, sizeof *ip);
1043 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1045 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
1046 struct arp_eth_header *arp;
1048 b->l3 = arp = ofpbuf_put_zeros(b, sizeof *arp);
1049 arp->ar_hrd = htons(1);
1050 arp->ar_pro = htons(ETH_TYPE_IP);
1051 arp->ar_hln = ETH_ADDR_LEN;
1053 arp->ar_op = htons(flow->nw_proto);
1055 if (flow->nw_proto == ARP_OP_REQUEST ||
1056 flow->nw_proto == ARP_OP_REPLY) {
1057 arp->ar_spa = flow->nw_src;
1058 arp->ar_tpa = flow->nw_dst;
1059 memcpy(arp->ar_sha, flow->arp_sha, ETH_ADDR_LEN);
1060 memcpy(arp->ar_tha, flow->arp_tha, ETH_ADDR_LEN);