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