]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_damp.c
Merge pull request #5703 from ton31337/feature/limit_outgoing_prefixes
[mirror_frr.git] / bgpd / bgp_damp.c
1 /* BGP flap dampening
2 * Copyright (C) 2001 IP Infusion Inc.
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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 #include <zebra.h>
22 #include <math.h>
23
24 #include "prefix.h"
25 #include "memory.h"
26 #include "command.h"
27 #include "log.h"
28 #include "thread.h"
29 #include "queue.h"
30 #include "filter.h"
31
32 #include "bgpd/bgpd.h"
33 #include "bgpd/bgp_damp.h"
34 #include "bgpd/bgp_table.h"
35 #include "bgpd/bgp_route.h"
36 #include "bgpd/bgp_attr.h"
37 #include "bgpd/bgp_advertise.h"
38
39 /* Global variable to access damping configuration */
40 static struct bgp_damp_config damp[AFI_MAX][SAFI_MAX];
41
42 /* Utility macro to add and delete BGP dampening information to no
43 used list. */
44 #define BGP_DAMP_LIST_ADD(N, A) BGP_PATH_INFO_ADD(N, A, no_reuse_list)
45 #define BGP_DAMP_LIST_DEL(N, A) BGP_PATH_INFO_DEL(N, A, no_reuse_list)
46
47 /* Calculate reuse list index by penalty value. */
48 static int bgp_reuse_index(int penalty, struct bgp_damp_config *bdc)
49 {
50 unsigned int i;
51 int index;
52
53 i = (int)(((double)penalty / bdc->reuse_limit - 1.0)
54 * bdc->scale_factor);
55
56 if (i >= bdc->reuse_index_size)
57 i = bdc->reuse_index_size - 1;
58
59 index = bdc->reuse_index[i] - bdc->reuse_index[0];
60
61 return (bdc->reuse_offset + index) % bdc->reuse_list_size;
62 }
63
64 /* Add BGP dampening information to reuse list. */
65 static void bgp_reuse_list_add(struct bgp_damp_info *bdi,
66 struct bgp_damp_config *bdc)
67 {
68 int index;
69
70 index = bdi->index = bgp_reuse_index(bdi->penalty, bdc);
71
72 bdi->prev = NULL;
73 bdi->next = bdc->reuse_list[index];
74 if (bdc->reuse_list[index])
75 bdc->reuse_list[index]->prev = bdi;
76 bdc->reuse_list[index] = bdi;
77 }
78
79 /* Delete BGP dampening information from reuse list. */
80 static void bgp_reuse_list_delete(struct bgp_damp_info *bdi,
81 struct bgp_damp_config *bdc)
82 {
83 if (bdi->next)
84 bdi->next->prev = bdi->prev;
85 if (bdi->prev)
86 bdi->prev->next = bdi->next;
87 else
88 bdc->reuse_list[bdi->index] = bdi->next;
89 }
90
91 /* Return decayed penalty value. */
92 int bgp_damp_decay(time_t tdiff, int penalty, struct bgp_damp_config *bdc)
93 {
94 unsigned int i;
95
96 i = (int)((double)tdiff / DELTA_T);
97
98 if (i == 0)
99 return penalty;
100
101 if (i >= bdc->decay_array_size)
102 return 0;
103
104 return (int)(penalty * bdc->decay_array[i]);
105 }
106
107 /* Handler of reuse timer event. Each route in the current reuse-list
108 is evaluated. RFC2439 Section 4.8.7. */
109 static int bgp_reuse_timer(struct thread *t)
110 {
111 struct bgp_damp_info *bdi;
112 struct bgp_damp_info *next;
113 time_t t_now, t_diff;
114
115 struct bgp_damp_config *bdc = THREAD_ARG(t);
116
117 bdc->t_reuse = NULL;
118 thread_add_timer(bm->master, bgp_reuse_timer, bdc, DELTA_REUSE,
119 &bdc->t_reuse);
120
121 t_now = bgp_clock();
122
123 /* 1. save a pointer to the current zeroth queue head and zero the
124 list head entry. */
125 bdi = bdc->reuse_list[bdc->reuse_offset];
126 bdc->reuse_list[bdc->reuse_offset] = NULL;
127
128 /* 2. set offset = modulo reuse-list-size ( offset + 1 ), thereby
129 rotating the circular queue of list-heads. */
130 bdc->reuse_offset = (bdc->reuse_offset + 1) % bdc->reuse_list_size;
131
132 /* 3. if ( the saved list head pointer is non-empty ) */
133 for (; bdi; bdi = next) {
134 struct bgp *bgp = bdi->path->peer->bgp;
135
136 next = bdi->next;
137
138 /* Set t-diff = t-now - t-updated. */
139 t_diff = t_now - bdi->t_updated;
140
141 /* Set figure-of-merit = figure-of-merit * decay-array-ok
142 * [t-diff] */
143 bdi->penalty = bgp_damp_decay(t_diff, bdi->penalty, bdc);
144
145 /* Set t-updated = t-now. */
146 bdi->t_updated = t_now;
147
148 /* if (figure-of-merit < reuse). */
149 if (bdi->penalty < bdc->reuse_limit) {
150 /* Reuse the route. */
151 bgp_path_info_unset_flag(bdi->rn, bdi->path,
152 BGP_PATH_DAMPED);
153 bdi->suppress_time = 0;
154
155 if (bdi->lastrecord == BGP_RECORD_UPDATE) {
156 bgp_path_info_unset_flag(bdi->rn, bdi->path,
157 BGP_PATH_HISTORY);
158 bgp_aggregate_increment(bgp, &bdi->rn->p,
159 bdi->path, bdi->afi,
160 bdi->safi);
161 bgp_process(bgp, bdi->rn, bdi->afi, bdi->safi);
162 }
163
164 if (bdi->penalty <= bdc->reuse_limit / 2.0)
165 bgp_damp_info_free(bdi, 1, bdc->afi, bdc->safi);
166 else
167 BGP_DAMP_LIST_ADD(bdc, bdi);
168 } else
169 /* Re-insert into another list (See RFC2439 Section
170 * 4.8.6). */
171 bgp_reuse_list_add(bdi, bdc);
172 }
173
174 return 0;
175 }
176
177 /* A route becomes unreachable (RFC2439 Section 4.8.2). */
178 int bgp_damp_withdraw(struct bgp_path_info *path, struct bgp_node *rn,
179 afi_t afi, safi_t safi, int attr_change)
180 {
181 time_t t_now;
182 struct bgp_damp_info *bdi = NULL;
183 unsigned int last_penalty = 0;
184 struct bgp_damp_config *bdc = &damp[afi][safi];
185
186 t_now = bgp_clock();
187
188 /* Processing Unreachable Messages. */
189 if (path->extra)
190 bdi = path->extra->damp_info;
191
192 if (bdi == NULL) {
193 /* If there is no previous stability history. */
194
195 /* RFC2439 said:
196 1. allocate a damping structure.
197 2. set figure-of-merit = 1.
198 3. withdraw the route. */
199
200 bdi = XCALLOC(MTYPE_BGP_DAMP_INFO,
201 sizeof(struct bgp_damp_info));
202 bdi->path = path;
203 bdi->rn = rn;
204 bdi->penalty =
205 (attr_change ? DEFAULT_PENALTY / 2 : DEFAULT_PENALTY);
206 bdi->flap = 1;
207 bdi->start_time = t_now;
208 bdi->suppress_time = 0;
209 bdi->index = -1;
210 bdi->afi = afi;
211 bdi->safi = safi;
212 (bgp_path_info_extra_get(path))->damp_info = bdi;
213 BGP_DAMP_LIST_ADD(bdc, bdi);
214 } else {
215 last_penalty = bdi->penalty;
216
217 /* 1. Set t-diff = t-now - t-updated. */
218 bdi->penalty = (bgp_damp_decay(t_now - bdi->t_updated,
219 bdi->penalty, bdc)
220 + (attr_change ? DEFAULT_PENALTY / 2
221 : DEFAULT_PENALTY));
222
223 if (bdi->penalty > bdc->ceiling)
224 bdi->penalty = bdc->ceiling;
225
226 bdi->flap++;
227 }
228
229 assert((rn == bdi->rn) && (path == bdi->path));
230
231 bdi->lastrecord = BGP_RECORD_WITHDRAW;
232 bdi->t_updated = t_now;
233
234 /* Make this route as historical status. */
235 bgp_path_info_set_flag(rn, path, BGP_PATH_HISTORY);
236
237 /* Remove the route from a reuse list if it is on one. */
238 if (CHECK_FLAG(bdi->path->flags, BGP_PATH_DAMPED)) {
239 /* If decay rate isn't equal to 0, reinsert brn. */
240 if (bdi->penalty != last_penalty && bdi->index >= 0) {
241 bgp_reuse_list_delete(bdi, bdc);
242 bgp_reuse_list_add(bdi, bdc);
243 }
244 return BGP_DAMP_SUPPRESSED;
245 }
246
247 /* If not suppressed before, do annonunce this withdraw and
248 insert into reuse_list. */
249 if (bdi->penalty >= bdc->suppress_value) {
250 bgp_path_info_set_flag(rn, path, BGP_PATH_DAMPED);
251 bdi->suppress_time = t_now;
252 BGP_DAMP_LIST_DEL(bdc, bdi);
253 bgp_reuse_list_add(bdi, bdc);
254 }
255
256 return BGP_DAMP_USED;
257 }
258
259 int bgp_damp_update(struct bgp_path_info *path, struct bgp_node *rn, afi_t afi,
260 safi_t safi)
261 {
262 time_t t_now;
263 struct bgp_damp_info *bdi;
264 int status;
265 struct bgp_damp_config *bdc = &damp[afi][safi];
266
267 if (!path->extra || !((bdi = path->extra->damp_info)))
268 return BGP_DAMP_USED;
269
270 t_now = bgp_clock();
271 bgp_path_info_unset_flag(rn, path, BGP_PATH_HISTORY);
272
273 bdi->lastrecord = BGP_RECORD_UPDATE;
274 bdi->penalty =
275 bgp_damp_decay(t_now - bdi->t_updated, bdi->penalty, bdc);
276
277 if (!CHECK_FLAG(bdi->path->flags, BGP_PATH_DAMPED)
278 && (bdi->penalty < bdc->suppress_value))
279 status = BGP_DAMP_USED;
280 else if (CHECK_FLAG(bdi->path->flags, BGP_PATH_DAMPED)
281 && (bdi->penalty < bdc->reuse_limit)) {
282 bgp_path_info_unset_flag(rn, path, BGP_PATH_DAMPED);
283 bgp_reuse_list_delete(bdi, bdc);
284 BGP_DAMP_LIST_ADD(bdc, bdi);
285 bdi->suppress_time = 0;
286 status = BGP_DAMP_USED;
287 } else
288 status = BGP_DAMP_SUPPRESSED;
289
290 if (bdi->penalty > bdc->reuse_limit / 2.0)
291 bdi->t_updated = t_now;
292 else
293 bgp_damp_info_free(bdi, 0, afi, safi);
294
295 return status;
296 }
297
298 void bgp_damp_info_free(struct bgp_damp_info *bdi, int withdraw, afi_t afi,
299 safi_t safi)
300 {
301 struct bgp_path_info *path;
302 struct bgp_damp_config *bdc = &damp[afi][safi];
303
304 if (!bdi)
305 return;
306
307 path = bdi->path;
308 path->extra->damp_info = NULL;
309
310 if (CHECK_FLAG(path->flags, BGP_PATH_DAMPED))
311 bgp_reuse_list_delete(bdi, bdc);
312 else
313 BGP_DAMP_LIST_DEL(bdc, bdi);
314
315 bgp_path_info_unset_flag(bdi->rn, path,
316 BGP_PATH_HISTORY | BGP_PATH_DAMPED);
317
318 if (bdi->lastrecord == BGP_RECORD_WITHDRAW && withdraw)
319 bgp_path_info_delete(bdi->rn, path);
320
321 XFREE(MTYPE_BGP_DAMP_INFO, bdi);
322 }
323
324 static void bgp_damp_parameter_set(int hlife, int reuse, int sup, int maxsup,
325 struct bgp_damp_config *bdc)
326 {
327 double reuse_max_ratio;
328 unsigned int i;
329 double j;
330
331 bdc->suppress_value = sup;
332 bdc->half_life = hlife;
333 bdc->reuse_limit = reuse;
334 bdc->max_suppress_time = maxsup;
335
336 /* Initialize params per bgp_damp_config. */
337 bdc->reuse_index_size = REUSE_ARRAY_SIZE;
338
339 bdc->ceiling = (int)(bdc->reuse_limit
340 * (pow(2, (double)bdc->max_suppress_time
341 / bdc->half_life)));
342
343 /* Decay-array computations */
344 bdc->decay_array_size = ceil((double)bdc->max_suppress_time / DELTA_T);
345 bdc->decay_array = XMALLOC(MTYPE_BGP_DAMP_ARRAY,
346 sizeof(double) * (bdc->decay_array_size));
347 bdc->decay_array[0] = 1.0;
348 bdc->decay_array[1] =
349 exp((1.0 / ((double)bdc->half_life / DELTA_T)) * log(0.5));
350
351 /* Calculate decay values for all possible times */
352 for (i = 2; i < bdc->decay_array_size; i++)
353 bdc->decay_array[i] =
354 bdc->decay_array[i - 1] * bdc->decay_array[1];
355
356 /* Reuse-list computations */
357 i = ceil((double)bdc->max_suppress_time / DELTA_REUSE) + 1;
358 if (i > REUSE_LIST_SIZE || i == 0)
359 i = REUSE_LIST_SIZE;
360 bdc->reuse_list_size = i;
361
362 bdc->reuse_list =
363 XCALLOC(MTYPE_BGP_DAMP_ARRAY,
364 bdc->reuse_list_size * sizeof(struct bgp_reuse_node *));
365
366 /* Reuse-array computations */
367 bdc->reuse_index = XCALLOC(MTYPE_BGP_DAMP_ARRAY,
368 sizeof(int) * bdc->reuse_index_size);
369
370 reuse_max_ratio = (double)bdc->ceiling / bdc->reuse_limit;
371 j = (exp((double)bdc->max_suppress_time / bdc->half_life) * log10(2.0));
372 if (reuse_max_ratio > j && j != 0)
373 reuse_max_ratio = j;
374
375 bdc->scale_factor =
376 (double)bdc->reuse_index_size / (reuse_max_ratio - 1);
377
378 for (i = 0; i < bdc->reuse_index_size; i++) {
379 bdc->reuse_index[i] =
380 (int)(((double)bdc->half_life / DELTA_REUSE)
381 * log10(1.0
382 / (bdc->reuse_limit
383 * (1.0
384 + ((double)i / bdc->scale_factor))))
385 / log10(0.5));
386 }
387 }
388
389 int bgp_damp_enable(struct bgp *bgp, afi_t afi, safi_t safi, time_t half,
390 unsigned int reuse, unsigned int suppress, time_t max)
391 {
392 struct bgp_damp_config *bdc = &damp[afi][safi];
393
394 if (CHECK_FLAG(bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING)) {
395 if (bdc->half_life == half && bdc->reuse_limit == reuse
396 && bdc->suppress_value == suppress
397 && bdc->max_suppress_time == max)
398 return 0;
399 bgp_damp_disable(bgp, afi, safi);
400 }
401
402 SET_FLAG(bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING);
403 bgp_damp_parameter_set(half, reuse, suppress, max, bdc);
404
405 /* Register reuse timer. */
406 thread_add_timer(bm->master, bgp_reuse_timer, bdc, DELTA_REUSE,
407 &bdc->t_reuse);
408
409 return 0;
410 }
411
412 static void bgp_damp_config_clean(struct bgp_damp_config *bdc)
413 {
414 /* Free decay array */
415 XFREE(MTYPE_BGP_DAMP_ARRAY, bdc->decay_array);
416 bdc->decay_array_size = 0;
417
418 /* Free reuse index array */
419 XFREE(MTYPE_BGP_DAMP_ARRAY, bdc->reuse_index);
420 bdc->reuse_index_size = 0;
421
422 /* Free reuse list array. */
423 XFREE(MTYPE_BGP_DAMP_ARRAY, bdc->reuse_list);
424 bdc->reuse_list_size = 0;
425 }
426
427 /* Clean all the bgp_damp_info stored in reuse_list. */
428 void bgp_damp_info_clean(afi_t afi, safi_t safi)
429 {
430 unsigned int i;
431 struct bgp_damp_info *bdi, *next;
432 struct bgp_damp_config *bdc = &damp[afi][safi];
433
434 bdc->reuse_offset = 0;
435
436 for (i = 0; i < bdc->reuse_list_size; i++) {
437 if (!bdc->reuse_list[i])
438 continue;
439
440 for (bdi = bdc->reuse_list[i]; bdi; bdi = next) {
441 next = bdi->next;
442 bgp_damp_info_free(bdi, 1, afi, safi);
443 }
444 bdc->reuse_list[i] = NULL;
445 }
446
447 for (bdi = bdc->no_reuse_list; bdi; bdi = next) {
448 next = bdi->next;
449 bgp_damp_info_free(bdi, 1, afi, safi);
450 }
451 bdc->no_reuse_list = NULL;
452 }
453
454 int bgp_damp_disable(struct bgp *bgp, afi_t afi, safi_t safi)
455 {
456 struct bgp_damp_config *bdc = &damp[afi][safi];
457 /* If it wasn't enabled, there's nothing to do. */
458 if (!CHECK_FLAG(bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING))
459 return 0;
460
461 /* Cancel reuse thread. */
462 if (bdc->t_reuse)
463 thread_cancel(bdc->t_reuse);
464 bdc->t_reuse = NULL;
465
466 /* Clean BGP dampening information. */
467 bgp_damp_info_clean(afi, safi);
468
469 /* Clear configuration */
470 bgp_damp_config_clean(bdc);
471
472 UNSET_FLAG(bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING);
473 return 0;
474 }
475
476 void bgp_config_write_damp(struct vty *vty, afi_t afi, safi_t safi)
477 {
478 if (damp[afi][safi].half_life == DEFAULT_HALF_LIFE * 60
479 && damp[afi][safi].reuse_limit == DEFAULT_REUSE
480 && damp[afi][safi].suppress_value == DEFAULT_SUPPRESS
481 && damp[afi][safi].max_suppress_time
482 == damp[afi][safi].half_life * 4)
483 vty_out(vty, " bgp dampening\n");
484 else if (damp[afi][safi].half_life != DEFAULT_HALF_LIFE * 60
485 && damp[afi][safi].reuse_limit == DEFAULT_REUSE
486 && damp[afi][safi].suppress_value == DEFAULT_SUPPRESS
487 && damp[afi][safi].max_suppress_time
488 == damp[afi][safi].half_life * 4)
489 vty_out(vty, " bgp dampening %lld\n",
490 damp[afi][safi].half_life / 60LL);
491 else
492 vty_out(vty, " bgp dampening %lld %d %d %lld\n",
493 damp[afi][safi].half_life / 60LL,
494 damp[afi][safi].reuse_limit,
495 damp[afi][safi].suppress_value,
496 damp[afi][safi].max_suppress_time / 60LL);
497 }
498
499 static const char *bgp_get_reuse_time(unsigned int penalty, char *buf,
500 size_t len, afi_t afi, safi_t safi,
501 bool use_json, json_object *json)
502 {
503 time_t reuse_time = 0;
504 struct tm *tm = NULL;
505 int time_store = 0;
506
507 if (penalty > damp[afi][safi].reuse_limit) {
508 reuse_time = (int)(DELTA_T
509 * ((log((double)damp[afi][safi].reuse_limit
510 / penalty))
511 / (log(damp[afi][safi].decay_array[1]))));
512
513 if (reuse_time > damp[afi][safi].max_suppress_time)
514 reuse_time = damp[afi][safi].max_suppress_time;
515
516 tm = gmtime(&reuse_time);
517 } else
518 reuse_time = 0;
519
520 /* Making formatted timer strings. */
521 if (reuse_time == 0) {
522 if (use_json)
523 json_object_int_add(json, "reuseTimerMsecs", 0);
524 else
525 snprintf(buf, len, "00:00:00");
526 } else if (reuse_time < ONE_DAY_SECOND) {
527 if (use_json) {
528 time_store = (3600000 * tm->tm_hour)
529 + (60000 * tm->tm_min)
530 + (1000 * tm->tm_sec);
531 json_object_int_add(json, "reuseTimerMsecs",
532 time_store);
533 } else
534 snprintf(buf, len, "%02d:%02d:%02d", tm->tm_hour,
535 tm->tm_min, tm->tm_sec);
536 } else if (reuse_time < ONE_WEEK_SECOND) {
537 if (use_json) {
538 time_store = (86400000 * tm->tm_yday)
539 + (3600000 * tm->tm_hour)
540 + (60000 * tm->tm_min)
541 + (1000 * tm->tm_sec);
542 json_object_int_add(json, "reuseTimerMsecs",
543 time_store);
544 } else
545 snprintf(buf, len, "%dd%02dh%02dm", tm->tm_yday,
546 tm->tm_hour, tm->tm_min);
547 } else {
548 if (use_json) {
549 time_store =
550 (604800000 * tm->tm_yday / 7)
551 + (86400000
552 * (tm->tm_yday - ((tm->tm_yday / 7) * 7)))
553 + (3600000 * tm->tm_hour) + (60000 * tm->tm_min)
554 + (1000 * tm->tm_sec);
555 json_object_int_add(json, "reuseTimerMsecs",
556 time_store);
557 } else
558 snprintf(buf, len, "%02dw%dd%02dh", tm->tm_yday / 7,
559 tm->tm_yday - ((tm->tm_yday / 7) * 7),
560 tm->tm_hour);
561 }
562
563 return buf;
564 }
565
566 void bgp_damp_info_vty(struct vty *vty, struct bgp_path_info *path, afi_t afi,
567 safi_t safi, json_object *json_path)
568 {
569 struct bgp_damp_info *bdi;
570 time_t t_now, t_diff;
571 char timebuf[BGP_UPTIME_LEN];
572 int penalty;
573 struct bgp_damp_config *bdc = &damp[afi][safi];
574
575 if (!path->extra)
576 return;
577
578 /* BGP dampening information. */
579 bdi = path->extra->damp_info;
580
581 /* If dampening is not enabled or there is no dampening information,
582 return immediately. */
583 if (!bdc || !bdi)
584 return;
585
586 /* Calculate new penalty. */
587 t_now = bgp_clock();
588 t_diff = t_now - bdi->t_updated;
589 penalty = bgp_damp_decay(t_diff, bdi->penalty, bdc);
590
591 if (json_path) {
592 json_object_int_add(json_path, "dampeningPenalty", penalty);
593 json_object_int_add(json_path, "dampeningFlapCount", bdi->flap);
594 peer_uptime(bdi->start_time, timebuf, BGP_UPTIME_LEN, 1,
595 json_path);
596
597 if (CHECK_FLAG(path->flags, BGP_PATH_DAMPED)
598 && !CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
599 bgp_get_reuse_time(penalty, timebuf, BGP_UPTIME_LEN,
600 afi, safi, 1, json_path);
601 } else {
602 vty_out(vty,
603 " Dampinfo: penalty %d, flapped %d times in %s",
604 penalty, bdi->flap,
605 peer_uptime(bdi->start_time, timebuf, BGP_UPTIME_LEN, 0,
606 json_path));
607
608 if (CHECK_FLAG(path->flags, BGP_PATH_DAMPED)
609 && !CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
610 vty_out(vty, ", reuse in %s",
611 bgp_get_reuse_time(penalty, timebuf,
612 BGP_UPTIME_LEN, afi, safi, 0,
613 json_path));
614
615 vty_out(vty, "\n");
616 }
617 }
618
619 const char *bgp_damp_reuse_time_vty(struct vty *vty, struct bgp_path_info *path,
620 char *timebuf, size_t len, afi_t afi,
621 safi_t safi, bool use_json,
622 json_object *json)
623 {
624 struct bgp_damp_info *bdi;
625 time_t t_now, t_diff;
626 int penalty;
627 struct bgp_damp_config *bdc = &damp[afi][safi];
628
629 if (!path->extra)
630 return NULL;
631
632 /* BGP dampening information. */
633 bdi = path->extra->damp_info;
634
635 /* If dampening is not enabled or there is no dampening information,
636 return immediately. */
637 if (!bdc || !bdi)
638 return NULL;
639
640 /* Calculate new penalty. */
641 t_now = bgp_clock();
642 t_diff = t_now - bdi->t_updated;
643 penalty = bgp_damp_decay(t_diff, bdi->penalty, bdc);
644
645 return bgp_get_reuse_time(penalty, timebuf, len, afi, safi, use_json,
646 json);
647 }
648
649 int bgp_show_dampening_parameters(struct vty *vty, afi_t afi, safi_t safi)
650 {
651 struct bgp *bgp;
652 bgp = bgp_get_default();
653
654 if (bgp == NULL) {
655 vty_out(vty, "No BGP process is configured\n");
656 return CMD_WARNING;
657 }
658
659 if (CHECK_FLAG(bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING)) {
660 vty_out(vty, "Half-life time: %lld min\n",
661 (long long)damp[afi][safi].half_life / 60);
662 vty_out(vty, "Reuse penalty: %d\n",
663 damp[afi][safi].reuse_limit);
664 vty_out(vty, "Suppress penalty: %d\n",
665 damp[afi][safi].suppress_value);
666 vty_out(vty, "Max suppress time: %lld min\n",
667 (long long)damp[afi][safi].max_suppress_time / 60);
668 vty_out(vty, "Max suppress penalty: %u\n",
669 damp[afi][safi].ceiling);
670 vty_out(vty, "\n");
671 } else
672 vty_out(vty, "dampening not enabled for %s\n",
673 afi == AFI_IP ? "IPv4" : "IPv6");
674
675 return CMD_SUCCESS;
676 }