/* 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 == 8); if (wc->wildcards != FWW_ALL || wc->tun_id_mask != htonll(0) || wc->nw_src_mask != htonl(0) || wc->nw_dst_mask != htonl(0) || wc->tp_src_mask != htons(0) || wc->tp_dst_mask != htons(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; } } return true; }
/* Returns true if 'a' and 'b' represent the same wildcards, false if they are * different. */ bool flow_wildcards_equal(const struct flow_wildcards *a, const struct flow_wildcards *b) { int i; BUILD_ASSERT_DECL(FLOW_WC_SEQ == 8); if (a->wildcards != b->wildcards || a->tun_id_mask != b->tun_id_mask || a->nw_src_mask != b->nw_src_mask || a->nw_dst_mask != b->nw_dst_mask || a->vlan_tci_mask != b->vlan_tci_mask || !ipv6_addr_equals(&a->ipv6_src_mask, &b->ipv6_src_mask) || !ipv6_addr_equals(&a->ipv6_dst_mask, &b->ipv6_dst_mask) || a->tp_src_mask != b->tp_src_mask || a->tp_dst_mask != b->tp_dst_mask) { return false; } for (i = 0; i < FLOW_N_REGS; i++) { if (a->reg_masks[i] != b->reg_masks[i]) { return false; } } return true; }
/* Initializes 'dst' as the combination of wildcards in 'src1' and 'src2'. * That is, a bit or a field is wildcarded in 'dst' if it is wildcarded in * 'src1' or 'src2' or both. */ void flow_wildcards_combine(struct flow_wildcards *dst, const struct flow_wildcards *src1, const struct flow_wildcards *src2) { int i; BUILD_ASSERT_DECL(FLOW_WC_SEQ == 14); dst->wildcards = src1->wildcards | src2->wildcards; dst->tun_id_mask = src1->tun_id_mask & src2->tun_id_mask; dst->nw_src_mask = src1->nw_src_mask & src2->nw_src_mask; dst->nw_dst_mask = src1->nw_dst_mask & src2->nw_dst_mask; dst->ipv6_src_mask = ipv6_addr_bitand(&src1->ipv6_src_mask, &src2->ipv6_src_mask); dst->ipv6_dst_mask = ipv6_addr_bitand(&src1->ipv6_dst_mask, &src2->ipv6_dst_mask); dst->ipv6_label_mask = src1->ipv6_label_mask & src2->ipv6_label_mask; dst->nd_target_mask = ipv6_addr_bitand(&src1->nd_target_mask, &src2->nd_target_mask); for (i = 0; i < FLOW_N_REGS; i++) { dst->reg_masks[i] = src1->reg_masks[i] & src2->reg_masks[i]; } dst->metadata_mask = src1->metadata_mask & src2->metadata_mask; dst->vlan_tci_mask = src1->vlan_tci_mask & src2->vlan_tci_mask; dst->tp_src_mask = src1->tp_src_mask & src2->tp_src_mask; dst->tp_dst_mask = src1->tp_dst_mask & src2->tp_dst_mask; dst->nw_frag_mask = src1->nw_frag_mask & src2->nw_frag_mask; eth_addr_bitand(src1->dl_src_mask, src2->dl_src_mask, dst->dl_src_mask); eth_addr_bitand(src1->dl_dst_mask, src2->dl_dst_mask, dst->dl_dst_mask); eth_addr_bitand(src1->arp_sha_mask, src2->arp_sha_mask, dst->arp_sha_mask); eth_addr_bitand(src1->arp_tha_mask, src2->arp_tha_mask, dst->arp_tha_mask); }
/* Returns true if at least one bit or field is wildcarded in 'a' but not in * 'b', false otherwise. */ bool flow_wildcards_has_extra(const struct flow_wildcards *a, const struct flow_wildcards *b) { int i; struct in6_addr ipv6_masked; BUILD_ASSERT_DECL(FLOW_WC_SEQ == 8); for (i = 0; i < FLOW_N_REGS; i++) { if ((a->reg_masks[i] & b->reg_masks[i]) != b->reg_masks[i]) { return true; } } ipv6_masked = ipv6_addr_bitand(&a->ipv6_src_mask, &b->ipv6_src_mask); if (!ipv6_addr_equals(&ipv6_masked, &b->ipv6_src_mask)) { return true; } ipv6_masked = ipv6_addr_bitand(&a->ipv6_dst_mask, &b->ipv6_dst_mask); if (!ipv6_addr_equals(&ipv6_masked, &b->ipv6_dst_mask)) { return true; } return (a->wildcards & ~b->wildcards || (a->tun_id_mask & b->tun_id_mask) != b->tun_id_mask || (a->nw_src_mask & b->nw_src_mask) != b->nw_src_mask || (a->nw_dst_mask & b->nw_dst_mask) != b->nw_dst_mask || (a->vlan_tci_mask & b->vlan_tci_mask) != b->vlan_tci_mask || (a->tp_src_mask & b->tp_src_mask) != b->tp_src_mask || (a->tp_dst_mask & b->tp_dst_mask) != b->tp_dst_mask); }
/* 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 == 14); 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; wc->ipv6_label_mask = htonl(UINT32_MAX); wc->nd_target_mask = in6addr_exact; memset(wc->reg_masks, 0xff, sizeof wc->reg_masks); wc->metadata_mask = htonll(UINT64_MAX); wc->vlan_tci_mask = htons(UINT16_MAX); wc->nw_frag_mask = UINT8_MAX; wc->tp_src_mask = htons(UINT16_MAX); wc->tp_dst_mask = htons(UINT16_MAX); memset(wc->dl_src_mask, 0xff, ETH_ADDR_LEN); memset(wc->dl_dst_mask, 0xff, ETH_ADDR_LEN); memset(wc->arp_sha_mask, 0xff, ETH_ADDR_LEN); memset(wc->arp_tha_mask, 0xff, ETH_ADDR_LEN); memset(wc->zeros, 0, sizeof wc->zeros); }
/* 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 == 14); if (wc->wildcards != FWW_ALL || wc->tun_id_mask != htonll(0) || wc->nw_src_mask != htonl(0) || wc->nw_dst_mask != htonl(0) || wc->tp_src_mask != htons(0) || wc->tp_dst_mask != htons(0) || wc->vlan_tci_mask != htons(0) || wc->metadata_mask != htonll(0) || !eth_addr_is_zero(wc->dl_src_mask) || !eth_addr_is_zero(wc->dl_dst_mask) || !eth_addr_is_zero(wc->arp_sha_mask) || !eth_addr_is_zero(wc->arp_tha_mask) || !ipv6_mask_is_any(&wc->ipv6_src_mask) || !ipv6_mask_is_any(&wc->ipv6_dst_mask) || wc->ipv6_label_mask != htonl(0) || !ipv6_mask_is_any(&wc->nd_target_mask) || wc->nw_frag_mask != 0) { return false; } for (i = 0; i < FLOW_N_REGS; i++) { if (wc->reg_masks[i] != 0) { return false; } } return true; }
/* 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 == 14); 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; wc->ipv6_label_mask = htonl(0); wc->nd_target_mask = in6addr_any; memset(wc->reg_masks, 0, sizeof wc->reg_masks); wc->metadata_mask = htonll(0); wc->vlan_tci_mask = htons(0); wc->nw_frag_mask = 0; wc->tp_src_mask = htons(0); wc->tp_dst_mask = htons(0); memset(wc->dl_src_mask, 0, ETH_ADDR_LEN); memset(wc->dl_dst_mask, 0, ETH_ADDR_LEN); memset(wc->arp_sha_mask, 0, ETH_ADDR_LEN); memset(wc->arp_tha_mask, 0, ETH_ADDR_LEN); memset(wc->zeros, 0, sizeof wc->zeros); }
/* Returns true if 'a' and 'b' represent the same wildcards, false if they are * different. */ bool flow_wildcards_equal(const struct flow_wildcards *a, const struct flow_wildcards *b) { int i; BUILD_ASSERT_DECL(FLOW_WC_SEQ == 14); if (a->wildcards != b->wildcards || a->tun_id_mask != b->tun_id_mask || a->nw_src_mask != b->nw_src_mask || a->nw_dst_mask != b->nw_dst_mask || a->vlan_tci_mask != b->vlan_tci_mask || a->metadata_mask != b->metadata_mask || !ipv6_addr_equals(&a->ipv6_src_mask, &b->ipv6_src_mask) || !ipv6_addr_equals(&a->ipv6_dst_mask, &b->ipv6_dst_mask) || a->ipv6_label_mask != b->ipv6_label_mask || !ipv6_addr_equals(&a->nd_target_mask, &b->nd_target_mask) || a->tp_src_mask != b->tp_src_mask || a->tp_dst_mask != b->tp_dst_mask || !eth_addr_equals(a->dl_src_mask, b->dl_src_mask) || !eth_addr_equals(a->dl_dst_mask, b->dl_dst_mask) || !eth_addr_equals(a->arp_sha_mask, b->arp_sha_mask) || !eth_addr_equals(a->arp_tha_mask, b->arp_tha_mask)) { return false; } for (i = 0; i < FLOW_N_REGS; i++) { if (a->reg_masks[i] != b->reg_masks[i]) { return false; } } return true; }
/* 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 == 8); 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->tp_src_mask != htons(UINT16_MAX) || wc->tp_dst_mask != htons(UINT16_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 a hash of the wildcards in 'wc'. */ uint32_t flow_wildcards_hash(const struct flow_wildcards *wc, uint32_t basis) { /* If you change struct flow_wildcards and thereby trigger this * assertion, please check that the new struct flow_wildcards has no holes * in it before you update the assertion. */ BUILD_ASSERT_DECL(sizeof *wc == 60 + FLOW_N_REGS * 4); return hash_bytes(wc, sizeof *wc, basis); }
uint32_t hash_double(double x, uint32_t basis) { uint32_t value[2]; BUILD_ASSERT_DECL(sizeof x == sizeof value); memcpy(value, &x, sizeof value); return hash_3words(value[0], value[1], basis); }
/* Initializes 'fmd' with the metadata found in 'flow'. */ void flow_get_metadata(const struct flow *flow, struct flow_metadata *fmd) { BUILD_ASSERT_DECL(FLOW_WC_SEQ == 19); fmd->tun_id = flow->tunnel.tun_id; fmd->metadata = flow->metadata; memcpy(fmd->regs, flow->regs, sizeof fmd->regs); fmd->in_port = flow->in_port; }
/* Initializes 'fmd' with the metadata found in 'flow'. */ void flow_get_metadata(const struct flow *flow, struct flow_metadata *fmd) { BUILD_ASSERT_DECL(FLOW_WC_SEQ == 20); fmd->tun_id = flow->tunnel.tun_id; fmd->tun_src = flow->tunnel.ip_src; fmd->tun_dst = flow->tunnel.ip_dst; fmd->metadata = flow->metadata; memcpy(fmd->regs, flow->regs, sizeof fmd->regs); fmd->pkt_mark = flow->pkt_mark; fmd->in_port = flow->in_port.ofp_port; }
/* Returns the number of fields that differ from 'a' to 'b'. */ static int opp_differs(const struct ofp_phy_port *a, const struct ofp_phy_port *b) { BUILD_ASSERT_DECL(sizeof *a == 48); /* Trips when we add or remove fields. */ return ((a->port_no != b->port_no) + (memcmp(a->hw_addr, b->hw_addr, sizeof a->hw_addr) != 0) + (memcmp(a->name, b->name, sizeof a->name) != 0) + (a->config != b->config) + (a->state != b->state) + (a->curr != b->curr) + (a->advertised != b->advertised) + (a->supported != b->supported) + (a->peer != b->peer)); }
/* For every bit of a field that is wildcarded in 'wildcards', sets the * corresponding bit in 'flow' to zero. */ void flow_zero_wildcards(struct flow *flow, const struct flow_wildcards *wildcards) { const flow_wildcards_t wc = wildcards->wildcards; int i; BUILD_ASSERT_DECL(FLOW_WC_SEQ == 14); for (i = 0; i < FLOW_N_REGS; i++) { flow->regs[i] &= wildcards->reg_masks[i]; } flow->tun_id &= wildcards->tun_id_mask; flow->metadata &= wildcards->metadata_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); } flow->tp_src &= wildcards->tp_src_mask; flow->tp_dst &= wildcards->tp_dst_mask; eth_addr_bitand(flow->dl_src, wildcards->dl_src_mask, flow->dl_src); eth_addr_bitand(flow->dl_dst, wildcards->dl_dst_mask, flow->dl_dst); if (wc & FWW_NW_PROTO) { flow->nw_proto = 0; } flow->ipv6_label &= wildcards->ipv6_label_mask; 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; eth_addr_bitand(flow->arp_sha, wildcards->arp_sha_mask, flow->arp_sha); eth_addr_bitand(flow->arp_tha, wildcards->arp_tha_mask, 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); flow->nd_target = ipv6_addr_bitand(&flow->nd_target, &wildcards->nd_target_mask); flow->skb_priority = 0; }
/* 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); }
/* Initializes 'fmd' with the metadata found in 'flow'. */ void flow_get_metadata(const struct flow *flow, struct flow_metadata *fmd) { BUILD_ASSERT_DECL(FLOW_WC_SEQ == 14); fmd->tun_id = flow->tun_id; fmd->tun_id_mask = htonll(UINT64_MAX); fmd->metadata = flow->metadata; fmd->metadata_mask = htonll(UINT64_MAX); memcpy(fmd->regs, flow->regs, sizeof fmd->regs); memset(fmd->reg_masks, 0xff, sizeof fmd->reg_masks); fmd->in_port = flow->in_port; }
/* Initializes 'dst' as the combination of wildcards in 'src1' and 'src2'. * That is, a bit or a field is wildcarded in 'dst' if it is wildcarded in * 'src1' or 'src2' or both. */ void flow_wildcards_combine(struct flow_wildcards *dst, const struct flow_wildcards *src1, const struct flow_wildcards *src2) { int i; BUILD_ASSERT_DECL(FLOW_WC_SEQ == 8); dst->wildcards = src1->wildcards | src2->wildcards; dst->tun_id_mask = src1->tun_id_mask & src2->tun_id_mask; dst->nw_src_mask = src1->nw_src_mask & src2->nw_src_mask; dst->nw_dst_mask = src1->nw_dst_mask & src2->nw_dst_mask; dst->ipv6_src_mask = ipv6_addr_bitand(&src1->ipv6_src_mask, &src2->ipv6_src_mask); dst->ipv6_dst_mask = ipv6_addr_bitand(&src1->ipv6_dst_mask, &src2->ipv6_dst_mask); for (i = 0; i < FLOW_N_REGS; i++) { dst->reg_masks[i] = src1->reg_masks[i] & src2->reg_masks[i]; } dst->vlan_tci_mask = src1->vlan_tci_mask & src2->vlan_tci_mask; dst->tp_src_mask = src1->tp_src_mask & src2->tp_src_mask; dst->tp_dst_mask = src1->tp_dst_mask & src2->tp_dst_mask; }
/* Appends a string representation of 'match' to 's'. If 'priority' is * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */ void match_format(const struct match *match, struct ds *s, unsigned int priority) { const struct flow_wildcards *wc = &match->wc; size_t start_len = s->length; const struct flow *f = &match->flow; bool skip_type = false; bool skip_proto = false; int i; BUILD_ASSERT_DECL(FLOW_WC_SEQ == 19); if (priority != OFP_DEFAULT_PRIORITY) { ds_put_format(s, "priority=%u,", priority); } if (wc->masks.skb_mark) { ds_put_format(s, "skb_mark=%#"PRIx32",", f->skb_mark); } if (wc->masks.skb_priority) { ds_put_format(s, "skb_priority=%#"PRIx32",", f->skb_priority); } if (wc->masks.dl_type) { skip_type = true; if (f->dl_type == htons(ETH_TYPE_IP)) { if (wc->masks.nw_proto) { skip_proto = true; if (f->nw_proto == IPPROTO_ICMP) { ds_put_cstr(s, "icmp,"); } else if (f->nw_proto == IPPROTO_TCP) { ds_put_cstr(s, "tcp,"); } else if (f->nw_proto == IPPROTO_UDP) { ds_put_cstr(s, "udp,"); } else { ds_put_cstr(s, "ip,"); skip_proto = false; } } else { ds_put_cstr(s, "ip,"); } } else if (f->dl_type == htons(ETH_TYPE_IPV6)) { if (wc->masks.nw_proto) { skip_proto = true; if (f->nw_proto == IPPROTO_ICMPV6) { ds_put_cstr(s, "icmp6,"); } else if (f->nw_proto == IPPROTO_TCP) { ds_put_cstr(s, "tcp6,"); } else if (f->nw_proto == IPPROTO_UDP) { ds_put_cstr(s, "udp6,"); } else { ds_put_cstr(s, "ipv6,"); skip_proto = false; } } else { ds_put_cstr(s, "ipv6,"); } } else if (f->dl_type == htons(ETH_TYPE_ARP)) { ds_put_cstr(s, "arp,"); } else if (f->dl_type == htons(ETH_TYPE_RARP)) { ds_put_cstr(s, "rarp,"); } else if (f->mpls_depth) { flow_format_mpls(f, s); } else { skip_type = false; } } for (i = 0; i < FLOW_N_REGS; i++) { switch (wc->masks.regs[i]) { case 0: break; case UINT32_MAX: ds_put_format(s, "reg%d=0x%"PRIx32",", i, f->regs[i]); break; default: ds_put_format(s, "reg%d=0x%"PRIx32"/0x%"PRIx32",", i, f->regs[i], wc->masks.regs[i]); break; } } format_flow_tunnel(s, match); switch (wc->masks.metadata) { case 0: break; case CONSTANT_HTONLL(UINT64_MAX): ds_put_format(s, "metadata=%#"PRIx64",", ntohll(f->metadata)); break; default: ds_put_format(s, "metadata=%#"PRIx64"/%#"PRIx64",", ntohll(f->metadata), ntohll(wc->masks.metadata)); break; } if (wc->masks.in_port) { ds_put_cstr(s, "in_port="); ofputil_format_port(f->in_port, s); ds_put_char(s, ','); } if (wc->masks.vlan_tci) { ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK); ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK); ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI); if (cfi && f->vlan_tci & htons(VLAN_CFI) && (!vid_mask || vid_mask == htons(VLAN_VID_MASK)) && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK)) && (vid_mask || pcp_mask)) { if (vid_mask) { ds_put_format(s, "dl_vlan=%"PRIu16",", vlan_tci_to_vid(f->vlan_tci)); } if (pcp_mask) { ds_put_format(s, "dl_vlan_pcp=%d,", vlan_tci_to_pcp(f->vlan_tci)); } } else if (wc->masks.vlan_tci == htons(0xffff)) { ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci)); } else { ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",", ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci)); } } format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src); format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst); if (!skip_type && wc->masks.dl_type) { ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type)); } if (f->dl_type == htons(ETH_TYPE_IPV6)) { format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src); format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst); if (wc->masks.ipv6_label) { if (wc->masks.ipv6_label == htonl(UINT32_MAX)) { ds_put_format(s, "ipv6_label=0x%05"PRIx32",", ntohl(f->ipv6_label)); } else { ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",", ntohl(f->ipv6_label), ntohl(wc->masks.ipv6_label)); } } } else if (f->dl_type == htons(ETH_TYPE_ARP) || f->dl_type == htons(ETH_TYPE_RARP)) { format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src); format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst); } else { format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src); format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst); } if (!skip_proto && wc->masks.nw_proto) { if (f->dl_type == htons(ETH_TYPE_ARP) || f->dl_type == htons(ETH_TYPE_RARP)) { ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto); } else { ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto); } } if (f->dl_type == htons(ETH_TYPE_ARP) || f->dl_type == htons(ETH_TYPE_RARP)) { format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha); format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha); } if (wc->masks.nw_tos & IP_DSCP_MASK) { ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK); } if (wc->masks.nw_tos & IP_ECN_MASK) { ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK); } if (wc->masks.nw_ttl) { ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl); } if (wc->masks.mpls_lse & htonl(MPLS_LABEL_MASK)) { ds_put_format(s, "mpls_label=%"PRIu32",", mpls_lse_to_label(f->mpls_lse)); } if (wc->masks.mpls_lse & htonl(MPLS_TC_MASK)) { ds_put_format(s, "mpls_tc=%"PRIu8",", mpls_lse_to_tc(f->mpls_lse)); } if (wc->masks.mpls_lse & htonl(MPLS_BOS_MASK)) { ds_put_format(s, "mpls_bos=%"PRIu8",", mpls_lse_to_bos(f->mpls_lse)); } switch (wc->masks.nw_frag) { case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER: ds_put_format(s, "nw_frag=%s,", f->nw_frag & FLOW_NW_FRAG_ANY ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first") : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no")); break; case FLOW_NW_FRAG_ANY: ds_put_format(s, "nw_frag=%s,", f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no"); break; case FLOW_NW_FRAG_LATER: ds_put_format(s, "nw_frag=%s,", f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later"); break; } if (f->dl_type == htons(ETH_TYPE_IP) && f->nw_proto == IPPROTO_ICMP) { format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src); format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst); } else if (f->dl_type == htons(ETH_TYPE_IPV6) && f->nw_proto == IPPROTO_ICMPV6) { format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src); format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst); format_ipv6_netmask(s, "nd_target", &f->nd_target, &wc->masks.nd_target); format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha); format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha); } else { format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src); format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst); } if (s->length > start_len && ds_last(s) == ',') { s->length--; } }
/* Appends a string representation of 'match' to 's'. If 'priority' is * different from OFP_DEFAULT_PRIORITY, includes it in 's'. */ void match_format(const struct match *match, struct ds *s, unsigned int priority) { const struct flow_wildcards *wc = &match->wc; size_t start_len = s->length; const struct flow *f = &match->flow; bool skip_type = false; bool skip_proto = false; int i; BUILD_ASSERT_DECL(FLOW_WC_SEQ == 26); if (priority != OFP_DEFAULT_PRIORITY) { ds_put_format(s, "priority=%u,", priority); } format_uint32_masked(s, "pkt_mark", f->pkt_mark, wc->masks.pkt_mark); if (wc->masks.recirc_id) { format_uint32_masked(s, "recirc_id", f->recirc_id, wc->masks.recirc_id); } if (f->dp_hash && wc->masks.dp_hash) { format_uint32_masked(s, "dp_hash", f->dp_hash, wc->masks.dp_hash); } if (wc->masks.skb_priority) { ds_put_format(s, "skb_priority=%#"PRIx32",", f->skb_priority); } if (wc->masks.dl_type) { skip_type = true; if (f->dl_type == htons(ETH_TYPE_IP)) { if (wc->masks.nw_proto) { skip_proto = true; if (f->nw_proto == IPPROTO_ICMP) { ds_put_cstr(s, "icmp,"); } else if (f->nw_proto == IPPROTO_TCP) { ds_put_cstr(s, "tcp,"); } else if (f->nw_proto == IPPROTO_UDP) { ds_put_cstr(s, "udp,"); } else if (f->nw_proto == IPPROTO_SCTP) { ds_put_cstr(s, "sctp,"); } else { ds_put_cstr(s, "ip,"); skip_proto = false; } } else { ds_put_cstr(s, "ip,"); } } else if (f->dl_type == htons(ETH_TYPE_IPV6)) { if (wc->masks.nw_proto) { skip_proto = true; if (f->nw_proto == IPPROTO_ICMPV6) { ds_put_cstr(s, "icmp6,"); } else if (f->nw_proto == IPPROTO_TCP) { ds_put_cstr(s, "tcp6,"); } else if (f->nw_proto == IPPROTO_UDP) { ds_put_cstr(s, "udp6,"); } else if (f->nw_proto == IPPROTO_SCTP) { ds_put_cstr(s, "sctp6,"); } else { ds_put_cstr(s, "ipv6,"); skip_proto = false; } } else { ds_put_cstr(s, "ipv6,"); } } else if (f->dl_type == htons(ETH_TYPE_ARP)) { ds_put_cstr(s, "arp,"); } else if (f->dl_type == htons(ETH_TYPE_RARP)) { ds_put_cstr(s, "rarp,"); } else if (f->dl_type == htons(ETH_TYPE_MPLS)) { ds_put_cstr(s, "mpls,"); } else if (f->dl_type == htons(ETH_TYPE_MPLS_MCAST)) { ds_put_cstr(s, "mplsm,"); } else { skip_type = false; } } for (i = 0; i < FLOW_N_REGS; i++) { #define REGNAME_LEN 20 char regname[REGNAME_LEN]; if (snprintf(regname, REGNAME_LEN, "reg%d", i) >= REGNAME_LEN) { strcpy(regname, "reg?"); } format_uint32_masked(s, regname, f->regs[i], wc->masks.regs[i]); } format_flow_tunnel(s, match); format_be64_masked(s, "metadata", f->metadata, wc->masks.metadata); if (wc->masks.in_port.ofp_port) { ds_put_cstr(s, "in_port="); ofputil_format_port(f->in_port.ofp_port, s); ds_put_char(s, ','); } if (wc->masks.vlan_tci) { ovs_be16 vid_mask = wc->masks.vlan_tci & htons(VLAN_VID_MASK); ovs_be16 pcp_mask = wc->masks.vlan_tci & htons(VLAN_PCP_MASK); ovs_be16 cfi = wc->masks.vlan_tci & htons(VLAN_CFI); if (cfi && f->vlan_tci & htons(VLAN_CFI) && (!vid_mask || vid_mask == htons(VLAN_VID_MASK)) && (!pcp_mask || pcp_mask == htons(VLAN_PCP_MASK)) && (vid_mask || pcp_mask)) { if (vid_mask) { ds_put_format(s, "dl_vlan=%"PRIu16",", vlan_tci_to_vid(f->vlan_tci)); } if (pcp_mask) { ds_put_format(s, "dl_vlan_pcp=%d,", vlan_tci_to_pcp(f->vlan_tci)); } } else if (wc->masks.vlan_tci == htons(0xffff)) { ds_put_format(s, "vlan_tci=0x%04"PRIx16",", ntohs(f->vlan_tci)); } else { ds_put_format(s, "vlan_tci=0x%04"PRIx16"/0x%04"PRIx16",", ntohs(f->vlan_tci), ntohs(wc->masks.vlan_tci)); } } format_eth_masked(s, "dl_src", f->dl_src, wc->masks.dl_src); format_eth_masked(s, "dl_dst", f->dl_dst, wc->masks.dl_dst); if (!skip_type && wc->masks.dl_type) { ds_put_format(s, "dl_type=0x%04"PRIx16",", ntohs(f->dl_type)); } if (f->dl_type == htons(ETH_TYPE_IPV6)) { format_ipv6_netmask(s, "ipv6_src", &f->ipv6_src, &wc->masks.ipv6_src); format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->masks.ipv6_dst); if (wc->masks.ipv6_label) { if (wc->masks.ipv6_label == OVS_BE32_MAX) { ds_put_format(s, "ipv6_label=0x%05"PRIx32",", ntohl(f->ipv6_label)); } else { ds_put_format(s, "ipv6_label=0x%05"PRIx32"/0x%05"PRIx32",", ntohl(f->ipv6_label), ntohl(wc->masks.ipv6_label)); } } } else if (f->dl_type == htons(ETH_TYPE_ARP) || f->dl_type == htons(ETH_TYPE_RARP)) { format_ip_netmask(s, "arp_spa", f->nw_src, wc->masks.nw_src); format_ip_netmask(s, "arp_tpa", f->nw_dst, wc->masks.nw_dst); } else { format_ip_netmask(s, "nw_src", f->nw_src, wc->masks.nw_src); format_ip_netmask(s, "nw_dst", f->nw_dst, wc->masks.nw_dst); } if (!skip_proto && wc->masks.nw_proto) { if (f->dl_type == htons(ETH_TYPE_ARP) || f->dl_type == htons(ETH_TYPE_RARP)) { ds_put_format(s, "arp_op=%"PRIu8",", f->nw_proto); } else { ds_put_format(s, "nw_proto=%"PRIu8",", f->nw_proto); } } if (f->dl_type == htons(ETH_TYPE_ARP) || f->dl_type == htons(ETH_TYPE_RARP)) { format_eth_masked(s, "arp_sha", f->arp_sha, wc->masks.arp_sha); format_eth_masked(s, "arp_tha", f->arp_tha, wc->masks.arp_tha); } if (wc->masks.nw_tos & IP_DSCP_MASK) { ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK); } if (wc->masks.nw_tos & IP_ECN_MASK) { ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK); } if (wc->masks.nw_ttl) { ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl); } if (wc->masks.mpls_lse[0] & htonl(MPLS_LABEL_MASK)) { ds_put_format(s, "mpls_label=%"PRIu32",", mpls_lse_to_label(f->mpls_lse[0])); } if (wc->masks.mpls_lse[0] & htonl(MPLS_TC_MASK)) { ds_put_format(s, "mpls_tc=%"PRIu8",", mpls_lse_to_tc(f->mpls_lse[0])); } if (wc->masks.mpls_lse[0] & htonl(MPLS_TTL_MASK)) { ds_put_format(s, "mpls_ttl=%"PRIu8",", mpls_lse_to_ttl(f->mpls_lse[0])); } if (wc->masks.mpls_lse[0] & htonl(MPLS_BOS_MASK)) { ds_put_format(s, "mpls_bos=%"PRIu8",", mpls_lse_to_bos(f->mpls_lse[0])); } format_be32_masked(s, "mpls_lse1", f->mpls_lse[1], wc->masks.mpls_lse[1]); format_be32_masked(s, "mpls_lse2", f->mpls_lse[2], wc->masks.mpls_lse[2]); switch (wc->masks.nw_frag) { case FLOW_NW_FRAG_ANY | FLOW_NW_FRAG_LATER: ds_put_format(s, "nw_frag=%s,", f->nw_frag & FLOW_NW_FRAG_ANY ? (f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "first") : (f->nw_frag & FLOW_NW_FRAG_LATER ? "<error>" : "no")); break; case FLOW_NW_FRAG_ANY: ds_put_format(s, "nw_frag=%s,", f->nw_frag & FLOW_NW_FRAG_ANY ? "yes" : "no"); break; case FLOW_NW_FRAG_LATER: ds_put_format(s, "nw_frag=%s,", f->nw_frag & FLOW_NW_FRAG_LATER ? "later" : "not_later"); break; } if (f->dl_type == htons(ETH_TYPE_IP) && f->nw_proto == IPPROTO_ICMP) { format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src); format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst); //add by ljx // format_be16_masked(s, "icmp_identify", *(ovs_be16*)(&f->ipv6_label), // *(ovs_be16*)&wc->masks.ipv6_label); format_be16_masked(s, "icmp_identify", f->ipv6_label, wc->masks.ipv6_label); } else if (f->dl_type == htons(ETH_TYPE_IPV6) && f->nw_proto == IPPROTO_ICMPV6) { format_be16_masked(s, "icmp_type", f->tp_src, wc->masks.tp_src); format_be16_masked(s, "icmp_code", f->tp_dst, wc->masks.tp_dst); format_ipv6_netmask(s, "nd_target", &f->nd_target, &wc->masks.nd_target); format_eth_masked(s, "nd_sll", f->arp_sha, wc->masks.arp_sha); format_eth_masked(s, "nd_tll", f->arp_tha, wc->masks.arp_tha); } else { format_be16_masked(s, "tp_src", f->tp_src, wc->masks.tp_src); format_be16_masked(s, "tp_dst", f->tp_dst, wc->masks.tp_dst); } if (is_ip_any(f) && f->nw_proto == IPPROTO_TCP && wc->masks.tcp_flags) { uint16_t mask = TCP_FLAGS(wc->masks.tcp_flags); if (mask == TCP_FLAGS(OVS_BE16_MAX)) { ds_put_format(s, "tcp_flags=0x%03"PRIx16",", ntohs(f->tcp_flags)); } else { format_flags_masked(s, "tcp_flags", packet_tcp_flag_to_string, ntohs(f->tcp_flags), mask); } } if (s->length > start_len && ds_last(s) == ',') { s->length--; } }
/* Checks that 'learn' (which must be at least 'sizeof *learn' bytes long) is a * valid action on 'flow'. */ enum ofperr learn_check(const struct nx_action_learn *learn, const struct flow *flow) { struct cls_rule rule; const void *p, *end; cls_rule_init_catchall(&rule, 0); if (learn->flags & ~htons(OFPFF_SEND_FLOW_REM) || learn->pad || learn->table_id == 0xff) { return OFPERR_OFPBAC_BAD_ARGUMENT; } end = (char *) learn + ntohs(learn->len); for (p = learn + 1; p != end; ) { uint16_t header = ntohs(get_be16(&p)); int n_bits = header & NX_LEARN_N_BITS_MASK; int src_type = header & NX_LEARN_SRC_MASK; int dst_type = header & NX_LEARN_DST_MASK; enum ofperr error; uint64_t value; if (!header) { break; } error = learn_check_header(header, (char *) end - (char *) p); if (error) { return error; } /* Check the source. */ if (src_type == NX_LEARN_SRC_FIELD) { struct mf_subfield src; get_subfield(n_bits, &p, &src); error = mf_check_src(&src, flow); if (error) { return error; } value = 0; } else { value = get_bits(n_bits, &p); } /* Check the destination. */ if (dst_type == NX_LEARN_DST_MATCH || dst_type == NX_LEARN_DST_LOAD) { struct mf_subfield dst; get_subfield(n_bits, &p, &dst); error = (dst_type == NX_LEARN_DST_LOAD ? mf_check_dst(&dst, &rule.flow) : mf_check_src(&dst, &rule.flow)); if (error) { return error; } if (dst_type == NX_LEARN_DST_MATCH && src_type == NX_LEARN_SRC_IMMEDIATE) { if (n_bits <= 64) { mf_set_subfield(&dst, value, &rule); } else { /* We're only setting subfields to allow us to check * prerequisites. No prerequisite depends on the value of * a field that is wider than 64 bits. So just skip * setting it entirely. */ BUILD_ASSERT_DECL(FLOW_WC_SEQ == 11); } } } } if (!is_all_zeros(p, (char *) end - (char *) p)) { return OFPERR_OFPBAC_BAD_ARGUMENT; } return 0; }
/* Returns true if at least one bit or field is wildcarded in 'a' but not in * 'b', false otherwise. */ bool flow_wildcards_has_extra(const struct flow_wildcards *a, const struct flow_wildcards *b) { int i; uint8_t eth_masked[ETH_ADDR_LEN]; struct in6_addr ipv6_masked; BUILD_ASSERT_DECL(FLOW_WC_SEQ == 14); for (i = 0; i < FLOW_N_REGS; i++) { if ((a->reg_masks[i] & b->reg_masks[i]) != b->reg_masks[i]) { return true; } } eth_addr_bitand(a->dl_src_mask, b->dl_src_mask, eth_masked); if (!eth_addr_equals(eth_masked, b->dl_src_mask)) { return true; } eth_addr_bitand(a->dl_dst_mask, b->dl_dst_mask, eth_masked); if (!eth_addr_equals(eth_masked, b->dl_dst_mask)) { return true; } eth_addr_bitand(a->arp_sha_mask, b->arp_sha_mask, eth_masked); if (!eth_addr_equals(eth_masked, b->arp_sha_mask)) { return true; } eth_addr_bitand(a->arp_tha_mask, b->arp_tha_mask, eth_masked); if (!eth_addr_equals(eth_masked, b->arp_tha_mask)) { return true; } ipv6_masked = ipv6_addr_bitand(&a->ipv6_src_mask, &b->ipv6_src_mask); if (!ipv6_addr_equals(&ipv6_masked, &b->ipv6_src_mask)) { return true; } ipv6_masked = ipv6_addr_bitand(&a->ipv6_dst_mask, &b->ipv6_dst_mask); if (!ipv6_addr_equals(&ipv6_masked, &b->ipv6_dst_mask)) { return true; } ipv6_masked = ipv6_addr_bitand(&a->nd_target_mask, &b->nd_target_mask); if (!ipv6_addr_equals(&ipv6_masked, &b->nd_target_mask)) { return true; } return (a->wildcards & ~b->wildcards || (a->tun_id_mask & b->tun_id_mask) != b->tun_id_mask || (a->nw_src_mask & b->nw_src_mask) != b->nw_src_mask || (a->nw_dst_mask & b->nw_dst_mask) != b->nw_dst_mask || (a->ipv6_label_mask & b->ipv6_label_mask) != b->ipv6_label_mask || (a->vlan_tci_mask & b->vlan_tci_mask) != b->vlan_tci_mask || (a->metadata_mask & b->metadata_mask) != b->metadata_mask || (a->tp_src_mask & b->tp_src_mask) != b->tp_src_mask || (a->tp_dst_mask & b->tp_dst_mask) != b->tp_dst_mask || (a->nw_frag_mask & b->nw_frag_mask) != b->nw_frag_mask); }
static uint32_t ipf_addr_hash_add(uint32_t hash, const union ipf_addr *addr) { BUILD_ASSERT_DECL(sizeof *addr % 4 == 0); return hash_add_bytes32(hash, (const uint32_t *) addr, sizeof *addr); }
/* For every bit of a field that is wildcarded in 'wildcards', sets the * corresponding bit in 'flow' to zero. */ void flow_zero_wildcards(struct flow *flow, const struct flow_wildcards *wildcards) { const flow_wildcards_t wc = wildcards->wildcards; int i; BUILD_ASSERT_DECL(FLOW_WC_SEQ == 8); 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); } flow->tp_src &= wildcards->tp_src_mask; flow->tp_dst &= wildcards->tp_dst_mask; 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->skb_priority = 0; }