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;
45 static bier_fmask_db_t bier_fmask_db;
49 bier_fmask_get_index (const bier_fmask_t *bfm)
51 return (bfm - bier_fmask_db.bfdb_pool);
55 bier_fmask_db_mk_key (index_t bti,
56 const fib_route_path_t *rpath,
60 * Depending on what the ID is there may be padding.
61 * This key will be memcmp'd in the mhash, so make sure it's all 0
63 memset(key, 0, sizeof(*key));
66 * Pick the attributes from the path that make the FMask unique
68 if (FIB_ROUTE_PATH_UDP_ENCAP & rpath->frp_flags)
70 key->bfmi_id = rpath->frp_udp_encap_id;
71 key->bfmi_nh_type = BIER_NH_UDP;
75 memcpy(&key->bfmi_nh, &rpath->frp_addr, sizeof(rpath->frp_addr));
76 key->bfmi_nh_type = BIER_NH_IP;
78 if (NULL == rpath->frp_label_stack)
80 key->bfmi_hdr_type = BIER_HDR_O_OTHER;
84 key->bfmi_hdr_type = BIER_HDR_O_MPLS;
90 bier_fmask_db_find (index_t bti,
91 const fib_route_path_t *rpath)
96 bier_fmask_db_mk_key(bti, rpath, &fmid);
97 p = hash_get_mem(bier_fmask_db.bfdb_hash, &fmid);
104 return (INDEX_INVALID);
108 bier_fmask_db_find_or_create_and_lock (index_t bti,
109 const fib_route_path_t *rpath)
111 bier_fmask_id_t fmid;
115 bier_fmask_db_mk_key(bti, rpath, &fmid);
116 p = hash_get_mem(bier_fmask_db.bfdb_hash, &fmid);
122 * adding a new fmask object
124 index = bier_fmask_create_and_lock(&fmid, rpath);
125 bfm = bier_fmask_get(index);
126 hash_set_mem(bier_fmask_db.bfdb_hash, bfm->bfm_id, index);
131 bier_fmask_lock(index);
138 bier_fmask_db_remove (const bier_fmask_id_t *fmid)
142 p = hash_get_mem(bier_fmask_db.bfdb_hash, fmid);
146 * remove a non-exitant entry - oops
148 ASSERT (!"remove non-existant fmask");
150 hash_unset(bier_fmask_db.bfdb_hash, fmid);
155 bier_fmask_db_walk (bier_fmask_walk_fn_t fn, void *ctx)
157 CLIB_UNUSED (bier_fmask_id_t *fmid);
160 hash_foreach(fmid, bfmi, bier_fmask_db.bfdb_hash,
162 if (WALK_STOP == fn(*bfmi, ctx))
168 bier_fmask_db_module_init (vlib_main_t *vm)
170 bier_fmask_db.bfdb_hash = hash_create_mem(0,
171 sizeof(bier_fmask_id_t),
177 VLIB_INIT_FUNCTION (bier_fmask_db_module_init);