/* * 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 /** \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 (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; } 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); vec_add1 (nm->pending_interrupt_node_runtime_indices, n->runtime_index); } 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); } /* Fetches frame with given handle. */ always_inline vlib_frame_t * vlib_get_frame_no_check (vlib_main_t * vm, uword frame_index) { vlib_frame_t *f; u32 cpu_index = frame_index & VLIB_CPU_MASK; u32 offset = frame_index & VLIB_OFFSET_MASK; vm = vlib_mains[cpu_index]; f = vm->heap_base + offset; return f; } always_inline u32 vlib_frame_index_no_check (vlib_main_t * vm, vlib_frame_t * f) { u32 i; ASSERT (((uword) f & VLIB_CPU_MASK) == 0); vm = vlib_mains[f->cpu_index]; i = ((u8 *) f - (u8 *) vm->heap_base); return i | f->cpu_index; } always_inline vlib_frame_t * vlib_get_frame (vlib_main_t * vm, uword frame_index) { vlib_frame_t *f = vlib_get_frame_no_check (vm, frame_index); ASSERT (f->flags & VLIB_FRAME_IS_ALLOCATED); return f; } always_inline u32 vlib_frame_index (vlib_main_t * vm, vlib_frame_t * f) { uword i = vlib_frame_index_no_check (vm, f); ASSERT (vlib_get_frame (vm, i) == f); return i; } /* 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 vecto
/*
 * Copyright (c) 2020 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.
 */


#ifndef __CNAT_INLINE_H__
#define __CNAT_INLINE_H__

#include <cnat/cnat_types.h>

always_inline u32
cnat_timestamp_new (f64 t)
{
  u32 index;
  cnat_timestamp_t *ts;
  clib_rwlock_writer_lock (&cnat_main.ts_lock);
  pool_get (cnat_timestamps, ts);
  ts->last_seen = t;
  ts->lifetime = cnat_main.session_max_age;
  ts->refcnt = CNAT_TIMESTAMP_INIT_REFCNT;
  index = ts - cnat_timestamps;
  clib_rwlock_writer_unlock (&cnat_main.ts_lock);
  return index;
}

always_inline void
cnat_timestamp_inc_refcnt (u32 index)
{
  clib_rwlock_reader_lock (&cnat_main.ts_lock);
  cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index);
  ts->refcnt++;
  clib_rwlock_reader_unlock (&cnat_main.ts_lock);
}

always_inline void
cnat_timestamp_update (u32 index, f64 t)
{
  clib_rwlock_reader_lock (&cnat_main.ts_lock);
  cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index);
  ts->last_seen = t;
  clib_rwlock_reader_unlock (&cnat_main.ts_lock);
}

always_inline void
cnat_timestamp_set_lifetime (u32 index, u16 lifetime)
{
  clib_rwlock_reader_lock (&cnat_main.ts_lock);
  cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index);
  ts->lifetime = lifetime;
  clib_rwlock_reader_unlock (&cnat_main.ts_lock);
}

always_inline f64
cnat_timestamp_exp (u32 index)
{
  f64 t;
  if (INDEX_INVALID == index)
    return -1;
  clib_rwlock_reader_lock (&cnat_main.ts_lock);
  cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index);
  t = ts->last_seen + (f64) ts->lifetime;
  clib_rwlock_reader_unlock (&cnat_main.ts_lock);
  return t;
}

always_inline void
cnat_timestamp_free (u32 index)
{
  if (INDEX_INVALID == index)
    return;
  clib_rwlock_writer_lock (&cnat_main.ts_lock);
  cnat_timestamp_t *ts = pool_elt_at_index (cnat_timestamps, index);
  ts->refcnt--;
  if (0 == ts->refcnt)
    pool_put (cnat_timestamps, ts);
  clib_rwlock_writer_unlock (&cnat_main.ts_lock);
}

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

#endif
n_data_elts, uword n_data_elt_bytes) { vlib_node_main_t *nm = &vm->node_main; vlib_node_t *n = vlib_get_node (vm, node_index); vlib_process_t *p = vec_elt (nm->processes, n->runtime_index); return vlib_process_signal_event_helper (nm, n, p, type_index, n_data_elts, n_data_elt_bytes); } always_inline void vlib_process_signal_event (vlib_main_t * vm, uword node_index, uword type_opaque, uword data) { uword *d = vlib_process_signal_event_data (vm, node_index, type_opaque, 1 /* elts */ , sizeof (uword)); d[0] = data; } always_inline void vlib_process_signal_event_pointer (vlib_main_t * vm, uword node_index, uword type_opaque, void *data) { void **d = vlib_process_signal_event_data (vm, node_index, type_opaque, 1 /* elts */ , sizeof (data)); d[0] = data; } always_inline void vlib_process_signal_one_time_event (vlib_main_t * vm, uword node_index, uword type_index, uword data) { uword *d = vlib_process_signal_one_time_event_data (vm, node_index, type_index, 1 /* elts */ , sizeof (uword)); d[0] = data; } always_inline void vlib_signal_one_time_waiting_process (vlib_main_t * vm, vlib_one_time_waiting_process_t * p) { vlib_process_signal_one_time_event (vm, p->node_index, p->one_time_event, /* data */ ~0); memset (p, ~0, sizeof (p[0])); } 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); /* 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); } /* 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; } #endif /* included_vlib_node_funcs_h */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */