nx-match: Enable senders of NXM fields to specify length.
[cascardo/ovs.git] / lib / nx-match.c
index 4b72460..1a6b89a 100644 (file)
@@ -97,6 +97,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 +194,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. */
@@ -319,21 +323,42 @@ bad_len:
                 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;
     }
@@ -353,12 +378,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 +393,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
@@ -647,7 +678,7 @@ static void
 nxm_put_unmasked(struct ofpbuf *b, enum mf_field_id field,
                  enum ofp_version version, const void *value, size_t n_bytes)
 {
-    nx_put_header(b, field, version, false);
+    nx_put_header_len(b, field, version, false, n_bytes);
     ofpbuf_put(b, value, n_bytes);
 }
 
@@ -657,7 +688,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);
+        nx_put_header_len(b, field, version, masked, n_bytes);
         ofpbuf_put(b, value, n_bytes);
         if (masked) {
             ofpbuf_put(b, mask, n_bytes);
@@ -1123,7 +1154,7 @@ oxm_put_field_array(struct ofpbuf *b, const struct field_array *fa,
     int i;
 
     /* Field arrays are only used with the group selection method
-     * property and group properties are only available in OpenFlow 1.5+.
+     * 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
@@ -1161,6 +1192,19 @@ 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,
@@ -1891,7 +1935,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);
@@ -1904,16 +1948,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;