2 * Copyright (c) 2017-2019 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.h>
18 #include <vnet/session/session_table.h>
19 #include <vnet/session/session.h>
20 #include <vnet/fib/fib_table.h>
21 #include <vppinfra/file.h>
22 #include <vppinfra/format_table.h>
23 #include <vlib/unix/unix.h>
26 * Hash table of application namespaces by app ns ids
28 uword *app_namespace_lookup_table;
31 * Pool of application namespaces
33 static app_namespace_t *app_namespace_pool;
35 static u8 app_sapi_enabled;
38 app_namespace_get (u32 index)
40 return pool_elt_at_index (app_namespace_pool, index);
44 app_namespace_get_from_id (const u8 *ns_id)
46 u32 index = app_namespace_index_from_id (ns_id);
47 if (index == APP_NAMESPACE_INVALID_INDEX)
49 return app_namespace_get (index);
53 app_namespace_index (app_namespace_t * app_ns)
55 return (app_ns - app_namespace_pool);
59 app_namespace_free (app_namespace_t *app_ns)
61 hash_unset_mem (app_namespace_lookup_table, app_ns->ns_id);
62 vec_free (app_ns->ns_id);
64 pool_put (app_namespace_pool, app_ns);
68 app_namespace_alloc (const u8 *ns_id)
70 app_namespace_t *app_ns;
72 pool_get (app_namespace_pool, app_ns);
73 clib_memset (app_ns, 0, sizeof (*app_ns));
75 app_ns->ns_id = vec_dup ((u8 *) ns_id);
76 vec_terminate_c_string (app_ns->ns_id);
78 hash_set_mem (app_namespace_lookup_table, app_ns->ns_id,
79 app_ns - app_namespace_pool);
85 vnet_app_namespace_add_del (vnet_app_namespace_add_del_args_t * a)
87 app_namespace_t *app_ns;
94 if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX
95 && !vnet_get_sw_interface_or_null (vnet_get_main (),
97 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
100 if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX)
103 fib_table_get_table_id_for_sw_if_index (FIB_PROTOCOL_IP4,
106 fib_table_get_table_id_for_sw_if_index (FIB_PROTOCOL_IP6,
109 if (a->sw_if_index == APP_NAMESPACE_INVALID_INDEX
110 && a->ip4_fib_id == APP_NAMESPACE_INVALID_INDEX)
111 return VNET_API_ERROR_INVALID_VALUE;
113 app_ns = app_namespace_get_from_id (a->ns_id);
116 app_ns = app_namespace_alloc (a->ns_id);
117 st = session_table_alloc ();
118 session_table_init (st, FIB_PROTOCOL_MAX);
120 st->appns_index = app_namespace_index (app_ns);
121 app_ns->local_table_index = session_table_index (st);
124 app_ns->netns = vec_dup (a->netns);
125 vec_terminate_c_string (app_ns->netns);
129 app_ns->sock_name = vec_dup (a->sock_name);
130 vec_terminate_c_string (app_ns->sock_name);
133 /* Add socket for namespace,
134 * only at creation time */
135 if (app_sapi_enabled)
137 rv = appns_sapi_add_ns_socket (app_ns);
143 app_ns->ns_secret = a->secret;
144 app_ns->sw_if_index = a->sw_if_index;
145 app_ns->ip4_fib_index =
146 fib_table_find (FIB_PROTOCOL_IP4, a->ip4_fib_id);
147 app_ns->ip6_fib_index =
148 fib_table_find (FIB_PROTOCOL_IP6, a->ip6_fib_id);
149 session_lookup_set_tables_appns (app_ns);
154 ns_index = app_namespace_index_from_id (a->ns_id);
155 if (ns_index == APP_NAMESPACE_INVALID_INDEX)
156 return VNET_API_ERROR_INVALID_VALUE;
158 app_ns = app_namespace_get (ns_index);
160 return VNET_API_ERROR_INVALID_VALUE;
162 application_namespace_cleanup (app_ns);
164 if (app_sapi_enabled)
165 appns_sapi_del_ns_socket (app_ns);
167 st = session_table_get (app_ns->local_table_index);
169 session_table_free (st, FIB_PROTOCOL_MAX);
171 vec_free (app_ns->netns);
172 if (app_ns->sock_name)
173 vec_free (app_ns->sock_name);
175 app_namespace_free (app_ns);
182 app_namespace_id (app_namespace_t * app_ns)
184 return app_ns->ns_id;
188 app_namespace_index_from_id (const u8 * ns_id)
193 key = vec_dup ((u8 *) ns_id);
194 vec_terminate_c_string (key);
196 indexp = hash_get_mem (app_namespace_lookup_table, key);
199 return APP_NAMESPACE_INVALID_INDEX;
204 app_namespace_id_from_index (u32 index)
206 app_namespace_t *app_ns;
208 app_ns = app_namespace_get (index);
209 return app_namespace_id (app_ns);
213 app_namespace_get_fib_index (app_namespace_t * app_ns, u8 fib_proto)
215 return fib_proto == FIB_PROTOCOL_IP4 ?
216 app_ns->ip4_fib_index : app_ns->ip6_fib_index;
220 app_namespace_get_local_table (app_namespace_t * app_ns)
222 return session_table_get (app_ns->local_table_index);
226 appns_sapi_enable_disable (int is_enable)
228 /* This cannot be called with active sockets */
229 if (pool_elts (app_namespace_pool))
232 app_sapi_enabled = is_enable;
237 appns_sapi_enabled (void)
239 return app_sapi_enabled;
243 app_namespaces_init (void)
245 u8 *ns_id = format (0, "default");
247 if (!app_namespace_lookup_table)
248 app_namespace_lookup_table =
249 hash_create_vec (0, sizeof (u8), sizeof (uword));
252 * Allocate default namespace
255 /* clang-format off */
256 vnet_app_namespace_add_del_args_t a = {
261 .sw_if_index = APP_NAMESPACE_INVALID_INDEX,
264 /* clang-format on */
266 vnet_app_namespace_add_del (&a);
270 static clib_error_t *
271 app_ns_fn (vlib_main_t * vm, unformat_input_t * input,
272 vlib_cli_command_t * cmd)
274 u8 is_add = 0, *ns_id = 0, secret_set = 0, sw_if_index_set = 0;
275 u8 *netns = 0, *sock_name = 0;
276 unformat_input_t _line_input, *line_input = &_line_input;
277 u32 sw_if_index, fib_id = APP_NAMESPACE_INVALID_INDEX;
278 vnet_main_t *vnm = vnet_get_main ();
280 clib_error_t *error = 0;
283 session_cli_return_if_not_enabled ();
285 if (!unformat_user (input, unformat_line_input, line_input))
288 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
290 if (unformat (line_input, "add"))
292 else if (unformat (line_input, "del"))
294 else if (unformat (line_input, "id %_%v%_", &ns_id))
296 else if (unformat (line_input, "secret %lu", &secret))
298 else if (unformat (line_input, "sw_if_index %u", &sw_if_index))
300 else if (unformat (line_input, "if %U", unformat_vnet_sw_interface, vnm,
303 else if (unformat (line_input, "fib_id", &fib_id))
305 else if (unformat (line_input, "netns %_%v%_", &netns))
307 else if (unformat (line_input, "sock-name %_%v%_", &sock_name))
311 error = clib_error_return (0, "unknown input `%U'",
312 format_unformat_error, line_input);
319 vlib_cli_output (vm, "namespace-id must be provided");
323 if (is_add && (!secret_set || !sw_if_index_set))
325 vlib_cli_output (vm, "secret and interface must be provided");
329 /* clang-format off */
330 vnet_app_namespace_add_del_args_t args = {
334 .sw_if_index = sw_if_index,
335 .sock_name = sock_name,
336 .ip4_fib_id = fib_id,
339 /* clang-format on */
341 if ((rv = vnet_app_namespace_add_del (&args)))
342 error = clib_error_return (0, "app namespace add del returned %d", rv);
348 vec_free (sock_name);
349 unformat_free (line_input);
355 VLIB_CLI_COMMAND (app_ns_command, static) = {
357 .short_help = "app ns [add|del] id <namespace-id> secret <secret> "
358 "sw_if_index <sw_if_index> if <interface> [netns <ns>]",
359 .function = app_ns_fn,
364 format_app_namespace (u8 * s, va_list * args)
366 app_namespace_t *app_ns = va_arg (*args, app_namespace_t *);
367 vnet_main_t *vnm = vnet_get_main ();
369 s = format (s, "Application namespace [%u]\nid: %s\nsecret: %lu",
370 app_namespace_index (app_ns), app_ns->ns_id, app_ns->ns_secret);
371 if (app_ns->sw_if_index != (u32) ~0)
372 s = format (s, "\nInterface: %U", format_vnet_sw_if_index_name, vnm,
373 app_ns->sw_if_index);
375 s = format (s, "\nNetns: %s", app_ns->netns);
376 if (app_ns->sock_name)
377 s = format (s, "\nSocket: %s", app_ns->sock_name);
383 app_namespace_show_api (vlib_main_t * vm, app_namespace_t * app_ns)
385 app_ns_api_handle_t *handle;
386 app_worker_t *app_wrk;
390 if (!app_sapi_enabled)
392 vlib_cli_output (vm, "app socket api not enabled!");
396 vlib_cli_output (vm, "socket: %v\n", app_ns->sock_name);
398 if (!pool_elts (app_ns->app_sockets))
401 vlib_cli_output (vm, "%12s%12s%5s", "app index", "wrk index", "fd");
405 pool_foreach (cs, app_ns->app_sockets) {
406 handle = (app_ns_api_handle_t *) &cs->private_data;
407 cf = clib_file_get (&file_main, handle->aah_file_index);
408 if (handle->aah_app_wrk_index == APP_INVALID_INDEX)
410 vlib_cli_output (vm, "%12d%12d%5u", -1, -1, cf->file_descriptor);
413 app_wrk = app_worker_get (handle->aah_app_wrk_index);
414 vlib_cli_output (vm, "%12d%12d%5u", app_wrk->app_index,
415 app_wrk->wrk_map_index, cf->file_descriptor);
420 static clib_error_t *
421 show_app_ns_fn (vlib_main_t * vm, unformat_input_t * main_input,
422 vlib_cli_command_t * cmd)
424 unformat_input_t _line_input, *line_input = &_line_input;
425 u8 *ns_id = 0, do_table = 0, had_input = 1, do_api = 0;
426 app_namespace_t *app_ns;
427 vnet_main_t *vnm = vnet_get_main ();
429 table_t table = {}, *t = &table;
431 session_cli_return_if_not_enabled ();
433 if (!unformat_user (main_input, unformat_line_input, line_input))
439 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
441 if (unformat (line_input, "id %_%v%_", &ns_id))
443 else if (unformat (line_input, "api-clients"))
447 vlib_cli_output (vm, "unknown input [%U]", format_unformat_error,
457 vlib_cli_output (vm, "must specify a table for api");
460 app_ns = app_namespace_get_from_id (ns_id);
461 app_namespace_show_api (vm, app_ns);
466 app_ns = app_namespace_get_from_id (ns_id);
469 vlib_cli_output (vm, "ns %v not found", ns_id);
472 st = session_table_get (app_ns->local_table_index);
475 vlib_cli_output (vm, "table for ns %v could not be found", ns_id);
478 vlib_cli_output (vm, "%U", format_app_namespace, app_ns);
479 session_lookup_show_table_entries (vm, st, 0, 1);
485 table_add_header_col (t, 6, "Index", "Secret", "Interface", "Id", "Netns",
488 pool_foreach (app_ns, app_namespace_pool)
491 table_format_cell (t, i, j++, "%u", app_namespace_index (app_ns));
492 table_format_cell (t, i, j++, "%lu", app_ns->ns_secret);
493 table_format_cell (t, i, j++, "%U", format_vnet_sw_if_index_name, vnm,
494 app_ns->sw_if_index);
495 table_format_cell (t, i, j++, "%s", app_ns->ns_id);
496 table_format_cell (t, i, j++, "%s", app_ns->netns);
497 table_format_cell (t, i++, j++, "%s", app_ns->sock_name);
500 t->default_body.align = TTAA_LEFT;
501 t->default_header_col.align = TTAA_LEFT;
502 t->default_header_col.fg_color = TTAC_YELLOW;
503 t->default_header_col.flags = TTAF_FG_COLOR_SET;
504 vlib_cli_output (vm, "%U", format_table, t);
509 unformat_free (line_input);
514 VLIB_CLI_COMMAND (show_app_ns_command, static) = {
515 .path = "show app ns",
516 .short_help = "show app ns [id <id> [api-clients]]",
517 .function = show_app_ns_fn,
522 * fd.io coding-style-patch-verification: ON
525 * eval: (c-set-style "gnu")