/* * 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. */ /* * node_funcs.h: processing nodes global functions/inlines * * Copyright (c) 2008 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. */ /** \file vlib node functions */ #ifndef included_vlib_node_funcs_h #define included_vlib_node_funcs_h #include #include /** \brief Get vlib node by index. @warning This function will ASSERT if @c i is out of range. @param vm vlib_main_t pointer, varies by thread @param i node index. @return pointer to the requested vlib_node_t. */ always_inline vlib_node_t * vlib_get_node (vlib_main_t * vm, u32 i) { return vec_elt (vm->node_main.nodes, i); } /** \brief Get vlib node by graph arc (next) index. @param vm vlib_main_t pointer, varies by thread @param node_index index of original node @param next_index graph arc index @return pointer to the vlib_node_t at the end of the indicated arc */ always_inline vlib_node_t * vlib_get_next_node (vlib_main_t * vm, u32 node_index, u32 next_index) { vlib_node_main_t *nm = &vm->node_main; vlib_node_t *n; n = vec_elt (nm->nodes, node_index); ASSERT (next_index < vec_len (n->next_nodes)); return vlib_get_node (vm, n->next_nodes[next_index]); } /** \brief Get node runtime by node index. @param vm vlib_main_t pointer, varies by thread @param node_index index of node @return pointer to the indicated vlib_node_runtime_t */ always_inline vlib_node_runtime_t * vlib_node_get_runtime (vlib_main_t * vm, u32 node_index) { vlib_node_main_t *nm = &vm->node_main; vlib_node_t *n = vec_elt (nm->nodes, node_index); vlib_process_t *p; if (n->type != VLIB_NODE_TYPE_PROCESS) return vec_elt_at_index (nm->nodes_by_type[n->type], n->runtime_index); else { p = vec_elt (nm->processes, n->runtime_index); return &p->node_runtime; } } /** \brief Get node runtime private data by node index. @param vm vlib_main_t pointer, varies by thread @param node_index index of the node @return pointer to the indicated vlib_node_runtime_t private data */ always_inline void * vlib_node_get_runtime_data (vlib_main_t * vm, u32 node_index) { vlib_node_runtime_t *r = vlib_node_get_runtime (vm, node_index); return r->runtime_data; } /** \brief Set node runtime private data. @param vm vlib_main_t pointer, varies by thread @param node_index index of the node @param runtime_data arbitrary runtime private data @param n_runtime_data_bytes size of runtime private data */ always_inline void vlib_node_set_runtime_data (vlib_main_t * vm, u32 node_index, void *runtime_data, u32 n_runtime_data_bytes) { vlib_node_t *n = vlib_get_node (vm, node_index); vlib_node_runtime_t *r = vlib_node_get_runtime (vm, node_index); n->runtime_data_bytes = n_runtime_data_bytes; vec_free (n->runtime_data); vec_add (n->runtime_data, runtime_data, n_runtime_data_bytes); ASSERT (vec_len (n->runtime_data) <= sizeof (vlib_node_runtime_t) - STRUCT_OFFSET_OF (vlib_node_runtime_t, runtime_data)); if (vec_len (n->runtime_data) > 0) clib_memcpy_fast (r->runtime_data, n->runtime_data, vec_len (n->runtime_data)); } /** \brief Set node dispatch state. @param vm vlib_main_t pointer, varies by thread @param node_index index of the node @param new_state new state for node, see vlib_node_state_t */ always_inline void vlib_node_set_state (vlib_main_t * vm, u32 node_index, vlib_node_state_t new_state) { vlib_node_main_t *nm = &vm->node_main; vlib_node_t *n; vlib_node_runtime_t *r; n = vec_elt (nm->nodes, node_index); if (n->type == VLIB_NODE_TYPE_PROCESS) { vlib_process_t *p = vec_elt (nm->processes, n->runtime_index); r = &p->node_runtime; /* When disabling make sure flags are cleared. */ p->flags &= ~(VLIB_PROCESS_RESUME_PENDING | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_CLOCK | VLIB_PROCESS_IS_SUSPENDED_WAITING_FOR_EVENT); } else r = vec_elt_at_index (nm->nodes_by_type[n->type], n->runtime_index); ASSERT (new_state < VLIB_N_NODE_STATE); if (n->type == VLIB_NODE_TYPE_INPUT) { ASSERT (nm->input_node_counts_by_state[n->state] > 0); nm->input_node_counts_by_state[n->state] -= 1; nm->input_node_counts_by_state[new_state] += 1; } n->state = new_state; r->state = new_state; } /** \brief Get node dispatch state. @param vm vlib_main_t pointer, varies by thread @param node_index index of the node @return state for node, see vlib_node_state_t */ always_inline vlib_node_state_t vlib_node_get_state (vlib_main_t * vm, u32 node_index) { vlib_node_main_t *nm = &vm->node_main; vlib_node_t *n; n = vec_elt (nm->nodes, node_index); return n->state; } always_inline void vlib_node_set_interrupt_pending (vlib_main_t * vm, u32 node_index) { vlib_node_main_t *nm = &vm->node_main; vlib_node_t *n = vec_elt (nm->nodes, node_index); ASSERT (n->type == VLIB_NODE_TYPE_INPUT); clib_spinlock_lock_if_init (&nm->pending_interrupt_lock); vec_add1 (nm->pending_interrupt_node_runtime_indices, n->runtime_index); clib_spinlock_unlock_if_init (&nm->pending_interrupt_lock); } always_inline vlib_process_t * vlib_get_process_from_node (vlib_main_t * vm, vlib_node_t * node) { vlib_node_main_t *nm = &vm->node_main; ASSERT (node->type == VLIB_NODE_TYPE_PROCESS); return vec_elt (nm->processes, node->runtime_index); } always_inline vlib_frame_t * vlib_get_frame (vlib_main_t * vm, vlib_frame_t * f) { ASSERT (f != NULL); ASSERT (f->frame_flags & VLIB_FRAME_IS_ALLOCATED); return f; } always_inline void vlib_frame_no_append (vlib_frame_t * f) { f->frame_flags |= VLIB_FRAME_NO_APPEND; } /* Byte alignment for vector arguments. */ #define VLIB_FRAME_VECTOR_ALIGN (1 << 4) always_inline u32 vlib_frame_vector_byte_offset (u32 scalar_size) { return round_pow2 (sizeof (vlib_frame_t) + scalar_size, VLIB_FRAME_VECTOR_ALIGN); } /** \brief Get pointer to frame vector data. @param f vlib_frame_t pointer @return pointer to first vector element in frame */ always_inline void * vlib_frame_vector_args (vlib_frame_t * f) { return (void *) f + vlib_frame_vector_byte_offset (f->scalar_size); } /** \brief Get pointer to frame scalar data. @param f vlib_frame_t pointer @return arbitrary node scalar data @sa vlib_frame_vector_args */ always_inline void * vlib_frame_scalar_args (vlib_frame_t * f) { return vlib_frame_vector_args (f) - f->scalar_size; } always_inline vlib_next_frame_t * vlib_node_runtime_get_next_frame (vlib_main_t * vm, vlib_node_runtime_t * n, u32 next_index) { vlib_node_main_t *nm = &vm->node_main; vlib_next_frame_t *nf; ASSERT (next_index < n->n_next_nodes); nf = vec_elt_at_index (nm->next_frames, n->next_frame_index + next_index); if (CLIB_DEBUG > 0) { vlib_node_t *node, *next; node = vec_elt (nm->nodes, n->node_index); next = vec_elt (nm->nodes, node->next_nodes[next_index]); ASSERT (nf->node_runtime_index == next->runtime_index); } return nf; } /** \brief Get pointer to frame by (@c node_index, @c next_index). @warn
/*
 * Copyright (c) 2017 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.
 */
#undef BIHASH_TYPE
#undef BIHASH_KVP_PER_PAGE
#undef BIHASH_32_64_SVM
#undef BIHASH_ENABLE_STATS
#undef BIHASH_KVP_AT_BUCKET_LEVEL
#undef BIHASH_LAZY_INSTANTIATE
#undef BIHASH_BUCKET_PREFETCH_CACHE_LINES

#define BIHASH_TYPE _vec8_8
#define BIHASH_KVP_PER_PAGE 4
#define BIHASH_KVP_AT_BUCKET_LEVEL 0
#define BIHASH_LAZY_INSTANTIATE 1
#define BIHASH_BUCKET_PREFETCH_CACHE_LINES 1

#ifndef __included_bihash_vec8_8_h__
#define __included_bihash_vec8_8_h__

#include <vppinfra/heap.h>
#include <vppinfra/format.h>
#include <vppinfra/pool.h>
#include <vppinfra/xxhash.h>
#include <vppinfra/crc32.h>

/** 8 octet key, 8 octet key value pair */
typedef struct
{
  u64 key;			/**< the key */
  u64 value;			/**< the value */
} clib_bihash_kv_vec8_8_t;

/** Decide if a clib_bihash_kv_vec8_8_t instance is free
    @param v- pointer to the (key,value) pair
*/
static inline int
clib_bihash_is_free_vec8_8 (clib_bihash_kv_vec8_8_t * v)
{
  if (v->key == ~0ULL && v->value == ~0ULL)
    return 1;
  return 0;
}

/** Hash a clib_bihash_kv_vec8_8_t instance
    @param v - pointer to the (key,value) pair, hash the key (only)
*/
static inline u64
clib_bihash_hash_vec8_8 (clib_bihash_kv_vec8_8_t * v)
{
  u8 *keyp = (u8 *) (v->key);
  /* Note: to torture-test linear scan, make this fn return a constant */
#ifdef clib_crc32c_uses_intrinsics
  return clib_crc32c (keyp, vec_len (keyp));
#else
  {
    int i, j;
    u64 sum = 0;

    for (i = 0, j = 0; i < vec_len (keyp); i++)
      {
	sum ^= keyp[i] << (j * 8);
	j++;
	if (j == 4)
	  j = 0;
      }

    return clib_xxhash (sum);
  }
#endif
}

/** Format a clib_bihash_kv_vec8_8_t instance
    @param s - u8 * vector under construction
    @param args (vararg) - the (key,value) pair to format
    @return s - the u8 * vector under construction
*/
static inline u8 *
format_bihash_kvp_vec8_8 (u8 * s, va_list * args)
{
  clib_bihash_kv_vec8_8_t *v = va_arg (*args, clib_bihash_kv_vec8_8_t *);

  s = format (s, "key %U value %llu",
	      format_hex_bytes, v->key, vec_len ((u8 *) (v->key)), v->value);
  return s;
}

/** Compare two clib_bihash_kv_vec8_8_t instances
    @param a - first key
    @param b - second key
*/
static inline int
clib_bihash_key_compare_vec8_8 (u64 a_arg, u64 b_arg)
{
  u8 *a = (u8 *) a_arg;
  u8 *b = (u8 *) b_arg;

  if (a_arg == ~0ULL || b_arg == ~0ULL)
    return 0;

  if (vec_len (a) != vec_len (b))
    return 0;

  return memcmp (a, b, vec_len (a)) == 0;
}

#undef __included_bihash_template_h__
#include <vppinfra/bihash_template.h>

#endif /* __included_bihash_vec8_8_h__ */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
} always_inline void vlib_signal_one_time_waiting_process_vector (vlib_main_t * vm, vlib_one_time_waiting_process_t ** wps) { vlib_one_time_waiting_process_t *wp; vec_foreach (wp, *wps) vlib_signal_one_time_waiting_process (vm, wp); vec_free (*wps); } always_inline void vlib_current_process_wait_for_one_time_event (vlib_main_t * vm, vlib_one_time_waiting_process_t * p) { p->node_index = vlib_current_process (vm); p->one_time_event = vlib_process_create_one_time_event (vm, p->node_index, /* type opaque */ ~0); vlib_process_wait_for_one_time_event (vm, /* don't care about data */ 0, p->one_time_event); } always_inline void vlib_current_process_wait_for_one_time_event_vector (vlib_main_t * vm, vlib_one_time_waiting_process_t ** wps) { vlib_one_time_waiting_process_t *wp; vec_add2 (*wps, wp, 1); vlib_current_process_wait_for_one_time_event (vm, wp); } always_inline u32 vlib_node_runtime_update_main_loop_vector_stats (vlib_main_t * vm, vlib_node_runtime_t * node, uword n_vectors) { u32 i, d, vi0, vi1; u32 i0, i1; ASSERT (is_pow2 (ARRAY_LEN (node->main_loop_vector_stats))); i = ((vm->main_loop_count >> VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE) & (ARRAY_LEN (node->main_loop_vector_stats) - 1)); i0 = i ^ 0; i1 = i ^ 1; d = ((vm->main_loop_count >> VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE) - (node->main_loop_count_last_dispatch >> VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE)); vi0 = node->main_loop_vector_stats[i0]; vi1 = node->main_loop_vector_stats[i1]; vi0 = d == 0 ? vi0 : 0; vi1 = d <= 1 ? vi1 : 0; vi0 += n_vectors; node->main_loop_vector_stats[i0] = vi0; node->main_loop_vector_stats[i1] = vi1; node->main_loop_count_last_dispatch = vm->main_loop_count; /* Return previous counter. */ return node->main_loop_vector_stats[i1]; } always_inline f64 vlib_node_vectors_per_main_loop_as_float (vlib_main_t * vm, u32 node_index) { vlib_node_runtime_t *rt = vlib_node_get_runtime (vm, node_index); u32 v; v = vlib_node_runtime_update_main_loop_vector_stats (vm, rt, /* n_vectors */ 0); return (f64) v / (1 << VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE); } always_inline u32 vlib_node_vectors_per_main_loop_as_integer (vlib_main_t * vm, u32 node_index) { vlib_node_runtime_t *rt = vlib_node_get_runtime (vm, node_index); u32 v; v = vlib_node_runtime_update_main_loop_vector_stats (vm, rt, /* n_vectors */ 0); return v >> VLIB_LOG2_MAIN_LOOPS_PER_STATS_UPDATE; } void vlib_frame_free (vlib_main_t * vm, vlib_node_runtime_t * r, vlib_frame_t * f); /* Return the edge index if present, ~0 otherwise */ uword vlib_node_get_next (vlib_main_t * vm, uword node, uword next_node); /* Add next node to given node in given slot. */ uword vlib_node_add_next_with_slot (vlib_main_t * vm, uword node, uword next_node, uword slot); /* As above but adds to end of node's next vector. */ always_inline uword vlib_node_add_next (vlib_main_t * vm, uword node, uword next_node) { return vlib_node_add_next_with_slot (vm, node, next_node, ~0); } /* Add next node to given node in given slot. */ uword vlib_node_add_named_next_with_slot (vlib_main_t * vm, uword node, char *next_name, uword slot); /* As above but adds to end of node's next vector. */ always_inline uword vlib_node_add_named_next (vlib_main_t * vm, uword node, char *name) { return vlib_node_add_named_next_with_slot (vm, node, name, ~0); } /** * Get list of nodes */ void vlib_node_get_nodes (vlib_main_t * vm, u32 max_threads, int include_stats, int barrier_sync, vlib_node_t **** node_dupsp, vlib_main_t *** stat_vmsp); /* Query node given name. */ vlib_node_t *vlib_get_node_by_name (vlib_main_t * vm, u8 * name); /* Rename a node. */ void vlib_node_rename (vlib_main_t * vm, u32 node_index, char *fmt, ...); /* Register new packet processing node. Nodes can be registered dynamically via this call or statically via the VLIB_REGISTER_NODE macro. */ u32 vlib_register_node (vlib_main_t * vm, vlib_node_registration_t * r); /* Register all static nodes registered via VLIB_REGISTER_NODE. */ void vlib_register_all_static_nodes (vlib_main_t * vm); /* Start a process. */ void vlib_start_process (vlib_main_t * vm, uword process_index); /* Sync up runtime and main node stats. */ void vlib_node_sync_stats (vlib_main_t * vm, vlib_node_t * n); /* Node graph initialization function. */ clib_error_t *vlib_node_main_init (vlib_main_t * vm); format_function_t format_vlib_node_graph; format_function_t format_vlib_node_name; format_function_t format_vlib_next_node_name; format_function_t format_vlib_node_and_next; format_function_t format_vlib_cpu_time; format_function_t format_vlib_time; /* Parse node name -> node index. */ unformat_function_t unformat_vlib_node; always_inline void vlib_node_increment_counter (vlib_main_t * vm, u32 node_index, u32 counter_index, u64 increment) { vlib_node_t *n = vlib_get_node (vm, node_index); vlib_error_main_t *em = &vm->error_main; u32 node_counter_base_index = n->error_heap_index; em->counters[node_counter_base_index + counter_index] += increment; } /** @brief Create a vlib process * @param vm &vlib_global_main * @param f the process node function * @param log2_n_stack_bytes size of the process stack, defaults to 16K * @return newly-create node index * @warning call only on the main thread. Barrier sync required */ u32 vlib_process_create (vlib_main_t * vm, char *name, vlib_node_function_t * f, u32 log2_n_stack_bytes); #endif /* included_vlib_node_funcs_h */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */