+ for (i = 0; i < FLOW_N_REGS; i++) {
+ flow->regs[i] &= wildcards->reg_masks[i];
+ }
+ flow->tun_id &= wildcards->tun_id_mask;
+ flow->nw_src &= wildcards->nw_src_mask;
+ flow->nw_dst &= wildcards->nw_dst_mask;
+ if (wc & FWW_IN_PORT) {
+ flow->in_port = 0;
+ }
+ flow->vlan_tci &= wildcards->vlan_tci_mask;
+ if (wc & FWW_DL_TYPE) {
+ flow->dl_type = htons(0);
+ }
+ if (wc & FWW_TP_SRC) {
+ flow->tp_src = htons(0);
+ }
+ if (wc & FWW_TP_DST) {
+ flow->tp_dst = htons(0);
+ }
+ if (wc & FWW_DL_SRC) {
+ memset(flow->dl_src, 0, sizeof flow->dl_src);
+ }
+ if (wc & FWW_DL_DST) {
+ flow->dl_dst[0] &= 0x01;
+ memset(&flow->dl_dst[1], 0, 5);
+ }
+ if (wc & FWW_ETH_MCAST) {
+ flow->dl_dst[0] &= 0xfe;
+ }
+ if (wc & FWW_NW_PROTO) {
+ flow->nw_proto = 0;
+ }
+ if (wc & FWW_IPV6_LABEL) {
+ flow->ipv6_label = htonl(0);
+ }
+ if (wc & FWW_NW_DSCP) {
+ flow->nw_tos &= ~IP_DSCP_MASK;
+ }
+ if (wc & FWW_NW_ECN) {
+ flow->nw_tos &= ~IP_ECN_MASK;
+ }
+ if (wc & FWW_NW_TTL) {
+ flow->nw_ttl = 0;
+ }
+ flow->nw_frag &= wildcards->nw_frag_mask;
+ if (wc & FWW_ARP_SHA) {
+ memset(flow->arp_sha, 0, sizeof flow->arp_sha);
+ }
+ if (wc & FWW_ARP_THA) {
+ memset(flow->arp_tha, 0, sizeof flow->arp_tha);
+ }
+ flow->ipv6_src = ipv6_addr_bitand(&flow->ipv6_src,
+ &wildcards->ipv6_src_mask);
+ flow->ipv6_dst = ipv6_addr_bitand(&flow->ipv6_dst,
+ &wildcards->ipv6_dst_mask);
+ if (wc & FWW_ND_TARGET) {
+ memset(&flow->nd_target, 0, sizeof flow->nd_target);
+ }
+ flow->priority = 0;
+}
+
+char *
+flow_to_string(const struct flow *flow)
+{
+ struct ds ds = DS_EMPTY_INITIALIZER;
+ flow_format(&ds, flow);
+ return ds_cstr(&ds);
+}
+
+void
+flow_format(struct ds *ds, const struct flow *flow)
+{
+ ds_put_format(ds, "priority%"PRIu32
+ ":tunnel%#"PRIx64
+ ":in_port%04"PRIx16,
+ flow->priority,
+ ntohll(flow->tun_id),
+ flow->in_port);
+
+ ds_put_format(ds, ":tci(");
+ if (flow->vlan_tci) {
+ ds_put_format(ds, "vlan%"PRIu16",pcp%d",
+ vlan_tci_to_vid(flow->vlan_tci),
+ vlan_tci_to_pcp(flow->vlan_tci));
+ } else {
+ ds_put_char(ds, '0');
+ }
+ ds_put_format(ds, ") mac"ETH_ADDR_FMT"->"ETH_ADDR_FMT
+ " type%04"PRIx16,
+ ETH_ADDR_ARGS(flow->dl_src),
+ ETH_ADDR_ARGS(flow->dl_dst),
+ ntohs(flow->dl_type));
+
+ if (flow->dl_type == htons(ETH_TYPE_IPV6)) {
+ ds_put_format(ds, " label%#"PRIx32" proto%"PRIu8" tos%#"PRIx8
+ " ttl%"PRIu8" ipv6",
+ ntohl(flow->ipv6_label), flow->nw_proto,
+ flow->nw_tos, flow->nw_ttl);
+ print_ipv6_addr(ds, &flow->ipv6_src);
+ ds_put_cstr(ds, "->");
+ print_ipv6_addr(ds, &flow->ipv6_dst);
+
+ } else {
+ ds_put_format(ds, " proto%"PRIu8" tos%#"PRIx8" ttl%"PRIu8
+ " ip"IP_FMT"->"IP_FMT,
+ flow->nw_proto, flow->nw_tos, flow->nw_ttl,
+ IP_ARGS(&flow->nw_src), IP_ARGS(&flow->nw_dst));
+ }
+ if (flow->nw_frag) {
+ ds_put_format(ds, " frag(%s)",
+ flow->nw_frag == FLOW_NW_FRAG_ANY ? "first"
+ : flow->nw_frag == (FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER)
+ ? "later" : "<error>");
+ }
+ if (flow->tp_src || flow->tp_dst) {
+ ds_put_format(ds, " port%"PRIu16"->%"PRIu16,
+ ntohs(flow->tp_src), ntohs(flow->tp_dst));
+ }
+ if (!eth_addr_is_zero(flow->arp_sha) || !eth_addr_is_zero(flow->arp_tha)) {
+ ds_put_format(ds, " arp_ha"ETH_ADDR_FMT"->"ETH_ADDR_FMT,
+ ETH_ADDR_ARGS(flow->arp_sha),
+ ETH_ADDR_ARGS(flow->arp_tha));
+ }
+}
+
+void
+flow_print(FILE *stream, const struct flow *flow)
+{
+ char *s = flow_to_string(flow);
+ fputs(s, stream);
+ free(s);
+}
+\f
+/* flow_wildcards functions. */
+
+/* Initializes 'wc' as a set of wildcards that matches every packet. */
+void
+flow_wildcards_init_catchall(struct flow_wildcards *wc)
+{
+ BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7);
+
+ wc->wildcards = FWW_ALL;
+ wc->tun_id_mask = htonll(0);
+ wc->nw_src_mask = htonl(0);
+ wc->nw_dst_mask = htonl(0);
+ wc->ipv6_src_mask = in6addr_any;
+ wc->ipv6_dst_mask = in6addr_any;
+ memset(wc->reg_masks, 0, sizeof wc->reg_masks);
+ wc->vlan_tci_mask = htons(0);
+ wc->nw_frag_mask = 0;
+ memset(wc->zeros, 0, sizeof wc->zeros);
+}
+
+/* Initializes 'wc' as an exact-match set of wildcards; that is, 'wc' does not
+ * wildcard any bits or fields. */
+void
+flow_wildcards_init_exact(struct flow_wildcards *wc)
+{
+ BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7);
+
+ wc->wildcards = 0;
+ wc->tun_id_mask = htonll(UINT64_MAX);
+ wc->nw_src_mask = htonl(UINT32_MAX);
+ wc->nw_dst_mask = htonl(UINT32_MAX);
+ wc->ipv6_src_mask = in6addr_exact;
+ wc->ipv6_dst_mask = in6addr_exact;
+ memset(wc->reg_masks, 0xff, sizeof wc->reg_masks);
+ wc->vlan_tci_mask = htons(UINT16_MAX);
+ wc->nw_frag_mask = UINT8_MAX;
+ memset(wc->zeros, 0, sizeof wc->zeros);
+}
+
+/* Returns true if 'wc' is exact-match, false if 'wc' wildcards any bits or
+ * fields. */
+bool
+flow_wildcards_is_exact(const struct flow_wildcards *wc)
+{
+ int i;
+
+ BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7);
+
+ if (wc->wildcards
+ || wc->tun_id_mask != htonll(UINT64_MAX)
+ || wc->nw_src_mask != htonl(UINT32_MAX)
+ || wc->nw_dst_mask != htonl(UINT32_MAX)
+ || wc->vlan_tci_mask != htons(UINT16_MAX)
+ || !ipv6_mask_is_exact(&wc->ipv6_src_mask)
+ || !ipv6_mask_is_exact(&wc->ipv6_dst_mask)
+ || wc->nw_frag_mask != UINT8_MAX) {
+ return false;
+ }
+
+ for (i = 0; i < FLOW_N_REGS; i++) {
+ if (wc->reg_masks[i] != UINT32_MAX) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+/* Returns true if 'wc' matches every packet, false if 'wc' fixes any bits or
+ * fields. */
+bool
+flow_wildcards_is_catchall(const struct flow_wildcards *wc)
+{
+ int i;
+
+ BUILD_ASSERT_DECL(FLOW_WC_SEQ == 7);
+
+ if (wc->wildcards != FWW_ALL
+ || wc->tun_id_mask != htonll(0)
+ || wc->nw_src_mask != htonl(0)
+ || wc->nw_dst_mask != htonl(0)
+ || wc->vlan_tci_mask != htons(0)
+ || !ipv6_mask_is_any(&wc->ipv6_src_mask)
+ || !ipv6_mask_is_any(&wc->ipv6_dst_mask)
+ || wc->nw_frag_mask != 0) {
+ return false;
+ }
+
+ for (i = 0; i < FLOW_N_REGS; i++) {
+ if (wc->reg_masks[i] != 0) {
+ return false;