odp-util: Print in_port in decimal in format_odp_flow_key().
[openvswitch] / lib / flow.c
index 964877655b51e10a3748bd47b08e5068d95d01c0..ca745187d20780ac3994085b7fda4eaa6b0b7d70 100644 (file)
@@ -88,7 +88,7 @@ parse_vlan(struct ofpbuf *b, struct flow *flow)
     if (b->size >= sizeof(struct qtag_prefix) + sizeof(ovs_be16)) {
         struct qtag_prefix *qp = ofpbuf_pull(b, sizeof *qp);
         flow->dl_vlan = qp->tci & htons(VLAN_VID_MASK);
-        flow->dl_vlan_pcp = (ntohs(qp->tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
+        flow->dl_vlan_pcp = vlan_tci_to_pcp(qp->tci);
     }
 }
 
@@ -367,6 +367,8 @@ flow_nw_bits_to_mask(uint32_t wildcards, int shift)
     return wildcards < 32 ? htonl(~((1u << wildcards) - 1)) : 0;
 }
 
+/* Initializes 'wc' from 'wildcards', which may be any combination of the
+ * OFPFW_* and OVSFW_* wildcard bits. */
 void
 flow_wildcards_init(struct flow_wildcards *wc, uint32_t wildcards)
 {
@@ -375,3 +377,59 @@ flow_wildcards_init(struct flow_wildcards *wc, uint32_t wildcards)
     wc->nw_dst_mask = flow_nw_bits_to_mask(wc->wildcards, OFPFW_NW_DST_SHIFT);
 }
 
+/* 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)
+{
+    flow_wildcards_init(wc, 0);
+}
+
+static int
+count_ones(ovs_be32 mask)
+{
+#if __GNUC__ >= 4
+    return __builtin_popcount(mask);
+#else
+    int bits;
+
+    for (bits = 0; mask; bits++) {
+        mask &= mask - 1;
+    }
+
+    return bits;
+#endif
+}
+
+static bool
+set_nw_mask(struct flow_wildcards *wc, ovs_be32 mask,
+            ovs_be32 *maskp, int shift)
+{
+    int wcbits = 32 - count_ones(mask);
+    if (flow_nw_bits_to_mask(wcbits, 0) == mask) {
+        wc->wildcards &= ~(0x3f << shift);
+        wc->wildcards |= wcbits << shift;
+        *maskp = mask;
+        return true;
+    } else {
+        return false;
+    }
+}
+
+/* Sets the IP (or ARP) source wildcard mask to CIDR 'mask' (consisting of N
+ * high-order 1-bit and 32-N low-order 0-bits).  Returns true if successful,
+ * false if 'mask' is not a CIDR mask.  */
+bool
+flow_wildcards_set_nw_src_mask(struct flow_wildcards *wc, ovs_be32 mask)
+{
+    return set_nw_mask(wc, mask, &wc->nw_src_mask, OFPFW_NW_SRC_SHIFT);
+}
+
+/* Sets the IP (or ARP) destination wildcard mask to CIDR 'mask' (consisting of
+ * N high-order 1-bit and 32-N low-order 0-bits).  Returns true if successful,
+ * false if 'mask' is not a CIDR mask.  */
+bool
+flow_wildcards_set_nw_dst_mask(struct flow_wildcards *wc, ovs_be32 mask)
+{
+    return set_nw_mask(wc, mask, &wc->nw_dst_mask, OFPFW_NW_DST_SHIFT);
+}