FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
fib_node.c
Go to the documentation of this file.
1 /*
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/fib/fib_node.h>
17 #include <vnet/fib/fib_node_list.h>
18 #include <vnet/fib/fib_table.h>
19 #include <vnet/mfib/mfib_table.h>
20 
21 /*
22  * The per-type vector of virtual function tables
23  */
25 
26 /**
27  * The last registered new type
28  */
30 
31 /*
32  * the node type names
33  */
34 static const char *fn_type_names[] = FIB_NODE_TYPES;
35 
36 const char*
38 {
40  return (fn_type_names[type]);
41  else
42  {
43  if (NULL != fn_vfts[type].fnv_format)
44  {
45  return ("fixme");
46  }
47  else
48  {
49  return ("unknown");
50  }
51  }
52 }
53 
54 /**
55  * fib_node_register_type
56  *
57  * Register the function table for a given type
58  */
59 void
61  const fib_node_vft_t *vft)
62 {
63  /*
64  * assert that one only registration is made per-node type
65  */
66  if (vec_len(fn_vfts) > type)
67  ASSERT(NULL == fn_vfts[type].fnv_get);
68 
69  /*
70  * Assert that we are getting each of the required functions
71  */
72  ASSERT(NULL != vft->fnv_get);
73  ASSERT(NULL != vft->fnv_last_lock);
74 
76  fn_vfts[type] = *vft;
77 }
78 
81 {
82  fib_node_type_t new_type;
83 
84  new_type = ++last_new_type;
85 
86  fib_node_register_type(new_type, vft);
87 
88  return (new_type);
89 }
90 
91 static u8*
93 {
94  return (format(s, "{%s:%d}", fn_type_names[fnp->fnp_type], fnp->fnp_index));
95 }
96 
97 u32
99  fib_node_index_t parent_index,
102 {
103  fib_node_t *parent;
104 
105  parent = fn_vfts[parent_type].fnv_get(parent_index);
106 
107  /*
108  * return the index of the sibling in the child list
109  */
110  fib_node_lock(parent);
111 
112  if (FIB_NODE_INDEX_INVALID == parent->fn_children)
113  {
114  parent->fn_children = fib_node_list_create();
115  }
116 
117  return (fib_node_list_push_front(parent->fn_children,
118  0, type,
119  index));
120 }
121 
122 void
124  fib_node_index_t parent_index,
125  fib_node_index_t sibling_index)
126 {
127  fib_node_t *parent;
128 
129  parent = fn_vfts[parent_type].fnv_get(parent_index);
130 
131  fib_node_list_remove(parent->fn_children, sibling_index);
132 
133  if (0 == fib_node_list_get_size(parent->fn_children))
134  {
136  }
137 
138  fib_node_unlock(parent);
139 }
140 
141 u32
143  fib_node_index_t parent_index)
144 {
145  fib_node_t *parent;
146 
147  parent = fn_vfts[parent_type].fnv_get(parent_index);
148 
149  return (fib_node_list_get_size(parent->fn_children));
150 }
151 
152 
156 {
157  fib_node_t *node;
158 
159  node = fn_vfts[ptr->fnp_type].fnv_get(ptr->fnp_index);
160 
161  return (fn_vfts[ptr->fnp_type].fnv_back_walk(node, ctx));
162 }
163 
164 static walk_rc_t
166  void *arg)
167 {
168  u8 **s = (u8**) arg;
169 
170  *s = fib_node_format(ptr, *s);
171 
172  return (WALK_CONTINUE);
173 }
174 
175 u8*
177  u8 *s)
178 {
180 
181  return (s);
182 }
183 
184 void
187 {
188  /**
189  * The node's type. used to retrieve the VFT.
190  */
191  node->fn_type = type;
192  node->fn_locks = 0;
193  node->fn_children = FIB_NODE_INDEX_INVALID;
194 }
195 
196 void
198 {
199  fib_node_list_destroy(&node->fn_children);
200 }
201 
202 void
204 {
205  node->fn_locks++;
206 }
207 
208 void
210 {
211  node->fn_locks--;
212 
213  if (0 == node->fn_locks)
214  {
215  fn_vfts[node->fn_type].fnv_last_lock(node);
216  }
217 }
218 
219 void
221  u32 in_use_elts,
222  u32 allocd_elts,
223  size_t size_elt)
224 {
225  vlib_cli_output (vlib_get_main(), "%=30s %=5d %=8d/%=9d %d/%d ",
226  name, size_elt,
227  in_use_elts, allocd_elts,
228  in_use_elts*size_elt, allocd_elts*size_elt);
229 }
230 
231 static clib_error_t *
233  unformat_input_t * input,
234  vlib_cli_command_t * cmd)
235 {
236  fib_node_vft_t *vft;
237 
238  vlib_cli_output (vm, "FIB memory");
239  vlib_cli_output (vm, " Tables:");
240  vlib_cli_output (vm, "%=30s %=6s %=12s", "SAFI", "Number", "Bytes");
243  vlib_cli_output (vm, " Nodes:");
244  vlib_cli_output (vm, "%=30s %=5s %=8s/%=9s totals",
245  "Name","Size", "in-use", "allocated");
246 
247  vec_foreach(vft, fn_vfts)
248  {
249  if (NULL != vft->fnv_mem_show)
250  vft->fnv_mem_show();
251  }
252 
254 
255  return (NULL);
256 }
257 
258 /* *INDENT-OFF* */
259 /*?
260  * The '<em>sh fib memory </em>' command displays the memory usage for each
261  * FIB object type.
262  *
263  * @cliexpar
264  * @cliexstart{show fib memory}
265  *FIB memory
266  * Tables:
267  * SAFI Number Bytes
268  * IPv4 unicast 2 673066
269  * IPv6 unicast 2 1054608
270  * MPLS 1 4194312
271  * IPv4 multicast 2 2322
272  * IPv6 multicast 2 ???
273  * Nodes:
274  * Name Size in-use /allocated totals
275  * Entry 96 20 / 20 1920/1920
276  * Entry Source 32 0 / 0 0/0
277  * Entry Path-Extensions 60 0 / 0 0/0
278  * multicast-Entry 192 12 / 12 2304/2304
279  * Path-list 40 28 / 28 1120/1120
280  * uRPF-list 16 20 / 20 320/320
281  * Path 72 28 / 28 2016/2016
282  * Node-list elements 20 28 / 28 560/560
283  * Node-list heads 8 30 / 30 240/240
284  * @cliexend
285 ?*/
287  .path = "show fib memory",
288  .function = fib_memory_show,
289  .short_help = "show fib memory",
290 };
291 /* *INDENT-ON* */
WALK_CONTINUE
@ WALK_CONTINUE
Definition: interface_funcs.h:174
fib_node_back_walk_rc_t
enum fib_node_back_walk_rc_t_ fib_node_back_walk_rc_t
Return code from a back walk function.
fib_node_type_get_name
const char * fib_node_type_get_name(fib_node_type_t type)
Definition: fib_node.c:37
fib_node_vft_t_::fnv_last_lock
fib_node_last_lock_gone_t fnv_last_lock
Definition: fib_node.h:290
fib_node_ptr_t_::fnp_type
fib_node_type_t fnp_type
node type
Definition: fib_node.h:199
name
string name[64]
Definition: fib.api:25
fib_node_list_t
u32 fib_node_list_t
A list of FIB nodes.
Definition: fib_node.h:209
last_new_type
static fib_node_type_t last_new_type
The last registered new type.
Definition: fib_node.c:29
FIB_NODE_TYPE_LAST
@ FIB_NODE_TYPE_LAST
Definition: fib_node.h:57
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
fib_node_t_::fn_children
fib_node_list_t fn_children
Vector of nodes that depend upon/use/share this node.
Definition: fib_node.h:315
fib_node_vft_t_
A FIB graph nodes virtual function table.
Definition: fib_node.h:288
vlib_cli_command_t::path
char * path
Definition: cli.h:96
FIB_NODE_INDEX_INVALID
#define FIB_NODE_INDEX_INVALID
Definition: fib_types.h:30
fib_table.h
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
fib_node_children_format
u8 * fib_node_children_format(fib_node_list_t list, u8 *s)
Definition: fib_node.c:176
fib_node_list_memory_show
void fib_node_list_memory_show(void)
Definition: fib_node_list.c:381
unformat_input_t
struct _unformat_input_t unformat_input_t
fib_node.h
format_fib_table_memory
u8 * format_fib_table_memory(u8 *s, va_list *args)
format (display) the memory used by the FIB tables
Definition: fib_table.c:1539
fib_node_type_t
enum fib_node_type_t_ fib_node_type_t
The types of nodes in a FIB graph.
show_fib_memory
static vlib_cli_command_t show_fib_memory
(constructor) VLIB_CLI_COMMAND (show_fib_memory)
Definition: fib_node.c:286
fib_node_get_n_children
u32 fib_node_get_n_children(fib_node_type_t parent_type, fib_node_index_t parent_index)
Definition: fib_node.c:142
fib_memory_show
static clib_error_t * fib_memory_show(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: fib_node.c:232
fib_node_vft_t_::fnv_mem_show
fib_node_memory_show_t fnv_mem_show
Definition: fib_node.h:293
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
fib_node_index_t
u32 fib_node_index_t
A typedef of a node index.
Definition: fib_types.h:29
fib_show_memory_usage
void fib_show_memory_usage(const char *name, u32 in_use_elts, u32 allocd_elts, size_t size_elt)
Show the memory usage for a type.
Definition: fib_node.c:220
vec_validate
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment)
Definition: vec.h:523
VLIB_CLI_COMMAND
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:163
fib_node_format
static u8 * fib_node_format(fib_node_ptr_t *fnp, u8 *s)
Definition: fib_node.c:92
fib_node_register_type
void fib_node_register_type(fib_node_type_t type, const fib_node_vft_t *vft)
fib_node_register_type
Definition: fib_node.c:60
mfib_table.h
vlib_cli_output
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:716
fib_node_ptr_t_
A representation of one pointer to another node.
Definition: fib_node.h:195
fib_node_back_walk_one
fib_node_back_walk_rc_t fib_node_back_walk_one(fib_node_ptr_t *ptr, fib_node_back_walk_ctx_t *ctx)
Definition: fib_node.c:154
fib_node_ptr_t_::fnp_index
fib_node_index_t fnp_index
node's index
Definition: fib_node.h:203
index
u32 index
Definition: flow_types.api:221
format
description fragment has unexpected format
Definition: map.api:433
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
u32
unsigned int u32
Definition: types.h:88
fib_node_list_remove
void fib_node_list_remove(fib_node_list_t list, u32 sibling)
Definition: fib_node_list.c:234
FIB_NODE_TYPES
#define FIB_NODE_TYPES
Definition: fib_node.h:62
fib_node_list_get_size
u32 fib_node_list_get_size(fib_node_list_t list)
Definition: fib_node_list.c:313
ctx
long ctx[MAX_CONNS]
Definition: main.c:144
vec_foreach
#define vec_foreach(var, vec)
Vector iterator.
Definition: vec_bootstrap.h:213
fib_node_list_walk
void fib_node_list_walk(fib_node_list_t list, fib_node_list_walk_cb_t fn, void *args)
Walk the list of node.
Definition: fib_node_list.c:354
fib_node_lock
void fib_node_lock(fib_node_t *node)
Definition: fib_node.c:203
fib_node_t_
An node in the FIB graph.
Definition: fib_node.h:301
vlib_main_t
Definition: main.h:102
fib_node_init
void fib_node_init(fib_node_t *node, fib_node_type_t type)
Definition: fib_node.c:185
vlib_get_main
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:38
fib_node_list_push_front
u32 fib_node_list_push_front(fib_node_list_t list, int owner_id, fib_node_type_t type, fib_node_index_t index)
Insert an element at the from of the list.
Definition: fib_node_list.c:156
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
fib_node_unlock
void fib_node_unlock(fib_node_t *node)
Definition: fib_node.c:209
fib_node_list_create
fib_node_list_t fib_node_list_create(void)
Create a new node list.
Definition: fib_node_list.c:125
fn_type_names
static const char * fn_type_names[]
Definition: fib_node.c:34
fib_node_back_walk_ctx_t_
Context passed between object during a back walk.
Definition: fib_node.h:214
fib_node_vft_t_::fnv_get
fib_node_get_t fnv_get
Definition: fib_node.h:289
fib_node_vft_t_::fnv_back_walk
fib_node_back_walk_t fnv_back_walk
Definition: fib_node.h:291
fib_node_child_remove
void fib_node_child_remove(fib_node_type_t parent_type, fib_node_index_t parent_index, fib_node_index_t sibling_index)
Definition: fib_node.c:123
fib_node_list_destroy
void fib_node_list_destroy(fib_node_list_t *list)
Definition: fib_node_list.c:137
fib_node_list.h
fib_node_ptr_format_one_child
static walk_rc_t fib_node_ptr_format_one_child(fib_node_ptr_t *ptr, void *arg)
Definition: fib_node.c:165
fib_node_deinit
void fib_node_deinit(fib_node_t *node)
Definition: fib_node.c:197
fn_vfts
static fib_node_vft_t * fn_vfts
Definition: fib_node.c:24
vlib_cli_command_t
Definition: cli.h:92
walk_rc_t
enum walk_rc_t_ walk_rc_t
Walk return code.
fib_node_register_new_type
fib_node_type_t fib_node_register_new_type(const fib_node_vft_t *vft)
Create a new FIB node type and Register the function table for it.
Definition: fib_node.c:80
format_mfib_table_memory
u8 * format_mfib_table_memory(u8 *s, va_list *args)
format (display) the memory usage for mfibs
Definition: mfib_table.c:882
fib_node_child_add
u32 fib_node_child_add(fib_node_type_t parent_type, fib_node_index_t parent_index, fib_node_type_t type, fib_node_index_t index)
Definition: fib_node.c:98
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123