2 * nsim.c - skeleton vpp engine plug-in
4 * Copyright (c) <current-year> <your-organization>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 #include <vnet/pg/pg.h>
21 #include <vppinfra/error.h>
22 #include <nsim/nsim.h>
30 #ifndef CLIB_MARCH_VARIANT
31 /* packet trace format function */
33 format_nsim_tx_trace (u8 * s, va_list * args)
35 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
36 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
37 nsim_tx_trace_t *t = va_arg (*args, nsim_tx_trace_t *);
39 s = format (s, "NSIM: tx at %.6f next_index %d", t->expired, t->next_index);
42 #endif /* CLIB_MARCH_VARIANT */
44 #define foreach_nsim_tx_error \
45 _(TRANSMITTED, "Packets transmitted")
49 #define _(sym,str) NSIM_TX_ERROR_##sym,
55 #ifndef CLIB_MARCH_VARIANT
56 static char *nsim_tx_error_strings[] = {
57 #define _(sym,string) string,
61 #endif /* CLIB_MARCH_VARIANT */
70 nsim_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
71 vlib_frame_t * f, int is_trace)
73 nsim_main_t *nsm = &nsim_main;
74 u32 my_thread_index = vm->thread_index;
75 nsim_wheel_t *wp = nsm->wheel_by_thread[my_thread_index];
76 f64 now = vlib_time_now (vm);
77 uword n_tx_packets = 0;
82 nsim_wheel_entry_t *ep;
84 /* Nothing on the scheduler wheel? */
88 /* First entry on the wheel isn't expired? */
89 ep = wp->entries + wp->head;
90 if (ep->tx_time > now)
93 next_index = node->cached_next_index;
95 /* Be aware: this is not the usual coding pattern */
96 while (wp->cursize && ep->tx_time <= now)
100 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
102 while (n_left_to_next > 0)
104 /* prefetch one line / 2 entries ahead */
105 if ((((uword) ep) & (CLIB_CACHE_LINE_BYTES - 1)) == 0)
106 CLIB_PREFETCH ((ep + 2), CLIB_CACHE_LINE_BYTES, LOAD);
107 /* Pick up buffer from the wheel */
108 bi0 = ep->buffer_index;
114 next0 = ep->output_next_index;
116 /* verify speculative enqueue, maybe switch current next frame */
117 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
118 to_next, n_left_to_next,
120 /* Advance to the next ring entry */
122 if (wp->head == wp->wheel_size)
125 ep = wp->entries + wp->head;
130 b0 = vlib_get_buffer (vm, bi0);
131 if (b0->flags & VLIB_BUFFER_IS_TRACED)
134 vlib_add_trace (vm, node, b0, sizeof (*t));
136 t->next_index = next0;
140 /* Out of ring entries? */
141 if (PREDICT_FALSE (wp->cursize == 0))
145 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
147 /* If the current entry hasn't expired, we're done */
148 if (ep->tx_time > now)
151 vlib_node_increment_counter (vm, node->node_index,
152 NSIM_TX_ERROR_TRANSMITTED, n_tx_packets);
156 VLIB_NODE_FN (nsim_input_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
157 vlib_frame_t * frame)
159 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
160 return nsim_input_inline (vm, node, frame, 1 /* is_trace */ );
162 return nsim_input_inline (vm, node, frame, 0 /* is_trace */ );
167 #ifndef CLIB_MARCH_VARIANT
168 VLIB_REGISTER_NODE (nsim_input_node) =
170 .type = VLIB_NODE_TYPE_INPUT,
171 .name = "nsim-wheel",
173 /* Will be enabled if/when the feature is configured */
174 .state = VLIB_NODE_STATE_DISABLED,
176 .format_trace = format_nsim_tx_trace,
178 .n_errors = NSIM_TX_N_ERROR,
179 .error_strings = nsim_tx_error_strings,
181 #endif /* CLIB_MARCH_VARIANT */
185 * fd.io coding-style-patch-verification: ON
188 * eval: (c-set-style "gnu")