2 * Copyright (c) 2017 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/session/application_namespace.h>
17 #include <vnet/session/application_interface.h>
18 #include <vnet/session/application.h>
19 #include <vnet/session/session.h>
21 #define SESSION_TEST_I(_cond, _comment, _args...) \
23 int _evald = (_cond); \
25 fformat(stderr, "FAIL:%d: " _comment "\n", \
28 fformat(stderr, "PASS:%d: " _comment "\n", \
34 #define SESSION_TEST(_cond, _comment, _args...) \
36 if (!SESSION_TEST_I(_cond, _comment, ##_args)) { \
42 dummy_session_reset_callback (stream_session_t * s)
44 clib_warning ("called...");
48 dummy_session_connected_callback (u32 app_index, u32 api_context,
49 stream_session_t * s, u8 is_fail)
51 clib_warning ("called...");
56 dummy_add_segment_callback (u32 client_index, const u8 * seg_name,
59 clib_warning ("called...");
64 dummy_redirect_connect_callback (u32 client_index, void *mp)
66 return VNET_API_ERROR_SESSION_REDIRECT;
70 dummy_session_disconnect_callback (stream_session_t * s)
72 clib_warning ("called...");
76 dummy_session_accept_callback (stream_session_t * s)
78 clib_warning ("called...");
83 dummy_server_rx_callback (stream_session_t * s)
85 clib_warning ("called...");
90 static session_cb_vft_t dummy_session_cbs = {
91 .session_reset_callback = dummy_session_reset_callback,
92 .session_connected_callback = dummy_session_connected_callback,
93 .session_accept_callback = dummy_session_accept_callback,
94 .session_disconnect_callback = dummy_session_disconnect_callback,
95 .builtin_server_rx_callback = dummy_server_rx_callback,
96 .redirect_connect_callback = dummy_redirect_connect_callback,
101 session_test_namespace (vlib_main_t * vm, unformat_input_t * input)
103 u64 options[SESSION_OPTIONS_N_OPTIONS], dummy_secret = 1234;
104 u32 server_index, server_st_index, server_local_st_index;
105 u32 dummy_port = 1234, local_listener, client_index;
106 u32 dummy_api_context = 4321, dummy_client_api_index = 1234;
107 u32 dummy_server_api_index = ~0, sw_if_index = 0;
108 session_endpoint_t server_sep = SESSION_ENDPOINT_NULL;
109 session_endpoint_t client_sep = SESSION_ENDPOINT_NULL;
110 session_endpoint_t intf_sep = SESSION_ENDPOINT_NULL;
111 clib_error_t *error = 0;
112 u8 *ns_id = format (0, "appns1"), intf_mac[6];
113 app_namespace_t *app_ns;
114 u8 segment_name[128];
115 application_t *server;
119 server_sep.is_ip4 = 1;
120 server_sep.port = dummy_port;
121 client_sep.is_ip4 = 1;
122 client_sep.port = dummy_port;
123 memset (options, 0, sizeof (options));
124 memset (intf_mac, 0, sizeof (intf_mac));
126 options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_BUILTIN_APP;
127 options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_ACCEPT_REDIRECT;
128 vnet_app_attach_args_t attach_args = {
129 .api_client_index = ~0,
132 .session_cb_vft = &dummy_session_cbs,
133 .segment_name = segment_name,
136 vnet_bind_args_t bind_args = {
141 vnet_connect_args_t connect_args = {
147 vnet_unbind_args_t unbind_args = {
148 .handle = bind_args.handle,
152 vnet_app_detach_args_t detach_args = {
156 ip4_address_t intf_addr = {
157 .as_u32 = clib_host_to_net_u32 (0x06000105),
160 intf_sep.ip.ip4 = intf_addr;
162 intf_sep.port = dummy_port;
165 * Insert namespace and lookup
168 vnet_app_namespace_add_del_args_t ns_args = {
170 .secret = dummy_secret,
171 .sw_if_index = APP_NAMESPACE_INVALID_INDEX,
174 error = vnet_app_namespace_add_del (&ns_args);
175 SESSION_TEST ((error == 0), "app ns insertion should succeed: %d",
176 clib_error_get_code (error));
178 app_ns = app_namespace_get_from_id (ns_id);
179 SESSION_TEST ((app_ns != 0), "should find ns %v status", ns_id);
180 SESSION_TEST ((app_ns->ns_secret == dummy_secret), "secret should be %d",
182 SESSION_TEST ((app_ns->sw_if_index == APP_NAMESPACE_INVALID_INDEX),
183 "sw_if_index should be invalid");
186 * Try application attach with wrong secret
189 options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
190 options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
191 options[APP_OPTIONS_NAMESPACE_SECRET] = dummy_secret - 1;
192 attach_args.namespace_id = ns_id;
193 attach_args.api_client_index = dummy_server_api_index;
195 error = vnet_application_attach (&attach_args);
196 SESSION_TEST ((error != 0), "app attachment should fail");
197 code = clib_error_get_code (error);
198 SESSION_TEST ((code == VNET_API_ERROR_APP_WRONG_NS_SECRET),
199 "code should be wrong ns secret: %d", code);
202 * Attach server with global default scope
204 options[APP_OPTIONS_FLAGS] &= ~APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
205 options[APP_OPTIONS_FLAGS] &= ~APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
206 options[APP_OPTIONS_NAMESPACE_SECRET] = 0;
207 attach_args.namespace_id = 0;
208 attach_args.api_client_index = dummy_server_api_index;
209 error = vnet_application_attach (&attach_args);
210 SESSION_TEST ((error == 0), "server attachment should work");
211 server_index = attach_args.app_index;
212 server = application_get (server_index);
213 SESSION_TEST ((server->ns_index == 0),
214 "server should be in the default ns");
216 bind_args.app_index = server_index;
217 error = vnet_bind (&bind_args);
218 SESSION_TEST ((error == 0), "server bind should work");
220 server_st_index = application_session_table (server, FIB_PROTOCOL_IP4);
221 s = session_lookup_listener (server_st_index, &server_sep);
222 SESSION_TEST ((s != 0), "listener should exist in global table");
223 SESSION_TEST ((s->app_index == server_index), "app_index should be that of "
225 server_local_st_index = application_local_session_table (server);
226 SESSION_TEST ((server_local_st_index == APP_INVALID_INDEX),
227 "server shouldn't have access to local table");
229 unbind_args.app_index = server_index;
230 error = vnet_unbind (&unbind_args);
231 SESSION_TEST ((error == 0), "unbind should work");
233 s = session_lookup_listener (server_st_index, &server_sep);
234 SESSION_TEST ((s == 0), "listener should not exist in global table");
236 detach_args.app_index = server_index;
237 vnet_application_detach (&detach_args);
240 * Attach server with local and global scope
242 options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
243 options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
244 options[APP_OPTIONS_NAMESPACE_SECRET] = dummy_secret;
245 attach_args.namespace_id = ns_id;
246 attach_args.api_client_index = dummy_server_api_index;
247 error = vnet_application_attach (&attach_args);
248 SESSION_TEST ((error == 0), "server attachment should work");
249 server_index = attach_args.app_index;
250 server = application_get (server_index);
251 SESSION_TEST ((server->ns_index == app_namespace_index (app_ns)),
252 "server should be in the right ns");
254 bind_args.app_index = server_index;
255 error = vnet_bind (&bind_args);
256 SESSION_TEST ((error == 0), "bind should work");
257 server_st_index = application_session_table (server, FIB_PROTOCOL_IP4);
258 s = session_lookup_listener (server_st_index, &server_sep);
259 SESSION_TEST ((s != 0), "listener should exist in global table");
260 SESSION_TEST ((s->app_index == server_index), "app_index should be that of "
262 server_local_st_index = application_local_session_table (server);
264 session_lookup_local_session_endpoint (server_local_st_index,
266 SESSION_TEST ((local_listener != SESSION_INVALID_INDEX),
267 "listener should exist in local table");
270 * Try client connect with 1) local scope 2) global scope
272 options[APP_OPTIONS_FLAGS] &= ~APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
273 attach_args.api_client_index = dummy_client_api_index;
274 error = vnet_application_attach (&attach_args);
275 SESSION_TEST ((error == 0), "client attachment should work");
276 client_index = attach_args.app_index;
277 connect_args.api_context = dummy_api_context;
278 connect_args.app_index = client_index;
279 error = vnet_connect (&connect_args);
280 SESSION_TEST ((error != 0), "client connect should return error code");
281 code = clib_error_get_code (error);
282 SESSION_TEST ((code == VNET_API_ERROR_INVALID_VALUE),
283 "error code should be invalid value (zero ip)");
284 connect_args.sep.ip.ip4.as_u8[0] = 127;
285 error = vnet_connect (&connect_args);
286 SESSION_TEST ((error != 0), "client connect should return error code");
287 code = clib_error_get_code (error);
288 SESSION_TEST ((code == VNET_API_ERROR_SESSION_REDIRECT),
289 "error code should be redirect");
290 detach_args.app_index = client_index;
291 vnet_application_detach (&detach_args);
293 options[APP_OPTIONS_FLAGS] &= ~APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
294 options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
295 attach_args.api_client_index = dummy_client_api_index;
296 error = vnet_application_attach (&attach_args);
297 SESSION_TEST ((error == 0), "client attachment should work");
298 error = vnet_connect (&connect_args);
299 SESSION_TEST ((error != 0), "client connect should return error code");
300 code = clib_error_get_code (error);
301 SESSION_TEST ((code == VNET_API_ERROR_SESSION_CONNECT),
302 "error code should be connect (nothing in local scope)");
303 detach_args.app_index = client_index;
304 vnet_application_detach (&detach_args);
307 * Unbind and detach server and then re-attach with local scope only
309 unbind_args.handle = bind_args.handle;
310 unbind_args.app_index = server_index;
311 error = vnet_unbind (&unbind_args);
312 SESSION_TEST ((error == 0), "unbind should work");
314 s = session_lookup_listener (server_st_index, &server_sep);
315 SESSION_TEST ((s == 0), "listener should not exist in global table");
317 session_lookup_local_session_endpoint (server_local_st_index,
319 SESSION_TEST ((s == 0), "listener should not exist in local table");
321 detach_args.app_index = server_index;
322 vnet_application_detach (&detach_args);
324 options[APP_OPTIONS_FLAGS] &= ~APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
325 options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
326 attach_args.api_client_index = dummy_server_api_index;
327 error = vnet_application_attach (&attach_args);
328 SESSION_TEST ((error == 0), "app attachment should work");
329 server_index = attach_args.app_index;
330 server = application_get (server_index);
331 SESSION_TEST ((server->ns_index == app_namespace_index (app_ns)),
332 "app should be in the right ns");
334 bind_args.app_index = server_index;
335 error = vnet_bind (&bind_args);
336 SESSION_TEST ((error == 0), "bind should work");
338 server_st_index = application_session_table (server, FIB_PROTOCOL_IP4);
339 s = session_lookup_listener (server_st_index, &server_sep);
340 SESSION_TEST ((s == 0), "listener should not exist in global table");
341 server_local_st_index = application_local_session_table (server);
343 session_lookup_local_session_endpoint (server_local_st_index,
345 SESSION_TEST ((local_listener != SESSION_INVALID_INDEX),
346 "listener should exist in local table");
348 unbind_args.handle = bind_args.handle;
349 error = vnet_unbind (&unbind_args);
350 SESSION_TEST ((error == 0), "unbind should work");
353 session_lookup_local_session_endpoint (server_local_st_index,
355 SESSION_TEST ((local_listener == SESSION_INVALID_INDEX),
356 "listener should not exist in local table");
359 * Client attach + connect in default ns with local scope
361 options[APP_OPTIONS_FLAGS] &= ~APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
362 options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
363 attach_args.namespace_id = 0;
364 attach_args.api_client_index = dummy_client_api_index;
365 vnet_application_attach (&attach_args);
366 error = vnet_connect (&connect_args);
367 SESSION_TEST ((error != 0), "client connect should return error code");
368 code = clib_error_get_code (error);
369 SESSION_TEST ((code == VNET_API_ERROR_SESSION_CONNECT),
370 "error code should be connect (not in same ns)");
371 detach_args.app_index = client_index;
372 vnet_application_detach (&detach_args);
377 detach_args.app_index = server_index;
378 vnet_application_detach (&detach_args);
381 * Create loopback interface
383 if (vnet_create_loopback_interface (&sw_if_index, intf_mac, 0, 0))
385 clib_warning ("couldn't create loopback. stopping the test!");
388 vnet_sw_interface_set_flags (vnet_get_main (), sw_if_index,
389 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
390 ip4_add_del_interface_address (vlib_get_main (), sw_if_index, &intf_addr,
396 ns_args.sw_if_index = sw_if_index;
397 error = vnet_app_namespace_add_del (&ns_args);
398 SESSION_TEST ((error == 0), "app ns insertion should succeed: %d",
399 clib_error_get_code (error));
402 * Attach server with local and global scope
404 options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
405 options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
406 options[APP_OPTIONS_NAMESPACE_SECRET] = dummy_secret;
407 attach_args.namespace_id = ns_id;
408 attach_args.api_client_index = dummy_server_api_index;
409 error = vnet_application_attach (&attach_args);
410 SESSION_TEST ((error == 0), "server attachment should work");
411 server_index = attach_args.app_index;
413 bind_args.app_index = server_index;
414 error = vnet_bind (&bind_args);
415 server_st_index = application_session_table (server, FIB_PROTOCOL_IP4);
416 s = session_lookup_listener (server_st_index, &server_sep);
417 SESSION_TEST ((s == 0), "zero listener should not exist in global table");
419 s = session_lookup_listener (server_st_index, &intf_sep);
420 SESSION_TEST ((s != 0), "intf listener should exist in global table");
421 SESSION_TEST ((s->app_index == server_index), "app_index should be that of "
423 server_local_st_index = application_local_session_table (server);
425 session_lookup_local_session_endpoint (server_local_st_index,
427 SESSION_TEST ((local_listener != SESSION_INVALID_INDEX),
428 "zero listener should exist in local table");
429 detach_args.app_index = server_index;
430 vnet_application_detach (&detach_args);
436 vnet_delete_loopback_interface (sw_if_index);
440 static clib_error_t *
441 session_test (vlib_main_t * vm,
442 unformat_input_t * input, vlib_cli_command_t * cmd_arg)
446 vnet_session_enable_disable (vm, 1);
448 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
450 if (unformat (input, "namespace"))
452 res = session_test_namespace (vm, input);
459 return clib_error_return (0, "Session unit test failed");
464 VLIB_CLI_COMMAND (tcp_test_command, static) =
466 .path = "test session",
467 .short_help = "internal session unit tests",
468 .function = session_test,
473 * fd.io coding-style-patch-verification: ON
476 * eval: (c-set-style "gnu")