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: rfapi_import.c
23 * Purpose: Handle import of routes from BGP to RFAPI
28 #include "lib/zebra.h"
29 #include "lib/prefix.h"
30 #include "lib/table.h"
32 #include "lib/memory.h"
34 #include "lib/skiplist.h"
35 #include "lib/thread.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" /* prefix_rd2str() */
42 #include "bgpd/bgp_vnc_types.h"
44 #include "bgpd/rfapi/rfapi.h"
45 #include "bgpd/rfapi/bgp_rfapi_cfg.h"
46 #include "bgpd/rfapi/rfapi_backend.h"
47 #include "bgpd/rfapi/rfapi_import.h"
48 #include "bgpd/rfapi/rfapi_private.h"
49 #include "bgpd/rfapi/rfapi_monitor.h"
50 #include "bgpd/rfapi/rfapi_nve_addr.h"
51 #include "bgpd/rfapi/rfapi_vty.h"
52 #include "bgpd/rfapi/vnc_export_bgp.h"
53 #include "bgpd/rfapi/vnc_export_bgp_p.h"
54 #include "bgpd/rfapi/vnc_zebra.h"
55 #include "bgpd/rfapi/vnc_import_bgp.h"
56 #include "bgpd/rfapi/vnc_import_bgp_p.h"
57 #include "bgpd/rfapi/rfapi_rib.h"
58 #include "bgpd/rfapi/rfapi_encap_tlv.h"
59 #include "bgpd/rfapi/vnc_debug.h"
61 #ifdef HAVE_GLIBC_BACKTRACE
62 /* for backtrace and friends */
64 #endif /* HAVE_GLIBC_BACKTRACE */
66 #undef DEBUG_MONITOR_MOVE_SHORTER
67 #undef DEBUG_RETURNED_NHL
68 #undef DEBUG_ROUTE_COUNTERS
69 #undef DEBUG_ENCAP_MONITOR
72 #undef DEBUG_BI_SEARCH
75 * Allocated for each withdraw timer instance; freed when the timer
76 * expires or is canceled
78 struct rfapi_withdraw
{
79 struct rfapi_import_table
*import_table
;
80 struct route_node
*node
;
81 struct bgp_info
*info
;
82 safi_t safi
; /* used only for bulk operations */
84 * For import table node reference count checking (i.e., debugging).
85 * Normally when a timer expires, lockoffset should be 0. However, if
86 * the timer expiration function is called directly (e.g.,
87 * rfapiExpireVpnNow), the node could be locked by a preceding
88 * route_top() or route_next() in a loop, so we need to pass this
96 * It's evil and fiendish. It's compiler-dependent.
97 * ? Might need LDFLAGS -rdynamic to produce all function names
99 void rfapiDebugBacktrace(void)
101 #ifdef HAVE_GLIBC_BACKTRACE
102 #define RFAPI_DEBUG_BACKTRACE_NENTRIES 200
103 void *buf
[RFAPI_DEBUG_BACKTRACE_NENTRIES
];
108 size
= backtrace(buf
, RFAPI_DEBUG_BACKTRACE_NENTRIES
);
109 syms
= backtrace_symbols(buf
, size
);
111 for (i
= 0; i
< size
&& i
< RFAPI_DEBUG_BACKTRACE_NENTRIES
; ++i
) {
112 vnc_zlog_debug_verbose("backtrace[%2zu]: %s", i
, syms
[i
]);
122 * Count remote routes and compare with actively-maintained values.
123 * Abort if they disagree.
125 void rfapiCheckRouteCount()
127 struct bgp
*bgp
= bgp_get_default();
129 struct rfapi_import_table
*it
;
137 for (it
= h
->imports
; it
; it
= it
->next
) {
138 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
140 struct route_table
*rt
;
141 struct route_node
*rn
;
143 int holddown_count
= 0;
145 int imported_count
= 0;
146 int remote_count
= 0;
148 rt
= it
->imported_vpn
[afi
];
150 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
)) {
152 struct bgp_info
*next
;
154 for (bi
= rn
->info
; bi
; bi
= next
) {
157 if (CHECK_FLAG(bi
->flags
,
162 if (RFAPI_LOCAL_BI(bi
)) {
165 if (RFAPI_DIRECT_IMPORT_BI(
176 if (it
->holddown_count
[afi
] != holddown_count
) {
177 vnc_zlog_debug_verbose(
178 "%s: it->holddown_count %d != holddown_count %d",
179 __func__
, it
->holddown_count
[afi
],
183 if (it
->remote_count
[afi
] != remote_count
) {
184 vnc_zlog_debug_verbose(
185 "%s: it->remote_count %d != remote_count %d",
186 __func__
, it
->remote_count
[afi
],
190 if (it
->imported_count
[afi
] != imported_count
) {
191 vnc_zlog_debug_verbose(
192 "%s: it->imported_count %d != imported_count %d",
193 __func__
, it
->imported_count
[afi
],
201 #if DEBUG_ROUTE_COUNTERS
202 #define VNC_ITRCCK do {rfapiCheckRouteCount();} while (0)
208 * Validate reference count for a node in an import table
210 * Normally lockoffset is 0 for nodes in quiescent state. However,
211 * route_unlock_node will delete the node if it is called when
212 * node->lock == 1, and we have to validate the refcount before
213 * the node is deleted. In this case, we specify lockoffset 1.
215 void rfapiCheckRefcount(struct route_node
*rn
, safi_t safi
, int lockoffset
)
217 unsigned int count_bi
= 0;
218 unsigned int count_monitor
= 0;
220 struct rfapi_monitor_encap
*hme
;
221 struct rfapi_monitor_vpn
*hmv
;
223 for (bi
= rn
->info
; bi
; bi
= bi
->next
)
228 ++count_monitor
; /* rfapi_it_extra */
235 for (hme
= RFAPI_MONITOR_ENCAP(rn
); hme
;
242 for (hmv
= RFAPI_MONITOR_VPN(rn
); hmv
; hmv
= hmv
->next
)
245 if (RFAPI_MONITOR_EXTERIOR(rn
)->source
) {
246 ++count_monitor
; /* sl */
248 for (rc
= skiplist_next(
249 RFAPI_MONITOR_EXTERIOR(rn
)->source
,
250 NULL
, NULL
, &cursor
);
253 RFAPI_MONITOR_EXTERIOR(rn
)->source
,
254 NULL
, NULL
, &cursor
)) {
256 ++count_monitor
; /* sl entry */
266 if (count_bi
+ count_monitor
+ lockoffset
!= rn
->lock
) {
267 vnc_zlog_debug_verbose(
268 "%s: count_bi=%d, count_monitor=%d, lockoffset=%d, rn->lock=%d",
269 __func__
, count_bi
, count_monitor
, lockoffset
,
276 * Perform deferred rfapi_close operations that were queued
279 static wq_item_status
rfapi_deferred_close_workfunc(struct work_queue
*q
,
282 struct rfapi_descriptor
*rfd
= data
;
283 struct rfapi
*h
= q
->spec
.data
;
285 assert(!(h
->flags
& RFAPI_INCALLBACK
));
287 vnc_zlog_debug_verbose("%s: completed deferred close on handle %p",
293 * Extract layer 2 option from Encap TLVS in BGP attrs
295 int rfapiGetL2o(struct attr
*attr
, struct rfapi_l2address_option
*l2o
)
299 struct bgp_attr_encap_subtlv
*pEncap
;
301 for (pEncap
= attr
->vnc_subtlvs
; pEncap
;
302 pEncap
= pEncap
->next
) {
304 if (pEncap
->type
== BGP_VNC_SUBTLV_TYPE_RFPOPTION
) {
306 == RFAPI_VN_OPTION_TYPE_L2ADDR
) {
308 if (pEncap
->value
[1] == 14) {
309 memcpy(l2o
->macaddr
.octet
,
326 l2o
->logical_net_id
=
329 + ((pEncap
->value
[14]
332 + ((pEncap
->value
[13]
347 * Extract the lifetime from the Tunnel Encap attribute of a route in
350 int rfapiGetVncLifetime(struct attr
*attr
, uint32_t *lifetime
)
352 struct bgp_attr_encap_subtlv
*pEncap
;
354 *lifetime
= RFAPI_INFINITE_LIFETIME
; /* default to infinite */
358 for (pEncap
= attr
->vnc_subtlvs
; pEncap
;
359 pEncap
= pEncap
->next
) {
362 == BGP_VNC_SUBTLV_TYPE_LIFETIME
) { /* lifetime */
363 if (pEncap
->length
== 4) {
364 memcpy(lifetime
, pEncap
->value
, 4);
365 *lifetime
= ntohl(*lifetime
);
376 * Extract the tunnel type from the extended community
378 int rfapiGetTunnelType(struct attr
*attr
, bgp_encap_types
*type
)
380 *type
= BGP_ENCAP_TYPE_MPLS
; /* default to MPLS */
381 if (attr
&& attr
->ecommunity
) {
382 struct ecommunity
*ecom
= attr
->ecommunity
;
385 for (i
= 0; i
< (ecom
->size
* ECOMMUNITY_SIZE
);
386 i
+= ECOMMUNITY_SIZE
) {
390 if (ep
[0] == ECOMMUNITY_ENCODE_OPAQUE
391 && ep
[1] == ECOMMUNITY_OPAQUE_SUBTYPE_ENCAP
) {
392 *type
= (ep
[6] << 8) + ep
[7];
403 * Look for UN address in Encap attribute
405 int rfapiGetVncTunnelUnAddr(struct attr
*attr
, struct prefix
*p
)
407 struct bgp_attr_encap_subtlv
*pEncap
;
408 bgp_encap_types tun_type
;
410 rfapiGetTunnelType(attr
, &tun_type
);
411 if (tun_type
== BGP_ENCAP_TYPE_MPLS
) {
414 /* MPLS carries UN address in next hop */
415 rfapiNexthop2Prefix(attr
, p
);
422 for (pEncap
= attr
->encap_subtlvs
; pEncap
;
423 pEncap
= pEncap
->next
) {
426 == BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT
) { /* un
429 switch (pEncap
->length
) {
434 memcpy(p
->u
.val
, pEncap
->value
,
441 p
->family
= AF_INET6
;
443 memcpy(p
->u
.val
, pEncap
->value
,
456 * Get UN address wherever it might be
458 int rfapiGetUnAddrOfVpnBi(struct bgp_info
*bi
, struct prefix
*p
)
460 /* If it's in this route's VNC attribute, we're done */
461 if (!rfapiGetVncTunnelUnAddr(bi
->attr
, p
))
464 * Otherwise, see if it's cached from a corresponding ENCAP SAFI
468 switch (bi
->extra
->vnc
.import
.un_family
) {
471 p
->family
= bi
->extra
->vnc
.import
.un_family
;
472 p
->u
.prefix4
= bi
->extra
->vnc
.import
.un
.addr4
;
478 p
->family
= bi
->extra
->vnc
.import
.un_family
;
479 p
->u
.prefix6
= bi
->extra
->vnc
.import
.un
.addr6
;
486 #if DEBUG_ENCAP_MONITOR
487 vnc_zlog_debug_verbose(
488 "%s: bi->extra->vnc.import.un_family is 0, no UN addr",
500 * Make a new bgp_info from gathered parameters
502 static struct bgp_info
*rfapiBgpInfoCreate(struct attr
*attr
, struct peer
*peer
,
503 void *rfd
, struct prefix_rd
*prd
,
504 u_char type
, u_char sub_type
,
507 struct bgp_info
*new;
509 new = bgp_info_new();
514 new->attr
= bgp_attr_intern(attr
);
516 bgp_info_extra_get(new);
518 new->extra
->vnc
.import
.rd
= *prd
;
519 rfapi_time(&new->extra
->vnc
.import
.create_time
);
522 encode_label(*label
, &new->extra
->label
);
524 new->sub_type
= sub_type
;
532 * Frees bgp_info as used in import tables (parts are not
533 * allocated exactly the way they are in the main RIBs)
535 static void rfapiBgpInfoFree(struct bgp_info
*goner
)
541 vnc_zlog_debug_verbose("%s: calling peer_unlock(%p), #%d",
542 __func__
, goner
->peer
,
544 peer_unlock(goner
->peer
);
548 bgp_attr_unintern(&goner
->attr
);
551 assert(!goner
->extra
->damp_info
); /* Not used in import tbls */
552 XFREE(MTYPE_BGP_ROUTE_EXTRA
, goner
->extra
);
555 XFREE(MTYPE_BGP_ROUTE
, goner
);
558 struct rfapi_import_table
*rfapiMacImportTableGetNoAlloc(struct bgp
*bgp
,
562 struct rfapi_import_table
*it
= NULL
;
563 uintptr_t lni_as_ptr
= lni
;
572 if (skiplist_search(h
->import_mac
, (void *)lni_as_ptr
, (void **)&it
))
578 struct rfapi_import_table
*rfapiMacImportTableGet(struct bgp
*bgp
, uint32_t lni
)
581 struct rfapi_import_table
*it
= NULL
;
582 uintptr_t lni_as_ptr
= lni
;
587 if (!h
->import_mac
) {
588 /* default cmp is good enough for LNI */
589 h
->import_mac
= skiplist_new(0, NULL
, NULL
);
592 if (skiplist_search(h
->import_mac
, (void *)lni_as_ptr
, (void **)&it
)) {
594 struct ecommunity
*enew
;
595 struct ecommunity_val eval
;
598 it
= XCALLOC(MTYPE_RFAPI_IMPORTTABLE
,
599 sizeof(struct rfapi_import_table
));
600 /* set RT list of new import table based on LNI */
601 memset((char *)&eval
, 0, sizeof(eval
));
602 eval
.val
[0] = 0; /* VNC L2VPN */
603 eval
.val
[1] = 2; /* VNC L2VPN */
604 eval
.val
[5] = (lni
>> 16) & 0xff;
605 eval
.val
[6] = (lni
>> 8) & 0xff;
606 eval
.val
[7] = (lni
>> 0) & 0xff;
608 enew
= ecommunity_new();
609 ecommunity_add_val(enew
, &eval
);
610 it
->rt_import_list
= enew
;
612 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
613 it
->imported_vpn
[afi
] = route_table_init();
614 it
->imported_encap
[afi
] = route_table_init();
617 it
->l2_logical_net_id
= lni
;
619 skiplist_insert(h
->import_mac
, (void *)lni_as_ptr
, it
);
627 * Implement MONITOR_MOVE_SHORTER(original_node) from
628 * RFAPI-Import-Event-Handling.txt
630 * Returns pointer to the list of moved monitors
632 static struct rfapi_monitor_vpn
*
633 rfapiMonitorMoveShorter(struct route_node
*original_vpn_node
, int lockoffset
)
636 struct route_node
*par
;
637 struct rfapi_monitor_vpn
*m
;
638 struct rfapi_monitor_vpn
*mlast
;
639 struct rfapi_monitor_vpn
*moved
;
641 int parent_already_refcounted
= 0;
643 RFAPI_CHECK_REFCOUNT(original_vpn_node
, SAFI_MPLS_VPN
, lockoffset
);
645 #if DEBUG_MONITOR_MOVE_SHORTER
649 prefix2str(&original_vpn_node
->p
, buf
, BUFSIZ
);
651 vnc_zlog_debug_verbose("%s: called with node pfx=%s", __func__
,
657 * 1. If there is at least one bi (either regular route or
658 * route marked as withdrawn, with a pending timer) at
659 * original_node with a valid UN address, we're done. Return.
661 for (bi
= original_vpn_node
->info
; bi
; bi
= bi
->next
) {
664 if (!rfapiGetUnAddrOfVpnBi(bi
, &pfx
)) {
665 #if DEBUG_MONITOR_MOVE_SHORTER
666 vnc_zlog_debug_verbose(
667 "%s: have valid UN at original node, no change",
675 * 2. Travel up the tree (toward less-specific prefixes) from
676 * original_node to find the first node that has at least
677 * one route (even if it is only a withdrawn route) with a
678 * valid UN address. Call this node "Node P."
680 for (par
= original_vpn_node
->parent
; par
; par
= par
->parent
) {
681 for (bi
= par
->info
; bi
; bi
= bi
->next
) {
683 if (!rfapiGetUnAddrOfVpnBi(bi
, &pfx
)) {
692 RFAPI_CHECK_REFCOUNT(par
, SAFI_MPLS_VPN
, 0);
696 * If no less-specific routes, try to use the 0/0 node
699 /* this isn't necessarily 0/0 */
700 par
= route_top(original_vpn_node
->table
);
703 * If we got the top node but it wasn't 0/0,
706 if (par
&& par
->p
.prefixlen
) {
707 route_unlock_node(par
); /* maybe free */
712 ++parent_already_refcounted
;
717 * Create 0/0 node if it isn't there
720 struct prefix pfx_default
;
722 memset(&pfx_default
, 0, sizeof(pfx_default
));
723 pfx_default
.family
= original_vpn_node
->p
.family
;
725 /* creates default node if none exists */
726 par
= route_node_get(original_vpn_node
->table
, &pfx_default
);
727 ++parent_already_refcounted
;
731 * 3. Move each of the monitors found at original_node to Node P.
732 * These are "Moved Monitors."
737 * Attach at end so that the list pointer we return points
738 * only to the moved routes
740 for (m
= RFAPI_MONITOR_VPN(par
), mlast
= NULL
; m
;
741 mlast
= m
, m
= m
->next
)
745 moved
= mlast
->next
= RFAPI_MONITOR_VPN(original_vpn_node
);
747 moved
= RFAPI_MONITOR_VPN_W_ALLOC(par
) =
748 RFAPI_MONITOR_VPN(original_vpn_node
);
750 if (RFAPI_MONITOR_VPN(
751 original_vpn_node
)) /* check agg, so not allocated */
752 RFAPI_MONITOR_VPN_W_ALLOC(original_vpn_node
) = NULL
;
755 * update the node pointers on the monitors
757 for (m
= moved
; m
; m
= m
->next
) {
762 RFAPI_CHECK_REFCOUNT(par
, SAFI_MPLS_VPN
,
763 parent_already_refcounted
- movecount
);
764 while (movecount
> parent_already_refcounted
) {
765 route_lock_node(par
);
766 ++parent_already_refcounted
;
768 while (movecount
< parent_already_refcounted
) {
769 /* unlikely, but code defensively */
770 route_unlock_node(par
);
771 --parent_already_refcounted
;
773 RFAPI_CHECK_REFCOUNT(original_vpn_node
, SAFI_MPLS_VPN
,
774 movecount
+ lockoffset
);
775 while (movecount
--) {
776 route_unlock_node(original_vpn_node
);
779 #if DEBUG_MONITOR_MOVE_SHORTER
783 prefix2str(&par
->p
, buf
, BUFSIZ
);
785 vnc_zlog_debug_verbose("%s: moved to node pfx=%s", __func__
,
795 * Implement MONITOR_MOVE_LONGER(new_node) from
796 * RFAPI-Import-Event-Handling.txt
798 static void rfapiMonitorMoveLonger(struct route_node
*new_vpn_node
)
800 struct rfapi_monitor_vpn
*monitor
;
801 struct rfapi_monitor_vpn
*mlast
;
803 struct route_node
*par
;
805 RFAPI_CHECK_REFCOUNT(new_vpn_node
, SAFI_MPLS_VPN
, 0);
808 * Make sure we have at least one valid route at the new node
810 for (bi
= new_vpn_node
->info
; bi
; bi
= bi
->next
) {
812 if (!rfapiGetUnAddrOfVpnBi(bi
, &pfx
))
817 vnc_zlog_debug_verbose(
818 "%s: no valid routes at node %p, so not attempting moves",
819 __func__
, new_vpn_node
);
824 * Find first parent node that has monitors
826 for (par
= new_vpn_node
->parent
; par
; par
= par
->parent
) {
827 if (RFAPI_MONITOR_VPN(par
))
832 vnc_zlog_debug_verbose(
833 "%s: no parent nodes with monitors, done", __func__
);
838 * Check each of these monitors to see of their longest-match
839 * is now the updated node. Move any such monitors to the more-
840 * specific updated node
842 for (mlast
= NULL
, monitor
= RFAPI_MONITOR_VPN(par
); monitor
;) {
845 * If new longest match for monitor prefix is the new
846 * route's prefix, move monitor to new route's prefix
848 if (prefix_match(&new_vpn_node
->p
, &monitor
->p
)) {
851 mlast
->next
= monitor
->next
;
853 RFAPI_MONITOR_VPN_W_ALLOC(par
) = monitor
->next
;
858 monitor
->next
= RFAPI_MONITOR_VPN(new_vpn_node
);
859 RFAPI_MONITOR_VPN_W_ALLOC(new_vpn_node
) = monitor
;
860 monitor
->node
= new_vpn_node
;
862 route_lock_node(new_vpn_node
); /* incr refcount */
864 monitor
= mlast
? mlast
->next
: RFAPI_MONITOR_VPN(par
);
866 RFAPI_CHECK_REFCOUNT(par
, SAFI_MPLS_VPN
, 1);
867 /* decr refcount after we're done with par as this might
869 route_unlock_node(par
);
874 monitor
= monitor
->next
;
877 RFAPI_CHECK_REFCOUNT(new_vpn_node
, SAFI_MPLS_VPN
, 0);
881 static void rfapiBgpInfoChainFree(struct bgp_info
*bi
)
883 struct bgp_info
*next
;
888 * If there is a timer waiting to delete this bi, cancel
889 * the timer and delete immediately
891 if (CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
)
892 && bi
->extra
->vnc
.import
.timer
) {
895 (struct thread
*)bi
->extra
->vnc
.import
.timer
;
896 struct rfapi_withdraw
*wcb
= t
->arg
;
898 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
904 rfapiBgpInfoFree(bi
);
909 static void rfapiImportTableFlush(struct rfapi_import_table
*it
)
916 ecommunity_free(&it
->rt_import_list
);
917 it
->rt_import_list
= NULL
;
919 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
921 struct route_node
*rn
;
923 for (rn
= route_top(it
->imported_vpn
[afi
]); rn
;
924 rn
= route_next(rn
)) {
926 * Each route_node has:
927 * aggregate: points to rfapi_it_extra with monitor
929 * info: points to chain of bgp_info
931 /* free bgp_info and its children */
932 rfapiBgpInfoChainFree(rn
->info
);
935 rfapiMonitorExtraFlush(SAFI_MPLS_VPN
, rn
);
938 for (rn
= route_top(it
->imported_encap
[afi
]); rn
;
939 rn
= route_next(rn
)) {
940 /* free bgp_info and its children */
941 rfapiBgpInfoChainFree(rn
->info
);
944 rfapiMonitorExtraFlush(SAFI_ENCAP
, rn
);
947 route_table_finish(it
->imported_vpn
[afi
]);
948 route_table_finish(it
->imported_encap
[afi
]);
950 if (it
->monitor_exterior_orphans
) {
951 skiplist_free(it
->monitor_exterior_orphans
);
955 void rfapiImportTableRefDelByIt(struct bgp
*bgp
,
956 struct rfapi_import_table
*it_target
)
959 struct rfapi_import_table
*it
;
960 struct rfapi_import_table
*prev
= NULL
;
967 for (it
= h
->imports
; it
; prev
= it
, it
= it
->next
) {
973 assert(it
->refcount
);
979 prev
->next
= it
->next
;
981 h
->imports
= it
->next
;
983 rfapiImportTableFlush(it
);
984 XFREE(MTYPE_RFAPI_IMPORTTABLE
, it
);
988 #if RFAPI_REQUIRE_ENCAP_BEEC
990 * Look for magic BGP Encapsulation Extended Community value
991 * Format in RFC 5512 Sect. 4.5
993 static int rfapiEcommunitiesMatchBeec(struct ecommunity
*ecom
,
994 bgp_encap_types type
)
1001 for (i
= 0; i
< (ecom
->size
* ECOMMUNITY_SIZE
); i
+= ECOMMUNITY_SIZE
) {
1007 if (ep
[0] == ECOMMUNITY_ENCODE_OPAQUE
1008 && ep
[1] == ECOMMUNITY_OPAQUE_SUBTYPE_ENCAP
1009 && ep
[6] == ((type
&& 0xff00) >> 8)
1010 && ep
[7] == (type
& 0xff)) {
1019 int rfapiEcommunitiesIntersect(struct ecommunity
*e1
, struct ecommunity
*e2
)
1028 s1
= ecommunity_ecom2str(e1
, ECOMMUNITY_FORMAT_DISPLAY
, 0);
1029 s2
= ecommunity_ecom2str(e2
, ECOMMUNITY_FORMAT_DISPLAY
, 0);
1030 vnc_zlog_debug_verbose("%s: e1[%s], e2[%s]", __func__
, s1
, s2
);
1031 XFREE(MTYPE_ECOMMUNITY_STR
, s1
);
1032 XFREE(MTYPE_ECOMMUNITY_STR
, s2
);
1035 for (i
= 0; i
< e1
->size
; ++i
) {
1036 for (j
= 0; j
< e2
->size
; ++j
) {
1037 if (!memcmp(e1
->val
+ (i
* ECOMMUNITY_SIZE
),
1038 e2
->val
+ (j
* ECOMMUNITY_SIZE
),
1048 int rfapiEcommunityGetLNI(struct ecommunity
*ecom
, uint32_t *lni
)
1052 for (i
= 0; i
< ecom
->size
; ++i
) {
1053 uint8_t *p
= ecom
->val
+ (i
* ECOMMUNITY_SIZE
);
1055 if ((*(p
+ 0) == 0x00) && (*(p
+ 1) == 0x02)) {
1057 *lni
= (*(p
+ 5) << 16) | (*(p
+ 6) << 8)
1066 int rfapiEcommunityGetEthernetTag(struct ecommunity
*ecom
, uint16_t *tag_id
)
1068 struct bgp
*bgp
= bgp_get_default();
1069 *tag_id
= 0; /* default to untagged */
1072 for (i
= 0; i
< ecom
->size
; ++i
) {
1075 uint8_t *p
= ecom
->val
+ (i
* ECOMMUNITY_SIZE
);
1077 /* High-order octet of type. */
1080 if (*p
++ == ECOMMUNITY_ROUTE_TARGET
) {
1081 if (encode
== ECOMMUNITY_ENCODE_AS4
) {
1086 } else if (encode
== ECOMMUNITY_ENCODE_AS
) {
1090 2; /* skip next two, tag/vid
1091 always in lowest bytes */
1093 if (as
== bgp
->as
) {
1094 *tag_id
= *p
++ << 8;
1104 static int rfapiVpnBiNhEqualsPt(struct bgp_info
*bi
, struct rfapi_ip_addr
*hpt
)
1111 family
= BGP_MP_NEXTHOP_FAMILY(bi
->attr
->mp_nexthop_len
);
1113 if (hpt
->addr_family
!= family
)
1118 if (bi
->attr
->mp_nexthop_global_in
.s_addr
1119 != hpt
->addr
.v4
.s_addr
)
1124 if (IPV6_ADDR_CMP(&bi
->attr
->mp_nexthop_global
, &hpt
->addr
.v6
))
1138 * Compare 2 VPN BIs. Return true if they have the same VN and UN addresses
1140 static int rfapiVpnBiSamePtUn(struct bgp_info
*bi1
, struct bgp_info
*bi2
)
1142 struct prefix pfx_un1
;
1143 struct prefix pfx_un2
;
1148 if (!bi1
->attr
|| !bi2
->attr
)
1152 * VN address comparisons
1155 if (BGP_MP_NEXTHOP_FAMILY(bi1
->attr
->mp_nexthop_len
)
1156 != BGP_MP_NEXTHOP_FAMILY(bi2
->attr
->mp_nexthop_len
)) {
1160 switch (BGP_MP_NEXTHOP_FAMILY(bi1
->attr
->mp_nexthop_len
)) {
1162 if (bi1
->attr
->mp_nexthop_global_in
.s_addr
1163 != bi2
->attr
->mp_nexthop_global_in
.s_addr
)
1168 if (IPV6_ADDR_CMP(&bi1
->attr
->mp_nexthop_global
,
1169 &bi2
->attr
->mp_nexthop_global
))
1179 * UN address comparisons
1181 if (rfapiGetVncTunnelUnAddr(bi1
->attr
, &pfx_un1
)) {
1183 pfx_un1
.family
= bi1
->extra
->vnc
.import
.un_family
;
1184 switch (bi1
->extra
->vnc
.import
.un_family
) {
1187 bi1
->extra
->vnc
.import
.un
.addr4
;
1191 bi1
->extra
->vnc
.import
.un
.addr6
;
1200 if (rfapiGetVncTunnelUnAddr(bi2
->attr
, &pfx_un2
)) {
1202 pfx_un2
.family
= bi2
->extra
->vnc
.import
.un_family
;
1203 switch (bi2
->extra
->vnc
.import
.un_family
) {
1206 bi2
->extra
->vnc
.import
.un
.addr4
;
1210 bi2
->extra
->vnc
.import
.un
.addr6
;
1219 if (!pfx_un1
.family
|| !pfx_un2
.family
)
1222 if (pfx_un1
.family
!= pfx_un2
.family
)
1225 switch (pfx_un1
.family
) {
1227 if (!IPV4_ADDR_SAME(&pfx_un1
.u
.prefix4
.s_addr
,
1228 &pfx_un2
.u
.prefix4
.s_addr
))
1232 if (!IPV6_ADDR_SAME(&pfx_un1
.u
.prefix6
, &pfx_un2
.u
.prefix6
))
1241 uint8_t rfapiRfpCost(struct attr
*attr
)
1243 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)) {
1244 if (attr
->local_pref
> 255) {
1247 return 255 - attr
->local_pref
;
1253 /*------------------------------------------
1256 * Find Layer 2 options in an option chain
1262 * l2o layer 2 options extracted
1266 * 1 no options found
1268 --------------------------------------------*/
1269 int rfapi_extract_l2o(
1270 struct bgp_tea_options
*pHop
, /* chain of options */
1271 struct rfapi_l2address_option
*l2o
) /* return extracted value */
1273 struct bgp_tea_options
*p
;
1275 for (p
= pHop
; p
; p
= p
->next
) {
1276 if ((p
->type
== RFAPI_VN_OPTION_TYPE_L2ADDR
)
1277 && (p
->length
>= 8)) {
1281 memcpy(&l2o
->macaddr
, v
, 6);
1283 l2o
->label
= ((v
[6] << 12) & 0xff000)
1284 + ((v
[7] << 4) & 0xff0)
1285 + ((v
[8] >> 4) & 0xf);
1287 l2o
->local_nve_id
= (uint8_t)v
[10];
1289 l2o
->logical_net_id
=
1290 (v
[11] << 16) + (v
[12] << 8) + (v
[13] << 0);
1298 static struct rfapi_next_hop_entry
*
1299 rfapiRouteInfo2NextHopEntry(struct rfapi_ip_prefix
*rprefix
,
1300 struct bgp_info
*bi
, /* route to encode */
1301 uint32_t lifetime
, /* use this in nhe */
1302 struct route_node
*rn
) /* req for L2 eth addr */
1304 struct rfapi_next_hop_entry
*new;
1305 int have_vnc_tunnel_un
= 0;
1307 #if DEBUG_ENCAP_MONITOR
1308 vnc_zlog_debug_verbose("%s: entry, bi %p, rn %p", __func__
, bi
, rn
);
1311 new = XCALLOC(MTYPE_RFAPI_NEXTHOP
, sizeof(struct rfapi_next_hop_entry
));
1314 new->prefix
= *rprefix
;
1317 && decode_rd_type(bi
->extra
->vnc
.import
.rd
.val
)
1318 == RD_TYPE_VNC_ETH
) {
1321 struct rfapi_vn_option
*vo
;
1323 vo
= XCALLOC(MTYPE_RFAPI_VN_OPTION
,
1324 sizeof(struct rfapi_vn_option
));
1327 vo
->type
= RFAPI_VN_OPTION_TYPE_L2ADDR
;
1329 memcpy(&vo
->v
.l2addr
.macaddr
, &rn
->p
.u
.prefix_eth
.octet
,
1331 /* only low 3 bytes of this are significant */
1333 (void)rfapiEcommunityGetLNI(
1334 bi
->attr
->ecommunity
,
1335 &vo
->v
.l2addr
.logical_net_id
);
1336 (void)rfapiEcommunityGetEthernetTag(
1337 bi
->attr
->ecommunity
, &vo
->v
.l2addr
.tag_id
);
1340 /* local_nve_id comes from lower byte of RD type */
1341 vo
->v
.l2addr
.local_nve_id
= bi
->extra
->vnc
.import
.rd
.val
[1];
1343 /* label comes from MP_REACH_NLRI label */
1344 vo
->v
.l2addr
.label
= decode_label(&bi
->extra
->label
);
1346 new->vn_options
= vo
;
1349 * If there is an auxiliary prefix (i.e., host IP address),
1350 * use it as the nexthop prefix instead of the query prefix
1352 if (bi
->extra
->vnc
.import
.aux_prefix
.family
) {
1353 rfapiQprefix2Rprefix(&bi
->extra
->vnc
.import
.aux_prefix
,
1359 bgp_encap_types tun_type
;
1360 new->prefix
.cost
= rfapiRfpCost(bi
->attr
);
1362 struct bgp_attr_encap_subtlv
*pEncap
;
1364 switch (BGP_MP_NEXTHOP_FAMILY(bi
->attr
->mp_nexthop_len
)) {
1366 new->vn_address
.addr_family
= AF_INET
;
1367 new->vn_address
.addr
.v4
=
1368 bi
->attr
->mp_nexthop_global_in
;
1372 new->vn_address
.addr_family
= AF_INET6
;
1373 new->vn_address
.addr
.v6
= bi
->attr
->mp_nexthop_global
;
1377 zlog_warn("%s: invalid vpn nexthop length: %d",
1378 __func__
, bi
->attr
->mp_nexthop_len
);
1379 rfapi_free_next_hop_list(new);
1383 for (pEncap
= bi
->attr
->vnc_subtlvs
; pEncap
;
1384 pEncap
= pEncap
->next
) {
1385 switch (pEncap
->type
) {
1386 case BGP_VNC_SUBTLV_TYPE_LIFETIME
:
1387 /* use configured lifetime, not attr lifetime */
1391 zlog_warn("%s: unknown VNC option type %d",
1392 __func__
, pEncap
->type
);
1399 rfapiGetTunnelType(bi
->attr
, &tun_type
);
1400 if (tun_type
== BGP_ENCAP_TYPE_MPLS
) {
1402 /* MPLS carries UN address in next hop */
1403 rfapiNexthop2Prefix(bi
->attr
, &p
);
1404 if (p
.family
!= 0) {
1405 rfapiQprefix2Raddr(&p
, &new->un_address
);
1406 have_vnc_tunnel_un
= 1;
1410 for (pEncap
= bi
->attr
->encap_subtlvs
; pEncap
;
1411 pEncap
= pEncap
->next
) {
1412 switch (pEncap
->type
) {
1413 case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT
:
1415 * Overrides ENCAP UN address, if any
1417 switch (pEncap
->length
) {
1420 new->un_address
.addr_family
= AF_INET
;
1421 memcpy(&new->un_address
.addr
.v4
,
1423 have_vnc_tunnel_un
= 1;
1427 new->un_address
.addr_family
= AF_INET6
;
1428 memcpy(&new->un_address
.addr
.v6
,
1430 have_vnc_tunnel_un
= 1;
1435 "%s: invalid tunnel subtlv UN addr length (%d) for bi %p",
1436 __func__
, pEncap
->length
, bi
);
1442 "%s: unknown Encap Attribute option type %d",
1443 __func__
, pEncap
->type
);
1450 new->un_options
= rfapi_encap_tlv_to_un_option(bi
->attr
);
1452 #if DEBUG_ENCAP_MONITOR
1453 vnc_zlog_debug_verbose("%s: line %d: have_vnc_tunnel_un=%d",
1454 __func__
, __LINE__
, have_vnc_tunnel_un
);
1457 if (!have_vnc_tunnel_un
&& bi
&& bi
->extra
) {
1459 * use cached UN address from ENCAP route
1461 new->un_address
.addr_family
=
1462 bi
->extra
->vnc
.import
.un_family
;
1463 switch (new->un_address
.addr_family
) {
1465 new->un_address
.addr
.v4
=
1466 bi
->extra
->vnc
.import
.un
.addr4
;
1469 new->un_address
.addr
.v6
=
1470 bi
->extra
->vnc
.import
.un
.addr6
;
1474 "%s: invalid UN addr family (%d) for bi %p",
1475 __func__
, new->un_address
.addr_family
,
1477 rfapi_free_next_hop_list(new);
1484 new->lifetime
= lifetime
;
1488 int rfapiHasNonRemovedRoutes(struct route_node
*rn
)
1490 struct bgp_info
*bi
;
1492 for (bi
= rn
->info
; bi
; bi
= bi
->next
) {
1495 if (!CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
)
1496 && (bi
->extra
&& !rfapiGetUnAddrOfVpnBi(bi
, &pfx
))) {
1508 void rfapiDumpNode(struct route_node
*rn
)
1510 struct bgp_info
*bi
;
1512 vnc_zlog_debug_verbose("%s: rn=%p", __func__
, rn
);
1513 for (bi
= rn
->info
; bi
; bi
= bi
->next
) {
1515 int ctrc
= rfapiGetUnAddrOfVpnBi(bi
, &pfx
);
1518 if (!CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
)
1519 && (bi
->extra
&& !ctrc
)) {
1526 vnc_zlog_debug_verbose(
1527 " bi=%p, nr=%d, flags=0x%x, extra=%p, ctrc=%d", bi
, nr
,
1528 bi
->flags
, bi
->extra
, ctrc
);
1533 static int rfapiNhlAddNodeRoutes(
1534 struct route_node
*rn
, /* in */
1535 struct rfapi_ip_prefix
*rprefix
, /* in */
1536 uint32_t lifetime
, /* in */
1537 int removed
, /* in */
1538 struct rfapi_next_hop_entry
**head
, /* in/out */
1539 struct rfapi_next_hop_entry
**tail
, /* in/out */
1540 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1541 struct route_node
*rfd_rib_node
, /* preload this NVE rib node */
1542 struct prefix
*pfx_target_original
) /* query target */
1544 struct bgp_info
*bi
;
1545 struct rfapi_next_hop_entry
*new;
1546 struct prefix pfx_un
;
1547 struct skiplist
*seen_nexthops
;
1549 int is_l2
= (rn
->p
.family
== AF_ETHERNET
);
1551 if (rfapiRibFTDFilterRecentPrefix(
1552 (struct rfapi_descriptor
*)(rfd_rib_node
->table
->info
), rn
,
1553 pfx_target_original
)) {
1558 skiplist_new(0, vnc_prefix_cmp
, (void (*)(void *))prefix_free
);
1560 for (bi
= rn
->info
; bi
; bi
= bi
->next
) {
1562 struct prefix pfx_vn
;
1563 struct prefix
*newpfx
;
1565 if (removed
&& !CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
)) {
1566 #if DEBUG_RETURNED_NHL
1567 vnc_zlog_debug_verbose(
1568 "%s: want holddown, this route not holddown, skip",
1573 if (!removed
&& CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
)) {
1582 * Check for excluded VN address
1584 if (rfapiVpnBiNhEqualsPt(bi
, exclude_vnaddr
))
1588 * Check for VN address (nexthop) copied already
1591 /* L2 routes: semantic nexthop in aux_prefix; VN addr
1593 pfx_vn
= bi
->extra
->vnc
.import
.aux_prefix
;
1595 rfapiNexthop2Prefix(bi
->attr
, &pfx_vn
);
1597 if (!skiplist_search(seen_nexthops
, &pfx_vn
, NULL
)) {
1598 #if DEBUG_RETURNED_NHL
1601 prefix2str(&pfx_vn
, buf
, BUFSIZ
);
1602 buf
[BUFSIZ
- 1] = 0; /* guarantee NUL-terminated */
1603 vnc_zlog_debug_verbose(
1604 "%s: already put VN/nexthop %s, skip", __func__
,
1610 if (rfapiGetUnAddrOfVpnBi(bi
, &pfx_un
)) {
1611 #if DEBUG_ENCAP_MONITOR
1612 vnc_zlog_debug_verbose(
1613 "%s: failed to get UN address of this VPN bi",
1619 newpfx
= prefix_new();
1621 skiplist_insert(seen_nexthops
, newpfx
, newpfx
);
1623 new = rfapiRouteInfo2NextHopEntry(rprefix
, bi
, lifetime
, rn
);
1625 if (rfapiRibPreloadBi(rfd_rib_node
, &pfx_vn
, &pfx_un
,
1627 /* duplicate filtered by RIB */
1628 rfapi_free_next_hop_list(new);
1635 (*tail
)->next
= new;
1644 skiplist_free(seen_nexthops
);
1653 * omit_node is meant for the situation where we are adding a subtree
1654 * of a parent of some original requested node. The response already
1655 * contains the original requested node, and we don't want to duplicate
1656 * its routes in the list, so we skip it if the right or left node
1657 * matches (of course, we still travel down its child subtrees).
1659 static int rfapiNhlAddSubtree(
1660 struct route_node
*rn
, /* in */
1661 uint32_t lifetime
, /* in */
1662 struct rfapi_next_hop_entry
**head
, /* in/out */
1663 struct rfapi_next_hop_entry
**tail
, /* in/out */
1664 struct route_node
*omit_node
, /* in */
1665 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1666 struct route_table
*rfd_rib_table
, /* preload here */
1667 struct prefix
*pfx_target_original
) /* query target */
1669 struct rfapi_ip_prefix rprefix
;
1672 /* FIXME: need to find a better way here to work without sticking our
1673 * hands in node->link */
1674 if (rn
->l_left
&& rn
->l_left
!= omit_node
) {
1675 if (rn
->l_left
->info
) {
1677 struct route_node
*rib_rn
= NULL
;
1679 rfapiQprefix2Rprefix(&rn
->l_left
->p
, &rprefix
);
1680 if (rfd_rib_table
) {
1681 rib_rn
= route_node_get(rfd_rib_table
,
1685 count
= rfapiNhlAddNodeRoutes(
1686 rn
->l_left
, &rprefix
, lifetime
, 0, head
, tail
,
1687 exclude_vnaddr
, rib_rn
, pfx_target_original
);
1689 count
= rfapiNhlAddNodeRoutes(
1690 rn
->l_left
, &rprefix
, lifetime
, 1, head
,
1691 tail
, exclude_vnaddr
, rib_rn
,
1692 pfx_target_original
);
1696 route_unlock_node(rib_rn
);
1700 if (rn
->l_right
&& rn
->l_right
!= omit_node
) {
1701 if (rn
->l_right
->info
) {
1703 struct route_node
*rib_rn
= NULL
;
1705 rfapiQprefix2Rprefix(&rn
->l_right
->p
, &rprefix
);
1706 if (rfd_rib_table
) {
1707 rib_rn
= route_node_get(rfd_rib_table
,
1710 count
= rfapiNhlAddNodeRoutes(
1711 rn
->l_right
, &rprefix
, lifetime
, 0, head
, tail
,
1712 exclude_vnaddr
, rib_rn
, pfx_target_original
);
1714 count
= rfapiNhlAddNodeRoutes(
1715 rn
->l_right
, &rprefix
, lifetime
, 1,
1716 head
, tail
, exclude_vnaddr
, rib_rn
,
1717 pfx_target_original
);
1721 route_unlock_node(rib_rn
);
1726 rcount
+= rfapiNhlAddSubtree(
1727 rn
->l_left
, lifetime
, head
, tail
, omit_node
,
1728 exclude_vnaddr
, rfd_rib_table
, pfx_target_original
);
1731 rcount
+= rfapiNhlAddSubtree(
1732 rn
->l_right
, lifetime
, head
, tail
, omit_node
,
1733 exclude_vnaddr
, rfd_rib_table
, pfx_target_original
);
1740 * Implementation of ROUTE_LIST(node) from RFAPI-Import-Event-Handling.txt
1742 * Construct an rfapi nexthop list based on the routes attached to
1743 * the specified node.
1745 * If there are any routes that do NOT have BGP_INFO_REMOVED set,
1746 * return those only. If there are ONLY routes with BGP_INFO_REMOVED,
1747 * then return those, and also include all the non-removed routes from the
1748 * next less-specific node (i.e., this node's parent) at the end.
1750 struct rfapi_next_hop_entry
*rfapiRouteNode2NextHopList(
1751 struct route_node
*rn
, uint32_t lifetime
, /* put into nexthop entries */
1752 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1753 struct route_table
*rfd_rib_table
, /* preload here */
1754 struct prefix
*pfx_target_original
) /* query target */
1756 struct rfapi_ip_prefix rprefix
;
1757 struct rfapi_next_hop_entry
*answer
= NULL
;
1758 struct rfapi_next_hop_entry
*last
= NULL
;
1759 struct route_node
*parent
;
1761 struct route_node
*rib_rn
;
1763 #if DEBUG_RETURNED_NHL
1767 prefix2str(&rn
->p
, buf
, BUFSIZ
);
1768 buf
[BUFSIZ
- 1] = 0;
1769 vnc_zlog_debug_verbose("%s: called with node pfx=%s", __func__
,
1772 rfapiDebugBacktrace();
1775 rfapiQprefix2Rprefix(&rn
->p
, &rprefix
);
1777 rib_rn
= rfd_rib_table
? route_node_get(rfd_rib_table
, &rn
->p
) : NULL
;
1780 * Add non-withdrawn routes at this node
1782 count
= rfapiNhlAddNodeRoutes(rn
, &rprefix
, lifetime
, 0, &answer
, &last
,
1783 exclude_vnaddr
, rib_rn
,
1784 pfx_target_original
);
1787 * If the list has at least one entry, it's finished
1790 count
+= rfapiNhlAddSubtree(rn
, lifetime
, &answer
, &last
, NULL
,
1791 exclude_vnaddr
, rfd_rib_table
,
1792 pfx_target_original
);
1793 vnc_zlog_debug_verbose("%s: %d nexthops, answer=%p", __func__
,
1795 #if DEBUG_RETURNED_NHL
1796 rfapiPrintNhl(NULL
, answer
);
1799 route_unlock_node(rib_rn
);
1804 * Add withdrawn routes at this node
1806 count
= rfapiNhlAddNodeRoutes(rn
, &rprefix
, lifetime
, 1, &answer
, &last
,
1807 exclude_vnaddr
, rib_rn
,
1808 pfx_target_original
);
1810 route_unlock_node(rib_rn
);
1812 // rfapiPrintNhl(NULL, answer);
1815 * walk up the tree until we find a node with non-deleted
1816 * routes, then add them
1818 for (parent
= rn
->parent
; parent
; parent
= parent
->parent
) {
1819 if (rfapiHasNonRemovedRoutes(parent
)) {
1825 * Add non-withdrawn routes from less-specific prefix
1828 rib_rn
= rfd_rib_table
1829 ? route_node_get(rfd_rib_table
, &parent
->p
)
1831 rfapiQprefix2Rprefix(&parent
->p
, &rprefix
);
1832 count
+= rfapiNhlAddNodeRoutes(parent
, &rprefix
, lifetime
, 0,
1833 &answer
, &last
, exclude_vnaddr
,
1834 rib_rn
, pfx_target_original
);
1835 count
+= rfapiNhlAddSubtree(parent
, lifetime
, &answer
, &last
,
1836 rn
, exclude_vnaddr
, rfd_rib_table
,
1837 pfx_target_original
);
1839 route_unlock_node(rib_rn
);
1842 * There is no parent with non-removed routes. Still need to
1843 * add subtree of original node if it contributed routes to the
1847 count
+= rfapiNhlAddSubtree(rn
, lifetime
, &answer
,
1848 &last
, rn
, exclude_vnaddr
,
1850 pfx_target_original
);
1853 vnc_zlog_debug_verbose("%s: %d nexthops, answer=%p", __func__
, count
,
1855 #if DEBUG_RETURNED_NHL
1856 rfapiPrintNhl(NULL
, answer
);
1862 * Construct nexthop list of all routes in table
1864 struct rfapi_next_hop_entry
*rfapiRouteTable2NextHopList(
1865 struct route_table
*rt
,
1866 uint32_t lifetime
, /* put into nexthop entries */
1867 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1868 struct route_table
*rfd_rib_table
, /* preload this NVE rib table */
1869 struct prefix
*pfx_target_original
) /* query target */
1871 struct route_node
*rn
;
1872 struct rfapi_next_hop_entry
*biglist
= NULL
;
1873 struct rfapi_next_hop_entry
*nhl
;
1874 struct rfapi_next_hop_entry
*tail
= NULL
;
1877 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
)) {
1879 nhl
= rfapiRouteNode2NextHopList(rn
, lifetime
, exclude_vnaddr
,
1881 pfx_target_original
);
1883 tail
= biglist
= nhl
;
1890 while (tail
->next
) {
1897 vnc_zlog_debug_verbose("%s: returning %d routes", __func__
, count
);
1901 struct rfapi_next_hop_entry
*rfapiEthRouteNode2NextHopList(
1902 struct route_node
*rn
, struct rfapi_ip_prefix
*rprefix
,
1903 uint32_t lifetime
, /* put into nexthop entries */
1904 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1905 struct route_table
*rfd_rib_table
, /* preload NVE rib table */
1906 struct prefix
*pfx_target_original
) /* query target */
1909 struct rfapi_next_hop_entry
*answer
= NULL
;
1910 struct rfapi_next_hop_entry
*last
= NULL
;
1911 struct route_node
*rib_rn
;
1913 rib_rn
= rfd_rib_table
? route_node_get(rfd_rib_table
, &rn
->p
) : NULL
;
1915 count
= rfapiNhlAddNodeRoutes(rn
, rprefix
, lifetime
, 0, &answer
, &last
,
1916 NULL
, rib_rn
, pfx_target_original
);
1918 #if DEBUG_ENCAP_MONITOR
1919 vnc_zlog_debug_verbose("%s: node %p: %d non-holddown routes", __func__
,
1924 count
= rfapiNhlAddNodeRoutes(rn
, rprefix
, lifetime
, 1, &answer
,
1925 &last
, exclude_vnaddr
, rib_rn
,
1926 pfx_target_original
);
1927 vnc_zlog_debug_verbose("%s: node %p: %d holddown routes",
1928 __func__
, rn
, count
);
1932 route_unlock_node(rib_rn
);
1934 #if DEBUG_RETURNED_NHL
1935 rfapiPrintNhl(NULL
, answer
);
1943 * Construct nexthop list of all routes in table
1945 struct rfapi_next_hop_entry
*rfapiEthRouteTable2NextHopList(
1946 uint32_t logical_net_id
, struct rfapi_ip_prefix
*rprefix
,
1947 uint32_t lifetime
, /* put into nexthop entries */
1948 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1949 struct route_table
*rfd_rib_table
, /* preload NVE rib node */
1950 struct prefix
*pfx_target_original
) /* query target */
1952 struct rfapi_import_table
*it
;
1953 struct bgp
*bgp
= bgp_get_default();
1954 struct route_table
*rt
;
1955 struct route_node
*rn
;
1956 struct rfapi_next_hop_entry
*biglist
= NULL
;
1957 struct rfapi_next_hop_entry
*nhl
;
1958 struct rfapi_next_hop_entry
*tail
= NULL
;
1962 it
= rfapiMacImportTableGet(bgp
, logical_net_id
);
1963 rt
= it
->imported_vpn
[AFI_L2VPN
];
1965 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
)) {
1967 nhl
= rfapiEthRouteNode2NextHopList(
1968 rn
, rprefix
, lifetime
, exclude_vnaddr
, rfd_rib_table
,
1969 pfx_target_original
);
1971 tail
= biglist
= nhl
;
1978 while (tail
->next
) {
1985 vnc_zlog_debug_verbose("%s: returning %d routes", __func__
, count
);
1990 * Insert a new bi to the imported route table node,
1991 * keeping the list of BIs sorted best route first
1993 static void rfapiBgpInfoAttachSorted(struct route_node
*rn
,
1994 struct bgp_info
*info_new
, afi_t afi
,
1998 struct bgp_info
*prev
;
1999 struct bgp_info
*next
;
2000 char pfx_buf
[PREFIX2STR_BUFFER
];
2003 bgp
= bgp_get_default(); /* assume 1 instance for now */
2005 if (VNC_DEBUG(IMPORT_BI_ATTACH
)) {
2006 vnc_zlog_debug_verbose("%s: info_new->peer=%p", __func__
,
2008 vnc_zlog_debug_verbose("%s: info_new->peer->su_remote=%p",
2009 __func__
, info_new
->peer
->su_remote
);
2012 for (prev
= NULL
, next
= rn
->info
; next
;
2013 prev
= next
, next
= next
->next
) {
2014 if (!bgp
|| (!CHECK_FLAG(info_new
->flags
, BGP_INFO_REMOVED
)
2015 && CHECK_FLAG(next
->flags
, BGP_INFO_REMOVED
))
2016 || bgp_info_cmp_compatible(bgp
, info_new
, next
, pfx_buf
,
2018 == -1) { /* -1 if 1st is better */
2022 vnc_zlog_debug_verbose("%s: prev=%p, next=%p", __func__
, prev
, next
);
2024 prev
->next
= info_new
;
2026 rn
->info
= info_new
;
2028 info_new
->prev
= prev
;
2029 info_new
->next
= next
;
2031 next
->prev
= info_new
;
2032 bgp_attr_intern(info_new
->attr
);
2035 static void rfapiBgpInfoDetach(struct route_node
*rn
, struct bgp_info
*bi
)
2038 * Remove the route (doubly-linked)
2040 // bgp_attr_unintern (&bi->attr);
2042 bi
->next
->prev
= bi
->prev
;
2044 bi
->prev
->next
= bi
->next
;
2046 rn
->info
= bi
->next
;
2050 * For L3-indexed import tables
2052 static int rfapi_bi_peer_rd_cmp(void *b1
, void *b2
)
2054 struct bgp_info
*bi1
= b1
;
2055 struct bgp_info
*bi2
= b2
;
2060 if (bi1
->peer
< bi2
->peer
)
2062 if (bi1
->peer
> bi2
->peer
)
2068 return vnc_prefix_cmp((struct prefix
*)&bi1
->extra
->vnc
.import
.rd
,
2069 (struct prefix
*)&bi2
->extra
->vnc
.import
.rd
);
2073 * For L2-indexed import tables
2074 * The BIs in these tables should ALWAYS have an aux_prefix set because
2075 * they arrive via IPv4 or IPv6 advertisements.
2077 static int rfapi_bi_peer_rd_aux_cmp(void *b1
, void *b2
)
2079 struct bgp_info
*bi1
= b1
;
2080 struct bgp_info
*bi2
= b2
;
2086 if (bi1
->peer
< bi2
->peer
)
2088 if (bi1
->peer
> bi2
->peer
)
2094 rc
= vnc_prefix_cmp((struct prefix
*)&bi1
->extra
->vnc
.import
.rd
,
2095 (struct prefix
*)&bi2
->extra
->vnc
.import
.rd
);
2101 * L2 import tables can have multiple entries with the
2102 * same MAC address, same RD, but different L3 addresses.
2104 * Use presence of aux_prefix with AF=ethernet and prefixlen=1
2105 * as magic value to signify explicit wildcarding of the aux_prefix.
2106 * This magic value will not appear in bona fide bi entries in
2107 * the import table, but is allowed in the "fake" bi used to
2108 * probe the table when searching. (We have to test both b1 and b2
2109 * because there is no guarantee of the order the test key and
2110 * the real key will be passed)
2112 if ((bi1
->extra
->vnc
.import
.aux_prefix
.family
== AF_ETHERNET
2113 && (bi1
->extra
->vnc
.import
.aux_prefix
.prefixlen
== 1))
2114 || (bi2
->extra
->vnc
.import
.aux_prefix
.family
== AF_ETHERNET
2115 && (bi2
->extra
->vnc
.import
.aux_prefix
.prefixlen
== 1))) {
2118 * wildcard aux address specified
2123 return vnc_prefix_cmp(&bi1
->extra
->vnc
.import
.aux_prefix
,
2124 &bi2
->extra
->vnc
.import
.aux_prefix
);
2129 * Index on RD and Peer
2131 static void rfapiItBiIndexAdd(struct route_node
*rn
, /* Import table VPN node */
2132 struct bgp_info
*bi
) /* new BI */
2134 struct skiplist
*sl
;
2142 prefix_rd2str(&bi
->extra
->vnc
.import
.rd
, buf
, BUFSIZ
);
2143 vnc_zlog_debug_verbose("%s: bi %p, peer %p, rd %s", __func__
,
2147 sl
= RFAPI_RDINDEX_W_ALLOC(rn
);
2149 if (AF_ETHERNET
== rn
->p
.family
) {
2150 sl
= skiplist_new(0, rfapi_bi_peer_rd_aux_cmp
, NULL
);
2152 sl
= skiplist_new(0, rfapi_bi_peer_rd_cmp
, NULL
);
2154 RFAPI_IT_EXTRA_GET(rn
)->u
.vpn
.idx_rd
= sl
;
2155 route_lock_node(rn
); /* for skiplist */
2157 assert(!skiplist_insert(sl
, (void *)bi
, (void *)bi
));
2158 route_lock_node(rn
); /* for skiplist entry */
2160 /* NB: BIs in import tables are not refcounted */
2163 static void rfapiItBiIndexDump(struct route_node
*rn
)
2165 struct skiplist
*sl
;
2166 void *cursor
= NULL
;
2171 sl
= RFAPI_RDINDEX(rn
);
2175 for (rc
= skiplist_next(sl
, (void **)&k
, (void **)&v
, &cursor
); !rc
;
2176 rc
= skiplist_next(sl
, (void **)&k
, (void **)&v
, &cursor
)) {
2179 char buf_aux_pfx
[BUFSIZ
];
2181 prefix_rd2str(&k
->extra
->vnc
.import
.rd
, buf
, BUFSIZ
);
2183 if (k
->extra
->vnc
.import
.aux_prefix
.family
) {
2184 prefix2str(&k
->extra
->vnc
.import
.aux_prefix
,
2185 buf_aux_pfx
, BUFSIZ
);
2187 strncpy(buf_aux_pfx
, "(none)", BUFSIZ
);
2188 buf_aux_pfx
[BUFSIZ
- 1] = 0;
2191 vnc_zlog_debug_verbose("bi %p, peer %p, rd %s, aux_prefix %s",
2192 k
, k
->peer
, buf
, buf_aux_pfx
);
2196 static struct bgp_info
*rfapiItBiIndexSearch(
2197 struct route_node
*rn
, /* Import table VPN node */
2198 struct prefix_rd
*prd
, struct peer
*peer
,
2199 struct prefix
*aux_prefix
) /* optional L3 addr for L2 ITs */
2201 struct skiplist
*sl
;
2203 struct bgp_info bi_fake
;
2204 struct bgp_info_extra bi_extra
;
2205 struct bgp_info
*bi_result
;
2207 sl
= RFAPI_RDINDEX(rn
);
2214 char buf_aux_pfx
[BUFSIZ
];
2216 prefix_rd2str(prd
, buf
, BUFSIZ
);
2218 prefix2str(aux_prefix
, buf_aux_pfx
, BUFSIZ
);
2220 strncpy(buf_aux_pfx
, "(nil)", BUFSIZ
- 1);
2221 buf_aux_pfx
[BUFSIZ
- 1] = 0;
2224 vnc_zlog_debug_verbose("%s want prd=%s, peer=%p, aux_prefix=%s",
2225 __func__
, buf
, peer
, buf_aux_pfx
);
2226 rfapiItBiIndexDump(rn
);
2230 /* threshold is a WAG */
2231 if (sl
->count
< 3) {
2233 vnc_zlog_debug_verbose("%s: short list algorithm", __func__
);
2235 /* if short list, linear search might be faster */
2236 for (bi_result
= rn
->info
; bi_result
;
2237 bi_result
= bi_result
->next
) {
2241 prefix_rd2str(&bi_result
->extra
->vnc
.import
.rd
,
2243 vnc_zlog_debug_verbose(
2244 "%s: bi has prd=%s, peer=%p", __func__
,
2245 buf
, bi_result
->peer
);
2248 if (peer
== bi_result
->peer
2249 && !prefix_cmp((struct prefix
*)&bi_result
->extra
2251 (struct prefix
*)prd
)) {
2254 vnc_zlog_debug_verbose(
2255 "%s: peer and RD same, doing aux_prefix check",
2259 || !prefix_cmp(aux_prefix
,
2260 &bi_result
->extra
->vnc
.import
2264 vnc_zlog_debug_verbose("%s: match",
2274 bi_fake
.peer
= peer
;
2275 bi_fake
.extra
= &bi_extra
;
2276 bi_fake
.extra
->vnc
.import
.rd
= *(struct prefix_rd
*)prd
;
2278 bi_fake
.extra
->vnc
.import
.aux_prefix
= *aux_prefix
;
2281 bi_fake
.extra
->vnc
.import
.aux_prefix
.family
= AF_ETHERNET
;
2282 bi_fake
.extra
->vnc
.import
.aux_prefix
.prefixlen
= 1;
2285 rc
= skiplist_search(sl
, (void *)&bi_fake
, (void *)&bi_result
);
2289 vnc_zlog_debug_verbose("%s: no match", __func__
);
2295 vnc_zlog_debug_verbose("%s: matched bi=%p", __func__
, bi_result
);
2301 static void rfapiItBiIndexDel(struct route_node
*rn
, /* Import table VPN node */
2302 struct bgp_info
*bi
) /* old BI */
2304 struct skiplist
*sl
;
2309 prefix_rd2str(&bi
->extra
->vnc
.import
.rd
, buf
, BUFSIZ
);
2310 vnc_zlog_debug_verbose("%s: bi %p, peer %p, rd %s", __func__
,
2314 sl
= RFAPI_RDINDEX(rn
);
2317 rc
= skiplist_delete(sl
, (void *)(bi
), (void *)bi
);
2319 rfapiItBiIndexDump(rn
);
2323 route_unlock_node(rn
); /* for skiplist entry */
2325 /* NB: BIs in import tables are not refcounted */
2329 * Add a backreference at the ENCAP node to the VPN route that
2332 static void rfapiMonitorEncapAdd(struct rfapi_import_table
*import_table
,
2333 struct prefix
*p
, /* VN address */
2334 struct route_node
*vpn_rn
, /* VPN node */
2335 struct bgp_info
*vpn_bi
) /* VPN bi/route */
2337 afi_t afi
= family2afi(p
->family
);
2338 struct route_node
*rn
;
2339 struct rfapi_monitor_encap
*m
;
2342 rn
= route_node_get(import_table
->imported_encap
[afi
],
2346 m
= XCALLOC(MTYPE_RFAPI_MONITOR_ENCAP
,
2347 sizeof(struct rfapi_monitor_encap
));
2354 /* insert to encap node's list */
2355 m
->next
= RFAPI_MONITOR_ENCAP(rn
);
2358 RFAPI_MONITOR_ENCAP_W_ALLOC(rn
) = m
;
2360 /* for easy lookup when deleting vpn route */
2361 vpn_bi
->extra
->vnc
.import
.hme
= m
;
2363 vnc_zlog_debug_verbose(
2364 "%s: it=%p, vpn_bi=%p, afi=%d, encap rn=%p, setting vpn_bi->extra->vnc.import.hme=%p",
2365 __func__
, import_table
, vpn_bi
, afi
, rn
, m
);
2367 RFAPI_CHECK_REFCOUNT(rn
, SAFI_ENCAP
, 0);
2368 bgp_attr_intern(vpn_bi
->attr
);
2371 static void rfapiMonitorEncapDelete(struct bgp_info
*vpn_bi
)
2374 * Remove encap monitor
2376 vnc_zlog_debug_verbose("%s: vpn_bi=%p", __func__
, vpn_bi
);
2377 if (vpn_bi
->extra
) {
2378 struct rfapi_monitor_encap
*hme
= vpn_bi
->extra
->vnc
.import
.hme
;
2382 vnc_zlog_debug_verbose("%s: hme=%p", __func__
, hme
);
2384 /* Refcount checking takes too long here */
2385 // RFAPI_CHECK_REFCOUNT(hme->rn, SAFI_ENCAP, 0);
2387 hme
->next
->prev
= hme
->prev
;
2389 hme
->prev
->next
= hme
->next
;
2391 RFAPI_MONITOR_ENCAP_W_ALLOC(hme
->rn
) =
2393 /* Refcount checking takes too long here */
2394 // RFAPI_CHECK_REFCOUNT(hme->rn, SAFI_ENCAP, 1);
2396 /* see if the struct rfapi_it_extra is empty and can be
2398 rfapiMonitorExtraPrune(SAFI_ENCAP
, hme
->rn
);
2400 route_unlock_node(hme
->rn
); /* decr ref count */
2401 XFREE(MTYPE_RFAPI_MONITOR_ENCAP
, hme
);
2402 vpn_bi
->extra
->vnc
.import
.hme
= NULL
;
2408 * quagga lib/thread.h says this must return int even though
2409 * it doesn't do anything with the return value
2411 static int rfapiWithdrawTimerVPN(struct thread
*t
)
2413 struct rfapi_withdraw
*wcb
= t
->arg
;
2414 struct bgp_info
*bi
= wcb
->info
;
2415 struct bgp
*bgp
= bgp_get_default();
2417 struct rfapi_monitor_vpn
*moved
;
2422 assert(wcb
->import_table
);
2425 RFAPI_CHECK_REFCOUNT(wcb
->node
, SAFI_MPLS_VPN
, wcb
->lockoffset
);
2430 vnc_zlog_debug_verbose(
2431 "%s: removing bi %p at prefix %s/%d", __func__
, bi
,
2432 rfapi_ntop(wcb
->node
->p
.family
, &wcb
->node
->p
.u
.prefix
,
2434 wcb
->node
->p
.prefixlen
);
2438 * Remove the route (doubly-linked)
2440 if (CHECK_FLAG(bi
->flags
, BGP_INFO_VALID
)
2441 && VALID_INTERIOR_TYPE(bi
->type
))
2442 RFAPI_MONITOR_EXTERIOR(wcb
->node
)->valid_interior_count
--;
2444 afi
= family2afi(wcb
->node
->p
.family
);
2445 wcb
->import_table
->holddown_count
[afi
] -= 1; /* keep count consistent */
2446 rfapiItBiIndexDel(wcb
->node
, bi
);
2447 rfapiBgpInfoDetach(wcb
->node
, bi
); /* with removed bi */
2449 vnc_import_bgp_exterior_del_route_interior(bgp
, wcb
->import_table
,
2454 * If VNC is configured to send response remove messages, AND
2455 * if the removed route had a UN address, do response removal
2458 if (!(bgp
->rfapi_cfg
->flags
2459 & BGP_VNC_CONFIG_RESPONSE_REMOVAL_DISABLE
)) {
2461 int has_valid_duplicate
= 0;
2462 struct bgp_info
*bii
;
2465 * First check if there are any OTHER routes at this node
2466 * that have the same nexthop and a valid UN address. If
2467 * there are (e.g., from other peers), then the route isn't
2468 * really gone, so skip sending a response removal message.
2470 for (bii
= wcb
->node
->info
; bii
; bii
= bii
->next
) {
2471 if (rfapiVpnBiSamePtUn(bi
, bii
)) {
2472 has_valid_duplicate
= 1;
2477 vnc_zlog_debug_verbose("%s: has_valid_duplicate=%d", __func__
,
2478 has_valid_duplicate
);
2480 if (!has_valid_duplicate
) {
2481 rfapiRibPendingDeleteRoute(bgp
, wcb
->import_table
, afi
,
2486 rfapiMonitorEncapDelete(bi
);
2489 * If there are no VPN monitors at this VPN Node A,
2492 if (!RFAPI_MONITOR_VPN(wcb
->node
)) {
2493 vnc_zlog_debug_verbose("%s: no VPN monitors at this node",
2499 * rfapiMonitorMoveShorter only moves monitors if there are
2500 * no remaining valid routes at the current node
2502 moved
= rfapiMonitorMoveShorter(wcb
->node
, 1);
2505 rfapiMonitorMovedUp(wcb
->import_table
, wcb
->node
, moved
->node
,
2513 rfapiBgpInfoFree(bi
);
2517 * If route count at this node has gone to 0, withdraw exported prefix
2519 if (!wcb
->node
->info
) {
2520 /* see if the struct rfapi_it_extra is empty and can be freed */
2521 rfapiMonitorExtraPrune(SAFI_MPLS_VPN
, wcb
->node
);
2522 vnc_direct_bgp_del_prefix(bgp
, wcb
->import_table
, wcb
->node
);
2523 vnc_zebra_del_prefix(bgp
, wcb
->import_table
, wcb
->node
);
2526 * nexthop change event
2527 * vnc_direct_bgp_add_prefix() will recompute the VN addr
2530 vnc_direct_bgp_add_prefix(bgp
, wcb
->import_table
, wcb
->node
);
2533 RFAPI_CHECK_REFCOUNT(wcb
->node
, SAFI_MPLS_VPN
, 1 + wcb
->lockoffset
);
2534 route_unlock_node(wcb
->node
); /* decr ref count */
2535 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
2540 * This works for multiprotocol extension, but not for plain ol'
2541 * unicast IPv4 because that nexthop is stored in attr->nexthop
2543 void rfapiNexthop2Prefix(struct attr
*attr
, struct prefix
*p
)
2548 memset(p
, 0, sizeof(struct prefix
));
2550 switch (p
->family
= BGP_MP_NEXTHOP_FAMILY(attr
->mp_nexthop_len
)) {
2552 p
->u
.prefix4
= attr
->mp_nexthop_global_in
;
2557 p
->u
.prefix6
= attr
->mp_nexthop_global
;
2562 vnc_zlog_debug_verbose("%s: Family is unknown = %d", __func__
,
2567 void rfapiUnicastNexthop2Prefix(afi_t afi
, struct attr
*attr
, struct prefix
*p
)
2569 if (afi
== AFI_IP
) {
2570 p
->family
= AF_INET
;
2572 p
->u
.prefix4
= attr
->nexthop
;
2574 rfapiNexthop2Prefix(attr
, p
);
2578 static int rfapiAttrNexthopAddrDifferent(struct prefix
*p1
, struct prefix
*p2
)
2581 vnc_zlog_debug_verbose("%s: p1 or p2 is NULL", __func__
);
2586 * Are address families the same?
2588 if (p1
->family
!= p2
->family
) {
2592 switch (p1
->family
) {
2594 if (IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
2599 if (IPV6_ADDR_SAME(&p1
->u
.prefix6
, &p2
->u
.prefix6
))
2610 static void rfapiCopyUnEncap2VPN(struct bgp_info
*encap_bi
,
2611 struct bgp_info
*vpn_bi
)
2613 if (!encap_bi
->attr
) {
2614 zlog_warn("%s: no encap bi attr/extra, can't copy UN address",
2619 if (!vpn_bi
|| !vpn_bi
->extra
) {
2620 zlog_warn("%s: no vpn bi attr/extra, can't copy UN address",
2625 switch (BGP_MP_NEXTHOP_FAMILY(encap_bi
->attr
->mp_nexthop_len
)) {
2629 * instrumentation to debug segfault of 091127
2631 vnc_zlog_debug_verbose("%s: vpn_bi=%p", __func__
, vpn_bi
);
2633 vnc_zlog_debug_verbose("%s: vpn_bi->extra=%p", __func__
,
2637 vpn_bi
->extra
->vnc
.import
.un_family
= AF_INET
;
2638 vpn_bi
->extra
->vnc
.import
.un
.addr4
=
2639 encap_bi
->attr
->mp_nexthop_global_in
;
2643 vpn_bi
->extra
->vnc
.import
.un_family
= AF_INET6
;
2644 vpn_bi
->extra
->vnc
.import
.un
.addr6
=
2645 encap_bi
->attr
->mp_nexthop_global
;
2649 zlog_warn("%s: invalid encap nexthop length: %d", __func__
,
2650 encap_bi
->attr
->mp_nexthop_len
);
2651 vpn_bi
->extra
->vnc
.import
.un_family
= 0;
2657 * returns 0 on success, nonzero on error
2659 static int rfapiWithdrawEncapUpdateCachedUn(
2660 struct rfapi_import_table
*import_table
, struct bgp_info
*encap_bi
,
2661 struct route_node
*vpn_rn
, struct bgp_info
*vpn_bi
)
2666 * clear cached UN address
2668 if (!vpn_bi
|| !vpn_bi
->extra
) {
2670 "%s: missing VPN bi/extra, can't clear UN addr",
2674 vpn_bi
->extra
->vnc
.import
.un_family
= 0;
2675 memset(&vpn_bi
->extra
->vnc
.import
.un
, 0,
2676 sizeof(vpn_bi
->extra
->vnc
.import
.un
));
2677 if (CHECK_FLAG(vpn_bi
->flags
, BGP_INFO_VALID
)) {
2678 if (rfapiGetVncTunnelUnAddr(vpn_bi
->attr
, NULL
)) {
2679 UNSET_FLAG(vpn_bi
->flags
, BGP_INFO_VALID
);
2680 if (VALID_INTERIOR_TYPE(vpn_bi
->type
))
2681 RFAPI_MONITOR_EXTERIOR(vpn_rn
)
2682 ->valid_interior_count
--;
2683 /* signal interior route withdrawal to
2684 * import-exterior */
2685 vnc_import_bgp_exterior_del_route_interior(
2686 bgp_get_default(), import_table
, vpn_rn
,
2693 zlog_warn("%s: missing VPN bi, can't clear UN addr",
2697 rfapiCopyUnEncap2VPN(encap_bi
, vpn_bi
);
2698 if (!CHECK_FLAG(vpn_bi
->flags
, BGP_INFO_VALID
)) {
2699 SET_FLAG(vpn_bi
->flags
, BGP_INFO_VALID
);
2700 if (VALID_INTERIOR_TYPE(vpn_bi
->type
))
2701 RFAPI_MONITOR_EXTERIOR(vpn_rn
)
2702 ->valid_interior_count
++;
2703 /* signal interior route withdrawal to import-exterior
2705 vnc_import_bgp_exterior_add_route_interior(
2706 bgp_get_default(), import_table
, vpn_rn
,
2713 static int rfapiWithdrawTimerEncap(struct thread
*t
)
2715 struct rfapi_withdraw
*wcb
= t
->arg
;
2716 struct bgp_info
*bi
= wcb
->info
;
2717 int was_first_route
= 0;
2718 struct rfapi_monitor_encap
*em
;
2719 struct skiplist
*vpn_node_sl
= skiplist_new(0, NULL
, NULL
);
2723 assert(wcb
->import_table
);
2725 RFAPI_CHECK_REFCOUNT(wcb
->node
, SAFI_ENCAP
, 0);
2727 if (wcb
->node
->info
== bi
)
2728 was_first_route
= 1;
2731 * Remove the route/bi and free it
2733 rfapiBgpInfoDetach(wcb
->node
, bi
);
2734 rfapiBgpInfoFree(bi
);
2736 if (!was_first_route
)
2739 for (em
= RFAPI_MONITOR_ENCAP(wcb
->node
); em
; em
= em
->next
) {
2742 * Update monitoring VPN BIs with new encap info at the
2743 * head of the encap bi chain (which could be NULL after
2744 * removing the expiring bi above)
2746 if (rfapiWithdrawEncapUpdateCachedUn(wcb
->import_table
,
2747 wcb
->node
->info
, em
->node
,
2752 * Build a list of unique VPN nodes referenced by these
2754 * Use a skiplist for speed.
2756 skiplist_insert(vpn_node_sl
, em
->node
, em
->node
);
2761 * for each VPN node referenced in the ENCAP monitors:
2763 struct route_node
*rn
;
2764 while (!skiplist_first(vpn_node_sl
, (void **)&rn
, NULL
)) {
2765 if (!wcb
->node
->info
) {
2766 struct rfapi_monitor_vpn
*moved
;
2768 moved
= rfapiMonitorMoveShorter(rn
, 0);
2770 // rfapiDoRouteCallback(wcb->import_table,
2771 // moved->node, moved);
2772 rfapiMonitorMovedUp(wcb
->import_table
, rn
,
2773 moved
->node
, moved
);
2776 // rfapiDoRouteCallback(wcb->import_table, rn, NULL);
2777 rfapiMonitorItNodeChanged(wcb
->import_table
, rn
, NULL
);
2779 skiplist_delete_first(vpn_node_sl
);
2783 RFAPI_CHECK_REFCOUNT(wcb
->node
, SAFI_ENCAP
, 1);
2784 route_unlock_node(wcb
->node
); /* decr ref count */
2785 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
2786 skiplist_free(vpn_node_sl
);
2792 * Works for both VPN and ENCAP routes; timer_service_func is different
2796 rfapiBiStartWithdrawTimer(struct rfapi_import_table
*import_table
,
2797 struct route_node
*rn
, struct bgp_info
*bi
, afi_t afi
,
2799 int (*timer_service_func
)(struct thread
*))
2802 struct rfapi_withdraw
*wcb
;
2805 CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
)
2808 * Already on the path to being withdrawn,
2809 * should already have a timer set up to
2812 vnc_zlog_debug_verbose(
2813 "%s: already being withdrawn, do nothing",
2818 rfapiGetVncLifetime(bi
->attr
, &lifetime
);
2819 vnc_zlog_debug_verbose("%s: VNC lifetime is %u", __func__
, lifetime
);
2822 * withdrawn routes get to hang around for a while
2824 SET_FLAG(bi
->flags
, BGP_INFO_REMOVED
);
2826 /* set timer to remove the route later */
2827 lifetime
= rfapiGetHolddownFromLifetime(lifetime
);
2828 vnc_zlog_debug_verbose("%s: using timeout %u", __func__
, lifetime
);
2831 * Stash import_table, node, and info for use by timer
2832 * service routine, which is supposed to free the wcb.
2834 wcb
= XCALLOC(MTYPE_RFAPI_WITHDRAW
, sizeof(struct rfapi_withdraw
));
2838 wcb
->import_table
= import_table
;
2839 bgp_attr_intern(bi
->attr
);
2841 if (VNC_DEBUG(VERBOSE
)) {
2842 vnc_zlog_debug_verbose(
2843 "%s: wcb values: node=%p, info=%p, import_table=%p (bi follows)",
2844 __func__
, wcb
->node
, wcb
->info
, wcb
->import_table
);
2845 rfapiPrintBi(NULL
, bi
);
2850 if (lifetime
> UINT32_MAX
/ 1001) {
2851 /* sub-optimal case, but will probably never happen */
2852 bi
->extra
->vnc
.import
.timer
= NULL
;
2853 thread_add_timer(bm
->master
, timer_service_func
, wcb
, lifetime
,
2854 &bi
->extra
->vnc
.import
.timer
);
2856 static uint32_t jitter
;
2857 uint32_t lifetime_msec
;
2860 * the goal here is to spread out the timers so they are
2861 * sortable in the skip list
2863 if (++jitter
>= 1000)
2866 lifetime_msec
= (lifetime
* 1000) + jitter
;
2868 bi
->extra
->vnc
.import
.timer
= NULL
;
2869 thread_add_timer_msec(bm
->master
, timer_service_func
, wcb
,
2871 &bi
->extra
->vnc
.import
.timer
);
2874 /* re-sort route list (BGP_INFO_REMOVED routes are last) */
2875 if (((struct bgp_info
*)rn
->info
)->next
) {
2876 rfapiBgpInfoDetach(rn
, bi
);
2877 rfapiBgpInfoAttachSorted(rn
, bi
, afi
, safi
);
2882 typedef void(rfapi_bi_filtered_import_f
)(struct rfapi_import_table
*, int,
2883 struct peer
*, void *, struct prefix
*,
2884 struct prefix
*, afi_t
,
2885 struct prefix_rd
*, struct attr
*,
2886 u_char
, u_char
, uint32_t *);
2889 static void rfapiExpireEncapNow(struct rfapi_import_table
*it
,
2890 struct route_node
*rn
, struct bgp_info
*bi
)
2892 struct rfapi_withdraw
*wcb
;
2896 * pretend we're an expiring timer
2898 wcb
= XCALLOC(MTYPE_RFAPI_WITHDRAW
, sizeof(struct rfapi_withdraw
));
2901 wcb
->import_table
= it
;
2902 memset(&t
, 0, sizeof(t
));
2904 rfapiWithdrawTimerEncap(&t
); /* frees wcb */
2907 static int rfapiGetNexthop(struct attr
*attr
, struct prefix
*prefix
)
2909 switch (BGP_MP_NEXTHOP_FAMILY(attr
->mp_nexthop_len
)) {
2911 prefix
->family
= AF_INET
;
2912 prefix
->prefixlen
= 32;
2913 prefix
->u
.prefix4
= attr
->mp_nexthop_global_in
;
2916 prefix
->family
= AF_INET6
;
2917 prefix
->prefixlen
= 128;
2918 prefix
->u
.prefix6
= attr
->mp_nexthop_global
;
2921 vnc_zlog_debug_verbose("%s: unknown attr->mp_nexthop_len %d",
2922 __func__
, attr
->mp_nexthop_len
);
2929 * import a bgp_info if its route target list intersects with the
2930 * import table's route target list
2932 static void rfapiBgpInfoFilteredImportEncap(
2933 struct rfapi_import_table
*import_table
, int action
, struct peer
*peer
,
2934 void *rfd
, /* set for looped back routes */
2936 struct prefix
*aux_prefix
, /* Unused for encap routes */
2937 afi_t afi
, struct prefix_rd
*prd
,
2938 struct attr
*attr
, /* part of bgp_info */
2939 u_char type
, /* part of bgp_info */
2940 u_char sub_type
, /* part of bgp_info */
2941 uint32_t *label
) /* part of bgp_info */
2943 struct route_table
*rt
= NULL
;
2944 struct route_node
*rn
;
2945 struct bgp_info
*info_new
;
2946 struct bgp_info
*bi
;
2947 struct bgp_info
*next
;
2950 struct prefix p_firstbi_old
;
2951 struct prefix p_firstbi_new
;
2953 const char *action_str
= NULL
;
2954 struct prefix un_prefix
;
2957 bgp
= bgp_get_default(); /* assume 1 instance for now */
2960 case FIF_ACTION_UPDATE
:
2961 action_str
= "update";
2963 case FIF_ACTION_WITHDRAW
:
2964 action_str
= "withdraw";
2966 case FIF_ACTION_KILL
:
2967 action_str
= "kill";
2974 vnc_zlog_debug_verbose(
2975 "%s: entry: %s: prefix %s/%d", __func__
, action_str
,
2976 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
2978 memset(&p_firstbi_old
, 0, sizeof(p_firstbi_old
));
2979 memset(&p_firstbi_new
, 0, sizeof(p_firstbi_new
));
2981 if (action
== FIF_ACTION_UPDATE
) {
2983 * Compare rt lists. If no intersection, don't import this route
2984 * On a withdraw, peer and RD are sufficient to determine if
2987 if (!attr
|| !attr
->ecommunity
) {
2989 vnc_zlog_debug_verbose(
2990 "%s: attr, extra, or ecommunity missing, not importing",
2994 #if RFAPI_REQUIRE_ENCAP_BEEC
2995 if (!rfapiEcommunitiesMatchBeec(attr
->ecommunity
)) {
2996 vnc_zlog_debug_verbose(
2997 "%s: it=%p: no match for BGP Encapsulation ecommunity",
2998 __func__
, import_table
);
3002 if (!rfapiEcommunitiesIntersect(import_table
->rt_import_list
,
3003 attr
->ecommunity
)) {
3005 vnc_zlog_debug_verbose(
3006 "%s: it=%p: no ecommunity intersection",
3007 __func__
, import_table
);
3012 * Updates must also have a nexthop address
3014 memset(&un_prefix
, 0,
3015 sizeof(un_prefix
)); /* keep valgrind happy */
3016 if (rfapiGetNexthop(attr
, &un_prefix
)) {
3017 vnc_zlog_debug_verbose("%s: missing nexthop address",
3024 * Figure out which radix tree the route would go into
3029 rt
= import_table
->imported_encap
[afi
];
3033 zlog_err("%s: bad afi %d", __func__
, afi
);
3038 * route_node_lookup returns a node only if there is at least
3039 * one route attached.
3041 rn
= route_node_lookup(rt
, p
);
3043 #if DEBUG_ENCAP_MONITOR
3044 vnc_zlog_debug_verbose("%s: initial encap lookup(it=%p) rn=%p",
3045 __func__
, import_table
, rn
);
3050 RFAPI_CHECK_REFCOUNT(rn
, SAFI_ENCAP
, 1);
3051 route_unlock_node(rn
); /* undo lock in route_node_lookup */
3055 * capture nexthop of first bi
3058 rfapiNexthop2Prefix(
3059 ((struct bgp_info
*)(rn
->info
))->attr
,
3063 for (bi
= rn
->info
; bi
; bi
= bi
->next
) {
3066 * Does this bgp_info refer to the same route
3067 * as we are trying to add?
3069 vnc_zlog_debug_verbose("%s: comparing BI %p", __func__
,
3076 * RD of import table bi is in bi->extra->vnc.import.rd
3077 * RD of info_orig is in prd
3080 vnc_zlog_debug_verbose("%s: no bi->extra",
3085 (struct prefix
*)&bi
->extra
->vnc
.import
.rd
,
3086 (struct prefix
*)prd
)) {
3088 vnc_zlog_debug_verbose("%s: prd does not match",
3096 if (bi
->peer
!= peer
) {
3097 vnc_zlog_debug_verbose(
3098 "%s: peer does not match", __func__
);
3102 vnc_zlog_debug_verbose("%s: found matching bi",
3105 /* Same route. Delete this bi, replace with new one */
3107 if (action
== FIF_ACTION_WITHDRAW
) {
3109 vnc_zlog_debug_verbose(
3110 "%s: withdrawing at prefix %s/%d",
3112 inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
,
3116 rfapiBiStartWithdrawTimer(
3117 import_table
, rn
, bi
, afi
, SAFI_ENCAP
,
3118 rfapiWithdrawTimerEncap
);
3121 vnc_zlog_debug_verbose(
3122 "%s: %s at prefix %s/%d", __func__
,
3123 ((action
== FIF_ACTION_KILL
)
3126 inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
,
3131 * If this route is waiting to be deleted
3133 * a previous withdraw, we must cancel its
3136 if (CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
)
3137 && bi
->extra
->vnc
.import
.timer
) {
3140 (struct thread
*)bi
->extra
->vnc
3142 struct rfapi_withdraw
*wcb
= t
->arg
;
3144 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
3148 if (action
== FIF_ACTION_UPDATE
) {
3149 rfapiBgpInfoDetach(rn
, bi
);
3150 rfapiBgpInfoFree(bi
);
3154 * Kill: do export stuff when removing
3157 struct rfapi_withdraw
*wcb
;
3161 * pretend we're an expiring timer
3164 MTYPE_RFAPI_WITHDRAW
,
3165 sizeof(struct rfapi_withdraw
));
3168 wcb
->import_table
= import_table
;
3169 memset(&t
, 0, sizeof(t
));
3171 rfapiWithdrawTimerEncap(
3172 &t
); /* frees wcb */
3181 RFAPI_CHECK_REFCOUNT(rn
, SAFI_ENCAP
, replacing
? 1 : 0);
3183 if (action
== FIF_ACTION_WITHDRAW
|| action
== FIF_ACTION_KILL
)
3187 rfapiBgpInfoCreate(attr
, peer
, rfd
, prd
, type
, sub_type
, NULL
);
3191 route_lock_node(rn
); /* incr ref count for new BI */
3193 rn
= route_node_get(rt
, p
);
3196 vnc_zlog_debug_verbose(
3197 "%s: (afi=%d, rn=%p) inserting at prefix %s/%d", __func__
, afi
,
3198 rn
, inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, BUFSIZ
),
3201 rfapiBgpInfoAttachSorted(rn
, info_new
, afi
, SAFI_ENCAP
);
3204 * Delete holddown routes from same NVE. See details in
3205 * rfapiBgpInfoFilteredImportVPN()
3207 for (bi
= info_new
->next
; bi
; bi
= next
) {
3209 struct prefix pfx_un
;
3213 if (!CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
))
3217 * We already match the VN address (it is the prefix
3218 * of the route node)
3221 if (!rfapiGetNexthop(bi
->attr
, &pfx_un
)
3222 && prefix_same(&pfx_un
, &un_prefix
)) {
3230 vnc_zlog_debug_verbose(
3231 "%s: removing holddown bi matching NVE of new route",
3233 if (bi
->extra
->vnc
.import
.timer
) {
3235 (struct thread
*)bi
->extra
->vnc
.import
.timer
;
3236 struct rfapi_withdraw
*wcb
= t
->arg
;
3238 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
3241 rfapiExpireEncapNow(import_table
, rn
, bi
);
3244 rfapiNexthop2Prefix(((struct bgp_info
*)(rn
->info
))->attr
,
3248 * If the nexthop address of the selected Encap route (i.e.,
3249 * the UN address) has changed, then we must update the VPN
3250 * routes that refer to this Encap route and possibly force
3253 if (rfapiAttrNexthopAddrDifferent(&p_firstbi_old
, &p_firstbi_new
)) {
3255 struct rfapi_monitor_encap
*m
;
3256 struct rfapi_monitor_encap
*mnext
;
3258 struct route_node
*referenced_vpn_prefix
;
3261 * Optimized approach: build radix tree on the fly to
3262 * hold list of VPN nodes referenced by the ENCAP monitors
3264 * The nodes in this table correspond to prefixes of VPN routes.
3265 * The "info" pointer of the node points to a chain of
3266 * struct rfapi_monitor_encap, each of which refers to a
3267 * specific VPN node.
3269 struct route_table
*referenced_vpn_table
;
3271 referenced_vpn_table
= route_table_init();
3272 assert(referenced_vpn_table
);
3275 * iterate over the set of monitors at this ENCAP node.
3277 #if DEBUG_ENCAP_MONITOR
3278 vnc_zlog_debug_verbose("%s: examining monitors at rn=%p",
3281 for (m
= RFAPI_MONITOR_ENCAP(rn
); m
; m
= m
->next
) {
3284 * For each referenced bi/route, copy the ENCAP route's
3285 * nexthop to the VPN route's cached UN address field
3287 * the address family of the cached UN address field.
3289 rfapiCopyUnEncap2VPN(info_new
, m
->bi
);
3290 if (!CHECK_FLAG(m
->bi
->flags
, BGP_INFO_VALID
)) {
3291 SET_FLAG(m
->bi
->flags
, BGP_INFO_VALID
);
3292 if (VALID_INTERIOR_TYPE(m
->bi
->type
))
3293 RFAPI_MONITOR_EXTERIOR(m
->node
)
3294 ->valid_interior_count
++;
3295 vnc_import_bgp_exterior_add_route_interior(
3296 bgp
, import_table
, m
->node
, m
->bi
);
3300 * Build a list of unique VPN nodes referenced by these
3303 * There could be more than one VPN node here with a
3305 * prefix. Those are currently in an unsorted linear
3310 referenced_vpn_prefix
= route_node_get(
3311 referenced_vpn_table
, &m
->node
->p
);
3312 assert(referenced_vpn_prefix
);
3313 for (mnext
= referenced_vpn_prefix
->info
; mnext
;
3314 mnext
= mnext
->next
) {
3316 if (mnext
->node
== m
->node
)
3322 * already have an entry for this VPN node
3324 route_unlock_node(referenced_vpn_prefix
);
3327 MTYPE_RFAPI_MONITOR_ENCAP
,
3328 sizeof(struct rfapi_monitor_encap
));
3330 mnext
->node
= m
->node
;
3331 mnext
->next
= referenced_vpn_prefix
->info
;
3332 referenced_vpn_prefix
->info
= mnext
;
3337 * for each VPN node referenced in the ENCAP monitors:
3339 for (referenced_vpn_prefix
= route_top(referenced_vpn_table
);
3340 referenced_vpn_prefix
; referenced_vpn_prefix
= route_next(
3341 referenced_vpn_prefix
)) {
3343 while ((m
= referenced_vpn_prefix
->info
)) {
3345 struct route_node
*n
;
3347 rfapiMonitorMoveLonger(m
->node
);
3348 for (n
= m
->node
; n
; n
= n
->parent
) {
3349 // rfapiDoRouteCallback(import_table, n,
3352 rfapiMonitorItNodeChanged(import_table
, m
->node
,
3355 referenced_vpn_prefix
->info
= m
->next
;
3356 route_unlock_node(referenced_vpn_prefix
);
3357 XFREE(MTYPE_RFAPI_MONITOR_ENCAP
, m
);
3360 route_table_finish(referenced_vpn_table
);
3363 RFAPI_CHECK_REFCOUNT(rn
, SAFI_ENCAP
, 0);
3366 static void rfapiExpireVpnNow(struct rfapi_import_table
*it
,
3367 struct route_node
*rn
, struct bgp_info
*bi
,
3370 struct rfapi_withdraw
*wcb
;
3374 * pretend we're an expiring timer
3376 wcb
= XCALLOC(MTYPE_RFAPI_WITHDRAW
, sizeof(struct rfapi_withdraw
));
3379 wcb
->import_table
= it
;
3380 wcb
->lockoffset
= lockoffset
;
3381 memset(&t
, 0, sizeof(t
));
3383 rfapiWithdrawTimerVPN(&t
); /* frees wcb */
3388 * import a bgp_info if its route target list intersects with the
3389 * import table's route target list
3391 void rfapiBgpInfoFilteredImportVPN(
3392 struct rfapi_import_table
*import_table
, int action
, struct peer
*peer
,
3393 void *rfd
, /* set for looped back routes */
3395 struct prefix
*aux_prefix
, /* AFI_L2VPN: optional IP */
3396 afi_t afi
, struct prefix_rd
*prd
,
3397 struct attr
*attr
, /* part of bgp_info */
3398 u_char type
, /* part of bgp_info */
3399 u_char sub_type
, /* part of bgp_info */
3400 uint32_t *label
) /* part of bgp_info */
3402 struct route_table
*rt
= NULL
;
3403 struct route_node
*rn
;
3404 struct route_node
*n
;
3405 struct bgp_info
*info_new
;
3406 struct bgp_info
*bi
;
3407 struct bgp_info
*next
;
3409 struct prefix vn_prefix
;
3410 struct prefix un_prefix
;
3411 int un_prefix_valid
= 0;
3412 struct route_node
*ern
;
3414 int original_had_routes
= 0;
3415 struct prefix original_nexthop
;
3416 const char *action_str
= NULL
;
3420 bgp
= bgp_get_default(); /* assume 1 instance for now */
3423 case FIF_ACTION_UPDATE
:
3424 action_str
= "update";
3426 case FIF_ACTION_WITHDRAW
:
3427 action_str
= "withdraw";
3429 case FIF_ACTION_KILL
:
3430 action_str
= "kill";
3437 if (import_table
== bgp
->rfapi
->it_ce
)
3440 vnc_zlog_debug_verbose("%s: entry: %s%s: prefix %s/%d: it %p, afi %s",
3441 __func__
, (is_it_ce
? "CE-IT " : ""), action_str
,
3442 rfapi_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
3443 p
->prefixlen
, import_table
, afi2str(afi
));
3448 * Compare rt lists. If no intersection, don't import this route
3449 * On a withdraw, peer and RD are sufficient to determine if
3452 if (action
== FIF_ACTION_UPDATE
) {
3453 if (!attr
|| !attr
->ecommunity
) {
3455 vnc_zlog_debug_verbose(
3456 "%s: attr, extra, or ecommunity missing, not importing",
3460 if ((import_table
!= bgp
->rfapi
->it_ce
)
3461 && !rfapiEcommunitiesIntersect(import_table
->rt_import_list
,
3462 attr
->ecommunity
)) {
3464 vnc_zlog_debug_verbose(
3465 "%s: it=%p: no ecommunity intersection",
3466 __func__
, import_table
);
3470 memset(&vn_prefix
, 0,
3471 sizeof(vn_prefix
)); /* keep valgrind happy */
3472 if (rfapiGetNexthop(attr
, &vn_prefix
)) {
3473 /* missing nexthop address would be a bad, bad thing */
3474 vnc_zlog_debug_verbose("%s: missing nexthop", __func__
);
3480 * Figure out which radix tree the route would go into
3486 rt
= import_table
->imported_vpn
[afi
];
3490 zlog_err("%s: bad afi %d", __func__
, afi
);
3495 memset(&original_nexthop
, 0, sizeof(original_nexthop
));
3498 * route_node_lookup returns a node only if there is at least
3499 * one route attached.
3501 rn
= route_node_lookup(rt
, p
);
3503 vnc_zlog_debug_verbose("%s: rn=%p", __func__
, rn
);
3507 RFAPI_CHECK_REFCOUNT(rn
, SAFI_MPLS_VPN
, 1);
3508 route_unlock_node(rn
); /* undo lock in route_node_lookup */
3511 original_had_routes
= 1;
3513 if (VNC_DEBUG(VERBOSE
)) {
3514 vnc_zlog_debug_verbose("%s: showing IT node on entry",
3516 rfapiShowItNode(NULL
, rn
); /* debug */
3520 * Look for same route (will have same RD and peer)
3522 bi
= rfapiItBiIndexSearch(rn
, prd
, peer
, aux_prefix
);
3527 * This was an old test when we iterated over the
3528 * BIs linearly. Since we're now looking up with
3529 * RD and peer, comparing types should not be
3530 * needed. Changed to assertion.
3532 * Compare types. Doing so prevents a RFP-originated
3533 * route from matching an imported route, for example.
3535 assert(bi
->type
== type
);
3537 vnc_zlog_debug_verbose("%s: found matching bi",
3541 * In the special CE table, withdrawals occur without
3544 if (import_table
== bgp
->rfapi
->it_ce
) {
3545 vnc_direct_bgp_del_route_ce(bgp
, rn
, bi
);
3546 if (action
== FIF_ACTION_WITHDRAW
)
3547 action
= FIF_ACTION_KILL
;
3550 if (action
== FIF_ACTION_WITHDRAW
) {
3553 CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
);
3555 vnc_zlog_debug_verbose(
3556 "%s: withdrawing at prefix %s/%d%s",
3557 __func__
, rfapi_ntop(rn
->p
.family
,
3562 ? " (already being withdrawn)"
3567 rfapiBiStartWithdrawTimer(
3568 import_table
, rn
, bi
, afi
,
3570 rfapiWithdrawTimerVPN
);
3572 RFAPI_UPDATE_ITABLE_COUNT(
3573 bi
, import_table
, afi
, -1);
3574 import_table
->holddown_count
[afi
] += 1;
3578 vnc_zlog_debug_verbose(
3579 "%s: %s at prefix %s/%d", __func__
,
3580 ((action
== FIF_ACTION_KILL
)
3583 rfapi_ntop(rn
->p
.family
,
3584 &rn
->p
.u
.prefix
, buf
,
3589 * If this route is waiting to be deleted
3591 * a previous withdraw, we must cancel its
3594 if (CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
)
3595 && bi
->extra
->vnc
.import
.timer
) {
3598 (struct thread
*)bi
->extra
->vnc
3600 struct rfapi_withdraw
*wcb
= t
->arg
;
3602 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
3605 import_table
->holddown_count
[afi
] -= 1;
3606 RFAPI_UPDATE_ITABLE_COUNT(
3607 bi
, import_table
, afi
, 1);
3610 * decrement remote count (if route is remote)
3612 * we are going to remove it below
3614 RFAPI_UPDATE_ITABLE_COUNT(bi
, import_table
, afi
,
3616 if (action
== FIF_ACTION_UPDATE
) {
3620 * make copy of original nexthop so we
3621 * can see if it changed
3623 rfapiGetNexthop(bi
->attr
,
3627 * remove bi without doing any export
3630 if (CHECK_FLAG(bi
->flags
,
3632 && VALID_INTERIOR_TYPE(bi
->type
))
3633 RFAPI_MONITOR_EXTERIOR(rn
)
3634 ->valid_interior_count
--;
3635 rfapiItBiIndexDel(rn
, bi
);
3636 rfapiBgpInfoDetach(rn
, bi
);
3637 rfapiMonitorEncapDelete(bi
);
3638 vnc_import_bgp_exterior_del_route_interior(
3639 bgp
, import_table
, rn
, bi
);
3640 rfapiBgpInfoFree(bi
);
3644 * remove bi and do export processing
3646 import_table
->holddown_count
[afi
] += 1;
3647 rfapiExpireVpnNow(import_table
, rn
, bi
,
3655 RFAPI_CHECK_REFCOUNT(rn
, SAFI_MPLS_VPN
, replacing
? 1 : 0);
3657 if (action
== FIF_ACTION_WITHDRAW
|| action
== FIF_ACTION_KILL
) {
3663 rfapiBgpInfoCreate(attr
, peer
, rfd
, prd
, type
, sub_type
, label
);
3666 * lookup un address in encap table
3668 ern
= route_node_match(import_table
->imported_encap
[afi
], &vn_prefix
);
3670 rfapiCopyUnEncap2VPN(ern
->info
, info_new
);
3671 route_unlock_node(ern
); /* undo lock in route_note_match */
3674 prefix2str(&vn_prefix
, buf
, sizeof(buf
));
3675 buf
[BUFSIZ
- 1] = 0;
3676 /* Not a big deal, just means VPN route got here first */
3677 vnc_zlog_debug_verbose("%s: no encap route for vn addr %s",
3679 info_new
->extra
->vnc
.import
.un_family
= 0;
3684 route_lock_node(rn
);
3687 * No need to increment reference count, so only "get"
3688 * if the node is not there already
3690 rn
= route_node_get(rt
, p
);
3694 * For ethernet routes, if there is an accompanying IP address,
3697 if ((AFI_L2VPN
== afi
) && aux_prefix
) {
3699 vnc_zlog_debug_verbose("%s: setting BI's aux_prefix", __func__
);
3700 info_new
->extra
->vnc
.import
.aux_prefix
= *aux_prefix
;
3703 vnc_zlog_debug_verbose(
3704 "%s: inserting bi %p at prefix %s/%d #%d", __func__
, info_new
,
3705 rfapi_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, BUFSIZ
),
3706 rn
->p
.prefixlen
, rn
->lock
);
3708 rfapiBgpInfoAttachSorted(rn
, info_new
, afi
, SAFI_MPLS_VPN
);
3709 rfapiItBiIndexAdd(rn
, info_new
);
3710 if (!rfapiGetUnAddrOfVpnBi(info_new
, NULL
)) {
3711 if (VALID_INTERIOR_TYPE(info_new
->type
))
3712 RFAPI_MONITOR_EXTERIOR(rn
)->valid_interior_count
++;
3713 SET_FLAG(info_new
->flags
, BGP_INFO_VALID
);
3715 RFAPI_UPDATE_ITABLE_COUNT(info_new
, import_table
, afi
, 1);
3716 vnc_import_bgp_exterior_add_route_interior(bgp
, import_table
, rn
,
3719 if (import_table
== bgp
->rfapi
->it_ce
)
3720 vnc_direct_bgp_add_route_ce(bgp
, rn
, info_new
);
3722 if (VNC_DEBUG(VERBOSE
)) {
3723 vnc_zlog_debug_verbose("%s: showing IT node", __func__
);
3724 rfapiShowItNode(NULL
, rn
); /* debug */
3727 rfapiMonitorEncapAdd(import_table
, &vn_prefix
, rn
, info_new
);
3729 if (!rfapiGetUnAddrOfVpnBi(info_new
, &un_prefix
)) {
3732 * if we have a valid UN address (either via Encap route
3733 * or via tunnel attribute), then we should attempt
3734 * to move any monitors at less-specific nodes to this node
3736 rfapiMonitorMoveLonger(rn
);
3738 un_prefix_valid
= 1;
3742 * 101129 Enhancement: if we add a route (implication: it is not
3743 * in holddown), delete all other routes from this nve at this
3744 * node that are in holddown, regardless of peer.
3746 * Reasons it's OK to do that:
3748 * - if the holddown route being deleted originally came from BGP VPN,
3749 * it is already gone from BGP (implication of holddown), so there
3750 * won't be any added inconsistency with the BGP RIB.
3752 * - once a fresh route is added at a prefix, any routes in holddown
3753 * at that prefix will not show up in RFP responses, so deleting
3754 * the holddown routes won't affect the contents of responses.
3756 * - lifetimes are supposed to be consistent, so there should not
3757 * be a case where the fresh route has a shorter lifetime than
3758 * the holddown route, so we don't expect the fresh route to
3759 * disappear and complete its holddown time before the existing
3760 * holddown routes time out. Therefore, we won't have a situation
3761 * where we expect the existing holddown routes to be hidden and
3762 * then to reappear sometime later (as holddown routes) in a
3765 * Among other things, this would enable us to skirt the problem
3766 * of local holddown routes that refer to NVE descriptors that
3767 * have already been closed (if the same NVE triggers a subsequent
3768 * rfapi_open(), the new peer is different and doesn't match the
3769 * peer of the holddown route, so the stale holddown route still
3770 * hangs around until it times out instead of just being replaced
3771 * by the fresh route).
3774 * We know that the new bi will have been inserted before any routes
3775 * in holddown, so we can skip any that came before it
3777 for (bi
= info_new
->next
; bi
; bi
= next
) {
3779 struct prefix pfx_vn
;
3780 struct prefix pfx_un
;
3782 int remote_peer_match
= 0;
3789 if (!CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
))
3793 * Must match VN address (nexthop of VPN route)
3795 if (rfapiGetNexthop(bi
->attr
, &pfx_vn
))
3797 if (!prefix_same(&pfx_vn
, &vn_prefix
))
3800 if (un_prefix_valid
&& /* new route UN addr */
3801 !rfapiGetUnAddrOfVpnBi(bi
, &pfx_un
)
3802 && /* old route UN addr */
3803 prefix_same(&pfx_un
, &un_prefix
)) { /* compare */
3806 if (!RFAPI_LOCAL_BI(bi
) && !RFAPI_LOCAL_BI(info_new
)
3807 && sockunion_same(&bi
->peer
->su
, &info_new
->peer
->su
)) {
3808 /* old & new are both remote, same peer */
3809 remote_peer_match
= 1;
3812 if (!un_match
& !remote_peer_match
)
3815 vnc_zlog_debug_verbose(
3816 "%s: removing holddown bi matching NVE of new route",
3818 if (bi
->extra
->vnc
.import
.timer
) {
3820 (struct thread
*)bi
->extra
->vnc
.import
.timer
;
3821 struct rfapi_withdraw
*wcb
= t
->arg
;
3823 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
3826 rfapiExpireVpnNow(import_table
, rn
, bi
, 0);
3829 if (!original_had_routes
) {
3831 * We went from 0 usable routes to 1 usable route. Perform the
3832 * "Adding a Route" export process.
3834 vnc_direct_bgp_add_prefix(bgp
, import_table
, rn
);
3835 vnc_zebra_add_prefix(bgp
, import_table
, rn
);
3838 * Check for nexthop change event
3839 * Note: the prefix_same() test below detects two situations:
3840 * 1. route is replaced, new route has different nexthop
3841 * 2. new route is added (original_nexthop is 0)
3843 struct prefix new_nexthop
;
3845 rfapiGetNexthop(attr
, &new_nexthop
);
3846 if (!prefix_same(&original_nexthop
, &new_nexthop
)) {
3848 * nexthop change event
3849 * vnc_direct_bgp_add_prefix() will recompute VN addr
3852 vnc_direct_bgp_add_prefix(bgp
, import_table
, rn
);
3856 if (!(bgp
->rfapi_cfg
->flags
& BGP_VNC_CONFIG_CALLBACK_DISABLE
)) {
3857 for (n
= rn
; n
; n
= n
->parent
) {
3858 // rfapiDoRouteCallback(import_table, n, NULL);
3860 rfapiMonitorItNodeChanged(import_table
, rn
, NULL
);
3862 RFAPI_CHECK_REFCOUNT(rn
, SAFI_MPLS_VPN
, 0);
3866 static rfapi_bi_filtered_import_f
*
3867 rfapiBgpInfoFilteredImportFunction(safi_t safi
)
3871 return rfapiBgpInfoFilteredImportVPN
;
3874 return rfapiBgpInfoFilteredImportEncap
;
3880 zlog_err("%s: bad safi %d", __func__
, safi
);
3884 void rfapiProcessUpdate(struct peer
*peer
,
3885 void *rfd
, /* set when looped from RFP/RFAPI */
3886 struct prefix
*p
, struct prefix_rd
*prd
,
3887 struct attr
*attr
, afi_t afi
, safi_t safi
, u_char type
,
3888 u_char sub_type
, uint32_t *label
)
3892 struct rfapi_import_table
*it
;
3893 int has_ip_route
= 1;
3896 bgp
= bgp_get_default(); /* assume 1 instance for now */
3903 * look at high-order byte of RD. FF means MAC
3904 * address is present (VNC L2VPN)
3906 if ((safi
== SAFI_MPLS_VPN
)
3907 && (decode_rd_type(prd
->val
) == RD_TYPE_VNC_ETH
)) {
3908 struct prefix pfx_mac_buf
;
3909 struct prefix pfx_nexthop_buf
;
3913 * Set flag if prefix and nexthop are the same - don't
3914 * add the route to normal IP-based import tables
3916 if (!rfapiGetNexthop(attr
, &pfx_nexthop_buf
)) {
3917 if (!prefix_cmp(&pfx_nexthop_buf
, p
)) {
3922 memset(&pfx_mac_buf
, 0, sizeof(pfx_mac_buf
));
3923 pfx_mac_buf
.family
= AF_ETHERNET
;
3924 pfx_mac_buf
.prefixlen
= 48;
3925 memcpy(&pfx_mac_buf
.u
.prefix_eth
.octet
, prd
->val
+ 2, 6);
3928 * Find rt containing LNI (Logical Network ID), which
3929 * _should_ always be present when mac address is present
3931 rc
= rfapiEcommunityGetLNI(attr
->ecommunity
, &lni
);
3933 vnc_zlog_debug_verbose(
3934 "%s: rfapiEcommunityGetLNI returned %d, lni=%d, attr=%p",
3935 __func__
, rc
, lni
, attr
);
3937 it
= rfapiMacImportTableGet(bgp
, lni
);
3939 rfapiBgpInfoFilteredImportVPN(
3940 it
, FIF_ACTION_UPDATE
, peer
, rfd
,
3941 &pfx_mac_buf
, /* prefix */
3942 p
, /* aux prefix: IP addr */
3943 AFI_L2VPN
, prd
, attr
, type
, sub_type
, label
);
3951 * Iterate over all import tables; do a filtered import
3952 * for the afi/safi combination
3954 for (it
= h
->imports
; it
; it
= it
->next
) {
3955 (*rfapiBgpInfoFilteredImportFunction(safi
))(
3956 it
, FIF_ACTION_UPDATE
, peer
, rfd
, p
, /* prefix */
3957 NULL
, afi
, prd
, attr
, type
, sub_type
, label
);
3960 if (safi
== SAFI_MPLS_VPN
) {
3961 vnc_direct_bgp_rh_add_route(bgp
, afi
, p
, peer
, attr
);
3964 if (safi
== SAFI_MPLS_VPN
) {
3965 rfapiBgpInfoFilteredImportVPN(
3966 bgp
->rfapi
->it_ce
, FIF_ACTION_UPDATE
, peer
, rfd
,
3968 NULL
, afi
, prd
, attr
, type
, sub_type
, label
);
3973 void rfapiProcessWithdraw(struct peer
*peer
, void *rfd
, struct prefix
*p
,
3974 struct prefix_rd
*prd
, struct attr
*attr
, afi_t afi
,
3975 safi_t safi
, u_char type
, int kill
)
3979 struct rfapi_import_table
*it
;
3981 bgp
= bgp_get_default(); /* assume 1 instance for now */
3988 * look at high-order byte of RD. FF means MAC
3989 * address is present (VNC L2VPN)
3991 if (h
->import_mac
!= NULL
&& safi
== SAFI_MPLS_VPN
3992 && decode_rd_type(prd
->val
) == RD_TYPE_VNC_ETH
) {
3993 struct prefix pfx_mac_buf
;
3994 void *cursor
= NULL
;
3997 memset(&pfx_mac_buf
, 0, sizeof(pfx_mac_buf
));
3998 pfx_mac_buf
.family
= AF_ETHERNET
;
3999 pfx_mac_buf
.prefixlen
= 48;
4000 memcpy(&pfx_mac_buf
.u
.prefix_eth
, prd
->val
+ 2, 6);
4003 * withdraw does not contain attrs, so we don't have
4004 * access to the route's LNI, which would ordinarily
4005 * select the specific mac-based import table. Instead,
4006 * we must iterate over all mac-based tables and rely
4007 * on the RD to match.
4009 * If this approach is too slow, add an index where
4010 * key is {RD, peer} and value is the import table
4012 for (rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4014 rc
== 0; rc
= skiplist_next(h
->import_mac
, NULL
,
4015 (void **)&it
, &cursor
)) {
4018 vnc_zlog_debug_verbose(
4019 "%s: calling rfapiBgpInfoFilteredImportVPN(it=%p, afi=AFI_L2VPN)",
4023 rfapiBgpInfoFilteredImportVPN(
4025 (kill
? FIF_ACTION_KILL
: FIF_ACTION_WITHDRAW
),
4026 peer
, rfd
, &pfx_mac_buf
, /* prefix */
4027 p
, /* aux_prefix: IP */
4028 AFI_L2VPN
, prd
, attr
, type
, 0,
4029 NULL
); /* sub_type & label unused for withdraw
4035 * XXX For the case where the withdraw involves an L2
4036 * route with no IP information, we rely on the lack
4037 * of RT-list intersection to filter out the withdraw
4038 * from the IP-based import tables below
4042 * Iterate over all import tables; do a filtered import
4043 * for the afi/safi combination
4046 for (it
= h
->imports
; it
; it
= it
->next
) {
4047 (*rfapiBgpInfoFilteredImportFunction(safi
))(
4048 it
, (kill
? FIF_ACTION_KILL
: FIF_ACTION_WITHDRAW
),
4049 peer
, rfd
, p
, /* prefix */
4050 NULL
, afi
, prd
, attr
, type
, 0,
4051 NULL
); /* sub_type & label unused for withdraw */
4054 /* TBD the deletion should happen after the lifetime expires */
4055 if (safi
== SAFI_MPLS_VPN
)
4056 vnc_direct_bgp_rh_del_route(bgp
, afi
, p
, peer
);
4058 if (safi
== SAFI_MPLS_VPN
) {
4059 rfapiBgpInfoFilteredImportVPN(
4061 (kill
? FIF_ACTION_KILL
: FIF_ACTION_WITHDRAW
), peer
,
4062 rfd
, p
, /* prefix */
4063 NULL
, afi
, prd
, attr
, type
, 0,
4064 NULL
); /* sub_type & label unused for withdraw */
4069 * TBD optimized withdraw timer algorithm for case of many
4070 * routes expiring at the same time due to peer drop.
4073 * 1. Visit all BIs in all ENCAP import tables.
4075 * a. If a bi's peer is the failed peer, remove the bi.
4076 * b. If the removed ENCAP bi was first in the list of
4077 * BIs at this ENCAP node, loop over all monitors
4080 * (1) for each ENCAP monitor, loop over all its
4081 * VPN node monitors and set their RFAPI_MON_FLAG_NEEDCALLBACK
4084 * 2. Visit all BIs in all VPN import tables.
4085 * a. If a bi's peer is the failed peer, remove the bi.
4086 * b. loop over all the VPN node monitors and set their
4087 * RFAPI_MON_FLAG_NEEDCALLBACK flags
4088 * c. If there are no BIs left at this VPN node,
4093 /* surprise, this gets called from peer_delete(), from rfapi_close() */
4094 static void rfapiProcessPeerDownRt(struct peer
*peer
,
4095 struct rfapi_import_table
*import_table
,
4096 afi_t afi
, safi_t safi
)
4098 struct route_node
*rn
;
4099 struct bgp_info
*bi
;
4100 struct route_table
*rt
;
4101 int (*timer_service_func
)(struct thread
*);
4103 assert(afi
== AFI_IP
|| afi
== AFI_IP6
);
4109 rt
= import_table
->imported_vpn
[afi
];
4110 timer_service_func
= rfapiWithdrawTimerVPN
;
4113 rt
= import_table
->imported_encap
[afi
];
4114 timer_service_func
= rfapiWithdrawTimerEncap
;
4121 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
)) {
4122 for (bi
= rn
->info
; bi
; bi
= bi
->next
) {
4123 if (bi
->peer
== peer
) {
4125 if (CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
)) {
4126 /* already in holddown, skip */
4130 if (safi
== SAFI_MPLS_VPN
) {
4131 RFAPI_UPDATE_ITABLE_COUNT(
4132 bi
, import_table
, afi
, -1);
4133 import_table
->holddown_count
[afi
] += 1;
4135 rfapiBiStartWithdrawTimer(import_table
, rn
, bi
,
4137 timer_service_func
);
4145 * This gets called when a peer connection drops. We have to remove
4146 * all the routes from this peer.
4148 * Current approach is crude. TBD Optimize by setting fewer timers and
4149 * grouping withdrawn routes so we can generate callbacks more
4152 void rfapiProcessPeerDown(struct peer
*peer
)
4156 struct rfapi_import_table
*it
;
4159 * If this peer is a "dummy" peer structure atached to a RFAPI
4160 * nve_descriptor, we don't need to walk the import tables
4161 * because the routes are already withdrawn by rfapi_close()
4163 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_IS_RFAPI_HD
))
4167 * 1. Visit all BIs in all ENCAP import tables.
4168 * Start withdraw timer on the BIs that match peer.
4170 * 2. Visit All BIs in all VPN import tables.
4171 * Start withdraw timer on the BIs that match peer.
4174 bgp
= bgp_get_default(); /* assume 1 instance for now */
4181 for (it
= h
->imports
; it
; it
= it
->next
) {
4182 rfapiProcessPeerDownRt(peer
, it
, AFI_IP
, SAFI_ENCAP
);
4183 rfapiProcessPeerDownRt(peer
, it
, AFI_IP6
, SAFI_ENCAP
);
4184 rfapiProcessPeerDownRt(peer
, it
, AFI_IP
, SAFI_MPLS_VPN
);
4185 rfapiProcessPeerDownRt(peer
, it
, AFI_IP6
, SAFI_MPLS_VPN
);
4189 rfapiProcessPeerDownRt(peer
, h
->it_ce
, AFI_IP
, SAFI_MPLS_VPN
);
4190 rfapiProcessPeerDownRt(peer
, h
->it_ce
, AFI_IP6
, SAFI_MPLS_VPN
);
4195 * Import an entire RIB (for an afi/safi) to an import table RIB,
4196 * filtered according to the import table's RT list
4198 * TBD: does this function need additions to match rfapiProcessUpdate()
4199 * for, e.g., L2 handling?
4201 static void rfapiBgpTableFilteredImport(struct bgp
*bgp
,
4202 struct rfapi_import_table
*it
,
4203 afi_t afi
, safi_t safi
)
4205 struct bgp_node
*rn1
;
4206 struct bgp_node
*rn2
;
4208 /* Only these SAFIs have 2-level RIBS */
4209 assert(safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
);
4212 * Now visit all the rd nodes and the nodes of all the
4213 * route tables attached to them, and import the routes
4214 * if they have matching route targets
4216 for (rn1
= bgp_table_top(bgp
->rib
[afi
][safi
]); rn1
;
4217 rn1
= bgp_route_next(rn1
)) {
4220 for (rn2
= bgp_table_top(rn1
->info
); rn2
;
4221 rn2
= bgp_route_next(rn2
)) {
4223 struct bgp_info
*bi
;
4225 for (bi
= rn2
->info
; bi
; bi
= bi
->next
) {
4226 u_int32_t label
= 0;
4228 if (CHECK_FLAG(bi
->flags
,
4233 label
= decode_label(
4235 (*rfapiBgpInfoFilteredImportFunction(
4237 it
, /* which import table */
4238 FIF_ACTION_UPDATE
, bi
->peer
,
4239 NULL
, &rn2
->p
, /* prefix */
4241 (struct prefix_rd
*)&rn1
->p
,
4243 bi
->sub_type
, &label
);
4251 /* per-bgp-instance rfapi data */
4252 struct rfapi
*bgp_rfapi_new(struct bgp
*bgp
)
4256 struct rfapi_rfp_cfg
*cfg
= NULL
;
4257 struct rfapi_rfp_cb_methods
*cbm
= NULL
;
4259 assert(bgp
->rfapi_cfg
== NULL
);
4261 h
= (struct rfapi
*)XCALLOC(MTYPE_RFAPI
, sizeof(struct rfapi
));
4263 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
4264 h
->un
[afi
] = route_table_init();
4268 * initialize the ce import table
4270 h
->it_ce
= XCALLOC(MTYPE_RFAPI_IMPORTTABLE
,
4271 sizeof(struct rfapi_import_table
));
4272 h
->it_ce
->imported_vpn
[AFI_IP
] = route_table_init();
4273 h
->it_ce
->imported_vpn
[AFI_IP6
] = route_table_init();
4274 h
->it_ce
->imported_encap
[AFI_IP
] = route_table_init();
4275 h
->it_ce
->imported_encap
[AFI_IP6
] = route_table_init();
4276 rfapiBgpTableFilteredImport(bgp
, h
->it_ce
, AFI_IP
, SAFI_MPLS_VPN
);
4277 rfapiBgpTableFilteredImport(bgp
, h
->it_ce
, AFI_IP6
, SAFI_MPLS_VPN
);
4280 * Set up work queue for deferred rfapi_close operations
4282 h
->deferred_close_q
=
4283 work_queue_new(bm
->master
, "rfapi deferred close");
4284 h
->deferred_close_q
->spec
.workfunc
= rfapi_deferred_close_workfunc
;
4285 h
->deferred_close_q
->spec
.data
= h
;
4287 h
->rfp
= rfp_start(bm
->master
, &cfg
, &cbm
);
4288 bgp
->rfapi_cfg
= bgp_rfapi_cfg_new(cfg
);
4290 h
->rfp_methods
= *cbm
;
4295 void bgp_rfapi_destroy(struct bgp
*bgp
, struct rfapi
*h
)
4299 if (bgp
== NULL
|| h
== NULL
)
4302 if (h
->resolve_nve_nexthop
) {
4303 skiplist_free(h
->resolve_nve_nexthop
);
4304 h
->resolve_nve_nexthop
= NULL
;
4307 route_table_finish(h
->it_ce
->imported_vpn
[AFI_IP
]);
4308 route_table_finish(h
->it_ce
->imported_vpn
[AFI_IP6
]);
4309 route_table_finish(h
->it_ce
->imported_encap
[AFI_IP
]);
4310 route_table_finish(h
->it_ce
->imported_encap
[AFI_IP6
]);
4312 if (h
->import_mac
) {
4313 struct rfapi_import_table
*it
;
4318 rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4320 !rc
; rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4323 rfapiImportTableFlush(it
);
4324 XFREE(MTYPE_RFAPI_IMPORTTABLE
, it
);
4326 skiplist_free(h
->import_mac
);
4327 h
->import_mac
= NULL
;
4330 work_queue_free(h
->deferred_close_q
);
4335 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
4336 route_table_finish(h
->un
[afi
]);
4339 XFREE(MTYPE_RFAPI_IMPORTTABLE
, h
->it_ce
);
4340 XFREE(MTYPE_RFAPI
, h
);
4343 struct rfapi_import_table
*
4344 rfapiImportTableRefAdd(struct bgp
*bgp
, struct ecommunity
*rt_import_list
,
4345 struct rfapi_nve_group_cfg
*rfg
)
4348 struct rfapi_import_table
*it
;
4354 for (it
= h
->imports
; it
; it
= it
->next
) {
4355 if (ecommunity_cmp(it
->rt_import_list
, rt_import_list
))
4359 vnc_zlog_debug_verbose("%s: matched it=%p", __func__
, it
);
4362 it
= XCALLOC(MTYPE_RFAPI_IMPORTTABLE
,
4363 sizeof(struct rfapi_import_table
));
4365 it
->next
= h
->imports
;
4368 it
->rt_import_list
= ecommunity_dup(rt_import_list
);
4370 it
->monitor_exterior_orphans
=
4371 skiplist_new(0, NULL
, (void (*)(void *))prefix_free
);
4374 * fill import route tables from RIBs
4376 * Potential area for optimization. If this occurs when
4377 * tables are large (e.g., the operator adds a nve group
4378 * with a new RT list to a running system), it could take
4382 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
4384 it
->imported_vpn
[afi
] = route_table_init();
4385 it
->imported_encap
[afi
] = route_table_init();
4387 rfapiBgpTableFilteredImport(bgp
, it
, afi
,
4389 rfapiBgpTableFilteredImport(bgp
, it
, afi
, SAFI_ENCAP
);
4391 vnc_import_bgp_exterior_redist_enable_it(bgp
, afi
, it
);
4401 * skiplist element free function
4403 static void delete_rem_pfx_na_free(void *na
)
4405 uint32_t *pCounter
= ((struct rfapi_nve_addr
*)na
)->info
;
4408 XFREE(MTYPE_RFAPI_NVE_ADDR
, na
);
4412 * Common deleter for IP and MAC import tables
4414 static void rfapiDeleteRemotePrefixesIt(
4415 struct bgp
*bgp
, struct rfapi_import_table
*it
, struct prefix
*un
,
4416 struct prefix
*vn
, struct prefix
*p
, int delete_active
,
4417 int delete_holddown
, uint32_t *pARcount
, uint32_t *pAHcount
,
4418 uint32_t *pHRcount
, uint32_t *pHHcount
,
4419 struct skiplist
*uniq_active_nves
, struct skiplist
*uniq_holddown_nves
)
4425 char buf_pfx
[BUFSIZ
];
4428 prefix2str(p
, buf_pfx
, BUFSIZ
);
4434 vnc_zlog_debug_verbose(
4435 "%s: entry, p=%s, delete_active=%d, delete_holddown=%d",
4436 __func__
, buf_pfx
, delete_active
, delete_holddown
);
4440 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
4442 struct route_table
*rt
;
4443 struct route_node
*rn
;
4445 if (p
&& (family2afi(p
->family
) != afi
)) {
4449 rt
= it
->imported_vpn
[afi
];
4453 vnc_zlog_debug_verbose("%s: scanning rt for afi=%d", __func__
,
4456 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
)) {
4457 struct bgp_info
*bi
;
4458 struct bgp_info
*next
;
4460 if (VNC_DEBUG(IMPORT_DEL_REMOTE
)) {
4461 char p1line
[BUFSIZ
];
4462 char p2line
[BUFSIZ
];
4464 prefix2str(p
, p1line
, BUFSIZ
);
4465 prefix2str(&rn
->p
, p2line
, BUFSIZ
);
4466 vnc_zlog_debug_any("%s: want %s, have %s",
4467 __func__
, p1line
, p2line
);
4470 if (p
&& prefix_cmp(p
, &rn
->p
))
4474 char buf_pfx
[BUFSIZ
];
4475 prefix2str(&rn
->p
, buf_pfx
, BUFSIZ
);
4476 vnc_zlog_debug_verbose("%s: rn pfx=%s",
4480 /* TBD is this valid for afi == AFI_L2VPN? */
4481 RFAPI_CHECK_REFCOUNT(rn
, SAFI_MPLS_VPN
, 1);
4483 for (bi
= rn
->info
; bi
; bi
= next
) {
4492 vnc_zlog_debug_verbose("%s: examining bi %p",
4496 if (!rfapiGetNexthop(bi
->attr
, &qpt
))
4501 || !prefix_match(vn
, &qpt
)) {
4503 vnc_zlog_debug_verbose(
4504 "%s: continue at vn && !qpt_valid || !prefix_match(vn, &qpt)",
4511 if (!rfapiGetUnAddrOfVpnBi(bi
, &qct
))
4516 || !prefix_match(un
, &qct
)) {
4518 vnc_zlog_debug_verbose(
4519 "%s: continue at un && !qct_valid || !prefix_match(un, &qct)",
4531 * If this route is waiting to be deleted
4533 * a previous withdraw, we must cancel its
4536 if (CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
)) {
4537 if (!delete_holddown
)
4539 if (bi
->extra
->vnc
.import
.timer
) {
4545 struct rfapi_withdraw
*wcb
=
4549 ->holddown_count
[afi
] -=
4551 RFAPI_UPDATE_ITABLE_COUNT(
4552 bi
, wcb
->import_table
,
4554 XFREE(MTYPE_RFAPI_WITHDRAW
,
4564 vnc_zlog_debug_verbose(
4565 "%s: deleting bi %p (qct_valid=%d, qpt_valid=%d, delete_holddown=%d, delete_active=%d)",
4566 __func__
, bi
, qct_valid
, qpt_valid
,
4567 delete_holddown
, delete_active
);
4573 if (qct_valid
&& qpt_valid
) {
4575 struct rfapi_nve_addr na
;
4576 struct rfapi_nve_addr
*nap
;
4578 memset(&na
, 0, sizeof(na
));
4579 assert(!rfapiQprefix2Raddr(&qct
,
4581 assert(!rfapiQprefix2Raddr(&qpt
,
4584 if (skiplist_search(
4587 : uniq_holddown_nves
),
4588 &na
, (void **)&nap
)) {
4592 MTYPE_RFAPI_NVE_ADDR
,
4597 nap
->info
= is_active
4603 : uniq_holddown_nves
),
4606 rfapiNveAddr2Str(nap
, line
,
4611 vnc_direct_bgp_rh_del_route(bgp
, afi
, &rn
->p
,
4614 RFAPI_UPDATE_ITABLE_COUNT(bi
, it
, afi
, -1);
4615 it
->holddown_count
[afi
] += 1;
4616 rfapiExpireVpnNow(it
, rn
, bi
, 1);
4618 vnc_zlog_debug_verbose(
4619 "%s: incrementing count (is_active=%d)",
4620 __func__
, is_active
);
4633 * For use by the "clear vnc prefixes" command
4635 /*------------------------------------------
4636 * rfapiDeleteRemotePrefixes
4638 * UI helper: For use by the "clear vnc prefixes" command
4641 * un if set, tunnel must match this prefix
4642 * vn if set, nexthop prefix must match this prefix
4643 * p if set, prefix must match this prefix
4644 * it if set, only look in this import table
4647 * pARcount number of active routes deleted
4648 * pAHcount number of active nves deleted
4649 * pHRcount number of holddown routes deleted
4650 * pHHcount number of holddown nves deleted
4654 --------------------------------------------*/
4655 void rfapiDeleteRemotePrefixes(struct prefix
*un
, struct prefix
*vn
,
4657 struct rfapi_import_table
*arg_it
,
4658 int delete_active
, int delete_holddown
,
4659 uint32_t *pARcount
, uint32_t *pAHcount
,
4660 uint32_t *pHRcount
, uint32_t *pHHcount
)
4664 struct rfapi_import_table
*it
;
4665 uint32_t deleted_holddown_route_count
= 0;
4666 uint32_t deleted_active_route_count
= 0;
4667 uint32_t deleted_holddown_nve_count
= 0;
4668 uint32_t deleted_active_nve_count
= 0;
4669 struct skiplist
*uniq_holddown_nves
;
4670 struct skiplist
*uniq_active_nves
;
4674 bgp
= bgp_get_default(); /* assume 1 instance for now */
4675 /* If no bgp instantiated yet, no vnc prefixes exist */
4682 uniq_holddown_nves
=
4683 skiplist_new(0, rfapi_nve_addr_cmp
, delete_rem_pfx_na_free
);
4685 skiplist_new(0, rfapi_nve_addr_cmp
, delete_rem_pfx_na_free
);
4688 * Iterate over all import tables; do a filtered import
4689 * for the afi/safi combination
4698 vnc_zlog_debug_verbose(
4699 "%s: calling rfapiDeleteRemotePrefixesIt() on (IP) import %p",
4702 rfapiDeleteRemotePrefixesIt(
4703 bgp
, it
, un
, vn
, p
, delete_active
, delete_holddown
,
4704 &deleted_active_route_count
, &deleted_active_nve_count
,
4705 &deleted_holddown_route_count
,
4706 &deleted_holddown_nve_count
, uniq_active_nves
,
4707 uniq_holddown_nves
);
4716 * Now iterate over L2 import tables
4718 if (h
->import_mac
&& !(p
&& (p
->family
!= AF_ETHERNET
))) {
4720 void *cursor
= NULL
;
4724 rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4726 !rc
; rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4729 vnc_zlog_debug_verbose(
4730 "%s: calling rfapiDeleteRemotePrefixesIt() on import_mac %p",
4733 rfapiDeleteRemotePrefixesIt(
4734 bgp
, it
, un
, vn
, p
, delete_active
,
4735 delete_holddown
, &deleted_active_route_count
,
4736 &deleted_active_nve_count
,
4737 &deleted_holddown_route_count
,
4738 &deleted_holddown_nve_count
, uniq_active_nves
,
4739 uniq_holddown_nves
);
4744 * our custom element freeing function above counts as it deletes
4746 skiplist_free(uniq_holddown_nves
);
4747 skiplist_free(uniq_active_nves
);
4750 *pARcount
= deleted_active_route_count
;
4752 *pAHcount
= deleted_active_nve_count
;
4754 *pHRcount
= deleted_holddown_route_count
;
4756 *pHHcount
= deleted_holddown_nve_count
;
4761 /*------------------------------------------
4762 * rfapiCountRemoteRoutes
4764 * UI helper: count VRF routes from BGP side
4769 * pALRcount count of active local routes
4770 * pARRcount count of active remote routes
4771 * pHRcount count of holddown routes
4772 * pIRcount count of direct imported routes
4776 --------------------------------------------*/
4777 void rfapiCountAllItRoutes(int *pALRcount
, /* active local routes */
4778 int *pARRcount
, /* active remote routes */
4779 int *pHRcount
, /* holddown routes */
4780 int *pIRcount
) /* imported routes */
4784 struct rfapi_import_table
*it
;
4787 int total_active_local
= 0;
4788 int total_active_remote
= 0;
4789 int total_holddown
= 0;
4790 int total_imported
= 0;
4792 bgp
= bgp_get_default(); /* assume 1 instance for now */
4799 * Iterate over all import tables; do a filtered import
4800 * for the afi/safi combination
4803 for (it
= h
->imports
; it
; it
= it
->next
) {
4805 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
4807 total_active_local
+= it
->local_count
[afi
];
4808 total_active_remote
+= it
->remote_count
[afi
];
4809 total_holddown
+= it
->holddown_count
[afi
];
4810 total_imported
+= it
->imported_count
[afi
];
4817 if (h
->import_mac
) {
4819 rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4821 !rc
; rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4824 total_active_local
+= it
->local_count
[AFI_L2VPN
];
4825 total_active_remote
+= it
->remote_count
[AFI_L2VPN
];
4826 total_holddown
+= it
->holddown_count
[AFI_L2VPN
];
4827 total_imported
+= it
->imported_count
[AFI_L2VPN
];
4833 *pALRcount
= total_active_local
;
4836 *pARRcount
= total_active_remote
;
4839 *pHRcount
= total_holddown
;
4842 *pIRcount
= total_imported
;
4846 /*------------------------------------------
4847 * rfapiGetHolddownFromLifetime
4849 * calculate holddown value based on lifetime
4855 * Holddown value based on lifetime, holddown_factor,
4856 * and RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY
4858 --------------------------------------------*/
4859 /* hold down time maxes out at RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY */
4860 uint32_t rfapiGetHolddownFromLifetime(uint32_t lifetime
)
4865 bgp
= bgp_get_default();
4866 if (bgp
&& bgp
->rfapi_cfg
)
4867 factor
= bgp
->rfapi_cfg
->rfp_cfg
.holddown_factor
;
4869 factor
= RFAPI_RFP_CFG_DEFAULT_HOLDDOWN_FACTOR
;
4871 if (factor
< 100 || lifetime
< RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY
)
4872 lifetime
= lifetime
* factor
/ 100;
4873 if (lifetime
< RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY
)
4876 return RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY
;