VPP-286: Add CLI Command documentation via doxygen comments for vnet/vnet/ip.
[vpp.git] / vnet / vnet / ip / ip4_test.c
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
18 /**
19  * @file
20  * @brief IPv4 FIB Tester.
21  *
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
24  * the hash-table FIB.
25  * 
26  * Manipulate the FIB by means of the debug CLI commands, to minimize
27  * the chances of doing something idiotic.
28  */
29
30 /* 
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  */
36 clib_error_t *
37 vnet_ip_route_cmd (vlib_main_t * vm, 
38                    unformat_input_t * input, vlib_cli_command_t * cmd_arg);
39
40 int ip4_lookup_validate (ip4_address_t *a, u32 fib_index0);
41
42 ip4_fib_t *
43 find_fib_by_table_index_or_id (ip4_main_t * im, u32 table_index_or_id, 
44                                u32 flags);
45
46 /* Routes to insert/delete/probe in FIB */
47 typedef struct {
48   ip4_address_t address;
49   u32 mask_width;
50   u32 interface_id;             /* not an xx_if_index */
51 } test_route_t;
52
53 typedef struct {
54   /* Test routes in use */
55   test_route_t *route_pool;
56
57   /* Number of fake ethernets created */
58   u32 test_interfaces_created;
59 } test_main_t;
60
61 test_main_t test_main;
62
63 /* fake ethernet device class, distinct from "fake-ethX" */
64 static u8 * format_test_interface_name (u8 * s, va_list * args)
65 {
66   u32 dev_instance = va_arg (*args, u32);
67   return format (s, "test-eth%d", dev_instance);
68 }
69
70 static uword dummy_interface_tx (vlib_main_t * vm,
71                                  vlib_node_runtime_t * node,
72                                  vlib_frame_t * frame)
73 {
74   clib_warning ("you shouldn't be here, leaking buffers...");
75   return frame->n_vectors;
76 }
77
78 VNET_DEVICE_CLASS (test_interface_device_class,static) = {
79   .name = "Test interface",
80   .format_device_name = format_test_interface_name,
81   .tx_function = dummy_interface_tx,
82 };
83
84 static clib_error_t *
85 thrash (vlib_main_t * vm, 
86         unformat_input_t * main_input, vlib_cli_command_t * cmd_arg)
87 {
88   u32 seed = 0xdeaddabe;
89   u32 niter = 10;
90   u32 nroutes = 10;
91   u32 ninterfaces = 4;
92   f64 min_mask_bits = 7.0;
93   f64 max_mask_bits = 32.0;
94   u32 table_id = 11;            /* my amp goes to 11 (use fib 11) */
95   u32 table_index;
96   int iter, i;
97   u8 * cmd;
98   test_route_t *tr;
99   test_main_t *tm = &test_main;
100   ip4_main_t * im = &ip4_main;
101   vnet_main_t * vnm = vnet_get_main();
102   unformat_input_t cmd_input;
103   f64 rf;
104   u32 *masks = 0;
105   u32 tmp;
106   u32 hw_if_index;
107   clib_error_t * error = 0;
108   uword *p;
109   unformat_input_t _line_input, * line_input = &_line_input;
110   u8 hw_address[6];
111   ip4_fib_t * fib;
112   int verbose = 0;
113
114   /* Precompute mask width -> mask vector */
115   tmp = (u32)~0;
116   vec_validate (masks, 32);
117   for (i = 32; i > 0; i--)
118     {
119       masks [i] = tmp;
120       tmp <<= 1;
121     }
122
123   if (unformat_user (main_input, unformat_line_input, line_input))
124     {
125       while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
126         {
127           if (unformat (line_input, "seed %d", &seed))
128             ;
129           else if (unformat (line_input, "niter %d", &niter))
130             ;
131           else if (unformat (line_input, "nroutes %d", &nroutes))
132             ;
133           else if (unformat (line_input, "ninterfaces %d", &ninterfaces))
134             ;
135           else if (unformat (line_input, "min-mask-bits %d", &tmp))
136             min_mask_bits = (f64) tmp;
137           else if (unformat (line_input, "max-mask-bits %d", &tmp))
138             max_mask_bits = (f64) tmp;
139           else if (unformat (line_input, "verbose"))
140             verbose = 1;
141           else
142             return clib_error_return (0, "unknown input `%U'",
143                                       format_unformat_error, line_input);
144         }
145     }
146
147   /* Find or create FIB table 11 */
148   fib = ip4_fib_find_or_create_fib_by_table_id (table_id);
149
150   for (i = tm->test_interfaces_created; i < ninterfaces; i++)
151     {
152       vnet_hw_interface_t * hw;
153       memset (hw_address, 0, sizeof (hw_address));
154       hw_address[0] = 0xd0;
155       hw_address[1] = 0x0f;
156       hw_address[5] = i;
157
158       error = ethernet_register_interface
159         (vnm,
160          test_interface_device_class.index,
161          i /* instance */,
162          hw_address,
163          &hw_if_index, 
164          /* flag change */ 0);
165       
166       /* Fake interfaces use FIB table 11 */
167       hw = vnet_get_hw_interface (vnm, hw_if_index);
168       vec_validate (im->fib_index_by_sw_if_index, hw->sw_if_index);
169       im->fib_index_by_sw_if_index[hw->sw_if_index] = fib->index;
170       ip4_sw_interface_enable_disable(sw_if_index, 1);
171     }
172
173   tm->test_interfaces_created = ninterfaces;
174
175   /* Find fib index corresponding to FIB id 11 */
176   p = hash_get (im->fib_index_by_table_id, table_id);
177   if (p == 0)
178     {
179       vlib_cli_output (vm, "Couldn't map fib id %d to fib index\n",
180                        table_id);
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++)
189         {
190           int j;
191
192           pool_get (tm->route_pool, tr);
193           memset (tr, 0, sizeof (*tr));
194
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);
202
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             }
212
213           rf = random_f64 (&seed);
214           tr->interface_id = (u32) (rf * ninterfaces);
215         }
216       
217       /* Add them */
218       for (i = 0; i < nroutes; i++)
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         }
235       /* Probe them */
236       for (i = 0; i < nroutes; i++)
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, 
247                        tr->mask_width);
248             }
249         }
250
251       /* Delete them */
252       for (i = 0; i < nroutes; i++)
253         {
254           int j;
255           tr = pool_elt_at_index (tm->route_pool, i);
256           if (0)
257               cmd = format (0, "del table %d %U/%d via test-eth%d",
258                             table_id,
259                         format_ip4_address, &tr->address, 
260                             tr->mask_width, tr->interface_id);
261           else
262               cmd = format (0, "del table %d %U/%d",
263                             table_id,
264                             format_ip4_address, &tr->address, 
265                             tr->mask_width);
266           vec_add1(cmd,0);
267           if (verbose)
268             fformat(stderr, "ip route %s\n", cmd);
269           unformat_init_string (&cmd_input, (char *) cmd, vec_len(cmd)-1);
270           error = vnet_ip_route_cmd (vm, &cmd_input, cmd_arg);
271           if (error)
272             clib_error_report(error);
273           unformat_free (&cmd_input);
274           vec_free(cmd);
275
276           /* Make sure all undeleted routes still work */
277           for (j = i+1; j < nroutes; j++)
278             {
279               test_route_t *rr; /* remaining route */
280               rr = pool_elt_at_index (tm->route_pool, j);
281               if (!ip4_lookup_validate (&rr->address, table_index))
282                 {
283                   if (verbose)
284                     fformat (stderr, "test lookup table %d %U\n", 
285                              table_index, format_ip4_address, &rr->address);
286                   
287                   fformat (stderr, "FAIL: %U/%d AWOL\n",
288                            format_ip4_address, &rr->address, 
289                            rr->mask_width);
290                   fformat (stderr, " iter %d after %d of %d deletes\n",
291                            iter, i, nroutes);
292                   fformat (stderr, " last route deleted %U/%d\n",
293                            format_ip4_address, &tr->address,
294                            tr->mask_width);
295                 }
296             }
297         }
298
299       pool_free (tm->route_pool);
300     }
301   return 0;
302 }
303
304 /*?
305  * This command in not in the build by default. It is an internal
306  * command used to test the route functonality.
307  *
308  * Create test routes on IPv4 FIB table 11. Table will be created if it
309  * does not exist.
310  *
311  * There are several optional attributes:
312  * - If not provided, <seed> defaults to 0xdeaddabe.
313  * - If not provided, <num-iter> defaults to 10.
314  * - If not provided, <num-iface> defaults to 4.
315  * - If not provided, <min-mask> defaults to 7.0.
316  * - If not provided, <max-mask> defaults to 32.0.
317  *
318  * @cliexpar
319  * Example of how to run:
320  * @cliexcmd{test route}
321 ?*/
322 /* *INDENT-OFF* */
323 VLIB_CLI_COMMAND (test_route_command, static) = {
324     .path = "test route",
325     .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,
326     .function = thrash,
327 };
328 /* *INDENT-ON* */
329
330 clib_error_t *test_route_init (vlib_main_t *vm)
331 {
332   return 0;
333 }
334
335 VLIB_INIT_FUNCTION (test_route_init);