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 pim_msdp_sa_state_timer_setup(sa
, false);
233 XFREE(MTYPE_PIM_MSDP_SA
, sa
);
236 static struct pim_msdp_sa
*pim_msdp_sa_new(struct pim_instance
*pim
,
237 struct prefix_sg
*sg
,
240 struct pim_msdp_sa
*sa
;
242 sa
= XCALLOC(MTYPE_PIM_MSDP_SA
, sizeof(*sa
));
246 pim_str_sg_set(sg
, sa
->sg_str
);
248 sa
->uptime
= pim_time_monotonic_sec();
250 /* insert into misc tables for easy access */
251 sa
= hash_get(pim
->msdp
.sa_hash
, sa
, hash_alloc_intern
);
252 listnode_add_sort(pim
->msdp
.sa_list
, sa
);
254 if (PIM_DEBUG_MSDP_EVENTS
) {
255 zlog_debug("MSDP SA %s created", sa
->sg_str
);
261 static struct pim_msdp_sa
*pim_msdp_sa_find(struct pim_instance
*pim
,
262 struct prefix_sg
*sg
)
264 struct pim_msdp_sa lookup
;
267 return hash_lookup(pim
->msdp
.sa_hash
, &lookup
);
270 static struct pim_msdp_sa
*pim_msdp_sa_add(struct pim_instance
*pim
,
271 struct prefix_sg
*sg
,
274 struct pim_msdp_sa
*sa
;
276 sa
= pim_msdp_sa_find(pim
, sg
);
281 return pim_msdp_sa_new(pim
, sg
, rp
);
284 static void pim_msdp_sa_del(struct pim_msdp_sa
*sa
)
286 /* this is somewhat redundant - still want to be careful not to leave
287 * stale upstream references */
288 pim_msdp_sa_upstream_del(sa
);
291 pim_msdp_sa_state_timer_setup(sa
, false /* start */);
293 /* remove the entry from various tables */
294 listnode_delete(sa
->pim
->msdp
.sa_list
, sa
);
295 hash_release(sa
->pim
->msdp
.sa_hash
, sa
);
297 if (PIM_DEBUG_MSDP_EVENTS
) {
298 zlog_debug("MSDP SA %s deleted", sa
->sg_str
);
301 /* free up any associated memory */
302 pim_msdp_sa_free(sa
);
305 static void pim_msdp_sa_peer_ip_set(struct pim_msdp_sa
*sa
,
306 struct pim_msdp_peer
*mp
, struct in_addr rp
)
308 struct pim_msdp_peer
*old_mp
;
310 /* optimize the "no change" case as it will happen
311 * frequently/periodically */
312 if (mp
&& (sa
->peer
.s_addr
== mp
->peer
.s_addr
)) {
316 /* any time the peer ip changes also update the rp address */
317 if (PIM_INADDR_ISNOT_ANY(sa
->peer
)) {
318 old_mp
= pim_msdp_peer_find(sa
->pim
, sa
->peer
);
319 if (old_mp
&& old_mp
->sa_cnt
) {
328 sa
->peer
.s_addr
= PIM_NET_INADDR_ANY
;
333 /* When a local active-source is removed there is no way to withdraw the
334 * source from peers. We will simply remove it from the SA cache so it will
335 * not be sent in supsequent SA updates. Peers will consequently timeout the
337 * Similarly a "peer-added" SA is never explicitly deleted. It is simply
338 * aged out overtime if not seen in the SA updates from the peers.
339 * XXX: should we provide a knob to drop entries learnt from a peer when the
341 static void pim_msdp_sa_deref(struct pim_msdp_sa
*sa
,
342 enum pim_msdp_sa_flags flags
)
344 bool update_up
= false;
346 if ((sa
->flags
& PIM_MSDP_SAF_LOCAL
)) {
347 if (flags
& PIM_MSDP_SAF_LOCAL
) {
348 if (PIM_DEBUG_MSDP_EVENTS
) {
349 zlog_debug("MSDP SA %s local reference removed",
352 if (sa
->pim
->msdp
.local_cnt
)
353 --sa
->pim
->msdp
.local_cnt
;
357 if ((sa
->flags
& PIM_MSDP_SAF_PEER
)) {
358 if (flags
& PIM_MSDP_SAF_PEER
) {
361 if (PIM_DEBUG_MSDP_EVENTS
) {
362 zlog_debug("MSDP SA %s peer reference removed",
365 pim_msdp_sa_state_timer_setup(sa
, false /* start */);
366 rp
.s_addr
= INADDR_ANY
;
367 pim_msdp_sa_peer_ip_set(sa
, NULL
/* mp */, rp
);
368 /* if peer ref was removed we need to remove the msdp
377 pim_msdp_sa_upstream_update(sa
, NULL
/* xg_up */, "sa-deref");
380 if (!(sa
->flags
& PIM_MSDP_SAF_REF
)) {
385 void pim_msdp_sa_ref(struct pim_instance
*pim
, struct pim_msdp_peer
*mp
,
386 struct prefix_sg
*sg
, struct in_addr rp
)
388 struct pim_msdp_sa
*sa
;
390 sa
= pim_msdp_sa_add(pim
, sg
, rp
);
397 if (!(sa
->flags
& PIM_MSDP_SAF_PEER
)) {
398 sa
->flags
|= PIM_MSDP_SAF_PEER
;
399 if (PIM_DEBUG_MSDP_EVENTS
) {
400 zlog_debug("MSDP SA %s added by peer",
404 pim_msdp_sa_peer_ip_set(sa
, mp
, rp
);
405 /* start/re-start the state timer to prevent cache expiry */
406 pim_msdp_sa_state_timer_setup(sa
, true /* start */);
407 /* We re-evaluate SA "SPT-trigger" everytime we hear abt it from
409 * peer. XXX: If this becomes too much of a periodic overhead we
410 * can make it event based */
411 pim_msdp_sa_upstream_update(sa
, NULL
/* xg_up */, "peer-ref");
413 if (!(sa
->flags
& PIM_MSDP_SAF_LOCAL
)) {
414 sa
->flags
|= PIM_MSDP_SAF_LOCAL
;
415 ++sa
->pim
->msdp
.local_cnt
;
416 if (PIM_DEBUG_MSDP_EVENTS
) {
417 zlog_debug("MSDP SA %s added locally",
420 /* send an immediate SA update to peers */
421 pim_msdp_pkt_sa_tx_one(sa
);
423 sa
->flags
&= ~PIM_MSDP_SAF_STALE
;
427 /* The following criteria must be met to originate an SA from the MSDP
429 * 1. KAT must be running i.e. source is active.
430 * 2. We must be RP for the group.
431 * 3. Source must be registrable to the RP (this is where the RFC is vague
432 * and especially ambiguous in CLOS networks; with anycast RP all sources
433 * are potentially registrable to all RPs in the domain). We assume #3 is
435 * a. We are also the FHR-DR for the source (OR)
436 * b. We rxed a pim register (null or data encapsulated) within the last
437 * (3 * (1.5 * register_suppression_timer))).
439 static bool pim_msdp_sa_local_add_ok(struct pim_upstream
*up
)
441 struct pim_instance
*pim
= up
->channel_oil
->pim
;
443 if (!(pim
->msdp
.flags
& PIM_MSDPF_ENABLE
)) {
447 if (!up
->t_ka_timer
) {
448 /* stream is not active */
452 if (!I_am_RP(pim
, up
->sg
.grp
)) {
453 /* we are not RP for the group */
457 /* we are the FHR-DR for this stream or we are RP and have seen
459 * from a FHR for this source */
460 if (PIM_UPSTREAM_FLAG_TEST_FHR(up
->flags
) || up
->t_msdp_reg_timer
) {
467 static void pim_msdp_sa_local_add(struct pim_instance
*pim
,
468 struct prefix_sg
*sg
)
472 pim_msdp_sa_ref(pim
, NULL
/* mp */, sg
, rp
);
475 void pim_msdp_sa_local_del(struct pim_instance
*pim
, struct prefix_sg
*sg
)
477 struct pim_msdp_sa
*sa
;
479 sa
= pim_msdp_sa_find(pim
, sg
);
481 pim_msdp_sa_deref(sa
, PIM_MSDP_SAF_LOCAL
);
485 /* we need to be very cautious with this API as SA del too can trigger an
486 * upstream del and we will get stuck in a simple loop */
487 static void pim_msdp_sa_local_del_on_up_del(struct pim_instance
*pim
,
488 struct prefix_sg
*sg
)
490 struct pim_msdp_sa
*sa
;
492 sa
= pim_msdp_sa_find(pim
, 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 struct pim_instance
*pim
= up
->channel_oil
->pim
;
552 if (pim_msdp_sa_local_add_ok(up
)) {
553 pim_msdp_sa_local_add(pim
, &up
->sg
);
555 pim_msdp_sa_local_del(pim
, &up
->sg
);
559 static void pim_msdp_sa_local_setup(struct pim_instance
*pim
)
561 struct pim_upstream
*up
;
562 struct listnode
*up_node
;
564 for (ALL_LIST_ELEMENTS_RO(pim
->upstream_list
, up_node
, up
)) {
565 pim_msdp_sa_local_update(up
);
569 /* whenever the RP changes we need to re-evaluate the "local" SA-cache */
570 /* XXX: needs to be tested */
571 void pim_msdp_i_am_rp_changed(struct pim_instance
*pim
)
573 struct listnode
*sanode
;
574 struct listnode
*nextnode
;
575 struct pim_msdp_sa
*sa
;
577 if (!(pim
->msdp
.flags
& PIM_MSDPF_ENABLE
)) {
578 /* if the feature is not enabled do nothing */
582 if (PIM_DEBUG_MSDP_INTERNAL
) {
583 zlog_debug("MSDP i_am_rp changed");
586 /* mark all local entries as stale */
587 for (ALL_LIST_ELEMENTS_RO(pim
->msdp
.sa_list
, sanode
, sa
)) {
588 if (sa
->flags
& PIM_MSDP_SAF_LOCAL
) {
589 sa
->flags
|= PIM_MSDP_SAF_STALE
;
593 /* re-setup local SA entries */
594 pim_msdp_sa_local_setup(pim
);
596 for (ALL_LIST_ELEMENTS(pim
->msdp
.sa_list
, sanode
, nextnode
, sa
)) {
597 /* purge stale SA entries */
598 if (sa
->flags
& PIM_MSDP_SAF_STALE
) {
599 /* clear the stale flag; the entry may be kept even
602 sa
->flags
&= ~PIM_MSDP_SAF_STALE
;
603 /* sa_deref can end up freeing the sa; so don't access
605 pim_msdp_sa_deref(sa
, PIM_MSDP_SAF_LOCAL
);
607 /* if the souce is still active check if we can
609 pim_msdp_sa_upstream_update(sa
, NULL
/* xg_up */,
615 /* We track the join state of (*, G) entries. If G has sources in the SA-cache
616 * we need to setup or teardown SPT when the JoinDesired status changes for
618 void pim_msdp_up_join_state_changed(struct pim_instance
*pim
,
619 struct pim_upstream
*xg_up
)
621 struct listnode
*sanode
;
622 struct pim_msdp_sa
*sa
;
624 if (PIM_DEBUG_MSDP_INTERNAL
) {
625 zlog_debug("MSDP join state changed for %s", xg_up
->sg_str
);
628 /* If this is not really an XG entry just move on */
629 if ((xg_up
->sg
.src
.s_addr
!= INADDR_ANY
)
630 || (xg_up
->sg
.grp
.s_addr
== INADDR_ANY
)) {
634 /* XXX: Need to maintain SAs per-group to avoid all this unnecessary
636 for (ALL_LIST_ELEMENTS_RO(pim
->msdp
.sa_list
, sanode
, sa
)) {
637 if (sa
->sg
.grp
.s_addr
!= xg_up
->sg
.grp
.s_addr
) {
640 pim_msdp_sa_upstream_update(sa
, xg_up
, "up-jp-change");
644 static void pim_msdp_up_xg_del(struct pim_instance
*pim
, struct prefix_sg
*sg
)
646 struct listnode
*sanode
;
647 struct pim_msdp_sa
*sa
;
649 if (PIM_DEBUG_MSDP_INTERNAL
) {
650 zlog_debug("MSDP %s del", pim_str_sg_dump(sg
));
653 /* If this is not really an XG entry just move on */
654 if ((sg
->src
.s_addr
!= INADDR_ANY
) || (sg
->grp
.s_addr
== INADDR_ANY
)) {
658 /* XXX: Need to maintain SAs per-group to avoid all this unnecessary
660 for (ALL_LIST_ELEMENTS_RO(pim
->msdp
.sa_list
, sanode
, sa
)) {
661 if (sa
->sg
.grp
.s_addr
!= sg
->grp
.s_addr
) {
664 pim_msdp_sa_upstream_update(sa
, NULL
/* xg */, "up-jp-change");
668 void pim_msdp_up_del(struct pim_instance
*pim
, struct prefix_sg
*sg
)
670 if (PIM_DEBUG_MSDP_INTERNAL
) {
671 zlog_debug("MSDP up %s del", pim_str_sg_dump(sg
));
673 if (sg
->src
.s_addr
== INADDR_ANY
) {
674 pim_msdp_up_xg_del(pim
, sg
);
676 pim_msdp_sa_local_del_on_up_del(pim
, sg
);
680 /* sa hash and peer list helpers */
681 static unsigned int pim_msdp_sa_hash_key_make(void *p
)
683 struct pim_msdp_sa
*sa
= p
;
685 return (jhash_2words(sa
->sg
.src
.s_addr
, sa
->sg
.grp
.s_addr
, 0));
688 static int pim_msdp_sa_hash_eq(const void *p1
, const void *p2
)
690 const struct pim_msdp_sa
*sa1
= p1
;
691 const struct pim_msdp_sa
*sa2
= p2
;
693 return ((sa1
->sg
.src
.s_addr
== sa2
->sg
.src
.s_addr
)
694 && (sa1
->sg
.grp
.s_addr
== sa2
->sg
.grp
.s_addr
));
697 static int pim_msdp_sa_comp(const void *p1
, const void *p2
)
699 const struct pim_msdp_sa
*sa1
= p1
;
700 const struct pim_msdp_sa
*sa2
= p2
;
702 if (ntohl(sa1
->sg
.grp
.s_addr
) < ntohl(sa2
->sg
.grp
.s_addr
))
705 if (ntohl(sa1
->sg
.grp
.s_addr
) > ntohl(sa2
->sg
.grp
.s_addr
))
708 if (ntohl(sa1
->sg
.src
.s_addr
) < ntohl(sa2
->sg
.src
.s_addr
))
711 if (ntohl(sa1
->sg
.src
.s_addr
) > ntohl(sa2
->sg
.src
.s_addr
))
717 /* RFC-3618:Sec-10.1.3 - Peer-RPF forwarding */
718 /* XXX: this can use a bit of refining and extensions */
719 bool pim_msdp_peer_rpf_check(struct pim_msdp_peer
*mp
, struct in_addr rp
)
721 if (mp
->peer
.s_addr
== rp
.s_addr
) {
728 /************************ Peer session management **************************/
729 char *pim_msdp_state_dump(enum pim_msdp_peer_state state
, char *buf
,
733 case PIM_MSDP_DISABLED
:
734 snprintf(buf
, buf_size
, "%s", "disabled");
736 case PIM_MSDP_INACTIVE
:
737 snprintf(buf
, buf_size
, "%s", "inactive");
739 case PIM_MSDP_LISTEN
:
740 snprintf(buf
, buf_size
, "%s", "listen");
742 case PIM_MSDP_CONNECTING
:
743 snprintf(buf
, buf_size
, "%s", "connecting");
745 case PIM_MSDP_ESTABLISHED
:
746 snprintf(buf
, buf_size
, "%s", "established");
749 snprintf(buf
, buf_size
, "unk-%d", state
);
754 char *pim_msdp_peer_key_dump(struct pim_msdp_peer
*mp
, char *buf
, int buf_size
,
757 char peer_str
[INET_ADDRSTRLEN
];
758 char local_str
[INET_ADDRSTRLEN
];
760 pim_inet4_dump("<peer?>", mp
->peer
, peer_str
, sizeof(peer_str
));
762 pim_inet4_dump("<local?>", mp
->local
, local_str
,
764 snprintf(buf
, buf_size
, "MSDP peer %s local %s mg %s", peer_str
,
765 local_str
, mp
->mesh_group_name
);
767 snprintf(buf
, buf_size
, "MSDP peer %s", peer_str
);
773 static void pim_msdp_peer_state_chg_log(struct pim_msdp_peer
*mp
)
775 char state_str
[PIM_MSDP_STATE_STRLEN
];
777 pim_msdp_state_dump(mp
->state
, state_str
, sizeof(state_str
));
778 zlog_debug("MSDP peer %s state chg to %s", mp
->key_str
, state_str
);
781 /* MSDP Connection State Machine actions (defined in RFC-3618:Sec-11.2) */
782 /* 11.2.A2: active peer - start connect retry timer; when the timer fires
783 * a tcp connection will be made */
784 static void pim_msdp_peer_connect(struct pim_msdp_peer
*mp
)
786 mp
->state
= PIM_MSDP_CONNECTING
;
787 if (PIM_DEBUG_MSDP_EVENTS
) {
788 pim_msdp_peer_state_chg_log(mp
);
791 pim_msdp_peer_cr_timer_setup(mp
, true /* start */);
794 /* 11.2.A3: passive peer - just listen for connections */
795 static void pim_msdp_peer_listen(struct pim_msdp_peer
*mp
)
797 mp
->state
= PIM_MSDP_LISTEN
;
798 if (PIM_DEBUG_MSDP_EVENTS
) {
799 pim_msdp_peer_state_chg_log(mp
);
802 /* this is interntionally asymmetric i.e. we set up listen-socket when
804 * first listening peer is configured; but don't bother tearing it down
806 * all the peers go down */
807 pim_msdp_sock_listen(mp
->pim
);
810 /* 11.2.A4 and 11.2.A5: transition active or passive peer to
811 * established state */
812 void pim_msdp_peer_established(struct pim_msdp_peer
*mp
)
814 if (mp
->state
!= PIM_MSDP_ESTABLISHED
) {
818 mp
->state
= PIM_MSDP_ESTABLISHED
;
819 mp
->uptime
= pim_time_monotonic_sec();
821 if (PIM_DEBUG_MSDP_EVENTS
) {
822 pim_msdp_peer_state_chg_log(mp
);
825 /* stop retry timer on active peers */
826 pim_msdp_peer_cr_timer_setup(mp
, false /* start */);
828 /* send KA; start KA and hold timers */
829 pim_msdp_pkt_ka_tx(mp
);
830 pim_msdp_peer_ka_timer_setup(mp
, true /* start */);
831 pim_msdp_peer_hold_timer_setup(mp
, true /* start */);
833 pim_msdp_pkt_sa_tx_to_one_peer(mp
);
835 PIM_MSDP_PEER_WRITE_ON(mp
);
836 PIM_MSDP_PEER_READ_ON(mp
);
839 /* 11.2.A6, 11.2.A7 and 11.2.A8: shutdown the peer tcp connection */
840 void pim_msdp_peer_stop_tcp_conn(struct pim_msdp_peer
*mp
, bool chg_state
)
843 if (mp
->state
== PIM_MSDP_ESTABLISHED
) {
846 mp
->state
= PIM_MSDP_INACTIVE
;
847 if (PIM_DEBUG_MSDP_EVENTS
) {
848 pim_msdp_peer_state_chg_log(mp
);
852 if (PIM_DEBUG_MSDP_INTERNAL
) {
853 zlog_debug("MSDP peer %s pim_msdp_peer_stop_tcp_conn",
856 /* stop read and write threads */
857 PIM_MSDP_PEER_READ_OFF(mp
);
858 PIM_MSDP_PEER_WRITE_OFF(mp
);
863 stream_reset(mp
->ibuf
);
865 stream_fifo_clean(mp
->obuf
);
867 /* stop all peer timers */
868 pim_msdp_peer_ka_timer_setup(mp
, false /* start */);
869 pim_msdp_peer_cr_timer_setup(mp
, false /* start */);
870 pim_msdp_peer_hold_timer_setup(mp
, false /* start */);
872 /* close connection */
879 /* RFC-3618:Sec-5.6 - stop the peer tcp connection and startover */
880 void pim_msdp_peer_reset_tcp_conn(struct pim_msdp_peer
*mp
, const char *rc_str
)
882 if (PIM_DEBUG_EVENTS
) {
883 zlog_debug("MSDP peer %s tcp reset %s", mp
->key_str
, rc_str
);
884 snprintf(mp
->last_reset
, sizeof(mp
->last_reset
), "%s", rc_str
);
887 /* close the connection and transition to listening or connecting */
888 pim_msdp_peer_stop_tcp_conn(mp
, true /* chg_state */);
889 if (PIM_MSDP_PEER_IS_LISTENER(mp
)) {
890 pim_msdp_peer_listen(mp
);
892 pim_msdp_peer_connect(mp
);
896 static void pim_msdp_peer_timer_expiry_log(struct pim_msdp_peer
*mp
,
897 const char *timer_str
)
899 zlog_debug("MSDP peer %s %s timer expired", mp
->key_str
, timer_str
);
902 /* RFC-3618:Sec-5.4 - peer hold timer */
903 static int pim_msdp_peer_hold_timer_cb(struct thread
*t
)
905 struct pim_msdp_peer
*mp
;
909 if (PIM_DEBUG_MSDP_EVENTS
) {
910 pim_msdp_peer_timer_expiry_log(mp
, "hold");
913 if (mp
->state
!= PIM_MSDP_ESTABLISHED
) {
917 if (PIM_DEBUG_MSDP_EVENTS
) {
918 pim_msdp_peer_state_chg_log(mp
);
920 pim_msdp_peer_reset_tcp_conn(mp
, "ht-expired");
924 static void pim_msdp_peer_hold_timer_setup(struct pim_msdp_peer
*mp
, bool start
)
926 struct pim_instance
*pim
= mp
->pim
;
927 THREAD_OFF(mp
->hold_timer
);
929 thread_add_timer(pim
->msdp
.master
, pim_msdp_peer_hold_timer_cb
,
930 mp
, PIM_MSDP_PEER_HOLD_TIME
, &mp
->hold_timer
);
935 /* RFC-3618:Sec-5.5 - peer keepalive timer */
936 static int pim_msdp_peer_ka_timer_cb(struct thread
*t
)
938 struct pim_msdp_peer
*mp
;
942 if (PIM_DEBUG_MSDP_EVENTS
) {
943 pim_msdp_peer_timer_expiry_log(mp
, "ka");
946 pim_msdp_pkt_ka_tx(mp
);
947 pim_msdp_peer_ka_timer_setup(mp
, true /* start */);
950 static void pim_msdp_peer_ka_timer_setup(struct pim_msdp_peer
*mp
, bool start
)
952 THREAD_OFF(mp
->ka_timer
);
954 thread_add_timer(mp
->pim
->msdp
.master
,
955 pim_msdp_peer_ka_timer_cb
, mp
,
956 PIM_MSDP_PEER_KA_TIME
, &mp
->ka_timer
);
960 static void pim_msdp_peer_active_connect(struct pim_msdp_peer
*mp
)
964 rc
= pim_msdp_sock_connect(mp
);
966 if (PIM_DEBUG_MSDP_INTERNAL
) {
967 zlog_debug("MSDP peer %s pim_msdp_peer_active_connect: %d",
974 /* connect failed restart the connect-retry timer */
975 pim_msdp_peer_cr_timer_setup(mp
, true /* start */);
978 case connect_success
:
979 /* connect was sucessful move to established */
980 pim_msdp_peer_established(mp
);
983 case connect_in_progress
:
984 /* for NB content we need to wait till sock is readable or
986 PIM_MSDP_PEER_WRITE_ON(mp
);
987 PIM_MSDP_PEER_READ_ON(mp
);
988 /* also restart connect-retry timer to reset the socket if
991 pim_msdp_peer_cr_timer_setup(mp
, true /* start */);
996 /* RFC-3618:Sec-5.6 - connection retry on active peer */
997 static int pim_msdp_peer_cr_timer_cb(struct thread
*t
)
999 struct pim_msdp_peer
*mp
;
1003 if (PIM_DEBUG_MSDP_EVENTS
) {
1004 pim_msdp_peer_timer_expiry_log(mp
, "connect-retry");
1007 if (mp
->state
!= PIM_MSDP_CONNECTING
|| PIM_MSDP_PEER_IS_LISTENER(mp
)) {
1011 pim_msdp_peer_active_connect(mp
);
1014 static void pim_msdp_peer_cr_timer_setup(struct pim_msdp_peer
*mp
, bool start
)
1016 THREAD_OFF(mp
->cr_timer
);
1019 mp
->pim
->msdp
.master
, pim_msdp_peer_cr_timer_cb
, mp
,
1020 PIM_MSDP_PEER_CONNECT_RETRY_TIME
, &mp
->cr_timer
);
1024 /* if a valid packet is rxed from the peer we can restart hold timer */
1025 void pim_msdp_peer_pkt_rxed(struct pim_msdp_peer
*mp
)
1027 if (mp
->state
== PIM_MSDP_ESTABLISHED
) {
1028 pim_msdp_peer_hold_timer_setup(mp
, true /* start */);
1032 /* if a valid packet is txed to the peer we can restart ka timer and avoid
1033 * unnecessary ka noise in the network */
1034 void pim_msdp_peer_pkt_txed(struct pim_msdp_peer
*mp
)
1036 if (mp
->state
== PIM_MSDP_ESTABLISHED
) {
1037 pim_msdp_peer_ka_timer_setup(mp
, true /* start */);
1038 if (PIM_DEBUG_MSDP_INTERNAL
) {
1039 zlog_debug("MSDP ka timer restart on pkt tx to %s",
1045 static void pim_msdp_addr2su(union sockunion
*su
, struct in_addr addr
)
1048 su
->sin
.sin_addr
= addr
;
1049 su
->sin
.sin_family
= AF_INET
;
1050 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1051 su
->sin
.sin_len
= sizeof(struct sockaddr_in
);
1052 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1055 /* 11.2.A1: create a new peer and transition state to listen or connecting */
1056 static enum pim_msdp_err
pim_msdp_peer_new(struct pim_instance
*pim
,
1057 struct in_addr peer_addr
,
1058 struct in_addr local_addr
,
1059 const char *mesh_group_name
,
1060 struct pim_msdp_peer
**mp_p
)
1062 struct pim_msdp_peer
*mp
;
1064 pim_msdp_enable(pim
);
1066 mp
= XCALLOC(MTYPE_PIM_MSDP_PEER
, sizeof(*mp
));
1069 mp
->peer
= peer_addr
;
1070 pim_inet4_dump("<peer?>", mp
->peer
, mp
->key_str
, sizeof(mp
->key_str
));
1071 pim_msdp_addr2su(&mp
->su_peer
, mp
->peer
);
1072 mp
->local
= local_addr
;
1073 /* XXX: originator_id setting needs to move to the mesh group */
1074 pim
->msdp
.originator_id
= local_addr
;
1075 pim_msdp_addr2su(&mp
->su_local
, mp
->local
);
1076 mp
->mesh_group_name
= XSTRDUP(MTYPE_PIM_MSDP_MG_NAME
, mesh_group_name
);
1077 mp
->state
= PIM_MSDP_INACTIVE
;
1079 strcpy(mp
->last_reset
, "-");
1080 /* higher IP address is listener */
1081 if (ntohl(mp
->local
.s_addr
) > ntohl(mp
->peer
.s_addr
)) {
1082 mp
->flags
|= PIM_MSDP_PEERF_LISTENER
;
1085 /* setup packet buffers */
1086 mp
->ibuf
= stream_new(PIM_MSDP_MAX_PACKET_SIZE
);
1087 mp
->obuf
= stream_fifo_new();
1089 /* insert into misc tables for easy access */
1090 mp
= hash_get(pim
->msdp
.peer_hash
, mp
, hash_alloc_intern
);
1091 listnode_add_sort(pim
->msdp
.peer_list
, mp
);
1093 if (PIM_DEBUG_MSDP_EVENTS
) {
1094 zlog_debug("MSDP peer %s created", mp
->key_str
);
1096 pim_msdp_peer_state_chg_log(mp
);
1099 /* fireup the connect state machine */
1100 if (PIM_MSDP_PEER_IS_LISTENER(mp
)) {
1101 pim_msdp_peer_listen(mp
);
1103 pim_msdp_peer_connect(mp
);
1108 return PIM_MSDP_ERR_NONE
;
1111 struct pim_msdp_peer
*pim_msdp_peer_find(struct pim_instance
*pim
,
1112 struct in_addr peer_addr
)
1114 struct pim_msdp_peer lookup
;
1116 lookup
.peer
= peer_addr
;
1117 return hash_lookup(pim
->msdp
.peer_hash
, &lookup
);
1120 /* add peer configuration if it doesn't already exist */
1121 enum pim_msdp_err
pim_msdp_peer_add(struct pim_instance
*pim
,
1122 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(pim
, peer_addr
);
1150 return PIM_MSDP_ERR_PEER_EXISTS
;
1153 return pim_msdp_peer_new(pim
, peer_addr
, local_addr
, mesh_group_name
,
1157 /* release all mem associated with a peer */
1158 static void pim_msdp_peer_free(struct pim_msdp_peer
*mp
)
1161 * Let's make sure we are not running when we delete
1162 * the underlying data structure
1164 pim_msdp_peer_stop_tcp_conn(mp
, false);
1167 stream_free(mp
->ibuf
);
1171 stream_fifo_free(mp
->obuf
);
1174 if (mp
->mesh_group_name
) {
1175 XFREE(MTYPE_PIM_MSDP_MG_NAME
, mp
->mesh_group_name
);
1179 XFREE(MTYPE_PIM_MSDP_PEER
, mp
);
1182 /* delete the peer config */
1183 static enum pim_msdp_err
pim_msdp_peer_do_del(struct pim_msdp_peer
*mp
)
1185 /* stop the tcp connection and shutdown all timers */
1186 pim_msdp_peer_stop_tcp_conn(mp
, true /* chg_state */);
1188 /* remove the session from various tables */
1189 listnode_delete(mp
->pim
->msdp
.peer_list
, mp
);
1190 hash_release(mp
->pim
->msdp
.peer_hash
, mp
);
1192 if (PIM_DEBUG_MSDP_EVENTS
) {
1193 zlog_debug("MSDP peer %s deleted", mp
->key_str
);
1196 /* free up any associated memory */
1197 pim_msdp_peer_free(mp
);
1199 return PIM_MSDP_ERR_NONE
;
1202 enum pim_msdp_err
pim_msdp_peer_del(struct pim_instance
*pim
,
1203 struct in_addr peer_addr
)
1205 struct pim_msdp_peer
*mp
;
1207 mp
= pim_msdp_peer_find(pim
, peer_addr
);
1209 return PIM_MSDP_ERR_NO_PEER
;
1212 return pim_msdp_peer_do_del(mp
);
1215 /* peer hash and peer list helpers */
1216 static unsigned int pim_msdp_peer_hash_key_make(void *p
)
1218 struct pim_msdp_peer
*mp
= p
;
1219 return (jhash_1word(mp
->peer
.s_addr
, 0));
1222 static int pim_msdp_peer_hash_eq(const void *p1
, const void *p2
)
1224 const struct pim_msdp_peer
*mp1
= p1
;
1225 const struct pim_msdp_peer
*mp2
= p2
;
1227 return (mp1
->peer
.s_addr
== mp2
->peer
.s_addr
);
1230 static int pim_msdp_peer_comp(const void *p1
, const void *p2
)
1232 const struct pim_msdp_peer
*mp1
= p1
;
1233 const struct pim_msdp_peer
*mp2
= p2
;
1235 if (ntohl(mp1
->peer
.s_addr
) < ntohl(mp2
->peer
.s_addr
))
1238 if (ntohl(mp1
->peer
.s_addr
) > ntohl(mp2
->peer
.s_addr
))
1244 /************************** Mesh group management **************************/
1245 static void pim_msdp_mg_free(struct pim_instance
*pim
)
1247 struct pim_msdp_mg
*mg
= pim
->msdp
.mg
;
1249 /* If the mesh-group has valid member or src_ip don't delete it */
1250 if (!mg
|| mg
->mbr_cnt
|| (mg
->src_ip
.s_addr
!= INADDR_ANY
)) {
1254 if (PIM_DEBUG_MSDP_EVENTS
) {
1255 zlog_debug("MSDP mesh-group %s deleted", mg
->mesh_group_name
);
1257 if (mg
->mesh_group_name
)
1258 XFREE(MTYPE_PIM_MSDP_MG_NAME
, mg
->mesh_group_name
);
1261 list_delete_and_null(&mg
->mbr_list
);
1263 XFREE(MTYPE_PIM_MSDP_MG
, pim
->msdp
.mg
);
1266 static struct pim_msdp_mg
*pim_msdp_mg_new(const char *mesh_group_name
)
1268 struct pim_msdp_mg
*mg
;
1270 mg
= XCALLOC(MTYPE_PIM_MSDP_MG
, sizeof(*mg
));
1272 mg
->mesh_group_name
= XSTRDUP(MTYPE_PIM_MSDP_MG_NAME
, mesh_group_name
);
1273 mg
->mbr_list
= list_new();
1274 mg
->mbr_list
->del
= (void (*)(void *))pim_msdp_mg_mbr_free
;
1275 mg
->mbr_list
->cmp
= (int (*)(void *, void *))pim_msdp_mg_mbr_comp
;
1277 if (PIM_DEBUG_MSDP_EVENTS
) {
1278 zlog_debug("MSDP mesh-group %s created", mg
->mesh_group_name
);
1283 enum pim_msdp_err
pim_msdp_mg_del(struct pim_instance
*pim
,
1284 const char *mesh_group_name
)
1286 struct pim_msdp_mg
*mg
= pim
->msdp
.mg
;
1287 struct pim_msdp_mg_mbr
*mbr
;
1289 if (!mg
|| strcmp(mg
->mesh_group_name
, mesh_group_name
)) {
1290 return PIM_MSDP_ERR_NO_MG
;
1293 /* delete all the mesh-group members */
1294 while (!list_isempty(mg
->mbr_list
)) {
1295 mbr
= listnode_head(mg
->mbr_list
);
1296 pim_msdp_mg_mbr_do_del(mg
, mbr
);
1300 mg
->src_ip
.s_addr
= INADDR_ANY
;
1302 /* free up the mesh-group */
1303 pim_msdp_mg_free(pim
);
1304 return PIM_MSDP_ERR_NONE
;
1307 static enum pim_msdp_err
pim_msdp_mg_add(struct pim_instance
*pim
,
1308 const char *mesh_group_name
)
1311 if (!strcmp(pim
->msdp
.mg
->mesh_group_name
, mesh_group_name
)) {
1312 return PIM_MSDP_ERR_NONE
;
1314 /* currently only one mesh-group can exist at a time */
1315 return PIM_MSDP_ERR_MAX_MESH_GROUPS
;
1318 pim
->msdp
.mg
= pim_msdp_mg_new(mesh_group_name
);
1319 if (!pim
->msdp
.mg
) {
1320 return PIM_MSDP_ERR_OOM
;
1323 return PIM_MSDP_ERR_NONE
;
1326 static int pim_msdp_mg_mbr_comp(const void *p1
, const void *p2
)
1328 const struct pim_msdp_mg_mbr
*mbr1
= p1
;
1329 const struct pim_msdp_mg_mbr
*mbr2
= p2
;
1331 if (ntohl(mbr1
->mbr_ip
.s_addr
) < ntohl(mbr2
->mbr_ip
.s_addr
))
1334 if (ntohl(mbr1
->mbr_ip
.s_addr
) > ntohl(mbr2
->mbr_ip
.s_addr
))
1340 static void pim_msdp_mg_mbr_free(struct pim_msdp_mg_mbr
*mbr
)
1342 XFREE(MTYPE_PIM_MSDP_MG_MBR
, mbr
);
1345 static struct pim_msdp_mg_mbr
*pim_msdp_mg_mbr_find(struct pim_instance
*pim
,
1346 struct in_addr mbr_ip
)
1348 struct pim_msdp_mg_mbr
*mbr
;
1349 struct listnode
*mbr_node
;
1351 if (!pim
->msdp
.mg
) {
1354 /* we can move this to a hash but considering that number of peers in
1355 * a mesh-group that seems like bit of an overkill */
1356 for (ALL_LIST_ELEMENTS_RO(pim
->msdp
.mg
->mbr_list
, mbr_node
, mbr
)) {
1357 if (mbr
->mbr_ip
.s_addr
== mbr_ip
.s_addr
) {
1364 enum pim_msdp_err
pim_msdp_mg_mbr_add(struct pim_instance
*pim
,
1365 const char *mesh_group_name
,
1366 struct in_addr mbr_ip
)
1369 struct pim_msdp_mg_mbr
*mbr
;
1370 struct pim_msdp_mg
*mg
;
1372 rc
= pim_msdp_mg_add(pim
, mesh_group_name
);
1373 if (rc
!= PIM_MSDP_ERR_NONE
) {
1378 mbr
= pim_msdp_mg_mbr_find(pim
, mbr_ip
);
1380 return PIM_MSDP_ERR_MG_MBR_EXISTS
;
1383 mbr
= XCALLOC(MTYPE_PIM_MSDP_MG_MBR
, sizeof(*mbr
));
1384 mbr
->mbr_ip
= mbr_ip
;
1385 listnode_add_sort(mg
->mbr_list
, mbr
);
1387 /* if valid SIP has been configured add peer session */
1388 if (mg
->src_ip
.s_addr
!= INADDR_ANY
) {
1389 pim_msdp_peer_add(pim
, mbr_ip
, mg
->src_ip
, mesh_group_name
,
1393 if (PIM_DEBUG_MSDP_EVENTS
) {
1394 char ip_str
[INET_ADDRSTRLEN
];
1395 pim_inet4_dump("<mbr?>", mbr
->mbr_ip
, ip_str
, sizeof(ip_str
));
1396 zlog_debug("MSDP mesh-group %s mbr %s created",
1397 mg
->mesh_group_name
, ip_str
);
1400 return PIM_MSDP_ERR_NONE
;
1403 static void pim_msdp_mg_mbr_do_del(struct pim_msdp_mg
*mg
,
1404 struct pim_msdp_mg_mbr
*mbr
)
1406 /* Delete active peer session if any */
1408 pim_msdp_peer_do_del(mbr
->mp
);
1411 listnode_delete(mg
->mbr_list
, mbr
);
1412 if (PIM_DEBUG_MSDP_EVENTS
) {
1413 char ip_str
[INET_ADDRSTRLEN
];
1414 pim_inet4_dump("<mbr?>", mbr
->mbr_ip
, ip_str
, sizeof(ip_str
));
1415 zlog_debug("MSDP mesh-group %s mbr %s deleted",
1416 mg
->mesh_group_name
, ip_str
);
1418 pim_msdp_mg_mbr_free(mbr
);
1424 enum pim_msdp_err
pim_msdp_mg_mbr_del(struct pim_instance
*pim
,
1425 const char *mesh_group_name
,
1426 struct in_addr mbr_ip
)
1428 struct pim_msdp_mg_mbr
*mbr
;
1429 struct pim_msdp_mg
*mg
= pim
->msdp
.mg
;
1431 if (!mg
|| strcmp(mg
->mesh_group_name
, mesh_group_name
)) {
1432 return PIM_MSDP_ERR_NO_MG
;
1435 mbr
= pim_msdp_mg_mbr_find(pim
, mbr_ip
);
1437 return PIM_MSDP_ERR_NO_MG_MBR
;
1440 pim_msdp_mg_mbr_do_del(mg
, mbr
);
1441 /* if there are no references to the mg free it */
1442 pim_msdp_mg_free(pim
);
1444 return PIM_MSDP_ERR_NONE
;
1447 static void pim_msdp_mg_src_do_del(struct pim_instance
*pim
)
1449 struct pim_msdp_mg_mbr
*mbr
;
1450 struct listnode
*mbr_node
;
1451 struct pim_msdp_mg
*mg
= pim
->msdp
.mg
;
1453 /* SIP is being removed - tear down all active peer sessions */
1454 for (ALL_LIST_ELEMENTS_RO(mg
->mbr_list
, mbr_node
, mbr
)) {
1456 pim_msdp_peer_do_del(mbr
->mp
);
1460 if (PIM_DEBUG_MSDP_EVENTS
) {
1461 zlog_debug("MSDP mesh-group %s src cleared",
1462 mg
->mesh_group_name
);
1466 enum pim_msdp_err
pim_msdp_mg_src_del(struct pim_instance
*pim
,
1467 const char *mesh_group_name
)
1469 struct pim_msdp_mg
*mg
= pim
->msdp
.mg
;
1471 if (!mg
|| strcmp(mg
->mesh_group_name
, mesh_group_name
)) {
1472 return PIM_MSDP_ERR_NO_MG
;
1475 if (mg
->src_ip
.s_addr
!= INADDR_ANY
) {
1476 mg
->src_ip
.s_addr
= INADDR_ANY
;
1477 pim_msdp_mg_src_do_del(pim
);
1478 /* if there are no references to the mg free it */
1479 pim_msdp_mg_free(pim
);
1481 return PIM_MSDP_ERR_NONE
;
1484 enum pim_msdp_err
pim_msdp_mg_src_add(struct pim_instance
*pim
,
1485 const char *mesh_group_name
,
1486 struct in_addr src_ip
)
1489 struct pim_msdp_mg_mbr
*mbr
;
1490 struct listnode
*mbr_node
;
1491 struct pim_msdp_mg
*mg
;
1493 if (src_ip
.s_addr
== INADDR_ANY
) {
1494 pim_msdp_mg_src_del(pim
, mesh_group_name
);
1495 return PIM_MSDP_ERR_NONE
;
1498 rc
= pim_msdp_mg_add(pim
, mesh_group_name
);
1499 if (rc
!= PIM_MSDP_ERR_NONE
) {
1504 if (mg
->src_ip
.s_addr
!= INADDR_ANY
) {
1505 pim_msdp_mg_src_do_del(pim
);
1507 mg
->src_ip
= src_ip
;
1509 for (ALL_LIST_ELEMENTS_RO(mg
->mbr_list
, mbr_node
, mbr
)) {
1510 pim_msdp_peer_add(pim
, mbr
->mbr_ip
, mg
->src_ip
, mesh_group_name
,
1514 if (PIM_DEBUG_MSDP_EVENTS
) {
1515 char ip_str
[INET_ADDRSTRLEN
];
1516 pim_inet4_dump("<src?>", mg
->src_ip
, ip_str
, sizeof(ip_str
));
1517 zlog_debug("MSDP mesh-group %s src %s set", mg
->mesh_group_name
,
1520 return PIM_MSDP_ERR_NONE
;
1523 /*********************** MSDP feature APIs *********************************/
1524 int pim_msdp_config_write_helper(struct pim_instance
*pim
, struct vty
*vty
,
1527 struct listnode
*mbrnode
;
1528 struct pim_msdp_mg_mbr
*mbr
;
1529 struct pim_msdp_mg
*mg
= pim
->msdp
.mg
;
1530 char mbr_str
[INET_ADDRSTRLEN
];
1531 char src_str
[INET_ADDRSTRLEN
];
1538 if (mg
->src_ip
.s_addr
!= INADDR_ANY
) {
1539 pim_inet4_dump("<src?>", mg
->src_ip
, src_str
, sizeof(src_str
));
1540 vty_out(vty
, "%sip msdp mesh-group %s source %s\n", spaces
,
1541 mg
->mesh_group_name
, src_str
);
1545 for (ALL_LIST_ELEMENTS_RO(mg
->mbr_list
, mbrnode
, mbr
)) {
1546 pim_inet4_dump("<mbr?>", mbr
->mbr_ip
, mbr_str
, sizeof(mbr_str
));
1547 vty_out(vty
, "%sip msdp mesh-group %s member %s\n", spaces
,
1548 mg
->mesh_group_name
, mbr_str
);
1554 int pim_msdp_config_write(struct vty
*vty
)
1556 return pim_msdp_config_write_helper(pimg
, vty
, "");
1559 /* Enable feature including active/periodic timers etc. on the first peer
1560 * config. Till then MSDP should just stay quiet. */
1561 static void pim_msdp_enable(struct pim_instance
*pim
)
1563 if (pim
->msdp
.flags
& PIM_MSDPF_ENABLE
) {
1564 /* feature is already enabled */
1567 pim
->msdp
.flags
|= PIM_MSDPF_ENABLE
;
1568 pim
->msdp
.work_obuf
= stream_new(PIM_MSDP_MAX_PACKET_SIZE
);
1569 pim_msdp_sa_adv_timer_setup(pim
, true /* start */);
1570 /* setup sa cache based on local sources */
1571 pim_msdp_sa_local_setup(pim
);
1575 void pim_msdp_init(struct pim_instance
*pim
, struct thread_master
*master
)
1577 pim
->msdp
.master
= master
;
1580 snprintf(hash_name
, 64, "PIM %s MSDP Peer Hash", pim
->vrf
->name
);
1581 pim
->msdp
.peer_hash
= hash_create(pim_msdp_peer_hash_key_make
,
1582 pim_msdp_peer_hash_eq
, hash_name
);
1583 pim
->msdp
.peer_list
= list_new();
1584 pim
->msdp
.peer_list
->del
= (void (*)(void *))pim_msdp_peer_free
;
1585 pim
->msdp
.peer_list
->cmp
= (int (*)(void *, void *))pim_msdp_peer_comp
;
1587 snprintf(hash_name
, 64, "PIM %s MSDP SA Hash", pim
->vrf
->name
);
1588 pim
->msdp
.sa_hash
= hash_create(pim_msdp_sa_hash_key_make
,
1589 pim_msdp_sa_hash_eq
, hash_name
);
1590 pim
->msdp
.sa_list
= list_new();
1591 pim
->msdp
.sa_list
->del
= (void (*)(void *))pim_msdp_sa_free
;
1592 pim
->msdp
.sa_list
->cmp
= (int (*)(void *, void *))pim_msdp_sa_comp
;
1595 /* counterpart to MSDP init; XXX: unused currently */
1596 void pim_msdp_exit(struct pim_instance
*pim
)
1598 pim_msdp_sa_adv_timer_setup(pim
, false);
1600 /* XXX: stop listener and delete all peer sessions */
1602 pim_msdp_mg_free(pim
);
1604 if (pim
->msdp
.peer_hash
) {
1605 hash_clean(pim
->msdp
.peer_hash
, NULL
);
1606 hash_free(pim
->msdp
.peer_hash
);
1607 pim
->msdp
.peer_hash
= NULL
;
1610 if (pim
->msdp
.peer_list
) {
1611 list_delete_and_null(&pim
->msdp
.peer_list
);
1614 if (pim
->msdp
.sa_hash
) {
1615 hash_clean(pim
->msdp
.sa_hash
, NULL
);
1616 hash_free(pim
->msdp
.sa_hash
);
1617 pim
->msdp
.sa_hash
= NULL
;
1620 if (pim
->msdp
.sa_list
) {
1621 list_delete_and_null(&pim
->msdp
.sa_list
);
1624 if (pim
->msdp
.work_obuf
)
1625 stream_free(pim
->msdp
.work_obuf
);
1626 pim
->msdp
.work_obuf
= NULL
;