blob: 7fc2d3f53c958b3e9f25cd23bb58270a3dd3eb52 [file] [log] [blame]
Neale Ranns0bfe5d82016-08-25 15:29:12 +01001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/**
16 * @brief The IPv4 FIB
17 *
18 * FIBs are composed of two prefix data-bases (akak tables). The non-forwarding
19 * table contains all the routes that the control plane has programmed, the
20 * forwarding table contains the sub-set of those routes that can be used to
21 * forward packets.
22 * In the IPv4 FIB the non-forwarding table is an array of hash tables indexed
23 * by mask length, the forwarding table is an mtrie
24 *
25 * This IPv4 FIB is used by the protocol independent FIB. So directly using
26 * this APIs in client code is not encouraged. However, this IPv4 FIB can be
27 * used if all the client wants is an IPv4 prefix data-base
28 */
29
30#ifndef __IP4_FIB_H__
31#define __IP4_FIB_H__
32
33#include <vlib/vlib.h>
34#include <vnet/ip/ip.h>
35#include <vnet/fib/fib_entry.h>
36#include <vnet/fib/fib_table.h>
Neale Rannsa3af3372017-03-28 03:49:52 -070037#include <vnet/ip/ip4_mtrie.h>
38
39typedef struct ip4_fib_t_
40{
41 /**
42 * Mtrie for fast lookups. Hash is used to maintain overlapping prefixes.
43 * First member so it's in the first cacheline.
44 */
45 ip4_fib_mtrie_t mtrie;
46
47 /* Hash table for each prefix length mapping. */
48 uword *fib_entry_by_dst_address[33];
49
50 /* Table ID (hash key) for this FIB. */
51 u32 table_id;
52
53 /* Index into FIB vector. */
54 u32 index;
55
Neale Rannsa3af3372017-03-28 03:49:52 -070056 /* N-tuple classifier indices */
57 u32 fwd_classify_table_index;
58 u32 rev_classify_table_index;
59
Dave Baracheb987d32018-05-03 08:26:39 -040060 /* Required for pool_get_aligned */
61 CLIB_CACHE_LINE_ALIGN_MARK(cacheline0);
Neale Rannsa3af3372017-03-28 03:49:52 -070062} ip4_fib_t;
Neale Ranns0bfe5d82016-08-25 15:29:12 +010063
64extern fib_node_index_t ip4_fib_table_lookup(const ip4_fib_t *fib,
65 const ip4_address_t *addr,
66 u32 len);
67extern fib_node_index_t ip4_fib_table_lookup_exact_match(const ip4_fib_t *fib,
68 const ip4_address_t *addr,
69 u32 len);
70
71extern void ip4_fib_table_entry_remove(ip4_fib_t *fib,
72 const ip4_address_t *addr,
73 u32 len);
74
75extern void ip4_fib_table_entry_insert(ip4_fib_t *fib,
76 const ip4_address_t *addr,
77 u32 len,
78 fib_node_index_t fib_entry_index);
Neale Rannsa3af3372017-03-28 03:49:52 -070079extern void ip4_fib_table_destroy(u32 fib_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010080
81extern void ip4_fib_table_fwding_dpo_update(ip4_fib_t *fib,
82 const ip4_address_t *addr,
83 u32 len,
84 const dpo_id_t *dpo);
85
86extern void ip4_fib_table_fwding_dpo_remove(ip4_fib_t *fib,
87 const ip4_address_t *addr,
88 u32 len,
Neale Rannsa3af3372017-03-28 03:49:52 -070089 const dpo_id_t *dpo,
90 fib_node_index_t cover_index);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010091extern u32 ip4_fib_table_lookup_lb (ip4_fib_t *fib,
92 const ip4_address_t * dst);
93
94/**
Neale Ranns32e1c012016-11-22 17:07:28 +000095 * @brief Walk all entries in a FIB table
96 * N.B: This is NOT safe to deletes. If you need to delete walk the whole
97 * table and store elements in a vector, then delete the elements
98 */
99extern void ip4_fib_table_walk(ip4_fib_t *fib,
100 fib_table_walk_fn_t fn,
101 void *ctx);
102
103/**
Neale Ranns89541992017-04-06 04:41:02 -0700104 * @brief Walk all entries in a sub-tree of the FIB table
105 * N.B: This is NOT safe to deletes. If you need to delete walk the whole
106 * table and store elements in a vector, then delete the elements
107 */
108extern void ip4_fib_table_sub_tree_walk(ip4_fib_t *fib,
109 const fib_prefix_t *root,
110 fib_table_walk_fn_t fn,
111 void *ctx);
112
113/**
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100114 * @brief Get the FIB at the given index
115 */
116static inline ip4_fib_t *
117ip4_fib_get (u32 index)
118{
Neale Rannsa3af3372017-03-28 03:49:52 -0700119 return (pool_elt_at_index(ip4_main.v4_fibs, index));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100120}
121
122always_inline u32
123ip4_fib_lookup (ip4_main_t * im, u32 sw_if_index, ip4_address_t * dst)
124{
125 return (ip4_fib_table_lookup_lb(
126 ip4_fib_get(vec_elt (im->fib_index_by_sw_if_index, sw_if_index)),
127 dst));
128}
129
130/**
131 * @brief Get or create an IPv4 fib.
132 *
133 * Get or create an IPv4 fib with the provided table ID.
134 *
135 * @param table_id
136 * When set to \c ~0, an arbitrary and unused fib ID is picked
137 * and can be retrieved with \c ret->table_id.
138 * Otherwise, the fib ID to be used to retrieve or create the desired fib.
139 * @returns A pointer to the retrieved or created fib.
140 *
141 */
Neale Ranns15002542017-09-10 04:39:11 -0700142extern u32 ip4_fib_table_find_or_create_and_lock(u32 table_id,
143 fib_source_t src);
144extern u32 ip4_fib_table_create_and_lock(fib_source_t src);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100145
Neale Rannsc87aafa2017-11-29 00:59:31 -0800146extern u8 *format_ip4_fib_table_memory(u8 * s, va_list * args);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100147
148static inline
149u32 ip4_fib_index_from_table_id (u32 table_id)
150{
151 ip4_main_t * im = &ip4_main;
152 uword * p;
153
154 p = hash_get (im->fib_index_by_table_id, table_id);
155 if (!p)
156 return ~0;
157
158 return p[0];
159}
160
161extern u32 ip4_fib_table_get_index_for_sw_if_index(u32 sw_if_index);
162
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100163always_inline index_t
164ip4_fib_forwarding_lookup (u32 fib_index,
165 const ip4_address_t * addr)
166{
167 ip4_fib_mtrie_leaf_t leaf;
168 ip4_fib_mtrie_t * mtrie;
169
170 mtrie = &ip4_fib_get(fib_index)->mtrie;
171
Neale Ranns04a75e32017-03-23 06:46:01 -0700172 leaf = ip4_fib_mtrie_lookup_step_one (mtrie, addr);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100173 leaf = ip4_fib_mtrie_lookup_step (mtrie, leaf, addr, 2);
174 leaf = ip4_fib_mtrie_lookup_step (mtrie, leaf, addr, 3);
175
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100176 return (ip4_fib_mtrie_leaf_get_adj_index(leaf));
177}
178
179
180#endif
181