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>
21 #include <netinet/in.h>
22 #include <netinet/icmp6.h>
23 #include <netinet/ip6.h>
26 #include "byte-order.h"
29 #include "dynamic-string.h"
32 #include "openflow/openflow.h"
33 #include "openvswitch/datapath-protocol.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)
138 int nh_len = sizeof(struct ip6_hdr);
143 if (packet->size < sizeof *nh) {
148 nexthdr = nh->ip6_nxt;
149 payload_len = ntohs(nh->ip6_plen);
151 flow->ipv6_src = nh->ip6_src;
152 flow->ipv6_dst = nh->ip6_dst;
154 tc_flow = get_unaligned_be32(&nh->ip6_flow);
155 flow->nw_tos = (ntohl(tc_flow) >> 4) & IP_DSCP_MASK;
156 flow->nw_proto = IPPROTO_NONE;
158 /* We don't process jumbograms. */
163 if (packet->size < sizeof *nh + payload_len) {
168 if ((nexthdr != IPPROTO_HOPOPTS)
169 && (nexthdr != IPPROTO_ROUTING)
170 && (nexthdr != IPPROTO_DSTOPTS)
171 && (nexthdr != IPPROTO_AH)
172 && (nexthdr != IPPROTO_FRAGMENT)) {
173 /* It's either a terminal header (e.g., TCP, UDP) or one we
174 * don't understand. In either case, we're done with the
175 * packet, so use it to fill in 'nw_proto'. */
179 /* We only verify that at least 8 bytes of the next header are
180 * available, but many of these headers are longer. Ensure that
181 * accesses within the extension header are within those first 8
183 if (packet->size < nh_len + 8) {
187 if ((nexthdr == IPPROTO_HOPOPTS)
188 || (nexthdr == IPPROTO_ROUTING)
189 || (nexthdr == IPPROTO_DSTOPTS)) {
190 /* These headers, while different, have the fields we care about
191 * in the same location and with the same interpretation. */
192 struct ip6_ext *ext_hdr;
194 ext_hdr = (struct ip6_ext *)((char *)packet->data + nh_len);
195 nexthdr = ext_hdr->ip6e_nxt;
196 nh_len += (ext_hdr->ip6e_len + 1) * 8;
197 } else if (nexthdr == IPPROTO_AH) {
198 /* A standard AH definition isn't available, but the fields
199 * we care about are in the same location as the generic
200 * option header--only the header length is calculated
202 struct ip6_ext *ext_hdr;
204 ext_hdr = (struct ip6_ext *)((char *)packet->data + nh_len);
205 nexthdr = ext_hdr->ip6e_nxt;
206 nh_len += (ext_hdr->ip6e_len + 2) * 4;
207 } else if (nexthdr == IPPROTO_FRAGMENT) {
208 struct ip6_frag *frag_hdr;
210 frag_hdr = (struct ip6_frag *)((char *)packet->data + nh_len);
212 nexthdr = frag_hdr->ip6f_nxt;
213 nh_len += sizeof *frag_hdr;
215 /* We only process the first fragment. */
216 if ((frag_hdr->ip6f_offlg & IP6F_OFF_MASK) != htons(0)) {
217 nexthdr = IPPROTO_FRAGMENT;
223 /* The payload length claims to be smaller than the size of the
224 * headers we've already processed. */
225 if (payload_len < nh_len - sizeof *nh) {
229 flow->nw_proto = nexthdr;
233 /* Neighbor Discovery Solicitation and Advertisement messages are
234 * identical in structure, so we'll just use one of them. To be safe,
235 * we'll assert that they're still identical. */
236 BUILD_ASSERT_DECL(sizeof(struct nd_neighbor_solicit)
237 == sizeof(struct nd_neighbor_advert));
240 parse_icmpv6(struct ofpbuf *b, struct flow *flow, int icmp_len)
242 const struct icmp6_hdr *icmp = pull_icmpv6(b);
248 /* The ICMPv6 type and code fields use the 16-bit transport port
249 * fields, so we need to store them in 16-bit network byte order. */
250 flow->icmp_type = htons(icmp->icmp6_type);
251 flow->icmp_code = htons(icmp->icmp6_code);
253 if (!icmp->icmp6_code
254 && ((icmp->icmp6_type == ND_NEIGHBOR_SOLICIT)
255 || (icmp->icmp6_type == ND_NEIGHBOR_ADVERT))) {
256 struct nd_neighbor_solicit *nd_ns; /* Identical to ND advert */
258 /* In order to process neighbor discovery options, we need the
260 if ((icmp_len < sizeof *nd_ns)
261 || (!ofpbuf_try_pull(b, sizeof *nd_ns - sizeof *icmp))) {
264 nd_ns = (struct nd_neighbor_solicit *)icmp;
265 flow->nd_target = nd_ns->nd_ns_target;
267 icmp_len -= sizeof(*nd_ns);
268 while (icmp_len >= 8) {
269 struct nd_opt_hdr *nd_opt;
273 /* The minimum size of an option is 8 bytes, which also is
274 * the size of Ethernet link-layer options. */
275 nd_opt = ofpbuf_pull(b, 8);
276 if (!nd_opt->nd_opt_len || nd_opt->nd_opt_len * 8 > icmp_len) {
279 opt_len = nd_opt->nd_opt_len * 8;
280 data = (const uint8_t *)(nd_opt + 1);
282 /* Store the link layer address if the appropriate option is
283 * provided. It is considered an error if the same link
284 * layer option is specified twice. */
285 if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LINKADDR
287 if (eth_addr_is_zero(flow->arp_sha)) {
288 memcpy(flow->arp_sha, data, ETH_ADDR_LEN);
292 } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LINKADDR
294 if (eth_addr_is_zero(flow->arp_tha)) {
295 memcpy(flow->arp_tha, data, ETH_ADDR_LEN);
301 /* Pull the rest of this option. */
302 if (!ofpbuf_try_pull(b, opt_len - 8)) {
313 memset(&flow->nd_target, '\0', sizeof(flow->nd_target));
314 memset(flow->arp_sha, '\0', sizeof(flow->arp_sha));
315 memset(flow->arp_tha, '\0', sizeof(flow->arp_tha));
321 /* Initializes 'flow' members from 'packet', 'tun_id', and 'in_port.
322 * Initializes 'packet' header pointers as follows:
324 * - packet->l2 to the start of the Ethernet header.
326 * - packet->l3 to just past the Ethernet header, or just past the
327 * vlan_header if one is present, to the first byte of the payload of the
330 * - packet->l4 to just past the IPv4 header, if one is present and has a
331 * correct length, and otherwise NULL.
333 * - packet->l7 to just past the TCP or UDP or ICMP header, if one is
334 * present and has a correct length, and otherwise NULL.
337 flow_extract(struct ofpbuf *packet, ovs_be64 tun_id, uint16_t in_port,
340 struct ofpbuf b = *packet;
341 struct eth_header *eth;
344 COVERAGE_INC(flow_extract);
346 memset(flow, 0, sizeof *flow);
347 flow->tun_id = tun_id;
348 flow->in_port = in_port;
355 if (b.size < sizeof *eth) {
361 memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
362 memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
364 /* dl_type, vlan_tci. */
365 ofpbuf_pull(&b, ETH_ADDR_LEN * 2);
366 if (eth->eth_type == htons(ETH_TYPE_VLAN)) {
367 parse_vlan(&b, flow);
369 flow->dl_type = parse_ethertype(&b);
373 if (flow->dl_type == htons(ETH_TYPE_IP)) {
374 const struct ip_header *nh = pull_ip(&b);
376 flow->nw_src = get_unaligned_be32(&nh->ip_src);
377 flow->nw_dst = get_unaligned_be32(&nh->ip_dst);
378 flow->nw_tos = nh->ip_tos & IP_DSCP_MASK;
379 flow->nw_proto = nh->ip_proto;
381 if (!IP_IS_FRAGMENT(nh->ip_frag_off)) {
382 if (flow->nw_proto == IPPROTO_TCP) {
383 const struct tcp_header *tcp = pull_tcp(&b);
385 flow->tp_src = tcp->tcp_src;
386 flow->tp_dst = tcp->tcp_dst;
389 } else if (flow->nw_proto == IPPROTO_UDP) {
390 const struct udp_header *udp = pull_udp(&b);
392 flow->tp_src = udp->udp_src;
393 flow->tp_dst = udp->udp_dst;
396 } else if (flow->nw_proto == IPPROTO_ICMP) {
397 const struct icmp_header *icmp = pull_icmp(&b);
399 flow->icmp_type = htons(icmp->icmp_type);
400 flow->icmp_code = htons(icmp->icmp_code);
408 } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
410 const struct ip6_hdr *nh;
412 nh_len = parse_ipv6(&b, flow);
417 nh = ofpbuf_pull(&b, nh_len);
420 if (flow->nw_proto == IPPROTO_TCP) {
421 const struct tcp_header *tcp = pull_tcp(&b);
423 flow->tp_src = tcp->tcp_src;
424 flow->tp_dst = tcp->tcp_dst;
427 } else if (flow->nw_proto == IPPROTO_UDP) {
428 const struct udp_header *udp = pull_udp(&b);
430 flow->tp_src = udp->udp_src;
431 flow->tp_dst = udp->udp_dst;
434 } else if (flow->nw_proto == IPPROTO_ICMPV6) {
435 int icmp_len = ntohs(nh->ip6_plen) + sizeof *nh - nh_len;
436 if (parse_icmpv6(&b, flow, icmp_len)) {
441 } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
442 const struct arp_eth_header *arp = pull_arp(&b);
443 if (arp && arp->ar_hrd == htons(1)
444 && arp->ar_pro == htons(ETH_TYPE_IP)
445 && arp->ar_hln == ETH_ADDR_LEN
446 && arp->ar_pln == 4) {
447 /* We only match on the lower 8 bits of the opcode. */
448 if (ntohs(arp->ar_op) <= 0xff) {
449 flow->nw_proto = ntohs(arp->ar_op);
452 if ((flow->nw_proto == ARP_OP_REQUEST)
453 || (flow->nw_proto == ARP_OP_REPLY)) {
454 flow->nw_src = arp->ar_spa;
455 flow->nw_dst = arp->ar_tpa;
456 memcpy(flow->arp_sha, arp->ar_sha, ETH_ADDR_LEN);
457 memcpy(flow->arp_tha, arp->ar_tha, ETH_ADDR_LEN);
465 /* Extracts the flow stats for a packet. The 'flow' and 'packet'
466 * arguments must have been initialized through a call to flow_extract().
469 flow_extract_stats(const struct flow *flow, struct ofpbuf *packet,
470 struct dpif_flow_stats *stats)
472 memset(stats, 0, sizeof(*stats));
474 if ((flow->dl_type == htons(ETH_TYPE_IP)) && packet->l4) {
475 if ((flow->nw_proto == IPPROTO_TCP) && packet->l7) {
476 struct tcp_header *tcp = packet->l4;
477 stats->tcp_flags = TCP_FLAGS(tcp->tcp_ctl);
481 stats->n_bytes = packet->size;
482 stats->n_packets = 1;
486 flow_to_string(const struct flow *flow)
488 struct ds ds = DS_EMPTY_INITIALIZER;
489 flow_format(&ds, flow);
494 flow_format(struct ds *ds, const struct flow *flow)
496 ds_put_format(ds, "tunnel%#"PRIx64":in_port%04"PRIx16":tci(",
497 flow->tun_id, flow->in_port);
498 if (flow->vlan_tci) {
499 ds_put_format(ds, "vlan%"PRIu16",pcp%d",
500 vlan_tci_to_vid(flow->vlan_tci),
501 vlan_tci_to_pcp(flow->vlan_tci));
503 ds_put_char(ds, '0');
505 ds_put_format(ds, ") mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT
507 ETH_ADDR_ARGS(flow->dl_src),
508 ETH_ADDR_ARGS(flow->dl_dst),
509 ntohs(flow->dl_type));
511 if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
512 ds_put_format(ds, " proto%"PRIu8" tos%"PRIu8" ipv6",
513 flow->nw_proto, flow->nw_tos);
514 print_ipv6_addr(ds, &flow->ipv6_src);
515 ds_put_cstr(ds, "->");
516 print_ipv6_addr(ds, &flow->ipv6_dst);
519 ds_put_format(ds, " proto%"PRIu8
521 " ip"IP_FMT"->"IP_FMT,
524 IP_ARGS(&flow->nw_src),
525 IP_ARGS(&flow->nw_dst));
527 if (flow->tp_src || flow->tp_dst) {
528 ds_put_format(ds, " port%"PRIu16"->%"PRIu16,
529 ntohs(flow->tp_src), ntohs(flow->tp_dst));
531 if (!eth_addr_is_zero(flow->arp_sha) || !eth_addr_is_zero(flow->arp_tha)) {
532 ds_put_format(ds, " arp_ha"ETH_ADDR_FMT"->"ETH_ADDR_FMT,
533 ETH_ADDR_ARGS(flow->arp_sha),
534 ETH_ADDR_ARGS(flow->arp_tha));
539 flow_print(FILE *stream, const struct flow *flow)
541 char *s = flow_to_string(flow);
546 /* flow_wildcards functions. */
548 /* Initializes 'wc' as a set of wildcards that matches every packet. */
550 flow_wildcards_init_catchall(struct flow_wildcards *wc)
552 wc->wildcards = FWW_ALL;
553 wc->tun_id_mask = htonll(0);
554 wc->nw_src_mask = htonl(0);
555 wc->nw_dst_mask = htonl(0);
556 wc->ipv6_src_mask = in6addr_any;
557 wc->ipv6_dst_mask = in6addr_any;
558 memset(wc->reg_masks, 0, sizeof wc->reg_masks);
559 wc->vlan_tci_mask = htons(0);
563 /* Initializes 'wc' as an exact-match set of wildcards; that is, 'wc' does not
564 * wildcard any bits or fields. */
566 flow_wildcards_init_exact(struct flow_wildcards *wc)
569 wc->tun_id_mask = htonll(UINT64_MAX);
570 wc->nw_src_mask = htonl(UINT32_MAX);
571 wc->nw_dst_mask = htonl(UINT32_MAX);
572 wc->ipv6_src_mask = in6addr_exact;
573 wc->ipv6_dst_mask = in6addr_exact;
574 memset(wc->reg_masks, 0xff, sizeof wc->reg_masks);
575 wc->vlan_tci_mask = htons(UINT16_MAX);
579 /* Returns true if 'wc' is exact-match, false if 'wc' wildcards any bits or
582 flow_wildcards_is_exact(const struct flow_wildcards *wc)
587 || wc->tun_id_mask != htonll(UINT64_MAX)
588 || wc->nw_src_mask != htonl(UINT32_MAX)
589 || wc->nw_dst_mask != htonl(UINT32_MAX)
590 || wc->vlan_tci_mask != htons(UINT16_MAX)
591 || !ipv6_mask_is_exact(&wc->ipv6_src_mask)
592 || !ipv6_mask_is_exact(&wc->ipv6_dst_mask)) {
596 for (i = 0; i < FLOW_N_REGS; i++) {
597 if (wc->reg_masks[i] != htonl(UINT32_MAX)) {
605 /* Initializes 'dst' as the combination of wildcards in 'src1' and 'src2'.
606 * That is, a bit or a field is wildcarded in 'dst' if it is wildcarded in
607 * 'src1' or 'src2' or both. */
609 flow_wildcards_combine(struct flow_wildcards *dst,
610 const struct flow_wildcards *src1,
611 const struct flow_wildcards *src2)
615 dst->wildcards = src1->wildcards | src2->wildcards;
616 dst->tun_id_mask = src1->tun_id_mask & src2->tun_id_mask;
617 dst->nw_src_mask = src1->nw_src_mask & src2->nw_src_mask;
618 dst->nw_dst_mask = src1->nw_dst_mask & src2->nw_dst_mask;
619 dst->ipv6_src_mask = ipv6_addr_bitand(&src1->ipv6_src_mask,
620 &src2->ipv6_src_mask);
621 dst->ipv6_dst_mask = ipv6_addr_bitand(&src1->ipv6_dst_mask,
622 &src2->ipv6_dst_mask);
623 for (i = 0; i < FLOW_N_REGS; i++) {
624 dst->reg_masks[i] = src1->reg_masks[i] & src2->reg_masks[i];
626 dst->vlan_tci_mask = src1->vlan_tci_mask & src2->vlan_tci_mask;
629 /* Returns a hash of the wildcards in 'wc'. */
631 flow_wildcards_hash(const struct flow_wildcards *wc)
633 /* If you change struct flow_wildcards and thereby trigger this
634 * assertion, please check that the new struct flow_wildcards has no holes
635 * in it before you update the assertion. */
636 BUILD_ASSERT_DECL(sizeof *wc == 56 + FLOW_N_REGS * 4);
637 return hash_bytes(wc, sizeof *wc, 0);
640 /* Returns true if 'a' and 'b' represent the same wildcards, false if they are
643 flow_wildcards_equal(const struct flow_wildcards *a,
644 const struct flow_wildcards *b)
648 if (a->wildcards != b->wildcards
649 || a->tun_id_mask != b->tun_id_mask
650 || a->nw_src_mask != b->nw_src_mask
651 || a->nw_dst_mask != b->nw_dst_mask
652 || a->vlan_tci_mask != b->vlan_tci_mask
653 || !ipv6_addr_equals(&a->ipv6_src_mask, &b->ipv6_src_mask)
654 || !ipv6_addr_equals(&a->ipv6_dst_mask, &b->ipv6_dst_mask)) {
658 for (i = 0; i < FLOW_N_REGS; i++) {
659 if (a->reg_masks[i] != b->reg_masks[i]) {
667 /* Returns true if at least one bit or field is wildcarded in 'a' but not in
668 * 'b', false otherwise. */
670 flow_wildcards_has_extra(const struct flow_wildcards *a,
671 const struct flow_wildcards *b)
674 struct in6_addr ipv6_masked;
676 for (i = 0; i < FLOW_N_REGS; i++) {
677 if ((a->reg_masks[i] & b->reg_masks[i]) != b->reg_masks[i]) {
682 ipv6_masked = ipv6_addr_bitand(&a->ipv6_src_mask, &b->ipv6_src_mask);
683 if (!ipv6_addr_equals(&ipv6_masked, &b->ipv6_src_mask)) {
687 ipv6_masked = ipv6_addr_bitand(&a->ipv6_dst_mask, &b->ipv6_dst_mask);
688 if (!ipv6_addr_equals(&ipv6_masked, &b->ipv6_dst_mask)) {
692 return (a->wildcards & ~b->wildcards
693 || (a->tun_id_mask & b->tun_id_mask) != b->tun_id_mask
694 || (a->nw_src_mask & b->nw_src_mask) != b->nw_src_mask
695 || (a->nw_dst_mask & b->nw_dst_mask) != b->nw_dst_mask
696 || (a->vlan_tci_mask & b->vlan_tci_mask) != b->vlan_tci_mask);
700 set_nw_mask(ovs_be32 *maskp, ovs_be32 mask)
702 if (ip_is_cidr(mask)) {
710 /* Sets the IP (or ARP) source wildcard mask to CIDR 'mask' (consisting of N
711 * high-order 1-bit and 32-N low-order 0-bits). Returns true if successful,
712 * false if 'mask' is not a CIDR mask. */
714 flow_wildcards_set_nw_src_mask(struct flow_wildcards *wc, ovs_be32 mask)
716 return set_nw_mask(&wc->nw_src_mask, mask);
719 /* Sets the IP (or ARP) destination wildcard mask to CIDR 'mask' (consisting of
720 * N high-order 1-bit and 32-N low-order 0-bits). Returns true if successful,
721 * false if 'mask' is not a CIDR mask. */
723 flow_wildcards_set_nw_dst_mask(struct flow_wildcards *wc, ovs_be32 mask)
725 return set_nw_mask(&wc->nw_dst_mask, mask);
729 set_ipv6_mask(struct in6_addr *maskp, const struct in6_addr *mask)
731 if (ipv6_is_cidr(mask)) {
739 /* Sets the IPv6 source wildcard mask to CIDR 'mask' (consisting of N
740 * high-order 1-bit and 128-N low-order 0-bits). Returns true if successful,
741 * false if 'mask' is not a CIDR mask. */
743 flow_wildcards_set_ipv6_src_mask(struct flow_wildcards *wc,
744 const struct in6_addr *mask)
746 return set_ipv6_mask(&wc->ipv6_src_mask, mask);
749 /* Sets the IPv6 destination wildcard mask to CIDR 'mask' (consisting of
750 * N high-order 1-bit and 128-N low-order 0-bits). Returns true if
751 * successful, false if 'mask' is not a CIDR mask. */
753 flow_wildcards_set_ipv6_dst_mask(struct flow_wildcards *wc,
754 const struct in6_addr *mask)
756 return set_ipv6_mask(&wc->ipv6_dst_mask, mask);
759 /* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
760 * (A 0-bit indicates a wildcard bit.) */
762 flow_wildcards_set_reg_mask(struct flow_wildcards *wc, int idx, uint32_t mask)
764 wc->reg_masks[idx] = mask;
767 /* Hashes 'flow' based on its L2 through L4 protocol information. */
769 flow_hash_symmetric_l4(const struct flow *flow, uint32_t basis)
774 struct in6_addr ipv6_addr;
779 uint8_t eth_addr[ETH_ADDR_LEN];
785 memset(&fields, 0, sizeof fields);
786 for (i = 0; i < ETH_ADDR_LEN; i++) {
787 fields.eth_addr[i] = flow->dl_src[i] ^ flow->dl_dst[i];
789 fields.vlan_tci = flow->vlan_tci & htons(VLAN_VID_MASK);
790 fields.eth_type = flow->dl_type;
791 if (fields.eth_type == htons(ETH_TYPE_IP)) {
792 fields.ipv4_addr = flow->nw_src ^ flow->nw_dst;
793 fields.ip_proto = flow->nw_proto;
794 if (fields.ip_proto == IPPROTO_TCP || fields.ip_proto == IPPROTO_UDP) {
795 fields.tp_addr = flow->tp_src ^ flow->tp_dst;
797 } else if (fields.eth_type == htons(ETH_TYPE_IPV6)) {
798 const uint8_t *a = &flow->ipv6_src.s6_addr[0];
799 const uint8_t *b = &flow->ipv6_dst.s6_addr[0];
800 uint8_t *ipv6_addr = &fields.ipv6_addr.s6_addr[0];
802 for (i=0; i<16; i++) {
803 ipv6_addr[i] = a[i] ^ b[i];
805 fields.ip_proto = flow->nw_proto;
806 if (fields.ip_proto == IPPROTO_TCP || fields.ip_proto == IPPROTO_UDP) {
807 fields.tp_addr = flow->tp_src ^ flow->tp_dst;
810 return hash_bytes(&fields, sizeof fields, basis);