986837cbf2423eb511e28601370c33932f1c52f3
[vpp.git] / src / vnet / session / session_test.c
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   unbind_args.handle = bind_args.handle;
231   error = vnet_unbind (&unbind_args);
232   SESSION_TEST ((error == 0), "unbind should work");
233
234   s = session_lookup_listener (server_st_index, &server_sep);
235   SESSION_TEST ((s == 0), "listener should not exist in global table");
236
237   detach_args.app_index = server_index;
238   vnet_application_detach (&detach_args);
239
240   /*
241    * Attach server with local and global scope
242    */
243   options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
244   options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
245   options[APP_OPTIONS_NAMESPACE_SECRET] = dummy_secret;
246   attach_args.namespace_id = ns_id;
247   attach_args.api_client_index = dummy_server_api_index;
248   error = vnet_application_attach (&attach_args);
249   SESSION_TEST ((error == 0), "server attachment should work");
250   server_index = attach_args.app_index;
251   server = application_get (server_index);
252   SESSION_TEST ((server->ns_index == app_namespace_index (app_ns)),
253                 "server should be in the right ns");
254
255   bind_args.app_index = server_index;
256   error = vnet_bind (&bind_args);
257   SESSION_TEST ((error == 0), "bind should work");
258   server_st_index = application_session_table (server, FIB_PROTOCOL_IP4);
259   s = session_lookup_listener (server_st_index, &server_sep);
260   SESSION_TEST ((s != 0), "listener should exist in global table");
261   SESSION_TEST ((s->app_index == server_index), "app_index should be that of "
262                 "the server");
263   server_local_st_index = application_local_session_table (server);
264   local_listener =
265     session_lookup_local_session_endpoint (server_local_st_index,
266                                            &server_sep);
267   SESSION_TEST ((local_listener != SESSION_INVALID_INDEX),
268                 "listener should exist in local table");
269
270   /*
271    * Try client connect with 1) local scope 2) global scope
272    */
273   options[APP_OPTIONS_FLAGS] &= ~APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
274   attach_args.api_client_index = dummy_client_api_index;
275   error = vnet_application_attach (&attach_args);
276   SESSION_TEST ((error == 0), "client attachment should work");
277   client_index = attach_args.app_index;
278   connect_args.api_context = dummy_api_context;
279   connect_args.app_index = client_index;
280   error = vnet_connect (&connect_args);
281   SESSION_TEST ((error != 0), "client connect should return error code");
282   code = clib_error_get_code (error);
283   SESSION_TEST ((code == VNET_API_ERROR_INVALID_VALUE),
284                 "error code should be invalid value (zero ip)");
285   connect_args.sep.ip.ip4.as_u8[0] = 127;
286   error = vnet_connect (&connect_args);
287   SESSION_TEST ((error != 0), "client connect should return error code");
288   code = clib_error_get_code (error);
289   SESSION_TEST ((code == VNET_API_ERROR_SESSION_REDIRECT),
290                 "error code should be redirect");
291   detach_args.app_index = client_index;
292   vnet_application_detach (&detach_args);
293
294   options[APP_OPTIONS_FLAGS] &= ~APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
295   options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
296   attach_args.api_client_index = dummy_client_api_index;
297   error = vnet_application_attach (&attach_args);
298   SESSION_TEST ((error == 0), "client attachment should work");
299   error = vnet_connect (&connect_args);
300   SESSION_TEST ((error != 0), "client connect should return error code");
301   code = clib_error_get_code (error);
302   SESSION_TEST ((code == VNET_API_ERROR_SESSION_CONNECT),
303                 "error code should be connect (nothing in local scope)");
304   detach_args.app_index = client_index;
305   vnet_application_detach (&detach_args);
306
307   /*
308    * Unbind and detach server and then re-attach with local scope only
309    */
310   unbind_args.handle = bind_args.handle;
311   unbind_args.app_index = server_index;
312   error = vnet_unbind (&unbind_args);
313   SESSION_TEST ((error == 0), "unbind should work");
314
315   s = session_lookup_listener (server_st_index, &server_sep);
316   SESSION_TEST ((s == 0), "listener should not exist in global table");
317   local_listener =
318     session_lookup_local_session_endpoint (server_local_st_index,
319                                            &server_sep);
320   SESSION_TEST ((s == 0), "listener should not exist in local table");
321
322   detach_args.app_index = server_index;
323   vnet_application_detach (&detach_args);
324
325   options[APP_OPTIONS_FLAGS] &= ~APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
326   options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
327   attach_args.api_client_index = dummy_server_api_index;
328   error = vnet_application_attach (&attach_args);
329   SESSION_TEST ((error == 0), "app attachment should work");
330   server_index = attach_args.app_index;
331   server = application_get (server_index);
332   SESSION_TEST ((server->ns_index == app_namespace_index (app_ns)),
333                 "app should be in the right ns");
334
335   bind_args.app_index = server_index;
336   error = vnet_bind (&bind_args);
337   SESSION_TEST ((error == 0), "bind should work");
338
339   server_st_index = application_session_table (server, FIB_PROTOCOL_IP4);
340   s = session_lookup_listener (server_st_index, &server_sep);
341   SESSION_TEST ((s == 0), "listener should not exist in global table");
342   server_local_st_index = application_local_session_table (server);
343   local_listener =
344     session_lookup_local_session_endpoint (server_local_st_index,
345                                            &server_sep);
346   SESSION_TEST ((local_listener != SESSION_INVALID_INDEX),
347                 "listener should exist in local table");
348
349   unbind_args.handle = bind_args.handle;
350   error = vnet_unbind (&unbind_args);
351   SESSION_TEST ((error == 0), "unbind should work");
352
353   local_listener =
354     session_lookup_local_session_endpoint (server_local_st_index,
355                                            &server_sep);
356   SESSION_TEST ((local_listener == SESSION_INVALID_INDEX),
357                 "listener should not exist in local table");
358
359   /*
360    * Client attach + connect in default ns with local scope
361    */
362   options[APP_OPTIONS_FLAGS] &= ~APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
363   options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
364   attach_args.namespace_id = 0;
365   attach_args.api_client_index = dummy_client_api_index;
366   vnet_application_attach (&attach_args);
367   error = vnet_connect (&connect_args);
368   SESSION_TEST ((error != 0), "client connect should return error code");
369   code = clib_error_get_code (error);
370   SESSION_TEST ((code == VNET_API_ERROR_SESSION_CONNECT),
371                 "error code should be connect (not in same ns)");
372   detach_args.app_index = client_index;
373   vnet_application_detach (&detach_args);
374
375   /*
376    * Detach server
377    */
378   detach_args.app_index = server_index;
379   vnet_application_detach (&detach_args);
380
381   /*
382    * Create loopback interface
383    */
384   if (vnet_create_loopback_interface (&sw_if_index, intf_mac, 0, 0))
385     {
386       clib_warning ("couldn't create loopback. stopping the test!");
387       return 0;
388     }
389   vnet_sw_interface_set_flags (vnet_get_main (), sw_if_index,
390                                VNET_SW_INTERFACE_FLAG_ADMIN_UP);
391   ip4_add_del_interface_address (vlib_get_main (), sw_if_index, &intf_addr,
392                                  24, 0);
393
394   /*
395    * Update namespace
396    */
397   ns_args.sw_if_index = sw_if_index;
398   error = vnet_app_namespace_add_del (&ns_args);
399   SESSION_TEST ((error == 0), "app ns insertion should succeed: %d",
400                 clib_error_get_code (error));
401
402   /*
403    * Attach server with local and global scope
404    */
405   options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
406   options[APP_OPTIONS_FLAGS] |= APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE;
407   options[APP_OPTIONS_NAMESPACE_SECRET] = dummy_secret;
408   attach_args.namespace_id = ns_id;
409   attach_args.api_client_index = dummy_server_api_index;
410   error = vnet_application_attach (&attach_args);
411   SESSION_TEST ((error == 0), "server attachment should work");
412   server_index = attach_args.app_index;
413
414   bind_args.app_index = server_index;
415   error = vnet_bind (&bind_args);
416   server_st_index = application_session_table (server, FIB_PROTOCOL_IP4);
417   s = session_lookup_listener (server_st_index, &server_sep);
418   SESSION_TEST ((s == 0), "zero listener should not exist in global table");
419
420   s = session_lookup_listener (server_st_index, &intf_sep);
421   SESSION_TEST ((s != 0), "intf listener should exist in global table");
422   SESSION_TEST ((s->app_index == server_index), "app_index should be that of "
423                 "the server");
424   server_local_st_index = application_local_session_table (server);
425   local_listener =
426     session_lookup_local_session_endpoint (server_local_st_index,
427                                            &server_sep);
428   SESSION_TEST ((local_listener != SESSION_INVALID_INDEX),
429                 "zero listener should exist in local table");
430   detach_args.app_index = server_index;
431   vnet_application_detach (&detach_args);
432
433   /*
434    * Cleanup
435    */
436   vec_free (ns_id);
437   vnet_delete_loopback_interface (sw_if_index);
438   return 0;
439 }
440
441 static clib_error_t *
442 session_test (vlib_main_t * vm,
443               unformat_input_t * input, vlib_cli_command_t * cmd_arg)
444 {
445   int res = 0;
446
447   vnet_session_enable_disable (vm, 1);
448
449   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
450     {
451       if (unformat (input, "namespace"))
452         {
453           res = session_test_namespace (vm, input);
454         }
455       else
456         break;
457     }
458
459   if (res)
460     return clib_error_return (0, "Session unit test failed");
461   return 0;
462 }
463
464 /* *INDENT-OFF* */
465 VLIB_CLI_COMMAND (tcp_test_command, static) =
466 {
467   .path = "test session",
468   .short_help = "internal session unit tests",
469   .function = session_test,
470 };
471 /* *INDENT-ON* */
472
473 /*
474  * fd.io coding-style-patch-verification: ON
475  *
476  * Local Variables:
477  * eval: (c-set-style "gnu")
478  * End:
479  */