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 | #include <vnet/adj/adj.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 17 | #include <vnet/adj/adj_internal.h> |
| 18 | #include <vnet/fib/fib_walk.h> |
| 19 | |
| 20 | /* |
| 21 | * The 'DB' of all glean adjs. |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 22 | * There is one glean per-{interface, protocol, connected prefix} |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 23 | */ |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 24 | static uword **adj_gleans[FIB_PROTOCOL_IP_MAX]; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 25 | |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 26 | static inline u32 |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 27 | adj_get_glean_node (fib_protocol_t proto) |
| 28 | { |
| 29 | switch (proto) { |
| 30 | case FIB_PROTOCOL_IP4: |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 31 | return (ip4_glean_node.index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 32 | case FIB_PROTOCOL_IP6: |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 33 | return (ip6_glean_node.index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 34 | case FIB_PROTOCOL_MPLS: |
| 35 | break; |
| 36 | } |
| 37 | ASSERT(0); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 38 | return (~0); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 39 | } |
| 40 | |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 41 | static adj_index_t |
| 42 | adj_glean_db_lookup (fib_protocol_t proto, |
| 43 | u32 sw_if_index, |
| 44 | const ip46_address_t *nh_addr) |
| 45 | { |
| 46 | uword *p; |
| 47 | |
| 48 | if (vec_len(adj_gleans[proto]) <= sw_if_index) |
| 49 | return (ADJ_INDEX_INVALID); |
| 50 | |
| 51 | p = hash_get_mem (adj_gleans[proto][sw_if_index], nh_addr); |
| 52 | |
| 53 | if (p) |
| 54 | return (p[0]); |
| 55 | |
| 56 | return (ADJ_INDEX_INVALID); |
| 57 | } |
| 58 | |
| 59 | static void |
| 60 | adj_glean_db_insert (fib_protocol_t proto, |
| 61 | u32 sw_if_index, |
| 62 | const ip46_address_t *nh_addr, |
| 63 | adj_index_t ai) |
| 64 | { |
| 65 | vlib_main_t *vm = vlib_get_main(); |
| 66 | |
| 67 | vlib_worker_thread_barrier_sync(vm); |
| 68 | |
| 69 | vec_validate(adj_gleans[proto], sw_if_index); |
| 70 | |
| 71 | if (NULL == adj_gleans[proto][sw_if_index]) |
| 72 | { |
| 73 | adj_gleans[proto][sw_if_index] = |
| 74 | hash_create_mem (0, sizeof(ip46_address_t), sizeof(adj_index_t)); |
| 75 | } |
| 76 | |
| 77 | hash_set_mem_alloc (&adj_gleans[proto][sw_if_index], |
| 78 | nh_addr, ai); |
| 79 | |
| 80 | vlib_worker_thread_barrier_release(vm); |
| 81 | } |
| 82 | |
| 83 | static void |
| 84 | adj_glean_db_remove (fib_protocol_t proto, |
| 85 | u32 sw_if_index, |
| 86 | const ip46_address_t *nh_addr) |
| 87 | { |
| 88 | vlib_main_t *vm = vlib_get_main(); |
| 89 | |
| 90 | vlib_worker_thread_barrier_sync(vm); |
| 91 | |
| 92 | ASSERT(ADJ_INDEX_INVALID != adj_glean_db_lookup(proto, sw_if_index, nh_addr)); |
| 93 | hash_unset_mem_free (&adj_gleans[proto][sw_if_index], |
| 94 | nh_addr); |
| 95 | |
| 96 | if (0 == hash_elts(adj_gleans[proto][sw_if_index])) |
| 97 | { |
| 98 | hash_free(adj_gleans[proto][sw_if_index]); |
| 99 | adj_gleans[proto][sw_if_index] = NULL; |
| 100 | } |
| 101 | vlib_worker_thread_barrier_release(vm); |
| 102 | } |
| 103 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 104 | /* |
| 105 | * adj_glean_add_or_lock |
| 106 | * |
| 107 | * The next_hop address here is used for source address selection in the DP. |
| 108 | * The glean adj is added to an interface's connected prefix, the next-hop |
| 109 | * passed here is the local prefix on the same interface. |
| 110 | */ |
| 111 | adj_index_t |
| 112 | adj_glean_add_or_lock (fib_protocol_t proto, |
Ole Troan | 6ee4051 | 2018-02-12 18:14:39 +0100 | [diff] [blame] | 113 | vnet_link_t linkt, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 114 | u32 sw_if_index, |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 115 | const fib_prefix_t *conn) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 116 | { |
| 117 | ip_adjacency_t * adj; |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 118 | adj_index_t ai; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 119 | |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 120 | ai = adj_glean_db_lookup(proto, sw_if_index, &conn->fp_addr); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 121 | |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 122 | if (ADJ_INDEX_INVALID == ai) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 123 | { |
| 124 | adj = adj_alloc(proto); |
| 125 | |
| 126 | adj->lookup_next_index = IP_LOOKUP_NEXT_GLEAN; |
| 127 | adj->ia_nh_proto = proto; |
Ole Troan | 6ee4051 | 2018-02-12 18:14:39 +0100 | [diff] [blame] | 128 | adj->ia_link = linkt; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 129 | adj->ia_node_index = adj_get_glean_node(proto); |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 130 | ai = adj_get_index(adj); |
| 131 | adj_lock(ai); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 132 | |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 133 | ASSERT(conn); |
| 134 | fib_prefix_normalize(conn, &adj->sub_type.glean.rx_pfx); |
Neale Ranns | c819fc6 | 2018-02-16 02:44:05 -0800 | [diff] [blame] | 135 | adj->rewrite_header.sw_if_index = sw_if_index; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 136 | adj->rewrite_header.data_bytes = 0; |
Ole Troan | 6ee4051 | 2018-02-12 18:14:39 +0100 | [diff] [blame] | 137 | adj->rewrite_header.max_l3_packet_bytes = |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 138 | vnet_sw_interface_get_mtu(vnet_get_main(), sw_if_index, |
| 139 | vnet_link_to_mtu(linkt)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 140 | |
Neale Ranns | c819fc6 | 2018-02-16 02:44:05 -0800 | [diff] [blame] | 141 | vnet_update_adjacency_for_sw_interface(vnet_get_main(), |
| 142 | sw_if_index, |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 143 | ai); |
| 144 | |
| 145 | adj_glean_db_insert(proto, sw_if_index, |
| 146 | &adj->sub_type.glean.rx_pfx.fp_addr, ai); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 147 | } |
| 148 | else |
| 149 | { |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 150 | adj = adj_get(ai); |
| 151 | adj_lock(ai); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Neale Ranns | 77cfc01 | 2019-12-15 22:26:37 +0000 | [diff] [blame] | 154 | adj_delegate_adj_created(adj); |
| 155 | |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 156 | return (ai); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 157 | } |
| 158 | |
Neale Ranns | c819fc6 | 2018-02-16 02:44:05 -0800 | [diff] [blame] | 159 | /** |
| 160 | * adj_glean_update_rewrite |
| 161 | */ |
| 162 | void |
| 163 | adj_glean_update_rewrite (adj_index_t adj_index) |
| 164 | { |
| 165 | ip_adjacency_t *adj; |
| 166 | |
| 167 | ASSERT(ADJ_INDEX_INVALID != adj_index); |
| 168 | |
| 169 | adj = adj_get(adj_index); |
| 170 | |
| 171 | vnet_rewrite_for_sw_interface(vnet_get_main(), |
| 172 | adj_fib_proto_2_nd(adj->ia_nh_proto), |
| 173 | adj->rewrite_header.sw_if_index, |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 174 | adj->ia_node_index, |
Neale Ranns | c819fc6 | 2018-02-16 02:44:05 -0800 | [diff] [blame] | 175 | VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST, |
| 176 | &adj->rewrite_header, |
| 177 | sizeof (adj->rewrite_data)); |
| 178 | } |
| 179 | |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 180 | static adj_walk_rc_t |
| 181 | adj_glean_update_rewrite_walk (adj_index_t ai, |
| 182 | void *data) |
| 183 | { |
| 184 | adj_glean_update_rewrite(ai); |
| 185 | |
| 186 | return (ADJ_WALK_RC_CONTINUE); |
| 187 | } |
| 188 | |
| 189 | void |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 190 | adj_glean_walk (u32 sw_if_index, |
| 191 | adj_walk_cb_t cb, |
| 192 | void *data) |
| 193 | { |
| 194 | fib_protocol_t proto; |
| 195 | |
| 196 | FOR_EACH_FIB_IP_PROTOCOL(proto) |
| 197 | { |
| 198 | adj_index_t ai, *aip, *ais = NULL; |
| 199 | ip46_address_t *conn; |
| 200 | |
| 201 | if (vec_len(adj_gleans[proto]) <= sw_if_index || |
| 202 | NULL == adj_gleans[proto][sw_if_index]) |
| 203 | continue; |
| 204 | |
| 205 | /* |
| 206 | * Walk first to collect the indices |
| 207 | * then walk the collection. This is safe |
| 208 | * to modifications of the hash table |
| 209 | */ |
| 210 | hash_foreach_mem(conn, ai, adj_gleans[proto][sw_if_index], |
| 211 | ({ |
| 212 | vec_add1(ais, ai); |
| 213 | })); |
| 214 | |
| 215 | vec_foreach(aip, ais) |
| 216 | { |
| 217 | if (ADJ_WALK_RC_STOP == cb(*aip, data)) |
| 218 | break; |
| 219 | } |
| 220 | vec_free(ais); |
| 221 | } |
| 222 | } |
| 223 | |
Neale Ranns | c819fc6 | 2018-02-16 02:44:05 -0800 | [diff] [blame] | 224 | adj_index_t |
| 225 | adj_glean_get (fib_protocol_t proto, |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 226 | u32 sw_if_index, |
| 227 | const ip46_address_t *nh) |
Neale Ranns | c819fc6 | 2018-02-16 02:44:05 -0800 | [diff] [blame] | 228 | { |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 229 | if (NULL != nh) |
Neale Ranns | c819fc6 | 2018-02-16 02:44:05 -0800 | [diff] [blame] | 230 | { |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 231 | return adj_glean_db_lookup(proto, sw_if_index, nh); |
| 232 | } |
| 233 | else |
| 234 | { |
| 235 | ip46_address_t *conn; |
| 236 | adj_index_t ai; |
| 237 | |
| 238 | if (vec_len(adj_gleans[proto]) <= sw_if_index || |
| 239 | NULL == adj_gleans[proto][sw_if_index]) |
| 240 | return (ADJ_INDEX_INVALID); |
| 241 | |
| 242 | hash_foreach_mem(conn, ai, adj_gleans[proto][sw_if_index], |
| 243 | ({ |
| 244 | return (ai); |
| 245 | })); |
Neale Ranns | c819fc6 | 2018-02-16 02:44:05 -0800 | [diff] [blame] | 246 | } |
| 247 | return (ADJ_INDEX_INVALID); |
| 248 | } |
| 249 | |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 250 | const ip46_address_t * |
| 251 | adj_glean_get_src (fib_protocol_t proto, |
| 252 | u32 sw_if_index, |
| 253 | const ip46_address_t *nh) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 254 | { |
Július Milan | 98874cd | 2021-02-16 19:20:47 +0100 | [diff] [blame] | 255 | const ip46_address_t *conn, *source; |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 256 | const ip_adjacency_t *adj; |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 257 | adj_index_t ai; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 258 | |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 259 | if (vec_len(adj_gleans[proto]) <= sw_if_index || |
| 260 | NULL == adj_gleans[proto][sw_if_index]) |
| 261 | return (NULL); |
| 262 | |
| 263 | fib_prefix_t pfx = { |
| 264 | .fp_len = fib_prefix_get_host_length(proto), |
| 265 | .fp_proto = proto, |
| 266 | }; |
| 267 | |
| 268 | if (nh) |
| 269 | pfx.fp_addr = *nh; |
| 270 | |
Július Milan | 98874cd | 2021-02-16 19:20:47 +0100 | [diff] [blame] | 271 | /* |
| 272 | * An interface can have more than one glean address. Where |
| 273 | * possible we want to return a source address from the same |
| 274 | * subnet as the destination. If this is not possible then any address |
| 275 | * will do. |
| 276 | */ |
| 277 | source = NULL; |
| 278 | |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 279 | hash_foreach_mem(conn, ai, adj_gleans[proto][sw_if_index], |
| 280 | ({ |
| 281 | adj = adj_get(ai); |
| 282 | |
| 283 | if (adj->sub_type.glean.rx_pfx.fp_len > 0) |
| 284 | { |
Július Milan | 98874cd | 2021-02-16 19:20:47 +0100 | [diff] [blame] | 285 | source = &adj->sub_type.glean.rx_pfx.fp_addr; |
| 286 | |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 287 | /* if no destination is specified use the just glean */ |
| 288 | if (NULL == nh) |
Július Milan | 98874cd | 2021-02-16 19:20:47 +0100 | [diff] [blame] | 289 | return (source); |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 290 | |
| 291 | /* check the clean covers the desintation */ |
| 292 | if (fib_prefix_is_cover(&adj->sub_type.glean.rx_pfx, &pfx)) |
Július Milan | 98874cd | 2021-02-16 19:20:47 +0100 | [diff] [blame] | 293 | return (source); |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 294 | } |
| 295 | })); |
| 296 | |
Július Milan | 98874cd | 2021-02-16 19:20:47 +0100 | [diff] [blame] | 297 | return (source); |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | void |
| 301 | adj_glean_remove (ip_adjacency_t *adj) |
| 302 | { |
| 303 | fib_prefix_t norm; |
| 304 | |
| 305 | fib_prefix_normalize(&adj->sub_type.glean.rx_pfx, |
| 306 | &norm); |
| 307 | adj_glean_db_remove(adj->ia_nh_proto, |
| 308 | adj->rewrite_header.sw_if_index, |
| 309 | &norm.fp_addr); |
| 310 | } |
| 311 | |
| 312 | static adj_walk_rc_t |
| 313 | adj_glean_start_backwalk (adj_index_t ai, |
| 314 | void *data) |
| 315 | { |
| 316 | fib_node_back_walk_ctx_t bw_ctx = *(fib_node_back_walk_ctx_t*) data; |
| 317 | |
| 318 | fib_walk_sync(FIB_NODE_TYPE_ADJ, ai, &bw_ctx); |
| 319 | |
| 320 | return (ADJ_WALK_RC_CONTINUE); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | static clib_error_t * |
| 324 | adj_glean_interface_state_change (vnet_main_t * vnm, |
| 325 | u32 sw_if_index, |
| 326 | u32 flags) |
| 327 | { |
| 328 | /* |
| 329 | * for each glean on the interface trigger a walk back to the children |
| 330 | */ |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 331 | fib_node_back_walk_ctx_t bw_ctx = { |
| 332 | .fnbw_reason = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP ? |
| 333 | FIB_NODE_BW_REASON_FLAG_INTERFACE_UP : |
| 334 | FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN), |
| 335 | }; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 336 | |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 337 | adj_glean_walk (sw_if_index, adj_glean_start_backwalk, &bw_ctx); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 338 | |
| 339 | return (NULL); |
| 340 | } |
| 341 | |
| 342 | VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(adj_glean_interface_state_change); |
| 343 | |
Neale Ranns | 8b37b87 | 2016-11-21 12:25:22 +0000 | [diff] [blame] | 344 | /** |
| 345 | * @brief Invoked on each SW interface of a HW interface when the |
| 346 | * HW interface state changes |
| 347 | */ |
Neale Ranns | 0053de6 | 2018-05-22 08:40:52 -0700 | [diff] [blame] | 348 | static walk_rc_t |
Neale Ranns | 8b37b87 | 2016-11-21 12:25:22 +0000 | [diff] [blame] | 349 | adj_nbr_hw_sw_interface_state_change (vnet_main_t * vnm, |
| 350 | u32 sw_if_index, |
| 351 | void *arg) |
| 352 | { |
| 353 | adj_glean_interface_state_change(vnm, sw_if_index, (uword) arg); |
Neale Ranns | 0053de6 | 2018-05-22 08:40:52 -0700 | [diff] [blame] | 354 | |
| 355 | return (WALK_CONTINUE); |
Neale Ranns | 8b37b87 | 2016-11-21 12:25:22 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /** |
| 359 | * @brief Registered callback for HW interface state changes |
| 360 | */ |
| 361 | static clib_error_t * |
| 362 | adj_glean_hw_interface_state_change (vnet_main_t * vnm, |
| 363 | u32 hw_if_index, |
| 364 | u32 flags) |
| 365 | { |
| 366 | /* |
| 367 | * walk SW interfaces on the HW |
| 368 | */ |
| 369 | uword sw_flags; |
| 370 | |
| 371 | sw_flags = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? |
| 372 | VNET_SW_INTERFACE_FLAG_ADMIN_UP : |
| 373 | 0); |
| 374 | |
| 375 | vnet_hw_interface_walk_sw(vnm, hw_if_index, |
| 376 | adj_nbr_hw_sw_interface_state_change, |
| 377 | (void*) sw_flags); |
| 378 | |
| 379 | return (NULL); |
| 380 | } |
| 381 | |
| 382 | VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION( |
| 383 | adj_glean_hw_interface_state_change); |
| 384 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 385 | static clib_error_t * |
| 386 | adj_glean_interface_delete (vnet_main_t * vnm, |
| 387 | u32 sw_if_index, |
| 388 | u32 is_add) |
| 389 | { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 390 | if (is_add) |
| 391 | { |
| 392 | /* |
| 393 | * not interested in interface additions. we will not back walk |
| 394 | * to resolve paths through newly added interfaces. Why? The control |
| 395 | * plane should have the brains to add interfaces first, then routes. |
| 396 | * So the case where there are paths with a interface that matches |
| 397 | * one just created is the case where the path resolved through an |
| 398 | * interface that was deleted, and still has not been removed. The |
| 399 | * new interface added, is NO GUARANTEE that the interface being |
| 400 | * added now, even though it may have the same sw_if_index, is the |
| 401 | * same interface that the path needs. So tough! |
| 402 | * If the control plane wants these routes to resolve it needs to |
| 403 | * remove and add them again. |
| 404 | */ |
| 405 | return (NULL); |
| 406 | } |
| 407 | |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 408 | /* |
| 409 | * for each glean on the interface trigger a walk back to the children |
| 410 | */ |
| 411 | fib_node_back_walk_ctx_t bw_ctx = { |
| 412 | .fnbw_reason = FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE, |
| 413 | }; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 414 | |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 415 | adj_glean_walk (sw_if_index, adj_glean_start_backwalk, &bw_ctx); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 416 | |
| 417 | return (NULL); |
| 418 | } |
| 419 | |
| 420 | VNET_SW_INTERFACE_ADD_DEL_FUNCTION(adj_glean_interface_delete); |
| 421 | |
Neale Ranns | ea8adf7 | 2021-08-13 08:10:59 +0000 | [diff] [blame] | 422 | /** |
| 423 | * Callback function invoked when an interface's MAC Address changes |
| 424 | */ |
| 425 | static void |
| 426 | adj_glean_ethernet_change_mac (ethernet_main_t * em, |
| 427 | u32 sw_if_index, uword opaque) |
| 428 | { |
| 429 | adj_glean_walk (sw_if_index, adj_glean_update_rewrite_walk, NULL); |
| 430 | } |
| 431 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 432 | u8* |
| 433 | format_adj_glean (u8* s, va_list *ap) |
| 434 | { |
Billy McFall | cfcf1e2 | 2016-10-14 09:51:49 -0400 | [diff] [blame] | 435 | index_t index = va_arg(*ap, index_t); |
| 436 | CLIB_UNUSED(u32 indent) = va_arg(*ap, u32); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 437 | ip_adjacency_t * adj = adj_get(index); |
| 438 | |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 439 | s = format(s, "%U-glean: [src:%U] %U", |
Neale Ranns | c819fc6 | 2018-02-16 02:44:05 -0800 | [diff] [blame] | 440 | format_fib_protocol, adj->ia_nh_proto, |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 441 | format_fib_prefix, &adj->sub_type.glean.rx_pfx, |
| 442 | format_vnet_rewrite, |
| 443 | &adj->rewrite_header, sizeof (adj->rewrite_data), 0); |
Neale Ranns | c819fc6 | 2018-02-16 02:44:05 -0800 | [diff] [blame] | 444 | |
| 445 | return (s); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 446 | } |
| 447 | |
Neale Ranns | e2fe097 | 2020-11-26 08:37:27 +0000 | [diff] [blame] | 448 | u32 |
| 449 | adj_glean_db_size (void) |
| 450 | { |
| 451 | fib_protocol_t proto; |
| 452 | u32 sw_if_index = 0; |
| 453 | u64 count = 0; |
| 454 | |
| 455 | FOR_EACH_FIB_IP_PROTOCOL(proto) |
| 456 | { |
| 457 | vec_foreach_index(sw_if_index, adj_gleans[proto]) |
| 458 | { |
| 459 | if (NULL != adj_gleans[proto][sw_if_index]) |
| 460 | { |
| 461 | count += hash_elts(adj_gleans[proto][sw_if_index]); |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | return (count); |
| 466 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 467 | |
| 468 | static void |
| 469 | adj_dpo_lock (dpo_id_t *dpo) |
| 470 | { |
| 471 | adj_lock(dpo->dpoi_index); |
| 472 | } |
| 473 | static void |
| 474 | adj_dpo_unlock (dpo_id_t *dpo) |
| 475 | { |
| 476 | adj_unlock(dpo->dpoi_index); |
| 477 | } |
| 478 | |
| 479 | const static dpo_vft_t adj_glean_dpo_vft = { |
| 480 | .dv_lock = adj_dpo_lock, |
| 481 | .dv_unlock = adj_dpo_unlock, |
| 482 | .dv_format = format_adj_glean, |
Andrew Yourtchenko | 5f3fcb9 | 2017-10-25 05:50:37 -0700 | [diff] [blame] | 483 | .dv_get_urpf = adj_dpo_get_urpf, |
Neale Ranns | 8f5fef2 | 2020-12-21 08:29:34 +0000 | [diff] [blame] | 484 | .dv_get_mtu = adj_dpo_get_mtu, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 485 | }; |
| 486 | |
| 487 | /** |
| 488 | * @brief The per-protocol VLIB graph nodes that are assigned to a glean |
| 489 | * object. |
| 490 | * |
| 491 | * this means that these graph nodes are ones from which a glean is the |
| 492 | * parent object in the DPO-graph. |
| 493 | */ |
| 494 | const static char* const glean_ip4_nodes[] = |
| 495 | { |
| 496 | "ip4-glean", |
| 497 | NULL, |
| 498 | }; |
| 499 | const static char* const glean_ip6_nodes[] = |
| 500 | { |
| 501 | "ip6-glean", |
| 502 | NULL, |
| 503 | }; |
| 504 | |
| 505 | const static char* const * const glean_nodes[DPO_PROTO_NUM] = |
| 506 | { |
| 507 | [DPO_PROTO_IP4] = glean_ip4_nodes, |
| 508 | [DPO_PROTO_IP6] = glean_ip6_nodes, |
| 509 | [DPO_PROTO_MPLS] = NULL, |
| 510 | }; |
| 511 | |
| 512 | void |
| 513 | adj_glean_module_init (void) |
| 514 | { |
| 515 | dpo_register(DPO_ADJACENCY_GLEAN, &adj_glean_dpo_vft, glean_nodes); |
Neale Ranns | ea8adf7 | 2021-08-13 08:10:59 +0000 | [diff] [blame] | 516 | |
| 517 | ethernet_address_change_ctx_t ctx = { |
| 518 | .function = adj_glean_ethernet_change_mac, |
| 519 | .function_opaque = 0, |
| 520 | }; |
| 521 | vec_add1 (ethernet_main.address_change_callbacks, ctx); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 522 | } |