summaryrefslogtreecommitdiffstats
path: root/bgpd/bgp_damp.c
blob: 339bfae56d4c16de66fc84010f38eb087fc2e06f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
// SPDX-License-Identifier: GPL-2.0-or-later
/* BGP flap dampening
 * Copyright (C) 2001 IP Infusion Inc.
 */

#include <zebra.h>
#include <math.h>

#include "prefix.h"
#include "memory.h"
#include "command.h"
#include "log.h"
#include "frrevent.h"
#include "queue.h"
#include "filter.h"

#include "bgpd/bgpd.h"
#include "bgpd/bgp_damp.h"
#include "bgpd/bgp_table.h"
#include "bgpd/bgp_route.h"
#include "bgpd/bgp_attr.h"
#include "bgpd/bgp_advertise.h"
#include "bgpd/bgp_vty.h"

static void bgp_reuselist_add(struct reuselist *list, struct bgp_damp_info *info)
{
	assert(info);
	SLIST_INSERT_HEAD(list, info, entry);
}

static void bgp_reuselist_del(struct reuselist *list, struct bgp_damp_info *info)
{
	assert(info);
	SLIST_REMOVE(list, info, bgp_damp_info, entry);
}

static void bgp_reuselist_switch(struct reuselist *source,
				 struct bgp_damp_info *info,
				 struct reuselist *target)
{
	assert(source && target && info);
	SLIST_REMOVE(source, info, bgp_damp_info, entry);
	SLIST_INSERT_HEAD(target, info, entry);
}

static void bgp_damp_info_unclaim(struct bgp_damp_info *bdi,
				  struct reuselist *list)
{
	assert(bdi && bdi->config);
	if (bdi->index == BGP_DAMP_NO_REUSE_LIST_INDEX)
		bgp_reuselist_del(&bdi->config->no_reuse_list, bdi);
	else
		bgp_reuselist_del(list ? list
				       : &bdi->config->reuse_list[bdi->index],
				  bdi);
	bdi->config = NULL;
}

static void bgp_damp_info_claim(struct bgp_damp_info *bdi,
				struct bgp_damp_config *bdc)
{
	assert(bdc && bdi);
	if (bdi->config == NULL) {
		bdi->config = bdc;
		return;
	}
	bgp_damp_info_unclaim(bdi, NULL);
	bdi->config = bdc;
	bdi->afi = bdc->afi;
	bdi->safi = bdc->safi;
}

struct bgp_damp_config *get_active_bdc_from_pi(struct bgp_path_info *pi,
					       afi_t afi, safi_t safi)
{
	if (!pi)
		return NULL;
	if (CHECK_FLAG(pi->peer->af_flags[afi][safi],
		       PEER_FLAG_CONFIG_DAMPENING))
		return &pi->peer->damp[afi][safi];
	if (peer_group_active(pi->peer))
		if (CHECK_FLAG(pi->peer->group->conf->af_flags[afi][safi],
			       PEER_FLAG_CONFIG_DAMPENING))
			return &pi->peer->group->conf->damp[afi][safi];
	if (CHECK_FLAG(pi->peer->bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING))
		return &pi->peer->bgp->damp[afi][safi];
	return NULL;
}

/* Calculate reuse list index by penalty value.  */
static int bgp_reuse_index(int penalty, struct bgp_damp_config *bdc)
{
	unsigned int i;
	unsigned int index;

	/*
	 * reuse_limit can't be zero, this is for Coverity
	 * to bypass division by zero test.
	 */
	assert(bdc->reuse_limit);

	i = (int)(((double)penalty / bdc->reuse_limit - 1.0)
		  * bdc->scale_factor);

	if (i >= bdc->reuse_index_size)
		i = bdc->reuse_index_size - 1;

	index = bdc->reuse_index[i] - bdc->reuse_index[0];

	return (bdc->reuse_offset + index) % bdc->reuse_list_size;
}

/* Add BGP dampening information to reuse list.  */
static void bgp_reuse_list_add(struct bgp_damp_info *bdi,
			       struct bgp_damp_config *bdc)
{
	bgp_damp_info_claim(bdi, bdc);
	bdi->index = bgp_reuse_index(bdi->penalty, bdc);
	bgp_reuselist_add(&bdc->reuse_list[bdi->index], bdi);
}

/* Delete BGP dampening information from reuse list.  */
static void bgp_reuse_list_delete(struct bgp_damp_info *bdi)
{
	bgp_damp_info_unclaim(bdi, NULL);
}

static void bgp_no_reuse_list_add(struct bgp_damp_info *bdi,
				  struct bgp_damp_config *bdc)
{
	bgp_damp_info_claim(bdi, bdc);
	bdi->index = BGP_DAMP_NO_REUSE_LIST_INDEX;
	bgp_reuselist_add(&bdc->no_reuse_list, bdi);
}

static void bgp_no_reuse_list_delete(struct bgp_damp_info *bdi)
{
	bgp_damp_info_unclaim(bdi, NULL);
}

/* Return decayed penalty value.  */
int bgp_damp_decay(time_t tdiff, int penalty, struct bgp_damp_config *bdc)
{
	unsigned int i;

	i = (int)((double)tdiff / DELTA_T);

	if (i == 0)
		return penalty;

	if (i >= bdc->decay_array_size)
		return 0;

	return (int)(penalty * bdc->decay_array[i]);
}

/* Handler of reuse timer event.  Each route in the current reuse-list
   is evaluated.  RFC2439 Section 4.8.7.  */
static void bgp_reuse_timer(struct event *t)
{
	struct bgp_damp_info *bdi, *bdi_next;
	struct reuselist plist;
	struct bgp *bgp;
	time_t t_now, t_diff;
	struct bgp_damp_config *bdc = EVENT_ARG(t);

	bdc->t_reuse = NULL;
	event_add_timer(bm->master, bgp_reuse_timer, bdc, DELTA_REUSE,
			&bdc->t_reuse);

	t_now = monotime(NULL);

	/* 1.  save a pointer to the current queue head and zero the list head
	 * list head entry. */
	assert(bdc->reuse_offset < bdc->reuse_list_size);
	plist = bdc->reuse_list[bdc->reuse_offset];
	SLIST_INIT(&bdc->reuse_list[bdc->reuse_offset]);

	/* 2.  set offset = modulo reuse-list-size ( offset + 1 ), thereby
	   rotating the circular queue of list-heads.  */
	bdc->reuse_offset = (bdc->reuse_offset + 1) % bdc->reuse_list_size;
	assert(bdc->reuse_offset < bdc->reuse_list_size);

	/* 3. if ( the saved list head pointer is non-empty ) */
	SLIST_FOREACH_SAFE (bdi, &plist, entry, bdi_next) {
		bgp = bdi->path->peer->bgp;

		/* Set t-diff = t-now - t-updated.  */
		t_diff = t_now - bdi->t_updated;

		/* Set figure-of-merit = figure-of-merit * decay-array-ok
		 * [t-diff] */
		bdi->penalty = bgp_damp_decay(t_diff, bdi->penalty, bdc);

		/* Set t-updated = t-now.  */
		bdi->t_updated = t_now;

		/* if (figure-of-merit < reuse).  */
		if (bdi->penalty < bdc->reuse_limit) {
			/* Reuse the route.  */
			bgp_path_info_unset_flag(bdi->dest, bdi->path,
						 BGP_PATH_DAMPED);
			bdi->suppress_time = 0;

			if (bdi->lastrecord == BGP_RECORD_UPDATE) {
				bgp_path_info_unset_flag(bdi->dest, bdi->path,
							 BGP_PATH_HISTORY);
				bgp_aggregate_increment(
					bgp, bgp_dest_get_prefix(bdi->dest),
					bdi->path, bdi->afi, bdi->safi);
				bgp_process(bgp, bdi->dest, bdi->path, bdi->afi,
					    bdi->safi);
			}

			if (bdi->penalty <= bdc->reuse_limit / 2.0) {
				bgp_damp_info_free(bdi, &plist, 1);
			} else {
				bdi->index = BGP_DAMP_NO_REUSE_LIST_INDEX;
				bgp_reuselist_switch(&plist, bdi,
						     &bdc->no_reuse_list);
			}
		} else {
			/* Re-insert into another list (See RFC2439 Section
			 * 4.8.6).  */
			bdi->index = bgp_reuse_index(bdi->penalty, bdc);
			bgp_reuselist_switch(&plist, bdi,
					     &bdc->reuse_list[bdi->index]);
		}
	}

	assert(SLIST_EMPTY(&plist));
}

/* A route becomes unreachable (RFC2439 Section 4.8.2).  */
int bgp_damp_withdraw(struct bgp_path_info *path, struct bgp_dest *dest,
		      afi_t afi, safi_t safi, int attr_change)
{
	time_t t_now;
	struct bgp_damp_info *bdi = NULL;
	unsigned int last_penalty = 0;
	struct bgp_damp_config *bdc;

	bdc = get_active_bdc_from_pi(path, afi, safi);
	if (!bdc)
		return BGP_DAMP_USED;

	t_now = monotime(NULL);
	/* Processing Unreachable Messages.  */
	if (path->extra)
		bdi = path->extra->damp_info;

	if (bdi == NULL) {
		/* If there is no previous stability history. */

		/* RFC2439 said:
		   1. allocate a damping structure.
		   2. set figure-of-merit = 1.
		   3. withdraw the route.  */

		bdi = XCALLOC(MTYPE_BGP_DAMP_INFO,
			      sizeof(struct bgp_damp_info));
		bdi->path = path;
		bdi->dest = dest;
		bdi->penalty =
			(attr_change ? DEFAULT_PENALTY / 2 : DEFAULT_PENALTY);
		bdi->flap = 1;
		bdi->start_time = t_now;
		bdi->suppress_time = 0;
		bdi->index = BGP_DAMP_NO_REUSE_LIST_INDEX;
		bdi->afi = afi;
		bdi->safi = safi;
		(bgp_path_info_extra_get(path))->damp_info = bdi;
		bgp_no_reuse_list_add(bdi, bdc);
	} else {
		if (bdi->config != bdc) {
			bgp_damp_info_claim(bdi, bdc);
			if (bdi->index == BGP_DAMP_NO_REUSE_LIST_INDEX)
				bgp_reuselist_add(&bdc->no_reuse_list, bdi);
			else
				bgp_reuselist_add(&bdc->reuse_list[bdi->index],
						  bdi);
		}
		last_penalty = bdi->penalty;

		/* 1. Set t-diff = t-now - t-updated.  */
		bdi->penalty = (bgp_damp_decay(t_now - bdi->t_updated,
					       bdi->penalty, bdc)
				+ (attr_change ? DEFAULT_PENALTY / 2
					       : DEFAULT_PENALTY));

		if (bdi->penalty > bdc->ceiling)
			bdi->penalty = bdc->ceiling;

		bdi->flap++;
	}

	assert((dest == bdi->dest) && (path == bdi->path));

	bdi->lastrecord = BGP_RECORD_WITHDRAW;
	bdi->t_updated = t_now;

	/* Make this route as historical status.  */
	bgp_path_info_set_flag(dest, path, BGP_PATH_HISTORY);

	/* Remove the route from a reuse list if it is on one.  */
	if (CHECK_FLAG(bdi->path->flags, BGP_PATH_DAMPED)) {
		/* If decay rate isn't equal to 0, reinsert brn. */
		if (bdi->penalty != last_penalty) {
			bgp_reuse_list_delete(bdi);
			bgp_reuse_list_add(bdi, bdc);
		}
		return BGP_DAMP_SUPPRESSED;
	}

	/* If not suppressed before, do annonunce this withdraw and
	   insert into reuse_list.  */
	if (bdi->penalty >= bdc->suppress_value) {
		bgp_path_info_set_flag(dest, path, BGP_PATH_DAMPED);
		bdi->suppress_time = t_now;
		bgp_no_reuse_list_delete(bdi);
		bgp_reuse_list_add(bdi, bdc);
	}
	return BGP_DAMP_USED;
}

int bgp_damp_update(struct bgp_path_info *path, struct bgp_dest *dest,
		    afi_t afi, safi_t safi)
{
	time_t t_now;
	struct bgp_damp_info *bdi;
	int status;
	struct bgp_damp_config *bdc;

	bdc = get_active_bdc_from_pi(path, afi, safi);
	assert(bdc);

	if (!path->extra || !((bdi = path->extra->damp_info)))
		return BGP_DAMP_USED;

	t_now = monotime(NULL);
	bgp_path_info_unset_flag(dest, path, BGP_PATH_HISTORY);

	bdi->lastrecord = BGP_RECORD_UPDATE;
	bdi->penalty =
		bgp_damp_decay(t_now - bdi->t_updated, bdi->penalty, bdc);

	if (!CHECK_FLAG(bdi->path->flags, BGP_PATH_DAMPED)
	    && (bdi->penalty < bdc->suppress_value))
		status = BGP_DAMP_USED;
	else if (CHECK_FLAG(bdi->path->flags, BGP_PATH_DAMPED)
		 && (bdi->penalty < bdc->reuse_limit)) {
		bgp_path_info_unset_flag(dest, path, BGP_PATH_DAMPED);
		bgp_reuse_list_delete(bdi);
		bgp_no_reuse_list_add(bdi, bdc);
		bdi->suppress_time = 0;
		status = BGP_DAMP_USED;
	} else
		status = BGP_DAMP_SUPPRESSED;

	if (bdi->penalty > bdc->reuse_limit / 2.0)
		bdi->t_updated = t_now;
	else
		bgp_damp_info_free(bdi, NULL, 0);

	return status;
}

void bgp_damp_info_free(struct bgp_damp_info *bdi, struct reuselist *list,
			int withdraw)
{
	assert(bdi);

	afi_t afi = bdi->afi;
	safi_t safi = bdi->safi;
	struct bgp_path_info *bpi = bdi->path;
	struct bgp_dest *dest = bdi->dest;
	struct bgp *bgp = bpi->peer->bgp;
	const struct prefix *p = bgp_dest_get_prefix(bdi->dest);

	bgp_damp_info_unclaim(bdi, list);

	bpi->extra->damp_info = NULL;
	bgp_path_info_unset_flag(dest, bpi, BGP_PATH_HISTORY | BGP_PATH_DAMPED);
	if (bdi->lastrecord == BGP_RECORD_WITHDRAW && withdraw) {
		bgp_aggregate_decrement(bgp, p, bpi, afi, SAFI_UNICAST);
		bgp_path_info_delete(dest, bpi);
		bgp_process(bgp, dest, bpi, afi, safi);
	}

	XFREE(MTYPE_BGP_DAMP_INFO, bdi);
}

static void bgp_damp_parameter_set(time_t hlife, unsigned int reuse,
				   unsigned int sup, time_t maxsup,
				   struct bgp_damp_config *bdc)
{
	double reuse_max_ratio;
	unsigned int i;
	double j;

	bdc->suppress_value = sup;
	bdc->half_life = hlife;
	bdc->reuse_limit = reuse;
	bdc->max_suppress_time = maxsup;

	/* Initialize params per bgp_damp_config. */
	bdc->reuse_index_size = REUSE_ARRAY_SIZE;

	bdc->ceiling = (int)(bdc->reuse_limit
			     * (pow(2, (double)bdc->max_suppress_time
					       / bdc->half_life)));

	/* Decay-array computations */
	bdc->decay_array_size = ceil((double)bdc->max_suppress_time / DELTA_T);
	bdc->decay_array = XMALLOC(MTYPE_BGP_DAMP_ARRAY,
				   sizeof(double) * (bdc->decay_array_size));
	bdc->decay_array[0] = 1.0;
	bdc->decay_array[1] =
		exp((1.0 / ((double)bdc->half_life / DELTA_T)) * log(0.5));

	/* Calculate decay values for all possible times */
	for (i = 2; i < bdc->decay_array_size; i++)
		bdc->decay_array[i] =
			bdc->decay_array[i - 1] * bdc->decay_array[1];

	/* Reuse-list computations */
	i = ceil((double)bdc->max_suppress_time / DELTA_REUSE) + 1;
	if (i > REUSE_LIST_SIZE || i == 0)
		i = REUSE_LIST_SIZE;
	bdc->reuse_list_size = i;

	bdc->reuse_list =
		XCALLOC(MTYPE_BGP_DAMP_ARRAY,
			bdc->reuse_list_size * sizeof(struct reuselist));
	/* Reuse-array computations */
	bdc->reuse_index = XCALLOC(MTYPE_BGP_DAMP_ARRAY,
				   sizeof(int) * bdc->reuse_index_size);

	reuse_max_ratio = (double)bdc->ceiling / bdc->reuse_limit;
	j = (exp((double)bdc->max_suppress_time / bdc->half_life) * log10(2.0));
	if (reuse_max_ratio > j && j != 0)
		reuse_max_ratio = j;

	bdc->scale_factor =
		(double)bdc->reuse_index_size / (reuse_max_ratio - 1);

	for (i = 0; i < bdc->reuse_index_size; i++) {
		bdc->reuse_index[i] =
			(int)(((double)bdc->half_life / DELTA_REUSE)
			      * log10(1.0
				      / (bdc->reuse_limit
					 * (1.0
					    + ((double)i / bdc->scale_factor))))
			      / log10(0.5));
	}
}

int bgp_damp_enable(struct bgp *bgp, afi_t afi, safi_t safi, time_t half,
		    unsigned int reuse, unsigned int suppress, time_t max)
{
	struct bgp_damp_config *bdc = &bgp->damp[afi][safi];

	if (CHECK_FLAG(bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING)) {
		if (bdc->half_life == half && bdc->reuse_limit == reuse
		    && bdc->suppress_value == suppress
		    && bdc->max_suppress_time == max)
			return 0;
		bgp_damp_disable(bgp, afi, safi);
	}

	SET_FLAG(bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING);
	bgp_damp_parameter_set(half, reuse, suppress, max, bdc);
	bdc->afi = afi;
	bdc->safi = safi;

	/* Register reuse timer.  */
	event_add_timer(bm->master, bgp_reuse_timer, bdc, DELTA_REUSE,
			&bdc->t_reuse);

	return 0;
}

/* Clean all the bgp_damp_info stored in reuse_list and no_reuse_list. */
void bgp_damp_info_clean(struct bgp *bgp, struct bgp_damp_config *bdc,
			 afi_t afi, safi_t safi)
{
	struct bgp_damp_info *bdi;
	struct reuselist *list;
	unsigned int i;

	bdc->reuse_offset = 0;
	for (i = 0; i < bdc->reuse_list_size; ++i) {
		list = &bdc->reuse_list[i];
		while ((bdi = SLIST_FIRST(list)) != NULL) {
			if (bdi->lastrecord == BGP_RECORD_UPDATE) {
				bgp_aggregate_increment(bgp,
							bgp_dest_get_prefix(
								bdi->dest),
							bdi->path, bdi->afi,
							bdi->safi);
				bgp_process(bgp, bdi->dest, bdi->path, bdi->afi,
					    bdi->safi);
			}
			bgp_damp_info_free(bdi, list, 1);
		}
	}

	while ((bdi = SLIST_FIRST(&bdc->no_reuse_list)) != NULL)
		bgp_damp_info_free(bdi, &bdc->no_reuse_list, 1);

	/* Free decay array */
	XFREE(MTYPE_BGP_DAMP_ARRAY, bdc->decay_array);
	bdc->decay_array_size = 0;

	/* Free reuse index array */
	XFREE(MTYPE_BGP_DAMP_ARRAY, bdc->reuse_index);
	bdc->reuse_index_size = 0;

	XFREE(MTYPE_BGP_DAMP_ARRAY, bdc->reuse_list);
	bdc->reuse_list_size = 0;

	EVENT_OFF(bdc->t_reuse);
}

/* Disable route flap dampening for a bgp instance.
 *
 * Please note that this function also gets used to free memory when deleting a
 * bgp instance.
 */
int bgp_damp_disable(struct bgp *bgp, afi_t afi, safi_t safi)
{
	struct bgp_damp_config *bdc;

	bdc = &bgp->damp[afi][safi];
	if (!bdc)
		return 0;

	/* If it wasn't enabled, there's nothing to do. */
	if (!CHECK_FLAG(bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING))
		return 0;

	/* Cancel reuse event. */
	EVENT_OFF(bdc->t_reuse);

	/* Clean BGP dampening information.  */
	bgp_damp_info_clean(bgp, bdc, afi, safi);

	UNSET_FLAG(bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING);

	return 0;
}

void bgp_config_write_damp(struct vty *vty, struct bgp *bgp, afi_t afi,
			   safi_t safi)
{
	struct bgp_damp_config *bdc;

	bdc = &bgp->damp[afi][safi];
	if (bdc->half_life == DEFAULT_HALF_LIFE * 60 &&
	    bdc->reuse_limit == DEFAULT_REUSE &&
	    bdc->suppress_value == DEFAULT_SUPPRESS &&
	    bdc->max_suppress_time == bdc->half_life * 4)
		vty_out(vty, "  bgp dampening\n");
	else if (bdc->half_life != DEFAULT_HALF_LIFE * 60 &&
		 bdc->reuse_limit == DEFAULT_REUSE &&
		 bdc->suppress_value == DEFAULT_SUPPRESS &&
		 bdc->max_suppress_time == bdc->half_life * 4)
		vty_out(vty, "  bgp dampening %lld\n", bdc->half_life / 60LL);
	else
		vty_out(vty, "  bgp dampening %lld %d %d %lld\n",
			bdc->half_life / 60LL, bdc->reuse_limit,
			bdc->suppress_value, bdc->max_suppress_time / 60LL);
}

static const char *bgp_get_reuse_time(struct bgp_damp_config *bdc,
				      unsigned int penalty, char *buf,
				      size_t len, bool use_json,
				      json_object *json)
{
	time_t reuse_time = 0;
	struct tm tm;
	int time_store = 0;

	if (penalty > bdc->reuse_limit) {
		reuse_time = (int)(DELTA_T *
				   ((log((double)bdc->reuse_limit / penalty)) /
				    (log(bdc->decay_array[1]))));

		if (reuse_time > bdc->max_suppress_time)
			reuse_time = bdc->max_suppress_time;

		gmtime_r(&reuse_time, &tm);
	} else
		reuse_time = 0;

	/* Making formatted timer strings. */
	if (reuse_time == 0) {
		if (use_json)
			json_object_int_add(json, "reuseTimerMsecs", 0);
		else
			snprintf(buf, len, "00:00:00");
	} else if (reuse_time < ONE_DAY_SECOND) {
		if (use_json) {
			time_store = (3600000 * tm.tm_hour)
				     + (60000 * tm.tm_min)
				     + (1000 * tm.tm_sec);
			json_object_int_add(json, "reuseTimerMsecs",
					    time_store);
		} else
			snprintf(buf, len, "%02d:%02d:%02d", tm.tm_hour,
				 tm.tm_min, tm.tm_sec);
	} else if (reuse_time < ONE_WEEK_SECOND) {
		if (use_json) {
			time_store = (86400000 * tm.tm_yday)
				     + (3600000 * tm.tm_hour)
				     + (60000 * tm.tm_min)
				     + (1000 * tm.tm_sec);
			json_object_int_add(json, "reuseTimerMsecs",
					    time_store);
		} else
			snprintf(buf, len, "%dd%02dh%02dm", tm.tm_yday,
				 tm.tm_hour, tm.tm_min);
	} else {
		if (use_json) {
			time_store =
				(604800000 * tm.tm_yday / 7)
				+ (86400000
				   * (tm.tm_yday - ((tm.tm_yday / 7) * 7)))
				+ (3600000 * tm.tm_hour) + (60000 * tm.tm_min)
				+ (1000 * tm.tm_sec);
			json_object_int_add(json, "reuseTimerMsecs",
					    time_store);
		} else
			snprintf(buf, len, "%02dw%dd%02dh", tm.tm_yday / 7,
				 tm.tm_yday - ((tm.tm_yday / 7) * 7),
				 tm.tm_hour);
	}

	return buf;
}

void bgp_damp_info_vty(struct vty *vty, struct bgp *bgp,
		       struct bgp_path_info *path, afi_t afi, safi_t safi,
		       json_object *json_path)
{
	struct bgp_damp_info *bdi;
	time_t t_now, t_diff;
	char timebuf[BGP_UPTIME_LEN] = {};
	int penalty;
	struct bgp_damp_config *bdc = &bgp->damp[afi][safi];

	if (!path->extra)
		return;

	/* BGP dampening information.  */
	bdi = path->extra->damp_info;

	/* If dampening is not enabled or there is no dampening information,
	   return immediately.  */
	if (!CHECK_FLAG(path->peer->bgp->af_flags[afi][safi],
			BGP_CONFIG_DAMPENING) ||
	    !bdi)
		return;

	/* Calculate new penalty.  */
	t_now = monotime(NULL);
	t_diff = t_now - bdi->t_updated;
	penalty = bgp_damp_decay(t_diff, bdi->penalty, bdc);

	if (json_path) {
		json_object_int_add(json_path, "dampeningPenalty", penalty);
		json_object_int_add(json_path, "dampeningFlapCount", bdi->flap);
		peer_uptime(bdi->start_time, timebuf, BGP_UPTIME_LEN, 1,
			    json_path);

		if (CHECK_FLAG(path->flags, BGP_PATH_DAMPED)
		    && !CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
			bgp_get_reuse_time(bdc, penalty, timebuf,
					   BGP_UPTIME_LEN, 1, json_path);
	} else {
		vty_out(vty,
			"      Dampinfo: penalty %d, flapped %d times in %s",
			penalty, bdi->flap,
			peer_uptime(bdi->start_time, timebuf, BGP_UPTIME_LEN, 0,
				    json_path));

		if (CHECK_FLAG(path->flags, BGP_PATH_DAMPED)
		    && !CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
			vty_out(vty, ", reuse in %s",
				bgp_get_reuse_time(bdc, penalty, timebuf,
						   BGP_UPTIME_LEN, 0,
						   json_path));

		vty_out(vty, "\n");
	}
}


const char *bgp_damp_reuse_time_vty(struct vty *vty, struct bgp_path_info *path,
				    char *timebuf, size_t len, afi_t afi,
				    safi_t safi, bool use_json,
				    json_object *json)
{
	struct bgp_damp_info *bdi;
	time_t t_now, t_diff;
	int penalty;
	struct bgp_damp_config *bdc;

	bdc = get_active_bdc_from_pi(path, afi, safi);
	if (!bdc)
		return NULL;

	if (!path->extra)
		return NULL;

	/* BGP dampening information.  */
	bdi = path->extra->damp_info;

	/* If dampening is not enabled or there is no dampening information,
	   return immediately.  */
	if (!CHECK_FLAG(path->peer->bgp->af_flags[afi][safi],
			BGP_CONFIG_DAMPENING) ||
	    !bdi)
		return NULL;

	/* Calculate new penalty.  */
	t_now = monotime(NULL);
	t_diff = t_now - bdi->t_updated;
	penalty = bgp_damp_decay(t_diff, bdi->penalty, bdc);

	return bgp_get_reuse_time(bdc, penalty, timebuf, len, use_json, json);
}


static int bgp_print_dampening_parameters(struct bgp *bgp, struct vty *vty,
					  afi_t afi, safi_t safi, bool use_json)
{
	if (CHECK_FLAG(bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING)) {
		struct bgp_damp_config *bdc = &bgp->damp[afi][safi];

		if (use_json) {
			json_object *json = json_object_new_object();

			json_object_int_add(json, "halfLifeSecs",
					    bdc->half_life);
			json_object_int_add(json, "reusePenalty",
					    bdc->reuse_limit);
			json_object_int_add(json, "suppressPenalty",
					    bdc->suppress_value);
			json_object_int_add(json, "maxSuppressTimeSecs",
					    bdc->max_suppress_time);
			json_object_int_add(json, "maxSuppressPenalty",
					    bdc->ceiling);

			vty_json(vty, json);
		} else {
			vty_out(vty, "Half-life time: %lld min\n",
				(long long)bdc->half_life / 60);
			vty_out(vty, "Reuse penalty: %d\n", bdc->reuse_limit);
			vty_out(vty, "Suppress penalty: %d\n",
				bdc->suppress_value);
			vty_out(vty, "Max suppress time: %lld min\n",
				(long long)bdc->max_suppress_time / 60);
			vty_out(vty, "Max suppress penalty: %u\n",
				bdc->ceiling);
			vty_out(vty, "\n");
		}
	} else if (!use_json)
		vty_out(vty, "dampening not enabled for %s\n",
			get_afi_safi_str(afi, safi, false));

	return CMD_SUCCESS;
}

int bgp_show_dampening_parameters(struct vty *vty, afi_t afi, safi_t safi,
				  uint16_t show_flags)
{
	struct bgp *bgp;
	bool use_json = CHECK_FLAG(show_flags, BGP_SHOW_OPT_JSON);

	bgp = bgp_get_default();
	if (bgp == NULL) {
		vty_out(vty, "No BGP process is configured\n");
		return CMD_WARNING;
	}

	if (!CHECK_FLAG(show_flags, BGP_SHOW_OPT_AFI_ALL))
		return bgp_print_dampening_parameters(bgp, vty, afi, safi,
						      use_json);

	if (CHECK_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP)
	    || CHECK_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP6)) {
		afi = CHECK_FLAG(show_flags, BGP_SHOW_OPT_AFI_IP) ? AFI_IP
								  : AFI_IP6;
		FOREACH_SAFI (safi) {
			if (strmatch(get_afi_safi_str(afi, safi, true),
				     "Unknown"))
				continue;

			if (!use_json)
				vty_out(vty, "\nFor address family: %s\n\n",
					get_afi_safi_str(afi, safi, false));

			bgp_print_dampening_parameters(bgp, vty, afi, safi,
						       use_json);
		}
	} else {
		FOREACH_AFI_SAFI (afi, safi) {
			if (strmatch(get_afi_safi_str(afi, safi, true),
				     "Unknown"))
				continue;

			if (!use_json)
				vty_out(vty, "\nFor address family: %s\n",
					get_afi_safi_str(afi, safi, false));

			bgp_print_dampening_parameters(bgp, vty, afi, safi,
						       use_json);
		}
	}
	return CMD_SUCCESS;
}

void bgp_peer_damp_enable(struct peer *peer, afi_t afi, safi_t safi, time_t half,
			  unsigned int reuse, unsigned int suppress, time_t max)
{
	struct bgp_damp_config *bdc;

	if (!peer)
		return;
	bdc = &peer->damp[afi][safi];
	if (peer_af_flag_check(peer, afi, safi, PEER_FLAG_CONFIG_DAMPENING)) {
		if (bdc->half_life == half && bdc->reuse_limit == reuse &&
		    bdc->suppress_value == suppress &&
		    bdc->max_suppress_time == max)
			return;
		bgp_peer_damp_disable(peer, afi, safi);
	}
	SET_FLAG(peer->af_flags[afi][safi], PEER_FLAG_CONFIG_DAMPENING);
	bgp_damp_parameter_set(half, reuse, suppress, max, bdc);
	bdc->afi = afi;
	bdc->safi = safi;
	event_add_timer(bm->master, bgp_reuse_timer, bdc, DELTA_REUSE,
			&bdc->t_reuse);
}

/* Disable route flap dampening for a peer.
 *
 * Please note that this function also gets used to free memory when deleting a
 * peer or peer group.
 */
void bgp_peer_damp_disable(struct peer *peer, afi_t afi, safi_t safi)
{
	struct bgp_damp_config *bdc;

	if (!peer_af_flag_check(peer, afi, safi, PEER_FLAG_CONFIG_DAMPENING))
		return;
	bdc = &peer->damp[afi][safi];
	if (!bdc)
		return;
	bgp_damp_info_clean(peer->bgp, bdc, afi, safi);
	UNSET_FLAG(peer->af_flags[afi][safi], PEER_FLAG_CONFIG_DAMPENING);
}

void bgp_config_write_peer_damp(struct vty *vty, struct peer *peer, afi_t afi,
				safi_t safi)
{
	struct bgp_damp_config *bdc;

	bdc = &peer->damp[afi][safi];
	if (bdc->half_life == DEFAULT_HALF_LIFE * 60 &&
	    bdc->reuse_limit == DEFAULT_REUSE &&
	    bdc->suppress_value == DEFAULT_SUPPRESS &&
	    bdc->max_suppress_time == bdc->half_life * 4)
		vty_out(vty, "  neighbor %s dampening\n", peer->host);
	else if (bdc->half_life != DEFAULT_HALF_LIFE * 60 &&
		 bdc->reuse_limit == DEFAULT_REUSE &&
		 bdc->suppress_value == DEFAULT_SUPPRESS &&
		 bdc->max_suppress_time == bdc->half_life * 4)
		vty_out(vty, "  neighbor %s dampening %lld\n", peer->host,
			bdc->half_life / 60LL);
	else
		vty_out(vty, "  neighbor %s dampening %lld %d %d %lld\n",
			peer->host, bdc->half_life / 60LL, bdc->reuse_limit,
			bdc->suppress_value, bdc->max_suppress_time / 60LL);
}

static void bgp_print_peer_dampening_parameters(struct vty *vty,
						struct peer *peer, afi_t afi,
						safi_t safi, bool use_json,
						json_object *json)
{
	struct bgp_damp_config *bdc;

	if (!peer)
		return;
	if (CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_CONFIG_DAMPENING)) {
		bdc = &peer->damp[afi][safi];
		if (!bdc)
			return;
		if (use_json) {
			json_object_int_add(json, "halfLifeSecs",
					    bdc->half_life);
			json_object_int_add(json, "reusePenalty",
					    bdc->reuse_limit);
			json_object_int_add(json, "suppressPenalty",
					    bdc->suppress_value);
			json_object_int_add(json, "maxSuppressTimeSecs",
					    bdc->max_suppress_time);
			json_object_int_add(json, "maxSuppressPenalty",
					    bdc->ceiling);
		} else {
			vty_out(vty, "Half-life time: %lld min\n",
				(long long)bdc->half_life / 60);
			vty_out(vty, "Reuse penalty: %d\n", bdc->reuse_limit);
			vty_out(vty, "Suppress penalty: %d\n",
				bdc->suppress_value);
			vty_out(vty, "Max suppress time: %lld min\n",
				(long long)bdc->max_suppress_time / 60);
			vty_out(vty, "Max suppress penalty: %u\n", bdc->ceiling);
			vty_out(vty, "\n");
		}
	} else if (!use_json)
		vty_out(vty, "neighbor dampening not enabled for %s\n",
			get_afi_safi_str(afi, safi, false));
}

void bgp_show_peer_dampening_parameters(struct vty *vty, struct peer *peer,
					afi_t afi, safi_t safi, bool use_json)
{
	json_object *json;

	if (use_json) {
		json = json_object_new_object();
		json_object_string_add(json, "addressFamily",
				       get_afi_safi_str(afi, safi, false));
		bgp_print_peer_dampening_parameters(vty, peer, afi, safi, true,
						    json);
		vty_out(vty, "%s\n",
			json_object_to_json_string_ext(json,
						       JSON_C_TO_STRING_PRETTY));
		json_object_free(json);
	} else {
		vty_out(vty, "\nFor address family: %s\n",
			get_afi_safi_str(afi, safi, false));
		bgp_print_peer_dampening_parameters(vty, peer, afi, safi, false,
						    NULL);
	}
}