/* * 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.c: VLIB processing nodes * * 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. */ #include #include /* Query node given name. */ vlib_node_t * vlib_get_node_by_name (vlib_main_t * vm, u8 * name) { vlib_node_main_t *nm = &vm->node_main; uword *p; u8 *key = name; key = format (0, "%s", key); p = hash_get (nm->node_by_name, key); if (key != name) vec_free (key); return p ? vec_elt (nm->nodes, p[0]) : 0; } static void node_set_elog_name (vlib_main_t * vm, uword node_index) { vlib_node_t *n = vlib_get_node (vm, node_index); elog_event_type_t *t; t = vec_elt_at_index (vm->node_call_elog_event_types, node_index); vec_free (t->format); t->format = (char *) format (0, "%v-call: %%d%c", n->name, 0); t = vec_elt_at_index (vm->node_return_elog_event_types, node_index); vec_free (t->format); t->format = (char *) format (0, "%v-return: %%d%c", n->name, 0); n->name_elog_string = elog_string (&vlib_global_main.elog_main, "%v%c", n->name, 0); } void vlib_node_rename (vlib_main_t * vm, u32 node_index, char *fmt, ...) { va_list va; vlib_node_main_t *nm = &vm->node_main; vlib_node_t *n = vlib_get_node (vm, node_index); va_start (va, fmt); hash_unset (nm->node_by_name, n->name); vec_free (n->name); n->name = va_format (0, fmt, &va); va_end (va); hash_set (nm->node_by_name, n->name, n->index); node_set_elog_name (vm, node_index); /* Propagate the change to all worker threads */ vlib_worker_thread_node_runtime_update (); } static void vlib_node_runtime_update (vlib_main_t * vm, u32 node_index, u32 next_index) { vlib_node_main_t *nm = &vm->node_main; vlib_node_runtime_t *r, *s; vlib_node_t *node, *next_node; vlib_next_frame_t *nf; vlib_pending_frame_t *pf; i32 i, j, n_insert; node = vec_elt (nm->nodes, node_index); r = vlib_node_get_runtime (vm, node_index); n_insert = vec_len (node->next_nodes) - r->n_next_nodes; if (n_insert > 0) { i = r->next_frame_index + r->n_next_nodes; vec_insert (nm->next_frames, n_insert, i); /* Initialize newly inserted next frames. */ for (j = 0; j < n_insert; j++) vlib_next_frame_init (nm->next_frames + i + j); /* Relocate other next frames at higher indices. */ for (j = 0; j < vec_len (nm->nodes); j++) { s = vlib_node_get_runtime (vm, j); if (j != node_index && s->next_frame_index >= i) s->next_frame_index += n_insert; } /* Pending frames may need to be relocated also. */ vec_foreach (pf, nm->pending_frames) { if (pf->next_frame_index != VLIB_PENDING_FRAME_NO_NEXT_FRAME && pf->next_frame_index >= i) pf->next_frame_index += n_insert; } /* *INDENT-OFF* */ pool_foreach (pf, nm->suspended_process_frames) { if (pf->next_frame_index != ~0 && pf->next_frame_index >= i) pf->next_frame_index += n_insert; } /* *INDENT-ON* */ r->n_next_nodes = vec_len (node->next_nodes); } /* Set frame's node runtime index. */ next_node = vlib_get_node (vm, node->next_nodes[next_index]); nf = nm->next_frames + r->next_frame_index + next_index; nf->node_runtime_index = next_node->runtime_index; vlib_worker_thread_node_runtime_update (); } uword vlib_node_get_next (vlib_main_t * vm, uword node_index, uword next_node_index) { vlib_node_main_t *nm = &vm->node_main; vlib_node_t *node; uword *p; node = vec_elt (nm->nodes, node_index); /* Runtime has to be initialized. */ ASSERT (nm->flags & VLIB_NODE_MAIN_RUNTIME_STARTED); if ((p = hash_get (node->next_slot_by_node, next_node_index))) { return p[0]; } return (~0); } /* Add next node to given node in given slot. */ uword vlib_node_add_next_with_slot (vlib_main_t * vm, uword node_index, uword next_node_index, uword slot) { vlib_node_main_t *nm = &vm->node_main; vlib_node_t *node, *next, *old_next; u32 old_next_index; uword *p; ASSERT (vlib_get_thread_index () == 0); node = vec_elt (nm->nodes, node_index); next = vec_elt (nm->nodes, next_node_index); /* Runtime has to be initialized. */ ASSERT (nm->flags &
.. _relatedprojects:

#################
Related Projects
#################

.. toctree::

*****
CICN
*****

`<https://docs.fd.io/cicn>`_

************************************************
Continuous System Integration and Testing (CSIT)
************************************************

`<https://docs.fd.io/csit>`_

*******
HC2VPP
*******

`<https://docs.fd.io/hc2vpp>`_

**********
Honeycomb
**********

`<https://docs.fd.io/honeycomb>`_

****
NSH
****

`<https://docs.fd.io/nsh_sfc>`_
\ v->suffix = #s; \ v->index = CLIB_MARCH_VARIANT_TYPE_##s; \ v->priority = clib_cpu_march_priority_##s (); \ v->desc = n; foreach_march_variant; #undef _ nm->node_fn_march_variant_by_suffix = hash_create_string (0, sizeof (u32)); vec_foreach (v, nm->variants) { ASSERT (v->index == v - nm->variants); hash_set (nm->node_fn_march_variant_by_suffix, v->suffix, v->index); if (v->priority > prio) prio = v->priority; } } void vlib_register_all_static_nodes (vlib_main_t * vm) { vlib_global_main_t *vgm = vlib_get_global_main (); vlib_node_registration_t *r; static char *null_node_error_strings[] = { "blackholed packets", }; static vlib_node_registration_t null_node_reg = { .function = null_node_fn, .vector_size = sizeof (u32), .n_errors = 1, .error_strings = null_node_error_strings, }; /* make sure that node index 0 is not used by real node */ vlib_register_node (vm, &null_node_reg, "null-node"); r = vgm->node_registrations; while (r) { vlib_register_node (vm, r, "%s", r->name); r = r->next_registration; } } 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) { vlib_node_main_t *nm = &vm->node_main; vlib_node_t *n; vlib_node_t ***node_dups = *node_dupsp; vlib_node_t **nodes; vlib_main_t **stat_vms = *stat_vmsp; vlib_main_t *stat_vm; uword i, j; u32 threads_to_serialize; if (vec_len (stat_vms) == 0) { for (i = 0; i < vlib_get_n_threads (); i++) { stat_vm = vlib_get_main_by_index (i); if (stat_vm) vec_add1 (stat_vms, stat_vm); } } threads_to_serialize = clib_min (max_threads, vec_len (stat_vms)); vec_validate (node_dups, threads_to_serialize - 1); /* * Barrier sync across stats scraping. * Otherwise, the counts will be grossly inaccurate. */ if (barrier_sync) vlib_worker_thread_barrier_sync (vm); for (j = 0; j < threads_to_serialize; j++) { stat_vm = stat_vms[j]; nm = &stat_vm->node_main; if (include_stats) { for (i = 0; i < vec_len (nm->nodes); i++) { n = nm->nodes[i]; vlib_node_sync_stats (stat_vm, n); } } nodes = node_dups[j]; vec_validate (nodes, vec_len (nm->nodes) - 1); clib_memcpy (nodes, nm->nodes, vec_len (nm->nodes) * sizeof (nodes[0])); node_dups[j] = nodes; } if (barrier_sync) vlib_worker_thread_barrier_release (vm); *node_dupsp = node_dups; *stat_vmsp = stat_vms; } clib_error_t * vlib_node_main_init (vlib_main_t * vm) { vlib_node_main_t *nm = &vm->node_main; clib_error_t *error = 0; vlib_node_t *n; uword ni; nm->flags |= VLIB_NODE_MAIN_RUNTIME_STARTED; /* Generate sibling relationships */ { vlib_node_t *n, *sib; uword si; for (ni = 0; ni < vec_len (nm->nodes); ni++) { n = vec_elt (nm->nodes, ni); if (!n->sibling_of) continue; sib = vlib_get_node_by_name (vm, (u8 *) n->sibling_of); if (!sib) { error = clib_error_create ("sibling `%s' not found for node `%v'", n->sibling_of, n->name); goto done; } /* *INDENT-OFF* */ clib_bitmap_foreach (si, sib->sibling_bitmap) { vlib_node_t * m = vec_elt (nm->nodes, si); /* Connect all of sibling's siblings to us. */ m->sibling_bitmap = clib_bitmap_ori (m->sibling_bitmap, n->index); /* Connect us to all of sibling's siblings. */ n->sibling_bitmap = clib_bitmap_ori (n->sibling_bitmap, si); } /* *INDENT-ON* */ /* Connect sibling to us. */ sib->sibling_bitmap = clib_bitmap_ori (sib->sibling_bitmap, n->index); /* Connect us to sibling. */ n->sibling_bitmap = clib_bitmap_ori (n->sibling_bitmap, sib->index); } } /* Resolve next names into next indices. */ for (ni = 0; ni < vec_len (nm->nodes); ni++) { uword i; n = vec_elt (nm->nodes, ni); for (i = 0; i < vec_len (n->next_node_names); i++) { char *a = n->next_node_names[i]; if (!a) continue; if (~0 == vlib_node_add_named_next_with_slot (vm, n->index, a, i)) { error = clib_error_create ("node `%v' refers to unknown node `%s'", n->name, a); goto done; } } vec_free (n->next_node_names); } /* Set previous node pointers. */ for (ni = 0; ni < vec_len (nm->nodes); ni++) { vlib_node_t *n_next; uword i; n = vec_elt (nm->nodes, ni); for (i = 0; i < vec_len (n->next_nodes); i++) { if (n->next_nodes[i] >= vec_len (nm->nodes)) continue; n_next = vec_elt (nm->nodes, n->next_nodes[i]); n_next->prev_node_bitmap = clib_bitmap_ori (n_next->prev_node_bitmap, n->index); } } { vlib_next_frame_t *nf; vlib_node_runtime_t *r; vlib_node_t *next; uword i; vec_foreach (r, nm->nodes_by_type[VLIB_NODE_TYPE_INTERNAL]) { if (r->n_next_nodes == 0) continue; n = vlib_get_node (vm, r->node_index); nf = vec_elt_at_index (nm->next_frames, r->next_frame_index); for (i = 0; i < vec_len (n->next_nodes); i++) { next = vlib_get_node (vm, n->next_nodes[i]); /* Validate node runtime indices are correctly initialized. */ ASSERT (nf[i].node_runtime_index == next->runtime_index); nf[i].flags = 0; if (next->flags & VLIB_NODE_FLAG_FRAME_NO_FREE_AFTER_DISPATCH) nf[i].flags |= VLIB_FRAME_NO_FREE_AFTER_DISPATCH; } } } done: return error; } u32 vlib_process_create (vlib_main_t * vm, char *name, vlib_node_function_t * f, u32 log2_n_stack_bytes) { vlib_node_registration_t r; vlib_node_t *n; memset (&r, 0, sizeof (r)); r.function = f; r.process_log2_n_stack_bytes = log2_n_stack_bytes; r.type = VLIB_NODE_TYPE_PROCESS; vlib_worker_thread_barrier_sync (vm); vlib_register_node (vm, &r, "%s", name); vec_free (r.name); vlib_worker_thread_node_runtime_update (); vlib_worker_thread_barrier_release (vm); n = vlib_get_node (vm, r.index); vlib_start_process (vm, n->runtime_index); return (r.index); } int vlib_node_set_march_variant (vlib_main_t *vm, u32 node_index, clib_march_variant_type_t march_variant) { vlib_node_fn_registration_t *fnr; vlib_node_fn_variant_t *v; vlib_node_t *n = vlib_get_node (vm, node_index); if (n->node_fn_registrations == 0) return -1; fnr = n->node_fn_registrations; v = vec_elt_at_index (vm->node_main.variants, march_variant); while (fnr) { if (fnr->march_variant == v->index) { n->function = fnr->function; for (int i = 0; i < vlib_get_n_threads (); i++) { vlib_node_runtime_t *nrt; nrt = vlib_node_get_runtime (vlib_get_main_by_index (i), n->index); nrt->function = fnr->function; } return 0; } fnr = fnr->next_registration; } return -1; } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */