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(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(void *pfx1
, void *pfx2
)
137 struct prefix
*p1
= pfx1
;
138 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 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 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
, struct prefix
*prefix
, /* unicast prefix */
515 struct ecommunity
*ecom
, /* generated ecoms */
516 uint32_t *local_pref
, /* NULL = no local_pref */
517 uint32_t *med
, /* NULL = no med */
518 struct prefix
*ubpi_nexthop
) /* unicast nexthop */
521 struct bgp_path_info
*bpi
;
527 char str_nh
[PREFIX_STRLEN
];
529 prefix2str(ubpi_nexthop
, str_nh
, sizeof(str_nh
));
531 vnc_zlog_debug_verbose("%s: ubpi_nexthop=%s", __func__
, str_nh
);
535 bn
= bgp_node_lookup(table_rd
, ubpi_nexthop
);
537 vnc_zlog_debug_verbose(
538 "%s: no match in RD's table for ubpi_nexthop",
543 /* Iterate over bgp_info items at this node */
544 for (bpi
= bgp_node_get_bgp_path_info(bn
); bpi
; bpi
= bpi
->next
) {
546 vnc_import_bgp_add_route_mode_resolve_nve_one_bi(
547 bgp
, afi
, bpi
, /* VPN bpi */
548 prd
, prefix
, local_pref
, med
, ecom
);
554 static void vnc_import_bgp_add_route_mode_resolve_nve(
555 struct bgp
*bgp
, struct prefix
*prefix
, /* unicast prefix */
556 struct bgp_path_info
*info
) /* unicast info */
558 afi_t afi
= family2afi(prefix
->family
);
560 struct prefix pfx_unicast_nexthop
= {0}; /* happy valgrind */
562 struct ecommunity
*ecom
= NULL
;
564 uint32_t *med
= NULL
;
566 struct prefix_bag
*pb
;
567 struct bgp_node
*bnp
; /* prd table node */
570 if (VNC_DEBUG(VERBOSE
)) {
571 char str_pfx
[PREFIX_STRLEN
];
572 char str_nh
[PREFIX_STRLEN
];
575 prefix2str(prefix
, str_pfx
, sizeof(str_pfx
));
578 rfapiUnicastNexthop2Prefix(afi
, info
->attr
, &nh
);
580 prefix2str(&nh
, str_nh
, sizeof(str_nh
));
586 vnc_zlog_debug_verbose(
587 "%s(bgp=%p, unicast prefix=%s, unicast nh=%s)",
588 __func__
, bgp
, str_pfx
, str_nh
);
591 if (info
->type
!= ZEBRA_ROUTE_BGP
) {
592 vnc_zlog_debug_verbose(
593 "%s: unicast type %d=\"%s\" is not %d=%s, skipping",
594 __func__
, info
->type
, zebra_route_string(info
->type
),
595 ZEBRA_ROUTE_BGP
, "ZEBRA_ROUTE_BGP");
604 flog_err(EC_LIB_DEVELOPMENT
, "%s: can't get afi of prefix",
609 if (!(bgp
->rfapi_cfg
)) {
610 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
615 /* check vnc redist flag for bgp direct routes */
616 if (!bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT
]) {
617 vnc_zlog_debug_verbose(
618 "%s: bgp->rfapi_cfg->redist[afi=%d][type=ZEBRA_ROUTE_BGP_DIRECT] is 0, skipping",
624 if (process_unicast_route(bgp
, afi
, prefix
, info
, &ecom
,
625 &pfx_unicast_nexthop
)) {
627 vnc_zlog_debug_verbose(
628 "%s: process_unicast_route error, skipping", __func__
);
632 local_pref
= calc_local_pref(info
->attr
, info
->peer
);
633 if (info
->attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
))
634 med
= &info
->attr
->med
;
637 * At this point, we have allocated:
639 * ecom ecommunity ptr, union of unicast and ROO parts (no NVE part)
643 * pfx_unicast_nexthop nexthop of uncast route
646 if (!bgp
->rfapi
->resolve_nve_nexthop
) {
647 bgp
->rfapi
->resolve_nve_nexthop
=
648 skiplist_new(SKIPLIST_FLAG_ALLOW_DUPLICATES
,
649 vnc_prefix_cmp
, prefix_bag_free
);
652 pb
= XCALLOC(MTYPE_RFAPI_PREFIX_BAG
, sizeof(struct prefix_bag
));
653 pb
->hpfx
= pfx_unicast_nexthop
;
657 bgp_path_info_lock(info
); /* skiplist refers to it */
658 skiplist_insert(bgp
->rfapi
->resolve_nve_nexthop
, &pb
->hpfx
, pb
);
661 * Iterate over RDs in VPN RIB. For each RD, look up unicast nexthop
662 * (exact match, /32). If an exact match is found, call add_vnc_route.
665 for (bnp
= bgp_table_top(bgp
->rib
[afi
][SAFI_MPLS_VPN
]); bnp
;
666 bnp
= bgp_route_next(bnp
)) {
668 struct bgp_table
*table
;
670 table
= bgp_node_get_bgp_table_info(bnp
);
675 vnc_import_bgp_add_route_mode_resolve_nve_one_rd(
676 (struct prefix_rd
*)&bnp
->p
, table
, afi
, bgp
, prefix
,
677 ecom
, &local_pref
, med
, &pfx_unicast_nexthop
);
682 ecommunity_free(&ecom
);
684 vnc_zlog_debug_verbose("%s: done", __func__
);
688 static void vnc_import_bgp_add_route_mode_plain(struct bgp
*bgp
,
689 struct prefix
*prefix
,
690 struct bgp_path_info
*info
)
692 afi_t afi
= family2afi(prefix
->family
);
693 struct peer
*peer
= info
->peer
;
694 struct attr
*attr
= info
->attr
;
696 struct rfapi_cfg
*hc
= bgp
->rfapi_cfg
;
697 struct attr
*iattr
= NULL
;
699 struct rfapi_ip_addr vnaddr
;
700 struct prefix vn_pfx_space
;
701 struct prefix
*vn_pfx
= NULL
;
703 struct ecommunity
*ecom
= NULL
;
704 struct prefix_rd prd
;
705 struct route_map
*rmap
= NULL
;
707 uint32_t *med
= NULL
;
710 char buf
[PREFIX_STRLEN
];
712 prefix2str(prefix
, buf
, sizeof(buf
));
713 vnc_zlog_debug_verbose("%s(prefix=%s) entry", __func__
, buf
);
717 flog_err(EC_LIB_DEVELOPMENT
, "%s: can't get afi of prefix",
723 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
728 /* check vnc redist flag for bgp direct routes */
729 if (!bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT
]) {
730 vnc_zlog_debug_verbose(
731 "%s: bgp->rfapi_cfg->redist[afi=%d][type=ZEBRA_ROUTE_BGP_DIRECT] is 0, skipping",
737 * mode "plain" specific code
740 vnc_zlog_debug_verbose("%s: NOT using redist RFG", __func__
);
745 if (hc
->plist_redist
[ZEBRA_ROUTE_BGP_DIRECT
][afi
]) {
746 vnc_zlog_debug_verbose(
747 "%s: HC prefix list is set, checking",
749 if (prefix_list_apply(
750 hc
->plist_redist
[ZEBRA_ROUTE_BGP_DIRECT
]
754 vnc_zlog_debug_verbose(
755 "%s: prefix list returns DENY, blocking route",
759 vnc_zlog_debug_verbose(
760 "%s: prefix list returns PASS, allowing route",
764 /* apply routemap, if any, later */
765 rmap
= hc
->routemap_redist
[ZEBRA_ROUTE_BGP_DIRECT
];
768 * Incoming prefix is unicast. If v6, it is in multiprotocol
770 * but if v4 it is in attr->nexthop
772 rfapiUnicastNexthop2Prefix(afi
, attr
, &vn_pfx_space
);
773 vn_pfx
= &vn_pfx_space
;
776 ahr_flags
|= RFAPI_AHR_NO_TUNNEL_SUBTLV
;
779 if (VNC_DEBUG(IMPORT_BGP_ADD_ROUTE
)) {
780 char buf
[PREFIX_STRLEN
];
782 prefix2str(vn_pfx
, buf
, sizeof(buf
));
783 vnc_zlog_debug_any("%s vn_pfx=%s", __func__
, buf
);
789 if (rfapiQprefix2Raddr(vn_pfx
, &vnaddr
)) {
790 vnc_zlog_debug_verbose("%s: redist VN invalid, skipping",
797 * This code is here because it allocates an interned attr which
798 * must be freed before we return. It's easier to put it after
799 * all of the possible returns above.
801 memset(&hattr
, 0, sizeof(struct attr
));
802 /* hattr becomes a ghost attr */
806 struct bgp_path_info info
;
807 route_map_result_t ret
;
809 memset(&info
, 0, sizeof(info
));
812 ret
= route_map_apply(rmap
, prefix
, RMAP_BGP
, &info
);
813 if (ret
== RMAP_DENYMATCH
) {
814 bgp_attr_flush(&hattr
);
815 vnc_zlog_debug_verbose(
816 "%s: route map \"%s\" says DENY, returning",
817 __func__
, rmap
->name
);
822 iattr
= bgp_attr_intern(&hattr
);
823 bgp_attr_flush(&hattr
);
825 /* Now iattr is an allocated interned attr */
828 * Mode "plain" specific code
830 * Sets RD in dummy HD
834 if (vnaddr
.addr_family
!= AF_INET
) {
835 vnc_zlog_debug_verbose(
836 "%s: can't auto-assign RD, VN AF (%d) is not IPv4, skipping",
837 __func__
, vnaddr
.addr_family
);
839 bgp_attr_unintern(&iattr
);
843 memset(&prd
, 0, sizeof(prd
));
844 rfapi_set_autord_from_vn(&prd
, &vnaddr
);
846 if (iattr
&& iattr
->ecommunity
)
847 ecom
= ecommunity_dup(iattr
->ecommunity
);
850 local_pref
= calc_local_pref(iattr
, peer
);
852 if (iattr
&& (iattr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
))) {
856 if (VNC_DEBUG(IMPORT_BGP_ADD_ROUTE
)) {
857 char buf
[PREFIX_STRLEN
];
859 rfapiRfapiIpAddr2Str(&vnaddr
, buf
, sizeof(buf
));
860 vnc_zlog_debug_any("%s: setting vnaddr to %s", __func__
, buf
);
863 vncHDBgpDirect
.peer
= peer
;
864 add_vnc_route(&vncHDBgpDirect
, bgp
, SAFI_MPLS_VPN
, prefix
, &prd
,
865 &vnaddr
, &local_pref
, &(bgp
->rfapi_cfg
->redist_lifetime
),
866 NULL
, /* RFP options */
867 NULL
, NULL
, ecom
, med
, /* med */
868 NULL
, /* label: default */
869 ZEBRA_ROUTE_BGP_DIRECT
, BGP_ROUTE_REDISTRIBUTE
,
871 vncHDBgpDirect
.peer
= NULL
;
874 ecommunity_free(&ecom
);
878 vnc_import_bgp_add_route_mode_nvegroup(struct bgp
*bgp
, struct prefix
*prefix
,
879 struct bgp_path_info
*info
,
880 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 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 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 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
, struct prefix
*prefix
, /* unicast prefix */
1276 struct prefix
*ubpi_nexthop
) /* unicast bpi's nexthop */
1278 struct bgp_node
*bn
;
1279 struct bgp_path_info
*bpi
;
1285 char str_nh
[PREFIX_STRLEN
];
1287 prefix2str(ubpi_nexthop
, str_nh
, sizeof(str_nh
));
1288 vnc_zlog_debug_verbose("%s: ubpi_nexthop=%s", __func__
, str_nh
);
1293 bn
= bgp_node_lookup(table_rd
, ubpi_nexthop
);
1295 vnc_zlog_debug_verbose(
1296 "%s: no match in RD's table for ubpi_nexthop",
1301 /* Iterate over bgp_info items at this node */
1302 for (bpi
= bgp_node_get_bgp_path_info(bn
); bpi
; bpi
= bpi
->next
) {
1304 vnc_import_bgp_del_route_mode_resolve_nve_one_bi(
1305 bgp
, afi
, bpi
, /* VPN bpi */
1307 prefix
); /* unicast route prefix */
1310 bgp_unlock_node(bn
);
1314 vnc_import_bgp_del_route_mode_resolve_nve(struct bgp
*bgp
, afi_t afi
,
1315 struct prefix
*prefix
,
1316 struct bgp_path_info
*info
)
1318 struct ecommunity
*ecom
= NULL
;
1319 struct prefix pfx_unicast_nexthop
= {0}; /* happy valgrind */
1321 // struct listnode *hnode;
1322 // struct rfapi_descriptor *rfd;
1323 struct prefix_bag
*pb
;
1325 struct skiplist
*sl
= bgp
->rfapi
->resolve_nve_nexthop
;
1327 struct bgp_node
*bnp
; /* prd table node */
1330 vnc_zlog_debug_verbose("%s: no RHN entries, skipping",
1335 if (info
->type
!= ZEBRA_ROUTE_BGP
) {
1336 vnc_zlog_debug_verbose(
1337 "%s: unicast type %d=\"%s\" is not %d=%s, skipping",
1338 __func__
, info
->type
, zebra_route_string(info
->type
),
1339 ZEBRA_ROUTE_BGP
, "ZEBRA_ROUTE_BGP");
1343 if (process_unicast_route(bgp
, afi
, prefix
, info
, &ecom
,
1344 &pfx_unicast_nexthop
)) {
1346 vnc_zlog_debug_verbose(
1347 "%s: process_unicast_route error, skipping", __func__
);
1351 rc
= skiplist_first_value(sl
, &pfx_unicast_nexthop
, (void *)&pb
,
1354 if (pb
->ubpi
== info
) {
1355 skiplist_delete(sl
, &pfx_unicast_nexthop
, pb
);
1356 bgp_path_info_unlock(info
);
1359 rc
= skiplist_next_value(sl
, &pfx_unicast_nexthop
, (void *)&pb
,
1364 * Iterate over RDs in VPN RIB. For each RD, look up unicast nexthop
1365 * (exact match, /32). If an exact match is found, call add_vnc_route.
1368 for (bnp
= bgp_table_top(bgp
->rib
[afi
][SAFI_MPLS_VPN
]); bnp
;
1369 bnp
= bgp_route_next(bnp
)) {
1371 struct bgp_table
*table
;
1373 table
= bgp_node_get_bgp_table_info(bnp
);
1378 vnc_import_bgp_del_route_mode_resolve_nve_one_rd(
1379 (struct prefix_rd
*)&bnp
->p
, table
, afi
, bgp
, prefix
,
1380 &pfx_unicast_nexthop
); /* TBD how is this set? */
1384 ecommunity_free(&ecom
);
1388 /***********************************************************************
1389 * Add/Delete CE->NVE routes
1390 ***********************************************************************/
1393 * Should be called whan a bpi is added to VPN RIB. This function
1394 * will check if it is a host route and return immediately if not.
1396 void vnc_import_bgp_add_vnc_host_route_mode_resolve_nve(
1397 struct bgp
*bgp
, struct prefix_rd
*prd
, /* RD */
1398 struct bgp_table
*table_rd
, /* per-rd VPN route table */
1399 struct prefix
*prefix
, /* VPN prefix */
1400 struct bgp_path_info
*bpi
) /* new VPN host route */
1402 afi_t afi
= family2afi(prefix
->family
);
1403 struct skiplist
*sl
= NULL
;
1405 struct prefix_bag
*pb
;
1407 struct rfapi_cfg
*hc
= NULL
;
1409 vnc_zlog_debug_verbose("%s: entry", __func__
);
1411 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
) {
1412 vnc_zlog_debug_verbose("%s: bad afi %d, skipping", __func__
,
1417 if (!(hc
= bgp
->rfapi_cfg
)) {
1418 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
1423 /* check vnc redist flag for bgp direct routes */
1424 if (!hc
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT
]) {
1425 vnc_zlog_debug_verbose(
1426 "%s: bgp->rfapi_cfg->redist[afi=%d][type=ZEBRA_ROUTE_BGP_DIRECT] is 0, skipping",
1431 if (hc
->redist_mode
!= VNC_REDIST_MODE_RESOLVE_NVE
) {
1432 vnc_zlog_debug_verbose("%s: not in resolve-nve mode, skipping",
1438 sl
= bgp
->rfapi
->resolve_nve_nexthop
;
1441 vnc_zlog_debug_verbose(
1442 "%s: no resolve_nve_nexthop skiplist, skipping",
1447 if (!is_host_prefix(prefix
)) {
1448 vnc_zlog_debug_verbose("%s: not host prefix, skipping",
1453 rc
= skiplist_first_value(sl
, prefix
, (void *)&pb
, &cursor
);
1455 struct ecommunity
*ecom
;
1456 struct prefix pfx_unicast_nexthop
;
1457 uint32_t *med
= NULL
;
1458 uint32_t local_pref
;
1460 memset(&pfx_unicast_nexthop
, 0,
1461 sizeof(struct prefix
)); /* keep valgrind happy */
1463 if (VNC_DEBUG(IMPORT_BGP_ADD_ROUTE
)) {
1464 char hbuf
[PREFIX_STRLEN
];
1465 char ubuf
[PREFIX_STRLEN
];
1467 prefix2str(&pb
->hpfx
, hbuf
, sizeof(hbuf
));
1468 prefix2str(&pb
->upfx
, ubuf
, sizeof(ubuf
));
1471 "%s: examining RHN Entry (q=%p): upfx=%s, hpfx=%s, ubpi=%p",
1472 __func__
, cursor
, ubuf
, hbuf
, pb
->ubpi
);
1475 if (process_unicast_route(bgp
, afi
, &pb
->upfx
, pb
->ubpi
, &ecom
,
1476 &pfx_unicast_nexthop
)) {
1478 vnc_zlog_debug_verbose(
1479 "%s: process_unicast_route error, skipping",
1483 local_pref
= calc_local_pref(pb
->ubpi
->attr
, pb
->ubpi
->peer
);
1485 if (pb
->ubpi
->attr
->flag
1486 & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC
))
1487 med
= &pb
->ubpi
->attr
->med
;
1492 if (vnc_prefix_cmp(&pfx_unicast_nexthop
, prefix
)) {
1493 char str_unh
[PREFIX_STRLEN
];
1494 char str_nve_pfx
[PREFIX_STRLEN
];
1496 prefix2str(&pfx_unicast_nexthop
, str_unh
,
1498 prefix2str(prefix
, str_nve_pfx
, sizeof(str_nve_pfx
));
1500 vnc_zlog_debug_verbose(
1501 "%s: FATAL: resolve_nve_nexthop list item bpi nexthop %s != nve pfx %s",
1502 __func__
, str_unh
, str_nve_pfx
);
1506 vnc_import_bgp_add_route_mode_resolve_nve_one_bi(
1507 bgp
, afi
, bpi
, /* VPN bpi */
1508 prd
, &pb
->upfx
, /* unicast prefix */
1509 &local_pref
, med
, ecom
);
1512 ecommunity_free(&ecom
);
1517 char pbuf
[PREFIX_STRLEN
];
1519 prefix2str(prefix
, pbuf
, sizeof(pbuf
));
1521 vnc_zlog_debug_verbose(
1522 "%s: advancing past RHN Entry (q=%p): with prefix %s",
1523 __func__
, cursor
, pbuf
);
1524 print_rhn_list(__func__
, NULL
); /* debug */
1527 rc
= skiplist_next_value(sl
, prefix
, (void *)&pb
, &cursor
);
1529 vnc_zlog_debug_verbose("%s: done", __func__
);
1533 void vnc_import_bgp_del_vnc_host_route_mode_resolve_nve(
1534 struct bgp
*bgp
, struct prefix_rd
*prd
, /* RD */
1535 struct bgp_table
*table_rd
, /* per-rd VPN route table */
1536 struct prefix
*prefix
, /* VPN prefix */
1537 struct bgp_path_info
*bpi
) /* old VPN host route */
1539 afi_t afi
= family2afi(prefix
->family
);
1540 struct skiplist
*sl
= NULL
;
1541 struct prefix_bag
*pb
;
1543 struct rfapi_cfg
*hc
= NULL
;
1547 char str_pfx
[PREFIX_STRLEN
];
1549 prefix2str(prefix
, str_pfx
, sizeof(str_pfx
));
1551 vnc_zlog_debug_verbose("%s(bgp=%p, nve prefix=%s)", __func__
,
1555 if (afi
!= AFI_IP
&& afi
!= AFI_IP6
)
1558 if (!(hc
= bgp
->rfapi_cfg
)) {
1559 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
1564 /* check vnc redist flag for bgp direct routes */
1565 if (!hc
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT
]) {
1566 vnc_zlog_debug_verbose(
1567 "%s: bgp->rfapi_cfg->redist[afi=%d][type=ZEBRA_ROUTE_BGP_DIRECT] is 0, skipping",
1572 if (hc
->redist_mode
!= VNC_REDIST_MODE_RESOLVE_NVE
) {
1573 vnc_zlog_debug_verbose("%s: not in resolve-nve mode, skipping",
1579 sl
= bgp
->rfapi
->resolve_nve_nexthop
;
1582 vnc_zlog_debug_verbose("%s: no RHN entries, skipping",
1587 if (!is_host_prefix(prefix
)) {
1588 vnc_zlog_debug_verbose("%s: not host route, skip", __func__
);
1593 * Find all entries with key == CE in the RHN list
1595 rc
= skiplist_first_value(sl
, prefix
, (void *)&pb
, &cursor
);
1598 struct ecommunity
*ecom
;
1599 struct prefix pfx_unicast_nexthop
;
1601 memset(&pfx_unicast_nexthop
, 0,
1602 sizeof(struct prefix
)); /* keep valgrind happy */
1604 if (process_unicast_route(bgp
, afi
, &pb
->upfx
, pb
->ubpi
, &ecom
,
1605 &pfx_unicast_nexthop
)) {
1607 vnc_zlog_debug_verbose(
1608 "%s: process_unicast_route error, skipping",
1616 if (vnc_prefix_cmp(&pfx_unicast_nexthop
, prefix
)) {
1617 char str_unh
[PREFIX_STRLEN
];
1618 char str_nve_pfx
[PREFIX_STRLEN
];
1620 prefix2str(&pfx_unicast_nexthop
, str_unh
,
1622 prefix2str(prefix
, str_nve_pfx
, sizeof(str_nve_pfx
));
1624 vnc_zlog_debug_verbose(
1625 "%s: FATAL: resolve_nve_nexthop list item bpi nexthop %s != nve pfx %s",
1626 __func__
, str_unh
, str_nve_pfx
);
1630 vnc_import_bgp_del_route_mode_resolve_nve_one_bi(
1631 bgp
, afi
, bpi
, prd
, &pb
->upfx
);
1634 ecommunity_free(&ecom
);
1636 rc
= skiplist_next_value(sl
, prefix
, (void *)&pb
, &cursor
);
1641 /***********************************************************************
1643 ***********************************************************************/
1645 #define DEBUG_IS_USABLE_INTERIOR 1
1647 static int is_usable_interior_route(struct bgp_path_info
*bpi_interior
)
1649 if (!VALID_INTERIOR_TYPE(bpi_interior
->type
)) {
1650 #if DEBUG_IS_USABLE_INTERIOR
1651 vnc_zlog_debug_verbose(
1652 "%s: NO: type %d is not valid interior type", __func__
,
1653 bpi_interior
->type
);
1657 if (!CHECK_FLAG(bpi_interior
->flags
, BGP_PATH_VALID
)) {
1658 #if DEBUG_IS_USABLE_INTERIOR
1659 vnc_zlog_debug_verbose("%s: NO: BGP_PATH_VALID not set",
1668 * There should be only one of these per prefix at a time.
1669 * This should be called as a result of selection operation
1671 * NB should be called espacially for bgp instances that are named,
1672 * because the exterior routes will always come from one of those.
1673 * We filter here on the instance name to make sure we get only the
1676 static void vnc_import_bgp_exterior_add_route_it(
1677 struct bgp
*bgp
, /* exterior instance, we hope */
1678 struct prefix
*prefix
, /* unicast prefix */
1679 struct bgp_path_info
*info
, /* unicast info */
1680 struct rfapi_import_table
*it_only
) /* NULL, or limit to this IT */
1683 struct rfapi_cfg
*hc
;
1684 struct prefix pfx_orig_nexthop
;
1685 struct rfapi_import_table
*it
;
1686 struct bgp
*bgp_default
= bgp_get_default();
1687 afi_t afi
= family2afi(prefix
->family
);
1692 h
= bgp_default
->rfapi
;
1693 hc
= bgp_default
->rfapi_cfg
;
1695 vnc_zlog_debug_verbose("%s: entry with it=%p", __func__
, it_only
);
1698 vnc_zlog_debug_verbose(
1699 "%s: rfapi or rfapi_cfg not instantiated, skipping",
1703 if (!hc
->redist_bgp_exterior_view
) {
1704 vnc_zlog_debug_verbose("%s: exterior view not set, skipping",
1708 if (bgp
!= hc
->redist_bgp_exterior_view
) {
1709 vnc_zlog_debug_verbose(
1710 "%s: bgp %p != hc->redist_bgp_exterior_view %p, skipping",
1711 __func__
, bgp
, hc
->redist_bgp_exterior_view
);
1715 if (!hc
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT_EXT
]) {
1716 vnc_zlog_debug_verbose(
1717 "%s: redist of exterior routes not enabled, skipping",
1723 * Extract nexthop from exterior route
1725 * Incoming prefix is unicast. If v6, it is in multiprotocol area,
1726 * but if v4 it is in attr->nexthop
1728 rfapiUnicastNexthop2Prefix(afi
, info
->attr
, &pfx_orig_nexthop
);
1730 for (it
= h
->imports
; it
; it
= it
->next
) {
1731 struct agg_table
*table
;
1732 struct agg_node
*rn
;
1733 struct agg_node
*par
;
1734 struct bgp_path_info
*bpi_interior
;
1735 int have_usable_route
;
1737 vnc_zlog_debug_verbose("%s: doing it %p", __func__
, it
);
1739 if (it_only
&& (it_only
!= it
)) {
1740 vnc_zlog_debug_verbose("%s: doesn't match it_only %p",
1745 table
= it
->imported_vpn
[afi
];
1747 for (rn
= agg_node_match(table
, &pfx_orig_nexthop
),
1748 have_usable_route
= 0;
1749 (!have_usable_route
) && rn
;) {
1751 vnc_zlog_debug_verbose("%s: it %p trying rn %p",
1754 for (bpi_interior
= rn
->info
; bpi_interior
;
1755 bpi_interior
= bpi_interior
->next
) {
1756 struct prefix_rd
*prd
;
1757 struct attr new_attr
;
1760 if (!is_usable_interior_route(bpi_interior
))
1763 vnc_zlog_debug_verbose(
1764 "%s: usable: bpi_interior %p", __func__
,
1768 * have a legitimate route to exterior's nexthop
1771 * Import unicast route to the import table
1773 have_usable_route
= 1;
1775 if (bpi_interior
->extra
) {
1776 prd
= &bpi_interior
->extra
->vnc
.import
1778 label
= decode_label(
1779 &bpi_interior
->extra
->label
[0]);
1783 /* use local_pref from unicast route */
1784 memset(&new_attr
, 0, sizeof(struct attr
));
1785 new_attr
= *bpi_interior
->attr
;
1786 if (info
->attr
->flag
1787 & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)) {
1788 new_attr
.local_pref
=
1789 info
->attr
->local_pref
;
1790 new_attr
.flag
|= ATTR_FLAG_BIT(
1791 BGP_ATTR_LOCAL_PREF
);
1794 rfapiBgpInfoFilteredImportVPN(
1795 it
, FIF_ACTION_UPDATE
,
1796 bpi_interior
->peer
, NULL
, /* rfd */
1797 prefix
, NULL
, afi
, prd
, &new_attr
,
1798 ZEBRA_ROUTE_BGP_DIRECT_EXT
,
1799 BGP_ROUTE_REDISTRIBUTE
, &label
);
1802 if (have_usable_route
) {
1806 * TBD factor this out into its own function
1808 struct prefix
*pfx_mon
= prefix_new();
1809 if (!RFAPI_MONITOR_EXTERIOR(rn
)->source
) {
1810 RFAPI_MONITOR_EXTERIOR(rn
)->source
=
1814 agg_lock_node(rn
); /* for skiplist */
1816 agg_lock_node(rn
); /* for skiplist entry */
1817 prefix_copy(pfx_mon
, prefix
);
1818 if (!skiplist_insert(
1819 RFAPI_MONITOR_EXTERIOR(rn
)->source
,
1822 bgp_path_info_lock(info
);
1825 par
= agg_node_parent(rn
);
1828 agg_unlock_node(rn
);
1832 agg_unlock_node(rn
);
1834 if (!have_usable_route
) {
1835 struct prefix
*pfx_mon
= prefix_new();
1836 prefix_copy(pfx_mon
, prefix
);
1837 if (!skiplist_insert(it
->monitor_exterior_orphans
, info
,
1840 bgp_path_info_lock(info
);
1846 void vnc_import_bgp_exterior_add_route(
1847 struct bgp
*bgp
, /* exterior instance, we hope */
1848 struct prefix
*prefix
, /* unicast prefix */
1849 struct bgp_path_info
*info
) /* unicast info */
1851 vnc_import_bgp_exterior_add_route_it(bgp
, prefix
, info
, NULL
);
1855 * There should be only one of these per prefix at a time.
1856 * This should probably be called as a result of selection operation.
1858 * NB should be called espacially for bgp instances that are named,
1859 * because the exterior routes will always come from one of those.
1860 * We filter here on the instance name to make sure we get only the
1863 void vnc_import_bgp_exterior_del_route(
1864 struct bgp
*bgp
, struct prefix
*prefix
, /* unicast prefix */
1865 struct bgp_path_info
*info
) /* unicast info */
1868 struct rfapi_cfg
*hc
;
1869 struct rfapi_import_table
*it
;
1870 struct prefix pfx_orig_nexthop
;
1871 afi_t afi
= family2afi(prefix
->family
);
1872 struct bgp
*bgp_default
= bgp_get_default();
1877 memset(&pfx_orig_nexthop
, 0,
1878 sizeof(struct prefix
)); /* keep valgrind happy */
1880 h
= bgp_default
->rfapi
;
1881 hc
= bgp_default
->rfapi_cfg
;
1884 vnc_zlog_debug_verbose(
1885 "%s: rfapi or rfapi_cfg not instantiated, skipping",
1889 if (!hc
->redist_bgp_exterior_view
) {
1890 vnc_zlog_debug_verbose("%s: exterior view not set, skipping",
1894 if (bgp
!= hc
->redist_bgp_exterior_view
) {
1895 vnc_zlog_debug_verbose(
1896 "%s: bgp %p != hc->redist_bgp_exterior_view %p, skipping",
1897 __func__
, bgp
, hc
->redist_bgp_exterior_view
);
1900 if (!hc
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT_EXT
]) {
1901 vnc_zlog_debug_verbose(
1902 "%s: redist of exterior routes no enabled, skipping",
1908 * Extract nexthop from exterior route
1910 * Incoming prefix is unicast. If v6, it is in multiprotocol area,
1911 * but if v4 it is in attr->nexthop
1913 rfapiUnicastNexthop2Prefix(afi
, info
->attr
, &pfx_orig_nexthop
);
1915 for (it
= h
->imports
; it
; it
= it
->next
) {
1916 struct agg_table
*table
;
1917 struct agg_node
*rn
;
1918 struct agg_node
*par
;
1919 struct bgp_path_info
*bpi_interior
;
1920 int have_usable_route
;
1922 table
= it
->imported_vpn
[afi
];
1924 for (rn
= agg_node_match(table
, &pfx_orig_nexthop
),
1925 have_usable_route
= 0;
1926 (!have_usable_route
) && rn
;) {
1928 for (bpi_interior
= rn
->info
; bpi_interior
;
1929 bpi_interior
= bpi_interior
->next
) {
1930 struct prefix_rd
*prd
;
1933 if (!is_usable_interior_route(bpi_interior
))
1937 * have a legitimate route to exterior's nexthop
1940 * Import unicast route to the import table
1942 have_usable_route
= 1;
1944 if (bpi_interior
->extra
) {
1945 prd
= &bpi_interior
->extra
->vnc
.import
1947 label
= decode_label(
1948 &bpi_interior
->extra
->label
[0]);
1952 rfapiBgpInfoFilteredImportVPN(
1953 it
, FIF_ACTION_KILL
, bpi_interior
->peer
,
1955 prefix
, NULL
, afi
, prd
,
1957 ZEBRA_ROUTE_BGP_DIRECT_EXT
,
1958 BGP_ROUTE_REDISTRIBUTE
, &label
);
1963 * TBD factor this out into its own function
1966 if (RFAPI_MONITOR_EXTERIOR(rn
)
1968 if (!skiplist_delete(
1969 RFAPI_MONITOR_EXTERIOR(
1974 bgp_path_info_unlock(
1981 RFAPI_MONITOR_EXTERIOR(
1985 RFAPI_MONITOR_EXTERIOR(
1988 RFAPI_MONITOR_EXTERIOR(
1999 par
= agg_node_parent(rn
);
2002 agg_unlock_node(rn
);
2006 agg_unlock_node(rn
);
2008 if (!have_usable_route
) {
2009 if (!skiplist_delete(it
->monitor_exterior_orphans
, info
,
2012 bgp_path_info_unlock(info
);
2019 * This function should be called after a new interior VPN route
2020 * has been added to an import_table.
2022 * NB should also be called whenever an existing vpn interior route
2023 * becomes valid (e.g., valid_interior_count is inremented)
2025 void vnc_import_bgp_exterior_add_route_interior(
2026 struct bgp
*bgp
, struct rfapi_import_table
*it
,
2027 struct agg_node
*rn_interior
, /* VPN IT node */
2028 struct bgp_path_info
*bpi_interior
) /* VPN IT route */
2030 afi_t afi
= family2afi(rn_interior
->p
.family
);
2031 struct agg_node
*par
;
2032 struct bgp_path_info
*bpi_exterior
;
2033 struct prefix
*pfx_exterior
; /* exterior pfx */
2036 struct list
*list_adopted
;
2038 vnc_zlog_debug_verbose("%s: entry", __func__
);
2040 if (!is_usable_interior_route(bpi_interior
)) {
2041 vnc_zlog_debug_verbose(
2042 "%s: not usable interior route, skipping", __func__
);
2046 if (!bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT_EXT
]) {
2047 vnc_zlog_debug_verbose(
2048 "%s: redist of exterior routes no enabled, skipping",
2053 if (it
== bgp
->rfapi
->it_ce
) {
2054 vnc_zlog_debug_verbose("%s: import table is it_ce, skipping",
2061 char str_pfx
[PREFIX_STRLEN
];
2063 prefix2str(&rn_interior
->p
, str_pfx
, sizeof(str_pfx
));
2064 vnc_zlog_debug_verbose("%s: interior prefix=%s, bpi type=%d",
2065 __func__
, str_pfx
, bpi_interior
->type
);
2068 if (RFAPI_HAS_MONITOR_EXTERIOR(rn_interior
)) {
2070 int count
= 0; /* debugging */
2072 vnc_zlog_debug_verbose(
2073 "%s: has exterior monitor; ext src: %p", __func__
,
2074 RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
);
2077 * There is a monitor here already. Therefore, we do not need
2078 * to do any pulldown. Just construct exterior routes based
2079 * on the new interior route.
2082 for (rc
= skiplist_next(
2083 RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
,
2084 (void **)&bpi_exterior
, (void **)&pfx_exterior
,
2086 !rc
; rc
= skiplist_next(
2087 RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
,
2088 (void **)&bpi_exterior
,
2089 (void **)&pfx_exterior
, &cursor
)) {
2091 struct prefix_rd
*prd
;
2092 struct attr new_attr
;
2096 ++count
; /* debugging */
2098 assert(bpi_exterior
);
2099 assert(pfx_exterior
);
2101 if (bpi_interior
->extra
) {
2102 prd
= &bpi_interior
->extra
->vnc
.import
.rd
;
2103 label
= decode_label(
2104 &bpi_interior
->extra
->label
[0]);
2108 /* use local_pref from unicast route */
2109 memset(&new_attr
, 0, sizeof(struct attr
));
2110 new_attr
= *bpi_interior
->attr
;
2112 && (bpi_exterior
->attr
->flag
2113 & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
))) {
2114 new_attr
.local_pref
=
2115 bpi_exterior
->attr
->local_pref
;
2117 ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
);
2120 rfapiBgpInfoFilteredImportVPN(
2121 it
, FIF_ACTION_UPDATE
, bpi_interior
->peer
,
2123 pfx_exterior
, NULL
, afi
, prd
, &new_attr
,
2124 ZEBRA_ROUTE_BGP_DIRECT_EXT
,
2125 BGP_ROUTE_REDISTRIBUTE
, &label
);
2127 vnc_zlog_debug_verbose(
2128 "%s: finished constructing exteriors based on existing monitors",
2133 vnc_zlog_debug_verbose("%s: no exterior monitor", __func__
);
2136 * No monitor at this node. Is this the first valid interior
2137 * route at this node?
2139 if (RFAPI_MONITOR_EXTERIOR(rn_interior
)->valid_interior_count
> 1) {
2140 vnc_zlog_debug_verbose(
2141 "%s: new interior route not first valid one, skipping pulldown",
2147 * Look up the tree for possible pulldown candidates.
2148 * Find nearest parent with an exterior route monitor
2150 for (par
= agg_node_parent(rn_interior
); par
;
2151 par
= agg_node_parent(par
)) {
2152 if (RFAPI_HAS_MONITOR_EXTERIOR(par
))
2158 vnc_zlog_debug_verbose(
2159 "%s: checking parent %p for possible pulldowns",
2162 /* check monitors at par for possible pulldown */
2164 for (rc
= skiplist_next(RFAPI_MONITOR_EXTERIOR(par
)->source
,
2165 (void **)&bpi_exterior
,
2166 (void **)&pfx_exterior
, &cursor
);
2168 rc
= skiplist_next(RFAPI_MONITOR_EXTERIOR(par
)->source
,
2169 (void **)&bpi_exterior
,
2170 (void **)&pfx_exterior
, &cursor
)) {
2172 struct prefix pfx_nexthop
;
2174 memset(&pfx_nexthop
, 0,
2175 sizeof(struct prefix
)); /* keep valgrind happy */
2177 /* check original nexthop for prefix match */
2178 rfapiUnicastNexthop2Prefix(afi
, bpi_exterior
->attr
,
2181 if (prefix_match(&rn_interior
->p
, &pfx_nexthop
)) {
2183 struct bgp_path_info
*bpi
;
2184 struct prefix_rd
*prd
;
2185 struct attr new_attr
;
2191 * add monitor to longer prefix
2193 struct prefix
*pfx_mon
= prefix_new();
2194 prefix_copy(pfx_mon
, pfx_exterior
);
2195 if (!RFAPI_MONITOR_EXTERIOR(rn_interior
)
2197 RFAPI_MONITOR_EXTERIOR(rn_interior
)
2198 ->source
= skiplist_new(
2199 0, NULL
, prefix_free_lists
);
2200 agg_lock_node(rn_interior
);
2203 RFAPI_MONITOR_EXTERIOR(rn_interior
)
2205 bpi_exterior
, pfx_mon
);
2206 agg_lock_node(rn_interior
);
2209 * Delete constructed exterior routes based on
2212 for (bpi
= par
->info
; bpi
; bpi
= bpi
->next
) {
2215 prd
= &bpi
->extra
->vnc
.import
2217 label
= decode_label(
2218 &bpi
->extra
->label
[0]);
2222 rfapiBgpInfoFilteredImportVPN(
2223 it
, FIF_ACTION_KILL
, bpi
->peer
,
2225 pfx_exterior
, NULL
, afi
, prd
,
2227 ZEBRA_ROUTE_BGP_DIRECT_EXT
,
2228 BGP_ROUTE_REDISTRIBUTE
, &label
);
2233 * Add constructed exterior routes based on
2234 * the new interior route at longer prefix.
2236 if (bpi_interior
->extra
) {
2237 prd
= &bpi_interior
->extra
->vnc
.import
2239 label
= decode_label(
2240 &bpi_interior
->extra
->label
[0]);
2244 /* use local_pref from unicast route */
2245 memset(&new_attr
, 0, sizeof(struct attr
));
2246 new_attr
= *bpi_interior
->attr
;
2248 && (bpi_exterior
->attr
->flag
2249 & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
))) {
2250 new_attr
.local_pref
=
2251 bpi_exterior
->attr
->local_pref
;
2252 new_attr
.flag
|= ATTR_FLAG_BIT(
2253 BGP_ATTR_LOCAL_PREF
);
2256 rfapiBgpInfoFilteredImportVPN(
2257 it
, FIF_ACTION_UPDATE
,
2258 bpi_interior
->peer
, NULL
, /* rfd */
2259 pfx_exterior
, NULL
, afi
, prd
, &new_attr
,
2260 ZEBRA_ROUTE_BGP_DIRECT_EXT
,
2261 BGP_ROUTE_REDISTRIBUTE
, &label
);
2266 * The only monitors at rn_interior are the ones we added just
2267 * above, so we can use the rn_interior list to identify which
2268 * monitors to delete from the parent.
2271 for (rc
= skiplist_next(
2272 RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
,
2273 (void **)&bpi_exterior
, NULL
, &cursor
);
2274 !rc
; rc
= skiplist_next(
2275 RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
,
2276 (void **)&bpi_exterior
, NULL
, &cursor
)) {
2279 skiplist_delete(RFAPI_MONITOR_EXTERIOR(par
)->source
,
2280 bpi_exterior
, NULL
);
2281 agg_unlock_node(par
); /* sl entry */
2283 if (skiplist_empty(RFAPI_MONITOR_EXTERIOR(par
)->source
)) {
2284 skiplist_free(RFAPI_MONITOR_EXTERIOR(par
)->source
);
2285 RFAPI_MONITOR_EXTERIOR(par
)->source
= NULL
;
2286 agg_unlock_node(par
); /* sl itself */
2290 vnc_zlog_debug_verbose("%s: checking orphans", __func__
);
2293 * See if any orphans can be pulled down to the current node
2296 list_adopted
= NULL
;
2297 for (rc
= skiplist_next(it
->monitor_exterior_orphans
,
2298 (void **)&bpi_exterior
, (void **)&pfx_exterior
,
2300 !rc
; rc
= skiplist_next(it
->monitor_exterior_orphans
,
2301 (void **)&bpi_exterior
,
2302 (void **)&pfx_exterior
, &cursor
)) {
2304 struct prefix pfx_nexthop
;
2305 char buf
[PREFIX_STRLEN
];
2306 afi_t afi_exterior
= family2afi(pfx_exterior
->family
);
2308 prefix2str(pfx_exterior
, buf
, sizeof(buf
));
2309 vnc_zlog_debug_verbose(
2310 "%s: checking exterior orphan at prefix %s", __func__
,
2313 if (afi_exterior
!= afi
) {
2314 vnc_zlog_debug_verbose(
2315 "%s: exterior orphan afi %d != interior afi %d, skip",
2316 __func__
, afi_exterior
, afi
);
2320 /* check original nexthop for prefix match */
2321 rfapiUnicastNexthop2Prefix(afi
, bpi_exterior
->attr
,
2324 if (prefix_match(&rn_interior
->p
, &pfx_nexthop
)) {
2326 struct prefix_rd
*prd
;
2327 struct attr new_attr
;
2333 * add monitor to longer prefix
2336 struct prefix
*pfx_mon
= prefix_new();
2337 prefix_copy(pfx_mon
, pfx_exterior
);
2338 if (!RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
) {
2339 RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
=
2341 0, NULL
, prefix_free_lists
);
2342 agg_lock_node(rn_interior
); /* sl */
2345 RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
,
2346 bpi_exterior
, pfx_mon
);
2347 agg_lock_node(rn_interior
); /* sl entry */
2348 if (!list_adopted
) {
2349 list_adopted
= list_new();
2351 listnode_add(list_adopted
, bpi_exterior
);
2354 * Add constructed exterior routes based on the
2355 * new interior route at the longer prefix.
2357 if (bpi_interior
->extra
) {
2358 prd
= &bpi_interior
->extra
->vnc
.import
.rd
;
2359 label
= decode_label(
2360 &bpi_interior
->extra
->label
[0]);
2364 /* use local_pref from unicast route */
2365 memset(&new_attr
, 0, sizeof(struct attr
));
2366 new_attr
= *bpi_interior
->attr
;
2368 && (bpi_exterior
->attr
->flag
2369 & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
))) {
2370 new_attr
.local_pref
=
2371 bpi_exterior
->attr
->local_pref
;
2373 ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
);
2376 rfapiBgpInfoFilteredImportVPN(
2377 it
, FIF_ACTION_UPDATE
, bpi_interior
->peer
,
2379 pfx_exterior
, NULL
, afi
, prd
, &new_attr
,
2380 ZEBRA_ROUTE_BGP_DIRECT_EXT
,
2381 BGP_ROUTE_REDISTRIBUTE
, &label
);
2385 struct listnode
*node
;
2386 struct agg_node
*an_bpi_exterior
;
2388 for (ALL_LIST_ELEMENTS_RO(list_adopted
, node
,
2390 skiplist_delete(it
->monitor_exterior_orphans
,
2391 an_bpi_exterior
, NULL
);
2393 list_delete(&list_adopted
);
2398 * This function should be called after an interior VPN route
2399 * has been deleted from an import_table.
2400 * bpi_interior must still be valid, but it must already be detached
2401 * from its route node and the route node's valid_interior_count
2402 * must already be decremented.
2404 * NB should also be called whenever an existing vpn interior route
2405 * becomes invalid (e.g., valid_interior_count is decremented)
2407 void vnc_import_bgp_exterior_del_route_interior(
2408 struct bgp
*bgp
, struct rfapi_import_table
*it
,
2409 struct agg_node
*rn_interior
, /* VPN IT node */
2410 struct bgp_path_info
*bpi_interior
) /* VPN IT route */
2412 afi_t afi
= family2afi(rn_interior
->p
.family
);
2413 struct agg_node
*par
;
2414 struct bgp_path_info
*bpi_exterior
;
2415 struct prefix
*pfx_exterior
; /* exterior pfx */
2419 if (!VALID_INTERIOR_TYPE(bpi_interior
->type
)) {
2420 vnc_zlog_debug_verbose(
2421 "%s: type %d not valid interior type, skipping",
2422 __func__
, bpi_interior
->type
);
2426 if (!bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT_EXT
]) {
2427 vnc_zlog_debug_verbose(
2428 "%s: redist of exterior routes no enabled, skipping",
2433 if (it
== bgp
->rfapi
->it_ce
) {
2434 vnc_zlog_debug_verbose("%s: it is it_ce, skipping", __func__
);
2438 /* If no exterior routes depend on this prefix, nothing to do */
2439 if (!RFAPI_HAS_MONITOR_EXTERIOR(rn_interior
)) {
2440 vnc_zlog_debug_verbose("%s: no exterior monitor, skipping",
2447 char str_pfx
[PREFIX_STRLEN
];
2449 prefix2str(&rn_interior
->p
, str_pfx
, sizeof(str_pfx
));
2451 vnc_zlog_debug_verbose("%s: interior prefix=%s, bpi type=%d",
2452 __func__
, str_pfx
, bpi_interior
->type
);
2456 * Remove constructed routes based on the deleted interior route
2459 for (rc
= skiplist_next(RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
,
2460 (void **)&bpi_exterior
, (void **)&pfx_exterior
,
2463 rc
= skiplist_next(RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
,
2464 (void **)&bpi_exterior
, (void **)&pfx_exterior
,
2467 struct prefix_rd
*prd
;
2470 if (bpi_interior
->extra
) {
2471 prd
= &bpi_interior
->extra
->vnc
.import
.rd
;
2472 label
= decode_label(&bpi_interior
->extra
->label
[0]);
2476 rfapiBgpInfoFilteredImportVPN(
2477 it
, FIF_ACTION_KILL
, bpi_interior
->peer
, NULL
, /* rfd */
2478 pfx_exterior
, NULL
, afi
, prd
, bpi_interior
->attr
,
2479 ZEBRA_ROUTE_BGP_DIRECT_EXT
, BGP_ROUTE_REDISTRIBUTE
,
2484 * If there are no remaining valid interior routes at this prefix,
2485 * we need to look up the tree for a possible node to move monitors to
2487 if (RFAPI_MONITOR_EXTERIOR(rn_interior
)->valid_interior_count
) {
2488 vnc_zlog_debug_verbose(
2489 "%s: interior routes still present, skipping",
2495 * Find nearest parent with at least one valid interior route
2496 * If none is found, par will end up NULL, and we will move
2497 * the monitors to the orphan list for this import table
2499 for (par
= agg_node_parent(rn_interior
); par
;
2500 par
= agg_node_parent(par
)) {
2501 if (RFAPI_MONITOR_EXTERIOR(par
)->valid_interior_count
)
2505 vnc_zlog_debug_verbose("%s: par=%p, ext src: %p", __func__
, par
,
2506 RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
);
2508 /* move all monitors */
2510 * We will use and delete every element of the source skiplist
2512 while (!skiplist_first(RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
,
2513 (void **)&bpi_exterior
,
2514 (void **)&pfx_exterior
)) {
2516 struct prefix
*pfx_mon
= prefix_new();
2518 prefix_copy(pfx_mon
, pfx_exterior
);
2522 struct bgp_path_info
*bpi
;
2525 * Add monitor to parent node
2527 if (!RFAPI_MONITOR_EXTERIOR(par
)->source
) {
2528 RFAPI_MONITOR_EXTERIOR(par
)->source
=
2530 0, NULL
, prefix_free_lists
);
2531 agg_lock_node(par
); /* sl */
2533 skiplist_insert(RFAPI_MONITOR_EXTERIOR(par
)->source
,
2534 bpi_exterior
, pfx_mon
);
2535 agg_lock_node(par
); /* sl entry */
2537 /* Add constructed exterior routes based on parent */
2538 for (bpi
= par
->info
; bpi
; bpi
= bpi
->next
) {
2540 struct prefix_rd
*prd
;
2541 struct attr new_attr
;
2544 if (bpi
->type
== ZEBRA_ROUTE_BGP_DIRECT_EXT
)
2548 prd
= &bpi
->extra
->vnc
.import
.rd
;
2549 label
= decode_label(
2550 &bpi
->extra
->label
[0]);
2554 /* use local_pref from unicast route */
2555 memset(&new_attr
, 0, sizeof(struct attr
));
2556 new_attr
= *bpi
->attr
;
2558 && (bpi_exterior
->attr
->flag
2559 & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
))) {
2560 new_attr
.local_pref
=
2561 bpi_exterior
->attr
->local_pref
;
2562 new_attr
.flag
|= ATTR_FLAG_BIT(
2563 BGP_ATTR_LOCAL_PREF
);
2566 rfapiBgpInfoFilteredImportVPN(
2567 it
, FIF_ACTION_UPDATE
, bpi
->peer
,
2569 pfx_exterior
, NULL
, afi
, prd
, &new_attr
,
2570 ZEBRA_ROUTE_BGP_DIRECT_EXT
,
2571 BGP_ROUTE_REDISTRIBUTE
, &label
);
2577 * No interior route for exterior's nexthop. Save
2579 * in orphan list to await future route.
2581 skiplist_insert(it
->monitor_exterior_orphans
,
2582 bpi_exterior
, pfx_mon
);
2585 skiplist_delete_first(
2586 RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
);
2587 agg_unlock_node(rn_interior
); /* sl entry */
2589 if (skiplist_empty(RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
)) {
2590 skiplist_free(RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
);
2591 RFAPI_MONITOR_EXTERIOR(rn_interior
)->source
= NULL
;
2592 agg_unlock_node(rn_interior
); /* sl itself */
2596 /***********************************************************************
2597 * Generic add/delete unicast routes
2598 ***********************************************************************/
2600 void vnc_import_bgp_add_route(struct bgp
*bgp
, struct prefix
*prefix
,
2601 struct bgp_path_info
*info
)
2603 afi_t afi
= family2afi(prefix
->family
);
2605 if (VNC_DEBUG(VERBOSE
)) {
2606 struct prefix pfx_nexthop
;
2607 char buf
[PREFIX_STRLEN
];
2608 char buf_nh
[PREFIX_STRLEN
];
2610 prefix2str(prefix
, buf
, sizeof(buf
));
2611 rfapiUnicastNexthop2Prefix(afi
, info
->attr
, &pfx_nexthop
);
2612 prefix2str(&pfx_nexthop
, buf_nh
, sizeof(buf_nh
));
2614 vnc_zlog_debug_verbose("%s: pfx %s, nh %s", __func__
, buf
,
2618 print_rhn_list(__func__
, "ENTER ");
2623 flog_err(EC_LIB_DEVELOPMENT
, "%s: can't get afi of prefix",
2628 if (!bgp
->rfapi_cfg
) {
2629 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
2634 /* check vnc redist flag for bgp direct routes */
2635 if (!bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT
]) {
2636 vnc_zlog_debug_verbose(
2637 "%s: bgp->rfapi_cfg->redist[afi=%d][type=%d=ZEBRA_ROUTE_BGP_DIRECT] is 0, skipping",
2638 __func__
, afi
, ZEBRA_ROUTE_BGP_DIRECT
);
2642 switch (bgp
->rfapi_cfg
->redist_mode
) {
2643 case VNC_REDIST_MODE_PLAIN
:
2644 vnc_import_bgp_add_route_mode_plain(bgp
, prefix
, info
);
2647 case VNC_REDIST_MODE_RFG
:
2648 if (bgp
->rfapi_cfg
->rfg_redist
)
2649 vnc_import_bgp_add_route_mode_nvegroup(
2650 bgp
, prefix
, info
, bgp
->rfapi_cfg
->rfg_redist
);
2652 vnc_zlog_debug_verbose("%s: mode RFG but no redist RFG",
2656 case VNC_REDIST_MODE_RESOLVE_NVE
:
2657 vnc_import_bgp_add_route_mode_resolve_nve(bgp
, prefix
, info
);
2661 print_rhn_list(__func__
, "LEAVE ");
2667 * "Withdrawing a Route" import process
2669 void vnc_import_bgp_del_route(struct bgp
*bgp
, struct prefix
*prefix
,
2670 struct bgp_path_info
*info
) /* unicast info */
2672 afi_t afi
= family2afi(prefix
->family
);
2677 struct prefix pfx_nexthop
;
2678 char buf
[PREFIX_STRLEN
];
2679 char buf_nh
[PREFIX_STRLEN
];
2681 prefix2str(prefix
, buf
, sizeof(buf
));
2682 rfapiUnicastNexthop2Prefix(afi
, info
->attr
, &pfx_nexthop
);
2683 prefix2str(&pfx_nexthop
, buf_nh
, sizeof(buf_nh
));
2685 vnc_zlog_debug_verbose("%s: pfx %s, nh %s", __func__
, buf
,
2689 print_rhn_list(__func__
, "ENTER ");
2693 if (!bgp
->rfapi_cfg
) {
2694 vnc_zlog_debug_verbose("%s: bgp->rfapi_cfg is NULL, skipping",
2699 /* check bgp redist flag for vnc direct ("vpn") routes */
2700 if (!bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT
]) {
2701 vnc_zlog_debug_verbose(
2702 "%s: bgp redistribution of afi=%d VNC direct routes is off",
2707 switch (bgp
->rfapi_cfg
->redist_mode
) {
2708 case VNC_REDIST_MODE_PLAIN
:
2709 vnc_import_bgp_del_route_mode_plain(bgp
, prefix
, info
);
2712 case VNC_REDIST_MODE_RFG
:
2713 if (bgp
->rfapi_cfg
->rfg_redist
)
2714 vnc_import_bgp_del_route_mode_nvegroup(bgp
, prefix
,
2717 vnc_zlog_debug_verbose("%s: mode RFG but no redist RFG",
2721 case VNC_REDIST_MODE_RESOLVE_NVE
:
2722 vnc_import_bgp_del_route_mode_resolve_nve(bgp
, afi
, prefix
,
2727 print_rhn_list(__func__
, "LEAVE ");
2733 /***********************************************************************
2735 ***********************************************************************/
2737 void vnc_import_bgp_redist_enable(struct bgp
*bgp
, afi_t afi
)
2739 /* iterate over bgp unicast v4 and v6 routes, call
2740 * vnc_import_bgp_add_route */
2742 struct bgp_node
*rn
;
2744 vnc_zlog_debug_verbose("%s: entry, afi=%d", __func__
, afi
);
2746 if (bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT
]) {
2747 vnc_zlog_debug_verbose(
2748 "%s: already enabled for afi %d, skipping", __func__
,
2752 bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT
] = 1;
2754 for (rn
= bgp_table_top(bgp
->rib
[afi
][SAFI_UNICAST
]); rn
;
2755 rn
= bgp_route_next(rn
)) {
2757 struct bgp_path_info
*bpi
;
2759 for (bpi
= bgp_node_get_bgp_path_info(rn
); bpi
;
2762 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
))
2765 vnc_import_bgp_add_route(bgp
, &rn
->p
, bpi
);
2768 vnc_zlog_debug_verbose(
2769 "%s: set redist[afi=%d][type=%d=ZEBRA_ROUTE_BGP_DIRECT] return",
2770 __func__
, afi
, ZEBRA_ROUTE_BGP_DIRECT
);
2773 void vnc_import_bgp_exterior_redist_enable(struct bgp
*bgp
, afi_t afi
)
2775 struct bgp
*bgp_exterior
;
2776 struct bgp_node
*rn
;
2778 bgp_exterior
= bgp
->rfapi_cfg
->redist_bgp_exterior_view
;
2780 if (bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT_EXT
]) {
2781 vnc_zlog_debug_verbose(
2782 "%s: already enabled for afi %d, skipping", __func__
,
2786 bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT_EXT
] = 1;
2788 if (!bgp_exterior
) {
2789 vnc_zlog_debug_verbose(
2790 "%s: no exterior view set yet, no routes to import yet",
2795 for (rn
= bgp_table_top(bgp_exterior
->rib
[afi
][SAFI_UNICAST
]); rn
;
2796 rn
= bgp_route_next(rn
)) {
2798 struct bgp_path_info
*bpi
;
2800 for (bpi
= bgp_node_get_bgp_path_info(rn
); bpi
;
2803 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
))
2806 vnc_import_bgp_exterior_add_route(bgp_exterior
, &rn
->p
,
2810 vnc_zlog_debug_verbose(
2811 "%s: set redist[afi=%d][type=%d=ZEBRA_ROUTE_BGP_DIRECT] return",
2812 __func__
, afi
, ZEBRA_ROUTE_BGP_DIRECT
);
2816 * This function is for populating a newly-created Import Table
2818 void vnc_import_bgp_exterior_redist_enable_it(
2819 struct bgp
*bgp
, afi_t afi
, struct rfapi_import_table
*it_only
)
2821 struct bgp
*bgp_exterior
;
2822 struct bgp_node
*rn
;
2824 vnc_zlog_debug_verbose("%s: entry", __func__
);
2826 bgp_exterior
= bgp
->rfapi_cfg
->redist_bgp_exterior_view
;
2828 if (!bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT_EXT
]) {
2829 vnc_zlog_debug_verbose("%s: not enabled for afi %d, skipping",
2834 if (!bgp_exterior
) {
2835 vnc_zlog_debug_verbose(
2836 "%s: no exterior view set yet, no routes to import yet",
2841 for (rn
= bgp_table_top(bgp_exterior
->rib
[afi
][SAFI_UNICAST
]); rn
;
2842 rn
= bgp_route_next(rn
)) {
2844 struct bgp_path_info
*bpi
;
2846 for (bpi
= bgp_node_get_bgp_path_info(rn
); bpi
;
2849 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
))
2852 vnc_import_bgp_exterior_add_route_it(
2853 bgp_exterior
, &rn
->p
, bpi
, it_only
);
2859 void vnc_import_bgp_redist_disable(struct bgp
*bgp
, afi_t afi
)
2862 * iterate over vpn routes, find routes of type ZEBRA_ROUTE_BGP_DIRECT,
2863 * delete (call timer expire immediately)
2865 struct bgp_node
*rn1
;
2866 struct bgp_node
*rn2
;
2868 vnc_zlog_debug_verbose("%s: entry", __func__
);
2870 if (!bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT
]) {
2871 vnc_zlog_debug_verbose(
2872 "%s: already disabled for afi %d, skipping", __func__
,
2878 * Two-level table for SAFI_MPLS_VPN
2879 * Be careful when changing the things we iterate over
2881 for (rn1
= bgp_table_top(bgp
->rib
[afi
][SAFI_MPLS_VPN
]); rn1
;
2882 rn1
= bgp_route_next(rn1
)) {
2884 if (bgp_node_has_bgp_path_info_data(rn1
)) {
2886 for (rn2
= bgp_table_top(
2887 bgp_node_get_bgp_table_info(rn1
));
2888 rn2
; rn2
= bgp_route_next(rn2
)) {
2890 struct bgp_path_info
*bpi
;
2891 struct bgp_path_info
*nextbpi
;
2893 for (bpi
= bgp_node_get_bgp_path_info(rn2
); bpi
;
2896 nextbpi
= bpi
->next
;
2899 == ZEBRA_ROUTE_BGP_DIRECT
) {
2901 struct rfapi_descriptor
*rfd
;
2902 vncHDBgpDirect
.peer
= bpi
->peer
;
2906 rfd
= bpi
->extra
->vnc
.export
2909 vnc_zlog_debug_verbose(
2910 "%s: deleting bpi=%p, bpi->peer=%p, bpi->type=%d, bpi->sub_type=%d, bpi->extra->vnc.export.rfapi_handle=%p [passing rfd=%p]",
2912 bpi
->peer
, bpi
->type
,
2924 rfd
, bpi
->peer
, bgp
,
2925 SAFI_MPLS_VPN
, &rn2
->p
,
2926 (struct prefix_rd
*)&rn1
2929 bpi
->sub_type
, NULL
,
2932 vncHDBgpDirect
.peer
= NULL
;
2938 /* Clear RHN list */
2939 if (bgp
->rfapi
->resolve_nve_nexthop
) {
2940 struct prefix_bag
*pb
;
2941 struct bgp_path_info
*info
;
2942 while (!skiplist_first(bgp
->rfapi
->resolve_nve_nexthop
, NULL
,
2945 skiplist_delete_first(bgp
->rfapi
->resolve_nve_nexthop
);
2946 bgp_path_info_unlock(info
);
2950 bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT
] = 0;
2951 vnc_zlog_debug_verbose("%s: return", __func__
);
2955 void vnc_import_bgp_exterior_redist_disable(struct bgp
*bgp
, afi_t afi
)
2957 struct rfapi_cfg
*hc
= bgp
->rfapi_cfg
;
2958 struct bgp
*bgp_exterior
= hc
->redist_bgp_exterior_view
;
2960 vnc_zlog_debug_verbose("%s: entry", __func__
);
2962 if (!hc
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT_EXT
]) {
2963 vnc_zlog_debug_verbose(
2964 "%s: already disabled for afi %d, skipping", __func__
,
2969 if (!bgp_exterior
) {
2970 vnc_zlog_debug_verbose(
2971 "%s: bgp exterior view not defined, skipping",
2978 struct bgp_node
*rn
;
2979 for (rn
= bgp_table_top(bgp_exterior
->rib
[afi
][SAFI_UNICAST
]);
2980 rn
; rn
= bgp_route_next(rn
)) {
2982 struct bgp_path_info
*bpi
;
2984 for (bpi
= bgp_node_get_bgp_path_info(rn
); bpi
;
2987 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
))
2990 vnc_import_bgp_exterior_del_route(bgp_exterior
,
2995 print_rhn_list(__func__
, NULL
);
2999 bgp
->rfapi_cfg
->redist
[afi
][ZEBRA_ROUTE_BGP_DIRECT_EXT
] = 0;
3000 vnc_zlog_debug_verbose("%s: return", __func__
);