2 * Copyright (c) 2015 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.
15 #include <vnet/ip/ip.h>
16 #include <vnet/ethernet/ethernet.h>
20 * @brief IPv4 FIB Tester.
22 * Not compiled in by default. IPv4 FIB tester. Add, probe, delete a bunch of
23 * random routes / masks and make sure that the mtrie agrees with
26 * Manipulate the FIB by means of the debug CLI commands, to minimize
27 * the chances of doing something idiotic.
31 * These routines need to be redeclared non-static elsewhere.
33 * Also: rename ip_route() -> vnet_ip_route_cmd() and add the usual
34 * test_route_init() call to main.c
36 clib_error_t *vnet_ip_route_cmd (vlib_main_t * vm,
37 unformat_input_t * input,
38 vlib_cli_command_t * cmd_arg);
40 int ip4_lookup_validate (ip4_address_t * a, u32 fib_index0);
42 ip4_fib_t *find_fib_by_table_index_or_id (ip4_main_t * im,
43 u32 table_index_or_id, u32 flags);
45 /* Routes to insert/delete/probe in FIB */
48 ip4_address_t address;
50 u32 interface_id; /* not an xx_if_index */
55 /* Test routes in use */
56 test_route_t *route_pool;
58 /* Number of fake ethernets created */
59 u32 test_interfaces_created;
62 test_main_t test_main;
64 /* fake ethernet device class, distinct from "fake-ethX" */
66 format_test_interface_name (u8 * s, va_list * args)
68 u32 dev_instance = va_arg (*args, u32);
69 return format (s, "test-eth%d", dev_instance);
73 dummy_interface_tx (vlib_main_t * vm,
74 vlib_node_runtime_t * node, vlib_frame_t * frame)
76 clib_warning ("you shouldn't be here, leaking buffers...");
77 return frame->n_vectors;
81 VNET_DEVICE_CLASS (test_interface_device_class,static) = {
82 .name = "Test interface",
83 .format_device_name = format_test_interface_name,
84 .tx_function = dummy_interface_tx,
89 thrash (vlib_main_t * vm,
90 unformat_input_t * main_input, vlib_cli_command_t * cmd_arg)
92 u32 seed = 0xdeaddabe;
96 f64 min_mask_bits = 7.0;
97 f64 max_mask_bits = 32.0;
98 u32 table_id = 11; /* my amp goes to 11 (use fib 11) */
103 test_main_t *tm = &test_main;
104 ip4_main_t *im = &ip4_main;
105 vnet_main_t *vnm = vnet_get_main ();
106 unformat_input_t cmd_input;
111 clib_error_t *error = 0;
113 unformat_input_t _line_input, *line_input = &_line_input;
118 /* Precompute mask width -> mask vector */
120 vec_validate (masks, 32);
121 for (i = 32; i > 0; i--)
127 if (unformat_user (main_input, unformat_line_input, line_input))
129 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
131 if (unformat (line_input, "seed %d", &seed))
133 else if (unformat (line_input, "niter %d", &niter))
135 else if (unformat (line_input, "nroutes %d", &nroutes))
137 else if (unformat (line_input, "ninterfaces %d", &ninterfaces))
139 else if (unformat (line_input, "min-mask-bits %d", &tmp))
140 min_mask_bits = (f64) tmp;
141 else if (unformat (line_input, "max-mask-bits %d", &tmp))
142 max_mask_bits = (f64) tmp;
143 else if (unformat (line_input, "verbose"))
147 error = clib_error_return (0, "unknown input `%U'",
148 format_unformat_error, line_input);
154 /* Find or create FIB table 11 */
155 fib = ip4_fib_find_or_create_fib_by_table_id (table_id);
157 for (i = tm->test_interfaces_created; i < ninterfaces; i++)
159 vnet_hw_interface_t *hw;
160 memset (hw_address, 0, sizeof (hw_address));
161 hw_address[0] = 0xd0;
162 hw_address[1] = 0x0f;
165 error = ethernet_register_interface
166 (vnm, test_interface_device_class.index, i /* instance */ ,
167 hw_address, &hw_if_index,
168 /* flag change */ 0);
170 /* Fake interfaces use FIB table 11 */
171 hw = vnet_get_hw_interface (vnm, hw_if_index);
172 vec_validate (im->fib_index_by_sw_if_index, hw->sw_if_index);
173 im->fib_index_by_sw_if_index[hw->sw_if_index] = fib->index;
174 ip4_sw_interface_enable_disable (sw_if_index, 1);
177 tm->test_interfaces_created = ninterfaces;
179 /* Find fib index corresponding to FIB id 11 */
180 p = hash_get (im->fib_index_by_table_id, table_id);
183 vlib_cli_output (vm, "Couldn't map fib id %d to fib index\n", table_id);
188 for (iter = 0; iter < niter; iter++)
190 /* Pick random routes to install */
191 for (i = 0; i < nroutes; i++)
195 pool_get (tm->route_pool, tr);
196 memset (tr, 0, sizeof (*tr));
199 rf = random_f64 (&seed);
200 tr->mask_width = (u32) (min_mask_bits
201 + rf * (max_mask_bits - min_mask_bits));
202 tmp = random_u32 (&seed);
203 tmp &= masks[tr->mask_width];
204 tr->address.as_u32 = clib_host_to_net_u32 (tmp);
206 /* We can't add the same address/mask twice... */
207 for (j = 0; j < i; j++)
210 prev = pool_elt_at_index (tm->route_pool, j);
211 if ((prev->address.as_u32 == tr->address.as_u32)
212 && (prev->mask_width == tr->mask_width))
216 rf = random_f64 (&seed);
217 tr->interface_id = (u32) (rf * ninterfaces);
221 for (i = 0; i < nroutes; i++)
223 tr = pool_elt_at_index (tm->route_pool, i);
224 cmd = format (0, "add table %d %U/%d via test-eth%d",
226 format_ip4_address, &tr->address,
227 tr->mask_width, tr->interface_id);
230 fformat (stderr, "ip route %s\n", cmd);
231 unformat_init_string (&cmd_input, (char *) cmd, vec_len (cmd) - 1);
232 error = vnet_ip_route_cmd (vm, &cmd_input, cmd_arg);
234 clib_error_report (error);
235 unformat_free (&cmd_input);
239 for (i = 0; i < nroutes; i++)
241 tr = pool_elt_at_index (tm->route_pool, i);
242 if (!ip4_lookup_validate (&tr->address, table_index))
245 fformat (stderr, "test lookup table %d %U\n",
246 table_index, format_ip4_address, &tr->address);
248 fformat (stderr, "FAIL-after-insert: %U/%d\n",
249 format_ip4_address, &tr->address, tr->mask_width);
254 for (i = 0; i < nroutes; i++)
257 tr = pool_elt_at_index (tm->route_pool, i);
259 cmd = format (0, "del table %d %U/%d via test-eth%d",
261 format_ip4_address, &tr->address,
262 tr->mask_width, tr->interface_id);
264 cmd = format (0, "del table %d %U/%d",
266 format_ip4_address, &tr->address, tr->mask_width);
269 fformat (stderr, "ip route %s\n", cmd);
270 unformat_init_string (&cmd_input, (char *) cmd, vec_len (cmd) - 1);
271 error = vnet_ip_route_cmd (vm, &cmd_input, cmd_arg);
273 clib_error_report (error);
274 unformat_free (&cmd_input);
277 /* Make sure all undeleted routes still work */
278 for (j = i + 1; j < nroutes; j++)
280 test_route_t *rr; /* remaining route */
281 rr = pool_elt_at_index (tm->route_pool, j);
282 if (!ip4_lookup_validate (&rr->address, table_index))
285 fformat (stderr, "test lookup table %d %U\n",
286 table_index, format_ip4_address, &rr->address);
288 fformat (stderr, "FAIL: %U/%d AWOL\n",
289 format_ip4_address, &rr->address, rr->mask_width);
290 fformat (stderr, " iter %d after %d of %d deletes\n",
292 fformat (stderr, " last route deleted %U/%d\n",
293 format_ip4_address, &tr->address, tr->mask_width);
298 pool_free (tm->route_pool);
302 unformat_free (line_input);
308 * This command in not in the build by default. It is an internal
309 * command used to test the route functonality.
311 * Create test routes on IPv4 FIB table 11. Table will be created if it
314 * There are several optional attributes:
315 * - If not provided, <seed> defaults to 0xdeaddabe.
316 * - If not provided, <num-iter> defaults to 10.
317 * - If not provided, <num-iface> defaults to 4.
318 * - If not provided, <min-mask> defaults to 7.0.
319 * - If not provided, <max-mask> defaults to 32.0.
322 * Example of how to run:
323 * @cliexcmd{test route}
326 VLIB_CLI_COMMAND (test_route_command, static) = {
327 .path = "test route",
328 .short_help = "test route [seed <seed-num>] [niter <num-iter>] [ninterfaces <num-iface>] [min-mask-bits <min-mask>] [max-mask-bits <max-mask>] [verbose]", .function = thrash,
334 test_route_init (vlib_main_t * vm)
339 VLIB_INIT_FUNCTION (test_route_init);
342 * fd.io coding-style-patch-verification: ON
345 * eval: (c-set-style "gnu")