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