FD.io VPP  v21.06-3-gbb25fbf28
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 
33  if (INDEX_INVALID != ui)
34  {
35  urpf = fib_urpf_list_get(ui);
36 
37  s = format(s, "uPRF-list:%d len:%d itfs:[",
38  ui, vec_len(urpf->furpf_itfs));
39 
40  vec_foreach(swi, urpf->furpf_itfs)
41  {
42  s = format(s, "%d, ", *swi);
43  }
44  s = format(s, "]");
45  }
46  else
47  {
48  s = format(s, "uRPF-list: None");
49  }
50 
51  return (s);
52 }
53 
54 index_t
56 {
57  fib_urpf_list_t *urpf;
58 
60  clib_memset(urpf, 0, sizeof(*urpf));
61 
62  urpf->furpf_locks++;
63 
64  return (urpf - fib_urpf_list_pool);
65 }
66 
67 void
69 {
70  fib_urpf_list_t *urpf;
71 
72  if (INDEX_INVALID == ui)
73  return;
74 
75  urpf = fib_urpf_list_get(ui);
76 
77  urpf->furpf_locks--;
78 
79  if (0 == urpf->furpf_locks)
80  {
81  vec_free(urpf->furpf_itfs);
83  }
84 }
85 
86 void
88 {
89  fib_urpf_list_t *urpf;
90 
91  urpf = fib_urpf_list_get(ui);
92 
93  urpf->furpf_locks++;
94 }
95 
96 /**
97  * @brief Append another interface to the list.
98  */
99 void
102 {
103  fib_urpf_list_t *urpf;
104 
105  urpf = fib_urpf_list_get(ui);
106 
108 }
109 
110 /**
111  * @brief Combine to interface lists
112  */
113 void
115  index_t ui2)
116 {
117  fib_urpf_list_t *urpf1, *urpf2;
118 
119  urpf1 = fib_urpf_list_get(ui1);
120  urpf2 = fib_urpf_list_get(ui2);
121 
122  vec_append(urpf1->furpf_itfs, urpf2->furpf_itfs);
123 }
124 
125 /**
126  * @brief Sort the interface indicies.
127  * The sort is the first step in obtaining a unique list, so the order,
128  * w.r.t. next-hop, interface,etc is not important. So a sort based on the
129  * index is all we need.
130  */
131 static int
133  void * v2)
134 {
135  const adj_index_t *i1 = v1, *i2 = v2;
136  return (*i2 < *i1);
137 }
138 
139 /**
140  * @brief Convert the uRPF list from the itf set obtained during the walk
141  * to a unique list.
142  */
143 void
145 {
146  fib_urpf_list_t *urpf;
147 
148  urpf = fib_urpf_list_get(ui);
149 
151 
152  if (vec_len(urpf->furpf_itfs) > 1)
153  {
154  u32 i, j;
155  /*
156  * cat list | sort | uniq > rpf_list
157  */
158  /* sort */
160  /* remove duplicates */
161  i = 0;
162  for (j=1; j<vec_len(urpf->furpf_itfs); j++)
163  if (urpf->furpf_itfs[i] != urpf->furpf_itfs[j])
164  urpf->furpf_itfs[++i] = urpf->furpf_itfs[j];
165  /* set the length of the vector to the number of unique itfs */
166  _vec_len(urpf->furpf_itfs) = i+1;
167  }
168 
170 }
171 
172 void
174 {
175  fib_show_memory_usage("uRPF-list",
178  sizeof(fib_urpf_list_t));
179 }
180 
181 static clib_error_t *
183  unformat_input_t * input,
184  vlib_cli_command_t * cmd)
185 {
186  index_t ui;
187 
188  if (unformat (input, "%d", &ui))
189  {
190  /*
191  * show one in detail
192  */
194  {
195  vlib_cli_output (vm, "%d@%U",
196  ui,
198  }
199  else
200  {
201  vlib_cli_output (vm, "uRPF %d invalid", ui);
202  }
203  }
204  else
205  {
206  /*
207  * show all
208  */
209  vlib_cli_output (vm, "FIB uRPF Entries:");
211  {
212  vlib_cli_output (vm, "%d@%U",
213  ui,
215  }
216  }
217 
218  return (NULL);
219 }
220 
221 /* *INDENT-OFF* */
222 /*?
223  * The '<em>sh fib uRPF [index] </em>' command displays the uRPF lists
224  *
225  * @cliexpar
226  * @cliexstart{show fib uRPF}
227  * FIB uRPF Entries:
228  * 0@uPRF-list:0 len:0 itfs:[]
229  * 1@uPRF-list:1 len:2 itfs:[1, 2, ]
230  * 2@uPRF-list:2 len:1 itfs:[3, ]
231  * 3@uPRF-list:3 len:1 itfs:[9, ]
232  * @cliexend
233 ?*/
235  .path = "show fib uRPF",
236  .function = show_fib_urpf_list_command,
237  .short_help = "show fib uRPF",
238 };
239 /* *INDENT-OFF* */
adj.h
fib_urpf_list_t_::furpf_flags
fib_urpf_list_flag_t furpf_flags
flags
Definition: fib_urpf_list.h:68
vlib_cli_command_t::path
char * path
Definition: cli.h:96
fib_urpf_list_t_::furpf_locks
u32 furpf_locks
uRPF lists are shared amongst many entries so we require a locking mechanism.
Definition: fib_urpf_list.h:74
vec_append
#define vec_append(v1, v2)
Append v2 after v1.
Definition: vec.h:911
pool_put
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:305
fib_urpf_list_combine
void fib_urpf_list_combine(index_t ui1, index_t ui2)
Combine to interface lists.
Definition: fib_urpf_list.c:114
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
fib_urpf_list_lock
void fib_urpf_list_lock(index_t ui)
Definition: fib_urpf_list.c:87
unformat_input_t
struct _unformat_input_t unformat_input_t
fib_urpf_list_pool
fib_urpf_list_t * fib_urpf_list_pool
pool of all fib_urpf_list
Definition: fib_urpf_list.c:22
show_fib_urpf_list_command
static clib_error_t * show_fib_urpf_list_command(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: fib_urpf_list.c:182
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
pool_is_free_index
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:302
fib_urpf_itf_cmp_for_sort
static int fib_urpf_itf_cmp_for_sort(void *v1, void *v2)
Sort the interface indicies.
Definition: fib_urpf_list.c:132
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
show_fib_urpf_list
static vlib_cli_command_t show_fib_urpf_list
(constructor) VLIB_CLI_COMMAND (show_fib_urpf_list)
Definition: fib_urpf_list.c:234
vec_add1
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:606
index_t
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:43
fib_urpf_list_t_
Definition: fib_urpf_list.h:58
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
i
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
pool_get
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:255
format_fib_urpf_list
u8 * format_fib_urpf_list(u8 *s, va_list *args)
Definition: fib_urpf_list.c:25
VLIB_CLI_COMMAND
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:163
vlib_cli_output
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:716
vec_free
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:395
pool_len
#define pool_len(p)
Number of elements in pool vector.
Definition: pool.h:139
pool_foreach_index
#define pool_foreach_index(i, v)
Definition: pool.h:576
fib_urpf_list_show_mem
void fib_urpf_list_show_mem(void)
Definition: fib_urpf_list.c:173
format
description fragment has unexpected format
Definition: map.api:433
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
fib_urpf_list_append
void fib_urpf_list_append(index_t ui, u32 sw_if_index)
Append another interface to the list.
Definition: fib_urpf_list.c:100
u32
unsigned int u32
Definition: types.h:88
fib_urpf_list.h
vec_foreach
#define vec_foreach(var, vec)
Vector iterator.
Definition: vec_bootstrap.h:213
pool_elts
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:127
fib_urpf_list_get
static fib_urpf_list_t * fib_urpf_list_get(index_t index)
Definition: fib_urpf_list.h:96
FIB_URPF_LIST_BAKED
@ FIB_URPF_LIST_BAKED
Set to indicated that the uRPF list has already been baked.
Definition: fib_urpf_list.h:55
vec_sort_with_function
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:1097
adj_index_t
u32 adj_index_t
An index for adjacencies.
Definition: adj_types.h:30
clib_memset
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vlib_main_t
Definition: main.h:102
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
fib_urpf_list_alloc_and_lock
index_t fib_urpf_list_alloc_and_lock(void)
Definition: fib_urpf_list.c:55
fib_urpf_list_t_::furpf_itfs
adj_index_t * furpf_itfs
The list of interfaces that comprise the allowed accepting interfaces.
Definition: fib_urpf_list.h:63
fib_urpf_list_bake
void fib_urpf_list_bake(index_t ui)
Convert the uRPF list from the itf set obtained during the walk to a unique list.
Definition: fib_urpf_list.c:144
vlib_cli_command_t
Definition: cli.h:92
INDEX_INVALID
#define INDEX_INVALID
Invalid index - used when no index is known blazoned capitals INVALID speak volumes where ~0 does not...
Definition: dpo.h:49
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
fib_urpf_list_unlock
void fib_urpf_list_unlock(index_t ui)
Definition: fib_urpf_list.c:68