A Protocol Independent Hierarchical FIB (VPP-352)
[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  * ip4 FIB tester. Add, probe, delete a bunch of
20  * random routes / masks and make sure that the mtrie agrees with
21  * the hash-table FIB.
22  * 
23  * Manipulate the FIB by means of the debug CLI commands, to minimize
24  * the chances of doing something idiotic.
25  */
26
27 /* 
28  * These routines need to be redeclared non-static elsewhere.
29  *
30  * Also: rename ip_route() -> vnet_ip_route_cmd() and add the usual
31  * test_route_init() call to main.c
32  */
33 clib_error_t *
34 vnet_ip_route_cmd (vlib_main_t * vm, 
35                    unformat_input_t * input, vlib_cli_command_t * cmd_arg);
36
37 int ip4_lookup_validate (ip4_address_t *a, u32 fib_index0);
38
39 ip4_fib_t *
40 find_fib_by_table_index_or_id (ip4_main_t * im, u32 table_index_or_id, 
41                                u32 flags);
42
43 /* Routes to insert/delete/probe in FIB */
44 typedef struct {
45   ip4_address_t address;
46   u32 mask_width;
47   u32 interface_id;             /* not an xx_if_index */
48 } test_route_t;
49
50 typedef struct {
51   /* Test routes in use */
52   test_route_t *route_pool;
53
54   /* Number of fake ethernets created */
55   u32 test_interfaces_created;
56 } test_main_t;
57
58 test_main_t test_main;
59
60 /* fake ethernet device class, distinct from "fake-ethX" */
61 static u8 * format_test_interface_name (u8 * s, va_list * args)
62 {
63   u32 dev_instance = va_arg (*args, u32);
64   return format (s, "test-eth%d", dev_instance);
65 }
66
67 static uword dummy_interface_tx (vlib_main_t * vm,
68                                  vlib_node_runtime_t * node,
69                                  vlib_frame_t * frame)
70 {
71   clib_warning ("you shouldn't be here, leaking buffers...");
72   return frame->n_vectors;
73 }
74
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,
79 };
80
81 static clib_error_t *
82 thrash (vlib_main_t * vm, 
83         unformat_input_t * main_input, vlib_cli_command_t * cmd_arg)
84 {
85   u32 seed = 0xdeaddabe;
86   u32 niter = 10;
87   u32 nroutes = 10;
88   u32 ninterfaces = 4;
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) */
92   u32 table_index;
93   int iter, i;
94   u8 * cmd;
95   test_route_t *tr;
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;
100   f64 rf;
101   u32 *masks = 0;
102   u32 tmp;
103   u32 hw_if_index;
104   clib_error_t * error = 0;
105   uword *p;
106   unformat_input_t _line_input, * line_input = &_line_input;
107   u8 hw_address[6];
108   ip4_fib_t * fib;
109   int verbose = 0;
110
111   /* Precompute mask width -> mask vector */
112   tmp = (u32)~0;
113   vec_validate (masks, 32);
114   for (i = 32; i > 0; i--)
115     {
116       masks [i] = tmp;
117       tmp <<= 1;
118     }
119
120   if (unformat_user (main_input, unformat_line_input, line_input))
121     {
122       while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
123         {
124           if (unformat (line_input, "seed %d", &seed))
125             ;
126           else if (unformat (line_input, "niter %d", &niter))
127             ;
128           else if (unformat (line_input, "nroutes %d", &nroutes))
129             ;
130           else if (unformat (line_input, "ninterfaces %d", &ninterfaces))
131             ;
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"))
137             verbose = 1;
138           else
139             return clib_error_return (0, "unknown input `%U'",
140                                       format_unformat_error, line_input);
141         }
142     }
143
144   /* Find or create FIB table 11 */
145   fib = ip4_fib_find_or_create_fib_by_table_id (table_id);
146
147   for (i = tm->test_interfaces_created; i < ninterfaces; i++)
148     {
149       vnet_hw_interface_t * hw;
150       memset (hw_address, 0, sizeof (hw_address));
151       hw_address[0] = 0xd0;
152       hw_address[1] = 0x0f;
153       hw_address[5] = i;
154
155       error = ethernet_register_interface
156         (vnm,
157          test_interface_device_class.index,
158          i /* instance */,
159          hw_address,
160          &hw_if_index, 
161          /* flag change */ 0);
162       
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;
167       ip4_sw_interface_enable_disable(sw_if_index, 1);
168     }
169
170   tm->test_interfaces_created = ninterfaces;
171
172   /* Find fib index corresponding to FIB id 11 */
173   p = hash_get (im->fib_index_by_table_id, table_id);
174   if (p == 0)
175     {
176       vlib_cli_output (vm, "Couldn't map fib id %d to fib index\n",
177                        table_id);
178       return 0;
179     }
180   table_index = p[0];
181
182   for (iter = 0; iter < niter; iter++)
183     {
184       /* Pick random routes to install */
185       for (i = 0; i < nroutes; i++)
186         {
187           int j;
188
189           pool_get (tm->route_pool, tr);
190           memset (tr, 0, sizeof (*tr));
191
192         again:
193           rf = random_f64 (&seed);
194           tr->mask_width = (u32) (min_mask_bits 
195                                  + rf * (max_mask_bits - min_mask_bits));
196           tmp = random_u32 (&seed);
197           tmp &= masks[tr->mask_width];
198           tr->address.as_u32 = clib_host_to_net_u32(tmp);
199
200           /* We can't add the same address/mask twice... */
201           for (j = 0; j < i; j++)
202             {
203               test_route_t *prev;
204               prev = pool_elt_at_index (tm->route_pool, j);
205               if ((prev->address.as_u32 == tr->address.as_u32)
206                   && (prev->mask_width == tr->mask_width))
207                 goto again;
208             }
209
210           rf = random_f64 (&seed);
211           tr->interface_id = (u32) (rf * ninterfaces);
212         }
213       
214       /* Add them */
215       for (i = 0; i < nroutes; i++)
216         {
217           tr = pool_elt_at_index (tm->route_pool, i);
218           cmd = format (0, "add table %d %U/%d via test-eth%d",
219                         table_id,
220                         format_ip4_address, &tr->address, 
221                         tr->mask_width, tr->interface_id);
222           vec_add1(cmd,0);
223           if (verbose)
224             fformat(stderr, "ip route %s\n", cmd);
225           unformat_init_string (&cmd_input, (char *) cmd, vec_len(cmd)-1);
226           error = vnet_ip_route_cmd (vm, &cmd_input, cmd_arg);
227           if (error)
228             clib_error_report(error);
229           unformat_free (&cmd_input);
230           vec_free(cmd);
231         }
232       /* Probe them */
233       for (i = 0; i < nroutes; i++)
234         {
235           tr = pool_elt_at_index (tm->route_pool, i);
236           if (!ip4_lookup_validate (&tr->address, table_index))
237             {
238               if (verbose)
239                 fformat (stderr, "test lookup table %d %U\n", 
240                          table_index, format_ip4_address, &tr->address);
241                        
242               fformat (stderr, "FAIL-after-insert: %U/%d\n",
243                        format_ip4_address, &tr->address, 
244                        tr->mask_width);
245             }
246         }
247
248       /* Delete them */
249       for (i = 0; i < nroutes; i++)
250         {
251           int j;
252           tr = pool_elt_at_index (tm->route_pool, i);
253           if (0)
254               cmd = format (0, "del table %d %U/%d via test-eth%d",
255                             table_id,
256                         format_ip4_address, &tr->address, 
257                             tr->mask_width, tr->interface_id);
258           else
259               cmd = format (0, "del table %d %U/%d",
260                             table_id,
261                             format_ip4_address, &tr->address, 
262                             tr->mask_width);
263           vec_add1(cmd,0);
264           if (verbose)
265             fformat(stderr, "ip route %s\n", cmd);
266           unformat_init_string (&cmd_input, (char *) cmd, vec_len(cmd)-1);
267           error = vnet_ip_route_cmd (vm, &cmd_input, cmd_arg);
268           if (error)
269             clib_error_report(error);
270           unformat_free (&cmd_input);
271           vec_free(cmd);
272
273           /* Make sure all undeleted routes still work */
274           for (j = i+1; j < nroutes; j++)
275             {
276               test_route_t *rr; /* remaining route */
277               rr = pool_elt_at_index (tm->route_pool, j);
278               if (!ip4_lookup_validate (&rr->address, table_index))
279                 {
280                   if (verbose)
281                     fformat (stderr, "test lookup table %d %U\n", 
282                              table_index, format_ip4_address, &rr->address);
283                   
284                   fformat (stderr, "FAIL: %U/%d AWOL\n",
285                            format_ip4_address, &rr->address, 
286                            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,
291                            tr->mask_width);
292                 }
293             }
294         }
295
296       pool_free (tm->route_pool);
297     }
298   return 0;
299 }
300
301 VLIB_CLI_COMMAND (test_route_command, static) = {
302     .path = "test route",
303     .short_help = "test route",
304     .function = thrash,
305 };
306
307 clib_error_t *test_route_init (vlib_main_t *vm)
308 {
309   return 0;
310 }
311
312 VLIB_INIT_FUNCTION (test_route_init);