3 * Copyright (C) 2015 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
34 #include "lib_errors.h"
39 #include "pim_iface.h"
44 #include "pim_memory.h"
45 #include "pim_iface.h"
48 #include "pim_mroute.h"
50 #include "pim_zebra.h"
52 /* Cleanup pim->rpf_hash each node data */
53 void pim_rp_list_hash_clean(void *data
)
55 struct pim_nexthop_cache
*pnc
= (struct pim_nexthop_cache
*)data
;
57 list_delete(&pnc
->rp_list
);
59 hash_clean(pnc
->upstream_hash
, NULL
);
60 hash_free(pnc
->upstream_hash
);
61 pnc
->upstream_hash
= NULL
;
63 nexthops_free(pnc
->nexthop
);
65 XFREE(MTYPE_PIM_NEXTHOP_CACHE
, pnc
);
68 static void pim_rp_info_free(struct rp_info
*rp_info
)
70 XFREE(MTYPE_PIM_FILTER_NAME
, rp_info
->plist
);
72 XFREE(MTYPE_PIM_RP
, rp_info
);
75 int pim_rp_list_cmp(void *v1
, void *v2
)
77 struct rp_info
*rp1
= (struct rp_info
*)v1
;
78 struct rp_info
*rp2
= (struct rp_info
*)v2
;
81 * Sort by RP IP address
83 if (rp1
->rp
.rpf_addr
.u
.prefix4
.s_addr
84 < rp2
->rp
.rpf_addr
.u
.prefix4
.s_addr
)
87 if (rp1
->rp
.rpf_addr
.u
.prefix4
.s_addr
88 > rp2
->rp
.rpf_addr
.u
.prefix4
.s_addr
)
92 * Sort by group IP address
94 if (rp1
->group
.u
.prefix4
.s_addr
< rp2
->group
.u
.prefix4
.s_addr
)
97 if (rp1
->group
.u
.prefix4
.s_addr
> rp2
->group
.u
.prefix4
.s_addr
)
103 void pim_rp_init(struct pim_instance
*pim
)
105 struct rp_info
*rp_info
;
106 struct route_node
*rn
;
108 pim
->rp_list
= list_new();
109 pim
->rp_list
->del
= (void (*)(void *))pim_rp_info_free
;
110 pim
->rp_list
->cmp
= pim_rp_list_cmp
;
112 pim
->rp_table
= route_table_init();
114 rp_info
= XCALLOC(MTYPE_PIM_RP
, sizeof(*rp_info
));
116 if (!str2prefix("224.0.0.0/4", &rp_info
->group
)) {
117 flog_err(EC_LIB_DEVELOPMENT
,
118 "Unable to convert 224.0.0.0/4 to prefix");
119 list_delete(&pim
->rp_list
);
120 route_table_finish(pim
->rp_table
);
121 XFREE(MTYPE_PIM_RP
, rp_info
);
124 rp_info
->group
.family
= AF_INET
;
125 rp_info
->rp
.rpf_addr
.family
= AF_INET
;
126 rp_info
->rp
.rpf_addr
.prefixlen
= IPV4_MAX_PREFIXLEN
;
127 rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
= INADDR_NONE
;
129 listnode_add(pim
->rp_list
, rp_info
);
131 rn
= route_node_get(pim
->rp_table
, &rp_info
->group
);
135 "Allocated: %p for rp_info: %p(224.0.0.0/4) Lock: %d",
136 rn
, rp_info
, rn
->lock
);
139 void pim_rp_free(struct pim_instance
*pim
)
142 list_delete(&pim
->rp_list
);
146 * Given an RP's prefix-list, return the RP's rp_info for that prefix-list
148 static struct rp_info
*pim_rp_find_prefix_list(struct pim_instance
*pim
,
152 struct listnode
*node
;
153 struct rp_info
*rp_info
;
155 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
156 if (rp
.s_addr
== rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
157 && rp_info
->plist
&& strcmp(rp_info
->plist
, plist
) == 0) {
166 * Return true if plist is used by any rp_info
168 static int pim_rp_prefix_list_used(struct pim_instance
*pim
, const char *plist
)
170 struct listnode
*node
;
171 struct rp_info
*rp_info
;
173 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
174 if (rp_info
->plist
&& strcmp(rp_info
->plist
, plist
) == 0) {
183 * Given an RP's address, return the RP's rp_info that is an exact match for
186 static struct rp_info
*pim_rp_find_exact(struct pim_instance
*pim
,
188 const struct prefix
*group
)
190 struct listnode
*node
;
191 struct rp_info
*rp_info
;
193 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
194 if (rp
.s_addr
== rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
195 && prefix_same(&rp_info
->group
, group
))
203 * Given a group, return the rp_info for that group
205 struct rp_info
*pim_rp_find_match_group(struct pim_instance
*pim
,
206 const struct prefix
*group
)
208 struct listnode
*node
;
209 struct rp_info
*best
= NULL
;
210 struct rp_info
*rp_info
;
211 struct prefix_list
*plist
;
212 const struct prefix
*p
, *bp
;
213 struct route_node
*rn
;
216 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
217 if (rp_info
->plist
) {
218 plist
= prefix_list_lookup(AFI_IP
, rp_info
->plist
);
220 if (prefix_list_apply_which_prefix(plist
, &p
, group
)
230 if (bp
&& bp
->prefixlen
< p
->prefixlen
) {
237 rn
= route_node_match(pim
->rp_table
, group
);
241 "%s: BUG We should have found default group information\n",
242 __PRETTY_FUNCTION__
);
247 if (PIM_DEBUG_TRACE
) {
248 char buf
[PREFIX_STRLEN
];
250 route_unlock_node(rn
);
251 zlog_debug("Lookedup: %p for rp_info: %p(%s) Lock: %d", rn
,
253 prefix2str(&rp_info
->group
, buf
, sizeof(buf
)),
260 if (rp_info
->group
.prefixlen
< best
->group
.prefixlen
)
267 * When the user makes "ip pim rp" configuration changes or if they change the
268 * prefix-list(s) used by these statements we must tickle the upstream state
269 * for each group to make them re-lookup who their RP should be.
271 * This is a placeholder function for now.
273 static void pim_rp_refresh_group_to_rp_mapping(struct pim_instance
*pim
)
275 pim_msdp_i_am_rp_changed(pim
);
278 void pim_rp_prefix_list_update(struct pim_instance
*pim
,
279 struct prefix_list
*plist
)
281 struct listnode
*node
;
282 struct rp_info
*rp_info
;
283 int refresh_needed
= 0;
285 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
287 && strcmp(rp_info
->plist
, prefix_list_name(plist
)) == 0) {
294 pim_rp_refresh_group_to_rp_mapping(pim
);
297 static int pim_rp_check_interface_addrs(struct rp_info
*rp_info
,
298 struct pim_interface
*pim_ifp
)
300 struct listnode
*node
;
301 struct pim_secondary_addr
*sec_addr
;
303 if (pim_ifp
->primary_address
.s_addr
304 == rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
)
307 if (!pim_ifp
->sec_addr_list
) {
311 for (ALL_LIST_ELEMENTS_RO(pim_ifp
->sec_addr_list
, node
, sec_addr
)) {
312 if (prefix_same(&sec_addr
->addr
, &rp_info
->rp
.rpf_addr
)) {
320 static void pim_rp_check_interfaces(struct pim_instance
*pim
,
321 struct rp_info
*rp_info
)
323 struct interface
*ifp
;
325 rp_info
->i_am_rp
= 0;
326 FOR_ALL_INTERFACES (pim
->vrf
, ifp
) {
327 struct pim_interface
*pim_ifp
= ifp
->info
;
332 if (pim_rp_check_interface_addrs(rp_info
, pim_ifp
)) {
333 rp_info
->i_am_rp
= 1;
338 void pim_upstream_update(struct pim_instance
*pim
, struct pim_upstream
*up
)
340 struct pim_rpf old_rpf
;
341 enum pim_rpf_result rpf_result
;
342 struct in_addr old_upstream_addr
;
343 struct in_addr new_upstream_addr
;
346 old_upstream_addr
= up
->upstream_addr
;
347 pim_rp_set_upstream_addr(pim
, &new_upstream_addr
, up
->sg
.src
,
351 zlog_debug("%s: pim upstream update for old upstream %s",
353 inet_ntoa(old_upstream_addr
));
355 if (old_upstream_addr
.s_addr
== new_upstream_addr
.s_addr
)
358 /* Lets consider a case, where a PIM upstream has a better RP as a
359 * result of a new RP configuration with more precise group range.
360 * This upstream has to be added to the upstream hash of new RP's
361 * NHT(pnc) and has to be removed from old RP's NHT upstream hash
363 if (old_upstream_addr
.s_addr
!= INADDR_ANY
) {
364 /* Deregister addr with Zebra NHT */
365 nht_p
.family
= AF_INET
;
366 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
367 nht_p
.u
.prefix4
= old_upstream_addr
;
368 if (PIM_DEBUG_TRACE
) {
369 char buf
[PREFIX2STR_BUFFER
];
371 prefix2str(&nht_p
, buf
, sizeof(buf
));
372 zlog_debug("%s: Deregister upstream %s addr %s with Zebra NHT",
373 __PRETTY_FUNCTION__
, up
->sg_str
, buf
);
375 pim_delete_tracked_nexthop(pim
, &nht_p
, up
, NULL
);
378 /* Update the upstream address */
379 up
->upstream_addr
= new_upstream_addr
;
381 old_rpf
.source_nexthop
.interface
= up
->rpf
.source_nexthop
.interface
;
383 rpf_result
= pim_rpf_update(pim
, up
, &old_rpf
, 1);
384 if (rpf_result
== PIM_RPF_FAILURE
)
385 pim_mroute_del(up
->channel_oil
, __PRETTY_FUNCTION__
);
387 /* update kernel multicast forwarding cache (MFC) */
388 if (up
->rpf
.source_nexthop
.interface
&& up
->channel_oil
) {
389 ifindex_t ifindex
= up
->rpf
.source_nexthop
.interface
->ifindex
;
390 int vif_index
= pim_if_find_vifindex_by_ifindex(pim
, ifindex
);
391 /* Pass Current selected NH vif index to mroute download */
393 pim_scan_individual_oil(up
->channel_oil
, vif_index
);
395 if (PIM_DEBUG_PIM_NHT
)
397 "%s: NHT upstream %s channel_oil IIF %s vif_index is not valid",
398 __PRETTY_FUNCTION__
, up
->sg_str
,
399 up
->rpf
.source_nexthop
.interface
->name
);
403 if (rpf_result
== PIM_RPF_CHANGED
)
404 pim_zebra_upstream_rpf_changed(pim
, up
, &old_rpf
);
406 pim_zebra_update_all_interfaces(pim
);
409 int pim_rp_new(struct pim_instance
*pim
, const char *rp
,
410 const char *group_range
, const char *plist
)
413 struct rp_info
*rp_info
;
414 struct rp_info
*rp_all
;
415 struct prefix group_all
;
416 struct listnode
*node
, *nnode
;
417 struct rp_info
*tmp_rp_info
;
421 struct route_node
*rn
;
422 struct pim_upstream
*up
;
423 struct listnode
*upnode
;
425 rp_info
= XCALLOC(MTYPE_PIM_RP
, sizeof(*rp_info
));
427 if (group_range
== NULL
)
428 result
= str2prefix("224.0.0.0/4", &rp_info
->group
);
430 result
= str2prefix(group_range
, &rp_info
->group
);
432 prefix_copy(&temp
, &rp_info
->group
);
434 if (!prefix_same(&rp_info
->group
, &temp
)) {
435 XFREE(MTYPE_PIM_RP
, rp_info
);
436 return PIM_GROUP_BAD_ADDR_MASK_COMBO
;
442 XFREE(MTYPE_PIM_RP
, rp_info
);
443 return PIM_GROUP_BAD_ADDRESS
;
446 rp_info
->rp
.rpf_addr
.family
= AF_INET
;
447 rp_info
->rp
.rpf_addr
.prefixlen
= IPV4_MAX_PREFIXLEN
;
448 result
= inet_pton(rp_info
->rp
.rpf_addr
.family
, rp
,
449 &rp_info
->rp
.rpf_addr
.u
.prefix4
);
452 XFREE(MTYPE_PIM_RP
, rp_info
);
453 return PIM_RP_BAD_ADDRESS
;
458 * Return if the prefix-list is already configured for this RP
460 if (pim_rp_find_prefix_list(pim
, rp_info
->rp
.rpf_addr
.u
.prefix4
,
462 XFREE(MTYPE_PIM_RP
, rp_info
);
467 * Barf if the prefix-list is already configured for an RP
469 if (pim_rp_prefix_list_used(pim
, plist
)) {
470 XFREE(MTYPE_PIM_RP
, rp_info
);
471 return PIM_RP_PFXLIST_IN_USE
;
475 * Free any existing rp_info entries for this RP
477 for (ALL_LIST_ELEMENTS(pim
->rp_list
, node
, nnode
,
479 if (rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
480 == tmp_rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
) {
481 if (tmp_rp_info
->plist
)
482 pim_rp_del(pim
, rp
, NULL
,
487 prefix2str(&tmp_rp_info
->group
,
493 rp_info
->plist
= XSTRDUP(MTYPE_PIM_FILTER_NAME
, plist
);
496 if (!str2prefix("224.0.0.0/4", &group_all
)) {
497 XFREE(MTYPE_PIM_RP
, rp_info
);
498 return PIM_GROUP_BAD_ADDRESS
;
500 rp_all
= pim_rp_find_match_group(pim
, &group_all
);
503 * Barf if group is a non-multicast subnet
505 if (!prefix_match(&rp_all
->group
, &rp_info
->group
)) {
506 XFREE(MTYPE_PIM_RP
, rp_info
);
507 return PIM_GROUP_BAD_ADDRESS
;
511 * Remove any prefix-list rp_info entries for this RP
513 for (ALL_LIST_ELEMENTS(pim
->rp_list
, node
, nnode
,
515 if (tmp_rp_info
->plist
516 && rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
517 == tmp_rp_info
->rp
.rpf_addr
.u
.prefix4
519 pim_rp_del(pim
, rp
, NULL
, tmp_rp_info
->plist
);
524 * Take over the 224.0.0.0/4 group if the rp is INADDR_NONE
526 if (prefix_same(&rp_all
->group
, &rp_info
->group
)
527 && pim_rpf_addr_is_inaddr_none(&rp_all
->rp
)) {
528 rp_all
->rp
.rpf_addr
= rp_info
->rp
.rpf_addr
;
529 XFREE(MTYPE_PIM_RP
, rp_info
);
531 /* Register addr with Zebra NHT */
532 nht_p
.family
= AF_INET
;
533 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
535 rp_all
->rp
.rpf_addr
.u
.prefix4
; // RP address
536 if (PIM_DEBUG_PIM_NHT_RP
) {
537 char buf
[PREFIX2STR_BUFFER
];
538 char buf1
[PREFIX2STR_BUFFER
];
539 prefix2str(&nht_p
, buf
, sizeof(buf
));
540 prefix2str(&rp_all
->group
, buf1
, sizeof(buf1
));
542 "%s: NHT Register rp_all addr %s grp %s ",
543 __PRETTY_FUNCTION__
, buf
, buf1
);
546 for (ALL_LIST_ELEMENTS_RO(pim
->upstream_list
, upnode
,
548 /* Find (*, G) upstream whose RP is not
551 if ((up
->upstream_addr
.s_addr
== INADDR_ANY
)
552 && (up
->sg
.src
.s_addr
== INADDR_ANY
)) {
554 struct rp_info
*trp_info
;
556 grp
.family
= AF_INET
;
557 grp
.prefixlen
= IPV4_MAX_BITLEN
;
558 grp
.u
.prefix4
= up
->sg
.grp
;
559 trp_info
= pim_rp_find_match_group(pim
,
561 if (trp_info
== rp_all
)
562 pim_upstream_update(pim
, up
);
566 pim_rp_check_interfaces(pim
, rp_all
);
567 pim_rp_refresh_group_to_rp_mapping(pim
);
569 pim_find_or_track_nexthop(pim
, &nht_p
, NULL
, rp_all
,
571 if (!pim_ecmp_nexthop_lookup(pim
,
572 &rp_all
->rp
.source_nexthop
,
573 &nht_p
, &rp_all
->group
, 1))
574 return PIM_RP_NO_PATH
;
580 * Return if the group is already configured for this RP
582 if (pim_rp_find_exact(pim
, rp_info
->rp
.rpf_addr
.u
.prefix4
,
584 XFREE(MTYPE_PIM_RP
, rp_info
);
589 * Barf if this group is already covered by some other RP
591 tmp_rp_info
= pim_rp_find_match_group(pim
, &rp_info
->group
);
594 if (tmp_rp_info
->plist
) {
595 XFREE(MTYPE_PIM_RP
, rp_info
);
596 return PIM_GROUP_PFXLIST_OVERLAP
;
599 * If the only RP that covers this group is an
601 * 224.0.0.0/4 that is fine, ignore that one.
603 * though we must return PIM_GROUP_OVERLAP
605 if (prefix_same(&rp_info
->group
,
606 &tmp_rp_info
->group
)) {
607 XFREE(MTYPE_PIM_RP
, rp_info
);
608 return PIM_GROUP_OVERLAP
;
614 listnode_add_sort(pim
->rp_list
, rp_info
);
615 rn
= route_node_get(pim
->rp_table
, &rp_info
->group
);
618 if (PIM_DEBUG_TRACE
) {
619 char buf
[PREFIX_STRLEN
];
621 zlog_debug("Allocated: %p for rp_info: %p(%s) Lock: %d", rn
,
623 prefix2str(&rp_info
->group
, buf
, sizeof(buf
)),
627 for (ALL_LIST_ELEMENTS_RO(pim
->upstream_list
, upnode
, up
)) {
628 if (up
->sg
.src
.s_addr
== INADDR_ANY
) {
630 struct rp_info
*trp_info
;
632 grp
.family
= AF_INET
;
633 grp
.prefixlen
= IPV4_MAX_BITLEN
;
634 grp
.u
.prefix4
= up
->sg
.grp
;
635 trp_info
= pim_rp_find_match_group(pim
, &grp
);
637 if (trp_info
== rp_info
)
638 pim_upstream_update(pim
, up
);
642 pim_rp_check_interfaces(pim
, rp_info
);
643 pim_rp_refresh_group_to_rp_mapping(pim
);
645 /* Register addr with Zebra NHT */
646 nht_p
.family
= AF_INET
;
647 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
648 nht_p
.u
.prefix4
= rp_info
->rp
.rpf_addr
.u
.prefix4
;
649 if (PIM_DEBUG_PIM_NHT_RP
) {
650 char buf
[PREFIX2STR_BUFFER
];
651 char buf1
[PREFIX2STR_BUFFER
];
652 prefix2str(&nht_p
, buf
, sizeof(buf
));
653 prefix2str(&rp_info
->group
, buf1
, sizeof(buf1
));
654 zlog_debug("%s: NHT Register RP addr %s grp %s with Zebra ",
655 __PRETTY_FUNCTION__
, buf
, buf1
);
658 pim_find_or_track_nexthop(pim
, &nht_p
, NULL
, rp_info
, NULL
);
659 if (!pim_ecmp_nexthop_lookup(pim
, &rp_info
->rp
.source_nexthop
, &nht_p
,
661 return PIM_RP_NO_PATH
;
666 int pim_rp_del(struct pim_instance
*pim
, const char *rp
,
667 const char *group_range
, const char *plist
)
670 struct in_addr rp_addr
;
672 struct rp_info
*rp_info
;
673 struct rp_info
*rp_all
;
676 struct route_node
*rn
;
677 bool was_plist
= false;
678 struct rp_info
*trp_info
;
679 struct pim_upstream
*up
;
680 struct listnode
*upnode
;
682 if (group_range
== NULL
)
683 result
= str2prefix("224.0.0.0/4", &group
);
685 result
= str2prefix(group_range
, &group
);
688 return PIM_GROUP_BAD_ADDRESS
;
690 result
= inet_pton(AF_INET
, rp
, &rp_addr
);
692 return PIM_RP_BAD_ADDRESS
;
695 rp_info
= pim_rp_find_prefix_list(pim
, rp_addr
, plist
);
697 rp_info
= pim_rp_find_exact(pim
, rp_addr
, &group
);
700 return PIM_RP_NOT_FOUND
;
702 if (rp_info
->plist
) {
703 XFREE(MTYPE_PIM_FILTER_NAME
, rp_info
->plist
);
707 /* Deregister addr with Zebra NHT */
708 nht_p
.family
= AF_INET
;
709 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
710 nht_p
.u
.prefix4
= rp_info
->rp
.rpf_addr
.u
.prefix4
;
711 if (PIM_DEBUG_PIM_NHT_RP
) {
712 char buf
[PREFIX2STR_BUFFER
];
713 prefix2str(&nht_p
, buf
, sizeof(buf
));
714 zlog_debug("%s: Deregister RP addr %s with Zebra ",
715 __PRETTY_FUNCTION__
, buf
);
717 pim_delete_tracked_nexthop(pim
, &nht_p
, NULL
, rp_info
);
719 if (!str2prefix("224.0.0.0/4", &g_all
))
720 return PIM_RP_BAD_ADDRESS
;
722 rp_all
= pim_rp_find_match_group(pim
, &g_all
);
724 if (rp_all
== rp_info
) {
725 for (ALL_LIST_ELEMENTS_RO(pim
->upstream_list
, upnode
, up
)) {
726 /* Find the upstream (*, G) whose upstream address is
727 * same as the deleted RP
729 if ((up
->upstream_addr
.s_addr
== rp_addr
.s_addr
) &&
730 (up
->sg
.src
.s_addr
== INADDR_ANY
)) {
732 grp
.family
= AF_INET
;
733 grp
.prefixlen
= IPV4_MAX_BITLEN
;
734 grp
.u
.prefix4
= up
->sg
.grp
;
735 trp_info
= pim_rp_find_match_group(pim
, &grp
);
736 if (trp_info
== rp_all
) {
737 pim_upstream_rpf_clear(pim
, up
);
738 up
->upstream_addr
.s_addr
= INADDR_ANY
;
742 rp_all
->rp
.rpf_addr
.family
= AF_INET
;
743 rp_all
->rp
.rpf_addr
.u
.prefix4
.s_addr
= INADDR_NONE
;
748 listnode_delete(pim
->rp_list
, rp_info
);
751 rn
= route_node_get(pim
->rp_table
, &rp_info
->group
);
753 if (rn
->info
!= rp_info
)
756 "Expected rn->info to be equal to rp_info");
758 if (PIM_DEBUG_TRACE
) {
759 char buf
[PREFIX_STRLEN
];
762 "%s:Found for Freeing: %p for rp_info: %p(%s) Lock: %d",
763 __PRETTY_FUNCTION__
, rn
, rp_info
,
764 prefix2str(&rp_info
->group
, buf
,
769 route_unlock_node(rn
);
770 route_unlock_node(rn
);
774 pim_rp_refresh_group_to_rp_mapping(pim
);
776 for (ALL_LIST_ELEMENTS_RO(pim
->upstream_list
, upnode
, up
)) {
777 /* Find the upstream (*, G) whose upstream address is same as
780 if ((up
->upstream_addr
.s_addr
== rp_addr
.s_addr
) &&
781 (up
->sg
.src
.s_addr
== INADDR_ANY
)) {
784 grp
.family
= AF_INET
;
785 grp
.prefixlen
= IPV4_MAX_BITLEN
;
786 grp
.u
.prefix4
= up
->sg
.grp
;
788 trp_info
= pim_rp_find_match_group(pim
, &grp
);
790 /* RP not found for the group grp */
791 if (pim_rpf_addr_is_inaddr_none(&trp_info
->rp
)) {
792 pim_upstream_rpf_clear(pim
, up
);
793 pim_rp_set_upstream_addr(pim
,
795 up
->sg
.src
, up
->sg
.grp
);
798 /* RP found for the group grp */
800 pim_upstream_update(pim
, up
);
804 XFREE(MTYPE_PIM_RP
, rp_info
);
808 void pim_rp_setup(struct pim_instance
*pim
)
810 struct listnode
*node
;
811 struct rp_info
*rp_info
;
814 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
815 if (rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
== INADDR_NONE
)
818 nht_p
.family
= AF_INET
;
819 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
820 nht_p
.u
.prefix4
= rp_info
->rp
.rpf_addr
.u
.prefix4
;
822 pim_find_or_track_nexthop(pim
, &nht_p
, NULL
, rp_info
, NULL
);
823 if (!pim_ecmp_nexthop_lookup(pim
, &rp_info
->rp
.source_nexthop
,
824 &nht_p
, &rp_info
->group
, 1))
825 if (PIM_DEBUG_PIM_NHT_RP
)
827 "Unable to lookup nexthop for rp specified");
832 * Checks to see if we should elect ourself the actual RP when new if
833 * addresses are added against an interface.
835 void pim_rp_check_on_if_add(struct pim_interface
*pim_ifp
)
837 struct listnode
*node
;
838 struct rp_info
*rp_info
;
839 bool i_am_rp_changed
= false;
840 struct pim_instance
*pim
= pim_ifp
->pim
;
842 if (pim
->rp_list
== NULL
)
845 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
846 if (pim_rpf_addr_is_inaddr_none(&rp_info
->rp
))
849 /* if i_am_rp is already set nothing to be done (adding new
851 * is not going to make a difference). */
852 if (rp_info
->i_am_rp
) {
856 if (pim_rp_check_interface_addrs(rp_info
, pim_ifp
)) {
857 i_am_rp_changed
= true;
858 rp_info
->i_am_rp
= 1;
859 if (PIM_DEBUG_PIM_NHT_RP
) {
860 char rp
[PREFIX_STRLEN
];
861 pim_addr_dump("<rp?>", &rp_info
->rp
.rpf_addr
,
863 zlog_debug("%s: %s: i am rp", __func__
, rp
);
868 if (i_am_rp_changed
) {
869 pim_msdp_i_am_rp_changed(pim
);
873 /* up-optimized re-evaluation of "i_am_rp". this is used when ifaddresses
874 * are removed. Removing numbers is an uncommon event in an active network
875 * so I have made no attempt to optimize it. */
876 void pim_i_am_rp_re_evaluate(struct pim_instance
*pim
)
878 struct listnode
*node
;
879 struct rp_info
*rp_info
;
880 bool i_am_rp_changed
= false;
883 if (pim
->rp_list
== NULL
)
886 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
887 if (pim_rpf_addr_is_inaddr_none(&rp_info
->rp
))
890 old_i_am_rp
= rp_info
->i_am_rp
;
891 pim_rp_check_interfaces(pim
, rp_info
);
893 if (old_i_am_rp
!= rp_info
->i_am_rp
) {
894 i_am_rp_changed
= true;
895 if (PIM_DEBUG_PIM_NHT_RP
) {
896 char rp
[PREFIX_STRLEN
];
897 pim_addr_dump("<rp?>", &rp_info
->rp
.rpf_addr
,
899 if (rp_info
->i_am_rp
) {
900 zlog_debug("%s: %s: i am rp", __func__
,
903 zlog_debug("%s: %s: i am no longer rp",
910 if (i_am_rp_changed
) {
911 pim_msdp_i_am_rp_changed(pim
);
916 * I_am_RP(G) is true if the group-to-RP mapping indicates that
917 * this router is the RP for the group.
919 * Since we only have static RP, all groups are part of this RP
921 int pim_rp_i_am_rp(struct pim_instance
*pim
, struct in_addr group
)
924 struct rp_info
*rp_info
;
926 memset(&g
, 0, sizeof(g
));
931 rp_info
= pim_rp_find_match_group(pim
, &g
);
934 return rp_info
->i_am_rp
;
942 * Return the RP that the Group belongs too.
944 struct pim_rpf
*pim_rp_g(struct pim_instance
*pim
, struct in_addr group
)
947 struct rp_info
*rp_info
;
949 memset(&g
, 0, sizeof(g
));
954 rp_info
= pim_rp_find_match_group(pim
, &g
);
959 /* Register addr with Zebra NHT */
960 nht_p
.family
= AF_INET
;
961 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
962 nht_p
.u
.prefix4
= rp_info
->rp
.rpf_addr
.u
.prefix4
;
963 if (PIM_DEBUG_PIM_NHT_RP
) {
964 char buf
[PREFIX2STR_BUFFER
];
965 char buf1
[PREFIX2STR_BUFFER
];
966 prefix2str(&nht_p
, buf
, sizeof(buf
));
967 prefix2str(&rp_info
->group
, buf1
, sizeof(buf1
));
969 "%s: NHT Register RP addr %s grp %s with Zebra",
970 __PRETTY_FUNCTION__
, buf
, buf1
);
973 pim_find_or_track_nexthop(pim
, &nht_p
, NULL
, rp_info
, NULL
);
974 pim_rpf_set_refresh_time(pim
);
975 (void)pim_ecmp_nexthop_lookup(pim
, &rp_info
->rp
.source_nexthop
,
976 &nht_p
, &rp_info
->group
, 1);
977 return (&rp_info
->rp
);
985 * Set the upstream IP address we want to talk to based upon
986 * the rp configured and the source address
988 * If we have don't have a RP configured and the source address is *
989 * then set the upstream addr as INADDR_ANY and return failure.
992 int pim_rp_set_upstream_addr(struct pim_instance
*pim
, struct in_addr
*up
,
993 struct in_addr source
, struct in_addr group
)
995 struct rp_info
*rp_info
;
998 memset(&g
, 0, sizeof(g
));
1001 g
.u
.prefix4
= group
;
1003 rp_info
= pim_rp_find_match_group(pim
, &g
);
1005 if ((pim_rpf_addr_is_inaddr_none(&rp_info
->rp
))
1006 && (source
.s_addr
== INADDR_ANY
)) {
1007 if (PIM_DEBUG_PIM_NHT_RP
)
1008 zlog_debug("%s: Received a (*,G) with no RP configured",
1009 __PRETTY_FUNCTION__
);
1010 up
->s_addr
= INADDR_ANY
;
1014 *up
= (source
.s_addr
== INADDR_ANY
) ? rp_info
->rp
.rpf_addr
.u
.prefix4
1020 int pim_rp_config_write(struct pim_instance
*pim
, struct vty
*vty
,
1023 struct listnode
*node
;
1024 struct rp_info
*rp_info
;
1026 char group_buffer
[32];
1029 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
1030 if (pim_rpf_addr_is_inaddr_none(&rp_info
->rp
))
1034 vty_out(vty
, "%sip pim rp %s prefix-list %s\n", spaces
,
1036 &rp_info
->rp
.rpf_addr
.u
.prefix4
,
1040 vty_out(vty
, "%sip pim rp %s %s\n", spaces
,
1042 &rp_info
->rp
.rpf_addr
.u
.prefix4
,
1044 prefix2str(&rp_info
->group
, group_buffer
, 32));
1051 bool pim_rp_check_is_my_ip_address(struct pim_instance
*pim
,
1052 struct in_addr dest_addr
)
1054 if (if_lookup_exact_address(&dest_addr
, AF_INET
, pim
->vrf_id
))
1060 void pim_rp_show_information(struct pim_instance
*pim
, struct vty
*vty
, bool uj
)
1062 struct rp_info
*rp_info
;
1063 struct rp_info
*prev_rp_info
= NULL
;
1064 struct listnode
*node
;
1066 json_object
*json
= NULL
;
1067 json_object
*json_rp_rows
= NULL
;
1068 json_object
*json_row
= NULL
;
1071 json
= json_object_new_object();
1074 "RP address group/prefix-list OIF I am RP\n");
1076 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
1077 if (!pim_rpf_addr_is_inaddr_none(&rp_info
->rp
)) {
1082 * If we have moved on to a new RP then add the
1083 * entry for the previous RP
1086 && prev_rp_info
->rp
.rpf_addr
.u
.prefix4
1088 != rp_info
->rp
.rpf_addr
.u
.prefix4
1090 json_object_object_add(
1092 inet_ntoa(prev_rp_info
->rp
1096 json_rp_rows
= NULL
;
1100 json_rp_rows
= json_object_new_array();
1102 json_row
= json_object_new_object();
1103 if (rp_info
->rp
.source_nexthop
.interface
)
1104 json_object_string_add(
1105 json_row
, "outboundInterface",
1106 rp_info
->rp
.source_nexthop
1109 if (rp_info
->i_am_rp
)
1110 json_object_boolean_true_add(json_row
,
1114 json_object_string_add(json_row
,
1118 json_object_string_add(
1120 prefix2str(&rp_info
->group
, buf
,
1123 json_object_array_add(json_rp_rows
, json_row
);
1125 vty_out(vty
, "%-15s ",
1126 inet_ntoa(rp_info
->rp
.rpf_addr
.u
1130 vty_out(vty
, "%-18s ", rp_info
->plist
);
1132 vty_out(vty
, "%-18s ",
1133 prefix2str(&rp_info
->group
, buf
,
1136 if (rp_info
->rp
.source_nexthop
.interface
)
1137 vty_out(vty
, "%-16s ",
1138 rp_info
->rp
.source_nexthop
1141 vty_out(vty
, "%-16s ", "(Unknown)");
1143 if (rp_info
->i_am_rp
)
1144 vty_out(vty
, "yes\n");
1146 vty_out(vty
, "no\n");
1149 prev_rp_info
= rp_info
;
1154 if (prev_rp_info
&& json_rp_rows
)
1155 json_object_object_add(
1157 inet_ntoa(prev_rp_info
->rp
.rpf_addr
.u
.prefix4
),
1160 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
1161 json
, JSON_C_TO_STRING_PRETTY
));
1162 json_object_free(json
);
1166 void pim_resolve_rp_nh(struct pim_instance
*pim
, struct pim_neighbor
*nbr
)
1168 struct listnode
*node
= NULL
;
1169 struct rp_info
*rp_info
= NULL
;
1170 struct nexthop
*nh_node
= NULL
;
1171 struct prefix nht_p
;
1172 struct pim_nexthop_cache pnc
;
1174 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
1175 if (rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
== INADDR_NONE
)
1178 nht_p
.family
= AF_INET
;
1179 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
1180 nht_p
.u
.prefix4
= rp_info
->rp
.rpf_addr
.u
.prefix4
;
1181 memset(&pnc
, 0, sizeof(struct pim_nexthop_cache
));
1182 if (!pim_find_or_track_nexthop(pim
, &nht_p
, NULL
, rp_info
,
1186 for (nh_node
= pnc
.nexthop
; nh_node
; nh_node
= nh_node
->next
) {
1187 if (nh_node
->gate
.ipv4
.s_addr
!= 0)
1190 struct interface
*ifp1
= if_lookup_by_index(
1191 nh_node
->ifindex
, pim
->vrf_id
);
1193 if (nbr
->interface
!= ifp1
)
1196 nh_node
->gate
.ipv4
= nbr
->source_addr
;
1197 if (PIM_DEBUG_PIM_NHT_RP
) {
1198 char str
[PREFIX_STRLEN
];
1199 char str1
[INET_ADDRSTRLEN
];
1200 pim_inet4_dump("<nht_nbr?>", nbr
->source_addr
,
1201 str1
, sizeof(str1
));
1202 pim_addr_dump("<nht_addr?>", &nht_p
, str
,
1205 "%s: addr %s new nexthop addr %s interface %s",
1206 __PRETTY_FUNCTION__
, str
, str1
,