3 * Copyright (C) 2008 Everton da Silva Marques
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include "pim_iface.h"
34 #include "pim_ifchannel.h"
35 #include "pim_zebra.h"
41 #include "pim_macro.h"
43 #include "pim_upstream.h"
47 RB_GENERATE(pim_ifchannel_rb
, pim_ifchannel
, pim_ifp_rb
, pim_ifchannel_compare
);
49 int pim_ifchannel_compare(const struct pim_ifchannel
*ch1
,
50 const struct pim_ifchannel
*ch2
)
52 struct pim_interface
*pim_ifp1
;
53 struct pim_interface
*pim_ifp2
;
55 pim_ifp1
= ch1
->interface
->info
;
56 pim_ifp2
= ch2
->interface
->info
;
58 if (pim_ifp1
->mroute_vif_index
< pim_ifp2
->mroute_vif_index
)
61 if (pim_ifp1
->mroute_vif_index
> pim_ifp2
->mroute_vif_index
)
64 if (ntohl(ch1
->sg
.grp
.s_addr
) < ntohl(ch2
->sg
.grp
.s_addr
))
67 if (ntohl(ch1
->sg
.grp
.s_addr
) > ntohl(ch2
->sg
.grp
.s_addr
))
70 if (ntohl(ch1
->sg
.src
.s_addr
) < ntohl(ch2
->sg
.src
.s_addr
))
73 if (ntohl(ch1
->sg
.src
.s_addr
) > ntohl(ch2
->sg
.src
.s_addr
))
80 * A (*,G) or a (*,*) is going away
81 * remove the parent pointer from
82 * those pointing at us
84 static void pim_ifchannel_remove_children(struct pim_ifchannel
*ch
)
86 struct pim_ifchannel
*child
;
91 while (!list_isempty(ch
->sources
)) {
92 child
= listnode_head(ch
->sources
);
94 listnode_delete(ch
->sources
, child
);
99 * A (*,G) or a (*,*) is being created
100 * find all the children that would point
103 static void pim_ifchannel_find_new_children(struct pim_ifchannel
*ch
)
105 struct pim_interface
*pim_ifp
= ch
->interface
->info
;
106 struct pim_ifchannel
*child
;
108 // Basic Sanity that we are not being silly
109 if ((ch
->sg
.src
.s_addr
!= INADDR_ANY
)
110 && (ch
->sg
.grp
.s_addr
!= INADDR_ANY
))
113 if ((ch
->sg
.src
.s_addr
== INADDR_ANY
)
114 && (ch
->sg
.grp
.s_addr
== INADDR_ANY
))
117 RB_FOREACH (child
, pim_ifchannel_rb
, &pim_ifp
->ifchannel_rb
) {
118 if ((ch
->sg
.grp
.s_addr
!= INADDR_ANY
)
119 && (child
->sg
.grp
.s_addr
== ch
->sg
.grp
.s_addr
)
122 listnode_add_sort(ch
->sources
, child
);
127 void pim_ifchannel_delete(struct pim_ifchannel
*ch
)
129 struct pim_interface
*pim_ifp
;
131 pim_ifp
= ch
->interface
->info
;
133 if (ch
->upstream
->channel_oil
) {
134 uint32_t mask
= PIM_OIF_FLAG_PROTO_PIM
;
135 if (ch
->upstream
->flags
& PIM_UPSTREAM_FLAG_MASK_SRC_IGMP
)
136 mask
|= PIM_OIF_FLAG_PROTO_IGMP
;
139 * A S,G RPT channel can have an empty oil, we also
140 * need to take into account the fact that a ifchannel
141 * might have been suppressing a *,G ifchannel from
142 * being inherited. So let's figure out what
143 * needs to be done here
145 if ((ch
->sg
.src
.s_addr
!= INADDR_ANY
) &&
146 pim_upstream_evaluate_join_desired_interface(
147 ch
->upstream
, ch
, ch
->parent
))
148 pim_channel_add_oif(ch
->upstream
->channel_oil
,
150 PIM_OIF_FLAG_PROTO_STAR
,
153 pim_channel_del_oif(ch
->upstream
->channel_oil
,
154 ch
->interface
, mask
, __func__
);
156 * Do we have any S,G's that are inheriting?
157 * Nuke from on high too.
159 if (ch
->upstream
->sources
) {
160 struct pim_upstream
*child
;
161 struct listnode
*up_node
;
163 for (ALL_LIST_ELEMENTS_RO(ch
->upstream
->sources
,
165 pim_channel_del_inherited_oif(
173 * When this channel is removed
174 * we need to find all our children
175 * and make sure our pointers are fixed
177 pim_ifchannel_remove_children(ch
);
180 list_delete(&ch
->sources
);
182 listnode_delete(ch
->upstream
->ifchannels
, ch
);
184 pim_upstream_update_join_desired(pim_ifp
->pim
, ch
->upstream
);
186 /* upstream is common across ifchannels, check if upstream's
187 ifchannel list is empty before deleting upstream_del
188 ref count will take care of it.
190 if (ch
->upstream
->ref_count
> 0)
191 pim_upstream_del(pim_ifp
->pim
, ch
->upstream
, __func__
);
194 if (PIM_DEBUG_PIM_TRACE
)
196 "%s: Avoiding deletion of upstream with ref_count %d "
197 "from ifchannel(%s): %s",
198 __func__
, ch
->upstream
->ref_count
,
199 ch
->interface
->name
, ch
->sg_str
);
204 THREAD_OFF(ch
->t_ifjoin_expiry_timer
);
205 THREAD_OFF(ch
->t_ifjoin_prune_pending_timer
);
206 THREAD_OFF(ch
->t_ifassert_timer
);
209 listnode_delete(ch
->parent
->sources
, ch
);
213 RB_REMOVE(pim_ifchannel_rb
, &pim_ifp
->ifchannel_rb
, ch
);
215 if (PIM_DEBUG_PIM_TRACE
)
216 zlog_debug("%s: ifchannel entry %s is deleted ", __func__
,
219 XFREE(MTYPE_PIM_IFCHANNEL
, ch
);
222 void pim_ifchannel_delete_all(struct interface
*ifp
)
224 struct pim_interface
*pim_ifp
;
225 struct pim_ifchannel
*ch
;
231 while (!RB_EMPTY(pim_ifchannel_rb
, &pim_ifp
->ifchannel_rb
)) {
232 ch
= RB_ROOT(pim_ifchannel_rb
, &pim_ifp
->ifchannel_rb
);
234 pim_ifchannel_ifjoin_switch(__func__
, ch
, PIM_IFJOIN_NOINFO
);
235 pim_ifchannel_delete(ch
);
239 static void delete_on_noinfo(struct pim_ifchannel
*ch
)
241 if (ch
->local_ifmembership
== PIM_IFMEMBERSHIP_NOINFO
242 && ch
->ifjoin_state
== PIM_IFJOIN_NOINFO
243 && ch
->t_ifjoin_expiry_timer
== NULL
)
244 pim_ifchannel_delete(ch
);
247 void pim_ifchannel_ifjoin_switch(const char *caller
, struct pim_ifchannel
*ch
,
248 enum pim_ifjoin_state new_state
)
250 enum pim_ifjoin_state old_state
= ch
->ifjoin_state
;
251 struct pim_interface
*pim_ifp
= ch
->interface
->info
;
252 struct pim_ifchannel
*child_ch
;
254 if (PIM_DEBUG_PIM_EVENTS
)
256 "PIM_IFCHANNEL(%s): %s is switching from %s to %s",
257 ch
->interface
->name
, ch
->sg_str
,
258 pim_ifchannel_ifjoin_name(ch
->ifjoin_state
, ch
->flags
),
259 pim_ifchannel_ifjoin_name(new_state
, 0));
262 if (old_state
== new_state
) {
263 if (PIM_DEBUG_PIM_EVENTS
) {
265 "%s calledby %s: non-transition on state %d (%s)",
266 __func__
, caller
, new_state
,
267 pim_ifchannel_ifjoin_name(new_state
, 0));
272 ch
->ifjoin_state
= new_state
;
274 if (ch
->sg
.src
.s_addr
== INADDR_ANY
) {
275 struct pim_upstream
*up
= ch
->upstream
;
276 struct pim_upstream
*child
;
277 struct listnode
*up_node
;
280 if (ch
->ifjoin_state
== PIM_IFJOIN_NOINFO
) {
281 for (ALL_LIST_ELEMENTS_RO(up
->sources
, up_node
,
283 struct channel_oil
*c_oil
=
286 if (PIM_DEBUG_PIM_TRACE
)
288 "%s %s: Prune(S,G)=%s from %s",
296 * If the S,G has no if channel and the
298 * has output here then the *,G was
299 * supplying the implied
300 * if channel. So remove it.
302 if (c_oil
->oil
.mfcc_ttls
303 [pim_ifp
->mroute_vif_index
])
304 pim_channel_del_inherited_oif(
305 c_oil
, ch
->interface
,
309 if (ch
->ifjoin_state
== PIM_IFJOIN_JOIN
) {
310 for (ALL_LIST_ELEMENTS_RO(up
->sources
, up_node
,
312 if (PIM_DEBUG_PIM_TRACE
)
314 "%s %s: Join(S,G)=%s from %s",
319 /* check if the channel can be
320 * inherited into the SG's OIL
322 child_ch
= pim_ifchannel_find(
325 if (pim_upstream_eval_inherit_if(
326 child
, child_ch
, ch
)) {
330 PIM_OIF_FLAG_PROTO_STAR
,
332 pim_upstream_update_join_desired(
333 pim_ifp
->pim
, child
);
339 /* Transition to/from NOINFO ? */
340 if ((old_state
== PIM_IFJOIN_NOINFO
)
341 || (new_state
== PIM_IFJOIN_NOINFO
)) {
343 if (PIM_DEBUG_PIM_EVENTS
) {
344 zlog_debug("PIM_IFCHANNEL_%s: (S,G)=%s on interface %s",
345 ((new_state
== PIM_IFJOIN_NOINFO
) ? "DOWN"
347 ch
->sg_str
, ch
->interface
->name
);
351 Record uptime of state transition to/from NOINFO
353 ch
->ifjoin_creation
= pim_time_monotonic_sec();
355 pim_upstream_update_join_desired(pim_ifp
->pim
, ch
->upstream
);
356 pim_ifchannel_update_could_assert(ch
);
357 pim_ifchannel_update_assert_tracking_desired(ch
);
361 const char *pim_ifchannel_ifjoin_name(enum pim_ifjoin_state ifjoin_state
,
364 switch (ifjoin_state
) {
365 case PIM_IFJOIN_NOINFO
:
366 if (PIM_IF_FLAG_TEST_S_G_RPT(flags
))
370 case PIM_IFJOIN_JOIN
:
372 case PIM_IFJOIN_PRUNE
:
373 if (PIM_IF_FLAG_TEST_S_G_RPT(flags
))
377 case PIM_IFJOIN_PRUNE_PENDING
:
378 if (PIM_IF_FLAG_TEST_S_G_RPT(flags
))
382 case PIM_IFJOIN_PRUNE_TMP
:
383 if (PIM_IF_FLAG_TEST_S_G_RPT(flags
))
387 case PIM_IFJOIN_PRUNE_PENDING_TMP
:
388 if (PIM_IF_FLAG_TEST_S_G_RPT(flags
))
394 return "ifjoin_bad_state";
397 const char *pim_ifchannel_ifassert_name(enum pim_ifassert_state ifassert_state
)
399 switch (ifassert_state
) {
400 case PIM_IFASSERT_NOINFO
:
402 case PIM_IFASSERT_I_AM_WINNER
:
404 case PIM_IFASSERT_I_AM_LOSER
:
408 return "ifassert_bad_state";
412 RFC 4601: 4.6.5. Assert State Macros
414 AssertWinner(S,G,I) defaults to NULL and AssertWinnerMetric(S,G,I)
415 defaults to Infinity when in the NoInfo state.
417 void reset_ifassert_state(struct pim_ifchannel
*ch
)
419 struct in_addr any
= {.s_addr
= INADDR_ANY
};
421 THREAD_OFF(ch
->t_ifassert_timer
);
423 pim_ifassert_winner_set(ch
, PIM_IFASSERT_NOINFO
, any
,
424 router
->infinite_assert_metric
);
427 struct pim_ifchannel
*pim_ifchannel_find(struct interface
*ifp
,
428 struct prefix_sg
*sg
)
430 struct pim_interface
*pim_ifp
;
431 struct pim_ifchannel
*ch
;
432 struct pim_ifchannel lookup
;
437 zlog_warn("%s: (S,G)=%s: multicast not enabled on interface %s",
438 __func__
, pim_str_sg_dump(sg
), ifp
->name
);
443 lookup
.interface
= ifp
;
444 ch
= RB_FIND(pim_ifchannel_rb
, &pim_ifp
->ifchannel_rb
, &lookup
);
449 static void ifmembership_set(struct pim_ifchannel
*ch
,
450 enum pim_ifmembership membership
)
452 struct pim_interface
*pim_ifp
= ch
->interface
->info
;
454 if (ch
->local_ifmembership
== membership
)
457 if (PIM_DEBUG_PIM_EVENTS
) {
458 zlog_debug("%s: (S,G)=%s membership now is %s on interface %s",
459 __func__
, ch
->sg_str
,
460 membership
== PIM_IFMEMBERSHIP_INCLUDE
? "INCLUDE"
462 ch
->interface
->name
);
465 ch
->local_ifmembership
= membership
;
467 pim_upstream_update_join_desired(pim_ifp
->pim
, ch
->upstream
);
468 pim_ifchannel_update_could_assert(ch
);
469 pim_ifchannel_update_assert_tracking_desired(ch
);
473 void pim_ifchannel_membership_clear(struct interface
*ifp
)
475 struct pim_interface
*pim_ifp
;
476 struct pim_ifchannel
*ch
;
481 RB_FOREACH (ch
, pim_ifchannel_rb
, &pim_ifp
->ifchannel_rb
)
482 ifmembership_set(ch
, PIM_IFMEMBERSHIP_NOINFO
);
485 void pim_ifchannel_delete_on_noinfo(struct interface
*ifp
)
487 struct pim_interface
*pim_ifp
;
488 struct pim_ifchannel
*ch
, *ch_tmp
;
493 RB_FOREACH_SAFE (ch
, pim_ifchannel_rb
, &pim_ifp
->ifchannel_rb
, ch_tmp
)
494 delete_on_noinfo(ch
);
498 * For a given Interface, if we are given a S,G
499 * Find the *,G (If we have it).
500 * If we are passed a *,G, find the *,* ifchannel
503 static struct pim_ifchannel
*pim_ifchannel_find_parent(struct pim_ifchannel
*ch
)
505 struct prefix_sg parent_sg
= ch
->sg
;
506 struct pim_ifchannel
*parent
= NULL
;
509 if ((parent_sg
.src
.s_addr
!= INADDR_ANY
)
510 && (parent_sg
.grp
.s_addr
!= INADDR_ANY
)) {
511 parent_sg
.src
.s_addr
= INADDR_ANY
;
512 parent
= pim_ifchannel_find(ch
->interface
, &parent_sg
);
515 listnode_add(parent
->sources
, ch
);
522 struct pim_ifchannel
*pim_ifchannel_add(struct interface
*ifp
,
523 struct prefix_sg
*sg
,
524 uint8_t source_flags
, int up_flags
)
526 struct pim_interface
*pim_ifp
;
527 struct pim_ifchannel
*ch
;
528 struct pim_upstream
*up
;
530 ch
= pim_ifchannel_find(ifp
, sg
);
536 ch
= XCALLOC(MTYPE_PIM_IFCHANNEL
, sizeof(*ch
));
539 if ((source_flags
& PIM_ENCODE_RPT_BIT
)
540 && !(source_flags
& PIM_ENCODE_WC_BIT
))
541 PIM_IF_FLAG_SET_S_G_RPT(ch
->flags
);
545 pim_str_sg_set(sg
, ch
->sg_str
);
546 ch
->parent
= pim_ifchannel_find_parent(ch
);
547 if (ch
->sg
.src
.s_addr
== INADDR_ANY
) {
548 ch
->sources
= list_new();
550 (int (*)(void *, void *))pim_ifchannel_compare
;
554 pim_ifchannel_find_new_children(ch
);
555 ch
->local_ifmembership
= PIM_IFMEMBERSHIP_NOINFO
;
557 ch
->ifjoin_state
= PIM_IFJOIN_NOINFO
;
558 ch
->t_ifjoin_expiry_timer
= NULL
;
559 ch
->t_ifjoin_prune_pending_timer
= NULL
;
560 ch
->ifjoin_creation
= 0;
562 RB_INSERT(pim_ifchannel_rb
, &pim_ifp
->ifchannel_rb
, ch
);
564 up
= pim_upstream_add(pim_ifp
->pim
, sg
, NULL
, up_flags
, __func__
, ch
);
568 listnode_add_sort(up
->ifchannels
, ch
);
570 ch
->ifassert_my_metric
= pim_macro_ch_my_assert_metric_eval(ch
);
571 ch
->ifassert_winner_metric
= pim_macro_ch_my_assert_metric_eval(ch
);
573 ch
->ifassert_winner
.s_addr
= INADDR_ANY
;
576 ch
->t_ifassert_timer
= NULL
;
577 ch
->ifassert_state
= PIM_IFASSERT_NOINFO
;
578 reset_ifassert_state(ch
);
579 if (pim_macro_ch_could_assert_eval(ch
))
580 PIM_IF_FLAG_SET_COULD_ASSERT(ch
->flags
);
582 PIM_IF_FLAG_UNSET_COULD_ASSERT(ch
->flags
);
584 if (pim_macro_assert_tracking_desired_eval(ch
))
585 PIM_IF_FLAG_SET_ASSERT_TRACKING_DESIRED(ch
->flags
);
587 PIM_IF_FLAG_UNSET_ASSERT_TRACKING_DESIRED(ch
->flags
);
589 if (PIM_DEBUG_PIM_TRACE
)
590 zlog_debug("%s: ifchannel %s is created ", __func__
,
596 static void ifjoin_to_noinfo(struct pim_ifchannel
*ch
, bool ch_del
)
598 pim_forward_stop(ch
, !ch_del
);
599 pim_ifchannel_ifjoin_switch(__func__
, ch
, PIM_IFJOIN_NOINFO
);
601 delete_on_noinfo(ch
);
604 static int on_ifjoin_expiry_timer(struct thread
*t
)
606 struct pim_ifchannel
*ch
;
610 if (PIM_DEBUG_PIM_TRACE
)
611 zlog_debug("%s: ifchannel %s expiry timer", __func__
,
614 ifjoin_to_noinfo(ch
, true);
615 /* ch may have been deleted */
620 static int on_ifjoin_prune_pending_timer(struct thread
*t
)
622 struct pim_ifchannel
*ch
;
623 int send_prune_echo
; /* boolean */
624 struct interface
*ifp
;
625 struct pim_interface
*pim_ifp
;
629 if (PIM_DEBUG_PIM_TRACE
)
631 "%s: IFCHANNEL%s %s Prune Pending Timer Popped",
632 __func__
, pim_str_sg_dump(&ch
->sg
),
633 pim_ifchannel_ifjoin_name(ch
->ifjoin_state
, ch
->flags
));
635 if (ch
->ifjoin_state
== PIM_IFJOIN_PRUNE_PENDING
) {
638 if (!PIM_IF_FLAG_TEST_S_G_RPT(ch
->flags
)) {
639 /* Send PruneEcho(S,G) ? */
641 (listcount(pim_ifp
->pim_neighbor_list
) > 1);
643 if (send_prune_echo
) {
646 rpf
.source_nexthop
.interface
= ifp
;
647 rpf
.rpf_addr
.u
.prefix4
=
648 pim_ifp
->primary_address
;
649 pim_jp_agg_single_upstream_send(
650 &rpf
, ch
->upstream
, 0);
653 ifjoin_to_noinfo(ch
, true);
655 /* If SGRpt flag is set on ifchannel, Trigger SGRpt
656 * message on RP path upon prune timer expiry.
658 ch
->ifjoin_state
= PIM_IFJOIN_PRUNE
;
660 struct pim_upstream
*parent
=
661 ch
->upstream
->parent
;
663 pim_upstream_update_join_desired(pim_ifp
->pim
,
666 pim_jp_agg_single_upstream_send(&parent
->rpf
,
670 /* from here ch may have been deleted */
676 static void check_recv_upstream(int is_join
, struct interface
*recv_ifp
,
677 struct in_addr upstream
, struct prefix_sg
*sg
,
678 uint8_t source_flags
, int holdtime
)
680 struct pim_upstream
*up
;
681 struct pim_interface
*pim_ifp
= recv_ifp
->info
;
683 /* Upstream (S,G) in Joined state ? */
684 up
= pim_upstream_find(pim_ifp
->pim
, sg
);
687 if (up
->join_state
!= PIM_UPSTREAM_JOINED
)
690 /* Upstream (S,G) in Joined state */
692 if (pim_rpf_addr_is_inaddr_any(&up
->rpf
)) {
693 /* RPF'(S,G) not found */
694 zlog_warn("%s %s: RPF'%s not found", __FILE__
, __func__
,
699 /* upstream directed to RPF'(S,G) ? */
700 if (upstream
.s_addr
!= up
->rpf
.rpf_addr
.u
.prefix4
.s_addr
) {
701 char up_str
[INET_ADDRSTRLEN
];
702 char rpf_str
[PREFIX_STRLEN
];
703 pim_inet4_dump("<up?>", upstream
, up_str
, sizeof(up_str
));
704 pim_addr_dump("<rpf?>", &up
->rpf
.rpf_addr
, rpf_str
,
707 "%s %s: (S,G)=%s upstream=%s not directed to RPF'(S,G)=%s on interface %s",
708 __FILE__
, __func__
, up
->sg_str
, up_str
, rpf_str
,
712 /* upstream directed to RPF'(S,G) */
715 /* Join(S,G) to RPF'(S,G) */
716 pim_upstream_join_suppress(up
, up
->rpf
.rpf_addr
.u
.prefix4
,
721 /* Prune to RPF'(S,G) */
723 if (source_flags
& PIM_RPT_BIT_MASK
) {
724 if (source_flags
& PIM_WILDCARD_BIT_MASK
) {
725 /* Prune(*,G) to RPF'(S,G) */
726 pim_upstream_join_timer_decrease_to_t_override(
731 /* Prune(S,G,rpt) to RPF'(S,G) */
732 pim_upstream_join_timer_decrease_to_t_override("Prune(S,G,rpt)",
737 /* Prune(S,G) to RPF'(S,G) */
738 pim_upstream_join_timer_decrease_to_t_override("Prune(S,G)", up
);
741 static int nonlocal_upstream(int is_join
, struct interface
*recv_ifp
,
742 struct in_addr upstream
, struct prefix_sg
*sg
,
743 uint8_t source_flags
, uint16_t holdtime
)
745 struct pim_interface
*recv_pim_ifp
;
746 int is_local
; /* boolean */
748 recv_pim_ifp
= recv_ifp
->info
;
749 zassert(recv_pim_ifp
);
751 is_local
= (upstream
.s_addr
== recv_pim_ifp
->primary_address
.s_addr
);
756 if (PIM_DEBUG_PIM_TRACE_DETAIL
) {
757 char up_str
[INET_ADDRSTRLEN
];
758 pim_inet4_dump("<upstream?>", upstream
, up_str
, sizeof(up_str
));
759 zlog_warn("%s: recv %s (S,G)=%s to non-local upstream=%s on %s",
760 __func__
, is_join
? "join" : "prune",
761 pim_str_sg_dump(sg
), up_str
, recv_ifp
->name
);
765 * Since recv upstream addr was not directed to our primary
766 * address, check if we should react to it in any way.
768 check_recv_upstream(is_join
, recv_ifp
, upstream
, sg
, source_flags
,
771 return 1; /* non-local */
774 static void pim_ifchannel_ifjoin_handler(struct pim_ifchannel
*ch
,
775 struct pim_interface
*pim_ifp
)
777 pim_ifchannel_ifjoin_switch(__func__
, ch
, PIM_IFJOIN_JOIN
);
778 PIM_IF_FLAG_UNSET_S_G_RPT(ch
->flags
);
779 /* check if the interface qualifies as an immediate
782 if (pim_upstream_evaluate_join_desired_interface(
785 pim_channel_add_oif(ch
->upstream
->channel_oil
,
787 PIM_OIF_FLAG_PROTO_PIM
,
789 pim_upstream_update_join_desired(pim_ifp
->pim
,
795 void pim_ifchannel_join_add(struct interface
*ifp
, struct in_addr neigh_addr
,
796 struct in_addr upstream
, struct prefix_sg
*sg
,
797 uint8_t source_flags
, uint16_t holdtime
)
799 struct pim_interface
*pim_ifp
;
800 struct pim_ifchannel
*ch
;
802 if (nonlocal_upstream(1 /* join */, ifp
, upstream
, sg
, source_flags
,
807 ch
= pim_ifchannel_add(ifp
, sg
, source_flags
,
808 PIM_UPSTREAM_FLAG_MASK_SRC_PIM
);
811 RFC 4601: 4.6.1. (S,G) Assert Message State Machine
813 Transitions from "I am Assert Loser" State
815 Receive Join(S,G) on Interface I
817 We receive a Join(S,G) that has the Upstream Neighbor Address
818 field set to my primary IP address on interface I. The action is
819 to transition to NoInfo state, delete this (S,G) assert state
820 (Actions A5 below), and allow the normal PIM Join/Prune mechanisms
823 Notice: The nonlocal_upstream() test above ensures the upstream
824 address of the join message is our primary address.
826 if (ch
->ifassert_state
== PIM_IFASSERT_I_AM_LOSER
) {
827 char neigh_str
[INET_ADDRSTRLEN
];
828 pim_inet4_dump("<neigh?>", neigh_addr
, neigh_str
,
830 zlog_warn("%s: Assert Loser recv Join%s from %s on %s",
831 __func__
, ch
->sg_str
, neigh_str
, ifp
->name
);
833 assert_action_a5(ch
);
839 switch (ch
->ifjoin_state
) {
840 case PIM_IFJOIN_NOINFO
:
841 pim_ifchannel_ifjoin_switch(__func__
, ch
, PIM_IFJOIN_JOIN
);
842 if (pim_macro_chisin_oiflist(ch
)) {
843 pim_upstream_inherited_olist(pim_ifp
->pim
,
845 pim_forward_start(ch
);
848 * If we are going to be a LHR, we need to note it
850 if (ch
->upstream
->parent
&&
851 (PIM_UPSTREAM_FLAG_TEST_CAN_BE_LHR(
852 ch
->upstream
->parent
->flags
))
853 && !(ch
->upstream
->flags
854 & PIM_UPSTREAM_FLAG_MASK_SRC_LHR
)) {
855 pim_upstream_ref(ch
->upstream
,
856 PIM_UPSTREAM_FLAG_MASK_SRC_LHR
,
858 pim_upstream_keep_alive_timer_start(
859 ch
->upstream
, pim_ifp
->pim
->keep_alive_time
);
862 case PIM_IFJOIN_JOIN
:
863 zassert(!ch
->t_ifjoin_prune_pending_timer
);
866 In the JOIN state ch->t_ifjoin_expiry_timer may be NULL due to
868 previously received join message with holdtime=0xFFFF.
870 if (ch
->t_ifjoin_expiry_timer
) {
871 unsigned long remain
= thread_timer_remain_second(
872 ch
->t_ifjoin_expiry_timer
);
873 if (remain
> holdtime
) {
875 RFC 4601: 4.5.3. Receiving (S,G) Join/Prune
878 Transitions from Join State
880 The (S,G) downstream state machine on
881 interface I remains in
882 Join state, and the Expiry Timer (ET) is
884 maximum of its current value and the HoldTime
886 triggering Join/Prune message.
888 Conclusion: Do not change the ET if the
890 higher than the received join holdtime.
895 THREAD_OFF(ch
->t_ifjoin_expiry_timer
);
897 case PIM_IFJOIN_PRUNE
:
898 if (source_flags
& PIM_ENCODE_RPT_BIT
)
899 pim_ifchannel_ifjoin_switch(__func__
, ch
,
902 pim_ifchannel_ifjoin_handler(ch
, pim_ifp
);
904 case PIM_IFJOIN_PRUNE_PENDING
:
905 THREAD_OFF(ch
->t_ifjoin_prune_pending_timer
);
906 if (source_flags
& PIM_ENCODE_RPT_BIT
) {
907 THREAD_OFF(ch
->t_ifjoin_expiry_timer
);
908 pim_ifchannel_ifjoin_switch(__func__
, ch
,
911 pim_ifchannel_ifjoin_handler(ch
, pim_ifp
);
914 case PIM_IFJOIN_PRUNE_TMP
:
916 case PIM_IFJOIN_PRUNE_PENDING_TMP
:
920 if (holdtime
!= 0xFFFF) {
921 thread_add_timer(router
->master
, on_ifjoin_expiry_timer
, ch
,
922 holdtime
, &ch
->t_ifjoin_expiry_timer
);
926 void pim_ifchannel_prune(struct interface
*ifp
, struct in_addr upstream
,
927 struct prefix_sg
*sg
, uint8_t source_flags
,
930 struct pim_ifchannel
*ch
;
931 struct pim_interface
*pim_ifp
;
932 int jp_override_interval_msec
;
934 if (nonlocal_upstream(0 /* prune */, ifp
, upstream
, sg
, source_flags
,
939 ch
= pim_ifchannel_find(ifp
, sg
);
940 if (!ch
&& !(source_flags
& PIM_ENCODE_RPT_BIT
)) {
941 if (PIM_DEBUG_PIM_TRACE
)
943 "%s: Received prune with no relevant ifchannel %s%s state: %d",
944 __func__
, ifp
->name
, pim_str_sg_dump(sg
),
949 ch
= pim_ifchannel_add(ifp
, sg
, source_flags
,
950 PIM_UPSTREAM_FLAG_MASK_SRC_PIM
);
954 switch (ch
->ifjoin_state
) {
955 case PIM_IFJOIN_NOINFO
:
956 if (source_flags
& PIM_ENCODE_RPT_BIT
) {
957 if (!(source_flags
& PIM_ENCODE_WC_BIT
))
958 PIM_IF_FLAG_SET_S_G_RPT(ch
->flags
);
960 ch
->ifjoin_state
= PIM_IFJOIN_PRUNE_PENDING
;
961 if (listcount(pim_ifp
->pim_neighbor_list
) > 1)
962 jp_override_interval_msec
=
963 pim_if_jp_override_interval_msec(ifp
);
965 jp_override_interval_msec
=
966 0; /* schedule to expire immediately */
967 /* If we called ifjoin_prune() directly instead, care
969 be taken not to use "ch" afterwards since it would be
972 THREAD_OFF(ch
->t_ifjoin_prune_pending_timer
);
973 THREAD_OFF(ch
->t_ifjoin_expiry_timer
);
974 thread_add_timer_msec(
975 router
->master
, on_ifjoin_prune_pending_timer
,
976 ch
, jp_override_interval_msec
,
977 &ch
->t_ifjoin_prune_pending_timer
);
978 thread_add_timer(router
->master
, on_ifjoin_expiry_timer
,
980 &ch
->t_ifjoin_expiry_timer
);
981 pim_upstream_update_join_desired(pim_ifp
->pim
,
985 case PIM_IFJOIN_PRUNE_PENDING
:
988 case PIM_IFJOIN_JOIN
:
989 THREAD_OFF(ch
->t_ifjoin_expiry_timer
);
991 pim_ifchannel_ifjoin_switch(__func__
, ch
,
992 PIM_IFJOIN_PRUNE_PENDING
);
994 if (listcount(pim_ifp
->pim_neighbor_list
) > 1)
995 jp_override_interval_msec
=
996 pim_if_jp_override_interval_msec(ifp
);
998 jp_override_interval_msec
=
999 0; /* schedule to expire immediately */
1000 /* If we called ifjoin_prune() directly instead, care should
1001 be taken not to use "ch" afterwards since it would be
1003 THREAD_OFF(ch
->t_ifjoin_prune_pending_timer
);
1004 thread_add_timer_msec(router
->master
,
1005 on_ifjoin_prune_pending_timer
, ch
,
1006 jp_override_interval_msec
,
1007 &ch
->t_ifjoin_prune_pending_timer
);
1009 case PIM_IFJOIN_PRUNE
:
1010 if (source_flags
& PIM_ENCODE_RPT_BIT
) {
1011 THREAD_OFF(ch
->t_ifjoin_prune_pending_timer
);
1012 thread_add_timer(router
->master
, on_ifjoin_expiry_timer
,
1014 &ch
->t_ifjoin_expiry_timer
);
1017 case PIM_IFJOIN_PRUNE_TMP
:
1018 if (source_flags
& PIM_ENCODE_RPT_BIT
) {
1019 ch
->ifjoin_state
= PIM_IFJOIN_PRUNE
;
1020 THREAD_OFF(ch
->t_ifjoin_expiry_timer
);
1021 thread_add_timer(router
->master
, on_ifjoin_expiry_timer
,
1023 &ch
->t_ifjoin_expiry_timer
);
1026 case PIM_IFJOIN_PRUNE_PENDING_TMP
:
1027 if (source_flags
& PIM_ENCODE_RPT_BIT
) {
1028 ch
->ifjoin_state
= PIM_IFJOIN_PRUNE_PENDING
;
1029 THREAD_OFF(ch
->t_ifjoin_expiry_timer
);
1030 thread_add_timer(router
->master
, on_ifjoin_expiry_timer
,
1032 &ch
->t_ifjoin_expiry_timer
);
1038 int pim_ifchannel_local_membership_add(struct interface
*ifp
,
1039 struct prefix_sg
*sg
, bool is_vxlan
)
1041 struct pim_ifchannel
*ch
, *starch
;
1042 struct pim_interface
*pim_ifp
;
1043 struct pim_instance
*pim
;
1046 /* PIM enabled on interface? */
1047 pim_ifp
= ifp
->info
;
1049 if (PIM_DEBUG_EVENTS
)
1050 zlog_debug("%s:%s Expected pim interface setup for %s",
1051 __func__
, pim_str_sg_dump(sg
), ifp
->name
);
1055 if (!PIM_IF_TEST_PIM(pim_ifp
->options
)) {
1056 if (PIM_DEBUG_EVENTS
)
1058 "%s:%s PIM is not configured on this interface %s",
1059 __func__
, pim_str_sg_dump(sg
), ifp
->name
);
1065 /* skip (*,G) ch creation if G is of type SSM */
1066 if (sg
->src
.s_addr
== INADDR_ANY
) {
1067 if (pim_is_grp_ssm(pim
, sg
->grp
)) {
1068 if (PIM_DEBUG_PIM_EVENTS
)
1070 "%s: local membership (S,G)=%s ignored as group is SSM",
1071 __func__
, pim_str_sg_dump(sg
));
1076 up_flags
= is_vxlan
? PIM_UPSTREAM_FLAG_MASK_SRC_VXLAN_TERM
:
1077 PIM_UPSTREAM_FLAG_MASK_SRC_IGMP
;
1078 ch
= pim_ifchannel_add(ifp
, sg
, 0, up_flags
);
1080 ifmembership_set(ch
, PIM_IFMEMBERSHIP_INCLUDE
);
1082 if (sg
->src
.s_addr
== INADDR_ANY
) {
1083 struct pim_upstream
*up
= pim_upstream_find(pim
, sg
);
1084 struct pim_upstream
*child
;
1085 struct listnode
*up_node
;
1089 for (ALL_LIST_ELEMENTS_RO(up
->sources
, up_node
, child
)) {
1090 if (PIM_DEBUG_EVENTS
)
1091 zlog_debug("%s %s: IGMP (S,G)=%s(%s) from %s",
1092 __FILE__
, __func__
, child
->sg_str
,
1093 ifp
->name
, up
->sg_str
);
1095 ch
= pim_ifchannel_find(ifp
, &child
->sg
);
1096 if (pim_upstream_evaluate_join_desired_interface(
1097 child
, ch
, starch
)) {
1098 pim_channel_add_oif(child
->channel_oil
, ifp
,
1099 PIM_OIF_FLAG_PROTO_STAR
,
1101 pim_upstream_update_join_desired(pim
, child
);
1105 if (pim
->spt
.switchover
== PIM_SPT_INFINITY
) {
1106 if (pim
->spt
.plist
) {
1107 struct prefix_list
*plist
= prefix_list_lookup(
1108 AFI_IP
, pim
->spt
.plist
);
1111 g
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1112 g
.u
.prefix4
= up
->sg
.grp
;
1114 if (prefix_list_apply(plist
, &g
)
1116 pim_channel_add_oif(
1117 up
->channel_oil
, pim
->regiface
,
1118 PIM_OIF_FLAG_PROTO_IGMP
,
1123 pim_channel_add_oif(up
->channel_oil
, pim
->regiface
,
1124 PIM_OIF_FLAG_PROTO_IGMP
,
1131 void pim_ifchannel_local_membership_del(struct interface
*ifp
,
1132 struct prefix_sg
*sg
)
1134 struct pim_ifchannel
*starch
, *ch
, *orig
;
1135 struct pim_interface
*pim_ifp
;
1137 /* PIM enabled on interface? */
1138 pim_ifp
= ifp
->info
;
1141 if (!PIM_IF_TEST_PIM(pim_ifp
->options
))
1144 orig
= ch
= pim_ifchannel_find(ifp
, sg
);
1147 ifmembership_set(ch
, PIM_IFMEMBERSHIP_NOINFO
);
1149 if (sg
->src
.s_addr
== INADDR_ANY
) {
1150 struct pim_upstream
*up
= pim_upstream_find(pim_ifp
->pim
, sg
);
1151 struct pim_upstream
*child
;
1152 struct listnode
*up_node
, *up_nnode
;
1156 for (ALL_LIST_ELEMENTS(up
->sources
, up_node
, up_nnode
, child
)) {
1157 struct channel_oil
*c_oil
= child
->channel_oil
;
1158 struct pim_ifchannel
*chchannel
=
1159 pim_ifchannel_find(ifp
, &child
->sg
);
1161 pim_ifp
= ifp
->info
;
1163 if (PIM_DEBUG_EVENTS
)
1164 zlog_debug("%s %s: Prune(S,G)=%s(%s) from %s",
1165 __FILE__
, __func__
, up
->sg_str
,
1166 ifp
->name
, child
->sg_str
);
1168 ch
= pim_ifchannel_find(ifp
, &child
->sg
);
1170 * If the S,G has no if channel and the c_oil still
1171 * has output here then the *,G was supplying the
1173 * if channel. So remove it.
1175 if (!pim_upstream_evaluate_join_desired_interface(
1176 child
, ch
, starch
) ||
1178 c_oil
->oil
.mfcc_ttls
[pim_ifp
->mroute_vif_index
])) {
1179 pim_channel_del_inherited_oif(c_oil
, ifp
,
1183 /* Child node removal/ref count-- will happen as part of
1184 * parent' delete_no_info */
1187 delete_on_noinfo(orig
);
1190 void pim_ifchannel_update_could_assert(struct pim_ifchannel
*ch
)
1192 int old_couldassert
=
1193 PIM_FORCE_BOOLEAN(PIM_IF_FLAG_TEST_COULD_ASSERT(ch
->flags
));
1194 int new_couldassert
=
1195 PIM_FORCE_BOOLEAN(pim_macro_ch_could_assert_eval(ch
));
1197 if (new_couldassert
== old_couldassert
)
1200 if (PIM_DEBUG_PIM_EVENTS
) {
1201 char src_str
[INET_ADDRSTRLEN
];
1202 char grp_str
[INET_ADDRSTRLEN
];
1203 pim_inet4_dump("<src?>", ch
->sg
.src
, src_str
, sizeof(src_str
));
1204 pim_inet4_dump("<grp?>", ch
->sg
.grp
, grp_str
, sizeof(grp_str
));
1205 zlog_debug("%s: CouldAssert(%s,%s,%s) changed from %d to %d",
1206 __func__
, src_str
, grp_str
, ch
->interface
->name
,
1207 old_couldassert
, new_couldassert
);
1210 if (new_couldassert
) {
1211 /* CouldAssert(S,G,I) switched from false to true */
1212 PIM_IF_FLAG_SET_COULD_ASSERT(ch
->flags
);
1214 /* CouldAssert(S,G,I) switched from true to false */
1215 PIM_IF_FLAG_UNSET_COULD_ASSERT(ch
->flags
);
1217 if (ch
->ifassert_state
== PIM_IFASSERT_I_AM_WINNER
) {
1218 assert_action_a4(ch
);
1222 pim_ifchannel_update_my_assert_metric(ch
);
1226 my_assert_metric may be affected by:
1229 pim_ifp->primary_address
1230 rpf->source_nexthop.mrib_metric_preference;
1231 rpf->source_nexthop.mrib_route_metric;
1233 void pim_ifchannel_update_my_assert_metric(struct pim_ifchannel
*ch
)
1235 struct pim_assert_metric my_metric_new
=
1236 pim_macro_ch_my_assert_metric_eval(ch
);
1238 if (pim_assert_metric_match(&my_metric_new
, &ch
->ifassert_my_metric
))
1241 if (PIM_DEBUG_PIM_EVENTS
) {
1242 char src_str
[INET_ADDRSTRLEN
];
1243 char grp_str
[INET_ADDRSTRLEN
];
1244 char old_addr_str
[INET_ADDRSTRLEN
];
1245 char new_addr_str
[INET_ADDRSTRLEN
];
1246 pim_inet4_dump("<src?>", ch
->sg
.src
, src_str
, sizeof(src_str
));
1247 pim_inet4_dump("<grp?>", ch
->sg
.grp
, grp_str
, sizeof(grp_str
));
1248 pim_inet4_dump("<old_addr?>", ch
->ifassert_my_metric
.ip_address
,
1249 old_addr_str
, sizeof(old_addr_str
));
1250 pim_inet4_dump("<new_addr?>", my_metric_new
.ip_address
,
1251 new_addr_str
, sizeof(new_addr_str
));
1253 "%s: my_assert_metric(%s,%s,%s) changed from %u,%u,%u,%s to %u,%u,%u,%s",
1254 __func__
, src_str
, grp_str
, ch
->interface
->name
,
1255 ch
->ifassert_my_metric
.rpt_bit_flag
,
1256 ch
->ifassert_my_metric
.metric_preference
,
1257 ch
->ifassert_my_metric
.route_metric
, old_addr_str
,
1258 my_metric_new
.rpt_bit_flag
,
1259 my_metric_new
.metric_preference
,
1260 my_metric_new
.route_metric
, new_addr_str
);
1263 ch
->ifassert_my_metric
= my_metric_new
;
1265 if (pim_assert_metric_better(&ch
->ifassert_my_metric
,
1266 &ch
->ifassert_winner_metric
)) {
1267 assert_action_a5(ch
);
1271 void pim_ifchannel_update_assert_tracking_desired(struct pim_ifchannel
*ch
)
1273 int old_atd
= PIM_FORCE_BOOLEAN(
1274 PIM_IF_FLAG_TEST_ASSERT_TRACKING_DESIRED(ch
->flags
));
1276 PIM_FORCE_BOOLEAN(pim_macro_assert_tracking_desired_eval(ch
));
1278 if (new_atd
== old_atd
)
1281 if (PIM_DEBUG_PIM_EVENTS
) {
1282 char src_str
[INET_ADDRSTRLEN
];
1283 char grp_str
[INET_ADDRSTRLEN
];
1284 pim_inet4_dump("<src?>", ch
->sg
.src
, src_str
, sizeof(src_str
));
1285 pim_inet4_dump("<grp?>", ch
->sg
.grp
, grp_str
, sizeof(grp_str
));
1287 "%s: AssertTrackingDesired(%s,%s,%s) changed from %d to %d",
1288 __func__
, src_str
, grp_str
, ch
->interface
->name
,
1293 /* AssertTrackingDesired(S,G,I) switched from false to true */
1294 PIM_IF_FLAG_SET_ASSERT_TRACKING_DESIRED(ch
->flags
);
1296 /* AssertTrackingDesired(S,G,I) switched from true to false */
1297 PIM_IF_FLAG_UNSET_ASSERT_TRACKING_DESIRED(ch
->flags
);
1299 if (ch
->ifassert_state
== PIM_IFASSERT_I_AM_LOSER
) {
1300 assert_action_a5(ch
);
1306 * If we have a new pim interface, check to
1307 * see if any of the pre-existing channels have
1308 * their upstream out that way and turn on forwarding
1309 * for that ifchannel then.
1311 void pim_ifchannel_scan_forward_start(struct interface
*new_ifp
)
1313 struct pim_interface
*new_pim_ifp
= new_ifp
->info
;
1314 struct pim_instance
*pim
= new_pim_ifp
->pim
;
1315 struct interface
*ifp
;
1317 FOR_ALL_INTERFACES (pim
->vrf
, ifp
) {
1318 struct pim_interface
*loop_pim_ifp
= ifp
->info
;
1319 struct pim_ifchannel
*ch
;
1324 if (new_pim_ifp
== loop_pim_ifp
)
1327 RB_FOREACH (ch
, pim_ifchannel_rb
, &loop_pim_ifp
->ifchannel_rb
) {
1328 if (ch
->ifjoin_state
== PIM_IFJOIN_JOIN
) {
1329 struct pim_upstream
*up
= ch
->upstream
;
1330 if ((!up
->channel_oil
)
1331 && (up
->rpf
.source_nexthop
1332 .interface
== new_ifp
))
1333 pim_forward_start(ch
);
1340 * Downstream per-interface (S,G,rpt) state machine
1341 * states that we need to move (S,G,rpt) items
1342 * into different states at the start of the
1343 * reception of a *,G join as well, when
1344 * we get End of Message
1346 void pim_ifchannel_set_star_g_join_state(struct pim_ifchannel
*ch
, int eom
,
1349 bool send_upstream_starg
= false;
1350 struct pim_ifchannel
*child
;
1351 struct listnode
*ch_node
, *nch_node
;
1352 struct pim_instance
*pim
=
1353 ((struct pim_interface
*)ch
->interface
->info
)->pim
;
1354 struct pim_upstream
*starup
= ch
->upstream
;
1356 if (PIM_DEBUG_PIM_TRACE
)
1358 "%s: %s %s eom: %d join %u", __func__
,
1359 pim_ifchannel_ifjoin_name(ch
->ifjoin_state
, ch
->flags
),
1360 ch
->sg_str
, eom
, join
);
1364 for (ALL_LIST_ELEMENTS(ch
->sources
, ch_node
, nch_node
, child
)) {
1365 if (!PIM_IF_FLAG_TEST_S_G_RPT(child
->flags
))
1368 switch (child
->ifjoin_state
) {
1369 case PIM_IFJOIN_NOINFO
:
1370 case PIM_IFJOIN_JOIN
:
1372 case PIM_IFJOIN_PRUNE
:
1374 child
->ifjoin_state
= PIM_IFJOIN_PRUNE_TMP
;
1376 case PIM_IFJOIN_PRUNE_PENDING
:
1378 child
->ifjoin_state
=
1379 PIM_IFJOIN_PRUNE_PENDING_TMP
;
1381 case PIM_IFJOIN_PRUNE_TMP
:
1382 case PIM_IFJOIN_PRUNE_PENDING_TMP
:
1386 if (child
->ifjoin_state
== PIM_IFJOIN_PRUNE_PENDING_TMP
)
1387 THREAD_OFF(child
->t_ifjoin_prune_pending_timer
);
1388 THREAD_OFF(child
->t_ifjoin_expiry_timer
);
1390 PIM_IF_FLAG_UNSET_S_G_RPT(child
->flags
);
1391 child
->ifjoin_state
= PIM_IFJOIN_NOINFO
;
1393 if ((I_am_RP(pim
, child
->sg
.grp
)) &&
1394 (!pim_upstream_empty_inherited_olist(
1395 child
->upstream
))) {
1396 pim_channel_add_oif(
1397 child
->upstream
->channel_oil
,
1398 ch
->interface
, PIM_OIF_FLAG_PROTO_STAR
,
1400 pim_upstream_update_join_desired(pim
,
1403 send_upstream_starg
= true;
1405 delete_on_noinfo(child
);
1410 if (send_upstream_starg
)
1411 pim_jp_agg_single_upstream_send(&starup
->rpf
, starup
, true);
1414 unsigned int pim_ifchannel_hash_key(const void *arg
)
1416 const struct pim_ifchannel
*ch
= arg
;
1418 return jhash_2words(ch
->sg
.src
.s_addr
, ch
->sg
.grp
.s_addr
, 0);