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
26 #include "lib/zebra.h"
27 #include "lib/prefix.h"
28 #include "lib/agg_table.h"
30 #include "lib/memory.h"
32 #include "lib/skiplist.h"
33 #include "lib/thread.h"
34 #include "lib/stream.h"
35 #include "lib/lib_errors.h"
37 #include "bgpd/bgpd.h"
38 #include "bgpd/bgp_ecommunity.h"
39 #include "bgpd/bgp_attr.h"
40 #include "bgpd/bgp_route.h"
41 #include "bgpd/bgp_mplsvpn.h" /* prefix_rd2str() */
42 #include "bgpd/bgp_vnc_types.h"
43 #include "bgpd/bgp_rd.h"
45 #include "bgpd/rfapi/rfapi.h"
46 #include "bgpd/rfapi/bgp_rfapi_cfg.h"
47 #include "bgpd/rfapi/rfapi_backend.h"
48 #include "bgpd/rfapi/rfapi_import.h"
49 #include "bgpd/rfapi/rfapi_private.h"
50 #include "bgpd/rfapi/rfapi_monitor.h"
51 #include "bgpd/rfapi/rfapi_nve_addr.h"
52 #include "bgpd/rfapi/rfapi_vty.h"
53 #include "bgpd/rfapi/vnc_export_bgp.h"
54 #include "bgpd/rfapi/vnc_export_bgp_p.h"
55 #include "bgpd/rfapi/vnc_zebra.h"
56 #include "bgpd/rfapi/vnc_import_bgp.h"
57 #include "bgpd/rfapi/vnc_import_bgp_p.h"
58 #include "bgpd/rfapi/rfapi_rib.h"
59 #include "bgpd/rfapi/rfapi_encap_tlv.h"
60 #include "bgpd/rfapi/vnc_debug.h"
62 #ifdef HAVE_GLIBC_BACKTRACE
63 /* for backtrace and friends */
65 #endif /* HAVE_GLIBC_BACKTRACE */
67 #undef DEBUG_MONITOR_MOVE_SHORTER
68 #undef DEBUG_RETURNED_NHL
69 #undef DEBUG_ROUTE_COUNTERS
70 #undef DEBUG_ENCAP_MONITOR
73 #undef DEBUG_BI_SEARCH
76 * Allocated for each withdraw timer instance; freed when the timer
77 * expires or is canceled
79 struct rfapi_withdraw
{
80 struct rfapi_import_table
*import_table
;
81 struct agg_node
*node
;
82 struct bgp_info
*info
;
83 safi_t safi
; /* used only for bulk operations */
85 * For import table node reference count checking (i.e., debugging).
86 * Normally when a timer expires, lockoffset should be 0. However, if
87 * the timer expiration function is called directly (e.g.,
88 * rfapiExpireVpnNow), the node could be locked by a preceding
89 * agg_route_top() or agg_route_next() in a loop, so we need to pass
97 * It's evil and fiendish. It's compiler-dependent.
98 * ? Might need LDFLAGS -rdynamic to produce all function names
100 void rfapiDebugBacktrace(void)
102 #ifdef HAVE_GLIBC_BACKTRACE
103 #define RFAPI_DEBUG_BACKTRACE_NENTRIES 200
104 void *buf
[RFAPI_DEBUG_BACKTRACE_NENTRIES
];
109 size
= backtrace(buf
, RFAPI_DEBUG_BACKTRACE_NENTRIES
);
110 syms
= backtrace_symbols(buf
, size
);
112 for (i
= 0; i
< size
&& i
< RFAPI_DEBUG_BACKTRACE_NENTRIES
; ++i
) {
113 vnc_zlog_debug_verbose("backtrace[%2zu]: %s", i
, syms
[i
]);
123 * Count remote routes and compare with actively-maintained values.
124 * Abort if they disagree.
126 void rfapiCheckRouteCount()
128 struct bgp
*bgp
= bgp_get_default();
130 struct rfapi_import_table
*it
;
138 for (it
= h
->imports
; it
; it
= it
->next
) {
139 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
141 struct agg_table
*rt
;
144 int holddown_count
= 0;
146 int imported_count
= 0;
147 int remote_count
= 0;
149 rt
= it
->imported_vpn
[afi
];
151 for (rn
= agg_route_top(rt
); rn
;
152 rn
= agg_route_next(rn
)) {
154 struct bgp_info
*next
;
156 for (bi
= rn
->info
; bi
; bi
= next
) {
159 if (CHECK_FLAG(bi
->flags
,
164 if (RFAPI_LOCAL_BI(bi
)) {
167 if (RFAPI_DIRECT_IMPORT_BI(
178 if (it
->holddown_count
[afi
] != holddown_count
) {
179 vnc_zlog_debug_verbose(
180 "%s: it->holddown_count %d != holddown_count %d",
181 __func__
, it
->holddown_count
[afi
],
185 if (it
->remote_count
[afi
] != remote_count
) {
186 vnc_zlog_debug_verbose(
187 "%s: it->remote_count %d != remote_count %d",
188 __func__
, it
->remote_count
[afi
],
192 if (it
->imported_count
[afi
] != imported_count
) {
193 vnc_zlog_debug_verbose(
194 "%s: it->imported_count %d != imported_count %d",
195 __func__
, it
->imported_count
[afi
],
203 #if DEBUG_ROUTE_COUNTERS
204 #define VNC_ITRCCK do {rfapiCheckRouteCount();} while (0)
210 * Validate reference count for a node in an import table
212 * Normally lockoffset is 0 for nodes in quiescent state. However,
213 * agg_unlock_node will delete the node if it is called when
214 * node->lock == 1, and we have to validate the refcount before
215 * the node is deleted. In this case, we specify lockoffset 1.
217 void rfapiCheckRefcount(struct agg_node
*rn
, safi_t safi
, int lockoffset
)
219 unsigned int count_bi
= 0;
220 unsigned int count_monitor
= 0;
222 struct rfapi_monitor_encap
*hme
;
223 struct rfapi_monitor_vpn
*hmv
;
225 for (bi
= rn
->info
; bi
; bi
= bi
->next
)
230 ++count_monitor
; /* rfapi_it_extra */
237 for (hme
= RFAPI_MONITOR_ENCAP(rn
); hme
;
244 for (hmv
= RFAPI_MONITOR_VPN(rn
); hmv
; hmv
= hmv
->next
)
247 if (RFAPI_MONITOR_EXTERIOR(rn
)->source
) {
248 ++count_monitor
; /* sl */
250 for (rc
= skiplist_next(
251 RFAPI_MONITOR_EXTERIOR(rn
)->source
,
252 NULL
, NULL
, &cursor
);
255 RFAPI_MONITOR_EXTERIOR(rn
)->source
,
256 NULL
, NULL
, &cursor
)) {
258 ++count_monitor
; /* sl entry */
268 if (count_bi
+ count_monitor
+ lockoffset
!= rn
->lock
) {
269 vnc_zlog_debug_verbose(
270 "%s: count_bi=%d, count_monitor=%d, lockoffset=%d, rn->lock=%d",
271 __func__
, count_bi
, count_monitor
, lockoffset
,
278 * Perform deferred rfapi_close operations that were queued
281 static wq_item_status
rfapi_deferred_close_workfunc(struct work_queue
*q
,
284 struct rfapi_descriptor
*rfd
= data
;
285 struct rfapi
*h
= q
->spec
.data
;
287 assert(!(h
->flags
& RFAPI_INCALLBACK
));
289 vnc_zlog_debug_verbose("%s: completed deferred close on handle %p",
295 * Extract layer 2 option from Encap TLVS in BGP attrs
297 int rfapiGetL2o(struct attr
*attr
, struct rfapi_l2address_option
*l2o
)
301 struct bgp_attr_encap_subtlv
*pEncap
;
303 for (pEncap
= attr
->vnc_subtlvs
; pEncap
;
304 pEncap
= pEncap
->next
) {
306 if (pEncap
->type
== BGP_VNC_SUBTLV_TYPE_RFPOPTION
) {
308 == RFAPI_VN_OPTION_TYPE_L2ADDR
) {
310 if (pEncap
->value
[1] == 14) {
311 memcpy(l2o
->macaddr
.octet
,
328 l2o
->logical_net_id
=
331 + ((pEncap
->value
[14]
334 + ((pEncap
->value
[13]
349 * Extract the lifetime from the Tunnel Encap attribute of a route in
352 int rfapiGetVncLifetime(struct attr
*attr
, uint32_t *lifetime
)
354 struct bgp_attr_encap_subtlv
*pEncap
;
356 *lifetime
= RFAPI_INFINITE_LIFETIME
; /* default to infinite */
360 for (pEncap
= attr
->vnc_subtlvs
; pEncap
;
361 pEncap
= pEncap
->next
) {
364 == BGP_VNC_SUBTLV_TYPE_LIFETIME
) { /* lifetime */
365 if (pEncap
->length
== 4) {
366 memcpy(lifetime
, pEncap
->value
, 4);
367 *lifetime
= ntohl(*lifetime
);
378 * Extract the tunnel type from the extended community
380 int rfapiGetTunnelType(struct attr
*attr
, bgp_encap_types
*type
)
382 *type
= BGP_ENCAP_TYPE_MPLS
; /* default to MPLS */
383 if (attr
&& attr
->ecommunity
) {
384 struct ecommunity
*ecom
= attr
->ecommunity
;
387 for (i
= 0; i
< (ecom
->size
* ECOMMUNITY_SIZE
);
388 i
+= ECOMMUNITY_SIZE
) {
392 if (ep
[0] == ECOMMUNITY_ENCODE_OPAQUE
393 && ep
[1] == ECOMMUNITY_OPAQUE_SUBTYPE_ENCAP
) {
394 *type
= (ep
[6] << 8) + ep
[7];
405 * Look for UN address in Encap attribute
407 int rfapiGetVncTunnelUnAddr(struct attr
*attr
, struct prefix
*p
)
409 struct bgp_attr_encap_subtlv
*pEncap
;
410 bgp_encap_types tun_type
;
412 rfapiGetTunnelType(attr
, &tun_type
);
413 if (tun_type
== BGP_ENCAP_TYPE_MPLS
) {
416 /* MPLS carries UN address in next hop */
417 rfapiNexthop2Prefix(attr
, p
);
424 for (pEncap
= attr
->encap_subtlvs
; pEncap
;
425 pEncap
= pEncap
->next
) {
428 == BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT
) { /* un
431 switch (pEncap
->length
) {
436 memcpy(p
->u
.val
, pEncap
->value
,
443 p
->family
= AF_INET6
;
445 memcpy(p
->u
.val
, pEncap
->value
,
458 * Get UN address wherever it might be
460 int rfapiGetUnAddrOfVpnBi(struct bgp_info
*bi
, struct prefix
*p
)
462 /* If it's in this route's VNC attribute, we're done */
463 if (!rfapiGetVncTunnelUnAddr(bi
->attr
, p
))
466 * Otherwise, see if it's cached from a corresponding ENCAP SAFI
470 switch (bi
->extra
->vnc
.import
.un_family
) {
473 p
->family
= bi
->extra
->vnc
.import
.un_family
;
474 p
->u
.prefix4
= bi
->extra
->vnc
.import
.un
.addr4
;
480 p
->family
= bi
->extra
->vnc
.import
.un_family
;
481 p
->u
.prefix6
= bi
->extra
->vnc
.import
.un
.addr6
;
488 #if DEBUG_ENCAP_MONITOR
489 vnc_zlog_debug_verbose(
490 "%s: bi->extra->vnc.import.un_family is 0, no UN addr",
502 * Make a new bgp_info from gathered parameters
504 static struct bgp_info
*rfapiBgpInfoCreate(struct attr
*attr
, struct peer
*peer
,
505 void *rfd
, struct prefix_rd
*prd
,
506 uint8_t type
, uint8_t sub_type
,
509 struct bgp_info
*new;
511 new = bgp_info_new();
516 new->attr
= bgp_attr_intern(attr
);
518 bgp_info_extra_get(new);
520 new->extra
->vnc
.import
.rd
= *prd
;
521 rfapi_time(&new->extra
->vnc
.import
.create_time
);
524 encode_label(*label
, &new->extra
->label
[0]);
526 new->sub_type
= sub_type
;
534 * Frees bgp_info as used in import tables (parts are not
535 * allocated exactly the way they are in the main RIBs)
537 static void rfapiBgpInfoFree(struct bgp_info
*goner
)
543 vnc_zlog_debug_verbose("%s: calling peer_unlock(%p), #%d",
544 __func__
, goner
->peer
,
546 peer_unlock(goner
->peer
);
550 bgp_attr_unintern(&goner
->attr
);
553 assert(!goner
->extra
->damp_info
); /* Not used in import tbls */
554 XFREE(MTYPE_BGP_ROUTE_EXTRA
, goner
->extra
);
557 XFREE(MTYPE_BGP_ROUTE
, goner
);
560 struct rfapi_import_table
*rfapiMacImportTableGetNoAlloc(struct bgp
*bgp
,
564 struct rfapi_import_table
*it
= NULL
;
565 uintptr_t lni_as_ptr
= lni
;
574 if (skiplist_search(h
->import_mac
, (void *)lni_as_ptr
, (void **)&it
))
580 struct rfapi_import_table
*rfapiMacImportTableGet(struct bgp
*bgp
, uint32_t lni
)
583 struct rfapi_import_table
*it
= NULL
;
584 uintptr_t lni_as_ptr
= lni
;
589 if (!h
->import_mac
) {
590 /* default cmp is good enough for LNI */
591 h
->import_mac
= skiplist_new(0, NULL
, NULL
);
594 if (skiplist_search(h
->import_mac
, (void *)lni_as_ptr
, (void **)&it
)) {
596 struct ecommunity
*enew
;
597 struct ecommunity_val eval
;
600 it
= XCALLOC(MTYPE_RFAPI_IMPORTTABLE
,
601 sizeof(struct rfapi_import_table
));
602 /* set RT list of new import table based on LNI */
603 memset((char *)&eval
, 0, sizeof(eval
));
604 eval
.val
[0] = 0; /* VNC L2VPN */
605 eval
.val
[1] = 2; /* VNC L2VPN */
606 eval
.val
[5] = (lni
>> 16) & 0xff;
607 eval
.val
[6] = (lni
>> 8) & 0xff;
608 eval
.val
[7] = (lni
>> 0) & 0xff;
610 enew
= ecommunity_new();
611 ecommunity_add_val(enew
, &eval
);
612 it
->rt_import_list
= enew
;
614 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
615 it
->imported_vpn
[afi
] = agg_table_init();
616 it
->imported_encap
[afi
] = agg_table_init();
619 it
->l2_logical_net_id
= lni
;
621 skiplist_insert(h
->import_mac
, (void *)lni_as_ptr
, it
);
629 * Implement MONITOR_MOVE_SHORTER(original_node) from
630 * RFAPI-Import-Event-Handling.txt
632 * Returns pointer to the list of moved monitors
634 static struct rfapi_monitor_vpn
*
635 rfapiMonitorMoveShorter(struct agg_node
*original_vpn_node
, int lockoffset
)
638 struct agg_node
*par
;
639 struct rfapi_monitor_vpn
*m
;
640 struct rfapi_monitor_vpn
*mlast
;
641 struct rfapi_monitor_vpn
*moved
;
643 int parent_already_refcounted
= 0;
645 RFAPI_CHECK_REFCOUNT(original_vpn_node
, SAFI_MPLS_VPN
, lockoffset
);
647 #if DEBUG_MONITOR_MOVE_SHORTER
649 char buf
[PREFIX_STRLEN
];
651 prefix2str(&original_vpn_node
->p
, buf
, sizeof(buf
));
652 vnc_zlog_debug_verbose("%s: called with node pfx=%s", __func__
,
658 * 1. If there is at least one bi (either regular route or
659 * route marked as withdrawn, with a pending timer) at
660 * original_node with a valid UN address, we're done. Return.
662 for (bi
= original_vpn_node
->info
; bi
; bi
= bi
->next
) {
665 if (!rfapiGetUnAddrOfVpnBi(bi
, &pfx
)) {
666 #if DEBUG_MONITOR_MOVE_SHORTER
667 vnc_zlog_debug_verbose(
668 "%s: have valid UN at original node, no change",
676 * 2. Travel up the tree (toward less-specific prefixes) from
677 * original_node to find the first node that has at least
678 * one route (even if it is only a withdrawn route) with a
679 * valid UN address. Call this node "Node P."
681 for (par
= agg_node_parent(original_vpn_node
); par
;
682 par
= agg_node_parent(par
)) {
683 for (bi
= par
->info
; bi
; bi
= bi
->next
) {
685 if (!rfapiGetUnAddrOfVpnBi(bi
, &pfx
)) {
694 RFAPI_CHECK_REFCOUNT(par
, SAFI_MPLS_VPN
, 0);
698 * If no less-specific routes, try to use the 0/0 node
701 /* this isn't necessarily 0/0 */
702 par
= agg_route_table_top(original_vpn_node
);
705 * If we got the top node but it wasn't 0/0,
708 if (par
&& par
->p
.prefixlen
) {
709 agg_unlock_node(par
); /* maybe free */
714 ++parent_already_refcounted
;
719 * Create 0/0 node if it isn't there
722 struct prefix pfx_default
;
724 memset(&pfx_default
, 0, sizeof(pfx_default
));
725 pfx_default
.family
= original_vpn_node
->p
.family
;
727 /* creates default node if none exists */
728 par
= agg_node_get(agg_get_table(original_vpn_node
),
730 ++parent_already_refcounted
;
734 * 3. Move each of the monitors found at original_node to Node P.
735 * These are "Moved Monitors."
740 * Attach at end so that the list pointer we return points
741 * only to the moved routes
743 for (m
= RFAPI_MONITOR_VPN(par
), mlast
= NULL
; m
;
744 mlast
= m
, m
= m
->next
)
748 moved
= mlast
->next
= RFAPI_MONITOR_VPN(original_vpn_node
);
750 moved
= RFAPI_MONITOR_VPN_W_ALLOC(par
) =
751 RFAPI_MONITOR_VPN(original_vpn_node
);
753 if (RFAPI_MONITOR_VPN(
754 original_vpn_node
)) /* check agg, so not allocated */
755 RFAPI_MONITOR_VPN_W_ALLOC(original_vpn_node
) = NULL
;
758 * update the node pointers on the monitors
760 for (m
= moved
; m
; m
= m
->next
) {
765 RFAPI_CHECK_REFCOUNT(par
, SAFI_MPLS_VPN
,
766 parent_already_refcounted
- movecount
);
767 while (movecount
> parent_already_refcounted
) {
769 ++parent_already_refcounted
;
771 while (movecount
< parent_already_refcounted
) {
772 /* unlikely, but code defensively */
773 agg_unlock_node(par
);
774 --parent_already_refcounted
;
776 RFAPI_CHECK_REFCOUNT(original_vpn_node
, SAFI_MPLS_VPN
,
777 movecount
+ lockoffset
);
778 while (movecount
--) {
779 agg_unlock_node(original_vpn_node
);
782 #if DEBUG_MONITOR_MOVE_SHORTER
784 char buf
[PREFIX_STRLEN
];
786 prefix2str(&par
->p
, buf
, sizeof(buf
));
787 vnc_zlog_debug_verbose("%s: moved to node pfx=%s", __func__
,
797 * Implement MONITOR_MOVE_LONGER(new_node) from
798 * RFAPI-Import-Event-Handling.txt
800 static void rfapiMonitorMoveLonger(struct agg_node
*new_vpn_node
)
802 struct rfapi_monitor_vpn
*monitor
;
803 struct rfapi_monitor_vpn
*mlast
;
805 struct agg_node
*par
;
807 RFAPI_CHECK_REFCOUNT(new_vpn_node
, SAFI_MPLS_VPN
, 0);
810 * Make sure we have at least one valid route at the new node
812 for (bi
= new_vpn_node
->info
; bi
; bi
= bi
->next
) {
814 if (!rfapiGetUnAddrOfVpnBi(bi
, &pfx
))
819 vnc_zlog_debug_verbose(
820 "%s: no valid routes at node %p, so not attempting moves",
821 __func__
, new_vpn_node
);
826 * Find first parent node that has monitors
828 for (par
= agg_node_parent(new_vpn_node
); par
;
829 par
= agg_node_parent(par
)) {
830 if (RFAPI_MONITOR_VPN(par
))
835 vnc_zlog_debug_verbose(
836 "%s: no parent nodes with monitors, done", __func__
);
841 * Check each of these monitors to see of their longest-match
842 * is now the updated node. Move any such monitors to the more-
843 * specific updated node
845 for (mlast
= NULL
, monitor
= RFAPI_MONITOR_VPN(par
); monitor
;) {
848 * If new longest match for monitor prefix is the new
849 * route's prefix, move monitor to new route's prefix
851 if (prefix_match(&new_vpn_node
->p
, &monitor
->p
)) {
854 mlast
->next
= monitor
->next
;
856 RFAPI_MONITOR_VPN_W_ALLOC(par
) = monitor
->next
;
861 monitor
->next
= RFAPI_MONITOR_VPN(new_vpn_node
);
862 RFAPI_MONITOR_VPN_W_ALLOC(new_vpn_node
) = monitor
;
863 monitor
->node
= new_vpn_node
;
865 agg_lock_node(new_vpn_node
); /* incr refcount */
867 monitor
= mlast
? mlast
->next
: RFAPI_MONITOR_VPN(par
);
869 RFAPI_CHECK_REFCOUNT(par
, SAFI_MPLS_VPN
, 1);
870 /* decr refcount after we're done with par as this might
872 agg_unlock_node(par
);
877 monitor
= monitor
->next
;
880 RFAPI_CHECK_REFCOUNT(new_vpn_node
, SAFI_MPLS_VPN
, 0);
884 static void rfapiBgpInfoChainFree(struct bgp_info
*bi
)
886 struct bgp_info
*next
;
891 * If there is a timer waiting to delete this bi, cancel
892 * the timer and delete immediately
894 if (CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
)
895 && bi
->extra
->vnc
.import
.timer
) {
898 (struct thread
*)bi
->extra
->vnc
.import
.timer
;
899 struct rfapi_withdraw
*wcb
= t
->arg
;
901 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
907 rfapiBgpInfoFree(bi
);
912 static void rfapiImportTableFlush(struct rfapi_import_table
*it
)
919 ecommunity_free(&it
->rt_import_list
);
920 it
->rt_import_list
= NULL
;
922 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
926 for (rn
= agg_route_top(it
->imported_vpn
[afi
]); rn
;
927 rn
= agg_route_next(rn
)) {
929 * Each route_node has:
930 * aggregate: points to rfapi_it_extra with monitor
932 * info: points to chain of bgp_info
934 /* free bgp_info and its children */
935 rfapiBgpInfoChainFree(rn
->info
);
938 rfapiMonitorExtraFlush(SAFI_MPLS_VPN
, rn
);
941 for (rn
= agg_route_top(it
->imported_encap
[afi
]); rn
;
942 rn
= agg_route_next(rn
)) {
943 /* free bgp_info and its children */
944 rfapiBgpInfoChainFree(rn
->info
);
947 rfapiMonitorExtraFlush(SAFI_ENCAP
, rn
);
950 agg_table_finish(it
->imported_vpn
[afi
]);
951 agg_table_finish(it
->imported_encap
[afi
]);
953 if (it
->monitor_exterior_orphans
) {
954 skiplist_free(it
->monitor_exterior_orphans
);
958 void rfapiImportTableRefDelByIt(struct bgp
*bgp
,
959 struct rfapi_import_table
*it_target
)
962 struct rfapi_import_table
*it
;
963 struct rfapi_import_table
*prev
= NULL
;
970 for (it
= h
->imports
; it
; prev
= it
, it
= it
->next
) {
976 assert(it
->refcount
);
982 prev
->next
= it
->next
;
984 h
->imports
= it
->next
;
986 rfapiImportTableFlush(it
);
987 XFREE(MTYPE_RFAPI_IMPORTTABLE
, it
);
991 #if RFAPI_REQUIRE_ENCAP_BEEC
993 * Look for magic BGP Encapsulation Extended Community value
994 * Format in RFC 5512 Sect. 4.5
996 static int rfapiEcommunitiesMatchBeec(struct ecommunity
*ecom
,
997 bgp_encap_types type
)
1004 for (i
= 0; i
< (ecom
->size
* ECOMMUNITY_SIZE
); i
+= ECOMMUNITY_SIZE
) {
1010 if (ep
[0] == ECOMMUNITY_ENCODE_OPAQUE
1011 && ep
[1] == ECOMMUNITY_OPAQUE_SUBTYPE_ENCAP
1012 && ep
[6] == ((type
&& 0xff00) >> 8)
1013 && ep
[7] == (type
& 0xff)) {
1022 int rfapiEcommunitiesIntersect(struct ecommunity
*e1
, struct ecommunity
*e2
)
1031 s1
= ecommunity_ecom2str(e1
, ECOMMUNITY_FORMAT_DISPLAY
, 0);
1032 s2
= ecommunity_ecom2str(e2
, ECOMMUNITY_FORMAT_DISPLAY
, 0);
1033 vnc_zlog_debug_verbose("%s: e1[%s], e2[%s]", __func__
, s1
, s2
);
1034 XFREE(MTYPE_ECOMMUNITY_STR
, s1
);
1035 XFREE(MTYPE_ECOMMUNITY_STR
, s2
);
1038 for (i
= 0; i
< e1
->size
; ++i
) {
1039 for (j
= 0; j
< e2
->size
; ++j
) {
1040 if (!memcmp(e1
->val
+ (i
* ECOMMUNITY_SIZE
),
1041 e2
->val
+ (j
* ECOMMUNITY_SIZE
),
1051 int rfapiEcommunityGetLNI(struct ecommunity
*ecom
, uint32_t *lni
)
1055 for (i
= 0; i
< ecom
->size
; ++i
) {
1056 uint8_t *p
= ecom
->val
+ (i
* ECOMMUNITY_SIZE
);
1058 if ((*(p
+ 0) == 0x00) && (*(p
+ 1) == 0x02)) {
1060 *lni
= (*(p
+ 5) << 16) | (*(p
+ 6) << 8)
1069 int rfapiEcommunityGetEthernetTag(struct ecommunity
*ecom
, uint16_t *tag_id
)
1071 struct bgp
*bgp
= bgp_get_default();
1072 *tag_id
= 0; /* default to untagged */
1075 for (i
= 0; i
< ecom
->size
; ++i
) {
1078 uint8_t *p
= ecom
->val
+ (i
* ECOMMUNITY_SIZE
);
1080 /* High-order octet of type. */
1083 if (*p
++ == ECOMMUNITY_ROUTE_TARGET
) {
1084 if (encode
== ECOMMUNITY_ENCODE_AS4
) {
1085 p
= ptr_get_be32(p
, &as
);
1086 } else if (encode
== ECOMMUNITY_ENCODE_AS
) {
1089 p
+= 2; /* skip next two, tag/vid
1090 always in lowest bytes */
1092 if (as
== bgp
->as
) {
1093 *tag_id
= *p
++ << 8;
1103 static int rfapiVpnBiNhEqualsPt(struct bgp_info
*bi
, struct rfapi_ip_addr
*hpt
)
1110 family
= BGP_MP_NEXTHOP_FAMILY(bi
->attr
->mp_nexthop_len
);
1112 if (hpt
->addr_family
!= family
)
1117 if (bi
->attr
->mp_nexthop_global_in
.s_addr
1118 != hpt
->addr
.v4
.s_addr
)
1123 if (IPV6_ADDR_CMP(&bi
->attr
->mp_nexthop_global
, &hpt
->addr
.v6
))
1137 * Compare 2 VPN BIs. Return true if they have the same VN and UN addresses
1139 static int rfapiVpnBiSamePtUn(struct bgp_info
*bi1
, struct bgp_info
*bi2
)
1141 struct prefix pfx_un1
;
1142 struct prefix pfx_un2
;
1147 if (!bi1
->attr
|| !bi2
->attr
)
1151 * VN address comparisons
1154 if (BGP_MP_NEXTHOP_FAMILY(bi1
->attr
->mp_nexthop_len
)
1155 != BGP_MP_NEXTHOP_FAMILY(bi2
->attr
->mp_nexthop_len
)) {
1159 switch (BGP_MP_NEXTHOP_FAMILY(bi1
->attr
->mp_nexthop_len
)) {
1161 if (bi1
->attr
->mp_nexthop_global_in
.s_addr
1162 != bi2
->attr
->mp_nexthop_global_in
.s_addr
)
1167 if (IPV6_ADDR_CMP(&bi1
->attr
->mp_nexthop_global
,
1168 &bi2
->attr
->mp_nexthop_global
))
1178 * UN address comparisons
1180 if (rfapiGetVncTunnelUnAddr(bi1
->attr
, &pfx_un1
)) {
1182 pfx_un1
.family
= bi1
->extra
->vnc
.import
.un_family
;
1183 switch (bi1
->extra
->vnc
.import
.un_family
) {
1186 bi1
->extra
->vnc
.import
.un
.addr4
;
1190 bi1
->extra
->vnc
.import
.un
.addr6
;
1199 if (rfapiGetVncTunnelUnAddr(bi2
->attr
, &pfx_un2
)) {
1201 pfx_un2
.family
= bi2
->extra
->vnc
.import
.un_family
;
1202 switch (bi2
->extra
->vnc
.import
.un_family
) {
1205 bi2
->extra
->vnc
.import
.un
.addr4
;
1209 bi2
->extra
->vnc
.import
.un
.addr6
;
1218 if (!pfx_un1
.family
|| !pfx_un2
.family
)
1221 if (pfx_un1
.family
!= pfx_un2
.family
)
1224 switch (pfx_un1
.family
) {
1226 if (!IPV4_ADDR_SAME(&pfx_un1
.u
.prefix4
, &pfx_un2
.u
.prefix4
))
1230 if (!IPV6_ADDR_SAME(&pfx_un1
.u
.prefix6
, &pfx_un2
.u
.prefix6
))
1239 uint8_t rfapiRfpCost(struct attr
*attr
)
1241 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)) {
1242 if (attr
->local_pref
> 255) {
1245 return 255 - attr
->local_pref
;
1251 /*------------------------------------------
1254 * Find Layer 2 options in an option chain
1260 * l2o layer 2 options extracted
1264 * 1 no options found
1266 --------------------------------------------*/
1267 int rfapi_extract_l2o(
1268 struct bgp_tea_options
*pHop
, /* chain of options */
1269 struct rfapi_l2address_option
*l2o
) /* return extracted value */
1271 struct bgp_tea_options
*p
;
1273 for (p
= pHop
; p
; p
= p
->next
) {
1274 if ((p
->type
== RFAPI_VN_OPTION_TYPE_L2ADDR
)
1275 && (p
->length
>= 8)) {
1279 memcpy(&l2o
->macaddr
, v
, 6);
1281 l2o
->label
= ((v
[6] << 12) & 0xff000)
1282 + ((v
[7] << 4) & 0xff0)
1283 + ((v
[8] >> 4) & 0xf);
1285 l2o
->local_nve_id
= (uint8_t)v
[10];
1287 l2o
->logical_net_id
=
1288 (v
[11] << 16) + (v
[12] << 8) + (v
[13] << 0);
1296 static struct rfapi_next_hop_entry
*
1297 rfapiRouteInfo2NextHopEntry(struct rfapi_ip_prefix
*rprefix
,
1298 struct bgp_info
*bi
, /* route to encode */
1299 uint32_t lifetime
, /* use this in nhe */
1300 struct agg_node
*rn
) /* req for L2 eth addr */
1302 struct rfapi_next_hop_entry
*new;
1303 int have_vnc_tunnel_un
= 0;
1305 #if DEBUG_ENCAP_MONITOR
1306 vnc_zlog_debug_verbose("%s: entry, bi %p, rn %p", __func__
, bi
, rn
);
1309 new = XCALLOC(MTYPE_RFAPI_NEXTHOP
, sizeof(struct rfapi_next_hop_entry
));
1312 new->prefix
= *rprefix
;
1315 && decode_rd_type(bi
->extra
->vnc
.import
.rd
.val
)
1316 == RD_TYPE_VNC_ETH
) {
1319 struct rfapi_vn_option
*vo
;
1321 vo
= XCALLOC(MTYPE_RFAPI_VN_OPTION
,
1322 sizeof(struct rfapi_vn_option
));
1325 vo
->type
= RFAPI_VN_OPTION_TYPE_L2ADDR
;
1327 memcpy(&vo
->v
.l2addr
.macaddr
, &rn
->p
.u
.prefix_eth
.octet
,
1329 /* only low 3 bytes of this are significant */
1331 (void)rfapiEcommunityGetLNI(
1332 bi
->attr
->ecommunity
,
1333 &vo
->v
.l2addr
.logical_net_id
);
1334 (void)rfapiEcommunityGetEthernetTag(
1335 bi
->attr
->ecommunity
, &vo
->v
.l2addr
.tag_id
);
1338 /* local_nve_id comes from lower byte of RD type */
1339 vo
->v
.l2addr
.local_nve_id
= bi
->extra
->vnc
.import
.rd
.val
[1];
1341 /* label comes from MP_REACH_NLRI label */
1342 vo
->v
.l2addr
.label
= decode_label(&bi
->extra
->label
[0]);
1344 new->vn_options
= vo
;
1347 * If there is an auxiliary prefix (i.e., host IP address),
1348 * use it as the nexthop prefix instead of the query prefix
1350 if (bi
->extra
->vnc
.import
.aux_prefix
.family
) {
1351 rfapiQprefix2Rprefix(&bi
->extra
->vnc
.import
.aux_prefix
,
1357 bgp_encap_types tun_type
;
1358 new->prefix
.cost
= rfapiRfpCost(bi
->attr
);
1360 struct bgp_attr_encap_subtlv
*pEncap
;
1362 switch (BGP_MP_NEXTHOP_FAMILY(bi
->attr
->mp_nexthop_len
)) {
1364 new->vn_address
.addr_family
= AF_INET
;
1365 new->vn_address
.addr
.v4
=
1366 bi
->attr
->mp_nexthop_global_in
;
1370 new->vn_address
.addr_family
= AF_INET6
;
1371 new->vn_address
.addr
.v6
= bi
->attr
->mp_nexthop_global
;
1375 zlog_warn("%s: invalid vpn nexthop length: %d",
1376 __func__
, bi
->attr
->mp_nexthop_len
);
1377 rfapi_free_next_hop_list(new);
1381 for (pEncap
= bi
->attr
->vnc_subtlvs
; pEncap
;
1382 pEncap
= pEncap
->next
) {
1383 switch (pEncap
->type
) {
1384 case BGP_VNC_SUBTLV_TYPE_LIFETIME
:
1385 /* use configured lifetime, not attr lifetime */
1389 zlog_warn("%s: unknown VNC option type %d",
1390 __func__
, pEncap
->type
);
1397 rfapiGetTunnelType(bi
->attr
, &tun_type
);
1398 if (tun_type
== BGP_ENCAP_TYPE_MPLS
) {
1400 /* MPLS carries UN address in next hop */
1401 rfapiNexthop2Prefix(bi
->attr
, &p
);
1402 if (p
.family
!= 0) {
1403 rfapiQprefix2Raddr(&p
, &new->un_address
);
1404 have_vnc_tunnel_un
= 1;
1408 for (pEncap
= bi
->attr
->encap_subtlvs
; pEncap
;
1409 pEncap
= pEncap
->next
) {
1410 switch (pEncap
->type
) {
1411 case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT
:
1413 * Overrides ENCAP UN address, if any
1415 switch (pEncap
->length
) {
1418 new->un_address
.addr_family
= AF_INET
;
1419 memcpy(&new->un_address
.addr
.v4
,
1421 have_vnc_tunnel_un
= 1;
1425 new->un_address
.addr_family
= AF_INET6
;
1426 memcpy(&new->un_address
.addr
.v6
,
1428 have_vnc_tunnel_un
= 1;
1433 "%s: invalid tunnel subtlv UN addr length (%d) for bi %p",
1434 __func__
, pEncap
->length
, bi
);
1440 "%s: unknown Encap Attribute option type %d",
1441 __func__
, pEncap
->type
);
1448 new->un_options
= rfapi_encap_tlv_to_un_option(bi
->attr
);
1450 #if DEBUG_ENCAP_MONITOR
1451 vnc_zlog_debug_verbose("%s: line %d: have_vnc_tunnel_un=%d",
1452 __func__
, __LINE__
, have_vnc_tunnel_un
);
1455 if (!have_vnc_tunnel_un
&& bi
->extra
) {
1457 * use cached UN address from ENCAP route
1459 new->un_address
.addr_family
=
1460 bi
->extra
->vnc
.import
.un_family
;
1461 switch (new->un_address
.addr_family
) {
1463 new->un_address
.addr
.v4
=
1464 bi
->extra
->vnc
.import
.un
.addr4
;
1467 new->un_address
.addr
.v6
=
1468 bi
->extra
->vnc
.import
.un
.addr6
;
1472 "%s: invalid UN addr family (%d) for bi %p",
1473 __func__
, new->un_address
.addr_family
,
1475 rfapi_free_next_hop_list(new);
1482 new->lifetime
= lifetime
;
1486 int rfapiHasNonRemovedRoutes(struct agg_node
*rn
)
1488 struct bgp_info
*bi
;
1490 for (bi
= rn
->info
; bi
; bi
= bi
->next
) {
1493 if (!CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
)
1494 && (bi
->extra
&& !rfapiGetUnAddrOfVpnBi(bi
, &pfx
))) {
1506 void rfapiDumpNode(struct agg_node
*rn
)
1508 struct bgp_info
*bi
;
1510 vnc_zlog_debug_verbose("%s: rn=%p", __func__
, rn
);
1511 for (bi
= rn
->info
; bi
; bi
= bi
->next
) {
1513 int ctrc
= rfapiGetUnAddrOfVpnBi(bi
, &pfx
);
1516 if (!CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
)
1517 && (bi
->extra
&& !ctrc
)) {
1524 vnc_zlog_debug_verbose(
1525 " bi=%p, nr=%d, flags=0x%x, extra=%p, ctrc=%d", bi
, nr
,
1526 bi
->flags
, bi
->extra
, ctrc
);
1531 static int rfapiNhlAddNodeRoutes(
1532 struct agg_node
*rn
, /* in */
1533 struct rfapi_ip_prefix
*rprefix
, /* in */
1534 uint32_t lifetime
, /* in */
1535 int removed
, /* in */
1536 struct rfapi_next_hop_entry
**head
, /* in/out */
1537 struct rfapi_next_hop_entry
**tail
, /* in/out */
1538 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1539 struct agg_node
*rfd_rib_node
, /* preload this NVE rib node */
1540 struct prefix
*pfx_target_original
) /* query target */
1542 struct bgp_info
*bi
;
1543 struct rfapi_next_hop_entry
*new;
1544 struct prefix pfx_un
;
1545 struct skiplist
*seen_nexthops
;
1547 int is_l2
= (rn
->p
.family
== AF_ETHERNET
);
1550 struct agg_table
*atable
= agg_get_table(rfd_rib_node
);
1551 struct rfapi_descriptor
*rfd
;
1554 rfd
= agg_get_table_info(atable
);
1556 if (rfapiRibFTDFilterRecentPrefix(rfd
, rn
,
1557 pfx_target_original
))
1563 skiplist_new(0, vnc_prefix_cmp
, (void (*)(void *))prefix_free
);
1565 for (bi
= rn
->info
; bi
; bi
= bi
->next
) {
1567 struct prefix pfx_vn
;
1568 struct prefix
*newpfx
;
1570 if (removed
&& !CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
)) {
1571 #if DEBUG_RETURNED_NHL
1572 vnc_zlog_debug_verbose(
1573 "%s: want holddown, this route not holddown, skip",
1578 if (!removed
&& CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
)) {
1587 * Check for excluded VN address
1589 if (rfapiVpnBiNhEqualsPt(bi
, exclude_vnaddr
))
1593 * Check for VN address (nexthop) copied already
1596 /* L2 routes: semantic nexthop in aux_prefix; VN addr
1598 pfx_vn
= bi
->extra
->vnc
.import
.aux_prefix
;
1600 rfapiNexthop2Prefix(bi
->attr
, &pfx_vn
);
1602 if (!skiplist_search(seen_nexthops
, &pfx_vn
, NULL
)) {
1603 #if DEBUG_RETURNED_NHL
1604 char buf
[PREFIX_STRLEN
];
1606 prefix2str(&pfx_vn
, buf
, sizeof(buf
));
1607 vnc_zlog_debug_verbose(
1608 "%s: already put VN/nexthop %s, skip", __func__
,
1614 if (rfapiGetUnAddrOfVpnBi(bi
, &pfx_un
)) {
1615 #if DEBUG_ENCAP_MONITOR
1616 vnc_zlog_debug_verbose(
1617 "%s: failed to get UN address of this VPN bi",
1623 newpfx
= prefix_new();
1625 skiplist_insert(seen_nexthops
, newpfx
, newpfx
);
1627 new = rfapiRouteInfo2NextHopEntry(rprefix
, bi
, lifetime
, rn
);
1629 if (rfapiRibPreloadBi(rfd_rib_node
, &pfx_vn
, &pfx_un
,
1631 /* duplicate filtered by RIB */
1632 rfapi_free_next_hop_list(new);
1639 (*tail
)->next
= new;
1648 skiplist_free(seen_nexthops
);
1657 * omit_node is meant for the situation where we are adding a subtree
1658 * of a parent of some original requested node. The response already
1659 * contains the original requested node, and we don't want to duplicate
1660 * its routes in the list, so we skip it if the right or left node
1661 * matches (of course, we still travel down its child subtrees).
1663 static int rfapiNhlAddSubtree(
1664 struct agg_node
*rn
, /* in */
1665 uint32_t lifetime
, /* in */
1666 struct rfapi_next_hop_entry
**head
, /* in/out */
1667 struct rfapi_next_hop_entry
**tail
, /* in/out */
1668 struct agg_node
*omit_node
, /* in */
1669 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1670 struct agg_table
*rfd_rib_table
, /* preload here */
1671 struct prefix
*pfx_target_original
) /* query target */
1673 struct rfapi_ip_prefix rprefix
;
1676 /* FIXME: need to find a better way here to work without sticking our
1677 * hands in node->link */
1678 if (agg_node_left(rn
) && agg_node_left(rn
) != omit_node
) {
1679 if (agg_node_left(rn
)->info
) {
1681 struct agg_node
*rib_rn
= NULL
;
1683 rfapiQprefix2Rprefix(&agg_node_left(rn
)->p
, &rprefix
);
1684 if (rfd_rib_table
) {
1685 rib_rn
= agg_node_get(rfd_rib_table
,
1686 &agg_node_left(rn
)->p
);
1689 count
= rfapiNhlAddNodeRoutes(
1690 agg_node_left(rn
), &rprefix
, lifetime
, 0, head
,
1691 tail
, exclude_vnaddr
, rib_rn
,
1692 pfx_target_original
);
1694 count
= rfapiNhlAddNodeRoutes(
1695 agg_node_left(rn
), &rprefix
, lifetime
,
1696 1, head
, tail
, exclude_vnaddr
, rib_rn
,
1697 pfx_target_original
);
1701 agg_unlock_node(rib_rn
);
1705 if (agg_node_right(rn
) && agg_node_right(rn
) != omit_node
) {
1706 if (agg_node_right(rn
)->info
) {
1708 struct agg_node
*rib_rn
= NULL
;
1710 rfapiQprefix2Rprefix(&agg_node_right(rn
)->p
, &rprefix
);
1711 if (rfd_rib_table
) {
1712 rib_rn
= agg_node_get(rfd_rib_table
,
1713 &agg_node_right(rn
)->p
);
1715 count
= rfapiNhlAddNodeRoutes(
1716 agg_node_right(rn
), &rprefix
, lifetime
, 0, head
,
1717 tail
, exclude_vnaddr
, rib_rn
,
1718 pfx_target_original
);
1720 count
= rfapiNhlAddNodeRoutes(
1721 agg_node_right(rn
), &rprefix
, lifetime
,
1722 1, head
, tail
, exclude_vnaddr
, rib_rn
,
1723 pfx_target_original
);
1727 agg_unlock_node(rib_rn
);
1731 if (agg_node_left(rn
)) {
1732 rcount
+= rfapiNhlAddSubtree(
1733 agg_node_left(rn
), lifetime
, head
, tail
, omit_node
,
1734 exclude_vnaddr
, rfd_rib_table
, pfx_target_original
);
1736 if (agg_node_right(rn
)) {
1737 rcount
+= rfapiNhlAddSubtree(
1738 agg_node_right(rn
), lifetime
, head
, tail
, omit_node
,
1739 exclude_vnaddr
, rfd_rib_table
, pfx_target_original
);
1746 * Implementation of ROUTE_LIST(node) from RFAPI-Import-Event-Handling.txt
1748 * Construct an rfapi nexthop list based on the routes attached to
1749 * the specified node.
1751 * If there are any routes that do NOT have BGP_INFO_REMOVED set,
1752 * return those only. If there are ONLY routes with BGP_INFO_REMOVED,
1753 * then return those, and also include all the non-removed routes from the
1754 * next less-specific node (i.e., this node's parent) at the end.
1756 struct rfapi_next_hop_entry
*rfapiRouteNode2NextHopList(
1757 struct agg_node
*rn
, uint32_t lifetime
, /* put into nexthop entries */
1758 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1759 struct agg_table
*rfd_rib_table
, /* preload here */
1760 struct prefix
*pfx_target_original
) /* query target */
1762 struct rfapi_ip_prefix rprefix
;
1763 struct rfapi_next_hop_entry
*answer
= NULL
;
1764 struct rfapi_next_hop_entry
*last
= NULL
;
1765 struct agg_node
*parent
;
1767 struct agg_node
*rib_rn
;
1769 #if DEBUG_RETURNED_NHL
1771 char buf
[PREFIX_STRLEN
];
1773 prefix2str(&rn
->p
, buf
, sizeof(buf
));
1774 vnc_zlog_debug_verbose("%s: called with node pfx=%s", __func__
,
1777 rfapiDebugBacktrace();
1780 rfapiQprefix2Rprefix(&rn
->p
, &rprefix
);
1782 rib_rn
= rfd_rib_table
? agg_node_get(rfd_rib_table
, &rn
->p
) : NULL
;
1785 * Add non-withdrawn routes at this node
1787 count
= rfapiNhlAddNodeRoutes(rn
, &rprefix
, lifetime
, 0, &answer
, &last
,
1788 exclude_vnaddr
, rib_rn
,
1789 pfx_target_original
);
1792 * If the list has at least one entry, it's finished
1795 count
+= rfapiNhlAddSubtree(rn
, lifetime
, &answer
, &last
, NULL
,
1796 exclude_vnaddr
, rfd_rib_table
,
1797 pfx_target_original
);
1798 vnc_zlog_debug_verbose("%s: %d nexthops, answer=%p", __func__
,
1800 #if DEBUG_RETURNED_NHL
1801 rfapiPrintNhl(NULL
, answer
);
1804 agg_unlock_node(rib_rn
);
1809 * Add withdrawn routes at this node
1811 count
= rfapiNhlAddNodeRoutes(rn
, &rprefix
, lifetime
, 1, &answer
, &last
,
1812 exclude_vnaddr
, rib_rn
,
1813 pfx_target_original
);
1815 agg_unlock_node(rib_rn
);
1817 // rfapiPrintNhl(NULL, answer);
1820 * walk up the tree until we find a node with non-deleted
1821 * routes, then add them
1823 for (parent
= agg_node_parent(rn
); parent
;
1824 parent
= agg_node_parent(parent
)) {
1825 if (rfapiHasNonRemovedRoutes(parent
)) {
1831 * Add non-withdrawn routes from less-specific prefix
1834 rib_rn
= rfd_rib_table
? agg_node_get(rfd_rib_table
, &parent
->p
)
1836 rfapiQprefix2Rprefix(&parent
->p
, &rprefix
);
1837 count
+= rfapiNhlAddNodeRoutes(parent
, &rprefix
, lifetime
, 0,
1838 &answer
, &last
, exclude_vnaddr
,
1839 rib_rn
, pfx_target_original
);
1840 count
+= rfapiNhlAddSubtree(parent
, lifetime
, &answer
, &last
,
1841 rn
, exclude_vnaddr
, rfd_rib_table
,
1842 pfx_target_original
);
1844 agg_unlock_node(rib_rn
);
1847 * There is no parent with non-removed routes. Still need to
1848 * add subtree of original node if it contributed routes to the
1852 count
+= rfapiNhlAddSubtree(rn
, lifetime
, &answer
,
1853 &last
, rn
, exclude_vnaddr
,
1855 pfx_target_original
);
1858 vnc_zlog_debug_verbose("%s: %d nexthops, answer=%p", __func__
, count
,
1860 #if DEBUG_RETURNED_NHL
1861 rfapiPrintNhl(NULL
, answer
);
1867 * Construct nexthop list of all routes in table
1869 struct rfapi_next_hop_entry
*rfapiRouteTable2NextHopList(
1870 struct agg_table
*rt
, uint32_t lifetime
, /* put into nexthop entries */
1871 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1872 struct agg_table
*rfd_rib_table
, /* preload this NVE rib table */
1873 struct prefix
*pfx_target_original
) /* query target */
1875 struct agg_node
*rn
;
1876 struct rfapi_next_hop_entry
*biglist
= NULL
;
1877 struct rfapi_next_hop_entry
*nhl
;
1878 struct rfapi_next_hop_entry
*tail
= NULL
;
1881 for (rn
= agg_route_top(rt
); rn
; rn
= agg_route_next(rn
)) {
1883 nhl
= rfapiRouteNode2NextHopList(rn
, lifetime
, exclude_vnaddr
,
1885 pfx_target_original
);
1887 tail
= biglist
= nhl
;
1894 while (tail
->next
) {
1901 vnc_zlog_debug_verbose("%s: returning %d routes", __func__
, count
);
1905 struct rfapi_next_hop_entry
*rfapiEthRouteNode2NextHopList(
1906 struct agg_node
*rn
, struct rfapi_ip_prefix
*rprefix
,
1907 uint32_t lifetime
, /* put into nexthop entries */
1908 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1909 struct agg_table
*rfd_rib_table
, /* preload NVE rib table */
1910 struct prefix
*pfx_target_original
) /* query target */
1913 struct rfapi_next_hop_entry
*answer
= NULL
;
1914 struct rfapi_next_hop_entry
*last
= NULL
;
1915 struct agg_node
*rib_rn
;
1917 rib_rn
= rfd_rib_table
? agg_node_get(rfd_rib_table
, &rn
->p
) : NULL
;
1919 count
= rfapiNhlAddNodeRoutes(rn
, rprefix
, lifetime
, 0, &answer
, &last
,
1920 NULL
, rib_rn
, pfx_target_original
);
1922 #if DEBUG_ENCAP_MONITOR
1923 vnc_zlog_debug_verbose("%s: node %p: %d non-holddown routes", __func__
,
1928 count
= rfapiNhlAddNodeRoutes(rn
, rprefix
, lifetime
, 1, &answer
,
1929 &last
, exclude_vnaddr
, rib_rn
,
1930 pfx_target_original
);
1931 vnc_zlog_debug_verbose("%s: node %p: %d holddown routes",
1932 __func__
, rn
, count
);
1936 agg_unlock_node(rib_rn
);
1938 #if DEBUG_RETURNED_NHL
1939 rfapiPrintNhl(NULL
, answer
);
1947 * Construct nexthop list of all routes in table
1949 struct rfapi_next_hop_entry
*rfapiEthRouteTable2NextHopList(
1950 uint32_t logical_net_id
, struct rfapi_ip_prefix
*rprefix
,
1951 uint32_t lifetime
, /* put into nexthop entries */
1952 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1953 struct agg_table
*rfd_rib_table
, /* preload NVE rib node */
1954 struct prefix
*pfx_target_original
) /* query target */
1956 struct rfapi_import_table
*it
;
1957 struct bgp
*bgp
= bgp_get_default();
1958 struct agg_table
*rt
;
1959 struct agg_node
*rn
;
1960 struct rfapi_next_hop_entry
*biglist
= NULL
;
1961 struct rfapi_next_hop_entry
*nhl
;
1962 struct rfapi_next_hop_entry
*tail
= NULL
;
1966 it
= rfapiMacImportTableGet(bgp
, logical_net_id
);
1967 rt
= it
->imported_vpn
[AFI_L2VPN
];
1969 for (rn
= agg_route_top(rt
); rn
; rn
= agg_route_next(rn
)) {
1971 nhl
= rfapiEthRouteNode2NextHopList(
1972 rn
, rprefix
, lifetime
, exclude_vnaddr
, rfd_rib_table
,
1973 pfx_target_original
);
1975 tail
= biglist
= nhl
;
1982 while (tail
->next
) {
1989 vnc_zlog_debug_verbose("%s: returning %d routes", __func__
, count
);
1994 * Insert a new bi to the imported route table node,
1995 * keeping the list of BIs sorted best route first
1997 static void rfapiBgpInfoAttachSorted(struct agg_node
*rn
,
1998 struct bgp_info
*info_new
, afi_t afi
,
2002 struct bgp_info
*prev
;
2003 struct bgp_info
*next
;
2004 char pfx_buf
[PREFIX2STR_BUFFER
];
2007 bgp
= bgp_get_default(); /* assume 1 instance for now */
2009 if (VNC_DEBUG(IMPORT_BI_ATTACH
)) {
2010 vnc_zlog_debug_verbose("%s: info_new->peer=%p", __func__
,
2012 vnc_zlog_debug_verbose("%s: info_new->peer->su_remote=%p",
2013 __func__
, info_new
->peer
->su_remote
);
2016 for (prev
= NULL
, next
= rn
->info
; next
;
2017 prev
= next
, next
= next
->next
) {
2018 if (!bgp
|| (!CHECK_FLAG(info_new
->flags
, BGP_INFO_REMOVED
)
2019 && CHECK_FLAG(next
->flags
, BGP_INFO_REMOVED
))
2020 || bgp_info_cmp_compatible(bgp
, info_new
, next
, pfx_buf
,
2022 == -1) { /* -1 if 1st is better */
2026 vnc_zlog_debug_verbose("%s: prev=%p, next=%p", __func__
, prev
, next
);
2028 prev
->next
= info_new
;
2030 rn
->info
= info_new
;
2032 info_new
->prev
= prev
;
2033 info_new
->next
= next
;
2035 next
->prev
= info_new
;
2036 bgp_attr_intern(info_new
->attr
);
2039 static void rfapiBgpInfoDetach(struct agg_node
*rn
, struct bgp_info
*bi
)
2042 * Remove the route (doubly-linked)
2044 // bgp_attr_unintern (&bi->attr);
2046 bi
->next
->prev
= bi
->prev
;
2048 bi
->prev
->next
= bi
->next
;
2050 rn
->info
= bi
->next
;
2054 * For L3-indexed import tables
2056 static int rfapi_bi_peer_rd_cmp(void *b1
, void *b2
)
2058 struct bgp_info
*bi1
= b1
;
2059 struct bgp_info
*bi2
= b2
;
2064 if (bi1
->peer
< bi2
->peer
)
2066 if (bi1
->peer
> bi2
->peer
)
2072 return vnc_prefix_cmp((struct prefix
*)&bi1
->extra
->vnc
.import
.rd
,
2073 (struct prefix
*)&bi2
->extra
->vnc
.import
.rd
);
2077 * For L2-indexed import tables
2078 * The BIs in these tables should ALWAYS have an aux_prefix set because
2079 * they arrive via IPv4 or IPv6 advertisements.
2081 static int rfapi_bi_peer_rd_aux_cmp(void *b1
, void *b2
)
2083 struct bgp_info
*bi1
= b1
;
2084 struct bgp_info
*bi2
= b2
;
2090 if (bi1
->peer
< bi2
->peer
)
2092 if (bi1
->peer
> bi2
->peer
)
2098 rc
= vnc_prefix_cmp((struct prefix
*)&bi1
->extra
->vnc
.import
.rd
,
2099 (struct prefix
*)&bi2
->extra
->vnc
.import
.rd
);
2105 * L2 import tables can have multiple entries with the
2106 * same MAC address, same RD, but different L3 addresses.
2108 * Use presence of aux_prefix with AF=ethernet and prefixlen=1
2109 * as magic value to signify explicit wildcarding of the aux_prefix.
2110 * This magic value will not appear in bona fide bi entries in
2111 * the import table, but is allowed in the "fake" bi used to
2112 * probe the table when searching. (We have to test both b1 and b2
2113 * because there is no guarantee of the order the test key and
2114 * the real key will be passed)
2116 if ((bi1
->extra
->vnc
.import
.aux_prefix
.family
== AF_ETHERNET
2117 && (bi1
->extra
->vnc
.import
.aux_prefix
.prefixlen
== 1))
2118 || (bi2
->extra
->vnc
.import
.aux_prefix
.family
== AF_ETHERNET
2119 && (bi2
->extra
->vnc
.import
.aux_prefix
.prefixlen
== 1))) {
2122 * wildcard aux address specified
2127 return vnc_prefix_cmp(&bi1
->extra
->vnc
.import
.aux_prefix
,
2128 &bi2
->extra
->vnc
.import
.aux_prefix
);
2133 * Index on RD and Peer
2135 static void rfapiItBiIndexAdd(struct agg_node
*rn
, /* Import table VPN node */
2136 struct bgp_info
*bi
) /* new BI */
2138 struct skiplist
*sl
;
2145 char buf
[RD_ADDRSTRLEN
];
2147 vnc_zlog_debug_verbose("%s: bi %p, peer %p, rd %s", __func__
,
2149 prefix_rd2str(&bi
->extra
->vnc
.import
.rd
,
2153 sl
= RFAPI_RDINDEX_W_ALLOC(rn
);
2155 if (AF_ETHERNET
== rn
->p
.family
) {
2156 sl
= skiplist_new(0, rfapi_bi_peer_rd_aux_cmp
, NULL
);
2158 sl
= skiplist_new(0, rfapi_bi_peer_rd_cmp
, NULL
);
2160 RFAPI_IT_EXTRA_GET(rn
)->u
.vpn
.idx_rd
= sl
;
2161 agg_lock_node(rn
); /* for skiplist */
2163 assert(!skiplist_insert(sl
, (void *)bi
, (void *)bi
));
2164 agg_lock_node(rn
); /* for skiplist entry */
2166 /* NB: BIs in import tables are not refcounted */
2169 static void rfapiItBiIndexDump(struct agg_node
*rn
)
2171 struct skiplist
*sl
;
2172 void *cursor
= NULL
;
2177 sl
= RFAPI_RDINDEX(rn
);
2181 for (rc
= skiplist_next(sl
, (void **)&k
, (void **)&v
, &cursor
); !rc
;
2182 rc
= skiplist_next(sl
, (void **)&k
, (void **)&v
, &cursor
)) {
2184 char buf
[RD_ADDRSTRLEN
];
2185 char buf_aux_pfx
[PREFIX_STRLEN
];
2187 prefix_rd2str(&k
->extra
->vnc
.import
.rd
, buf
, sizeof(buf
));
2188 if (k
->extra
->vnc
.import
.aux_prefix
.family
) {
2189 prefix2str(&k
->extra
->vnc
.import
.aux_prefix
,
2190 buf_aux_pfx
, sizeof(buf_aux_pfx
));
2192 strncpy(buf_aux_pfx
, "(none)", PREFIX_STRLEN
);
2194 vnc_zlog_debug_verbose("bi %p, peer %p, rd %s, aux_prefix %s",
2195 k
, k
->peer
, buf
, buf_aux_pfx
);
2199 static struct bgp_info
*rfapiItBiIndexSearch(
2200 struct agg_node
*rn
, /* Import table VPN node */
2201 struct prefix_rd
*prd
, struct peer
*peer
,
2202 struct prefix
*aux_prefix
) /* optional L3 addr for L2 ITs */
2204 struct skiplist
*sl
;
2206 struct bgp_info bi_fake
;
2207 struct bgp_info_extra bi_extra
;
2208 struct bgp_info
*bi_result
;
2210 sl
= RFAPI_RDINDEX(rn
);
2216 char buf
[RD_ADDRSTRLEN
];
2217 char buf_aux_pfx
[PREFIX_STRLEN
];
2220 prefix2str(aux_prefix
, buf_aux_pfx
,
2221 sizeof(buf_aux_pfx
));
2223 strncpy(buf_aux_pfx
, "(nil)", sizeof(buf_aux_pfx
));
2225 vnc_zlog_debug_verbose("%s want prd=%s, peer=%p, aux_prefix=%s",
2227 prefix_rd2str(prd
, buf
, sizeof(buf
)),
2229 rfapiItBiIndexDump(rn
);
2233 /* threshold is a WAG */
2234 if (sl
->count
< 3) {
2236 vnc_zlog_debug_verbose("%s: short list algorithm", __func__
);
2238 /* if short list, linear search might be faster */
2239 for (bi_result
= rn
->info
; bi_result
;
2240 bi_result
= bi_result
->next
) {
2243 char buf
[RD_ADDRSTRLEN
];
2245 vnc_zlog_debug_verbose(
2246 "%s: bi has prd=%s, peer=%p", __func__
,
2247 prefix_rd2str(&bi_result
->extra
->vnc
2253 if (peer
== bi_result
->peer
2254 && !prefix_cmp((struct prefix
*)&bi_result
->extra
2256 (struct prefix
*)prd
)) {
2259 vnc_zlog_debug_verbose(
2260 "%s: peer and RD same, doing aux_prefix check",
2264 || !prefix_cmp(aux_prefix
,
2265 &bi_result
->extra
->vnc
.import
2269 vnc_zlog_debug_verbose("%s: match",
2279 bi_fake
.peer
= peer
;
2280 bi_fake
.extra
= &bi_extra
;
2281 bi_fake
.extra
->vnc
.import
.rd
= *(struct prefix_rd
*)prd
;
2283 bi_fake
.extra
->vnc
.import
.aux_prefix
= *aux_prefix
;
2286 bi_fake
.extra
->vnc
.import
.aux_prefix
.family
= AF_ETHERNET
;
2287 bi_fake
.extra
->vnc
.import
.aux_prefix
.prefixlen
= 1;
2290 rc
= skiplist_search(sl
, (void *)&bi_fake
, (void *)&bi_result
);
2294 vnc_zlog_debug_verbose("%s: no match", __func__
);
2300 vnc_zlog_debug_verbose("%s: matched bi=%p", __func__
, bi_result
);
2306 static void rfapiItBiIndexDel(struct agg_node
*rn
, /* Import table VPN node */
2307 struct bgp_info
*bi
) /* old BI */
2309 struct skiplist
*sl
;
2313 char buf
[RD_ADDRSTRLEN
];
2315 vnc_zlog_debug_verbose("%s: bi %p, peer %p, rd %s", __func__
,
2317 prefix_rd2str(&bi
->extra
->vnc
.import
.rd
,
2321 sl
= RFAPI_RDINDEX(rn
);
2324 rc
= skiplist_delete(sl
, (void *)(bi
), (void *)bi
);
2326 rfapiItBiIndexDump(rn
);
2330 agg_unlock_node(rn
); /* for skiplist entry */
2332 /* NB: BIs in import tables are not refcounted */
2336 * Add a backreference at the ENCAP node to the VPN route that
2339 static void rfapiMonitorEncapAdd(struct rfapi_import_table
*import_table
,
2340 struct prefix
*p
, /* VN address */
2341 struct agg_node
*vpn_rn
, /* VPN node */
2342 struct bgp_info
*vpn_bi
) /* VPN bi/route */
2344 afi_t afi
= family2afi(p
->family
);
2345 struct agg_node
*rn
;
2346 struct rfapi_monitor_encap
*m
;
2349 rn
= agg_node_get(import_table
->imported_encap
[afi
], p
); /* locks rn */
2352 m
= XCALLOC(MTYPE_RFAPI_MONITOR_ENCAP
,
2353 sizeof(struct rfapi_monitor_encap
));
2360 /* insert to encap node's list */
2361 m
->next
= RFAPI_MONITOR_ENCAP(rn
);
2364 RFAPI_MONITOR_ENCAP_W_ALLOC(rn
) = m
;
2366 /* for easy lookup when deleting vpn route */
2367 vpn_bi
->extra
->vnc
.import
.hme
= m
;
2369 vnc_zlog_debug_verbose(
2370 "%s: it=%p, vpn_bi=%p, afi=%d, encap rn=%p, setting vpn_bi->extra->vnc.import.hme=%p",
2371 __func__
, import_table
, vpn_bi
, afi
, rn
, m
);
2373 RFAPI_CHECK_REFCOUNT(rn
, SAFI_ENCAP
, 0);
2374 bgp_attr_intern(vpn_bi
->attr
);
2377 static void rfapiMonitorEncapDelete(struct bgp_info
*vpn_bi
)
2380 * Remove encap monitor
2382 vnc_zlog_debug_verbose("%s: vpn_bi=%p", __func__
, vpn_bi
);
2383 if (vpn_bi
->extra
) {
2384 struct rfapi_monitor_encap
*hme
= vpn_bi
->extra
->vnc
.import
.hme
;
2388 vnc_zlog_debug_verbose("%s: hme=%p", __func__
, hme
);
2390 /* Refcount checking takes too long here */
2391 // RFAPI_CHECK_REFCOUNT(hme->rn, SAFI_ENCAP, 0);
2393 hme
->next
->prev
= hme
->prev
;
2395 hme
->prev
->next
= hme
->next
;
2397 RFAPI_MONITOR_ENCAP_W_ALLOC(hme
->rn
) =
2399 /* Refcount checking takes too long here */
2400 // RFAPI_CHECK_REFCOUNT(hme->rn, SAFI_ENCAP, 1);
2402 /* see if the struct rfapi_it_extra is empty and can be
2404 rfapiMonitorExtraPrune(SAFI_ENCAP
, hme
->rn
);
2406 agg_unlock_node(hme
->rn
); /* decr ref count */
2407 XFREE(MTYPE_RFAPI_MONITOR_ENCAP
, hme
);
2408 vpn_bi
->extra
->vnc
.import
.hme
= NULL
;
2414 * quagga lib/thread.h says this must return int even though
2415 * it doesn't do anything with the return value
2417 static int rfapiWithdrawTimerVPN(struct thread
*t
)
2419 struct rfapi_withdraw
*wcb
= t
->arg
;
2420 struct bgp_info
*bi
= wcb
->info
;
2421 struct bgp
*bgp
= bgp_get_default();
2423 struct rfapi_monitor_vpn
*moved
;
2428 assert(wcb
->import_table
);
2431 RFAPI_CHECK_REFCOUNT(wcb
->node
, SAFI_MPLS_VPN
, wcb
->lockoffset
);
2436 vnc_zlog_debug_verbose(
2437 "%s: removing bi %p at prefix %s/%d", __func__
, bi
,
2438 rfapi_ntop(wcb
->node
->p
.family
, &wcb
->node
->p
.u
.prefix
,
2440 wcb
->node
->p
.prefixlen
);
2444 * Remove the route (doubly-linked)
2446 if (CHECK_FLAG(bi
->flags
, BGP_INFO_VALID
)
2447 && VALID_INTERIOR_TYPE(bi
->type
))
2448 RFAPI_MONITOR_EXTERIOR(wcb
->node
)->valid_interior_count
--;
2450 afi
= family2afi(wcb
->node
->p
.family
);
2451 wcb
->import_table
->holddown_count
[afi
] -= 1; /* keep count consistent */
2452 rfapiItBiIndexDel(wcb
->node
, bi
);
2453 rfapiBgpInfoDetach(wcb
->node
, bi
); /* with removed bi */
2455 vnc_import_bgp_exterior_del_route_interior(bgp
, wcb
->import_table
,
2460 * If VNC is configured to send response remove messages, AND
2461 * if the removed route had a UN address, do response removal
2464 if (!(bgp
->rfapi_cfg
->flags
2465 & BGP_VNC_CONFIG_RESPONSE_REMOVAL_DISABLE
)) {
2467 int has_valid_duplicate
= 0;
2468 struct bgp_info
*bii
;
2471 * First check if there are any OTHER routes at this node
2472 * that have the same nexthop and a valid UN address. If
2473 * there are (e.g., from other peers), then the route isn't
2474 * really gone, so skip sending a response removal message.
2476 for (bii
= wcb
->node
->info
; bii
; bii
= bii
->next
) {
2477 if (rfapiVpnBiSamePtUn(bi
, bii
)) {
2478 has_valid_duplicate
= 1;
2483 vnc_zlog_debug_verbose("%s: has_valid_duplicate=%d", __func__
,
2484 has_valid_duplicate
);
2486 if (!has_valid_duplicate
) {
2487 rfapiRibPendingDeleteRoute(bgp
, wcb
->import_table
, afi
,
2492 rfapiMonitorEncapDelete(bi
);
2495 * If there are no VPN monitors at this VPN Node A,
2498 if (!RFAPI_MONITOR_VPN(wcb
->node
)) {
2499 vnc_zlog_debug_verbose("%s: no VPN monitors at this node",
2505 * rfapiMonitorMoveShorter only moves monitors if there are
2506 * no remaining valid routes at the current node
2508 moved
= rfapiMonitorMoveShorter(wcb
->node
, 1);
2511 rfapiMonitorMovedUp(wcb
->import_table
, wcb
->node
, moved
->node
,
2519 rfapiBgpInfoFree(bi
);
2523 * If route count at this node has gone to 0, withdraw exported prefix
2525 if (!wcb
->node
->info
) {
2526 /* see if the struct rfapi_it_extra is empty and can be freed */
2527 rfapiMonitorExtraPrune(SAFI_MPLS_VPN
, wcb
->node
);
2528 vnc_direct_bgp_del_prefix(bgp
, wcb
->import_table
, wcb
->node
);
2529 vnc_zebra_del_prefix(bgp
, wcb
->import_table
, wcb
->node
);
2532 * nexthop change event
2533 * vnc_direct_bgp_add_prefix() will recompute the VN addr
2536 vnc_direct_bgp_add_prefix(bgp
, wcb
->import_table
, wcb
->node
);
2539 RFAPI_CHECK_REFCOUNT(wcb
->node
, SAFI_MPLS_VPN
, 1 + wcb
->lockoffset
);
2540 agg_unlock_node(wcb
->node
); /* decr ref count */
2541 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
2546 * This works for multiprotocol extension, but not for plain ol'
2547 * unicast IPv4 because that nexthop is stored in attr->nexthop
2549 void rfapiNexthop2Prefix(struct attr
*attr
, struct prefix
*p
)
2554 memset(p
, 0, sizeof(struct prefix
));
2556 switch (p
->family
= BGP_MP_NEXTHOP_FAMILY(attr
->mp_nexthop_len
)) {
2558 p
->u
.prefix4
= attr
->mp_nexthop_global_in
;
2563 p
->u
.prefix6
= attr
->mp_nexthop_global
;
2568 vnc_zlog_debug_verbose("%s: Family is unknown = %d", __func__
,
2573 void rfapiUnicastNexthop2Prefix(afi_t afi
, struct attr
*attr
, struct prefix
*p
)
2575 if (afi
== AFI_IP
) {
2576 p
->family
= AF_INET
;
2578 p
->u
.prefix4
= attr
->nexthop
;
2580 rfapiNexthop2Prefix(attr
, p
);
2584 static int rfapiAttrNexthopAddrDifferent(struct prefix
*p1
, struct prefix
*p2
)
2587 vnc_zlog_debug_verbose("%s: p1 or p2 is NULL", __func__
);
2592 * Are address families the same?
2594 if (p1
->family
!= p2
->family
) {
2598 switch (p1
->family
) {
2600 if (IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
2605 if (IPV6_ADDR_SAME(&p1
->u
.prefix6
, &p2
->u
.prefix6
))
2616 static void rfapiCopyUnEncap2VPN(struct bgp_info
*encap_bi
,
2617 struct bgp_info
*vpn_bi
)
2619 if (!encap_bi
->attr
) {
2620 zlog_warn("%s: no encap bi attr/extra, can't copy UN address",
2625 if (!vpn_bi
|| !vpn_bi
->extra
) {
2626 zlog_warn("%s: no vpn bi attr/extra, can't copy UN address",
2631 switch (BGP_MP_NEXTHOP_FAMILY(encap_bi
->attr
->mp_nexthop_len
)) {
2635 * instrumentation to debug segfault of 091127
2637 vnc_zlog_debug_verbose("%s: vpn_bi=%p", __func__
, vpn_bi
);
2639 vnc_zlog_debug_verbose("%s: vpn_bi->extra=%p", __func__
,
2643 vpn_bi
->extra
->vnc
.import
.un_family
= AF_INET
;
2644 vpn_bi
->extra
->vnc
.import
.un
.addr4
=
2645 encap_bi
->attr
->mp_nexthop_global_in
;
2649 vpn_bi
->extra
->vnc
.import
.un_family
= AF_INET6
;
2650 vpn_bi
->extra
->vnc
.import
.un
.addr6
=
2651 encap_bi
->attr
->mp_nexthop_global
;
2655 zlog_warn("%s: invalid encap nexthop length: %d", __func__
,
2656 encap_bi
->attr
->mp_nexthop_len
);
2657 vpn_bi
->extra
->vnc
.import
.un_family
= 0;
2663 * returns 0 on success, nonzero on error
2665 static int rfapiWithdrawEncapUpdateCachedUn(
2666 struct rfapi_import_table
*import_table
, struct bgp_info
*encap_bi
,
2667 struct agg_node
*vpn_rn
, struct bgp_info
*vpn_bi
)
2672 * clear cached UN address
2674 if (!vpn_bi
|| !vpn_bi
->extra
) {
2676 "%s: missing VPN bi/extra, can't clear UN addr",
2680 vpn_bi
->extra
->vnc
.import
.un_family
= 0;
2681 memset(&vpn_bi
->extra
->vnc
.import
.un
, 0,
2682 sizeof(vpn_bi
->extra
->vnc
.import
.un
));
2683 if (CHECK_FLAG(vpn_bi
->flags
, BGP_INFO_VALID
)) {
2684 if (rfapiGetVncTunnelUnAddr(vpn_bi
->attr
, NULL
)) {
2685 UNSET_FLAG(vpn_bi
->flags
, BGP_INFO_VALID
);
2686 if (VALID_INTERIOR_TYPE(vpn_bi
->type
))
2687 RFAPI_MONITOR_EXTERIOR(vpn_rn
)
2688 ->valid_interior_count
--;
2689 /* signal interior route withdrawal to
2690 * import-exterior */
2691 vnc_import_bgp_exterior_del_route_interior(
2692 bgp_get_default(), import_table
, vpn_rn
,
2699 zlog_warn("%s: missing VPN bi, can't clear UN addr",
2703 rfapiCopyUnEncap2VPN(encap_bi
, vpn_bi
);
2704 if (!CHECK_FLAG(vpn_bi
->flags
, BGP_INFO_VALID
)) {
2705 SET_FLAG(vpn_bi
->flags
, BGP_INFO_VALID
);
2706 if (VALID_INTERIOR_TYPE(vpn_bi
->type
))
2707 RFAPI_MONITOR_EXTERIOR(vpn_rn
)
2708 ->valid_interior_count
++;
2709 /* signal interior route withdrawal to import-exterior
2711 vnc_import_bgp_exterior_add_route_interior(
2712 bgp_get_default(), import_table
, vpn_rn
,
2719 static int rfapiWithdrawTimerEncap(struct thread
*t
)
2721 struct rfapi_withdraw
*wcb
= t
->arg
;
2722 struct bgp_info
*bi
= wcb
->info
;
2723 int was_first_route
= 0;
2724 struct rfapi_monitor_encap
*em
;
2725 struct skiplist
*vpn_node_sl
= skiplist_new(0, NULL
, NULL
);
2729 assert(wcb
->import_table
);
2731 RFAPI_CHECK_REFCOUNT(wcb
->node
, SAFI_ENCAP
, 0);
2733 if (wcb
->node
->info
== bi
)
2734 was_first_route
= 1;
2737 * Remove the route/bi and free it
2739 rfapiBgpInfoDetach(wcb
->node
, bi
);
2740 rfapiBgpInfoFree(bi
);
2742 if (!was_first_route
)
2745 for (em
= RFAPI_MONITOR_ENCAP(wcb
->node
); em
; em
= em
->next
) {
2748 * Update monitoring VPN BIs with new encap info at the
2749 * head of the encap bi chain (which could be NULL after
2750 * removing the expiring bi above)
2752 if (rfapiWithdrawEncapUpdateCachedUn(wcb
->import_table
,
2753 wcb
->node
->info
, em
->node
,
2758 * Build a list of unique VPN nodes referenced by these
2760 * Use a skiplist for speed.
2762 skiplist_insert(vpn_node_sl
, em
->node
, em
->node
);
2767 * for each VPN node referenced in the ENCAP monitors:
2769 struct agg_node
*rn
;
2770 while (!skiplist_first(vpn_node_sl
, (void **)&rn
, NULL
)) {
2771 if (!wcb
->node
->info
) {
2772 struct rfapi_monitor_vpn
*moved
;
2774 moved
= rfapiMonitorMoveShorter(rn
, 0);
2776 // rfapiDoRouteCallback(wcb->import_table,
2777 // moved->node, moved);
2778 rfapiMonitorMovedUp(wcb
->import_table
, rn
,
2779 moved
->node
, moved
);
2782 // rfapiDoRouteCallback(wcb->import_table, rn, NULL);
2783 rfapiMonitorItNodeChanged(wcb
->import_table
, rn
, NULL
);
2785 skiplist_delete_first(vpn_node_sl
);
2789 RFAPI_CHECK_REFCOUNT(wcb
->node
, SAFI_ENCAP
, 1);
2790 agg_unlock_node(wcb
->node
); /* decr ref count */
2791 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
2792 skiplist_free(vpn_node_sl
);
2798 * Works for both VPN and ENCAP routes; timer_service_func is different
2802 rfapiBiStartWithdrawTimer(struct rfapi_import_table
*import_table
,
2803 struct agg_node
*rn
, struct bgp_info
*bi
, afi_t afi
,
2805 int (*timer_service_func
)(struct thread
*))
2808 struct rfapi_withdraw
*wcb
;
2810 if (CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
)) {
2812 * Already on the path to being withdrawn,
2813 * should already have a timer set up to
2816 vnc_zlog_debug_verbose(
2817 "%s: already being withdrawn, do nothing", __func__
);
2821 rfapiGetVncLifetime(bi
->attr
, &lifetime
);
2822 vnc_zlog_debug_verbose("%s: VNC lifetime is %u", __func__
, lifetime
);
2825 * withdrawn routes get to hang around for a while
2827 SET_FLAG(bi
->flags
, BGP_INFO_REMOVED
);
2829 /* set timer to remove the route later */
2830 lifetime
= rfapiGetHolddownFromLifetime(lifetime
);
2831 vnc_zlog_debug_verbose("%s: using timeout %u", __func__
, lifetime
);
2834 * Stash import_table, node, and info for use by timer
2835 * service routine, which is supposed to free the wcb.
2837 wcb
= XCALLOC(MTYPE_RFAPI_WITHDRAW
, sizeof(struct rfapi_withdraw
));
2841 wcb
->import_table
= import_table
;
2842 bgp_attr_intern(bi
->attr
);
2844 if (VNC_DEBUG(VERBOSE
)) {
2845 vnc_zlog_debug_verbose(
2846 "%s: wcb values: node=%p, info=%p, import_table=%p (bi follows)",
2847 __func__
, wcb
->node
, wcb
->info
, wcb
->import_table
);
2848 rfapiPrintBi(NULL
, bi
);
2853 if (lifetime
> UINT32_MAX
/ 1001) {
2854 /* sub-optimal case, but will probably never happen */
2855 bi
->extra
->vnc
.import
.timer
= NULL
;
2856 thread_add_timer(bm
->master
, timer_service_func
, wcb
, lifetime
,
2857 &bi
->extra
->vnc
.import
.timer
);
2859 static uint32_t jitter
;
2860 uint32_t lifetime_msec
;
2863 * the goal here is to spread out the timers so they are
2864 * sortable in the skip list
2866 if (++jitter
>= 1000)
2869 lifetime_msec
= (lifetime
* 1000) + jitter
;
2871 bi
->extra
->vnc
.import
.timer
= NULL
;
2872 thread_add_timer_msec(bm
->master
, timer_service_func
, wcb
,
2874 &bi
->extra
->vnc
.import
.timer
);
2877 /* re-sort route list (BGP_INFO_REMOVED routes are last) */
2878 if (((struct bgp_info
*)rn
->info
)->next
) {
2879 rfapiBgpInfoDetach(rn
, bi
);
2880 rfapiBgpInfoAttachSorted(rn
, bi
, afi
, safi
);
2885 typedef void(rfapi_bi_filtered_import_f
)(struct rfapi_import_table
*, int,
2886 struct peer
*, void *, struct prefix
*,
2887 struct prefix
*, afi_t
,
2888 struct prefix_rd
*, struct attr
*,
2889 uint8_t, uint8_t, uint32_t *);
2892 static void rfapiExpireEncapNow(struct rfapi_import_table
*it
,
2893 struct agg_node
*rn
, struct bgp_info
*bi
)
2895 struct rfapi_withdraw
*wcb
;
2899 * pretend we're an expiring timer
2901 wcb
= XCALLOC(MTYPE_RFAPI_WITHDRAW
, sizeof(struct rfapi_withdraw
));
2904 wcb
->import_table
= it
;
2905 memset(&t
, 0, sizeof(t
));
2907 rfapiWithdrawTimerEncap(&t
); /* frees wcb */
2910 static int rfapiGetNexthop(struct attr
*attr
, struct prefix
*prefix
)
2912 switch (BGP_MP_NEXTHOP_FAMILY(attr
->mp_nexthop_len
)) {
2914 prefix
->family
= AF_INET
;
2915 prefix
->prefixlen
= 32;
2916 prefix
->u
.prefix4
= attr
->mp_nexthop_global_in
;
2919 prefix
->family
= AF_INET6
;
2920 prefix
->prefixlen
= 128;
2921 prefix
->u
.prefix6
= attr
->mp_nexthop_global
;
2924 vnc_zlog_debug_verbose("%s: unknown attr->mp_nexthop_len %d",
2925 __func__
, attr
->mp_nexthop_len
);
2932 * import a bgp_info if its route target list intersects with the
2933 * import table's route target list
2935 static void rfapiBgpInfoFilteredImportEncap(
2936 struct rfapi_import_table
*import_table
, int action
, struct peer
*peer
,
2937 void *rfd
, /* set for looped back routes */
2939 struct prefix
*aux_prefix
, /* Unused for encap routes */
2940 afi_t afi
, struct prefix_rd
*prd
,
2941 struct attr
*attr
, /* part of bgp_info */
2942 uint8_t type
, /* part of bgp_info */
2943 uint8_t sub_type
, /* part of bgp_info */
2944 uint32_t *label
) /* part of bgp_info */
2946 struct agg_table
*rt
= NULL
;
2947 struct agg_node
*rn
;
2948 struct bgp_info
*info_new
;
2949 struct bgp_info
*bi
;
2950 struct bgp_info
*next
;
2953 struct prefix p_firstbi_old
;
2954 struct prefix p_firstbi_new
;
2956 const char *action_str
= NULL
;
2957 struct prefix un_prefix
;
2960 bgp
= bgp_get_default(); /* assume 1 instance for now */
2963 case FIF_ACTION_UPDATE
:
2964 action_str
= "update";
2966 case FIF_ACTION_WITHDRAW
:
2967 action_str
= "withdraw";
2969 case FIF_ACTION_KILL
:
2970 action_str
= "kill";
2977 vnc_zlog_debug_verbose(
2978 "%s: entry: %s: prefix %s/%d", __func__
, action_str
,
2979 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
2981 memset(&p_firstbi_old
, 0, sizeof(p_firstbi_old
));
2982 memset(&p_firstbi_new
, 0, sizeof(p_firstbi_new
));
2984 if (action
== FIF_ACTION_UPDATE
) {
2986 * Compare rt lists. If no intersection, don't import this route
2987 * On a withdraw, peer and RD are sufficient to determine if
2990 if (!attr
|| !attr
->ecommunity
) {
2992 vnc_zlog_debug_verbose(
2993 "%s: attr, extra, or ecommunity missing, not importing",
2997 #if RFAPI_REQUIRE_ENCAP_BEEC
2998 if (!rfapiEcommunitiesMatchBeec(attr
->ecommunity
)) {
2999 vnc_zlog_debug_verbose(
3000 "%s: it=%p: no match for BGP Encapsulation ecommunity",
3001 __func__
, import_table
);
3005 if (!rfapiEcommunitiesIntersect(import_table
->rt_import_list
,
3006 attr
->ecommunity
)) {
3008 vnc_zlog_debug_verbose(
3009 "%s: it=%p: no ecommunity intersection",
3010 __func__
, import_table
);
3015 * Updates must also have a nexthop address
3017 memset(&un_prefix
, 0,
3018 sizeof(un_prefix
)); /* keep valgrind happy */
3019 if (rfapiGetNexthop(attr
, &un_prefix
)) {
3020 vnc_zlog_debug_verbose("%s: missing nexthop address",
3027 * Figure out which radix tree the route would go into
3032 rt
= import_table
->imported_encap
[afi
];
3036 flog_err(EC_LIB_DEVELOPMENT
, "%s: bad afi %d", __func__
, afi
);
3041 * agg_node_lookup returns a node only if there is at least
3042 * one route attached.
3044 rn
= agg_node_lookup(rt
, p
);
3046 #if DEBUG_ENCAP_MONITOR
3047 vnc_zlog_debug_verbose("%s: initial encap lookup(it=%p) rn=%p",
3048 __func__
, import_table
, rn
);
3053 RFAPI_CHECK_REFCOUNT(rn
, SAFI_ENCAP
, 1);
3054 agg_unlock_node(rn
); /* undo lock in agg_node_lookup */
3058 * capture nexthop of first bi
3061 rfapiNexthop2Prefix(
3062 ((struct bgp_info
*)(rn
->info
))->attr
,
3066 for (bi
= rn
->info
; bi
; bi
= bi
->next
) {
3069 * Does this bgp_info refer to the same route
3070 * as we are trying to add?
3072 vnc_zlog_debug_verbose("%s: comparing BI %p", __func__
,
3079 * RD of import table bi is in bi->extra->vnc.import.rd
3080 * RD of info_orig is in prd
3083 vnc_zlog_debug_verbose("%s: no bi->extra",
3088 (struct prefix
*)&bi
->extra
->vnc
.import
.rd
,
3089 (struct prefix
*)prd
)) {
3091 vnc_zlog_debug_verbose("%s: prd does not match",
3099 if (bi
->peer
!= peer
) {
3100 vnc_zlog_debug_verbose(
3101 "%s: peer does not match", __func__
);
3105 vnc_zlog_debug_verbose("%s: found matching bi",
3108 /* Same route. Delete this bi, replace with new one */
3110 if (action
== FIF_ACTION_WITHDRAW
) {
3112 vnc_zlog_debug_verbose(
3113 "%s: withdrawing at prefix %s/%d",
3115 inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
,
3119 rfapiBiStartWithdrawTimer(
3120 import_table
, rn
, bi
, afi
, SAFI_ENCAP
,
3121 rfapiWithdrawTimerEncap
);
3124 vnc_zlog_debug_verbose(
3125 "%s: %s at prefix %s/%d", __func__
,
3126 ((action
== FIF_ACTION_KILL
)
3129 inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
,
3134 * If this route is waiting to be deleted
3136 * a previous withdraw, we must cancel its
3139 if (CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
)
3140 && bi
->extra
->vnc
.import
.timer
) {
3143 (struct thread
*)bi
->extra
->vnc
3145 struct rfapi_withdraw
*wcb
= t
->arg
;
3147 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
3151 if (action
== FIF_ACTION_UPDATE
) {
3152 rfapiBgpInfoDetach(rn
, bi
);
3153 rfapiBgpInfoFree(bi
);
3157 * Kill: do export stuff when removing
3160 struct rfapi_withdraw
*wcb
;
3164 * pretend we're an expiring timer
3167 MTYPE_RFAPI_WITHDRAW
,
3168 sizeof(struct rfapi_withdraw
));
3171 wcb
->import_table
= import_table
;
3172 memset(&t
, 0, sizeof(t
));
3174 rfapiWithdrawTimerEncap(
3175 &t
); /* frees wcb */
3184 RFAPI_CHECK_REFCOUNT(rn
, SAFI_ENCAP
, replacing
? 1 : 0);
3186 if (action
== FIF_ACTION_WITHDRAW
|| action
== FIF_ACTION_KILL
)
3190 rfapiBgpInfoCreate(attr
, peer
, rfd
, prd
, type
, sub_type
, NULL
);
3194 agg_lock_node(rn
); /* incr ref count for new BI */
3196 rn
= agg_node_get(rt
, p
);
3199 vnc_zlog_debug_verbose(
3200 "%s: (afi=%d, rn=%p) inserting at prefix %s/%d", __func__
, afi
,
3201 rn
, inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, BUFSIZ
),
3204 rfapiBgpInfoAttachSorted(rn
, info_new
, afi
, SAFI_ENCAP
);
3207 * Delete holddown routes from same NVE. See details in
3208 * rfapiBgpInfoFilteredImportVPN()
3210 for (bi
= info_new
->next
; bi
; bi
= next
) {
3212 struct prefix pfx_un
;
3216 if (!CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
))
3220 * We already match the VN address (it is the prefix
3221 * of the route node)
3224 if (!rfapiGetNexthop(bi
->attr
, &pfx_un
)
3225 && prefix_same(&pfx_un
, &un_prefix
)) {
3233 vnc_zlog_debug_verbose(
3234 "%s: removing holddown bi matching NVE of new route",
3236 if (bi
->extra
->vnc
.import
.timer
) {
3238 (struct thread
*)bi
->extra
->vnc
.import
.timer
;
3239 struct rfapi_withdraw
*wcb
= t
->arg
;
3241 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
3244 rfapiExpireEncapNow(import_table
, rn
, bi
);
3247 rfapiNexthop2Prefix(((struct bgp_info
*)(rn
->info
))->attr
,
3251 * If the nexthop address of the selected Encap route (i.e.,
3252 * the UN address) has changed, then we must update the VPN
3253 * routes that refer to this Encap route and possibly force
3256 if (rfapiAttrNexthopAddrDifferent(&p_firstbi_old
, &p_firstbi_new
)) {
3258 struct rfapi_monitor_encap
*m
;
3259 struct rfapi_monitor_encap
*mnext
;
3261 struct agg_node
*referenced_vpn_prefix
;
3264 * Optimized approach: build radix tree on the fly to
3265 * hold list of VPN nodes referenced by the ENCAP monitors
3267 * The nodes in this table correspond to prefixes of VPN routes.
3268 * The "info" pointer of the node points to a chain of
3269 * struct rfapi_monitor_encap, each of which refers to a
3270 * specific VPN node.
3272 struct agg_table
*referenced_vpn_table
;
3274 referenced_vpn_table
= agg_table_init();
3275 assert(referenced_vpn_table
);
3278 * iterate over the set of monitors at this ENCAP node.
3280 #if DEBUG_ENCAP_MONITOR
3281 vnc_zlog_debug_verbose("%s: examining monitors at rn=%p",
3284 for (m
= RFAPI_MONITOR_ENCAP(rn
); m
; m
= m
->next
) {
3287 * For each referenced bi/route, copy the ENCAP route's
3288 * nexthop to the VPN route's cached UN address field
3290 * the address family of the cached UN address field.
3292 rfapiCopyUnEncap2VPN(info_new
, m
->bi
);
3293 if (!CHECK_FLAG(m
->bi
->flags
, BGP_INFO_VALID
)) {
3294 SET_FLAG(m
->bi
->flags
, BGP_INFO_VALID
);
3295 if (VALID_INTERIOR_TYPE(m
->bi
->type
))
3296 RFAPI_MONITOR_EXTERIOR(m
->node
)
3297 ->valid_interior_count
++;
3298 vnc_import_bgp_exterior_add_route_interior(
3299 bgp
, import_table
, m
->node
, m
->bi
);
3303 * Build a list of unique VPN nodes referenced by these
3306 * There could be more than one VPN node here with a
3308 * prefix. Those are currently in an unsorted linear
3313 referenced_vpn_prefix
=
3314 agg_node_get(referenced_vpn_table
, &m
->node
->p
);
3315 assert(referenced_vpn_prefix
);
3316 for (mnext
= referenced_vpn_prefix
->info
; mnext
;
3317 mnext
= mnext
->next
) {
3319 if (mnext
->node
== m
->node
)
3325 * already have an entry for this VPN node
3327 agg_unlock_node(referenced_vpn_prefix
);
3330 MTYPE_RFAPI_MONITOR_ENCAP
,
3331 sizeof(struct rfapi_monitor_encap
));
3333 mnext
->node
= m
->node
;
3334 mnext
->next
= referenced_vpn_prefix
->info
;
3335 referenced_vpn_prefix
->info
= mnext
;
3340 * for each VPN node referenced in the ENCAP monitors:
3342 for (referenced_vpn_prefix
=
3343 agg_route_top(referenced_vpn_table
);
3344 referenced_vpn_prefix
;
3345 referenced_vpn_prefix
=
3346 agg_route_next(referenced_vpn_prefix
)) {
3348 while ((m
= referenced_vpn_prefix
->info
)) {
3352 rfapiMonitorMoveLonger(m
->node
);
3353 for (n
= m
->node
; n
; n
= agg_node_parent(n
)) {
3354 // rfapiDoRouteCallback(import_table, n,
3357 rfapiMonitorItNodeChanged(import_table
, m
->node
,
3360 referenced_vpn_prefix
->info
= m
->next
;
3361 agg_unlock_node(referenced_vpn_prefix
);
3362 XFREE(MTYPE_RFAPI_MONITOR_ENCAP
, m
);
3365 agg_table_finish(referenced_vpn_table
);
3368 RFAPI_CHECK_REFCOUNT(rn
, SAFI_ENCAP
, 0);
3371 static void rfapiExpireVpnNow(struct rfapi_import_table
*it
,
3372 struct agg_node
*rn
, struct bgp_info
*bi
,
3375 struct rfapi_withdraw
*wcb
;
3379 * pretend we're an expiring timer
3381 wcb
= XCALLOC(MTYPE_RFAPI_WITHDRAW
, sizeof(struct rfapi_withdraw
));
3384 wcb
->import_table
= it
;
3385 wcb
->lockoffset
= lockoffset
;
3386 memset(&t
, 0, sizeof(t
));
3388 rfapiWithdrawTimerVPN(&t
); /* frees wcb */
3393 * import a bgp_info if its route target list intersects with the
3394 * import table's route target list
3396 void rfapiBgpInfoFilteredImportVPN(
3397 struct rfapi_import_table
*import_table
, int action
, struct peer
*peer
,
3398 void *rfd
, /* set for looped back routes */
3400 struct prefix
*aux_prefix
, /* AFI_L2VPN: optional IP */
3401 afi_t afi
, struct prefix_rd
*prd
,
3402 struct attr
*attr
, /* part of bgp_info */
3403 uint8_t type
, /* part of bgp_info */
3404 uint8_t sub_type
, /* part of bgp_info */
3405 uint32_t *label
) /* part of bgp_info */
3407 struct agg_table
*rt
= NULL
;
3408 struct agg_node
*rn
;
3410 struct bgp_info
*info_new
;
3411 struct bgp_info
*bi
;
3412 struct bgp_info
*next
;
3414 struct prefix vn_prefix
;
3415 struct prefix un_prefix
;
3416 int un_prefix_valid
= 0;
3417 struct agg_node
*ern
;
3419 int original_had_routes
= 0;
3420 struct prefix original_nexthop
;
3421 const char *action_str
= NULL
;
3425 bgp
= bgp_get_default(); /* assume 1 instance for now */
3428 case FIF_ACTION_UPDATE
:
3429 action_str
= "update";
3431 case FIF_ACTION_WITHDRAW
:
3432 action_str
= "withdraw";
3434 case FIF_ACTION_KILL
:
3435 action_str
= "kill";
3442 if (import_table
== bgp
->rfapi
->it_ce
)
3445 vnc_zlog_debug_verbose("%s: entry: %s%s: prefix %s/%d: it %p, afi %s",
3446 __func__
, (is_it_ce
? "CE-IT " : ""), action_str
,
3447 rfapi_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
3448 p
->prefixlen
, import_table
, afi2str(afi
));
3453 * Compare rt lists. If no intersection, don't import this route
3454 * On a withdraw, peer and RD are sufficient to determine if
3457 if (action
== FIF_ACTION_UPDATE
) {
3458 if (!attr
|| !attr
->ecommunity
) {
3460 vnc_zlog_debug_verbose(
3461 "%s: attr, extra, or ecommunity missing, not importing",
3465 if ((import_table
!= bgp
->rfapi
->it_ce
)
3466 && !rfapiEcommunitiesIntersect(import_table
->rt_import_list
,
3467 attr
->ecommunity
)) {
3469 vnc_zlog_debug_verbose(
3470 "%s: it=%p: no ecommunity intersection",
3471 __func__
, import_table
);
3475 memset(&vn_prefix
, 0,
3476 sizeof(vn_prefix
)); /* keep valgrind happy */
3477 if (rfapiGetNexthop(attr
, &vn_prefix
)) {
3478 /* missing nexthop address would be a bad, bad thing */
3479 vnc_zlog_debug_verbose("%s: missing nexthop", __func__
);
3485 * Figure out which radix tree the route would go into
3491 rt
= import_table
->imported_vpn
[afi
];
3495 flog_err(EC_LIB_DEVELOPMENT
, "%s: bad afi %d", __func__
, afi
);
3500 memset(&original_nexthop
, 0, sizeof(original_nexthop
));
3503 * agg_node_lookup returns a node only if there is at least
3504 * one route attached.
3506 rn
= agg_node_lookup(rt
, p
);
3508 vnc_zlog_debug_verbose("%s: rn=%p", __func__
, rn
);
3512 RFAPI_CHECK_REFCOUNT(rn
, SAFI_MPLS_VPN
, 1);
3513 agg_unlock_node(rn
); /* undo lock in agg_node_lookup */
3516 original_had_routes
= 1;
3518 if (VNC_DEBUG(VERBOSE
)) {
3519 vnc_zlog_debug_verbose("%s: showing IT node on entry",
3521 rfapiShowItNode(NULL
, rn
); /* debug */
3525 * Look for same route (will have same RD and peer)
3527 bi
= rfapiItBiIndexSearch(rn
, prd
, peer
, aux_prefix
);
3532 * This was an old test when we iterated over the
3533 * BIs linearly. Since we're now looking up with
3534 * RD and peer, comparing types should not be
3535 * needed. Changed to assertion.
3537 * Compare types. Doing so prevents a RFP-originated
3538 * route from matching an imported route, for example.
3540 if (VNC_DEBUG(VERBOSE
) && bi
->type
!= type
)
3541 /* should be handled by RDs, but warn for now */
3542 zlog_warn("%s: type mismatch! (bi=%d, arg=%d)",
3543 __func__
, bi
->type
, type
);
3545 vnc_zlog_debug_verbose("%s: found matching bi",
3549 * In the special CE table, withdrawals occur without
3552 if (import_table
== bgp
->rfapi
->it_ce
) {
3553 vnc_direct_bgp_del_route_ce(bgp
, rn
, bi
);
3554 if (action
== FIF_ACTION_WITHDRAW
)
3555 action
= FIF_ACTION_KILL
;
3558 if (action
== FIF_ACTION_WITHDRAW
) {
3561 CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
);
3563 vnc_zlog_debug_verbose(
3564 "%s: withdrawing at prefix %s/%d%s",
3565 __func__
, rfapi_ntop(rn
->p
.family
,
3570 ? " (already being withdrawn)"
3575 rfapiBiStartWithdrawTimer(
3576 import_table
, rn
, bi
, afi
,
3578 rfapiWithdrawTimerVPN
);
3580 RFAPI_UPDATE_ITABLE_COUNT(
3581 bi
, import_table
, afi
, -1);
3582 import_table
->holddown_count
[afi
] += 1;
3586 vnc_zlog_debug_verbose(
3587 "%s: %s at prefix %s/%d", __func__
,
3588 ((action
== FIF_ACTION_KILL
)
3591 rfapi_ntop(rn
->p
.family
,
3592 &rn
->p
.u
.prefix
, buf
,
3597 * If this route is waiting to be deleted
3599 * a previous withdraw, we must cancel its
3602 if (CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
)
3603 && bi
->extra
->vnc
.import
.timer
) {
3606 (struct thread
*)bi
->extra
->vnc
3608 struct rfapi_withdraw
*wcb
= t
->arg
;
3610 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
3613 import_table
->holddown_count
[afi
] -= 1;
3614 RFAPI_UPDATE_ITABLE_COUNT(
3615 bi
, import_table
, afi
, 1);
3618 * decrement remote count (if route is remote)
3620 * we are going to remove it below
3622 RFAPI_UPDATE_ITABLE_COUNT(bi
, import_table
, afi
,
3624 if (action
== FIF_ACTION_UPDATE
) {
3628 * make copy of original nexthop so we
3629 * can see if it changed
3631 rfapiGetNexthop(bi
->attr
,
3635 * remove bi without doing any export
3638 if (CHECK_FLAG(bi
->flags
,
3640 && VALID_INTERIOR_TYPE(bi
->type
))
3641 RFAPI_MONITOR_EXTERIOR(rn
)
3642 ->valid_interior_count
--;
3643 rfapiItBiIndexDel(rn
, bi
);
3644 rfapiBgpInfoDetach(rn
, bi
);
3645 rfapiMonitorEncapDelete(bi
);
3646 vnc_import_bgp_exterior_del_route_interior(
3647 bgp
, import_table
, rn
, bi
);
3648 rfapiBgpInfoFree(bi
);
3652 * remove bi and do export processing
3654 import_table
->holddown_count
[afi
] += 1;
3655 rfapiExpireVpnNow(import_table
, rn
, bi
,
3663 RFAPI_CHECK_REFCOUNT(rn
, SAFI_MPLS_VPN
, replacing
? 1 : 0);
3665 if (action
== FIF_ACTION_WITHDRAW
|| action
== FIF_ACTION_KILL
) {
3671 rfapiBgpInfoCreate(attr
, peer
, rfd
, prd
, type
, sub_type
, label
);
3674 * lookup un address in encap table
3676 ern
= agg_node_match(import_table
->imported_encap
[afi
], &vn_prefix
);
3678 rfapiCopyUnEncap2VPN(ern
->info
, info_new
);
3679 agg_unlock_node(ern
); /* undo lock in route_note_match */
3681 char bpf
[PREFIX_STRLEN
];
3683 prefix2str(&vn_prefix
, bpf
, sizeof(bpf
));
3684 /* Not a big deal, just means VPN route got here first */
3685 vnc_zlog_debug_verbose("%s: no encap route for vn addr %s",
3687 info_new
->extra
->vnc
.import
.un_family
= 0;
3695 * No need to increment reference count, so only "get"
3696 * if the node is not there already
3698 rn
= agg_node_get(rt
, p
);
3702 * For ethernet routes, if there is an accompanying IP address,
3705 if ((AFI_L2VPN
== afi
) && aux_prefix
) {
3707 vnc_zlog_debug_verbose("%s: setting BI's aux_prefix", __func__
);
3708 info_new
->extra
->vnc
.import
.aux_prefix
= *aux_prefix
;
3711 vnc_zlog_debug_verbose(
3712 "%s: inserting bi %p at prefix %s/%d #%d", __func__
, info_new
,
3713 rfapi_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, BUFSIZ
),
3714 rn
->p
.prefixlen
, rn
->lock
);
3716 rfapiBgpInfoAttachSorted(rn
, info_new
, afi
, SAFI_MPLS_VPN
);
3717 rfapiItBiIndexAdd(rn
, info_new
);
3718 if (!rfapiGetUnAddrOfVpnBi(info_new
, NULL
)) {
3719 if (VALID_INTERIOR_TYPE(info_new
->type
))
3720 RFAPI_MONITOR_EXTERIOR(rn
)->valid_interior_count
++;
3721 SET_FLAG(info_new
->flags
, BGP_INFO_VALID
);
3723 RFAPI_UPDATE_ITABLE_COUNT(info_new
, import_table
, afi
, 1);
3724 vnc_import_bgp_exterior_add_route_interior(bgp
, import_table
, rn
,
3727 if (import_table
== bgp
->rfapi
->it_ce
)
3728 vnc_direct_bgp_add_route_ce(bgp
, rn
, info_new
);
3730 if (VNC_DEBUG(VERBOSE
)) {
3731 vnc_zlog_debug_verbose("%s: showing IT node", __func__
);
3732 rfapiShowItNode(NULL
, rn
); /* debug */
3735 rfapiMonitorEncapAdd(import_table
, &vn_prefix
, rn
, info_new
);
3737 if (!rfapiGetUnAddrOfVpnBi(info_new
, &un_prefix
)) {
3740 * if we have a valid UN address (either via Encap route
3741 * or via tunnel attribute), then we should attempt
3742 * to move any monitors at less-specific nodes to this node
3744 rfapiMonitorMoveLonger(rn
);
3746 un_prefix_valid
= 1;
3750 * 101129 Enhancement: if we add a route (implication: it is not
3751 * in holddown), delete all other routes from this nve at this
3752 * node that are in holddown, regardless of peer.
3754 * Reasons it's OK to do that:
3756 * - if the holddown route being deleted originally came from BGP VPN,
3757 * it is already gone from BGP (implication of holddown), so there
3758 * won't be any added inconsistency with the BGP RIB.
3760 * - once a fresh route is added at a prefix, any routes in holddown
3761 * at that prefix will not show up in RFP responses, so deleting
3762 * the holddown routes won't affect the contents of responses.
3764 * - lifetimes are supposed to be consistent, so there should not
3765 * be a case where the fresh route has a shorter lifetime than
3766 * the holddown route, so we don't expect the fresh route to
3767 * disappear and complete its holddown time before the existing
3768 * holddown routes time out. Therefore, we won't have a situation
3769 * where we expect the existing holddown routes to be hidden and
3770 * then to reappear sometime later (as holddown routes) in a
3773 * Among other things, this would enable us to skirt the problem
3774 * of local holddown routes that refer to NVE descriptors that
3775 * have already been closed (if the same NVE triggers a subsequent
3776 * rfapi_open(), the new peer is different and doesn't match the
3777 * peer of the holddown route, so the stale holddown route still
3778 * hangs around until it times out instead of just being replaced
3779 * by the fresh route).
3782 * We know that the new bi will have been inserted before any routes
3783 * in holddown, so we can skip any that came before it
3785 for (bi
= info_new
->next
; bi
; bi
= next
) {
3787 struct prefix pfx_vn
;
3788 struct prefix pfx_un
;
3790 int remote_peer_match
= 0;
3797 if (!CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
))
3801 * Must match VN address (nexthop of VPN route)
3803 if (rfapiGetNexthop(bi
->attr
, &pfx_vn
))
3805 if (!prefix_same(&pfx_vn
, &vn_prefix
))
3808 if (un_prefix_valid
&& /* new route UN addr */
3809 !rfapiGetUnAddrOfVpnBi(bi
, &pfx_un
)
3810 && /* old route UN addr */
3811 prefix_same(&pfx_un
, &un_prefix
)) { /* compare */
3814 if (!RFAPI_LOCAL_BI(bi
) && !RFAPI_LOCAL_BI(info_new
)
3815 && sockunion_same(&bi
->peer
->su
, &info_new
->peer
->su
)) {
3816 /* old & new are both remote, same peer */
3817 remote_peer_match
= 1;
3820 if (!un_match
& !remote_peer_match
)
3823 vnc_zlog_debug_verbose(
3824 "%s: removing holddown bi matching NVE of new route",
3826 if (bi
->extra
->vnc
.import
.timer
) {
3828 (struct thread
*)bi
->extra
->vnc
.import
.timer
;
3829 struct rfapi_withdraw
*wcb
= t
->arg
;
3831 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
3834 rfapiExpireVpnNow(import_table
, rn
, bi
, 0);
3837 if (!original_had_routes
) {
3839 * We went from 0 usable routes to 1 usable route. Perform the
3840 * "Adding a Route" export process.
3842 vnc_direct_bgp_add_prefix(bgp
, import_table
, rn
);
3843 vnc_zebra_add_prefix(bgp
, import_table
, rn
);
3846 * Check for nexthop change event
3847 * Note: the prefix_same() test below detects two situations:
3848 * 1. route is replaced, new route has different nexthop
3849 * 2. new route is added (original_nexthop is 0)
3851 struct prefix new_nexthop
;
3853 rfapiGetNexthop(attr
, &new_nexthop
);
3854 if (!prefix_same(&original_nexthop
, &new_nexthop
)) {
3856 * nexthop change event
3857 * vnc_direct_bgp_add_prefix() will recompute VN addr
3860 vnc_direct_bgp_add_prefix(bgp
, import_table
, rn
);
3864 if (!(bgp
->rfapi_cfg
->flags
& BGP_VNC_CONFIG_CALLBACK_DISABLE
)) {
3865 for (n
= rn
; n
; n
= agg_node_parent(n
)) {
3866 // rfapiDoRouteCallback(import_table, n, NULL);
3868 rfapiMonitorItNodeChanged(import_table
, rn
, NULL
);
3870 RFAPI_CHECK_REFCOUNT(rn
, SAFI_MPLS_VPN
, 0);
3874 static void rfapiBgpInfoFilteredImportBadSafi(
3875 struct rfapi_import_table
*import_table
, int action
, struct peer
*peer
,
3876 void *rfd
, /* set for looped back routes */
3878 struct prefix
*aux_prefix
, /* AFI_L2VPN: optional IP */
3879 afi_t afi
, struct prefix_rd
*prd
,
3880 struct attr
*attr
, /* part of bgp_info */
3881 uint8_t type
, /* part of bgp_info */
3882 uint8_t sub_type
, /* part of bgp_info */
3883 uint32_t *label
) /* part of bgp_info */
3885 vnc_zlog_debug_verbose("%s: Error, bad safi", __func__
);
3888 static rfapi_bi_filtered_import_f
*
3889 rfapiBgpInfoFilteredImportFunction(safi_t safi
)
3893 return rfapiBgpInfoFilteredImportVPN
;
3896 return rfapiBgpInfoFilteredImportEncap
;
3900 flog_err(EC_LIB_DEVELOPMENT
, "%s: bad safi %d", __func__
,
3902 return rfapiBgpInfoFilteredImportBadSafi
;
3906 void rfapiProcessUpdate(struct peer
*peer
,
3907 void *rfd
, /* set when looped from RFP/RFAPI */
3908 struct prefix
*p
, struct prefix_rd
*prd
,
3909 struct attr
*attr
, afi_t afi
, safi_t safi
, uint8_t type
,
3910 uint8_t sub_type
, uint32_t *label
)
3914 struct rfapi_import_table
*it
;
3915 int has_ip_route
= 1;
3918 bgp
= bgp_get_default(); /* assume 1 instance for now */
3925 * look at high-order byte of RD. FF means MAC
3926 * address is present (VNC L2VPN)
3928 if ((safi
== SAFI_MPLS_VPN
)
3929 && (decode_rd_type(prd
->val
) == RD_TYPE_VNC_ETH
)) {
3930 struct prefix pfx_mac_buf
;
3931 struct prefix pfx_nexthop_buf
;
3935 * Set flag if prefix and nexthop are the same - don't
3936 * add the route to normal IP-based import tables
3938 if (!rfapiGetNexthop(attr
, &pfx_nexthop_buf
)) {
3939 if (!prefix_cmp(&pfx_nexthop_buf
, p
)) {
3944 memset(&pfx_mac_buf
, 0, sizeof(pfx_mac_buf
));
3945 pfx_mac_buf
.family
= AF_ETHERNET
;
3946 pfx_mac_buf
.prefixlen
= 48;
3947 memcpy(&pfx_mac_buf
.u
.prefix_eth
.octet
, prd
->val
+ 2, 6);
3950 * Find rt containing LNI (Logical Network ID), which
3951 * _should_ always be present when mac address is present
3953 rc
= rfapiEcommunityGetLNI(attr
->ecommunity
, &lni
);
3955 vnc_zlog_debug_verbose(
3956 "%s: rfapiEcommunityGetLNI returned %d, lni=%d, attr=%p",
3957 __func__
, rc
, lni
, attr
);
3959 it
= rfapiMacImportTableGet(bgp
, lni
);
3961 rfapiBgpInfoFilteredImportVPN(
3962 it
, FIF_ACTION_UPDATE
, peer
, rfd
,
3963 &pfx_mac_buf
, /* prefix */
3964 p
, /* aux prefix: IP addr */
3965 AFI_L2VPN
, prd
, attr
, type
, sub_type
, label
);
3973 * Iterate over all import tables; do a filtered import
3974 * for the afi/safi combination
3976 for (it
= h
->imports
; it
; it
= it
->next
) {
3977 (*rfapiBgpInfoFilteredImportFunction(safi
))(
3978 it
, FIF_ACTION_UPDATE
, peer
, rfd
, p
, /* prefix */
3979 NULL
, afi
, prd
, attr
, type
, sub_type
, label
);
3982 if (safi
== SAFI_MPLS_VPN
) {
3983 vnc_direct_bgp_rh_add_route(bgp
, afi
, p
, peer
, attr
);
3984 rfapiBgpInfoFilteredImportVPN(
3985 bgp
->rfapi
->it_ce
, FIF_ACTION_UPDATE
, peer
, rfd
,
3987 NULL
, afi
, prd
, attr
, type
, sub_type
, label
);
3992 void rfapiProcessWithdraw(struct peer
*peer
, void *rfd
, struct prefix
*p
,
3993 struct prefix_rd
*prd
, struct attr
*attr
, afi_t afi
,
3994 safi_t safi
, uint8_t type
, int kill
)
3998 struct rfapi_import_table
*it
;
4000 bgp
= bgp_get_default(); /* assume 1 instance for now */
4007 * look at high-order byte of RD. FF means MAC
4008 * address is present (VNC L2VPN)
4010 if (h
->import_mac
!= NULL
&& safi
== SAFI_MPLS_VPN
4011 && decode_rd_type(prd
->val
) == RD_TYPE_VNC_ETH
) {
4012 struct prefix pfx_mac_buf
;
4013 void *cursor
= NULL
;
4016 memset(&pfx_mac_buf
, 0, sizeof(pfx_mac_buf
));
4017 pfx_mac_buf
.family
= AF_ETHERNET
;
4018 pfx_mac_buf
.prefixlen
= 48;
4019 memcpy(&pfx_mac_buf
.u
.prefix_eth
, prd
->val
+ 2, 6);
4022 * withdraw does not contain attrs, so we don't have
4023 * access to the route's LNI, which would ordinarily
4024 * select the specific mac-based import table. Instead,
4025 * we must iterate over all mac-based tables and rely
4026 * on the RD to match.
4028 * If this approach is too slow, add an index where
4029 * key is {RD, peer} and value is the import table
4031 for (rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4033 rc
== 0; rc
= skiplist_next(h
->import_mac
, NULL
,
4034 (void **)&it
, &cursor
)) {
4037 vnc_zlog_debug_verbose(
4038 "%s: calling rfapiBgpInfoFilteredImportVPN(it=%p, afi=AFI_L2VPN)",
4042 rfapiBgpInfoFilteredImportVPN(
4044 (kill
? FIF_ACTION_KILL
: FIF_ACTION_WITHDRAW
),
4045 peer
, rfd
, &pfx_mac_buf
, /* prefix */
4046 p
, /* aux_prefix: IP */
4047 AFI_L2VPN
, prd
, attr
, type
, 0,
4048 NULL
); /* sub_type & label unused for withdraw
4054 * XXX For the case where the withdraw involves an L2
4055 * route with no IP information, we rely on the lack
4056 * of RT-list intersection to filter out the withdraw
4057 * from the IP-based import tables below
4061 * Iterate over all import tables; do a filtered import
4062 * for the afi/safi combination
4065 for (it
= h
->imports
; it
; it
= it
->next
) {
4066 (*rfapiBgpInfoFilteredImportFunction(safi
))(
4067 it
, (kill
? FIF_ACTION_KILL
: FIF_ACTION_WITHDRAW
),
4068 peer
, rfd
, p
, /* prefix */
4069 NULL
, afi
, prd
, attr
, type
, 0,
4070 NULL
); /* sub_type & label unused for withdraw */
4073 /* TBD the deletion should happen after the lifetime expires */
4074 if (safi
== SAFI_MPLS_VPN
)
4075 vnc_direct_bgp_rh_del_route(bgp
, afi
, p
, peer
);
4077 if (safi
== SAFI_MPLS_VPN
) {
4078 rfapiBgpInfoFilteredImportVPN(
4080 (kill
? FIF_ACTION_KILL
: FIF_ACTION_WITHDRAW
), peer
,
4081 rfd
, p
, /* prefix */
4082 NULL
, afi
, prd
, attr
, type
, 0,
4083 NULL
); /* sub_type & label unused for withdraw */
4088 * TBD optimized withdraw timer algorithm for case of many
4089 * routes expiring at the same time due to peer drop.
4092 * 1. Visit all BIs in all ENCAP import tables.
4094 * a. If a bi's peer is the failed peer, remove the bi.
4095 * b. If the removed ENCAP bi was first in the list of
4096 * BIs at this ENCAP node, loop over all monitors
4099 * (1) for each ENCAP monitor, loop over all its
4100 * VPN node monitors and set their RFAPI_MON_FLAG_NEEDCALLBACK
4103 * 2. Visit all BIs in all VPN import tables.
4104 * a. If a bi's peer is the failed peer, remove the bi.
4105 * b. loop over all the VPN node monitors and set their
4106 * RFAPI_MON_FLAG_NEEDCALLBACK flags
4107 * c. If there are no BIs left at this VPN node,
4112 /* surprise, this gets called from peer_delete(), from rfapi_close() */
4113 static void rfapiProcessPeerDownRt(struct peer
*peer
,
4114 struct rfapi_import_table
*import_table
,
4115 afi_t afi
, safi_t safi
)
4117 struct agg_node
*rn
;
4118 struct bgp_info
*bi
;
4119 struct agg_table
*rt
;
4120 int (*timer_service_func
)(struct thread
*);
4122 assert(afi
== AFI_IP
|| afi
== AFI_IP6
);
4128 rt
= import_table
->imported_vpn
[afi
];
4129 timer_service_func
= rfapiWithdrawTimerVPN
;
4132 rt
= import_table
->imported_encap
[afi
];
4133 timer_service_func
= rfapiWithdrawTimerEncap
;
4140 for (rn
= agg_route_top(rt
); rn
; rn
= agg_route_next(rn
)) {
4141 for (bi
= rn
->info
; bi
; bi
= bi
->next
) {
4142 if (bi
->peer
== peer
) {
4144 if (CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
)) {
4145 /* already in holddown, skip */
4149 if (safi
== SAFI_MPLS_VPN
) {
4150 RFAPI_UPDATE_ITABLE_COUNT(
4151 bi
, import_table
, afi
, -1);
4152 import_table
->holddown_count
[afi
] += 1;
4154 rfapiBiStartWithdrawTimer(import_table
, rn
, bi
,
4156 timer_service_func
);
4164 * This gets called when a peer connection drops. We have to remove
4165 * all the routes from this peer.
4167 * Current approach is crude. TBD Optimize by setting fewer timers and
4168 * grouping withdrawn routes so we can generate callbacks more
4171 void rfapiProcessPeerDown(struct peer
*peer
)
4175 struct rfapi_import_table
*it
;
4178 * If this peer is a "dummy" peer structure atached to a RFAPI
4179 * nve_descriptor, we don't need to walk the import tables
4180 * because the routes are already withdrawn by rfapi_close()
4182 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_IS_RFAPI_HD
))
4186 * 1. Visit all BIs in all ENCAP import tables.
4187 * Start withdraw timer on the BIs that match peer.
4189 * 2. Visit All BIs in all VPN import tables.
4190 * Start withdraw timer on the BIs that match peer.
4193 bgp
= bgp_get_default(); /* assume 1 instance for now */
4200 for (it
= h
->imports
; it
; it
= it
->next
) {
4201 rfapiProcessPeerDownRt(peer
, it
, AFI_IP
, SAFI_ENCAP
);
4202 rfapiProcessPeerDownRt(peer
, it
, AFI_IP6
, SAFI_ENCAP
);
4203 rfapiProcessPeerDownRt(peer
, it
, AFI_IP
, SAFI_MPLS_VPN
);
4204 rfapiProcessPeerDownRt(peer
, it
, AFI_IP6
, SAFI_MPLS_VPN
);
4208 rfapiProcessPeerDownRt(peer
, h
->it_ce
, AFI_IP
, SAFI_MPLS_VPN
);
4209 rfapiProcessPeerDownRt(peer
, h
->it_ce
, AFI_IP6
, SAFI_MPLS_VPN
);
4214 * Import an entire RIB (for an afi/safi) to an import table RIB,
4215 * filtered according to the import table's RT list
4217 * TBD: does this function need additions to match rfapiProcessUpdate()
4218 * for, e.g., L2 handling?
4220 static void rfapiBgpTableFilteredImport(struct bgp
*bgp
,
4221 struct rfapi_import_table
*it
,
4222 afi_t afi
, safi_t safi
)
4224 struct bgp_node
*rn1
;
4225 struct bgp_node
*rn2
;
4227 /* Only these SAFIs have 2-level RIBS */
4228 assert(safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
);
4231 * Now visit all the rd nodes and the nodes of all the
4232 * route tables attached to them, and import the routes
4233 * if they have matching route targets
4235 for (rn1
= bgp_table_top(bgp
->rib
[afi
][safi
]); rn1
;
4236 rn1
= bgp_route_next(rn1
)) {
4239 for (rn2
= bgp_table_top(rn1
->info
); rn2
;
4240 rn2
= bgp_route_next(rn2
)) {
4242 struct bgp_info
*bi
;
4244 for (bi
= rn2
->info
; bi
; bi
= bi
->next
) {
4247 if (CHECK_FLAG(bi
->flags
,
4252 label
= decode_label(
4253 &bi
->extra
->label
[0]);
4254 (*rfapiBgpInfoFilteredImportFunction(
4256 it
, /* which import table */
4257 FIF_ACTION_UPDATE
, bi
->peer
,
4258 NULL
, &rn2
->p
, /* prefix */
4260 (struct prefix_rd
*)&rn1
->p
,
4262 bi
->sub_type
, &label
);
4270 /* per-bgp-instance rfapi data */
4271 struct rfapi
*bgp_rfapi_new(struct bgp
*bgp
)
4275 struct rfapi_rfp_cfg
*cfg
= NULL
;
4276 struct rfapi_rfp_cb_methods
*cbm
= NULL
;
4278 assert(bgp
->rfapi_cfg
== NULL
);
4280 h
= (struct rfapi
*)XCALLOC(MTYPE_RFAPI
, sizeof(struct rfapi
));
4282 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
4283 h
->un
[afi
] = agg_table_init();
4287 * initialize the ce import table
4289 h
->it_ce
= XCALLOC(MTYPE_RFAPI_IMPORTTABLE
,
4290 sizeof(struct rfapi_import_table
));
4291 h
->it_ce
->imported_vpn
[AFI_IP
] = agg_table_init();
4292 h
->it_ce
->imported_vpn
[AFI_IP6
] = agg_table_init();
4293 h
->it_ce
->imported_encap
[AFI_IP
] = agg_table_init();
4294 h
->it_ce
->imported_encap
[AFI_IP6
] = agg_table_init();
4295 rfapiBgpTableFilteredImport(bgp
, h
->it_ce
, AFI_IP
, SAFI_MPLS_VPN
);
4296 rfapiBgpTableFilteredImport(bgp
, h
->it_ce
, AFI_IP6
, SAFI_MPLS_VPN
);
4299 * Set up work queue for deferred rfapi_close operations
4301 h
->deferred_close_q
=
4302 work_queue_new(bm
->master
, "rfapi deferred close");
4303 h
->deferred_close_q
->spec
.workfunc
= rfapi_deferred_close_workfunc
;
4304 h
->deferred_close_q
->spec
.data
= h
;
4306 h
->rfp
= rfp_start(bm
->master
, &cfg
, &cbm
);
4307 bgp
->rfapi_cfg
= bgp_rfapi_cfg_new(cfg
);
4309 h
->rfp_methods
= *cbm
;
4314 void bgp_rfapi_destroy(struct bgp
*bgp
, struct rfapi
*h
)
4318 if (bgp
== NULL
|| h
== NULL
)
4321 if (h
->resolve_nve_nexthop
) {
4322 skiplist_free(h
->resolve_nve_nexthop
);
4323 h
->resolve_nve_nexthop
= NULL
;
4326 agg_table_finish(h
->it_ce
->imported_vpn
[AFI_IP
]);
4327 agg_table_finish(h
->it_ce
->imported_vpn
[AFI_IP6
]);
4328 agg_table_finish(h
->it_ce
->imported_encap
[AFI_IP
]);
4329 agg_table_finish(h
->it_ce
->imported_encap
[AFI_IP6
]);
4331 if (h
->import_mac
) {
4332 struct rfapi_import_table
*it
;
4337 rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4339 !rc
; rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4342 rfapiImportTableFlush(it
);
4343 XFREE(MTYPE_RFAPI_IMPORTTABLE
, it
);
4345 skiplist_free(h
->import_mac
);
4346 h
->import_mac
= NULL
;
4349 work_queue_free_and_null(&h
->deferred_close_q
);
4354 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
4355 agg_table_finish(h
->un
[afi
]);
4358 XFREE(MTYPE_RFAPI_IMPORTTABLE
, h
->it_ce
);
4359 XFREE(MTYPE_RFAPI
, h
);
4362 struct rfapi_import_table
*
4363 rfapiImportTableRefAdd(struct bgp
*bgp
, struct ecommunity
*rt_import_list
,
4364 struct rfapi_nve_group_cfg
*rfg
)
4367 struct rfapi_import_table
*it
;
4373 for (it
= h
->imports
; it
; it
= it
->next
) {
4374 if (ecommunity_cmp(it
->rt_import_list
, rt_import_list
))
4378 vnc_zlog_debug_verbose("%s: matched it=%p", __func__
, it
);
4381 it
= XCALLOC(MTYPE_RFAPI_IMPORTTABLE
,
4382 sizeof(struct rfapi_import_table
));
4384 it
->next
= h
->imports
;
4387 it
->rt_import_list
= ecommunity_dup(rt_import_list
);
4389 it
->monitor_exterior_orphans
=
4390 skiplist_new(0, NULL
, (void (*)(void *))prefix_free
);
4393 * fill import route tables from RIBs
4395 * Potential area for optimization. If this occurs when
4396 * tables are large (e.g., the operator adds a nve group
4397 * with a new RT list to a running system), it could take
4401 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
4403 it
->imported_vpn
[afi
] = agg_table_init();
4404 it
->imported_encap
[afi
] = agg_table_init();
4406 rfapiBgpTableFilteredImport(bgp
, it
, afi
,
4408 rfapiBgpTableFilteredImport(bgp
, it
, afi
, SAFI_ENCAP
);
4410 vnc_import_bgp_exterior_redist_enable_it(bgp
, afi
, it
);
4420 * skiplist element free function
4422 static void delete_rem_pfx_na_free(void *na
)
4424 uint32_t *pCounter
= ((struct rfapi_nve_addr
*)na
)->info
;
4427 XFREE(MTYPE_RFAPI_NVE_ADDR
, na
);
4431 * Common deleter for IP and MAC import tables
4433 static void rfapiDeleteRemotePrefixesIt(
4434 struct bgp
*bgp
, struct rfapi_import_table
*it
, struct prefix
*un
,
4435 struct prefix
*vn
, struct prefix
*p
, int delete_active
,
4436 int delete_holddown
, uint32_t *pARcount
, uint32_t *pAHcount
,
4437 uint32_t *pHRcount
, uint32_t *pHHcount
,
4438 struct skiplist
*uniq_active_nves
, struct skiplist
*uniq_holddown_nves
)
4444 char buf_pfx
[PREFIX_STRLEN
];
4447 prefix2str(p
, buf_pfx
, sizeof(buf_pfx
));
4453 vnc_zlog_debug_verbose(
4454 "%s: entry, p=%s, delete_active=%d, delete_holddown=%d",
4455 __func__
, buf_pfx
, delete_active
, delete_holddown
);
4459 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
4461 struct agg_table
*rt
;
4462 struct agg_node
*rn
;
4464 if (p
&& (family2afi(p
->family
) != afi
)) {
4468 rt
= it
->imported_vpn
[afi
];
4472 vnc_zlog_debug_verbose("%s: scanning rt for afi=%d", __func__
,
4475 for (rn
= agg_route_top(rt
); rn
; rn
= agg_route_next(rn
)) {
4476 struct bgp_info
*bi
;
4477 struct bgp_info
*next
;
4479 if (VNC_DEBUG(IMPORT_DEL_REMOTE
)) {
4480 char p1line
[PREFIX_STRLEN
];
4481 char p2line
[PREFIX_STRLEN
];
4483 prefix2str(p
, p1line
, sizeof(p1line
));
4484 prefix2str(&rn
->p
, p2line
, sizeof(p2line
));
4485 vnc_zlog_debug_any("%s: want %s, have %s",
4486 __func__
, p1line
, p2line
);
4489 if (p
&& prefix_cmp(p
, &rn
->p
))
4493 char buf_pfx
[PREFIX_STRLEN
];
4495 prefix2str(&rn
->p
, buf_pfx
, sizeof(buf_pfx
));
4496 vnc_zlog_debug_verbose("%s: rn pfx=%s",
4500 /* TBD is this valid for afi == AFI_L2VPN? */
4501 RFAPI_CHECK_REFCOUNT(rn
, SAFI_MPLS_VPN
, 1);
4503 for (bi
= rn
->info
; bi
; bi
= next
) {
4512 vnc_zlog_debug_verbose("%s: examining bi %p",
4516 if (!rfapiGetNexthop(bi
->attr
, &qpt
))
4521 || !prefix_match(vn
, &qpt
)) {
4523 vnc_zlog_debug_verbose(
4524 "%s: continue at vn && !qpt_valid || !prefix_match(vn, &qpt)",
4531 if (!rfapiGetUnAddrOfVpnBi(bi
, &qct
))
4536 || !prefix_match(un
, &qct
)) {
4538 vnc_zlog_debug_verbose(
4539 "%s: continue at un && !qct_valid || !prefix_match(un, &qct)",
4551 * If this route is waiting to be deleted
4553 * a previous withdraw, we must cancel its
4556 if (CHECK_FLAG(bi
->flags
, BGP_INFO_REMOVED
)) {
4557 if (!delete_holddown
)
4559 if (bi
->extra
->vnc
.import
.timer
) {
4565 struct rfapi_withdraw
*wcb
=
4569 ->holddown_count
[afi
] -=
4571 RFAPI_UPDATE_ITABLE_COUNT(
4572 bi
, wcb
->import_table
,
4574 XFREE(MTYPE_RFAPI_WITHDRAW
,
4584 vnc_zlog_debug_verbose(
4585 "%s: deleting bi %p (qct_valid=%d, qpt_valid=%d, delete_holddown=%d, delete_active=%d)",
4586 __func__
, bi
, qct_valid
, qpt_valid
,
4587 delete_holddown
, delete_active
);
4593 if (qct_valid
&& qpt_valid
) {
4595 struct rfapi_nve_addr na
;
4596 struct rfapi_nve_addr
*nap
;
4598 memset(&na
, 0, sizeof(na
));
4599 assert(!rfapiQprefix2Raddr(&qct
,
4601 assert(!rfapiQprefix2Raddr(&qpt
,
4604 if (skiplist_search(
4607 : uniq_holddown_nves
),
4608 &na
, (void **)&nap
)) {
4612 MTYPE_RFAPI_NVE_ADDR
,
4617 nap
->info
= is_active
4623 : uniq_holddown_nves
),
4626 rfapiNveAddr2Str(nap
, line
,
4631 vnc_direct_bgp_rh_del_route(bgp
, afi
, &rn
->p
,
4634 RFAPI_UPDATE_ITABLE_COUNT(bi
, it
, afi
, -1);
4635 it
->holddown_count
[afi
] += 1;
4636 rfapiExpireVpnNow(it
, rn
, bi
, 1);
4638 vnc_zlog_debug_verbose(
4639 "%s: incrementing count (is_active=%d)",
4640 __func__
, is_active
);
4653 * For use by the "clear vnc prefixes" command
4655 /*------------------------------------------
4656 * rfapiDeleteRemotePrefixes
4658 * UI helper: For use by the "clear vnc prefixes" command
4661 * un if set, tunnel must match this prefix
4662 * vn if set, nexthop prefix must match this prefix
4663 * p if set, prefix must match this prefix
4664 * it if set, only look in this import table
4667 * pARcount number of active routes deleted
4668 * pAHcount number of active nves deleted
4669 * pHRcount number of holddown routes deleted
4670 * pHHcount number of holddown nves deleted
4674 --------------------------------------------*/
4675 void rfapiDeleteRemotePrefixes(struct prefix
*un
, struct prefix
*vn
,
4677 struct rfapi_import_table
*arg_it
,
4678 int delete_active
, int delete_holddown
,
4679 uint32_t *pARcount
, uint32_t *pAHcount
,
4680 uint32_t *pHRcount
, uint32_t *pHHcount
)
4684 struct rfapi_import_table
*it
;
4685 uint32_t deleted_holddown_route_count
= 0;
4686 uint32_t deleted_active_route_count
= 0;
4687 uint32_t deleted_holddown_nve_count
= 0;
4688 uint32_t deleted_active_nve_count
= 0;
4689 struct skiplist
*uniq_holddown_nves
;
4690 struct skiplist
*uniq_active_nves
;
4694 bgp
= bgp_get_default(); /* assume 1 instance for now */
4695 /* If no bgp instantiated yet, no vnc prefixes exist */
4702 uniq_holddown_nves
=
4703 skiplist_new(0, rfapi_nve_addr_cmp
, delete_rem_pfx_na_free
);
4705 skiplist_new(0, rfapi_nve_addr_cmp
, delete_rem_pfx_na_free
);
4708 * Iterate over all import tables; do a filtered import
4709 * for the afi/safi combination
4718 vnc_zlog_debug_verbose(
4719 "%s: calling rfapiDeleteRemotePrefixesIt() on (IP) import %p",
4722 rfapiDeleteRemotePrefixesIt(
4723 bgp
, it
, un
, vn
, p
, delete_active
, delete_holddown
,
4724 &deleted_active_route_count
, &deleted_active_nve_count
,
4725 &deleted_holddown_route_count
,
4726 &deleted_holddown_nve_count
, uniq_active_nves
,
4727 uniq_holddown_nves
);
4736 * Now iterate over L2 import tables
4738 if (h
->import_mac
&& !(p
&& (p
->family
!= AF_ETHERNET
))) {
4740 void *cursor
= NULL
;
4744 rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4746 !rc
; rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4749 vnc_zlog_debug_verbose(
4750 "%s: calling rfapiDeleteRemotePrefixesIt() on import_mac %p",
4753 rfapiDeleteRemotePrefixesIt(
4754 bgp
, it
, un
, vn
, p
, delete_active
,
4755 delete_holddown
, &deleted_active_route_count
,
4756 &deleted_active_nve_count
,
4757 &deleted_holddown_route_count
,
4758 &deleted_holddown_nve_count
, uniq_active_nves
,
4759 uniq_holddown_nves
);
4764 * our custom element freeing function above counts as it deletes
4766 skiplist_free(uniq_holddown_nves
);
4767 skiplist_free(uniq_active_nves
);
4770 *pARcount
= deleted_active_route_count
;
4772 *pAHcount
= deleted_active_nve_count
;
4774 *pHRcount
= deleted_holddown_route_count
;
4776 *pHHcount
= deleted_holddown_nve_count
;
4781 /*------------------------------------------
4782 * rfapiCountRemoteRoutes
4784 * UI helper: count VRF routes from BGP side
4789 * pALRcount count of active local routes
4790 * pARRcount count of active remote routes
4791 * pHRcount count of holddown routes
4792 * pIRcount count of direct imported routes
4796 --------------------------------------------*/
4797 void rfapiCountAllItRoutes(int *pALRcount
, /* active local routes */
4798 int *pARRcount
, /* active remote routes */
4799 int *pHRcount
, /* holddown routes */
4800 int *pIRcount
) /* imported routes */
4804 struct rfapi_import_table
*it
;
4807 int total_active_local
= 0;
4808 int total_active_remote
= 0;
4809 int total_holddown
= 0;
4810 int total_imported
= 0;
4812 bgp
= bgp_get_default(); /* assume 1 instance for now */
4819 * Iterate over all import tables; do a filtered import
4820 * for the afi/safi combination
4823 for (it
= h
->imports
; it
; it
= it
->next
) {
4825 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
4827 total_active_local
+= it
->local_count
[afi
];
4828 total_active_remote
+= it
->remote_count
[afi
];
4829 total_holddown
+= it
->holddown_count
[afi
];
4830 total_imported
+= it
->imported_count
[afi
];
4837 if (h
->import_mac
) {
4839 rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4841 !rc
; rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4844 total_active_local
+= it
->local_count
[AFI_L2VPN
];
4845 total_active_remote
+= it
->remote_count
[AFI_L2VPN
];
4846 total_holddown
+= it
->holddown_count
[AFI_L2VPN
];
4847 total_imported
+= it
->imported_count
[AFI_L2VPN
];
4853 *pALRcount
= total_active_local
;
4856 *pARRcount
= total_active_remote
;
4859 *pHRcount
= total_holddown
;
4862 *pIRcount
= total_imported
;
4866 /*------------------------------------------
4867 * rfapiGetHolddownFromLifetime
4869 * calculate holddown value based on lifetime
4875 * Holddown value based on lifetime, holddown_factor,
4876 * and RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY
4878 --------------------------------------------*/
4879 /* hold down time maxes out at RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY */
4880 uint32_t rfapiGetHolddownFromLifetime(uint32_t lifetime
)
4885 bgp
= bgp_get_default();
4886 if (bgp
&& bgp
->rfapi_cfg
)
4887 factor
= bgp
->rfapi_cfg
->rfp_cfg
.holddown_factor
;
4889 factor
= RFAPI_RFP_CFG_DEFAULT_HOLDDOWN_FACTOR
;
4891 if (factor
< 100 || lifetime
< RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY
)
4892 lifetime
= lifetime
* factor
/ 100;
4893 if (lifetime
< RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY
)
4896 return RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY
;