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"
47 * pim_sendmsg_zebra_rnh -- Format and send a nexthop register/Unregister
50 void pim_sendmsg_zebra_rnh(struct pim_instance
*pim
, struct zclient
*zclient
,
51 struct pim_nexthop_cache
*pnc
, int command
)
56 p
= &(pnc
->rpf
.rpf_addr
);
57 ret
= zclient_send_rnh(zclient
, command
, p
, false, false,
59 if (ret
== ZCLIENT_SEND_FAILURE
)
60 zlog_warn("sendmsg_nexthop: zclient_send_message() failed");
62 if (PIM_DEBUG_PIM_NHT
)
64 "%s: NHT %sregistered addr %pFX(%s) with Zebra ret:%d ",
66 (command
== ZEBRA_NEXTHOP_REGISTER
) ? " " : "de", p
,
72 struct pim_nexthop_cache
*pim_nexthop_cache_find(struct pim_instance
*pim
,
75 struct pim_nexthop_cache
*pnc
= NULL
;
76 struct pim_nexthop_cache lookup
;
78 lookup
.rpf
.rpf_addr
.family
= rpf
->rpf_addr
.family
;
79 lookup
.rpf
.rpf_addr
.prefixlen
= rpf
->rpf_addr
.prefixlen
;
80 lookup
.rpf
.rpf_addr
.u
.prefix4
.s_addr
= rpf
->rpf_addr
.u
.prefix4
.s_addr
;
82 pnc
= hash_lookup(pim
->rpf_hash
, &lookup
);
87 static struct pim_nexthop_cache
*pim_nexthop_cache_add(struct pim_instance
*pim
,
88 struct pim_rpf
*rpf_addr
)
90 struct pim_nexthop_cache
*pnc
;
93 pnc
= XCALLOC(MTYPE_PIM_NEXTHOP_CACHE
,
94 sizeof(struct pim_nexthop_cache
));
95 pnc
->rpf
.rpf_addr
.family
= rpf_addr
->rpf_addr
.family
;
96 pnc
->rpf
.rpf_addr
.prefixlen
= rpf_addr
->rpf_addr
.prefixlen
;
97 pnc
->rpf
.rpf_addr
.u
.prefix4
.s_addr
=
98 rpf_addr
->rpf_addr
.u
.prefix4
.s_addr
;
100 pnc
= hash_get(pim
->rpf_hash
, pnc
, hash_alloc_intern
);
102 pnc
->rp_list
= list_new();
103 pnc
->rp_list
->cmp
= pim_rp_list_cmp
;
105 snprintfrr(hash_name
, sizeof(hash_name
), "PNC %pFX(%s) Upstream Hash",
106 &pnc
->rpf
.rpf_addr
, pim
->vrf
->name
);
107 pnc
->upstream_hash
= hash_create_size(8192, pim_upstream_hash_key
,
108 pim_upstream_equal
, hash_name
);
113 static struct pim_nexthop_cache
*pim_nht_get(struct pim_instance
*pim
,
116 struct pim_nexthop_cache
*pnc
= NULL
;
118 struct zclient
*zclient
= NULL
;
120 zclient
= pim_zebra_zclient_get();
121 memset(&rpf
, 0, sizeof(struct pim_rpf
));
122 rpf
.rpf_addr
.family
= addr
->family
;
123 rpf
.rpf_addr
.prefixlen
= addr
->prefixlen
;
124 rpf
.rpf_addr
.u
.prefix4
= addr
->u
.prefix4
;
126 pnc
= pim_nexthop_cache_find(pim
, &rpf
);
128 pnc
= pim_nexthop_cache_add(pim
, &rpf
);
129 pim_sendmsg_zebra_rnh(pim
, zclient
, pnc
,
130 ZEBRA_NEXTHOP_REGISTER
);
131 if (PIM_DEBUG_PIM_NHT
)
133 "%s: NHT cache and zebra notification added for %pFX(%s)",
134 __func__
, addr
, pim
->vrf
->name
);
140 /* TBD: this does several distinct things and should probably be split up.
141 * (checking state vs. returning pnc vs. adding upstream vs. adding rp)
143 int pim_find_or_track_nexthop(struct pim_instance
*pim
, struct prefix
*addr
,
144 struct pim_upstream
*up
, struct rp_info
*rp
,
145 struct pim_nexthop_cache
*out_pnc
)
147 struct pim_nexthop_cache
*pnc
;
148 struct listnode
*ch_node
= NULL
;
150 pnc
= pim_nht_get(pim
, addr
);
152 assertf(up
|| rp
, "addr=%pFX", addr
);
155 ch_node
= listnode_lookup(pnc
->rp_list
, rp
);
157 listnode_add_sort(pnc
->rp_list
, rp
);
161 hash_get(pnc
->upstream_hash
, up
, hash_alloc_intern
);
163 if (CHECK_FLAG(pnc
->flags
, PIM_NEXTHOP_VALID
)) {
165 memcpy(out_pnc
, pnc
, sizeof(struct pim_nexthop_cache
));
172 void pim_nht_bsr_add(struct pim_instance
*pim
, struct in_addr addr
)
174 struct pim_nexthop_cache
*pnc
;
177 pfx
.family
= AF_INET
;
178 pfx
.prefixlen
= IPV4_MAX_BITLEN
;
179 pfx
.u
.prefix4
= addr
;
181 pnc
= pim_nht_get(pim
, &pfx
);
186 static void pim_nht_drop_maybe(struct pim_instance
*pim
,
187 struct pim_nexthop_cache
*pnc
)
189 if (PIM_DEBUG_PIM_NHT
)
191 "%s: NHT %pFX(%s) rp_list count:%d upstream count:%ld BSR count:%u",
192 __func__
, &pnc
->rpf
.rpf_addr
, pim
->vrf
->name
,
193 pnc
->rp_list
->count
, pnc
->upstream_hash
->count
,
196 if (pnc
->rp_list
->count
== 0 && pnc
->upstream_hash
->count
== 0
197 && pnc
->bsr_count
== 0) {
198 struct zclient
*zclient
= pim_zebra_zclient_get();
200 pim_sendmsg_zebra_rnh(pim
, zclient
, pnc
,
201 ZEBRA_NEXTHOP_UNREGISTER
);
203 list_delete(&pnc
->rp_list
);
204 hash_free(pnc
->upstream_hash
);
206 hash_release(pim
->rpf_hash
, pnc
);
208 nexthops_free(pnc
->nexthop
);
209 XFREE(MTYPE_PIM_NEXTHOP_CACHE
, pnc
);
213 void pim_delete_tracked_nexthop(struct pim_instance
*pim
, struct prefix
*addr
,
214 struct pim_upstream
*up
, struct rp_info
*rp
)
216 struct pim_nexthop_cache
*pnc
= NULL
;
217 struct pim_nexthop_cache lookup
;
218 struct pim_upstream
*upstream
= NULL
;
220 /* Remove from RPF hash if it is the last entry */
221 lookup
.rpf
.rpf_addr
= *addr
;
222 pnc
= hash_lookup(pim
->rpf_hash
, &lookup
);
224 zlog_warn("attempting to delete nonexistent NHT entry %pFX",
230 /* Release the (*, G)upstream from pnc->upstream_hash,
231 * whose Group belongs to the RP getting deleted
233 frr_each (rb_pim_upstream
, &pim
->upstream_head
, upstream
) {
235 struct rp_info
*trp_info
;
237 if (upstream
->sg
.src
.s_addr
!= INADDR_ANY
)
240 grp
.family
= AF_INET
;
241 grp
.prefixlen
= IPV4_MAX_BITLEN
;
242 grp
.u
.prefix4
= upstream
->sg
.grp
;
244 trp_info
= pim_rp_find_match_group(pim
, &grp
);
246 hash_release(pnc
->upstream_hash
, upstream
);
248 listnode_delete(pnc
->rp_list
, rp
);
252 hash_release(pnc
->upstream_hash
, up
);
254 pim_nht_drop_maybe(pim
, pnc
);
257 void pim_nht_bsr_del(struct pim_instance
*pim
, struct in_addr addr
)
259 struct pim_nexthop_cache
*pnc
= NULL
;
260 struct pim_nexthop_cache lookup
;
262 lookup
.rpf
.rpf_addr
.family
= AF_INET
;
263 lookup
.rpf
.rpf_addr
.prefixlen
= IPV4_MAX_BITLEN
;
264 lookup
.rpf
.rpf_addr
.u
.prefix4
= addr
;
266 pnc
= hash_lookup(pim
->rpf_hash
, &lookup
);
269 zlog_warn("attempting to delete nonexistent NHT BSR entry %pI4",
274 assertf(pnc
->bsr_count
> 0, "addr=%pI4", &addr
);
277 pim_nht_drop_maybe(pim
, pnc
);
280 bool pim_nht_bsr_rpf_check(struct pim_instance
*pim
, struct in_addr bsr_addr
,
281 struct interface
*src_ifp
, struct in_addr src_ip
)
283 struct pim_nexthop_cache
*pnc
= NULL
;
284 struct pim_nexthop_cache lookup
;
285 struct pim_neighbor
*nbr
= NULL
;
287 struct interface
*ifp
;
289 lookup
.rpf
.rpf_addr
.family
= AF_INET
;
290 lookup
.rpf
.rpf_addr
.prefixlen
= IPV4_MAX_BITLEN
;
291 lookup
.rpf
.rpf_addr
.u
.prefix4
= bsr_addr
;
293 pnc
= hash_lookup(pim
->rpf_hash
, &lookup
);
294 if (!pnc
|| !CHECK_FLAG(pnc
->flags
, PIM_NEXTHOP_ANSWER_RECEIVED
)) {
295 /* BSM from a new freshly registered BSR - do a synchronous
296 * zebra query since otherwise we'd drop the first packet,
297 * leading to additional delay in picking up BSM data
300 /* FIXME: this should really be moved into a generic NHT
301 * function that does "add and get immediate result" or maybe
302 * "check cache or get immediate result." But until that can
303 * be worked in, here's a copy of the code below :(
305 struct pim_zlookup_nexthop nexthop_tab
[MULTIPATH_NUM
];
307 struct interface
*ifp
= NULL
;
310 memset(nexthop_tab
, 0, sizeof(nexthop_tab
));
311 num_ifindex
= zclient_lookup_nexthop(pim
, nexthop_tab
,
312 MULTIPATH_NUM
, bsr_addr
,
313 PIM_NEXTHOP_LOOKUP_MAX
);
315 if (num_ifindex
<= 0)
318 for (i
= 0; i
< num_ifindex
; i
++) {
319 struct pim_zlookup_nexthop
*znh
= &nexthop_tab
[i
];
321 /* pim_zlookup_nexthop has no ->type */
323 /* 1:1 match code below with znh instead of nh */
324 ifp
= if_lookup_by_index(znh
->ifindex
,
327 if (!ifp
|| !ifp
->info
)
330 if (if_is_loopback(ifp
) && if_is_loopback(src_ifp
))
333 nbr
= pim_neighbor_find(ifp
,
334 znh
->nexthop_addr
.u
.prefix4
);
338 return znh
->ifindex
== src_ifp
->ifindex
339 && znh
->nexthop_addr
.u
.prefix4
.s_addr
345 if (!CHECK_FLAG(pnc
->flags
, PIM_NEXTHOP_VALID
))
348 /* if we accept BSMs from more than one ECMP nexthop, this will cause
349 * BSM message "multiplication" for each ECMP hop. i.e. if you have
350 * 4-way ECMP and 4 hops you end up with 256 copies of each BSM
353 * so... only accept the first (IPv4) valid nexthop as source.
356 for (nh
= pnc
->nexthop
; nh
; nh
= nh
->next
) {
357 struct in_addr nhaddr
;
360 case NEXTHOP_TYPE_IPV4
:
361 if (nh
->ifindex
== IFINDEX_INTERNAL
)
365 case NEXTHOP_TYPE_IPV4_IFINDEX
:
366 nhaddr
= nh
->gate
.ipv4
;
369 case NEXTHOP_TYPE_IFINDEX
:
377 ifp
= if_lookup_by_index(nh
->ifindex
, pim
->vrf
->vrf_id
);
378 if (!ifp
|| !ifp
->info
)
381 if (if_is_loopback(ifp
) && if_is_loopback(src_ifp
))
384 /* MRIB (IGP) may be pointing at a router where PIM is down */
385 nbr
= pim_neighbor_find(ifp
, nhaddr
);
389 return nh
->ifindex
== src_ifp
->ifindex
390 && nhaddr
.s_addr
== src_ip
.s_addr
;
395 void pim_rp_nexthop_del(struct rp_info
*rp_info
)
397 rp_info
->rp
.source_nexthop
.interface
= NULL
;
398 rp_info
->rp
.source_nexthop
.mrib_nexthop_addr
.u
.prefix4
.s_addr
=
400 rp_info
->rp
.source_nexthop
.mrib_metric_preference
=
401 router
->infinite_assert_metric
.metric_preference
;
402 rp_info
->rp
.source_nexthop
.mrib_route_metric
=
403 router
->infinite_assert_metric
.route_metric
;
406 /* Update RP nexthop info based on Nexthop update received from Zebra.*/
407 static void pim_update_rp_nh(struct pim_instance
*pim
,
408 struct pim_nexthop_cache
*pnc
)
410 struct listnode
*node
= NULL
;
411 struct rp_info
*rp_info
= NULL
;
413 /*Traverse RP list and update each RP Nexthop info */
414 for (ALL_LIST_ELEMENTS_RO(pnc
->rp_list
, node
, rp_info
)) {
415 if (rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
== INADDR_NONE
)
418 // Compute PIM RPF using cached nexthop
419 if (!pim_ecmp_nexthop_lookup(pim
, &rp_info
->rp
.source_nexthop
,
420 &rp_info
->rp
.rpf_addr
,
422 pim_rp_nexthop_del(rp_info
);
426 /* Update Upstream nexthop info based on Nexthop update received from Zebra.*/
427 static int pim_update_upstream_nh_helper(struct hash_bucket
*bucket
, void *arg
)
429 struct pim_instance
*pim
= (struct pim_instance
*)arg
;
430 struct pim_upstream
*up
= (struct pim_upstream
*)bucket
->data
;
432 enum pim_rpf_result rpf_result
;
435 old
.source_nexthop
.interface
= up
->rpf
.source_nexthop
.interface
;
436 rpf_result
= pim_rpf_update(pim
, up
, &old
, __func__
);
438 /* update kernel multicast forwarding cache (MFC); if the
439 * RPF nbr is now unreachable the MFC has already been updated
442 if (rpf_result
!= PIM_RPF_FAILURE
)
443 pim_upstream_mroute_iif_update(up
->channel_oil
, __func__
);
445 if (rpf_result
== PIM_RPF_CHANGED
||
446 (rpf_result
== PIM_RPF_FAILURE
&& old
.source_nexthop
.interface
))
447 pim_zebra_upstream_rpf_changed(pim
, up
, &old
);
450 if (PIM_DEBUG_PIM_NHT
) {
452 "%s: NHT upstream %s(%s) old ifp %s new ifp %s",
453 __func__
, up
->sg_str
, pim
->vrf
->name
,
454 old
.source_nexthop
.interface
? old
.source_nexthop
457 up
->rpf
.source_nexthop
.interface
? up
->rpf
.source_nexthop
462 return HASHWALK_CONTINUE
;
465 static int pim_update_upstream_nh(struct pim_instance
*pim
,
466 struct pim_nexthop_cache
*pnc
)
468 hash_walk(pnc
->upstream_hash
, pim_update_upstream_nh_helper
, pim
);
470 pim_zebra_update_all_interfaces(pim
);
475 uint32_t pim_compute_ecmp_hash(struct prefix
*src
, struct prefix
*grp
)
478 uint32_t s
= 0, g
= 0;
483 switch (src
->family
) {
485 s
= src
->u
.prefix4
.s_addr
;
488 g
= grp
->u
.prefix4
.s_addr
;
494 hash_val
= jhash_2words(g
, s
, 101);
498 static int pim_ecmp_nexthop_search(struct pim_instance
*pim
,
499 struct pim_nexthop_cache
*pnc
,
500 struct pim_nexthop
*nexthop
,
501 struct prefix
*src
, struct prefix
*grp
,
504 struct pim_neighbor
*nbrs
[MULTIPATH_NUM
], *nbr
= NULL
;
505 struct interface
*ifps
[MULTIPATH_NUM
];
506 struct nexthop
*nh_node
= NULL
;
507 ifindex_t first_ifindex
;
508 struct interface
*ifp
= NULL
;
509 uint32_t hash_val
= 0, mod_val
= 0;
510 uint8_t nh_iter
= 0, found
= 0;
511 uint32_t i
, num_nbrs
= 0;
513 if (!pnc
|| !pnc
->nexthop_num
|| !nexthop
)
516 memset(&nbrs
, 0, sizeof(nbrs
));
517 memset(&ifps
, 0, sizeof(ifps
));
519 // Current Nexthop is VALID, check to stay on the current path.
520 if (nexthop
->interface
&& nexthop
->interface
->info
521 && nexthop
->mrib_nexthop_addr
.u
.prefix4
.s_addr
522 != PIM_NET_INADDR_ANY
) {
523 /* User configured knob to explicitly switch
524 to new path is disabled or current path
525 metric is less than nexthop update.
528 if (pim
->ecmp_rebalance_enable
== 0) {
529 uint8_t curr_route_valid
= 0;
530 // Check if current nexthop is present in new updated
532 // If the current nexthop is not valid, candidate to
533 // choose new Nexthop.
534 for (nh_node
= pnc
->nexthop
; nh_node
;
535 nh_node
= nh_node
->next
) {
536 curr_route_valid
= (nexthop
->interface
->ifindex
537 == nh_node
->ifindex
);
538 if (curr_route_valid
)
543 && !pim_if_connected_to_source(nexthop
->interface
,
545 nbr
= pim_neighbor_find(
547 nexthop
->mrib_nexthop_addr
.u
.prefix4
);
549 && !if_is_loopback(nexthop
->interface
)) {
550 if (PIM_DEBUG_PIM_NHT
)
552 "%s: current nexthop does not have nbr ",
555 /* update metric even if the upstream
556 * neighbor stays unchanged
558 nexthop
->mrib_metric_preference
=
560 nexthop
->mrib_route_metric
=
562 if (PIM_DEBUG_PIM_NHT
) {
563 char src_str
[INET_ADDRSTRLEN
];
564 pim_inet4_dump("<addr?>",
568 char grp_str
[INET_ADDRSTRLEN
];
569 pim_inet4_dump("<addr?>",
574 "%s: (%s,%s)(%s) current nexthop %s is valid, skipping new path selection",
576 grp_str
, pim
->vrf
->name
,
577 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
);
594 nbrs
[i
] = pim_neighbor_find(ifps
[i
],
596 if (nbrs
[i
] || pim_if_connected_to_source(ifps
[i
],
602 if (pim
->ecmp_enable
) {
603 uint32_t consider
= pnc
->nexthop_num
;
605 if (neighbor_needed
&& num_nbrs
< consider
)
611 // PIM ECMP flag is enable then choose ECMP path.
612 hash_val
= pim_compute_ecmp_hash(src
, grp
);
613 mod_val
= hash_val
% consider
;
616 for (nh_node
= pnc
->nexthop
; nh_node
&& (found
== 0);
617 nh_node
= nh_node
->next
) {
618 first_ifindex
= nh_node
->ifindex
;
621 if (PIM_DEBUG_PIM_NHT
) {
622 char addr_str
[INET_ADDRSTRLEN
];
623 pim_inet4_dump("<addr?>", src
->u
.prefix4
,
624 addr_str
, sizeof(addr_str
));
626 "%s %s: could not find interface for ifindex %d (address %s(%s))",
627 __FILE__
, __func__
, first_ifindex
,
628 addr_str
, pim
->vrf
->name
);
630 if (nh_iter
== mod_val
)
631 mod_val
++; // Select nexthpath
636 if (PIM_DEBUG_PIM_NHT
) {
637 char addr_str
[INET_ADDRSTRLEN
];
638 pim_inet4_dump("<addr?>", src
->u
.prefix4
,
639 addr_str
, sizeof(addr_str
));
641 "%s: multicast not enabled on input interface %s(%s) (ifindex=%d, RPF for source %s)",
642 __func__
, ifp
->name
, pim
->vrf
->name
,
643 first_ifindex
, addr_str
);
645 if (nh_iter
== mod_val
)
646 mod_val
++; // Select nexthpath
652 && !pim_if_connected_to_source(ifp
, src
->u
.prefix4
)) {
654 if (!nbr
&& !if_is_loopback(ifp
)) {
655 if (PIM_DEBUG_PIM_NHT
)
657 "%s: pim nbr not found on input interface %s(%s)",
660 if (nh_iter
== mod_val
)
661 mod_val
++; // Select nexthpath
667 if (nh_iter
== mod_val
) {
668 nexthop
->interface
= ifp
;
669 nexthop
->mrib_nexthop_addr
.family
= AF_INET
;
670 nexthop
->mrib_nexthop_addr
.prefixlen
= IPV4_MAX_BITLEN
;
671 nexthop
->mrib_nexthop_addr
.u
.prefix4
=
673 nexthop
->mrib_metric_preference
= pnc
->distance
;
674 nexthop
->mrib_route_metric
= pnc
->metric
;
675 nexthop
->last_lookup
= src
->u
.prefix4
;
676 nexthop
->last_lookup_time
= pim_time_monotonic_usec();
679 if (PIM_DEBUG_PIM_NHT
) {
680 char buf
[INET_ADDRSTRLEN
];
681 char buf2
[INET_ADDRSTRLEN
];
682 char buf3
[INET_ADDRSTRLEN
];
683 pim_inet4_dump("<src?>", src
->u
.prefix4
, buf2
,
685 pim_inet4_dump("<grp?>", grp
->u
.prefix4
, buf3
,
689 nexthop
->mrib_nexthop_addr
.u
.prefix4
,
692 "%s: (%s,%s)(%s) selected nhop interface %s addr %s mod_val %u iter %d ecmp %d",
693 __func__
, buf2
, buf3
, pim
->vrf
->name
,
694 ifp
->name
, buf
, mod_val
, nh_iter
,
707 /* This API is used to parse Registered address nexthop update coming from Zebra
709 int pim_parse_nexthop_update(ZAPI_CALLBACK_ARGS
)
711 struct nexthop
*nexthop
;
712 struct nexthop
*nhlist_head
= NULL
;
713 struct nexthop
*nhlist_tail
= NULL
;
716 struct pim_nexthop_cache
*pnc
= NULL
;
717 struct pim_neighbor
*nbr
= NULL
;
718 struct interface
*ifp
= NULL
;
719 struct interface
*ifp1
= NULL
;
720 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
721 struct pim_instance
*pim
;
722 struct zapi_route nhr
;
728 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &nhr
)) {
729 zlog_err("%s: Decode of nexthop update from zebra failed",
734 if (cmd
== ZEBRA_NEXTHOP_UPDATE
) {
735 prefix_copy(&rpf
.rpf_addr
, &nhr
.prefix
);
736 pnc
= pim_nexthop_cache_find(pim
, &rpf
);
738 if (PIM_DEBUG_PIM_NHT
)
740 "%s: Skipping NHT update, addr %pFX is not in local cached DB.",
741 __func__
, &rpf
.rpf_addr
);
746 * We do not currently handle ZEBRA_IMPORT_CHECK_UPDATE
751 pnc
->last_update
= pim_time_monotonic_usec();
753 if (nhr
.nexthop_num
) {
754 pnc
->nexthop_num
= 0; // Only increment for pim enabled rpf.
756 for (i
= 0; i
< nhr
.nexthop_num
; i
++) {
757 nexthop
= nexthop_from_zapi_nexthop(&nhr
.nexthops
[i
]);
758 switch (nexthop
->type
) {
759 case NEXTHOP_TYPE_IPV4
:
760 case NEXTHOP_TYPE_IPV4_IFINDEX
:
761 case NEXTHOP_TYPE_IPV6
:
762 case NEXTHOP_TYPE_BLACKHOLE
:
764 case NEXTHOP_TYPE_IFINDEX
:
766 * Connected route (i.e. no nexthop), use
767 * RPF address from nexthop cache (i.e.
768 * destination) as PIM nexthop.
770 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
772 pnc
->rpf
.rpf_addr
.u
.prefix4
;
774 case NEXTHOP_TYPE_IPV6_IFINDEX
:
775 ifp1
= if_lookup_by_index(nexthop
->ifindex
,
781 nbr
= pim_neighbor_find_if(ifp1
);
782 /* Overwrite with Nbr address as NH addr */
784 nexthop
->gate
.ipv4
= nbr
->source_addr
;
786 // Mark nexthop address to 0 until PIM
788 nexthop
->gate
.ipv4
.s_addr
=
795 ifp
= if_lookup_by_index(nexthop
->ifindex
,
798 if (PIM_DEBUG_PIM_NHT
) {
799 char buf
[NEXTHOP_STRLEN
];
801 "%s: could not find interface for ifindex %d(%s) (addr %s)",
802 __func__
, nexthop
->ifindex
,
804 nexthop2str(nexthop
, buf
,
807 nexthop_free(nexthop
);
811 if (PIM_DEBUG_PIM_NHT
)
813 "%s: NHT addr %pFX(%s) %d-nhop via %pI4(%s) type %d distance:%u metric:%u ",
814 __func__
, &nhr
.prefix
, pim
->vrf
->name
,
815 i
+ 1, &nexthop
->gate
.ipv4
,
816 ifp
->name
, nexthop
->type
, nhr
.distance
,
821 * Though Multicast is not enabled on this
822 * Interface store it in database otheriwse we
823 * may miss this update and this will not cause
824 * any issue, because while choosing the path we
825 * are ommitting the Interfaces which are not
828 if (PIM_DEBUG_PIM_NHT
) {
829 char buf
[NEXTHOP_STRLEN
];
832 "%s: multicast not enabled on input interface %s(%s) (ifindex=%d, addr %s)",
836 nexthop2str(nexthop
, buf
,
842 nhlist_tail
->next
= nexthop
;
843 nhlist_tail
= nexthop
;
845 nhlist_tail
= nexthop
;
846 nhlist_head
= nexthop
;
848 // Only keep track of nexthops which are PIM enabled.
851 /* Reset existing pnc->nexthop before assigning new list */
852 nexthops_free(pnc
->nexthop
);
853 pnc
->nexthop
= nhlist_head
;
854 if (pnc
->nexthop_num
) {
855 pnc
->flags
|= PIM_NEXTHOP_VALID
;
856 pnc
->distance
= nhr
.distance
;
857 pnc
->metric
= nhr
.metric
;
860 pnc
->flags
&= ~PIM_NEXTHOP_VALID
;
861 pnc
->nexthop_num
= nhr
.nexthop_num
;
862 nexthops_free(pnc
->nexthop
);
865 SET_FLAG(pnc
->flags
, PIM_NEXTHOP_ANSWER_RECEIVED
);
867 if (PIM_DEBUG_PIM_NHT
)
869 "%s: NHT Update for %pFX(%s) num_nh %d num_pim_nh %d vrf:%u up %ld rp %d",
870 __func__
, &nhr
.prefix
, pim
->vrf
->name
, nhr
.nexthop_num
,
871 pnc
->nexthop_num
, vrf_id
, pnc
->upstream_hash
->count
,
872 listcount(pnc
->rp_list
));
874 pim_rpf_set_refresh_time(pim
);
876 if (listcount(pnc
->rp_list
))
877 pim_update_rp_nh(pim
, pnc
);
878 if (pnc
->upstream_hash
->count
)
879 pim_update_upstream_nh(pim
, pnc
);
884 int pim_ecmp_nexthop_lookup(struct pim_instance
*pim
,
885 struct pim_nexthop
*nexthop
, struct prefix
*src
,
886 struct prefix
*grp
, int neighbor_needed
)
888 struct pim_nexthop_cache
*pnc
;
889 struct pim_zlookup_nexthop nexthop_tab
[MULTIPATH_NUM
];
890 struct pim_neighbor
*nbrs
[MULTIPATH_NUM
], *nbr
= NULL
;
893 struct interface
*ifps
[MULTIPATH_NUM
], *ifp
;
897 uint32_t hash_val
= 0, mod_val
= 0;
898 uint32_t num_nbrs
= 0;
899 char addr_str
[PREFIX_STRLEN
];
901 if (PIM_DEBUG_PIM_NHT
) {
902 pim_inet4_dump("<addr?>", src
->u
.prefix4
, addr_str
,
904 zlog_debug("%s: Looking up: %s(%s), last lookup time: %lld",
905 __func__
, addr_str
, pim
->vrf
->name
,
906 nexthop
->last_lookup_time
);
909 memset(&rpf
, 0, sizeof(struct pim_rpf
));
910 rpf
.rpf_addr
.family
= AF_INET
;
911 rpf
.rpf_addr
.prefixlen
= IPV4_MAX_BITLEN
;
912 rpf
.rpf_addr
.u
.prefix4
= src
->u
.prefix4
;
914 pnc
= pim_nexthop_cache_find(pim
, &rpf
);
916 if (CHECK_FLAG(pnc
->flags
, PIM_NEXTHOP_ANSWER_RECEIVED
))
917 return pim_ecmp_nexthop_search(pim
, pnc
, nexthop
, src
, grp
,
921 memset(nexthop_tab
, 0,
922 sizeof(struct pim_zlookup_nexthop
) * MULTIPATH_NUM
);
924 zclient_lookup_nexthop(pim
, nexthop_tab
, MULTIPATH_NUM
,
925 src
->u
.prefix4
, PIM_NEXTHOP_LOOKUP_MAX
);
926 if (num_ifindex
< 1) {
927 if (PIM_DEBUG_PIM_NHT
)
929 "%s: could not find nexthop ifindex for address %s(%s)",
930 __func__
, addr_str
, pim
->vrf
->name
);
934 memset(&nbrs
, 0, sizeof(nbrs
));
935 memset(&ifps
, 0, sizeof(ifps
));
938 * Look up all interfaces and neighbors,
939 * store for later usage
941 for (i
= 0; i
< num_ifindex
; i
++) {
942 ifps
[i
] = if_lookup_by_index(nexthop_tab
[i
].ifindex
,
945 nbrs
[i
] = pim_neighbor_find(
946 ifps
[i
], nexthop_tab
[i
].nexthop_addr
.u
.prefix4
);
948 || pim_if_connected_to_source(ifps
[i
],
954 // If PIM ECMP enable then choose ECMP path.
955 if (pim
->ecmp_enable
) {
956 uint32_t consider
= num_ifindex
;
958 if (neighbor_needed
&& num_nbrs
< consider
)
964 hash_val
= pim_compute_ecmp_hash(src
, grp
);
965 mod_val
= hash_val
% consider
;
966 if (PIM_DEBUG_PIM_NHT_DETAIL
)
967 zlog_debug("%s: hash_val %u mod_val %u", __func__
,
972 while (!found
&& (i
< num_ifindex
)) {
973 first_ifindex
= nexthop_tab
[i
].ifindex
;
977 if (PIM_DEBUG_PIM_NHT
)
979 "%s %s: could not find interface for ifindex %d (address %s(%s))",
980 __FILE__
, __func__
, first_ifindex
,
981 addr_str
, pim
->vrf
->name
);
989 if (PIM_DEBUG_PIM_NHT
)
991 "%s: multicast not enabled on input interface %s(%s) (ifindex=%d, RPF for source %s)",
992 __func__
, ifp
->name
, pim
->vrf
->name
,
993 first_ifindex
, addr_str
);
1000 && !pim_if_connected_to_source(ifp
, src
->u
.prefix4
)) {
1002 if (PIM_DEBUG_PIM_NHT_DETAIL
)
1003 zlog_debug("ifp name: %s(%s), pim nbr: %p",
1004 ifp
->name
, pim
->vrf
->name
, nbr
);
1005 if (!nbr
&& !if_is_loopback(ifp
)) {
1009 if (PIM_DEBUG_PIM_NHT
)
1011 "%s: NBR not found on input interface %s(%s) (RPF for source %s)",
1012 __func__
, ifp
->name
,
1013 pim
->vrf
->name
, addr_str
);
1019 if (PIM_DEBUG_PIM_NHT
) {
1020 char nexthop_str
[PREFIX_STRLEN
];
1022 pim_addr_dump("<nexthop?>",
1023 &nexthop_tab
[i
].nexthop_addr
,
1024 nexthop_str
, sizeof(nexthop_str
));
1026 "%s: found nhop %s for addr %s interface %s(%s) metric %d dist %d",
1027 __func__
, nexthop_str
, addr_str
,
1028 ifp
->name
, pim
->vrf
->name
,
1029 nexthop_tab
[i
].route_metric
,
1030 nexthop_tab
[i
].protocol_distance
);
1032 /* update nexthop data */
1033 nexthop
->interface
= ifp
;
1034 nexthop
->mrib_nexthop_addr
=
1035 nexthop_tab
[i
].nexthop_addr
;
1036 nexthop
->mrib_metric_preference
=
1037 nexthop_tab
[i
].protocol_distance
;
1038 nexthop
->mrib_route_metric
=
1039 nexthop_tab
[i
].route_metric
;
1040 nexthop
->last_lookup
= src
->u
.prefix4
;
1041 nexthop
->last_lookup_time
= pim_time_monotonic_usec();
1054 int pim_ecmp_fib_lookup_if_vif_index(struct pim_instance
*pim
,
1055 struct prefix
*src
, struct prefix
*grp
)
1057 struct pim_nexthop nhop
;
1060 char addr_str
[PREFIX_STRLEN
];
1062 if (PIM_DEBUG_PIM_NHT
)
1063 pim_inet4_dump("<addr?>", src
->u
.prefix4
, addr_str
,
1066 memset(&nhop
, 0, sizeof(nhop
));
1067 if (!pim_ecmp_nexthop_lookup(pim
, &nhop
, src
, grp
, 1)) {
1068 if (PIM_DEBUG_PIM_NHT
)
1070 "%s: could not find nexthop ifindex for address %s(%s)",
1071 __func__
, addr_str
, pim
->vrf
->name
);
1075 ifindex
= nhop
.interface
->ifindex
;
1076 if (PIM_DEBUG_PIM_NHT
)
1078 "%s: found nexthop ifindex=%d (interface %s(%s)) for address %s",
1080 ifindex2ifname(ifindex
, pim
->vrf
->vrf_id
),
1081 pim
->vrf
->name
, addr_str
);
1083 vif_index
= pim_if_find_vifindex_by_ifindex(pim
, ifindex
);
1085 if (vif_index
< 0) {
1086 if (PIM_DEBUG_PIM_NHT
) {
1088 "%s: low vif_index=%d(%s) < 1 nexthop for address %s",
1089 __func__
, vif_index
, pim
->vrf
->name
, addr_str
);