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>
34 #include "pim_memory.h"
35 #include "pim_iface.h"
39 #include "pim_upstream.h"
43 #include "pim_msdp_packet.h"
44 #include "pim_msdp_socket.h"
46 // struct pim_msdp pim_msdp, *msdp = &pim_msdp;
48 static void pim_msdp_peer_listen(struct pim_msdp_peer
*mp
);
49 static void pim_msdp_peer_cr_timer_setup(struct pim_msdp_peer
*mp
, bool start
);
50 static void pim_msdp_peer_ka_timer_setup(struct pim_msdp_peer
*mp
, bool start
);
51 static void pim_msdp_peer_hold_timer_setup(struct pim_msdp_peer
*mp
,
53 static void pim_msdp_peer_free(struct pim_msdp_peer
*mp
);
54 static void pim_msdp_enable(struct pim_instance
*pim
);
55 static void pim_msdp_sa_adv_timer_setup(struct pim_instance
*pim
, bool start
);
56 static void pim_msdp_sa_deref(struct pim_msdp_sa
*sa
,
57 enum pim_msdp_sa_flags flags
);
58 static int pim_msdp_mg_mbr_comp(const void *p1
, const void *p2
);
59 static void pim_msdp_mg_mbr_free(struct pim_msdp_mg_mbr
*mbr
);
60 static void pim_msdp_mg_mbr_do_del(struct pim_msdp_mg
*mg
,
61 struct pim_msdp_mg_mbr
*mbr
);
63 /************************ SA cache management ******************************/
64 static void pim_msdp_sa_timer_expiry_log(struct pim_msdp_sa
*sa
,
65 const char *timer_str
)
67 zlog_debug("MSDP SA %s %s timer expired", sa
->sg_str
, timer_str
);
70 /* RFC-3618:Sec-5.1 - global active source advertisement timer */
71 static int pim_msdp_sa_adv_timer_cb(struct thread
*t
)
73 struct pim_instance
*pim
= THREAD_ARG(t
);
75 if (PIM_DEBUG_MSDP_EVENTS
) {
76 zlog_debug("MSDP SA advertisment timer expired");
79 pim_msdp_sa_adv_timer_setup(pim
, true /* start */);
80 pim_msdp_pkt_sa_tx(pim
);
83 static void pim_msdp_sa_adv_timer_setup(struct pim_instance
*pim
, bool start
)
85 THREAD_OFF(pim
->msdp
.sa_adv_timer
);
87 thread_add_timer(pim
->msdp
.master
, pim_msdp_sa_adv_timer_cb
,
88 pim
, PIM_MSDP_SA_ADVERTISMENT_TIME
,
89 &pim
->msdp
.sa_adv_timer
);
93 /* RFC-3618:Sec-5.3 - SA cache state timer */
94 static int pim_msdp_sa_state_timer_cb(struct thread
*t
)
96 struct pim_msdp_sa
*sa
;
100 if (PIM_DEBUG_MSDP_EVENTS
) {
101 pim_msdp_sa_timer_expiry_log(sa
, "state");
104 pim_msdp_sa_deref(sa
, PIM_MSDP_SAF_PEER
);
107 static void pim_msdp_sa_state_timer_setup(struct pim_msdp_sa
*sa
, bool start
)
109 THREAD_OFF(sa
->sa_state_timer
);
111 thread_add_timer(sa
->pim
->msdp
.master
,
112 pim_msdp_sa_state_timer_cb
, sa
,
113 PIM_MSDP_SA_HOLD_TIME
, &sa
->sa_state_timer
);
117 static void pim_msdp_sa_upstream_del(struct pim_msdp_sa
*sa
)
119 struct pim_upstream
*up
= sa
->up
;
125 if (PIM_UPSTREAM_FLAG_TEST_SRC_MSDP(up
->flags
)) {
126 PIM_UPSTREAM_FLAG_UNSET_SRC_MSDP(up
->flags
);
127 sa
->flags
|= PIM_MSDP_SAF_UP_DEL_IN_PROG
;
128 pim_upstream_del(sa
->pim
, up
, __PRETTY_FUNCTION__
);
129 sa
->flags
&= ~PIM_MSDP_SAF_UP_DEL_IN_PROG
;
132 if (PIM_DEBUG_MSDP_EVENTS
) {
133 zlog_debug("MSDP SA %s de-referenced SPT", sa
->sg_str
);
137 static bool pim_msdp_sa_upstream_add_ok(struct pim_msdp_sa
*sa
,
138 struct pim_upstream
*xg_up
)
140 if (!(sa
->flags
& PIM_MSDP_SAF_PEER
)) {
141 /* SA should have been rxed from a peer */
144 /* check if we are RP */
145 if (!I_am_RP(sa
->pim
, sa
->sg
.grp
)) {
149 /* check if we have a (*, G) with a non-empty immediate OIL */
153 memset(&sg
, 0, sizeof(sg
));
156 xg_up
= pim_upstream_find(sa
->pim
, &sg
);
158 if (!xg_up
|| (xg_up
->join_state
!= PIM_UPSTREAM_JOINED
)) {
159 /* join desired will be true for such (*, G) entries so we will
160 * just look at join_state and let the PIM state machine do the
169 /* Upstream add evaluation needs to happen everytime -
170 * 1. Peer reference is added or removed.
171 * 2. The RP for a group changes.
172 * 3. joinDesired for the associated (*, G) changes
173 * 4. associated (*, G) is removed - this seems like a bit redundant
174 * (considering #4); but just in case an entry gets nuked without
175 * upstream state transition
177 static void pim_msdp_sa_upstream_update(struct pim_msdp_sa
*sa
,
178 struct pim_upstream
*xg_up
,
181 struct pim_upstream
*up
;
183 if (!pim_msdp_sa_upstream_add_ok(sa
, xg_up
)) {
184 pim_msdp_sa_upstream_del(sa
);
193 up
= pim_upstream_find(sa
->pim
, &sa
->sg
);
194 if (up
&& (PIM_UPSTREAM_FLAG_TEST_SRC_MSDP(up
->flags
))) {
195 /* somehow we lost track of the upstream ptr? best log it */
197 if (PIM_DEBUG_MSDP_EVENTS
) {
198 zlog_debug("MSDP SA %s SPT reference missing",
204 /* RFC3618: "RP triggers a (S, G) join event towards the data source
205 * as if a JP message was rxed addressed to the RP itself." */
206 up
= pim_upstream_add(sa
->pim
, &sa
->sg
, NULL
/* iif */,
207 PIM_UPSTREAM_FLAG_MASK_SRC_MSDP
,
208 __PRETTY_FUNCTION__
, NULL
);
212 /* update inherited oil */
213 pim_upstream_inherited_olist(sa
->pim
, up
);
214 /* should we also start the kat in parallel? we will need it
217 if (PIM_DEBUG_MSDP_EVENTS
) {
218 zlog_debug("MSDP SA %s referenced SPT", sa
->sg_str
);
221 if (PIM_DEBUG_MSDP_EVENTS
) {
222 zlog_debug("MSDP SA %s SPT reference failed",
228 /* release all mem associated with a sa */
229 static void pim_msdp_sa_free(struct pim_msdp_sa
*sa
)
231 XFREE(MTYPE_PIM_MSDP_SA
, sa
);
234 static struct pim_msdp_sa
*pim_msdp_sa_new(struct pim_instance
*pim
,
235 struct prefix_sg
*sg
,
238 struct pim_msdp_sa
*sa
;
240 sa
= XCALLOC(MTYPE_PIM_MSDP_SA
, sizeof(*sa
));
242 zlog_err("%s: PIM XCALLOC(%zu) failure", __PRETTY_FUNCTION__
,
249 pim_str_sg_set(sg
, sa
->sg_str
);
251 sa
->uptime
= pim_time_monotonic_sec();
253 /* insert into misc tables for easy access */
254 sa
= hash_get(pim
->msdp
.sa_hash
, sa
, hash_alloc_intern
);
256 zlog_err("%s: PIM hash get failure", __PRETTY_FUNCTION__
);
257 pim_msdp_sa_free(sa
);
260 listnode_add_sort(pim
->msdp
.sa_list
, sa
);
262 if (PIM_DEBUG_MSDP_EVENTS
) {
263 zlog_debug("MSDP SA %s created", sa
->sg_str
);
269 static struct pim_msdp_sa
*pim_msdp_sa_find(struct pim_instance
*pim
,
270 struct prefix_sg
*sg
)
272 struct pim_msdp_sa lookup
;
275 return hash_lookup(pim
->msdp
.sa_hash
, &lookup
);
278 static struct pim_msdp_sa
*pim_msdp_sa_add(struct pim_instance
*pim
,
279 struct prefix_sg
*sg
,
282 struct pim_msdp_sa
*sa
;
284 sa
= pim_msdp_sa_find(pim
, sg
);
289 return pim_msdp_sa_new(pim
, sg
, rp
);
292 static void pim_msdp_sa_del(struct pim_msdp_sa
*sa
)
294 /* this is somewhat redundant - still want to be careful not to leave
295 * stale upstream references */
296 pim_msdp_sa_upstream_del(sa
);
299 pim_msdp_sa_state_timer_setup(sa
, false /* start */);
301 /* remove the entry from various tables */
302 listnode_delete(sa
->pim
->msdp
.sa_list
, sa
);
303 hash_release(sa
->pim
->msdp
.sa_hash
, sa
);
305 if (PIM_DEBUG_MSDP_EVENTS
) {
306 zlog_debug("MSDP SA %s deleted", sa
->sg_str
);
309 /* free up any associated memory */
310 pim_msdp_sa_free(sa
);
313 static void pim_msdp_sa_peer_ip_set(struct pim_msdp_sa
*sa
,
314 struct pim_msdp_peer
*mp
, struct in_addr rp
)
316 struct pim_msdp_peer
*old_mp
;
318 /* optimize the "no change" case as it will happen
319 * frequently/periodically */
320 if (mp
&& (sa
->peer
.s_addr
== mp
->peer
.s_addr
)) {
324 /* any time the peer ip changes also update the rp address */
325 if (PIM_INADDR_ISNOT_ANY(sa
->peer
)) {
326 old_mp
= pim_msdp_peer_find(sa
->pim
, sa
->peer
);
327 if (old_mp
&& old_mp
->sa_cnt
) {
336 sa
->peer
.s_addr
= PIM_NET_INADDR_ANY
;
341 /* When a local active-source is removed there is no way to withdraw the
342 * source from peers. We will simply remove it from the SA cache so it will
343 * not be sent in supsequent SA updates. Peers will consequently timeout the
345 * Similarly a "peer-added" SA is never explicitly deleted. It is simply
346 * aged out overtime if not seen in the SA updates from the peers.
347 * XXX: should we provide a knob to drop entries learnt from a peer when the
349 static void pim_msdp_sa_deref(struct pim_msdp_sa
*sa
,
350 enum pim_msdp_sa_flags flags
)
352 bool update_up
= false;
354 if ((sa
->flags
& PIM_MSDP_SAF_LOCAL
)) {
355 if (flags
& PIM_MSDP_SAF_LOCAL
) {
356 if (PIM_DEBUG_MSDP_EVENTS
) {
357 zlog_debug("MSDP SA %s local reference removed",
360 if (sa
->pim
->msdp
.local_cnt
)
361 --sa
->pim
->msdp
.local_cnt
;
365 if ((sa
->flags
& PIM_MSDP_SAF_PEER
)) {
366 if (flags
& PIM_MSDP_SAF_PEER
) {
369 if (PIM_DEBUG_MSDP_EVENTS
) {
370 zlog_debug("MSDP SA %s peer reference removed",
373 pim_msdp_sa_state_timer_setup(sa
, false /* start */);
374 rp
.s_addr
= INADDR_ANY
;
375 pim_msdp_sa_peer_ip_set(sa
, NULL
/* mp */, rp
);
376 /* if peer ref was removed we need to remove the msdp
385 pim_msdp_sa_upstream_update(sa
, NULL
/* xg_up */, "sa-deref");
388 if (!(sa
->flags
& PIM_MSDP_SAF_REF
)) {
393 void pim_msdp_sa_ref(struct pim_instance
*pim
, struct pim_msdp_peer
*mp
,
394 struct prefix_sg
*sg
, struct in_addr rp
)
396 struct pim_msdp_sa
*sa
;
398 sa
= pim_msdp_sa_add(pim
, sg
, rp
);
405 if (!(sa
->flags
& PIM_MSDP_SAF_PEER
)) {
406 sa
->flags
|= PIM_MSDP_SAF_PEER
;
407 if (PIM_DEBUG_MSDP_EVENTS
) {
408 zlog_debug("MSDP SA %s added by peer",
412 pim_msdp_sa_peer_ip_set(sa
, mp
, rp
);
413 /* start/re-start the state timer to prevent cache expiry */
414 pim_msdp_sa_state_timer_setup(sa
, true /* start */);
415 /* We re-evaluate SA "SPT-trigger" everytime we hear abt it from
417 * peer. XXX: If this becomes too much of a periodic overhead we
418 * can make it event based */
419 pim_msdp_sa_upstream_update(sa
, NULL
/* xg_up */, "peer-ref");
421 if (!(sa
->flags
& PIM_MSDP_SAF_LOCAL
)) {
422 sa
->flags
|= PIM_MSDP_SAF_LOCAL
;
423 ++sa
->pim
->msdp
.local_cnt
;
424 if (PIM_DEBUG_MSDP_EVENTS
) {
425 zlog_debug("MSDP SA %s added locally",
428 /* send an immediate SA update to peers */
429 pim_msdp_pkt_sa_tx_one(sa
);
431 sa
->flags
&= ~PIM_MSDP_SAF_STALE
;
435 /* The following criteria must be met to originate an SA from the MSDP
437 * 1. KAT must be running i.e. source is active.
438 * 2. We must be RP for the group.
439 * 3. Source must be registrable to the RP (this is where the RFC is vague
440 * and especially ambiguous in CLOS networks; with anycast RP all sources
441 * are potentially registrable to all RPs in the domain). We assume #3 is
443 * a. We are also the FHR-DR for the source (OR)
444 * b. We rxed a pim register (null or data encapsulated) within the last
445 * (3 * (1.5 * register_suppression_timer))).
447 static bool pim_msdp_sa_local_add_ok(struct pim_upstream
*up
)
449 struct pim_instance
*pim
= up
->channel_oil
->pim
;
451 if (!(pim
->msdp
.flags
& PIM_MSDPF_ENABLE
)) {
455 if (!up
->t_ka_timer
) {
456 /* stream is not active */
460 if (!I_am_RP(pim
, up
->sg
.grp
)) {
461 /* we are not RP for the group */
465 /* we are the FHR-DR for this stream or we are RP and have seen
467 * from a FHR for this source */
468 if (PIM_UPSTREAM_FLAG_TEST_FHR(up
->flags
) || up
->t_msdp_reg_timer
) {
475 static void pim_msdp_sa_local_add(struct pim_instance
*pim
,
476 struct prefix_sg
*sg
)
480 pim_msdp_sa_ref(pim
, NULL
/* mp */, sg
, rp
);
483 void pim_msdp_sa_local_del(struct pim_instance
*pim
, struct prefix_sg
*sg
)
485 struct pim_msdp_sa
*sa
;
487 sa
= pim_msdp_sa_find(pim
, sg
);
489 pim_msdp_sa_deref(sa
, PIM_MSDP_SAF_LOCAL
);
493 /* we need to be very cautious with this API as SA del too can trigger an
494 * upstream del and we will get stuck in a simple loop */
495 static void pim_msdp_sa_local_del_on_up_del(struct pim_instance
*pim
,
496 struct prefix_sg
*sg
)
498 struct pim_msdp_sa
*sa
;
500 sa
= pim_msdp_sa_find(pim
, sg
);
502 if (PIM_DEBUG_MSDP_INTERNAL
) {
503 zlog_debug("MSDP local sa %s del on up del",
507 /* if there is no local reference escape */
508 if (!(sa
->flags
& PIM_MSDP_SAF_LOCAL
)) {
509 if (PIM_DEBUG_MSDP_INTERNAL
) {
510 zlog_debug("MSDP local sa %s del; no local ref",
516 if (sa
->flags
& PIM_MSDP_SAF_UP_DEL_IN_PROG
) {
517 /* MSDP is the one that triggered the upstream del. if
519 * we most certainly have a bug in the PIM upstream
521 * will not have a local reference unless the KAT is
522 * running. And if the
523 * KAT is running there MUST be an additional
524 * source-stream reference to
525 * the flow. Accounting for such cases requires lot of
527 * address this in the next release? - XXX */
529 "MSDP sa %s SPT teardown is causing the local entry to be removed",
534 /* we are dropping the sa on upstream del we should not have an
535 * upstream reference */
537 if (PIM_DEBUG_MSDP_INTERNAL
) {
538 zlog_debug("MSDP local sa %s del; up non-NULL",
543 pim_msdp_sa_deref(sa
, PIM_MSDP_SAF_LOCAL
);
547 /* Local SA qualification needs to be re-evaluated when -
548 * 1. KAT is started or stopped
550 * 3. Whenever FHR status changes for a (S,G) - XXX - currently there
551 * is no clear path to transition an entry out of "MASK_FHR" need
552 * to discuss this with Donald. May result in some strangeness if the
553 * FHR is also the RP.
554 * 4. When msdp_reg timer is started or stopped
556 void pim_msdp_sa_local_update(struct pim_upstream
*up
)
558 struct pim_instance
*pim
= up
->channel_oil
->pim
;
560 if (pim_msdp_sa_local_add_ok(up
)) {
561 pim_msdp_sa_local_add(pim
, &up
->sg
);
563 pim_msdp_sa_local_del(pim
, &up
->sg
);
567 static void pim_msdp_sa_local_setup(struct pim_instance
*pim
)
569 struct pim_upstream
*up
;
570 struct listnode
*up_node
;
572 for (ALL_LIST_ELEMENTS_RO(pim
->upstream_list
, up_node
, up
)) {
573 pim_msdp_sa_local_update(up
);
577 /* whenever the RP changes we need to re-evaluate the "local" SA-cache */
578 /* XXX: needs to be tested */
579 void pim_msdp_i_am_rp_changed(struct pim_instance
*pim
)
581 struct listnode
*sanode
;
582 struct listnode
*nextnode
;
583 struct pim_msdp_sa
*sa
;
585 if (!(pim
->msdp
.flags
& PIM_MSDPF_ENABLE
)) {
586 /* if the feature is not enabled do nothing */
590 if (PIM_DEBUG_MSDP_INTERNAL
) {
591 zlog_debug("MSDP i_am_rp changed");
594 /* mark all local entries as stale */
595 for (ALL_LIST_ELEMENTS_RO(pim
->msdp
.sa_list
, sanode
, sa
)) {
596 if (sa
->flags
& PIM_MSDP_SAF_LOCAL
) {
597 sa
->flags
|= PIM_MSDP_SAF_STALE
;
601 /* re-setup local SA entries */
602 pim_msdp_sa_local_setup(pim
);
604 for (ALL_LIST_ELEMENTS(pim
->msdp
.sa_list
, sanode
, nextnode
, sa
)) {
605 /* purge stale SA entries */
606 if (sa
->flags
& PIM_MSDP_SAF_STALE
) {
607 /* clear the stale flag; the entry may be kept even
610 sa
->flags
&= ~PIM_MSDP_SAF_STALE
;
611 /* sa_deref can end up freeing the sa; so don't access
613 pim_msdp_sa_deref(sa
, PIM_MSDP_SAF_LOCAL
);
615 /* if the souce is still active check if we can
617 pim_msdp_sa_upstream_update(sa
, NULL
/* xg_up */,
623 /* We track the join state of (*, G) entries. If G has sources in the SA-cache
624 * we need to setup or teardown SPT when the JoinDesired status changes for
626 void pim_msdp_up_join_state_changed(struct pim_instance
*pim
,
627 struct pim_upstream
*xg_up
)
629 struct listnode
*sanode
;
630 struct pim_msdp_sa
*sa
;
632 if (PIM_DEBUG_MSDP_INTERNAL
) {
633 zlog_debug("MSDP join state changed for %s", xg_up
->sg_str
);
636 /* If this is not really an XG entry just move on */
637 if ((xg_up
->sg
.src
.s_addr
!= INADDR_ANY
)
638 || (xg_up
->sg
.grp
.s_addr
== INADDR_ANY
)) {
642 /* XXX: Need to maintain SAs per-group to avoid all this unnecessary
644 for (ALL_LIST_ELEMENTS_RO(pim
->msdp
.sa_list
, sanode
, sa
)) {
645 if (sa
->sg
.grp
.s_addr
!= xg_up
->sg
.grp
.s_addr
) {
648 pim_msdp_sa_upstream_update(sa
, xg_up
, "up-jp-change");
652 static void pim_msdp_up_xg_del(struct pim_instance
*pim
, struct prefix_sg
*sg
)
654 struct listnode
*sanode
;
655 struct pim_msdp_sa
*sa
;
657 if (PIM_DEBUG_MSDP_INTERNAL
) {
658 zlog_debug("MSDP %s del", pim_str_sg_dump(sg
));
661 /* If this is not really an XG entry just move on */
662 if ((sg
->src
.s_addr
!= INADDR_ANY
) || (sg
->grp
.s_addr
== INADDR_ANY
)) {
666 /* XXX: Need to maintain SAs per-group to avoid all this unnecessary
668 for (ALL_LIST_ELEMENTS_RO(pim
->msdp
.sa_list
, sanode
, sa
)) {
669 if (sa
->sg
.grp
.s_addr
!= sg
->grp
.s_addr
) {
672 pim_msdp_sa_upstream_update(sa
, NULL
/* xg */, "up-jp-change");
676 void pim_msdp_up_del(struct pim_instance
*pim
, struct prefix_sg
*sg
)
678 if (PIM_DEBUG_MSDP_INTERNAL
) {
679 zlog_debug("MSDP up %s del", pim_str_sg_dump(sg
));
681 if (sg
->src
.s_addr
== INADDR_ANY
) {
682 pim_msdp_up_xg_del(pim
, sg
);
684 pim_msdp_sa_local_del_on_up_del(pim
, sg
);
688 /* sa hash and peer list helpers */
689 static unsigned int pim_msdp_sa_hash_key_make(void *p
)
691 struct pim_msdp_sa
*sa
= p
;
693 return (jhash_2words(sa
->sg
.src
.s_addr
, sa
->sg
.grp
.s_addr
, 0));
696 static int pim_msdp_sa_hash_eq(const void *p1
, const void *p2
)
698 const struct pim_msdp_sa
*sa1
= p1
;
699 const struct pim_msdp_sa
*sa2
= p2
;
701 return ((sa1
->sg
.src
.s_addr
== sa2
->sg
.src
.s_addr
)
702 && (sa1
->sg
.grp
.s_addr
== sa2
->sg
.grp
.s_addr
));
705 static int pim_msdp_sa_comp(const void *p1
, const void *p2
)
707 const struct pim_msdp_sa
*sa1
= p1
;
708 const struct pim_msdp_sa
*sa2
= p2
;
710 if (ntohl(sa1
->sg
.grp
.s_addr
) < ntohl(sa2
->sg
.grp
.s_addr
))
713 if (ntohl(sa1
->sg
.grp
.s_addr
) > ntohl(sa2
->sg
.grp
.s_addr
))
716 if (ntohl(sa1
->sg
.src
.s_addr
) < ntohl(sa2
->sg
.src
.s_addr
))
719 if (ntohl(sa1
->sg
.src
.s_addr
) > ntohl(sa2
->sg
.src
.s_addr
))
725 /* RFC-3618:Sec-10.1.3 - Peer-RPF forwarding */
726 /* XXX: this can use a bit of refining and extensions */
727 bool pim_msdp_peer_rpf_check(struct pim_msdp_peer
*mp
, struct in_addr rp
)
729 if (mp
->peer
.s_addr
== rp
.s_addr
) {
736 /************************ Peer session management **************************/
737 char *pim_msdp_state_dump(enum pim_msdp_peer_state state
, char *buf
,
741 case PIM_MSDP_DISABLED
:
742 snprintf(buf
, buf_size
, "%s", "disabled");
744 case PIM_MSDP_INACTIVE
:
745 snprintf(buf
, buf_size
, "%s", "inactive");
747 case PIM_MSDP_LISTEN
:
748 snprintf(buf
, buf_size
, "%s", "listen");
750 case PIM_MSDP_CONNECTING
:
751 snprintf(buf
, buf_size
, "%s", "connecting");
753 case PIM_MSDP_ESTABLISHED
:
754 snprintf(buf
, buf_size
, "%s", "established");
757 snprintf(buf
, buf_size
, "unk-%d", state
);
762 char *pim_msdp_peer_key_dump(struct pim_msdp_peer
*mp
, char *buf
, int buf_size
,
765 char peer_str
[INET_ADDRSTRLEN
];
766 char local_str
[INET_ADDRSTRLEN
];
768 pim_inet4_dump("<peer?>", mp
->peer
, peer_str
, sizeof(peer_str
));
770 pim_inet4_dump("<local?>", mp
->local
, local_str
,
772 snprintf(buf
, buf_size
, "MSDP peer %s local %s mg %s", peer_str
,
773 local_str
, mp
->mesh_group_name
);
775 snprintf(buf
, buf_size
, "MSDP peer %s", peer_str
);
781 static void pim_msdp_peer_state_chg_log(struct pim_msdp_peer
*mp
)
783 char state_str
[PIM_MSDP_STATE_STRLEN
];
785 pim_msdp_state_dump(mp
->state
, state_str
, sizeof(state_str
));
786 zlog_debug("MSDP peer %s state chg to %s", mp
->key_str
, state_str
);
789 /* MSDP Connection State Machine actions (defined in RFC-3618:Sec-11.2) */
790 /* 11.2.A2: active peer - start connect retry timer; when the timer fires
791 * a tcp connection will be made */
792 static void pim_msdp_peer_connect(struct pim_msdp_peer
*mp
)
794 mp
->state
= PIM_MSDP_CONNECTING
;
795 if (PIM_DEBUG_MSDP_EVENTS
) {
796 pim_msdp_peer_state_chg_log(mp
);
799 pim_msdp_peer_cr_timer_setup(mp
, true /* start */);
802 /* 11.2.A3: passive peer - just listen for connections */
803 static void pim_msdp_peer_listen(struct pim_msdp_peer
*mp
)
805 mp
->state
= PIM_MSDP_LISTEN
;
806 if (PIM_DEBUG_MSDP_EVENTS
) {
807 pim_msdp_peer_state_chg_log(mp
);
810 /* this is interntionally asymmetric i.e. we set up listen-socket when
812 * first listening peer is configured; but don't bother tearing it down
814 * all the peers go down */
815 pim_msdp_sock_listen(mp
->pim
);
818 /* 11.2.A4 and 11.2.A5: transition active or passive peer to
819 * established state */
820 void pim_msdp_peer_established(struct pim_msdp_peer
*mp
)
822 if (mp
->state
!= PIM_MSDP_ESTABLISHED
) {
826 mp
->state
= PIM_MSDP_ESTABLISHED
;
827 mp
->uptime
= pim_time_monotonic_sec();
829 if (PIM_DEBUG_MSDP_EVENTS
) {
830 pim_msdp_peer_state_chg_log(mp
);
833 /* stop retry timer on active peers */
834 pim_msdp_peer_cr_timer_setup(mp
, false /* start */);
836 /* send KA; start KA and hold timers */
837 pim_msdp_pkt_ka_tx(mp
);
838 pim_msdp_peer_ka_timer_setup(mp
, true /* start */);
839 pim_msdp_peer_hold_timer_setup(mp
, true /* start */);
841 pim_msdp_pkt_sa_tx_to_one_peer(mp
);
843 PIM_MSDP_PEER_WRITE_ON(mp
);
844 PIM_MSDP_PEER_READ_ON(mp
);
847 /* 11.2.A6, 11.2.A7 and 11.2.A8: shutdown the peer tcp connection */
848 void pim_msdp_peer_stop_tcp_conn(struct pim_msdp_peer
*mp
, bool chg_state
)
851 if (mp
->state
== PIM_MSDP_ESTABLISHED
) {
854 mp
->state
= PIM_MSDP_INACTIVE
;
855 if (PIM_DEBUG_MSDP_EVENTS
) {
856 pim_msdp_peer_state_chg_log(mp
);
860 if (PIM_DEBUG_MSDP_INTERNAL
) {
861 zlog_debug("MSDP peer %s pim_msdp_peer_stop_tcp_conn",
864 /* stop read and write threads */
865 PIM_MSDP_PEER_READ_OFF(mp
);
866 PIM_MSDP_PEER_WRITE_OFF(mp
);
871 stream_reset(mp
->ibuf
);
873 stream_fifo_clean(mp
->obuf
);
875 /* stop all peer timers */
876 pim_msdp_peer_ka_timer_setup(mp
, false /* start */);
877 pim_msdp_peer_cr_timer_setup(mp
, false /* start */);
878 pim_msdp_peer_hold_timer_setup(mp
, false /* start */);
880 /* close connection */
887 /* RFC-3618:Sec-5.6 - stop the peer tcp connection and startover */
888 void pim_msdp_peer_reset_tcp_conn(struct pim_msdp_peer
*mp
, const char *rc_str
)
890 if (PIM_DEBUG_EVENTS
) {
891 zlog_debug("MSDP peer %s tcp reset %s", mp
->key_str
, rc_str
);
892 snprintf(mp
->last_reset
, sizeof(mp
->last_reset
), "%s", rc_str
);
895 /* close the connection and transition to listening or connecting */
896 pim_msdp_peer_stop_tcp_conn(mp
, true /* chg_state */);
897 if (PIM_MSDP_PEER_IS_LISTENER(mp
)) {
898 pim_msdp_peer_listen(mp
);
900 pim_msdp_peer_connect(mp
);
904 static void pim_msdp_peer_timer_expiry_log(struct pim_msdp_peer
*mp
,
905 const char *timer_str
)
907 zlog_debug("MSDP peer %s %s timer expired", mp
->key_str
, timer_str
);
910 /* RFC-3618:Sec-5.4 - peer hold timer */
911 static int pim_msdp_peer_hold_timer_cb(struct thread
*t
)
913 struct pim_msdp_peer
*mp
;
917 if (PIM_DEBUG_MSDP_EVENTS
) {
918 pim_msdp_peer_timer_expiry_log(mp
, "hold");
921 if (mp
->state
!= PIM_MSDP_ESTABLISHED
) {
925 if (PIM_DEBUG_MSDP_EVENTS
) {
926 pim_msdp_peer_state_chg_log(mp
);
928 pim_msdp_peer_reset_tcp_conn(mp
, "ht-expired");
932 static void pim_msdp_peer_hold_timer_setup(struct pim_msdp_peer
*mp
, bool start
)
934 struct pim_instance
*pim
= mp
->pim
;
935 THREAD_OFF(mp
->hold_timer
);
937 thread_add_timer(pim
->msdp
.master
, pim_msdp_peer_hold_timer_cb
,
938 mp
, PIM_MSDP_PEER_HOLD_TIME
, &mp
->hold_timer
);
943 /* RFC-3618:Sec-5.5 - peer keepalive timer */
944 static int pim_msdp_peer_ka_timer_cb(struct thread
*t
)
946 struct pim_msdp_peer
*mp
;
950 if (PIM_DEBUG_MSDP_EVENTS
) {
951 pim_msdp_peer_timer_expiry_log(mp
, "ka");
954 pim_msdp_pkt_ka_tx(mp
);
955 pim_msdp_peer_ka_timer_setup(mp
, true /* start */);
958 static void pim_msdp_peer_ka_timer_setup(struct pim_msdp_peer
*mp
, bool start
)
960 THREAD_OFF(mp
->ka_timer
);
962 thread_add_timer(mp
->pim
->msdp
.master
,
963 pim_msdp_peer_ka_timer_cb
, mp
,
964 PIM_MSDP_PEER_KA_TIME
, &mp
->ka_timer
);
968 static void pim_msdp_peer_active_connect(struct pim_msdp_peer
*mp
)
972 rc
= pim_msdp_sock_connect(mp
);
974 if (PIM_DEBUG_MSDP_INTERNAL
) {
975 zlog_debug("MSDP peer %s pim_msdp_peer_active_connect: %d",
982 /* connect failed restart the connect-retry timer */
983 pim_msdp_peer_cr_timer_setup(mp
, true /* start */);
986 case connect_success
:
987 /* connect was sucessful move to established */
988 pim_msdp_peer_established(mp
);
991 case connect_in_progress
:
992 /* for NB content we need to wait till sock is readable or
994 PIM_MSDP_PEER_WRITE_ON(mp
);
995 PIM_MSDP_PEER_READ_ON(mp
);
996 /* also restart connect-retry timer to reset the socket if
999 pim_msdp_peer_cr_timer_setup(mp
, true /* start */);
1004 /* RFC-3618:Sec-5.6 - connection retry on active peer */
1005 static int pim_msdp_peer_cr_timer_cb(struct thread
*t
)
1007 struct pim_msdp_peer
*mp
;
1011 if (PIM_DEBUG_MSDP_EVENTS
) {
1012 pim_msdp_peer_timer_expiry_log(mp
, "connect-retry");
1015 if (mp
->state
!= PIM_MSDP_CONNECTING
|| PIM_MSDP_PEER_IS_LISTENER(mp
)) {
1019 pim_msdp_peer_active_connect(mp
);
1022 static void pim_msdp_peer_cr_timer_setup(struct pim_msdp_peer
*mp
, bool start
)
1024 THREAD_OFF(mp
->cr_timer
);
1027 mp
->pim
->msdp
.master
, pim_msdp_peer_cr_timer_cb
, mp
,
1028 PIM_MSDP_PEER_CONNECT_RETRY_TIME
, &mp
->cr_timer
);
1032 /* if a valid packet is rxed from the peer we can restart hold timer */
1033 void pim_msdp_peer_pkt_rxed(struct pim_msdp_peer
*mp
)
1035 if (mp
->state
== PIM_MSDP_ESTABLISHED
) {
1036 pim_msdp_peer_hold_timer_setup(mp
, true /* start */);
1040 /* if a valid packet is txed to the peer we can restart ka timer and avoid
1041 * unnecessary ka noise in the network */
1042 void pim_msdp_peer_pkt_txed(struct pim_msdp_peer
*mp
)
1044 if (mp
->state
== PIM_MSDP_ESTABLISHED
) {
1045 pim_msdp_peer_ka_timer_setup(mp
, true /* start */);
1046 if (PIM_DEBUG_MSDP_INTERNAL
) {
1047 zlog_debug("MSDP ka timer restart on pkt tx to %s",
1053 static void pim_msdp_addr2su(union sockunion
*su
, struct in_addr addr
)
1056 su
->sin
.sin_addr
= addr
;
1057 su
->sin
.sin_family
= AF_INET
;
1058 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1059 su
->sin
.sin_len
= sizeof(struct sockaddr_in
);
1060 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1063 /* 11.2.A1: create a new peer and transition state to listen or connecting */
1064 static enum pim_msdp_err
pim_msdp_peer_new(struct pim_instance
*pim
,
1065 struct in_addr peer_addr
,
1066 struct in_addr local_addr
,
1067 const char *mesh_group_name
,
1068 struct pim_msdp_peer
**mp_p
)
1070 struct pim_msdp_peer
*mp
;
1072 pim_msdp_enable(pim
);
1074 mp
= XCALLOC(MTYPE_PIM_MSDP_PEER
, sizeof(*mp
));
1076 zlog_err("%s: PIM XCALLOC(%zu) failure", __PRETTY_FUNCTION__
,
1078 return PIM_MSDP_ERR_OOM
;
1082 mp
->peer
= peer_addr
;
1083 pim_inet4_dump("<peer?>", mp
->peer
, mp
->key_str
, sizeof(mp
->key_str
));
1084 pim_msdp_addr2su(&mp
->su_peer
, mp
->peer
);
1085 mp
->local
= local_addr
;
1086 /* XXX: originator_id setting needs to move to the mesh group */
1087 pim
->msdp
.originator_id
= local_addr
;
1088 pim_msdp_addr2su(&mp
->su_local
, mp
->local
);
1089 mp
->mesh_group_name
= XSTRDUP(MTYPE_PIM_MSDP_MG_NAME
, mesh_group_name
);
1090 mp
->state
= PIM_MSDP_INACTIVE
;
1092 strcpy(mp
->last_reset
, "-");
1093 /* higher IP address is listener */
1094 if (ntohl(mp
->local
.s_addr
) > ntohl(mp
->peer
.s_addr
)) {
1095 mp
->flags
|= PIM_MSDP_PEERF_LISTENER
;
1098 /* setup packet buffers */
1099 mp
->ibuf
= stream_new(PIM_MSDP_MAX_PACKET_SIZE
);
1100 mp
->obuf
= stream_fifo_new();
1102 /* insert into misc tables for easy access */
1103 mp
= hash_get(pim
->msdp
.peer_hash
, mp
, hash_alloc_intern
);
1104 listnode_add_sort(pim
->msdp
.peer_list
, mp
);
1106 if (PIM_DEBUG_MSDP_EVENTS
) {
1107 zlog_debug("MSDP peer %s created", mp
->key_str
);
1109 pim_msdp_peer_state_chg_log(mp
);
1112 /* fireup the connect state machine */
1113 if (PIM_MSDP_PEER_IS_LISTENER(mp
)) {
1114 pim_msdp_peer_listen(mp
);
1116 pim_msdp_peer_connect(mp
);
1121 return PIM_MSDP_ERR_NONE
;
1124 struct pim_msdp_peer
*pim_msdp_peer_find(struct pim_instance
*pim
,
1125 struct in_addr peer_addr
)
1127 struct pim_msdp_peer lookup
;
1129 lookup
.peer
= peer_addr
;
1130 return hash_lookup(pim
->msdp
.peer_hash
, &lookup
);
1133 /* add peer configuration if it doesn't already exist */
1134 enum pim_msdp_err
pim_msdp_peer_add(struct pim_instance
*pim
,
1135 struct in_addr peer_addr
,
1136 struct in_addr local_addr
,
1137 const char *mesh_group_name
,
1138 struct pim_msdp_peer
**mp_p
)
1140 struct pim_msdp_peer
*mp
;
1146 if (peer_addr
.s_addr
== local_addr
.s_addr
) {
1147 /* skip session setup if config is invalid */
1148 if (PIM_DEBUG_MSDP_EVENTS
) {
1149 char peer_str
[INET_ADDRSTRLEN
];
1151 pim_inet4_dump("<peer?>", peer_addr
, peer_str
,
1153 zlog_debug("%s add skipped as DIP=SIP", peer_str
);
1155 return PIM_MSDP_ERR_SIP_EQ_DIP
;
1158 mp
= pim_msdp_peer_find(pim
, peer_addr
);
1163 return PIM_MSDP_ERR_PEER_EXISTS
;
1166 return pim_msdp_peer_new(pim
, peer_addr
, local_addr
, mesh_group_name
,
1170 /* release all mem associated with a peer */
1171 static void pim_msdp_peer_free(struct pim_msdp_peer
*mp
)
1174 stream_free(mp
->ibuf
);
1178 stream_fifo_free(mp
->obuf
);
1181 if (mp
->mesh_group_name
) {
1182 XFREE(MTYPE_PIM_MSDP_MG_NAME
, mp
->mesh_group_name
);
1184 XFREE(MTYPE_PIM_MSDP_PEER
, mp
);
1187 /* delete the peer config */
1188 static enum pim_msdp_err
pim_msdp_peer_do_del(struct pim_msdp_peer
*mp
)
1190 /* stop the tcp connection and shutdown all timers */
1191 pim_msdp_peer_stop_tcp_conn(mp
, true /* chg_state */);
1193 /* remove the session from various tables */
1194 listnode_delete(mp
->pim
->msdp
.peer_list
, mp
);
1195 hash_release(mp
->pim
->msdp
.peer_hash
, mp
);
1197 if (PIM_DEBUG_MSDP_EVENTS
) {
1198 zlog_debug("MSDP peer %s deleted", mp
->key_str
);
1201 /* free up any associated memory */
1202 pim_msdp_peer_free(mp
);
1204 return PIM_MSDP_ERR_NONE
;
1207 enum pim_msdp_err
pim_msdp_peer_del(struct pim_instance
*pim
,
1208 struct in_addr peer_addr
)
1210 struct pim_msdp_peer
*mp
;
1212 mp
= pim_msdp_peer_find(pim
, peer_addr
);
1214 return PIM_MSDP_ERR_NO_PEER
;
1217 return pim_msdp_peer_do_del(mp
);
1220 /* peer hash and peer list helpers */
1221 static unsigned int pim_msdp_peer_hash_key_make(void *p
)
1223 struct pim_msdp_peer
*mp
= p
;
1224 return (jhash_1word(mp
->peer
.s_addr
, 0));
1227 static int pim_msdp_peer_hash_eq(const void *p1
, const void *p2
)
1229 const struct pim_msdp_peer
*mp1
= p1
;
1230 const struct pim_msdp_peer
*mp2
= p2
;
1232 return (mp1
->peer
.s_addr
== mp2
->peer
.s_addr
);
1235 static int pim_msdp_peer_comp(const void *p1
, const void *p2
)
1237 const struct pim_msdp_peer
*mp1
= p1
;
1238 const struct pim_msdp_peer
*mp2
= p2
;
1240 if (ntohl(mp1
->peer
.s_addr
) < ntohl(mp2
->peer
.s_addr
))
1243 if (ntohl(mp1
->peer
.s_addr
) > ntohl(mp2
->peer
.s_addr
))
1249 /************************** Mesh group management **************************/
1250 static void pim_msdp_mg_free(struct pim_instance
*pim
, struct pim_msdp_mg
*mg
)
1252 /* If the mesh-group has valid member or src_ip don't delete it */
1253 if (!mg
|| mg
->mbr_cnt
|| (mg
->src_ip
.s_addr
!= INADDR_ANY
)) {
1257 if (PIM_DEBUG_MSDP_EVENTS
) {
1258 zlog_debug("MSDP mesh-group %s deleted", mg
->mesh_group_name
);
1260 if (mg
->mesh_group_name
)
1261 XFREE(MTYPE_PIM_MSDP_MG_NAME
, mg
->mesh_group_name
);
1264 list_delete(mg
->mbr_list
);
1266 XFREE(MTYPE_PIM_MSDP_MG
, mg
);
1267 pim
->msdp
.mg
= NULL
;
1270 static struct pim_msdp_mg
*pim_msdp_mg_new(const char *mesh_group_name
)
1272 struct pim_msdp_mg
*mg
;
1274 mg
= XCALLOC(MTYPE_PIM_MSDP_MG
, sizeof(*mg
));
1276 zlog_err("%s: PIM XCALLOC(%zu) failure", __PRETTY_FUNCTION__
,
1281 mg
->mesh_group_name
= XSTRDUP(MTYPE_PIM_MSDP_MG_NAME
, mesh_group_name
);
1282 mg
->mbr_list
= list_new();
1283 mg
->mbr_list
->del
= (void (*)(void *))pim_msdp_mg_mbr_free
;
1284 mg
->mbr_list
->cmp
= (int (*)(void *, void *))pim_msdp_mg_mbr_comp
;
1286 if (PIM_DEBUG_MSDP_EVENTS
) {
1287 zlog_debug("MSDP mesh-group %s created", mg
->mesh_group_name
);
1292 enum pim_msdp_err
pim_msdp_mg_del(struct pim_instance
*pim
,
1293 const char *mesh_group_name
)
1295 struct pim_msdp_mg
*mg
= pim
->msdp
.mg
;
1296 struct pim_msdp_mg_mbr
*mbr
;
1298 if (!mg
|| strcmp(mg
->mesh_group_name
, mesh_group_name
)) {
1299 return PIM_MSDP_ERR_NO_MG
;
1302 /* delete all the mesh-group members */
1303 while (!list_isempty(mg
->mbr_list
)) {
1304 mbr
= listnode_head(mg
->mbr_list
);
1305 pim_msdp_mg_mbr_do_del(mg
, mbr
);
1309 mg
->src_ip
.s_addr
= INADDR_ANY
;
1311 /* free up the mesh-group */
1312 pim_msdp_mg_free(pim
, mg
);
1313 return PIM_MSDP_ERR_NONE
;
1316 static enum pim_msdp_err
pim_msdp_mg_add(struct pim_instance
*pim
,
1317 const char *mesh_group_name
)
1320 if (!strcmp(pim
->msdp
.mg
->mesh_group_name
, mesh_group_name
)) {
1321 return PIM_MSDP_ERR_NONE
;
1323 /* currently only one mesh-group can exist at a time */
1324 return PIM_MSDP_ERR_MAX_MESH_GROUPS
;
1327 pim
->msdp
.mg
= pim_msdp_mg_new(mesh_group_name
);
1328 if (!pim
->msdp
.mg
) {
1329 return PIM_MSDP_ERR_OOM
;
1332 return PIM_MSDP_ERR_NONE
;
1335 static int pim_msdp_mg_mbr_comp(const void *p1
, const void *p2
)
1337 const struct pim_msdp_mg_mbr
*mbr1
= p1
;
1338 const struct pim_msdp_mg_mbr
*mbr2
= p2
;
1340 if (ntohl(mbr1
->mbr_ip
.s_addr
) < ntohl(mbr2
->mbr_ip
.s_addr
))
1343 if (ntohl(mbr1
->mbr_ip
.s_addr
) > ntohl(mbr2
->mbr_ip
.s_addr
))
1349 static void pim_msdp_mg_mbr_free(struct pim_msdp_mg_mbr
*mbr
)
1351 XFREE(MTYPE_PIM_MSDP_MG_MBR
, mbr
);
1354 static struct pim_msdp_mg_mbr
*pim_msdp_mg_mbr_find(struct pim_instance
*pim
,
1355 struct in_addr mbr_ip
)
1357 struct pim_msdp_mg_mbr
*mbr
;
1358 struct listnode
*mbr_node
;
1360 if (!pim
->msdp
.mg
) {
1363 /* we can move this to a hash but considering that number of peers in
1364 * a mesh-group that seems like bit of an overkill */
1365 for (ALL_LIST_ELEMENTS_RO(pim
->msdp
.mg
->mbr_list
, mbr_node
, mbr
)) {
1366 if (mbr
->mbr_ip
.s_addr
== mbr_ip
.s_addr
) {
1373 enum pim_msdp_err
pim_msdp_mg_mbr_add(struct pim_instance
*pim
,
1374 const char *mesh_group_name
,
1375 struct in_addr mbr_ip
)
1378 struct pim_msdp_mg_mbr
*mbr
;
1379 struct pim_msdp_mg
*mg
;
1381 rc
= pim_msdp_mg_add(pim
, mesh_group_name
);
1382 if (rc
!= PIM_MSDP_ERR_NONE
) {
1387 mbr
= pim_msdp_mg_mbr_find(pim
, mbr_ip
);
1389 return PIM_MSDP_ERR_MG_MBR_EXISTS
;
1392 mbr
= XCALLOC(MTYPE_PIM_MSDP_MG_MBR
, sizeof(*mbr
));
1394 zlog_err("%s: PIM XCALLOC(%zu) failure", __PRETTY_FUNCTION__
,
1396 /* if there are no references to the mg free it */
1397 pim_msdp_mg_free(pim
, mg
);
1398 return PIM_MSDP_ERR_OOM
;
1400 mbr
->mbr_ip
= mbr_ip
;
1401 listnode_add_sort(mg
->mbr_list
, mbr
);
1403 /* if valid SIP has been configured add peer session */
1404 if (mg
->src_ip
.s_addr
!= INADDR_ANY
) {
1405 pim_msdp_peer_add(pim
, mbr_ip
, mg
->src_ip
, mesh_group_name
,
1409 if (PIM_DEBUG_MSDP_EVENTS
) {
1410 char ip_str
[INET_ADDRSTRLEN
];
1411 pim_inet4_dump("<mbr?>", mbr
->mbr_ip
, ip_str
, sizeof(ip_str
));
1412 zlog_debug("MSDP mesh-group %s mbr %s created",
1413 mg
->mesh_group_name
, ip_str
);
1416 return PIM_MSDP_ERR_NONE
;
1419 static void pim_msdp_mg_mbr_do_del(struct pim_msdp_mg
*mg
,
1420 struct pim_msdp_mg_mbr
*mbr
)
1422 /* Delete active peer session if any */
1424 pim_msdp_peer_do_del(mbr
->mp
);
1427 listnode_delete(mg
->mbr_list
, mbr
);
1428 if (PIM_DEBUG_MSDP_EVENTS
) {
1429 char ip_str
[INET_ADDRSTRLEN
];
1430 pim_inet4_dump("<mbr?>", mbr
->mbr_ip
, ip_str
, sizeof(ip_str
));
1431 zlog_debug("MSDP mesh-group %s mbr %s deleted",
1432 mg
->mesh_group_name
, ip_str
);
1434 pim_msdp_mg_mbr_free(mbr
);
1440 enum pim_msdp_err
pim_msdp_mg_mbr_del(struct pim_instance
*pim
,
1441 const char *mesh_group_name
,
1442 struct in_addr mbr_ip
)
1444 struct pim_msdp_mg_mbr
*mbr
;
1445 struct pim_msdp_mg
*mg
= pim
->msdp
.mg
;
1447 if (!mg
|| strcmp(mg
->mesh_group_name
, mesh_group_name
)) {
1448 return PIM_MSDP_ERR_NO_MG
;
1451 mbr
= pim_msdp_mg_mbr_find(pim
, mbr_ip
);
1453 return PIM_MSDP_ERR_NO_MG_MBR
;
1456 pim_msdp_mg_mbr_do_del(mg
, mbr
);
1457 /* if there are no references to the mg free it */
1458 pim_msdp_mg_free(pim
, mg
);
1460 return PIM_MSDP_ERR_NONE
;
1463 static void pim_msdp_mg_src_do_del(struct pim_instance
*pim
)
1465 struct pim_msdp_mg_mbr
*mbr
;
1466 struct listnode
*mbr_node
;
1467 struct pim_msdp_mg
*mg
= pim
->msdp
.mg
;
1469 /* SIP is being removed - tear down all active peer sessions */
1470 for (ALL_LIST_ELEMENTS_RO(mg
->mbr_list
, mbr_node
, mbr
)) {
1472 pim_msdp_peer_do_del(mbr
->mp
);
1476 if (PIM_DEBUG_MSDP_EVENTS
) {
1477 zlog_debug("MSDP mesh-group %s src cleared",
1478 mg
->mesh_group_name
);
1482 enum pim_msdp_err
pim_msdp_mg_src_del(struct pim_instance
*pim
,
1483 const char *mesh_group_name
)
1485 struct pim_msdp_mg
*mg
= pim
->msdp
.mg
;
1487 if (!mg
|| strcmp(mg
->mesh_group_name
, mesh_group_name
)) {
1488 return PIM_MSDP_ERR_NO_MG
;
1491 if (mg
->src_ip
.s_addr
!= INADDR_ANY
) {
1492 mg
->src_ip
.s_addr
= INADDR_ANY
;
1493 pim_msdp_mg_src_do_del(pim
);
1494 /* if there are no references to the mg free it */
1495 pim_msdp_mg_free(pim
, mg
);
1497 return PIM_MSDP_ERR_NONE
;
1500 enum pim_msdp_err
pim_msdp_mg_src_add(struct pim_instance
*pim
,
1501 const char *mesh_group_name
,
1502 struct in_addr src_ip
)
1505 struct pim_msdp_mg_mbr
*mbr
;
1506 struct listnode
*mbr_node
;
1507 struct pim_msdp_mg
*mg
;
1509 if (src_ip
.s_addr
== INADDR_ANY
) {
1510 pim_msdp_mg_src_del(pim
, mesh_group_name
);
1511 return PIM_MSDP_ERR_NONE
;
1514 rc
= pim_msdp_mg_add(pim
, mesh_group_name
);
1515 if (rc
!= PIM_MSDP_ERR_NONE
) {
1520 if (mg
->src_ip
.s_addr
!= INADDR_ANY
) {
1521 pim_msdp_mg_src_do_del(pim
);
1523 mg
->src_ip
= src_ip
;
1525 for (ALL_LIST_ELEMENTS_RO(mg
->mbr_list
, mbr_node
, mbr
)) {
1526 pim_msdp_peer_add(pim
, mbr
->mbr_ip
, mg
->src_ip
, mesh_group_name
,
1530 if (PIM_DEBUG_MSDP_EVENTS
) {
1531 char ip_str
[INET_ADDRSTRLEN
];
1532 pim_inet4_dump("<src?>", mg
->src_ip
, ip_str
, sizeof(ip_str
));
1533 zlog_debug("MSDP mesh-group %s src %s set", mg
->mesh_group_name
,
1536 return PIM_MSDP_ERR_NONE
;
1539 /*********************** MSDP feature APIs *********************************/
1540 int pim_msdp_config_write_helper(struct pim_instance
*pim
, struct vty
*vty
,
1543 struct listnode
*mbrnode
;
1544 struct pim_msdp_mg_mbr
*mbr
;
1545 struct pim_msdp_mg
*mg
= pim
->msdp
.mg
;
1546 char mbr_str
[INET_ADDRSTRLEN
];
1547 char src_str
[INET_ADDRSTRLEN
];
1554 if (mg
->src_ip
.s_addr
!= INADDR_ANY
) {
1555 pim_inet4_dump("<src?>", mg
->src_ip
, src_str
, sizeof(src_str
));
1556 vty_out(vty
, "%sip msdp mesh-group %s source %s\n", spaces
,
1557 mg
->mesh_group_name
, src_str
);
1561 for (ALL_LIST_ELEMENTS_RO(mg
->mbr_list
, mbrnode
, mbr
)) {
1562 pim_inet4_dump("<mbr?>", mbr
->mbr_ip
, mbr_str
, sizeof(mbr_str
));
1563 vty_out(vty
, "%sip msdp mesh-group %s member %s\n", spaces
,
1564 mg
->mesh_group_name
, mbr_str
);
1570 int pim_msdp_config_write(struct vty
*vty
)
1572 return pim_msdp_config_write_helper(pimg
, vty
, "");
1575 /* Enable feature including active/periodic timers etc. on the first peer
1576 * config. Till then MSDP should just stay quiet. */
1577 static void pim_msdp_enable(struct pim_instance
*pim
)
1579 if (pim
->msdp
.flags
& PIM_MSDPF_ENABLE
) {
1580 /* feature is already enabled */
1583 pim
->msdp
.flags
|= PIM_MSDPF_ENABLE
;
1584 pim
->msdp
.work_obuf
= stream_new(PIM_MSDP_MAX_PACKET_SIZE
);
1585 pim_msdp_sa_adv_timer_setup(pim
, true /* start */);
1586 /* setup sa cache based on local sources */
1587 pim_msdp_sa_local_setup(pim
);
1591 void pim_msdp_init(struct pim_instance
*pim
, struct thread_master
*master
)
1593 pim
->msdp
.master
= master
;
1596 snprintf(hash_name
, 64, "PIM %s MSDP Peer Hash", pim
->vrf
->name
);
1597 pim
->msdp
.peer_hash
= hash_create(pim_msdp_peer_hash_key_make
,
1598 pim_msdp_peer_hash_eq
, hash_name
);
1599 pim
->msdp
.peer_list
= list_new();
1600 pim
->msdp
.peer_list
->del
= (void (*)(void *))pim_msdp_peer_free
;
1601 pim
->msdp
.peer_list
->cmp
= (int (*)(void *, void *))pim_msdp_peer_comp
;
1603 snprintf(hash_name
, 64, "PIM %s MSDP SA Hash", pim
->vrf
->name
);
1604 pim
->msdp
.sa_hash
= hash_create(pim_msdp_sa_hash_key_make
,
1605 pim_msdp_sa_hash_eq
, hash_name
);
1606 pim
->msdp
.sa_list
= list_new();
1607 pim
->msdp
.sa_list
->del
= (void (*)(void *))pim_msdp_sa_free
;
1608 pim
->msdp
.sa_list
->cmp
= (int (*)(void *, void *))pim_msdp_sa_comp
;
1611 /* counterpart to MSDP init; XXX: unused currently */
1612 void pim_msdp_exit(struct pim_instance
*pim
)
1614 /* XXX: stop listener and delete all peer sessions */
1616 if (pim
->msdp
.peer_hash
) {
1617 hash_free(pim
->msdp
.peer_hash
);
1618 pim
->msdp
.peer_hash
= NULL
;
1621 if (pim
->msdp
.peer_list
) {
1622 list_delete(pim
->msdp
.peer_list
);
1623 pim
->msdp
.peer_list
= NULL
;
1626 if (pim
->msdp
.sa_hash
) {
1627 hash_free(pim
->msdp
.sa_hash
);
1628 pim
->msdp
.sa_hash
= NULL
;
1631 if (pim
->msdp
.sa_list
) {
1632 list_delete(pim
->msdp
.sa_list
);
1633 pim
->msdp
.sa_list
= NULL
;