2 * node.c - skeleton vpp engine plug-in dual-loop node skeleton
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.
17 #include <vlib/vlib.h>
18 #include <vnet/vnet.h>
19 #include <vppinfra/error.h>
20 #include <nsim/nsim.h>
30 #ifndef CLIB_MARCH_VARIANT
32 /* packet trace format function */
34 format_nsim_trace (u8 * s, va_list * args)
36 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
37 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
38 nsim_trace_t *t = va_arg (*args, nsim_trace_t *);
41 s = format (s, "NSIM: dropped, %s", t->is_lost ?
42 "simulated network loss" : "no space in ring");
44 s = format (s, "NSIM: tx time %.6f sw_if_index %d",
45 t->expires, t->tx_sw_if_index);
50 vlib_node_registration_t nsim_node;
51 #endif /* CLIB_MARCH_VARIANT */
53 #define foreach_nsim_error \
54 _(BUFFERED, "Packets buffered") \
55 _(DROPPED, "Packets dropped due to lack of space") \
56 _(LOSS, "Network loss simulation drop packets") \
57 _(REORDERED, "Packets reordered")
61 #define _(sym,str) NSIM_ERROR_##sym,
67 #ifndef CLIB_MARCH_VARIANT
68 static char *nsim_error_strings[] = {
69 #define _(sym,string) string,
73 #endif /* CLIB_MARCH_VARIANT */
82 nsim_set_actions (nsim_main_t * nsm, vlib_buffer_t ** b,
83 nsim_node_ctx_t * ctx, u32 n_actions)
87 memset (ctx->action, 0, n_actions * sizeof (ctx->action[0]));
89 if (PREDICT_FALSE (nsm->drop_fraction != 0.0))
91 for (i = 0; i < n_actions; i++)
92 if (random_f64 (&nsm->seed) <= nsm->drop_fraction)
93 ctx->action[i] |= NSIM_ACTION_DROP;
96 if (PREDICT_FALSE (nsm->reorder_fraction != 0.0))
98 for (i = 0; i < n_actions; i++)
99 if (random_f64 (&nsm->seed) <= nsm->reorder_fraction)
100 ctx->action[i] |= NSIM_ACTION_REORDER;
105 nsim_trace_buffer (vlib_main_t * vm, vlib_node_runtime_t * node,
106 vlib_buffer_t * b, nsim_node_ctx_t * ctx, u32 is_drop)
108 if (b->flags & VLIB_BUFFER_IS_TRACED)
110 nsim_trace_t *t = vlib_add_trace (vm, node, b, sizeof (*t));
111 t->expires = ctx->expires;
112 t->is_drop = is_drop;
113 t->is_lost = ctx->action[0] & NSIM_ACTION_DROP;
114 t->tx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
119 nsim_buffer_fwd_lookup (nsim_main_t * nsm, vlib_buffer_t * b,
120 u32 * next, u8 is_cross_connect)
122 if (is_cross_connect)
124 vnet_buffer (b)->sw_if_index[VLIB_TX] =
125 (vnet_buffer (b)->sw_if_index[VLIB_RX] == nsm->sw_if_index0) ?
126 nsm->sw_if_index1 : nsm->sw_if_index0;
128 (vnet_buffer (b)->sw_if_index[VLIB_TX] == nsm->sw_if_index0) ?
129 nsm->output_next_index0 : nsm->output_next_index1;
131 else /* output feature, even easier... */
133 u32 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
134 *next = nsm->output_next_index_by_sw_if_index[sw_if_index];
139 nsim_dispatch_buffer (vlib_main_t * vm, vlib_node_runtime_t * node,
140 nsim_main_t * nsm, nsim_wheel_t * wp, vlib_buffer_t * b,
141 u32 bi, nsim_node_ctx_t * ctx, u8 is_cross_connect,
144 if (PREDICT_TRUE (!(ctx->action[0] & NSIM_ACTION_DROP)))
146 if (PREDICT_FALSE (ctx->action[0] & NSIM_ACTION_REORDER))
150 vnet_get_config_data (&ctx->fcm->config_main,
151 &b->current_config_index, &next, 0);
152 ctx->reord_nexts[0] = next;
154 ctx->reord_nexts += 1;
158 nsim_wheel_entry_t *ep = wp->entries + wp->tail;
160 if (wp->tail == wp->wheel_size)
164 ep->tx_time = ctx->expires;
165 ep->rx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
166 nsim_buffer_fwd_lookup (nsm, b, &ep->output_next_index,
168 ep->tx_sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
169 ep->buffer_index = bi;
170 ctx->n_buffered += 1;
181 if (PREDICT_FALSE (is_trace))
182 nsim_trace_buffer (vm, node, b, ctx, 0);
188 nsim_inline (vlib_main_t * vm,
189 vlib_node_runtime_t * node, vlib_frame_t * frame, int is_trace,
190 int is_cross_connect)
192 nsim_main_t *nsm = &nsim_main;
193 u32 n_left_from, *from, drops[VLIB_FRAME_SIZE], reorders[VLIB_FRAME_SIZE];
194 nsim_wheel_t *wp = nsm->wheel_by_thread[vm->thread_index];
195 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
196 u16 reorders_nexts[VLIB_FRAME_SIZE];
197 u8 actions[VLIB_FRAME_SIZE];
202 from = vlib_frame_vector_args (frame);
203 n_left_from = frame->n_vectors;
205 vlib_get_buffers (vm, from, bufs, n_left_from);
208 ctx.fcm = vnet_feature_get_config_main (nsm->arc_index);
212 ctx.reord = reorders;
213 ctx.reord_nexts = reorders_nexts;
214 ctx.action = actions;
215 ctx.expires = vlib_time_now (vm) + nsm->delay;
217 nsim_set_actions (nsm, b, &ctx, n_left_from);
219 while (n_left_from >= 8)
221 vlib_prefetch_buffer_header (b[4], STORE);
222 vlib_prefetch_buffer_header (b[5], STORE);
223 vlib_prefetch_buffer_header (b[6], STORE);
224 vlib_prefetch_buffer_header (b[7], STORE);
226 if (PREDICT_FALSE (wp->cursize + 4 >= wp->wheel_size))
229 nsim_dispatch_buffer (vm, node, nsm, wp, b[0], from[0], &ctx,
230 is_cross_connect, is_trace);
231 nsim_dispatch_buffer (vm, node, nsm, wp, b[1], from[1], &ctx,
232 is_cross_connect, is_trace);
233 nsim_dispatch_buffer (vm, node, nsm, wp, b[2], from[2], &ctx,
234 is_cross_connect, is_trace);
235 nsim_dispatch_buffer (vm, node, nsm, wp, b[3], from[3], &ctx,
236 is_cross_connect, is_trace);
245 while (n_left_from > 0)
247 /* Drop if out of wheel space and not drop or reorder */
248 if (PREDICT_TRUE (wp->cursize < wp->wheel_size
249 || (ctx.action[0] & NSIM_ACTION_DROP)
250 || (ctx.action[0] & NSIM_ACTION_REORDER)))
252 nsim_dispatch_buffer (vm, node, nsm, wp, b[0], from[0], &ctx,
253 is_cross_connect, is_trace);
257 ctx.drop[0] = from[0];
259 if (PREDICT_FALSE (is_trace))
260 nsim_trace_buffer (vm, node, b[0], &ctx, 1);
269 if (PREDICT_FALSE (ctx.drop > drops))
271 u32 n_left_to_drop = ctx.drop - drops;
272 vlib_buffer_free (vm, drops, n_left_to_drop);
273 vlib_node_increment_counter (vm, node->node_index, NSIM_ERROR_LOSS,
275 vlib_node_increment_counter (vm, node->node_index, NSIM_ERROR_DROPPED,
276 n_left_to_drop - ctx.n_loss);
278 if (PREDICT_FALSE (ctx.reord > reorders))
280 u32 n_reordered = ctx.reord - reorders;
281 vlib_buffer_enqueue_to_next (vm, node, reorders, reorders_nexts,
283 vlib_node_increment_counter (vm, node->node_index, NSIM_ERROR_REORDERED,
286 vlib_node_increment_counter (vm, node->node_index,
287 NSIM_ERROR_BUFFERED, ctx.n_buffered);
288 return frame->n_vectors;
291 VLIB_NODE_FN (nsim_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
292 vlib_frame_t * frame)
294 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
295 return nsim_inline (vm, node, frame,
296 1 /* is_trace */ , 1 /* is_cross_connect */ );
298 return nsim_inline (vm, node, frame,
299 0 /* is_trace */ , 1 /* is_cross_connect */ );
303 #ifndef CLIB_MARCH_VARIANT
304 VLIB_REGISTER_NODE (nsim_node) =
307 .vector_size = sizeof (u32),
308 .format_trace = format_nsim_trace,
309 .type = VLIB_NODE_TYPE_INTERNAL,
311 .n_errors = ARRAY_LEN(nsim_error_strings),
312 .error_strings = nsim_error_strings,
314 .n_next_nodes = NSIM_N_NEXT,
316 /* edit / add dispositions here */
318 [NSIM_NEXT_DROP] = "error-drop",
321 #endif /* CLIB_MARCH_VARIANT */
324 VLIB_NODE_FN (nsim_feature_node) (vlib_main_t * vm,
325 vlib_node_runtime_t * node,
326 vlib_frame_t * frame)
328 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
329 return nsim_inline (vm, node, frame,
330 1 /* is_trace */ , 0 /* is_cross_connect */ );
332 return nsim_inline (vm, node, frame,
333 0 /* is_trace */ , 0 /* is_cross_connect */ );
337 #ifndef CLIB_MARCH_VARIANT
338 VLIB_REGISTER_NODE (nsim_feature_node) =
340 .name = "nsim-output-feature",
341 .vector_size = sizeof (u32),
342 .format_trace = format_nsim_trace,
343 .type = VLIB_NODE_TYPE_INTERNAL,
345 .n_errors = ARRAY_LEN(nsim_error_strings),
346 .error_strings = nsim_error_strings,
348 .n_next_nodes = NSIM_N_NEXT,
350 /* edit / add dispositions here */
352 [NSIM_NEXT_DROP] = "error-drop",
355 #endif /* CLIB_MARCH_VARIANT */
359 * fd.io coding-style-patch-verification: ON
362 * eval: (c-set-style "gnu")