ofpbuf: Fix trivial spelling typo.
[cascardo/ovs.git] / lib / nx-match.c
index 9e9aeb4..4999b1a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
+ * Copyright (c) 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include "openflow/nicira-ext.h"
 #include "packets.h"
 #include "shash.h"
+#include "tun-metadata.h"
 #include "unaligned.h"
 #include "util.h"
-#include "vlog.h"
+#include "openvswitch/vlog.h"
 
 VLOG_DEFINE_THIS_MODULE(nx_match);
 
@@ -97,6 +98,7 @@ static int nxm_class(uint64_t header) { return header >> 48; }
 static int nxm_field(uint64_t header) { return (header >> 41) & 0x7f; }
 static bool nxm_hasmask(uint64_t header) { return (header >> 40) & 1; }
 static int nxm_length(uint64_t header) { return (header >> 32) & 0xff; }
+static uint64_t nxm_no_len(uint64_t header) { return header & 0xffffff80ffffffffULL; }
 
 static bool
 is_experimenter_oxm(uint64_t header)
@@ -193,6 +195,9 @@ static const struct nxm_field *nxm_field_by_mf_id(enum mf_field_id,
                                                   enum ofp_version);
 
 static void nx_put_header__(struct ofpbuf *, uint64_t header, bool masked);
+static void nx_put_header_len(struct ofpbuf *, enum mf_field_id field,
+                              enum ofp_version version, bool masked,
+                              size_t n_bytes);
 
 /* Rate limit for nx_match parse errors.  These always indicate a bug in the
  * peer and so there's not much point in showing a lot of them. */
@@ -283,22 +288,22 @@ static enum ofperr
 nx_pull_header__(struct ofpbuf *b, bool allow_cookie, uint64_t *header,
                  const struct mf_field **field)
 {
-    if (ofpbuf_size(b) < 4) {
+    if (b->size < 4) {
         goto bad_len;
     }
 
-    *header = ((uint64_t) ntohl(get_unaligned_be32(ofpbuf_data(b)))) << 32;
+    *header = ((uint64_t) ntohl(get_unaligned_be32(b->data))) << 32;
     if (is_experimenter_oxm(*header)) {
-        if (ofpbuf_size(b) < 8) {
+        if (b->size < 8) {
             goto bad_len;
         }
-        *header = ntohll(get_unaligned_be64(ofpbuf_data(b)));
+        *header = ntohll(get_unaligned_be64(b->data));
     }
-    if (nxm_length(*header) <= nxm_experimenter_len(*header)) {
+    if (nxm_length(*header) < nxm_experimenter_len(*header)) {
         VLOG_WARN_RL(&rl, "OXM header "NXM_HEADER_FMT" has invalid length %d "
                      "(minimum is %d)",
                      NXM_HEADER_ARGS(*header), nxm_length(*header),
-                     nxm_header_len(*header) + 1);
+                     nxm_header_len(*header));
         goto error;
     }
     ofpbuf_pull(b, nxm_header_len(*header));
@@ -316,24 +321,45 @@ nx_pull_header__(struct ofpbuf *b, bool allow_cookie, uint64_t *header,
 
 bad_len:
     VLOG_DBG_RL(&rl, "encountered partial (%"PRIu32"-byte) OXM entry",
-                ofpbuf_size(b));
+                b->size);
 error:
     *header = 0;
-    *field = NULL;
+    if (field) {
+        *field = NULL;
+    }
     return OFPERR_OFPBMC_BAD_LEN;
 }
 
+static void
+copy_entry_value(const struct mf_field *field, union mf_value *value,
+                 const uint8_t *payload, int width)
+{
+    int copy_len;
+    void *copy_dst;
+
+    copy_dst = value;
+    copy_len = MIN(width, field ? field->n_bytes : sizeof *value);
+
+    if (field && field->variable_len) {
+        memset(value, 0, field->n_bytes);
+        copy_dst = &value->u8 + field->n_bytes - copy_len;
+    }
+
+    memcpy(copy_dst, payload, copy_len);
+}
+
 static enum ofperr
 nx_pull_entry__(struct ofpbuf *b, bool allow_cookie, uint64_t *header,
-                const struct mf_field **field,
+                const struct mf_field **field_,
                 union mf_value *value, union mf_value *mask)
 {
+    const struct mf_field *field;
     enum ofperr header_error;
     unsigned int payload_len;
     const uint8_t *payload;
     int width;
 
-    header_error = nx_pull_header__(b, allow_cookie, header, field);
+    header_error = nx_pull_header__(b, allow_cookie, header, &field);
     if (header_error && header_error != OFPERR_OFPBMC_BAD_FIELD) {
         return header_error;
     }
@@ -343,7 +369,7 @@ nx_pull_entry__(struct ofpbuf *b, bool allow_cookie, uint64_t *header,
     if (!payload) {
         VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" calls for %u-byte "
                     "payload but only %"PRIu32" bytes follow OXM header",
-                    NXM_HEADER_ARGS(*header), payload_len, ofpbuf_size(b));
+                    NXM_HEADER_ARGS(*header), payload_len, b->size);
         return OFPERR_OFPBMC_BAD_LEN;
     }
 
@@ -353,12 +379,13 @@ nx_pull_entry__(struct ofpbuf *b, bool allow_cookie, uint64_t *header,
         return OFPERR_OFPBMC_BAD_WILDCARDS;
     }
 
-    memcpy(value, payload, MIN(width, sizeof *value));
+    copy_entry_value(field, value, payload, width);
+
     if (mask) {
         if (nxm_hasmask(*header)) {
-            memcpy(mask, payload + width, MIN(width, sizeof *mask));
+            copy_entry_value(field, mask, payload + width, width);
         } else {
-            memset(mask, 0xff, MIN(width, sizeof *mask));
+            memset(mask, 0xff, sizeof *mask);
         }
     } else if (nxm_hasmask(*header)) {
         VLOG_DBG_RL(&rl, "OXM header "NXM_HEADER_FMT" includes mask but "
@@ -367,7 +394,12 @@ nx_pull_entry__(struct ofpbuf *b, bool allow_cookie, uint64_t *header,
         return OFPERR_OFPBMC_BAD_MASK;
     }
 
-    return header_error;
+    if (field_) {
+        *field_ = field;
+        return header_error;
+    }
+
+    return 0;
 }
 
 /* Attempts to pull an NXM or OXM header, value, and mask (if present) from the
@@ -443,8 +475,6 @@ static enum ofperr
 nx_pull_raw(const uint8_t *p, unsigned int match_len, bool strict,
             struct match *match, ovs_be64 *cookie, ovs_be64 *cookie_mask)
 {
-    struct ofpbuf b;
-
     ovs_assert((cookie != NULL) == (cookie_mask != NULL));
 
     match_init_catchall(match);
@@ -452,9 +482,9 @@ nx_pull_raw(const uint8_t *p, unsigned int match_len, bool strict,
         *cookie = *cookie_mask = htonll(0);
     }
 
-    ofpbuf_use_const(&b, p, match_len);
-    while (ofpbuf_size(&b)) {
-        const uint8_t *pos = ofpbuf_data(&b);
+    struct ofpbuf b = ofpbuf_const_initializer(p, match_len);
+    while (b.size) {
+        const uint8_t *pos = b.data;
         const struct mf_field *field;
         union mf_value value;
         union mf_value mask;
@@ -479,7 +509,15 @@ nx_pull_raw(const uint8_t *p, unsigned int match_len, bool strict,
         } else if (!mf_is_all_wild(field, &match->wc)) {
             error = OFPERR_OFPBMC_DUP_FIELD;
         } else {
-            mf_set(field, &value, &mask, match);
+            char *err_str;
+
+            mf_set(field, &value, &mask, match, &err_str);
+            if (err_str) {
+                VLOG_DBG_RL(&rl, "error parsing OXM at offset %"PRIdPTR" "
+                           "within match (%s)", pos - p, err_str);
+                free(err_str);
+                return OFPERR_OFPBMC_BAD_VALUE;
+            }
         }
 
         if (error) {
@@ -505,7 +543,7 @@ nx_pull_match__(struct ofpbuf *b, unsigned int match_len, bool strict,
         if (!p) {
             VLOG_DBG_RL(&rl, "nx_match length %u, rounded up to a "
                         "multiple of 8, is longer than space in message (max "
-                        "length %"PRIu32")", match_len, ofpbuf_size(b));
+                        "length %"PRIu32")", match_len, b->size);
             return OFPERR_OFPBMC_BAD_LEN;
         }
     }
@@ -541,11 +579,11 @@ nx_pull_match_loose(struct ofpbuf *b, unsigned int match_len,
 static enum ofperr
 oxm_pull_match__(struct ofpbuf *b, bool strict, struct match *match)
 {
-    struct ofp11_match_header *omh = ofpbuf_data(b);
+    struct ofp11_match_header *omh = b->data;
     uint8_t *p;
     uint16_t match_len;
 
-    if (ofpbuf_size(b) < sizeof *omh) {
+    if (b->size < sizeof *omh) {
         return OFPERR_OFPBMC_BAD_LEN;
     }
 
@@ -562,7 +600,7 @@ oxm_pull_match__(struct ofpbuf *b, bool strict, struct match *match)
     if (!p) {
         VLOG_DBG_RL(&rl, "oxm length %u, rounded up to a "
                     "multiple of 8, is longer than space in message (max "
-                    "length %"PRIu32")", match_len, ofpbuf_size(b));
+                    "length %"PRIu32")", match_len, b->size);
         return OFPERR_OFPBMC_BAD_LEN;
     }
 
@@ -589,6 +627,62 @@ oxm_pull_match_loose(struct ofpbuf *b, struct match *match)
 {
     return oxm_pull_match__(b, false, match);
 }
+
+/* Parses the OXM match description in the 'oxm_len' bytes in 'oxm'.  Stores
+ * the result in 'match'.
+ *
+ * Fails with an error when encountering unknown OXM headers.
+ *
+ * Returns 0 if successful, otherwise an OpenFlow error code. */
+enum ofperr
+oxm_decode_match(const void *oxm, size_t oxm_len, struct match *match)
+{
+    return nx_pull_raw(oxm, oxm_len, true, match, NULL, NULL);
+}
+
+/* Verify an array of OXM TLVs treating value of each TLV as a mask,
+ * disallowing masks in each TLV and ignoring pre-requisites. */
+enum ofperr
+oxm_pull_field_array(const void *fields_data, size_t fields_len,
+                     struct field_array *fa)
+{
+    struct ofpbuf b = ofpbuf_const_initializer(fields_data, fields_len);
+    while (b.size) {
+        const uint8_t *pos = b.data;
+        const struct mf_field *field;
+        union mf_value value;
+        enum ofperr error;
+        uint64_t header;
+
+        error = nx_pull_entry__(&b, false, &header, &field, &value, NULL);
+        if (error) {
+            VLOG_DBG_RL(&rl, "error pulling field array field");
+            return error;
+        } else if (!field) {
+            VLOG_DBG_RL(&rl, "unknown field array field");
+            error = OFPERR_OFPBMC_BAD_FIELD;
+        } else if (bitmap_is_set(fa->used.bm, field->id)) {
+            VLOG_DBG_RL(&rl, "duplicate field array field '%s'", field->name);
+            error = OFPERR_OFPBMC_DUP_FIELD;
+        } else if (!mf_is_mask_valid(field, &value)) {
+            VLOG_DBG_RL(&rl, "bad mask in field array field '%s'", field->name);
+            return OFPERR_OFPBMC_BAD_MASK;
+        } else {
+            field_array_set(field->id, &value, fa);
+        }
+
+        if (error) {
+            const uint8_t *start = fields_data;
+
+            VLOG_DBG_RL(&rl, "error parsing OXM at offset %"PRIdPTR" "
+                        "within field array (%s)", pos - start,
+                        ofperr_to_string(error));
+            return error;
+        }
+    }
+
+    return 0;
+}
 \f
 /* nx_put_match() and helpers.
  *
@@ -596,13 +690,16 @@ oxm_pull_match_loose(struct ofpbuf *b, struct match *match)
  * 'put' functions whose names end in 'm' add a field that might be wildcarded.
  * Other 'put' functions add exact-match fields.
  */
-
-static void
-nxm_put_unmasked(struct ofpbuf *b, enum mf_field_id field,
-                 enum ofp_version version, const void *value, size_t n_bytes)
+void
+nxm_put__(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
+          const void *value, const void *mask, size_t n_bytes)
 {
-    nx_put_header(b, field, version, false);
+    nx_put_header_len(b, field, version, !!mask, n_bytes);
     ofpbuf_put(b, value, n_bytes);
+    if (mask) {
+        ofpbuf_put(b, mask, n_bytes);
+    }
+
 }
 
 static void
@@ -611,11 +708,7 @@ nxm_put(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
 {
     if (!is_all_zeros(mask, n_bytes)) {
         bool masked = !is_all_ones(mask, n_bytes);
-        nx_put_header(b, field, version, masked);
-        ofpbuf_put(b, value, n_bytes);
-        if (masked) {
-            ofpbuf_put(b, mask, n_bytes);
-        }
+        nxm_put__(b, field, version, value, masked ? mask : NULL, n_bytes);
     }
 }
 
@@ -630,7 +723,7 @@ static void
 nxm_put_8(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
           uint8_t value)
 {
-    nxm_put_unmasked(b, field, version, &value, sizeof value);
+    nxm_put__(b, field, version, &value, NULL, sizeof value);
 }
 
 static void
@@ -644,7 +737,7 @@ static void
 nxm_put_16(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
            ovs_be16 value)
 {
-    nxm_put_unmasked(b, field, version, &value, sizeof value);
+    nxm_put__(b, field, version, &value, NULL, sizeof value);
 }
 
 static void
@@ -658,7 +751,7 @@ static void
 nxm_put_32(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
            ovs_be32 value)
 {
-    nxm_put_unmasked(b, field, version, &value, sizeof value);
+    nxm_put__(b, field, version, &value, NULL, sizeof value);
 }
 
 static void
@@ -668,13 +761,20 @@ nxm_put_64m(struct ofpbuf *b, enum mf_field_id field, enum ofp_version version,
     nxm_put(b, field, version, &value, &mask, sizeof value);
 }
 
+static void
+nxm_put_128m(struct ofpbuf *b,
+             enum mf_field_id field, enum ofp_version version,
+             const ovs_be128 value, const ovs_be128 mask)
+{
+    nxm_put(b, field, version, &value, &mask, sizeof(value));
+}
+
 static void
 nxm_put_eth_masked(struct ofpbuf *b,
                    enum mf_field_id field, enum ofp_version version,
-                   const uint8_t value[ETH_ADDR_LEN],
-                   const uint8_t mask[ETH_ADDR_LEN])
+                   const struct eth_addr value, const struct eth_addr mask)
 {
-    nxm_put(b, field, version, value, mask, ETH_ADDR_LEN);
+    nxm_put(b, field, version, value.ea, mask.ea, ETH_ADDR_LEN);
 }
 
 static void
@@ -733,7 +833,7 @@ nxm_put_ip(struct ofpbuf *b, const struct match *match, enum ofp_version oxm)
                   flow->nw_tos & IP_ECN_MASK);
     }
 
-    if (!oxm && match->wc.masks.nw_ttl) {
+    if (match->wc.masks.nw_ttl) {
         nxm_put_8(b, MFF_IP_TTL, oxm, flow->nw_ttl);
     }
 
@@ -813,11 +913,11 @@ nx_put_raw(struct ofpbuf *b, enum ofp_version oxm, const struct match *match,
            ovs_be64 cookie, ovs_be64 cookie_mask)
 {
     const struct flow *flow = &match->flow;
-    const size_t start_len = ofpbuf_size(b);
+    const size_t start_len = b->size;
     int match_len;
     int i;
 
-    BUILD_ASSERT_DECL(FLOW_WC_SEQ == 28);
+    BUILD_ASSERT_DECL(FLOW_WC_SEQ == 35);
 
     /* Metadata. */
     if (match->wc.masks.dp_hash) {
@@ -829,6 +929,10 @@ nx_put_raw(struct ofpbuf *b, enum ofp_version oxm, const struct match *match,
         nxm_put_32(b, MFF_RECIRC_ID, oxm, htonl(flow->recirc_id));
     }
 
+    if (match->wc.masks.conj_id) {
+        nxm_put_32(b, MFF_CONJ_ID, oxm, htonl(flow->conj_id));
+    }
+
     if (match->wc.masks.in_port.ofp_port) {
         ofp_port_t in_port = flow->in_port.ofp_port;
         if (oxm) {
@@ -918,10 +1022,21 @@ nx_put_raw(struct ofpbuf *b, enum ofp_version oxm, const struct match *match,
                 flow->tunnel.tun_id, match->wc.masks.tunnel.tun_id);
 
     /* Other tunnel metadata. */
+    nxm_put_16m(b, MFF_TUN_FLAGS, oxm,
+                htons(flow->tunnel.flags), htons(match->wc.masks.tunnel.flags));
     nxm_put_32m(b, MFF_TUN_SRC, oxm,
                 flow->tunnel.ip_src, match->wc.masks.tunnel.ip_src);
     nxm_put_32m(b, MFF_TUN_DST, oxm,
                 flow->tunnel.ip_dst, match->wc.masks.tunnel.ip_dst);
+    nxm_put_ipv6(b, MFF_TUN_IPV6_SRC, oxm,
+                 &flow->tunnel.ipv6_src, &match->wc.masks.tunnel.ipv6_src);
+    nxm_put_ipv6(b, MFF_TUN_IPV6_DST, oxm,
+                 &flow->tunnel.ipv6_dst, &match->wc.masks.tunnel.ipv6_dst);
+    nxm_put_16m(b, MFF_TUN_GBP_ID, oxm,
+                flow->tunnel.gbp_id, match->wc.masks.tunnel.gbp_id);
+    nxm_put_8m(b, MFF_TUN_GBP_FLAGS, oxm,
+               flow->tunnel.gbp_flags, match->wc.masks.tunnel.gbp_flags);
+    tun_metadata_to_nx_match(b, oxm, match);
 
     /* Registers. */
     if (oxm < OFP15_VERSION) {
@@ -937,10 +1052,20 @@ nx_put_raw(struct ofpbuf *b, enum ofp_version oxm, const struct match *match,
         }
     }
 
-    /* Mark. */
+    /* Packet mark. */
     nxm_put_32m(b, MFF_PKT_MARK, oxm, htonl(flow->pkt_mark),
                 htonl(match->wc.masks.pkt_mark));
 
+    /* Connection tracking. */
+    nxm_put_32m(b, MFF_CT_STATE, oxm, htonl(flow->ct_state),
+                htonl(match->wc.masks.ct_state));
+    nxm_put_16m(b, MFF_CT_ZONE, oxm, htons(flow->ct_zone),
+                htons(match->wc.masks.ct_zone));
+    nxm_put_32m(b, MFF_CT_MARK, oxm, htonl(flow->ct_mark),
+                htonl(match->wc.masks.ct_mark));
+    nxm_put_128m(b, MFF_CT_LABEL, oxm, hton128(flow->ct_label),
+                 hton128(match->wc.masks.ct_label));
+
     /* OpenFlow 1.1+ Metadata. */
     nxm_put_64m(b, MFF_METADATA, oxm,
                 flow->metadata, match->wc.masks.metadata);
@@ -957,7 +1082,7 @@ nx_put_raw(struct ofpbuf *b, enum ofp_version oxm, const struct match *match,
         }
     }
 
-    match_len = ofpbuf_size(b) - start_len;
+    match_len = b->size - start_len;
     return match_len;
 }
 
@@ -982,7 +1107,7 @@ nx_put_match(struct ofpbuf *b, const struct match *match,
 }
 
 /* Appends to 'b' an struct ofp11_match_header followed by the OXM format that
- * expresses 'cr', plus enough zero bytes to pad the data appended out to a
+ * expresses 'match', plus enough zero bytes to pad the data appended out to a
  * multiple of 8.
  *
  * OXM differs slightly among versions of OpenFlow.  Specify the OpenFlow
@@ -998,7 +1123,7 @@ oxm_put_match(struct ofpbuf *b, const struct match *match,
 {
     int match_len;
     struct ofp11_match_header *omh;
-    size_t start_len = ofpbuf_size(b);
+    size_t start_len = b->size;
     ovs_be64 cookie = htonll(0), cookie_mask = htonll(0);
 
     ofpbuf_put_uninit(b, sizeof *omh);
@@ -1013,6 +1138,100 @@ oxm_put_match(struct ofpbuf *b, const struct match *match,
     return match_len;
 }
 
+/* Appends to 'b' the OXM formats that expresses 'match', without header or
+ * padding.
+ *
+ * OXM differs slightly among versions of OpenFlow.  Specify the OpenFlow
+ * version in use as 'version'.
+ *
+ * This function can cause 'b''s data to be reallocated. */
+void
+oxm_put_raw(struct ofpbuf *b, const struct match *match,
+            enum ofp_version version)
+{
+    nx_put_raw(b, version, match, 0, 0);
+}
+
+/* Appends to 'b' the nx_match format that expresses the tlv corresponding
+ * to 'id'. If mask is not all-ones then it is also formated as the value
+ * of the tlv. */
+static void
+nx_format_mask_tlv(struct ds *ds, enum mf_field_id id,
+                   const union mf_value *mask)
+{
+    const struct mf_field *mf = mf_from_id(id);
+
+    ds_put_format(ds, "%s", mf->name);
+
+    if (!is_all_ones(mask, mf->n_bytes)) {
+        ds_put_char(ds, '=');
+        mf_format(mf, mask, NULL, ds);
+    }
+
+    ds_put_char(ds, ',');
+}
+
+/* Appends a string representation of 'fa_' to 'ds'.
+ * The TLVS value of 'fa_' is treated as a mask and
+ * only the name of fields is formated if it is all ones. */
+void
+oxm_format_field_array(struct ds *ds, const struct field_array *fa)
+{
+    size_t start_len = ds->length;
+    int i;
+
+    for (i = 0; i < MFF_N_IDS; i++) {
+        if (bitmap_is_set(fa->used.bm, i)) {
+            nx_format_mask_tlv(ds, i, &fa->value[i]);
+        }
+    }
+
+    if (ds->length > start_len) {
+        ds_chomp(ds, ',');
+    }
+}
+
+/* Appends to 'b' a series of OXM TLVs corresponding to the series
+ * of enum mf_field_id and value tuples in 'fa_'.
+ *
+ * OXM differs slightly among versions of OpenFlow.  Specify the OpenFlow
+ * version in use as 'version'.
+ *
+ * This function can cause 'b''s data to be reallocated.
+ *
+ * Returns the number of bytes appended to 'b'.  May return zero. */
+int
+oxm_put_field_array(struct ofpbuf *b, const struct field_array *fa,
+                    enum ofp_version version)
+{
+    size_t start_len = b->size;
+    int i;
+
+    /* Field arrays are only used with the group selection method
+     * property and group properties are only available in OpenFlow 1.5+.
+     * So the following assertion should never fail.
+     *
+     * If support for older OpenFlow versions is desired then some care
+     * will need to be taken of different TLVs that handle the same
+     * flow fields. In particular:
+     * - VLAN_TCI, VLAN_VID and MFF_VLAN_PCP
+     * - IP_DSCP_MASK and DSCP_SHIFTED
+     * - REGS and XREGS
+     */
+    ovs_assert(version >= OFP15_VERSION);
+
+    for (i = 0; i < MFF_N_IDS; i++) {
+        if (bitmap_is_set(fa->used.bm, i)) {
+            int len = mf_field_len(mf_from_id(i), &fa->value[i], NULL, NULL);
+            nxm_put__(b, i, version,
+                      &fa->value[i].u8 + mf_from_id(i)->n_bytes - len, NULL,
+                      len);
+        }
+    }
+
+    return b->size - start_len;
+}
+
 static void
 nx_put_header__(struct ofpbuf *b, uint64_t header, bool masked)
 {
@@ -1029,18 +1248,35 @@ nx_put_header(struct ofpbuf *b, enum mf_field_id field,
     nx_put_header__(b, mf_oxm_header(field, version), masked);
 }
 
+static void
+nx_put_header_len(struct ofpbuf *b, enum mf_field_id field,
+                  enum ofp_version version, bool masked, size_t n_bytes)
+{
+    uint64_t header = mf_oxm_header(field, version);
+
+    header = NXM_HEADER(nxm_vendor(header), nxm_class(header),
+                        nxm_field(header), false,
+                        nxm_experimenter_len(header) + n_bytes);
+
+    nx_put_header__(b, header, masked);
+}
+
 void
 nx_put_entry(struct ofpbuf *b,
              enum mf_field_id field, enum ofp_version version,
              const union mf_value *value, const union mf_value *mask)
 {
-    int n_bytes = mf_from_id(field)->n_bytes;
-    bool masked = mask && !is_all_ones(mask, n_bytes);
+    const struct mf_field *mf = mf_from_id(field);
+    bool masked;
+    int len, offset;
 
-    nx_put_header(b, field, version, masked);
-    ofpbuf_put(b, value, n_bytes);
+    len = mf_field_len(mf, value, mask, &masked);
+    offset = mf->n_bytes - len;
+
+    nx_put_header_len(b, field, version, masked, len);
+    ofpbuf_put(b, &value->u8 + offset, len);
     if (masked) {
-        ofpbuf_put(b, mask, n_bytes);
+        ofpbuf_put(b, &mask->u8 + offset, len);
     }
 }
 \f
@@ -1051,16 +1287,13 @@ static void format_nxm_field_name(struct ds *, uint64_t header);
 char *
 nx_match_to_string(const uint8_t *p, unsigned int match_len)
 {
-    struct ofpbuf b;
-    struct ds s;
-
     if (!match_len) {
         return xstrdup("<any>");
     }
 
-    ofpbuf_use_const(&b, p, match_len);
-    ds_init(&s);
-    while (ofpbuf_size(&b)) {
+    struct ofpbuf b = ofpbuf_const_initializer(p, match_len);
+    struct ds s = DS_EMPTY_INITIALIZER;
+    while (b.size) {
         union mf_value value;
         union mf_value mask;
         enum ofperr error;
@@ -1092,12 +1325,12 @@ nx_match_to_string(const uint8_t *p, unsigned int match_len)
         ds_put_char(&s, ')');
     }
 
-    if (ofpbuf_size(&b)) {
+    if (b.size) {
         if (s.length) {
             ds_put_cstr(&s, ", ");
         }
 
-        ds_put_format(&s, "<%u invalid bytes>", ofpbuf_size(&b));
+        ds_put_format(&s, "<%u invalid bytes>", b.size);
     }
 
     return ds_steal_cstr(&s);
@@ -1106,7 +1339,7 @@ nx_match_to_string(const uint8_t *p, unsigned int match_len)
 char *
 oxm_match_to_string(const struct ofpbuf *p, unsigned int match_len)
 {
-    const struct ofp11_match_header *omh = ofpbuf_data(p);
+    const struct ofp11_match_header *omh = p->data;
     uint16_t match_len_;
     struct ds s;
 
@@ -1222,10 +1455,10 @@ static int
 nx_match_from_string_raw(const char *s, struct ofpbuf *b)
 {
     const char *full_s = s;
-    const size_t start_len = ofpbuf_size(b);
+    const size_t start_len = b->size;
 
     if (!strcmp(s, "<any>")) {
-        /* Ensure that 'ofpbuf_data(b)' isn't actually null. */
+        /* Ensure that 'b->data' isn't actually null. */
         ofpbuf_prealloc_tailroom(b, 1);
         return 0;
     }
@@ -1233,6 +1466,8 @@ nx_match_from_string_raw(const char *s, struct ofpbuf *b)
     for (s += strspn(s, ", "); *s; s += strspn(s, ", ")) {
         const char *name;
         uint64_t header;
+        ovs_be64 nw_header;
+        ovs_be64 *header_ptr;
         int name_len;
         size_t n;
 
@@ -1249,11 +1484,31 @@ nx_match_from_string_raw(const char *s, struct ofpbuf *b)
 
         s += name_len + 1;
 
-        nx_put_header__(b, header, false);
+        header_ptr = ofpbuf_put_uninit(b, nxm_header_len(header));
         s = ofpbuf_put_hex(b, s, &n);
         if (n != nxm_field_bytes(header)) {
-            ovs_fatal(0, "%.2s: hex digits expected", s);
+            const struct mf_field *field = mf_from_oxm_header(header);
+
+            if (field && field->variable_len) {
+                if (n <= field->n_bytes) {
+                    int len = (nxm_hasmask(header) ? n * 2 : n) +
+                              nxm_experimenter_len(header);
+
+                    header = NXM_HEADER(nxm_vendor(header), nxm_class(header),
+                                        nxm_field(header),
+                                        nxm_hasmask(header) ? 1 : 0, len);
+                } else {
+                    ovs_fatal(0, "expected to read at most %d bytes but got "
+                              "%"PRIuSIZE, field->n_bytes, n);
+                }
+            } else {
+                ovs_fatal(0, "expected to read %d bytes but got %"PRIuSIZE,
+                          nxm_field_bytes(header), n);
+            }
         }
+        nw_header = htonll(header);
+        memcpy(header_ptr, &nw_header, nxm_header_len(header));
+
         if (nxm_hasmask(header)) {
             s += strspn(s, " ");
             if (*s != '/') {
@@ -1274,7 +1529,7 @@ nx_match_from_string_raw(const char *s, struct ofpbuf *b)
         s++;
     }
 
-    return ofpbuf_size(b) - start_len;
+    return b->size - start_len;
 }
 
 int
@@ -1290,7 +1545,7 @@ oxm_match_from_string(const char *s, struct ofpbuf *b)
 {
     int match_len;
     struct ofp11_match_header *omh;
-    size_t start_len = ofpbuf_size(b);
+    size_t start_len = b->size;
 
     ofpbuf_put_uninit(b, sizeof *omh);
     match_len = nx_match_from_string_raw(s, b) + sizeof *omh;
@@ -1369,8 +1624,8 @@ nxm_execute_reg_move(const struct ofpact_reg_move *move,
     union mf_value src_value;
     union mf_value dst_value;
 
-    mf_mask_field_and_prereqs(move->dst.field, &wc->masks);
-    mf_mask_field_and_prereqs(move->src.field, &wc->masks);
+    mf_mask_field_and_prereqs(move->dst.field, wc);
+    mf_mask_field_and_prereqs(move->src.field, wc);
 
     /* A flow may wildcard nw_frag.  Do nothing if setting a transport
      * header field on a packet that does not have them. */
@@ -1467,9 +1722,9 @@ nx_stack_pop(struct ofpbuf *stack)
 {
     union mf_subvalue *v = NULL;
 
-    if (ofpbuf_size(stack)) {
+    if (stack->size) {
 
-        ofpbuf_set_size(stack, ofpbuf_size(stack) - sizeof *v);
+        stack->size -= sizeof *v;
         v = (union mf_subvalue *) ofpbuf_tail(stack);
     }
 
@@ -1510,7 +1765,7 @@ nxm_execute_stack_pop(const struct ofpact_stack *pop,
     } else {
         if (!VLOG_DROP_WARN(&rl)) {
             char *flow_str = flow_to_string(flow);
-            VLOG_WARN_RL(&rl, "Failed to pop from an empty stack. On flow \n"
+            VLOG_WARN_RL(&rl, "Failed to pop from an empty stack. On flow\n"
                            " %s", flow_str);
             free(flow_str);
         }
@@ -1736,7 +1991,7 @@ oxm_maskable_fields(void)
 struct nxm_field_index {
     struct hmap_node header_node; /* In nxm_header_map. */
     struct hmap_node name_node;   /* In nxm_name_map. */
-    struct list mf_node;          /* In mf_mf_map[nf.id]. */
+    struct ovs_list mf_node;      /* In mf_mf_map[nf.id]. */
     const struct nxm_field nf;
 };
 
@@ -1744,7 +1999,7 @@ struct nxm_field_index {
 
 static struct hmap nxm_header_map;
 static struct hmap nxm_name_map;
-static struct list nxm_mf_map[MFF_N_IDS];
+static struct ovs_list nxm_mf_map[MFF_N_IDS];
 
 static void
 nxm_init(void)
@@ -1759,7 +2014,7 @@ nxm_init(void)
         for (struct nxm_field_index *nfi = all_nxm_fields;
              nfi < &all_nxm_fields[ARRAY_SIZE(all_nxm_fields)]; nfi++) {
             hmap_insert(&nxm_header_map, &nfi->header_node,
-                        hash_int(nfi->nf.header, 0));
+                        hash_uint64(nxm_no_len(nfi->nf.header)));
             hmap_insert(&nxm_name_map, &nfi->name_node,
                         hash_string(nfi->nf.name, 0));
             list_push_back(&nxm_mf_map[nfi->nf.id], &nfi->mf_node);
@@ -1772,16 +2027,24 @@ static const struct nxm_field *
 nxm_field_by_header(uint64_t header)
 {
     const struct nxm_field_index *nfi;
+    uint64_t header_no_len;
 
     nxm_init();
     if (nxm_hasmask(header)) {
         header = nxm_make_exact_header(header);
     }
 
-    HMAP_FOR_EACH_IN_BUCKET (nfi, header_node, hash_int(header, 0),
+    header_no_len = nxm_no_len(header);
+
+    HMAP_FOR_EACH_IN_BUCKET (nfi, header_node, hash_uint64(header_no_len),
                              &nxm_header_map) {
-        if (header == nfi->nf.header) {
-            return &nfi->nf;
+        if (header_no_len == nxm_no_len(nfi->nf.header)) {
+            if (nxm_length(header) == nxm_length(nfi->nf.header) ||
+                mf_from_id(nfi->nf.id)->variable_len) {
+                return &nfi->nf;
+            } else {
+                return NULL;
+            }
         }
     }
     return NULL;