]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/rfapi/rfapi_import.c
bgpd: Remove double test against rfapi which is already checked
[mirror_frr.git] / bgpd / rfapi / rfapi_import.c
CommitLineData
d62a17ae 1/*
9d303b37
DL
2*
3* Copyright 2009-2016, LabN Consulting, L.L.C.
4*
5*
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.
10*
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.
15*
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
19*/
65efcfce
LB
20
21/*
22 * File: rfapi_import.c
23 * Purpose: Handle import of routes from BGP to RFAPI
24 */
25
f8b6f499
LB
26#include "lib/zebra.h"
27#include "lib/prefix.h"
fe08ba7e 28#include "lib/agg_table.h"
f8b6f499
LB
29#include "lib/vty.h"
30#include "lib/memory.h"
31#include "lib/log.h"
32#include "lib/skiplist.h"
33#include "lib/thread.h"
937652c6 34#include "lib/stream.h"
02705213 35#include "lib/lib_errors.h"
65efcfce 36
f8b6f499
LB
37#include "bgpd/bgpd.h"
38#include "bgpd/bgp_ecommunity.h"
39#include "bgpd/bgp_attr.h"
40#include "bgpd/bgp_route.h"
d62a17ae 41#include "bgpd/bgp_mplsvpn.h" /* prefix_rd2str() */
f8b6f499 42#include "bgpd/bgp_vnc_types.h"
06b9f471 43#include "bgpd/bgp_rd.h"
65efcfce 44
f8b6f499
LB
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"
65efcfce
LB
61
62#ifdef HAVE_GLIBC_BACKTRACE
63/* for backtrace and friends */
64#include <execinfo.h>
65#endif /* HAVE_GLIBC_BACKTRACE */
66
67#undef DEBUG_MONITOR_MOVE_SHORTER
68#undef DEBUG_RETURNED_NHL
69#undef DEBUG_ROUTE_COUNTERS
70#undef DEBUG_ENCAP_MONITOR
71#undef DEBUG_L2_EXTRA
72#undef DEBUG_IT_NODES
73#undef DEBUG_BI_SEARCH
74
75/*
76 * Allocated for each withdraw timer instance; freed when the timer
77 * expires or is canceled
78 */
d62a17ae 79struct rfapi_withdraw {
80 struct rfapi_import_table *import_table;
fe08ba7e 81 struct agg_node *node;
4b7e6066 82 struct bgp_path_info *info;
d62a17ae 83 safi_t safi; /* used only for bulk operations */
84 /*
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
fe08ba7e
DS
89 * agg_route_top() or agg_route_next() in a loop, so we need to pass
90 * this value in.
d62a17ae 91 */
92 int lockoffset;
65efcfce
LB
93};
94
d62a17ae 95/*
65efcfce
LB
96 * DEBUG FUNCTION
97 * It's evil and fiendish. It's compiler-dependent.
98 * ? Might need LDFLAGS -rdynamic to produce all function names
99 */
d62a17ae 100void rfapiDebugBacktrace(void)
65efcfce
LB
101{
102#ifdef HAVE_GLIBC_BACKTRACE
103#define RFAPI_DEBUG_BACKTRACE_NENTRIES 200
d62a17ae 104 void *buf[RFAPI_DEBUG_BACKTRACE_NENTRIES];
105 char **syms;
106 size_t i;
107 size_t size;
65efcfce 108
d62a17ae 109 size = backtrace(buf, RFAPI_DEBUG_BACKTRACE_NENTRIES);
110 syms = backtrace_symbols(buf, size);
65efcfce 111
d62a17ae 112 for (i = 0; i < size && i < RFAPI_DEBUG_BACKTRACE_NENTRIES; ++i) {
113 vnc_zlog_debug_verbose("backtrace[%2zu]: %s", i, syms[i]);
114 }
65efcfce 115
d62a17ae 116 free(syms);
65efcfce
LB
117#else
118#endif
119}
120
121/*
122 * DEBUG FUNCTION
123 * Count remote routes and compare with actively-maintained values.
124 * Abort if they disagree.
125 */
4d762f26 126void rfapiCheckRouteCount(void)
65efcfce 127{
d62a17ae 128 struct bgp *bgp = bgp_get_default();
129 struct rfapi *h;
130 struct rfapi_import_table *it;
131 afi_t afi;
132
133 assert(bgp);
134
135 h = bgp->rfapi;
136 assert(h);
137
138 for (it = h->imports; it; it = it->next) {
139 for (afi = AFI_IP; afi < AFI_MAX; ++afi) {
140
fe08ba7e
DS
141 struct agg_table *rt;
142 struct agg_node *rn;
d62a17ae 143
144 int holddown_count = 0;
145 int local_count = 0;
146 int imported_count = 0;
147 int remote_count = 0;
148
149 rt = it->imported_vpn[afi];
150
fe08ba7e
DS
151 for (rn = agg_route_top(rt); rn;
152 rn = agg_route_next(rn)) {
40381db7 153 struct bgp_path_info *bpi;
4b7e6066 154 struct bgp_path_info *next;
d62a17ae 155
40381db7
DS
156 for (bpi = rn->info; bpi; bpi = next) {
157 next = bpi->next;
d62a17ae 158
40381db7 159 if (CHECK_FLAG(bpi->flags,
1defdda8 160 BGP_PATH_REMOVED)) {
d62a17ae 161 ++holddown_count;
162
163 } else {
40381db7 164 if (RFAPI_LOCAL_BI(bpi)) {
d62a17ae 165 ++local_count;
166 } else {
167 if (RFAPI_DIRECT_IMPORT_BI(
40381db7 168 bpi)) {
d62a17ae 169 ++imported_count;
170 } else {
171 ++remote_count;
172 }
173 }
174 }
175 }
176 }
177
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],
182 holddown_count);
183 assert(0);
184 }
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],
189 remote_count);
190 assert(0);
191 }
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],
196 imported_count);
197 assert(0);
198 }
199 }
200 }
65efcfce
LB
201}
202
1e20238a 203#ifdef DEBUG_ROUTE_COUNTERS
65efcfce
LB
204#define VNC_ITRCCK do {rfapiCheckRouteCount();} while (0)
205#else
206#define VNC_ITRCCK
207#endif
208
209/*
210 * Validate reference count for a node in an import table
211 *
212 * Normally lockoffset is 0 for nodes in quiescent state. However,
fe08ba7e 213 * agg_unlock_node will delete the node if it is called when
65efcfce
LB
214 * node->lock == 1, and we have to validate the refcount before
215 * the node is deleted. In this case, we specify lockoffset 1.
216 */
fe08ba7e 217void rfapiCheckRefcount(struct agg_node *rn, safi_t safi, int lockoffset)
65efcfce 218{
40381db7 219 unsigned int count_bpi = 0;
d62a17ae 220 unsigned int count_monitor = 0;
40381db7 221 struct bgp_path_info *bpi;
d62a17ae 222 struct rfapi_monitor_encap *hme;
223 struct rfapi_monitor_vpn *hmv;
224
40381db7
DS
225 for (bpi = rn->info; bpi; bpi = bpi->next)
226 ++count_bpi;
d62a17ae 227
228
229 if (rn->aggregate) {
230 ++count_monitor; /* rfapi_it_extra */
231
232 switch (safi) {
233 void *cursor;
234 int rc;
235
236 case SAFI_ENCAP:
237 for (hme = RFAPI_MONITOR_ENCAP(rn); hme;
238 hme = hme->next)
239 ++count_monitor;
240 break;
241
242 case SAFI_MPLS_VPN:
243
244 for (hmv = RFAPI_MONITOR_VPN(rn); hmv; hmv = hmv->next)
245 ++count_monitor;
246
247 if (RFAPI_MONITOR_EXTERIOR(rn)->source) {
248 ++count_monitor; /* sl */
249 cursor = NULL;
250 for (rc = skiplist_next(
251 RFAPI_MONITOR_EXTERIOR(rn)->source,
252 NULL, NULL, &cursor);
253 !rc;
254 rc = skiplist_next(
255 RFAPI_MONITOR_EXTERIOR(rn)->source,
256 NULL, NULL, &cursor)) {
257
258 ++count_monitor; /* sl entry */
259 }
260 }
261 break;
262
263 default:
264 assert(0);
265 }
266 }
267
c10e14e9
DS
268 if (count_bpi + count_monitor + lockoffset
269 != agg_node_get_lock_count(rn)) {
d62a17ae 270 vnc_zlog_debug_verbose(
40381db7
DS
271 "%s: count_bpi=%d, count_monitor=%d, lockoffset=%d, rn->lock=%d",
272 __func__, count_bpi, count_monitor, lockoffset,
c10e14e9 273 agg_node_get_lock_count(rn));
d62a17ae 274 assert(0);
275 }
65efcfce
LB
276}
277
278/*
279 * Perform deferred rfapi_close operations that were queued
280 * during callbacks.
281 */
d62a17ae 282static wq_item_status rfapi_deferred_close_workfunc(struct work_queue *q,
283 void *data)
65efcfce 284{
d62a17ae 285 struct rfapi_descriptor *rfd = data;
286 struct rfapi *h = q->spec.data;
287
288 assert(!(h->flags & RFAPI_INCALLBACK));
289 rfapi_close(rfd);
290 vnc_zlog_debug_verbose("%s: completed deferred close on handle %p",
291 __func__, rfd);
292 return WQ_SUCCESS;
65efcfce
LB
293}
294
295/*
296 * Extract layer 2 option from Encap TLVS in BGP attrs
297 */
d62a17ae 298int rfapiGetL2o(struct attr *attr, struct rfapi_l2address_option *l2o)
65efcfce 299{
d62a17ae 300 if (attr) {
d62a17ae 301 struct bgp_attr_encap_subtlv *pEncap;
302
91ebf12c 303 for (pEncap = bgp_attr_get_vnc_subtlvs(attr); pEncap;
d62a17ae 304 pEncap = pEncap->next) {
305
306 if (pEncap->type == BGP_VNC_SUBTLV_TYPE_RFPOPTION) {
307 if (pEncap->value[0]
308 == RFAPI_VN_OPTION_TYPE_L2ADDR) {
309
310 if (pEncap->value[1] == 14) {
311 memcpy(l2o->macaddr.octet,
312 pEncap->value + 2,
28328ea9 313 ETH_ALEN);
d62a17ae 314 l2o->label =
315 ((pEncap->value[10]
316 >> 4)
317 & 0x0f)
318 + ((pEncap->value[9]
319 << 4)
320 & 0xff0)
321 + ((pEncap->value[8]
322 << 12)
323 & 0xff000);
324
325 l2o->local_nve_id =
326 pEncap->value[12];
327
328 l2o->logical_net_id =
329 (pEncap->value[15]
330 & 0xff)
331 + ((pEncap->value[14]
332 << 8)
333 & 0xff00)
334 + ((pEncap->value[13]
335 << 16)
336 & 0xff0000);
337 }
338
339 return 0;
340 }
341 }
342 }
343 }
344
345 return ENOENT;
65efcfce
LB
346}
347
348/*
349 * Extract the lifetime from the Tunnel Encap attribute of a route in
350 * an import table
351 */
d62a17ae 352int rfapiGetVncLifetime(struct attr *attr, uint32_t *lifetime)
65efcfce 353{
d62a17ae 354 struct bgp_attr_encap_subtlv *pEncap;
65efcfce 355
d62a17ae 356 *lifetime = RFAPI_INFINITE_LIFETIME; /* default to infinite */
65efcfce 357
d62a17ae 358 if (attr) {
65efcfce 359
91ebf12c 360 for (pEncap = bgp_attr_get_vnc_subtlvs(attr); pEncap;
d62a17ae 361 pEncap = pEncap->next) {
65efcfce 362
d62a17ae 363 if (pEncap->type
364 == BGP_VNC_SUBTLV_TYPE_LIFETIME) { /* lifetime */
365 if (pEncap->length == 4) {
366 memcpy(lifetime, pEncap->value, 4);
367 *lifetime = ntohl(*lifetime);
368 return 0;
369 }
370 }
371 }
372 }
65efcfce 373
d62a17ae 374 return ENOENT;
65efcfce
LB
375}
376
65efcfce
LB
377/*
378 * Look for UN address in Encap attribute
379 */
d62a17ae 380int rfapiGetVncTunnelUnAddr(struct attr *attr, struct prefix *p)
65efcfce 381{
d62a17ae 382 struct bgp_attr_encap_subtlv *pEncap;
f4bd90c5 383 bgp_encap_types tun_type = BGP_ENCAP_TYPE_MPLS;/*Default tunnel type*/
d62a17ae 384
f4bd90c5 385 bgp_attr_extcom_tunnel_type(attr, &tun_type);
d62a17ae 386 if (tun_type == BGP_ENCAP_TYPE_MPLS) {
387 if (!p)
388 return 0;
389 /* MPLS carries UN address in next hop */
390 rfapiNexthop2Prefix(attr, p);
391 if (p->family != 0)
392 return 0;
393
394 return ENOENT;
395 }
396 if (attr) {
397 for (pEncap = attr->encap_subtlvs; pEncap;
398 pEncap = pEncap->next) {
399
400 if (pEncap->type
401 == BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT) { /* un
402 addr
403 */
404 switch (pEncap->length) {
405 case 8:
406 if (p) {
407 p->family = AF_INET;
408 p->prefixlen = 32;
409 memcpy(p->u.val, pEncap->value,
410 4);
411 }
412 return 0;
413
414 case 20:
415 if (p) {
416 p->family = AF_INET6;
417 p->prefixlen = 128;
418 memcpy(p->u.val, pEncap->value,
419 16);
420 }
421 return 0;
422 }
423 }
424 }
425 }
426
427 return ENOENT;
65efcfce
LB
428}
429
430/*
431 * Get UN address wherever it might be
432 */
40381db7 433int rfapiGetUnAddrOfVpnBi(struct bgp_path_info *bpi, struct prefix *p)
65efcfce 434{
d62a17ae 435 /* If it's in this route's VNC attribute, we're done */
40381db7 436 if (!rfapiGetVncTunnelUnAddr(bpi->attr, p))
d62a17ae 437 return 0;
438 /*
439 * Otherwise, see if it's cached from a corresponding ENCAP SAFI
440 * advertisement
441 */
40381db7
DS
442 if (bpi->extra) {
443 switch (bpi->extra->vnc.import.un_family) {
d62a17ae 444 case AF_INET:
445 if (p) {
40381db7
DS
446 p->family = bpi->extra->vnc.import.un_family;
447 p->u.prefix4 = bpi->extra->vnc.import.un.addr4;
d62a17ae 448 p->prefixlen = 32;
449 }
450 return 0;
451 case AF_INET6:
452 if (p) {
40381db7
DS
453 p->family = bpi->extra->vnc.import.un_family;
454 p->u.prefix6 = bpi->extra->vnc.import.un.addr6;
d62a17ae 455 p->prefixlen = 128;
456 }
457 return 0;
458 default:
459 if (p)
460 p->family = 0;
1e20238a 461#ifdef DEBUG_ENCAP_MONITOR
d62a17ae 462 vnc_zlog_debug_verbose(
40381db7 463 "%s: bpi->extra->vnc.import.un_family is 0, no UN addr",
d62a17ae 464 __func__);
65efcfce 465#endif
d62a17ae 466 break;
467 }
468 }
65efcfce 469
d62a17ae 470 return ENOENT;
65efcfce
LB
471}
472
473
474/*
9b6d8fcf 475 * Make a new bgp_path_info from gathered parameters
65efcfce 476 */
4b7e6066
DS
477static struct bgp_path_info *rfapiBgpInfoCreate(struct attr *attr,
478 struct peer *peer, void *rfd,
479 struct prefix_rd *prd,
480 uint8_t type, uint8_t sub_type,
481 uint32_t *label)
65efcfce 482{
4b7e6066 483 struct bgp_path_info *new;
d62a17ae 484
9591da26
DS
485 new = info_make(type, sub_type, 0, peer, attr, NULL);
486
05864da7 487 new->attr = bgp_attr_intern(attr);
d62a17ae 488
18ee8310 489 bgp_path_info_extra_get(new);
d62a17ae 490 if (prd) {
491 new->extra->vnc.import.rd = *prd;
492 rfapi_time(&new->extra->vnc.import.create_time);
493 }
494 if (label)
317f1fe0 495 encode_label(*label, &new->extra->label[0]);
9591da26 496
d62a17ae 497 peer_lock(peer);
498
499 return new;
65efcfce
LB
500}
501
502/*
9b6d8fcf 503 * Frees bgp_path_info as used in import tables (parts are not
65efcfce
LB
504 * allocated exactly the way they are in the main RIBs)
505 */
4b7e6066 506static void rfapiBgpInfoFree(struct bgp_path_info *goner)
65efcfce 507{
d62a17ae 508 if (!goner)
509 return;
510
511 if (goner->peer) {
512 vnc_zlog_debug_verbose("%s: calling peer_unlock(%p), #%d",
513 __func__, goner->peer,
514 goner->peer->lock);
515 peer_unlock(goner->peer);
516 }
517
05864da7
DS
518 bgp_attr_unintern(&goner->attr);
519
a2e219fe
PG
520 if (goner->extra)
521 bgp_path_info_extra_free(&goner->extra);
d62a17ae 522 XFREE(MTYPE_BGP_ROUTE, goner);
65efcfce
LB
523}
524
d62a17ae 525struct rfapi_import_table *rfapiMacImportTableGetNoAlloc(struct bgp *bgp,
526 uint32_t lni)
65efcfce 527{
d62a17ae 528 struct rfapi *h;
529 struct rfapi_import_table *it = NULL;
530 uintptr_t lni_as_ptr = lni;
65efcfce 531
d62a17ae 532 h = bgp->rfapi;
533 if (!h)
534 return NULL;
65efcfce 535
d62a17ae 536 if (!h->import_mac)
537 return NULL;
65efcfce 538
d62a17ae 539 if (skiplist_search(h->import_mac, (void *)lni_as_ptr, (void **)&it))
540 return NULL;
65efcfce 541
d62a17ae 542 return it;
65efcfce
LB
543}
544
d62a17ae 545struct rfapi_import_table *rfapiMacImportTableGet(struct bgp *bgp, uint32_t lni)
65efcfce 546{
d62a17ae 547 struct rfapi *h;
548 struct rfapi_import_table *it = NULL;
549 uintptr_t lni_as_ptr = lni;
550
551 h = bgp->rfapi;
552 assert(h);
553
554 if (!h->import_mac) {
555 /* default cmp is good enough for LNI */
556 h->import_mac = skiplist_new(0, NULL, NULL);
557 }
558
559 if (skiplist_search(h->import_mac, (void *)lni_as_ptr, (void **)&it)) {
560
561 struct ecommunity *enew;
562 struct ecommunity_val eval;
563 afi_t afi;
564
565 it = XCALLOC(MTYPE_RFAPI_IMPORTTABLE,
566 sizeof(struct rfapi_import_table));
567 /* set RT list of new import table based on LNI */
568 memset((char *)&eval, 0, sizeof(eval));
569 eval.val[0] = 0; /* VNC L2VPN */
570 eval.val[1] = 2; /* VNC L2VPN */
571 eval.val[5] = (lni >> 16) & 0xff;
572 eval.val[6] = (lni >> 8) & 0xff;
573 eval.val[7] = (lni >> 0) & 0xff;
574
575 enew = ecommunity_new();
1207a5bc 576 ecommunity_add_val(enew, &eval, false, false);
d62a17ae 577 it->rt_import_list = enew;
578
579 for (afi = AFI_IP; afi < AFI_MAX; ++afi) {
fe08ba7e
DS
580 it->imported_vpn[afi] = agg_table_init();
581 it->imported_encap[afi] = agg_table_init();
d62a17ae 582 }
583
584 it->l2_logical_net_id = lni;
585
586 skiplist_insert(h->import_mac, (void *)lni_as_ptr, it);
587 }
588
589 assert(it);
590 return it;
65efcfce
LB
591}
592
593/*
594 * Implement MONITOR_MOVE_SHORTER(original_node) from
595 * RFAPI-Import-Event-Handling.txt
d62a17ae 596 *
65efcfce
LB
597 * Returns pointer to the list of moved monitors
598 */
599static struct rfapi_monitor_vpn *
fe08ba7e 600rfapiMonitorMoveShorter(struct agg_node *original_vpn_node, int lockoffset)
65efcfce 601{
40381db7 602 struct bgp_path_info *bpi;
fe08ba7e 603 struct agg_node *par;
d62a17ae 604 struct rfapi_monitor_vpn *m;
605 struct rfapi_monitor_vpn *mlast;
606 struct rfapi_monitor_vpn *moved;
607 int movecount = 0;
608 int parent_already_refcounted = 0;
65efcfce 609
d62a17ae 610 RFAPI_CHECK_REFCOUNT(original_vpn_node, SAFI_MPLS_VPN, lockoffset);
65efcfce 611
1e20238a 612#ifdef DEBUG_MONITOR_MOVE_SHORTER
d62a17ae 613 {
2dbe669b
DA
614 vnc_zlog_debug_verbose("%s: called with node pfx=%pFX",
615 __func__, &original_vpn_node->p);
d62a17ae 616 }
65efcfce
LB
617#endif
618
d62a17ae 619 /*
40381db7 620 * 1. If there is at least one bpi (either regular route or
d62a17ae 621 * route marked as withdrawn, with a pending timer) at
622 * original_node with a valid UN address, we're done. Return.
623 */
40381db7 624 for (bpi = original_vpn_node->info; bpi; bpi = bpi->next) {
d62a17ae 625 struct prefix pfx;
626
40381db7 627 if (!rfapiGetUnAddrOfVpnBi(bpi, &pfx)) {
1e20238a 628#ifdef DEBUG_MONITOR_MOVE_SHORTER
d62a17ae 629 vnc_zlog_debug_verbose(
630 "%s: have valid UN at original node, no change",
631 __func__);
65efcfce 632#endif
d62a17ae 633 return NULL;
634 }
635 }
636
637 /*
638 * 2. Travel up the tree (toward less-specific prefixes) from
639 * original_node to find the first node that has at least
640 * one route (even if it is only a withdrawn route) with a
641 * valid UN address. Call this node "Node P."
642 */
fe08ba7e
DS
643 for (par = agg_node_parent(original_vpn_node); par;
644 par = agg_node_parent(par)) {
40381db7 645 for (bpi = par->info; bpi; bpi = bpi->next) {
d62a17ae 646 struct prefix pfx;
40381db7 647 if (!rfapiGetUnAddrOfVpnBi(bpi, &pfx)) {
d62a17ae 648 break;
649 }
650 }
40381db7 651 if (bpi)
d62a17ae 652 break;
653 }
654
655 if (par) {
656 RFAPI_CHECK_REFCOUNT(par, SAFI_MPLS_VPN, 0);
657 }
658
659 /*
660 * If no less-specific routes, try to use the 0/0 node
661 */
662 if (!par) {
26a3ffd6 663 const struct prefix *p;
d62a17ae 664 /* this isn't necessarily 0/0 */
fe08ba7e 665 par = agg_route_table_top(original_vpn_node);
d62a17ae 666
26a3ffd6
DS
667 if (par)
668 p = agg_node_get_prefix(par);
d62a17ae 669 /*
670 * If we got the top node but it wasn't 0/0,
671 * ignore it
672 */
26a3ffd6 673 if (par && p->prefixlen) {
fe08ba7e 674 agg_unlock_node(par); /* maybe free */
d62a17ae 675 par = NULL;
676 }
677
678 if (par) {
679 ++parent_already_refcounted;
680 }
681 }
682
683 /*
684 * Create 0/0 node if it isn't there
685 */
686 if (!par) {
687 struct prefix pfx_default;
26a3ffd6 688 const struct prefix *p = agg_node_get_prefix(original_vpn_node);
d62a17ae 689
690 memset(&pfx_default, 0, sizeof(pfx_default));
26a3ffd6 691 pfx_default.family = p->family;
d62a17ae 692
693 /* creates default node if none exists */
fe08ba7e
DS
694 par = agg_node_get(agg_get_table(original_vpn_node),
695 &pfx_default);
d62a17ae 696 ++parent_already_refcounted;
697 }
698
699 /*
700 * 3. Move each of the monitors found at original_node to Node P.
701 * These are "Moved Monitors."
702 *
703 */
704
705 /*
706 * Attach at end so that the list pointer we return points
707 * only to the moved routes
708 */
709 for (m = RFAPI_MONITOR_VPN(par), mlast = NULL; m;
710 mlast = m, m = m->next)
711 ;
712
713 if (mlast) {
714 moved = mlast->next = RFAPI_MONITOR_VPN(original_vpn_node);
715 } else {
716 moved = RFAPI_MONITOR_VPN_W_ALLOC(par) =
717 RFAPI_MONITOR_VPN(original_vpn_node);
718 }
719 if (RFAPI_MONITOR_VPN(
720 original_vpn_node)) /* check agg, so not allocated */
721 RFAPI_MONITOR_VPN_W_ALLOC(original_vpn_node) = NULL;
722
723 /*
724 * update the node pointers on the monitors
725 */
726 for (m = moved; m; m = m->next) {
727 ++movecount;
728 m->node = par;
729 }
730
731 RFAPI_CHECK_REFCOUNT(par, SAFI_MPLS_VPN,
732 parent_already_refcounted - movecount);
733 while (movecount > parent_already_refcounted) {
fe08ba7e 734 agg_lock_node(par);
d62a17ae 735 ++parent_already_refcounted;
736 }
737 while (movecount < parent_already_refcounted) {
738 /* unlikely, but code defensively */
fe08ba7e 739 agg_unlock_node(par);
d62a17ae 740 --parent_already_refcounted;
741 }
742 RFAPI_CHECK_REFCOUNT(original_vpn_node, SAFI_MPLS_VPN,
743 movecount + lockoffset);
744 while (movecount--) {
fe08ba7e 745 agg_unlock_node(original_vpn_node);
d62a17ae 746 }
65efcfce 747
1e20238a 748#ifdef DEBUG_MONITOR_MOVE_SHORTER
d62a17ae 749 {
2dbe669b
DA
750 vnc_zlog_debug_verbose("%s: moved to node pfx=%pFX", __func__,
751 &par->p);
d62a17ae 752 }
65efcfce
LB
753#endif
754
755
d62a17ae 756 return moved;
65efcfce
LB
757}
758
759/*
760 * Implement MONITOR_MOVE_LONGER(new_node) from
761 * RFAPI-Import-Event-Handling.txt
762 */
fe08ba7e 763static void rfapiMonitorMoveLonger(struct agg_node *new_vpn_node)
65efcfce 764{
d62a17ae 765 struct rfapi_monitor_vpn *monitor;
766 struct rfapi_monitor_vpn *mlast;
40381db7 767 struct bgp_path_info *bpi;
fe08ba7e 768 struct agg_node *par;
26a3ffd6 769 const struct prefix *new_vpn_node_p = agg_node_get_prefix(new_vpn_node);
d62a17ae 770
771 RFAPI_CHECK_REFCOUNT(new_vpn_node, SAFI_MPLS_VPN, 0);
772
773 /*
774 * Make sure we have at least one valid route at the new node
775 */
40381db7 776 for (bpi = new_vpn_node->info; bpi; bpi = bpi->next) {
d62a17ae 777 struct prefix pfx;
40381db7 778 if (!rfapiGetUnAddrOfVpnBi(bpi, &pfx))
d62a17ae 779 break;
780 }
781
40381db7 782 if (!bpi) {
d62a17ae 783 vnc_zlog_debug_verbose(
784 "%s: no valid routes at node %p, so not attempting moves",
785 __func__, new_vpn_node);
786 return;
787 }
788
789 /*
790 * Find first parent node that has monitors
791 */
fe08ba7e
DS
792 for (par = agg_node_parent(new_vpn_node); par;
793 par = agg_node_parent(par)) {
d62a17ae 794 if (RFAPI_MONITOR_VPN(par))
795 break;
796 }
797
798 if (!par) {
799 vnc_zlog_debug_verbose(
800 "%s: no parent nodes with monitors, done", __func__);
801 return;
802 }
803
804 /*
805 * Check each of these monitors to see of their longest-match
806 * is now the updated node. Move any such monitors to the more-
807 * specific updated node
808 */
809 for (mlast = NULL, monitor = RFAPI_MONITOR_VPN(par); monitor;) {
d62a17ae 810 /*
811 * If new longest match for monitor prefix is the new
812 * route's prefix, move monitor to new route's prefix
813 */
26a3ffd6 814 if (prefix_match(new_vpn_node_p, &monitor->p)) {
d62a17ae 815 /* detach */
816 if (mlast) {
817 mlast->next = monitor->next;
818 } else {
819 RFAPI_MONITOR_VPN_W_ALLOC(par) = monitor->next;
820 }
821
822
823 /* attach */
824 monitor->next = RFAPI_MONITOR_VPN(new_vpn_node);
825 RFAPI_MONITOR_VPN_W_ALLOC(new_vpn_node) = monitor;
826 monitor->node = new_vpn_node;
827
fe08ba7e 828 agg_lock_node(new_vpn_node); /* incr refcount */
d62a17ae 829
830 monitor = mlast ? mlast->next : RFAPI_MONITOR_VPN(par);
831
832 RFAPI_CHECK_REFCOUNT(par, SAFI_MPLS_VPN, 1);
833 /* decr refcount after we're done with par as this might
834 * free it */
fe08ba7e 835 agg_unlock_node(par);
d62a17ae 836
837 continue;
838 }
839 mlast = monitor;
840 monitor = monitor->next;
841 }
842
843 RFAPI_CHECK_REFCOUNT(new_vpn_node, SAFI_MPLS_VPN, 0);
65efcfce
LB
844}
845
846
40381db7 847static void rfapiBgpInfoChainFree(struct bgp_path_info *bpi)
65efcfce 848{
4b7e6066 849 struct bgp_path_info *next;
d62a17ae 850
40381db7 851 while (bpi) {
d62a17ae 852
853 /*
40381db7 854 * If there is a timer waiting to delete this bpi, cancel
d62a17ae 855 * the timer and delete immediately
856 */
40381db7
DS
857 if (CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED)
858 && bpi->extra->vnc.import.timer) {
d62a17ae 859
b3d6bc6e
MS
860 struct thread **t =
861 &(bpi->extra->vnc.import.timer);
862 struct rfapi_withdraw *wcb = (*t)->arg;
d62a17ae 863
864 XFREE(MTYPE_RFAPI_WITHDRAW, wcb);
865 thread_cancel(t);
866 }
867
40381db7
DS
868 next = bpi->next;
869 bpi->next = NULL;
870 rfapiBgpInfoFree(bpi);
871 bpi = next;
d62a17ae 872 }
65efcfce
LB
873}
874
d62a17ae 875static void rfapiImportTableFlush(struct rfapi_import_table *it)
65efcfce 876{
d62a17ae 877 afi_t afi;
878
879 /*
880 * Free ecommunity
881 */
882 ecommunity_free(&it->rt_import_list);
883 it->rt_import_list = NULL;
884
885 for (afi = AFI_IP; afi < AFI_MAX; ++afi) {
886
fe08ba7e 887 struct agg_node *rn;
d62a17ae 888
fe08ba7e
DS
889 for (rn = agg_route_top(it->imported_vpn[afi]); rn;
890 rn = agg_route_next(rn)) {
d62a17ae 891 /*
892 * Each route_node has:
893 * aggregate: points to rfapi_it_extra with monitor
894 * chain(s)
9b6d8fcf 895 * info: points to chain of bgp_path_info
d62a17ae 896 */
9b6d8fcf 897 /* free bgp_path_info and its children */
d62a17ae 898 rfapiBgpInfoChainFree(rn->info);
899 rn->info = NULL;
900
901 rfapiMonitorExtraFlush(SAFI_MPLS_VPN, rn);
902 }
903
fe08ba7e
DS
904 for (rn = agg_route_top(it->imported_encap[afi]); rn;
905 rn = agg_route_next(rn)) {
9b6d8fcf 906 /* free bgp_path_info and its children */
d62a17ae 907 rfapiBgpInfoChainFree(rn->info);
908 rn->info = NULL;
909
910 rfapiMonitorExtraFlush(SAFI_ENCAP, rn);
911 }
912
fe08ba7e
DS
913 agg_table_finish(it->imported_vpn[afi]);
914 agg_table_finish(it->imported_encap[afi]);
d62a17ae 915 }
916 if (it->monitor_exterior_orphans) {
917 skiplist_free(it->monitor_exterior_orphans);
918 }
65efcfce
LB
919}
920
d62a17ae 921void rfapiImportTableRefDelByIt(struct bgp *bgp,
922 struct rfapi_import_table *it_target)
65efcfce 923{
d62a17ae 924 struct rfapi *h;
925 struct rfapi_import_table *it;
926 struct rfapi_import_table *prev = NULL;
927
928 assert(it_target);
929
930 h = bgp->rfapi;
931 assert(h);
932
933 for (it = h->imports; it; prev = it, it = it->next) {
934 if (it == it_target)
935 break;
936 }
937
938 assert(it);
939 assert(it->refcount);
940
941 it->refcount -= 1;
942
943 if (!it->refcount) {
944 if (prev) {
945 prev->next = it->next;
946 } else {
947 h->imports = it->next;
948 }
949 rfapiImportTableFlush(it);
950 XFREE(MTYPE_RFAPI_IMPORTTABLE, it);
951 }
65efcfce
LB
952}
953
1e20238a 954#ifdef RFAPI_REQUIRE_ENCAP_BEEC
65efcfce
LB
955/*
956 * Look for magic BGP Encapsulation Extended Community value
957 * Format in RFC 5512 Sect. 4.5
958 */
d62a17ae 959static int rfapiEcommunitiesMatchBeec(struct ecommunity *ecom,
960 bgp_encap_types type)
65efcfce 961{
d62a17ae 962 int i;
65efcfce 963
d62a17ae 964 if (!ecom)
965 return 0;
65efcfce 966
d62a17ae 967 for (i = 0; i < (ecom->size * ECOMMUNITY_SIZE); i += ECOMMUNITY_SIZE) {
65efcfce 968
d62a17ae 969 uint8_t *ep;
65efcfce 970
d62a17ae 971 ep = ecom->val + i;
65efcfce 972
d62a17ae 973 if (ep[0] == ECOMMUNITY_ENCODE_OPAQUE
974 && ep[1] == ECOMMUNITY_OPAQUE_SUBTYPE_ENCAP
975 && ep[6] == ((type && 0xff00) >> 8)
976 && ep[7] == (type & 0xff)) {
65efcfce 977
d62a17ae 978 return 1;
979 }
980 }
981 return 0;
65efcfce
LB
982}
983#endif
984
d62a17ae 985int rfapiEcommunitiesIntersect(struct ecommunity *e1, struct ecommunity *e2)
65efcfce 986{
f6e07e1b 987 uint32_t i, j;
d62a17ae 988
989 if (!e1 || !e2)
990 return 0;
991
992 {
993 char *s1, *s2;
994 s1 = ecommunity_ecom2str(e1, ECOMMUNITY_FORMAT_DISPLAY, 0);
995 s2 = ecommunity_ecom2str(e2, ECOMMUNITY_FORMAT_DISPLAY, 0);
996 vnc_zlog_debug_verbose("%s: e1[%s], e2[%s]", __func__, s1, s2);
997 XFREE(MTYPE_ECOMMUNITY_STR, s1);
998 XFREE(MTYPE_ECOMMUNITY_STR, s2);
999 }
1000
1001 for (i = 0; i < e1->size; ++i) {
1002 for (j = 0; j < e2->size; ++j) {
1003 if (!memcmp(e1->val + (i * ECOMMUNITY_SIZE),
1004 e2->val + (j * ECOMMUNITY_SIZE),
1005 ECOMMUNITY_SIZE)) {
1006
1007 return 1;
1008 }
1009 }
1010 }
1011 return 0;
65efcfce
LB
1012}
1013
d62a17ae 1014int rfapiEcommunityGetLNI(struct ecommunity *ecom, uint32_t *lni)
65efcfce 1015{
d62a17ae 1016 if (ecom) {
f6e07e1b
DS
1017 uint32_t i;
1018
d62a17ae 1019 for (i = 0; i < ecom->size; ++i) {
1020 uint8_t *p = ecom->val + (i * ECOMMUNITY_SIZE);
1021
1022 if ((*(p + 0) == 0x00) && (*(p + 1) == 0x02)) {
1023
1024 *lni = (*(p + 5) << 16) | (*(p + 6) << 8)
1025 | (*(p + 7));
1026 return 0;
1027 }
1028 }
1029 }
1030 return ENOENT;
65efcfce
LB
1031}
1032
d62a17ae 1033int rfapiEcommunityGetEthernetTag(struct ecommunity *ecom, uint16_t *tag_id)
79799987 1034{
d62a17ae 1035 struct bgp *bgp = bgp_get_default();
1036 *tag_id = 0; /* default to untagged */
1037 if (ecom) {
f6e07e1b
DS
1038 uint32_t i;
1039
d62a17ae 1040 for (i = 0; i < ecom->size; ++i) {
1041 as_t as = 0;
1042 int encode = 0;
1be1693e 1043 const uint8_t *p = ecom->val + (i * ECOMMUNITY_SIZE);
d62a17ae 1044
1045 /* High-order octet of type. */
1046 encode = *p++;
1047
1048 if (*p++ == ECOMMUNITY_ROUTE_TARGET) {
1049 if (encode == ECOMMUNITY_ENCODE_AS4) {
937652c6 1050 p = ptr_get_be32(p, &as);
d62a17ae 1051 } else if (encode == ECOMMUNITY_ENCODE_AS) {
1052 as = (*p++ << 8);
1053 as |= (*p++);
996c9314
LB
1054 p += 2; /* skip next two, tag/vid
1055 always in lowest bytes */
d62a17ae 1056 }
1057 if (as == bgp->as) {
1058 *tag_id = *p++ << 8;
1059 *tag_id |= (*p++);
1060 return 0;
1061 }
1062 }
1063 }
1064 }
1065 return ENOENT;
79799987
LB
1066}
1067
40381db7 1068static int rfapiVpnBiNhEqualsPt(struct bgp_path_info *bpi,
4b7e6066 1069 struct rfapi_ip_addr *hpt)
65efcfce 1070{
d62a17ae 1071 uint8_t family;
65efcfce 1072
40381db7 1073 if (!hpt || !bpi)
d62a17ae 1074 return 0;
65efcfce 1075
40381db7 1076 family = BGP_MP_NEXTHOP_FAMILY(bpi->attr->mp_nexthop_len);
65efcfce 1077
d62a17ae 1078 if (hpt->addr_family != family)
1079 return 0;
65efcfce 1080
d62a17ae 1081 switch (family) {
1082 case AF_INET:
40381db7 1083 if (bpi->attr->mp_nexthop_global_in.s_addr
d62a17ae 1084 != hpt->addr.v4.s_addr)
1085 return 0;
1086 break;
65efcfce 1087
d62a17ae 1088 case AF_INET6:
40381db7 1089 if (IPV6_ADDR_CMP(&bpi->attr->mp_nexthop_global, &hpt->addr.v6))
d62a17ae 1090 return 0;
1091 break;
65efcfce 1092
d62a17ae 1093 default:
1094 return 0;
d62a17ae 1095 }
65efcfce 1096
d62a17ae 1097 return 1;
65efcfce
LB
1098}
1099
1100
1101/*
1102 * Compare 2 VPN BIs. Return true if they have the same VN and UN addresses
1103 */
40381db7
DS
1104static int rfapiVpnBiSamePtUn(struct bgp_path_info *bpi1,
1105 struct bgp_path_info *bpi2)
65efcfce 1106{
d62a17ae 1107 struct prefix pfx_un1;
1108 struct prefix pfx_un2;
1109
40381db7 1110 if (!bpi1 || !bpi2)
d62a17ae 1111 return 0;
1112
d62a17ae 1113 /*
1114 * VN address comparisons
1115 */
1116
40381db7
DS
1117 if (BGP_MP_NEXTHOP_FAMILY(bpi1->attr->mp_nexthop_len)
1118 != BGP_MP_NEXTHOP_FAMILY(bpi2->attr->mp_nexthop_len)) {
d62a17ae 1119 return 0;
1120 }
1121
40381db7 1122 switch (BGP_MP_NEXTHOP_FAMILY(bpi1->attr->mp_nexthop_len)) {
d62a17ae 1123 case AF_INET:
40381db7
DS
1124 if (bpi1->attr->mp_nexthop_global_in.s_addr
1125 != bpi2->attr->mp_nexthop_global_in.s_addr)
d62a17ae 1126 return 0;
1127 break;
1128
1129 case AF_INET6:
40381db7
DS
1130 if (IPV6_ADDR_CMP(&bpi1->attr->mp_nexthop_global,
1131 &bpi2->attr->mp_nexthop_global))
d62a17ae 1132 return 0;
1133 break;
1134
1135 default:
1136 return 0;
d62a17ae 1137 }
1138
fa3bf3a2
MS
1139 memset(&pfx_un1, 0, sizeof(pfx_un1));
1140 memset(&pfx_un2, 0, sizeof(pfx_un2));
1141
d62a17ae 1142 /*
1143 * UN address comparisons
1144 */
40381db7
DS
1145 if (rfapiGetVncTunnelUnAddr(bpi1->attr, &pfx_un1)) {
1146 if (bpi1->extra) {
1147 pfx_un1.family = bpi1->extra->vnc.import.un_family;
1148 switch (bpi1->extra->vnc.import.un_family) {
d62a17ae 1149 case AF_INET:
1150 pfx_un1.u.prefix4 =
40381db7 1151 bpi1->extra->vnc.import.un.addr4;
d62a17ae 1152 break;
1153 case AF_INET6:
1154 pfx_un1.u.prefix6 =
40381db7 1155 bpi1->extra->vnc.import.un.addr6;
d62a17ae 1156 break;
1157 default:
1158 pfx_un1.family = 0;
1159 break;
1160 }
1161 }
1162 }
1163
40381db7
DS
1164 if (rfapiGetVncTunnelUnAddr(bpi2->attr, &pfx_un2)) {
1165 if (bpi2->extra) {
1166 pfx_un2.family = bpi2->extra->vnc.import.un_family;
1167 switch (bpi2->extra->vnc.import.un_family) {
d62a17ae 1168 case AF_INET:
1169 pfx_un2.u.prefix4 =
40381db7 1170 bpi2->extra->vnc.import.un.addr4;
d62a17ae 1171 break;
1172 case AF_INET6:
1173 pfx_un2.u.prefix6 =
40381db7 1174 bpi2->extra->vnc.import.un.addr6;
d62a17ae 1175 break;
1176 default:
1177 pfx_un2.family = 0;
1178 break;
1179 }
1180 }
1181 }
1182
fa3bf3a2 1183 if (pfx_un1.family == 0 || pfx_un2.family == 0)
d62a17ae 1184 return 0;
1185
1186 if (pfx_un1.family != pfx_un2.family)
1187 return 0;
1188
1189 switch (pfx_un1.family) {
1190 case AF_INET:
996c9314 1191 if (!IPV4_ADDR_SAME(&pfx_un1.u.prefix4, &pfx_un2.u.prefix4))
d62a17ae 1192 return 0;
1193 break;
1194 case AF_INET6:
1195 if (!IPV6_ADDR_SAME(&pfx_un1.u.prefix6, &pfx_un2.u.prefix6))
1196 return 0;
1197 break;
1198 }
1199
1200
1201 return 1;
65efcfce
LB
1202}
1203
d62a17ae 1204uint8_t rfapiRfpCost(struct attr *attr)
65efcfce 1205{
d62a17ae 1206 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF)) {
1207 if (attr->local_pref > 255) {
1208 return 0;
1209 }
1210 return 255 - attr->local_pref;
1211 }
1212
1213 return 255;
65efcfce
LB
1214}
1215
1216/*------------------------------------------
1217 * rfapi_extract_l2o
1218 *
d62a17ae 1219 * Find Layer 2 options in an option chain
65efcfce 1220 *
d62a17ae 1221 * input:
65efcfce
LB
1222 * pHop option chain
1223 *
1224 * output:
1225 * l2o layer 2 options extracted
1226 *
1227 * return value:
1228 * 0 OK
1229 * 1 no options found
1230 *
1231 --------------------------------------------*/
d62a17ae 1232int rfapi_extract_l2o(
1233 struct bgp_tea_options *pHop, /* chain of options */
1234 struct rfapi_l2address_option *l2o) /* return extracted value */
65efcfce 1235{
d62a17ae 1236 struct bgp_tea_options *p;
65efcfce 1237
d62a17ae 1238 for (p = pHop; p; p = p->next) {
1239 if ((p->type == RFAPI_VN_OPTION_TYPE_L2ADDR)
1240 && (p->length >= 8)) {
65efcfce 1241
d62a17ae 1242 char *v = p->value;
65efcfce 1243
d62a17ae 1244 memcpy(&l2o->macaddr, v, 6);
65efcfce 1245
d62a17ae 1246 l2o->label = ((v[6] << 12) & 0xff000)
1247 + ((v[7] << 4) & 0xff0)
1248 + ((v[8] >> 4) & 0xf);
65efcfce 1249
d62a17ae 1250 l2o->local_nve_id = (uint8_t)v[10];
65efcfce 1251
d62a17ae 1252 l2o->logical_net_id =
1253 (v[11] << 16) + (v[12] << 8) + (v[13] << 0);
65efcfce 1254
d62a17ae 1255 return 0;
1256 }
1257 }
1258 return 1;
65efcfce
LB
1259}
1260
1261static struct rfapi_next_hop_entry *
d62a17ae 1262rfapiRouteInfo2NextHopEntry(struct rfapi_ip_prefix *rprefix,
40381db7
DS
1263 struct bgp_path_info *bpi, /* route to encode */
1264 uint32_t lifetime, /* use this in nhe */
1265 struct agg_node *rn) /* req for L2 eth addr */
65efcfce 1266{
d62a17ae 1267 struct rfapi_next_hop_entry *new;
1268 int have_vnc_tunnel_un = 0;
26a3ffd6 1269 const struct prefix *p = agg_node_get_prefix(rn);
65efcfce 1270
1e20238a 1271#ifdef DEBUG_ENCAP_MONITOR
40381db7 1272 vnc_zlog_debug_verbose("%s: entry, bpi %p, rn %p", __func__, bpi, rn);
65efcfce
LB
1273#endif
1274
d62a17ae 1275 new = XCALLOC(MTYPE_RFAPI_NEXTHOP, sizeof(struct rfapi_next_hop_entry));
1276 assert(new);
1277
1278 new->prefix = *rprefix;
1279
40381db7
DS
1280 if (bpi->extra
1281 && decode_rd_type(bpi->extra->vnc.import.rd.val)
d62a17ae 1282 == RD_TYPE_VNC_ETH) {
1283 /* ethernet */
1284
1285 struct rfapi_vn_option *vo;
1286
1287 vo = XCALLOC(MTYPE_RFAPI_VN_OPTION,
1288 sizeof(struct rfapi_vn_option));
1289 assert(vo);
1290
1291 vo->type = RFAPI_VN_OPTION_TYPE_L2ADDR;
1292
26a3ffd6 1293 memcpy(&vo->v.l2addr.macaddr, &p->u.prefix_eth.octet, ETH_ALEN);
d62a17ae 1294 /* only low 3 bytes of this are significant */
05864da7
DS
1295 (void)rfapiEcommunityGetLNI(bpi->attr->ecommunity,
1296 &vo->v.l2addr.logical_net_id);
1297 (void)rfapiEcommunityGetEthernetTag(bpi->attr->ecommunity,
1298 &vo->v.l2addr.tag_id);
d62a17ae 1299
1300 /* local_nve_id comes from lower byte of RD type */
40381db7 1301 vo->v.l2addr.local_nve_id = bpi->extra->vnc.import.rd.val[1];
d62a17ae 1302
1303 /* label comes from MP_REACH_NLRI label */
40381db7 1304 vo->v.l2addr.label = decode_label(&bpi->extra->label[0]);
d62a17ae 1305
1306 new->vn_options = vo;
1307
1308 /*
1309 * If there is an auxiliary prefix (i.e., host IP address),
1310 * use it as the nexthop prefix instead of the query prefix
1311 */
40381db7
DS
1312 if (bpi->extra->vnc.import.aux_prefix.family) {
1313 rfapiQprefix2Rprefix(&bpi->extra->vnc.import.aux_prefix,
d62a17ae 1314 &new->prefix);
1315 }
1316 }
1317
05864da7
DS
1318 bgp_encap_types tun_type = BGP_ENCAP_TYPE_MPLS; /*Default*/
1319 new->prefix.cost = rfapiRfpCost(bpi->attr);
d62a17ae 1320
05864da7 1321 struct bgp_attr_encap_subtlv *pEncap;
d62a17ae 1322
05864da7
DS
1323 switch (BGP_MP_NEXTHOP_FAMILY(bpi->attr->mp_nexthop_len)) {
1324 case AF_INET:
1325 new->vn_address.addr_family = AF_INET;
1326 new->vn_address.addr.v4 = bpi->attr->mp_nexthop_global_in;
1327 break;
d62a17ae 1328
05864da7
DS
1329 case AF_INET6:
1330 new->vn_address.addr_family = AF_INET6;
1331 new->vn_address.addr.v6 = bpi->attr->mp_nexthop_global;
1332 break;
d62a17ae 1333
05864da7
DS
1334 default:
1335 zlog_warn("%s: invalid vpn nexthop length: %d", __func__,
1336 bpi->attr->mp_nexthop_len);
1337 rfapi_free_next_hop_list(new);
1338 return NULL;
1339 }
d62a17ae 1340
91ebf12c
DS
1341 for (pEncap = bgp_attr_get_vnc_subtlvs(bpi->attr); pEncap;
1342 pEncap = pEncap->next) {
05864da7
DS
1343 switch (pEncap->type) {
1344 case BGP_VNC_SUBTLV_TYPE_LIFETIME:
1345 /* use configured lifetime, not attr lifetime */
1346 break;
d62a17ae 1347
05864da7
DS
1348 default:
1349 zlog_warn("%s: unknown VNC option type %d", __func__,
1350 pEncap->type);
d62a17ae 1351
05864da7 1352 break;
d62a17ae 1353 }
05864da7 1354 }
d62a17ae 1355
05864da7
DS
1356 bgp_attr_extcom_tunnel_type(bpi->attr, &tun_type);
1357 if (tun_type == BGP_ENCAP_TYPE_MPLS) {
1358 struct prefix p;
1359 /* MPLS carries UN address in next hop */
1360 rfapiNexthop2Prefix(bpi->attr, &p);
1361 if (p.family != 0) {
1362 rfapiQprefix2Raddr(&p, &new->un_address);
1363 have_vnc_tunnel_un = 1;
d62a17ae 1364 }
05864da7 1365 }
d62a17ae 1366
05864da7
DS
1367 for (pEncap = bpi->attr->encap_subtlvs; pEncap; pEncap = pEncap->next) {
1368 switch (pEncap->type) {
1369 case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
1370 /*
1371 * Overrides ENCAP UN address, if any
1372 */
1373 switch (pEncap->length) {
d62a17ae 1374
05864da7
DS
1375 case 8:
1376 new->un_address.addr_family = AF_INET;
1377 memcpy(&new->un_address.addr.v4, pEncap->value,
1378 4);
1379 have_vnc_tunnel_un = 1;
1380 break;
d62a17ae 1381
05864da7
DS
1382 case 20:
1383 new->un_address.addr_family = AF_INET6;
1384 memcpy(&new->un_address.addr.v6, pEncap->value,
1385 16);
1386 have_vnc_tunnel_un = 1;
d62a17ae 1387 break;
1388
1389 default:
1390 zlog_warn(
05864da7
DS
1391 "%s: invalid tunnel subtlv UN addr length (%d) for bpi %p",
1392 __func__, pEncap->length, bpi);
d62a17ae 1393 }
05864da7
DS
1394 break;
1395
1396 default:
1397 zlog_warn("%s: unknown Encap Attribute option type %d",
1398 __func__, pEncap->type);
1399 break;
d62a17ae 1400 }
05864da7 1401 }
d62a17ae 1402
05864da7 1403 new->un_options = rfapi_encap_tlv_to_un_option(bpi->attr);
65efcfce 1404
1e20238a 1405#ifdef DEBUG_ENCAP_MONITOR
05864da7
DS
1406 vnc_zlog_debug_verbose("%s: line %d: have_vnc_tunnel_un=%d", __func__,
1407 __LINE__, have_vnc_tunnel_un);
65efcfce
LB
1408#endif
1409
05864da7
DS
1410 if (!have_vnc_tunnel_un && bpi->extra) {
1411 /*
1412 * use cached UN address from ENCAP route
1413 */
1414 new->un_address.addr_family = bpi->extra->vnc.import.un_family;
1415 switch (new->un_address.addr_family) {
1416 case AF_INET:
1417 new->un_address.addr.v4 =
1418 bpi->extra->vnc.import.un.addr4;
1419 break;
1420 case AF_INET6:
1421 new->un_address.addr.v6 =
1422 bpi->extra->vnc.import.un.addr6;
1423 break;
1424 default:
1425 zlog_warn("%s: invalid UN addr family (%d) for bpi %p",
1426 __func__, new->un_address.addr_family, bpi);
1427 rfapi_free_next_hop_list(new);
1428 return NULL;
d62a17ae 1429 }
1430 }
1431
1432 new->lifetime = lifetime;
1433 return new;
65efcfce
LB
1434}
1435
fe08ba7e 1436int rfapiHasNonRemovedRoutes(struct agg_node *rn)
65efcfce 1437{
40381db7 1438 struct bgp_path_info *bpi;
65efcfce 1439
40381db7 1440 for (bpi = rn->info; bpi; bpi = bpi->next) {
d62a17ae 1441 struct prefix pfx;
65efcfce 1442
40381db7
DS
1443 if (!CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED)
1444 && (bpi->extra && !rfapiGetUnAddrOfVpnBi(bpi, &pfx))) {
65efcfce 1445
d62a17ae 1446 return 1;
1447 }
1448 }
1449 return 0;
65efcfce
LB
1450}
1451
1e20238a 1452#ifdef DEBUG_IT_NODES
d62a17ae 1453/*
65efcfce
LB
1454 * DEBUG FUNCTION
1455 */
fe08ba7e 1456void rfapiDumpNode(struct agg_node *rn)
65efcfce 1457{
40381db7 1458 struct bgp_path_info *bpi;
d62a17ae 1459
1460 vnc_zlog_debug_verbose("%s: rn=%p", __func__, rn);
40381db7 1461 for (bpi = rn->info; bpi; bpi = bpi->next) {
d62a17ae 1462 struct prefix pfx;
40381db7 1463 int ctrc = rfapiGetUnAddrOfVpnBi(bpi, &pfx);
d62a17ae 1464 int nr;
1465
40381db7
DS
1466 if (!CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED)
1467 && (bpi->extra && !ctrc)) {
d62a17ae 1468
1469 nr = 1;
1470 } else {
1471 nr = 0;
1472 }
1473
1474 vnc_zlog_debug_verbose(
40381db7
DS
1475 " bpi=%p, nr=%d, flags=0x%x, extra=%p, ctrc=%d", bpi,
1476 nr, bpi->flags, bpi->extra, ctrc);
d62a17ae 1477 }
65efcfce
LB
1478}
1479#endif
1480
d62a17ae 1481static int rfapiNhlAddNodeRoutes(
fe08ba7e 1482 struct agg_node *rn, /* in */
d62a17ae 1483 struct rfapi_ip_prefix *rprefix, /* in */
1484 uint32_t lifetime, /* in */
1485 int removed, /* in */
1486 struct rfapi_next_hop_entry **head, /* in/out */
1487 struct rfapi_next_hop_entry **tail, /* in/out */
1488 struct rfapi_ip_addr *exclude_vnaddr, /* omit routes to same NVE */
fe08ba7e 1489 struct agg_node *rfd_rib_node, /* preload this NVE rib node */
d62a17ae 1490 struct prefix *pfx_target_original) /* query target */
65efcfce 1491{
40381db7 1492 struct bgp_path_info *bpi;
d62a17ae 1493 struct rfapi_next_hop_entry *new;
1494 struct prefix pfx_un;
1495 struct skiplist *seen_nexthops;
1496 int count = 0;
26a3ffd6
DS
1497 const struct prefix *p = agg_node_get_prefix(rn);
1498 int is_l2 = (p->family == AF_ETHERNET);
d62a17ae 1499
fe08ba7e
DS
1500 if (rfd_rib_node) {
1501 struct agg_table *atable = agg_get_table(rfd_rib_node);
84cc1207
PZ
1502 struct rfapi_descriptor *rfd;
1503
fe08ba7e
DS
1504 if (atable) {
1505 rfd = agg_get_table_info(atable);
84cc1207 1506
fe08ba7e
DS
1507 if (rfapiRibFTDFilterRecentPrefix(rfd, rn,
1508 pfx_target_original))
1509 return 0;
1510 }
d62a17ae 1511 }
1512
1513 seen_nexthops =
63265b5c 1514 skiplist_new(0, vnc_prefix_cmp, prefix_free_lists);
d62a17ae 1515
40381db7 1516 for (bpi = rn->info; bpi; bpi = bpi->next) {
d62a17ae 1517
1518 struct prefix pfx_vn;
1519 struct prefix *newpfx;
1520
40381db7 1521 if (removed && !CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED)) {
1e20238a 1522#ifdef DEBUG_RETURNED_NHL
d62a17ae 1523 vnc_zlog_debug_verbose(
1524 "%s: want holddown, this route not holddown, skip",
1525 __func__);
65efcfce 1526#endif
d62a17ae 1527 continue;
1528 }
40381db7 1529 if (!removed && CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED)) {
d62a17ae 1530 continue;
1531 }
1532
40381db7 1533 if (!bpi->extra) {
d62a17ae 1534 continue;
1535 }
1536
1537 /*
1538 * Check for excluded VN address
1539 */
40381db7 1540 if (rfapiVpnBiNhEqualsPt(bpi, exclude_vnaddr))
d62a17ae 1541 continue;
1542
1543 /*
1544 * Check for VN address (nexthop) copied already
1545 */
1546 if (is_l2) {
1547 /* L2 routes: semantic nexthop in aux_prefix; VN addr
1548 * ain't it */
40381db7 1549 pfx_vn = bpi->extra->vnc.import.aux_prefix;
d62a17ae 1550 } else {
40381db7 1551 rfapiNexthop2Prefix(bpi->attr, &pfx_vn);
d62a17ae 1552 }
1553 if (!skiplist_search(seen_nexthops, &pfx_vn, NULL)) {
1e20238a 1554#ifdef DEBUG_RETURNED_NHL
d62a17ae 1555 vnc_zlog_debug_verbose(
2dbe669b
DA
1556 "%s: already put VN/nexthop %pFX, skip",
1557 __func__, &pfx_vn);
65efcfce 1558#endif
d62a17ae 1559 continue;
1560 }
65efcfce 1561
40381db7 1562 if (rfapiGetUnAddrOfVpnBi(bpi, &pfx_un)) {
1e20238a 1563#ifdef DEBUG_ENCAP_MONITOR
d62a17ae 1564 vnc_zlog_debug_verbose(
40381db7 1565 "%s: failed to get UN address of this VPN bpi",
d62a17ae 1566 __func__);
65efcfce 1567#endif
d62a17ae 1568 continue;
1569 }
1570
1571 newpfx = prefix_new();
1572 *newpfx = pfx_vn;
1573 skiplist_insert(seen_nexthops, newpfx, newpfx);
1574
40381db7 1575 new = rfapiRouteInfo2NextHopEntry(rprefix, bpi, lifetime, rn);
d62a17ae 1576 if (new) {
1577 if (rfapiRibPreloadBi(rfd_rib_node, &pfx_vn, &pfx_un,
40381db7 1578 lifetime, bpi)) {
d62a17ae 1579 /* duplicate filtered by RIB */
1580 rfapi_free_next_hop_list(new);
1581 new = NULL;
1582 }
1583 }
1584
1585 if (new) {
1586 if (*tail) {
1587 (*tail)->next = new;
1588 } else {
1589 *head = new;
1590 }
1591 *tail = new;
1592 ++count;
1593 }
1594 }
1595
1596 skiplist_free(seen_nexthops);
1597
1598 return count;
65efcfce
LB
1599}
1600
1601
1602/*
1603 * Breadth-first
1604 *
1605 * omit_node is meant for the situation where we are adding a subtree
1606 * of a parent of some original requested node. The response already
1607 * contains the original requested node, and we don't want to duplicate
1608 * its routes in the list, so we skip it if the right or left node
1609 * matches (of course, we still travel down its child subtrees).
1610 */
d62a17ae 1611static int rfapiNhlAddSubtree(
fe08ba7e 1612 struct agg_node *rn, /* in */
d62a17ae 1613 uint32_t lifetime, /* in */
1614 struct rfapi_next_hop_entry **head, /* in/out */
1615 struct rfapi_next_hop_entry **tail, /* in/out */
fe08ba7e 1616 struct agg_node *omit_node, /* in */
d62a17ae 1617 struct rfapi_ip_addr *exclude_vnaddr, /* omit routes to same NVE */
fe08ba7e 1618 struct agg_table *rfd_rib_table, /* preload here */
d62a17ae 1619 struct prefix *pfx_target_original) /* query target */
65efcfce 1620{
d62a17ae 1621 struct rfapi_ip_prefix rprefix;
1622 int rcount = 0;
1623
1624 /* FIXME: need to find a better way here to work without sticking our
1625 * hands in node->link */
fe08ba7e
DS
1626 if (agg_node_left(rn) && agg_node_left(rn) != omit_node) {
1627 if (agg_node_left(rn)->info) {
26a3ffd6
DS
1628 const struct prefix *p =
1629 agg_node_get_prefix(agg_node_left(rn));
d62a17ae 1630 int count = 0;
fe08ba7e 1631 struct agg_node *rib_rn = NULL;
d62a17ae 1632
26a3ffd6
DS
1633 rfapiQprefix2Rprefix(p, &rprefix);
1634 if (rfd_rib_table)
1635 rib_rn = agg_node_get(rfd_rib_table, p);
d62a17ae 1636
1637 count = rfapiNhlAddNodeRoutes(
fe08ba7e
DS
1638 agg_node_left(rn), &rprefix, lifetime, 0, head,
1639 tail, exclude_vnaddr, rib_rn,
1640 pfx_target_original);
d62a17ae 1641 if (!count) {
1642 count = rfapiNhlAddNodeRoutes(
fe08ba7e
DS
1643 agg_node_left(rn), &rprefix, lifetime,
1644 1, head, tail, exclude_vnaddr, rib_rn,
d62a17ae 1645 pfx_target_original);
1646 }
1647 rcount += count;
1648 if (rib_rn)
fe08ba7e 1649 agg_unlock_node(rib_rn);
d62a17ae 1650 }
1651 }
1652
fe08ba7e
DS
1653 if (agg_node_right(rn) && agg_node_right(rn) != omit_node) {
1654 if (agg_node_right(rn)->info) {
26a3ffd6
DS
1655 const struct prefix *p =
1656 agg_node_get_prefix(agg_node_right(rn));
d62a17ae 1657 int count = 0;
fe08ba7e 1658 struct agg_node *rib_rn = NULL;
d62a17ae 1659
26a3ffd6
DS
1660 rfapiQprefix2Rprefix(p, &rprefix);
1661 if (rfd_rib_table)
1662 rib_rn = agg_node_get(rfd_rib_table, p);
1663
d62a17ae 1664 count = rfapiNhlAddNodeRoutes(
fe08ba7e
DS
1665 agg_node_right(rn), &rprefix, lifetime, 0, head,
1666 tail, exclude_vnaddr, rib_rn,
1667 pfx_target_original);
d62a17ae 1668 if (!count) {
1669 count = rfapiNhlAddNodeRoutes(
fe08ba7e
DS
1670 agg_node_right(rn), &rprefix, lifetime,
1671 1, head, tail, exclude_vnaddr, rib_rn,
d62a17ae 1672 pfx_target_original);
1673 }
1674 rcount += count;
1675 if (rib_rn)
fe08ba7e 1676 agg_unlock_node(rib_rn);
d62a17ae 1677 }
1678 }
1679
fe08ba7e 1680 if (agg_node_left(rn)) {
d62a17ae 1681 rcount += rfapiNhlAddSubtree(
fe08ba7e 1682 agg_node_left(rn), lifetime, head, tail, omit_node,
d62a17ae 1683 exclude_vnaddr, rfd_rib_table, pfx_target_original);
1684 }
fe08ba7e 1685 if (agg_node_right(rn)) {
d62a17ae 1686 rcount += rfapiNhlAddSubtree(
fe08ba7e 1687 agg_node_right(rn), lifetime, head, tail, omit_node,
d62a17ae 1688 exclude_vnaddr, rfd_rib_table, pfx_target_original);
1689 }
1690
1691 return rcount;
65efcfce
LB
1692}
1693
1694/*
1695 * Implementation of ROUTE_LIST(node) from RFAPI-Import-Event-Handling.txt
1696 *
1697 * Construct an rfapi nexthop list based on the routes attached to
1698 * the specified node.
1699 *
1defdda8
DS
1700 * If there are any routes that do NOT have BGP_PATH_REMOVED set,
1701 * return those only. If there are ONLY routes with BGP_PATH_REMOVED,
65efcfce
LB
1702 * then return those, and also include all the non-removed routes from the
1703 * next less-specific node (i.e., this node's parent) at the end.
1704 */
d62a17ae 1705struct rfapi_next_hop_entry *rfapiRouteNode2NextHopList(
fe08ba7e
DS
1706 struct agg_node *rn, uint32_t lifetime, /* put into nexthop entries */
1707 struct rfapi_ip_addr *exclude_vnaddr, /* omit routes to same NVE */
1708 struct agg_table *rfd_rib_table, /* preload here */
1709 struct prefix *pfx_target_original) /* query target */
65efcfce 1710{
d62a17ae 1711 struct rfapi_ip_prefix rprefix;
1712 struct rfapi_next_hop_entry *answer = NULL;
1713 struct rfapi_next_hop_entry *last = NULL;
fe08ba7e 1714 struct agg_node *parent;
26a3ffd6 1715 const struct prefix *p = agg_node_get_prefix(rn);
d62a17ae 1716 int count = 0;
fe08ba7e 1717 struct agg_node *rib_rn;
65efcfce 1718
1e20238a 1719#ifdef DEBUG_RETURNED_NHL
26a3ffd6 1720 vnc_zlog_debug_verbose("%s: called with node pfx=%rRN", __func__, rn);
d62a17ae 1721 rfapiDebugBacktrace();
65efcfce
LB
1722#endif
1723
26a3ffd6 1724 rfapiQprefix2Rprefix(p, &rprefix);
d62a17ae 1725
26a3ffd6 1726 rib_rn = rfd_rib_table ? agg_node_get(rfd_rib_table, p) : NULL;
d62a17ae 1727
1728 /*
1729 * Add non-withdrawn routes at this node
1730 */
1731 count = rfapiNhlAddNodeRoutes(rn, &rprefix, lifetime, 0, &answer, &last,
1732 exclude_vnaddr, rib_rn,
1733 pfx_target_original);
1734
1735 /*
1736 * If the list has at least one entry, it's finished
1737 */
1738 if (count) {
1739 count += rfapiNhlAddSubtree(rn, lifetime, &answer, &last, NULL,
1740 exclude_vnaddr, rfd_rib_table,
1741 pfx_target_original);
1742 vnc_zlog_debug_verbose("%s: %d nexthops, answer=%p", __func__,
1743 count, answer);
1e20238a 1744#ifdef DEBUG_RETURNED_NHL
d62a17ae 1745 rfapiPrintNhl(NULL, answer);
65efcfce 1746#endif
d62a17ae 1747 if (rib_rn)
fe08ba7e 1748 agg_unlock_node(rib_rn);
d62a17ae 1749 return answer;
1750 }
1751
1752 /*
1753 * Add withdrawn routes at this node
1754 */
1755 count = rfapiNhlAddNodeRoutes(rn, &rprefix, lifetime, 1, &answer, &last,
1756 exclude_vnaddr, rib_rn,
1757 pfx_target_original);
1758 if (rib_rn)
fe08ba7e 1759 agg_unlock_node(rib_rn);
d62a17ae 1760
1761 // rfapiPrintNhl(NULL, answer);
1762
1763 /*
1764 * walk up the tree until we find a node with non-deleted
1765 * routes, then add them
1766 */
fe08ba7e
DS
1767 for (parent = agg_node_parent(rn); parent;
1768 parent = agg_node_parent(parent)) {
d62a17ae 1769 if (rfapiHasNonRemovedRoutes(parent)) {
1770 break;
1771 }
1772 }
1773
1774 /*
1775 * Add non-withdrawn routes from less-specific prefix
1776 */
1777 if (parent) {
26a3ffd6
DS
1778 const struct prefix *p = agg_node_get_prefix(parent);
1779
1780 rib_rn = rfd_rib_table ? agg_node_get(rfd_rib_table, p) : NULL;
1781 rfapiQprefix2Rprefix(p, &rprefix);
d62a17ae 1782 count += rfapiNhlAddNodeRoutes(parent, &rprefix, lifetime, 0,
1783 &answer, &last, exclude_vnaddr,
1784 rib_rn, pfx_target_original);
1785 count += rfapiNhlAddSubtree(parent, lifetime, &answer, &last,
1786 rn, exclude_vnaddr, rfd_rib_table,
1787 pfx_target_original);
1788 if (rib_rn)
fe08ba7e 1789 agg_unlock_node(rib_rn);
d62a17ae 1790 } else {
1791 /*
1792 * There is no parent with non-removed routes. Still need to
1793 * add subtree of original node if it contributed routes to the
1794 * answer.
1795 */
1796 if (count)
1797 count += rfapiNhlAddSubtree(rn, lifetime, &answer,
1798 &last, rn, exclude_vnaddr,
1799 rfd_rib_table,
1800 pfx_target_original);
1801 }
1802
1803 vnc_zlog_debug_verbose("%s: %d nexthops, answer=%p", __func__, count,
1804 answer);
1e20238a 1805#ifdef DEBUG_RETURNED_NHL
d62a17ae 1806 rfapiPrintNhl(NULL, answer);
65efcfce 1807#endif
d62a17ae 1808 return answer;
65efcfce
LB
1809}
1810
1811/*
1812 * Construct nexthop list of all routes in table
1813 */
d62a17ae 1814struct rfapi_next_hop_entry *rfapiRouteTable2NextHopList(
fe08ba7e
DS
1815 struct agg_table *rt, uint32_t lifetime, /* put into nexthop entries */
1816 struct rfapi_ip_addr *exclude_vnaddr, /* omit routes to same NVE */
1817 struct agg_table *rfd_rib_table, /* preload this NVE rib table */
1818 struct prefix *pfx_target_original) /* query target */
65efcfce 1819{
fe08ba7e 1820 struct agg_node *rn;
d62a17ae 1821 struct rfapi_next_hop_entry *biglist = NULL;
1822 struct rfapi_next_hop_entry *nhl;
1823 struct rfapi_next_hop_entry *tail = NULL;
1824 int count = 0;
1825
fe08ba7e 1826 for (rn = agg_route_top(rt); rn; rn = agg_route_next(rn)) {
d62a17ae 1827
1828 nhl = rfapiRouteNode2NextHopList(rn, lifetime, exclude_vnaddr,
1829 rfd_rib_table,
1830 pfx_target_original);
1831 if (!tail) {
1832 tail = biglist = nhl;
1833 if (tail)
1834 count = 1;
1835 } else {
1836 tail->next = nhl;
1837 }
1838 if (tail) {
1839 while (tail->next) {
1840 ++count;
1841 tail = tail->next;
1842 }
1843 }
1844 }
1845
1846 vnc_zlog_debug_verbose("%s: returning %d routes", __func__, count);
1847 return biglist;
65efcfce
LB
1848}
1849
d62a17ae 1850struct rfapi_next_hop_entry *rfapiEthRouteNode2NextHopList(
fe08ba7e 1851 struct agg_node *rn, struct rfapi_ip_prefix *rprefix,
d62a17ae 1852 uint32_t lifetime, /* put into nexthop entries */
1853 struct rfapi_ip_addr *exclude_vnaddr, /* omit routes to same NVE */
fe08ba7e 1854 struct agg_table *rfd_rib_table, /* preload NVE rib table */
d62a17ae 1855 struct prefix *pfx_target_original) /* query target */
65efcfce 1856{
d62a17ae 1857 int count = 0;
1858 struct rfapi_next_hop_entry *answer = NULL;
1859 struct rfapi_next_hop_entry *last = NULL;
fe08ba7e 1860 struct agg_node *rib_rn;
65efcfce 1861
26a3ffd6
DS
1862 rib_rn = rfd_rib_table
1863 ? agg_node_get(rfd_rib_table, agg_node_get_prefix(rn))
1864 : NULL;
65efcfce 1865
d62a17ae 1866 count = rfapiNhlAddNodeRoutes(rn, rprefix, lifetime, 0, &answer, &last,
1867 NULL, rib_rn, pfx_target_original);
65efcfce 1868
1e20238a 1869#ifdef DEBUG_ENCAP_MONITOR
d62a17ae 1870 vnc_zlog_debug_verbose("%s: node %p: %d non-holddown routes", __func__,
1871 rn, count);
65efcfce
LB
1872#endif
1873
d62a17ae 1874 if (!count) {
1875 count = rfapiNhlAddNodeRoutes(rn, rprefix, lifetime, 1, &answer,
1876 &last, exclude_vnaddr, rib_rn,
1877 pfx_target_original);
1878 vnc_zlog_debug_verbose("%s: node %p: %d holddown routes",
1879 __func__, rn, count);
1880 }
65efcfce 1881
d62a17ae 1882 if (rib_rn)
fe08ba7e 1883 agg_unlock_node(rib_rn);
65efcfce 1884
1e20238a 1885#ifdef DEBUG_RETURNED_NHL
d62a17ae 1886 rfapiPrintNhl(NULL, answer);
65efcfce
LB
1887#endif
1888
d62a17ae 1889 return answer;
65efcfce
LB
1890}
1891
1892
1893/*
1894 * Construct nexthop list of all routes in table
1895 */
d62a17ae 1896struct rfapi_next_hop_entry *rfapiEthRouteTable2NextHopList(
1897 uint32_t logical_net_id, struct rfapi_ip_prefix *rprefix,
1898 uint32_t lifetime, /* put into nexthop entries */
1899 struct rfapi_ip_addr *exclude_vnaddr, /* omit routes to same NVE */
fe08ba7e 1900 struct agg_table *rfd_rib_table, /* preload NVE rib node */
d62a17ae 1901 struct prefix *pfx_target_original) /* query target */
65efcfce 1902{
d62a17ae 1903 struct rfapi_import_table *it;
1904 struct bgp *bgp = bgp_get_default();
fe08ba7e
DS
1905 struct agg_table *rt;
1906 struct agg_node *rn;
d62a17ae 1907 struct rfapi_next_hop_entry *biglist = NULL;
1908 struct rfapi_next_hop_entry *nhl;
1909 struct rfapi_next_hop_entry *tail = NULL;
1910 int count = 0;
1911
1912
1913 it = rfapiMacImportTableGet(bgp, logical_net_id);
1914 rt = it->imported_vpn[AFI_L2VPN];
1915
fe08ba7e 1916 for (rn = agg_route_top(rt); rn; rn = agg_route_next(rn)) {
d62a17ae 1917
1918 nhl = rfapiEthRouteNode2NextHopList(
1919 rn, rprefix, lifetime, exclude_vnaddr, rfd_rib_table,
1920 pfx_target_original);
1921 if (!tail) {
1922 tail = biglist = nhl;
1923 if (tail)
1924 count = 1;
1925 } else {
1926 tail->next = nhl;
1927 }
1928 if (tail) {
1929 while (tail->next) {
1930 ++count;
1931 tail = tail->next;
1932 }
1933 }
1934 }
1935
1936 vnc_zlog_debug_verbose("%s: returning %d routes", __func__, count);
1937 return biglist;
65efcfce
LB
1938}
1939
1940/*
40381db7
DS
1941 * Insert a new bpi to the imported route table node,
1942 * keeping the list of BPIs sorted best route first
65efcfce 1943 */
fe08ba7e 1944static void rfapiBgpInfoAttachSorted(struct agg_node *rn,
4b7e6066 1945 struct bgp_path_info *info_new, afi_t afi,
d62a17ae 1946 safi_t safi)
65efcfce 1947{
d62a17ae 1948 struct bgp *bgp;
4b7e6066
DS
1949 struct bgp_path_info *prev;
1950 struct bgp_path_info *next;
d62a17ae 1951 char pfx_buf[PREFIX2STR_BUFFER];
1952
1953
1954 bgp = bgp_get_default(); /* assume 1 instance for now */
1955
1956 if (VNC_DEBUG(IMPORT_BI_ATTACH)) {
1957 vnc_zlog_debug_verbose("%s: info_new->peer=%p", __func__,
1958 info_new->peer);
1959 vnc_zlog_debug_verbose("%s: info_new->peer->su_remote=%p",
1960 __func__, info_new->peer->su_remote);
1961 }
1962
1963 for (prev = NULL, next = rn->info; next;
1964 prev = next, next = next->next) {
fdf81fa0
DS
1965 enum bgp_path_selection_reason reason;
1966
1defdda8
DS
1967 if (!bgp
1968 || (!CHECK_FLAG(info_new->flags, BGP_PATH_REMOVED)
1969 && CHECK_FLAG(next->flags, BGP_PATH_REMOVED))
18ee8310 1970 || bgp_path_info_cmp_compatible(bgp, info_new, next,
fdf81fa0
DS
1971 pfx_buf, afi, safi,
1972 &reason)
d62a17ae 1973 == -1) { /* -1 if 1st is better */
1974 break;
1975 }
1976 }
1977 vnc_zlog_debug_verbose("%s: prev=%p, next=%p", __func__, prev, next);
1978 if (prev) {
1979 prev->next = info_new;
1980 } else {
1981 rn->info = info_new;
1982 }
1983 info_new->prev = prev;
1984 info_new->next = next;
1985 if (next)
1986 next->prev = info_new;
1987 bgp_attr_intern(info_new->attr);
65efcfce
LB
1988}
1989
40381db7 1990static void rfapiBgpInfoDetach(struct agg_node *rn, struct bgp_path_info *bpi)
65efcfce 1991{
d62a17ae 1992 /*
1993 * Remove the route (doubly-linked)
1994 */
40381db7
DS
1995 // bgp_attr_unintern (&bpi->attr);
1996 if (bpi->next)
1997 bpi->next->prev = bpi->prev;
1998 if (bpi->prev)
1999 bpi->prev->next = bpi->next;
d62a17ae 2000 else
40381db7 2001 rn->info = bpi->next;
65efcfce
LB
2002}
2003
2004/*
2005 * For L3-indexed import tables
2006 */
1a4189d4 2007static int rfapi_bi_peer_rd_cmp(const void *b1, const void *b2)
65efcfce 2008{
1a4189d4
DS
2009 const struct bgp_path_info *bpi1 = b1;
2010 const struct bgp_path_info *bpi2 = b2;
d62a17ae 2011
2012 /*
2013 * Compare peers
2014 */
40381db7 2015 if (bpi1->peer < bpi2->peer)
d62a17ae 2016 return -1;
40381db7 2017 if (bpi1->peer > bpi2->peer)
d62a17ae 2018 return 1;
2019
2020 /*
2021 * compare RDs
2022 */
1a4189d4
DS
2023 return vnc_prefix_cmp(
2024 (const struct prefix *)&bpi1->extra->vnc.import.rd,
2025 (const struct prefix *)&bpi2->extra->vnc.import.rd);
65efcfce
LB
2026}
2027
2028/*
2029 * For L2-indexed import tables
40381db7 2030 * The BPIs in these tables should ALWAYS have an aux_prefix set because
65efcfce
LB
2031 * they arrive via IPv4 or IPv6 advertisements.
2032 */
1a4189d4 2033static int rfapi_bi_peer_rd_aux_cmp(const void *b1, const void *b2)
65efcfce 2034{
1a4189d4
DS
2035 const struct bgp_path_info *bpi1 = b1;
2036 const struct bgp_path_info *bpi2 = b2;
d62a17ae 2037 int rc;
2038
2039 /*
2040 * Compare peers
2041 */
40381db7 2042 if (bpi1->peer < bpi2->peer)
d62a17ae 2043 return -1;
40381db7 2044 if (bpi1->peer > bpi2->peer)
d62a17ae 2045 return 1;
2046
2047 /*
2048 * compare RDs
2049 */
40381db7
DS
2050 rc = vnc_prefix_cmp((struct prefix *)&bpi1->extra->vnc.import.rd,
2051 (struct prefix *)&bpi2->extra->vnc.import.rd);
d62a17ae 2052 if (rc) {
2053 return rc;
2054 }
2055
2056 /*
2057 * L2 import tables can have multiple entries with the
2058 * same MAC address, same RD, but different L3 addresses.
2059 *
2060 * Use presence of aux_prefix with AF=ethernet and prefixlen=1
2061 * as magic value to signify explicit wildcarding of the aux_prefix.
40381db7
DS
2062 * This magic value will not appear in bona fide bpi entries in
2063 * the import table, but is allowed in the "fake" bpi used to
d62a17ae 2064 * probe the table when searching. (We have to test both b1 and b2
2065 * because there is no guarantee of the order the test key and
2066 * the real key will be passed)
2067 */
40381db7
DS
2068 if ((bpi1->extra->vnc.import.aux_prefix.family == AF_ETHERNET
2069 && (bpi1->extra->vnc.import.aux_prefix.prefixlen == 1))
2070 || (bpi2->extra->vnc.import.aux_prefix.family == AF_ETHERNET
2071 && (bpi2->extra->vnc.import.aux_prefix.prefixlen == 1))) {
d62a17ae 2072
2073 /*
2074 * wildcard aux address specified
2075 */
2076 return 0;
2077 }
2078
40381db7
DS
2079 return vnc_prefix_cmp(&bpi1->extra->vnc.import.aux_prefix,
2080 &bpi2->extra->vnc.import.aux_prefix);
65efcfce
LB
2081}
2082
2083
2084/*
2085 * Index on RD and Peer
2086 */
fe08ba7e 2087static void rfapiItBiIndexAdd(struct agg_node *rn, /* Import table VPN node */
40381db7 2088 struct bgp_path_info *bpi) /* new BPI */
65efcfce 2089{
d62a17ae 2090 struct skiplist *sl;
26a3ffd6 2091 const struct prefix *p;
d62a17ae 2092
2093 assert(rn);
40381db7
DS
2094 assert(bpi);
2095 assert(bpi->extra);
d62a17ae 2096
2097 {
06b9f471
DS
2098 char buf[RD_ADDRSTRLEN];
2099
40381db7
DS
2100 vnc_zlog_debug_verbose("%s: bpi %p, peer %p, rd %s", __func__,
2101 bpi, bpi->peer,
2102 prefix_rd2str(&bpi->extra->vnc.import.rd,
06b9f471 2103 buf, sizeof(buf)));
d62a17ae 2104 }
2105
2106 sl = RFAPI_RDINDEX_W_ALLOC(rn);
2107 if (!sl) {
26a3ffd6
DS
2108 p = agg_node_get_prefix(rn);
2109 if (AF_ETHERNET == p->family) {
d62a17ae 2110 sl = skiplist_new(0, rfapi_bi_peer_rd_aux_cmp, NULL);
2111 } else {
2112 sl = skiplist_new(0, rfapi_bi_peer_rd_cmp, NULL);
2113 }
2114 RFAPI_IT_EXTRA_GET(rn)->u.vpn.idx_rd = sl;
fe08ba7e 2115 agg_lock_node(rn); /* for skiplist */
d62a17ae 2116 }
40381db7 2117 assert(!skiplist_insert(sl, (void *)bpi, (void *)bpi));
fe08ba7e 2118 agg_lock_node(rn); /* for skiplist entry */
d62a17ae 2119
40381db7 2120 /* NB: BPIs in import tables are not refcounted */
65efcfce
LB
2121}
2122
fe08ba7e 2123static void rfapiItBiIndexDump(struct agg_node *rn)
65efcfce 2124{
d62a17ae 2125 struct skiplist *sl;
2126 void *cursor = NULL;
4b7e6066
DS
2127 struct bgp_path_info *k;
2128 struct bgp_path_info *v;
d62a17ae 2129 int rc;
2130
2131 sl = RFAPI_RDINDEX(rn);
2132 if (!sl)
2133 return;
2134
2135 for (rc = skiplist_next(sl, (void **)&k, (void **)&v, &cursor); !rc;
2136 rc = skiplist_next(sl, (void **)&k, (void **)&v, &cursor)) {
2137
06b9f471 2138 char buf[RD_ADDRSTRLEN];
872ed4c7 2139 char buf_aux_pfx[PREFIX_STRLEN];
d62a17ae 2140
06b9f471 2141 prefix_rd2str(&k->extra->vnc.import.rd, buf, sizeof(buf));
d62a17ae 2142 if (k->extra->vnc.import.aux_prefix.family) {
2143 prefix2str(&k->extra->vnc.import.aux_prefix,
06b9f471
DS
2144 buf_aux_pfx, sizeof(buf_aux_pfx));
2145 } else
9825c71e 2146 strlcpy(buf_aux_pfx, "(none)", sizeof(buf_aux_pfx));
d62a17ae 2147
40381db7 2148 vnc_zlog_debug_verbose("bpi %p, peer %p, rd %s, aux_prefix %s",
d62a17ae 2149 k, k->peer, buf, buf_aux_pfx);
2150 }
65efcfce
LB
2151}
2152
4b7e6066 2153static struct bgp_path_info *rfapiItBiIndexSearch(
fe08ba7e 2154 struct agg_node *rn, /* Import table VPN node */
d62a17ae 2155 struct prefix_rd *prd, struct peer *peer,
5a1ae2c2 2156 const struct prefix *aux_prefix) /* optional L3 addr for L2 ITs */
65efcfce 2157{
d62a17ae 2158 struct skiplist *sl;
2159 int rc;
b8694ef4
DS
2160 struct bgp_path_info bpi_fake = {0};
2161 struct bgp_path_info_extra bpi_extra = {0};
40381db7 2162 struct bgp_path_info *bpi_result;
65efcfce 2163
d62a17ae 2164 sl = RFAPI_RDINDEX(rn);
2165 if (!sl)
2166 return NULL;
65efcfce 2167
1e20238a 2168#ifdef DEBUG_BI_SEARCH
d62a17ae 2169 {
06b9f471 2170 char buf[RD_ADDRSTRLEN];
872ed4c7 2171 char buf_aux_pfx[PREFIX_STRLEN];
d62a17ae 2172
d62a17ae 2173 if (aux_prefix) {
872ed4c7
DS
2174 prefix2str(aux_prefix, buf_aux_pfx,
2175 sizeof(buf_aux_pfx));
06b9f471 2176 } else
9825c71e 2177 strlcpy(buf_aux_pfx, "(nil)", sizeof(buf_aux_pfx));
d62a17ae 2178
2179 vnc_zlog_debug_verbose("%s want prd=%s, peer=%p, aux_prefix=%s",
06b9f471
DS
2180 __func__,
2181 prefix_rd2str(prd, buf, sizeof(buf)),
2182 peer, buf_aux_pfx);
d62a17ae 2183 rfapiItBiIndexDump(rn);
2184 }
65efcfce
LB
2185#endif
2186
d62a17ae 2187 /* threshold is a WAG */
2188 if (sl->count < 3) {
1e20238a 2189#ifdef DEBUG_BI_SEARCH
d62a17ae 2190 vnc_zlog_debug_verbose("%s: short list algorithm", __func__);
65efcfce 2191#endif
d62a17ae 2192 /* if short list, linear search might be faster */
40381db7
DS
2193 for (bpi_result = rn->info; bpi_result;
2194 bpi_result = bpi_result->next) {
1e20238a 2195#ifdef DEBUG_BI_SEARCH
d62a17ae 2196 {
06b9f471
DS
2197 char buf[RD_ADDRSTRLEN];
2198
d62a17ae 2199 vnc_zlog_debug_verbose(
40381db7
DS
2200 "%s: bpi has prd=%s, peer=%p", __func__,
2201 prefix_rd2str(&bpi_result->extra->vnc
996c9314
LB
2202 .import.rd,
2203 buf, sizeof(buf)),
40381db7 2204 bpi_result->peer);
d62a17ae 2205 }
65efcfce 2206#endif
40381db7
DS
2207 if (peer == bpi_result->peer
2208 && !prefix_cmp((struct prefix *)&bpi_result->extra
d62a17ae 2209 ->vnc.import.rd,
2210 (struct prefix *)prd)) {
65efcfce 2211
1e20238a 2212#ifdef DEBUG_BI_SEARCH
d62a17ae 2213 vnc_zlog_debug_verbose(
2214 "%s: peer and RD same, doing aux_prefix check",
2215 __func__);
65efcfce 2216#endif
d62a17ae 2217 if (!aux_prefix
40381db7
DS
2218 || !prefix_cmp(
2219 aux_prefix,
2220 &bpi_result->extra->vnc.import
2221 .aux_prefix)) {
65efcfce 2222
1e20238a 2223#ifdef DEBUG_BI_SEARCH
d62a17ae 2224 vnc_zlog_debug_verbose("%s: match",
2225 __func__);
65efcfce 2226#endif
d62a17ae 2227 break;
2228 }
2229 }
2230 }
40381db7 2231 return bpi_result;
d62a17ae 2232 }
2233
40381db7
DS
2234 bpi_fake.peer = peer;
2235 bpi_fake.extra = &bpi_extra;
c4efd0f4 2236 bpi_fake.extra->vnc.import.rd = *prd;
d62a17ae 2237 if (aux_prefix) {
40381db7 2238 bpi_fake.extra->vnc.import.aux_prefix = *aux_prefix;
d62a17ae 2239 } else {
2240 /* wildcard */
40381db7
DS
2241 bpi_fake.extra->vnc.import.aux_prefix.family = AF_ETHERNET;
2242 bpi_fake.extra->vnc.import.aux_prefix.prefixlen = 1;
d62a17ae 2243 }
2244
40381db7 2245 rc = skiplist_search(sl, (void *)&bpi_fake, (void *)&bpi_result);
d62a17ae 2246
2247 if (rc) {
1e20238a 2248#ifdef DEBUG_BI_SEARCH
d62a17ae 2249 vnc_zlog_debug_verbose("%s: no match", __func__);
65efcfce 2250#endif
d62a17ae 2251 return NULL;
2252 }
65efcfce 2253
1e20238a 2254#ifdef DEBUG_BI_SEARCH
40381db7 2255 vnc_zlog_debug_verbose("%s: matched bpi=%p", __func__, bpi_result);
65efcfce
LB
2256#endif
2257
40381db7 2258 return bpi_result;
65efcfce
LB
2259}
2260
fe08ba7e 2261static void rfapiItBiIndexDel(struct agg_node *rn, /* Import table VPN node */
40381db7 2262 struct bgp_path_info *bpi) /* old BPI */
65efcfce 2263{
d62a17ae 2264 struct skiplist *sl;
2265 int rc;
65efcfce 2266
d62a17ae 2267 {
06b9f471
DS
2268 char buf[RD_ADDRSTRLEN];
2269
40381db7
DS
2270 vnc_zlog_debug_verbose("%s: bpi %p, peer %p, rd %s", __func__,
2271 bpi, bpi->peer,
2272 prefix_rd2str(&bpi->extra->vnc.import.rd,
06b9f471 2273 buf, sizeof(buf)));
d62a17ae 2274 }
65efcfce 2275
d62a17ae 2276 sl = RFAPI_RDINDEX(rn);
2277 assert(sl);
65efcfce 2278
40381db7 2279 rc = skiplist_delete(sl, (void *)(bpi), (void *)bpi);
d62a17ae 2280 if (rc) {
2281 rfapiItBiIndexDump(rn);
2282 }
2283 assert(!rc);
65efcfce 2284
fe08ba7e 2285 agg_unlock_node(rn); /* for skiplist entry */
65efcfce 2286
40381db7 2287 /* NB: BPIs in import tables are not refcounted */
65efcfce
LB
2288}
2289
2290/*
2291 * Add a backreference at the ENCAP node to the VPN route that
2292 * refers to it
2293 */
4b7e6066
DS
2294static void
2295rfapiMonitorEncapAdd(struct rfapi_import_table *import_table,
40381db7
DS
2296 struct prefix *p, /* VN address */
2297 struct agg_node *vpn_rn, /* VPN node */
2298 struct bgp_path_info *vpn_bpi) /* VPN bpi/route */
65efcfce 2299{
d62a17ae 2300 afi_t afi = family2afi(p->family);
fe08ba7e 2301 struct agg_node *rn;
d62a17ae 2302 struct rfapi_monitor_encap *m;
2303
2304 assert(afi);
fe08ba7e 2305 rn = agg_node_get(import_table->imported_encap[afi], p); /* locks rn */
d62a17ae 2306 assert(rn);
2307
2308 m = XCALLOC(MTYPE_RFAPI_MONITOR_ENCAP,
2309 sizeof(struct rfapi_monitor_encap));
2310 assert(m);
2311
2312 m->node = vpn_rn;
40381db7 2313 m->bpi = vpn_bpi;
d62a17ae 2314 m->rn = rn;
2315
2316 /* insert to encap node's list */
2317 m->next = RFAPI_MONITOR_ENCAP(rn);
2318 if (m->next)
2319 m->next->prev = m;
2320 RFAPI_MONITOR_ENCAP_W_ALLOC(rn) = m;
2321
2322 /* for easy lookup when deleting vpn route */
40381db7 2323 vpn_bpi->extra->vnc.import.hme = m;
d62a17ae 2324
2325 vnc_zlog_debug_verbose(
40381db7
DS
2326 "%s: it=%p, vpn_bpi=%p, afi=%d, encap rn=%p, setting vpn_bpi->extra->vnc.import.hme=%p",
2327 __func__, import_table, vpn_bpi, afi, rn, m);
d62a17ae 2328
2329 RFAPI_CHECK_REFCOUNT(rn, SAFI_ENCAP, 0);
40381db7 2330 bgp_attr_intern(vpn_bpi->attr);
65efcfce
LB
2331}
2332
40381db7 2333static void rfapiMonitorEncapDelete(struct bgp_path_info *vpn_bpi)
65efcfce 2334{
d62a17ae 2335 /*
2336 * Remove encap monitor
2337 */
40381db7
DS
2338 vnc_zlog_debug_verbose("%s: vpn_bpi=%p", __func__, vpn_bpi);
2339 if (vpn_bpi->extra) {
2340 struct rfapi_monitor_encap *hme =
2341 vpn_bpi->extra->vnc.import.hme;
d62a17ae 2342
2343 if (hme) {
2344
2345 vnc_zlog_debug_verbose("%s: hme=%p", __func__, hme);
2346
2347 /* Refcount checking takes too long here */
2348 // RFAPI_CHECK_REFCOUNT(hme->rn, SAFI_ENCAP, 0);
2349 if (hme->next)
2350 hme->next->prev = hme->prev;
2351 if (hme->prev)
2352 hme->prev->next = hme->next;
2353 else
2354 RFAPI_MONITOR_ENCAP_W_ALLOC(hme->rn) =
2355 hme->next;
2356 /* Refcount checking takes too long here */
2357 // RFAPI_CHECK_REFCOUNT(hme->rn, SAFI_ENCAP, 1);
2358
2359 /* see if the struct rfapi_it_extra is empty and can be
2360 * freed */
2361 rfapiMonitorExtraPrune(SAFI_ENCAP, hme->rn);
2362
fe08ba7e 2363 agg_unlock_node(hme->rn); /* decr ref count */
d62a17ae 2364 XFREE(MTYPE_RFAPI_MONITOR_ENCAP, hme);
40381db7 2365 vpn_bpi->extra->vnc.import.hme = NULL;
d62a17ae 2366 }
2367 }
65efcfce
LB
2368}
2369
2370/*
2371 * quagga lib/thread.h says this must return int even though
2372 * it doesn't do anything with the return value
2373 */
d62a17ae 2374static int rfapiWithdrawTimerVPN(struct thread *t)
65efcfce 2375{
d62a17ae 2376 struct rfapi_withdraw *wcb = t->arg;
40381db7 2377 struct bgp_path_info *bpi = wcb->info;
d62a17ae 2378 struct bgp *bgp = bgp_get_default();
26a3ffd6 2379 const struct prefix *p;
d62a17ae 2380 struct rfapi_monitor_vpn *moved;
2381 afi_t afi;
2382
f4c713ae
LB
2383 if (bgp == NULL) {
2384 vnc_zlog_debug_verbose(
2385 "%s: NULL BGP pointer, assume shutdown race condition!!!",
2386 __func__);
2387 return 0;
2388 }
892fedb6 2389 if (CHECK_FLAG(bgp->flags, BGP_FLAG_DELETE_IN_PROGRESS)) {
f4c713ae 2390 vnc_zlog_debug_verbose(
892fedb6
DA
2391 "%s: BGP delete in progress, assume shutdown race condition!!!",
2392 __func__);
f4c713ae
LB
2393 return 0;
2394 }
d62a17ae 2395 assert(wcb->node);
40381db7 2396 assert(bpi);
d62a17ae 2397 assert(wcb->import_table);
40381db7 2398 assert(bpi->extra);
d62a17ae 2399
2400 RFAPI_CHECK_REFCOUNT(wcb->node, SAFI_MPLS_VPN, wcb->lockoffset);
2401
26a3ffd6
DS
2402 vnc_zlog_debug_verbose("%s: removing bpi %p at prefix %pRN", __func__,
2403 bpi, wcb->node);
d62a17ae 2404
2405 /*
2406 * Remove the route (doubly-linked)
2407 */
40381db7
DS
2408 if (CHECK_FLAG(bpi->flags, BGP_PATH_VALID)
2409 && VALID_INTERIOR_TYPE(bpi->type))
d62a17ae 2410 RFAPI_MONITOR_EXTERIOR(wcb->node)->valid_interior_count--;
2411
26a3ffd6
DS
2412 p = agg_node_get_prefix(wcb->node);
2413 afi = family2afi(p->family);
d62a17ae 2414 wcb->import_table->holddown_count[afi] -= 1; /* keep count consistent */
40381db7
DS
2415 rfapiItBiIndexDel(wcb->node, bpi);
2416 rfapiBgpInfoDetach(wcb->node, bpi); /* with removed bpi */
d62a17ae 2417
2418 vnc_import_bgp_exterior_del_route_interior(bgp, wcb->import_table,
40381db7 2419 wcb->node, bpi);
d62a17ae 2420
2421
2422 /*
2423 * If VNC is configured to send response remove messages, AND
2424 * if the removed route had a UN address, do response removal
2425 * processing.
2426 */
2427 if (!(bgp->rfapi_cfg->flags
2428 & BGP_VNC_CONFIG_RESPONSE_REMOVAL_DISABLE)) {
2429
2430 int has_valid_duplicate = 0;
40381db7 2431 struct bgp_path_info *bpii;
d62a17ae 2432
2433 /*
2434 * First check if there are any OTHER routes at this node
2435 * that have the same nexthop and a valid UN address. If
2436 * there are (e.g., from other peers), then the route isn't
2437 * really gone, so skip sending a response removal message.
2438 */
40381db7
DS
2439 for (bpii = wcb->node->info; bpii; bpii = bpii->next) {
2440 if (rfapiVpnBiSamePtUn(bpi, bpii)) {
d62a17ae 2441 has_valid_duplicate = 1;
2442 break;
2443 }
2444 }
2445
2446 vnc_zlog_debug_verbose("%s: has_valid_duplicate=%d", __func__,
2447 has_valid_duplicate);
2448
2449 if (!has_valid_duplicate) {
2450 rfapiRibPendingDeleteRoute(bgp, wcb->import_table, afi,
2451 wcb->node);
2452 }
2453 }
2454
40381db7 2455 rfapiMonitorEncapDelete(bpi);
d62a17ae 2456
2457 /*
2458 * If there are no VPN monitors at this VPN Node A,
2459 * we are done
2460 */
2461 if (!RFAPI_MONITOR_VPN(wcb->node)) {
2462 vnc_zlog_debug_verbose("%s: no VPN monitors at this node",
2463 __func__);
2464 goto done;
2465 }
2466
2467 /*
2468 * rfapiMonitorMoveShorter only moves monitors if there are
2469 * no remaining valid routes at the current node
2470 */
2471 moved = rfapiMonitorMoveShorter(wcb->node, 1);
2472
2473 if (moved) {
2474 rfapiMonitorMovedUp(wcb->import_table, wcb->node, moved->node,
2475 moved);
2476 }
65efcfce
LB
2477
2478done:
d62a17ae 2479 /*
40381db7 2480 * Free VPN bpi
d62a17ae 2481 */
40381db7 2482 rfapiBgpInfoFree(bpi);
d62a17ae 2483 wcb->info = NULL;
2484
2485 /*
2486 * If route count at this node has gone to 0, withdraw exported prefix
2487 */
2488 if (!wcb->node->info) {
2489 /* see if the struct rfapi_it_extra is empty and can be freed */
2490 rfapiMonitorExtraPrune(SAFI_MPLS_VPN, wcb->node);
2491 vnc_direct_bgp_del_prefix(bgp, wcb->import_table, wcb->node);
2492 vnc_zebra_del_prefix(bgp, wcb->import_table, wcb->node);
2493 } else {
2494 /*
2495 * nexthop change event
2496 * vnc_direct_bgp_add_prefix() will recompute the VN addr
2497 * ecommunity
2498 */
2499 vnc_direct_bgp_add_prefix(bgp, wcb->import_table, wcb->node);
2500 }
2501
2502 RFAPI_CHECK_REFCOUNT(wcb->node, SAFI_MPLS_VPN, 1 + wcb->lockoffset);
fe08ba7e 2503 agg_unlock_node(wcb->node); /* decr ref count */
d62a17ae 2504 XFREE(MTYPE_RFAPI_WITHDRAW, wcb);
2505 return 0;
65efcfce
LB
2506}
2507
2508/*
2509 * This works for multiprotocol extension, but not for plain ol'
2510 * unicast IPv4 because that nexthop is stored in attr->nexthop
2511 */
d62a17ae 2512void rfapiNexthop2Prefix(struct attr *attr, struct prefix *p)
65efcfce 2513{
d62a17ae 2514 assert(p);
2515 assert(attr);
2516
2517 memset(p, 0, sizeof(struct prefix));
2518
2519 switch (p->family = BGP_MP_NEXTHOP_FAMILY(attr->mp_nexthop_len)) {
2520 case AF_INET:
2521 p->u.prefix4 = attr->mp_nexthop_global_in;
2522 p->prefixlen = 32;
2523 break;
2524
2525 case AF_INET6:
2526 p->u.prefix6 = attr->mp_nexthop_global;
2527 p->prefixlen = 128;
2528 break;
2529
2530 default:
2531 vnc_zlog_debug_verbose("%s: Family is unknown = %d", __func__,
2532 p->family);
2533 }
65efcfce
LB
2534}
2535
d62a17ae 2536void rfapiUnicastNexthop2Prefix(afi_t afi, struct attr *attr, struct prefix *p)
65efcfce 2537{
d62a17ae 2538 if (afi == AFI_IP) {
2539 p->family = AF_INET;
2540 p->prefixlen = 32;
2541 p->u.prefix4 = attr->nexthop;
2542 } else {
2543 rfapiNexthop2Prefix(attr, p);
2544 }
65efcfce
LB
2545}
2546
d62a17ae 2547static int rfapiAttrNexthopAddrDifferent(struct prefix *p1, struct prefix *p2)
65efcfce 2548{
d62a17ae 2549 if (!p1 || !p2) {
2550 vnc_zlog_debug_verbose("%s: p1 or p2 is NULL", __func__);
2551 return 1;
2552 }
2553
2554 /*
2555 * Are address families the same?
2556 */
2557 if (p1->family != p2->family) {
2558 return 1;
2559 }
2560
2561 switch (p1->family) {
2562 case AF_INET:
2563 if (IPV4_ADDR_SAME(&p1->u.prefix4, &p2->u.prefix4))
2564 return 0;
2565 break;
2566
2567 case AF_INET6:
2568 if (IPV6_ADDR_SAME(&p1->u.prefix6, &p2->u.prefix6))
2569 return 0;
2570 break;
2571
2572 default:
2573 assert(1);
2574 }
2575
2576 return 1;
65efcfce
LB
2577}
2578
40381db7
DS
2579static void rfapiCopyUnEncap2VPN(struct bgp_path_info *encap_bpi,
2580 struct bgp_path_info *vpn_bpi)
65efcfce 2581{
40381db7
DS
2582 if (!vpn_bpi || !vpn_bpi->extra) {
2583 zlog_warn("%s: no vpn bpi attr/extra, can't copy UN address",
d62a17ae 2584 __func__);
2585 return;
2586 }
2587
40381db7 2588 switch (BGP_MP_NEXTHOP_FAMILY(encap_bpi->attr->mp_nexthop_len)) {
d62a17ae 2589 case AF_INET:
2590
2591 /*
2592 * instrumentation to debug segfault of 091127
2593 */
40381db7
DS
2594 vnc_zlog_debug_verbose("%s: vpn_bpi=%p", __func__, vpn_bpi);
2595 if (vpn_bpi) {
2596 vnc_zlog_debug_verbose("%s: vpn_bpi->extra=%p",
2597 __func__, vpn_bpi->extra);
d62a17ae 2598 }
2599
40381db7
DS
2600 vpn_bpi->extra->vnc.import.un_family = AF_INET;
2601 vpn_bpi->extra->vnc.import.un.addr4 =
2602 encap_bpi->attr->mp_nexthop_global_in;
d62a17ae 2603 break;
2604
2605 case AF_INET6:
40381db7
DS
2606 vpn_bpi->extra->vnc.import.un_family = AF_INET6;
2607 vpn_bpi->extra->vnc.import.un.addr6 =
2608 encap_bpi->attr->mp_nexthop_global;
d62a17ae 2609 break;
2610
2611 default:
2612 zlog_warn("%s: invalid encap nexthop length: %d", __func__,
40381db7
DS
2613 encap_bpi->attr->mp_nexthop_len);
2614 vpn_bpi->extra->vnc.import.un_family = 0;
d62a17ae 2615 break;
2616 }
65efcfce
LB
2617}
2618
2619/*
2620 * returns 0 on success, nonzero on error
2621 */
40381db7
DS
2622static int
2623rfapiWithdrawEncapUpdateCachedUn(struct rfapi_import_table *import_table,
2624 struct bgp_path_info *encap_bpi,
2625 struct agg_node *vpn_rn,
2626 struct bgp_path_info *vpn_bpi)
65efcfce 2627{
40381db7 2628 if (!encap_bpi) {
d62a17ae 2629
2630 /*
2631 * clear cached UN address
2632 */
40381db7 2633 if (!vpn_bpi || !vpn_bpi->extra) {
d62a17ae 2634 zlog_warn(
40381db7 2635 "%s: missing VPN bpi/extra, can't clear UN addr",
d62a17ae 2636 __func__);
2637 return 1;
2638 }
40381db7
DS
2639 vpn_bpi->extra->vnc.import.un_family = 0;
2640 memset(&vpn_bpi->extra->vnc.import.un, 0,
2641 sizeof(vpn_bpi->extra->vnc.import.un));
2642 if (CHECK_FLAG(vpn_bpi->flags, BGP_PATH_VALID)) {
2643 if (rfapiGetVncTunnelUnAddr(vpn_bpi->attr, NULL)) {
2644 UNSET_FLAG(vpn_bpi->flags, BGP_PATH_VALID);
2645 if (VALID_INTERIOR_TYPE(vpn_bpi->type))
d62a17ae 2646 RFAPI_MONITOR_EXTERIOR(vpn_rn)
2647 ->valid_interior_count--;
2648 /* signal interior route withdrawal to
2649 * import-exterior */
2650 vnc_import_bgp_exterior_del_route_interior(
2651 bgp_get_default(), import_table, vpn_rn,
40381db7 2652 vpn_bpi);
d62a17ae 2653 }
2654 }
2655
2656 } else {
40381db7
DS
2657 if (!vpn_bpi) {
2658 zlog_warn("%s: missing VPN bpi, can't clear UN addr",
d62a17ae 2659 __func__);
2660 return 1;
2661 }
40381db7
DS
2662 rfapiCopyUnEncap2VPN(encap_bpi, vpn_bpi);
2663 if (!CHECK_FLAG(vpn_bpi->flags, BGP_PATH_VALID)) {
2664 SET_FLAG(vpn_bpi->flags, BGP_PATH_VALID);
2665 if (VALID_INTERIOR_TYPE(vpn_bpi->type))
d62a17ae 2666 RFAPI_MONITOR_EXTERIOR(vpn_rn)
2667 ->valid_interior_count++;
2668 /* signal interior route withdrawal to import-exterior
2669 */
2670 vnc_import_bgp_exterior_add_route_interior(
2671 bgp_get_default(), import_table, vpn_rn,
40381db7 2672 vpn_bpi);
d62a17ae 2673 }
2674 }
2675 return 0;
65efcfce
LB
2676}
2677
d62a17ae 2678static int rfapiWithdrawTimerEncap(struct thread *t)
65efcfce 2679{
d62a17ae 2680 struct rfapi_withdraw *wcb = t->arg;
40381db7 2681 struct bgp_path_info *bpi = wcb->info;
d62a17ae 2682 int was_first_route = 0;
2683 struct rfapi_monitor_encap *em;
2684 struct skiplist *vpn_node_sl = skiplist_new(0, NULL, NULL);
2685
2686 assert(wcb->node);
40381db7 2687 assert(bpi);
d62a17ae 2688 assert(wcb->import_table);
2689
2690 RFAPI_CHECK_REFCOUNT(wcb->node, SAFI_ENCAP, 0);
2691
40381db7 2692 if (wcb->node->info == bpi)
d62a17ae 2693 was_first_route = 1;
2694
2695 /*
40381db7 2696 * Remove the route/bpi and free it
d62a17ae 2697 */
40381db7
DS
2698 rfapiBgpInfoDetach(wcb->node, bpi);
2699 rfapiBgpInfoFree(bpi);
d62a17ae 2700
2701 if (!was_first_route)
2702 goto done;
2703
2704 for (em = RFAPI_MONITOR_ENCAP(wcb->node); em; em = em->next) {
2705
2706 /*
40381db7
DS
2707 * Update monitoring VPN BPIs with new encap info at the
2708 * head of the encap bpi chain (which could be NULL after
2709 * removing the expiring bpi above)
d62a17ae 2710 */
2711 if (rfapiWithdrawEncapUpdateCachedUn(wcb->import_table,
2712 wcb->node->info, em->node,
40381db7 2713 em->bpi))
d62a17ae 2714 continue;
2715
2716 /*
2717 * Build a list of unique VPN nodes referenced by these
2718 * monitors.
2719 * Use a skiplist for speed.
2720 */
2721 skiplist_insert(vpn_node_sl, em->node, em->node);
2722 }
2723
2724
2725 /*
2726 * for each VPN node referenced in the ENCAP monitors:
2727 */
fe08ba7e 2728 struct agg_node *rn;
d62a17ae 2729 while (!skiplist_first(vpn_node_sl, (void **)&rn, NULL)) {
2730 if (!wcb->node->info) {
2731 struct rfapi_monitor_vpn *moved;
2732
2733 moved = rfapiMonitorMoveShorter(rn, 0);
2734 if (moved) {
2735 // rfapiDoRouteCallback(wcb->import_table,
2736 // moved->node, moved);
2737 rfapiMonitorMovedUp(wcb->import_table, rn,
2738 moved->node, moved);
2739 }
2740 } else {
2741 // rfapiDoRouteCallback(wcb->import_table, rn, NULL);
2742 rfapiMonitorItNodeChanged(wcb->import_table, rn, NULL);
2743 }
2744 skiplist_delete_first(vpn_node_sl);
2745 }
65efcfce
LB
2746
2747done:
d62a17ae 2748 RFAPI_CHECK_REFCOUNT(wcb->node, SAFI_ENCAP, 1);
fe08ba7e 2749 agg_unlock_node(wcb->node); /* decr ref count */
d62a17ae 2750 XFREE(MTYPE_RFAPI_WITHDRAW, wcb);
2751 skiplist_free(vpn_node_sl);
2752 return 0;
65efcfce
LB
2753}
2754
2755
2756/*
2757 * Works for both VPN and ENCAP routes; timer_service_func is different
2758 * in each case
2759 */
2760static void
d62a17ae 2761rfapiBiStartWithdrawTimer(struct rfapi_import_table *import_table,
40381db7 2762 struct agg_node *rn, struct bgp_path_info *bpi,
4b7e6066 2763 afi_t afi, safi_t safi,
d62a17ae 2764 int (*timer_service_func)(struct thread *))
65efcfce 2765{
d62a17ae 2766 uint32_t lifetime;
2767 struct rfapi_withdraw *wcb;
2768
40381db7 2769 if (CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED)) {
a8fd6fde
QY
2770 /*
2771 * Already on the path to being withdrawn,
2772 * should already have a timer set up to
2773 * delete it.
2774 */
2775 vnc_zlog_debug_verbose(
2776 "%s: already being withdrawn, do nothing", __func__);
2777 return;
2778 }
d62a17ae 2779
40381db7 2780 rfapiGetVncLifetime(bpi->attr, &lifetime);
d62a17ae 2781 vnc_zlog_debug_verbose("%s: VNC lifetime is %u", __func__, lifetime);
2782
2783 /*
2784 * withdrawn routes get to hang around for a while
2785 */
40381db7 2786 SET_FLAG(bpi->flags, BGP_PATH_REMOVED);
d62a17ae 2787
2788 /* set timer to remove the route later */
2789 lifetime = rfapiGetHolddownFromLifetime(lifetime);
2790 vnc_zlog_debug_verbose("%s: using timeout %u", __func__, lifetime);
2791
2792 /*
2793 * Stash import_table, node, and info for use by timer
2794 * service routine, which is supposed to free the wcb.
2795 */
2796 wcb = XCALLOC(MTYPE_RFAPI_WITHDRAW, sizeof(struct rfapi_withdraw));
2797 assert(wcb);
2798 wcb->node = rn;
40381db7 2799 wcb->info = bpi;
d62a17ae 2800 wcb->import_table = import_table;
40381db7 2801 bgp_attr_intern(bpi->attr);
d62a17ae 2802
2803 if (VNC_DEBUG(VERBOSE)) {
2804 vnc_zlog_debug_verbose(
40381db7 2805 "%s: wcb values: node=%p, info=%p, import_table=%p (bpi follows)",
d62a17ae 2806 __func__, wcb->node, wcb->info, wcb->import_table);
40381db7 2807 rfapiPrintBi(NULL, bpi);
d62a17ae 2808 }
2809
2810
40381db7 2811 assert(bpi->extra);
d62a17ae 2812 if (lifetime > UINT32_MAX / 1001) {
2813 /* sub-optimal case, but will probably never happen */
40381db7 2814 bpi->extra->vnc.import.timer = NULL;
d62a17ae 2815 thread_add_timer(bm->master, timer_service_func, wcb, lifetime,
40381db7 2816 &bpi->extra->vnc.import.timer);
d62a17ae 2817 } else {
2818 static uint32_t jitter;
2819 uint32_t lifetime_msec;
2820
2821 /*
2822 * the goal here is to spread out the timers so they are
2823 * sortable in the skip list
2824 */
2825 if (++jitter >= 1000)
2826 jitter = 0;
2827
2828 lifetime_msec = (lifetime * 1000) + jitter;
2829
40381db7 2830 bpi->extra->vnc.import.timer = NULL;
d62a17ae 2831 thread_add_timer_msec(bm->master, timer_service_func, wcb,
2832 lifetime_msec,
40381db7 2833 &bpi->extra->vnc.import.timer);
d62a17ae 2834 }
2835
1defdda8 2836 /* re-sort route list (BGP_PATH_REMOVED routes are last) */
4b7e6066 2837 if (((struct bgp_path_info *)rn->info)->next) {
40381db7
DS
2838 rfapiBgpInfoDetach(rn, bpi);
2839 rfapiBgpInfoAttachSorted(rn, bpi, afi, safi);
d62a17ae 2840 }
65efcfce
LB
2841}
2842
2843
5a1ae2c2
DS
2844typedef void(rfapi_bi_filtered_import_f)(struct rfapi_import_table *table,
2845 int action, struct peer *peer,
2846 void *rfd, const struct prefix *prefix,
2847 const struct prefix *aux_prefix,
2848 afi_t afi, struct prefix_rd *prd,
2849 struct attr *attr, uint8_t type,
2850 uint8_t sub_type, uint32_t *label);
65efcfce
LB
2851
2852
d62a17ae 2853static void rfapiExpireEncapNow(struct rfapi_import_table *it,
40381db7 2854 struct agg_node *rn, struct bgp_path_info *bpi)
65efcfce 2855{
d62a17ae 2856 struct rfapi_withdraw *wcb;
2857 struct thread t;
2858
2859 /*
2860 * pretend we're an expiring timer
2861 */
2862 wcb = XCALLOC(MTYPE_RFAPI_WITHDRAW, sizeof(struct rfapi_withdraw));
40381db7 2863 wcb->info = bpi;
d62a17ae 2864 wcb->node = rn;
2865 wcb->import_table = it;
2866 memset(&t, 0, sizeof(t));
2867 t.arg = wcb;
2868 rfapiWithdrawTimerEncap(&t); /* frees wcb */
65efcfce
LB
2869}
2870
d62a17ae 2871static int rfapiGetNexthop(struct attr *attr, struct prefix *prefix)
65efcfce 2872{
d62a17ae 2873 switch (BGP_MP_NEXTHOP_FAMILY(attr->mp_nexthop_len)) {
2874 case AF_INET:
2875 prefix->family = AF_INET;
2876 prefix->prefixlen = 32;
2877 prefix->u.prefix4 = attr->mp_nexthop_global_in;
2878 break;
2879 case AF_INET6:
2880 prefix->family = AF_INET6;
2881 prefix->prefixlen = 128;
2882 prefix->u.prefix6 = attr->mp_nexthop_global;
2883 break;
2884 default:
2885 vnc_zlog_debug_verbose("%s: unknown attr->mp_nexthop_len %d",
2886 __func__, attr->mp_nexthop_len);
2887 return EINVAL;
2888 }
2889 return 0;
65efcfce
LB
2890}
2891
d62a17ae 2892/*
9b6d8fcf 2893 * import a bgp_path_info if its route target list intersects with the
65efcfce
LB
2894 * import table's route target list
2895 */
d62a17ae 2896static void rfapiBgpInfoFilteredImportEncap(
2897 struct rfapi_import_table *import_table, int action, struct peer *peer,
2898 void *rfd, /* set for looped back routes */
5a1ae2c2
DS
2899 const struct prefix *p,
2900 const struct prefix *aux_prefix, /* Unused for encap routes */
d62a17ae 2901 afi_t afi, struct prefix_rd *prd,
9b6d8fcf 2902 struct attr *attr, /* part of bgp_path_info */
5a1ae2c2 2903 uint8_t type, /* part of bgp_path_info */
9b6d8fcf
DS
2904 uint8_t sub_type, /* part of bgp_path_info */
2905 uint32_t *label) /* part of bgp_path_info */
65efcfce 2906{
fe08ba7e
DS
2907 struct agg_table *rt = NULL;
2908 struct agg_node *rn;
4b7e6066 2909 struct bgp_path_info *info_new;
40381db7 2910 struct bgp_path_info *bpi;
4b7e6066 2911 struct bgp_path_info *next;
d62a17ae 2912 char buf[BUFSIZ];
2913
40381db7
DS
2914 struct prefix p_firstbpi_old;
2915 struct prefix p_firstbpi_new;
d62a17ae 2916 int replacing = 0;
2917 const char *action_str = NULL;
2918 struct prefix un_prefix;
2919
2920 struct bgp *bgp;
2921 bgp = bgp_get_default(); /* assume 1 instance for now */
2922
2923 switch (action) {
2924 case FIF_ACTION_UPDATE:
2925 action_str = "update";
2926 break;
2927 case FIF_ACTION_WITHDRAW:
2928 action_str = "withdraw";
2929 break;
2930 case FIF_ACTION_KILL:
2931 action_str = "kill";
2932 break;
2933 default:
2934 assert(0);
2935 break;
2936 }
2937
2938 vnc_zlog_debug_verbose(
2939 "%s: entry: %s: prefix %s/%d", __func__, action_str,
2940 inet_ntop(p->family, &p->u.prefix, buf, BUFSIZ), p->prefixlen);
2941
40381db7
DS
2942 memset(&p_firstbpi_old, 0, sizeof(p_firstbpi_old));
2943 memset(&p_firstbpi_new, 0, sizeof(p_firstbpi_new));
d62a17ae 2944
2945 if (action == FIF_ACTION_UPDATE) {
2946 /*
2947 * Compare rt lists. If no intersection, don't import this route
2948 * On a withdraw, peer and RD are sufficient to determine if
2949 * we should act.
2950 */
2951 if (!attr || !attr->ecommunity) {
2952
2953 vnc_zlog_debug_verbose(
2954 "%s: attr, extra, or ecommunity missing, not importing",
2955 __func__);
2956 return;
2957 }
1e20238a 2958#ifdef RFAPI_REQUIRE_ENCAP_BEEC
d62a17ae 2959 if (!rfapiEcommunitiesMatchBeec(attr->ecommunity)) {
2960 vnc_zlog_debug_verbose(
2961 "%s: it=%p: no match for BGP Encapsulation ecommunity",
2962 __func__, import_table);
2963 return;
2964 }
65efcfce 2965#endif
d62a17ae 2966 if (!rfapiEcommunitiesIntersect(import_table->rt_import_list,
2967 attr->ecommunity)) {
2968
2969 vnc_zlog_debug_verbose(
2970 "%s: it=%p: no ecommunity intersection",
2971 __func__, import_table);
2972 return;
2973 }
2974
2975 /*
2976 * Updates must also have a nexthop address
2977 */
2978 memset(&un_prefix, 0,
2979 sizeof(un_prefix)); /* keep valgrind happy */
2980 if (rfapiGetNexthop(attr, &un_prefix)) {
2981 vnc_zlog_debug_verbose("%s: missing nexthop address",
2982 __func__);
2983 return;
2984 }
2985 }
2986
2987 /*
2988 * Figure out which radix tree the route would go into
2989 */
2990 switch (afi) {
2991 case AFI_IP:
2992 case AFI_IP6:
2993 rt = import_table->imported_encap[afi];
2994 break;
2995
2996 default:
450971aa 2997 flog_err(EC_LIB_DEVELOPMENT, "%s: bad afi %d", __func__, afi);
d62a17ae 2998 return;
2999 }
3000
3001 /*
fe08ba7e 3002 * agg_node_lookup returns a node only if there is at least
d62a17ae 3003 * one route attached.
3004 */
fe08ba7e 3005 rn = agg_node_lookup(rt, p);
65efcfce 3006
1e20238a 3007#ifdef DEBUG_ENCAP_MONITOR
d62a17ae 3008 vnc_zlog_debug_verbose("%s: initial encap lookup(it=%p) rn=%p",
3009 __func__, import_table, rn);
65efcfce
LB
3010#endif
3011
d62a17ae 3012 if (rn) {
3013
3014 RFAPI_CHECK_REFCOUNT(rn, SAFI_ENCAP, 1);
fe08ba7e 3015 agg_unlock_node(rn); /* undo lock in agg_node_lookup */
d62a17ae 3016
3017
3018 /*
40381db7 3019 * capture nexthop of first bpi
d62a17ae 3020 */
3021 if (rn->info) {
3022 rfapiNexthop2Prefix(
4b7e6066 3023 ((struct bgp_path_info *)(rn->info))->attr,
40381db7 3024 &p_firstbpi_old);
d62a17ae 3025 }
3026
40381db7 3027 for (bpi = rn->info; bpi; bpi = bpi->next) {
d62a17ae 3028
3029 /*
9b6d8fcf 3030 * Does this bgp_path_info refer to the same route
d62a17ae 3031 * as we are trying to add?
3032 */
40381db7
DS
3033 vnc_zlog_debug_verbose("%s: comparing BPI %p", __func__,
3034 bpi);
d62a17ae 3035
3036
3037 /*
3038 * Compare RDs
3039 *
40381db7
DS
3040 * RD of import table bpi is in
3041 * bpi->extra->vnc.import.rd RD of info_orig is in prd
d62a17ae 3042 */
40381db7
DS
3043 if (!bpi->extra) {
3044 vnc_zlog_debug_verbose("%s: no bpi->extra",
d62a17ae 3045 __func__);
3046 continue;
3047 }
3048 if (prefix_cmp(
40381db7 3049 (struct prefix *)&bpi->extra->vnc.import.rd,
d62a17ae 3050 (struct prefix *)prd)) {
3051
3052 vnc_zlog_debug_verbose("%s: prd does not match",
3053 __func__);
3054 continue;
3055 }
3056
3057 /*
3058 * Compare peers
3059 */
40381db7 3060 if (bpi->peer != peer) {
d62a17ae 3061 vnc_zlog_debug_verbose(
3062 "%s: peer does not match", __func__);
3063 continue;
3064 }
3065
40381db7 3066 vnc_zlog_debug_verbose("%s: found matching bpi",
d62a17ae 3067 __func__);
3068
40381db7 3069 /* Same route. Delete this bpi, replace with new one */
d62a17ae 3070
3071 if (action == FIF_ACTION_WITHDRAW) {
3072
3073 vnc_zlog_debug_verbose(
26a3ffd6
DS
3074 "%s: withdrawing at prefix %pRN",
3075 __func__, rn);
d62a17ae 3076
3077 rfapiBiStartWithdrawTimer(
40381db7 3078 import_table, rn, bpi, afi, SAFI_ENCAP,
d62a17ae 3079 rfapiWithdrawTimerEncap);
3080
3081 } else {
3082 vnc_zlog_debug_verbose(
26a3ffd6 3083 "%s: %s at prefix %pRN", __func__,
d62a17ae 3084 ((action == FIF_ACTION_KILL)
3085 ? "killing"
3086 : "replacing"),
26a3ffd6 3087 rn);
d62a17ae 3088
3089 /*
3090 * If this route is waiting to be deleted
3091 * because of
3092 * a previous withdraw, we must cancel its
3093 * timer.
3094 */
40381db7
DS
3095 if (CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED)
3096 && bpi->extra->vnc.import.timer) {
d62a17ae 3097
b3d6bc6e
MS
3098 struct thread **t =
3099 &(bpi->extra->vnc.import.timer);
3100 struct rfapi_withdraw *wcb = (*t)->arg;
d62a17ae 3101
3102 XFREE(MTYPE_RFAPI_WITHDRAW, wcb);
3103 thread_cancel(t);
3104 }
3105
3106 if (action == FIF_ACTION_UPDATE) {
40381db7
DS
3107 rfapiBgpInfoDetach(rn, bpi);
3108 rfapiBgpInfoFree(bpi);
d62a17ae 3109 replacing = 1;
3110 } else {
3111 /*
3112 * Kill: do export stuff when removing
40381db7 3113 * bpi
d62a17ae 3114 */
3115 struct rfapi_withdraw *wcb;
3116 struct thread t;
3117
3118 /*
3119 * pretend we're an expiring timer
3120 */
3121 wcb = XCALLOC(
3122 MTYPE_RFAPI_WITHDRAW,
3123 sizeof(struct rfapi_withdraw));
40381db7 3124 wcb->info = bpi;
d62a17ae 3125 wcb->node = rn;
3126 wcb->import_table = import_table;
3127 memset(&t, 0, sizeof(t));
3128 t.arg = wcb;
3129 rfapiWithdrawTimerEncap(
3130 &t); /* frees wcb */
3131 }
3132 }
3133
3134 break;
3135 }
3136 }
3137
3138 if (rn)
3139 RFAPI_CHECK_REFCOUNT(rn, SAFI_ENCAP, replacing ? 1 : 0);
3140
3141 if (action == FIF_ACTION_WITHDRAW || action == FIF_ACTION_KILL)
3142 return;
3143
3144 info_new =
3145 rfapiBgpInfoCreate(attr, peer, rfd, prd, type, sub_type, NULL);
3146
3147 if (rn) {
3148 if (!replacing)
40381db7 3149 agg_lock_node(rn); /* incr ref count for new BPI */
d62a17ae 3150 } else {
fe08ba7e 3151 rn = agg_node_get(rt, p);
d62a17ae 3152 }
3153
26a3ffd6
DS
3154 vnc_zlog_debug_verbose("%s: (afi=%d, rn=%p) inserting at prefix %pRN",
3155 __func__, afi, rn, rn);
d62a17ae 3156
3157 rfapiBgpInfoAttachSorted(rn, info_new, afi, SAFI_ENCAP);
3158
3159 /*
3160 * Delete holddown routes from same NVE. See details in
3161 * rfapiBgpInfoFilteredImportVPN()
3162 */
40381db7 3163 for (bpi = info_new->next; bpi; bpi = next) {
d62a17ae 3164
3165 struct prefix pfx_un;
3166 int un_match = 0;
3167
40381db7
DS
3168 next = bpi->next;
3169 if (!CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED))
d62a17ae 3170 continue;
3171
3172 /*
3173 * We already match the VN address (it is the prefix
3174 * of the route node)
3175 */
3176
40381db7 3177 if (!rfapiGetNexthop(bpi->attr, &pfx_un)
d62a17ae 3178 && prefix_same(&pfx_un, &un_prefix)) {
3179
3180 un_match = 1;
3181 }
3182
3183 if (!un_match)
3184 continue;
3185
3186 vnc_zlog_debug_verbose(
40381db7 3187 "%s: removing holddown bpi matching NVE of new route",
d62a17ae 3188 __func__);
40381db7 3189 if (bpi->extra->vnc.import.timer) {
b3d6bc6e
MS
3190 struct thread **t =
3191 &(bpi->extra->vnc.import.timer);
3192 struct rfapi_withdraw *wcb = (*t)->arg;
d62a17ae 3193
3194 XFREE(MTYPE_RFAPI_WITHDRAW, wcb);
3195 thread_cancel(t);
3196 }
40381db7 3197 rfapiExpireEncapNow(import_table, rn, bpi);
d62a17ae 3198 }
3199
4b7e6066 3200 rfapiNexthop2Prefix(((struct bgp_path_info *)(rn->info))->attr,
40381db7 3201 &p_firstbpi_new);
d62a17ae 3202
3203 /*
3204 * If the nexthop address of the selected Encap route (i.e.,
3205 * the UN address) has changed, then we must update the VPN
3206 * routes that refer to this Encap route and possibly force
3207 * rfapi callbacks.
3208 */
40381db7 3209 if (rfapiAttrNexthopAddrDifferent(&p_firstbpi_old, &p_firstbpi_new)) {
d62a17ae 3210
3211 struct rfapi_monitor_encap *m;
3212 struct rfapi_monitor_encap *mnext;
3213
fe08ba7e 3214 struct agg_node *referenced_vpn_prefix;
d62a17ae 3215
3216 /*
3217 * Optimized approach: build radix tree on the fly to
3218 * hold list of VPN nodes referenced by the ENCAP monitors
3219 *
3220 * The nodes in this table correspond to prefixes of VPN routes.
3221 * The "info" pointer of the node points to a chain of
3222 * struct rfapi_monitor_encap, each of which refers to a
3223 * specific VPN node.
3224 */
fe08ba7e 3225 struct agg_table *referenced_vpn_table;
d62a17ae 3226
fe08ba7e 3227 referenced_vpn_table = agg_table_init();
d62a17ae 3228 assert(referenced_vpn_table);
3229
3230/*
3231 * iterate over the set of monitors at this ENCAP node.
3232 */
1e20238a 3233#ifdef DEBUG_ENCAP_MONITOR
d62a17ae 3234 vnc_zlog_debug_verbose("%s: examining monitors at rn=%p",
3235 __func__, rn);
65efcfce 3236#endif
d62a17ae 3237 for (m = RFAPI_MONITOR_ENCAP(rn); m; m = m->next) {
26a3ffd6 3238 const struct prefix *p;
d62a17ae 3239
3240 /*
40381db7 3241 * For each referenced bpi/route, copy the ENCAP route's
d62a17ae 3242 * nexthop to the VPN route's cached UN address field
3243 * and set
3244 * the address family of the cached UN address field.
3245 */
40381db7
DS
3246 rfapiCopyUnEncap2VPN(info_new, m->bpi);
3247 if (!CHECK_FLAG(m->bpi->flags, BGP_PATH_VALID)) {
3248 SET_FLAG(m->bpi->flags, BGP_PATH_VALID);
3249 if (VALID_INTERIOR_TYPE(m->bpi->type))
d62a17ae 3250 RFAPI_MONITOR_EXTERIOR(m->node)
3251 ->valid_interior_count++;
3252 vnc_import_bgp_exterior_add_route_interior(
40381db7 3253 bgp, import_table, m->node, m->bpi);
d62a17ae 3254 }
3255
3256 /*
3257 * Build a list of unique VPN nodes referenced by these
3258 * monitors
3259 *
3260 * There could be more than one VPN node here with a
3261 * given
3262 * prefix. Those are currently in an unsorted linear
3263 * list
3264 * per prefix.
3265 */
26a3ffd6 3266 p = agg_node_get_prefix(m->node);
fe08ba7e 3267 referenced_vpn_prefix =
26a3ffd6 3268 agg_node_get(referenced_vpn_table, p);
d62a17ae 3269 assert(referenced_vpn_prefix);
3270 for (mnext = referenced_vpn_prefix->info; mnext;
3271 mnext = mnext->next) {
3272
3273 if (mnext->node == m->node)
3274 break;
3275 }
3276
3277 if (mnext) {
3278 /*
3279 * already have an entry for this VPN node
3280 */
fe08ba7e 3281 agg_unlock_node(referenced_vpn_prefix);
d62a17ae 3282 } else {
3283 mnext = XCALLOC(
3284 MTYPE_RFAPI_MONITOR_ENCAP,
3285 sizeof(struct rfapi_monitor_encap));
3286 assert(mnext);
3287 mnext->node = m->node;
3288 mnext->next = referenced_vpn_prefix->info;
3289 referenced_vpn_prefix->info = mnext;
3290 }
3291 }
3292
3293 /*
3294 * for each VPN node referenced in the ENCAP monitors:
3295 */
fe08ba7e
DS
3296 for (referenced_vpn_prefix =
3297 agg_route_top(referenced_vpn_table);
3298 referenced_vpn_prefix;
3299 referenced_vpn_prefix =
3300 agg_route_next(referenced_vpn_prefix)) {
d62a17ae 3301
3302 while ((m = referenced_vpn_prefix->info)) {
3303
fe08ba7e 3304 struct agg_node *n;
d62a17ae 3305
3306 rfapiMonitorMoveLonger(m->node);
fe08ba7e 3307 for (n = m->node; n; n = agg_node_parent(n)) {
d62a17ae 3308 // rfapiDoRouteCallback(import_table, n,
3309 // NULL);
3310 }
3311 rfapiMonitorItNodeChanged(import_table, m->node,
3312 NULL);
3313
3314 referenced_vpn_prefix->info = m->next;
fe08ba7e 3315 agg_unlock_node(referenced_vpn_prefix);
d62a17ae 3316 XFREE(MTYPE_RFAPI_MONITOR_ENCAP, m);
3317 }
3318 }
fe08ba7e 3319 agg_table_finish(referenced_vpn_table);
d62a17ae 3320 }
3321
3322 RFAPI_CHECK_REFCOUNT(rn, SAFI_ENCAP, 0);
65efcfce
LB
3323}
3324
d62a17ae 3325static void rfapiExpireVpnNow(struct rfapi_import_table *it,
40381db7 3326 struct agg_node *rn, struct bgp_path_info *bpi,
d62a17ae 3327 int lockoffset)
65efcfce 3328{
d62a17ae 3329 struct rfapi_withdraw *wcb;
3330 struct thread t;
3331
3332 /*
3333 * pretend we're an expiring timer
3334 */
3335 wcb = XCALLOC(MTYPE_RFAPI_WITHDRAW, sizeof(struct rfapi_withdraw));
40381db7 3336 wcb->info = bpi;
d62a17ae 3337 wcb->node = rn;
3338 wcb->import_table = it;
3339 wcb->lockoffset = lockoffset;
3340 memset(&t, 0, sizeof(t));
3341 t.arg = wcb;
3342 rfapiWithdrawTimerVPN(&t); /* frees wcb */
65efcfce
LB
3343}
3344
3345
d62a17ae 3346/*
9b6d8fcf 3347 * import a bgp_path_info if its route target list intersects with the
65efcfce
LB
3348 * import table's route target list
3349 */
d62a17ae 3350void rfapiBgpInfoFilteredImportVPN(
3351 struct rfapi_import_table *import_table, int action, struct peer *peer,
3352 void *rfd, /* set for looped back routes */
5a1ae2c2
DS
3353 const struct prefix *p,
3354 const struct prefix *aux_prefix, /* AFI_L2VPN: optional IP */
d62a17ae 3355 afi_t afi, struct prefix_rd *prd,
9b6d8fcf 3356 struct attr *attr, /* part of bgp_path_info */
5a1ae2c2 3357 uint8_t type, /* part of bgp_path_info */
9b6d8fcf
DS
3358 uint8_t sub_type, /* part of bgp_path_info */
3359 uint32_t *label) /* part of bgp_path_info */
65efcfce 3360{
fe08ba7e
DS
3361 struct agg_table *rt = NULL;
3362 struct agg_node *rn;
3363 struct agg_node *n;
4b7e6066 3364 struct bgp_path_info *info_new;
40381db7 3365 struct bgp_path_info *bpi;
4b7e6066 3366 struct bgp_path_info *next;
d62a17ae 3367 char buf[BUFSIZ];
3368 struct prefix vn_prefix;
3369 struct prefix un_prefix;
3370 int un_prefix_valid = 0;
fe08ba7e 3371 struct agg_node *ern;
d62a17ae 3372 int replacing = 0;
3373 int original_had_routes = 0;
3374 struct prefix original_nexthop;
3375 const char *action_str = NULL;
3376 int is_it_ce = 0;
3377
3378 struct bgp *bgp;
3379 bgp = bgp_get_default(); /* assume 1 instance for now */
3380
3381 switch (action) {
3382 case FIF_ACTION_UPDATE:
3383 action_str = "update";
3384 break;
3385 case FIF_ACTION_WITHDRAW:
3386 action_str = "withdraw";
3387 break;
3388 case FIF_ACTION_KILL:
3389 action_str = "kill";
3390 break;
3391 default:
3392 assert(0);
3393 break;
3394 }
3395
3396 if (import_table == bgp->rfapi->it_ce)
3397 is_it_ce = 1;
3398
3399 vnc_zlog_debug_verbose("%s: entry: %s%s: prefix %s/%d: it %p, afi %s",
3400 __func__, (is_it_ce ? "CE-IT " : ""), action_str,
3401 rfapi_ntop(p->family, &p->u.prefix, buf, BUFSIZ),
3402 p->prefixlen, import_table, afi2str(afi));
3403
3404 VNC_ITRCCK;
3405
3406 /*
3407 * Compare rt lists. If no intersection, don't import this route
3408 * On a withdraw, peer and RD are sufficient to determine if
3409 * we should act.
3410 */
3411 if (action == FIF_ACTION_UPDATE) {
3412 if (!attr || !attr->ecommunity) {
3413
3414 vnc_zlog_debug_verbose(
3415 "%s: attr, extra, or ecommunity missing, not importing",
3416 __func__);
3417 return;
3418 }
3419 if ((import_table != bgp->rfapi->it_ce)
3420 && !rfapiEcommunitiesIntersect(import_table->rt_import_list,
3421 attr->ecommunity)) {
3422
3423 vnc_zlog_debug_verbose(
3424 "%s: it=%p: no ecommunity intersection",
3425 __func__, import_table);
3426 return;
3427 }
3428
3429 memset(&vn_prefix, 0,
3430 sizeof(vn_prefix)); /* keep valgrind happy */
3431 if (rfapiGetNexthop(attr, &vn_prefix)) {
3432 /* missing nexthop address would be a bad, bad thing */
3433 vnc_zlog_debug_verbose("%s: missing nexthop", __func__);
3434 return;
3435 }
3436 }
3437
3438 /*
3439 * Figure out which radix tree the route would go into
3440 */
3441 switch (afi) {
3442 case AFI_IP:
3443 case AFI_IP6:
3444 case AFI_L2VPN:
3445 rt = import_table->imported_vpn[afi];
3446 break;
3447
3448 default:
450971aa 3449 flog_err(EC_LIB_DEVELOPMENT, "%s: bad afi %d", __func__, afi);
d62a17ae 3450 return;
3451 }
3452
3453 /* clear it */
3454 memset(&original_nexthop, 0, sizeof(original_nexthop));
3455
3456 /*
fe08ba7e 3457 * agg_node_lookup returns a node only if there is at least
d62a17ae 3458 * one route attached.
3459 */
fe08ba7e 3460 rn = agg_node_lookup(rt, p);
d62a17ae 3461
3462 vnc_zlog_debug_verbose("%s: rn=%p", __func__, rn);
3463
3464 if (rn) {
3465
3466 RFAPI_CHECK_REFCOUNT(rn, SAFI_MPLS_VPN, 1);
fe08ba7e 3467 agg_unlock_node(rn); /* undo lock in agg_node_lookup */
d62a17ae 3468
3469 if (rn->info)
3470 original_had_routes = 1;
3471
3472 if (VNC_DEBUG(VERBOSE)) {
3473 vnc_zlog_debug_verbose("%s: showing IT node on entry",
3474 __func__);
3475 rfapiShowItNode(NULL, rn); /* debug */
3476 }
3477
3478 /*
3479 * Look for same route (will have same RD and peer)
3480 */
40381db7 3481 bpi = rfapiItBiIndexSearch(rn, prd, peer, aux_prefix);
d62a17ae 3482
40381db7 3483 if (bpi) {
d62a17ae 3484
3485 /*
3486 * This was an old test when we iterated over the
40381db7 3487 * BPIs linearly. Since we're now looking up with
d62a17ae 3488 * RD and peer, comparing types should not be
3489 * needed. Changed to assertion.
3490 *
3491 * Compare types. Doing so prevents a RFP-originated
3492 * route from matching an imported route, for example.
3493 */
40381db7 3494 if (VNC_DEBUG(VERBOSE) && bpi->type != type)
3af8cb57 3495 /* should be handled by RDs, but warn for now */
40381db7
DS
3496 zlog_warn("%s: type mismatch! (bpi=%d, arg=%d)",
3497 __func__, bpi->type, type);
d62a17ae 3498
40381db7 3499 vnc_zlog_debug_verbose("%s: found matching bpi",
d62a17ae 3500 __func__);
3501
3502 /*
3503 * In the special CE table, withdrawals occur without
3504 * holddown
3505 */
3506 if (import_table == bgp->rfapi->it_ce) {
40381db7 3507 vnc_direct_bgp_del_route_ce(bgp, rn, bpi);
d62a17ae 3508 if (action == FIF_ACTION_WITHDRAW)
3509 action = FIF_ACTION_KILL;
3510 }
3511
3512 if (action == FIF_ACTION_WITHDRAW) {
3513
40381db7
DS
3514 int washolddown = CHECK_FLAG(bpi->flags,
3515 BGP_PATH_REMOVED);
d62a17ae 3516
3517 vnc_zlog_debug_verbose(
26a3ffd6
DS
3518 "%s: withdrawing at prefix %pRN%s",
3519 __func__, rn,
d62a17ae 3520 (washolddown
3521 ? " (already being withdrawn)"
3522 : ""));
3523
3524 VNC_ITRCCK;
3525 if (!washolddown) {
3526 rfapiBiStartWithdrawTimer(
40381db7 3527 import_table, rn, bpi, afi,
d62a17ae 3528 SAFI_MPLS_VPN,
3529 rfapiWithdrawTimerVPN);
3530
3531 RFAPI_UPDATE_ITABLE_COUNT(
40381db7 3532 bpi, import_table, afi, -1);
d62a17ae 3533 import_table->holddown_count[afi] += 1;
3534 }
3535 VNC_ITRCCK;
3536 } else {
3537 vnc_zlog_debug_verbose(
26a3ffd6 3538 "%s: %s at prefix %pRN", __func__,
d62a17ae 3539 ((action == FIF_ACTION_KILL)
3540 ? "killing"
3541 : "replacing"),
26a3ffd6 3542 rn);
d62a17ae 3543
3544 /*
3545 * If this route is waiting to be deleted
3546 * because of
3547 * a previous withdraw, we must cancel its
3548 * timer.
3549 */
40381db7
DS
3550 if (CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED)
3551 && bpi->extra->vnc.import.timer) {
d62a17ae 3552
b3d6bc6e
MS
3553 struct thread **t =
3554 &(bpi->extra->vnc.import.timer);
3555 struct rfapi_withdraw *wcb = (*t)->arg;
d62a17ae 3556
3557 XFREE(MTYPE_RFAPI_WITHDRAW, wcb);
3558 thread_cancel(t);
3559
3560 import_table->holddown_count[afi] -= 1;
3561 RFAPI_UPDATE_ITABLE_COUNT(
40381db7 3562 bpi, import_table, afi, 1);
d62a17ae 3563 }
3564 /*
3565 * decrement remote count (if route is remote)
3566 * because
3567 * we are going to remove it below
3568 */
40381db7
DS
3569 RFAPI_UPDATE_ITABLE_COUNT(bpi, import_table,
3570 afi, -1);
d62a17ae 3571 if (action == FIF_ACTION_UPDATE) {
3572 replacing = 1;
3573
3574 /*
3575 * make copy of original nexthop so we
3576 * can see if it changed
3577 */
40381db7 3578 rfapiGetNexthop(bpi->attr,
d62a17ae 3579 &original_nexthop);
3580
3581 /*
40381db7 3582 * remove bpi without doing any export
d62a17ae 3583 * processing
3584 */
40381db7 3585 if (CHECK_FLAG(bpi->flags,
1defdda8 3586 BGP_PATH_VALID)
40381db7 3587 && VALID_INTERIOR_TYPE(bpi->type))
d62a17ae 3588 RFAPI_MONITOR_EXTERIOR(rn)
3589 ->valid_interior_count--;
40381db7
DS
3590 rfapiItBiIndexDel(rn, bpi);
3591 rfapiBgpInfoDetach(rn, bpi);
3592 rfapiMonitorEncapDelete(bpi);
d62a17ae 3593 vnc_import_bgp_exterior_del_route_interior(
40381db7
DS
3594 bgp, import_table, rn, bpi);
3595 rfapiBgpInfoFree(bpi);
d62a17ae 3596 } else {
3597 /* Kill */
3598 /*
40381db7 3599 * remove bpi and do export processing
d62a17ae 3600 */
3601 import_table->holddown_count[afi] += 1;
40381db7 3602 rfapiExpireVpnNow(import_table, rn, bpi,
d62a17ae 3603 0);
3604 }
3605 }
3606 }
3607 }
3608
3609 if (rn)
3610 RFAPI_CHECK_REFCOUNT(rn, SAFI_MPLS_VPN, replacing ? 1 : 0);
3611
3612 if (action == FIF_ACTION_WITHDRAW || action == FIF_ACTION_KILL) {
3613 VNC_ITRCCK;
3614 return;
3615 }
3616
3617 info_new =
3618 rfapiBgpInfoCreate(attr, peer, rfd, prd, type, sub_type, label);
3619
3620 /*
3621 * lookup un address in encap table
3622 */
fe08ba7e 3623 ern = agg_node_match(import_table->imported_encap[afi], &vn_prefix);
d62a17ae 3624 if (ern) {
3625 rfapiCopyUnEncap2VPN(ern->info, info_new);
fe08ba7e 3626 agg_unlock_node(ern); /* undo lock in route_note_match */
d62a17ae 3627 } else {
d62a17ae 3628 /* Not a big deal, just means VPN route got here first */
2dbe669b
DA
3629 vnc_zlog_debug_verbose("%s: no encap route for vn addr %pFX",
3630 __func__, &vn_prefix);
d62a17ae 3631 info_new->extra->vnc.import.un_family = 0;
3632 }
3633
3634 if (rn) {
3635 if (!replacing)
fe08ba7e 3636 agg_lock_node(rn);
d62a17ae 3637 } else {
3638 /*
3639 * No need to increment reference count, so only "get"
3640 * if the node is not there already
3641 */
fe08ba7e 3642 rn = agg_node_get(rt, p);
d62a17ae 3643 }
3644
3645 /*
3646 * For ethernet routes, if there is an accompanying IP address,
40381db7 3647 * save it in the bpi
d62a17ae 3648 */
3649 if ((AFI_L2VPN == afi) && aux_prefix) {
3650
40381db7
DS
3651 vnc_zlog_debug_verbose("%s: setting BPI's aux_prefix",
3652 __func__);
d62a17ae 3653 info_new->extra->vnc.import.aux_prefix = *aux_prefix;
3654 }
3655
26a3ffd6 3656 vnc_zlog_debug_verbose("%s: inserting bpi %p at prefix %pRN #%d",
c10e14e9
DS
3657 __func__, info_new, rn,
3658 agg_node_get_lock_count(rn));
d62a17ae 3659
3660 rfapiBgpInfoAttachSorted(rn, info_new, afi, SAFI_MPLS_VPN);
3661 rfapiItBiIndexAdd(rn, info_new);
3662 if (!rfapiGetUnAddrOfVpnBi(info_new, NULL)) {
3663 if (VALID_INTERIOR_TYPE(info_new->type))
3664 RFAPI_MONITOR_EXTERIOR(rn)->valid_interior_count++;
1defdda8 3665 SET_FLAG(info_new->flags, BGP_PATH_VALID);
d62a17ae 3666 }
3667 RFAPI_UPDATE_ITABLE_COUNT(info_new, import_table, afi, 1);
3668 vnc_import_bgp_exterior_add_route_interior(bgp, import_table, rn,
3669 info_new);
3670
3671 if (import_table == bgp->rfapi->it_ce)
3672 vnc_direct_bgp_add_route_ce(bgp, rn, info_new);
3673
3674 if (VNC_DEBUG(VERBOSE)) {
3675 vnc_zlog_debug_verbose("%s: showing IT node", __func__);
3676 rfapiShowItNode(NULL, rn); /* debug */
3677 }
3678
3679 rfapiMonitorEncapAdd(import_table, &vn_prefix, rn, info_new);
3680
3681 if (!rfapiGetUnAddrOfVpnBi(info_new, &un_prefix)) {
3682
3683 /*
3684 * if we have a valid UN address (either via Encap route
3685 * or via tunnel attribute), then we should attempt
3686 * to move any monitors at less-specific nodes to this node
3687 */
3688 rfapiMonitorMoveLonger(rn);
3689
3690 un_prefix_valid = 1;
3691 }
3692
3693 /*
3694 * 101129 Enhancement: if we add a route (implication: it is not
3695 * in holddown), delete all other routes from this nve at this
3696 * node that are in holddown, regardless of peer.
3697 *
3698 * Reasons it's OK to do that:
3699 *
3700 * - if the holddown route being deleted originally came from BGP VPN,
3701 * it is already gone from BGP (implication of holddown), so there
3702 * won't be any added inconsistency with the BGP RIB.
3703 *
3704 * - once a fresh route is added at a prefix, any routes in holddown
3705 * at that prefix will not show up in RFP responses, so deleting
3706 * the holddown routes won't affect the contents of responses.
3707 *
3708 * - lifetimes are supposed to be consistent, so there should not
3709 * be a case where the fresh route has a shorter lifetime than
3710 * the holddown route, so we don't expect the fresh route to
3711 * disappear and complete its holddown time before the existing
3712 * holddown routes time out. Therefore, we won't have a situation
3713 * where we expect the existing holddown routes to be hidden and
3714 * then to reappear sometime later (as holddown routes) in a
3715 * RFP response.
3716 *
3717 * Among other things, this would enable us to skirt the problem
3718 * of local holddown routes that refer to NVE descriptors that
3719 * have already been closed (if the same NVE triggers a subsequent
3720 * rfapi_open(), the new peer is different and doesn't match the
3721 * peer of the holddown route, so the stale holddown route still
3722 * hangs around until it times out instead of just being replaced
3723 * by the fresh route).
3724 */
3725 /*
40381db7 3726 * We know that the new bpi will have been inserted before any routes
d62a17ae 3727 * in holddown, so we can skip any that came before it
3728 */
40381db7 3729 for (bpi = info_new->next; bpi; bpi = next) {
d62a17ae 3730
3731 struct prefix pfx_vn;
3732 struct prefix pfx_un;
3733 int un_match = 0;
3734 int remote_peer_match = 0;
3735
40381db7 3736 next = bpi->next;
d62a17ae 3737
3738 /*
3739 * Must be holddown
3740 */
40381db7 3741 if (!CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED))
d62a17ae 3742 continue;
3743
3744 /*
3745 * Must match VN address (nexthop of VPN route)
3746 */
40381db7 3747 if (rfapiGetNexthop(bpi->attr, &pfx_vn))
d62a17ae 3748 continue;
3749 if (!prefix_same(&pfx_vn, &vn_prefix))
3750 continue;
3751
3752 if (un_prefix_valid && /* new route UN addr */
40381db7 3753 !rfapiGetUnAddrOfVpnBi(bpi, &pfx_un)
d62a17ae 3754 && /* old route UN addr */
3755 prefix_same(&pfx_un, &un_prefix)) { /* compare */
3756 un_match = 1;
3757 }
40381db7
DS
3758 if (!RFAPI_LOCAL_BI(bpi) && !RFAPI_LOCAL_BI(info_new)
3759 && sockunion_same(&bpi->peer->su, &info_new->peer->su)) {
d62a17ae 3760 /* old & new are both remote, same peer */
3761 remote_peer_match = 1;
3762 }
3763
3764 if (!un_match & !remote_peer_match)
3765 continue;
3766
3767 vnc_zlog_debug_verbose(
40381db7 3768 "%s: removing holddown bpi matching NVE of new route",
d62a17ae 3769 __func__);
40381db7 3770 if (bpi->extra->vnc.import.timer) {
b3d6bc6e
MS
3771 struct thread **t =
3772 &(bpi->extra->vnc.import.timer);
3773 struct rfapi_withdraw *wcb = (*t)->arg;
d62a17ae 3774
3775 XFREE(MTYPE_RFAPI_WITHDRAW, wcb);
3776 thread_cancel(t);
3777 }
40381db7 3778 rfapiExpireVpnNow(import_table, rn, bpi, 0);
d62a17ae 3779 }
3780
3781 if (!original_had_routes) {
3782 /*
3783 * We went from 0 usable routes to 1 usable route. Perform the
3784 * "Adding a Route" export process.
3785 */
3786 vnc_direct_bgp_add_prefix(bgp, import_table, rn);
3787 vnc_zebra_add_prefix(bgp, import_table, rn);
3788 } else {
3789 /*
3790 * Check for nexthop change event
3791 * Note: the prefix_same() test below detects two situations:
3792 * 1. route is replaced, new route has different nexthop
3793 * 2. new route is added (original_nexthop is 0)
3794 */
3795 struct prefix new_nexthop;
3796
3797 rfapiGetNexthop(attr, &new_nexthop);
3798 if (!prefix_same(&original_nexthop, &new_nexthop)) {
3799 /*
3800 * nexthop change event
3801 * vnc_direct_bgp_add_prefix() will recompute VN addr
3802 * ecommunity
3803 */
3804 vnc_direct_bgp_add_prefix(bgp, import_table, rn);
3805 }
3806 }
3807
3808 if (!(bgp->rfapi_cfg->flags & BGP_VNC_CONFIG_CALLBACK_DISABLE)) {
fe08ba7e 3809 for (n = rn; n; n = agg_node_parent(n)) {
d62a17ae 3810 // rfapiDoRouteCallback(import_table, n, NULL);
3811 }
3812 rfapiMonitorItNodeChanged(import_table, rn, NULL);
3813 }
3814 RFAPI_CHECK_REFCOUNT(rn, SAFI_MPLS_VPN, 0);
3815 VNC_ITRCCK;
65efcfce
LB
3816}
3817
21cf6b21
VJ
3818static void rfapiBgpInfoFilteredImportBadSafi(
3819 struct rfapi_import_table *import_table, int action, struct peer *peer,
3820 void *rfd, /* set for looped back routes */
5a1ae2c2
DS
3821 const struct prefix *p,
3822 const struct prefix *aux_prefix, /* AFI_L2VPN: optional IP */
21cf6b21 3823 afi_t afi, struct prefix_rd *prd,
9b6d8fcf 3824 struct attr *attr, /* part of bgp_path_info */
5a1ae2c2 3825 uint8_t type, /* part of bgp_path_info */
9b6d8fcf
DS
3826 uint8_t sub_type, /* part of bgp_path_info */
3827 uint32_t *label) /* part of bgp_path_info */
21cf6b21
VJ
3828{
3829 vnc_zlog_debug_verbose("%s: Error, bad safi", __func__);
3830}
3831
65efcfce 3832static rfapi_bi_filtered_import_f *
d62a17ae 3833rfapiBgpInfoFilteredImportFunction(safi_t safi)
65efcfce 3834{
d62a17ae 3835 switch (safi) {
3836 case SAFI_MPLS_VPN:
3837 return rfapiBgpInfoFilteredImportVPN;
3838
3839 case SAFI_ENCAP:
3840 return rfapiBgpInfoFilteredImportEncap;
5c525538
RW
3841
3842 default:
3843 /* not expected */
1c50c1c0 3844 flog_err(EC_LIB_DEVELOPMENT, "%s: bad safi %d", __func__, safi);
21cf6b21 3845 return rfapiBgpInfoFilteredImportBadSafi;
d62a17ae 3846 }
65efcfce
LB
3847}
3848
d62a17ae 3849void rfapiProcessUpdate(struct peer *peer,
3850 void *rfd, /* set when looped from RFP/RFAPI */
5a1ae2c2 3851 const struct prefix *p, struct prefix_rd *prd,
d7c0a89a
QY
3852 struct attr *attr, afi_t afi, safi_t safi, uint8_t type,
3853 uint8_t sub_type, uint32_t *label)
65efcfce 3854{
d62a17ae 3855 struct bgp *bgp;
3856 struct rfapi *h;
3857 struct rfapi_import_table *it;
3858 int has_ip_route = 1;
3859 uint32_t lni = 0;
3860
3861 bgp = bgp_get_default(); /* assume 1 instance for now */
3862 assert(bgp);
3863
3864 h = bgp->rfapi;
3865 assert(h);
3866
3867 /*
3868 * look at high-order byte of RD. FF means MAC
3869 * address is present (VNC L2VPN)
3870 */
3871 if ((safi == SAFI_MPLS_VPN)
3872 && (decode_rd_type(prd->val) == RD_TYPE_VNC_ETH)) {
3873 struct prefix pfx_mac_buf;
3874 struct prefix pfx_nexthop_buf;
3875 int rc;
3876
3877 /*
3878 * Set flag if prefix and nexthop are the same - don't
3879 * add the route to normal IP-based import tables
3880 */
3881 if (!rfapiGetNexthop(attr, &pfx_nexthop_buf)) {
3882 if (!prefix_cmp(&pfx_nexthop_buf, p)) {
3883 has_ip_route = 0;
3884 }
3885 }
3886
3887 memset(&pfx_mac_buf, 0, sizeof(pfx_mac_buf));
3888 pfx_mac_buf.family = AF_ETHERNET;
3889 pfx_mac_buf.prefixlen = 48;
3890 memcpy(&pfx_mac_buf.u.prefix_eth.octet, prd->val + 2, 6);
3891
3892 /*
3893 * Find rt containing LNI (Logical Network ID), which
3894 * _should_ always be present when mac address is present
3895 */
3896 rc = rfapiEcommunityGetLNI(attr->ecommunity, &lni);
3897
3898 vnc_zlog_debug_verbose(
3899 "%s: rfapiEcommunityGetLNI returned %d, lni=%d, attr=%p",
3900 __func__, rc, lni, attr);
8cea9547 3901 if (!rc) {
d62a17ae 3902 it = rfapiMacImportTableGet(bgp, lni);
3903
3904 rfapiBgpInfoFilteredImportVPN(
3905 it, FIF_ACTION_UPDATE, peer, rfd,
3906 &pfx_mac_buf, /* prefix */
3907 p, /* aux prefix: IP addr */
3908 AFI_L2VPN, prd, attr, type, sub_type, label);
3909 }
3910 }
3911
3912 if (!has_ip_route)
3913 return;
3914
3915 /*
3916 * Iterate over all import tables; do a filtered import
3917 * for the afi/safi combination
3918 */
3919 for (it = h->imports; it; it = it->next) {
3920 (*rfapiBgpInfoFilteredImportFunction(safi))(
3921 it, FIF_ACTION_UPDATE, peer, rfd, p, /* prefix */
3922 NULL, afi, prd, attr, type, sub_type, label);
3923 }
3924
3925 if (safi == SAFI_MPLS_VPN) {
3926 vnc_direct_bgp_rh_add_route(bgp, afi, p, peer, attr);
d62a17ae 3927 rfapiBgpInfoFilteredImportVPN(
3928 bgp->rfapi->it_ce, FIF_ACTION_UPDATE, peer, rfd,
3929 p, /* prefix */
3930 NULL, afi, prd, attr, type, sub_type, label);
3931 }
65efcfce
LB
3932}
3933
3934
5a1ae2c2 3935void rfapiProcessWithdraw(struct peer *peer, void *rfd, const struct prefix *p,
d62a17ae 3936 struct prefix_rd *prd, struct attr *attr, afi_t afi,
d7c0a89a 3937 safi_t safi, uint8_t type, int kill)
65efcfce 3938{
d62a17ae 3939 struct bgp *bgp;
3940 struct rfapi *h;
3941 struct rfapi_import_table *it;
3942
3943 bgp = bgp_get_default(); /* assume 1 instance for now */
3944 assert(bgp);
3945
3946 h = bgp->rfapi;
3947 assert(h);
3948
3949 /*
3950 * look at high-order byte of RD. FF means MAC
3951 * address is present (VNC L2VPN)
3952 */
3953 if (h->import_mac != NULL && safi == SAFI_MPLS_VPN
3954 && decode_rd_type(prd->val) == RD_TYPE_VNC_ETH) {
3955 struct prefix pfx_mac_buf;
3956 void *cursor = NULL;
3957 int rc;
3958
3959 memset(&pfx_mac_buf, 0, sizeof(pfx_mac_buf));
3960 pfx_mac_buf.family = AF_ETHERNET;
3961 pfx_mac_buf.prefixlen = 48;
3962 memcpy(&pfx_mac_buf.u.prefix_eth, prd->val + 2, 6);
3963
3964 /*
3965 * withdraw does not contain attrs, so we don't have
3966 * access to the route's LNI, which would ordinarily
3967 * select the specific mac-based import table. Instead,
3968 * we must iterate over all mac-based tables and rely
3969 * on the RD to match.
3970 *
3971 * If this approach is too slow, add an index where
3972 * key is {RD, peer} and value is the import table
3973 */
3974 for (rc = skiplist_next(h->import_mac, NULL, (void **)&it,
3975 &cursor);
3976 rc == 0; rc = skiplist_next(h->import_mac, NULL,
3977 (void **)&it, &cursor)) {
65efcfce 3978
1e20238a 3979#ifdef DEBUG_L2_EXTRA
d62a17ae 3980 vnc_zlog_debug_verbose(
3981 "%s: calling rfapiBgpInfoFilteredImportVPN(it=%p, afi=AFI_L2VPN)",
3982 __func__, it);
65efcfce
LB
3983#endif
3984
d62a17ae 3985 rfapiBgpInfoFilteredImportVPN(
3986 it,
3987 (kill ? FIF_ACTION_KILL : FIF_ACTION_WITHDRAW),
3988 peer, rfd, &pfx_mac_buf, /* prefix */
3989 p, /* aux_prefix: IP */
3990 AFI_L2VPN, prd, attr, type, 0,
3991 NULL); /* sub_type & label unused for withdraw
9d303b37 3992 */
d62a17ae 3993 }
3994 }
3995
3996 /*
3997 * XXX For the case where the withdraw involves an L2
3998 * route with no IP information, we rely on the lack
3999 * of RT-list intersection to filter out the withdraw
4000 * from the IP-based import tables below
4001 */
4002
4003 /*
4004 * Iterate over all import tables; do a filtered import
4005 * for the afi/safi combination
4006 */
4007
4008 for (it = h->imports; it; it = it->next) {
4009 (*rfapiBgpInfoFilteredImportFunction(safi))(
4010 it, (kill ? FIF_ACTION_KILL : FIF_ACTION_WITHDRAW),
4011 peer, rfd, p, /* prefix */
4012 NULL, afi, prd, attr, type, 0,
4013 NULL); /* sub_type & label unused for withdraw */
4014 }
4015
4016 /* TBD the deletion should happen after the lifetime expires */
4017 if (safi == SAFI_MPLS_VPN)
4018 vnc_direct_bgp_rh_del_route(bgp, afi, p, peer);
4019
4020 if (safi == SAFI_MPLS_VPN) {
4021 rfapiBgpInfoFilteredImportVPN(
4022 bgp->rfapi->it_ce,
4023 (kill ? FIF_ACTION_KILL : FIF_ACTION_WITHDRAW), peer,
4024 rfd, p, /* prefix */
4025 NULL, afi, prd, attr, type, 0,
4026 NULL); /* sub_type & label unused for withdraw */
4027 }
65efcfce
LB
4028}
4029
4030/*
4031 * TBD optimized withdraw timer algorithm for case of many
4032 * routes expiring at the same time due to peer drop.
4033 */
4034/*
40381db7 4035 * 1. Visit all BPIs in all ENCAP import tables.
65efcfce 4036 *
40381db7
DS
4037 * a. If a bpi's peer is the failed peer, remove the bpi.
4038 * b. If the removed ENCAP bpi was first in the list of
4039 * BPIs at this ENCAP node, loop over all monitors
65efcfce
LB
4040 * at this node:
4041 *
4042 * (1) for each ENCAP monitor, loop over all its
4043 * VPN node monitors and set their RFAPI_MON_FLAG_NEEDCALLBACK
4044 * flags.
4045 *
40381db7
DS
4046 * 2. Visit all BPIs in all VPN import tables.
4047 * a. If a bpi's peer is the failed peer, remove the bpi.
65efcfce
LB
4048 * b. loop over all the VPN node monitors and set their
4049 * RFAPI_MON_FLAG_NEEDCALLBACK flags
40381db7 4050 * c. If there are no BPIs left at this VPN node,
65efcfce
LB
4051 *
4052 */
4053
4054
4055/* surprise, this gets called from peer_delete(), from rfapi_close() */
d62a17ae 4056static void rfapiProcessPeerDownRt(struct peer *peer,
4057 struct rfapi_import_table *import_table,
4058 afi_t afi, safi_t safi)
65efcfce 4059{
fe08ba7e 4060 struct agg_node *rn;
40381db7 4061 struct bgp_path_info *bpi;
fe08ba7e 4062 struct agg_table *rt;
d62a17ae 4063 int (*timer_service_func)(struct thread *);
4064
4065 assert(afi == AFI_IP || afi == AFI_IP6);
4066
4067 VNC_ITRCCK;
4068
4069 switch (safi) {
4070 case SAFI_MPLS_VPN:
4071 rt = import_table->imported_vpn[afi];
4072 timer_service_func = rfapiWithdrawTimerVPN;
4073 break;
4074 case SAFI_ENCAP:
4075 rt = import_table->imported_encap[afi];
4076 timer_service_func = rfapiWithdrawTimerEncap;
4077 break;
4078 default:
21fe4510
QY
4079 /* Suppress uninitialized variable warning */
4080 rt = NULL;
4081 timer_service_func = NULL;
d62a17ae 4082 assert(0);
4083 }
4084
4085
fe08ba7e 4086 for (rn = agg_route_top(rt); rn; rn = agg_route_next(rn)) {
40381db7
DS
4087 for (bpi = rn->info; bpi; bpi = bpi->next) {
4088 if (bpi->peer == peer) {
d62a17ae 4089
40381db7 4090 if (CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED)) {
d62a17ae 4091 /* already in holddown, skip */
4092 continue;
4093 }
4094
4095 if (safi == SAFI_MPLS_VPN) {
4096 RFAPI_UPDATE_ITABLE_COUNT(
40381db7 4097 bpi, import_table, afi, -1);
d62a17ae 4098 import_table->holddown_count[afi] += 1;
4099 }
40381db7 4100 rfapiBiStartWithdrawTimer(import_table, rn, bpi,
d62a17ae 4101 afi, safi,
4102 timer_service_func);
4103 }
4104 }
4105 }
4106 VNC_ITRCCK;
65efcfce
LB
4107}
4108
d62a17ae 4109/*
65efcfce
LB
4110 * This gets called when a peer connection drops. We have to remove
4111 * all the routes from this peer.
4112 *
4113 * Current approach is crude. TBD Optimize by setting fewer timers and
4114 * grouping withdrawn routes so we can generate callbacks more
4115 * efficiently.
4116 */
d62a17ae 4117void rfapiProcessPeerDown(struct peer *peer)
65efcfce 4118{
d62a17ae 4119 struct bgp *bgp;
4120 struct rfapi *h;
4121 struct rfapi_import_table *it;
4122
4123 /*
4124 * If this peer is a "dummy" peer structure atached to a RFAPI
4125 * nve_descriptor, we don't need to walk the import tables
4126 * because the routes are already withdrawn by rfapi_close()
4127 */
4128 if (CHECK_FLAG(peer->flags, PEER_FLAG_IS_RFAPI_HD))
4129 return;
4130
4131 /*
40381db7
DS
4132 * 1. Visit all BPIs in all ENCAP import tables.
4133 * Start withdraw timer on the BPIs that match peer.
d62a17ae 4134 *
40381db7
DS
4135 * 2. Visit All BPIs in all VPN import tables.
4136 * Start withdraw timer on the BPIs that match peer.
d62a17ae 4137 */
4138
4139 bgp = bgp_get_default(); /* assume 1 instance for now */
4140 if (!bgp)
4141 return;
4142
4143 h = bgp->rfapi;
4144 assert(h);
4145
4146 for (it = h->imports; it; it = it->next) {
4147 rfapiProcessPeerDownRt(peer, it, AFI_IP, SAFI_ENCAP);
4148 rfapiProcessPeerDownRt(peer, it, AFI_IP6, SAFI_ENCAP);
4149 rfapiProcessPeerDownRt(peer, it, AFI_IP, SAFI_MPLS_VPN);
4150 rfapiProcessPeerDownRt(peer, it, AFI_IP6, SAFI_MPLS_VPN);
4151 }
4152
4153 if (h->it_ce) {
4154 rfapiProcessPeerDownRt(peer, h->it_ce, AFI_IP, SAFI_MPLS_VPN);
4155 rfapiProcessPeerDownRt(peer, h->it_ce, AFI_IP6, SAFI_MPLS_VPN);
4156 }
65efcfce
LB
4157}
4158
4159/*
4160 * Import an entire RIB (for an afi/safi) to an import table RIB,
4161 * filtered according to the import table's RT list
4162 *
4163 * TBD: does this function need additions to match rfapiProcessUpdate()
d62a17ae 4164 * for, e.g., L2 handling?
65efcfce 4165 */
d62a17ae 4166static void rfapiBgpTableFilteredImport(struct bgp *bgp,
4167 struct rfapi_import_table *it,
4168 afi_t afi, safi_t safi)
65efcfce 4169{
9bcb3eef
DS
4170 struct bgp_dest *dest1;
4171 struct bgp_dest *dest2;
d62a17ae 4172
4173 /* Only these SAFIs have 2-level RIBS */
4174 assert(safi == SAFI_MPLS_VPN || safi == SAFI_ENCAP);
4175
4176 /*
4177 * Now visit all the rd nodes and the nodes of all the
4178 * route tables attached to them, and import the routes
4179 * if they have matching route targets
4180 */
9bcb3eef
DS
4181 for (dest1 = bgp_table_top(bgp->rib[afi][safi]); dest1;
4182 dest1 = bgp_route_next(dest1)) {
d62a17ae 4183
9bcb3eef 4184 if (bgp_dest_has_bgp_path_info_data(dest1)) {
6f94b685 4185
9bcb3eef
DS
4186 for (dest2 = bgp_table_top(
4187 bgp_dest_get_bgp_table_info(dest1));
4188 dest2; dest2 = bgp_route_next(dest2)) {
d62a17ae 4189
40381db7 4190 struct bgp_path_info *bpi;
d62a17ae 4191
9bcb3eef 4192 for (bpi = bgp_dest_get_bgp_path_info(dest2);
6f94b685 4193 bpi; bpi = bpi->next) {
d7c0a89a 4194 uint32_t label = 0;
d62a17ae 4195
40381db7 4196 if (CHECK_FLAG(bpi->flags,
1defdda8 4197 BGP_PATH_REMOVED))
d62a17ae 4198 continue;
4199
40381db7 4200 if (bpi->extra)
d62a17ae 4201 label = decode_label(
40381db7 4202 &bpi->extra->label[0]);
d62a17ae 4203 (*rfapiBgpInfoFilteredImportFunction(
4204 safi))(
4205 it, /* which import table */
40381db7 4206 FIF_ACTION_UPDATE, bpi->peer,
9bcb3eef
DS
4207 NULL,
4208 bgp_dest_get_prefix(dest2),
d62a17ae 4209 NULL, afi,
b54892e0 4210 (struct prefix_rd *)
9bcb3eef
DS
4211 bgp_dest_get_prefix(
4212 dest1),
40381db7
DS
4213 bpi->attr, bpi->type,
4214 bpi->sub_type, &label);
d62a17ae 4215 }
4216 }
4217 }
4218 }
65efcfce
LB
4219}
4220
4221
4222/* per-bgp-instance rfapi data */
d62a17ae 4223struct rfapi *bgp_rfapi_new(struct bgp *bgp)
65efcfce 4224{
d62a17ae 4225 struct rfapi *h;
e7038dde 4226 afi_t afi;
d62a17ae 4227 struct rfapi_rfp_cfg *cfg = NULL;
4228 struct rfapi_rfp_cb_methods *cbm = NULL;
4229
4230 assert(bgp->rfapi_cfg == NULL);
4231
9f5dc319 4232 h = XCALLOC(MTYPE_RFAPI, sizeof(struct rfapi));
d62a17ae 4233
4234 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
fe08ba7e 4235 h->un[afi] = agg_table_init();
d62a17ae 4236 }
4237
4238 /*
4239 * initialize the ce import table
4240 */
4241 h->it_ce = XCALLOC(MTYPE_RFAPI_IMPORTTABLE,
4242 sizeof(struct rfapi_import_table));
fe08ba7e
DS
4243 h->it_ce->imported_vpn[AFI_IP] = agg_table_init();
4244 h->it_ce->imported_vpn[AFI_IP6] = agg_table_init();
4245 h->it_ce->imported_encap[AFI_IP] = agg_table_init();
4246 h->it_ce->imported_encap[AFI_IP6] = agg_table_init();
d62a17ae 4247 rfapiBgpTableFilteredImport(bgp, h->it_ce, AFI_IP, SAFI_MPLS_VPN);
4248 rfapiBgpTableFilteredImport(bgp, h->it_ce, AFI_IP6, SAFI_MPLS_VPN);
4249
4250 /*
4251 * Set up work queue for deferred rfapi_close operations
4252 */
4253 h->deferred_close_q =
4254 work_queue_new(bm->master, "rfapi deferred close");
4255 h->deferred_close_q->spec.workfunc = rfapi_deferred_close_workfunc;
4256 h->deferred_close_q->spec.data = h;
4257
4258 h->rfp = rfp_start(bm->master, &cfg, &cbm);
4259 bgp->rfapi_cfg = bgp_rfapi_cfg_new(cfg);
4260 if (cbm != NULL) {
4261 h->rfp_methods = *cbm;
4262 }
4263 return h;
65efcfce
LB
4264}
4265
d62a17ae 4266void bgp_rfapi_destroy(struct bgp *bgp, struct rfapi *h)
65efcfce 4267{
e7038dde 4268 afi_t afi;
0ae6124f 4269
d62a17ae 4270 if (bgp == NULL || h == NULL)
4271 return;
4272
4273 if (h->resolve_nve_nexthop) {
4274 skiplist_free(h->resolve_nve_nexthop);
4275 h->resolve_nve_nexthop = NULL;
4276 }
4277
fe08ba7e
DS
4278 agg_table_finish(h->it_ce->imported_vpn[AFI_IP]);
4279 agg_table_finish(h->it_ce->imported_vpn[AFI_IP6]);
4280 agg_table_finish(h->it_ce->imported_encap[AFI_IP]);
4281 agg_table_finish(h->it_ce->imported_encap[AFI_IP6]);
d62a17ae 4282
4283 if (h->import_mac) {
4284 struct rfapi_import_table *it;
4285 void *cursor;
4286 int rc;
4287
9d303b37
DL
4288 for (cursor = NULL,
4289 rc = skiplist_next(h->import_mac, NULL, (void **)&it,
4290 &cursor);
d62a17ae 4291 !rc; rc = skiplist_next(h->import_mac, NULL, (void **)&it,
4292 &cursor)) {
4293
4294 rfapiImportTableFlush(it);
4295 XFREE(MTYPE_RFAPI_IMPORTTABLE, it);
4296 }
4297 skiplist_free(h->import_mac);
4298 h->import_mac = NULL;
4299 }
4300
e208c8f9 4301 work_queue_free_and_null(&h->deferred_close_q);
d62a17ae 4302
4303 if (h->rfp != NULL)
4304 rfp_stop(h->rfp);
0ae6124f
LB
4305
4306 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
fe08ba7e 4307 agg_table_finish(h->un[afi]);
0ae6124f
LB
4308 }
4309
d62a17ae 4310 XFREE(MTYPE_RFAPI_IMPORTTABLE, h->it_ce);
4311 XFREE(MTYPE_RFAPI, h);
65efcfce
LB
4312}
4313
4314struct rfapi_import_table *
d62a17ae 4315rfapiImportTableRefAdd(struct bgp *bgp, struct ecommunity *rt_import_list,
4316 struct rfapi_nve_group_cfg *rfg)
65efcfce 4317{
d62a17ae 4318 struct rfapi *h;
4319 struct rfapi_import_table *it;
4320 afi_t afi;
4321
4322 h = bgp->rfapi;
4323 assert(h);
4324
4325 for (it = h->imports; it; it = it->next) {
4326 if (ecommunity_cmp(it->rt_import_list, rt_import_list))
4327 break;
4328 }
4329
4330 vnc_zlog_debug_verbose("%s: matched it=%p", __func__, it);
4331
4332 if (!it) {
4333 it = XCALLOC(MTYPE_RFAPI_IMPORTTABLE,
4334 sizeof(struct rfapi_import_table));
4335 assert(it);
4336 it->next = h->imports;
4337 h->imports = it;
4338
4339 it->rt_import_list = ecommunity_dup(rt_import_list);
4340 it->rfg = rfg;
4341 it->monitor_exterior_orphans =
63265b5c 4342 skiplist_new(0, NULL, prefix_free_lists);
d62a17ae 4343
4344 /*
4345 * fill import route tables from RIBs
4346 *
4347 * Potential area for optimization. If this occurs when
4348 * tables are large (e.g., the operator adds a nve group
4349 * with a new RT list to a running system), it could take
4350 * a while.
4351 *
4352 */
4353 for (afi = AFI_IP; afi < AFI_MAX; ++afi) {
4354
fe08ba7e
DS
4355 it->imported_vpn[afi] = agg_table_init();
4356 it->imported_encap[afi] = agg_table_init();
d62a17ae 4357
4358 rfapiBgpTableFilteredImport(bgp, it, afi,
4359 SAFI_MPLS_VPN);
4360 rfapiBgpTableFilteredImport(bgp, it, afi, SAFI_ENCAP);
4361
4362 vnc_import_bgp_exterior_redist_enable_it(bgp, afi, it);
4363 }
4364 }
4365
4366 it->refcount += 1;
4367
4368 return it;
65efcfce
LB
4369}
4370
4371/*
4372 * skiplist element free function
4373 */
d62a17ae 4374static void delete_rem_pfx_na_free(void *na)
65efcfce 4375{
d62a17ae 4376 uint32_t *pCounter = ((struct rfapi_nve_addr *)na)->info;
65efcfce 4377
d62a17ae 4378 *pCounter += 1;
4379 XFREE(MTYPE_RFAPI_NVE_ADDR, na);
65efcfce
LB
4380}
4381
4382/*
4383 * Common deleter for IP and MAC import tables
4384 */
d62a17ae 4385static void rfapiDeleteRemotePrefixesIt(
4386 struct bgp *bgp, struct rfapi_import_table *it, struct prefix *un,
4387 struct prefix *vn, struct prefix *p, int delete_active,
4388 int delete_holddown, uint32_t *pARcount, uint32_t *pAHcount,
4389 uint32_t *pHRcount, uint32_t *pHHcount,
4390 struct skiplist *uniq_active_nves, struct skiplist *uniq_holddown_nves)
65efcfce 4391{
d62a17ae 4392 afi_t afi;
65efcfce 4393
1e20238a 4394#ifdef DEBUG_L2_EXTRA
d62a17ae 4395 {
872ed4c7 4396 char buf_pfx[PREFIX_STRLEN];
d62a17ae 4397
4398 if (p) {
872ed4c7 4399 prefix2str(p, buf_pfx, sizeof(buf_pfx));
d62a17ae 4400 } else {
4401 buf_pfx[0] = '*';
4402 buf_pfx[1] = 0;
4403 }
4404
4405 vnc_zlog_debug_verbose(
4406 "%s: entry, p=%s, delete_active=%d, delete_holddown=%d",
4407 __func__, buf_pfx, delete_active, delete_holddown);
4408 }
65efcfce
LB
4409#endif
4410
d62a17ae 4411 for (afi = AFI_IP; afi < AFI_MAX; ++afi) {
4412
fe08ba7e
DS
4413 struct agg_table *rt;
4414 struct agg_node *rn;
d62a17ae 4415
4416 if (p && (family2afi(p->family) != afi)) {
4417 continue;
4418 }
4419
4420 rt = it->imported_vpn[afi];
4421 if (!rt)
4422 continue;
4423
4424 vnc_zlog_debug_verbose("%s: scanning rt for afi=%d", __func__,
4425 afi);
4426
fe08ba7e 4427 for (rn = agg_route_top(rt); rn; rn = agg_route_next(rn)) {
40381db7 4428 struct bgp_path_info *bpi;
4b7e6066 4429 struct bgp_path_info *next;
26a3ffd6 4430 const struct prefix *rn_p = agg_node_get_prefix(rn);
d62a17ae 4431
2dbe669b
DA
4432 if (p && VNC_DEBUG(IMPORT_DEL_REMOTE))
4433 vnc_zlog_debug_any("%s: want %pFX, have %pRN",
4434 __func__, p, rn);
d62a17ae 4435
26a3ffd6 4436 if (p && prefix_cmp(p, rn_p))
d62a17ae 4437 continue;
4438
26a3ffd6 4439 vnc_zlog_debug_verbose("%s: rn pfx=%pRN", __func__, rn);
d62a17ae 4440
4441 /* TBD is this valid for afi == AFI_L2VPN? */
4442 RFAPI_CHECK_REFCOUNT(rn, SAFI_MPLS_VPN, 1);
4443
40381db7
DS
4444 for (bpi = rn->info; bpi; bpi = next) {
4445 next = bpi->next;
d62a17ae 4446
4447 struct prefix qpt;
4448 struct prefix qct;
4449 int qpt_valid = 0;
4450 int qct_valid = 0;
4451 int is_active = 0;
4452
40381db7
DS
4453 vnc_zlog_debug_verbose("%s: examining bpi %p",
4454 __func__, bpi);
d62a17ae 4455
05864da7
DS
4456 if (!rfapiGetNexthop(bpi->attr, &qpt))
4457 qpt_valid = 1;
4458
d62a17ae 4459 if (vn) {
4460 if (!qpt_valid
4461 || !prefix_match(vn, &qpt)) {
1e20238a 4462#ifdef DEBUG_L2_EXTRA
d62a17ae 4463 vnc_zlog_debug_verbose(
4464 "%s: continue at vn && !qpt_valid || !prefix_match(vn, &qpt)",
4465 __func__);
65efcfce 4466#endif
d62a17ae 4467 continue;
4468 }
4469 }
65efcfce 4470
40381db7 4471 if (!rfapiGetUnAddrOfVpnBi(bpi, &qct))
d62a17ae 4472 qct_valid = 1;
65efcfce 4473
d62a17ae 4474 if (un) {
4475 if (!qct_valid
4476 || !prefix_match(un, &qct)) {
1e20238a 4477#ifdef DEBUG_L2_EXTRA
d62a17ae 4478 vnc_zlog_debug_verbose(
4479 "%s: continue at un && !qct_valid || !prefix_match(un, &qct)",
4480 __func__);
65efcfce 4481#endif
d62a17ae 4482 continue;
4483 }
4484 }
4485
4486
4487 /*
40381db7 4488 * Blow bpi away
d62a17ae 4489 */
4490 /*
4491 * If this route is waiting to be deleted
4492 * because of
4493 * a previous withdraw, we must cancel its
4494 * timer.
4495 */
40381db7 4496 if (CHECK_FLAG(bpi->flags, BGP_PATH_REMOVED)) {
d62a17ae 4497 if (!delete_holddown)
4498 continue;
40381db7 4499 if (bpi->extra->vnc.import.timer) {
d62a17ae 4500
b3d6bc6e
MS
4501 struct thread **t =
4502 &(bpi->extra->vnc
4503 .import.timer);
d62a17ae 4504 struct rfapi_withdraw *wcb =
b3d6bc6e 4505 (*t)->arg;
d62a17ae 4506
4507 wcb->import_table
4508 ->holddown_count[afi] -=
4509 1;
4510 RFAPI_UPDATE_ITABLE_COUNT(
40381db7 4511 bpi, wcb->import_table,
d62a17ae 4512 afi, 1);
4513 XFREE(MTYPE_RFAPI_WITHDRAW,
4514 wcb);
4515 thread_cancel(t);
4516 }
4517 } else {
4518 if (!delete_active)
4519 continue;
4520 is_active = 1;
4521 }
4522
4523 vnc_zlog_debug_verbose(
40381db7
DS
4524 "%s: deleting bpi %p (qct_valid=%d, qpt_valid=%d, delete_holddown=%d, delete_active=%d)",
4525 __func__, bpi, qct_valid, qpt_valid,
d62a17ae 4526 delete_holddown, delete_active);
4527
4528
4529 /*
4530 * add nve to list
4531 */
4532 if (qct_valid && qpt_valid) {
4533
4534 struct rfapi_nve_addr na;
4535 struct rfapi_nve_addr *nap;
4536
4537 memset(&na, 0, sizeof(na));
4538 assert(!rfapiQprefix2Raddr(&qct,
4539 &na.un));
4540 assert(!rfapiQprefix2Raddr(&qpt,
4541 &na.vn));
4542
4543 if (skiplist_search(
4544 (is_active
4545 ? uniq_active_nves
4546 : uniq_holddown_nves),
4547 &na, (void **)&nap)) {
4548 char line[BUFSIZ];
4549
4550 nap = XCALLOC(
4551 MTYPE_RFAPI_NVE_ADDR,
4552 sizeof(struct
4553 rfapi_nve_addr));
4554 assert(nap);
4555 *nap = na;
4556 nap->info = is_active
4557 ? pAHcount
4558 : pHHcount;
4559 skiplist_insert(
4560 (is_active
4561 ? uniq_active_nves
4562 : uniq_holddown_nves),
4563 nap, nap);
4564
4565 rfapiNveAddr2Str(nap, line,
4566 BUFSIZ);
4567 }
4568 }
4569
26a3ffd6 4570 vnc_direct_bgp_rh_del_route(bgp, afi, rn_p,
40381db7 4571 bpi->peer);
d62a17ae 4572
40381db7 4573 RFAPI_UPDATE_ITABLE_COUNT(bpi, it, afi, -1);
d62a17ae 4574 it->holddown_count[afi] += 1;
40381db7 4575 rfapiExpireVpnNow(it, rn, bpi, 1);
d62a17ae 4576
4577 vnc_zlog_debug_verbose(
4578 "%s: incrementing count (is_active=%d)",
4579 __func__, is_active);
4580
4581 if (is_active)
4582 ++*pARcount;
4583 else
4584 ++*pHRcount;
4585 }
4586 }
4587 }
65efcfce
LB
4588}
4589
4590
4591/*
4592 * For use by the "clear vnc prefixes" command
4593 */
4594/*------------------------------------------
4595 * rfapiDeleteRemotePrefixes
4596 *
4597 * UI helper: For use by the "clear vnc prefixes" command
4598 *
d62a17ae 4599 * input:
65efcfce
LB
4600 * un if set, tunnel must match this prefix
4601 * vn if set, nexthop prefix must match this prefix
4602 * p if set, prefix must match this prefix
5ff06872 4603 * it if set, only look in this import table
65efcfce
LB
4604 *
4605 * output
4606 * pARcount number of active routes deleted
4607 * pAHcount number of active nves deleted
4608 * pHRcount number of holddown routes deleted
4609 * pHHcount number of holddown nves deleted
4610 *
4611 * return value:
4612 * void
4613 --------------------------------------------*/
d62a17ae 4614void rfapiDeleteRemotePrefixes(struct prefix *un, struct prefix *vn,
4615 struct prefix *p,
4616 struct rfapi_import_table *arg_it,
4617 int delete_active, int delete_holddown,
4618 uint32_t *pARcount, uint32_t *pAHcount,
4619 uint32_t *pHRcount, uint32_t *pHHcount)
65efcfce 4620{
d62a17ae 4621 struct bgp *bgp;
4622 struct rfapi *h;
4623 struct rfapi_import_table *it;
4624 uint32_t deleted_holddown_route_count = 0;
4625 uint32_t deleted_active_route_count = 0;
4626 uint32_t deleted_holddown_nve_count = 0;
4627 uint32_t deleted_active_nve_count = 0;
4628 struct skiplist *uniq_holddown_nves;
4629 struct skiplist *uniq_active_nves;
4630
4631 VNC_ITRCCK;
4632
4633 bgp = bgp_get_default(); /* assume 1 instance for now */
4634 /* If no bgp instantiated yet, no vnc prefixes exist */
4635 if (!bgp)
4636 return;
4637
4638 h = bgp->rfapi;
4639 assert(h);
4640
4641 uniq_holddown_nves =
4642 skiplist_new(0, rfapi_nve_addr_cmp, delete_rem_pfx_na_free);
4643 uniq_active_nves =
4644 skiplist_new(0, rfapi_nve_addr_cmp, delete_rem_pfx_na_free);
4645
4646 /*
4647 * Iterate over all import tables; do a filtered import
4648 * for the afi/safi combination
4649 */
4650
4651 if (arg_it)
4652 it = arg_it;
4653 else
4654 it = h->imports;
4655 for (; it;) {
4656
4657 vnc_zlog_debug_verbose(
4658 "%s: calling rfapiDeleteRemotePrefixesIt() on (IP) import %p",
4659 __func__, it);
4660
4661 rfapiDeleteRemotePrefixesIt(
4662 bgp, it, un, vn, p, delete_active, delete_holddown,
4663 &deleted_active_route_count, &deleted_active_nve_count,
4664 &deleted_holddown_route_count,
4665 &deleted_holddown_nve_count, uniq_active_nves,
4666 uniq_holddown_nves);
4667
4668 if (arg_it)
4669 it = NULL;
4670 else
4671 it = it->next;
4672 }
4673
4674 /*
4675 * Now iterate over L2 import tables
4676 */
4677 if (h->import_mac && !(p && (p->family != AF_ETHERNET))) {
4678
4679 void *cursor = NULL;
4680 int rc;
4681
9d303b37
DL
4682 for (cursor = NULL,
4683 rc = skiplist_next(h->import_mac, NULL, (void **)&it,
4684 &cursor);
d62a17ae 4685 !rc; rc = skiplist_next(h->import_mac, NULL, (void **)&it,
4686 &cursor)) {
4687
4688 vnc_zlog_debug_verbose(
4689 "%s: calling rfapiDeleteRemotePrefixesIt() on import_mac %p",
4690 __func__, it);
4691
4692 rfapiDeleteRemotePrefixesIt(
4693 bgp, it, un, vn, p, delete_active,
4694 delete_holddown, &deleted_active_route_count,
4695 &deleted_active_nve_count,
4696 &deleted_holddown_route_count,
4697 &deleted_holddown_nve_count, uniq_active_nves,
4698 uniq_holddown_nves);
4699 }
4700 }
4701
4702 /*
4703 * our custom element freeing function above counts as it deletes
4704 */
4705 skiplist_free(uniq_holddown_nves);
4706 skiplist_free(uniq_active_nves);
4707
4708 if (pARcount)
4709 *pARcount = deleted_active_route_count;
4710 if (pAHcount)
4711 *pAHcount = deleted_active_nve_count;
4712 if (pHRcount)
4713 *pHRcount = deleted_holddown_route_count;
4714 if (pHHcount)
4715 *pHHcount = deleted_holddown_nve_count;
4716
4717 VNC_ITRCCK;
65efcfce
LB
4718}
4719
4720/*------------------------------------------
4721 * rfapiCountRemoteRoutes
4722 *
4723 * UI helper: count VRF routes from BGP side
4724 *
d62a17ae 4725 * input:
65efcfce
LB
4726 *
4727 * output
4728 * pALRcount count of active local routes
4729 * pARRcount count of active remote routes
4730 * pHRcount count of holddown routes
4731 * pIRcount count of direct imported routes
4732 *
4733 * return value:
4734 * void
4735 --------------------------------------------*/
d62a17ae 4736void rfapiCountAllItRoutes(int *pALRcount, /* active local routes */
4737 int *pARRcount, /* active remote routes */
4738 int *pHRcount, /* holddown routes */
4739 int *pIRcount) /* imported routes */
65efcfce 4740{
d62a17ae 4741 struct bgp *bgp;
4742 struct rfapi *h;
4743 struct rfapi_import_table *it;
4744 afi_t afi;
4745
4746 int total_active_local = 0;
4747 int total_active_remote = 0;
4748 int total_holddown = 0;
4749 int total_imported = 0;
4750
4751 bgp = bgp_get_default(); /* assume 1 instance for now */
4752 assert(bgp);
4753
4754 h = bgp->rfapi;
4755 assert(h);
4756
4757 /*
4758 * Iterate over all import tables; do a filtered import
4759 * for the afi/safi combination
4760 */
4761
4762 for (it = h->imports; it; it = it->next) {
4763
4764 for (afi = AFI_IP; afi < AFI_MAX; ++afi) {
4765
4766 total_active_local += it->local_count[afi];
4767 total_active_remote += it->remote_count[afi];
4768 total_holddown += it->holddown_count[afi];
4769 total_imported += it->imported_count[afi];
4770 }
4771 }
4772
4773 void *cursor;
4774 int rc;
4775
4776 if (h->import_mac) {
9d303b37
DL
4777 for (cursor = NULL,
4778 rc = skiplist_next(h->import_mac, NULL, (void **)&it,
4779 &cursor);
d62a17ae 4780 !rc; rc = skiplist_next(h->import_mac, NULL, (void **)&it,
4781 &cursor)) {
4782
4783 total_active_local += it->local_count[AFI_L2VPN];
4784 total_active_remote += it->remote_count[AFI_L2VPN];
4785 total_holddown += it->holddown_count[AFI_L2VPN];
4786 total_imported += it->imported_count[AFI_L2VPN];
4787 }
4788 }
4789
4790
4791 if (pALRcount) {
4792 *pALRcount = total_active_local;
4793 }
4794 if (pARRcount) {
4795 *pARRcount = total_active_remote;
4796 }
4797 if (pHRcount) {
4798 *pHRcount = total_holddown;
4799 }
4800 if (pIRcount) {
4801 *pIRcount = total_imported;
4802 }
65efcfce
LB
4803}
4804
4805/*------------------------------------------
4806 * rfapiGetHolddownFromLifetime
4807 *
4808 * calculate holddown value based on lifetime
4809 *
d62a17ae 4810 * input:
65efcfce
LB
4811 * lifetime lifetime
4812 *
4813 * return value:
4814 * Holddown value based on lifetime, holddown_factor,
4815 * and RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY
4816 *
4817 --------------------------------------------*/
4818/* hold down time maxes out at RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY */
d62a17ae 4819uint32_t rfapiGetHolddownFromLifetime(uint32_t lifetime)
65efcfce 4820{
d62a17ae 4821 uint32_t factor;
4822 struct bgp *bgp;
4823
4824 bgp = bgp_get_default();
4825 if (bgp && bgp->rfapi_cfg)
4826 factor = bgp->rfapi_cfg->rfp_cfg.holddown_factor;
4827 else
4828 factor = RFAPI_RFP_CFG_DEFAULT_HOLDDOWN_FACTOR;
4829
4830 if (factor < 100 || lifetime < RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY)
4831 lifetime = lifetime * factor / 100;
4832 if (lifetime < RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY)
4833 return lifetime;
4834 else
4835 return RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY;
65efcfce 4836}