aboutsummaryrefslogtreecommitdiffstats
path: root/vpp/vnet/vnet/ip/ip4_test.c
blob: 45d171130df36ef3fd62764ec95196ec7dc9326d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
 * Copyright (c) 2015 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <vnet/ip/ip.h>
#include <vnet/ethernet/ethernet.h>

/**
 * @file
 * @brief IPv4 FIB Tester.
 *
 * Not compiled in by default. IPv4 FIB tester. Add, probe, delete a bunch of
 * random routes / masks and make sure that the mtrie agrees with
 * the hash-table FIB.
 *
 * Manipulate the FIB by means of the debug CLI commands, to minimize
 * the chances of doing something idiotic.
 */

/*
 * These routines need to be redeclared non-static elsewhere.
 *
 * Also: rename ip_route() -> vnet_ip_route_cmd() and add the usual
 * test_route_init() call to main.c
 */
clib_error_t *vnet_ip_route_cmd (vlib_main_t * vm,
				 unformat_input_t * input,
				 vlib_cli_command_t * cmd_arg);

int ip4_lookup_validate (ip4_address_t * a, u32 fib_index0);

ip4_fib_t *find_fib_by_table_index_or_id (ip4_main_t * im,
					  u32 table_index_or_id, u32 flags);

/* Routes to insert/delete/probe in FIB */
typedef struct
{
  ip4_address_t address;
  u32 mask_width;
  u32 interface_id;		/* not an xx_if_index */
} test_route_t;

typedef struct
{
  /* Test routes in use */
  test_route_t *route_pool;

  /* Number of fake ethernets created */
  u32 test_interfaces_created;
} test_main_t;

test_main_t test_main;

/* fake ethernet device class, distinct from "fake-ethX" */
static u8 *
format_test_interface_name (u8 * s, va_list * args)
{
  u32 dev_instance = va_arg (*args, u32);
  return format (s, "test-eth%d", dev_instance);
}

static uword
dummy_interface_tx (vlib_main_t * vm,
		    vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  clib_warning ("you shouldn't be here, leaking buffers...");
  return frame->n_vectors;
}

/* *INDENT-OFF* */
VNET_DEVICE_CLASS (test_interface_device_class,static) = {
  .name = "Test interface",
  .format_device_name = format_test_interface_name,
  .tx_function = dummy_interface_tx,
};
/* *INDENT-ON* */

static clib_error_t *
thrash (vlib_main_t * vm,
	unformat_input_t * main_input, vlib_cli_command_t * cmd_arg)
{
  u32 seed = 0xdeaddabe;
  u32 niter = 10;
  u32 nroutes = 10;
  u32 ninterfaces = 4;
  f64 min_mask_bits = 7.0;
  f64 max_mask_bits = 32.0;
  u32 table_id = 11;		/* my amp goes to 11 (use fib 11) */
  u32 table_index;
  int iter, i;
  u8 *cmd;
  test_route_t *tr;
  test_main_t *tm = &test_main;
  ip4_main_t *im = &ip4_main;
  vnet_main_t *vnm = vnet_get_main ();
  unformat_input_t cmd_input;
  f64 rf;
  u32 *masks = 0;
  u32 tmp;
  u32 hw_if_index;
  clib_error_t *error = 0;
  uword *p;
  unformat_input_t _line_input, *line_input = &_line_input;
  u8 hw_address[6];
  ip4_fib_t *fib;
  int verbose = 0;

  /* Precompute mask width -> mask vector */
  tmp = (u32) ~ 0;
  vec_validate (masks, 32);
  for (i = 32; i > 0; i--)
    {
      masks[i] = tmp;
      tmp <<= 1;
    }

  if (unformat_user (main_input, unformat_line_input, line_input))
    {
      while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
	{
	  if (unformat (line_input, "seed %d", &seed))
	    ;
	  else if (unformat (line_input, "niter %d", &niter))
	    ;
	  else if (unformat (line_input, "nroutes %d", &nroutes))
	    ;
	  else if (unformat (line_input, "ninterfaces %d", &ninterfaces))
	    ;
	  else if (unformat (line_input, "min-mask-bits %d", &tmp))
	    min_mask_bits = (f64) tmp;
	  else if (unformat (line_input, "max-mask-bits %d", &tmp))
	    max_mask_bits = (f64) tmp;
	  else if (unformat (line_input, "verbose"))
	    verbose = 1;
	  else
	    return clib_error_return (0, "unknown input `%U'",
				      format_unformat_error, line_input);
	}
    }

  /* Find or create FIB table 11 */
  fib = ip4_fib_find_or_create_fib_by_table_id (table_id);

  for (i = tm->test_interfaces_created; i < ninterfaces; i++)
    {
      vnet_hw_interface_t *hw;
      memset (hw_address, 0, sizeof (hw_address));
      hw_address[0] = 0xd0;
      hw_address[1] = 0x0f;
      hw_address[5] = i;

      error = ethernet_register_interface
	(vnm, test_interface_device_class.index, i /* instance */ ,
	 hw_address, &hw_if_index,
	 /* flag change */ 0);

      /* Fake interfaces use FIB table 11 */
      hw = vnet_get_hw_interface (vnm, hw_if_index);
      vec_validate (im->fib_index_by_sw_if_index, hw->sw_if_index);
      im->fib_index_by_sw_if_index[hw->sw_if_index] = fib->index;
      ip4_sw_interface_enable_disable (sw_if_index, 1);
    }

  tm->test_interfaces_created = ninterfaces;

  /* Find fib index corresponding to FIB id 11 */
  p = hash_get (im->fib_index_by_table_id, table_id);
  if (p == 0)
    {
      vlib_cli_output (vm, "Couldn't map fib id %d to fib index\n", table_id);
      return 0;
    }
  table_index = p[0];

  for (iter = 0; iter < niter; iter++)
    {
      /* Pick random routes to install */
      for (i = 0; i < nroutes; i++)
	{
	  int j;

	  pool_get (tm->route_pool, tr);
	  memset (tr, 0, sizeof (*tr));

	again:
	  rf = random_f64 (&seed);
	  tr->mask_width = (u32) (min_mask_bits
				  + rf * (max_mask_bits - min_mask_bits));
	  tmp = random_u32 (&seed);
	  tmp &= masks[tr->mask_width];
	  tr->address.as_u32 = clib_host_to_net_u32 (tmp);

	  /* We can't add the same address/mask twice... */
	  for (j = 0; j < i; j++)
	    {
	      test_route_t *prev;
	      prev = pool_elt_at_index (tm->route_pool, j);
	      if ((prev->address.as_u32 == tr->address.as_u32)
		  && (prev->mask_width == tr->mask_width))
		goto again;
	    }

	  rf = random_f64 (&seed);
	  tr->interface_id = (u32) (rf * ninterfaces);
	}

      /* Add them */
      for (i = 0; i < nroutes; i++)
	{
	  tr = pool_elt_at_index (tm->route_pool, i);
	  cmd = format (0, "add table %d %U/%d via test-eth%d",
			table_id,
			format_ip4_address, &tr->address,
			tr->mask_width, tr->interface_id);
	  vec_add1 (cmd, 0);
	  if (verbose)
	    fformat (stderr, "ip route %s\n", cmd);
	  unformat_init_string (&cmd_input, (char *) cmd, vec_len (cmd) - 1);
	  error = vnet_ip_route_cmd (vm, &cmd_input, cmd_arg);
	  if (error)
	    clib_error_report (error);
	  unformat_free (&cmd_input);
	  vec_free (cmd);
	}
      /* Probe them */
      for (i = 0; i < nroutes; i++)
	{
	  tr = pool_elt_at_index (tm->route_pool, i);
	  if (!ip4_lookup_validate (&tr->address, table_index))
	    {
	      if (verbose)
		fformat (stderr, "test lookup table %d %U\n",
			 table_index, format_ip4_address, &tr->address);

	      fformat (stderr, "FAIL-after-insert: %U/%d\n",
		       format_ip4_address, &tr->address, tr->mask_width);
	    }
	}

      /* Delete them */
      for (i = 0; i < nroutes; i++)
	{
	  int j;
	  tr = pool_elt_at_index (tm->route_pool, i);
	  if (0)
	    cmd = format (0, "del table %d %U/%d via test-eth%d",
			  table_id,
			  format_ip4_address, &tr->address,
			  tr->mask_width, tr->interface_id);
	  else
	    cmd = format (0, "del table %d %U/%d",
			  table_id,
			  format_ip4_address, &tr->address, tr->mask_width);
	  vec_add1 (cmd, 0);
	  if (verbose)
	    fformat (stderr, "ip route %s\n", cmd);
	  unformat_init_string (&cmd_input, (char *) cmd, vec_len (cmd) - 1);
	  error = vnet_ip_route_cmd (vm, &cmd_input, cmd_arg);
	  if (error)
	    clib_error_report (error);
	  unformat_free (&cmd_input);
	  vec_free (cmd);

	  /* Make sure all undeleted routes still work */
	  for (j = i + 1; j < nroutes; j++)
	    {
	      test_route_t *rr;	/* remaining route */
	      rr = pool_elt_at_index (tm->route_pool, j);
	      if (!ip4_lookup_validate (&rr->address, table_index))
		{
		  if (verbose)
		    fformat (stderr, "test lookup table %d %U\n",
			     table_index, format_ip4_address, &rr->address);

		  fformat (stderr, "FAIL: %U/%d AWOL\n",
			   format_ip4_address, &rr->address, rr->mask_width);
		  fformat (stderr, " iter %d after %d of %d deletes\n",
			   iter, i, nroutes);
		  fformat (stderr, " last route deleted %U/%d\n",
			   format_ip4_address, &tr->address, tr->mask_width);
		}
	    }
	}

      pool_free (tm->route_pool);
    }
  return 0;
}

/*?
 * This command in not in the build by default. It is an internal
 * command used to test the route functonality.
 *
 * Create test routes on IPv4 FIB table 11. Table will be created if it
 * does not exist.
 *
 * There are several optional attributes:
 * - If not provided, <seed> defaults to 0xdeaddabe.
 * - If not provided, <num-iter> defaults to 10.
 * - If not provided, <num-iface> defaults to 4.
 * - If not provided, <min-mask> defaults to 7.0.
 * - If not provided, <max-mask> defaults to 32.0.
 *
 * @cliexpar
 * Example of how to run:
 * @cliexcmd{test route}
?*/
/* *INDENT-OFF* */
VLIB_CLI_COMMAND (test_route_command, static) = {
    .path = "test route",
    .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,
    .function = thrash,
};
/* *INDENT-ON* */

clib_error_t *
test_route_init (vlib_main_t * vm)
{
  return 0;
}

VLIB_INIT_FUNCTION (test_route_init);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */