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
16 * along with this program; see the file COPYING; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
24 #include <lib/jhash.h>
26 #include <lib/prefix.h>
27 #include <lib/sockunion.h>
28 #include <lib/stream.h>
29 #include <lib/thread.h>
31 #include <lib/plist.h>
35 #include "pim_memory.h"
36 #include "pim_iface.h"
40 #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(void);
55 static void pim_msdp_sa_adv_timer_setup(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 msdp
->sa_adv_timer
= NULL
;
74 if (PIM_DEBUG_MSDP_EVENTS
) {
75 zlog_debug("MSDP SA advertisment timer expired");
78 pim_msdp_sa_adv_timer_setup(true /* start */);
82 static void pim_msdp_sa_adv_timer_setup(bool start
)
84 THREAD_OFF(msdp
->sa_adv_timer
);
86 THREAD_TIMER_ON(msdp
->master
, msdp
->sa_adv_timer
,
87 pim_msdp_sa_adv_timer_cb
, NULL
,
88 PIM_MSDP_SA_ADVERTISMENT_TIME
);
92 /* RFC-3618:Sec-5.3 - SA cache state timer */
93 static int pim_msdp_sa_state_timer_cb(struct thread
*t
)
95 struct pim_msdp_sa
*sa
;
98 sa
->sa_state_timer
= NULL
;
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_TIMER_ON(msdp
->master
, sa
->sa_state_timer
,
112 pim_msdp_sa_state_timer_cb
, sa
,
113 PIM_MSDP_SA_HOLD_TIME
);
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(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
->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(&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
->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
->sg
, NULL
/* iif */,
207 PIM_UPSTREAM_FLAG_MASK_SRC_MSDP
,
208 __PRETTY_FUNCTION__
);
212 /* update inherited oil */
213 pim_upstream_inherited_olist(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 prefix_sg
*sg
,
237 struct pim_msdp_sa
*sa
;
239 sa
= XCALLOC(MTYPE_PIM_MSDP_SA
, sizeof(*sa
));
241 zlog_err("%s: PIM XCALLOC(%zu) failure", __PRETTY_FUNCTION__
,
247 pim_str_sg_set(sg
, sa
->sg_str
);
249 sa
->uptime
= pim_time_monotonic_sec();
251 /* insert into misc tables for easy access */
252 sa
= hash_get(msdp
->sa_hash
, sa
, hash_alloc_intern
);
254 zlog_err("%s: PIM hash get failure", __PRETTY_FUNCTION__
);
255 pim_msdp_sa_free(sa
);
258 listnode_add_sort(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 prefix_sg
*sg
)
269 struct pim_msdp_sa lookup
;
272 return hash_lookup(msdp
->sa_hash
, &lookup
);
275 static struct pim_msdp_sa
*pim_msdp_sa_add(struct prefix_sg
*sg
,
278 struct pim_msdp_sa
*sa
;
280 sa
= pim_msdp_sa_find(sg
);
285 return pim_msdp_sa_new(sg
, rp
);
288 static void pim_msdp_sa_del(struct pim_msdp_sa
*sa
)
290 /* this is somewhat redundant - still want to be careful not to leave
291 * stale upstream references */
292 pim_msdp_sa_upstream_del(sa
);
295 pim_msdp_sa_state_timer_setup(sa
, false /* start */);
297 /* remove the entry from various tables */
298 listnode_delete(msdp
->sa_list
, sa
);
299 hash_release(msdp
->sa_hash
, sa
);
301 if (PIM_DEBUG_MSDP_EVENTS
) {
302 zlog_debug("MSDP SA %s deleted", sa
->sg_str
);
305 /* free up any associated memory */
306 pim_msdp_sa_free(sa
);
309 static void pim_msdp_sa_peer_ip_set(struct pim_msdp_sa
*sa
,
310 struct pim_msdp_peer
*mp
, struct in_addr rp
)
312 struct pim_msdp_peer
*old_mp
;
314 /* optimize the "no change" case as it will happen
315 * frequently/periodically */
316 if (mp
&& (sa
->peer
.s_addr
== mp
->peer
.s_addr
)) {
320 /* any time the peer ip changes also update the rp address */
321 if (PIM_INADDR_ISNOT_ANY(sa
->peer
)) {
322 old_mp
= pim_msdp_peer_find(sa
->peer
);
323 if (old_mp
&& old_mp
->sa_cnt
) {
332 sa
->peer
.s_addr
= PIM_NET_INADDR_ANY
;
337 /* When a local active-source is removed there is no way to withdraw the
338 * source from peers. We will simply remove it from the SA cache so it will
339 * not be sent in supsequent SA updates. Peers will consequently timeout the
341 * Similarly a "peer-added" SA is never explicitly deleted. It is simply
342 * aged out overtime if not seen in the SA updates from the peers.
343 * XXX: should we provide a knob to drop entries learnt from a peer when the
345 static void pim_msdp_sa_deref(struct pim_msdp_sa
*sa
,
346 enum pim_msdp_sa_flags flags
)
348 bool update_up
= false;
350 if ((sa
->flags
& PIM_MSDP_SAF_LOCAL
)) {
351 if (flags
& PIM_MSDP_SAF_LOCAL
) {
352 if (PIM_DEBUG_MSDP_EVENTS
) {
353 zlog_debug("MSDP SA %s local reference removed",
361 if ((sa
->flags
& PIM_MSDP_SAF_PEER
)) {
362 if (flags
& PIM_MSDP_SAF_PEER
) {
365 if (PIM_DEBUG_MSDP_EVENTS
) {
366 zlog_debug("MSDP SA %s peer reference removed",
369 pim_msdp_sa_state_timer_setup(sa
, false /* start */);
370 rp
.s_addr
= INADDR_ANY
;
371 pim_msdp_sa_peer_ip_set(sa
, NULL
/* mp */, rp
);
372 /* if peer ref was removed we need to remove the msdp
381 pim_msdp_sa_upstream_update(sa
, NULL
/* xg_up */, "sa-deref");
384 if (!(sa
->flags
& PIM_MSDP_SAF_REF
)) {
389 void pim_msdp_sa_ref(struct pim_msdp_peer
*mp
, struct prefix_sg
*sg
,
392 struct pim_msdp_sa
*sa
;
394 sa
= pim_msdp_sa_add(sg
, rp
);
401 if (!(sa
->flags
& PIM_MSDP_SAF_PEER
)) {
402 sa
->flags
|= PIM_MSDP_SAF_PEER
;
403 if (PIM_DEBUG_MSDP_EVENTS
) {
404 zlog_debug("MSDP SA %s added by peer",
408 pim_msdp_sa_peer_ip_set(sa
, mp
, rp
);
409 /* start/re-start the state timer to prevent cache expiry */
410 pim_msdp_sa_state_timer_setup(sa
, true /* start */);
411 /* We re-evaluate SA "SPT-trigger" everytime we hear abt it from
413 * peer. XXX: If this becomes too much of a periodic overhead we
414 * can make it event based */
415 pim_msdp_sa_upstream_update(sa
, NULL
/* xg_up */, "peer-ref");
417 if (!(sa
->flags
& PIM_MSDP_SAF_LOCAL
)) {
418 sa
->flags
|= PIM_MSDP_SAF_LOCAL
;
420 if (PIM_DEBUG_MSDP_EVENTS
) {
421 zlog_debug("MSDP SA %s added locally",
424 /* send an immediate SA update to peers */
425 pim_msdp_pkt_sa_tx_one(sa
);
427 sa
->flags
&= ~PIM_MSDP_SAF_STALE
;
431 /* The following criteria must be met to originate an SA from the MSDP
433 * 1. KAT must be running i.e. source is active.
434 * 2. We must be RP for the group.
435 * 3. Source must be registrable to the RP (this is where the RFC is vague
436 * and especially ambiguous in CLOS networks; with anycast RP all sources
437 * are potentially registrable to all RPs in the domain). We assume #3 is
439 * a. We are also the FHR-DR for the source (OR)
440 * b. We rxed a pim register (null or data encapsulated) within the last
441 * (3 * (1.5 * register_suppression_timer))).
443 static bool pim_msdp_sa_local_add_ok(struct pim_upstream
*up
)
445 if (!(msdp
->flags
& PIM_MSDPF_ENABLE
)) {
449 if (!up
->t_ka_timer
) {
450 /* stream is not active */
454 if (!I_am_RP(up
->sg
.grp
)) {
455 /* we are not RP for the group */
459 /* we are the FHR-DR for this stream or we are RP and have seen
461 * from a FHR for this source */
462 if (PIM_UPSTREAM_FLAG_TEST_FHR(up
->flags
) || up
->t_msdp_reg_timer
) {
469 static void pim_msdp_sa_local_add(struct prefix_sg
*sg
)
473 pim_msdp_sa_ref(NULL
/* mp */, sg
, rp
);
476 void pim_msdp_sa_local_del(struct prefix_sg
*sg
)
478 struct pim_msdp_sa
*sa
;
480 sa
= pim_msdp_sa_find(sg
);
482 pim_msdp_sa_deref(sa
, PIM_MSDP_SAF_LOCAL
);
486 /* we need to be very cautious with this API as SA del too can trigger an
487 * upstream del and we will get stuck in a simple loop */
488 static void pim_msdp_sa_local_del_on_up_del(struct prefix_sg
*sg
)
490 struct pim_msdp_sa
*sa
;
492 sa
= pim_msdp_sa_find(sg
);
494 if (PIM_DEBUG_MSDP_INTERNAL
) {
495 zlog_debug("MSDP local sa %s del on up del",
499 /* if there is no local reference escape */
500 if (!(sa
->flags
& PIM_MSDP_SAF_LOCAL
)) {
501 if (PIM_DEBUG_MSDP_INTERNAL
) {
502 zlog_debug("MSDP local sa %s del; no local ref",
508 if (sa
->flags
& PIM_MSDP_SAF_UP_DEL_IN_PROG
) {
509 /* MSDP is the one that triggered the upstream del. if
511 * we most certainly have a bug in the PIM upstream
513 * will not have a local reference unless the KAT is
514 * running. And if the
515 * KAT is running there MUST be an additional
516 * source-stream reference to
517 * the flow. Accounting for such cases requires lot of
519 * address this in the next release? - XXX */
521 "MSDP sa %s SPT teardown is causing the local entry to be removed",
526 /* we are dropping the sa on upstream del we should not have an
527 * upstream reference */
529 if (PIM_DEBUG_MSDP_INTERNAL
) {
530 zlog_debug("MSDP local sa %s del; up non-NULL",
535 pim_msdp_sa_deref(sa
, PIM_MSDP_SAF_LOCAL
);
539 /* Local SA qualification needs to be re-evaluated when -
540 * 1. KAT is started or stopped
542 * 3. Whenever FHR status changes for a (S,G) - XXX - currently there
543 * is no clear path to transition an entry out of "MASK_FHR" need
544 * to discuss this with Donald. May result in some strangeness if the
545 * FHR is also the RP.
546 * 4. When msdp_reg timer is started or stopped
548 void pim_msdp_sa_local_update(struct pim_upstream
*up
)
550 if (pim_msdp_sa_local_add_ok(up
)) {
551 pim_msdp_sa_local_add(&up
->sg
);
553 pim_msdp_sa_local_del(&up
->sg
);
557 static void pim_msdp_sa_local_setup(void)
559 struct pim_upstream
*up
;
560 struct listnode
*up_node
;
562 for (ALL_LIST_ELEMENTS_RO(pim_upstream_list
, up_node
, up
)) {
563 pim_msdp_sa_local_update(up
);
567 /* whenever the RP changes we need to re-evaluate the "local" SA-cache */
568 /* XXX: needs to be tested */
569 void pim_msdp_i_am_rp_changed(void)
571 struct listnode
*sanode
;
572 struct listnode
*nextnode
;
573 struct pim_msdp_sa
*sa
;
575 if (!(msdp
->flags
& PIM_MSDPF_ENABLE
)) {
576 /* if the feature is not enabled do nothing */
580 if (PIM_DEBUG_MSDP_INTERNAL
) {
581 zlog_debug("MSDP i_am_rp changed");
584 /* mark all local entries as stale */
585 for (ALL_LIST_ELEMENTS_RO(msdp
->sa_list
, sanode
, sa
)) {
586 if (sa
->flags
& PIM_MSDP_SAF_LOCAL
) {
587 sa
->flags
|= PIM_MSDP_SAF_STALE
;
591 /* re-setup local SA entries */
592 pim_msdp_sa_local_setup();
594 for (ALL_LIST_ELEMENTS(msdp
->sa_list
, sanode
, nextnode
, sa
)) {
595 /* purge stale SA entries */
596 if (sa
->flags
& PIM_MSDP_SAF_STALE
) {
597 /* clear the stale flag; the entry may be kept even
600 sa
->flags
&= ~PIM_MSDP_SAF_STALE
;
601 /* sa_deref can end up freeing the sa; so don't access
603 pim_msdp_sa_deref(sa
, PIM_MSDP_SAF_LOCAL
);
605 /* if the souce is still active check if we can
607 pim_msdp_sa_upstream_update(sa
, NULL
/* xg_up */,
613 /* We track the join state of (*, G) entries. If G has sources in the SA-cache
614 * we need to setup or teardown SPT when the JoinDesired status changes for
616 void pim_msdp_up_join_state_changed(struct pim_upstream
*xg_up
)
618 struct listnode
*sanode
;
619 struct pim_msdp_sa
*sa
;
621 if (PIM_DEBUG_MSDP_INTERNAL
) {
622 zlog_debug("MSDP join state changed for %s", xg_up
->sg_str
);
625 /* If this is not really an XG entry just move on */
626 if ((xg_up
->sg
.src
.s_addr
!= INADDR_ANY
)
627 || (xg_up
->sg
.grp
.s_addr
== INADDR_ANY
)) {
631 /* XXX: Need to maintain SAs per-group to avoid all this unnecessary
633 for (ALL_LIST_ELEMENTS_RO(msdp
->sa_list
, sanode
, sa
)) {
634 if (sa
->sg
.grp
.s_addr
!= xg_up
->sg
.grp
.s_addr
) {
637 pim_msdp_sa_upstream_update(sa
, xg_up
, "up-jp-change");
641 static void pim_msdp_up_xg_del(struct prefix_sg
*sg
)
643 struct listnode
*sanode
;
644 struct pim_msdp_sa
*sa
;
646 if (PIM_DEBUG_MSDP_INTERNAL
) {
647 zlog_debug("MSDP %s del", pim_str_sg_dump(sg
));
650 /* If this is not really an XG entry just move on */
651 if ((sg
->src
.s_addr
!= INADDR_ANY
) || (sg
->grp
.s_addr
== INADDR_ANY
)) {
655 /* XXX: Need to maintain SAs per-group to avoid all this unnecessary
657 for (ALL_LIST_ELEMENTS_RO(msdp
->sa_list
, sanode
, sa
)) {
658 if (sa
->sg
.grp
.s_addr
!= sg
->grp
.s_addr
) {
661 pim_msdp_sa_upstream_update(sa
, NULL
/* xg */, "up-jp-change");
665 void pim_msdp_up_del(struct prefix_sg
*sg
)
667 if (PIM_DEBUG_MSDP_INTERNAL
) {
668 zlog_debug("MSDP up %s del", pim_str_sg_dump(sg
));
670 if (sg
->src
.s_addr
== INADDR_ANY
) {
671 pim_msdp_up_xg_del(sg
);
673 pim_msdp_sa_local_del_on_up_del(sg
);
677 /* sa hash and peer list helpers */
678 static unsigned int pim_msdp_sa_hash_key_make(void *p
)
680 struct pim_msdp_sa
*sa
= p
;
682 return (jhash_2words(sa
->sg
.src
.s_addr
, sa
->sg
.grp
.s_addr
, 0));
685 static int pim_msdp_sa_hash_eq(const void *p1
, const void *p2
)
687 const struct pim_msdp_sa
*sa1
= p1
;
688 const struct pim_msdp_sa
*sa2
= p2
;
690 return ((sa1
->sg
.src
.s_addr
== sa2
->sg
.src
.s_addr
)
691 && (sa1
->sg
.grp
.s_addr
== sa2
->sg
.grp
.s_addr
));
694 static int pim_msdp_sa_comp(const void *p1
, const void *p2
)
696 const struct pim_msdp_sa
*sa1
= p1
;
697 const struct pim_msdp_sa
*sa2
= p2
;
699 if (ntohl(sa1
->sg
.grp
.s_addr
) < ntohl(sa2
->sg
.grp
.s_addr
))
702 if (ntohl(sa1
->sg
.grp
.s_addr
) > ntohl(sa2
->sg
.grp
.s_addr
))
705 if (ntohl(sa1
->sg
.src
.s_addr
) < ntohl(sa2
->sg
.src
.s_addr
))
708 if (ntohl(sa1
->sg
.src
.s_addr
) > ntohl(sa2
->sg
.src
.s_addr
))
714 /* RFC-3618:Sec-10.1.3 - Peer-RPF forwarding */
715 /* XXX: this can use a bit of refining and extensions */
716 bool pim_msdp_peer_rpf_check(struct pim_msdp_peer
*mp
, struct in_addr rp
)
718 if (mp
->peer
.s_addr
== rp
.s_addr
) {
725 /************************ Peer session management **************************/
726 char *pim_msdp_state_dump(enum pim_msdp_peer_state state
, char *buf
,
730 case PIM_MSDP_DISABLED
:
731 snprintf(buf
, buf_size
, "%s", "disabled");
733 case PIM_MSDP_INACTIVE
:
734 snprintf(buf
, buf_size
, "%s", "inactive");
736 case PIM_MSDP_LISTEN
:
737 snprintf(buf
, buf_size
, "%s", "listen");
739 case PIM_MSDP_CONNECTING
:
740 snprintf(buf
, buf_size
, "%s", "connecting");
742 case PIM_MSDP_ESTABLISHED
:
743 snprintf(buf
, buf_size
, "%s", "established");
746 snprintf(buf
, buf_size
, "unk-%d", state
);
751 char *pim_msdp_peer_key_dump(struct pim_msdp_peer
*mp
, char *buf
, int buf_size
,
754 char peer_str
[INET_ADDRSTRLEN
];
755 char local_str
[INET_ADDRSTRLEN
];
757 pim_inet4_dump("<peer?>", mp
->peer
, peer_str
, sizeof(peer_str
));
759 pim_inet4_dump("<local?>", mp
->local
, local_str
,
761 snprintf(buf
, buf_size
, "MSDP peer %s local %s mg %s", peer_str
,
762 local_str
, mp
->mesh_group_name
);
764 snprintf(buf
, buf_size
, "MSDP peer %s", peer_str
);
770 static void pim_msdp_peer_state_chg_log(struct pim_msdp_peer
*mp
)
772 char state_str
[PIM_MSDP_STATE_STRLEN
];
774 pim_msdp_state_dump(mp
->state
, state_str
, sizeof(state_str
));
775 zlog_debug("MSDP peer %s state chg to %s", mp
->key_str
, state_str
);
778 /* MSDP Connection State Machine actions (defined in RFC-3618:Sec-11.2) */
779 /* 11.2.A2: active peer - start connect retry timer; when the timer fires
780 * a tcp connection will be made */
781 static void pim_msdp_peer_connect(struct pim_msdp_peer
*mp
)
783 mp
->state
= PIM_MSDP_CONNECTING
;
784 if (PIM_DEBUG_MSDP_EVENTS
) {
785 pim_msdp_peer_state_chg_log(mp
);
788 pim_msdp_peer_cr_timer_setup(mp
, true /* start */);
791 /* 11.2.A3: passive peer - just listen for connections */
792 static void pim_msdp_peer_listen(struct pim_msdp_peer
*mp
)
794 mp
->state
= PIM_MSDP_LISTEN
;
795 if (PIM_DEBUG_MSDP_EVENTS
) {
796 pim_msdp_peer_state_chg_log(mp
);
799 /* this is interntionally asymmetric i.e. we set up listen-socket when
801 * first listening peer is configured; but don't bother tearing it down
803 * all the peers go down */
804 pim_msdp_sock_listen();
807 /* 11.2.A4 and 11.2.A5: transition active or passive peer to
808 * established state */
809 void pim_msdp_peer_established(struct pim_msdp_peer
*mp
)
811 if (mp
->state
!= PIM_MSDP_ESTABLISHED
) {
815 mp
->state
= PIM_MSDP_ESTABLISHED
;
816 mp
->uptime
= pim_time_monotonic_sec();
818 if (PIM_DEBUG_MSDP_EVENTS
) {
819 pim_msdp_peer_state_chg_log(mp
);
822 /* stop retry timer on active peers */
823 pim_msdp_peer_cr_timer_setup(mp
, false /* start */);
825 /* send KA; start KA and hold timers */
826 pim_msdp_pkt_ka_tx(mp
);
827 pim_msdp_peer_ka_timer_setup(mp
, true /* start */);
828 pim_msdp_peer_hold_timer_setup(mp
, true /* start */);
830 pim_msdp_pkt_sa_tx_to_one_peer(mp
);
832 PIM_MSDP_PEER_WRITE_ON(mp
);
833 PIM_MSDP_PEER_READ_ON(mp
);
836 /* 11.2.A6, 11.2.A7 and 11.2.A8: shutdown the peer tcp connection */
837 void pim_msdp_peer_stop_tcp_conn(struct pim_msdp_peer
*mp
, bool chg_state
)
840 if (mp
->state
== PIM_MSDP_ESTABLISHED
) {
843 mp
->state
= PIM_MSDP_INACTIVE
;
844 if (PIM_DEBUG_MSDP_EVENTS
) {
845 pim_msdp_peer_state_chg_log(mp
);
849 if (PIM_DEBUG_MSDP_INTERNAL
) {
850 zlog_debug("MSDP peer %s pim_msdp_peer_stop_tcp_conn",
853 /* stop read and write threads */
854 PIM_MSDP_PEER_READ_OFF(mp
);
855 PIM_MSDP_PEER_WRITE_OFF(mp
);
860 stream_reset(mp
->ibuf
);
862 stream_fifo_clean(mp
->obuf
);
864 /* stop all peer timers */
865 pim_msdp_peer_ka_timer_setup(mp
, false /* start */);
866 pim_msdp_peer_cr_timer_setup(mp
, false /* start */);
867 pim_msdp_peer_hold_timer_setup(mp
, false /* start */);
869 /* close connection */
876 /* RFC-3618:Sec-5.6 - stop the peer tcp connection and startover */
877 void pim_msdp_peer_reset_tcp_conn(struct pim_msdp_peer
*mp
, const char *rc_str
)
879 if (PIM_DEBUG_EVENTS
) {
880 zlog_debug("MSDP peer %s tcp reset %s", mp
->key_str
, rc_str
);
881 snprintf(mp
->last_reset
, sizeof(mp
->last_reset
), "%s", rc_str
);
884 /* close the connection and transition to listening or connecting */
885 pim_msdp_peer_stop_tcp_conn(mp
, true /* chg_state */);
886 if (PIM_MSDP_PEER_IS_LISTENER(mp
)) {
887 pim_msdp_peer_listen(mp
);
889 pim_msdp_peer_connect(mp
);
893 static void pim_msdp_peer_timer_expiry_log(struct pim_msdp_peer
*mp
,
894 const char *timer_str
)
896 zlog_debug("MSDP peer %s %s timer expired", mp
->key_str
, timer_str
);
899 /* RFC-3618:Sec-5.4 - peer hold timer */
900 static int pim_msdp_peer_hold_timer_cb(struct thread
*t
)
902 struct pim_msdp_peer
*mp
;
905 mp
->hold_timer
= NULL
;
907 if (PIM_DEBUG_MSDP_EVENTS
) {
908 pim_msdp_peer_timer_expiry_log(mp
, "hold");
911 if (mp
->state
!= PIM_MSDP_ESTABLISHED
) {
915 if (PIM_DEBUG_MSDP_EVENTS
) {
916 pim_msdp_peer_state_chg_log(mp
);
918 pim_msdp_peer_reset_tcp_conn(mp
, "ht-expired");
921 static void pim_msdp_peer_hold_timer_setup(struct pim_msdp_peer
*mp
, bool start
)
923 THREAD_OFF(mp
->hold_timer
);
925 THREAD_TIMER_ON(msdp
->master
, mp
->hold_timer
,
926 pim_msdp_peer_hold_timer_cb
, mp
,
927 PIM_MSDP_PEER_HOLD_TIME
);
932 /* RFC-3618:Sec-5.5 - peer keepalive timer */
933 static int pim_msdp_peer_ka_timer_cb(struct thread
*t
)
935 struct pim_msdp_peer
*mp
;
940 if (PIM_DEBUG_MSDP_EVENTS
) {
941 pim_msdp_peer_timer_expiry_log(mp
, "ka");
944 pim_msdp_pkt_ka_tx(mp
);
945 pim_msdp_peer_ka_timer_setup(mp
, true /* start */);
948 static void pim_msdp_peer_ka_timer_setup(struct pim_msdp_peer
*mp
, bool start
)
950 THREAD_OFF(mp
->ka_timer
);
952 THREAD_TIMER_ON(msdp
->master
, mp
->ka_timer
,
953 pim_msdp_peer_ka_timer_cb
, mp
,
954 PIM_MSDP_PEER_KA_TIME
);
958 static void pim_msdp_peer_active_connect(struct pim_msdp_peer
*mp
)
962 rc
= pim_msdp_sock_connect(mp
);
964 if (PIM_DEBUG_MSDP_INTERNAL
) {
965 zlog_debug("MSDP peer %s pim_msdp_peer_active_connect: %d",
972 /* connect failed restart the connect-retry timer */
973 pim_msdp_peer_cr_timer_setup(mp
, true /* start */);
976 case connect_success
:
977 /* connect was sucessful move to established */
978 pim_msdp_peer_established(mp
);
981 case connect_in_progress
:
982 /* for NB content we need to wait till sock is readable or
984 PIM_MSDP_PEER_WRITE_ON(mp
);
985 PIM_MSDP_PEER_READ_ON(mp
);
986 /* also restart connect-retry timer to reset the socket if
989 pim_msdp_peer_cr_timer_setup(mp
, true /* start */);
994 /* RFC-3618:Sec-5.6 - connection retry on active peer */
995 static int pim_msdp_peer_cr_timer_cb(struct thread
*t
)
997 struct pim_msdp_peer
*mp
;
1000 mp
->cr_timer
= NULL
;
1002 if (PIM_DEBUG_MSDP_EVENTS
) {
1003 pim_msdp_peer_timer_expiry_log(mp
, "connect-retry");
1006 if (mp
->state
!= PIM_MSDP_CONNECTING
|| PIM_MSDP_PEER_IS_LISTENER(mp
)) {
1010 pim_msdp_peer_active_connect(mp
);
1013 static void pim_msdp_peer_cr_timer_setup(struct pim_msdp_peer
*mp
, bool start
)
1015 THREAD_OFF(mp
->cr_timer
);
1017 THREAD_TIMER_ON(msdp
->master
, mp
->cr_timer
,
1018 pim_msdp_peer_cr_timer_cb
, mp
,
1019 PIM_MSDP_PEER_CONNECT_RETRY_TIME
);
1023 /* if a valid packet is rxed from the peer we can restart hold timer */
1024 void pim_msdp_peer_pkt_rxed(struct pim_msdp_peer
*mp
)
1026 if (mp
->state
== PIM_MSDP_ESTABLISHED
) {
1027 pim_msdp_peer_hold_timer_setup(mp
, true /* start */);
1031 /* if a valid packet is txed to the peer we can restart ka timer and avoid
1032 * unnecessary ka noise in the network */
1033 void pim_msdp_peer_pkt_txed(struct pim_msdp_peer
*mp
)
1035 if (mp
->state
== PIM_MSDP_ESTABLISHED
) {
1036 pim_msdp_peer_ka_timer_setup(mp
, true /* start */);
1037 if (PIM_DEBUG_MSDP_INTERNAL
) {
1038 zlog_debug("MSDP ka timer restart on pkt tx to %s",
1044 static void pim_msdp_addr2su(union sockunion
*su
, struct in_addr addr
)
1047 su
->sin
.sin_addr
= addr
;
1048 su
->sin
.sin_family
= AF_INET
;
1049 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1050 su
->sin
.sin_len
= sizeof(struct sockaddr_in
);
1051 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1054 /* 11.2.A1: create a new peer and transition state to listen or connecting */
1055 static enum pim_msdp_err
pim_msdp_peer_new(struct in_addr peer_addr
,
1056 struct in_addr local_addr
,
1057 const char *mesh_group_name
,
1058 struct pim_msdp_peer
**mp_p
)
1060 struct pim_msdp_peer
*mp
;
1064 mp
= XCALLOC(MTYPE_PIM_MSDP_PEER
, sizeof(*mp
));
1066 zlog_err("%s: PIM XCALLOC(%zu) failure", __PRETTY_FUNCTION__
,
1068 return PIM_MSDP_ERR_OOM
;
1071 mp
->peer
= peer_addr
;
1072 pim_inet4_dump("<peer?>", mp
->peer
, mp
->key_str
, sizeof(mp
->key_str
));
1073 pim_msdp_addr2su(&mp
->su_peer
, mp
->peer
);
1074 mp
->local
= local_addr
;
1075 /* XXX: originator_id setting needs to move to the mesh group */
1076 msdp
->originator_id
= local_addr
;
1077 pim_msdp_addr2su(&mp
->su_local
, mp
->local
);
1078 mp
->mesh_group_name
= XSTRDUP(MTYPE_PIM_MSDP_MG_NAME
, mesh_group_name
);
1079 mp
->state
= PIM_MSDP_INACTIVE
;
1081 strcpy(mp
->last_reset
, "-");
1082 /* higher IP address is listener */
1083 if (ntohl(mp
->local
.s_addr
) > ntohl(mp
->peer
.s_addr
)) {
1084 mp
->flags
|= PIM_MSDP_PEERF_LISTENER
;
1087 /* setup packet buffers */
1088 mp
->ibuf
= stream_new(PIM_MSDP_MAX_PACKET_SIZE
);
1089 mp
->obuf
= stream_fifo_new();
1091 /* insert into misc tables for easy access */
1092 mp
= hash_get(msdp
->peer_hash
, mp
, hash_alloc_intern
);
1093 listnode_add_sort(msdp
->peer_list
, mp
);
1095 if (PIM_DEBUG_MSDP_EVENTS
) {
1096 zlog_debug("MSDP peer %s created", mp
->key_str
);
1098 pim_msdp_peer_state_chg_log(mp
);
1101 /* fireup the connect state machine */
1102 if (PIM_MSDP_PEER_IS_LISTENER(mp
)) {
1103 pim_msdp_peer_listen(mp
);
1105 pim_msdp_peer_connect(mp
);
1110 return PIM_MSDP_ERR_NONE
;
1113 struct pim_msdp_peer
*pim_msdp_peer_find(struct in_addr peer_addr
)
1115 struct pim_msdp_peer lookup
;
1117 lookup
.peer
= peer_addr
;
1118 return hash_lookup(msdp
->peer_hash
, &lookup
);
1121 /* add peer configuration if it doesn't already exist */
1122 enum pim_msdp_err
pim_msdp_peer_add(struct in_addr peer_addr
,
1123 struct in_addr local_addr
,
1124 const char *mesh_group_name
,
1125 struct pim_msdp_peer
**mp_p
)
1127 struct pim_msdp_peer
*mp
;
1133 if (peer_addr
.s_addr
== local_addr
.s_addr
) {
1134 /* skip session setup if config is invalid */
1135 if (PIM_DEBUG_MSDP_EVENTS
) {
1136 char peer_str
[INET_ADDRSTRLEN
];
1138 pim_inet4_dump("<peer?>", peer_addr
, peer_str
,
1140 zlog_debug("%s add skipped as DIP=SIP", peer_str
);
1142 return PIM_MSDP_ERR_SIP_EQ_DIP
;
1145 mp
= pim_msdp_peer_find(peer_addr
);
1150 return PIM_MSDP_ERR_PEER_EXISTS
;
1153 return pim_msdp_peer_new(peer_addr
, local_addr
, mesh_group_name
, mp_p
);
1156 /* release all mem associated with a peer */
1157 static void pim_msdp_peer_free(struct pim_msdp_peer
*mp
)
1160 stream_free(mp
->ibuf
);
1164 stream_fifo_free(mp
->obuf
);
1167 if (mp
->mesh_group_name
) {
1168 XFREE(MTYPE_PIM_MSDP_MG_NAME
, mp
->mesh_group_name
);
1170 XFREE(MTYPE_PIM_MSDP_PEER
, mp
);
1173 /* delete the peer config */
1174 static enum pim_msdp_err
pim_msdp_peer_do_del(struct pim_msdp_peer
*mp
)
1176 /* stop the tcp connection and shutdown all timers */
1177 pim_msdp_peer_stop_tcp_conn(mp
, true /* chg_state */);
1179 /* remove the session from various tables */
1180 listnode_delete(msdp
->peer_list
, mp
);
1181 hash_release(msdp
->peer_hash
, mp
);
1183 if (PIM_DEBUG_MSDP_EVENTS
) {
1184 zlog_debug("MSDP peer %s deleted", mp
->key_str
);
1187 /* free up any associated memory */
1188 pim_msdp_peer_free(mp
);
1190 return PIM_MSDP_ERR_NONE
;
1193 enum pim_msdp_err
pim_msdp_peer_del(struct in_addr peer_addr
)
1195 struct pim_msdp_peer
*mp
;
1197 mp
= pim_msdp_peer_find(peer_addr
);
1199 return PIM_MSDP_ERR_NO_PEER
;
1202 return pim_msdp_peer_do_del(mp
);
1205 /* peer hash and peer list helpers */
1206 static unsigned int pim_msdp_peer_hash_key_make(void *p
)
1208 struct pim_msdp_peer
*mp
= p
;
1209 return (jhash_1word(mp
->peer
.s_addr
, 0));
1212 static int pim_msdp_peer_hash_eq(const void *p1
, const void *p2
)
1214 const struct pim_msdp_peer
*mp1
= p1
;
1215 const struct pim_msdp_peer
*mp2
= p2
;
1217 return (mp1
->peer
.s_addr
== mp2
->peer
.s_addr
);
1220 static int pim_msdp_peer_comp(const void *p1
, const void *p2
)
1222 const struct pim_msdp_peer
*mp1
= p1
;
1223 const struct pim_msdp_peer
*mp2
= p2
;
1225 if (ntohl(mp1
->peer
.s_addr
) < ntohl(mp2
->peer
.s_addr
))
1228 if (ntohl(mp1
->peer
.s_addr
) > ntohl(mp2
->peer
.s_addr
))
1234 /************************** Mesh group management **************************/
1235 static void pim_msdp_mg_free(struct pim_msdp_mg
*mg
)
1237 /* If the mesh-group has valid member or src_ip don't delete it */
1238 if (!mg
|| mg
->mbr_cnt
|| (mg
->src_ip
.s_addr
!= INADDR_ANY
)) {
1242 if (PIM_DEBUG_MSDP_EVENTS
) {
1243 zlog_debug("MSDP mesh-group %s deleted", mg
->mesh_group_name
);
1245 if (mg
->mesh_group_name
)
1246 XFREE(MTYPE_PIM_MSDP_MG_NAME
, mg
->mesh_group_name
);
1249 list_free(mg
->mbr_list
);
1251 XFREE(MTYPE_PIM_MSDP_MG
, mg
);
1255 static struct pim_msdp_mg
*pim_msdp_mg_new(const char *mesh_group_name
)
1257 struct pim_msdp_mg
*mg
;
1259 mg
= XCALLOC(MTYPE_PIM_MSDP_MG
, sizeof(*mg
));
1261 zlog_err("%s: PIM XCALLOC(%zu) failure", __PRETTY_FUNCTION__
,
1266 mg
->mesh_group_name
= XSTRDUP(MTYPE_PIM_MSDP_MG_NAME
, mesh_group_name
);
1267 mg
->mbr_list
= list_new();
1268 mg
->mbr_list
->del
= (void (*)(void *))pim_msdp_mg_mbr_free
;
1269 mg
->mbr_list
->cmp
= (int (*)(void *, void *))pim_msdp_mg_mbr_comp
;
1271 if (PIM_DEBUG_MSDP_EVENTS
) {
1272 zlog_debug("MSDP mesh-group %s created", mg
->mesh_group_name
);
1277 enum pim_msdp_err
pim_msdp_mg_del(const char *mesh_group_name
)
1279 struct pim_msdp_mg
*mg
= msdp
->mg
;
1280 struct pim_msdp_mg_mbr
*mbr
;
1282 if (!mg
|| strcmp(mg
->mesh_group_name
, mesh_group_name
)) {
1283 return PIM_MSDP_ERR_NO_MG
;
1286 /* delete all the mesh-group members */
1287 while (!list_isempty(mg
->mbr_list
)) {
1288 mbr
= listnode_head(mg
->mbr_list
);
1289 pim_msdp_mg_mbr_do_del(mg
, mbr
);
1293 mg
->src_ip
.s_addr
= INADDR_ANY
;
1295 /* free up the mesh-group */
1296 pim_msdp_mg_free(mg
);
1297 return PIM_MSDP_ERR_NONE
;
1300 static enum pim_msdp_err
pim_msdp_mg_add(const char *mesh_group_name
)
1303 if (!strcmp(msdp
->mg
->mesh_group_name
, mesh_group_name
)) {
1304 return PIM_MSDP_ERR_NONE
;
1306 /* currently only one mesh-group can exist at a time */
1307 return PIM_MSDP_ERR_MAX_MESH_GROUPS
;
1310 msdp
->mg
= pim_msdp_mg_new(mesh_group_name
);
1312 return PIM_MSDP_ERR_OOM
;
1315 return PIM_MSDP_ERR_NONE
;
1318 static int pim_msdp_mg_mbr_comp(const void *p1
, const void *p2
)
1320 const struct pim_msdp_mg_mbr
*mbr1
= p1
;
1321 const struct pim_msdp_mg_mbr
*mbr2
= p2
;
1323 if (ntohl(mbr1
->mbr_ip
.s_addr
) < ntohl(mbr2
->mbr_ip
.s_addr
))
1326 if (ntohl(mbr1
->mbr_ip
.s_addr
) > ntohl(mbr2
->mbr_ip
.s_addr
))
1332 static void pim_msdp_mg_mbr_free(struct pim_msdp_mg_mbr
*mbr
)
1334 XFREE(MTYPE_PIM_MSDP_MG_MBR
, mbr
);
1337 static struct pim_msdp_mg_mbr
*pim_msdp_mg_mbr_find(struct in_addr mbr_ip
)
1339 struct pim_msdp_mg_mbr
*mbr
;
1340 struct listnode
*mbr_node
;
1345 /* we can move this to a hash but considering that number of peers in
1346 * a mesh-group that seems like bit of an overkill */
1347 for (ALL_LIST_ELEMENTS_RO(msdp
->mg
->mbr_list
, mbr_node
, mbr
)) {
1348 if (mbr
->mbr_ip
.s_addr
== mbr_ip
.s_addr
) {
1355 enum pim_msdp_err
pim_msdp_mg_mbr_add(const char *mesh_group_name
,
1356 struct in_addr mbr_ip
)
1359 struct pim_msdp_mg_mbr
*mbr
;
1360 struct pim_msdp_mg
*mg
;
1362 rc
= pim_msdp_mg_add(mesh_group_name
);
1363 if (rc
!= PIM_MSDP_ERR_NONE
) {
1368 mbr
= pim_msdp_mg_mbr_find(mbr_ip
);
1370 return PIM_MSDP_ERR_MG_MBR_EXISTS
;
1373 mbr
= XCALLOC(MTYPE_PIM_MSDP_MG_MBR
, sizeof(*mbr
));
1375 zlog_err("%s: PIM XCALLOC(%zu) failure", __PRETTY_FUNCTION__
,
1377 /* if there are no references to the mg free it */
1378 pim_msdp_mg_free(mg
);
1379 return PIM_MSDP_ERR_OOM
;
1381 mbr
->mbr_ip
= mbr_ip
;
1382 listnode_add_sort(mg
->mbr_list
, mbr
);
1384 /* if valid SIP has been configured add peer session */
1385 if (mg
->src_ip
.s_addr
!= INADDR_ANY
) {
1386 pim_msdp_peer_add(mbr_ip
, mg
->src_ip
, mesh_group_name
,
1390 if (PIM_DEBUG_MSDP_EVENTS
) {
1391 char ip_str
[INET_ADDRSTRLEN
];
1392 pim_inet4_dump("<mbr?>", mbr
->mbr_ip
, ip_str
, sizeof(ip_str
));
1393 zlog_debug("MSDP mesh-group %s mbr %s created",
1394 mg
->mesh_group_name
, ip_str
);
1397 return PIM_MSDP_ERR_NONE
;
1400 static void pim_msdp_mg_mbr_do_del(struct pim_msdp_mg
*mg
,
1401 struct pim_msdp_mg_mbr
*mbr
)
1403 /* Delete active peer session if any */
1405 pim_msdp_peer_do_del(mbr
->mp
);
1408 listnode_delete(mg
->mbr_list
, mbr
);
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 deleted",
1413 mg
->mesh_group_name
, ip_str
);
1415 pim_msdp_mg_mbr_free(mbr
);
1421 enum pim_msdp_err
pim_msdp_mg_mbr_del(const char *mesh_group_name
,
1422 struct in_addr mbr_ip
)
1424 struct pim_msdp_mg_mbr
*mbr
;
1425 struct pim_msdp_mg
*mg
= msdp
->mg
;
1427 if (!mg
|| strcmp(mg
->mesh_group_name
, mesh_group_name
)) {
1428 return PIM_MSDP_ERR_NO_MG
;
1431 mbr
= pim_msdp_mg_mbr_find(mbr_ip
);
1433 return PIM_MSDP_ERR_NO_MG_MBR
;
1436 pim_msdp_mg_mbr_do_del(mg
, mbr
);
1437 /* if there are no references to the mg free it */
1438 pim_msdp_mg_free(mg
);
1440 return PIM_MSDP_ERR_NONE
;
1443 static void pim_msdp_mg_src_do_del(void)
1445 struct pim_msdp_mg_mbr
*mbr
;
1446 struct listnode
*mbr_node
;
1447 struct pim_msdp_mg
*mg
= msdp
->mg
;
1449 /* SIP is being removed - tear down all active peer sessions */
1450 for (ALL_LIST_ELEMENTS_RO(mg
->mbr_list
, mbr_node
, mbr
)) {
1452 pim_msdp_peer_do_del(mbr
->mp
);
1456 if (PIM_DEBUG_MSDP_EVENTS
) {
1457 zlog_debug("MSDP mesh-group %s src cleared",
1458 mg
->mesh_group_name
);
1462 enum pim_msdp_err
pim_msdp_mg_src_del(const char *mesh_group_name
)
1464 struct pim_msdp_mg
*mg
= msdp
->mg
;
1466 if (!mg
|| strcmp(mg
->mesh_group_name
, mesh_group_name
)) {
1467 return PIM_MSDP_ERR_NO_MG
;
1470 if (mg
->src_ip
.s_addr
!= INADDR_ANY
) {
1471 mg
->src_ip
.s_addr
= INADDR_ANY
;
1472 pim_msdp_mg_src_do_del();
1473 /* if there are no references to the mg free it */
1474 pim_msdp_mg_free(mg
);
1476 return PIM_MSDP_ERR_NONE
;
1479 enum pim_msdp_err
pim_msdp_mg_src_add(const char *mesh_group_name
,
1480 struct in_addr src_ip
)
1483 struct pim_msdp_mg_mbr
*mbr
;
1484 struct listnode
*mbr_node
;
1485 struct pim_msdp_mg
*mg
;
1487 if (src_ip
.s_addr
== INADDR_ANY
) {
1488 pim_msdp_mg_src_del(mesh_group_name
);
1489 return PIM_MSDP_ERR_NONE
;
1492 rc
= pim_msdp_mg_add(mesh_group_name
);
1493 if (rc
!= PIM_MSDP_ERR_NONE
) {
1498 if (mg
->src_ip
.s_addr
!= INADDR_ANY
) {
1499 pim_msdp_mg_src_do_del();
1501 mg
->src_ip
= src_ip
;
1503 for (ALL_LIST_ELEMENTS_RO(mg
->mbr_list
, mbr_node
, mbr
)) {
1504 pim_msdp_peer_add(mbr
->mbr_ip
, mg
->src_ip
, mesh_group_name
,
1508 if (PIM_DEBUG_MSDP_EVENTS
) {
1509 char ip_str
[INET_ADDRSTRLEN
];
1510 pim_inet4_dump("<src?>", mg
->src_ip
, ip_str
, sizeof(ip_str
));
1511 zlog_debug("MSDP mesh-group %s src %s set", mg
->mesh_group_name
,
1514 return PIM_MSDP_ERR_NONE
;
1517 /*********************** MSDP feature APIs *********************************/
1518 int pim_msdp_config_write(struct vty
*vty
)
1520 struct listnode
*mbrnode
;
1521 struct pim_msdp_mg_mbr
*mbr
;
1522 struct pim_msdp_mg
*mg
= msdp
->mg
;
1523 char mbr_str
[INET_ADDRSTRLEN
];
1524 char src_str
[INET_ADDRSTRLEN
];
1531 if (mg
->src_ip
.s_addr
!= INADDR_ANY
) {
1532 pim_inet4_dump("<src?>", mg
->src_ip
, src_str
, sizeof(src_str
));
1533 vty_out(vty
, "ip msdp mesh-group %s source %s%s",
1534 mg
->mesh_group_name
, src_str
, VTY_NEWLINE
);
1538 for (ALL_LIST_ELEMENTS_RO(mg
->mbr_list
, mbrnode
, mbr
)) {
1539 pim_inet4_dump("<mbr?>", mbr
->mbr_ip
, mbr_str
, sizeof(mbr_str
));
1540 vty_out(vty
, "ip msdp mesh-group %s member %s%s",
1541 mg
->mesh_group_name
, mbr_str
, VTY_NEWLINE
);
1547 /* Enable feature including active/periodic timers etc. on the first peer
1548 * config. Till then MSDP should just stay quiet. */
1549 static void pim_msdp_enable(void)
1551 if (msdp
->flags
& PIM_MSDPF_ENABLE
) {
1552 /* feature is already enabled */
1555 msdp
->flags
|= PIM_MSDPF_ENABLE
;
1556 msdp
->work_obuf
= stream_new(PIM_MSDP_MAX_PACKET_SIZE
);
1557 pim_msdp_sa_adv_timer_setup(true /* start */);
1558 /* setup sa cache based on local sources */
1559 pim_msdp_sa_local_setup();
1563 void pim_msdp_init(struct thread_master
*master
)
1565 msdp
->master
= master
;
1568 hash_create(pim_msdp_peer_hash_key_make
, pim_msdp_peer_hash_eq
);
1569 msdp
->peer_list
= list_new();
1570 msdp
->peer_list
->del
= (void (*)(void *))pim_msdp_peer_free
;
1571 msdp
->peer_list
->cmp
= (int (*)(void *, void *))pim_msdp_peer_comp
;
1574 hash_create(pim_msdp_sa_hash_key_make
, pim_msdp_sa_hash_eq
);
1575 msdp
->sa_list
= list_new();
1576 msdp
->sa_list
->del
= (void (*)(void *))pim_msdp_sa_free
;
1577 msdp
->sa_list
->cmp
= (int (*)(void *, void *))pim_msdp_sa_comp
;
1580 /* counterpart to MSDP init; XXX: unused currently */
1581 void pim_msdp_exit(void)
1583 /* XXX: stop listener and delete all peer sessions */
1585 if (msdp
->peer_hash
) {
1586 hash_free(msdp
->peer_hash
);
1587 msdp
->peer_hash
= NULL
;
1590 if (msdp
->peer_list
) {
1591 list_free(msdp
->peer_list
);
1592 msdp
->peer_list
= NULL
;