[ovs-dev] [PATCH 7/7] lib/classifier: Constify cls_subtable fields.

Jarno Rajahalme jrajahalme at nicira.com
Fri Oct 24 20:36:41 UTC 2014


Some struct cls_subtable fields were documented of being const.  Make
them const and use CONST_CAST where appropriate to initialize them.

This will help catch future errors modifying those fields after
initialization.

Signed-off-by: Jarno Rajahalme <jrajahalme at nicira.com>
---
 lib/classifier-private.h |   12 ++++++------
 lib/classifier.c         |   19 +++++++++++--------
 2 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/lib/classifier-private.h b/lib/classifier-private.h
index 1ab76fc..d690f47 100644
--- a/lib/classifier-private.h
+++ b/lib/classifier-private.h
@@ -38,18 +38,18 @@ struct cls_subtable {
      * following data structures. */
 
     /* These fields are accessed by readers who care about wildcarding. */
-    tag_type tag;       /* Tag generated from mask for partitioning (const). */
-    uint8_t n_indices;                   /* How many indices to use (const). */
-    uint8_t index_ofs[CLS_MAX_INDICES];   /* u32 segment boundaries (const). */
+    const tag_type tag;       /* Tag generated from mask for partitioning. */
+    const uint8_t n_indices;                   /* How many indices to use. */
+    const uint8_t index_ofs[CLS_MAX_INDICES];  /* u32 segment boundaries. */
     unsigned int trie_plen[CLS_MAX_TRIES];  /* Trie prefix length in 'mask'
                                              * (runtime configurable). */
-    int ports_mask_len;                     /* (const) */
+    const int ports_mask_len;
     struct cmap indices[CLS_MAX_INDICES];   /* Staged lookup indices. */
     rcu_trie_ptr ports_trie;                /* NULL if none. */
 
     /* These fields are accessed by all readers. */
-    struct cmap rules;                      /* Contains "struct cls_rule"s. */
-    struct minimask mask;                   /* Wildcards for fields (const). */
+    struct cmap rules;                      /* Contains 'cls_match'es. */
+    const struct minimask mask;             /* Wildcards for fields. */
     /* 'mask' must be the last field. */
 };
 
diff --git a/lib/classifier.c b/lib/classifier.c
index c6391c8..5dca3ba 100644
--- a/lib/classifier.c
+++ b/lib/classifier.c
@@ -1106,7 +1106,8 @@ insert_subtable(struct classifier *cls, const struct minimask *mask)
     subtable = xzalloc(sizeof *subtable - sizeof mask->masks.inline_values
                        + MINIFLOW_VALUES_SIZE(count));
     cmap_init(&subtable->rules);
-    miniflow_clone_inline(&subtable->mask.masks, &mask->masks, count);
+    miniflow_clone_inline(CONST_CAST(struct miniflow *, &subtable->mask.masks),
+                          &mask->masks, count);
 
     /* Init indices for segmented lookup, if any. */
     flow_wildcards_init_catchall(&new);
@@ -1118,7 +1119,8 @@ insert_subtable(struct classifier *cls, const struct minimask *mask)
         /* Add an index if it adds mask bits. */
         if (!flow_wildcards_equal(&new, &old)) {
             cmap_init(&subtable->indices[index]);
-            subtable->index_ofs[index] = cls->flow_segments[i];
+            *CONST_CAST(uint8_t *, &subtable->index_ofs[index])
+                = cls->flow_segments[i];
             index++;
             old = new;
         }
@@ -1130,15 +1132,16 @@ insert_subtable(struct classifier *cls, const struct minimask *mask)
         flow_wildcards_fold_minimask_range(&new, mask, prev, FLOW_U32S);
         if (flow_wildcards_equal(&new, &old)) {
             --index;
-            subtable->index_ofs[index] = 0;
+            *CONST_CAST(uint8_t *, &subtable->index_ofs[index]) = 0;
             cmap_destroy(&subtable->indices[index]);
         }
     }
-    subtable->n_indices = index;
+    *CONST_CAST(uint8_t *, &subtable->n_indices) = index;
 
-    subtable->tag = (minimask_get_metadata_mask(mask) == OVS_BE64_MAX
-                     ? tag_create_deterministic(hash)
-                     : TAG_ALL);
+    *CONST_CAST(tag_type *, &subtable->tag) =
+        (minimask_get_metadata_mask(mask) == OVS_BE64_MAX
+         ? tag_create_deterministic(hash)
+         : TAG_ALL);
 
     for (i = 0; i < cls->n_tries; i++) {
         subtable->trie_plen[i] = minimask_get_prefix_len(mask,
@@ -1147,7 +1150,7 @@ insert_subtable(struct classifier *cls, const struct minimask *mask)
 
     /* Ports trie. */
     ovsrcu_set_hidden(&subtable->ports_trie, NULL);
-    subtable->ports_mask_len
+    *CONST_CAST(int *, &subtable->ports_mask_len)
         = 32 - ctz32(ntohl(MINIFLOW_GET_BE32(&mask->masks, tp_src)));
 
     /* List of rules. */
-- 
1.7.10.4




More information about the dev mailing list