FD.io VPP  v20.01-48-g3e0dafb74
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 {
51  ASSERT(vec_len(fib_source_regs) > src);
52 
53  return (((u16)fib_source_regs[src].fsr_prio.fsp_class << 8) |
54  fib_source_regs[src].fsr_prio.fsp_slot);
55 }
56 
59 {
60  ASSERT(vec_len(fib_source_regs) > src);
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 
70  ASSERT(vec_len(fib_source_regs) > src);
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 
107  vec_validate(fib_source_regs, src);
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 {
141  fib_source_reg_init(src, fib_source_names[src], prio, bh);
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 
174  vec_foreach(fsr, fib_source_regs)
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  {
202  vlib_cli_output(vm, "%U", format_fib_source_reg, fsr);
203  }
204  vec_free(fsrs);
205 
206  return (NULL);
207 }
208 
209 VLIB_CLI_COMMAND (show_fib_sources, static) = {
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 }
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:440
enum fib_source_t_ fib_source_t
The different sources that can create a route.
static fib_source_reg_t * fib_source_regs
Definition: fib_source.c:45
u16 fib_source_get_prio(fib_source_t src)
Definition: fib_source.c:49
a
Definition: bitmap.h:538
static const char * fib_source_behaviour_names[]
Definition: fib_source.c:19
static fib_source_priority_t fib_source_prio_by_class[0x100]
Definition: fib_source.c:35
#define NULL
Definition: clib.h:58
#define foreach_fib_source
The fixed source to priority mappings.
Definition: fib_source.h:244
void fib_source_walk(fib_source_walk_t fn, void *ctx)
Definition: fib_source.c:169
vl_api_address_t src
Definition: gre.api:60
enum fib_source_behaviour_t_ fib_source_behaviour_t
Each source has a defined behaviour that controls how entries behave that have that source...
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
u8 fib_source_priority_t
Each source is assigned a priority.
Definition: fib_source.h:179
unsigned char u8
Definition: types.h:56
fib_source_priority_cmp_t fib_source_cmp(fib_source_t s1, fib_source_t s2)
Definition: fib_source.c:76
void fib_source_register(fib_source_t src, fib_source_priority_t prio, fib_source_behaviour_t bh)
Definition: fib_source.c:137
struct fib_source_prio_t_ fib_source_prio_t
fib_source_t fsr_source
Definition: fib_source.c:39
#define FIB_SOURCE_BEHAVIOURS
Definition: fib_source.h:227
fib_source_t fib_source_allocate(const char *name, fib_source_priority_t prio, fib_source_behaviour_t bh)
Definition: fib_source.c:118
long ctx[MAX_CONNS]
Definition: main.c:144
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:376
vlib_main_t * vm
Definition: in2out_ed.c:1810
u8 slot
Definition: pci_types.api:22
#define UNFORMAT_END_OF_INPUT
Definition: format.h:145
void fib_source_module_init(void)
Definition: fib_source.c:217
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:342
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_priority_t fsp_class
Definition: fib_source.c:25
string name[64]
Definition: ip.api:44
static u8 * format_fib_source_reg(u8 *s, va_list *a)
Definition: fib_source.c:145
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:152
const char * fsr_name
Definition: fib_source.c:40
fib_source_behaviour_t fib_source_get_behaviour(fib_source_t src)
Definition: fib_source.c:58
#define ASSERT(truth)
fib_source_behaviour_t fsr_behaviour
Definition: fib_source.c:41
u8 * format_fib_source(u8 *s, va_list *a)
Definition: fib_source.c:66
fib_source_prio_t fsr_prio
Definition: fib_source.c:42
static int fib_source_reg_cmp_for_sort(void *v1, void *v2)
Definition: fib_source.c:159
#define FIB_SOURCES
Definition: fib_source.h:148
struct fib_source_reg_t_ fib_source_reg_t
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
An invalid source This is not a real source, so don&#39;t use it to source a prefix.
Definition: fib_source.h:32
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:983
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
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:292
#define vec_foreach(var, vec)
Vector iterator.
static fib_source_t fib_source_id
Definition: fib_source.c:21
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:689
static const char * fib_source_names[]
Definition: fib_source.c:18
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...
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
enum fib_source_priority_cmp_t_ fib_source_priority_cmp_t
source comparison
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:171
fib_source_priority_t fsp_slot
Definition: fib_source.c:26