blob: 8d18b008b0e0bf1e066bf1af6564be77dd7d0672 [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#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 Rannsa3af3372017-03-28 03:49:52 -070028#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 Ranns227038a2017-04-21 01:07:59 -070036/**
37 * There are no options for controlling the MPLS flow hash
38 */
39#define MPLS_FLOW_HASH_DEFAULT 0
40
Neale Rannsa3af3372017-03-28 03:49:52 -070041typedef struct mpls_fib_t_
42{
43 /**
Dave Baracheb987d32018-05-03 08:26:39 -040044 * Required for pool_get_aligned
45 */
46 CLIB_CACHE_LINE_ALIGN_MARK(cacheline0);
47
48 /**
Neale Rannsa3af3372017-03-28 03:49:52 -070049 * A hash table of entries. 21 bit key
50 * Hash table for reduced memory footprint
51 */
52 uword * mf_entries;
53
54 /**
55 * The load-balance indices keyed by 21 bit label+eos bit.
56 * A flat array for maximum lookup performace.
57 */
58 index_t mf_lbs[MPLS_FIB_DB_SIZE];
59} mpls_fib_t;
60
Neale Ranns0bfe5d82016-08-25 15:29:12 +010061static inline mpls_fib_t*
62mpls_fib_get (fib_node_index_t index)
63{
Neale Rannsa3af3372017-03-28 03:49:52 -070064 return (pool_elt_at_index(mpls_main.mpls_fibs, index));
Neale Ranns0bfe5d82016-08-25 15:29:12 +010065}
66
Neale Ranns15002542017-09-10 04:39:11 -070067extern u32 mpls_fib_table_find_or_create_and_lock(u32 table_id,
68 fib_source_t src);
69extern u32 mpls_fib_table_create_and_lock(fib_source_t src);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010070// extern mpls_fib_t * mpls_fib_find(u32 table_id);
71extern u32 mpls_fib_index_from_table_id(u32 table_id);
72
73extern u8 *format_mpls_fib_table_name(u8 * s, va_list * args);
74
75extern fib_node_index_t mpls_fib_table_entry_add_from_ip_fib_entry (
76 u32 table_id,
77 mpls_label_t label,
78 mpls_eos_bit_t eos,
79 fib_node_index_t fib_entry_index);
80
81
82extern fib_node_index_t mpls_fib_table_lookup(const mpls_fib_t *mf,
83 mpls_label_t label,
84 mpls_eos_bit_t eos);
85
86extern void mpls_fib_table_entry_remove(mpls_fib_t *mf,
87 mpls_label_t label,
88 mpls_eos_bit_t eos);
89extern void mpls_fib_table_entry_insert(mpls_fib_t *mf,
90 mpls_label_t label,
91 mpls_eos_bit_t eos,
92 fib_node_index_t fei);
Neale Rannsa3af3372017-03-28 03:49:52 -070093extern void mpls_fib_table_destroy(u32 fib_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010094
95
96extern void mpls_fib_forwarding_table_update(mpls_fib_t *mf,
97 mpls_label_t label,
98 mpls_eos_bit_t eos,
99 const dpo_id_t *dpo);
100extern void mpls_fib_forwarding_table_reset(mpls_fib_t *mf,
101 mpls_label_t label,
102 mpls_eos_bit_t eos);
103
104/**
Neale Ranns32e1c012016-11-22 17:07:28 +0000105 * @brief Walk all entries in a FIB table
106 * N.B: This is NOT safe to deletes. If you need to delete walk the whole
107 * table and store elements in a vector, then delete the elements
108 */
109extern void mpls_fib_table_walk(mpls_fib_t *fib,
110 fib_table_walk_fn_t fn,
111 void *ctx);
112
Neale Rannsc87aafa2017-11-29 00:59:31 -0800113extern u8 *format_mpls_fib_table_memory(u8 * s, va_list * args);
114
Neale Ranns32e1c012016-11-22 17:07:28 +0000115/**
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100116 * @brief
117 * Lookup a label and EOS bit in the MPLS_FIB table to retrieve the
118 * load-balance index to be used for packet forwarding.
119 */
120static inline index_t
121mpls_fib_table_forwarding_lookup (u32 mpls_fib_index,
122 const mpls_unicast_header_t *hdr)
123{
124 mpls_label_t label;
125 mpls_fib_t *mf;
126 u32 key;
127
128 label = clib_net_to_host_u32(hdr->label_exp_s_ttl);
129 key = (vnet_mpls_uc_get_label(label) << 1) | vnet_mpls_uc_get_s(label);
130
131 mf = mpls_fib_get(mpls_fib_index);
132
133 return (mf->mf_lbs[key]);
134}
135
136static inline u32
137mpls_fib_table_get_index_for_sw_if_index (u32 sw_if_index)
138{
139 mpls_main_t *mm = &mpls_main;
140
Neale Rannsad422ed2016-11-02 14:20:04 +0000141 ASSERT(vec_len(mm->fib_index_by_sw_if_index) > sw_if_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100142
143 return (mm->fib_index_by_sw_if_index[sw_if_index]);
144}
145
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100146#endif