Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1 | /* |
| 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: |
| 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 | |
| 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> |
| 20 | |
| 21 | #define SESSION_TEST_I(_cond, _comment, _args...) \ |
| 22 | ({ \ |
| 23 | int _evald = (_cond); \ |
| 24 | if (!(_evald)) { \ |
| 25 | fformat(stderr, "FAIL:%d: " _comment "\n", \ |
| 26 | __LINE__, ##_args); \ |
| 27 | } else { \ |
| 28 | fformat(stderr, "PASS:%d: " _comment "\n", \ |
| 29 | __LINE__, ##_args); \ |
| 30 | } \ |
| 31 | _evald; \ |
| 32 | }) |
| 33 | |
| 34 | #define SESSION_TEST(_cond, _comment, _args...) \ |
| 35 | { \ |
| 36 | if (!SESSION_TEST_I(_cond, _comment, ##_args)) { \ |
| 37 | return 1; \ |
| 38 | } \ |
| 39 | } |
| 40 | |
| 41 | void |
| 42 | dummy_session_reset_callback (stream_session_t * s) |
| 43 | { |
| 44 | clib_warning ("called..."); |
| 45 | } |
| 46 | |
| 47 | int |
| 48 | dummy_session_connected_callback (u32 app_index, u32 api_context, |
| 49 | stream_session_t * s, u8 is_fail) |
| 50 | { |
| 51 | clib_warning ("called..."); |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | int |
| 56 | dummy_add_segment_callback (u32 client_index, const u8 * seg_name, |
| 57 | u32 seg_size) |
| 58 | { |
| 59 | clib_warning ("called..."); |
| 60 | return -1; |
| 61 | } |
| 62 | |
| 63 | int |
| 64 | dummy_redirect_connect_callback (u32 client_index, void *mp) |
| 65 | { |
| 66 | return VNET_API_ERROR_SESSION_REDIRECT; |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | dummy_session_disconnect_callback (stream_session_t * s) |
| 71 | { |
| 72 | clib_warning ("called..."); |
| 73 | } |
| 74 | |
| 75 | int |
| 76 | dummy_session_accept_callback (stream_session_t * s) |
| 77 | { |
| 78 | clib_warning ("called..."); |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | int |
| 83 | dummy_server_rx_callback (stream_session_t * s) |
| 84 | { |
| 85 | clib_warning ("called..."); |
| 86 | return -1; |
| 87 | } |
| 88 | |
| 89 | /* *INDENT-OFF* */ |
| 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, |
| 97 | }; |
| 98 | /* *INDENT-ON* */ |
| 99 | |
| 100 | static int |
| 101 | session_test_namespace (vlib_main_t * vm, unformat_input_t * input) |
| 102 | { |
| 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; |
| 116 | stream_session_t *s; |
| 117 | int code; |
| 118 | |
| 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)); |
| 125 | |
| 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, |
| 130 | .options = options, |
| 131 | .namespace_id = 0, |
| 132 | .session_cb_vft = &dummy_session_cbs, |
| 133 | .segment_name = segment_name, |
| 134 | }; |
| 135 | |
| 136 | vnet_bind_args_t bind_args = { |
| 137 | .sep = server_sep, |
| 138 | .app_index = 0, |
| 139 | }; |
| 140 | |
| 141 | vnet_connect_args_t connect_args = { |
| 142 | .sep = client_sep, |
| 143 | .app_index = 0, |
| 144 | .api_context = 0, |
| 145 | }; |
| 146 | |
| 147 | vnet_unbind_args_t unbind_args = { |
| 148 | .handle = bind_args.handle, |
| 149 | .app_index = 0, |
| 150 | }; |
| 151 | |
| 152 | vnet_app_detach_args_t detach_args = { |
| 153 | .app_index = 0, |
| 154 | }; |
| 155 | |
| 156 | ip4_address_t intf_addr = { |
| 157 | .as_u32 = clib_host_to_net_u32 (0x06000105), |
| 158 | }; |
| 159 | |
| 160 | intf_sep.ip.ip4 = intf_addr; |
| 161 | intf_sep.is_ip4 = 1; |
| 162 | intf_sep.port = dummy_port; |
| 163 | |
| 164 | /* |
| 165 | * Insert namespace and lookup |
| 166 | */ |
| 167 | |
| 168 | vnet_app_namespace_add_del_args_t ns_args = { |
| 169 | .ns_id = ns_id, |
| 170 | .secret = dummy_secret, |
| 171 | .sw_if_index = APP_NAMESPACE_INVALID_INDEX, |
| 172 | .is_add = 1 |
| 173 | }; |
| 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)); |
| 177 | |
| 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", |
| 181 | dummy_secret); |
| 182 | SESSION_TEST ((app_ns->sw_if_index == APP_NAMESPACE_INVALID_INDEX), |
| 183 | "sw_if_index should be invalid"); |
| 184 | |
| 185 | /* |
| 186 | * Try application attach with wrong secret |
| 187 | */ |
| 188 | |
| 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; |
| 194 | |
| 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); |
| 200 | |
| 201 | /* |
| 202 | * Attach server with global default scope |
| 203 | */ |
| 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"); |
| 215 | |
| 216 | bind_args.app_index = server_index; |
| 217 | error = vnet_bind (&bind_args); |
| 218 | SESSION_TEST ((error == 0), "server bind should work"); |
| 219 | |
| 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 " |
| 224 | "the server"); |
| 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"); |
| 228 | |
| 229 | unbind_args.app_index = server_index; |
| 230 | error = vnet_unbind (&unbind_args); |
| 231 | SESSION_TEST ((error == 0), "unbind should work"); |
| 232 | |
| 233 | s = session_lookup_listener (server_st_index, &server_sep); |
| 234 | SESSION_TEST ((s == 0), "listener should not exist in global table"); |
| 235 | |
| 236 | detach_args.app_index = server_index; |
| 237 | vnet_application_detach (&detach_args); |
| 238 | |
| 239 | /* |
| 240 | * Attach server with local and global scope |
| 241 | */ |
| 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"); |
| 253 | |
| 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 " |
| 261 | "the server"); |
| 262 | server_local_st_index = application_local_session_table (server); |
| 263 | local_listener = session_lookup_session_endpoint (server_local_st_index, |
| 264 | &server_sep); |
| 265 | SESSION_TEST ((local_listener != SESSION_INVALID_INDEX), |
| 266 | "listener should exist in local table"); |
| 267 | |
| 268 | /* |
| 269 | * Try client connect with 1) local scope 2) global scope |
| 270 | */ |
| 271 | options[APP_OPTIONS_FLAGS] &= ~APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; |
| 272 | attach_args.api_client_index = dummy_client_api_index; |
| 273 | error = vnet_application_attach (&attach_args); |
| 274 | SESSION_TEST ((error == 0), "client attachment should work"); |
| 275 | client_index = attach_args.app_index; |
| 276 | connect_args.api_context = dummy_api_context; |
| 277 | connect_args.app_index = client_index; |
| 278 | error = vnet_connect (&connect_args); |
| 279 | SESSION_TEST ((error != 0), "client connect should return error code"); |
| 280 | code = clib_error_get_code (error); |
| 281 | SESSION_TEST ((code == VNET_API_ERROR_INVALID_VALUE), |
| 282 | "error code should be invalid value (zero ip)"); |
| 283 | connect_args.sep.ip.ip4.as_u8[0] = 127; |
| 284 | error = vnet_connect (&connect_args); |
| 285 | SESSION_TEST ((error != 0), "client connect should return error code"); |
| 286 | code = clib_error_get_code (error); |
| 287 | SESSION_TEST ((code == VNET_API_ERROR_SESSION_REDIRECT), |
| 288 | "error code should be redirect"); |
| 289 | detach_args.app_index = client_index; |
| 290 | vnet_application_detach (&detach_args); |
| 291 | |
| 292 | options[APP_OPTIONS_FLAGS] &= ~APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE; |
| 293 | options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; |
| 294 | attach_args.api_client_index = dummy_client_api_index; |
| 295 | error = vnet_application_attach (&attach_args); |
| 296 | SESSION_TEST ((error == 0), "client attachment should work"); |
| 297 | error = vnet_connect (&connect_args); |
| 298 | SESSION_TEST ((error != 0), "client connect should return error code"); |
| 299 | code = clib_error_get_code (error); |
| 300 | SESSION_TEST ((code == VNET_API_ERROR_SESSION_CONNECT), |
| 301 | "error code should be connect (nothing in local scope)"); |
| 302 | detach_args.app_index = client_index; |
| 303 | vnet_application_detach (&detach_args); |
| 304 | |
| 305 | /* |
| 306 | * Unbind and detach server and then re-attach with local scope only |
| 307 | */ |
| 308 | unbind_args.handle = bind_args.handle; |
| 309 | unbind_args.app_index = server_index; |
| 310 | error = vnet_unbind (&unbind_args); |
| 311 | SESSION_TEST ((error == 0), "unbind should work"); |
| 312 | |
| 313 | s = session_lookup_listener (server_st_index, &server_sep); |
| 314 | SESSION_TEST ((s == 0), "listener should not exist in global table"); |
| 315 | local_listener = session_lookup_session_endpoint (server_local_st_index, |
| 316 | &server_sep); |
| 317 | SESSION_TEST ((s == 0), "listener should not exist in local table"); |
| 318 | |
| 319 | detach_args.app_index = server_index; |
| 320 | vnet_application_detach (&detach_args); |
| 321 | |
| 322 | options[APP_OPTIONS_FLAGS] &= ~APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; |
| 323 | options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE; |
| 324 | attach_args.api_client_index = dummy_server_api_index; |
| 325 | error = vnet_application_attach (&attach_args); |
| 326 | SESSION_TEST ((error == 0), "app attachment should work"); |
| 327 | server_index = attach_args.app_index; |
| 328 | server = application_get (server_index); |
| 329 | SESSION_TEST ((server->ns_index == app_namespace_index (app_ns)), |
| 330 | "app should be in the right ns"); |
| 331 | |
| 332 | bind_args.app_index = server_index; |
| 333 | error = vnet_bind (&bind_args); |
| 334 | SESSION_TEST ((error == 0), "bind should work"); |
| 335 | |
| 336 | server_st_index = application_session_table (server, FIB_PROTOCOL_IP4); |
| 337 | s = session_lookup_listener (server_st_index, &server_sep); |
| 338 | SESSION_TEST ((s == 0), "listener should not exist in global table"); |
| 339 | server_local_st_index = application_local_session_table (server); |
| 340 | local_listener = session_lookup_session_endpoint (server_local_st_index, |
| 341 | &server_sep); |
| 342 | SESSION_TEST ((local_listener != SESSION_INVALID_INDEX), |
| 343 | "listener should exist in local table"); |
| 344 | |
| 345 | unbind_args.handle = bind_args.handle; |
| 346 | error = vnet_unbind (&unbind_args); |
| 347 | SESSION_TEST ((error == 0), "unbind should work"); |
| 348 | |
| 349 | local_listener = session_lookup_session_endpoint (server_local_st_index, |
| 350 | &server_sep); |
| 351 | SESSION_TEST ((local_listener == SESSION_INVALID_INDEX), |
| 352 | "listener should not exist in local table"); |
| 353 | |
| 354 | /* |
| 355 | * Client attach + connect in default ns with local scope |
| 356 | */ |
| 357 | options[APP_OPTIONS_FLAGS] &= ~APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; |
| 358 | options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE; |
| 359 | attach_args.namespace_id = 0; |
| 360 | attach_args.api_client_index = dummy_client_api_index; |
| 361 | vnet_application_attach (&attach_args); |
| 362 | error = vnet_connect (&connect_args); |
| 363 | SESSION_TEST ((error != 0), "client connect should return error code"); |
| 364 | code = clib_error_get_code (error); |
| 365 | SESSION_TEST ((code == VNET_API_ERROR_SESSION_CONNECT), |
| 366 | "error code should be connect (not in same ns)"); |
| 367 | detach_args.app_index = client_index; |
| 368 | vnet_application_detach (&detach_args); |
| 369 | |
| 370 | /* |
| 371 | * Detach server |
| 372 | */ |
| 373 | detach_args.app_index = server_index; |
| 374 | vnet_application_detach (&detach_args); |
| 375 | |
| 376 | /* |
| 377 | * Create loopback interface |
| 378 | */ |
| 379 | if (vnet_create_loopback_interface (&sw_if_index, intf_mac, 0, 0)) |
| 380 | { |
| 381 | clib_warning ("couldn't create loopback. stopping the test!"); |
| 382 | return 0; |
| 383 | } |
| 384 | vnet_sw_interface_set_flags (vnet_get_main (), sw_if_index, |
| 385 | VNET_SW_INTERFACE_FLAG_ADMIN_UP); |
| 386 | ip4_add_del_interface_address (vlib_get_main (), sw_if_index, &intf_addr, |
| 387 | 24, 0); |
| 388 | |
| 389 | /* |
| 390 | * Update namespace |
| 391 | */ |
| 392 | ns_args.sw_if_index = sw_if_index; |
| 393 | error = vnet_app_namespace_add_del (&ns_args); |
| 394 | SESSION_TEST ((error == 0), "app ns insertion should succeed: %d", |
| 395 | clib_error_get_code (error)); |
| 396 | |
| 397 | /* |
| 398 | * Attach server with local and global scope |
| 399 | */ |
| 400 | options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE; |
| 401 | options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE; |
| 402 | options[APP_OPTIONS_NAMESPACE_SECRET] = dummy_secret; |
| 403 | attach_args.namespace_id = ns_id; |
| 404 | attach_args.api_client_index = dummy_server_api_index; |
| 405 | error = vnet_application_attach (&attach_args); |
| 406 | SESSION_TEST ((error == 0), "server attachment should work"); |
| 407 | server_index = attach_args.app_index; |
| 408 | |
| 409 | bind_args.app_index = server_index; |
| 410 | error = vnet_bind (&bind_args); |
| 411 | server_st_index = application_session_table (server, FIB_PROTOCOL_IP4); |
| 412 | s = session_lookup_listener (server_st_index, &server_sep); |
| 413 | SESSION_TEST ((s == 0), "zero listener should not exist in global table"); |
| 414 | |
| 415 | s = session_lookup_listener (server_st_index, &intf_sep); |
| 416 | SESSION_TEST ((s != 0), "intf listener should exist in global table"); |
| 417 | SESSION_TEST ((s->app_index == server_index), "app_index should be that of " |
| 418 | "the server"); |
| 419 | server_local_st_index = application_local_session_table (server); |
| 420 | local_listener = session_lookup_session_endpoint (server_local_st_index, |
| 421 | &server_sep); |
| 422 | SESSION_TEST ((local_listener != SESSION_INVALID_INDEX), |
| 423 | "zero listener should exist in local table"); |
| 424 | detach_args.app_index = server_index; |
| 425 | vnet_application_detach (&detach_args); |
| 426 | |
| 427 | /* |
| 428 | * Cleanup |
| 429 | */ |
| 430 | vec_free (ns_id); |
| 431 | vnet_delete_loopback_interface (sw_if_index); |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | static clib_error_t * |
| 436 | session_test (vlib_main_t * vm, |
| 437 | unformat_input_t * input, vlib_cli_command_t * cmd_arg) |
| 438 | { |
| 439 | int res = 0; |
| 440 | |
| 441 | vnet_session_enable_disable (vm, 1); |
| 442 | |
| 443 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 444 | { |
| 445 | if (unformat (input, "namespace")) |
| 446 | { |
| 447 | res = session_test_namespace (vm, input); |
| 448 | } |
| 449 | else |
| 450 | break; |
| 451 | } |
| 452 | |
| 453 | if (res) |
| 454 | return clib_error_return (0, "Session unit test failed"); |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | /* *INDENT-OFF* */ |
| 459 | VLIB_CLI_COMMAND (tcp_test_command, static) = |
| 460 | { |
| 461 | .path = "test session", |
| 462 | .short_help = "internal session unit tests", |
| 463 | .function = session_test, |
| 464 | }; |
| 465 | /* *INDENT-ON* */ |
| 466 | |
| 467 | /* |
| 468 | * fd.io coding-style-patch-verification: ON |
| 469 | * |
| 470 | * Local Variables: |
| 471 | * eval: (c-set-style "gnu") |
| 472 | * End: |
| 473 | */ |