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()
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
*bi
;
154 struct bgp_path_info
*next
;
156 for (bi
= rn
->info
; bi
; bi
= next
) {
159 if (CHECK_FLAG(bi
->flags
,
164 if (RFAPI_LOCAL_BI(bi
)) {
167 if (RFAPI_DIRECT_IMPORT_BI(
178 if (it
->holddown_count
[afi
] != holddown_count
) {
179 vnc_zlog_debug_verbose(
180 "%s: it->holddown_count %d != holddown_count %d",
181 __func__
, it
->holddown_count
[afi
],
185 if (it
->remote_count
[afi
] != remote_count
) {
186 vnc_zlog_debug_verbose(
187 "%s: it->remote_count %d != remote_count %d",
188 __func__
, it
->remote_count
[afi
],
192 if (it
->imported_count
[afi
] != imported_count
) {
193 vnc_zlog_debug_verbose(
194 "%s: it->imported_count %d != imported_count %d",
195 __func__
, it
->imported_count
[afi
],
203 #if DEBUG_ROUTE_COUNTERS
204 #define VNC_ITRCCK do {rfapiCheckRouteCount();} while (0)
210 * Validate reference count for a node in an import table
212 * Normally lockoffset is 0 for nodes in quiescent state. However,
213 * agg_unlock_node will delete the node if it is called when
214 * node->lock == 1, and we have to validate the refcount before
215 * the node is deleted. In this case, we specify lockoffset 1.
217 void rfapiCheckRefcount(struct agg_node
*rn
, safi_t safi
, int lockoffset
)
219 unsigned int count_bi
= 0;
220 unsigned int count_monitor
= 0;
221 struct bgp_path_info
*bi
;
222 struct rfapi_monitor_encap
*hme
;
223 struct rfapi_monitor_vpn
*hmv
;
225 for (bi
= rn
->info
; bi
; bi
= bi
->next
)
230 ++count_monitor
; /* rfapi_it_extra */
237 for (hme
= RFAPI_MONITOR_ENCAP(rn
); hme
;
244 for (hmv
= RFAPI_MONITOR_VPN(rn
); hmv
; hmv
= hmv
->next
)
247 if (RFAPI_MONITOR_EXTERIOR(rn
)->source
) {
248 ++count_monitor
; /* sl */
250 for (rc
= skiplist_next(
251 RFAPI_MONITOR_EXTERIOR(rn
)->source
,
252 NULL
, NULL
, &cursor
);
255 RFAPI_MONITOR_EXTERIOR(rn
)->source
,
256 NULL
, NULL
, &cursor
)) {
258 ++count_monitor
; /* sl entry */
268 if (count_bi
+ count_monitor
+ lockoffset
!= rn
->lock
) {
269 vnc_zlog_debug_verbose(
270 "%s: count_bi=%d, count_monitor=%d, lockoffset=%d, rn->lock=%d",
271 __func__
, count_bi
, count_monitor
, lockoffset
,
278 * Perform deferred rfapi_close operations that were queued
281 static wq_item_status
rfapi_deferred_close_workfunc(struct work_queue
*q
,
284 struct rfapi_descriptor
*rfd
= data
;
285 struct rfapi
*h
= q
->spec
.data
;
287 assert(!(h
->flags
& RFAPI_INCALLBACK
));
289 vnc_zlog_debug_verbose("%s: completed deferred close on handle %p",
295 * Extract layer 2 option from Encap TLVS in BGP attrs
297 int rfapiGetL2o(struct attr
*attr
, struct rfapi_l2address_option
*l2o
)
301 struct bgp_attr_encap_subtlv
*pEncap
;
303 for (pEncap
= attr
->vnc_subtlvs
; pEncap
;
304 pEncap
= pEncap
->next
) {
306 if (pEncap
->type
== BGP_VNC_SUBTLV_TYPE_RFPOPTION
) {
308 == RFAPI_VN_OPTION_TYPE_L2ADDR
) {
310 if (pEncap
->value
[1] == 14) {
311 memcpy(l2o
->macaddr
.octet
,
328 l2o
->logical_net_id
=
331 + ((pEncap
->value
[14]
334 + ((pEncap
->value
[13]
349 * Extract the lifetime from the Tunnel Encap attribute of a route in
352 int rfapiGetVncLifetime(struct attr
*attr
, uint32_t *lifetime
)
354 struct bgp_attr_encap_subtlv
*pEncap
;
356 *lifetime
= RFAPI_INFINITE_LIFETIME
; /* default to infinite */
360 for (pEncap
= attr
->vnc_subtlvs
; pEncap
;
361 pEncap
= pEncap
->next
) {
364 == BGP_VNC_SUBTLV_TYPE_LIFETIME
) { /* lifetime */
365 if (pEncap
->length
== 4) {
366 memcpy(lifetime
, pEncap
->value
, 4);
367 *lifetime
= ntohl(*lifetime
);
378 * Extract the tunnel type from the extended community
380 int rfapiGetTunnelType(struct attr
*attr
, bgp_encap_types
*type
)
382 *type
= BGP_ENCAP_TYPE_MPLS
; /* default to MPLS */
383 if (attr
&& attr
->ecommunity
) {
384 struct ecommunity
*ecom
= attr
->ecommunity
;
387 for (i
= 0; i
< (ecom
->size
* ECOMMUNITY_SIZE
);
388 i
+= ECOMMUNITY_SIZE
) {
392 if (ep
[0] == ECOMMUNITY_ENCODE_OPAQUE
393 && ep
[1] == ECOMMUNITY_OPAQUE_SUBTYPE_ENCAP
) {
394 *type
= (ep
[6] << 8) + ep
[7];
405 * Look for UN address in Encap attribute
407 int rfapiGetVncTunnelUnAddr(struct attr
*attr
, struct prefix
*p
)
409 struct bgp_attr_encap_subtlv
*pEncap
;
410 bgp_encap_types tun_type
;
412 rfapiGetTunnelType(attr
, &tun_type
);
413 if (tun_type
== BGP_ENCAP_TYPE_MPLS
) {
416 /* MPLS carries UN address in next hop */
417 rfapiNexthop2Prefix(attr
, p
);
424 for (pEncap
= attr
->encap_subtlvs
; pEncap
;
425 pEncap
= pEncap
->next
) {
428 == BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT
) { /* un
431 switch (pEncap
->length
) {
436 memcpy(p
->u
.val
, pEncap
->value
,
443 p
->family
= AF_INET6
;
445 memcpy(p
->u
.val
, pEncap
->value
,
458 * Get UN address wherever it might be
460 int rfapiGetUnAddrOfVpnBi(struct bgp_path_info
*bi
, struct prefix
*p
)
462 /* If it's in this route's VNC attribute, we're done */
463 if (!rfapiGetVncTunnelUnAddr(bi
->attr
, p
))
466 * Otherwise, see if it's cached from a corresponding ENCAP SAFI
470 switch (bi
->extra
->vnc
.import
.un_family
) {
473 p
->family
= bi
->extra
->vnc
.import
.un_family
;
474 p
->u
.prefix4
= bi
->extra
->vnc
.import
.un
.addr4
;
480 p
->family
= bi
->extra
->vnc
.import
.un_family
;
481 p
->u
.prefix6
= bi
->extra
->vnc
.import
.un
.addr6
;
488 #if DEBUG_ENCAP_MONITOR
489 vnc_zlog_debug_verbose(
490 "%s: bi->extra->vnc.import.un_family is 0, no UN addr",
502 * Make a new bgp_info from gathered parameters
504 static struct bgp_path_info
*rfapiBgpInfoCreate(struct attr
*attr
,
505 struct peer
*peer
, void *rfd
,
506 struct prefix_rd
*prd
,
507 uint8_t type
, uint8_t sub_type
,
510 struct bgp_path_info
*new;
512 new = bgp_path_info_new();
517 new->attr
= bgp_attr_intern(attr
);
519 bgp_path_info_extra_get(new);
521 new->extra
->vnc
.import
.rd
= *prd
;
522 rfapi_time(&new->extra
->vnc
.import
.create_time
);
525 encode_label(*label
, &new->extra
->label
[0]);
527 new->sub_type
= sub_type
;
535 * Frees bgp_info as used in import tables (parts are not
536 * allocated exactly the way they are in the main RIBs)
538 static void rfapiBgpInfoFree(struct bgp_path_info
*goner
)
544 vnc_zlog_debug_verbose("%s: calling peer_unlock(%p), #%d",
545 __func__
, goner
->peer
,
547 peer_unlock(goner
->peer
);
551 bgp_attr_unintern(&goner
->attr
);
554 assert(!goner
->extra
->damp_info
); /* Not used in import tbls */
555 XFREE(MTYPE_BGP_ROUTE_EXTRA
, goner
->extra
);
558 XFREE(MTYPE_BGP_ROUTE
, goner
);
561 struct rfapi_import_table
*rfapiMacImportTableGetNoAlloc(struct bgp
*bgp
,
565 struct rfapi_import_table
*it
= NULL
;
566 uintptr_t lni_as_ptr
= lni
;
575 if (skiplist_search(h
->import_mac
, (void *)lni_as_ptr
, (void **)&it
))
581 struct rfapi_import_table
*rfapiMacImportTableGet(struct bgp
*bgp
, uint32_t lni
)
584 struct rfapi_import_table
*it
= NULL
;
585 uintptr_t lni_as_ptr
= lni
;
590 if (!h
->import_mac
) {
591 /* default cmp is good enough for LNI */
592 h
->import_mac
= skiplist_new(0, NULL
, NULL
);
595 if (skiplist_search(h
->import_mac
, (void *)lni_as_ptr
, (void **)&it
)) {
597 struct ecommunity
*enew
;
598 struct ecommunity_val eval
;
601 it
= XCALLOC(MTYPE_RFAPI_IMPORTTABLE
,
602 sizeof(struct rfapi_import_table
));
603 /* set RT list of new import table based on LNI */
604 memset((char *)&eval
, 0, sizeof(eval
));
605 eval
.val
[0] = 0; /* VNC L2VPN */
606 eval
.val
[1] = 2; /* VNC L2VPN */
607 eval
.val
[5] = (lni
>> 16) & 0xff;
608 eval
.val
[6] = (lni
>> 8) & 0xff;
609 eval
.val
[7] = (lni
>> 0) & 0xff;
611 enew
= ecommunity_new();
612 ecommunity_add_val(enew
, &eval
);
613 it
->rt_import_list
= enew
;
615 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
616 it
->imported_vpn
[afi
] = agg_table_init();
617 it
->imported_encap
[afi
] = agg_table_init();
620 it
->l2_logical_net_id
= lni
;
622 skiplist_insert(h
->import_mac
, (void *)lni_as_ptr
, it
);
630 * Implement MONITOR_MOVE_SHORTER(original_node) from
631 * RFAPI-Import-Event-Handling.txt
633 * Returns pointer to the list of moved monitors
635 static struct rfapi_monitor_vpn
*
636 rfapiMonitorMoveShorter(struct agg_node
*original_vpn_node
, int lockoffset
)
638 struct bgp_path_info
*bi
;
639 struct agg_node
*par
;
640 struct rfapi_monitor_vpn
*m
;
641 struct rfapi_monitor_vpn
*mlast
;
642 struct rfapi_monitor_vpn
*moved
;
644 int parent_already_refcounted
= 0;
646 RFAPI_CHECK_REFCOUNT(original_vpn_node
, SAFI_MPLS_VPN
, lockoffset
);
648 #if DEBUG_MONITOR_MOVE_SHORTER
650 char buf
[PREFIX_STRLEN
];
652 prefix2str(&original_vpn_node
->p
, buf
, sizeof(buf
));
653 vnc_zlog_debug_verbose("%s: called with node pfx=%s", __func__
,
659 * 1. If there is at least one bi (either regular route or
660 * route marked as withdrawn, with a pending timer) at
661 * original_node with a valid UN address, we're done. Return.
663 for (bi
= original_vpn_node
->info
; bi
; bi
= bi
->next
) {
666 if (!rfapiGetUnAddrOfVpnBi(bi
, &pfx
)) {
667 #if DEBUG_MONITOR_MOVE_SHORTER
668 vnc_zlog_debug_verbose(
669 "%s: have valid UN at original node, no change",
677 * 2. Travel up the tree (toward less-specific prefixes) from
678 * original_node to find the first node that has at least
679 * one route (even if it is only a withdrawn route) with a
680 * valid UN address. Call this node "Node P."
682 for (par
= agg_node_parent(original_vpn_node
); par
;
683 par
= agg_node_parent(par
)) {
684 for (bi
= par
->info
; bi
; bi
= bi
->next
) {
686 if (!rfapiGetUnAddrOfVpnBi(bi
, &pfx
)) {
695 RFAPI_CHECK_REFCOUNT(par
, SAFI_MPLS_VPN
, 0);
699 * If no less-specific routes, try to use the 0/0 node
702 /* this isn't necessarily 0/0 */
703 par
= agg_route_table_top(original_vpn_node
);
706 * If we got the top node but it wasn't 0/0,
709 if (par
&& par
->p
.prefixlen
) {
710 agg_unlock_node(par
); /* maybe free */
715 ++parent_already_refcounted
;
720 * Create 0/0 node if it isn't there
723 struct prefix pfx_default
;
725 memset(&pfx_default
, 0, sizeof(pfx_default
));
726 pfx_default
.family
= original_vpn_node
->p
.family
;
728 /* creates default node if none exists */
729 par
= agg_node_get(agg_get_table(original_vpn_node
),
731 ++parent_already_refcounted
;
735 * 3. Move each of the monitors found at original_node to Node P.
736 * These are "Moved Monitors."
741 * Attach at end so that the list pointer we return points
742 * only to the moved routes
744 for (m
= RFAPI_MONITOR_VPN(par
), mlast
= NULL
; m
;
745 mlast
= m
, m
= m
->next
)
749 moved
= mlast
->next
= RFAPI_MONITOR_VPN(original_vpn_node
);
751 moved
= RFAPI_MONITOR_VPN_W_ALLOC(par
) =
752 RFAPI_MONITOR_VPN(original_vpn_node
);
754 if (RFAPI_MONITOR_VPN(
755 original_vpn_node
)) /* check agg, so not allocated */
756 RFAPI_MONITOR_VPN_W_ALLOC(original_vpn_node
) = NULL
;
759 * update the node pointers on the monitors
761 for (m
= moved
; m
; m
= m
->next
) {
766 RFAPI_CHECK_REFCOUNT(par
, SAFI_MPLS_VPN
,
767 parent_already_refcounted
- movecount
);
768 while (movecount
> parent_already_refcounted
) {
770 ++parent_already_refcounted
;
772 while (movecount
< parent_already_refcounted
) {
773 /* unlikely, but code defensively */
774 agg_unlock_node(par
);
775 --parent_already_refcounted
;
777 RFAPI_CHECK_REFCOUNT(original_vpn_node
, SAFI_MPLS_VPN
,
778 movecount
+ lockoffset
);
779 while (movecount
--) {
780 agg_unlock_node(original_vpn_node
);
783 #if DEBUG_MONITOR_MOVE_SHORTER
785 char buf
[PREFIX_STRLEN
];
787 prefix2str(&par
->p
, buf
, sizeof(buf
));
788 vnc_zlog_debug_verbose("%s: moved to node pfx=%s", __func__
,
798 * Implement MONITOR_MOVE_LONGER(new_node) from
799 * RFAPI-Import-Event-Handling.txt
801 static void rfapiMonitorMoveLonger(struct agg_node
*new_vpn_node
)
803 struct rfapi_monitor_vpn
*monitor
;
804 struct rfapi_monitor_vpn
*mlast
;
805 struct bgp_path_info
*bi
;
806 struct agg_node
*par
;
808 RFAPI_CHECK_REFCOUNT(new_vpn_node
, SAFI_MPLS_VPN
, 0);
811 * Make sure we have at least one valid route at the new node
813 for (bi
= new_vpn_node
->info
; bi
; bi
= bi
->next
) {
815 if (!rfapiGetUnAddrOfVpnBi(bi
, &pfx
))
820 vnc_zlog_debug_verbose(
821 "%s: no valid routes at node %p, so not attempting moves",
822 __func__
, new_vpn_node
);
827 * Find first parent node that has monitors
829 for (par
= agg_node_parent(new_vpn_node
); par
;
830 par
= agg_node_parent(par
)) {
831 if (RFAPI_MONITOR_VPN(par
))
836 vnc_zlog_debug_verbose(
837 "%s: no parent nodes with monitors, done", __func__
);
842 * Check each of these monitors to see of their longest-match
843 * is now the updated node. Move any such monitors to the more-
844 * specific updated node
846 for (mlast
= NULL
, monitor
= RFAPI_MONITOR_VPN(par
); monitor
;) {
849 * If new longest match for monitor prefix is the new
850 * route's prefix, move monitor to new route's prefix
852 if (prefix_match(&new_vpn_node
->p
, &monitor
->p
)) {
855 mlast
->next
= monitor
->next
;
857 RFAPI_MONITOR_VPN_W_ALLOC(par
) = monitor
->next
;
862 monitor
->next
= RFAPI_MONITOR_VPN(new_vpn_node
);
863 RFAPI_MONITOR_VPN_W_ALLOC(new_vpn_node
) = monitor
;
864 monitor
->node
= new_vpn_node
;
866 agg_lock_node(new_vpn_node
); /* incr refcount */
868 monitor
= mlast
? mlast
->next
: RFAPI_MONITOR_VPN(par
);
870 RFAPI_CHECK_REFCOUNT(par
, SAFI_MPLS_VPN
, 1);
871 /* decr refcount after we're done with par as this might
873 agg_unlock_node(par
);
878 monitor
= monitor
->next
;
881 RFAPI_CHECK_REFCOUNT(new_vpn_node
, SAFI_MPLS_VPN
, 0);
885 static void rfapiBgpInfoChainFree(struct bgp_path_info
*bi
)
887 struct bgp_path_info
*next
;
892 * If there is a timer waiting to delete this bi, cancel
893 * the timer and delete immediately
895 if (CHECK_FLAG(bi
->flags
, BGP_PATH_REMOVED
)
896 && bi
->extra
->vnc
.import
.timer
) {
899 (struct thread
*)bi
->extra
->vnc
.import
.timer
;
900 struct rfapi_withdraw
*wcb
= t
->arg
;
902 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
908 rfapiBgpInfoFree(bi
);
913 static void rfapiImportTableFlush(struct rfapi_import_table
*it
)
920 ecommunity_free(&it
->rt_import_list
);
921 it
->rt_import_list
= NULL
;
923 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
927 for (rn
= agg_route_top(it
->imported_vpn
[afi
]); rn
;
928 rn
= agg_route_next(rn
)) {
930 * Each route_node has:
931 * aggregate: points to rfapi_it_extra with monitor
933 * info: points to chain of bgp_info
935 /* free bgp_info and its children */
936 rfapiBgpInfoChainFree(rn
->info
);
939 rfapiMonitorExtraFlush(SAFI_MPLS_VPN
, rn
);
942 for (rn
= agg_route_top(it
->imported_encap
[afi
]); rn
;
943 rn
= agg_route_next(rn
)) {
944 /* free bgp_info and its children */
945 rfapiBgpInfoChainFree(rn
->info
);
948 rfapiMonitorExtraFlush(SAFI_ENCAP
, rn
);
951 agg_table_finish(it
->imported_vpn
[afi
]);
952 agg_table_finish(it
->imported_encap
[afi
]);
954 if (it
->monitor_exterior_orphans
) {
955 skiplist_free(it
->monitor_exterior_orphans
);
959 void rfapiImportTableRefDelByIt(struct bgp
*bgp
,
960 struct rfapi_import_table
*it_target
)
963 struct rfapi_import_table
*it
;
964 struct rfapi_import_table
*prev
= NULL
;
971 for (it
= h
->imports
; it
; prev
= it
, it
= it
->next
) {
977 assert(it
->refcount
);
983 prev
->next
= it
->next
;
985 h
->imports
= it
->next
;
987 rfapiImportTableFlush(it
);
988 XFREE(MTYPE_RFAPI_IMPORTTABLE
, it
);
992 #if RFAPI_REQUIRE_ENCAP_BEEC
994 * Look for magic BGP Encapsulation Extended Community value
995 * Format in RFC 5512 Sect. 4.5
997 static int rfapiEcommunitiesMatchBeec(struct ecommunity
*ecom
,
998 bgp_encap_types type
)
1005 for (i
= 0; i
< (ecom
->size
* ECOMMUNITY_SIZE
); i
+= ECOMMUNITY_SIZE
) {
1011 if (ep
[0] == ECOMMUNITY_ENCODE_OPAQUE
1012 && ep
[1] == ECOMMUNITY_OPAQUE_SUBTYPE_ENCAP
1013 && ep
[6] == ((type
&& 0xff00) >> 8)
1014 && ep
[7] == (type
& 0xff)) {
1023 int rfapiEcommunitiesIntersect(struct ecommunity
*e1
, struct ecommunity
*e2
)
1032 s1
= ecommunity_ecom2str(e1
, ECOMMUNITY_FORMAT_DISPLAY
, 0);
1033 s2
= ecommunity_ecom2str(e2
, ECOMMUNITY_FORMAT_DISPLAY
, 0);
1034 vnc_zlog_debug_verbose("%s: e1[%s], e2[%s]", __func__
, s1
, s2
);
1035 XFREE(MTYPE_ECOMMUNITY_STR
, s1
);
1036 XFREE(MTYPE_ECOMMUNITY_STR
, s2
);
1039 for (i
= 0; i
< e1
->size
; ++i
) {
1040 for (j
= 0; j
< e2
->size
; ++j
) {
1041 if (!memcmp(e1
->val
+ (i
* ECOMMUNITY_SIZE
),
1042 e2
->val
+ (j
* ECOMMUNITY_SIZE
),
1052 int rfapiEcommunityGetLNI(struct ecommunity
*ecom
, uint32_t *lni
)
1056 for (i
= 0; i
< ecom
->size
; ++i
) {
1057 uint8_t *p
= ecom
->val
+ (i
* ECOMMUNITY_SIZE
);
1059 if ((*(p
+ 0) == 0x00) && (*(p
+ 1) == 0x02)) {
1061 *lni
= (*(p
+ 5) << 16) | (*(p
+ 6) << 8)
1070 int rfapiEcommunityGetEthernetTag(struct ecommunity
*ecom
, uint16_t *tag_id
)
1072 struct bgp
*bgp
= bgp_get_default();
1073 *tag_id
= 0; /* default to untagged */
1076 for (i
= 0; i
< ecom
->size
; ++i
) {
1079 uint8_t *p
= ecom
->val
+ (i
* ECOMMUNITY_SIZE
);
1081 /* High-order octet of type. */
1084 if (*p
++ == ECOMMUNITY_ROUTE_TARGET
) {
1085 if (encode
== ECOMMUNITY_ENCODE_AS4
) {
1086 p
= ptr_get_be32(p
, &as
);
1087 } else if (encode
== ECOMMUNITY_ENCODE_AS
) {
1090 p
+= 2; /* skip next two, tag/vid
1091 always in lowest bytes */
1093 if (as
== bgp
->as
) {
1094 *tag_id
= *p
++ << 8;
1104 static int rfapiVpnBiNhEqualsPt(struct bgp_path_info
*bi
,
1105 struct rfapi_ip_addr
*hpt
)
1112 family
= BGP_MP_NEXTHOP_FAMILY(bi
->attr
->mp_nexthop_len
);
1114 if (hpt
->addr_family
!= family
)
1119 if (bi
->attr
->mp_nexthop_global_in
.s_addr
1120 != hpt
->addr
.v4
.s_addr
)
1125 if (IPV6_ADDR_CMP(&bi
->attr
->mp_nexthop_global
, &hpt
->addr
.v6
))
1139 * Compare 2 VPN BIs. Return true if they have the same VN and UN addresses
1141 static int rfapiVpnBiSamePtUn(struct bgp_path_info
*bi1
,
1142 struct bgp_path_info
*bi2
)
1144 struct prefix pfx_un1
;
1145 struct prefix pfx_un2
;
1150 if (!bi1
->attr
|| !bi2
->attr
)
1154 * VN address comparisons
1157 if (BGP_MP_NEXTHOP_FAMILY(bi1
->attr
->mp_nexthop_len
)
1158 != BGP_MP_NEXTHOP_FAMILY(bi2
->attr
->mp_nexthop_len
)) {
1162 switch (BGP_MP_NEXTHOP_FAMILY(bi1
->attr
->mp_nexthop_len
)) {
1164 if (bi1
->attr
->mp_nexthop_global_in
.s_addr
1165 != bi2
->attr
->mp_nexthop_global_in
.s_addr
)
1170 if (IPV6_ADDR_CMP(&bi1
->attr
->mp_nexthop_global
,
1171 &bi2
->attr
->mp_nexthop_global
))
1181 * UN address comparisons
1183 if (rfapiGetVncTunnelUnAddr(bi1
->attr
, &pfx_un1
)) {
1185 pfx_un1
.family
= bi1
->extra
->vnc
.import
.un_family
;
1186 switch (bi1
->extra
->vnc
.import
.un_family
) {
1189 bi1
->extra
->vnc
.import
.un
.addr4
;
1193 bi1
->extra
->vnc
.import
.un
.addr6
;
1202 if (rfapiGetVncTunnelUnAddr(bi2
->attr
, &pfx_un2
)) {
1204 pfx_un2
.family
= bi2
->extra
->vnc
.import
.un_family
;
1205 switch (bi2
->extra
->vnc
.import
.un_family
) {
1208 bi2
->extra
->vnc
.import
.un
.addr4
;
1212 bi2
->extra
->vnc
.import
.un
.addr6
;
1221 if (!pfx_un1
.family
|| !pfx_un2
.family
)
1224 if (pfx_un1
.family
!= pfx_un2
.family
)
1227 switch (pfx_un1
.family
) {
1229 if (!IPV4_ADDR_SAME(&pfx_un1
.u
.prefix4
, &pfx_un2
.u
.prefix4
))
1233 if (!IPV6_ADDR_SAME(&pfx_un1
.u
.prefix6
, &pfx_un2
.u
.prefix6
))
1242 uint8_t rfapiRfpCost(struct attr
*attr
)
1244 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF
)) {
1245 if (attr
->local_pref
> 255) {
1248 return 255 - attr
->local_pref
;
1254 /*------------------------------------------
1257 * Find Layer 2 options in an option chain
1263 * l2o layer 2 options extracted
1267 * 1 no options found
1269 --------------------------------------------*/
1270 int rfapi_extract_l2o(
1271 struct bgp_tea_options
*pHop
, /* chain of options */
1272 struct rfapi_l2address_option
*l2o
) /* return extracted value */
1274 struct bgp_tea_options
*p
;
1276 for (p
= pHop
; p
; p
= p
->next
) {
1277 if ((p
->type
== RFAPI_VN_OPTION_TYPE_L2ADDR
)
1278 && (p
->length
>= 8)) {
1282 memcpy(&l2o
->macaddr
, v
, 6);
1284 l2o
->label
= ((v
[6] << 12) & 0xff000)
1285 + ((v
[7] << 4) & 0xff0)
1286 + ((v
[8] >> 4) & 0xf);
1288 l2o
->local_nve_id
= (uint8_t)v
[10];
1290 l2o
->logical_net_id
=
1291 (v
[11] << 16) + (v
[12] << 8) + (v
[13] << 0);
1299 static struct rfapi_next_hop_entry
*
1300 rfapiRouteInfo2NextHopEntry(struct rfapi_ip_prefix
*rprefix
,
1301 struct bgp_path_info
*bi
, /* route to encode */
1302 uint32_t lifetime
, /* use this in nhe */
1303 struct agg_node
*rn
) /* req for L2 eth addr */
1305 struct rfapi_next_hop_entry
*new;
1306 int have_vnc_tunnel_un
= 0;
1308 #if DEBUG_ENCAP_MONITOR
1309 vnc_zlog_debug_verbose("%s: entry, bi %p, rn %p", __func__
, bi
, rn
);
1312 new = XCALLOC(MTYPE_RFAPI_NEXTHOP
, sizeof(struct rfapi_next_hop_entry
));
1315 new->prefix
= *rprefix
;
1318 && decode_rd_type(bi
->extra
->vnc
.import
.rd
.val
)
1319 == RD_TYPE_VNC_ETH
) {
1322 struct rfapi_vn_option
*vo
;
1324 vo
= XCALLOC(MTYPE_RFAPI_VN_OPTION
,
1325 sizeof(struct rfapi_vn_option
));
1328 vo
->type
= RFAPI_VN_OPTION_TYPE_L2ADDR
;
1330 memcpy(&vo
->v
.l2addr
.macaddr
, &rn
->p
.u
.prefix_eth
.octet
,
1332 /* only low 3 bytes of this are significant */
1334 (void)rfapiEcommunityGetLNI(
1335 bi
->attr
->ecommunity
,
1336 &vo
->v
.l2addr
.logical_net_id
);
1337 (void)rfapiEcommunityGetEthernetTag(
1338 bi
->attr
->ecommunity
, &vo
->v
.l2addr
.tag_id
);
1341 /* local_nve_id comes from lower byte of RD type */
1342 vo
->v
.l2addr
.local_nve_id
= bi
->extra
->vnc
.import
.rd
.val
[1];
1344 /* label comes from MP_REACH_NLRI label */
1345 vo
->v
.l2addr
.label
= decode_label(&bi
->extra
->label
[0]);
1347 new->vn_options
= vo
;
1350 * If there is an auxiliary prefix (i.e., host IP address),
1351 * use it as the nexthop prefix instead of the query prefix
1353 if (bi
->extra
->vnc
.import
.aux_prefix
.family
) {
1354 rfapiQprefix2Rprefix(&bi
->extra
->vnc
.import
.aux_prefix
,
1360 bgp_encap_types tun_type
;
1361 new->prefix
.cost
= rfapiRfpCost(bi
->attr
);
1363 struct bgp_attr_encap_subtlv
*pEncap
;
1365 switch (BGP_MP_NEXTHOP_FAMILY(bi
->attr
->mp_nexthop_len
)) {
1367 new->vn_address
.addr_family
= AF_INET
;
1368 new->vn_address
.addr
.v4
=
1369 bi
->attr
->mp_nexthop_global_in
;
1373 new->vn_address
.addr_family
= AF_INET6
;
1374 new->vn_address
.addr
.v6
= bi
->attr
->mp_nexthop_global
;
1378 zlog_warn("%s: invalid vpn nexthop length: %d",
1379 __func__
, bi
->attr
->mp_nexthop_len
);
1380 rfapi_free_next_hop_list(new);
1384 for (pEncap
= bi
->attr
->vnc_subtlvs
; pEncap
;
1385 pEncap
= pEncap
->next
) {
1386 switch (pEncap
->type
) {
1387 case BGP_VNC_SUBTLV_TYPE_LIFETIME
:
1388 /* use configured lifetime, not attr lifetime */
1392 zlog_warn("%s: unknown VNC option type %d",
1393 __func__
, pEncap
->type
);
1400 rfapiGetTunnelType(bi
->attr
, &tun_type
);
1401 if (tun_type
== BGP_ENCAP_TYPE_MPLS
) {
1403 /* MPLS carries UN address in next hop */
1404 rfapiNexthop2Prefix(bi
->attr
, &p
);
1405 if (p
.family
!= 0) {
1406 rfapiQprefix2Raddr(&p
, &new->un_address
);
1407 have_vnc_tunnel_un
= 1;
1411 for (pEncap
= bi
->attr
->encap_subtlvs
; pEncap
;
1412 pEncap
= pEncap
->next
) {
1413 switch (pEncap
->type
) {
1414 case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT
:
1416 * Overrides ENCAP UN address, if any
1418 switch (pEncap
->length
) {
1421 new->un_address
.addr_family
= AF_INET
;
1422 memcpy(&new->un_address
.addr
.v4
,
1424 have_vnc_tunnel_un
= 1;
1428 new->un_address
.addr_family
= AF_INET6
;
1429 memcpy(&new->un_address
.addr
.v6
,
1431 have_vnc_tunnel_un
= 1;
1436 "%s: invalid tunnel subtlv UN addr length (%d) for bi %p",
1437 __func__
, pEncap
->length
, bi
);
1443 "%s: unknown Encap Attribute option type %d",
1444 __func__
, pEncap
->type
);
1451 new->un_options
= rfapi_encap_tlv_to_un_option(bi
->attr
);
1453 #if DEBUG_ENCAP_MONITOR
1454 vnc_zlog_debug_verbose("%s: line %d: have_vnc_tunnel_un=%d",
1455 __func__
, __LINE__
, have_vnc_tunnel_un
);
1458 if (!have_vnc_tunnel_un
&& bi
->extra
) {
1460 * use cached UN address from ENCAP route
1462 new->un_address
.addr_family
=
1463 bi
->extra
->vnc
.import
.un_family
;
1464 switch (new->un_address
.addr_family
) {
1466 new->un_address
.addr
.v4
=
1467 bi
->extra
->vnc
.import
.un
.addr4
;
1470 new->un_address
.addr
.v6
=
1471 bi
->extra
->vnc
.import
.un
.addr6
;
1475 "%s: invalid UN addr family (%d) for bi %p",
1476 __func__
, new->un_address
.addr_family
,
1478 rfapi_free_next_hop_list(new);
1485 new->lifetime
= lifetime
;
1489 int rfapiHasNonRemovedRoutes(struct agg_node
*rn
)
1491 struct bgp_path_info
*bi
;
1493 for (bi
= rn
->info
; bi
; bi
= bi
->next
) {
1496 if (!CHECK_FLAG(bi
->flags
, BGP_PATH_REMOVED
)
1497 && (bi
->extra
&& !rfapiGetUnAddrOfVpnBi(bi
, &pfx
))) {
1509 void rfapiDumpNode(struct agg_node
*rn
)
1511 struct bgp_path_info
*bi
;
1513 vnc_zlog_debug_verbose("%s: rn=%p", __func__
, rn
);
1514 for (bi
= rn
->info
; bi
; bi
= bi
->next
) {
1516 int ctrc
= rfapiGetUnAddrOfVpnBi(bi
, &pfx
);
1519 if (!CHECK_FLAG(bi
->flags
, BGP_PATH_REMOVED
)
1520 && (bi
->extra
&& !ctrc
)) {
1527 vnc_zlog_debug_verbose(
1528 " bi=%p, nr=%d, flags=0x%x, extra=%p, ctrc=%d", bi
, nr
,
1529 bi
->flags
, bi
->extra
, ctrc
);
1534 static int rfapiNhlAddNodeRoutes(
1535 struct agg_node
*rn
, /* in */
1536 struct rfapi_ip_prefix
*rprefix
, /* in */
1537 uint32_t lifetime
, /* in */
1538 int removed
, /* in */
1539 struct rfapi_next_hop_entry
**head
, /* in/out */
1540 struct rfapi_next_hop_entry
**tail
, /* in/out */
1541 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1542 struct agg_node
*rfd_rib_node
, /* preload this NVE rib node */
1543 struct prefix
*pfx_target_original
) /* query target */
1545 struct bgp_path_info
*bi
;
1546 struct rfapi_next_hop_entry
*new;
1547 struct prefix pfx_un
;
1548 struct skiplist
*seen_nexthops
;
1550 int is_l2
= (rn
->p
.family
== AF_ETHERNET
);
1553 struct agg_table
*atable
= agg_get_table(rfd_rib_node
);
1554 struct rfapi_descriptor
*rfd
;
1557 rfd
= agg_get_table_info(atable
);
1559 if (rfapiRibFTDFilterRecentPrefix(rfd
, rn
,
1560 pfx_target_original
))
1566 skiplist_new(0, vnc_prefix_cmp
, (void (*)(void *))prefix_free
);
1568 for (bi
= rn
->info
; bi
; bi
= bi
->next
) {
1570 struct prefix pfx_vn
;
1571 struct prefix
*newpfx
;
1573 if (removed
&& !CHECK_FLAG(bi
->flags
, BGP_PATH_REMOVED
)) {
1574 #if DEBUG_RETURNED_NHL
1575 vnc_zlog_debug_verbose(
1576 "%s: want holddown, this route not holddown, skip",
1581 if (!removed
&& CHECK_FLAG(bi
->flags
, BGP_PATH_REMOVED
)) {
1590 * Check for excluded VN address
1592 if (rfapiVpnBiNhEqualsPt(bi
, exclude_vnaddr
))
1596 * Check for VN address (nexthop) copied already
1599 /* L2 routes: semantic nexthop in aux_prefix; VN addr
1601 pfx_vn
= bi
->extra
->vnc
.import
.aux_prefix
;
1603 rfapiNexthop2Prefix(bi
->attr
, &pfx_vn
);
1605 if (!skiplist_search(seen_nexthops
, &pfx_vn
, NULL
)) {
1606 #if DEBUG_RETURNED_NHL
1607 char buf
[PREFIX_STRLEN
];
1609 prefix2str(&pfx_vn
, buf
, sizeof(buf
));
1610 vnc_zlog_debug_verbose(
1611 "%s: already put VN/nexthop %s, skip", __func__
,
1617 if (rfapiGetUnAddrOfVpnBi(bi
, &pfx_un
)) {
1618 #if DEBUG_ENCAP_MONITOR
1619 vnc_zlog_debug_verbose(
1620 "%s: failed to get UN address of this VPN bi",
1626 newpfx
= prefix_new();
1628 skiplist_insert(seen_nexthops
, newpfx
, newpfx
);
1630 new = rfapiRouteInfo2NextHopEntry(rprefix
, bi
, lifetime
, rn
);
1632 if (rfapiRibPreloadBi(rfd_rib_node
, &pfx_vn
, &pfx_un
,
1634 /* duplicate filtered by RIB */
1635 rfapi_free_next_hop_list(new);
1642 (*tail
)->next
= new;
1651 skiplist_free(seen_nexthops
);
1660 * omit_node is meant for the situation where we are adding a subtree
1661 * of a parent of some original requested node. The response already
1662 * contains the original requested node, and we don't want to duplicate
1663 * its routes in the list, so we skip it if the right or left node
1664 * matches (of course, we still travel down its child subtrees).
1666 static int rfapiNhlAddSubtree(
1667 struct agg_node
*rn
, /* in */
1668 uint32_t lifetime
, /* in */
1669 struct rfapi_next_hop_entry
**head
, /* in/out */
1670 struct rfapi_next_hop_entry
**tail
, /* in/out */
1671 struct agg_node
*omit_node
, /* in */
1672 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1673 struct agg_table
*rfd_rib_table
, /* preload here */
1674 struct prefix
*pfx_target_original
) /* query target */
1676 struct rfapi_ip_prefix rprefix
;
1679 /* FIXME: need to find a better way here to work without sticking our
1680 * hands in node->link */
1681 if (agg_node_left(rn
) && agg_node_left(rn
) != omit_node
) {
1682 if (agg_node_left(rn
)->info
) {
1684 struct agg_node
*rib_rn
= NULL
;
1686 rfapiQprefix2Rprefix(&agg_node_left(rn
)->p
, &rprefix
);
1687 if (rfd_rib_table
) {
1688 rib_rn
= agg_node_get(rfd_rib_table
,
1689 &agg_node_left(rn
)->p
);
1692 count
= rfapiNhlAddNodeRoutes(
1693 agg_node_left(rn
), &rprefix
, lifetime
, 0, head
,
1694 tail
, exclude_vnaddr
, rib_rn
,
1695 pfx_target_original
);
1697 count
= rfapiNhlAddNodeRoutes(
1698 agg_node_left(rn
), &rprefix
, lifetime
,
1699 1, head
, tail
, exclude_vnaddr
, rib_rn
,
1700 pfx_target_original
);
1704 agg_unlock_node(rib_rn
);
1708 if (agg_node_right(rn
) && agg_node_right(rn
) != omit_node
) {
1709 if (agg_node_right(rn
)->info
) {
1711 struct agg_node
*rib_rn
= NULL
;
1713 rfapiQprefix2Rprefix(&agg_node_right(rn
)->p
, &rprefix
);
1714 if (rfd_rib_table
) {
1715 rib_rn
= agg_node_get(rfd_rib_table
,
1716 &agg_node_right(rn
)->p
);
1718 count
= rfapiNhlAddNodeRoutes(
1719 agg_node_right(rn
), &rprefix
, lifetime
, 0, head
,
1720 tail
, exclude_vnaddr
, rib_rn
,
1721 pfx_target_original
);
1723 count
= rfapiNhlAddNodeRoutes(
1724 agg_node_right(rn
), &rprefix
, lifetime
,
1725 1, head
, tail
, exclude_vnaddr
, rib_rn
,
1726 pfx_target_original
);
1730 agg_unlock_node(rib_rn
);
1734 if (agg_node_left(rn
)) {
1735 rcount
+= rfapiNhlAddSubtree(
1736 agg_node_left(rn
), lifetime
, head
, tail
, omit_node
,
1737 exclude_vnaddr
, rfd_rib_table
, pfx_target_original
);
1739 if (agg_node_right(rn
)) {
1740 rcount
+= rfapiNhlAddSubtree(
1741 agg_node_right(rn
), lifetime
, head
, tail
, omit_node
,
1742 exclude_vnaddr
, rfd_rib_table
, pfx_target_original
);
1749 * Implementation of ROUTE_LIST(node) from RFAPI-Import-Event-Handling.txt
1751 * Construct an rfapi nexthop list based on the routes attached to
1752 * the specified node.
1754 * If there are any routes that do NOT have BGP_PATH_REMOVED set,
1755 * return those only. If there are ONLY routes with BGP_PATH_REMOVED,
1756 * then return those, and also include all the non-removed routes from the
1757 * next less-specific node (i.e., this node's parent) at the end.
1759 struct rfapi_next_hop_entry
*rfapiRouteNode2NextHopList(
1760 struct agg_node
*rn
, uint32_t lifetime
, /* put into nexthop entries */
1761 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1762 struct agg_table
*rfd_rib_table
, /* preload here */
1763 struct prefix
*pfx_target_original
) /* query target */
1765 struct rfapi_ip_prefix rprefix
;
1766 struct rfapi_next_hop_entry
*answer
= NULL
;
1767 struct rfapi_next_hop_entry
*last
= NULL
;
1768 struct agg_node
*parent
;
1770 struct agg_node
*rib_rn
;
1772 #if DEBUG_RETURNED_NHL
1774 char buf
[PREFIX_STRLEN
];
1776 prefix2str(&rn
->p
, buf
, sizeof(buf
));
1777 vnc_zlog_debug_verbose("%s: called with node pfx=%s", __func__
,
1780 rfapiDebugBacktrace();
1783 rfapiQprefix2Rprefix(&rn
->p
, &rprefix
);
1785 rib_rn
= rfd_rib_table
? agg_node_get(rfd_rib_table
, &rn
->p
) : NULL
;
1788 * Add non-withdrawn routes at this node
1790 count
= rfapiNhlAddNodeRoutes(rn
, &rprefix
, lifetime
, 0, &answer
, &last
,
1791 exclude_vnaddr
, rib_rn
,
1792 pfx_target_original
);
1795 * If the list has at least one entry, it's finished
1798 count
+= rfapiNhlAddSubtree(rn
, lifetime
, &answer
, &last
, NULL
,
1799 exclude_vnaddr
, rfd_rib_table
,
1800 pfx_target_original
);
1801 vnc_zlog_debug_verbose("%s: %d nexthops, answer=%p", __func__
,
1803 #if DEBUG_RETURNED_NHL
1804 rfapiPrintNhl(NULL
, answer
);
1807 agg_unlock_node(rib_rn
);
1812 * Add withdrawn routes at this node
1814 count
= rfapiNhlAddNodeRoutes(rn
, &rprefix
, lifetime
, 1, &answer
, &last
,
1815 exclude_vnaddr
, rib_rn
,
1816 pfx_target_original
);
1818 agg_unlock_node(rib_rn
);
1820 // rfapiPrintNhl(NULL, answer);
1823 * walk up the tree until we find a node with non-deleted
1824 * routes, then add them
1826 for (parent
= agg_node_parent(rn
); parent
;
1827 parent
= agg_node_parent(parent
)) {
1828 if (rfapiHasNonRemovedRoutes(parent
)) {
1834 * Add non-withdrawn routes from less-specific prefix
1837 rib_rn
= rfd_rib_table
? agg_node_get(rfd_rib_table
, &parent
->p
)
1839 rfapiQprefix2Rprefix(&parent
->p
, &rprefix
);
1840 count
+= rfapiNhlAddNodeRoutes(parent
, &rprefix
, lifetime
, 0,
1841 &answer
, &last
, exclude_vnaddr
,
1842 rib_rn
, pfx_target_original
);
1843 count
+= rfapiNhlAddSubtree(parent
, lifetime
, &answer
, &last
,
1844 rn
, exclude_vnaddr
, rfd_rib_table
,
1845 pfx_target_original
);
1847 agg_unlock_node(rib_rn
);
1850 * There is no parent with non-removed routes. Still need to
1851 * add subtree of original node if it contributed routes to the
1855 count
+= rfapiNhlAddSubtree(rn
, lifetime
, &answer
,
1856 &last
, rn
, exclude_vnaddr
,
1858 pfx_target_original
);
1861 vnc_zlog_debug_verbose("%s: %d nexthops, answer=%p", __func__
, count
,
1863 #if DEBUG_RETURNED_NHL
1864 rfapiPrintNhl(NULL
, answer
);
1870 * Construct nexthop list of all routes in table
1872 struct rfapi_next_hop_entry
*rfapiRouteTable2NextHopList(
1873 struct agg_table
*rt
, uint32_t lifetime
, /* put into nexthop entries */
1874 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1875 struct agg_table
*rfd_rib_table
, /* preload this NVE rib table */
1876 struct prefix
*pfx_target_original
) /* query target */
1878 struct agg_node
*rn
;
1879 struct rfapi_next_hop_entry
*biglist
= NULL
;
1880 struct rfapi_next_hop_entry
*nhl
;
1881 struct rfapi_next_hop_entry
*tail
= NULL
;
1884 for (rn
= agg_route_top(rt
); rn
; rn
= agg_route_next(rn
)) {
1886 nhl
= rfapiRouteNode2NextHopList(rn
, lifetime
, exclude_vnaddr
,
1888 pfx_target_original
);
1890 tail
= biglist
= nhl
;
1897 while (tail
->next
) {
1904 vnc_zlog_debug_verbose("%s: returning %d routes", __func__
, count
);
1908 struct rfapi_next_hop_entry
*rfapiEthRouteNode2NextHopList(
1909 struct agg_node
*rn
, struct rfapi_ip_prefix
*rprefix
,
1910 uint32_t lifetime
, /* put into nexthop entries */
1911 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1912 struct agg_table
*rfd_rib_table
, /* preload NVE rib table */
1913 struct prefix
*pfx_target_original
) /* query target */
1916 struct rfapi_next_hop_entry
*answer
= NULL
;
1917 struct rfapi_next_hop_entry
*last
= NULL
;
1918 struct agg_node
*rib_rn
;
1920 rib_rn
= rfd_rib_table
? agg_node_get(rfd_rib_table
, &rn
->p
) : NULL
;
1922 count
= rfapiNhlAddNodeRoutes(rn
, rprefix
, lifetime
, 0, &answer
, &last
,
1923 NULL
, rib_rn
, pfx_target_original
);
1925 #if DEBUG_ENCAP_MONITOR
1926 vnc_zlog_debug_verbose("%s: node %p: %d non-holddown routes", __func__
,
1931 count
= rfapiNhlAddNodeRoutes(rn
, rprefix
, lifetime
, 1, &answer
,
1932 &last
, exclude_vnaddr
, rib_rn
,
1933 pfx_target_original
);
1934 vnc_zlog_debug_verbose("%s: node %p: %d holddown routes",
1935 __func__
, rn
, count
);
1939 agg_unlock_node(rib_rn
);
1941 #if DEBUG_RETURNED_NHL
1942 rfapiPrintNhl(NULL
, answer
);
1950 * Construct nexthop list of all routes in table
1952 struct rfapi_next_hop_entry
*rfapiEthRouteTable2NextHopList(
1953 uint32_t logical_net_id
, struct rfapi_ip_prefix
*rprefix
,
1954 uint32_t lifetime
, /* put into nexthop entries */
1955 struct rfapi_ip_addr
*exclude_vnaddr
, /* omit routes to same NVE */
1956 struct agg_table
*rfd_rib_table
, /* preload NVE rib node */
1957 struct prefix
*pfx_target_original
) /* query target */
1959 struct rfapi_import_table
*it
;
1960 struct bgp
*bgp
= bgp_get_default();
1961 struct agg_table
*rt
;
1962 struct agg_node
*rn
;
1963 struct rfapi_next_hop_entry
*biglist
= NULL
;
1964 struct rfapi_next_hop_entry
*nhl
;
1965 struct rfapi_next_hop_entry
*tail
= NULL
;
1969 it
= rfapiMacImportTableGet(bgp
, logical_net_id
);
1970 rt
= it
->imported_vpn
[AFI_L2VPN
];
1972 for (rn
= agg_route_top(rt
); rn
; rn
= agg_route_next(rn
)) {
1974 nhl
= rfapiEthRouteNode2NextHopList(
1975 rn
, rprefix
, lifetime
, exclude_vnaddr
, rfd_rib_table
,
1976 pfx_target_original
);
1978 tail
= biglist
= nhl
;
1985 while (tail
->next
) {
1992 vnc_zlog_debug_verbose("%s: returning %d routes", __func__
, count
);
1997 * Insert a new bi to the imported route table node,
1998 * keeping the list of BIs sorted best route first
2000 static void rfapiBgpInfoAttachSorted(struct agg_node
*rn
,
2001 struct bgp_path_info
*info_new
, afi_t afi
,
2005 struct bgp_path_info
*prev
;
2006 struct bgp_path_info
*next
;
2007 char pfx_buf
[PREFIX2STR_BUFFER
];
2010 bgp
= bgp_get_default(); /* assume 1 instance for now */
2012 if (VNC_DEBUG(IMPORT_BI_ATTACH
)) {
2013 vnc_zlog_debug_verbose("%s: info_new->peer=%p", __func__
,
2015 vnc_zlog_debug_verbose("%s: info_new->peer->su_remote=%p",
2016 __func__
, info_new
->peer
->su_remote
);
2019 for (prev
= NULL
, next
= rn
->info
; next
;
2020 prev
= next
, next
= next
->next
) {
2022 || (!CHECK_FLAG(info_new
->flags
, BGP_PATH_REMOVED
)
2023 && CHECK_FLAG(next
->flags
, BGP_PATH_REMOVED
))
2024 || bgp_path_info_cmp_compatible(bgp
, info_new
, next
,
2026 == -1) { /* -1 if 1st is better */
2030 vnc_zlog_debug_verbose("%s: prev=%p, next=%p", __func__
, prev
, next
);
2032 prev
->next
= info_new
;
2034 rn
->info
= info_new
;
2036 info_new
->prev
= prev
;
2037 info_new
->next
= next
;
2039 next
->prev
= info_new
;
2040 bgp_attr_intern(info_new
->attr
);
2043 static void rfapiBgpInfoDetach(struct agg_node
*rn
, struct bgp_path_info
*bi
)
2046 * Remove the route (doubly-linked)
2048 // bgp_attr_unintern (&bi->attr);
2050 bi
->next
->prev
= bi
->prev
;
2052 bi
->prev
->next
= bi
->next
;
2054 rn
->info
= bi
->next
;
2058 * For L3-indexed import tables
2060 static int rfapi_bi_peer_rd_cmp(void *b1
, void *b2
)
2062 struct bgp_path_info
*bi1
= b1
;
2063 struct bgp_path_info
*bi2
= b2
;
2068 if (bi1
->peer
< bi2
->peer
)
2070 if (bi1
->peer
> bi2
->peer
)
2076 return vnc_prefix_cmp((struct prefix
*)&bi1
->extra
->vnc
.import
.rd
,
2077 (struct prefix
*)&bi2
->extra
->vnc
.import
.rd
);
2081 * For L2-indexed import tables
2082 * The BIs in these tables should ALWAYS have an aux_prefix set because
2083 * they arrive via IPv4 or IPv6 advertisements.
2085 static int rfapi_bi_peer_rd_aux_cmp(void *b1
, void *b2
)
2087 struct bgp_path_info
*bi1
= b1
;
2088 struct bgp_path_info
*bi2
= b2
;
2094 if (bi1
->peer
< bi2
->peer
)
2096 if (bi1
->peer
> bi2
->peer
)
2102 rc
= vnc_prefix_cmp((struct prefix
*)&bi1
->extra
->vnc
.import
.rd
,
2103 (struct prefix
*)&bi2
->extra
->vnc
.import
.rd
);
2109 * L2 import tables can have multiple entries with the
2110 * same MAC address, same RD, but different L3 addresses.
2112 * Use presence of aux_prefix with AF=ethernet and prefixlen=1
2113 * as magic value to signify explicit wildcarding of the aux_prefix.
2114 * This magic value will not appear in bona fide bi entries in
2115 * the import table, but is allowed in the "fake" bi used to
2116 * probe the table when searching. (We have to test both b1 and b2
2117 * because there is no guarantee of the order the test key and
2118 * the real key will be passed)
2120 if ((bi1
->extra
->vnc
.import
.aux_prefix
.family
== AF_ETHERNET
2121 && (bi1
->extra
->vnc
.import
.aux_prefix
.prefixlen
== 1))
2122 || (bi2
->extra
->vnc
.import
.aux_prefix
.family
== AF_ETHERNET
2123 && (bi2
->extra
->vnc
.import
.aux_prefix
.prefixlen
== 1))) {
2126 * wildcard aux address specified
2131 return vnc_prefix_cmp(&bi1
->extra
->vnc
.import
.aux_prefix
,
2132 &bi2
->extra
->vnc
.import
.aux_prefix
);
2137 * Index on RD and Peer
2139 static void rfapiItBiIndexAdd(struct agg_node
*rn
, /* Import table VPN node */
2140 struct bgp_path_info
*bi
) /* new BI */
2142 struct skiplist
*sl
;
2149 char buf
[RD_ADDRSTRLEN
];
2151 vnc_zlog_debug_verbose("%s: bi %p, peer %p, rd %s", __func__
,
2153 prefix_rd2str(&bi
->extra
->vnc
.import
.rd
,
2157 sl
= RFAPI_RDINDEX_W_ALLOC(rn
);
2159 if (AF_ETHERNET
== rn
->p
.family
) {
2160 sl
= skiplist_new(0, rfapi_bi_peer_rd_aux_cmp
, NULL
);
2162 sl
= skiplist_new(0, rfapi_bi_peer_rd_cmp
, NULL
);
2164 RFAPI_IT_EXTRA_GET(rn
)->u
.vpn
.idx_rd
= sl
;
2165 agg_lock_node(rn
); /* for skiplist */
2167 assert(!skiplist_insert(sl
, (void *)bi
, (void *)bi
));
2168 agg_lock_node(rn
); /* for skiplist entry */
2170 /* NB: BIs in import tables are not refcounted */
2173 static void rfapiItBiIndexDump(struct agg_node
*rn
)
2175 struct skiplist
*sl
;
2176 void *cursor
= NULL
;
2177 struct bgp_path_info
*k
;
2178 struct bgp_path_info
*v
;
2181 sl
= RFAPI_RDINDEX(rn
);
2185 for (rc
= skiplist_next(sl
, (void **)&k
, (void **)&v
, &cursor
); !rc
;
2186 rc
= skiplist_next(sl
, (void **)&k
, (void **)&v
, &cursor
)) {
2188 char buf
[RD_ADDRSTRLEN
];
2189 char buf_aux_pfx
[PREFIX_STRLEN
];
2191 prefix_rd2str(&k
->extra
->vnc
.import
.rd
, buf
, sizeof(buf
));
2192 if (k
->extra
->vnc
.import
.aux_prefix
.family
) {
2193 prefix2str(&k
->extra
->vnc
.import
.aux_prefix
,
2194 buf_aux_pfx
, sizeof(buf_aux_pfx
));
2196 strncpy(buf_aux_pfx
, "(none)", PREFIX_STRLEN
);
2198 vnc_zlog_debug_verbose("bi %p, peer %p, rd %s, aux_prefix %s",
2199 k
, k
->peer
, buf
, buf_aux_pfx
);
2203 static struct bgp_path_info
*rfapiItBiIndexSearch(
2204 struct agg_node
*rn
, /* Import table VPN node */
2205 struct prefix_rd
*prd
, struct peer
*peer
,
2206 struct prefix
*aux_prefix
) /* optional L3 addr for L2 ITs */
2208 struct skiplist
*sl
;
2210 struct bgp_path_info bi_fake
;
2211 struct bgp_path_info_extra bi_extra
;
2212 struct bgp_path_info
*bi_result
;
2214 sl
= RFAPI_RDINDEX(rn
);
2220 char buf
[RD_ADDRSTRLEN
];
2221 char buf_aux_pfx
[PREFIX_STRLEN
];
2224 prefix2str(aux_prefix
, buf_aux_pfx
,
2225 sizeof(buf_aux_pfx
));
2227 strncpy(buf_aux_pfx
, "(nil)", sizeof(buf_aux_pfx
));
2229 vnc_zlog_debug_verbose("%s want prd=%s, peer=%p, aux_prefix=%s",
2231 prefix_rd2str(prd
, buf
, sizeof(buf
)),
2233 rfapiItBiIndexDump(rn
);
2237 /* threshold is a WAG */
2238 if (sl
->count
< 3) {
2240 vnc_zlog_debug_verbose("%s: short list algorithm", __func__
);
2242 /* if short list, linear search might be faster */
2243 for (bi_result
= rn
->info
; bi_result
;
2244 bi_result
= bi_result
->next
) {
2247 char buf
[RD_ADDRSTRLEN
];
2249 vnc_zlog_debug_verbose(
2250 "%s: bi has prd=%s, peer=%p", __func__
,
2251 prefix_rd2str(&bi_result
->extra
->vnc
2257 if (peer
== bi_result
->peer
2258 && !prefix_cmp((struct prefix
*)&bi_result
->extra
2260 (struct prefix
*)prd
)) {
2263 vnc_zlog_debug_verbose(
2264 "%s: peer and RD same, doing aux_prefix check",
2268 || !prefix_cmp(aux_prefix
,
2269 &bi_result
->extra
->vnc
.import
2273 vnc_zlog_debug_verbose("%s: match",
2283 bi_fake
.peer
= peer
;
2284 bi_fake
.extra
= &bi_extra
;
2285 bi_fake
.extra
->vnc
.import
.rd
= *(struct prefix_rd
*)prd
;
2287 bi_fake
.extra
->vnc
.import
.aux_prefix
= *aux_prefix
;
2290 bi_fake
.extra
->vnc
.import
.aux_prefix
.family
= AF_ETHERNET
;
2291 bi_fake
.extra
->vnc
.import
.aux_prefix
.prefixlen
= 1;
2294 rc
= skiplist_search(sl
, (void *)&bi_fake
, (void *)&bi_result
);
2298 vnc_zlog_debug_verbose("%s: no match", __func__
);
2304 vnc_zlog_debug_verbose("%s: matched bi=%p", __func__
, bi_result
);
2310 static void rfapiItBiIndexDel(struct agg_node
*rn
, /* Import table VPN node */
2311 struct bgp_path_info
*bi
) /* old BI */
2313 struct skiplist
*sl
;
2317 char buf
[RD_ADDRSTRLEN
];
2319 vnc_zlog_debug_verbose("%s: bi %p, peer %p, rd %s", __func__
,
2321 prefix_rd2str(&bi
->extra
->vnc
.import
.rd
,
2325 sl
= RFAPI_RDINDEX(rn
);
2328 rc
= skiplist_delete(sl
, (void *)(bi
), (void *)bi
);
2330 rfapiItBiIndexDump(rn
);
2334 agg_unlock_node(rn
); /* for skiplist entry */
2336 /* NB: BIs in import tables are not refcounted */
2340 * Add a backreference at the ENCAP node to the VPN route that
2344 rfapiMonitorEncapAdd(struct rfapi_import_table
*import_table
,
2345 struct prefix
*p
, /* VN address */
2346 struct agg_node
*vpn_rn
, /* VPN node */
2347 struct bgp_path_info
*vpn_bi
) /* VPN bi/route */
2349 afi_t afi
= family2afi(p
->family
);
2350 struct agg_node
*rn
;
2351 struct rfapi_monitor_encap
*m
;
2354 rn
= agg_node_get(import_table
->imported_encap
[afi
], p
); /* locks rn */
2357 m
= XCALLOC(MTYPE_RFAPI_MONITOR_ENCAP
,
2358 sizeof(struct rfapi_monitor_encap
));
2365 /* insert to encap node's list */
2366 m
->next
= RFAPI_MONITOR_ENCAP(rn
);
2369 RFAPI_MONITOR_ENCAP_W_ALLOC(rn
) = m
;
2371 /* for easy lookup when deleting vpn route */
2372 vpn_bi
->extra
->vnc
.import
.hme
= m
;
2374 vnc_zlog_debug_verbose(
2375 "%s: it=%p, vpn_bi=%p, afi=%d, encap rn=%p, setting vpn_bi->extra->vnc.import.hme=%p",
2376 __func__
, import_table
, vpn_bi
, afi
, rn
, m
);
2378 RFAPI_CHECK_REFCOUNT(rn
, SAFI_ENCAP
, 0);
2379 bgp_attr_intern(vpn_bi
->attr
);
2382 static void rfapiMonitorEncapDelete(struct bgp_path_info
*vpn_bi
)
2385 * Remove encap monitor
2387 vnc_zlog_debug_verbose("%s: vpn_bi=%p", __func__
, vpn_bi
);
2388 if (vpn_bi
->extra
) {
2389 struct rfapi_monitor_encap
*hme
= vpn_bi
->extra
->vnc
.import
.hme
;
2393 vnc_zlog_debug_verbose("%s: hme=%p", __func__
, hme
);
2395 /* Refcount checking takes too long here */
2396 // RFAPI_CHECK_REFCOUNT(hme->rn, SAFI_ENCAP, 0);
2398 hme
->next
->prev
= hme
->prev
;
2400 hme
->prev
->next
= hme
->next
;
2402 RFAPI_MONITOR_ENCAP_W_ALLOC(hme
->rn
) =
2404 /* Refcount checking takes too long here */
2405 // RFAPI_CHECK_REFCOUNT(hme->rn, SAFI_ENCAP, 1);
2407 /* see if the struct rfapi_it_extra is empty and can be
2409 rfapiMonitorExtraPrune(SAFI_ENCAP
, hme
->rn
);
2411 agg_unlock_node(hme
->rn
); /* decr ref count */
2412 XFREE(MTYPE_RFAPI_MONITOR_ENCAP
, hme
);
2413 vpn_bi
->extra
->vnc
.import
.hme
= NULL
;
2419 * quagga lib/thread.h says this must return int even though
2420 * it doesn't do anything with the return value
2422 static int rfapiWithdrawTimerVPN(struct thread
*t
)
2424 struct rfapi_withdraw
*wcb
= t
->arg
;
2425 struct bgp_path_info
*bi
= wcb
->info
;
2426 struct bgp
*bgp
= bgp_get_default();
2428 struct rfapi_monitor_vpn
*moved
;
2433 assert(wcb
->import_table
);
2436 RFAPI_CHECK_REFCOUNT(wcb
->node
, SAFI_MPLS_VPN
, wcb
->lockoffset
);
2441 vnc_zlog_debug_verbose(
2442 "%s: removing bi %p at prefix %s/%d", __func__
, bi
,
2443 rfapi_ntop(wcb
->node
->p
.family
, &wcb
->node
->p
.u
.prefix
,
2445 wcb
->node
->p
.prefixlen
);
2449 * Remove the route (doubly-linked)
2451 if (CHECK_FLAG(bi
->flags
, BGP_PATH_VALID
)
2452 && VALID_INTERIOR_TYPE(bi
->type
))
2453 RFAPI_MONITOR_EXTERIOR(wcb
->node
)->valid_interior_count
--;
2455 afi
= family2afi(wcb
->node
->p
.family
);
2456 wcb
->import_table
->holddown_count
[afi
] -= 1; /* keep count consistent */
2457 rfapiItBiIndexDel(wcb
->node
, bi
);
2458 rfapiBgpInfoDetach(wcb
->node
, bi
); /* with removed bi */
2460 vnc_import_bgp_exterior_del_route_interior(bgp
, wcb
->import_table
,
2465 * If VNC is configured to send response remove messages, AND
2466 * if the removed route had a UN address, do response removal
2469 if (!(bgp
->rfapi_cfg
->flags
2470 & BGP_VNC_CONFIG_RESPONSE_REMOVAL_DISABLE
)) {
2472 int has_valid_duplicate
= 0;
2473 struct bgp_path_info
*bii
;
2476 * First check if there are any OTHER routes at this node
2477 * that have the same nexthop and a valid UN address. If
2478 * there are (e.g., from other peers), then the route isn't
2479 * really gone, so skip sending a response removal message.
2481 for (bii
= wcb
->node
->info
; bii
; bii
= bii
->next
) {
2482 if (rfapiVpnBiSamePtUn(bi
, bii
)) {
2483 has_valid_duplicate
= 1;
2488 vnc_zlog_debug_verbose("%s: has_valid_duplicate=%d", __func__
,
2489 has_valid_duplicate
);
2491 if (!has_valid_duplicate
) {
2492 rfapiRibPendingDeleteRoute(bgp
, wcb
->import_table
, afi
,
2497 rfapiMonitorEncapDelete(bi
);
2500 * If there are no VPN monitors at this VPN Node A,
2503 if (!RFAPI_MONITOR_VPN(wcb
->node
)) {
2504 vnc_zlog_debug_verbose("%s: no VPN monitors at this node",
2510 * rfapiMonitorMoveShorter only moves monitors if there are
2511 * no remaining valid routes at the current node
2513 moved
= rfapiMonitorMoveShorter(wcb
->node
, 1);
2516 rfapiMonitorMovedUp(wcb
->import_table
, wcb
->node
, moved
->node
,
2524 rfapiBgpInfoFree(bi
);
2528 * If route count at this node has gone to 0, withdraw exported prefix
2530 if (!wcb
->node
->info
) {
2531 /* see if the struct rfapi_it_extra is empty and can be freed */
2532 rfapiMonitorExtraPrune(SAFI_MPLS_VPN
, wcb
->node
);
2533 vnc_direct_bgp_del_prefix(bgp
, wcb
->import_table
, wcb
->node
);
2534 vnc_zebra_del_prefix(bgp
, wcb
->import_table
, wcb
->node
);
2537 * nexthop change event
2538 * vnc_direct_bgp_add_prefix() will recompute the VN addr
2541 vnc_direct_bgp_add_prefix(bgp
, wcb
->import_table
, wcb
->node
);
2544 RFAPI_CHECK_REFCOUNT(wcb
->node
, SAFI_MPLS_VPN
, 1 + wcb
->lockoffset
);
2545 agg_unlock_node(wcb
->node
); /* decr ref count */
2546 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
2551 * This works for multiprotocol extension, but not for plain ol'
2552 * unicast IPv4 because that nexthop is stored in attr->nexthop
2554 void rfapiNexthop2Prefix(struct attr
*attr
, struct prefix
*p
)
2559 memset(p
, 0, sizeof(struct prefix
));
2561 switch (p
->family
= BGP_MP_NEXTHOP_FAMILY(attr
->mp_nexthop_len
)) {
2563 p
->u
.prefix4
= attr
->mp_nexthop_global_in
;
2568 p
->u
.prefix6
= attr
->mp_nexthop_global
;
2573 vnc_zlog_debug_verbose("%s: Family is unknown = %d", __func__
,
2578 void rfapiUnicastNexthop2Prefix(afi_t afi
, struct attr
*attr
, struct prefix
*p
)
2580 if (afi
== AFI_IP
) {
2581 p
->family
= AF_INET
;
2583 p
->u
.prefix4
= attr
->nexthop
;
2585 rfapiNexthop2Prefix(attr
, p
);
2589 static int rfapiAttrNexthopAddrDifferent(struct prefix
*p1
, struct prefix
*p2
)
2592 vnc_zlog_debug_verbose("%s: p1 or p2 is NULL", __func__
);
2597 * Are address families the same?
2599 if (p1
->family
!= p2
->family
) {
2603 switch (p1
->family
) {
2605 if (IPV4_ADDR_SAME(&p1
->u
.prefix4
, &p2
->u
.prefix4
))
2610 if (IPV6_ADDR_SAME(&p1
->u
.prefix6
, &p2
->u
.prefix6
))
2621 static void rfapiCopyUnEncap2VPN(struct bgp_path_info
*encap_bi
,
2622 struct bgp_path_info
*vpn_bi
)
2624 if (!encap_bi
->attr
) {
2625 zlog_warn("%s: no encap bi attr/extra, can't copy UN address",
2630 if (!vpn_bi
|| !vpn_bi
->extra
) {
2631 zlog_warn("%s: no vpn bi attr/extra, can't copy UN address",
2636 switch (BGP_MP_NEXTHOP_FAMILY(encap_bi
->attr
->mp_nexthop_len
)) {
2640 * instrumentation to debug segfault of 091127
2642 vnc_zlog_debug_verbose("%s: vpn_bi=%p", __func__
, vpn_bi
);
2644 vnc_zlog_debug_verbose("%s: vpn_bi->extra=%p", __func__
,
2648 vpn_bi
->extra
->vnc
.import
.un_family
= AF_INET
;
2649 vpn_bi
->extra
->vnc
.import
.un
.addr4
=
2650 encap_bi
->attr
->mp_nexthop_global_in
;
2654 vpn_bi
->extra
->vnc
.import
.un_family
= AF_INET6
;
2655 vpn_bi
->extra
->vnc
.import
.un
.addr6
=
2656 encap_bi
->attr
->mp_nexthop_global
;
2660 zlog_warn("%s: invalid encap nexthop length: %d", __func__
,
2661 encap_bi
->attr
->mp_nexthop_len
);
2662 vpn_bi
->extra
->vnc
.import
.un_family
= 0;
2668 * returns 0 on success, nonzero on error
2670 static int rfapiWithdrawEncapUpdateCachedUn(
2671 struct rfapi_import_table
*import_table
, struct bgp_path_info
*encap_bi
,
2672 struct agg_node
*vpn_rn
, struct bgp_path_info
*vpn_bi
)
2677 * clear cached UN address
2679 if (!vpn_bi
|| !vpn_bi
->extra
) {
2681 "%s: missing VPN bi/extra, can't clear UN addr",
2685 vpn_bi
->extra
->vnc
.import
.un_family
= 0;
2686 memset(&vpn_bi
->extra
->vnc
.import
.un
, 0,
2687 sizeof(vpn_bi
->extra
->vnc
.import
.un
));
2688 if (CHECK_FLAG(vpn_bi
->flags
, BGP_PATH_VALID
)) {
2689 if (rfapiGetVncTunnelUnAddr(vpn_bi
->attr
, NULL
)) {
2690 UNSET_FLAG(vpn_bi
->flags
, BGP_PATH_VALID
);
2691 if (VALID_INTERIOR_TYPE(vpn_bi
->type
))
2692 RFAPI_MONITOR_EXTERIOR(vpn_rn
)
2693 ->valid_interior_count
--;
2694 /* signal interior route withdrawal to
2695 * import-exterior */
2696 vnc_import_bgp_exterior_del_route_interior(
2697 bgp_get_default(), import_table
, vpn_rn
,
2704 zlog_warn("%s: missing VPN bi, can't clear UN addr",
2708 rfapiCopyUnEncap2VPN(encap_bi
, vpn_bi
);
2709 if (!CHECK_FLAG(vpn_bi
->flags
, BGP_PATH_VALID
)) {
2710 SET_FLAG(vpn_bi
->flags
, BGP_PATH_VALID
);
2711 if (VALID_INTERIOR_TYPE(vpn_bi
->type
))
2712 RFAPI_MONITOR_EXTERIOR(vpn_rn
)
2713 ->valid_interior_count
++;
2714 /* signal interior route withdrawal to import-exterior
2716 vnc_import_bgp_exterior_add_route_interior(
2717 bgp_get_default(), import_table
, vpn_rn
,
2724 static int rfapiWithdrawTimerEncap(struct thread
*t
)
2726 struct rfapi_withdraw
*wcb
= t
->arg
;
2727 struct bgp_path_info
*bi
= wcb
->info
;
2728 int was_first_route
= 0;
2729 struct rfapi_monitor_encap
*em
;
2730 struct skiplist
*vpn_node_sl
= skiplist_new(0, NULL
, NULL
);
2734 assert(wcb
->import_table
);
2736 RFAPI_CHECK_REFCOUNT(wcb
->node
, SAFI_ENCAP
, 0);
2738 if (wcb
->node
->info
== bi
)
2739 was_first_route
= 1;
2742 * Remove the route/bi and free it
2744 rfapiBgpInfoDetach(wcb
->node
, bi
);
2745 rfapiBgpInfoFree(bi
);
2747 if (!was_first_route
)
2750 for (em
= RFAPI_MONITOR_ENCAP(wcb
->node
); em
; em
= em
->next
) {
2753 * Update monitoring VPN BIs with new encap info at the
2754 * head of the encap bi chain (which could be NULL after
2755 * removing the expiring bi above)
2757 if (rfapiWithdrawEncapUpdateCachedUn(wcb
->import_table
,
2758 wcb
->node
->info
, em
->node
,
2763 * Build a list of unique VPN nodes referenced by these
2765 * Use a skiplist for speed.
2767 skiplist_insert(vpn_node_sl
, em
->node
, em
->node
);
2772 * for each VPN node referenced in the ENCAP monitors:
2774 struct agg_node
*rn
;
2775 while (!skiplist_first(vpn_node_sl
, (void **)&rn
, NULL
)) {
2776 if (!wcb
->node
->info
) {
2777 struct rfapi_monitor_vpn
*moved
;
2779 moved
= rfapiMonitorMoveShorter(rn
, 0);
2781 // rfapiDoRouteCallback(wcb->import_table,
2782 // moved->node, moved);
2783 rfapiMonitorMovedUp(wcb
->import_table
, rn
,
2784 moved
->node
, moved
);
2787 // rfapiDoRouteCallback(wcb->import_table, rn, NULL);
2788 rfapiMonitorItNodeChanged(wcb
->import_table
, rn
, NULL
);
2790 skiplist_delete_first(vpn_node_sl
);
2794 RFAPI_CHECK_REFCOUNT(wcb
->node
, SAFI_ENCAP
, 1);
2795 agg_unlock_node(wcb
->node
); /* decr ref count */
2796 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
2797 skiplist_free(vpn_node_sl
);
2803 * Works for both VPN and ENCAP routes; timer_service_func is different
2807 rfapiBiStartWithdrawTimer(struct rfapi_import_table
*import_table
,
2808 struct agg_node
*rn
, struct bgp_path_info
*bi
,
2809 afi_t afi
, safi_t safi
,
2810 int (*timer_service_func
)(struct thread
*))
2813 struct rfapi_withdraw
*wcb
;
2815 if (CHECK_FLAG(bi
->flags
, BGP_PATH_REMOVED
)) {
2817 * Already on the path to being withdrawn,
2818 * should already have a timer set up to
2821 vnc_zlog_debug_verbose(
2822 "%s: already being withdrawn, do nothing", __func__
);
2826 rfapiGetVncLifetime(bi
->attr
, &lifetime
);
2827 vnc_zlog_debug_verbose("%s: VNC lifetime is %u", __func__
, lifetime
);
2830 * withdrawn routes get to hang around for a while
2832 SET_FLAG(bi
->flags
, BGP_PATH_REMOVED
);
2834 /* set timer to remove the route later */
2835 lifetime
= rfapiGetHolddownFromLifetime(lifetime
);
2836 vnc_zlog_debug_verbose("%s: using timeout %u", __func__
, lifetime
);
2839 * Stash import_table, node, and info for use by timer
2840 * service routine, which is supposed to free the wcb.
2842 wcb
= XCALLOC(MTYPE_RFAPI_WITHDRAW
, sizeof(struct rfapi_withdraw
));
2846 wcb
->import_table
= import_table
;
2847 bgp_attr_intern(bi
->attr
);
2849 if (VNC_DEBUG(VERBOSE
)) {
2850 vnc_zlog_debug_verbose(
2851 "%s: wcb values: node=%p, info=%p, import_table=%p (bi follows)",
2852 __func__
, wcb
->node
, wcb
->info
, wcb
->import_table
);
2853 rfapiPrintBi(NULL
, bi
);
2858 if (lifetime
> UINT32_MAX
/ 1001) {
2859 /* sub-optimal case, but will probably never happen */
2860 bi
->extra
->vnc
.import
.timer
= NULL
;
2861 thread_add_timer(bm
->master
, timer_service_func
, wcb
, lifetime
,
2862 &bi
->extra
->vnc
.import
.timer
);
2864 static uint32_t jitter
;
2865 uint32_t lifetime_msec
;
2868 * the goal here is to spread out the timers so they are
2869 * sortable in the skip list
2871 if (++jitter
>= 1000)
2874 lifetime_msec
= (lifetime
* 1000) + jitter
;
2876 bi
->extra
->vnc
.import
.timer
= NULL
;
2877 thread_add_timer_msec(bm
->master
, timer_service_func
, wcb
,
2879 &bi
->extra
->vnc
.import
.timer
);
2882 /* re-sort route list (BGP_PATH_REMOVED routes are last) */
2883 if (((struct bgp_path_info
*)rn
->info
)->next
) {
2884 rfapiBgpInfoDetach(rn
, bi
);
2885 rfapiBgpInfoAttachSorted(rn
, bi
, afi
, safi
);
2890 typedef void(rfapi_bi_filtered_import_f
)(struct rfapi_import_table
*, int,
2891 struct peer
*, void *, struct prefix
*,
2892 struct prefix
*, afi_t
,
2893 struct prefix_rd
*, struct attr
*,
2894 uint8_t, uint8_t, uint32_t *);
2897 static void rfapiExpireEncapNow(struct rfapi_import_table
*it
,
2898 struct agg_node
*rn
, struct bgp_path_info
*bi
)
2900 struct rfapi_withdraw
*wcb
;
2904 * pretend we're an expiring timer
2906 wcb
= XCALLOC(MTYPE_RFAPI_WITHDRAW
, sizeof(struct rfapi_withdraw
));
2909 wcb
->import_table
= it
;
2910 memset(&t
, 0, sizeof(t
));
2912 rfapiWithdrawTimerEncap(&t
); /* frees wcb */
2915 static int rfapiGetNexthop(struct attr
*attr
, struct prefix
*prefix
)
2917 switch (BGP_MP_NEXTHOP_FAMILY(attr
->mp_nexthop_len
)) {
2919 prefix
->family
= AF_INET
;
2920 prefix
->prefixlen
= 32;
2921 prefix
->u
.prefix4
= attr
->mp_nexthop_global_in
;
2924 prefix
->family
= AF_INET6
;
2925 prefix
->prefixlen
= 128;
2926 prefix
->u
.prefix6
= attr
->mp_nexthop_global
;
2929 vnc_zlog_debug_verbose("%s: unknown attr->mp_nexthop_len %d",
2930 __func__
, attr
->mp_nexthop_len
);
2937 * import a bgp_info if its route target list intersects with the
2938 * import table's route target list
2940 static void rfapiBgpInfoFilteredImportEncap(
2941 struct rfapi_import_table
*import_table
, int action
, struct peer
*peer
,
2942 void *rfd
, /* set for looped back routes */
2944 struct prefix
*aux_prefix
, /* Unused for encap routes */
2945 afi_t afi
, struct prefix_rd
*prd
,
2946 struct attr
*attr
, /* part of bgp_info */
2947 uint8_t type
, /* part of bgp_info */
2948 uint8_t sub_type
, /* part of bgp_info */
2949 uint32_t *label
) /* part of bgp_info */
2951 struct agg_table
*rt
= NULL
;
2952 struct agg_node
*rn
;
2953 struct bgp_path_info
*info_new
;
2954 struct bgp_path_info
*bi
;
2955 struct bgp_path_info
*next
;
2958 struct prefix p_firstbi_old
;
2959 struct prefix p_firstbi_new
;
2961 const char *action_str
= NULL
;
2962 struct prefix un_prefix
;
2965 bgp
= bgp_get_default(); /* assume 1 instance for now */
2968 case FIF_ACTION_UPDATE
:
2969 action_str
= "update";
2971 case FIF_ACTION_WITHDRAW
:
2972 action_str
= "withdraw";
2974 case FIF_ACTION_KILL
:
2975 action_str
= "kill";
2982 vnc_zlog_debug_verbose(
2983 "%s: entry: %s: prefix %s/%d", __func__
, action_str
,
2984 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
), p
->prefixlen
);
2986 memset(&p_firstbi_old
, 0, sizeof(p_firstbi_old
));
2987 memset(&p_firstbi_new
, 0, sizeof(p_firstbi_new
));
2989 if (action
== FIF_ACTION_UPDATE
) {
2991 * Compare rt lists. If no intersection, don't import this route
2992 * On a withdraw, peer and RD are sufficient to determine if
2995 if (!attr
|| !attr
->ecommunity
) {
2997 vnc_zlog_debug_verbose(
2998 "%s: attr, extra, or ecommunity missing, not importing",
3002 #if RFAPI_REQUIRE_ENCAP_BEEC
3003 if (!rfapiEcommunitiesMatchBeec(attr
->ecommunity
)) {
3004 vnc_zlog_debug_verbose(
3005 "%s: it=%p: no match for BGP Encapsulation ecommunity",
3006 __func__
, import_table
);
3010 if (!rfapiEcommunitiesIntersect(import_table
->rt_import_list
,
3011 attr
->ecommunity
)) {
3013 vnc_zlog_debug_verbose(
3014 "%s: it=%p: no ecommunity intersection",
3015 __func__
, import_table
);
3020 * Updates must also have a nexthop address
3022 memset(&un_prefix
, 0,
3023 sizeof(un_prefix
)); /* keep valgrind happy */
3024 if (rfapiGetNexthop(attr
, &un_prefix
)) {
3025 vnc_zlog_debug_verbose("%s: missing nexthop address",
3032 * Figure out which radix tree the route would go into
3037 rt
= import_table
->imported_encap
[afi
];
3041 flog_err(EC_LIB_DEVELOPMENT
, "%s: bad afi %d", __func__
, afi
);
3046 * agg_node_lookup returns a node only if there is at least
3047 * one route attached.
3049 rn
= agg_node_lookup(rt
, p
);
3051 #if DEBUG_ENCAP_MONITOR
3052 vnc_zlog_debug_verbose("%s: initial encap lookup(it=%p) rn=%p",
3053 __func__
, import_table
, rn
);
3058 RFAPI_CHECK_REFCOUNT(rn
, SAFI_ENCAP
, 1);
3059 agg_unlock_node(rn
); /* undo lock in agg_node_lookup */
3063 * capture nexthop of first bi
3066 rfapiNexthop2Prefix(
3067 ((struct bgp_path_info
*)(rn
->info
))->attr
,
3071 for (bi
= rn
->info
; bi
; bi
= bi
->next
) {
3074 * Does this bgp_info refer to the same route
3075 * as we are trying to add?
3077 vnc_zlog_debug_verbose("%s: comparing BI %p", __func__
,
3084 * RD of import table bi is in bi->extra->vnc.import.rd
3085 * RD of info_orig is in prd
3088 vnc_zlog_debug_verbose("%s: no bi->extra",
3093 (struct prefix
*)&bi
->extra
->vnc
.import
.rd
,
3094 (struct prefix
*)prd
)) {
3096 vnc_zlog_debug_verbose("%s: prd does not match",
3104 if (bi
->peer
!= peer
) {
3105 vnc_zlog_debug_verbose(
3106 "%s: peer does not match", __func__
);
3110 vnc_zlog_debug_verbose("%s: found matching bi",
3113 /* Same route. Delete this bi, replace with new one */
3115 if (action
== FIF_ACTION_WITHDRAW
) {
3117 vnc_zlog_debug_verbose(
3118 "%s: withdrawing at prefix %s/%d",
3120 inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
,
3124 rfapiBiStartWithdrawTimer(
3125 import_table
, rn
, bi
, afi
, SAFI_ENCAP
,
3126 rfapiWithdrawTimerEncap
);
3129 vnc_zlog_debug_verbose(
3130 "%s: %s at prefix %s/%d", __func__
,
3131 ((action
== FIF_ACTION_KILL
)
3134 inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
,
3139 * If this route is waiting to be deleted
3141 * a previous withdraw, we must cancel its
3144 if (CHECK_FLAG(bi
->flags
, BGP_PATH_REMOVED
)
3145 && bi
->extra
->vnc
.import
.timer
) {
3148 (struct thread
*)bi
->extra
->vnc
3150 struct rfapi_withdraw
*wcb
= t
->arg
;
3152 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
3156 if (action
== FIF_ACTION_UPDATE
) {
3157 rfapiBgpInfoDetach(rn
, bi
);
3158 rfapiBgpInfoFree(bi
);
3162 * Kill: do export stuff when removing
3165 struct rfapi_withdraw
*wcb
;
3169 * pretend we're an expiring timer
3172 MTYPE_RFAPI_WITHDRAW
,
3173 sizeof(struct rfapi_withdraw
));
3176 wcb
->import_table
= import_table
;
3177 memset(&t
, 0, sizeof(t
));
3179 rfapiWithdrawTimerEncap(
3180 &t
); /* frees wcb */
3189 RFAPI_CHECK_REFCOUNT(rn
, SAFI_ENCAP
, replacing
? 1 : 0);
3191 if (action
== FIF_ACTION_WITHDRAW
|| action
== FIF_ACTION_KILL
)
3195 rfapiBgpInfoCreate(attr
, peer
, rfd
, prd
, type
, sub_type
, NULL
);
3199 agg_lock_node(rn
); /* incr ref count for new BI */
3201 rn
= agg_node_get(rt
, p
);
3204 vnc_zlog_debug_verbose(
3205 "%s: (afi=%d, rn=%p) inserting at prefix %s/%d", __func__
, afi
,
3206 rn
, inet_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, BUFSIZ
),
3209 rfapiBgpInfoAttachSorted(rn
, info_new
, afi
, SAFI_ENCAP
);
3212 * Delete holddown routes from same NVE. See details in
3213 * rfapiBgpInfoFilteredImportVPN()
3215 for (bi
= info_new
->next
; bi
; bi
= next
) {
3217 struct prefix pfx_un
;
3221 if (!CHECK_FLAG(bi
->flags
, BGP_PATH_REMOVED
))
3225 * We already match the VN address (it is the prefix
3226 * of the route node)
3229 if (!rfapiGetNexthop(bi
->attr
, &pfx_un
)
3230 && prefix_same(&pfx_un
, &un_prefix
)) {
3238 vnc_zlog_debug_verbose(
3239 "%s: removing holddown bi matching NVE of new route",
3241 if (bi
->extra
->vnc
.import
.timer
) {
3243 (struct thread
*)bi
->extra
->vnc
.import
.timer
;
3244 struct rfapi_withdraw
*wcb
= t
->arg
;
3246 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
3249 rfapiExpireEncapNow(import_table
, rn
, bi
);
3252 rfapiNexthop2Prefix(((struct bgp_path_info
*)(rn
->info
))->attr
,
3256 * If the nexthop address of the selected Encap route (i.e.,
3257 * the UN address) has changed, then we must update the VPN
3258 * routes that refer to this Encap route and possibly force
3261 if (rfapiAttrNexthopAddrDifferent(&p_firstbi_old
, &p_firstbi_new
)) {
3263 struct rfapi_monitor_encap
*m
;
3264 struct rfapi_monitor_encap
*mnext
;
3266 struct agg_node
*referenced_vpn_prefix
;
3269 * Optimized approach: build radix tree on the fly to
3270 * hold list of VPN nodes referenced by the ENCAP monitors
3272 * The nodes in this table correspond to prefixes of VPN routes.
3273 * The "info" pointer of the node points to a chain of
3274 * struct rfapi_monitor_encap, each of which refers to a
3275 * specific VPN node.
3277 struct agg_table
*referenced_vpn_table
;
3279 referenced_vpn_table
= agg_table_init();
3280 assert(referenced_vpn_table
);
3283 * iterate over the set of monitors at this ENCAP node.
3285 #if DEBUG_ENCAP_MONITOR
3286 vnc_zlog_debug_verbose("%s: examining monitors at rn=%p",
3289 for (m
= RFAPI_MONITOR_ENCAP(rn
); m
; m
= m
->next
) {
3292 * For each referenced bi/route, copy the ENCAP route's
3293 * nexthop to the VPN route's cached UN address field
3295 * the address family of the cached UN address field.
3297 rfapiCopyUnEncap2VPN(info_new
, m
->bi
);
3298 if (!CHECK_FLAG(m
->bi
->flags
, BGP_PATH_VALID
)) {
3299 SET_FLAG(m
->bi
->flags
, BGP_PATH_VALID
);
3300 if (VALID_INTERIOR_TYPE(m
->bi
->type
))
3301 RFAPI_MONITOR_EXTERIOR(m
->node
)
3302 ->valid_interior_count
++;
3303 vnc_import_bgp_exterior_add_route_interior(
3304 bgp
, import_table
, m
->node
, m
->bi
);
3308 * Build a list of unique VPN nodes referenced by these
3311 * There could be more than one VPN node here with a
3313 * prefix. Those are currently in an unsorted linear
3318 referenced_vpn_prefix
=
3319 agg_node_get(referenced_vpn_table
, &m
->node
->p
);
3320 assert(referenced_vpn_prefix
);
3321 for (mnext
= referenced_vpn_prefix
->info
; mnext
;
3322 mnext
= mnext
->next
) {
3324 if (mnext
->node
== m
->node
)
3330 * already have an entry for this VPN node
3332 agg_unlock_node(referenced_vpn_prefix
);
3335 MTYPE_RFAPI_MONITOR_ENCAP
,
3336 sizeof(struct rfapi_monitor_encap
));
3338 mnext
->node
= m
->node
;
3339 mnext
->next
= referenced_vpn_prefix
->info
;
3340 referenced_vpn_prefix
->info
= mnext
;
3345 * for each VPN node referenced in the ENCAP monitors:
3347 for (referenced_vpn_prefix
=
3348 agg_route_top(referenced_vpn_table
);
3349 referenced_vpn_prefix
;
3350 referenced_vpn_prefix
=
3351 agg_route_next(referenced_vpn_prefix
)) {
3353 while ((m
= referenced_vpn_prefix
->info
)) {
3357 rfapiMonitorMoveLonger(m
->node
);
3358 for (n
= m
->node
; n
; n
= agg_node_parent(n
)) {
3359 // rfapiDoRouteCallback(import_table, n,
3362 rfapiMonitorItNodeChanged(import_table
, m
->node
,
3365 referenced_vpn_prefix
->info
= m
->next
;
3366 agg_unlock_node(referenced_vpn_prefix
);
3367 XFREE(MTYPE_RFAPI_MONITOR_ENCAP
, m
);
3370 agg_table_finish(referenced_vpn_table
);
3373 RFAPI_CHECK_REFCOUNT(rn
, SAFI_ENCAP
, 0);
3376 static void rfapiExpireVpnNow(struct rfapi_import_table
*it
,
3377 struct agg_node
*rn
, struct bgp_path_info
*bi
,
3380 struct rfapi_withdraw
*wcb
;
3384 * pretend we're an expiring timer
3386 wcb
= XCALLOC(MTYPE_RFAPI_WITHDRAW
, sizeof(struct rfapi_withdraw
));
3389 wcb
->import_table
= it
;
3390 wcb
->lockoffset
= lockoffset
;
3391 memset(&t
, 0, sizeof(t
));
3393 rfapiWithdrawTimerVPN(&t
); /* frees wcb */
3398 * import a bgp_info if its route target list intersects with the
3399 * import table's route target list
3401 void rfapiBgpInfoFilteredImportVPN(
3402 struct rfapi_import_table
*import_table
, int action
, struct peer
*peer
,
3403 void *rfd
, /* set for looped back routes */
3405 struct prefix
*aux_prefix
, /* AFI_L2VPN: optional IP */
3406 afi_t afi
, struct prefix_rd
*prd
,
3407 struct attr
*attr
, /* part of bgp_info */
3408 uint8_t type
, /* part of bgp_info */
3409 uint8_t sub_type
, /* part of bgp_info */
3410 uint32_t *label
) /* part of bgp_info */
3412 struct agg_table
*rt
= NULL
;
3413 struct agg_node
*rn
;
3415 struct bgp_path_info
*info_new
;
3416 struct bgp_path_info
*bi
;
3417 struct bgp_path_info
*next
;
3419 struct prefix vn_prefix
;
3420 struct prefix un_prefix
;
3421 int un_prefix_valid
= 0;
3422 struct agg_node
*ern
;
3424 int original_had_routes
= 0;
3425 struct prefix original_nexthop
;
3426 const char *action_str
= NULL
;
3430 bgp
= bgp_get_default(); /* assume 1 instance for now */
3433 case FIF_ACTION_UPDATE
:
3434 action_str
= "update";
3436 case FIF_ACTION_WITHDRAW
:
3437 action_str
= "withdraw";
3439 case FIF_ACTION_KILL
:
3440 action_str
= "kill";
3447 if (import_table
== bgp
->rfapi
->it_ce
)
3450 vnc_zlog_debug_verbose("%s: entry: %s%s: prefix %s/%d: it %p, afi %s",
3451 __func__
, (is_it_ce
? "CE-IT " : ""), action_str
,
3452 rfapi_ntop(p
->family
, &p
->u
.prefix
, buf
, BUFSIZ
),
3453 p
->prefixlen
, import_table
, afi2str(afi
));
3458 * Compare rt lists. If no intersection, don't import this route
3459 * On a withdraw, peer and RD are sufficient to determine if
3462 if (action
== FIF_ACTION_UPDATE
) {
3463 if (!attr
|| !attr
->ecommunity
) {
3465 vnc_zlog_debug_verbose(
3466 "%s: attr, extra, or ecommunity missing, not importing",
3470 if ((import_table
!= bgp
->rfapi
->it_ce
)
3471 && !rfapiEcommunitiesIntersect(import_table
->rt_import_list
,
3472 attr
->ecommunity
)) {
3474 vnc_zlog_debug_verbose(
3475 "%s: it=%p: no ecommunity intersection",
3476 __func__
, import_table
);
3480 memset(&vn_prefix
, 0,
3481 sizeof(vn_prefix
)); /* keep valgrind happy */
3482 if (rfapiGetNexthop(attr
, &vn_prefix
)) {
3483 /* missing nexthop address would be a bad, bad thing */
3484 vnc_zlog_debug_verbose("%s: missing nexthop", __func__
);
3490 * Figure out which radix tree the route would go into
3496 rt
= import_table
->imported_vpn
[afi
];
3500 flog_err(EC_LIB_DEVELOPMENT
, "%s: bad afi %d", __func__
, afi
);
3505 memset(&original_nexthop
, 0, sizeof(original_nexthop
));
3508 * agg_node_lookup returns a node only if there is at least
3509 * one route attached.
3511 rn
= agg_node_lookup(rt
, p
);
3513 vnc_zlog_debug_verbose("%s: rn=%p", __func__
, rn
);
3517 RFAPI_CHECK_REFCOUNT(rn
, SAFI_MPLS_VPN
, 1);
3518 agg_unlock_node(rn
); /* undo lock in agg_node_lookup */
3521 original_had_routes
= 1;
3523 if (VNC_DEBUG(VERBOSE
)) {
3524 vnc_zlog_debug_verbose("%s: showing IT node on entry",
3526 rfapiShowItNode(NULL
, rn
); /* debug */
3530 * Look for same route (will have same RD and peer)
3532 bi
= rfapiItBiIndexSearch(rn
, prd
, peer
, aux_prefix
);
3537 * This was an old test when we iterated over the
3538 * BIs linearly. Since we're now looking up with
3539 * RD and peer, comparing types should not be
3540 * needed. Changed to assertion.
3542 * Compare types. Doing so prevents a RFP-originated
3543 * route from matching an imported route, for example.
3545 if (VNC_DEBUG(VERBOSE
) && bi
->type
!= type
)
3546 /* should be handled by RDs, but warn for now */
3547 zlog_warn("%s: type mismatch! (bi=%d, arg=%d)",
3548 __func__
, bi
->type
, type
);
3550 vnc_zlog_debug_verbose("%s: found matching bi",
3554 * In the special CE table, withdrawals occur without
3557 if (import_table
== bgp
->rfapi
->it_ce
) {
3558 vnc_direct_bgp_del_route_ce(bgp
, rn
, bi
);
3559 if (action
== FIF_ACTION_WITHDRAW
)
3560 action
= FIF_ACTION_KILL
;
3563 if (action
== FIF_ACTION_WITHDRAW
) {
3566 CHECK_FLAG(bi
->flags
, BGP_PATH_REMOVED
);
3568 vnc_zlog_debug_verbose(
3569 "%s: withdrawing at prefix %s/%d%s",
3570 __func__
, rfapi_ntop(rn
->p
.family
,
3575 ? " (already being withdrawn)"
3580 rfapiBiStartWithdrawTimer(
3581 import_table
, rn
, bi
, afi
,
3583 rfapiWithdrawTimerVPN
);
3585 RFAPI_UPDATE_ITABLE_COUNT(
3586 bi
, import_table
, afi
, -1);
3587 import_table
->holddown_count
[afi
] += 1;
3591 vnc_zlog_debug_verbose(
3592 "%s: %s at prefix %s/%d", __func__
,
3593 ((action
== FIF_ACTION_KILL
)
3596 rfapi_ntop(rn
->p
.family
,
3597 &rn
->p
.u
.prefix
, buf
,
3602 * If this route is waiting to be deleted
3604 * a previous withdraw, we must cancel its
3607 if (CHECK_FLAG(bi
->flags
, BGP_PATH_REMOVED
)
3608 && bi
->extra
->vnc
.import
.timer
) {
3611 (struct thread
*)bi
->extra
->vnc
3613 struct rfapi_withdraw
*wcb
= t
->arg
;
3615 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
3618 import_table
->holddown_count
[afi
] -= 1;
3619 RFAPI_UPDATE_ITABLE_COUNT(
3620 bi
, import_table
, afi
, 1);
3623 * decrement remote count (if route is remote)
3625 * we are going to remove it below
3627 RFAPI_UPDATE_ITABLE_COUNT(bi
, import_table
, afi
,
3629 if (action
== FIF_ACTION_UPDATE
) {
3633 * make copy of original nexthop so we
3634 * can see if it changed
3636 rfapiGetNexthop(bi
->attr
,
3640 * remove bi without doing any export
3643 if (CHECK_FLAG(bi
->flags
,
3645 && VALID_INTERIOR_TYPE(bi
->type
))
3646 RFAPI_MONITOR_EXTERIOR(rn
)
3647 ->valid_interior_count
--;
3648 rfapiItBiIndexDel(rn
, bi
);
3649 rfapiBgpInfoDetach(rn
, bi
);
3650 rfapiMonitorEncapDelete(bi
);
3651 vnc_import_bgp_exterior_del_route_interior(
3652 bgp
, import_table
, rn
, bi
);
3653 rfapiBgpInfoFree(bi
);
3657 * remove bi and do export processing
3659 import_table
->holddown_count
[afi
] += 1;
3660 rfapiExpireVpnNow(import_table
, rn
, bi
,
3668 RFAPI_CHECK_REFCOUNT(rn
, SAFI_MPLS_VPN
, replacing
? 1 : 0);
3670 if (action
== FIF_ACTION_WITHDRAW
|| action
== FIF_ACTION_KILL
) {
3676 rfapiBgpInfoCreate(attr
, peer
, rfd
, prd
, type
, sub_type
, label
);
3679 * lookup un address in encap table
3681 ern
= agg_node_match(import_table
->imported_encap
[afi
], &vn_prefix
);
3683 rfapiCopyUnEncap2VPN(ern
->info
, info_new
);
3684 agg_unlock_node(ern
); /* undo lock in route_note_match */
3686 char bpf
[PREFIX_STRLEN
];
3688 prefix2str(&vn_prefix
, bpf
, sizeof(bpf
));
3689 /* Not a big deal, just means VPN route got here first */
3690 vnc_zlog_debug_verbose("%s: no encap route for vn addr %s",
3692 info_new
->extra
->vnc
.import
.un_family
= 0;
3700 * No need to increment reference count, so only "get"
3701 * if the node is not there already
3703 rn
= agg_node_get(rt
, p
);
3707 * For ethernet routes, if there is an accompanying IP address,
3710 if ((AFI_L2VPN
== afi
) && aux_prefix
) {
3712 vnc_zlog_debug_verbose("%s: setting BI's aux_prefix", __func__
);
3713 info_new
->extra
->vnc
.import
.aux_prefix
= *aux_prefix
;
3716 vnc_zlog_debug_verbose(
3717 "%s: inserting bi %p at prefix %s/%d #%d", __func__
, info_new
,
3718 rfapi_ntop(rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, BUFSIZ
),
3719 rn
->p
.prefixlen
, rn
->lock
);
3721 rfapiBgpInfoAttachSorted(rn
, info_new
, afi
, SAFI_MPLS_VPN
);
3722 rfapiItBiIndexAdd(rn
, info_new
);
3723 if (!rfapiGetUnAddrOfVpnBi(info_new
, NULL
)) {
3724 if (VALID_INTERIOR_TYPE(info_new
->type
))
3725 RFAPI_MONITOR_EXTERIOR(rn
)->valid_interior_count
++;
3726 SET_FLAG(info_new
->flags
, BGP_PATH_VALID
);
3728 RFAPI_UPDATE_ITABLE_COUNT(info_new
, import_table
, afi
, 1);
3729 vnc_import_bgp_exterior_add_route_interior(bgp
, import_table
, rn
,
3732 if (import_table
== bgp
->rfapi
->it_ce
)
3733 vnc_direct_bgp_add_route_ce(bgp
, rn
, info_new
);
3735 if (VNC_DEBUG(VERBOSE
)) {
3736 vnc_zlog_debug_verbose("%s: showing IT node", __func__
);
3737 rfapiShowItNode(NULL
, rn
); /* debug */
3740 rfapiMonitorEncapAdd(import_table
, &vn_prefix
, rn
, info_new
);
3742 if (!rfapiGetUnAddrOfVpnBi(info_new
, &un_prefix
)) {
3745 * if we have a valid UN address (either via Encap route
3746 * or via tunnel attribute), then we should attempt
3747 * to move any monitors at less-specific nodes to this node
3749 rfapiMonitorMoveLonger(rn
);
3751 un_prefix_valid
= 1;
3755 * 101129 Enhancement: if we add a route (implication: it is not
3756 * in holddown), delete all other routes from this nve at this
3757 * node that are in holddown, regardless of peer.
3759 * Reasons it's OK to do that:
3761 * - if the holddown route being deleted originally came from BGP VPN,
3762 * it is already gone from BGP (implication of holddown), so there
3763 * won't be any added inconsistency with the BGP RIB.
3765 * - once a fresh route is added at a prefix, any routes in holddown
3766 * at that prefix will not show up in RFP responses, so deleting
3767 * the holddown routes won't affect the contents of responses.
3769 * - lifetimes are supposed to be consistent, so there should not
3770 * be a case where the fresh route has a shorter lifetime than
3771 * the holddown route, so we don't expect the fresh route to
3772 * disappear and complete its holddown time before the existing
3773 * holddown routes time out. Therefore, we won't have a situation
3774 * where we expect the existing holddown routes to be hidden and
3775 * then to reappear sometime later (as holddown routes) in a
3778 * Among other things, this would enable us to skirt the problem
3779 * of local holddown routes that refer to NVE descriptors that
3780 * have already been closed (if the same NVE triggers a subsequent
3781 * rfapi_open(), the new peer is different and doesn't match the
3782 * peer of the holddown route, so the stale holddown route still
3783 * hangs around until it times out instead of just being replaced
3784 * by the fresh route).
3787 * We know that the new bi will have been inserted before any routes
3788 * in holddown, so we can skip any that came before it
3790 for (bi
= info_new
->next
; bi
; bi
= next
) {
3792 struct prefix pfx_vn
;
3793 struct prefix pfx_un
;
3795 int remote_peer_match
= 0;
3802 if (!CHECK_FLAG(bi
->flags
, BGP_PATH_REMOVED
))
3806 * Must match VN address (nexthop of VPN route)
3808 if (rfapiGetNexthop(bi
->attr
, &pfx_vn
))
3810 if (!prefix_same(&pfx_vn
, &vn_prefix
))
3813 if (un_prefix_valid
&& /* new route UN addr */
3814 !rfapiGetUnAddrOfVpnBi(bi
, &pfx_un
)
3815 && /* old route UN addr */
3816 prefix_same(&pfx_un
, &un_prefix
)) { /* compare */
3819 if (!RFAPI_LOCAL_BI(bi
) && !RFAPI_LOCAL_BI(info_new
)
3820 && sockunion_same(&bi
->peer
->su
, &info_new
->peer
->su
)) {
3821 /* old & new are both remote, same peer */
3822 remote_peer_match
= 1;
3825 if (!un_match
& !remote_peer_match
)
3828 vnc_zlog_debug_verbose(
3829 "%s: removing holddown bi matching NVE of new route",
3831 if (bi
->extra
->vnc
.import
.timer
) {
3833 (struct thread
*)bi
->extra
->vnc
.import
.timer
;
3834 struct rfapi_withdraw
*wcb
= t
->arg
;
3836 XFREE(MTYPE_RFAPI_WITHDRAW
, wcb
);
3839 rfapiExpireVpnNow(import_table
, rn
, bi
, 0);
3842 if (!original_had_routes
) {
3844 * We went from 0 usable routes to 1 usable route. Perform the
3845 * "Adding a Route" export process.
3847 vnc_direct_bgp_add_prefix(bgp
, import_table
, rn
);
3848 vnc_zebra_add_prefix(bgp
, import_table
, rn
);
3851 * Check for nexthop change event
3852 * Note: the prefix_same() test below detects two situations:
3853 * 1. route is replaced, new route has different nexthop
3854 * 2. new route is added (original_nexthop is 0)
3856 struct prefix new_nexthop
;
3858 rfapiGetNexthop(attr
, &new_nexthop
);
3859 if (!prefix_same(&original_nexthop
, &new_nexthop
)) {
3861 * nexthop change event
3862 * vnc_direct_bgp_add_prefix() will recompute VN addr
3865 vnc_direct_bgp_add_prefix(bgp
, import_table
, rn
);
3869 if (!(bgp
->rfapi_cfg
->flags
& BGP_VNC_CONFIG_CALLBACK_DISABLE
)) {
3870 for (n
= rn
; n
; n
= agg_node_parent(n
)) {
3871 // rfapiDoRouteCallback(import_table, n, NULL);
3873 rfapiMonitorItNodeChanged(import_table
, rn
, NULL
);
3875 RFAPI_CHECK_REFCOUNT(rn
, SAFI_MPLS_VPN
, 0);
3879 static void rfapiBgpInfoFilteredImportBadSafi(
3880 struct rfapi_import_table
*import_table
, int action
, struct peer
*peer
,
3881 void *rfd
, /* set for looped back routes */
3883 struct prefix
*aux_prefix
, /* AFI_L2VPN: optional IP */
3884 afi_t afi
, struct prefix_rd
*prd
,
3885 struct attr
*attr
, /* part of bgp_info */
3886 uint8_t type
, /* part of bgp_info */
3887 uint8_t sub_type
, /* part of bgp_info */
3888 uint32_t *label
) /* part of bgp_info */
3890 vnc_zlog_debug_verbose("%s: Error, bad safi", __func__
);
3893 static rfapi_bi_filtered_import_f
*
3894 rfapiBgpInfoFilteredImportFunction(safi_t safi
)
3898 return rfapiBgpInfoFilteredImportVPN
;
3901 return rfapiBgpInfoFilteredImportEncap
;
3905 flog_err(EC_LIB_DEVELOPMENT
, "%s: bad safi %d", __func__
, safi
);
3906 return rfapiBgpInfoFilteredImportBadSafi
;
3910 void rfapiProcessUpdate(struct peer
*peer
,
3911 void *rfd
, /* set when looped from RFP/RFAPI */
3912 struct prefix
*p
, struct prefix_rd
*prd
,
3913 struct attr
*attr
, afi_t afi
, safi_t safi
, uint8_t type
,
3914 uint8_t sub_type
, uint32_t *label
)
3918 struct rfapi_import_table
*it
;
3919 int has_ip_route
= 1;
3922 bgp
= bgp_get_default(); /* assume 1 instance for now */
3929 * look at high-order byte of RD. FF means MAC
3930 * address is present (VNC L2VPN)
3932 if ((safi
== SAFI_MPLS_VPN
)
3933 && (decode_rd_type(prd
->val
) == RD_TYPE_VNC_ETH
)) {
3934 struct prefix pfx_mac_buf
;
3935 struct prefix pfx_nexthop_buf
;
3939 * Set flag if prefix and nexthop are the same - don't
3940 * add the route to normal IP-based import tables
3942 if (!rfapiGetNexthop(attr
, &pfx_nexthop_buf
)) {
3943 if (!prefix_cmp(&pfx_nexthop_buf
, p
)) {
3948 memset(&pfx_mac_buf
, 0, sizeof(pfx_mac_buf
));
3949 pfx_mac_buf
.family
= AF_ETHERNET
;
3950 pfx_mac_buf
.prefixlen
= 48;
3951 memcpy(&pfx_mac_buf
.u
.prefix_eth
.octet
, prd
->val
+ 2, 6);
3954 * Find rt containing LNI (Logical Network ID), which
3955 * _should_ always be present when mac address is present
3957 rc
= rfapiEcommunityGetLNI(attr
->ecommunity
, &lni
);
3959 vnc_zlog_debug_verbose(
3960 "%s: rfapiEcommunityGetLNI returned %d, lni=%d, attr=%p",
3961 __func__
, rc
, lni
, attr
);
3963 it
= rfapiMacImportTableGet(bgp
, lni
);
3965 rfapiBgpInfoFilteredImportVPN(
3966 it
, FIF_ACTION_UPDATE
, peer
, rfd
,
3967 &pfx_mac_buf
, /* prefix */
3968 p
, /* aux prefix: IP addr */
3969 AFI_L2VPN
, prd
, attr
, type
, sub_type
, label
);
3977 * Iterate over all import tables; do a filtered import
3978 * for the afi/safi combination
3980 for (it
= h
->imports
; it
; it
= it
->next
) {
3981 (*rfapiBgpInfoFilteredImportFunction(safi
))(
3982 it
, FIF_ACTION_UPDATE
, peer
, rfd
, p
, /* prefix */
3983 NULL
, afi
, prd
, attr
, type
, sub_type
, label
);
3986 if (safi
== SAFI_MPLS_VPN
) {
3987 vnc_direct_bgp_rh_add_route(bgp
, afi
, p
, peer
, attr
);
3988 rfapiBgpInfoFilteredImportVPN(
3989 bgp
->rfapi
->it_ce
, FIF_ACTION_UPDATE
, peer
, rfd
,
3991 NULL
, afi
, prd
, attr
, type
, sub_type
, label
);
3996 void rfapiProcessWithdraw(struct peer
*peer
, void *rfd
, struct prefix
*p
,
3997 struct prefix_rd
*prd
, struct attr
*attr
, afi_t afi
,
3998 safi_t safi
, uint8_t type
, int kill
)
4002 struct rfapi_import_table
*it
;
4004 bgp
= bgp_get_default(); /* assume 1 instance for now */
4011 * look at high-order byte of RD. FF means MAC
4012 * address is present (VNC L2VPN)
4014 if (h
->import_mac
!= NULL
&& safi
== SAFI_MPLS_VPN
4015 && decode_rd_type(prd
->val
) == RD_TYPE_VNC_ETH
) {
4016 struct prefix pfx_mac_buf
;
4017 void *cursor
= NULL
;
4020 memset(&pfx_mac_buf
, 0, sizeof(pfx_mac_buf
));
4021 pfx_mac_buf
.family
= AF_ETHERNET
;
4022 pfx_mac_buf
.prefixlen
= 48;
4023 memcpy(&pfx_mac_buf
.u
.prefix_eth
, prd
->val
+ 2, 6);
4026 * withdraw does not contain attrs, so we don't have
4027 * access to the route's LNI, which would ordinarily
4028 * select the specific mac-based import table. Instead,
4029 * we must iterate over all mac-based tables and rely
4030 * on the RD to match.
4032 * If this approach is too slow, add an index where
4033 * key is {RD, peer} and value is the import table
4035 for (rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4037 rc
== 0; rc
= skiplist_next(h
->import_mac
, NULL
,
4038 (void **)&it
, &cursor
)) {
4041 vnc_zlog_debug_verbose(
4042 "%s: calling rfapiBgpInfoFilteredImportVPN(it=%p, afi=AFI_L2VPN)",
4046 rfapiBgpInfoFilteredImportVPN(
4048 (kill
? FIF_ACTION_KILL
: FIF_ACTION_WITHDRAW
),
4049 peer
, rfd
, &pfx_mac_buf
, /* prefix */
4050 p
, /* aux_prefix: IP */
4051 AFI_L2VPN
, prd
, attr
, type
, 0,
4052 NULL
); /* sub_type & label unused for withdraw
4058 * XXX For the case where the withdraw involves an L2
4059 * route with no IP information, we rely on the lack
4060 * of RT-list intersection to filter out the withdraw
4061 * from the IP-based import tables below
4065 * Iterate over all import tables; do a filtered import
4066 * for the afi/safi combination
4069 for (it
= h
->imports
; it
; it
= it
->next
) {
4070 (*rfapiBgpInfoFilteredImportFunction(safi
))(
4071 it
, (kill
? FIF_ACTION_KILL
: FIF_ACTION_WITHDRAW
),
4072 peer
, rfd
, p
, /* prefix */
4073 NULL
, afi
, prd
, attr
, type
, 0,
4074 NULL
); /* sub_type & label unused for withdraw */
4077 /* TBD the deletion should happen after the lifetime expires */
4078 if (safi
== SAFI_MPLS_VPN
)
4079 vnc_direct_bgp_rh_del_route(bgp
, afi
, p
, peer
);
4081 if (safi
== SAFI_MPLS_VPN
) {
4082 rfapiBgpInfoFilteredImportVPN(
4084 (kill
? FIF_ACTION_KILL
: FIF_ACTION_WITHDRAW
), peer
,
4085 rfd
, p
, /* prefix */
4086 NULL
, afi
, prd
, attr
, type
, 0,
4087 NULL
); /* sub_type & label unused for withdraw */
4092 * TBD optimized withdraw timer algorithm for case of many
4093 * routes expiring at the same time due to peer drop.
4096 * 1. Visit all BIs in all ENCAP import tables.
4098 * a. If a bi's peer is the failed peer, remove the bi.
4099 * b. If the removed ENCAP bi was first in the list of
4100 * BIs at this ENCAP node, loop over all monitors
4103 * (1) for each ENCAP monitor, loop over all its
4104 * VPN node monitors and set their RFAPI_MON_FLAG_NEEDCALLBACK
4107 * 2. Visit all BIs in all VPN import tables.
4108 * a. If a bi's peer is the failed peer, remove the bi.
4109 * b. loop over all the VPN node monitors and set their
4110 * RFAPI_MON_FLAG_NEEDCALLBACK flags
4111 * c. If there are no BIs left at this VPN node,
4116 /* surprise, this gets called from peer_delete(), from rfapi_close() */
4117 static void rfapiProcessPeerDownRt(struct peer
*peer
,
4118 struct rfapi_import_table
*import_table
,
4119 afi_t afi
, safi_t safi
)
4121 struct agg_node
*rn
;
4122 struct bgp_path_info
*bi
;
4123 struct agg_table
*rt
;
4124 int (*timer_service_func
)(struct thread
*);
4126 assert(afi
== AFI_IP
|| afi
== AFI_IP6
);
4132 rt
= import_table
->imported_vpn
[afi
];
4133 timer_service_func
= rfapiWithdrawTimerVPN
;
4136 rt
= import_table
->imported_encap
[afi
];
4137 timer_service_func
= rfapiWithdrawTimerEncap
;
4144 for (rn
= agg_route_top(rt
); rn
; rn
= agg_route_next(rn
)) {
4145 for (bi
= rn
->info
; bi
; bi
= bi
->next
) {
4146 if (bi
->peer
== peer
) {
4148 if (CHECK_FLAG(bi
->flags
, BGP_PATH_REMOVED
)) {
4149 /* already in holddown, skip */
4153 if (safi
== SAFI_MPLS_VPN
) {
4154 RFAPI_UPDATE_ITABLE_COUNT(
4155 bi
, import_table
, afi
, -1);
4156 import_table
->holddown_count
[afi
] += 1;
4158 rfapiBiStartWithdrawTimer(import_table
, rn
, bi
,
4160 timer_service_func
);
4168 * This gets called when a peer connection drops. We have to remove
4169 * all the routes from this peer.
4171 * Current approach is crude. TBD Optimize by setting fewer timers and
4172 * grouping withdrawn routes so we can generate callbacks more
4175 void rfapiProcessPeerDown(struct peer
*peer
)
4179 struct rfapi_import_table
*it
;
4182 * If this peer is a "dummy" peer structure atached to a RFAPI
4183 * nve_descriptor, we don't need to walk the import tables
4184 * because the routes are already withdrawn by rfapi_close()
4186 if (CHECK_FLAG(peer
->flags
, PEER_FLAG_IS_RFAPI_HD
))
4190 * 1. Visit all BIs in all ENCAP import tables.
4191 * Start withdraw timer on the BIs that match peer.
4193 * 2. Visit All BIs in all VPN import tables.
4194 * Start withdraw timer on the BIs that match peer.
4197 bgp
= bgp_get_default(); /* assume 1 instance for now */
4204 for (it
= h
->imports
; it
; it
= it
->next
) {
4205 rfapiProcessPeerDownRt(peer
, it
, AFI_IP
, SAFI_ENCAP
);
4206 rfapiProcessPeerDownRt(peer
, it
, AFI_IP6
, SAFI_ENCAP
);
4207 rfapiProcessPeerDownRt(peer
, it
, AFI_IP
, SAFI_MPLS_VPN
);
4208 rfapiProcessPeerDownRt(peer
, it
, AFI_IP6
, SAFI_MPLS_VPN
);
4212 rfapiProcessPeerDownRt(peer
, h
->it_ce
, AFI_IP
, SAFI_MPLS_VPN
);
4213 rfapiProcessPeerDownRt(peer
, h
->it_ce
, AFI_IP6
, SAFI_MPLS_VPN
);
4218 * Import an entire RIB (for an afi/safi) to an import table RIB,
4219 * filtered according to the import table's RT list
4221 * TBD: does this function need additions to match rfapiProcessUpdate()
4222 * for, e.g., L2 handling?
4224 static void rfapiBgpTableFilteredImport(struct bgp
*bgp
,
4225 struct rfapi_import_table
*it
,
4226 afi_t afi
, safi_t safi
)
4228 struct bgp_node
*rn1
;
4229 struct bgp_node
*rn2
;
4231 /* Only these SAFIs have 2-level RIBS */
4232 assert(safi
== SAFI_MPLS_VPN
|| safi
== SAFI_ENCAP
);
4235 * Now visit all the rd nodes and the nodes of all the
4236 * route tables attached to them, and import the routes
4237 * if they have matching route targets
4239 for (rn1
= bgp_table_top(bgp
->rib
[afi
][safi
]); rn1
;
4240 rn1
= bgp_route_next(rn1
)) {
4243 for (rn2
= bgp_table_top(rn1
->info
); rn2
;
4244 rn2
= bgp_route_next(rn2
)) {
4246 struct bgp_path_info
*bi
;
4248 for (bi
= rn2
->info
; bi
; bi
= bi
->next
) {
4251 if (CHECK_FLAG(bi
->flags
,
4256 label
= decode_label(
4257 &bi
->extra
->label
[0]);
4258 (*rfapiBgpInfoFilteredImportFunction(
4260 it
, /* which import table */
4261 FIF_ACTION_UPDATE
, bi
->peer
,
4262 NULL
, &rn2
->p
, /* prefix */
4264 (struct prefix_rd
*)&rn1
->p
,
4266 bi
->sub_type
, &label
);
4274 /* per-bgp-instance rfapi data */
4275 struct rfapi
*bgp_rfapi_new(struct bgp
*bgp
)
4279 struct rfapi_rfp_cfg
*cfg
= NULL
;
4280 struct rfapi_rfp_cb_methods
*cbm
= NULL
;
4282 assert(bgp
->rfapi_cfg
== NULL
);
4284 h
= (struct rfapi
*)XCALLOC(MTYPE_RFAPI
, sizeof(struct rfapi
));
4286 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
4287 h
->un
[afi
] = agg_table_init();
4291 * initialize the ce import table
4293 h
->it_ce
= XCALLOC(MTYPE_RFAPI_IMPORTTABLE
,
4294 sizeof(struct rfapi_import_table
));
4295 h
->it_ce
->imported_vpn
[AFI_IP
] = agg_table_init();
4296 h
->it_ce
->imported_vpn
[AFI_IP6
] = agg_table_init();
4297 h
->it_ce
->imported_encap
[AFI_IP
] = agg_table_init();
4298 h
->it_ce
->imported_encap
[AFI_IP6
] = agg_table_init();
4299 rfapiBgpTableFilteredImport(bgp
, h
->it_ce
, AFI_IP
, SAFI_MPLS_VPN
);
4300 rfapiBgpTableFilteredImport(bgp
, h
->it_ce
, AFI_IP6
, SAFI_MPLS_VPN
);
4303 * Set up work queue for deferred rfapi_close operations
4305 h
->deferred_close_q
=
4306 work_queue_new(bm
->master
, "rfapi deferred close");
4307 h
->deferred_close_q
->spec
.workfunc
= rfapi_deferred_close_workfunc
;
4308 h
->deferred_close_q
->spec
.data
= h
;
4310 h
->rfp
= rfp_start(bm
->master
, &cfg
, &cbm
);
4311 bgp
->rfapi_cfg
= bgp_rfapi_cfg_new(cfg
);
4313 h
->rfp_methods
= *cbm
;
4318 void bgp_rfapi_destroy(struct bgp
*bgp
, struct rfapi
*h
)
4322 if (bgp
== NULL
|| h
== NULL
)
4325 if (h
->resolve_nve_nexthop
) {
4326 skiplist_free(h
->resolve_nve_nexthop
);
4327 h
->resolve_nve_nexthop
= NULL
;
4330 agg_table_finish(h
->it_ce
->imported_vpn
[AFI_IP
]);
4331 agg_table_finish(h
->it_ce
->imported_vpn
[AFI_IP6
]);
4332 agg_table_finish(h
->it_ce
->imported_encap
[AFI_IP
]);
4333 agg_table_finish(h
->it_ce
->imported_encap
[AFI_IP6
]);
4335 if (h
->import_mac
) {
4336 struct rfapi_import_table
*it
;
4341 rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4343 !rc
; rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4346 rfapiImportTableFlush(it
);
4347 XFREE(MTYPE_RFAPI_IMPORTTABLE
, it
);
4349 skiplist_free(h
->import_mac
);
4350 h
->import_mac
= NULL
;
4353 work_queue_free_and_null(&h
->deferred_close_q
);
4358 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
4359 agg_table_finish(h
->un
[afi
]);
4362 XFREE(MTYPE_RFAPI_IMPORTTABLE
, h
->it_ce
);
4363 XFREE(MTYPE_RFAPI
, h
);
4366 struct rfapi_import_table
*
4367 rfapiImportTableRefAdd(struct bgp
*bgp
, struct ecommunity
*rt_import_list
,
4368 struct rfapi_nve_group_cfg
*rfg
)
4371 struct rfapi_import_table
*it
;
4377 for (it
= h
->imports
; it
; it
= it
->next
) {
4378 if (ecommunity_cmp(it
->rt_import_list
, rt_import_list
))
4382 vnc_zlog_debug_verbose("%s: matched it=%p", __func__
, it
);
4385 it
= XCALLOC(MTYPE_RFAPI_IMPORTTABLE
,
4386 sizeof(struct rfapi_import_table
));
4388 it
->next
= h
->imports
;
4391 it
->rt_import_list
= ecommunity_dup(rt_import_list
);
4393 it
->monitor_exterior_orphans
=
4394 skiplist_new(0, NULL
, (void (*)(void *))prefix_free
);
4397 * fill import route tables from RIBs
4399 * Potential area for optimization. If this occurs when
4400 * tables are large (e.g., the operator adds a nve group
4401 * with a new RT list to a running system), it could take
4405 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
4407 it
->imported_vpn
[afi
] = agg_table_init();
4408 it
->imported_encap
[afi
] = agg_table_init();
4410 rfapiBgpTableFilteredImport(bgp
, it
, afi
,
4412 rfapiBgpTableFilteredImport(bgp
, it
, afi
, SAFI_ENCAP
);
4414 vnc_import_bgp_exterior_redist_enable_it(bgp
, afi
, it
);
4424 * skiplist element free function
4426 static void delete_rem_pfx_na_free(void *na
)
4428 uint32_t *pCounter
= ((struct rfapi_nve_addr
*)na
)->info
;
4431 XFREE(MTYPE_RFAPI_NVE_ADDR
, na
);
4435 * Common deleter for IP and MAC import tables
4437 static void rfapiDeleteRemotePrefixesIt(
4438 struct bgp
*bgp
, struct rfapi_import_table
*it
, struct prefix
*un
,
4439 struct prefix
*vn
, struct prefix
*p
, int delete_active
,
4440 int delete_holddown
, uint32_t *pARcount
, uint32_t *pAHcount
,
4441 uint32_t *pHRcount
, uint32_t *pHHcount
,
4442 struct skiplist
*uniq_active_nves
, struct skiplist
*uniq_holddown_nves
)
4448 char buf_pfx
[PREFIX_STRLEN
];
4451 prefix2str(p
, buf_pfx
, sizeof(buf_pfx
));
4457 vnc_zlog_debug_verbose(
4458 "%s: entry, p=%s, delete_active=%d, delete_holddown=%d",
4459 __func__
, buf_pfx
, delete_active
, delete_holddown
);
4463 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
4465 struct agg_table
*rt
;
4466 struct agg_node
*rn
;
4468 if (p
&& (family2afi(p
->family
) != afi
)) {
4472 rt
= it
->imported_vpn
[afi
];
4476 vnc_zlog_debug_verbose("%s: scanning rt for afi=%d", __func__
,
4479 for (rn
= agg_route_top(rt
); rn
; rn
= agg_route_next(rn
)) {
4480 struct bgp_path_info
*bi
;
4481 struct bgp_path_info
*next
;
4483 if (VNC_DEBUG(IMPORT_DEL_REMOTE
)) {
4484 char p1line
[PREFIX_STRLEN
];
4485 char p2line
[PREFIX_STRLEN
];
4487 prefix2str(p
, p1line
, sizeof(p1line
));
4488 prefix2str(&rn
->p
, p2line
, sizeof(p2line
));
4489 vnc_zlog_debug_any("%s: want %s, have %s",
4490 __func__
, p1line
, p2line
);
4493 if (p
&& prefix_cmp(p
, &rn
->p
))
4497 char buf_pfx
[PREFIX_STRLEN
];
4499 prefix2str(&rn
->p
, buf_pfx
, sizeof(buf_pfx
));
4500 vnc_zlog_debug_verbose("%s: rn pfx=%s",
4504 /* TBD is this valid for afi == AFI_L2VPN? */
4505 RFAPI_CHECK_REFCOUNT(rn
, SAFI_MPLS_VPN
, 1);
4507 for (bi
= rn
->info
; bi
; bi
= next
) {
4516 vnc_zlog_debug_verbose("%s: examining bi %p",
4520 if (!rfapiGetNexthop(bi
->attr
, &qpt
))
4525 || !prefix_match(vn
, &qpt
)) {
4527 vnc_zlog_debug_verbose(
4528 "%s: continue at vn && !qpt_valid || !prefix_match(vn, &qpt)",
4535 if (!rfapiGetUnAddrOfVpnBi(bi
, &qct
))
4540 || !prefix_match(un
, &qct
)) {
4542 vnc_zlog_debug_verbose(
4543 "%s: continue at un && !qct_valid || !prefix_match(un, &qct)",
4555 * If this route is waiting to be deleted
4557 * a previous withdraw, we must cancel its
4560 if (CHECK_FLAG(bi
->flags
, BGP_PATH_REMOVED
)) {
4561 if (!delete_holddown
)
4563 if (bi
->extra
->vnc
.import
.timer
) {
4569 struct rfapi_withdraw
*wcb
=
4573 ->holddown_count
[afi
] -=
4575 RFAPI_UPDATE_ITABLE_COUNT(
4576 bi
, wcb
->import_table
,
4578 XFREE(MTYPE_RFAPI_WITHDRAW
,
4588 vnc_zlog_debug_verbose(
4589 "%s: deleting bi %p (qct_valid=%d, qpt_valid=%d, delete_holddown=%d, delete_active=%d)",
4590 __func__
, bi
, qct_valid
, qpt_valid
,
4591 delete_holddown
, delete_active
);
4597 if (qct_valid
&& qpt_valid
) {
4599 struct rfapi_nve_addr na
;
4600 struct rfapi_nve_addr
*nap
;
4602 memset(&na
, 0, sizeof(na
));
4603 assert(!rfapiQprefix2Raddr(&qct
,
4605 assert(!rfapiQprefix2Raddr(&qpt
,
4608 if (skiplist_search(
4611 : uniq_holddown_nves
),
4612 &na
, (void **)&nap
)) {
4616 MTYPE_RFAPI_NVE_ADDR
,
4621 nap
->info
= is_active
4627 : uniq_holddown_nves
),
4630 rfapiNveAddr2Str(nap
, line
,
4635 vnc_direct_bgp_rh_del_route(bgp
, afi
, &rn
->p
,
4638 RFAPI_UPDATE_ITABLE_COUNT(bi
, it
, afi
, -1);
4639 it
->holddown_count
[afi
] += 1;
4640 rfapiExpireVpnNow(it
, rn
, bi
, 1);
4642 vnc_zlog_debug_verbose(
4643 "%s: incrementing count (is_active=%d)",
4644 __func__
, is_active
);
4657 * For use by the "clear vnc prefixes" command
4659 /*------------------------------------------
4660 * rfapiDeleteRemotePrefixes
4662 * UI helper: For use by the "clear vnc prefixes" command
4665 * un if set, tunnel must match this prefix
4666 * vn if set, nexthop prefix must match this prefix
4667 * p if set, prefix must match this prefix
4668 * it if set, only look in this import table
4671 * pARcount number of active routes deleted
4672 * pAHcount number of active nves deleted
4673 * pHRcount number of holddown routes deleted
4674 * pHHcount number of holddown nves deleted
4678 --------------------------------------------*/
4679 void rfapiDeleteRemotePrefixes(struct prefix
*un
, struct prefix
*vn
,
4681 struct rfapi_import_table
*arg_it
,
4682 int delete_active
, int delete_holddown
,
4683 uint32_t *pARcount
, uint32_t *pAHcount
,
4684 uint32_t *pHRcount
, uint32_t *pHHcount
)
4688 struct rfapi_import_table
*it
;
4689 uint32_t deleted_holddown_route_count
= 0;
4690 uint32_t deleted_active_route_count
= 0;
4691 uint32_t deleted_holddown_nve_count
= 0;
4692 uint32_t deleted_active_nve_count
= 0;
4693 struct skiplist
*uniq_holddown_nves
;
4694 struct skiplist
*uniq_active_nves
;
4698 bgp
= bgp_get_default(); /* assume 1 instance for now */
4699 /* If no bgp instantiated yet, no vnc prefixes exist */
4706 uniq_holddown_nves
=
4707 skiplist_new(0, rfapi_nve_addr_cmp
, delete_rem_pfx_na_free
);
4709 skiplist_new(0, rfapi_nve_addr_cmp
, delete_rem_pfx_na_free
);
4712 * Iterate over all import tables; do a filtered import
4713 * for the afi/safi combination
4722 vnc_zlog_debug_verbose(
4723 "%s: calling rfapiDeleteRemotePrefixesIt() on (IP) import %p",
4726 rfapiDeleteRemotePrefixesIt(
4727 bgp
, it
, un
, vn
, p
, delete_active
, delete_holddown
,
4728 &deleted_active_route_count
, &deleted_active_nve_count
,
4729 &deleted_holddown_route_count
,
4730 &deleted_holddown_nve_count
, uniq_active_nves
,
4731 uniq_holddown_nves
);
4740 * Now iterate over L2 import tables
4742 if (h
->import_mac
&& !(p
&& (p
->family
!= AF_ETHERNET
))) {
4744 void *cursor
= NULL
;
4748 rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4750 !rc
; rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4753 vnc_zlog_debug_verbose(
4754 "%s: calling rfapiDeleteRemotePrefixesIt() on import_mac %p",
4757 rfapiDeleteRemotePrefixesIt(
4758 bgp
, it
, un
, vn
, p
, delete_active
,
4759 delete_holddown
, &deleted_active_route_count
,
4760 &deleted_active_nve_count
,
4761 &deleted_holddown_route_count
,
4762 &deleted_holddown_nve_count
, uniq_active_nves
,
4763 uniq_holddown_nves
);
4768 * our custom element freeing function above counts as it deletes
4770 skiplist_free(uniq_holddown_nves
);
4771 skiplist_free(uniq_active_nves
);
4774 *pARcount
= deleted_active_route_count
;
4776 *pAHcount
= deleted_active_nve_count
;
4778 *pHRcount
= deleted_holddown_route_count
;
4780 *pHHcount
= deleted_holddown_nve_count
;
4785 /*------------------------------------------
4786 * rfapiCountRemoteRoutes
4788 * UI helper: count VRF routes from BGP side
4793 * pALRcount count of active local routes
4794 * pARRcount count of active remote routes
4795 * pHRcount count of holddown routes
4796 * pIRcount count of direct imported routes
4800 --------------------------------------------*/
4801 void rfapiCountAllItRoutes(int *pALRcount
, /* active local routes */
4802 int *pARRcount
, /* active remote routes */
4803 int *pHRcount
, /* holddown routes */
4804 int *pIRcount
) /* imported routes */
4808 struct rfapi_import_table
*it
;
4811 int total_active_local
= 0;
4812 int total_active_remote
= 0;
4813 int total_holddown
= 0;
4814 int total_imported
= 0;
4816 bgp
= bgp_get_default(); /* assume 1 instance for now */
4823 * Iterate over all import tables; do a filtered import
4824 * for the afi/safi combination
4827 for (it
= h
->imports
; it
; it
= it
->next
) {
4829 for (afi
= AFI_IP
; afi
< AFI_MAX
; ++afi
) {
4831 total_active_local
+= it
->local_count
[afi
];
4832 total_active_remote
+= it
->remote_count
[afi
];
4833 total_holddown
+= it
->holddown_count
[afi
];
4834 total_imported
+= it
->imported_count
[afi
];
4841 if (h
->import_mac
) {
4843 rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4845 !rc
; rc
= skiplist_next(h
->import_mac
, NULL
, (void **)&it
,
4848 total_active_local
+= it
->local_count
[AFI_L2VPN
];
4849 total_active_remote
+= it
->remote_count
[AFI_L2VPN
];
4850 total_holddown
+= it
->holddown_count
[AFI_L2VPN
];
4851 total_imported
+= it
->imported_count
[AFI_L2VPN
];
4857 *pALRcount
= total_active_local
;
4860 *pARRcount
= total_active_remote
;
4863 *pHRcount
= total_holddown
;
4866 *pIRcount
= total_imported
;
4870 /*------------------------------------------
4871 * rfapiGetHolddownFromLifetime
4873 * calculate holddown value based on lifetime
4879 * Holddown value based on lifetime, holddown_factor,
4880 * and RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY
4882 --------------------------------------------*/
4883 /* hold down time maxes out at RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY */
4884 uint32_t rfapiGetHolddownFromLifetime(uint32_t lifetime
)
4889 bgp
= bgp_get_default();
4890 if (bgp
&& bgp
->rfapi_cfg
)
4891 factor
= bgp
->rfapi_cfg
->rfp_cfg
.holddown_factor
;
4893 factor
= RFAPI_RFP_CFG_DEFAULT_HOLDDOWN_FACTOR
;
4895 if (factor
< 100 || lifetime
< RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY
)
4896 lifetime
= lifetime
* factor
/ 100;
4897 if (lifetime
< RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY
)
4900 return RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY
;