blob: 50b360bcd1298d56d9d8099f532a2efdc40ba32c [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#ifndef __IP6_FIB_H__
17#define __IP6_FIB_H__
18
19#include <vlib/vlib.h>
20#include <vnet/ip/format.h>
21#include <vnet/fib/fib_entry.h>
22#include <vnet/fib/fib_table.h>
23#include <vnet/ip/lookup.h>
24#include <vnet/dpo/load_balance.h>
25
26extern fib_node_index_t ip6_fib_table_lookup(u32 fib_index,
27 const ip6_address_t *addr,
28 u32 len);
29extern fib_node_index_t ip6_fib_table_lookup_exact_match(u32 fib_index,
30 const ip6_address_t *addr,
31 u32 len);
32
33extern void ip6_fib_table_entry_remove(u32 fib_index,
34 const ip6_address_t *addr,
35 u32 len);
36
37extern void ip6_fib_table_entry_insert(u32 fib_index,
38 const ip6_address_t *addr,
39 u32 len,
40 fib_node_index_t fib_entry_index);
41extern void ip6_fib_table_destroy(u32 fib_index);
42
43extern void ip6_fib_table_fwding_dpo_update(u32 fib_index,
44 const ip6_address_t *addr,
45 u32 len,
46 const dpo_id_t *dpo);
47
48extern void ip6_fib_table_fwding_dpo_remove(u32 fib_index,
49 const ip6_address_t *addr,
50 u32 len,
51 const dpo_id_t *dpo);
52
53u32 ip6_fib_table_fwding_lookup_with_if_index(ip6_main_t * im,
54 u32 sw_if_index,
55 const ip6_address_t * dst);
Neale Ranns0bfe5d82016-08-25 15:29:12 +010056
Neale Ranns0bfe5d82016-08-25 15:29:12 +010057/**
Neale Ranns32e1c012016-11-22 17:07:28 +000058 * @brief Walk all entries in a FIB table
59 * N.B: This is NOT safe to deletes. If you need to delete walk the whole
60 * table and store elements in a vector, then delete the elements
61 */
62extern void ip6_fib_table_walk(u32 fib_index,
63 fib_table_walk_fn_t fn,
64 void *ctx);
65
Neale Ranns630198f2017-05-22 09:20:20 -040066always_inline u32
Simon Zhange7eba482019-08-25 15:30:45 +080067ip6_fib_table_fwding_lookup (u32 fib_index,
Neale Ranns630198f2017-05-22 09:20:20 -040068 const ip6_address_t * dst)
69{
Dave Barach908a5ea2017-07-14 12:42:21 -040070 ip6_fib_table_instance_t *table;
Neale Rannsae809832018-11-23 09:00:27 -080071 clib_bihash_kv_24_8_t kv, value;
Neale Ranns630198f2017-05-22 09:20:20 -040072 int i, len;
73 int rv;
Neale Ranns630198f2017-05-22 09:20:20 -040074 u64 fib;
75
76 table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING];
77 len = vec_len (table->prefix_lengths_in_search_order);
78
79 kv.key[0] = dst->as_u64[0];
80 kv.key[1] = dst->as_u64[1];
81 fib = ((u64)((fib_index))<<32);
82
83 for (i = 0; i < len; i++)
84 {
85 int dst_address_length = table->prefix_lengths_in_search_order[i];
86 ip6_address_t * mask = &ip6_main.fib_masks[dst_address_length];
87
88 ASSERT(dst_address_length >= 0 && dst_address_length <= 128);
89 //As lengths are decreasing, masks are increasingly specific.
90 kv.key[0] &= mask->as_u64[0];
91 kv.key[1] &= mask->as_u64[1];
92 kv.key[2] = fib | dst_address_length;
93
Neale Rannsae809832018-11-23 09:00:27 -080094 rv = clib_bihash_search_inline_2_24_8(&table->ip6_hash, &kv, &value);
Neale Ranns630198f2017-05-22 09:20:20 -040095 if (rv == 0)
96 return value.value;
97 }
98
99 /* default route is always present */
100 ASSERT(0);
101 return 0;
102}
103
Neale Ranns32e1c012016-11-22 17:07:28 +0000104/**
Neale Ranns89541992017-04-06 04:41:02 -0700105 * @brief Walk all entries in a sub-tree of the 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 ip6_fib_table_sub_tree_walk(u32 fib_index,
110 const fib_prefix_t *root,
111 fib_table_walk_fn_t fn,
112 void *ctx);
113
114/**
AkshayaNadahalli8ea6d712017-02-07 23:59:54 +0530115 * @brief return the DPO that the LB stacks on.
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100116 */
117always_inline u32
118ip6_src_lookup_for_packet (ip6_main_t * im,
119 vlib_buffer_t * b,
120 ip6_header_t * i)
121{
122 if (vnet_buffer (b)->ip.adj_index[VLIB_RX] == ~0)
123 {
124 const dpo_id_t *dpo;
125 index_t lbi;
126
127 lbi = ip6_fib_table_fwding_lookup_with_if_index(
128 im,
129 vnet_buffer (b)->sw_if_index[VLIB_RX],
130 &i->src_address);
131
132 dpo = load_balance_get_bucket_i(load_balance_get(lbi), 0);
133
134 if (dpo_is_adj(dpo))
135 {
136 vnet_buffer (b)->ip.adj_index[VLIB_RX] = dpo->dpoi_index;
137 }
138 }
139 return vnet_buffer (b)->ip.adj_index[VLIB_RX];
140}
141
142/**
143 * \brief Get or create an IPv6 fib.
144 *
145 * Get or create an IPv4 fib with the provided table ID.
146 *
147 * \param im
148 * ip4_main pointer.
149 * \param table_id
150 * When set to \c ~0, an arbitrary and unused fib ID is picked
151 * and can be retrieved with \c ret->table_id.
152 * Otherwise, the fib ID to be used to retrieve or create the desired fib.
153 * \returns A pointer to the retrieved or created fib.
154 *
155 */
Neale Ranns15002542017-09-10 04:39:11 -0700156extern u32 ip6_fib_table_find_or_create_and_lock(u32 table_id,
157 fib_source_t src);
Neale Ranns53da2212018-02-24 02:11:19 -0800158extern u32 ip6_fib_table_create_and_lock(fib_source_t src,
159 fib_table_flags_t flags,
160 u8* desc);
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100161
Neale Rannsc87aafa2017-11-29 00:59:31 -0800162extern u8 *format_ip6_fib_table_memory(u8 * s, va_list * args);
163
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100164static inline ip6_fib_t *
165ip6_fib_get (fib_node_index_t index)
166{
167 ASSERT(!pool_is_free_index(ip6_main.fibs, index));
Neale Rannsa3af3372017-03-28 03:49:52 -0700168 return (pool_elt_at_index (ip6_main.v6_fibs, index));
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100169}
170
171static inline
172u32 ip6_fib_index_from_table_id (u32 table_id)
173{
174 ip6_main_t * im = &ip6_main;
175 uword * p;
176
177 p = hash_get (im->fib_index_by_table_id, table_id);
178 if (!p)
179 return ~0;
180
181 return p[0];
182}
183
184extern u32 ip6_fib_table_get_index_for_sw_if_index(u32 sw_if_index);
185
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100186#endif
187