FD.io VPP  v18.07-rc0-415-g6c78436
Vector Packet Processing
application_namespace.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 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 
18 #include <vnet/session/session.h>
19 #include <vnet/fib/fib_table.h>
20 
21 /**
22  * Hash table of application namespaces by app ns ids
23  */
25 
26 /**
27  * Pool of application namespaces
28  */
30 
33 {
34  return pool_elt_at_index (app_namespace_pool, index);
35 }
36 
39 {
40  u32 index = app_namespace_index_from_id (ns_id);
41  if (index == APP_NAMESPACE_INVALID_INDEX)
42  return 0;
43  return app_namespace_get (index);
44 }
45 
46 u32
48 {
49  return (app_ns - app_namespace_pool);
50 }
51 
54 {
55  app_namespace_t *app_ns;
56  pool_get (app_namespace_pool, app_ns);
57  memset (app_ns, 0, sizeof (*app_ns));
58  app_ns->ns_id = vec_dup (ns_id);
60  app_ns - app_namespace_pool);
61  return app_ns;
62 }
63 
66 {
67  app_namespace_t *app_ns;
68  session_table_t *st;
69 
70  if (a->is_add)
71  {
72  if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX
73  && !vnet_get_sw_interface_safe (vnet_get_main (), a->sw_if_index))
74  return clib_error_return_code (0, VNET_API_ERROR_INVALID_SW_IF_INDEX,
75  0, "sw_if_index %u doesn't exist",
76  a->sw_if_index);
77 
78  if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX)
79  {
80  a->ip4_fib_id =
82  a->sw_if_index);
83  a->ip6_fib_id =
85  a->sw_if_index);
86  }
87  if (a->sw_if_index == APP_NAMESPACE_INVALID_INDEX
88  && a->ip4_fib_id == APP_NAMESPACE_INVALID_INDEX)
89  return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0,
90  "sw_if_index or fib_id must be "
91  "configured");
92  app_ns = app_namespace_get_from_id (a->ns_id);
93  if (!app_ns)
94  {
95  app_ns = app_namespace_alloc (a->ns_id);
96  st = session_table_alloc ();
98  st->is_local = 1;
99  st->appns_index = app_namespace_index (app_ns);
100  app_ns->local_table_index = session_table_index (st);
101  }
102  app_ns->ns_secret = a->secret;
103  app_ns->sw_if_index = a->sw_if_index;
104  app_ns->ip4_fib_index =
105  fib_table_find (FIB_PROTOCOL_IP4, a->ip4_fib_id);
106  app_ns->ip6_fib_index =
107  fib_table_find (FIB_PROTOCOL_IP6, a->ip6_fib_id);
109  }
110  else
111  {
112  return clib_error_return_code (0, VNET_API_ERROR_UNIMPLEMENTED, 0,
113  "namespace deletion not supported");
114  }
115  return 0;
116 }
117 
118 const u8 *
120 {
121  return app_ns->ns_id;
122 }
123 
124 u32
126 {
127  uword *indexp;
128  indexp = hash_get_mem (app_namespace_lookup_table, ns_id);
129  if (!indexp)
131  return *indexp;
132 }
133 
134 const u8 *
136 {
137  app_namespace_t *app_ns;
138 
139  app_ns = app_namespace_get (index);
140  return app_namespace_id (app_ns);
141 }
142 
143 u32
145 {
146  return fib_proto == FIB_PROTOCOL_IP4 ?
147  app_ns->ip4_fib_index : app_ns->ip6_fib_index;
148 }
149 
152 {
153  return session_table_get (app_ns->local_table_index);
154 }
155 
156 void
158 {
159  u8 *ns_id = format (0, "default");
160 
163  hash_create_vec (0, sizeof (u8), sizeof (uword));
164 
165  /*
166  * Allocate default namespace
167  */
169  .ns_id = ns_id,
170  .secret = 0,
171  .sw_if_index = APP_NAMESPACE_INVALID_INDEX,
172  .is_add = 1
173  };
175  vec_free (ns_id);
176 }
177 
178 static clib_error_t *
180  vlib_cli_command_t * cmd)
181 {
182  unformat_input_t _line_input, *line_input = &_line_input;
183  u8 is_add = 0, *ns_id = 0, secret_set = 0, sw_if_index_set = 0;
184  u32 sw_if_index, fib_id = APP_NAMESPACE_INVALID_INDEX;
185  u64 secret;
186  clib_error_t *error = 0;
187 
189 
190  if (!unformat_user (input, unformat_line_input, line_input))
191  return 0;
192 
193  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
194  {
195  if (unformat (line_input, "add"))
196  is_add = 1;
197  else if (unformat (line_input, "id %_%v%_", &ns_id))
198  ;
199  else if (unformat (line_input, "secret %lu", &secret))
200  secret_set = 1;
201  else if (unformat (line_input, "sw_if_index %u", &sw_if_index))
202  sw_if_index_set = 1;
203  else if (unformat (line_input, "fib_id", &fib_id))
204  ;
205  else
206  {
207  error = clib_error_return (0, "unknown input `%U'",
208  format_unformat_error, line_input);
209  unformat_free (line_input);
210  return error;
211  }
212  }
213  unformat_free (line_input);
214 
215  if (!ns_id || !secret_set || !sw_if_index_set)
216  {
217  vlib_cli_output (vm, "namespace-id, secret and sw_if_index must be "
218  "provided");
219  return 0;
220  }
221 
222  if (is_add)
223  {
225  .ns_id = ns_id,
226  .secret = secret,
227  .sw_if_index = sw_if_index,
228  .ip4_fib_id = fib_id,
229  .is_add = 1
230  };
231  error = vnet_app_namespace_add_del (&args);
232  }
233 
234  return error;
235 }
236 
237 /* *INDENT-OFF* */
238 VLIB_CLI_COMMAND (app_ns_command, static) =
239 {
240  .path = "app ns",
241  .short_help = "app ns [add] id <namespace-id> secret <secret> "
242  "sw_if_index <sw_if_index>",
243  .function = app_ns_fn,
244 };
245 /* *INDENT-ON* */
246 
247 u8 *
248 format_app_namespace (u8 * s, va_list * args)
249 {
250  app_namespace_t *app_ns = va_arg (*args, app_namespace_t *);
251  s = format (s, "%-10u%-20lu%-20u%-50v", app_namespace_index (app_ns),
252  app_ns->ns_secret, app_ns->sw_if_index, app_ns->ns_id);
253  return s;
254 }
255 
256 static clib_error_t *
258  vlib_cli_command_t * cmd)
259 {
260  unformat_input_t _line_input, *line_input = &_line_input;
261  app_namespace_t *app_ns;
262  session_table_t *st;
263  u8 *ns_id, do_table = 0, had_input = 1;
264 
266 
267  if (!unformat_user (main_input, unformat_line_input, line_input))
268  {
269  had_input = 0;
270  goto do_ns_list;
271  }
272 
273  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
274  {
275  if (unformat (line_input, "table %_%v%_", &ns_id))
276  do_table = 1;
277  else
278  {
279  vlib_cli_output (vm, "unknown input [%U]", format_unformat_error,
280  line_input);
281  goto done;
282  }
283  }
284 
285  if (do_table)
286  {
287  app_ns = app_namespace_get_from_id (ns_id);
288  if (!app_ns)
289  {
290  vlib_cli_output (vm, "ns %v not found", ns_id);
291  goto done;
292  }
293  st = session_table_get (app_ns->local_table_index);
294  if (!st)
295  {
296  vlib_cli_output (vm, "table for ns %v could not be found", ns_id);
297  goto done;
298  }
299  session_lookup_show_table_entries (vm, st, 0, 1);
300  vec_free (ns_id);
301  goto done;
302  }
303 
304 do_ns_list:
305  vlib_cli_output (vm, "%-10s%-20s%-20s%-50s", "Index", "Secret",
306  "sw_if_index", "Name");
307 
308  /* *INDENT-OFF* */
309  pool_foreach (app_ns, app_namespace_pool, ({
310  vlib_cli_output (vm, "%U", format_app_namespace, app_ns);
311  }));
312  /* *INDENT-ON* */
313 
314 done:
315  if (had_input)
316  unformat_free (line_input);
317  return 0;
318 }
319 
320 /* *INDENT-OFF* */
321 VLIB_CLI_COMMAND (show_app_ns_command, static) =
322 {
323  .path = "show app ns",
324  .short_help = "show app ns",
325  .function = show_app_ns_fn,
326 };
327 /* *INDENT-ON* */
328 
329 /*
330  * fd.io coding-style-patch-verification: ON
331  *
332  * Local Variables:
333  * eval: (c-set-style "gnu")
334  * End:
335  */
#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)
clib_error_t * vnet_app_namespace_add_del(vnet_app_namespace_add_del_args_t *a)
a
Definition: bitmap.h:537
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
#define session_cli_return_if_not_enabled()
Definition: session.h:616
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)
void session_lookup_set_tables_appns(app_namespace_t *app_ns)
Mark (global) tables as pertaining to app ns.
struct _vnet_app_namespace_add_del_args vnet_app_namespace_add_del_args_t
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:983
#define hash_set_mem(h, key, value)
Definition: hash.h:275
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:227
unsigned char u8
Definition: types.h:56
app_namespace_t * app_namespace_alloc(u8 *ns_id)
u8 * format_app_namespace(u8 *s, va_list *args)
void app_namespaces_init(void)
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:440
#define clib_error_return(e, args...)
Definition: error.h:99
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:1056
unformat_function_t unformat_line_input
Definition: format.h:281
void session_table_init(session_table_t *slt, u8 fib_proto)
Initialize session table hash tables.
Definition: session_table.c:70
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:461
struct _unformat_input_t unformat_input_t
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:373
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:143
vlib_main_t * vm
Definition: buffer.c:294
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:339
static vnet_sw_interface_t * vnet_get_sw_interface_safe(vnet_main_t *vnm, u32 sw_if_index)
const u8 * app_namespace_id_from_index(u32 index)
const u8 * app_namespace_id(app_namespace_t *app_ns)
struct _app_namespace app_namespace_t
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
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:1032
#define hash_create_vec(elts, key_bytes, value_bytes)
Definition: hash.h:668
u64 uword
Definition: types.h:112
static void unformat_free(unformat_input_t *i)
Definition: format.h:161
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:52
#define hash_get_mem(h, key)
Definition: hash.h:269
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.
#define clib_error_return_code(e, code, flags, args...)
Definition: error.h:93
static clib_error_t * show_app_ns_fn(vlib_main_t *vm, unformat_input_t *main_input, vlib_cli_command_t *cmd)
app_namespace_t * app_namespace_get_from_id(const u8 *ns_id)
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:681
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169