Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 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); |
| 57 | memset (app_ns, 0, sizeof (*app_ns)); |
| 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 | |
| 64 | clib_error_t * |
| 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 |
| 73 | && !vnet_get_sw_interface_safe (vnet_get_main (), a->sw_if_index)) |
| 74 | return clib_error_return_code (0, VNET_API_ERROR_INVALID_SW_IF_INDEX, |
| 75 | 0, "sw_if_index %u doesn't exist", |
| 76 | a->sw_if_index); |
| 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 = |
| 84 | fib_table_get_table_id_for_sw_if_index (FIB_PROTOCOL_IP4, |
| 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) |
| 89 | return clib_error_return_code (0, VNET_API_ERROR_INVALID_VALUE, 0, |
| 90 | "sw_if_index or fib_id must be " |
| 91 | "configured"); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 92 | app_ns = app_namespace_get_from_id (a->ns_id); |
| 93 | if (!app_ns) |
Florin Coras | fc1c612 | 2017-10-26 14:25:12 -0700 | [diff] [blame] | 94 | { |
| 95 | app_ns = app_namespace_alloc (a->ns_id); |
| 96 | st = session_table_alloc (); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 97 | session_table_init (st, FIB_PROTOCOL_MAX); |
| 98 | st->is_local = 1; |
| 99 | st->appns_index = app_namespace_index (app_ns); |
Florin Coras | fc1c612 | 2017-10-26 14:25:12 -0700 | [diff] [blame] | 100 | app_ns->local_table_index = session_table_index (st); |
| 101 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 102 | app_ns->ns_secret = a->secret; |
| 103 | app_ns->sw_if_index = a->sw_if_index; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 104 | app_ns->ip4_fib_index = |
| 105 | fib_table_find (FIB_PROTOCOL_IP4, a->ip4_fib_id); |
| 106 | app_ns->ip6_fib_index = |
| 107 | fib_table_find (FIB_PROTOCOL_IP6, a->ip6_fib_id); |
Florin Coras | 6c36f53 | 2017-11-03 18:32:34 -0700 | [diff] [blame] | 108 | session_lookup_set_tables_appns (app_ns); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 109 | } |
| 110 | else |
| 111 | { |
| 112 | return clib_error_return_code (0, VNET_API_ERROR_UNIMPLEMENTED, 0, |
| 113 | "namespace deletion not supported"); |
| 114 | } |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | const u8 * |
| 119 | app_namespace_id (app_namespace_t * app_ns) |
| 120 | { |
| 121 | return app_ns->ns_id; |
| 122 | } |
| 123 | |
| 124 | u32 |
| 125 | app_namespace_index_from_id (const u8 * ns_id) |
| 126 | { |
| 127 | uword *indexp; |
| 128 | indexp = hash_get_mem (app_namespace_lookup_table, ns_id); |
| 129 | if (!indexp) |
| 130 | return APP_NAMESPACE_INVALID_INDEX; |
| 131 | return *indexp; |
| 132 | } |
| 133 | |
| 134 | const u8 * |
| 135 | app_namespace_id_from_index (u32 index) |
| 136 | { |
| 137 | app_namespace_t *app_ns; |
| 138 | |
| 139 | app_ns = app_namespace_get (index); |
| 140 | return app_namespace_id (app_ns); |
| 141 | } |
| 142 | |
Florin Coras | 1c71045 | 2017-10-17 00:03:13 -0700 | [diff] [blame] | 143 | u32 |
| 144 | app_namespace_get_fib_index (app_namespace_t * app_ns, u8 fib_proto) |
| 145 | { |
| 146 | return fib_proto == FIB_PROTOCOL_IP4 ? |
| 147 | app_ns->ip4_fib_index : app_ns->ip6_fib_index; |
| 148 | } |
| 149 | |
| 150 | session_table_t * |
| 151 | app_namespace_get_local_table (app_namespace_t * app_ns) |
| 152 | { |
| 153 | return session_table_get (app_ns->local_table_index); |
| 154 | } |
| 155 | |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 156 | void |
| 157 | app_namespaces_init (void) |
| 158 | { |
| 159 | u8 *ns_id = format (0, "default"); |
Florin Coras | fc1c612 | 2017-10-26 14:25:12 -0700 | [diff] [blame] | 160 | |
| 161 | if (!app_namespace_lookup_table) |
| 162 | app_namespace_lookup_table = |
| 163 | hash_create_vec (0, sizeof (u8), sizeof (uword)); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 164 | |
| 165 | /* |
| 166 | * Allocate default namespace |
| 167 | */ |
| 168 | vnet_app_namespace_add_del_args_t a = { |
| 169 | .ns_id = ns_id, |
| 170 | .secret = 0, |
| 171 | .sw_if_index = APP_NAMESPACE_INVALID_INDEX, |
| 172 | .is_add = 1 |
| 173 | }; |
| 174 | vnet_app_namespace_add_del (&a); |
| 175 | vec_free (ns_id); |
| 176 | } |
| 177 | |
| 178 | static clib_error_t * |
| 179 | app_ns_fn (vlib_main_t * vm, unformat_input_t * input, |
| 180 | vlib_cli_command_t * cmd) |
| 181 | { |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 182 | unformat_input_t _line_input, *line_input = &_line_input; |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 183 | u8 is_add = 0, *ns_id = 0, secret_set = 0, sw_if_index_set = 0; |
| 184 | u32 sw_if_index, fib_id = APP_NAMESPACE_INVALID_INDEX; |
| 185 | u64 secret; |
| 186 | clib_error_t *error = 0; |
| 187 | |
| 188 | session_cli_return_if_not_enabled (); |
| 189 | |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 190 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 191 | return 0; |
| 192 | |
| 193 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 194 | { |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 195 | if (unformat (line_input, "add")) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 196 | is_add = 1; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 197 | else if (unformat (line_input, "id %_%v%_", &ns_id)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 198 | ; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 199 | else if (unformat (line_input, "secret %lu", &secret)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 200 | secret_set = 1; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 201 | else if (unformat (line_input, "sw_if_index %u", &sw_if_index)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 202 | sw_if_index_set = 1; |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 203 | else if (unformat (line_input, "fib_id", &fib_id)) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 204 | ; |
| 205 | else |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 206 | { |
| 207 | error = clib_error_return (0, "unknown input `%U'", |
| 208 | format_unformat_error, line_input); |
| 209 | unformat_free (line_input); |
| 210 | return error; |
| 211 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 212 | } |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 213 | unformat_free (line_input); |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 214 | |
| 215 | if (!ns_id || !secret_set || !sw_if_index_set) |
| 216 | { |
| 217 | vlib_cli_output (vm, "namespace-id, secret and sw_if_index must be " |
| 218 | "provided"); |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | if (is_add) |
| 223 | { |
| 224 | vnet_app_namespace_add_del_args_t args = { |
| 225 | .ns_id = ns_id, |
| 226 | .secret = secret, |
| 227 | .sw_if_index = sw_if_index, |
| 228 | .ip4_fib_id = fib_id, |
| 229 | .is_add = 1 |
| 230 | }; |
| 231 | error = vnet_app_namespace_add_del (&args); |
| 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 *); |
| 251 | s = format (s, "%-20v%-20lu%-20u", app_ns->ns_id, app_ns->ns_secret, |
| 252 | app_ns->sw_if_index); |
| 253 | return s; |
| 254 | } |
| 255 | |
| 256 | static clib_error_t * |
| 257 | show_app_ns_fn (vlib_main_t * vm, unformat_input_t * input, |
| 258 | vlib_cli_command_t * cmd) |
| 259 | { |
| 260 | app_namespace_t *app_ns; |
| 261 | session_table_t *st; |
| 262 | u8 *ns_id, do_table = 0; |
| 263 | |
| 264 | session_cli_return_if_not_enabled (); |
| 265 | |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 266 | if (unformat_peek_input (input) != UNFORMAT_END_OF_INPUT) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 267 | { |
| 268 | if (unformat (input, "table %_%v%_", &ns_id)) |
| 269 | do_table = 1; |
| 270 | else |
Dave Wallace | 8af2054 | 2017-10-26 03:29:30 -0400 | [diff] [blame] | 271 | { |
| 272 | vlib_cli_output (vm, "unknown input [%U]", |
| 273 | format_unformat_error, input); |
| 274 | return clib_error_return (0, "unknown input `%U'", |
| 275 | format_unformat_error, input); |
| 276 | } |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | if (do_table) |
| 280 | { |
| 281 | app_ns = app_namespace_get_from_id (ns_id); |
| 282 | if (!app_ns) |
| 283 | { |
| 284 | vlib_cli_output (vm, "ns %v not found", ns_id); |
| 285 | return 0; |
| 286 | } |
| 287 | st = session_table_get (app_ns->local_table_index); |
| 288 | if (!st) |
| 289 | { |
| 290 | vlib_cli_output (vm, "table for ns %v could not be found", ns_id); |
| 291 | return 0; |
| 292 | } |
| 293 | session_lookup_show_table_entries (vm, st, 0, 1); |
| 294 | vec_free (ns_id); |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | vlib_cli_output (vm, "%-20s%-20s%-20s", "Namespace", "Secret", |
| 299 | "sw_if_index"); |
| 300 | |
| 301 | /* *INDENT-OFF* */ |
| 302 | pool_foreach (app_ns, app_namespace_pool, ({ |
| 303 | vlib_cli_output (vm, "%U", format_app_namespace, app_ns); |
| 304 | })); |
| 305 | /* *INDENT-ON* */ |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | /* *INDENT-OFF* */ |
| 311 | VLIB_CLI_COMMAND (show_app_ns_command, static) = |
| 312 | { |
| 313 | .path = "show app ns", |
| 314 | .short_help = "show app ns", |
| 315 | .function = show_app_ns_fn, |
| 316 | }; |
| 317 | /* *INDENT-ON* */ |
| 318 | |
| 319 | /* |
| 320 | * fd.io coding-style-patch-verification: ON |
| 321 | * |
| 322 | * Local Variables: |
| 323 | * eval: (c-set-style "gnu") |
| 324 | * End: |
| 325 | */ |