blob: 47a369ed765e2e39f1968cecb2ad4c2bec00ed51 [file] [log] [blame]
Florin Corascea194d2017-10-02 00:18:51 -07001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Florin Corascea194d2017-10-02 00:18:51 -07003 * 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/session_table.h>
18#include <vnet/session/session.h>
19#include <vnet/fib/fib_table.h>
20
21/**
22 * Hash table of application namespaces by app ns ids
23 */
24uword *app_namespace_lookup_table;
25
26/**
27 * Pool of application namespaces
28 */
29static app_namespace_t *app_namespace_pool;
30
31app_namespace_t *
32app_namespace_get (u32 index)
33{
34 return pool_elt_at_index (app_namespace_pool, index);
35}
36
37app_namespace_t *
38app_namespace_get_from_id (const u8 * ns_id)
39{
40 u32 index = app_namespace_index_from_id (ns_id);
41 if (index == APP_NAMESPACE_INVALID_INDEX)
42 return 0;
43 return app_namespace_get (index);
44}
45
46u32
47app_namespace_index (app_namespace_t * app_ns)
48{
49 return (app_ns - app_namespace_pool);
50}
51
52app_namespace_t *
53app_namespace_alloc (u8 * ns_id)
54{
55 app_namespace_t *app_ns;
56 pool_get (app_namespace_pool, app_ns);
Dave Barachb7b92992018-10-17 10:38:51 -040057 clib_memset (app_ns, 0, sizeof (*app_ns));
Florin Corascea194d2017-10-02 00:18:51 -070058 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);
61 return app_ns;
62}
63
Florin Corasc1a42652019-02-08 18:27:29 -080064int
Florin Corascea194d2017-10-02 00:18:51 -070065vnet_app_namespace_add_del (vnet_app_namespace_add_del_args_t * a)
66{
67 app_namespace_t *app_ns;
68 session_table_t *st;
69
70 if (a->is_add)
71 {
72 if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX
73 && !vnet_get_sw_interface_safe (vnet_get_main (), a->sw_if_index))
Florin Corasc1a42652019-02-08 18:27:29 -080074 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
75
Florin Corascea194d2017-10-02 00:18:51 -070076
77 if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX)
78 {
79 a->ip4_fib_id =
80 fib_table_get_table_id_for_sw_if_index (FIB_PROTOCOL_IP4,
81 a->sw_if_index);
82 a->ip6_fib_id =
Florin Coras56b39f62018-03-27 17:29:32 -070083 fib_table_get_table_id_for_sw_if_index (FIB_PROTOCOL_IP6,
Florin Corascea194d2017-10-02 00:18:51 -070084 a->sw_if_index);
85 }
86 if (a->sw_if_index == APP_NAMESPACE_INVALID_INDEX
87 && a->ip4_fib_id == APP_NAMESPACE_INVALID_INDEX)
Florin Corasc1a42652019-02-08 18:27:29 -080088 return VNET_API_ERROR_INVALID_VALUE;
89
Florin Corascea194d2017-10-02 00:18:51 -070090 app_ns = app_namespace_get_from_id (a->ns_id);
91 if (!app_ns)
Florin Corasfc1c6122017-10-26 14:25:12 -070092 {
93 app_ns = app_namespace_alloc (a->ns_id);
94 st = session_table_alloc ();
Florin Coras6c36f532017-11-03 18:32:34 -070095 session_table_init (st, FIB_PROTOCOL_MAX);
96 st->is_local = 1;
97 st->appns_index = app_namespace_index (app_ns);
Florin Corasfc1c6122017-10-26 14:25:12 -070098 app_ns->local_table_index = session_table_index (st);
99 }
Florin Corascea194d2017-10-02 00:18:51 -0700100 app_ns->ns_secret = a->secret;
101 app_ns->sw_if_index = a->sw_if_index;
Florin Corascea194d2017-10-02 00:18:51 -0700102 app_ns->ip4_fib_index =
103 fib_table_find (FIB_PROTOCOL_IP4, a->ip4_fib_id);
104 app_ns->ip6_fib_index =
105 fib_table_find (FIB_PROTOCOL_IP6, a->ip6_fib_id);
Florin Coras6c36f532017-11-03 18:32:34 -0700106 session_lookup_set_tables_appns (app_ns);
Florin Corascea194d2017-10-02 00:18:51 -0700107 }
108 else
109 {
Florin Corasc1a42652019-02-08 18:27:29 -0800110 return VNET_API_ERROR_UNIMPLEMENTED;
Florin Corascea194d2017-10-02 00:18:51 -0700111 }
112 return 0;
113}
114
115const u8 *
116app_namespace_id (app_namespace_t * app_ns)
117{
118 return app_ns->ns_id;
119}
120
121u32
122app_namespace_index_from_id (const u8 * ns_id)
123{
124 uword *indexp;
125 indexp = hash_get_mem (app_namespace_lookup_table, ns_id);
126 if (!indexp)
127 return APP_NAMESPACE_INVALID_INDEX;
128 return *indexp;
129}
130
131const u8 *
132app_namespace_id_from_index (u32 index)
133{
134 app_namespace_t *app_ns;
135
136 app_ns = app_namespace_get (index);
137 return app_namespace_id (app_ns);
138}
139
Florin Coras1c710452017-10-17 00:03:13 -0700140u32
141app_namespace_get_fib_index (app_namespace_t * app_ns, u8 fib_proto)
142{
143 return fib_proto == FIB_PROTOCOL_IP4 ?
144 app_ns->ip4_fib_index : app_ns->ip6_fib_index;
145}
146
147session_table_t *
148app_namespace_get_local_table (app_namespace_t * app_ns)
149{
150 return session_table_get (app_ns->local_table_index);
151}
152
Florin Corascea194d2017-10-02 00:18:51 -0700153void
154app_namespaces_init (void)
155{
156 u8 *ns_id = format (0, "default");
Florin Corasfc1c6122017-10-26 14:25:12 -0700157
158 if (!app_namespace_lookup_table)
159 app_namespace_lookup_table =
160 hash_create_vec (0, sizeof (u8), sizeof (uword));
Florin Corascea194d2017-10-02 00:18:51 -0700161
162 /*
163 * Allocate default namespace
164 */
165 vnet_app_namespace_add_del_args_t a = {
166 .ns_id = ns_id,
167 .secret = 0,
168 .sw_if_index = APP_NAMESPACE_INVALID_INDEX,
169 .is_add = 1
170 };
171 vnet_app_namespace_add_del (&a);
172 vec_free (ns_id);
173}
174
175static clib_error_t *
176app_ns_fn (vlib_main_t * vm, unformat_input_t * input,
177 vlib_cli_command_t * cmd)
178{
Dave Wallace8af20542017-10-26 03:29:30 -0400179 unformat_input_t _line_input, *line_input = &_line_input;
Florin Corascea194d2017-10-02 00:18:51 -0700180 u8 is_add = 0, *ns_id = 0, secret_set = 0, sw_if_index_set = 0;
181 u32 sw_if_index, fib_id = APP_NAMESPACE_INVALID_INDEX;
182 u64 secret;
183 clib_error_t *error = 0;
Florin Corasc1a42652019-02-08 18:27:29 -0800184 int rv;
Florin Corascea194d2017-10-02 00:18:51 -0700185
186 session_cli_return_if_not_enabled ();
187
Dave Wallace8af20542017-10-26 03:29:30 -0400188 if (!unformat_user (input, unformat_line_input, line_input))
189 return 0;
190
191 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Florin Corascea194d2017-10-02 00:18:51 -0700192 {
Dave Wallace8af20542017-10-26 03:29:30 -0400193 if (unformat (line_input, "add"))
Florin Corascea194d2017-10-02 00:18:51 -0700194 is_add = 1;
Dave Wallace8af20542017-10-26 03:29:30 -0400195 else if (unformat (line_input, "id %_%v%_", &ns_id))
Florin Corascea194d2017-10-02 00:18:51 -0700196 ;
Dave Wallace8af20542017-10-26 03:29:30 -0400197 else if (unformat (line_input, "secret %lu", &secret))
Florin Corascea194d2017-10-02 00:18:51 -0700198 secret_set = 1;
Dave Wallace8af20542017-10-26 03:29:30 -0400199 else if (unformat (line_input, "sw_if_index %u", &sw_if_index))
Florin Corascea194d2017-10-02 00:18:51 -0700200 sw_if_index_set = 1;
Dave Wallace8af20542017-10-26 03:29:30 -0400201 else if (unformat (line_input, "fib_id", &fib_id))
Florin Corascea194d2017-10-02 00:18:51 -0700202 ;
203 else
Dave Wallace8af20542017-10-26 03:29:30 -0400204 {
205 error = clib_error_return (0, "unknown input `%U'",
206 format_unformat_error, line_input);
207 unformat_free (line_input);
208 return error;
209 }
Florin Corascea194d2017-10-02 00:18:51 -0700210 }
Dave Wallace8af20542017-10-26 03:29:30 -0400211 unformat_free (line_input);
Florin Corascea194d2017-10-02 00:18:51 -0700212
213 if (!ns_id || !secret_set || !sw_if_index_set)
214 {
215 vlib_cli_output (vm, "namespace-id, secret and sw_if_index must be "
216 "provided");
217 return 0;
218 }
219
220 if (is_add)
221 {
222 vnet_app_namespace_add_del_args_t args = {
223 .ns_id = ns_id,
224 .secret = secret,
225 .sw_if_index = sw_if_index,
226 .ip4_fib_id = fib_id,
227 .is_add = 1
228 };
Florin Corasc1a42652019-02-08 18:27:29 -0800229 if ((rv = vnet_app_namespace_add_del (&args)))
230 return clib_error_return (0, "app namespace add del returned %d", rv);
Florin Corascea194d2017-10-02 00:18:51 -0700231 }
232
233 return error;
234}
235
236/* *INDENT-OFF* */
237VLIB_CLI_COMMAND (app_ns_command, static) =
238{
239 .path = "app ns",
240 .short_help = "app ns [add] id <namespace-id> secret <secret> "
241 "sw_if_index <sw_if_index>",
242 .function = app_ns_fn,
243};
244/* *INDENT-ON* */
245
246u8 *
247format_app_namespace (u8 * s, va_list * args)
248{
249 app_namespace_t *app_ns = va_arg (*args, app_namespace_t *);
Florin Coras8f107b32017-11-21 22:13:03 -0800250 s = format (s, "%-10u%-20lu%-20u%-50v", app_namespace_index (app_ns),
251 app_ns->ns_secret, app_ns->sw_if_index, app_ns->ns_id);
Florin Corascea194d2017-10-02 00:18:51 -0700252 return s;
253}
254
255static clib_error_t *
Florin Corasdcdff6e2017-11-07 22:36:02 -0800256show_app_ns_fn (vlib_main_t * vm, unformat_input_t * main_input,
Florin Corascea194d2017-10-02 00:18:51 -0700257 vlib_cli_command_t * cmd)
258{
Florin Corasdcdff6e2017-11-07 22:36:02 -0800259 unformat_input_t _line_input, *line_input = &_line_input;
Florin Corascea194d2017-10-02 00:18:51 -0700260 app_namespace_t *app_ns;
261 session_table_t *st;
Florin Coras26408082017-11-09 02:06:07 -0800262 u8 *ns_id, do_table = 0, had_input = 1;
Florin Corascea194d2017-10-02 00:18:51 -0700263
264 session_cli_return_if_not_enabled ();
265
Florin Corasdcdff6e2017-11-07 22:36:02 -0800266 if (!unformat_user (main_input, unformat_line_input, line_input))
Florin Coras26408082017-11-09 02:06:07 -0800267 {
268 had_input = 0;
269 goto do_ns_list;
270 }
Florin Corasdcdff6e2017-11-07 22:36:02 -0800271
272 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Florin Corascea194d2017-10-02 00:18:51 -0700273 {
Florin Corasdcdff6e2017-11-07 22:36:02 -0800274 if (unformat (line_input, "table %_%v%_", &ns_id))
Florin Corascea194d2017-10-02 00:18:51 -0700275 do_table = 1;
276 else
Dave Wallace8af20542017-10-26 03:29:30 -0400277 {
Florin Corasdcdff6e2017-11-07 22:36:02 -0800278 vlib_cli_output (vm, "unknown input [%U]", format_unformat_error,
279 line_input);
280 goto done;
Dave Wallace8af20542017-10-26 03:29:30 -0400281 }
Florin Corascea194d2017-10-02 00:18:51 -0700282 }
283
284 if (do_table)
285 {
286 app_ns = app_namespace_get_from_id (ns_id);
287 if (!app_ns)
288 {
289 vlib_cli_output (vm, "ns %v not found", ns_id);
Florin Corasdcdff6e2017-11-07 22:36:02 -0800290 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700291 }
292 st = session_table_get (app_ns->local_table_index);
293 if (!st)
294 {
295 vlib_cli_output (vm, "table for ns %v could not be found", ns_id);
Florin Corasdcdff6e2017-11-07 22:36:02 -0800296 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700297 }
298 session_lookup_show_table_entries (vm, st, 0, 1);
299 vec_free (ns_id);
Florin Corasdcdff6e2017-11-07 22:36:02 -0800300 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700301 }
302
Florin Coras26408082017-11-09 02:06:07 -0800303do_ns_list:
Florin Coras8f107b32017-11-21 22:13:03 -0800304 vlib_cli_output (vm, "%-10s%-20s%-20s%-50s", "Index", "Secret",
305 "sw_if_index", "Name");
Florin Corasdff48db2017-11-19 18:06:58 -0800306
Florin Corascea194d2017-10-02 00:18:51 -0700307 /* *INDENT-OFF* */
308 pool_foreach (app_ns, app_namespace_pool, ({
309 vlib_cli_output (vm, "%U", format_app_namespace, app_ns);
310 }));
311 /* *INDENT-ON* */
312
Florin Corasdcdff6e2017-11-07 22:36:02 -0800313done:
Florin Coras26408082017-11-09 02:06:07 -0800314 if (had_input)
315 unformat_free (line_input);
Florin Corascea194d2017-10-02 00:18:51 -0700316 return 0;
317}
318
319/* *INDENT-OFF* */
320VLIB_CLI_COMMAND (show_app_ns_command, static) =
321{
322 .path = "show app ns",
323 .short_help = "show app ns",
324 .function = show_app_ns_fn,
325};
326/* *INDENT-ON* */
327
328/*
329 * fd.io coding-style-patch-verification: ON
330 *
331 * Local Variables:
332 * eval: (c-set-style "gnu")
333 * End:
334 */