Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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: |
| 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 | #include <vnet/ip/ip.h> |
| 16 | #include <vnet/ethernet/ethernet.h> |
| 17 | |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 18 | /** |
| 19 | * @file |
| 20 | * @brief IPv4 FIB Tester. |
| 21 | * |
| 22 | * Not compiled in by default. IPv4 FIB tester. Add, probe, delete a bunch of |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 23 | * random routes / masks and make sure that the mtrie agrees with |
| 24 | * the hash-table FIB. |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 25 | * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 26 | * Manipulate the FIB by means of the debug CLI commands, to minimize |
| 27 | * the chances of doing something idiotic. |
| 28 | */ |
| 29 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 30 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 31 | * These routines need to be redeclared non-static elsewhere. |
| 32 | * |
| 33 | * Also: rename ip_route() -> vnet_ip_route_cmd() and add the usual |
| 34 | * test_route_init() call to main.c |
| 35 | */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 36 | clib_error_t *vnet_ip_route_cmd (vlib_main_t * vm, |
| 37 | unformat_input_t * input, |
| 38 | vlib_cli_command_t * cmd_arg); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 39 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 40 | int ip4_lookup_validate (ip4_address_t * a, u32 fib_index0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 41 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 42 | ip4_fib_t *find_fib_by_table_index_or_id (ip4_main_t * im, |
| 43 | u32 table_index_or_id, u32 flags); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 44 | |
| 45 | /* Routes to insert/delete/probe in FIB */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 46 | typedef struct |
| 47 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | ip4_address_t address; |
| 49 | u32 mask_width; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 50 | u32 interface_id; /* not an xx_if_index */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | } test_route_t; |
| 52 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 53 | typedef struct |
| 54 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | /* Test routes in use */ |
| 56 | test_route_t *route_pool; |
| 57 | |
| 58 | /* Number of fake ethernets created */ |
| 59 | u32 test_interfaces_created; |
| 60 | } test_main_t; |
| 61 | |
| 62 | test_main_t test_main; |
| 63 | |
| 64 | /* fake ethernet device class, distinct from "fake-ethX" */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 65 | static u8 * |
| 66 | format_test_interface_name (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | { |
| 68 | u32 dev_instance = va_arg (*args, u32); |
| 69 | return format (s, "test-eth%d", dev_instance); |
| 70 | } |
| 71 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 72 | static uword |
| 73 | dummy_interface_tx (vlib_main_t * vm, |
| 74 | vlib_node_runtime_t * node, vlib_frame_t * frame) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | { |
| 76 | clib_warning ("you shouldn't be here, leaking buffers..."); |
| 77 | return frame->n_vectors; |
| 78 | } |
| 79 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 80 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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, |
| 85 | }; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 86 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | |
| 88 | static clib_error_t * |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 89 | thrash (vlib_main_t * vm, |
| 90 | unformat_input_t * main_input, vlib_cli_command_t * cmd_arg) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | { |
| 92 | u32 seed = 0xdeaddabe; |
| 93 | u32 niter = 10; |
| 94 | u32 nroutes = 10; |
| 95 | u32 ninterfaces = 4; |
| 96 | f64 min_mask_bits = 7.0; |
| 97 | f64 max_mask_bits = 32.0; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 98 | u32 table_id = 11; /* my amp goes to 11 (use fib 11) */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | u32 table_index; |
| 100 | int iter, i; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 101 | u8 *cmd; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 102 | test_route_t *tr; |
| 103 | test_main_t *tm = &test_main; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 104 | ip4_main_t *im = &ip4_main; |
| 105 | vnet_main_t *vnm = vnet_get_main (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | unformat_input_t cmd_input; |
| 107 | f64 rf; |
| 108 | u32 *masks = 0; |
| 109 | u32 tmp; |
| 110 | u32 hw_if_index; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 111 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | uword *p; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 113 | unformat_input_t _line_input, *line_input = &_line_input; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | u8 hw_address[6]; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 115 | ip4_fib_t *fib; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 116 | int verbose = 0; |
| 117 | |
| 118 | /* Precompute mask width -> mask vector */ |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 119 | tmp = (u32) ~ 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | vec_validate (masks, 32); |
| 121 | for (i = 32; i > 0; i--) |
| 122 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 123 | masks[i] = tmp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 124 | tmp <<= 1; |
| 125 | } |
| 126 | |
| 127 | if (unformat_user (main_input, unformat_line_input, line_input)) |
| 128 | { |
| 129 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 130 | { |
| 131 | if (unformat (line_input, "seed %d", &seed)) |
| 132 | ; |
| 133 | else if (unformat (line_input, "niter %d", &niter)) |
| 134 | ; |
| 135 | else if (unformat (line_input, "nroutes %d", &nroutes)) |
| 136 | ; |
| 137 | else if (unformat (line_input, "ninterfaces %d", &ninterfaces)) |
| 138 | ; |
| 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")) |
| 144 | verbose = 1; |
| 145 | else |
| 146 | return clib_error_return (0, "unknown input `%U'", |
| 147 | format_unformat_error, line_input); |
| 148 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* Find or create FIB table 11 */ |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 152 | fib = ip4_fib_find_or_create_fib_by_table_id (table_id); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | |
| 154 | for (i = tm->test_interfaces_created; i < ninterfaces; i++) |
| 155 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 156 | vnet_hw_interface_t *hw; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | memset (hw_address, 0, sizeof (hw_address)); |
| 158 | hw_address[0] = 0xd0; |
| 159 | hw_address[1] = 0x0f; |
| 160 | hw_address[5] = i; |
| 161 | |
| 162 | error = ethernet_register_interface |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 163 | (vnm, test_interface_device_class.index, i /* instance */ , |
| 164 | hw_address, &hw_if_index, |
| 165 | /* flag change */ 0); |
| 166 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 167 | /* Fake interfaces use FIB table 11 */ |
| 168 | hw = vnet_get_hw_interface (vnm, hw_if_index); |
| 169 | vec_validate (im->fib_index_by_sw_if_index, hw->sw_if_index); |
| 170 | im->fib_index_by_sw_if_index[hw->sw_if_index] = fib->index; |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 171 | ip4_sw_interface_enable_disable (sw_if_index, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | tm->test_interfaces_created = ninterfaces; |
| 175 | |
| 176 | /* Find fib index corresponding to FIB id 11 */ |
| 177 | p = hash_get (im->fib_index_by_table_id, table_id); |
| 178 | if (p == 0) |
| 179 | { |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 180 | vlib_cli_output (vm, "Couldn't map fib id %d to fib index\n", table_id); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | return 0; |
| 182 | } |
| 183 | table_index = p[0]; |
| 184 | |
| 185 | for (iter = 0; iter < niter; iter++) |
| 186 | { |
| 187 | /* Pick random routes to install */ |
| 188 | for (i = 0; i < nroutes; i++) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 189 | { |
| 190 | int j; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 191 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 192 | pool_get (tm->route_pool, tr); |
| 193 | memset (tr, 0, sizeof (*tr)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 194 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 195 | again: |
| 196 | rf = random_f64 (&seed); |
| 197 | tr->mask_width = (u32) (min_mask_bits |
| 198 | + rf * (max_mask_bits - min_mask_bits)); |
| 199 | tmp = random_u32 (&seed); |
| 200 | tmp &= masks[tr->mask_width]; |
| 201 | tr->address.as_u32 = clib_host_to_net_u32 (tmp); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 203 | /* We can't add the same address/mask twice... */ |
| 204 | for (j = 0; j < i; j++) |
| 205 | { |
| 206 | test_route_t *prev; |
| 207 | prev = pool_elt_at_index (tm->route_pool, j); |
| 208 | if ((prev->address.as_u32 == tr->address.as_u32) |
| 209 | && (prev->mask_width == tr->mask_width)) |
| 210 | goto again; |
| 211 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 212 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 213 | rf = random_f64 (&seed); |
| 214 | tr->interface_id = (u32) (rf * ninterfaces); |
| 215 | } |
| 216 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 217 | /* Add them */ |
| 218 | for (i = 0; i < nroutes; i++) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 219 | { |
| 220 | tr = pool_elt_at_index (tm->route_pool, i); |
| 221 | cmd = format (0, "add table %d %U/%d via test-eth%d", |
| 222 | table_id, |
| 223 | format_ip4_address, &tr->address, |
| 224 | tr->mask_width, tr->interface_id); |
| 225 | vec_add1 (cmd, 0); |
| 226 | if (verbose) |
| 227 | fformat (stderr, "ip route %s\n", cmd); |
| 228 | unformat_init_string (&cmd_input, (char *) cmd, vec_len (cmd) - 1); |
| 229 | error = vnet_ip_route_cmd (vm, &cmd_input, cmd_arg); |
| 230 | if (error) |
| 231 | clib_error_report (error); |
| 232 | unformat_free (&cmd_input); |
| 233 | vec_free (cmd); |
| 234 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 235 | /* Probe them */ |
| 236 | for (i = 0; i < nroutes; i++) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 237 | { |
| 238 | tr = pool_elt_at_index (tm->route_pool, i); |
| 239 | if (!ip4_lookup_validate (&tr->address, table_index)) |
| 240 | { |
| 241 | if (verbose) |
| 242 | fformat (stderr, "test lookup table %d %U\n", |
| 243 | table_index, format_ip4_address, &tr->address); |
| 244 | |
| 245 | fformat (stderr, "FAIL-after-insert: %U/%d\n", |
| 246 | format_ip4_address, &tr->address, tr->mask_width); |
| 247 | } |
| 248 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 249 | |
| 250 | /* Delete them */ |
| 251 | for (i = 0; i < nroutes; i++) |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 252 | { |
| 253 | int j; |
| 254 | tr = pool_elt_at_index (tm->route_pool, i); |
| 255 | if (0) |
| 256 | cmd = format (0, "del table %d %U/%d via test-eth%d", |
| 257 | table_id, |
| 258 | format_ip4_address, &tr->address, |
| 259 | tr->mask_width, tr->interface_id); |
| 260 | else |
| 261 | cmd = format (0, "del table %d %U/%d", |
| 262 | table_id, |
| 263 | format_ip4_address, &tr->address, tr->mask_width); |
| 264 | vec_add1 (cmd, 0); |
| 265 | if (verbose) |
| 266 | fformat (stderr, "ip route %s\n", cmd); |
| 267 | unformat_init_string (&cmd_input, (char *) cmd, vec_len (cmd) - 1); |
| 268 | error = vnet_ip_route_cmd (vm, &cmd_input, cmd_arg); |
| 269 | if (error) |
| 270 | clib_error_report (error); |
| 271 | unformat_free (&cmd_input); |
| 272 | vec_free (cmd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 273 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 274 | /* Make sure all undeleted routes still work */ |
| 275 | for (j = i + 1; j < nroutes; j++) |
| 276 | { |
| 277 | test_route_t *rr; /* remaining route */ |
| 278 | rr = pool_elt_at_index (tm->route_pool, j); |
| 279 | if (!ip4_lookup_validate (&rr->address, table_index)) |
| 280 | { |
| 281 | if (verbose) |
| 282 | fformat (stderr, "test lookup table %d %U\n", |
| 283 | table_index, format_ip4_address, &rr->address); |
| 284 | |
| 285 | fformat (stderr, "FAIL: %U/%d AWOL\n", |
| 286 | format_ip4_address, &rr->address, rr->mask_width); |
| 287 | fformat (stderr, " iter %d after %d of %d deletes\n", |
| 288 | iter, i, nroutes); |
| 289 | fformat (stderr, " last route deleted %U/%d\n", |
| 290 | format_ip4_address, &tr->address, tr->mask_width); |
| 291 | } |
| 292 | } |
| 293 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 294 | |
| 295 | pool_free (tm->route_pool); |
| 296 | } |
| 297 | return 0; |
| 298 | } |
| 299 | |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 300 | /*? |
| 301 | * This command in not in the build by default. It is an internal |
| 302 | * command used to test the route functonality. |
| 303 | * |
| 304 | * Create test routes on IPv4 FIB table 11. Table will be created if it |
| 305 | * does not exist. |
| 306 | * |
| 307 | * There are several optional attributes: |
| 308 | * - If not provided, <seed> defaults to 0xdeaddabe. |
| 309 | * - If not provided, <num-iter> defaults to 10. |
| 310 | * - If not provided, <num-iface> defaults to 4. |
| 311 | * - If not provided, <min-mask> defaults to 7.0. |
| 312 | * - If not provided, <max-mask> defaults to 32.0. |
| 313 | * |
| 314 | * @cliexpar |
| 315 | * Example of how to run: |
| 316 | * @cliexcmd{test route} |
| 317 | ?*/ |
| 318 | /* *INDENT-OFF* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 319 | VLIB_CLI_COMMAND (test_route_command, static) = { |
| 320 | .path = "test route", |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 321 | .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, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 322 | .function = thrash, |
| 323 | }; |
Billy McFall | 0683c9c | 2016-10-13 08:27:31 -0400 | [diff] [blame] | 324 | /* *INDENT-ON* */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 325 | |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 326 | clib_error_t * |
| 327 | test_route_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 328 | { |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | VLIB_INIT_FUNCTION (test_route_init); |
Dave Barach | d7cb1b5 | 2016-12-09 09:52:16 -0500 | [diff] [blame] | 333 | |
| 334 | /* |
| 335 | * fd.io coding-style-patch-verification: ON |
| 336 | * |
| 337 | * Local Variables: |
| 338 | * eval: (c-set-style "gnu") |
| 339 | * End: |
| 340 | */ |