/* * Copyright (c) 2015 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. */ /* Copyright (c) 2005,2009 Eliot Dresselhaus Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include #include #include static inline void elog_lock (elog_main_t * em) { if (PREDICT_FALSE (em->lock != 0)) while (clib_atomic_test_and_set (em->lock)) CLIB_PAUSE (); } static inline void elog_unlock (elog_main_t * em) { if (PREDICT_FALSE (em->lock != 0)) { clib_atomic_release (em->lock); } } /* Non-inline version. */ void * elog_event_data (elog_main_t * em, elog_event_type_t * type, elog_track_t * track, u64 cpu_time) { return elog_event_data_inline (em, type, track, cpu_time); } static void new_event_type (elog_main_t * em, uword i) { elog_event_type_t *t = vec_elt_at_index (em->event_types, i); if (!em->event_type_by_format) em->event_type_by_format = hash_create_vec ( /* size */ 0, sizeof (u8), sizeof (uword)); t->type_index_plus_one = i + 1; hash_set_mem (em->event_type_by_format, t->format, i); } static uword find_or_create_type (elog_main_t * em, elog_event_type_t * t) { uword *p = hash_get_mem (em->event_type_by_format, t->format); uword i; if (p) i = p[0]; else { i = vec_len (em->event_types); vec_add1 (em->event_types, t[0]); new_event_type (em, i); } return i; } /* External function to register types. */ word elog_event_type_register (elog_main_t * em, elog_event_type_t * t) { elog_event_type_t *static_type = t; word l; elog_lock (em); /* Multiple simultaneous registration attempts, */ if (t->type_index_plus_one > 0) { elog_unlock (em); return t->type_index_plus_one - 1; } l = vec_len (em->event_types); t->type_index_plus_one = 1 + l; ASSERT (t->format); /* If format args are not specified try to be smart about providing defaults so most of the time user does not have to specify them. */ if (!t->format_args) { uword i, l; char *this_arg; l = strlen (t->format); for (i = 0; i < l; i++) { if (t->format[i] != '%') continue; if (i + 1 >= l) continue; if (t->format[i + 1] == '%') /* %% */ continue; switch (t->format[i + 1]) { default: case 'd': case 'x': case 'u': this_arg = "i4"; /* size of u32 */ break; case 'f': this_arg = "f8"; /* defaults to f64 */ break; case 's': this_arg = "s0"; /* defaults to null terminated string. */ break; } t->format_args = (char *) format ((u8 *) t->format_args, "%s", this_arg); } /* Null terminate. */ vec_add1 (t->format_args, 0); } vec_add1 (em->event_types, t[0]); t = em->event_types + l; /* Make copies of strings for hashing etc. */ if (t->function) t->format = (char *) format (0, "%s %s%c", t->function, t->format, 0); else t->format = (char *) format (0, "%s%c", t->format, 0); t->format_args = (char *) format (0, "%s%c", t->format_args, 0); /* Construct string table. */ { uword i; t->n_enum_strings = static_type->n_enum_strings; for (i = 0; i < t->n_enum_strings; i++) { if (!static_type->enum_strings[i]) static_type->enum_strings[i] = "MISSING"; vec_add1 (t->enum_strings_vector, (char *) format (0, "%s%c", static_type->enum_strings[i], 0)); } } new_event_type (em, l); elog_unlock (em); return l; } word elog_track_register (elog_main_t * em, elog_track_t * t) { word l; elog_lock (em); l = vec_len (em->tracks); t->track_index_plus_one = 1 + l; ASSERT (t->name); vec_add1 (em->tracks, t[0]); t = em->tracks + l; t->name = (char *) format (0, "%s%c", t->name, 0); elog_unlock (em); return l; } static uword parse_2digit_decimal (char *p, uword * number) { uword i = 0; u8 digits[2]; digits[0] = digits[1] = 0; while (p[i] >= '0' && p[i] <= '9') { if (i >= 2) break; digits[i] = p[i] - '0'; i++; } if (i >= 1 && i <= 2) { if (i == 1) *number = digits[0]; else *number = 10 * digits[0] + digits[1]; return i; } else return 0; } static u8 * fixed_format (u8 * s, char *fmt, char *result, uword * result_len) { char *f = fmt; char *percent; uword l = 0; while (1) { if (f[0] == 0) break; if (f[0] == '%' && f[1] != '%') break; f++; } if (f > fmt) vec_add (s, fmt, f - fmt); if (f[0] != '%') goto done; /* Skip percent. */ percent = f++; /* Skip possible +-= justification. */ f += f[0] == '+' || f[0] == '-' || f[0] == '='; /* Skip possible X.Y width. */ while ((f[0] >= '0' && f[0] <= '9') || f[0] == '.') f++; /* Skip wlL as in e.g. %Ld. */ f += f[0] == 'w' || f[0] == 'l' || f[0] == 'L'; /* Finally skip format letter. */ f += f[0] != 0; ASSERT (*result_len > f - percent); l = clib_min (f - percent, *result_len - 1); clib_memcpy (result, percent, l); result[l] = 0; done: *result_len = f - fmt; return s; } u8 * format_elog_event (u8 * s, va_list * va) { elog_main_t *em = va_arg (*va, elog_main_t *); elog_event_t *e = va_arg (*va, elog_event_t *); elog_event_type_t *t; char *a, *f; void *d = (u8 *) e->data; char arg_format[64]; t = vec_elt_at_index (em->event_types, e->event_type); f = t->format; a = t->format_args; while (1) { uword n_bytes = 0, n_digits, f_bytes = 0; f_bytes = sizeof (arg_format); s = fixed_format (s, f, arg_format, &f_bytes); f += f_bytes; if (a == 0 || a[0] == 0) { /* Format must also be at end. */ ASSERT (f[0] == 0); break; } /* Don't go past end of event data. */ ASSERT (d < (void *) (e->data + sizeof (e->data))); n_digits = parse_2digit_decimal (a + 1, &n_bytes); switch (a[0]) { case 'i': case 't': case 'T': { u32 i = 0; u64 l = 0; if (n_bytes == 1) i = ((u8 *) d)[0]; else if (n_bytes == 2) i = clib_mem_unaligned (d, u16); else if (n_bytes == 4) i = clib_mem_unaligned (d, u32); else if (n_bytes == 8) l = clib_mem_unaligned (d, u64); else ASSERT (0); if (a[0] == 't') { char *e = vec_elt (t->enum_strings_vector, n_bytes == 8 ? l : i); s = format (s, arg_format, e); } else if (a[0] == 'T') { char *e = vec_elt_at_index (em->string_table, n_bytes == 8 ? l : i); s = format (s, arg_format, e); } else if (n_bytes == 8) s = format (s, arg_format, l); else s = format (s, arg_format, i); } break; case 'f': { f64 x = 0; if (n_bytes == 4) x = clib_mem_unaligned (d, f32); else if (n_bytes == 8) x = clib_mem_unaligned (d, f64); else ASSERT (0); s = format (s, arg_format, x); } break; case 's': s = format (s, arg_format, d); if (n_bytes == 0) n_bytes = strlen (d) + 1; break; default: ASSERT (0); break; } ASSERT (n_digits > 0 && n_digits <= 2); a += 1 + n_digits; d += n_bytes; } return s; } u8 * format_elog_track_name (u8 * s, va_list * va) { elog_main_t *em = va_arg (*va, elog_main_t *); elog_event_t *e = va_arg (*va, elog_event_t *); elog_track_t *t = vec_elt_at_index (em->tracks, e->track); return format (s, "%s", t->name); } u8 * format_elog_track (u8 * s, va_list * args) { elog_main_t *em = va_arg (*args, elog_main_t *); f64 dt = va_arg (*args, f64); int track_index = va_arg (*args, int); elog_event_t *e, *es; u8 indent; indent = format_get_indent (s) + 1; es = elog_peek_events (em); vec_foreach (e, es) { if (e->track != track_index) continue; s = format (s, "%U%18.9f: %U\n", format_white_space, indent, e->time + dt, format_elog_event, em, e); } vec_free (es); return s; } char * format_one_elog_event (void *em_arg, void *ep_arg) { elog_main_t *em = (elog_main_t *) em_arg; elog_event_t *ep = (elog_event_t *) ep_arg; return (char *) format (0, "%U", format_elog_event, em, ep); } void elog_time_now (elog_time_stamp_t * et) { u64 cpu_time_now, os_time_now_nsec; struct timespec ts; #ifdef CLIB_UNIX { #include #ifdef __APPLE__ clock_gettime (CLOCK_REALTIME, &ts); #else syscall (SYS_clock_gettime, CLOCK_REALTIME, &ts); #endif cpu_time_now = clib_cpu_time_now (); /* Subtract 3/30/2017's worth of seconds to retain precision */ os_time_now_nsec = 1e9 * (ts.tv_sec - 1490885108) + ts.tv_nsec; } #else cpu_time_now = clib_cpu_time_now (); os_time_now_nsec = 0; #endif et->cpu = cpu_time_now; et->os_nsec = os_time_now_nsec; } always_inline i64 elog_time_stamp_diff_os_nsec (elog_time_stamp_t * t1, elog_time_stamp_t * t2) { return (i64) t1->os_nsec - (i64) t2->os_nsec; } always_inline i64 elog_time_stamp_diff_cpu (elog_time_stamp_t * t1, elog_time_stamp_t * t2) { return (i64) t1->cpu - (i64) t2->cpu; } always_inline f64 elog_nsec_per_clock (elog_main_t * em) { return ((f64) elog_time_stamp_diff_os_nsec (&em->serialize_time, &em->init_time) / (f64) elog_time_stamp_diff_cpu (&em->serialize_time, &em->init_time)); } void elog_alloc (elog_main_t * em, u32 n_events) { if (em->event_ring) vec_free (em->event_ring); /* Ring size must be a power of 2. */ em->event_ring_size = n_events = max_pow2 (n_events); /* Leave an empty ievent at end so we can always speculatively write and event there (possibly a long form event). */ vec_resize_aligned (em->event_ring, n_events, CLIB_CACHE_LINE_BYTES); } void elog_init (elog_main_t * em, u32 n_events) { clib_memset (em, 0, sizeof (em[0])); em->lock = 0; if (n_events > 0) elog_alloc (em, n_events); clib_time_init (&em->cpu_timer); em->n_total_events_disable_limit = ~0; /* Make track 0. */ em->default_track.name = "default"; elog_track_register (em, &em->default_track); elog_time_now (&em->init_time); em->string_table_hash = hash_create_string (0, sizeof (uword)); } /* Returns number of events in ring and start index. */ static uword elog_event_range (elog_main_t * em, uword * lo) { uword l = em->event_ring_size; u64 i = em->n_total_events; /* Ring never wrapped? */ if (i <= (u64) l) { if (lo) *lo = 0; return i; } else { if (lo) *lo = i & (l - 1); return l; } } elog_event_t * elog_peek_events (elog_main_t * em) { elog_event_t *e, *f, *es = 0; uword i, j, n; n = elog_event_range (em, &j); for (i = 0; i < n; i++) { vec_add2 (es, e, 1); f = vec_elt_at_index (em->event_ring, j); e[0] = f[0]; /* Convert absolute time from cycles to seconds from start. */ e->time = (e->time_cycles - em->init_time.cpu) * em->cpu_timer.seconds_per_clock; j = (j + 1) & (em->event_ring_size - 1); } return es; } /* Add a formatted string to the string table. */ u32 elog_string (elog_main_t * em, char *fmt, ...) { u32 offset; uword *p; uword len; va_list va; elog_lock (em); vec_reset_length (em->string_table_tmp); va_start (va, fmt); em->string_table_tmp = va_format (em->string_table_tmp, fmt, &va); va_end (va); /* String table entries MUST be NULL terminated */ len = vec_len (em->string_table_tmp); ASSERT (len > 0); if (em->string_table_tmp[len - 1] != 0) vec_add1 (em->string_table_tmp, 0); /* See if we already have this string in the string table */ p = hash_get_mem (em->string_table_hash, em->string_table_tmp); /* We already have the string, so give the caller its offset */ if (p) { elog_unlock (em); return (p[0]); } /* We don't, so add it. */ offset = vec_len (em->string_table); vec_append (em->string_table, em->string_table_tmp); hash_set_mem (em->string_table_hash, em->string_table_tmp, offset); /* We gave the key to the string table hash, so we can't reuse it! */ em->string_table_tmp = 0; elog_unlock (em); return offset; } elog_event_t * elog_get_events (elog_main_t * em) { vec_reset_length (em->events); em->events = elog_peek_events (em); return em->events; } static void maybe_fix_string_table_offset (elog_event_t * e, elog_event_type_t * t, u32 offset) { void *d = (u8 *) e->data; char *a; if (offset == 0) return; a = t->format_args; while (1) { uword n_bytes = 0, n_digits; if (a[0] == 0) break; /* Don't go past end of event data. */ ASSERT (d < (void *) (e->data + sizeof (e->data))); n_digits = parse_2digit_decimal (a + 1, &n_bytes); switch (a[0]) { case 'T': ASSERT (n_bytes == 4); clib_mem_unaligned (d, u32) += offset; break; case 'i': case 't': case 'f': case 's': break; default: ASSERT (0); break; } ASSERT (n_digits > 0 && n_digits <= 2); a += 1 + n_digits; d += n_bytes; } } static int elog_cmp (void *a1, void *a2) { elog_event_t *e1 = a1; elog_event_t *e2 = a2; if (e1->time < e2->time) return -1; if (e1->time > e2->time) return 1; return 0; } /* * merge two event logs. Complicated and cranky. */ void elog_merge (elog_main_t * dst, u8 * dst_tag, elog_main_t * src, u8 * src_tag, f64 align_tweak) { elog_event_t *e; uword l; u32 string_table_offset_for_src_events; u32 track_offset_for_src_tracks; elog_track_t newt; int i; clib_memset (&newt, 0, sizeof (newt)); /* Acquire src and dst events */ elog_get_events (src); elog_get_events (dst); string_table_offset_for_src_events = vec_len (dst->string_table); vec_append (dst->string_table, src->string_table); l = vec_len (dst->events); vec_append (dst->events, src->events); /* Prepend the supplied tag (if any) to all dst track names */ if (dst_tag) { for (i = 0; i < vec_len (dst->tracks); i++) { elog_track_t *t = vec_elt_at_index (dst->tracks, i); char *new_name; new_name = (char *) format (0, "%s:%s%c", dst_tag, t->name, 0); vec_free (t->name); t->name = new_name; } } /* * Remember where we started allocating new tracks while merging */ track_offset_for_src_tracks = vec_len (dst->tracks); /* Copy / tag source tracks */ for (i = 0; i < vec_len (src->tracks); i++) { elog_track_t *t = vec_elt_at_i
/*
 * node.c - skeleton vpp engine plug-in dual-loop node skeleton
 *
 * Copyright (c) <current-year> <your-organization>
 * 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 <vlib/vlib.h>
#include <vnet/vnet.h>
#include <vnet/pg/pg.h>
#include <vppinfra/error.h>
#include <mactime/mactime.h>

typedef struct
{
  u32 next_index;
  u32 device_index;
  u8 src_mac[6];
  u8 device_name[64];
} mactime_trace_t;

vlib_node_registration_t mactime_node;
vlib_node_registration_t mactime_tx_node;

#define foreach_mactime_error                   \
_(DROP, "Dropped packets")                      \
_(OK, "Permitted packets")

typedef enum
{
#define _(sym,str) MACTIME_ERROR_##sym,
  foreach_mactime_error
#undef _
    MACTIME_N_ERROR,
} mactime_error_t;

static char *mactime_error_strings[] = {
#define _(sym,string) string,
  foreach_mactime_error
#undef _
};

typedef enum
{
  MACTIME_NEXT_DROP,
  MACT