blob: 7f90943f9cd9f513e338db310d66d48ddd3c4122 [file] [log] [blame]
Florin Corascea194d2017-10-02 00:18:51 -07001/*
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/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);
57 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);
61 return app_ns;
62}
63
64clib_error_t *
65vnet_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))
74 return clib_error_return_code (0, VNET_API_ERROR_INVALID_SW_IF_INDEX,
75 0, "sw_if_index %u doesn't exist",
76 a->sw_if_index);
77
78 if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX)
79 {
80 a->ip4_fib_id =
81 fib_table_get_table_id_for_sw_if_index (FIB_PROTOCOL_IP4,
82 a->sw_if_index);
83 a->ip6_fib_id =
84 fib_table_get_table_id_for_sw_if_index (FIB_PROTOCOL_IP4,
85 a->sw_if_index);
86 }
87 if (a->sw_if_index == APP_NAMESPACE_INVALID_INDEX
88 && a->ip4_fib_id == APP_NAMESPACE_INVALID_INDEX)
89 return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
90 "sw_if_index or fib_id must be "
91 "configured");
92 st = session_table_alloc ();
93 session_table_init (st);
94
95 app_ns = app_namespace_get_from_id (a->ns_id);
96 if (!app_ns)
97 app_ns = app_namespace_alloc (a->ns_id);
98 app_ns->ns_secret = a->secret;
99 app_ns->sw_if_index = a->sw_if_index;
100 app_ns->local_table_index = session_table_index (st);
101 app_ns->ip4_fib_index =
102 fib_table_find (FIB_PROTOCOL_IP4, a->ip4_fib_id);
103 app_ns->ip6_fib_index =
104 fib_table_find (FIB_PROTOCOL_IP6, a->ip6_fib_id);
105 }
106 else
107 {
108 return clib_error_return_code (0, VNET_API_ERROR_UNIMPLEMENTED, 0,
109 "namespace deletion not supported");
110 }
111 return 0;
112}
113
114const u8 *
115app_namespace_id (app_namespace_t * app_ns)
116{
117 return app_ns->ns_id;
118}
119
120u32
121app_namespace_index_from_id (const u8 * ns_id)
122{
123 uword *indexp;
124 indexp = hash_get_mem (app_namespace_lookup_table, ns_id);
125 if (!indexp)
126 return APP_NAMESPACE_INVALID_INDEX;
127 return *indexp;
128}
129
130const u8 *
131app_namespace_id_from_index (u32 index)
132{
133 app_namespace_t *app_ns;
134
135 app_ns = app_namespace_get (index);
136 return app_namespace_id (app_ns);
137}
138
139void
140app_namespaces_init (void)
141{
142 u8 *ns_id = format (0, "default");
143 app_namespace_lookup_table =
144 hash_create_vec (0, sizeof (u8), sizeof (uword));
145
146 /*
147 * Allocate default namespace
148 */
149 vnet_app_namespace_add_del_args_t a = {
150 .ns_id = ns_id,
151 .secret = 0,
152 .sw_if_index = APP_NAMESPACE_INVALID_INDEX,
153 .is_add = 1
154 };
155 vnet_app_namespace_add_del (&a);
156 vec_free (ns_id);
157}
158
159static clib_error_t *
160app_ns_fn (vlib_main_t * vm, unformat_input_t * input,
161 vlib_cli_command_t * cmd)
162{
163 u8 is_add = 0, *ns_id = 0, secret_set = 0, sw_if_index_set = 0;
164 u32 sw_if_index, fib_id = APP_NAMESPACE_INVALID_INDEX;
165 u64 secret;
166 clib_error_t *error = 0;
167
168 session_cli_return_if_not_enabled ();
169
170 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
171 {
172 if (unformat (input, "add"))
173 is_add = 1;
174 else if (unformat (input, "id %_%v%_", &ns_id))
175 ;
176 else if (unformat (input, "secret %lu", &secret))
177 secret_set = 1;
178 else if (unformat (input, "sw_if_index %u", &sw_if_index))
179 sw_if_index_set = 1;
180 else if (unformat (input, "fib_id", &fib_id))
181 ;
182 else
183 return clib_error_return (0, "unknown input `%U'",
184 format_unformat_error, input);
185 }
186
187 if (!ns_id || !secret_set || !sw_if_index_set)
188 {
189 vlib_cli_output (vm, "namespace-id, secret and sw_if_index must be "
190 "provided");
191 return 0;
192 }
193
194 if (is_add)
195 {
196 vnet_app_namespace_add_del_args_t args = {
197 .ns_id = ns_id,
198 .secret = secret,
199 .sw_if_index = sw_if_index,
200 .ip4_fib_id = fib_id,
201 .is_add = 1
202 };
203 error = vnet_app_namespace_add_del (&args);
204 }
205
206 return error;
207}
208
209/* *INDENT-OFF* */
210VLIB_CLI_COMMAND (app_ns_command, static) =
211{
212 .path = "app ns",
213 .short_help = "app ns [add] id <namespace-id> secret <secret> "
214 "sw_if_index <sw_if_index>",
215 .function = app_ns_fn,
216};
217/* *INDENT-ON* */
218
219u8 *
220format_app_namespace (u8 * s, va_list * args)
221{
222 app_namespace_t *app_ns = va_arg (*args, app_namespace_t *);
223 s = format (s, "%-20v%-20lu%-20u", app_ns->ns_id, app_ns->ns_secret,
224 app_ns->sw_if_index);
225 return s;
226}
227
228static clib_error_t *
229show_app_ns_fn (vlib_main_t * vm, unformat_input_t * input,
230 vlib_cli_command_t * cmd)
231{
232 app_namespace_t *app_ns;
233 session_table_t *st;
234 u8 *ns_id, do_table = 0;
235
236 session_cli_return_if_not_enabled ();
237
238 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
239 {
240 if (unformat (input, "table %_%v%_", &ns_id))
241 do_table = 1;
242 else
243 return clib_error_return (0, "unknown input `%U'",
244 format_unformat_error, input);
245 }
246
247 if (do_table)
248 {
249 app_ns = app_namespace_get_from_id (ns_id);
250 if (!app_ns)
251 {
252 vlib_cli_output (vm, "ns %v not found", ns_id);
253 return 0;
254 }
255 st = session_table_get (app_ns->local_table_index);
256 if (!st)
257 {
258 vlib_cli_output (vm, "table for ns %v could not be found", ns_id);
259 return 0;
260 }
261 session_lookup_show_table_entries (vm, st, 0, 1);
262 vec_free (ns_id);
263 return 0;
264 }
265
266 vlib_cli_output (vm, "%-20s%-20s%-20s", "Namespace", "Secret",
267 "sw_if_index");
268
269 /* *INDENT-OFF* */
270 pool_foreach (app_ns, app_namespace_pool, ({
271 vlib_cli_output (vm, "%U", format_app_namespace, app_ns);
272 }));
273 /* *INDENT-ON* */
274
275 return 0;
276}
277
278/* *INDENT-OFF* */
279VLIB_CLI_COMMAND (show_app_ns_command, static) =
280{
281 .path = "show app ns",
282 .short_help = "show app ns",
283 .function = show_app_ns_fn,
284};
285/* *INDENT-ON* */
286
287/*
288 * fd.io coding-style-patch-verification: ON
289 *
290 * Local Variables:
291 * eval: (c-set-style "gnu")
292 * End:
293 */