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