blob: 2520188d88c3032ebb054b91634a3449049fc372 [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
Steven Luong67bae202024-07-08 11:21:23 -070025/*
26 * fib source when locking the fib table
27 */
28static fib_source_t app_namespace_fib_src = FIB_SOURCE_INVALID;
29static u32 *fib_index_to_lock_count[FIB_PROTOCOL_IP6 + 1];
30
Florin Corascea194d2017-10-02 00:18:51 -070031/**
32 * Hash table of application namespaces by app ns ids
33 */
34uword *app_namespace_lookup_table;
35
36/**
37 * Pool of application namespaces
38 */
39static app_namespace_t *app_namespace_pool;
40
Florin Coras61ae0562020-09-02 19:10:28 -070041static u8 app_sapi_enabled;
42
Florin Corascea194d2017-10-02 00:18:51 -070043app_namespace_t *
44app_namespace_get (u32 index)
45{
46 return pool_elt_at_index (app_namespace_pool, index);
47}
48
49app_namespace_t *
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +020050app_namespace_get_from_id (const u8 *ns_id)
Florin Corascea194d2017-10-02 00:18:51 -070051{
52 u32 index = app_namespace_index_from_id (ns_id);
53 if (index == APP_NAMESPACE_INVALID_INDEX)
54 return 0;
55 return app_namespace_get (index);
56}
57
58u32
59app_namespace_index (app_namespace_t * app_ns)
60{
61 return (app_ns - app_namespace_pool);
62}
63
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +020064void
65app_namespace_free (app_namespace_t *app_ns)
66{
67 hash_unset_mem (app_namespace_lookup_table, app_ns->ns_id);
68 vec_free (app_ns->ns_id);
69
70 pool_put (app_namespace_pool, app_ns);
71}
72
Florin Corascea194d2017-10-02 00:18:51 -070073app_namespace_t *
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +020074app_namespace_alloc (const u8 *ns_id)
Florin Corascea194d2017-10-02 00:18:51 -070075{
76 app_namespace_t *app_ns;
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +020077
Florin Corascea194d2017-10-02 00:18:51 -070078 pool_get (app_namespace_pool, app_ns);
Dave Barachb7b92992018-10-17 10:38:51 -040079 clib_memset (app_ns, 0, sizeof (*app_ns));
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +020080
81 app_ns->ns_id = vec_dup ((u8 *) ns_id);
82 vec_terminate_c_string (app_ns->ns_id);
83
Florin Corascea194d2017-10-02 00:18:51 -070084 hash_set_mem (app_namespace_lookup_table, app_ns->ns_id,
85 app_ns - app_namespace_pool);
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +020086
Florin Corascea194d2017-10-02 00:18:51 -070087 return app_ns;
88}
89
Steven Luong67bae202024-07-08 11:21:23 -070090static void
91app_namespace_fib_table_lock (u32 fib_index, u32 protocol)
92{
93 fib_table_lock (fib_index, protocol, app_namespace_fib_src);
94 vec_validate (fib_index_to_lock_count[protocol], fib_index);
95 fib_index_to_lock_count[protocol][fib_index]++;
96 ASSERT (fib_index_to_lock_count[protocol][fib_index] > 0);
97}
98
99static void
100app_namespace_fib_table_unlock (u32 fib_index, u32 protocol)
101{
102 fib_table_unlock (fib_index, protocol, app_namespace_fib_src);
103 ASSERT (fib_index_to_lock_count[protocol][fib_index] > 0);
104 fib_index_to_lock_count[protocol][fib_index]--;
105}
106
107static void
108app_namespace_del_global_table (app_namespace_t *app_ns)
109{
110 session_table_t *st;
111 u32 table_index;
112
113 app_namespace_fib_table_unlock (app_ns->ip4_fib_index, FIB_PROTOCOL_IP4);
114 if (fib_index_to_lock_count[FIB_PROTOCOL_IP4][app_ns->ip4_fib_index] == 0)
115 {
116 table_index = session_lookup_get_index_for_fib (FIB_PROTOCOL_IP4,
117 app_ns->ip4_fib_index);
118 st = session_table_get (table_index);
119 if (st)
120 session_table_free (st, FIB_PROTOCOL_IP4);
121 }
122
123 app_namespace_fib_table_unlock (app_ns->ip6_fib_index, FIB_PROTOCOL_IP6);
124 if (fib_index_to_lock_count[FIB_PROTOCOL_IP6][app_ns->ip6_fib_index] == 0)
125 {
126 table_index = session_lookup_get_index_for_fib (FIB_PROTOCOL_IP6,
127 app_ns->ip6_fib_index);
128 st = session_table_get (table_index);
129 if (st)
130 session_table_free (st, FIB_PROTOCOL_IP6);
131 }
132}
133
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200134session_error_t
135vnet_app_namespace_add_del (vnet_app_namespace_add_del_args_t *a)
Florin Corascea194d2017-10-02 00:18:51 -0700136{
137 app_namespace_t *app_ns;
138 session_table_t *st;
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200139 u32 ns_index;
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200140 session_error_t rv;
Florin Corascea194d2017-10-02 00:18:51 -0700141
142 if (a->is_add)
143 {
144 if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX
Dave Barach3940de32019-07-23 16:28:36 -0400145 && !vnet_get_sw_interface_or_null (vnet_get_main (),
146 a->sw_if_index))
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200147 return SESSION_E_INVALID;
Florin Corascea194d2017-10-02 00:18:51 -0700148
149 if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX)
150 {
151 a->ip4_fib_id =
152 fib_table_get_table_id_for_sw_if_index (FIB_PROTOCOL_IP4,
153 a->sw_if_index);
154 a->ip6_fib_id =
Florin Coras56b39f62018-03-27 17:29:32 -0700155 fib_table_get_table_id_for_sw_if_index (FIB_PROTOCOL_IP6,
Florin Corascea194d2017-10-02 00:18:51 -0700156 a->sw_if_index);
157 }
158 if (a->sw_if_index == APP_NAMESPACE_INVALID_INDEX
159 && a->ip4_fib_id == APP_NAMESPACE_INVALID_INDEX)
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200160 return SESSION_E_INVALID;
Florin Corasc1a42652019-02-08 18:27:29 -0800161
Florin Corascea194d2017-10-02 00:18:51 -0700162 app_ns = app_namespace_get_from_id (a->ns_id);
163 if (!app_ns)
Florin Corasfc1c6122017-10-26 14:25:12 -0700164 {
165 app_ns = app_namespace_alloc (a->ns_id);
166 st = session_table_alloc ();
Florin Coras6c36f532017-11-03 18:32:34 -0700167 session_table_init (st, FIB_PROTOCOL_MAX);
168 st->is_local = 1;
169 st->appns_index = app_namespace_index (app_ns);
Florin Corasfc1c6122017-10-26 14:25:12 -0700170 app_ns->local_table_index = session_table_index (st);
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200171 if (a->sock_name)
172 {
173 app_ns->sock_name = vec_dup (a->sock_name);
174 vec_terminate_c_string (app_ns->sock_name);
175 }
176
177 /* Add socket for namespace,
178 * only at creation time */
179 if (app_sapi_enabled)
180 {
181 rv = appns_sapi_add_ns_socket (app_ns);
182 if (rv)
183 return rv;
184 }
Florin Corasfc1c6122017-10-26 14:25:12 -0700185 }
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200186
Florin Corascea194d2017-10-02 00:18:51 -0700187 app_ns->ns_secret = a->secret;
188 app_ns->sw_if_index = a->sw_if_index;
Florin Coras61ae0562020-09-02 19:10:28 -0700189
Steven Luong67bae202024-07-08 11:21:23 -0700190 app_ns->ip4_fib_index = fib_table_find (FIB_PROTOCOL_IP4, a->ip4_fib_id);
191 app_namespace_fib_table_lock (app_ns->ip4_fib_index, FIB_PROTOCOL_IP4);
192
193 app_ns->ip6_fib_index = fib_table_find (FIB_PROTOCOL_IP6, a->ip6_fib_id);
194 app_namespace_fib_table_lock (app_ns->ip6_fib_index, FIB_PROTOCOL_IP6);
195
196 session_lookup_set_tables_appns (app_ns);
Florin Corascea194d2017-10-02 00:18:51 -0700197 }
198 else
199 {
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200200 ns_index = app_namespace_index_from_id (a->ns_id);
201 if (ns_index == APP_NAMESPACE_INVALID_INDEX)
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200202 return SESSION_E_INVALID;
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200203
204 app_ns = app_namespace_get (ns_index);
205 if (!app_ns)
Filip Tehlar0028e6f2023-06-28 10:47:32 +0200206 return SESSION_E_INVALID;
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200207
208 application_namespace_cleanup (app_ns);
209
210 if (app_sapi_enabled)
211 appns_sapi_del_ns_socket (app_ns);
212
213 st = session_table_get (app_ns->local_table_index);
214
215 session_table_free (st, FIB_PROTOCOL_MAX);
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200216 if (app_ns->sock_name)
217 vec_free (app_ns->sock_name);
218
Steven Luong67bae202024-07-08 11:21:23 -0700219 app_namespace_del_global_table (app_ns);
220
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200221 app_namespace_free (app_ns);
Florin Corascea194d2017-10-02 00:18:51 -0700222 }
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200223
Florin Corascea194d2017-10-02 00:18:51 -0700224 return 0;
225}
226
227const u8 *
228app_namespace_id (app_namespace_t * app_ns)
229{
230 return app_ns->ns_id;
231}
232
233u32
234app_namespace_index_from_id (const u8 * ns_id)
235{
236 uword *indexp;
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200237 u8 *key;
238
239 key = vec_dup ((u8 *) ns_id);
240 vec_terminate_c_string (key);
241
242 indexp = hash_get_mem (app_namespace_lookup_table, key);
243 vec_free (key);
Florin Corascea194d2017-10-02 00:18:51 -0700244 if (!indexp)
245 return APP_NAMESPACE_INVALID_INDEX;
246 return *indexp;
247}
248
249const u8 *
250app_namespace_id_from_index (u32 index)
251{
252 app_namespace_t *app_ns;
253
254 app_ns = app_namespace_get (index);
255 return app_namespace_id (app_ns);
256}
257
Florin Coras1c710452017-10-17 00:03:13 -0700258u32
259app_namespace_get_fib_index (app_namespace_t * app_ns, u8 fib_proto)
260{
261 return fib_proto == FIB_PROTOCOL_IP4 ?
262 app_ns->ip4_fib_index : app_ns->ip6_fib_index;
263}
264
265session_table_t *
266app_namespace_get_local_table (app_namespace_t * app_ns)
267{
268 return session_table_get (app_ns->local_table_index);
269}
270
Nathan Skrzypczak7b3a3df2021-07-28 14:09:50 +0200271int
272appns_sapi_enable_disable (int is_enable)
Florin Coras61ae0562020-09-02 19:10:28 -0700273{
Nathan Skrzypczak7b3a3df2021-07-28 14:09:50 +0200274 /* This cannot be called with active sockets */
275 if (pool_elts (app_namespace_pool))
276 return -1;
277
278 app_sapi_enabled = is_enable;
279 return 0;
Florin Coras61ae0562020-09-02 19:10:28 -0700280}
281
282u8
283appns_sapi_enabled (void)
284{
285 return app_sapi_enabled;
286}
287
288void
Florin Corascea194d2017-10-02 00:18:51 -0700289app_namespaces_init (void)
290{
291 u8 *ns_id = format (0, "default");
Florin Corasfc1c6122017-10-26 14:25:12 -0700292
Steven Luong67bae202024-07-08 11:21:23 -0700293 /* We are not contributing any route to the fib. But we allocate a fib source
294 * so that when we lock the fib table, we can view that we have a lock on the
295 * particular fib table in case we wonder why the fib table is not free after
296 * "ip table del"
297 */
298 if (app_namespace_fib_src == FIB_SOURCE_INVALID)
299 app_namespace_fib_src = fib_source_allocate (
300 "application namespace", FIB_SOURCE_PRIORITY_LOW, FIB_SOURCE_BH_SIMPLE);
301
Florin Corasfc1c6122017-10-26 14:25:12 -0700302 if (!app_namespace_lookup_table)
303 app_namespace_lookup_table =
304 hash_create_vec (0, sizeof (u8), sizeof (uword));
Florin Corascea194d2017-10-02 00:18:51 -0700305
306 /*
307 * Allocate default namespace
308 */
Florin Coras7cb471a2021-07-23 08:39:26 -0700309
310 /* clang-format off */
Florin Corascea194d2017-10-02 00:18:51 -0700311 vnet_app_namespace_add_del_args_t a = {
312 .ns_id = ns_id,
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200313 .sock_name = 0,
Florin Corascea194d2017-10-02 00:18:51 -0700314 .secret = 0,
315 .sw_if_index = APP_NAMESPACE_INVALID_INDEX,
316 .is_add = 1
317 };
Florin Coras7cb471a2021-07-23 08:39:26 -0700318 /* clang-format on */
319
Florin Corascea194d2017-10-02 00:18:51 -0700320 vnet_app_namespace_add_del (&a);
321 vec_free (ns_id);
322}
323
324static clib_error_t *
325app_ns_fn (vlib_main_t * vm, unformat_input_t * input,
326 vlib_cli_command_t * cmd)
327{
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200328 u8 is_add = 0, *ns_id = 0, secret_set = 0, sw_if_index_set = 0;
Nathan Skrzypczak51f1b262023-04-27 12:43:46 +0200329 u8 *sock_name = 0;
Dave Wallace8af20542017-10-26 03:29:30 -0400330 unformat_input_t _line_input, *line_input = &_line_input;
Florin Corascea194d2017-10-02 00:18:51 -0700331 u32 sw_if_index, fib_id = APP_NAMESPACE_INVALID_INDEX;
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200332 vnet_main_t *vnm = vnet_get_main ();
Florin Corascea194d2017-10-02 00:18:51 -0700333 u64 secret;
334 clib_error_t *error = 0;
Florin Corasc1a42652019-02-08 18:27:29 -0800335 int rv;
Florin Corascea194d2017-10-02 00:18:51 -0700336
337 session_cli_return_if_not_enabled ();
338
Dave Wallace8af20542017-10-26 03:29:30 -0400339 if (!unformat_user (input, unformat_line_input, line_input))
340 return 0;
341
342 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Florin Corascea194d2017-10-02 00:18:51 -0700343 {
Dave Wallace8af20542017-10-26 03:29:30 -0400344 if (unformat (line_input, "add"))
Florin Corascea194d2017-10-02 00:18:51 -0700345 is_add = 1;
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200346 else if (unformat (line_input, "del"))
347 is_add = 0;
Dave Wallace8af20542017-10-26 03:29:30 -0400348 else if (unformat (line_input, "id %_%v%_", &ns_id))
Florin Corascea194d2017-10-02 00:18:51 -0700349 ;
Dave Wallace8af20542017-10-26 03:29:30 -0400350 else if (unformat (line_input, "secret %lu", &secret))
Florin Corascea194d2017-10-02 00:18:51 -0700351 secret_set = 1;
Dave Wallace8af20542017-10-26 03:29:30 -0400352 else if (unformat (line_input, "sw_if_index %u", &sw_if_index))
Florin Corascea194d2017-10-02 00:18:51 -0700353 sw_if_index_set = 1;
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200354 else if (unformat (line_input, "if %U", unformat_vnet_sw_interface, vnm,
355 &sw_if_index))
356 sw_if_index_set = 1;
Dave Wallace8af20542017-10-26 03:29:30 -0400357 else if (unformat (line_input, "fib_id", &fib_id))
Florin Corascea194d2017-10-02 00:18:51 -0700358 ;
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200359 else if (unformat (line_input, "sock-name %_%v%_", &sock_name))
360 ;
Florin Corascea194d2017-10-02 00:18:51 -0700361 else
Dave Wallace8af20542017-10-26 03:29:30 -0400362 {
363 error = clib_error_return (0, "unknown input `%U'",
364 format_unformat_error, line_input);
Florin Coras7cb471a2021-07-23 08:39:26 -0700365 goto done;
Dave Wallace8af20542017-10-26 03:29:30 -0400366 }
Florin Corascea194d2017-10-02 00:18:51 -0700367 }
368
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200369 if (!ns_id)
Florin Corascea194d2017-10-02 00:18:51 -0700370 {
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200371 vlib_cli_output (vm, "namespace-id must be provided");
Florin Coras7cb471a2021-07-23 08:39:26 -0700372 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700373 }
374
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200375 if (is_add && (!secret_set || !sw_if_index_set))
Florin Corascea194d2017-10-02 00:18:51 -0700376 {
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200377 vlib_cli_output (vm, "secret and interface must be provided");
378 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700379 }
380
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200381 /* clang-format off */
382 vnet_app_namespace_add_del_args_t args = {
383 .ns_id = ns_id,
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200384 .secret = secret,
385 .sw_if_index = sw_if_index,
386 .sock_name = sock_name,
387 .ip4_fib_id = fib_id,
388 .is_add = is_add,
389 };
390 /* clang-format on */
391
392 if ((rv = vnet_app_namespace_add_del (&args)))
393 error = clib_error_return (0, "app namespace add del returned %d", rv);
394
Florin Coras7cb471a2021-07-23 08:39:26 -0700395done:
396
397 vec_free (ns_id);
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200398 vec_free (sock_name);
Florin Coras7cb471a2021-07-23 08:39:26 -0700399 unformat_free (line_input);
400
Florin Corascea194d2017-10-02 00:18:51 -0700401 return error;
402}
403
Florin Coras7cb471a2021-07-23 08:39:26 -0700404VLIB_CLI_COMMAND (app_ns_command, static) = {
Florin Corascea194d2017-10-02 00:18:51 -0700405 .path = "app ns",
Nathan Skrzypczakb3ea73e2021-08-05 10:22:52 +0200406 .short_help = "app ns [add|del] id <namespace-id> secret <secret> "
Nathan Skrzypczak51f1b262023-04-27 12:43:46 +0200407 "sw_if_index <sw_if_index> if <interface>",
Florin Corascea194d2017-10-02 00:18:51 -0700408 .function = app_ns_fn,
409};
Florin Corascea194d2017-10-02 00:18:51 -0700410
411u8 *
412format_app_namespace (u8 * s, va_list * args)
413{
414 app_namespace_t *app_ns = va_arg (*args, app_namespace_t *);
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200415 vnet_main_t *vnm = vnet_get_main ();
Florin Coras7cb471a2021-07-23 08:39:26 -0700416
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200417 s = format (s, "Application namespace [%u]\nid: %s\nsecret: %lu",
418 app_namespace_index (app_ns), app_ns->ns_id, app_ns->ns_secret);
419 if (app_ns->sw_if_index != (u32) ~0)
420 s = format (s, "\nInterface: %U", format_vnet_sw_if_index_name, vnm,
421 app_ns->sw_if_index);
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200422 if (app_ns->sock_name)
423 s = format (s, "\nSocket: %s", app_ns->sock_name);
424
Florin Corascea194d2017-10-02 00:18:51 -0700425 return s;
426}
427
Florin Coras61ae0562020-09-02 19:10:28 -0700428static void
429app_namespace_show_api (vlib_main_t * vm, app_namespace_t * app_ns)
430{
431 app_ns_api_handle_t *handle;
432 app_worker_t *app_wrk;
433 clib_socket_t *cs;
434 clib_file_t *cf;
435
436 if (!app_sapi_enabled)
437 {
438 vlib_cli_output (vm, "app socket api not enabled!");
439 return;
440 }
441
442 vlib_cli_output (vm, "socket: %v\n", app_ns->sock_name);
443
444 if (!pool_elts (app_ns->app_sockets))
445 return;
446
447 vlib_cli_output (vm, "%12s%12s%5s", "app index", "wrk index", "fd");
448
449
Damjan Marionb2c31b62020-12-13 21:47:40 +0100450 pool_foreach (cs, app_ns->app_sockets) {
Florin Coras61ae0562020-09-02 19:10:28 -0700451 handle = (app_ns_api_handle_t *) &cs->private_data;
452 cf = clib_file_get (&file_main, handle->aah_file_index);
453 if (handle->aah_app_wrk_index == APP_INVALID_INDEX)
454 {
455 vlib_cli_output (vm, "%12d%12d%5u", -1, -1, cf->file_descriptor);
456 continue;
457 }
458 app_wrk = app_worker_get (handle->aah_app_wrk_index);
459 vlib_cli_output (vm, "%12d%12d%5u", app_wrk->app_index,
460 app_wrk->wrk_map_index, cf->file_descriptor);
Damjan Marionb2c31b62020-12-13 21:47:40 +0100461 }
Florin Coras61ae0562020-09-02 19:10:28 -0700462}
463
Florin Corascea194d2017-10-02 00:18:51 -0700464static clib_error_t *
Florin Corasdcdff6e2017-11-07 22:36:02 -0800465show_app_ns_fn (vlib_main_t * vm, unformat_input_t * main_input,
Florin Corascea194d2017-10-02 00:18:51 -0700466 vlib_cli_command_t * cmd)
467{
Florin Corasdcdff6e2017-11-07 22:36:02 -0800468 unformat_input_t _line_input, *line_input = &_line_input;
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200469 u8 *ns_id = 0, do_table = 0, had_input = 1, do_api = 0;
Florin Corascea194d2017-10-02 00:18:51 -0700470 app_namespace_t *app_ns;
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200471 vnet_main_t *vnm = vnet_get_main ();
Florin Corascea194d2017-10-02 00:18:51 -0700472 session_table_t *st;
Nathan Skrzypczak3d5e7412021-09-17 11:53:25 +0200473 table_t table = {}, *t = &table;
Florin Corascea194d2017-10-02 00:18:51 -0700474
475 session_cli_return_if_not_enabled ();
476
Florin Corasdcdff6e2017-11-07 22:36:02 -0800477 if (!unformat_user (main_input, unformat_line_input, line_input))
Florin Coras26408082017-11-09 02:06:07 -0800478 {
479 had_input = 0;
480 goto do_ns_list;
481 }
Florin Corasdcdff6e2017-11-07 22:36:02 -0800482
483 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
Florin Corascea194d2017-10-02 00:18:51 -0700484 {
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200485 if (unformat (line_input, "id %_%v%_", &ns_id))
Florin Corascea194d2017-10-02 00:18:51 -0700486 do_table = 1;
Florin Coras61ae0562020-09-02 19:10:28 -0700487 else if (unformat (line_input, "api-clients"))
488 do_api = 1;
Florin Corascea194d2017-10-02 00:18:51 -0700489 else
Dave Wallace8af20542017-10-26 03:29:30 -0400490 {
Florin Corasdcdff6e2017-11-07 22:36:02 -0800491 vlib_cli_output (vm, "unknown input [%U]", format_unformat_error,
492 line_input);
493 goto done;
Dave Wallace8af20542017-10-26 03:29:30 -0400494 }
Florin Corascea194d2017-10-02 00:18:51 -0700495 }
496
Florin Coras61ae0562020-09-02 19:10:28 -0700497 if (do_api)
498 {
499 if (!do_table)
500 {
501 vlib_cli_output (vm, "must specify a table for api");
502 goto done;
503 }
504 app_ns = app_namespace_get_from_id (ns_id);
505 app_namespace_show_api (vm, app_ns);
506 goto done;
507 }
Florin Corascea194d2017-10-02 00:18:51 -0700508 if (do_table)
509 {
510 app_ns = app_namespace_get_from_id (ns_id);
511 if (!app_ns)
512 {
513 vlib_cli_output (vm, "ns %v not found", ns_id);
Florin Corasdcdff6e2017-11-07 22:36:02 -0800514 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700515 }
516 st = session_table_get (app_ns->local_table_index);
517 if (!st)
518 {
519 vlib_cli_output (vm, "table for ns %v could not be found", ns_id);
Florin Corasdcdff6e2017-11-07 22:36:02 -0800520 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700521 }
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200522 vlib_cli_output (vm, "%U", format_app_namespace, app_ns);
Florin Corascea194d2017-10-02 00:18:51 -0700523 session_lookup_show_table_entries (vm, st, 0, 1);
524 vec_free (ns_id);
Florin Corasdcdff6e2017-11-07 22:36:02 -0800525 goto done;
Florin Corascea194d2017-10-02 00:18:51 -0700526 }
527
Florin Coras26408082017-11-09 02:06:07 -0800528do_ns_list:
Nathan Skrzypczak51f1b262023-04-27 12:43:46 +0200529 table_add_header_col (t, 5, "Index", "Secret", "Interface", "Id", "Socket");
Nathan Skrzypczak3d5e7412021-09-17 11:53:25 +0200530 int i = 0;
531 pool_foreach (app_ns, app_namespace_pool)
532 {
533 int j = 0;
534 table_format_cell (t, i, j++, "%u", app_namespace_index (app_ns));
535 table_format_cell (t, i, j++, "%lu", app_ns->ns_secret);
536 table_format_cell (t, i, j++, "%U", format_vnet_sw_if_index_name, vnm,
537 app_ns->sw_if_index);
538 table_format_cell (t, i, j++, "%s", app_ns->ns_id);
Nathan Skrzypczak3d5e7412021-09-17 11:53:25 +0200539 table_format_cell (t, i++, j++, "%s", app_ns->sock_name);
540 }
Florin Corasdff48db2017-11-19 18:06:58 -0800541
Nathan Skrzypczak3d5e7412021-09-17 11:53:25 +0200542 t->default_body.align = TTAA_LEFT;
543 t->default_header_col.align = TTAA_LEFT;
544 t->default_header_col.fg_color = TTAC_YELLOW;
545 t->default_header_col.flags = TTAF_FG_COLOR_SET;
546 vlib_cli_output (vm, "%U", format_table, t);
547 table_free (t);
Florin Corascea194d2017-10-02 00:18:51 -0700548
Florin Corasdcdff6e2017-11-07 22:36:02 -0800549done:
Florin Coras26408082017-11-09 02:06:07 -0800550 if (had_input)
551 unformat_free (line_input);
Florin Corascea194d2017-10-02 00:18:51 -0700552 return 0;
553}
554
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200555VLIB_CLI_COMMAND (show_app_ns_command, static) = {
Florin Corascea194d2017-10-02 00:18:51 -0700556 .path = "show app ns",
Nathan Skrzypczak1a9e2f92021-07-28 19:35:08 +0200557 .short_help = "show app ns [id <id> [api-clients]]",
Florin Corascea194d2017-10-02 00:18:51 -0700558 .function = show_app_ns_fn,
559};
Florin Corascea194d2017-10-02 00:18:51 -0700560
561/*
562 * fd.io coding-style-patch-verification: ON
563 *
564 * Local Variables:
565 * eval: (c-set-style "gnu")
566 * End:
567 */