FD.io VPP  v21.01.1
Vector Packet Processing
application_namespace.c
Go to the documentation of this file.
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 
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  */
28 
29 /**
30  * Pool of application namespaces
31  */
33 
35 
38 {
39  return pool_elt_at_index (app_namespace_pool, index);
40 }
41 
44 {
46  if (index == APP_NAMESPACE_INVALID_INDEX)
47  return 0;
48  return app_namespace_get (index);
49 }
50 
51 u32
53 {
54  return (app_ns - app_namespace_pool);
55 }
56 
59 {
60  app_namespace_t *app_ns;
61  pool_get (app_namespace_pool, app_ns);
62  clib_memset (app_ns, 0, sizeof (*app_ns));
63  app_ns->ns_id = vec_dup (ns_id);
65  app_ns - app_namespace_pool);
66  return app_ns;
67 }
68 
69 int
71 {
72  app_namespace_t *app_ns;
73  session_table_t *st;
74 
75  if (a->is_add)
76  {
77  if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX
79  a->sw_if_index))
80  return VNET_API_ERROR_INVALID_SW_IF_INDEX;
81 
82 
83  if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX)
84  {
85  a->ip4_fib_id =
87  a->sw_if_index);
88  a->ip6_fib_id =
90  a->sw_if_index);
91  }
92  if (a->sw_if_index == APP_NAMESPACE_INVALID_INDEX
93  && a->ip4_fib_id == APP_NAMESPACE_INVALID_INDEX)
94  return VNET_API_ERROR_INVALID_VALUE;
95 
96  app_ns = app_namespace_get_from_id (a->ns_id);
97  if (!app_ns)
98  {
99  app_ns = app_namespace_alloc (a->ns_id);
100  st = session_table_alloc ();
102  st->is_local = 1;
103  st->appns_index = app_namespace_index (app_ns);
104  app_ns->local_table_index = session_table_index (st);
105  }
106  app_ns->ns_secret = a->secret;
107  app_ns->sw_if_index = a->sw_if_index;
108  app_ns->ip4_fib_index =
109  fib_table_find (FIB_PROTOCOL_IP4, a->ip4_fib_id);
110  app_ns->ip6_fib_index =
111  fib_table_find (FIB_PROTOCOL_IP6, a->ip6_fib_id);
113 
114  /* Add socket for namespace */
115  if (app_sapi_enabled)
116  appns_sapi_add_ns_socket (app_ns);
117  }
118  else
119  {
120  return VNET_API_ERROR_UNIMPLEMENTED;
121  }
122  return 0;
123 }
124 
125 const u8 *
127 {
128  return app_ns->ns_id;
129 }
130 
131 u32
133 {
134  uword *indexp;
135  indexp = hash_get_mem (app_namespace_lookup_table, ns_id);
136  if (!indexp)
138  return *indexp;
139 }
140 
141 const u8 *
143 {
144  app_namespace_t *app_ns;
145 
146  app_ns = app_namespace_get (index);
147  return app_namespace_id (app_ns);
148 }
149 
150 u32
152 {
153  return fib_proto == FIB_PROTOCOL_IP4 ?
154  app_ns->ip4_fib_index : app_ns->ip6_fib_index;
155 }
156 
159 {
160  return session_table_get (app_ns->local_table_index);
161 }
162 
163 void
165 {
166  app_sapi_enabled = 1;
167 }
168 
169 u8
171 {
172  return app_sapi_enabled;
173 }
174 
175 void
177 {
178  u8 *ns_id = format (0, "default");
179 
182  hash_create_vec (0, sizeof (u8), sizeof (uword));
183 
184  /*
185  * Allocate default namespace
186  */
188  .ns_id = ns_id,
189  .secret = 0,
190  .sw_if_index = APP_NAMESPACE_INVALID_INDEX,
191  .is_add = 1
192  };
194  vec_free (ns_id);
195 }
196 
197 static clib_error_t *
199  vlib_cli_command_t * cmd)
200 {
201  unformat_input_t _line_input, *line_input = &_line_input;
202  u8 is_add = 0, *ns_id = 0, secret_set = 0, sw_if_index_set = 0;
204  u64 secret;
205  clib_error_t *error = 0;
206  int rv;
207 
209 
210  if (!unformat_user (input, unformat_line_input, line_input))
211  return 0;
212 
213  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
214  {
215  if (unformat (line_input, "add"))
216  is_add = 1;
217  else if (unformat (line_input, "id %_%v%_", &ns_id))
218  ;
219  else if (unformat (line_input, "secret %lu", &secret))
220  secret_set = 1;
221  else if (unformat (line_input, "sw_if_index %u", &sw_if_index))
222  sw_if_index_set = 1;
223  else if (unformat (line_input, "fib_id", &fib_id))
224  ;
225  else
226  {
227  error = clib_error_return (0, "unknown input `%U'",
228  format_unformat_error, line_input);
229  unformat_free (line_input);
230  return error;
231  }
232  }
233  unformat_free (line_input);
234 
235  if (!ns_id || !secret_set || !sw_if_index_set)
236  {
237  vlib_cli_output (vm, "namespace-id, secret and sw_if_index must be "
238  "provided");
239  return 0;
240  }
241 
242  if (is_add)
243  {
245  .ns_id = ns_id,
246  .secret = secret,
247  .sw_if_index = sw_if_index,
248  .ip4_fib_id = fib_id,
249  .is_add = 1
250  };
251  if ((rv = vnet_app_namespace_add_del (&args)))
252  return clib_error_return (0, "app namespace add del returned %d", rv);
253  }
254 
255  return error;
256 }
257 
258 /* *INDENT-OFF* */
259 VLIB_CLI_COMMAND (app_ns_command, static) =
260 {
261  .path = "app ns",
262  .short_help = "app ns [add] id <namespace-id> secret <secret> "
263  "sw_if_index <sw_if_index>",
264  .function = app_ns_fn,
265 };
266 /* *INDENT-ON* */
267 
268 u8 *
269 format_app_namespace (u8 * s, va_list * args)
270 {
271  app_namespace_t *app_ns = va_arg (*args, app_namespace_t *);
272  s = format (s, "%-10u%-20lu%-20u%-50v", app_namespace_index (app_ns),
273  app_ns->ns_secret, app_ns->sw_if_index, app_ns->ns_id);
274  return s;
275 }
276 
277 static void
279 {
280  app_ns_api_handle_t *handle;
281  app_worker_t *app_wrk;
282  clib_socket_t *cs;
283  clib_file_t *cf;
284 
285  if (!app_sapi_enabled)
286  {
287  vlib_cli_output (vm, "app socket api not enabled!");
288  return;
289  }
290 
291  vlib_cli_output (vm, "socket: %v\n", app_ns->sock_name);
292 
293  if (!pool_elts (app_ns->app_sockets))
294  return;
295 
296  vlib_cli_output (vm, "%12s%12s%5s", "app index", "wrk index", "fd");
297 
298 
299  /* *INDENT-OFF* */
300  pool_foreach (cs, app_ns->app_sockets) {
301  handle = (app_ns_api_handle_t *) &cs->private_data;
302  cf = clib_file_get (&file_main, handle->aah_file_index);
303  if (handle->aah_app_wrk_index == APP_INVALID_INDEX)
304  {
305  vlib_cli_output (vm, "%12d%12d%5u", -1, -1, cf->file_descriptor);
306  continue;
307  }
308  app_wrk = app_worker_get (handle->aah_app_wrk_index);
309  vlib_cli_output (vm, "%12d%12d%5u", app_wrk->app_index,
310  app_wrk->wrk_map_index, cf->file_descriptor);
311  }
312  /* *INDENT-ON* */
313 }
314 
315 static clib_error_t *
317  vlib_cli_command_t * cmd)
318 {
319  unformat_input_t _line_input, *line_input = &_line_input;
320  u8 *ns_id, do_table = 0, had_input = 1, do_api = 0;
321  app_namespace_t *app_ns;
322  session_table_t *st;
323 
325 
326  if (!unformat_user (main_input, unformat_line_input, line_input))
327  {
328  had_input = 0;
329  goto do_ns_list;
330  }
331 
332  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
333  {
334  if (unformat (line_input, "table %_%v%_", &ns_id))
335  do_table = 1;
336  else if (unformat (line_input, "api-clients"))
337  do_api = 1;
338  else
339  {
340  vlib_cli_output (vm, "unknown input [%U]", format_unformat_error,
341  line_input);
342  goto done;
343  }
344  }
345 
346  if (do_api)
347  {
348  if (!do_table)
349  {
350  vlib_cli_output (vm, "must specify a table for api");
351  goto done;
352  }
353  app_ns = app_namespace_get_from_id (ns_id);
354  app_namespace_show_api (vm, app_ns);
355  goto done;
356  }
357  if (do_table)
358  {
359  app_ns = app_namespace_get_from_id (ns_id);
360  if (!app_ns)
361  {
362  vlib_cli_output (vm, "ns %v not found", ns_id);
363  goto done;
364  }
365  st = session_table_get (app_ns->local_table_index);
366  if (!st)
367  {
368  vlib_cli_output (vm, "table for ns %v could not be found", ns_id);
369  goto done;
370  }
371  session_lookup_show_table_entries (vm, st, 0, 1);
372  vec_free (ns_id);
373  goto done;
374  }
375 
376 do_ns_list:
377  vlib_cli_output (vm, "%-10s%-20s%-20s%-50s", "Index", "Secret",
378  "sw_if_index", "Name");
379 
380  /* *INDENT-OFF* */
381  pool_foreach (app_ns, app_namespace_pool) {
382  vlib_cli_output (vm, "%U", format_app_namespace, app_ns);
383  }
384  /* *INDENT-ON* */
385 
386 done:
387  if (had_input)
388  unformat_free (line_input);
389  return 0;
390 }
391 
392 /* *INDENT-OFF* */
393 VLIB_CLI_COMMAND (show_app_ns_command, static) =
394 {
395  .path = "show app ns",
396  .short_help = "show app ns [table <id> [api-clients]]",
397  .function = show_app_ns_fn,
398 };
399 /* *INDENT-ON* */
400 
401 /*
402  * fd.io coding-style-patch-verification: ON
403  *
404  * Local Variables:
405  * eval: (c-set-style "gnu")
406  * End:
407  */
#define APP_NAMESPACE_INVALID_INDEX
session_table_t * session_table_alloc(void)
Definition: session_table.c:31
void session_lookup_show_table_entries(vlib_main_t *vm, session_table_t *table, u8 type, u8 is_local)
a
Definition: bitmap.h:544
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#define pool_foreach(VAR, POOL)
Iterate through pool.
Definition: pool.h:527
#define session_cli_return_if_not_enabled()
Definition: session.h:656
int vnet_app_namespace_add_del(vnet_app_namespace_add_del_args_t *a)
u32 app_namespace_index(app_namespace_t *app_ns)
unsigned long u64
Definition: types.h:89
static clib_error_t * app_ns_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
void session_lookup_set_tables_appns(app_namespace_t *app_ns)
Mark (global) tables as pertaining to app ns.
u32 file_descriptor
Definition: file.h:54
struct _vnet_app_namespace_add_del_args vnet_app_namespace_add_del_args_t
u32 wrk_map_index
Worker index in app&#39;s map pool.
Definition: application.h:40
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
#define hash_set_mem(h, key, value)
Definition: hash.h:275
vlib_main_t * vm
Definition: in2out_ed.c:1580
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:251
unsigned char u8
Definition: types.h:56
app_namespace_t * app_namespace_alloc(u8 *ns_id)
int appns_sapi_add_ns_socket(app_namespace_t *app_ns)
Definition: session_api.c:1663
u8 * format_app_namespace(u8 *s, va_list *args)
static clib_file_t * clib_file_get(clib_file_main_t *fm, u32 file_index)
Definition: file.h:152
void app_namespaces_init(void)
description fragment has unexpected format
Definition: map.api:433
#define clib_error_return(e, args...)
Definition: error.h:99
clib_file_main_t file_main
Definition: main.c:63
unsigned int u32
Definition: types.h:88
u32 app_namespace_index_from_id(const u8 *ns_id)
u32 fib_table_find(fib_protocol_t proto, u32 table_id)
Get the index of the FIB for a Table-ID.
Definition: fib_table.c:1106
unformat_function_t unformat_line_input
Definition: format.h:282
void session_table_init(session_table_t *slt, u8 fib_proto)
Initialize session table hash tables.
Definition: session_table.c:70
Definition: cJSON.c:84
u32 app_namespace_get_fib_index(app_namespace_t *app_ns, u8 fib_proto)
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:546
static void app_namespace_show_api(vlib_main_t *vm, app_namespace_t *app_ns)
struct _unformat_input_t unformat_input_t
#define APP_INVALID_INDEX
Definition: application.h:178
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:429
app_namespace_t * app_namespace_get(u32 index)
uword * app_namespace_lookup_table
Hash table of application namespaces by app ns ids.
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
session_table_t * app_namespace_get_local_table(app_namespace_t *app_ns)
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
const u8 * app_namespace_id_from_index(u32 index)
static u8 app_sapi_enabled
const u8 * app_namespace_id(app_namespace_t *app_ns)
struct _app_namespace app_namespace_t
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:158
session_table_t * session_table_get(u32 table_index)
Definition: session_table.c:46
u32 fib_table_get_table_id_for_sw_if_index(fib_protocol_t proto, u32 sw_if_index)
Get the Table-ID of the FIB bound to the interface.
Definition: fib_table.c:1082
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:696
struct _socket_t clib_socket_t
#define hash_create_vec(elts, key_bytes, value_bytes)
Definition: hash.h:668
app_worker_t * app_worker_get(u32 wrk_index)
void appns_sapi_enable(void)
u64 uword
Definition: types.h:112
static void unformat_free(unformat_input_t *i)
Definition: format.h:162
u32 index
Definition: flow_types.api:221
struct _session_lookup_table session_table_t
#define FIB_PROTOCOL_MAX
Definition outside of enum so it does not need to be included in non-defaulted switch statements...
Definition: fib_types.h:51
#define hash_get_mem(h, key)
Definition: hash.h:269
u8 appns_sapi_enabled(void)
u32 app_index
Index of owning app.
Definition: application.h:43
u32 session_table_index(session_table_t *slt)
Definition: session_table.c:40
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:91
static app_namespace_t * app_namespace_pool
Pool of application namespaces.
static clib_error_t * show_app_ns_fn(vlib_main_t *vm, unformat_input_t *main_input, vlib_cli_command_t *cmd)
Definition: file.h:51
app_namespace_t * app_namespace_get_from_id(const u8 *ns_id)
static vnet_sw_interface_t * vnet_get_sw_interface_or_null(vnet_main_t *vnm, u32 sw_if_index)
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:127