/* * Copyright (c) 2016 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 #if __x86_64__ && CLIB_DEBUG == 0 #define foreach_march_variant_runtime(macro, _args...) \ macro(avx512, avx512f, _args) \ macro(avx2, avx2, _args) #else #define foreach_march_variant_runtime(macro, _args...) #endif #define CLIB_MARCH_ARCH_CHECK(arch, archname, fn) \ if (clib_cpu_supports_ ## archname ()) \ return & fn ## _ ##arch; #define CLIB_MARCH_SELECT_FN(fn,...) \ __VA_ARGS__ void * fn ## _multiarch_select(void) \ { \ foreach_march_variant_runtime(CLIB_MARCH_ARCH_CHECK, fn) \ return & fn; \ } #define DEFINE_WEAK_FN(_arch, _fn, _args...) \ int __attribute__((weak)) _fn##_ma_##_arch(_args); #define DEFINE_FN_HELPER(arch, archname, macro, _args...) \ macro(arch, _args) #ifndef CLIB_MARCH_VARIANT #define MARCH_FN(fn, _args...) \ static void * (*fn ## _selected) (void); \ foreach_march_variant_runtime (DEFINE_FN_HELPER, DEFINE_WEAK_FN, fn, \ _args); \ static inline int CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args) #else #define MARCH_FN(fn, _args...) \ int CLIB_CPU_OPTIMIZED CLIB_MARCH_SFX (fn ## _ma)(_args) #endif static inline u8 position_lt (svm_fifo_t * f, u32 a, u32 b) { return (ooo_segment_distance_from_tail (f, a) < ooo_segment_distance_from_tail (f, b)); } static inline u8 position_leq (svm_fifo_t * f, u32 a, u32 b) { return (ooo_segment_distance_from_tail (f, a) <= ooo_segment_distance_from_tail (f, b)); } static inline u8 position_gt (svm_fifo_t * f, u32 a, u32 b) { return (ooo_segment_distance_from_tail (f, a) > ooo_segment_distance_from_tail (f, b)); } static inline u32 position_diff (svm_fifo_t * f, u32 posa, u32 posb) { return ooo_segment_distance_from_tail (f, posa) - ooo_segment_distance_from_tail (f, posb); } static inline u32 ooo_segment_end_pos (svm_fifo_t * f, ooo_segment_t * s) { return (s->start + s->length) % f->nitems; } #ifndef CLIB_MARCH_VARIANT u8 * format_ooo_segment (u8 * s, va_list * args) { svm_fifo_t *f = va_arg (*args, svm_fifo_t *); ooo_segment_t *seg = va_arg (*args, ooo_segment_t *); u32 normalized_start = (seg->start + f->nitems - f->tail) % f->nitems; s = format (s, "[%u, %u], len %u, next %d, prev %d", normalized_start, (normalized_start + seg->length) % f->nitems, seg->length, seg->next, seg->prev); return s; } u8 * svm_fifo_dump_trace (u8 * s, svm_fifo_t * f) { #if SVM_FIFO_TRACE svm_fifo_trace_elem_t *seg = 0; int i = 0; if (f->trace) { vec_foreach (seg, f->trace) { s = format (s, "{%u, %u, %u}, ", seg->offset, seg->len, seg->action); i++; if (i % 5 == 0) s = format (s, "\n"); } s = format (s, "\n"); } return s; #else return 0; #endif } u8 * svm_fifo_replay (u8 * s, svm_fifo_t * f, u8 no_read, u8 verbose) { int i, trace_len; u8 *data = 0; svm_fifo_trace_elem_t *trace; u32 offset; svm_fifo_t *dummy_fifo; if (!f) return s; #if SVM_FIFO_TRACE trace = f->trace; trace_len = vec_len (trace); #else trace = 0; trace_len = 0; #endif dummy_fifo = svm_fifo_create (f->nitems); memset (f->data, 0xFF, f->nitems); vec_validate (data, f->nitems); for (i = 0; i < vec_len (data); i++) data[i] = i; for (i = 0; i < trace_len; i++) { offset = trace[i].offset; if (trace[i].action == 1) { if (verbose) s = format (s, "adding [%u, %u]:", trace[i].offset, (trace[i].offset + trace[i].len) % dummy_fifo->nitems); svm_fifo_enqueue_with_offset (dummy_fifo, trace[i].offset, trace[i].len, &data[offset]); } else if (trace[i].action == 2) { if (verbose) s = format (s, "adding [%u, %u]:", 0, trace[i].len); svm_fifo_enqueue_nowait (dummy_fifo, trace[i].len, &data[offset]); } else if (!no_read) { if (verbose) s = format (s, "read: %u", trace[i].len); svm_fifo_dequeue_drop (dummy_fifo, trace[i].len); } if (verbose) s = format (s, "%U", format_svm_fifo, dummy_fifo, 1); } s = format (s, "result: %U", format_svm_fifo, dummy_fifo, 1); return s; } u8 * format_ooo_list (u8 * s, va_list * args) { svm_fifo_t *f = va_arg (*args, svm_fifo_t *); u32 ooo_segment_index = f->ooos_list_head; ooo_segment_t *seg; while (ooo_segment_index != OOO_SEGMENT_INVALID_INDEX) { seg = pool_elt_at_index (f->ooo_segments, ooo_segment_index); s = format (s, " %U\n", format_ooo_segment, f, seg); ooo_segment_index = seg->next; } return s; } u8 * format_svm_fifo (u8 * s, va_list * args) { svm_fifo_t *f = va_arg (*args, svm_fifo_t *); int verbose = va_arg (*args, int); if (!s) return s; s = format (s, "cursize %u nitems %u has_event %d\n", f->cursize, f->nitems, f->has_event); s = format (s, " head %d tail %d segment manager %u\n", f->head, f->tail, f->segment_manager); if (verbose > 1) s = format (s, " vpp session %d thread %d app session %d thread %d\n", f->master_session_index, f->master_thread_index, f->client_session_index, f->client_thread_index); if (verbose) { s = format (s, " ooo pool %d active elts newest %u\n", pool_elts (f->ooo_segments), f->ooos_newest); if (svm_fifo_has_ooo_data (f)) s = format (s, " %U", format_ooo_list, f, verbose); } return s; } /** create an svm fifo, in the current heap. Fails vs blow up the process */ svm_fifo_t * svm_fifo_create (u32 data_size_in_bytes) { svm_fifo_t *f; u32 rounded_data_size; /* always round fifo data size to the next highest power-of-two */ rounded_data_size = (1 << (max_log2 (data_size_in_bytes))); f = clib_mem_alloc_aligned_or_null (sizeof (*f)
;;; plugin-msg-enum-skel.el - vpp engine plug-in message enum skeleton
;;;
;;; Copyright (c) 2016 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.

(require 'skeleton)

(define-skeleton skel-plugin-msg-enum
"Insert a plug-in message enumeration skeleton "
nil
'(if (not (boundp 'plugin-name))
     (setq plugin-name (read-string "Plugin name: ")))
'(setq PLUGIN-NAME (upcase plugin-name))
"
/*
 * " plugin-name "_msg_enum.h - skeleton vpp engine plug-in message enumeration
 *
 * 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.
 */
#ifndef included_" plugin-name "_msg_enum_h
#define included_" plugin-name "_msg_enum_h

#include <vppinfra/byte_order.h>

#define vl_msg_id(n,h) n,
typedef enum {
#include <" plugin-name "/" plugin-name "_all_api_h.h>
    /* We'll want to know how many messages IDs we need... */
    VL_MSG_FIRST_AVAILABLE,
} vl_msg_id_t;
#undef vl_msg_id

#endif /* included_" plugin-name "_msg_enum_h */
")
copy_here) { return ((int (*)(svm_fifo_t * f, u32, u32, u8 *)) (*svm_fifo_peek_selected)) (f, relative_offset, max_bytes, copy_here); } int svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes) { u32 total_drop_bytes, first_drop_bytes, second_drop_bytes; u32 cursize, nitems; /* read cursize, which can only increase while we're working */ cursize = svm_fifo_max_dequeue (f); if (PREDICT_FALSE (cursize == 0)) return -2; /* nothing in the fifo */ nitems = f->nitems; /* Number of bytes we're going to drop */ total_drop_bytes = (cursize < max_bytes) ? cursize : max_bytes; svm_fifo_trace_add (f, f->tail, total_drop_bytes, 3); /* Number of bytes in first copy segment */ first_drop_bytes = ((nitems - f->head) < total_drop_bytes) ? (nitems - f->head) : total_drop_bytes; f->head += first_drop_bytes; f->head = (f->head == nitems) ? 0 : f->head; /* Number of bytes in second drop segment, if any */ second_drop_bytes = total_drop_bytes - first_drop_bytes; if (second_drop_bytes) { f->head += second_drop_bytes; f->head = (f->head == nitems) ? 0 : f->head; } ASSERT (f->head <= nitems); ASSERT (cursize >= total_drop_bytes); __sync_fetch_and_sub (&f->cursize, total_drop_bytes); return total_drop_bytes; } void svm_fifo_dequeue_drop_all (svm_fifo_t * f) { f->head = f->tail; __sync_fetch_and_sub (&f->cursize, f->cursize); } int svm_fifo_segments (svm_fifo_t * f, svm_fifo_segment_t * fs) { u32 cursize, nitems; /* read cursize, which can only increase while we're working */ cursize = svm_fifo_max_dequeue (f); if (PREDICT_FALSE (cursize == 0)) return -2; nitems = f->nitems; fs[0].len = ((nitems - f->head) < cursize) ? (nitems - f->head) : cursize; fs[0].data = f->data + f->head; if (fs[0].len < cursize) { fs[1].len = cursize - fs[0].len; fs[1].data = f->data; } else { fs[1].len = 0; fs[1].data = 0; } return cursize; } void svm_fifo_segments_free (svm_fifo_t * f, svm_fifo_segment_t * fs) { u32 total_drop_bytes; ASSERT (fs[0].data == f->data + f->head); if (fs[1].len) { f->head = fs[1].len; total_drop_bytes = fs[0].len + fs[1].len; } else { f->head = (f->head + fs[0].len) % f->nitems; total_drop_bytes = fs[0].len; } __sync_fetch_and_sub (&f->cursize, total_drop_bytes); } u32 svm_fifo_number_ooo_segments (svm_fifo_t * f) { return pool_elts (f->ooo_segments); } ooo_segment_t * svm_fifo_first_ooo_segment (svm_fifo_t * f) { return pool_elt_at_index (f->ooo_segments, f->ooos_list_head); } /** * Set fifo pointers to requested offset */ void svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer) { f->head = f->tail = pointer % f->nitems; } #define foreach_svm_fifo_march_fn \ _(svm_fifo_enqueue_nowait) \ _(svm_fifo_enqueue_with_offset) \ _(svm_fifo_dequeue_nowait) \ _(svm_fifo_peek) \ #define _(_fn, _args...) CLIB_MARCH_SELECT_FN(_fn ## _ma); foreach_svm_fifo_march_fn #undef _ void __clib_constructor svm_fifo_select_march_fns (void) { #define _(_fn, _args...) _fn ## _selected = _fn ## _ma_multiarch_select (); foreach_svm_fifo_march_fn #undef _ } #endif /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */