udp: refactor udp code
[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   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 =
264     session_lookup_local_session_endpoint (server_local_st_index,
265                                            &server_sep);
266   SESSION_TEST ((local_listener != SESSION_INVALID_INDEX),
267                 "listener should exist in local table");
268
269   /*
270    * Try client connect with 1) local scope 2) global scope
271    */
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);
292
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);
305
306   /*
307    * Unbind and detach server and then re-attach with local scope only
308    */
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");
313
314   s = session_lookup_listener (server_st_index, &server_sep);
315   SESSION_TEST ((s == 0), "listener should not exist in global table");
316   local_listener =
317     session_lookup_local_session_endpoint (server_local_st_index,
318                                            &server_sep);
319   SESSION_TEST ((s == 0), "listener should not exist in local table");
320
321   detach_args.app_index = server_index;
322   vnet_application_detach (&detach_args);
323
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");
333
334   bind_args.app_index = server_index;
335   error = vnet_bind (&bind_args);
336   SESSION_TEST ((error == 0), "bind should work");
337
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);
342   local_listener =
343     session_lookup_local_session_endpoint (server_local_st_index,
344                                            &server_sep);
345   SESSION_TEST ((local_listener != SESSION_INVALID_INDEX),
346                 "listener should exist in local table");
347
348   unbind_args.handle = bind_args.handle;
349   error = vnet_unbind (&unbind_args);
350   SESSION_TEST ((error == 0), "unbind should work");
351
352   local_listener =
353     session_lookup_local_session_endpoint (server_local_st_index,
354                                            &server_sep);
355   SESSION_TEST ((local_listener == SESSION_INVALID_INDEX),
356                 "listener should not exist in local table");
357
358   /*
359    * Client attach + connect in default ns with local scope
360    */
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);
373
374   /*
375    * Detach server
376    */
377   detach_args.app_index = server_index;
378   vnet_application_detach (&detach_args);
379
380   /*
381    * Create loopback interface
382    */
383   if (vnet_create_loopback_interface (&sw_if_index, intf_mac, 0, 0))
384     {
385       clib_warning ("couldn't create loopback. stopping the test!");
386       return 0;
387     }
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,
391                                  24, 0);
392
393   /*
394    * Update namespace
395    */
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));
400
401   /*
402    * Attach server with local and global scope
403    */
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;
412
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");
418
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 "
422                 "the server");
423   server_local_st_index = application_local_session_table (server);
424   local_listener =
425     session_lookup_local_session_endpoint (server_local_st_index,
426                                            &server_sep);
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);
431
432   /*
433    * Cleanup
434    */
435   vec_free (ns_id);
436   vnet_delete_loopback_interface (sw_if_index);
437   return 0;
438 }
439
440 static clib_error_t *
441 session_test (vlib_main_t * vm,
442               unformat_input_t * input, vlib_cli_command_t * cmd_arg)
443 {
444   int res = 0;
445
446   vnet_session_enable_disable (vm, 1);
447
448   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
449     {
450       if (unformat (input, "namespace"))
451         {
452           res = session_test_namespace (vm, input);
453         }
454       else
455         break;
456     }
457
458   if (res)
459     return clib_error_return (0, "Session unit test failed");
460   return 0;
461 }
462
463 /* *INDENT-OFF* */
464 VLIB_CLI_COMMAND (tcp_test_command, static) =
465 {
466   .path = "test session",
467   .short_help = "internal session unit tests",
468   .function = session_test,
469 };
470 /* *INDENT-ON* */
471
472 /*
473  * fd.io coding-style-patch-verification: ON
474  *
475  * Local Variables:
476  * eval: (c-set-style "gnu")
477  * End:
478  */