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 <vnet/pg/pg.h>
20 #include <vppinfra/error.h>
21 #include <nsim/nsim.h>
31 #ifndef CLIB_MARCH_VARIANT
33 /* packet trace format function */
35 format_nsim_trace (u8 * s, va_list * args)
37 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
38 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
39 nsim_trace_t *t = va_arg (*args, nsim_trace_t *);
42 s = format (s, "NSIM: dropped, %s", t->is_lost ?
43 "simulated network loss" : "no space in ring");
45 s = format (s, "NSIM: tx time %.6f sw_if_index %d",
46 t->expires, t->tx_sw_if_index);
51 vlib_node_registration_t nsim_node;
52 #endif /* CLIB_MARCH_VARIANT */
54 #define foreach_nsim_error \
55 _(BUFFERED, "Packets buffered") \
56 _(DROPPED, "Packets dropped due to lack of space") \
57 _(LOSS, "Network loss simulation drop packets")
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_inline (vlib_main_t * vm,
83 vlib_node_runtime_t * node, vlib_frame_t * frame, int is_trace)
85 nsim_main_t *nsm = &nsim_main;
86 u32 n_left_from, *from;
87 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
88 u16 nexts[VLIB_FRAME_SIZE], *next;
89 u32 my_thread_index = vm->thread_index;
90 nsim_wheel_t *wp = nsm->wheel_by_thread[my_thread_index];
91 f64 now = vlib_time_now (vm);
92 f64 expires = now + nsm->delay;
94 u32 no_error = node->errors[NSIM_ERROR_BUFFERED];
95 u32 no_buffer_error = node->errors[NSIM_ERROR_DROPPED];
96 u32 loss_error = node->errors[NSIM_ERROR_LOSS];
97 nsim_wheel_entry_t *ep = 0;
101 from = vlib_frame_vector_args (frame);
102 n_left_from = frame->n_vectors;
104 vlib_get_buffers (vm, from, bufs, n_left_from);
108 /* There is no point in trying to do more than 1 pkt here */
109 while (n_left_from > 0)
111 b[0]->error = no_error;
112 next[0] = NSIM_NEXT_DROP;
114 if (PREDICT_TRUE (wp->cursize < wp->wheel_size))
116 if (PREDICT_FALSE (nsm->drop_fraction != 0.0))
118 /* Get a random number on the closed interval [0,1] */
119 f64 rnd = random_f64 (&nsm->seed);
121 if (rnd <= nsm->drop_fraction)
123 b[0]->error = loss_error;
129 ep = wp->entries + wp->tail;
131 if (wp->tail == wp->wheel_size)
135 ep->tx_time = expires;
137 (vnet_buffer (b[0])->sw_if_index[VLIB_RX] == nsm->sw_if_index0)
138 ? nsm->sw_if_index1 : nsm->sw_if_index0;
139 ep->current_length = vlib_buffer_length_in_chain (vm, b[0]);
140 ASSERT (ep->current_length <= WHEEL_ENTRY_DATA_SIZE);
141 clib_memcpy_fast (ep->data, vlib_buffer_get_current (b[0]),
144 else /* out of wheel space, drop pkt */
146 b[0]->error = no_buffer_error;
153 if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
155 nsim_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t));
156 t->expires = expires;
157 t->is_drop = is_drop0;
158 t->is_lost = b[0]->error == loss_error;
159 t->tx_sw_if_index = (is_drop0 == 0) ? ep->tx_sw_if_index : 0;
167 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
168 return frame->n_vectors;
171 VLIB_NODE_FN (nsim_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
172 vlib_frame_t * frame)
174 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
175 return nsim_inline (vm, node, frame, 1 /* is_trace */ );
177 return nsim_inline (vm, node, frame, 0 /* is_trace */ );
181 #ifndef CLIB_MARCH_VARIANT
182 VLIB_REGISTER_NODE (nsim_node) =
185 .vector_size = sizeof (u32),
186 .format_trace = format_nsim_trace,
187 .type = VLIB_NODE_TYPE_INTERNAL,
189 .n_errors = ARRAY_LEN(nsim_error_strings),
190 .error_strings = nsim_error_strings,
192 .n_next_nodes = NSIM_N_NEXT,
194 /* edit / add dispositions here */
196 [NSIM_NEXT_DROP] = "error-drop",
199 #endif /* CLIB_MARCH_VARIANT */
203 * fd.io coding-style-patch-verification: ON
206 * eval: (c-set-style "gnu")