3 * Copyright (C) 2017 Cumulus Networks, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "lib/printfrr.h"
32 #include "pimd/pim_nht.h"
36 #include "pim_ifchannel.h"
37 #include "pim_mroute.h"
38 #include "pim_zebra.h"
39 #include "pim_upstream.h"
41 #include "pim_jp_agg.h"
42 #include "pim_zebra.h"
43 #include "pim_zlookup.h"
48 * pim_sendmsg_zebra_rnh -- Format and send a nexthop register/Unregister
51 void pim_sendmsg_zebra_rnh(struct pim_instance
*pim
, struct zclient
*zclient
,
52 struct pim_nexthop_cache
*pnc
, int command
)
57 p
= &(pnc
->rpf
.rpf_addr
);
58 ret
= zclient_send_rnh(zclient
, command
, p
, false, false,
60 if (ret
== ZCLIENT_SEND_FAILURE
)
61 zlog_warn("sendmsg_nexthop: zclient_send_message() failed");
63 if (PIM_DEBUG_PIM_NHT
)
65 "%s: NHT %sregistered addr %pFX(%s) with Zebra ret:%d ",
67 (command
== ZEBRA_NEXTHOP_REGISTER
) ? " " : "de", p
,
73 struct pim_nexthop_cache
*pim_nexthop_cache_find(struct pim_instance
*pim
,
76 struct pim_nexthop_cache
*pnc
= NULL
;
77 struct pim_nexthop_cache lookup
;
79 lookup
.rpf
.rpf_addr
= rpf
->rpf_addr
;
80 pnc
= hash_lookup(pim
->rpf_hash
, &lookup
);
85 static struct pim_nexthop_cache
*pim_nexthop_cache_add(struct pim_instance
*pim
,
86 struct pim_rpf
*rpf_addr
)
88 struct pim_nexthop_cache
*pnc
;
91 pnc
= XCALLOC(MTYPE_PIM_NEXTHOP_CACHE
,
92 sizeof(struct pim_nexthop_cache
));
93 pnc
->rpf
.rpf_addr
= rpf_addr
->rpf_addr
;
95 pnc
= hash_get(pim
->rpf_hash
, pnc
, hash_alloc_intern
);
97 pnc
->rp_list
= list_new();
98 pnc
->rp_list
->cmp
= pim_rp_list_cmp
;
100 snprintfrr(hash_name
, sizeof(hash_name
), "PNC %pFX(%s) Upstream Hash",
101 &pnc
->rpf
.rpf_addr
, pim
->vrf
->name
);
102 pnc
->upstream_hash
= hash_create_size(8192, pim_upstream_hash_key
,
103 pim_upstream_equal
, hash_name
);
108 static struct pim_nexthop_cache
*pim_nht_get(struct pim_instance
*pim
,
111 struct pim_nexthop_cache
*pnc
= NULL
;
113 struct zclient
*zclient
= NULL
;
115 zclient
= pim_zebra_zclient_get();
116 memset(&rpf
, 0, sizeof(struct pim_rpf
));
117 rpf
.rpf_addr
= *addr
;
119 pnc
= pim_nexthop_cache_find(pim
, &rpf
);
121 pnc
= pim_nexthop_cache_add(pim
, &rpf
);
122 pim_sendmsg_zebra_rnh(pim
, zclient
, pnc
,
123 ZEBRA_NEXTHOP_REGISTER
);
124 if (PIM_DEBUG_PIM_NHT
)
126 "%s: NHT cache and zebra notification added for %pFX(%s)",
127 __func__
, addr
, pim
->vrf
->name
);
133 /* TBD: this does several distinct things and should probably be split up.
134 * (checking state vs. returning pnc vs. adding upstream vs. adding rp)
136 int pim_find_or_track_nexthop(struct pim_instance
*pim
, struct prefix
*addr
,
137 struct pim_upstream
*up
, struct rp_info
*rp
,
138 struct pim_nexthop_cache
*out_pnc
)
140 struct pim_nexthop_cache
*pnc
;
141 struct listnode
*ch_node
= NULL
;
143 pnc
= pim_nht_get(pim
, addr
);
145 assertf(up
|| rp
, "addr=%pFX", addr
);
148 ch_node
= listnode_lookup(pnc
->rp_list
, rp
);
150 listnode_add_sort(pnc
->rp_list
, rp
);
154 hash_get(pnc
->upstream_hash
, up
, hash_alloc_intern
);
156 if (CHECK_FLAG(pnc
->flags
, PIM_NEXTHOP_VALID
)) {
158 memcpy(out_pnc
, pnc
, sizeof(struct pim_nexthop_cache
));
165 void pim_nht_bsr_add(struct pim_instance
*pim
, struct in_addr addr
)
167 struct pim_nexthop_cache
*pnc
;
170 pfx
.family
= AF_INET
;
171 pfx
.prefixlen
= IPV4_MAX_BITLEN
;
172 pfx
.u
.prefix4
= addr
;
174 pnc
= pim_nht_get(pim
, &pfx
);
179 static void pim_nht_drop_maybe(struct pim_instance
*pim
,
180 struct pim_nexthop_cache
*pnc
)
182 if (PIM_DEBUG_PIM_NHT
)
184 "%s: NHT %pFX(%s) rp_list count:%d upstream count:%ld BSR count:%u",
185 __func__
, &pnc
->rpf
.rpf_addr
, pim
->vrf
->name
,
186 pnc
->rp_list
->count
, pnc
->upstream_hash
->count
,
189 if (pnc
->rp_list
->count
== 0 && pnc
->upstream_hash
->count
== 0
190 && pnc
->bsr_count
== 0) {
191 struct zclient
*zclient
= pim_zebra_zclient_get();
193 pim_sendmsg_zebra_rnh(pim
, zclient
, pnc
,
194 ZEBRA_NEXTHOP_UNREGISTER
);
196 list_delete(&pnc
->rp_list
);
197 hash_free(pnc
->upstream_hash
);
199 hash_release(pim
->rpf_hash
, pnc
);
201 nexthops_free(pnc
->nexthop
);
202 XFREE(MTYPE_PIM_NEXTHOP_CACHE
, pnc
);
206 void pim_delete_tracked_nexthop(struct pim_instance
*pim
, struct prefix
*addr
,
207 struct pim_upstream
*up
, struct rp_info
*rp
)
209 struct pim_nexthop_cache
*pnc
= NULL
;
210 struct pim_nexthop_cache lookup
;
211 struct pim_upstream
*upstream
= NULL
;
213 /* Remove from RPF hash if it is the last entry */
214 lookup
.rpf
.rpf_addr
= *addr
;
215 pnc
= hash_lookup(pim
->rpf_hash
, &lookup
);
217 zlog_warn("attempting to delete nonexistent NHT entry %pFX",
223 /* Release the (*, G)upstream from pnc->upstream_hash,
224 * whose Group belongs to the RP getting deleted
226 frr_each (rb_pim_upstream
, &pim
->upstream_head
, upstream
) {
228 struct rp_info
*trp_info
;
230 if (!pim_addr_is_any(upstream
->sg
.src
))
233 pim_addr_to_prefix(&grp
, upstream
->sg
.grp
);
234 trp_info
= pim_rp_find_match_group(pim
, &grp
);
236 hash_release(pnc
->upstream_hash
, upstream
);
238 listnode_delete(pnc
->rp_list
, rp
);
242 hash_release(pnc
->upstream_hash
, up
);
244 pim_nht_drop_maybe(pim
, pnc
);
247 void pim_nht_bsr_del(struct pim_instance
*pim
, struct in_addr addr
)
249 struct pim_nexthop_cache
*pnc
= NULL
;
250 struct pim_nexthop_cache lookup
;
253 * Nothing to do here if the address to unregister
254 * is 0.0.0.0 as that the BSR has not been registered
257 if (addr
.s_addr
== INADDR_ANY
)
260 lookup
.rpf
.rpf_addr
.family
= AF_INET
;
261 lookup
.rpf
.rpf_addr
.prefixlen
= IPV4_MAX_BITLEN
;
262 lookup
.rpf
.rpf_addr
.u
.prefix4
= addr
;
264 pnc
= hash_lookup(pim
->rpf_hash
, &lookup
);
267 zlog_warn("attempting to delete nonexistent NHT BSR entry %pI4",
272 assertf(pnc
->bsr_count
> 0, "addr=%pI4", &addr
);
275 pim_nht_drop_maybe(pim
, pnc
);
278 bool pim_nht_bsr_rpf_check(struct pim_instance
*pim
, struct in_addr bsr_addr
,
279 struct interface
*src_ifp
, struct in_addr src_ip
)
281 struct pim_nexthop_cache
*pnc
= NULL
;
282 struct pim_nexthop_cache lookup
;
283 struct pim_neighbor
*nbr
= NULL
;
285 struct interface
*ifp
;
287 lookup
.rpf
.rpf_addr
.family
= AF_INET
;
288 lookup
.rpf
.rpf_addr
.prefixlen
= IPV4_MAX_BITLEN
;
289 lookup
.rpf
.rpf_addr
.u
.prefix4
= bsr_addr
;
291 pnc
= hash_lookup(pim
->rpf_hash
, &lookup
);
292 if (!pnc
|| !CHECK_FLAG(pnc
->flags
, PIM_NEXTHOP_ANSWER_RECEIVED
)) {
293 /* BSM from a new freshly registered BSR - do a synchronous
294 * zebra query since otherwise we'd drop the first packet,
295 * leading to additional delay in picking up BSM data
298 /* FIXME: this should really be moved into a generic NHT
299 * function that does "add and get immediate result" or maybe
300 * "check cache or get immediate result." But until that can
301 * be worked in, here's a copy of the code below :(
303 struct pim_zlookup_nexthop nexthop_tab
[MULTIPATH_NUM
];
305 struct interface
*ifp
= NULL
;
308 memset(nexthop_tab
, 0, sizeof(nexthop_tab
));
309 num_ifindex
= zclient_lookup_nexthop(pim
, nexthop_tab
,
310 MULTIPATH_NUM
, bsr_addr
,
311 PIM_NEXTHOP_LOOKUP_MAX
);
313 if (num_ifindex
<= 0)
316 for (i
= 0; i
< num_ifindex
; i
++) {
317 struct pim_zlookup_nexthop
*znh
= &nexthop_tab
[i
];
319 /* pim_zlookup_nexthop has no ->type */
321 /* 1:1 match code below with znh instead of nh */
322 ifp
= if_lookup_by_index(znh
->ifindex
,
325 if (!ifp
|| !ifp
->info
)
328 if (if_is_loopback(ifp
) && if_is_loopback(src_ifp
))
331 nbr
= pim_neighbor_find_prefix(ifp
, &znh
->nexthop_addr
);
335 return znh
->ifindex
== src_ifp
->ifindex
336 && znh
->nexthop_addr
.u
.prefix4
.s_addr
342 if (!CHECK_FLAG(pnc
->flags
, PIM_NEXTHOP_VALID
))
345 /* if we accept BSMs from more than one ECMP nexthop, this will cause
346 * BSM message "multiplication" for each ECMP hop. i.e. if you have
347 * 4-way ECMP and 4 hops you end up with 256 copies of each BSM
350 * so... only accept the first (IPv4) valid nexthop as source.
353 for (nh
= pnc
->nexthop
; nh
; nh
= nh
->next
) {
358 case NEXTHOP_TYPE_IPV4
:
359 if (nh
->ifindex
== IFINDEX_INTERNAL
)
363 case NEXTHOP_TYPE_IPV4_IFINDEX
:
364 nhaddr
= nh
->gate
.ipv4
;
367 case NEXTHOP_TYPE_IPV6
:
368 if (nh
->ifindex
== IFINDEX_INTERNAL
)
372 case NEXTHOP_TYPE_IPV6_IFINDEX
:
373 nhaddr
= nh
->gate
.ipv6
;
376 case NEXTHOP_TYPE_IFINDEX
:
384 ifp
= if_lookup_by_index(nh
->ifindex
, pim
->vrf
->vrf_id
);
385 if (!ifp
|| !ifp
->info
)
388 if (if_is_loopback(ifp
) && if_is_loopback(src_ifp
))
391 /* MRIB (IGP) may be pointing at a router where PIM is down */
392 nbr
= pim_neighbor_find(ifp
, nhaddr
);
396 return nh
->ifindex
== src_ifp
->ifindex
397 && nhaddr
.s_addr
== src_ip
.s_addr
;
402 void pim_rp_nexthop_del(struct rp_info
*rp_info
)
404 rp_info
->rp
.source_nexthop
.interface
= NULL
;
405 pim_addr_to_prefix(&rp_info
->rp
.source_nexthop
.mrib_nexthop_addr
,
407 rp_info
->rp
.source_nexthop
.mrib_metric_preference
=
408 router
->infinite_assert_metric
.metric_preference
;
409 rp_info
->rp
.source_nexthop
.mrib_route_metric
=
410 router
->infinite_assert_metric
.route_metric
;
413 /* Update RP nexthop info based on Nexthop update received from Zebra.*/
414 static void pim_update_rp_nh(struct pim_instance
*pim
,
415 struct pim_nexthop_cache
*pnc
)
417 struct listnode
*node
= NULL
;
418 struct rp_info
*rp_info
= NULL
;
420 /*Traverse RP list and update each RP Nexthop info */
421 for (ALL_LIST_ELEMENTS_RO(pnc
->rp_list
, node
, rp_info
)) {
422 if (rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
== INADDR_NONE
)
425 // Compute PIM RPF using cached nexthop
426 if (!pim_ecmp_nexthop_lookup(pim
, &rp_info
->rp
.source_nexthop
,
427 &rp_info
->rp
.rpf_addr
,
429 pim_rp_nexthop_del(rp_info
);
433 /* Update Upstream nexthop info based on Nexthop update received from Zebra.*/
434 static int pim_update_upstream_nh_helper(struct hash_bucket
*bucket
, void *arg
)
436 struct pim_instance
*pim
= (struct pim_instance
*)arg
;
437 struct pim_upstream
*up
= (struct pim_upstream
*)bucket
->data
;
439 enum pim_rpf_result rpf_result
;
442 old
.source_nexthop
.interface
= up
->rpf
.source_nexthop
.interface
;
443 rpf_result
= pim_rpf_update(pim
, up
, &old
, __func__
);
445 /* update kernel multicast forwarding cache (MFC); if the
446 * RPF nbr is now unreachable the MFC has already been updated
449 if (rpf_result
!= PIM_RPF_FAILURE
)
450 pim_upstream_mroute_iif_update(up
->channel_oil
, __func__
);
452 if (rpf_result
== PIM_RPF_CHANGED
||
453 (rpf_result
== PIM_RPF_FAILURE
&& old
.source_nexthop
.interface
))
454 pim_zebra_upstream_rpf_changed(pim
, up
, &old
);
457 if (PIM_DEBUG_PIM_NHT
) {
459 "%s: NHT upstream %s(%s) old ifp %s new ifp %s",
460 __func__
, up
->sg_str
, pim
->vrf
->name
,
461 old
.source_nexthop
.interface
? old
.source_nexthop
464 up
->rpf
.source_nexthop
.interface
? up
->rpf
.source_nexthop
469 return HASHWALK_CONTINUE
;
472 static int pim_update_upstream_nh(struct pim_instance
*pim
,
473 struct pim_nexthop_cache
*pnc
)
475 hash_walk(pnc
->upstream_hash
, pim_update_upstream_nh_helper
, pim
);
477 pim_zebra_update_all_interfaces(pim
);
482 uint32_t pim_compute_ecmp_hash(struct prefix
*src
, struct prefix
*grp
)
485 uint32_t s
= 0, g
= 0;
490 switch (src
->family
) {
492 s
= src
->u
.prefix4
.s_addr
;
495 g
= grp
->u
.prefix4
.s_addr
;
501 hash_val
= jhash_2words(g
, s
, 101);
505 static int pim_ecmp_nexthop_search(struct pim_instance
*pim
,
506 struct pim_nexthop_cache
*pnc
,
507 struct pim_nexthop
*nexthop
,
508 struct prefix
*src
, struct prefix
*grp
,
511 struct pim_neighbor
*nbrs
[MULTIPATH_NUM
], *nbr
= NULL
;
512 struct interface
*ifps
[MULTIPATH_NUM
];
513 struct nexthop
*nh_node
= NULL
;
514 ifindex_t first_ifindex
;
515 struct interface
*ifp
= NULL
;
516 uint32_t hash_val
= 0, mod_val
= 0;
517 uint8_t nh_iter
= 0, found
= 0;
518 uint32_t i
, num_nbrs
= 0;
519 pim_addr nh_addr
= pim_addr_from_prefix(&(nexthop
->mrib_nexthop_addr
));
520 pim_addr src_addr
= pim_addr_from_prefix(src
);
521 pim_addr grp_addr
= pim_addr_from_prefix(grp
);
523 if (!pnc
|| !pnc
->nexthop_num
|| !nexthop
)
526 memset(&nbrs
, 0, sizeof(nbrs
));
527 memset(&ifps
, 0, sizeof(ifps
));
530 // Current Nexthop is VALID, check to stay on the current path.
531 if (nexthop
->interface
&& nexthop
->interface
->info
&&
532 (!pim_addr_is_any(nh_addr
))) {
533 /* User configured knob to explicitly switch
534 to new path is disabled or current path
535 metric is less than nexthop update.
538 if (pim
->ecmp_rebalance_enable
== 0) {
539 uint8_t curr_route_valid
= 0;
540 // Check if current nexthop is present in new updated
542 // If the current nexthop is not valid, candidate to
543 // choose new Nexthop.
544 for (nh_node
= pnc
->nexthop
; nh_node
;
545 nh_node
= nh_node
->next
) {
546 curr_route_valid
= (nexthop
->interface
->ifindex
547 == nh_node
->ifindex
);
548 if (curr_route_valid
)
553 && !pim_if_connected_to_source(nexthop
->interface
,
555 nbr
= pim_neighbor_find_prefix(
557 &nexthop
->mrib_nexthop_addr
);
559 && !if_is_loopback(nexthop
->interface
)) {
560 if (PIM_DEBUG_PIM_NHT
)
562 "%s: current nexthop does not have nbr ",
565 /* update metric even if the upstream
566 * neighbor stays unchanged
568 nexthop
->mrib_metric_preference
=
570 nexthop
->mrib_route_metric
=
572 if (PIM_DEBUG_PIM_NHT
)
574 "%s: (%pPA,%pPA)(%s) current nexthop %s is valid, skipping new path selection",
578 nexthop
->interface
->name
);
586 * Look up all interfaces and neighbors,
587 * store for later usage
589 for (nh_node
= pnc
->nexthop
, i
= 0; nh_node
;
590 nh_node
= nh_node
->next
, i
++) {
592 if_lookup_by_index(nh_node
->ifindex
, pim
->vrf
->vrf_id
);
595 pim_addr nhaddr
= nh_node
->gate
.ipv4
;
597 pim_addr nhaddr
= nh_node
->gate
.ipv6
;
599 nbrs
[i
] = pim_neighbor_find(ifps
[i
], nhaddr
);
601 pim_if_connected_to_source(ifps
[i
], src_addr
))
605 if (pim
->ecmp_enable
) {
606 uint32_t consider
= pnc
->nexthop_num
;
608 if (neighbor_needed
&& num_nbrs
< consider
)
614 // PIM ECMP flag is enable then choose ECMP path.
615 hash_val
= pim_compute_ecmp_hash(src
, grp
);
616 mod_val
= hash_val
% consider
;
619 for (nh_node
= pnc
->nexthop
; nh_node
&& (found
== 0);
620 nh_node
= nh_node
->next
) {
621 first_ifindex
= nh_node
->ifindex
;
624 if (PIM_DEBUG_PIM_NHT
)
626 "%s %s: could not find interface for ifindex %d (address %pPA(%s))",
627 __FILE__
, __func__
, first_ifindex
,
628 &src_addr
, pim
->vrf
->name
);
629 if (nh_iter
== mod_val
)
630 mod_val
++; // Select nexthpath
635 if (PIM_DEBUG_PIM_NHT
)
637 "%s: multicast not enabled on input interface %s(%s) (ifindex=%d, RPF for source %pPA)",
638 __func__
, ifp
->name
, pim
->vrf
->name
,
639 first_ifindex
, &src_addr
);
640 if (nh_iter
== mod_val
)
641 mod_val
++; // Select nexthpath
646 if (neighbor_needed
&&
647 !pim_if_connected_to_source(ifp
, src_addr
)) {
649 if (!nbr
&& !if_is_loopback(ifp
)) {
650 if (PIM_DEBUG_PIM_NHT
)
652 "%s: pim nbr not found on input interface %s(%s)",
655 if (nh_iter
== mod_val
)
656 mod_val
++; // Select nexthpath
662 if (nh_iter
== mod_val
) {
663 nexthop
->interface
= ifp
;
664 nexthop
->mrib_nexthop_addr
.family
= PIM_AF
;
665 nexthop
->mrib_nexthop_addr
.prefixlen
= PIM_MAX_BITLEN
;
667 nexthop
->mrib_nexthop_addr
.u
.prefix4
=
670 nexthop
->mrib_nexthop_addr
.u
.prefix6
=
673 nexthop
->mrib_metric_preference
= pnc
->distance
;
674 nexthop
->mrib_route_metric
= pnc
->metric
;
675 nexthop
->last_lookup
= src_addr
;
676 nexthop
->last_lookup_time
= pim_time_monotonic_usec();
679 if (PIM_DEBUG_PIM_NHT
)
681 "%s: (%pPA,%pPA)(%s) selected nhop interface %s addr %pPAs mod_val %u iter %d ecmp %d",
682 __func__
, &src_addr
, &grp_addr
,
683 pim
->vrf
->name
, ifp
->name
, &nh_addr
,
684 mod_val
, nh_iter
, pim
->ecmp_enable
);
695 /* This API is used to parse Registered address nexthop update coming from Zebra
697 int pim_parse_nexthop_update(ZAPI_CALLBACK_ARGS
)
699 struct nexthop
*nexthop
;
700 struct nexthop
*nhlist_head
= NULL
;
701 struct nexthop
*nhlist_tail
= NULL
;
704 struct pim_nexthop_cache
*pnc
= NULL
;
705 struct pim_neighbor
*nbr
= NULL
;
706 struct interface
*ifp
= NULL
;
707 struct interface
*ifp1
= NULL
;
708 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
709 struct pim_instance
*pim
;
710 struct zapi_route nhr
;
716 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &nhr
)) {
717 zlog_err("%s: Decode of nexthop update from zebra failed",
722 if (cmd
== ZEBRA_NEXTHOP_UPDATE
) {
723 prefix_copy(&rpf
.rpf_addr
, &nhr
.prefix
);
724 pnc
= pim_nexthop_cache_find(pim
, &rpf
);
726 if (PIM_DEBUG_PIM_NHT
)
728 "%s: Skipping NHT update, addr %pFX is not in local cached DB.",
729 __func__
, &rpf
.rpf_addr
);
734 * We do not currently handle ZEBRA_IMPORT_CHECK_UPDATE
739 pnc
->last_update
= pim_time_monotonic_usec();
741 if (nhr
.nexthop_num
) {
742 pnc
->nexthop_num
= 0; // Only increment for pim enabled rpf.
744 for (i
= 0; i
< nhr
.nexthop_num
; i
++) {
745 nexthop
= nexthop_from_zapi_nexthop(&nhr
.nexthops
[i
]);
746 switch (nexthop
->type
) {
747 case NEXTHOP_TYPE_IPV4
:
748 case NEXTHOP_TYPE_IPV4_IFINDEX
:
749 case NEXTHOP_TYPE_IPV6
:
750 case NEXTHOP_TYPE_BLACKHOLE
:
752 case NEXTHOP_TYPE_IFINDEX
:
754 * Connected route (i.e. no nexthop), use
755 * RPF address from nexthop cache (i.e.
756 * destination) as PIM nexthop.
759 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
761 pnc
->rpf
.rpf_addr
.u
.prefix4
;
763 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
765 pnc
->rpf
.rpf_addr
.u
.prefix6
;
768 case NEXTHOP_TYPE_IPV6_IFINDEX
:
769 ifp1
= if_lookup_by_index(nexthop
->ifindex
,
775 nbr
= pim_neighbor_find_if(ifp1
);
776 /* Overwrite with Nbr address as NH addr */
779 nexthop
->gate
.ipv4
= nbr
->source_addr
;
781 nexthop
->gate
.ipv6
= nbr
->source_addr
;
784 // Mark nexthop address to 0 until PIM
787 nexthop
->gate
.ipv4
= PIMADDR_ANY
;
789 nexthop
->gate
.ipv6
= PIMADDR_ANY
;
796 ifp
= if_lookup_by_index(nexthop
->ifindex
,
799 if (PIM_DEBUG_PIM_NHT
) {
800 char buf
[NEXTHOP_STRLEN
];
802 "%s: could not find interface for ifindex %d(%s) (addr %s)",
803 __func__
, nexthop
->ifindex
,
805 nexthop2str(nexthop
, buf
,
808 nexthop_free(nexthop
);
812 if (PIM_DEBUG_PIM_NHT
)
814 "%s: NHT addr %pFX(%s) %d-nhop via %pI4(%s) type %d distance:%u metric:%u ",
815 __func__
, &nhr
.prefix
, pim
->vrf
->name
,
816 i
+ 1, &nexthop
->gate
.ipv4
,
817 ifp
->name
, nexthop
->type
, nhr
.distance
,
822 * Though Multicast is not enabled on this
823 * Interface store it in database otheriwse we
824 * may miss this update and this will not cause
825 * any issue, because while choosing the path we
826 * are ommitting the Interfaces which are not
829 if (PIM_DEBUG_PIM_NHT
) {
830 char buf
[NEXTHOP_STRLEN
];
833 "%s: multicast not enabled on input interface %s(%s) (ifindex=%d, addr %s)",
837 nexthop2str(nexthop
, buf
,
843 nhlist_tail
->next
= nexthop
;
844 nhlist_tail
= nexthop
;
846 nhlist_tail
= nexthop
;
847 nhlist_head
= nexthop
;
849 // Only keep track of nexthops which are PIM enabled.
852 /* Reset existing pnc->nexthop before assigning new list */
853 nexthops_free(pnc
->nexthop
);
854 pnc
->nexthop
= nhlist_head
;
855 if (pnc
->nexthop_num
) {
856 pnc
->flags
|= PIM_NEXTHOP_VALID
;
857 pnc
->distance
= nhr
.distance
;
858 pnc
->metric
= nhr
.metric
;
861 pnc
->flags
&= ~PIM_NEXTHOP_VALID
;
862 pnc
->nexthop_num
= nhr
.nexthop_num
;
863 nexthops_free(pnc
->nexthop
);
866 SET_FLAG(pnc
->flags
, PIM_NEXTHOP_ANSWER_RECEIVED
);
868 if (PIM_DEBUG_PIM_NHT
)
870 "%s: NHT Update for %pFX(%s) num_nh %d num_pim_nh %d vrf:%u up %ld rp %d",
871 __func__
, &nhr
.prefix
, pim
->vrf
->name
, nhr
.nexthop_num
,
872 pnc
->nexthop_num
, vrf_id
, pnc
->upstream_hash
->count
,
873 listcount(pnc
->rp_list
));
875 pim_rpf_set_refresh_time(pim
);
877 if (listcount(pnc
->rp_list
))
878 pim_update_rp_nh(pim
, pnc
);
879 if (pnc
->upstream_hash
->count
)
880 pim_update_upstream_nh(pim
, pnc
);
885 int pim_ecmp_nexthop_lookup(struct pim_instance
*pim
,
886 struct pim_nexthop
*nexthop
, struct prefix
*src
,
887 struct prefix
*grp
, int neighbor_needed
)
889 struct pim_nexthop_cache
*pnc
;
890 struct pim_zlookup_nexthop nexthop_tab
[MULTIPATH_NUM
];
891 struct pim_neighbor
*nbrs
[MULTIPATH_NUM
], *nbr
= NULL
;
894 struct interface
*ifps
[MULTIPATH_NUM
], *ifp
;
898 uint32_t hash_val
= 0, mod_val
= 0;
899 uint32_t num_nbrs
= 0;
900 pim_addr src_addr
= pim_addr_from_prefix(src
);
902 if (PIM_DEBUG_PIM_NHT
)
903 zlog_debug("%s: Looking up: %pPA(%s), last lookup time: %lld",
904 __func__
, &src_addr
, pim
->vrf
->name
,
905 nexthop
->last_lookup_time
);
909 pnc
= pim_nexthop_cache_find(pim
, &rpf
);
911 if (CHECK_FLAG(pnc
->flags
, PIM_NEXTHOP_ANSWER_RECEIVED
))
912 return pim_ecmp_nexthop_search(pim
, pnc
, nexthop
, src
, grp
,
916 memset(nexthop_tab
, 0,
917 sizeof(struct pim_zlookup_nexthop
) * MULTIPATH_NUM
);
918 num_ifindex
= zclient_lookup_nexthop(pim
, nexthop_tab
, MULTIPATH_NUM
,
919 src_addr
, PIM_NEXTHOP_LOOKUP_MAX
);
920 if (num_ifindex
< 1) {
921 if (PIM_DEBUG_PIM_NHT
)
923 "%s: could not find nexthop ifindex for address %pPA(%s)",
924 __func__
, &src_addr
, pim
->vrf
->name
);
928 memset(&nbrs
, 0, sizeof(nbrs
));
929 memset(&ifps
, 0, sizeof(ifps
));
932 * Look up all interfaces and neighbors,
933 * store for later usage
935 for (i
= 0; i
< num_ifindex
; i
++) {
936 ifps
[i
] = if_lookup_by_index(nexthop_tab
[i
].ifindex
,
939 nbrs
[i
] = pim_neighbor_find_prefix(
940 ifps
[i
], &nexthop_tab
[i
].nexthop_addr
);
942 pim_if_connected_to_source(ifps
[i
], src_addr
))
947 // If PIM ECMP enable then choose ECMP path.
948 if (pim
->ecmp_enable
) {
949 uint32_t consider
= num_ifindex
;
951 if (neighbor_needed
&& num_nbrs
< consider
)
957 hash_val
= pim_compute_ecmp_hash(src
, grp
);
958 mod_val
= hash_val
% consider
;
959 if (PIM_DEBUG_PIM_NHT_DETAIL
)
960 zlog_debug("%s: hash_val %u mod_val %u", __func__
,
965 while (!found
&& (i
< num_ifindex
)) {
966 first_ifindex
= nexthop_tab
[i
].ifindex
;
970 if (PIM_DEBUG_PIM_NHT
)
972 "%s %s: could not find interface for ifindex %d (address %pPA(%s))",
973 __FILE__
, __func__
, first_ifindex
,
974 &src_addr
, pim
->vrf
->name
);
982 if (PIM_DEBUG_PIM_NHT
)
984 "%s: multicast not enabled on input interface %s(%s) (ifindex=%d, RPF for source %pPA)",
985 __func__
, ifp
->name
, pim
->vrf
->name
,
986 first_ifindex
, &src_addr
);
992 if (neighbor_needed
&&
993 !pim_if_connected_to_source(ifp
, src_addr
)) {
995 if (PIM_DEBUG_PIM_NHT_DETAIL
)
996 zlog_debug("ifp name: %s(%s), pim nbr: %p",
997 ifp
->name
, pim
->vrf
->name
, nbr
);
998 if (!nbr
&& !if_is_loopback(ifp
)) {
1002 if (PIM_DEBUG_PIM_NHT
)
1004 "%s: NBR not found on input interface %s(%s) (RPF for source %pPA)",
1005 __func__
, ifp
->name
,
1006 pim
->vrf
->name
, &src_addr
);
1012 if (PIM_DEBUG_PIM_NHT
) {
1013 char nexthop_str
[PREFIX_STRLEN
];
1015 pim_addr_dump("<nexthop?>",
1016 &nexthop_tab
[i
].nexthop_addr
,
1017 nexthop_str
, sizeof(nexthop_str
));
1019 "%s: found nhop %s for addr %pPA interface %s(%s) metric %d dist %d",
1020 __func__
, nexthop_str
, &src_addr
,
1021 ifp
->name
, pim
->vrf
->name
,
1022 nexthop_tab
[i
].route_metric
,
1023 nexthop_tab
[i
].protocol_distance
);
1025 /* update nexthop data */
1026 nexthop
->interface
= ifp
;
1027 nexthop
->mrib_nexthop_addr
=
1028 nexthop_tab
[i
].nexthop_addr
;
1029 nexthop
->mrib_metric_preference
=
1030 nexthop_tab
[i
].protocol_distance
;
1031 nexthop
->mrib_route_metric
=
1032 nexthop_tab
[i
].route_metric
;
1033 nexthop
->last_lookup
= src_addr
;
1034 nexthop
->last_lookup_time
= pim_time_monotonic_usec();
1047 int pim_ecmp_fib_lookup_if_vif_index(struct pim_instance
*pim
,
1048 struct prefix
*src
, struct prefix
*grp
)
1050 struct pim_nexthop nhop
;
1055 if (PIM_DEBUG_PIM_NHT
) {
1056 src_addr
= pim_addr_from_prefix(src
);
1059 memset(&nhop
, 0, sizeof(nhop
));
1060 if (!pim_ecmp_nexthop_lookup(pim
, &nhop
, src
, grp
, 1)) {
1061 if (PIM_DEBUG_PIM_NHT
)
1063 "%s: could not find nexthop ifindex for address %pPA(%s)",
1064 __func__
, &src_addr
, pim
->vrf
->name
);
1068 ifindex
= nhop
.interface
->ifindex
;
1069 if (PIM_DEBUG_PIM_NHT
)
1071 "%s: found nexthop ifindex=%d (interface %s(%s)) for address %pPA",
1073 ifindex2ifname(ifindex
, pim
->vrf
->vrf_id
),
1074 pim
->vrf
->name
, &src_addr
);
1076 vif_index
= pim_if_find_vifindex_by_ifindex(pim
, ifindex
);
1078 if (vif_index
< 0) {
1079 if (PIM_DEBUG_PIM_NHT
) {
1081 "%s: low vif_index=%d(%s) < 1 nexthop for address %pPA",
1082 __func__
, vif_index
, pim
->vrf
->name
, &src_addr
);