blob: 67d3bd1cf859f8236fb2170965448b84ac32b482 [file] [log] [blame]
Neale Rannsd792d9c2017-10-21 10:53:20 -07001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include <vnet/bier/bier_fmask_db.h>
17#include <vnet/bier/bier_fmask.h>
18
19/**
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.
25 *
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.
29 */
30typedef struct bier_fmask_db_t_ {
31 /**
32 * hash table for underlying storage
33 */
34 mhash_t bfdb_hash;
35
36 /**
37 * Pool for memory
38 */
39 struct bier_fmask_t_ *bfdb_pool;
40} bier_fmask_db_t;
41
42/**
Neale Rannsd792d9c2017-10-21 10:53:20 -070043 * Single fmask DB
44 */
45static bier_fmask_db_t bier_fmask_db;
46
47
48u32
49bier_fmask_get_index (const bier_fmask_t *bfm)
50{
51 return (bfm - bier_fmask_db.bfdb_pool);
52}
53
Neale Ranns91286372017-12-05 13:24:04 -080054static void
55bier_fmask_db_mk_key (index_t bti,
56 const fib_route_path_t *rpath,
57 bier_fmask_id_t *key)
58{
59 /*
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
62 */
63 memset(key, 0, sizeof(*key));
64
65 /*
66 * Pick the attributes from the path that make the FMask unique
67 */
68 if (FIB_ROUTE_PATH_UDP_ENCAP & rpath->frp_flags)
69 {
70 key->bfmi_id = rpath->frp_udp_encap_id;
71 }
72 else
73 {
74 key->bfmi_sw_if_index = rpath->frp_sw_if_index;
75 memcpy(&key->bfmi_nh, &rpath->frp_addr, sizeof(rpath->frp_addr));
76 }
77 if (NULL == rpath->frp_label_stack)
78 {
79 key->bfmi_hdr_type = BIER_HDR_O_OTHER;
80 }
81 else
82 {
83 key->bfmi_hdr_type = BIER_HDR_O_MPLS;
84 }
85}
86
87u32
88bier_fmask_db_find (index_t bti,
89 const fib_route_path_t *rpath)
90{
91 bier_fmask_id_t fmid;
92 uword *p;
93
94 bier_fmask_db_mk_key(bti, rpath, &fmid);
95 p = mhash_get(&bier_fmask_db.bfdb_hash, &fmid);
96
97 if (NULL != p)
98 {
99 return (p[0]);
100 }
101
102 return (INDEX_INVALID);
103}
104
Neale Rannsd792d9c2017-10-21 10:53:20 -0700105u32
106bier_fmask_db_find_or_create_and_lock (index_t bti,
Neale Rannsd792d9c2017-10-21 10:53:20 -0700107 const fib_route_path_t *rpath)
108{
Neale Ranns91286372017-12-05 13:24:04 -0800109 bier_fmask_id_t fmid;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700110 u32 index;
111 uword *p;
112
Neale Ranns91286372017-12-05 13:24:04 -0800113 bier_fmask_db_mk_key(bti, rpath, &fmid);
114 p = mhash_get(&bier_fmask_db.bfdb_hash, &fmid);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700115
116 if (NULL == p)
117 {
Neale Ranns91286372017-12-05 13:24:04 -0800118 bier_fmask_t *bfm;
Neale Rannsd792d9c2017-10-21 10:53:20 -0700119 /*
120 * adding a new fmask object
121 */
Neale Ranns91286372017-12-05 13:24:04 -0800122 index = bier_fmask_create_and_lock(&fmid, rpath);
123 bfm = bier_fmask_get(index);
124 mhash_set(&bier_fmask_db.bfdb_hash, bfm->bfm_id, index, 0);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700125 }
126 else
127 {
128 index = p[0];
129 bier_fmask_lock(index);
130 }
131
132 return (index);
133}
134
Neale Rannsd792d9c2017-10-21 10:53:20 -0700135void
Neale Ranns91286372017-12-05 13:24:04 -0800136bier_fmask_db_remove (const bier_fmask_id_t *fmid)
Neale Rannsd792d9c2017-10-21 10:53:20 -0700137{
Neale Rannsd792d9c2017-10-21 10:53:20 -0700138 uword *p;
139
Neale Ranns91286372017-12-05 13:24:04 -0800140 p = mhash_get(&bier_fmask_db.bfdb_hash, fmid);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700141
142 if (NULL == p) {
143 /*
144 * remove a non-exitant entry - oops
145 */
146 ASSERT (!"remove non-existant fmask");
147 } else {
Neale Ranns91286372017-12-05 13:24:04 -0800148 mhash_unset(&(bier_fmask_db.bfdb_hash), (void*)fmid, 0);
Neale Rannsd792d9c2017-10-21 10:53:20 -0700149 }
150}
151
152clib_error_t *
153bier_fmask_db_module_init (vlib_main_t *vm)
154{
Neale Ranns91286372017-12-05 13:24:04 -0800155 mhash_init(&bier_fmask_db.bfdb_hash,
156 sizeof(index_t),
157 sizeof(bier_fmask_id_t));
Neale Rannsd792d9c2017-10-21 10:53:20 -0700158
159 return (NULL);
160}
161
162VLIB_INIT_FUNCTION (bier_fmask_db_module_init);