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 <vlib/vlib.h> |
| 17 | #include <vnet/ip/format.h> |
| 18 | #include <vnet/ip/lookup.h> |
| 19 | #include <vnet/adj/adj.h> |
Neale Ranns | 3dffb1e | 2016-11-01 20:38:53 +0000 | [diff] [blame] | 20 | #include <vnet/dpo/drop_dpo.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 21 | |
| 22 | #include "fib_entry_src.h" |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 23 | #include "fib_entry_src_rr.h" |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 24 | #include "fib_entry_cover.h" |
| 25 | #include "fib_entry.h" |
| 26 | #include "fib_table.h" |
| 27 | |
| 28 | /* |
| 29 | * fib_entry_src_rr_resolve_via_connected |
| 30 | * |
| 31 | * Resolve via a connected cover. |
| 32 | */ |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 33 | void |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 34 | fib_entry_src_rr_resolve_via_connected (fib_entry_src_t *src, |
| 35 | const fib_entry_t *fib_entry, |
| 36 | const fib_entry_t *cover) |
| 37 | { |
| 38 | const fib_route_path_t path = { |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 39 | .frp_proto = fib_proto_to_dpo(fib_entry->fe_prefix.fp_proto), |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 40 | .frp_addr = fib_entry->fe_prefix.fp_addr, |
| 41 | .frp_sw_if_index = fib_entry_get_resolving_interface( |
| 42 | fib_entry_get_index(cover)), |
| 43 | .frp_fib_index = ~0, |
| 44 | .frp_weight = 1, |
| 45 | }; |
| 46 | fib_route_path_t *paths = NULL; |
| 47 | vec_add1(paths, path); |
| 48 | |
| 49 | /* |
| 50 | * since the cover is connected, the address this entry corresponds |
| 51 | * to is a peer (ARP-able for) on the interface to which the cover is |
| 52 | * connected. The fact we resolve via the cover, just means this RR |
| 53 | * source is the first SRC to use said peer. The ARP source will be along |
| 54 | * shortly to over-rule this RR source. |
| 55 | */ |
| 56 | src->fes_pl = fib_path_list_create(FIB_PATH_LIST_FLAG_NONE, paths); |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 57 | src->fes_entry_flags |= (fib_entry_get_flags(fib_entry_get_index(cover)) & |
| 58 | FIB_ENTRY_FLAGS_RR_INHERITED); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 59 | |
| 60 | vec_free(paths); |
| 61 | } |
| 62 | |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 63 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 64 | /** |
| 65 | * Source initialisation Function |
| 66 | */ |
| 67 | static void |
| 68 | fib_entry_src_rr_init (fib_entry_src_t *src) |
| 69 | { |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 70 | src->u.rr.fesr_cover = FIB_NODE_INDEX_INVALID; |
| 71 | src->u.rr.fesr_sibling = FIB_NODE_INDEX_INVALID; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Neale Ranns | 08b1648 | 2017-05-13 05:52:58 -0700 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * use the path-list of the cover, unless it would form a loop. |
| 77 | * that is unless the cover is via this entry. |
| 78 | * If a loop were to form it would be a 1 level loop (i.e. X via X), |
| 79 | * and there would be 2 locks on the path-list; one since its used |
| 80 | * by the cover, and 1 from here. The first lock will go when the |
| 81 | * cover is removed, the second, and last, when the covered walk |
| 82 | * occurs during the cover's removel - this is not a place where |
| 83 | * we can handle last lock gone. |
| 84 | * In short, don't let the loop form. The usual rules of 'we must |
| 85 | * let it form so we know when it breaks' don't apply here, since |
| 86 | * the loop will break when the cover changes, and this function |
| 87 | * will be called again when that happens. |
| 88 | */ |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 89 | void |
Neale Ranns | 08b1648 | 2017-05-13 05:52:58 -0700 | [diff] [blame] | 90 | fib_entry_src_rr_use_covers_pl (fib_entry_src_t *src, |
| 91 | const fib_entry_t *fib_entry, |
| 92 | const fib_entry_t *cover) |
| 93 | { |
| 94 | fib_node_index_t *entries = NULL; |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 95 | dpo_proto_t proto; |
Neale Ranns | 08b1648 | 2017-05-13 05:52:58 -0700 | [diff] [blame] | 96 | |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 97 | proto = fib_proto_to_dpo(fib_entry->fe_prefix.fp_proto); |
Neale Ranns | 08b1648 | 2017-05-13 05:52:58 -0700 | [diff] [blame] | 98 | vec_add1(entries, fib_entry_get_index(fib_entry)); |
| 99 | |
| 100 | if (fib_path_list_recursive_loop_detect(cover->fe_parent, |
| 101 | &entries)) |
| 102 | { |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 103 | src->fes_pl = fib_path_list_create_special(proto, |
| 104 | FIB_PATH_LIST_FLAG_DROP, |
| 105 | drop_dpo_get(proto)); |
Neale Ranns | 08b1648 | 2017-05-13 05:52:58 -0700 | [diff] [blame] | 106 | } |
| 107 | else |
| 108 | { |
| 109 | src->fes_pl = cover->fe_parent; |
| 110 | } |
| 111 | vec_free(entries); |
| 112 | } |
| 113 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 114 | /* |
| 115 | * Source activation. Called when the source is the new best source on the entry |
| 116 | */ |
| 117 | static int |
| 118 | fib_entry_src_rr_activate (fib_entry_src_t *src, |
| 119 | const fib_entry_t *fib_entry) |
| 120 | { |
| 121 | fib_entry_t *cover; |
| 122 | |
| 123 | /* |
| 124 | * find the covering prefix. become a dependent thereof. |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 125 | * for IP there should always be a cover, though it may be the default route. |
| 126 | * For MPLS there is never a cover. |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 127 | */ |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 128 | if (FIB_PROTOCOL_MPLS == fib_entry->fe_prefix.fp_proto) |
| 129 | { |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 130 | src->fes_pl = fib_path_list_create_special(DPO_PROTO_MPLS, |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 131 | FIB_PATH_LIST_FLAG_DROP, |
| 132 | NULL); |
| 133 | fib_path_list_lock(src->fes_pl); |
| 134 | return (!0); |
| 135 | } |
| 136 | |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 137 | src->u.rr.fesr_cover = fib_table_get_less_specific(fib_entry->fe_fib_index, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 138 | &fib_entry->fe_prefix); |
| 139 | |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 140 | ASSERT(FIB_NODE_INDEX_INVALID != src->u.rr.fesr_cover); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 141 | |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 142 | cover = fib_entry_get(src->u.rr.fesr_cover); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 143 | |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 144 | src->u.rr.fesr_sibling = |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 145 | fib_entry_cover_track(cover, fib_entry_get_index(fib_entry)); |
| 146 | |
| 147 | /* |
Neale Ranns | f12a83f | 2017-04-18 09:09:40 -0700 | [diff] [blame] | 148 | * if the cover is attached then install an attached-host path |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 149 | * (like an adj-fib). Otherwise inherit the forwarding from the cover |
| 150 | */ |
| 151 | if (FIB_ENTRY_FLAG_ATTACHED & fib_entry_get_flags_i(cover)) |
| 152 | { |
| 153 | fib_entry_src_rr_resolve_via_connected(src, fib_entry, cover); |
| 154 | } |
| 155 | else |
| 156 | { |
Neale Ranns | 08b1648 | 2017-05-13 05:52:58 -0700 | [diff] [blame] | 157 | fib_entry_src_rr_use_covers_pl(src, fib_entry, cover); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 158 | } |
| 159 | fib_path_list_lock(src->fes_pl); |
| 160 | |
| 161 | /* |
| 162 | * return go for install |
| 163 | */ |
| 164 | return (!0); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Source Deactivate. |
| 169 | * Called when the source is no longer best source on the entry |
| 170 | */ |
| 171 | static void |
| 172 | fib_entry_src_rr_deactivate (fib_entry_src_t *src, |
| 173 | const fib_entry_t *fib_entry) |
| 174 | { |
| 175 | fib_entry_t *cover; |
| 176 | |
| 177 | /* |
| 178 | * remove the depednecy on the covering entry |
| 179 | */ |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 180 | if (FIB_NODE_INDEX_INVALID != src->u.rr.fesr_cover) |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 181 | { |
mu.duojiao | 11232e5 | 2019-02-14 16:57:54 +0800 | [diff] [blame^] | 182 | fib_node_index_t *entries = NULL; |
| 183 | |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 184 | cover = fib_entry_get(src->u.rr.fesr_cover); |
| 185 | fib_entry_cover_untrack(cover, src->u.rr.fesr_sibling); |
| 186 | src->u.rr.fesr_cover = FIB_NODE_INDEX_INVALID; |
mu.duojiao | 11232e5 | 2019-02-14 16:57:54 +0800 | [diff] [blame^] | 187 | |
| 188 | if (FIB_NODE_INDEX_INVALID != cover->fe_parent) |
| 189 | { |
| 190 | fib_path_list_recursive_loop_detect(cover->fe_parent, &entries); |
| 191 | |
| 192 | vec_free(entries); |
| 193 | } |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 194 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 195 | |
| 196 | fib_path_list_unlock(src->fes_pl); |
| 197 | src->fes_pl = FIB_NODE_INDEX_INVALID; |
| 198 | src->fes_entry_flags = FIB_ENTRY_FLAG_NONE; |
| 199 | } |
| 200 | |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 201 | fib_entry_src_cover_res_t |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 202 | fib_entry_src_rr_cover_change (fib_entry_src_t *src, |
| 203 | const fib_entry_t *fib_entry) |
| 204 | { |
| 205 | fib_entry_src_cover_res_t res = { |
| 206 | .install = !0, |
| 207 | .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE, |
| 208 | }; |
| 209 | |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 210 | if (FIB_NODE_INDEX_INVALID == src->u.rr.fesr_cover) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 211 | { |
| 212 | /* |
| 213 | * the source may be added, but it is not active |
| 214 | * if it is not tracking the cover. |
| 215 | */ |
| 216 | return (res); |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * this function is called when this entry's cover has a more specific |
| 221 | * entry inserted benaeth it. That does not necessarily mean that this |
| 222 | * entry is covered by the new prefix. check that |
| 223 | */ |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 224 | if (src->u.rr.fesr_cover != fib_table_get_less_specific(fib_entry->fe_fib_index, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 225 | &fib_entry->fe_prefix)) |
| 226 | { |
| 227 | fib_entry_src_rr_deactivate(src, fib_entry); |
| 228 | fib_entry_src_rr_activate(src, fib_entry); |
| 229 | |
| 230 | /* |
| 231 | * dependent children need to re-resolve to the new forwarding info |
| 232 | */ |
| 233 | res.bw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE; |
| 234 | } |
| 235 | return (res); |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * fib_entry_src_rr_cover_update |
| 240 | * |
| 241 | * This entry's cover has updated its forwarding info. This entry |
| 242 | * will need to re-inheret. |
| 243 | */ |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 244 | fib_entry_src_cover_res_t |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 245 | fib_entry_src_rr_cover_update (fib_entry_src_t *src, |
| 246 | const fib_entry_t *fib_entry) |
| 247 | { |
| 248 | fib_entry_src_cover_res_t res = { |
| 249 | .install = !0, |
| 250 | .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE, |
| 251 | }; |
| 252 | fib_node_index_t old_path_list; |
| 253 | fib_entry_t *cover; |
| 254 | |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 255 | if (FIB_NODE_INDEX_INVALID == src->u.rr.fesr_cover) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 256 | { |
| 257 | /* |
| 258 | * the source may be added, but it is not active |
| 259 | * if it is not tracking the cover. |
| 260 | */ |
| 261 | return (res); |
| 262 | } |
| 263 | |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 264 | cover = fib_entry_get(src->u.rr.fesr_cover); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 265 | old_path_list = src->fes_pl; |
| 266 | |
| 267 | /* |
| 268 | * if the ocver is attached then install an attached-host path |
| 269 | * (like an adj-fib). Otherwise inherit the forwarding from the cover |
| 270 | */ |
| 271 | if (FIB_ENTRY_FLAG_ATTACHED & fib_entry_get_flags_i(cover)) |
| 272 | { |
| 273 | fib_entry_src_rr_resolve_via_connected(src, fib_entry, cover); |
| 274 | } |
| 275 | else |
| 276 | { |
Neale Ranns | 08b1648 | 2017-05-13 05:52:58 -0700 | [diff] [blame] | 277 | fib_entry_src_rr_use_covers_pl(src, fib_entry, cover); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 278 | } |
| 279 | fib_path_list_lock(src->fes_pl); |
| 280 | fib_path_list_unlock(old_path_list); |
| 281 | |
| 282 | /* |
| 283 | * dependent children need to re-resolve to the new forwarding info |
| 284 | */ |
| 285 | res.bw_reason = FIB_NODE_BW_REASON_FLAG_EVALUATE; |
| 286 | |
| 287 | return (res); |
| 288 | } |
| 289 | |
| 290 | static u8* |
| 291 | fib_entry_src_rr_format (fib_entry_src_t *src, |
| 292 | u8* s) |
| 293 | { |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 294 | return (format(s, " cover:%d", src->u.rr.fesr_cover)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | const static fib_entry_src_vft_t rr_src_vft = { |
| 298 | .fesv_init = fib_entry_src_rr_init, |
| 299 | .fesv_activate = fib_entry_src_rr_activate, |
| 300 | .fesv_deactivate = fib_entry_src_rr_deactivate, |
| 301 | .fesv_cover_change = fib_entry_src_rr_cover_change, |
| 302 | .fesv_cover_update = fib_entry_src_rr_cover_update, |
| 303 | .fesv_format = fib_entry_src_rr_format, |
| 304 | }; |
| 305 | |
| 306 | void |
| 307 | fib_entry_src_rr_register (void) |
| 308 | { |
| 309 | fib_entry_src_register(FIB_SOURCE_RR, &rr_src_vft); |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 310 | fib_entry_src_register(FIB_SOURCE_URPF_EXEMPT, &rr_src_vft); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 311 | } |