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
30 #include "pimd/pim_nht.h"
34 #include "pim_ifchannel.h"
35 #include "pim_mroute.h"
36 #include "pim_zebra.h"
37 #include "pim_upstream.h"
39 #include "pim_jp_agg.h"
40 #include "pim_zebra.h"
41 #include "pim_zlookup.h"
45 * pim_sendmsg_zebra_rnh -- Format and send a nexthop register/Unregister
48 void pim_sendmsg_zebra_rnh(struct pim_instance
*pim
, struct zclient
*zclient
,
49 struct pim_nexthop_cache
*pnc
, int command
)
54 p
= &(pnc
->rpf
.rpf_addr
);
55 ret
= zclient_send_rnh(zclient
, command
, p
, false, pim
->vrf_id
);
57 zlog_warn("sendmsg_nexthop: zclient_send_message() failed");
59 if (PIM_DEBUG_PIM_NHT
) {
60 char buf
[PREFIX2STR_BUFFER
];
61 prefix2str(p
, buf
, sizeof(buf
));
63 "%s: NHT %sregistered addr %s(%s) with Zebra ret:%d ",
65 (command
== ZEBRA_NEXTHOP_REGISTER
) ? " " : "de", buf
,
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
;
94 pnc
= XCALLOC(MTYPE_PIM_NEXTHOP_CACHE
,
95 sizeof(struct pim_nexthop_cache
));
96 pnc
->rpf
.rpf_addr
.family
= rpf_addr
->rpf_addr
.family
;
97 pnc
->rpf
.rpf_addr
.prefixlen
= rpf_addr
->rpf_addr
.prefixlen
;
98 pnc
->rpf
.rpf_addr
.u
.prefix4
.s_addr
=
99 rpf_addr
->rpf_addr
.u
.prefix4
.s_addr
;
101 pnc
= hash_get(pim
->rpf_hash
, pnc
, hash_alloc_intern
);
103 pnc
->rp_list
= list_new();
104 pnc
->rp_list
->cmp
= pim_rp_list_cmp
;
106 snprintf(hash_name
, 64, "PNC %s(%s) Upstream Hash",
107 prefix2str(&pnc
->rpf
.rpf_addr
, buf1
, 64), pim
->vrf
->name
);
108 pnc
->upstream_hash
= hash_create_size(8192, pim_upstream_hash_key
,
109 pim_upstream_equal
, hash_name
);
115 * pim_find_or_track_nexthop
117 * This API is used to Register an address with Zebra
122 int pim_find_or_track_nexthop(struct pim_instance
*pim
, struct prefix
*addr
,
123 struct pim_upstream
*up
, struct rp_info
*rp
,
124 bool bsr_track_needed
,
125 struct pim_nexthop_cache
*out_pnc
)
127 struct pim_nexthop_cache
*pnc
= NULL
;
129 struct listnode
*ch_node
= NULL
;
130 struct zclient
*zclient
= NULL
;
132 zclient
= pim_zebra_zclient_get();
133 memset(&rpf
, 0, sizeof(struct pim_rpf
));
134 rpf
.rpf_addr
.family
= addr
->family
;
135 rpf
.rpf_addr
.prefixlen
= addr
->prefixlen
;
136 rpf
.rpf_addr
.u
.prefix4
= addr
->u
.prefix4
;
138 pnc
= pim_nexthop_cache_find(pim
, &rpf
);
140 pnc
= pim_nexthop_cache_add(pim
, &rpf
);
141 pim_sendmsg_zebra_rnh(pim
, zclient
, pnc
,
142 ZEBRA_NEXTHOP_REGISTER
);
143 if (PIM_DEBUG_PIM_NHT
) {
144 char buf
[PREFIX2STR_BUFFER
];
145 prefix2str(addr
, buf
, sizeof(buf
));
147 "%s: NHT cache and zebra notification added for %s(%s)",
148 __PRETTY_FUNCTION__
, buf
, pim
->vrf
->name
);
153 ch_node
= listnode_lookup(pnc
->rp_list
, rp
);
155 listnode_add_sort(pnc
->rp_list
, rp
);
159 hash_get(pnc
->upstream_hash
, up
, hash_alloc_intern
);
161 if (bsr_track_needed
)
162 pnc
->bsr_tracking
= true;
164 if (CHECK_FLAG(pnc
->flags
, PIM_NEXTHOP_VALID
)) {
166 memcpy(out_pnc
, pnc
, sizeof(struct pim_nexthop_cache
));
173 void pim_delete_tracked_nexthop(struct pim_instance
*pim
, struct prefix
*addr
,
174 struct pim_upstream
*up
, struct rp_info
*rp
,
175 bool del_bsr_tracking
)
177 struct pim_nexthop_cache
*pnc
= NULL
;
178 struct pim_nexthop_cache lookup
;
179 struct zclient
*zclient
= NULL
;
180 struct listnode
*upnode
= NULL
;
181 struct pim_upstream
*upstream
= NULL
;
183 zclient
= pim_zebra_zclient_get();
185 /* Remove from RPF hash if it is the last entry */
186 lookup
.rpf
.rpf_addr
= *addr
;
187 pnc
= hash_lookup(pim
->rpf_hash
, &lookup
);
190 /* Release the (*, G)upstream from pnc->upstream_hash,
191 * whose Group belongs to the RP getting deleted
193 for (ALL_LIST_ELEMENTS_RO(pim
->upstream_list
, upnode
,
196 struct rp_info
*trp_info
;
198 if (upstream
->sg
.src
.s_addr
!= INADDR_ANY
)
201 grp
.family
= AF_INET
;
202 grp
.prefixlen
= IPV4_MAX_BITLEN
;
203 grp
.u
.prefix4
= upstream
->sg
.grp
;
205 trp_info
= pim_rp_find_match_group(pim
, &grp
);
207 hash_release(pnc
->upstream_hash
,
210 listnode_delete(pnc
->rp_list
, rp
);
214 hash_release(pnc
->upstream_hash
, up
);
216 if (del_bsr_tracking
)
217 pnc
->bsr_tracking
= false;
219 if (PIM_DEBUG_PIM_NHT
) {
220 char buf
[PREFIX_STRLEN
];
221 prefix2str(addr
, buf
, sizeof buf
);
223 "%s: NHT %s(%s) rp_list count:%d upstream count:%ld",
224 __PRETTY_FUNCTION__
, buf
, pim
->vrf
->name
,
225 pnc
->rp_list
->count
, pnc
->upstream_hash
->count
);
228 if (pnc
->rp_list
->count
== 0
229 && pnc
->upstream_hash
->count
== 0
230 && pnc
->bsr_tracking
== false) {
231 pim_sendmsg_zebra_rnh(pim
, zclient
, pnc
,
232 ZEBRA_NEXTHOP_UNREGISTER
);
234 list_delete(&pnc
->rp_list
);
235 hash_free(pnc
->upstream_hash
);
237 hash_release(pim
->rpf_hash
, pnc
);
239 nexthops_free(pnc
->nexthop
);
240 XFREE(MTYPE_PIM_NEXTHOP_CACHE
, pnc
);
245 /* Given a source address and a neighbor address, check if the neighbor is one
246 * of the next hop to reach the source. search from zebra route database
248 bool pim_nexthop_match(struct pim_instance
*pim
, struct in_addr addr
,
249 struct in_addr ip_src
)
251 struct pim_zlookup_nexthop nexthop_tab
[MULTIPATH_NUM
];
253 ifindex_t first_ifindex
= 0;
254 struct interface
*ifp
= NULL
;
255 struct pim_neighbor
*nbr
= NULL
;
258 if (addr
.s_addr
== INADDR_NONE
)
261 memset(nexthop_tab
, 0,
262 sizeof(struct pim_zlookup_nexthop
) * MULTIPATH_NUM
);
263 num_ifindex
= zclient_lookup_nexthop(pim
, nexthop_tab
, MULTIPATH_NUM
,
264 addr
, PIM_NEXTHOP_LOOKUP_MAX
);
265 if (num_ifindex
< 1) {
266 char addr_str
[INET_ADDRSTRLEN
];
268 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
270 "%s %s: could not find nexthop ifindex for address %s",
271 __FILE__
, __PRETTY_FUNCTION__
, addr_str
);
275 while (i
< num_ifindex
) {
276 first_ifindex
= nexthop_tab
[i
].ifindex
;
278 ifp
= if_lookup_by_index(first_ifindex
, pim
->vrf_id
);
280 if (PIM_DEBUG_ZEBRA
) {
281 char addr_str
[INET_ADDRSTRLEN
];
283 pim_inet4_dump("<addr?>", addr
, addr_str
,
286 "%s %s: could not find interface for ifindex %d (address %s)",
287 __FILE__
, __PRETTY_FUNCTION__
,
288 first_ifindex
, addr_str
);
295 if (PIM_DEBUG_ZEBRA
) {
296 char addr_str
[INET_ADDRSTRLEN
];
298 pim_inet4_dump("<addr?>", addr
, addr_str
,
301 "%s: multicast not enabled on input interface %s (ifindex=%d, RPF for source %s)",
302 __PRETTY_FUNCTION__
, ifp
->name
,
303 first_ifindex
, addr_str
);
309 if (!pim_if_connected_to_source(ifp
, addr
)) {
310 nbr
= pim_neighbor_find(
311 ifp
, nexthop_tab
[i
].nexthop_addr
.u
.prefix4
);
312 if (PIM_DEBUG_PIM_TRACE_DETAIL
)
313 zlog_debug("ifp name: %s, pim nbr: %p",
315 if (!nbr
&& !if_is_loopback(ifp
)) {
321 if (nexthop_tab
[i
].nexthop_addr
.u
.prefix4
.s_addr
331 /* Given a source address and a neighbor address, check if the neighbor is one
332 * of the next hop to reach the source. search from pim next hop cache
334 bool pim_nexthop_match_nht_cache(struct pim_instance
*pim
, struct in_addr addr
,
335 struct in_addr ip_src
)
338 ifindex_t first_ifindex
;
339 struct interface
*ifp
= NULL
;
341 struct pim_neighbor
*nbr
= NULL
;
342 struct nexthop
*nh_node
= NULL
;
343 struct pim_nexthop_cache
*pnc
= NULL
;
345 memset(&rpf
, 0, sizeof(struct pim_rpf
));
346 rpf
.rpf_addr
.family
= AF_INET
;
347 rpf
.rpf_addr
.prefixlen
= IPV4_MAX_BITLEN
;
348 rpf
.rpf_addr
.u
.prefix4
= addr
;
350 pnc
= pim_nexthop_cache_find(pim
, &rpf
);
351 if (!pnc
|| !pnc
->nexthop_num
)
354 for (nh_node
= pnc
->nexthop
; nh_node
; nh_node
= nh_node
->next
) {
355 first_ifindex
= nh_node
->ifindex
;
356 ifp
= if_lookup_by_index(first_ifindex
, pim
->vrf_id
);
358 if (PIM_DEBUG_PIM_NHT
) {
359 char addr_str
[INET_ADDRSTRLEN
];
361 pim_inet4_dump("<addr?>", addr
, addr_str
,
364 "%s %s: could not find interface for ifindex %d (address %s(%s))",
365 __FILE__
, __PRETTY_FUNCTION__
,
366 first_ifindex
, addr_str
,
373 if (PIM_DEBUG_PIM_NHT
) {
374 char addr_str
[INET_ADDRSTRLEN
];
376 pim_inet4_dump("<addr?>", addr
, addr_str
,
379 "%s: multicast not enabled on input interface %s(%s) (ifindex=%d, RPF for source %s)",
380 __PRETTY_FUNCTION__
, ifp
->name
,
381 pim
->vrf
->name
, first_ifindex
,
388 if (!pim_if_connected_to_source(ifp
, addr
)) {
389 nbr
= pim_neighbor_find(ifp
, nh_node
->gate
.ipv4
);
390 if (!nbr
&& !if_is_loopback(ifp
)) {
391 if (PIM_DEBUG_PIM_NHT
)
393 "%s: pim nbr not found on input interface %s(%s)",
394 __PRETTY_FUNCTION__
, ifp
->name
,
401 if (nh_node
->gate
.ipv4
.s_addr
== ip_src
.s_addr
)
408 void pim_rp_nexthop_del(struct rp_info
*rp_info
)
410 rp_info
->rp
.source_nexthop
.interface
= NULL
;
411 rp_info
->rp
.source_nexthop
.mrib_nexthop_addr
.u
.prefix4
.s_addr
=
413 rp_info
->rp
.source_nexthop
.mrib_metric_preference
=
414 router
->infinite_assert_metric
.metric_preference
;
415 rp_info
->rp
.source_nexthop
.mrib_route_metric
=
416 router
->infinite_assert_metric
.route_metric
;
419 /* Update RP nexthop info based on Nexthop update received from Zebra.*/
420 static void pim_update_rp_nh(struct pim_instance
*pim
,
421 struct pim_nexthop_cache
*pnc
)
423 struct listnode
*node
= NULL
;
424 struct rp_info
*rp_info
= NULL
;
426 /*Traverse RP list and update each RP Nexthop info */
427 for (ALL_LIST_ELEMENTS_RO(pnc
->rp_list
, node
, rp_info
)) {
428 if (rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
== INADDR_NONE
)
431 // Compute PIM RPF using cached nexthop
432 if (!pim_ecmp_nexthop_lookup(pim
, &rp_info
->rp
.source_nexthop
,
433 &rp_info
->rp
.rpf_addr
,
435 pim_rp_nexthop_del(rp_info
);
439 /* Update Upstream nexthop info based on Nexthop update received from Zebra.*/
440 static int pim_update_upstream_nh_helper(struct hash_bucket
*bucket
, void *arg
)
442 struct pim_instance
*pim
= (struct pim_instance
*)arg
;
443 struct pim_upstream
*up
= (struct pim_upstream
*)bucket
->data
;
445 enum pim_rpf_result rpf_result
;
448 old
.source_nexthop
.interface
= up
->rpf
.source_nexthop
.interface
;
449 rpf_result
= pim_rpf_update(pim
, up
, &old
, __func__
);
451 /* update kernel multicast forwarding cache (MFC); if the
452 * RPF nbr is now unreachable the MFC has already been updated
455 if (rpf_result
!= PIM_RPF_FAILURE
)
456 pim_upstream_mroute_iif_update(up
->channel_oil
, __func__
);
458 if (rpf_result
== PIM_RPF_CHANGED
||
459 (rpf_result
== PIM_RPF_FAILURE
&& old
.source_nexthop
.interface
))
460 pim_zebra_upstream_rpf_changed(pim
, up
, &old
);
463 if (PIM_DEBUG_PIM_NHT
) {
464 zlog_debug("%s: NHT upstream %s(%s) old ifp %s new ifp %s",
465 __PRETTY_FUNCTION__
, up
->sg_str
, pim
->vrf
->name
,
466 old
.source_nexthop
.interface
467 ? old
.source_nexthop
.interface
->name
: "Unknown",
468 up
->rpf
.source_nexthop
.interface
469 ? up
->rpf
.source_nexthop
.interface
->name
: "Unknown");
472 return HASHWALK_CONTINUE
;
475 static int pim_update_upstream_nh(struct pim_instance
*pim
,
476 struct pim_nexthop_cache
*pnc
)
478 hash_walk(pnc
->upstream_hash
, pim_update_upstream_nh_helper
, pim
);
480 pim_zebra_update_all_interfaces(pim
);
485 uint32_t pim_compute_ecmp_hash(struct prefix
*src
, struct prefix
*grp
)
488 uint32_t s
= 0, g
= 0;
493 switch (src
->family
) {
495 s
= src
->u
.prefix4
.s_addr
;
498 g
= grp
->u
.prefix4
.s_addr
;
504 hash_val
= jhash_2words(g
, s
, 101);
508 static int pim_ecmp_nexthop_search(struct pim_instance
*pim
,
509 struct pim_nexthop_cache
*pnc
,
510 struct pim_nexthop
*nexthop
,
511 struct prefix
*src
, struct prefix
*grp
,
514 struct pim_neighbor
*nbrs
[MULTIPATH_NUM
], *nbr
= NULL
;
515 struct interface
*ifps
[MULTIPATH_NUM
];
516 struct nexthop
*nh_node
= NULL
;
517 ifindex_t first_ifindex
;
518 struct interface
*ifp
= NULL
;
519 uint32_t hash_val
= 0, mod_val
= 0;
520 uint8_t nh_iter
= 0, found
= 0;
521 uint32_t i
, num_nbrs
= 0;
523 if (!pnc
|| !pnc
->nexthop_num
|| !nexthop
)
526 memset(&nbrs
, 0, sizeof(nbrs
));
527 memset(&ifps
, 0, sizeof(ifps
));
529 // Current Nexthop is VALID, check to stay on the current path.
530 if (nexthop
->interface
&& nexthop
->interface
->info
531 && nexthop
->mrib_nexthop_addr
.u
.prefix4
.s_addr
532 != PIM_NET_INADDR_ANY
) {
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(
557 nexthop
->mrib_nexthop_addr
.u
.prefix4
);
559 && !if_is_loopback(nexthop
->interface
)) {
560 if (PIM_DEBUG_PIM_NHT
)
562 "%s: current nexthop does not have nbr ",
563 __PRETTY_FUNCTION__
);
565 if (PIM_DEBUG_PIM_NHT
) {
566 char src_str
[INET_ADDRSTRLEN
];
567 pim_inet4_dump("<addr?>",
571 char grp_str
[INET_ADDRSTRLEN
];
572 pim_inet4_dump("<addr?>",
577 "%s: (%s,%s)(%s) current nexthop %s is valid, skipping new path selection",
581 nexthop
->interface
->name
);
590 * Look up all interfaces and neighbors,
591 * store for later usage
593 for (nh_node
= pnc
->nexthop
, i
= 0; nh_node
;
594 nh_node
= nh_node
->next
, i
++) {
595 ifps
[i
] = if_lookup_by_index(nh_node
->ifindex
, pim
->vrf_id
);
597 nbrs
[i
] = pim_neighbor_find(ifps
[i
],
599 if (nbrs
[i
] || pim_if_connected_to_source(ifps
[i
],
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
) {
625 char addr_str
[INET_ADDRSTRLEN
];
626 pim_inet4_dump("<addr?>", src
->u
.prefix4
,
627 addr_str
, sizeof(addr_str
));
629 "%s %s: could not find interface for ifindex %d (address %s(%s))",
630 __FILE__
, __PRETTY_FUNCTION__
,
631 first_ifindex
, addr_str
,
634 if (nh_iter
== mod_val
)
635 mod_val
++; // Select nexthpath
640 if (PIM_DEBUG_PIM_NHT
) {
641 char addr_str
[INET_ADDRSTRLEN
];
642 pim_inet4_dump("<addr?>", src
->u
.prefix4
,
643 addr_str
, sizeof(addr_str
));
645 "%s: multicast not enabled on input interface %s(%s) (ifindex=%d, RPF for source %s)",
646 __PRETTY_FUNCTION__
, ifp
->name
,
647 pim
->vrf
->name
, first_ifindex
,
650 if (nh_iter
== mod_val
)
651 mod_val
++; // Select nexthpath
657 && !pim_if_connected_to_source(ifp
, src
->u
.prefix4
)) {
659 if (!nbr
&& !if_is_loopback(ifp
)) {
660 if (PIM_DEBUG_PIM_NHT
)
662 "%s: pim nbr not found on input interface %s(%s)",
663 __PRETTY_FUNCTION__
, ifp
->name
,
665 if (nh_iter
== mod_val
)
666 mod_val
++; // Select nexthpath
672 if (nh_iter
== mod_val
) {
673 nexthop
->interface
= ifp
;
674 nexthop
->mrib_nexthop_addr
.family
= AF_INET
;
675 nexthop
->mrib_nexthop_addr
.prefixlen
= IPV4_MAX_BITLEN
;
676 nexthop
->mrib_nexthop_addr
.u
.prefix4
=
678 nexthop
->mrib_metric_preference
= pnc
->distance
;
679 nexthop
->mrib_route_metric
= pnc
->metric
;
680 nexthop
->last_lookup
= src
->u
.prefix4
;
681 nexthop
->last_lookup_time
= pim_time_monotonic_usec();
684 if (PIM_DEBUG_PIM_NHT
) {
685 char buf
[INET_ADDRSTRLEN
];
686 char buf2
[INET_ADDRSTRLEN
];
687 char buf3
[INET_ADDRSTRLEN
];
688 pim_inet4_dump("<src?>", src
->u
.prefix4
, buf2
,
690 pim_inet4_dump("<grp?>", grp
->u
.prefix4
, buf3
,
694 nexthop
->mrib_nexthop_addr
.u
.prefix4
,
697 "%s: (%s,%s)(%s) selected nhop interface %s addr %s mod_val %u iter %d ecmp %d",
698 __PRETTY_FUNCTION__
, buf2
, buf3
,
699 pim
->vrf
->name
, ifp
->name
, buf
, mod_val
,
700 nh_iter
, pim
->ecmp_enable
);
712 /* This API is used to parse Registered address nexthop update coming from Zebra
714 int pim_parse_nexthop_update(ZAPI_CALLBACK_ARGS
)
716 struct nexthop
*nexthop
;
717 struct nexthop
*nhlist_head
= NULL
;
718 struct nexthop
*nhlist_tail
= NULL
;
721 struct pim_nexthop_cache
*pnc
= NULL
;
722 struct pim_neighbor
*nbr
= NULL
;
723 struct interface
*ifp
= NULL
;
724 struct interface
*ifp1
= NULL
;
725 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
726 struct pim_instance
*pim
;
727 struct zapi_route nhr
;
733 if (!zapi_nexthop_update_decode(zclient
->ibuf
, &nhr
)) {
734 if (PIM_DEBUG_PIM_NHT
)
736 "%s: Decode of nexthop update from zebra failed",
737 __PRETTY_FUNCTION__
);
741 if (cmd
== ZEBRA_NEXTHOP_UPDATE
) {
742 prefix_copy(&rpf
.rpf_addr
, &nhr
.prefix
);
743 pnc
= pim_nexthop_cache_find(pim
, &rpf
);
745 if (PIM_DEBUG_PIM_NHT
) {
746 char buf
[PREFIX2STR_BUFFER
];
747 prefix2str(&rpf
.rpf_addr
, buf
, sizeof(buf
));
749 "%s: Skipping NHT update, addr %s is not in local cached DB.",
750 __PRETTY_FUNCTION__
, buf
);
756 * We do not currently handle ZEBRA_IMPORT_CHECK_UPDATE
761 pnc
->last_update
= pim_time_monotonic_usec();
763 if (nhr
.nexthop_num
) {
764 pnc
->nexthop_num
= 0; // Only increment for pim enabled rpf.
766 for (i
= 0; i
< nhr
.nexthop_num
; i
++) {
767 nexthop
= nexthop_from_zapi_nexthop(&nhr
.nexthops
[i
]);
768 switch (nexthop
->type
) {
769 case NEXTHOP_TYPE_IPV4
:
770 case NEXTHOP_TYPE_IPV4_IFINDEX
:
771 case NEXTHOP_TYPE_IPV6
:
772 case NEXTHOP_TYPE_BLACKHOLE
:
774 case NEXTHOP_TYPE_IFINDEX
:
776 * Connected route (i.e. no nexthop), use
777 * RPF address from nexthop cache (i.e.
778 * destination) as PIM nexthop.
780 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
782 pnc
->rpf
.rpf_addr
.u
.prefix4
;
784 case NEXTHOP_TYPE_IPV6_IFINDEX
:
785 ifp1
= if_lookup_by_index(nexthop
->ifindex
,
787 nbr
= pim_neighbor_find_if(ifp1
);
788 /* Overwrite with Nbr address as NH addr */
790 nexthop
->gate
.ipv4
= nbr
->source_addr
;
792 // Mark nexthop address to 0 until PIM
794 nexthop
->gate
.ipv4
.s_addr
=
801 ifp
= if_lookup_by_index(nexthop
->ifindex
, pim
->vrf_id
);
803 if (PIM_DEBUG_PIM_NHT
) {
804 char buf
[NEXTHOP_STRLEN
];
806 "%s: could not find interface for ifindex %d(%s) (addr %s)",
810 nexthop2str(nexthop
, buf
,
813 nexthop_free(nexthop
);
817 if (PIM_DEBUG_PIM_NHT
) {
818 char p_str
[PREFIX2STR_BUFFER
];
820 prefix2str(&nhr
.prefix
, p_str
, sizeof(p_str
));
822 "%s: NHT addr %s(%s) %d-nhop via %s(%s) type %d distance:%u metric:%u ",
823 __PRETTY_FUNCTION__
, p_str
,
824 pim
->vrf
->name
, i
+ 1,
825 inet_ntoa(nexthop
->gate
.ipv4
),
826 ifp
->name
, nexthop
->type
, nhr
.distance
,
832 * Though Multicast is not enabled on this
833 * Interface store it in database otheriwse we
834 * may miss this update and this will not cause
835 * any issue, because while choosing the path we
836 * are ommitting the Interfaces which are not
839 if (PIM_DEBUG_PIM_NHT
) {
840 char buf
[NEXTHOP_STRLEN
];
843 "%s: multicast not enabled on input interface %s(%s) (ifindex=%d, addr %s)",
844 __PRETTY_FUNCTION__
, ifp
->name
,
847 nexthop2str(nexthop
, buf
,
853 nhlist_tail
->next
= nexthop
;
854 nhlist_tail
= nexthop
;
856 nhlist_tail
= nexthop
;
857 nhlist_head
= nexthop
;
859 // Only keep track of nexthops which are PIM enabled.
862 /* Reset existing pnc->nexthop before assigning new list */
863 nexthops_free(pnc
->nexthop
);
864 pnc
->nexthop
= nhlist_head
;
865 if (pnc
->nexthop_num
) {
866 pnc
->flags
|= PIM_NEXTHOP_VALID
;
867 pnc
->distance
= nhr
.distance
;
868 pnc
->metric
= nhr
.metric
;
871 pnc
->flags
&= ~PIM_NEXTHOP_VALID
;
872 pnc
->nexthop_num
= nhr
.nexthop_num
;
873 nexthops_free(pnc
->nexthop
);
876 SET_FLAG(pnc
->flags
, PIM_NEXTHOP_ANSWER_RECEIVED
);
878 if (PIM_DEBUG_PIM_NHT
) {
879 char buf
[PREFIX2STR_BUFFER
];
880 prefix2str(&nhr
.prefix
, buf
, sizeof(buf
));
882 "%s: NHT Update for %s(%s) num_nh %d num_pim_nh %d vrf:%u up %ld rp %d",
883 __PRETTY_FUNCTION__
, buf
, pim
->vrf
->name
,
884 nhr
.nexthop_num
, pnc
->nexthop_num
, vrf_id
,
885 pnc
->upstream_hash
->count
, listcount(pnc
->rp_list
));
888 pim_rpf_set_refresh_time(pim
);
890 if (listcount(pnc
->rp_list
))
891 pim_update_rp_nh(pim
, pnc
);
892 if (pnc
->upstream_hash
->count
)
893 pim_update_upstream_nh(pim
, pnc
);
898 int pim_ecmp_nexthop_lookup(struct pim_instance
*pim
,
899 struct pim_nexthop
*nexthop
, struct prefix
*src
,
900 struct prefix
*grp
, int neighbor_needed
)
902 struct pim_nexthop_cache
*pnc
;
903 struct pim_zlookup_nexthop nexthop_tab
[MULTIPATH_NUM
];
904 struct pim_neighbor
*nbrs
[MULTIPATH_NUM
], *nbr
= NULL
;
907 struct interface
*ifps
[MULTIPATH_NUM
], *ifp
;
911 uint32_t hash_val
= 0, mod_val
= 0;
912 uint32_t num_nbrs
= 0;
913 char addr_str
[PREFIX_STRLEN
];
915 if (PIM_DEBUG_PIM_NHT
) {
916 pim_inet4_dump("<addr?>", src
->u
.prefix4
, addr_str
,
918 zlog_debug("%s: Looking up: %s(%s), last lookup time: %lld",
919 __PRETTY_FUNCTION__
, addr_str
, pim
->vrf
->name
,
920 nexthop
->last_lookup_time
);
923 memset(&rpf
, 0, sizeof(struct pim_rpf
));
924 rpf
.rpf_addr
.family
= AF_INET
;
925 rpf
.rpf_addr
.prefixlen
= IPV4_MAX_BITLEN
;
926 rpf
.rpf_addr
.u
.prefix4
= src
->u
.prefix4
;
928 pnc
= pim_nexthop_cache_find(pim
, &rpf
);
930 if (CHECK_FLAG(pnc
->flags
, PIM_NEXTHOP_ANSWER_RECEIVED
))
931 return pim_ecmp_nexthop_search(pim
, pnc
, nexthop
, src
, grp
,
935 memset(nexthop_tab
, 0,
936 sizeof(struct pim_zlookup_nexthop
) * MULTIPATH_NUM
);
938 zclient_lookup_nexthop(pim
, nexthop_tab
, MULTIPATH_NUM
,
939 src
->u
.prefix4
, PIM_NEXTHOP_LOOKUP_MAX
);
940 if (num_ifindex
< 1) {
941 if (PIM_DEBUG_PIM_NHT
)
943 "%s: could not find nexthop ifindex for address %s(%s)",
944 __PRETTY_FUNCTION__
, addr_str
, pim
->vrf
->name
);
948 memset(&nbrs
, 0, sizeof(nbrs
));
949 memset(&ifps
, 0, sizeof(ifps
));
952 * Look up all interfaces and neighbors,
953 * store for later usage
955 for (i
= 0; i
< num_ifindex
; i
++) {
956 ifps
[i
] = if_lookup_by_index(nexthop_tab
[i
].ifindex
,
959 nbrs
[i
] = pim_neighbor_find(
960 ifps
[i
], nexthop_tab
[i
].nexthop_addr
.u
.prefix4
);
962 || pim_if_connected_to_source(ifps
[i
],
968 // If PIM ECMP enable then choose ECMP path.
969 if (pim
->ecmp_enable
) {
970 uint32_t consider
= num_ifindex
;
972 if (neighbor_needed
&& num_nbrs
< consider
)
978 hash_val
= pim_compute_ecmp_hash(src
, grp
);
979 mod_val
= hash_val
% consider
;
980 if (PIM_DEBUG_PIM_NHT_DETAIL
)
981 zlog_debug("%s: hash_val %u mod_val %u",
982 __PRETTY_FUNCTION__
, hash_val
, mod_val
);
986 while (!found
&& (i
< num_ifindex
)) {
987 first_ifindex
= nexthop_tab
[i
].ifindex
;
991 if (PIM_DEBUG_PIM_NHT
)
993 "%s %s: could not find interface for ifindex %d (address %s(%s))",
994 __FILE__
, __PRETTY_FUNCTION__
,
995 first_ifindex
, addr_str
,
1004 if (PIM_DEBUG_PIM_NHT
)
1006 "%s: multicast not enabled on input interface %s(%s) (ifindex=%d, RPF for source %s)",
1007 __PRETTY_FUNCTION__
, ifp
->name
,
1008 pim
->vrf
->name
, first_ifindex
,
1016 && !pim_if_connected_to_source(ifp
, src
->u
.prefix4
)) {
1018 if (PIM_DEBUG_PIM_NHT_DETAIL
)
1019 zlog_debug("ifp name: %s(%s), pim nbr: %p",
1020 ifp
->name
, pim
->vrf
->name
, nbr
);
1021 if (!nbr
&& !if_is_loopback(ifp
)) {
1025 if (PIM_DEBUG_PIM_NHT
)
1027 "%s: NBR not found on input interface %s(%s) (RPF for source %s)",
1028 __PRETTY_FUNCTION__
, ifp
->name
,
1029 pim
->vrf
->name
, addr_str
);
1035 if (PIM_DEBUG_PIM_NHT
) {
1036 char nexthop_str
[PREFIX_STRLEN
];
1038 pim_addr_dump("<nexthop?>",
1039 &nexthop_tab
[i
].nexthop_addr
,
1040 nexthop_str
, sizeof(nexthop_str
));
1042 "%s: found nhop %s for addr %s interface %s(%s) metric %d dist %d",
1043 __PRETTY_FUNCTION__
, nexthop_str
,
1044 addr_str
, ifp
->name
, pim
->vrf
->name
,
1045 nexthop_tab
[i
].route_metric
,
1046 nexthop_tab
[i
].protocol_distance
);
1048 /* update nexthop data */
1049 nexthop
->interface
= ifp
;
1050 nexthop
->mrib_nexthop_addr
=
1051 nexthop_tab
[i
].nexthop_addr
;
1052 nexthop
->mrib_metric_preference
=
1053 nexthop_tab
[i
].protocol_distance
;
1054 nexthop
->mrib_route_metric
=
1055 nexthop_tab
[i
].route_metric
;
1056 nexthop
->last_lookup
= src
->u
.prefix4
;
1057 nexthop
->last_lookup_time
= pim_time_monotonic_usec();
1070 int pim_ecmp_fib_lookup_if_vif_index(struct pim_instance
*pim
,
1071 struct prefix
*src
, struct prefix
*grp
)
1073 struct pim_nexthop nhop
;
1076 char addr_str
[PREFIX_STRLEN
];
1078 if (PIM_DEBUG_PIM_NHT
)
1079 pim_inet4_dump("<addr?>", src
->u
.prefix4
, addr_str
,
1082 memset(&nhop
, 0, sizeof(nhop
));
1083 if (!pim_ecmp_nexthop_lookup(pim
, &nhop
, src
, grp
, 0)) {
1084 if (PIM_DEBUG_PIM_NHT
)
1086 "%s: could not find nexthop ifindex for address %s(%s)",
1087 __PRETTY_FUNCTION__
, addr_str
, pim
->vrf
->name
);
1091 ifindex
= nhop
.interface
->ifindex
;
1092 if (PIM_DEBUG_PIM_NHT
)
1094 "%s: found nexthop ifindex=%d (interface %s(%s)) for address %s",
1095 __PRETTY_FUNCTION__
, ifindex
,
1096 ifindex2ifname(ifindex
, pim
->vrf_id
),
1097 pim
->vrf
->name
, addr_str
);
1099 vif_index
= pim_if_find_vifindex_by_ifindex(pim
, ifindex
);
1101 if (vif_index
< 0) {
1102 if (PIM_DEBUG_PIM_NHT
) {
1104 "%s: low vif_index=%d(%s) < 1 nexthop for address %s",
1105 __PRETTY_FUNCTION__
, vif_index
, pim
->vrf
->name
,