/* * 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 #include #include #include #include #include vlib_log_main_t log_main = { .default_log_level = VLIB_LOG_LEVEL_NOTICE, .default_syslog_log_level = VLIB_LOG_LEVEL_WARNING, .unthrottle_time = 3, .size = 512, .add_to_elog = 1, .default_rate_limit = 50, }; /* *INDENT-OFF* */ VLIB_REGISTER_LOG_CLASS (log_log, static) = { .class_name = "log", }; /* *INDENT-ON* */ static const int colors[] = { [VLIB_LOG_LEVEL_EMERG] = 1, /* red */ [VLIB_LOG_LEVEL_ALERT] = 1, /* red */ [VLIB_LOG_LEVEL_CRIT] = 1, /* red */ [VLIB_LOG_LEVEL_ERR] = 1, /* red */ [VLIB_LOG_LEVEL_WARNING] = 3, /* yellow */ [VLIB_LOG_LEVEL_NOTICE] = 2, /* green */ [VLIB_LOG_LEVEL_INFO] = 4, /* blue */ [VLIB_LOG_LEVEL_DEBUG] = 6, /* cyan */ }; static const int log_level_to_syslog_priority[] = { [VLIB_LOG_LEVEL_EMERG] = LOG_EMERG, [VLIB_LOG_LEVEL_ALERT] = LOG_ALERT, [VLIB_LOG_LEVEL_CRIT] = LOG_CRIT, [VLIB_LOG_LEVEL_ERR] = LOG_ERR, [VLIB_LOG_LEVEL_WARNING] = LOG_WARNING, [VLIB_LOG_LEVEL_NOTICE] = LOG_NOTICE, [VLIB_LOG_LEVEL_INFO] = LOG_INFO, [VLIB_LOG_LEVEL_DEBUG] = LOG_DEBUG, [VLIB_LOG_LEVEL_DISABLED] = LOG_DEBUG, }; int last_log_entry () { vlib_log_main_t *lm = &log_main; int i; i = lm->next - lm->count; if (i < 0) i += lm->size; return i; } static vlib_log_class_data_t * get_class_data (vlib_log_class_t ci) { vlib_log_main_t *lm = &log_main; return vec_elt_at_index (lm->classes, (ci >> 16)); } static vlib_log_subclass_data_t * get_subclass_data (vlib_log_class_t ci) { vlib_log_class_data_t *c = get_class_data (ci); return vec_elt_at_index (c->subclasses, (ci & 0xffff)); } u8 * format_vlib_log_class (u8 * s, va_list * args) { vlib_log_class_t ci = va_arg (*args, vlib_log_class_t); vlib_log_class_data_t *c = get_class_data (ci); vlib_log_subclass_data_t *sc = get_subclass_data (ci); if (sc->name) return format (s, "%v/%v", c->name, sc->name); else return format (s, "%v", c->name, 0); } u8 * format_indent (u8 * s, va_list * args) { u8 *v = va_arg (*args, u8 *); u32 indent = va_arg (*args, u32); u8 *c; /* *INDENT-OFF* */ vec_foreach (c, v) { vec_add (s, c, 1); if (c[0] == '\n') for (u32 i = 0; i < indent; i++) vec_add1 (s, (u8) ' '); } /* *INDENT-ON* */ return s; } static int log_level_is_enabled (vlib_log_level_t level, vlib_log_level_t configured) { if (configured == VLIB_LOG_LEVEL_DISABLED) return 0; if (level > configured) return 0; return 1; } void vlib_log (vlib_log_level_t level, vlib_log_class_t class, char *fmt, ...) { vlib_main_t *vm = vlib_get_main (); vlib_log_main_t *lm = &log_main; vlib_log_entry_t *e; vlib_log_subclass_data_t *sc = get_subclass_data (class); va_list va; f64 t = vlib_time_now (vm); f64 delta = t - sc->last_event_timestamp; int log_enabled = log_level_is_enabled (level, sc->level); int syslog_enabled = log_level_is_enabled (level, sc->syslog_level); u8 *s = 0; /* make sure we are running on the main thread to avoid use in dataplane code, for dataplane logging consider use of event-logger */ ASSERT (vlib_get_thread_index () == 0); if ((log_enabled || syslog_enabled) == 0) return; vec_validate (lm->entries, lm->size); if ((delta > lm->unthrottle_time) || (sc->is_throttling == 0 && (delta > 1))) { sc->last_event_timestamp = t; sc->last_sec_count = 0; sc->is_throttling = 0; } else { sc->last_sec_count++; if (sc->last_sec_count > sc->rate_limit) return; else if (sc->last_sec_count == sc->rate_limit) { vec_reset_length (s); s = format (s, "--- message(s) throttled ---"); sc->is_throttling = 1; } } if (s == 0) { va_start (va, fmt); s = va_format (s, fmt, &va); va_end (va); } if (syslog_enabled) { u8 *l = 0; if (unix_main.flags & (UNIX_FLAG_INTERACTIVE | UNIX_FLAG_NOSYSLOG)) { int indent = 0; int with_colors = (unix_main.flags & UNIX_FLAG_NOCOLOR) == 0; u8 *fmt; if (with_colors) { l = format (l, "\x1b[%um", 90 + colors[level]); indent = vec_len (l); } fmt = format (0, "%%-%uU [%%-6U]: ", lm->max_class_name_length); vec_terminate_c_string (fmt); l = format (l, (char *) fmt, format_vlib_log_class, class, format_vlib_log_level, level); vec_free (fmt); indent = vec_len (l) - indent; if (with_colors) l = format (l, "\x1b[0m"); l = format (l, "%U", format_indent, s, indent); fformat (stderr, "%v\n", l); fflush (stderr); } else { l = format (l, "%U", format_vlib_log_class, class); int prio = log_level_to_syslog_priority[level]; int is_term = vec_c_string_is_terminated (l) ? 1 : 0; syslog (prio, "%.*s: %.*s", (int) vec_len (l), l, (int) vec_len (s) - is_term, s); } vec_free (l); } if (log_enabled) { e = vec_elt_at_index (lm->entries, lm->next); vec_free (e->string); e->level = level; e->class = class; e->string = s; e->timestamp = t; s = 0; if (lm->add_to_elog) { /* *INDENT-OFF* */ ELOG_TYPE_DECLARE(ee) = { .format = "log-%s: %s", .format_args = "t4T4", .n_enum_strings = 9, .enum_strings = { "emerg", "alert", "crit", "err", "warn", "notice", "info", "debug", "disabled", }, }; struct { u32 log_level; u32 string_index; } *ed; /* *INDENT-ON* */ ed = ELOG_DATA (&vlib_global_main.elog_main, ee); ed->log_level = level; ed->string_index = elog_string (&vlib_global_main.elog_main, "%v", e->string); } lm->next = (lm->next + 1) % lm->size; if (lm->size > lm->count) lm->count++; } vec_free (s); } static vlib_log_class_t vlib_log_register_class_internal (char *class, char *subclass, u32 limit) { vlib_log_main_t *lm = &log_main; vlib_log_class_data_t *c = NULL; vlib_log_subclass_data_t *s; vlib_log_class_data_t *tmp; vlib_log_class_config_t *cc = 0, *scc = 0; uword *p; u8 *str; u32 length = 0; if ((p = hash_get_mem (lm->config_index_by_name, class))) cc = vec_elt_at_index (lm->configs, p[0]); str = format (0, "%s/%s%c", class, subclass, 0); if ((p = hash_get_mem (lm->config_index_by_name, (char *) str))) scc = vec_elt_at_index (lm->configs, p[0]); vec_free (str); vec_foreach (tmp, lm->classes) { if (vec_len (tmp->name) != strlen (class)) continue; if (!memcmp (class, tmp->name, vec_len (tmp->name))) { c = tmp; break; } } if (!c) { vec_add2 (lm->classes, c, 1); c->index = c - lm->classes; c->name = format (0, "%s", class); } length = vec_len (c->name); vec_add2 (c->subclasses, s, 1); s->index = s - c->subclasses; s->name = subclass ? format (0, "%s", subclass) : 0; if (scc && scc->rate_limit != ~0) s->rate_limit = scc->rate_limit; else if (cc && cc->rate_limit != ~0) s->rate_limit = cc->rate_limit; else if (limit) s->rate_limit = limit; else s->rate_limit = lm->default_rate_limit; if (scc && scc->level != ~0) s->level = scc->level; else if (cc && cc->level != ~0) s->level = cc->level; else s->level = lm->default_log_level; if (scc && scc->syslog_level != ~0) s->syslog_level = scc->syslog_level; else if (cc && cc->syslog_level != ~0) s->syslog_level = cc->syslog_level; else s->syslog_level = lm->default_sysl
/*
 *------------------------------------------------------------------
 * Copyright (c) 2017 Intel 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 <pppoe/pppoe.h>

int
pppoe_add_del_cp (u32 cp_if_index, u8 is_add)
{
  pppoe_main_t *pem = &pppoe_main;

  if (cp_if_index == 0)
    {
      return ~0;
    }

  vnet_feature_enable_disable ("device-input", "pppoe-input",
			       cp_if_index, is_add, 0, 0);

  if (is_add)
    {
      pem->cp_if_index = cp_if_index;
    }
  else
    {
      pem->cp_if_index = ~0;
    }
  return 0;
}

static clib_error_t *
pppoe_add_del_cp_command_fn (vlib_main_t * vm,
			     unformat_input_t * input,
			     vlib_cli_command_t * cmd)
{
  unformat_input_t _line_input, *line_input = &_line_input;
  pppoe_main_t *pem = &pppoe_main;
  u8 is_add = 1;
  u8 cp_if_index_set = 0;
  u32 cp_if_index = 0;
  clib_error_t *error = NULL;

  /* Get a line of input. */
  if (!unformat_user (input, unformat_line_input,