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/agg_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 agg_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
));
86 switch (use_nexthop
->family
) {
88 new->nexthop
= use_nexthop
->u
.prefix4
;
89 new->mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
; /* 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
= BGP_ATTR_NHLEN_IPV6_GLOBAL
; /* 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
;
139 struct ecommunity
*ecomm
= bgp_attr_get_ecommunity(attr
);
141 for (ecp
= ecomm
->val
, i
= 0; i
< ecomm
->size
;
142 ++i
, ecp
+= ECOMMUNITY_SIZE
) {
144 if (VNC_DEBUG(EXPORT_BGP_GETCE
)) {
146 "%s: %02x %02x %02x %02x %02x %02x %02x %02x",
147 __func__
, ecp
[0], ecp
[1], ecp
[2], ecp
[3],
148 ecp
[4], ecp
[5], ecp
[6], ecp
[7]);
154 if (ecp
[0] != 1 || ecp
[1] != 3) {
159 * Match local admin value?
161 if (ecp
[6] != ((localadmin
& 0xff00) >> 8)
162 || ecp
[7] != (localadmin
& 0xff))
165 memset((uint8_t *)pfx_ce
, 0, sizeof(*pfx_ce
));
166 memcpy(&pfx_ce
->u
.prefix4
, ecp
+ 2, 4);
167 pfx_ce
->family
= AF_INET
;
168 pfx_ce
->prefixlen
= IPV4_MAX_BITLEN
;
176 void vnc_direct_bgp_add_route_ce(struct bgp
*bgp
, struct agg_node
*rn
,
177 struct bgp_path_info
*bpi
)
179 struct attr
*attr
= bpi
->attr
;
180 struct peer
*peer
= bpi
->peer
;
181 const struct prefix
*prefix
= agg_node_get_prefix(rn
);
182 afi_t afi
= family2afi(prefix
->family
);
183 struct bgp_dest
*udest
;
184 struct bgp_path_info
*ubpi
;
187 struct prefix ce_nexthop
;
188 struct prefix post_routemap_nexthop
;
192 flog_err(EC_LIB_DEVELOPMENT
, "%s: can't get afi of route node",
197 if ((bpi
->type
!= ZEBRA_ROUTE_BGP
)
198 || (bpi
->sub_type
!= BGP_ROUTE_NORMAL
199 && bpi
->sub_type
!= BGP_ROUTE_RFP
200 && bpi
->sub_type
!= BGP_ROUTE_STATIC
)) {
202 vnc_zlog_debug_verbose(
203 "%s: wrong route type/sub_type for export, skipping",
208 /* check bgp redist flag for vnc direct ("vpn") routes */
209 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
210 vnc_zlog_debug_verbose(
211 "%s: bgp redistribution of VNC direct routes is off",
216 if (!bgp
->rfapi_cfg
) {
217 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
222 if (!VNC_EXPORT_BGP_CE_ENABLED(bgp
->rfapi_cfg
)) {
223 vnc_zlog_debug_verbose(
224 "%s: export-to-bgp ce mode not enabled, skipping",
232 if (bgp
->rfapi_cfg
->plist_export_bgp
[afi
]) {
233 if (prefix_list_apply(bgp
->rfapi_cfg
->plist_export_bgp
[afi
],
236 vnc_zlog_debug_verbose(
237 "%s: prefix list denied, skipping", __func__
);
245 * This works only for IPv4 because IPv6 addresses are too big
246 * to fit in an extended community
248 if (getce(bgp
, attr
, &ce_nexthop
)) {
249 vnc_zlog_debug_verbose("%s: EC has no encoded CE, skipping",
255 * Is this route already represented in the unicast RIB?
256 * (look up prefix; compare route type, sub_type, peer, nexthop)
258 udest
= bgp_afi_node_get(bgp
->rib
[afi
][SAFI_UNICAST
], afi
, SAFI_UNICAST
,
260 for (ubpi
= bgp_dest_get_bgp_path_info(udest
); ubpi
;
262 struct prefix unicast_nexthop
;
264 if (CHECK_FLAG(ubpi
->flags
, BGP_PATH_REMOVED
))
267 rfapiUnicastNexthop2Prefix(afi
, ubpi
->attr
, &unicast_nexthop
);
269 if (ubpi
->type
== ZEBRA_ROUTE_VNC_DIRECT
270 && ubpi
->sub_type
== BGP_ROUTE_REDISTRIBUTE
271 && ubpi
->peer
== peer
272 && prefix_same(&unicast_nexthop
, &ce_nexthop
)) {
274 vnc_zlog_debug_verbose(
275 "%s: already have matching exported unicast route, skipping",
282 * Construct new attribute set with CE addr as
283 * nexthop and without Tunnel Encap attr
285 encap_attr_export_ce(&hattr
, attr
, &ce_nexthop
);
286 if (bgp
->rfapi_cfg
->routemap_export_bgp
) {
287 struct bgp_path_info info
;
288 route_map_result_t ret
;
290 memset(&info
, 0, sizeof(info
));
293 ret
= route_map_apply(bgp
->rfapi_cfg
->routemap_export_bgp
,
295 if (ret
== RMAP_DENYMATCH
) {
296 bgp_attr_flush(&hattr
);
301 iattr
= bgp_attr_intern(&hattr
);
302 bgp_attr_flush(&hattr
);
305 * Rule: disallow route-map alteration of next-hop, because it
306 * would make it too difficult to keep track of the correspondence
307 * between VPN routes and unicast routes.
309 rfapiUnicastNexthop2Prefix(afi
, iattr
, &post_routemap_nexthop
);
311 if (!prefix_same(&ce_nexthop
, &post_routemap_nexthop
)) {
312 vnc_zlog_debug_verbose(
313 "%s: route-map modification of nexthop not allowed, skipping",
315 bgp_attr_unintern(&iattr
);
319 bgp_update(peer
, prefix
, 0, /* addpath_id */
320 iattr
, /* bgp_update copies this attr */
321 afi
, SAFI_UNICAST
, ZEBRA_ROUTE_VNC_DIRECT
,
322 BGP_ROUTE_REDISTRIBUTE
, NULL
, /* RD not used for unicast */
323 NULL
, 0, /* tag not used for unicast */
324 0, NULL
); /* EVPN not used */
325 bgp_attr_unintern(&iattr
);
330 * "Withdrawing a Route" export process
332 void vnc_direct_bgp_del_route_ce(struct bgp
*bgp
, struct agg_node
*rn
,
333 struct bgp_path_info
*bpi
)
335 const struct prefix
*p
= agg_node_get_prefix(rn
);
336 afi_t afi
= family2afi(p
->family
);
337 struct bgp_path_info
*vbpi
;
338 struct prefix ce_nexthop
;
341 flog_err(EC_LIB_DEVELOPMENT
, "%s: bad afi", __func__
);
345 /* check bgp redist flag for vnc direct ("vpn") routes */
346 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
347 vnc_zlog_debug_verbose(
348 "%s: bgp redistribution of VNC direct routes is off",
353 if (!bgp
->rfapi_cfg
) {
354 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
358 if (!VNC_EXPORT_BGP_CE_ENABLED(bgp
->rfapi_cfg
)) {
359 vnc_zlog_debug_verbose(
360 "%s: export-to-bgp ce mode not enabled, skipping",
367 * This works only for IPv4 because IPv6 addresses are too big
368 * to fit in an extended community
370 if (getce(bgp
, bpi
->attr
, &ce_nexthop
)) {
371 vnc_zlog_debug_verbose("%s: EC has no encoded CE, skipping",
377 * Look for other VPN routes with same prefix, same 5226 CE,
378 * same peer. If at least one is present, don't remove the
379 * route from the unicast RIB
382 for (vbpi
= rn
->info
; vbpi
; vbpi
= vbpi
->next
) {
386 if (bpi
->peer
!= vbpi
->peer
)
388 if (getce(bgp
, vbpi
->attr
, &ce
))
390 if (prefix_same(&ce
, &ce_nexthop
)) {
391 vnc_zlog_debug_verbose(
392 "%s: still have a route via CE, not deleting unicast",
401 bgp_withdraw(bpi
->peer
, p
, 0, /* addpath_id */
402 NULL
, /* attr, ignored */
403 afi
, SAFI_UNICAST
, ZEBRA_ROUTE_VNC_DIRECT
,
404 BGP_ROUTE_REDISTRIBUTE
, NULL
, /* RD not used for unicast */
405 NULL
, 0, NULL
); /* tag not used for unicast */
408 static void vnc_direct_bgp_vpn_enable_ce(struct bgp
*bgp
, afi_t afi
)
411 struct bgp_path_info
*ri
;
413 vnc_zlog_debug_verbose("%s: entry, afi=%d", __func__
, afi
);
418 if (!(bgp
->rfapi_cfg
))
421 if (!VNC_EXPORT_BGP_CE_ENABLED(bgp
->rfapi_cfg
)) {
422 vnc_zlog_debug_verbose(
423 "%s: export of CE routes not enabled, skipping",
428 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
) {
429 vnc_zlog_debug_verbose("%s: bad afi: %d", __func__
, afi
);
434 * Go through entire ce import table and export to BGP unicast.
436 for (rn
= agg_route_top(bgp
->rfapi
->it_ce
->imported_vpn
[afi
]); rn
;
437 rn
= agg_route_next(rn
)) {
441 vnc_zlog_debug_verbose("%s: checking prefix %pRN", __func__
,
444 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
446 vnc_zlog_debug_verbose("%s: ri->sub_type: %d", __func__
,
449 if (ri
->sub_type
== BGP_ROUTE_NORMAL
450 || ri
->sub_type
== BGP_ROUTE_RFP
451 || ri
->sub_type
== BGP_ROUTE_STATIC
) {
453 vnc_direct_bgp_add_route_ce(bgp
, rn
, ri
);
459 static void vnc_direct_bgp_vpn_disable_ce(struct bgp
*bgp
, afi_t afi
)
461 struct bgp_dest
*dest
;
463 vnc_zlog_debug_verbose("%s: entry, afi=%d", __func__
, afi
);
468 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
) {
469 vnc_zlog_debug_verbose("%s: bad afi: %d", __func__
, afi
);
474 * Go through the entire BGP unicast table and remove routes that
477 for (dest
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); dest
;
478 dest
= bgp_route_next(dest
)) {
480 struct bgp_path_info
*ri
;
481 struct bgp_path_info
*next
;
483 for (ri
= bgp_dest_get_bgp_path_info(dest
), next
= NULL
; ri
;
488 if (ri
->type
== ZEBRA_ROUTE_VNC_DIRECT
489 && ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
) {
492 ri
->peer
, bgp_dest_get_prefix(dest
),
495 AFI_IP
, SAFI_UNICAST
,
496 ZEBRA_ROUTE_VNC_DIRECT
,
497 BGP_ROUTE_REDISTRIBUTE
,
498 NULL
, /* RD not used for unicast */
500 NULL
); /* tag not used for unicast */
506 /***********************************************************************
507 * Export methods that set nexthop to CE (from 5226 roo EC) END
508 ***********************************************************************/
510 /***********************************************************************
511 * Export methods that proxy nexthop BEGIN
512 ***********************************************************************/
514 static struct ecommunity
*vnc_route_origin_ecom(struct agg_node
*rn
)
516 struct ecommunity
*new;
517 struct bgp_path_info
*bpi
;
522 new = ecommunity_new();
524 for (bpi
= rn
->info
; bpi
; bpi
= bpi
->next
) {
526 struct ecommunity_val roec
;
528 switch (BGP_MP_NEXTHOP_FAMILY(bpi
->attr
->mp_nexthop_len
)) {
530 memset(&roec
, 0, sizeof(roec
));
534 &bpi
->attr
->mp_nexthop_global_in
.s_addr
, 4);
537 ecommunity_add_val(new, &roec
, false, false);
540 /* No support for IPv6 addresses in extended communities
547 ecommunity_free(&new);
554 static struct ecommunity
*vnc_route_origin_ecom_single(struct in_addr
*origin
)
556 struct ecommunity
*new;
557 struct ecommunity_val roec
;
559 memset(&roec
, 0, sizeof(roec
));
562 memcpy(roec
.val
+ 2, &origin
->s_addr
, 4);
566 new = ecommunity_new();
567 ecommunity_add_val(new, &roec
, false, false);
570 ecommunity_free(&new);
579 * New memory allocation approach: make a ghost attr that
580 * has non-interned parts for the modifications. ghost attr
581 * memory is allocated by caller.
584 encap_attr_export(struct attr
*new, struct attr
*orig
,
585 struct prefix
*new_nexthop
,
586 struct agg_node
*rn
) /* for VN addrs for ecom list */
587 /* if rn is 0, use route's nexthop */
589 struct prefix orig_nexthop
;
590 struct prefix
*use_nexthop
;
591 static struct ecommunity
*ecom_ro
;
594 use_nexthop
= new_nexthop
;
596 use_nexthop
= &orig_nexthop
;
597 orig_nexthop
.family
=
598 BGP_MP_NEXTHOP_FAMILY(orig
->mp_nexthop_len
);
599 if (orig_nexthop
.family
== AF_INET
) {
600 orig_nexthop
.prefixlen
= IPV4_MAX_BITLEN
;
601 orig_nexthop
.u
.prefix4
= orig
->mp_nexthop_global_in
;
602 } else if (orig_nexthop
.family
== AF_INET6
) {
603 orig_nexthop
.prefixlen
= IPV6_MAX_BITLEN
;
604 orig_nexthop
.u
.prefix6
= orig
->mp_nexthop_global
;
606 return -1; /* FAIL - can't compute nexthop */
612 * Make "new" a ghost attr copy of "orig"
614 memset(new, 0, sizeof(struct attr
));
620 switch (use_nexthop
->family
) {
622 new->nexthop
= use_nexthop
->u
.prefix4
;
623 new->mp_nexthop_len
= BGP_ATTR_NHLEN_IPV4
; /* bytes */
624 new->flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
628 new->mp_nexthop_global
= use_nexthop
->u
.prefix6
;
629 new->mp_nexthop_len
= BGP_ATTR_NHLEN_IPV6_GLOBAL
; /* bytes */
638 ecom_ro
= vnc_route_origin_ecom(rn
);
640 /* TBD use lcom for IPv6 */
641 ecom_ro
= vnc_route_origin_ecom_single(&use_nexthop
->u
.prefix4
);
643 if (bgp_attr_get_ecommunity(new)) {
645 bgp_attr_set_ecommunity(
647 ecommunity_merge(ecom_ro
,
648 bgp_attr_get_ecommunity(new)));
650 bgp_attr_set_ecommunity(new, ecom_ro
);
656 * Note that it will be deleted when BGP sends to any eBGP
657 * peer unless PEER_FLAG_MED_UNCHANGED is set:
659 * neighbor NEIGHBOR attribute-unchanged med
661 if (!CHECK_FLAG(new->flag
, BGP_ATTR_MULTI_EXIT_DISC
)) {
662 if (CHECK_FLAG(new->flag
, BGP_ATTR_LOCAL_PREF
)) {
663 if (new->local_pref
> 255)
666 new->med
= 255 - new->local_pref
;
668 new->med
= 255; /* shouldn't happen */
670 new->flag
|= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
);
674 * "new" is now a ghost attr:
675 * - it owns an "extra" struct
676 * - it owns any non-interned parts
677 * - any references to interned parts are not counted
679 * Caller should, after using the attr, call:
680 * - bgp_attr_flush() to free non-interned parts
687 * "Adding a Route" export process
689 void vnc_direct_bgp_add_prefix(struct bgp
*bgp
,
690 struct rfapi_import_table
*import_table
,
693 struct attr attr
= {0};
694 struct listnode
*node
, *nnode
;
695 struct rfapi_rfg_name
*rfgn
;
696 const struct prefix
*p
= agg_node_get_prefix(rn
);
697 afi_t afi
= family2afi(p
->family
);
700 flog_err(EC_LIB_DEVELOPMENT
, "%s: can't get afi of route node",
705 /* check bgp redist flag for vnc direct ("vpn") routes */
706 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
707 vnc_zlog_debug_verbose(
708 "%s: bgp redistribution of VNC direct routes is off",
713 if (!bgp
->rfapi_cfg
) {
714 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
719 if (!VNC_EXPORT_BGP_GRP_ENABLED(bgp
->rfapi_cfg
)) {
720 vnc_zlog_debug_verbose(
721 "%s: export-to-bgp group mode not enabled, skipping",
726 if (!listcount(bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
)) {
727 vnc_zlog_debug_verbose(
728 "%s: no bgp-direct export nve group, skipping",
733 bgp_attr_default_set(&attr
, bgp
, BGP_ORIGIN_INCOMPLETE
);
734 /* TBD set some configured med, see add_vnc_route() */
736 vnc_zlog_debug_verbose(
737 "%s: looping over nve-groups in direct-bgp export list",
740 for (ALL_LIST_ELEMENTS(bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
, node
,
746 * If nve group is not defined yet, skip it
752 * If the nve group uses a different import table, skip it
754 if (import_table
!= rfgn
->rfg
->rfapi_import_table
)
758 * if no NVEs currently associated with this group, skip it
760 if (rfgn
->rfg
->type
!= RFAPI_GROUP_CFG_VRF
&& !rfgn
->rfg
->nves
)
764 * per-nve-group prefix list check
766 if (rfgn
->rfg
->plist_export_bgp
[afi
]) {
767 if (prefix_list_apply(rfgn
->rfg
->plist_export_bgp
[afi
],
774 if (rfgn
->rfg
->type
== RFAPI_GROUP_CFG_VRF
) {
775 vnc_direct_add_rn_group_rd(bgp
, rfgn
->rfg
, rn
, &attr
,
776 afi
, rfgn
->rfg
->rfd
);
779 * - but consistent with rest of function
784 * For each NVE that is assigned to the export nve group,
786 * a route with that NVE as its next hop
788 for (ln
= listhead(rfgn
->rfg
->nves
); ln
;
789 ln
= listnextnode(ln
)) {
790 vnc_direct_add_rn_group_rd(bgp
, rfgn
->rfg
, rn
, &attr
,
791 afi
, listgetdata(ln
));
795 aspath_unintern(&attr
.aspath
);
799 * "Withdrawing a Route" export process
801 void vnc_direct_bgp_del_prefix(struct bgp
*bgp
,
802 struct rfapi_import_table
*import_table
,
805 struct listnode
*node
, *nnode
;
806 struct rfapi_rfg_name
*rfgn
;
807 const struct prefix
*p
= agg_node_get_prefix(rn
);
808 afi_t afi
= family2afi(p
->family
);
811 flog_err(EC_LIB_DEVELOPMENT
, "%s: can't get afi route node",
816 /* check bgp redist flag for vnc direct ("vpn") routes */
817 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
818 vnc_zlog_debug_verbose(
819 "%s: bgp redistribution of VNC direct routes is off",
824 if (!bgp
->rfapi_cfg
) {
825 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
830 if (!VNC_EXPORT_BGP_GRP_ENABLED(bgp
->rfapi_cfg
)) {
831 vnc_zlog_debug_verbose(
832 "%s: export-to-bgp group mode not enabled, skipping",
837 if (!listcount(bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
)) {
838 vnc_zlog_debug_verbose(
839 "%s: no bgp-direct export nve group, skipping",
844 for (ALL_LIST_ELEMENTS(bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
, node
,
850 * If nve group is not defined yet, skip it
856 * if no NVEs currently associated with this group, skip it
858 if (rfgn
->rfg
->type
!= RFAPI_GROUP_CFG_VRF
&& !rfgn
->rfg
->nves
)
862 * If the nve group uses a different import table,
865 if (import_table
!= rfgn
->rfg
->rfapi_import_table
)
868 if (rfgn
->rfg
->type
== RFAPI_GROUP_CFG_VRF
) {
870 struct rfapi_descriptor
*irfd
;
872 irfd
= rfgn
->rfg
->rfd
;
874 if (rfapiRaddr2Qprefix(&irfd
->vn_addr
, &nhp
))
877 bgp_withdraw(irfd
->peer
, p
, /* prefix */
879 NULL
, /* attr, ignored */
880 afi
, SAFI_UNICAST
, ZEBRA_ROUTE_VNC_DIRECT
,
881 BGP_ROUTE_REDISTRIBUTE
,
882 NULL
, /* RD not used for unicast */
884 NULL
); /* tag not used for unicast */
887 * - but consistent with rest of function
892 * For each NVE that is assigned to the export nve group,
894 * a route with that NVE as its next hop
896 for (ln
= listhead(rfgn
->rfg
->nves
); ln
;
897 ln
= listnextnode(ln
)) {
900 struct rfapi_descriptor
*irfd
;
902 irfd
= listgetdata(ln
);
904 if (rfapiRaddr2Qprefix(&irfd
->vn_addr
, &nhp
))
907 bgp_withdraw(irfd
->peer
, p
, /* prefix */
909 NULL
, /* attr, ignored */
910 afi
, SAFI_UNICAST
, ZEBRA_ROUTE_VNC_DIRECT
,
911 BGP_ROUTE_REDISTRIBUTE
,
912 NULL
, /* RD not used for unicast */
914 NULL
); /* tag not used for unicast */
919 void vnc_direct_bgp_add_nve(struct bgp
*bgp
, struct rfapi_descriptor
*rfd
)
921 struct listnode
*node
, *nnode
;
922 struct rfapi_rfg_name
*rfgn
;
923 struct rfapi_nve_group_cfg
*rfg
= rfd
->rfg
;
924 afi_t afi
= family2afi(rfd
->vn_addr
.addr_family
);
927 flog_err(EC_LIB_DEVELOPMENT
, "%s: can't get afi of nve vn addr",
934 if (!bgp
->rfapi_cfg
) {
935 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
939 if (!VNC_EXPORT_BGP_GRP_ENABLED(bgp
->rfapi_cfg
)) {
940 vnc_zlog_debug_verbose(
941 "%s: export-to-bgp group mode not enabled, skipping",
946 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
947 vnc_zlog_debug_verbose(
948 "%s: bgp redistribution of VNC direct routes is off",
954 * Loop over the list of NVE-Groups configured for
955 * exporting to direct-bgp and see if this new NVE's
956 * group is among them.
958 for (ALL_LIST_ELEMENTS(bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
, node
,
962 * Yes, this NVE's group is configured for export to direct-bgp
964 if (rfgn
->rfg
== rfg
) {
966 struct agg_table
*rt
= NULL
;
968 struct attr attr
= {0};
969 struct rfapi_import_table
*import_table
;
972 import_table
= rfg
->rfapi_import_table
;
974 if (afi
== AFI_IP
|| afi
== AFI_IP6
) {
975 rt
= import_table
->imported_vpn
[afi
];
977 flog_err(EC_LIB_DEVELOPMENT
, "%s: bad afi %d",
982 bgp_attr_default_set(&attr
, bgp
, BGP_ORIGIN_INCOMPLETE
);
983 /* TBD set some configured med, see add_vnc_route() */
986 * Walk the NVE-Group's VNC Import table
988 for (rn
= agg_route_top(rt
); rn
;
989 rn
= agg_route_next(rn
)) {
994 struct rfapi_descriptor
*irfd
= rfd
;
997 struct bgp_path_info info
;
998 const struct prefix
*p
=
999 agg_node_get_prefix(rn
);
1001 if (rfapiRaddr2Qprefix(&irfd
->vn_addr
,
1006 * per-nve-group prefix list check
1008 if (rfgn
->rfg
->plist_export_bgp
[afi
]) {
1009 if (prefix_list_apply(
1010 rfgn
->rfg
->plist_export_bgp
1020 * Construct new attribute set with
1022 * nexthop and without Tunnel Encap attr
1024 if (encap_attr_export(&hattr
, &attr
,
1028 if (rfgn
->rfg
->routemap_export_bgp
) {
1029 route_map_result_t ret
;
1030 info
.peer
= irfd
->peer
;
1032 ret
= route_map_apply(
1034 ->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
, 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(EC_LIB_DEVELOPMENT
, "%s: can't get afi of nve vn addr",
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 agg_table
*rt
= NULL
;
1115 struct agg_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(EC_LIB_DEVELOPMENT
, "%s: bad afi %d",
1129 * Walk the NVE-Group's VNC Import table
1131 for (rn
= agg_route_top(rt
); rn
;
1132 rn
= agg_route_next(rn
)) {
1135 const struct prefix
*p
=
1136 agg_node_get_prefix(rn
);
1138 struct rfapi_descriptor
*irfd
= rfd
;
1140 if (rfapiRaddr2Qprefix(&irfd
->vn_addr
,
1144 bgp_withdraw(irfd
->peer
, p
, /* prefix */
1146 NULL
, /* attr, ignored */
1148 ZEBRA_ROUTE_VNC_DIRECT
,
1149 BGP_ROUTE_REDISTRIBUTE
,
1150 NULL
, /* RD not used for
1152 NULL
, 0, NULL
); /* tag not
1161 static void vnc_direct_add_rn_group_rd(struct bgp
*bgp
,
1162 struct rfapi_nve_group_cfg
*rfg
,
1163 struct agg_node
*rn
, struct attr
*attr
,
1164 afi_t afi
, struct rfapi_descriptor
*irfd
)
1167 struct bgp_path_info info
;
1170 const struct prefix
*p
= agg_node_get_prefix(rn
);
1172 if (irfd
== NULL
&& rfg
->type
!= RFAPI_GROUP_CFG_VRF
) {
1173 /* need new rfapi_handle, for peer strcture
1174 * -- based on vnc_add_vrf_prefi */
1175 assert(rfg
->rfd
== NULL
);
1177 if (!rfg
->rt_export_list
|| !rfg
->rfapi_import_table
) {
1178 vnc_zlog_debug_verbose(
1179 "%s: VRF \"%s\" is missing RT import/export configuration.",
1180 __func__
, rfg
->name
);
1183 if (!rfg
->rd
.prefixlen
) {
1184 vnc_zlog_debug_verbose(
1185 "%s: VRF \"%s\" is missing RD configuration.",
1186 __func__
, rfg
->name
);
1189 if (rfg
->label
> MPLS_LABEL_MAX
) {
1190 vnc_zlog_debug_verbose(
1191 "%s: VRF \"%s\" is missing default label configuration.",
1192 __func__
, rfg
->name
);
1196 irfd
= XCALLOC(MTYPE_RFAPI_DESC
,
1197 sizeof(struct rfapi_descriptor
));
1201 * leave most fields empty as will get from (dynamic) config
1204 irfd
->default_tunneltype_option
.type
= BGP_ENCAP_TYPE_MPLS
;
1206 if (rfg
->vn_prefix
.family
1207 && !CHECK_FLAG(rfg
->flags
, RFAPI_RFG_VPN_NH_SELF
)) {
1208 rfapiQprefix2Raddr(&rfg
->vn_prefix
, &irfd
->vn_addr
);
1210 memset(&irfd
->vn_addr
, 0, sizeof(struct rfapi_ip_addr
));
1211 irfd
->vn_addr
.addr_family
= AF_INET
;
1212 irfd
->vn_addr
.addr
.v4
= bgp
->router_id
;
1214 irfd
->un_addr
= irfd
->vn_addr
; /* sigh, need something in UN for
1216 vnc_zlog_debug_verbose("%s: Opening RFD for VRF %s", __func__
,
1218 rfapi_init_and_open(bgp
, irfd
, rfg
);
1221 if (irfd
== NULL
|| rfapiRaddr2Qprefix(&irfd
->vn_addr
, &nhp
))
1225 * Construct new attribute set with NVE's VN
1227 * nexthop and without Tunnel Encap attr
1229 if (encap_attr_export(&hattr
, attr
, &nhp
, rn
))
1232 if (VNC_DEBUG(EXPORT_BGP_DIRECT_ADD
)) {
1233 vnc_zlog_debug_any("%s: attr follows", __func__
);
1234 rfapiPrintAttrPtrs(NULL
, attr
);
1235 vnc_zlog_debug_any("%s: hattr follows", __func__
);
1236 rfapiPrintAttrPtrs(NULL
, &hattr
);
1239 if (rfg
->routemap_export_bgp
) {
1240 route_map_result_t ret
;
1242 info
.peer
= irfd
->peer
;
1244 ret
= route_map_apply(rfg
->routemap_export_bgp
, p
, &info
);
1245 if (ret
== RMAP_DENYMATCH
) {
1246 bgp_attr_flush(&hattr
);
1247 vnc_zlog_debug_verbose(
1248 "%s: route map says DENY, so not calling bgp_update",
1254 if (VNC_DEBUG(EXPORT_BGP_DIRECT_ADD
)) {
1255 vnc_zlog_debug_any("%s: hattr after route_map_apply:",
1257 rfapiPrintAttrPtrs(NULL
, &hattr
);
1259 iattr
= bgp_attr_intern(&hattr
);
1260 bgp_attr_flush(&hattr
);
1262 bgp_update(irfd
->peer
, p
, /* prefix */
1264 iattr
, /* bgp_update copies it */
1265 afi
, SAFI_UNICAST
, ZEBRA_ROUTE_VNC_DIRECT
,
1266 BGP_ROUTE_REDISTRIBUTE
, NULL
, /* RD not used for unicast */
1267 NULL
, /* tag not used for unicast */
1268 0, 0, NULL
); /* EVPN not used */
1270 bgp_attr_unintern(&iattr
);
1276 * Caller is responsible for ensuring that the specified nve-group
1277 * is actually part of the list of exported nve groups.
1279 static void vnc_direct_bgp_add_group_afi(struct bgp
*bgp
,
1280 struct rfapi_nve_group_cfg
*rfg
,
1283 struct agg_table
*rt
= NULL
;
1284 struct agg_node
*rn
;
1285 struct attr attr
= {0};
1286 struct rfapi_import_table
*import_table
;
1288 vnc_zlog_debug_verbose("%s: entry", __func__
);
1290 import_table
= rfg
->rfapi_import_table
;
1291 if (!import_table
) {
1292 vnc_zlog_debug_verbose(
1293 "%s: import table not defined, returning", __func__
);
1297 if (afi
== AFI_IP
|| afi
== AFI_IP6
) {
1298 rt
= import_table
->imported_vpn
[afi
];
1300 flog_err(EC_LIB_DEVELOPMENT
, "%s: bad afi %d", __func__
, afi
);
1304 if (!rfg
->nves
&& rfg
->type
!= RFAPI_GROUP_CFG_VRF
) {
1305 vnc_zlog_debug_verbose("%s: no NVEs in this group", __func__
);
1309 bgp_attr_default_set(&attr
, bgp
, BGP_ORIGIN_INCOMPLETE
);
1310 /* TBD set some configured med, see add_vnc_route() */
1313 * Walk the NVE-Group's VNC Import table
1315 for (rn
= agg_route_top(rt
); rn
; rn
= agg_route_next(rn
)) {
1318 const struct prefix
*p
= agg_node_get_prefix(rn
);
1319 struct listnode
*ln
;
1322 * per-nve-group prefix list check
1324 if (rfg
->plist_export_bgp
[afi
]) {
1325 if (prefix_list_apply(
1326 rfg
->plist_export_bgp
[afi
], p
)
1331 if (rfg
->type
== RFAPI_GROUP_CFG_VRF
) {
1332 vnc_direct_add_rn_group_rd(bgp
, rfg
, rn
, &attr
,
1336 * - but consistent with rest of function
1341 * For each NVE that is assigned to the export nve
1343 * a route with that NVE as its next hop
1345 for (ln
= listhead(rfg
->nves
); ln
;
1346 ln
= listnextnode(ln
)) {
1347 vnc_direct_add_rn_group_rd(bgp
, rfg
, rn
, &attr
,
1354 aspath_unintern(&attr
.aspath
);
1359 * Caller is responsible for ensuring that the specified nve-group
1360 * is actually part of the list of exported nve groups.
1362 void vnc_direct_bgp_add_group(struct bgp
*bgp
, struct rfapi_nve_group_cfg
*rfg
)
1364 vnc_direct_bgp_add_group_afi(bgp
, rfg
, AFI_IP
);
1365 vnc_direct_bgp_add_group_afi(bgp
, rfg
, AFI_IP6
);
1368 static void vnc_direct_del_rn_group_rd(struct bgp
*bgp
,
1369 struct rfapi_nve_group_cfg
*rfg
,
1370 struct agg_node
*rn
, afi_t afi
,
1371 struct rfapi_descriptor
*irfd
)
1376 bgp_withdraw(irfd
->peer
, agg_node_get_prefix(rn
), /* prefix */
1378 NULL
, /* attr, ignored */
1379 afi
, SAFI_UNICAST
, ZEBRA_ROUTE_VNC_DIRECT
,
1380 BGP_ROUTE_REDISTRIBUTE
, NULL
, /* RD not used for unicast */
1381 NULL
, 0, NULL
); /* tag not used for unicast */
1386 * Caller is responsible for ensuring that the specified nve-group
1387 * was actually part of the list of exported nve groups.
1389 static void vnc_direct_bgp_del_group_afi(struct bgp
*bgp
,
1390 struct rfapi_nve_group_cfg
*rfg
,
1393 struct agg_table
*rt
= NULL
;
1394 struct agg_node
*rn
;
1395 struct rfapi_import_table
*import_table
;
1397 vnc_zlog_debug_verbose("%s: entry", __func__
);
1399 import_table
= rfg
->rfapi_import_table
;
1400 if (!import_table
) {
1401 vnc_zlog_debug_verbose(
1402 "%s: import table not defined, returning", __func__
);
1406 rt
= import_table
->imported_vpn
[afi
];
1408 if (!rfg
->nves
&& rfg
->type
!= RFAPI_GROUP_CFG_VRF
) {
1409 vnc_zlog_debug_verbose("%s: no NVEs in this group", __func__
);
1414 * Walk the NVE-Group's VNC Import table
1416 for (rn
= agg_route_top(rt
); rn
; rn
= agg_route_next(rn
))
1418 if (rfg
->type
== RFAPI_GROUP_CFG_VRF
)
1419 vnc_direct_del_rn_group_rd(bgp
, rfg
, rn
, afi
,
1422 struct listnode
*ln
;
1425 * For each NVE that is assigned to the export
1428 * a route with that NVE as its next hop
1430 for (ln
= listhead(rfg
->nves
); ln
;
1431 ln
= listnextnode(ln
))
1432 vnc_direct_del_rn_group_rd(
1440 * Caller is responsible for ensuring that the specified nve-group
1441 * was actually part of the list of exported nve groups.
1443 void vnc_direct_bgp_del_group(struct bgp
*bgp
, struct rfapi_nve_group_cfg
*rfg
)
1445 vnc_direct_bgp_del_group_afi(bgp
, rfg
, AFI_IP
);
1446 vnc_direct_bgp_del_group_afi(bgp
, rfg
, AFI_IP6
);
1449 void vnc_direct_bgp_reexport_group_afi(struct bgp
*bgp
,
1450 struct rfapi_nve_group_cfg
*rfg
,
1453 struct listnode
*node
;
1454 struct rfapi_rfg_name
*rfgn
;
1456 if (VNC_EXPORT_BGP_GRP_ENABLED(bgp
->rfapi_cfg
)) {
1458 * look in the list of currently-exported groups
1460 for (ALL_LIST_ELEMENTS_RO(
1461 bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
, node
,
1464 if (rfgn
->rfg
== rfg
) {
1466 * If it matches, reexport it
1468 vnc_direct_bgp_del_group_afi(bgp
, rfg
, afi
);
1469 vnc_direct_bgp_add_group_afi(bgp
, rfg
, afi
);
1477 static void vnc_direct_bgp_unexport_table(afi_t afi
, struct agg_table
*rt
,
1478 struct list
*nve_list
)
1482 struct agg_node
*rn
;
1484 for (rn
= agg_route_top(rt
); rn
; rn
= agg_route_next(rn
)) {
1488 struct listnode
*hln
;
1489 struct rfapi_descriptor
*irfd
;
1491 for (ALL_LIST_ELEMENTS_RO(nve_list
, hln
,
1494 bgp_withdraw(irfd
->peer
,
1495 agg_node_get_prefix(rn
),
1497 NULL
, /* attr, ignored */
1499 ZEBRA_ROUTE_VNC_DIRECT
,
1500 BGP_ROUTE_REDISTRIBUTE
,
1501 NULL
, /* RD not used for
1503 NULL
, 0, NULL
); /* tag not
1514 static void import_table_to_nve_list_direct_bgp(struct bgp
*bgp
,
1515 struct rfapi_import_table
*it
,
1519 struct listnode
*node
;
1520 struct rfapi_rfg_name
*rfgn
;
1523 * Loop over the list of NVE-Groups configured for
1524 * exporting to direct-bgp.
1526 * Build a list of NVEs that use this import table
1529 for (ALL_LIST_ELEMENTS_RO(bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
, node
,
1533 * If this NVE-Group's import table matches the current one
1535 if (rfgn
->rfg
&& rfgn
->rfg
->rfapi_import_table
== it
) {
1536 if (rfgn
->rfg
->nves
)
1537 nve_group_to_nve_list(rfgn
->rfg
, nves
, family
);
1538 else if (rfgn
->rfg
->rfd
1539 && rfgn
->rfg
->type
== RFAPI_GROUP_CFG_VRF
) {
1542 listnode_add(*nves
, rfgn
->rfg
->rfd
);
1548 void vnc_direct_bgp_vpn_enable(struct bgp
*bgp
, afi_t afi
)
1550 struct listnode
*rfgn
;
1551 struct rfapi_nve_group_cfg
*rfg
;
1556 if (!VNC_EXPORT_BGP_GRP_ENABLED(bgp
->rfapi_cfg
)) {
1557 vnc_zlog_debug_verbose(
1558 "%s: export-to-bgp group mode not enabled, skipping",
1563 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
) {
1564 vnc_zlog_debug_verbose("%s: bad afi: %d", __func__
, afi
);
1569 * Policy is applied per-nve-group, so we need to iterate
1570 * over the groups to add everything.
1572 for (ALL_LIST_ELEMENTS_RO(bgp
->rfapi_cfg
->nve_groups_sequential
, rfgn
,
1576 * contains policy management
1578 vnc_direct_bgp_add_group_afi(bgp
, rfg
, afi
);
1583 void vnc_direct_bgp_vpn_disable(struct bgp
*bgp
, afi_t afi
)
1585 struct rfapi_import_table
*it
;
1586 uint8_t family
= afi2family(afi
);
1588 vnc_zlog_debug_verbose("%s: entry, afi=%d", __func__
, afi
);
1594 vnc_zlog_debug_verbose("%s: rfapi not initialized", __func__
);
1598 if (!family
|| (afi
!= AFI_IP
&& afi
!= AFI_IP6
)) {
1599 vnc_zlog_debug_verbose("%s: bad afi: %d", __func__
, afi
);
1603 for (it
= bgp
->rfapi
->imports
; it
; it
= it
->next
) {
1605 struct list
*nve_list
= NULL
;
1607 import_table_to_nve_list_direct_bgp(bgp
, it
, &nve_list
, family
);
1610 vnc_direct_bgp_unexport_table(
1611 afi
, it
->imported_vpn
[afi
], nve_list
);
1612 list_delete(&nve_list
);
1618 /***********************************************************************
1619 * Export methods that proxy nexthop END
1620 ***********************************************************************/
1623 /***********************************************************************
1624 * Export methods that preserve original nexthop BEGIN
1625 * rh = "registering nve"
1626 ***********************************************************************/
1630 * "Adding a Route" export process
1631 * TBD do we need to check bpi->type and bpi->sub_type here, or does
1634 void vnc_direct_bgp_rh_add_route(struct bgp
*bgp
, afi_t afi
,
1635 const struct prefix
*prefix
, struct peer
*peer
,
1638 struct vnc_export_info
*eti
;
1640 struct rfapi_cfg
*hc
;
1644 flog_err(EC_LIB_DEVELOPMENT
, "%s: can't get afi of route node",
1649 /* check bgp redist flag for vnc direct ("vpn") routes */
1650 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
1651 vnc_zlog_debug_verbose(
1652 "%s: bgp redistribution of VNC direct routes is off",
1657 if (!(hc
= bgp
->rfapi_cfg
)) {
1658 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
1663 if (!VNC_EXPORT_BGP_RH_ENABLED(bgp
->rfapi_cfg
)) {
1664 vnc_zlog_debug_verbose(
1665 "%s: export-to-bgp RH mode not enabled, skipping",
1673 if (hc
->plist_export_bgp
[afi
]) {
1674 if (prefix_list_apply(hc
->plist_export_bgp
[afi
], prefix
)
1680 * Construct new attribute set with NVE's VN addr as
1681 * nexthop and without Tunnel Encap attr
1683 if (encap_attr_export(&hattr
, attr
, NULL
, NULL
))
1685 if (hc
->routemap_export_bgp
) {
1686 struct bgp_path_info info
;
1687 route_map_result_t ret
;
1689 memset(&info
, 0, sizeof(info
));
1692 ret
= route_map_apply(hc
->routemap_export_bgp
, prefix
, &info
);
1693 if (ret
== RMAP_DENYMATCH
) {
1694 bgp_attr_flush(&hattr
);
1699 iattr
= bgp_attr_intern(&hattr
);
1700 bgp_attr_flush(&hattr
);
1703 * record route information that we will need to expire
1706 eti
= vnc_eti_get(bgp
, EXPORT_TYPE_BGP
, prefix
, peer
,
1707 ZEBRA_ROUTE_VNC_DIRECT_RH
, BGP_ROUTE_REDISTRIBUTE
);
1708 rfapiGetVncLifetime(attr
, &eti
->lifetime
);
1709 eti
->lifetime
= rfapiGetHolddownFromLifetime(eti
->lifetime
);
1712 * export expiration timer is already running on
1713 * this route: cancel it
1715 THREAD_OFF(eti
->timer
);
1717 bgp_update(peer
, prefix
, /* prefix */
1719 iattr
, /* bgp_update copies this attr */
1720 afi
, SAFI_UNICAST
, ZEBRA_ROUTE_VNC_DIRECT_RH
,
1721 BGP_ROUTE_REDISTRIBUTE
, NULL
, /* RD not used for unicast */
1722 NULL
, /* tag not used for unicast, EVPN neither */
1723 0, 0, NULL
); /* EVPN not used */
1724 bgp_attr_unintern(&iattr
);
1727 static void vncExportWithdrawTimer(struct thread
*t
)
1729 struct vnc_export_info
*eti
= THREAD_ARG(t
);
1730 const struct prefix
*p
= agg_node_get_prefix(eti
->node
);
1733 * withdraw the route
1735 bgp_withdraw(eti
->peer
, p
, 0, /* addpath_id */
1736 NULL
, /* attr, ignored */
1737 family2afi(p
->family
), SAFI_UNICAST
, eti
->type
,
1738 eti
->subtype
, NULL
, /* RD not used for unicast */
1740 NULL
); /* tag not used for unicast, EVPN neither */
1745 vnc_eti_delete(eti
);
1749 * "Withdrawing a Route" export process
1750 * TBD do we need to check bpi->type and bpi->sub_type here, or does
1753 void vnc_direct_bgp_rh_del_route(struct bgp
*bgp
, afi_t afi
,
1754 const struct prefix
*prefix
, struct peer
*peer
)
1756 struct vnc_export_info
*eti
;
1759 flog_err(EC_LIB_DEVELOPMENT
, "%s: can't get afi route node",
1764 /* check bgp redist flag for vnc direct ("vpn") routes */
1765 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
1766 vnc_zlog_debug_verbose(
1767 "%s: bgp redistribution of VNC direct routes is off",
1772 if (!bgp
->rfapi_cfg
) {
1773 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
1777 if (!VNC_EXPORT_BGP_RH_ENABLED(bgp
->rfapi_cfg
)) {
1778 vnc_zlog_debug_verbose(
1779 "%s: export-to-bgp group mode not enabled, skipping",
1784 eti
= vnc_eti_get(bgp
, EXPORT_TYPE_BGP
, prefix
, peer
,
1785 ZEBRA_ROUTE_VNC_DIRECT_RH
, BGP_ROUTE_REDISTRIBUTE
);
1787 if (!eti
->timer
&& eti
->lifetime
<= INT32_MAX
) {
1789 thread_add_timer(bm
->master
, vncExportWithdrawTimer
, eti
,
1790 eti
->lifetime
, &eti
->timer
);
1791 vnc_zlog_debug_verbose(
1792 "%s: set expiration timer for %u seconds", __func__
,
1798 void vnc_direct_bgp_rh_vpn_enable(struct bgp
*bgp
, afi_t afi
)
1800 struct prefix_rd prd
;
1801 struct bgp_dest
*pdest
;
1802 struct rfapi_cfg
*hc
;
1804 vnc_zlog_debug_verbose("%s: entry, afi=%d", __func__
, afi
);
1809 if (!(hc
= bgp
->rfapi_cfg
))
1812 if (!VNC_EXPORT_BGP_RH_ENABLED(bgp
->rfapi_cfg
)) {
1813 vnc_zlog_debug_verbose(
1814 "%s: export of RH routes not enabled, skipping",
1819 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
) {
1820 vnc_zlog_debug_verbose("%s: bad afi: %d", __func__
, afi
);
1825 * Go through the entire BGP VPN table and export to BGP unicast.
1828 vnc_zlog_debug_verbose("%s: starting RD loop", __func__
);
1830 /* Loop over all the RDs */
1831 for (pdest
= bgp_table_top(bgp
->rib
[afi
][SAFI_MPLS_VPN
]); pdest
;
1832 pdest
= bgp_route_next(pdest
)) {
1834 struct bgp_table
*table
;
1835 struct bgp_dest
*dest
;
1836 struct bgp_path_info
*ri
;
1837 const struct prefix
*pdest_p
= bgp_dest_get_prefix(pdest
);
1839 memset(&prd
, 0, sizeof(prd
));
1840 prd
.family
= AF_UNSPEC
;
1842 memcpy(prd
.val
, pdest_p
->u
.val
, 8);
1844 /* This is the per-RD table of prefixes */
1845 table
= bgp_dest_get_bgp_table_info(pdest
);
1850 for (dest
= bgp_table_top(table
); dest
;
1851 dest
= bgp_route_next(dest
)) {
1852 const struct prefix
*dest_p
;
1855 * skip prefix list check if no routes here
1857 if (!bgp_dest_has_bgp_path_info_data(dest
))
1860 vnc_zlog_debug_verbose("%s: checking prefix %pBD",
1863 dest_p
= bgp_dest_get_prefix(dest
);
1868 if (hc
->plist_export_bgp
[afi
]) {
1869 if (prefix_list_apply(hc
->plist_export_bgp
[afi
],
1873 vnc_zlog_debug_verbose(
1874 "%s: prefix list says DENY",
1880 for (ri
= bgp_dest_get_bgp_path_info(dest
); ri
;
1883 vnc_zlog_debug_verbose("%s: ri->sub_type: %d",
1884 __func__
, ri
->sub_type
);
1886 if (ri
->sub_type
== BGP_ROUTE_NORMAL
1887 || ri
->sub_type
== BGP_ROUTE_RFP
) {
1889 struct vnc_export_info
*eti
;
1894 * Construct new attribute set with
1896 * nexthop and without Tunnel Encap attr
1898 if (encap_attr_export(&hattr
, ri
->attr
,
1900 vnc_zlog_debug_verbose(
1901 "%s: encap_attr_export failed",
1906 if (hc
->routemap_export_bgp
) {
1907 struct bgp_path_info info
;
1908 route_map_result_t ret
;
1910 memset(&info
, 0, sizeof(info
));
1911 info
.peer
= ri
->peer
;
1913 ret
= route_map_apply(
1914 hc
->routemap_export_bgp
,
1916 if (ret
== RMAP_DENYMATCH
) {
1917 bgp_attr_flush(&hattr
);
1918 vnc_zlog_debug_verbose(
1919 "%s: route map says DENY",
1925 iattr
= bgp_attr_intern(&hattr
);
1926 bgp_attr_flush(&hattr
);
1929 * record route information that we will
1934 bgp
, EXPORT_TYPE_BGP
, dest_p
,
1936 ZEBRA_ROUTE_VNC_DIRECT_RH
,
1937 BGP_ROUTE_REDISTRIBUTE
);
1938 rfapiGetVncLifetime(ri
->attr
,
1942 * export expiration timer is
1943 * already running on
1944 * this route: cancel it
1946 THREAD_OFF(eti
->timer
);
1948 vnc_zlog_debug_verbose(
1949 "%s: calling bgp_update",
1953 ri
->peer
, dest_p
, /* prefix */
1955 iattr
, /* bgp_update copies
1957 AFI_IP
, SAFI_UNICAST
,
1958 ZEBRA_ROUTE_VNC_DIRECT_RH
,
1959 BGP_ROUTE_REDISTRIBUTE
, NULL
,
1960 /* RD not used for unicast */
1962 /* tag not used for unicast,
1964 0, 0, NULL
); /* EVPN not used */
1966 bgp_attr_unintern(&iattr
);
1973 void vnc_direct_bgp_rh_vpn_disable(struct bgp
*bgp
, afi_t afi
)
1975 struct bgp_dest
*dest
;
1977 vnc_zlog_debug_verbose("%s: entry, afi=%d", __func__
, afi
);
1982 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
) {
1983 vnc_zlog_debug_verbose("%s: bad afi: %d", __func__
, afi
);
1988 * Go through the entire BGP unicast table and remove routes that
1989 * originated from us
1991 for (dest
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); dest
;
1992 dest
= bgp_route_next(dest
)) {
1993 const struct prefix
*dest_p
= bgp_dest_get_prefix(dest
);
1994 struct bgp_path_info
*ri
;
1995 struct bgp_path_info
*next
;
1997 for (ri
= bgp_dest_get_bgp_path_info(dest
), next
= NULL
; ri
;
2002 if (ri
->type
== ZEBRA_ROUTE_VNC_DIRECT_RH
2003 && ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
) {
2005 struct vnc_export_info
*eti
;
2008 * Delete routes immediately (no timer)
2010 eti
= vnc_eti_checktimer(
2011 bgp
, EXPORT_TYPE_BGP
, dest_p
, ri
->peer
,
2012 ZEBRA_ROUTE_VNC_DIRECT_RH
,
2013 BGP_ROUTE_REDISTRIBUTE
);
2015 THREAD_OFF(eti
->timer
);
2016 vnc_eti_delete(eti
);
2019 bgp_withdraw(ri
->peer
, dest_p
, /* prefix */
2022 AFI_IP
, SAFI_UNICAST
,
2023 ZEBRA_ROUTE_VNC_DIRECT_RH
,
2024 BGP_ROUTE_REDISTRIBUTE
,
2025 NULL
, /* RD not used for unicast */
2026 NULL
, 0, NULL
); /* tag not used for
2034 void vnc_direct_bgp_rh_reexport(struct bgp
*bgp
, afi_t afi
)
2036 if (VNC_EXPORT_BGP_RH_ENABLED(bgp
->rfapi_cfg
)) {
2037 vnc_direct_bgp_rh_vpn_disable(bgp
, afi
);
2038 vnc_direct_bgp_rh_vpn_enable(bgp
, afi
);
2042 /***********************************************************************
2043 * Generic Export methods
2044 ***********************************************************************/
2047 * Assumes the correct mode bits are already turned on. Thus it
2048 * is OK to call this function from, e.g., bgp_redistribute_set()
2049 * without caring if export is enabled or not
2051 void vnc_export_bgp_enable(struct bgp
*bgp
, afi_t afi
)
2053 if (!bgp
->rfapi_cfg
)
2056 switch (bgp
->rfapi_cfg
->flags
& BGP_VNC_CONFIG_EXPORT_BGP_MODE_BITS
) {
2057 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_NONE
:
2060 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_GRP
:
2061 vnc_direct_bgp_vpn_enable(bgp
, afi
);
2064 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_RH
:
2065 vnc_direct_bgp_rh_vpn_enable(bgp
, afi
);
2068 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_CE
:
2069 vnc_direct_bgp_vpn_enable_ce(bgp
, afi
);
2074 void vnc_export_bgp_disable(struct bgp
*bgp
, afi_t afi
)
2076 if (!bgp
->rfapi_cfg
)
2079 switch (bgp
->rfapi_cfg
->flags
& BGP_VNC_CONFIG_EXPORT_BGP_MODE_BITS
) {
2080 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_NONE
:
2083 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_GRP
:
2084 vnc_direct_bgp_vpn_disable(bgp
, afi
);
2087 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_RH
:
2088 vnc_direct_bgp_rh_vpn_disable(bgp
, afi
);
2091 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_CE
:
2092 vnc_direct_bgp_vpn_disable_ce(bgp
, afi
);
2097 void vnc_export_bgp_prechange(struct bgp
*bgp
)
2099 vnc_export_bgp_disable(bgp
, AFI_IP
);
2100 vnc_export_bgp_disable(bgp
, AFI_IP6
);
2103 void vnc_export_bgp_postchange(struct bgp
*bgp
)
2105 vnc_export_bgp_enable(bgp
, AFI_IP
);
2106 vnc_export_bgp_enable(bgp
, AFI_IP6
);
2109 void vnc_direct_bgp_reexport(struct bgp
*bgp
, afi_t afi
)
2111 vnc_export_bgp_disable(bgp
, afi
);
2112 vnc_export_bgp_enable(bgp
, afi
);