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 pim_zebra_upstream_rpf_changed(pim
, up
, &old_rpf
);
396 pim_zebra_update_all_interfaces(pim
);
399 int pim_rp_new_config(struct pim_instance
*pim
, const char *rp
,
400 const char *group_range
, const char *plist
)
404 struct in_addr rp_addr
;
406 if (group_range
== NULL
)
407 result
= str2prefix("224.0.0.0/4", &group
);
409 result
= str2prefix(group_range
, &group
);
413 prefix_copy(&temp
, &group
);
415 if (!prefix_same(&group
, &temp
))
416 return PIM_GROUP_BAD_ADDR_MASK_COMBO
;
421 return PIM_GROUP_BAD_ADDRESS
;
423 result
= inet_pton(AF_INET
, rp
, &rp_addr
);
426 return PIM_RP_BAD_ADDRESS
;
428 result
= pim_rp_new(pim
, rp_addr
, group
, plist
, RP_SRC_STATIC
);
432 int pim_rp_new(struct pim_instance
*pim
, struct in_addr rp_addr
,
433 struct prefix group
, const char *plist
,
434 enum rp_source rp_src_flag
)
437 char rp
[INET_ADDRSTRLEN
];
438 struct rp_info
*rp_info
;
439 struct rp_info
*rp_all
;
440 struct prefix group_all
;
441 struct listnode
*node
, *nnode
;
442 struct rp_info
*tmp_rp_info
;
445 struct route_node
*rn
;
446 struct pim_upstream
*up
;
447 struct listnode
*upnode
;
449 rp_info
= XCALLOC(MTYPE_PIM_RP
, sizeof(*rp_info
));
451 rp_info
->rp
.rpf_addr
.family
= AF_INET
;
452 rp_info
->rp
.rpf_addr
.prefixlen
= IPV4_MAX_PREFIXLEN
;
453 rp_info
->rp
.rpf_addr
.u
.prefix4
= rp_addr
;
454 prefix_copy(&rp_info
->group
, &group
);
455 rp_info
->rp_src
= rp_src_flag
;
457 inet_ntop(AF_INET
, &rp_info
->rp
.rpf_addr
.u
.prefix4
, rp
, sizeof(rp
));
461 * Return if the prefix-list is already configured for this RP
463 if (pim_rp_find_prefix_list(pim
, rp_info
->rp
.rpf_addr
.u
.prefix4
,
465 XFREE(MTYPE_PIM_RP
, rp_info
);
470 * Barf if the prefix-list is already configured for an RP
472 if (pim_rp_prefix_list_used(pim
, plist
)) {
473 XFREE(MTYPE_PIM_RP
, rp_info
);
474 return PIM_RP_PFXLIST_IN_USE
;
478 * Free any existing rp_info entries for this RP
480 for (ALL_LIST_ELEMENTS(pim
->rp_list
, node
, nnode
,
482 if (rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
483 == tmp_rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
) {
484 if (tmp_rp_info
->plist
)
485 pim_rp_del_config(pim
, rp
, NULL
,
490 prefix2str(&tmp_rp_info
->group
,
496 rp_info
->plist
= XSTRDUP(MTYPE_PIM_FILTER_NAME
, plist
);
499 if (!str2prefix("224.0.0.0/4", &group_all
)) {
500 XFREE(MTYPE_PIM_RP
, rp_info
);
501 return PIM_GROUP_BAD_ADDRESS
;
503 rp_all
= pim_rp_find_match_group(pim
, &group_all
);
506 * Barf if group is a non-multicast subnet
508 if (!prefix_match(&rp_all
->group
, &rp_info
->group
)) {
509 XFREE(MTYPE_PIM_RP
, rp_info
);
510 return PIM_GROUP_BAD_ADDRESS
;
514 * Remove any prefix-list rp_info entries for this RP
516 for (ALL_LIST_ELEMENTS(pim
->rp_list
, node
, nnode
,
518 if (tmp_rp_info
->plist
519 && rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
520 == tmp_rp_info
->rp
.rpf_addr
.u
.prefix4
522 pim_rp_del_config(pim
, rp
, NULL
,
528 * Take over the 224.0.0.0/4 group if the rp is INADDR_NONE
530 if (prefix_same(&rp_all
->group
, &rp_info
->group
)
531 && pim_rpf_addr_is_inaddr_none(&rp_all
->rp
)) {
532 rp_all
->rp
.rpf_addr
= rp_info
->rp
.rpf_addr
;
533 rp_all
->rp_src
= rp_src_flag
;
534 XFREE(MTYPE_PIM_RP
, rp_info
);
536 /* Register addr with Zebra NHT */
537 nht_p
.family
= AF_INET
;
538 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
540 rp_all
->rp
.rpf_addr
.u
.prefix4
; // RP address
541 if (PIM_DEBUG_PIM_NHT_RP
) {
542 char buf
[PREFIX2STR_BUFFER
];
543 char buf1
[PREFIX2STR_BUFFER
];
544 prefix2str(&nht_p
, buf
, sizeof(buf
));
545 prefix2str(&rp_all
->group
, buf1
, sizeof(buf1
));
547 "%s: NHT Register rp_all addr %s grp %s ",
548 __PRETTY_FUNCTION__
, buf
, buf1
);
551 for (ALL_LIST_ELEMENTS_RO(pim
->upstream_list
, upnode
,
553 /* Find (*, G) upstream whose RP is not
556 if ((up
->upstream_addr
.s_addr
== INADDR_ANY
)
557 && (up
->sg
.src
.s_addr
== INADDR_ANY
)) {
559 struct rp_info
*trp_info
;
561 grp
.family
= AF_INET
;
562 grp
.prefixlen
= IPV4_MAX_BITLEN
;
563 grp
.u
.prefix4
= up
->sg
.grp
;
564 trp_info
= pim_rp_find_match_group(
566 if (trp_info
== rp_all
)
567 pim_upstream_update(pim
, up
);
571 pim_rp_check_interfaces(pim
, rp_all
);
572 pim_rp_refresh_group_to_rp_mapping(pim
);
573 pim_find_or_track_nexthop(pim
, &nht_p
, NULL
, rp_all
,
576 if (!pim_ecmp_nexthop_lookup(pim
,
577 &rp_all
->rp
.source_nexthop
,
578 &nht_p
, &rp_all
->group
, 1))
579 return PIM_RP_NO_PATH
;
584 * Return if the group is already configured for this RP
586 tmp_rp_info
= pim_rp_find_exact(
587 pim
, rp_info
->rp
.rpf_addr
.u
.prefix4
, &rp_info
->group
);
589 if ((tmp_rp_info
->rp_src
!= rp_src_flag
)
590 && (rp_src_flag
== RP_SRC_STATIC
))
591 tmp_rp_info
->rp_src
= rp_src_flag
;
592 XFREE(MTYPE_PIM_RP
, rp_info
);
597 * Barf if this group is already covered by some other RP
599 tmp_rp_info
= pim_rp_find_match_group(pim
, &rp_info
->group
);
602 if (tmp_rp_info
->plist
) {
603 XFREE(MTYPE_PIM_RP
, rp_info
);
604 return PIM_GROUP_PFXLIST_OVERLAP
;
607 * If the only RP that covers this group is an
609 * 224.0.0.0/4 that is fine, ignore that one.
611 * though we must return PIM_GROUP_OVERLAP
613 if (prefix_same(&rp_info
->group
,
614 &tmp_rp_info
->group
)) {
615 if ((rp_src_flag
== RP_SRC_STATIC
)
616 && (tmp_rp_info
->rp_src
618 XFREE(MTYPE_PIM_RP
, rp_info
);
619 return PIM_GROUP_OVERLAP
;
622 result
= pim_rp_change(
624 rp_info
->rp
.rpf_addr
.u
.prefix4
,
627 XFREE(MTYPE_PIM_RP
, rp_info
);
634 listnode_add_sort(pim
->rp_list
, rp_info
);
635 rn
= route_node_get(pim
->rp_table
, &rp_info
->group
);
638 if (PIM_DEBUG_PIM_TRACE
) {
639 char buf
[PREFIX_STRLEN
];
641 zlog_debug("Allocated: %p for rp_info: %p(%s) Lock: %d", rn
,
643 prefix2str(&rp_info
->group
, buf
, sizeof(buf
)),
647 for (ALL_LIST_ELEMENTS_RO(pim
->upstream_list
, upnode
, up
)) {
648 if (up
->sg
.src
.s_addr
== INADDR_ANY
) {
650 struct rp_info
*trp_info
;
652 grp
.family
= AF_INET
;
653 grp
.prefixlen
= IPV4_MAX_BITLEN
;
654 grp
.u
.prefix4
= up
->sg
.grp
;
655 trp_info
= pim_rp_find_match_group(pim
, &grp
);
657 if (trp_info
== rp_info
)
658 pim_upstream_update(pim
, up
);
662 pim_rp_check_interfaces(pim
, rp_info
);
663 pim_rp_refresh_group_to_rp_mapping(pim
);
665 /* Register addr with Zebra NHT */
666 nht_p
.family
= AF_INET
;
667 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
668 nht_p
.u
.prefix4
= rp_info
->rp
.rpf_addr
.u
.prefix4
;
669 if (PIM_DEBUG_PIM_NHT_RP
) {
670 char buf
[PREFIX2STR_BUFFER
];
671 char buf1
[PREFIX2STR_BUFFER
];
672 prefix2str(&nht_p
, buf
, sizeof(buf
));
673 prefix2str(&rp_info
->group
, buf1
, sizeof(buf1
));
674 zlog_debug("%s: NHT Register RP addr %s grp %s with Zebra ",
675 __PRETTY_FUNCTION__
, buf
, buf1
);
677 pim_find_or_track_nexthop(pim
, &nht_p
, NULL
, rp_info
, false, NULL
);
678 if (!pim_ecmp_nexthop_lookup(pim
, &rp_info
->rp
.source_nexthop
, &nht_p
,
680 return PIM_RP_NO_PATH
;
685 int pim_rp_del_config(struct pim_instance
*pim
, const char *rp
,
686 const char *group_range
, const char *plist
)
689 struct in_addr rp_addr
;
692 if (group_range
== NULL
)
693 result
= str2prefix("224.0.0.0/4", &group
);
695 result
= str2prefix(group_range
, &group
);
698 return PIM_GROUP_BAD_ADDRESS
;
700 result
= inet_pton(AF_INET
, rp
, &rp_addr
);
702 return PIM_RP_BAD_ADDRESS
;
704 result
= pim_rp_del(pim
, rp_addr
, group
, plist
, RP_SRC_STATIC
);
708 int pim_rp_del(struct pim_instance
*pim
, struct in_addr rp_addr
,
709 struct prefix group
, const char *plist
,
710 enum rp_source rp_src_flag
)
713 struct rp_info
*rp_info
;
714 struct rp_info
*rp_all
;
716 struct route_node
*rn
;
717 bool was_plist
= false;
718 struct rp_info
*trp_info
;
719 struct pim_upstream
*up
;
720 struct listnode
*upnode
;
721 struct bsgrp_node
*bsgrp
= NULL
;
722 struct bsm_rpinfo
*bsrp
= NULL
;
723 char grp_str
[PREFIX2STR_BUFFER
];
724 char rp_str
[INET_ADDRSTRLEN
];
726 if (!inet_ntop(AF_INET
, &rp_addr
, rp_str
, sizeof(rp_str
)))
727 sprintf(rp_str
, "<rp?>");
728 prefix2str(&group
, grp_str
, sizeof(grp_str
));
731 rp_info
= pim_rp_find_prefix_list(pim
, rp_addr
, plist
);
733 rp_info
= pim_rp_find_exact(pim
, rp_addr
, &group
);
736 return PIM_RP_NOT_FOUND
;
738 if (rp_info
->plist
) {
739 XFREE(MTYPE_PIM_FILTER_NAME
, rp_info
->plist
);
743 if (PIM_DEBUG_PIM_TRACE
)
744 zlog_debug("%s: Delete RP %s for the group %s",
745 __PRETTY_FUNCTION__
, rp_str
, grp_str
);
747 /* While static RP is getting deleted, we need to check if dynamic RP
748 * present for the same group in BSM RP table, then install the dynamic
749 * RP for the group node into the main rp table
751 if (rp_src_flag
== RP_SRC_STATIC
) {
752 bsgrp
= pim_bsm_get_bsgrp_node(&pim
->global_scope
, &group
);
755 bsrp
= listnode_head(bsgrp
->bsrp_list
);
757 if (PIM_DEBUG_PIM_TRACE
) {
758 char bsrp_str
[INET_ADDRSTRLEN
];
760 if (!inet_ntop(AF_INET
, bsrp
, bsrp_str
,
762 sprintf(bsrp_str
, "<bsrp?>");
764 zlog_debug("%s: BSM RP %s found for the group %s",
768 return pim_rp_change(pim
, bsrp
->rp_address
,
772 if (PIM_DEBUG_PIM_TRACE
)
774 "%s: BSM RP not found for the group %s",
775 __PRETTY_FUNCTION__
, grp_str
);
779 /* Deregister addr with Zebra NHT */
780 nht_p
.family
= AF_INET
;
781 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
782 nht_p
.u
.prefix4
= rp_info
->rp
.rpf_addr
.u
.prefix4
;
783 if (PIM_DEBUG_PIM_NHT_RP
) {
784 char buf
[PREFIX2STR_BUFFER
];
785 prefix2str(&nht_p
, buf
, sizeof(buf
));
786 zlog_debug("%s: Deregister RP addr %s with Zebra ",
787 __PRETTY_FUNCTION__
, buf
);
789 pim_delete_tracked_nexthop(pim
, &nht_p
, NULL
, rp_info
, false);
791 if (!str2prefix("224.0.0.0/4", &g_all
))
792 return PIM_RP_BAD_ADDRESS
;
794 rp_all
= pim_rp_find_match_group(pim
, &g_all
);
796 if (rp_all
== rp_info
) {
797 for (ALL_LIST_ELEMENTS_RO(pim
->upstream_list
, upnode
, up
)) {
798 /* Find the upstream (*, G) whose upstream address is
799 * same as the deleted RP
801 if ((up
->upstream_addr
.s_addr
802 == rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
)
803 && (up
->sg
.src
.s_addr
== INADDR_ANY
)) {
805 grp
.family
= AF_INET
;
806 grp
.prefixlen
= IPV4_MAX_BITLEN
;
807 grp
.u
.prefix4
= up
->sg
.grp
;
808 trp_info
= pim_rp_find_match_group(pim
, &grp
);
809 if (trp_info
== rp_all
) {
810 pim_upstream_rpf_clear(pim
, up
);
811 up
->upstream_addr
.s_addr
= INADDR_ANY
;
815 rp_all
->rp
.rpf_addr
.family
= AF_INET
;
816 rp_all
->rp
.rpf_addr
.u
.prefix4
.s_addr
= INADDR_NONE
;
821 listnode_delete(pim
->rp_list
, rp_info
);
824 rn
= route_node_get(pim
->rp_table
, &rp_info
->group
);
826 if (rn
->info
!= rp_info
)
829 "Expected rn->info to be equal to rp_info");
831 if (PIM_DEBUG_PIM_TRACE
) {
832 char buf
[PREFIX_STRLEN
];
835 "%s:Found for Freeing: %p for rp_info: %p(%s) Lock: %d",
836 __PRETTY_FUNCTION__
, rn
, rp_info
,
837 prefix2str(&rp_info
->group
, buf
,
842 route_unlock_node(rn
);
843 route_unlock_node(rn
);
847 pim_rp_refresh_group_to_rp_mapping(pim
);
849 for (ALL_LIST_ELEMENTS_RO(pim
->upstream_list
, upnode
, up
)) {
850 /* Find the upstream (*, G) whose upstream address is same as
853 if ((up
->upstream_addr
.s_addr
854 == rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
)
855 && (up
->sg
.src
.s_addr
== INADDR_ANY
)) {
858 grp
.family
= AF_INET
;
859 grp
.prefixlen
= IPV4_MAX_BITLEN
;
860 grp
.u
.prefix4
= up
->sg
.grp
;
862 trp_info
= pim_rp_find_match_group(pim
, &grp
);
864 /* RP not found for the group grp */
865 if (pim_rpf_addr_is_inaddr_none(&trp_info
->rp
)) {
866 pim_upstream_rpf_clear(pim
, up
);
867 pim_rp_set_upstream_addr(
868 pim
, &up
->upstream_addr
, up
->sg
.src
,
872 /* RP found for the group grp */
874 pim_upstream_update(pim
, up
);
878 XFREE(MTYPE_PIM_RP
, rp_info
);
882 int pim_rp_change(struct pim_instance
*pim
, struct in_addr new_rp_addr
,
883 struct prefix group
, enum rp_source rp_src_flag
)
886 struct route_node
*rn
;
888 struct rp_info
*rp_info
= NULL
;
889 struct pim_upstream
*up
;
890 struct listnode
*upnode
;
892 rn
= route_node_lookup(pim
->rp_table
, &group
);
894 result
= pim_rp_new(pim
, new_rp_addr
, group
, NULL
, rp_src_flag
);
901 route_unlock_node(rn
);
902 result
= pim_rp_new(pim
, new_rp_addr
, group
, NULL
, rp_src_flag
);
906 if (rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
== new_rp_addr
.s_addr
) {
907 if (rp_info
->rp_src
!= rp_src_flag
) {
908 rp_info
->rp_src
= rp_src_flag
;
909 route_unlock_node(rn
);
914 nht_p
.family
= AF_INET
;
915 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
917 /* Deregister old RP addr with Zebra NHT */
918 if (rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
!= INADDR_ANY
) {
919 nht_p
.u
.prefix4
= rp_info
->rp
.rpf_addr
.u
.prefix4
;
920 if (PIM_DEBUG_PIM_NHT_RP
) {
921 char buf
[PREFIX2STR_BUFFER
];
923 prefix2str(&nht_p
, buf
, sizeof(buf
));
924 zlog_debug("%s: Deregister RP addr %s with Zebra ",
925 __PRETTY_FUNCTION__
, buf
);
927 pim_delete_tracked_nexthop(pim
, &nht_p
, NULL
, rp_info
, false);
930 pim_rp_nexthop_del(rp_info
);
931 listnode_delete(pim
->rp_list
, rp_info
);
932 /* Update the new RP address*/
933 rp_info
->rp
.rpf_addr
.u
.prefix4
= new_rp_addr
;
934 rp_info
->rp_src
= rp_src_flag
;
935 rp_info
->i_am_rp
= 0;
937 listnode_add_sort(pim
->rp_list
, rp_info
);
939 for (ALL_LIST_ELEMENTS_RO(pim
->upstream_list
, upnode
, up
)) {
940 if (up
->sg
.src
.s_addr
== INADDR_ANY
) {
942 struct rp_info
*trp_info
;
944 grp
.family
= AF_INET
;
945 grp
.prefixlen
= IPV4_MAX_BITLEN
;
946 grp
.u
.prefix4
= up
->sg
.grp
;
947 trp_info
= pim_rp_find_match_group(pim
, &grp
);
949 if (trp_info
== rp_info
)
950 pim_upstream_update(pim
, up
);
954 /* Register new RP addr with Zebra NHT */
955 nht_p
.u
.prefix4
= rp_info
->rp
.rpf_addr
.u
.prefix4
;
956 if (PIM_DEBUG_PIM_NHT_RP
) {
957 char buf
[PREFIX2STR_BUFFER
];
958 char buf1
[PREFIX2STR_BUFFER
];
960 prefix2str(&nht_p
, buf
, sizeof(buf
));
961 prefix2str(&rp_info
->group
, buf1
, sizeof(buf1
));
962 zlog_debug("%s: NHT Register RP addr %s grp %s with Zebra ",
963 __PRETTY_FUNCTION__
, buf
, buf1
);
966 pim_find_or_track_nexthop(pim
, &nht_p
, NULL
, rp_info
, false, NULL
);
967 if (!pim_ecmp_nexthop_lookup(pim
, &rp_info
->rp
.source_nexthop
, &nht_p
,
968 &rp_info
->group
, 1)) {
969 route_unlock_node(rn
);
970 return PIM_RP_NO_PATH
;
973 pim_rp_check_interfaces(pim
, rp_info
);
975 route_unlock_node(rn
);
977 pim_rp_refresh_group_to_rp_mapping(pim
);
982 void pim_rp_setup(struct pim_instance
*pim
)
984 struct listnode
*node
;
985 struct rp_info
*rp_info
;
988 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
989 if (rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
== INADDR_NONE
)
992 nht_p
.family
= AF_INET
;
993 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
994 nht_p
.u
.prefix4
= rp_info
->rp
.rpf_addr
.u
.prefix4
;
996 pim_find_or_track_nexthop(pim
, &nht_p
, NULL
, rp_info
, false,
998 if (!pim_ecmp_nexthop_lookup(pim
, &rp_info
->rp
.source_nexthop
,
999 &nht_p
, &rp_info
->group
, 1))
1000 if (PIM_DEBUG_PIM_NHT_RP
)
1002 "Unable to lookup nexthop for rp specified");
1007 * Checks to see if we should elect ourself the actual RP when new if
1008 * addresses are added against an interface.
1010 void pim_rp_check_on_if_add(struct pim_interface
*pim_ifp
)
1012 struct listnode
*node
;
1013 struct rp_info
*rp_info
;
1014 bool i_am_rp_changed
= false;
1015 struct pim_instance
*pim
= pim_ifp
->pim
;
1017 if (pim
->rp_list
== NULL
)
1020 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
1021 if (pim_rpf_addr_is_inaddr_none(&rp_info
->rp
))
1024 /* if i_am_rp is already set nothing to be done (adding new
1026 * is not going to make a difference). */
1027 if (rp_info
->i_am_rp
) {
1031 if (pim_rp_check_interface_addrs(rp_info
, pim_ifp
)) {
1032 i_am_rp_changed
= true;
1033 rp_info
->i_am_rp
= 1;
1034 if (PIM_DEBUG_PIM_NHT_RP
) {
1035 char rp
[PREFIX_STRLEN
];
1036 pim_addr_dump("<rp?>", &rp_info
->rp
.rpf_addr
,
1038 zlog_debug("%s: %s: i am rp", __func__
, rp
);
1043 if (i_am_rp_changed
) {
1044 pim_msdp_i_am_rp_changed(pim
);
1045 pim_upstream_reeval_use_rpt(pim
);
1049 /* up-optimized re-evaluation of "i_am_rp". this is used when ifaddresses
1050 * are removed. Removing numbers is an uncommon event in an active network
1051 * so I have made no attempt to optimize it. */
1052 void pim_i_am_rp_re_evaluate(struct pim_instance
*pim
)
1054 struct listnode
*node
;
1055 struct rp_info
*rp_info
;
1056 bool i_am_rp_changed
= false;
1059 if (pim
->rp_list
== NULL
)
1062 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
1063 if (pim_rpf_addr_is_inaddr_none(&rp_info
->rp
))
1066 old_i_am_rp
= rp_info
->i_am_rp
;
1067 pim_rp_check_interfaces(pim
, rp_info
);
1069 if (old_i_am_rp
!= rp_info
->i_am_rp
) {
1070 i_am_rp_changed
= true;
1071 if (PIM_DEBUG_PIM_NHT_RP
) {
1072 char rp
[PREFIX_STRLEN
];
1073 pim_addr_dump("<rp?>", &rp_info
->rp
.rpf_addr
,
1075 if (rp_info
->i_am_rp
) {
1076 zlog_debug("%s: %s: i am rp", __func__
,
1079 zlog_debug("%s: %s: i am no longer rp",
1086 if (i_am_rp_changed
) {
1087 pim_msdp_i_am_rp_changed(pim
);
1088 pim_upstream_reeval_use_rpt(pim
);
1093 * I_am_RP(G) is true if the group-to-RP mapping indicates that
1094 * this router is the RP for the group.
1096 * Since we only have static RP, all groups are part of this RP
1098 int pim_rp_i_am_rp(struct pim_instance
*pim
, struct in_addr group
)
1101 struct rp_info
*rp_info
;
1103 memset(&g
, 0, sizeof(g
));
1106 g
.u
.prefix4
= group
;
1108 rp_info
= pim_rp_find_match_group(pim
, &g
);
1111 return rp_info
->i_am_rp
;
1119 * Return the RP that the Group belongs too.
1121 struct pim_rpf
*pim_rp_g(struct pim_instance
*pim
, struct in_addr group
)
1124 struct rp_info
*rp_info
;
1126 memset(&g
, 0, sizeof(g
));
1129 g
.u
.prefix4
= group
;
1131 rp_info
= pim_rp_find_match_group(pim
, &g
);
1134 struct prefix nht_p
;
1136 /* Register addr with Zebra NHT */
1137 nht_p
.family
= AF_INET
;
1138 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
1139 nht_p
.u
.prefix4
= rp_info
->rp
.rpf_addr
.u
.prefix4
;
1140 if (PIM_DEBUG_PIM_NHT_RP
) {
1141 char buf
[PREFIX2STR_BUFFER
];
1142 char buf1
[PREFIX2STR_BUFFER
];
1143 prefix2str(&nht_p
, buf
, sizeof(buf
));
1144 prefix2str(&rp_info
->group
, buf1
, sizeof(buf1
));
1146 "%s: NHT Register RP addr %s grp %s with Zebra",
1147 __PRETTY_FUNCTION__
, buf
, buf1
);
1149 pim_find_or_track_nexthop(pim
, &nht_p
, NULL
, rp_info
, false,
1151 pim_rpf_set_refresh_time(pim
);
1152 (void)pim_ecmp_nexthop_lookup(pim
, &rp_info
->rp
.source_nexthop
,
1153 &nht_p
, &rp_info
->group
, 1);
1154 return (&rp_info
->rp
);
1162 * Set the upstream IP address we want to talk to based upon
1163 * the rp configured and the source address
1165 * If we have don't have a RP configured and the source address is *
1166 * then set the upstream addr as INADDR_ANY and return failure.
1169 int pim_rp_set_upstream_addr(struct pim_instance
*pim
, struct in_addr
*up
,
1170 struct in_addr source
, struct in_addr group
)
1172 struct rp_info
*rp_info
;
1175 memset(&g
, 0, sizeof(g
));
1178 g
.u
.prefix4
= group
;
1180 rp_info
= pim_rp_find_match_group(pim
, &g
);
1182 if ((pim_rpf_addr_is_inaddr_none(&rp_info
->rp
))
1183 && (source
.s_addr
== INADDR_ANY
)) {
1184 if (PIM_DEBUG_PIM_NHT_RP
)
1185 zlog_debug("%s: Received a (*,G) with no RP configured",
1186 __PRETTY_FUNCTION__
);
1187 up
->s_addr
= INADDR_ANY
;
1191 *up
= (source
.s_addr
== INADDR_ANY
) ? rp_info
->rp
.rpf_addr
.u
.prefix4
1197 int pim_rp_config_write(struct pim_instance
*pim
, struct vty
*vty
,
1200 struct listnode
*node
;
1201 struct rp_info
*rp_info
;
1203 char group_buffer
[32];
1206 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
1207 if (pim_rpf_addr_is_inaddr_none(&rp_info
->rp
))
1210 if (rp_info
->rp_src
== RP_SRC_BSR
)
1214 vty_out(vty
, "%sip pim rp %s prefix-list %s\n", spaces
,
1216 &rp_info
->rp
.rpf_addr
.u
.prefix4
,
1220 vty_out(vty
, "%sip pim rp %s %s\n", spaces
,
1222 &rp_info
->rp
.rpf_addr
.u
.prefix4
,
1224 prefix2str(&rp_info
->group
, group_buffer
, 32));
1231 bool pim_rp_check_is_my_ip_address(struct pim_instance
*pim
,
1232 struct in_addr dest_addr
)
1234 if (if_lookup_exact_address(&dest_addr
, AF_INET
, pim
->vrf_id
))
1240 void pim_rp_show_information(struct pim_instance
*pim
, struct vty
*vty
, bool uj
)
1242 struct rp_info
*rp_info
;
1243 struct rp_info
*prev_rp_info
= NULL
;
1244 struct listnode
*node
;
1247 json_object
*json
= NULL
;
1248 json_object
*json_rp_rows
= NULL
;
1249 json_object
*json_row
= NULL
;
1252 json
= json_object_new_object();
1255 "RP address group/prefix-list OIF I am RP Source\n");
1256 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
1257 if (!pim_rpf_addr_is_inaddr_none(&rp_info
->rp
)) {
1260 if (rp_info
->rp_src
== RP_SRC_STATIC
)
1261 strcpy(source
, "Static");
1262 else if (rp_info
->rp_src
== RP_SRC_BSR
)
1263 strcpy(source
, "BSR");
1265 strcpy(source
, "None");
1268 * If we have moved on to a new RP then add the
1269 * entry for the previous RP
1272 && prev_rp_info
->rp
.rpf_addr
.u
.prefix4
1274 != rp_info
->rp
.rpf_addr
.u
.prefix4
1276 json_object_object_add(
1278 inet_ntoa(prev_rp_info
->rp
1282 json_rp_rows
= NULL
;
1286 json_rp_rows
= json_object_new_array();
1288 json_row
= json_object_new_object();
1289 if (rp_info
->rp
.source_nexthop
.interface
)
1290 json_object_string_add(
1291 json_row
, "outboundInterface",
1292 rp_info
->rp
.source_nexthop
1295 if (rp_info
->i_am_rp
)
1296 json_object_boolean_true_add(json_row
,
1300 json_object_string_add(json_row
,
1304 json_object_string_add(
1306 prefix2str(&rp_info
->group
, buf
,
1308 json_object_string_add(json_row
, "source",
1311 json_object_array_add(json_rp_rows
, json_row
);
1313 vty_out(vty
, "%-15s ",
1314 inet_ntoa(rp_info
->rp
.rpf_addr
.u
1318 vty_out(vty
, "%-18s ", rp_info
->plist
);
1320 vty_out(vty
, "%-18s ",
1321 prefix2str(&rp_info
->group
, buf
,
1324 if (rp_info
->rp
.source_nexthop
.interface
)
1325 vty_out(vty
, "%-16s ",
1326 rp_info
->rp
.source_nexthop
1329 vty_out(vty
, "%-16s ", "(Unknown)");
1331 if (rp_info
->i_am_rp
)
1332 vty_out(vty
, "yes\n");
1336 vty_out(vty
, "%14s\n", source
);
1338 prev_rp_info
= rp_info
;
1343 if (prev_rp_info
&& json_rp_rows
)
1344 json_object_object_add(
1346 inet_ntoa(prev_rp_info
->rp
.rpf_addr
.u
.prefix4
),
1349 vty_out(vty
, "%s\n", json_object_to_json_string_ext(
1350 json
, JSON_C_TO_STRING_PRETTY
));
1351 json_object_free(json
);
1355 void pim_resolve_rp_nh(struct pim_instance
*pim
, struct pim_neighbor
*nbr
)
1357 struct listnode
*node
= NULL
;
1358 struct rp_info
*rp_info
= NULL
;
1359 struct nexthop
*nh_node
= NULL
;
1360 struct prefix nht_p
;
1361 struct pim_nexthop_cache pnc
;
1363 for (ALL_LIST_ELEMENTS_RO(pim
->rp_list
, node
, rp_info
)) {
1364 if (rp_info
->rp
.rpf_addr
.u
.prefix4
.s_addr
== INADDR_NONE
)
1367 nht_p
.family
= AF_INET
;
1368 nht_p
.prefixlen
= IPV4_MAX_BITLEN
;
1369 nht_p
.u
.prefix4
= rp_info
->rp
.rpf_addr
.u
.prefix4
;
1370 memset(&pnc
, 0, sizeof(struct pim_nexthop_cache
));
1371 if (!pim_find_or_track_nexthop(pim
, &nht_p
, NULL
, rp_info
,
1375 for (nh_node
= pnc
.nexthop
; nh_node
; nh_node
= nh_node
->next
) {
1376 if (nh_node
->gate
.ipv4
.s_addr
!= 0)
1379 struct interface
*ifp1
= if_lookup_by_index(
1380 nh_node
->ifindex
, pim
->vrf_id
);
1382 if (nbr
->interface
!= ifp1
)
1385 nh_node
->gate
.ipv4
= nbr
->source_addr
;
1386 if (PIM_DEBUG_PIM_NHT_RP
) {
1387 char str
[PREFIX_STRLEN
];
1388 char str1
[INET_ADDRSTRLEN
];
1389 pim_inet4_dump("<nht_nbr?>", nbr
->source_addr
,
1390 str1
, sizeof(str1
));
1391 pim_addr_dump("<nht_addr?>", &nht_p
, str
,
1394 "%s: addr %s new nexthop addr %s interface %s",
1395 __PRETTY_FUNCTION__
, str
, str1
,