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> |
| 17 | #include <vnet/dpo/load_balance.h> |
| 18 | #include <vnet/dpo/mpls_label_dpo.h> |
| 19 | #include <vnet/dpo/drop_dpo.h> |
| 20 | |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 21 | #include <vnet/fib/fib_entry_src.h> |
| 22 | #include <vnet/fib/fib_table.h> |
| 23 | #include <vnet/fib/fib_path_ext.h> |
| 24 | #include <vnet/fib/fib_urpf_list.h> |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | * per-source type vft |
| 28 | */ |
| 29 | static fib_entry_src_vft_t fib_entry_src_vft[FIB_SOURCE_MAX]; |
| 30 | |
| 31 | static fib_protocol_t |
| 32 | fib_entry_get_proto (const fib_entry_t * fib_entry) |
| 33 | { |
| 34 | return (fib_entry->fe_prefix.fp_proto); |
| 35 | } |
| 36 | |
| 37 | void |
| 38 | fib_entry_src_register (fib_source_t source, |
| 39 | const fib_entry_src_vft_t *vft) |
| 40 | { |
| 41 | fib_entry_src_vft[source] = *vft; |
| 42 | } |
| 43 | |
| 44 | static int |
| 45 | fib_entry_src_cmp_for_sort (void * v1, |
| 46 | void * v2) |
| 47 | { |
| 48 | fib_entry_src_t *esrc1 = v1, *esrc2 = v2; |
| 49 | |
| 50 | return (esrc1->fes_src - esrc2->fes_src); |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | fib_entry_src_action_init (fib_entry_t *fib_entry, |
| 55 | fib_source_t source) |
| 56 | |
| 57 | { |
| 58 | fib_entry_src_t esrc = { |
| 59 | .fes_pl = FIB_NODE_INDEX_INVALID, |
| 60 | .fes_flags = FIB_ENTRY_SRC_FLAG_NONE, |
| 61 | .fes_src = source, |
| 62 | }; |
| 63 | |
| 64 | if (NULL != fib_entry_src_vft[source].fesv_init) |
| 65 | { |
| 66 | fib_entry_src_vft[source].fesv_init(&esrc); |
| 67 | } |
| 68 | |
| 69 | vec_add1(fib_entry->fe_srcs, esrc); |
| 70 | vec_sort_with_function(fib_entry->fe_srcs, |
| 71 | fib_entry_src_cmp_for_sort); |
| 72 | } |
| 73 | |
| 74 | static fib_entry_src_t * |
| 75 | fib_entry_src_find (const fib_entry_t *fib_entry, |
| 76 | fib_source_t source, |
| 77 | u32 *index) |
| 78 | |
| 79 | { |
| 80 | fib_entry_src_t *esrc; |
| 81 | int ii; |
| 82 | |
| 83 | ii = 0; |
| 84 | vec_foreach(esrc, fib_entry->fe_srcs) |
| 85 | { |
| 86 | if (esrc->fes_src == source) |
| 87 | { |
| 88 | if (NULL != index) |
| 89 | { |
| 90 | *index = ii; |
| 91 | } |
| 92 | return (esrc); |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | ii++; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return (NULL); |
| 101 | } |
| 102 | |
| 103 | int |
| 104 | fib_entry_is_sourced (fib_node_index_t fib_entry_index, |
| 105 | fib_source_t source) |
| 106 | { |
| 107 | fib_entry_t *fib_entry; |
| 108 | |
| 109 | fib_entry = fib_entry_get(fib_entry_index); |
| 110 | |
| 111 | return (NULL != fib_entry_src_find(fib_entry, source, NULL)); |
| 112 | } |
| 113 | |
| 114 | static fib_entry_src_t * |
| 115 | fib_entry_src_find_or_create (fib_entry_t *fib_entry, |
| 116 | fib_source_t source, |
| 117 | u32 *index) |
| 118 | { |
| 119 | fib_entry_src_t *esrc; |
| 120 | |
| 121 | esrc = fib_entry_src_find(fib_entry, source, NULL); |
| 122 | |
| 123 | if (NULL == esrc) |
| 124 | { |
| 125 | fib_entry_src_action_init(fib_entry, source); |
| 126 | } |
| 127 | |
| 128 | return (fib_entry_src_find(fib_entry, source, NULL)); |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | fib_entry_src_action_deinit (fib_entry_t *fib_entry, |
| 133 | fib_source_t source) |
| 134 | |
| 135 | { |
| 136 | fib_entry_src_t *esrc; |
| 137 | u32 index = ~0; |
| 138 | |
| 139 | esrc = fib_entry_src_find(fib_entry, source, &index); |
| 140 | |
| 141 | ASSERT(NULL != esrc); |
| 142 | |
| 143 | if (NULL != fib_entry_src_vft[source].fesv_deinit) |
| 144 | { |
| 145 | fib_entry_src_vft[source].fesv_deinit(esrc); |
| 146 | } |
| 147 | |
| 148 | vec_free(esrc->fes_path_exts); |
| 149 | vec_del1(fib_entry->fe_srcs, index); |
| 150 | } |
| 151 | |
| 152 | fib_entry_src_cover_res_t |
| 153 | fib_entry_src_action_cover_change (fib_entry_t *fib_entry, |
| 154 | fib_source_t source) |
| 155 | { |
| 156 | if (NULL != fib_entry_src_vft[source].fesv_cover_change) |
| 157 | { |
| 158 | return (fib_entry_src_vft[source].fesv_cover_change( |
| 159 | fib_entry_src_find(fib_entry, source, NULL), |
| 160 | fib_entry)); |
| 161 | } |
| 162 | |
| 163 | fib_entry_src_cover_res_t res = { |
| 164 | .install = !0, |
| 165 | .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE, |
| 166 | }; |
| 167 | return (res); |
| 168 | } |
| 169 | |
| 170 | fib_entry_src_cover_res_t |
| 171 | fib_entry_src_action_cover_update (fib_entry_t *fib_entry, |
| 172 | fib_source_t source) |
| 173 | { |
| 174 | if (NULL != fib_entry_src_vft[source].fesv_cover_update) |
| 175 | { |
| 176 | return (fib_entry_src_vft[source].fesv_cover_update( |
| 177 | fib_entry_src_find(fib_entry, source, NULL), |
| 178 | fib_entry)); |
| 179 | } |
| 180 | |
| 181 | fib_entry_src_cover_res_t res = { |
| 182 | .install = !0, |
| 183 | .bw_reason = FIB_NODE_BW_REASON_FLAG_NONE, |
| 184 | }; |
| 185 | return (res); |
| 186 | } |
| 187 | |
| 188 | typedef struct fib_entry_src_collect_forwarding_ctx_t_ |
| 189 | { |
| 190 | load_balance_path_t * next_hops; |
| 191 | const fib_entry_t *fib_entry; |
| 192 | const fib_entry_src_t *esrc; |
| 193 | fib_forward_chain_type_t fct; |
| 194 | int is_recursive; |
| 195 | } fib_entry_src_collect_forwarding_ctx_t; |
| 196 | |
| 197 | /** |
| 198 | * @brief Determine whether this FIB entry should use a load-balance MAP |
| 199 | * to support PIC edge fast convergence |
| 200 | */ |
| 201 | load_balance_flags_t |
| 202 | fib_entry_calc_lb_flags (fib_entry_src_collect_forwarding_ctx_t *ctx) |
| 203 | { |
| 204 | /** |
| 205 | * We'll use a LB map is the path-list has recursive paths. |
| 206 | * recursive paths implies BGP, and hence scale. |
| 207 | */ |
| 208 | if (ctx->is_recursive) |
| 209 | { |
| 210 | return (LOAD_BALANCE_FLAG_USES_MAP); |
| 211 | } |
| 212 | return (LOAD_BALANCE_FLAG_NONE); |
| 213 | } |
| 214 | |
| 215 | static int |
| 216 | fib_entry_src_valid_out_label (mpls_label_t label) |
| 217 | { |
| 218 | return ((MPLS_LABEL_IS_REAL(label) || |
| 219 | MPLS_IETF_IPV4_EXPLICIT_NULL_LABEL == label || |
| 220 | MPLS_IETF_IPV6_EXPLICIT_NULL_LABEL == label || |
| 221 | MPLS_IETF_IMPLICIT_NULL_LABEL == label)); |
| 222 | } |
| 223 | |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 224 | /** |
| 225 | * @brief Turn the chain type requested by the client into the one they |
| 226 | * really wanted |
| 227 | */ |
| 228 | fib_forward_chain_type_t |
| 229 | fib_entry_chain_type_fixup (const fib_entry_t *entry, |
| 230 | fib_forward_chain_type_t fct) |
| 231 | { |
| 232 | ASSERT(FIB_FORW_CHAIN_TYPE_MPLS_EOS == fct); |
| 233 | |
| 234 | /* |
| 235 | * The EOS chain is a tricky since one cannot know the adjacency |
| 236 | * to link to without knowing what the packets payload protocol |
| 237 | * will be once the label is popped. |
| 238 | */ |
| 239 | fib_forward_chain_type_t dfct; |
| 240 | |
| 241 | dfct = fib_entry_get_default_chain_type(entry); |
| 242 | |
| 243 | if (FIB_FORW_CHAIN_TYPE_MPLS_EOS == dfct) |
| 244 | { |
| 245 | /* |
| 246 | * If the entry being asked is a eos-MPLS label entry, |
| 247 | * then use the payload-protocol field, that we stashed there |
| 248 | * for just this purpose |
| 249 | */ |
| 250 | return (fib_forw_chain_type_from_dpo_proto( |
| 251 | entry->fe_prefix.fp_payload_proto)); |
| 252 | } |
| 253 | /* |
| 254 | * else give them what this entry would be by default. i.e. if it's a v6 |
| 255 | * entry, then the label its local labelled should be carrying v6 traffic. |
| 256 | * If it's a non-EOS label entry, then there are more labels and we want |
| 257 | * a non-eos chain. |
| 258 | */ |
| 259 | return (dfct); |
| 260 | } |
| 261 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 262 | static int |
| 263 | fib_entry_src_collect_forwarding (fib_node_index_t pl_index, |
| 264 | fib_node_index_t path_index, |
| 265 | void *arg) |
| 266 | { |
| 267 | fib_entry_src_collect_forwarding_ctx_t *ctx; |
| 268 | fib_path_ext_t *path_ext; |
| 269 | |
| 270 | ctx = arg; |
| 271 | |
| 272 | /* |
| 273 | * if the path is not resolved, don't include it. |
| 274 | */ |
| 275 | if (!fib_path_is_resolved(path_index)) |
| 276 | { |
| 277 | return (!0); |
| 278 | } |
| 279 | |
| 280 | if (fib_path_is_recursive(path_index)) |
| 281 | { |
| 282 | ctx->is_recursive = 1; |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * get the matching path-extension for the path being visited. |
| 287 | */ |
| 288 | vec_foreach(path_ext, ctx->esrc->fes_path_exts) |
| 289 | { |
| 290 | if (path_ext->fpe_path_index == path_index) |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | if (NULL != path_ext && |
| 295 | path_ext->fpe_path_index == path_index && |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 296 | fib_entry_src_valid_out_label(path_ext->fpe_label_stack[0])) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 297 | { |
| 298 | /* |
| 299 | * found a matching extension. stack it to obtain the forwarding |
| 300 | * info for this path. |
| 301 | */ |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 302 | ctx->next_hops = fib_path_ext_stack(path_ext, ctx->fib_entry, ctx->fct, ctx->next_hops); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 303 | } |
| 304 | else |
| 305 | { |
| 306 | load_balance_path_t *nh; |
| 307 | |
| 308 | /* |
| 309 | * no extension => no out-going label for this path. that's OK |
| 310 | * in the case of an IP or EOS chain, but not for non-EOS |
| 311 | */ |
| 312 | switch (ctx->fct) |
| 313 | { |
| 314 | case FIB_FORW_CHAIN_TYPE_UNICAST_IP4: |
| 315 | case FIB_FORW_CHAIN_TYPE_UNICAST_IP6: |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 316 | case FIB_FORW_CHAIN_TYPE_MCAST_IP4: |
| 317 | case FIB_FORW_CHAIN_TYPE_MCAST_IP6: |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 318 | /* |
| 319 | * EOS traffic with no label to stack, we need the IP Adj |
| 320 | */ |
| 321 | vec_add2(ctx->next_hops, nh, 1); |
| 322 | |
| 323 | nh->path_index = path_index; |
| 324 | nh->path_weight = fib_path_get_weight(path_index); |
| 325 | fib_path_contribute_forwarding(path_index, ctx->fct, &nh->path_dpo); |
| 326 | |
| 327 | break; |
| 328 | case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS: |
| 329 | if (fib_path_is_exclusive(path_index) || |
| 330 | fib_path_is_deag(path_index)) |
| 331 | { |
| 332 | vec_add2(ctx->next_hops, nh, 1); |
| 333 | |
| 334 | nh->path_index = path_index; |
| 335 | nh->path_weight = fib_path_get_weight(path_index); |
| 336 | fib_path_contribute_forwarding(path_index, |
| 337 | FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS, |
| 338 | &nh->path_dpo); |
| 339 | } |
| 340 | break; |
| 341 | case FIB_FORW_CHAIN_TYPE_MPLS_EOS: |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 342 | { |
| 343 | /* |
| 344 | * no label. we need a chain based on the payload. fixup. |
| 345 | */ |
| 346 | vec_add2(ctx->next_hops, nh, 1); |
| 347 | |
| 348 | nh->path_index = path_index; |
| 349 | nh->path_weight = fib_path_get_weight(path_index); |
| 350 | fib_path_contribute_forwarding(path_index, |
| 351 | fib_entry_chain_type_fixup(ctx->fib_entry, |
| 352 | ctx->fct), |
| 353 | &nh->path_dpo); |
| 354 | |
| 355 | break; |
| 356 | } |
Neale Ranns | 5e575b1 | 2016-10-03 09:40:25 +0100 | [diff] [blame] | 357 | case FIB_FORW_CHAIN_TYPE_ETHERNET: |
Florin Coras | ce1b4c7 | 2017-01-26 14:25:34 -0800 | [diff] [blame^] | 358 | case FIB_FORW_CHAIN_TYPE_NSH: |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 359 | ASSERT(0); |
| 360 | break; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | return (!0); |
| 365 | } |
| 366 | |
| 367 | void |
| 368 | fib_entry_src_mk_lb (fib_entry_t *fib_entry, |
| 369 | const fib_entry_src_t *esrc, |
| 370 | fib_forward_chain_type_t fct, |
| 371 | dpo_id_t *dpo_lb) |
| 372 | { |
| 373 | dpo_proto_t lb_proto; |
| 374 | |
| 375 | /* |
| 376 | * If the entry has path extensions then we construct a load-balance |
| 377 | * by stacking the extensions on the forwarding chains of the paths. |
| 378 | * Otherwise we use the load-balance of the path-list |
| 379 | */ |
| 380 | fib_entry_src_collect_forwarding_ctx_t ctx = { |
| 381 | .esrc = esrc, |
| 382 | .fib_entry = fib_entry, |
| 383 | .next_hops = NULL, |
| 384 | .is_recursive = 0, |
| 385 | .fct = fct, |
| 386 | }; |
| 387 | |
Neale Ranns | c0790cf | 2017-01-05 01:01:47 -0800 | [diff] [blame] | 388 | /* |
| 389 | * As an optimisation we allocate the vector of next-hops to be sized |
| 390 | * equal to the maximum nuber of paths we will need, which is also the |
| 391 | * most likely number we will need, since in most cases the paths are 'up'. |
| 392 | */ |
| 393 | vec_validate(ctx.next_hops, fib_path_list_get_n_paths(esrc->fes_pl)); |
| 394 | vec_reset_length(ctx.next_hops); |
| 395 | |
Neale Ranns | 8fe8cc2 | 2016-11-01 10:05:08 +0000 | [diff] [blame] | 396 | lb_proto = fib_proto_to_dpo(fib_entry->fe_prefix.fp_proto); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 397 | |
| 398 | fib_path_list_walk(esrc->fes_pl, |
| 399 | fib_entry_src_collect_forwarding, |
| 400 | &ctx); |
| 401 | |
| 402 | if (esrc->fes_entry_flags & FIB_ENTRY_FLAG_EXCLUSIVE) |
| 403 | { |
| 404 | /* |
| 405 | * the client provided the DPO that the entry should link to. |
| 406 | * all entries must link to a LB, so if it is an LB already |
| 407 | * then we can use it. |
| 408 | */ |
| 409 | if ((1 == vec_len(ctx.next_hops)) && |
| 410 | (DPO_LOAD_BALANCE == ctx.next_hops[0].path_dpo.dpoi_type)) |
| 411 | { |
| 412 | dpo_copy(dpo_lb, &ctx.next_hops[0].path_dpo); |
| 413 | dpo_reset(&ctx.next_hops[0].path_dpo); |
| 414 | return; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | if (!dpo_id_is_valid(dpo_lb)) |
| 419 | { |
| 420 | /* |
| 421 | * first time create |
| 422 | */ |
| 423 | flow_hash_config_t fhc; |
| 424 | |
| 425 | fhc = fib_table_get_flow_hash_config(fib_entry->fe_fib_index, |
| 426 | dpo_proto_to_fib(lb_proto)); |
| 427 | dpo_set(dpo_lb, |
| 428 | DPO_LOAD_BALANCE, |
| 429 | lb_proto, |
| 430 | load_balance_create(0, lb_proto, fhc)); |
| 431 | } |
| 432 | |
| 433 | load_balance_multipath_update(dpo_lb, |
| 434 | ctx.next_hops, |
| 435 | fib_entry_calc_lb_flags(&ctx)); |
Neale Ranns | 33a7dd5 | 2016-10-07 15:14:33 +0100 | [diff] [blame] | 436 | vec_free(ctx.next_hops); |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 437 | |
| 438 | /* |
| 439 | * if this entry is sourced by the uRPF-exempt source then we |
| 440 | * append the always present local0 interface (index 0) to the |
| 441 | * uRPF list so it is not empty. that way packets pass the loose check. |
| 442 | */ |
| 443 | index_t ui = fib_path_list_get_urpf(esrc->fes_pl); |
| 444 | |
| 445 | if (fib_entry_is_sourced(fib_entry_get_index(fib_entry), |
| 446 | FIB_SOURCE_URPF_EXEMPT) && |
| 447 | (0 == fib_urpf_check_size(ui))) |
| 448 | { |
| 449 | /* |
| 450 | * The uRPF list we get from the path-list is shared by all |
| 451 | * other users of the list, but the uRPF exemption applies |
| 452 | * only to this prefix. So we need our own list. |
| 453 | */ |
| 454 | ui = fib_urpf_list_alloc_and_lock(); |
| 455 | fib_urpf_list_append(ui, 0); |
| 456 | fib_urpf_list_bake(ui); |
| 457 | load_balance_set_urpf(dpo_lb->dpoi_index, ui); |
| 458 | fib_urpf_list_unlock(ui); |
| 459 | } |
| 460 | else |
| 461 | { |
| 462 | load_balance_set_urpf(dpo_lb->dpoi_index, ui); |
| 463 | } |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 464 | load_balance_set_fib_entry_flags(dpo_lb->dpoi_index, |
| 465 | fib_entry_get_flags_i(fib_entry)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | void |
| 469 | fib_entry_src_action_install (fib_entry_t *fib_entry, |
| 470 | fib_source_t source) |
| 471 | { |
| 472 | /* |
| 473 | * Install the forwarding chain for the given source into the forwarding |
| 474 | * tables |
| 475 | */ |
| 476 | fib_forward_chain_type_t fct; |
| 477 | fib_entry_src_t *esrc; |
Neale Ranns | 33a7dd5 | 2016-10-07 15:14:33 +0100 | [diff] [blame] | 478 | int insert; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 479 | |
| 480 | fct = fib_entry_get_default_chain_type(fib_entry); |
| 481 | esrc = fib_entry_src_find(fib_entry, source, NULL); |
| 482 | |
Neale Ranns | 33a7dd5 | 2016-10-07 15:14:33 +0100 | [diff] [blame] | 483 | /* |
| 484 | * Every entry has its own load-balance object. All changes to the entry's |
| 485 | * forwarding result in an inplace modify of the load-balance. This means |
| 486 | * the load-balance object only needs to be added to the forwarding |
| 487 | * DB once, when it is created. |
| 488 | */ |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 489 | insert = !dpo_id_is_valid(&fib_entry->fe_lb); |
Neale Ranns | 33a7dd5 | 2016-10-07 15:14:33 +0100 | [diff] [blame] | 490 | |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 491 | fib_entry_src_mk_lb(fib_entry, esrc, fct, &fib_entry->fe_lb); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 492 | |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 493 | ASSERT(dpo_id_is_valid(&fib_entry->fe_lb)); |
| 494 | FIB_ENTRY_DBG(fib_entry, "install: %d", fib_entry->fe_lb); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 495 | |
| 496 | /* |
| 497 | * insert the adj into the data-plane forwarding trie |
| 498 | */ |
Neale Ranns | 33a7dd5 | 2016-10-07 15:14:33 +0100 | [diff] [blame] | 499 | if (insert) |
| 500 | { |
| 501 | fib_table_fwding_dpo_update(fib_entry->fe_fib_index, |
| 502 | &fib_entry->fe_prefix, |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 503 | &fib_entry->fe_lb); |
Neale Ranns | 33a7dd5 | 2016-10-07 15:14:33 +0100 | [diff] [blame] | 504 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 505 | |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 506 | /* |
| 507 | * if any of the other chain types are already created they will need |
| 508 | * updating too |
| 509 | */ |
| 510 | fib_entry_delegate_type_t fdt; |
| 511 | fib_entry_delegate_t *fed; |
| 512 | |
| 513 | FOR_EACH_DELEGATE_CHAIN(fib_entry, fdt, fed, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 514 | { |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 515 | fib_entry_src_mk_lb(fib_entry, esrc, |
| 516 | fib_entry_delegate_type_to_chain_type(fdt), |
| 517 | &fed->fd_dpo); |
| 518 | }); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | void |
| 522 | fib_entry_src_action_uninstall (fib_entry_t *fib_entry) |
| 523 | { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 524 | /* |
Neale Ranns | 3ee4404 | 2016-10-03 13:05:48 +0100 | [diff] [blame] | 525 | * uninstall the forwarding chain from the forwarding tables |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 526 | */ |
| 527 | FIB_ENTRY_DBG(fib_entry, "uninstall: %d", |
| 528 | fib_entry->fe_adj_index); |
| 529 | |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 530 | if (dpo_id_is_valid(&fib_entry->fe_lb)) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 531 | { |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 532 | fib_table_fwding_dpo_remove( |
| 533 | fib_entry->fe_fib_index, |
| 534 | &fib_entry->fe_prefix, |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 535 | &fib_entry->fe_lb); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 536 | |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 537 | dpo_reset(&fib_entry->fe_lb); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
| 541 | static void |
| 542 | fib_entry_recursive_loop_detect_i (fib_node_index_t path_list_index) |
| 543 | { |
| 544 | fib_node_index_t *entries = NULL; |
| 545 | |
| 546 | fib_path_list_recursive_loop_detect(path_list_index, &entries); |
| 547 | |
| 548 | vec_free(entries); |
| 549 | } |
| 550 | |
| 551 | void |
| 552 | fib_entry_src_action_activate (fib_entry_t *fib_entry, |
| 553 | fib_source_t source) |
| 554 | |
| 555 | { |
| 556 | int houston_we_are_go_for_install; |
| 557 | fib_entry_src_t *esrc; |
| 558 | |
| 559 | esrc = fib_entry_src_find(fib_entry, source, NULL); |
| 560 | |
| 561 | ASSERT(!(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE)); |
| 562 | ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED); |
| 563 | |
| 564 | esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ACTIVE; |
| 565 | |
| 566 | if (NULL != fib_entry_src_vft[source].fesv_activate) |
| 567 | { |
| 568 | houston_we_are_go_for_install = |
| 569 | fib_entry_src_vft[source].fesv_activate(esrc, fib_entry); |
| 570 | } |
| 571 | else |
| 572 | { |
| 573 | /* |
| 574 | * the source is not providing an activate function, we'll assume |
| 575 | * therefore it has no objection to installing the entry |
| 576 | */ |
| 577 | houston_we_are_go_for_install = !0; |
| 578 | } |
| 579 | |
| 580 | /* |
| 581 | * link to the path-list provided by the source, and go check |
| 582 | * if that forms any loops in the graph. |
| 583 | */ |
| 584 | fib_entry->fe_parent = esrc->fes_pl; |
| 585 | fib_entry->fe_sibling = |
| 586 | fib_path_list_child_add(fib_entry->fe_parent, |
| 587 | FIB_NODE_TYPE_ENTRY, |
| 588 | fib_entry_get_index(fib_entry)); |
| 589 | |
| 590 | fib_entry_recursive_loop_detect_i(fib_entry->fe_parent); |
| 591 | |
| 592 | FIB_ENTRY_DBG(fib_entry, "activate: %d", |
| 593 | fib_entry->fe_parent); |
| 594 | |
| 595 | if (0 != houston_we_are_go_for_install) |
| 596 | { |
| 597 | fib_entry_src_action_install(fib_entry, source); |
| 598 | } |
| 599 | else |
| 600 | { |
| 601 | fib_entry_src_action_uninstall(fib_entry); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | void |
| 606 | fib_entry_src_action_deactivate (fib_entry_t *fib_entry, |
| 607 | fib_source_t source) |
| 608 | |
| 609 | { |
| 610 | fib_node_index_t path_list_index; |
| 611 | fib_entry_src_t *esrc; |
| 612 | |
| 613 | esrc = fib_entry_src_find(fib_entry, source, NULL); |
| 614 | |
| 615 | ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE); |
| 616 | |
| 617 | if (NULL != fib_entry_src_vft[source].fesv_deactivate) |
| 618 | { |
| 619 | fib_entry_src_vft[source].fesv_deactivate(esrc, fib_entry); |
| 620 | } |
| 621 | |
| 622 | esrc->fes_flags &= ~FIB_ENTRY_SRC_FLAG_ACTIVE; |
| 623 | |
| 624 | FIB_ENTRY_DBG(fib_entry, "deactivate: %d", fib_entry->fe_parent); |
| 625 | |
| 626 | /* |
| 627 | * un-link from an old path-list. Check for any loops this will clear |
| 628 | */ |
| 629 | path_list_index = fib_entry->fe_parent; |
| 630 | fib_entry->fe_parent = FIB_NODE_INDEX_INVALID; |
| 631 | |
| 632 | fib_entry_recursive_loop_detect_i(path_list_index); |
| 633 | |
| 634 | /* |
| 635 | * this will unlock the path-list, so it may be invalid thereafter. |
| 636 | */ |
| 637 | fib_path_list_child_remove(path_list_index, fib_entry->fe_sibling); |
| 638 | fib_entry->fe_sibling = FIB_NODE_INDEX_INVALID; |
| 639 | } |
| 640 | |
| 641 | static void |
| 642 | fib_entry_src_action_fwd_update (const fib_entry_t *fib_entry, |
| 643 | fib_source_t source) |
| 644 | { |
| 645 | fib_entry_src_t *esrc; |
| 646 | |
| 647 | vec_foreach(esrc, fib_entry->fe_srcs) |
| 648 | { |
| 649 | if (NULL != fib_entry_src_vft[esrc->fes_src].fesv_fwd_update) |
| 650 | { |
| 651 | fib_entry_src_vft[esrc->fes_src].fesv_fwd_update(esrc, |
| 652 | fib_entry, |
| 653 | source); |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | void |
| 659 | fib_entry_src_action_reactivate (fib_entry_t *fib_entry, |
| 660 | fib_source_t source) |
| 661 | { |
| 662 | fib_node_index_t path_list_index; |
| 663 | fib_entry_src_t *esrc; |
| 664 | |
| 665 | esrc = fib_entry_src_find(fib_entry, source, NULL); |
| 666 | |
| 667 | ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE); |
| 668 | |
| 669 | FIB_ENTRY_DBG(fib_entry, "reactivate: %d to %d", |
| 670 | fib_entry->fe_parent, |
| 671 | esrc->fes_pl); |
| 672 | |
| 673 | if (fib_entry->fe_parent != esrc->fes_pl) |
| 674 | { |
| 675 | /* |
| 676 | * un-link from an old path-list. Check for any loops this will clear |
| 677 | */ |
| 678 | path_list_index = fib_entry->fe_parent; |
| 679 | fib_entry->fe_parent = FIB_NODE_INDEX_INVALID; |
| 680 | |
| 681 | /* |
| 682 | * temporary lock so it doesn't get deleted when this entry is no |
| 683 | * longer a child. |
| 684 | */ |
| 685 | fib_path_list_lock(path_list_index); |
| 686 | |
| 687 | /* |
| 688 | * this entry is no longer a child. after unlinking check if any loops |
| 689 | * were broken |
| 690 | */ |
| 691 | fib_path_list_child_remove(path_list_index, |
| 692 | fib_entry->fe_sibling); |
| 693 | |
| 694 | fib_entry_recursive_loop_detect_i(path_list_index); |
| 695 | |
| 696 | /* |
| 697 | * link to the path-list provided by the source, and go check |
| 698 | * if that forms any loops in the graph. |
| 699 | */ |
| 700 | fib_entry->fe_parent = esrc->fes_pl; |
| 701 | fib_entry->fe_sibling = |
| 702 | fib_path_list_child_add(fib_entry->fe_parent, |
| 703 | FIB_NODE_TYPE_ENTRY, |
| 704 | fib_entry_get_index(fib_entry)); |
| 705 | |
| 706 | fib_entry_recursive_loop_detect_i(fib_entry->fe_parent); |
| 707 | fib_path_list_unlock(path_list_index); |
| 708 | } |
| 709 | fib_entry_src_action_install(fib_entry, source); |
| 710 | fib_entry_src_action_fwd_update(fib_entry, source); |
| 711 | } |
| 712 | |
| 713 | void |
| 714 | fib_entry_src_action_installed (const fib_entry_t *fib_entry, |
| 715 | fib_source_t source) |
| 716 | { |
| 717 | fib_entry_src_t *esrc; |
| 718 | |
| 719 | esrc = fib_entry_src_find(fib_entry, source, NULL); |
| 720 | |
| 721 | if (NULL != fib_entry_src_vft[source].fesv_installed) |
| 722 | { |
| 723 | fib_entry_src_vft[source].fesv_installed(esrc, |
| 724 | fib_entry); |
| 725 | } |
| 726 | |
| 727 | fib_entry_src_action_fwd_update(fib_entry, source); |
| 728 | } |
| 729 | |
| 730 | /* |
| 731 | * fib_entry_src_action_add |
| 732 | * |
| 733 | * Adding a source can result in a new fib_entry being created, which |
| 734 | * can inturn mean the pool is realloc'd and thus the entry passed as |
| 735 | * an argument it also realloc'd |
| 736 | * @return the original entry |
| 737 | */ |
| 738 | fib_entry_t * |
| 739 | fib_entry_src_action_add (fib_entry_t *fib_entry, |
| 740 | fib_source_t source, |
| 741 | fib_entry_flag_t flags, |
| 742 | const dpo_id_t *dpo) |
| 743 | { |
| 744 | fib_node_index_t fib_entry_index; |
| 745 | fib_entry_src_t *esrc; |
| 746 | |
| 747 | esrc = fib_entry_src_find_or_create(fib_entry, source, NULL); |
| 748 | |
| 749 | esrc->fes_ref_count++; |
| 750 | |
| 751 | if (1 != esrc->fes_ref_count) |
| 752 | { |
| 753 | /* |
| 754 | * we only want to add the source on the 0->1 transition |
| 755 | */ |
| 756 | return (fib_entry); |
| 757 | } |
| 758 | |
| 759 | esrc->fes_entry_flags = flags; |
| 760 | |
| 761 | /* |
| 762 | * save variable so we can recover from a fib_entry realloc. |
| 763 | */ |
| 764 | fib_entry_index = fib_entry_get_index(fib_entry); |
| 765 | |
| 766 | if (NULL != fib_entry_src_vft[source].fesv_add) |
| 767 | { |
| 768 | fib_entry_src_vft[source].fesv_add(esrc, |
| 769 | fib_entry, |
| 770 | flags, |
| 771 | fib_entry_get_proto(fib_entry), |
| 772 | dpo); |
| 773 | } |
| 774 | |
| 775 | fib_entry = fib_entry_get(fib_entry_index); |
| 776 | |
| 777 | esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED; |
| 778 | |
| 779 | fib_path_list_lock(esrc->fes_pl); |
| 780 | |
| 781 | /* |
| 782 | * the source owns a lock on the entry |
| 783 | */ |
| 784 | fib_entry_lock(fib_entry_get_index(fib_entry)); |
| 785 | |
| 786 | return (fib_entry); |
| 787 | } |
| 788 | |
Neale Ranns | 948e00f | 2016-10-20 13:39:34 +0100 | [diff] [blame] | 789 | /* |
| 790 | * fib_entry_src_action_update |
| 791 | * |
| 792 | * Adding a source can result in a new fib_entry being created, which |
| 793 | * can inturn mean the pool is realloc'd and thus the entry passed as |
| 794 | * an argument it also realloc'd |
| 795 | * @return the original entry |
| 796 | */ |
| 797 | fib_entry_t * |
| 798 | fib_entry_src_action_update (fib_entry_t *fib_entry, |
| 799 | fib_source_t source, |
| 800 | fib_entry_flag_t flags, |
| 801 | const dpo_id_t *dpo) |
| 802 | { |
| 803 | fib_node_index_t fib_entry_index, old_path_list_index; |
| 804 | fib_entry_src_t *esrc; |
| 805 | |
| 806 | esrc = fib_entry_src_find_or_create(fib_entry, source, NULL); |
| 807 | |
| 808 | if (NULL == esrc) |
| 809 | return (fib_entry_src_action_add(fib_entry, source, flags, dpo)); |
| 810 | |
| 811 | old_path_list_index = esrc->fes_pl; |
| 812 | esrc->fes_entry_flags = flags; |
| 813 | |
| 814 | /* |
| 815 | * save variable so we can recover from a fib_entry realloc. |
| 816 | */ |
| 817 | fib_entry_index = fib_entry_get_index(fib_entry); |
| 818 | |
| 819 | if (NULL != fib_entry_src_vft[source].fesv_add) |
| 820 | { |
| 821 | fib_entry_src_vft[source].fesv_add(esrc, |
| 822 | fib_entry, |
| 823 | flags, |
| 824 | fib_entry_get_proto(fib_entry), |
| 825 | dpo); |
| 826 | } |
| 827 | |
| 828 | fib_entry = fib_entry_get(fib_entry_index); |
| 829 | |
| 830 | esrc->fes_flags |= FIB_ENTRY_SRC_FLAG_ADDED; |
| 831 | |
| 832 | fib_path_list_lock(esrc->fes_pl); |
| 833 | fib_path_list_unlock(old_path_list_index); |
| 834 | |
| 835 | return (fib_entry); |
| 836 | } |
| 837 | |
| 838 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 839 | fib_entry_src_flag_t |
| 840 | fib_entry_src_action_remove (fib_entry_t *fib_entry, |
| 841 | fib_source_t source) |
| 842 | |
| 843 | { |
| 844 | fib_node_index_t old_path_list; |
| 845 | fib_entry_src_flag_t sflags; |
| 846 | fib_entry_src_t *esrc; |
| 847 | |
| 848 | esrc = fib_entry_src_find(fib_entry, source, NULL); |
| 849 | |
| 850 | if (NULL == esrc) |
| 851 | return (FIB_ENTRY_SRC_FLAG_ACTIVE); |
| 852 | |
| 853 | esrc->fes_ref_count--; |
| 854 | sflags = esrc->fes_flags; |
| 855 | |
| 856 | if (0 != esrc->fes_ref_count) |
| 857 | { |
| 858 | /* |
| 859 | * only remove the source on the 1->0 transisition |
| 860 | */ |
| 861 | return (sflags); |
| 862 | } |
| 863 | |
| 864 | if (esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ACTIVE) |
| 865 | { |
| 866 | fib_entry_src_action_deactivate(fib_entry, source); |
| 867 | } |
| 868 | |
| 869 | old_path_list = esrc->fes_pl; |
| 870 | |
| 871 | if (NULL != fib_entry_src_vft[source].fesv_remove) |
| 872 | { |
| 873 | fib_entry_src_vft[source].fesv_remove(esrc); |
| 874 | } |
| 875 | |
| 876 | fib_path_list_unlock(old_path_list); |
| 877 | fib_entry_unlock(fib_entry_get_index(fib_entry)); |
| 878 | |
| 879 | sflags &= ~FIB_ENTRY_SRC_FLAG_ADDED; |
| 880 | fib_entry_src_action_deinit(fib_entry, source); |
| 881 | |
| 882 | return (sflags); |
| 883 | } |
| 884 | |
| 885 | static inline int |
| 886 | fib_route_recurses_via_self (const fib_prefix_t *prefix, |
| 887 | const fib_route_path_t *rpath) |
| 888 | { |
| 889 | /* |
| 890 | * not all zeros next hop && |
| 891 | * is recursive path && |
| 892 | * nexthop is same as the route's address |
| 893 | */ |
| 894 | return ((!ip46_address_is_zero(&rpath->frp_addr)) && |
| 895 | (~0 == rpath->frp_sw_if_index) && |
| 896 | (0 == ip46_address_cmp(&rpath->frp_addr, &prefix->fp_addr))); |
| 897 | |
| 898 | } |
| 899 | |
| 900 | /* |
| 901 | * fib_route_attached_cross_table |
| 902 | * |
| 903 | * Return true the the route is attached via an interface that |
| 904 | * is not in the same table as the route |
| 905 | */ |
| 906 | static inline int |
| 907 | fib_route_attached_cross_table (const fib_entry_t *fib_entry, |
| 908 | const fib_route_path_t *rpath) |
| 909 | { |
| 910 | /* |
| 911 | * - All zeros next-hop |
| 912 | * - a valid interface |
| 913 | * - entry's fib index not equeal to interface's index |
| 914 | */ |
| 915 | if (ip46_address_is_zero(&rpath->frp_addr) && |
| 916 | (~0 != rpath->frp_sw_if_index) && |
| 917 | (fib_entry->fe_fib_index != |
| 918 | fib_table_get_index_for_sw_if_index(fib_entry_get_proto(fib_entry), |
| 919 | rpath->frp_sw_if_index))) |
| 920 | { |
| 921 | return (!0); |
| 922 | } |
| 923 | return (0); |
| 924 | } |
| 925 | |
| 926 | /* |
| 927 | * fib_route_attached_cross_table |
| 928 | * |
| 929 | * Return true the the route is attached via an interface that |
| 930 | * is not in the same table as the route |
| 931 | */ |
| 932 | static inline int |
| 933 | fib_path_is_attached (const fib_route_path_t *rpath) |
| 934 | { |
| 935 | /* |
| 936 | * - All zeros next-hop |
| 937 | * - a valid interface |
| 938 | */ |
| 939 | if (ip46_address_is_zero(&rpath->frp_addr) && |
| 940 | (~0 != rpath->frp_sw_if_index)) |
| 941 | { |
| 942 | return (!0); |
| 943 | } |
| 944 | return (0); |
| 945 | } |
| 946 | |
| 947 | fib_path_list_flags_t |
| 948 | fib_entry_src_flags_2_path_list_flags (fib_entry_flag_t eflags) |
| 949 | { |
| 950 | fib_path_list_flags_t plf = FIB_PATH_LIST_FLAG_NONE; |
| 951 | |
| 952 | if (eflags & FIB_ENTRY_FLAG_DROP) |
| 953 | { |
| 954 | plf |= FIB_PATH_LIST_FLAG_DROP; |
| 955 | } |
| 956 | if (eflags & FIB_ENTRY_FLAG_LOCAL) |
| 957 | { |
| 958 | plf |= FIB_PATH_LIST_FLAG_LOCAL; |
| 959 | } |
| 960 | if (eflags & FIB_ENTRY_FLAG_EXCLUSIVE) |
| 961 | { |
| 962 | plf |= FIB_PATH_LIST_FLAG_EXCLUSIVE; |
| 963 | } |
| 964 | |
| 965 | return (plf); |
| 966 | } |
| 967 | |
| 968 | static void |
| 969 | fib_entry_flags_update (const fib_entry_t *fib_entry, |
| 970 | const fib_route_path_t *rpath, |
| 971 | fib_path_list_flags_t *pl_flags, |
| 972 | fib_entry_src_t *esrc) |
| 973 | { |
| 974 | /* |
| 975 | * don't allow the addition of a recursive looped path for prefix |
| 976 | * via itself. |
| 977 | */ |
| 978 | if (fib_route_recurses_via_self(&fib_entry->fe_prefix, rpath)) |
| 979 | { |
| 980 | /* |
| 981 | * force the install of a drop path-list. |
| 982 | * we want the entry to have some path-list, mainly so |
| 983 | * the dodgy path can be rmeoved when the source stops playing |
| 984 | * silly buggers. |
| 985 | */ |
| 986 | *pl_flags |= FIB_PATH_LIST_FLAG_DROP; |
| 987 | } |
| 988 | else |
| 989 | { |
| 990 | *pl_flags &= ~FIB_PATH_LIST_FLAG_DROP; |
| 991 | } |
| 992 | |
| 993 | if ((esrc->fes_src == FIB_SOURCE_API) || |
| 994 | (esrc->fes_src == FIB_SOURCE_CLI)) |
| 995 | { |
| 996 | if (fib_path_is_attached(rpath)) |
| 997 | { |
| 998 | esrc->fes_entry_flags |= FIB_ENTRY_FLAG_ATTACHED; |
| 999 | } |
| 1000 | else |
| 1001 | { |
| 1002 | esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_ATTACHED; |
| 1003 | } |
| 1004 | } |
| 1005 | if (fib_route_attached_cross_table(fib_entry, rpath)) |
| 1006 | { |
| 1007 | esrc->fes_entry_flags |= FIB_ENTRY_FLAG_IMPORT; |
| 1008 | } |
| 1009 | else |
| 1010 | { |
| 1011 | esrc->fes_entry_flags &= ~FIB_ENTRY_FLAG_IMPORT; |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | /* |
| 1016 | * fib_entry_src_path_ext_add |
| 1017 | * |
| 1018 | * append a path extension to the entry's list |
| 1019 | */ |
| 1020 | static void |
| 1021 | fib_entry_src_path_ext_append (fib_entry_src_t *esrc, |
| 1022 | const fib_route_path_t *rpath) |
| 1023 | { |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 1024 | if (NULL != rpath->frp_label_stack) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1025 | { |
| 1026 | fib_path_ext_t *path_ext; |
| 1027 | |
| 1028 | vec_add2(esrc->fes_path_exts, path_ext, 1); |
| 1029 | |
| 1030 | fib_path_ext_init(path_ext, esrc->fes_pl, rpath); |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | /* |
| 1035 | * fib_entry_src_path_ext_insert |
| 1036 | * |
| 1037 | * insert, sorted, a path extension to the entry's list. |
| 1038 | * It's not strictly necessary in sort the path extensions, since each |
| 1039 | * extension has the path index to which it resolves. However, by being |
| 1040 | * sorted the load-balance produced has a deterministic order, not an order |
| 1041 | * based on the sequence of extension additions. this is a considerable benefit. |
| 1042 | */ |
| 1043 | static void |
| 1044 | fib_entry_src_path_ext_insert (fib_entry_src_t *esrc, |
| 1045 | const fib_route_path_t *rpath) |
| 1046 | { |
| 1047 | if (0 == vec_len(esrc->fes_path_exts)) |
| 1048 | return (fib_entry_src_path_ext_append(esrc, rpath)); |
| 1049 | |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 1050 | if (NULL != rpath->frp_label_stack) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1051 | { |
| 1052 | fib_path_ext_t path_ext; |
| 1053 | int i = 0; |
| 1054 | |
| 1055 | fib_path_ext_init(&path_ext, esrc->fes_pl, rpath); |
| 1056 | |
| 1057 | while (i < vec_len(esrc->fes_path_exts) && |
| 1058 | (fib_path_ext_cmp(&esrc->fes_path_exts[i], rpath) < 0)) |
| 1059 | { |
| 1060 | i++; |
| 1061 | } |
| 1062 | |
| 1063 | vec_insert_elts(esrc->fes_path_exts, &path_ext, 1, i); |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | /* |
| 1068 | * fib_entry_src_action_add |
| 1069 | * |
| 1070 | * Adding a source can result in a new fib_entry being created, which |
| 1071 | * can inturn mean the pool is realloc'd and thus the entry passed as |
| 1072 | * an argument it also realloc'd |
| 1073 | * @return the entry |
| 1074 | */ |
| 1075 | fib_entry_t* |
| 1076 | fib_entry_src_action_path_add (fib_entry_t *fib_entry, |
| 1077 | fib_source_t source, |
| 1078 | fib_entry_flag_t flags, |
| 1079 | const fib_route_path_t *rpath) |
| 1080 | { |
| 1081 | fib_node_index_t old_path_list, fib_entry_index; |
| 1082 | fib_path_list_flags_t pl_flags; |
| 1083 | fib_path_ext_t *path_ext; |
| 1084 | fib_entry_src_t *esrc; |
| 1085 | |
| 1086 | /* |
| 1087 | * save variable so we can recover from a fib_entry realloc. |
| 1088 | */ |
| 1089 | fib_entry_index = fib_entry_get_index(fib_entry); |
| 1090 | |
| 1091 | esrc = fib_entry_src_find(fib_entry, source, NULL); |
| 1092 | if (NULL == esrc) |
| 1093 | { |
| 1094 | fib_entry = |
| 1095 | fib_entry_src_action_add(fib_entry, |
| 1096 | source, |
| 1097 | flags, |
| 1098 | drop_dpo_get( |
| 1099 | fib_proto_to_dpo( |
| 1100 | fib_entry_get_proto(fib_entry)))); |
| 1101 | esrc = fib_entry_src_find(fib_entry, source, NULL); |
| 1102 | } |
| 1103 | |
| 1104 | /* |
| 1105 | * we are no doubt modifying a path-list. If the path-list |
| 1106 | * is shared, and hence not modifiable, then the index returned |
| 1107 | * will be for a different path-list. This FIB entry to needs |
| 1108 | * to maintain its lock appropriately. |
| 1109 | */ |
| 1110 | old_path_list = esrc->fes_pl; |
| 1111 | |
| 1112 | ASSERT(NULL != fib_entry_src_vft[source].fesv_path_add); |
| 1113 | |
| 1114 | pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry)); |
| 1115 | fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc); |
| 1116 | |
| 1117 | fib_entry_src_vft[source].fesv_path_add(esrc, fib_entry, pl_flags, rpath); |
| 1118 | fib_entry = fib_entry_get(fib_entry_index); |
| 1119 | |
| 1120 | /* |
| 1121 | * re-resolve all the path-extensions with the new path-list |
| 1122 | */ |
| 1123 | vec_foreach(path_ext, esrc->fes_path_exts) |
| 1124 | { |
| 1125 | fib_path_ext_resolve(path_ext, esrc->fes_pl); |
| 1126 | } |
| 1127 | /* |
| 1128 | * if the path has a label we need to add a path extension |
| 1129 | */ |
| 1130 | fib_entry_src_path_ext_insert(esrc, rpath); |
| 1131 | |
| 1132 | fib_path_list_lock(esrc->fes_pl); |
| 1133 | fib_path_list_unlock(old_path_list); |
| 1134 | |
| 1135 | return (fib_entry); |
| 1136 | } |
| 1137 | |
| 1138 | /* |
| 1139 | * fib_entry_src_action_swap |
| 1140 | * |
| 1141 | * The source is providing new paths to replace the old ones. |
| 1142 | * Adding a source can result in a new fib_entry being created, which |
| 1143 | * can inturn mean the pool is realloc'd and thus the entry passed as |
| 1144 | * an argument it also realloc'd |
| 1145 | * @return the entry |
| 1146 | */ |
| 1147 | fib_entry_t* |
| 1148 | fib_entry_src_action_path_swap (fib_entry_t *fib_entry, |
| 1149 | fib_source_t source, |
| 1150 | fib_entry_flag_t flags, |
| 1151 | const fib_route_path_t *rpaths) |
| 1152 | { |
| 1153 | fib_node_index_t old_path_list, fib_entry_index; |
| 1154 | fib_path_list_flags_t pl_flags; |
| 1155 | const fib_route_path_t *rpath; |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 1156 | fib_path_ext_t *path_ext; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1157 | fib_entry_src_t *esrc; |
| 1158 | |
| 1159 | esrc = fib_entry_src_find(fib_entry, source, NULL); |
| 1160 | |
| 1161 | /* |
| 1162 | * save variable so we can recover from a fib_entry realloc. |
| 1163 | */ |
| 1164 | fib_entry_index = fib_entry_get_index(fib_entry); |
| 1165 | |
| 1166 | if (NULL == esrc) |
| 1167 | { |
| 1168 | fib_entry = fib_entry_src_action_add(fib_entry, |
| 1169 | source, |
| 1170 | flags, |
| 1171 | drop_dpo_get( |
| 1172 | fib_proto_to_dpo( |
| 1173 | fib_entry_get_proto(fib_entry)))); |
| 1174 | esrc = fib_entry_src_find(fib_entry, source, NULL); |
| 1175 | } |
| 1176 | |
| 1177 | /* |
| 1178 | * swapping paths may create a new path-list (or may use an existing shared) |
| 1179 | * but we are certainly getting a different one. This FIB entry to needs |
| 1180 | * to maintain its lock appropriately. |
| 1181 | */ |
| 1182 | old_path_list = esrc->fes_pl; |
| 1183 | |
| 1184 | ASSERT(NULL != fib_entry_src_vft[source].fesv_path_swap); |
| 1185 | |
Neale Ranns | df089a8 | 2016-10-02 16:39:06 +0100 | [diff] [blame] | 1186 | pl_flags = fib_entry_src_flags_2_path_list_flags(flags); |
| 1187 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1188 | vec_foreach(rpath, rpaths) |
| 1189 | { |
| 1190 | fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc); |
| 1191 | } |
| 1192 | |
| 1193 | fib_entry_src_vft[source].fesv_path_swap(esrc, |
| 1194 | fib_entry, |
| 1195 | pl_flags, |
| 1196 | rpaths); |
| 1197 | |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 1198 | vec_foreach(path_ext, esrc->fes_path_exts) |
| 1199 | { |
| 1200 | vec_free(path_ext->fpe_label_stack); |
| 1201 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1202 | vec_free(esrc->fes_path_exts); |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 1203 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1204 | vec_foreach(rpath, rpaths) |
| 1205 | { |
| 1206 | fib_entry_src_path_ext_append(esrc, rpath); |
| 1207 | } |
| 1208 | |
| 1209 | fib_entry = fib_entry_get(fib_entry_index); |
| 1210 | |
| 1211 | fib_path_list_lock(esrc->fes_pl); |
| 1212 | fib_path_list_unlock(old_path_list); |
| 1213 | |
| 1214 | return (fib_entry); |
| 1215 | } |
| 1216 | |
| 1217 | fib_entry_src_flag_t |
| 1218 | fib_entry_src_action_path_remove (fib_entry_t *fib_entry, |
| 1219 | fib_source_t source, |
| 1220 | const fib_route_path_t *rpath) |
| 1221 | { |
| 1222 | fib_path_list_flags_t pl_flags; |
| 1223 | fib_node_index_t old_path_list; |
| 1224 | fib_path_ext_t *path_ext; |
| 1225 | fib_entry_src_t *esrc; |
| 1226 | |
| 1227 | esrc = fib_entry_src_find(fib_entry, source, NULL); |
| 1228 | |
| 1229 | ASSERT(NULL != esrc); |
| 1230 | ASSERT(esrc->fes_flags & FIB_ENTRY_SRC_FLAG_ADDED); |
| 1231 | |
| 1232 | /* |
| 1233 | * we no doubt modifying a path-list. If the path-list |
| 1234 | * is shared, and hence not modifiable, then the index returned |
| 1235 | * will be for a different path-list. This FIB entry to needs |
| 1236 | * to maintain its lock appropriately. |
| 1237 | */ |
| 1238 | old_path_list = esrc->fes_pl; |
| 1239 | |
| 1240 | ASSERT(NULL != fib_entry_src_vft[source].fesv_path_remove); |
| 1241 | |
| 1242 | pl_flags = fib_entry_src_flags_2_path_list_flags(fib_entry_get_flags_i(fib_entry)); |
| 1243 | fib_entry_flags_update(fib_entry, rpath, &pl_flags, esrc); |
| 1244 | |
| 1245 | fib_entry_src_vft[source].fesv_path_remove(esrc, pl_flags, rpath); |
| 1246 | /* |
| 1247 | * find the matching path extension and remove it |
| 1248 | */ |
| 1249 | vec_foreach(path_ext, esrc->fes_path_exts) |
| 1250 | { |
| 1251 | if (!fib_path_ext_cmp(path_ext, rpath)) |
| 1252 | { |
| 1253 | /* |
| 1254 | * delete the element moving the remaining elements down 1 position. |
| 1255 | * this preserves the sorted order. |
| 1256 | */ |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 1257 | vec_free(path_ext->fpe_label_stack); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1258 | vec_delete(esrc->fes_path_exts, 1, (path_ext - esrc->fes_path_exts)); |
| 1259 | break; |
| 1260 | } |
| 1261 | } |
| 1262 | /* |
| 1263 | * re-resolve all the path-extensions with the new path-list |
| 1264 | */ |
| 1265 | vec_foreach(path_ext, esrc->fes_path_exts) |
| 1266 | { |
| 1267 | fib_path_ext_resolve(path_ext, esrc->fes_pl); |
| 1268 | } |
| 1269 | |
| 1270 | /* |
| 1271 | * lock the new path-list, unlock the old if it had one |
| 1272 | */ |
| 1273 | fib_path_list_unlock(old_path_list); |
| 1274 | |
| 1275 | if (FIB_NODE_INDEX_INVALID != esrc->fes_pl) { |
| 1276 | fib_path_list_lock(esrc->fes_pl); |
| 1277 | return (FIB_ENTRY_SRC_FLAG_ADDED); |
| 1278 | } |
| 1279 | else |
| 1280 | { |
| 1281 | /* |
| 1282 | * no more paths left from this source |
| 1283 | */ |
| 1284 | fib_entry_src_action_remove(fib_entry, source); |
| 1285 | return (FIB_ENTRY_SRC_FLAG_NONE); |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | u8* |
| 1290 | fib_entry_src_format (fib_entry_t *fib_entry, |
| 1291 | fib_source_t source, |
| 1292 | u8* s) |
| 1293 | { |
| 1294 | fib_entry_src_t *esrc; |
| 1295 | |
| 1296 | esrc = fib_entry_src_find(fib_entry, source, NULL); |
| 1297 | |
| 1298 | if (NULL != fib_entry_src_vft[source].fesv_format) |
| 1299 | { |
| 1300 | return (fib_entry_src_vft[source].fesv_format(esrc, s)); |
| 1301 | } |
| 1302 | return (s); |
| 1303 | } |
| 1304 | |
| 1305 | adj_index_t |
| 1306 | fib_entry_get_adj_for_source (fib_node_index_t fib_entry_index, |
| 1307 | fib_source_t source) |
| 1308 | { |
| 1309 | fib_entry_t *fib_entry; |
| 1310 | fib_entry_src_t *esrc; |
| 1311 | |
| 1312 | if (FIB_NODE_INDEX_INVALID == fib_entry_index) |
| 1313 | return (ADJ_INDEX_INVALID); |
| 1314 | |
| 1315 | fib_entry = fib_entry_get(fib_entry_index); |
| 1316 | esrc = fib_entry_src_find(fib_entry, source, NULL); |
| 1317 | |
| 1318 | if (NULL != esrc) |
| 1319 | { |
| 1320 | if (FIB_NODE_INDEX_INVALID != esrc->fes_pl) |
| 1321 | { |
| 1322 | return (fib_path_list_get_adj( |
| 1323 | esrc->fes_pl, |
| 1324 | fib_entry_get_default_chain_type(fib_entry))); |
| 1325 | } |
| 1326 | } |
| 1327 | return (ADJ_INDEX_INVALID); |
| 1328 | } |
| 1329 | |
| 1330 | const int |
| 1331 | fib_entry_get_dpo_for_source (fib_node_index_t fib_entry_index, |
| 1332 | fib_source_t source, |
| 1333 | dpo_id_t *dpo) |
| 1334 | { |
| 1335 | fib_entry_t *fib_entry; |
| 1336 | fib_entry_src_t *esrc; |
| 1337 | |
| 1338 | if (FIB_NODE_INDEX_INVALID == fib_entry_index) |
| 1339 | return (0); |
| 1340 | |
| 1341 | fib_entry = fib_entry_get(fib_entry_index); |
| 1342 | esrc = fib_entry_src_find(fib_entry, source, NULL); |
| 1343 | |
| 1344 | if (NULL != esrc) |
| 1345 | { |
| 1346 | if (FIB_NODE_INDEX_INVALID != esrc->fes_pl) |
| 1347 | { |
| 1348 | fib_path_list_contribute_forwarding( |
| 1349 | esrc->fes_pl, |
| 1350 | fib_entry_get_default_chain_type(fib_entry), |
| 1351 | dpo); |
| 1352 | |
| 1353 | return (dpo_id_is_valid(dpo)); |
| 1354 | } |
| 1355 | } |
| 1356 | return (0); |
| 1357 | } |
| 1358 | |
Neale Ranns | df089a8 | 2016-10-02 16:39:06 +0100 | [diff] [blame] | 1359 | u32 |
| 1360 | fib_entry_get_resolving_interface_for_source (fib_node_index_t entry_index, |
| 1361 | fib_source_t source) |
| 1362 | { |
| 1363 | fib_entry_t *fib_entry; |
| 1364 | fib_entry_src_t *esrc; |
| 1365 | |
| 1366 | fib_entry = fib_entry_get(entry_index); |
| 1367 | |
| 1368 | esrc = fib_entry_src_find(fib_entry, source, NULL); |
| 1369 | |
| 1370 | if (NULL != esrc) |
| 1371 | { |
| 1372 | if (FIB_NODE_INDEX_INVALID != esrc->fes_pl) |
| 1373 | { |
| 1374 | return (fib_path_list_get_resolving_interface(esrc->fes_pl)); |
| 1375 | } |
| 1376 | } |
| 1377 | return (~0); |
| 1378 | } |
| 1379 | |
| 1380 | fib_entry_flag_t |
| 1381 | fib_entry_get_flags_for_source (fib_node_index_t entry_index, |
| 1382 | fib_source_t source) |
| 1383 | { |
| 1384 | fib_entry_t *fib_entry; |
| 1385 | fib_entry_src_t *esrc; |
| 1386 | |
| 1387 | fib_entry = fib_entry_get(entry_index); |
| 1388 | |
| 1389 | esrc = fib_entry_src_find(fib_entry, source, NULL); |
| 1390 | |
| 1391 | if (NULL != esrc) |
| 1392 | { |
| 1393 | return (esrc->fes_entry_flags); |
| 1394 | } |
| 1395 | |
| 1396 | return (FIB_ENTRY_FLAG_NONE); |
| 1397 | } |
| 1398 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1399 | fib_entry_flag_t |
| 1400 | fib_entry_get_flags_i (const fib_entry_t *fib_entry) |
| 1401 | { |
| 1402 | fib_entry_flag_t flags; |
| 1403 | |
| 1404 | /* |
| 1405 | * the vector of sources is deliberately arranged in priority order |
| 1406 | */ |
| 1407 | if (0 == vec_len(fib_entry->fe_srcs)) |
| 1408 | { |
| 1409 | flags = FIB_ENTRY_FLAG_NONE; |
| 1410 | } |
| 1411 | else |
| 1412 | { |
| 1413 | fib_entry_src_t *esrc; |
| 1414 | |
| 1415 | esrc = vec_elt_at_index(fib_entry->fe_srcs, 0); |
| 1416 | flags = esrc->fes_entry_flags; |
| 1417 | } |
| 1418 | |
| 1419 | return (flags); |
| 1420 | } |
| 1421 | |
| 1422 | void |
| 1423 | fib_entry_set_source_data (fib_node_index_t fib_entry_index, |
| 1424 | fib_source_t source, |
| 1425 | const void *data) |
| 1426 | { |
| 1427 | fib_entry_t *fib_entry; |
| 1428 | fib_entry_src_t *esrc; |
| 1429 | |
| 1430 | fib_entry = fib_entry_get(fib_entry_index); |
| 1431 | esrc = fib_entry_src_find(fib_entry, source, NULL); |
| 1432 | |
| 1433 | if (NULL != esrc && |
| 1434 | NULL != fib_entry_src_vft[source].fesv_set_data) |
| 1435 | { |
| 1436 | fib_entry_src_vft[source].fesv_set_data(esrc, fib_entry, data); |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | const void* |
| 1441 | fib_entry_get_source_data (fib_node_index_t fib_entry_index, |
| 1442 | fib_source_t source) |
| 1443 | { |
| 1444 | fib_entry_t *fib_entry; |
| 1445 | fib_entry_src_t *esrc; |
| 1446 | |
| 1447 | fib_entry = fib_entry_get(fib_entry_index); |
| 1448 | esrc = fib_entry_src_find(fib_entry, source, NULL); |
| 1449 | |
| 1450 | if (NULL != esrc && |
| 1451 | NULL != fib_entry_src_vft[source].fesv_get_data) |
| 1452 | { |
| 1453 | return (fib_entry_src_vft[source].fesv_get_data(esrc, fib_entry)); |
| 1454 | } |
| 1455 | return (NULL); |
| 1456 | } |
| 1457 | |
| 1458 | void |
| 1459 | fib_entry_src_module_init (void) |
| 1460 | { |
| 1461 | fib_entry_src_rr_register(); |
| 1462 | fib_entry_src_interface_register(); |
| 1463 | fib_entry_src_default_route_register(); |
| 1464 | fib_entry_src_special_register(); |
| 1465 | fib_entry_src_api_register(); |
| 1466 | fib_entry_src_adj_register(); |
| 1467 | fib_entry_src_mpls_register(); |
| 1468 | fib_entry_src_lisp_register(); |
| 1469 | } |