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> |
| 17 | #include <vnet/session/session_table.h> |
| 18 | #include <vnet/session/session.h> |
| 19 | #include <vnet/fib/fib_table.h> |
| 20 | |
| 21 | /** |
| 22 | * Hash table of application namespaces by app ns ids |
| 23 | */ |
| 24 | uword *app_namespace_lookup_table; |
| 25 | |
| 26 | /** |
| 27 | * Pool of application namespaces |
| 28 | */ |
| 29 | static app_namespace_t *app_namespace_pool; |
| 30 | |
| 31 | app_namespace_t * |
| 32 | app_namespace_get (u32 index) |
| 33 | { |
| 34 | return pool_elt_at_index (app_namespace_pool, index); |
| 35 | } |
| 36 | |
| 37 | app_namespace_t * |
| 38 | app_namespace_get_from_id (const u8 * ns_id) |
| 39 | { |
| 40 | u32 index = app_namespace_index_from_id (ns_id); |
| 41 | if (index == APP_NAMESPACE_INVALID_INDEX) |
| 42 | return 0; |
| 43 | return app_namespace_get (index); |
| 44 | } |
| 45 | |
| 46 | u32 |
| 47 | app_namespace_index (app_namespace_t * app_ns) |
| 48 | { |
| 49 | return (app_ns - app_namespace_pool); |
| 50 | } |
| 51 | |
| 52 | app_namespace_t * |
| 53 | app_namespace_alloc (u8 * ns_id) |
| 54 | { |
| 55 | app_namespace_t *app_ns; |
| 56 | pool_get (app_namespace_pool, app_ns); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 57 | clib_memset (app_ns, 0, sizeof (*app_ns)); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 58 | app_ns->ns_id = vec_dup (ns_id); |
| 59 | hash_set_mem (app_namespace_lookup_table, app_ns->ns_id, |
| 60 | app_ns - app_namespace_pool); |
| 61 | return app_ns; |
| 62 | } |
| 63 | |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 64 | int |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 65 | vnet_app_namespace_add_del (vnet_app_namespace_add_del_args_t * a) |
| 66 | { |
| 67 | app_namespace_t *app_ns; |
| 68 | session_table_t *st; |
| 69 | |
| 70 | if (a->is_add) |
| 71 | { |
| 72 | if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 73 | && !vnet_get_sw_interface_or_null (vnet_get_main (), |
| 74 | a->sw_if_index)) |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 75 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 76 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 77 | |
| 78 | if (a->sw_if_index != APP_NAMESPACE_INVALID_INDEX) |
| 79 | { |
| 80 | a->ip4_fib_id = |
| 81 | fib_table_get_table_id_for_sw_if_index (FIB_PROTOCOL_IP4, |
| 82 | a->sw_if_index); |
| 83 | a->ip6_fib_id = |
Florin Coras | 56b39f6 | 2018-03-27 17:29:32 -0700 | [diff] [blame] | 84 | fib_table_get_table_id_for_sw_if_index (FIB_PROTOCOL_IP6, |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 85 | a->sw_if_index); |
| 86 | } |
| 87 | if (a->sw_if_index == APP_NAMESPACE_INVALID_INDEX |
| 88 | && a->ip4_fib_id == APP_NAMESPACE_INVALID_INDEX) |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 89 | return VNET_API_ERROR_INVALID_VALUE; |
| 90 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 91 | app_ns = app_namespace_get_from_id (a->ns_id); |
| 92 | if (!app_ns) |
Florin Coras | fc1c612 | 2017-10-26 14:25:12 -0700 | [diff] [blame] | 93 | { |
| 94 | app_ns = app_namespace_alloc (a->ns_id); |
| 95 | st = session_table_alloc (); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 96 | session_table_init (st, FIB_PROTOCOL_MAX); |
| 97 | st->is_local = 1; |
| 98 | st->appns_index = app_namespace_index (app_ns); |
Florin Coras | fc1c612 | 2017-10-26 14:25:12 -0700 | [diff] [blame] | 99 | app_ns->local_table_index = session_table_index (st); |
| 100 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 101 | app_ns->ns_secret = a->secret; |
| 102 | app_ns->sw_if_index = a->sw_if_index; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 103 | app_ns->ip4_fib_index = |
| 104 | fib_table_find (FIB_PROTOCOL_IP4, a->ip4_fib_id); |
| 105 | app_ns->ip6_fib_index = |
| 106 | fib_table_find (FIB_PROTOCOL_IP6, a->ip6_fib_id); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 107 | session_lookup_set_tables_appns (app_ns); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 108 | } |
| 109 | else |
| 110 | { |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 111 | return VNET_API_ERROR_UNIMPLEMENTED; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 112 | } |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | const u8 * |
| 117 | app_namespace_id (app_namespace_t * app_ns) |
| 118 | { |
| 119 | return app_ns->ns_id; |
| 120 | } |
| 121 | |
| 122 | u32 |
| 123 | app_namespace_index_from_id (const u8 * ns_id) |
| 124 | { |
| 125 | uword *indexp; |
| 126 | indexp = hash_get_mem (app_namespace_lookup_table, ns_id); |
| 127 | if (!indexp) |
| 128 | return APP_NAMESPACE_INVALID_INDEX; |
| 129 | return *indexp; |
| 130 | } |
| 131 | |
| 132 | const u8 * |
| 133 | app_namespace_id_from_index (u32 index) |
| 134 | { |
| 135 | app_namespace_t *app_ns; |
| 136 | |
| 137 | app_ns = app_namespace_get (index); |
| 138 | return app_namespace_id (app_ns); |
| 139 | } |
| 140 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 141 | u32 |
| 142 | app_namespace_get_fib_index (app_namespace_t * app_ns, u8 fib_proto) |
| 143 | { |
| 144 | return fib_proto == FIB_PROTOCOL_IP4 ? |
| 145 | app_ns->ip4_fib_index : app_ns->ip6_fib_index; |
| 146 | } |
| 147 | |
| 148 | session_table_t * |
| 149 | app_namespace_get_local_table (app_namespace_t * app_ns) |
| 150 | { |
| 151 | return session_table_get (app_ns->local_table_index); |
| 152 | } |
| 153 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 154 | void |
| 155 | app_namespaces_init (void) |
| 156 | { |
| 157 | u8 *ns_id = format (0, "default"); |
Florin Coras | fc1c612 | 2017-10-26 14:25:12 -0700 | [diff] [blame] | 158 | |
| 159 | if (!app_namespace_lookup_table) |
| 160 | app_namespace_lookup_table = |
| 161 | hash_create_vec (0, sizeof (u8), sizeof (uword)); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 162 | |
| 163 | /* |
| 164 | * Allocate default namespace |
| 165 | */ |
| 166 | vnet_app_namespace_add_del_args_t a = { |
| 167 | .ns_id = ns_id, |
| 168 | .secret = 0, |
| 169 | .sw_if_index = APP_NAMESPACE_INVALID_INDEX, |
| 170 | .is_add = 1 |
| 171 | }; |
| 172 | vnet_app_namespace_add_del (&a); |
| 173 | vec_free (ns_id); |
| 174 | } |
| 175 | |
| 176 | static clib_error_t * |
| 177 | app_ns_fn (vlib_main_t * vm, unformat_input_t * input, |
| 178 | vlib_cli_command_t * cmd) |
| 179 | { |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 180 | unformat_input_t _line_input, *line_input = &_line_input; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 181 | u8 is_add = 0, *ns_id = 0, secret_set = 0, sw_if_index_set = 0; |
| 182 | u32 sw_if_index, fib_id = APP_NAMESPACE_INVALID_INDEX; |
| 183 | u64 secret; |
| 184 | clib_error_t *error = 0; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 185 | int rv; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 186 | |
| 187 | session_cli_return_if_not_enabled (); |
| 188 | |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 189 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 190 | return 0; |
| 191 | |
| 192 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 193 | { |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 194 | if (unformat (line_input, "add")) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 195 | is_add = 1; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 196 | else if (unformat (line_input, "id %_%v%_", &ns_id)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 197 | ; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 198 | else if (unformat (line_input, "secret %lu", &secret)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 199 | secret_set = 1; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 200 | else if (unformat (line_input, "sw_if_index %u", &sw_if_index)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 201 | sw_if_index_set = 1; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 202 | else if (unformat (line_input, "fib_id", &fib_id)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 203 | ; |
| 204 | else |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 205 | { |
| 206 | error = clib_error_return (0, "unknown input `%U'", |
| 207 | format_unformat_error, line_input); |
| 208 | unformat_free (line_input); |
| 209 | return error; |
| 210 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 211 | } |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 212 | unformat_free (line_input); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 213 | |
| 214 | if (!ns_id || !secret_set || !sw_if_index_set) |
| 215 | { |
| 216 | vlib_cli_output (vm, "namespace-id, secret and sw_if_index must be " |
| 217 | "provided"); |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | if (is_add) |
| 222 | { |
| 223 | vnet_app_namespace_add_del_args_t args = { |
| 224 | .ns_id = ns_id, |
| 225 | .secret = secret, |
| 226 | .sw_if_index = sw_if_index, |
| 227 | .ip4_fib_id = fib_id, |
| 228 | .is_add = 1 |
| 229 | }; |
Florin Coras | c1a4265 | 2019-02-08 18:27:29 -0800 | [diff] [blame] | 230 | if ((rv = vnet_app_namespace_add_del (&args))) |
| 231 | return clib_error_return (0, "app namespace add del returned %d", rv); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | return error; |
| 235 | } |
| 236 | |
| 237 | /* *INDENT-OFF* */ |
| 238 | VLIB_CLI_COMMAND (app_ns_command, static) = |
| 239 | { |
| 240 | .path = "app ns", |
| 241 | .short_help = "app ns [add] id <namespace-id> secret <secret> " |
| 242 | "sw_if_index <sw_if_index>", |
| 243 | .function = app_ns_fn, |
| 244 | }; |
| 245 | /* *INDENT-ON* */ |
| 246 | |
| 247 | u8 * |
| 248 | format_app_namespace (u8 * s, va_list * args) |
| 249 | { |
| 250 | app_namespace_t *app_ns = va_arg (*args, app_namespace_t *); |
Florin Coras | 8f107b3 | 2017-11-21 22:13:03 -0800 | [diff] [blame] | 251 | s = format (s, "%-10u%-20lu%-20u%-50v", app_namespace_index (app_ns), |
| 252 | app_ns->ns_secret, app_ns->sw_if_index, app_ns->ns_id); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 253 | return s; |
| 254 | } |
| 255 | |
| 256 | static clib_error_t * |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 257 | 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] | 258 | vlib_cli_command_t * cmd) |
| 259 | { |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 260 | unformat_input_t _line_input, *line_input = &_line_input; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 261 | app_namespace_t *app_ns; |
| 262 | session_table_t *st; |
Florin Coras | 2640808 | 2017-11-09 02:06:07 -0800 | [diff] [blame] | 263 | u8 *ns_id, do_table = 0, had_input = 1; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 264 | |
| 265 | session_cli_return_if_not_enabled (); |
| 266 | |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 267 | if (!unformat_user (main_input, unformat_line_input, line_input)) |
Florin Coras | 2640808 | 2017-11-09 02:06:07 -0800 | [diff] [blame] | 268 | { |
| 269 | had_input = 0; |
| 270 | goto do_ns_list; |
| 271 | } |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 272 | |
| 273 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 274 | { |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 275 | if (unformat (line_input, "table %_%v%_", &ns_id)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 276 | do_table = 1; |
| 277 | else |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 278 | { |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 279 | vlib_cli_output (vm, "unknown input [%U]", format_unformat_error, |
| 280 | line_input); |
| 281 | goto done; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 282 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | if (do_table) |
| 286 | { |
| 287 | app_ns = app_namespace_get_from_id (ns_id); |
| 288 | if (!app_ns) |
| 289 | { |
| 290 | vlib_cli_output (vm, "ns %v not found", ns_id); |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 291 | goto done; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 292 | } |
| 293 | st = session_table_get (app_ns->local_table_index); |
| 294 | if (!st) |
| 295 | { |
| 296 | 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] | 297 | goto done; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 298 | } |
| 299 | session_lookup_show_table_entries (vm, st, 0, 1); |
| 300 | vec_free (ns_id); |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 301 | goto done; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Florin Coras | 2640808 | 2017-11-09 02:06:07 -0800 | [diff] [blame] | 304 | do_ns_list: |
Florin Coras | 8f107b3 | 2017-11-21 22:13:03 -0800 | [diff] [blame] | 305 | vlib_cli_output (vm, "%-10s%-20s%-20s%-50s", "Index", "Secret", |
| 306 | "sw_if_index", "Name"); |
Florin Coras | dff48db | 2017-11-19 18:06:58 -0800 | [diff] [blame] | 307 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 308 | /* *INDENT-OFF* */ |
| 309 | pool_foreach (app_ns, app_namespace_pool, ({ |
| 310 | vlib_cli_output (vm, "%U", format_app_namespace, app_ns); |
| 311 | })); |
| 312 | /* *INDENT-ON* */ |
| 313 | |
Florin Coras | dcdff6e | 2017-11-07 22:36:02 -0800 | [diff] [blame] | 314 | done: |
Florin Coras | 2640808 | 2017-11-09 02:06:07 -0800 | [diff] [blame] | 315 | if (had_input) |
| 316 | unformat_free (line_input); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | /* *INDENT-OFF* */ |
| 321 | VLIB_CLI_COMMAND (show_app_ns_command, static) = |
| 322 | { |
| 323 | .path = "show app ns", |
| 324 | .short_help = "show app ns", |
| 325 | .function = show_app_ns_fn, |
| 326 | }; |
| 327 | /* *INDENT-ON* */ |
| 328 | |
| 329 | /* |
| 330 | * fd.io coding-style-patch-verification: ON |
| 331 | * |
| 332 | * Local Variables: |
| 333 | * eval: (c-set-style "gnu") |
| 334 | * End: |
| 335 | */ |