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