summaryrefslogtreecommitdiffstats
path: root/src/plugins/cdp/cdp.h
diff options
context:
space:
mode:
authorSteven Luong <sluong@cisco.com>2021-02-08 23:48:30 -0800
committerDave Wallace <dwallacelf@gmail.com>2021-02-09 22:40:34 +0000
commit16f08657758db0f32b60cc88644b3a1c8fc28cbc (patch)
treec382af5be9c7a2598f7bc96efd92ee22698edf82 /src/plugins/cdp/cdp.h
parent4b47ee26cba610b26bbfc088736846541bee7be3 (diff)
l2: crash on l2_input_is_xconnect
Running vpp without any interface configured and then invoking the binary-api l2_xconnect_dump causes vpp to crash in l2_input_is_xconnect due to l2input_main.configs has no memory allocated to it, not even for the local interface which exists all the times. The reason that l2input_main.configs has no memory allocated to it was due to gerrit patch 29232 which took out a line in l2input_init /* Create the config vector */ vec_validate (mp->configs, 100); The fix is to iterate through l2input_main.configs for each interface in l2 to call l2_input_is_xconnect when dumping l2_xconnect interfaces. Type: fix Fixes: gerrit 29232 Signed-off-by: Steven Luong <sluong@cisco.com> Change-Id: I8d9cba4b7eba4c2e0c60887c4fd57d5ec3b06d3b
Diffstat (limited to 'src/plugins/cdp/cdp.h')
0 files changed, 0 insertions, 0 deletions
'#n162'>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
/*
 * Copyright (c) 2018 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vppinfra/time_range.h>

__clib_export void
clib_timebase_init (clib_timebase_t * tb, i32 timezone_offset_in_hours,
		    clib_timebase_daylight_time_t daylight_type,
		    clib_time_t * clib_time)
{
  clib_memset (tb, 0, sizeof (*tb));

  if (clib_time == 0)
    {
      tb->clib_time = clib_mem_alloc_aligned
	(sizeof (*clib_time), CLIB_CACHE_LINE_BYTES);
      memset (tb->clib_time, 0, sizeof (*clib_time));
      clib_time_init (tb->clib_time);
    }
  else
    tb->clib_time = clib_time;

  tb->timezone_offset = ((f64) (timezone_offset_in_hours)) * 3600.0;
  tb->daylight_time_type = daylight_type;
  switch (tb->daylight_time_type)
    {
    case CLIB_TIMEBASE_DAYLIGHT_NONE:
      tb->summer_offset = 0.0;
      break;
    case CLIB_TIMEBASE_DAYLIGHT_USA:
      tb->summer_offset = 3600.0;
      break;
    default:
      clib_warning ("unknown daylight type %d", tb->daylight_time_type);
      tb->daylight_time_type = CLIB_TIMEBASE_DAYLIGHT_NONE;
      tb->summer_offset = 0.0;
    }
}

const static u32 days_per_month[] = {
  31,				/* Jan */
  28,				/* Feb */
  31,				/* Mar */
  30,				/* Apr */
  31,				/* May */
  30,				/* Jun */
  31,				/* Jul */
  31,				/* Aug */
  30,				/* Sep */
  31,				/* Oct */
  30,				/* Nov */
  31,				/* Dec */
};

const static char *month_short_names[] = {
  "Jan",
  "Feb",
  "Mar",
  "Apr",
  "May",
  "Jun",
  "Jul",
  "Aug",
  "Sep",
  "Oct",
  "Nov",
  "Dec",
};

const static char *day_names_epoch_order[] = {
  "Thu",
  "Fri",
  "Sat",
  "Sun",
  "Mon",
  "Tue",
  "Wed",
};

const static char *day_names_calendar_order[] = {
  "Sun",
  "Mon",
  "Tue",
  "Wed",
  "Thu",
  "Fri",
  "Sat",
};


__clib_export void
clib_timebase_time_to_components (f64 now, clib_timebase_component_t * cp)
{
  u32 year, month, hours, minutes, seconds, nanoseconds;
  u32 days_in_year, days_in_month, day_of_month;
  u32 days_since_epoch;
  u32 day_name_index;

  /* Unix epoch is 1/1/1970 00:00:00.00, a Thursday */

  year = 1970;
  days_since_epoch = 0;

  do
    {
      days_in_year = clib_timebase_is_leap_year (year) ? 366 : 365;
      days_since_epoch += days_in_year;
      now = now - ((f64) days_in_year) * 86400.0;
      year++;
    }
  while (now > 0.0);

  days_since_epoch -= days_in_year;
  now += ((f64) days_in_year) * 86400;
  year--;

  month = 0;

  do
    {
      days_in_month = days_per_month[month];
      if (month == 1 && clib_timebase_is_leap_year (year))
	days_in_month++;

      days_since_epoch += days_in_month;
      now = now - ((f64) days_in_month) * 86400.0;
      month++;
    }
  while (now > 0.0);

  days_since_epoch -= days_in_month;
  now += ((f64) days_in_month) * 86400;
  month--;

  day_of_month = 1;
  do
    {
      now = now - 86400;
      day_of_month++;
      days_since_epoch++;
    }
  while (now > 0.0);

  day_of_month--;
  days_since_epoch--;
  now += 86400.0;

  day_name_index = days_since_epoch % 7;

  hours = (u32) (now / (3600.0));
  now -= (f64) (hours * 3600);

  minutes = (u32) (now / 60.0);
  now -= (f64) (minutes * 60);

  seconds = (u32) (now);
  now -= (f64) (seconds);

  nanoseconds = (f64) (now * 1e9);

  cp->year = year;
  cp->month = month;
  cp->day = day_of_month;
  cp->day_name_index = day_name_index;
  cp->hour = hours;
  cp->minute = minutes;
  cp->second = seconds;
  cp->nanosecond = nanoseconds;
  cp->fractional_seconds = now;
}

__clib_export f64
clib_timebase_components_to_time (clib_timebase_component_t * cp)
{
  f64 now = 0;
  u32 year, days_in_year, month, days_in_month;

  year = 1970;

  while (year < cp->year)
    {
      days_in_year = clib_timebase_is_leap_year (year) ? 366 : 365;
      now += ((f64) days_in_year) * 86400.0;
      year++;
    }

  month = 0;

  while (month < cp->month)
    {
      days_in_month = days_per_month[month];
      if (month == 1 && clib_timebase_is_leap_year (year))
	days_in_month++;

      now += ((f64) days_in_month) * 86400.0;
      month++;
    }

  now += ((f64) cp->day - 1) * 86400.0;
  now += ((f64) cp->hour) * 3600.0;
  now += ((f64) cp->minute) * 60.0;
  now += ((f64) cp->second);
  now += ((f64) cp->nanosecond) * 1e-9;

  return (now);
}

__clib_export f64
clib_timebase_find_sunday_midnight (f64 start_time)
{
  clib_timebase_component_t _c, *cp = &_c;

  clib_timebase_time_to_components (start_time, cp);

  /* back up to midnight */
  cp->hour = cp->minute = cp->second = 0;

  start_time = clib_timebase_components_to_time (cp);

  while (cp->day_name_index != 3 /* sunday */ )
    {
      /* Back up one day */
      start_time -= 86400.0;
      clib_timebase_time_to_components (start_time, cp);
    }
  /* Clean up residual fraction */
  start_time -= cp->fractional_seconds;
  start_time += 1e-6;		/* 1us inside Sunday  */

  return (start_time);
}

f64
clib_timebase_offset_from_sunday (u8 * day)
{
  int i;

  for (i = 0; i < ARRAY_LEN (day_names_calendar_order); i++)
    {
      if (!strncmp ((char *) day, day_names_calendar_order[i], 3))
	return ((f64) i) * 86400.0;
    }
  return 0.0;
}


__clib_export u8 *
format_clib_timebase_time (u8 * s, va_list * args)
{
  f64 now = va_arg (*args, f64);
  clib_timebase_component_t _c, *cp = &_c;

  clib_timebase_time_to_components (now, cp);

  s = format (s, "%s, %u %s %u %u:%02u:%02u",
	      day_names_epoch_order[cp->day_name_index],
	      cp->day,
	      month_short_names[cp->month],
	      cp->year, cp->hour, cp->minute, cp->second);
  return (s);
}

uword
unformat_clib_timebase_range_hms (unformat_input_t * input, va_list * args)
{
  clib_timebase_range_t *rp = va_arg (*args, clib_timebase_range_t *);
  clib_timebase_component_t _c, *cp = &_c;
  u32 start_hour, start_minute, start_second;
  u32 end_hour, end_minute, end_second;

  start_hour = start_minute = start_second
    = end_hour = end_minute = end_second = 0;

  if (unformat (input, "%u:%u:%u - %u:%u:%u",
		&start_hour, &start_minute, &start_second,
		&end_hour, &end_minute, &end_second))
    ;
  else if (unformat (input, "%u:%u - %u:%u",
		     &start_hour, &start_minute, &end_hour, &end_minute))
    ;
  else if (unformat (input, "%u - %u", &start_hour, &end_hour))
    ;
  else
    return 0;

  clib_timebase_time_to_components (1e-6, cp);

  cp->hour = start_hour;
  cp->minute = start_minute;
  cp->second = start_second;

  rp->start = clib_timebase_components_to_time (cp);

  cp->hour = end_hour;
  cp->minute = end_minute;
  cp->second = end_second;

  rp->end = clib_timebase_components_to_time (cp);

  return 1;
}

__clib_export uword
unformat_clib_timebase_range_vector (unformat_input_t * input, va_list * args)
{
  clib_timebase_range_t **rpp = va_arg (*args, clib_timebase_range_t **);
  clib_timebase_range_t _tmp, *tmp = &_tmp;
  clib_timebase_range_t *rp, *new_rp;
  int day_range_match = 0;
  int time_range_match = 0;
  f64 range_start_time_offset;
  f64 range_end_time_offset;
  f64 now;
  u8 *start_day = 0, *end_day = 0;

  rp = *rpp;

  while (1)
    {
      if (!day_range_match
	  && unformat (input, "%s - %s", &start_day, &end_day))
	{
	  range_start_time_offset
	    = clib_timebase_offset_from_sunday (start_day);
	  range_end_time_offset = clib_timebase_offset_from_sunday (end_day);
	  vec_free (start_day);
	  vec_free (end_day);
	  day_range_match = 1;
	  time_range_match = 0;
	}
      else if (!day_range_match && unformat (input, "%s", &start_day))
	{
	  range_start_time_offset
	    = clib_timebase_offset_from_sunday (start_day);
	  range_end_time_offset = range_start_time_offset + 86399.0;
	  day_range_match = 1;
	  vec_free (start_day);
	  day_range_match = 1;
	  time_range_match = 0;
	}
      else if (day_range_match &&
	       unformat (input, "%U", unformat_clib_timebase_range_hms, tmp))
	{
	  /* Across the week... */
	  for (now = range_start_time_offset; now <= range_end_time_offset;
	       now += 86400.0)
	    {
	      vec_add2 (rp, new_rp, 1);
	      new_rp->start = now + tmp->start;
	      new_rp->end = now + tmp->end;
	    }
	  day_range_match = 0;
	  time_range_match = 1;
	}
      else if (time_range_match)
	break;
      else
	{
	  vec_free (rp);
	  *rpp = 0;
	  return 0;
	}
    }

  if (time_range_match)
    {
      *rpp = rp;
      return 1;
    }
  else
    {
      vec_free (rp);
      *rpp = 0;
      return 0;
    }
}

__clib_export f64
clib_timebase_summer_offset (clib_timebase_t * tb, f64 now)
{
  clib_timebase_component_t _c, *cp = &_c;
  f64 second_sunday_march_2am;
  f64 first_sunday_november_2am;

  if (PREDICT_TRUE
      (now >= tb->cached_year_start && now <= tb->cached_year_end))
    {
      if (now >= tb->cached_summer_start && now <= tb->cached_summer_end)
	return tb->summer_offset;
      else
	return (0.0);
    }

  clib_timebase_time_to_components (now, cp);

  cp->month = 0;
  cp->day = 1;
  cp->hour = 0;
  cp->minute = 0;
  cp->second = 1;

  tb->cached_year_start = clib_timebase_components_to_time (cp);

  cp->year += 1;

  tb->cached_year_end = clib_timebase_components_to_time (cp);

  cp->year -= 1;

  /* Search for the second sunday in march, 2am */
  cp->month = 2;
  cp->day = 1;
  cp->hour = 2;
  cp->second = 0;
  cp->nanosecond = 1;

  /* March 1st will never be the second sunday... */
  second_sunday_march_2am = clib_timebase_components_to_time (cp);
  cp->day_name_index = 0;

  /* Find the first sunday */
  do
    {
      clib_timebase_time_to_components (second_sunday_march_2am, cp);
      second_sunday_march_2am += 86400.0;
    }
  while (cp->day_name_index != 3 /* sunday */ );

  /* Find the second sunday */
  do
    {
      clib_timebase_time_to_components (second_sunday_march_2am, cp);
      second_sunday_march_2am += 86400.0;
    }
  while (cp->day_name_index != 3 /* sunday */ );

  second_sunday_march_2am -= 86400.0;

  tb->cached_summer_start = second_sunday_march_2am;

  /* Find the first sunday in November, which can easily be 11/1 */
  cp->month = 10;
  cp->day = 1;

  first_sunday_november_2am = clib_timebase_components_to_time (cp);
  clib_timebase_time_to_components (first_sunday_november_2am, cp);

  while (cp->day_name_index != 3 /* sunday */ )
    {
      first_sunday_november_2am += 86400.0;
      clib_timebase_time_to_components (first_sunday_november_2am, cp);
    }

  tb->cached_summer_end = first_sunday_november_2am;

  if (now >= tb->cached_summer_start && now <= tb->cached_summer_end)
    return tb->summer_offset;
  else
    return (0.0);
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */