+ * - Otherwise, makes 'rule' match only packets with an 802.1Q header whose
+ * VID equals the low 12 bits of 'dl_vlan'.
+ */
+void
+cls_rule_set_dl_vlan(struct cls_rule *rule, ovs_be16 dl_vlan)
+{
+ if (dl_vlan == htons(OFP_VLAN_NONE)) {
+ cls_rule_set_dl_tci(rule, htons(0));
+ } else {
+ dl_vlan &= htons(VLAN_VID_MASK);
+ rule->flow.vlan_tci &= ~htons(VLAN_VID_MASK);
+ rule->flow.vlan_tci |= htons(VLAN_CFI) | dl_vlan;
+ rule->wc.vlan_tci_mask |= htons(VLAN_VID_MASK | VLAN_CFI);
+ }
+}
+
+/* Modifies 'rule' so that the VLAN PCP is wildcarded. If the VID is already
+ * wildcarded, then 'rule' will match a packet regardless of whether it has an
+ * 802.1Q header or not. */
+void
+cls_rule_set_any_pcp(struct cls_rule *rule)
+{
+ if (rule->wc.vlan_tci_mask & htons(VLAN_VID_MASK)) {
+ rule->wc.vlan_tci_mask &= ~htons(VLAN_PCP_MASK);
+ rule->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
+ } else {
+ cls_rule_set_dl_tci_masked(rule, htons(0), htons(0));
+ }
+}
+
+/* Modifies 'rule' so that it matches only packets with an 802.1Q header whose
+ * PCP equals the low 3 bits of 'dl_vlan_pcp'. */
+void
+cls_rule_set_dl_vlan_pcp(struct cls_rule *rule, uint8_t dl_vlan_pcp)
+{
+ dl_vlan_pcp &= 0x07;
+ rule->flow.vlan_tci &= ~htons(VLAN_PCP_MASK);
+ rule->flow.vlan_tci |= htons((dl_vlan_pcp << VLAN_PCP_SHIFT) | VLAN_CFI);
+ rule->wc.vlan_tci_mask |= htons(VLAN_CFI | VLAN_PCP_MASK);
+}
+
+void
+cls_rule_set_tp_src(struct cls_rule *rule, ovs_be16 tp_src)
+{
+ rule->wc.wildcards &= ~FWW_TP_SRC;
+ rule->flow.tp_src = tp_src;
+}
+
+void
+cls_rule_set_tp_dst(struct cls_rule *rule, ovs_be16 tp_dst)
+{
+ rule->wc.wildcards &= ~FWW_TP_DST;
+ rule->flow.tp_dst = tp_dst;
+}
+
+void
+cls_rule_set_nw_proto(struct cls_rule *rule, uint8_t nw_proto)
+{
+ rule->wc.wildcards &= ~FWW_NW_PROTO;
+ rule->flow.nw_proto = nw_proto;
+}
+
+void
+cls_rule_set_nw_src(struct cls_rule *rule, ovs_be32 nw_src)
+{
+ cls_rule_set_nw_src_masked(rule, nw_src, htonl(UINT32_MAX));
+}
+
+bool
+cls_rule_set_nw_src_masked(struct cls_rule *rule, ovs_be32 ip, ovs_be32 mask)
+{
+ if (flow_wildcards_set_nw_src_mask(&rule->wc, mask)) {
+ rule->flow.nw_src = ip & mask;
+ return true;
+ } else {
+ return false;
+ }
+}
+
+void
+cls_rule_set_nw_dst(struct cls_rule *rule, ovs_be32 nw_dst)
+{
+ cls_rule_set_nw_dst_masked(rule, nw_dst, htonl(UINT32_MAX));
+}
+
+bool
+cls_rule_set_nw_dst_masked(struct cls_rule *rule, ovs_be32 ip, ovs_be32 mask)
+{
+ if (flow_wildcards_set_nw_dst_mask(&rule->wc, mask)) {
+ rule->flow.nw_dst = ip & mask;
+ return true;
+ } else {
+ return false;
+ }
+}
+
+void
+cls_rule_set_nw_tos(struct cls_rule *rule, uint8_t nw_tos)
+{
+ rule->wc.wildcards &= ~FWW_NW_TOS;
+ rule->flow.nw_tos = nw_tos & IP_DSCP_MASK;
+}
+
+void
+cls_rule_set_icmp_type(struct cls_rule *rule, uint8_t icmp_type)
+{
+ rule->wc.wildcards &= ~FWW_TP_SRC;
+ rule->flow.icmp_type = htons(icmp_type);
+
+}
+
+void
+cls_rule_set_icmp_code(struct cls_rule *rule, uint8_t icmp_code)
+{
+ rule->wc.wildcards &= ~FWW_TP_DST;
+ rule->flow.icmp_code = htons(icmp_code);
+}
+
+void
+cls_rule_set_arp_sha(struct cls_rule *rule, const uint8_t sha[ETH_ADDR_LEN])
+{
+ rule->wc.wildcards &= ~FWW_ARP_SHA;
+ memcpy(rule->flow.arp_sha, sha, ETH_ADDR_LEN);
+}
+
+void
+cls_rule_set_arp_tha(struct cls_rule *rule, const uint8_t tha[ETH_ADDR_LEN])
+{
+ rule->wc.wildcards &= ~FWW_ARP_THA;
+ memcpy(rule->flow.arp_tha, tha, ETH_ADDR_LEN);
+}
+
+void
+cls_rule_set_ipv6_src(struct cls_rule *rule, const struct in6_addr *src)
+{
+ cls_rule_set_ipv6_src_masked(rule, src, &in6addr_exact);
+}
+
+bool
+cls_rule_set_ipv6_src_masked(struct cls_rule *rule, const struct in6_addr *src,
+ const struct in6_addr *mask)
+{
+ if (flow_wildcards_set_ipv6_src_mask(&rule->wc, mask)) {
+ rule->flow.ipv6_src = ipv6_addr_bitand(src, mask);
+ return true;
+ } else {
+ return false;
+ }
+}
+
+void
+cls_rule_set_ipv6_dst(struct cls_rule *rule, const struct in6_addr *dst)
+{
+ cls_rule_set_ipv6_dst_masked(rule, dst, &in6addr_exact);
+}
+
+bool
+cls_rule_set_ipv6_dst_masked(struct cls_rule *rule, const struct in6_addr *dst,
+ const struct in6_addr *mask)
+{
+ if (flow_wildcards_set_ipv6_dst_mask(&rule->wc, mask)) {
+ rule->flow.ipv6_dst = ipv6_addr_bitand(dst, mask);
+ return true;
+ } else {
+ return false;
+ }
+}
+
+void
+cls_rule_set_nd_target(struct cls_rule *rule, const struct in6_addr target)
+{
+ rule->wc.wildcards &= ~FWW_ND_TARGET;
+ rule->flow.nd_target = target;
+}
+
+/* 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));
+}
+
+static void
+format_ip_netmask(struct ds *s, const char *name, ovs_be32 ip,
+ ovs_be32 netmask)
+{
+ if (netmask) {
+ ds_put_format(s, "%s="IP_FMT, name, IP_ARGS(&ip));
+ if (netmask != htonl(UINT32_MAX)) {
+ if (ip_is_cidr(netmask)) {
+ int wcbits = ofputil_netmask_to_wcbits(netmask);
+ ds_put_format(s, "/%d", 32 - wcbits);
+ } else {
+ ds_put_format(s, "/"IP_FMT, IP_ARGS(&netmask));
+ }
+ }
+ 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_addr(s, addr);
+ if (!ipv6_mask_is_exact(netmask)) {
+ if (ipv6_is_cidr(netmask)) {
+ int cidr_bits = ipv6_count_cidr_bits(netmask);
+ ds_put_format(s, "/%d", cidr_bits);
+ } else {
+ ds_put_char(s, '/');
+ print_ipv6_addr(s, netmask);
+ }
+ }
+ ds_put_char(s, ',');
+ }
+}
+