]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/rfapi/rfapi_monitor.c
eirgpd: fix clang warning
[mirror_frr.git] / bgpd / rfapi / rfapi_monitor.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_monitor.c
23 */
24
25/* TBD remove unneeded includes */
26
27#include <errno.h>
28
f8b6f499
LB
29#include "lib/zebra.h"
30#include "lib/prefix.h"
31#include "lib/table.h"
32#include "lib/vty.h"
33#include "lib/memory.h"
34#include "lib/log.h"
35#include "lib/table.h"
36#include "lib/skiplist.h"
37
38#include "bgpd/bgpd.h"
39
40#include "bgpd/rfapi/bgp_rfapi_cfg.h"
41#include "bgpd/rfapi/rfapi.h"
42#include "bgpd/rfapi/rfapi_backend.h"
43
44#include "bgpd/rfapi/rfapi.h"
45#include "bgpd/rfapi/rfapi_import.h"
46#include "bgpd/rfapi/vnc_import_bgp.h"
47#include "bgpd/rfapi/rfapi_private.h"
48#include "bgpd/rfapi/rfapi_monitor.h"
49#include "bgpd/rfapi/rfapi_vty.h"
50#include "bgpd/rfapi/rfapi_rib.h"
a3b55c25 51#include "bgpd/rfapi/vnc_debug.h"
65efcfce
LB
52
53#define DEBUG_L2_EXTRA 0
54#define DEBUG_DUP_CHECK 0
55#define DEBUG_ETH_SL 0
56
d62a17ae 57static void rfapiMonitorTimerRestart(struct rfapi_monitor_vpn *m);
65efcfce 58
d62a17ae 59static void rfapiMonitorEthTimerRestart(struct rfapi_monitor_eth *m);
65efcfce
LB
60
61/*
62 * Forward declarations
63 */
d62a17ae 64static void rfapiMonitorEthDetachImport(struct bgp *bgp,
65 struct rfapi_monitor_eth *mon);
65efcfce
LB
66
67#if DEBUG_ETH_SL
68/*
69 * Debug function, special case
70 */
d62a17ae 71void rfapiMonitorEthSlCheck(struct route_node *rn, const char *tag1,
72 const char *tag2)
65efcfce 73{
d62a17ae 74 struct route_node *rn_saved = NULL;
75 static struct skiplist *sl_saved = NULL;
76 struct skiplist *sl;
77
78 if (!rn)
79 return;
80
81 if (rn_saved && (rn != rn_saved))
82 return;
83
84 if (!rn_saved)
85 rn_saved = rn;
86
87 sl = RFAPI_MONITOR_ETH(rn);
88 if (sl || sl_saved) {
89 vnc_zlog_debug_verbose(
90 "%s[%s%s]: rn=%p, rn->lock=%d, old sl=%p, new sl=%p",
91 __func__, (tag1 ? tag1 : ""), (tag2 ? tag2 : ""), rn,
92 rn->lock, sl_saved, sl);
93 sl_saved = sl;
94 }
65efcfce
LB
95}
96#endif
97
98/*
99 * Debugging function that aborts when it finds monitors whose
100 * "next" pointer * references themselves
101 */
d62a17ae 102void rfapiMonitorLoopCheck(struct rfapi_monitor_vpn *mchain)
65efcfce 103{
d62a17ae 104 struct rfapi_monitor_vpn *m;
65efcfce 105
d62a17ae 106 for (m = mchain; m; m = m->next)
107 assert(m != m->next);
65efcfce
LB
108}
109
110#if DEBUG_DUP_CHECK
111/*
112 * Debugging code: see if a monitor is mentioned more than once
113 * in a HD's monitor list
114 */
d62a17ae 115void rfapiMonitorDupCheck(struct bgp *bgp)
65efcfce 116{
d62a17ae 117 struct listnode *hnode;
118 struct rfapi_descriptor *rfd;
119
120 for (ALL_LIST_ELEMENTS_RO(&bgp->rfapi->descriptors, hnode, rfd)) {
121 struct route_node *mrn;
122
123 if (!rfd->mon)
124 continue;
125
126 for (mrn = route_top(rfd->mon); mrn; mrn = route_next(mrn)) {
127 struct rfapi_monitor_vpn *m;
128 for (m = (struct rfapi_monitor_vpn *)(mrn->info); m;
129 m = m->next)
130 m->dcount = 0;
131 }
132 }
133
134 for (ALL_LIST_ELEMENTS_RO(&bgp->rfapi->descriptors, hnode, rfd)) {
135 struct route_node *mrn;
136
137 if (!rfd->mon)
138 continue;
139
140 for (mrn = route_top(rfd->mon); mrn; mrn = route_next(mrn)) {
141 struct rfapi_monitor_vpn *m;
142
143 for (m = (struct rfapi_monitor_vpn *)(mrn->info); m;
144 m = m->next)
145 assert(++m->dcount == 1);
146 }
147 }
65efcfce
LB
148}
149#endif
150
151/* debug */
d62a17ae 152void rfapiMonitorCleanCheck(struct bgp *bgp)
65efcfce 153{
d62a17ae 154 struct listnode *hnode;
155 struct rfapi_descriptor *rfd;
65efcfce 156
d62a17ae 157 for (ALL_LIST_ELEMENTS_RO(&bgp->rfapi->descriptors, hnode, rfd)) {
158 assert(!rfd->import_table->vpn0_queries[AFI_IP]);
159 assert(!rfd->import_table->vpn0_queries[AFI_IP6]);
65efcfce 160
d62a17ae 161 struct route_node *rn;
65efcfce 162
d62a17ae 163 for (rn = route_top(rfd->import_table->imported_vpn[AFI_IP]);
164 rn; rn = route_next(rn)) {
65efcfce 165
d62a17ae 166 assert(!RFAPI_MONITOR_VPN(rn));
167 }
168 for (rn = route_top(rfd->import_table->imported_vpn[AFI_IP6]);
169 rn; rn = route_next(rn)) {
65efcfce 170
d62a17ae 171 assert(!RFAPI_MONITOR_VPN(rn));
172 }
173 }
65efcfce
LB
174}
175
176/* debug */
d62a17ae 177void rfapiMonitorCheckAttachAllowed(void)
65efcfce 178{
d62a17ae 179 struct bgp *bgp = bgp_get_default();
180 assert(!(bgp->rfapi_cfg->flags & BGP_VNC_CONFIG_CALLBACK_DISABLE));
65efcfce
LB
181}
182
d62a17ae 183void rfapiMonitorExtraFlush(safi_t safi, struct route_node *rn)
65efcfce 184{
d62a17ae 185 struct rfapi_it_extra *hie;
186 struct rfapi_monitor_vpn *v;
187 struct rfapi_monitor_vpn *v_next;
188 struct rfapi_monitor_encap *e = NULL;
189 struct rfapi_monitor_encap *e_next = NULL;
190
191 if (!rn)
192 return;
193
194 if (!rn->aggregate)
195 return;
196
197 hie = (struct rfapi_it_extra *)(rn->aggregate);
198
199 switch (safi) {
200 case SAFI_ENCAP:
201 for (e = hie->u.encap.e; e; e = e_next) {
202 e_next = e->next;
203 e->next = NULL;
204 XFREE(MTYPE_RFAPI_MONITOR_ENCAP, e);
205 route_unlock_node(rn);
206 }
207 hie->u.encap.e = NULL;
208 break;
209
210 case SAFI_MPLS_VPN:
211 for (v = hie->u.vpn.v; v; v = v_next) {
212 v_next = v->next;
213 v->next = NULL;
214 XFREE(MTYPE_RFAPI_MONITOR, e);
215 route_unlock_node(rn);
216 }
217 hie->u.vpn.v = NULL;
218 if (hie->u.vpn.e.source) {
219 while (!skiplist_delete_first(hie->u.vpn.e.source)) {
220 route_unlock_node(rn);
221 }
222 skiplist_free(hie->u.vpn.e.source);
223 hie->u.vpn.e.source = NULL;
224 route_unlock_node(rn);
225 }
226 if (hie->u.vpn.idx_rd) {
227 /* looping through bi->extra->vnc.import.rd is tbd */
228 while (!skiplist_delete_first(hie->u.vpn.idx_rd)) {
229 route_unlock_node(rn);
230 }
231 skiplist_free(hie->u.vpn.idx_rd);
232 hie->u.vpn.idx_rd = NULL;
233 route_unlock_node(rn);
234 }
235 if (hie->u.vpn.mon_eth) {
236 while (!skiplist_delete_first(hie->u.vpn.mon_eth)) {
237 route_unlock_node(rn);
238 }
239 skiplist_free(hie->u.vpn.mon_eth);
240 hie->u.vpn.mon_eth = NULL;
241 route_unlock_node(rn);
242 }
243 break;
244
245 default:
246 assert(0);
247 }
248 XFREE(MTYPE_RFAPI_IT_EXTRA, hie);
249 rn->aggregate = NULL;
250 route_unlock_node(rn);
65efcfce
LB
251}
252
253/*
254 * If the child lists are empty, release the rfapi_it_extra struct
255 */
d62a17ae 256void rfapiMonitorExtraPrune(safi_t safi, struct route_node *rn)
65efcfce 257{
d62a17ae 258 struct rfapi_it_extra *hie;
259
260 if (!rn)
261 return;
262
263 if (!rn->aggregate)
264 return;
265
266 hie = (struct rfapi_it_extra *)(rn->aggregate);
267
268 switch (safi) {
269 case SAFI_ENCAP:
270 if (hie->u.encap.e)
271 return;
272 break;
273
274 case SAFI_MPLS_VPN:
275 if (hie->u.vpn.v)
276 return;
277 if (hie->u.vpn.mon_eth) {
278 if (skiplist_count(hie->u.vpn.mon_eth))
279 return;
280 skiplist_free(hie->u.vpn.mon_eth);
281 hie->u.vpn.mon_eth = NULL;
282 route_unlock_node(rn); /* uncount skiplist */
283 }
284 if (hie->u.vpn.e.source) {
285 if (skiplist_count(hie->u.vpn.e.source))
286 return;
287 skiplist_free(hie->u.vpn.e.source);
288 hie->u.vpn.e.source = NULL;
289 route_unlock_node(rn);
290 }
291 if (hie->u.vpn.idx_rd) {
292 if (skiplist_count(hie->u.vpn.idx_rd))
293 return;
294 skiplist_free(hie->u.vpn.idx_rd);
295 hie->u.vpn.idx_rd = NULL;
296 route_unlock_node(rn);
297 }
298 if (hie->u.vpn.mon_eth) {
299 if (skiplist_count(hie->u.vpn.mon_eth))
300 return;
301 skiplist_free(hie->u.vpn.mon_eth);
302 hie->u.vpn.mon_eth = NULL;
303 route_unlock_node(rn);
304 }
305 break;
306
307 default:
308 assert(0);
309 }
310 XFREE(MTYPE_RFAPI_IT_EXTRA, hie);
311 rn->aggregate = NULL;
312 route_unlock_node(rn);
65efcfce
LB
313}
314
315/*
316 * returns locked node
317 */
d62a17ae 318struct route_node *rfapiMonitorGetAttachNode(struct rfapi_descriptor *rfd,
319 struct prefix *p)
65efcfce 320{
d62a17ae 321 afi_t afi;
322 struct route_node *rn;
323
324 if (RFAPI_0_PREFIX(p)) {
325 assert(1);
326 }
327
328 afi = family2afi(p->family);
329 assert(afi);
330
331 /*
332 * It's possible that even though there is a route at this node,
333 * there are no routes with valid UN addresses (i.e,. with no
334 * valid tunnel routes). Check for that and walk back up the
335 * tree if necessary.
336 *
337 * When the outer loop completes, the matched node, if any, is
338 * locked (i.e., its reference count has been incremented) to
339 * account for the VPN monitor we are about to attach.
340 *
341 * if a monitor is moved to another node, there must be
342 * corresponding unlock/locks
343 */
344 for (rn = route_node_match(rfd->import_table->imported_vpn[afi], p);
345 rn;) {
346
347 struct bgp_info *bi;
348 struct prefix pfx_dummy;
349
350 /* TBD update this code to use new valid_interior_count */
351 for (bi = rn->info; bi; bi = bi->next) {
352 /*
353 * If there is a cached ENCAP UN address, it's a usable
354 * VPN route
355 */
356 if (bi->extra && bi->extra->vnc.import.un_family) {
357 break;
358 }
359
360 /*
361 * Or if there is a valid Encap Attribute tunnel subtlv
362 * address,
363 * it's a usable VPN route.
364 */
365 if (!rfapiGetVncTunnelUnAddr(bi->attr, &pfx_dummy)) {
366 break;
367 }
368 }
369 if (bi)
370 break;
371
372 route_unlock_node(rn);
373 if ((rn = rn->parent)) {
374 route_lock_node(rn);
375 }
376 }
377
378 if (!rn) {
379 struct prefix pfx_default;
380
381 memset(&pfx_default, 0, sizeof(pfx_default));
382 pfx_default.family = p->family;
383
384 /* creates default node if none exists, and increments ref count
385 */
386 rn = route_node_get(rfd->import_table->imported_vpn[afi],
387 &pfx_default);
388 }
389
390 return rn;
65efcfce
LB
391}
392
d62a17ae 393/*
65efcfce
LB
394 * If this function happens to attach the monitor to a radix tree
395 * node (as opposed to the 0-prefix list), the node pointer is
396 * returned (for the benefit of caller which might like to use it
397 * to generate an immediate query response).
398 */
d62a17ae 399static struct route_node *rfapiMonitorAttachImport(struct rfapi_descriptor *rfd,
400 struct rfapi_monitor_vpn *m)
65efcfce 401{
d62a17ae 402 struct route_node *rn;
403
404 rfapiMonitorCheckAttachAllowed();
405
406 if (RFAPI_0_PREFIX(&m->p)) {
407 /*
408 * Add new monitor entry to vpn0 list
409 */
410 afi_t afi;
411
412 afi = family2afi(m->p.family);
413 assert(afi);
414
415 m->next = rfd->import_table->vpn0_queries[afi];
416 rfd->import_table->vpn0_queries[afi] = m;
417 vnc_zlog_debug_verbose("%s: attached monitor %p to vpn0 list",
418 __func__, m);
419 return NULL;
420 }
421
422 /*
423 * Attach new monitor entry to import table node
424 */
425 rn = rfapiMonitorGetAttachNode(rfd, &m->p); /* returns locked rn */
426 m->node = rn;
427 m->next = RFAPI_MONITOR_VPN(rn);
428 RFAPI_MONITOR_VPN_W_ALLOC(rn) = m;
429 RFAPI_CHECK_REFCOUNT(rn, SAFI_MPLS_VPN, 0);
430 vnc_zlog_debug_verbose("%s: attached monitor %p to rn %p", __func__, m,
431 rn);
432 return rn;
65efcfce
LB
433}
434
435
436/*
437 * reattach monitors for this HD to import table
438 */
d62a17ae 439void rfapiMonitorAttachImportHd(struct rfapi_descriptor *rfd)
65efcfce 440{
d62a17ae 441 struct route_node *mrn;
442
443 if (!rfd->mon) {
444 /*
445 * No monitors for this HD
446 */
447 return;
448 }
449
450 for (mrn = route_top(rfd->mon); mrn; mrn = route_next(mrn)) {
451
452 if (!mrn->info)
453 continue;
454
455 (void)rfapiMonitorAttachImport(
456 rfd, (struct rfapi_monitor_vpn *)(mrn->info));
457 }
65efcfce
LB
458}
459
460/*
461 * Adds a monitor for a query to the NVE descriptor's list
462 * and, if callbacks are enabled, attaches it to the import table.
463 *
464 * If we happened to locate the import table radix tree attachment
465 * point, return it so the caller can use it to generate a query
466 * response without repeating the lookup. Note that when callbacks
467 * are disabled, this function will not perform a lookup, and the
468 * caller will have to do its own lookup.
469 */
470struct route_node *
d62a17ae 471rfapiMonitorAdd(struct bgp *bgp, struct rfapi_descriptor *rfd, struct prefix *p)
65efcfce 472{
d62a17ae 473 struct rfapi_monitor_vpn *m;
474 struct route_node *rn;
475
476 /*
477 * Initialize nve's monitor list if needed
478 * NB use the same radix tree for IPv4 and IPv6 targets.
479 * The prefix will always have full-length mask (/32, /128)
480 * or be 0/0 so they won't get mixed up.
481 */
482 if (!rfd->mon) {
483 rfd->mon = route_table_init();
484 }
485 rn = route_node_get(rfd->mon, p);
486 if (rn->info) {
487 /*
488 * received this query before, no further action needed
489 */
490 rfapiMonitorTimerRestart((struct rfapi_monitor_vpn *)rn->info);
491 route_unlock_node(rn);
492 return NULL;
493 }
494
495 /*
496 * New query for this nve, record it in the HD
497 */
498 rn->info =
499 XCALLOC(MTYPE_RFAPI_MONITOR, sizeof(struct rfapi_monitor_vpn));
500 m = (struct rfapi_monitor_vpn *)(rn->info);
501 m->rfd = rfd;
502 prefix_copy(&m->p, p);
503
504 ++rfd->monitor_count;
505 ++bgp->rfapi->monitor_count;
506
507 rfapiMonitorTimerRestart(m);
508
509 if (bgp->rfapi_cfg->flags & BGP_VNC_CONFIG_CALLBACK_DISABLE) {
510 /*
511 * callbacks turned off, so don't attach monitor to import table
512 */
513 return NULL;
514 }
515
516
517 /*
518 * attach to import table
519 */
520 return rfapiMonitorAttachImport(rfd, m);
65efcfce
LB
521}
522
523/*
524 * returns monitor pointer if found, NULL if not
525 */
526static struct rfapi_monitor_vpn *
d62a17ae 527rfapiMonitorDetachImport(struct rfapi_monitor_vpn *m)
65efcfce 528{
d62a17ae 529 struct rfapi_monitor_vpn *prev;
530 struct rfapi_monitor_vpn *this = NULL;
531
532 if (RFAPI_0_PREFIX(&m->p)) {
533 afi_t afi;
534
535 /*
536 * 0-prefix monitors are stored in a special list and not
537 * in the import VPN tree
538 */
539
540 afi = family2afi(m->p.family);
541 assert(afi);
542
543 if (m->rfd->import_table) {
544 for (prev = NULL,
545 this = m->rfd->import_table->vpn0_queries[afi];
546 this; prev = this, this = this->next) {
547
548 if (this == m)
549 break;
550 }
551 if (this) {
552 if (!prev) {
553 m->rfd->import_table
554 ->vpn0_queries[afi] =
555 this->next;
556 } else {
557 prev->next = this->next;
558 }
559 }
560 }
561 } else {
562
563 if (m->node) {
564 for (prev = NULL, this = RFAPI_MONITOR_VPN(m->node);
565 this; prev = this, this = this->next) {
566
567 if (this == m)
568 break;
569 }
570 if (this) {
571 if (prev) {
572 prev->next = this->next;
573 } else {
574 RFAPI_MONITOR_VPN_W_ALLOC(m->node) =
575 this->next;
576 }
577 RFAPI_CHECK_REFCOUNT(m->node, SAFI_MPLS_VPN, 1);
578 route_unlock_node(m->node);
579 }
580 m->node = NULL;
581 }
582 }
583 return this;
65efcfce
LB
584}
585
586
d62a17ae 587void rfapiMonitorDetachImportHd(struct rfapi_descriptor *rfd)
65efcfce 588{
d62a17ae 589 struct route_node *rn;
590
591 if (!rfd->mon)
592 return;
593
594 for (rn = route_top(rfd->mon); rn; rn = route_next(rn)) {
595 if (rn->info) {
596 rfapiMonitorDetachImport(
597 (struct rfapi_monitor_vpn *)(rn->info));
598 }
599 }
65efcfce
LB
600}
601
d62a17ae 602void rfapiMonitorDel(struct bgp *bgp, struct rfapi_descriptor *rfd,
603 struct prefix *p)
65efcfce 604{
d62a17ae 605 struct route_node *rn;
606 struct rfapi_monitor_vpn *m;
607
608 assert(rfd->mon);
609 rn = route_node_get(rfd->mon, p); /* locks node */
610 m = rn->info;
611
612 assert(m);
613
614 /*
615 * remove from import table
616 */
617 if (!(bgp->rfapi_cfg->flags & BGP_VNC_CONFIG_CALLBACK_DISABLE)) {
618 rfapiMonitorDetachImport(m);
619 }
620
621 if (m->timer) {
622 thread_cancel(m->timer);
623 m->timer = NULL;
624 }
625
626 /*
627 * remove from rfd list
628 */
629 XFREE(MTYPE_RFAPI_MONITOR, m);
630 rn->info = NULL;
631 route_unlock_node(rn); /* undo original lock when created */
632 route_unlock_node(rn); /* undo lock in route_node_get */
633
634 --rfd->monitor_count;
635 --bgp->rfapi->monitor_count;
65efcfce
LB
636}
637
638/*
639 * returns count of monitors deleted
640 */
d62a17ae 641int rfapiMonitorDelHd(struct rfapi_descriptor *rfd)
65efcfce 642{
d62a17ae 643 struct route_node *rn;
644 struct bgp *bgp;
645 int count = 0;
646
647 vnc_zlog_debug_verbose("%s: entry rfd=%p", __func__, rfd);
648
649 bgp = bgp_get_default();
650
651 if (rfd->mon) {
652 for (rn = route_top(rfd->mon); rn; rn = route_next(rn)) {
653 struct rfapi_monitor_vpn *m;
654 if ((m = rn->info)) {
655 if (!(bgp->rfapi_cfg->flags
656 & BGP_VNC_CONFIG_CALLBACK_DISABLE)) {
657 rfapiMonitorDetachImport(m);
658 }
659
660 if (m->timer) {
661 thread_cancel(m->timer);
662 m->timer = NULL;
663 }
664
665 XFREE(MTYPE_RFAPI_MONITOR, m);
666 rn->info = NULL;
667 route_unlock_node(rn); /* undo original lock
668 when created */
669 ++count;
670 --rfd->monitor_count;
671 --bgp->rfapi->monitor_count;
672 }
673 }
674 route_table_finish(rfd->mon);
675 rfd->mon = NULL;
676 }
677
678 if (rfd->mon_eth) {
679
680 struct rfapi_monitor_eth *mon_eth;
681
682 while (!skiplist_first(rfd->mon_eth, NULL, (void **)&mon_eth)) {
683
684 int rc;
685
686 if (!(bgp->rfapi_cfg->flags
687 & BGP_VNC_CONFIG_CALLBACK_DISABLE)) {
688 rfapiMonitorEthDetachImport(bgp, mon_eth);
689 } else {
65efcfce 690#if DEBUG_L2_EXTRA
d62a17ae 691 vnc_zlog_debug_verbose(
692 "%s: callbacks disabled, not attempting to detach mon_eth %p",
693 __func__, mon_eth);
65efcfce 694#endif
d62a17ae 695 }
696
697 if (mon_eth->timer) {
698 thread_cancel(mon_eth->timer);
699 mon_eth->timer = NULL;
700 }
701
702 /*
703 * remove from rfd list
704 */
705 rc = skiplist_delete(rfd->mon_eth, mon_eth, mon_eth);
706 assert(!rc);
707
708 vnc_zlog_debug_verbose("%s: freeing mon_eth %p",
709 __func__, mon_eth);
710 XFREE(MTYPE_RFAPI_MONITOR_ETH, mon_eth);
711
712 ++count;
713 --rfd->monitor_count;
714 --bgp->rfapi->monitor_count;
715 }
716 skiplist_free(rfd->mon_eth);
717 rfd->mon_eth = NULL;
718 }
719
720 return count;
65efcfce
LB
721}
722
d62a17ae 723void rfapiMonitorResponseRemovalOff(struct bgp *bgp)
65efcfce 724{
d62a17ae 725 if (bgp->rfapi_cfg->flags & BGP_VNC_CONFIG_RESPONSE_REMOVAL_DISABLE) {
726 return;
727 }
728 bgp->rfapi_cfg->flags |= BGP_VNC_CONFIG_RESPONSE_REMOVAL_DISABLE;
65efcfce
LB
729}
730
d62a17ae 731void rfapiMonitorResponseRemovalOn(struct bgp *bgp)
65efcfce 732{
d62a17ae 733 if (!(bgp->rfapi_cfg->flags
734 & BGP_VNC_CONFIG_RESPONSE_REMOVAL_DISABLE)) {
735 return;
736 }
737 bgp->rfapi_cfg->flags &= ~BGP_VNC_CONFIG_RESPONSE_REMOVAL_DISABLE;
65efcfce
LB
738}
739
d62a17ae 740static int rfapiMonitorTimerExpire(struct thread *t)
65efcfce 741{
d62a17ae 742 struct rfapi_monitor_vpn *m = t->arg;
65efcfce 743
d62a17ae 744 /* forget reference to thread, it's gone */
745 m->timer = NULL;
65efcfce 746
d62a17ae 747 /* delete the monitor */
748 rfapiMonitorDel(bgp_get_default(), m->rfd, &m->p);
65efcfce 749
d62a17ae 750 return 0;
65efcfce
LB
751}
752
d62a17ae 753static void rfapiMonitorTimerRestart(struct rfapi_monitor_vpn *m)
65efcfce 754{
d62a17ae 755 if (m->timer) {
756 unsigned long remain = thread_timer_remain_second(m->timer);
757
758 /* unexpected case, but avoid wraparound problems below */
759 if (remain > m->rfd->response_lifetime)
760 return;
761
762 /* don't restart if we just restarted recently */
763 if (m->rfd->response_lifetime - remain < 2)
764 return;
765
766 thread_cancel(m->timer);
767 m->timer = NULL;
768 }
769
770 {
771 char buf[BUFSIZ];
772
773 vnc_zlog_debug_verbose(
774 "%s: target %s life %u", __func__,
775 rfapi_ntop(m->p.family, m->p.u.val, buf, BUFSIZ),
776 m->rfd->response_lifetime);
777 }
778 m->timer = NULL;
779 thread_add_timer(bm->master, rfapiMonitorTimerExpire, m,
780 m->rfd->response_lifetime, &m->timer);
65efcfce
LB
781}
782
d62a17ae 783/*
65efcfce
LB
784 * called when an updated response is sent to the NVE. Per
785 * ticket 255, restart timers for any monitors that could have
786 * been responsible for the response, i.e., any monitors for
787 * the exact prefix or a parent of it.
788 */
d62a17ae 789void rfapiMonitorTimersRestart(struct rfapi_descriptor *rfd, struct prefix *p)
65efcfce 790{
d62a17ae 791 struct route_node *rn;
792
793 if (AF_ETHERNET == p->family) {
794 struct rfapi_monitor_eth *mon_eth;
795 int rc;
796 void *cursor;
797
798 /*
799 * XXX match any LNI
800 */
801 for (cursor = NULL,
802 rc = skiplist_next(rfd->mon_eth, NULL, (void **)&mon_eth,
803 &cursor);
804 rc == 0; rc = skiplist_next(rfd->mon_eth, NULL,
805 (void **)&mon_eth, &cursor)) {
806
807 if (!memcmp(mon_eth->macaddr.octet,
28328ea9 808 p->u.prefix_eth.octet, ETH_ALEN)) {
d62a17ae 809
810 rfapiMonitorEthTimerRestart(mon_eth);
811 }
812 }
813
814 } else {
815 for (rn = route_top(rfd->mon); rn; rn = route_next(rn)) {
816 struct rfapi_monitor_vpn *m;
817
818 if (!((m = rn->info)))
819 continue;
820
821 /* NB order of test is significant ! */
822 if (!m->node || prefix_match(&m->node->p, p)) {
823 rfapiMonitorTimerRestart(m);
824 }
825 }
826 }
65efcfce
LB
827}
828
829/*
830 * Find monitors at this node and all its parents. Call
831 * rfapiRibUpdatePendingNode with this node and all corresponding NVEs.
832 */
d62a17ae 833void rfapiMonitorItNodeChanged(
834 struct rfapi_import_table *import_table, struct route_node *it_node,
835 struct rfapi_monitor_vpn *monitor_list) /* for base it node, NULL=all */
65efcfce 836{
d62a17ae 837 struct skiplist *nves_seen;
838 struct route_node *rn = it_node;
839 struct bgp *bgp = bgp_get_default();
840 afi_t afi = family2afi(rn->p.family);
65efcfce 841#if DEBUG_L2_EXTRA
d62a17ae 842 char buf_prefix[BUFSIZ];
65efcfce
LB
843#endif
844
d62a17ae 845 assert(bgp);
846 assert(import_table);
65efcfce 847
d62a17ae 848 nves_seen = skiplist_new(0, NULL, NULL);
65efcfce
LB
849
850#if DEBUG_L2_EXTRA
d62a17ae 851 prefix2str(&it_node->p, buf_prefix, BUFSIZ);
852 vnc_zlog_debug_verbose("%s: it=%p, it_node=%p, it_node->prefix=%s",
853 __func__, import_table, it_node, buf_prefix);
65efcfce
LB
854#endif
855
d62a17ae 856 if (AFI_L2VPN == afi) {
857 struct rfapi_monitor_eth *m;
858 struct skiplist *sl;
859 void *cursor;
860 int rc;
861
862 if ((sl = RFAPI_MONITOR_ETH(rn))) {
863
864 for (cursor = NULL,
865 rc = skiplist_next(sl, NULL, (void **)&m,
866 (void **)&cursor);
867 !rc; rc = skiplist_next(sl, NULL, (void **)&m,
868 (void **)&cursor)) {
869
870 if (skiplist_search(nves_seen, m->rfd, NULL)) {
871 /*
872 * Haven't done this NVE yet. Add to
873 * "seen" list.
874 */
875 assert(!skiplist_insert(nves_seen,
876 m->rfd, NULL));
877
878 /*
879 * update its RIB
880 */
881 rfapiRibUpdatePendingNode(
882 bgp, m->rfd, import_table,
883 it_node,
884 m->rfd->response_lifetime);
885 }
886 }
887 }
888
889 } else {
890
891 struct rfapi_monitor_vpn *m;
892
893 if (monitor_list) {
894 m = monitor_list;
895 } else {
896 m = RFAPI_MONITOR_VPN(rn);
897 }
898
899 do {
900 /*
901 * If we have reached the root node (parent==NULL) and
902 * there
903 * are no routes here (info==NULL), and the IT node that
904 * changed was not the root node (it_node->parent !=
905 * NULL),
906 * then any monitors at this node are here because they
907 * had
908 * no match at all. Therefore, do not send route updates
909 * to them
910 * because we haven't sent them an initial route.
911 */
912 if (!rn->parent && !rn->info && it_node->parent)
913 break;
914
915 for (; m; m = m->next) {
916
917 if (RFAPI_0_PREFIX(&m->p)) {
918 /* shouldn't happen, but be safe */
919 continue;
920 }
921 if (skiplist_search(nves_seen, m->rfd, NULL)) {
922 /*
923 * Haven't done this NVE yet. Add to
924 * "seen" list.
925 */
926 assert(!skiplist_insert(nves_seen,
927 m->rfd, NULL));
928
929 {
930 char buf_attach_pfx[BUFSIZ];
931 char buf_target_pfx[BUFSIZ];
932
933 prefix2str(&m->node->p,
934 buf_attach_pfx,
935 BUFSIZ);
936 prefix2str(&m->p,
937 buf_target_pfx,
938 BUFSIZ);
939 vnc_zlog_debug_verbose(
940 "%s: update rfd %p attached to pfx %s (targ=%s)",
941 __func__, m->rfd,
942 buf_attach_pfx,
943 buf_target_pfx);
944 }
945
946 /*
947 * update its RIB
948 */
949 rfapiRibUpdatePendingNode(
950 bgp, m->rfd, import_table,
951 it_node,
952 m->rfd->response_lifetime);
953 }
954 }
955 rn = rn->parent;
956 if (rn)
957 m = RFAPI_MONITOR_VPN(rn);
958 } while (rn);
959 }
960
961 /*
962 * All-routes L2 monitors
963 */
964 if (AFI_L2VPN == afi) {
965 struct rfapi_monitor_eth *e;
65efcfce
LB
966
967#if DEBUG_L2_EXTRA
d62a17ae 968 vnc_zlog_debug_verbose("%s: checking L2 all-routes monitors",
969 __func__);
65efcfce
LB
970#endif
971
d62a17ae 972 for (e = import_table->eth0_queries; e; e = e->next) {
65efcfce 973#if DEBUG_L2_EXTRA
d62a17ae 974 vnc_zlog_debug_verbose("%s: checking eth0 mon=%p",
975 __func__, e);
65efcfce 976#endif
d62a17ae 977 if (skiplist_search(nves_seen, e->rfd, NULL)) {
978 /*
979 * Haven't done this NVE yet. Add to "seen"
980 * list.
981 */
982 assert(!skiplist_insert(nves_seen, e->rfd,
983 NULL));
984
985/*
986 * update its RIB
987 */
65efcfce 988#if DEBUG_L2_EXTRA
d62a17ae 989 vnc_zlog_debug_verbose(
990 "%s: found L2 all-routes monitor %p",
991 __func__, e);
65efcfce 992#endif
d62a17ae 993 rfapiRibUpdatePendingNode(
994 bgp, e->rfd, import_table, it_node,
995 e->rfd->response_lifetime);
996 }
997 }
998 } else {
999 struct rfapi_monitor_vpn *m;
1000
1001 /*
1002 * All-routes IPv4. IPv6 monitors
1003 */
1004 for (m = import_table->vpn0_queries[afi]; m; m = m->next) {
1005 if (skiplist_search(nves_seen, m->rfd, NULL)) {
1006 /*
1007 * Haven't done this NVE yet. Add to "seen"
1008 * list.
1009 */
1010 assert(!skiplist_insert(nves_seen, m->rfd,
1011 NULL));
1012
1013 /*
1014 * update its RIB
1015 */
1016 rfapiRibUpdatePendingNode(
1017 bgp, m->rfd, import_table, it_node,
1018 m->rfd->response_lifetime);
1019 }
1020 }
1021 }
1022
1023 skiplist_free(nves_seen);
65efcfce
LB
1024}
1025
1026/*
1027 * For the listed monitors, update new node and its subtree, but
1028 * omit old node and its subtree
1029 */
d62a17ae 1030void rfapiMonitorMovedUp(struct rfapi_import_table *import_table,
1031 struct route_node *old_node,
1032 struct route_node *new_node,
1033 struct rfapi_monitor_vpn *monitor_list)
65efcfce 1034{
d62a17ae 1035 struct bgp *bgp = bgp_get_default();
1036 struct rfapi_monitor_vpn *m;
1037
1038 assert(new_node);
1039 assert(old_node);
1040 assert(new_node != old_node);
1041
1042 /*
1043 * If new node is 0/0 and there is no route there, don't
1044 * generate an update because it will not contain any
1045 * routes including the target.
1046 */
1047 if (!new_node->parent && !new_node->info) {
1048 vnc_zlog_debug_verbose(
1049 "%s: new monitor at 0/0 and no routes, no updates",
1050 __func__);
1051 return;
1052 }
1053
1054 for (m = monitor_list; m; m = m->next) {
1055 rfapiRibUpdatePendingNode(bgp, m->rfd, import_table, new_node,
1056 m->rfd->response_lifetime);
1057 rfapiRibUpdatePendingNodeSubtree(bgp, m->rfd, import_table,
1058 new_node, old_node,
1059 m->rfd->response_lifetime);
1060 }
65efcfce
LB
1061}
1062
d62a17ae 1063static int rfapiMonitorEthTimerExpire(struct thread *t)
65efcfce 1064{
d62a17ae 1065 struct rfapi_monitor_eth *m = t->arg;
65efcfce 1066
d62a17ae 1067 /* forget reference to thread, it's gone */
1068 m->timer = NULL;
65efcfce 1069
d62a17ae 1070 /* delete the monitor */
1071 rfapiMonitorEthDel(bgp_get_default(), m->rfd, &m->macaddr,
1072 m->logical_net_id);
65efcfce 1073
d62a17ae 1074 return 0;
65efcfce
LB
1075}
1076
d62a17ae 1077static void rfapiMonitorEthTimerRestart(struct rfapi_monitor_eth *m)
65efcfce 1078{
d62a17ae 1079 if (m->timer) {
1080 unsigned long remain = thread_timer_remain_second(m->timer);
1081
1082 /* unexpected case, but avoid wraparound problems below */
1083 if (remain > m->rfd->response_lifetime)
1084 return;
1085
1086 /* don't restart if we just restarted recently */
1087 if (m->rfd->response_lifetime - remain < 2)
1088 return;
1089
1090 thread_cancel(m->timer);
1091 m->timer = NULL;
1092 }
1093
1094 {
1095 char buf[BUFSIZ];
1096
1097 vnc_zlog_debug_verbose(
1098 "%s: target %s life %u", __func__,
1099 rfapiEthAddr2Str(&m->macaddr, buf, BUFSIZ),
1100 m->rfd->response_lifetime);
1101 }
1102 m->timer = NULL;
1103 thread_add_timer(bm->master, rfapiMonitorEthTimerExpire, m,
1104 m->rfd->response_lifetime, &m->timer);
65efcfce
LB
1105}
1106
d62a17ae 1107static int mon_eth_cmp(void *a, void *b)
65efcfce 1108{
d62a17ae 1109 struct rfapi_monitor_eth *m1;
1110 struct rfapi_monitor_eth *m2;
1111
1112 int i;
1113
1114 m1 = (struct rfapi_monitor_eth *)a;
1115 m2 = (struct rfapi_monitor_eth *)b;
1116
1117 /*
1118 * compare ethernet addresses
1119 */
28328ea9 1120 for (i = 0; i < ETH_ALEN; ++i) {
d62a17ae 1121 if (m1->macaddr.octet[i] != m2->macaddr.octet[i])
1122 return (m1->macaddr.octet[i] - m2->macaddr.octet[i]);
1123 }
1124
1125 /*
1126 * compare LNIs
1127 */
1128 return (m1->logical_net_id - m2->logical_net_id);
65efcfce
LB
1129}
1130
d62a17ae 1131static void rfapiMonitorEthAttachImport(
1132 struct rfapi_import_table *it,
1133 struct route_node *rn, /* it node attach point if non-0 */
1134 struct rfapi_monitor_eth *mon) /* monitor struct to attach */
65efcfce 1135{
d62a17ae 1136 struct skiplist *sl;
1137 int rc;
65efcfce 1138
d62a17ae 1139 vnc_zlog_debug_verbose("%s: it=%p", __func__, it);
65efcfce 1140
d62a17ae 1141 rfapiMonitorCheckAttachAllowed();
65efcfce 1142
d62a17ae 1143 if (RFAPI_0_ETHERADDR(&mon->macaddr)) {
1144 /*
1145 * These go on a different list
1146 */
1147 mon->next = it->eth0_queries;
1148 it->eth0_queries = mon;
65efcfce 1149#if DEBUG_L2_EXTRA
d62a17ae 1150 vnc_zlog_debug_verbose("%s: attached monitor %p to eth0 list",
1151 __func__, mon);
65efcfce 1152#endif
d62a17ae 1153 return;
1154 }
65efcfce 1155
d62a17ae 1156 if (rn == NULL) {
65efcfce 1157#if DEBUG_L2_EXTRA
d62a17ae 1158 vnc_zlog_debug_verbose("%s: rn is null!", __func__);
65efcfce 1159#endif
d62a17ae 1160 return;
1161 }
1162
1163 /*
1164 * Get sl to attach to
1165 */
1166 sl = RFAPI_MONITOR_ETH_W_ALLOC(rn);
1167 if (!sl) {
1168 sl = RFAPI_MONITOR_ETH_W_ALLOC(rn) =
1169 skiplist_new(0, NULL, NULL);
1170 route_lock_node(rn); /* count skiplist mon_eth */
1171 }
65efcfce
LB
1172
1173#if DEBUG_L2_EXTRA
d62a17ae 1174 vnc_zlog_debug_verbose(
1175 "%s: rn=%p, rn->lock=%d, sl=%p, attaching eth mon %p", __func__,
1176 rn, rn->lock, sl, mon);
65efcfce
LB
1177#endif
1178
d62a17ae 1179 rc = skiplist_insert(sl, (void *)mon, (void *)mon);
1180 assert(!rc);
65efcfce 1181
d62a17ae 1182 /* count eth monitor */
1183 route_lock_node(rn);
65efcfce
LB
1184}
1185
1186/*
1187 * reattach monitors for this HD to import table
1188 */
d62a17ae 1189static void rfapiMonitorEthAttachImportHd(struct bgp *bgp,
1190 struct rfapi_descriptor *rfd)
65efcfce 1191{
d62a17ae 1192 void *cursor;
1193 struct rfapi_monitor_eth *mon;
1194 int rc;
1195
1196 if (!rfd->mon_eth) {
1197 /*
1198 * No monitors for this HD
1199 */
1200 return;
1201 }
1202
1203 for (cursor = NULL,
1204 rc = skiplist_next(rfd->mon_eth, NULL, (void **)&mon, &cursor);
1205 rc == 0;
1206 rc = skiplist_next(rfd->mon_eth, NULL, (void **)&mon, &cursor)) {
1207
1208 struct rfapi_import_table *it;
1209 struct prefix pfx_mac_buf;
1210 struct route_node *rn;
1211
1212 it = rfapiMacImportTableGet(bgp, mon->logical_net_id);
1213 assert(it);
1214
1215 memset((void *)&pfx_mac_buf, 0, sizeof(struct prefix));
1216 pfx_mac_buf.family = AF_ETHERNET;
1217 pfx_mac_buf.prefixlen = 48;
1218 pfx_mac_buf.u.prefix_eth = mon->macaddr;
1219
1220 rn = route_node_get(it->imported_vpn[AFI_L2VPN], &pfx_mac_buf);
1221 assert(rn);
1222
1223 (void)rfapiMonitorEthAttachImport(it, rn, mon);
1224 }
65efcfce
LB
1225}
1226
d62a17ae 1227static void rfapiMonitorEthDetachImport(
1228 struct bgp *bgp,
1229 struct rfapi_monitor_eth *mon) /* monitor struct to detach */
65efcfce 1230{
d62a17ae 1231 struct rfapi_import_table *it;
1232 struct prefix pfx_mac_buf;
1233 struct skiplist *sl;
1234 struct route_node *rn;
1235 int rc;
1236
1237 it = rfapiMacImportTableGet(bgp, mon->logical_net_id);
1238 assert(it);
1239
1240 if (RFAPI_0_ETHERADDR(&mon->macaddr)) {
1241 struct rfapi_monitor_eth *prev;
1242 struct rfapi_monitor_eth *this = NULL;
1243
1244 for (prev = NULL, this = it->eth0_queries; this;
1245 prev = this, this = this->next) {
1246
1247 if (this == mon)
1248 break;
1249 }
1250 if (this) {
1251 if (!prev) {
1252 it->eth0_queries = this->next;
1253 } else {
1254 prev->next = this->next;
1255 }
1256 }
65efcfce 1257#if DEBUG_L2_EXTRA
d62a17ae 1258 vnc_zlog_debug_verbose(
1259 "%s: it=%p, LNI=%d, detached eth0 mon %p", __func__, it,
1260 mon->logical_net_id, mon);
65efcfce 1261#endif
d62a17ae 1262 return;
1263 }
65efcfce 1264
d62a17ae 1265 memset((void *)&pfx_mac_buf, 0, sizeof(struct prefix));
1266 pfx_mac_buf.family = AF_ETHERNET;
1267 pfx_mac_buf.prefixlen = 48;
1268 pfx_mac_buf.u.prefix_eth = mon->macaddr;
65efcfce 1269
d62a17ae 1270 rn = route_node_get(it->imported_vpn[AFI_L2VPN], &pfx_mac_buf);
1271 assert(rn);
65efcfce
LB
1272
1273#if DEBUG_L2_EXTRA
d62a17ae 1274 char buf_prefix[BUFSIZ];
1275 prefix2str(&rn->p, buf_prefix, BUFSIZ);
65efcfce
LB
1276#endif
1277
d62a17ae 1278 /*
1279 * Get sl to detach from
1280 */
1281 sl = RFAPI_MONITOR_ETH(rn);
65efcfce 1282#if DEBUG_L2_EXTRA
d62a17ae 1283 vnc_zlog_debug_verbose(
1284 "%s: it=%p, rn=%p, rn->lock=%d, sl=%p, pfx=%s, LNI=%d, detaching eth mon %p",
1285 __func__, it, rn, rn->lock, sl, buf_prefix, mon->logical_net_id,
1286 mon);
65efcfce 1287#endif
d62a17ae 1288 assert(sl);
65efcfce
LB
1289
1290
d62a17ae 1291 rc = skiplist_delete(sl, (void *)mon, (void *)mon);
1292 assert(!rc);
65efcfce 1293
d62a17ae 1294 /* uncount eth monitor */
1295 route_unlock_node(rn);
65efcfce
LB
1296}
1297
d62a17ae 1298struct route_node *rfapiMonitorEthAdd(struct bgp *bgp,
1299 struct rfapi_descriptor *rfd,
1300 struct ethaddr *macaddr,
1301 uint32_t logical_net_id)
65efcfce 1302{
d62a17ae 1303 int rc;
1304 struct rfapi_monitor_eth mon_buf;
1305 struct rfapi_monitor_eth *val;
1306 struct rfapi_import_table *it;
1307 struct route_node *rn = NULL;
1308 struct prefix pfx_mac_buf;
1309
1310 if (!rfd->mon_eth) {
1311 rfd->mon_eth = skiplist_new(0, mon_eth_cmp, NULL);
1312 }
1313
1314 it = rfapiMacImportTableGet(bgp, logical_net_id);
1315 assert(it);
1316
1317 /*
1318 * Get route node in import table. Here is where we attach the
1319 * monitor.
1320 *
1321 * Look it up now because we return it to caller regardless of
1322 * whether we create a new monitor or not.
1323 */
1324 memset((void *)&pfx_mac_buf, 0, sizeof(struct prefix));
1325 pfx_mac_buf.family = AF_ETHERNET;
1326 pfx_mac_buf.prefixlen = 48;
1327 pfx_mac_buf.u.prefix_eth = *macaddr;
1328
1329 if (!RFAPI_0_ETHERADDR(macaddr)) {
1330 rn = route_node_get(it->imported_vpn[AFI_L2VPN], &pfx_mac_buf);
1331 assert(rn);
1332 }
1333
1334 memset((void *)&mon_buf, 0, sizeof(mon_buf));
1335 mon_buf.rfd = rfd;
1336 mon_buf.macaddr = *macaddr;
1337 mon_buf.logical_net_id = logical_net_id;
1338
1339 {
1340 char buf[BUFSIZ];
1341
9d303b37
DL
1342 vnc_zlog_debug_verbose(
1343 "%s: LNI=%d: rfd=%p, pfx=%s", __func__, logical_net_id,
1344 rfd, rfapi_ntop(pfx_mac_buf.family, pfx_mac_buf.u.val,
1345 buf, BUFSIZ));
d62a17ae 1346 }
1347
1348
1349 /*
1350 * look up query
1351 */
1352 rc = skiplist_search(rfd->mon_eth, (void *)&mon_buf, (void **)&val);
1353 if (!rc) {
1354 /*
1355 * Found monitor - we have seen this query before
1356 * restart timer
1357 */
1358 vnc_zlog_debug_verbose(
1359 "%s: already present in rfd->mon_eth, not adding",
1360 __func__);
1361 rfapiMonitorEthTimerRestart(val);
1362 return rn;
1363 }
1364
1365 /*
1366 * New query
1367 */
1368 val = XCALLOC(MTYPE_RFAPI_MONITOR_ETH,
1369 sizeof(struct rfapi_monitor_eth));
1370 assert(val);
1371 *val = mon_buf;
1372
1373 ++rfd->monitor_count;
1374 ++bgp->rfapi->monitor_count;
1375
1376 rc = skiplist_insert(rfd->mon_eth, val, val);
65efcfce
LB
1377
1378#if DEBUG_L2_EXTRA
d62a17ae 1379 vnc_zlog_debug_verbose("%s: inserted rfd=%p mon_eth=%p, rc=%d",
1380 __func__, rfd, val, rc);
65efcfce
LB
1381#endif
1382
d62a17ae 1383 /*
1384 * start timer
1385 */
1386 rfapiMonitorEthTimerRestart(val);
65efcfce 1387
d62a17ae 1388 if (bgp->rfapi_cfg->flags & BGP_VNC_CONFIG_CALLBACK_DISABLE) {
1389/*
1390 * callbacks turned off, so don't attach monitor to import table
1391 */
65efcfce 1392#if DEBUG_L2_EXTRA
d62a17ae 1393 vnc_zlog_debug_verbose(
1394 "%s: callbacks turned off, not attaching mon_eth %p to import table",
1395 __func__, val);
65efcfce 1396#endif
d62a17ae 1397 return rn;
1398 }
65efcfce 1399
d62a17ae 1400 /*
1401 * attach to import table
1402 */
1403 rfapiMonitorEthAttachImport(it, rn, val);
65efcfce 1404
d62a17ae 1405 return rn;
65efcfce
LB
1406}
1407
d62a17ae 1408void rfapiMonitorEthDel(struct bgp *bgp, struct rfapi_descriptor *rfd,
1409 struct ethaddr *macaddr, uint32_t logical_net_id)
65efcfce 1410{
d62a17ae 1411 struct rfapi_monitor_eth *val;
1412 struct rfapi_monitor_eth mon_buf;
1413 int rc;
65efcfce 1414
d62a17ae 1415 vnc_zlog_debug_verbose("%s: entry rfd=%p", __func__, rfd);
65efcfce 1416
d62a17ae 1417 assert(rfd->mon_eth);
65efcfce 1418
d62a17ae 1419 memset((void *)&mon_buf, 0, sizeof(mon_buf));
1420 mon_buf.macaddr = *macaddr;
1421 mon_buf.logical_net_id = logical_net_id;
65efcfce 1422
d62a17ae 1423 rc = skiplist_search(rfd->mon_eth, (void *)&mon_buf, (void **)&val);
1424 assert(!rc);
65efcfce 1425
d62a17ae 1426 /*
1427 * remove from import table
1428 */
1429 if (!(bgp->rfapi_cfg->flags & BGP_VNC_CONFIG_CALLBACK_DISABLE)) {
1430 rfapiMonitorEthDetachImport(bgp, val);
1431 }
65efcfce 1432
d62a17ae 1433 if (val->timer) {
1434 thread_cancel(val->timer);
1435 val->timer = NULL;
1436 }
65efcfce 1437
d62a17ae 1438 /*
1439 * remove from rfd list
1440 */
1441 rc = skiplist_delete(rfd->mon_eth, val, val);
1442 assert(!rc);
65efcfce
LB
1443
1444#if DEBUG_L2_EXTRA
d62a17ae 1445 vnc_zlog_debug_verbose("%s: freeing mon_eth %p", __func__, val);
65efcfce 1446#endif
d62a17ae 1447 XFREE(MTYPE_RFAPI_MONITOR_ETH, val);
65efcfce 1448
d62a17ae 1449 --rfd->monitor_count;
1450 --bgp->rfapi->monitor_count;
65efcfce
LB
1451}
1452
1453
d62a17ae 1454void rfapiMonitorCallbacksOff(struct bgp *bgp)
65efcfce 1455{
d62a17ae 1456 struct rfapi_import_table *it;
1457 afi_t afi;
1458 struct route_table *rt;
1459 struct route_node *rn;
1460 void *cursor;
1461 int rc;
1462 struct rfapi *h = bgp->rfapi;
1463
1464 if (bgp->rfapi_cfg->flags & BGP_VNC_CONFIG_CALLBACK_DISABLE) {
1465 /*
1466 * Already off.
1467 */
1468 return;
1469 }
1470 bgp->rfapi_cfg->flags |= BGP_VNC_CONFIG_CALLBACK_DISABLE;
65efcfce
LB
1471
1472#if DEBUG_L2_EXTRA
d62a17ae 1473 vnc_zlog_debug_verbose("%s: turned off callbacks", __func__);
65efcfce
LB
1474#endif
1475
d62a17ae 1476 if (h == NULL)
1477 return;
1478 /*
1479 * detach monitors from import VPN tables. The monitors
1480 * will still be linked in per-nve monitor lists.
1481 */
1482 for (it = h->imports; it; it = it->next) {
1483 for (afi = AFI_IP; afi < AFI_MAX; ++afi) {
1484
1485 struct rfapi_monitor_vpn *m;
1486 struct rfapi_monitor_vpn *next;
1487
1488 rt = it->imported_vpn[afi];
1489
1490 for (rn = route_top(rt); rn; rn = route_next(rn)) {
1491 m = RFAPI_MONITOR_VPN(rn);
1492 if (RFAPI_MONITOR_VPN(rn))
1493 RFAPI_MONITOR_VPN_W_ALLOC(rn) = NULL;
1494 for (; m; m = next) {
1495 next = m->next;
1496 m->next =
1497 NULL; /* gratuitous safeness */
1498 m->node = NULL;
1499 route_unlock_node(rn); /* uncount */
1500 }
1501 }
1502
1503 for (m = it->vpn0_queries[afi]; m; m = next) {
1504 next = m->next;
1505 m->next = NULL; /* gratuitous safeness */
1506 m->node = NULL;
1507 }
1508 it->vpn0_queries[afi] = NULL; /* detach first monitor */
1509 }
1510 }
1511
1512 /*
1513 * detach monitors from import Eth tables. The monitors
1514 * will still be linked in per-nve monitor lists.
1515 */
1516
1517 /*
1518 * Loop over ethernet import tables
1519 */
1520 for (cursor = NULL,
1521 rc = skiplist_next(h->import_mac, NULL, (void **)&it, &cursor);
1522 !rc;
1523 rc = skiplist_next(h->import_mac, NULL, (void **)&it, &cursor)) {
1524 struct rfapi_monitor_eth *e;
1525 struct rfapi_monitor_eth *enext;
1526
1527 /*
1528 * The actual route table
1529 */
1530 rt = it->imported_vpn[AFI_L2VPN];
1531
1532 /*
1533 * Find non-0 monitors (i.e., actual addresses, not FTD
1534 * monitors)
1535 */
1536 for (rn = route_top(rt); rn; rn = route_next(rn)) {
1537 struct skiplist *sl;
1538
1539 sl = RFAPI_MONITOR_ETH(rn);
1540 while (!skiplist_delete_first(sl)) {
1541 route_unlock_node(rn); /* uncount monitor */
1542 }
1543 }
1544
1545 /*
1546 * Find 0-monitors (FTD queries)
1547 */
1548 for (e = it->eth0_queries; e; e = enext) {
65efcfce 1549#if DEBUG_L2_EXTRA
d62a17ae 1550 vnc_zlog_debug_verbose("%s: detaching eth0 mon %p",
1551 __func__, e);
65efcfce 1552#endif
d62a17ae 1553 enext = e->next;
1554 e->next = NULL; /* gratuitous safeness */
1555 }
1556 it->eth0_queries = NULL; /* detach first monitor */
1557 }
65efcfce
LB
1558}
1559
d62a17ae 1560void rfapiMonitorCallbacksOn(struct bgp *bgp)
65efcfce 1561{
d62a17ae 1562 struct listnode *hnode;
1563 struct rfapi_descriptor *rfd;
1564
1565 if (!(bgp->rfapi_cfg->flags & BGP_VNC_CONFIG_CALLBACK_DISABLE)) {
1566 /*
1567 * Already on. It's important that we don't try to reattach
1568 * monitors that are already attached because, in the interest
1569 * of performance, there is no checking at the lower level
1570 * whether a monitor is already attached. It leads to
1571 * corrupted chains (e.g., looped pointers)
1572 */
1573 return;
1574 }
1575 bgp->rfapi_cfg->flags &= ~BGP_VNC_CONFIG_CALLBACK_DISABLE;
65efcfce 1576#if DEBUG_L2_EXTRA
d62a17ae 1577 vnc_zlog_debug_verbose("%s: turned on callbacks", __func__);
65efcfce 1578#endif
d62a17ae 1579 if (bgp->rfapi == NULL)
1580 return;
1581
1582 /*
1583 * reattach monitors
1584 */
1585 for (ALL_LIST_ELEMENTS_RO(&bgp->rfapi->descriptors, hnode, rfd)) {
1586
1587 rfapiMonitorAttachImportHd(rfd);
1588 rfapiMonitorEthAttachImportHd(bgp, rfd);
1589 }
65efcfce 1590}