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