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"
37 #include "bgpd/bgpd.h"
38 #include "bgpd/bgp_ecommunity.h"
39 #include "bgpd/bgp_attr.h"
40 #include "bgpd/bgp_aspath.h"
42 #include "bgpd/rfapi/vnc_export_bgp.h"
43 #include "bgpd/rfapi/vnc_export_bgp_p.h"
44 #include "bgpd/rfapi/vnc_export_table.h"
45 #include "bgpd/rfapi/bgp_rfapi_cfg.h"
46 #include "bgpd/rfapi/rfapi.h"
47 #include "bgpd/rfapi/rfapi_import.h"
48 #include "bgpd/rfapi/rfapi_private.h"
49 #include "bgpd/rfapi/rfapi_backend.h"
50 #include "bgpd/rfapi/rfapi_vty.h"
51 #include "bgpd/rfapi/vnc_debug.h"
54 static void vnc_direct_add_rn_group_rd(struct bgp
*bgp
,
55 struct rfapi_nve_group_cfg
*rfg
,
56 struct route_node
*rn
, struct attr
*attr
,
58 struct rfapi_descriptor
*irfd
);
60 /***********************************************************************
61 * Export methods that set nexthop to CE (from 5226 roo EC) BEGIN
62 ***********************************************************************/
65 * Memory allocation approach: make a ghost attr that
66 * has non-interned parts for the modifications. ghost attr
67 * memory is allocated by caller.
69 * - extract ce (=5226) EC and use as new nexthop
70 * - strip Tunnel Encap attr
73 static void encap_attr_export_ce(struct attr
*new, struct attr
*orig
,
74 struct prefix
*use_nexthop
)
77 * Make "new" a ghost attr copy of "orig"
79 memset(new, 0, sizeof(struct attr
));
80 bgp_attr_dup(new, orig
);
85 switch (use_nexthop
->family
) {
87 new->nexthop
= use_nexthop
->u
.prefix4
;
88 new->mp_nexthop_len
= 4; /* bytes */
89 new->flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
93 new->mp_nexthop_global
= use_nexthop
->u
.prefix6
;
94 new->mp_nexthop_len
= 16; /* bytes */
105 * Note that it will be deleted when BGP sends to any eBGP
106 * peer unless PEER_FLAG_MED_UNCHANGED is set:
108 * neighbor NEIGHBOR attribute-unchanged med
110 if (!CHECK_FLAG(new->flag
, BGP_ATTR_MULTI_EXIT_DISC
)) {
111 if (CHECK_FLAG(new->flag
, BGP_ATTR_LOCAL_PREF
)) {
112 if (new->local_pref
> 255)
115 new->med
= 255 - new->local_pref
;
117 new->med
= 255; /* shouldn't happen */
119 new->flag
|= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
);
123 * "new" is now a ghost attr:
124 * - it owns an "extra" struct
125 * - it owns any non-interned parts
126 * - any references to interned parts are not counted
128 * Caller should, after using the attr, call:
129 * - bgp_attr_flush() to free non-interned parts
133 static int getce(struct bgp
*bgp
, struct attr
*attr
, struct prefix
*pfx_ce
)
137 uint16_t localadmin
= bgp
->rfapi_cfg
->resolve_nve_roo_local_admin
;
139 for (ecp
= attr
->ecommunity
->val
, i
= 0; i
< attr
->ecommunity
->size
;
140 ++i
, ecp
+= ECOMMUNITY_SIZE
) {
142 if (VNC_DEBUG(EXPORT_BGP_GETCE
)) {
144 "%s: %02x %02x %02x %02x %02x %02x %02x %02x",
145 __func__
, ecp
[0], ecp
[1], ecp
[2], ecp
[3],
146 ecp
[4], ecp
[5], ecp
[6], ecp
[7]);
152 if (ecp
[0] != 1 || ecp
[1] != 3) {
157 * Match local admin value?
159 if (ecp
[6] != ((localadmin
& 0xff00) >> 8)
160 || ecp
[7] != (localadmin
& 0xff))
163 memset((uint8_t *)pfx_ce
, 0, sizeof(*pfx_ce
));
164 memcpy(&pfx_ce
->u
.prefix4
, ecp
+ 2, 4);
165 pfx_ce
->family
= AF_INET
;
166 pfx_ce
->prefixlen
= 32;
174 void vnc_direct_bgp_add_route_ce(struct bgp
*bgp
, struct route_node
*rn
,
177 struct attr
*attr
= bi
->attr
;
178 struct peer
*peer
= bi
->peer
;
179 struct prefix
*prefix
= &rn
->p
;
180 afi_t afi
= family2afi(prefix
->family
);
181 struct bgp_node
*urn
;
182 struct bgp_info
*ubi
;
185 struct prefix ce_nexthop
;
186 struct prefix post_routemap_nexthop
;
190 zlog_err("%s: can't get afi of route node", __func__
);
194 if ((bi
->type
!= ZEBRA_ROUTE_BGP
)
195 || (bi
->sub_type
!= BGP_ROUTE_NORMAL
196 && bi
->sub_type
!= BGP_ROUTE_RFP
197 && bi
->sub_type
!= BGP_ROUTE_STATIC
)) {
199 vnc_zlog_debug_verbose(
200 "%s: wrong route type/sub_type for export, skipping",
205 /* check bgp redist flag for vnc direct ("vpn") routes */
206 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
207 vnc_zlog_debug_verbose(
208 "%s: bgp redistribution of VNC direct routes is off",
213 if (!bgp
->rfapi_cfg
) {
214 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
219 if (!VNC_EXPORT_BGP_CE_ENABLED(bgp
->rfapi_cfg
)) {
220 vnc_zlog_debug_verbose(
221 "%s: export-to-bgp ce mode not enabled, skipping",
229 if (bgp
->rfapi_cfg
->plist_export_bgp
[afi
]) {
230 if (prefix_list_apply(bgp
->rfapi_cfg
->plist_export_bgp
[afi
],
233 vnc_zlog_debug_verbose(
234 "%s: prefix list denied, skipping", __func__
);
242 * This works only for IPv4 because IPv6 addresses are too big
243 * to fit in an extended community
245 if (getce(bgp
, attr
, &ce_nexthop
)) {
246 vnc_zlog_debug_verbose("%s: EC has no encoded CE, skipping",
252 * Is this route already represented in the unicast RIB?
253 * (look up prefix; compare route type, sub_type, peer, nexthop)
255 urn
= bgp_afi_node_get(bgp
->rib
[afi
][SAFI_UNICAST
], afi
, SAFI_UNICAST
,
257 for (ubi
= urn
->info
; ubi
; ubi
= ubi
->next
) {
258 struct prefix unicast_nexthop
;
260 if (CHECK_FLAG(ubi
->flags
, BGP_INFO_REMOVED
))
263 rfapiUnicastNexthop2Prefix(afi
, ubi
->attr
, &unicast_nexthop
);
265 if (ubi
->type
== ZEBRA_ROUTE_VNC_DIRECT
266 && ubi
->sub_type
== BGP_ROUTE_REDISTRIBUTE
268 && prefix_same(&unicast_nexthop
, &ce_nexthop
)) {
270 vnc_zlog_debug_verbose(
271 "%s: already have matching exported unicast route, skipping",
278 * Construct new attribute set with CE addr as
279 * nexthop and without Tunnel Encap attr
281 encap_attr_export_ce(&hattr
, attr
, &ce_nexthop
);
282 if (bgp
->rfapi_cfg
->routemap_export_bgp
) {
283 struct bgp_info info
;
284 route_map_result_t ret
;
286 memset(&info
, 0, sizeof(info
));
289 ret
= route_map_apply(bgp
->rfapi_cfg
->routemap_export_bgp
,
290 prefix
, RMAP_BGP
, &info
);
291 if (ret
== RMAP_DENYMATCH
) {
292 bgp_attr_flush(&hattr
);
297 iattr
= bgp_attr_intern(&hattr
);
298 bgp_attr_flush(&hattr
);
301 * Rule: disallow route-map alteration of next-hop, because it
302 * would make it too difficult to keep track of the correspondence
303 * between VPN routes and unicast routes.
305 rfapiUnicastNexthop2Prefix(afi
, iattr
, &post_routemap_nexthop
);
307 if (!prefix_same(&ce_nexthop
, &post_routemap_nexthop
)) {
308 vnc_zlog_debug_verbose(
309 "%s: route-map modification of nexthop not allowed, skipping",
311 bgp_attr_unintern(&iattr
);
315 bgp_update(peer
, prefix
, 0, /* addpath_id */
316 iattr
, /* bgp_update copies this attr */
317 afi
, SAFI_UNICAST
, ZEBRA_ROUTE_VNC_DIRECT
,
318 BGP_ROUTE_REDISTRIBUTE
, NULL
, /* RD not used for unicast */
319 NULL
, 0, /* tag not used for unicast */
320 0, NULL
); /* EVPN not used */
321 bgp_attr_unintern(&iattr
);
326 * "Withdrawing a Route" export process
328 void vnc_direct_bgp_del_route_ce(struct bgp
*bgp
, struct route_node
*rn
,
331 afi_t afi
= family2afi(rn
->p
.family
);
332 struct bgp_info
*vbi
;
333 struct prefix ce_nexthop
;
336 zlog_err("%s: bad afi", __func__
);
340 /* check bgp redist flag for vnc direct ("vpn") routes */
341 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
342 vnc_zlog_debug_verbose(
343 "%s: bgp redistribution of VNC direct routes is off",
348 if (!bgp
->rfapi_cfg
) {
349 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
353 if (!VNC_EXPORT_BGP_CE_ENABLED(bgp
->rfapi_cfg
)) {
354 vnc_zlog_debug_verbose(
355 "%s: export-to-bgp ce mode not enabled, skipping",
362 * This works only for IPv4 because IPv6 addresses are too big
363 * to fit in an extended community
365 if (getce(bgp
, bi
->attr
, &ce_nexthop
)) {
366 vnc_zlog_debug_verbose("%s: EC has no encoded CE, skipping",
372 * Look for other VPN routes with same prefix, same 5226 CE,
373 * same peer. If at least one is present, don't remove the
374 * route from the unicast RIB
377 for (vbi
= rn
->info
; vbi
; vbi
= vbi
->next
) {
381 if (bi
->peer
!= vbi
->peer
)
383 if (getce(bgp
, vbi
->attr
, &ce
))
385 if (prefix_same(&ce
, &ce_nexthop
)) {
386 vnc_zlog_debug_verbose(
387 "%s: still have a route via CE, not deleting unicast",
396 bgp_withdraw(bi
->peer
, &rn
->p
, 0, /* addpath_id */
397 NULL
, /* attr, ignored */
398 afi
, SAFI_UNICAST
, ZEBRA_ROUTE_VNC_DIRECT
,
399 BGP_ROUTE_REDISTRIBUTE
, NULL
, /* RD not used for unicast */
400 NULL
, 0, NULL
); /* tag not used for unicast */
403 static void vnc_direct_bgp_vpn_enable_ce(struct bgp
*bgp
, afi_t afi
)
405 struct rfapi_cfg
*hc
;
406 struct route_node
*rn
;
409 vnc_zlog_debug_verbose("%s: entry, afi=%d", __func__
, afi
);
414 if (!(hc
= bgp
->rfapi_cfg
))
417 if (!VNC_EXPORT_BGP_CE_ENABLED(bgp
->rfapi_cfg
)) {
418 vnc_zlog_debug_verbose(
419 "%s: export of CE routes not enabled, skipping",
424 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
) {
425 vnc_zlog_debug_verbose("%s: bad afi: %d", __func__
, afi
);
430 * Go through entire ce import table and export to BGP unicast.
432 for (rn
= route_top(bgp
->rfapi
->it_ce
->imported_vpn
[afi
]); rn
;
433 rn
= route_next(rn
)) {
439 char prefixstr
[PREFIX_STRLEN
];
441 prefix2str(&rn
->p
, prefixstr
, sizeof(prefixstr
));
442 vnc_zlog_debug_verbose("%s: checking prefix %s",
443 __func__
, prefixstr
);
446 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
448 vnc_zlog_debug_verbose("%s: ri->sub_type: %d", __func__
,
451 if (ri
->sub_type
== BGP_ROUTE_NORMAL
452 || ri
->sub_type
== BGP_ROUTE_RFP
453 || ri
->sub_type
== BGP_ROUTE_STATIC
) {
455 vnc_direct_bgp_add_route_ce(bgp
, rn
, ri
);
461 static void vnc_direct_bgp_vpn_disable_ce(struct bgp
*bgp
, afi_t afi
)
465 vnc_zlog_debug_verbose("%s: entry, afi=%d", __func__
, afi
);
470 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
) {
471 vnc_zlog_debug_verbose("%s: bad afi: %d", __func__
, afi
);
476 * Go through the entire BGP unicast table and remove routes that
479 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
;
480 rn
= bgp_route_next(rn
)) {
483 struct bgp_info
*next
;
485 for (ri
= rn
->info
, next
= NULL
; ri
; ri
= next
) {
489 if (ri
->type
== ZEBRA_ROUTE_VNC_DIRECT
490 && ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
) {
493 ri
->peer
, &rn
->p
, /* prefix */
496 AFI_IP
, SAFI_UNICAST
,
497 ZEBRA_ROUTE_VNC_DIRECT
,
498 BGP_ROUTE_REDISTRIBUTE
,
499 NULL
, /* RD not used for unicast */
501 NULL
); /* tag not used for unicast */
507 /***********************************************************************
508 * Export methods that set nexthop to CE (from 5226 roo EC) END
509 ***********************************************************************/
511 /***********************************************************************
512 * Export methods that proxy nexthop BEGIN
513 ***********************************************************************/
515 static struct ecommunity
*vnc_route_origin_ecom(struct route_node
*rn
)
517 struct ecommunity
*new;
523 new = ecommunity_new();
525 for (bi
= rn
->info
; bi
; bi
= bi
->next
) {
527 struct ecommunity_val roec
;
529 switch (BGP_MP_NEXTHOP_FAMILY(bi
->attr
->mp_nexthop_len
)) {
531 memset(&roec
, 0, sizeof(roec
));
535 &bi
->attr
->mp_nexthop_global_in
.s_addr
, 4);
538 ecommunity_add_val(new, &roec
);
541 /* No support for IPv6 addresses in extended communities
548 ecommunity_free(&new);
555 static struct ecommunity
*vnc_route_origin_ecom_single(struct in_addr
*origin
)
557 struct ecommunity
*new;
558 struct ecommunity_val roec
;
560 memset(&roec
, 0, sizeof(roec
));
563 memcpy(roec
.val
+ 2, &origin
->s_addr
, 4);
567 new = ecommunity_new();
569 ecommunity_add_val(new, &roec
);
572 ecommunity_free(&new);
581 * New memory allocation approach: make a ghost attr that
582 * has non-interned parts for the modifications. ghost attr
583 * memory is allocated by caller.
586 encap_attr_export(struct attr
*new, struct attr
*orig
,
587 struct prefix
*new_nexthop
,
588 struct route_node
*rn
) /* for VN addrs for ecom list */
589 /* if rn is 0, use route's nexthop */
591 struct prefix orig_nexthop
;
592 struct prefix
*use_nexthop
;
593 static struct ecommunity
*ecom_ro
;
596 use_nexthop
= new_nexthop
;
598 use_nexthop
= &orig_nexthop
;
599 orig_nexthop
.family
=
600 BGP_MP_NEXTHOP_FAMILY(orig
->mp_nexthop_len
);
601 if (orig_nexthop
.family
== AF_INET
) {
602 orig_nexthop
.prefixlen
= 32;
603 orig_nexthop
.u
.prefix4
= orig
->mp_nexthop_global_in
;
604 } else if (orig_nexthop
.family
== AF_INET6
) {
605 orig_nexthop
.prefixlen
= 128;
606 orig_nexthop
.u
.prefix6
= orig
->mp_nexthop_global
;
608 return -1; /* FAIL - can't compute nexthop */
614 * Make "new" a ghost attr copy of "orig"
616 memset(new, 0, sizeof(struct attr
));
617 bgp_attr_dup(new, orig
);
622 switch (use_nexthop
->family
) {
624 new->nexthop
= use_nexthop
->u
.prefix4
;
625 new->mp_nexthop_len
= 4; /* bytes */
626 new->flag
|= ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
);
630 new->mp_nexthop_global
= use_nexthop
->u
.prefix6
;
631 new->mp_nexthop_len
= 16; /* bytes */
640 ecom_ro
= vnc_route_origin_ecom(rn
);
642 /* TBD use lcom for IPv6 */
643 ecom_ro
= vnc_route_origin_ecom_single(&use_nexthop
->u
.prefix4
);
645 if (new->ecommunity
) {
648 ecommunity_merge(ecom_ro
, new->ecommunity
);
650 new->ecommunity
= ecom_ro
;
653 new->flag
|= ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES
);
659 * Note that it will be deleted when BGP sends to any eBGP
660 * peer unless PEER_FLAG_MED_UNCHANGED is set:
662 * neighbor NEIGHBOR attribute-unchanged med
664 if (!CHECK_FLAG(new->flag
, BGP_ATTR_MULTI_EXIT_DISC
)) {
665 if (CHECK_FLAG(new->flag
, BGP_ATTR_LOCAL_PREF
)) {
666 if (new->local_pref
> 255)
669 new->med
= 255 - new->local_pref
;
671 new->med
= 255; /* shouldn't happen */
673 new->flag
|= ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
);
677 * "new" is now a ghost attr:
678 * - it owns an "extra" struct
679 * - it owns any non-interned parts
680 * - any references to interned parts are not counted
682 * Caller should, after using the attr, call:
683 * - bgp_attr_flush() to free non-interned parts
690 * "Adding a Route" export process
692 void vnc_direct_bgp_add_prefix(struct bgp
*bgp
,
693 struct rfapi_import_table
*import_table
,
694 struct route_node
*rn
)
696 struct attr attr
= {0};
697 struct listnode
*node
, *nnode
;
698 struct rfapi_rfg_name
*rfgn
;
699 afi_t afi
= family2afi(rn
->p
.family
);
702 zlog_err("%s: can't get afi of route node", __func__
);
706 /* check bgp redist flag for vnc direct ("vpn") routes */
707 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
708 vnc_zlog_debug_verbose(
709 "%s: bgp redistribution of VNC direct routes is off",
714 if (!bgp
->rfapi_cfg
) {
715 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
720 if (!VNC_EXPORT_BGP_GRP_ENABLED(bgp
->rfapi_cfg
)) {
721 vnc_zlog_debug_verbose(
722 "%s: export-to-bgp group mode not enabled, skipping",
727 if (!listcount(bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
)) {
728 vnc_zlog_debug_verbose(
729 "%s: no bgp-direct export nve group, skipping",
734 bgp_attr_default_set(&attr
, BGP_ORIGIN_INCOMPLETE
);
735 /* TBD set some configured med, see add_vnc_route() */
737 vnc_zlog_debug_verbose(
738 "%s: looping over nve-groups in direct-bgp export list",
741 for (ALL_LIST_ELEMENTS(bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
, node
,
747 * If nve group is not defined yet, skip it
753 * If the nve group uses a different import table, skip it
755 if (import_table
!= rfgn
->rfg
->rfapi_import_table
)
759 * if no NVEs currently associated with this group, skip it
761 if (rfgn
->rfg
->type
!= RFAPI_GROUP_CFG_VRF
&& !rfgn
->rfg
->nves
)
765 * per-nve-group prefix list check
767 if (rfgn
->rfg
->plist_export_bgp
[afi
]) {
768 if (prefix_list_apply(rfgn
->rfg
->plist_export_bgp
[afi
],
775 if (rfgn
->rfg
->type
== RFAPI_GROUP_CFG_VRF
) {
776 vnc_direct_add_rn_group_rd(bgp
, rfgn
->rfg
, rn
, &attr
,
777 afi
, rfgn
->rfg
->rfd
);
780 * - but consistent with rest of function
785 * For each NVE that is assigned to the export nve group,
787 * a route with that NVE as its next hop
789 for (ln
= listhead(rfgn
->rfg
->nves
); ln
;
790 ln
= listnextnode(ln
)) {
791 vnc_direct_add_rn_group_rd(bgp
, rfgn
->rfg
, rn
, &attr
,
792 afi
, listgetdata(ln
));
796 aspath_unintern(&attr
.aspath
);
800 * "Withdrawing a Route" export process
802 void vnc_direct_bgp_del_prefix(struct bgp
*bgp
,
803 struct rfapi_import_table
*import_table
,
804 struct route_node
*rn
)
806 struct listnode
*node
, *nnode
;
807 struct rfapi_rfg_name
*rfgn
;
808 afi_t afi
= family2afi(rn
->p
.family
);
811 zlog_err("%s: can't get afi route node", __func__
);
815 /* check bgp redist flag for vnc direct ("vpn") routes */
816 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
817 vnc_zlog_debug_verbose(
818 "%s: bgp redistribution of VNC direct routes is off",
823 if (!bgp
->rfapi_cfg
) {
824 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
829 if (!VNC_EXPORT_BGP_GRP_ENABLED(bgp
->rfapi_cfg
)) {
830 vnc_zlog_debug_verbose(
831 "%s: export-to-bgp group mode not enabled, skipping",
836 if (!listcount(bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
)) {
837 vnc_zlog_debug_verbose(
838 "%s: no bgp-direct export nve group, skipping",
843 for (ALL_LIST_ELEMENTS(bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
, node
,
849 * If nve group is not defined yet, skip it
855 * if no NVEs currently associated with this group, skip it
857 if (rfgn
->rfg
->type
!= RFAPI_GROUP_CFG_VRF
&& !rfgn
->rfg
->nves
)
861 * If the nve group uses a different import table,
864 if (import_table
!= rfgn
->rfg
->rfapi_import_table
)
867 if (rfgn
->rfg
->type
== RFAPI_GROUP_CFG_VRF
) {
869 struct rfapi_descriptor
*irfd
;
871 irfd
= rfgn
->rfg
->rfd
;
873 if (rfapiRaddr2Qprefix(&irfd
->vn_addr
, &nhp
))
876 bgp_withdraw(irfd
->peer
, &rn
->p
, /* prefix */
878 NULL
, /* attr, ignored */
879 afi
, SAFI_UNICAST
, ZEBRA_ROUTE_VNC_DIRECT
,
880 BGP_ROUTE_REDISTRIBUTE
,
881 NULL
, /* RD not used for unicast */
883 NULL
); /* tag not used for unicast */
886 * - but consistent with rest of function
891 * For each NVE that is assigned to the export nve group,
893 * a route with that NVE as its next hop
895 for (ln
= listhead(rfgn
->rfg
->nves
); ln
;
896 ln
= listnextnode(ln
)) {
899 struct rfapi_descriptor
*irfd
;
901 irfd
= listgetdata(ln
);
903 if (rfapiRaddr2Qprefix(&irfd
->vn_addr
, &nhp
))
906 bgp_withdraw(irfd
->peer
, &rn
->p
, /* prefix */
908 NULL
, /* attr, ignored */
909 afi
, SAFI_UNICAST
, ZEBRA_ROUTE_VNC_DIRECT
,
910 BGP_ROUTE_REDISTRIBUTE
,
911 NULL
, /* RD not used for unicast */
913 NULL
); /* tag not used for unicast */
918 void vnc_direct_bgp_add_nve(struct bgp
*bgp
, struct rfapi_descriptor
*rfd
)
920 struct listnode
*node
, *nnode
;
921 struct rfapi_rfg_name
*rfgn
;
922 struct rfapi_nve_group_cfg
*rfg
= rfd
->rfg
;
923 afi_t afi
= family2afi(rfd
->vn_addr
.addr_family
);
926 zlog_err("%s: can't get afi of nve vn addr", __func__
);
932 if (!bgp
->rfapi_cfg
) {
933 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
937 if (!VNC_EXPORT_BGP_GRP_ENABLED(bgp
->rfapi_cfg
)) {
938 vnc_zlog_debug_verbose(
939 "%s: export-to-bgp group mode not enabled, skipping",
944 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
945 vnc_zlog_debug_verbose(
946 "%s: bgp redistribution of VNC direct routes is off",
952 * Loop over the list of NVE-Groups configured for
953 * exporting to direct-bgp and see if this new NVE's
954 * group is among them.
956 for (ALL_LIST_ELEMENTS(bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
, node
,
960 * Yes, this NVE's group is configured for export to direct-bgp
962 if (rfgn
->rfg
== rfg
) {
964 struct route_table
*rt
= NULL
;
965 struct route_node
*rn
;
966 struct attr attr
= {0};
967 struct rfapi_import_table
*import_table
;
970 import_table
= rfg
->rfapi_import_table
;
972 bgp_attr_default_set(&attr
, BGP_ORIGIN_INCOMPLETE
);
973 /* TBD set some configured med, see add_vnc_route() */
975 if (afi
== AFI_IP
|| afi
== AFI_IP6
) {
976 rt
= import_table
->imported_vpn
[afi
];
978 zlog_err("%s: bad afi %d", __func__
, afi
);
983 * Walk the NVE-Group's VNC Import table
985 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
)) {
990 struct rfapi_descriptor
*irfd
= rfd
;
993 struct bgp_info info
;
995 if (rfapiRaddr2Qprefix(&irfd
->vn_addr
,
1000 * per-nve-group prefix list check
1002 if (rfgn
->rfg
->plist_export_bgp
[afi
]) {
1003 if (prefix_list_apply(
1004 rfgn
->rfg
->plist_export_bgp
1014 * Construct new attribute set with
1016 * nexthop and without Tunnel Encap attr
1018 if (encap_attr_export(&hattr
, &attr
,
1022 if (rfgn
->rfg
->routemap_export_bgp
) {
1023 route_map_result_t ret
;
1024 info
.peer
= irfd
->peer
;
1026 ret
= route_map_apply(
1028 ->routemap_export_bgp
,
1031 if (ret
== RMAP_DENYMATCH
) {
1032 bgp_attr_flush(&hattr
);
1037 iattr
= bgp_attr_intern(&hattr
);
1038 bgp_attr_flush(&hattr
);
1040 irfd
->peer
, &rn
->p
, /* prefix */
1042 iattr
, /* bgp_update copies
1045 ZEBRA_ROUTE_VNC_DIRECT
,
1046 BGP_ROUTE_REDISTRIBUTE
, NULL
,
1047 /* RD not used for unicast */
1049 /* tag not used for unicast */
1050 0, 0, NULL
); /* EVPN not used */
1052 bgp_attr_unintern(&iattr
);
1056 aspath_unintern(&attr
.aspath
);
1062 void vnc_direct_bgp_del_nve(struct bgp
*bgp
, struct rfapi_descriptor
*rfd
)
1064 struct listnode
*node
, *nnode
;
1065 struct rfapi_rfg_name
*rfgn
;
1066 struct rfapi_nve_group_cfg
*rfg
= rfd
->rfg
;
1067 afi_t afi
= family2afi(rfd
->vn_addr
.addr_family
);
1070 zlog_err("%s: can't get afi of nve vn addr", __func__
);
1076 if (!bgp
->rfapi_cfg
) {
1077 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
1081 if (!VNC_EXPORT_BGP_GRP_ENABLED(bgp
->rfapi_cfg
)) {
1082 vnc_zlog_debug_verbose(
1083 "%s: export-to-bgp group mode not enabled, skipping",
1088 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
1089 vnc_zlog_debug_verbose(
1090 "%s: bgp redistribution of VNC direct routes is off",
1096 * Loop over the list of NVE-Groups configured for
1097 * exporting to direct-bgp and see if this new NVE's
1098 * group is among them.
1100 for (ALL_LIST_ELEMENTS(bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
, node
,
1104 * Yes, this NVE's group is configured for export to direct-bgp
1106 if (rfg
&& rfgn
->rfg
== rfg
) {
1108 struct route_table
*rt
= NULL
;
1109 struct route_node
*rn
;
1110 struct rfapi_import_table
*import_table
;
1112 import_table
= rfg
->rfapi_import_table
;
1114 if (afi
== AFI_IP
|| afi
== AFI_IP6
) {
1115 rt
= import_table
->imported_vpn
[afi
];
1117 zlog_err("%s: bad afi %d", __func__
, afi
);
1122 * Walk the NVE-Group's VNC Import table
1124 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
)) {
1129 struct rfapi_descriptor
*irfd
= rfd
;
1131 if (rfapiRaddr2Qprefix(&irfd
->vn_addr
,
1135 bgp_withdraw(irfd
->peer
,
1136 &rn
->p
, /* prefix */
1138 NULL
, /* attr, ignored */
1140 ZEBRA_ROUTE_VNC_DIRECT
,
1141 BGP_ROUTE_REDISTRIBUTE
,
1142 NULL
, /* RD not used for
1144 NULL
, 0, NULL
); /* tag not
1153 static void vnc_direct_add_rn_group_rd(struct bgp
*bgp
,
1154 struct rfapi_nve_group_cfg
*rfg
,
1155 struct route_node
*rn
, struct attr
*attr
,
1156 afi_t afi
, struct rfapi_descriptor
*irfd
)
1159 struct bgp_info info
;
1163 if (irfd
== NULL
&& rfg
->type
!= RFAPI_GROUP_CFG_VRF
) {
1164 /* need new rfapi_handle, for peer strcture
1165 * -- based on vnc_add_vrf_prefi */
1166 assert(rfg
->rfd
== NULL
);
1168 if (!rfg
->rt_export_list
|| !rfg
->rfapi_import_table
) {
1169 vnc_zlog_debug_verbose(
1170 "%s: VRF \"%s\" is missing RT import/export configuration.\n",
1171 __func__
, rfg
->name
);
1174 if (!rfg
->rd
.prefixlen
) {
1175 vnc_zlog_debug_verbose(
1176 "%s: VRF \"%s\" is missing RD configuration.\n",
1177 __func__
, rfg
->name
);
1180 if (rfg
->label
> MPLS_LABEL_MAX
) {
1181 vnc_zlog_debug_verbose(
1182 "%s: VRF \"%s\" is missing defaul label configuration.\n",
1183 __func__
, rfg
->name
);
1187 irfd
= XCALLOC(MTYPE_RFAPI_DESC
,
1188 sizeof(struct rfapi_descriptor
));
1192 * leave most fields empty as will get from (dynamic) config
1195 irfd
->default_tunneltype_option
.type
= BGP_ENCAP_TYPE_MPLS
;
1197 if (rfg
->vn_prefix
.family
1198 && !CHECK_FLAG(rfg
->flags
, RFAPI_RFG_VPN_NH_SELF
)) {
1199 rfapiQprefix2Raddr(&rfg
->vn_prefix
, &irfd
->vn_addr
);
1201 memset(&irfd
->vn_addr
, 0, sizeof(struct rfapi_ip_addr
));
1202 irfd
->vn_addr
.addr_family
= AF_INET
;
1203 irfd
->vn_addr
.addr
.v4
= bgp
->router_id
;
1205 irfd
->un_addr
= irfd
->vn_addr
; /* sigh, need something in UN for
1207 vnc_zlog_debug_verbose("%s: Opening RFD for VRF %s", __func__
,
1209 rfapi_init_and_open(bgp
, irfd
, rfg
);
1212 if (irfd
== NULL
|| rfapiRaddr2Qprefix(&irfd
->vn_addr
, &nhp
))
1216 * Construct new attribute set with NVE's VN
1218 * nexthop and without Tunnel Encap attr
1220 if (encap_attr_export(&hattr
, attr
, &nhp
, rn
))
1223 if (VNC_DEBUG(EXPORT_BGP_DIRECT_ADD
)) {
1224 vnc_zlog_debug_any("%s: attr follows", __func__
);
1225 rfapiPrintAttrPtrs(NULL
, attr
);
1226 vnc_zlog_debug_any("%s: hattr follows", __func__
);
1227 rfapiPrintAttrPtrs(NULL
, &hattr
);
1230 if (rfg
->routemap_export_bgp
) {
1231 route_map_result_t ret
;
1233 info
.peer
= irfd
->peer
;
1235 ret
= route_map_apply(rfg
->routemap_export_bgp
, &rn
->p
,
1237 if (ret
== RMAP_DENYMATCH
) {
1238 bgp_attr_flush(&hattr
);
1239 vnc_zlog_debug_verbose(
1240 "%s: route map says DENY, so not calling bgp_update",
1246 if (VNC_DEBUG(EXPORT_BGP_DIRECT_ADD
)) {
1247 vnc_zlog_debug_any("%s: hattr after route_map_apply:",
1249 rfapiPrintAttrPtrs(NULL
, &hattr
);
1251 iattr
= bgp_attr_intern(&hattr
);
1252 bgp_attr_flush(&hattr
);
1254 bgp_update(irfd
->peer
, &rn
->p
, /* prefix */
1256 iattr
, /* bgp_update copies it */
1257 afi
, SAFI_UNICAST
, ZEBRA_ROUTE_VNC_DIRECT
,
1258 BGP_ROUTE_REDISTRIBUTE
, NULL
, /* RD not used for unicast */
1259 NULL
, /* tag not used for unicast */
1260 0, 0, NULL
); /* EVPN not used */
1262 bgp_attr_unintern(&iattr
);
1268 * Caller is responsible for ensuring that the specified nve-group
1269 * is actually part of the list of exported nve groups.
1271 static void vnc_direct_bgp_add_group_afi(struct bgp
*bgp
,
1272 struct rfapi_nve_group_cfg
*rfg
,
1275 struct route_table
*rt
= NULL
;
1276 struct route_node
*rn
;
1277 struct attr attr
= {0};
1278 struct rfapi_import_table
*import_table
;
1280 vnc_zlog_debug_verbose("%s: entry", __func__
);
1282 import_table
= rfg
->rfapi_import_table
;
1283 if (!import_table
) {
1284 vnc_zlog_debug_verbose(
1285 "%s: import table not defined, returning", __func__
);
1289 if (afi
== AFI_IP
|| afi
== AFI_IP6
) {
1290 rt
= import_table
->imported_vpn
[afi
];
1292 zlog_err("%s: bad afi %d", __func__
, afi
);
1296 if (!rfg
->nves
&& rfg
->type
!= RFAPI_GROUP_CFG_VRF
) {
1297 vnc_zlog_debug_verbose("%s: no NVEs in this group", __func__
);
1301 bgp_attr_default_set(&attr
, BGP_ORIGIN_INCOMPLETE
);
1302 /* TBD set some configured med, see add_vnc_route() */
1305 * Walk the NVE-Group's VNC Import table
1307 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
)) {
1311 struct listnode
*ln
;
1314 * per-nve-group prefix list check
1316 if (rfg
->plist_export_bgp
[afi
]) {
1317 if (prefix_list_apply(
1318 rfg
->plist_export_bgp
[afi
], &rn
->p
)
1323 if (rfg
->type
== RFAPI_GROUP_CFG_VRF
) {
1324 vnc_direct_add_rn_group_rd(bgp
, rfg
, rn
, &attr
,
1328 * - but consistent with rest of function
1333 * For each NVE that is assigned to the export nve
1335 * a route with that NVE as its next hop
1337 for (ln
= listhead(rfg
->nves
); ln
;
1338 ln
= listnextnode(ln
)) {
1339 vnc_direct_add_rn_group_rd(bgp
, rfg
, rn
, &attr
,
1346 aspath_unintern(&attr
.aspath
);
1351 * Caller is responsible for ensuring that the specified nve-group
1352 * is actually part of the list of exported nve groups.
1354 void vnc_direct_bgp_add_group(struct bgp
*bgp
, struct rfapi_nve_group_cfg
*rfg
)
1356 vnc_direct_bgp_add_group_afi(bgp
, rfg
, AFI_IP
);
1357 vnc_direct_bgp_add_group_afi(bgp
, rfg
, AFI_IP6
);
1360 static void vnc_direct_del_rn_group_rd(struct bgp
*bgp
,
1361 struct rfapi_nve_group_cfg
*rfg
,
1362 struct route_node
*rn
, afi_t afi
,
1363 struct rfapi_descriptor
*irfd
)
1367 bgp_withdraw(irfd
->peer
, &rn
->p
, /* prefix */
1369 NULL
, /* attr, ignored */
1370 afi
, SAFI_UNICAST
, ZEBRA_ROUTE_VNC_DIRECT
,
1371 BGP_ROUTE_REDISTRIBUTE
, NULL
, /* RD not used for unicast */
1372 NULL
, 0, NULL
); /* tag not used for unicast */
1377 * Caller is responsible for ensuring that the specified nve-group
1378 * was actually part of the list of exported nve groups.
1380 static void vnc_direct_bgp_del_group_afi(struct bgp
*bgp
,
1381 struct rfapi_nve_group_cfg
*rfg
,
1384 struct route_table
*rt
= NULL
;
1385 struct route_node
*rn
;
1386 struct rfapi_import_table
*import_table
;
1388 vnc_zlog_debug_verbose("%s: entry", __func__
);
1390 import_table
= rfg
->rfapi_import_table
;
1391 if (!import_table
) {
1392 vnc_zlog_debug_verbose(
1393 "%s: import table not defined, returning", __func__
);
1397 assert(afi
== AFI_IP
|| afi
== AFI_IP6
);
1398 rt
= import_table
->imported_vpn
[afi
];
1400 if (!rfg
->nves
&& rfg
->type
!= RFAPI_GROUP_CFG_VRF
) {
1401 vnc_zlog_debug_verbose("%s: no NVEs in this group", __func__
);
1406 * Walk the NVE-Group's VNC Import table
1408 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
))
1410 if (rfg
->type
== RFAPI_GROUP_CFG_VRF
)
1411 vnc_direct_del_rn_group_rd(bgp
, rfg
, rn
, afi
,
1414 struct listnode
*ln
;
1417 * For each NVE that is assigned to the export
1420 * a route with that NVE as its next hop
1422 for (ln
= listhead(rfg
->nves
); ln
;
1423 ln
= listnextnode(ln
))
1424 vnc_direct_del_rn_group_rd(
1432 * Caller is responsible for ensuring that the specified nve-group
1433 * was actually part of the list of exported nve groups.
1435 void vnc_direct_bgp_del_group(struct bgp
*bgp
, struct rfapi_nve_group_cfg
*rfg
)
1437 vnc_direct_bgp_del_group_afi(bgp
, rfg
, AFI_IP
);
1438 vnc_direct_bgp_del_group_afi(bgp
, rfg
, AFI_IP6
);
1441 void vnc_direct_bgp_reexport_group_afi(struct bgp
*bgp
,
1442 struct rfapi_nve_group_cfg
*rfg
,
1445 struct listnode
*node
;
1446 struct rfapi_rfg_name
*rfgn
;
1448 if (VNC_EXPORT_BGP_GRP_ENABLED(bgp
->rfapi_cfg
)) {
1450 * look in the list of currently-exported groups
1452 for (ALL_LIST_ELEMENTS_RO(
1453 bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
, node
,
1456 if (rfgn
->rfg
== rfg
) {
1458 * If it matches, reexport it
1460 vnc_direct_bgp_del_group_afi(bgp
, rfg
, afi
);
1461 vnc_direct_bgp_add_group_afi(bgp
, rfg
, afi
);
1469 static void vnc_direct_bgp_unexport_table(afi_t afi
, struct route_table
*rt
,
1470 struct list
*nve_list
)
1474 struct route_node
*rn
;
1476 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
)) {
1480 struct listnode
*hln
;
1481 struct rfapi_descriptor
*irfd
;
1483 for (ALL_LIST_ELEMENTS_RO(nve_list
, hln
,
1486 bgp_withdraw(irfd
->peer
,
1487 &rn
->p
, /* prefix */
1489 NULL
, /* attr, ignored */
1491 ZEBRA_ROUTE_VNC_DIRECT
,
1492 BGP_ROUTE_REDISTRIBUTE
,
1493 NULL
, /* RD not used for
1495 NULL
, 0, NULL
); /* tag not
1506 static void import_table_to_nve_list_direct_bgp(struct bgp
*bgp
,
1507 struct rfapi_import_table
*it
,
1511 struct listnode
*node
;
1512 struct rfapi_rfg_name
*rfgn
;
1515 * Loop over the list of NVE-Groups configured for
1516 * exporting to direct-bgp.
1518 * Build a list of NVEs that use this import table
1521 for (ALL_LIST_ELEMENTS_RO(bgp
->rfapi_cfg
->rfg_export_direct_bgp_l
, node
,
1525 * If this NVE-Group's import table matches the current one
1527 if (rfgn
->rfg
&& rfgn
->rfg
->rfapi_import_table
== it
) {
1528 if (rfgn
->rfg
->nves
)
1529 nve_group_to_nve_list(rfgn
->rfg
, nves
, family
);
1530 else if (rfgn
->rfg
->rfd
1531 && rfgn
->rfg
->type
== RFAPI_GROUP_CFG_VRF
) {
1534 listnode_add(*nves
, rfgn
->rfg
->rfd
);
1540 void vnc_direct_bgp_vpn_enable(struct bgp
*bgp
, afi_t afi
)
1542 struct listnode
*rfgn
;
1543 struct rfapi_nve_group_cfg
*rfg
;
1548 if (!VNC_EXPORT_BGP_GRP_ENABLED(bgp
->rfapi_cfg
)) {
1549 vnc_zlog_debug_verbose(
1550 "%s: export-to-bgp group mode not enabled, skipping",
1555 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
) {
1556 vnc_zlog_debug_verbose("%s: bad afi: %d", __func__
, afi
);
1561 * Policy is applied per-nve-group, so we need to iterate
1562 * over the groups to add everything.
1564 for (ALL_LIST_ELEMENTS_RO(bgp
->rfapi_cfg
->nve_groups_sequential
, rfgn
,
1568 * contains policy management
1570 vnc_direct_bgp_add_group_afi(bgp
, rfg
, afi
);
1575 void vnc_direct_bgp_vpn_disable(struct bgp
*bgp
, afi_t afi
)
1577 struct rfapi_import_table
*it
;
1578 uint8_t family
= afi2family(afi
);
1580 vnc_zlog_debug_verbose("%s: entry, afi=%d", __func__
, afi
);
1586 vnc_zlog_debug_verbose("%s: rfapi not initialized", __func__
);
1590 if (!family
|| (afi
!= AFI_IP
&& afi
!= AFI_IP6
)) {
1591 vnc_zlog_debug_verbose("%s: bad afi: %d", __func__
, afi
);
1595 for (it
= bgp
->rfapi
->imports
; it
; it
= it
->next
) {
1597 struct list
*nve_list
= NULL
;
1599 import_table_to_nve_list_direct_bgp(bgp
, it
, &nve_list
, family
);
1602 vnc_direct_bgp_unexport_table(
1603 afi
, it
->imported_vpn
[afi
], nve_list
);
1604 list_delete_and_null(&nve_list
);
1610 /***********************************************************************
1611 * Export methods that proxy nexthop END
1612 ***********************************************************************/
1615 /***********************************************************************
1616 * Export methods that preserve original nexthop BEGIN
1617 * rh = "registering nve"
1618 ***********************************************************************/
1622 * "Adding a Route" export process
1623 * TBD do we need to check bi->type and bi->sub_type here, or does
1626 void vnc_direct_bgp_rh_add_route(struct bgp
*bgp
, afi_t afi
,
1627 struct prefix
*prefix
, struct peer
*peer
,
1630 struct vnc_export_info
*eti
;
1632 struct rfapi_cfg
*hc
;
1636 zlog_err("%s: can't get afi of route node", __func__
);
1640 /* check bgp redist flag for vnc direct ("vpn") routes */
1641 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
1642 vnc_zlog_debug_verbose(
1643 "%s: bgp redistribution of VNC direct routes is off",
1648 if (!(hc
= bgp
->rfapi_cfg
)) {
1649 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
1654 if (!VNC_EXPORT_BGP_RH_ENABLED(bgp
->rfapi_cfg
)) {
1655 vnc_zlog_debug_verbose(
1656 "%s: export-to-bgp RH mode not enabled, skipping",
1664 if (hc
->plist_export_bgp
[afi
]) {
1665 if (prefix_list_apply(hc
->plist_export_bgp
[afi
], prefix
)
1671 * Construct new attribute set with NVE's VN addr as
1672 * nexthop and without Tunnel Encap attr
1674 if (encap_attr_export(&hattr
, attr
, NULL
, NULL
))
1676 if (hc
->routemap_export_bgp
) {
1677 struct bgp_info info
;
1678 route_map_result_t ret
;
1680 memset(&info
, 0, sizeof(info
));
1683 ret
= route_map_apply(hc
->routemap_export_bgp
, prefix
, RMAP_BGP
,
1685 if (ret
== RMAP_DENYMATCH
) {
1686 bgp_attr_flush(&hattr
);
1691 iattr
= bgp_attr_intern(&hattr
);
1692 bgp_attr_flush(&hattr
);
1695 * record route information that we will need to expire
1698 eti
= vnc_eti_get(bgp
, EXPORT_TYPE_BGP
, prefix
, peer
,
1699 ZEBRA_ROUTE_VNC_DIRECT_RH
, BGP_ROUTE_REDISTRIBUTE
);
1700 rfapiGetVncLifetime(attr
, &eti
->lifetime
);
1701 eti
->lifetime
= rfapiGetHolddownFromLifetime(eti
->lifetime
);
1705 * export expiration timer is already running on
1706 * this route: cancel it
1708 thread_cancel(eti
->timer
);
1712 bgp_update(peer
, prefix
, /* prefix */
1714 iattr
, /* bgp_update copies this attr */
1715 afi
, SAFI_UNICAST
, ZEBRA_ROUTE_VNC_DIRECT_RH
,
1716 BGP_ROUTE_REDISTRIBUTE
, NULL
, /* RD not used for unicast */
1717 NULL
, /* tag not used for unicast, EVPN neither */
1718 0, 0, NULL
); /* EVPN not used */
1719 bgp_attr_unintern(&iattr
);
1722 static int vncExportWithdrawTimer(struct thread
*t
)
1724 struct vnc_export_info
*eti
= t
->arg
;
1727 * withdraw the route
1729 bgp_withdraw(eti
->peer
, &eti
->node
->p
, 0, /* addpath_id */
1730 NULL
, /* attr, ignored */
1731 family2afi(eti
->node
->p
.family
), SAFI_UNICAST
, eti
->type
,
1732 eti
->subtype
, NULL
, /* RD not used for unicast */
1734 NULL
); /* tag not used for unicast, EVPN neither */
1739 vnc_eti_delete(eti
);
1745 * "Withdrawing a Route" export process
1746 * TBD do we need to check bi->type and bi->sub_type here, or does
1749 void vnc_direct_bgp_rh_del_route(struct bgp
*bgp
, afi_t afi
,
1750 struct prefix
*prefix
, struct peer
*peer
)
1752 struct vnc_export_info
*eti
;
1755 zlog_err("%s: can't get afi route node", __func__
);
1759 /* check bgp redist flag for vnc direct ("vpn") routes */
1760 if (!bgp
->redist
[afi
][ZEBRA_ROUTE_VNC_DIRECT
]) {
1761 vnc_zlog_debug_verbose(
1762 "%s: bgp redistribution of VNC direct routes is off",
1767 if (!bgp
->rfapi_cfg
) {
1768 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
1772 if (!VNC_EXPORT_BGP_RH_ENABLED(bgp
->rfapi_cfg
)) {
1773 vnc_zlog_debug_verbose(
1774 "%s: export-to-bgp group mode not enabled, skipping",
1779 eti
= vnc_eti_get(bgp
, EXPORT_TYPE_BGP
, prefix
, peer
,
1780 ZEBRA_ROUTE_VNC_DIRECT_RH
, BGP_ROUTE_REDISTRIBUTE
);
1782 if (!eti
->timer
&& eti
->lifetime
<= INT32_MAX
) {
1784 thread_add_timer(bm
->master
, vncExportWithdrawTimer
, eti
,
1785 eti
->lifetime
, &eti
->timer
);
1786 vnc_zlog_debug_verbose(
1787 "%s: set expiration timer for %u seconds", __func__
,
1793 void vnc_direct_bgp_rh_vpn_enable(struct bgp
*bgp
, afi_t afi
)
1795 struct prefix_rd prd
;
1796 struct bgp_node
*prn
;
1797 struct rfapi_cfg
*hc
;
1799 vnc_zlog_debug_verbose("%s: entry, afi=%d", __func__
, afi
);
1804 if (!(hc
= bgp
->rfapi_cfg
))
1807 if (!VNC_EXPORT_BGP_RH_ENABLED(bgp
->rfapi_cfg
)) {
1808 vnc_zlog_debug_verbose(
1809 "%s: export of RH routes not enabled, skipping",
1814 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
) {
1815 vnc_zlog_debug_verbose("%s: bad afi: %d", __func__
, afi
);
1820 * Go through the entire BGP VPN table and export to BGP unicast.
1823 vnc_zlog_debug_verbose("%s: starting RD loop", __func__
);
1825 /* Loop over all the RDs */
1826 for (prn
= bgp_table_top(bgp
->rib
[afi
][SAFI_MPLS_VPN
]); prn
;
1827 prn
= bgp_route_next(prn
)) {
1829 struct bgp_table
*table
;
1830 struct bgp_node
*rn
;
1831 struct bgp_info
*ri
;
1833 memset(&prd
, 0, sizeof(prd
));
1834 prd
.family
= AF_UNSPEC
;
1836 memcpy(prd
.val
, prn
->p
.u
.val
, 8);
1838 /* This is the per-RD table of prefixes */
1844 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
1847 * skip prefix list check if no routes here
1853 char prefixstr
[PREFIX_STRLEN
];
1855 prefix2str(&rn
->p
, prefixstr
,
1857 vnc_zlog_debug_verbose("%s: checking prefix %s",
1858 __func__
, prefixstr
);
1864 if (hc
->plist_export_bgp
[afi
]) {
1865 if (prefix_list_apply(hc
->plist_export_bgp
[afi
],
1869 vnc_zlog_debug_verbose(
1870 "%s: prefix list says DENY",
1876 for (ri
= rn
->info
; ri
; ri
= ri
->next
) {
1878 vnc_zlog_debug_verbose("%s: ri->sub_type: %d",
1879 __func__
, ri
->sub_type
);
1881 if (ri
->sub_type
== BGP_ROUTE_NORMAL
1882 || ri
->sub_type
== BGP_ROUTE_RFP
) {
1884 struct vnc_export_info
*eti
;
1889 * Construct new attribute set with
1891 * nexthop and without Tunnel Encap attr
1893 if (encap_attr_export(&hattr
, ri
->attr
,
1895 vnc_zlog_debug_verbose(
1896 "%s: encap_attr_export failed",
1901 if (hc
->routemap_export_bgp
) {
1902 struct bgp_info info
;
1903 route_map_result_t ret
;
1905 memset(&info
, 0, sizeof(info
));
1906 info
.peer
= ri
->peer
;
1908 ret
= route_map_apply(
1909 hc
->routemap_export_bgp
,
1912 if (ret
== RMAP_DENYMATCH
) {
1913 bgp_attr_flush(&hattr
);
1914 vnc_zlog_debug_verbose(
1915 "%s: route map says DENY",
1921 iattr
= bgp_attr_intern(&hattr
);
1922 bgp_attr_flush(&hattr
);
1925 * record route information that we will
1930 bgp
, EXPORT_TYPE_BGP
, &rn
->p
,
1932 ZEBRA_ROUTE_VNC_DIRECT_RH
,
1933 BGP_ROUTE_REDISTRIBUTE
);
1934 rfapiGetVncLifetime(ri
->attr
,
1939 * export expiration timer is
1940 * already running on
1941 * this route: cancel it
1943 thread_cancel(eti
->timer
);
1947 vnc_zlog_debug_verbose(
1948 "%s: calling bgp_update",
1952 ri
->peer
, &rn
->p
, /* prefix */
1954 iattr
, /* bgp_update copies
1956 AFI_IP
, SAFI_UNICAST
,
1957 ZEBRA_ROUTE_VNC_DIRECT_RH
,
1958 BGP_ROUTE_REDISTRIBUTE
, NULL
,
1959 /* RD not used for unicast */
1961 /* tag not used for unicast,
1963 0, 0, NULL
); /* EVPN not used */
1965 bgp_attr_unintern(&iattr
);
1972 void vnc_direct_bgp_rh_vpn_disable(struct bgp
*bgp
, afi_t afi
)
1974 struct bgp_node
*rn
;
1976 vnc_zlog_debug_verbose("%s: entry, afi=%d", __func__
, afi
);
1981 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
) {
1982 vnc_zlog_debug_verbose("%s: bad afi: %d", __func__
, afi
);
1987 * Go through the entire BGP unicast table and remove routes that
1988 * originated from us
1990 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
;
1991 rn
= bgp_route_next(rn
)) {
1993 struct bgp_info
*ri
;
1994 struct bgp_info
*next
;
1996 for (ri
= rn
->info
, next
= NULL
; ri
; ri
= next
) {
2000 if (ri
->type
== ZEBRA_ROUTE_VNC_DIRECT_RH
2001 && ri
->sub_type
== BGP_ROUTE_REDISTRIBUTE
) {
2003 struct vnc_export_info
*eti
;
2006 * Delete routes immediately (no timer)
2008 eti
= vnc_eti_checktimer(
2009 bgp
, EXPORT_TYPE_BGP
, &rn
->p
, ri
->peer
,
2010 ZEBRA_ROUTE_VNC_DIRECT_RH
,
2011 BGP_ROUTE_REDISTRIBUTE
);
2014 thread_cancel(eti
->timer
);
2015 vnc_eti_delete(eti
);
2018 bgp_withdraw(ri
->peer
, &rn
->p
, /* prefix */
2021 AFI_IP
, SAFI_UNICAST
,
2022 ZEBRA_ROUTE_VNC_DIRECT_RH
,
2023 BGP_ROUTE_REDISTRIBUTE
,
2024 NULL
, /* RD not used for unicast */
2025 NULL
, 0, NULL
); /* tag not used for
2033 void vnc_direct_bgp_rh_reexport(struct bgp
*bgp
, afi_t afi
)
2035 if (VNC_EXPORT_BGP_RH_ENABLED(bgp
->rfapi_cfg
)) {
2036 vnc_direct_bgp_rh_vpn_disable(bgp
, afi
);
2037 vnc_direct_bgp_rh_vpn_enable(bgp
, afi
);
2041 /***********************************************************************
2042 * Generic Export methods
2043 ***********************************************************************/
2046 * Assumes the correct mode bits are already turned on. Thus it
2047 * is OK to call this function from, e.g., bgp_redistribute_set()
2048 * without caring if export is enabled or not
2050 void vnc_export_bgp_enable(struct bgp
*bgp
, afi_t afi
)
2052 switch (bgp
->rfapi_cfg
->flags
& BGP_VNC_CONFIG_EXPORT_BGP_MODE_BITS
) {
2053 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_NONE
:
2056 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_GRP
:
2057 vnc_direct_bgp_vpn_enable(bgp
, afi
);
2060 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_RH
:
2061 vnc_direct_bgp_rh_vpn_enable(bgp
, afi
);
2064 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_CE
:
2065 vnc_direct_bgp_vpn_enable_ce(bgp
, afi
);
2070 void vnc_export_bgp_disable(struct bgp
*bgp
, afi_t afi
)
2072 switch (bgp
->rfapi_cfg
->flags
& BGP_VNC_CONFIG_EXPORT_BGP_MODE_BITS
) {
2073 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_NONE
:
2076 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_GRP
:
2077 vnc_direct_bgp_vpn_disable(bgp
, afi
);
2080 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_RH
:
2081 vnc_direct_bgp_rh_vpn_disable(bgp
, afi
);
2084 case BGP_VNC_CONFIG_EXPORT_BGP_MODE_CE
:
2085 vnc_direct_bgp_vpn_disable_ce(bgp
, afi
);
2090 void vnc_export_bgp_prechange(struct bgp
*bgp
)
2092 vnc_export_bgp_disable(bgp
, AFI_IP
);
2093 vnc_export_bgp_disable(bgp
, AFI_IP6
);
2096 void vnc_export_bgp_postchange(struct bgp
*bgp
)
2098 vnc_export_bgp_enable(bgp
, AFI_IP
);
2099 vnc_export_bgp_enable(bgp
, AFI_IP6
);
2102 void vnc_direct_bgp_reexport(struct bgp
*bgp
, afi_t afi
)
2104 vnc_export_bgp_disable(bgp
, afi
);
2105 vnc_export_bgp_enable(bgp
, afi
);