Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 1 | /* |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 2 | * teib.h: Tunnel Endpoint Information Base |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 3 | * |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 4 | * Copyright (c) 2020 Cisco and/or its affiliates. |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | #include <vnet/teib/teib.h> |
| 20 | #include <vnet/fib/fib_table.h> |
| 21 | #include <vnet/adj/adj_midchain.h> |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 22 | #include <vnet/ip/ip6_ll_table.h> |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 23 | |
| 24 | typedef struct teib_key_t_ |
| 25 | { |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 26 | ip_address_t tk_peer; |
| 27 | u8 __pad[3]; |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 28 | u32 tk_sw_if_index; |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 29 | } __clib_packed teib_key_t; |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 30 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 31 | STATIC_ASSERT_SIZEOF (teib_key_t, 24); |
| 32 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 33 | struct teib_entry_t_ |
| 34 | { |
| 35 | teib_key_t *te_key; |
| 36 | fib_prefix_t te_nh; |
| 37 | u32 te_fib_index; |
| 38 | }; |
| 39 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 40 | typedef struct teib_db_t_ |
| 41 | { |
| 42 | u32 td_n_entries[N_AF]; |
| 43 | uword *td_db; |
| 44 | } teib_db_t; |
| 45 | |
| 46 | static teib_db_t teib_db; |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 47 | static teib_entry_t *teib_pool; |
| 48 | static teib_vft_t *teib_vfts; |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 49 | static vlib_log_class_t teib_logger; |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 50 | |
| 51 | #define TEIB_NOTIFY(_te, _fn) { \ |
| 52 | teib_vft_t *_vft; \ |
| 53 | vec_foreach(_vft, teib_vfts) { \ |
| 54 | if (_vft->_fn) { \ |
| 55 | _vft->_fn(_te); \ |
| 56 | } \ |
| 57 | } \ |
| 58 | } |
| 59 | |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 60 | #define TEIB_DBG(...) \ |
| 61 | vlib_log_debug (teib_logger, __VA_ARGS__); |
| 62 | |
| 63 | #define TEIB_INFO(...) \ |
| 64 | vlib_log_notice (teib_logger, __VA_ARGS__); |
| 65 | |
| 66 | #define TEIB_TE_DBG(_te, _fmt, _args...) \ |
| 67 | vlib_log_debug (teib_logger, "[%U]: " _fmt, format_teib_entry, _te - teib_pool, ##_args) |
| 68 | #define TEIB_TE_INFO(_te, _fmt, _args...) \ |
| 69 | vlib_log_notice (teib_logger, "[%U]: " _fmt, format_teib_entry, _te - teib_pool, ##_args) |
| 70 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 71 | u32 |
| 72 | teib_entry_get_sw_if_index (const teib_entry_t * te) |
| 73 | { |
| 74 | return (te->te_key->tk_sw_if_index); |
| 75 | } |
| 76 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 77 | static ip_address_family_t |
| 78 | teib_entry_get_af (const teib_entry_t * te) |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 79 | { |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 80 | return (ip_addr_version (&te->te_key->tk_peer)); |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 83 | u32 |
| 84 | teib_entry_get_fib_index (const teib_entry_t * te) |
| 85 | { |
| 86 | return (te->te_fib_index); |
| 87 | } |
| 88 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 89 | const ip_address_t * |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 90 | teib_entry_get_peer (const teib_entry_t * te) |
| 91 | { |
| 92 | return (&te->te_key->tk_peer); |
| 93 | } |
| 94 | |
| 95 | const fib_prefix_t * |
| 96 | teib_entry_get_nh (const teib_entry_t * te) |
| 97 | { |
| 98 | return (&te->te_nh); |
| 99 | } |
| 100 | |
| 101 | void |
| 102 | teib_entry_adj_stack (const teib_entry_t * te, adj_index_t ai) |
| 103 | { |
| 104 | adj_midchain_delegate_stack (ai, te->te_fib_index, &te->te_nh); |
| 105 | } |
| 106 | |
| 107 | teib_entry_t * |
| 108 | teib_entry_get (index_t tei) |
| 109 | { |
| 110 | return pool_elt_at_index (teib_pool, tei); |
| 111 | } |
| 112 | |
| 113 | teib_entry_t * |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 114 | teib_entry_find (u32 sw_if_index, const ip_address_t * peer) |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 115 | { |
| 116 | teib_key_t nk = { |
| 117 | .tk_peer = *peer, |
| 118 | .tk_sw_if_index = sw_if_index, |
| 119 | }; |
| 120 | uword *p; |
| 121 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 122 | p = hash_get_mem (teib_db.td_db, &nk); |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 123 | |
| 124 | if (NULL != p) |
| 125 | return teib_entry_get (p[0]); |
| 126 | |
| 127 | return (NULL); |
| 128 | } |
| 129 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 130 | teib_entry_t * |
| 131 | teib_entry_find_46 (u32 sw_if_index, |
| 132 | fib_protocol_t fproto, const ip46_address_t * peer) |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 133 | { |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 134 | ip_address_t ip; |
| 135 | |
| 136 | ip_address_from_46 (peer, fproto, &ip); |
| 137 | |
| 138 | return (teib_entry_find (sw_if_index, &ip)); |
| 139 | } |
| 140 | |
| 141 | static void |
| 142 | teib_adj_fib_add (const ip_address_t * ip, u32 sw_if_index, u32 fib_index) |
| 143 | { |
| 144 | if (AF_IP6 == ip_addr_version (ip) && |
| 145 | ip6_address_is_link_local_unicast (&ip_addr_v6 (ip))) |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 146 | { |
| 147 | ip6_ll_prefix_t pfx = { |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 148 | .ilp_addr = ip_addr_v6 (ip), |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 149 | .ilp_sw_if_index = sw_if_index, |
| 150 | }; |
| 151 | ip6_ll_table_entry_update (&pfx, FIB_ROUTE_PATH_FLAG_NONE); |
| 152 | } |
| 153 | else |
| 154 | { |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 155 | fib_prefix_t pfx; |
| 156 | |
| 157 | ip_address_to_fib_prefix (ip, &pfx); |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 158 | fib_table_entry_path_add (fib_index, &pfx, FIB_SOURCE_ADJ, |
| 159 | FIB_ENTRY_FLAG_ATTACHED, |
| 160 | fib_proto_to_dpo (pfx.fp_proto), |
| 161 | &pfx.fp_addr, |
| 162 | sw_if_index, |
| 163 | ~0, 1, NULL, FIB_ROUTE_PATH_FLAG_NONE); |
| 164 | |
| 165 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 166 | if (0 == teib_db.td_n_entries[ip_addr_version (ip)]++) |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 167 | fib_table_lock (fib_index, pfx.fp_proto, FIB_SOURCE_ADJ); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | static void |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 172 | teib_adj_fib_remove (ip_address_t * ip, u32 sw_if_index, u32 fib_index) |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 173 | { |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 174 | if (AF_IP6 == ip_addr_version (ip) && |
| 175 | ip6_address_is_link_local_unicast (&ip_addr_v6 (ip))) |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 176 | { |
| 177 | ip6_ll_prefix_t pfx = { |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 178 | .ilp_addr = ip_addr_v6 (ip), |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 179 | .ilp_sw_if_index = sw_if_index, |
| 180 | }; |
| 181 | ip6_ll_table_entry_delete (&pfx); |
| 182 | } |
| 183 | else |
| 184 | { |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 185 | fib_prefix_t pfx; |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 186 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 187 | ip_address_to_fib_prefix (ip, &pfx); |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 188 | fib_table_entry_path_remove (fib_index, &pfx, FIB_SOURCE_ADJ, |
| 189 | fib_proto_to_dpo (pfx.fp_proto), |
| 190 | &pfx.fp_addr, |
| 191 | sw_if_index, |
| 192 | ~0, 1, FIB_ROUTE_PATH_FLAG_NONE); |
| 193 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 194 | if (0 == --teib_db.td_n_entries[ip_addr_version (ip)]) |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 195 | fib_table_unlock (fib_index, pfx.fp_proto, FIB_SOURCE_ADJ); |
| 196 | } |
| 197 | } |
| 198 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 199 | int |
| 200 | teib_entry_add (u32 sw_if_index, |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 201 | const ip_address_t * peer, |
| 202 | u32 nh_table_id, const ip_address_t * nh) |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 203 | { |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 204 | fib_protocol_t nh_proto; |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 205 | teib_entry_t *te; |
| 206 | u32 fib_index; |
| 207 | index_t tei; |
| 208 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 209 | nh_proto = (AF_IP4 == ip_addr_version (nh) ? |
| 210 | FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6); |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 211 | |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 212 | fib_index = fib_table_find (nh_proto, nh_table_id); |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 213 | |
| 214 | if (~0 == fib_index) |
| 215 | { |
| 216 | return (VNET_API_ERROR_NO_SUCH_FIB); |
| 217 | } |
| 218 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 219 | te = teib_entry_find (sw_if_index, peer); |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 220 | |
| 221 | if (NULL == te) |
| 222 | { |
| 223 | teib_key_t nk = { |
| 224 | .tk_peer = *peer, |
| 225 | .tk_sw_if_index = sw_if_index, |
| 226 | }; |
| 227 | teib_entry_t *te; |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 228 | u32 fib_index; |
| 229 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 230 | fib_index = fib_table_get_index_for_sw_if_index (nh_proto, sw_if_index); |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 231 | |
| 232 | pool_get_zero (teib_pool, te); |
| 233 | |
| 234 | tei = te - teib_pool; |
| 235 | te->te_key = clib_mem_alloc (sizeof (*te->te_key)); |
| 236 | clib_memcpy (te->te_key, &nk, sizeof (*te->te_key)); |
| 237 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 238 | ip_address_to_fib_prefix (nh, &te->te_nh); |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 239 | te->te_fib_index = fib_index; |
| 240 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 241 | hash_set_mem (teib_db.td_db, te->te_key, tei); |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 242 | |
| 243 | /* we how have a /32 in the overlay, add an adj-fib */ |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 244 | teib_adj_fib_add (&te->te_key->tk_peer, sw_if_index, fib_index); |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 245 | |
| 246 | TEIB_NOTIFY (te, nv_added); |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 247 | TEIB_TE_INFO (te, "created"); |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 248 | } |
| 249 | else |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 250 | { |
| 251 | TEIB_TE_INFO (te, "exists"); |
| 252 | return (VNET_API_ERROR_ENTRY_ALREADY_EXISTS); |
| 253 | } |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | int |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 258 | teib_entry_del (u32 sw_if_index, const ip_address_t * peer) |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 259 | { |
| 260 | teib_entry_t *te; |
| 261 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 262 | te = teib_entry_find (sw_if_index, peer); |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 263 | |
| 264 | if (te != NULL) |
| 265 | { |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 266 | TEIB_TE_INFO (te, "removed"); |
| 267 | |
| 268 | u32 fib_index; |
| 269 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 270 | fib_index = fib_table_get_index_for_sw_if_index |
| 271 | (ip_address_family_to_fib_proto (ip_addr_version (peer)), |
| 272 | sw_if_index); |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 273 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 274 | teib_adj_fib_remove (&te->te_key->tk_peer, sw_if_index, fib_index); |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 275 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 276 | hash_unset_mem (teib_db.td_db, te->te_key); |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 277 | |
| 278 | TEIB_NOTIFY (te, nv_deleted); |
| 279 | |
| 280 | clib_mem_free (te->te_key); |
| 281 | pool_put (teib_pool, te); |
| 282 | } |
| 283 | else |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 284 | { |
| 285 | TEIB_INFO ("no such entry: %U, %U, %U", |
| 286 | format_vnet_sw_if_index_name, |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 287 | vnet_get_main (), sw_if_index, format_ip_address, peer); |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 288 | return (VNET_API_ERROR_NO_SUCH_ENTRY); |
| 289 | } |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | u8 * |
| 294 | format_teib_entry (u8 * s, va_list * args) |
| 295 | { |
| 296 | index_t tei = va_arg (*args, index_t); |
| 297 | vnet_main_t *vnm = vnet_get_main (); |
| 298 | teib_entry_t *te; |
| 299 | |
| 300 | te = teib_entry_get (tei); |
| 301 | |
| 302 | s = format (s, "[%d] ", tei); |
| 303 | s = format (s, "%U:", format_vnet_sw_if_index_name, |
| 304 | vnm, te->te_key->tk_sw_if_index); |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 305 | s = format (s, "%U", format_ip_address, |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 306 | &te->te_key->tk_peer, IP46_TYPE_ANY); |
| 307 | s = format (s, " via [%d]:%U", |
| 308 | fib_table_get_table_id (te->te_fib_index, te->te_nh.fp_proto), |
| 309 | format_fib_prefix, &te->te_nh); |
| 310 | |
| 311 | return (s); |
| 312 | } |
| 313 | |
| 314 | void |
| 315 | teib_walk (teib_walk_cb_t fn, void *ctx) |
| 316 | { |
| 317 | index_t tei; |
| 318 | |
| 319 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 320 | pool_foreach_index (tei, teib_pool) |
| 321 | { |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 322 | fn(tei, ctx); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 323 | } |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 324 | /* *INDENT-ON* */ |
| 325 | } |
| 326 | |
| 327 | void |
| 328 | teib_walk_itf (u32 sw_if_index, teib_walk_cb_t fn, void *ctx) |
| 329 | { |
| 330 | index_t tei; |
| 331 | |
| 332 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 333 | pool_foreach_index (tei, teib_pool) |
| 334 | { |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 335 | if (sw_if_index == teib_entry_get_sw_if_index(teib_entry_get(tei))) |
| 336 | fn(tei, ctx); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 337 | } |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 338 | /* *INDENT-ON* */ |
| 339 | } |
| 340 | |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 341 | static void |
| 342 | teib_walk_itf_proto (u32 sw_if_index, |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 343 | ip_address_family_t af, teib_walk_cb_t fn, void *ctx) |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 344 | { |
| 345 | index_t tei; |
| 346 | |
| 347 | /* *INDENT-OFF* */ |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 348 | pool_foreach_index (tei, teib_pool) |
| 349 | { |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 350 | if (sw_if_index == teib_entry_get_sw_if_index(teib_entry_get(tei)) && |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 351 | af == teib_entry_get_af(teib_entry_get(tei))) |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 352 | fn(tei, ctx); |
Damjan Marion | b2c31b6 | 2020-12-13 21:47:40 +0100 | [diff] [blame] | 353 | } |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 354 | /* *INDENT-ON* */ |
| 355 | } |
| 356 | |
| 357 | typedef struct teib_table_bind_ctx_t_ |
| 358 | { |
| 359 | u32 new_fib_index; |
| 360 | u32 old_fib_index; |
| 361 | } teib_table_bind_ctx_t; |
| 362 | |
| 363 | static walk_rc_t |
| 364 | teib_walk_table_bind (index_t tei, void *arg) |
| 365 | { |
| 366 | teib_table_bind_ctx_t *ctx = arg; |
| 367 | teib_entry_t *te; |
| 368 | |
| 369 | te = teib_entry_get (tei); |
| 370 | |
| 371 | TEIB_TE_INFO (te, "bind: %d -> %d", ctx->old_fib_index, ctx->new_fib_index); |
| 372 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 373 | teib_adj_fib_remove (&te->te_key->tk_peer, |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 374 | te->te_key->tk_sw_if_index, ctx->old_fib_index); |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 375 | teib_adj_fib_add (&te->te_key->tk_peer, |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 376 | te->te_key->tk_sw_if_index, ctx->new_fib_index); |
| 377 | |
| 378 | return (WALK_CONTINUE); |
| 379 | } |
| 380 | |
| 381 | static void |
| 382 | teib_table_bind_v4 (ip4_main_t * im, |
| 383 | uword opaque, |
| 384 | u32 sw_if_index, u32 new_fib_index, u32 old_fib_index) |
| 385 | { |
| 386 | teib_table_bind_ctx_t ctx = { |
| 387 | .old_fib_index = old_fib_index, |
| 388 | .new_fib_index = new_fib_index, |
| 389 | }; |
| 390 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 391 | teib_walk_itf_proto (sw_if_index, AF_IP4, teib_walk_table_bind, &ctx); |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | static void |
| 395 | teib_table_bind_v6 (ip6_main_t * im, |
| 396 | uword opaque, |
| 397 | u32 sw_if_index, u32 new_fib_index, u32 old_fib_index) |
| 398 | { |
| 399 | teib_table_bind_ctx_t ctx = { |
| 400 | .old_fib_index = old_fib_index, |
| 401 | .new_fib_index = new_fib_index, |
| 402 | }; |
| 403 | |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 404 | teib_walk_itf_proto (sw_if_index, AF_IP6, teib_walk_table_bind, &ctx); |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 407 | void |
| 408 | teib_register (const teib_vft_t * vft) |
| 409 | { |
| 410 | vec_add1 (teib_vfts, *vft); |
| 411 | } |
| 412 | |
| 413 | static clib_error_t * |
| 414 | teib_init (vlib_main_t * vm) |
| 415 | { |
Neale Ranns | e6b8305 | 2020-09-17 12:56:47 +0000 | [diff] [blame] | 416 | teib_db.td_db = hash_create_mem (0, sizeof (teib_key_t), sizeof (u32)); |
Neale Ranns | 256b67b | 2020-09-02 14:46:53 +0000 | [diff] [blame] | 417 | |
| 418 | ip4_table_bind_callback_t cb4 = { |
| 419 | .function = teib_table_bind_v4, |
| 420 | }; |
| 421 | vec_add1 (ip4_main.table_bind_callbacks, cb4); |
| 422 | |
| 423 | ip6_table_bind_callback_t cb6 = { |
| 424 | .function = teib_table_bind_v6, |
| 425 | }; |
| 426 | vec_add1 (ip6_main.table_bind_callbacks, cb6); |
| 427 | |
| 428 | teib_logger = vlib_log_register_class ("teib", "teib"); |
Neale Ranns | 03ce462 | 2020-02-03 10:55:09 +0000 | [diff] [blame] | 429 | |
| 430 | return (NULL); |
| 431 | } |
| 432 | |
| 433 | VLIB_INIT_FUNCTION (teib_init); |
| 434 | |
| 435 | /* |
| 436 | * fd.io coding-style-patch-verification: ON |
| 437 | * |
| 438 | * Local Variables: |
| 439 | * eval: (c-set-style "gnu") |
| 440 | * End: |
| 441 | */ |