aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/auto_sdl/test/auto_sdl_test.c
blob: 690ebff7d112900a94b31c44de6e46eb07ac7027 (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
/* SPDX-License-Identifier: Apache-2.0
 * Copyright(c) 2024 Cisco Systems, Inc.
 */

#include <vnet/plugin/plugin.h>
#include <vpp/app/version.h>
#include <arpa/inet.h>
#include <vnet/session/session.h>
#include <vnet/session/session_rules_table.h>
#include <vnet/tcp/tcp_sdl.h>
#include <plugins/auto_sdl/auto_sdl.h>

#define AUTO_SDL_TEST_I(_cond, _comment, _args...)                            \
  ({                                                                          \
    int _evald = (_cond);                                                     \
    if (!(_evald))                                                            \
      {                                                                       \
	fformat (stderr, "FAIL:%d: " _comment "\n", __LINE__, ##_args);       \
      }                                                                       \
    else                                                                      \
      {                                                                       \
	fformat (stderr, "PASS:%d: " _comment "\n", __LINE__, ##_args);       \
      }                                                                       \
    _evald;                                                                   \
  })

#define AUTO_SDL_TEST(_cond, _comment, _args...)                              \
  {                                                                           \
    if (!AUTO_SDL_TEST_I (_cond, _comment, ##_args))                          \
      {                                                                       \
	return 1;                                                             \
      }                                                                       \
  }

static void
auto_sdl_test_disable_rt_backend_engine (vlib_main_t *vm)
{
  session_enable_disable_args_t args = { .is_en = 0,
					 .rt_engine_type =
					   RT_BACKEND_ENGINE_DISABLE };
  vnet_session_enable_disable (vm, &args);
}

static void
auto_sdl_test_enable_sdl_engine (vlib_main_t *vm)
{
  session_enable_disable_args_t args = { .is_en = 1,
					 .rt_engine_type =
					   RT_BACKEND_ENGINE_SDL };
  vnet_session_enable_disable (vm, &args);
}

static int
auto_sdl_test_auto_sdl (vlib_main_t *vm, unformat_input_t *input)
{
  u32 rmt_plen = 0;
  ip46_address_t rmt_ip = {};
  int fib_proto = ~0;
  u8 *ns_id = 0;
  auto_sdl_track_prefix_args_t args;
  app_namespace_t *app_ns;
  u8 *tag = 0;
  u32 action = 0;
  int error = 0;
  auto_sdl_config_args_t asdl_args = {
    .enable = 1,
    .remove_timeout = 300,
    .threshold = 1,
  };
  auto_sdl_plugin_methods_t auto_sdl_plugin;
  clib_error_t *init_res;

  auto_sdl_test_disable_rt_backend_engine (vm);
  auto_sdl_test_enable_sdl_engine (vm);
  if (session_sdl_is_enabled () == 0)
    {
      vlib_cli_output (vm, "session sdl engine is not enabled");
      return -1;
    }
  init_res = auto_sdl_plugin_exports_init (&auto_sdl_plugin);
  if (init_res)
    {
      vlib_cli_output (vm, "Error in auto sdl plugin init");
      return -1;
    }
  auto_sdl_plugin.config (&asdl_args);

  if (unformat_check_input (input) == UNFORMAT_END_OF_INPUT)
    {
      const char ip_str[] = "10.1.0.0";
      const char ip6_str[] = "2501:0db8:85a3:0000:0000:8a2e:0371:0";
      u32 address;
      ip6_address_t address6;
      memset (&args, 0, sizeof (args));
      rmt_plen = 32;
      fib_proto = FIB_PROTOCOL_IP4;
      app_ns = app_namespace_get_default ();
      inet_pton (AF_INET, ip_str, &address);
      address = htonl (address);
      for (int j = 1; j <= 10; j++)
	{
	  address += (j << 8);
	  for (int i = 1; i < 255; i++)
	    {
	      address++;
	      rmt_ip.ip4.as_u32 = ntohl (address);
	      args.prefix.fp_addr = rmt_ip;
	      args.prefix.fp_proto = fib_proto;
	      args.prefix.fp_len = rmt_plen;
	      args.action_index = action;
	      args.tag = tag;
	      args.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);

	      if (auto_sdl_plugin.track_prefix (&args) != 0)
		{
		  vlib_cli_output (vm, "error adding track prefix");
		  error = -1;
		  goto done;
		}
	    }
	}
      /* Add ip6 */
      inet_pton (AF_INET6, ip6_str, &address6);
      fib_proto = FIB_PROTOCOL_IP6;
      rmt_plen = 128;
      for (int i = 1; i < 255; i++)
	{
	  address = htonl (address6.as_u32[3]);
	  address++;
	  address6.as_u32[3] = ntohl (address);
	  memcpy (&rmt_ip.ip6, &address6, sizeof (address6));
	  args.prefix.fp_addr = rmt_ip;
	  args.prefix.fp_proto = fib_proto;
	  args.prefix.fp_len = rmt_plen;
	  args.action_index = action;
	  args.tag = tag;
	  args.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);

	  if (auto_sdl_plugin.track_prefix (&args) != 0)
	    {
	      vlib_cli_output (vm, "error adding track prefix");
	      error = -1;
	      goto done;
	    }
	}

      uword expected = 254 * 10 + 254;
      uword total = auto_sdl_plugin.pool_size ();
      AUTO_SDL_TEST ((total == expected),
		     "total auto sdl entries is %u, expected %u", total,
		     expected);
      auto_sdl_test_disable_rt_backend_engine (vm);
      total = auto_sdl_plugin.pool_size ();
      expected = 0;
      AUTO_SDL_TEST ((total == expected),
		     "total auto sdl entries is %u, expected %u", total,
		     expected);
      goto done;
    }

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "%U/%d", unformat_ip4_address, &rmt_ip.ip4,
		    &rmt_plen))
	fib_proto = FIB_PROTOCOL_IP4;
      else if (unformat (input, "%U/%d", unformat_ip6_address, &rmt_ip.ip6,
			 &rmt_plen))
	fib_proto = FIB_PROTOCOL_IP6;
      else if (unformat (input, "action %d", &action))
	;
      else if (unformat (input, "tag %_%v%_", &tag))
	;
      else if (unformat (input, "appns %_%v%_", &ns_id))
	;
      else
	{
	  vlib_cli_output (vm, "unknown input `%U'", format_unformat_error,
			   input);
	  error = -1;
	  goto done;
	}
    }

  if (fib_proto == ~0)
    {
      vlib_cli_output (vm, "tracked prefix must be entered");
      error = -1;
      goto done;
    }

  if (vec_len (tag) > SESSION_RULE_TAG_MAX_LEN)
    {
      vlib_cli_output (vm, "tag too long (max u64)");
      error = -1;
      goto done;
    }

  if (ns_id)
    {
      app_ns = app_namespace_get_from_id (ns_id);
      if (!app_ns)
	{
	  vlib_cli_output (vm, "namespace %v does not exist", ns_id);
	  error = -1;
	  goto done;
	}
    }
  else
    app_ns = app_namespace_get_default ();

  memset (&args, 0, sizeof (args));
  args.prefix.fp_addr = rmt_ip;
  args.prefix.fp_proto = fib_proto;
  args.prefix.fp_len = rmt_plen;
  args.action_index = action;
  args.tag = tag;
  args.fib_index = app_namespace_get_fib_index (app_ns, fib_proto);

  if (auto_sdl_plugin.track_prefix (&args) != 0)
    {
      vlib_cli_output (vm, "error adding track prefix");
      error = -1;
    }
done:
  vec_free (ns_id);
  vec_free (tag);
  return error;

  return 0;
}

static clib_error_t *
auto_sdl_test_command_fn (vlib_main_t *vm, unformat_input_t *input,
			  vlib_cli_command_t *cmd_arg)
{
  int res = 0;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "all"))
	;
      res = auto_sdl_test_auto_sdl (vm, input);
      goto done;
    }

done:
  if (res)
    return clib_error_return (0, "Auto SDL unit test failed");
  return 0;
}

VLIB_CLI_COMMAND (auto_sdl_test_command, static) = {
  .path = "test auto-sdl",
  .short_help = "auto-sdl unit tests",
  .function = auto_sdl_test_command_fn,
};

VLIB_PLUGIN_REGISTER () = {
  .version = VPP_BUILD_VER,
  .description = "Auto SDL - Unit Test",
  .default_disabled = 1,
};

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