Line data Source code
1 : /*
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:
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/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>
24 :
25 : /**
26 : * Hash table of application namespaces by app ns ids
27 : */
28 : uword *app_namespace_lookup_table;
29 :
30 : /**
31 : * Pool of application namespaces
32 : */
33 : static app_namespace_t *app_namespace_pool;
34 :
35 : static u8 app_sapi_enabled;
36 :
37 : app_namespace_t *
38 528 : app_namespace_get (u32 index)
39 : {
40 528 : return pool_elt_at_index (app_namespace_pool, index);
41 : }
42 :
43 : app_namespace_t *
44 97 : app_namespace_get_from_id (const u8 *ns_id)
45 : {
46 97 : u32 index = app_namespace_index_from_id (ns_id);
47 97 : if (index == APP_NAMESPACE_INVALID_INDEX)
48 92 : return 0;
49 5 : return app_namespace_get (index);
50 : }
51 :
52 : u32
53 361 : app_namespace_index (app_namespace_t * app_ns)
54 : {
55 361 : return (app_ns - app_namespace_pool);
56 : }
57 :
58 : void
59 3 : app_namespace_free (app_namespace_t *app_ns)
60 : {
61 6 : hash_unset_mem (app_namespace_lookup_table, app_ns->ns_id);
62 3 : vec_free (app_ns->ns_id);
63 :
64 3 : pool_put (app_namespace_pool, app_ns);
65 3 : }
66 :
67 : app_namespace_t *
68 92 : app_namespace_alloc (const u8 *ns_id)
69 : {
70 : app_namespace_t *app_ns;
71 :
72 92 : pool_get (app_namespace_pool, app_ns);
73 92 : clib_memset (app_ns, 0, sizeof (*app_ns));
74 :
75 92 : app_ns->ns_id = vec_dup ((u8 *) ns_id);
76 92 : vec_terminate_c_string (app_ns->ns_id);
77 :
78 184 : hash_set_mem (app_namespace_lookup_table, app_ns->ns_id,
79 : app_ns - app_namespace_pool);
80 :
81 92 : return app_ns;
82 : }
83 :
84 : session_error_t
85 98 : vnet_app_namespace_add_del (vnet_app_namespace_add_del_args_t *a)
86 : {
87 : app_namespace_t *app_ns;
88 : session_table_t *st;
89 : u32 ns_index;
90 : session_error_t rv;
91 :
92 98 : if (a->is_add)
93 : {
94 95 : if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX
95 44 : && !vnet_get_sw_interface_or_null (vnet_get_main (),
96 : a->sw_if_index))
97 0 : return SESSION_E_INVALID;
98 :
99 95 : if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX)
100 : {
101 44 : a->ip4_fib_id =
102 44 : fib_table_get_table_id_for_sw_if_index (FIB_PROTOCOL_IP4,
103 : a->sw_if_index);
104 44 : a->ip6_fib_id =
105 44 : fib_table_get_table_id_for_sw_if_index (FIB_PROTOCOL_IP6,
106 : a->sw_if_index);
107 : }
108 95 : if (a->sw_if_index == APP_NAMESPACE_INVALID_INDEX
109 51 : && a->ip4_fib_id == APP_NAMESPACE_INVALID_INDEX)
110 0 : return SESSION_E_INVALID;
111 :
112 95 : app_ns = app_namespace_get_from_id (a->ns_id);
113 95 : if (!app_ns)
114 : {
115 92 : app_ns = app_namespace_alloc (a->ns_id);
116 92 : st = session_table_alloc ();
117 92 : session_table_init (st, FIB_PROTOCOL_MAX);
118 92 : st->is_local = 1;
119 92 : st->appns_index = app_namespace_index (app_ns);
120 92 : app_ns->local_table_index = session_table_index (st);
121 92 : if (a->sock_name)
122 : {
123 0 : app_ns->sock_name = vec_dup (a->sock_name);
124 0 : vec_terminate_c_string (app_ns->sock_name);
125 : }
126 :
127 : /* Add socket for namespace,
128 : * only at creation time */
129 92 : if (app_sapi_enabled)
130 : {
131 35 : rv = appns_sapi_add_ns_socket (app_ns);
132 35 : if (rv)
133 0 : return rv;
134 : }
135 : }
136 :
137 95 : app_ns->ns_secret = a->secret;
138 95 : app_ns->sw_if_index = a->sw_if_index;
139 95 : app_ns->ip4_fib_index =
140 95 : fib_table_find (FIB_PROTOCOL_IP4, a->ip4_fib_id);
141 95 : app_ns->ip6_fib_index =
142 95 : fib_table_find (FIB_PROTOCOL_IP6, a->ip6_fib_id);
143 95 : session_lookup_set_tables_appns (app_ns);
144 :
145 : }
146 : else
147 : {
148 3 : ns_index = app_namespace_index_from_id (a->ns_id);
149 3 : if (ns_index == APP_NAMESPACE_INVALID_INDEX)
150 0 : return SESSION_E_INVALID;
151 :
152 3 : app_ns = app_namespace_get (ns_index);
153 3 : if (!app_ns)
154 0 : return SESSION_E_INVALID;
155 :
156 3 : application_namespace_cleanup (app_ns);
157 :
158 3 : if (app_sapi_enabled)
159 0 : appns_sapi_del_ns_socket (app_ns);
160 :
161 3 : st = session_table_get (app_ns->local_table_index);
162 :
163 3 : session_table_free (st, FIB_PROTOCOL_MAX);
164 3 : if (app_ns->sock_name)
165 0 : vec_free (app_ns->sock_name);
166 :
167 3 : app_namespace_free (app_ns);
168 : }
169 :
170 98 : return 0;
171 : }
172 :
173 : const u8 *
174 0 : app_namespace_id (app_namespace_t * app_ns)
175 : {
176 0 : return app_ns->ns_id;
177 : }
178 :
179 : u32
180 196 : app_namespace_index_from_id (const u8 * ns_id)
181 : {
182 : uword *indexp;
183 : u8 *key;
184 :
185 196 : key = vec_dup ((u8 *) ns_id);
186 196 : vec_terminate_c_string (key);
187 :
188 392 : indexp = hash_get_mem (app_namespace_lookup_table, key);
189 196 : vec_free (key);
190 196 : if (!indexp)
191 92 : return APP_NAMESPACE_INVALID_INDEX;
192 104 : return *indexp;
193 : }
194 :
195 : const u8 *
196 0 : app_namespace_id_from_index (u32 index)
197 : {
198 : app_namespace_t *app_ns;
199 :
200 0 : app_ns = app_namespace_get (index);
201 0 : return app_namespace_id (app_ns);
202 : }
203 :
204 : u32
205 207 : app_namespace_get_fib_index (app_namespace_t * app_ns, u8 fib_proto)
206 : {
207 : return fib_proto == FIB_PROTOCOL_IP4 ?
208 207 : app_ns->ip4_fib_index : app_ns->ip6_fib_index;
209 : }
210 :
211 : session_table_t *
212 18 : app_namespace_get_local_table (app_namespace_t * app_ns)
213 : {
214 18 : return session_table_get (app_ns->local_table_index);
215 : }
216 :
217 : int
218 25 : appns_sapi_enable_disable (int is_enable)
219 : {
220 : /* This cannot be called with active sockets */
221 25 : if (pool_elts (app_namespace_pool))
222 0 : return -1;
223 :
224 25 : app_sapi_enabled = is_enable;
225 25 : return 0;
226 : }
227 :
228 : u8
229 136 : appns_sapi_enabled (void)
230 : {
231 136 : return app_sapi_enabled;
232 : }
233 :
234 : void
235 49 : app_namespaces_init (void)
236 : {
237 49 : u8 *ns_id = format (0, "default");
238 :
239 49 : if (!app_namespace_lookup_table)
240 49 : app_namespace_lookup_table =
241 49 : hash_create_vec (0, sizeof (u8), sizeof (uword));
242 :
243 : /*
244 : * Allocate default namespace
245 : */
246 :
247 : /* clang-format off */
248 49 : vnet_app_namespace_add_del_args_t a = {
249 : .ns_id = ns_id,
250 : .sock_name = 0,
251 : .secret = 0,
252 : .sw_if_index = APP_NAMESPACE_INVALID_INDEX,
253 : .is_add = 1
254 : };
255 : /* clang-format on */
256 :
257 49 : vnet_app_namespace_add_del (&a);
258 49 : vec_free (ns_id);
259 49 : }
260 :
261 : static clib_error_t *
262 0 : app_ns_fn (vlib_main_t * vm, unformat_input_t * input,
263 : vlib_cli_command_t * cmd)
264 : {
265 0 : u8 is_add = 0, *ns_id = 0, secret_set = 0, sw_if_index_set = 0;
266 0 : u8 *sock_name = 0;
267 0 : unformat_input_t _line_input, *line_input = &_line_input;
268 0 : u32 sw_if_index, fib_id = APP_NAMESPACE_INVALID_INDEX;
269 0 : vnet_main_t *vnm = vnet_get_main ();
270 : u64 secret;
271 0 : clib_error_t *error = 0;
272 : int rv;
273 :
274 0 : session_cli_return_if_not_enabled ();
275 :
276 0 : if (!unformat_user (input, unformat_line_input, line_input))
277 0 : return 0;
278 :
279 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
280 : {
281 0 : if (unformat (line_input, "add"))
282 0 : is_add = 1;
283 0 : else if (unformat (line_input, "del"))
284 0 : is_add = 0;
285 0 : else if (unformat (line_input, "id %_%v%_", &ns_id))
286 : ;
287 0 : else if (unformat (line_input, "secret %lu", &secret))
288 0 : secret_set = 1;
289 0 : else if (unformat (line_input, "sw_if_index %u", &sw_if_index))
290 0 : sw_if_index_set = 1;
291 0 : else if (unformat (line_input, "if %U", unformat_vnet_sw_interface, vnm,
292 : &sw_if_index))
293 0 : sw_if_index_set = 1;
294 0 : else if (unformat (line_input, "fib_id", &fib_id))
295 : ;
296 0 : else if (unformat (line_input, "sock-name %_%v%_", &sock_name))
297 : ;
298 : else
299 : {
300 0 : error = clib_error_return (0, "unknown input `%U'",
301 : format_unformat_error, line_input);
302 0 : goto done;
303 : }
304 : }
305 :
306 0 : if (!ns_id)
307 : {
308 0 : vlib_cli_output (vm, "namespace-id must be provided");
309 0 : goto done;
310 : }
311 :
312 0 : if (is_add && (!secret_set || !sw_if_index_set))
313 : {
314 0 : vlib_cli_output (vm, "secret and interface must be provided");
315 0 : goto done;
316 : }
317 :
318 : /* clang-format off */
319 0 : vnet_app_namespace_add_del_args_t args = {
320 : .ns_id = ns_id,
321 : .secret = secret,
322 : .sw_if_index = sw_if_index,
323 : .sock_name = sock_name,
324 : .ip4_fib_id = fib_id,
325 : .is_add = is_add,
326 : };
327 : /* clang-format on */
328 :
329 0 : if ((rv = vnet_app_namespace_add_del (&args)))
330 0 : error = clib_error_return (0, "app namespace add del returned %d", rv);
331 :
332 0 : done:
333 :
334 0 : vec_free (ns_id);
335 0 : vec_free (sock_name);
336 0 : unformat_free (line_input);
337 :
338 0 : return error;
339 : }
340 :
341 : /* *INDENT-OFF* */
342 285289 : VLIB_CLI_COMMAND (app_ns_command, static) = {
343 : .path = "app ns",
344 : .short_help = "app ns [add|del] id <namespace-id> secret <secret> "
345 : "sw_if_index <sw_if_index> if <interface>",
346 : .function = app_ns_fn,
347 : };
348 : /* *INDENT-ON* */
349 :
350 : u8 *
351 0 : format_app_namespace (u8 * s, va_list * args)
352 : {
353 0 : app_namespace_t *app_ns = va_arg (*args, app_namespace_t *);
354 0 : vnet_main_t *vnm = vnet_get_main ();
355 :
356 0 : s = format (s, "Application namespace [%u]\nid: %s\nsecret: %lu",
357 : app_namespace_index (app_ns), app_ns->ns_id, app_ns->ns_secret);
358 0 : if (app_ns->sw_if_index != (u32) ~0)
359 0 : s = format (s, "\nInterface: %U", format_vnet_sw_if_index_name, vnm,
360 : app_ns->sw_if_index);
361 0 : if (app_ns->sock_name)
362 0 : s = format (s, "\nSocket: %s", app_ns->sock_name);
363 :
364 0 : return s;
365 : }
366 :
367 : static void
368 0 : app_namespace_show_api (vlib_main_t * vm, app_namespace_t * app_ns)
369 : {
370 : app_ns_api_handle_t *handle;
371 : app_worker_t *app_wrk;
372 : clib_socket_t *cs;
373 : clib_file_t *cf;
374 :
375 0 : if (!app_sapi_enabled)
376 : {
377 0 : vlib_cli_output (vm, "app socket api not enabled!");
378 0 : return;
379 : }
380 :
381 0 : vlib_cli_output (vm, "socket: %v\n", app_ns->sock_name);
382 :
383 0 : if (!pool_elts (app_ns->app_sockets))
384 0 : return;
385 :
386 0 : vlib_cli_output (vm, "%12s%12s%5s", "app index", "wrk index", "fd");
387 :
388 :
389 : /* *INDENT-OFF* */
390 0 : pool_foreach (cs, app_ns->app_sockets) {
391 0 : handle = (app_ns_api_handle_t *) &cs->private_data;
392 0 : cf = clib_file_get (&file_main, handle->aah_file_index);
393 0 : if (handle->aah_app_wrk_index == APP_INVALID_INDEX)
394 : {
395 0 : vlib_cli_output (vm, "%12d%12d%5u", -1, -1, cf->file_descriptor);
396 0 : continue;
397 : }
398 0 : app_wrk = app_worker_get (handle->aah_app_wrk_index);
399 0 : vlib_cli_output (vm, "%12d%12d%5u", app_wrk->app_index,
400 : app_wrk->wrk_map_index, cf->file_descriptor);
401 : }
402 : /* *INDENT-ON* */
403 : }
404 :
405 : static clib_error_t *
406 0 : show_app_ns_fn (vlib_main_t * vm, unformat_input_t * main_input,
407 : vlib_cli_command_t * cmd)
408 : {
409 0 : unformat_input_t _line_input, *line_input = &_line_input;
410 0 : u8 *ns_id = 0, do_table = 0, had_input = 1, do_api = 0;
411 : app_namespace_t *app_ns;
412 0 : vnet_main_t *vnm = vnet_get_main ();
413 : session_table_t *st;
414 0 : table_t table = {}, *t = &table;
415 :
416 0 : session_cli_return_if_not_enabled ();
417 :
418 0 : if (!unformat_user (main_input, unformat_line_input, line_input))
419 : {
420 0 : had_input = 0;
421 0 : goto do_ns_list;
422 : }
423 :
424 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
425 : {
426 0 : if (unformat (line_input, "id %_%v%_", &ns_id))
427 0 : do_table = 1;
428 0 : else if (unformat (line_input, "api-clients"))
429 0 : do_api = 1;
430 : else
431 : {
432 0 : vlib_cli_output (vm, "unknown input [%U]", format_unformat_error,
433 : line_input);
434 0 : goto done;
435 : }
436 : }
437 :
438 0 : if (do_api)
439 : {
440 0 : if (!do_table)
441 : {
442 0 : vlib_cli_output (vm, "must specify a table for api");
443 0 : goto done;
444 : }
445 0 : app_ns = app_namespace_get_from_id (ns_id);
446 0 : app_namespace_show_api (vm, app_ns);
447 0 : goto done;
448 : }
449 0 : if (do_table)
450 : {
451 0 : app_ns = app_namespace_get_from_id (ns_id);
452 0 : if (!app_ns)
453 : {
454 0 : vlib_cli_output (vm, "ns %v not found", ns_id);
455 0 : goto done;
456 : }
457 0 : st = session_table_get (app_ns->local_table_index);
458 0 : if (!st)
459 : {
460 0 : vlib_cli_output (vm, "table for ns %v could not be found", ns_id);
461 0 : goto done;
462 : }
463 0 : vlib_cli_output (vm, "%U", format_app_namespace, app_ns);
464 0 : session_lookup_show_table_entries (vm, st, 0, 1);
465 0 : vec_free (ns_id);
466 0 : goto done;
467 : }
468 :
469 0 : do_ns_list:
470 0 : table_add_header_col (t, 5, "Index", "Secret", "Interface", "Id", "Socket");
471 0 : int i = 0;
472 0 : pool_foreach (app_ns, app_namespace_pool)
473 : {
474 0 : int j = 0;
475 0 : table_format_cell (t, i, j++, "%u", app_namespace_index (app_ns));
476 0 : table_format_cell (t, i, j++, "%lu", app_ns->ns_secret);
477 0 : table_format_cell (t, i, j++, "%U", format_vnet_sw_if_index_name, vnm,
478 : app_ns->sw_if_index);
479 0 : table_format_cell (t, i, j++, "%s", app_ns->ns_id);
480 0 : table_format_cell (t, i++, j++, "%s", app_ns->sock_name);
481 : }
482 :
483 0 : t->default_body.align = TTAA_LEFT;
484 0 : t->default_header_col.align = TTAA_LEFT;
485 0 : t->default_header_col.fg_color = TTAC_YELLOW;
486 0 : t->default_header_col.flags = TTAF_FG_COLOR_SET;
487 0 : vlib_cli_output (vm, "%U", format_table, t);
488 0 : table_free (t);
489 :
490 0 : done:
491 0 : if (had_input)
492 0 : unformat_free (line_input);
493 0 : return 0;
494 : }
495 :
496 : /* *INDENT-OFF* */
497 285289 : VLIB_CLI_COMMAND (show_app_ns_command, static) = {
498 : .path = "show app ns",
499 : .short_help = "show app ns [id <id> [api-clients]]",
500 : .function = show_app_ns_fn,
501 : };
502 : /* *INDENT-ON* */
503 :
504 : /*
505 : * fd.io coding-style-patch-verification: ON
506 : *
507 : * Local Variables:
508 : * eval: (c-set-style "gnu")
509 : * End:
510 : */
|