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 | * @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> |
| 37 | |
| 38 | extern fib_node_index_t ip4_fib_table_lookup(const ip4_fib_t *fib, |
| 39 | const ip4_address_t *addr, |
| 40 | u32 len); |
| 41 | extern fib_node_index_t ip4_fib_table_lookup_exact_match(const ip4_fib_t *fib, |
| 42 | const ip4_address_t *addr, |
| 43 | u32 len); |
| 44 | |
| 45 | extern void ip4_fib_table_entry_remove(ip4_fib_t *fib, |
| 46 | const ip4_address_t *addr, |
| 47 | u32 len); |
| 48 | |
| 49 | extern void ip4_fib_table_entry_insert(ip4_fib_t *fib, |
| 50 | const ip4_address_t *addr, |
| 51 | u32 len, |
| 52 | fib_node_index_t fib_entry_index); |
| 53 | extern void ip4_fib_table_destroy(ip4_fib_t *fib); |
| 54 | |
| 55 | extern void ip4_fib_table_fwding_dpo_update(ip4_fib_t *fib, |
| 56 | const ip4_address_t *addr, |
| 57 | u32 len, |
| 58 | const dpo_id_t *dpo); |
| 59 | |
| 60 | extern void ip4_fib_table_fwding_dpo_remove(ip4_fib_t *fib, |
| 61 | const ip4_address_t *addr, |
| 62 | u32 len, |
| 63 | const dpo_id_t *dpo); |
| 64 | extern u32 ip4_fib_table_lookup_lb (ip4_fib_t *fib, |
| 65 | const ip4_address_t * dst); |
| 66 | |
| 67 | /** |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 68 | * @brief Walk all entries in a FIB table |
| 69 | * N.B: This is NOT safe to deletes. If you need to delete walk the whole |
| 70 | * table and store elements in a vector, then delete the elements |
| 71 | */ |
| 72 | extern void ip4_fib_table_walk(ip4_fib_t *fib, |
| 73 | fib_table_walk_fn_t fn, |
| 74 | void *ctx); |
| 75 | |
| 76 | /** |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 77 | * @brief Get the FIB at the given index |
| 78 | */ |
| 79 | static inline ip4_fib_t * |
| 80 | ip4_fib_get (u32 index) |
| 81 | { |
| 82 | return (&(pool_elt_at_index(ip4_main.fibs, index)->v4)); |
| 83 | } |
| 84 | |
| 85 | always_inline u32 |
| 86 | ip4_fib_lookup (ip4_main_t * im, u32 sw_if_index, ip4_address_t * dst) |
| 87 | { |
| 88 | return (ip4_fib_table_lookup_lb( |
| 89 | ip4_fib_get(vec_elt (im->fib_index_by_sw_if_index, sw_if_index)), |
| 90 | dst)); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @brief Get or create an IPv4 fib. |
| 95 | * |
| 96 | * Get or create an IPv4 fib with the provided table ID. |
| 97 | * |
| 98 | * @param table_id |
| 99 | * When set to \c ~0, an arbitrary and unused fib ID is picked |
| 100 | * and can be retrieved with \c ret->table_id. |
| 101 | * Otherwise, the fib ID to be used to retrieve or create the desired fib. |
| 102 | * @returns A pointer to the retrieved or created fib. |
| 103 | * |
| 104 | */ |
| 105 | extern u32 ip4_fib_table_find_or_create_and_lock(u32 table_id); |
| 106 | extern u32 ip4_fib_table_create_and_lock(void); |
| 107 | |
| 108 | |
| 109 | static inline |
| 110 | u32 ip4_fib_index_from_table_id (u32 table_id) |
| 111 | { |
| 112 | ip4_main_t * im = &ip4_main; |
| 113 | uword * p; |
| 114 | |
| 115 | p = hash_get (im->fib_index_by_table_id, table_id); |
| 116 | if (!p) |
| 117 | return ~0; |
| 118 | |
| 119 | return p[0]; |
| 120 | } |
| 121 | |
| 122 | extern u32 ip4_fib_table_get_index_for_sw_if_index(u32 sw_if_index); |
| 123 | |
| 124 | extern flow_hash_config_t ip4_fib_table_get_flow_hash_config(u32 fib_index); |
| 125 | |
| 126 | |
| 127 | always_inline index_t |
| 128 | ip4_fib_forwarding_lookup (u32 fib_index, |
| 129 | const ip4_address_t * addr) |
| 130 | { |
| 131 | ip4_fib_mtrie_leaf_t leaf; |
| 132 | ip4_fib_mtrie_t * mtrie; |
| 133 | |
| 134 | mtrie = &ip4_fib_get(fib_index)->mtrie; |
| 135 | |
| 136 | leaf = IP4_FIB_MTRIE_LEAF_ROOT; |
| 137 | leaf = ip4_fib_mtrie_lookup_step (mtrie, leaf, addr, 0); |
| 138 | leaf = ip4_fib_mtrie_lookup_step (mtrie, leaf, addr, 1); |
| 139 | leaf = ip4_fib_mtrie_lookup_step (mtrie, leaf, addr, 2); |
| 140 | leaf = ip4_fib_mtrie_lookup_step (mtrie, leaf, addr, 3); |
| 141 | |
| 142 | /* Handle default route. */ |
| 143 | leaf = (leaf == IP4_FIB_MTRIE_LEAF_EMPTY ? mtrie->default_leaf : leaf); |
| 144 | |
| 145 | return (ip4_fib_mtrie_leaf_get_adj_index(leaf)); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | #endif |
| 150 | |