Typos. A bunch of typos I've been collecting.
[vpp.git] / src / vnet / bier / bier_fmask_db.c
index 37cbb36..d0f5ba1 100644 (file)
  *
  * The table's key is part of this DB key, since the fmasks therein build up
  * their forwarding mask based on the routes that resolve through
- * it, so cross polination would be bad.
+ * it, so cross pollination would be bad.
  */
 typedef struct bier_fmask_db_t_ {
     /**
      * hash table for underlying storage
      */
-    mhash_t bfdb_hash;
+    uword *bfdb_hash;
 
     /**
      * Pool for memory
@@ -39,16 +39,6 @@ typedef struct bier_fmask_db_t_ {
     struct bier_fmask_t_ *bfdb_pool;
 } bier_fmask_db_t;
 
-/**
- * The key used in the fmask DB to compare fmask objects.
- * There is one global DB, so we need to use the table's ID and the fmasks ID
- */
-typedef struct bier_fmask_db_key_t_ {
-    bier_fmask_id_t bfmdbk_fm_id;
-    index_t bfmdbk_tbl_id;
-} bier_fmask_db_key_t;
-// TODO packed?
-
 /**
  * Single fmask DB
  */
@@ -61,99 +51,125 @@ bier_fmask_get_index (const bier_fmask_t *bfm)
     return (bfm - bier_fmask_db.bfdb_pool);
 }
 
-u32
-bier_fmask_db_find_or_create_and_lock (index_t bti,
-                                       const bier_fmask_id_t *fmid,
-                                       const fib_route_path_t *rpath)
+static void
+bier_fmask_db_mk_key (index_t bti,
+                      const fib_route_path_t *rpath,
+                      bier_fmask_id_t *key)
 {
-    bier_fmask_db_key_t key;
-    u32 index;
-    uword *p;
-
     /*
-     * there be padding in that thar key, and it's
-     * used as a memcmp in the mhash.
+     * Depending on what the ID is there may be padding.
+     * This key will be memcmp'd in the mhash, so make sure it's all 0
      */
-    memset(&key, 0, sizeof(key));
-    key.bfmdbk_tbl_id = bti;
-    key.bfmdbk_fm_id = *fmid;
+    clib_memset(key, 0, sizeof(*key));
 
-    index = INDEX_INVALID;
-    p = mhash_get (&bier_fmask_db.bfdb_hash, &key);
-
-    if (NULL == p)
+    /*
+     * Pick the attributes from the path that make the FMask unique
+     */
+    if (FIB_ROUTE_PATH_UDP_ENCAP & rpath->frp_flags)
     {
-        /*
-         * adding a new fmask object
-         */
-        index = bier_fmask_create_and_lock(fmid, bti, rpath);
-
-        mhash_set (&bier_fmask_db.bfdb_hash, &key, index, 0 /*old_value*/);
+        key->bfmi_id = rpath->frp_udp_encap_id;
+        key->bfmi_nh_type = BIER_NH_UDP;
     }
     else
     {
-        index = p[0];
-        bier_fmask_lock(index);
+        memcpy(&key->bfmi_nh, &rpath->frp_addr, sizeof(rpath->frp_addr));
+        key->bfmi_nh_type = BIER_NH_IP;
     }
-
-    return (index);
+    if (NULL == rpath->frp_label_stack)
+    {
+        key->bfmi_hdr_type = BIER_HDR_O_OTHER;
+    }
+    else
+    {
+        key->bfmi_hdr_type = BIER_HDR_O_MPLS;
+    }
+    key->bfmi_bti = bti;
 }
 
 u32
 bier_fmask_db_find (index_t bti,
-                    const bier_fmask_id_t *fmid)
+                    const fib_route_path_t *rpath)
 {
-    bier_fmask_db_key_t key;
-    u32 index;
+    bier_fmask_id_t fmid;
     uword *p;
 
-    /*
-     * there be padding in that thar key, and it's
-     * used as a memcmp in the mhash.
-     */
-    memset(&key, 0, sizeof(key));
-    key.bfmdbk_tbl_id = bti;
-    key.bfmdbk_fm_id = *fmid;
-
-    index = INDEX_INVALID;
-    p = mhash_get(&bier_fmask_db.bfdb_hash, &key);
+    bier_fmask_db_mk_key(bti, rpath, &fmid);
+    p = hash_get_mem(bier_fmask_db.bfdb_hash, &fmid);
 
     if (NULL != p)
+    {
+        return (p[0]);
+    }
+
+    return (INDEX_INVALID);
+}
+
+u32
+bier_fmask_db_find_or_create_and_lock (index_t bti,
+                                       const fib_route_path_t *rpath)
+{
+    bier_fmask_id_t fmid;
+    u32 index;
+    uword *p;
+
+    bier_fmask_db_mk_key(bti, rpath, &fmid);
+    p = hash_get_mem(bier_fmask_db.bfdb_hash, &fmid);
+
+    if (NULL == p)
+    {
+        bier_fmask_t *bfm;
+        /*
+         * adding a new fmask object
+         */
+        index = bier_fmask_create_and_lock(&fmid, rpath);
+        bfm = bier_fmask_get(index);
+        hash_set_mem(bier_fmask_db.bfdb_hash, bfm->bfm_id, index);
+    }
+    else
     {
         index = p[0];
+        bier_fmask_lock(index);
     }
 
     return (index);
 }
 
 void
-bier_fmask_db_remove (index_t bti,
-                      const bier_fmask_id_t *fmid)
+bier_fmask_db_remove (const bier_fmask_id_t *fmid)
 {
-    bier_fmask_db_key_t key = {
-        .bfmdbk_tbl_id = bti,
-        .bfmdbk_fm_id = *fmid,
-    };
     uword *p;
 
-    p = mhash_get (&bier_fmask_db.bfdb_hash, &key);
+    p = hash_get_mem(bier_fmask_db.bfdb_hash, fmid);
 
     if (NULL == p) {
         /*
-         * remove a non-exitant entry - oops
+         * remove a non-existent entry - oops
          */
-        ASSERT (!"remove non-existant fmask");
+        ASSERT (!"remove non-existent fmask");
     } else {
-        mhash_unset (&(bier_fmask_db.bfdb_hash), &key, 0);
+        hash_unset(bier_fmask_db.bfdb_hash, fmid);
     }
 }
 
+void
+bier_fmask_db_walk (bier_fmask_walk_fn_t fn, void *ctx)
+{
+    CLIB_UNUSED (bier_fmask_id_t *fmid);
+    uword *bfmi;
+
+    hash_foreach(fmid, bfmi, bier_fmask_db.bfdb_hash,
+    ({
+        if (WALK_STOP == fn(*bfmi, ctx))
+            break;
+    }));
+}
+
 clib_error_t *
 bier_fmask_db_module_init (vlib_main_t *vm)
 {
-    mhash_init (&bier_fmask_db.bfdb_hash,
-                sizeof(uword),
-                sizeof(bier_fmask_db_key_t));
+    bier_fmask_db.bfdb_hash = hash_create_mem(0,
+                                              sizeof(bier_fmask_id_t),
+                                              sizeof(index_t));
 
     return (NULL);
 }