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 | |
| 25 | /** |
| 26 | * Hash table of application namespaces by app ns ids |
| 27 | */ |
| 28 | uword *app_namespace_lookup_table; |
| 29 | |
| 30 | /** |
| 31 | * Pool of application namespaces |
| 32 | */ |
| 33 | static app_namespace_t *app_namespace_pool; |
| 34 | |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 35 | static u8 app_sapi_enabled; |
| 36 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 37 | app_namespace_t * |
| 38 | app_namespace_get (u32 index) |
| 39 | { |
| 40 | return pool_elt_at_index (app_namespace_pool, index); |
| 41 | } |
| 42 | |
| 43 | app_namespace_t * |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 44 | app_namespace_get_from_id (const u8 *ns_id) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 45 | { |
| 46 | u32 index = app_namespace_index_from_id (ns_id); |
| 47 | if (index == APP_NAMESPACE_INVALID_INDEX) |
| 48 | return 0; |
| 49 | return app_namespace_get (index); |
| 50 | } |
| 51 | |
| 52 | u32 |
| 53 | app_namespace_index (app_namespace_t * app_ns) |
| 54 | { |
| 55 | return (app_ns - app_namespace_pool); |
| 56 | } |
| 57 | |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 58 | void |
| 59 | app_namespace_free (app_namespace_t *app_ns) |
| 60 | { |
| 61 | hash_unset_mem (app_namespace_lookup_table, app_ns->ns_id); |
| 62 | vec_free (app_ns->ns_id); |
| 63 | |
| 64 | pool_put (app_namespace_pool, app_ns); |
| 65 | } |
| 66 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 67 | app_namespace_t * |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 68 | app_namespace_alloc (const u8 *ns_id) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 69 | { |
| 70 | app_namespace_t *app_ns; |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 71 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 72 | pool_get (app_namespace_pool, app_ns); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 73 | clib_memset (app_ns, 0, sizeof (*app_ns)); |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 74 | |
| 75 | app_ns->ns_id = vec_dup ((u8 *) ns_id); |
| 76 | vec_terminate_c_string (app_ns->ns_id); |
| 77 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 78 | hash_set_mem (app_namespace_lookup_table, app_ns->ns_id, |
| 79 | app_ns - app_namespace_pool); |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 80 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 81 | return app_ns; |
| 82 | } |
| 83 | |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 84 | session_error_t |
| 85 | 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] | 86 | { |
| 87 | app_namespace_t *app_ns; |
| 88 | session_table_t *st; |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 89 | u32 ns_index; |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 90 | session_error_t rv; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 91 | |
| 92 | if (a->is_add) |
| 93 | { |
| 94 | if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 95 | && !vnet_get_sw_interface_or_null (vnet_get_main (), |
| 96 | a->sw_if_index)) |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 97 | return SESSION_E_INVALID; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 98 | |
| 99 | if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX) |
| 100 | { |
| 101 | a->ip4_fib_id = |
| 102 | fib_table_get_table_id_for_sw_if_index (FIB_PROTOCOL_IP4, |
| 103 | a->sw_if_index); |
| 104 | a->ip6_fib_id = |
Florin Coras | 56b39f6 | 2018-03-27 17:29:32 -0700 | [diff] [blame] | 105 | fib_table_get_table_id_for_sw_if_index (FIB_PROTOCOL_IP6, |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 106 | a->sw_if_index); |
| 107 | } |
| 108 | if (a->sw_if_index == APP_NAMESPACE_INVALID_INDEX |
| 109 | && a->ip4_fib_id == APP_NAMESPACE_INVALID_INDEX) |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 110 | return SESSION_E_INVALID; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 111 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 112 | app_ns = app_namespace_get_from_id (a->ns_id); |
| 113 | if (!app_ns) |
Florin Coras | fc1c612 | 2017-10-26 14:25:12 -0700 | [diff] [blame] | 114 | { |
| 115 | app_ns = app_namespace_alloc (a->ns_id); |
| 116 | st = session_table_alloc (); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 117 | session_table_init (st, FIB_PROTOCOL_MAX); |
| 118 | st->is_local = 1; |
| 119 | st->appns_index = app_namespace_index (app_ns); |
Florin Coras | fc1c612 | 2017-10-26 14:25:12 -0700 | [diff] [blame] | 120 | app_ns->local_table_index = session_table_index (st); |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 121 | if (a->sock_name) |
| 122 | { |
| 123 | app_ns->sock_name = vec_dup (a->sock_name); |
| 124 | vec_terminate_c_string (app_ns->sock_name); |
| 125 | } |
| 126 | |
| 127 | /* Add socket for namespace, |
| 128 | * only at creation time */ |
| 129 | if (app_sapi_enabled) |
| 130 | { |
| 131 | rv = appns_sapi_add_ns_socket (app_ns); |
| 132 | if (rv) |
| 133 | return rv; |
| 134 | } |
Florin Coras | fc1c612 | 2017-10-26 14:25:12 -0700 | [diff] [blame] | 135 | } |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 136 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 137 | app_ns->ns_secret = a->secret; |
| 138 | app_ns->sw_if_index = a->sw_if_index; |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 139 | |
Steven Luong | 67bae20 | 2024-07-08 11:21:23 -0700 | [diff] [blame] | 140 | app_ns->ip4_fib_index = fib_table_find (FIB_PROTOCOL_IP4, a->ip4_fib_id); |
Steven Luong | 67bae20 | 2024-07-08 11:21:23 -0700 | [diff] [blame] | 141 | app_ns->ip6_fib_index = fib_table_find (FIB_PROTOCOL_IP6, a->ip6_fib_id); |
Steven Luong | 67bae20 | 2024-07-08 11:21:23 -0700 | [diff] [blame] | 142 | session_lookup_set_tables_appns (app_ns); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 143 | } |
| 144 | else |
| 145 | { |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 146 | ns_index = app_namespace_index_from_id (a->ns_id); |
| 147 | if (ns_index == APP_NAMESPACE_INVALID_INDEX) |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 148 | return SESSION_E_INVALID; |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 149 | |
| 150 | app_ns = app_namespace_get (ns_index); |
| 151 | if (!app_ns) |
Filip Tehlar | 0028e6f | 2023-06-28 10:47:32 +0200 | [diff] [blame] | 152 | return SESSION_E_INVALID; |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 153 | |
| 154 | application_namespace_cleanup (app_ns); |
| 155 | |
| 156 | if (app_sapi_enabled) |
| 157 | appns_sapi_del_ns_socket (app_ns); |
| 158 | |
| 159 | st = session_table_get (app_ns->local_table_index); |
| 160 | |
| 161 | session_table_free (st, FIB_PROTOCOL_MAX); |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 162 | if (app_ns->sock_name) |
| 163 | vec_free (app_ns->sock_name); |
| 164 | |
Steven Luong | 5682ca8 | 2024-07-17 16:16:05 -0700 | [diff] [blame] | 165 | session_lookup_table_cleanup (FIB_PROTOCOL_IP4, app_ns->ip4_fib_index); |
| 166 | session_lookup_table_cleanup (FIB_PROTOCOL_IP6, app_ns->ip6_fib_index); |
Steven Luong | 67bae20 | 2024-07-08 11:21:23 -0700 | [diff] [blame] | 167 | |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 168 | app_namespace_free (app_ns); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 169 | } |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 170 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | const u8 * |
| 175 | app_namespace_id (app_namespace_t * app_ns) |
| 176 | { |
| 177 | return app_ns->ns_id; |
| 178 | } |
| 179 | |
| 180 | u32 |
| 181 | app_namespace_index_from_id (const u8 * ns_id) |
| 182 | { |
| 183 | uword *indexp; |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 184 | u8 *key; |
| 185 | |
| 186 | key = vec_dup ((u8 *) ns_id); |
| 187 | vec_terminate_c_string (key); |
| 188 | |
| 189 | indexp = hash_get_mem (app_namespace_lookup_table, key); |
| 190 | vec_free (key); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 191 | if (!indexp) |
| 192 | return APP_NAMESPACE_INVALID_INDEX; |
| 193 | return *indexp; |
| 194 | } |
| 195 | |
| 196 | const u8 * |
| 197 | app_namespace_id_from_index (u32 index) |
| 198 | { |
| 199 | app_namespace_t *app_ns; |
| 200 | |
| 201 | app_ns = app_namespace_get (index); |
| 202 | return app_namespace_id (app_ns); |
| 203 | } |
| 204 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 205 | u32 |
| 206 | app_namespace_get_fib_index (app_namespace_t * app_ns, u8 fib_proto) |
| 207 | { |
| 208 | return fib_proto == FIB_PROTOCOL_IP4 ? |
| 209 | app_ns->ip4_fib_index : app_ns->ip6_fib_index; |
| 210 | } |
| 211 | |
| 212 | session_table_t * |
| 213 | app_namespace_get_local_table (app_namespace_t * app_ns) |
| 214 | { |
| 215 | return session_table_get (app_ns->local_table_index); |
| 216 | } |
| 217 | |
Nathan Skrzypczak | 7b3a3df | 2021-07-28 14:09:50 +0200 | [diff] [blame] | 218 | int |
| 219 | appns_sapi_enable_disable (int is_enable) |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 220 | { |
Nathan Skrzypczak | 7b3a3df | 2021-07-28 14:09:50 +0200 | [diff] [blame] | 221 | /* This cannot be called with active sockets */ |
| 222 | if (pool_elts (app_namespace_pool)) |
| 223 | return -1; |
| 224 | |
| 225 | app_sapi_enabled = is_enable; |
| 226 | return 0; |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | u8 |
| 230 | appns_sapi_enabled (void) |
| 231 | { |
| 232 | return app_sapi_enabled; |
| 233 | } |
| 234 | |
| 235 | void |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 236 | app_namespaces_init (void) |
| 237 | { |
| 238 | u8 *ns_id = format (0, "default"); |
Florin Coras | fc1c612 | 2017-10-26 14:25:12 -0700 | [diff] [blame] | 239 | |
| 240 | if (!app_namespace_lookup_table) |
| 241 | app_namespace_lookup_table = |
| 242 | hash_create_vec (0, sizeof (u8), sizeof (uword)); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 243 | |
| 244 | /* |
| 245 | * Allocate default namespace |
| 246 | */ |
Florin Coras | 7cb471a | 2021-07-23 08:39:26 -0700 | [diff] [blame] | 247 | |
| 248 | /* clang-format off */ |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 249 | vnet_app_namespace_add_del_args_t a = { |
| 250 | .ns_id = ns_id, |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 251 | .sock_name = 0, |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 252 | .secret = 0, |
| 253 | .sw_if_index = APP_NAMESPACE_INVALID_INDEX, |
| 254 | .is_add = 1 |
| 255 | }; |
Florin Coras | 7cb471a | 2021-07-23 08:39:26 -0700 | [diff] [blame] | 256 | /* clang-format on */ |
| 257 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 258 | vnet_app_namespace_add_del (&a); |
| 259 | vec_free (ns_id); |
| 260 | } |
| 261 | |
| 262 | static clib_error_t * |
| 263 | app_ns_fn (vlib_main_t * vm, unformat_input_t * input, |
| 264 | vlib_cli_command_t * cmd) |
| 265 | { |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 266 | 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] | 267 | u8 *sock_name = 0; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 268 | unformat_input_t _line_input, *line_input = &_line_input; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 269 | u32 sw_if_index, fib_id = APP_NAMESPACE_INVALID_INDEX; |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 270 | vnet_main_t *vnm = vnet_get_main (); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 271 | u64 secret; |
| 272 | clib_error_t *error = 0; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 273 | int rv; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 274 | |
| 275 | session_cli_return_if_not_enabled (); |
| 276 | |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 277 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 278 | return 0; |
| 279 | |
| 280 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 281 | { |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 282 | if (unformat (line_input, "add")) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 283 | is_add = 1; |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 284 | else if (unformat (line_input, "del")) |
| 285 | is_add = 0; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 286 | else if (unformat (line_input, "id %_%v%_", &ns_id)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 287 | ; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 288 | else if (unformat (line_input, "secret %lu", &secret)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 289 | secret_set = 1; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 290 | else if (unformat (line_input, "sw_if_index %u", &sw_if_index)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 291 | sw_if_index_set = 1; |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 292 | else if (unformat (line_input, "if %U", unformat_vnet_sw_interface, vnm, |
| 293 | &sw_if_index)) |
| 294 | sw_if_index_set = 1; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 295 | else if (unformat (line_input, "fib_id", &fib_id)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 296 | ; |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 297 | else if (unformat (line_input, "sock-name %_%v%_", &sock_name)) |
| 298 | ; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 299 | else |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 300 | { |
| 301 | error = clib_error_return (0, "unknown input `%U'", |
| 302 | format_unformat_error, line_input); |
Florin Coras | 7cb471a | 2021-07-23 08:39:26 -0700 | [diff] [blame] | 303 | goto done; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 304 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 307 | if (!ns_id) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 308 | { |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 309 | vlib_cli_output (vm, "namespace-id must be provided"); |
Florin Coras | 7cb471a | 2021-07-23 08:39:26 -0700 | [diff] [blame] | 310 | goto done; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 313 | if (is_add && (!secret_set || !sw_if_index_set)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 314 | { |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 315 | vlib_cli_output (vm, "secret and interface must be provided"); |
| 316 | goto done; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 319 | /* clang-format off */ |
| 320 | vnet_app_namespace_add_del_args_t args = { |
| 321 | .ns_id = ns_id, |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 322 | .secret = secret, |
| 323 | .sw_if_index = sw_if_index, |
| 324 | .sock_name = sock_name, |
| 325 | .ip4_fib_id = fib_id, |
| 326 | .is_add = is_add, |
| 327 | }; |
| 328 | /* clang-format on */ |
| 329 | |
| 330 | if ((rv = vnet_app_namespace_add_del (&args))) |
| 331 | error = clib_error_return (0, "app namespace add del returned %d", rv); |
| 332 | |
Florin Coras | 7cb471a | 2021-07-23 08:39:26 -0700 | [diff] [blame] | 333 | done: |
| 334 | |
| 335 | vec_free (ns_id); |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 336 | vec_free (sock_name); |
Florin Coras | 7cb471a | 2021-07-23 08:39:26 -0700 | [diff] [blame] | 337 | unformat_free (line_input); |
| 338 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 339 | return error; |
| 340 | } |
| 341 | |
Florin Coras | 7cb471a | 2021-07-23 08:39:26 -0700 | [diff] [blame] | 342 | VLIB_CLI_COMMAND (app_ns_command, static) = { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 343 | .path = "app ns", |
Nathan Skrzypczak | b3ea73e | 2021-08-05 10:22:52 +0200 | [diff] [blame] | 344 | .short_help = "app ns [add|del] id <namespace-id> secret <secret> " |
Nathan Skrzypczak | 51f1b26 | 2023-04-27 12:43:46 +0200 | [diff] [blame] | 345 | "sw_if_index <sw_if_index> if <interface>", |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 346 | .function = app_ns_fn, |
| 347 | }; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 348 | |
| 349 | u8 * |
| 350 | format_app_namespace (u8 * s, va_list * args) |
| 351 | { |
| 352 | app_namespace_t *app_ns = va_arg (*args, app_namespace_t *); |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 353 | vnet_main_t *vnm = vnet_get_main (); |
Florin Coras | 7cb471a | 2021-07-23 08:39:26 -0700 | [diff] [blame] | 354 | |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 355 | s = format (s, "Application namespace [%u]\nid: %s\nsecret: %lu", |
| 356 | app_namespace_index (app_ns), app_ns->ns_id, app_ns->ns_secret); |
| 357 | if (app_ns->sw_if_index != (u32) ~0) |
| 358 | s = format (s, "\nInterface: %U", format_vnet_sw_if_index_name, vnm, |
| 359 | app_ns->sw_if_index); |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 360 | if (app_ns->sock_name) |
| 361 | s = format (s, "\nSocket: %s", app_ns->sock_name); |
| 362 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 363 | return s; |
| 364 | } |
| 365 | |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 366 | static void |
| 367 | app_namespace_show_api (vlib_main_t * vm, app_namespace_t * app_ns) |
| 368 | { |
| 369 | app_ns_api_handle_t *handle; |
| 370 | app_worker_t *app_wrk; |
| 371 | clib_socket_t *cs; |
| 372 | clib_file_t *cf; |
| 373 | |
| 374 | if (!app_sapi_enabled) |
| 375 | { |
| 376 | vlib_cli_output (vm, "app socket api not enabled!"); |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | vlib_cli_output (vm, "socket: %v\n", app_ns->sock_name); |
| 381 | |
| 382 | if (!pool_elts (app_ns->app_sockets)) |
| 383 | return; |
| 384 | |
| 385 | vlib_cli_output (vm, "%12s%12s%5s", "app index", "wrk index", "fd"); |
| 386 | |
| 387 | |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 388 | pool_foreach (cs, app_ns->app_sockets) { |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 389 | handle = (app_ns_api_handle_t *) &cs->private_data; |
| 390 | cf = clib_file_get (&file_main, handle->aah_file_index); |
| 391 | if (handle->aah_app_wrk_index == APP_INVALID_INDEX) |
| 392 | { |
| 393 | vlib_cli_output (vm, "%12d%12d%5u", -1, -1, cf->file_descriptor); |
| 394 | continue; |
| 395 | } |
| 396 | app_wrk = app_worker_get (handle->aah_app_wrk_index); |
| 397 | vlib_cli_output (vm, "%12d%12d%5u", app_wrk->app_index, |
| 398 | app_wrk->wrk_map_index, cf->file_descriptor); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 399 | } |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 402 | static clib_error_t * |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 403 | 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] | 404 | vlib_cli_command_t * cmd) |
| 405 | { |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 406 | unformat_input_t _line_input, *line_input = &_line_input; |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 407 | 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] | 408 | app_namespace_t *app_ns; |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 409 | vnet_main_t *vnm = vnet_get_main (); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 410 | session_table_t *st; |
Nathan Skrzypczak | 3d5e741 | 2021-09-17 11:53:25 +0200 | [diff] [blame] | 411 | table_t table = {}, *t = &table; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 412 | |
| 413 | session_cli_return_if_not_enabled (); |
| 414 | |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 415 | if (!unformat_user (main_input, unformat_line_input, line_input)) |
Florin Coras | 2640808 | 2017-11-09 02:06:07 -0800 | [diff] [blame] | 416 | { |
| 417 | had_input = 0; |
| 418 | goto do_ns_list; |
| 419 | } |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 420 | |
| 421 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 422 | { |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 423 | if (unformat (line_input, "id %_%v%_", &ns_id)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 424 | do_table = 1; |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 425 | else if (unformat (line_input, "api-clients")) |
| 426 | do_api = 1; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 427 | else |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 428 | { |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 429 | vlib_cli_output (vm, "unknown input [%U]", format_unformat_error, |
| 430 | line_input); |
| 431 | goto done; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 432 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Florin Coras | 61ae056 | 2020-09-02 19:10:28 -0700 | [diff] [blame] | 435 | if (do_api) |
| 436 | { |
| 437 | if (!do_table) |
| 438 | { |
| 439 | vlib_cli_output (vm, "must specify a table for api"); |
| 440 | goto done; |
| 441 | } |
| 442 | app_ns = app_namespace_get_from_id (ns_id); |
| 443 | app_namespace_show_api (vm, app_ns); |
| 444 | goto done; |
| 445 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 446 | if (do_table) |
| 447 | { |
| 448 | app_ns = app_namespace_get_from_id (ns_id); |
| 449 | if (!app_ns) |
| 450 | { |
| 451 | vlib_cli_output (vm, "ns %v not found", ns_id); |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 452 | goto done; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 453 | } |
| 454 | st = session_table_get (app_ns->local_table_index); |
| 455 | if (!st) |
| 456 | { |
| 457 | 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] | 458 | goto done; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 459 | } |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 460 | vlib_cli_output (vm, "%U", format_app_namespace, app_ns); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 461 | session_lookup_show_table_entries (vm, st, 0, 1); |
| 462 | vec_free (ns_id); |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 463 | goto done; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Florin Coras | 2640808 | 2017-11-09 02:06:07 -0800 | [diff] [blame] | 466 | do_ns_list: |
Nathan Skrzypczak | 51f1b26 | 2023-04-27 12:43:46 +0200 | [diff] [blame] | 467 | table_add_header_col (t, 5, "Index", "Secret", "Interface", "Id", "Socket"); |
Nathan Skrzypczak | 3d5e741 | 2021-09-17 11:53:25 +0200 | [diff] [blame] | 468 | int i = 0; |
| 469 | pool_foreach (app_ns, app_namespace_pool) |
| 470 | { |
| 471 | int j = 0; |
| 472 | table_format_cell (t, i, j++, "%u", app_namespace_index (app_ns)); |
| 473 | table_format_cell (t, i, j++, "%lu", app_ns->ns_secret); |
| 474 | table_format_cell (t, i, j++, "%U", format_vnet_sw_if_index_name, vnm, |
| 475 | app_ns->sw_if_index); |
| 476 | table_format_cell (t, i, j++, "%s", app_ns->ns_id); |
Nathan Skrzypczak | 3d5e741 | 2021-09-17 11:53:25 +0200 | [diff] [blame] | 477 | table_format_cell (t, i++, j++, "%s", app_ns->sock_name); |
| 478 | } |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 479 | |
Nathan Skrzypczak | 3d5e741 | 2021-09-17 11:53:25 +0200 | [diff] [blame] | 480 | t->default_body.align = TTAA_LEFT; |
| 481 | t->default_header_col.align = TTAA_LEFT; |
| 482 | t->default_header_col.fg_color = TTAC_YELLOW; |
| 483 | t->default_header_col.flags = TTAF_FG_COLOR_SET; |
| 484 | vlib_cli_output (vm, "%U", format_table, t); |
| 485 | table_free (t); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 486 | |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 487 | done: |
Florin Coras | 2640808 | 2017-11-09 02:06:07 -0800 | [diff] [blame] | 488 | if (had_input) |
| 489 | unformat_free (line_input); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 490 | return 0; |
| 491 | } |
| 492 | |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 493 | VLIB_CLI_COMMAND (show_app_ns_command, static) = { |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 494 | .path = "show app ns", |
Nathan Skrzypczak | 1a9e2f9 | 2021-07-28 19:35:08 +0200 | [diff] [blame] | 495 | .short_help = "show app ns [id <id> [api-clients]]", |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 496 | .function = show_app_ns_fn, |
| 497 | }; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 498 | |
| 499 | /* |
| 500 | * fd.io coding-style-patch-verification: ON |
| 501 | * |
| 502 | * Local Variables: |
| 503 | * eval: (c-set-style "gnu") |
| 504 | * End: |
| 505 | */ |