2 * mpls_fib.h: The Label/MPLS FIB
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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.
33 * - lower memory overhead, since there are few DB entries.
35 * - slower DP performance in the case the chains differ, as more objects are
36 * encounterd in the switch path
40 * - faster DP performance
42 * - increased memory footprint.
44 * Switching between schemes based on observed/measured action similarity is not
45 * considered on the grounds of complexity and flip-flopping.
47 * VPP mantra - favour performance over memory. We choose a 21 bit key.
50 #include <vnet/fib/fib_table.h>
51 #include <vnet/fib/mpls_fib.h>
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>
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
62 static index_t mpls_fib_drop_dpo_index = INDEX_INVALID;
65 mpls_fib_entry_mk_key (mpls_label_t label,
69 return (label << 1 | eos);
73 mpls_fib_index_from_table_id (u32 table_id)
75 mpls_main_t *mm = &mpls_main;
78 p = hash_get (mm->fib_index_by_table_id, table_id);
80 return FIB_NODE_INDEX_INVALID;
86 mpls_fib_create_with_table_id (u32 table_id)
88 dpo_id_t dpo = DPO_INVALID;
89 fib_table_t *fib_table;
94 pool_get_aligned(mpls_main.fibs, fib_table, CLIB_CACHE_LINE_BYTES);
95 pool_get_aligned(mpls_main.mpls_fibs, mf, CLIB_CACHE_LINE_BYTES);
97 ASSERT((fib_table - mpls_main.fibs) ==
98 (mf - mpls_main.mpls_fibs));
100 memset(fib_table, 0, sizeof(*fib_table));
102 fib_table->ft_proto = FIB_PROTOCOL_MPLS;
103 fib_table->ft_index = (fib_table - mpls_main.fibs);
105 hash_set (mpls_main.fib_index_by_table_id, table_id, fib_table->ft_index);
107 fib_table->ft_table_id = table_id;
108 fib_table->ft_flow_hash_config = MPLS_FLOW_HASH_DEFAULT;
110 fib_table_lock(fib_table->ft_index, FIB_PROTOCOL_MPLS);
112 if (INDEX_INVALID == mpls_fib_drop_dpo_index)
114 mpls_fib_drop_dpo_index = load_balance_create(1, DPO_PROTO_MPLS, 0);
115 load_balance_set_bucket(mpls_fib_drop_dpo_index,
117 drop_dpo_get(DPO_PROTO_MPLS));
120 mf->mf_entries = hash_create(0, sizeof(fib_node_index_t));
121 for (i = 0; i < MPLS_FIB_DB_SIZE; i++)
124 * initialise each DPO in the data-path lookup table
125 * to be the special MPLS drop
127 mf->mf_lbs[i] = mpls_fib_drop_dpo_index;
131 * non-default forwarding for the special labels.
133 fib_prefix_t prefix = {
134 .fp_proto = FIB_PROTOCOL_MPLS,
135 .fp_payload_proto = DPO_PROTO_MPLS,
139 * PUNT the router alert, both EOS and non-eos
141 prefix.fp_label = MPLS_IETF_ROUTER_ALERT_LABEL;
142 FOR_EACH_MPLS_EOS_BIT(eos)
145 fib_table_entry_special_dpo_add(fib_table->ft_index,
148 FIB_ENTRY_FLAG_EXCLUSIVE,
149 punt_dpo_get(DPO_PROTO_MPLS));
153 * IPv4 explicit NULL EOS lookup in the interface's IPv4 table
155 prefix.fp_label = MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL;
156 prefix.fp_payload_proto = DPO_PROTO_IP4;
157 prefix.fp_eos = MPLS_EOS;
159 lookup_dpo_add_or_lock_w_fib_index(0, // unused
162 LOOKUP_INPUT_DST_ADDR,
163 LOOKUP_TABLE_FROM_INPUT_INTERFACE,
165 fib_table_entry_special_dpo_add(fib_table->ft_index,
168 FIB_ENTRY_FLAG_EXCLUSIVE,
171 prefix.fp_payload_proto = DPO_PROTO_MPLS;
172 prefix.fp_eos = MPLS_NON_EOS;
174 lookup_dpo_add_or_lock_w_fib_index(0, //unsued
177 LOOKUP_INPUT_DST_ADDR,
178 LOOKUP_TABLE_FROM_INPUT_INTERFACE,
180 fib_table_entry_special_dpo_add(fib_table->ft_index,
183 FIB_ENTRY_FLAG_EXCLUSIVE,
187 * IPv6 explicit NULL EOS lookup in the interface's IPv6 table
189 prefix.fp_label = MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL;
190 prefix.fp_payload_proto = DPO_PROTO_IP6;
191 prefix.fp_eos = MPLS_EOS;
193 lookup_dpo_add_or_lock_w_fib_index(0, //unused
196 LOOKUP_INPUT_DST_ADDR,
197 LOOKUP_TABLE_FROM_INPUT_INTERFACE,
199 fib_table_entry_special_dpo_add(fib_table->ft_index,
202 FIB_ENTRY_FLAG_EXCLUSIVE,
205 prefix.fp_payload_proto = DPO_PROTO_MPLS;
206 prefix.fp_eos = MPLS_NON_EOS;
207 lookup_dpo_add_or_lock_w_fib_index(0, // unsued
210 LOOKUP_INPUT_DST_ADDR,
211 LOOKUP_TABLE_FROM_INPUT_INTERFACE,
213 fib_table_entry_special_dpo_add(fib_table->ft_index,
216 FIB_ENTRY_FLAG_EXCLUSIVE,
219 return (fib_table->ft_index);
223 mpls_fib_table_find_or_create_and_lock (u32 table_id)
227 index = mpls_fib_index_from_table_id(table_id);
229 return mpls_fib_create_with_table_id(table_id);
231 fib_table_lock(index, FIB_PROTOCOL_MPLS);
236 mpls_fib_table_create_and_lock (void)
238 return (mpls_fib_create_with_table_id(~0));
242 mpls_fib_table_destroy (u32 fib_index)
244 fib_table_t *fib_table = pool_elt_at_index(mpls_main.fibs, fib_index);
245 mpls_fib_t *mf = pool_elt_at_index(mpls_main.mpls_fibs, fib_index);
246 fib_prefix_t prefix = {
247 .fp_proto = FIB_PROTOCOL_MPLS,
249 mpls_label_t special_labels[] = {
250 MPLS_IETF_ROUTER_ALERT_LABEL,
251 MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL,
252 MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL,
257 for (ii = 0; ii < ARRAY_LEN(special_labels); ii++)
259 FOR_EACH_MPLS_EOS_BIT(eos)
261 prefix.fp_label = special_labels[ii];
264 fib_table_entry_delete(fib_table->ft_index,
269 if (~0 != fib_table->ft_table_id)
271 hash_unset(mpls_main.fib_index_by_table_id,
272 fib_table->ft_table_id);
274 hash_free(mf->mf_entries);
276 pool_put(mpls_main.mpls_fibs, mf);
277 pool_put(mpls_main.fibs, fib_table);
281 mpls_fib_table_lookup (const mpls_fib_t *mf,
287 p = hash_get(mf->mf_entries, mpls_fib_entry_mk_key(label, eos));
290 return FIB_NODE_INDEX_INVALID;
296 mpls_fib_table_entry_insert (mpls_fib_t *mf,
299 fib_node_index_t lfei)
301 hash_set(mf->mf_entries, mpls_fib_entry_mk_key(label, eos), lfei);
305 mpls_fib_table_entry_remove (mpls_fib_t *mf,
309 hash_unset(mf->mf_entries, mpls_fib_entry_mk_key(label, eos));
313 mpls_fib_forwarding_table_update (mpls_fib_t *mf,
320 ASSERT((DPO_LOAD_BALANCE == dpo->dpoi_type) ||
321 (DPO_REPLICATE == dpo->dpoi_type));
324 if (DPO_REPLICATE == dpo->dpoi_type)
325 ASSERT(dpo->dpoi_index & MPLS_IS_REPLICATE);
326 if (DPO_LOAD_BALANCE == dpo->dpoi_type)
327 ASSERT(!(dpo->dpoi_index & MPLS_IS_REPLICATE));
329 key = mpls_fib_entry_mk_key(label, eos);
331 mf->mf_lbs[key] = dpo->dpoi_index;
335 mpls_fib_forwarding_table_reset (mpls_fib_t *mf,
341 key = mpls_fib_entry_mk_key(label, eos);
343 mf->mf_lbs[key] = mpls_fib_drop_dpo_index;
347 mpls_fib_table_walk (mpls_fib_t *mpls_fib,
348 fib_table_walk_fn_t fn,
351 fib_node_index_t lfei;
354 hash_foreach(key, lfei, mpls_fib->mf_entries,
361 mpls_fib_table_show_all (const mpls_fib_t *mpls_fib,
364 fib_node_index_t lfei, *lfeip, *lfeis = NULL;
367 hash_foreach(key, lfei, mpls_fib->mf_entries,
369 vec_add1(lfeis, lfei);
372 vec_sort_with_function(lfeis, fib_entry_cmp_for_sort);
374 vec_foreach(lfeip, lfeis)
376 vlib_cli_output (vm, "%U",
377 format_fib_entry, *lfeip,
378 FIB_ENTRY_FORMAT_DETAIL);
384 mpls_fib_table_show_one (const mpls_fib_t *mpls_fib,
388 fib_node_index_t lfei;
391 FOR_EACH_MPLS_EOS_BIT(eos)
393 lfei = mpls_fib_table_lookup(mpls_fib, label, eos);
395 if (FIB_NODE_INDEX_INVALID != lfei)
397 vlib_cli_output (vm, "%U",
398 format_fib_entry, lfei, FIB_ENTRY_FORMAT_DETAIL);
403 static clib_error_t *
404 mpls_fib_show (vlib_main_t * vm,
405 unformat_input_t * input,
406 vlib_cli_command_t * cmd)
408 fib_table_t * fib_table;
413 label = MPLS_LABEL_INVALID;
415 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
417 /* if (unformat (input, "brief") || unformat (input, "summary") */
418 /* || unformat (input, "sum")) */
421 if (unformat (input, "%d", &label))
423 else if (unformat (input, "table %d", &table_id))
429 pool_foreach (fib_table, mpls_main.fibs,
431 if (table_id >= 0 && table_id != fib_table->ft_table_id)
434 vlib_cli_output (vm, "%v, fib_index %d",
435 fib_table->ft_desc, mpls_main.fibs - fib_table);
437 if (MPLS_LABEL_INVALID == label)
439 mpls_fib_table_show_all(mpls_fib_get(fib_table->ft_index), vm);
443 mpls_fib_table_show_one(mpls_fib_get(fib_table->ft_index), label, vm);
450 VLIB_CLI_COMMAND (mpls_fib_show_command, static) = {
451 .path = "show mpls fib",
452 .short_help = "show mpls fib [summary] [table <n>]",
453 .function = mpls_fib_show,