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_path_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(void)
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
)) {
153 struct bgp_path_info
*bpi
;
154 struct bgp_path_info
*next
;
156 for (bpi
= rn
->info
; bpi
; bpi
= next
) {
159 if (CHECK_FLAG(bpi
->flags
,
164 if (RFAPI_LOCAL_BI(bpi
)) {
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 #ifdef 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_bpi
= 0;
220 unsigned int count_monitor
= 0;
221 struct bgp_path_info
*bpi
;
222 struct rfapi_monitor_encap
*hme
;
223 struct rfapi_monitor_vpn
*hmv
;
225 for (bpi
= rn
->info
; bpi
; bpi
= bpi
->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_bpi
+ count_monitor
+ lockoffset
!= rn
->lock
) {
269 vnc_zlog_debug_verbose(
270 "%s: count_bpi=%d, count_monitor=%d, lockoffset=%d, rn->lock=%d",
271 __func__
, count_bpi
, 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 * Look for UN address in Encap attribute
380 int rfapiGetVncTunnelUnAddr(struct attr
*attr
, struct prefix
*p
)
382 struct bgp_attr_encap_subtlv
*pEncap
;
383 bgp_encap_types tun_type
= BGP_ENCAP_TYPE_MPLS
;/*Default tunnel type*/
385 bgp_attr_extcom_tunnel_type(attr
, &tun_type
);
386 if (tun_type
== BGP_ENCAP_TYPE_MPLS
) {
389 /* MPLS carries UN address in next hop */
390 rfapiNexthop2Prefix(attr
, p
);
397 for (pEncap
= attr
->encap_subtlvs
; pEncap
;
398 pEncap
= pEncap
->next
) {
401 == BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT
) { /* un
404 switch (pEncap
->length
) {
409 memcpy(p
->u
.val
, pEncap
->value
,
416 p
->family
= AF_INET6
;
418 memcpy(p
->u
.val
, pEncap
->value
,
431 * Get UN address wherever it might be
433 int rfapiGetUnAddrOfVpnBi(struct bgp_path_info
*bpi
, struct prefix
*p
)
435 /* If it's in this route's VNC attribute, we're done */
436 if (!rfapiGetVncTunnelUnAddr(bpi
->attr
, p
))
439 * Otherwise, see if it's cached from a corresponding ENCAP SAFI
443 switch (bpi
->extra
->vnc
.import
.un_family
) {
446 p
->family
= bpi
->extra
->vnc
.import
.un_family
;
447 p
->u
.prefix4
= bpi
->extra
->vnc
.import
.un
.addr4
;
453 p
->family
= bpi
->extra
->vnc
.import
.un_family
;
454 p
->u
.prefix6
= bpi
->extra
->vnc
.import
.un
.addr6
;
461 #ifdef DEBUG_ENCAP_MONITOR
462 vnc_zlog_debug_verbose(
463 "%s: bpi->extra->vnc.import.un_family is 0, no UN addr",
475 * Make a new bgp_path_info from gathered parameters
477 static struct bgp_path_info
*rfapiBgpInfoCreate(struct attr
*attr
,
478 struct peer
*peer
, void *rfd
,
479 struct prefix_rd
*prd
,
480 uint8_t type
, uint8_t sub_type
,
483 struct bgp_path_info
*new;
485 new = info_make(type
, sub_type
, 0, peer
, attr
, NULL
);
487 new->attr
= bgp_attr_intern(attr
);
489 bgp_path_info_extra_get(new);
491 new->extra
->vnc
.import
.rd
= *prd
;
492 rfapi_time(&new->extra
->vnc
.import
.create_time
);
495 encode_label(*label
, &new->extra
->label
[0]);
503 * Frees bgp_path_info as used in import tables (parts are not
504 * allocated exactly the way they are in the main RIBs)
506 static void rfapiBgpInfoFree(struct bgp_path_info
*goner
)
512 vnc_zlog_debug_verbose("%s: calling peer_unlock(%p), #%d",
513 __func__
, goner
->peer
,
515 peer_unlock(goner
->peer
);
518 bgp_attr_unintern(&goner
->attr
);
521 bgp_path_info_extra_free(&goner
->extra
);
522 XFREE(MTYPE_BGP_ROUTE
, goner
);
525 struct rfapi_import_table
*rfapiMacImportTableGetNoAlloc(struct bgp
*bgp
,
529 struct rfapi_import_table
*it
= NULL
;
530 uintptr_t lni_as_ptr
= lni
;
539 if (skiplist_search(h
->import_mac
, (void *)lni_as_ptr
, (void **)&it
))
545 struct rfapi_import_table
*rfapiMacImportTableGet(struct bgp
*bgp
, uint32_t lni
)
548 struct rfapi_import_table
*it
= NULL
;
549 uintptr_t lni_as_ptr
= lni
;
554 if (!h
->import_mac
) {
555 /* default cmp is good enough for LNI */
556 h
->import_mac
= skiplist_new(0, NULL
, NULL
);
559 if (skiplist_search(h
->import_mac
, (void *)lni_as_ptr
, (void **)&it
)) {
561 struct ecommunity
*enew
;
562 struct ecommunity_val eval
;
565 it
= XCALLOC(MTYPE_RFAPI_IMPORTTABLE
,
566 sizeof(struct rfapi_import_table
));
567 /* set RT list of new import table based on LNI */
568 memset((char *)&eval
, 0, sizeof(eval
));
569 eval
.val
[0] = 0; /* VNC L2VPN */
570 eval
.val
[1] = 2; /* VNC L2VPN */
571 eval
.val
[5] = (lni
>> 16) & 0xff;
572 eval
.val
[6] = (lni
>> 8) & 0xff;
573 eval
.val
[7] = (lni
>> 0) & 0xff;
575 enew
= ecommunity_new();
576 ecommunity_add_val(enew
, &eval
);
577 it
->rt_import_list
= enew
;
579 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
580 it
->imported_vpn
[afi
] = agg_table_init();
581 it
->imported_encap
[afi
] = agg_table_init();
584 it
->l2_logical_net_id
= lni
;
586 skiplist_insert(h
->import_mac
, (void *)lni_as_ptr
, it
);
594 * Implement MONITOR_MOVE_SHORTER(original_node) from
595 * RFAPI-Import-Event-Handling.txt
597 * Returns pointer to the list of moved monitors
599 static struct rfapi_monitor_vpn
*
600 rfapiMonitorMoveShorter(struct agg_node
*original_vpn_node
, int lockoffset
)
602 struct bgp_path_info
*bpi
;
603 struct agg_node
*par
;
604 struct rfapi_monitor_vpn
*m
;
605 struct rfapi_monitor_vpn
*mlast
;
606 struct rfapi_monitor_vpn
*moved
;
608 int parent_already_refcounted
= 0;
610 RFAPI_CHECK_REFCOUNT(original_vpn_node
, SAFI_MPLS_VPN
, lockoffset
);
612 #ifdef DEBUG_MONITOR_MOVE_SHORTER
614 char buf
[PREFIX_STRLEN
];
616 prefix2str(&original_vpn_node
->p
, buf
, sizeof(buf
));
617 vnc_zlog_debug_verbose("%s: called with node pfx=%s", __func__
,
623 * 1. If there is at least one bpi (either regular route or
624 * route marked as withdrawn, with a pending timer) at
625 * original_node with a valid UN address, we're done. Return.
627 for (bpi
= original_vpn_node
->info
; bpi
; bpi
= bpi
->next
) {
630 if (!rfapiGetUnAddrOfVpnBi(bpi
, &pfx
)) {
631 #ifdef DEBUG_MONITOR_MOVE_SHORTER
632 vnc_zlog_debug_verbose(
633 "%s: have valid UN at original node, no change",
641 * 2. Travel up the tree (toward less-specific prefixes) from
642 * original_node to find the first node that has at least
643 * one route (even if it is only a withdrawn route) with a
644 * valid UN address. Call this node "Node P."
646 for (par
= agg_node_parent(original_vpn_node
); par
;
647 par
= agg_node_parent(par
)) {
648 for (bpi
= par
->info
; bpi
; bpi
= bpi
->next
) {
650 if (!rfapiGetUnAddrOfVpnBi(bpi
, &pfx
)) {
659 RFAPI_CHECK_REFCOUNT(par
, SAFI_MPLS_VPN
, 0);
663 * If no less-specific routes, try to use the 0/0 node
666 const struct prefix
*p
;
667 /* this isn't necessarily 0/0 */
668 par
= agg_route_table_top(original_vpn_node
);
671 p
= agg_node_get_prefix(par
);
673 * If we got the top node but it wasn't 0/0,
676 if (par
&& p
->prefixlen
) {
677 agg_unlock_node(par
); /* maybe free */
682 ++parent_already_refcounted
;
687 * Create 0/0 node if it isn't there
690 struct prefix pfx_default
;
691 const struct prefix
*p
= agg_node_get_prefix(original_vpn_node
);
693 memset(&pfx_default
, 0, sizeof(pfx_default
));
694 pfx_default
.family
= p
->family
;
696 /* creates default node if none exists */
697 par
= agg_node_get(agg_get_table(original_vpn_node
),
699 ++parent_already_refcounted
;
703 * 3. Move each of the monitors found at original_node to Node P.
704 * These are "Moved Monitors."
709 * Attach at end so that the list pointer we return points
710 * only to the moved routes
712 for (m
= RFAPI_MONITOR_VPN(par
), mlast
= NULL
; m
;
713 mlast
= m
, m
= m
->next
)
717 moved
= mlast
->next
= RFAPI_MONITOR_VPN(original_vpn_node
);
719 moved
= RFAPI_MONITOR_VPN_W_ALLOC(par
) =
720 RFAPI_MONITOR_VPN(original_vpn_node
);
722 if (RFAPI_MONITOR_VPN(
723 original_vpn_node
)) /* check agg, so not allocated */
724 RFAPI_MONITOR_VPN_W_ALLOC(original_vpn_node
) = NULL
;
727 * update the node pointers on the monitors
729 for (m
= moved
; m
; m
= m
->next
) {
734 RFAPI_CHECK_REFCOUNT(par
, SAFI_MPLS_VPN
,
735 parent_already_refcounted
- movecount
);
736 while (movecount
> parent_already_refcounted
) {
738 ++parent_already_refcounted
;
740 while (movecount
< parent_already_refcounted
) {
741 /* unlikely, but code defensively */
742 agg_unlock_node(par
);
743 --parent_already_refcounted
;
745 RFAPI_CHECK_REFCOUNT(original_vpn_node
, SAFI_MPLS_VPN
,
746 movecount
+ lockoffset
);
747 while (movecount
--) {
748 agg_unlock_node(original_vpn_node
);
751 #ifdef DEBUG_MONITOR_MOVE_SHORTER
753 char buf
[PREFIX_STRLEN
];
755 prefix2str(&par
->p
, buf
, sizeof(buf
));
756 vnc_zlog_debug_verbose("%s: moved to node pfx=%s", __func__
,
766 * Implement MONITOR_MOVE_LONGER(new_node) from
767 * RFAPI-Import-Event-Handling.txt
769 static void rfapiMonitorMoveLonger(struct agg_node
*new_vpn_node
)
771 struct rfapi_monitor_vpn
*monitor
;
772 struct rfapi_monitor_vpn
*mlast
;
773 struct bgp_path_info
*bpi
;
774 struct agg_node
*par
;
775 const struct prefix
*new_vpn_node_p
= agg_node_get_prefix(new_vpn_node
);
777 RFAPI_CHECK_REFCOUNT(new_vpn_node
, SAFI_MPLS_VPN
, 0);
780 * Make sure we have at least one valid route at the new node
782 for (bpi
= new_vpn_node
->info
; bpi
; bpi
= bpi
->next
) {
784 if (!rfapiGetUnAddrOfVpnBi(bpi
, &pfx
))
789 vnc_zlog_debug_verbose(
790 "%s: no valid routes at node %p, so not attempting moves",
791 __func__
, new_vpn_node
);
796 * Find first parent node that has monitors
798 for (par
= agg_node_parent(new_vpn_node
); par
;
799 par
= agg_node_parent(par
)) {
800 if (RFAPI_MONITOR_VPN(par
))
805 vnc_zlog_debug_verbose(
806 "%s: no parent nodes with monitors, done", __func__
);
811 * Check each of these monitors to see of their longest-match
812 * is now the updated node. Move any such monitors to the more-
813 * specific updated node
815 for (mlast
= NULL
, monitor
= RFAPI_MONITOR_VPN(par
); monitor
;) {
817 * If new longest match for monitor prefix is the new
818 * route's prefix, move monitor to new route's prefix
820 if (prefix_match(new_vpn_node_p
, &monitor
->p
)) {
823 mlast
->next
= monitor
->next
;
825 RFAPI_MONITOR_VPN_W_ALLOC(par
) = monitor
->next
;
830 monitor
->next
= RFAPI_MONITOR_VPN(new_vpn_node
);
831 RFAPI_MONITOR_VPN_W_ALLOC(new_vpn_node
) = monitor
;
832 monitor
->node
= new_vpn_node
;
834 agg_lock_node(new_vpn_node
); /* incr refcount */
836 monitor
= mlast
? mlast
->next
: RFAPI_MONITOR_VPN(par
);
838 RFAPI_CHECK_REFCOUNT(par
, SAFI_MPLS_VPN
, 1);
839 /* decr refcount after we're done with par as this might
841 agg_unlock_node(par
);
846 monitor
= monitor
->next
;
849 RFAPI_CHECK_REFCOUNT(new_vpn_node
, SAFI_MPLS_VPN
, 0);
853 static void rfapiBgpInfoChainFree(struct bgp_path_info
*bpi
)
855 struct bgp_path_info
*next
;
860 * If there is a timer waiting to delete this bpi, cancel
861 * the timer and delete immediately
863 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
)
864 && bpi
->extra
->vnc
.import
.timer
) {
867 (struct thread
*)bpi
->extra
->vnc
.import
.timer
;
868 struct rfapi_withdraw
*wcb
= t
->arg
;
870 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
876 rfapiBgpInfoFree(bpi
);
881 static void rfapiImportTableFlush(struct rfapi_import_table
*it
)
888 ecommunity_free(&it
->rt_import_list
);
889 it
->rt_import_list
= NULL
;
891 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
895 for (rn
= agg_route_top(it
->imported_vpn
[afi
]); rn
;
896 rn
= agg_route_next(rn
)) {
898 * Each route_node has:
899 * aggregate: points to rfapi_it_extra with monitor
901 * info: points to chain of bgp_path_info
903 /* free bgp_path_info and its children */
904 rfapiBgpInfoChainFree(rn
->info
);
907 rfapiMonitorExtraFlush(SAFI_MPLS_VPN
, rn
);
910 for (rn
= agg_route_top(it
->imported_encap
[afi
]); rn
;
911 rn
= agg_route_next(rn
)) {
912 /* free bgp_path_info and its children */
913 rfapiBgpInfoChainFree(rn
->info
);
916 rfapiMonitorExtraFlush(SAFI_ENCAP
, rn
);
919 agg_table_finish(it
->imported_vpn
[afi
]);
920 agg_table_finish(it
->imported_encap
[afi
]);
922 if (it
->monitor_exterior_orphans
) {
923 skiplist_free(it
->monitor_exterior_orphans
);
927 void rfapiImportTableRefDelByIt(struct bgp
*bgp
,
928 struct rfapi_import_table
*it_target
)
931 struct rfapi_import_table
*it
;
932 struct rfapi_import_table
*prev
= NULL
;
939 for (it
= h
->imports
; it
; prev
= it
, it
= it
->next
) {
945 assert(it
->refcount
);
951 prev
->next
= it
->next
;
953 h
->imports
= it
->next
;
955 rfapiImportTableFlush(it
);
956 XFREE(MTYPE_RFAPI_IMPORTTABLE
, it
);
960 #ifdef RFAPI_REQUIRE_ENCAP_BEEC
962 * Look for magic BGP Encapsulation Extended Community value
963 * Format in RFC 5512 Sect. 4.5
965 static int rfapiEcommunitiesMatchBeec(struct ecommunity
*ecom
,
966 bgp_encap_types type
)
973 for (i
= 0; i
< (ecom
->size
* ECOMMUNITY_SIZE
); i
+= ECOMMUNITY_SIZE
) {
979 if (ep
[0] == ECOMMUNITY_ENCODE_OPAQUE
980 && ep
[1] == ECOMMUNITY_OPAQUE_SUBTYPE_ENCAP
981 && ep
[6] == ((type
&& 0xff00) >> 8)
982 && ep
[7] == (type
& 0xff)) {
991 int rfapiEcommunitiesIntersect(struct ecommunity
*e1
, struct ecommunity
*e2
)
1000 s1
= ecommunity_ecom2str(e1
, ECOMMUNITY_FORMAT_DISPLAY
, 0);
1001 s2
= ecommunity_ecom2str(e2
, ECOMMUNITY_FORMAT_DISPLAY
, 0);
1002 vnc_zlog_debug_verbose("%s: e1[%s], e2[%s]", __func__
, s1
, s2
);
1003 XFREE(MTYPE_ECOMMUNITY_STR
, s1
);
1004 XFREE(MTYPE_ECOMMUNITY_STR
, s2
);
1007 for (i
= 0; i
< e1
->size
; ++i
) {
1008 for (j
= 0; j
< e2
->size
; ++j
) {
1009 if (!memcmp(e1
->val
+ (i
* ECOMMUNITY_SIZE
),
1010 e2
->val
+ (j
* ECOMMUNITY_SIZE
),
1020 int rfapiEcommunityGetLNI(struct ecommunity
*ecom
, uint32_t *lni
)
1024 for (i
= 0; i
< ecom
->size
; ++i
) {
1025 uint8_t *p
= ecom
->val
+ (i
* ECOMMUNITY_SIZE
);
1027 if ((*(p
+ 0) == 0x00) && (*(p
+ 1) == 0x02)) {
1029 *lni
= (*(p
+ 5) << 16) | (*(p
+ 6) << 8)
1038 int rfapiEcommunityGetEthernetTag(struct ecommunity
*ecom
, uint16_t *tag_id
)
1040 struct bgp
*bgp
= bgp_get_default();
1041 *tag_id
= 0; /* default to untagged */
1044 for (i
= 0; i
< ecom
->size
; ++i
) {
1047 const uint8_t *p
= ecom
->val
+ (i
* ECOMMUNITY_SIZE
);
1049 /* High-order octet of type. */
1052 if (*p
++ == ECOMMUNITY_ROUTE_TARGET
) {
1053 if (encode
== ECOMMUNITY_ENCODE_AS4
) {
1054 p
= ptr_get_be32(p
, &as
);
1055 } else if (encode
== ECOMMUNITY_ENCODE_AS
) {
1058 p
+= 2; /* skip next two, tag/vid
1059 always in lowest bytes */
1061 if (as
== bgp
->as
) {
1062 *tag_id
= *p
++ << 8;
1072 static int rfapiVpnBiNhEqualsPt(struct bgp_path_info
*bpi
,
1073 struct rfapi_ip_addr
*hpt
)
1080 family
= BGP_MP_NEXTHOP_FAMILY(bpi
->attr
->mp_nexthop_len
);
1082 if (hpt
->addr_family
!= family
)
1087 if (bpi
->attr
->mp_nexthop_global_in
.s_addr
1088 != hpt
->addr
.v4
.s_addr
)
1093 if (IPV6_ADDR_CMP(&bpi
->attr
->mp_nexthop_global
, &hpt
->addr
.v6
))
1106 * Compare 2 VPN BIs. Return true if they have the same VN and UN addresses
1108 static int rfapiVpnBiSamePtUn(struct bgp_path_info
*bpi1
,
1109 struct bgp_path_info
*bpi2
)
1111 struct prefix pfx_un1
;
1112 struct prefix pfx_un2
;
1118 * VN address comparisons
1121 if (BGP_MP_NEXTHOP_FAMILY(bpi1
->attr
->mp_nexthop_len
)
1122 != BGP_MP_NEXTHOP_FAMILY(bpi2
->attr
->mp_nexthop_len
)) {
1126 switch (BGP_MP_NEXTHOP_FAMILY(bpi1
->attr
->mp_nexthop_len
)) {
1128 if (bpi1
->attr
->mp_nexthop_global_in
.s_addr
1129 != bpi2
->attr
->mp_nexthop_global_in
.s_addr
)
1134 if (IPV6_ADDR_CMP(&bpi1
->attr
->mp_nexthop_global
,
1135 &bpi2
->attr
->mp_nexthop_global
))
1143 memset(&pfx_un1
, 0, sizeof(pfx_un1
));
1144 memset(&pfx_un2
, 0, sizeof(pfx_un2
));
1147 * UN address comparisons
1149 if (rfapiGetVncTunnelUnAddr(bpi1
->attr
, &pfx_un1
)) {
1151 pfx_un1
.family
= bpi1
->extra
->vnc
.import
.un_family
;
1152 switch (bpi1
->extra
->vnc
.import
.un_family
) {
1155 bpi1
->extra
->vnc
.import
.un
.addr4
;
1159 bpi1
->extra
->vnc
.import
.un
.addr6
;
1168 if (rfapiGetVncTunnelUnAddr(bpi2
->attr
, &pfx_un2
)) {
1170 pfx_un2
.family
= bpi2
->extra
->vnc
.import
.un_family
;
1171 switch (bpi2
->extra
->vnc
.import
.un_family
) {
1174 bpi2
->extra
->vnc
.import
.un
.addr4
;
1178 bpi2
->extra
->vnc
.import
.un
.addr6
;
1187 if (pfx_un1
.family
== 0 || pfx_un2
.family
== 0)
1190 if (pfx_un1
.family
!= pfx_un2
.family
)
1193 switch (pfx_un1
.family
) {
1195 if (!IPV4_ADDR_SAME(&pfx_un1
.u
.prefix4
, &pfx_un2
.u
.prefix4
))
1199 if (!IPV6_ADDR_SAME(&pfx_un1
.u
.prefix6
, &pfx_un2
.u
.prefix6
))
1208 uint8_t rfapiRfpCost(struct attr
*attr
)
1210 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)) {
1211 if (attr
->local_pref
> 255) {
1214 return 255 - attr
->local_pref
;
1220 /*------------------------------------------
1223 * Find Layer 2 options in an option chain
1229 * l2o layer 2 options extracted
1233 * 1 no options found
1235 --------------------------------------------*/
1236 int rfapi_extract_l2o(
1237 struct bgp_tea_options
*pHop
, /* chain of options */
1238 struct rfapi_l2address_option
*l2o
) /* return extracted value */
1240 struct bgp_tea_options
*p
;
1242 for (p
= pHop
; p
; p
= p
->next
) {
1243 if ((p
->type
== RFAPI_VN_OPTION_TYPE_L2ADDR
)
1244 && (p
->length
>= 8)) {
1248 memcpy(&l2o
->macaddr
, v
, 6);
1250 l2o
->label
= ((v
[6] << 12) & 0xff000)
1251 + ((v
[7] << 4) & 0xff0)
1252 + ((v
[8] >> 4) & 0xf);
1254 l2o
->local_nve_id
= (uint8_t)v
[10];
1256 l2o
->logical_net_id
=
1257 (v
[11] << 16) + (v
[12] << 8) + (v
[13] << 0);
1265 static struct rfapi_next_hop_entry
*
1266 rfapiRouteInfo2NextHopEntry(struct rfapi_ip_prefix
*rprefix
,
1267 struct bgp_path_info
*bpi
, /* route to encode */
1268 uint32_t lifetime
, /* use this in nhe */
1269 struct agg_node
*rn
) /* req for L2 eth addr */
1271 struct rfapi_next_hop_entry
*new;
1272 int have_vnc_tunnel_un
= 0;
1273 const struct prefix
*p
= agg_node_get_prefix(rn
);
1275 #ifdef DEBUG_ENCAP_MONITOR
1276 vnc_zlog_debug_verbose("%s: entry, bpi %p, rn %p", __func__
, bpi
, rn
);
1279 new = XCALLOC(MTYPE_RFAPI_NEXTHOP
, sizeof(struct rfapi_next_hop_entry
));
1282 new->prefix
= *rprefix
;
1285 && decode_rd_type(bpi
->extra
->vnc
.import
.rd
.val
)
1286 == RD_TYPE_VNC_ETH
) {
1289 struct rfapi_vn_option
*vo
;
1291 vo
= XCALLOC(MTYPE_RFAPI_VN_OPTION
,
1292 sizeof(struct rfapi_vn_option
));
1295 vo
->type
= RFAPI_VN_OPTION_TYPE_L2ADDR
;
1297 memcpy(&vo
->v
.l2addr
.macaddr
, &p
->u
.prefix_eth
.octet
, ETH_ALEN
);
1298 /* only low 3 bytes of this are significant */
1299 (void)rfapiEcommunityGetLNI(bpi
->attr
->ecommunity
,
1300 &vo
->v
.l2addr
.logical_net_id
);
1301 (void)rfapiEcommunityGetEthernetTag(bpi
->attr
->ecommunity
,
1302 &vo
->v
.l2addr
.tag_id
);
1304 /* local_nve_id comes from lower byte of RD type */
1305 vo
->v
.l2addr
.local_nve_id
= bpi
->extra
->vnc
.import
.rd
.val
[1];
1307 /* label comes from MP_REACH_NLRI label */
1308 vo
->v
.l2addr
.label
= decode_label(&bpi
->extra
->label
[0]);
1310 new->vn_options
= vo
;
1313 * If there is an auxiliary prefix (i.e., host IP address),
1314 * use it as the nexthop prefix instead of the query prefix
1316 if (bpi
->extra
->vnc
.import
.aux_prefix
.family
) {
1317 rfapiQprefix2Rprefix(&bpi
->extra
->vnc
.import
.aux_prefix
,
1322 bgp_encap_types tun_type
= BGP_ENCAP_TYPE_MPLS
; /*Default*/
1323 new->prefix
.cost
= rfapiRfpCost(bpi
->attr
);
1325 struct bgp_attr_encap_subtlv
*pEncap
;
1327 switch (BGP_MP_NEXTHOP_FAMILY(bpi
->attr
->mp_nexthop_len
)) {
1329 new->vn_address
.addr_family
= AF_INET
;
1330 new->vn_address
.addr
.v4
= bpi
->attr
->mp_nexthop_global_in
;
1334 new->vn_address
.addr_family
= AF_INET6
;
1335 new->vn_address
.addr
.v6
= bpi
->attr
->mp_nexthop_global
;
1339 zlog_warn("%s: invalid vpn nexthop length: %d", __func__
,
1340 bpi
->attr
->mp_nexthop_len
);
1341 rfapi_free_next_hop_list(new);
1345 for (pEncap
= bpi
->attr
->vnc_subtlvs
; pEncap
; pEncap
= pEncap
->next
) {
1346 switch (pEncap
->type
) {
1347 case BGP_VNC_SUBTLV_TYPE_LIFETIME
:
1348 /* use configured lifetime, not attr lifetime */
1352 zlog_warn("%s: unknown VNC option type %d", __func__
,
1359 bgp_attr_extcom_tunnel_type(bpi
->attr
, &tun_type
);
1360 if (tun_type
== BGP_ENCAP_TYPE_MPLS
) {
1362 /* MPLS carries UN address in next hop */
1363 rfapiNexthop2Prefix(bpi
->attr
, &p
);
1364 if (p
.family
!= 0) {
1365 rfapiQprefix2Raddr(&p
, &new->un_address
);
1366 have_vnc_tunnel_un
= 1;
1370 for (pEncap
= bpi
->attr
->encap_subtlvs
; pEncap
; pEncap
= pEncap
->next
) {
1371 switch (pEncap
->type
) {
1372 case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT
:
1374 * Overrides ENCAP UN address, if any
1376 switch (pEncap
->length
) {
1379 new->un_address
.addr_family
= AF_INET
;
1380 memcpy(&new->un_address
.addr
.v4
, pEncap
->value
,
1382 have_vnc_tunnel_un
= 1;
1386 new->un_address
.addr_family
= AF_INET6
;
1387 memcpy(&new->un_address
.addr
.v6
, pEncap
->value
,
1389 have_vnc_tunnel_un
= 1;
1394 "%s: invalid tunnel subtlv UN addr length (%d) for bpi %p",
1395 __func__
, pEncap
->length
, bpi
);
1400 zlog_warn("%s: unknown Encap Attribute option type %d",
1401 __func__
, pEncap
->type
);
1406 new->un_options
= rfapi_encap_tlv_to_un_option(bpi
->attr
);
1408 #ifdef DEBUG_ENCAP_MONITOR
1409 vnc_zlog_debug_verbose("%s: line %d: have_vnc_tunnel_un=%d", __func__
,
1410 __LINE__
, have_vnc_tunnel_un
);
1413 if (!have_vnc_tunnel_un
&& bpi
->extra
) {
1415 * use cached UN address from ENCAP route
1417 new->un_address
.addr_family
= bpi
->extra
->vnc
.import
.un_family
;
1418 switch (new->un_address
.addr_family
) {
1420 new->un_address
.addr
.v4
=
1421 bpi
->extra
->vnc
.import
.un
.addr4
;
1424 new->un_address
.addr
.v6
=
1425 bpi
->extra
->vnc
.import
.un
.addr6
;
1428 zlog_warn("%s: invalid UN addr family (%d) for bpi %p",
1429 __func__
, new->un_address
.addr_family
, bpi
);
1430 rfapi_free_next_hop_list(new);
1435 new->lifetime
= lifetime
;
1439 int rfapiHasNonRemovedRoutes(struct agg_node
*rn
)
1441 struct bgp_path_info
*bpi
;
1443 for (bpi
= rn
->info
; bpi
; bpi
= bpi
->next
) {
1446 if (!CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
)
1447 && (bpi
->extra
&& !rfapiGetUnAddrOfVpnBi(bpi
, &pfx
))) {
1455 #ifdef DEBUG_IT_NODES
1459 void rfapiDumpNode(struct agg_node
*rn
)
1461 struct bgp_path_info
*bpi
;
1463 vnc_zlog_debug_verbose("%s: rn=%p", __func__
, rn
);
1464 for (bpi
= rn
->info
; bpi
; bpi
= bpi
->next
) {
1466 int ctrc
= rfapiGetUnAddrOfVpnBi(bpi
, &pfx
);
1469 if (!CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
)
1470 && (bpi
->extra
&& !ctrc
)) {
1477 vnc_zlog_debug_verbose(
1478 " bpi=%p, nr=%d, flags=0x%x, extra=%p, ctrc=%d", bpi
,
1479 nr
, bpi
->flags
, bpi
->extra
, ctrc
);
1484 static int rfapiNhlAddNodeRoutes(
1485 struct agg_node
*rn
, /* in */
1486 struct rfapi_ip_prefix
*rprefix
, /* in */
1487 uint32_t lifetime
, /* in */
1488 int removed
, /* in */
1489 struct rfapi_next_hop_entry
**head
, /* in/out */
1490 struct rfapi_next_hop_entry
**tail
, /* in/out */
1491 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1492 struct agg_node
*rfd_rib_node
, /* preload this NVE rib node */
1493 struct prefix
*pfx_target_original
) /* query target */
1495 struct bgp_path_info
*bpi
;
1496 struct rfapi_next_hop_entry
*new;
1497 struct prefix pfx_un
;
1498 struct skiplist
*seen_nexthops
;
1500 const struct prefix
*p
= agg_node_get_prefix(rn
);
1501 int is_l2
= (p
->family
== AF_ETHERNET
);
1504 struct agg_table
*atable
= agg_get_table(rfd_rib_node
);
1505 struct rfapi_descriptor
*rfd
;
1508 rfd
= agg_get_table_info(atable
);
1510 if (rfapiRibFTDFilterRecentPrefix(rfd
, rn
,
1511 pfx_target_original
))
1517 skiplist_new(0, vnc_prefix_cmp
, prefix_free_lists
);
1519 for (bpi
= rn
->info
; bpi
; bpi
= bpi
->next
) {
1521 struct prefix pfx_vn
;
1522 struct prefix
*newpfx
;
1524 if (removed
&& !CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
)) {
1525 #ifdef DEBUG_RETURNED_NHL
1526 vnc_zlog_debug_verbose(
1527 "%s: want holddown, this route not holddown, skip",
1532 if (!removed
&& CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
)) {
1541 * Check for excluded VN address
1543 if (rfapiVpnBiNhEqualsPt(bpi
, exclude_vnaddr
))
1547 * Check for VN address (nexthop) copied already
1550 /* L2 routes: semantic nexthop in aux_prefix; VN addr
1552 pfx_vn
= bpi
->extra
->vnc
.import
.aux_prefix
;
1554 rfapiNexthop2Prefix(bpi
->attr
, &pfx_vn
);
1556 if (!skiplist_search(seen_nexthops
, &pfx_vn
, NULL
)) {
1557 #ifdef DEBUG_RETURNED_NHL
1558 char buf
[PREFIX_STRLEN
];
1560 prefix2str(&pfx_vn
, buf
, sizeof(buf
));
1561 vnc_zlog_debug_verbose(
1562 "%s: already put VN/nexthop %s, skip", __func__
,
1568 if (rfapiGetUnAddrOfVpnBi(bpi
, &pfx_un
)) {
1569 #ifdef DEBUG_ENCAP_MONITOR
1570 vnc_zlog_debug_verbose(
1571 "%s: failed to get UN address of this VPN bpi",
1577 newpfx
= prefix_new();
1579 skiplist_insert(seen_nexthops
, newpfx
, newpfx
);
1581 new = rfapiRouteInfo2NextHopEntry(rprefix
, bpi
, lifetime
, rn
);
1583 if (rfapiRibPreloadBi(rfd_rib_node
, &pfx_vn
, &pfx_un
,
1585 /* duplicate filtered by RIB */
1586 rfapi_free_next_hop_list(new);
1593 (*tail
)->next
= new;
1602 skiplist_free(seen_nexthops
);
1611 * omit_node is meant for the situation where we are adding a subtree
1612 * of a parent of some original requested node. The response already
1613 * contains the original requested node, and we don't want to duplicate
1614 * its routes in the list, so we skip it if the right or left node
1615 * matches (of course, we still travel down its child subtrees).
1617 static int rfapiNhlAddSubtree(
1618 struct agg_node
*rn
, /* in */
1619 uint32_t lifetime
, /* in */
1620 struct rfapi_next_hop_entry
**head
, /* in/out */
1621 struct rfapi_next_hop_entry
**tail
, /* in/out */
1622 struct agg_node
*omit_node
, /* in */
1623 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1624 struct agg_table
*rfd_rib_table
, /* preload here */
1625 struct prefix
*pfx_target_original
) /* query target */
1627 struct rfapi_ip_prefix rprefix
;
1630 /* FIXME: need to find a better way here to work without sticking our
1631 * hands in node->link */
1632 if (agg_node_left(rn
) && agg_node_left(rn
) != omit_node
) {
1633 if (agg_node_left(rn
)->info
) {
1634 const struct prefix
*p
=
1635 agg_node_get_prefix(agg_node_left(rn
));
1637 struct agg_node
*rib_rn
= NULL
;
1639 rfapiQprefix2Rprefix(p
, &rprefix
);
1641 rib_rn
= agg_node_get(rfd_rib_table
, p
);
1643 count
= rfapiNhlAddNodeRoutes(
1644 agg_node_left(rn
), &rprefix
, lifetime
, 0, head
,
1645 tail
, exclude_vnaddr
, rib_rn
,
1646 pfx_target_original
);
1648 count
= rfapiNhlAddNodeRoutes(
1649 agg_node_left(rn
), &rprefix
, lifetime
,
1650 1, head
, tail
, exclude_vnaddr
, rib_rn
,
1651 pfx_target_original
);
1655 agg_unlock_node(rib_rn
);
1659 if (agg_node_right(rn
) && agg_node_right(rn
) != omit_node
) {
1660 if (agg_node_right(rn
)->info
) {
1661 const struct prefix
*p
=
1662 agg_node_get_prefix(agg_node_right(rn
));
1664 struct agg_node
*rib_rn
= NULL
;
1666 rfapiQprefix2Rprefix(p
, &rprefix
);
1668 rib_rn
= agg_node_get(rfd_rib_table
, p
);
1670 count
= rfapiNhlAddNodeRoutes(
1671 agg_node_right(rn
), &rprefix
, lifetime
, 0, head
,
1672 tail
, exclude_vnaddr
, rib_rn
,
1673 pfx_target_original
);
1675 count
= rfapiNhlAddNodeRoutes(
1676 agg_node_right(rn
), &rprefix
, lifetime
,
1677 1, head
, tail
, exclude_vnaddr
, rib_rn
,
1678 pfx_target_original
);
1682 agg_unlock_node(rib_rn
);
1686 if (agg_node_left(rn
)) {
1687 rcount
+= rfapiNhlAddSubtree(
1688 agg_node_left(rn
), lifetime
, head
, tail
, omit_node
,
1689 exclude_vnaddr
, rfd_rib_table
, pfx_target_original
);
1691 if (agg_node_right(rn
)) {
1692 rcount
+= rfapiNhlAddSubtree(
1693 agg_node_right(rn
), lifetime
, head
, tail
, omit_node
,
1694 exclude_vnaddr
, rfd_rib_table
, pfx_target_original
);
1701 * Implementation of ROUTE_LIST(node) from RFAPI-Import-Event-Handling.txt
1703 * Construct an rfapi nexthop list based on the routes attached to
1704 * the specified node.
1706 * If there are any routes that do NOT have BGP_PATH_REMOVED set,
1707 * return those only. If there are ONLY routes with BGP_PATH_REMOVED,
1708 * then return those, and also include all the non-removed routes from the
1709 * next less-specific node (i.e., this node's parent) at the end.
1711 struct rfapi_next_hop_entry
*rfapiRouteNode2NextHopList(
1712 struct agg_node
*rn
, uint32_t lifetime
, /* put into nexthop entries */
1713 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1714 struct agg_table
*rfd_rib_table
, /* preload here */
1715 struct prefix
*pfx_target_original
) /* query target */
1717 struct rfapi_ip_prefix rprefix
;
1718 struct rfapi_next_hop_entry
*answer
= NULL
;
1719 struct rfapi_next_hop_entry
*last
= NULL
;
1720 struct agg_node
*parent
;
1721 const struct prefix
*p
= agg_node_get_prefix(rn
);
1723 struct agg_node
*rib_rn
;
1725 #ifdef DEBUG_RETURNED_NHL
1726 vnc_zlog_debug_verbose("%s: called with node pfx=%rRN", __func__
, rn
);
1727 rfapiDebugBacktrace();
1730 rfapiQprefix2Rprefix(p
, &rprefix
);
1732 rib_rn
= rfd_rib_table
? agg_node_get(rfd_rib_table
, p
) : NULL
;
1735 * Add non-withdrawn routes at this node
1737 count
= rfapiNhlAddNodeRoutes(rn
, &rprefix
, lifetime
, 0, &answer
, &last
,
1738 exclude_vnaddr
, rib_rn
,
1739 pfx_target_original
);
1742 * If the list has at least one entry, it's finished
1745 count
+= rfapiNhlAddSubtree(rn
, lifetime
, &answer
, &last
, NULL
,
1746 exclude_vnaddr
, rfd_rib_table
,
1747 pfx_target_original
);
1748 vnc_zlog_debug_verbose("%s: %d nexthops, answer=%p", __func__
,
1750 #ifdef DEBUG_RETURNED_NHL
1751 rfapiPrintNhl(NULL
, answer
);
1754 agg_unlock_node(rib_rn
);
1759 * Add withdrawn routes at this node
1761 count
= rfapiNhlAddNodeRoutes(rn
, &rprefix
, lifetime
, 1, &answer
, &last
,
1762 exclude_vnaddr
, rib_rn
,
1763 pfx_target_original
);
1765 agg_unlock_node(rib_rn
);
1767 // rfapiPrintNhl(NULL, answer);
1770 * walk up the tree until we find a node with non-deleted
1771 * routes, then add them
1773 for (parent
= agg_node_parent(rn
); parent
;
1774 parent
= agg_node_parent(parent
)) {
1775 if (rfapiHasNonRemovedRoutes(parent
)) {
1781 * Add non-withdrawn routes from less-specific prefix
1784 const struct prefix
*p
= agg_node_get_prefix(parent
);
1786 rib_rn
= rfd_rib_table
? agg_node_get(rfd_rib_table
, p
) : NULL
;
1787 rfapiQprefix2Rprefix(p
, &rprefix
);
1788 count
+= rfapiNhlAddNodeRoutes(parent
, &rprefix
, lifetime
, 0,
1789 &answer
, &last
, exclude_vnaddr
,
1790 rib_rn
, pfx_target_original
);
1791 count
+= rfapiNhlAddSubtree(parent
, lifetime
, &answer
, &last
,
1792 rn
, exclude_vnaddr
, rfd_rib_table
,
1793 pfx_target_original
);
1795 agg_unlock_node(rib_rn
);
1798 * There is no parent with non-removed routes. Still need to
1799 * add subtree of original node if it contributed routes to the
1803 count
+= rfapiNhlAddSubtree(rn
, lifetime
, &answer
,
1804 &last
, rn
, exclude_vnaddr
,
1806 pfx_target_original
);
1809 vnc_zlog_debug_verbose("%s: %d nexthops, answer=%p", __func__
, count
,
1811 #ifdef DEBUG_RETURNED_NHL
1812 rfapiPrintNhl(NULL
, answer
);
1818 * Construct nexthop list of all routes in table
1820 struct rfapi_next_hop_entry
*rfapiRouteTable2NextHopList(
1821 struct agg_table
*rt
, uint32_t lifetime
, /* put into nexthop entries */
1822 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1823 struct agg_table
*rfd_rib_table
, /* preload this NVE rib table */
1824 struct prefix
*pfx_target_original
) /* query target */
1826 struct agg_node
*rn
;
1827 struct rfapi_next_hop_entry
*biglist
= NULL
;
1828 struct rfapi_next_hop_entry
*nhl
;
1829 struct rfapi_next_hop_entry
*tail
= NULL
;
1832 for (rn
= agg_route_top(rt
); rn
; rn
= agg_route_next(rn
)) {
1834 nhl
= rfapiRouteNode2NextHopList(rn
, lifetime
, exclude_vnaddr
,
1836 pfx_target_original
);
1838 tail
= biglist
= nhl
;
1845 while (tail
->next
) {
1852 vnc_zlog_debug_verbose("%s: returning %d routes", __func__
, count
);
1856 struct rfapi_next_hop_entry
*rfapiEthRouteNode2NextHopList(
1857 struct agg_node
*rn
, struct rfapi_ip_prefix
*rprefix
,
1858 uint32_t lifetime
, /* put into nexthop entries */
1859 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1860 struct agg_table
*rfd_rib_table
, /* preload NVE rib table */
1861 struct prefix
*pfx_target_original
) /* query target */
1864 struct rfapi_next_hop_entry
*answer
= NULL
;
1865 struct rfapi_next_hop_entry
*last
= NULL
;
1866 struct agg_node
*rib_rn
;
1868 rib_rn
= rfd_rib_table
1869 ? agg_node_get(rfd_rib_table
, agg_node_get_prefix(rn
))
1872 count
= rfapiNhlAddNodeRoutes(rn
, rprefix
, lifetime
, 0, &answer
, &last
,
1873 NULL
, rib_rn
, pfx_target_original
);
1875 #ifdef DEBUG_ENCAP_MONITOR
1876 vnc_zlog_debug_verbose("%s: node %p: %d non-holddown routes", __func__
,
1881 count
= rfapiNhlAddNodeRoutes(rn
, rprefix
, lifetime
, 1, &answer
,
1882 &last
, exclude_vnaddr
, rib_rn
,
1883 pfx_target_original
);
1884 vnc_zlog_debug_verbose("%s: node %p: %d holddown routes",
1885 __func__
, rn
, count
);
1889 agg_unlock_node(rib_rn
);
1891 #ifdef DEBUG_RETURNED_NHL
1892 rfapiPrintNhl(NULL
, answer
);
1900 * Construct nexthop list of all routes in table
1902 struct rfapi_next_hop_entry
*rfapiEthRouteTable2NextHopList(
1903 uint32_t logical_net_id
, struct rfapi_ip_prefix
*rprefix
,
1904 uint32_t lifetime
, /* put into nexthop entries */
1905 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1906 struct agg_table
*rfd_rib_table
, /* preload NVE rib node */
1907 struct prefix
*pfx_target_original
) /* query target */
1909 struct rfapi_import_table
*it
;
1910 struct bgp
*bgp
= bgp_get_default();
1911 struct agg_table
*rt
;
1912 struct agg_node
*rn
;
1913 struct rfapi_next_hop_entry
*biglist
= NULL
;
1914 struct rfapi_next_hop_entry
*nhl
;
1915 struct rfapi_next_hop_entry
*tail
= NULL
;
1919 it
= rfapiMacImportTableGet(bgp
, logical_net_id
);
1920 rt
= it
->imported_vpn
[AFI_L2VPN
];
1922 for (rn
= agg_route_top(rt
); rn
; rn
= agg_route_next(rn
)) {
1924 nhl
= rfapiEthRouteNode2NextHopList(
1925 rn
, rprefix
, lifetime
, exclude_vnaddr
, rfd_rib_table
,
1926 pfx_target_original
);
1928 tail
= biglist
= nhl
;
1935 while (tail
->next
) {
1942 vnc_zlog_debug_verbose("%s: returning %d routes", __func__
, count
);
1947 * Insert a new bpi to the imported route table node,
1948 * keeping the list of BPIs sorted best route first
1950 static void rfapiBgpInfoAttachSorted(struct agg_node
*rn
,
1951 struct bgp_path_info
*info_new
, afi_t afi
,
1955 struct bgp_path_info
*prev
;
1956 struct bgp_path_info
*next
;
1957 char pfx_buf
[PREFIX2STR_BUFFER
];
1960 bgp
= bgp_get_default(); /* assume 1 instance for now */
1962 if (VNC_DEBUG(IMPORT_BI_ATTACH
)) {
1963 vnc_zlog_debug_verbose("%s: info_new->peer=%p", __func__
,
1965 vnc_zlog_debug_verbose("%s: info_new->peer->su_remote=%p",
1966 __func__
, info_new
->peer
->su_remote
);
1969 for (prev
= NULL
, next
= rn
->info
; next
;
1970 prev
= next
, next
= next
->next
) {
1971 enum bgp_path_selection_reason reason
;
1974 || (!CHECK_FLAG(info_new
->flags
, BGP_PATH_REMOVED
)
1975 && CHECK_FLAG(next
->flags
, BGP_PATH_REMOVED
))
1976 || bgp_path_info_cmp_compatible(bgp
, info_new
, next
,
1979 == -1) { /* -1 if 1st is better */
1983 vnc_zlog_debug_verbose("%s: prev=%p, next=%p", __func__
, prev
, next
);
1985 prev
->next
= info_new
;
1987 rn
->info
= info_new
;
1989 info_new
->prev
= prev
;
1990 info_new
->next
= next
;
1992 next
->prev
= info_new
;
1993 bgp_attr_intern(info_new
->attr
);
1996 static void rfapiBgpInfoDetach(struct agg_node
*rn
, struct bgp_path_info
*bpi
)
1999 * Remove the route (doubly-linked)
2001 // bgp_attr_unintern (&bpi->attr);
2003 bpi
->next
->prev
= bpi
->prev
;
2005 bpi
->prev
->next
= bpi
->next
;
2007 rn
->info
= bpi
->next
;
2011 * For L3-indexed import tables
2013 static int rfapi_bi_peer_rd_cmp(const void *b1
, const void *b2
)
2015 const struct bgp_path_info
*bpi1
= b1
;
2016 const struct bgp_path_info
*bpi2
= b2
;
2021 if (bpi1
->peer
< bpi2
->peer
)
2023 if (bpi1
->peer
> bpi2
->peer
)
2029 return vnc_prefix_cmp(
2030 (const struct prefix
*)&bpi1
->extra
->vnc
.import
.rd
,
2031 (const struct prefix
*)&bpi2
->extra
->vnc
.import
.rd
);
2035 * For L2-indexed import tables
2036 * The BPIs in these tables should ALWAYS have an aux_prefix set because
2037 * they arrive via IPv4 or IPv6 advertisements.
2039 static int rfapi_bi_peer_rd_aux_cmp(const void *b1
, const void *b2
)
2041 const struct bgp_path_info
*bpi1
= b1
;
2042 const struct bgp_path_info
*bpi2
= b2
;
2048 if (bpi1
->peer
< bpi2
->peer
)
2050 if (bpi1
->peer
> bpi2
->peer
)
2056 rc
= vnc_prefix_cmp((struct prefix
*)&bpi1
->extra
->vnc
.import
.rd
,
2057 (struct prefix
*)&bpi2
->extra
->vnc
.import
.rd
);
2063 * L2 import tables can have multiple entries with the
2064 * same MAC address, same RD, but different L3 addresses.
2066 * Use presence of aux_prefix with AF=ethernet and prefixlen=1
2067 * as magic value to signify explicit wildcarding of the aux_prefix.
2068 * This magic value will not appear in bona fide bpi entries in
2069 * the import table, but is allowed in the "fake" bpi used to
2070 * probe the table when searching. (We have to test both b1 and b2
2071 * because there is no guarantee of the order the test key and
2072 * the real key will be passed)
2074 if ((bpi1
->extra
->vnc
.import
.aux_prefix
.family
== AF_ETHERNET
2075 && (bpi1
->extra
->vnc
.import
.aux_prefix
.prefixlen
== 1))
2076 || (bpi2
->extra
->vnc
.import
.aux_prefix
.family
== AF_ETHERNET
2077 && (bpi2
->extra
->vnc
.import
.aux_prefix
.prefixlen
== 1))) {
2080 * wildcard aux address specified
2085 return vnc_prefix_cmp(&bpi1
->extra
->vnc
.import
.aux_prefix
,
2086 &bpi2
->extra
->vnc
.import
.aux_prefix
);
2091 * Index on RD and Peer
2093 static void rfapiItBiIndexAdd(struct agg_node
*rn
, /* Import table VPN node */
2094 struct bgp_path_info
*bpi
) /* new BPI */
2096 struct skiplist
*sl
;
2097 const struct prefix
*p
;
2104 char buf
[RD_ADDRSTRLEN
];
2106 vnc_zlog_debug_verbose("%s: bpi %p, peer %p, rd %s", __func__
,
2108 prefix_rd2str(&bpi
->extra
->vnc
.import
.rd
,
2112 sl
= RFAPI_RDINDEX_W_ALLOC(rn
);
2114 p
= agg_node_get_prefix(rn
);
2115 if (AF_ETHERNET
== p
->family
) {
2116 sl
= skiplist_new(0, rfapi_bi_peer_rd_aux_cmp
, NULL
);
2118 sl
= skiplist_new(0, rfapi_bi_peer_rd_cmp
, NULL
);
2120 RFAPI_IT_EXTRA_GET(rn
)->u
.vpn
.idx_rd
= sl
;
2121 agg_lock_node(rn
); /* for skiplist */
2123 assert(!skiplist_insert(sl
, (void *)bpi
, (void *)bpi
));
2124 agg_lock_node(rn
); /* for skiplist entry */
2126 /* NB: BPIs in import tables are not refcounted */
2129 static void rfapiItBiIndexDump(struct agg_node
*rn
)
2131 struct skiplist
*sl
;
2132 void *cursor
= NULL
;
2133 struct bgp_path_info
*k
;
2134 struct bgp_path_info
*v
;
2137 sl
= RFAPI_RDINDEX(rn
);
2141 for (rc
= skiplist_next(sl
, (void **)&k
, (void **)&v
, &cursor
); !rc
;
2142 rc
= skiplist_next(sl
, (void **)&k
, (void **)&v
, &cursor
)) {
2144 char buf
[RD_ADDRSTRLEN
];
2145 char buf_aux_pfx
[PREFIX_STRLEN
];
2147 prefix_rd2str(&k
->extra
->vnc
.import
.rd
, buf
, sizeof(buf
));
2148 if (k
->extra
->vnc
.import
.aux_prefix
.family
) {
2149 prefix2str(&k
->extra
->vnc
.import
.aux_prefix
,
2150 buf_aux_pfx
, sizeof(buf_aux_pfx
));
2152 strlcpy(buf_aux_pfx
, "(none)", sizeof(buf_aux_pfx
));
2154 vnc_zlog_debug_verbose("bpi %p, peer %p, rd %s, aux_prefix %s",
2155 k
, k
->peer
, buf
, buf_aux_pfx
);
2159 static struct bgp_path_info
*rfapiItBiIndexSearch(
2160 struct agg_node
*rn
, /* Import table VPN node */
2161 struct prefix_rd
*prd
, struct peer
*peer
,
2162 const struct prefix
*aux_prefix
) /* optional L3 addr for L2 ITs */
2164 struct skiplist
*sl
;
2166 struct bgp_path_info bpi_fake
= {0};
2167 struct bgp_path_info_extra bpi_extra
= {0};
2168 struct bgp_path_info
*bpi_result
;
2170 sl
= RFAPI_RDINDEX(rn
);
2174 #ifdef DEBUG_BI_SEARCH
2176 char buf
[RD_ADDRSTRLEN
];
2177 char buf_aux_pfx
[PREFIX_STRLEN
];
2180 prefix2str(aux_prefix
, buf_aux_pfx
,
2181 sizeof(buf_aux_pfx
));
2183 strlcpy(buf_aux_pfx
, "(nil)", sizeof(buf_aux_pfx
));
2185 vnc_zlog_debug_verbose("%s want prd=%s, peer=%p, aux_prefix=%s",
2187 prefix_rd2str(prd
, buf
, sizeof(buf
)),
2189 rfapiItBiIndexDump(rn
);
2193 /* threshold is a WAG */
2194 if (sl
->count
< 3) {
2195 #ifdef DEBUG_BI_SEARCH
2196 vnc_zlog_debug_verbose("%s: short list algorithm", __func__
);
2198 /* if short list, linear search might be faster */
2199 for (bpi_result
= rn
->info
; bpi_result
;
2200 bpi_result
= bpi_result
->next
) {
2201 #ifdef DEBUG_BI_SEARCH
2203 char buf
[RD_ADDRSTRLEN
];
2205 vnc_zlog_debug_verbose(
2206 "%s: bpi has prd=%s, peer=%p", __func__
,
2207 prefix_rd2str(&bpi_result
->extra
->vnc
2213 if (peer
== bpi_result
->peer
2214 && !prefix_cmp((struct prefix
*)&bpi_result
->extra
2216 (struct prefix
*)prd
)) {
2218 #ifdef DEBUG_BI_SEARCH
2219 vnc_zlog_debug_verbose(
2220 "%s: peer and RD same, doing aux_prefix check",
2226 &bpi_result
->extra
->vnc
.import
2229 #ifdef DEBUG_BI_SEARCH
2230 vnc_zlog_debug_verbose("%s: match",
2240 bpi_fake
.peer
= peer
;
2241 bpi_fake
.extra
= &bpi_extra
;
2242 bpi_fake
.extra
->vnc
.import
.rd
= *(struct prefix_rd
*)prd
;
2244 bpi_fake
.extra
->vnc
.import
.aux_prefix
= *aux_prefix
;
2247 bpi_fake
.extra
->vnc
.import
.aux_prefix
.family
= AF_ETHERNET
;
2248 bpi_fake
.extra
->vnc
.import
.aux_prefix
.prefixlen
= 1;
2251 rc
= skiplist_search(sl
, (void *)&bpi_fake
, (void *)&bpi_result
);
2254 #ifdef DEBUG_BI_SEARCH
2255 vnc_zlog_debug_verbose("%s: no match", __func__
);
2260 #ifdef DEBUG_BI_SEARCH
2261 vnc_zlog_debug_verbose("%s: matched bpi=%p", __func__
, bpi_result
);
2267 static void rfapiItBiIndexDel(struct agg_node
*rn
, /* Import table VPN node */
2268 struct bgp_path_info
*bpi
) /* old BPI */
2270 struct skiplist
*sl
;
2274 char buf
[RD_ADDRSTRLEN
];
2276 vnc_zlog_debug_verbose("%s: bpi %p, peer %p, rd %s", __func__
,
2278 prefix_rd2str(&bpi
->extra
->vnc
.import
.rd
,
2282 sl
= RFAPI_RDINDEX(rn
);
2285 rc
= skiplist_delete(sl
, (void *)(bpi
), (void *)bpi
);
2287 rfapiItBiIndexDump(rn
);
2291 agg_unlock_node(rn
); /* for skiplist entry */
2293 /* NB: BPIs in import tables are not refcounted */
2297 * Add a backreference at the ENCAP node to the VPN route that
2301 rfapiMonitorEncapAdd(struct rfapi_import_table
*import_table
,
2302 struct prefix
*p
, /* VN address */
2303 struct agg_node
*vpn_rn
, /* VPN node */
2304 struct bgp_path_info
*vpn_bpi
) /* VPN bpi/route */
2306 afi_t afi
= family2afi(p
->family
);
2307 struct agg_node
*rn
;
2308 struct rfapi_monitor_encap
*m
;
2311 rn
= agg_node_get(import_table
->imported_encap
[afi
], p
); /* locks rn */
2314 m
= XCALLOC(MTYPE_RFAPI_MONITOR_ENCAP
,
2315 sizeof(struct rfapi_monitor_encap
));
2322 /* insert to encap node's list */
2323 m
->next
= RFAPI_MONITOR_ENCAP(rn
);
2326 RFAPI_MONITOR_ENCAP_W_ALLOC(rn
) = m
;
2328 /* for easy lookup when deleting vpn route */
2329 vpn_bpi
->extra
->vnc
.import
.hme
= m
;
2331 vnc_zlog_debug_verbose(
2332 "%s: it=%p, vpn_bpi=%p, afi=%d, encap rn=%p, setting vpn_bpi->extra->vnc.import.hme=%p",
2333 __func__
, import_table
, vpn_bpi
, afi
, rn
, m
);
2335 RFAPI_CHECK_REFCOUNT(rn
, SAFI_ENCAP
, 0);
2336 bgp_attr_intern(vpn_bpi
->attr
);
2339 static void rfapiMonitorEncapDelete(struct bgp_path_info
*vpn_bpi
)
2342 * Remove encap monitor
2344 vnc_zlog_debug_verbose("%s: vpn_bpi=%p", __func__
, vpn_bpi
);
2345 if (vpn_bpi
->extra
) {
2346 struct rfapi_monitor_encap
*hme
=
2347 vpn_bpi
->extra
->vnc
.import
.hme
;
2351 vnc_zlog_debug_verbose("%s: hme=%p", __func__
, hme
);
2353 /* Refcount checking takes too long here */
2354 // RFAPI_CHECK_REFCOUNT(hme->rn, SAFI_ENCAP, 0);
2356 hme
->next
->prev
= hme
->prev
;
2358 hme
->prev
->next
= hme
->next
;
2360 RFAPI_MONITOR_ENCAP_W_ALLOC(hme
->rn
) =
2362 /* Refcount checking takes too long here */
2363 // RFAPI_CHECK_REFCOUNT(hme->rn, SAFI_ENCAP, 1);
2365 /* see if the struct rfapi_it_extra is empty and can be
2367 rfapiMonitorExtraPrune(SAFI_ENCAP
, hme
->rn
);
2369 agg_unlock_node(hme
->rn
); /* decr ref count */
2370 XFREE(MTYPE_RFAPI_MONITOR_ENCAP
, hme
);
2371 vpn_bpi
->extra
->vnc
.import
.hme
= NULL
;
2377 * quagga lib/thread.h says this must return int even though
2378 * it doesn't do anything with the return value
2380 static int rfapiWithdrawTimerVPN(struct thread
*t
)
2382 struct rfapi_withdraw
*wcb
= t
->arg
;
2383 struct bgp_path_info
*bpi
= wcb
->info
;
2384 struct bgp
*bgp
= bgp_get_default();
2385 const struct prefix
*p
;
2386 struct rfapi_monitor_vpn
*moved
;
2390 vnc_zlog_debug_verbose(
2391 "%s: NULL BGP pointer, assume shutdown race condition!!!",
2395 if (CHECK_FLAG(bgp
->flags
, BGP_FLAG_DELETE_IN_PROGRESS
)) {
2396 vnc_zlog_debug_verbose(
2397 "%s: BGP delete in progress, assume shutdown race condition!!!",
2403 assert(wcb
->import_table
);
2406 RFAPI_CHECK_REFCOUNT(wcb
->node
, SAFI_MPLS_VPN
, wcb
->lockoffset
);
2408 vnc_zlog_debug_verbose("%s: removing bpi %p at prefix %pRN", __func__
,
2412 * Remove the route (doubly-linked)
2414 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_VALID
)
2415 && VALID_INTERIOR_TYPE(bpi
->type
))
2416 RFAPI_MONITOR_EXTERIOR(wcb
->node
)->valid_interior_count
--;
2418 p
= agg_node_get_prefix(wcb
->node
);
2419 afi
= family2afi(p
->family
);
2420 wcb
->import_table
->holddown_count
[afi
] -= 1; /* keep count consistent */
2421 rfapiItBiIndexDel(wcb
->node
, bpi
);
2422 rfapiBgpInfoDetach(wcb
->node
, bpi
); /* with removed bpi */
2424 vnc_import_bgp_exterior_del_route_interior(bgp
, wcb
->import_table
,
2429 * If VNC is configured to send response remove messages, AND
2430 * if the removed route had a UN address, do response removal
2433 if (!(bgp
->rfapi_cfg
->flags
2434 & BGP_VNC_CONFIG_RESPONSE_REMOVAL_DISABLE
)) {
2436 int has_valid_duplicate
= 0;
2437 struct bgp_path_info
*bpii
;
2440 * First check if there are any OTHER routes at this node
2441 * that have the same nexthop and a valid UN address. If
2442 * there are (e.g., from other peers), then the route isn't
2443 * really gone, so skip sending a response removal message.
2445 for (bpii
= wcb
->node
->info
; bpii
; bpii
= bpii
->next
) {
2446 if (rfapiVpnBiSamePtUn(bpi
, bpii
)) {
2447 has_valid_duplicate
= 1;
2452 vnc_zlog_debug_verbose("%s: has_valid_duplicate=%d", __func__
,
2453 has_valid_duplicate
);
2455 if (!has_valid_duplicate
) {
2456 rfapiRibPendingDeleteRoute(bgp
, wcb
->import_table
, afi
,
2461 rfapiMonitorEncapDelete(bpi
);
2464 * If there are no VPN monitors at this VPN Node A,
2467 if (!RFAPI_MONITOR_VPN(wcb
->node
)) {
2468 vnc_zlog_debug_verbose("%s: no VPN monitors at this node",
2474 * rfapiMonitorMoveShorter only moves monitors if there are
2475 * no remaining valid routes at the current node
2477 moved
= rfapiMonitorMoveShorter(wcb
->node
, 1);
2480 rfapiMonitorMovedUp(wcb
->import_table
, wcb
->node
, moved
->node
,
2488 rfapiBgpInfoFree(bpi
);
2492 * If route count at this node has gone to 0, withdraw exported prefix
2494 if (!wcb
->node
->info
) {
2495 /* see if the struct rfapi_it_extra is empty and can be freed */
2496 rfapiMonitorExtraPrune(SAFI_MPLS_VPN
, wcb
->node
);
2497 vnc_direct_bgp_del_prefix(bgp
, wcb
->import_table
, wcb
->node
);
2498 vnc_zebra_del_prefix(bgp
, wcb
->import_table
, wcb
->node
);
2501 * nexthop change event
2502 * vnc_direct_bgp_add_prefix() will recompute the VN addr
2505 vnc_direct_bgp_add_prefix(bgp
, wcb
->import_table
, wcb
->node
);
2508 RFAPI_CHECK_REFCOUNT(wcb
->node
, SAFI_MPLS_VPN
, 1 + wcb
->lockoffset
);
2509 agg_unlock_node(wcb
->node
); /* decr ref count */
2510 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
2515 * This works for multiprotocol extension, but not for plain ol'
2516 * unicast IPv4 because that nexthop is stored in attr->nexthop
2518 void rfapiNexthop2Prefix(struct attr
*attr
, struct prefix
*p
)
2523 memset(p
, 0, sizeof(struct prefix
));
2525 switch (p
->family
= BGP_MP_NEXTHOP_FAMILY(attr
->mp_nexthop_len
)) {
2527 p
->u
.prefix4
= attr
->mp_nexthop_global_in
;
2532 p
->u
.prefix6
= attr
->mp_nexthop_global
;
2537 vnc_zlog_debug_verbose("%s: Family is unknown = %d", __func__
,
2542 void rfapiUnicastNexthop2Prefix(afi_t afi
, struct attr
*attr
, struct prefix
*p
)
2544 if (afi
== AFI_IP
) {
2545 p
->family
= AF_INET
;
2547 p
->u
.prefix4
= attr
->nexthop
;
2549 rfapiNexthop2Prefix(attr
, p
);
2553 static int rfapiAttrNexthopAddrDifferent(struct prefix
*p1
, struct prefix
*p2
)
2556 vnc_zlog_debug_verbose("%s: p1 or p2 is NULL", __func__
);
2561 * Are address families the same?
2563 if (p1
->family
!= p2
->family
) {
2567 switch (p1
->family
) {
2569 if (IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
2574 if (IPV6_ADDR_SAME(&p1
->u
.prefix6
, &p2
->u
.prefix6
))
2585 static void rfapiCopyUnEncap2VPN(struct bgp_path_info
*encap_bpi
,
2586 struct bgp_path_info
*vpn_bpi
)
2588 if (!vpn_bpi
|| !vpn_bpi
->extra
) {
2589 zlog_warn("%s: no vpn bpi attr/extra, can't copy UN address",
2594 switch (BGP_MP_NEXTHOP_FAMILY(encap_bpi
->attr
->mp_nexthop_len
)) {
2598 * instrumentation to debug segfault of 091127
2600 vnc_zlog_debug_verbose("%s: vpn_bpi=%p", __func__
, vpn_bpi
);
2602 vnc_zlog_debug_verbose("%s: vpn_bpi->extra=%p",
2603 __func__
, vpn_bpi
->extra
);
2606 vpn_bpi
->extra
->vnc
.import
.un_family
= AF_INET
;
2607 vpn_bpi
->extra
->vnc
.import
.un
.addr4
=
2608 encap_bpi
->attr
->mp_nexthop_global_in
;
2612 vpn_bpi
->extra
->vnc
.import
.un_family
= AF_INET6
;
2613 vpn_bpi
->extra
->vnc
.import
.un
.addr6
=
2614 encap_bpi
->attr
->mp_nexthop_global
;
2618 zlog_warn("%s: invalid encap nexthop length: %d", __func__
,
2619 encap_bpi
->attr
->mp_nexthop_len
);
2620 vpn_bpi
->extra
->vnc
.import
.un_family
= 0;
2626 * returns 0 on success, nonzero on error
2629 rfapiWithdrawEncapUpdateCachedUn(struct rfapi_import_table
*import_table
,
2630 struct bgp_path_info
*encap_bpi
,
2631 struct agg_node
*vpn_rn
,
2632 struct bgp_path_info
*vpn_bpi
)
2637 * clear cached UN address
2639 if (!vpn_bpi
|| !vpn_bpi
->extra
) {
2641 "%s: missing VPN bpi/extra, can't clear UN addr",
2645 vpn_bpi
->extra
->vnc
.import
.un_family
= 0;
2646 memset(&vpn_bpi
->extra
->vnc
.import
.un
, 0,
2647 sizeof(vpn_bpi
->extra
->vnc
.import
.un
));
2648 if (CHECK_FLAG(vpn_bpi
->flags
, BGP_PATH_VALID
)) {
2649 if (rfapiGetVncTunnelUnAddr(vpn_bpi
->attr
, NULL
)) {
2650 UNSET_FLAG(vpn_bpi
->flags
, BGP_PATH_VALID
);
2651 if (VALID_INTERIOR_TYPE(vpn_bpi
->type
))
2652 RFAPI_MONITOR_EXTERIOR(vpn_rn
)
2653 ->valid_interior_count
--;
2654 /* signal interior route withdrawal to
2655 * import-exterior */
2656 vnc_import_bgp_exterior_del_route_interior(
2657 bgp_get_default(), import_table
, vpn_rn
,
2664 zlog_warn("%s: missing VPN bpi, can't clear UN addr",
2668 rfapiCopyUnEncap2VPN(encap_bpi
, vpn_bpi
);
2669 if (!CHECK_FLAG(vpn_bpi
->flags
, BGP_PATH_VALID
)) {
2670 SET_FLAG(vpn_bpi
->flags
, BGP_PATH_VALID
);
2671 if (VALID_INTERIOR_TYPE(vpn_bpi
->type
))
2672 RFAPI_MONITOR_EXTERIOR(vpn_rn
)
2673 ->valid_interior_count
++;
2674 /* signal interior route withdrawal to import-exterior
2676 vnc_import_bgp_exterior_add_route_interior(
2677 bgp_get_default(), import_table
, vpn_rn
,
2684 static int rfapiWithdrawTimerEncap(struct thread
*t
)
2686 struct rfapi_withdraw
*wcb
= t
->arg
;
2687 struct bgp_path_info
*bpi
= wcb
->info
;
2688 int was_first_route
= 0;
2689 struct rfapi_monitor_encap
*em
;
2690 struct skiplist
*vpn_node_sl
= skiplist_new(0, NULL
, NULL
);
2694 assert(wcb
->import_table
);
2696 RFAPI_CHECK_REFCOUNT(wcb
->node
, SAFI_ENCAP
, 0);
2698 if (wcb
->node
->info
== bpi
)
2699 was_first_route
= 1;
2702 * Remove the route/bpi and free it
2704 rfapiBgpInfoDetach(wcb
->node
, bpi
);
2705 rfapiBgpInfoFree(bpi
);
2707 if (!was_first_route
)
2710 for (em
= RFAPI_MONITOR_ENCAP(wcb
->node
); em
; em
= em
->next
) {
2713 * Update monitoring VPN BPIs with new encap info at the
2714 * head of the encap bpi chain (which could be NULL after
2715 * removing the expiring bpi above)
2717 if (rfapiWithdrawEncapUpdateCachedUn(wcb
->import_table
,
2718 wcb
->node
->info
, em
->node
,
2723 * Build a list of unique VPN nodes referenced by these
2725 * Use a skiplist for speed.
2727 skiplist_insert(vpn_node_sl
, em
->node
, em
->node
);
2732 * for each VPN node referenced in the ENCAP monitors:
2734 struct agg_node
*rn
;
2735 while (!skiplist_first(vpn_node_sl
, (void **)&rn
, NULL
)) {
2736 if (!wcb
->node
->info
) {
2737 struct rfapi_monitor_vpn
*moved
;
2739 moved
= rfapiMonitorMoveShorter(rn
, 0);
2741 // rfapiDoRouteCallback(wcb->import_table,
2742 // moved->node, moved);
2743 rfapiMonitorMovedUp(wcb
->import_table
, rn
,
2744 moved
->node
, moved
);
2747 // rfapiDoRouteCallback(wcb->import_table, rn, NULL);
2748 rfapiMonitorItNodeChanged(wcb
->import_table
, rn
, NULL
);
2750 skiplist_delete_first(vpn_node_sl
);
2754 RFAPI_CHECK_REFCOUNT(wcb
->node
, SAFI_ENCAP
, 1);
2755 agg_unlock_node(wcb
->node
); /* decr ref count */
2756 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
2757 skiplist_free(vpn_node_sl
);
2763 * Works for both VPN and ENCAP routes; timer_service_func is different
2767 rfapiBiStartWithdrawTimer(struct rfapi_import_table
*import_table
,
2768 struct agg_node
*rn
, struct bgp_path_info
*bpi
,
2769 afi_t afi
, safi_t safi
,
2770 int (*timer_service_func
)(struct thread
*))
2773 struct rfapi_withdraw
*wcb
;
2775 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
)) {
2777 * Already on the path to being withdrawn,
2778 * should already have a timer set up to
2781 vnc_zlog_debug_verbose(
2782 "%s: already being withdrawn, do nothing", __func__
);
2786 rfapiGetVncLifetime(bpi
->attr
, &lifetime
);
2787 vnc_zlog_debug_verbose("%s: VNC lifetime is %u", __func__
, lifetime
);
2790 * withdrawn routes get to hang around for a while
2792 SET_FLAG(bpi
->flags
, BGP_PATH_REMOVED
);
2794 /* set timer to remove the route later */
2795 lifetime
= rfapiGetHolddownFromLifetime(lifetime
);
2796 vnc_zlog_debug_verbose("%s: using timeout %u", __func__
, lifetime
);
2799 * Stash import_table, node, and info for use by timer
2800 * service routine, which is supposed to free the wcb.
2802 wcb
= XCALLOC(MTYPE_RFAPI_WITHDRAW
, sizeof(struct rfapi_withdraw
));
2806 wcb
->import_table
= import_table
;
2807 bgp_attr_intern(bpi
->attr
);
2809 if (VNC_DEBUG(VERBOSE
)) {
2810 vnc_zlog_debug_verbose(
2811 "%s: wcb values: node=%p, info=%p, import_table=%p (bpi follows)",
2812 __func__
, wcb
->node
, wcb
->info
, wcb
->import_table
);
2813 rfapiPrintBi(NULL
, bpi
);
2818 if (lifetime
> UINT32_MAX
/ 1001) {
2819 /* sub-optimal case, but will probably never happen */
2820 bpi
->extra
->vnc
.import
.timer
= NULL
;
2821 thread_add_timer(bm
->master
, timer_service_func
, wcb
, lifetime
,
2822 &bpi
->extra
->vnc
.import
.timer
);
2824 static uint32_t jitter
;
2825 uint32_t lifetime_msec
;
2828 * the goal here is to spread out the timers so they are
2829 * sortable in the skip list
2831 if (++jitter
>= 1000)
2834 lifetime_msec
= (lifetime
* 1000) + jitter
;
2836 bpi
->extra
->vnc
.import
.timer
= NULL
;
2837 thread_add_timer_msec(bm
->master
, timer_service_func
, wcb
,
2839 &bpi
->extra
->vnc
.import
.timer
);
2842 /* re-sort route list (BGP_PATH_REMOVED routes are last) */
2843 if (((struct bgp_path_info
*)rn
->info
)->next
) {
2844 rfapiBgpInfoDetach(rn
, bpi
);
2845 rfapiBgpInfoAttachSorted(rn
, bpi
, afi
, safi
);
2850 typedef void(rfapi_bi_filtered_import_f
)(struct rfapi_import_table
*table
,
2851 int action
, struct peer
*peer
,
2852 void *rfd
, const struct prefix
*prefix
,
2853 const struct prefix
*aux_prefix
,
2854 afi_t afi
, struct prefix_rd
*prd
,
2855 struct attr
*attr
, uint8_t type
,
2856 uint8_t sub_type
, uint32_t *label
);
2859 static void rfapiExpireEncapNow(struct rfapi_import_table
*it
,
2860 struct agg_node
*rn
, struct bgp_path_info
*bpi
)
2862 struct rfapi_withdraw
*wcb
;
2866 * pretend we're an expiring timer
2868 wcb
= XCALLOC(MTYPE_RFAPI_WITHDRAW
, sizeof(struct rfapi_withdraw
));
2871 wcb
->import_table
= it
;
2872 memset(&t
, 0, sizeof(t
));
2874 rfapiWithdrawTimerEncap(&t
); /* frees wcb */
2877 static int rfapiGetNexthop(struct attr
*attr
, struct prefix
*prefix
)
2879 switch (BGP_MP_NEXTHOP_FAMILY(attr
->mp_nexthop_len
)) {
2881 prefix
->family
= AF_INET
;
2882 prefix
->prefixlen
= 32;
2883 prefix
->u
.prefix4
= attr
->mp_nexthop_global_in
;
2886 prefix
->family
= AF_INET6
;
2887 prefix
->prefixlen
= 128;
2888 prefix
->u
.prefix6
= attr
->mp_nexthop_global
;
2891 vnc_zlog_debug_verbose("%s: unknown attr->mp_nexthop_len %d",
2892 __func__
, attr
->mp_nexthop_len
);
2899 * import a bgp_path_info if its route target list intersects with the
2900 * import table's route target list
2902 static void rfapiBgpInfoFilteredImportEncap(
2903 struct rfapi_import_table
*import_table
, int action
, struct peer
*peer
,
2904 void *rfd
, /* set for looped back routes */
2905 const struct prefix
*p
,
2906 const struct prefix
*aux_prefix
, /* Unused for encap routes */
2907 afi_t afi
, struct prefix_rd
*prd
,
2908 struct attr
*attr
, /* part of bgp_path_info */
2909 uint8_t type
, /* part of bgp_path_info */
2910 uint8_t sub_type
, /* part of bgp_path_info */
2911 uint32_t *label
) /* part of bgp_path_info */
2913 struct agg_table
*rt
= NULL
;
2914 struct agg_node
*rn
;
2915 struct bgp_path_info
*info_new
;
2916 struct bgp_path_info
*bpi
;
2917 struct bgp_path_info
*next
;
2920 struct prefix p_firstbpi_old
;
2921 struct prefix p_firstbpi_new
;
2923 const char *action_str
= NULL
;
2924 struct prefix un_prefix
;
2927 bgp
= bgp_get_default(); /* assume 1 instance for now */
2930 case FIF_ACTION_UPDATE
:
2931 action_str
= "update";
2933 case FIF_ACTION_WITHDRAW
:
2934 action_str
= "withdraw";
2936 case FIF_ACTION_KILL
:
2937 action_str
= "kill";
2944 vnc_zlog_debug_verbose(
2945 "%s: entry: %s: prefix %s/%d", __func__
, action_str
,
2946 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
2948 memset(&p_firstbpi_old
, 0, sizeof(p_firstbpi_old
));
2949 memset(&p_firstbpi_new
, 0, sizeof(p_firstbpi_new
));
2951 if (action
== FIF_ACTION_UPDATE
) {
2953 * Compare rt lists. If no intersection, don't import this route
2954 * On a withdraw, peer and RD are sufficient to determine if
2957 if (!attr
|| !attr
->ecommunity
) {
2959 vnc_zlog_debug_verbose(
2960 "%s: attr, extra, or ecommunity missing, not importing",
2964 #ifdef RFAPI_REQUIRE_ENCAP_BEEC
2965 if (!rfapiEcommunitiesMatchBeec(attr
->ecommunity
)) {
2966 vnc_zlog_debug_verbose(
2967 "%s: it=%p: no match for BGP Encapsulation ecommunity",
2968 __func__
, import_table
);
2972 if (!rfapiEcommunitiesIntersect(import_table
->rt_import_list
,
2973 attr
->ecommunity
)) {
2975 vnc_zlog_debug_verbose(
2976 "%s: it=%p: no ecommunity intersection",
2977 __func__
, import_table
);
2982 * Updates must also have a nexthop address
2984 memset(&un_prefix
, 0,
2985 sizeof(un_prefix
)); /* keep valgrind happy */
2986 if (rfapiGetNexthop(attr
, &un_prefix
)) {
2987 vnc_zlog_debug_verbose("%s: missing nexthop address",
2994 * Figure out which radix tree the route would go into
2999 rt
= import_table
->imported_encap
[afi
];
3003 flog_err(EC_LIB_DEVELOPMENT
, "%s: bad afi %d", __func__
, afi
);
3008 * agg_node_lookup returns a node only if there is at least
3009 * one route attached.
3011 rn
= agg_node_lookup(rt
, p
);
3013 #ifdef DEBUG_ENCAP_MONITOR
3014 vnc_zlog_debug_verbose("%s: initial encap lookup(it=%p) rn=%p",
3015 __func__
, import_table
, rn
);
3020 RFAPI_CHECK_REFCOUNT(rn
, SAFI_ENCAP
, 1);
3021 agg_unlock_node(rn
); /* undo lock in agg_node_lookup */
3025 * capture nexthop of first bpi
3028 rfapiNexthop2Prefix(
3029 ((struct bgp_path_info
*)(rn
->info
))->attr
,
3033 for (bpi
= rn
->info
; bpi
; bpi
= bpi
->next
) {
3036 * Does this bgp_path_info refer to the same route
3037 * as we are trying to add?
3039 vnc_zlog_debug_verbose("%s: comparing BPI %p", __func__
,
3046 * RD of import table bpi is in
3047 * bpi->extra->vnc.import.rd RD of info_orig is in prd
3050 vnc_zlog_debug_verbose("%s: no bpi->extra",
3055 (struct prefix
*)&bpi
->extra
->vnc
.import
.rd
,
3056 (struct prefix
*)prd
)) {
3058 vnc_zlog_debug_verbose("%s: prd does not match",
3066 if (bpi
->peer
!= peer
) {
3067 vnc_zlog_debug_verbose(
3068 "%s: peer does not match", __func__
);
3072 vnc_zlog_debug_verbose("%s: found matching bpi",
3075 /* Same route. Delete this bpi, replace with new one */
3077 if (action
== FIF_ACTION_WITHDRAW
) {
3079 vnc_zlog_debug_verbose(
3080 "%s: withdrawing at prefix %pRN",
3083 rfapiBiStartWithdrawTimer(
3084 import_table
, rn
, bpi
, afi
, SAFI_ENCAP
,
3085 rfapiWithdrawTimerEncap
);
3088 vnc_zlog_debug_verbose(
3089 "%s: %s at prefix %pRN", __func__
,
3090 ((action
== FIF_ACTION_KILL
)
3096 * If this route is waiting to be deleted
3098 * a previous withdraw, we must cancel its
3101 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
)
3102 && bpi
->extra
->vnc
.import
.timer
) {
3105 (struct thread
*)bpi
->extra
->vnc
3107 struct rfapi_withdraw
*wcb
= t
->arg
;
3109 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
3113 if (action
== FIF_ACTION_UPDATE
) {
3114 rfapiBgpInfoDetach(rn
, bpi
);
3115 rfapiBgpInfoFree(bpi
);
3119 * Kill: do export stuff when removing
3122 struct rfapi_withdraw
*wcb
;
3126 * pretend we're an expiring timer
3129 MTYPE_RFAPI_WITHDRAW
,
3130 sizeof(struct rfapi_withdraw
));
3133 wcb
->import_table
= import_table
;
3134 memset(&t
, 0, sizeof(t
));
3136 rfapiWithdrawTimerEncap(
3137 &t
); /* frees wcb */
3146 RFAPI_CHECK_REFCOUNT(rn
, SAFI_ENCAP
, replacing
? 1 : 0);
3148 if (action
== FIF_ACTION_WITHDRAW
|| action
== FIF_ACTION_KILL
)
3152 rfapiBgpInfoCreate(attr
, peer
, rfd
, prd
, type
, sub_type
, NULL
);
3156 agg_lock_node(rn
); /* incr ref count for new BPI */
3158 rn
= agg_node_get(rt
, p
);
3161 vnc_zlog_debug_verbose("%s: (afi=%d, rn=%p) inserting at prefix %pRN",
3162 __func__
, afi
, rn
, rn
);
3164 rfapiBgpInfoAttachSorted(rn
, info_new
, afi
, SAFI_ENCAP
);
3167 * Delete holddown routes from same NVE. See details in
3168 * rfapiBgpInfoFilteredImportVPN()
3170 for (bpi
= info_new
->next
; bpi
; bpi
= next
) {
3172 struct prefix pfx_un
;
3176 if (!CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
))
3180 * We already match the VN address (it is the prefix
3181 * of the route node)
3184 if (!rfapiGetNexthop(bpi
->attr
, &pfx_un
)
3185 && prefix_same(&pfx_un
, &un_prefix
)) {
3193 vnc_zlog_debug_verbose(
3194 "%s: removing holddown bpi matching NVE of new route",
3196 if (bpi
->extra
->vnc
.import
.timer
) {
3198 (struct thread
*)bpi
->extra
->vnc
.import
.timer
;
3199 struct rfapi_withdraw
*wcb
= t
->arg
;
3201 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
3204 rfapiExpireEncapNow(import_table
, rn
, bpi
);
3207 rfapiNexthop2Prefix(((struct bgp_path_info
*)(rn
->info
))->attr
,
3211 * If the nexthop address of the selected Encap route (i.e.,
3212 * the UN address) has changed, then we must update the VPN
3213 * routes that refer to this Encap route and possibly force
3216 if (rfapiAttrNexthopAddrDifferent(&p_firstbpi_old
, &p_firstbpi_new
)) {
3218 struct rfapi_monitor_encap
*m
;
3219 struct rfapi_monitor_encap
*mnext
;
3221 struct agg_node
*referenced_vpn_prefix
;
3224 * Optimized approach: build radix tree on the fly to
3225 * hold list of VPN nodes referenced by the ENCAP monitors
3227 * The nodes in this table correspond to prefixes of VPN routes.
3228 * The "info" pointer of the node points to a chain of
3229 * struct rfapi_monitor_encap, each of which refers to a
3230 * specific VPN node.
3232 struct agg_table
*referenced_vpn_table
;
3234 referenced_vpn_table
= agg_table_init();
3235 assert(referenced_vpn_table
);
3238 * iterate over the set of monitors at this ENCAP node.
3240 #ifdef DEBUG_ENCAP_MONITOR
3241 vnc_zlog_debug_verbose("%s: examining monitors at rn=%p",
3244 for (m
= RFAPI_MONITOR_ENCAP(rn
); m
; m
= m
->next
) {
3245 const struct prefix
*p
;
3248 * For each referenced bpi/route, copy the ENCAP route's
3249 * nexthop to the VPN route's cached UN address field
3251 * the address family of the cached UN address field.
3253 rfapiCopyUnEncap2VPN(info_new
, m
->bpi
);
3254 if (!CHECK_FLAG(m
->bpi
->flags
, BGP_PATH_VALID
)) {
3255 SET_FLAG(m
->bpi
->flags
, BGP_PATH_VALID
);
3256 if (VALID_INTERIOR_TYPE(m
->bpi
->type
))
3257 RFAPI_MONITOR_EXTERIOR(m
->node
)
3258 ->valid_interior_count
++;
3259 vnc_import_bgp_exterior_add_route_interior(
3260 bgp
, import_table
, m
->node
, m
->bpi
);
3264 * Build a list of unique VPN nodes referenced by these
3267 * There could be more than one VPN node here with a
3269 * prefix. Those are currently in an unsorted linear
3273 p
= agg_node_get_prefix(m
->node
);
3274 referenced_vpn_prefix
=
3275 agg_node_get(referenced_vpn_table
, p
);
3276 assert(referenced_vpn_prefix
);
3277 for (mnext
= referenced_vpn_prefix
->info
; mnext
;
3278 mnext
= mnext
->next
) {
3280 if (mnext
->node
== m
->node
)
3286 * already have an entry for this VPN node
3288 agg_unlock_node(referenced_vpn_prefix
);
3291 MTYPE_RFAPI_MONITOR_ENCAP
,
3292 sizeof(struct rfapi_monitor_encap
));
3294 mnext
->node
= m
->node
;
3295 mnext
->next
= referenced_vpn_prefix
->info
;
3296 referenced_vpn_prefix
->info
= mnext
;
3301 * for each VPN node referenced in the ENCAP monitors:
3303 for (referenced_vpn_prefix
=
3304 agg_route_top(referenced_vpn_table
);
3305 referenced_vpn_prefix
;
3306 referenced_vpn_prefix
=
3307 agg_route_next(referenced_vpn_prefix
)) {
3309 while ((m
= referenced_vpn_prefix
->info
)) {
3313 rfapiMonitorMoveLonger(m
->node
);
3314 for (n
= m
->node
; n
; n
= agg_node_parent(n
)) {
3315 // rfapiDoRouteCallback(import_table, n,
3318 rfapiMonitorItNodeChanged(import_table
, m
->node
,
3321 referenced_vpn_prefix
->info
= m
->next
;
3322 agg_unlock_node(referenced_vpn_prefix
);
3323 XFREE(MTYPE_RFAPI_MONITOR_ENCAP
, m
);
3326 agg_table_finish(referenced_vpn_table
);
3329 RFAPI_CHECK_REFCOUNT(rn
, SAFI_ENCAP
, 0);
3332 static void rfapiExpireVpnNow(struct rfapi_import_table
*it
,
3333 struct agg_node
*rn
, struct bgp_path_info
*bpi
,
3336 struct rfapi_withdraw
*wcb
;
3340 * pretend we're an expiring timer
3342 wcb
= XCALLOC(MTYPE_RFAPI_WITHDRAW
, sizeof(struct rfapi_withdraw
));
3345 wcb
->import_table
= it
;
3346 wcb
->lockoffset
= lockoffset
;
3347 memset(&t
, 0, sizeof(t
));
3349 rfapiWithdrawTimerVPN(&t
); /* frees wcb */
3354 * import a bgp_path_info if its route target list intersects with the
3355 * import table's route target list
3357 void rfapiBgpInfoFilteredImportVPN(
3358 struct rfapi_import_table
*import_table
, int action
, struct peer
*peer
,
3359 void *rfd
, /* set for looped back routes */
3360 const struct prefix
*p
,
3361 const struct prefix
*aux_prefix
, /* AFI_L2VPN: optional IP */
3362 afi_t afi
, struct prefix_rd
*prd
,
3363 struct attr
*attr
, /* part of bgp_path_info */
3364 uint8_t type
, /* part of bgp_path_info */
3365 uint8_t sub_type
, /* part of bgp_path_info */
3366 uint32_t *label
) /* part of bgp_path_info */
3368 struct agg_table
*rt
= NULL
;
3369 struct agg_node
*rn
;
3371 struct bgp_path_info
*info_new
;
3372 struct bgp_path_info
*bpi
;
3373 struct bgp_path_info
*next
;
3375 struct prefix vn_prefix
;
3376 struct prefix un_prefix
;
3377 int un_prefix_valid
= 0;
3378 struct agg_node
*ern
;
3380 int original_had_routes
= 0;
3381 struct prefix original_nexthop
;
3382 const char *action_str
= NULL
;
3386 bgp
= bgp_get_default(); /* assume 1 instance for now */
3389 case FIF_ACTION_UPDATE
:
3390 action_str
= "update";
3392 case FIF_ACTION_WITHDRAW
:
3393 action_str
= "withdraw";
3395 case FIF_ACTION_KILL
:
3396 action_str
= "kill";
3403 if (import_table
== bgp
->rfapi
->it_ce
)
3406 vnc_zlog_debug_verbose("%s: entry: %s%s: prefix %s/%d: it %p, afi %s",
3407 __func__
, (is_it_ce
? "CE-IT " : ""), action_str
,
3408 rfapi_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
3409 p
->prefixlen
, import_table
, afi2str(afi
));
3414 * Compare rt lists. If no intersection, don't import this route
3415 * On a withdraw, peer and RD are sufficient to determine if
3418 if (action
== FIF_ACTION_UPDATE
) {
3419 if (!attr
|| !attr
->ecommunity
) {
3421 vnc_zlog_debug_verbose(
3422 "%s: attr, extra, or ecommunity missing, not importing",
3426 if ((import_table
!= bgp
->rfapi
->it_ce
)
3427 && !rfapiEcommunitiesIntersect(import_table
->rt_import_list
,
3428 attr
->ecommunity
)) {
3430 vnc_zlog_debug_verbose(
3431 "%s: it=%p: no ecommunity intersection",
3432 __func__
, import_table
);
3436 memset(&vn_prefix
, 0,
3437 sizeof(vn_prefix
)); /* keep valgrind happy */
3438 if (rfapiGetNexthop(attr
, &vn_prefix
)) {
3439 /* missing nexthop address would be a bad, bad thing */
3440 vnc_zlog_debug_verbose("%s: missing nexthop", __func__
);
3446 * Figure out which radix tree the route would go into
3452 rt
= import_table
->imported_vpn
[afi
];
3456 flog_err(EC_LIB_DEVELOPMENT
, "%s: bad afi %d", __func__
, afi
);
3461 memset(&original_nexthop
, 0, sizeof(original_nexthop
));
3464 * agg_node_lookup returns a node only if there is at least
3465 * one route attached.
3467 rn
= agg_node_lookup(rt
, p
);
3469 vnc_zlog_debug_verbose("%s: rn=%p", __func__
, rn
);
3473 RFAPI_CHECK_REFCOUNT(rn
, SAFI_MPLS_VPN
, 1);
3474 agg_unlock_node(rn
); /* undo lock in agg_node_lookup */
3477 original_had_routes
= 1;
3479 if (VNC_DEBUG(VERBOSE
)) {
3480 vnc_zlog_debug_verbose("%s: showing IT node on entry",
3482 rfapiShowItNode(NULL
, rn
); /* debug */
3486 * Look for same route (will have same RD and peer)
3488 bpi
= rfapiItBiIndexSearch(rn
, prd
, peer
, aux_prefix
);
3493 * This was an old test when we iterated over the
3494 * BPIs linearly. Since we're now looking up with
3495 * RD and peer, comparing types should not be
3496 * needed. Changed to assertion.
3498 * Compare types. Doing so prevents a RFP-originated
3499 * route from matching an imported route, for example.
3501 if (VNC_DEBUG(VERBOSE
) && bpi
->type
!= type
)
3502 /* should be handled by RDs, but warn for now */
3503 zlog_warn("%s: type mismatch! (bpi=%d, arg=%d)",
3504 __func__
, bpi
->type
, type
);
3506 vnc_zlog_debug_verbose("%s: found matching bpi",
3510 * In the special CE table, withdrawals occur without
3513 if (import_table
== bgp
->rfapi
->it_ce
) {
3514 vnc_direct_bgp_del_route_ce(bgp
, rn
, bpi
);
3515 if (action
== FIF_ACTION_WITHDRAW
)
3516 action
= FIF_ACTION_KILL
;
3519 if (action
== FIF_ACTION_WITHDRAW
) {
3521 int washolddown
= CHECK_FLAG(bpi
->flags
,
3524 vnc_zlog_debug_verbose(
3525 "%s: withdrawing at prefix %pRN%s",
3528 ? " (already being withdrawn)"
3533 rfapiBiStartWithdrawTimer(
3534 import_table
, rn
, bpi
, afi
,
3536 rfapiWithdrawTimerVPN
);
3538 RFAPI_UPDATE_ITABLE_COUNT(
3539 bpi
, import_table
, afi
, -1);
3540 import_table
->holddown_count
[afi
] += 1;
3544 vnc_zlog_debug_verbose(
3545 "%s: %s at prefix %pRN", __func__
,
3546 ((action
== FIF_ACTION_KILL
)
3552 * If this route is waiting to be deleted
3554 * a previous withdraw, we must cancel its
3557 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
)
3558 && bpi
->extra
->vnc
.import
.timer
) {
3561 (struct thread
*)bpi
->extra
->vnc
3563 struct rfapi_withdraw
*wcb
= t
->arg
;
3565 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
3568 import_table
->holddown_count
[afi
] -= 1;
3569 RFAPI_UPDATE_ITABLE_COUNT(
3570 bpi
, import_table
, afi
, 1);
3573 * decrement remote count (if route is remote)
3575 * we are going to remove it below
3577 RFAPI_UPDATE_ITABLE_COUNT(bpi
, import_table
,
3579 if (action
== FIF_ACTION_UPDATE
) {
3583 * make copy of original nexthop so we
3584 * can see if it changed
3586 rfapiGetNexthop(bpi
->attr
,
3590 * remove bpi without doing any export
3593 if (CHECK_FLAG(bpi
->flags
,
3595 && VALID_INTERIOR_TYPE(bpi
->type
))
3596 RFAPI_MONITOR_EXTERIOR(rn
)
3597 ->valid_interior_count
--;
3598 rfapiItBiIndexDel(rn
, bpi
);
3599 rfapiBgpInfoDetach(rn
, bpi
);
3600 rfapiMonitorEncapDelete(bpi
);
3601 vnc_import_bgp_exterior_del_route_interior(
3602 bgp
, import_table
, rn
, bpi
);
3603 rfapiBgpInfoFree(bpi
);
3607 * remove bpi and do export processing
3609 import_table
->holddown_count
[afi
] += 1;
3610 rfapiExpireVpnNow(import_table
, rn
, bpi
,
3618 RFAPI_CHECK_REFCOUNT(rn
, SAFI_MPLS_VPN
, replacing
? 1 : 0);
3620 if (action
== FIF_ACTION_WITHDRAW
|| action
== FIF_ACTION_KILL
) {
3626 rfapiBgpInfoCreate(attr
, peer
, rfd
, prd
, type
, sub_type
, label
);
3629 * lookup un address in encap table
3631 ern
= agg_node_match(import_table
->imported_encap
[afi
], &vn_prefix
);
3633 rfapiCopyUnEncap2VPN(ern
->info
, info_new
);
3634 agg_unlock_node(ern
); /* undo lock in route_note_match */
3636 char bpf
[PREFIX_STRLEN
];
3638 prefix2str(&vn_prefix
, bpf
, sizeof(bpf
));
3639 /* Not a big deal, just means VPN route got here first */
3640 vnc_zlog_debug_verbose("%s: no encap route for vn addr %s",
3642 info_new
->extra
->vnc
.import
.un_family
= 0;
3650 * No need to increment reference count, so only "get"
3651 * if the node is not there already
3653 rn
= agg_node_get(rt
, p
);
3657 * For ethernet routes, if there is an accompanying IP address,
3658 * save it in the bpi
3660 if ((AFI_L2VPN
== afi
) && aux_prefix
) {
3662 vnc_zlog_debug_verbose("%s: setting BPI's aux_prefix",
3664 info_new
->extra
->vnc
.import
.aux_prefix
= *aux_prefix
;
3667 vnc_zlog_debug_verbose("%s: inserting bpi %p at prefix %pRN #%d",
3668 __func__
, info_new
, rn
, rn
->lock
);
3670 rfapiBgpInfoAttachSorted(rn
, info_new
, afi
, SAFI_MPLS_VPN
);
3671 rfapiItBiIndexAdd(rn
, info_new
);
3672 if (!rfapiGetUnAddrOfVpnBi(info_new
, NULL
)) {
3673 if (VALID_INTERIOR_TYPE(info_new
->type
))
3674 RFAPI_MONITOR_EXTERIOR(rn
)->valid_interior_count
++;
3675 SET_FLAG(info_new
->flags
, BGP_PATH_VALID
);
3677 RFAPI_UPDATE_ITABLE_COUNT(info_new
, import_table
, afi
, 1);
3678 vnc_import_bgp_exterior_add_route_interior(bgp
, import_table
, rn
,
3681 if (import_table
== bgp
->rfapi
->it_ce
)
3682 vnc_direct_bgp_add_route_ce(bgp
, rn
, info_new
);
3684 if (VNC_DEBUG(VERBOSE
)) {
3685 vnc_zlog_debug_verbose("%s: showing IT node", __func__
);
3686 rfapiShowItNode(NULL
, rn
); /* debug */
3689 rfapiMonitorEncapAdd(import_table
, &vn_prefix
, rn
, info_new
);
3691 if (!rfapiGetUnAddrOfVpnBi(info_new
, &un_prefix
)) {
3694 * if we have a valid UN address (either via Encap route
3695 * or via tunnel attribute), then we should attempt
3696 * to move any monitors at less-specific nodes to this node
3698 rfapiMonitorMoveLonger(rn
);
3700 un_prefix_valid
= 1;
3704 * 101129 Enhancement: if we add a route (implication: it is not
3705 * in holddown), delete all other routes from this nve at this
3706 * node that are in holddown, regardless of peer.
3708 * Reasons it's OK to do that:
3710 * - if the holddown route being deleted originally came from BGP VPN,
3711 * it is already gone from BGP (implication of holddown), so there
3712 * won't be any added inconsistency with the BGP RIB.
3714 * - once a fresh route is added at a prefix, any routes in holddown
3715 * at that prefix will not show up in RFP responses, so deleting
3716 * the holddown routes won't affect the contents of responses.
3718 * - lifetimes are supposed to be consistent, so there should not
3719 * be a case where the fresh route has a shorter lifetime than
3720 * the holddown route, so we don't expect the fresh route to
3721 * disappear and complete its holddown time before the existing
3722 * holddown routes time out. Therefore, we won't have a situation
3723 * where we expect the existing holddown routes to be hidden and
3724 * then to reappear sometime later (as holddown routes) in a
3727 * Among other things, this would enable us to skirt the problem
3728 * of local holddown routes that refer to NVE descriptors that
3729 * have already been closed (if the same NVE triggers a subsequent
3730 * rfapi_open(), the new peer is different and doesn't match the
3731 * peer of the holddown route, so the stale holddown route still
3732 * hangs around until it times out instead of just being replaced
3733 * by the fresh route).
3736 * We know that the new bpi will have been inserted before any routes
3737 * in holddown, so we can skip any that came before it
3739 for (bpi
= info_new
->next
; bpi
; bpi
= next
) {
3741 struct prefix pfx_vn
;
3742 struct prefix pfx_un
;
3744 int remote_peer_match
= 0;
3751 if (!CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
))
3755 * Must match VN address (nexthop of VPN route)
3757 if (rfapiGetNexthop(bpi
->attr
, &pfx_vn
))
3759 if (!prefix_same(&pfx_vn
, &vn_prefix
))
3762 if (un_prefix_valid
&& /* new route UN addr */
3763 !rfapiGetUnAddrOfVpnBi(bpi
, &pfx_un
)
3764 && /* old route UN addr */
3765 prefix_same(&pfx_un
, &un_prefix
)) { /* compare */
3768 if (!RFAPI_LOCAL_BI(bpi
) && !RFAPI_LOCAL_BI(info_new
)
3769 && sockunion_same(&bpi
->peer
->su
, &info_new
->peer
->su
)) {
3770 /* old & new are both remote, same peer */
3771 remote_peer_match
= 1;
3774 if (!un_match
& !remote_peer_match
)
3777 vnc_zlog_debug_verbose(
3778 "%s: removing holddown bpi matching NVE of new route",
3780 if (bpi
->extra
->vnc
.import
.timer
) {
3782 (struct thread
*)bpi
->extra
->vnc
.import
.timer
;
3783 struct rfapi_withdraw
*wcb
= t
->arg
;
3785 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
3788 rfapiExpireVpnNow(import_table
, rn
, bpi
, 0);
3791 if (!original_had_routes
) {
3793 * We went from 0 usable routes to 1 usable route. Perform the
3794 * "Adding a Route" export process.
3796 vnc_direct_bgp_add_prefix(bgp
, import_table
, rn
);
3797 vnc_zebra_add_prefix(bgp
, import_table
, rn
);
3800 * Check for nexthop change event
3801 * Note: the prefix_same() test below detects two situations:
3802 * 1. route is replaced, new route has different nexthop
3803 * 2. new route is added (original_nexthop is 0)
3805 struct prefix new_nexthop
;
3807 rfapiGetNexthop(attr
, &new_nexthop
);
3808 if (!prefix_same(&original_nexthop
, &new_nexthop
)) {
3810 * nexthop change event
3811 * vnc_direct_bgp_add_prefix() will recompute VN addr
3814 vnc_direct_bgp_add_prefix(bgp
, import_table
, rn
);
3818 if (!(bgp
->rfapi_cfg
->flags
& BGP_VNC_CONFIG_CALLBACK_DISABLE
)) {
3819 for (n
= rn
; n
; n
= agg_node_parent(n
)) {
3820 // rfapiDoRouteCallback(import_table, n, NULL);
3822 rfapiMonitorItNodeChanged(import_table
, rn
, NULL
);
3824 RFAPI_CHECK_REFCOUNT(rn
, SAFI_MPLS_VPN
, 0);
3828 static void rfapiBgpInfoFilteredImportBadSafi(
3829 struct rfapi_import_table
*import_table
, int action
, struct peer
*peer
,
3830 void *rfd
, /* set for looped back routes */
3831 const struct prefix
*p
,
3832 const struct prefix
*aux_prefix
, /* AFI_L2VPN: optional IP */
3833 afi_t afi
, struct prefix_rd
*prd
,
3834 struct attr
*attr
, /* part of bgp_path_info */
3835 uint8_t type
, /* part of bgp_path_info */
3836 uint8_t sub_type
, /* part of bgp_path_info */
3837 uint32_t *label
) /* part of bgp_path_info */
3839 vnc_zlog_debug_verbose("%s: Error, bad safi", __func__
);
3842 static rfapi_bi_filtered_import_f
*
3843 rfapiBgpInfoFilteredImportFunction(safi_t safi
)
3847 return rfapiBgpInfoFilteredImportVPN
;
3850 return rfapiBgpInfoFilteredImportEncap
;
3854 flog_err(EC_LIB_DEVELOPMENT
, "%s: bad safi %d", __func__
, safi
);
3855 return rfapiBgpInfoFilteredImportBadSafi
;
3859 void rfapiProcessUpdate(struct peer
*peer
,
3860 void *rfd
, /* set when looped from RFP/RFAPI */
3861 const struct prefix
*p
, struct prefix_rd
*prd
,
3862 struct attr
*attr
, afi_t afi
, safi_t safi
, uint8_t type
,
3863 uint8_t sub_type
, uint32_t *label
)
3867 struct rfapi_import_table
*it
;
3868 int has_ip_route
= 1;
3871 bgp
= bgp_get_default(); /* assume 1 instance for now */
3878 * look at high-order byte of RD. FF means MAC
3879 * address is present (VNC L2VPN)
3881 if ((safi
== SAFI_MPLS_VPN
)
3882 && (decode_rd_type(prd
->val
) == RD_TYPE_VNC_ETH
)) {
3883 struct prefix pfx_mac_buf
;
3884 struct prefix pfx_nexthop_buf
;
3888 * Set flag if prefix and nexthop are the same - don't
3889 * add the route to normal IP-based import tables
3891 if (!rfapiGetNexthop(attr
, &pfx_nexthop_buf
)) {
3892 if (!prefix_cmp(&pfx_nexthop_buf
, p
)) {
3897 memset(&pfx_mac_buf
, 0, sizeof(pfx_mac_buf
));
3898 pfx_mac_buf
.family
= AF_ETHERNET
;
3899 pfx_mac_buf
.prefixlen
= 48;
3900 memcpy(&pfx_mac_buf
.u
.prefix_eth
.octet
, prd
->val
+ 2, 6);
3903 * Find rt containing LNI (Logical Network ID), which
3904 * _should_ always be present when mac address is present
3906 rc
= rfapiEcommunityGetLNI(attr
->ecommunity
, &lni
);
3908 vnc_zlog_debug_verbose(
3909 "%s: rfapiEcommunityGetLNI returned %d, lni=%d, attr=%p",
3910 __func__
, rc
, lni
, attr
);
3912 it
= rfapiMacImportTableGet(bgp
, lni
);
3914 rfapiBgpInfoFilteredImportVPN(
3915 it
, FIF_ACTION_UPDATE
, peer
, rfd
,
3916 &pfx_mac_buf
, /* prefix */
3917 p
, /* aux prefix: IP addr */
3918 AFI_L2VPN
, prd
, attr
, type
, sub_type
, label
);
3926 * Iterate over all import tables; do a filtered import
3927 * for the afi/safi combination
3929 for (it
= h
->imports
; it
; it
= it
->next
) {
3930 (*rfapiBgpInfoFilteredImportFunction(safi
))(
3931 it
, FIF_ACTION_UPDATE
, peer
, rfd
, p
, /* prefix */
3932 NULL
, afi
, prd
, attr
, type
, sub_type
, label
);
3935 if (safi
== SAFI_MPLS_VPN
) {
3936 vnc_direct_bgp_rh_add_route(bgp
, afi
, p
, peer
, attr
);
3937 rfapiBgpInfoFilteredImportVPN(
3938 bgp
->rfapi
->it_ce
, FIF_ACTION_UPDATE
, peer
, rfd
,
3940 NULL
, afi
, prd
, attr
, type
, sub_type
, label
);
3945 void rfapiProcessWithdraw(struct peer
*peer
, void *rfd
, const struct prefix
*p
,
3946 struct prefix_rd
*prd
, struct attr
*attr
, afi_t afi
,
3947 safi_t safi
, uint8_t type
, int kill
)
3951 struct rfapi_import_table
*it
;
3953 bgp
= bgp_get_default(); /* assume 1 instance for now */
3960 * look at high-order byte of RD. FF means MAC
3961 * address is present (VNC L2VPN)
3963 if (h
->import_mac
!= NULL
&& safi
== SAFI_MPLS_VPN
3964 && decode_rd_type(prd
->val
) == RD_TYPE_VNC_ETH
) {
3965 struct prefix pfx_mac_buf
;
3966 void *cursor
= NULL
;
3969 memset(&pfx_mac_buf
, 0, sizeof(pfx_mac_buf
));
3970 pfx_mac_buf
.family
= AF_ETHERNET
;
3971 pfx_mac_buf
.prefixlen
= 48;
3972 memcpy(&pfx_mac_buf
.u
.prefix_eth
, prd
->val
+ 2, 6);
3975 * withdraw does not contain attrs, so we don't have
3976 * access to the route's LNI, which would ordinarily
3977 * select the specific mac-based import table. Instead,
3978 * we must iterate over all mac-based tables and rely
3979 * on the RD to match.
3981 * If this approach is too slow, add an index where
3982 * key is {RD, peer} and value is the import table
3984 for (rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
3986 rc
== 0; rc
= skiplist_next(h
->import_mac
, NULL
,
3987 (void **)&it
, &cursor
)) {
3989 #ifdef DEBUG_L2_EXTRA
3990 vnc_zlog_debug_verbose(
3991 "%s: calling rfapiBgpInfoFilteredImportVPN(it=%p, afi=AFI_L2VPN)",
3995 rfapiBgpInfoFilteredImportVPN(
3997 (kill
? FIF_ACTION_KILL
: FIF_ACTION_WITHDRAW
),
3998 peer
, rfd
, &pfx_mac_buf
, /* prefix */
3999 p
, /* aux_prefix: IP */
4000 AFI_L2VPN
, prd
, attr
, type
, 0,
4001 NULL
); /* sub_type & label unused for withdraw
4007 * XXX For the case where the withdraw involves an L2
4008 * route with no IP information, we rely on the lack
4009 * of RT-list intersection to filter out the withdraw
4010 * from the IP-based import tables below
4014 * Iterate over all import tables; do a filtered import
4015 * for the afi/safi combination
4018 for (it
= h
->imports
; it
; it
= it
->next
) {
4019 (*rfapiBgpInfoFilteredImportFunction(safi
))(
4020 it
, (kill
? FIF_ACTION_KILL
: FIF_ACTION_WITHDRAW
),
4021 peer
, rfd
, p
, /* prefix */
4022 NULL
, afi
, prd
, attr
, type
, 0,
4023 NULL
); /* sub_type & label unused for withdraw */
4026 /* TBD the deletion should happen after the lifetime expires */
4027 if (safi
== SAFI_MPLS_VPN
)
4028 vnc_direct_bgp_rh_del_route(bgp
, afi
, p
, peer
);
4030 if (safi
== SAFI_MPLS_VPN
) {
4031 rfapiBgpInfoFilteredImportVPN(
4033 (kill
? FIF_ACTION_KILL
: FIF_ACTION_WITHDRAW
), peer
,
4034 rfd
, p
, /* prefix */
4035 NULL
, afi
, prd
, attr
, type
, 0,
4036 NULL
); /* sub_type & label unused for withdraw */
4041 * TBD optimized withdraw timer algorithm for case of many
4042 * routes expiring at the same time due to peer drop.
4045 * 1. Visit all BPIs in all ENCAP import tables.
4047 * a. If a bpi's peer is the failed peer, remove the bpi.
4048 * b. If the removed ENCAP bpi was first in the list of
4049 * BPIs at this ENCAP node, loop over all monitors
4052 * (1) for each ENCAP monitor, loop over all its
4053 * VPN node monitors and set their RFAPI_MON_FLAG_NEEDCALLBACK
4056 * 2. Visit all BPIs in all VPN import tables.
4057 * a. If a bpi's peer is the failed peer, remove the bpi.
4058 * b. loop over all the VPN node monitors and set their
4059 * RFAPI_MON_FLAG_NEEDCALLBACK flags
4060 * c. If there are no BPIs left at this VPN node,
4065 /* surprise, this gets called from peer_delete(), from rfapi_close() */
4066 static void rfapiProcessPeerDownRt(struct peer
*peer
,
4067 struct rfapi_import_table
*import_table
,
4068 afi_t afi
, safi_t safi
)
4070 struct agg_node
*rn
;
4071 struct bgp_path_info
*bpi
;
4072 struct agg_table
*rt
;
4073 int (*timer_service_func
)(struct thread
*);
4075 assert(afi
== AFI_IP
|| afi
== AFI_IP6
);
4081 rt
= import_table
->imported_vpn
[afi
];
4082 timer_service_func
= rfapiWithdrawTimerVPN
;
4085 rt
= import_table
->imported_encap
[afi
];
4086 timer_service_func
= rfapiWithdrawTimerEncap
;
4089 /* Suppress uninitialized variable warning */
4091 timer_service_func
= NULL
;
4096 for (rn
= agg_route_top(rt
); rn
; rn
= agg_route_next(rn
)) {
4097 for (bpi
= rn
->info
; bpi
; bpi
= bpi
->next
) {
4098 if (bpi
->peer
== peer
) {
4100 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
)) {
4101 /* already in holddown, skip */
4105 if (safi
== SAFI_MPLS_VPN
) {
4106 RFAPI_UPDATE_ITABLE_COUNT(
4107 bpi
, import_table
, afi
, -1);
4108 import_table
->holddown_count
[afi
] += 1;
4110 rfapiBiStartWithdrawTimer(import_table
, rn
, bpi
,
4112 timer_service_func
);
4120 * This gets called when a peer connection drops. We have to remove
4121 * all the routes from this peer.
4123 * Current approach is crude. TBD Optimize by setting fewer timers and
4124 * grouping withdrawn routes so we can generate callbacks more
4127 void rfapiProcessPeerDown(struct peer
*peer
)
4131 struct rfapi_import_table
*it
;
4134 * If this peer is a "dummy" peer structure atached to a RFAPI
4135 * nve_descriptor, we don't need to walk the import tables
4136 * because the routes are already withdrawn by rfapi_close()
4138 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_IS_RFAPI_HD
))
4142 * 1. Visit all BPIs in all ENCAP import tables.
4143 * Start withdraw timer on the BPIs that match peer.
4145 * 2. Visit All BPIs in all VPN import tables.
4146 * Start withdraw timer on the BPIs that match peer.
4149 bgp
= bgp_get_default(); /* assume 1 instance for now */
4156 for (it
= h
->imports
; it
; it
= it
->next
) {
4157 rfapiProcessPeerDownRt(peer
, it
, AFI_IP
, SAFI_ENCAP
);
4158 rfapiProcessPeerDownRt(peer
, it
, AFI_IP6
, SAFI_ENCAP
);
4159 rfapiProcessPeerDownRt(peer
, it
, AFI_IP
, SAFI_MPLS_VPN
);
4160 rfapiProcessPeerDownRt(peer
, it
, AFI_IP6
, SAFI_MPLS_VPN
);
4164 rfapiProcessPeerDownRt(peer
, h
->it_ce
, AFI_IP
, SAFI_MPLS_VPN
);
4165 rfapiProcessPeerDownRt(peer
, h
->it_ce
, AFI_IP6
, SAFI_MPLS_VPN
);
4170 * Import an entire RIB (for an afi/safi) to an import table RIB,
4171 * filtered according to the import table's RT list
4173 * TBD: does this function need additions to match rfapiProcessUpdate()
4174 * for, e.g., L2 handling?
4176 static void rfapiBgpTableFilteredImport(struct bgp
*bgp
,
4177 struct rfapi_import_table
*it
,
4178 afi_t afi
, safi_t safi
)
4180 struct bgp_node
*rn1
;
4181 struct bgp_node
*rn2
;
4183 /* Only these SAFIs have 2-level RIBS */
4184 assert(safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
);
4187 * Now visit all the rd nodes and the nodes of all the
4188 * route tables attached to them, and import the routes
4189 * if they have matching route targets
4191 for (rn1
= bgp_table_top(bgp
->rib
[afi
][safi
]); rn1
;
4192 rn1
= bgp_route_next(rn1
)) {
4194 if (bgp_node_has_bgp_path_info_data(rn1
)) {
4196 for (rn2
= bgp_table_top(bgp_node_get_bgp_table_info(rn1
)); rn2
;
4197 rn2
= bgp_route_next(rn2
)) {
4199 struct bgp_path_info
*bpi
;
4201 for (bpi
= bgp_node_get_bgp_path_info(rn2
);
4202 bpi
; bpi
= bpi
->next
) {
4205 if (CHECK_FLAG(bpi
->flags
,
4210 label
= decode_label(
4211 &bpi
->extra
->label
[0]);
4212 (*rfapiBgpInfoFilteredImportFunction(
4214 it
, /* which import table */
4215 FIF_ACTION_UPDATE
, bpi
->peer
,
4216 NULL
, bgp_node_get_prefix(rn2
),
4218 (struct prefix_rd
*)
4219 bgp_node_get_prefix(
4221 bpi
->attr
, bpi
->type
,
4222 bpi
->sub_type
, &label
);
4230 /* per-bgp-instance rfapi data */
4231 struct rfapi
*bgp_rfapi_new(struct bgp
*bgp
)
4235 struct rfapi_rfp_cfg
*cfg
= NULL
;
4236 struct rfapi_rfp_cb_methods
*cbm
= NULL
;
4238 assert(bgp
->rfapi_cfg
== NULL
);
4240 h
= XCALLOC(MTYPE_RFAPI
, sizeof(struct rfapi
));
4242 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
4243 h
->un
[afi
] = agg_table_init();
4247 * initialize the ce import table
4249 h
->it_ce
= XCALLOC(MTYPE_RFAPI_IMPORTTABLE
,
4250 sizeof(struct rfapi_import_table
));
4251 h
->it_ce
->imported_vpn
[AFI_IP
] = agg_table_init();
4252 h
->it_ce
->imported_vpn
[AFI_IP6
] = agg_table_init();
4253 h
->it_ce
->imported_encap
[AFI_IP
] = agg_table_init();
4254 h
->it_ce
->imported_encap
[AFI_IP6
] = agg_table_init();
4255 rfapiBgpTableFilteredImport(bgp
, h
->it_ce
, AFI_IP
, SAFI_MPLS_VPN
);
4256 rfapiBgpTableFilteredImport(bgp
, h
->it_ce
, AFI_IP6
, SAFI_MPLS_VPN
);
4259 * Set up work queue for deferred rfapi_close operations
4261 h
->deferred_close_q
=
4262 work_queue_new(bm
->master
, "rfapi deferred close");
4263 h
->deferred_close_q
->spec
.workfunc
= rfapi_deferred_close_workfunc
;
4264 h
->deferred_close_q
->spec
.data
= h
;
4266 h
->rfp
= rfp_start(bm
->master
, &cfg
, &cbm
);
4267 bgp
->rfapi_cfg
= bgp_rfapi_cfg_new(cfg
);
4269 h
->rfp_methods
= *cbm
;
4274 void bgp_rfapi_destroy(struct bgp
*bgp
, struct rfapi
*h
)
4278 if (bgp
== NULL
|| h
== NULL
)
4281 if (h
->resolve_nve_nexthop
) {
4282 skiplist_free(h
->resolve_nve_nexthop
);
4283 h
->resolve_nve_nexthop
= NULL
;
4286 agg_table_finish(h
->it_ce
->imported_vpn
[AFI_IP
]);
4287 agg_table_finish(h
->it_ce
->imported_vpn
[AFI_IP6
]);
4288 agg_table_finish(h
->it_ce
->imported_encap
[AFI_IP
]);
4289 agg_table_finish(h
->it_ce
->imported_encap
[AFI_IP6
]);
4291 if (h
->import_mac
) {
4292 struct rfapi_import_table
*it
;
4297 rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4299 !rc
; rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4302 rfapiImportTableFlush(it
);
4303 XFREE(MTYPE_RFAPI_IMPORTTABLE
, it
);
4305 skiplist_free(h
->import_mac
);
4306 h
->import_mac
= NULL
;
4309 work_queue_free_and_null(&h
->deferred_close_q
);
4314 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
4315 agg_table_finish(h
->un
[afi
]);
4318 XFREE(MTYPE_RFAPI_IMPORTTABLE
, h
->it_ce
);
4319 XFREE(MTYPE_RFAPI
, h
);
4322 struct rfapi_import_table
*
4323 rfapiImportTableRefAdd(struct bgp
*bgp
, struct ecommunity
*rt_import_list
,
4324 struct rfapi_nve_group_cfg
*rfg
)
4327 struct rfapi_import_table
*it
;
4333 for (it
= h
->imports
; it
; it
= it
->next
) {
4334 if (ecommunity_cmp(it
->rt_import_list
, rt_import_list
))
4338 vnc_zlog_debug_verbose("%s: matched it=%p", __func__
, it
);
4341 it
= XCALLOC(MTYPE_RFAPI_IMPORTTABLE
,
4342 sizeof(struct rfapi_import_table
));
4344 it
->next
= h
->imports
;
4347 it
->rt_import_list
= ecommunity_dup(rt_import_list
);
4349 it
->monitor_exterior_orphans
=
4350 skiplist_new(0, NULL
, prefix_free_lists
);
4353 * fill import route tables from RIBs
4355 * Potential area for optimization. If this occurs when
4356 * tables are large (e.g., the operator adds a nve group
4357 * with a new RT list to a running system), it could take
4361 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
4363 it
->imported_vpn
[afi
] = agg_table_init();
4364 it
->imported_encap
[afi
] = agg_table_init();
4366 rfapiBgpTableFilteredImport(bgp
, it
, afi
,
4368 rfapiBgpTableFilteredImport(bgp
, it
, afi
, SAFI_ENCAP
);
4370 vnc_import_bgp_exterior_redist_enable_it(bgp
, afi
, it
);
4380 * skiplist element free function
4382 static void delete_rem_pfx_na_free(void *na
)
4384 uint32_t *pCounter
= ((struct rfapi_nve_addr
*)na
)->info
;
4387 XFREE(MTYPE_RFAPI_NVE_ADDR
, na
);
4391 * Common deleter for IP and MAC import tables
4393 static void rfapiDeleteRemotePrefixesIt(
4394 struct bgp
*bgp
, struct rfapi_import_table
*it
, struct prefix
*un
,
4395 struct prefix
*vn
, struct prefix
*p
, int delete_active
,
4396 int delete_holddown
, uint32_t *pARcount
, uint32_t *pAHcount
,
4397 uint32_t *pHRcount
, uint32_t *pHHcount
,
4398 struct skiplist
*uniq_active_nves
, struct skiplist
*uniq_holddown_nves
)
4402 #ifdef DEBUG_L2_EXTRA
4404 char buf_pfx
[PREFIX_STRLEN
];
4407 prefix2str(p
, buf_pfx
, sizeof(buf_pfx
));
4413 vnc_zlog_debug_verbose(
4414 "%s: entry, p=%s, delete_active=%d, delete_holddown=%d",
4415 __func__
, buf_pfx
, delete_active
, delete_holddown
);
4419 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
4421 struct agg_table
*rt
;
4422 struct agg_node
*rn
;
4424 if (p
&& (family2afi(p
->family
) != afi
)) {
4428 rt
= it
->imported_vpn
[afi
];
4432 vnc_zlog_debug_verbose("%s: scanning rt for afi=%d", __func__
,
4435 for (rn
= agg_route_top(rt
); rn
; rn
= agg_route_next(rn
)) {
4436 struct bgp_path_info
*bpi
;
4437 struct bgp_path_info
*next
;
4438 const struct prefix
*rn_p
= agg_node_get_prefix(rn
);
4440 if (p
&& VNC_DEBUG(IMPORT_DEL_REMOTE
)) {
4441 char p1line
[PREFIX_STRLEN
];
4443 prefix2str(p
, p1line
, sizeof(p1line
));
4444 vnc_zlog_debug_any("%s: want %s, have %pRN",
4445 __func__
, p1line
, rn
);
4448 if (p
&& prefix_cmp(p
, rn_p
))
4451 vnc_zlog_debug_verbose("%s: rn pfx=%pRN", __func__
, rn
);
4453 /* TBD is this valid for afi == AFI_L2VPN? */
4454 RFAPI_CHECK_REFCOUNT(rn
, SAFI_MPLS_VPN
, 1);
4456 for (bpi
= rn
->info
; bpi
; bpi
= next
) {
4465 vnc_zlog_debug_verbose("%s: examining bpi %p",
4468 if (!rfapiGetNexthop(bpi
->attr
, &qpt
))
4473 || !prefix_match(vn
, &qpt
)) {
4474 #ifdef DEBUG_L2_EXTRA
4475 vnc_zlog_debug_verbose(
4476 "%s: continue at vn && !qpt_valid || !prefix_match(vn, &qpt)",
4483 if (!rfapiGetUnAddrOfVpnBi(bpi
, &qct
))
4488 || !prefix_match(un
, &qct
)) {
4489 #ifdef DEBUG_L2_EXTRA
4490 vnc_zlog_debug_verbose(
4491 "%s: continue at un && !qct_valid || !prefix_match(un, &qct)",
4503 * If this route is waiting to be deleted
4505 * a previous withdraw, we must cancel its
4508 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
)) {
4509 if (!delete_holddown
)
4511 if (bpi
->extra
->vnc
.import
.timer
) {
4514 (struct thread
*)bpi
4517 struct rfapi_withdraw
*wcb
=
4521 ->holddown_count
[afi
] -=
4523 RFAPI_UPDATE_ITABLE_COUNT(
4524 bpi
, wcb
->import_table
,
4526 XFREE(MTYPE_RFAPI_WITHDRAW
,
4536 vnc_zlog_debug_verbose(
4537 "%s: deleting bpi %p (qct_valid=%d, qpt_valid=%d, delete_holddown=%d, delete_active=%d)",
4538 __func__
, bpi
, qct_valid
, qpt_valid
,
4539 delete_holddown
, delete_active
);
4545 if (qct_valid
&& qpt_valid
) {
4547 struct rfapi_nve_addr na
;
4548 struct rfapi_nve_addr
*nap
;
4550 memset(&na
, 0, sizeof(na
));
4551 assert(!rfapiQprefix2Raddr(&qct
,
4553 assert(!rfapiQprefix2Raddr(&qpt
,
4556 if (skiplist_search(
4559 : uniq_holddown_nves
),
4560 &na
, (void **)&nap
)) {
4564 MTYPE_RFAPI_NVE_ADDR
,
4569 nap
->info
= is_active
4575 : uniq_holddown_nves
),
4578 rfapiNveAddr2Str(nap
, line
,
4583 vnc_direct_bgp_rh_del_route(bgp
, afi
, rn_p
,
4586 RFAPI_UPDATE_ITABLE_COUNT(bpi
, it
, afi
, -1);
4587 it
->holddown_count
[afi
] += 1;
4588 rfapiExpireVpnNow(it
, rn
, bpi
, 1);
4590 vnc_zlog_debug_verbose(
4591 "%s: incrementing count (is_active=%d)",
4592 __func__
, is_active
);
4605 * For use by the "clear vnc prefixes" command
4607 /*------------------------------------------
4608 * rfapiDeleteRemotePrefixes
4610 * UI helper: For use by the "clear vnc prefixes" command
4613 * un if set, tunnel must match this prefix
4614 * vn if set, nexthop prefix must match this prefix
4615 * p if set, prefix must match this prefix
4616 * it if set, only look in this import table
4619 * pARcount number of active routes deleted
4620 * pAHcount number of active nves deleted
4621 * pHRcount number of holddown routes deleted
4622 * pHHcount number of holddown nves deleted
4626 --------------------------------------------*/
4627 void rfapiDeleteRemotePrefixes(struct prefix
*un
, struct prefix
*vn
,
4629 struct rfapi_import_table
*arg_it
,
4630 int delete_active
, int delete_holddown
,
4631 uint32_t *pARcount
, uint32_t *pAHcount
,
4632 uint32_t *pHRcount
, uint32_t *pHHcount
)
4636 struct rfapi_import_table
*it
;
4637 uint32_t deleted_holddown_route_count
= 0;
4638 uint32_t deleted_active_route_count
= 0;
4639 uint32_t deleted_holddown_nve_count
= 0;
4640 uint32_t deleted_active_nve_count
= 0;
4641 struct skiplist
*uniq_holddown_nves
;
4642 struct skiplist
*uniq_active_nves
;
4646 bgp
= bgp_get_default(); /* assume 1 instance for now */
4647 /* If no bgp instantiated yet, no vnc prefixes exist */
4654 uniq_holddown_nves
=
4655 skiplist_new(0, rfapi_nve_addr_cmp
, delete_rem_pfx_na_free
);
4657 skiplist_new(0, rfapi_nve_addr_cmp
, delete_rem_pfx_na_free
);
4660 * Iterate over all import tables; do a filtered import
4661 * for the afi/safi combination
4670 vnc_zlog_debug_verbose(
4671 "%s: calling rfapiDeleteRemotePrefixesIt() on (IP) import %p",
4674 rfapiDeleteRemotePrefixesIt(
4675 bgp
, it
, un
, vn
, p
, delete_active
, delete_holddown
,
4676 &deleted_active_route_count
, &deleted_active_nve_count
,
4677 &deleted_holddown_route_count
,
4678 &deleted_holddown_nve_count
, uniq_active_nves
,
4679 uniq_holddown_nves
);
4688 * Now iterate over L2 import tables
4690 if (h
->import_mac
&& !(p
&& (p
->family
!= AF_ETHERNET
))) {
4692 void *cursor
= NULL
;
4696 rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4698 !rc
; rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4701 vnc_zlog_debug_verbose(
4702 "%s: calling rfapiDeleteRemotePrefixesIt() on import_mac %p",
4705 rfapiDeleteRemotePrefixesIt(
4706 bgp
, it
, un
, vn
, p
, delete_active
,
4707 delete_holddown
, &deleted_active_route_count
,
4708 &deleted_active_nve_count
,
4709 &deleted_holddown_route_count
,
4710 &deleted_holddown_nve_count
, uniq_active_nves
,
4711 uniq_holddown_nves
);
4716 * our custom element freeing function above counts as it deletes
4718 skiplist_free(uniq_holddown_nves
);
4719 skiplist_free(uniq_active_nves
);
4722 *pARcount
= deleted_active_route_count
;
4724 *pAHcount
= deleted_active_nve_count
;
4726 *pHRcount
= deleted_holddown_route_count
;
4728 *pHHcount
= deleted_holddown_nve_count
;
4733 /*------------------------------------------
4734 * rfapiCountRemoteRoutes
4736 * UI helper: count VRF routes from BGP side
4741 * pALRcount count of active local routes
4742 * pARRcount count of active remote routes
4743 * pHRcount count of holddown routes
4744 * pIRcount count of direct imported routes
4748 --------------------------------------------*/
4749 void rfapiCountAllItRoutes(int *pALRcount
, /* active local routes */
4750 int *pARRcount
, /* active remote routes */
4751 int *pHRcount
, /* holddown routes */
4752 int *pIRcount
) /* imported routes */
4756 struct rfapi_import_table
*it
;
4759 int total_active_local
= 0;
4760 int total_active_remote
= 0;
4761 int total_holddown
= 0;
4762 int total_imported
= 0;
4764 bgp
= bgp_get_default(); /* assume 1 instance for now */
4771 * Iterate over all import tables; do a filtered import
4772 * for the afi/safi combination
4775 for (it
= h
->imports
; it
; it
= it
->next
) {
4777 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
4779 total_active_local
+= it
->local_count
[afi
];
4780 total_active_remote
+= it
->remote_count
[afi
];
4781 total_holddown
+= it
->holddown_count
[afi
];
4782 total_imported
+= it
->imported_count
[afi
];
4789 if (h
->import_mac
) {
4791 rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4793 !rc
; rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4796 total_active_local
+= it
->local_count
[AFI_L2VPN
];
4797 total_active_remote
+= it
->remote_count
[AFI_L2VPN
];
4798 total_holddown
+= it
->holddown_count
[AFI_L2VPN
];
4799 total_imported
+= it
->imported_count
[AFI_L2VPN
];
4805 *pALRcount
= total_active_local
;
4808 *pARRcount
= total_active_remote
;
4811 *pHRcount
= total_holddown
;
4814 *pIRcount
= total_imported
;
4818 /*------------------------------------------
4819 * rfapiGetHolddownFromLifetime
4821 * calculate holddown value based on lifetime
4827 * Holddown value based on lifetime, holddown_factor,
4828 * and RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY
4830 --------------------------------------------*/
4831 /* hold down time maxes out at RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY */
4832 uint32_t rfapiGetHolddownFromLifetime(uint32_t lifetime
)
4837 bgp
= bgp_get_default();
4838 if (bgp
&& bgp
->rfapi_cfg
)
4839 factor
= bgp
->rfapi_cfg
->rfp_cfg
.holddown_factor
;
4841 factor
= RFAPI_RFP_CFG_DEFAULT_HOLDDOWN_FACTOR
;
4843 if (factor
< 100 || lifetime
< RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY
)
4844 lifetime
= lifetime
* factor
/ 100;
4845 if (lifetime
< RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY
)
4848 return RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY
;