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 #if DEBUG_ROUTE_COUNTERS
204 #define VNC_ITRCCK do {rfapiCheckRouteCount();} while (0)
210 * Validate reference count for a node in an import table
212 * Normally lockoffset is 0 for nodes in quiescent state. However,
213 * agg_unlock_node will delete the node if it is called when
214 * node->lock == 1, and we have to validate the refcount before
215 * the node is deleted. In this case, we specify lockoffset 1.
217 void rfapiCheckRefcount(struct agg_node
*rn
, safi_t safi
, int lockoffset
)
219 unsigned int count_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 #if 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 #if 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 #if 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 /* this isn't necessarily 0/0 */
667 par
= agg_route_table_top(original_vpn_node
);
670 * If we got the top node but it wasn't 0/0,
673 if (par
&& par
->p
.prefixlen
) {
674 agg_unlock_node(par
); /* maybe free */
679 ++parent_already_refcounted
;
684 * Create 0/0 node if it isn't there
687 struct prefix pfx_default
;
689 memset(&pfx_default
, 0, sizeof(pfx_default
));
690 pfx_default
.family
= original_vpn_node
->p
.family
;
692 /* creates default node if none exists */
693 par
= agg_node_get(agg_get_table(original_vpn_node
),
695 ++parent_already_refcounted
;
699 * 3. Move each of the monitors found at original_node to Node P.
700 * These are "Moved Monitors."
705 * Attach at end so that the list pointer we return points
706 * only to the moved routes
708 for (m
= RFAPI_MONITOR_VPN(par
), mlast
= NULL
; m
;
709 mlast
= m
, m
= m
->next
)
713 moved
= mlast
->next
= RFAPI_MONITOR_VPN(original_vpn_node
);
715 moved
= RFAPI_MONITOR_VPN_W_ALLOC(par
) =
716 RFAPI_MONITOR_VPN(original_vpn_node
);
718 if (RFAPI_MONITOR_VPN(
719 original_vpn_node
)) /* check agg, so not allocated */
720 RFAPI_MONITOR_VPN_W_ALLOC(original_vpn_node
) = NULL
;
723 * update the node pointers on the monitors
725 for (m
= moved
; m
; m
= m
->next
) {
730 RFAPI_CHECK_REFCOUNT(par
, SAFI_MPLS_VPN
,
731 parent_already_refcounted
- movecount
);
732 while (movecount
> parent_already_refcounted
) {
734 ++parent_already_refcounted
;
736 while (movecount
< parent_already_refcounted
) {
737 /* unlikely, but code defensively */
738 agg_unlock_node(par
);
739 --parent_already_refcounted
;
741 RFAPI_CHECK_REFCOUNT(original_vpn_node
, SAFI_MPLS_VPN
,
742 movecount
+ lockoffset
);
743 while (movecount
--) {
744 agg_unlock_node(original_vpn_node
);
747 #if DEBUG_MONITOR_MOVE_SHORTER
749 char buf
[PREFIX_STRLEN
];
751 prefix2str(&par
->p
, buf
, sizeof(buf
));
752 vnc_zlog_debug_verbose("%s: moved to node pfx=%s", __func__
,
762 * Implement MONITOR_MOVE_LONGER(new_node) from
763 * RFAPI-Import-Event-Handling.txt
765 static void rfapiMonitorMoveLonger(struct agg_node
*new_vpn_node
)
767 struct rfapi_monitor_vpn
*monitor
;
768 struct rfapi_monitor_vpn
*mlast
;
769 struct bgp_path_info
*bpi
;
770 struct agg_node
*par
;
772 RFAPI_CHECK_REFCOUNT(new_vpn_node
, SAFI_MPLS_VPN
, 0);
775 * Make sure we have at least one valid route at the new node
777 for (bpi
= new_vpn_node
->info
; bpi
; bpi
= bpi
->next
) {
779 if (!rfapiGetUnAddrOfVpnBi(bpi
, &pfx
))
784 vnc_zlog_debug_verbose(
785 "%s: no valid routes at node %p, so not attempting moves",
786 __func__
, new_vpn_node
);
791 * Find first parent node that has monitors
793 for (par
= agg_node_parent(new_vpn_node
); par
;
794 par
= agg_node_parent(par
)) {
795 if (RFAPI_MONITOR_VPN(par
))
800 vnc_zlog_debug_verbose(
801 "%s: no parent nodes with monitors, done", __func__
);
806 * Check each of these monitors to see of their longest-match
807 * is now the updated node. Move any such monitors to the more-
808 * specific updated node
810 for (mlast
= NULL
, monitor
= RFAPI_MONITOR_VPN(par
); monitor
;) {
813 * If new longest match for monitor prefix is the new
814 * route's prefix, move monitor to new route's prefix
816 if (prefix_match(&new_vpn_node
->p
, &monitor
->p
)) {
819 mlast
->next
= monitor
->next
;
821 RFAPI_MONITOR_VPN_W_ALLOC(par
) = monitor
->next
;
826 monitor
->next
= RFAPI_MONITOR_VPN(new_vpn_node
);
827 RFAPI_MONITOR_VPN_W_ALLOC(new_vpn_node
) = monitor
;
828 monitor
->node
= new_vpn_node
;
830 agg_lock_node(new_vpn_node
); /* incr refcount */
832 monitor
= mlast
? mlast
->next
: RFAPI_MONITOR_VPN(par
);
834 RFAPI_CHECK_REFCOUNT(par
, SAFI_MPLS_VPN
, 1);
835 /* decr refcount after we're done with par as this might
837 agg_unlock_node(par
);
842 monitor
= monitor
->next
;
845 RFAPI_CHECK_REFCOUNT(new_vpn_node
, SAFI_MPLS_VPN
, 0);
849 static void rfapiBgpInfoChainFree(struct bgp_path_info
*bpi
)
851 struct bgp_path_info
*next
;
856 * If there is a timer waiting to delete this bpi, cancel
857 * the timer and delete immediately
859 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
)
860 && bpi
->extra
->vnc
.import
.timer
) {
863 (struct thread
*)bpi
->extra
->vnc
.import
.timer
;
864 struct rfapi_withdraw
*wcb
= t
->arg
;
866 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
872 rfapiBgpInfoFree(bpi
);
877 static void rfapiImportTableFlush(struct rfapi_import_table
*it
)
884 ecommunity_free(&it
->rt_import_list
);
885 it
->rt_import_list
= NULL
;
887 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
891 for (rn
= agg_route_top(it
->imported_vpn
[afi
]); rn
;
892 rn
= agg_route_next(rn
)) {
894 * Each route_node has:
895 * aggregate: points to rfapi_it_extra with monitor
897 * info: points to chain of bgp_path_info
899 /* free bgp_path_info and its children */
900 rfapiBgpInfoChainFree(rn
->info
);
903 rfapiMonitorExtraFlush(SAFI_MPLS_VPN
, rn
);
906 for (rn
= agg_route_top(it
->imported_encap
[afi
]); rn
;
907 rn
= agg_route_next(rn
)) {
908 /* free bgp_path_info and its children */
909 rfapiBgpInfoChainFree(rn
->info
);
912 rfapiMonitorExtraFlush(SAFI_ENCAP
, rn
);
915 agg_table_finish(it
->imported_vpn
[afi
]);
916 agg_table_finish(it
->imported_encap
[afi
]);
918 if (it
->monitor_exterior_orphans
) {
919 skiplist_free(it
->monitor_exterior_orphans
);
923 void rfapiImportTableRefDelByIt(struct bgp
*bgp
,
924 struct rfapi_import_table
*it_target
)
927 struct rfapi_import_table
*it
;
928 struct rfapi_import_table
*prev
= NULL
;
935 for (it
= h
->imports
; it
; prev
= it
, it
= it
->next
) {
941 assert(it
->refcount
);
947 prev
->next
= it
->next
;
949 h
->imports
= it
->next
;
951 rfapiImportTableFlush(it
);
952 XFREE(MTYPE_RFAPI_IMPORTTABLE
, it
);
956 #if RFAPI_REQUIRE_ENCAP_BEEC
958 * Look for magic BGP Encapsulation Extended Community value
959 * Format in RFC 5512 Sect. 4.5
961 static int rfapiEcommunitiesMatchBeec(struct ecommunity
*ecom
,
962 bgp_encap_types type
)
969 for (i
= 0; i
< (ecom
->size
* ECOMMUNITY_SIZE
); i
+= ECOMMUNITY_SIZE
) {
975 if (ep
[0] == ECOMMUNITY_ENCODE_OPAQUE
976 && ep
[1] == ECOMMUNITY_OPAQUE_SUBTYPE_ENCAP
977 && ep
[6] == ((type
&& 0xff00) >> 8)
978 && ep
[7] == (type
& 0xff)) {
987 int rfapiEcommunitiesIntersect(struct ecommunity
*e1
, struct ecommunity
*e2
)
996 s1
= ecommunity_ecom2str(e1
, ECOMMUNITY_FORMAT_DISPLAY
, 0);
997 s2
= ecommunity_ecom2str(e2
, ECOMMUNITY_FORMAT_DISPLAY
, 0);
998 vnc_zlog_debug_verbose("%s: e1[%s], e2[%s]", __func__
, s1
, s2
);
999 XFREE(MTYPE_ECOMMUNITY_STR
, s1
);
1000 XFREE(MTYPE_ECOMMUNITY_STR
, s2
);
1003 for (i
= 0; i
< e1
->size
; ++i
) {
1004 for (j
= 0; j
< e2
->size
; ++j
) {
1005 if (!memcmp(e1
->val
+ (i
* ECOMMUNITY_SIZE
),
1006 e2
->val
+ (j
* ECOMMUNITY_SIZE
),
1016 int rfapiEcommunityGetLNI(struct ecommunity
*ecom
, uint32_t *lni
)
1020 for (i
= 0; i
< ecom
->size
; ++i
) {
1021 uint8_t *p
= ecom
->val
+ (i
* ECOMMUNITY_SIZE
);
1023 if ((*(p
+ 0) == 0x00) && (*(p
+ 1) == 0x02)) {
1025 *lni
= (*(p
+ 5) << 16) | (*(p
+ 6) << 8)
1034 int rfapiEcommunityGetEthernetTag(struct ecommunity
*ecom
, uint16_t *tag_id
)
1036 struct bgp
*bgp
= bgp_get_default();
1037 *tag_id
= 0; /* default to untagged */
1040 for (i
= 0; i
< ecom
->size
; ++i
) {
1043 uint8_t *p
= ecom
->val
+ (i
* ECOMMUNITY_SIZE
);
1045 /* High-order octet of type. */
1048 if (*p
++ == ECOMMUNITY_ROUTE_TARGET
) {
1049 if (encode
== ECOMMUNITY_ENCODE_AS4
) {
1050 p
= ptr_get_be32(p
, &as
);
1051 } else if (encode
== ECOMMUNITY_ENCODE_AS
) {
1054 p
+= 2; /* skip next two, tag/vid
1055 always in lowest bytes */
1057 if (as
== bgp
->as
) {
1058 *tag_id
= *p
++ << 8;
1068 static int rfapiVpnBiNhEqualsPt(struct bgp_path_info
*bpi
,
1069 struct rfapi_ip_addr
*hpt
)
1076 family
= BGP_MP_NEXTHOP_FAMILY(bpi
->attr
->mp_nexthop_len
);
1078 if (hpt
->addr_family
!= family
)
1083 if (bpi
->attr
->mp_nexthop_global_in
.s_addr
1084 != hpt
->addr
.v4
.s_addr
)
1089 if (IPV6_ADDR_CMP(&bpi
->attr
->mp_nexthop_global
, &hpt
->addr
.v6
))
1103 * Compare 2 VPN BIs. Return true if they have the same VN and UN addresses
1105 static int rfapiVpnBiSamePtUn(struct bgp_path_info
*bpi1
,
1106 struct bgp_path_info
*bpi2
)
1108 struct prefix pfx_un1
;
1109 struct prefix pfx_un2
;
1115 * VN address comparisons
1118 if (BGP_MP_NEXTHOP_FAMILY(bpi1
->attr
->mp_nexthop_len
)
1119 != BGP_MP_NEXTHOP_FAMILY(bpi2
->attr
->mp_nexthop_len
)) {
1123 switch (BGP_MP_NEXTHOP_FAMILY(bpi1
->attr
->mp_nexthop_len
)) {
1125 if (bpi1
->attr
->mp_nexthop_global_in
.s_addr
1126 != bpi2
->attr
->mp_nexthop_global_in
.s_addr
)
1131 if (IPV6_ADDR_CMP(&bpi1
->attr
->mp_nexthop_global
,
1132 &bpi2
->attr
->mp_nexthop_global
))
1141 memset(&pfx_un1
, 0, sizeof(pfx_un1
));
1142 memset(&pfx_un2
, 0, sizeof(pfx_un2
));
1145 * UN address comparisons
1147 if (rfapiGetVncTunnelUnAddr(bpi1
->attr
, &pfx_un1
)) {
1149 pfx_un1
.family
= bpi1
->extra
->vnc
.import
.un_family
;
1150 switch (bpi1
->extra
->vnc
.import
.un_family
) {
1153 bpi1
->extra
->vnc
.import
.un
.addr4
;
1157 bpi1
->extra
->vnc
.import
.un
.addr6
;
1166 if (rfapiGetVncTunnelUnAddr(bpi2
->attr
, &pfx_un2
)) {
1168 pfx_un2
.family
= bpi2
->extra
->vnc
.import
.un_family
;
1169 switch (bpi2
->extra
->vnc
.import
.un_family
) {
1172 bpi2
->extra
->vnc
.import
.un
.addr4
;
1176 bpi2
->extra
->vnc
.import
.un
.addr6
;
1185 if (pfx_un1
.family
== 0 || pfx_un2
.family
== 0)
1188 if (pfx_un1
.family
!= pfx_un2
.family
)
1191 switch (pfx_un1
.family
) {
1193 if (!IPV4_ADDR_SAME(&pfx_un1
.u
.prefix4
, &pfx_un2
.u
.prefix4
))
1197 if (!IPV6_ADDR_SAME(&pfx_un1
.u
.prefix6
, &pfx_un2
.u
.prefix6
))
1206 uint8_t rfapiRfpCost(struct attr
*attr
)
1208 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)) {
1209 if (attr
->local_pref
> 255) {
1212 return 255 - attr
->local_pref
;
1218 /*------------------------------------------
1221 * Find Layer 2 options in an option chain
1227 * l2o layer 2 options extracted
1231 * 1 no options found
1233 --------------------------------------------*/
1234 int rfapi_extract_l2o(
1235 struct bgp_tea_options
*pHop
, /* chain of options */
1236 struct rfapi_l2address_option
*l2o
) /* return extracted value */
1238 struct bgp_tea_options
*p
;
1240 for (p
= pHop
; p
; p
= p
->next
) {
1241 if ((p
->type
== RFAPI_VN_OPTION_TYPE_L2ADDR
)
1242 && (p
->length
>= 8)) {
1246 memcpy(&l2o
->macaddr
, v
, 6);
1248 l2o
->label
= ((v
[6] << 12) & 0xff000)
1249 + ((v
[7] << 4) & 0xff0)
1250 + ((v
[8] >> 4) & 0xf);
1252 l2o
->local_nve_id
= (uint8_t)v
[10];
1254 l2o
->logical_net_id
=
1255 (v
[11] << 16) + (v
[12] << 8) + (v
[13] << 0);
1263 static struct rfapi_next_hop_entry
*
1264 rfapiRouteInfo2NextHopEntry(struct rfapi_ip_prefix
*rprefix
,
1265 struct bgp_path_info
*bpi
, /* route to encode */
1266 uint32_t lifetime
, /* use this in nhe */
1267 struct agg_node
*rn
) /* req for L2 eth addr */
1269 struct rfapi_next_hop_entry
*new;
1270 int have_vnc_tunnel_un
= 0;
1272 #if DEBUG_ENCAP_MONITOR
1273 vnc_zlog_debug_verbose("%s: entry, bpi %p, rn %p", __func__
, bpi
, rn
);
1276 new = XCALLOC(MTYPE_RFAPI_NEXTHOP
, sizeof(struct rfapi_next_hop_entry
));
1279 new->prefix
= *rprefix
;
1282 && decode_rd_type(bpi
->extra
->vnc
.import
.rd
.val
)
1283 == RD_TYPE_VNC_ETH
) {
1286 struct rfapi_vn_option
*vo
;
1288 vo
= XCALLOC(MTYPE_RFAPI_VN_OPTION
,
1289 sizeof(struct rfapi_vn_option
));
1292 vo
->type
= RFAPI_VN_OPTION_TYPE_L2ADDR
;
1294 memcpy(&vo
->v
.l2addr
.macaddr
, &rn
->p
.u
.prefix_eth
.octet
,
1296 /* only low 3 bytes of this are significant */
1297 (void)rfapiEcommunityGetLNI(bpi
->attr
->ecommunity
,
1298 &vo
->v
.l2addr
.logical_net_id
);
1299 (void)rfapiEcommunityGetEthernetTag(bpi
->attr
->ecommunity
,
1300 &vo
->v
.l2addr
.tag_id
);
1302 /* local_nve_id comes from lower byte of RD type */
1303 vo
->v
.l2addr
.local_nve_id
= bpi
->extra
->vnc
.import
.rd
.val
[1];
1305 /* label comes from MP_REACH_NLRI label */
1306 vo
->v
.l2addr
.label
= decode_label(&bpi
->extra
->label
[0]);
1308 new->vn_options
= vo
;
1311 * If there is an auxiliary prefix (i.e., host IP address),
1312 * use it as the nexthop prefix instead of the query prefix
1314 if (bpi
->extra
->vnc
.import
.aux_prefix
.family
) {
1315 rfapiQprefix2Rprefix(&bpi
->extra
->vnc
.import
.aux_prefix
,
1320 bgp_encap_types tun_type
= BGP_ENCAP_TYPE_MPLS
; /*Default*/
1321 new->prefix
.cost
= rfapiRfpCost(bpi
->attr
);
1323 struct bgp_attr_encap_subtlv
*pEncap
;
1325 switch (BGP_MP_NEXTHOP_FAMILY(bpi
->attr
->mp_nexthop_len
)) {
1327 new->vn_address
.addr_family
= AF_INET
;
1328 new->vn_address
.addr
.v4
= bpi
->attr
->mp_nexthop_global_in
;
1332 new->vn_address
.addr_family
= AF_INET6
;
1333 new->vn_address
.addr
.v6
= bpi
->attr
->mp_nexthop_global
;
1337 zlog_warn("%s: invalid vpn nexthop length: %d", __func__
,
1338 bpi
->attr
->mp_nexthop_len
);
1339 rfapi_free_next_hop_list(new);
1343 for (pEncap
= bpi
->attr
->vnc_subtlvs
; pEncap
; pEncap
= pEncap
->next
) {
1344 switch (pEncap
->type
) {
1345 case BGP_VNC_SUBTLV_TYPE_LIFETIME
:
1346 /* use configured lifetime, not attr lifetime */
1350 zlog_warn("%s: unknown VNC option type %d", __func__
,
1357 bgp_attr_extcom_tunnel_type(bpi
->attr
, &tun_type
);
1358 if (tun_type
== BGP_ENCAP_TYPE_MPLS
) {
1360 /* MPLS carries UN address in next hop */
1361 rfapiNexthop2Prefix(bpi
->attr
, &p
);
1362 if (p
.family
!= 0) {
1363 rfapiQprefix2Raddr(&p
, &new->un_address
);
1364 have_vnc_tunnel_un
= 1;
1368 for (pEncap
= bpi
->attr
->encap_subtlvs
; pEncap
; pEncap
= pEncap
->next
) {
1369 switch (pEncap
->type
) {
1370 case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT
:
1372 * Overrides ENCAP UN address, if any
1374 switch (pEncap
->length
) {
1377 new->un_address
.addr_family
= AF_INET
;
1378 memcpy(&new->un_address
.addr
.v4
, pEncap
->value
,
1380 have_vnc_tunnel_un
= 1;
1384 new->un_address
.addr_family
= AF_INET6
;
1385 memcpy(&new->un_address
.addr
.v6
, pEncap
->value
,
1387 have_vnc_tunnel_un
= 1;
1392 "%s: invalid tunnel subtlv UN addr length (%d) for bpi %p",
1393 __func__
, pEncap
->length
, bpi
);
1398 zlog_warn("%s: unknown Encap Attribute option type %d",
1399 __func__
, pEncap
->type
);
1404 new->un_options
= rfapi_encap_tlv_to_un_option(bpi
->attr
);
1406 #if DEBUG_ENCAP_MONITOR
1407 vnc_zlog_debug_verbose("%s: line %d: have_vnc_tunnel_un=%d", __func__
,
1408 __LINE__
, have_vnc_tunnel_un
);
1411 if (!have_vnc_tunnel_un
&& bpi
->extra
) {
1413 * use cached UN address from ENCAP route
1415 new->un_address
.addr_family
= bpi
->extra
->vnc
.import
.un_family
;
1416 switch (new->un_address
.addr_family
) {
1418 new->un_address
.addr
.v4
=
1419 bpi
->extra
->vnc
.import
.un
.addr4
;
1422 new->un_address
.addr
.v6
=
1423 bpi
->extra
->vnc
.import
.un
.addr6
;
1426 zlog_warn("%s: invalid UN addr family (%d) for bpi %p",
1427 __func__
, new->un_address
.addr_family
, bpi
);
1428 rfapi_free_next_hop_list(new);
1434 new->lifetime
= lifetime
;
1438 int rfapiHasNonRemovedRoutes(struct agg_node
*rn
)
1440 struct bgp_path_info
*bpi
;
1442 for (bpi
= rn
->info
; bpi
; bpi
= bpi
->next
) {
1445 if (!CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
)
1446 && (bpi
->extra
&& !rfapiGetUnAddrOfVpnBi(bpi
, &pfx
))) {
1458 void rfapiDumpNode(struct agg_node
*rn
)
1460 struct bgp_path_info
*bpi
;
1462 vnc_zlog_debug_verbose("%s: rn=%p", __func__
, rn
);
1463 for (bpi
= rn
->info
; bpi
; bpi
= bpi
->next
) {
1465 int ctrc
= rfapiGetUnAddrOfVpnBi(bpi
, &pfx
);
1468 if (!CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
)
1469 && (bpi
->extra
&& !ctrc
)) {
1476 vnc_zlog_debug_verbose(
1477 " bpi=%p, nr=%d, flags=0x%x, extra=%p, ctrc=%d", bpi
,
1478 nr
, bpi
->flags
, bpi
->extra
, ctrc
);
1483 static int rfapiNhlAddNodeRoutes(
1484 struct agg_node
*rn
, /* in */
1485 struct rfapi_ip_prefix
*rprefix
, /* in */
1486 uint32_t lifetime
, /* in */
1487 int removed
, /* in */
1488 struct rfapi_next_hop_entry
**head
, /* in/out */
1489 struct rfapi_next_hop_entry
**tail
, /* in/out */
1490 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1491 struct agg_node
*rfd_rib_node
, /* preload this NVE rib node */
1492 struct prefix
*pfx_target_original
) /* query target */
1494 struct bgp_path_info
*bpi
;
1495 struct rfapi_next_hop_entry
*new;
1496 struct prefix pfx_un
;
1497 struct skiplist
*seen_nexthops
;
1499 int is_l2
= (rn
->p
.family
== AF_ETHERNET
);
1502 struct agg_table
*atable
= agg_get_table(rfd_rib_node
);
1503 struct rfapi_descriptor
*rfd
;
1506 rfd
= agg_get_table_info(atable
);
1508 if (rfapiRibFTDFilterRecentPrefix(rfd
, rn
,
1509 pfx_target_original
))
1515 skiplist_new(0, vnc_prefix_cmp
, prefix_free_lists
);
1517 for (bpi
= rn
->info
; bpi
; bpi
= bpi
->next
) {
1519 struct prefix pfx_vn
;
1520 struct prefix
*newpfx
;
1522 if (removed
&& !CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
)) {
1523 #if DEBUG_RETURNED_NHL
1524 vnc_zlog_debug_verbose(
1525 "%s: want holddown, this route not holddown, skip",
1530 if (!removed
&& CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
)) {
1539 * Check for excluded VN address
1541 if (rfapiVpnBiNhEqualsPt(bpi
, exclude_vnaddr
))
1545 * Check for VN address (nexthop) copied already
1548 /* L2 routes: semantic nexthop in aux_prefix; VN addr
1550 pfx_vn
= bpi
->extra
->vnc
.import
.aux_prefix
;
1552 rfapiNexthop2Prefix(bpi
->attr
, &pfx_vn
);
1554 if (!skiplist_search(seen_nexthops
, &pfx_vn
, NULL
)) {
1555 #if DEBUG_RETURNED_NHL
1556 char buf
[PREFIX_STRLEN
];
1558 prefix2str(&pfx_vn
, buf
, sizeof(buf
));
1559 vnc_zlog_debug_verbose(
1560 "%s: already put VN/nexthop %s, skip", __func__
,
1566 if (rfapiGetUnAddrOfVpnBi(bpi
, &pfx_un
)) {
1567 #if DEBUG_ENCAP_MONITOR
1568 vnc_zlog_debug_verbose(
1569 "%s: failed to get UN address of this VPN bpi",
1575 newpfx
= prefix_new();
1577 skiplist_insert(seen_nexthops
, newpfx
, newpfx
);
1579 new = rfapiRouteInfo2NextHopEntry(rprefix
, bpi
, lifetime
, rn
);
1581 if (rfapiRibPreloadBi(rfd_rib_node
, &pfx_vn
, &pfx_un
,
1583 /* duplicate filtered by RIB */
1584 rfapi_free_next_hop_list(new);
1591 (*tail
)->next
= new;
1600 skiplist_free(seen_nexthops
);
1609 * omit_node is meant for the situation where we are adding a subtree
1610 * of a parent of some original requested node. The response already
1611 * contains the original requested node, and we don't want to duplicate
1612 * its routes in the list, so we skip it if the right or left node
1613 * matches (of course, we still travel down its child subtrees).
1615 static int rfapiNhlAddSubtree(
1616 struct agg_node
*rn
, /* in */
1617 uint32_t lifetime
, /* in */
1618 struct rfapi_next_hop_entry
**head
, /* in/out */
1619 struct rfapi_next_hop_entry
**tail
, /* in/out */
1620 struct agg_node
*omit_node
, /* in */
1621 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1622 struct agg_table
*rfd_rib_table
, /* preload here */
1623 struct prefix
*pfx_target_original
) /* query target */
1625 struct rfapi_ip_prefix rprefix
;
1628 /* FIXME: need to find a better way here to work without sticking our
1629 * hands in node->link */
1630 if (agg_node_left(rn
) && agg_node_left(rn
) != omit_node
) {
1631 if (agg_node_left(rn
)->info
) {
1633 struct agg_node
*rib_rn
= NULL
;
1635 rfapiQprefix2Rprefix(&agg_node_left(rn
)->p
, &rprefix
);
1636 if (rfd_rib_table
) {
1637 rib_rn
= agg_node_get(rfd_rib_table
,
1638 &agg_node_left(rn
)->p
);
1641 count
= rfapiNhlAddNodeRoutes(
1642 agg_node_left(rn
), &rprefix
, lifetime
, 0, head
,
1643 tail
, exclude_vnaddr
, rib_rn
,
1644 pfx_target_original
);
1646 count
= rfapiNhlAddNodeRoutes(
1647 agg_node_left(rn
), &rprefix
, lifetime
,
1648 1, head
, tail
, exclude_vnaddr
, rib_rn
,
1649 pfx_target_original
);
1653 agg_unlock_node(rib_rn
);
1657 if (agg_node_right(rn
) && agg_node_right(rn
) != omit_node
) {
1658 if (agg_node_right(rn
)->info
) {
1660 struct agg_node
*rib_rn
= NULL
;
1662 rfapiQprefix2Rprefix(&agg_node_right(rn
)->p
, &rprefix
);
1663 if (rfd_rib_table
) {
1664 rib_rn
= agg_node_get(rfd_rib_table
,
1665 &agg_node_right(rn
)->p
);
1667 count
= rfapiNhlAddNodeRoutes(
1668 agg_node_right(rn
), &rprefix
, lifetime
, 0, head
,
1669 tail
, exclude_vnaddr
, rib_rn
,
1670 pfx_target_original
);
1672 count
= rfapiNhlAddNodeRoutes(
1673 agg_node_right(rn
), &rprefix
, lifetime
,
1674 1, head
, tail
, exclude_vnaddr
, rib_rn
,
1675 pfx_target_original
);
1679 agg_unlock_node(rib_rn
);
1683 if (agg_node_left(rn
)) {
1684 rcount
+= rfapiNhlAddSubtree(
1685 agg_node_left(rn
), lifetime
, head
, tail
, omit_node
,
1686 exclude_vnaddr
, rfd_rib_table
, pfx_target_original
);
1688 if (agg_node_right(rn
)) {
1689 rcount
+= rfapiNhlAddSubtree(
1690 agg_node_right(rn
), lifetime
, head
, tail
, omit_node
,
1691 exclude_vnaddr
, rfd_rib_table
, pfx_target_original
);
1698 * Implementation of ROUTE_LIST(node) from RFAPI-Import-Event-Handling.txt
1700 * Construct an rfapi nexthop list based on the routes attached to
1701 * the specified node.
1703 * If there are any routes that do NOT have BGP_PATH_REMOVED set,
1704 * return those only. If there are ONLY routes with BGP_PATH_REMOVED,
1705 * then return those, and also include all the non-removed routes from the
1706 * next less-specific node (i.e., this node's parent) at the end.
1708 struct rfapi_next_hop_entry
*rfapiRouteNode2NextHopList(
1709 struct agg_node
*rn
, uint32_t lifetime
, /* put into nexthop entries */
1710 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1711 struct agg_table
*rfd_rib_table
, /* preload here */
1712 struct prefix
*pfx_target_original
) /* query target */
1714 struct rfapi_ip_prefix rprefix
;
1715 struct rfapi_next_hop_entry
*answer
= NULL
;
1716 struct rfapi_next_hop_entry
*last
= NULL
;
1717 struct agg_node
*parent
;
1719 struct agg_node
*rib_rn
;
1721 #if DEBUG_RETURNED_NHL
1723 char buf
[PREFIX_STRLEN
];
1725 prefix2str(&rn
->p
, buf
, sizeof(buf
));
1726 vnc_zlog_debug_verbose("%s: called with node pfx=%s", __func__
,
1729 rfapiDebugBacktrace();
1732 rfapiQprefix2Rprefix(&rn
->p
, &rprefix
);
1734 rib_rn
= rfd_rib_table
? agg_node_get(rfd_rib_table
, &rn
->p
) : NULL
;
1737 * Add non-withdrawn routes at this node
1739 count
= rfapiNhlAddNodeRoutes(rn
, &rprefix
, lifetime
, 0, &answer
, &last
,
1740 exclude_vnaddr
, rib_rn
,
1741 pfx_target_original
);
1744 * If the list has at least one entry, it's finished
1747 count
+= rfapiNhlAddSubtree(rn
, lifetime
, &answer
, &last
, NULL
,
1748 exclude_vnaddr
, rfd_rib_table
,
1749 pfx_target_original
);
1750 vnc_zlog_debug_verbose("%s: %d nexthops, answer=%p", __func__
,
1752 #if DEBUG_RETURNED_NHL
1753 rfapiPrintNhl(NULL
, answer
);
1756 agg_unlock_node(rib_rn
);
1761 * Add withdrawn routes at this node
1763 count
= rfapiNhlAddNodeRoutes(rn
, &rprefix
, lifetime
, 1, &answer
, &last
,
1764 exclude_vnaddr
, rib_rn
,
1765 pfx_target_original
);
1767 agg_unlock_node(rib_rn
);
1769 // rfapiPrintNhl(NULL, answer);
1772 * walk up the tree until we find a node with non-deleted
1773 * routes, then add them
1775 for (parent
= agg_node_parent(rn
); parent
;
1776 parent
= agg_node_parent(parent
)) {
1777 if (rfapiHasNonRemovedRoutes(parent
)) {
1783 * Add non-withdrawn routes from less-specific prefix
1786 rib_rn
= rfd_rib_table
? agg_node_get(rfd_rib_table
, &parent
->p
)
1788 rfapiQprefix2Rprefix(&parent
->p
, &rprefix
);
1789 count
+= rfapiNhlAddNodeRoutes(parent
, &rprefix
, lifetime
, 0,
1790 &answer
, &last
, exclude_vnaddr
,
1791 rib_rn
, pfx_target_original
);
1792 count
+= rfapiNhlAddSubtree(parent
, lifetime
, &answer
, &last
,
1793 rn
, exclude_vnaddr
, rfd_rib_table
,
1794 pfx_target_original
);
1796 agg_unlock_node(rib_rn
);
1799 * There is no parent with non-removed routes. Still need to
1800 * add subtree of original node if it contributed routes to the
1804 count
+= rfapiNhlAddSubtree(rn
, lifetime
, &answer
,
1805 &last
, rn
, exclude_vnaddr
,
1807 pfx_target_original
);
1810 vnc_zlog_debug_verbose("%s: %d nexthops, answer=%p", __func__
, count
,
1812 #if DEBUG_RETURNED_NHL
1813 rfapiPrintNhl(NULL
, answer
);
1819 * Construct nexthop list of all routes in table
1821 struct rfapi_next_hop_entry
*rfapiRouteTable2NextHopList(
1822 struct agg_table
*rt
, uint32_t lifetime
, /* put into nexthop entries */
1823 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1824 struct agg_table
*rfd_rib_table
, /* preload this NVE rib table */
1825 struct prefix
*pfx_target_original
) /* query target */
1827 struct agg_node
*rn
;
1828 struct rfapi_next_hop_entry
*biglist
= NULL
;
1829 struct rfapi_next_hop_entry
*nhl
;
1830 struct rfapi_next_hop_entry
*tail
= NULL
;
1833 for (rn
= agg_route_top(rt
); rn
; rn
= agg_route_next(rn
)) {
1835 nhl
= rfapiRouteNode2NextHopList(rn
, lifetime
, exclude_vnaddr
,
1837 pfx_target_original
);
1839 tail
= biglist
= nhl
;
1846 while (tail
->next
) {
1853 vnc_zlog_debug_verbose("%s: returning %d routes", __func__
, count
);
1857 struct rfapi_next_hop_entry
*rfapiEthRouteNode2NextHopList(
1858 struct agg_node
*rn
, struct rfapi_ip_prefix
*rprefix
,
1859 uint32_t lifetime
, /* put into nexthop entries */
1860 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1861 struct agg_table
*rfd_rib_table
, /* preload NVE rib table */
1862 struct prefix
*pfx_target_original
) /* query target */
1865 struct rfapi_next_hop_entry
*answer
= NULL
;
1866 struct rfapi_next_hop_entry
*last
= NULL
;
1867 struct agg_node
*rib_rn
;
1869 rib_rn
= rfd_rib_table
? agg_node_get(rfd_rib_table
, &rn
->p
) : NULL
;
1871 count
= rfapiNhlAddNodeRoutes(rn
, rprefix
, lifetime
, 0, &answer
, &last
,
1872 NULL
, rib_rn
, pfx_target_original
);
1874 #if DEBUG_ENCAP_MONITOR
1875 vnc_zlog_debug_verbose("%s: node %p: %d non-holddown routes", __func__
,
1880 count
= rfapiNhlAddNodeRoutes(rn
, rprefix
, lifetime
, 1, &answer
,
1881 &last
, exclude_vnaddr
, rib_rn
,
1882 pfx_target_original
);
1883 vnc_zlog_debug_verbose("%s: node %p: %d holddown routes",
1884 __func__
, rn
, count
);
1888 agg_unlock_node(rib_rn
);
1890 #if DEBUG_RETURNED_NHL
1891 rfapiPrintNhl(NULL
, answer
);
1899 * Construct nexthop list of all routes in table
1901 struct rfapi_next_hop_entry
*rfapiEthRouteTable2NextHopList(
1902 uint32_t logical_net_id
, struct rfapi_ip_prefix
*rprefix
,
1903 uint32_t lifetime
, /* put into nexthop entries */
1904 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1905 struct agg_table
*rfd_rib_table
, /* preload NVE rib node */
1906 struct prefix
*pfx_target_original
) /* query target */
1908 struct rfapi_import_table
*it
;
1909 struct bgp
*bgp
= bgp_get_default();
1910 struct agg_table
*rt
;
1911 struct agg_node
*rn
;
1912 struct rfapi_next_hop_entry
*biglist
= NULL
;
1913 struct rfapi_next_hop_entry
*nhl
;
1914 struct rfapi_next_hop_entry
*tail
= NULL
;
1918 it
= rfapiMacImportTableGet(bgp
, logical_net_id
);
1919 rt
= it
->imported_vpn
[AFI_L2VPN
];
1921 for (rn
= agg_route_top(rt
); rn
; rn
= agg_route_next(rn
)) {
1923 nhl
= rfapiEthRouteNode2NextHopList(
1924 rn
, rprefix
, lifetime
, exclude_vnaddr
, rfd_rib_table
,
1925 pfx_target_original
);
1927 tail
= biglist
= nhl
;
1934 while (tail
->next
) {
1941 vnc_zlog_debug_verbose("%s: returning %d routes", __func__
, count
);
1946 * Insert a new bpi to the imported route table node,
1947 * keeping the list of BPIs sorted best route first
1949 static void rfapiBgpInfoAttachSorted(struct agg_node
*rn
,
1950 struct bgp_path_info
*info_new
, afi_t afi
,
1954 struct bgp_path_info
*prev
;
1955 struct bgp_path_info
*next
;
1956 char pfx_buf
[PREFIX2STR_BUFFER
];
1959 bgp
= bgp_get_default(); /* assume 1 instance for now */
1961 if (VNC_DEBUG(IMPORT_BI_ATTACH
)) {
1962 vnc_zlog_debug_verbose("%s: info_new->peer=%p", __func__
,
1964 vnc_zlog_debug_verbose("%s: info_new->peer->su_remote=%p",
1965 __func__
, info_new
->peer
->su_remote
);
1968 for (prev
= NULL
, next
= rn
->info
; next
;
1969 prev
= next
, next
= next
->next
) {
1970 enum bgp_path_selection_reason reason
;
1973 || (!CHECK_FLAG(info_new
->flags
, BGP_PATH_REMOVED
)
1974 && CHECK_FLAG(next
->flags
, BGP_PATH_REMOVED
))
1975 || bgp_path_info_cmp_compatible(bgp
, info_new
, next
,
1978 == -1) { /* -1 if 1st is better */
1982 vnc_zlog_debug_verbose("%s: prev=%p, next=%p", __func__
, prev
, next
);
1984 prev
->next
= info_new
;
1986 rn
->info
= info_new
;
1988 info_new
->prev
= prev
;
1989 info_new
->next
= next
;
1991 next
->prev
= info_new
;
1992 bgp_attr_intern(info_new
->attr
);
1995 static void rfapiBgpInfoDetach(struct agg_node
*rn
, struct bgp_path_info
*bpi
)
1998 * Remove the route (doubly-linked)
2000 // bgp_attr_unintern (&bpi->attr);
2002 bpi
->next
->prev
= bpi
->prev
;
2004 bpi
->prev
->next
= bpi
->next
;
2006 rn
->info
= bpi
->next
;
2010 * For L3-indexed import tables
2012 static int rfapi_bi_peer_rd_cmp(void *b1
, void *b2
)
2014 struct bgp_path_info
*bpi1
= b1
;
2015 struct bgp_path_info
*bpi2
= b2
;
2020 if (bpi1
->peer
< bpi2
->peer
)
2022 if (bpi1
->peer
> bpi2
->peer
)
2028 return vnc_prefix_cmp((struct prefix
*)&bpi1
->extra
->vnc
.import
.rd
,
2029 (struct prefix
*)&bpi2
->extra
->vnc
.import
.rd
);
2033 * For L2-indexed import tables
2034 * The BPIs in these tables should ALWAYS have an aux_prefix set because
2035 * they arrive via IPv4 or IPv6 advertisements.
2037 static int rfapi_bi_peer_rd_aux_cmp(void *b1
, void *b2
)
2039 struct bgp_path_info
*bpi1
= b1
;
2040 struct bgp_path_info
*bpi2
= b2
;
2046 if (bpi1
->peer
< bpi2
->peer
)
2048 if (bpi1
->peer
> bpi2
->peer
)
2054 rc
= vnc_prefix_cmp((struct prefix
*)&bpi1
->extra
->vnc
.import
.rd
,
2055 (struct prefix
*)&bpi2
->extra
->vnc
.import
.rd
);
2061 * L2 import tables can have multiple entries with the
2062 * same MAC address, same RD, but different L3 addresses.
2064 * Use presence of aux_prefix with AF=ethernet and prefixlen=1
2065 * as magic value to signify explicit wildcarding of the aux_prefix.
2066 * This magic value will not appear in bona fide bpi entries in
2067 * the import table, but is allowed in the "fake" bpi used to
2068 * probe the table when searching. (We have to test both b1 and b2
2069 * because there is no guarantee of the order the test key and
2070 * the real key will be passed)
2072 if ((bpi1
->extra
->vnc
.import
.aux_prefix
.family
== AF_ETHERNET
2073 && (bpi1
->extra
->vnc
.import
.aux_prefix
.prefixlen
== 1))
2074 || (bpi2
->extra
->vnc
.import
.aux_prefix
.family
== AF_ETHERNET
2075 && (bpi2
->extra
->vnc
.import
.aux_prefix
.prefixlen
== 1))) {
2078 * wildcard aux address specified
2083 return vnc_prefix_cmp(&bpi1
->extra
->vnc
.import
.aux_prefix
,
2084 &bpi2
->extra
->vnc
.import
.aux_prefix
);
2089 * Index on RD and Peer
2091 static void rfapiItBiIndexAdd(struct agg_node
*rn
, /* Import table VPN node */
2092 struct bgp_path_info
*bpi
) /* new BPI */
2094 struct skiplist
*sl
;
2101 char buf
[RD_ADDRSTRLEN
];
2103 vnc_zlog_debug_verbose("%s: bpi %p, peer %p, rd %s", __func__
,
2105 prefix_rd2str(&bpi
->extra
->vnc
.import
.rd
,
2109 sl
= RFAPI_RDINDEX_W_ALLOC(rn
);
2111 if (AF_ETHERNET
== rn
->p
.family
) {
2112 sl
= skiplist_new(0, rfapi_bi_peer_rd_aux_cmp
, NULL
);
2114 sl
= skiplist_new(0, rfapi_bi_peer_rd_cmp
, NULL
);
2116 RFAPI_IT_EXTRA_GET(rn
)->u
.vpn
.idx_rd
= sl
;
2117 agg_lock_node(rn
); /* for skiplist */
2119 assert(!skiplist_insert(sl
, (void *)bpi
, (void *)bpi
));
2120 agg_lock_node(rn
); /* for skiplist entry */
2122 /* NB: BPIs in import tables are not refcounted */
2125 static void rfapiItBiIndexDump(struct agg_node
*rn
)
2127 struct skiplist
*sl
;
2128 void *cursor
= NULL
;
2129 struct bgp_path_info
*k
;
2130 struct bgp_path_info
*v
;
2133 sl
= RFAPI_RDINDEX(rn
);
2137 for (rc
= skiplist_next(sl
, (void **)&k
, (void **)&v
, &cursor
); !rc
;
2138 rc
= skiplist_next(sl
, (void **)&k
, (void **)&v
, &cursor
)) {
2140 char buf
[RD_ADDRSTRLEN
];
2141 char buf_aux_pfx
[PREFIX_STRLEN
];
2143 prefix_rd2str(&k
->extra
->vnc
.import
.rd
, buf
, sizeof(buf
));
2144 if (k
->extra
->vnc
.import
.aux_prefix
.family
) {
2145 prefix2str(&k
->extra
->vnc
.import
.aux_prefix
,
2146 buf_aux_pfx
, sizeof(buf_aux_pfx
));
2148 strlcpy(buf_aux_pfx
, "(none)", sizeof(buf_aux_pfx
));
2150 vnc_zlog_debug_verbose("bpi %p, peer %p, rd %s, aux_prefix %s",
2151 k
, k
->peer
, buf
, buf_aux_pfx
);
2155 static struct bgp_path_info
*rfapiItBiIndexSearch(
2156 struct agg_node
*rn
, /* Import table VPN node */
2157 struct prefix_rd
*prd
, struct peer
*peer
,
2158 struct prefix
*aux_prefix
) /* optional L3 addr for L2 ITs */
2160 struct skiplist
*sl
;
2162 struct bgp_path_info bpi_fake
= {0};
2163 struct bgp_path_info_extra bpi_extra
= {0};
2164 struct bgp_path_info
*bpi_result
;
2166 sl
= RFAPI_RDINDEX(rn
);
2172 char buf
[RD_ADDRSTRLEN
];
2173 char buf_aux_pfx
[PREFIX_STRLEN
];
2176 prefix2str(aux_prefix
, buf_aux_pfx
,
2177 sizeof(buf_aux_pfx
));
2179 strlcpy(buf_aux_pfx
, "(nil)", sizeof(buf_aux_pfx
));
2181 vnc_zlog_debug_verbose("%s want prd=%s, peer=%p, aux_prefix=%s",
2183 prefix_rd2str(prd
, buf
, sizeof(buf
)),
2185 rfapiItBiIndexDump(rn
);
2189 /* threshold is a WAG */
2190 if (sl
->count
< 3) {
2192 vnc_zlog_debug_verbose("%s: short list algorithm", __func__
);
2194 /* if short list, linear search might be faster */
2195 for (bpi_result
= rn
->info
; bpi_result
;
2196 bpi_result
= bpi_result
->next
) {
2199 char buf
[RD_ADDRSTRLEN
];
2201 vnc_zlog_debug_verbose(
2202 "%s: bpi has prd=%s, peer=%p", __func__
,
2203 prefix_rd2str(&bpi_result
->extra
->vnc
2209 if (peer
== bpi_result
->peer
2210 && !prefix_cmp((struct prefix
*)&bpi_result
->extra
2212 (struct prefix
*)prd
)) {
2215 vnc_zlog_debug_verbose(
2216 "%s: peer and RD same, doing aux_prefix check",
2222 &bpi_result
->extra
->vnc
.import
2226 vnc_zlog_debug_verbose("%s: match",
2236 bpi_fake
.peer
= peer
;
2237 bpi_fake
.extra
= &bpi_extra
;
2238 bpi_fake
.extra
->vnc
.import
.rd
= *(struct prefix_rd
*)prd
;
2240 bpi_fake
.extra
->vnc
.import
.aux_prefix
= *aux_prefix
;
2243 bpi_fake
.extra
->vnc
.import
.aux_prefix
.family
= AF_ETHERNET
;
2244 bpi_fake
.extra
->vnc
.import
.aux_prefix
.prefixlen
= 1;
2247 rc
= skiplist_search(sl
, (void *)&bpi_fake
, (void *)&bpi_result
);
2251 vnc_zlog_debug_verbose("%s: no match", __func__
);
2257 vnc_zlog_debug_verbose("%s: matched bpi=%p", __func__
, bpi_result
);
2263 static void rfapiItBiIndexDel(struct agg_node
*rn
, /* Import table VPN node */
2264 struct bgp_path_info
*bpi
) /* old BPI */
2266 struct skiplist
*sl
;
2270 char buf
[RD_ADDRSTRLEN
];
2272 vnc_zlog_debug_verbose("%s: bpi %p, peer %p, rd %s", __func__
,
2274 prefix_rd2str(&bpi
->extra
->vnc
.import
.rd
,
2278 sl
= RFAPI_RDINDEX(rn
);
2281 rc
= skiplist_delete(sl
, (void *)(bpi
), (void *)bpi
);
2283 rfapiItBiIndexDump(rn
);
2287 agg_unlock_node(rn
); /* for skiplist entry */
2289 /* NB: BPIs in import tables are not refcounted */
2293 * Add a backreference at the ENCAP node to the VPN route that
2297 rfapiMonitorEncapAdd(struct rfapi_import_table
*import_table
,
2298 struct prefix
*p
, /* VN address */
2299 struct agg_node
*vpn_rn
, /* VPN node */
2300 struct bgp_path_info
*vpn_bpi
) /* VPN bpi/route */
2302 afi_t afi
= family2afi(p
->family
);
2303 struct agg_node
*rn
;
2304 struct rfapi_monitor_encap
*m
;
2307 rn
= agg_node_get(import_table
->imported_encap
[afi
], p
); /* locks rn */
2310 m
= XCALLOC(MTYPE_RFAPI_MONITOR_ENCAP
,
2311 sizeof(struct rfapi_monitor_encap
));
2318 /* insert to encap node's list */
2319 m
->next
= RFAPI_MONITOR_ENCAP(rn
);
2322 RFAPI_MONITOR_ENCAP_W_ALLOC(rn
) = m
;
2324 /* for easy lookup when deleting vpn route */
2325 vpn_bpi
->extra
->vnc
.import
.hme
= m
;
2327 vnc_zlog_debug_verbose(
2328 "%s: it=%p, vpn_bpi=%p, afi=%d, encap rn=%p, setting vpn_bpi->extra->vnc.import.hme=%p",
2329 __func__
, import_table
, vpn_bpi
, afi
, rn
, m
);
2331 RFAPI_CHECK_REFCOUNT(rn
, SAFI_ENCAP
, 0);
2332 bgp_attr_intern(vpn_bpi
->attr
);
2335 static void rfapiMonitorEncapDelete(struct bgp_path_info
*vpn_bpi
)
2338 * Remove encap monitor
2340 vnc_zlog_debug_verbose("%s: vpn_bpi=%p", __func__
, vpn_bpi
);
2341 if (vpn_bpi
->extra
) {
2342 struct rfapi_monitor_encap
*hme
=
2343 vpn_bpi
->extra
->vnc
.import
.hme
;
2347 vnc_zlog_debug_verbose("%s: hme=%p", __func__
, hme
);
2349 /* Refcount checking takes too long here */
2350 // RFAPI_CHECK_REFCOUNT(hme->rn, SAFI_ENCAP, 0);
2352 hme
->next
->prev
= hme
->prev
;
2354 hme
->prev
->next
= hme
->next
;
2356 RFAPI_MONITOR_ENCAP_W_ALLOC(hme
->rn
) =
2358 /* Refcount checking takes too long here */
2359 // RFAPI_CHECK_REFCOUNT(hme->rn, SAFI_ENCAP, 1);
2361 /* see if the struct rfapi_it_extra is empty and can be
2363 rfapiMonitorExtraPrune(SAFI_ENCAP
, hme
->rn
);
2365 agg_unlock_node(hme
->rn
); /* decr ref count */
2366 XFREE(MTYPE_RFAPI_MONITOR_ENCAP
, hme
);
2367 vpn_bpi
->extra
->vnc
.import
.hme
= NULL
;
2373 * quagga lib/thread.h says this must return int even though
2374 * it doesn't do anything with the return value
2376 static int rfapiWithdrawTimerVPN(struct thread
*t
)
2378 struct rfapi_withdraw
*wcb
= t
->arg
;
2379 struct bgp_path_info
*bpi
= wcb
->info
;
2380 struct bgp
*bgp
= bgp_get_default();
2382 struct rfapi_monitor_vpn
*moved
;
2386 vnc_zlog_debug_verbose(
2387 "%s: NULL BGP pointer, assume shutdown race condition!!!",
2391 if (bgp_flag_check(bgp
, BGP_FLAG_DELETE_IN_PROGRESS
)) {
2392 vnc_zlog_debug_verbose(
2393 "%s: BGP delete in progress, assume shutdown race condition!!!",
2399 assert(wcb
->import_table
);
2402 RFAPI_CHECK_REFCOUNT(wcb
->node
, SAFI_MPLS_VPN
, wcb
->lockoffset
);
2407 vnc_zlog_debug_verbose(
2408 "%s: removing bpi %p at prefix %s/%d", __func__
, bpi
,
2409 rfapi_ntop(wcb
->node
->p
.family
, &wcb
->node
->p
.u
.prefix
,
2411 wcb
->node
->p
.prefixlen
);
2415 * Remove the route (doubly-linked)
2417 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_VALID
)
2418 && VALID_INTERIOR_TYPE(bpi
->type
))
2419 RFAPI_MONITOR_EXTERIOR(wcb
->node
)->valid_interior_count
--;
2421 afi
= family2afi(wcb
->node
->p
.family
);
2422 wcb
->import_table
->holddown_count
[afi
] -= 1; /* keep count consistent */
2423 rfapiItBiIndexDel(wcb
->node
, bpi
);
2424 rfapiBgpInfoDetach(wcb
->node
, bpi
); /* with removed bpi */
2426 vnc_import_bgp_exterior_del_route_interior(bgp
, wcb
->import_table
,
2431 * If VNC is configured to send response remove messages, AND
2432 * if the removed route had a UN address, do response removal
2435 if (!(bgp
->rfapi_cfg
->flags
2436 & BGP_VNC_CONFIG_RESPONSE_REMOVAL_DISABLE
)) {
2438 int has_valid_duplicate
= 0;
2439 struct bgp_path_info
*bpii
;
2442 * First check if there are any OTHER routes at this node
2443 * that have the same nexthop and a valid UN address. If
2444 * there are (e.g., from other peers), then the route isn't
2445 * really gone, so skip sending a response removal message.
2447 for (bpii
= wcb
->node
->info
; bpii
; bpii
= bpii
->next
) {
2448 if (rfapiVpnBiSamePtUn(bpi
, bpii
)) {
2449 has_valid_duplicate
= 1;
2454 vnc_zlog_debug_verbose("%s: has_valid_duplicate=%d", __func__
,
2455 has_valid_duplicate
);
2457 if (!has_valid_duplicate
) {
2458 rfapiRibPendingDeleteRoute(bgp
, wcb
->import_table
, afi
,
2463 rfapiMonitorEncapDelete(bpi
);
2466 * If there are no VPN monitors at this VPN Node A,
2469 if (!RFAPI_MONITOR_VPN(wcb
->node
)) {
2470 vnc_zlog_debug_verbose("%s: no VPN monitors at this node",
2476 * rfapiMonitorMoveShorter only moves monitors if there are
2477 * no remaining valid routes at the current node
2479 moved
= rfapiMonitorMoveShorter(wcb
->node
, 1);
2482 rfapiMonitorMovedUp(wcb
->import_table
, wcb
->node
, moved
->node
,
2490 rfapiBgpInfoFree(bpi
);
2494 * If route count at this node has gone to 0, withdraw exported prefix
2496 if (!wcb
->node
->info
) {
2497 /* see if the struct rfapi_it_extra is empty and can be freed */
2498 rfapiMonitorExtraPrune(SAFI_MPLS_VPN
, wcb
->node
);
2499 vnc_direct_bgp_del_prefix(bgp
, wcb
->import_table
, wcb
->node
);
2500 vnc_zebra_del_prefix(bgp
, wcb
->import_table
, wcb
->node
);
2503 * nexthop change event
2504 * vnc_direct_bgp_add_prefix() will recompute the VN addr
2507 vnc_direct_bgp_add_prefix(bgp
, wcb
->import_table
, wcb
->node
);
2510 RFAPI_CHECK_REFCOUNT(wcb
->node
, SAFI_MPLS_VPN
, 1 + wcb
->lockoffset
);
2511 agg_unlock_node(wcb
->node
); /* decr ref count */
2512 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
2517 * This works for multiprotocol extension, but not for plain ol'
2518 * unicast IPv4 because that nexthop is stored in attr->nexthop
2520 void rfapiNexthop2Prefix(struct attr
*attr
, struct prefix
*p
)
2525 memset(p
, 0, sizeof(struct prefix
));
2527 switch (p
->family
= BGP_MP_NEXTHOP_FAMILY(attr
->mp_nexthop_len
)) {
2529 p
->u
.prefix4
= attr
->mp_nexthop_global_in
;
2534 p
->u
.prefix6
= attr
->mp_nexthop_global
;
2539 vnc_zlog_debug_verbose("%s: Family is unknown = %d", __func__
,
2544 void rfapiUnicastNexthop2Prefix(afi_t afi
, struct attr
*attr
, struct prefix
*p
)
2546 if (afi
== AFI_IP
) {
2547 p
->family
= AF_INET
;
2549 p
->u
.prefix4
= attr
->nexthop
;
2551 rfapiNexthop2Prefix(attr
, p
);
2555 static int rfapiAttrNexthopAddrDifferent(struct prefix
*p1
, struct prefix
*p2
)
2558 vnc_zlog_debug_verbose("%s: p1 or p2 is NULL", __func__
);
2563 * Are address families the same?
2565 if (p1
->family
!= p2
->family
) {
2569 switch (p1
->family
) {
2571 if (IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
2576 if (IPV6_ADDR_SAME(&p1
->u
.prefix6
, &p2
->u
.prefix6
))
2587 static void rfapiCopyUnEncap2VPN(struct bgp_path_info
*encap_bpi
,
2588 struct bgp_path_info
*vpn_bpi
)
2590 if (!vpn_bpi
|| !vpn_bpi
->extra
) {
2591 zlog_warn("%s: no vpn bpi attr/extra, can't copy UN address",
2596 switch (BGP_MP_NEXTHOP_FAMILY(encap_bpi
->attr
->mp_nexthop_len
)) {
2600 * instrumentation to debug segfault of 091127
2602 vnc_zlog_debug_verbose("%s: vpn_bpi=%p", __func__
, vpn_bpi
);
2604 vnc_zlog_debug_verbose("%s: vpn_bpi->extra=%p",
2605 __func__
, vpn_bpi
->extra
);
2608 vpn_bpi
->extra
->vnc
.import
.un_family
= AF_INET
;
2609 vpn_bpi
->extra
->vnc
.import
.un
.addr4
=
2610 encap_bpi
->attr
->mp_nexthop_global_in
;
2614 vpn_bpi
->extra
->vnc
.import
.un_family
= AF_INET6
;
2615 vpn_bpi
->extra
->vnc
.import
.un
.addr6
=
2616 encap_bpi
->attr
->mp_nexthop_global
;
2620 zlog_warn("%s: invalid encap nexthop length: %d", __func__
,
2621 encap_bpi
->attr
->mp_nexthop_len
);
2622 vpn_bpi
->extra
->vnc
.import
.un_family
= 0;
2628 * returns 0 on success, nonzero on error
2631 rfapiWithdrawEncapUpdateCachedUn(struct rfapi_import_table
*import_table
,
2632 struct bgp_path_info
*encap_bpi
,
2633 struct agg_node
*vpn_rn
,
2634 struct bgp_path_info
*vpn_bpi
)
2639 * clear cached UN address
2641 if (!vpn_bpi
|| !vpn_bpi
->extra
) {
2643 "%s: missing VPN bpi/extra, can't clear UN addr",
2647 vpn_bpi
->extra
->vnc
.import
.un_family
= 0;
2648 memset(&vpn_bpi
->extra
->vnc
.import
.un
, 0,
2649 sizeof(vpn_bpi
->extra
->vnc
.import
.un
));
2650 if (CHECK_FLAG(vpn_bpi
->flags
, BGP_PATH_VALID
)) {
2651 if (rfapiGetVncTunnelUnAddr(vpn_bpi
->attr
, NULL
)) {
2652 UNSET_FLAG(vpn_bpi
->flags
, BGP_PATH_VALID
);
2653 if (VALID_INTERIOR_TYPE(vpn_bpi
->type
))
2654 RFAPI_MONITOR_EXTERIOR(vpn_rn
)
2655 ->valid_interior_count
--;
2656 /* signal interior route withdrawal to
2657 * import-exterior */
2658 vnc_import_bgp_exterior_del_route_interior(
2659 bgp_get_default(), import_table
, vpn_rn
,
2666 zlog_warn("%s: missing VPN bpi, can't clear UN addr",
2670 rfapiCopyUnEncap2VPN(encap_bpi
, vpn_bpi
);
2671 if (!CHECK_FLAG(vpn_bpi
->flags
, BGP_PATH_VALID
)) {
2672 SET_FLAG(vpn_bpi
->flags
, BGP_PATH_VALID
);
2673 if (VALID_INTERIOR_TYPE(vpn_bpi
->type
))
2674 RFAPI_MONITOR_EXTERIOR(vpn_rn
)
2675 ->valid_interior_count
++;
2676 /* signal interior route withdrawal to import-exterior
2678 vnc_import_bgp_exterior_add_route_interior(
2679 bgp_get_default(), import_table
, vpn_rn
,
2686 static int rfapiWithdrawTimerEncap(struct thread
*t
)
2688 struct rfapi_withdraw
*wcb
= t
->arg
;
2689 struct bgp_path_info
*bpi
= wcb
->info
;
2690 int was_first_route
= 0;
2691 struct rfapi_monitor_encap
*em
;
2692 struct skiplist
*vpn_node_sl
= skiplist_new(0, NULL
, NULL
);
2696 assert(wcb
->import_table
);
2698 RFAPI_CHECK_REFCOUNT(wcb
->node
, SAFI_ENCAP
, 0);
2700 if (wcb
->node
->info
== bpi
)
2701 was_first_route
= 1;
2704 * Remove the route/bpi and free it
2706 rfapiBgpInfoDetach(wcb
->node
, bpi
);
2707 rfapiBgpInfoFree(bpi
);
2709 if (!was_first_route
)
2712 for (em
= RFAPI_MONITOR_ENCAP(wcb
->node
); em
; em
= em
->next
) {
2715 * Update monitoring VPN BPIs with new encap info at the
2716 * head of the encap bpi chain (which could be NULL after
2717 * removing the expiring bpi above)
2719 if (rfapiWithdrawEncapUpdateCachedUn(wcb
->import_table
,
2720 wcb
->node
->info
, em
->node
,
2725 * Build a list of unique VPN nodes referenced by these
2727 * Use a skiplist for speed.
2729 skiplist_insert(vpn_node_sl
, em
->node
, em
->node
);
2734 * for each VPN node referenced in the ENCAP monitors:
2736 struct agg_node
*rn
;
2737 while (!skiplist_first(vpn_node_sl
, (void **)&rn
, NULL
)) {
2738 if (!wcb
->node
->info
) {
2739 struct rfapi_monitor_vpn
*moved
;
2741 moved
= rfapiMonitorMoveShorter(rn
, 0);
2743 // rfapiDoRouteCallback(wcb->import_table,
2744 // moved->node, moved);
2745 rfapiMonitorMovedUp(wcb
->import_table
, rn
,
2746 moved
->node
, moved
);
2749 // rfapiDoRouteCallback(wcb->import_table, rn, NULL);
2750 rfapiMonitorItNodeChanged(wcb
->import_table
, rn
, NULL
);
2752 skiplist_delete_first(vpn_node_sl
);
2756 RFAPI_CHECK_REFCOUNT(wcb
->node
, SAFI_ENCAP
, 1);
2757 agg_unlock_node(wcb
->node
); /* decr ref count */
2758 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
2759 skiplist_free(vpn_node_sl
);
2765 * Works for both VPN and ENCAP routes; timer_service_func is different
2769 rfapiBiStartWithdrawTimer(struct rfapi_import_table
*import_table
,
2770 struct agg_node
*rn
, struct bgp_path_info
*bpi
,
2771 afi_t afi
, safi_t safi
,
2772 int (*timer_service_func
)(struct thread
*))
2775 struct rfapi_withdraw
*wcb
;
2777 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
)) {
2779 * Already on the path to being withdrawn,
2780 * should already have a timer set up to
2783 vnc_zlog_debug_verbose(
2784 "%s: already being withdrawn, do nothing", __func__
);
2788 rfapiGetVncLifetime(bpi
->attr
, &lifetime
);
2789 vnc_zlog_debug_verbose("%s: VNC lifetime is %u", __func__
, lifetime
);
2792 * withdrawn routes get to hang around for a while
2794 SET_FLAG(bpi
->flags
, BGP_PATH_REMOVED
);
2796 /* set timer to remove the route later */
2797 lifetime
= rfapiGetHolddownFromLifetime(lifetime
);
2798 vnc_zlog_debug_verbose("%s: using timeout %u", __func__
, lifetime
);
2801 * Stash import_table, node, and info for use by timer
2802 * service routine, which is supposed to free the wcb.
2804 wcb
= XCALLOC(MTYPE_RFAPI_WITHDRAW
, sizeof(struct rfapi_withdraw
));
2808 wcb
->import_table
= import_table
;
2809 bgp_attr_intern(bpi
->attr
);
2811 if (VNC_DEBUG(VERBOSE
)) {
2812 vnc_zlog_debug_verbose(
2813 "%s: wcb values: node=%p, info=%p, import_table=%p (bpi follows)",
2814 __func__
, wcb
->node
, wcb
->info
, wcb
->import_table
);
2815 rfapiPrintBi(NULL
, bpi
);
2820 if (lifetime
> UINT32_MAX
/ 1001) {
2821 /* sub-optimal case, but will probably never happen */
2822 bpi
->extra
->vnc
.import
.timer
= NULL
;
2823 thread_add_timer(bm
->master
, timer_service_func
, wcb
, lifetime
,
2824 &bpi
->extra
->vnc
.import
.timer
);
2826 static uint32_t jitter
;
2827 uint32_t lifetime_msec
;
2830 * the goal here is to spread out the timers so they are
2831 * sortable in the skip list
2833 if (++jitter
>= 1000)
2836 lifetime_msec
= (lifetime
* 1000) + jitter
;
2838 bpi
->extra
->vnc
.import
.timer
= NULL
;
2839 thread_add_timer_msec(bm
->master
, timer_service_func
, wcb
,
2841 &bpi
->extra
->vnc
.import
.timer
);
2844 /* re-sort route list (BGP_PATH_REMOVED routes are last) */
2845 if (((struct bgp_path_info
*)rn
->info
)->next
) {
2846 rfapiBgpInfoDetach(rn
, bpi
);
2847 rfapiBgpInfoAttachSorted(rn
, bpi
, afi
, safi
);
2852 typedef void(rfapi_bi_filtered_import_f
)(struct rfapi_import_table
*, int,
2853 struct peer
*, void *, struct prefix
*,
2854 struct prefix
*, afi_t
,
2855 struct prefix_rd
*, struct attr
*,
2856 uint8_t, uint8_t, uint32_t *);
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 */
2906 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 #if 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 #if 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 %s/%d",
3082 inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
,
3086 rfapiBiStartWithdrawTimer(
3087 import_table
, rn
, bpi
, afi
, SAFI_ENCAP
,
3088 rfapiWithdrawTimerEncap
);
3091 vnc_zlog_debug_verbose(
3092 "%s: %s at prefix %s/%d", __func__
,
3093 ((action
== FIF_ACTION_KILL
)
3096 inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
,
3101 * If this route is waiting to be deleted
3103 * a previous withdraw, we must cancel its
3106 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
)
3107 && bpi
->extra
->vnc
.import
.timer
) {
3110 (struct thread
*)bpi
->extra
->vnc
3112 struct rfapi_withdraw
*wcb
= t
->arg
;
3114 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
3118 if (action
== FIF_ACTION_UPDATE
) {
3119 rfapiBgpInfoDetach(rn
, bpi
);
3120 rfapiBgpInfoFree(bpi
);
3124 * Kill: do export stuff when removing
3127 struct rfapi_withdraw
*wcb
;
3131 * pretend we're an expiring timer
3134 MTYPE_RFAPI_WITHDRAW
,
3135 sizeof(struct rfapi_withdraw
));
3138 wcb
->import_table
= import_table
;
3139 memset(&t
, 0, sizeof(t
));
3141 rfapiWithdrawTimerEncap(
3142 &t
); /* frees wcb */
3151 RFAPI_CHECK_REFCOUNT(rn
, SAFI_ENCAP
, replacing
? 1 : 0);
3153 if (action
== FIF_ACTION_WITHDRAW
|| action
== FIF_ACTION_KILL
)
3157 rfapiBgpInfoCreate(attr
, peer
, rfd
, prd
, type
, sub_type
, NULL
);
3161 agg_lock_node(rn
); /* incr ref count for new BPI */
3163 rn
= agg_node_get(rt
, p
);
3166 vnc_zlog_debug_verbose(
3167 "%s: (afi=%d, rn=%p) inserting at prefix %s/%d", __func__
, afi
,
3168 rn
, inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, BUFSIZ
),
3171 rfapiBgpInfoAttachSorted(rn
, info_new
, afi
, SAFI_ENCAP
);
3174 * Delete holddown routes from same NVE. See details in
3175 * rfapiBgpInfoFilteredImportVPN()
3177 for (bpi
= info_new
->next
; bpi
; bpi
= next
) {
3179 struct prefix pfx_un
;
3183 if (!CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
))
3187 * We already match the VN address (it is the prefix
3188 * of the route node)
3191 if (!rfapiGetNexthop(bpi
->attr
, &pfx_un
)
3192 && prefix_same(&pfx_un
, &un_prefix
)) {
3200 vnc_zlog_debug_verbose(
3201 "%s: removing holddown bpi matching NVE of new route",
3203 if (bpi
->extra
->vnc
.import
.timer
) {
3205 (struct thread
*)bpi
->extra
->vnc
.import
.timer
;
3206 struct rfapi_withdraw
*wcb
= t
->arg
;
3208 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
3211 rfapiExpireEncapNow(import_table
, rn
, bpi
);
3214 rfapiNexthop2Prefix(((struct bgp_path_info
*)(rn
->info
))->attr
,
3218 * If the nexthop address of the selected Encap route (i.e.,
3219 * the UN address) has changed, then we must update the VPN
3220 * routes that refer to this Encap route and possibly force
3223 if (rfapiAttrNexthopAddrDifferent(&p_firstbpi_old
, &p_firstbpi_new
)) {
3225 struct rfapi_monitor_encap
*m
;
3226 struct rfapi_monitor_encap
*mnext
;
3228 struct agg_node
*referenced_vpn_prefix
;
3231 * Optimized approach: build radix tree on the fly to
3232 * hold list of VPN nodes referenced by the ENCAP monitors
3234 * The nodes in this table correspond to prefixes of VPN routes.
3235 * The "info" pointer of the node points to a chain of
3236 * struct rfapi_monitor_encap, each of which refers to a
3237 * specific VPN node.
3239 struct agg_table
*referenced_vpn_table
;
3241 referenced_vpn_table
= agg_table_init();
3242 assert(referenced_vpn_table
);
3245 * iterate over the set of monitors at this ENCAP node.
3247 #if DEBUG_ENCAP_MONITOR
3248 vnc_zlog_debug_verbose("%s: examining monitors at rn=%p",
3251 for (m
= RFAPI_MONITOR_ENCAP(rn
); m
; m
= m
->next
) {
3254 * For each referenced bpi/route, copy the ENCAP route's
3255 * nexthop to the VPN route's cached UN address field
3257 * the address family of the cached UN address field.
3259 rfapiCopyUnEncap2VPN(info_new
, m
->bpi
);
3260 if (!CHECK_FLAG(m
->bpi
->flags
, BGP_PATH_VALID
)) {
3261 SET_FLAG(m
->bpi
->flags
, BGP_PATH_VALID
);
3262 if (VALID_INTERIOR_TYPE(m
->bpi
->type
))
3263 RFAPI_MONITOR_EXTERIOR(m
->node
)
3264 ->valid_interior_count
++;
3265 vnc_import_bgp_exterior_add_route_interior(
3266 bgp
, import_table
, m
->node
, m
->bpi
);
3270 * Build a list of unique VPN nodes referenced by these
3273 * There could be more than one VPN node here with a
3275 * prefix. Those are currently in an unsorted linear
3280 referenced_vpn_prefix
=
3281 agg_node_get(referenced_vpn_table
, &m
->node
->p
);
3282 assert(referenced_vpn_prefix
);
3283 for (mnext
= referenced_vpn_prefix
->info
; mnext
;
3284 mnext
= mnext
->next
) {
3286 if (mnext
->node
== m
->node
)
3292 * already have an entry for this VPN node
3294 agg_unlock_node(referenced_vpn_prefix
);
3297 MTYPE_RFAPI_MONITOR_ENCAP
,
3298 sizeof(struct rfapi_monitor_encap
));
3300 mnext
->node
= m
->node
;
3301 mnext
->next
= referenced_vpn_prefix
->info
;
3302 referenced_vpn_prefix
->info
= mnext
;
3307 * for each VPN node referenced in the ENCAP monitors:
3309 for (referenced_vpn_prefix
=
3310 agg_route_top(referenced_vpn_table
);
3311 referenced_vpn_prefix
;
3312 referenced_vpn_prefix
=
3313 agg_route_next(referenced_vpn_prefix
)) {
3315 while ((m
= referenced_vpn_prefix
->info
)) {
3319 rfapiMonitorMoveLonger(m
->node
);
3320 for (n
= m
->node
; n
; n
= agg_node_parent(n
)) {
3321 // rfapiDoRouteCallback(import_table, n,
3324 rfapiMonitorItNodeChanged(import_table
, m
->node
,
3327 referenced_vpn_prefix
->info
= m
->next
;
3328 agg_unlock_node(referenced_vpn_prefix
);
3329 XFREE(MTYPE_RFAPI_MONITOR_ENCAP
, m
);
3332 agg_table_finish(referenced_vpn_table
);
3335 RFAPI_CHECK_REFCOUNT(rn
, SAFI_ENCAP
, 0);
3338 static void rfapiExpireVpnNow(struct rfapi_import_table
*it
,
3339 struct agg_node
*rn
, struct bgp_path_info
*bpi
,
3342 struct rfapi_withdraw
*wcb
;
3346 * pretend we're an expiring timer
3348 wcb
= XCALLOC(MTYPE_RFAPI_WITHDRAW
, sizeof(struct rfapi_withdraw
));
3351 wcb
->import_table
= it
;
3352 wcb
->lockoffset
= lockoffset
;
3353 memset(&t
, 0, sizeof(t
));
3355 rfapiWithdrawTimerVPN(&t
); /* frees wcb */
3360 * import a bgp_path_info if its route target list intersects with the
3361 * import table's route target list
3363 void rfapiBgpInfoFilteredImportVPN(
3364 struct rfapi_import_table
*import_table
, int action
, struct peer
*peer
,
3365 void *rfd
, /* set for looped back routes */
3367 struct prefix
*aux_prefix
, /* AFI_L2VPN: optional IP */
3368 afi_t afi
, struct prefix_rd
*prd
,
3369 struct attr
*attr
, /* part of bgp_path_info */
3370 uint8_t type
, /* part of bgp_path_info */
3371 uint8_t sub_type
, /* part of bgp_path_info */
3372 uint32_t *label
) /* part of bgp_path_info */
3374 struct agg_table
*rt
= NULL
;
3375 struct agg_node
*rn
;
3377 struct bgp_path_info
*info_new
;
3378 struct bgp_path_info
*bpi
;
3379 struct bgp_path_info
*next
;
3381 struct prefix vn_prefix
;
3382 struct prefix un_prefix
;
3383 int un_prefix_valid
= 0;
3384 struct agg_node
*ern
;
3386 int original_had_routes
= 0;
3387 struct prefix original_nexthop
;
3388 const char *action_str
= NULL
;
3392 bgp
= bgp_get_default(); /* assume 1 instance for now */
3395 case FIF_ACTION_UPDATE
:
3396 action_str
= "update";
3398 case FIF_ACTION_WITHDRAW
:
3399 action_str
= "withdraw";
3401 case FIF_ACTION_KILL
:
3402 action_str
= "kill";
3409 if (import_table
== bgp
->rfapi
->it_ce
)
3412 vnc_zlog_debug_verbose("%s: entry: %s%s: prefix %s/%d: it %p, afi %s",
3413 __func__
, (is_it_ce
? "CE-IT " : ""), action_str
,
3414 rfapi_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
3415 p
->prefixlen
, import_table
, afi2str(afi
));
3420 * Compare rt lists. If no intersection, don't import this route
3421 * On a withdraw, peer and RD are sufficient to determine if
3424 if (action
== FIF_ACTION_UPDATE
) {
3425 if (!attr
|| !attr
->ecommunity
) {
3427 vnc_zlog_debug_verbose(
3428 "%s: attr, extra, or ecommunity missing, not importing",
3432 if ((import_table
!= bgp
->rfapi
->it_ce
)
3433 && !rfapiEcommunitiesIntersect(import_table
->rt_import_list
,
3434 attr
->ecommunity
)) {
3436 vnc_zlog_debug_verbose(
3437 "%s: it=%p: no ecommunity intersection",
3438 __func__
, import_table
);
3442 memset(&vn_prefix
, 0,
3443 sizeof(vn_prefix
)); /* keep valgrind happy */
3444 if (rfapiGetNexthop(attr
, &vn_prefix
)) {
3445 /* missing nexthop address would be a bad, bad thing */
3446 vnc_zlog_debug_verbose("%s: missing nexthop", __func__
);
3452 * Figure out which radix tree the route would go into
3458 rt
= import_table
->imported_vpn
[afi
];
3462 flog_err(EC_LIB_DEVELOPMENT
, "%s: bad afi %d", __func__
, afi
);
3467 memset(&original_nexthop
, 0, sizeof(original_nexthop
));
3470 * agg_node_lookup returns a node only if there is at least
3471 * one route attached.
3473 rn
= agg_node_lookup(rt
, p
);
3475 vnc_zlog_debug_verbose("%s: rn=%p", __func__
, rn
);
3479 RFAPI_CHECK_REFCOUNT(rn
, SAFI_MPLS_VPN
, 1);
3480 agg_unlock_node(rn
); /* undo lock in agg_node_lookup */
3483 original_had_routes
= 1;
3485 if (VNC_DEBUG(VERBOSE
)) {
3486 vnc_zlog_debug_verbose("%s: showing IT node on entry",
3488 rfapiShowItNode(NULL
, rn
); /* debug */
3492 * Look for same route (will have same RD and peer)
3494 bpi
= rfapiItBiIndexSearch(rn
, prd
, peer
, aux_prefix
);
3499 * This was an old test when we iterated over the
3500 * BPIs linearly. Since we're now looking up with
3501 * RD and peer, comparing types should not be
3502 * needed. Changed to assertion.
3504 * Compare types. Doing so prevents a RFP-originated
3505 * route from matching an imported route, for example.
3507 if (VNC_DEBUG(VERBOSE
) && bpi
->type
!= type
)
3508 /* should be handled by RDs, but warn for now */
3509 zlog_warn("%s: type mismatch! (bpi=%d, arg=%d)",
3510 __func__
, bpi
->type
, type
);
3512 vnc_zlog_debug_verbose("%s: found matching bpi",
3516 * In the special CE table, withdrawals occur without
3519 if (import_table
== bgp
->rfapi
->it_ce
) {
3520 vnc_direct_bgp_del_route_ce(bgp
, rn
, bpi
);
3521 if (action
== FIF_ACTION_WITHDRAW
)
3522 action
= FIF_ACTION_KILL
;
3525 if (action
== FIF_ACTION_WITHDRAW
) {
3527 int washolddown
= CHECK_FLAG(bpi
->flags
,
3530 vnc_zlog_debug_verbose(
3531 "%s: withdrawing at prefix %s/%d%s",
3532 __func__
, rfapi_ntop(rn
->p
.family
,
3537 ? " (already being withdrawn)"
3542 rfapiBiStartWithdrawTimer(
3543 import_table
, rn
, bpi
, afi
,
3545 rfapiWithdrawTimerVPN
);
3547 RFAPI_UPDATE_ITABLE_COUNT(
3548 bpi
, import_table
, afi
, -1);
3549 import_table
->holddown_count
[afi
] += 1;
3553 vnc_zlog_debug_verbose(
3554 "%s: %s at prefix %s/%d", __func__
,
3555 ((action
== FIF_ACTION_KILL
)
3558 rfapi_ntop(rn
->p
.family
,
3559 &rn
->p
.u
.prefix
, buf
,
3564 * If this route is waiting to be deleted
3566 * a previous withdraw, we must cancel its
3569 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
)
3570 && bpi
->extra
->vnc
.import
.timer
) {
3573 (struct thread
*)bpi
->extra
->vnc
3575 struct rfapi_withdraw
*wcb
= t
->arg
;
3577 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
3580 import_table
->holddown_count
[afi
] -= 1;
3581 RFAPI_UPDATE_ITABLE_COUNT(
3582 bpi
, import_table
, afi
, 1);
3585 * decrement remote count (if route is remote)
3587 * we are going to remove it below
3589 RFAPI_UPDATE_ITABLE_COUNT(bpi
, import_table
,
3591 if (action
== FIF_ACTION_UPDATE
) {
3595 * make copy of original nexthop so we
3596 * can see if it changed
3598 rfapiGetNexthop(bpi
->attr
,
3602 * remove bpi without doing any export
3605 if (CHECK_FLAG(bpi
->flags
,
3607 && VALID_INTERIOR_TYPE(bpi
->type
))
3608 RFAPI_MONITOR_EXTERIOR(rn
)
3609 ->valid_interior_count
--;
3610 rfapiItBiIndexDel(rn
, bpi
);
3611 rfapiBgpInfoDetach(rn
, bpi
);
3612 rfapiMonitorEncapDelete(bpi
);
3613 vnc_import_bgp_exterior_del_route_interior(
3614 bgp
, import_table
, rn
, bpi
);
3615 rfapiBgpInfoFree(bpi
);
3619 * remove bpi and do export processing
3621 import_table
->holddown_count
[afi
] += 1;
3622 rfapiExpireVpnNow(import_table
, rn
, bpi
,
3630 RFAPI_CHECK_REFCOUNT(rn
, SAFI_MPLS_VPN
, replacing
? 1 : 0);
3632 if (action
== FIF_ACTION_WITHDRAW
|| action
== FIF_ACTION_KILL
) {
3638 rfapiBgpInfoCreate(attr
, peer
, rfd
, prd
, type
, sub_type
, label
);
3641 * lookup un address in encap table
3643 ern
= agg_node_match(import_table
->imported_encap
[afi
], &vn_prefix
);
3645 rfapiCopyUnEncap2VPN(ern
->info
, info_new
);
3646 agg_unlock_node(ern
); /* undo lock in route_note_match */
3648 char bpf
[PREFIX_STRLEN
];
3650 prefix2str(&vn_prefix
, bpf
, sizeof(bpf
));
3651 /* Not a big deal, just means VPN route got here first */
3652 vnc_zlog_debug_verbose("%s: no encap route for vn addr %s",
3654 info_new
->extra
->vnc
.import
.un_family
= 0;
3662 * No need to increment reference count, so only "get"
3663 * if the node is not there already
3665 rn
= agg_node_get(rt
, p
);
3669 * For ethernet routes, if there is an accompanying IP address,
3670 * save it in the bpi
3672 if ((AFI_L2VPN
== afi
) && aux_prefix
) {
3674 vnc_zlog_debug_verbose("%s: setting BPI's aux_prefix",
3676 info_new
->extra
->vnc
.import
.aux_prefix
= *aux_prefix
;
3679 vnc_zlog_debug_verbose(
3680 "%s: inserting bpi %p at prefix %s/%d #%d", __func__
, info_new
,
3681 rfapi_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, BUFSIZ
),
3682 rn
->p
.prefixlen
, rn
->lock
);
3684 rfapiBgpInfoAttachSorted(rn
, info_new
, afi
, SAFI_MPLS_VPN
);
3685 rfapiItBiIndexAdd(rn
, info_new
);
3686 if (!rfapiGetUnAddrOfVpnBi(info_new
, NULL
)) {
3687 if (VALID_INTERIOR_TYPE(info_new
->type
))
3688 RFAPI_MONITOR_EXTERIOR(rn
)->valid_interior_count
++;
3689 SET_FLAG(info_new
->flags
, BGP_PATH_VALID
);
3691 RFAPI_UPDATE_ITABLE_COUNT(info_new
, import_table
, afi
, 1);
3692 vnc_import_bgp_exterior_add_route_interior(bgp
, import_table
, rn
,
3695 if (import_table
== bgp
->rfapi
->it_ce
)
3696 vnc_direct_bgp_add_route_ce(bgp
, rn
, info_new
);
3698 if (VNC_DEBUG(VERBOSE
)) {
3699 vnc_zlog_debug_verbose("%s: showing IT node", __func__
);
3700 rfapiShowItNode(NULL
, rn
); /* debug */
3703 rfapiMonitorEncapAdd(import_table
, &vn_prefix
, rn
, info_new
);
3705 if (!rfapiGetUnAddrOfVpnBi(info_new
, &un_prefix
)) {
3708 * if we have a valid UN address (either via Encap route
3709 * or via tunnel attribute), then we should attempt
3710 * to move any monitors at less-specific nodes to this node
3712 rfapiMonitorMoveLonger(rn
);
3714 un_prefix_valid
= 1;
3718 * 101129 Enhancement: if we add a route (implication: it is not
3719 * in holddown), delete all other routes from this nve at this
3720 * node that are in holddown, regardless of peer.
3722 * Reasons it's OK to do that:
3724 * - if the holddown route being deleted originally came from BGP VPN,
3725 * it is already gone from BGP (implication of holddown), so there
3726 * won't be any added inconsistency with the BGP RIB.
3728 * - once a fresh route is added at a prefix, any routes in holddown
3729 * at that prefix will not show up in RFP responses, so deleting
3730 * the holddown routes won't affect the contents of responses.
3732 * - lifetimes are supposed to be consistent, so there should not
3733 * be a case where the fresh route has a shorter lifetime than
3734 * the holddown route, so we don't expect the fresh route to
3735 * disappear and complete its holddown time before the existing
3736 * holddown routes time out. Therefore, we won't have a situation
3737 * where we expect the existing holddown routes to be hidden and
3738 * then to reappear sometime later (as holddown routes) in a
3741 * Among other things, this would enable us to skirt the problem
3742 * of local holddown routes that refer to NVE descriptors that
3743 * have already been closed (if the same NVE triggers a subsequent
3744 * rfapi_open(), the new peer is different and doesn't match the
3745 * peer of the holddown route, so the stale holddown route still
3746 * hangs around until it times out instead of just being replaced
3747 * by the fresh route).
3750 * We know that the new bpi will have been inserted before any routes
3751 * in holddown, so we can skip any that came before it
3753 for (bpi
= info_new
->next
; bpi
; bpi
= next
) {
3755 struct prefix pfx_vn
;
3756 struct prefix pfx_un
;
3758 int remote_peer_match
= 0;
3765 if (!CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
))
3769 * Must match VN address (nexthop of VPN route)
3771 if (rfapiGetNexthop(bpi
->attr
, &pfx_vn
))
3773 if (!prefix_same(&pfx_vn
, &vn_prefix
))
3776 if (un_prefix_valid
&& /* new route UN addr */
3777 !rfapiGetUnAddrOfVpnBi(bpi
, &pfx_un
)
3778 && /* old route UN addr */
3779 prefix_same(&pfx_un
, &un_prefix
)) { /* compare */
3782 if (!RFAPI_LOCAL_BI(bpi
) && !RFAPI_LOCAL_BI(info_new
)
3783 && sockunion_same(&bpi
->peer
->su
, &info_new
->peer
->su
)) {
3784 /* old & new are both remote, same peer */
3785 remote_peer_match
= 1;
3788 if (!un_match
& !remote_peer_match
)
3791 vnc_zlog_debug_verbose(
3792 "%s: removing holddown bpi matching NVE of new route",
3794 if (bpi
->extra
->vnc
.import
.timer
) {
3796 (struct thread
*)bpi
->extra
->vnc
.import
.timer
;
3797 struct rfapi_withdraw
*wcb
= t
->arg
;
3799 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
3802 rfapiExpireVpnNow(import_table
, rn
, bpi
, 0);
3805 if (!original_had_routes
) {
3807 * We went from 0 usable routes to 1 usable route. Perform the
3808 * "Adding a Route" export process.
3810 vnc_direct_bgp_add_prefix(bgp
, import_table
, rn
);
3811 vnc_zebra_add_prefix(bgp
, import_table
, rn
);
3814 * Check for nexthop change event
3815 * Note: the prefix_same() test below detects two situations:
3816 * 1. route is replaced, new route has different nexthop
3817 * 2. new route is added (original_nexthop is 0)
3819 struct prefix new_nexthop
;
3821 rfapiGetNexthop(attr
, &new_nexthop
);
3822 if (!prefix_same(&original_nexthop
, &new_nexthop
)) {
3824 * nexthop change event
3825 * vnc_direct_bgp_add_prefix() will recompute VN addr
3828 vnc_direct_bgp_add_prefix(bgp
, import_table
, rn
);
3832 if (!(bgp
->rfapi_cfg
->flags
& BGP_VNC_CONFIG_CALLBACK_DISABLE
)) {
3833 for (n
= rn
; n
; n
= agg_node_parent(n
)) {
3834 // rfapiDoRouteCallback(import_table, n, NULL);
3836 rfapiMonitorItNodeChanged(import_table
, rn
, NULL
);
3838 RFAPI_CHECK_REFCOUNT(rn
, SAFI_MPLS_VPN
, 0);
3842 static void rfapiBgpInfoFilteredImportBadSafi(
3843 struct rfapi_import_table
*import_table
, int action
, struct peer
*peer
,
3844 void *rfd
, /* set for looped back routes */
3846 struct prefix
*aux_prefix
, /* AFI_L2VPN: optional IP */
3847 afi_t afi
, struct prefix_rd
*prd
,
3848 struct attr
*attr
, /* part of bgp_path_info */
3849 uint8_t type
, /* part of bgp_path_info */
3850 uint8_t sub_type
, /* part of bgp_path_info */
3851 uint32_t *label
) /* part of bgp_path_info */
3853 vnc_zlog_debug_verbose("%s: Error, bad safi", __func__
);
3856 static rfapi_bi_filtered_import_f
*
3857 rfapiBgpInfoFilteredImportFunction(safi_t safi
)
3861 return rfapiBgpInfoFilteredImportVPN
;
3864 return rfapiBgpInfoFilteredImportEncap
;
3868 flog_err(EC_LIB_DEVELOPMENT
, "%s: bad safi %d", __func__
, safi
);
3869 return rfapiBgpInfoFilteredImportBadSafi
;
3873 void rfapiProcessUpdate(struct peer
*peer
,
3874 void *rfd
, /* set when looped from RFP/RFAPI */
3875 struct prefix
*p
, struct prefix_rd
*prd
,
3876 struct attr
*attr
, afi_t afi
, safi_t safi
, uint8_t type
,
3877 uint8_t sub_type
, uint32_t *label
)
3881 struct rfapi_import_table
*it
;
3882 int has_ip_route
= 1;
3885 bgp
= bgp_get_default(); /* assume 1 instance for now */
3892 * look at high-order byte of RD. FF means MAC
3893 * address is present (VNC L2VPN)
3895 if ((safi
== SAFI_MPLS_VPN
)
3896 && (decode_rd_type(prd
->val
) == RD_TYPE_VNC_ETH
)) {
3897 struct prefix pfx_mac_buf
;
3898 struct prefix pfx_nexthop_buf
;
3902 * Set flag if prefix and nexthop are the same - don't
3903 * add the route to normal IP-based import tables
3905 if (!rfapiGetNexthop(attr
, &pfx_nexthop_buf
)) {
3906 if (!prefix_cmp(&pfx_nexthop_buf
, p
)) {
3911 memset(&pfx_mac_buf
, 0, sizeof(pfx_mac_buf
));
3912 pfx_mac_buf
.family
= AF_ETHERNET
;
3913 pfx_mac_buf
.prefixlen
= 48;
3914 memcpy(&pfx_mac_buf
.u
.prefix_eth
.octet
, prd
->val
+ 2, 6);
3917 * Find rt containing LNI (Logical Network ID), which
3918 * _should_ always be present when mac address is present
3920 rc
= rfapiEcommunityGetLNI(attr
->ecommunity
, &lni
);
3922 vnc_zlog_debug_verbose(
3923 "%s: rfapiEcommunityGetLNI returned %d, lni=%d, attr=%p",
3924 __func__
, rc
, lni
, attr
);
3926 it
= rfapiMacImportTableGet(bgp
, lni
);
3928 rfapiBgpInfoFilteredImportVPN(
3929 it
, FIF_ACTION_UPDATE
, peer
, rfd
,
3930 &pfx_mac_buf
, /* prefix */
3931 p
, /* aux prefix: IP addr */
3932 AFI_L2VPN
, prd
, attr
, type
, sub_type
, label
);
3940 * Iterate over all import tables; do a filtered import
3941 * for the afi/safi combination
3943 for (it
= h
->imports
; it
; it
= it
->next
) {
3944 (*rfapiBgpInfoFilteredImportFunction(safi
))(
3945 it
, FIF_ACTION_UPDATE
, peer
, rfd
, p
, /* prefix */
3946 NULL
, afi
, prd
, attr
, type
, sub_type
, label
);
3949 if (safi
== SAFI_MPLS_VPN
) {
3950 vnc_direct_bgp_rh_add_route(bgp
, afi
, p
, peer
, attr
);
3951 rfapiBgpInfoFilteredImportVPN(
3952 bgp
->rfapi
->it_ce
, FIF_ACTION_UPDATE
, peer
, rfd
,
3954 NULL
, afi
, prd
, attr
, type
, sub_type
, label
);
3959 void rfapiProcessWithdraw(struct peer
*peer
, void *rfd
, struct prefix
*p
,
3960 struct prefix_rd
*prd
, struct attr
*attr
, afi_t afi
,
3961 safi_t safi
, uint8_t type
, int kill
)
3965 struct rfapi_import_table
*it
;
3967 bgp
= bgp_get_default(); /* assume 1 instance for now */
3974 * look at high-order byte of RD. FF means MAC
3975 * address is present (VNC L2VPN)
3977 if (h
->import_mac
!= NULL
&& safi
== SAFI_MPLS_VPN
3978 && decode_rd_type(prd
->val
) == RD_TYPE_VNC_ETH
) {
3979 struct prefix pfx_mac_buf
;
3980 void *cursor
= NULL
;
3983 memset(&pfx_mac_buf
, 0, sizeof(pfx_mac_buf
));
3984 pfx_mac_buf
.family
= AF_ETHERNET
;
3985 pfx_mac_buf
.prefixlen
= 48;
3986 memcpy(&pfx_mac_buf
.u
.prefix_eth
, prd
->val
+ 2, 6);
3989 * withdraw does not contain attrs, so we don't have
3990 * access to the route's LNI, which would ordinarily
3991 * select the specific mac-based import table. Instead,
3992 * we must iterate over all mac-based tables and rely
3993 * on the RD to match.
3995 * If this approach is too slow, add an index where
3996 * key is {RD, peer} and value is the import table
3998 for (rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4000 rc
== 0; rc
= skiplist_next(h
->import_mac
, NULL
,
4001 (void **)&it
, &cursor
)) {
4004 vnc_zlog_debug_verbose(
4005 "%s: calling rfapiBgpInfoFilteredImportVPN(it=%p, afi=AFI_L2VPN)",
4009 rfapiBgpInfoFilteredImportVPN(
4011 (kill
? FIF_ACTION_KILL
: FIF_ACTION_WITHDRAW
),
4012 peer
, rfd
, &pfx_mac_buf
, /* prefix */
4013 p
, /* aux_prefix: IP */
4014 AFI_L2VPN
, prd
, attr
, type
, 0,
4015 NULL
); /* sub_type & label unused for withdraw
4021 * XXX For the case where the withdraw involves an L2
4022 * route with no IP information, we rely on the lack
4023 * of RT-list intersection to filter out the withdraw
4024 * from the IP-based import tables below
4028 * Iterate over all import tables; do a filtered import
4029 * for the afi/safi combination
4032 for (it
= h
->imports
; it
; it
= it
->next
) {
4033 (*rfapiBgpInfoFilteredImportFunction(safi
))(
4034 it
, (kill
? FIF_ACTION_KILL
: FIF_ACTION_WITHDRAW
),
4035 peer
, rfd
, p
, /* prefix */
4036 NULL
, afi
, prd
, attr
, type
, 0,
4037 NULL
); /* sub_type & label unused for withdraw */
4040 /* TBD the deletion should happen after the lifetime expires */
4041 if (safi
== SAFI_MPLS_VPN
)
4042 vnc_direct_bgp_rh_del_route(bgp
, afi
, p
, peer
);
4044 if (safi
== SAFI_MPLS_VPN
) {
4045 rfapiBgpInfoFilteredImportVPN(
4047 (kill
? FIF_ACTION_KILL
: FIF_ACTION_WITHDRAW
), peer
,
4048 rfd
, p
, /* prefix */
4049 NULL
, afi
, prd
, attr
, type
, 0,
4050 NULL
); /* sub_type & label unused for withdraw */
4055 * TBD optimized withdraw timer algorithm for case of many
4056 * routes expiring at the same time due to peer drop.
4059 * 1. Visit all BPIs in all ENCAP import tables.
4061 * a. If a bpi's peer is the failed peer, remove the bpi.
4062 * b. If the removed ENCAP bpi was first in the list of
4063 * BPIs at this ENCAP node, loop over all monitors
4066 * (1) for each ENCAP monitor, loop over all its
4067 * VPN node monitors and set their RFAPI_MON_FLAG_NEEDCALLBACK
4070 * 2. Visit all BPIs in all VPN import tables.
4071 * a. If a bpi's peer is the failed peer, remove the bpi.
4072 * b. loop over all the VPN node monitors and set their
4073 * RFAPI_MON_FLAG_NEEDCALLBACK flags
4074 * c. If there are no BPIs left at this VPN node,
4079 /* surprise, this gets called from peer_delete(), from rfapi_close() */
4080 static void rfapiProcessPeerDownRt(struct peer
*peer
,
4081 struct rfapi_import_table
*import_table
,
4082 afi_t afi
, safi_t safi
)
4084 struct agg_node
*rn
;
4085 struct bgp_path_info
*bpi
;
4086 struct agg_table
*rt
;
4087 int (*timer_service_func
)(struct thread
*);
4089 assert(afi
== AFI_IP
|| afi
== AFI_IP6
);
4095 rt
= import_table
->imported_vpn
[afi
];
4096 timer_service_func
= rfapiWithdrawTimerVPN
;
4099 rt
= import_table
->imported_encap
[afi
];
4100 timer_service_func
= rfapiWithdrawTimerEncap
;
4103 /* Suppress uninitialized variable warning */
4105 timer_service_func
= NULL
;
4110 for (rn
= agg_route_top(rt
); rn
; rn
= agg_route_next(rn
)) {
4111 for (bpi
= rn
->info
; bpi
; bpi
= bpi
->next
) {
4112 if (bpi
->peer
== peer
) {
4114 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
)) {
4115 /* already in holddown, skip */
4119 if (safi
== SAFI_MPLS_VPN
) {
4120 RFAPI_UPDATE_ITABLE_COUNT(
4121 bpi
, import_table
, afi
, -1);
4122 import_table
->holddown_count
[afi
] += 1;
4124 rfapiBiStartWithdrawTimer(import_table
, rn
, bpi
,
4126 timer_service_func
);
4134 * This gets called when a peer connection drops. We have to remove
4135 * all the routes from this peer.
4137 * Current approach is crude. TBD Optimize by setting fewer timers and
4138 * grouping withdrawn routes so we can generate callbacks more
4141 void rfapiProcessPeerDown(struct peer
*peer
)
4145 struct rfapi_import_table
*it
;
4148 * If this peer is a "dummy" peer structure atached to a RFAPI
4149 * nve_descriptor, we don't need to walk the import tables
4150 * because the routes are already withdrawn by rfapi_close()
4152 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_IS_RFAPI_HD
))
4156 * 1. Visit all BPIs in all ENCAP import tables.
4157 * Start withdraw timer on the BPIs that match peer.
4159 * 2. Visit All BPIs in all VPN import tables.
4160 * Start withdraw timer on the BPIs that match peer.
4163 bgp
= bgp_get_default(); /* assume 1 instance for now */
4170 for (it
= h
->imports
; it
; it
= it
->next
) {
4171 rfapiProcessPeerDownRt(peer
, it
, AFI_IP
, SAFI_ENCAP
);
4172 rfapiProcessPeerDownRt(peer
, it
, AFI_IP6
, SAFI_ENCAP
);
4173 rfapiProcessPeerDownRt(peer
, it
, AFI_IP
, SAFI_MPLS_VPN
);
4174 rfapiProcessPeerDownRt(peer
, it
, AFI_IP6
, SAFI_MPLS_VPN
);
4178 rfapiProcessPeerDownRt(peer
, h
->it_ce
, AFI_IP
, SAFI_MPLS_VPN
);
4179 rfapiProcessPeerDownRt(peer
, h
->it_ce
, AFI_IP6
, SAFI_MPLS_VPN
);
4184 * Import an entire RIB (for an afi/safi) to an import table RIB,
4185 * filtered according to the import table's RT list
4187 * TBD: does this function need additions to match rfapiProcessUpdate()
4188 * for, e.g., L2 handling?
4190 static void rfapiBgpTableFilteredImport(struct bgp
*bgp
,
4191 struct rfapi_import_table
*it
,
4192 afi_t afi
, safi_t safi
)
4194 struct bgp_node
*rn1
;
4195 struct bgp_node
*rn2
;
4197 /* Only these SAFIs have 2-level RIBS */
4198 assert(safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
);
4201 * Now visit all the rd nodes and the nodes of all the
4202 * route tables attached to them, and import the routes
4203 * if they have matching route targets
4205 for (rn1
= bgp_table_top(bgp
->rib
[afi
][safi
]); rn1
;
4206 rn1
= bgp_route_next(rn1
)) {
4208 if (bgp_node_has_bgp_path_info_data(rn1
)) {
4210 for (rn2
= bgp_table_top(bgp_node_get_bgp_table_info(rn1
)); rn2
;
4211 rn2
= bgp_route_next(rn2
)) {
4213 struct bgp_path_info
*bpi
;
4215 for (bpi
= bgp_node_get_bgp_path_info(rn2
);
4216 bpi
; bpi
= bpi
->next
) {
4219 if (CHECK_FLAG(bpi
->flags
,
4224 label
= decode_label(
4225 &bpi
->extra
->label
[0]);
4226 (*rfapiBgpInfoFilteredImportFunction(
4228 it
, /* which import table */
4229 FIF_ACTION_UPDATE
, bpi
->peer
,
4230 NULL
, &rn2
->p
, /* prefix */
4232 (struct prefix_rd
*)&rn1
->p
,
4233 bpi
->attr
, bpi
->type
,
4234 bpi
->sub_type
, &label
);
4242 /* per-bgp-instance rfapi data */
4243 struct rfapi
*bgp_rfapi_new(struct bgp
*bgp
)
4247 struct rfapi_rfp_cfg
*cfg
= NULL
;
4248 struct rfapi_rfp_cb_methods
*cbm
= NULL
;
4250 assert(bgp
->rfapi_cfg
== NULL
);
4252 h
= XCALLOC(MTYPE_RFAPI
, sizeof(struct rfapi
));
4254 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
4255 h
->un
[afi
] = agg_table_init();
4259 * initialize the ce import table
4261 h
->it_ce
= XCALLOC(MTYPE_RFAPI_IMPORTTABLE
,
4262 sizeof(struct rfapi_import_table
));
4263 h
->it_ce
->imported_vpn
[AFI_IP
] = agg_table_init();
4264 h
->it_ce
->imported_vpn
[AFI_IP6
] = agg_table_init();
4265 h
->it_ce
->imported_encap
[AFI_IP
] = agg_table_init();
4266 h
->it_ce
->imported_encap
[AFI_IP6
] = agg_table_init();
4267 rfapiBgpTableFilteredImport(bgp
, h
->it_ce
, AFI_IP
, SAFI_MPLS_VPN
);
4268 rfapiBgpTableFilteredImport(bgp
, h
->it_ce
, AFI_IP6
, SAFI_MPLS_VPN
);
4271 * Set up work queue for deferred rfapi_close operations
4273 h
->deferred_close_q
=
4274 work_queue_new(bm
->master
, "rfapi deferred close");
4275 h
->deferred_close_q
->spec
.workfunc
= rfapi_deferred_close_workfunc
;
4276 h
->deferred_close_q
->spec
.data
= h
;
4278 h
->rfp
= rfp_start(bm
->master
, &cfg
, &cbm
);
4279 bgp
->rfapi_cfg
= bgp_rfapi_cfg_new(cfg
);
4281 h
->rfp_methods
= *cbm
;
4286 void bgp_rfapi_destroy(struct bgp
*bgp
, struct rfapi
*h
)
4290 if (bgp
== NULL
|| h
== NULL
)
4293 if (h
->resolve_nve_nexthop
) {
4294 skiplist_free(h
->resolve_nve_nexthop
);
4295 h
->resolve_nve_nexthop
= NULL
;
4298 agg_table_finish(h
->it_ce
->imported_vpn
[AFI_IP
]);
4299 agg_table_finish(h
->it_ce
->imported_vpn
[AFI_IP6
]);
4300 agg_table_finish(h
->it_ce
->imported_encap
[AFI_IP
]);
4301 agg_table_finish(h
->it_ce
->imported_encap
[AFI_IP6
]);
4303 if (h
->import_mac
) {
4304 struct rfapi_import_table
*it
;
4309 rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4311 !rc
; rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4314 rfapiImportTableFlush(it
);
4315 XFREE(MTYPE_RFAPI_IMPORTTABLE
, it
);
4317 skiplist_free(h
->import_mac
);
4318 h
->import_mac
= NULL
;
4321 work_queue_free_and_null(&h
->deferred_close_q
);
4326 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
4327 agg_table_finish(h
->un
[afi
]);
4330 XFREE(MTYPE_RFAPI_IMPORTTABLE
, h
->it_ce
);
4331 XFREE(MTYPE_RFAPI
, h
);
4334 struct rfapi_import_table
*
4335 rfapiImportTableRefAdd(struct bgp
*bgp
, struct ecommunity
*rt_import_list
,
4336 struct rfapi_nve_group_cfg
*rfg
)
4339 struct rfapi_import_table
*it
;
4345 for (it
= h
->imports
; it
; it
= it
->next
) {
4346 if (ecommunity_cmp(it
->rt_import_list
, rt_import_list
))
4350 vnc_zlog_debug_verbose("%s: matched it=%p", __func__
, it
);
4353 it
= XCALLOC(MTYPE_RFAPI_IMPORTTABLE
,
4354 sizeof(struct rfapi_import_table
));
4356 it
->next
= h
->imports
;
4359 it
->rt_import_list
= ecommunity_dup(rt_import_list
);
4361 it
->monitor_exterior_orphans
=
4362 skiplist_new(0, NULL
, prefix_free_lists
);
4365 * fill import route tables from RIBs
4367 * Potential area for optimization. If this occurs when
4368 * tables are large (e.g., the operator adds a nve group
4369 * with a new RT list to a running system), it could take
4373 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
4375 it
->imported_vpn
[afi
] = agg_table_init();
4376 it
->imported_encap
[afi
] = agg_table_init();
4378 rfapiBgpTableFilteredImport(bgp
, it
, afi
,
4380 rfapiBgpTableFilteredImport(bgp
, it
, afi
, SAFI_ENCAP
);
4382 vnc_import_bgp_exterior_redist_enable_it(bgp
, afi
, it
);
4392 * skiplist element free function
4394 static void delete_rem_pfx_na_free(void *na
)
4396 uint32_t *pCounter
= ((struct rfapi_nve_addr
*)na
)->info
;
4399 XFREE(MTYPE_RFAPI_NVE_ADDR
, na
);
4403 * Common deleter for IP and MAC import tables
4405 static void rfapiDeleteRemotePrefixesIt(
4406 struct bgp
*bgp
, struct rfapi_import_table
*it
, struct prefix
*un
,
4407 struct prefix
*vn
, struct prefix
*p
, int delete_active
,
4408 int delete_holddown
, uint32_t *pARcount
, uint32_t *pAHcount
,
4409 uint32_t *pHRcount
, uint32_t *pHHcount
,
4410 struct skiplist
*uniq_active_nves
, struct skiplist
*uniq_holddown_nves
)
4416 char buf_pfx
[PREFIX_STRLEN
];
4419 prefix2str(p
, buf_pfx
, sizeof(buf_pfx
));
4425 vnc_zlog_debug_verbose(
4426 "%s: entry, p=%s, delete_active=%d, delete_holddown=%d",
4427 __func__
, buf_pfx
, delete_active
, delete_holddown
);
4431 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
4433 struct agg_table
*rt
;
4434 struct agg_node
*rn
;
4436 if (p
&& (family2afi(p
->family
) != afi
)) {
4440 rt
= it
->imported_vpn
[afi
];
4444 vnc_zlog_debug_verbose("%s: scanning rt for afi=%d", __func__
,
4447 for (rn
= agg_route_top(rt
); rn
; rn
= agg_route_next(rn
)) {
4448 struct bgp_path_info
*bpi
;
4449 struct bgp_path_info
*next
;
4451 if (p
&& VNC_DEBUG(IMPORT_DEL_REMOTE
)) {
4452 char p1line
[PREFIX_STRLEN
];
4453 char p2line
[PREFIX_STRLEN
];
4455 prefix2str(p
, p1line
, sizeof(p1line
));
4456 prefix2str(&rn
->p
, p2line
, sizeof(p2line
));
4457 vnc_zlog_debug_any("%s: want %s, have %s",
4458 __func__
, p1line
, p2line
);
4461 if (p
&& prefix_cmp(p
, &rn
->p
))
4465 char buf_pfx
[PREFIX_STRLEN
];
4467 prefix2str(&rn
->p
, buf_pfx
, sizeof(buf_pfx
));
4468 vnc_zlog_debug_verbose("%s: rn pfx=%s",
4472 /* TBD is this valid for afi == AFI_L2VPN? */
4473 RFAPI_CHECK_REFCOUNT(rn
, SAFI_MPLS_VPN
, 1);
4475 for (bpi
= rn
->info
; bpi
; bpi
= next
) {
4484 vnc_zlog_debug_verbose("%s: examining bpi %p",
4487 if (!rfapiGetNexthop(bpi
->attr
, &qpt
))
4492 || !prefix_match(vn
, &qpt
)) {
4494 vnc_zlog_debug_verbose(
4495 "%s: continue at vn && !qpt_valid || !prefix_match(vn, &qpt)",
4502 if (!rfapiGetUnAddrOfVpnBi(bpi
, &qct
))
4507 || !prefix_match(un
, &qct
)) {
4509 vnc_zlog_debug_verbose(
4510 "%s: continue at un && !qct_valid || !prefix_match(un, &qct)",
4522 * If this route is waiting to be deleted
4524 * a previous withdraw, we must cancel its
4527 if (CHECK_FLAG(bpi
->flags
, BGP_PATH_REMOVED
)) {
4528 if (!delete_holddown
)
4530 if (bpi
->extra
->vnc
.import
.timer
) {
4533 (struct thread
*)bpi
4536 struct rfapi_withdraw
*wcb
=
4540 ->holddown_count
[afi
] -=
4542 RFAPI_UPDATE_ITABLE_COUNT(
4543 bpi
, wcb
->import_table
,
4545 XFREE(MTYPE_RFAPI_WITHDRAW
,
4555 vnc_zlog_debug_verbose(
4556 "%s: deleting bpi %p (qct_valid=%d, qpt_valid=%d, delete_holddown=%d, delete_active=%d)",
4557 __func__
, bpi
, qct_valid
, qpt_valid
,
4558 delete_holddown
, delete_active
);
4564 if (qct_valid
&& qpt_valid
) {
4566 struct rfapi_nve_addr na
;
4567 struct rfapi_nve_addr
*nap
;
4569 memset(&na
, 0, sizeof(na
));
4570 assert(!rfapiQprefix2Raddr(&qct
,
4572 assert(!rfapiQprefix2Raddr(&qpt
,
4575 if (skiplist_search(
4578 : uniq_holddown_nves
),
4579 &na
, (void **)&nap
)) {
4583 MTYPE_RFAPI_NVE_ADDR
,
4588 nap
->info
= is_active
4594 : uniq_holddown_nves
),
4597 rfapiNveAddr2Str(nap
, line
,
4602 vnc_direct_bgp_rh_del_route(bgp
, afi
, &rn
->p
,
4605 RFAPI_UPDATE_ITABLE_COUNT(bpi
, it
, afi
, -1);
4606 it
->holddown_count
[afi
] += 1;
4607 rfapiExpireVpnNow(it
, rn
, bpi
, 1);
4609 vnc_zlog_debug_verbose(
4610 "%s: incrementing count (is_active=%d)",
4611 __func__
, is_active
);
4624 * For use by the "clear vnc prefixes" command
4626 /*------------------------------------------
4627 * rfapiDeleteRemotePrefixes
4629 * UI helper: For use by the "clear vnc prefixes" command
4632 * un if set, tunnel must match this prefix
4633 * vn if set, nexthop prefix must match this prefix
4634 * p if set, prefix must match this prefix
4635 * it if set, only look in this import table
4638 * pARcount number of active routes deleted
4639 * pAHcount number of active nves deleted
4640 * pHRcount number of holddown routes deleted
4641 * pHHcount number of holddown nves deleted
4645 --------------------------------------------*/
4646 void rfapiDeleteRemotePrefixes(struct prefix
*un
, struct prefix
*vn
,
4648 struct rfapi_import_table
*arg_it
,
4649 int delete_active
, int delete_holddown
,
4650 uint32_t *pARcount
, uint32_t *pAHcount
,
4651 uint32_t *pHRcount
, uint32_t *pHHcount
)
4655 struct rfapi_import_table
*it
;
4656 uint32_t deleted_holddown_route_count
= 0;
4657 uint32_t deleted_active_route_count
= 0;
4658 uint32_t deleted_holddown_nve_count
= 0;
4659 uint32_t deleted_active_nve_count
= 0;
4660 struct skiplist
*uniq_holddown_nves
;
4661 struct skiplist
*uniq_active_nves
;
4665 bgp
= bgp_get_default(); /* assume 1 instance for now */
4666 /* If no bgp instantiated yet, no vnc prefixes exist */
4673 uniq_holddown_nves
=
4674 skiplist_new(0, rfapi_nve_addr_cmp
, delete_rem_pfx_na_free
);
4676 skiplist_new(0, rfapi_nve_addr_cmp
, delete_rem_pfx_na_free
);
4679 * Iterate over all import tables; do a filtered import
4680 * for the afi/safi combination
4689 vnc_zlog_debug_verbose(
4690 "%s: calling rfapiDeleteRemotePrefixesIt() on (IP) import %p",
4693 rfapiDeleteRemotePrefixesIt(
4694 bgp
, it
, un
, vn
, p
, delete_active
, delete_holddown
,
4695 &deleted_active_route_count
, &deleted_active_nve_count
,
4696 &deleted_holddown_route_count
,
4697 &deleted_holddown_nve_count
, uniq_active_nves
,
4698 uniq_holddown_nves
);
4707 * Now iterate over L2 import tables
4709 if (h
->import_mac
&& !(p
&& (p
->family
!= AF_ETHERNET
))) {
4711 void *cursor
= NULL
;
4715 rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4717 !rc
; rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4720 vnc_zlog_debug_verbose(
4721 "%s: calling rfapiDeleteRemotePrefixesIt() on import_mac %p",
4724 rfapiDeleteRemotePrefixesIt(
4725 bgp
, it
, un
, vn
, p
, delete_active
,
4726 delete_holddown
, &deleted_active_route_count
,
4727 &deleted_active_nve_count
,
4728 &deleted_holddown_route_count
,
4729 &deleted_holddown_nve_count
, uniq_active_nves
,
4730 uniq_holddown_nves
);
4735 * our custom element freeing function above counts as it deletes
4737 skiplist_free(uniq_holddown_nves
);
4738 skiplist_free(uniq_active_nves
);
4741 *pARcount
= deleted_active_route_count
;
4743 *pAHcount
= deleted_active_nve_count
;
4745 *pHRcount
= deleted_holddown_route_count
;
4747 *pHHcount
= deleted_holddown_nve_count
;
4752 /*------------------------------------------
4753 * rfapiCountRemoteRoutes
4755 * UI helper: count VRF routes from BGP side
4760 * pALRcount count of active local routes
4761 * pARRcount count of active remote routes
4762 * pHRcount count of holddown routes
4763 * pIRcount count of direct imported routes
4767 --------------------------------------------*/
4768 void rfapiCountAllItRoutes(int *pALRcount
, /* active local routes */
4769 int *pARRcount
, /* active remote routes */
4770 int *pHRcount
, /* holddown routes */
4771 int *pIRcount
) /* imported routes */
4775 struct rfapi_import_table
*it
;
4778 int total_active_local
= 0;
4779 int total_active_remote
= 0;
4780 int total_holddown
= 0;
4781 int total_imported
= 0;
4783 bgp
= bgp_get_default(); /* assume 1 instance for now */
4790 * Iterate over all import tables; do a filtered import
4791 * for the afi/safi combination
4794 for (it
= h
->imports
; it
; it
= it
->next
) {
4796 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
4798 total_active_local
+= it
->local_count
[afi
];
4799 total_active_remote
+= it
->remote_count
[afi
];
4800 total_holddown
+= it
->holddown_count
[afi
];
4801 total_imported
+= it
->imported_count
[afi
];
4808 if (h
->import_mac
) {
4810 rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4812 !rc
; rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4815 total_active_local
+= it
->local_count
[AFI_L2VPN
];
4816 total_active_remote
+= it
->remote_count
[AFI_L2VPN
];
4817 total_holddown
+= it
->holddown_count
[AFI_L2VPN
];
4818 total_imported
+= it
->imported_count
[AFI_L2VPN
];
4824 *pALRcount
= total_active_local
;
4827 *pARRcount
= total_active_remote
;
4830 *pHRcount
= total_holddown
;
4833 *pIRcount
= total_imported
;
4837 /*------------------------------------------
4838 * rfapiGetHolddownFromLifetime
4840 * calculate holddown value based on lifetime
4846 * Holddown value based on lifetime, holddown_factor,
4847 * and RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY
4849 --------------------------------------------*/
4850 /* hold down time maxes out at RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY */
4851 uint32_t rfapiGetHolddownFromLifetime(uint32_t lifetime
)
4856 bgp
= bgp_get_default();
4857 if (bgp
&& bgp
->rfapi_cfg
)
4858 factor
= bgp
->rfapi_cfg
->rfp_cfg
.holddown_factor
;
4860 factor
= RFAPI_RFP_CFG_DEFAULT_HOLDDOWN_FACTOR
;
4862 if (factor
< 100 || lifetime
< RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY
)
4863 lifetime
= lifetime
* factor
/ 100;
4864 if (lifetime
< RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY
)
4867 return RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY
;