Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * mpls_fib.h: The Label/MPLS FIB |
| 3 | * |
| 4 | * Copyright (c) 2012 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | /** |
| 18 | * An MPLS_FIB table; |
| 19 | * |
| 20 | * The entries in the table are programmed wtih one or more MOIs. These MOIs |
| 21 | * may result in different forwarding actions for end-of-stack (EOS) and non-EOS |
| 22 | * packets. Whether the two actions are the same more often than they are |
| 23 | * different, or vice versa, is a function of the deployment in which the router |
| 24 | * is used and thus not predictable. |
| 25 | * The desgin choice to make with an MPLS_FIB table is: |
| 26 | * 1 - 20 bit key: label only. |
| 27 | * When the EOS and non-EOS actions differ the result is a 'EOS-choice' object. |
| 28 | * 2 - 21 bit key: label and EOS-bit. |
| 29 | * The result is then the specific action based on EOS-bit. |
| 30 | * |
| 31 | * 20 bit key: |
| 32 | * Advantages: |
| 33 | * - lower memory overhead, since there are few DB entries. |
| 34 | * Disadvantages: |
| 35 | * - slower DP performance in the case the chains differ, as more objects are |
| 36 | * encounterd in the switch path |
| 37 | * |
| 38 | * 21 bit key: |
| 39 | * Advantages: |
| 40 | * - faster DP performance |
| 41 | * Disadvantages |
| 42 | * - increased memory footprint. |
| 43 | * |
| 44 | * Switching between schemes based on observed/measured action similarity is not |
| 45 | * considered on the grounds of complexity and flip-flopping. |
| 46 | * |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 47 | * VPP mantra - favour performance over memory. We choose a 21 bit key. |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 48 | */ |
| 49 | |
| 50 | #include <vnet/fib/fib_table.h> |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 51 | #include <vnet/fib/mpls_fib.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 52 | #include <vnet/dpo/load_balance.h> |
| 53 | #include <vnet/dpo/drop_dpo.h> |
| 54 | #include <vnet/dpo/punt_dpo.h> |
| 55 | #include <vnet/dpo/lookup_dpo.h> |
| 56 | #include <vnet/mpls/mpls.h> |
| 57 | |
| 58 | /** |
| 59 | * All lookups in an MPLS_FIB table must result in a DPO of type load-balance. |
| 60 | * This is the default result which links to drop |
| 61 | */ |
| 62 | static index_t mpls_fib_drop_dpo_index = INDEX_INVALID; |
| 63 | |
| 64 | /** |
| 65 | * FIXME |
| 66 | */ |
| 67 | #define MPLS_FLOW_HASH_DEFAULT 0 |
| 68 | |
| 69 | static inline u32 |
| 70 | mpls_fib_entry_mk_key (mpls_label_t label, |
| 71 | mpls_eos_bit_t eos) |
| 72 | { |
| 73 | ASSERT(eos <= 1); |
| 74 | return (label << 1 | eos); |
| 75 | } |
| 76 | |
| 77 | u32 |
| 78 | mpls_fib_index_from_table_id (u32 table_id) |
| 79 | { |
| 80 | mpls_main_t *mm = &mpls_main; |
| 81 | uword * p; |
| 82 | |
| 83 | p = hash_get (mm->fib_index_by_table_id, table_id); |
| 84 | if (!p) |
| 85 | return FIB_NODE_INDEX_INVALID; |
| 86 | |
| 87 | return p[0]; |
| 88 | } |
| 89 | |
| 90 | static u32 |
| 91 | mpls_fib_create_with_table_id (u32 table_id) |
| 92 | { |
Neale Ranns | 948e00f | 2016-10-20 13:39:34 +0100 | [diff] [blame] | 93 | dpo_id_t dpo = DPO_INVALID; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 94 | fib_table_t *fib_table; |
| 95 | mpls_eos_bit_t eos; |
| 96 | mpls_fib_t *mf; |
| 97 | int i; |
| 98 | |
| 99 | pool_get_aligned(mpls_main.fibs, fib_table, CLIB_CACHE_LINE_BYTES); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame^] | 100 | pool_get_aligned(mpls_main.mpls_fibs, mf, CLIB_CACHE_LINE_BYTES); |
| 101 | |
| 102 | ASSERT((fib_table - mpls_main.fibs) == |
| 103 | (mf - mpls_main.mpls_fibs)); |
| 104 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 105 | memset(fib_table, 0, sizeof(*fib_table)); |
| 106 | |
| 107 | fib_table->ft_proto = FIB_PROTOCOL_MPLS; |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame^] | 108 | fib_table->ft_index = (fib_table - mpls_main.fibs); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 109 | |
| 110 | hash_set (mpls_main.fib_index_by_table_id, table_id, fib_table->ft_index); |
| 111 | |
| 112 | fib_table->ft_table_id = |
| 113 | table_id; |
| 114 | fib_table->ft_flow_hash_config = |
| 115 | MPLS_FLOW_HASH_DEFAULT; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 116 | |
| 117 | fib_table_lock(fib_table->ft_index, FIB_PROTOCOL_MPLS); |
| 118 | |
| 119 | if (INDEX_INVALID == mpls_fib_drop_dpo_index) |
| 120 | { |
| 121 | mpls_fib_drop_dpo_index = load_balance_create(1, DPO_PROTO_MPLS, 0); |
| 122 | load_balance_set_bucket(mpls_fib_drop_dpo_index, |
| 123 | 0, |
| 124 | drop_dpo_get(DPO_PROTO_MPLS)); |
| 125 | } |
| 126 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 127 | mf->mf_entries = hash_create(0, sizeof(fib_node_index_t)); |
| 128 | for (i = 0; i < MPLS_FIB_DB_SIZE; i++) |
| 129 | { |
| 130 | /* |
| 131 | * initialise each DPO in the data-path lookup table |
| 132 | * to be the special MPLS drop |
| 133 | */ |
| 134 | mf->mf_lbs[i] = mpls_fib_drop_dpo_index; |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * non-default forwarding for the special labels. |
| 139 | */ |
| 140 | fib_prefix_t prefix = { |
| 141 | .fp_proto = FIB_PROTOCOL_MPLS, |
| 142 | .fp_payload_proto = DPO_PROTO_MPLS, |
| 143 | }; |
| 144 | |
| 145 | /* |
| 146 | * PUNT the router alert, both EOS and non-eos |
| 147 | */ |
| 148 | prefix.fp_label = MPLS_IETF_ROUTER_ALERT_LABEL; |
| 149 | FOR_EACH_MPLS_EOS_BIT(eos) |
| 150 | { |
| 151 | prefix.fp_eos = eos; |
| 152 | fib_table_entry_special_dpo_add(fib_table->ft_index, |
| 153 | &prefix, |
| 154 | FIB_SOURCE_SPECIAL, |
| 155 | FIB_ENTRY_FLAG_EXCLUSIVE, |
| 156 | punt_dpo_get(DPO_PROTO_MPLS)); |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * IPv4 explicit NULL EOS lookup in the interface's IPv4 table |
| 161 | */ |
| 162 | prefix.fp_label = MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL; |
| 163 | prefix.fp_payload_proto = DPO_PROTO_IP4; |
| 164 | prefix.fp_eos = MPLS_EOS; |
| 165 | |
| 166 | lookup_dpo_add_or_lock_w_fib_index(0, // unused |
| 167 | DPO_PROTO_IP4, |
| 168 | LOOKUP_INPUT_DST_ADDR, |
| 169 | LOOKUP_TABLE_FROM_INPUT_INTERFACE, |
| 170 | &dpo); |
| 171 | fib_table_entry_special_dpo_add(fib_table->ft_index, |
| 172 | &prefix, |
| 173 | FIB_SOURCE_SPECIAL, |
| 174 | FIB_ENTRY_FLAG_EXCLUSIVE, |
| 175 | &dpo); |
| 176 | |
| 177 | prefix.fp_payload_proto = DPO_PROTO_MPLS; |
| 178 | prefix.fp_eos = MPLS_NON_EOS; |
| 179 | |
| 180 | lookup_dpo_add_or_lock_w_fib_index(0, //unsued |
| 181 | DPO_PROTO_MPLS, |
| 182 | LOOKUP_INPUT_DST_ADDR, |
| 183 | LOOKUP_TABLE_FROM_INPUT_INTERFACE, |
| 184 | &dpo); |
| 185 | fib_table_entry_special_dpo_add(fib_table->ft_index, |
| 186 | &prefix, |
| 187 | FIB_SOURCE_SPECIAL, |
| 188 | FIB_ENTRY_FLAG_EXCLUSIVE, |
| 189 | &dpo); |
| 190 | |
| 191 | /* |
| 192 | * IPv6 explicit NULL EOS lookup in the interface's IPv6 table |
| 193 | */ |
| 194 | prefix.fp_label = MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL; |
| 195 | prefix.fp_payload_proto = DPO_PROTO_IP6; |
| 196 | prefix.fp_eos = MPLS_EOS; |
| 197 | |
| 198 | lookup_dpo_add_or_lock_w_fib_index(0, //unused |
| 199 | DPO_PROTO_IP6, |
| 200 | LOOKUP_INPUT_DST_ADDR, |
| 201 | LOOKUP_TABLE_FROM_INPUT_INTERFACE, |
| 202 | &dpo); |
| 203 | fib_table_entry_special_dpo_add(fib_table->ft_index, |
| 204 | &prefix, |
| 205 | FIB_SOURCE_SPECIAL, |
| 206 | FIB_ENTRY_FLAG_EXCLUSIVE, |
| 207 | &dpo); |
| 208 | |
| 209 | prefix.fp_payload_proto = DPO_PROTO_MPLS; |
| 210 | prefix.fp_eos = MPLS_NON_EOS; |
| 211 | lookup_dpo_add_or_lock_w_fib_index(0, // unsued |
| 212 | DPO_PROTO_MPLS, |
| 213 | LOOKUP_INPUT_DST_ADDR, |
| 214 | LOOKUP_TABLE_FROM_INPUT_INTERFACE, |
| 215 | &dpo); |
| 216 | fib_table_entry_special_dpo_add(fib_table->ft_index, |
| 217 | &prefix, |
| 218 | FIB_SOURCE_SPECIAL, |
| 219 | FIB_ENTRY_FLAG_EXCLUSIVE, |
| 220 | &dpo); |
| 221 | |
| 222 | return (fib_table->ft_index); |
| 223 | } |
| 224 | |
| 225 | u32 |
| 226 | mpls_fib_table_find_or_create_and_lock (u32 table_id) |
| 227 | { |
| 228 | u32 index; |
| 229 | |
| 230 | index = mpls_fib_index_from_table_id(table_id); |
| 231 | if (~0 == index) |
| 232 | return mpls_fib_create_with_table_id(table_id); |
| 233 | |
| 234 | fib_table_lock(index, FIB_PROTOCOL_MPLS); |
| 235 | |
| 236 | return (index); |
| 237 | } |
| 238 | u32 |
| 239 | mpls_fib_table_create_and_lock (void) |
| 240 | { |
| 241 | return (mpls_fib_create_with_table_id(~0)); |
| 242 | } |
| 243 | |
| 244 | void |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame^] | 245 | mpls_fib_table_destroy (u32 fib_index) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 246 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame^] | 247 | fib_table_t *fib_table = pool_elt_at_index(mpls_main.fibs, fib_index); |
| 248 | mpls_fib_t *mf = pool_elt_at_index(mpls_main.mpls_fibs, fib_index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 249 | fib_prefix_t prefix = { |
| 250 | .fp_proto = FIB_PROTOCOL_MPLS, |
| 251 | }; |
| 252 | mpls_label_t special_labels[] = { |
| 253 | MPLS_IETF_ROUTER_ALERT_LABEL, |
| 254 | MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL, |
| 255 | MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL, |
| 256 | }; |
| 257 | mpls_eos_bit_t eos; |
| 258 | u32 ii; |
| 259 | |
| 260 | for (ii = 0; ii < ARRAY_LEN(special_labels); ii++) |
| 261 | { |
| 262 | FOR_EACH_MPLS_EOS_BIT(eos) |
| 263 | { |
| 264 | prefix.fp_label = special_labels[ii]; |
| 265 | prefix.fp_eos = eos; |
| 266 | |
| 267 | fib_table_entry_delete(fib_table->ft_index, |
| 268 | &prefix, |
| 269 | FIB_SOURCE_SPECIAL); |
| 270 | } |
| 271 | } |
| 272 | if (~0 != fib_table->ft_table_id) |
| 273 | { |
| 274 | hash_unset(mpls_main.fib_index_by_table_id, |
| 275 | fib_table->ft_table_id); |
| 276 | } |
Neale Ranns | bcc6aa4 | 2017-02-24 01:34:14 -0800 | [diff] [blame] | 277 | hash_free(mf->mf_entries); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 278 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame^] | 279 | pool_put(mpls_main.mpls_fibs, mf); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 280 | pool_put(mpls_main.fibs, fib_table); |
| 281 | } |
| 282 | |
| 283 | fib_node_index_t |
| 284 | mpls_fib_table_lookup (const mpls_fib_t *mf, |
| 285 | mpls_label_t label, |
| 286 | mpls_eos_bit_t eos) |
| 287 | { |
| 288 | uword *p; |
| 289 | |
| 290 | p = hash_get(mf->mf_entries, mpls_fib_entry_mk_key(label, eos)); |
| 291 | |
| 292 | if (NULL == p) |
| 293 | return FIB_NODE_INDEX_INVALID; |
| 294 | |
| 295 | return p[0]; |
| 296 | } |
| 297 | |
| 298 | void |
| 299 | mpls_fib_table_entry_insert (mpls_fib_t *mf, |
| 300 | mpls_label_t label, |
| 301 | mpls_eos_bit_t eos, |
| 302 | fib_node_index_t lfei) |
| 303 | { |
| 304 | hash_set(mf->mf_entries, mpls_fib_entry_mk_key(label, eos), lfei); |
| 305 | } |
| 306 | |
| 307 | void |
| 308 | mpls_fib_table_entry_remove (mpls_fib_t *mf, |
| 309 | mpls_label_t label, |
| 310 | mpls_eos_bit_t eos) |
| 311 | { |
| 312 | hash_unset(mf->mf_entries, mpls_fib_entry_mk_key(label, eos)); |
| 313 | } |
| 314 | |
| 315 | void |
| 316 | mpls_fib_forwarding_table_update (mpls_fib_t *mf, |
| 317 | mpls_label_t label, |
| 318 | mpls_eos_bit_t eos, |
| 319 | const dpo_id_t *dpo) |
| 320 | { |
| 321 | mpls_label_t key; |
| 322 | |
| 323 | ASSERT(DPO_LOAD_BALANCE == dpo->dpoi_type); |
| 324 | |
| 325 | key = mpls_fib_entry_mk_key(label, eos); |
| 326 | |
| 327 | mf->mf_lbs[key] = dpo->dpoi_index; |
| 328 | } |
| 329 | |
| 330 | void |
| 331 | mpls_fib_forwarding_table_reset (mpls_fib_t *mf, |
| 332 | mpls_label_t label, |
| 333 | mpls_eos_bit_t eos) |
| 334 | { |
| 335 | mpls_label_t key; |
| 336 | |
| 337 | key = mpls_fib_entry_mk_key(label, eos); |
| 338 | |
| 339 | mf->mf_lbs[key] = mpls_fib_drop_dpo_index; |
| 340 | } |
| 341 | |
| 342 | flow_hash_config_t |
| 343 | mpls_fib_table_get_flow_hash_config (u32 fib_index) |
| 344 | { |
| 345 | // FIXME. |
| 346 | return (0); |
| 347 | } |
| 348 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 349 | void |
| 350 | mpls_fib_table_walk (mpls_fib_t *mpls_fib, |
| 351 | fib_table_walk_fn_t fn, |
| 352 | void *ctx) |
| 353 | { |
| 354 | fib_node_index_t lfei; |
| 355 | mpls_label_t key; |
| 356 | |
| 357 | hash_foreach(key, lfei, mpls_fib->mf_entries, |
| 358 | ({ |
| 359 | fn(lfei, ctx); |
| 360 | })); |
| 361 | } |
| 362 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 363 | static void |
| 364 | mpls_fib_table_show_all (const mpls_fib_t *mpls_fib, |
| 365 | vlib_main_t * vm) |
| 366 | { |
| 367 | fib_node_index_t lfei, *lfeip, *lfeis = NULL; |
| 368 | mpls_label_t key; |
| 369 | |
| 370 | hash_foreach(key, lfei, mpls_fib->mf_entries, |
| 371 | ({ |
| 372 | vec_add1(lfeis, lfei); |
| 373 | })); |
| 374 | |
| 375 | vec_sort_with_function(lfeis, fib_entry_cmp_for_sort); |
| 376 | |
| 377 | vec_foreach(lfeip, lfeis) |
| 378 | { |
| 379 | vlib_cli_output (vm, "%U", |
| 380 | format_fib_entry, *lfeip, |
| 381 | FIB_ENTRY_FORMAT_DETAIL); |
| 382 | } |
| 383 | vec_free(lfeis); |
| 384 | } |
| 385 | |
| 386 | static void |
| 387 | mpls_fib_table_show_one (const mpls_fib_t *mpls_fib, |
| 388 | mpls_label_t label, |
| 389 | vlib_main_t * vm) |
| 390 | { |
| 391 | fib_node_index_t lfei; |
| 392 | mpls_eos_bit_t eos; |
| 393 | |
| 394 | FOR_EACH_MPLS_EOS_BIT(eos) |
| 395 | { |
| 396 | lfei = mpls_fib_table_lookup(mpls_fib, label, eos); |
| 397 | |
| 398 | if (FIB_NODE_INDEX_INVALID != lfei) |
| 399 | { |
| 400 | vlib_cli_output (vm, "%U", |
| 401 | format_fib_entry, lfei, FIB_ENTRY_FORMAT_DETAIL); |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | static clib_error_t * |
| 407 | mpls_fib_show (vlib_main_t * vm, |
| 408 | unformat_input_t * input, |
| 409 | vlib_cli_command_t * cmd) |
| 410 | { |
| 411 | fib_table_t * fib_table; |
| 412 | mpls_label_t label; |
| 413 | int table_id; |
| 414 | |
| 415 | table_id = -1; |
| 416 | label = MPLS_LABEL_INVALID; |
| 417 | |
| 418 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 419 | { |
| 420 | /* if (unformat (input, "brief") || unformat (input, "summary") */ |
| 421 | /* || unformat (input, "sum")) */ |
| 422 | /* verbose = 0; */ |
| 423 | |
| 424 | if (unformat (input, "%d", &label)) |
| 425 | continue; |
| 426 | else if (unformat (input, "table %d", &table_id)) |
| 427 | ; |
| 428 | else |
| 429 | break; |
| 430 | } |
| 431 | |
| 432 | pool_foreach (fib_table, mpls_main.fibs, |
| 433 | ({ |
| 434 | if (table_id >= 0 && table_id != fib_table->ft_table_id) |
| 435 | continue; |
| 436 | |
| 437 | vlib_cli_output (vm, "%v, fib_index %d", |
| 438 | fib_table->ft_desc, mpls_main.fibs - fib_table); |
| 439 | |
| 440 | if (MPLS_LABEL_INVALID == label) |
| 441 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame^] | 442 | mpls_fib_table_show_all(mpls_fib_get(fib_table->ft_index), vm); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 443 | } |
| 444 | else |
| 445 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame^] | 446 | mpls_fib_table_show_one(mpls_fib_get(fib_table->ft_index), label, vm); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 447 | } |
| 448 | })); |
| 449 | |
| 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | VLIB_CLI_COMMAND (mpls_fib_show_command, static) = { |
| 454 | .path = "show mpls fib", |
| 455 | .short_help = "show mpls fib [summary] [table <n>]", |
| 456 | .function = mpls_fib_show, |
| 457 | }; |