FD.io VPP  v21.10.1-2-g0a485f517
Vector Packet Processing
fib_source.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_source.h>
17 
18 static const char *fib_source_names[] = FIB_SOURCES;
20 
22 
23 typedef struct fib_source_prio_t_
24 {
28 
29 /**
30  * for each client requested priority count the number pf uses of
31  * that prio so we can asign is usage a slot number, and therefore
32  * each request will have a unique value.
33  */
36 
37 typedef struct fib_source_reg_t_
38 {
40  const char *fsr_name;
44 
46 
47 
48 u16
50 {
52 
53  return (((u16)fib_source_regs[src].fsr_prio.fsp_class << 8) |
55 }
56 
59 {
61 
62  return (fib_source_regs[src].fsr_behaviour);
63 }
64 
65 u8 *
66 format_fib_source (u8 *s, va_list *a)
67 {
68  fib_source_t src = va_arg(*a, int);
69 
71 
72  return (format(s, "%s", fib_source_regs[src].fsr_name));
73 }
74 
77  fib_source_t s2)
78 {
79  if (fib_source_get_prio(s1) <
81  {
82  return (FIB_SOURCE_CMP_BETTER);
83  }
84  else if (fib_source_get_prio(s1) >
86  {
87  return (FIB_SOURCE_CMP_WORSE);
88  }
89  return (FIB_SOURCE_CMP_EQUAL);
90 }
91 
92 static void
94  const char *name,
97 {
99  fib_source_reg_t *fsr;
100 
101  /*
102  * ensure we assign a unique priority to each request
103  * otherwise different source will be treated like ECMP
104  */
105  slot = fib_source_prio_by_class[prio]++;
106 
108 
109  fsr = &fib_source_regs[src];
110  fsr->fsr_source = src;
111  fsr->fsr_name = strdup(name);
112  fsr->fsr_prio.fsp_class = prio;
113  fsr->fsr_prio.fsp_slot = slot;
114  fsr->fsr_behaviour = bh;
115 }
116 
121 {
123 
124  // max value range
125  ASSERT(fib_source_id < 255);
126  if (fib_source_id == 255)
127  return (FIB_SOURCE_INVALID);
128 
129  src = fib_source_id++;
130 
131  fib_source_reg_init(src, name, prio, bh);
132 
133  return (src);
134 }
135 
136 void
140 {
142 }
143 
144 static u8 *
145 format_fib_source_reg (u8 *s, va_list *a)
146 {
147  fib_source_reg_t *fsr = va_arg(*a, fib_source_reg_t*);
148 
149  s = format(s, "[%d] %U prio:%d.%d behaviour:%s",
150  fsr->fsr_source,
152  fsr->fsr_prio.fsp_class, fsr->fsr_prio.fsp_slot,
154 
155  return (s);
156 }
157 
158 static int
160  void * v2)
161 {
162  fib_source_reg_t *fsr1 = v1, *fsr2 = v2;
163 
164  return (fib_source_get_prio(fsr1->fsr_source) -
165  fib_source_get_prio(fsr2->fsr_source));
166 }
167 
168 void
170  void *ctx)
171 {
172  fib_source_reg_t *fsr;
173 
175  {
176  if (WALK_STOP == fn(fsr->fsr_source,
177  fsr->fsr_name,
178  fsr->fsr_prio.fsp_class,
179  fsr->fsr_behaviour,
180  ctx))
181  break;
182  }
183 }
184 
185 static clib_error_t *
187  unformat_input_t * input,
188  vlib_cli_command_t * cmd)
189 {
190  fib_source_reg_t *fsr, *fsrs;
191 
192  fsrs = vec_dup(fib_source_regs);
193 
195  {
196  if (unformat (input, "prio") ||
197  unformat (input, "priority"))
199  }
200  vec_foreach(fsr, fsrs)
201  {
203  }
204  vec_free(fsrs);
205 
206  return (NULL);
207 }
208 
210  .path = "show fib source",
211  .function = fib_source_show,
212  .short_help = "show fib source [prio]",
213 };
214 
215 
216 void
218 {
219 #define _(s,p,b) fib_source_register(s,p,b);
221 #undef _
222 }
fib_source.h
fib_source_prio_t_::fsp_slot
fib_source_priority_t fsp_slot
Definition: fib_source.c:26
FIB_SOURCE_CMP_WORSE
@ FIB_SOURCE_CMP_WORSE
Definition: fib_source.h:190
fib_source_names
static const char * fib_source_names[]
Definition: fib_source.c:18
fib_source_prio_t
struct fib_source_prio_t_ fib_source_prio_t
show_fib_sources
static vlib_cli_command_t show_fib_sources
(constructor) VLIB_CLI_COMMAND (show_fib_sources)
Definition: fib_source.c:209
fib_source_get_behaviour
fib_source_behaviour_t fib_source_get_behaviour(fib_source_t src)
Definition: fib_source.c:58
name
string name[64]
Definition: fib.api:25
fib_source_register
void fib_source_register(fib_source_t src, fib_source_priority_t prio, fib_source_behaviour_t bh)
Definition: fib_source.c:137
FIB_SOURCES
#define FIB_SOURCES
Definition: fib_source.h:151
vlib_cli_command_t::path
char * path
Definition: cli.h:96
u16
unsigned short u16
Definition: types.h:57
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
fib_source_regs
static fib_source_reg_t * fib_source_regs
Definition: fib_source.c:45
fib_source_walk_t
walk_rc_t(* fib_source_walk_t)(fib_source_t id, const char *name, fib_source_priority_t prio, fib_source_behaviour_t bh, void *ctx)
Definition: fib_source.h:303
fib_source_cmp
fib_source_priority_cmp_t fib_source_cmp(fib_source_t s1, fib_source_t s2)
Definition: fib_source.c:76
fib_source_prio_t_
Definition: fib_source.c:23
unformat_input_t
struct _unformat_input_t unformat_input_t
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
FIB_SOURCE_CMP_EQUAL
@ FIB_SOURCE_CMP_EQUAL
Definition: fib_source.h:191
slot
u8 slot
Definition: pci_types.api:22
FIB_SOURCE_INVALID
@ FIB_SOURCE_INVALID
An invalid source This is not a real source, so don't use it to source a prefix.
Definition: fib_source.h:35
vec_dup
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:444
fib_source_behaviour_names
static const char * fib_source_behaviour_names[]
Definition: fib_source.c:19
unformat_check_input
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:163
fib_source_reg_t_::fsr_behaviour
fib_source_behaviour_t fsr_behaviour
Definition: fib_source.c:41
fib_source_get_prio
u16 fib_source_get_prio(fib_source_t src)
Definition: fib_source.c:49
fib_source_show
static clib_error_t * fib_source_show(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: fib_source.c:186
fib_source_reg_t_::fsr_source
fib_source_t fsr_source
Definition: fib_source.c:39
fib_source_id
static fib_source_t fib_source_id
Definition: fib_source.c:21
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
src
vl_api_address_t src
Definition: gre.api:54
FIB_SOURCE_LAST
@ FIB_SOURCE_LAST
Marker.
Definition: fib_source.h:145
fib_source_behaviour_t
enum fib_source_behaviour_t_ fib_source_behaviour_t
Each source has a defined behaviour that controls how entries behave that have that source.
format_fib_source_reg
static u8 * format_fib_source_reg(u8 *s, va_list *a)
Definition: fib_source.c:145
vlib_cli_output
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:716
fib_source_allocate
fib_source_t fib_source_allocate(const char *name, fib_source_priority_t prio, fib_source_behaviour_t bh)
Definition: fib_source.c:118
fib_source_walk
void fib_source_walk(fib_source_walk_t fn, void *ctx)
Definition: fib_source.c:169
fib_source_priority_t
u8 fib_source_priority_t
Each source is assigned a priority.
Definition: fib_source.h:182
fib_source_reg_init
static void fib_source_reg_init(fib_source_t src, const char *name, fib_source_priority_t prio, fib_source_behaviour_t bh)
Definition: fib_source.c:93
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
fib_source_prio_t_::fsp_class
fib_source_priority_t fsp_class
Definition: fib_source.c:25
fib_source_reg_cmp_for_sort
static int fib_source_reg_cmp_for_sort(void *v1, void *v2)
Definition: fib_source.c:159
fib_source_reg_t_::fsr_prio
fib_source_prio_t fsr_prio
Definition: fib_source.c:42
format
description fragment has unexpected format
Definition: map.api:433
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
ctx
long ctx[MAX_CONNS]
Definition: main.c:144
vec_foreach
#define vec_foreach(var, vec)
Vector iterator.
Definition: vec_bootstrap.h:213
FIB_SOURCE_BEHAVIOURS
#define FIB_SOURCE_BEHAVIOURS
Definition: fib_source.h:238
STATIC_ASSERT_SIZEOF
STATIC_ASSERT_SIZEOF(fib_source_priority_t, 1)
for each client requested priority count the number pf uses of that prio so we can asign is usage a s...
fib_source_reg_t_
Definition: fib_source.c:37
vec_sort_with_function
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:1097
vlib_main_t
Definition: main.h:102
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
a
a
Definition: bitmap.h:525
format_fib_source
u8 * format_fib_source(u8 *s, va_list *a)
Definition: fib_source.c:66
FIB_SOURCE_CMP_BETTER
@ FIB_SOURCE_CMP_BETTER
Definition: fib_source.h:189
fib_source_t
enum fib_source_t_ fib_source_t
The different sources that can create a route.
foreach_fib_source
#define foreach_fib_source
The fixed source to priority mappings.
Definition: fib_source.h:255
fib_source_reg_t_::fsr_name
const char * fsr_name
Definition: fib_source.c:40
vlib_cli_command_t
Definition: cli.h:92
fib_source_reg_t
struct fib_source_reg_t_ fib_source_reg_t
fib_source_prio_by_class
static fib_source_priority_t fib_source_prio_by_class[0x100]
Definition: fib_source.c:35
fib_source_module_init
void fib_source_module_init(void)
Definition: fib_source.c:217
WALK_STOP
@ WALK_STOP
Definition: interface_funcs.h:173
fib_source_priority_cmp_t
enum fib_source_priority_cmp_t_ fib_source_priority_cmp_t
source comparison
UNFORMAT_END_OF_INPUT
#define UNFORMAT_END_OF_INPUT
Definition: format.h:137