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);
32 urpf = fib_urpf_list_get(ui);
34 s = format(s, "uPRF-list:%d len:%d itfs:[",
35 ui, vec_len(urpf->furpf_itfs));
37 vec_foreach(swi, urpf->furpf_itfs)
39 s = format(s, "%d, ", *swi);
47 fib_urpf_list_alloc_and_lock (void)
49 fib_urpf_list_t *urpf;
51 pool_get(fib_urpf_list_pool, urpf);
52 memset(urpf, 0, sizeof(*urpf));
56 return (urpf - fib_urpf_list_pool);
60 fib_urpf_list_unlock (index_t ui)
62 fib_urpf_list_t *urpf;
64 if (INDEX_INVALID == ui)
67 urpf = fib_urpf_list_get(ui);
71 if (0 == urpf->furpf_locks)
73 vec_free(urpf->furpf_itfs);
74 pool_put(fib_urpf_list_pool, urpf);
79 fib_urpf_list_lock (index_t ui)
81 fib_urpf_list_t *urpf;
83 urpf = fib_urpf_list_get(ui);
89 * @brief Append another interface to the list.
92 fib_urpf_list_append (index_t ui,
95 fib_urpf_list_t *urpf;
97 urpf = fib_urpf_list_get(ui);
99 vec_add1(urpf->furpf_itfs, sw_if_index);
103 * @brief Combine to interface lists
106 fib_urpf_list_combine (index_t ui1,
109 fib_urpf_list_t *urpf1, *urpf2;
111 urpf1 = fib_urpf_list_get(ui1);
112 urpf2 = fib_urpf_list_get(ui2);
114 vec_append(urpf1->furpf_itfs, urpf2->furpf_itfs);
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.
124 fib_urpf_itf_cmp_for_sort (void * v1,
127 fib_node_index_t *i1 = v1, *i2 = v2;
133 * @brief Convert the uRPF list from the itf set obtained during the walk
137 fib_urpf_list_bake (index_t ui)
139 fib_urpf_list_t *urpf;
141 urpf = fib_urpf_list_get(ui);
143 ASSERT(!(urpf->furpf_flags & FIB_URPF_LIST_BAKED));
145 if (vec_len(urpf->furpf_itfs) > 1)
150 * cat list | sort | uniq > rpf_list
152 vec_sort_with_function(urpf->furpf_itfs, fib_urpf_itf_cmp_for_sort);
155 while (j < vec_len(urpf->furpf_itfs))
157 if (urpf->furpf_itfs[i] == urpf->furpf_itfs[j])
160 * the itfacenct entries are the same.
161 * search forward for a unique one
163 while (urpf->furpf_itfs[i] == urpf->furpf_itfs[j] &&
164 j < vec_len(urpf->furpf_itfs))
168 if (j == vec_len(urpf->furpf_itfs))
171 * ran off the end without finding a unique index.
178 urpf->furpf_itfs[i+1] = urpf->furpf_itfs[j];
185 * set the length of the vector to the number of unique itfs
187 _vec_len(urpf->furpf_itfs) = i+1;
190 urpf->furpf_flags |= FIB_URPF_LIST_BAKED;
194 fib_urpf_list_show_mem (void)
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));
202 static clib_error_t *
203 show_fib_urpf_list_command (vlib_main_t * vm,
204 unformat_input_t * input,
205 vlib_cli_command_t * cmd)
209 if (unformat (input, "%d", &ui))
214 if (!pool_is_free_index(fib_urpf_list_pool, ui))
216 vlib_cli_output (vm, "%d@%U",
218 format_fib_urpf_list, ui);
222 vlib_cli_output (vm, "uRPF %d invalid", ui);
230 vlib_cli_output (vm, "FIB uRPF Entries:");
231 pool_foreach_index(ui, fib_urpf_list_pool,
233 vlib_cli_output (vm, "%d@%U",
235 format_fib_urpf_list, ui);
244 * The '<em>sh fib uRPF [index] </em>' command displays the uRPF lists
247 * @cliexstart{show fib uRPF}
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, ]
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",