flow: Move flow_extract_stats() to dpif.c, as dpif_flow_stats_extract().
[sliver-openvswitch.git] / lib / flow.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #include <config.h>
17 #include <sys/types.h>
18 #include "flow.h"
19 #include <assert.h>
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <netinet/in.h>
23 #include <netinet/icmp6.h>
24 #include <netinet/ip6.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "byte-order.h"
28 #include "coverage.h"
29 #include "dynamic-string.h"
30 #include "hash.h"
31 #include "ofpbuf.h"
32 #include "openflow/openflow.h"
33 #include "packets.h"
34 #include "unaligned.h"
35 #include "vlog.h"
36
37 VLOG_DEFINE_THIS_MODULE(flow);
38
39 COVERAGE_DEFINE(flow_extract);
40
41 static struct arp_eth_header *
42 pull_arp(struct ofpbuf *packet)
43 {
44     return ofpbuf_try_pull(packet, ARP_ETH_HEADER_LEN);
45 }
46
47 static struct ip_header *
48 pull_ip(struct ofpbuf *packet)
49 {
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);
55         }
56     }
57     return NULL;
58 }
59
60 static struct tcp_header *
61 pull_tcp(struct ofpbuf *packet)
62 {
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);
68         }
69     }
70     return NULL;
71 }
72
73 static struct udp_header *
74 pull_udp(struct ofpbuf *packet)
75 {
76     return ofpbuf_try_pull(packet, UDP_HEADER_LEN);
77 }
78
79 static struct icmp_header *
80 pull_icmp(struct ofpbuf *packet)
81 {
82     return ofpbuf_try_pull(packet, ICMP_HEADER_LEN);
83 }
84
85 static struct icmp6_hdr *
86 pull_icmpv6(struct ofpbuf *packet)
87 {
88     return ofpbuf_try_pull(packet, sizeof(struct icmp6_hdr));
89 }
90
91 static void
92 parse_vlan(struct ofpbuf *b, struct flow *flow)
93 {
94     struct qtag_prefix {
95         ovs_be16 eth_type;      /* ETH_TYPE_VLAN */
96         ovs_be16 tci;
97     };
98
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);
102     }
103 }
104
105 static ovs_be16
106 parse_ethertype(struct ofpbuf *b)
107 {
108     struct llc_snap_header *llc;
109     ovs_be16 proto;
110
111     proto = *(ovs_be16 *) ofpbuf_pull(b, sizeof proto);
112     if (ntohs(proto) >= ETH_TYPE_MIN) {
113         return proto;
114     }
115
116     if (b->size < sizeof *llc) {
117         return htons(FLOW_DL_TYPE_NONE);
118     }
119
120     llc = b->data;
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);
127     }
128
129     ofpbuf_pull(b, sizeof *llc);
130     return llc->snap.snap_type;
131 }
132
133 static int
134 parse_ipv6(struct ofpbuf *packet, struct flow *flow)
135 {
136     const struct ip6_hdr *nh;
137     ovs_be32 tc_flow;
138     int nexthdr;
139
140     nh = ofpbuf_try_pull(packet, sizeof *nh);
141     if (!nh) {
142         return EINVAL;
143     }
144
145     nexthdr = nh->ip6_nxt;
146
147     flow->ipv6_src = nh->ip6_src;
148     flow->ipv6_dst = nh->ip6_dst;
149
150     tc_flow = get_unaligned_be32(&nh->ip6_flow);
151     flow->nw_tos = (ntohl(tc_flow) >> 4) & IP_DSCP_MASK;
152     flow->nw_proto = IPPROTO_NONE;
153
154     while (1) {
155         if ((nexthdr != IPPROTO_HOPOPTS)
156                 && (nexthdr != IPPROTO_ROUTING)
157                 && (nexthdr != IPPROTO_DSTOPTS)
158                 && (nexthdr != IPPROTO_AH)
159                 && (nexthdr != IPPROTO_FRAGMENT)) {
160             /* It's either a terminal header (e.g., TCP, UDP) or one we
161              * don't understand.  In either case, we're done with the
162              * packet, so use it to fill in 'nw_proto'. */
163             break;
164         }
165
166         /* We only verify that at least 8 bytes of the next header are
167          * available, but many of these headers are longer.  Ensure that
168          * accesses within the extension header are within those first 8
169          * bytes. All extension headers are required to be at least 8
170          * bytes. */
171         if (packet->size < 8) {
172             return EINVAL;
173         }
174
175         if ((nexthdr == IPPROTO_HOPOPTS)
176                 || (nexthdr == IPPROTO_ROUTING)
177                 || (nexthdr == IPPROTO_DSTOPTS)) {
178             /* These headers, while different, have the fields we care about
179              * in the same location and with the same interpretation. */
180             const struct ip6_ext *ext_hdr = (struct ip6_ext *)packet->data;
181             nexthdr = ext_hdr->ip6e_nxt;
182             if (!ofpbuf_try_pull(packet, (ext_hdr->ip6e_len + 1) * 8)) {
183                 return EINVAL;
184             }
185         } else if (nexthdr == IPPROTO_AH) {
186             /* A standard AH definition isn't available, but the fields
187              * we care about are in the same location as the generic
188              * option header--only the header length is calculated
189              * differently. */
190             const struct ip6_ext *ext_hdr = (struct ip6_ext *)packet->data;
191             nexthdr = ext_hdr->ip6e_nxt;
192             if (!ofpbuf_try_pull(packet, (ext_hdr->ip6e_len + 2) * 4)) {
193                return EINVAL;
194             }
195         } else if (nexthdr == IPPROTO_FRAGMENT) {
196             const struct ip6_frag *frag_hdr = (struct ip6_frag *)packet->data;
197
198             nexthdr = frag_hdr->ip6f_nxt;
199             if (!ofpbuf_try_pull(packet, sizeof *frag_hdr)) {
200                 return EINVAL;
201             }
202
203             /* We only process the first fragment. */
204             if ((frag_hdr->ip6f_offlg & IP6F_OFF_MASK) != htons(0)) {
205                 nexthdr = IPPROTO_FRAGMENT;
206                 break;
207             }
208         }
209     }
210
211     flow->nw_proto = nexthdr;
212     return 0;
213 }
214
215 static void
216 parse_tcp(struct ofpbuf *packet, struct ofpbuf *b, struct flow *flow)
217 {
218     const struct tcp_header *tcp = pull_tcp(b);
219     if (tcp) {
220         flow->tp_src = tcp->tcp_src;
221         flow->tp_dst = tcp->tcp_dst;
222         packet->l7 = b->data;
223     }
224 }
225
226 static void
227 parse_udp(struct ofpbuf *packet, struct ofpbuf *b, struct flow *flow)
228 {
229     const struct udp_header *udp = pull_udp(b);
230     if (udp) {
231         flow->tp_src = udp->udp_src;
232         flow->tp_dst = udp->udp_dst;
233         packet->l7 = b->data;
234     }
235 }
236
237 static bool
238 parse_icmpv6(struct ofpbuf *b, struct flow *flow)
239 {
240     const struct icmp6_hdr *icmp = pull_icmpv6(b);
241
242     if (!icmp) {
243         return false;
244     }
245
246     /* The ICMPv6 type and code fields use the 16-bit transport port
247      * fields, so we need to store them in 16-bit network byte order. */
248     flow->icmp_type = htons(icmp->icmp6_type);
249     flow->icmp_code = htons(icmp->icmp6_code);
250
251     if (icmp->icmp6_code == 0 &&
252         (icmp->icmp6_type == ND_NEIGHBOR_SOLICIT ||
253          icmp->icmp6_type == ND_NEIGHBOR_ADVERT)) {
254         const struct in6_addr *nd_target;
255
256         nd_target = ofpbuf_try_pull(b, sizeof *nd_target);
257         if (!nd_target) {
258             return false;
259         }
260         flow->nd_target = *nd_target;
261
262         while (b->size >= 8) {
263             /* The minimum size of an option is 8 bytes, which also is
264              * the size of Ethernet link-layer options. */
265             const struct nd_opt_hdr *nd_opt = b->data;
266             int opt_len = nd_opt->nd_opt_len * 8;
267
268             if (!opt_len || opt_len > b->size) {
269                 goto invalid;
270             }
271
272             /* Store the link layer address if the appropriate option is
273              * provided.  It is considered an error if the same link
274              * layer option is specified twice. */
275             if (nd_opt->nd_opt_type == ND_OPT_SOURCE_LINKADDR
276                     && opt_len == 8) {
277                 if (eth_addr_is_zero(flow->arp_sha)) {
278                     memcpy(flow->arp_sha, nd_opt + 1, ETH_ADDR_LEN);
279                 } else {
280                     goto invalid;
281                 }
282             } else if (nd_opt->nd_opt_type == ND_OPT_TARGET_LINKADDR
283                     && opt_len == 8) {
284                 if (eth_addr_is_zero(flow->arp_tha)) {
285                     memcpy(flow->arp_tha, nd_opt + 1, ETH_ADDR_LEN);
286                 } else {
287                     goto invalid;
288                 }
289             }
290
291             if (!ofpbuf_try_pull(b, opt_len)) {
292                 goto invalid;
293             }
294         }
295     }
296
297     return true;
298
299 invalid:
300     memset(&flow->nd_target, 0, sizeof(flow->nd_target));
301     memset(flow->arp_sha, 0, sizeof(flow->arp_sha));
302     memset(flow->arp_tha, 0, sizeof(flow->arp_tha));
303
304     return false;
305
306 }
307
308 /* Initializes 'flow' members from 'packet', 'tun_id', and 'ofp_in_port'.
309  * Initializes 'packet' header pointers as follows:
310  *
311  *    - packet->l2 to the start of the Ethernet header.
312  *
313  *    - packet->l3 to just past the Ethernet header, or just past the
314  *      vlan_header if one is present, to the first byte of the payload of the
315  *      Ethernet frame.
316  *
317  *    - packet->l4 to just past the IPv4 header, if one is present and has a
318  *      correct length, and otherwise NULL.
319  *
320  *    - packet->l7 to just past the TCP or UDP or ICMP header, if one is
321  *      present and has a correct length, and otherwise NULL.
322  */
323 int
324 flow_extract(struct ofpbuf *packet, ovs_be64 tun_id, uint16_t ofp_in_port,
325              struct flow *flow)
326 {
327     struct ofpbuf b = *packet;
328     struct eth_header *eth;
329     int retval = 0;
330
331     COVERAGE_INC(flow_extract);
332
333     memset(flow, 0, sizeof *flow);
334     flow->tun_id = tun_id;
335     flow->in_port = ofp_in_port;
336
337     packet->l2 = b.data;
338     packet->l3 = NULL;
339     packet->l4 = NULL;
340     packet->l7 = NULL;
341
342     if (b.size < sizeof *eth) {
343         return 0;
344     }
345
346     /* Link layer. */
347     eth = b.data;
348     memcpy(flow->dl_src, eth->eth_src, ETH_ADDR_LEN);
349     memcpy(flow->dl_dst, eth->eth_dst, ETH_ADDR_LEN);
350
351     /* dl_type, vlan_tci. */
352     ofpbuf_pull(&b, ETH_ADDR_LEN * 2);
353     if (eth->eth_type == htons(ETH_TYPE_VLAN)) {
354         parse_vlan(&b, flow);
355     }
356     flow->dl_type = parse_ethertype(&b);
357
358     /* Network layer. */
359     packet->l3 = b.data;
360     if (flow->dl_type == htons(ETH_TYPE_IP)) {
361         const struct ip_header *nh = pull_ip(&b);
362         if (nh) {
363             flow->nw_src = get_unaligned_be32(&nh->ip_src);
364             flow->nw_dst = get_unaligned_be32(&nh->ip_dst);
365             flow->nw_tos = nh->ip_tos & IP_DSCP_MASK;
366             flow->nw_proto = nh->ip_proto;
367             packet->l4 = b.data;
368             if (!IP_IS_FRAGMENT(nh->ip_frag_off)) {
369                 if (flow->nw_proto == IPPROTO_TCP) {
370                     parse_tcp(packet, &b, flow);
371                 } else if (flow->nw_proto == IPPROTO_UDP) {
372                     parse_udp(packet, &b, flow);
373                 } else if (flow->nw_proto == IPPROTO_ICMP) {
374                     const struct icmp_header *icmp = pull_icmp(&b);
375                     if (icmp) {
376                         flow->icmp_type = htons(icmp->icmp_type);
377                         flow->icmp_code = htons(icmp->icmp_code);
378                         packet->l7 = b.data;
379                     }
380                 }
381             } else {
382                 retval = 1;
383             }
384         }
385     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
386
387         retval = parse_ipv6(&b, flow);
388         if (retval) {
389             return 0;
390         }
391
392         packet->l4 = b.data;
393         if (flow->nw_proto == IPPROTO_TCP) {
394             parse_tcp(packet, &b, flow);
395         } else if (flow->nw_proto == IPPROTO_UDP) {
396             parse_udp(packet, &b, flow);
397         } else if (flow->nw_proto == IPPROTO_ICMPV6) {
398             if (parse_icmpv6(&b, flow)) {
399                 packet->l7 = b.data;
400             }
401         }
402     } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
403         const struct arp_eth_header *arp = pull_arp(&b);
404         if (arp && arp->ar_hrd == htons(1)
405             && arp->ar_pro == htons(ETH_TYPE_IP)
406             && arp->ar_hln == ETH_ADDR_LEN
407             && arp->ar_pln == 4) {
408             /* We only match on the lower 8 bits of the opcode. */
409             if (ntohs(arp->ar_op) <= 0xff) {
410                 flow->nw_proto = ntohs(arp->ar_op);
411             }
412
413             if ((flow->nw_proto == ARP_OP_REQUEST)
414                 || (flow->nw_proto == ARP_OP_REPLY)) {
415                 flow->nw_src = arp->ar_spa;
416                 flow->nw_dst = arp->ar_tpa;
417                 memcpy(flow->arp_sha, arp->ar_sha, ETH_ADDR_LEN);
418                 memcpy(flow->arp_tha, arp->ar_tha, ETH_ADDR_LEN);
419             }
420         }
421     }
422
423     return retval;
424 }
425
426 /* For every bit of a field that is wildcarded in 'wildcards', sets the
427  * corresponding bit in 'flow' to zero. */
428 void
429 flow_zero_wildcards(struct flow *flow, const struct flow_wildcards *wildcards)
430 {
431     const flow_wildcards_t wc = wildcards->wildcards;
432     int i;
433
434     BUILD_ASSERT_DECL(FLOW_WC_SEQ == 1);
435
436     for (i = 0; i < FLOW_N_REGS; i++) {
437         flow->regs[i] &= wildcards->reg_masks[i];
438     }
439     flow->tun_id &= wildcards->tun_id_mask;
440     flow->nw_src &= wildcards->nw_src_mask;
441     flow->nw_dst &= wildcards->nw_dst_mask;
442     if (wc & FWW_IN_PORT) {
443         flow->in_port = 0;
444     }
445     flow->vlan_tci &= wildcards->vlan_tci_mask;
446     if (wc & FWW_DL_TYPE) {
447         flow->dl_type = 0;
448     }
449     if (wc & FWW_TP_SRC) {
450         flow->tp_src = 0;
451     }
452     if (wc & FWW_TP_DST) {
453         flow->tp_dst = 0;
454     }
455     if (wc & FWW_DL_SRC) {
456         memset(flow->dl_src, 0, sizeof flow->dl_src);
457     }
458     if (wc & FWW_DL_DST) {
459         flow->dl_dst[0] &= 0x01;
460         memset(&flow->dl_dst[1], 0, 5);
461     }
462     if (wc & FWW_ETH_MCAST) {
463         flow->dl_dst[0] &= 0xfe;
464     }
465     if (wc & FWW_NW_PROTO) {
466         flow->nw_proto = 0;
467     }
468     if (wc & FWW_NW_TOS) {
469         flow->nw_tos = 0;
470     }
471     if (wc & FWW_ARP_SHA) {
472         memset(flow->arp_sha, 0, sizeof flow->arp_sha);
473     }
474     if (wc & FWW_ARP_THA) {
475         memset(flow->arp_tha, 0, sizeof flow->arp_tha);
476     }
477     flow->ipv6_src = ipv6_addr_bitand(&flow->ipv6_src,
478             &wildcards->ipv6_src_mask);
479     flow->ipv6_dst = ipv6_addr_bitand(&flow->ipv6_dst,
480             &wildcards->ipv6_dst_mask);
481     if (wc & FWW_ND_TARGET) {
482         memset(&flow->nd_target, 0, sizeof flow->nd_target);
483     }
484 }
485
486 char *
487 flow_to_string(const struct flow *flow)
488 {
489     struct ds ds = DS_EMPTY_INITIALIZER;
490     flow_format(&ds, flow);
491     return ds_cstr(&ds);
492 }
493
494 void
495 flow_format(struct ds *ds, const struct flow *flow)
496 {
497     ds_put_format(ds, "tunnel%#"PRIx64":in_port%04"PRIx16":tci(",
498                   ntohll(flow->tun_id), flow->in_port);
499     if (flow->vlan_tci) {
500         ds_put_format(ds, "vlan%"PRIu16",pcp%d",
501                       vlan_tci_to_vid(flow->vlan_tci),
502                       vlan_tci_to_pcp(flow->vlan_tci));
503     } else {
504         ds_put_char(ds, '0');
505     }
506     ds_put_format(ds, ") mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT
507                       " type%04"PRIx16,
508                   ETH_ADDR_ARGS(flow->dl_src),
509                   ETH_ADDR_ARGS(flow->dl_dst),
510                   ntohs(flow->dl_type));
511
512     if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
513         ds_put_format(ds, " proto%"PRIu8" tos%"PRIu8" ipv6",
514                       flow->nw_proto, flow->nw_tos);
515         print_ipv6_addr(ds, &flow->ipv6_src);
516         ds_put_cstr(ds, "->");
517         print_ipv6_addr(ds, &flow->ipv6_dst);
518
519     } else {
520         ds_put_format(ds, " proto%"PRIu8
521                           " tos%"PRIu8
522                           " ip"IP_FMT"->"IP_FMT,
523                       flow->nw_proto,
524                       flow->nw_tos,
525                       IP_ARGS(&flow->nw_src),
526                       IP_ARGS(&flow->nw_dst));
527     }
528     if (flow->tp_src || flow->tp_dst) {
529         ds_put_format(ds, " port%"PRIu16"->%"PRIu16,
530                 ntohs(flow->tp_src), ntohs(flow->tp_dst));
531     }
532     if (!eth_addr_is_zero(flow->arp_sha) || !eth_addr_is_zero(flow->arp_tha)) {
533         ds_put_format(ds, " arp_ha"ETH_ADDR_FMT"->"ETH_ADDR_FMT,
534                 ETH_ADDR_ARGS(flow->arp_sha),
535                 ETH_ADDR_ARGS(flow->arp_tha));
536     }
537 }
538
539 void
540 flow_print(FILE *stream, const struct flow *flow)
541 {
542     char *s = flow_to_string(flow);
543     fputs(s, stream);
544     free(s);
545 }
546 \f
547 /* flow_wildcards functions. */
548
549 /* Initializes 'wc' as a set of wildcards that matches every packet. */
550 void
551 flow_wildcards_init_catchall(struct flow_wildcards *wc)
552 {
553     wc->wildcards = FWW_ALL;
554     wc->tun_id_mask = htonll(0);
555     wc->nw_src_mask = htonl(0);
556     wc->nw_dst_mask = htonl(0);
557     wc->ipv6_src_mask = in6addr_any;
558     wc->ipv6_dst_mask = in6addr_any;
559     memset(wc->reg_masks, 0, sizeof wc->reg_masks);
560     wc->vlan_tci_mask = htons(0);
561     wc->zero = 0;
562 }
563
564 /* Initializes 'wc' as an exact-match set of wildcards; that is, 'wc' does not
565  * wildcard any bits or fields. */
566 void
567 flow_wildcards_init_exact(struct flow_wildcards *wc)
568 {
569     wc->wildcards = 0;
570     wc->tun_id_mask = htonll(UINT64_MAX);
571     wc->nw_src_mask = htonl(UINT32_MAX);
572     wc->nw_dst_mask = htonl(UINT32_MAX);
573     wc->ipv6_src_mask = in6addr_exact;
574     wc->ipv6_dst_mask = in6addr_exact;
575     memset(wc->reg_masks, 0xff, sizeof wc->reg_masks);
576     wc->vlan_tci_mask = htons(UINT16_MAX);
577     wc->zero = 0;
578 }
579
580 /* Returns true if 'wc' is exact-match, false if 'wc' wildcards any bits or
581  * fields. */
582 bool
583 flow_wildcards_is_exact(const struct flow_wildcards *wc)
584 {
585     int i;
586
587     BUILD_ASSERT_DECL(FLOW_WC_SEQ == 1);
588
589     if (wc->wildcards
590         || wc->tun_id_mask != htonll(UINT64_MAX)
591         || wc->nw_src_mask != htonl(UINT32_MAX)
592         || wc->nw_dst_mask != htonl(UINT32_MAX)
593         || wc->vlan_tci_mask != htons(UINT16_MAX)
594         || !ipv6_mask_is_exact(&wc->ipv6_src_mask)
595         || !ipv6_mask_is_exact(&wc->ipv6_dst_mask)) {
596         return false;
597     }
598
599     for (i = 0; i < FLOW_N_REGS; i++) {
600         if (wc->reg_masks[i] != UINT32_MAX) {
601             return false;
602         }
603     }
604
605     return true;
606 }
607
608 /* Returns true if 'wc' matches every packet, false if 'wc' fixes any bits or
609  * fields. */
610 bool
611 flow_wildcards_is_catchall(const struct flow_wildcards *wc)
612 {
613     int i;
614
615     BUILD_ASSERT_DECL(FLOW_WC_SEQ == 1);
616
617     if (wc->wildcards != FWW_ALL
618         || wc->tun_id_mask != htonll(0)
619         || wc->nw_src_mask != htonl(0)
620         || wc->nw_dst_mask != htonl(0)
621         || wc->vlan_tci_mask != htons(0)
622         || !ipv6_mask_is_any(&wc->ipv6_src_mask)
623         || !ipv6_mask_is_any(&wc->ipv6_dst_mask)) {
624         return false;
625     }
626
627     for (i = 0; i < FLOW_N_REGS; i++) {
628         if (wc->reg_masks[i] != 0) {
629             return false;
630         }
631     }
632
633     return true;
634 }
635
636 /* Initializes 'dst' as the combination of wildcards in 'src1' and 'src2'.
637  * That is, a bit or a field is wildcarded in 'dst' if it is wildcarded in
638  * 'src1' or 'src2' or both.  */
639 void
640 flow_wildcards_combine(struct flow_wildcards *dst,
641                        const struct flow_wildcards *src1,
642                        const struct flow_wildcards *src2)
643 {
644     int i;
645
646     dst->wildcards = src1->wildcards | src2->wildcards;
647     dst->tun_id_mask = src1->tun_id_mask & src2->tun_id_mask;
648     dst->nw_src_mask = src1->nw_src_mask & src2->nw_src_mask;
649     dst->nw_dst_mask = src1->nw_dst_mask & src2->nw_dst_mask;
650     dst->ipv6_src_mask = ipv6_addr_bitand(&src1->ipv6_src_mask,
651                                         &src2->ipv6_src_mask);
652     dst->ipv6_dst_mask = ipv6_addr_bitand(&src1->ipv6_dst_mask,
653                                         &src2->ipv6_dst_mask);
654     for (i = 0; i < FLOW_N_REGS; i++) {
655         dst->reg_masks[i] = src1->reg_masks[i] & src2->reg_masks[i];
656     }
657     dst->vlan_tci_mask = src1->vlan_tci_mask & src2->vlan_tci_mask;
658 }
659
660 /* Returns a hash of the wildcards in 'wc'. */
661 uint32_t
662 flow_wildcards_hash(const struct flow_wildcards *wc, uint32_t basis)
663 {
664     /* If you change struct flow_wildcards and thereby trigger this
665      * assertion, please check that the new struct flow_wildcards has no holes
666      * in it before you update the assertion. */
667     BUILD_ASSERT_DECL(sizeof *wc == 56 + FLOW_N_REGS * 4);
668     return hash_bytes(wc, sizeof *wc, basis);
669 }
670
671 /* Returns true if 'a' and 'b' represent the same wildcards, false if they are
672  * different. */
673 bool
674 flow_wildcards_equal(const struct flow_wildcards *a,
675                      const struct flow_wildcards *b)
676 {
677     int i;
678
679     if (a->wildcards != b->wildcards
680         || a->tun_id_mask != b->tun_id_mask
681         || a->nw_src_mask != b->nw_src_mask
682         || a->nw_dst_mask != b->nw_dst_mask
683         || a->vlan_tci_mask != b->vlan_tci_mask
684         || !ipv6_addr_equals(&a->ipv6_src_mask, &b->ipv6_src_mask)
685         || !ipv6_addr_equals(&a->ipv6_dst_mask, &b->ipv6_dst_mask)) {
686         return false;
687     }
688
689     for (i = 0; i < FLOW_N_REGS; i++) {
690         if (a->reg_masks[i] != b->reg_masks[i]) {
691             return false;
692         }
693     }
694
695     return true;
696 }
697
698 /* Returns true if at least one bit or field is wildcarded in 'a' but not in
699  * 'b', false otherwise. */
700 bool
701 flow_wildcards_has_extra(const struct flow_wildcards *a,
702                          const struct flow_wildcards *b)
703 {
704     int i;
705     struct in6_addr ipv6_masked;
706
707     for (i = 0; i < FLOW_N_REGS; i++) {
708         if ((a->reg_masks[i] & b->reg_masks[i]) != b->reg_masks[i]) {
709             return true;
710         }
711     }
712
713     ipv6_masked = ipv6_addr_bitand(&a->ipv6_src_mask, &b->ipv6_src_mask);
714     if (!ipv6_addr_equals(&ipv6_masked, &b->ipv6_src_mask)) {
715         return true;
716     }
717
718     ipv6_masked = ipv6_addr_bitand(&a->ipv6_dst_mask, &b->ipv6_dst_mask);
719     if (!ipv6_addr_equals(&ipv6_masked, &b->ipv6_dst_mask)) {
720         return true;
721     }
722
723     return (a->wildcards & ~b->wildcards
724             || (a->tun_id_mask & b->tun_id_mask) != b->tun_id_mask
725             || (a->nw_src_mask & b->nw_src_mask) != b->nw_src_mask
726             || (a->nw_dst_mask & b->nw_dst_mask) != b->nw_dst_mask
727             || (a->vlan_tci_mask & b->vlan_tci_mask) != b->vlan_tci_mask);
728 }
729
730 static bool
731 set_nw_mask(ovs_be32 *maskp, ovs_be32 mask)
732 {
733     if (ip_is_cidr(mask)) {
734         *maskp = mask;
735         return true;
736     } else {
737         return false;
738     }
739 }
740
741 /* Sets the IP (or ARP) source wildcard mask to CIDR 'mask' (consisting of N
742  * high-order 1-bit and 32-N low-order 0-bits).  Returns true if successful,
743  * false if 'mask' is not a CIDR mask.  */
744 bool
745 flow_wildcards_set_nw_src_mask(struct flow_wildcards *wc, ovs_be32 mask)
746 {
747     return set_nw_mask(&wc->nw_src_mask, mask);
748 }
749
750 /* Sets the IP (or ARP) destination wildcard mask to CIDR 'mask' (consisting of
751  * N high-order 1-bit and 32-N low-order 0-bits).  Returns true if successful,
752  * false if 'mask' is not a CIDR mask.  */
753 bool
754 flow_wildcards_set_nw_dst_mask(struct flow_wildcards *wc, ovs_be32 mask)
755 {
756     return set_nw_mask(&wc->nw_dst_mask, mask);
757 }
758
759 static bool
760 set_ipv6_mask(struct in6_addr *maskp, const struct in6_addr *mask)
761 {
762     if (ipv6_is_cidr(mask)) {
763         *maskp = *mask;
764         return true;
765     } else {
766         return false;
767     }
768 }
769
770 /* Sets the IPv6 source wildcard mask to CIDR 'mask' (consisting of N
771  * high-order 1-bit and 128-N low-order 0-bits).  Returns true if successful,
772  * false if 'mask' is not a CIDR mask.  */
773 bool
774 flow_wildcards_set_ipv6_src_mask(struct flow_wildcards *wc,
775                                  const struct in6_addr *mask)
776 {
777     return set_ipv6_mask(&wc->ipv6_src_mask, mask);
778 }
779
780 /* Sets the IPv6 destination wildcard mask to CIDR 'mask' (consisting of
781  * N high-order 1-bit and 128-N low-order 0-bits).  Returns true if
782  * successful, false if 'mask' is not a CIDR mask.  */
783 bool
784 flow_wildcards_set_ipv6_dst_mask(struct flow_wildcards *wc,
785                                  const struct in6_addr *mask)
786 {
787     return set_ipv6_mask(&wc->ipv6_dst_mask, mask);
788 }
789
790 /* Sets the wildcard mask for register 'idx' in 'wc' to 'mask'.
791  * (A 0-bit indicates a wildcard bit.) */
792 void
793 flow_wildcards_set_reg_mask(struct flow_wildcards *wc, int idx, uint32_t mask)
794 {
795     wc->reg_masks[idx] = mask;
796 }
797
798 /* Returns the wildcard bitmask for the Ethernet destination address
799  * that 'wc' specifies.  The bitmask has a 0 in each bit that is wildcarded
800  * and a 1 in each bit that must match.  */
801 const uint8_t *
802 flow_wildcards_to_dl_dst_mask(flow_wildcards_t wc)
803 {
804     static const uint8_t    no_wild[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
805     static const uint8_t  addr_wild[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
806     static const uint8_t mcast_wild[] = {0xfe, 0xff, 0xff, 0xff, 0xff, 0xff};
807     static const uint8_t   all_wild[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
808
809     switch (wc & (FWW_DL_DST | FWW_ETH_MCAST)) {
810     case 0:                             return no_wild;
811     case FWW_DL_DST:                    return addr_wild;
812     case FWW_ETH_MCAST:                 return mcast_wild;
813     case FWW_DL_DST | FWW_ETH_MCAST:    return all_wild;
814     }
815     NOT_REACHED();
816 }
817
818 /* Returns true if 'mask' is a valid wildcard bitmask for the Ethernet
819  * destination address.  Valid bitmasks are either all-bits-0 or all-bits-1,
820  * except that the multicast bit may differ from the rest of the bits.  So,
821  * there are four possible valid bitmasks:
822  *
823  *  - 00:00:00:00:00:00
824  *  - 01:00:00:00:00:00
825  *  - fe:ff:ff:ff:ff:ff
826  *  - ff:ff:ff:ff:ff:ff
827  *
828  * All other bitmasks are invalid. */
829 bool
830 flow_wildcards_is_dl_dst_mask_valid(const uint8_t mask[ETH_ADDR_LEN])
831 {
832     switch (mask[0]) {
833     case 0x00:
834     case 0x01:
835         return (mask[1] | mask[2] | mask[3] | mask[4] | mask[5]) == 0x00;
836
837     case 0xfe:
838     case 0xff:
839         return (mask[1] & mask[2] & mask[3] & mask[4] & mask[5]) == 0xff;
840
841     default:
842         return false;
843     }
844 }
845
846 /* Returns 'wc' with the FWW_DL_DST and FWW_ETH_MCAST bits modified
847  * appropriately to match 'mask'.
848  *
849  * This function will assert-fail if 'mask' is invalid.  Only 'mask' values
850  * accepted by flow_wildcards_is_dl_dst_mask_valid() are allowed. */
851 flow_wildcards_t
852 flow_wildcards_set_dl_dst_mask(flow_wildcards_t wc,
853                                const uint8_t mask[ETH_ADDR_LEN])
854 {
855     assert(flow_wildcards_is_dl_dst_mask_valid(mask));
856
857     switch (mask[0]) {
858     case 0x00:
859         return wc | FWW_DL_DST | FWW_ETH_MCAST;
860
861     case 0x01:
862         return (wc | FWW_DL_DST) & ~FWW_ETH_MCAST;
863
864     case 0xfe:
865         return (wc & ~FWW_DL_DST) | FWW_ETH_MCAST;
866
867     case 0xff:
868         return wc & ~(FWW_DL_DST | FWW_ETH_MCAST);
869
870     default:
871         NOT_REACHED();
872     }
873 }
874
875 /* Hashes 'flow' based on its L2 through L4 protocol information. */
876 uint32_t
877 flow_hash_symmetric_l4(const struct flow *flow, uint32_t basis)
878 {
879     struct {
880         union {
881             ovs_be32 ipv4_addr;
882             struct in6_addr ipv6_addr;
883         };
884         ovs_be16 eth_type;
885         ovs_be16 vlan_tci;
886         ovs_be16 tp_addr;
887         uint8_t eth_addr[ETH_ADDR_LEN];
888         uint8_t ip_proto;
889     } fields;
890
891     int i;
892
893     memset(&fields, 0, sizeof fields);
894     for (i = 0; i < ETH_ADDR_LEN; i++) {
895         fields.eth_addr[i] = flow->dl_src[i] ^ flow->dl_dst[i];
896     }
897     fields.vlan_tci = flow->vlan_tci & htons(VLAN_VID_MASK);
898     fields.eth_type = flow->dl_type;
899
900     /* UDP source and destination port are not taken into account because they
901      * will not necessarily be symmetric in a bidirectional flow. */
902     if (fields.eth_type == htons(ETH_TYPE_IP)) {
903         fields.ipv4_addr = flow->nw_src ^ flow->nw_dst;
904         fields.ip_proto = flow->nw_proto;
905         if (fields.ip_proto == IPPROTO_TCP) {
906             fields.tp_addr = flow->tp_src ^ flow->tp_dst;
907         }
908     } else if (fields.eth_type == htons(ETH_TYPE_IPV6)) {
909         const uint8_t *a = &flow->ipv6_src.s6_addr[0];
910         const uint8_t *b = &flow->ipv6_dst.s6_addr[0];
911         uint8_t *ipv6_addr = &fields.ipv6_addr.s6_addr[0];
912
913         for (i=0; i<16; i++) {
914             ipv6_addr[i] = a[i] ^ b[i];
915         }
916         fields.ip_proto = flow->nw_proto;
917         if (fields.ip_proto == IPPROTO_TCP) {
918             fields.tp_addr = flow->tp_src ^ flow->tp_dst;
919         }
920     }
921     return hash_bytes(&fields, sizeof fields, basis);
922 }
923
924 /* Hashes the portions of 'flow' designated by 'fields'. */
925 uint32_t
926 flow_hash_fields(const struct flow *flow, enum nx_hash_fields fields,
927                  uint16_t basis)
928 {
929     switch (fields) {
930
931     case NX_HASH_FIELDS_ETH_SRC:
932         return hash_bytes(flow->dl_src, sizeof flow->dl_src, basis);
933
934     case NX_HASH_FIELDS_SYMMETRIC_L4:
935         return flow_hash_symmetric_l4(flow, basis);
936     }
937
938     NOT_REACHED();
939 }
940
941 /* Returns a string representation of 'fields'. */
942 const char *
943 flow_hash_fields_to_str(enum nx_hash_fields fields)
944 {
945     switch (fields) {
946     case NX_HASH_FIELDS_ETH_SRC: return "eth_src";
947     case NX_HASH_FIELDS_SYMMETRIC_L4: return "symmetric_l4";
948     default: return "<unknown>";
949     }
950 }
951
952 /* Returns true if the value of 'fields' is supported. Otherwise false. */
953 bool
954 flow_hash_fields_valid(enum nx_hash_fields fields)
955 {
956     return fields == NX_HASH_FIELDS_ETH_SRC
957         || fields == NX_HASH_FIELDS_SYMMETRIC_L4;
958 }
959
960 /* Puts into 'b' a packet that flow_extract() would parse as having the given
961  * 'flow'.
962  *
963  * (This is useful only for testing, obviously, and the packet isn't really
964  * valid.  It hasn't got any checksums filled in, for one, and lots of fields
965  * are just zeroed.) */
966 void
967 flow_compose(struct ofpbuf *b, const struct flow *flow)
968 {
969     eth_compose(b, flow->dl_dst, flow->dl_src, ntohs(flow->dl_type), 0);
970     if (flow->dl_type == htons(FLOW_DL_TYPE_NONE)) {
971         struct eth_header *eth = b->l2;
972         eth->eth_type = htons(b->size);
973         return;
974     }
975
976     if (flow->vlan_tci & htons(VLAN_CFI)) {
977         eth_push_vlan(b, flow->vlan_tci & ~htons(VLAN_CFI));
978     }
979
980     if (flow->dl_type == htons(ETH_TYPE_IP)) {
981         struct ip_header *ip;
982
983         b->l3 = ip = ofpbuf_put_zeros(b, sizeof *ip);
984         ip->ip_ihl_ver = IP_IHL_VER(5, 4);
985         ip->ip_tos = flow->nw_tos;
986         ip->ip_proto = flow->nw_proto;
987         ip->ip_src = flow->nw_src;
988         ip->ip_dst = flow->nw_dst;
989
990         if (flow->nw_proto == IPPROTO_TCP) {
991             struct tcp_header *tcp;
992
993             b->l4 = tcp = ofpbuf_put_zeros(b, sizeof *tcp);
994             tcp->tcp_src = flow->tp_src;
995             tcp->tcp_dst = flow->tp_dst;
996         } else if (flow->nw_proto == IPPROTO_UDP) {
997             struct udp_header *udp;
998
999             b->l4 = udp = ofpbuf_put_zeros(b, sizeof *udp);
1000             udp->udp_src = flow->tp_src;
1001             udp->udp_dst = flow->tp_dst;
1002         } else if (flow->nw_proto == IPPROTO_ICMP) {
1003             struct icmp_header *icmp;
1004
1005             b->l4 = icmp = ofpbuf_put_zeros(b, sizeof *icmp);
1006             icmp->icmp_type = ntohs(flow->tp_src);
1007             icmp->icmp_code = ntohs(flow->tp_dst);
1008         }
1009     } else if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
1010         /* XXX */
1011     } else if (flow->dl_type == htons(ETH_TYPE_ARP)) {
1012         struct arp_eth_header *arp;
1013
1014         b->l3 = arp = ofpbuf_put_zeros(b, sizeof *arp);
1015         arp->ar_hrd = htons(1);
1016         arp->ar_pro = htons(ETH_TYPE_IP);
1017         arp->ar_hln = ETH_ADDR_LEN;
1018         arp->ar_pln = 4;
1019         arp->ar_op = htons(flow->nw_proto);
1020
1021         if (flow->nw_proto == ARP_OP_REQUEST ||
1022             flow->nw_proto == ARP_OP_REPLY) {
1023             arp->ar_spa = flow->nw_src;
1024             arp->ar_tpa = flow->nw_dst;
1025             memcpy(arp->ar_sha, flow->arp_sha, ETH_ADDR_LEN);
1026             memcpy(arp->ar_tha, flow->arp_tha, ETH_ADDR_LEN);
1027         }
1028     }
1029 }