3 * Copyright (C) 2016 Cumulus Networks, Inc.
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
23 #include <lib/jhash.h>
25 #include <lib/prefix.h>
26 #include <lib/sockunion.h>
27 #include <lib/stream.h>
28 #include <lib/thread.h>
30 #include <lib/plist.h>
31 #include <lib/lib_errors.h>
35 #include "pim_memory.h"
36 #include "pim_iface.h"
40 #include "pim_upstream.h"
44 #include "pim_msdp_packet.h"
45 #include "pim_msdp_socket.h"
47 // struct pim_msdp pim_msdp, *msdp = &pim_msdp;
49 static void pim_msdp_peer_listen(struct pim_msdp_peer
*mp
);
50 static void pim_msdp_peer_cr_timer_setup(struct pim_msdp_peer
*mp
, bool start
);
51 static void pim_msdp_peer_ka_timer_setup(struct pim_msdp_peer
*mp
, bool start
);
52 static void pim_msdp_peer_hold_timer_setup(struct pim_msdp_peer
*mp
,
54 static void pim_msdp_peer_free(struct pim_msdp_peer
*mp
);
55 static void pim_msdp_enable(struct pim_instance
*pim
);
56 static void pim_msdp_sa_adv_timer_setup(struct pim_instance
*pim
, bool start
);
57 static void pim_msdp_sa_deref(struct pim_msdp_sa
*sa
,
58 enum pim_msdp_sa_flags flags
);
59 static int pim_msdp_mg_mbr_comp(const void *p1
, const void *p2
);
60 static void pim_msdp_mg_mbr_free(struct pim_msdp_mg_mbr
*mbr
);
61 static void pim_msdp_mg_mbr_do_del(struct pim_msdp_mg
*mg
,
62 struct pim_msdp_mg_mbr
*mbr
);
64 /************************ SA cache management ******************************/
65 static void pim_msdp_sa_timer_expiry_log(struct pim_msdp_sa
*sa
,
66 const char *timer_str
)
68 zlog_debug("MSDP SA %s %s timer expired", sa
->sg_str
, timer_str
);
71 /* RFC-3618:Sec-5.1 - global active source advertisement timer */
72 static int pim_msdp_sa_adv_timer_cb(struct thread
*t
)
74 struct pim_instance
*pim
= THREAD_ARG(t
);
76 if (PIM_DEBUG_MSDP_EVENTS
) {
77 zlog_debug("MSDP SA advertisement timer expired");
80 pim_msdp_sa_adv_timer_setup(pim
, true /* start */);
81 pim_msdp_pkt_sa_tx(pim
);
84 static void pim_msdp_sa_adv_timer_setup(struct pim_instance
*pim
, bool start
)
86 THREAD_OFF(pim
->msdp
.sa_adv_timer
);
88 thread_add_timer(pim
->msdp
.master
, pim_msdp_sa_adv_timer_cb
,
89 pim
, PIM_MSDP_SA_ADVERTISMENT_TIME
,
90 &pim
->msdp
.sa_adv_timer
);
94 /* RFC-3618:Sec-5.3 - SA cache state timer */
95 static int pim_msdp_sa_state_timer_cb(struct thread
*t
)
97 struct pim_msdp_sa
*sa
;
101 if (PIM_DEBUG_MSDP_EVENTS
) {
102 pim_msdp_sa_timer_expiry_log(sa
, "state");
105 pim_msdp_sa_deref(sa
, PIM_MSDP_SAF_PEER
);
108 static void pim_msdp_sa_state_timer_setup(struct pim_msdp_sa
*sa
, bool start
)
110 THREAD_OFF(sa
->sa_state_timer
);
112 thread_add_timer(sa
->pim
->msdp
.master
,
113 pim_msdp_sa_state_timer_cb
, sa
,
114 PIM_MSDP_SA_HOLD_TIME
, &sa
->sa_state_timer
);
118 static void pim_msdp_sa_upstream_del(struct pim_msdp_sa
*sa
)
120 struct pim_upstream
*up
= sa
->up
;
126 if (PIM_UPSTREAM_FLAG_TEST_SRC_MSDP(up
->flags
)) {
127 PIM_UPSTREAM_FLAG_UNSET_SRC_MSDP(up
->flags
);
128 sa
->flags
|= PIM_MSDP_SAF_UP_DEL_IN_PROG
;
129 up
= pim_upstream_del(sa
->pim
, up
, __PRETTY_FUNCTION__
);
130 /* re-eval joinDesired; clearing peer-msdp-sa flag can
134 pim_upstream_update_join_desired(sa
->pim
, up
);
135 sa
->flags
&= ~PIM_MSDP_SAF_UP_DEL_IN_PROG
;
138 if (PIM_DEBUG_MSDP_EVENTS
) {
139 zlog_debug("MSDP SA %s de-referenced SPT", sa
->sg_str
);
143 static bool pim_msdp_sa_upstream_add_ok(struct pim_msdp_sa
*sa
,
144 struct pim_upstream
*xg_up
)
146 if (!(sa
->flags
& PIM_MSDP_SAF_PEER
)) {
147 /* SA should have been rxed from a peer */
150 /* check if we are RP */
151 if (!I_am_RP(sa
->pim
, sa
->sg
.grp
)) {
155 /* check if we have a (*, G) with a non-empty immediate OIL */
159 memset(&sg
, 0, sizeof(sg
));
162 xg_up
= pim_upstream_find(sa
->pim
, &sg
);
164 if (!xg_up
|| (xg_up
->join_state
!= PIM_UPSTREAM_JOINED
)) {
165 /* join desired will be true for such (*, G) entries so we will
166 * just look at join_state and let the PIM state machine do the
175 /* Upstream add evaluation needs to happen everytime -
176 * 1. Peer reference is added or removed.
177 * 2. The RP for a group changes.
178 * 3. joinDesired for the associated (*, G) changes
179 * 4. associated (*, G) is removed - this seems like a bit redundant
180 * (considering #4); but just in case an entry gets nuked without
181 * upstream state transition
183 static void pim_msdp_sa_upstream_update(struct pim_msdp_sa
*sa
,
184 struct pim_upstream
*xg_up
,
187 struct pim_upstream
*up
;
189 if (!pim_msdp_sa_upstream_add_ok(sa
, xg_up
)) {
190 pim_msdp_sa_upstream_del(sa
);
199 up
= pim_upstream_find(sa
->pim
, &sa
->sg
);
200 if (up
&& (PIM_UPSTREAM_FLAG_TEST_SRC_MSDP(up
->flags
))) {
201 /* somehow we lost track of the upstream ptr? best log it */
203 if (PIM_DEBUG_MSDP_EVENTS
) {
204 zlog_debug("MSDP SA %s SPT reference missing",
210 /* RFC3618: "RP triggers a (S, G) join event towards the data source
211 * as if a JP message was rxed addressed to the RP itself." */
212 up
= pim_upstream_add(sa
->pim
, &sa
->sg
, NULL
/* iif */,
213 PIM_UPSTREAM_FLAG_MASK_SRC_MSDP
,
214 __PRETTY_FUNCTION__
, NULL
);
218 /* update inherited oil */
219 pim_upstream_inherited_olist(sa
->pim
, up
);
220 /* should we also start the kat in parallel? we will need it
223 if (PIM_DEBUG_MSDP_EVENTS
) {
224 zlog_debug("MSDP SA %s referenced SPT", sa
->sg_str
);
227 if (PIM_DEBUG_MSDP_EVENTS
) {
228 zlog_debug("MSDP SA %s SPT reference failed",
234 /* release all mem associated with a sa */
235 static void pim_msdp_sa_free(struct pim_msdp_sa
*sa
)
237 pim_msdp_sa_state_timer_setup(sa
, false);
239 XFREE(MTYPE_PIM_MSDP_SA
, sa
);
242 static struct pim_msdp_sa
*pim_msdp_sa_new(struct pim_instance
*pim
,
243 struct prefix_sg
*sg
,
246 struct pim_msdp_sa
*sa
;
248 sa
= XCALLOC(MTYPE_PIM_MSDP_SA
, sizeof(*sa
));
252 pim_str_sg_set(sg
, sa
->sg_str
);
254 sa
->uptime
= pim_time_monotonic_sec();
256 /* insert into misc tables for easy access */
257 sa
= hash_get(pim
->msdp
.sa_hash
, sa
, hash_alloc_intern
);
258 listnode_add_sort(pim
->msdp
.sa_list
, sa
);
260 if (PIM_DEBUG_MSDP_EVENTS
) {
261 zlog_debug("MSDP SA %s created", sa
->sg_str
);
267 static struct pim_msdp_sa
*pim_msdp_sa_find(struct pim_instance
*pim
,
268 struct prefix_sg
*sg
)
270 struct pim_msdp_sa lookup
;
273 return hash_lookup(pim
->msdp
.sa_hash
, &lookup
);
276 static struct pim_msdp_sa
*pim_msdp_sa_add(struct pim_instance
*pim
,
277 struct prefix_sg
*sg
,
280 struct pim_msdp_sa
*sa
;
282 sa
= pim_msdp_sa_find(pim
, sg
);
287 return pim_msdp_sa_new(pim
, sg
, rp
);
290 static void pim_msdp_sa_del(struct pim_msdp_sa
*sa
)
292 /* this is somewhat redundant - still want to be careful not to leave
293 * stale upstream references */
294 pim_msdp_sa_upstream_del(sa
);
297 pim_msdp_sa_state_timer_setup(sa
, false /* start */);
299 /* remove the entry from various tables */
300 listnode_delete(sa
->pim
->msdp
.sa_list
, sa
);
301 hash_release(sa
->pim
->msdp
.sa_hash
, sa
);
303 if (PIM_DEBUG_MSDP_EVENTS
) {
304 zlog_debug("MSDP SA %s deleted", sa
->sg_str
);
307 /* free up any associated memory */
308 pim_msdp_sa_free(sa
);
311 static void pim_msdp_sa_peer_ip_set(struct pim_msdp_sa
*sa
,
312 struct pim_msdp_peer
*mp
, struct in_addr rp
)
314 struct pim_msdp_peer
*old_mp
;
316 /* optimize the "no change" case as it will happen
317 * frequently/periodically */
318 if (mp
&& (sa
->peer
.s_addr
== mp
->peer
.s_addr
)) {
322 /* any time the peer ip changes also update the rp address */
323 if (PIM_INADDR_ISNOT_ANY(sa
->peer
)) {
324 old_mp
= pim_msdp_peer_find(sa
->pim
, sa
->peer
);
325 if (old_mp
&& old_mp
->sa_cnt
) {
334 sa
->peer
.s_addr
= PIM_NET_INADDR_ANY
;
339 /* When a local active-source is removed there is no way to withdraw the
340 * source from peers. We will simply remove it from the SA cache so it will
341 * not be sent in supsequent SA updates. Peers will consequently timeout the
343 * Similarly a "peer-added" SA is never explicitly deleted. It is simply
344 * aged out overtime if not seen in the SA updates from the peers.
345 * XXX: should we provide a knob to drop entries learnt from a peer when the
347 static void pim_msdp_sa_deref(struct pim_msdp_sa
*sa
,
348 enum pim_msdp_sa_flags flags
)
350 bool update_up
= false;
352 if ((sa
->flags
& PIM_MSDP_SAF_LOCAL
)) {
353 if (flags
& PIM_MSDP_SAF_LOCAL
) {
354 if (PIM_DEBUG_MSDP_EVENTS
) {
355 zlog_debug("MSDP SA %s local reference removed",
358 if (sa
->pim
->msdp
.local_cnt
)
359 --sa
->pim
->msdp
.local_cnt
;
363 if ((sa
->flags
& PIM_MSDP_SAF_PEER
)) {
364 if (flags
& PIM_MSDP_SAF_PEER
) {
367 if (PIM_DEBUG_MSDP_EVENTS
) {
368 zlog_debug("MSDP SA %s peer reference removed",
371 pim_msdp_sa_state_timer_setup(sa
, false /* start */);
372 rp
.s_addr
= INADDR_ANY
;
373 pim_msdp_sa_peer_ip_set(sa
, NULL
/* mp */, rp
);
374 /* if peer ref was removed we need to remove the msdp
383 pim_msdp_sa_upstream_update(sa
, NULL
/* xg_up */, "sa-deref");
386 if (!(sa
->flags
& PIM_MSDP_SAF_REF
)) {
391 void pim_msdp_sa_ref(struct pim_instance
*pim
, struct pim_msdp_peer
*mp
,
392 struct prefix_sg
*sg
, struct in_addr rp
)
394 struct pim_msdp_sa
*sa
;
396 sa
= pim_msdp_sa_add(pim
, sg
, rp
);
403 if (!(sa
->flags
& PIM_MSDP_SAF_PEER
)) {
404 sa
->flags
|= PIM_MSDP_SAF_PEER
;
405 if (PIM_DEBUG_MSDP_EVENTS
) {
406 zlog_debug("MSDP SA %s added by peer",
410 pim_msdp_sa_peer_ip_set(sa
, mp
, rp
);
411 /* start/re-start the state timer to prevent cache expiry */
412 pim_msdp_sa_state_timer_setup(sa
, true /* start */);
413 /* We re-evaluate SA "SPT-trigger" everytime we hear abt it from
415 * peer. XXX: If this becomes too much of a periodic overhead we
416 * can make it event based */
417 pim_msdp_sa_upstream_update(sa
, NULL
/* xg_up */, "peer-ref");
419 if (!(sa
->flags
& PIM_MSDP_SAF_LOCAL
)) {
420 sa
->flags
|= PIM_MSDP_SAF_LOCAL
;
421 ++sa
->pim
->msdp
.local_cnt
;
422 if (PIM_DEBUG_MSDP_EVENTS
) {
423 zlog_debug("MSDP SA %s added locally",
426 /* send an immediate SA update to peers */
427 pim_msdp_pkt_sa_tx_one(sa
);
429 sa
->flags
&= ~PIM_MSDP_SAF_STALE
;
433 /* The following criteria must be met to originate an SA from the MSDP
435 * 1. KAT must be running i.e. source is active.
436 * 2. We must be RP for the group.
437 * 3. Source must be registrable to the RP (this is where the RFC is vague
438 * and especially ambiguous in CLOS networks; with anycast RP all sources
439 * are potentially registrable to all RPs in the domain). We assume #3 is
441 * a. We are also the FHR-DR for the source (OR)
442 * b. We rxed a pim register (null or data encapsulated) within the last
443 * (3 * (1.5 * register_suppression_timer))).
445 static bool pim_msdp_sa_local_add_ok(struct pim_upstream
*up
)
447 struct pim_instance
*pim
= up
->channel_oil
->pim
;
449 if (!(pim
->msdp
.flags
& PIM_MSDPF_ENABLE
)) {
453 if (!pim_upstream_is_kat_running(up
))
454 /* stream is not active */
457 if (!I_am_RP(pim
, up
->sg
.grp
)) {
458 /* we are not RP for the group */
462 /* we are the FHR-DR for this stream or we are RP and have seen
464 * from a FHR for this source */
465 if (PIM_UPSTREAM_FLAG_TEST_FHR(up
->flags
) || up
->t_msdp_reg_timer
) {
472 static void pim_msdp_sa_local_add(struct pim_instance
*pim
,
473 struct prefix_sg
*sg
)
476 rp
.s_addr
= INADDR_ANY
;
477 pim_msdp_sa_ref(pim
, NULL
/* mp */, sg
, rp
);
480 void pim_msdp_sa_local_del(struct pim_instance
*pim
, struct prefix_sg
*sg
)
482 struct pim_msdp_sa
*sa
;
484 sa
= pim_msdp_sa_find(pim
, sg
);
486 pim_msdp_sa_deref(sa
, PIM_MSDP_SAF_LOCAL
);
490 /* we need to be very cautious with this API as SA del too can trigger an
491 * upstream del and we will get stuck in a simple loop */
492 static void pim_msdp_sa_local_del_on_up_del(struct pim_instance
*pim
,
493 struct prefix_sg
*sg
)
495 struct pim_msdp_sa
*sa
;
497 sa
= pim_msdp_sa_find(pim
, sg
);
499 if (PIM_DEBUG_MSDP_INTERNAL
) {
500 zlog_debug("MSDP local sa %s del on up del",
504 /* if there is no local reference escape */
505 if (!(sa
->flags
& PIM_MSDP_SAF_LOCAL
)) {
506 if (PIM_DEBUG_MSDP_INTERNAL
) {
507 zlog_debug("MSDP local sa %s del; no local ref",
513 if (sa
->flags
& PIM_MSDP_SAF_UP_DEL_IN_PROG
) {
514 /* MSDP is the one that triggered the upstream del. if
516 * we most certainly have a bug in the PIM upstream
518 * will not have a local reference unless the KAT is
519 * running. And if the
520 * KAT is running there MUST be an additional
521 * source-stream reference to
522 * the flow. Accounting for such cases requires lot of
524 * address this in the next release? - XXX */
527 "MSDP sa %s SPT teardown is causing the local entry to be removed",
532 /* we are dropping the sa on upstream del we should not have an
533 * upstream reference */
535 if (PIM_DEBUG_MSDP_INTERNAL
) {
536 zlog_debug("MSDP local sa %s del; up non-NULL",
541 pim_msdp_sa_deref(sa
, PIM_MSDP_SAF_LOCAL
);
545 /* Local SA qualification needs to be re-evaluated when -
546 * 1. KAT is started or stopped
548 * 3. Whenever FHR status changes for a (S,G) - XXX - currently there
549 * is no clear path to transition an entry out of "MASK_FHR" need
550 * to discuss this with Donald. May result in some strangeness if the
551 * FHR is also the RP.
552 * 4. When msdp_reg timer is started or stopped
554 void pim_msdp_sa_local_update(struct pim_upstream
*up
)
556 struct pim_instance
*pim
= up
->channel_oil
->pim
;
558 if (pim_msdp_sa_local_add_ok(up
)) {
559 pim_msdp_sa_local_add(pim
, &up
->sg
);
561 pim_msdp_sa_local_del(pim
, &up
->sg
);
565 static void pim_msdp_sa_local_setup(struct pim_instance
*pim
)
567 struct pim_upstream
*up
;
569 frr_each (rb_pim_upstream
, &pim
->upstream_head
, up
)
570 pim_msdp_sa_local_update(up
);
573 /* whenever the RP changes we need to re-evaluate the "local" SA-cache */
574 /* XXX: needs to be tested */
575 void pim_msdp_i_am_rp_changed(struct pim_instance
*pim
)
577 struct listnode
*sanode
;
578 struct listnode
*nextnode
;
579 struct pim_msdp_sa
*sa
;
581 if (!(pim
->msdp
.flags
& PIM_MSDPF_ENABLE
)) {
582 /* if the feature is not enabled do nothing */
586 if (PIM_DEBUG_MSDP_INTERNAL
) {
587 zlog_debug("MSDP i_am_rp changed");
590 /* mark all local entries as stale */
591 for (ALL_LIST_ELEMENTS_RO(pim
->msdp
.sa_list
, sanode
, sa
)) {
592 if (sa
->flags
& PIM_MSDP_SAF_LOCAL
) {
593 sa
->flags
|= PIM_MSDP_SAF_STALE
;
597 /* re-setup local SA entries */
598 pim_msdp_sa_local_setup(pim
);
600 for (ALL_LIST_ELEMENTS(pim
->msdp
.sa_list
, sanode
, nextnode
, sa
)) {
601 /* purge stale SA entries */
602 if (sa
->flags
& PIM_MSDP_SAF_STALE
) {
603 /* clear the stale flag; the entry may be kept even
606 sa
->flags
&= ~PIM_MSDP_SAF_STALE
;
607 /* sa_deref can end up freeing the sa; so don't access
609 pim_msdp_sa_deref(sa
, PIM_MSDP_SAF_LOCAL
);
611 /* if the souce is still active check if we can
613 pim_msdp_sa_upstream_update(sa
, NULL
/* xg_up */,
619 /* We track the join state of (*, G) entries. If G has sources in the SA-cache
620 * we need to setup or teardown SPT when the JoinDesired status changes for
622 void pim_msdp_up_join_state_changed(struct pim_instance
*pim
,
623 struct pim_upstream
*xg_up
)
625 struct listnode
*sanode
;
626 struct pim_msdp_sa
*sa
;
628 if (PIM_DEBUG_MSDP_INTERNAL
) {
629 zlog_debug("MSDP join state changed for %s", xg_up
->sg_str
);
632 /* If this is not really an XG entry just move on */
633 if ((xg_up
->sg
.src
.s_addr
!= INADDR_ANY
)
634 || (xg_up
->sg
.grp
.s_addr
== INADDR_ANY
)) {
638 /* XXX: Need to maintain SAs per-group to avoid all this unnecessary
640 for (ALL_LIST_ELEMENTS_RO(pim
->msdp
.sa_list
, sanode
, sa
)) {
641 if (sa
->sg
.grp
.s_addr
!= xg_up
->sg
.grp
.s_addr
) {
644 pim_msdp_sa_upstream_update(sa
, xg_up
, "up-jp-change");
648 static void pim_msdp_up_xg_del(struct pim_instance
*pim
, struct prefix_sg
*sg
)
650 struct listnode
*sanode
;
651 struct pim_msdp_sa
*sa
;
653 if (PIM_DEBUG_MSDP_INTERNAL
) {
654 zlog_debug("MSDP %s del", pim_str_sg_dump(sg
));
657 /* If this is not really an XG entry just move on */
658 if ((sg
->src
.s_addr
!= INADDR_ANY
) || (sg
->grp
.s_addr
== INADDR_ANY
)) {
662 /* XXX: Need to maintain SAs per-group to avoid all this unnecessary
664 for (ALL_LIST_ELEMENTS_RO(pim
->msdp
.sa_list
, sanode
, sa
)) {
665 if (sa
->sg
.grp
.s_addr
!= sg
->grp
.s_addr
) {
668 pim_msdp_sa_upstream_update(sa
, NULL
/* xg */, "up-jp-change");
672 void pim_msdp_up_del(struct pim_instance
*pim
, struct prefix_sg
*sg
)
674 if (PIM_DEBUG_MSDP_INTERNAL
) {
675 zlog_debug("MSDP up %s del", pim_str_sg_dump(sg
));
677 if (sg
->src
.s_addr
== INADDR_ANY
) {
678 pim_msdp_up_xg_del(pim
, sg
);
680 pim_msdp_sa_local_del_on_up_del(pim
, sg
);
684 /* sa hash and peer list helpers */
685 static unsigned int pim_msdp_sa_hash_key_make(const void *p
)
687 const struct pim_msdp_sa
*sa
= p
;
689 return (jhash_2words(sa
->sg
.src
.s_addr
, sa
->sg
.grp
.s_addr
, 0));
692 static bool pim_msdp_sa_hash_eq(const void *p1
, const void *p2
)
694 const struct pim_msdp_sa
*sa1
= p1
;
695 const struct pim_msdp_sa
*sa2
= p2
;
697 return ((sa1
->sg
.src
.s_addr
== sa2
->sg
.src
.s_addr
)
698 && (sa1
->sg
.grp
.s_addr
== sa2
->sg
.grp
.s_addr
));
701 static int pim_msdp_sa_comp(const void *p1
, const void *p2
)
703 const struct pim_msdp_sa
*sa1
= p1
;
704 const struct pim_msdp_sa
*sa2
= p2
;
706 if (ntohl(sa1
->sg
.grp
.s_addr
) < ntohl(sa2
->sg
.grp
.s_addr
))
709 if (ntohl(sa1
->sg
.grp
.s_addr
) > ntohl(sa2
->sg
.grp
.s_addr
))
712 if (ntohl(sa1
->sg
.src
.s_addr
) < ntohl(sa2
->sg
.src
.s_addr
))
715 if (ntohl(sa1
->sg
.src
.s_addr
) > ntohl(sa2
->sg
.src
.s_addr
))
721 /* RFC-3618:Sec-10.1.3 - Peer-RPF forwarding */
722 /* XXX: this can use a bit of refining and extensions */
723 bool pim_msdp_peer_rpf_check(struct pim_msdp_peer
*mp
, struct in_addr rp
)
725 if (mp
->peer
.s_addr
== rp
.s_addr
) {
732 /************************ Peer session management **************************/
733 char *pim_msdp_state_dump(enum pim_msdp_peer_state state
, char *buf
,
737 case PIM_MSDP_DISABLED
:
738 snprintf(buf
, buf_size
, "%s", "disabled");
740 case PIM_MSDP_INACTIVE
:
741 snprintf(buf
, buf_size
, "%s", "inactive");
743 case PIM_MSDP_LISTEN
:
744 snprintf(buf
, buf_size
, "%s", "listen");
746 case PIM_MSDP_CONNECTING
:
747 snprintf(buf
, buf_size
, "%s", "connecting");
749 case PIM_MSDP_ESTABLISHED
:
750 snprintf(buf
, buf_size
, "%s", "established");
753 snprintf(buf
, buf_size
, "unk-%d", state
);
758 char *pim_msdp_peer_key_dump(struct pim_msdp_peer
*mp
, char *buf
, int buf_size
,
761 char peer_str
[INET_ADDRSTRLEN
];
762 char local_str
[INET_ADDRSTRLEN
];
764 pim_inet4_dump("<peer?>", mp
->peer
, peer_str
, sizeof(peer_str
));
766 pim_inet4_dump("<local?>", mp
->local
, local_str
,
768 snprintf(buf
, buf_size
, "MSDP peer %s local %s mg %s", peer_str
,
769 local_str
, mp
->mesh_group_name
);
771 snprintf(buf
, buf_size
, "MSDP peer %s", peer_str
);
777 static void pim_msdp_peer_state_chg_log(struct pim_msdp_peer
*mp
)
779 char state_str
[PIM_MSDP_STATE_STRLEN
];
781 pim_msdp_state_dump(mp
->state
, state_str
, sizeof(state_str
));
782 zlog_debug("MSDP peer %s state chg to %s", mp
->key_str
, state_str
);
785 /* MSDP Connection State Machine actions (defined in RFC-3618:Sec-11.2) */
786 /* 11.2.A2: active peer - start connect retry timer; when the timer fires
787 * a tcp connection will be made */
788 static void pim_msdp_peer_connect(struct pim_msdp_peer
*mp
)
790 mp
->state
= PIM_MSDP_CONNECTING
;
791 if (PIM_DEBUG_MSDP_EVENTS
) {
792 pim_msdp_peer_state_chg_log(mp
);
795 pim_msdp_peer_cr_timer_setup(mp
, true /* start */);
798 /* 11.2.A3: passive peer - just listen for connections */
799 static void pim_msdp_peer_listen(struct pim_msdp_peer
*mp
)
801 mp
->state
= PIM_MSDP_LISTEN
;
802 if (PIM_DEBUG_MSDP_EVENTS
) {
803 pim_msdp_peer_state_chg_log(mp
);
806 /* this is interntionally asymmetric i.e. we set up listen-socket when
808 * first listening peer is configured; but don't bother tearing it down
810 * all the peers go down */
811 pim_msdp_sock_listen(mp
->pim
);
814 /* 11.2.A4 and 11.2.A5: transition active or passive peer to
815 * established state */
816 void pim_msdp_peer_established(struct pim_msdp_peer
*mp
)
818 if (mp
->state
!= PIM_MSDP_ESTABLISHED
) {
822 mp
->state
= PIM_MSDP_ESTABLISHED
;
823 mp
->uptime
= pim_time_monotonic_sec();
825 if (PIM_DEBUG_MSDP_EVENTS
) {
826 pim_msdp_peer_state_chg_log(mp
);
829 /* stop retry timer on active peers */
830 pim_msdp_peer_cr_timer_setup(mp
, false /* start */);
832 /* send KA; start KA and hold timers */
833 pim_msdp_pkt_ka_tx(mp
);
834 pim_msdp_peer_ka_timer_setup(mp
, true /* start */);
835 pim_msdp_peer_hold_timer_setup(mp
, true /* start */);
837 pim_msdp_pkt_sa_tx_to_one_peer(mp
);
839 PIM_MSDP_PEER_WRITE_ON(mp
);
840 PIM_MSDP_PEER_READ_ON(mp
);
843 /* 11.2.A6, 11.2.A7 and 11.2.A8: shutdown the peer tcp connection */
844 void pim_msdp_peer_stop_tcp_conn(struct pim_msdp_peer
*mp
, bool chg_state
)
847 if (mp
->state
== PIM_MSDP_ESTABLISHED
) {
850 mp
->state
= PIM_MSDP_INACTIVE
;
851 if (PIM_DEBUG_MSDP_EVENTS
) {
852 pim_msdp_peer_state_chg_log(mp
);
856 if (PIM_DEBUG_MSDP_INTERNAL
) {
857 zlog_debug("MSDP peer %s pim_msdp_peer_stop_tcp_conn",
860 /* stop read and write threads */
861 PIM_MSDP_PEER_READ_OFF(mp
);
862 PIM_MSDP_PEER_WRITE_OFF(mp
);
867 stream_reset(mp
->ibuf
);
869 stream_fifo_clean(mp
->obuf
);
871 /* stop all peer timers */
872 pim_msdp_peer_ka_timer_setup(mp
, false /* start */);
873 pim_msdp_peer_cr_timer_setup(mp
, false /* start */);
874 pim_msdp_peer_hold_timer_setup(mp
, false /* start */);
876 /* close connection */
883 /* RFC-3618:Sec-5.6 - stop the peer tcp connection and startover */
884 void pim_msdp_peer_reset_tcp_conn(struct pim_msdp_peer
*mp
, const char *rc_str
)
886 if (PIM_DEBUG_EVENTS
) {
887 zlog_debug("MSDP peer %s tcp reset %s", mp
->key_str
, rc_str
);
888 snprintf(mp
->last_reset
, sizeof(mp
->last_reset
), "%s", rc_str
);
891 /* close the connection and transition to listening or connecting */
892 pim_msdp_peer_stop_tcp_conn(mp
, true /* chg_state */);
893 if (PIM_MSDP_PEER_IS_LISTENER(mp
)) {
894 pim_msdp_peer_listen(mp
);
896 pim_msdp_peer_connect(mp
);
900 static void pim_msdp_peer_timer_expiry_log(struct pim_msdp_peer
*mp
,
901 const char *timer_str
)
903 zlog_debug("MSDP peer %s %s timer expired", mp
->key_str
, timer_str
);
906 /* RFC-3618:Sec-5.4 - peer hold timer */
907 static int pim_msdp_peer_hold_timer_cb(struct thread
*t
)
909 struct pim_msdp_peer
*mp
;
913 if (PIM_DEBUG_MSDP_EVENTS
) {
914 pim_msdp_peer_timer_expiry_log(mp
, "hold");
917 if (mp
->state
!= PIM_MSDP_ESTABLISHED
) {
921 if (PIM_DEBUG_MSDP_EVENTS
) {
922 pim_msdp_peer_state_chg_log(mp
);
924 pim_msdp_peer_reset_tcp_conn(mp
, "ht-expired");
928 static void pim_msdp_peer_hold_timer_setup(struct pim_msdp_peer
*mp
, bool start
)
930 struct pim_instance
*pim
= mp
->pim
;
931 THREAD_OFF(mp
->hold_timer
);
933 thread_add_timer(pim
->msdp
.master
, pim_msdp_peer_hold_timer_cb
,
934 mp
, PIM_MSDP_PEER_HOLD_TIME
, &mp
->hold_timer
);
939 /* RFC-3618:Sec-5.5 - peer keepalive timer */
940 static int pim_msdp_peer_ka_timer_cb(struct thread
*t
)
942 struct pim_msdp_peer
*mp
;
946 if (PIM_DEBUG_MSDP_EVENTS
) {
947 pim_msdp_peer_timer_expiry_log(mp
, "ka");
950 pim_msdp_pkt_ka_tx(mp
);
951 pim_msdp_peer_ka_timer_setup(mp
, true /* start */);
954 static void pim_msdp_peer_ka_timer_setup(struct pim_msdp_peer
*mp
, bool start
)
956 THREAD_OFF(mp
->ka_timer
);
958 thread_add_timer(mp
->pim
->msdp
.master
,
959 pim_msdp_peer_ka_timer_cb
, mp
,
960 PIM_MSDP_PEER_KA_TIME
, &mp
->ka_timer
);
964 static void pim_msdp_peer_active_connect(struct pim_msdp_peer
*mp
)
968 rc
= pim_msdp_sock_connect(mp
);
970 if (PIM_DEBUG_MSDP_INTERNAL
) {
971 zlog_debug("MSDP peer %s pim_msdp_peer_active_connect: %d",
978 /* connect failed restart the connect-retry timer */
979 pim_msdp_peer_cr_timer_setup(mp
, true /* start */);
982 case connect_success
:
983 /* connect was sucessful move to established */
984 pim_msdp_peer_established(mp
);
987 case connect_in_progress
:
988 /* for NB content we need to wait till sock is readable or
990 PIM_MSDP_PEER_WRITE_ON(mp
);
991 PIM_MSDP_PEER_READ_ON(mp
);
992 /* also restart connect-retry timer to reset the socket if
995 pim_msdp_peer_cr_timer_setup(mp
, true /* start */);
1000 /* RFC-3618:Sec-5.6 - connection retry on active peer */
1001 static int pim_msdp_peer_cr_timer_cb(struct thread
*t
)
1003 struct pim_msdp_peer
*mp
;
1007 if (PIM_DEBUG_MSDP_EVENTS
) {
1008 pim_msdp_peer_timer_expiry_log(mp
, "connect-retry");
1011 if (mp
->state
!= PIM_MSDP_CONNECTING
|| PIM_MSDP_PEER_IS_LISTENER(mp
)) {
1015 pim_msdp_peer_active_connect(mp
);
1018 static void pim_msdp_peer_cr_timer_setup(struct pim_msdp_peer
*mp
, bool start
)
1020 THREAD_OFF(mp
->cr_timer
);
1023 mp
->pim
->msdp
.master
, pim_msdp_peer_cr_timer_cb
, mp
,
1024 PIM_MSDP_PEER_CONNECT_RETRY_TIME
, &mp
->cr_timer
);
1028 /* if a valid packet is rxed from the peer we can restart hold timer */
1029 void pim_msdp_peer_pkt_rxed(struct pim_msdp_peer
*mp
)
1031 if (mp
->state
== PIM_MSDP_ESTABLISHED
) {
1032 pim_msdp_peer_hold_timer_setup(mp
, true /* start */);
1036 /* if a valid packet is txed to the peer we can restart ka timer and avoid
1037 * unnecessary ka noise in the network */
1038 void pim_msdp_peer_pkt_txed(struct pim_msdp_peer
*mp
)
1040 if (mp
->state
== PIM_MSDP_ESTABLISHED
) {
1041 pim_msdp_peer_ka_timer_setup(mp
, true /* start */);
1042 if (PIM_DEBUG_MSDP_INTERNAL
) {
1043 zlog_debug("MSDP ka timer restart on pkt tx to %s",
1049 static void pim_msdp_addr2su(union sockunion
*su
, struct in_addr addr
)
1052 su
->sin
.sin_addr
= addr
;
1053 su
->sin
.sin_family
= AF_INET
;
1054 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1055 su
->sin
.sin_len
= sizeof(struct sockaddr_in
);
1056 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1059 /* 11.2.A1: create a new peer and transition state to listen or connecting */
1060 static enum pim_msdp_err
pim_msdp_peer_new(struct pim_instance
*pim
,
1061 struct in_addr peer_addr
,
1062 struct in_addr local_addr
,
1063 const char *mesh_group_name
,
1064 struct pim_msdp_peer
**mp_p
)
1066 struct pim_msdp_peer
*mp
;
1068 pim_msdp_enable(pim
);
1070 mp
= XCALLOC(MTYPE_PIM_MSDP_PEER
, sizeof(*mp
));
1073 mp
->peer
= peer_addr
;
1074 pim_inet4_dump("<peer?>", mp
->peer
, mp
->key_str
, sizeof(mp
->key_str
));
1075 pim_msdp_addr2su(&mp
->su_peer
, mp
->peer
);
1076 mp
->local
= local_addr
;
1077 /* XXX: originator_id setting needs to move to the mesh group */
1078 pim
->msdp
.originator_id
= local_addr
;
1079 pim_msdp_addr2su(&mp
->su_local
, mp
->local
);
1080 mp
->mesh_group_name
= XSTRDUP(MTYPE_PIM_MSDP_MG_NAME
, mesh_group_name
);
1081 mp
->state
= PIM_MSDP_INACTIVE
;
1083 strlcpy(mp
->last_reset
, "-", sizeof(mp
->last_reset
));
1084 /* higher IP address is listener */
1085 if (ntohl(mp
->local
.s_addr
) > ntohl(mp
->peer
.s_addr
)) {
1086 mp
->flags
|= PIM_MSDP_PEERF_LISTENER
;
1089 /* setup packet buffers */
1090 mp
->ibuf
= stream_new(PIM_MSDP_MAX_PACKET_SIZE
);
1091 mp
->obuf
= stream_fifo_new();
1093 /* insert into misc tables for easy access */
1094 mp
= hash_get(pim
->msdp
.peer_hash
, mp
, hash_alloc_intern
);
1095 listnode_add_sort(pim
->msdp
.peer_list
, mp
);
1097 if (PIM_DEBUG_MSDP_EVENTS
) {
1098 zlog_debug("MSDP peer %s created", mp
->key_str
);
1100 pim_msdp_peer_state_chg_log(mp
);
1103 /* fireup the connect state machine */
1104 if (PIM_MSDP_PEER_IS_LISTENER(mp
)) {
1105 pim_msdp_peer_listen(mp
);
1107 pim_msdp_peer_connect(mp
);
1112 return PIM_MSDP_ERR_NONE
;
1115 struct pim_msdp_peer
*pim_msdp_peer_find(struct pim_instance
*pim
,
1116 struct in_addr peer_addr
)
1118 struct pim_msdp_peer lookup
;
1120 lookup
.peer
= peer_addr
;
1121 return hash_lookup(pim
->msdp
.peer_hash
, &lookup
);
1124 /* add peer configuration if it doesn't already exist */
1125 enum pim_msdp_err
pim_msdp_peer_add(struct pim_instance
*pim
,
1126 struct in_addr peer_addr
,
1127 struct in_addr local_addr
,
1128 const char *mesh_group_name
,
1129 struct pim_msdp_peer
**mp_p
)
1131 struct pim_msdp_peer
*mp
;
1137 if (peer_addr
.s_addr
== local_addr
.s_addr
) {
1138 /* skip session setup if config is invalid */
1139 if (PIM_DEBUG_MSDP_EVENTS
) {
1140 char peer_str
[INET_ADDRSTRLEN
];
1142 pim_inet4_dump("<peer?>", peer_addr
, peer_str
,
1144 zlog_debug("%s add skipped as DIP=SIP", peer_str
);
1146 return PIM_MSDP_ERR_SIP_EQ_DIP
;
1149 mp
= pim_msdp_peer_find(pim
, peer_addr
);
1154 return PIM_MSDP_ERR_PEER_EXISTS
;
1157 return pim_msdp_peer_new(pim
, peer_addr
, local_addr
, mesh_group_name
,
1161 /* release all mem associated with a peer */
1162 static void pim_msdp_peer_free(struct pim_msdp_peer
*mp
)
1165 * Let's make sure we are not running when we delete
1166 * the underlying data structure
1168 pim_msdp_peer_stop_tcp_conn(mp
, false);
1171 stream_free(mp
->ibuf
);
1175 stream_fifo_free(mp
->obuf
);
1178 XFREE(MTYPE_PIM_MSDP_MG_NAME
, mp
->mesh_group_name
);
1181 XFREE(MTYPE_PIM_MSDP_PEER
, mp
);
1184 /* delete the peer config */
1185 static enum pim_msdp_err
pim_msdp_peer_do_del(struct pim_msdp_peer
*mp
)
1187 /* stop the tcp connection and shutdown all timers */
1188 pim_msdp_peer_stop_tcp_conn(mp
, true /* chg_state */);
1190 /* remove the session from various tables */
1191 listnode_delete(mp
->pim
->msdp
.peer_list
, mp
);
1192 hash_release(mp
->pim
->msdp
.peer_hash
, mp
);
1194 if (PIM_DEBUG_MSDP_EVENTS
) {
1195 zlog_debug("MSDP peer %s deleted", mp
->key_str
);
1198 /* free up any associated memory */
1199 pim_msdp_peer_free(mp
);
1201 return PIM_MSDP_ERR_NONE
;
1204 enum pim_msdp_err
pim_msdp_peer_del(struct pim_instance
*pim
,
1205 struct in_addr peer_addr
)
1207 struct pim_msdp_peer
*mp
;
1209 mp
= pim_msdp_peer_find(pim
, peer_addr
);
1211 return PIM_MSDP_ERR_NO_PEER
;
1214 return pim_msdp_peer_do_del(mp
);
1217 /* peer hash and peer list helpers */
1218 static unsigned int pim_msdp_peer_hash_key_make(const void *p
)
1220 const struct pim_msdp_peer
*mp
= p
;
1221 return (jhash_1word(mp
->peer
.s_addr
, 0));
1224 static bool pim_msdp_peer_hash_eq(const void *p1
, const void *p2
)
1226 const struct pim_msdp_peer
*mp1
= p1
;
1227 const struct pim_msdp_peer
*mp2
= p2
;
1229 return (mp1
->peer
.s_addr
== mp2
->peer
.s_addr
);
1232 static int pim_msdp_peer_comp(const void *p1
, const void *p2
)
1234 const struct pim_msdp_peer
*mp1
= p1
;
1235 const struct pim_msdp_peer
*mp2
= p2
;
1237 if (ntohl(mp1
->peer
.s_addr
) < ntohl(mp2
->peer
.s_addr
))
1240 if (ntohl(mp1
->peer
.s_addr
) > ntohl(mp2
->peer
.s_addr
))
1246 /************************** Mesh group management **************************/
1247 static void pim_msdp_mg_free(struct pim_instance
*pim
)
1249 struct pim_msdp_mg
*mg
= pim
->msdp
.mg
;
1251 /* If the mesh-group has valid member or src_ip don't delete it */
1252 if (!mg
|| mg
->mbr_cnt
|| (mg
->src_ip
.s_addr
!= INADDR_ANY
)) {
1256 if (PIM_DEBUG_MSDP_EVENTS
) {
1257 zlog_debug("MSDP mesh-group %s deleted", mg
->mesh_group_name
);
1259 XFREE(MTYPE_PIM_MSDP_MG_NAME
, mg
->mesh_group_name
);
1262 list_delete(&mg
->mbr_list
);
1264 XFREE(MTYPE_PIM_MSDP_MG
, pim
->msdp
.mg
);
1267 static struct pim_msdp_mg
*pim_msdp_mg_new(const char *mesh_group_name
)
1269 struct pim_msdp_mg
*mg
;
1271 mg
= XCALLOC(MTYPE_PIM_MSDP_MG
, sizeof(*mg
));
1273 mg
->mesh_group_name
= XSTRDUP(MTYPE_PIM_MSDP_MG_NAME
, mesh_group_name
);
1274 mg
->mbr_list
= list_new();
1275 mg
->mbr_list
->del
= (void (*)(void *))pim_msdp_mg_mbr_free
;
1276 mg
->mbr_list
->cmp
= (int (*)(void *, void *))pim_msdp_mg_mbr_comp
;
1278 if (PIM_DEBUG_MSDP_EVENTS
) {
1279 zlog_debug("MSDP mesh-group %s created", mg
->mesh_group_name
);
1284 enum pim_msdp_err
pim_msdp_mg_del(struct pim_instance
*pim
,
1285 const char *mesh_group_name
)
1287 struct pim_msdp_mg
*mg
= pim
->msdp
.mg
;
1288 struct pim_msdp_mg_mbr
*mbr
;
1290 if (!mg
|| strcmp(mg
->mesh_group_name
, mesh_group_name
)) {
1291 return PIM_MSDP_ERR_NO_MG
;
1294 /* delete all the mesh-group members */
1295 while (!list_isempty(mg
->mbr_list
)) {
1296 mbr
= listnode_head(mg
->mbr_list
);
1297 pim_msdp_mg_mbr_do_del(mg
, mbr
);
1301 mg
->src_ip
.s_addr
= INADDR_ANY
;
1303 /* free up the mesh-group */
1304 pim_msdp_mg_free(pim
);
1305 return PIM_MSDP_ERR_NONE
;
1308 static enum pim_msdp_err
pim_msdp_mg_add(struct pim_instance
*pim
,
1309 const char *mesh_group_name
)
1312 if (!strcmp(pim
->msdp
.mg
->mesh_group_name
, mesh_group_name
)) {
1313 return PIM_MSDP_ERR_NONE
;
1315 /* currently only one mesh-group can exist at a time */
1316 return PIM_MSDP_ERR_MAX_MESH_GROUPS
;
1319 pim
->msdp
.mg
= pim_msdp_mg_new(mesh_group_name
);
1320 if (!pim
->msdp
.mg
) {
1321 return PIM_MSDP_ERR_OOM
;
1324 return PIM_MSDP_ERR_NONE
;
1327 static int pim_msdp_mg_mbr_comp(const void *p1
, const void *p2
)
1329 const struct pim_msdp_mg_mbr
*mbr1
= p1
;
1330 const struct pim_msdp_mg_mbr
*mbr2
= p2
;
1332 if (ntohl(mbr1
->mbr_ip
.s_addr
) < ntohl(mbr2
->mbr_ip
.s_addr
))
1335 if (ntohl(mbr1
->mbr_ip
.s_addr
) > ntohl(mbr2
->mbr_ip
.s_addr
))
1341 static void pim_msdp_mg_mbr_free(struct pim_msdp_mg_mbr
*mbr
)
1343 XFREE(MTYPE_PIM_MSDP_MG_MBR
, mbr
);
1346 static struct pim_msdp_mg_mbr
*pim_msdp_mg_mbr_find(struct pim_instance
*pim
,
1347 struct in_addr mbr_ip
)
1349 struct pim_msdp_mg_mbr
*mbr
;
1350 struct listnode
*mbr_node
;
1352 if (!pim
->msdp
.mg
) {
1355 /* we can move this to a hash but considering that number of peers in
1356 * a mesh-group that seems like bit of an overkill */
1357 for (ALL_LIST_ELEMENTS_RO(pim
->msdp
.mg
->mbr_list
, mbr_node
, mbr
)) {
1358 if (mbr
->mbr_ip
.s_addr
== mbr_ip
.s_addr
) {
1365 enum pim_msdp_err
pim_msdp_mg_mbr_add(struct pim_instance
*pim
,
1366 const char *mesh_group_name
,
1367 struct in_addr mbr_ip
)
1370 struct pim_msdp_mg_mbr
*mbr
;
1371 struct pim_msdp_mg
*mg
;
1373 rc
= pim_msdp_mg_add(pim
, mesh_group_name
);
1374 if (rc
!= PIM_MSDP_ERR_NONE
) {
1379 mbr
= pim_msdp_mg_mbr_find(pim
, mbr_ip
);
1381 return PIM_MSDP_ERR_MG_MBR_EXISTS
;
1384 mbr
= XCALLOC(MTYPE_PIM_MSDP_MG_MBR
, sizeof(*mbr
));
1385 mbr
->mbr_ip
= mbr_ip
;
1386 listnode_add_sort(mg
->mbr_list
, mbr
);
1388 /* if valid SIP has been configured add peer session */
1389 if (mg
->src_ip
.s_addr
!= INADDR_ANY
) {
1390 pim_msdp_peer_add(pim
, mbr_ip
, mg
->src_ip
, mesh_group_name
,
1394 if (PIM_DEBUG_MSDP_EVENTS
) {
1395 char ip_str
[INET_ADDRSTRLEN
];
1396 pim_inet4_dump("<mbr?>", mbr
->mbr_ip
, ip_str
, sizeof(ip_str
));
1397 zlog_debug("MSDP mesh-group %s mbr %s created",
1398 mg
->mesh_group_name
, ip_str
);
1401 return PIM_MSDP_ERR_NONE
;
1404 static void pim_msdp_mg_mbr_do_del(struct pim_msdp_mg
*mg
,
1405 struct pim_msdp_mg_mbr
*mbr
)
1407 /* Delete active peer session if any */
1409 pim_msdp_peer_do_del(mbr
->mp
);
1412 listnode_delete(mg
->mbr_list
, mbr
);
1413 if (PIM_DEBUG_MSDP_EVENTS
) {
1414 char ip_str
[INET_ADDRSTRLEN
];
1415 pim_inet4_dump("<mbr?>", mbr
->mbr_ip
, ip_str
, sizeof(ip_str
));
1416 zlog_debug("MSDP mesh-group %s mbr %s deleted",
1417 mg
->mesh_group_name
, ip_str
);
1419 pim_msdp_mg_mbr_free(mbr
);
1425 enum pim_msdp_err
pim_msdp_mg_mbr_del(struct pim_instance
*pim
,
1426 const char *mesh_group_name
,
1427 struct in_addr mbr_ip
)
1429 struct pim_msdp_mg_mbr
*mbr
;
1430 struct pim_msdp_mg
*mg
= pim
->msdp
.mg
;
1432 if (!mg
|| strcmp(mg
->mesh_group_name
, mesh_group_name
)) {
1433 return PIM_MSDP_ERR_NO_MG
;
1436 mbr
= pim_msdp_mg_mbr_find(pim
, mbr_ip
);
1438 return PIM_MSDP_ERR_NO_MG_MBR
;
1441 pim_msdp_mg_mbr_do_del(mg
, mbr
);
1442 /* if there are no references to the mg free it */
1443 pim_msdp_mg_free(pim
);
1445 return PIM_MSDP_ERR_NONE
;
1448 static void pim_msdp_mg_src_do_del(struct pim_instance
*pim
)
1450 struct pim_msdp_mg_mbr
*mbr
;
1451 struct listnode
*mbr_node
;
1452 struct pim_msdp_mg
*mg
= pim
->msdp
.mg
;
1454 /* SIP is being removed - tear down all active peer sessions */
1455 for (ALL_LIST_ELEMENTS_RO(mg
->mbr_list
, mbr_node
, mbr
)) {
1457 pim_msdp_peer_do_del(mbr
->mp
);
1461 if (PIM_DEBUG_MSDP_EVENTS
) {
1462 zlog_debug("MSDP mesh-group %s src cleared",
1463 mg
->mesh_group_name
);
1467 enum pim_msdp_err
pim_msdp_mg_src_del(struct pim_instance
*pim
,
1468 const char *mesh_group_name
)
1470 struct pim_msdp_mg
*mg
= pim
->msdp
.mg
;
1472 if (!mg
|| strcmp(mg
->mesh_group_name
, mesh_group_name
)) {
1473 return PIM_MSDP_ERR_NO_MG
;
1476 if (mg
->src_ip
.s_addr
!= INADDR_ANY
) {
1477 mg
->src_ip
.s_addr
= INADDR_ANY
;
1478 pim_msdp_mg_src_do_del(pim
);
1479 /* if there are no references to the mg free it */
1480 pim_msdp_mg_free(pim
);
1482 return PIM_MSDP_ERR_NONE
;
1485 enum pim_msdp_err
pim_msdp_mg_src_add(struct pim_instance
*pim
,
1486 const char *mesh_group_name
,
1487 struct in_addr src_ip
)
1490 struct pim_msdp_mg_mbr
*mbr
;
1491 struct listnode
*mbr_node
;
1492 struct pim_msdp_mg
*mg
;
1494 if (src_ip
.s_addr
== INADDR_ANY
) {
1495 pim_msdp_mg_src_del(pim
, mesh_group_name
);
1496 return PIM_MSDP_ERR_NONE
;
1499 rc
= pim_msdp_mg_add(pim
, mesh_group_name
);
1500 if (rc
!= PIM_MSDP_ERR_NONE
) {
1505 if (mg
->src_ip
.s_addr
!= INADDR_ANY
) {
1506 pim_msdp_mg_src_do_del(pim
);
1508 mg
->src_ip
= src_ip
;
1510 for (ALL_LIST_ELEMENTS_RO(mg
->mbr_list
, mbr_node
, mbr
)) {
1511 pim_msdp_peer_add(pim
, mbr
->mbr_ip
, mg
->src_ip
, mesh_group_name
,
1515 if (PIM_DEBUG_MSDP_EVENTS
) {
1516 char ip_str
[INET_ADDRSTRLEN
];
1517 pim_inet4_dump("<src?>", mg
->src_ip
, ip_str
, sizeof(ip_str
));
1518 zlog_debug("MSDP mesh-group %s src %s set", mg
->mesh_group_name
,
1521 return PIM_MSDP_ERR_NONE
;
1524 /*********************** MSDP feature APIs *********************************/
1525 int pim_msdp_config_write(struct pim_instance
*pim
, struct vty
*vty
,
1528 struct listnode
*mbrnode
;
1529 struct pim_msdp_mg_mbr
*mbr
;
1530 struct pim_msdp_mg
*mg
= pim
->msdp
.mg
;
1531 char mbr_str
[INET_ADDRSTRLEN
];
1532 char src_str
[INET_ADDRSTRLEN
];
1539 if (mg
->src_ip
.s_addr
!= INADDR_ANY
) {
1540 pim_inet4_dump("<src?>", mg
->src_ip
, src_str
, sizeof(src_str
));
1541 vty_out(vty
, "%sip msdp mesh-group %s source %s\n", spaces
,
1542 mg
->mesh_group_name
, src_str
);
1546 for (ALL_LIST_ELEMENTS_RO(mg
->mbr_list
, mbrnode
, mbr
)) {
1547 pim_inet4_dump("<mbr?>", mbr
->mbr_ip
, mbr_str
, sizeof(mbr_str
));
1548 vty_out(vty
, "%sip msdp mesh-group %s member %s\n", spaces
,
1549 mg
->mesh_group_name
, mbr_str
);
1555 /* Enable feature including active/periodic timers etc. on the first peer
1556 * config. Till then MSDP should just stay quiet. */
1557 static void pim_msdp_enable(struct pim_instance
*pim
)
1559 if (pim
->msdp
.flags
& PIM_MSDPF_ENABLE
) {
1560 /* feature is already enabled */
1563 pim
->msdp
.flags
|= PIM_MSDPF_ENABLE
;
1564 pim
->msdp
.work_obuf
= stream_new(PIM_MSDP_MAX_PACKET_SIZE
);
1565 pim_msdp_sa_adv_timer_setup(pim
, true /* start */);
1566 /* setup sa cache based on local sources */
1567 pim_msdp_sa_local_setup(pim
);
1571 void pim_msdp_init(struct pim_instance
*pim
, struct thread_master
*master
)
1573 pim
->msdp
.master
= master
;
1576 snprintf(hash_name
, 64, "PIM %s MSDP Peer Hash", pim
->vrf
->name
);
1577 pim
->msdp
.peer_hash
= hash_create(pim_msdp_peer_hash_key_make
,
1578 pim_msdp_peer_hash_eq
, hash_name
);
1579 pim
->msdp
.peer_list
= list_new();
1580 pim
->msdp
.peer_list
->del
= (void (*)(void *))pim_msdp_peer_free
;
1581 pim
->msdp
.peer_list
->cmp
= (int (*)(void *, void *))pim_msdp_peer_comp
;
1583 snprintf(hash_name
, 64, "PIM %s MSDP SA Hash", pim
->vrf
->name
);
1584 pim
->msdp
.sa_hash
= hash_create(pim_msdp_sa_hash_key_make
,
1585 pim_msdp_sa_hash_eq
, hash_name
);
1586 pim
->msdp
.sa_list
= list_new();
1587 pim
->msdp
.sa_list
->del
= (void (*)(void *))pim_msdp_sa_free
;
1588 pim
->msdp
.sa_list
->cmp
= (int (*)(void *, void *))pim_msdp_sa_comp
;
1591 /* counterpart to MSDP init; XXX: unused currently */
1592 void pim_msdp_exit(struct pim_instance
*pim
)
1594 pim_msdp_sa_adv_timer_setup(pim
, false);
1596 /* XXX: stop listener and delete all peer sessions */
1598 pim_msdp_mg_free(pim
);
1600 if (pim
->msdp
.peer_hash
) {
1601 hash_clean(pim
->msdp
.peer_hash
, NULL
);
1602 hash_free(pim
->msdp
.peer_hash
);
1603 pim
->msdp
.peer_hash
= NULL
;
1606 if (pim
->msdp
.peer_list
) {
1607 list_delete(&pim
->msdp
.peer_list
);
1610 if (pim
->msdp
.sa_hash
) {
1611 hash_clean(pim
->msdp
.sa_hash
, NULL
);
1612 hash_free(pim
->msdp
.sa_hash
);
1613 pim
->msdp
.sa_hash
= NULL
;
1616 if (pim
->msdp
.sa_list
) {
1617 list_delete(&pim
->msdp
.sa_list
);
1620 if (pim
->msdp
.work_obuf
)
1621 stream_free(pim
->msdp
.work_obuf
);
1622 pim
->msdp
.work_obuf
= NULL
;