blob: c1c8527fb0201078097a2416e4d5fdbad230015d [file] [log] [blame]
Florin Corascea194d2017-10-02 00:18:51 -07001/*
Florin Coras288eaab2019-02-03 15:26:14 -08002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Florin Corascea194d2017-10-02 00:18:51 -07003 * 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>
Florin Coras61ae0562020-09-02 19:10:28 -070017#include <vnet/session/application.h>
Florin Corascea194d2017-10-02 00:18:51 -070018#include <vnet/session/session_table.h>
19#include <vnet/session/session.h>
20#include <vnet/fib/fib_table.h>
Florin Coras61ae0562020-09-02 19:10:28 -070021#include <vppinfra/file.h>
Nathan Skrzypczak3d5e7412021-09-17 11:53:25 +020022#include <vppinfra/format_table.h>
Florin Coras61ae0562020-09-02 19:10:28 -070023#include <vlib/unix/unix.h>
Florin Corascea194d2017-10-02 00:18:51 -070024
25/**
26 * Hash table of application namespaces by app ns ids
27 */
28uword *app_namespace_lookup_table;
29
30/**
31 * Pool of application namespaces
32 */
33static app_namespace_t *app_namespace_pool;
34
Florin Coras61ae0562020-09-02 19:10:28 -070035static u8 app_sapi_enabled;
36
Steven Luongc4b5d102024-07-30 13:44:01 -070037void
38app_namespace_walk (app_namespace_walk_fn_t fn, void *ctx)
39{
40 app_namespace_t *app_ns;
41
42 pool_foreach (app_ns, app_namespace_pool)
43 {
44 fn (app_ns, ctx);
45 }
46}
47
Florin Corascea194d2017-10-02 00:18:51 -070048app_namespace_t *
49app_namespace_get (u32 index)
50{
51 return pool_elt_at_index (app_namespace_pool, index);
52}
53
54app_namespace_t *
Steven Luonge38d9472024-11-06 13:47:26 -080055app_namespace_get_if_valid (u32 index)
56{
57 if (pool_is_free_index (app_namespace_pool, index))
58 return 0;
59 return pool_elt_at_index (app_namespace_pool, index);
60}
61
62app_namespace_t *
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +020063app_namespace_get_from_id (const u8 *ns_id)
Florin Corascea194d2017-10-02 00:18:51 -070064{
65 u32 index = app_namespace_index_from_id (ns_id);
66 if (index == APP_NAMESPACE_INVALID_INDEX)
67 return 0;
68 return app_namespace_get (index);
69}
70
71u32
72app_namespace_index (app_namespace_t * app_ns)
73{
74 return (app_ns - app_namespace_pool);
75}
76
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +020077void
78app_namespace_free (app_namespace_t *app_ns)
79{
80 hash_unset_mem (app_namespace_lookup_table, app_ns->ns_id);
81 vec_free (app_ns->ns_id);
82
83 pool_put (app_namespace_pool, app_ns);
84}
85
Florin Corascea194d2017-10-02 00:18:51 -070086app_namespace_t *
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +020087app_namespace_alloc (const u8 *ns_id)
Florin Corascea194d2017-10-02 00:18:51 -070088{
89 app_namespace_t *app_ns;
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +020090
Florin Corascea194d2017-10-02 00:18:51 -070091 pool_get (app_namespace_pool, app_ns);
Dave Barachb7b92992018-10-17 10:38:51 -040092 clib_memset (app_ns, 0, sizeof (*app_ns));
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +020093
94 app_ns->ns_id = vec_dup ((u8 *) ns_id);
95 vec_terminate_c_string (app_ns->ns_id);
96
Florin Corascea194d2017-10-02 00:18:51 -070097 hash_set_mem (app_namespace_lookup_table, app_ns->ns_id,
98 app_ns - app_namespace_pool);
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +020099
Florin Corascea194d2017-10-02 00:18:51 -0700100 return app_ns;
101}
102
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200103session_error_t
104vnet_app_namespace_add_del (vnet_app_namespace_add_del_args_t *a)
Florin Corascea194d2017-10-02 00:18:51 -0700105{
106 app_namespace_t *app_ns;
107 session_table_t *st;
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200108 u32 ns_index;
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200109 session_error_t rv;
Steven Luong2e67a3f2024-11-08 08:35:14 -0800110 u32 ip4_fib_index, ip6_fib_index;
Florin Corascea194d2017-10-02 00:18:51 -0700111
112 if (a->is_add)
113 {
114 if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX
Dave Barach3940de32019-07-23 16:28:36 -0400115 && !vnet_get_sw_interface_or_null (vnet_get_main (),
116 a->sw_if_index))
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200117 return SESSION_E_INVALID;
Florin Corascea194d2017-10-02 00:18:51 -0700118
Steven Luong2e67a3f2024-11-08 08:35:14 -0800119 /*
120 * sw_if_index takes precedence over fib_id's.
121 * When sw_if_index is provided, overwrite what's in fib_id's
122 */
Florin Corascea194d2017-10-02 00:18:51 -0700123 if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX)
124 {
125 a->ip4_fib_id =
126 fib_table_get_table_id_for_sw_if_index (FIB_PROTOCOL_IP4,
127 a->sw_if_index);
128 a->ip6_fib_id =
Florin Coras56b39f62018-03-27 17:29:32 -0700129 fib_table_get_table_id_for_sw_if_index (FIB_PROTOCOL_IP6,
Florin Corascea194d2017-10-02 00:18:51 -0700130 a->sw_if_index);
131 }
Steven Luong2e67a3f2024-11-08 08:35:14 -0800132 /* Either sw_if_index or one of 2 fib_id's must be provided */
133 if (a->sw_if_index == APP_NAMESPACE_INVALID_INDEX &&
134 a->ip4_fib_id == APP_NAMESPACE_INVALID_INDEX &&
135 a->ip6_fib_id == APP_NAMESPACE_INVALID_INDEX)
136 return SESSION_E_INVALID;
137
138 /* Validate the fib_id's */
139 ip4_fib_index = fib_table_find (FIB_PROTOCOL_IP4, a->ip4_fib_id);
140 ip6_fib_index = fib_table_find (FIB_PROTOCOL_IP6, a->ip6_fib_id);
141 if ((ip4_fib_index == ~0) && (ip6_fib_index == ~0))
142 return SESSION_E_INVALID;
143
144 /* if fib_id entered, check fib_id exist */
145 if (((a->ip4_fib_id != APP_NAMESPACE_INVALID_INDEX) &&
146 (ip4_fib_index == ~0)) ||
147 ((a->ip6_fib_id != APP_NAMESPACE_INVALID_INDEX) &&
148 (ip6_fib_index == ~0)))
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200149 return SESSION_E_INVALID;
Florin Corasc1a42652019-02-08 18:27:29 -0800150
Florin Corascea194d2017-10-02 00:18:51 -0700151 app_ns = app_namespace_get_from_id (a->ns_id);
152 if (!app_ns)
Florin Corasfc1c6122017-10-26 14:25:12 -0700153 {
154 app_ns = app_namespace_alloc (a->ns_id);
155 st = session_table_alloc ();
Florin Coras6c36f532017-11-03 18:32:34 -0700156 session_table_init (st, FIB_PROTOCOL_MAX);
157 st->is_local = 1;
Steven Luonge0c4e6e2024-10-22 10:44:07 -0700158 vec_add1 (st->appns_index, app_namespace_index (app_ns));
Florin Corasfc1c6122017-10-26 14:25:12 -0700159 app_ns->local_table_index = session_table_index (st);
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200160 if (a->sock_name)
161 {
162 app_ns->sock_name = vec_dup (a->sock_name);
163 vec_terminate_c_string (app_ns->sock_name);
164 }
165
166 /* Add socket for namespace,
167 * only at creation time */
168 if (app_sapi_enabled)
169 {
170 rv = appns_sapi_add_ns_socket (app_ns);
171 if (rv)
172 return rv;
173 }
Florin Corasfc1c6122017-10-26 14:25:12 -0700174 }
Steven Luong41ae1e22024-11-14 14:28:34 -0800175 else
176 {
177 /*
178 * Not creating a new app_ns. We are just changing the binding of an
179 * existing app_ns to different fib tables. Clean up the old session
180 * table that was bound to these fib indices.
181 */
182 ns_index = app_namespace_index (app_ns);
183 session_lookup_table_cleanup (FIB_PROTOCOL_IP4,
184 app_ns->ip4_fib_index, ns_index);
185 session_lookup_table_cleanup (FIB_PROTOCOL_IP6,
186 app_ns->ip6_fib_index, ns_index);
187 }
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200188
Florin Corascea194d2017-10-02 00:18:51 -0700189 app_ns->ns_secret = a->secret;
190 app_ns->sw_if_index = a->sw_if_index;
Florin Coras61ae0562020-09-02 19:10:28 -0700191
Steven Luong2e67a3f2024-11-08 08:35:14 -0800192 app_ns->ip4_fib_index = ip4_fib_index;
193 app_ns->ip6_fib_index = ip6_fib_index;
Steven Luong67bae202024-07-08 11:21:23 -0700194 session_lookup_set_tables_appns (app_ns);
Florin Corascea194d2017-10-02 00:18:51 -0700195 }
196 else
197 {
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200198 ns_index = app_namespace_index_from_id (a->ns_id);
199 if (ns_index == APP_NAMESPACE_INVALID_INDEX)
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200200 return SESSION_E_INVALID;
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200201
202 app_ns = app_namespace_get (ns_index);
203 if (!app_ns)
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200204 return SESSION_E_INVALID;
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200205
206 application_namespace_cleanup (app_ns);
207
208 if (app_sapi_enabled)
209 appns_sapi_del_ns_socket (app_ns);
210
211 st = session_table_get (app_ns->local_table_index);
212
213 session_table_free (st, FIB_PROTOCOL_MAX);
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200214 if (app_ns->sock_name)
215 vec_free (app_ns->sock_name);
216
Steven Luonge0c4e6e2024-10-22 10:44:07 -0700217 session_lookup_table_cleanup (FIB_PROTOCOL_IP4, app_ns->ip4_fib_index,
218 ns_index);
219 session_lookup_table_cleanup (FIB_PROTOCOL_IP6, app_ns->ip6_fib_index,
220 ns_index);
Steven Luong67bae202024-07-08 11:21:23 -0700221
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200222 app_namespace_free (app_ns);
Florin Corascea194d2017-10-02 00:18:51 -0700223 }
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200224
Florin Corascea194d2017-10-02 00:18:51 -0700225 return 0;
226}
227
228const u8 *
229app_namespace_id (app_namespace_t * app_ns)
230{
231 return app_ns->ns_id;
232}
233
234u32
235app_namespace_index_from_id (const u8 * ns_id)
236{
237 uword *indexp;
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200238 u8 *key;
239
240 key = vec_dup ((u8 *) ns_id);
241 vec_terminate_c_string (key);
242
243 indexp = hash_get_mem (app_namespace_lookup_table, key);
244 vec_free (key);
Florin Corascea194d2017-10-02 00:18:51 -0700245 if (!indexp)
246 return APP_NAMESPACE_INVALID_INDEX;
247 return *indexp;
248}
249
250const u8 *
251app_namespace_id_from_index (u32 index)
252{
253 app_namespace_t *app_ns;
254
255 app_ns = app_namespace_get (index);
256 return app_namespace_id (app_ns);
257}
258
Florin Coras1c710452017-10-17 00:03:13 -0700259u32
260app_namespace_get_fib_index (app_namespace_t * app_ns, u8 fib_proto)
261{
262 return fib_proto == FIB_PROTOCOL_IP4 ?
263 app_ns->ip4_fib_index : app_ns->ip6_fib_index;
264}
265
266session_table_t *
267app_namespace_get_local_table (app_namespace_t * app_ns)
268{
269 return session_table_get (app_ns->local_table_index);
270}
271
Nathan Skrzypczak7b3a3df2021-07-28 14:09:50 +0200272int
273appns_sapi_enable_disable (int is_enable)
Florin Coras61ae0562020-09-02 19:10:28 -0700274{
Nathan Skrzypczak7b3a3df2021-07-28 14:09:50 +0200275 /* This cannot be called with active sockets */
276 if (pool_elts (app_namespace_pool))
277 return -1;
278
279 app_sapi_enabled = is_enable;
280 return 0;
Florin Coras61ae0562020-09-02 19:10:28 -0700281}
282
283u8
284appns_sapi_enabled (void)
285{
286 return app_sapi_enabled;
287}
288
Steven Luong2e67a3f2024-11-08 08:35:14 -0800289static void
290app_namespace_table_bind_v4 (ip4_main_t *im, uword opaque, u32 sw_if_index,
291 u32 new_fib_index, u32 old_fib_index)
292{
293 app_namespace_t *app_ns;
294
295 pool_foreach (app_ns, app_namespace_pool)
296 {
297 if (app_ns->sw_if_index == sw_if_index)
298 {
299 /*
300 * For add, call vnet_app_namespace_add_del without sw_if_index and
301 * keep the existing ip6_fib_index.
302 */
303 vnet_app_namespace_add_del_args_t add = {
304 .ns_id = vec_dup (app_ns->ns_id),
305 .secret = app_ns->ns_secret,
306 .sw_if_index = APP_NAMESPACE_INVALID_INDEX,
307 .sock_name = vec_dup (app_ns->sock_name),
308 .ip4_fib_id =
309 fib_table_get_table_id (new_fib_index, FIB_PROTOCOL_IP4),
310 .ip6_fib_id =
311 fib_table_get_table_id (app_ns->ip6_fib_index, FIB_PROTOCOL_IP6),
312 .is_add = 1,
313 };
314 vnet_app_namespace_add_del_args_t del = {
315 .ns_id = app_ns->ns_id,
316 .is_add = 0,
317 };
318 vnet_app_namespace_add_del (&del);
319
320 vnet_app_namespace_add_del (&add);
321
322 /* keep the sw_if_index */
323 app_ns->sw_if_index = sw_if_index;
324
325 vec_free (add.ns_id);
326 vec_free (add.sock_name);
327 }
328 }
329}
330
331static void
332app_namespace_table_bind_v6 (ip6_main_t *im, uword opaque, u32 sw_if_index,
333 u32 new_fib_index, u32 old_fib_index)
334{
335 app_namespace_t *app_ns;
336
337 pool_foreach (app_ns, app_namespace_pool)
338 {
339 if (app_ns->sw_if_index == sw_if_index)
340 {
341 /*
342 * For add, call vnet_app_namespace_add_del without sw_if_index and
343 * keep the existing ip4_fib_index.
344 */
345 vnet_app_namespace_add_del_args_t add = {
346 .ns_id = vec_dup (app_ns->ns_id),
347 .secret = app_ns->ns_secret,
348 .sw_if_index = APP_NAMESPACE_INVALID_INDEX,
349 .sock_name = vec_dup (app_ns->sock_name),
350 .ip4_fib_id =
351 fib_table_get_table_id (app_ns->ip4_fib_index, FIB_PROTOCOL_IP4),
352 .ip6_fib_id =
353 fib_table_get_table_id (new_fib_index, FIB_PROTOCOL_IP6),
354 .is_add = 1,
355 };
356 vnet_app_namespace_add_del_args_t del = {
357 .ns_id = app_ns->ns_id,
358 .is_add = 0,
359 };
360 vnet_app_namespace_add_del (&del);
361
362 vnet_app_namespace_add_del (&add);
363
364 /* keep the sw_if_index */
365 app_ns->sw_if_index = sw_if_index;
366
367 vec_free (add.ns_id);
368 vec_free (add.sock_name);
369 }
370 }
371}
372
Florin Coras61ae0562020-09-02 19:10:28 -0700373void
Florin Corascea194d2017-10-02 00:18:51 -0700374app_namespaces_init (void)
375{
376 u8 *ns_id = format (0, "default");
Florin Corasfc1c6122017-10-26 14:25:12 -0700377
Steven Luong2e67a3f2024-11-08 08:35:14 -0800378 VNET_SW_INTERFACE_TABLE_BIND_V4_CB (app_namespace_table_bind_v4);
379 VNET_SW_INTERFACE_TABLE_BIND_V6_CB (app_namespace_table_bind_v6);
380
Florin Corasfc1c6122017-10-26 14:25:12 -0700381 if (!app_namespace_lookup_table)
382 app_namespace_lookup_table =
383 hash_create_vec (0, sizeof (u8), sizeof (uword));
Florin Corascea194d2017-10-02 00:18:51 -0700384
385 /*
386 * Allocate default namespace
387 */
Florin Coras7cb471a2021-07-23 08:39:26 -0700388
389 /* clang-format off */
Florin Corascea194d2017-10-02 00:18:51 -0700390 vnet_app_namespace_add_del_args_t a = {
391 .ns_id = ns_id,
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200392 .sock_name = 0,
Florin Corascea194d2017-10-02 00:18:51 -0700393 .secret = 0,
394 .sw_if_index = APP_NAMESPACE_INVALID_INDEX,
395 .is_add = 1
396 };
Florin Coras7cb471a2021-07-23 08:39:26 -0700397 /* clang-format on */
398
Florin Corascea194d2017-10-02 00:18:51 -0700399 vnet_app_namespace_add_del (&a);
400 vec_free (ns_id);
401}
402
403static clib_error_t *
404app_ns_fn (vlib_main_t * vm, unformat_input_t * input,
405 vlib_cli_command_t * cmd)
406{
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200407 u8 is_add = 0, *ns_id = 0, secret_set = 0, sw_if_index_set = 0;
Steven Luong2e67a3f2024-11-08 08:35:14 -0800408 u8 fib6_id_set = 0, fib4_id_set = 0;
Nathan Skrzypczak51f1b262023-04-27 12:43:46 +0200409 u8 *sock_name = 0;
Dave Wallace8af20542017-10-26 03:29:30 -0400410 unformat_input_t _line_input, *line_input = &_line_input;
Steven Luong2e67a3f2024-11-08 08:35:14 -0800411 u32 sw_if_index = APP_NAMESPACE_INVALID_INDEX;
412 u32 fib4_id = ~0, fib6_id = ~0;
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200413 vnet_main_t *vnm = vnet_get_main ();
Florin Corascea194d2017-10-02 00:18:51 -0700414 u64 secret;
415 clib_error_t *error = 0;
Florin Corasc1a42652019-02-08 18:27:29 -0800416 int rv;
Florin Corascea194d2017-10-02 00:18:51 -0700417
418 session_cli_return_if_not_enabled ();
419
Dave Wallace8af20542017-10-26 03:29:30 -0400420 if (!unformat_user (input, unformat_line_input, line_input))
421 return 0;
422
423 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Florin Corascea194d2017-10-02 00:18:51 -0700424 {
Dave Wallace8af20542017-10-26 03:29:30 -0400425 if (unformat (line_input, "add"))
Florin Corascea194d2017-10-02 00:18:51 -0700426 is_add = 1;
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200427 else if (unformat (line_input, "del"))
428 is_add = 0;
Dave Wallace8af20542017-10-26 03:29:30 -0400429 else if (unformat (line_input, "id %_%v%_", &ns_id))
Florin Corascea194d2017-10-02 00:18:51 -0700430 ;
Dave Wallace8af20542017-10-26 03:29:30 -0400431 else if (unformat (line_input, "secret %lu", &secret))
Florin Corascea194d2017-10-02 00:18:51 -0700432 secret_set = 1;
Dave Wallace8af20542017-10-26 03:29:30 -0400433 else if (unformat (line_input, "sw_if_index %u", &sw_if_index))
Florin Corascea194d2017-10-02 00:18:51 -0700434 sw_if_index_set = 1;
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200435 else if (unformat (line_input, "if %U", unformat_vnet_sw_interface, vnm,
436 &sw_if_index))
437 sw_if_index_set = 1;
Steven Luong2e67a3f2024-11-08 08:35:14 -0800438 else if (unformat (line_input, "ip4-fib-id %u", &fib4_id))
439 fib4_id_set = 1;
440 else if (unformat (line_input, "ip6-fib-id %u", &fib6_id))
441 fib6_id_set = 1;
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200442 else if (unformat (line_input, "sock-name %_%v%_", &sock_name))
443 ;
Florin Corascea194d2017-10-02 00:18:51 -0700444 else
Dave Wallace8af20542017-10-26 03:29:30 -0400445 {
446 error = clib_error_return (0, "unknown input `%U'",
447 format_unformat_error, line_input);
Florin Coras7cb471a2021-07-23 08:39:26 -0700448 goto done;
Dave Wallace8af20542017-10-26 03:29:30 -0400449 }
Florin Corascea194d2017-10-02 00:18:51 -0700450 }
451
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200452 if (!ns_id)
Florin Corascea194d2017-10-02 00:18:51 -0700453 {
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200454 vlib_cli_output (vm, "namespace-id must be provided");
Florin Coras7cb471a2021-07-23 08:39:26 -0700455 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700456 }
457
Steven Luong2e67a3f2024-11-08 08:35:14 -0800458 if (is_add &&
459 (!secret_set || (!sw_if_index_set && !fib4_id_set && !fib6_id_set)))
Florin Corascea194d2017-10-02 00:18:51 -0700460 {
Steven Luong2e67a3f2024-11-08 08:35:14 -0800461 vlib_cli_output (vm, "secret and interface or fib-id must be provided");
462 goto done;
463 }
464
465 /* Validate the fib-id is valid */
466 if (fib4_id_set && (fib_table_find (FIB_PROTOCOL_IP4, fib4_id) == ~0))
467 {
468 vlib_cli_output (vm, "ip4-fib-id %u does not exist", fib4_id);
469 goto done;
470 }
471
472 if (fib6_id_set && (fib_table_find (FIB_PROTOCOL_IP6, fib6_id) == ~0))
473 {
474 vlib_cli_output (vm, "ip6-fib-id %u does not exist", fib6_id);
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200475 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700476 }
477
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200478 /* clang-format off */
479 vnet_app_namespace_add_del_args_t args = {
480 .ns_id = ns_id,
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200481 .secret = secret,
482 .sw_if_index = sw_if_index,
483 .sock_name = sock_name,
Steven Luong2e67a3f2024-11-08 08:35:14 -0800484 .ip4_fib_id = fib4_id,
485 .ip6_fib_id = fib6_id,
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200486 .is_add = is_add,
487 };
488 /* clang-format on */
489
490 if ((rv = vnet_app_namespace_add_del (&args)))
491 error = clib_error_return (0, "app namespace add del returned %d", rv);
492
Florin Coras7cb471a2021-07-23 08:39:26 -0700493done:
494
495 vec_free (ns_id);
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200496 vec_free (sock_name);
Florin Coras7cb471a2021-07-23 08:39:26 -0700497 unformat_free (line_input);
498
Florin Corascea194d2017-10-02 00:18:51 -0700499 return error;
500}
501
Florin Coras7cb471a2021-07-23 08:39:26 -0700502VLIB_CLI_COMMAND (app_ns_command, static) = {
Florin Corascea194d2017-10-02 00:18:51 -0700503 .path = "app ns",
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200504 .short_help = "app ns [add|del] id <namespace-id> secret <secret> "
Steven Luong2e67a3f2024-11-08 08:35:14 -0800505 "sw_if_index <sw_if_index> "
506 "{ip4-fib-id <id> ip6-fib-id <id> | if <interface>}",
Florin Corascea194d2017-10-02 00:18:51 -0700507 .function = app_ns_fn,
508};
Florin Corascea194d2017-10-02 00:18:51 -0700509
510u8 *
511format_app_namespace (u8 * s, va_list * args)
512{
513 app_namespace_t *app_ns = va_arg (*args, app_namespace_t *);
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200514 vnet_main_t *vnm = vnet_get_main ();
Florin Coras7cb471a2021-07-23 08:39:26 -0700515
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200516 s = format (s, "Application namespace [%u]\nid: %s\nsecret: %lu",
517 app_namespace_index (app_ns), app_ns->ns_id, app_ns->ns_secret);
518 if (app_ns->sw_if_index != (u32) ~0)
519 s = format (s, "\nInterface: %U", format_vnet_sw_if_index_name, vnm,
520 app_ns->sw_if_index);
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200521 if (app_ns->sock_name)
522 s = format (s, "\nSocket: %s", app_ns->sock_name);
523
Florin Corascea194d2017-10-02 00:18:51 -0700524 return s;
525}
526
Florin Coras61ae0562020-09-02 19:10:28 -0700527static void
528app_namespace_show_api (vlib_main_t * vm, app_namespace_t * app_ns)
529{
530 app_ns_api_handle_t *handle;
531 app_worker_t *app_wrk;
532 clib_socket_t *cs;
533 clib_file_t *cf;
534
535 if (!app_sapi_enabled)
536 {
537 vlib_cli_output (vm, "app socket api not enabled!");
538 return;
539 }
540
541 vlib_cli_output (vm, "socket: %v\n", app_ns->sock_name);
542
543 if (!pool_elts (app_ns->app_sockets))
544 return;
545
546 vlib_cli_output (vm, "%12s%12s%5s", "app index", "wrk index", "fd");
547
548
Damjan Marionb2c31b62020-12-13 21:47:40 +0100549 pool_foreach (cs, app_ns->app_sockets) {
Florin Coras61ae0562020-09-02 19:10:28 -0700550 handle = (app_ns_api_handle_t *) &cs->private_data;
551 cf = clib_file_get (&file_main, handle->aah_file_index);
552 if (handle->aah_app_wrk_index == APP_INVALID_INDEX)
553 {
554 vlib_cli_output (vm, "%12d%12d%5u", -1, -1, cf->file_descriptor);
555 continue;
556 }
557 app_wrk = app_worker_get (handle->aah_app_wrk_index);
558 vlib_cli_output (vm, "%12d%12d%5u", app_wrk->app_index,
559 app_wrk->wrk_map_index, cf->file_descriptor);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100560 }
Florin Coras61ae0562020-09-02 19:10:28 -0700561}
562
Florin Corascea194d2017-10-02 00:18:51 -0700563static clib_error_t *
Florin Corasdcdff6e2017-11-07 22:36:02 -0800564show_app_ns_fn (vlib_main_t * vm, unformat_input_t * main_input,
Florin Corascea194d2017-10-02 00:18:51 -0700565 vlib_cli_command_t * cmd)
566{
Florin Corasdcdff6e2017-11-07 22:36:02 -0800567 unformat_input_t _line_input, *line_input = &_line_input;
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200568 u8 *ns_id = 0, do_table = 0, had_input = 1, do_api = 0;
Florin Corascea194d2017-10-02 00:18:51 -0700569 app_namespace_t *app_ns;
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200570 vnet_main_t *vnm = vnet_get_main ();
Florin Corascea194d2017-10-02 00:18:51 -0700571 session_table_t *st;
Nathan Skrzypczak3d5e7412021-09-17 11:53:25 +0200572 table_t table = {}, *t = &table;
Florin Corascea194d2017-10-02 00:18:51 -0700573
574 session_cli_return_if_not_enabled ();
575
Florin Corasdcdff6e2017-11-07 22:36:02 -0800576 if (!unformat_user (main_input, unformat_line_input, line_input))
Florin Coras26408082017-11-09 02:06:07 -0800577 {
578 had_input = 0;
579 goto do_ns_list;
580 }
Florin Corasdcdff6e2017-11-07 22:36:02 -0800581
582 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Florin Corascea194d2017-10-02 00:18:51 -0700583 {
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200584 if (unformat (line_input, "id %_%v%_", &ns_id))
Florin Corascea194d2017-10-02 00:18:51 -0700585 do_table = 1;
Florin Coras61ae0562020-09-02 19:10:28 -0700586 else if (unformat (line_input, "api-clients"))
587 do_api = 1;
Florin Corascea194d2017-10-02 00:18:51 -0700588 else
Dave Wallace8af20542017-10-26 03:29:30 -0400589 {
Florin Corasdcdff6e2017-11-07 22:36:02 -0800590 vlib_cli_output (vm, "unknown input [%U]", format_unformat_error,
591 line_input);
592 goto done;
Dave Wallace8af20542017-10-26 03:29:30 -0400593 }
Florin Corascea194d2017-10-02 00:18:51 -0700594 }
595
Florin Coras61ae0562020-09-02 19:10:28 -0700596 if (do_api)
597 {
598 if (!do_table)
599 {
600 vlib_cli_output (vm, "must specify a table for api");
601 goto done;
602 }
603 app_ns = app_namespace_get_from_id (ns_id);
604 app_namespace_show_api (vm, app_ns);
605 goto done;
606 }
Florin Corascea194d2017-10-02 00:18:51 -0700607 if (do_table)
608 {
609 app_ns = app_namespace_get_from_id (ns_id);
610 if (!app_ns)
611 {
612 vlib_cli_output (vm, "ns %v not found", ns_id);
Florin Corasdcdff6e2017-11-07 22:36:02 -0800613 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700614 }
615 st = session_table_get (app_ns->local_table_index);
616 if (!st)
617 {
618 vlib_cli_output (vm, "table for ns %v could not be found", ns_id);
Florin Corasdcdff6e2017-11-07 22:36:02 -0800619 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700620 }
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200621 vlib_cli_output (vm, "%U", format_app_namespace, app_ns);
Florin Corascea194d2017-10-02 00:18:51 -0700622 session_lookup_show_table_entries (vm, st, 0, 1);
623 vec_free (ns_id);
Florin Corasdcdff6e2017-11-07 22:36:02 -0800624 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700625 }
626
Florin Coras26408082017-11-09 02:06:07 -0800627do_ns_list:
Nathan Skrzypczak51f1b262023-04-27 12:43:46 +0200628 table_add_header_col (t, 5, "Index", "Secret", "Interface", "Id", "Socket");
Nathan Skrzypczak3d5e7412021-09-17 11:53:25 +0200629 int i = 0;
630 pool_foreach (app_ns, app_namespace_pool)
631 {
632 int j = 0;
633 table_format_cell (t, i, j++, "%u", app_namespace_index (app_ns));
634 table_format_cell (t, i, j++, "%lu", app_ns->ns_secret);
635 table_format_cell (t, i, j++, "%U", format_vnet_sw_if_index_name, vnm,
636 app_ns->sw_if_index);
637 table_format_cell (t, i, j++, "%s", app_ns->ns_id);
Nathan Skrzypczak3d5e7412021-09-17 11:53:25 +0200638 table_format_cell (t, i++, j++, "%s", app_ns->sock_name);
639 }
Florin Corasdff48db2017-11-19 18:06:58 -0800640
Nathan Skrzypczak3d5e7412021-09-17 11:53:25 +0200641 t->default_body.align = TTAA_LEFT;
642 t->default_header_col.align = TTAA_LEFT;
643 t->default_header_col.fg_color = TTAC_YELLOW;
644 t->default_header_col.flags = TTAF_FG_COLOR_SET;
645 vlib_cli_output (vm, "%U", format_table, t);
646 table_free (t);
Florin Corascea194d2017-10-02 00:18:51 -0700647
Florin Corasdcdff6e2017-11-07 22:36:02 -0800648done:
Florin Coras26408082017-11-09 02:06:07 -0800649 if (had_input)
650 unformat_free (line_input);
Florin Corascea194d2017-10-02 00:18:51 -0700651 return 0;
652}
653
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200654VLIB_CLI_COMMAND (show_app_ns_command, static) = {
Florin Corascea194d2017-10-02 00:18:51 -0700655 .path = "show app ns",
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200656 .short_help = "show app ns [id <id> [api-clients]]",
Florin Corascea194d2017-10-02 00:18:51 -0700657 .function = show_app_ns_fn,
658};
Florin Corascea194d2017-10-02 00:18:51 -0700659
660/*
661 * fd.io coding-style-patch-verification: ON
662 *
663 * Local Variables:
664 * eval: (c-set-style "gnu")
665 * End:
666 */