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. |
| 22 | * There is only one glean per-interface per-protocol, so this is a per-interface |
| 23 | * vector |
| 24 | */ |
| 25 | static adj_index_t *adj_gleans[FIB_PROTOCOL_MAX]; |
| 26 | |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 27 | static inline u32 |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 28 | adj_get_glean_node (fib_protocol_t proto) |
| 29 | { |
| 30 | switch (proto) { |
| 31 | case FIB_PROTOCOL_IP4: |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 32 | return (ip4_glean_node.index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 33 | case FIB_PROTOCOL_IP6: |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 34 | return (ip6_glean_node.index); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 35 | case FIB_PROTOCOL_MPLS: |
| 36 | break; |
| 37 | } |
| 38 | ASSERT(0); |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 39 | return (~0); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /* |
| 43 | * adj_glean_add_or_lock |
| 44 | * |
| 45 | * The next_hop address here is used for source address selection in the DP. |
| 46 | * The glean adj is added to an interface's connected prefix, the next-hop |
| 47 | * passed here is the local prefix on the same interface. |
| 48 | */ |
| 49 | adj_index_t |
| 50 | adj_glean_add_or_lock (fib_protocol_t proto, |
Ole Troan | 6ee4051 | 2018-02-12 18:14:39 +0100 | [diff] [blame] | 51 | vnet_link_t linkt, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 52 | u32 sw_if_index, |
| 53 | const ip46_address_t *nh_addr) |
| 54 | { |
| 55 | ip_adjacency_t * adj; |
| 56 | |
| 57 | vec_validate_init_empty(adj_gleans[proto], sw_if_index, ADJ_INDEX_INVALID); |
| 58 | |
| 59 | if (ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index]) |
| 60 | { |
| 61 | adj = adj_alloc(proto); |
| 62 | |
| 63 | adj->lookup_next_index = IP_LOOKUP_NEXT_GLEAN; |
| 64 | adj->ia_nh_proto = proto; |
Ole Troan | 6ee4051 | 2018-02-12 18:14:39 +0100 | [diff] [blame] | 65 | adj->ia_link = linkt; |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 66 | adj->ia_node_index = adj_get_glean_node(proto); |
Neale Ranns | 6c3ebcc | 2016-10-02 21:20:15 +0100 | [diff] [blame] | 67 | adj_gleans[proto][sw_if_index] = adj_get_index(adj); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 68 | |
| 69 | if (NULL != nh_addr) |
| 70 | { |
| 71 | adj->sub_type.glean.receive_addr = *nh_addr; |
| 72 | } |
Ole Troan | 6ee4051 | 2018-02-12 18:14:39 +0100 | [diff] [blame] | 73 | else |
| 74 | { |
| 75 | adj->sub_type.glean.receive_addr = zero_addr; |
| 76 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 77 | |
Neale Ranns | c819fc6 | 2018-02-16 02:44:05 -0800 | [diff] [blame] | 78 | adj->rewrite_header.sw_if_index = sw_if_index; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 79 | adj->rewrite_header.data_bytes = 0; |
Ole Troan | 6ee4051 | 2018-02-12 18:14:39 +0100 | [diff] [blame] | 80 | adj->rewrite_header.max_l3_packet_bytes = |
Ole Troan | d723161 | 2018-06-07 10:17:57 +0200 | [diff] [blame] | 81 | vnet_sw_interface_get_mtu(vnet_get_main(), sw_if_index, |
| 82 | vnet_link_to_mtu(linkt)); |
Neale Ranns | c819fc6 | 2018-02-16 02:44:05 -0800 | [diff] [blame] | 83 | adj_lock(adj_get_index(adj)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 84 | |
Neale Ranns | c819fc6 | 2018-02-16 02:44:05 -0800 | [diff] [blame] | 85 | vnet_update_adjacency_for_sw_interface(vnet_get_main(), |
| 86 | sw_if_index, |
| 87 | adj_get_index(adj)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 88 | } |
| 89 | else |
| 90 | { |
| 91 | adj = adj_get(adj_gleans[proto][sw_if_index]); |
Neale Ranns | c819fc6 | 2018-02-16 02:44:05 -0800 | [diff] [blame] | 92 | adj_lock(adj_get_index(adj)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Neale Ranns | 77cfc01 | 2019-12-15 22:26:37 +0000 | [diff] [blame] | 95 | adj_delegate_adj_created(adj); |
| 96 | |
Neale Ranns | 6c3ebcc | 2016-10-02 21:20:15 +0100 | [diff] [blame] | 97 | return (adj_get_index(adj)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Neale Ranns | c819fc6 | 2018-02-16 02:44:05 -0800 | [diff] [blame] | 100 | /** |
| 101 | * adj_glean_update_rewrite |
| 102 | */ |
| 103 | void |
| 104 | adj_glean_update_rewrite (adj_index_t adj_index) |
| 105 | { |
| 106 | ip_adjacency_t *adj; |
| 107 | |
| 108 | ASSERT(ADJ_INDEX_INVALID != adj_index); |
| 109 | |
| 110 | adj = adj_get(adj_index); |
| 111 | |
| 112 | vnet_rewrite_for_sw_interface(vnet_get_main(), |
| 113 | adj_fib_proto_2_nd(adj->ia_nh_proto), |
| 114 | adj->rewrite_header.sw_if_index, |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 115 | adj->ia_node_index, |
Neale Ranns | c819fc6 | 2018-02-16 02:44:05 -0800 | [diff] [blame] | 116 | VNET_REWRITE_FOR_SW_INTERFACE_ADDRESS_BROADCAST, |
| 117 | &adj->rewrite_header, |
| 118 | sizeof (adj->rewrite_data)); |
| 119 | } |
| 120 | |
| 121 | adj_index_t |
| 122 | adj_glean_get (fib_protocol_t proto, |
| 123 | u32 sw_if_index) |
| 124 | { |
| 125 | if (sw_if_index < vec_len(adj_gleans[proto])) |
| 126 | { |
| 127 | return (adj_gleans[proto][sw_if_index]); |
| 128 | } |
| 129 | return (ADJ_INDEX_INVALID); |
| 130 | } |
| 131 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 132 | void |
| 133 | adj_glean_remove (fib_protocol_t proto, |
| 134 | u32 sw_if_index) |
| 135 | { |
| 136 | ASSERT(sw_if_index < vec_len(adj_gleans[proto])); |
| 137 | |
| 138 | adj_gleans[proto][sw_if_index] = ADJ_INDEX_INVALID; |
| 139 | } |
| 140 | |
| 141 | static clib_error_t * |
| 142 | adj_glean_interface_state_change (vnet_main_t * vnm, |
| 143 | u32 sw_if_index, |
| 144 | u32 flags) |
| 145 | { |
| 146 | /* |
| 147 | * for each glean on the interface trigger a walk back to the children |
| 148 | */ |
| 149 | fib_protocol_t proto; |
| 150 | ip_adjacency_t *adj; |
| 151 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 152 | FOR_EACH_FIB_IP_PROTOCOL(proto) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 153 | { |
| 154 | if (sw_if_index >= vec_len(adj_gleans[proto]) || |
| 155 | ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index]) |
| 156 | continue; |
| 157 | |
| 158 | adj = adj_get(adj_gleans[proto][sw_if_index]); |
| 159 | |
| 160 | fib_node_back_walk_ctx_t bw_ctx = { |
| 161 | .fnbw_reason = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP ? |
| 162 | FIB_NODE_BW_REASON_FLAG_INTERFACE_UP : |
| 163 | FIB_NODE_BW_REASON_FLAG_INTERFACE_DOWN), |
| 164 | }; |
| 165 | |
Neale Ranns | 6c3ebcc | 2016-10-02 21:20:15 +0100 | [diff] [blame] | 166 | fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | return (NULL); |
| 170 | } |
| 171 | |
| 172 | VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION(adj_glean_interface_state_change); |
| 173 | |
Neale Ranns | 8b37b87 | 2016-11-21 12:25:22 +0000 | [diff] [blame] | 174 | /** |
| 175 | * @brief Invoked on each SW interface of a HW interface when the |
| 176 | * HW interface state changes |
| 177 | */ |
Neale Ranns | 0053de6 | 2018-05-22 08:40:52 -0700 | [diff] [blame] | 178 | static walk_rc_t |
Neale Ranns | 8b37b87 | 2016-11-21 12:25:22 +0000 | [diff] [blame] | 179 | adj_nbr_hw_sw_interface_state_change (vnet_main_t * vnm, |
| 180 | u32 sw_if_index, |
| 181 | void *arg) |
| 182 | { |
| 183 | adj_glean_interface_state_change(vnm, sw_if_index, (uword) arg); |
Neale Ranns | 0053de6 | 2018-05-22 08:40:52 -0700 | [diff] [blame] | 184 | |
| 185 | return (WALK_CONTINUE); |
Neale Ranns | 8b37b87 | 2016-11-21 12:25:22 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /** |
| 189 | * @brief Registered callback for HW interface state changes |
| 190 | */ |
| 191 | static clib_error_t * |
| 192 | adj_glean_hw_interface_state_change (vnet_main_t * vnm, |
| 193 | u32 hw_if_index, |
| 194 | u32 flags) |
| 195 | { |
| 196 | /* |
| 197 | * walk SW interfaces on the HW |
| 198 | */ |
| 199 | uword sw_flags; |
| 200 | |
| 201 | sw_flags = ((flags & VNET_HW_INTERFACE_FLAG_LINK_UP) ? |
| 202 | VNET_SW_INTERFACE_FLAG_ADMIN_UP : |
| 203 | 0); |
| 204 | |
| 205 | vnet_hw_interface_walk_sw(vnm, hw_if_index, |
| 206 | adj_nbr_hw_sw_interface_state_change, |
| 207 | (void*) sw_flags); |
| 208 | |
| 209 | return (NULL); |
| 210 | } |
| 211 | |
| 212 | VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION( |
| 213 | adj_glean_hw_interface_state_change); |
| 214 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 215 | static clib_error_t * |
| 216 | adj_glean_interface_delete (vnet_main_t * vnm, |
| 217 | u32 sw_if_index, |
| 218 | u32 is_add) |
| 219 | { |
| 220 | /* |
| 221 | * for each glean on the interface trigger a walk back to the children |
| 222 | */ |
| 223 | fib_protocol_t proto; |
| 224 | ip_adjacency_t *adj; |
| 225 | |
| 226 | if (is_add) |
| 227 | { |
| 228 | /* |
| 229 | * not interested in interface additions. we will not back walk |
| 230 | * to resolve paths through newly added interfaces. Why? The control |
| 231 | * plane should have the brains to add interfaces first, then routes. |
| 232 | * So the case where there are paths with a interface that matches |
| 233 | * one just created is the case where the path resolved through an |
| 234 | * interface that was deleted, and still has not been removed. The |
| 235 | * new interface added, is NO GUARANTEE that the interface being |
| 236 | * added now, even though it may have the same sw_if_index, is the |
| 237 | * same interface that the path needs. So tough! |
| 238 | * If the control plane wants these routes to resolve it needs to |
| 239 | * remove and add them again. |
| 240 | */ |
| 241 | return (NULL); |
| 242 | } |
| 243 | |
Neale Ranns | 2828721 | 2019-12-16 00:53:11 +0000 | [diff] [blame] | 244 | FOR_EACH_FIB_IP_PROTOCOL(proto) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 245 | { |
| 246 | if (sw_if_index >= vec_len(adj_gleans[proto]) || |
| 247 | ADJ_INDEX_INVALID == adj_gleans[proto][sw_if_index]) |
| 248 | continue; |
| 249 | |
| 250 | adj = adj_get(adj_gleans[proto][sw_if_index]); |
| 251 | |
| 252 | fib_node_back_walk_ctx_t bw_ctx = { |
| 253 | .fnbw_reason = FIB_NODE_BW_REASON_FLAG_INTERFACE_DELETE, |
| 254 | }; |
| 255 | |
Neale Ranns | 6c3ebcc | 2016-10-02 21:20:15 +0100 | [diff] [blame] | 256 | fib_walk_sync(FIB_NODE_TYPE_ADJ, adj_get_index(adj), &bw_ctx); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | return (NULL); |
| 260 | } |
| 261 | |
| 262 | VNET_SW_INTERFACE_ADD_DEL_FUNCTION(adj_glean_interface_delete); |
| 263 | |
| 264 | u8* |
| 265 | format_adj_glean (u8* s, va_list *ap) |
| 266 | { |
Billy McFall | cfcf1e2 | 2016-10-14 09:51:49 -0400 | [diff] [blame] | 267 | index_t index = va_arg(*ap, index_t); |
| 268 | CLIB_UNUSED(u32 indent) = va_arg(*ap, u32); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 269 | ip_adjacency_t * adj = adj_get(index); |
| 270 | |
Neale Ranns | c819fc6 | 2018-02-16 02:44:05 -0800 | [diff] [blame] | 271 | s = format(s, "%U-glean: %U", |
| 272 | format_fib_protocol, adj->ia_nh_proto, |
Neale Ranns | c819fc6 | 2018-02-16 02:44:05 -0800 | [diff] [blame] | 273 | format_vnet_rewrite, |
| 274 | &adj->rewrite_header, sizeof (adj->rewrite_data), 0); |
| 275 | |
| 276 | return (s); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | |
| 280 | static void |
| 281 | adj_dpo_lock (dpo_id_t *dpo) |
| 282 | { |
| 283 | adj_lock(dpo->dpoi_index); |
| 284 | } |
| 285 | static void |
| 286 | adj_dpo_unlock (dpo_id_t *dpo) |
| 287 | { |
| 288 | adj_unlock(dpo->dpoi_index); |
| 289 | } |
| 290 | |
| 291 | const static dpo_vft_t adj_glean_dpo_vft = { |
| 292 | .dv_lock = adj_dpo_lock, |
| 293 | .dv_unlock = adj_dpo_unlock, |
| 294 | .dv_format = format_adj_glean, |
Andrew Yourtchenko | 5f3fcb9 | 2017-10-25 05:50:37 -0700 | [diff] [blame] | 295 | .dv_get_urpf = adj_dpo_get_urpf, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 296 | }; |
| 297 | |
| 298 | /** |
| 299 | * @brief The per-protocol VLIB graph nodes that are assigned to a glean |
| 300 | * object. |
| 301 | * |
| 302 | * this means that these graph nodes are ones from which a glean is the |
| 303 | * parent object in the DPO-graph. |
| 304 | */ |
| 305 | const static char* const glean_ip4_nodes[] = |
| 306 | { |
| 307 | "ip4-glean", |
| 308 | NULL, |
| 309 | }; |
| 310 | const static char* const glean_ip6_nodes[] = |
| 311 | { |
| 312 | "ip6-glean", |
| 313 | NULL, |
| 314 | }; |
| 315 | |
| 316 | const static char* const * const glean_nodes[DPO_PROTO_NUM] = |
| 317 | { |
| 318 | [DPO_PROTO_IP4] = glean_ip4_nodes, |
| 319 | [DPO_PROTO_IP6] = glean_ip6_nodes, |
| 320 | [DPO_PROTO_MPLS] = NULL, |
| 321 | }; |
| 322 | |
| 323 | void |
| 324 | adj_glean_module_init (void) |
| 325 | { |
| 326 | dpo_register(DPO_ADJACENCY_GLEAN, &adj_glean_dpo_vft, glean_nodes); |
| 327 | } |