FD.io VPP  v17.01-9-ge7dcee4
Vector Packet Processing
fib_urpf_list.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_urpf_list.h>
17 #include <vnet/adj/adj.h>
18 
19 /**
20  * @brief pool of all fib_urpf_list
21  */
23 
24 u8 *
25 format_fib_urpf_list (u8 *s, va_list args)
26 {
27  fib_urpf_list_t *urpf;
28  index_t ui;
29  u32 *swi;
30 
31  ui = va_arg(args, index_t);
32  urpf = fib_urpf_list_get(ui);
33 
34  s = format(s, "uPRF-list:%d len:%d itfs:[",
35  ui, vec_len(urpf->furpf_itfs));
36 
37  vec_foreach(swi, urpf->furpf_itfs)
38  {
39  s = format(s, "%d, ", *swi);
40  }
41  s = format(s, "]");
42 
43  return (s);
44 }
45 
46 index_t
48 {
49  fib_urpf_list_t *urpf;
50 
51  pool_get(fib_urpf_list_pool, urpf);
52  memset(urpf, 0, sizeof(*urpf));
53 
54  urpf->furpf_locks++;
55 
56  return (urpf - fib_urpf_list_pool);
57 }
58 
59 void
61 {
62  fib_urpf_list_t *urpf;
63 
64  if (INDEX_INVALID == ui)
65  return;
66 
67  urpf = fib_urpf_list_get(ui);
68 
69  urpf->furpf_locks--;
70 
71  if (0 == urpf->furpf_locks)
72  {
73  vec_free(urpf->furpf_itfs);
74  pool_put(fib_urpf_list_pool, urpf);
75  }
76 }
77 
78 void
80 {
81  fib_urpf_list_t *urpf;
82 
83  urpf = fib_urpf_list_get(ui);
84 
85  urpf->furpf_locks++;
86 }
87 
88 /**
89  * @brief Append another interface to the list.
90  */
91 void
93  u32 sw_if_index)
94 {
95  fib_urpf_list_t *urpf;
96 
97  urpf = fib_urpf_list_get(ui);
98 
99  vec_add1(urpf->furpf_itfs, sw_if_index);
100 }
101 
102 /**
103  * @brief Combine to interface lists
104  */
105 void
107  index_t ui2)
108 {
109  fib_urpf_list_t *urpf1, *urpf2;
110 
111  urpf1 = fib_urpf_list_get(ui1);
112  urpf2 = fib_urpf_list_get(ui2);
113 
114  vec_append(urpf1->furpf_itfs, urpf2->furpf_itfs);
115 }
116 
117 /**
118  * @brief Sort the interface indicies.
119  * The sort is the first step in obtaining a unique list, so the order,
120  * w.r.t. next-hop, interface,etc is not important. So a sort based on the
121  * index is all we need.
122  */
123 static int
125  void * v2)
126 {
127  fib_node_index_t *i1 = v1, *i2 = v2;
128 
129  return (*i2 < *i1);
130 }
131 
132 /**
133  * @brief Convert the uRPF list from the itf set obtained during the walk
134  * to a unique list.
135  */
136 void
138 {
139  fib_urpf_list_t *urpf;
140 
141  urpf = fib_urpf_list_get(ui);
142 
144 
145  if (vec_len(urpf->furpf_itfs) > 1)
146  {
147  u32 i,j;
148 
149  /*
150  * cat list | sort | uniq > rpf_list
151  */
153 
154  i = 0, j = 1;
155  while (j < vec_len(urpf->furpf_itfs))
156  {
157  if (urpf->furpf_itfs[i] == urpf->furpf_itfs[j])
158  {
159  /*
160  * the itfacenct entries are the same.
161  * search forward for a unique one
162  */
163  while (urpf->furpf_itfs[i] == urpf->furpf_itfs[j] &&
164  j < vec_len(urpf->furpf_itfs))
165  {
166  j++;
167  }
168  if (j == vec_len(urpf->furpf_itfs))
169  {
170  /*
171  * ran off the end without finding a unique index.
172  * we are done.
173  */
174  break;
175  }
176  else
177  {
178  urpf->furpf_itfs[i+1] = urpf->furpf_itfs[j];
179  }
180  }
181  i++, j++;
182  }
183 
184  /*
185  * set the length of the vector to the number of unique itfs
186  */
187  _vec_len(urpf->furpf_itfs) = i+1;
188  }
189 
191 }
192 
193 void
195 {
196  fib_show_memory_usage("uRPF-list",
197  pool_elts(fib_urpf_list_pool),
198  pool_len(fib_urpf_list_pool),
199  sizeof(fib_urpf_list_t));
200 }
201 
202 static clib_error_t *
204  unformat_input_t * input,
205  vlib_cli_command_t * cmd)
206 {
207  index_t ui;
208 
209  if (unformat (input, "%d", &ui))
210  {
211  /*
212  * show one in detail
213  */
214  if (!pool_is_free_index(fib_urpf_list_pool, ui))
215  {
216  vlib_cli_output (vm, "%d@%U",
217  ui,
219  }
220  else
221  {
222  vlib_cli_output (vm, "uRPF %d invalid", ui);
223  }
224  }
225  else
226  {
227  /*
228  * show all
229  */
230  vlib_cli_output (vm, "FIB uRPF Entries:");
231  pool_foreach_index(ui, fib_urpf_list_pool,
232  ({
233  vlib_cli_output (vm, "%d@%U",
234  ui,
236  }));
237  }
238 
239  return (NULL);
240 }
241 
242 /* *INDENT-OFF* */
243 /*?
244  * The '<em>sh fib uRPF [index] </em>' command displays the uRPF lists
245  *
246  * @cliexpar
247  * @cliexstart{show fib uRPF}
248  * FIB uRPF Entries:
249  * 0@uPRF-list:0 len:0 itfs:[]
250  * 1@uPRF-list:1 len:2 itfs:[1, 2, ]
251  * 2@uPRF-list:2 len:1 itfs:[3, ]
252  * 3@uPRF-list:3 len:1 itfs:[9, ]
253  * @cliexend
254 ?*/
255 VLIB_CLI_COMMAND (show_fib_urpf_list, static) = {
256  .path = "show fib uRPF",
257  .function = show_fib_urpf_list_command,
258  .short_help = "show fib uRPF",
259 };
260 /* *INDENT-OFF* */
static int fib_urpf_itf_cmp_for_sort(void *v1, void *v2)
Sort the interface indicies.
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
u8 * format_fib_urpf_list(u8 *s, va_list args)
Definition: fib_urpf_list.c:25
#define NULL
Definition: clib.h:55
void fib_urpf_list_show_mem(void)
u32 index_t
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.h:41
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:482
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:200
#define pool_len(p)
Number of elements in pool vector.
Definition: pool.h:121
index_t fib_urpf_list_alloc_and_lock(void)
Definition: fib_urpf_list.c:47
void fib_urpf_list_lock(index_t ui)
Definition: fib_urpf_list.c:79
static fib_urpf_list_t * fib_urpf_list_get(index_t index)
Definition: fib_urpf_list.h:96
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:221
adj_index_t * furpf_itfs
The list of interfaces that comprise the allowed accepting interfaces.
Definition: fib_urpf_list.h:63
void fib_urpf_list_append(index_t ui, u32 sw_if_index)
Append another interface to the list.
Definition: fib_urpf_list.c:92
fib_urpf_list_flag_t furpf_flags
flags
Definition: fib_urpf_list.h:68
Set to indicated that the uRPF list has already been baked.
Definition: fib_urpf_list.h:55
static clib_error_t * show_fib_urpf_list_command(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:214
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:576
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
void fib_urpf_list_bake(index_t ui)
Convert the uRPF list from the itf set obtained during the walk to a unique list. ...
u32 fib_node_index_t
A typedef of a node index.
Definition: fib_types.h:28
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:211
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
void fib_urpf_list_combine(index_t ui1, index_t ui2)
Combine to interface lists.
#define vec_append(v1, v2)
Append v2 after v1.
Definition: vec.h:779
fib_urpf_list_t * fib_urpf_list_pool
pool of all fib_urpf_list
Definition: fib_urpf_list.c:22
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
#define INDEX_INVALID
Invalid index - used when no index is known blazoned capitals INVALID speak volumes where ~0 does not...
Definition: dpo.h:47
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:920
u32 furpf_locks
uRPF lists are shared amongst many entries so we require a locking mechanism.
Definition: fib_urpf_list.h:74
void fib_urpf_list_unlock(index_t ui)
Definition: fib_urpf_list.c:60
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
#define vec_foreach(var, vec)
Vector iterator.
struct _unformat_input_t unformat_input_t
#define pool_foreach_index(i, v, body)
Iterate pool by index.
Definition: pool.h:390
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:109