1 /* PIM support for VxLAN BUM flooding
3 * Copyright (C) 2019 Cumulus Networks, Inc.
5 * This file is part of FRR.
7 * FRR is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * FRR is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
31 #include "pim_iface.h"
32 #include "pim_memory.h"
34 #include "pim_register.h"
36 #include "pim_upstream.h"
37 #include "pim_ifchannel.h"
39 #include "pim_zebra.h"
40 #include "pim_vxlan.h"
43 /* pim-vxlan global info */
44 struct pim_vxlan vxlan_info
, *pim_vxlan_p
= &vxlan_info
;
46 static void pim_vxlan_work_timer_setup(bool start
);
47 static void pim_vxlan_set_peerlink_rif(struct pim_instance
*pim
,
48 struct interface
*ifp
);
50 /*************************** vxlan work list **********************************
51 * A work list is maintained for staggered generation of pim null register
52 * messages for vxlan SG entries that are in a reg_join state.
54 * A max of 500 NULL registers are generated at one shot. If paused reg
55 * generation continues on the next second and so on till all register
56 * messages have been sent out. And the process is restarted every 60s.
58 * purpose of this null register generation is to setup the SPT and maintain
59 * independent of the presence of overlay BUM traffic.
60 ****************************************************************************/
61 static void pim_vxlan_do_reg_work(void)
63 struct listnode
*listnode
;
65 struct pim_vxlan_sg
*vxlan_sg
;
70 if (sec_count
> PIM_VXLAN_NULL_REG_INTERVAL
) {
72 listnode
= vxlan_info
.next_work
?
73 vxlan_info
.next_work
:
74 vxlan_info
.work_list
->head
;
75 if (PIM_DEBUG_VXLAN
&& listnode
)
76 zlog_debug("vxlan SG work %s",
77 vxlan_info
.next_work
? "continues" : "starts");
79 listnode
= vxlan_info
.next_work
;
82 for (; listnode
; listnode
= listnode
->next
) {
83 vxlan_sg
= (struct pim_vxlan_sg
*)listnode
->data
;
84 if (vxlan_sg
->up
&& (vxlan_sg
->up
->reg_state
== PIM_REG_JOIN
)) {
86 zlog_debug("vxlan SG %s periodic NULL register",
90 * If we are on the work queue *and* the rpf
91 * has been lost on the vxlan_sg->up let's
92 * make sure that we don't send it.
94 if (vxlan_sg
->up
->rpf
.source_nexthop
.interface
) {
95 pim_null_register_send(vxlan_sg
->up
);
100 if (work_cnt
> vxlan_info
.max_work_cnt
) {
101 vxlan_info
.next_work
= listnode
->next
;
103 zlog_debug("vxlan SG %d work items proc and pause",
111 zlog_debug("vxlan SG %d work items proc", work_cnt
);
113 vxlan_info
.next_work
= NULL
;
116 /* Staggered work related info is initialized when the first work comes
119 static void pim_vxlan_init_work(void)
121 if (vxlan_info
.flags
& PIM_VXLANF_WORK_INITED
)
124 vxlan_info
.max_work_cnt
= PIM_VXLAN_WORK_MAX
;
125 vxlan_info
.flags
|= PIM_VXLANF_WORK_INITED
;
126 vxlan_info
.work_list
= list_new();
127 pim_vxlan_work_timer_setup(true/* start */);
130 static void pim_vxlan_add_work(struct pim_vxlan_sg
*vxlan_sg
)
132 if (vxlan_sg
->flags
& PIM_VXLAN_SGF_DEL_IN_PROG
) {
134 zlog_debug("vxlan SG %s skip work list; del-in-prog",
139 pim_vxlan_init_work();
141 /* already a part of the work list */
142 if (vxlan_sg
->work_node
)
146 zlog_debug("vxlan SG %s work list add",
148 vxlan_sg
->work_node
= listnode_add(vxlan_info
.work_list
, vxlan_sg
);
149 /* XXX: adjust max_work_cnt if needed */
152 static void pim_vxlan_del_work(struct pim_vxlan_sg
*vxlan_sg
)
154 if (!vxlan_sg
->work_node
)
158 zlog_debug("vxlan SG %s work list del",
161 if (vxlan_sg
->work_node
== vxlan_info
.next_work
)
162 vxlan_info
.next_work
= vxlan_sg
->work_node
->next
;
164 list_delete_node(vxlan_info
.work_list
, vxlan_sg
->work_node
);
165 vxlan_sg
->work_node
= NULL
;
168 void pim_vxlan_update_sg_reg_state(struct pim_instance
*pim
,
169 struct pim_upstream
*up
, bool reg_join
)
171 struct pim_vxlan_sg
*vxlan_sg
;
173 vxlan_sg
= pim_vxlan_sg_find(pim
, &up
->sg
);
177 /* add the vxlan sg entry to a work list for periodic reg joins.
178 * the entry will stay in the list as long as the register state is
182 pim_vxlan_add_work(vxlan_sg
);
184 pim_vxlan_del_work(vxlan_sg
);
187 static void pim_vxlan_work_timer_cb(struct thread
*t
)
189 pim_vxlan_do_reg_work();
190 pim_vxlan_work_timer_setup(true /* start */);
193 /* global 1second timer used for periodic processing */
194 static void pim_vxlan_work_timer_setup(bool start
)
196 THREAD_OFF(vxlan_info
.work_timer
);
198 thread_add_timer(router
->master
, pim_vxlan_work_timer_cb
, NULL
,
199 PIM_VXLAN_WORK_TIME
, &vxlan_info
.work_timer
);
202 /**************************** vxlan origination mroutes ***********************
203 * For every (local-vtep-ip, bum-mcast-grp) registered by evpn an origination
204 * mroute is setup by pimd. The purpose of this mroute is to forward vxlan
205 * encapsulated BUM (broadcast, unknown-unicast and unknown-multicast packets
206 * over the underlay.)
208 * Sample mroute (single VTEP):
209 * (27.0.0.7, 239.1.1.100) Iif: lo Oifs: uplink-1
211 * Sample mroute (anycast VTEP):
212 * (36.0.0.9, 239.1.1.100) Iif: peerlink-3.4094\
213 * Oifs: peerlink-3.4094 uplink-1
214 ***************************************************************************/
215 static void pim_vxlan_orig_mr_up_del(struct pim_vxlan_sg
*vxlan_sg
)
217 struct pim_upstream
*up
= vxlan_sg
->up
;
223 zlog_debug("vxlan SG %s orig mroute-up del",
228 if (up
->flags
& PIM_UPSTREAM_FLAG_MASK_SRC_VXLAN_ORIG
) {
229 /* clear out all the vxlan properties */
230 up
->flags
&= ~(PIM_UPSTREAM_FLAG_MASK_SRC_VXLAN_ORIG
|
231 PIM_UPSTREAM_FLAG_MASK_STATIC_IIF
|
232 PIM_UPSTREAM_FLAG_MASK_DISABLE_KAT_EXPIRY
|
233 PIM_UPSTREAM_FLAG_MASK_FORCE_PIMREG
|
234 PIM_UPSTREAM_FLAG_MASK_NO_PIMREG_DATA
|
235 PIM_UPSTREAM_FLAG_MASK_ALLOW_IIF_IN_OIL
);
237 /* We bring things to a grinding halt by force expirying
238 * the kat. Doing this will also remove the reference we
239 * created as a "vxlan" source and delete the upstream entry
240 * if there are no other references.
242 if (PIM_UPSTREAM_FLAG_TEST_SRC_STREAM(up
->flags
)) {
243 THREAD_OFF(up
->t_ka_timer
);
244 up
= pim_upstream_keep_alive_timer_proc(up
);
246 /* this is really unexpected as we force vxlan
247 * origination mroutes active sources but just in
250 up
= pim_upstream_del(vxlan_sg
->pim
, up
, __func__
);
252 /* if there are other references register the source
256 enum pim_rpf_result r
;
258 r
= pim_rpf_update(vxlan_sg
->pim
, up
, NULL
, __func__
);
259 if (r
== PIM_RPF_FAILURE
) {
262 "vxlan SG %s rpf_update failure",
269 static void pim_vxlan_orig_mr_up_iif_update(struct pim_vxlan_sg
*vxlan_sg
)
271 /* update MFC with the new IIF */
272 pim_upstream_fill_static_iif(vxlan_sg
->up
, vxlan_sg
->iif
);
273 pim_upstream_mroute_iif_update(vxlan_sg
->up
->channel_oil
, __func__
);
276 zlog_debug("vxlan SG %s orig mroute-up updated with iif %s",
278 vxlan_sg
->iif
?vxlan_sg
->iif
->name
:"-");
282 /* For every VxLAN BUM multicast group we setup a SG-up that has the following
283 * "forced properties" -
284 * 1. Directly connected on a DR interface i.e. we must act as an FHR
285 * 2. We prime the pump i.e. no multicast data is needed to register this
286 * source with the FHR. To do that we send periodic null registers if
287 * the SG entry is in a register-join state. We also prevent expiry of
289 * 3. As this SG is setup without data there is no need to register encapsulate
290 * data traffic. This encapsulation is explicitly skipped for the following
292 * a) Many levels of encapsulation are needed creating MTU disc challenges.
293 * Overlay BUM is encapsulated in a vxlan/UDP/IP header and then
294 * encapsulated again in a pim-register header.
295 * b) On a vxlan-aa setup both switches rx a copy of each BUM packet. if
296 * they both reg encapsulated traffic the RP will accept the duplicates
297 * as there are no RPF checks for this encapsulated data.
298 * a), b) can be workarounded if needed, but there is really no need because
299 * of (2) i.e. the pump is primed without data.
301 static void pim_vxlan_orig_mr_up_add(struct pim_vxlan_sg
*vxlan_sg
)
303 struct pim_upstream
*up
;
304 struct pim_interface
*term_ifp
;
306 struct pim_instance
*pim
= vxlan_sg
->pim
;
314 zlog_debug("vxlan SG %s orig mroute-up add with iif %s",
316 vxlan_sg
->iif
?vxlan_sg
->iif
->name
:"-");
318 PIM_UPSTREAM_FLAG_SET_SRC_VXLAN_ORIG(flags
);
319 /* pin the IIF to lo or peerlink-subinterface and disable NHT */
320 PIM_UPSTREAM_FLAG_SET_STATIC_IIF(flags
);
321 /* Fake traffic by setting SRC_STREAM and starting KAT */
322 /* We intentionally skip updating ref count for SRC_STREAM/FHR.
323 * Setting SRC_VXLAN should have already created a reference
324 * preventing the entry from being deleted
326 PIM_UPSTREAM_FLAG_SET_FHR(flags
);
327 PIM_UPSTREAM_FLAG_SET_SRC_STREAM(flags
);
328 /* Force pimreg even if non-DR. This is needed on a MLAG setup for
331 PIM_UPSTREAM_FLAG_SET_FORCE_PIMREG(flags
);
332 /* prevent KAT expiry. we want the MDT setup even if there is no BUM
335 PIM_UPSTREAM_FLAG_SET_DISABLE_KAT_EXPIRY(flags
);
336 /* SPT for vxlan BUM groups is primed and maintained via NULL
337 * registers so there is no need to reg-encapsulate
338 * vxlan-encapsulated overlay data traffic
340 PIM_UPSTREAM_FLAG_SET_NO_PIMREG_DATA(flags
);
341 /* On a MLAG setup we force a copy to the MLAG peer while also
342 * accepting traffic from the peer. To do this we set peerlink-rif as
343 * the IIF and also add it to the OIL
345 PIM_UPSTREAM_FLAG_SET_ALLOW_IIF_IN_OIL(flags
);
347 /* XXX: todo: defer pim_upstream add if pim is not enabled on the iif */
348 up
= pim_upstream_find(vxlan_sg
->pim
, &vxlan_sg
->sg
);
350 /* if the iif is set to something other than the vxlan_sg->iif
351 * we must dereg the old nexthop and force to new "static"
354 if (!PIM_UPSTREAM_FLAG_TEST_STATIC_IIF(up
->flags
)) {
355 pim_delete_tracked_nexthop(vxlan_sg
->pim
,
356 up
->upstream_addr
, up
, NULL
);
358 /* We are acting FHR; clear out use_rpt setting if any */
359 pim_upstream_update_use_rpt(up
, false /*update_mroute*/);
360 pim_upstream_ref(up
, flags
, __func__
);
362 term_ifp
= pim_vxlan_get_term_ifp(pim
);
363 /* mute termination device on origination mroutes */
365 pim_channel_update_oif_mute(up
->channel_oil
,
367 pim_vxlan_orig_mr_up_iif_update(vxlan_sg
);
368 /* mute pimreg on origination mroutes */
370 pim_channel_update_oif_mute(up
->channel_oil
,
371 pim
->regiface
->info
);
373 up
= pim_upstream_add(vxlan_sg
->pim
, &vxlan_sg
->sg
,
374 vxlan_sg
->iif
, flags
, __func__
, NULL
);
380 zlog_debug("vxlan SG %s orig mroute-up add failed",
385 pim_upstream_keep_alive_timer_start(up
, vxlan_sg
->pim
->keep_alive_time
);
387 /* register the source with the RP */
388 switch (up
->reg_state
) {
391 pim_register_join(up
);
392 pim_null_register_send(up
);
396 /* if the pim upstream entry is already in reg-join state
397 * send null_register right away and add to the register
400 pim_null_register_send(up
);
401 pim_vxlan_update_sg_reg_state(pim
, up
, true);
404 case PIM_REG_JOIN_PENDING
:
409 /* update the inherited OIL */
410 pim_upstream_inherited_olist(vxlan_sg
->pim
, up
);
411 if (!up
->channel_oil
->installed
)
412 pim_upstream_mroute_add(up
->channel_oil
, __func__
);
415 static void pim_vxlan_orig_mr_oif_add(struct pim_vxlan_sg
*vxlan_sg
)
417 if (!vxlan_sg
->up
|| !vxlan_sg
->orig_oif
)
421 zlog_debug("vxlan SG %s oif %s add",
422 vxlan_sg
->sg_str
, vxlan_sg
->orig_oif
->name
);
424 vxlan_sg
->flags
|= PIM_VXLAN_SGF_OIF_INSTALLED
;
425 pim_channel_add_oif(vxlan_sg
->up
->channel_oil
,
426 vxlan_sg
->orig_oif
, PIM_OIF_FLAG_PROTO_VXLAN
,
430 static void pim_vxlan_orig_mr_oif_del(struct pim_vxlan_sg
*vxlan_sg
)
432 struct interface
*orig_oif
;
434 orig_oif
= vxlan_sg
->orig_oif
;
435 vxlan_sg
->orig_oif
= NULL
;
437 if (!(vxlan_sg
->flags
& PIM_VXLAN_SGF_OIF_INSTALLED
))
441 zlog_debug("vxlan SG %s oif %s del",
442 vxlan_sg
->sg_str
, orig_oif
->name
);
444 vxlan_sg
->flags
&= ~PIM_VXLAN_SGF_OIF_INSTALLED
;
445 pim_channel_del_oif(vxlan_sg
->up
->channel_oil
,
446 orig_oif
, PIM_OIF_FLAG_PROTO_VXLAN
, __func__
);
449 static inline struct interface
*pim_vxlan_orig_mr_oif_get(
450 struct pim_instance
*pim
)
452 return (vxlan_mlag
.flags
& PIM_VXLAN_MLAGF_ENABLED
) ?
453 pim
->vxlan
.peerlink_rif
: NULL
;
456 /* Single VTEPs: IIF for the vxlan-origination-mroutes is lo or vrf-dev (if
457 * the mroute is in a non-default vrf).
458 * Anycast VTEPs: IIF is the MLAG ISL/peerlink.
460 static inline struct interface
*pim_vxlan_orig_mr_iif_get(
461 struct pim_instance
*pim
)
463 return ((vxlan_mlag
.flags
& PIM_VXLAN_MLAGF_ENABLED
) &&
464 pim
->vxlan
.peerlink_rif
) ?
465 pim
->vxlan
.peerlink_rif
: pim
->vxlan
.default_iif
;
468 static bool pim_vxlan_orig_mr_add_is_ok(struct pim_vxlan_sg
*vxlan_sg
)
470 struct pim_interface
*pim_ifp
;
472 vxlan_sg
->iif
= pim_vxlan_orig_mr_iif_get(vxlan_sg
->pim
);
476 pim_ifp
= (struct pim_interface
*)vxlan_sg
->iif
->info
;
477 if (!pim_ifp
|| (pim_ifp
->mroute_vif_index
< 0))
483 static void pim_vxlan_orig_mr_install(struct pim_vxlan_sg
*vxlan_sg
)
485 pim_vxlan_orig_mr_up_add(vxlan_sg
);
487 vxlan_sg
->orig_oif
= pim_vxlan_orig_mr_oif_get(vxlan_sg
->pim
);
488 pim_vxlan_orig_mr_oif_add(vxlan_sg
);
491 static void pim_vxlan_orig_mr_add(struct pim_vxlan_sg
*vxlan_sg
)
493 if (!pim_vxlan_orig_mr_add_is_ok(vxlan_sg
))
497 zlog_debug("vxlan SG %s orig-mr add", vxlan_sg
->sg_str
);
499 pim_vxlan_orig_mr_install(vxlan_sg
);
502 static void pim_vxlan_orig_mr_del(struct pim_vxlan_sg
*vxlan_sg
)
505 zlog_debug("vxlan SG %s orig-mr del", vxlan_sg
->sg_str
);
507 pim_vxlan_orig_mr_oif_del(vxlan_sg
);
508 pim_vxlan_orig_mr_up_del(vxlan_sg
);
511 static void pim_vxlan_orig_mr_iif_update(struct hash_bucket
*bucket
, void *arg
)
513 struct interface
*ifp
;
514 struct pim_vxlan_sg
*vxlan_sg
= (struct pim_vxlan_sg
*)bucket
->data
;
515 struct interface
*old_iif
= vxlan_sg
->iif
;
517 if (!pim_vxlan_is_orig_mroute(vxlan_sg
))
520 ifp
= pim_vxlan_orig_mr_iif_get(vxlan_sg
->pim
);
522 zlog_debug("vxlan SG %s iif changed from %s to %s",
524 old_iif
? old_iif
->name
: "-",
525 ifp
? ifp
->name
: "-");
527 if (pim_vxlan_orig_mr_add_is_ok(vxlan_sg
)) {
529 /* upstream exists but iif changed */
530 pim_vxlan_orig_mr_up_iif_update(vxlan_sg
);
533 pim_vxlan_orig_mr_install(vxlan_sg
);
536 pim_vxlan_orig_mr_del(vxlan_sg
);
540 /**************************** vxlan termination mroutes ***********************
541 * For every bum-mcast-grp registered by evpn a *G termination
542 * mroute is setup by pimd. The purpose of this mroute is to pull down vxlan
543 * packets with the bum-mcast-grp dip from the underlay and terminate the
544 * tunnel. This is done by including the vxlan termination device (ipmr-lo) in
545 * its OIL. The vxlan de-capsulated packets are subject to subsequent overlay
549 * (0.0.0.0, 239.1.1.100) Iif: uplink-1 Oifs: ipmr-lo, uplink-1
550 *****************************************************************************/
551 struct pim_interface
*pim_vxlan_get_term_ifp(struct pim_instance
*pim
)
553 return pim
->vxlan
.term_if
?
554 (struct pim_interface
*)pim
->vxlan
.term_if
->info
: NULL
;
557 static void pim_vxlan_term_mr_oif_add(struct pim_vxlan_sg
*vxlan_sg
)
559 if (vxlan_sg
->flags
& PIM_VXLAN_SGF_OIF_INSTALLED
)
563 zlog_debug("vxlan SG %s term-oif %s add",
564 vxlan_sg
->sg_str
, vxlan_sg
->term_oif
->name
);
566 if (pim_ifchannel_local_membership_add(vxlan_sg
->term_oif
,
567 &vxlan_sg
->sg
, true /*is_vxlan */)) {
568 vxlan_sg
->flags
|= PIM_VXLAN_SGF_OIF_INSTALLED
;
569 /* update the inherited OIL */
570 /* XXX - I don't see the inherited OIL updated when a local
571 * member is added. And that probably needs to be fixed. Till
572 * that happens we do a force update on the inherited OIL
575 pim_upstream_inherited_olist(vxlan_sg
->pim
, vxlan_sg
->up
);
577 zlog_warn("vxlan SG %s term-oif %s add failed",
578 vxlan_sg
->sg_str
, vxlan_sg
->term_oif
->name
);
582 static void pim_vxlan_term_mr_oif_del(struct pim_vxlan_sg
*vxlan_sg
)
584 if (!(vxlan_sg
->flags
& PIM_VXLAN_SGF_OIF_INSTALLED
))
588 zlog_debug("vxlan SG %s oif %s del",
589 vxlan_sg
->sg_str
, vxlan_sg
->term_oif
->name
);
591 vxlan_sg
->flags
&= ~PIM_VXLAN_SGF_OIF_INSTALLED
;
592 pim_ifchannel_local_membership_del(vxlan_sg
->term_oif
, &vxlan_sg
->sg
);
593 /* update the inherited OIL */
594 /* XXX - I don't see the inherited OIL updated when a local member
595 * is deleted. And that probably needs to be fixed. Till that happens
596 * we do a force update on the inherited OIL here.
598 pim_upstream_inherited_olist(vxlan_sg
->pim
, vxlan_sg
->up
);
601 static void pim_vxlan_update_sg_entry_mlag(struct pim_instance
*pim
,
602 struct pim_upstream
*up
, bool inherit
)
606 if (inherit
&& up
->parent
&&
607 PIM_UPSTREAM_FLAG_TEST_MLAG_VXLAN(up
->parent
->flags
) &&
608 PIM_UPSTREAM_FLAG_TEST_MLAG_NON_DF(up
->parent
->flags
))
611 pim_mlag_up_df_role_update(pim
, up
, is_df
, "inherit_xg_df");
614 /* We run MLAG DF election only on mroutes that have the termination
615 * device ipmr-lo in the immediate OIL. This is only (*, G) entries at the
616 * moment. For (S, G) entries that (with ipmr-lo in the inherited OIL) we
617 * inherit the DF role from the (*, G) entry.
619 void pim_vxlan_inherit_mlag_flags(struct pim_instance
*pim
,
620 struct pim_upstream
*up
, bool inherit
)
622 struct listnode
*listnode
;
623 struct pim_upstream
*child
;
625 for (ALL_LIST_ELEMENTS_RO(up
->sources
, listnode
,
627 pim_vxlan_update_sg_entry_mlag(pim
,
628 child
, true /* inherit */);
632 static void pim_vxlan_term_mr_up_add(struct pim_vxlan_sg
*vxlan_sg
)
634 struct pim_upstream
*up
;
643 zlog_debug("vxlan SG %s term mroute-up add",
646 PIM_UPSTREAM_FLAG_SET_SRC_VXLAN_TERM(flags
);
647 /* enable MLAG designated-forwarder election on termination mroutes */
648 PIM_UPSTREAM_FLAG_SET_MLAG_VXLAN(flags
);
650 up
= pim_upstream_add(vxlan_sg
->pim
, &vxlan_sg
->sg
, NULL
/* iif */,
651 flags
, __func__
, NULL
);
655 zlog_warn("vxlan SG %s term mroute-up add failed",
660 /* update existing SG entries with the parent's MLAG flag */
661 pim_vxlan_inherit_mlag_flags(vxlan_sg
->pim
, up
, true /*enable*/);
664 static void pim_vxlan_term_mr_up_del(struct pim_vxlan_sg
*vxlan_sg
)
666 struct pim_upstream
*up
= vxlan_sg
->up
;
672 zlog_debug("vxlan SG %s term mroute-up del",
675 if (up
->flags
& PIM_UPSTREAM_FLAG_MASK_SRC_VXLAN_TERM
) {
676 /* update SG entries that are inheriting from this XG entry */
677 pim_vxlan_inherit_mlag_flags(vxlan_sg
->pim
, up
,
679 /* clear out all the vxlan related flags */
680 up
->flags
&= ~(PIM_UPSTREAM_FLAG_MASK_SRC_VXLAN_TERM
|
681 PIM_UPSTREAM_FLAG_MASK_MLAG_VXLAN
);
682 pim_mlag_up_local_del(vxlan_sg
->pim
, up
);
683 pim_upstream_del(vxlan_sg
->pim
, up
, __func__
);
687 static void pim_vxlan_term_mr_add(struct pim_vxlan_sg
*vxlan_sg
)
690 zlog_debug("vxlan SG %s term mroute add", vxlan_sg
->sg_str
);
692 vxlan_sg
->term_oif
= vxlan_sg
->pim
->vxlan
.term_if
;
693 if (!vxlan_sg
->term_oif
)
694 /* defer termination mroute till we have a termination device */
697 pim_vxlan_term_mr_up_add(vxlan_sg
);
698 /* set up local membership for the term-oif */
699 pim_vxlan_term_mr_oif_add(vxlan_sg
);
702 static void pim_vxlan_term_mr_del(struct pim_vxlan_sg
*vxlan_sg
)
705 zlog_debug("vxlan SG %s term mroute del", vxlan_sg
->sg_str
);
707 /* remove local membership associated with the term oif */
708 pim_vxlan_term_mr_oif_del(vxlan_sg
);
709 /* remove references to the upstream entry */
710 pim_vxlan_term_mr_up_del(vxlan_sg
);
713 /************************** vxlan SG cache management ************************/
714 static unsigned int pim_vxlan_sg_hash_key_make(const void *p
)
716 const struct pim_vxlan_sg
*vxlan_sg
= p
;
718 return pim_sgaddr_hash(vxlan_sg
->sg
, 0);
721 static bool pim_vxlan_sg_hash_eq(const void *p1
, const void *p2
)
723 const struct pim_vxlan_sg
*sg1
= p1
;
724 const struct pim_vxlan_sg
*sg2
= p2
;
726 return !pim_sgaddr_cmp(sg1
->sg
, sg2
->sg
);
729 static struct pim_vxlan_sg
*pim_vxlan_sg_new(struct pim_instance
*pim
,
732 struct pim_vxlan_sg
*vxlan_sg
;
734 vxlan_sg
= XCALLOC(MTYPE_PIM_VXLAN_SG
, sizeof(*vxlan_sg
));
738 snprintfrr(vxlan_sg
->sg_str
, sizeof(vxlan_sg
->sg_str
), "%pSG", sg
);
741 zlog_debug("vxlan SG %s alloc", vxlan_sg
->sg_str
);
743 vxlan_sg
= hash_get(pim
->vxlan
.sg_hash
, vxlan_sg
, hash_alloc_intern
);
745 /* we register with the MLAG daemon in the first VxLAN SG and never
746 * de-register during that life of the pimd
748 if (pim
->vxlan
.sg_hash
->count
== 1) {
749 vxlan_mlag
.flags
|= PIM_VXLAN_MLAGF_DO_REG
;
756 struct pim_vxlan_sg
*pim_vxlan_sg_find(struct pim_instance
*pim
, pim_sgaddr
*sg
)
758 struct pim_vxlan_sg lookup
;
761 return hash_lookup(pim
->vxlan
.sg_hash
, &lookup
);
764 struct pim_vxlan_sg
*pim_vxlan_sg_add(struct pim_instance
*pim
, pim_sgaddr
*sg
)
766 struct pim_vxlan_sg
*vxlan_sg
;
768 vxlan_sg
= pim_vxlan_sg_find(pim
, sg
);
772 vxlan_sg
= pim_vxlan_sg_new(pim
, sg
);
774 if (pim_vxlan_is_orig_mroute(vxlan_sg
))
775 pim_vxlan_orig_mr_add(vxlan_sg
);
777 pim_vxlan_term_mr_add(vxlan_sg
);
782 static void pim_vxlan_sg_del_item(struct pim_vxlan_sg
*vxlan_sg
)
784 vxlan_sg
->flags
|= PIM_VXLAN_SGF_DEL_IN_PROG
;
786 pim_vxlan_del_work(vxlan_sg
);
788 if (pim_vxlan_is_orig_mroute(vxlan_sg
))
789 pim_vxlan_orig_mr_del(vxlan_sg
);
791 pim_vxlan_term_mr_del(vxlan_sg
);
794 zlog_debug("vxlan SG %s free", vxlan_sg
->sg_str
);
796 XFREE(MTYPE_PIM_VXLAN_SG
, vxlan_sg
);
799 void pim_vxlan_sg_del(struct pim_instance
*pim
, pim_sgaddr
*sg
)
801 struct pim_vxlan_sg
*vxlan_sg
;
803 vxlan_sg
= pim_vxlan_sg_find(pim
, sg
);
807 hash_release(pim
->vxlan
.sg_hash
, vxlan_sg
);
808 pim_vxlan_sg_del_item(vxlan_sg
);
811 /******************************* MLAG handling *******************************/
812 bool pim_vxlan_do_mlag_reg(void)
814 return (vxlan_mlag
.flags
& PIM_VXLAN_MLAGF_DO_REG
);
817 /* The peerlink sub-interface is added as an OIF to the origination-mroute.
818 * This is done to send a copy of the multicast-vxlan encapsulated traffic
819 * to the MLAG peer which may mroute it over the underlay if there are any
820 * interested receivers.
822 static void pim_vxlan_sg_peerlink_oif_update(struct hash_bucket
*bucket
,
825 struct interface
*new_oif
= (struct interface
*)arg
;
826 struct pim_vxlan_sg
*vxlan_sg
= (struct pim_vxlan_sg
*)bucket
->data
;
828 if (!pim_vxlan_is_orig_mroute(vxlan_sg
))
831 if (vxlan_sg
->orig_oif
== new_oif
)
834 pim_vxlan_orig_mr_oif_del(vxlan_sg
);
836 vxlan_sg
->orig_oif
= new_oif
;
837 pim_vxlan_orig_mr_oif_add(vxlan_sg
);
840 /* In the case of anycast VTEPs the VTEP-PIP must be used as the
843 bool pim_vxlan_get_register_src(struct pim_instance
*pim
,
844 struct pim_upstream
*up
, struct in_addr
*src_p
)
846 if (!(vxlan_mlag
.flags
& PIM_VXLAN_MLAGF_ENABLED
))
849 /* if address is not available suppress the pim-register */
850 if (vxlan_mlag
.reg_addr
.s_addr
== INADDR_ANY
)
853 *src_p
= vxlan_mlag
.reg_addr
;
857 void pim_vxlan_mlag_update(bool enable
, bool peer_state
, uint32_t role
,
858 struct interface
*peerlink_rif
,
859 struct in_addr
*reg_addr
)
861 struct pim_instance
*pim
;
862 char addr_buf
[INET_ADDRSTRLEN
];
863 struct pim_interface
*pim_ifp
= NULL
;
865 if (PIM_DEBUG_VXLAN
) {
866 inet_ntop(AF_INET
, reg_addr
,
867 addr_buf
, INET_ADDRSTRLEN
);
868 zlog_debug("vxlan MLAG update %s state %s role %d rif %s addr %s",
869 enable
? "enable" : "disable",
870 peer_state
? "up" : "down",
872 peerlink_rif
? peerlink_rif
->name
: "-",
876 /* XXX: for now vxlan termination is only possible in the default VRF
877 * when that changes this will need to change to iterate all VRFs
879 pim
= pim_get_pim_instance(VRF_DEFAULT
);
883 zlog_debug("%s: Unable to find pim instance", __func__
);
888 vxlan_mlag
.flags
|= PIM_VXLAN_MLAGF_ENABLED
;
890 vxlan_mlag
.flags
&= ~PIM_VXLAN_MLAGF_ENABLED
;
892 if (vxlan_mlag
.peerlink_rif
!= peerlink_rif
)
893 vxlan_mlag
.peerlink_rif
= peerlink_rif
;
895 vxlan_mlag
.reg_addr
= *reg_addr
;
896 vxlan_mlag
.peer_state
= peer_state
;
897 vxlan_mlag
.role
= role
;
899 /* process changes */
900 if (vxlan_mlag
.peerlink_rif
)
901 pim_ifp
= (struct pim_interface
*)vxlan_mlag
.peerlink_rif
->info
;
902 if ((vxlan_mlag
.flags
& PIM_VXLAN_MLAGF_ENABLED
) &&
903 pim_ifp
&& (pim_ifp
->mroute_vif_index
> 0))
904 pim_vxlan_set_peerlink_rif(pim
, peerlink_rif
);
906 pim_vxlan_set_peerlink_rif(pim
, NULL
);
909 /****************************** misc callbacks *******************************/
910 static void pim_vxlan_set_default_iif(struct pim_instance
*pim
,
911 struct interface
*ifp
)
913 struct interface
*old_iif
;
915 if (pim
->vxlan
.default_iif
== ifp
)
918 old_iif
= pim
->vxlan
.default_iif
;
920 zlog_debug("%s: vxlan default iif changed from %s to %s",
921 __func__
, old_iif
? old_iif
->name
: "-",
922 ifp
? ifp
->name
: "-");
924 old_iif
= pim_vxlan_orig_mr_iif_get(pim
);
925 pim
->vxlan
.default_iif
= ifp
;
926 ifp
= pim_vxlan_orig_mr_iif_get(pim
);
931 zlog_debug("%s: vxlan orig iif changed from %s to %s", __func__
,
932 old_iif
? old_iif
->name
: "-",
933 ifp
? ifp
->name
: "-");
935 /* add/del upstream entries for the existing vxlan SG when the
936 * interface becomes available
938 if (pim
->vxlan
.sg_hash
)
939 hash_iterate(pim
->vxlan
.sg_hash
,
940 pim_vxlan_orig_mr_iif_update
, NULL
);
943 static void pim_vxlan_up_cost_update(struct pim_instance
*pim
,
944 struct pim_upstream
*up
,
945 struct interface
*old_peerlink_rif
)
947 if (!PIM_UPSTREAM_FLAG_TEST_MLAG_VXLAN(up
->flags
))
950 if (up
->rpf
.source_nexthop
.interface
&&
951 ((up
->rpf
.source_nexthop
.interface
==
952 pim
->vxlan
.peerlink_rif
) ||
953 (up
->rpf
.source_nexthop
.interface
==
954 old_peerlink_rif
))) {
956 zlog_debug("RPF cost adjust for %s on peerlink-rif (old: %s, new: %s) change",
959 old_peerlink_rif
->name
: "-",
960 pim
->vxlan
.peerlink_rif
?
961 pim
->vxlan
.peerlink_rif
->name
: "-");
962 pim_mlag_up_local_add(pim
, up
);
966 static void pim_vxlan_term_mr_cost_update(struct hash_bucket
*bucket
, void *arg
)
968 struct interface
*old_peerlink_rif
= (struct interface
*)arg
;
969 struct pim_vxlan_sg
*vxlan_sg
= (struct pim_vxlan_sg
*)bucket
->data
;
970 struct pim_upstream
*up
;
971 struct listnode
*listnode
;
972 struct pim_upstream
*child
;
974 if (pim_vxlan_is_orig_mroute(vxlan_sg
))
977 /* Lookup all XG and SG entries with RPF-interface peerlink_rif */
982 pim_vxlan_up_cost_update(vxlan_sg
->pim
, up
,
985 for (ALL_LIST_ELEMENTS_RO(up
->sources
, listnode
,
987 pim_vxlan_up_cost_update(vxlan_sg
->pim
, child
,
991 static void pim_vxlan_sg_peerlink_rif_update(struct hash_bucket
*bucket
,
994 pim_vxlan_orig_mr_iif_update(bucket
, NULL
);
995 pim_vxlan_term_mr_cost_update(bucket
, arg
);
998 static void pim_vxlan_set_peerlink_rif(struct pim_instance
*pim
,
999 struct interface
*ifp
)
1001 struct interface
*old_iif
;
1002 struct interface
*new_iif
;
1003 struct interface
*old_oif
;
1004 struct interface
*new_oif
;
1006 if (pim
->vxlan
.peerlink_rif
== ifp
)
1009 old_iif
= pim
->vxlan
.peerlink_rif
;
1010 if (PIM_DEBUG_VXLAN
)
1011 zlog_debug("%s: vxlan peerlink_rif changed from %s to %s",
1012 __func__
, old_iif
? old_iif
->name
: "-",
1013 ifp
? ifp
->name
: "-");
1015 old_iif
= pim_vxlan_orig_mr_iif_get(pim
);
1016 old_oif
= pim_vxlan_orig_mr_oif_get(pim
);
1017 pim
->vxlan
.peerlink_rif
= ifp
;
1019 new_iif
= pim_vxlan_orig_mr_iif_get(pim
);
1020 if (old_iif
!= new_iif
) {
1021 if (PIM_DEBUG_VXLAN
)
1022 zlog_debug("%s: vxlan orig iif changed from %s to %s",
1023 __func__
, old_iif
? old_iif
->name
: "-",
1024 new_iif
? new_iif
->name
: "-");
1026 /* add/del upstream entries for the existing vxlan SG when the
1027 * interface becomes available
1029 if (pim
->vxlan
.sg_hash
)
1030 hash_iterate(pim
->vxlan
.sg_hash
,
1031 pim_vxlan_sg_peerlink_rif_update
,
1035 new_oif
= pim_vxlan_orig_mr_oif_get(pim
);
1036 if (old_oif
!= new_oif
) {
1037 if (PIM_DEBUG_VXLAN
)
1038 zlog_debug("%s: vxlan orig oif changed from %s to %s",
1039 __func__
, old_oif
? old_oif
->name
: "-",
1040 new_oif
? new_oif
->name
: "-");
1041 if (pim
->vxlan
.sg_hash
)
1042 hash_iterate(pim
->vxlan
.sg_hash
,
1043 pim_vxlan_sg_peerlink_oif_update
,
1048 static void pim_vxlan_term_mr_oif_update(struct hash_bucket
*bucket
, void *arg
)
1050 struct interface
*ifp
= (struct interface
*)arg
;
1051 struct pim_vxlan_sg
*vxlan_sg
= (struct pim_vxlan_sg
*)bucket
->data
;
1053 if (pim_vxlan_is_orig_mroute(vxlan_sg
))
1056 if (vxlan_sg
->term_oif
== ifp
)
1059 if (PIM_DEBUG_VXLAN
)
1060 zlog_debug("vxlan SG %s term oif changed from %s to %s",
1062 vxlan_sg
->term_oif
? vxlan_sg
->term_oif
->name
: "-",
1063 ifp
? ifp
->name
: "-");
1065 pim_vxlan_term_mr_del(vxlan_sg
);
1066 vxlan_sg
->term_oif
= ifp
;
1067 pim_vxlan_term_mr_add(vxlan_sg
);
1070 static void pim_vxlan_term_oif_update(struct pim_instance
*pim
,
1071 struct interface
*ifp
)
1073 if (pim
->vxlan
.term_if
== ifp
)
1076 if (PIM_DEBUG_VXLAN
)
1077 zlog_debug("vxlan term oif changed from %s to %s",
1078 pim
->vxlan
.term_if
? pim
->vxlan
.term_if
->name
: "-",
1079 ifp
? ifp
->name
: "-");
1081 pim
->vxlan
.term_if
= ifp
;
1082 if (pim
->vxlan
.sg_hash
)
1083 hash_iterate(pim
->vxlan
.sg_hash
,
1084 pim_vxlan_term_mr_oif_update
, ifp
);
1087 void pim_vxlan_add_vif(struct interface
*ifp
)
1089 struct pim_interface
*pim_ifp
= ifp
->info
;
1090 struct pim_instance
*pim
= pim_ifp
->pim
;
1092 if (pim
->vrf
->vrf_id
!= VRF_DEFAULT
)
1095 if (if_is_loopback(ifp
))
1096 pim_vxlan_set_default_iif(pim
, ifp
);
1098 if (vxlan_mlag
.flags
& PIM_VXLAN_MLAGF_ENABLED
&&
1099 (ifp
== vxlan_mlag
.peerlink_rif
))
1100 pim_vxlan_set_peerlink_rif(pim
, ifp
);
1102 if (pim
->vxlan
.term_if_cfg
== ifp
)
1103 pim_vxlan_term_oif_update(pim
, ifp
);
1106 void pim_vxlan_del_vif(struct interface
*ifp
)
1108 struct pim_interface
*pim_ifp
= ifp
->info
;
1109 struct pim_instance
*pim
= pim_ifp
->pim
;
1111 if (pim
->vrf
->vrf_id
!= VRF_DEFAULT
)
1114 if (pim
->vxlan
.default_iif
== ifp
)
1115 pim_vxlan_set_default_iif(pim
, NULL
);
1117 if (pim
->vxlan
.peerlink_rif
== ifp
)
1118 pim_vxlan_set_peerlink_rif(pim
, NULL
);
1120 if (pim
->vxlan
.term_if
== ifp
)
1121 pim_vxlan_term_oif_update(pim
, NULL
);
1124 /* enable pim implicitly on the termination device add */
1125 void pim_vxlan_add_term_dev(struct pim_instance
*pim
,
1126 struct interface
*ifp
)
1128 struct pim_interface
*pim_ifp
;
1130 if (pim
->vxlan
.term_if_cfg
== ifp
)
1133 if (PIM_DEBUG_VXLAN
)
1134 zlog_debug("vxlan term oif cfg changed from %s to %s",
1135 pim
->vxlan
.term_if_cfg
?
1136 pim
->vxlan
.term_if_cfg
->name
: "-",
1139 pim
->vxlan
.term_if_cfg
= ifp
;
1141 /* enable pim on the term ifp */
1142 pim_ifp
= (struct pim_interface
*)ifp
->info
;
1144 pim_ifp
->pim_enable
= true;
1145 /* ifp is already oper up; activate it as a term dev */
1146 if (pim_ifp
->mroute_vif_index
>= 0)
1147 pim_vxlan_term_oif_update(pim
, ifp
);
1149 /* ensure that pimreg exists before using the newly created
1150 * vxlan termination device
1152 pim_if_create_pimreg(pim
);
1153 (void)pim_if_new(ifp
, false /*igmp*/, true /*pim*/,
1154 false /*pimreg*/, true /*vxlan_term*/);
1158 /* disable pim implicitly, if needed, on the termination device deletion */
1159 void pim_vxlan_del_term_dev(struct pim_instance
*pim
)
1161 struct interface
*ifp
= pim
->vxlan
.term_if_cfg
;
1162 struct pim_interface
*pim_ifp
;
1164 if (PIM_DEBUG_VXLAN
)
1165 zlog_debug("vxlan term oif cfg changed from %s to -",
1168 pim
->vxlan
.term_if_cfg
= NULL
;
1170 pim_ifp
= (struct pim_interface
*)ifp
->info
;
1172 pim_ifp
->pim_enable
= false;
1173 if (!pim_ifp
->gm_enable
)
1178 void pim_vxlan_init(struct pim_instance
*pim
)
1182 snprintf(hash_name
, sizeof(hash_name
),
1183 "PIM %s vxlan SG hash", pim
->vrf
->name
);
1184 pim
->vxlan
.sg_hash
= hash_create(pim_vxlan_sg_hash_key_make
,
1185 pim_vxlan_sg_hash_eq
, hash_name
);
1188 void pim_vxlan_exit(struct pim_instance
*pim
)
1190 if (pim
->vxlan
.sg_hash
) {
1191 hash_clean(pim
->vxlan
.sg_hash
,
1192 (void (*)(void *))pim_vxlan_sg_del_item
);
1193 hash_free(pim
->vxlan
.sg_hash
);
1194 pim
->vxlan
.sg_hash
= NULL
;
1198 void pim_vxlan_terminate(void)
1200 pim_vxlan_work_timer_setup(false);