📄 bgp_damp.c
字号:
if (bdi->lastrecord == BGP_RECORD_UPDATE) return 1; else return 0; } } else { t_diff = t_now - bdi->t_updated; bdi->penalty = bgp_damp_decay (t_diff, bdi->penalty); if (bdi->penalty <= damp->reuse_limit / 2.0) { /* release the bdi, bdi->binfo. */ bgp_damp_info_free (bdi, 1); return 0; } else bdi->t_updated = t_now; } return 0;}voidbgp_damp_info_free (struct bgp_damp_info *bdi, int withdraw){ struct bgp_info *binfo; void bgp_info_delete (struct bgp_node *, struct bgp_info *); void bgp_info_free (struct bgp_info *); if (! bdi) return; binfo = bdi->binfo; binfo->damp_info = NULL; if (CHECK_FLAG (binfo->flags, BGP_INFO_DAMPED)) bgp_reuse_list_delete (bdi); else BGP_DAMP_LIST_DEL (damp, bdi); UNSET_FLAG (binfo->flags, BGP_INFO_DAMPED); UNSET_FLAG (binfo->flags, BGP_INFO_HISTORY); if (bdi->lastrecord == BGP_RECORD_WITHDRAW && withdraw) { bgp_info_delete (bdi->rn, binfo); bgp_info_free (binfo); bgp_unlock_node (bdi->rn); } XFREE (MTYPE_BGP_DAMP_INFO, bdi);}voidbgp_damp_parameter_set (int hlife, int reuse, int sup, int maxsup){ double reuse_max_ratio; int i; double j; damp->suppress_value = sup; damp->half_life = hlife; damp->reuse_limit = reuse; damp->max_suppress_time = maxsup; /* Initialize params per bgp_damp_config. */ damp->reuse_index_size = REUSE_ARRAY_SIZE; damp->ceiling = (int)(damp->reuse_limit * (pow(2, (double)damp->max_suppress_time/damp->half_life))); /* Decay-array computations */ damp->decay_array_size = ceil ((double) damp->max_suppress_time / DELTA_T); damp->decay_array = XMALLOC (MTYPE_BGP_DAMP_ARRAY, sizeof(double) * (damp->decay_array_size)); damp->decay_array[0] = 1.0; damp->decay_array[1] = exp ((1.0/((double)damp->half_life/DELTA_T)) * log(0.5)); /* Calculate decay values for all possible times */ for (i = 2; i < damp->decay_array_size; i++) damp->decay_array[i] = damp->decay_array[i-1] * damp->decay_array[1]; /* Reuse-list computations */ i = ceil ((double)damp->max_suppress_time / DELTA_REUSE) + 1; if (i > REUSE_LIST_SIZE || i == 0) i = REUSE_LIST_SIZE; damp->reuse_list_size = i; damp->reuse_list = XCALLOC (MTYPE_BGP_DAMP_ARRAY, damp->reuse_list_size * sizeof (struct bgp_reuse_node *)); memset (damp->reuse_list, 0x00, damp->reuse_list_size * sizeof (struct bgp_reuse_node *)); /* Reuse-array computations */ damp->reuse_index = XMALLOC (MTYPE_BGP_DAMP_ARRAY, sizeof(int) * damp->reuse_index_size); memset (damp->reuse_index, 0x00, damp->reuse_list_size * sizeof (int)); reuse_max_ratio = (double)damp->ceiling/damp->reuse_limit; j = (exp((double)damp->max_suppress_time/damp->half_life) * log10(2.0)); if ( reuse_max_ratio > j && j != 0 ) reuse_max_ratio = j; damp->scale_factor = (double)damp->reuse_index_size/(reuse_max_ratio - 1); for (i = 0; i < damp->reuse_index_size; i++) { damp->reuse_index[i] = (int)(((double)damp->half_life / DELTA_REUSE) * log10 (1.0 / (damp->reuse_limit * ( 1.0 + ((double)i/damp->scale_factor)))) / log10(0.5)); }}intbgp_damp_enable (struct bgp *bgp, afi_t afi, safi_t safi, int half, int reuse, int suppress, int max){ if (CHECK_FLAG (bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING)) { if (damp->half_life == half && damp->reuse_limit == reuse && damp->suppress_value == suppress && damp->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); /* Register reuse timer. */ if (! damp->t_reuse) damp->t_reuse = thread_add_timer (master, bgp_reuse_timer, NULL, DELTA_REUSE); return 0;}voidbgp_damp_config_clean (struct bgp_damp_config *damp){ /* Free decay array */ XFREE (MTYPE_BGP_DAMP_ARRAY, damp->decay_array); /* Free reuse index array */ XFREE (MTYPE_BGP_DAMP_ARRAY, damp->reuse_index); /* Free reuse list array. */ XFREE (MTYPE_BGP_DAMP_ARRAY, damp->reuse_list);}/* Clean all the bgp_damp_info stored in reuse_list. */voidbgp_damp_info_clean (){ int i; struct bgp_damp_info *bdi, *next; damp->reuse_offset = 0; for (i = 0; i < damp->reuse_list_size; i++) { if (! damp->reuse_list[i]) continue; for (bdi = damp->reuse_list[i]; bdi; bdi = next) { next = bdi->next; bgp_damp_info_free (bdi, 1); } damp->reuse_list[i] = NULL; } for (bdi = damp->no_reuse_list; bdi; bdi = next) { next = bdi->next; bgp_damp_info_free (bdi, 1); } damp->no_reuse_list = NULL;}intbgp_damp_disable (struct bgp *bgp, afi_t afi, safi_t safi){ /* Cancel reuse thread. */ if (damp->t_reuse ) thread_cancel (damp->t_reuse); damp->t_reuse = NULL; /* Clean BGP dampening information. */ bgp_damp_info_clean (); /* Clear configuration */ bgp_damp_config_clean (&bgp_damp_cfg); UNSET_FLAG (bgp->af_flags[afi][safi], BGP_CONFIG_DAMPENING); return 0;}intbgp_config_write_damp (struct vty *vty){ if (&bgp_damp_cfg) { if (bgp_damp_cfg.half_life == DEFAULT_HALF_LIFE*60 && bgp_damp_cfg.reuse_limit == DEFAULT_REUSE && bgp_damp_cfg.suppress_value == DEFAULT_SUPPRESS && bgp_damp_cfg.max_suppress_time == bgp_damp_cfg.half_life*4) vty_out (vty, " bgp dampening%s", VTY_NEWLINE); else if (bgp_damp_cfg.half_life != DEFAULT_HALF_LIFE*60 && bgp_damp_cfg.reuse_limit == DEFAULT_REUSE && bgp_damp_cfg.suppress_value == DEFAULT_SUPPRESS && bgp_damp_cfg.max_suppress_time == bgp_damp_cfg.half_life*4) vty_out (vty, " bgp dampening %d%s", bgp_damp_cfg.half_life/60, VTY_NEWLINE); else vty_out (vty, " bgp dampening %d %d %d %d%s", bgp_damp_cfg.half_life/60, bgp_damp_cfg.reuse_limit, bgp_damp_cfg.suppress_value, bgp_damp_cfg.max_suppress_time/60, VTY_NEWLINE); return 1; } return 0;}#define BGP_UPTIME_LEN 25char *bgp_get_reuse_time (int penalty, char *buf, size_t len){ time_t reuse_time = 0; struct tm *tm = NULL; if (penalty > damp->reuse_limit) { reuse_time = (int) (DELTA_T * ((log((double)damp->reuse_limit/penalty))/(log(damp->decay_array[1])))); if (reuse_time > damp->max_suppress_time) reuse_time = damp->max_suppress_time; tm = gmtime (&reuse_time); } else reuse_time = 0; /* Making formatted timer strings. */#define ONE_DAY_SECOND 60*60*24#define ONE_WEEK_SECOND 60*60*24*7 if (reuse_time == 0) snprintf (buf, len, "00:00:00"); else if (reuse_time < ONE_DAY_SECOND) snprintf (buf, len, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec); else if (reuse_time < ONE_WEEK_SECOND) snprintf (buf, len, "%dd%02dh%02dm", tm->tm_yday, tm->tm_hour, tm->tm_min); else snprintf (buf, len, "%02dw%dd%02dh", tm->tm_yday/7, tm->tm_yday - ((tm->tm_yday/7) * 7), tm->tm_hour); return buf;} voidbgp_damp_info_vty (struct vty *vty, struct bgp_info *binfo) { struct bgp_damp_info *bdi; time_t t_now, t_diff; char timebuf[BGP_UPTIME_LEN]; int penalty; /* BGP dampening information. */ bdi = binfo->damp_info; /* If dampening is not enabled or there is no dampening information, return immediately. */ if (! damp || ! bdi) return; /* Calculate new penalty. */ t_now = time (NULL); t_diff = t_now - bdi->t_updated; penalty = bgp_damp_decay (t_diff, bdi->penalty); vty_out (vty, " Dampinfo: penalty %d, flapped %d times in %s", penalty, bdi->flap, peer_uptime (bdi->start_time, timebuf, BGP_UPTIME_LEN)); if (CHECK_FLAG (binfo->flags, BGP_INFO_DAMPED) && ! CHECK_FLAG (binfo->flags, BGP_INFO_HISTORY)) vty_out (vty, ", reuse in %s", bgp_get_reuse_time (penalty, timebuf, BGP_UPTIME_LEN)); vty_out (vty, "%s", VTY_NEWLINE);}char *bgp_damp_reuse_time_vty (struct vty *vty, struct bgp_info *binfo){ struct bgp_damp_info *bdi; time_t t_now, t_diff; char timebuf[BGP_UPTIME_LEN]; int penalty; /* BGP dampening information. */ bdi = binfo->damp_info; /* If dampening is not enabled or there is no dampening information, return immediately. */ if (! damp || ! bdi) return NULL; /* Calculate new penalty. */ t_now = time (NULL); t_diff = t_now - bdi->t_updated; penalty = bgp_damp_decay (t_diff, bdi->penalty); return bgp_get_reuse_time (penalty, timebuf, BGP_UPTIME_LEN);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -