]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/rfapi/rfapi_rib.c
*: manual SPDX License ID conversions
[mirror_frr.git] / bgpd / rfapi / rfapi_rib.c
CommitLineData
d62a17ae 1/*
65efcfce
LB
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 *
896014f4
DL
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
65efcfce
LB
19 */
20
21/*
22 * File: rfapi_rib.c
23 * Purpose: maintain per-nve ribs and generate change lists
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/workqueue.h"
65efcfce 34
f8b6f499
LB
35#include "bgpd/bgpd.h"
36#include "bgpd/bgp_route.h"
37#include "bgpd/bgp_ecommunity.h"
38#include "bgpd/bgp_mplsvpn.h"
39#include "bgpd/bgp_vnc_types.h"
65efcfce 40
f8b6f499
LB
41#include "bgpd/rfapi/rfapi.h"
42#include "bgpd/rfapi/bgp_rfapi_cfg.h"
43#include "bgpd/rfapi/rfapi_import.h"
44#include "bgpd/rfapi/rfapi_private.h"
45#include "bgpd/rfapi/rfapi_vty.h"
46#include "bgpd/rfapi/vnc_import_bgp.h"
47#include "bgpd/rfapi/rfapi_rib.h"
48#include "bgpd/rfapi/rfapi_monitor.h"
49#include "bgpd/rfapi/rfapi_encap_tlv.h"
a3b55c25 50#include "bgpd/rfapi/vnc_debug.h"
65efcfce
LB
51
52#define DEBUG_PROCESS_PENDING_NODE 0
53#define DEBUG_PENDING_DELETE_ROUTE 0
54#define DEBUG_NHL 0
55#define DEBUG_RIB_SL_RD 0
56
57/* forward decl */
58#if DEBUG_NHL
d62a17ae 59static void rfapiRibShowRibSl(void *stream, struct prefix *pfx,
60 struct skiplist *sl);
65efcfce
LB
61#endif
62
63/*
64 * RIB
65 * ---
66 * Model of the set of routes currently in the NVE's RIB.
67 *
68 * node->info ptr to "struct skiplist".
69 * MUST be NULL if there are no routes.
70 * key = ptr to struct prefix {vn}
71 * val = ptr to struct rfapi_info
72 * skiplist.del = NULL
73 * skiplist.cmp = vnc_prefix_cmp
74 *
75 * node->aggregate ptr to "struct skiplist".
76 * key = ptr to struct prefix {vn}
77 * val = ptr to struct rfapi_info
78 * skiplist.del = rfapi_info_free
79 * skiplist.cmp = vnc_prefix_cmp
80 *
81 * This skiplist at "aggregate"
82 * contains the routes recently
83 * deleted
84 *
85 *
86 * Pending RIB
87 * -----------
88 * Sparse list of prefixes that need to be updated. Each node
89 * will have the complete set of routes for the prefix.
90 *
91 * node->info ptr to "struct list" (lib/linklist.h)
92 * "Cost List"
93 * List of routes sorted lowest cost first.
94 * This list is how the new complete set
95 * of routes should look.
96 * Set if there are updates to the prefix;
97 * MUST be NULL if there are no updates.
98 *
99 * .data = ptr to struct rfapi_info
100 * list.cmp = NULL (sorted manually)
101 * list.del = rfapi_info_free
102 *
103 * Special case: if node->info is 1, it means
104 * "delete all routes at this prefix".
105 *
106 * node->aggregate ptr to struct skiplist
107 * key = ptr to struct prefix {vn} (part of ri)
108 * val = struct rfapi_info
109 * skiplist.cmp = vnc_prefix_cmp
110 * skiplist.del = NULL
111 *
112 * ptlist is rewritten anew each time
113 * rfapiRibUpdatePendingNode() is called
114 *
115 * THE ptlist VALUES ARE REFERENCES TO THE
116 * rfapi_info STRUCTS IN THE node->info LIST.
117 */
118
119/*
120 * iterate over RIB to count responses, compare with running counters
121 */
d62a17ae 122void rfapiRibCheckCounts(
123 int checkstats, /* validate rfd & global counts */
124 unsigned int offset) /* number of ri's held separately */
65efcfce 125{
d62a17ae 126 struct rfapi_descriptor *rfd;
127 struct listnode *node;
128
129 struct bgp *bgp = bgp_get_default();
130
131 uint32_t t_pfx_active = 0;
132 uint32_t t_pfx_deleted = 0;
133
134 uint32_t t_ri_active = 0;
135 uint32_t t_ri_deleted = 0;
136 uint32_t t_ri_pend = 0;
137
138 unsigned int alloc_count;
139
140 /*
141 * loop over NVEs
142 */
143 for (ALL_LIST_ELEMENTS_RO(&bgp->rfapi->descriptors, node, rfd)) {
144
145 afi_t afi;
146 uint32_t pfx_active = 0;
147 uint32_t pfx_deleted = 0;
148
149 for (afi = AFI_IP; afi < AFI_MAX; ++afi) {
150
fe08ba7e 151 struct agg_node *rn;
d62a17ae 152
fe08ba7e
DS
153 for (rn = agg_route_top(rfd->rib[afi]); rn;
154 rn = agg_route_next(rn)) {
d62a17ae 155
156 struct skiplist *sl = rn->info;
157 struct skiplist *dsl = rn->aggregate;
158 uint32_t ri_active = 0;
159 uint32_t ri_deleted = 0;
160
161 if (sl) {
162 ri_active = skiplist_count(sl);
163 assert(ri_active);
164 t_ri_active += ri_active;
165 ++pfx_active;
166 ++t_pfx_active;
167 }
168
169 if (dsl) {
170 ri_deleted = skiplist_count(dsl);
171 t_ri_deleted += ri_deleted;
172 ++pfx_deleted;
173 ++t_pfx_deleted;
174 }
175 }
fe08ba7e
DS
176 for (rn = agg_route_top(rfd->rib_pending[afi]); rn;
177 rn = agg_route_next(rn)) {
d62a17ae 178
179 struct list *l = rn->info; /* sorted by cost */
180 struct skiplist *sl = rn->aggregate;
181 uint32_t ri_pend_cost = 0;
182 uint32_t ri_pend_uniq = 0;
183
184 if (sl) {
185 ri_pend_uniq = skiplist_count(sl);
186 }
187
188 if (l && (l != (void *)1)) {
189 ri_pend_cost = l->count;
190 t_ri_pend += l->count;
191 }
192
193 assert(ri_pend_uniq == ri_pend_cost);
194 }
195 }
196
197 if (checkstats) {
198 if (pfx_active != rfd->rib_prefix_count) {
199 vnc_zlog_debug_verbose(
200 "%s: rfd %p actual pfx count %u != running %u",
201 __func__, rfd, pfx_active,
202 rfd->rib_prefix_count);
203 assert(0);
204 }
205 }
206 }
207
8cea9547 208 if (checkstats && bgp->rfapi) {
d62a17ae 209 if (t_pfx_active != bgp->rfapi->rib_prefix_count_total) {
210 vnc_zlog_debug_verbose(
211 "%s: actual total pfx count %u != running %u",
212 __func__, t_pfx_active,
213 bgp->rfapi->rib_prefix_count_total);
214 assert(0);
215 }
216 }
217
218 /*
219 * Check against memory allocation count
220 */
221 alloc_count = mtype_stats_alloc(MTYPE_RFAPI_INFO);
222 assert(t_ri_active + t_ri_deleted + t_ri_pend + offset == alloc_count);
65efcfce
LB
223}
224
4d762f26 225static struct rfapi_info *rfapi_info_new(void)
65efcfce 226{
d62a17ae 227 return XCALLOC(MTYPE_RFAPI_INFO, sizeof(struct rfapi_info));
65efcfce
LB
228}
229
d62a17ae 230void rfapiFreeRfapiUnOptionChain(struct rfapi_un_option *p)
65efcfce 231{
d62a17ae 232 while (p) {
233 struct rfapi_un_option *next;
234
235 next = p->next;
236 XFREE(MTYPE_RFAPI_UN_OPTION, p);
237 p = next;
238 }
65efcfce
LB
239}
240
d62a17ae 241void rfapiFreeRfapiVnOptionChain(struct rfapi_vn_option *p)
65efcfce 242{
d62a17ae 243 while (p) {
244 struct rfapi_vn_option *next;
245
246 next = p->next;
247 XFREE(MTYPE_RFAPI_VN_OPTION, p);
248 p = next;
249 }
65efcfce
LB
250}
251
252
d62a17ae 253static void rfapi_info_free(struct rfapi_info *goner)
65efcfce 254{
d62a17ae 255 if (goner) {
256 if (goner->tea_options) {
257 rfapiFreeBgpTeaOptionChain(goner->tea_options);
258 goner->tea_options = NULL;
259 }
260 if (goner->un_options) {
261 rfapiFreeRfapiUnOptionChain(goner->un_options);
262 goner->un_options = NULL;
263 }
264 if (goner->vn_options) {
265 rfapiFreeRfapiVnOptionChain(goner->vn_options);
266 goner->vn_options = NULL;
267 }
268 if (goner->timer) {
269 struct rfapi_rib_tcb *tcb;
270
c3aaa89a
DS
271 tcb = THREAD_ARG(goner->timer);
272 THREAD_OFF(goner->timer);
d62a17ae 273 XFREE(MTYPE_RFAPI_RECENT_DELETE, tcb);
d62a17ae 274 }
275 XFREE(MTYPE_RFAPI_INFO, goner);
276 }
65efcfce
LB
277}
278
279/*
280 * Timer control block for recently-deleted and expired routes
281 */
d62a17ae 282struct rfapi_rib_tcb {
283 struct rfapi_descriptor *rfd;
284 struct skiplist *sl;
285 struct rfapi_info *ri;
fe08ba7e 286 struct agg_node *rn;
d62a17ae 287 int flags;
65efcfce
LB
288#define RFAPI_RIB_TCB_FLAG_DELETED 0x00000001
289};
290
291/*
292 * remove route from rib
293 */
cc9f21da 294static void rfapiRibExpireTimer(struct thread *t)
65efcfce 295{
c3aaa89a 296 struct rfapi_rib_tcb *tcb = THREAD_ARG(t);
d62a17ae 297
298 RFAPI_RIB_CHECK_COUNTS(1, 0);
299
300 /*
301 * Forget reference to thread. Otherwise rfapi_info_free() will
302 * attempt to free thread pointer as an option chain
303 */
304 tcb->ri->timer = NULL;
305
306 /* "deleted" skiplist frees ri, "active" doesn't */
307 assert(!skiplist_delete(tcb->sl, &tcb->ri->rk, NULL));
308 if (!tcb->sl->del) {
309 /*
310 * XXX in this case, skiplist has no delete function: we must
311 * therefore delete rfapi_info explicitly.
312 */
313 rfapi_info_free(tcb->ri);
314 }
315
316 if (skiplist_empty(tcb->sl)) {
317 if (CHECK_FLAG(tcb->flags, RFAPI_RIB_TCB_FLAG_DELETED))
318 tcb->rn->aggregate = NULL;
319 else {
320 struct bgp *bgp = bgp_get_default();
321 tcb->rn->info = NULL;
322 RFAPI_RIB_PREFIX_COUNT_DECR(tcb->rfd, bgp->rfapi);
323 }
324 skiplist_free(tcb->sl);
fe08ba7e 325 agg_unlock_node(tcb->rn);
d62a17ae 326 }
327
328 XFREE(MTYPE_RFAPI_RECENT_DELETE, tcb);
329
330 RFAPI_RIB_CHECK_COUNTS(1, 0);
65efcfce
LB
331}
332
fe08ba7e
DS
333static void rfapiRibStartTimer(struct rfapi_descriptor *rfd,
334 struct rfapi_info *ri,
335 struct agg_node *rn, /* route node attached to */
336 int deleted)
65efcfce 337{
d62a17ae 338 struct rfapi_rib_tcb *tcb = NULL;
d62a17ae 339
b3d6bc6e 340 if (ri->timer) {
c3aaa89a
DS
341 tcb = THREAD_ARG(ri->timer);
342 THREAD_OFF(ri->timer);
d62a17ae 343 } else {
344 tcb = XCALLOC(MTYPE_RFAPI_RECENT_DELETE,
345 sizeof(struct rfapi_rib_tcb));
346 }
347 tcb->rfd = rfd;
348 tcb->ri = ri;
349 tcb->rn = rn;
350 if (deleted) {
351 tcb->sl = (struct skiplist *)rn->aggregate;
352 SET_FLAG(tcb->flags, RFAPI_RIB_TCB_FLAG_DELETED);
353 } else {
354 tcb->sl = (struct skiplist *)rn->info;
355 UNSET_FLAG(tcb->flags, RFAPI_RIB_TCB_FLAG_DELETED);
356 }
357
26a3ffd6
DS
358 vnc_zlog_debug_verbose("%s: rfd %p pfx %pRN life %u", __func__, rfd, rn,
359 ri->lifetime);
04fd828f 360
d62a17ae 361 thread_add_timer(bm->master, rfapiRibExpireTimer, tcb, ri->lifetime,
362 &ri->timer);
65efcfce
LB
363}
364
d62a17ae 365extern void rfapi_rib_key_init(struct prefix *prefix, /* may be NULL */
366 struct prefix_rd *rd, /* may be NULL */
367 struct prefix *aux, /* may be NULL */
368 struct rfapi_rib_key *rk)
369
cedb5a71 370{
d62a17ae 371 memset((void *)rk, 0, sizeof(struct rfapi_rib_key));
372 if (prefix)
373 rk->vn = *prefix;
374 if (rd)
375 rk->rd = *rd;
376 if (aux)
377 rk->aux_prefix = *aux;
cedb5a71
LB
378}
379
65efcfce
LB
380/*
381 * Compares two <struct rfapi_rib_key>s
382 */
1a4189d4 383int rfapi_rib_key_cmp(const void *k1, const void *k2)
65efcfce 384{
1a4189d4
DS
385 const struct rfapi_rib_key *a = (struct rfapi_rib_key *)k1;
386 const struct rfapi_rib_key *b = (struct rfapi_rib_key *)k2;
d62a17ae 387 int ret;
65efcfce 388
d62a17ae 389 if (!a || !b)
390 return (a - b);
65efcfce 391
d62a17ae 392 ret = vnc_prefix_cmp(&a->vn, &b->vn);
393 if (ret)
394 return ret;
65efcfce 395
d62a17ae 396 ret = vnc_prefix_cmp(&a->rd, &b->rd);
397 if (ret)
398 return ret;
65efcfce 399
d62a17ae 400 ret = vnc_prefix_cmp(&a->aux_prefix, &b->aux_prefix);
65efcfce 401
d62a17ae 402 return ret;
65efcfce
LB
403}
404
405
406/*
407 * Note: this function will claim that two option chains are
408 * different unless their option items are in identical order.
409 * The consequence is that RFP updated responses can be sent
410 * unnecessarily, or that they might contain nexthop items
411 * that are not strictly needed.
412 *
413 * This function could be modified to compare option chains more
414 * thoroughly, but it's not clear that the extra compuation would
415 * be worth it.
416 */
d62a17ae 417static int bgp_tea_options_cmp(struct bgp_tea_options *a,
418 struct bgp_tea_options *b)
65efcfce 419{
d62a17ae 420 int rc;
421
422 if (!a || !b) {
423 return (a - b);
424 }
65efcfce 425
d62a17ae 426 if (a->type != b->type)
427 return (a->type - b->type);
428 if (a->length != b->length)
429 return (a->length = b->length);
430 if ((rc = memcmp(a->value, b->value, a->length)))
431 return rc;
432 if (!a->next != !b->next) { /* logical xor */
433 return (a->next - b->next);
434 }
435 if (a->next)
436 return bgp_tea_options_cmp(a->next, b->next);
437 return 0;
65efcfce
LB
438}
439
d62a17ae 440static int rfapi_info_cmp(struct rfapi_info *a, struct rfapi_info *b)
65efcfce 441{
d62a17ae 442 int rc;
65efcfce 443
d62a17ae 444 if (!a || !b)
445 return (a - b);
65efcfce 446
d62a17ae 447 if ((rc = rfapi_rib_key_cmp(&a->rk, &b->rk)))
448 return rc;
65efcfce 449
d62a17ae 450 if ((rc = vnc_prefix_cmp(&a->un, &b->un)))
451 return rc;
65efcfce 452
d62a17ae 453 if (a->cost != b->cost)
454 return (a->cost - b->cost);
65efcfce 455
d62a17ae 456 if (a->lifetime != b->lifetime)
457 return (a->lifetime - b->lifetime);
65efcfce 458
d62a17ae 459 if ((rc = bgp_tea_options_cmp(a->tea_options, b->tea_options)))
460 return rc;
65efcfce 461
d62a17ae 462 return 0;
65efcfce
LB
463}
464
d62a17ae 465void rfapiRibClear(struct rfapi_descriptor *rfd)
65efcfce 466{
d62a17ae 467 struct bgp *bgp;
468 afi_t afi;
65efcfce 469
d62a17ae 470 if (rfd->bgp)
471 bgp = rfd->bgp;
472 else
473 bgp = bgp_get_default();
1e20238a 474#ifdef DEBUG_L2_EXTRA
d62a17ae 475 vnc_zlog_debug_verbose("%s: rfd=%p", __func__, rfd);
65efcfce
LB
476#endif
477
d62a17ae 478 for (afi = AFI_IP; afi < AFI_MAX; ++afi) {
fe08ba7e
DS
479 struct agg_node *pn;
480 struct agg_node *rn;
d62a17ae 481
482 if (rfd->rib_pending[afi]) {
fe08ba7e
DS
483 for (pn = agg_route_top(rfd->rib_pending[afi]); pn;
484 pn = agg_route_next(pn)) {
d62a17ae 485 if (pn->aggregate) {
486 /*
487 * free references into the rfapi_info
488 * structures before
489 * freeing the structures themselves
490 */
491 skiplist_free(
492 (struct skiplist
493 *)(pn->aggregate));
494 pn->aggregate = NULL;
fe08ba7e 495 agg_unlock_node(
d62a17ae 496 pn); /* skiplist deleted */
497 }
498 /*
499 * free the rfapi_info structures
500 */
501 if (pn->info) {
502 if (pn->info != (void *)1) {
6a154c88 503 list_delete(
996c9314
LB
504 (struct list *
505 *)(&pn->info));
d62a17ae 506 }
507 pn->info = NULL;
affe9e99 508 /* linklist or 1 deleted */
fe08ba7e 509 agg_unlock_node(pn);
d62a17ae 510 }
511 }
512 }
513 if (rfd->rib[afi]) {
fe08ba7e
DS
514 for (rn = agg_route_top(rfd->rib[afi]); rn;
515 rn = agg_route_next(rn)) {
d62a17ae 516 if (rn->info) {
517
518 struct rfapi_info *ri;
519
9d303b37
DL
520 while (0 == skiplist_first(
521 (struct skiplist *)
522 rn->info,
523 NULL,
524 (void **)&ri)) {
d62a17ae 525
526 rfapi_info_free(ri);
527 skiplist_delete_first(
528 (struct skiplist *)
529 rn->info);
530 }
531 skiplist_free(
532 (struct skiplist *)rn->info);
533 rn->info = NULL;
fe08ba7e 534 agg_unlock_node(rn);
d62a17ae 535 RFAPI_RIB_PREFIX_COUNT_DECR(rfd,
536 bgp->rfapi);
537 }
538 if (rn->aggregate) {
539
540 struct rfapi_info *ri_del;
541
542 /* delete skiplist & contents */
543 while (!skiplist_first(
544 (struct skiplist
545 *)(rn->aggregate),
546 NULL, (void **)&ri_del)) {
547
548 /* sl->del takes care of ri_del
549 */
550 skiplist_delete_first((
551 struct skiplist
552 *)(rn->aggregate));
553 }
554 skiplist_free(
555 (struct skiplist
556 *)(rn->aggregate));
557
558 rn->aggregate = NULL;
fe08ba7e 559 agg_unlock_node(rn);
d62a17ae 560 }
561 }
562 }
563 }
e208c8f9
DS
564 if (rfd->updated_responses_queue)
565 work_queue_free_and_null(&rfd->updated_responses_queue);
65efcfce
LB
566}
567
568/*
569 * Release all dynamically-allocated memory that is part of an HD's RIB
570 */
d62a17ae 571void rfapiRibFree(struct rfapi_descriptor *rfd)
65efcfce 572{
d62a17ae 573 afi_t afi;
65efcfce
LB
574
575
d62a17ae 576 /*
577 * NB rfd is typically detached from master list, so is not included
578 * in the count performed by RFAPI_RIB_CHECK_COUNTS
579 */
65efcfce 580
d62a17ae 581 /*
582 * Free routes attached to radix trees
583 */
584 rfapiRibClear(rfd);
65efcfce 585
d62a17ae 586 /* Now the uncounted rfapi_info's are freed, so the check should succeed
587 */
588 RFAPI_RIB_CHECK_COUNTS(1, 0);
65efcfce 589
d62a17ae 590 /*
591 * Free radix trees
592 */
593 for (afi = AFI_IP; afi < AFI_MAX; ++afi) {
fe08ba7e
DS
594 if (rfd->rib_pending[afi])
595 agg_table_finish(rfd->rib_pending[afi]);
d62a17ae 596 rfd->rib_pending[afi] = NULL;
65efcfce 597
fe08ba7e
DS
598 if (rfd->rib[afi])
599 agg_table_finish(rfd->rib[afi]);
d62a17ae 600 rfd->rib[afi] = NULL;
65efcfce 601
fe08ba7e 602 /* NB agg_table_finish frees only prefix nodes, not chained
d62a17ae 603 * info */
fe08ba7e
DS
604 if (rfd->rsp_times[afi])
605 agg_table_finish(rfd->rsp_times[afi]);
d62a17ae 606 rfd->rib[afi] = NULL;
607 }
65efcfce
LB
608}
609
610/*
4b7e6066 611 * Copies struct bgp_path_info to struct rfapi_info, except for rk fields and un
65efcfce 612 */
40381db7 613static void rfapiRibBi2Ri(struct bgp_path_info *bpi, struct rfapi_info *ri,
d62a17ae 614 uint32_t lifetime)
65efcfce 615{
d62a17ae 616 struct bgp_attr_encap_subtlv *pEncap;
617
40381db7 618 ri->cost = rfapiRfpCost(bpi->attr);
d62a17ae 619 ri->lifetime = lifetime;
620
621 /* This loop based on rfapiRouteInfo2NextHopEntry() */
91ebf12c
DS
622 for (pEncap = bgp_attr_get_vnc_subtlvs(bpi->attr); pEncap;
623 pEncap = pEncap->next) {
d62a17ae 624 struct bgp_tea_options *hop;
625
626 switch (pEncap->type) {
627 case BGP_VNC_SUBTLV_TYPE_LIFETIME:
628 /* use configured lifetime, not attr lifetime */
629 break;
630
631 case BGP_VNC_SUBTLV_TYPE_RFPOPTION:
632 hop = XCALLOC(MTYPE_BGP_TEA_OPTIONS,
633 sizeof(struct bgp_tea_options));
634 assert(hop);
635 hop->type = pEncap->value[0];
636 hop->length = pEncap->value[1];
637 hop->value = XCALLOC(MTYPE_BGP_TEA_OPTIONS_VALUE,
638 pEncap->length - 2);
639 assert(hop->value);
640 memcpy(hop->value, pEncap->value + 2,
641 pEncap->length - 2);
642 if (hop->length > pEncap->length - 2) {
643 zlog_warn(
3efd0893 644 "%s: VNC subtlv length mismatch: RFP option says %d, attr says %d (shrinking)",
d62a17ae 645 __func__, hop->length,
646 pEncap->length - 2);
647 hop->length = pEncap->length - 2;
648 }
649 hop->next = ri->tea_options;
650 ri->tea_options = hop;
651 break;
652
653 default:
654 break;
655 }
656 }
657
658 rfapi_un_options_free(ri->un_options); /* maybe free old version */
40381db7 659 ri->un_options = rfapi_encap_tlv_to_un_option(bpi->attr);
d62a17ae 660
661 /*
662 * VN options
663 */
40381db7
DS
664 if (bpi->extra
665 && decode_rd_type(bpi->extra->vnc.import.rd.val)
d62a17ae 666 == RD_TYPE_VNC_ETH) {
667 /* ethernet route */
668
669 struct rfapi_vn_option *vo;
670
671 vo = XCALLOC(MTYPE_RFAPI_VN_OPTION,
672 sizeof(struct rfapi_vn_option));
673 assert(vo);
674
675 vo->type = RFAPI_VN_OPTION_TYPE_L2ADDR;
676
40381db7 677 /* copy from RD already stored in bpi, so we don't need it_node
d62a17ae 678 */
40381db7 679 memcpy(&vo->v.l2addr.macaddr, bpi->extra->vnc.import.rd.val + 2,
28328ea9 680 ETH_ALEN);
d62a17ae 681
b53e67a3 682 (void)rfapiEcommunityGetLNI(bgp_attr_get_ecommunity(bpi->attr),
8cea9547 683 &vo->v.l2addr.logical_net_id);
b53e67a3
DA
684 (void)rfapiEcommunityGetEthernetTag(
685 bgp_attr_get_ecommunity(bpi->attr),
686 &vo->v.l2addr.tag_id);
d62a17ae 687
688 /* local_nve_id comes from RD */
40381db7 689 vo->v.l2addr.local_nve_id = bpi->extra->vnc.import.rd.val[1];
d62a17ae 690
691 /* label comes from MP_REACH_NLRI label */
40381db7 692 vo->v.l2addr.label = decode_label(&bpi->extra->label[0]);
d62a17ae 693
694 rfapi_vn_options_free(
695 ri->vn_options); /* maybe free old version */
696 ri->vn_options = vo;
697 }
698
699 /*
700 * If there is an auxiliary IP address (L2 can have it), copy it
701 */
40381db7
DS
702 if (bpi->extra && bpi->extra->vnc.import.aux_prefix.family) {
703 ri->rk.aux_prefix = bpi->extra->vnc.import.aux_prefix;
d62a17ae 704 }
65efcfce
LB
705}
706
707/*
708 * rfapiRibPreloadBi
709 *
710 * Install route into NVE RIB model so as to be consistent with
711 * caller's response to rfapi_query().
712 *
713 * Also: return indication to caller whether this specific route
714 * should be included in the response to the NVE according to
715 * the following tests:
716 *
717 * 1. If there were prior duplicates of this route in this same
718 * query response, don't include the route.
719 *
720 * RETURN VALUE:
721 *
722 * 0 OK to include route in response
723 * !0 do not include route in response
724 */
d62a17ae 725int rfapiRibPreloadBi(
fe08ba7e 726 struct agg_node *rfd_rib_node, /* NULL = don't preload or filter */
d62a17ae 727 struct prefix *pfx_vn, struct prefix *pfx_un, uint32_t lifetime,
40381db7 728 struct bgp_path_info *bpi)
65efcfce 729{
d62a17ae 730 struct rfapi_descriptor *rfd;
731 struct skiplist *slRibPt = NULL;
732 struct rfapi_info *ori = NULL;
733 struct rfapi_rib_key rk;
fe08ba7e 734 struct agg_node *trn;
d62a17ae 735 afi_t afi;
26a3ffd6 736 const struct prefix *p = agg_node_get_prefix(rfd_rib_node);
d62a17ae 737
738 if (!rfd_rib_node)
739 return 0;
740
26a3ffd6 741 afi = family2afi(p->family);
d62a17ae 742
fe08ba7e 743 rfd = agg_get_table_info(agg_get_table(rfd_rib_node));
d62a17ae 744
745 memset((void *)&rk, 0, sizeof(rk));
746 rk.vn = *pfx_vn;
40381db7 747 rk.rd = bpi->extra->vnc.import.rd;
d62a17ae 748
749 /*
750 * If there is an auxiliary IP address (L2 can have it), copy it
751 */
40381db7
DS
752 if (bpi->extra->vnc.import.aux_prefix.family) {
753 rk.aux_prefix = bpi->extra->vnc.import.aux_prefix;
d62a17ae 754 }
755
756 /*
757 * is this route already in NVE's RIB?
758 */
759 slRibPt = (struct skiplist *)rfd_rib_node->info;
760
761 if (slRibPt && !skiplist_search(slRibPt, &rk, (void **)&ori)) {
762
763 if ((ori->rsp_counter == rfd->rsp_counter)
764 && (ori->last_sent_time == rfd->rsp_time)) {
765 return -1; /* duplicate in this response */
766 }
767
768 /* found: update contents of existing route in RIB */
769 ori->un = *pfx_un;
40381db7 770 rfapiRibBi2Ri(bpi, ori, lifetime);
d62a17ae 771 } else {
772 /* not found: add new route to RIB */
773 ori = rfapi_info_new();
774 ori->rk = rk;
775 ori->un = *pfx_un;
40381db7 776 rfapiRibBi2Ri(bpi, ori, lifetime);
d62a17ae 777
778 if (!slRibPt) {
779 slRibPt = skiplist_new(0, rfapi_rib_key_cmp, NULL);
780 rfd_rib_node->info = slRibPt;
fe08ba7e 781 agg_lock_node(rfd_rib_node);
d62a17ae 782 RFAPI_RIB_PREFIX_COUNT_INCR(rfd, rfd->bgp->rfapi);
783 }
784 skiplist_insert(slRibPt, &ori->rk, ori);
65efcfce
LB
785 }
786
98b7e975 787 ori->last_sent_time = monotime(NULL);
d62a17ae 788
789 /*
790 * poke timer
791 */
792 RFAPI_RIB_CHECK_COUNTS(0, 0);
793 rfapiRibStartTimer(rfd, ori, rfd_rib_node, 0);
794 RFAPI_RIB_CHECK_COUNTS(0, 0);
795
796 /*
797 * Update last sent time for prefix
798 */
26a3ffd6 799 trn = agg_node_get(rfd->rsp_times[afi], p); /* locks trn */
083ec940 800 trn->info = (void *)(uintptr_t)monotime(NULL);
c10e14e9 801 if (agg_node_get_lock_count(trn) > 1)
fe08ba7e 802 agg_unlock_node(trn);
d62a17ae 803
804 return 0;
65efcfce
LB
805}
806
807/*
808 * Frees rfapi_info items at node
809 *
810 * Adjust 'rib' and 'rib_pending' as follows:
811 *
812 * If rib_pending node->info is 1 (magic value):
813 * callback: NHL = RIB NHL with lifetime = withdraw_lifetime_value
814 * RIB = remove all routes at the node
815 * DONE
816 *
817 * For each item at rib node:
818 * if not present in pending node, move RIB item to "delete list"
819 *
820 * For each item at pending rib node:
821 * if present (same vn/un) in rib node with same lifetime & options, drop
822 * matching item from pending node
823 *
824 * For each remaining item at pending rib node, add or replace item
825 * at rib node.
826 *
827 * Construct NHL as concatenation of pending list + delete list
828 *
829 * Clear pending node
830 */
d62a17ae 831static void process_pending_node(struct bgp *bgp, struct rfapi_descriptor *rfd,
832 afi_t afi,
fe08ba7e 833 struct agg_node *pn, /* pending node */
d62a17ae 834 struct rfapi_next_hop_entry **head,
835 struct rfapi_next_hop_entry **tail)
65efcfce 836{
d62a17ae 837 struct listnode *node = NULL;
838 struct listnode *nnode = NULL;
839 struct rfapi_info *ri = NULL; /* happy valgrind */
840 struct rfapi_ip_prefix hp = {0}; /* pfx to put in NHE */
fe08ba7e 841 struct agg_node *rn = NULL;
d62a17ae 842 struct skiplist *slRibPt = NULL; /* rib list */
843 struct skiplist *slPendPt = NULL;
844 struct list *lPendCost = NULL;
845 struct list *delete_list = NULL;
846 int printedprefix = 0;
d62a17ae 847 int rib_node_started_nonempty = 0;
848 int sendingsomeroutes = 0;
26a3ffd6 849 const struct prefix *p;
65efcfce 850#if DEBUG_PROCESS_PENDING_NODE
d62a17ae 851 unsigned int count_rib_initial = 0;
852 unsigned int count_pend_vn_initial = 0;
853 unsigned int count_pend_cost_initial = 0;
65efcfce
LB
854#endif
855
d62a17ae 856 assert(pn);
26a3ffd6
DS
857 p = agg_node_get_prefix(pn);
858 vnc_zlog_debug_verbose("%s: afi=%d, %pRN pn->info=%p", __func__, afi,
859 pn, pn->info);
65efcfce 860
d62a17ae 861 if (AFI_L2VPN != afi) {
26a3ffd6 862 rfapiQprefix2Rprefix(p, &hp);
d62a17ae 863 }
65efcfce 864
d62a17ae 865 RFAPI_RIB_CHECK_COUNTS(1, 0);
65efcfce 866
d62a17ae 867 /*
868 * Find corresponding RIB node
869 */
26a3ffd6 870 rn = agg_node_get(rfd->rib[afi], p); /* locks rn */
65efcfce 871
d62a17ae 872 /*
873 * RIB skiplist has key=rfapi_addr={vn,un}, val = rfapi_info,
874 * skiplist.del = NULL
875 */
876 slRibPt = (struct skiplist *)rn->info;
877 if (slRibPt)
878 rib_node_started_nonempty = 1;
65efcfce 879
d62a17ae 880 slPendPt = (struct skiplist *)(pn->aggregate);
881 lPendCost = (struct list *)(pn->info);
65efcfce
LB
882
883#if DEBUG_PROCESS_PENDING_NODE
d62a17ae 884 /* debugging */
885 if (slRibPt)
886 count_rib_initial = skiplist_count(slRibPt);
65efcfce 887
d62a17ae 888 if (slPendPt)
889 count_pend_vn_initial = skiplist_count(slPendPt);
65efcfce 890
d62a17ae 891 if (lPendCost && lPendCost != (struct list *)1)
892 count_pend_cost_initial = lPendCost->count;
65efcfce
LB
893#endif
894
895
d62a17ae 896 /*
897 * Handle special case: delete all routes at prefix
898 */
899 if (lPendCost == (struct list *)1) {
900 vnc_zlog_debug_verbose("%s: lPendCost=1 => delete all",
901 __func__);
902 if (slRibPt && !skiplist_empty(slRibPt)) {
903 delete_list = list_new();
904 while (0
905 == skiplist_first(slRibPt, NULL, (void **)&ri)) {
d62a17ae 906 listnode_add(delete_list, ri);
907 vnc_zlog_debug_verbose(
908 "%s: after listnode_add, delete_list->count=%d",
909 __func__, delete_list->count);
910 rfapiFreeBgpTeaOptionChain(ri->tea_options);
911 ri->tea_options = NULL;
912
913 if (ri->timer) {
914 struct rfapi_rib_tcb *tcb;
915
c3aaa89a
DS
916 tcb = THREAD_ARG(ri->timer);
917 THREAD_OFF(ri->timer);
d62a17ae 918 XFREE(MTYPE_RFAPI_RECENT_DELETE, tcb);
d62a17ae 919 }
920
d62a17ae 921 vnc_zlog_debug_verbose(
2dbe669b
DA
922 "%s: put dl pfx=%pRN vn=%pFX un=%pFX cost=%d life=%d vn_options=%p",
923 __func__, pn, &ri->rk.vn, &ri->un,
924 ri->cost, ri->lifetime, ri->vn_options);
d62a17ae 925
926 skiplist_delete_first(slRibPt);
927 }
928
929 assert(skiplist_empty(slRibPt));
930
931 skiplist_free(slRibPt);
932 rn->info = slRibPt = NULL;
fe08ba7e 933 agg_unlock_node(rn);
d62a17ae 934
935 lPendCost = pn->info = NULL;
fe08ba7e 936 agg_unlock_node(pn);
d62a17ae 937
938 goto callback;
939 }
940 if (slRibPt) {
941 skiplist_free(slRibPt);
942 rn->info = NULL;
fe08ba7e 943 agg_unlock_node(rn);
d62a17ae 944 }
945
946 assert(!slPendPt);
947 if (slPendPt) { /* TBD I think we can toss this block */
948 skiplist_free(slPendPt);
949 pn->aggregate = NULL;
fe08ba7e 950 agg_unlock_node(pn);
d62a17ae 951 }
952
953 pn->info = NULL;
fe08ba7e 954 agg_unlock_node(pn);
d62a17ae 955
fe08ba7e 956 agg_unlock_node(rn); /* agg_node_get() */
d62a17ae 957
958 if (rib_node_started_nonempty) {
959 RFAPI_RIB_PREFIX_COUNT_DECR(rfd, bgp->rfapi);
960 }
961
962 RFAPI_RIB_CHECK_COUNTS(1, 0);
963
964 return;
965 }
966
967 vnc_zlog_debug_verbose("%s: lPendCost->count=%d, slRibPt->count=%d",
968 __func__,
969 (lPendCost ? (int)lPendCost->count : -1),
970 (slRibPt ? (int)slRibPt->count : -1));
971
972 /*
973 * Iterate over routes at RIB Node.
974 * If not found at Pending Node, delete from RIB Node and add to
975 * deletelist
976 * If found at Pending Node
977 * If identical rfapi_info, delete from Pending Node
978 */
979 if (slRibPt) {
980 void *cursor = NULL;
981 struct rfapi_info *ori;
982
983 /*
984 * Iterate over RIB List
985 *
986 */
987 while (!skiplist_next(slRibPt, NULL, (void **)&ori, &cursor)) {
988
989 if (skiplist_search(slPendPt, &ori->rk, (void **)&ri)) {
990 /*
991 * Not in Pending list, so it should be deleted
992 */
993 if (!delete_list)
994 delete_list = list_new();
995 listnode_add(delete_list, ori);
996 rfapiFreeBgpTeaOptionChain(ori->tea_options);
997 ori->tea_options = NULL;
998 if (ori->timer) {
999 struct rfapi_rib_tcb *tcb;
1000
c3aaa89a
DS
1001 tcb = THREAD_ARG(ori->timer);
1002 THREAD_OFF(ori->timer);
d62a17ae 1003 XFREE(MTYPE_RFAPI_RECENT_DELETE, tcb);
d62a17ae 1004 }
65efcfce
LB
1005
1006#if DEBUG_PROCESS_PENDING_NODE
d62a17ae 1007 /* deleted from slRibPt below, after we're done
1008 * iterating */
1009 vnc_zlog_debug_verbose(
1010 "%s: slRibPt ri %p not matched in pending list, delete",
1011 __func__, ori);
65efcfce
LB
1012#endif
1013
d62a17ae 1014 } else {
1015 /*
1016 * Found in pending list. If same lifetime,
1017 * cost, options,
1018 * then remove from pending list because the
1019 * route
1020 * hasn't changed.
1021 */
1022 if (!rfapi_info_cmp(ori, ri)) {
1023 skiplist_delete(slPendPt, &ri->rk,
1024 NULL);
1025 assert(lPendCost);
1026 if (lPendCost) {
1027 /* linear walk: might need
1028 * optimization */
1029 listnode_delete(lPendCost,
1030 ri); /* XXX
1031 doesn't
1032 free
1033 data!
1034 bug? */
1035 rfapi_info_free(
1036 ri); /* grr... */
1037 }
1038 }
65efcfce 1039#if DEBUG_PROCESS_PENDING_NODE
d62a17ae 1040 vnc_zlog_debug_verbose(
1041 "%s: slRibPt ri %p matched in pending list, %s",
1042 __func__, ori,
1043 (same ? "same info"
1044 : "different info"));
65efcfce 1045#endif
d62a17ae 1046 }
1047 }
1048 /*
1049 * Go back and delete items from RIB
1050 */
1051 if (delete_list) {
1052 for (ALL_LIST_ELEMENTS_RO(delete_list, node, ri)) {
1053 vnc_zlog_debug_verbose(
1054 "%s: deleting ri %p from slRibPt",
1055 __func__, ri);
1056 assert(!skiplist_delete(slRibPt, &ri->rk,
1057 NULL));
1058 }
1059 if (skiplist_empty(slRibPt)) {
1060 skiplist_free(slRibPt);
1061 slRibPt = rn->info = NULL;
fe08ba7e 1062 agg_unlock_node(rn);
d62a17ae 1063 }
1064 }
1065 }
1066
1067 RFAPI_RIB_CHECK_COUNTS(0, (delete_list ? delete_list->count : 0));
1068
1069 /*
1070 * Iterate over routes at Pending Node
1071 *
1072 * If {vn} found at RIB Node, update RIB Node route contents to match PN
1073 * If {vn} NOT found at RIB Node, add copy to RIB Node
1074 */
1075 if (lPendCost) {
1076 for (ALL_LIST_ELEMENTS_RO(lPendCost, node, ri)) {
1077
1078 struct rfapi_info *ori;
1079
1080 if (slRibPt
1081 && !skiplist_search(slRibPt, &ri->rk,
1082 (void **)&ori)) {
1083
1084 /* found: update contents of existing route in
1085 * RIB */
1086 ori->un = ri->un;
1087 ori->cost = ri->cost;
1088 ori->lifetime = ri->lifetime;
1089 rfapiFreeBgpTeaOptionChain(ori->tea_options);
1090 ori->tea_options =
1091 rfapiOptionsDup(ri->tea_options);
98b7e975 1092 ori->last_sent_time = monotime(NULL);
d62a17ae 1093
1094 rfapiFreeRfapiVnOptionChain(ori->vn_options);
1095 ori->vn_options =
1096 rfapiVnOptionsDup(ri->vn_options);
1097
1098 rfapiFreeRfapiUnOptionChain(ori->un_options);
1099 ori->un_options =
1100 rfapiUnOptionsDup(ri->un_options);
1101
1102 vnc_zlog_debug_verbose(
1103 "%s: matched lPendCost item %p in slRibPt, rewrote",
1104 __func__, ri);
1105
1106 } else {
d62a17ae 1107 /* not found: add new route to RIB */
1108 ori = rfapi_info_new();
1109 ori->rk = ri->rk;
1110 ori->un = ri->un;
1111 ori->cost = ri->cost;
1112 ori->lifetime = ri->lifetime;
1113 ori->tea_options =
1114 rfapiOptionsDup(ri->tea_options);
98b7e975 1115 ori->last_sent_time = monotime(NULL);
d62a17ae 1116 ori->vn_options =
1117 rfapiVnOptionsDup(ri->vn_options);
1118 ori->un_options =
1119 rfapiUnOptionsDup(ri->un_options);
1120
1121 if (!slRibPt) {
1122 slRibPt = skiplist_new(
1123 0, rfapi_rib_key_cmp, NULL);
1124 rn->info = slRibPt;
fe08ba7e 1125 agg_lock_node(rn);
d62a17ae 1126 }
1127 skiplist_insert(slRibPt, &ori->rk, ori);
65efcfce 1128
d62a17ae 1129 vnc_zlog_debug_verbose(
c4f64ea9
DA
1130 "%s: nomatch lPendCost item %p in slRibPt, added (rd=%pRD)",
1131 __func__, ri, &ori->rk.rd);
d62a17ae 1132 }
1133
1134 /*
1135 * poke timer
1136 */
1137 RFAPI_RIB_CHECK_COUNTS(
1138 0, (delete_list ? delete_list->count : 0));
1139 rfapiRibStartTimer(rfd, ori, rn, 0);
1140 RFAPI_RIB_CHECK_COUNTS(
1141 0, (delete_list ? delete_list->count : 0));
1142 }
1143 }
65efcfce
LB
1144
1145
1146callback:
d62a17ae 1147 /*
1148 * Construct NHL as concatenation of pending list + delete list
1149 */
1150
1151
1152 RFAPI_RIB_CHECK_COUNTS(0, (delete_list ? delete_list->count : 0));
1153
1154 if (lPendCost) {
1155
1156 char buf[BUFSIZ];
1157 char buf2[BUFSIZ];
1158
1159 vnc_zlog_debug_verbose("%s: lPendCost->count now %d", __func__,
1160 lPendCost->count);
26a3ffd6 1161 vnc_zlog_debug_verbose("%s: For prefix %pRN (a)", __func__, pn);
d62a17ae 1162 printedprefix = 1;
1163
1164 for (ALL_LIST_ELEMENTS(lPendCost, node, nnode, ri)) {
1165
1166 struct rfapi_next_hop_entry *new;
fe08ba7e 1167 struct agg_node *trn;
d62a17ae 1168
1169 new = XCALLOC(MTYPE_RFAPI_NEXTHOP,
1170 sizeof(struct rfapi_next_hop_entry));
d62a17ae 1171
1172 if (ri->rk.aux_prefix.family) {
1173 rfapiQprefix2Rprefix(&ri->rk.aux_prefix,
1174 &new->prefix);
1175 } else {
1176 new->prefix = hp;
1177 if (AFI_L2VPN == afi) {
1178 /* hp is 0; need to set length to match
1179 * AF of vn */
1180 new->prefix.length =
1181 (ri->rk.vn.family == AF_INET)
1182 ? 32
1183 : 128;
1184 }
1185 }
1186 new->prefix.cost = ri->cost;
1187 new->lifetime = ri->lifetime;
1188 rfapiQprefix2Raddr(&ri->rk.vn, &new->vn_address);
1189 rfapiQprefix2Raddr(&ri->un, &new->un_address);
1190 /* free option chain from ri */
1191 rfapiFreeBgpTeaOptionChain(ri->tea_options);
1192
1193 ri->tea_options =
1194 NULL; /* option chain was transferred to NHL */
1195
1196 new->vn_options = ri->vn_options;
1197 ri->vn_options =
1198 NULL; /* option chain was transferred to NHL */
1199
1200 new->un_options = ri->un_options;
1201 ri->un_options =
1202 NULL; /* option chain was transferred to NHL */
1203
1204 if (*tail)
1205 (*tail)->next = new;
1206 *tail = new;
1207 if (!*head) {
1208 *head = new;
1209 }
1210 sendingsomeroutes = 1;
1211
1212 ++rfd->stat_count_nh_reachable;
1213 ++bgp->rfapi->stat.count_updated_response_updates;
1214
1215 /*
1216 * update this NVE's timestamp for this prefix
1217 */
fe08ba7e 1218 trn = agg_node_get(rfd->rsp_times[afi],
26a3ffd6 1219 p); /* locks trn */
083ec940 1220 trn->info = (void *)(uintptr_t)monotime(NULL);
c10e14e9 1221 if (agg_node_get_lock_count(trn) > 1)
fe08ba7e 1222 agg_unlock_node(trn);
d62a17ae 1223
1224 rfapiRfapiIpAddr2Str(&new->vn_address, buf, BUFSIZ);
1225 rfapiRfapiIpAddr2Str(&new->un_address, buf2, BUFSIZ);
1226 vnc_zlog_debug_verbose(
1227 "%s: add vn=%s un=%s cost=%d life=%d",
1228 __func__, buf, buf2, new->prefix.cost,
1229 new->lifetime);
1230 }
1231 }
1232
1233 RFAPI_RIB_CHECK_COUNTS(0, (delete_list ? delete_list->count : 0));
1234
1235 if (delete_list) {
1236
1237 char buf[BUFSIZ];
1238 char buf2[BUFSIZ];
1239
1240 if (!printedprefix) {
26a3ffd6
DS
1241 vnc_zlog_debug_verbose("%s: For prefix %pRN (d)",
1242 __func__, pn);
d62a17ae 1243 }
1244 vnc_zlog_debug_verbose("%s: delete_list has %d elements",
1245 __func__, delete_list->count);
1246
1247 RFAPI_RIB_CHECK_COUNTS(0, delete_list->count);
1248 if (!CHECK_FLAG(bgp->rfapi_cfg->flags,
1249 BGP_VNC_CONFIG_RESPONSE_REMOVAL_DISABLE)) {
1250
1251 for (ALL_LIST_ELEMENTS(delete_list, node, nnode, ri)) {
1252
1253 struct rfapi_next_hop_entry *new;
1254 struct rfapi_info *ri_del;
1255
1256 RFAPI_RIB_CHECK_COUNTS(0, delete_list->count);
1257 new = XCALLOC(
1258 MTYPE_RFAPI_NEXTHOP,
1259 sizeof(struct rfapi_next_hop_entry));
d62a17ae 1260
1261 if (ri->rk.aux_prefix.family) {
1262 rfapiQprefix2Rprefix(&ri->rk.aux_prefix,
1263 &new->prefix);
1264 } else {
1265 new->prefix = hp;
1266 if (AFI_L2VPN == afi) {
1267 /* hp is 0; need to set length
1268 * to match AF of vn */
1269 new->prefix.length =
1270 (ri->rk.vn.family
1271 == AF_INET)
1272 ? 32
1273 : 128;
1274 }
1275 }
1276
1277 new->prefix.cost = ri->cost;
1278 new->lifetime = RFAPI_REMOVE_RESPONSE_LIFETIME;
1279 rfapiQprefix2Raddr(&ri->rk.vn,
1280 &new->vn_address);
1281 rfapiQprefix2Raddr(&ri->un, &new->un_address);
1282
1283 new->vn_options = ri->vn_options;
1284 ri->vn_options = NULL; /* option chain was
1285 transferred to NHL */
1286
1287 new->un_options = ri->un_options;
1288 ri->un_options = NULL; /* option chain was
1289 transferred to NHL */
1290
1291 if (*tail)
1292 (*tail)->next = new;
1293 *tail = new;
1294 if (!*head) {
1295 *head = new;
1296 }
1297 ++rfd->stat_count_nh_removal;
1298 ++bgp->rfapi->stat
1299 .count_updated_response_deletes;
1300
1301 rfapiRfapiIpAddr2Str(&new->vn_address, buf,
1302 BUFSIZ);
1303 rfapiRfapiIpAddr2Str(&new->un_address, buf2,
1304 BUFSIZ);
1305 vnc_zlog_debug_verbose(
1306 "%s: DEL vn=%s un=%s cost=%d life=%d",
1307 __func__, buf, buf2, new->prefix.cost,
1308 new->lifetime);
1309
1310 RFAPI_RIB_CHECK_COUNTS(0, delete_list->count);
1311 /*
1312 * Update/add to list of recent deletions at
1313 * this prefix
1314 */
1315 if (!rn->aggregate) {
1316 rn->aggregate = skiplist_new(
1317 0, rfapi_rib_key_cmp,
1318 (void (*)(void *))
1319 rfapi_info_free);
fe08ba7e 1320 agg_lock_node(rn);
d62a17ae 1321 }
1322 RFAPI_RIB_CHECK_COUNTS(0, delete_list->count);
1323
1324 /* sanity check lifetime */
1325 if (ri->lifetime
1326 > RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY)
1327 ri->lifetime =
1328 RFAPI_LIFETIME_INFINITE_WITHDRAW_DELAY;
1329
1330 RFAPI_RIB_CHECK_COUNTS(0, delete_list->count);
1331 /* cancel normal expire timer */
1332 if (ri->timer) {
1333 struct rfapi_rib_tcb *tcb;
1334
c3aaa89a
DS
1335 tcb = THREAD_ARG(ri->timer);
1336 THREAD_OFF(ri->timer);
d62a17ae 1337 XFREE(MTYPE_RFAPI_RECENT_DELETE, tcb);
d62a17ae 1338 }
1339 RFAPI_RIB_CHECK_COUNTS(0, delete_list->count);
1340
1341 /*
1342 * Look in "recently-deleted" list
1343 */
1344 if (skiplist_search(
1345 (struct skiplist *)(rn->aggregate),
1346 &ri->rk, (void **)&ri_del)) {
1347
1348 int rc;
1349
1350 RFAPI_RIB_CHECK_COUNTS(
1351 0, delete_list->count);
1352 /*
1353 * NOT in "recently-deleted" list
1354 */
1355 list_delete_node(
1356 delete_list,
1357 node); /* does not free ri */
1358 rc = skiplist_insert(
1359 (struct skiplist
1360 *)(rn->aggregate),
1361 &ri->rk, ri);
1362 assert(!rc);
1363
1364 RFAPI_RIB_CHECK_COUNTS(
1365 0, delete_list->count);
1366 rfapiRibStartTimer(rfd, ri, rn, 1);
1367 RFAPI_RIB_CHECK_COUNTS(
1368 0, delete_list->count);
98b7e975 1369 ri->last_sent_time = monotime(NULL);
65efcfce 1370#if DEBUG_RIB_SL_RD
c4f64ea9
DA
1371 vnc_zlog_debug_verbose(
1372 "%s: move route to recently deleted list, rd=%pRD",
1373 __func__, &ri->rk.rd);
65efcfce
LB
1374#endif
1375
d62a17ae 1376 } else {
1377 /*
1378 * IN "recently-deleted" list
1379 */
1380 RFAPI_RIB_CHECK_COUNTS(
1381 0, delete_list->count);
1382 rfapiRibStartTimer(rfd, ri_del, rn, 1);
1383 RFAPI_RIB_CHECK_COUNTS(
1384 0, delete_list->count);
98b7e975 1385 ri->last_sent_time = monotime(NULL);
d62a17ae 1386 }
1387 }
1388 } else {
1389 vnc_zlog_debug_verbose(
1390 "%s: response removal disabled, omitting removals",
1391 __func__);
1392 }
1393
1394 delete_list->del = (void (*)(void *))rfapi_info_free;
6a154c88 1395 list_delete(&delete_list);
d62a17ae 1396 }
1397
1398 RFAPI_RIB_CHECK_COUNTS(0, 0);
1399
1400 /*
fe08ba7e 1401 * Reset pending lists. The final agg_unlock_node() will probably
d62a17ae 1402 * cause the pending node to be released.
1403 */
1404 if (slPendPt) {
1405 skiplist_free(slPendPt);
1406 pn->aggregate = NULL;
fe08ba7e 1407 agg_unlock_node(pn);
d62a17ae 1408 }
1409 if (lPendCost) {
6a154c88 1410 list_delete(&lPendCost);
d62a17ae 1411 pn->info = NULL;
fe08ba7e 1412 agg_unlock_node(pn);
d62a17ae 1413 }
1414 RFAPI_RIB_CHECK_COUNTS(0, 0);
1415
1416 if (rib_node_started_nonempty) {
1417 if (!rn->info) {
1418 RFAPI_RIB_PREFIX_COUNT_DECR(rfd, bgp->rfapi);
1419 }
1420 } else {
1421 if (rn->info) {
1422 RFAPI_RIB_PREFIX_COUNT_INCR(rfd, bgp->rfapi);
1423 }
1424 }
1425
1426 if (sendingsomeroutes)
26a3ffd6 1427 rfapiMonitorTimersRestart(rfd, p);
d62a17ae 1428
fe08ba7e 1429 agg_unlock_node(rn); /* agg_node_get() */
d62a17ae 1430
1431 RFAPI_RIB_CHECK_COUNTS(1, 0);
65efcfce
LB
1432}
1433
1434/*
1435 * regardless of targets, construct a single callback by doing
1436 * only one traversal of the pending RIB
1437 *
1438 *
1439 * Do callback
1440 *
1441 */
d62a17ae 1442static void rib_do_callback_onepass(struct rfapi_descriptor *rfd, afi_t afi)
65efcfce 1443{
d62a17ae 1444 struct bgp *bgp = bgp_get_default();
1445 struct rfapi_next_hop_entry *head = NULL;
1446 struct rfapi_next_hop_entry *tail = NULL;
fe08ba7e 1447 struct agg_node *rn;
65efcfce 1448
1e20238a 1449#ifdef DEBUG_L2_EXTRA
d62a17ae 1450 vnc_zlog_debug_verbose("%s: rfd=%p, afi=%d", __func__, rfd, afi);
65efcfce
LB
1451#endif
1452
d62a17ae 1453 if (!rfd->rib_pending[afi])
1454 return;
65efcfce 1455
d62a17ae 1456 assert(bgp->rfapi);
65efcfce 1457
fe08ba7e
DS
1458 for (rn = agg_route_top(rfd->rib_pending[afi]); rn;
1459 rn = agg_route_next(rn)) {
d62a17ae 1460 process_pending_node(bgp, rfd, afi, rn, &head, &tail);
1461 }
65efcfce 1462
d62a17ae 1463 if (head) {
1464 rfapi_response_cb_t *f;
65efcfce
LB
1465
1466#if DEBUG_NHL
d62a17ae 1467 vnc_zlog_debug_verbose("%s: response callback NHL follows:",
1468 __func__);
1469 rfapiPrintNhl(NULL, head);
65efcfce
LB
1470#endif
1471
d62a17ae 1472 if (rfd->response_cb)
1473 f = rfd->response_cb;
1474 else
1475 f = bgp->rfapi->rfp_methods.response_cb;
1476
1477 bgp->rfapi->flags |= RFAPI_INCALLBACK;
1478 vnc_zlog_debug_verbose("%s: invoking updated response callback",
1479 __func__);
1480 (*f)(head, rfd->cookie);
1481 bgp->rfapi->flags &= ~RFAPI_INCALLBACK;
1482 ++bgp->rfapi->response_updated_count;
1483 }
65efcfce
LB
1484}
1485
d62a17ae 1486static wq_item_status rfapiRibDoQueuedCallback(struct work_queue *wq,
1487 void *data)
65efcfce 1488{
d62a17ae 1489 struct rfapi_descriptor *rfd;
1490 afi_t afi;
1491 uint32_t queued_flag;
65efcfce 1492
d62a17ae 1493 RFAPI_RIB_CHECK_COUNTS(1, 0);
65efcfce 1494
d62a17ae 1495 rfd = ((struct rfapi_updated_responses_queue *)data)->rfd;
1496 afi = ((struct rfapi_updated_responses_queue *)data)->afi;
65efcfce 1497
d62a17ae 1498 /* Make sure the HD wasn't closed after the work item was scheduled */
1499 if (rfapi_check(rfd))
1500 return WQ_SUCCESS;
65efcfce 1501
d62a17ae 1502 rib_do_callback_onepass(rfd, afi);
65efcfce 1503
d62a17ae 1504 queued_flag = RFAPI_QUEUED_FLAG(afi);
65efcfce 1505
d62a17ae 1506 UNSET_FLAG(rfd->flags, queued_flag);
65efcfce 1507
d62a17ae 1508 RFAPI_RIB_CHECK_COUNTS(1, 0);
65efcfce 1509
d62a17ae 1510 return WQ_SUCCESS;
65efcfce
LB
1511}
1512
d62a17ae 1513static void rfapiRibQueueItemDelete(struct work_queue *wq, void *data)
65efcfce 1514{
d62a17ae 1515 XFREE(MTYPE_RFAPI_UPDATED_RESPONSE_QUEUE, data);
65efcfce
LB
1516}
1517
d62a17ae 1518static void updated_responses_queue_init(struct rfapi_descriptor *rfd)
65efcfce 1519{
d62a17ae 1520 if (rfd->updated_responses_queue)
1521 return;
1522
1523 rfd->updated_responses_queue =
1524 work_queue_new(bm->master, "rfapi updated responses");
1525 assert(rfd->updated_responses_queue);
1526
1527 rfd->updated_responses_queue->spec.workfunc = rfapiRibDoQueuedCallback;
1528 rfd->updated_responses_queue->spec.del_item_data =
1529 rfapiRibQueueItemDelete;
1530 rfd->updated_responses_queue->spec.max_retries = 0;
1531 rfd->updated_responses_queue->spec.hold = 1;
65efcfce
LB
1532}
1533
1534/*
1535 * Called when an import table node is modified. Construct a
1536 * new complete nexthop list, sorted by cost (lowest first),
1537 * based on the import table node.
1538 *
1539 * Filter out duplicate nexthops (vn address). There should be
1540 * only one UN address per VN address from the point of view of
1541 * a given import table, so we can probably ignore UN addresses
1542 * while filtering.
1543 *
1544 * Based on rfapiNhlAddNodeRoutes()
1545 */
d62a17ae 1546void rfapiRibUpdatePendingNode(
1547 struct bgp *bgp, struct rfapi_descriptor *rfd,
1548 struct rfapi_import_table *it, /* needed for L2 */
fe08ba7e 1549 struct agg_node *it_node, uint32_t lifetime)
65efcfce 1550{
26a3ffd6 1551 const struct prefix *prefix;
40381db7 1552 struct bgp_path_info *bpi;
fe08ba7e 1553 struct agg_node *pn;
d62a17ae 1554 afi_t afi;
1555 uint32_t queued_flag;
1556 int count = 0;
d62a17ae 1557
1558 vnc_zlog_debug_verbose("%s: entry", __func__);
1559
1560 if (CHECK_FLAG(bgp->rfapi_cfg->flags, BGP_VNC_CONFIG_CALLBACK_DISABLE))
1561 return;
1562
1563 vnc_zlog_debug_verbose("%s: callbacks are not disabled", __func__);
1564
1565 RFAPI_RIB_CHECK_COUNTS(1, 0);
1566
26a3ffd6 1567 prefix = agg_node_get_prefix(it_node);
d62a17ae 1568 afi = family2afi(prefix->family);
2dbe669b 1569 vnc_zlog_debug_verbose("%s: prefix=%pFX", __func__, prefix);
d62a17ae 1570
fe08ba7e 1571 pn = agg_node_get(rfd->rib_pending[afi], prefix);
d62a17ae 1572 assert(pn);
1573
1574 vnc_zlog_debug_verbose("%s: pn->info=%p, pn->aggregate=%p", __func__,
1575 pn->info, pn->aggregate);
1576
1577 if (pn->aggregate) {
1578 /*
1579 * free references into the rfapi_info structures before
1580 * freeing the structures themselves
1581 */
1582 skiplist_free((struct skiplist *)(pn->aggregate));
1583 pn->aggregate = NULL;
fe08ba7e 1584 agg_unlock_node(pn); /* skiplist deleted */
d62a17ae 1585 }
1586
1587
1588 /*
1589 * free the rfapi_info structures
1590 */
1591 if (pn->info) {
1592 if (pn->info != (void *)1) {
6a154c88 1593 list_delete((struct list **)(&pn->info));
d62a17ae 1594 }
1595 pn->info = NULL;
fe08ba7e 1596 agg_unlock_node(pn); /* linklist or 1 deleted */
d62a17ae 1597 }
1598
1599 /*
40381db7 1600 * The BPIs in the import table are already sorted by cost
d62a17ae 1601 */
40381db7 1602 for (bpi = it_node->info; bpi; bpi = bpi->next) {
d62a17ae 1603
1604 struct rfapi_info *ri;
1605 struct prefix pfx_nh;
1606
40381db7 1607 if (!bpi->extra) {
d62a17ae 1608 /* shouldn't happen */
1609 /* TBD increment error stats counter */
1610 continue;
1611 }
1612
40381db7 1613 rfapiNexthop2Prefix(bpi->attr, &pfx_nh);
d62a17ae 1614
1615 /*
1616 * Omit route if nexthop is self
1617 */
1618 if (CHECK_FLAG(bgp->rfapi_cfg->flags,
1619 BGP_VNC_CONFIG_FILTER_SELF_FROM_RSP)) {
1620
1621 struct prefix pfx_vn;
1622
cbb65f5e 1623 assert(!rfapiRaddr2Qprefix(&rfd->vn_addr, &pfx_vn));
d62a17ae 1624 if (prefix_same(&pfx_vn, &pfx_nh))
1625 continue;
1626 }
1627
1628 ri = rfapi_info_new();
1629 ri->rk.vn = pfx_nh;
40381db7 1630 ri->rk.rd = bpi->extra->vnc.import.rd;
d62a17ae 1631 /*
1632 * If there is an auxiliary IP address (L2 can have it), copy it
1633 */
40381db7
DS
1634 if (bpi->extra->vnc.import.aux_prefix.family) {
1635 ri->rk.aux_prefix = bpi->extra->vnc.import.aux_prefix;
d62a17ae 1636 }
1637
40381db7 1638 if (rfapiGetUnAddrOfVpnBi(bpi, &ri->un)) {
d62a17ae 1639 rfapi_info_free(ri);
1640 continue;
1641 }
1642
1643 if (!pn->aggregate) {
1644 pn->aggregate =
1645 skiplist_new(0, rfapi_rib_key_cmp, NULL);
fe08ba7e 1646 agg_lock_node(pn);
d62a17ae 1647 }
1648
1649 /*
1650 * If we have already added this nexthop, the insert will fail.
1651 * Note that the skiplist key is a pointer INTO the rfapi_info
1652 * structure which will be added to the "info" list.
1653 * The skiplist entry VALUE is not used for anything but
1654 * might be useful during debugging.
1655 */
1656 if (skiplist_insert((struct skiplist *)pn->aggregate, &ri->rk,
1657 ri)) {
1658
1659 /*
1660 * duplicate
1661 */
1662 rfapi_info_free(ri);
1663 continue;
1664 }
1665
40381db7 1666 rfapiRibBi2Ri(bpi, ri, lifetime);
d62a17ae 1667
1668 if (!pn->info) {
1669 pn->info = list_new();
1670 ((struct list *)(pn->info))->del =
1671 (void (*)(void *))rfapi_info_free;
fe08ba7e 1672 agg_lock_node(pn);
d62a17ae 1673 }
1674
1675 listnode_add((struct list *)(pn->info), ri);
1676 }
1677
1678 if (pn->info) {
1679 count = ((struct list *)(pn->info))->count;
1680 }
1681
1682 if (!count) {
1683 assert(!pn->info);
1684 assert(!pn->aggregate);
1685 pn->info = (void *)1; /* magic value means this node has no
1686 routes */
fe08ba7e 1687 agg_lock_node(pn);
d62a17ae 1688 }
1689
fe08ba7e 1690 agg_unlock_node(pn); /* agg_node_get */
d62a17ae 1691
1692 queued_flag = RFAPI_QUEUED_FLAG(afi);
1693
1694 if (!CHECK_FLAG(rfd->flags, queued_flag)) {
1695
1696 struct rfapi_updated_responses_queue *urq;
1697
1698 urq = XCALLOC(MTYPE_RFAPI_UPDATED_RESPONSE_QUEUE,
1699 sizeof(struct rfapi_updated_responses_queue));
d62a17ae 1700 if (!rfd->updated_responses_queue)
1701 updated_responses_queue_init(rfd);
1702
1703 SET_FLAG(rfd->flags, queued_flag);
1704 urq->rfd = rfd;
1705 urq->afi = afi;
1706 work_queue_add(rfd->updated_responses_queue, urq);
1707 }
1708 RFAPI_RIB_CHECK_COUNTS(1, 0);
65efcfce
LB
1709}
1710
d62a17ae 1711void rfapiRibUpdatePendingNodeSubtree(
1712 struct bgp *bgp, struct rfapi_descriptor *rfd,
fe08ba7e
DS
1713 struct rfapi_import_table *it, struct agg_node *it_node,
1714 struct agg_node *omit_subtree, /* may be NULL */
d62a17ae 1715 uint32_t lifetime)
65efcfce 1716{
d62a17ae 1717 /* FIXME: need to find a better way here to work without sticking our
1718 * hands in node->link */
fe08ba7e
DS
1719 if (agg_node_left(it_node)
1720 && (agg_node_left(it_node) != omit_subtree)) {
1721 if (agg_node_left(it_node)->info)
1722 rfapiRibUpdatePendingNode(
1723 bgp, rfd, it, agg_node_left(it_node), lifetime);
1724 rfapiRibUpdatePendingNodeSubtree(bgp, rfd, it,
1725 agg_node_left(it_node),
d62a17ae 1726 omit_subtree, lifetime);
1727 }
1728
fe08ba7e
DS
1729 if (agg_node_right(it_node)
1730 && (agg_node_right(it_node) != omit_subtree)) {
1731 if (agg_node_right(it_node)->info)
d62a17ae 1732 rfapiRibUpdatePendingNode(bgp, rfd, it,
fe08ba7e
DS
1733 agg_node_right(it_node),
1734 lifetime);
1735 rfapiRibUpdatePendingNodeSubtree(bgp, rfd, it,
1736 agg_node_right(it_node),
d62a17ae 1737 omit_subtree, lifetime);
1738 }
65efcfce
LB
1739}
1740
1741/*
1742 * RETURN VALUE
1743 *
1744 * 0 allow prefix to be included in response
1745 * !0 don't allow prefix to be included in response
1746 */
d62a17ae 1747int rfapiRibFTDFilterRecentPrefix(
1748 struct rfapi_descriptor *rfd,
fe08ba7e 1749 struct agg_node *it_rn, /* import table node */
d62a17ae 1750 struct prefix *pfx_target_original) /* query target */
65efcfce 1751{
d62a17ae 1752 struct bgp *bgp = rfd->bgp;
26a3ffd6
DS
1753 const struct prefix *p = agg_node_get_prefix(it_rn);
1754 afi_t afi = family2afi(p->family);
d62a17ae 1755 time_t prefix_time;
fe08ba7e 1756 struct agg_node *trn;
d62a17ae 1757
1758 /*
1759 * Not in FTD mode, so allow prefix
1760 */
1761 if (bgp->rfapi_cfg->rfp_cfg.download_type != RFAPI_RFP_DOWNLOAD_FULL)
1762 return 0;
1763
1764 /*
1765 * TBD
1766 * This matches behavior of now-obsolete rfapiRibFTDFilterRecent(),
1767 * but we need to decide if that is correct.
1768 */
26a3ffd6 1769 if (p->family == AF_ETHERNET)
d62a17ae 1770 return 0;
65efcfce 1771
1e20238a 1772#ifdef DEBUG_FTD_FILTER_RECENT
d62a17ae 1773 {
2dbe669b
DA
1774 vnc_zlog_debug_verbose("%s: prefix %pFX", __func__,
1775 agg_node_get_prefix(it_rn));
d62a17ae 1776 }
65efcfce
LB
1777#endif
1778
d62a17ae 1779 /*
1780 * prefix covers target address, so allow prefix
1781 */
26a3ffd6 1782 if (prefix_match(p, pfx_target_original)) {
1e20238a 1783#ifdef DEBUG_FTD_FILTER_RECENT
d62a17ae 1784 vnc_zlog_debug_verbose("%s: prefix covers target, allowed",
1785 __func__);
65efcfce 1786#endif
d62a17ae 1787 return 0;
1788 }
65efcfce 1789
d62a17ae 1790 /*
1791 * check this NVE's timestamp for this prefix
1792 */
26a3ffd6 1793 trn = agg_node_get(rfd->rsp_times[afi], p); /* locks trn */
d62a17ae 1794 prefix_time = (time_t)trn->info;
c10e14e9 1795 if (agg_node_get_lock_count(trn) > 1)
fe08ba7e 1796 agg_unlock_node(trn);
65efcfce 1797
1e20238a 1798#ifdef DEBUG_FTD_FILTER_RECENT
d62a17ae 1799 vnc_zlog_debug_verbose("%s: last sent time %lu, last allowed time %lu",
1800 __func__, prefix_time,
1801 rfd->ftd_last_allowed_time);
65efcfce
LB
1802#endif
1803
d62a17ae 1804 /*
1805 * haven't sent this prefix, which doesn't cover target address,
1806 * to NVE since ftd_advertisement_interval, so OK to send now.
1807 */
1808 if (prefix_time <= rfd->ftd_last_allowed_time)
1809 return 0;
65efcfce 1810
d62a17ae 1811 return 1;
65efcfce
LB
1812}
1813
1814/*
1815 * Call when rfapi returns from rfapi_query() so the RIB reflects
1816 * the routes sent to the NVE before the first updated response
1817 *
1818 * Also: remove duplicates from response. Caller should use returned
1819 * value of nexthop chain.
1820 */
1821struct rfapi_next_hop_entry *
d62a17ae 1822rfapiRibPreload(struct bgp *bgp, struct rfapi_descriptor *rfd,
1823 struct rfapi_next_hop_entry *response, int use_eth_resolution)
65efcfce 1824{
d62a17ae 1825 struct rfapi_next_hop_entry *nhp;
1826 struct rfapi_next_hop_entry *nhp_next;
1827 struct rfapi_next_hop_entry *head = NULL;
1828 struct rfapi_next_hop_entry *tail = NULL;
1829 time_t new_last_sent_time;
1830
1831 vnc_zlog_debug_verbose("%s: loading response=%p, use_eth_resolution=%d",
1832 __func__, response, use_eth_resolution);
1833
98b7e975 1834 new_last_sent_time = monotime(NULL);
d62a17ae 1835
1836 for (nhp = response; nhp; nhp = nhp_next) {
1837
1838 struct prefix pfx;
1839 struct rfapi_rib_key rk;
1840 afi_t afi;
1841 struct rfapi_info *ri;
1842 int need_insert;
fe08ba7e 1843 struct agg_node *rn;
d62a17ae 1844 int rib_node_started_nonempty = 0;
fe08ba7e 1845 struct agg_node *trn;
d62a17ae 1846 int allowed = 0;
1847
1848 /* save in case we delete nhp */
1849 nhp_next = nhp->next;
1850
1851 if (nhp->lifetime == RFAPI_REMOVE_RESPONSE_LIFETIME) {
1852 /*
1853 * weird, shouldn't happen
1854 */
1855 vnc_zlog_debug_verbose(
1856 "%s: got nhp->lifetime == RFAPI_REMOVE_RESPONSE_LIFETIME",
1857 __func__);
1858 continue;
1859 }
1860
1861
1862 if (use_eth_resolution) {
1863 /* get the prefix of the ethernet address in the L2
1864 * option */
1865 struct rfapi_l2address_option *pL2o;
1866 struct rfapi_vn_option *vo;
1867
1868 /*
1869 * Look for VN option of type
1870 * RFAPI_VN_OPTION_TYPE_L2ADDR
1871 */
1872 for (pL2o = NULL, vo = nhp->vn_options; vo;
1873 vo = vo->next) {
1874 if (RFAPI_VN_OPTION_TYPE_L2ADDR == vo->type) {
1875 pL2o = &vo->v.l2addr;
1876 break;
1877 }
1878 }
1879
1880 if (!pL2o) {
1881 /*
1882 * not supposed to happen
1883 */
1884 vnc_zlog_debug_verbose("%s: missing L2 info",
1885 __func__);
1886 continue;
1887 }
1888
1889 afi = AFI_L2VPN;
1890 rfapiL2o2Qprefix(pL2o, &pfx);
1891 } else {
1892 rfapiRprefix2Qprefix(&nhp->prefix, &pfx);
1893 afi = family2afi(pfx.family);
1894 }
1895
1896 /*
1897 * TBD for ethernet, rib must know the right way to distinguish
1898 * duplicate routes
1899 *
1900 * Current approach: prefix is key to radix tree; then
1901 * each prefix has a set of routes with unique VN addrs
1902 */
1903
1904 /*
1905 * Look up prefix in RIB
1906 */
fe08ba7e 1907 rn = agg_node_get(rfd->rib[afi], &pfx); /* locks rn */
d62a17ae 1908
1909 if (rn->info) {
1910 rib_node_started_nonempty = 1;
1911 } else {
1912 rn->info = skiplist_new(0, rfapi_rib_key_cmp, NULL);
fe08ba7e 1913 agg_lock_node(rn);
d62a17ae 1914 }
1915
1916 /*
1917 * Look up route at prefix
1918 */
1919 need_insert = 0;
1920 memset((void *)&rk, 0, sizeof(rk));
1921 assert(!rfapiRaddr2Qprefix(&nhp->vn_address, &rk.vn));
1922
1923 if (use_eth_resolution) {
1924 /* copy what came from aux_prefix to rk.aux_prefix */
1925 rfapiRprefix2Qprefix(&nhp->prefix, &rk.aux_prefix);
1926 if (RFAPI_0_PREFIX(&rk.aux_prefix)
1927 && RFAPI_HOST_PREFIX(&rk.aux_prefix)) {
1928 /* mark as "none" if nhp->prefix is 0/32 or
1929 * 0/128 */
c7ca5ccf 1930 rk.aux_prefix.family = AF_UNSPEC;
d62a17ae 1931 }
1932 }
65efcfce
LB
1933
1934#if DEBUG_NHL
d62a17ae 1935 {
872ed4c7 1936 char str_aux_prefix[PREFIX_STRLEN];
d62a17ae 1937
d62a17ae 1938 str_aux_prefix[0] = 0;
1939
872ed4c7
DS
1940 prefix2str(&rk.aux_prefix, str_aux_prefix,
1941 sizeof(str_aux_prefix));
d62a17ae 1942
1943 if (!rk.aux_prefix.family) {
1944 }
1945 vnc_zlog_debug_verbose(
2dbe669b
DA
1946 "%s: rk.vn=%pFX rk.aux_prefix=%s", __func__,
1947 &rk.vn,
d62a17ae 1948 (rk.aux_prefix.family ? str_aux_prefix : "-"));
1949 }
1950 vnc_zlog_debug_verbose(
1951 "%s: RIB skiplist for this prefix follows", __func__);
b54892e0
DS
1952 rfapiRibShowRibSl(NULL, agg_node_get_prefix(rn),
1953 (struct skiplist *)rn->info);
65efcfce
LB
1954#endif
1955
1956
d62a17ae 1957 if (!skiplist_search((struct skiplist *)rn->info, &rk,
1958 (void **)&ri)) {
1959 /*
1960 * Already have this route; make values match
1961 */
1962 rfapiFreeRfapiUnOptionChain(ri->un_options);
1963 ri->un_options = NULL;
1964 rfapiFreeRfapiVnOptionChain(ri->vn_options);
1965 ri->vn_options = NULL;
65efcfce
LB
1966
1967#if DEBUG_NHL
d62a17ae 1968 vnc_zlog_debug_verbose("%s: found in RIB", __func__);
65efcfce
LB
1969#endif
1970
d62a17ae 1971 /*
1972 * Filter duplicate routes from initial response.
1973 * Check timestamps to avoid wraparound problems
1974 */
1975 if ((ri->rsp_counter != rfd->rsp_counter)
1976 || (ri->last_sent_time != new_last_sent_time)) {
65efcfce
LB
1977
1978#if DEBUG_NHL
d62a17ae 1979 vnc_zlog_debug_verbose(
1980 "%s: allowed due to counter/timestamp diff",
1981 __func__);
65efcfce 1982#endif
d62a17ae 1983 allowed = 1;
1984 }
65efcfce 1985
d62a17ae 1986 } else {
65efcfce
LB
1987
1988#if DEBUG_NHL
d62a17ae 1989 vnc_zlog_debug_verbose(
1990 "%s: allowed due to not yet in RIB", __func__);
65efcfce 1991#endif
d62a17ae 1992 /* not found: add new route to RIB */
1993 ri = rfapi_info_new();
1994 need_insert = 1;
1995 allowed = 1;
1996 }
1997
1998 ri->rk = rk;
1999 assert(!rfapiRaddr2Qprefix(&nhp->un_address, &ri->un));
2000 ri->cost = nhp->prefix.cost;
2001 ri->lifetime = nhp->lifetime;
2002 ri->vn_options = rfapiVnOptionsDup(nhp->vn_options);
2003 ri->rsp_counter = rfd->rsp_counter;
98b7e975 2004 ri->last_sent_time = monotime(NULL);
d62a17ae 2005
2006 if (need_insert) {
2007 int rc;
2008 rc = skiplist_insert((struct skiplist *)rn->info,
2009 &ri->rk, ri);
2010 assert(!rc);
2011 }
2012
2013 if (!rib_node_started_nonempty) {
2014 RFAPI_RIB_PREFIX_COUNT_INCR(rfd, bgp->rfapi);
2015 }
2016
2017 RFAPI_RIB_CHECK_COUNTS(0, 0);
2018 rfapiRibStartTimer(rfd, ri, rn, 0);
2019 RFAPI_RIB_CHECK_COUNTS(0, 0);
2020
fe08ba7e 2021 agg_unlock_node(rn);
d62a17ae 2022
2023 /*
2024 * update this NVE's timestamp for this prefix
2025 */
fe08ba7e 2026 trn = agg_node_get(rfd->rsp_times[afi], &pfx); /* locks trn */
083ec940 2027 trn->info = (void *)(uintptr_t)monotime(NULL);
c10e14e9 2028 if (agg_node_get_lock_count(trn) > 1)
fe08ba7e 2029 agg_unlock_node(trn);
d62a17ae 2030
2dbe669b
DA
2031 vnc_zlog_debug_verbose(
2032 "%s: added pfx=%pFX nh[vn]=%pFX, cost=%u, lifetime=%u, allowed=%d",
2033 __func__, &pfx, &rk.vn, nhp->prefix.cost, nhp->lifetime,
2034 allowed);
d62a17ae 2035
2036 if (allowed) {
2037 if (tail)
2038 (tail)->next = nhp;
2039 tail = nhp;
2040 if (!head) {
2041 head = nhp;
2042 }
2043 } else {
2044 rfapi_un_options_free(nhp->un_options);
2045 nhp->un_options = NULL;
2046 rfapi_vn_options_free(nhp->vn_options);
2047 nhp->vn_options = NULL;
2048
2049 XFREE(MTYPE_RFAPI_NEXTHOP, nhp);
d62a17ae 2050 }
2051 }
2052
2053 if (tail)
2054 tail->next = NULL;
2055 return head;
65efcfce
LB
2056}
2057
d62a17ae 2058void rfapiRibPendingDeleteRoute(struct bgp *bgp, struct rfapi_import_table *it,
fe08ba7e 2059 afi_t afi, struct agg_node *it_node)
65efcfce 2060{
d62a17ae 2061 struct rfapi_descriptor *rfd;
2062 struct listnode *node;
26a3ffd6 2063 const struct prefix *p = agg_node_get_prefix(it_node);
d62a17ae 2064
26a3ffd6
DS
2065 vnc_zlog_debug_verbose("%s: entry, it=%p, afi=%d, it_node=%p, pfx=%pRN",
2066 __func__, it, afi, it_node, it_node);
d62a17ae 2067
2068 if (AFI_L2VPN == afi) {
2069 /*
2070 * ethernet import tables are per-LNI and each ethernet monitor
2071 * identifies the rfd that owns it.
2072 */
2073 struct rfapi_monitor_eth *m;
fe08ba7e 2074 struct agg_node *rn;
d62a17ae 2075 struct skiplist *sl;
2076 void *cursor;
2077 int rc;
2078
2079 /*
2080 * route-specific monitors
2081 */
2082 if ((sl = RFAPI_MONITOR_ETH(it_node))) {
2083
2084 vnc_zlog_debug_verbose(
2085 "%s: route-specific skiplist: %p", __func__,
2086 sl);
2087
2088 for (cursor = NULL,
8998807f 2089 rc = skiplist_next(sl, NULL, (void **)&m, &cursor);
d62a17ae 2090 !rc; rc = skiplist_next(sl, NULL, (void **)&m,
8998807f 2091 &cursor)) {
65efcfce
LB
2092
2093#if DEBUG_PENDING_DELETE_ROUTE
d62a17ae 2094 vnc_zlog_debug_verbose("%s: eth monitor rfd=%p",
2095 __func__, m->rfd);
65efcfce 2096#endif
d62a17ae 2097 /*
2098 * If we have already sent a route with this
2099 * prefix to this
2100 * NVE, it's OK to send an update with the
2101 * delete
2102 */
fe08ba7e 2103 if ((rn = agg_node_lookup(m->rfd->rib[afi],
26a3ffd6 2104 p))) {
d62a17ae 2105 rfapiRibUpdatePendingNode(
2106 bgp, m->rfd, it, it_node,
2107 m->rfd->response_lifetime);
fe08ba7e 2108 agg_unlock_node(rn);
d62a17ae 2109 }
2110 }
2111 }
2112
2113 /*
2114 * all-routes/FTD monitors
2115 */
2116 for (m = it->eth0_queries; m; m = m->next) {
65efcfce 2117#if DEBUG_PENDING_DELETE_ROUTE
d62a17ae 2118 vnc_zlog_debug_verbose("%s: eth0 monitor rfd=%p",
2119 __func__, m->rfd);
65efcfce 2120#endif
d62a17ae 2121 /*
2122 * If we have already sent a route with this prefix to
2123 * this
2124 * NVE, it's OK to send an update with the delete
2125 */
26a3ffd6 2126 if ((rn = agg_node_lookup(m->rfd->rib[afi], p))) {
d62a17ae 2127 rfapiRibUpdatePendingNode(
2128 bgp, m->rfd, it, it_node,
2129 m->rfd->response_lifetime);
94f7f37d 2130 agg_unlock_node(rn);
d62a17ae 2131 }
2132 }
2133
2134 } else {
2135 /*
2136 * Find RFDs that reference this import table
2137 */
2138 for (ALL_LIST_ELEMENTS_RO(&bgp->rfapi->descriptors, node,
2139 rfd)) {
2140
fe08ba7e 2141 struct agg_node *rn;
d62a17ae 2142
2143 vnc_zlog_debug_verbose(
2144 "%s: comparing rfd(%p)->import_table=%p to it=%p",
2145 __func__, rfd, rfd->import_table, it);
2146
2147 if (rfd->import_table != it)
2148 continue;
2149
2150 vnc_zlog_debug_verbose("%s: matched rfd %p", __func__,
2151 rfd);
2152
2153 /*
2154 * If we have sent a response to this NVE with this
2155 * prefix
2156 * previously, we should send an updated response.
2157 */
26a3ffd6 2158 if ((rn = agg_node_lookup(rfd->rib[afi], p))) {
d62a17ae 2159 rfapiRibUpdatePendingNode(
2160 bgp, rfd, it, it_node,
2161 rfd->response_lifetime);
fe08ba7e 2162 agg_unlock_node(rn);
d62a17ae 2163 }
2164 }
2165 }
65efcfce
LB
2166}
2167
d62a17ae 2168void rfapiRibShowResponsesSummary(void *stream)
65efcfce 2169{
d62a17ae 2170 int (*fp)(void *, const char *, ...);
2171 struct vty *vty;
2172 void *out;
2173 const char *vty_newline;
2174 struct bgp *bgp = bgp_get_default();
2175
2176 int nves = 0;
2177 int nves_with_nonempty_ribs = 0;
2178 struct rfapi_descriptor *rfd;
2179 struct listnode *node;
2180
d62a17ae 2181 if (rfapiStream2Vty(stream, &fp, &vty, &out, &vty_newline) == 0)
2182 return;
9b86009a
RW
2183 if (!bgp) {
2184 fp(out, "Unable to find default BGP instance\n");
2185 return;
2186 }
d62a17ae 2187
2188 fp(out, "%-24s ", "Responses: (Prefixes)");
2189 fp(out, "%-8s %-8u ", "Active:", bgp->rfapi->rib_prefix_count_total);
2190 fp(out, "%-8s %-8u",
2191 "Maximum:", bgp->rfapi->rib_prefix_count_total_max);
2192 fp(out, "\n");
2193
2194 fp(out, "%-24s ", " (Updated)");
2195 fp(out, "%-8s %-8u ",
2196 "Update:", bgp->rfapi->stat.count_updated_response_updates);
2197 fp(out, "%-8s %-8u",
2198 "Remove:", bgp->rfapi->stat.count_updated_response_deletes);
2199 fp(out, "%-8s %-8u", "Total:",
2200 bgp->rfapi->stat.count_updated_response_updates
2201 + bgp->rfapi->stat.count_updated_response_deletes);
2202 fp(out, "\n");
2203
2204 fp(out, "%-24s ", " (NVEs)");
2205 for (ALL_LIST_ELEMENTS_RO(&bgp->rfapi->descriptors, node, rfd)) {
2206 ++nves;
2207 if (rfd->rib_prefix_count)
2208 ++nves_with_nonempty_ribs;
2209 }
2210 fp(out, "%-8s %-8u ", "Active:", nves_with_nonempty_ribs);
2211 fp(out, "%-8s %-8u", "Total:", nves);
2212 fp(out, "\n");
65efcfce
LB
2213}
2214
d62a17ae 2215void rfapiRibShowResponsesSummaryClear(void)
65efcfce 2216{
d62a17ae 2217 struct bgp *bgp = bgp_get_default();
65efcfce 2218
d62a17ae 2219 bgp->rfapi->rib_prefix_count_total_max =
2220 bgp->rfapi->rib_prefix_count_total;
65efcfce
LB
2221}
2222
d62a17ae 2223static int print_rib_sl(int (*fp)(void *, const char *, ...), struct vty *vty,
2224 void *out, struct skiplist *sl, int deleted,
2225 char *str_pfx, int *printedprefix)
65efcfce 2226{
d62a17ae 2227 struct rfapi_info *ri;
2228 int rc;
2229 void *cursor;
2230 int routes_displayed = 0;
2231
2232 cursor = NULL;
2233 for (rc = skiplist_next(sl, NULL, (void **)&ri, &cursor); !rc;
2234 rc = skiplist_next(sl, NULL, (void **)&ri, &cursor)) {
2235
872ed4c7
DS
2236 char str_vn[PREFIX_STRLEN];
2237 char str_un[PREFIX_STRLEN];
d62a17ae 2238 char str_lifetime[BUFSIZ];
2239 char str_age[BUFSIZ];
2240 char *p;
d62a17ae 2241
2242 ++routes_displayed;
2243
872ed4c7 2244 prefix2str(&ri->rk.vn, str_vn, sizeof(str_vn));
d62a17ae 2245 p = index(str_vn, '/');
2246 if (p)
2247 *p = 0;
2248
872ed4c7 2249 prefix2str(&ri->un, str_un, sizeof(str_un));
d62a17ae 2250 p = index(str_un, '/');
2251 if (p)
2252 *p = 0;
2253
2254 rfapiFormatSeconds(ri->lifetime, str_lifetime, BUFSIZ);
1e20238a 2255#ifdef RFAPI_REGISTRATIONS_REPORT_AGE
d62a17ae 2256 rfapiFormatAge(ri->last_sent_time, str_age, BUFSIZ);
65efcfce 2257#else
d62a17ae 2258 {
98b7e975 2259 time_t now = monotime(NULL);
d62a17ae 2260 time_t expire =
2261 ri->last_sent_time + (time_t)ri->lifetime;
2262 /* allow for delayed/async removal */
2263 rfapiFormatSeconds((expire > now ? expire - now : 1),
2264 str_age, BUFSIZ);
2265 }
65efcfce
LB
2266#endif
2267
c4f64ea9 2268 fp(out, " %c %-20s %-15s %-15s %-4u %-8s %-8s %pRD\n",
d62a17ae 2269 deleted ? 'r' : ' ', *printedprefix ? "" : str_pfx, str_vn,
c4f64ea9 2270 str_un, ri->cost, str_lifetime, str_age, &ri->rk.rd);
65efcfce 2271
d62a17ae 2272 if (!*printedprefix)
2273 *printedprefix = 1;
2274 }
2275 return routes_displayed;
65efcfce
LB
2276}
2277
2278#if DEBUG_NHL
2279/*
2280 * This one is for debugging (set stream to NULL to send output to log)
2281 */
d62a17ae 2282static void rfapiRibShowRibSl(void *stream, struct prefix *pfx,
2283 struct skiplist *sl)
65efcfce 2284{
d62a17ae 2285 int (*fp)(void *, const char *, ...);
2286 struct vty *vty;
2287 void *out;
2288 const char *vty_newline;
65efcfce 2289
d62a17ae 2290 int nhs_displayed = 0;
872ed4c7 2291 char str_pfx[PREFIX_STRLEN];
d62a17ae 2292 int printedprefix = 0;
65efcfce 2293
d62a17ae 2294 if (rfapiStream2Vty(stream, &fp, &vty, &out, &vty_newline) == 0)
2295 return;
65efcfce 2296
872ed4c7 2297 prefix2str(pfx, str_pfx, sizeof(str_pfx));
65efcfce 2298
d62a17ae 2299 nhs_displayed +=
2300 print_rib_sl(fp, vty, out, sl, 0, str_pfx, &printedprefix);
65efcfce
LB
2301}
2302#endif
2303
d62a17ae 2304void rfapiRibShowResponses(void *stream, struct prefix *pfx_match,
2305 int show_removed)
65efcfce 2306{
d62a17ae 2307 int (*fp)(void *, const char *, ...);
2308 struct vty *vty;
2309 void *out;
2310 const char *vty_newline;
2311
2312 struct rfapi_descriptor *rfd;
2313 struct listnode *node;
2314
2315 struct bgp *bgp = bgp_get_default();
2316 int printedheader = 0;
2317 int routes_total = 0;
2318 int nhs_total = 0;
2319 int prefixes_total = 0;
2320 int prefixes_displayed = 0;
2321 int nves_total = 0;
2322 int nves_with_routes = 0;
2323 int nves_displayed = 0;
2324 int routes_displayed = 0;
2325 int nhs_displayed = 0;
2326
2327 if (rfapiStream2Vty(stream, &fp, &vty, &out, &vty_newline) == 0)
2328 return;
9b86009a
RW
2329 if (!bgp) {
2330 fp(out, "Unable to find default BGP instance\n");
2331 return;
2332 }
2333
d62a17ae 2334 /*
2335 * loop over NVEs
2336 */
2337 for (ALL_LIST_ELEMENTS_RO(&bgp->rfapi->descriptors, node, rfd)) {
2338
2339 int printednve = 0;
2340 afi_t afi;
2341
2342 ++nves_total;
2343 if (rfd->rib_prefix_count)
2344 ++nves_with_routes;
2345
2346 for (afi = AFI_IP; afi < AFI_MAX; ++afi) {
2347
fe08ba7e 2348 struct agg_node *rn;
d62a17ae 2349
2350 if (!rfd->rib[afi])
2351 continue;
2352
fe08ba7e
DS
2353 for (rn = agg_route_top(rfd->rib[afi]); rn;
2354 rn = agg_route_next(rn)) {
26a3ffd6
DS
2355 const struct prefix *p =
2356 agg_node_get_prefix(rn);
d62a17ae 2357 struct skiplist *sl;
872ed4c7 2358 char str_pfx[PREFIX_STRLEN];
d62a17ae 2359 int printedprefix = 0;
2360
2361 if (!show_removed)
2362 sl = rn->info;
2363 else
2364 sl = rn->aggregate;
2365
2366 if (!sl)
2367 continue;
2368
2369 routes_total++;
2370 nhs_total += skiplist_count(sl);
2371 ++prefixes_total;
2372
26a3ffd6
DS
2373 if (pfx_match && !prefix_match(pfx_match, p)
2374 && !prefix_match(p, pfx_match))
d62a17ae 2375 continue;
2376
2377 ++prefixes_displayed;
2378
2379 if (!printedheader) {
2380 ++printedheader;
2381
2382 fp(out, "\n[%s]\n",
2383 show_removed ? "Removed" : "Active");
2384 fp(out, "%-15s %-15s\n", "Querying VN",
2385 "Querying UN");
2386 fp(out,
2387 " %-20s %-15s %-15s %4s %-8s %-8s\n",
2388 "Prefix", "Registered VN",
2389 "Registered UN", "Cost", "Lifetime",
1e20238a 2390#ifdef RFAPI_REGISTRATIONS_REPORT_AGE
d62a17ae 2391 "Age"
65efcfce 2392#else
d62a17ae 2393 "Remaining"
65efcfce 2394#endif
1e20238a 2395 );
d62a17ae 2396 }
2397 if (!printednve) {
2398 char str_vn[BUFSIZ];
2399 char str_un[BUFSIZ];
2400
2401 ++printednve;
2402 ++nves_displayed;
2403
2404 fp(out, "%-15s %-15s\n",
2405 rfapiRfapiIpAddr2Str(&rfd->vn_addr,
2406 str_vn, BUFSIZ),
2407 rfapiRfapiIpAddr2Str(&rfd->un_addr,
2408 str_un,
2409 BUFSIZ));
2410 }
26a3ffd6 2411 prefix2str(p, str_pfx, sizeof(str_pfx));
d62a17ae 2412 // fp(out, " %s\n", buf); /* prefix */
2413
2414 routes_displayed++;
2415 nhs_displayed += print_rib_sl(
2416 fp, vty, out, sl, show_removed, str_pfx,
2417 &printedprefix);
2418 }
2419 }
2420 }
2421
2422 if (routes_total) {
2423 fp(out, "\n");
2424 fp(out, "Displayed %u NVEs, and %u out of %u %s prefixes",
2425 nves_displayed, routes_displayed, routes_total,
2426 show_removed ? "removed" : "active");
2427 if (nhs_displayed != routes_displayed
2428 || nhs_total != routes_total)
2429 fp(out, " with %u out of %u next hops", nhs_displayed,
2430 nhs_total);
2431 fp(out, "\n");
2432 }
65efcfce 2433}