Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1 | /* |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 Cisco and/or its affiliates. |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 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> |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 17 | #include <vnet/session/application.h> |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 18 | #include <vnet/session/session_table.h> |
| 19 | #include <vnet/session/session.h> |
| 20 | #include <vnet/fib/fib_table.h> |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 21 | #include <vppinfra/file.h> |
Nathan Skrzypczak | 3d5e741 | 2021-09-17 11:53:25 +0200 | [diff] [blame] | 22 | #include <vppinfra/format_table.h> |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 23 | #include <vlib/unix/unix.h> |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 24 | |
Steven Luong | 67bae20 | 2024-07-08 11:21:23 -0700 | [diff] [blame] | 25 | /* |
| 26 | * fib source when locking the fib table |
| 27 | */ |
| 28 | static fib_source_t app_namespace_fib_src = FIB_SOURCE_INVALID; |
| 29 | static u32 *fib_index_to_lock_count[FIB_PROTOCOL_IP6 + 1]; |
| 30 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Hash table of application namespaces by app ns ids |
| 33 | */ |
| 34 | uword *app_namespace_lookup_table; |
| 35 | |
| 36 | /** |
| 37 | * Pool of application namespaces |
| 38 | */ |
| 39 | static app_namespace_t *app_namespace_pool; |
| 40 | |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 41 | static u8 app_sapi_enabled; |
| 42 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 43 | app_namespace_t * |
| 44 | app_namespace_get (u32 index) |
| 45 | { |
| 46 | return pool_elt_at_index (app_namespace_pool, index); |
| 47 | } |
| 48 | |
| 49 | app_namespace_t * |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 50 | app_namespace_get_from_id (const u8 *ns_id) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 51 | { |
| 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 | |
| 58 | u32 |
| 59 | app_namespace_index (app_namespace_t * app_ns) |
| 60 | { |
| 61 | return (app_ns - app_namespace_pool); |
| 62 | } |
| 63 | |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 64 | void |
| 65 | app_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 Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 73 | app_namespace_t * |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 74 | app_namespace_alloc (const u8 *ns_id) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 75 | { |
| 76 | app_namespace_t *app_ns; |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 77 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 78 | pool_get (app_namespace_pool, app_ns); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 79 | clib_memset (app_ns, 0, sizeof (*app_ns)); |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 80 | |
| 81 | app_ns->ns_id = vec_dup ((u8 *) ns_id); |
| 82 | vec_terminate_c_string (app_ns->ns_id); |
| 83 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 84 | hash_set_mem (app_namespace_lookup_table, app_ns->ns_id, |
| 85 | app_ns - app_namespace_pool); |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 86 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 87 | return app_ns; |
| 88 | } |
| 89 | |
Steven Luong | 67bae20 | 2024-07-08 11:21:23 -0700 | [diff] [blame] | 90 | static void |
| 91 | app_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 | |
| 99 | static void |
| 100 | app_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 | |
| 107 | static void |
| 108 | app_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 Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 134 | session_error_t |
| 135 | vnet_app_namespace_add_del (vnet_app_namespace_add_del_args_t *a) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 136 | { |
| 137 | app_namespace_t *app_ns; |
| 138 | session_table_t *st; |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 139 | u32 ns_index; |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 140 | session_error_t rv; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 141 | |
| 142 | if (a->is_add) |
| 143 | { |
| 144 | if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 145 | && !vnet_get_sw_interface_or_null (vnet_get_main (), |
| 146 | a->sw_if_index)) |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 147 | return SESSION_E_INVALID; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 148 | |
| 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 Coras | 56b39f6 | 2018-03-27 17:29:32 -0700 | [diff] [blame] | 155 | fib_table_get_table_id_for_sw_if_index (FIB_PROTOCOL_IP6, |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 156 | 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 Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 160 | return SESSION_E_INVALID; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 161 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 162 | app_ns = app_namespace_get_from_id (a->ns_id); |
| 163 | if (!app_ns) |
Florin Coras | fc1c612 | 2017-10-26 14:25:12 -0700 | [diff] [blame] | 164 | { |
| 165 | app_ns = app_namespace_alloc (a->ns_id); |
| 166 | st = session_table_alloc (); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 167 | session_table_init (st, FIB_PROTOCOL_MAX); |
| 168 | st->is_local = 1; |
| 169 | st->appns_index = app_namespace_index (app_ns); |
Florin Coras | fc1c612 | 2017-10-26 14:25:12 -0700 | [diff] [blame] | 170 | app_ns->local_table_index = session_table_index (st); |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 171 | 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 Coras | fc1c612 | 2017-10-26 14:25:12 -0700 | [diff] [blame] | 185 | } |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 186 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 187 | app_ns->ns_secret = a->secret; |
| 188 | app_ns->sw_if_index = a->sw_if_index; |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 189 | |
Steven Luong | 67bae20 | 2024-07-08 11:21:23 -0700 | [diff] [blame] | 190 | 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 Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 197 | } |
| 198 | else |
| 199 | { |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 200 | ns_index = app_namespace_index_from_id (a->ns_id); |
| 201 | if (ns_index == APP_NAMESPACE_INVALID_INDEX) |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 202 | return SESSION_E_INVALID; |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 203 | |
| 204 | app_ns = app_namespace_get (ns_index); |
| 205 | if (!app_ns) |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 206 | return SESSION_E_INVALID; |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 207 | |
| 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 Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 216 | if (app_ns->sock_name) |
| 217 | vec_free (app_ns->sock_name); |
| 218 | |
Steven Luong | 67bae20 | 2024-07-08 11:21:23 -0700 | [diff] [blame] | 219 | app_namespace_del_global_table (app_ns); |
| 220 | |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 221 | app_namespace_free (app_ns); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 222 | } |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 223 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | const u8 * |
| 228 | app_namespace_id (app_namespace_t * app_ns) |
| 229 | { |
| 230 | return app_ns->ns_id; |
| 231 | } |
| 232 | |
| 233 | u32 |
| 234 | app_namespace_index_from_id (const u8 * ns_id) |
| 235 | { |
| 236 | uword *indexp; |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 237 | 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 Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 244 | if (!indexp) |
| 245 | return APP_NAMESPACE_INVALID_INDEX; |
| 246 | return *indexp; |
| 247 | } |
| 248 | |
| 249 | const u8 * |
| 250 | app_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 Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 258 | u32 |
| 259 | app_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 | |
| 265 | session_table_t * |
| 266 | app_namespace_get_local_table (app_namespace_t * app_ns) |
| 267 | { |
| 268 | return session_table_get (app_ns->local_table_index); |
| 269 | } |
| 270 | |
Nathan Skrzypczak | 7b3a3df | 2021-07-28 14:09:50 +0200 | [diff] [blame] | 271 | int |
| 272 | appns_sapi_enable_disable (int is_enable) |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 273 | { |
Nathan Skrzypczak | 7b3a3df | 2021-07-28 14:09:50 +0200 | [diff] [blame] | 274 | /* 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 Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | u8 |
| 283 | appns_sapi_enabled (void) |
| 284 | { |
| 285 | return app_sapi_enabled; |
| 286 | } |
| 287 | |
| 288 | void |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 289 | app_namespaces_init (void) |
| 290 | { |
| 291 | u8 *ns_id = format (0, "default"); |
Florin Coras | fc1c612 | 2017-10-26 14:25:12 -0700 | [diff] [blame] | 292 | |
Steven Luong | 67bae20 | 2024-07-08 11:21:23 -0700 | [diff] [blame] | 293 | /* 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 Coras | fc1c612 | 2017-10-26 14:25:12 -0700 | [diff] [blame] | 302 | if (!app_namespace_lookup_table) |
| 303 | app_namespace_lookup_table = |
| 304 | hash_create_vec (0, sizeof (u8), sizeof (uword)); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 305 | |
| 306 | /* |
| 307 | * Allocate default namespace |
| 308 | */ |
Florin Coras | 7cb471a | 2021-07-23 08:39:26 -0700 | [diff] [blame] | 309 | |
| 310 | /* clang-format off */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 311 | vnet_app_namespace_add_del_args_t a = { |
| 312 | .ns_id = ns_id, |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 313 | .sock_name = 0, |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 314 | .secret = 0, |
| 315 | .sw_if_index = APP_NAMESPACE_INVALID_INDEX, |
| 316 | .is_add = 1 |
| 317 | }; |
Florin Coras | 7cb471a | 2021-07-23 08:39:26 -0700 | [diff] [blame] | 318 | /* clang-format on */ |
| 319 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 320 | vnet_app_namespace_add_del (&a); |
| 321 | vec_free (ns_id); |
| 322 | } |
| 323 | |
| 324 | static clib_error_t * |
| 325 | app_ns_fn (vlib_main_t * vm, unformat_input_t * input, |
| 326 | vlib_cli_command_t * cmd) |
| 327 | { |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 328 | u8 is_add = 0, *ns_id = 0, secret_set = 0, sw_if_index_set = 0; |
Nathan Skrzypczak | 51f1b26 | 2023-04-27 12:43:46 +0200 | [diff] [blame] | 329 | u8 *sock_name = 0; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 330 | unformat_input_t _line_input, *line_input = &_line_input; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 331 | u32 sw_if_index, fib_id = APP_NAMESPACE_INVALID_INDEX; |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 332 | vnet_main_t *vnm = vnet_get_main (); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 333 | u64 secret; |
| 334 | clib_error_t *error = 0; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 335 | int rv; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 336 | |
| 337 | session_cli_return_if_not_enabled (); |
| 338 | |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 339 | 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 Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 343 | { |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 344 | if (unformat (line_input, "add")) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 345 | is_add = 1; |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 346 | else if (unformat (line_input, "del")) |
| 347 | is_add = 0; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 348 | else if (unformat (line_input, "id %_%v%_", &ns_id)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 349 | ; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 350 | else if (unformat (line_input, "secret %lu", &secret)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 351 | secret_set = 1; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 352 | else if (unformat (line_input, "sw_if_index %u", &sw_if_index)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 353 | sw_if_index_set = 1; |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 354 | else if (unformat (line_input, "if %U", unformat_vnet_sw_interface, vnm, |
| 355 | &sw_if_index)) |
| 356 | sw_if_index_set = 1; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 357 | else if (unformat (line_input, "fib_id", &fib_id)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 358 | ; |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 359 | else if (unformat (line_input, "sock-name %_%v%_", &sock_name)) |
| 360 | ; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 361 | else |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 362 | { |
| 363 | error = clib_error_return (0, "unknown input `%U'", |
| 364 | format_unformat_error, line_input); |
Florin Coras | 7cb471a | 2021-07-23 08:39:26 -0700 | [diff] [blame] | 365 | goto done; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 366 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 369 | if (!ns_id) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 370 | { |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 371 | vlib_cli_output (vm, "namespace-id must be provided"); |
Florin Coras | 7cb471a | 2021-07-23 08:39:26 -0700 | [diff] [blame] | 372 | goto done; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 375 | if (is_add && (!secret_set || !sw_if_index_set)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 376 | { |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 377 | vlib_cli_output (vm, "secret and interface must be provided"); |
| 378 | goto done; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 381 | /* clang-format off */ |
| 382 | vnet_app_namespace_add_del_args_t args = { |
| 383 | .ns_id = ns_id, |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 384 | .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 Coras | 7cb471a | 2021-07-23 08:39:26 -0700 | [diff] [blame] | 395 | done: |
| 396 | |
| 397 | vec_free (ns_id); |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 398 | vec_free (sock_name); |
Florin Coras | 7cb471a | 2021-07-23 08:39:26 -0700 | [diff] [blame] | 399 | unformat_free (line_input); |
| 400 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 401 | return error; |
| 402 | } |
| 403 | |
Florin Coras | 7cb471a | 2021-07-23 08:39:26 -0700 | [diff] [blame] | 404 | VLIB_CLI_COMMAND (app_ns_command, static) = { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 405 | .path = "app ns", |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 406 | .short_help = "app ns [add|del] id <namespace-id> secret <secret> " |
Nathan Skrzypczak | 51f1b26 | 2023-04-27 12:43:46 +0200 | [diff] [blame] | 407 | "sw_if_index <sw_if_index> if <interface>", |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 408 | .function = app_ns_fn, |
| 409 | }; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 410 | |
| 411 | u8 * |
| 412 | format_app_namespace (u8 * s, va_list * args) |
| 413 | { |
| 414 | app_namespace_t *app_ns = va_arg (*args, app_namespace_t *); |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 415 | vnet_main_t *vnm = vnet_get_main (); |
Florin Coras | 7cb471a | 2021-07-23 08:39:26 -0700 | [diff] [blame] | 416 | |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 417 | 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 Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 422 | if (app_ns->sock_name) |
| 423 | s = format (s, "\nSocket: %s", app_ns->sock_name); |
| 424 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 425 | return s; |
| 426 | } |
| 427 | |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 428 | static void |
| 429 | app_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 Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 450 | pool_foreach (cs, app_ns->app_sockets) { |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 451 | 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 Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 461 | } |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 464 | static clib_error_t * |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 465 | show_app_ns_fn (vlib_main_t * vm, unformat_input_t * main_input, |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 466 | vlib_cli_command_t * cmd) |
| 467 | { |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 468 | unformat_input_t _line_input, *line_input = &_line_input; |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 469 | u8 *ns_id = 0, do_table = 0, had_input = 1, do_api = 0; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 470 | app_namespace_t *app_ns; |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 471 | vnet_main_t *vnm = vnet_get_main (); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 472 | session_table_t *st; |
Nathan Skrzypczak | 3d5e741 | 2021-09-17 11:53:25 +0200 | [diff] [blame] | 473 | table_t table = {}, *t = &table; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 474 | |
| 475 | session_cli_return_if_not_enabled (); |
| 476 | |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 477 | if (!unformat_user (main_input, unformat_line_input, line_input)) |
Florin Coras | 2640808 | 2017-11-09 02:06:07 -0800 | [diff] [blame] | 478 | { |
| 479 | had_input = 0; |
| 480 | goto do_ns_list; |
| 481 | } |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 482 | |
| 483 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 484 | { |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 485 | if (unformat (line_input, "id %_%v%_", &ns_id)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 486 | do_table = 1; |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 487 | else if (unformat (line_input, "api-clients")) |
| 488 | do_api = 1; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 489 | else |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 490 | { |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 491 | vlib_cli_output (vm, "unknown input [%U]", format_unformat_error, |
| 492 | line_input); |
| 493 | goto done; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 494 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 497 | 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 Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 508 | 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 Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 514 | goto done; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 515 | } |
| 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 Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 520 | goto done; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 521 | } |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 522 | vlib_cli_output (vm, "%U", format_app_namespace, app_ns); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 523 | session_lookup_show_table_entries (vm, st, 0, 1); |
| 524 | vec_free (ns_id); |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 525 | goto done; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Florin Coras | 2640808 | 2017-11-09 02:06:07 -0800 | [diff] [blame] | 528 | do_ns_list: |
Nathan Skrzypczak | 51f1b26 | 2023-04-27 12:43:46 +0200 | [diff] [blame] | 529 | table_add_header_col (t, 5, "Index", "Secret", "Interface", "Id", "Socket"); |
Nathan Skrzypczak | 3d5e741 | 2021-09-17 11:53:25 +0200 | [diff] [blame] | 530 | 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 Skrzypczak | 3d5e741 | 2021-09-17 11:53:25 +0200 | [diff] [blame] | 539 | table_format_cell (t, i++, j++, "%s", app_ns->sock_name); |
| 540 | } |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 541 | |
Nathan Skrzypczak | 3d5e741 | 2021-09-17 11:53:25 +0200 | [diff] [blame] | 542 | 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 Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 548 | |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 549 | done: |
Florin Coras | 2640808 | 2017-11-09 02:06:07 -0800 | [diff] [blame] | 550 | if (had_input) |
| 551 | unformat_free (line_input); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 552 | return 0; |
| 553 | } |
| 554 | |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 555 | VLIB_CLI_COMMAND (show_app_ns_command, static) = { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 556 | .path = "show app ns", |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 557 | .short_help = "show app ns [id <id> [api-clients]]", |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 558 | .function = show_app_ns_fn, |
| 559 | }; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 560 | |
| 561 | /* |
| 562 | * fd.io coding-style-patch-verification: ON |
| 563 | * |
| 564 | * Local Variables: |
| 565 | * eval: (c-set-style "gnu") |
| 566 | * End: |
| 567 | */ |