3 * Copyright 2009-2016, LabN Consulting, L.L.C.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * File: vnc_export_bgp.c
23 * Purpose: Export routes to BGP directly (not via zebra)
26 #include "lib/zebra.h"
27 #include "lib/prefix.h"
28 #include "lib/table.h"
31 #include "lib/stream.h"
32 #include "lib/memory.h"
33 #include "lib/linklist.h"
34 #include "lib/plist.h"
35 #include "lib/routemap.h"
36 #include "lib/lib_errors.h"
38 #include "bgpd/bgpd.h"
39 #include "bgpd/bgp_ecommunity.h"
40 #include "bgpd/bgp_attr.h"
41 #include "bgpd/bgp_aspath.h"
43 #include "bgpd/rfapi/vnc_export_bgp.h"
44 #include "bgpd/rfapi/vnc_export_bgp_p.h"
45 #include "bgpd/rfapi/vnc_export_table.h"
46 #include "bgpd/rfapi/bgp_rfapi_cfg.h"
47 #include "bgpd/rfapi/rfapi.h"
48 #include "bgpd/rfapi/rfapi_import.h"
49 #include "bgpd/rfapi/rfapi_private.h"
50 #include "bgpd/rfapi/rfapi_backend.h"
51 #include "bgpd/rfapi/rfapi_vty.h"
52 #include "bgpd/rfapi/vnc_debug.h"
55 static void vnc_direct_add_rn_group_rd(struct bgp
*bgp
,
56 struct rfapi_nve_group_cfg
*rfg
,
57 struct route_node
*rn
, struct attr
*attr
,
59 struct rfapi_descriptor
*irfd
);
61 /***********************************************************************
62 * Export methods that set nexthop to CE (from 5226 roo EC) BEGIN
63 ***********************************************************************/
66 * Memory allocation approach: make a ghost attr that
67 * has non-interned parts for the modifications. ghost attr
68 * memory is allocated by caller.
70 * - extract ce (=5226) EC and use as new nexthop
71 * - strip Tunnel Encap attr
74 static void encap_attr_export_ce(struct attr
*new, struct attr
*orig
,
75 struct prefix
*use_nexthop
)
78 * Make "new" a ghost attr copy of "orig"
80 memset(new, 0, sizeof(struct attr
));
81 bgp_attr_dup(new, orig
);
86 switch (use_nexthop
->family
) {
88 new->nexthop
= use_nexthop
->u
.prefix4
;
89 new->mp_nexthop_len
= 4; /* bytes */
90 new->flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
94 new->mp_nexthop_global
= use_nexthop
->u
.prefix6
;
95 new->mp_nexthop_len
= 16; /* bytes */
106 * Note that it will be deleted when BGP sends to any eBGP
107 * peer unless PEER_FLAG_MED_UNCHANGED is set:
109 * neighbor NEIGHBOR attribute-unchanged med
111 if (!CHECK_FLAG(new->flag
, BGP_ATTR_MULTI_EXIT_DISC
)) {
112 if (CHECK_FLAG(new->flag
, BGP_ATTR_LOCAL_PREF
)) {
113 if (new->local_pref
> 255)
116 new->med
= 255 - new->local_pref
;
118 new->med
= 255; /* shouldn't happen */
120 new->flag
|= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
);
124 * "new" is now a ghost attr:
125 * - it owns an "extra" struct
126 * - it owns any non-interned parts
127 * - any references to interned parts are not counted
129 * Caller should, after using the attr, call:
130 * - bgp_attr_flush() to free non-interned parts
134 static int getce(struct bgp
*bgp
, struct attr
*attr
, struct prefix
*pfx_ce
)
138 uint16_t localadmin
= bgp
->rfapi_cfg
->resolve_nve_roo_local_admin
;
140 for (ecp
= attr
->ecommunity
->val
, i
= 0; i
< attr
->ecommunity
->size
;
141 ++i
, ecp
+= ECOMMUNITY_SIZE
) {
143 if (VNC_DEBUG(EXPORT_BGP_GETCE
)) {
145 "%s: %02x %02x %02x %02x %02x %02x %02x %02x",
146 __func__
, ecp
[0], ecp
[1], ecp
[2], ecp
[3],
147 ecp
[4], ecp
[5], ecp
[6], ecp
[7]);
153 if (ecp
[0] != 1 || ecp
[1] != 3) {
158 * Match local admin value?
160 if (ecp
[6] != ((localadmin
& 0xff00) >> 8)
161 || ecp
[7] != (localadmin
& 0xff))
164 memset((uint8_t *)pfx_ce
, 0, sizeof(*pfx_ce
));
165 memcpy(&pfx_ce
->u
.prefix4
, ecp
+ 2, 4);
166 pfx_ce
->family
= AF_INET
;
167 pfx_ce
->prefixlen
= 32;
175 void vnc_direct_bgp_add_route_ce(struct bgp
*bgp
, struct route_node
*rn
,
178 struct attr
*attr
= bi
->attr
;
179 struct peer
*peer
= bi
->peer
;
180 struct prefix
*prefix
= &rn
->p
;
181 afi_t afi
= family2afi(prefix
->family
);
182 struct bgp_node
*urn
;
183 struct bgp_info
*ubi
;
186 struct prefix ce_nexthop
;
187 struct prefix post_routemap_nexthop
;
191 flog_err(LIB_ERR_DEVELOPMENT
,
192 "%s: can't get afi of route node", __func__
);
196 if ((bi
->type
!= ZEBRA_ROUTE_BGP
)
197 || (bi
->sub_type
!= BGP_ROUTE_NORMAL
198 && bi
->sub_type
!= BGP_ROUTE_RFP
199 && bi
->sub_type
!= BGP_ROUTE_STATIC
)) {
201 vnc_zlog_debug_verbose(
202 "%s: wrong route type/sub_type for export, skipping",
207 /* check bgp redist flag for vnc direct ("vpn") routes */
208 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
209 vnc_zlog_debug_verbose(
210 "%s: bgp redistribution of VNC direct routes is off",
215 if (!bgp
->rfapi_cfg
) {
216 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
221 if (!VNC_EXPORT_BGP_CE_ENABLED(bgp
->rfapi_cfg
)) {
222 vnc_zlog_debug_verbose(
223 "%s: export-to-bgp ce mode not enabled, skipping",
231 if (bgp
->rfapi_cfg
->plist_export_bgp
[afi
]) {
232 if (prefix_list_apply(bgp
->rfapi_cfg
->plist_export_bgp
[afi
],
235 vnc_zlog_debug_verbose(
236 "%s: prefix list denied, skipping", __func__
);
244 * This works only for IPv4 because IPv6 addresses are too big
245 * to fit in an extended community
247 if (getce(bgp
, attr
, &ce_nexthop
)) {
248 vnc_zlog_debug_verbose("%s: EC has no encoded CE, skipping",
254 * Is this route already represented in the unicast RIB?
255 * (look up prefix; compare route type, sub_type, peer, nexthop)
257 urn
= bgp_afi_node_get(bgp
->rib
[afi
][SAFI_UNICAST
], afi
, SAFI_UNICAST
,
259 for (ubi
= urn
->info
; ubi
; ubi
= ubi
->next
) {
260 struct prefix unicast_nexthop
;
262 if (CHECK_FLAG(ubi
->flags
, BGP_INFO_REMOVED
))
265 rfapiUnicastNexthop2Prefix(afi
, ubi
->attr
, &unicast_nexthop
);
267 if (ubi
->type
== ZEBRA_ROUTE_VNC_DIRECT
268 && ubi
->sub_type
== BGP_ROUTE_REDISTRIBUTE
270 && prefix_same(&unicast_nexthop
, &ce_nexthop
)) {
272 vnc_zlog_debug_verbose(
273 "%s: already have matching exported unicast route, skipping",
280 * Construct new attribute set with CE addr as
281 * nexthop and without Tunnel Encap attr
283 encap_attr_export_ce(&hattr
, attr
, &ce_nexthop
);
284 if (bgp
->rfapi_cfg
->routemap_export_bgp
) {
285 struct bgp_info info
;
286 route_map_result_t ret
;
288 memset(&info
, 0, sizeof(info
));
291 ret
= route_map_apply(bgp
->rfapi_cfg
->routemap_export_bgp
,
292 prefix
, RMAP_BGP
, &info
);
293 if (ret
== RMAP_DENYMATCH
) {
294 bgp_attr_flush(&hattr
);
299 iattr
= bgp_attr_intern(&hattr
);
300 bgp_attr_flush(&hattr
);
303 * Rule: disallow route-map alteration of next-hop, because it
304 * would make it too difficult to keep track of the correspondence
305 * between VPN routes and unicast routes.
307 rfapiUnicastNexthop2Prefix(afi
, iattr
, &post_routemap_nexthop
);
309 if (!prefix_same(&ce_nexthop
, &post_routemap_nexthop
)) {
310 vnc_zlog_debug_verbose(
311 "%s: route-map modification of nexthop not allowed, skipping",
313 bgp_attr_unintern(&iattr
);
317 bgp_update(peer
, prefix
, 0, /* addpath_id */
318 iattr
, /* bgp_update copies this attr */
319 afi
, SAFI_UNICAST
, ZEBRA_ROUTE_VNC_DIRECT
,
320 BGP_ROUTE_REDISTRIBUTE
, NULL
, /* RD not used for unicast */
321 NULL
, 0, /* tag not used for unicast */
322 0, NULL
); /* EVPN not used */
323 bgp_attr_unintern(&iattr
);
328 * "Withdrawing a Route" export process
330 void vnc_direct_bgp_del_route_ce(struct bgp
*bgp
, struct route_node
*rn
,
333 afi_t afi
= family2afi(rn
->p
.family
);
334 struct bgp_info
*vbi
;
335 struct prefix ce_nexthop
;
338 flog_err(LIB_ERR_DEVELOPMENT
, "%s: bad afi", __func__
);
342 /* check bgp redist flag for vnc direct ("vpn") routes */
343 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
344 vnc_zlog_debug_verbose(
345 "%s: bgp redistribution of VNC direct routes is off",
350 if (!bgp
->rfapi_cfg
) {
351 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
355 if (!VNC_EXPORT_BGP_CE_ENABLED(bgp
->rfapi_cfg
)) {
356 vnc_zlog_debug_verbose(
357 "%s: export-to-bgp ce mode not enabled, skipping",
364 * This works only for IPv4 because IPv6 addresses are too big
365 * to fit in an extended community
367 if (getce(bgp
, bi
->attr
, &ce_nexthop
)) {
368 vnc_zlog_debug_verbose("%s: EC has no encoded CE, skipping",
374 * Look for other VPN routes with same prefix, same 5226 CE,
375 * same peer. If at least one is present, don't remove the
376 * route from the unicast RIB
379 for (vbi
= rn
->info
; vbi
; vbi
= vbi
->next
) {
383 if (bi
->peer
!= vbi
->peer
)
385 if (getce(bgp
, vbi
->attr
, &ce
))
387 if (prefix_same(&ce
, &ce_nexthop
)) {
388 vnc_zlog_debug_verbose(
389 "%s: still have a route via CE, not deleting unicast",
398 bgp_withdraw(bi
->peer
, &rn
->p
, 0, /* addpath_id */
399 NULL
, /* attr, ignored */
400 afi
, SAFI_UNICAST
, ZEBRA_ROUTE_VNC_DIRECT
,
401 BGP_ROUTE_REDISTRIBUTE
, NULL
, /* RD not used for unicast */
402 NULL
, 0, NULL
); /* tag not used for unicast */
405 static void vnc_direct_bgp_vpn_enable_ce(struct bgp
*bgp
, afi_t afi
)
407 struct route_node
*rn
;
410 vnc_zlog_debug_verbose("%s: entry, afi=%d", __func__
, afi
);
415 if (!(bgp
->rfapi_cfg
))
418 if (!VNC_EXPORT_BGP_CE_ENABLED(bgp
->rfapi_cfg
)) {
419 vnc_zlog_debug_verbose(
420 "%s: export of CE routes not enabled, skipping",
425 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
) {
426 vnc_zlog_debug_verbose("%s: bad afi: %d", __func__
, afi
);
431 * Go through entire ce import table and export to BGP unicast.
433 for (rn
= route_top(bgp
->rfapi
->it_ce
->imported_vpn
[afi
]); rn
;
434 rn
= route_next(rn
)) {
440 char prefixstr
[PREFIX_STRLEN
];
442 prefix2str(&rn
->p
, prefixstr
, sizeof(prefixstr
));
443 vnc_zlog_debug_verbose("%s: checking prefix %s",
444 __func__
, prefixstr
);
447 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
449 vnc_zlog_debug_verbose("%s: ri->sub_type: %d", __func__
,
452 if (ri
->sub_type
== BGP_ROUTE_NORMAL
453 || ri
->sub_type
== BGP_ROUTE_RFP
454 || ri
->sub_type
== BGP_ROUTE_STATIC
) {
456 vnc_direct_bgp_add_route_ce(bgp
, rn
, ri
);
462 static void vnc_direct_bgp_vpn_disable_ce(struct bgp
*bgp
, afi_t afi
)
466 vnc_zlog_debug_verbose("%s: entry, afi=%d", __func__
, afi
);
471 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
) {
472 vnc_zlog_debug_verbose("%s: bad afi: %d", __func__
, afi
);
477 * Go through the entire BGP unicast table and remove routes that
480 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
;
481 rn
= bgp_route_next(rn
)) {
484 struct bgp_info
*next
;
486 for (ri
= rn
->info
, next
= NULL
; ri
; ri
= next
) {
490 if (ri
->type
== ZEBRA_ROUTE_VNC_DIRECT
491 && ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
) {
494 ri
->peer
, &rn
->p
, /* prefix */
497 AFI_IP
, SAFI_UNICAST
,
498 ZEBRA_ROUTE_VNC_DIRECT
,
499 BGP_ROUTE_REDISTRIBUTE
,
500 NULL
, /* RD not used for unicast */
502 NULL
); /* tag not used for unicast */
508 /***********************************************************************
509 * Export methods that set nexthop to CE (from 5226 roo EC) END
510 ***********************************************************************/
512 /***********************************************************************
513 * Export methods that proxy nexthop BEGIN
514 ***********************************************************************/
516 static struct ecommunity
*vnc_route_origin_ecom(struct route_node
*rn
)
518 struct ecommunity
*new;
524 new = ecommunity_new();
526 for (bi
= rn
->info
; bi
; bi
= bi
->next
) {
528 struct ecommunity_val roec
;
530 switch (BGP_MP_NEXTHOP_FAMILY(bi
->attr
->mp_nexthop_len
)) {
532 memset(&roec
, 0, sizeof(roec
));
536 &bi
->attr
->mp_nexthop_global_in
.s_addr
, 4);
539 ecommunity_add_val(new, &roec
);
542 /* No support for IPv6 addresses in extended communities
549 ecommunity_free(&new);
556 static struct ecommunity
*vnc_route_origin_ecom_single(struct in_addr
*origin
)
558 struct ecommunity
*new;
559 struct ecommunity_val roec
;
561 memset(&roec
, 0, sizeof(roec
));
564 memcpy(roec
.val
+ 2, &origin
->s_addr
, 4);
568 new = ecommunity_new();
570 ecommunity_add_val(new, &roec
);
573 ecommunity_free(&new);
582 * New memory allocation approach: make a ghost attr that
583 * has non-interned parts for the modifications. ghost attr
584 * memory is allocated by caller.
587 encap_attr_export(struct attr
*new, struct attr
*orig
,
588 struct prefix
*new_nexthop
,
589 struct route_node
*rn
) /* for VN addrs for ecom list */
590 /* if rn is 0, use route's nexthop */
592 struct prefix orig_nexthop
;
593 struct prefix
*use_nexthop
;
594 static struct ecommunity
*ecom_ro
;
597 use_nexthop
= new_nexthop
;
599 use_nexthop
= &orig_nexthop
;
600 orig_nexthop
.family
=
601 BGP_MP_NEXTHOP_FAMILY(orig
->mp_nexthop_len
);
602 if (orig_nexthop
.family
== AF_INET
) {
603 orig_nexthop
.prefixlen
= 32;
604 orig_nexthop
.u
.prefix4
= orig
->mp_nexthop_global_in
;
605 } else if (orig_nexthop
.family
== AF_INET6
) {
606 orig_nexthop
.prefixlen
= 128;
607 orig_nexthop
.u
.prefix6
= orig
->mp_nexthop_global
;
609 return -1; /* FAIL - can't compute nexthop */
615 * Make "new" a ghost attr copy of "orig"
617 memset(new, 0, sizeof(struct attr
));
618 bgp_attr_dup(new, orig
);
623 switch (use_nexthop
->family
) {
625 new->nexthop
= use_nexthop
->u
.prefix4
;
626 new->mp_nexthop_len
= 4; /* bytes */
627 new->flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
631 new->mp_nexthop_global
= use_nexthop
->u
.prefix6
;
632 new->mp_nexthop_len
= 16; /* bytes */
641 ecom_ro
= vnc_route_origin_ecom(rn
);
643 /* TBD use lcom for IPv6 */
644 ecom_ro
= vnc_route_origin_ecom_single(&use_nexthop
->u
.prefix4
);
646 if (new->ecommunity
) {
649 ecommunity_merge(ecom_ro
, new->ecommunity
);
651 new->ecommunity
= ecom_ro
;
654 new->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
660 * Note that it will be deleted when BGP sends to any eBGP
661 * peer unless PEER_FLAG_MED_UNCHANGED is set:
663 * neighbor NEIGHBOR attribute-unchanged med
665 if (!CHECK_FLAG(new->flag
, BGP_ATTR_MULTI_EXIT_DISC
)) {
666 if (CHECK_FLAG(new->flag
, BGP_ATTR_LOCAL_PREF
)) {
667 if (new->local_pref
> 255)
670 new->med
= 255 - new->local_pref
;
672 new->med
= 255; /* shouldn't happen */
674 new->flag
|= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
);
678 * "new" is now a ghost attr:
679 * - it owns an "extra" struct
680 * - it owns any non-interned parts
681 * - any references to interned parts are not counted
683 * Caller should, after using the attr, call:
684 * - bgp_attr_flush() to free non-interned parts
691 * "Adding a Route" export process
693 void vnc_direct_bgp_add_prefix(struct bgp
*bgp
,
694 struct rfapi_import_table
*import_table
,
695 struct route_node
*rn
)
697 struct attr attr
= {0};
698 struct listnode
*node
, *nnode
;
699 struct rfapi_rfg_name
*rfgn
;
700 afi_t afi
= family2afi(rn
->p
.family
);
703 flog_err(LIB_ERR_DEVELOPMENT
,
704 "%s: can't get afi of route node", __func__
);
708 /* check bgp redist flag for vnc direct ("vpn") routes */
709 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
710 vnc_zlog_debug_verbose(
711 "%s: bgp redistribution of VNC direct routes is off",
716 if (!bgp
->rfapi_cfg
) {
717 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
722 if (!VNC_EXPORT_BGP_GRP_ENABLED(bgp
->rfapi_cfg
)) {
723 vnc_zlog_debug_verbose(
724 "%s: export-to-bgp group mode not enabled, skipping",
729 if (!listcount(bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
)) {
730 vnc_zlog_debug_verbose(
731 "%s: no bgp-direct export nve group, skipping",
736 bgp_attr_default_set(&attr
, BGP_ORIGIN_INCOMPLETE
);
737 /* TBD set some configured med, see add_vnc_route() */
739 vnc_zlog_debug_verbose(
740 "%s: looping over nve-groups in direct-bgp export list",
743 for (ALL_LIST_ELEMENTS(bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
, node
,
749 * If nve group is not defined yet, skip it
755 * If the nve group uses a different import table, skip it
757 if (import_table
!= rfgn
->rfg
->rfapi_import_table
)
761 * if no NVEs currently associated with this group, skip it
763 if (rfgn
->rfg
->type
!= RFAPI_GROUP_CFG_VRF
&& !rfgn
->rfg
->nves
)
767 * per-nve-group prefix list check
769 if (rfgn
->rfg
->plist_export_bgp
[afi
]) {
770 if (prefix_list_apply(rfgn
->rfg
->plist_export_bgp
[afi
],
777 if (rfgn
->rfg
->type
== RFAPI_GROUP_CFG_VRF
) {
778 vnc_direct_add_rn_group_rd(bgp
, rfgn
->rfg
, rn
, &attr
,
779 afi
, rfgn
->rfg
->rfd
);
782 * - but consistent with rest of function
787 * For each NVE that is assigned to the export nve group,
789 * a route with that NVE as its next hop
791 for (ln
= listhead(rfgn
->rfg
->nves
); ln
;
792 ln
= listnextnode(ln
)) {
793 vnc_direct_add_rn_group_rd(bgp
, rfgn
->rfg
, rn
, &attr
,
794 afi
, listgetdata(ln
));
798 aspath_unintern(&attr
.aspath
);
802 * "Withdrawing a Route" export process
804 void vnc_direct_bgp_del_prefix(struct bgp
*bgp
,
805 struct rfapi_import_table
*import_table
,
806 struct route_node
*rn
)
808 struct listnode
*node
, *nnode
;
809 struct rfapi_rfg_name
*rfgn
;
810 afi_t afi
= family2afi(rn
->p
.family
);
813 flog_err(LIB_ERR_DEVELOPMENT
, "%s: can't get afi route node",
818 /* check bgp redist flag for vnc direct ("vpn") routes */
819 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
820 vnc_zlog_debug_verbose(
821 "%s: bgp redistribution of VNC direct routes is off",
826 if (!bgp
->rfapi_cfg
) {
827 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
832 if (!VNC_EXPORT_BGP_GRP_ENABLED(bgp
->rfapi_cfg
)) {
833 vnc_zlog_debug_verbose(
834 "%s: export-to-bgp group mode not enabled, skipping",
839 if (!listcount(bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
)) {
840 vnc_zlog_debug_verbose(
841 "%s: no bgp-direct export nve group, skipping",
846 for (ALL_LIST_ELEMENTS(bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
, node
,
852 * If nve group is not defined yet, skip it
858 * if no NVEs currently associated with this group, skip it
860 if (rfgn
->rfg
->type
!= RFAPI_GROUP_CFG_VRF
&& !rfgn
->rfg
->nves
)
864 * If the nve group uses a different import table,
867 if (import_table
!= rfgn
->rfg
->rfapi_import_table
)
870 if (rfgn
->rfg
->type
== RFAPI_GROUP_CFG_VRF
) {
872 struct rfapi_descriptor
*irfd
;
874 irfd
= rfgn
->rfg
->rfd
;
876 if (rfapiRaddr2Qprefix(&irfd
->vn_addr
, &nhp
))
879 bgp_withdraw(irfd
->peer
, &rn
->p
, /* prefix */
881 NULL
, /* attr, ignored */
882 afi
, SAFI_UNICAST
, ZEBRA_ROUTE_VNC_DIRECT
,
883 BGP_ROUTE_REDISTRIBUTE
,
884 NULL
, /* RD not used for unicast */
886 NULL
); /* tag not used for unicast */
889 * - but consistent with rest of function
894 * For each NVE that is assigned to the export nve group,
896 * a route with that NVE as its next hop
898 for (ln
= listhead(rfgn
->rfg
->nves
); ln
;
899 ln
= listnextnode(ln
)) {
902 struct rfapi_descriptor
*irfd
;
904 irfd
= listgetdata(ln
);
906 if (rfapiRaddr2Qprefix(&irfd
->vn_addr
, &nhp
))
909 bgp_withdraw(irfd
->peer
, &rn
->p
, /* prefix */
911 NULL
, /* attr, ignored */
912 afi
, SAFI_UNICAST
, ZEBRA_ROUTE_VNC_DIRECT
,
913 BGP_ROUTE_REDISTRIBUTE
,
914 NULL
, /* RD not used for unicast */
916 NULL
); /* tag not used for unicast */
921 void vnc_direct_bgp_add_nve(struct bgp
*bgp
, struct rfapi_descriptor
*rfd
)
923 struct listnode
*node
, *nnode
;
924 struct rfapi_rfg_name
*rfgn
;
925 struct rfapi_nve_group_cfg
*rfg
= rfd
->rfg
;
926 afi_t afi
= family2afi(rfd
->vn_addr
.addr_family
);
929 flog_err(LIB_ERR_DEVELOPMENT
,
930 "%s: can't get afi of nve vn addr", __func__
);
936 if (!bgp
->rfapi_cfg
) {
937 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
941 if (!VNC_EXPORT_BGP_GRP_ENABLED(bgp
->rfapi_cfg
)) {
942 vnc_zlog_debug_verbose(
943 "%s: export-to-bgp group mode not enabled, skipping",
948 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
949 vnc_zlog_debug_verbose(
950 "%s: bgp redistribution of VNC direct routes is off",
956 * Loop over the list of NVE-Groups configured for
957 * exporting to direct-bgp and see if this new NVE's
958 * group is among them.
960 for (ALL_LIST_ELEMENTS(bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
, node
,
964 * Yes, this NVE's group is configured for export to direct-bgp
966 if (rfgn
->rfg
== rfg
) {
968 struct route_table
*rt
= NULL
;
969 struct route_node
*rn
;
970 struct attr attr
= {0};
971 struct rfapi_import_table
*import_table
;
974 import_table
= rfg
->rfapi_import_table
;
976 bgp_attr_default_set(&attr
, BGP_ORIGIN_INCOMPLETE
);
977 /* TBD set some configured med, see add_vnc_route() */
979 if (afi
== AFI_IP
|| afi
== AFI_IP6
) {
980 rt
= import_table
->imported_vpn
[afi
];
982 flog_err(LIB_ERR_DEVELOPMENT
, "%s: bad afi %d",
988 * Walk the NVE-Group's VNC Import table
990 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
)) {
995 struct rfapi_descriptor
*irfd
= rfd
;
998 struct bgp_info info
;
1000 if (rfapiRaddr2Qprefix(&irfd
->vn_addr
,
1005 * per-nve-group prefix list check
1007 if (rfgn
->rfg
->plist_export_bgp
[afi
]) {
1008 if (prefix_list_apply(
1009 rfgn
->rfg
->plist_export_bgp
1019 * Construct new attribute set with
1021 * nexthop and without Tunnel Encap attr
1023 if (encap_attr_export(&hattr
, &attr
,
1027 if (rfgn
->rfg
->routemap_export_bgp
) {
1028 route_map_result_t ret
;
1029 info
.peer
= irfd
->peer
;
1031 ret
= route_map_apply(
1033 ->routemap_export_bgp
,
1036 if (ret
== RMAP_DENYMATCH
) {
1037 bgp_attr_flush(&hattr
);
1042 iattr
= bgp_attr_intern(&hattr
);
1043 bgp_attr_flush(&hattr
);
1045 irfd
->peer
, &rn
->p
, /* prefix */
1047 iattr
, /* bgp_update copies
1050 ZEBRA_ROUTE_VNC_DIRECT
,
1051 BGP_ROUTE_REDISTRIBUTE
, NULL
,
1052 /* RD not used for unicast */
1054 /* tag not used for unicast */
1055 0, 0, NULL
); /* EVPN not used */
1057 bgp_attr_unintern(&iattr
);
1061 aspath_unintern(&attr
.aspath
);
1067 void vnc_direct_bgp_del_nve(struct bgp
*bgp
, struct rfapi_descriptor
*rfd
)
1069 struct listnode
*node
, *nnode
;
1070 struct rfapi_rfg_name
*rfgn
;
1071 struct rfapi_nve_group_cfg
*rfg
= rfd
->rfg
;
1072 afi_t afi
= family2afi(rfd
->vn_addr
.addr_family
);
1075 flog_err(LIB_ERR_DEVELOPMENT
,
1076 "%s: can't get afi of nve vn addr", __func__
);
1082 if (!bgp
->rfapi_cfg
) {
1083 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
1087 if (!VNC_EXPORT_BGP_GRP_ENABLED(bgp
->rfapi_cfg
)) {
1088 vnc_zlog_debug_verbose(
1089 "%s: export-to-bgp group mode not enabled, skipping",
1094 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
1095 vnc_zlog_debug_verbose(
1096 "%s: bgp redistribution of VNC direct routes is off",
1102 * Loop over the list of NVE-Groups configured for
1103 * exporting to direct-bgp and see if this new NVE's
1104 * group is among them.
1106 for (ALL_LIST_ELEMENTS(bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
, node
,
1110 * Yes, this NVE's group is configured for export to direct-bgp
1112 if (rfg
&& rfgn
->rfg
== rfg
) {
1114 struct route_table
*rt
= NULL
;
1115 struct route_node
*rn
;
1116 struct rfapi_import_table
*import_table
;
1118 import_table
= rfg
->rfapi_import_table
;
1120 if (afi
== AFI_IP
|| afi
== AFI_IP6
) {
1121 rt
= import_table
->imported_vpn
[afi
];
1123 flog_err(LIB_ERR_DEVELOPMENT
, "%s: bad afi %d",
1129 * Walk the NVE-Group's VNC Import table
1131 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
)) {
1136 struct rfapi_descriptor
*irfd
= rfd
;
1138 if (rfapiRaddr2Qprefix(&irfd
->vn_addr
,
1142 bgp_withdraw(irfd
->peer
,
1143 &rn
->p
, /* prefix */
1145 NULL
, /* attr, ignored */
1147 ZEBRA_ROUTE_VNC_DIRECT
,
1148 BGP_ROUTE_REDISTRIBUTE
,
1149 NULL
, /* RD not used for
1151 NULL
, 0, NULL
); /* tag not
1160 static void vnc_direct_add_rn_group_rd(struct bgp
*bgp
,
1161 struct rfapi_nve_group_cfg
*rfg
,
1162 struct route_node
*rn
, struct attr
*attr
,
1163 afi_t afi
, struct rfapi_descriptor
*irfd
)
1166 struct bgp_info info
;
1170 if (irfd
== NULL
&& rfg
->type
!= RFAPI_GROUP_CFG_VRF
) {
1171 /* need new rfapi_handle, for peer strcture
1172 * -- based on vnc_add_vrf_prefi */
1173 assert(rfg
->rfd
== NULL
);
1175 if (!rfg
->rt_export_list
|| !rfg
->rfapi_import_table
) {
1176 vnc_zlog_debug_verbose(
1177 "%s: VRF \"%s\" is missing RT import/export configuration.\n",
1178 __func__
, rfg
->name
);
1181 if (!rfg
->rd
.prefixlen
) {
1182 vnc_zlog_debug_verbose(
1183 "%s: VRF \"%s\" is missing RD configuration.\n",
1184 __func__
, rfg
->name
);
1187 if (rfg
->label
> MPLS_LABEL_MAX
) {
1188 vnc_zlog_debug_verbose(
1189 "%s: VRF \"%s\" is missing defaul label configuration.\n",
1190 __func__
, rfg
->name
);
1194 irfd
= XCALLOC(MTYPE_RFAPI_DESC
,
1195 sizeof(struct rfapi_descriptor
));
1199 * leave most fields empty as will get from (dynamic) config
1202 irfd
->default_tunneltype_option
.type
= BGP_ENCAP_TYPE_MPLS
;
1204 if (rfg
->vn_prefix
.family
1205 && !CHECK_FLAG(rfg
->flags
, RFAPI_RFG_VPN_NH_SELF
)) {
1206 rfapiQprefix2Raddr(&rfg
->vn_prefix
, &irfd
->vn_addr
);
1208 memset(&irfd
->vn_addr
, 0, sizeof(struct rfapi_ip_addr
));
1209 irfd
->vn_addr
.addr_family
= AF_INET
;
1210 irfd
->vn_addr
.addr
.v4
= bgp
->router_id
;
1212 irfd
->un_addr
= irfd
->vn_addr
; /* sigh, need something in UN for
1214 vnc_zlog_debug_verbose("%s: Opening RFD for VRF %s", __func__
,
1216 rfapi_init_and_open(bgp
, irfd
, rfg
);
1219 if (irfd
== NULL
|| rfapiRaddr2Qprefix(&irfd
->vn_addr
, &nhp
))
1223 * Construct new attribute set with NVE's VN
1225 * nexthop and without Tunnel Encap attr
1227 if (encap_attr_export(&hattr
, attr
, &nhp
, rn
))
1230 if (VNC_DEBUG(EXPORT_BGP_DIRECT_ADD
)) {
1231 vnc_zlog_debug_any("%s: attr follows", __func__
);
1232 rfapiPrintAttrPtrs(NULL
, attr
);
1233 vnc_zlog_debug_any("%s: hattr follows", __func__
);
1234 rfapiPrintAttrPtrs(NULL
, &hattr
);
1237 if (rfg
->routemap_export_bgp
) {
1238 route_map_result_t ret
;
1240 info
.peer
= irfd
->peer
;
1242 ret
= route_map_apply(rfg
->routemap_export_bgp
, &rn
->p
,
1244 if (ret
== RMAP_DENYMATCH
) {
1245 bgp_attr_flush(&hattr
);
1246 vnc_zlog_debug_verbose(
1247 "%s: route map says DENY, so not calling bgp_update",
1253 if (VNC_DEBUG(EXPORT_BGP_DIRECT_ADD
)) {
1254 vnc_zlog_debug_any("%s: hattr after route_map_apply:",
1256 rfapiPrintAttrPtrs(NULL
, &hattr
);
1258 iattr
= bgp_attr_intern(&hattr
);
1259 bgp_attr_flush(&hattr
);
1261 bgp_update(irfd
->peer
, &rn
->p
, /* prefix */
1263 iattr
, /* bgp_update copies it */
1264 afi
, SAFI_UNICAST
, ZEBRA_ROUTE_VNC_DIRECT
,
1265 BGP_ROUTE_REDISTRIBUTE
, NULL
, /* RD not used for unicast */
1266 NULL
, /* tag not used for unicast */
1267 0, 0, NULL
); /* EVPN not used */
1269 bgp_attr_unintern(&iattr
);
1275 * Caller is responsible for ensuring that the specified nve-group
1276 * is actually part of the list of exported nve groups.
1278 static void vnc_direct_bgp_add_group_afi(struct bgp
*bgp
,
1279 struct rfapi_nve_group_cfg
*rfg
,
1282 struct route_table
*rt
= NULL
;
1283 struct route_node
*rn
;
1284 struct attr attr
= {0};
1285 struct rfapi_import_table
*import_table
;
1287 vnc_zlog_debug_verbose("%s: entry", __func__
);
1289 import_table
= rfg
->rfapi_import_table
;
1290 if (!import_table
) {
1291 vnc_zlog_debug_verbose(
1292 "%s: import table not defined, returning", __func__
);
1296 if (afi
== AFI_IP
|| afi
== AFI_IP6
) {
1297 rt
= import_table
->imported_vpn
[afi
];
1299 flog_err(LIB_ERR_DEVELOPMENT
, "%s: bad afi %d", __func__
, afi
);
1303 if (!rfg
->nves
&& rfg
->type
!= RFAPI_GROUP_CFG_VRF
) {
1304 vnc_zlog_debug_verbose("%s: no NVEs in this group", __func__
);
1308 bgp_attr_default_set(&attr
, BGP_ORIGIN_INCOMPLETE
);
1309 /* TBD set some configured med, see add_vnc_route() */
1312 * Walk the NVE-Group's VNC Import table
1314 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
)) {
1318 struct listnode
*ln
;
1321 * per-nve-group prefix list check
1323 if (rfg
->plist_export_bgp
[afi
]) {
1324 if (prefix_list_apply(
1325 rfg
->plist_export_bgp
[afi
], &rn
->p
)
1330 if (rfg
->type
== RFAPI_GROUP_CFG_VRF
) {
1331 vnc_direct_add_rn_group_rd(bgp
, rfg
, rn
, &attr
,
1335 * - but consistent with rest of function
1340 * For each NVE that is assigned to the export nve
1342 * a route with that NVE as its next hop
1344 for (ln
= listhead(rfg
->nves
); ln
;
1345 ln
= listnextnode(ln
)) {
1346 vnc_direct_add_rn_group_rd(bgp
, rfg
, rn
, &attr
,
1353 aspath_unintern(&attr
.aspath
);
1358 * Caller is responsible for ensuring that the specified nve-group
1359 * is actually part of the list of exported nve groups.
1361 void vnc_direct_bgp_add_group(struct bgp
*bgp
, struct rfapi_nve_group_cfg
*rfg
)
1363 vnc_direct_bgp_add_group_afi(bgp
, rfg
, AFI_IP
);
1364 vnc_direct_bgp_add_group_afi(bgp
, rfg
, AFI_IP6
);
1367 static void vnc_direct_del_rn_group_rd(struct bgp
*bgp
,
1368 struct rfapi_nve_group_cfg
*rfg
,
1369 struct route_node
*rn
, afi_t afi
,
1370 struct rfapi_descriptor
*irfd
)
1374 bgp_withdraw(irfd
->peer
, &rn
->p
, /* prefix */
1376 NULL
, /* attr, ignored */
1377 afi
, SAFI_UNICAST
, ZEBRA_ROUTE_VNC_DIRECT
,
1378 BGP_ROUTE_REDISTRIBUTE
, NULL
, /* RD not used for unicast */
1379 NULL
, 0, NULL
); /* tag not used for unicast */
1384 * Caller is responsible for ensuring that the specified nve-group
1385 * was actually part of the list of exported nve groups.
1387 static void vnc_direct_bgp_del_group_afi(struct bgp
*bgp
,
1388 struct rfapi_nve_group_cfg
*rfg
,
1391 struct route_table
*rt
= NULL
;
1392 struct route_node
*rn
;
1393 struct rfapi_import_table
*import_table
;
1395 vnc_zlog_debug_verbose("%s: entry", __func__
);
1397 import_table
= rfg
->rfapi_import_table
;
1398 if (!import_table
) {
1399 vnc_zlog_debug_verbose(
1400 "%s: import table not defined, returning", __func__
);
1404 rt
= import_table
->imported_vpn
[afi
];
1406 if (!rfg
->nves
&& rfg
->type
!= RFAPI_GROUP_CFG_VRF
) {
1407 vnc_zlog_debug_verbose("%s: no NVEs in this group", __func__
);
1412 * Walk the NVE-Group's VNC Import table
1414 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
))
1416 if (rfg
->type
== RFAPI_GROUP_CFG_VRF
)
1417 vnc_direct_del_rn_group_rd(bgp
, rfg
, rn
, afi
,
1420 struct listnode
*ln
;
1423 * For each NVE that is assigned to the export
1426 * a route with that NVE as its next hop
1428 for (ln
= listhead(rfg
->nves
); ln
;
1429 ln
= listnextnode(ln
))
1430 vnc_direct_del_rn_group_rd(
1438 * Caller is responsible for ensuring that the specified nve-group
1439 * was actually part of the list of exported nve groups.
1441 void vnc_direct_bgp_del_group(struct bgp
*bgp
, struct rfapi_nve_group_cfg
*rfg
)
1443 vnc_direct_bgp_del_group_afi(bgp
, rfg
, AFI_IP
);
1444 vnc_direct_bgp_del_group_afi(bgp
, rfg
, AFI_IP6
);
1447 void vnc_direct_bgp_reexport_group_afi(struct bgp
*bgp
,
1448 struct rfapi_nve_group_cfg
*rfg
,
1451 struct listnode
*node
;
1452 struct rfapi_rfg_name
*rfgn
;
1454 if (VNC_EXPORT_BGP_GRP_ENABLED(bgp
->rfapi_cfg
)) {
1456 * look in the list of currently-exported groups
1458 for (ALL_LIST_ELEMENTS_RO(
1459 bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
, node
,
1462 if (rfgn
->rfg
== rfg
) {
1464 * If it matches, reexport it
1466 vnc_direct_bgp_del_group_afi(bgp
, rfg
, afi
);
1467 vnc_direct_bgp_add_group_afi(bgp
, rfg
, afi
);
1475 static void vnc_direct_bgp_unexport_table(afi_t afi
, struct route_table
*rt
,
1476 struct list
*nve_list
)
1480 struct route_node
*rn
;
1482 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
)) {
1486 struct listnode
*hln
;
1487 struct rfapi_descriptor
*irfd
;
1489 for (ALL_LIST_ELEMENTS_RO(nve_list
, hln
,
1492 bgp_withdraw(irfd
->peer
,
1493 &rn
->p
, /* prefix */
1495 NULL
, /* attr, ignored */
1497 ZEBRA_ROUTE_VNC_DIRECT
,
1498 BGP_ROUTE_REDISTRIBUTE
,
1499 NULL
, /* RD not used for
1501 NULL
, 0, NULL
); /* tag not
1512 static void import_table_to_nve_list_direct_bgp(struct bgp
*bgp
,
1513 struct rfapi_import_table
*it
,
1517 struct listnode
*node
;
1518 struct rfapi_rfg_name
*rfgn
;
1521 * Loop over the list of NVE-Groups configured for
1522 * exporting to direct-bgp.
1524 * Build a list of NVEs that use this import table
1527 for (ALL_LIST_ELEMENTS_RO(bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
, node
,
1531 * If this NVE-Group's import table matches the current one
1533 if (rfgn
->rfg
&& rfgn
->rfg
->rfapi_import_table
== it
) {
1534 if (rfgn
->rfg
->nves
)
1535 nve_group_to_nve_list(rfgn
->rfg
, nves
, family
);
1536 else if (rfgn
->rfg
->rfd
1537 && rfgn
->rfg
->type
== RFAPI_GROUP_CFG_VRF
) {
1540 listnode_add(*nves
, rfgn
->rfg
->rfd
);
1546 void vnc_direct_bgp_vpn_enable(struct bgp
*bgp
, afi_t afi
)
1548 struct listnode
*rfgn
;
1549 struct rfapi_nve_group_cfg
*rfg
;
1554 if (!VNC_EXPORT_BGP_GRP_ENABLED(bgp
->rfapi_cfg
)) {
1555 vnc_zlog_debug_verbose(
1556 "%s: export-to-bgp group mode not enabled, skipping",
1561 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
) {
1562 vnc_zlog_debug_verbose("%s: bad afi: %d", __func__
, afi
);
1567 * Policy is applied per-nve-group, so we need to iterate
1568 * over the groups to add everything.
1570 for (ALL_LIST_ELEMENTS_RO(bgp
->rfapi_cfg
->nve_groups_sequential
, rfgn
,
1574 * contains policy management
1576 vnc_direct_bgp_add_group_afi(bgp
, rfg
, afi
);
1581 void vnc_direct_bgp_vpn_disable(struct bgp
*bgp
, afi_t afi
)
1583 struct rfapi_import_table
*it
;
1584 uint8_t family
= afi2family(afi
);
1586 vnc_zlog_debug_verbose("%s: entry, afi=%d", __func__
, afi
);
1592 vnc_zlog_debug_verbose("%s: rfapi not initialized", __func__
);
1596 if (!family
|| (afi
!= AFI_IP
&& afi
!= AFI_IP6
)) {
1597 vnc_zlog_debug_verbose("%s: bad afi: %d", __func__
, afi
);
1601 for (it
= bgp
->rfapi
->imports
; it
; it
= it
->next
) {
1603 struct list
*nve_list
= NULL
;
1605 import_table_to_nve_list_direct_bgp(bgp
, it
, &nve_list
, family
);
1608 vnc_direct_bgp_unexport_table(
1609 afi
, it
->imported_vpn
[afi
], nve_list
);
1610 list_delete_and_null(&nve_list
);
1616 /***********************************************************************
1617 * Export methods that proxy nexthop END
1618 ***********************************************************************/
1621 /***********************************************************************
1622 * Export methods that preserve original nexthop BEGIN
1623 * rh = "registering nve"
1624 ***********************************************************************/
1628 * "Adding a Route" export process
1629 * TBD do we need to check bi->type and bi->sub_type here, or does
1632 void vnc_direct_bgp_rh_add_route(struct bgp
*bgp
, afi_t afi
,
1633 struct prefix
*prefix
, struct peer
*peer
,
1636 struct vnc_export_info
*eti
;
1638 struct rfapi_cfg
*hc
;
1642 flog_err(LIB_ERR_DEVELOPMENT
,
1643 "%s: can't get afi of route node", __func__
);
1647 /* check bgp redist flag for vnc direct ("vpn") routes */
1648 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
1649 vnc_zlog_debug_verbose(
1650 "%s: bgp redistribution of VNC direct routes is off",
1655 if (!(hc
= bgp
->rfapi_cfg
)) {
1656 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
1661 if (!VNC_EXPORT_BGP_RH_ENABLED(bgp
->rfapi_cfg
)) {
1662 vnc_zlog_debug_verbose(
1663 "%s: export-to-bgp RH mode not enabled, skipping",
1671 if (hc
->plist_export_bgp
[afi
]) {
1672 if (prefix_list_apply(hc
->plist_export_bgp
[afi
], prefix
)
1678 * Construct new attribute set with NVE's VN addr as
1679 * nexthop and without Tunnel Encap attr
1681 if (encap_attr_export(&hattr
, attr
, NULL
, NULL
))
1683 if (hc
->routemap_export_bgp
) {
1684 struct bgp_info info
;
1685 route_map_result_t ret
;
1687 memset(&info
, 0, sizeof(info
));
1690 ret
= route_map_apply(hc
->routemap_export_bgp
, prefix
, RMAP_BGP
,
1692 if (ret
== RMAP_DENYMATCH
) {
1693 bgp_attr_flush(&hattr
);
1698 iattr
= bgp_attr_intern(&hattr
);
1699 bgp_attr_flush(&hattr
);
1702 * record route information that we will need to expire
1705 eti
= vnc_eti_get(bgp
, EXPORT_TYPE_BGP
, prefix
, peer
,
1706 ZEBRA_ROUTE_VNC_DIRECT_RH
, BGP_ROUTE_REDISTRIBUTE
);
1707 rfapiGetVncLifetime(attr
, &eti
->lifetime
);
1708 eti
->lifetime
= rfapiGetHolddownFromLifetime(eti
->lifetime
);
1712 * export expiration timer is already running on
1713 * this route: cancel it
1715 thread_cancel(eti
->timer
);
1719 bgp_update(peer
, prefix
, /* prefix */
1721 iattr
, /* bgp_update copies this attr */
1722 afi
, SAFI_UNICAST
, ZEBRA_ROUTE_VNC_DIRECT_RH
,
1723 BGP_ROUTE_REDISTRIBUTE
, NULL
, /* RD not used for unicast */
1724 NULL
, /* tag not used for unicast, EVPN neither */
1725 0, 0, NULL
); /* EVPN not used */
1726 bgp_attr_unintern(&iattr
);
1729 static int vncExportWithdrawTimer(struct thread
*t
)
1731 struct vnc_export_info
*eti
= t
->arg
;
1734 * withdraw the route
1736 bgp_withdraw(eti
->peer
, &eti
->node
->p
, 0, /* addpath_id */
1737 NULL
, /* attr, ignored */
1738 family2afi(eti
->node
->p
.family
), SAFI_UNICAST
, eti
->type
,
1739 eti
->subtype
, NULL
, /* RD not used for unicast */
1741 NULL
); /* tag not used for unicast, EVPN neither */
1746 vnc_eti_delete(eti
);
1752 * "Withdrawing a Route" export process
1753 * TBD do we need to check bi->type and bi->sub_type here, or does
1756 void vnc_direct_bgp_rh_del_route(struct bgp
*bgp
, afi_t afi
,
1757 struct prefix
*prefix
, struct peer
*peer
)
1759 struct vnc_export_info
*eti
;
1762 flog_err(LIB_ERR_DEVELOPMENT
, "%s: can't get afi route node",
1767 /* check bgp redist flag for vnc direct ("vpn") routes */
1768 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
1769 vnc_zlog_debug_verbose(
1770 "%s: bgp redistribution of VNC direct routes is off",
1775 if (!bgp
->rfapi_cfg
) {
1776 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
1780 if (!VNC_EXPORT_BGP_RH_ENABLED(bgp
->rfapi_cfg
)) {
1781 vnc_zlog_debug_verbose(
1782 "%s: export-to-bgp group mode not enabled, skipping",
1787 eti
= vnc_eti_get(bgp
, EXPORT_TYPE_BGP
, prefix
, peer
,
1788 ZEBRA_ROUTE_VNC_DIRECT_RH
, BGP_ROUTE_REDISTRIBUTE
);
1790 if (!eti
->timer
&& eti
->lifetime
<= INT32_MAX
) {
1792 thread_add_timer(bm
->master
, vncExportWithdrawTimer
, eti
,
1793 eti
->lifetime
, &eti
->timer
);
1794 vnc_zlog_debug_verbose(
1795 "%s: set expiration timer for %u seconds", __func__
,
1801 void vnc_direct_bgp_rh_vpn_enable(struct bgp
*bgp
, afi_t afi
)
1803 struct prefix_rd prd
;
1804 struct bgp_node
*prn
;
1805 struct rfapi_cfg
*hc
;
1807 vnc_zlog_debug_verbose("%s: entry, afi=%d", __func__
, afi
);
1812 if (!(hc
= bgp
->rfapi_cfg
))
1815 if (!VNC_EXPORT_BGP_RH_ENABLED(bgp
->rfapi_cfg
)) {
1816 vnc_zlog_debug_verbose(
1817 "%s: export of RH routes not enabled, skipping",
1822 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
) {
1823 vnc_zlog_debug_verbose("%s: bad afi: %d", __func__
, afi
);
1828 * Go through the entire BGP VPN table and export to BGP unicast.
1831 vnc_zlog_debug_verbose("%s: starting RD loop", __func__
);
1833 /* Loop over all the RDs */
1834 for (prn
= bgp_table_top(bgp
->rib
[afi
][SAFI_MPLS_VPN
]); prn
;
1835 prn
= bgp_route_next(prn
)) {
1837 struct bgp_table
*table
;
1838 struct bgp_node
*rn
;
1839 struct bgp_info
*ri
;
1841 memset(&prd
, 0, sizeof(prd
));
1842 prd
.family
= AF_UNSPEC
;
1844 memcpy(prd
.val
, prn
->p
.u
.val
, 8);
1846 /* This is the per-RD table of prefixes */
1852 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
1855 * skip prefix list check if no routes here
1861 char prefixstr
[PREFIX_STRLEN
];
1863 prefix2str(&rn
->p
, prefixstr
,
1865 vnc_zlog_debug_verbose("%s: checking prefix %s",
1866 __func__
, prefixstr
);
1872 if (hc
->plist_export_bgp
[afi
]) {
1873 if (prefix_list_apply(hc
->plist_export_bgp
[afi
],
1877 vnc_zlog_debug_verbose(
1878 "%s: prefix list says DENY",
1884 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
1886 vnc_zlog_debug_verbose("%s: ri->sub_type: %d",
1887 __func__
, ri
->sub_type
);
1889 if (ri
->sub_type
== BGP_ROUTE_NORMAL
1890 || ri
->sub_type
== BGP_ROUTE_RFP
) {
1892 struct vnc_export_info
*eti
;
1897 * Construct new attribute set with
1899 * nexthop and without Tunnel Encap attr
1901 if (encap_attr_export(&hattr
, ri
->attr
,
1903 vnc_zlog_debug_verbose(
1904 "%s: encap_attr_export failed",
1909 if (hc
->routemap_export_bgp
) {
1910 struct bgp_info info
;
1911 route_map_result_t ret
;
1913 memset(&info
, 0, sizeof(info
));
1914 info
.peer
= ri
->peer
;
1916 ret
= route_map_apply(
1917 hc
->routemap_export_bgp
,
1920 if (ret
== RMAP_DENYMATCH
) {
1921 bgp_attr_flush(&hattr
);
1922 vnc_zlog_debug_verbose(
1923 "%s: route map says DENY",
1929 iattr
= bgp_attr_intern(&hattr
);
1930 bgp_attr_flush(&hattr
);
1933 * record route information that we will
1938 bgp
, EXPORT_TYPE_BGP
, &rn
->p
,
1940 ZEBRA_ROUTE_VNC_DIRECT_RH
,
1941 BGP_ROUTE_REDISTRIBUTE
);
1942 rfapiGetVncLifetime(ri
->attr
,
1947 * export expiration timer is
1948 * already running on
1949 * this route: cancel it
1951 thread_cancel(eti
->timer
);
1955 vnc_zlog_debug_verbose(
1956 "%s: calling bgp_update",
1960 ri
->peer
, &rn
->p
, /* prefix */
1962 iattr
, /* bgp_update copies
1964 AFI_IP
, SAFI_UNICAST
,
1965 ZEBRA_ROUTE_VNC_DIRECT_RH
,
1966 BGP_ROUTE_REDISTRIBUTE
, NULL
,
1967 /* RD not used for unicast */
1969 /* tag not used for unicast,
1971 0, 0, NULL
); /* EVPN not used */
1973 bgp_attr_unintern(&iattr
);
1980 void vnc_direct_bgp_rh_vpn_disable(struct bgp
*bgp
, afi_t afi
)
1982 struct bgp_node
*rn
;
1984 vnc_zlog_debug_verbose("%s: entry, afi=%d", __func__
, afi
);
1989 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
) {
1990 vnc_zlog_debug_verbose("%s: bad afi: %d", __func__
, afi
);
1995 * Go through the entire BGP unicast table and remove routes that
1996 * originated from us
1998 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
;
1999 rn
= bgp_route_next(rn
)) {
2001 struct bgp_info
*ri
;
2002 struct bgp_info
*next
;
2004 for (ri
= rn
->info
, next
= NULL
; ri
; ri
= next
) {
2008 if (ri
->type
== ZEBRA_ROUTE_VNC_DIRECT_RH
2009 && ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
) {
2011 struct vnc_export_info
*eti
;
2014 * Delete routes immediately (no timer)
2016 eti
= vnc_eti_checktimer(
2017 bgp
, EXPORT_TYPE_BGP
, &rn
->p
, ri
->peer
,
2018 ZEBRA_ROUTE_VNC_DIRECT_RH
,
2019 BGP_ROUTE_REDISTRIBUTE
);
2022 thread_cancel(eti
->timer
);
2023 vnc_eti_delete(eti
);
2026 bgp_withdraw(ri
->peer
, &rn
->p
, /* prefix */
2029 AFI_IP
, SAFI_UNICAST
,
2030 ZEBRA_ROUTE_VNC_DIRECT_RH
,
2031 BGP_ROUTE_REDISTRIBUTE
,
2032 NULL
, /* RD not used for unicast */
2033 NULL
, 0, NULL
); /* tag not used for
2041 void vnc_direct_bgp_rh_reexport(struct bgp
*bgp
, afi_t afi
)
2043 if (VNC_EXPORT_BGP_RH_ENABLED(bgp
->rfapi_cfg
)) {
2044 vnc_direct_bgp_rh_vpn_disable(bgp
, afi
);
2045 vnc_direct_bgp_rh_vpn_enable(bgp
, afi
);
2049 /***********************************************************************
2050 * Generic Export methods
2051 ***********************************************************************/
2054 * Assumes the correct mode bits are already turned on. Thus it
2055 * is OK to call this function from, e.g., bgp_redistribute_set()
2056 * without caring if export is enabled or not
2058 void vnc_export_bgp_enable(struct bgp
*bgp
, afi_t afi
)
2060 if (!bgp
->rfapi_cfg
)
2063 switch (bgp
->rfapi_cfg
->flags
& BGP_VNC_CONFIG_EXPORT_BGP_MODE_BITS
) {
2064 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_NONE
:
2067 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_GRP
:
2068 vnc_direct_bgp_vpn_enable(bgp
, afi
);
2071 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_RH
:
2072 vnc_direct_bgp_rh_vpn_enable(bgp
, afi
);
2075 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_CE
:
2076 vnc_direct_bgp_vpn_enable_ce(bgp
, afi
);
2081 void vnc_export_bgp_disable(struct bgp
*bgp
, afi_t afi
)
2083 if (!bgp
->rfapi_cfg
)
2086 switch (bgp
->rfapi_cfg
->flags
& BGP_VNC_CONFIG_EXPORT_BGP_MODE_BITS
) {
2087 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_NONE
:
2090 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_GRP
:
2091 vnc_direct_bgp_vpn_disable(bgp
, afi
);
2094 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_RH
:
2095 vnc_direct_bgp_rh_vpn_disable(bgp
, afi
);
2098 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_CE
:
2099 vnc_direct_bgp_vpn_disable_ce(bgp
, afi
);
2104 void vnc_export_bgp_prechange(struct bgp
*bgp
)
2106 vnc_export_bgp_disable(bgp
, AFI_IP
);
2107 vnc_export_bgp_disable(bgp
, AFI_IP6
);
2110 void vnc_export_bgp_postchange(struct bgp
*bgp
)
2112 vnc_export_bgp_enable(bgp
, AFI_IP
);
2113 vnc_export_bgp_enable(bgp
, AFI_IP6
);
2116 void vnc_direct_bgp_reexport(struct bgp
*bgp
, afi_t afi
)
2118 vnc_export_bgp_disable(bgp
, afi
);
2119 vnc_export_bgp_enable(bgp
, afi
);