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>
19 * ip4 FIB tester. Add, probe, delete a bunch of
20 * random routes / masks and make sure that the mtrie agrees with
23 * Manipulate the FIB by means of the debug CLI commands, to minimize
24 * the chances of doing something idiotic.
28 * These routines need to be redeclared non-static elsewhere.
30 * Also: rename ip_route() -> vnet_ip_route_cmd() and add the usual
31 * test_route_init() call to main.c
34 vnet_ip_route_cmd (vlib_main_t * vm,
35 unformat_input_t * input, vlib_cli_command_t * cmd_arg);
37 int ip4_lookup_validate (ip4_address_t *a, u32 fib_index0);
40 find_fib_by_table_index_or_id (ip4_main_t * im, u32 table_index_or_id,
43 /* Routes to insert/delete/probe in FIB */
45 ip4_address_t address;
47 u32 interface_id; /* not an xx_if_index */
51 /* Test routes in use */
52 test_route_t *route_pool;
54 /* Number of fake ethernets created */
55 u32 test_interfaces_created;
58 test_main_t test_main;
60 /* fake ethernet device class, distinct from "fake-ethX" */
61 static u8 * format_test_interface_name (u8 * s, va_list * args)
63 u32 dev_instance = va_arg (*args, u32);
64 return format (s, "test-eth%d", dev_instance);
67 static uword dummy_interface_tx (vlib_main_t * vm,
68 vlib_node_runtime_t * node,
71 clib_warning ("you shouldn't be here, leaking buffers...");
72 return frame->n_vectors;
75 VNET_DEVICE_CLASS (test_interface_device_class,static) = {
76 .name = "Test interface",
77 .format_device_name = format_test_interface_name,
78 .tx_function = dummy_interface_tx,
82 thrash (vlib_main_t * vm,
83 unformat_input_t * main_input, vlib_cli_command_t * cmd_arg)
85 u32 seed = 0xdeaddabe;
89 f64 min_mask_bits = 7.0;
90 f64 max_mask_bits = 32.0;
91 u32 table_id = 11; /* my amp goes to 11 (use fib 11) */
96 test_main_t *tm = &test_main;
97 ip4_main_t * im = &ip4_main;
98 vnet_main_t * vnm = vnet_get_main();
99 unformat_input_t cmd_input;
104 clib_error_t * error = 0;
106 unformat_input_t _line_input, * line_input = &_line_input;
111 /* Precompute mask width -> mask vector */
113 vec_validate (masks, 32);
114 for (i = 32; i > 0; i--)
120 if (unformat_user (main_input, unformat_line_input, line_input))
122 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
124 if (unformat (line_input, "seed %d", &seed))
126 else if (unformat (line_input, "niter %d", &niter))
128 else if (unformat (line_input, "nroutes %d", &nroutes))
130 else if (unformat (line_input, "ninterfaces %d", &ninterfaces))
132 else if (unformat (line_input, "min-mask-bits %d", &tmp))
133 min_mask_bits = (f64) tmp;
134 else if (unformat (line_input, "max-mask-bits %d", &tmp))
135 max_mask_bits = (f64) tmp;
136 else if (unformat (line_input, "verbose"))
139 return clib_error_return (0, "unknown input `%U'",
140 format_unformat_error, line_input);
144 /* Find or create FIB table 11 */
145 fib = find_ip4_fib_by_table_index_or_id (im, table_id, IP4_ROUTE_FLAG_TABLE_ID);
147 for (i = tm->test_interfaces_created; i < ninterfaces; i++)
149 vnet_hw_interface_t * hw;
150 memset (hw_address, 0, sizeof (hw_address));
151 hw_address[0] = 0xd0;
152 hw_address[1] = 0x0f;
155 error = ethernet_register_interface
157 test_interface_device_class.index,
161 /* flag change */ 0);
163 /* Fake interfaces use FIB table 11 */
164 hw = vnet_get_hw_interface (vnm, hw_if_index);
165 vec_validate (im->fib_index_by_sw_if_index, hw->sw_if_index);
166 im->fib_index_by_sw_if_index[hw->sw_if_index] = fib->index;
169 tm->test_interfaces_created = ninterfaces;
171 /* Find fib index corresponding to FIB id 11 */
172 p = hash_get (im->fib_index_by_table_id, table_id);
175 vlib_cli_output (vm, "Couldn't map fib id %d to fib index\n",
181 for (iter = 0; iter < niter; iter++)
183 /* Pick random routes to install */
184 for (i = 0; i < nroutes; i++)
188 pool_get (tm->route_pool, tr);
189 memset (tr, 0, sizeof (*tr));
192 rf = random_f64 (&seed);
193 tr->mask_width = (u32) (min_mask_bits
194 + rf * (max_mask_bits - min_mask_bits));
195 tmp = random_u32 (&seed);
196 tmp &= masks[tr->mask_width];
197 tr->address.as_u32 = clib_host_to_net_u32(tmp);
199 /* We can't add the same address/mask twice... */
200 for (j = 0; j < i; j++)
203 prev = pool_elt_at_index (tm->route_pool, j);
204 if ((prev->address.as_u32 == tr->address.as_u32)
205 && (prev->mask_width == tr->mask_width))
209 rf = random_f64 (&seed);
210 tr->interface_id = (u32) (rf * ninterfaces);
214 for (i = 0; i < nroutes; i++)
216 tr = pool_elt_at_index (tm->route_pool, i);
217 cmd = format (0, "add table %d %U/%d via test-eth%d",
219 format_ip4_address, &tr->address,
220 tr->mask_width, tr->interface_id);
223 fformat(stderr, "ip route %s\n", cmd);
224 unformat_init_string (&cmd_input, (char *) cmd, vec_len(cmd)-1);
225 error = vnet_ip_route_cmd (vm, &cmd_input, cmd_arg);
227 clib_error_report(error);
228 unformat_free (&cmd_input);
232 for (i = 0; i < nroutes; i++)
234 tr = pool_elt_at_index (tm->route_pool, i);
235 if (!ip4_lookup_validate (&tr->address, table_index))
238 fformat (stderr, "test lookup table %d %U\n",
239 table_index, format_ip4_address, &tr->address);
241 fformat (stderr, "FAIL-after-insert: %U/%d\n",
242 format_ip4_address, &tr->address,
248 for (i = 0; i < nroutes; i++)
251 tr = pool_elt_at_index (tm->route_pool, i);
253 cmd = format (0, "del table %d %U/%d via test-eth%d",
255 format_ip4_address, &tr->address,
256 tr->mask_width, tr->interface_id);
258 cmd = format (0, "del table %d %U/%d",
260 format_ip4_address, &tr->address,
264 fformat(stderr, "ip route %s\n", cmd);
265 unformat_init_string (&cmd_input, (char *) cmd, vec_len(cmd)-1);
266 error = vnet_ip_route_cmd (vm, &cmd_input, cmd_arg);
268 clib_error_report(error);
269 unformat_free (&cmd_input);
272 /* Make sure all undeleted routes still work */
273 for (j = i+1; j < nroutes; j++)
275 test_route_t *rr; /* remaining route */
276 rr = pool_elt_at_index (tm->route_pool, j);
277 if (!ip4_lookup_validate (&rr->address, table_index))
280 fformat (stderr, "test lookup table %d %U\n",
281 table_index, format_ip4_address, &rr->address);
283 fformat (stderr, "FAIL: %U/%d AWOL\n",
284 format_ip4_address, &rr->address,
286 fformat (stderr, " iter %d after %d of %d deletes\n",
288 fformat (stderr, " last route deleted %U/%d\n",
289 format_ip4_address, &tr->address,
295 pool_free (tm->route_pool);
300 VLIB_CLI_COMMAND (test_route_command, static) = {
301 .path = "test route",
302 .short_help = "test route",
306 clib_error_t *test_route_init (vlib_main_t *vm)
311 VLIB_INIT_FUNCTION (test_route_init);