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 sw_if_index %d",
40 t->expired, t->tx_sw_if_index);
43 #endif /* CLIB_MARCH_VARIANT */
45 #define foreach_nsim_tx_error \
46 _(TX, "Packets transmitted") \
47 _(DROPPED, "No buffer drops")
51 #define _(sym,str) NSIM_TX_ERROR_##sym,
57 #ifndef CLIB_MARCH_VARIANT
58 static char *nsim_tx_error_strings[] = {
59 #define _(sym,string) string,
63 #endif /* CLIB_MARCH_VARIANT */
72 nsim_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
73 vlib_frame_t * f, int is_trace)
75 nsim_main_t *nsm = &nsim_main;
76 u32 my_thread_index = vm->thread_index;
77 u32 *my_buffer_cache = nsm->buffer_indices_by_thread[my_thread_index];
78 nsim_wheel_t *wp = nsm->wheel_by_thread[my_thread_index];
79 f64 now = vlib_time_now (vm);
80 uword n_rx_packets = 0;
83 vlib_buffer_free_list_t *fl;
87 nsim_wheel_entry_t *ep;
89 /* Nothing on the scheduler wheel? */
93 /* First entry on the wheel isn't expired? */
94 ep = wp->entries + wp->head;
95 if (ep->tx_time > now)
99 * We use per-thread buffer caches, so we need the freelist to
102 fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
103 next_index = node->cached_next_index;
107 /* Be aware: this is not the usual coding pattern */
108 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
110 while (n_left_to_next > 0 && ep->tx_time <= now)
112 /* Out of local buffer cache? */
113 if (PREDICT_FALSE (_vec_len (my_buffer_cache) == 0))
116 vlib_buffer_alloc (vm, my_buffer_cache, VLIB_FRAME_SIZE);
117 _vec_len (my_buffer_cache) = n;
119 /* Ugh, drop the rest of the expired entries */
123 while (ep->tx_time <= now && wp->cursize)
126 if (wp->head == wp->wheel_size)
128 ep = wp->entries + wp->head;
132 /* Count the drops */
133 vlib_node_increment_counter (vm, node->node_index,
134 NSIM_TX_ERROR_DROPPED, drops);
135 /* Ship any pkts we already processed */
136 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
137 return n_rx_packets + drops;
141 /* Allocate a buffer */
142 bi0 = my_buffer_cache[_vec_len (my_buffer_cache) - 1];
143 _vec_len (my_buffer_cache) -= 1;
149 b0 = vlib_get_buffer (vm, bi0);
150 /* Initialize the buffer */
151 vlib_buffer_init_for_free_list (b0, fl);
153 b0->current_data = 0;
154 b0->current_length = ep->current_length;
156 /* Copy data from the ring */
157 clib_memcpy (b0->data, ep->data, ep->current_length);
158 b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
159 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ep->tx_sw_if_index;
160 vnet_buffer (b0)->sw_if_index[VLIB_RX] =
161 (ep->tx_sw_if_index == nsm->sw_if_index0) ? nsm->sw_if_index1 :
163 next0 = (ep->tx_sw_if_index == nsm->sw_if_index0) ?
164 nsm->output_next_index0 : nsm->output_next_index1;
166 /* verify speculative enqueue, maybe switch current next frame */
167 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
168 to_next, n_left_to_next,
170 /* Advance to the next ring entry */
172 if (wp->head == wp->wheel_size)
175 ep = wp->entries + wp->head;
178 /* Out of ring entries? */
179 if (PREDICT_FALSE (wp->cursize == 0))
183 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
185 /* If the current entry hasn't expired, we're done */
186 if (ep->tx_time > now)
192 VLIB_NODE_FN (nsim_input_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
193 vlib_frame_t * frame)
195 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
196 return nsim_input_inline (vm, node, frame, 1 /* is_trace */ );
198 return nsim_input_inline (vm, node, frame, 0 /* is_trace */ );
203 #ifndef CLIB_MARCH_VARIANT
204 VLIB_REGISTER_NODE (nsim_input_node) =
206 .type = VLIB_NODE_TYPE_INPUT,
207 .name = "nsim-wheel",
209 /* Will be enabled if/when the feature is configured */
210 .state = VLIB_NODE_STATE_DISABLED,
212 .format_trace = format_nsim_tx_trace,
214 .n_errors = NSIM_N_ERROR,
215 .error_strings = nsim_tx_error_strings,
217 #endif /* CLIB_MARCH_VARIANT */
221 * fd.io coding-style-patch-verification: ON
224 * eval: (c-set-style "gnu")