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_import_bgp.c
23 * Purpose: Import routes from BGP unicast directly (not via zebra)
26 #include "lib/zebra.h"
27 #include "lib/prefix.h"
28 #include "lib/agg_table.h"
31 #include "lib/memory.h"
32 #include "lib/linklist.h"
33 #include "lib/plist.h"
34 #include "lib/routemap.h"
35 #include "lib/lib_errors.h"
37 #include "bgpd/bgpd.h"
38 #include "bgpd/bgp_ecommunity.h"
39 #include "bgpd/bgp_attr.h"
40 #include "bgpd/bgp_route.h"
41 #include "bgpd/bgp_mplsvpn.h" /* for RD_TYPE_IP */
43 #include "bgpd/rfapi/vnc_export_bgp.h"
44 #include "bgpd/rfapi/bgp_rfapi_cfg.h"
45 #include "bgpd/rfapi/rfapi.h"
46 #include "bgpd/rfapi/rfapi_import.h"
47 #include "bgpd/rfapi/rfapi_private.h"
48 #include "bgpd/rfapi/rfapi_monitor.h"
49 #include "bgpd/rfapi/rfapi_vty.h"
50 #include "bgpd/rfapi/vnc_import_bgp.h"
51 #include "bgpd/rfapi/vnc_import_bgp_p.h"
52 #include "bgpd/rfapi/vnc_debug.h"
54 #define ENABLE_VNC_RHNCK
56 #define DEBUG_RHN_LIST 0
58 static struct rfapi_descriptor vncHDBgpDirect
; /* dummy nve descriptor */
59 static struct rfapi_descriptor vncHDResolveNve
; /* dummy nve descriptor */
62 * For routes from another AS:
65 * LOCAL_PREF = 255 - MIN(255, MED)
67 * LOCAL_PREF = default_local_pref
69 * For routes from the same AS:
71 * LOCAL_PREF unchanged
73 uint32_t calc_local_pref(struct attr
*attr
, struct peer
*peer
)
75 uint32_t local_pref
= 0;
79 return peer
->bgp
->default_local_pref
;
81 return bgp_get_default()->default_local_pref
;
84 if (peer
&& (peer
->as
!= peer
->bgp
->as
)) {
85 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
)) {
86 if (attr
->med
> 255) {
89 local_pref
= 255 - attr
->med
;
92 local_pref
= peer
->bgp
->default_local_pref
;
95 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)) {
96 local_pref
= attr
->local_pref
;
98 if (peer
&& peer
->bgp
) {
99 local_pref
= peer
->bgp
->default_local_pref
;
107 static int is_host_prefix(const struct prefix
*p
)
111 return (p
->prefixlen
== 32);
113 return (p
->prefixlen
== 128);
118 /***********************************************************************
120 ***********************************************************************/
123 struct prefix hpfx
; /* ce address = unicast nexthop */
124 struct prefix upfx
; /* unicast prefix */
125 struct bgp_path_info
*ubpi
; /* unicast route */
128 static const uint8_t maskbit
[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0,
129 0xf8, 0xfc, 0xfe, 0xff};
131 int vnc_prefix_cmp(const void *pfx1
, const void *pfx2
)
137 const struct prefix
*p1
= pfx1
;
138 const struct prefix
*p2
= pfx2
;
140 if (p1
->family
< p2
->family
)
142 if (p1
->family
> p2
->family
)
145 if (p1
->prefixlen
< p2
->prefixlen
)
147 if (p1
->prefixlen
> p2
->prefixlen
)
150 offset
= p1
->prefixlen
/ 8;
151 shift
= p1
->prefixlen
% 8;
152 if (shift
== 0 && offset
) { /* catch aligned case */
157 /* Set both prefix's head pointer. */
158 const uint8_t *pp1
= (const uint8_t *)&p1
->u
.prefix
;
159 const uint8_t *pp2
= (const uint8_t *)&p2
->u
.prefix
;
170 mask
= maskbit
[shift
];
171 if ((*pp1
& mask
) < (*pp2
& mask
))
173 if ((*pp1
& mask
) > (*pp2
& mask
))
179 static void prefix_bag_free(void *pb
)
181 XFREE(MTYPE_RFAPI_PREFIX_BAG
, pb
);
185 static void print_rhn_list(const char *tag1
, const char *tag2
)
189 struct skiplistnode
*p
;
190 struct prefix_bag
*pb
;
193 bgp
= bgp_get_default();
197 sl
= bgp
->frapi
->resolve_nve_nexthop
;
199 vnc_zlog_debug_verbose("%s: %s: RHN List is empty",
200 (tag1
? tag1
: ""), (tag2
? tag2
: ""));
204 vnc_zlog_debug_verbose("%s: %s: RHN list:", (tag1
? tag1
: ""),
207 /* XXX uses secret knowledge of skiplist structure */
208 for (p
= sl
->header
->forward
[0]; p
; p
= p
->forward
[0]) {
209 char kbuf
[PREFIX_STRLEN
];
210 char hbuf
[PREFIX_STRLEN
];
211 char ubuf
[PREFIX_STRLEN
];
215 prefix2str(p
->key
, kbuf
, sizeof(kbuf
));
216 prefix2str(&pb
->hpfx
, hbuf
, sizeof(hbuf
));
217 prefix2str(&pb
->upfx
, ubuf
, sizeof(ubuf
));
219 vnc_zlog_debug_verbose(
220 "RHN Entry %d (q=%p): kpfx=%s, upfx=%s, hpfx=%s, ubpi=%p",
221 ++count
, p
, kbuf
, ubuf
, hbuf
, pb
->ubpi
);
226 #ifdef ENABLE_VNC_RHNCK
227 static void vnc_rhnck(char *tag
)
231 struct skiplistnode
*p
;
233 bgp
= bgp_get_default();
236 sl
= bgp
->rfapi
->resolve_nve_nexthop
;
241 /* XXX uses secret knowledge of skiplist structure */
242 for (p
= sl
->header
->forward
[0]; p
; p
= p
->forward
[0]) {
243 struct prefix_bag
*pb
;
246 struct prefix pfx_orig_nexthop
;
248 memset(&pfx_orig_nexthop
, 0,
249 sizeof(struct prefix
)); /* keep valgrind happy */
254 afi
= family2afi(pb
->upfx
.family
);
256 rfapiUnicastNexthop2Prefix(afi
, pb
->ubpi
->attr
,
259 /* pb->hpfx, pb->ubpi nexthop, pkey should all reflect the same
261 assert(!vnc_prefix_cmp(&pb
->hpfx
, pkey
));
262 if (vnc_prefix_cmp(&pb
->hpfx
, &pfx_orig_nexthop
)) {
263 char str_onh
[PREFIX_STRLEN
];
264 char str_nve_pfx
[PREFIX_STRLEN
];
266 prefix2str(&pfx_orig_nexthop
, str_onh
, sizeof(str_onh
));
267 prefix2str(&pb
->hpfx
, str_nve_pfx
, sizeof(str_nve_pfx
));
269 vnc_zlog_debug_verbose(
270 "%s: %s: FATAL: resolve_nve_nexthop list item bpi nexthop %s != nve pfx %s",
271 __func__
, tag
, str_onh
, str_nve_pfx
);
275 vnc_zlog_debug_verbose("%s: vnc_rhnck OK", tag
);
278 #define VNC_RHNCK(n) do {char buf[BUFSIZ];sprintf(buf,"%s: %s", __func__, #n);vnc_rhnck(buf);} while (0)
286 /***********************************************************************
287 * Add/Delete Unicast Route
288 ***********************************************************************/
291 * "Adding a Route" import process
295 * extract and package information from the BGP unicast route.
296 * Return code 0 means OK, non-0 means drop.
298 * If return code is 0, caller MUST release ecom
300 static int process_unicast_route(struct bgp
*bgp
, /* in */
302 const struct prefix
*prefix
, /* in */
303 struct bgp_path_info
*info
, /* in */
304 struct ecommunity
**ecom
, /* OUT */
305 struct prefix
*unicast_nexthop
) /* OUT */
307 struct rfapi_cfg
*hc
= bgp
->rfapi_cfg
;
308 struct peer
*peer
= info
->peer
;
309 struct attr
*attr
= info
->attr
;
311 struct route_map
*rmap
= NULL
;
312 struct prefix pfx_orig_nexthop
;
314 memset(&pfx_orig_nexthop
, 0,
315 sizeof(struct prefix
)); /* keep valgrind happy */
320 if (hc
->plist_redist
[ZEBRA_ROUTE_BGP_DIRECT
][afi
]) {
321 vnc_zlog_debug_verbose("%s: HC prefix list is set, checking",
323 if (prefix_list_apply(
324 hc
->plist_redist
[ZEBRA_ROUTE_BGP_DIRECT
][afi
],
327 vnc_zlog_debug_verbose(
328 "%s: prefix list returns DENY, blocking route",
332 vnc_zlog_debug_verbose(
333 "%s: prefix list returns PASS, allowing route",
337 /* apply routemap, if any, later */
338 rmap
= hc
->routemap_redist
[ZEBRA_ROUTE_BGP_DIRECT
];
341 * Extract original nexthop, which we expect to be a NVE connected
343 * Note that this is the nexthop before any possible application of
347 * Incoming prefix is unicast. If v6, it is in multiprotocol area,
348 * but if v4 it is in attr->nexthop
350 rfapiUnicastNexthop2Prefix(afi
, attr
, &pfx_orig_nexthop
);
354 * This code is here because it allocates an interned attr which
355 * must be freed before we return. It's easier to put it after
356 * all of the possible returns above.
358 memset(&hattr
, 0, sizeof(struct attr
));
359 /* hattr becomes a ghost attr */
363 struct bgp_path_info info
;
364 route_map_result_t ret
;
366 memset(&info
, 0, sizeof(info
));
369 ret
= route_map_apply(rmap
, prefix
, RMAP_BGP
, &info
);
370 if (ret
== RMAP_DENYMATCH
) {
371 bgp_attr_flush(&hattr
);
372 vnc_zlog_debug_verbose(
373 "%s: route map \"%s\" says DENY, returning",
374 __func__
, rmap
->name
);
380 * Get the (possibly altered by policy) unicast nexthop
381 * for later lookup in the Import Table by caller
383 rfapiUnicastNexthop2Prefix(afi
, &hattr
, unicast_nexthop
);
385 if (hattr
.ecommunity
)
386 *ecom
= ecommunity_dup(hattr
.ecommunity
);
388 *ecom
= ecommunity_new();
391 * Done with hattr, clean up
393 bgp_attr_flush(&hattr
);
396 * Add EC that carries original NH of iBGP route (2 bytes = magic
397 * value indicating it came from an VNC gateway; default 5226, but
398 * must be user configurable). Note that this is the nexthop before
399 * any application of policy.
402 struct ecommunity_val vnc_gateway_magic
;
405 /* Using route origin extended community type */
406 memset(&vnc_gateway_magic
, 0, sizeof(vnc_gateway_magic
));
407 vnc_gateway_magic
.val
[0] = 0x01;
408 vnc_gateway_magic
.val
[1] = 0x03;
410 /* Only works for IPv4 nexthops */
411 if (prefix
->family
== AF_INET
) {
412 memcpy(vnc_gateway_magic
.val
+ 2,
413 &unicast_nexthop
->u
.prefix4
, 4);
415 localadmin
= htons(hc
->resolve_nve_roo_local_admin
);
416 memcpy(vnc_gateway_magic
.val
+ 6, (char *)&localadmin
, 2);
418 ecommunity_add_val(*ecom
, &vnc_gateway_magic
);
425 static void vnc_import_bgp_add_route_mode_resolve_nve_one_bi(
426 struct bgp
*bgp
, afi_t afi
, struct bgp_path_info
*bpi
, /* VPN bpi */
427 struct prefix_rd
*prd
, /* RD */
428 const struct prefix
*prefix
, /* unicast route prefix */
429 uint32_t *local_pref
, /* NULL = no local_pref */
430 uint32_t *med
, /* NULL = no med */
431 struct ecommunity
*ecom
) /* generated ecoms */
434 struct prefix nexthop
;
435 struct rfapi_ip_addr nexthop_h
;
438 struct bgp_attr_encap_subtlv
*encaptlvs
;
441 struct rfapi_un_option optary
[3];
442 struct rfapi_un_option
*opt
= NULL
;
445 vnc_zlog_debug_verbose("%s: entry", __func__
);
447 if (bpi
->type
!= ZEBRA_ROUTE_BGP
448 && bpi
->type
!= ZEBRA_ROUTE_BGP_DIRECT
) {
452 if (bpi
->sub_type
!= BGP_ROUTE_NORMAL
453 && bpi
->sub_type
!= BGP_ROUTE_STATIC
454 && bpi
->sub_type
!= BGP_ROUTE_RFP
) {
458 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
))
461 vncHDResolveNve
.peer
= bpi
->peer
;
462 if (!rfapiGetVncTunnelUnAddr(bpi
->attr
, &un
)) {
463 if (rfapiQprefix2Raddr(&un
, &vncHDResolveNve
.un_addr
))
466 memset(&vncHDResolveNve
.un_addr
, 0,
467 sizeof(vncHDResolveNve
.un_addr
));
470 /* Use nexthop of VPN route as nexthop of constructed route */
471 rfapiNexthop2Prefix(bpi
->attr
, &nexthop
);
472 rfapiQprefix2Raddr(&nexthop
, &nexthop_h
);
474 if (rfapiGetVncLifetime(bpi
->attr
, &lifetime
)) {
477 plifetime
= &lifetime
;
480 encaptlvs
= bpi
->attr
->vnc_subtlvs
;
481 if (bpi
->attr
->encap_tunneltype
!= BGP_ENCAP_TYPE_RESERVED
482 && bpi
->attr
->encap_tunneltype
!= BGP_ENCAP_TYPE_MPLS
) {
483 opt
= &optary
[cur_opt
++];
484 memset(opt
, 0, sizeof(struct rfapi_un_option
));
485 opt
->type
= RFAPI_UN_OPTION_TYPE_TUNNELTYPE
;
486 opt
->v
.tunnel
.type
= bpi
->attr
->encap_tunneltype
;
487 /* TBD parse bpi->attr->extra->encap_subtlvs */
490 struct ecommunity
*new_ecom
= ecommunity_dup(ecom
);
492 if (bpi
->attr
->ecommunity
)
493 ecommunity_merge(new_ecom
, bpi
->attr
->ecommunity
);
496 label
= decode_label(&bpi
->extra
->label
[0]);
498 add_vnc_route(&vncHDResolveNve
, bgp
, SAFI_MPLS_VPN
,
499 prefix
, /* unicast route prefix */
500 prd
, &nexthop_h
, /* new nexthop */
501 local_pref
, plifetime
,
502 (struct bgp_tea_options
*)encaptlvs
, /* RFP options */
503 opt
, NULL
, new_ecom
, med
, /* NULL => don't set med */
504 (label
? &label
: NULL
), /* NULL= default */
505 ZEBRA_ROUTE_BGP_DIRECT
, BGP_ROUTE_REDISTRIBUTE
,
506 RFAPI_AHR_RFPOPT_IS_VNCTLV
); /* flags */
508 ecommunity_free(&new_ecom
);
511 static void vnc_import_bgp_add_route_mode_resolve_nve_one_rd(
512 struct prefix_rd
*prd
, /* RD */
513 struct bgp_table
*table_rd
, /* per-rd VPN route table */
514 afi_t afi
, struct bgp
*bgp
,
515 const struct prefix
*prefix
, /* unicast prefix */
516 struct ecommunity
*ecom
, /* generated ecoms */
517 uint32_t *local_pref
, /* NULL = no local_pref */
518 uint32_t *med
, /* NULL = no med */
519 struct prefix
*ubpi_nexthop
) /* unicast nexthop */
522 struct bgp_path_info
*bpi
;
528 char str_nh
[PREFIX_STRLEN
];
530 prefix2str(ubpi_nexthop
, str_nh
, sizeof(str_nh
));
532 vnc_zlog_debug_verbose("%s: ubpi_nexthop=%s", __func__
, str_nh
);
536 bn
= bgp_node_lookup(table_rd
, ubpi_nexthop
);
538 vnc_zlog_debug_verbose(
539 "%s: no match in RD's table for ubpi_nexthop",
544 /* Iterate over bgp_info items at this node */
545 for (bpi
= bgp_node_get_bgp_path_info(bn
); bpi
; bpi
= bpi
->next
) {
547 vnc_import_bgp_add_route_mode_resolve_nve_one_bi(
548 bgp
, afi
, bpi
, /* VPN bpi */
549 prd
, prefix
, local_pref
, med
, ecom
);
555 static void vnc_import_bgp_add_route_mode_resolve_nve(
556 struct bgp
*bgp
, const struct prefix
*prefix
, /* unicast prefix */
557 struct bgp_path_info
*info
) /* unicast info */
559 afi_t afi
= family2afi(prefix
->family
);
561 struct prefix pfx_unicast_nexthop
= {0}; /* happy valgrind */
563 struct ecommunity
*ecom
= NULL
;
565 uint32_t *med
= NULL
;
567 struct prefix_bag
*pb
;
568 struct bgp_node
*bnp
; /* prd table node */
571 if (VNC_DEBUG(VERBOSE
)) {
572 char str_pfx
[PREFIX_STRLEN
];
573 char str_nh
[PREFIX_STRLEN
];
576 prefix2str(prefix
, str_pfx
, sizeof(str_pfx
));
579 rfapiUnicastNexthop2Prefix(afi
, info
->attr
, &nh
);
581 prefix2str(&nh
, str_nh
, sizeof(str_nh
));
587 vnc_zlog_debug_verbose(
588 "%s(bgp=%p, unicast prefix=%s, unicast nh=%s)",
589 __func__
, bgp
, str_pfx
, str_nh
);
592 if (info
->type
!= ZEBRA_ROUTE_BGP
) {
593 vnc_zlog_debug_verbose(
594 "%s: unicast type %d=\"%s\" is not %d=%s, skipping",
595 __func__
, info
->type
, zebra_route_string(info
->type
),
596 ZEBRA_ROUTE_BGP
, "ZEBRA_ROUTE_BGP");
605 flog_err(EC_LIB_DEVELOPMENT
, "%s: can't get afi of prefix",
610 if (!(bgp
->rfapi_cfg
)) {
611 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
616 /* check vnc redist flag for bgp direct routes */
617 if (!bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT
]) {
618 vnc_zlog_debug_verbose(
619 "%s: bgp->rfapi_cfg->redist[afi=%d][type=ZEBRA_ROUTE_BGP_DIRECT] is 0, skipping",
625 if (process_unicast_route(bgp
, afi
, prefix
, info
, &ecom
,
626 &pfx_unicast_nexthop
)) {
628 vnc_zlog_debug_verbose(
629 "%s: process_unicast_route error, skipping", __func__
);
633 local_pref
= calc_local_pref(info
->attr
, info
->peer
);
634 if (info
->attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
))
635 med
= &info
->attr
->med
;
638 * At this point, we have allocated:
640 * ecom ecommunity ptr, union of unicast and ROO parts (no NVE part)
644 * pfx_unicast_nexthop nexthop of uncast route
647 if (!bgp
->rfapi
->resolve_nve_nexthop
) {
648 bgp
->rfapi
->resolve_nve_nexthop
=
649 skiplist_new(SKIPLIST_FLAG_ALLOW_DUPLICATES
,
650 vnc_prefix_cmp
, prefix_bag_free
);
653 pb
= XCALLOC(MTYPE_RFAPI_PREFIX_BAG
, sizeof(struct prefix_bag
));
654 pb
->hpfx
= pfx_unicast_nexthop
;
658 bgp_path_info_lock(info
); /* skiplist refers to it */
659 skiplist_insert(bgp
->rfapi
->resolve_nve_nexthop
, &pb
->hpfx
, pb
);
662 * Iterate over RDs in VPN RIB. For each RD, look up unicast nexthop
663 * (exact match, /32). If an exact match is found, call add_vnc_route.
666 for (bnp
= bgp_table_top(bgp
->rib
[afi
][SAFI_MPLS_VPN
]); bnp
;
667 bnp
= bgp_route_next(bnp
)) {
669 struct bgp_table
*table
;
671 table
= bgp_node_get_bgp_table_info(bnp
);
676 vnc_import_bgp_add_route_mode_resolve_nve_one_rd(
677 (struct prefix_rd
*)&bnp
->p
, table
, afi
, bgp
, prefix
,
678 ecom
, &local_pref
, med
, &pfx_unicast_nexthop
);
683 ecommunity_free(&ecom
);
685 vnc_zlog_debug_verbose("%s: done", __func__
);
689 static void vnc_import_bgp_add_route_mode_plain(struct bgp
*bgp
,
690 const struct prefix
*prefix
,
691 struct bgp_path_info
*info
)
693 afi_t afi
= family2afi(prefix
->family
);
694 struct peer
*peer
= info
->peer
;
695 struct attr
*attr
= info
->attr
;
697 struct rfapi_cfg
*hc
= bgp
->rfapi_cfg
;
698 struct attr
*iattr
= NULL
;
700 struct rfapi_ip_addr vnaddr
;
701 struct prefix vn_pfx_space
;
702 struct prefix
*vn_pfx
= NULL
;
704 struct ecommunity
*ecom
= NULL
;
705 struct prefix_rd prd
;
706 struct route_map
*rmap
= NULL
;
708 uint32_t *med
= NULL
;
711 char buf
[PREFIX_STRLEN
];
713 prefix2str(prefix
, buf
, sizeof(buf
));
714 vnc_zlog_debug_verbose("%s(prefix=%s) entry", __func__
, buf
);
718 flog_err(EC_LIB_DEVELOPMENT
, "%s: can't get afi of prefix",
724 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
729 /* check vnc redist flag for bgp direct routes */
730 if (!bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT
]) {
731 vnc_zlog_debug_verbose(
732 "%s: bgp->rfapi_cfg->redist[afi=%d][type=ZEBRA_ROUTE_BGP_DIRECT] is 0, skipping",
738 * mode "plain" specific code
741 vnc_zlog_debug_verbose("%s: NOT using redist RFG", __func__
);
746 if (hc
->plist_redist
[ZEBRA_ROUTE_BGP_DIRECT
][afi
]) {
747 vnc_zlog_debug_verbose(
748 "%s: HC prefix list is set, checking",
750 if (prefix_list_apply(
751 hc
->plist_redist
[ZEBRA_ROUTE_BGP_DIRECT
]
755 vnc_zlog_debug_verbose(
756 "%s: prefix list returns DENY, blocking route",
760 vnc_zlog_debug_verbose(
761 "%s: prefix list returns PASS, allowing route",
765 /* apply routemap, if any, later */
766 rmap
= hc
->routemap_redist
[ZEBRA_ROUTE_BGP_DIRECT
];
769 * Incoming prefix is unicast. If v6, it is in multiprotocol
771 * but if v4 it is in attr->nexthop
773 rfapiUnicastNexthop2Prefix(afi
, attr
, &vn_pfx_space
);
774 vn_pfx
= &vn_pfx_space
;
777 ahr_flags
|= RFAPI_AHR_NO_TUNNEL_SUBTLV
;
780 if (VNC_DEBUG(IMPORT_BGP_ADD_ROUTE
)) {
781 char buf
[PREFIX_STRLEN
];
783 prefix2str(vn_pfx
, buf
, sizeof(buf
));
784 vnc_zlog_debug_any("%s vn_pfx=%s", __func__
, buf
);
790 if (rfapiQprefix2Raddr(vn_pfx
, &vnaddr
)) {
791 vnc_zlog_debug_verbose("%s: redist VN invalid, skipping",
798 * This code is here because it allocates an interned attr which
799 * must be freed before we return. It's easier to put it after
800 * all of the possible returns above.
802 memset(&hattr
, 0, sizeof(struct attr
));
803 /* hattr becomes a ghost attr */
807 struct bgp_path_info info
;
808 route_map_result_t ret
;
810 memset(&info
, 0, sizeof(info
));
813 ret
= route_map_apply(rmap
, prefix
, RMAP_BGP
, &info
);
814 if (ret
== RMAP_DENYMATCH
) {
815 bgp_attr_flush(&hattr
);
816 vnc_zlog_debug_verbose(
817 "%s: route map \"%s\" says DENY, returning",
818 __func__
, rmap
->name
);
823 iattr
= bgp_attr_intern(&hattr
);
824 bgp_attr_flush(&hattr
);
826 /* Now iattr is an allocated interned attr */
829 * Mode "plain" specific code
831 * Sets RD in dummy HD
835 if (vnaddr
.addr_family
!= AF_INET
) {
836 vnc_zlog_debug_verbose(
837 "%s: can't auto-assign RD, VN AF (%d) is not IPv4, skipping",
838 __func__
, vnaddr
.addr_family
);
840 bgp_attr_unintern(&iattr
);
844 memset(&prd
, 0, sizeof(prd
));
845 rfapi_set_autord_from_vn(&prd
, &vnaddr
);
847 if (iattr
&& iattr
->ecommunity
)
848 ecom
= ecommunity_dup(iattr
->ecommunity
);
851 local_pref
= calc_local_pref(iattr
, peer
);
853 if (iattr
&& (iattr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
))) {
857 if (VNC_DEBUG(IMPORT_BGP_ADD_ROUTE
)) {
858 char buf
[PREFIX_STRLEN
];
860 rfapiRfapiIpAddr2Str(&vnaddr
, buf
, sizeof(buf
));
861 vnc_zlog_debug_any("%s: setting vnaddr to %s", __func__
, buf
);
864 vncHDBgpDirect
.peer
= peer
;
865 add_vnc_route(&vncHDBgpDirect
, bgp
, SAFI_MPLS_VPN
, prefix
, &prd
,
866 &vnaddr
, &local_pref
, &(bgp
->rfapi_cfg
->redist_lifetime
),
867 NULL
, /* RFP options */
868 NULL
, NULL
, ecom
, med
, /* med */
869 NULL
, /* label: default */
870 ZEBRA_ROUTE_BGP_DIRECT
, BGP_ROUTE_REDISTRIBUTE
,
872 vncHDBgpDirect
.peer
= NULL
;
875 ecommunity_free(&ecom
);
878 static void vnc_import_bgp_add_route_mode_nvegroup(
879 struct bgp
*bgp
, const struct prefix
*prefix
,
880 struct bgp_path_info
*info
, struct rfapi_nve_group_cfg
*rfg
)
882 afi_t afi
= family2afi(prefix
->family
);
883 struct peer
*peer
= info
->peer
;
884 struct attr
*attr
= info
->attr
;
886 struct attr
*iattr
= NULL
;
888 struct rfapi_ip_addr vnaddr
;
889 struct prefix
*vn_pfx
= NULL
;
891 struct ecommunity
*ecom
= NULL
;
892 struct prefix_rd prd
;
893 struct route_map
*rmap
= NULL
;
897 char buf
[PREFIX_STRLEN
];
899 prefix2str(prefix
, buf
, sizeof(buf
));
900 vnc_zlog_debug_verbose("%s(prefix=%s) entry", __func__
, buf
);
906 flog_err(EC_LIB_DEVELOPMENT
, "%s: can't get afi of prefix",
911 if (!(bgp
->rfapi_cfg
)) {
912 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
917 /* check vnc redist flag for bgp direct routes */
918 if (!bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT
]) {
919 vnc_zlog_debug_verbose(
920 "%s: bgp->rfapi_cfg->redist[afi=%d][type=ZEBRA_ROUTE_BGP_DIRECT] is 0, skipping",
931 struct rfapi_ip_prefix pfx_un
;
933 vnc_zlog_debug_verbose("%s: using redist RFG", __func__
);
936 * RFG prefix list check
938 if (rfg
->plist_redist
[ZEBRA_ROUTE_BGP_DIRECT
][afi
]) {
939 vnc_zlog_debug_verbose(
940 "%s: RFG prefix list is set, checking",
942 if (prefix_list_apply(
943 rfg
->plist_redist
[ZEBRA_ROUTE_BGP_DIRECT
]
947 vnc_zlog_debug_verbose(
948 "%s: prefix list returns DENY, blocking route",
952 vnc_zlog_debug_verbose(
953 "%s: prefix list returns PASS, allowing route",
957 /* apply routemap, if any, later */
958 rmap
= rfg
->routemap_redist
[ZEBRA_ROUTE_BGP_DIRECT
];
961 * export nve group's VN addr prefix must be a /32 which
962 * will yield the VN addr to use
964 vn_pfx
= &rfg
->vn_prefix
;
969 if (!is_host_prefix(&rfg
->un_prefix
)) {
970 /* NB prefixlen==0 means it has not been configured */
971 vnc_zlog_debug_verbose(
972 "%s: redist RFG UN pfx not host pfx (plen=%d), skipping",
973 __func__
, rfg
->un_prefix
.prefixlen
);
977 rfapiQprefix2Rprefix(&rfg
->un_prefix
, &pfx_un
);
979 vncHDBgpDirect
.un_addr
= pfx_un
.prefix
;
982 if (VNC_DEBUG(IMPORT_BGP_ADD_ROUTE
)) {
983 char buf
[PREFIX_STRLEN
];
985 prefix2str(vn_pfx
, buf
, sizeof(buf
));
986 vnc_zlog_debug_any("%s vn_pfx=%s", __func__
, buf
);
992 if (rfapiQprefix2Raddr(vn_pfx
, &vnaddr
)) {
993 vnc_zlog_debug_verbose("%s: redist VN invalid, skipping",
1000 * This code is here because it allocates an interned attr which
1001 * must be freed before we return. It's easier to put it after
1002 * all of the possible returns above.
1004 memset(&hattr
, 0, sizeof(struct attr
));
1005 /* hattr becomes a ghost attr */
1009 struct bgp_path_info path
;
1010 route_map_result_t ret
;
1012 memset(&path
, 0, sizeof(path
));
1015 ret
= route_map_apply(rmap
, prefix
, RMAP_BGP
, &path
);
1016 if (ret
== RMAP_DENYMATCH
) {
1017 bgp_attr_flush(&hattr
);
1018 vnc_zlog_debug_verbose(
1019 "%s: route map \"%s\" says DENY, returning",
1020 __func__
, rmap
->name
);
1025 iattr
= bgp_attr_intern(&hattr
);
1026 bgp_attr_flush(&hattr
);
1028 /* Now iattr is an allocated interned attr */
1033 * Sets RD in dummy HD
1038 memset(&prd
, 0, sizeof(prd
));
1040 prd
.family
= AF_UNSPEC
;
1043 if (rfg
->rd
.family
== AF_UNIX
) {
1044 rfapi_set_autord_from_vn(&prd
, &vnaddr
);
1047 if (rfg
->rt_export_list
)
1048 ecom
= ecommunity_dup(
1049 bgp
->rfapi_cfg
->rfg_redist
->rt_export_list
);
1051 ecom
= ecommunity_new();
1053 if (iattr
&& iattr
->ecommunity
)
1054 ecom
= ecommunity_merge(ecom
, iattr
->ecommunity
);
1057 local_pref
= calc_local_pref(iattr
, peer
);
1059 if (VNC_DEBUG(IMPORT_BGP_ADD_ROUTE
)) {
1063 rfapiRfapiIpAddr2Str(&vnaddr
, buf
, BUFSIZ
);
1064 buf
[BUFSIZ
- 1] = 0;
1065 vnc_zlog_debug_any("%s: setting vnaddr to %s", __func__
, buf
);
1068 vncHDBgpDirect
.peer
= peer
;
1069 add_vnc_route(&vncHDBgpDirect
, bgp
, SAFI_MPLS_VPN
, prefix
, &prd
,
1070 &vnaddr
, &local_pref
, &(bgp
->rfapi_cfg
->redist_lifetime
),
1071 NULL
, /* RFP options */
1072 NULL
, NULL
, ecom
, NULL
, /* med */
1073 NULL
, /* label: default */
1074 ZEBRA_ROUTE_BGP_DIRECT
, BGP_ROUTE_REDISTRIBUTE
,
1076 vncHDBgpDirect
.peer
= NULL
;
1079 ecommunity_free(&ecom
);
1082 static void vnc_import_bgp_del_route_mode_plain(struct bgp
*bgp
,
1083 const struct prefix
*prefix
,
1084 struct bgp_path_info
*info
)
1086 struct prefix_rd prd
;
1087 afi_t afi
= family2afi(prefix
->family
);
1088 struct prefix
*vn_pfx
= NULL
;
1089 struct rfapi_ip_addr vnaddr
;
1090 struct prefix vn_pfx_space
;
1096 * Compute VN address
1100 rfapiUnicastNexthop2Prefix(afi
, info
->attr
, &vn_pfx_space
);
1102 vnc_zlog_debug_verbose("%s: no attr, can't delete route",
1106 vn_pfx
= &vn_pfx_space
;
1108 vnaddr
.addr_family
= vn_pfx
->family
;
1109 switch (vn_pfx
->family
) {
1111 if (vn_pfx
->prefixlen
!= 32) {
1112 vnc_zlog_debug_verbose(
1113 "%s: redist VN plen (%d) != 32, skipping",
1114 __func__
, vn_pfx
->prefixlen
);
1117 vnaddr
.addr
.v4
= vn_pfx
->u
.prefix4
;
1121 if (vn_pfx
->prefixlen
!= 128) {
1122 vnc_zlog_debug_verbose(
1123 "%s: redist VN plen (%d) != 128, skipping",
1124 __func__
, vn_pfx
->prefixlen
);
1127 vnaddr
.addr
.v6
= vn_pfx
->u
.prefix6
;
1131 vnc_zlog_debug_verbose(
1132 "%s: no redist RFG VN host pfx configured, skipping",
1138 memset(&prd
, 0, sizeof(prd
));
1139 if (rfapi_set_autord_from_vn(&prd
, &vnaddr
)) {
1140 vnc_zlog_debug_verbose("%s: can't auto-assign RD, skipping",
1145 vncHDBgpDirect
.peer
= info
->peer
;
1146 vnc_zlog_debug_verbose("%s: setting peer to %p", __func__
,
1147 vncHDBgpDirect
.peer
);
1148 del_vnc_route(&vncHDBgpDirect
, info
->peer
, bgp
, SAFI_MPLS_VPN
, prefix
,
1149 &prd
, ZEBRA_ROUTE_BGP_DIRECT
, BGP_ROUTE_REDISTRIBUTE
,
1152 vncHDBgpDirect
.peer
= NULL
;
1155 static void vnc_import_bgp_del_route_mode_nvegroup(struct bgp
*bgp
,
1156 const struct prefix
*prefix
,
1157 struct bgp_path_info
*info
)
1159 struct prefix_rd prd
;
1160 afi_t afi
= family2afi(prefix
->family
);
1161 struct rfapi_nve_group_cfg
*rfg
= NULL
;
1162 struct prefix
*vn_pfx
= NULL
;
1163 struct rfapi_ip_addr vnaddr
;
1168 rfg
= bgp
->rfapi_cfg
->rfg_redist
;
1172 * Compute VN address
1176 * export nve group's VN addr prefix must be a /32 which
1177 * will yield the VN addr to use
1179 vn_pfx
= &rfg
->vn_prefix
;
1182 vnaddr
.addr_family
= vn_pfx
->family
;
1183 switch (vn_pfx
->family
) {
1185 if (vn_pfx
->prefixlen
!= 32) {
1186 vnc_zlog_debug_verbose(
1187 "%s: redist VN plen (%d) != 32, skipping",
1188 __func__
, vn_pfx
->prefixlen
);
1191 vnaddr
.addr
.v4
= vn_pfx
->u
.prefix4
;
1195 if (vn_pfx
->prefixlen
!= 128) {
1196 vnc_zlog_debug_verbose(
1197 "%s: redist VN plen (%d) != 128, skipping",
1198 __func__
, vn_pfx
->prefixlen
);
1201 vnaddr
.addr
.v6
= vn_pfx
->u
.prefix6
;
1205 vnc_zlog_debug_verbose(
1206 "%s: no redist RFG VN host pfx configured, skipping",
1211 memset(&prd
, 0, sizeof(prd
));
1213 prd
.family
= AF_UNSPEC
;
1216 if (rfg
->rd
.family
== AF_UNIX
) {
1217 /* means "auto" with VN addr */
1218 if (rfapi_set_autord_from_vn(&prd
, &vnaddr
)) {
1219 vnc_zlog_debug_verbose(
1220 "%s: can't auto-assign RD, skipping", __func__
);
1226 vncHDBgpDirect
.peer
= info
->peer
;
1227 vnc_zlog_debug_verbose("%s: setting peer to %p", __func__
,
1228 vncHDBgpDirect
.peer
);
1229 del_vnc_route(&vncHDBgpDirect
, info
->peer
, bgp
, SAFI_MPLS_VPN
, prefix
,
1230 &prd
, ZEBRA_ROUTE_BGP_DIRECT
, BGP_ROUTE_REDISTRIBUTE
,
1233 vncHDBgpDirect
.peer
= NULL
;
1236 static void vnc_import_bgp_del_route_mode_resolve_nve_one_bi(
1237 struct bgp
*bgp
, afi_t afi
, struct bgp_path_info
*bpi
, /* VPN bpi */
1238 struct prefix_rd
*prd
, /* RD */
1239 const struct prefix
*prefix
) /* unicast route prefix */
1243 if (bpi
->type
!= ZEBRA_ROUTE_BGP
1244 && bpi
->type
!= ZEBRA_ROUTE_BGP_DIRECT
) {
1248 if (bpi
->sub_type
!= BGP_ROUTE_NORMAL
1249 && bpi
->sub_type
!= BGP_ROUTE_STATIC
1250 && bpi
->sub_type
!= BGP_ROUTE_RFP
) {
1254 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
))
1257 vncHDResolveNve
.peer
= bpi
->peer
;
1258 if (!rfapiGetVncTunnelUnAddr(bpi
->attr
, &un
)) {
1259 if (rfapiQprefix2Raddr(&un
, &vncHDResolveNve
.un_addr
))
1262 memset(&vncHDResolveNve
.un_addr
, 0,
1263 sizeof(vncHDResolveNve
.un_addr
));
1266 del_vnc_route(&vncHDResolveNve
, vncHDResolveNve
.peer
, bgp
,
1267 SAFI_MPLS_VPN
, prefix
, /* unicast route prefix */
1268 prd
, ZEBRA_ROUTE_BGP_DIRECT
, BGP_ROUTE_REDISTRIBUTE
, NULL
,
1272 static void vnc_import_bgp_del_route_mode_resolve_nve_one_rd(
1273 struct prefix_rd
*prd
,
1274 struct bgp_table
*table_rd
, /* per-rd VPN route table */
1275 afi_t afi
, struct bgp
*bgp
,
1276 const struct prefix
*prefix
, /* unicast prefix */
1277 const struct prefix
*ubpi_nexthop
) /* unicast bpi's nexthop */
1279 struct bgp_node
*bn
;
1280 struct bgp_path_info
*bpi
;
1286 char str_nh
[PREFIX_STRLEN
];
1288 prefix2str(ubpi_nexthop
, str_nh
, sizeof(str_nh
));
1289 vnc_zlog_debug_verbose("%s: ubpi_nexthop=%s", __func__
, str_nh
);
1294 bn
= bgp_node_lookup(table_rd
, ubpi_nexthop
);
1296 vnc_zlog_debug_verbose(
1297 "%s: no match in RD's table for ubpi_nexthop",
1302 /* Iterate over bgp_info items at this node */
1303 for (bpi
= bgp_node_get_bgp_path_info(bn
); bpi
; bpi
= bpi
->next
) {
1305 vnc_import_bgp_del_route_mode_resolve_nve_one_bi(
1306 bgp
, afi
, bpi
, /* VPN bpi */
1308 prefix
); /* unicast route prefix */
1311 bgp_unlock_node(bn
);
1315 vnc_import_bgp_del_route_mode_resolve_nve(struct bgp
*bgp
, afi_t afi
,
1316 const struct prefix
*prefix
,
1317 struct bgp_path_info
*info
)
1319 struct ecommunity
*ecom
= NULL
;
1320 struct prefix pfx_unicast_nexthop
= {0}; /* happy valgrind */
1322 // struct listnode *hnode;
1323 // struct rfapi_descriptor *rfd;
1324 struct prefix_bag
*pb
;
1326 struct skiplist
*sl
= bgp
->rfapi
->resolve_nve_nexthop
;
1328 struct bgp_node
*bnp
; /* prd table node */
1331 vnc_zlog_debug_verbose("%s: no RHN entries, skipping",
1336 if (info
->type
!= ZEBRA_ROUTE_BGP
) {
1337 vnc_zlog_debug_verbose(
1338 "%s: unicast type %d=\"%s\" is not %d=%s, skipping",
1339 __func__
, info
->type
, zebra_route_string(info
->type
),
1340 ZEBRA_ROUTE_BGP
, "ZEBRA_ROUTE_BGP");
1344 if (process_unicast_route(bgp
, afi
, prefix
, info
, &ecom
,
1345 &pfx_unicast_nexthop
)) {
1347 vnc_zlog_debug_verbose(
1348 "%s: process_unicast_route error, skipping", __func__
);
1352 rc
= skiplist_first_value(sl
, &pfx_unicast_nexthop
, (void *)&pb
,
1355 if (pb
->ubpi
== info
) {
1356 skiplist_delete(sl
, &pfx_unicast_nexthop
, pb
);
1357 bgp_path_info_unlock(info
);
1360 rc
= skiplist_next_value(sl
, &pfx_unicast_nexthop
, (void *)&pb
,
1365 * Iterate over RDs in VPN RIB. For each RD, look up unicast nexthop
1366 * (exact match, /32). If an exact match is found, call add_vnc_route.
1369 for (bnp
= bgp_table_top(bgp
->rib
[afi
][SAFI_MPLS_VPN
]); bnp
;
1370 bnp
= bgp_route_next(bnp
)) {
1372 struct bgp_table
*table
;
1374 table
= bgp_node_get_bgp_table_info(bnp
);
1379 vnc_import_bgp_del_route_mode_resolve_nve_one_rd(
1380 (struct prefix_rd
*)&bnp
->p
, table
, afi
, bgp
, prefix
,
1381 &pfx_unicast_nexthop
); /* TBD how is this set? */
1385 ecommunity_free(&ecom
);
1389 /***********************************************************************
1390 * Add/Delete CE->NVE routes
1391 ***********************************************************************/
1394 * Should be called whan a bpi is added to VPN RIB. This function
1395 * will check if it is a host route and return immediately if not.
1397 void vnc_import_bgp_add_vnc_host_route_mode_resolve_nve(
1398 struct bgp
*bgp
, struct prefix_rd
*prd
, /* RD */
1399 struct bgp_table
*table_rd
, /* per-rd VPN route table */
1400 const struct prefix
*prefix
, /* VPN prefix */
1401 struct bgp_path_info
*bpi
) /* new VPN host route */
1403 afi_t afi
= family2afi(prefix
->family
);
1404 struct skiplist
*sl
= NULL
;
1406 struct prefix_bag
*pb
;
1408 struct rfapi_cfg
*hc
= NULL
;
1410 vnc_zlog_debug_verbose("%s: entry", __func__
);
1412 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
) {
1413 vnc_zlog_debug_verbose("%s: bad afi %d, skipping", __func__
,
1418 if (!(hc
= bgp
->rfapi_cfg
)) {
1419 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
1424 /* check vnc redist flag for bgp direct routes */
1425 if (!hc
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT
]) {
1426 vnc_zlog_debug_verbose(
1427 "%s: bgp->rfapi_cfg->redist[afi=%d][type=ZEBRA_ROUTE_BGP_DIRECT] is 0, skipping",
1432 if (hc
->redist_mode
!= VNC_REDIST_MODE_RESOLVE_NVE
) {
1433 vnc_zlog_debug_verbose("%s: not in resolve-nve mode, skipping",
1439 sl
= bgp
->rfapi
->resolve_nve_nexthop
;
1442 vnc_zlog_debug_verbose(
1443 "%s: no resolve_nve_nexthop skiplist, skipping",
1448 if (!is_host_prefix(prefix
)) {
1449 vnc_zlog_debug_verbose("%s: not host prefix, skipping",
1454 rc
= skiplist_first_value(sl
, prefix
, (void *)&pb
, &cursor
);
1456 struct ecommunity
*ecom
;
1457 struct prefix pfx_unicast_nexthop
;
1458 uint32_t *med
= NULL
;
1459 uint32_t local_pref
;
1461 memset(&pfx_unicast_nexthop
, 0,
1462 sizeof(struct prefix
)); /* keep valgrind happy */
1464 if (VNC_DEBUG(IMPORT_BGP_ADD_ROUTE
)) {
1465 char hbuf
[PREFIX_STRLEN
];
1466 char ubuf
[PREFIX_STRLEN
];
1468 prefix2str(&pb
->hpfx
, hbuf
, sizeof(hbuf
));
1469 prefix2str(&pb
->upfx
, ubuf
, sizeof(ubuf
));
1472 "%s: examining RHN Entry (q=%p): upfx=%s, hpfx=%s, ubpi=%p",
1473 __func__
, cursor
, ubuf
, hbuf
, pb
->ubpi
);
1476 if (process_unicast_route(bgp
, afi
, &pb
->upfx
, pb
->ubpi
, &ecom
,
1477 &pfx_unicast_nexthop
)) {
1479 vnc_zlog_debug_verbose(
1480 "%s: process_unicast_route error, skipping",
1484 local_pref
= calc_local_pref(pb
->ubpi
->attr
, pb
->ubpi
->peer
);
1486 if (pb
->ubpi
->attr
->flag
1487 & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
))
1488 med
= &pb
->ubpi
->attr
->med
;
1493 if (vnc_prefix_cmp(&pfx_unicast_nexthop
, prefix
)) {
1494 char str_unh
[PREFIX_STRLEN
];
1495 char str_nve_pfx
[PREFIX_STRLEN
];
1497 prefix2str(&pfx_unicast_nexthop
, str_unh
,
1499 prefix2str(prefix
, str_nve_pfx
, sizeof(str_nve_pfx
));
1501 vnc_zlog_debug_verbose(
1502 "%s: FATAL: resolve_nve_nexthop list item bpi nexthop %s != nve pfx %s",
1503 __func__
, str_unh
, str_nve_pfx
);
1507 vnc_import_bgp_add_route_mode_resolve_nve_one_bi(
1508 bgp
, afi
, bpi
, /* VPN bpi */
1509 prd
, &pb
->upfx
, /* unicast prefix */
1510 &local_pref
, med
, ecom
);
1513 ecommunity_free(&ecom
);
1518 char pbuf
[PREFIX_STRLEN
];
1520 prefix2str(prefix
, pbuf
, sizeof(pbuf
));
1522 vnc_zlog_debug_verbose(
1523 "%s: advancing past RHN Entry (q=%p): with prefix %s",
1524 __func__
, cursor
, pbuf
);
1525 print_rhn_list(__func__
, NULL
); /* debug */
1528 rc
= skiplist_next_value(sl
, prefix
, (void *)&pb
, &cursor
);
1530 vnc_zlog_debug_verbose("%s: done", __func__
);
1534 void vnc_import_bgp_del_vnc_host_route_mode_resolve_nve(
1535 struct bgp
*bgp
, struct prefix_rd
*prd
, /* RD */
1536 struct bgp_table
*table_rd
, /* per-rd VPN route table */
1537 const struct prefix
*prefix
, /* VPN prefix */
1538 struct bgp_path_info
*bpi
) /* old VPN host route */
1540 afi_t afi
= family2afi(prefix
->family
);
1541 struct skiplist
*sl
= NULL
;
1542 struct prefix_bag
*pb
;
1544 struct rfapi_cfg
*hc
= NULL
;
1548 char str_pfx
[PREFIX_STRLEN
];
1550 prefix2str(prefix
, str_pfx
, sizeof(str_pfx
));
1552 vnc_zlog_debug_verbose("%s(bgp=%p, nve prefix=%s)", __func__
,
1556 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
)
1559 if (!(hc
= bgp
->rfapi_cfg
)) {
1560 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
1565 /* check vnc redist flag for bgp direct routes */
1566 if (!hc
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT
]) {
1567 vnc_zlog_debug_verbose(
1568 "%s: bgp->rfapi_cfg->redist[afi=%d][type=ZEBRA_ROUTE_BGP_DIRECT] is 0, skipping",
1573 if (hc
->redist_mode
!= VNC_REDIST_MODE_RESOLVE_NVE
) {
1574 vnc_zlog_debug_verbose("%s: not in resolve-nve mode, skipping",
1580 sl
= bgp
->rfapi
->resolve_nve_nexthop
;
1583 vnc_zlog_debug_verbose("%s: no RHN entries, skipping",
1588 if (!is_host_prefix(prefix
)) {
1589 vnc_zlog_debug_verbose("%s: not host route, skip", __func__
);
1594 * Find all entries with key == CE in the RHN list
1596 rc
= skiplist_first_value(sl
, prefix
, (void *)&pb
, &cursor
);
1599 struct ecommunity
*ecom
;
1600 struct prefix pfx_unicast_nexthop
;
1602 memset(&pfx_unicast_nexthop
, 0,
1603 sizeof(struct prefix
)); /* keep valgrind happy */
1605 if (process_unicast_route(bgp
, afi
, &pb
->upfx
, pb
->ubpi
, &ecom
,
1606 &pfx_unicast_nexthop
)) {
1608 vnc_zlog_debug_verbose(
1609 "%s: process_unicast_route error, skipping",
1617 if (vnc_prefix_cmp(&pfx_unicast_nexthop
, prefix
)) {
1618 char str_unh
[PREFIX_STRLEN
];
1619 char str_nve_pfx
[PREFIX_STRLEN
];
1621 prefix2str(&pfx_unicast_nexthop
, str_unh
,
1623 prefix2str(prefix
, str_nve_pfx
, sizeof(str_nve_pfx
));
1625 vnc_zlog_debug_verbose(
1626 "%s: FATAL: resolve_nve_nexthop list item bpi nexthop %s != nve pfx %s",
1627 __func__
, str_unh
, str_nve_pfx
);
1631 vnc_import_bgp_del_route_mode_resolve_nve_one_bi(
1632 bgp
, afi
, bpi
, prd
, &pb
->upfx
);
1635 ecommunity_free(&ecom
);
1637 rc
= skiplist_next_value(sl
, prefix
, (void *)&pb
, &cursor
);
1642 /***********************************************************************
1644 ***********************************************************************/
1646 #define DEBUG_IS_USABLE_INTERIOR 1
1648 static int is_usable_interior_route(struct bgp_path_info
*bpi_interior
)
1650 if (!VALID_INTERIOR_TYPE(bpi_interior
->type
)) {
1651 #if DEBUG_IS_USABLE_INTERIOR
1652 vnc_zlog_debug_verbose(
1653 "%s: NO: type %d is not valid interior type", __func__
,
1654 bpi_interior
->type
);
1658 if (!CHECK_FLAG(bpi_interior
->flags
, BGP_PATH_VALID
)) {
1659 #if DEBUG_IS_USABLE_INTERIOR
1660 vnc_zlog_debug_verbose("%s: NO: BGP_PATH_VALID not set",
1669 * There should be only one of these per prefix at a time.
1670 * This should be called as a result of selection operation
1672 * NB should be called espacially for bgp instances that are named,
1673 * because the exterior routes will always come from one of those.
1674 * We filter here on the instance name to make sure we get only the
1677 static void vnc_import_bgp_exterior_add_route_it(
1678 struct bgp
*bgp
, /* exterior instance, we hope */
1679 const struct prefix
*prefix
, /* unicast prefix */
1680 struct bgp_path_info
*info
, /* unicast info */
1681 struct rfapi_import_table
*it_only
) /* NULL, or limit to this IT */
1684 struct rfapi_cfg
*hc
;
1685 struct prefix pfx_orig_nexthop
;
1686 struct rfapi_import_table
*it
;
1687 struct bgp
*bgp_default
= bgp_get_default();
1688 afi_t afi
= family2afi(prefix
->family
);
1693 h
= bgp_default
->rfapi
;
1694 hc
= bgp_default
->rfapi_cfg
;
1696 vnc_zlog_debug_verbose("%s: entry with it=%p", __func__
, it_only
);
1699 vnc_zlog_debug_verbose(
1700 "%s: rfapi or rfapi_cfg not instantiated, skipping",
1704 if (!hc
->redist_bgp_exterior_view
) {
1705 vnc_zlog_debug_verbose("%s: exterior view not set, skipping",
1709 if (bgp
!= hc
->redist_bgp_exterior_view
) {
1710 vnc_zlog_debug_verbose(
1711 "%s: bgp %p != hc->redist_bgp_exterior_view %p, skipping",
1712 __func__
, bgp
, hc
->redist_bgp_exterior_view
);
1716 if (!hc
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT_EXT
]) {
1717 vnc_zlog_debug_verbose(
1718 "%s: redist of exterior routes not enabled, skipping",
1724 * Extract nexthop from exterior route
1726 * Incoming prefix is unicast. If v6, it is in multiprotocol area,
1727 * but if v4 it is in attr->nexthop
1729 rfapiUnicastNexthop2Prefix(afi
, info
->attr
, &pfx_orig_nexthop
);
1731 for (it
= h
->imports
; it
; it
= it
->next
) {
1732 struct agg_table
*table
;
1733 struct agg_node
*rn
;
1734 struct agg_node
*par
;
1735 struct bgp_path_info
*bpi_interior
;
1736 int have_usable_route
;
1738 vnc_zlog_debug_verbose("%s: doing it %p", __func__
, it
);
1740 if (it_only
&& (it_only
!= it
)) {
1741 vnc_zlog_debug_verbose("%s: doesn't match it_only %p",
1746 table
= it
->imported_vpn
[afi
];
1748 for (rn
= agg_node_match(table
, &pfx_orig_nexthop
),
1749 have_usable_route
= 0;
1750 (!have_usable_route
) && rn
;) {
1752 vnc_zlog_debug_verbose("%s: it %p trying rn %p",
1755 for (bpi_interior
= rn
->info
; bpi_interior
;
1756 bpi_interior
= bpi_interior
->next
) {
1757 struct prefix_rd
*prd
;
1758 struct attr new_attr
;
1761 if (!is_usable_interior_route(bpi_interior
))
1764 vnc_zlog_debug_verbose(
1765 "%s: usable: bpi_interior %p", __func__
,
1769 * have a legitimate route to exterior's nexthop
1772 * Import unicast route to the import table
1774 have_usable_route
= 1;
1776 if (bpi_interior
->extra
) {
1777 prd
= &bpi_interior
->extra
->vnc
.import
1779 label
= decode_label(
1780 &bpi_interior
->extra
->label
[0]);
1784 /* use local_pref from unicast route */
1785 memset(&new_attr
, 0, sizeof(struct attr
));
1786 new_attr
= *bpi_interior
->attr
;
1787 if (info
->attr
->flag
1788 & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)) {
1789 new_attr
.local_pref
=
1790 info
->attr
->local_pref
;
1791 new_attr
.flag
|= ATTR_FLAG_BIT(
1792 BGP_ATTR_LOCAL_PREF
);
1795 rfapiBgpInfoFilteredImportVPN(
1796 it
, FIF_ACTION_UPDATE
,
1797 bpi_interior
->peer
, NULL
, /* rfd */
1798 prefix
, NULL
, afi
, prd
, &new_attr
,
1799 ZEBRA_ROUTE_BGP_DIRECT_EXT
,
1800 BGP_ROUTE_REDISTRIBUTE
, &label
);
1803 if (have_usable_route
) {
1807 * TBD factor this out into its own function
1809 struct prefix
*pfx_mon
= prefix_new();
1810 if (!RFAPI_MONITOR_EXTERIOR(rn
)->source
) {
1811 RFAPI_MONITOR_EXTERIOR(rn
)->source
=
1815 agg_lock_node(rn
); /* for skiplist */
1817 agg_lock_node(rn
); /* for skiplist entry */
1818 prefix_copy(pfx_mon
, prefix
);
1819 if (!skiplist_insert(
1820 RFAPI_MONITOR_EXTERIOR(rn
)->source
,
1823 bgp_path_info_lock(info
);
1826 par
= agg_node_parent(rn
);
1829 agg_unlock_node(rn
);
1833 agg_unlock_node(rn
);
1835 if (!have_usable_route
) {
1836 struct prefix
*pfx_mon
= prefix_new();
1837 prefix_copy(pfx_mon
, prefix
);
1838 if (!skiplist_insert(it
->monitor_exterior_orphans
, info
,
1841 bgp_path_info_lock(info
);
1847 void vnc_import_bgp_exterior_add_route(
1848 struct bgp
*bgp
, /* exterior instance, we hope */
1849 const struct prefix
*prefix
, /* unicast prefix */
1850 struct bgp_path_info
*info
) /* unicast info */
1852 vnc_import_bgp_exterior_add_route_it(bgp
, prefix
, info
, NULL
);
1856 * There should be only one of these per prefix at a time.
1857 * This should probably be called as a result of selection operation.
1859 * NB should be called espacially for bgp instances that are named,
1860 * because the exterior routes will always come from one of those.
1861 * We filter here on the instance name to make sure we get only the
1864 void vnc_import_bgp_exterior_del_route(
1865 struct bgp
*bgp
, const struct prefix
*prefix
, /* unicast prefix */
1866 struct bgp_path_info
*info
) /* unicast info */
1869 struct rfapi_cfg
*hc
;
1870 struct rfapi_import_table
*it
;
1871 struct prefix pfx_orig_nexthop
;
1872 afi_t afi
= family2afi(prefix
->family
);
1873 struct bgp
*bgp_default
= bgp_get_default();
1878 memset(&pfx_orig_nexthop
, 0,
1879 sizeof(struct prefix
)); /* keep valgrind happy */
1881 h
= bgp_default
->rfapi
;
1882 hc
= bgp_default
->rfapi_cfg
;
1885 vnc_zlog_debug_verbose(
1886 "%s: rfapi or rfapi_cfg not instantiated, skipping",
1890 if (!hc
->redist_bgp_exterior_view
) {
1891 vnc_zlog_debug_verbose("%s: exterior view not set, skipping",
1895 if (bgp
!= hc
->redist_bgp_exterior_view
) {
1896 vnc_zlog_debug_verbose(
1897 "%s: bgp %p != hc->redist_bgp_exterior_view %p, skipping",
1898 __func__
, bgp
, hc
->redist_bgp_exterior_view
);
1901 if (!hc
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT_EXT
]) {
1902 vnc_zlog_debug_verbose(
1903 "%s: redist of exterior routes no enabled, skipping",
1909 * Extract nexthop from exterior route
1911 * Incoming prefix is unicast. If v6, it is in multiprotocol area,
1912 * but if v4 it is in attr->nexthop
1914 rfapiUnicastNexthop2Prefix(afi
, info
->attr
, &pfx_orig_nexthop
);
1916 for (it
= h
->imports
; it
; it
= it
->next
) {
1917 struct agg_table
*table
;
1918 struct agg_node
*rn
;
1919 struct agg_node
*par
;
1920 struct bgp_path_info
*bpi_interior
;
1921 int have_usable_route
;
1923 table
= it
->imported_vpn
[afi
];
1925 for (rn
= agg_node_match(table
, &pfx_orig_nexthop
),
1926 have_usable_route
= 0;
1927 (!have_usable_route
) && rn
;) {
1929 for (bpi_interior
= rn
->info
; bpi_interior
;
1930 bpi_interior
= bpi_interior
->next
) {
1931 struct prefix_rd
*prd
;
1934 if (!is_usable_interior_route(bpi_interior
))
1938 * have a legitimate route to exterior's nexthop
1941 * Import unicast route to the import table
1943 have_usable_route
= 1;
1945 if (bpi_interior
->extra
) {
1946 prd
= &bpi_interior
->extra
->vnc
.import
1948 label
= decode_label(
1949 &bpi_interior
->extra
->label
[0]);
1953 rfapiBgpInfoFilteredImportVPN(
1954 it
, FIF_ACTION_KILL
, bpi_interior
->peer
,
1956 prefix
, NULL
, afi
, prd
,
1958 ZEBRA_ROUTE_BGP_DIRECT_EXT
,
1959 BGP_ROUTE_REDISTRIBUTE
, &label
);
1964 * TBD factor this out into its own function
1967 if (RFAPI_MONITOR_EXTERIOR(rn
)
1969 if (!skiplist_delete(
1970 RFAPI_MONITOR_EXTERIOR(
1975 bgp_path_info_unlock(
1982 RFAPI_MONITOR_EXTERIOR(
1986 RFAPI_MONITOR_EXTERIOR(
1989 RFAPI_MONITOR_EXTERIOR(
2000 par
= agg_node_parent(rn
);
2003 agg_unlock_node(rn
);
2007 agg_unlock_node(rn
);
2009 if (!have_usable_route
) {
2010 if (!skiplist_delete(it
->monitor_exterior_orphans
, info
,
2013 bgp_path_info_unlock(info
);
2020 * This function should be called after a new interior VPN route
2021 * has been added to an import_table.
2023 * NB should also be called whenever an existing vpn interior route
2024 * becomes valid (e.g., valid_interior_count is inremented)
2026 void vnc_import_bgp_exterior_add_route_interior(
2027 struct bgp
*bgp
, struct rfapi_import_table
*it
,
2028 struct agg_node
*rn_interior
, /* VPN IT node */
2029 struct bgp_path_info
*bpi_interior
) /* VPN IT route */
2031 const struct prefix
*p
= agg_node_get_prefix(rn_interior
);
2032 afi_t afi
= family2afi(p
->family
);
2033 struct agg_node
*par
;
2034 struct bgp_path_info
*bpi_exterior
;
2035 struct prefix
*pfx_exterior
; /* exterior pfx */
2038 struct list
*list_adopted
;
2040 vnc_zlog_debug_verbose("%s: entry", __func__
);
2042 if (!is_usable_interior_route(bpi_interior
)) {
2043 vnc_zlog_debug_verbose(
2044 "%s: not usable interior route, skipping", __func__
);
2048 if (!bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT_EXT
]) {
2049 vnc_zlog_debug_verbose(
2050 "%s: redist of exterior routes no enabled, skipping",
2055 if (it
== bgp
->rfapi
->it_ce
) {
2056 vnc_zlog_debug_verbose("%s: import table is it_ce, skipping",
2062 vnc_zlog_debug_verbose("%s: interior prefix=%pRN, bpi type=%d",
2063 __func__
, rn_interior
, bpi_interior
->type
);
2065 if (RFAPI_HAS_MONITOR_EXTERIOR(rn_interior
)) {
2067 int count
= 0; /* debugging */
2069 vnc_zlog_debug_verbose(
2070 "%s: has exterior monitor; ext src: %p", __func__
,
2071 RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
);
2074 * There is a monitor here already. Therefore, we do not need
2075 * to do any pulldown. Just construct exterior routes based
2076 * on the new interior route.
2079 for (rc
= skiplist_next(
2080 RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
,
2081 (void **)&bpi_exterior
, (void **)&pfx_exterior
,
2083 !rc
; rc
= skiplist_next(
2084 RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
,
2085 (void **)&bpi_exterior
,
2086 (void **)&pfx_exterior
, &cursor
)) {
2088 struct prefix_rd
*prd
;
2089 struct attr new_attr
;
2093 ++count
; /* debugging */
2095 assert(bpi_exterior
);
2096 assert(pfx_exterior
);
2098 if (bpi_interior
->extra
) {
2099 prd
= &bpi_interior
->extra
->vnc
.import
.rd
;
2100 label
= decode_label(
2101 &bpi_interior
->extra
->label
[0]);
2105 /* use local_pref from unicast route */
2106 memset(&new_attr
, 0, sizeof(struct attr
));
2107 new_attr
= *bpi_interior
->attr
;
2109 && (bpi_exterior
->attr
->flag
2110 & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
))) {
2111 new_attr
.local_pref
=
2112 bpi_exterior
->attr
->local_pref
;
2114 ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
);
2117 rfapiBgpInfoFilteredImportVPN(
2118 it
, FIF_ACTION_UPDATE
, bpi_interior
->peer
,
2120 pfx_exterior
, NULL
, afi
, prd
, &new_attr
,
2121 ZEBRA_ROUTE_BGP_DIRECT_EXT
,
2122 BGP_ROUTE_REDISTRIBUTE
, &label
);
2124 vnc_zlog_debug_verbose(
2125 "%s: finished constructing exteriors based on existing monitors",
2130 vnc_zlog_debug_verbose("%s: no exterior monitor", __func__
);
2133 * No monitor at this node. Is this the first valid interior
2134 * route at this node?
2136 if (RFAPI_MONITOR_EXTERIOR(rn_interior
)->valid_interior_count
> 1) {
2137 vnc_zlog_debug_verbose(
2138 "%s: new interior route not first valid one, skipping pulldown",
2144 * Look up the tree for possible pulldown candidates.
2145 * Find nearest parent with an exterior route monitor
2147 for (par
= agg_node_parent(rn_interior
); par
;
2148 par
= agg_node_parent(par
)) {
2149 if (RFAPI_HAS_MONITOR_EXTERIOR(par
))
2155 vnc_zlog_debug_verbose(
2156 "%s: checking parent %p for possible pulldowns",
2159 /* check monitors at par for possible pulldown */
2161 for (rc
= skiplist_next(RFAPI_MONITOR_EXTERIOR(par
)->source
,
2162 (void **)&bpi_exterior
,
2163 (void **)&pfx_exterior
, &cursor
);
2165 rc
= skiplist_next(RFAPI_MONITOR_EXTERIOR(par
)->source
,
2166 (void **)&bpi_exterior
,
2167 (void **)&pfx_exterior
, &cursor
)) {
2169 struct prefix pfx_nexthop
;
2171 memset(&pfx_nexthop
, 0,
2172 sizeof(struct prefix
)); /* keep valgrind happy */
2174 /* check original nexthop for prefix match */
2175 rfapiUnicastNexthop2Prefix(afi
, bpi_exterior
->attr
,
2178 if (prefix_match(p
, &pfx_nexthop
)) {
2180 struct bgp_path_info
*bpi
;
2181 struct prefix_rd
*prd
;
2182 struct attr new_attr
;
2188 * add monitor to longer prefix
2190 struct prefix
*pfx_mon
= prefix_new();
2191 prefix_copy(pfx_mon
, pfx_exterior
);
2192 if (!RFAPI_MONITOR_EXTERIOR(rn_interior
)
2194 RFAPI_MONITOR_EXTERIOR(rn_interior
)
2195 ->source
= skiplist_new(
2196 0, NULL
, prefix_free_lists
);
2197 agg_lock_node(rn_interior
);
2200 RFAPI_MONITOR_EXTERIOR(rn_interior
)
2202 bpi_exterior
, pfx_mon
);
2203 agg_lock_node(rn_interior
);
2206 * Delete constructed exterior routes based on
2209 for (bpi
= par
->info
; bpi
; bpi
= bpi
->next
) {
2212 prd
= &bpi
->extra
->vnc
.import
2214 label
= decode_label(
2215 &bpi
->extra
->label
[0]);
2219 rfapiBgpInfoFilteredImportVPN(
2220 it
, FIF_ACTION_KILL
, bpi
->peer
,
2222 pfx_exterior
, NULL
, afi
, prd
,
2224 ZEBRA_ROUTE_BGP_DIRECT_EXT
,
2225 BGP_ROUTE_REDISTRIBUTE
, &label
);
2230 * Add constructed exterior routes based on
2231 * the new interior route at longer prefix.
2233 if (bpi_interior
->extra
) {
2234 prd
= &bpi_interior
->extra
->vnc
.import
2236 label
= decode_label(
2237 &bpi_interior
->extra
->label
[0]);
2241 /* use local_pref from unicast route */
2242 memset(&new_attr
, 0, sizeof(struct attr
));
2243 new_attr
= *bpi_interior
->attr
;
2245 && (bpi_exterior
->attr
->flag
2246 & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
))) {
2247 new_attr
.local_pref
=
2248 bpi_exterior
->attr
->local_pref
;
2249 new_attr
.flag
|= ATTR_FLAG_BIT(
2250 BGP_ATTR_LOCAL_PREF
);
2253 rfapiBgpInfoFilteredImportVPN(
2254 it
, FIF_ACTION_UPDATE
,
2255 bpi_interior
->peer
, NULL
, /* rfd */
2256 pfx_exterior
, NULL
, afi
, prd
, &new_attr
,
2257 ZEBRA_ROUTE_BGP_DIRECT_EXT
,
2258 BGP_ROUTE_REDISTRIBUTE
, &label
);
2263 * The only monitors at rn_interior are the ones we added just
2264 * above, so we can use the rn_interior list to identify which
2265 * monitors to delete from the parent.
2268 for (rc
= skiplist_next(
2269 RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
,
2270 (void **)&bpi_exterior
, NULL
, &cursor
);
2271 !rc
; rc
= skiplist_next(
2272 RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
,
2273 (void **)&bpi_exterior
, NULL
, &cursor
)) {
2276 skiplist_delete(RFAPI_MONITOR_EXTERIOR(par
)->source
,
2277 bpi_exterior
, NULL
);
2278 agg_unlock_node(par
); /* sl entry */
2280 if (skiplist_empty(RFAPI_MONITOR_EXTERIOR(par
)->source
)) {
2281 skiplist_free(RFAPI_MONITOR_EXTERIOR(par
)->source
);
2282 RFAPI_MONITOR_EXTERIOR(par
)->source
= NULL
;
2283 agg_unlock_node(par
); /* sl itself */
2287 vnc_zlog_debug_verbose("%s: checking orphans", __func__
);
2290 * See if any orphans can be pulled down to the current node
2293 list_adopted
= NULL
;
2294 for (rc
= skiplist_next(it
->monitor_exterior_orphans
,
2295 (void **)&bpi_exterior
, (void **)&pfx_exterior
,
2297 !rc
; rc
= skiplist_next(it
->monitor_exterior_orphans
,
2298 (void **)&bpi_exterior
,
2299 (void **)&pfx_exterior
, &cursor
)) {
2301 struct prefix pfx_nexthop
;
2302 char buf
[PREFIX_STRLEN
];
2303 afi_t afi_exterior
= family2afi(pfx_exterior
->family
);
2305 prefix2str(pfx_exterior
, buf
, sizeof(buf
));
2306 vnc_zlog_debug_verbose(
2307 "%s: checking exterior orphan at prefix %s", __func__
,
2310 if (afi_exterior
!= afi
) {
2311 vnc_zlog_debug_verbose(
2312 "%s: exterior orphan afi %d != interior afi %d, skip",
2313 __func__
, afi_exterior
, afi
);
2317 /* check original nexthop for prefix match */
2318 rfapiUnicastNexthop2Prefix(afi
, bpi_exterior
->attr
,
2321 if (prefix_match(p
, &pfx_nexthop
)) {
2323 struct prefix_rd
*prd
;
2324 struct attr new_attr
;
2330 * add monitor to longer prefix
2333 struct prefix
*pfx_mon
= prefix_new();
2334 prefix_copy(pfx_mon
, pfx_exterior
);
2335 if (!RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
) {
2336 RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
=
2338 0, NULL
, prefix_free_lists
);
2339 agg_lock_node(rn_interior
); /* sl */
2342 RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
,
2343 bpi_exterior
, pfx_mon
);
2344 agg_lock_node(rn_interior
); /* sl entry */
2345 if (!list_adopted
) {
2346 list_adopted
= list_new();
2348 listnode_add(list_adopted
, bpi_exterior
);
2351 * Add constructed exterior routes based on the
2352 * new interior route at the longer prefix.
2354 if (bpi_interior
->extra
) {
2355 prd
= &bpi_interior
->extra
->vnc
.import
.rd
;
2356 label
= decode_label(
2357 &bpi_interior
->extra
->label
[0]);
2361 /* use local_pref from unicast route */
2362 memset(&new_attr
, 0, sizeof(struct attr
));
2363 new_attr
= *bpi_interior
->attr
;
2365 && (bpi_exterior
->attr
->flag
2366 & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
))) {
2367 new_attr
.local_pref
=
2368 bpi_exterior
->attr
->local_pref
;
2370 ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
);
2373 rfapiBgpInfoFilteredImportVPN(
2374 it
, FIF_ACTION_UPDATE
, bpi_interior
->peer
,
2376 pfx_exterior
, NULL
, afi
, prd
, &new_attr
,
2377 ZEBRA_ROUTE_BGP_DIRECT_EXT
,
2378 BGP_ROUTE_REDISTRIBUTE
, &label
);
2382 struct listnode
*node
;
2383 struct agg_node
*an_bpi_exterior
;
2385 for (ALL_LIST_ELEMENTS_RO(list_adopted
, node
,
2387 skiplist_delete(it
->monitor_exterior_orphans
,
2388 an_bpi_exterior
, NULL
);
2390 list_delete(&list_adopted
);
2395 * This function should be called after an interior VPN route
2396 * has been deleted from an import_table.
2397 * bpi_interior must still be valid, but it must already be detached
2398 * from its route node and the route node's valid_interior_count
2399 * must already be decremented.
2401 * NB should also be called whenever an existing vpn interior route
2402 * becomes invalid (e.g., valid_interior_count is decremented)
2404 void vnc_import_bgp_exterior_del_route_interior(
2405 struct bgp
*bgp
, struct rfapi_import_table
*it
,
2406 struct agg_node
*rn_interior
, /* VPN IT node */
2407 struct bgp_path_info
*bpi_interior
) /* VPN IT route */
2409 const struct prefix
*p
= agg_node_get_prefix(rn_interior
);
2410 afi_t afi
= family2afi(p
->family
);
2411 struct agg_node
*par
;
2412 struct bgp_path_info
*bpi_exterior
;
2413 struct prefix
*pfx_exterior
; /* exterior pfx */
2417 if (!VALID_INTERIOR_TYPE(bpi_interior
->type
)) {
2418 vnc_zlog_debug_verbose(
2419 "%s: type %d not valid interior type, skipping",
2420 __func__
, bpi_interior
->type
);
2424 if (!bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT_EXT
]) {
2425 vnc_zlog_debug_verbose(
2426 "%s: redist of exterior routes no enabled, skipping",
2431 if (it
== bgp
->rfapi
->it_ce
) {
2432 vnc_zlog_debug_verbose("%s: it is it_ce, skipping", __func__
);
2436 /* If no exterior routes depend on this prefix, nothing to do */
2437 if (!RFAPI_HAS_MONITOR_EXTERIOR(rn_interior
)) {
2438 vnc_zlog_debug_verbose("%s: no exterior monitor, skipping",
2444 vnc_zlog_debug_verbose("%s: interior prefix=%pRN, bpi type=%d",
2445 __func__
, rn_interior
, bpi_interior
->type
);
2448 * Remove constructed routes based on the deleted interior route
2451 for (rc
= skiplist_next(RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
,
2452 (void **)&bpi_exterior
, (void **)&pfx_exterior
,
2455 rc
= skiplist_next(RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
,
2456 (void **)&bpi_exterior
, (void **)&pfx_exterior
,
2459 struct prefix_rd
*prd
;
2462 if (bpi_interior
->extra
) {
2463 prd
= &bpi_interior
->extra
->vnc
.import
.rd
;
2464 label
= decode_label(&bpi_interior
->extra
->label
[0]);
2468 rfapiBgpInfoFilteredImportVPN(
2469 it
, FIF_ACTION_KILL
, bpi_interior
->peer
, NULL
, /* rfd */
2470 pfx_exterior
, NULL
, afi
, prd
, bpi_interior
->attr
,
2471 ZEBRA_ROUTE_BGP_DIRECT_EXT
, BGP_ROUTE_REDISTRIBUTE
,
2476 * If there are no remaining valid interior routes at this prefix,
2477 * we need to look up the tree for a possible node to move monitors to
2479 if (RFAPI_MONITOR_EXTERIOR(rn_interior
)->valid_interior_count
) {
2480 vnc_zlog_debug_verbose(
2481 "%s: interior routes still present, skipping",
2487 * Find nearest parent with at least one valid interior route
2488 * If none is found, par will end up NULL, and we will move
2489 * the monitors to the orphan list for this import table
2491 for (par
= agg_node_parent(rn_interior
); par
;
2492 par
= agg_node_parent(par
)) {
2493 if (RFAPI_MONITOR_EXTERIOR(par
)->valid_interior_count
)
2497 vnc_zlog_debug_verbose("%s: par=%p, ext src: %p", __func__
, par
,
2498 RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
);
2500 /* move all monitors */
2502 * We will use and delete every element of the source skiplist
2504 while (!skiplist_first(RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
,
2505 (void **)&bpi_exterior
,
2506 (void **)&pfx_exterior
)) {
2508 struct prefix
*pfx_mon
= prefix_new();
2510 prefix_copy(pfx_mon
, pfx_exterior
);
2514 struct bgp_path_info
*bpi
;
2517 * Add monitor to parent node
2519 if (!RFAPI_MONITOR_EXTERIOR(par
)->source
) {
2520 RFAPI_MONITOR_EXTERIOR(par
)->source
=
2522 0, NULL
, prefix_free_lists
);
2523 agg_lock_node(par
); /* sl */
2525 skiplist_insert(RFAPI_MONITOR_EXTERIOR(par
)->source
,
2526 bpi_exterior
, pfx_mon
);
2527 agg_lock_node(par
); /* sl entry */
2529 /* Add constructed exterior routes based on parent */
2530 for (bpi
= par
->info
; bpi
; bpi
= bpi
->next
) {
2532 struct prefix_rd
*prd
;
2533 struct attr new_attr
;
2536 if (bpi
->type
== ZEBRA_ROUTE_BGP_DIRECT_EXT
)
2540 prd
= &bpi
->extra
->vnc
.import
.rd
;
2541 label
= decode_label(
2542 &bpi
->extra
->label
[0]);
2546 /* use local_pref from unicast route */
2547 memset(&new_attr
, 0, sizeof(struct attr
));
2548 new_attr
= *bpi
->attr
;
2550 && (bpi_exterior
->attr
->flag
2551 & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
))) {
2552 new_attr
.local_pref
=
2553 bpi_exterior
->attr
->local_pref
;
2554 new_attr
.flag
|= ATTR_FLAG_BIT(
2555 BGP_ATTR_LOCAL_PREF
);
2558 rfapiBgpInfoFilteredImportVPN(
2559 it
, FIF_ACTION_UPDATE
, bpi
->peer
,
2561 pfx_exterior
, NULL
, afi
, prd
, &new_attr
,
2562 ZEBRA_ROUTE_BGP_DIRECT_EXT
,
2563 BGP_ROUTE_REDISTRIBUTE
, &label
);
2569 * No interior route for exterior's nexthop. Save
2571 * in orphan list to await future route.
2573 skiplist_insert(it
->monitor_exterior_orphans
,
2574 bpi_exterior
, pfx_mon
);
2577 skiplist_delete_first(
2578 RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
);
2579 agg_unlock_node(rn_interior
); /* sl entry */
2581 if (skiplist_empty(RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
)) {
2582 skiplist_free(RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
);
2583 RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
= NULL
;
2584 agg_unlock_node(rn_interior
); /* sl itself */
2588 /***********************************************************************
2589 * Generic add/delete unicast routes
2590 ***********************************************************************/
2592 void vnc_import_bgp_add_route(struct bgp
*bgp
, const struct prefix
*prefix
,
2593 struct bgp_path_info
*info
)
2595 afi_t afi
= family2afi(prefix
->family
);
2597 if (VNC_DEBUG(VERBOSE
)) {
2598 struct prefix pfx_nexthop
;
2599 char buf
[PREFIX_STRLEN
];
2600 char buf_nh
[PREFIX_STRLEN
];
2602 prefix2str(prefix
, buf
, sizeof(buf
));
2603 rfapiUnicastNexthop2Prefix(afi
, info
->attr
, &pfx_nexthop
);
2604 prefix2str(&pfx_nexthop
, buf_nh
, sizeof(buf_nh
));
2606 vnc_zlog_debug_verbose("%s: pfx %s, nh %s", __func__
, buf
,
2610 print_rhn_list(__func__
, "ENTER ");
2615 flog_err(EC_LIB_DEVELOPMENT
, "%s: can't get afi of prefix",
2620 if (!bgp
->rfapi_cfg
) {
2621 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
2626 /* check vnc redist flag for bgp direct routes */
2627 if (!bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT
]) {
2628 vnc_zlog_debug_verbose(
2629 "%s: bgp->rfapi_cfg->redist[afi=%d][type=%d=ZEBRA_ROUTE_BGP_DIRECT] is 0, skipping",
2630 __func__
, afi
, ZEBRA_ROUTE_BGP_DIRECT
);
2634 switch (bgp
->rfapi_cfg
->redist_mode
) {
2635 case VNC_REDIST_MODE_PLAIN
:
2636 vnc_import_bgp_add_route_mode_plain(bgp
, prefix
, info
);
2639 case VNC_REDIST_MODE_RFG
:
2640 if (bgp
->rfapi_cfg
->rfg_redist
)
2641 vnc_import_bgp_add_route_mode_nvegroup(
2642 bgp
, prefix
, info
, bgp
->rfapi_cfg
->rfg_redist
);
2644 vnc_zlog_debug_verbose("%s: mode RFG but no redist RFG",
2648 case VNC_REDIST_MODE_RESOLVE_NVE
:
2649 vnc_import_bgp_add_route_mode_resolve_nve(bgp
, prefix
, info
);
2653 print_rhn_list(__func__
, "LEAVE ");
2659 * "Withdrawing a Route" import process
2661 void vnc_import_bgp_del_route(struct bgp
*bgp
, const struct prefix
*prefix
,
2662 struct bgp_path_info
*info
) /* unicast info */
2664 afi_t afi
= family2afi(prefix
->family
);
2669 struct prefix pfx_nexthop
;
2670 char buf
[PREFIX_STRLEN
];
2671 char buf_nh
[PREFIX_STRLEN
];
2673 prefix2str(prefix
, buf
, sizeof(buf
));
2674 rfapiUnicastNexthop2Prefix(afi
, info
->attr
, &pfx_nexthop
);
2675 prefix2str(&pfx_nexthop
, buf_nh
, sizeof(buf_nh
));
2677 vnc_zlog_debug_verbose("%s: pfx %s, nh %s", __func__
, buf
,
2681 print_rhn_list(__func__
, "ENTER ");
2685 if (!bgp
->rfapi_cfg
) {
2686 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
2691 /* check bgp redist flag for vnc direct ("vpn") routes */
2692 if (!bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT
]) {
2693 vnc_zlog_debug_verbose(
2694 "%s: bgp redistribution of afi=%d VNC direct routes is off",
2699 switch (bgp
->rfapi_cfg
->redist_mode
) {
2700 case VNC_REDIST_MODE_PLAIN
:
2701 vnc_import_bgp_del_route_mode_plain(bgp
, prefix
, info
);
2704 case VNC_REDIST_MODE_RFG
:
2705 if (bgp
->rfapi_cfg
->rfg_redist
)
2706 vnc_import_bgp_del_route_mode_nvegroup(bgp
, prefix
,
2709 vnc_zlog_debug_verbose("%s: mode RFG but no redist RFG",
2713 case VNC_REDIST_MODE_RESOLVE_NVE
:
2714 vnc_import_bgp_del_route_mode_resolve_nve(bgp
, afi
, prefix
,
2719 print_rhn_list(__func__
, "LEAVE ");
2725 /***********************************************************************
2727 ***********************************************************************/
2729 void vnc_import_bgp_redist_enable(struct bgp
*bgp
, afi_t afi
)
2731 /* iterate over bgp unicast v4 and v6 routes, call
2732 * vnc_import_bgp_add_route */
2734 struct bgp_node
*rn
;
2736 vnc_zlog_debug_verbose("%s: entry, afi=%d", __func__
, afi
);
2738 if (bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT
]) {
2739 vnc_zlog_debug_verbose(
2740 "%s: already enabled for afi %d, skipping", __func__
,
2744 bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT
] = 1;
2746 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
;
2747 rn
= bgp_route_next(rn
)) {
2749 struct bgp_path_info
*bpi
;
2751 for (bpi
= bgp_node_get_bgp_path_info(rn
); bpi
;
2754 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
))
2757 vnc_import_bgp_add_route(bgp
, &rn
->p
, bpi
);
2760 vnc_zlog_debug_verbose(
2761 "%s: set redist[afi=%d][type=%d=ZEBRA_ROUTE_BGP_DIRECT] return",
2762 __func__
, afi
, ZEBRA_ROUTE_BGP_DIRECT
);
2765 void vnc_import_bgp_exterior_redist_enable(struct bgp
*bgp
, afi_t afi
)
2767 struct bgp
*bgp_exterior
;
2768 struct bgp_node
*rn
;
2770 bgp_exterior
= bgp
->rfapi_cfg
->redist_bgp_exterior_view
;
2772 if (bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT_EXT
]) {
2773 vnc_zlog_debug_verbose(
2774 "%s: already enabled for afi %d, skipping", __func__
,
2778 bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT_EXT
] = 1;
2780 if (!bgp_exterior
) {
2781 vnc_zlog_debug_verbose(
2782 "%s: no exterior view set yet, no routes to import yet",
2787 for (rn
= bgp_table_top(bgp_exterior
->rib
[afi
][SAFI_UNICAST
]); rn
;
2788 rn
= bgp_route_next(rn
)) {
2790 struct bgp_path_info
*bpi
;
2792 for (bpi
= bgp_node_get_bgp_path_info(rn
); bpi
;
2795 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
))
2798 vnc_import_bgp_exterior_add_route(bgp_exterior
, &rn
->p
,
2802 vnc_zlog_debug_verbose(
2803 "%s: set redist[afi=%d][type=%d=ZEBRA_ROUTE_BGP_DIRECT] return",
2804 __func__
, afi
, ZEBRA_ROUTE_BGP_DIRECT
);
2808 * This function is for populating a newly-created Import Table
2810 void vnc_import_bgp_exterior_redist_enable_it(
2811 struct bgp
*bgp
, afi_t afi
, struct rfapi_import_table
*it_only
)
2813 struct bgp
*bgp_exterior
;
2814 struct bgp_node
*rn
;
2816 vnc_zlog_debug_verbose("%s: entry", __func__
);
2818 bgp_exterior
= bgp
->rfapi_cfg
->redist_bgp_exterior_view
;
2820 if (!bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT_EXT
]) {
2821 vnc_zlog_debug_verbose("%s: not enabled for afi %d, skipping",
2826 if (!bgp_exterior
) {
2827 vnc_zlog_debug_verbose(
2828 "%s: no exterior view set yet, no routes to import yet",
2833 for (rn
= bgp_table_top(bgp_exterior
->rib
[afi
][SAFI_UNICAST
]); rn
;
2834 rn
= bgp_route_next(rn
)) {
2836 struct bgp_path_info
*bpi
;
2838 for (bpi
= bgp_node_get_bgp_path_info(rn
); bpi
;
2841 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
))
2844 vnc_import_bgp_exterior_add_route_it(
2845 bgp_exterior
, &rn
->p
, bpi
, it_only
);
2851 void vnc_import_bgp_redist_disable(struct bgp
*bgp
, afi_t afi
)
2854 * iterate over vpn routes, find routes of type ZEBRA_ROUTE_BGP_DIRECT,
2855 * delete (call timer expire immediately)
2857 struct bgp_node
*rn1
;
2858 struct bgp_node
*rn2
;
2860 vnc_zlog_debug_verbose("%s: entry", __func__
);
2862 if (!bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT
]) {
2863 vnc_zlog_debug_verbose(
2864 "%s: already disabled for afi %d, skipping", __func__
,
2870 * Two-level table for SAFI_MPLS_VPN
2871 * Be careful when changing the things we iterate over
2873 for (rn1
= bgp_table_top(bgp
->rib
[afi
][SAFI_MPLS_VPN
]); rn1
;
2874 rn1
= bgp_route_next(rn1
)) {
2876 if (bgp_node_has_bgp_path_info_data(rn1
)) {
2878 for (rn2
= bgp_table_top(
2879 bgp_node_get_bgp_table_info(rn1
));
2880 rn2
; rn2
= bgp_route_next(rn2
)) {
2882 struct bgp_path_info
*bpi
;
2883 struct bgp_path_info
*nextbpi
;
2885 for (bpi
= bgp_node_get_bgp_path_info(rn2
); bpi
;
2888 nextbpi
= bpi
->next
;
2891 == ZEBRA_ROUTE_BGP_DIRECT
) {
2893 struct rfapi_descriptor
*rfd
;
2894 vncHDBgpDirect
.peer
= bpi
->peer
;
2898 rfd
= bpi
->extra
->vnc
.export
2901 vnc_zlog_debug_verbose(
2902 "%s: deleting bpi=%p, bpi->peer=%p, bpi->type=%d, bpi->sub_type=%d, bpi->extra->vnc.export.rfapi_handle=%p [passing rfd=%p]",
2904 bpi
->peer
, bpi
->type
,
2916 rfd
, bpi
->peer
, bgp
,
2917 SAFI_MPLS_VPN
, &rn2
->p
,
2918 (struct prefix_rd
*)&rn1
2921 bpi
->sub_type
, NULL
,
2924 vncHDBgpDirect
.peer
= NULL
;
2930 /* Clear RHN list */
2931 if (bgp
->rfapi
->resolve_nve_nexthop
) {
2932 struct prefix_bag
*pb
;
2933 struct bgp_path_info
*info
;
2934 while (!skiplist_first(bgp
->rfapi
->resolve_nve_nexthop
, NULL
,
2937 skiplist_delete_first(bgp
->rfapi
->resolve_nve_nexthop
);
2938 bgp_path_info_unlock(info
);
2942 bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT
] = 0;
2943 vnc_zlog_debug_verbose("%s: return", __func__
);
2947 void vnc_import_bgp_exterior_redist_disable(struct bgp
*bgp
, afi_t afi
)
2949 struct rfapi_cfg
*hc
= bgp
->rfapi_cfg
;
2950 struct bgp
*bgp_exterior
= hc
->redist_bgp_exterior_view
;
2952 vnc_zlog_debug_verbose("%s: entry", __func__
);
2954 if (!hc
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT_EXT
]) {
2955 vnc_zlog_debug_verbose(
2956 "%s: already disabled for afi %d, skipping", __func__
,
2961 if (!bgp_exterior
) {
2962 vnc_zlog_debug_verbose(
2963 "%s: bgp exterior view not defined, skipping",
2970 struct bgp_node
*rn
;
2971 for (rn
= bgp_table_top(bgp_exterior
->rib
[afi
][SAFI_UNICAST
]);
2972 rn
; rn
= bgp_route_next(rn
)) {
2974 struct bgp_path_info
*bpi
;
2976 for (bpi
= bgp_node_get_bgp_path_info(rn
); bpi
;
2979 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
))
2982 vnc_import_bgp_exterior_del_route(bgp_exterior
,
2987 print_rhn_list(__func__
, NULL
);
2991 bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT_EXT
] = 0;
2992 vnc_zlog_debug_verbose("%s: return", __func__
);