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 | #ifndef __MPLS_FIB_TABLE_H__ |
| 19 | #define __MPLS_FIB_TABLE_H__ |
| 20 | |
| 21 | #include <vnet/vnet.h> |
| 22 | #include <vnet/mpls/mpls.h> |
| 23 | #include <vnet/fib/fib_types.h> |
| 24 | #include <vnet/dpo/dpo.h> |
| 25 | #include <vnet/mpls/mpls.h> |
| 26 | #include <vnet/fib/fib_table.h> |
| 27 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 28 | #define MPLS_FIB_DEFAULT_TABLE_ID 0 |
| 29 | |
| 30 | /** |
| 31 | * Type exposure is to allow the DP fast/inlined access |
| 32 | */ |
| 33 | #define MPLS_FIB_KEY_SIZE 21 |
| 34 | #define MPLS_FIB_DB_SIZE (1 << (MPLS_FIB_KEY_SIZE-1)) |
| 35 | |
Neale Ranns | 227038a | 2017-04-21 01:07:59 -0700 | [diff] [blame] | 36 | /** |
Neale Ranns | 0662127 | 2021-02-04 10:54:22 +0000 | [diff] [blame] | 37 | * There are no options for controlling the MPLS flow hash, |
| 38 | * but since it mostly entails using IP data to create one, use that. |
Neale Ranns | 227038a | 2017-04-21 01:07:59 -0700 | [diff] [blame] | 39 | */ |
Neale Ranns | 0662127 | 2021-02-04 10:54:22 +0000 | [diff] [blame] | 40 | #define MPLS_FLOW_HASH_DEFAULT IP_FLOW_HASH_DEFAULT |
Neale Ranns | 227038a | 2017-04-21 01:07:59 -0700 | [diff] [blame] | 41 | |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 42 | typedef struct mpls_fib_t_ |
| 43 | { |
| 44 | /** |
Dave Barach | eb987d3 | 2018-05-03 08:26:39 -0400 | [diff] [blame] | 45 | * Required for pool_get_aligned |
| 46 | */ |
| 47 | CLIB_CACHE_LINE_ALIGN_MARK(cacheline0); |
| 48 | |
| 49 | /** |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 50 | * A hash table of entries. 21 bit key |
| 51 | * Hash table for reduced memory footprint |
| 52 | */ |
| 53 | uword * mf_entries; |
| 54 | |
| 55 | /** |
| 56 | * The load-balance indices keyed by 21 bit label+eos bit. |
Nathan Skrzypczak | da33105 | 2021-09-29 15:28:26 +0200 | [diff] [blame^] | 57 | * A flat array for maximum lookup performance. |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 58 | */ |
| 59 | index_t mf_lbs[MPLS_FIB_DB_SIZE]; |
| 60 | } mpls_fib_t; |
| 61 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 62 | static inline mpls_fib_t* |
| 63 | mpls_fib_get (fib_node_index_t index) |
| 64 | { |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 65 | return (pool_elt_at_index(mpls_main.mpls_fibs, index)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 68 | extern u32 mpls_fib_table_find_or_create_and_lock(u32 table_id, |
| 69 | fib_source_t src); |
| 70 | extern u32 mpls_fib_table_create_and_lock(fib_source_t src); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 71 | // extern mpls_fib_t * mpls_fib_find(u32 table_id); |
| 72 | extern u32 mpls_fib_index_from_table_id(u32 table_id); |
| 73 | |
| 74 | extern u8 *format_mpls_fib_table_name(u8 * s, va_list * args); |
| 75 | |
| 76 | extern fib_node_index_t mpls_fib_table_entry_add_from_ip_fib_entry ( |
| 77 | u32 table_id, |
| 78 | mpls_label_t label, |
| 79 | mpls_eos_bit_t eos, |
| 80 | fib_node_index_t fib_entry_index); |
| 81 | |
| 82 | |
| 83 | extern fib_node_index_t mpls_fib_table_lookup(const mpls_fib_t *mf, |
| 84 | mpls_label_t label, |
| 85 | mpls_eos_bit_t eos); |
| 86 | |
| 87 | extern void mpls_fib_table_entry_remove(mpls_fib_t *mf, |
| 88 | mpls_label_t label, |
| 89 | mpls_eos_bit_t eos); |
| 90 | extern void mpls_fib_table_entry_insert(mpls_fib_t *mf, |
| 91 | mpls_label_t label, |
| 92 | mpls_eos_bit_t eos, |
| 93 | fib_node_index_t fei); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 94 | extern void mpls_fib_table_destroy(u32 fib_index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 95 | |
| 96 | |
| 97 | extern void mpls_fib_forwarding_table_update(mpls_fib_t *mf, |
| 98 | mpls_label_t label, |
| 99 | mpls_eos_bit_t eos, |
| 100 | const dpo_id_t *dpo); |
| 101 | extern void mpls_fib_forwarding_table_reset(mpls_fib_t *mf, |
| 102 | mpls_label_t label, |
| 103 | mpls_eos_bit_t eos); |
| 104 | |
| 105 | /** |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 106 | * @brief Walk all entries in a FIB table |
| 107 | * N.B: This is NOT safe to deletes. If you need to delete walk the whole |
| 108 | * table and store elements in a vector, then delete the elements |
| 109 | */ |
| 110 | extern void mpls_fib_table_walk(mpls_fib_t *fib, |
| 111 | fib_table_walk_fn_t fn, |
| 112 | void *ctx); |
| 113 | |
Neale Ranns | c87aafa | 2017-11-29 00:59:31 -0800 | [diff] [blame] | 114 | extern u8 *format_mpls_fib_table_memory(u8 * s, va_list * args); |
| 115 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 116 | /** |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 117 | * @brief |
| 118 | * Lookup a label and EOS bit in the MPLS_FIB table to retrieve the |
| 119 | * load-balance index to be used for packet forwarding. |
| 120 | */ |
| 121 | static inline index_t |
| 122 | mpls_fib_table_forwarding_lookup (u32 mpls_fib_index, |
| 123 | const mpls_unicast_header_t *hdr) |
| 124 | { |
| 125 | mpls_label_t label; |
| 126 | mpls_fib_t *mf; |
| 127 | u32 key; |
| 128 | |
| 129 | label = clib_net_to_host_u32(hdr->label_exp_s_ttl); |
| 130 | key = (vnet_mpls_uc_get_label(label) << 1) | vnet_mpls_uc_get_s(label); |
| 131 | |
| 132 | mf = mpls_fib_get(mpls_fib_index); |
| 133 | |
| 134 | return (mf->mf_lbs[key]); |
| 135 | } |
| 136 | |
| 137 | static inline u32 |
| 138 | mpls_fib_table_get_index_for_sw_if_index (u32 sw_if_index) |
| 139 | { |
| 140 | mpls_main_t *mm = &mpls_main; |
| 141 | |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 142 | ASSERT(vec_len(mm->fib_index_by_sw_if_index) > sw_if_index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 143 | |
| 144 | return (mm->fib_index_by_sw_if_index[sw_if_index]); |
| 145 | } |
| 146 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 147 | #endif |