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