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"
53 /* Cleanup pim->rpf_hash each node data */
54 void pim_rp_list_hash_clean(void *data
)
56 struct pim_nexthop_cache
*pnc
= (struct pim_nexthop_cache
*)data
;
58 list_delete(&pnc
->rp_list
);
60 hash_clean(pnc
->upstream_hash
, NULL
);
61 hash_free(pnc
->upstream_hash
);
62 pnc
->upstream_hash
= NULL
;
64 nexthops_free(pnc
->nexthop
);
66 XFREE(MTYPE_PIM_NEXTHOP_CACHE
, pnc
);
69 static void pim_rp_info_free(struct rp_info
*rp_info
)
71 XFREE(MTYPE_PIM_FILTER_NAME
, rp_info
->plist
);
73 XFREE(MTYPE_PIM_RP
, rp_info
);
76 int pim_rp_list_cmp(void *v1
, void *v2
)
78 struct rp_info
*rp1
= (struct rp_info
*)v1
;
79 struct rp_info
*rp2
= (struct rp_info
*)v2
;
82 * Sort by RP IP address
84 if (rp1
->rp
.rpf_addr
.u
.prefix4
.s_addr
85 < rp2
->rp
.rpf_addr
.u
.prefix4
.s_addr
)
88 if (rp1
->rp
.rpf_addr
.u
.prefix4
.s_addr
89 > rp2
->rp
.rpf_addr
.u
.prefix4
.s_addr
)
93 * Sort by group IP address
95 if (rp1
->group
.u
.prefix4
.s_addr
< rp2
->group
.u
.prefix4
.s_addr
)
98 if (rp1
->group
.u
.prefix4
.s_addr
> rp2
->group
.u
.prefix4
.s_addr
)
104 void pim_rp_init(struct pim_instance
*pim
)
106 struct rp_info
*rp_info
;
107 struct route_node
*rn
;
109 pim
->rp_list
= list_new();
110 pim
->rp_list
->del
= (void (*)(void *))pim_rp_info_free
;
111 pim
->rp_list
->cmp
= pim_rp_list_cmp
;
113 pim
->rp_table
= route_table_init();
115 rp_info
= XCALLOC(MTYPE_PIM_RP
, sizeof(*rp_info
));
117 if (!str2prefix("224.0.0.0/4", &rp_info
->group
)) {
118 flog_err(EC_LIB_DEVELOPMENT
,
119 "Unable to convert 224.0.0.0/4 to prefix");
120 list_delete(&pim
->rp_list
);
121 route_table_finish(pim
->rp_table
);
122 XFREE(MTYPE_PIM_RP
, rp_info
);
125 rp_info
->group
.family
= AF_INET
;
126 rp_info
->rp
.rpf_addr
.family
= AF_INET
;
127 rp_info
->rp
.rpf_addr
.prefixlen
= IPV4_MAX_PREFIXLEN
;
128 rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
= INADDR_NONE
;
130 listnode_add(pim
->rp_list
, rp_info
);
132 rn
= route_node_get(pim
->rp_table
, &rp_info
->group
);
134 if (PIM_DEBUG_PIM_TRACE
)
136 "Allocated: %p for rp_info: %p(224.0.0.0/4) Lock: %d",
137 rn
, rp_info
, rn
->lock
);
140 void pim_rp_free(struct pim_instance
*pim
)
143 list_delete(&pim
->rp_list
);
147 * Given an RP's prefix-list, return the RP's rp_info for that prefix-list
149 static struct rp_info
*pim_rp_find_prefix_list(struct pim_instance
*pim
,
153 struct listnode
*node
;
154 struct rp_info
*rp_info
;
156 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
157 if (rp
.s_addr
== rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
158 && rp_info
->plist
&& strcmp(rp_info
->plist
, plist
) == 0) {
167 * Return true if plist is used by any rp_info
169 static int pim_rp_prefix_list_used(struct pim_instance
*pim
, const char *plist
)
171 struct listnode
*node
;
172 struct rp_info
*rp_info
;
174 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
175 if (rp_info
->plist
&& strcmp(rp_info
->plist
, plist
) == 0) {
184 * Given an RP's address, return the RP's rp_info that is an exact match for
187 static struct rp_info
*pim_rp_find_exact(struct pim_instance
*pim
,
189 const struct prefix
*group
)
191 struct listnode
*node
;
192 struct rp_info
*rp_info
;
194 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
195 if (rp
.s_addr
== rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
196 && prefix_same(&rp_info
->group
, group
))
204 * Given a group, return the rp_info for that group
206 struct rp_info
*pim_rp_find_match_group(struct pim_instance
*pim
,
207 const struct prefix
*group
)
209 struct listnode
*node
;
210 struct rp_info
*best
= NULL
;
211 struct rp_info
*rp_info
;
212 struct prefix_list
*plist
;
213 const struct prefix
*p
, *bp
;
214 struct route_node
*rn
;
217 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
218 if (rp_info
->plist
) {
219 plist
= prefix_list_lookup(AFI_IP
, rp_info
->plist
);
221 if (prefix_list_apply_which_prefix(plist
, &p
, group
)
231 if (bp
&& bp
->prefixlen
< p
->prefixlen
) {
238 rn
= route_node_match(pim
->rp_table
, group
);
242 "%s: BUG We should have found default group information\n",
243 __PRETTY_FUNCTION__
);
248 if (PIM_DEBUG_PIM_TRACE
) {
249 char buf
[PREFIX_STRLEN
];
251 route_unlock_node(rn
);
252 zlog_debug("Lookedup: %p for rp_info: %p(%s) Lock: %d", rn
,
254 prefix2str(&rp_info
->group
, buf
, sizeof(buf
)),
261 if (rp_info
->group
.prefixlen
< best
->group
.prefixlen
)
268 * When the user makes "ip pim rp" configuration changes or if they change the
269 * prefix-list(s) used by these statements we must tickle the upstream state
270 * for each group to make them re-lookup who their RP should be.
272 * This is a placeholder function for now.
274 static void pim_rp_refresh_group_to_rp_mapping(struct pim_instance
*pim
)
276 pim_msdp_i_am_rp_changed(pim
);
277 pim_upstream_reeval_use_rpt(pim
);
280 void pim_rp_prefix_list_update(struct pim_instance
*pim
,
281 struct prefix_list
*plist
)
283 struct listnode
*node
;
284 struct rp_info
*rp_info
;
285 int refresh_needed
= 0;
287 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
289 && strcmp(rp_info
->plist
, prefix_list_name(plist
)) == 0) {
296 pim_rp_refresh_group_to_rp_mapping(pim
);
299 static int pim_rp_check_interface_addrs(struct rp_info
*rp_info
,
300 struct pim_interface
*pim_ifp
)
302 struct listnode
*node
;
303 struct pim_secondary_addr
*sec_addr
;
305 if (pim_ifp
->primary_address
.s_addr
306 == rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
)
309 if (!pim_ifp
->sec_addr_list
) {
313 for (ALL_LIST_ELEMENTS_RO(pim_ifp
->sec_addr_list
, node
, sec_addr
)) {
314 if (prefix_same(&sec_addr
->addr
, &rp_info
->rp
.rpf_addr
)) {
322 static void pim_rp_check_interfaces(struct pim_instance
*pim
,
323 struct rp_info
*rp_info
)
325 struct interface
*ifp
;
327 rp_info
->i_am_rp
= 0;
328 FOR_ALL_INTERFACES (pim
->vrf
, ifp
) {
329 struct pim_interface
*pim_ifp
= ifp
->info
;
334 if (pim_rp_check_interface_addrs(rp_info
, pim_ifp
)) {
335 rp_info
->i_am_rp
= 1;
340 void pim_upstream_update(struct pim_instance
*pim
, struct pim_upstream
*up
)
342 struct pim_rpf old_rpf
;
343 enum pim_rpf_result rpf_result
;
344 struct in_addr old_upstream_addr
;
345 struct in_addr new_upstream_addr
;
348 old_upstream_addr
= up
->upstream_addr
;
349 pim_rp_set_upstream_addr(pim
, &new_upstream_addr
, up
->sg
.src
,
352 if (PIM_DEBUG_PIM_TRACE
)
353 zlog_debug("%s: pim upstream update for old upstream %s",
355 inet_ntoa(old_upstream_addr
));
357 if (old_upstream_addr
.s_addr
== new_upstream_addr
.s_addr
)
360 /* Lets consider a case, where a PIM upstream has a better RP as a
361 * result of a new RP configuration with more precise group range.
362 * This upstream has to be added to the upstream hash of new RP's
363 * NHT(pnc) and has to be removed from old RP's NHT upstream hash
365 if (old_upstream_addr
.s_addr
!= INADDR_ANY
) {
366 /* Deregister addr with Zebra NHT */
367 nht_p
.family
= AF_INET
;
368 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
369 nht_p
.u
.prefix4
= old_upstream_addr
;
370 if (PIM_DEBUG_PIM_TRACE
) {
371 char buf
[PREFIX2STR_BUFFER
];
373 prefix2str(&nht_p
, buf
, sizeof(buf
));
374 zlog_debug("%s: Deregister upstream %s addr %s with Zebra NHT",
375 __PRETTY_FUNCTION__
, up
->sg_str
, buf
);
377 pim_delete_tracked_nexthop(pim
, &nht_p
, up
, NULL
, false);
380 /* Update the upstream address */
381 up
->upstream_addr
= new_upstream_addr
;
383 old_rpf
.source_nexthop
.interface
= up
->rpf
.source_nexthop
.interface
;
385 rpf_result
= pim_rpf_update(pim
, up
, &old_rpf
, __func__
);
386 if (rpf_result
== PIM_RPF_FAILURE
)
387 pim_mroute_del(up
->channel_oil
, __PRETTY_FUNCTION__
);
389 /* update kernel multicast forwarding cache (MFC) */
390 if (up
->rpf
.source_nexthop
.interface
&& up
->channel_oil
)
391 pim_upstream_mroute_iif_update(up
->channel_oil
, __func__
);
393 if (rpf_result
== PIM_RPF_CHANGED
||
394 (rpf_result
== PIM_RPF_FAILURE
&&
395 old_rpf
.source_nexthop
.interface
))
396 pim_zebra_upstream_rpf_changed(pim
, up
, &old_rpf
);
398 pim_zebra_update_all_interfaces(pim
);
401 int pim_rp_new_config(struct pim_instance
*pim
, const char *rp
,
402 const char *group_range
, const char *plist
)
406 struct in_addr rp_addr
;
408 if (group_range
== NULL
)
409 result
= str2prefix("224.0.0.0/4", &group
);
411 result
= str2prefix(group_range
, &group
);
415 prefix_copy(&temp
, &group
);
417 if (!prefix_same(&group
, &temp
))
418 return PIM_GROUP_BAD_ADDR_MASK_COMBO
;
423 return PIM_GROUP_BAD_ADDRESS
;
425 result
= inet_pton(AF_INET
, rp
, &rp_addr
);
428 return PIM_RP_BAD_ADDRESS
;
430 result
= pim_rp_new(pim
, rp_addr
, group
, plist
, RP_SRC_STATIC
);
434 int pim_rp_new(struct pim_instance
*pim
, struct in_addr rp_addr
,
435 struct prefix group
, const char *plist
,
436 enum rp_source rp_src_flag
)
439 char rp
[INET_ADDRSTRLEN
];
440 struct rp_info
*rp_info
;
441 struct rp_info
*rp_all
;
442 struct prefix group_all
;
443 struct listnode
*node
, *nnode
;
444 struct rp_info
*tmp_rp_info
;
447 struct route_node
*rn
;
448 struct pim_upstream
*up
;
450 if (rp_addr
.s_addr
== INADDR_ANY
||
451 rp_addr
.s_addr
== INADDR_NONE
)
452 return PIM_RP_BAD_ADDRESS
;
454 rp_info
= XCALLOC(MTYPE_PIM_RP
, sizeof(*rp_info
));
456 rp_info
->rp
.rpf_addr
.family
= AF_INET
;
457 rp_info
->rp
.rpf_addr
.prefixlen
= IPV4_MAX_PREFIXLEN
;
458 rp_info
->rp
.rpf_addr
.u
.prefix4
= rp_addr
;
459 prefix_copy(&rp_info
->group
, &group
);
460 rp_info
->rp_src
= rp_src_flag
;
462 inet_ntop(AF_INET
, &rp_info
->rp
.rpf_addr
.u
.prefix4
, rp
, sizeof(rp
));
466 * Return if the prefix-list is already configured for this RP
468 if (pim_rp_find_prefix_list(pim
, rp_info
->rp
.rpf_addr
.u
.prefix4
,
470 XFREE(MTYPE_PIM_RP
, rp_info
);
475 * Barf if the prefix-list is already configured for an RP
477 if (pim_rp_prefix_list_used(pim
, plist
)) {
478 XFREE(MTYPE_PIM_RP
, rp_info
);
479 return PIM_RP_PFXLIST_IN_USE
;
483 * Free any existing rp_info entries for this RP
485 for (ALL_LIST_ELEMENTS(pim
->rp_list
, node
, nnode
,
487 if (rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
488 == tmp_rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
) {
489 if (tmp_rp_info
->plist
)
490 pim_rp_del_config(pim
, rp
, NULL
,
495 prefix2str(&tmp_rp_info
->group
,
501 rp_info
->plist
= XSTRDUP(MTYPE_PIM_FILTER_NAME
, plist
);
504 if (!str2prefix("224.0.0.0/4", &group_all
)) {
505 XFREE(MTYPE_PIM_RP
, rp_info
);
506 return PIM_GROUP_BAD_ADDRESS
;
508 rp_all
= pim_rp_find_match_group(pim
, &group_all
);
511 * Barf if group is a non-multicast subnet
513 if (!prefix_match(&rp_all
->group
, &rp_info
->group
)) {
514 XFREE(MTYPE_PIM_RP
, rp_info
);
515 return PIM_GROUP_BAD_ADDRESS
;
519 * Remove any prefix-list rp_info entries for this RP
521 for (ALL_LIST_ELEMENTS(pim
->rp_list
, node
, nnode
,
523 if (tmp_rp_info
->plist
524 && rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
525 == tmp_rp_info
->rp
.rpf_addr
.u
.prefix4
527 pim_rp_del_config(pim
, rp
, NULL
,
533 * Take over the 224.0.0.0/4 group if the rp is INADDR_NONE
535 if (prefix_same(&rp_all
->group
, &rp_info
->group
)
536 && pim_rpf_addr_is_inaddr_none(&rp_all
->rp
)) {
537 rp_all
->rp
.rpf_addr
= rp_info
->rp
.rpf_addr
;
538 rp_all
->rp_src
= rp_src_flag
;
539 XFREE(MTYPE_PIM_RP
, rp_info
);
541 /* Register addr with Zebra NHT */
542 nht_p
.family
= AF_INET
;
543 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
545 rp_all
->rp
.rpf_addr
.u
.prefix4
; // RP address
546 if (PIM_DEBUG_PIM_NHT_RP
) {
547 char buf
[PREFIX2STR_BUFFER
];
548 char buf1
[PREFIX2STR_BUFFER
];
549 prefix2str(&nht_p
, buf
, sizeof(buf
));
550 prefix2str(&rp_all
->group
, buf1
, sizeof(buf1
));
552 "%s: NHT Register rp_all addr %s grp %s ",
553 __PRETTY_FUNCTION__
, buf
, buf1
);
556 frr_each (rb_pim_upstream
, &pim
->upstream_head
, up
) {
557 /* Find (*, G) upstream whose RP is not
560 if ((up
->upstream_addr
.s_addr
== INADDR_ANY
)
561 && (up
->sg
.src
.s_addr
== INADDR_ANY
)) {
563 struct rp_info
*trp_info
;
565 grp
.family
= AF_INET
;
566 grp
.prefixlen
= IPV4_MAX_BITLEN
;
567 grp
.u
.prefix4
= up
->sg
.grp
;
568 trp_info
= pim_rp_find_match_group(
570 if (trp_info
== rp_all
)
571 pim_upstream_update(pim
, up
);
575 pim_rp_check_interfaces(pim
, rp_all
);
576 pim_rp_refresh_group_to_rp_mapping(pim
);
577 pim_find_or_track_nexthop(pim
, &nht_p
, NULL
, rp_all
,
580 if (!pim_ecmp_nexthop_lookup(pim
,
581 &rp_all
->rp
.source_nexthop
,
582 &nht_p
, &rp_all
->group
, 1))
583 return PIM_RP_NO_PATH
;
588 * Return if the group is already configured for this RP
590 tmp_rp_info
= pim_rp_find_exact(
591 pim
, rp_info
->rp
.rpf_addr
.u
.prefix4
, &rp_info
->group
);
593 if ((tmp_rp_info
->rp_src
!= rp_src_flag
)
594 && (rp_src_flag
== RP_SRC_STATIC
))
595 tmp_rp_info
->rp_src
= rp_src_flag
;
596 XFREE(MTYPE_PIM_RP
, rp_info
);
601 * Barf if this group is already covered by some other RP
603 tmp_rp_info
= pim_rp_find_match_group(pim
, &rp_info
->group
);
606 if (tmp_rp_info
->plist
) {
607 XFREE(MTYPE_PIM_RP
, rp_info
);
608 return PIM_GROUP_PFXLIST_OVERLAP
;
611 * If the only RP that covers this group is an
613 * 224.0.0.0/4 that is fine, ignore that one.
615 * though we must return PIM_GROUP_OVERLAP
617 if (prefix_same(&rp_info
->group
,
618 &tmp_rp_info
->group
)) {
619 if ((rp_src_flag
== RP_SRC_STATIC
)
620 && (tmp_rp_info
->rp_src
622 XFREE(MTYPE_PIM_RP
, rp_info
);
623 return PIM_GROUP_OVERLAP
;
626 result
= pim_rp_change(
628 rp_info
->rp
.rpf_addr
.u
.prefix4
,
631 XFREE(MTYPE_PIM_RP
, rp_info
);
638 listnode_add_sort(pim
->rp_list
, rp_info
);
639 rn
= route_node_get(pim
->rp_table
, &rp_info
->group
);
642 if (PIM_DEBUG_PIM_TRACE
) {
643 char buf
[PREFIX_STRLEN
];
645 zlog_debug("Allocated: %p for rp_info: %p(%s) Lock: %d", rn
,
647 prefix2str(&rp_info
->group
, buf
, sizeof(buf
)),
651 frr_each (rb_pim_upstream
, &pim
->upstream_head
, up
) {
652 if (up
->sg
.src
.s_addr
== INADDR_ANY
) {
654 struct rp_info
*trp_info
;
656 grp
.family
= AF_INET
;
657 grp
.prefixlen
= IPV4_MAX_BITLEN
;
658 grp
.u
.prefix4
= up
->sg
.grp
;
659 trp_info
= pim_rp_find_match_group(pim
, &grp
);
661 if (trp_info
== rp_info
)
662 pim_upstream_update(pim
, up
);
666 pim_rp_check_interfaces(pim
, rp_info
);
667 pim_rp_refresh_group_to_rp_mapping(pim
);
669 /* Register addr with Zebra NHT */
670 nht_p
.family
= AF_INET
;
671 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
672 nht_p
.u
.prefix4
= rp_info
->rp
.rpf_addr
.u
.prefix4
;
673 if (PIM_DEBUG_PIM_NHT_RP
) {
674 char buf
[PREFIX2STR_BUFFER
];
675 char buf1
[PREFIX2STR_BUFFER
];
676 prefix2str(&nht_p
, buf
, sizeof(buf
));
677 prefix2str(&rp_info
->group
, buf1
, sizeof(buf1
));
678 zlog_debug("%s: NHT Register RP addr %s grp %s with Zebra ",
679 __PRETTY_FUNCTION__
, buf
, buf1
);
681 pim_find_or_track_nexthop(pim
, &nht_p
, NULL
, rp_info
, false, NULL
);
682 if (!pim_ecmp_nexthop_lookup(pim
, &rp_info
->rp
.source_nexthop
, &nht_p
,
684 return PIM_RP_NO_PATH
;
689 int pim_rp_del_config(struct pim_instance
*pim
, const char *rp
,
690 const char *group_range
, const char *plist
)
693 struct in_addr rp_addr
;
696 if (group_range
== NULL
)
697 result
= str2prefix("224.0.0.0/4", &group
);
699 result
= str2prefix(group_range
, &group
);
702 return PIM_GROUP_BAD_ADDRESS
;
704 result
= inet_pton(AF_INET
, rp
, &rp_addr
);
706 return PIM_RP_BAD_ADDRESS
;
708 result
= pim_rp_del(pim
, rp_addr
, group
, plist
, RP_SRC_STATIC
);
712 int pim_rp_del(struct pim_instance
*pim
, struct in_addr rp_addr
,
713 struct prefix group
, const char *plist
,
714 enum rp_source rp_src_flag
)
717 struct rp_info
*rp_info
;
718 struct rp_info
*rp_all
;
720 struct route_node
*rn
;
721 bool was_plist
= false;
722 struct rp_info
*trp_info
;
723 struct pim_upstream
*up
;
724 struct bsgrp_node
*bsgrp
= NULL
;
725 struct bsm_rpinfo
*bsrp
= NULL
;
726 char grp_str
[PREFIX2STR_BUFFER
];
727 char rp_str
[INET_ADDRSTRLEN
];
729 if (!inet_ntop(AF_INET
, &rp_addr
, rp_str
, sizeof(rp_str
)))
730 sprintf(rp_str
, "<rp?>");
731 prefix2str(&group
, grp_str
, sizeof(grp_str
));
734 rp_info
= pim_rp_find_prefix_list(pim
, rp_addr
, plist
);
736 rp_info
= pim_rp_find_exact(pim
, rp_addr
, &group
);
739 return PIM_RP_NOT_FOUND
;
741 if (rp_info
->plist
) {
742 XFREE(MTYPE_PIM_FILTER_NAME
, rp_info
->plist
);
746 if (PIM_DEBUG_PIM_TRACE
)
747 zlog_debug("%s: Delete RP %s for the group %s",
748 __PRETTY_FUNCTION__
, rp_str
, grp_str
);
750 /* While static RP is getting deleted, we need to check if dynamic RP
751 * present for the same group in BSM RP table, then install the dynamic
752 * RP for the group node into the main rp table
754 if (rp_src_flag
== RP_SRC_STATIC
) {
755 bsgrp
= pim_bsm_get_bsgrp_node(&pim
->global_scope
, &group
);
758 bsrp
= listnode_head(bsgrp
->bsrp_list
);
760 if (PIM_DEBUG_PIM_TRACE
) {
761 char bsrp_str
[INET_ADDRSTRLEN
];
763 if (!inet_ntop(AF_INET
, bsrp
, bsrp_str
,
765 sprintf(bsrp_str
, "<bsrp?>");
767 zlog_debug("%s: BSM RP %s found for the group %s",
771 return pim_rp_change(pim
, bsrp
->rp_address
,
775 if (PIM_DEBUG_PIM_TRACE
)
777 "%s: BSM RP not found for the group %s",
778 __PRETTY_FUNCTION__
, grp_str
);
782 /* Deregister addr with Zebra NHT */
783 nht_p
.family
= AF_INET
;
784 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
785 nht_p
.u
.prefix4
= rp_info
->rp
.rpf_addr
.u
.prefix4
;
786 if (PIM_DEBUG_PIM_NHT_RP
) {
787 char buf
[PREFIX2STR_BUFFER
];
788 prefix2str(&nht_p
, buf
, sizeof(buf
));
789 zlog_debug("%s: Deregister RP addr %s with Zebra ",
790 __PRETTY_FUNCTION__
, buf
);
792 pim_delete_tracked_nexthop(pim
, &nht_p
, NULL
, rp_info
, false);
794 if (!str2prefix("224.0.0.0/4", &g_all
))
795 return PIM_RP_BAD_ADDRESS
;
797 rp_all
= pim_rp_find_match_group(pim
, &g_all
);
799 if (rp_all
== rp_info
) {
800 frr_each (rb_pim_upstream
, &pim
->upstream_head
, up
) {
801 /* Find the upstream (*, G) whose upstream address is
802 * same as the deleted RP
804 if ((up
->upstream_addr
.s_addr
805 == rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
)
806 && (up
->sg
.src
.s_addr
== INADDR_ANY
)) {
808 grp
.family
= AF_INET
;
809 grp
.prefixlen
= IPV4_MAX_BITLEN
;
810 grp
.u
.prefix4
= up
->sg
.grp
;
811 trp_info
= pim_rp_find_match_group(pim
, &grp
);
812 if (trp_info
== rp_all
) {
813 pim_upstream_rpf_clear(pim
, up
);
814 up
->upstream_addr
.s_addr
= INADDR_ANY
;
818 rp_all
->rp
.rpf_addr
.family
= AF_INET
;
819 rp_all
->rp
.rpf_addr
.u
.prefix4
.s_addr
= INADDR_NONE
;
824 listnode_delete(pim
->rp_list
, rp_info
);
827 rn
= route_node_get(pim
->rp_table
, &rp_info
->group
);
829 if (rn
->info
!= rp_info
)
832 "Expected rn->info to be equal to rp_info");
834 if (PIM_DEBUG_PIM_TRACE
) {
835 char buf
[PREFIX_STRLEN
];
838 "%s:Found for Freeing: %p for rp_info: %p(%s) Lock: %d",
839 __PRETTY_FUNCTION__
, rn
, rp_info
,
840 prefix2str(&rp_info
->group
, buf
,
845 route_unlock_node(rn
);
846 route_unlock_node(rn
);
850 pim_rp_refresh_group_to_rp_mapping(pim
);
852 frr_each (rb_pim_upstream
, &pim
->upstream_head
, up
) {
853 /* Find the upstream (*, G) whose upstream address is same as
856 if ((up
->upstream_addr
.s_addr
857 == rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
)
858 && (up
->sg
.src
.s_addr
== INADDR_ANY
)) {
861 grp
.family
= AF_INET
;
862 grp
.prefixlen
= IPV4_MAX_BITLEN
;
863 grp
.u
.prefix4
= up
->sg
.grp
;
865 trp_info
= pim_rp_find_match_group(pim
, &grp
);
867 /* RP not found for the group grp */
868 if (pim_rpf_addr_is_inaddr_none(&trp_info
->rp
)) {
869 pim_upstream_rpf_clear(pim
, up
);
870 pim_rp_set_upstream_addr(
871 pim
, &up
->upstream_addr
, up
->sg
.src
,
875 /* RP found for the group grp */
877 pim_upstream_update(pim
, up
);
881 XFREE(MTYPE_PIM_RP
, rp_info
);
885 int pim_rp_change(struct pim_instance
*pim
, struct in_addr new_rp_addr
,
886 struct prefix group
, enum rp_source rp_src_flag
)
889 struct route_node
*rn
;
891 struct rp_info
*rp_info
= NULL
;
892 struct pim_upstream
*up
;
894 rn
= route_node_lookup(pim
->rp_table
, &group
);
896 result
= pim_rp_new(pim
, new_rp_addr
, group
, NULL
, rp_src_flag
);
903 route_unlock_node(rn
);
904 result
= pim_rp_new(pim
, new_rp_addr
, group
, NULL
, rp_src_flag
);
908 if (rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
== new_rp_addr
.s_addr
) {
909 if (rp_info
->rp_src
!= rp_src_flag
) {
910 rp_info
->rp_src
= rp_src_flag
;
911 route_unlock_node(rn
);
916 nht_p
.family
= AF_INET
;
917 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
919 /* Deregister old RP addr with Zebra NHT */
920 if (rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
!= INADDR_ANY
) {
921 nht_p
.u
.prefix4
= rp_info
->rp
.rpf_addr
.u
.prefix4
;
922 if (PIM_DEBUG_PIM_NHT_RP
) {
923 char buf
[PREFIX2STR_BUFFER
];
925 prefix2str(&nht_p
, buf
, sizeof(buf
));
926 zlog_debug("%s: Deregister RP addr %s with Zebra ",
927 __PRETTY_FUNCTION__
, buf
);
929 pim_delete_tracked_nexthop(pim
, &nht_p
, NULL
, rp_info
, false);
932 pim_rp_nexthop_del(rp_info
);
933 listnode_delete(pim
->rp_list
, rp_info
);
934 /* Update the new RP address*/
935 rp_info
->rp
.rpf_addr
.u
.prefix4
= new_rp_addr
;
936 rp_info
->rp_src
= rp_src_flag
;
937 rp_info
->i_am_rp
= 0;
939 listnode_add_sort(pim
->rp_list
, rp_info
);
941 frr_each (rb_pim_upstream
, &pim
->upstream_head
, up
) {
942 if (up
->sg
.src
.s_addr
== INADDR_ANY
) {
944 struct rp_info
*trp_info
;
946 grp
.family
= AF_INET
;
947 grp
.prefixlen
= IPV4_MAX_BITLEN
;
948 grp
.u
.prefix4
= up
->sg
.grp
;
949 trp_info
= pim_rp_find_match_group(pim
, &grp
);
951 if (trp_info
== rp_info
)
952 pim_upstream_update(pim
, up
);
956 /* Register new RP addr with Zebra NHT */
957 nht_p
.u
.prefix4
= rp_info
->rp
.rpf_addr
.u
.prefix4
;
958 if (PIM_DEBUG_PIM_NHT_RP
) {
959 char buf
[PREFIX2STR_BUFFER
];
960 char buf1
[PREFIX2STR_BUFFER
];
962 prefix2str(&nht_p
, buf
, sizeof(buf
));
963 prefix2str(&rp_info
->group
, buf1
, sizeof(buf1
));
964 zlog_debug("%s: NHT Register RP addr %s grp %s with Zebra ",
965 __PRETTY_FUNCTION__
, buf
, buf1
);
968 pim_find_or_track_nexthop(pim
, &nht_p
, NULL
, rp_info
, false, NULL
);
969 if (!pim_ecmp_nexthop_lookup(pim
, &rp_info
->rp
.source_nexthop
, &nht_p
,
970 &rp_info
->group
, 1)) {
971 route_unlock_node(rn
);
972 return PIM_RP_NO_PATH
;
975 pim_rp_check_interfaces(pim
, rp_info
);
977 route_unlock_node(rn
);
979 pim_rp_refresh_group_to_rp_mapping(pim
);
984 void pim_rp_setup(struct pim_instance
*pim
)
986 struct listnode
*node
;
987 struct rp_info
*rp_info
;
990 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
991 if (rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
== INADDR_NONE
)
994 nht_p
.family
= AF_INET
;
995 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
996 nht_p
.u
.prefix4
= rp_info
->rp
.rpf_addr
.u
.prefix4
;
998 pim_find_or_track_nexthop(pim
, &nht_p
, NULL
, rp_info
, false,
1000 if (!pim_ecmp_nexthop_lookup(pim
, &rp_info
->rp
.source_nexthop
,
1001 &nht_p
, &rp_info
->group
, 1))
1002 if (PIM_DEBUG_PIM_NHT_RP
)
1004 "Unable to lookup nexthop for rp specified");
1009 * Checks to see if we should elect ourself the actual RP when new if
1010 * addresses are added against an interface.
1012 void pim_rp_check_on_if_add(struct pim_interface
*pim_ifp
)
1014 struct listnode
*node
;
1015 struct rp_info
*rp_info
;
1016 bool i_am_rp_changed
= false;
1017 struct pim_instance
*pim
= pim_ifp
->pim
;
1019 if (pim
->rp_list
== NULL
)
1022 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
1023 if (pim_rpf_addr_is_inaddr_none(&rp_info
->rp
))
1026 /* if i_am_rp is already set nothing to be done (adding new
1028 * is not going to make a difference). */
1029 if (rp_info
->i_am_rp
) {
1033 if (pim_rp_check_interface_addrs(rp_info
, pim_ifp
)) {
1034 i_am_rp_changed
= true;
1035 rp_info
->i_am_rp
= 1;
1036 if (PIM_DEBUG_PIM_NHT_RP
) {
1037 char rp
[PREFIX_STRLEN
];
1038 pim_addr_dump("<rp?>", &rp_info
->rp
.rpf_addr
,
1040 zlog_debug("%s: %s: i am rp", __func__
, rp
);
1045 if (i_am_rp_changed
) {
1046 pim_msdp_i_am_rp_changed(pim
);
1047 pim_upstream_reeval_use_rpt(pim
);
1051 /* up-optimized re-evaluation of "i_am_rp". this is used when ifaddresses
1052 * are removed. Removing numbers is an uncommon event in an active network
1053 * so I have made no attempt to optimize it. */
1054 void pim_i_am_rp_re_evaluate(struct pim_instance
*pim
)
1056 struct listnode
*node
;
1057 struct rp_info
*rp_info
;
1058 bool i_am_rp_changed
= false;
1061 if (pim
->rp_list
== NULL
)
1064 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
1065 if (pim_rpf_addr_is_inaddr_none(&rp_info
->rp
))
1068 old_i_am_rp
= rp_info
->i_am_rp
;
1069 pim_rp_check_interfaces(pim
, rp_info
);
1071 if (old_i_am_rp
!= rp_info
->i_am_rp
) {
1072 i_am_rp_changed
= true;
1073 if (PIM_DEBUG_PIM_NHT_RP
) {
1074 char rp
[PREFIX_STRLEN
];
1075 pim_addr_dump("<rp?>", &rp_info
->rp
.rpf_addr
,
1077 if (rp_info
->i_am_rp
) {
1078 zlog_debug("%s: %s: i am rp", __func__
,
1081 zlog_debug("%s: %s: i am no longer rp",
1088 if (i_am_rp_changed
) {
1089 pim_msdp_i_am_rp_changed(pim
);
1090 pim_upstream_reeval_use_rpt(pim
);
1095 * I_am_RP(G) is true if the group-to-RP mapping indicates that
1096 * this router is the RP for the group.
1098 * Since we only have static RP, all groups are part of this RP
1100 int pim_rp_i_am_rp(struct pim_instance
*pim
, struct in_addr group
)
1103 struct rp_info
*rp_info
;
1105 memset(&g
, 0, sizeof(g
));
1108 g
.u
.prefix4
= group
;
1110 rp_info
= pim_rp_find_match_group(pim
, &g
);
1113 return rp_info
->i_am_rp
;
1121 * Return the RP that the Group belongs too.
1123 struct pim_rpf
*pim_rp_g(struct pim_instance
*pim
, struct in_addr group
)
1126 struct rp_info
*rp_info
;
1128 memset(&g
, 0, sizeof(g
));
1131 g
.u
.prefix4
= group
;
1133 rp_info
= pim_rp_find_match_group(pim
, &g
);
1136 struct prefix nht_p
;
1138 /* Register addr with Zebra NHT */
1139 nht_p
.family
= AF_INET
;
1140 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
1141 nht_p
.u
.prefix4
= rp_info
->rp
.rpf_addr
.u
.prefix4
;
1142 if (PIM_DEBUG_PIM_NHT_RP
) {
1143 char buf
[PREFIX2STR_BUFFER
];
1144 char buf1
[PREFIX2STR_BUFFER
];
1145 prefix2str(&nht_p
, buf
, sizeof(buf
));
1146 prefix2str(&rp_info
->group
, buf1
, sizeof(buf1
));
1148 "%s: NHT Register RP addr %s grp %s with Zebra",
1149 __PRETTY_FUNCTION__
, buf
, buf1
);
1151 pim_find_or_track_nexthop(pim
, &nht_p
, NULL
, rp_info
, false,
1153 pim_rpf_set_refresh_time(pim
);
1154 (void)pim_ecmp_nexthop_lookup(pim
, &rp_info
->rp
.source_nexthop
,
1155 &nht_p
, &rp_info
->group
, 1);
1156 return (&rp_info
->rp
);
1164 * Set the upstream IP address we want to talk to based upon
1165 * the rp configured and the source address
1167 * If we have don't have a RP configured and the source address is *
1168 * then set the upstream addr as INADDR_ANY and return failure.
1171 int pim_rp_set_upstream_addr(struct pim_instance
*pim
, struct in_addr
*up
,
1172 struct in_addr source
, struct in_addr group
)
1174 struct rp_info
*rp_info
;
1177 memset(&g
, 0, sizeof(g
));
1180 g
.u
.prefix4
= group
;
1182 rp_info
= pim_rp_find_match_group(pim
, &g
);
1184 if (!rp_info
|| ((pim_rpf_addr_is_inaddr_none(&rp_info
->rp
))
1185 && (source
.s_addr
== INADDR_ANY
))) {
1186 if (PIM_DEBUG_PIM_NHT_RP
)
1187 zlog_debug("%s: Received a (*,G) with no RP configured",
1188 __PRETTY_FUNCTION__
);
1189 up
->s_addr
= INADDR_ANY
;
1193 *up
= (source
.s_addr
== INADDR_ANY
) ? rp_info
->rp
.rpf_addr
.u
.prefix4
1199 int pim_rp_config_write(struct pim_instance
*pim
, struct vty
*vty
,
1202 struct listnode
*node
;
1203 struct rp_info
*rp_info
;
1205 char group_buffer
[32];
1208 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
1209 if (pim_rpf_addr_is_inaddr_none(&rp_info
->rp
))
1212 if (rp_info
->rp_src
== RP_SRC_BSR
)
1216 vty_out(vty
, "%sip pim rp %s prefix-list %s\n", spaces
,
1218 &rp_info
->rp
.rpf_addr
.u
.prefix4
,
1222 vty_out(vty
, "%sip pim rp %s %s\n", spaces
,
1224 &rp_info
->rp
.rpf_addr
.u
.prefix4
,
1226 prefix2str(&rp_info
->group
, group_buffer
, 32));
1233 bool pim_rp_check_is_my_ip_address(struct pim_instance
*pim
,
1234 struct in_addr dest_addr
)
1236 if (if_lookup_exact_address(&dest_addr
, AF_INET
, pim
->vrf_id
))
1242 void pim_rp_show_information(struct pim_instance
*pim
, struct vty
*vty
, bool uj
)
1244 struct rp_info
*rp_info
;
1245 struct rp_info
*prev_rp_info
= NULL
;
1246 struct listnode
*node
;
1249 json_object
*json
= NULL
;
1250 json_object
*json_rp_rows
= NULL
;
1251 json_object
*json_row
= NULL
;
1254 json
= json_object_new_object();
1257 "RP address group/prefix-list OIF I am RP Source\n");
1258 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
1259 if (!pim_rpf_addr_is_inaddr_none(&rp_info
->rp
)) {
1262 if (rp_info
->rp_src
== RP_SRC_STATIC
)
1263 strcpy(source
, "Static");
1264 else if (rp_info
->rp_src
== RP_SRC_BSR
)
1265 strcpy(source
, "BSR");
1267 strcpy(source
, "None");
1270 * If we have moved on to a new RP then add the
1271 * entry for the previous RP
1274 && prev_rp_info
->rp
.rpf_addr
.u
.prefix4
1276 != rp_info
->rp
.rpf_addr
.u
.prefix4
1278 json_object_object_add(
1280 inet_ntoa(prev_rp_info
->rp
1284 json_rp_rows
= NULL
;
1288 json_rp_rows
= json_object_new_array();
1290 json_row
= json_object_new_object();
1291 if (rp_info
->rp
.source_nexthop
.interface
)
1292 json_object_string_add(
1293 json_row
, "outboundInterface",
1294 rp_info
->rp
.source_nexthop
1297 if (rp_info
->i_am_rp
)
1298 json_object_boolean_true_add(json_row
,
1302 json_object_string_add(json_row
,
1306 json_object_string_add(
1308 prefix2str(&rp_info
->group
, buf
,
1310 json_object_string_add(json_row
, "source",
1313 json_object_array_add(json_rp_rows
, json_row
);
1315 vty_out(vty
, "%-15s ",
1316 inet_ntoa(rp_info
->rp
.rpf_addr
.u
1320 vty_out(vty
, "%-18s ", rp_info
->plist
);
1322 vty_out(vty
, "%-18s ",
1323 prefix2str(&rp_info
->group
, buf
,
1326 if (rp_info
->rp
.source_nexthop
.interface
)
1327 vty_out(vty
, "%-16s ",
1328 rp_info
->rp
.source_nexthop
1331 vty_out(vty
, "%-16s ", "(Unknown)");
1333 if (rp_info
->i_am_rp
)
1334 vty_out(vty
, "yes");
1338 vty_out(vty
, "%14s\n", source
);
1340 prev_rp_info
= rp_info
;
1345 if (prev_rp_info
&& json_rp_rows
)
1346 json_object_object_add(
1348 inet_ntoa(prev_rp_info
->rp
.rpf_addr
.u
.prefix4
),
1351 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
1352 json
, JSON_C_TO_STRING_PRETTY
));
1353 json_object_free(json
);
1357 void pim_resolve_rp_nh(struct pim_instance
*pim
, struct pim_neighbor
*nbr
)
1359 struct listnode
*node
= NULL
;
1360 struct rp_info
*rp_info
= NULL
;
1361 struct nexthop
*nh_node
= NULL
;
1362 struct prefix nht_p
;
1363 struct pim_nexthop_cache pnc
;
1365 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
1366 if (rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
== INADDR_NONE
)
1369 nht_p
.family
= AF_INET
;
1370 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
1371 nht_p
.u
.prefix4
= rp_info
->rp
.rpf_addr
.u
.prefix4
;
1372 memset(&pnc
, 0, sizeof(struct pim_nexthop_cache
));
1373 if (!pim_find_or_track_nexthop(pim
, &nht_p
, NULL
, rp_info
,
1377 for (nh_node
= pnc
.nexthop
; nh_node
; nh_node
= nh_node
->next
) {
1378 if (nh_node
->gate
.ipv4
.s_addr
!= 0)
1381 struct interface
*ifp1
= if_lookup_by_index(
1382 nh_node
->ifindex
, pim
->vrf_id
);
1384 if (nbr
->interface
!= ifp1
)
1387 nh_node
->gate
.ipv4
= nbr
->source_addr
;
1388 if (PIM_DEBUG_PIM_NHT_RP
) {
1389 char str
[PREFIX_STRLEN
];
1390 char str1
[INET_ADDRSTRLEN
];
1391 pim_inet4_dump("<nht_nbr?>", nbr
->source_addr
,
1392 str1
, sizeof(str1
));
1393 pim_addr_dump("<nht_addr?>", &nht_p
, str
,
1396 "%s: addr %s new nexthop addr %s interface %s",
1397 __PRETTY_FUNCTION__
, str
, str1
,