blob: 5cd0fd23f9263d8c3d2b1094170835dc14f99935 [file] [log] [blame]
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001/*
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 Ranns32e1c012016-11-22 17:07:28 +000047 * VPP mantra - favour performance over memory. We choose a 21 bit key.
Neale Ranns0bfe5d82016-08-25 15:29:12 +010048 */
49
50#include <vnet/fib/fib_table.h>
Neale Ranns32e1c012016-11-22 17:07:28 +000051#include <vnet/fib/mpls_fib.h>
Neale Ranns0bfe5d82016-08-25 15:29:12 +010052#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 */
62static index_t mpls_fib_drop_dpo_index = INDEX_INVALID;
63
64/**
65 * FIXME
66 */
67#define MPLS_FLOW_HASH_DEFAULT 0
68
69static inline u32
70mpls_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
77u32
78mpls_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
90static u32
91mpls_fib_create_with_table_id (u32 table_id)
92{
Neale Ranns948e00f2016-10-20 13:39:34 +010093 dpo_id_t dpo = DPO_INVALID;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010094 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);
100 memset(fib_table, 0, sizeof(*fib_table));
101
102 fib_table->ft_proto = FIB_PROTOCOL_MPLS;
103 fib_table->ft_index =
104 (fib_table - mpls_main.fibs);
105
106 hash_set (mpls_main.fib_index_by_table_id, table_id, fib_table->ft_index);
107
108 fib_table->ft_table_id =
109 table_id;
110 fib_table->ft_flow_hash_config =
111 MPLS_FLOW_HASH_DEFAULT;
112 fib_table->v4.fwd_classify_table_index = ~0;
113 fib_table->v4.rev_classify_table_index = ~0;
114
115 fib_table_lock(fib_table->ft_index, FIB_PROTOCOL_MPLS);
116
117 if (INDEX_INVALID == mpls_fib_drop_dpo_index)
118 {
119 mpls_fib_drop_dpo_index = load_balance_create(1, DPO_PROTO_MPLS, 0);
120 load_balance_set_bucket(mpls_fib_drop_dpo_index,
121 0,
122 drop_dpo_get(DPO_PROTO_MPLS));
123 }
124
125 mf = &fib_table->mpls;
126 mf->mf_entries = hash_create(0, sizeof(fib_node_index_t));
127 for (i = 0; i < MPLS_FIB_DB_SIZE; i++)
128 {
129 /*
130 * initialise each DPO in the data-path lookup table
131 * to be the special MPLS drop
132 */
133 mf->mf_lbs[i] = mpls_fib_drop_dpo_index;
134 }
135
136 /*
137 * non-default forwarding for the special labels.
138 */
139 fib_prefix_t prefix = {
140 .fp_proto = FIB_PROTOCOL_MPLS,
141 .fp_payload_proto = DPO_PROTO_MPLS,
142 };
143
144 /*
145 * PUNT the router alert, both EOS and non-eos
146 */
147 prefix.fp_label = MPLS_IETF_ROUTER_ALERT_LABEL;
148 FOR_EACH_MPLS_EOS_BIT(eos)
149 {
150 prefix.fp_eos = eos;
151 fib_table_entry_special_dpo_add(fib_table->ft_index,
152 &prefix,
153 FIB_SOURCE_SPECIAL,
154 FIB_ENTRY_FLAG_EXCLUSIVE,
155 punt_dpo_get(DPO_PROTO_MPLS));
156 }
157
158 /*
159 * IPv4 explicit NULL EOS lookup in the interface's IPv4 table
160 */
161 prefix.fp_label = MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL;
162 prefix.fp_payload_proto = DPO_PROTO_IP4;
163 prefix.fp_eos = MPLS_EOS;
164
165 lookup_dpo_add_or_lock_w_fib_index(0, // unused
166 DPO_PROTO_IP4,
167 LOOKUP_INPUT_DST_ADDR,
168 LOOKUP_TABLE_FROM_INPUT_INTERFACE,
169 &dpo);
170 fib_table_entry_special_dpo_add(fib_table->ft_index,
171 &prefix,
172 FIB_SOURCE_SPECIAL,
173 FIB_ENTRY_FLAG_EXCLUSIVE,
174 &dpo);
175
176 prefix.fp_payload_proto = DPO_PROTO_MPLS;
177 prefix.fp_eos = MPLS_NON_EOS;
178
179 lookup_dpo_add_or_lock_w_fib_index(0, //unsued
180 DPO_PROTO_MPLS,
181 LOOKUP_INPUT_DST_ADDR,
182 LOOKUP_TABLE_FROM_INPUT_INTERFACE,
183 &dpo);
184 fib_table_entry_special_dpo_add(fib_table->ft_index,
185 &prefix,
186 FIB_SOURCE_SPECIAL,
187 FIB_ENTRY_FLAG_EXCLUSIVE,
188 &dpo);
189
190 /*
191 * IPv6 explicit NULL EOS lookup in the interface's IPv6 table
192 */
193 prefix.fp_label = MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL;
194 prefix.fp_payload_proto = DPO_PROTO_IP6;
195 prefix.fp_eos = MPLS_EOS;
196
197 lookup_dpo_add_or_lock_w_fib_index(0, //unused
198 DPO_PROTO_IP6,
199 LOOKUP_INPUT_DST_ADDR,
200 LOOKUP_TABLE_FROM_INPUT_INTERFACE,
201 &dpo);
202 fib_table_entry_special_dpo_add(fib_table->ft_index,
203 &prefix,
204 FIB_SOURCE_SPECIAL,
205 FIB_ENTRY_FLAG_EXCLUSIVE,
206 &dpo);
207
208 prefix.fp_payload_proto = DPO_PROTO_MPLS;
209 prefix.fp_eos = MPLS_NON_EOS;
210 lookup_dpo_add_or_lock_w_fib_index(0, // unsued
211 DPO_PROTO_MPLS,
212 LOOKUP_INPUT_DST_ADDR,
213 LOOKUP_TABLE_FROM_INPUT_INTERFACE,
214 &dpo);
215 fib_table_entry_special_dpo_add(fib_table->ft_index,
216 &prefix,
217 FIB_SOURCE_SPECIAL,
218 FIB_ENTRY_FLAG_EXCLUSIVE,
219 &dpo);
220
221 return (fib_table->ft_index);
222}
223
224u32
225mpls_fib_table_find_or_create_and_lock (u32 table_id)
226{
227 u32 index;
228
229 index = mpls_fib_index_from_table_id(table_id);
230 if (~0 == index)
231 return mpls_fib_create_with_table_id(table_id);
232
233 fib_table_lock(index, FIB_PROTOCOL_MPLS);
234
235 return (index);
236}
237u32
238mpls_fib_table_create_and_lock (void)
239{
240 return (mpls_fib_create_with_table_id(~0));
241}
242
243void
244mpls_fib_table_destroy (mpls_fib_t *mf)
245{
246 fib_table_t *fib_table = (fib_table_t*)mf;
247 fib_prefix_t prefix = {
248 .fp_proto = FIB_PROTOCOL_MPLS,
249 };
250 mpls_label_t special_labels[] = {
251 MPLS_IETF_ROUTER_ALERT_LABEL,
252 MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL,
253 MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL,
254 };
255 mpls_eos_bit_t eos;
256 u32 ii;
257
258 for (ii = 0; ii < ARRAY_LEN(special_labels); ii++)
259 {
260 FOR_EACH_MPLS_EOS_BIT(eos)
261 {
262 prefix.fp_label = special_labels[ii];
263 prefix.fp_eos = eos;
264
265 fib_table_entry_delete(fib_table->ft_index,
266 &prefix,
267 FIB_SOURCE_SPECIAL);
268 }
269 }
270 if (~0 != fib_table->ft_table_id)
271 {
272 hash_unset(mpls_main.fib_index_by_table_id,
273 fib_table->ft_table_id);
274 }
Neale Rannsbcc6aa42017-02-24 01:34:14 -0800275 hash_free(mf->mf_entries);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100276
277 pool_put(mpls_main.fibs, fib_table);
278}
279
280fib_node_index_t
281mpls_fib_table_lookup (const mpls_fib_t *mf,
282 mpls_label_t label,
283 mpls_eos_bit_t eos)
284{
285 uword *p;
286
287 p = hash_get(mf->mf_entries, mpls_fib_entry_mk_key(label, eos));
288
289 if (NULL == p)
290 return FIB_NODE_INDEX_INVALID;
291
292 return p[0];
293}
294
295void
296mpls_fib_table_entry_insert (mpls_fib_t *mf,
297 mpls_label_t label,
298 mpls_eos_bit_t eos,
299 fib_node_index_t lfei)
300{
301 hash_set(mf->mf_entries, mpls_fib_entry_mk_key(label, eos), lfei);
302}
303
304void
305mpls_fib_table_entry_remove (mpls_fib_t *mf,
306 mpls_label_t label,
307 mpls_eos_bit_t eos)
308{
309 hash_unset(mf->mf_entries, mpls_fib_entry_mk_key(label, eos));
310}
311
312void
313mpls_fib_forwarding_table_update (mpls_fib_t *mf,
314 mpls_label_t label,
315 mpls_eos_bit_t eos,
316 const dpo_id_t *dpo)
317{
318 mpls_label_t key;
319
320 ASSERT(DPO_LOAD_BALANCE == dpo->dpoi_type);
321
322 key = mpls_fib_entry_mk_key(label, eos);
323
324 mf->mf_lbs[key] = dpo->dpoi_index;
325}
326
327void
328mpls_fib_forwarding_table_reset (mpls_fib_t *mf,
329 mpls_label_t label,
330 mpls_eos_bit_t eos)
331{
332 mpls_label_t key;
333
334 key = mpls_fib_entry_mk_key(label, eos);
335
336 mf->mf_lbs[key] = mpls_fib_drop_dpo_index;
337}
338
339flow_hash_config_t
340mpls_fib_table_get_flow_hash_config (u32 fib_index)
341{
342 // FIXME.
343 return (0);
344}
345
Neale Ranns32e1c012016-11-22 17:07:28 +0000346void
347mpls_fib_table_walk (mpls_fib_t *mpls_fib,
348 fib_table_walk_fn_t fn,
349 void *ctx)
350{
351 fib_node_index_t lfei;
352 mpls_label_t key;
353
354 hash_foreach(key, lfei, mpls_fib->mf_entries,
355 ({
356 fn(lfei, ctx);
357 }));
358}
359
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100360static void
361mpls_fib_table_show_all (const mpls_fib_t *mpls_fib,
362 vlib_main_t * vm)
363{
364 fib_node_index_t lfei, *lfeip, *lfeis = NULL;
365 mpls_label_t key;
366
367 hash_foreach(key, lfei, mpls_fib->mf_entries,
368 ({
369 vec_add1(lfeis, lfei);
370 }));
371
372 vec_sort_with_function(lfeis, fib_entry_cmp_for_sort);
373
374 vec_foreach(lfeip, lfeis)
375 {
376 vlib_cli_output (vm, "%U",
377 format_fib_entry, *lfeip,
378 FIB_ENTRY_FORMAT_DETAIL);
379 }
380 vec_free(lfeis);
381}
382
383static void
384mpls_fib_table_show_one (const mpls_fib_t *mpls_fib,
385 mpls_label_t label,
386 vlib_main_t * vm)
387{
388 fib_node_index_t lfei;
389 mpls_eos_bit_t eos;
390
391 FOR_EACH_MPLS_EOS_BIT(eos)
392 {
393 lfei = mpls_fib_table_lookup(mpls_fib, label, eos);
394
395 if (FIB_NODE_INDEX_INVALID != lfei)
396 {
397 vlib_cli_output (vm, "%U",
398 format_fib_entry, lfei, FIB_ENTRY_FORMAT_DETAIL);
399 }
400 }
401}
402
403static clib_error_t *
404mpls_fib_show (vlib_main_t * vm,
405 unformat_input_t * input,
406 vlib_cli_command_t * cmd)
407{
408 fib_table_t * fib_table;
409 mpls_label_t label;
410 int table_id;
411
412 table_id = -1;
413 label = MPLS_LABEL_INVALID;
414
415 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
416 {
417 /* if (unformat (input, "brief") || unformat (input, "summary") */
418 /* || unformat (input, "sum")) */
419 /* verbose = 0; */
420
421 if (unformat (input, "%d", &label))
422 continue;
423 else if (unformat (input, "table %d", &table_id))
424 ;
425 else
426 break;
427 }
428
429 pool_foreach (fib_table, mpls_main.fibs,
430 ({
431 if (table_id >= 0 && table_id != fib_table->ft_table_id)
432 continue;
433
434 vlib_cli_output (vm, "%v, fib_index %d",
435 fib_table->ft_desc, mpls_main.fibs - fib_table);
436
437 if (MPLS_LABEL_INVALID == label)
438 {
439 mpls_fib_table_show_all(&(fib_table->mpls), vm);
440 }
441 else
442 {
443 mpls_fib_table_show_one(&(fib_table->mpls), label, vm);
444 }
445 }));
446
447 return 0;
448}
449
450VLIB_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,
454};