Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [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 | |
| 18 | #include <vnet/mfib/mfib_entry.h> |
| 19 | #include <vnet/fib/fib_path_list.h> |
| 20 | |
| 21 | #include <vnet/dpo/drop_dpo.h> |
| 22 | #include <vnet/dpo/replicate_dpo.h> |
| 23 | |
| 24 | /** |
| 25 | * Debug macro |
| 26 | */ |
| 27 | #ifdef MFIB_DEBUG |
| 28 | #DEFIne MFIB_ENTRY_DBG(_e, _fmt, _args...) \ |
| 29 | { \ |
| 30 | u8*__tmp = NULL; \ |
| 31 | __tmp = format(__tmp, "e:[%d:%U", \ |
| 32 | mfib_entry_get_index(_e), \ |
| 33 | format_ip46_address, \ |
| 34 | &_e->mfe_prefix.fp_grp_addr, \ |
| 35 | IP46_TYPE_ANY); \ |
| 36 | __tmp = format(__tmp, "/%d,", \ |
| 37 | _e->mfe_prefix.fp_len); \ |
| 38 | __tmp = format(__tmp, "%U]", \ |
| 39 | mfib_entry_get_index(_e), \ |
| 40 | format_ip46_address, \ |
| 41 | &_e->mfe_prefix.fp_src_addr, \ |
| 42 | IP46_TYPE_ANY); \ |
| 43 | __tmp = format(__tmp, _fmt, ##_args); \ |
| 44 | clib_warning("%s", __tmp); \ |
| 45 | vec_free(__tmp); \ |
| 46 | } |
| 47 | #else |
| 48 | #define MFIB_ENTRY_DBG(_e, _fmt, _args...) |
| 49 | #endif |
| 50 | |
| 51 | /** |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 52 | * MFIB extensions to each path |
| 53 | */ |
| 54 | typedef struct mfib_path_ext_t_ |
| 55 | { |
| 56 | mfib_itf_flags_t mfpe_flags; |
| 57 | fib_node_index_t mfpe_path; |
| 58 | } mfib_path_ext_t; |
| 59 | |
| 60 | /** |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 61 | * The source of an MFIB entry |
| 62 | */ |
| 63 | typedef struct mfib_entry_src_t_ |
| 64 | { |
| 65 | /** |
| 66 | * Which source this is |
| 67 | */ |
| 68 | mfib_source_t mfes_src; |
| 69 | |
| 70 | /** |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 71 | * Route flags |
| 72 | */ |
| 73 | mfib_entry_flags_t mfes_flags; |
| 74 | |
| 75 | /** |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 76 | * The path-list of forwarding interfaces |
| 77 | */ |
| 78 | fib_node_index_t mfes_pl; |
| 79 | |
| 80 | /** |
| 81 | * RPF-ID |
| 82 | */ |
| 83 | fib_rpf_id_t mfes_rpf_id; |
| 84 | |
| 85 | /** |
| 86 | * Hash table of path extensions |
| 87 | */ |
| 88 | mfib_path_ext_t *mfes_exts; |
| 89 | |
| 90 | /** |
| 91 | * The hash table of all interfaces. |
| 92 | * This is forwarding time information derived from the paths |
| 93 | * and their extensions. |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 94 | */ |
| 95 | mfib_itf_t *mfes_itfs; |
| 96 | } mfib_entry_src_t; |
| 97 | |
| 98 | /** |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 99 | * Pool of path extensions |
| 100 | */ |
| 101 | static mfib_path_ext_t *mfib_path_ext_pool; |
| 102 | |
| 103 | /** |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 104 | * String names for each source |
| 105 | */ |
| 106 | static const char *mfib_source_names[] = MFIB_SOURCE_NAMES; |
| 107 | |
| 108 | /* |
| 109 | * Pool for all fib_entries |
| 110 | */ |
| 111 | mfib_entry_t *mfib_entry_pool; |
| 112 | |
| 113 | static fib_node_t * |
| 114 | mfib_entry_get_node (fib_node_index_t index) |
| 115 | { |
| 116 | return ((fib_node_t*)mfib_entry_get(index)); |
| 117 | } |
| 118 | |
| 119 | static fib_protocol_t |
| 120 | mfib_entry_get_proto (const mfib_entry_t * mfib_entry) |
| 121 | { |
| 122 | return (mfib_entry->mfe_prefix.fp_proto); |
| 123 | } |
| 124 | |
| 125 | fib_forward_chain_type_t |
| 126 | mfib_entry_get_default_chain_type (const mfib_entry_t *mfib_entry) |
| 127 | { |
| 128 | switch (mfib_entry->mfe_prefix.fp_proto) |
| 129 | { |
| 130 | case FIB_PROTOCOL_IP4: |
| 131 | return (FIB_FORW_CHAIN_TYPE_MCAST_IP4); |
| 132 | case FIB_PROTOCOL_IP6: |
| 133 | return (FIB_FORW_CHAIN_TYPE_MCAST_IP6); |
| 134 | case FIB_PROTOCOL_MPLS: |
| 135 | ASSERT(0); |
| 136 | break; |
| 137 | } |
| 138 | return (FIB_FORW_CHAIN_TYPE_MCAST_IP4); |
| 139 | } |
| 140 | |
| 141 | static u8 * |
| 142 | format_mfib_entry_dpo (u8 * s, va_list * args) |
| 143 | { |
| 144 | index_t fei = va_arg(*args, index_t); |
| 145 | CLIB_UNUSED(u32 indent) = va_arg(*args, u32); |
| 146 | |
| 147 | return (format(s, "%U", |
| 148 | format_mfib_entry, fei, |
| 149 | MFIB_ENTRY_FORMAT_BRIEF)); |
| 150 | } |
| 151 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 152 | static inline mfib_path_ext_t * |
| 153 | mfib_entry_path_ext_get (index_t mi) |
| 154 | { |
| 155 | return (pool_elt_at_index(mfib_path_ext_pool, mi)); |
| 156 | } |
| 157 | |
| 158 | static u8 * |
| 159 | format_mfib_entry_path_ext (u8 * s, va_list * args) |
| 160 | { |
| 161 | mfib_path_ext_t *path_ext; |
| 162 | index_t mpi = va_arg(*args, index_t); |
| 163 | |
| 164 | path_ext = mfib_entry_path_ext_get(mpi); |
| 165 | return (format(s, "path:%d flags:%U", |
| 166 | path_ext->mfpe_path, |
| 167 | format_mfib_itf_flags, path_ext->mfpe_flags)); |
| 168 | } |
| 169 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 170 | u8 * |
| 171 | format_mfib_entry (u8 * s, va_list * args) |
| 172 | { |
| 173 | fib_node_index_t fei, mfi; |
| 174 | mfib_entry_t *mfib_entry; |
| 175 | mfib_entry_src_t *msrc; |
| 176 | u32 sw_if_index; |
| 177 | int level; |
| 178 | |
| 179 | fei = va_arg (*args, fib_node_index_t); |
| 180 | level = va_arg (*args, int); |
| 181 | mfib_entry = mfib_entry_get(fei); |
| 182 | |
| 183 | s = format (s, "%U", format_mfib_prefix, &mfib_entry->mfe_prefix); |
| 184 | s = format (s, ": %U", format_mfib_entry_flags, mfib_entry->mfe_flags); |
| 185 | |
| 186 | if (level >= MFIB_ENTRY_FORMAT_DETAIL) |
| 187 | { |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 188 | fib_node_index_t path_index, mpi; |
| 189 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 190 | s = format (s, "\n"); |
| 191 | s = format (s, " fib:%d", mfib_entry->mfe_fib_index); |
| 192 | s = format (s, " index:%d", mfib_entry_get_index(mfib_entry)); |
| 193 | s = format (s, " locks:%d\n", mfib_entry->mfe_node.fn_locks); |
| 194 | vec_foreach(msrc, mfib_entry->mfe_srcs) |
| 195 | { |
| 196 | s = format (s, " src:%s", mfib_source_names[msrc->mfes_src]); |
| 197 | s = format (s, ": %U\n", format_mfib_entry_flags, msrc->mfes_flags); |
| 198 | if (FIB_NODE_INDEX_INVALID != msrc->mfes_pl) |
| 199 | { |
| 200 | s = fib_path_list_format(msrc->mfes_pl, s); |
| 201 | } |
Neale Ranns | 5d85f2d | 2017-05-02 10:17:17 -0700 | [diff] [blame] | 202 | s = format (s, " Extensions:\n"); |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 203 | hash_foreach(path_index, mpi, msrc->mfes_exts, |
| 204 | ({ |
| 205 | s = format(s, " %U\n", format_mfib_entry_path_ext, mpi); |
| 206 | })); |
Neale Ranns | 5d85f2d | 2017-05-02 10:17:17 -0700 | [diff] [blame] | 207 | s = format (s, " Interface-Forwarding:\n"); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 208 | hash_foreach(sw_if_index, mfi, msrc->mfes_itfs, |
| 209 | ({ |
| 210 | s = format(s, " %U\n", format_mfib_itf, mfi); |
| 211 | })); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | s = format(s, "\n Interfaces:"); |
| 216 | hash_foreach(sw_if_index, mfi, mfib_entry->mfe_itfs, |
| 217 | ({ |
| 218 | s = format(s, "\n %U", format_mfib_itf, mfi); |
| 219 | })); |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 220 | s = format(s, "\n RPF-ID:%d", mfib_entry->mfe_rpf_id); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 221 | s = format(s, "\n %U-chain\n %U", |
| 222 | format_fib_forw_chain_type, |
| 223 | mfib_entry_get_default_chain_type(mfib_entry), |
| 224 | format_dpo_id, |
| 225 | &mfib_entry->mfe_rep, |
| 226 | 2); |
| 227 | s = format(s, "\n"); |
| 228 | |
| 229 | if (level >= MFIB_ENTRY_FORMAT_DETAIL2) |
| 230 | { |
| 231 | s = format(s, "\nchildren:"); |
| 232 | s = fib_node_children_format(mfib_entry->mfe_node.fn_children, s); |
| 233 | } |
| 234 | |
| 235 | return (s); |
| 236 | } |
| 237 | |
| 238 | static mfib_entry_t* |
| 239 | mfib_entry_from_fib_node (fib_node_t *node) |
| 240 | { |
| 241 | #if CLIB_DEBUG > 0 |
| 242 | ASSERT(FIB_NODE_TYPE_MFIB_ENTRY == node->fn_type); |
| 243 | #endif |
| 244 | return ((mfib_entry_t*)node); |
| 245 | } |
| 246 | |
| 247 | static int |
| 248 | mfib_entry_src_cmp_for_sort (void * v1, |
| 249 | void * v2) |
| 250 | { |
| 251 | mfib_entry_src_t *esrc1 = v1, *esrc2 = v2; |
| 252 | |
| 253 | return (esrc1->mfes_src - esrc2->mfes_src); |
| 254 | } |
| 255 | |
| 256 | static void |
| 257 | mfib_entry_src_init (mfib_entry_t *mfib_entry, |
| 258 | mfib_source_t source) |
| 259 | |
| 260 | { |
| 261 | mfib_entry_src_t esrc = { |
| 262 | .mfes_pl = FIB_NODE_INDEX_INVALID, |
| 263 | .mfes_flags = MFIB_ENTRY_FLAG_NONE, |
| 264 | .mfes_src = source, |
| 265 | }; |
| 266 | |
| 267 | vec_add1(mfib_entry->mfe_srcs, esrc); |
| 268 | vec_sort_with_function(mfib_entry->mfe_srcs, |
| 269 | mfib_entry_src_cmp_for_sort); |
| 270 | } |
| 271 | |
| 272 | static mfib_entry_src_t * |
| 273 | mfib_entry_src_find (const mfib_entry_t *mfib_entry, |
| 274 | mfib_source_t source, |
| 275 | u32 *index) |
| 276 | |
| 277 | { |
| 278 | mfib_entry_src_t *esrc; |
| 279 | int ii; |
| 280 | |
| 281 | ii = 0; |
| 282 | vec_foreach(esrc, mfib_entry->mfe_srcs) |
| 283 | { |
| 284 | if (esrc->mfes_src == source) |
| 285 | { |
| 286 | if (NULL != index) |
| 287 | { |
| 288 | *index = ii; |
| 289 | } |
| 290 | return (esrc); |
| 291 | } |
| 292 | else |
| 293 | { |
| 294 | ii++; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | return (NULL); |
| 299 | } |
| 300 | |
| 301 | static mfib_entry_src_t * |
| 302 | mfib_entry_src_find_or_create (mfib_entry_t *mfib_entry, |
| 303 | mfib_source_t source) |
| 304 | { |
| 305 | mfib_entry_src_t *esrc; |
| 306 | |
| 307 | esrc = mfib_entry_src_find(mfib_entry, source, NULL); |
| 308 | |
| 309 | if (NULL == esrc) |
| 310 | { |
| 311 | mfib_entry_src_init(mfib_entry, source); |
| 312 | } |
| 313 | |
| 314 | return (mfib_entry_src_find(mfib_entry, source, NULL)); |
| 315 | } |
| 316 | |
| 317 | static mfib_entry_src_t* |
| 318 | mfib_entry_get_best_src (const mfib_entry_t *mfib_entry) |
| 319 | { |
| 320 | mfib_entry_src_t *bsrc; |
| 321 | |
| 322 | /* |
| 323 | * the enum of sources is deliberately arranged in priority order |
| 324 | */ |
| 325 | if (0 == vec_len(mfib_entry->mfe_srcs)) |
| 326 | { |
| 327 | bsrc = NULL; |
| 328 | } |
| 329 | else |
| 330 | { |
| 331 | bsrc = vec_elt_at_index(mfib_entry->mfe_srcs, 0); |
| 332 | } |
| 333 | |
| 334 | return (bsrc); |
| 335 | } |
| 336 | |
| 337 | static void |
| 338 | mfib_entry_src_flush (mfib_entry_src_t *msrc) |
| 339 | { |
| 340 | u32 sw_if_index; |
| 341 | index_t mfii; |
| 342 | |
| 343 | hash_foreach(sw_if_index, mfii, msrc->mfes_itfs, |
| 344 | ({ |
| 345 | mfib_itf_delete(mfib_itf_get(mfii)); |
| 346 | })); |
Neale Ranns | bcc6aa4 | 2017-02-24 01:34:14 -0800 | [diff] [blame] | 347 | hash_free(msrc->mfes_itfs); |
| 348 | msrc->mfes_itfs = NULL; |
Neale Ranns | a9374df | 2017-02-02 02:18:18 -0800 | [diff] [blame] | 349 | fib_path_list_unlock(msrc->mfes_pl); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static void |
| 353 | mfib_entry_src_remove (mfib_entry_t *mfib_entry, |
| 354 | mfib_source_t source) |
| 355 | |
| 356 | { |
| 357 | mfib_entry_src_t *msrc; |
| 358 | u32 index = ~0; |
| 359 | |
| 360 | msrc = mfib_entry_src_find(mfib_entry, source, &index); |
| 361 | |
| 362 | if (NULL != msrc) |
| 363 | { |
| 364 | mfib_entry_src_flush(msrc); |
| 365 | vec_del1(mfib_entry->mfe_srcs, index); |
| 366 | } |
| 367 | } |
| 368 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 369 | u32 |
| 370 | mfib_entry_child_add (fib_node_index_t mfib_entry_index, |
| 371 | fib_node_type_t child_type, |
| 372 | fib_node_index_t child_index) |
| 373 | { |
| 374 | return (fib_node_child_add(FIB_NODE_TYPE_MFIB_ENTRY, |
| 375 | mfib_entry_index, |
| 376 | child_type, |
| 377 | child_index)); |
| 378 | }; |
| 379 | |
| 380 | void |
| 381 | mfib_entry_child_remove (fib_node_index_t mfib_entry_index, |
| 382 | u32 sibling_index) |
| 383 | { |
| 384 | fib_node_child_remove(FIB_NODE_TYPE_MFIB_ENTRY, |
| 385 | mfib_entry_index, |
| 386 | sibling_index); |
| 387 | } |
| 388 | |
| 389 | static mfib_entry_t * |
| 390 | mfib_entry_alloc (u32 fib_index, |
| 391 | const mfib_prefix_t *prefix, |
| 392 | fib_node_index_t *mfib_entry_index) |
| 393 | { |
| 394 | mfib_entry_t *mfib_entry; |
| 395 | |
| 396 | pool_get(mfib_entry_pool, mfib_entry); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 397 | |
| 398 | fib_node_init(&mfib_entry->mfe_node, |
| 399 | FIB_NODE_TYPE_MFIB_ENTRY); |
| 400 | |
Neale Ranns | 5a72c1c | 2017-02-24 08:29:22 -0800 | [diff] [blame] | 401 | /* |
| 402 | * Some of the members require non-default initialisation |
| 403 | * so we also init those that don't and thus save on the call to memset. |
| 404 | */ |
| 405 | mfib_entry->mfe_flags = 0; |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 406 | mfib_entry->mfe_fib_index = fib_index; |
| 407 | mfib_entry->mfe_prefix = *prefix; |
Neale Ranns | 5a72c1c | 2017-02-24 08:29:22 -0800 | [diff] [blame] | 408 | mfib_entry->mfe_srcs = NULL; |
| 409 | mfib_entry->mfe_itfs = NULL; |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 410 | mfib_entry->mfe_rpf_id = MFIB_RPF_ID_NONE; |
Neale Ranns | c2aad53 | 2017-05-30 09:53:52 -0700 | [diff] [blame] | 411 | mfib_entry->mfe_pl = FIB_NODE_INDEX_INVALID; |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 412 | |
| 413 | dpo_reset(&mfib_entry->mfe_rep); |
| 414 | |
| 415 | *mfib_entry_index = mfib_entry_get_index(mfib_entry); |
| 416 | |
| 417 | MFIB_ENTRY_DBG(mfib_entry, "alloc"); |
| 418 | |
| 419 | return (mfib_entry); |
| 420 | } |
| 421 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 422 | static inline mfib_path_ext_t * |
| 423 | mfib_entry_path_ext_find (mfib_path_ext_t *exts, |
| 424 | fib_node_index_t path_index) |
| 425 | { |
| 426 | uword *p; |
| 427 | |
| 428 | p = hash_get(exts, path_index); |
| 429 | |
| 430 | if (NULL != p) |
| 431 | { |
| 432 | return (mfib_entry_path_ext_get(p[0])); |
| 433 | } |
| 434 | |
| 435 | return (NULL); |
| 436 | } |
| 437 | |
| 438 | static mfib_path_ext_t* |
| 439 | mfib_path_ext_add (mfib_entry_src_t *msrc, |
| 440 | fib_node_index_t path_index, |
| 441 | mfib_itf_flags_t mfi_flags) |
| 442 | { |
| 443 | mfib_path_ext_t *path_ext; |
| 444 | |
| 445 | pool_get(mfib_path_ext_pool, path_ext); |
| 446 | |
| 447 | path_ext->mfpe_flags = mfi_flags; |
| 448 | path_ext->mfpe_path = path_index; |
| 449 | |
| 450 | hash_set(msrc->mfes_exts, path_index, |
| 451 | path_ext - mfib_path_ext_pool); |
| 452 | |
| 453 | return (path_ext); |
| 454 | } |
| 455 | |
| 456 | static void |
| 457 | mfib_path_ext_remove (mfib_entry_src_t *msrc, |
| 458 | fib_node_index_t path_index) |
| 459 | { |
| 460 | mfib_path_ext_t *path_ext; |
| 461 | |
| 462 | path_ext = mfib_entry_path_ext_find(msrc->mfes_exts, path_index); |
| 463 | |
| 464 | hash_unset(msrc->mfes_exts, path_index); |
| 465 | pool_put(mfib_path_ext_pool, path_ext); |
| 466 | } |
| 467 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 468 | typedef struct mfib_entry_collect_forwarding_ctx_t_ |
| 469 | { |
| 470 | load_balance_path_t * next_hops; |
| 471 | fib_forward_chain_type_t fct; |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 472 | mfib_entry_src_t *msrc; |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 473 | } mfib_entry_collect_forwarding_ctx_t; |
| 474 | |
Neale Ranns | 8142499 | 2017-05-18 03:03:22 -0700 | [diff] [blame] | 475 | static fib_path_list_walk_rc_t |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 476 | mfib_entry_src_collect_forwarding (fib_node_index_t pl_index, |
| 477 | fib_node_index_t path_index, |
| 478 | void *arg) |
| 479 | { |
| 480 | mfib_entry_collect_forwarding_ctx_t *ctx; |
| 481 | load_balance_path_t *nh; |
| 482 | |
| 483 | ctx = arg; |
| 484 | |
| 485 | /* |
| 486 | * if the path is not resolved, don't include it. |
| 487 | */ |
| 488 | if (!fib_path_is_resolved(path_index)) |
| 489 | { |
Neale Ranns | 8142499 | 2017-05-18 03:03:22 -0700 | [diff] [blame] | 490 | return (FIB_PATH_LIST_WALK_CONTINUE); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 493 | /* |
| 494 | * If the path is not forwarding to use it |
| 495 | */ |
| 496 | mfib_path_ext_t *path_ext; |
| 497 | |
| 498 | path_ext = mfib_entry_path_ext_find(ctx->msrc->mfes_exts, |
| 499 | path_index); |
| 500 | |
| 501 | if (NULL != path_ext && |
| 502 | !(path_ext->mfpe_flags & MFIB_ITF_FLAG_FORWARD)) |
| 503 | { |
Neale Ranns | 8142499 | 2017-05-18 03:03:22 -0700 | [diff] [blame] | 504 | return (FIB_PATH_LIST_WALK_CONTINUE); |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 505 | } |
| 506 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 507 | switch (ctx->fct) |
| 508 | { |
| 509 | case FIB_FORW_CHAIN_TYPE_MCAST_IP4: |
| 510 | case FIB_FORW_CHAIN_TYPE_MCAST_IP6: |
| 511 | /* |
| 512 | * EOS traffic with no label to stack, we need the IP Adj |
| 513 | */ |
| 514 | vec_add2(ctx->next_hops, nh, 1); |
| 515 | |
| 516 | nh->path_index = path_index; |
| 517 | nh->path_weight = fib_path_get_weight(path_index); |
| 518 | fib_path_contribute_forwarding(path_index, ctx->fct, &nh->path_dpo); |
| 519 | break; |
| 520 | |
| 521 | case FIB_FORW_CHAIN_TYPE_UNICAST_IP4: |
| 522 | case FIB_FORW_CHAIN_TYPE_UNICAST_IP6: |
| 523 | case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS: |
| 524 | case FIB_FORW_CHAIN_TYPE_MPLS_EOS: |
| 525 | case FIB_FORW_CHAIN_TYPE_ETHERNET: |
Florin Coras | ce1b4c7 | 2017-01-26 14:25:34 -0800 | [diff] [blame] | 526 | case FIB_FORW_CHAIN_TYPE_NSH: |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 527 | ASSERT(0); |
| 528 | break; |
| 529 | } |
| 530 | |
Neale Ranns | 8142499 | 2017-05-18 03:03:22 -0700 | [diff] [blame] | 531 | return (FIB_PATH_LIST_WALK_CONTINUE); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | static void |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 535 | mfib_entry_stack (mfib_entry_t *mfib_entry, |
| 536 | mfib_entry_src_t *msrc) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 537 | { |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 538 | dpo_proto_t dp; |
| 539 | |
| 540 | dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry)); |
| 541 | |
Neale Ranns | c2aad53 | 2017-05-30 09:53:52 -0700 | [diff] [blame] | 542 | /* |
| 543 | * unlink the enty from the previous path list. |
| 544 | */ |
| 545 | if (FIB_NODE_INDEX_INVALID != mfib_entry->mfe_pl) |
| 546 | { |
| 547 | fib_path_list_child_remove(mfib_entry->mfe_pl, |
| 548 | mfib_entry->mfe_sibling); |
| 549 | } |
| 550 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 551 | if (NULL != msrc && |
| 552 | FIB_NODE_INDEX_INVALID != msrc->mfes_pl) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 553 | { |
Neale Ranns | a9374df | 2017-02-02 02:18:18 -0800 | [diff] [blame] | 554 | mfib_entry_collect_forwarding_ctx_t ctx = { |
| 555 | .next_hops = NULL, |
| 556 | .fct = mfib_entry_get_default_chain_type(mfib_entry), |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 557 | .msrc = msrc, |
Neale Ranns | a9374df | 2017-02-02 02:18:18 -0800 | [diff] [blame] | 558 | }; |
| 559 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 560 | fib_path_list_walk(msrc->mfes_pl, |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 561 | mfib_entry_src_collect_forwarding, |
| 562 | &ctx); |
| 563 | |
Neale Ranns | a9374df | 2017-02-02 02:18:18 -0800 | [diff] [blame] | 564 | if (!(MFIB_ENTRY_FLAG_EXCLUSIVE & mfib_entry->mfe_flags)) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 565 | { |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 566 | if (NULL == ctx.next_hops) |
Neale Ranns | a9374df | 2017-02-02 02:18:18 -0800 | [diff] [blame] | 567 | { |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 568 | /* |
| 569 | * no next-hops, stack directly on the drop |
| 570 | */ |
Neale Ranns | a9374df | 2017-02-02 02:18:18 -0800 | [diff] [blame] | 571 | dpo_stack(DPO_MFIB_ENTRY, dp, |
| 572 | &mfib_entry->mfe_rep, |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 573 | drop_dpo_get(dp)); |
Neale Ranns | a9374df | 2017-02-02 02:18:18 -0800 | [diff] [blame] | 574 | } |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 575 | else |
| 576 | { |
| 577 | /* |
| 578 | * each path contirbutes a next-hop. form a replicate |
| 579 | * from those choices. |
| 580 | */ |
| 581 | if (!dpo_id_is_valid(&mfib_entry->mfe_rep) || |
| 582 | dpo_is_drop(&mfib_entry->mfe_rep)) |
| 583 | { |
| 584 | dpo_id_t tmp_dpo = DPO_INVALID; |
| 585 | |
| 586 | dpo_set(&tmp_dpo, |
| 587 | DPO_REPLICATE, dp, |
| 588 | replicate_create(0, dp)); |
| 589 | |
| 590 | dpo_stack(DPO_MFIB_ENTRY, dp, |
| 591 | &mfib_entry->mfe_rep, |
| 592 | &tmp_dpo); |
| 593 | |
| 594 | dpo_reset(&tmp_dpo); |
| 595 | } |
| 596 | replicate_multipath_update(&mfib_entry->mfe_rep, |
| 597 | ctx.next_hops); |
| 598 | } |
Neale Ranns | a9374df | 2017-02-02 02:18:18 -0800 | [diff] [blame] | 599 | } |
| 600 | else |
| 601 | { |
| 602 | /* |
| 603 | * for exclusive routes the source provided a replicate DPO |
| 604 | * we we stashed inthe special path list with one path |
| 605 | * so we can stack directly on that. |
| 606 | */ |
| 607 | ASSERT(1 == vec_len(ctx.next_hops)); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 608 | |
| 609 | dpo_stack(DPO_MFIB_ENTRY, dp, |
| 610 | &mfib_entry->mfe_rep, |
Neale Ranns | a9374df | 2017-02-02 02:18:18 -0800 | [diff] [blame] | 611 | &ctx.next_hops[0].path_dpo); |
| 612 | dpo_reset(&ctx.next_hops[0].path_dpo); |
| 613 | vec_free(ctx.next_hops); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 614 | } |
Neale Ranns | c2aad53 | 2017-05-30 09:53:52 -0700 | [diff] [blame] | 615 | |
| 616 | /* |
| 617 | * link the entry to the path-list. |
| 618 | * The entry needs to be a child so that we receive the back-walk |
| 619 | * updates to recalculate forwarding. |
| 620 | */ |
| 621 | mfib_entry->mfe_pl = msrc->mfes_pl; |
| 622 | mfib_entry->mfe_sibling = |
| 623 | fib_path_list_child_add(mfib_entry->mfe_pl, |
| 624 | FIB_NODE_TYPE_MFIB_ENTRY, |
| 625 | mfib_entry_get_index(mfib_entry)); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 626 | } |
| 627 | else |
| 628 | { |
| 629 | dpo_stack(DPO_MFIB_ENTRY, dp, |
| 630 | &mfib_entry->mfe_rep, |
| 631 | drop_dpo_get(dp)); |
| 632 | } |
| 633 | } |
| 634 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 635 | static fib_node_index_t |
| 636 | mfib_entry_src_path_add (mfib_entry_src_t *msrc, |
| 637 | const fib_route_path_t *rpath) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 638 | { |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 639 | fib_node_index_t path_index; |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 640 | fib_route_path_t *rpaths; |
| 641 | |
Neale Ranns | a9374df | 2017-02-02 02:18:18 -0800 | [diff] [blame] | 642 | ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_flags)); |
| 643 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 644 | /* |
| 645 | * path-lists require a vector of paths |
| 646 | */ |
| 647 | rpaths = NULL; |
| 648 | vec_add1(rpaths, rpath[0]); |
| 649 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 650 | if (FIB_NODE_INDEX_INVALID == msrc->mfes_pl) |
| 651 | { |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 652 | /* A non-shared path-list */ |
| 653 | msrc->mfes_pl = fib_path_list_create(FIB_PATH_LIST_FLAG_NO_URPF, |
| 654 | NULL); |
| 655 | fib_path_list_lock(msrc->mfes_pl); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 656 | } |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 657 | |
| 658 | path_index = fib_path_list_path_add(msrc->mfes_pl, rpaths); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 659 | |
| 660 | vec_free(rpaths); |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 661 | |
| 662 | return (path_index); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 665 | static fib_node_index_t |
| 666 | mfib_entry_src_path_remove (mfib_entry_src_t *msrc, |
| 667 | const fib_route_path_t *rpath) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 668 | { |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 669 | fib_node_index_t path_index; |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 670 | fib_route_path_t *rpaths; |
| 671 | |
Neale Ranns | a9374df | 2017-02-02 02:18:18 -0800 | [diff] [blame] | 672 | ASSERT(!(MFIB_ENTRY_FLAG_EXCLUSIVE & msrc->mfes_flags)); |
| 673 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 674 | /* |
| 675 | * path-lists require a vector of paths |
| 676 | */ |
| 677 | rpaths = NULL; |
| 678 | vec_add1(rpaths, rpath[0]); |
| 679 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 680 | path_index = fib_path_list_path_remove(msrc->mfes_pl, rpaths); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 681 | |
| 682 | vec_free(rpaths); |
| 683 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 684 | return (path_index); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | static void |
| 688 | mfib_entry_recalculate_forwarding (mfib_entry_t *mfib_entry) |
| 689 | { |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 690 | mfib_entry_src_t *bsrc; |
| 691 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 692 | /* |
| 693 | * copy the forwarding data from the bast source |
| 694 | */ |
| 695 | bsrc = mfib_entry_get_best_src(mfib_entry); |
| 696 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 697 | if (NULL != bsrc) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 698 | { |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 699 | mfib_entry->mfe_flags = bsrc->mfes_flags; |
| 700 | mfib_entry->mfe_itfs = bsrc->mfes_itfs; |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 701 | mfib_entry->mfe_rpf_id = bsrc->mfes_rpf_id; |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 704 | mfib_entry_stack(mfib_entry, bsrc); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | |
| 708 | fib_node_index_t |
| 709 | mfib_entry_create (u32 fib_index, |
| 710 | mfib_source_t source, |
| 711 | const mfib_prefix_t *prefix, |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 712 | fib_rpf_id_t rpf_id, |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 713 | mfib_entry_flags_t entry_flags) |
| 714 | { |
| 715 | fib_node_index_t mfib_entry_index; |
| 716 | mfib_entry_t *mfib_entry; |
| 717 | mfib_entry_src_t *msrc; |
| 718 | |
| 719 | mfib_entry = mfib_entry_alloc(fib_index, prefix, |
| 720 | &mfib_entry_index); |
| 721 | msrc = mfib_entry_src_find_or_create(mfib_entry, source); |
| 722 | msrc->mfes_flags = entry_flags; |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 723 | msrc->mfes_rpf_id = rpf_id; |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 724 | |
| 725 | mfib_entry_recalculate_forwarding(mfib_entry); |
| 726 | |
| 727 | return (mfib_entry_index); |
| 728 | } |
| 729 | |
| 730 | static int |
| 731 | mfib_entry_ok_for_delete (mfib_entry_t *mfib_entry) |
| 732 | { |
| 733 | return (0 == vec_len(mfib_entry->mfe_srcs)); |
| 734 | } |
| 735 | |
| 736 | static int |
| 737 | mfib_entry_src_ok_for_delete (const mfib_entry_src_t *msrc) |
| 738 | { |
| 739 | return ((MFIB_ENTRY_FLAG_NONE == msrc->mfes_flags && |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 740 | 0 == fib_path_list_get_n_paths(msrc->mfes_pl))); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | int |
| 744 | mfib_entry_update (fib_node_index_t mfib_entry_index, |
| 745 | mfib_source_t source, |
Neale Ranns | a9374df | 2017-02-02 02:18:18 -0800 | [diff] [blame] | 746 | mfib_entry_flags_t entry_flags, |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 747 | fib_rpf_id_t rpf_id, |
Neale Ranns | a9374df | 2017-02-02 02:18:18 -0800 | [diff] [blame] | 748 | index_t repi) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 749 | { |
| 750 | mfib_entry_t *mfib_entry; |
| 751 | mfib_entry_src_t *msrc; |
| 752 | |
| 753 | mfib_entry = mfib_entry_get(mfib_entry_index); |
| 754 | msrc = mfib_entry_src_find_or_create(mfib_entry, source); |
| 755 | msrc->mfes_flags = entry_flags; |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 756 | msrc->mfes_rpf_id = rpf_id; |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 757 | |
Neale Ranns | a9374df | 2017-02-02 02:18:18 -0800 | [diff] [blame] | 758 | if (INDEX_INVALID != repi) |
| 759 | { |
| 760 | /* |
| 761 | * The source is providing its own replicate DPO. |
| 762 | * Create a sepcial path-list to manage it, that way |
| 763 | * this entry and the source are equivalent to a normal |
| 764 | * entry |
| 765 | */ |
| 766 | fib_node_index_t old_pl_index; |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 767 | dpo_proto_t dp; |
Neale Ranns | a9374df | 2017-02-02 02:18:18 -0800 | [diff] [blame] | 768 | dpo_id_t dpo = DPO_INVALID; |
| 769 | |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 770 | dp = fib_proto_to_dpo(mfib_entry_get_proto(mfib_entry)); |
Neale Ranns | a9374df | 2017-02-02 02:18:18 -0800 | [diff] [blame] | 771 | old_pl_index = msrc->mfes_pl; |
| 772 | |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 773 | dpo_set(&dpo, DPO_REPLICATE, dp, repi); |
Neale Ranns | a9374df | 2017-02-02 02:18:18 -0800 | [diff] [blame] | 774 | |
| 775 | msrc->mfes_pl = |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 776 | fib_path_list_create_special(dp, |
Neale Ranns | a9374df | 2017-02-02 02:18:18 -0800 | [diff] [blame] | 777 | FIB_PATH_LIST_FLAG_EXCLUSIVE, |
| 778 | &dpo); |
| 779 | |
| 780 | dpo_reset(&dpo); |
| 781 | fib_path_list_lock(msrc->mfes_pl); |
| 782 | fib_path_list_unlock(old_pl_index); |
| 783 | } |
| 784 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 785 | if (mfib_entry_src_ok_for_delete(msrc)) |
| 786 | { |
| 787 | /* |
| 788 | * this source has no interfaces and no flags. |
| 789 | * it has nothing left to give - remove it |
| 790 | */ |
| 791 | mfib_entry_src_remove(mfib_entry, source); |
| 792 | } |
| 793 | |
| 794 | mfib_entry_recalculate_forwarding(mfib_entry); |
| 795 | |
| 796 | return (mfib_entry_ok_for_delete(mfib_entry)); |
| 797 | } |
| 798 | |
| 799 | static void |
| 800 | mfib_entry_itf_add (mfib_entry_src_t *msrc, |
| 801 | u32 sw_if_index, |
| 802 | index_t mi) |
| 803 | { |
| 804 | hash_set(msrc->mfes_itfs, sw_if_index, mi); |
| 805 | } |
| 806 | |
| 807 | static void |
| 808 | mfib_entry_itf_remove (mfib_entry_src_t *msrc, |
| 809 | u32 sw_if_index) |
| 810 | { |
| 811 | mfib_itf_t *mfi; |
| 812 | |
| 813 | mfi = mfib_entry_itf_find(msrc->mfes_itfs, sw_if_index); |
| 814 | |
| 815 | mfib_itf_delete(mfi); |
| 816 | |
| 817 | hash_unset(msrc->mfes_itfs, sw_if_index); |
| 818 | } |
| 819 | |
| 820 | void |
| 821 | mfib_entry_path_update (fib_node_index_t mfib_entry_index, |
| 822 | mfib_source_t source, |
| 823 | const fib_route_path_t *rpath, |
| 824 | mfib_itf_flags_t itf_flags) |
| 825 | { |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 826 | fib_node_index_t path_index; |
| 827 | mfib_path_ext_t *path_ext; |
| 828 | mfib_itf_flags_t old, new; |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 829 | mfib_entry_t *mfib_entry; |
| 830 | mfib_entry_src_t *msrc; |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 831 | |
| 832 | mfib_entry = mfib_entry_get(mfib_entry_index); |
| 833 | ASSERT(NULL != mfib_entry); |
| 834 | msrc = mfib_entry_src_find_or_create(mfib_entry, source); |
| 835 | |
| 836 | /* |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 837 | * add the path to the path-list. If it's a duplicate we'll get |
| 838 | * back the original path. |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 839 | */ |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 840 | path_index = mfib_entry_src_path_add(msrc, rpath); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 841 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 842 | /* |
| 843 | * find the path extension for that path |
| 844 | */ |
| 845 | path_ext = mfib_entry_path_ext_find(msrc->mfes_exts, path_index); |
| 846 | |
| 847 | if (NULL == path_ext) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 848 | { |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 849 | old = MFIB_ITF_FLAG_NONE; |
| 850 | path_ext = mfib_path_ext_add(msrc, path_index, itf_flags); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 851 | } |
| 852 | else |
| 853 | { |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 854 | old = path_ext->mfpe_flags; |
| 855 | path_ext->mfpe_flags = itf_flags; |
| 856 | } |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 857 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 858 | /* |
| 859 | * Has the path changed its contribution to the input interface set. |
| 860 | * Which only paths with interfaces can do... |
| 861 | */ |
| 862 | if (~0 != rpath[0].frp_sw_if_index) |
| 863 | { |
| 864 | mfib_itf_t *mfib_itf; |
| 865 | |
| 866 | new = itf_flags; |
| 867 | |
| 868 | if (old != new) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 869 | { |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 870 | if (MFIB_ITF_FLAG_NONE == new) |
| 871 | { |
| 872 | /* |
| 873 | * no more interface flags on this path, remove |
| 874 | * from the data-plane set |
| 875 | */ |
| 876 | mfib_entry_itf_remove(msrc, rpath[0].frp_sw_if_index); |
| 877 | } |
| 878 | else if (MFIB_ITF_FLAG_NONE == old) |
| 879 | { |
| 880 | /* |
| 881 | * This interface is now contributing |
| 882 | */ |
| 883 | mfib_entry_itf_add(msrc, |
| 884 | rpath[0].frp_sw_if_index, |
| 885 | mfib_itf_create(rpath[0].frp_sw_if_index, |
| 886 | itf_flags)); |
| 887 | } |
| 888 | else |
| 889 | { |
| 890 | /* |
| 891 | * change of flag contributions |
| 892 | */ |
| 893 | mfib_itf = mfib_entry_itf_find(msrc->mfes_itfs, |
| 894 | rpath[0].frp_sw_if_index); |
| 895 | /* Seen by packets inflight */ |
| 896 | mfib_itf->mfi_flags = new; |
| 897 | } |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 898 | } |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | mfib_entry_recalculate_forwarding(mfib_entry); |
| 902 | } |
| 903 | |
| 904 | /* |
| 905 | * mfib_entry_path_remove |
| 906 | * |
| 907 | * remove a path from the entry. |
| 908 | * return the mfib_entry's index if it is still present, INVALID otherwise. |
| 909 | */ |
| 910 | int |
| 911 | mfib_entry_path_remove (fib_node_index_t mfib_entry_index, |
| 912 | mfib_source_t source, |
| 913 | const fib_route_path_t *rpath) |
| 914 | { |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 915 | fib_node_index_t path_index; |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 916 | mfib_entry_t *mfib_entry; |
| 917 | mfib_entry_src_t *msrc; |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 918 | |
| 919 | mfib_entry = mfib_entry_get(mfib_entry_index); |
| 920 | ASSERT(NULL != mfib_entry); |
| 921 | msrc = mfib_entry_src_find(mfib_entry, source, NULL); |
| 922 | |
| 923 | if (NULL == msrc) |
| 924 | { |
| 925 | /* |
| 926 | * there are no paths left for this source |
| 927 | */ |
| 928 | return (mfib_entry_ok_for_delete(mfib_entry)); |
| 929 | } |
| 930 | |
| 931 | /* |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 932 | * remove the path from the path-list. If it's not there we'll get |
| 933 | * back invalid |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 934 | */ |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 935 | path_index = mfib_entry_src_path_remove(msrc, rpath); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 936 | |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 937 | if (FIB_NODE_INDEX_INVALID != path_index) |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 938 | { |
| 939 | /* |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 940 | * don't need the extension, nor the interface anymore |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 941 | */ |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 942 | mfib_path_ext_remove(msrc, path_index); |
| 943 | if (~0 != rpath[0].frp_sw_if_index) |
| 944 | { |
| 945 | mfib_entry_itf_remove(msrc, rpath[0].frp_sw_if_index); |
| 946 | } |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 949 | if (mfib_entry_src_ok_for_delete(msrc)) |
| 950 | { |
| 951 | /* |
| 952 | * this source has no interfaces and no flags. |
| 953 | * it has nothing left to give - remove it |
| 954 | */ |
| 955 | mfib_entry_src_remove(mfib_entry, source); |
| 956 | } |
| 957 | |
| 958 | mfib_entry_recalculate_forwarding(mfib_entry); |
| 959 | |
| 960 | return (mfib_entry_ok_for_delete(mfib_entry)); |
| 961 | } |
| 962 | |
| 963 | /** |
| 964 | * mfib_entry_delete |
| 965 | * |
| 966 | * The source is withdrawing all the paths it provided |
| 967 | */ |
| 968 | int |
| 969 | mfib_entry_delete (fib_node_index_t mfib_entry_index, |
| 970 | mfib_source_t source) |
| 971 | { |
| 972 | mfib_entry_t *mfib_entry; |
| 973 | |
| 974 | mfib_entry = mfib_entry_get(mfib_entry_index); |
| 975 | mfib_entry_src_remove(mfib_entry, source); |
| 976 | |
| 977 | mfib_entry_recalculate_forwarding(mfib_entry); |
| 978 | |
| 979 | return (mfib_entry_ok_for_delete(mfib_entry)); |
| 980 | } |
| 981 | |
| 982 | static int |
| 983 | fib_ip4_address_compare (ip4_address_t * a1, |
| 984 | ip4_address_t * a2) |
| 985 | { |
| 986 | /* |
| 987 | * IP addresses are unsiged ints. the return value here needs to be signed |
| 988 | * a simple subtraction won't cut it. |
| 989 | * If the addresses are the same, the sort order is undefiend, so phoey. |
| 990 | */ |
| 991 | return ((clib_net_to_host_u32(a1->data_u32) > |
| 992 | clib_net_to_host_u32(a2->data_u32) ) ? |
| 993 | 1 : -1); |
| 994 | } |
| 995 | |
| 996 | static int |
| 997 | fib_ip6_address_compare (ip6_address_t * a1, |
| 998 | ip6_address_t * a2) |
| 999 | { |
| 1000 | int i; |
| 1001 | for (i = 0; i < ARRAY_LEN (a1->as_u16); i++) |
| 1002 | { |
| 1003 | int cmp = (clib_net_to_host_u16 (a1->as_u16[i]) - |
| 1004 | clib_net_to_host_u16 (a2->as_u16[i])); |
| 1005 | if (cmp != 0) |
| 1006 | return cmp; |
| 1007 | } |
| 1008 | return 0; |
| 1009 | } |
| 1010 | |
| 1011 | static int |
| 1012 | mfib_entry_cmp (fib_node_index_t mfib_entry_index1, |
| 1013 | fib_node_index_t mfib_entry_index2) |
| 1014 | { |
| 1015 | mfib_entry_t *mfib_entry1, *mfib_entry2; |
| 1016 | int cmp = 0; |
| 1017 | |
| 1018 | mfib_entry1 = mfib_entry_get(mfib_entry_index1); |
| 1019 | mfib_entry2 = mfib_entry_get(mfib_entry_index2); |
| 1020 | |
| 1021 | switch (mfib_entry1->mfe_prefix.fp_proto) |
| 1022 | { |
| 1023 | case FIB_PROTOCOL_IP4: |
| 1024 | cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip4, |
| 1025 | &mfib_entry2->mfe_prefix.fp_grp_addr.ip4); |
| 1026 | |
| 1027 | if (0 == cmp) |
| 1028 | { |
| 1029 | cmp = fib_ip4_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip4, |
| 1030 | &mfib_entry2->mfe_prefix.fp_src_addr.ip4); |
| 1031 | } |
| 1032 | break; |
| 1033 | case FIB_PROTOCOL_IP6: |
| 1034 | cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_grp_addr.ip6, |
| 1035 | &mfib_entry2->mfe_prefix.fp_grp_addr.ip6); |
| 1036 | |
| 1037 | if (0 == cmp) |
| 1038 | { |
| 1039 | cmp = fib_ip6_address_compare(&mfib_entry1->mfe_prefix.fp_src_addr.ip6, |
| 1040 | &mfib_entry2->mfe_prefix.fp_src_addr.ip6); |
| 1041 | } |
| 1042 | break; |
| 1043 | case FIB_PROTOCOL_MPLS: |
| 1044 | ASSERT(0); |
| 1045 | cmp = 0; |
| 1046 | break; |
| 1047 | } |
| 1048 | |
| 1049 | if (0 == cmp) { |
| 1050 | cmp = (mfib_entry1->mfe_prefix.fp_len - mfib_entry2->mfe_prefix.fp_len); |
| 1051 | } |
| 1052 | return (cmp); |
| 1053 | } |
| 1054 | |
| 1055 | int |
| 1056 | mfib_entry_cmp_for_sort (void *i1, void *i2) |
| 1057 | { |
| 1058 | fib_node_index_t *mfib_entry_index1 = i1, *mfib_entry_index2 = i2; |
| 1059 | |
| 1060 | return (mfib_entry_cmp(*mfib_entry_index1, |
| 1061 | *mfib_entry_index2)); |
| 1062 | } |
| 1063 | |
Neale Ranns | c2aad53 | 2017-05-30 09:53:52 -0700 | [diff] [blame] | 1064 | static void |
| 1065 | mfib_entry_last_lock_gone (fib_node_t *node) |
| 1066 | { |
| 1067 | mfib_entry_t *mfib_entry; |
| 1068 | mfib_entry_src_t *msrc; |
| 1069 | |
| 1070 | mfib_entry = mfib_entry_from_fib_node(node); |
| 1071 | |
| 1072 | dpo_reset(&mfib_entry->mfe_rep); |
| 1073 | |
| 1074 | MFIB_ENTRY_DBG(mfib_entry, "last-lock"); |
| 1075 | |
| 1076 | vec_foreach(msrc, mfib_entry->mfe_srcs) |
| 1077 | { |
| 1078 | mfib_entry_src_flush(msrc); |
| 1079 | } |
| 1080 | |
| 1081 | vec_free(mfib_entry->mfe_srcs); |
| 1082 | |
| 1083 | fib_node_deinit(&mfib_entry->mfe_node); |
| 1084 | pool_put(mfib_entry_pool, mfib_entry); |
| 1085 | } |
| 1086 | |
| 1087 | /* |
| 1088 | * mfib_entry_back_walk_notify |
| 1089 | * |
| 1090 | * A back walk has reach this entry. |
| 1091 | */ |
| 1092 | static fib_node_back_walk_rc_t |
| 1093 | mfib_entry_back_walk_notify (fib_node_t *node, |
| 1094 | fib_node_back_walk_ctx_t *ctx) |
| 1095 | { |
| 1096 | mfib_entry_recalculate_forwarding(mfib_entry_from_fib_node(node)); |
| 1097 | |
| 1098 | return (FIB_NODE_BACK_WALK_CONTINUE); |
| 1099 | } |
| 1100 | |
| 1101 | static void |
| 1102 | mfib_entry_show_memory (void) |
| 1103 | { |
| 1104 | fib_show_memory_usage("multicast-Entry", |
| 1105 | pool_elts(mfib_entry_pool), |
| 1106 | pool_len(mfib_entry_pool), |
| 1107 | sizeof(mfib_entry_t)); |
| 1108 | } |
| 1109 | |
| 1110 | /* |
| 1111 | * The MFIB entry's graph node virtual function table |
| 1112 | */ |
| 1113 | static const fib_node_vft_t mfib_entry_vft = { |
| 1114 | .fnv_get = mfib_entry_get_node, |
| 1115 | .fnv_last_lock = mfib_entry_last_lock_gone, |
| 1116 | .fnv_back_walk = mfib_entry_back_walk_notify, |
| 1117 | .fnv_mem_show = mfib_entry_show_memory, |
| 1118 | }; |
| 1119 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 1120 | void |
| 1121 | mfib_entry_lock (fib_node_index_t mfib_entry_index) |
| 1122 | { |
| 1123 | mfib_entry_t *mfib_entry; |
| 1124 | |
| 1125 | mfib_entry = mfib_entry_get(mfib_entry_index); |
| 1126 | |
| 1127 | fib_node_lock(&mfib_entry->mfe_node); |
| 1128 | } |
| 1129 | |
| 1130 | void |
| 1131 | mfib_entry_unlock (fib_node_index_t mfib_entry_index) |
| 1132 | { |
| 1133 | mfib_entry_t *mfib_entry; |
| 1134 | |
| 1135 | mfib_entry = mfib_entry_get(mfib_entry_index); |
| 1136 | |
| 1137 | fib_node_unlock(&mfib_entry->mfe_node); |
| 1138 | } |
| 1139 | |
| 1140 | static void |
| 1141 | mfib_entry_dpo_lock (dpo_id_t *dpo) |
| 1142 | { |
| 1143 | } |
| 1144 | static void |
| 1145 | mfib_entry_dpo_unlock (dpo_id_t *dpo) |
| 1146 | { |
| 1147 | } |
| 1148 | |
| 1149 | const static dpo_vft_t mfib_entry_dpo_vft = { |
| 1150 | .dv_lock = mfib_entry_dpo_lock, |
| 1151 | .dv_unlock = mfib_entry_dpo_unlock, |
| 1152 | .dv_format = format_mfib_entry_dpo, |
| 1153 | .dv_mem_show = mfib_entry_show_memory, |
| 1154 | }; |
| 1155 | |
| 1156 | const static char* const mfib_entry_ip4_nodes[] = |
| 1157 | { |
| 1158 | "ip4-mfib-forward-rpf", |
| 1159 | NULL, |
| 1160 | }; |
| 1161 | const static char* const mfib_entry_ip6_nodes[] = |
| 1162 | { |
| 1163 | "ip6-mfib-forward-rpf", |
| 1164 | NULL, |
| 1165 | }; |
| 1166 | |
| 1167 | const static char* const * const mfib_entry_nodes[DPO_PROTO_NUM] = |
| 1168 | { |
| 1169 | [DPO_PROTO_IP4] = mfib_entry_ip4_nodes, |
| 1170 | [DPO_PROTO_IP6] = mfib_entry_ip6_nodes, |
| 1171 | }; |
| 1172 | |
| 1173 | void |
| 1174 | mfib_entry_module_init (void) |
| 1175 | { |
| 1176 | fib_node_register_type (FIB_NODE_TYPE_MFIB_ENTRY, &mfib_entry_vft); |
| 1177 | dpo_register(DPO_MFIB_ENTRY, &mfib_entry_dpo_vft, mfib_entry_nodes); |
| 1178 | } |
| 1179 | |
| 1180 | void |
| 1181 | mfib_entry_encode (fib_node_index_t mfib_entry_index, |
| 1182 | fib_route_path_encode_t **api_rpaths) |
| 1183 | { |
| 1184 | mfib_entry_t *mfib_entry; |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 1185 | mfib_entry_src_t *bsrc; |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 1186 | |
| 1187 | mfib_entry = mfib_entry_get(mfib_entry_index); |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 1188 | bsrc = mfib_entry_get_best_src(mfib_entry); |
| 1189 | |
| 1190 | if (FIB_NODE_INDEX_INVALID != bsrc->mfes_pl) |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 1191 | { |
Neale Ranns | 0f26c5a | 2017-03-01 15:12:11 -0800 | [diff] [blame] | 1192 | fib_path_list_walk(bsrc->mfes_pl, |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 1193 | fib_path_encode, |
| 1194 | api_rpaths); |
| 1195 | } |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Neale Ranns | 5a8123b | 2017-01-26 01:18:23 -0800 | [diff] [blame] | 1198 | |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 1199 | void |
| 1200 | mfib_entry_get_prefix (fib_node_index_t mfib_entry_index, |
| 1201 | mfib_prefix_t *pfx) |
| 1202 | { |
| 1203 | mfib_entry_t *mfib_entry; |
| 1204 | |
| 1205 | mfib_entry = mfib_entry_get(mfib_entry_index); |
| 1206 | *pfx = mfib_entry->mfe_prefix; |
| 1207 | } |
| 1208 | |
| 1209 | u32 |
| 1210 | mfib_entry_get_fib_index (fib_node_index_t mfib_entry_index) |
| 1211 | { |
| 1212 | mfib_entry_t *mfib_entry; |
| 1213 | |
| 1214 | mfib_entry = mfib_entry_get(mfib_entry_index); |
| 1215 | |
| 1216 | return (mfib_entry->mfe_fib_index); |
| 1217 | } |
| 1218 | |
| 1219 | void |
| 1220 | mfib_entry_contribute_forwarding (fib_node_index_t mfib_entry_index, |
| 1221 | fib_forward_chain_type_t type, |
| 1222 | dpo_id_t *dpo) |
| 1223 | { |
| 1224 | /* |
| 1225 | * An IP mFIB entry can only provide a forwarding chain that |
| 1226 | * is the same IP proto as the prefix. |
| 1227 | * No use-cases (i know of) for other combinations. |
| 1228 | */ |
| 1229 | mfib_entry_t *mfib_entry; |
| 1230 | dpo_proto_t dp; |
| 1231 | |
| 1232 | mfib_entry = mfib_entry_get(mfib_entry_index); |
| 1233 | |
| 1234 | dp = fib_proto_to_dpo(mfib_entry->mfe_prefix.fp_proto); |
| 1235 | |
| 1236 | if (type == fib_forw_chain_type_from_dpo_proto(dp)) |
| 1237 | { |
| 1238 | dpo_copy(dpo, &mfib_entry->mfe_rep); |
| 1239 | } |
| 1240 | else |
| 1241 | { |
| 1242 | dpo_copy(dpo, drop_dpo_get(dp)); |
| 1243 | } |
| 1244 | } |
| 1245 | |
| 1246 | u32 |
| 1247 | mfib_entry_pool_size (void) |
| 1248 | { |
| 1249 | return (pool_elts(mfib_entry_pool)); |
| 1250 | } |
| 1251 | |
| 1252 | static clib_error_t * |
| 1253 | show_mfib_entry_command (vlib_main_t * vm, |
| 1254 | unformat_input_t * input, |
| 1255 | vlib_cli_command_t * cmd) |
| 1256 | { |
| 1257 | fib_node_index_t fei; |
| 1258 | |
| 1259 | if (unformat (input, "%d", &fei)) |
| 1260 | { |
| 1261 | /* |
| 1262 | * show one in detail |
| 1263 | */ |
| 1264 | if (!pool_is_free_index(mfib_entry_pool, fei)) |
| 1265 | { |
| 1266 | vlib_cli_output (vm, "%d@%U", |
| 1267 | fei, |
| 1268 | format_mfib_entry, fei, |
| 1269 | MFIB_ENTRY_FORMAT_DETAIL2); |
| 1270 | } |
| 1271 | else |
| 1272 | { |
| 1273 | vlib_cli_output (vm, "entry %d invalid", fei); |
| 1274 | } |
| 1275 | } |
| 1276 | else |
| 1277 | { |
| 1278 | /* |
| 1279 | * show all |
| 1280 | */ |
| 1281 | vlib_cli_output (vm, "FIB Entries:"); |
| 1282 | pool_foreach_index(fei, mfib_entry_pool, |
| 1283 | ({ |
| 1284 | vlib_cli_output (vm, "%d@%U", |
| 1285 | fei, |
| 1286 | format_mfib_entry, fei, |
| 1287 | MFIB_ENTRY_FORMAT_BRIEF); |
| 1288 | })); |
| 1289 | } |
| 1290 | |
| 1291 | return (NULL); |
| 1292 | } |
| 1293 | |
Neale Ranns | c756c1c | 2017-02-08 09:11:57 -0800 | [diff] [blame] | 1294 | /*? |
| 1295 | * This commnad displays an entry, or all entries, in the mfib tables indexed by their unique |
| 1296 | * numerical indentifier. |
| 1297 | ?*/ |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 1298 | VLIB_CLI_COMMAND (show_mfib_entry, static) = { |
| 1299 | .path = "show mfib entry", |
| 1300 | .function = show_mfib_entry_command, |
| 1301 | .short_help = "show mfib entry", |
| 1302 | }; |