Neale Ranns | 4c3ba81 | 2019-03-26 07:02:58 +0000 | [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 | #include <vnet/adj/adj_delegate.h> |
| 17 | #include <vnet/adj/adj_midchain.h> |
| 18 | #include <vnet/fib/fib_table.h> |
| 19 | |
| 20 | /** |
| 21 | * Midchain stacker delegate |
| 22 | */ |
| 23 | typedef struct adj_midchain_delegate_t_ |
| 24 | { |
| 25 | /** |
| 26 | * the Fib Entry we are stacked on |
| 27 | */ |
| 28 | fib_node_index_t amd_fei; |
| 29 | |
| 30 | /** |
| 31 | * The sibling entry on the FIB entry |
| 32 | */ |
| 33 | u32 amd_sibling; |
| 34 | } adj_midchain_delegate_t; |
| 35 | |
| 36 | /** |
| 37 | * Pool of delegates |
| 38 | */ |
| 39 | static adj_midchain_delegate_t *amd_pool; |
| 40 | |
| 41 | static inline const adj_midchain_delegate_t* |
| 42 | adj_midchain_from_const_base (const adj_delegate_t *ad) |
| 43 | { |
| 44 | if (NULL != ad) |
| 45 | { |
| 46 | return (pool_elt_at_index(amd_pool, ad->ad_index)); |
| 47 | } |
| 48 | return (NULL); |
| 49 | } |
| 50 | |
| 51 | static void |
| 52 | adj_midchain_delegate_restack_i (adj_index_t ai, |
| 53 | adj_midchain_delegate_t *amd) |
| 54 | { |
| 55 | if (vnet_sw_interface_is_admin_up (vnet_get_main (), |
| 56 | adj_get_sw_if_index(ai)) && |
| 57 | (FIB_NODE_INDEX_INVALID != amd->amd_fei)) |
| 58 | { |
| 59 | const fib_prefix_t *pfx; |
| 60 | |
| 61 | pfx = fib_entry_get_prefix(amd->amd_fei); |
| 62 | |
| 63 | adj_nbr_midchain_stack_on_fib_entry ( |
| 64 | ai, |
| 65 | amd->amd_fei, |
| 66 | fib_forw_chain_type_from_fib_proto(pfx->fp_proto)); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | adj_nbr_midchain_unstack (ai); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | adj_midchain_delegate_restack (adj_index_t ai) |
| 76 | { |
| 77 | adj_midchain_delegate_t *amd; |
| 78 | ip_adjacency_t *adj; |
| 79 | adj_delegate_t *ad; |
| 80 | |
| 81 | /* |
| 82 | * if there's a delegate already use that |
| 83 | */ |
| 84 | adj = adj_get(ai); |
| 85 | ad = adj_delegate_get(adj, ADJ_DELEGATE_MIDCHAIN); |
| 86 | |
| 87 | if (NULL != ad) |
| 88 | { |
| 89 | amd = pool_elt_at_index(amd_pool, ad->ad_index); |
| 90 | |
| 91 | adj_midchain_delegate_restack_i(ai, amd); |
| 92 | } |
| 93 | /* |
| 94 | * else |
| 95 | * nothing to stack |
| 96 | */ |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | adj_midchain_delegate_stack (adj_index_t ai, |
| 101 | u32 fib_index, |
| 102 | const fib_prefix_t *pfx) |
| 103 | { |
| 104 | adj_midchain_delegate_t *amd; |
| 105 | ip_adjacency_t *adj; |
| 106 | adj_delegate_t *ad; |
| 107 | |
| 108 | /* |
| 109 | * if there's a delegate already use that |
| 110 | */ |
| 111 | adj = adj_get(ai); |
| 112 | ad = adj_delegate_get(adj, ADJ_DELEGATE_MIDCHAIN); |
| 113 | |
| 114 | if (NULL != ad) |
| 115 | { |
| 116 | amd = pool_elt_at_index(amd_pool, ad->ad_index); |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | pool_get(amd_pool, amd); |
| 121 | amd->amd_fei = FIB_NODE_INDEX_INVALID; |
| 122 | adj_delegate_add(adj, ADJ_DELEGATE_MIDCHAIN, amd - amd_pool); |
| 123 | |
| 124 | amd->amd_fei = fib_table_entry_special_add(fib_index, |
| 125 | pfx, |
| 126 | FIB_SOURCE_RR, |
| 127 | FIB_ENTRY_FLAG_NONE); |
| 128 | amd->amd_sibling = fib_entry_child_add(amd->amd_fei, |
| 129 | FIB_NODE_TYPE_ADJ, |
| 130 | ai); |
| 131 | } |
| 132 | adj_midchain_delegate_restack_i(ai, amd); |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | adj_midchain_delegate_unstack (adj_index_t ai) |
| 137 | { |
| 138 | adj_nbr_midchain_unstack(ai); |
| 139 | } |
| 140 | |
| 141 | static void |
| 142 | adj_midchain_delegate_adj_deleted (adj_delegate_t *ad) |
| 143 | { |
| 144 | adj_midchain_delegate_t *amd; |
| 145 | |
| 146 | amd = pool_elt_at_index(amd_pool, ad->ad_index); |
| 147 | |
| 148 | fib_entry_child_remove (amd->amd_fei, amd->amd_sibling); |
| 149 | fib_table_entry_delete_index (amd->amd_fei, FIB_SOURCE_RR); |
| 150 | |
| 151 | pool_put(amd_pool, amd); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Print a delegate that represents MIDCHAIN tracking |
| 156 | */ |
| 157 | static u8 * |
| 158 | adj_midchain_delegate_fmt (const adj_delegate_t *aed, u8 *s) |
| 159 | { |
| 160 | const adj_midchain_delegate_t *amd = adj_midchain_from_const_base(aed); |
| 161 | |
| 162 | s = format(s, "MIDCHAIN:[fib-entry:%d]", amd->amd_fei); |
| 163 | |
| 164 | return (s); |
| 165 | } |
| 166 | |
| 167 | const static adj_delegate_vft_t adj_delegate_vft = { |
| 168 | .adv_format = adj_midchain_delegate_fmt, |
| 169 | .adv_adj_deleted = adj_midchain_delegate_adj_deleted, |
| 170 | }; |
| 171 | |
| 172 | static clib_error_t * |
| 173 | adj_midchain_delegate_module_init (vlib_main_t * vm) |
| 174 | { |
| 175 | clib_error_t * error = NULL; |
| 176 | |
| 177 | adj_delegate_register_type (ADJ_DELEGATE_MIDCHAIN, &adj_delegate_vft); |
| 178 | |
| 179 | return (error); |
| 180 | } |
| 181 | |
| 182 | VLIB_INIT_FUNCTION (adj_midchain_delegate_module_init); |
| 183 | |