Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 26 | extern fib_node_index_t ip6_fib_table_lookup(u32 fib_index, |
| 27 | const ip6_address_t *addr, |
| 28 | u32 len); |
| 29 | extern fib_node_index_t ip6_fib_table_lookup_exact_match(u32 fib_index, |
| 30 | const ip6_address_t *addr, |
| 31 | u32 len); |
| 32 | |
| 33 | extern void ip6_fib_table_entry_remove(u32 fib_index, |
| 34 | const ip6_address_t *addr, |
| 35 | u32 len); |
| 36 | |
| 37 | extern 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); |
| 41 | extern void ip6_fib_table_destroy(u32 fib_index); |
| 42 | |
| 43 | extern 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 | |
| 48 | extern 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 | |
| 53 | u32 ip6_fib_table_fwding_lookup_with_if_index(ip6_main_t * im, |
| 54 | u32 sw_if_index, |
| 55 | const ip6_address_t * dst); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 56 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 57 | /** |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 58 | * @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 | */ |
| 62 | extern void ip6_fib_table_walk(u32 fib_index, |
| 63 | fib_table_walk_fn_t fn, |
| 64 | void *ctx); |
| 65 | |
Neale Ranns | 630198f | 2017-05-22 09:20:20 -0400 | [diff] [blame] | 66 | always_inline u32 |
| 67 | ip6_fib_table_fwding_lookup (ip6_main_t * im, |
| 68 | u32 fib_index, |
| 69 | const ip6_address_t * dst) |
| 70 | { |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame^] | 71 | ip6_fib_table_instance_t *table; |
Neale Ranns | 630198f | 2017-05-22 09:20:20 -0400 | [diff] [blame] | 72 | int i, len; |
| 73 | int rv; |
| 74 | BVT(clib_bihash_kv) kv, value; |
| 75 | u64 fib; |
| 76 | |
| 77 | table = &ip6_main.ip6_table[IP6_FIB_TABLE_FWDING]; |
| 78 | len = vec_len (table->prefix_lengths_in_search_order); |
| 79 | |
| 80 | kv.key[0] = dst->as_u64[0]; |
| 81 | kv.key[1] = dst->as_u64[1]; |
| 82 | fib = ((u64)((fib_index))<<32); |
| 83 | |
| 84 | for (i = 0; i < len; i++) |
| 85 | { |
| 86 | int dst_address_length = table->prefix_lengths_in_search_order[i]; |
| 87 | ip6_address_t * mask = &ip6_main.fib_masks[dst_address_length]; |
| 88 | |
| 89 | ASSERT(dst_address_length >= 0 && dst_address_length <= 128); |
| 90 | //As lengths are decreasing, masks are increasingly specific. |
| 91 | kv.key[0] &= mask->as_u64[0]; |
| 92 | kv.key[1] &= mask->as_u64[1]; |
| 93 | kv.key[2] = fib | dst_address_length; |
| 94 | |
| 95 | rv = BV(clib_bihash_search_inline_2)(&table->ip6_hash, &kv, &value); |
| 96 | if (rv == 0) |
| 97 | return value.value; |
| 98 | } |
| 99 | |
| 100 | /* default route is always present */ |
| 101 | ASSERT(0); |
| 102 | return 0; |
| 103 | } |
| 104 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 105 | /** |
AkshayaNadahalli | 8ea6d71 | 2017-02-07 23:59:54 +0530 | [diff] [blame] | 106 | * @brief return the DPO that the LB stacks on. |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 107 | */ |
| 108 | always_inline u32 |
| 109 | ip6_src_lookup_for_packet (ip6_main_t * im, |
| 110 | vlib_buffer_t * b, |
| 111 | ip6_header_t * i) |
| 112 | { |
| 113 | if (vnet_buffer (b)->ip.adj_index[VLIB_RX] == ~0) |
| 114 | { |
| 115 | const dpo_id_t *dpo; |
| 116 | index_t lbi; |
| 117 | |
| 118 | lbi = ip6_fib_table_fwding_lookup_with_if_index( |
| 119 | im, |
| 120 | vnet_buffer (b)->sw_if_index[VLIB_RX], |
| 121 | &i->src_address); |
| 122 | |
| 123 | dpo = load_balance_get_bucket_i(load_balance_get(lbi), 0); |
| 124 | |
| 125 | if (dpo_is_adj(dpo)) |
| 126 | { |
| 127 | vnet_buffer (b)->ip.adj_index[VLIB_RX] = dpo->dpoi_index; |
| 128 | } |
| 129 | } |
| 130 | return vnet_buffer (b)->ip.adj_index[VLIB_RX]; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * \brief Get or create an IPv6 fib. |
| 135 | * |
| 136 | * Get or create an IPv4 fib with the provided table ID. |
| 137 | * |
| 138 | * \param im |
| 139 | * ip4_main pointer. |
| 140 | * \param table_id |
| 141 | * When set to \c ~0, an arbitrary and unused fib ID is picked |
| 142 | * and can be retrieved with \c ret->table_id. |
| 143 | * Otherwise, the fib ID to be used to retrieve or create the desired fib. |
| 144 | * \returns A pointer to the retrieved or created fib. |
| 145 | * |
| 146 | */ |
| 147 | extern u32 ip6_fib_table_find_or_create_and_lock(u32 table_id); |
| 148 | extern u32 ip6_fib_table_create_and_lock(void); |
| 149 | |
| 150 | static inline ip6_fib_t * |
| 151 | ip6_fib_get (fib_node_index_t index) |
| 152 | { |
| 153 | ASSERT(!pool_is_free_index(ip6_main.fibs, index)); |
Neale Ranns | a3af337 | 2017-03-28 03:49:52 -0700 | [diff] [blame] | 154 | return (pool_elt_at_index (ip6_main.v6_fibs, index)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static inline |
| 158 | u32 ip6_fib_index_from_table_id (u32 table_id) |
| 159 | { |
| 160 | ip6_main_t * im = &ip6_main; |
| 161 | uword * p; |
| 162 | |
| 163 | p = hash_get (im->fib_index_by_table_id, table_id); |
| 164 | if (!p) |
| 165 | return ~0; |
| 166 | |
| 167 | return p[0]; |
| 168 | } |
| 169 | |
| 170 | extern u32 ip6_fib_table_get_index_for_sw_if_index(u32 sw_if_index); |
| 171 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 172 | #endif |
| 173 | |