Initial commit of vpp code.
[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 = find_ip4_fib_by_table_index_or_id (im, table_id, IP4_ROUTE_FLAG_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     }
168
169   tm->test_interfaces_created = ninterfaces;
170
171   /* Find fib index corresponding to FIB id 11 */
172   p = hash_get (im->fib_index_by_table_id, table_id);
173   if (p == 0)
174     {
175       vlib_cli_output (vm, "Couldn't map fib id %d to fib index\n",
176                        table_id);
177       return 0;
178     }
179   table_index = p[0];
180
181   for (iter = 0; iter < niter; iter++)
182     {
183       /* Pick random routes to install */
184       for (i = 0; i < nroutes; i++)
185         {
186           int j;
187
188           pool_get (tm->route_pool, tr);
189           memset (tr, 0, sizeof (*tr));
190
191         again:
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);
198
199           /* We can't add the same address/mask twice... */
200           for (j = 0; j < i; j++)
201             {
202               test_route_t *prev;
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))
206                 goto again;
207             }
208
209           rf = random_f64 (&seed);
210           tr->interface_id = (u32) (rf * ninterfaces);
211         }
212       
213       /* Add them */
214       for (i = 0; i < nroutes; i++)
215         {
216           tr = pool_elt_at_index (tm->route_pool, i);
217           cmd = format (0, "add table %d %U/%d via test-eth%d",
218                         table_id,
219                         format_ip4_address, &tr->address, 
220                         tr->mask_width, tr->interface_id);
221           vec_add1(cmd,0);
222           if (verbose)
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);
226           if (error)
227             clib_error_report(error);
228           unformat_free (&cmd_input);
229           vec_free(cmd);
230         }
231       /* Probe them */
232       for (i = 0; i < nroutes; i++)
233         {
234           tr = pool_elt_at_index (tm->route_pool, i);
235           if (!ip4_lookup_validate (&tr->address, table_index))
236             {
237               if (verbose)
238                 fformat (stderr, "test lookup table %d %U\n", 
239                          table_index, format_ip4_address, &tr->address);
240                        
241               fformat (stderr, "FAIL-after-insert: %U/%d\n",
242                        format_ip4_address, &tr->address, 
243                        tr->mask_width);
244             }
245         }
246
247       /* Delete them */
248       for (i = 0; i < nroutes; i++)
249         {
250           int j;
251           tr = pool_elt_at_index (tm->route_pool, i);
252           if (0)
253               cmd = format (0, "del table %d %U/%d via test-eth%d",
254                             table_id,
255                         format_ip4_address, &tr->address, 
256                             tr->mask_width, tr->interface_id);
257           else
258               cmd = format (0, "del table %d %U/%d",
259                             table_id,
260                             format_ip4_address, &tr->address, 
261                             tr->mask_width);
262           vec_add1(cmd,0);
263           if (verbose)
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);
267           if (error)
268             clib_error_report(error);
269           unformat_free (&cmd_input);
270           vec_free(cmd);
271
272           /* Make sure all undeleted routes still work */
273           for (j = i+1; j < nroutes; j++)
274             {
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))
278                 {
279                   if (verbose)
280                     fformat (stderr, "test lookup table %d %U\n", 
281                              table_index, format_ip4_address, &rr->address);
282                   
283                   fformat (stderr, "FAIL: %U/%d AWOL\n",
284                            format_ip4_address, &rr->address, 
285                            rr->mask_width);
286                   fformat (stderr, " iter %d after %d of %d deletes\n",
287                            iter, i, nroutes);
288                   fformat (stderr, " last route deleted %U/%d\n",
289                            format_ip4_address, &tr->address,
290                            tr->mask_width);
291                 }
292             }
293         }
294
295       pool_free (tm->route_pool);
296     }
297   return 0;
298 }
299
300 VLIB_CLI_COMMAND (test_route_command, static) = {
301     .path = "test route",
302     .short_help = "test route",
303     .function = thrash,
304 };
305
306 clib_error_t *test_route_init (vlib_main_t *vm)
307 {
308   return 0;
309 }
310
311 VLIB_INIT_FUNCTION (test_route_init);