2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vnet/bier/bier_fmask_db.h>
17 #include <vnet/bier/bier_fmask.h>
20 * Global Table of fmask objects
21 * The key into this table includes the table's key and the fmask's key,
22 * so there could be a DB per-table. But it is more efficient
23 * at forwarding time to extract the fmask from a single global table
24 * which is hot in dcache.
26 * The table's key is part of this DB key, since the fmasks therein build up
27 * their forwarding mask based on the routes that resolve through
28 * it, so cross polination would be bad.
30 typedef struct bier_fmask_db_t_ {
32 * hash table for underlying storage
39 struct bier_fmask_t_ *bfdb_pool;
43 * The key used in the fmask DB to compare fmask objects.
44 * There is one global DB, so we need to use the table's ID and the fmasks ID
46 typedef struct bier_fmask_db_key_t_ {
47 bier_fmask_id_t bfmdbk_fm_id;
48 index_t bfmdbk_tbl_id;
49 } bier_fmask_db_key_t;
55 static bier_fmask_db_t bier_fmask_db;
59 bier_fmask_get_index (const bier_fmask_t *bfm)
61 return (bfm - bier_fmask_db.bfdb_pool);
65 bier_fmask_db_find_or_create_and_lock (index_t bti,
66 const bier_fmask_id_t *fmid,
67 const fib_route_path_t *rpath)
69 bier_fmask_db_key_t key;
74 * there be padding in that thar key, and it's
75 * used as a memcmp in the mhash.
77 memset(&key, 0, sizeof(key));
78 key.bfmdbk_tbl_id = bti;
79 key.bfmdbk_fm_id = *fmid;
81 index = INDEX_INVALID;
82 p = mhash_get (&bier_fmask_db.bfdb_hash, &key);
87 * adding a new fmask object
89 index = bier_fmask_create_and_lock(fmid, bti, rpath);
91 mhash_set (&bier_fmask_db.bfdb_hash, &key, index, 0 /*old_value*/);
96 bier_fmask_lock(index);
103 bier_fmask_db_find (index_t bti,
104 const bier_fmask_id_t *fmid)
106 bier_fmask_db_key_t key;
111 * there be padding in that thar key, and it's
112 * used as a memcmp in the mhash.
114 memset(&key, 0, sizeof(key));
115 key.bfmdbk_tbl_id = bti;
116 key.bfmdbk_fm_id = *fmid;
118 index = INDEX_INVALID;
119 p = mhash_get(&bier_fmask_db.bfdb_hash, &key);
130 bier_fmask_db_remove (index_t bti,
131 const bier_fmask_id_t *fmid)
133 bier_fmask_db_key_t key = {
134 .bfmdbk_tbl_id = bti,
135 .bfmdbk_fm_id = *fmid,
139 p = mhash_get (&bier_fmask_db.bfdb_hash, &key);
143 * remove a non-exitant entry - oops
145 ASSERT (!"remove non-existant fmask");
147 mhash_unset (&(bier_fmask_db.bfdb_hash), &key, 0);
152 bier_fmask_db_module_init (vlib_main_t *vm)
154 mhash_init (&bier_fmask_db.bfdb_hash,
156 sizeof(bier_fmask_db_key_t));
161 VLIB_INIT_FUNCTION (bier_fmask_db_module_init);