+ cls_rule_set_tp_dst(rule, htons(icmp_code));
+}
+
+void
+cls_rule_set_arp_sha(struct cls_rule *rule, const uint8_t sha[ETH_ADDR_LEN])
+{
+ cls_rule_set_eth(sha, rule->flow.arp_sha, rule->wc.arp_sha_mask);
+}
+
+void
+cls_rule_set_arp_sha_masked(struct cls_rule *rule,
+ const uint8_t arp_sha[ETH_ADDR_LEN],
+ const uint8_t mask[ETH_ADDR_LEN])
+{
+ cls_rule_set_eth_masked(arp_sha, mask,
+ rule->flow.arp_sha, rule->wc.arp_sha_mask);
+}
+
+void
+cls_rule_set_arp_tha(struct cls_rule *rule, const uint8_t tha[ETH_ADDR_LEN])
+{
+ cls_rule_set_eth(tha, rule->flow.arp_tha, rule->wc.arp_tha_mask);
+}
+
+void
+cls_rule_set_arp_tha_masked(struct cls_rule *rule,
+ const uint8_t arp_tha[ETH_ADDR_LEN],
+ const uint8_t mask[ETH_ADDR_LEN])
+{
+ cls_rule_set_eth_masked(arp_tha, mask,
+ rule->flow.arp_tha, rule->wc.arp_tha_mask);
+}
+
+void
+cls_rule_set_ipv6_src(struct cls_rule *rule, const struct in6_addr *src)
+{
+ rule->flow.ipv6_src = *src;
+ rule->wc.ipv6_src_mask = in6addr_exact;
+}
+
+void
+cls_rule_set_ipv6_src_masked(struct cls_rule *rule, const struct in6_addr *src,
+ const struct in6_addr *mask)
+{
+ rule->flow.ipv6_src = ipv6_addr_bitand(src, mask);
+ rule->wc.ipv6_src_mask = *mask;
+}
+
+void
+cls_rule_set_ipv6_dst(struct cls_rule *rule, const struct in6_addr *dst)
+{
+ rule->flow.ipv6_dst = *dst;
+ rule->wc.ipv6_dst_mask = in6addr_exact;
+}
+
+void
+cls_rule_set_ipv6_dst_masked(struct cls_rule *rule, const struct in6_addr *dst,
+ const struct in6_addr *mask)
+{
+ rule->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
+ rule->wc.ipv6_dst_mask = *mask;
+}
+
+void
+cls_rule_set_ipv6_label(struct cls_rule *rule, ovs_be32 ipv6_label)
+{
+ cls_rule_set_ipv6_label_masked(rule, ipv6_label, htonl(UINT32_MAX));
+}
+
+void
+cls_rule_set_ipv6_label_masked(struct cls_rule *rule, ovs_be32 ipv6_label,
+ ovs_be32 mask)
+{
+ rule->flow.ipv6_label = ipv6_label & mask;
+ rule->wc.ipv6_label_mask = mask;
+}
+
+void
+cls_rule_set_nd_target(struct cls_rule *rule, const struct in6_addr *target)
+{
+ rule->flow.nd_target = *target;
+ rule->wc.nd_target_mask = in6addr_exact;
+}
+
+void
+cls_rule_set_nd_target_masked(struct cls_rule *rule,
+ const struct in6_addr *target,
+ const struct in6_addr *mask)
+{
+ rule->flow.nd_target = ipv6_addr_bitand(target, mask);
+ rule->wc.nd_target_mask = *mask;
+}
+
+/* Returns true if 'a' and 'b' have the same priority, wildcard the same
+ * fields, and have the same values for fixed fields, otherwise false. */
+bool
+cls_rule_equal(const struct cls_rule *a, const struct cls_rule *b)
+{
+ return (a->priority == b->priority
+ && flow_wildcards_equal(&a->wc, &b->wc)
+ && flow_equal(&a->flow, &b->flow));
+}
+
+/* Returns a hash value for the flow, wildcards, and priority in 'rule',
+ * starting from 'basis'. */
+uint32_t
+cls_rule_hash(const struct cls_rule *rule, uint32_t basis)
+{
+ uint32_t h0 = flow_hash(&rule->flow, basis);
+ uint32_t h1 = flow_wildcards_hash(&rule->wc, h0);
+ return hash_int(rule->priority, h1);
+}
+
+static void
+format_eth_masked(struct ds *s, const char *name, const uint8_t eth[6],
+ const uint8_t mask[6])
+{
+ if (!eth_addr_is_zero(mask)) {
+ ds_put_format(s, "%s=", name);
+ eth_format_masked(eth, mask, s);
+ ds_put_char(s, ',');
+ }
+}
+
+static void
+format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
+ ovs_be32 netmask)
+{
+ if (netmask) {
+ ds_put_format(s, "%s=", name);
+ ip_format_masked(ip, netmask, s);
+ ds_put_char(s, ',');
+ }
+}
+
+static void
+format_ipv6_netmask(struct ds *s, const char *name,
+ const struct in6_addr *addr,
+ const struct in6_addr *netmask)
+{
+ if (!ipv6_mask_is_any(netmask)) {
+ ds_put_format(s, "%s=", name);
+ print_ipv6_masked(s, addr, netmask);
+ ds_put_char(s, ',');
+ }
+}
+
+
+static void
+format_be16_masked(struct ds *s, const char *name,
+ ovs_be16 value, ovs_be16 mask)
+{
+ if (mask != htons(0)) {
+ ds_put_format(s, "%s=", name);
+ if (mask == htons(UINT16_MAX)) {
+ ds_put_format(s, "%"PRIu16, ntohs(value));
+ } else {
+ ds_put_format(s, "0x%"PRIx16"/0x%"PRIx16,
+ ntohs(value), ntohs(mask));
+ }
+ ds_put_char(s, ',');
+ }
+}
+
+void
+cls_rule_format(const struct cls_rule *rule, struct ds *s)
+{
+ const struct flow_wildcards *wc = &rule->wc;
+ size_t start_len = s->length;
+ flow_wildcards_t w = wc->wildcards;
+ const struct flow *f = &rule->flow;
+ bool skip_type = false;
+ bool skip_proto = false;
+
+ int i;
+
+ BUILD_ASSERT_DECL(FLOW_WC_SEQ == 14);
+
+ if (rule->priority != OFP_DEFAULT_PRIORITY) {
+ ds_put_format(s, "priority=%d,", rule->priority);
+ }
+
+ if (!(w & FWW_DL_TYPE)) {
+ skip_type = true;
+ if (f->dl_type == htons(ETH_TYPE_IP)) {
+ if (!(w & FWW_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 (!(w & FWW_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 {
+ skip_type = false;
+ }
+ }
+ for (i = 0; i < FLOW_N_REGS; i++) {
+ switch (wc->reg_masks[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->reg_masks[i]);
+ break;
+ }
+ }
+ switch (wc->tun_id_mask) {
+ case 0:
+ break;
+ case CONSTANT_HTONLL(UINT64_MAX):
+ ds_put_format(s, "tun_id=%#"PRIx64",", ntohll(f->tun_id));
+ break;
+ default:
+ ds_put_format(s, "tun_id=%#"PRIx64"/%#"PRIx64",",
+ ntohll(f->tun_id), ntohll(wc->tun_id_mask));
+ break;
+ }
+ switch (wc->metadata_mask) {
+ 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->metadata_mask));
+ break;
+ }
+ if (!(w & FWW_IN_PORT)) {
+ ds_put_format(s, "in_port=%"PRIu16",", f->in_port);
+ }
+ if (wc->vlan_tci_mask) {
+ ovs_be16 vid_mask = wc->vlan_tci_mask & htons(VLAN_VID_MASK);
+ ovs_be16 pcp_mask = wc->vlan_tci_mask & htons(VLAN_PCP_MASK);
+ ovs_be16 cfi = wc->vlan_tci_mask & 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->vlan_tci_mask == 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->vlan_tci_mask));
+ }
+ }
+ format_eth_masked(s, "dl_src", f->dl_src, wc->dl_src_mask);
+ format_eth_masked(s, "dl_dst", f->dl_dst, wc->dl_dst_mask);
+ if (!skip_type && !(w & FWW_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->ipv6_src_mask);
+ format_ipv6_netmask(s, "ipv6_dst", &f->ipv6_dst, &wc->ipv6_dst_mask);
+ if (wc->ipv6_label_mask) {
+ if (wc->ipv6_label_mask == 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->ipv6_label_mask));
+ }
+ }
+ } else {
+ format_ip_netmask(s, "nw_src", f->nw_src, wc->nw_src_mask);
+ format_ip_netmask(s, "nw_dst", f->nw_dst, wc->nw_dst_mask);
+ }
+ if (!skip_proto && !(w & FWW_NW_PROTO)) {
+ if (f->dl_type == htons(ETH_TYPE_ARP)) {
+ 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)) {
+ format_eth_masked(s, "arp_sha", f->arp_sha, wc->arp_sha_mask);
+ format_eth_masked(s, "arp_tha", f->arp_tha, wc->arp_tha_mask);
+ }
+ if (!(w & FWW_NW_DSCP)) {
+ ds_put_format(s, "nw_tos=%"PRIu8",", f->nw_tos & IP_DSCP_MASK);
+ }
+ if (!(w & FWW_NW_ECN)) {
+ ds_put_format(s, "nw_ecn=%"PRIu8",", f->nw_tos & IP_ECN_MASK);
+ }
+ if (!(w & FWW_NW_TTL)) {
+ ds_put_format(s, "nw_ttl=%"PRIu8",", f->nw_ttl);
+ }
+ switch (wc->nw_frag_mask) {
+ 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->nw_proto == IPPROTO_ICMP) {
+ format_be16_masked(s, "icmp_type", f->tp_src, wc->tp_src_mask);
+ format_be16_masked(s, "icmp_code", f->tp_dst, wc->tp_dst_mask);
+ } else if (f->nw_proto == IPPROTO_ICMPV6) {
+ format_be16_masked(s, "icmp_type", f->tp_src, wc->tp_src_mask);
+ format_be16_masked(s, "icmp_code", f->tp_dst, wc->tp_dst_mask);
+ format_ipv6_netmask(s, "nd_target", &f->nd_target,
+ &wc->nd_target_mask);
+ format_eth_masked(s, "nd_sll", f->arp_sha, wc->arp_sha_mask);
+ format_eth_masked(s, "nd_tll", f->arp_tha, wc->arp_tha_mask);
+ } else {
+ format_be16_masked(s, "tp_src", f->tp_src, wc->tp_src_mask);
+ format_be16_masked(s, "tp_dst", f->tp_dst, wc->tp_dst_mask);
+ }
+
+ if (s->length > start_len && ds_last(s) == ',') {
+ s->length--;
+ }