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 <vlib/unix/unix.h>
25 * Hash table of application namespaces by app ns ids
27 uword *app_namespace_lookup_table;
30 * Pool of application namespaces
32 static app_namespace_t *app_namespace_pool;
34 static u8 app_sapi_enabled;
37 app_namespace_get (u32 index)
39 return pool_elt_at_index (app_namespace_pool, index);
43 app_namespace_get_from_id (const u8 * ns_id)
45 u32 index = app_namespace_index_from_id (ns_id);
46 if (index == APP_NAMESPACE_INVALID_INDEX)
48 return app_namespace_get (index);
52 app_namespace_index (app_namespace_t * app_ns)
54 return (app_ns - app_namespace_pool);
58 app_namespace_alloc (u8 * ns_id)
60 app_namespace_t *app_ns;
61 pool_get (app_namespace_pool, app_ns);
62 clib_memset (app_ns, 0, sizeof (*app_ns));
63 app_ns->ns_id = vec_dup (ns_id);
64 hash_set_mem (app_namespace_lookup_table, app_ns->ns_id,
65 app_ns - app_namespace_pool);
70 vnet_app_namespace_add_del (vnet_app_namespace_add_del_args_t * a)
72 app_namespace_t *app_ns;
77 if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX
78 && !vnet_get_sw_interface_or_null (vnet_get_main (),
80 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
83 if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX)
86 fib_table_get_table_id_for_sw_if_index (FIB_PROTOCOL_IP4,
89 fib_table_get_table_id_for_sw_if_index (FIB_PROTOCOL_IP6,
92 if (a->sw_if_index == APP_NAMESPACE_INVALID_INDEX
93 && a->ip4_fib_id == APP_NAMESPACE_INVALID_INDEX)
94 return VNET_API_ERROR_INVALID_VALUE;
96 app_ns = app_namespace_get_from_id (a->ns_id);
99 app_ns = app_namespace_alloc (a->ns_id);
100 st = session_table_alloc ();
101 session_table_init (st, FIB_PROTOCOL_MAX);
103 st->appns_index = app_namespace_index (app_ns);
104 app_ns->local_table_index = session_table_index (st);
106 app_ns->ns_secret = a->secret;
107 app_ns->netns = a->netns ? vec_dup (a->netns) : 0;
108 app_ns->sw_if_index = a->sw_if_index;
109 app_ns->ip4_fib_index =
110 fib_table_find (FIB_PROTOCOL_IP4, a->ip4_fib_id);
111 app_ns->ip6_fib_index =
112 fib_table_find (FIB_PROTOCOL_IP6, a->ip6_fib_id);
113 session_lookup_set_tables_appns (app_ns);
115 /* Add socket for namespace */
116 if (app_sapi_enabled)
117 appns_sapi_add_ns_socket (app_ns);
121 return VNET_API_ERROR_UNIMPLEMENTED;
127 app_namespace_id (app_namespace_t * app_ns)
129 return app_ns->ns_id;
133 app_namespace_index_from_id (const u8 * ns_id)
136 indexp = hash_get_mem (app_namespace_lookup_table, ns_id);
138 return APP_NAMESPACE_INVALID_INDEX;
143 app_namespace_id_from_index (u32 index)
145 app_namespace_t *app_ns;
147 app_ns = app_namespace_get (index);
148 return app_namespace_id (app_ns);
152 app_namespace_get_fib_index (app_namespace_t * app_ns, u8 fib_proto)
154 return fib_proto == FIB_PROTOCOL_IP4 ?
155 app_ns->ip4_fib_index : app_ns->ip6_fib_index;
159 app_namespace_get_local_table (app_namespace_t * app_ns)
161 return session_table_get (app_ns->local_table_index);
165 appns_sapi_enable (void)
167 app_sapi_enabled = 1;
171 appns_sapi_enabled (void)
173 return app_sapi_enabled;
177 app_namespaces_init (void)
179 u8 *ns_id = format (0, "default");
181 if (!app_namespace_lookup_table)
182 app_namespace_lookup_table =
183 hash_create_vec (0, sizeof (u8), sizeof (uword));
186 * Allocate default namespace
189 /* clang-format off */
190 vnet_app_namespace_add_del_args_t a = {
194 .sw_if_index = APP_NAMESPACE_INVALID_INDEX,
197 /* clang-format on */
199 vnet_app_namespace_add_del (&a);
203 static clib_error_t *
204 app_ns_fn (vlib_main_t * vm, unformat_input_t * input,
205 vlib_cli_command_t * cmd)
207 u8 is_add = 0, *ns_id = 0, secret_set = 0, sw_if_index_set = 0, *netns = 0;
208 unformat_input_t _line_input, *line_input = &_line_input;
209 u32 sw_if_index, fib_id = APP_NAMESPACE_INVALID_INDEX;
211 clib_error_t *error = 0;
214 session_cli_return_if_not_enabled ();
216 if (!unformat_user (input, unformat_line_input, line_input))
219 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
221 if (unformat (line_input, "add"))
223 else if (unformat (line_input, "id %_%v%_", &ns_id))
225 else if (unformat (line_input, "secret %lu", &secret))
227 else if (unformat (line_input, "sw_if_index %u", &sw_if_index))
229 else if (unformat (line_input, "fib_id", &fib_id))
231 else if (unformat (line_input, "netns %_%v%_", &netns))
235 error = clib_error_return (0, "unknown input `%U'",
236 format_unformat_error, line_input);
241 if (!ns_id || !secret_set || !sw_if_index_set)
243 vlib_cli_output (vm, "namespace-id, secret and sw_if_index must be "
250 /* clang-format off */
251 vnet_app_namespace_add_del_args_t args = {
255 .sw_if_index = sw_if_index,
256 .ip4_fib_id = fib_id,
259 /* clang-format on */
261 if ((rv = vnet_app_namespace_add_del (&args)))
262 error = clib_error_return (0, "app namespace add del returned %d", rv);
269 unformat_free (line_input);
275 VLIB_CLI_COMMAND (app_ns_command, static) = {
277 .short_help = "app ns [add] id <namespace-id> secret <secret> "
278 "sw_if_index <sw_if_index> [netns <ns>]",
279 .function = app_ns_fn,
284 format_app_namespace (u8 * s, va_list * args)
286 app_namespace_t *app_ns = va_arg (*args, app_namespace_t *);
289 format (s, "%-10u%-10lu%-15d%-15v%-15v%-40v", app_namespace_index (app_ns),
290 app_ns->ns_secret, app_ns->sw_if_index, app_ns->ns_id,
291 app_ns->netns, app_ns->sock_name);
296 app_namespace_show_api (vlib_main_t * vm, app_namespace_t * app_ns)
298 app_ns_api_handle_t *handle;
299 app_worker_t *app_wrk;
303 if (!app_sapi_enabled)
305 vlib_cli_output (vm, "app socket api not enabled!");
309 vlib_cli_output (vm, "socket: %v\n", app_ns->sock_name);
311 if (!pool_elts (app_ns->app_sockets))
314 vlib_cli_output (vm, "%12s%12s%5s", "app index", "wrk index", "fd");
318 pool_foreach (cs, app_ns->app_sockets) {
319 handle = (app_ns_api_handle_t *) &cs->private_data;
320 cf = clib_file_get (&file_main, handle->aah_file_index);
321 if (handle->aah_app_wrk_index == APP_INVALID_INDEX)
323 vlib_cli_output (vm, "%12d%12d%5u", -1, -1, cf->file_descriptor);
326 app_wrk = app_worker_get (handle->aah_app_wrk_index);
327 vlib_cli_output (vm, "%12d%12d%5u", app_wrk->app_index,
328 app_wrk->wrk_map_index, cf->file_descriptor);
333 static clib_error_t *
334 show_app_ns_fn (vlib_main_t * vm, unformat_input_t * main_input,
335 vlib_cli_command_t * cmd)
337 unformat_input_t _line_input, *line_input = &_line_input;
338 u8 *ns_id, do_table = 0, had_input = 1, do_api = 0;
339 app_namespace_t *app_ns;
342 session_cli_return_if_not_enabled ();
344 if (!unformat_user (main_input, unformat_line_input, line_input))
350 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
352 if (unformat (line_input, "table %_%v%_", &ns_id))
354 else if (unformat (line_input, "api-clients"))
358 vlib_cli_output (vm, "unknown input [%U]", format_unformat_error,
368 vlib_cli_output (vm, "must specify a table for api");
371 app_ns = app_namespace_get_from_id (ns_id);
372 app_namespace_show_api (vm, app_ns);
377 app_ns = app_namespace_get_from_id (ns_id);
380 vlib_cli_output (vm, "ns %v not found", ns_id);
383 st = session_table_get (app_ns->local_table_index);
386 vlib_cli_output (vm, "table for ns %v could not be found", ns_id);
389 session_lookup_show_table_entries (vm, st, 0, 1);
395 vlib_cli_output (vm, "%-10s%-10s%-15s%-15s%-15s%-40s", "Index", "Secret",
396 "sw_if_index", "Id", "netns", "Socket");
399 pool_foreach (app_ns, app_namespace_pool) {
400 vlib_cli_output (vm, "%U", format_app_namespace, app_ns);
406 unformat_free (line_input);
411 VLIB_CLI_COMMAND (show_app_ns_command, static) =
413 .path = "show app ns",
414 .short_help = "show app ns [table <id> [api-clients]]",
415 .function = show_app_ns_fn,
420 * fd.io coding-style-patch-verification: ON
423 * eval: (c-set-style "gnu")