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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/fib/fib_urpf_list.h>
17 #include <vnet/adj/adj.h>
20 * @brief pool of all fib_urpf_list
22 fib_urpf_list_t *fib_urpf_list_pool;
25 format_fib_urpf_list (u8 *s, va_list *args)
27 fib_urpf_list_t *urpf;
31 ui = va_arg(*args, index_t);
33 if (INDEX_INVALID != ui)
35 urpf = fib_urpf_list_get(ui);
37 s = format(s, "uPRF-list:%d len:%d itfs:[",
38 ui, vec_len(urpf->furpf_itfs));
40 vec_foreach(swi, urpf->furpf_itfs)
42 s = format(s, "%d, ", *swi);
48 s = format(s, "uRPF-list: None");
55 fib_urpf_list_alloc_and_lock (void)
57 fib_urpf_list_t *urpf;
59 pool_get(fib_urpf_list_pool, urpf);
60 memset(urpf, 0, sizeof(*urpf));
64 return (urpf - fib_urpf_list_pool);
68 fib_urpf_list_unlock (index_t ui)
70 fib_urpf_list_t *urpf;
72 if (INDEX_INVALID == ui)
75 urpf = fib_urpf_list_get(ui);
79 if (0 == urpf->furpf_locks)
81 vec_free(urpf->furpf_itfs);
82 pool_put(fib_urpf_list_pool, urpf);
87 fib_urpf_list_lock (index_t ui)
89 fib_urpf_list_t *urpf;
91 urpf = fib_urpf_list_get(ui);
97 * @brief Append another interface to the list.
100 fib_urpf_list_append (index_t ui,
103 fib_urpf_list_t *urpf;
105 urpf = fib_urpf_list_get(ui);
107 vec_add1(urpf->furpf_itfs, sw_if_index);
111 * @brief Combine to interface lists
114 fib_urpf_list_combine (index_t ui1,
117 fib_urpf_list_t *urpf1, *urpf2;
119 urpf1 = fib_urpf_list_get(ui1);
120 urpf2 = fib_urpf_list_get(ui2);
122 vec_append(urpf1->furpf_itfs, urpf2->furpf_itfs);
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.
132 fib_urpf_itf_cmp_for_sort (void * v1,
135 fib_node_index_t *i1 = v1, *i2 = v2;
141 * @brief Convert the uRPF list from the itf set obtained during the walk
145 fib_urpf_list_bake (index_t ui)
147 fib_urpf_list_t *urpf;
149 urpf = fib_urpf_list_get(ui);
151 ASSERT(!(urpf->furpf_flags & FIB_URPF_LIST_BAKED));
153 if (vec_len(urpf->furpf_itfs) > 1)
158 * cat list | sort | uniq > rpf_list
160 vec_sort_with_function(urpf->furpf_itfs, fib_urpf_itf_cmp_for_sort);
163 while (j < vec_len(urpf->furpf_itfs))
165 if (urpf->furpf_itfs[i] == urpf->furpf_itfs[j])
168 * the itfacenct entries are the same.
169 * search forward for a unique one
171 while (urpf->furpf_itfs[i] == urpf->furpf_itfs[j] &&
172 j < vec_len(urpf->furpf_itfs))
176 if (j == vec_len(urpf->furpf_itfs))
179 * ran off the end without finding a unique index.
186 urpf->furpf_itfs[i+1] = urpf->furpf_itfs[j];
193 * set the length of the vector to the number of unique itfs
195 _vec_len(urpf->furpf_itfs) = i+1;
198 urpf->furpf_flags |= FIB_URPF_LIST_BAKED;
202 fib_urpf_list_show_mem (void)
204 fib_show_memory_usage("uRPF-list",
205 pool_elts(fib_urpf_list_pool),
206 pool_len(fib_urpf_list_pool),
207 sizeof(fib_urpf_list_t));
210 static clib_error_t *
211 show_fib_urpf_list_command (vlib_main_t * vm,
212 unformat_input_t * input,
213 vlib_cli_command_t * cmd)
217 if (unformat (input, "%d", &ui))
222 if (!pool_is_free_index(fib_urpf_list_pool, ui))
224 vlib_cli_output (vm, "%d@%U",
226 format_fib_urpf_list, ui);
230 vlib_cli_output (vm, "uRPF %d invalid", ui);
238 vlib_cli_output (vm, "FIB uRPF Entries:");
239 pool_foreach_index(ui, fib_urpf_list_pool,
241 vlib_cli_output (vm, "%d@%U",
243 format_fib_urpf_list, ui);
252 * The '<em>sh fib uRPF [index] </em>' command displays the uRPF lists
255 * @cliexstart{show fib uRPF}
257 * 0@uPRF-list:0 len:0 itfs:[]
258 * 1@uPRF-list:1 len:2 itfs:[1, 2, ]
259 * 2@uPRF-list:2 len:1 itfs:[3, ]
260 * 3@uPRF-list:3 len:1 itfs:[9, ]
263 VLIB_CLI_COMMAND (show_fib_urpf_list, static) = {
264 .path = "show fib uRPF",
265 .function = show_fib_urpf_list_command,
266 .short_help = "show fib uRPF",