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