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