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/session_table.h>
18 #include <vnet/session/session.h>
19 #include <vnet/fib/fib_table.h>
22 * Hash table of application namespaces by app ns ids
24 uword *app_namespace_lookup_table;
27 * Pool of application namespaces
29 static app_namespace_t *app_namespace_pool;
32 app_namespace_get (u32 index)
34 return pool_elt_at_index (app_namespace_pool, index);
38 app_namespace_get_from_id (const u8 * ns_id)
40 u32 index = app_namespace_index_from_id (ns_id);
41 if (index == APP_NAMESPACE_INVALID_INDEX)
43 return app_namespace_get (index);
47 app_namespace_index (app_namespace_t * app_ns)
49 return (app_ns - app_namespace_pool);
53 app_namespace_alloc (u8 * ns_id)
55 app_namespace_t *app_ns;
56 pool_get (app_namespace_pool, app_ns);
57 clib_memset (app_ns, 0, sizeof (*app_ns));
58 app_ns->ns_id = vec_dup (ns_id);
59 hash_set_mem (app_namespace_lookup_table, app_ns->ns_id,
60 app_ns - app_namespace_pool);
65 vnet_app_namespace_add_del (vnet_app_namespace_add_del_args_t * a)
67 app_namespace_t *app_ns;
72 if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX
73 && !vnet_get_sw_interface_or_null (vnet_get_main (),
75 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
78 if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX)
81 fib_table_get_table_id_for_sw_if_index (FIB_PROTOCOL_IP4,
84 fib_table_get_table_id_for_sw_if_index (FIB_PROTOCOL_IP6,
87 if (a->sw_if_index == APP_NAMESPACE_INVALID_INDEX
88 && a->ip4_fib_id == APP_NAMESPACE_INVALID_INDEX)
89 return VNET_API_ERROR_INVALID_VALUE;
91 app_ns = app_namespace_get_from_id (a->ns_id);
94 app_ns = app_namespace_alloc (a->ns_id);
95 st = session_table_alloc ();
96 session_table_init (st, FIB_PROTOCOL_MAX);
98 st->appns_index = app_namespace_index (app_ns);
99 app_ns->local_table_index = session_table_index (st);
101 app_ns->ns_secret = a->secret;
102 app_ns->sw_if_index = a->sw_if_index;
103 app_ns->ip4_fib_index =
104 fib_table_find (FIB_PROTOCOL_IP4, a->ip4_fib_id);
105 app_ns->ip6_fib_index =
106 fib_table_find (FIB_PROTOCOL_IP6, a->ip6_fib_id);
107 session_lookup_set_tables_appns (app_ns);
111 return VNET_API_ERROR_UNIMPLEMENTED;
117 app_namespace_id (app_namespace_t * app_ns)
119 return app_ns->ns_id;
123 app_namespace_index_from_id (const u8 * ns_id)
126 indexp = hash_get_mem (app_namespace_lookup_table, ns_id);
128 return APP_NAMESPACE_INVALID_INDEX;
133 app_namespace_id_from_index (u32 index)
135 app_namespace_t *app_ns;
137 app_ns = app_namespace_get (index);
138 return app_namespace_id (app_ns);
142 app_namespace_get_fib_index (app_namespace_t * app_ns, u8 fib_proto)
144 return fib_proto == FIB_PROTOCOL_IP4 ?
145 app_ns->ip4_fib_index : app_ns->ip6_fib_index;
149 app_namespace_get_local_table (app_namespace_t * app_ns)
151 return session_table_get (app_ns->local_table_index);
155 app_namespaces_init (void)
157 u8 *ns_id = format (0, "default");
159 if (!app_namespace_lookup_table)
160 app_namespace_lookup_table =
161 hash_create_vec (0, sizeof (u8), sizeof (uword));
164 * Allocate default namespace
166 vnet_app_namespace_add_del_args_t a = {
169 .sw_if_index = APP_NAMESPACE_INVALID_INDEX,
172 vnet_app_namespace_add_del (&a);
176 static clib_error_t *
177 app_ns_fn (vlib_main_t * vm, unformat_input_t * input,
178 vlib_cli_command_t * cmd)
180 unformat_input_t _line_input, *line_input = &_line_input;
181 u8 is_add = 0, *ns_id = 0, secret_set = 0, sw_if_index_set = 0;
182 u32 sw_if_index, fib_id = APP_NAMESPACE_INVALID_INDEX;
184 clib_error_t *error = 0;
187 session_cli_return_if_not_enabled ();
189 if (!unformat_user (input, unformat_line_input, line_input))
192 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
194 if (unformat (line_input, "add"))
196 else if (unformat (line_input, "id %_%v%_", &ns_id))
198 else if (unformat (line_input, "secret %lu", &secret))
200 else if (unformat (line_input, "sw_if_index %u", &sw_if_index))
202 else if (unformat (line_input, "fib_id", &fib_id))
206 error = clib_error_return (0, "unknown input `%U'",
207 format_unformat_error, line_input);
208 unformat_free (line_input);
212 unformat_free (line_input);
214 if (!ns_id || !secret_set || !sw_if_index_set)
216 vlib_cli_output (vm, "namespace-id, secret and sw_if_index must be "
223 vnet_app_namespace_add_del_args_t args = {
226 .sw_if_index = sw_if_index,
227 .ip4_fib_id = fib_id,
230 if ((rv = vnet_app_namespace_add_del (&args)))
231 return clib_error_return (0, "app namespace add del returned %d", rv);
238 VLIB_CLI_COMMAND (app_ns_command, static) =
241 .short_help = "app ns [add] id <namespace-id> secret <secret> "
242 "sw_if_index <sw_if_index>",
243 .function = app_ns_fn,
248 format_app_namespace (u8 * s, va_list * args)
250 app_namespace_t *app_ns = va_arg (*args, app_namespace_t *);
251 s = format (s, "%-10u%-20lu%-20u%-50v", app_namespace_index (app_ns),
252 app_ns->ns_secret, app_ns->sw_if_index, app_ns->ns_id);
256 static clib_error_t *
257 show_app_ns_fn (vlib_main_t * vm, unformat_input_t * main_input,
258 vlib_cli_command_t * cmd)
260 unformat_input_t _line_input, *line_input = &_line_input;
261 app_namespace_t *app_ns;
263 u8 *ns_id, do_table = 0, had_input = 1;
265 session_cli_return_if_not_enabled ();
267 if (!unformat_user (main_input, unformat_line_input, line_input))
273 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
275 if (unformat (line_input, "table %_%v%_", &ns_id))
279 vlib_cli_output (vm, "unknown input [%U]", format_unformat_error,
287 app_ns = app_namespace_get_from_id (ns_id);
290 vlib_cli_output (vm, "ns %v not found", ns_id);
293 st = session_table_get (app_ns->local_table_index);
296 vlib_cli_output (vm, "table for ns %v could not be found", ns_id);
299 session_lookup_show_table_entries (vm, st, 0, 1);
305 vlib_cli_output (vm, "%-10s%-20s%-20s%-50s", "Index", "Secret",
306 "sw_if_index", "Name");
309 pool_foreach (app_ns, app_namespace_pool, ({
310 vlib_cli_output (vm, "%U", format_app_namespace, app_ns);
316 unformat_free (line_input);
321 VLIB_CLI_COMMAND (show_app_ns_command, static) =
323 .path = "show app ns",
324 .short_help = "show app ns",
325 .function = show_app_ns_fn,
330 * fd.io coding-style-patch-verification: ON
333 * eval: (c-set-style "gnu")