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