Network delay simulator plugin
[vpp.git] / src / plugins / nsim / nsim_input.c
1 /*
2  * nsim.c - skeleton vpp engine plug-in
3  *
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:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
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>
23
24 typedef struct
25 {
26   f64 expired;
27   u32 tx_sw_if_index;
28 } nsim_tx_trace_t;
29
30 #ifndef CLIB_MARCH_VARIANT
31 /* packet trace format function */
32 static u8 *
33 format_nsim_tx_trace (u8 * s, va_list * args)
34 {
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 *);
38
39   s = format (s, "NSIM: tx at %.6f sw_if_index %d",
40               t->expired, t->tx_sw_if_index);
41   return s;
42 }
43 #endif /* CLIB_MARCH_VARIANT */
44
45 vlib_node_registration_t nsim_node;
46
47 #define foreach_nsim_tx_error                      \
48 _(TX, "Packets transmitted")                    \
49 _(DROPPED, "No buffer drops")
50
51 typedef enum
52 {
53 #define _(sym,str) NSIM_TX_ERROR_##sym,
54   foreach_nsim_tx_error
55 #undef _
56     NSIM_N_ERROR,
57 } nsim_tx_error_t;
58
59 #ifndef CLIB_MARCH_VARIANT
60 static char *nsim_tx_error_strings[] = {
61 #define _(sym,string) string,
62   foreach_nsim_tx_error
63 #undef _
64 };
65 #endif /* CLIB_MARCH_VARIANT */
66
67 typedef enum
68 {
69   NSIM_NEXT_DROP,
70   NSIM_N_NEXT,
71 } nsim_next_t;
72
73 always_inline uword
74 nsim_input_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
75                    vlib_frame_t * f, int is_trace)
76 {
77   nsim_main_t *nsm = &nsim_main;
78   u32 my_thread_index = vm->thread_index;
79   u32 *my_buffer_cache = nsm->buffer_indices_by_thread[my_thread_index];
80   nsim_wheel_t *wp = nsm->wheel_by_thread[my_thread_index];
81   f64 now = vlib_time_now (vm);
82   uword n_rx_packets = 0;
83   vlib_buffer_t *b0;
84   u32 bi0, next0;
85   vlib_buffer_free_list_t *fl;
86   u32 *to_next;
87   u32 next_index;
88   u32 n_left_to_next;
89   nsim_wheel_entry_t *ep;
90
91   /* Nothing on the scheduler wheel? */
92   if (wp->cursize == 0)
93     return 0;
94
95   /* First entry on the wheel isn't expired? */
96   ep = wp->entries + wp->head;
97   if (ep->tx_time > now)
98     return n_rx_packets;
99
100   /*
101    * We use per-thread buffer caches, so we need the freelist to
102    * initialize them...
103    */
104   fl = vlib_buffer_get_free_list (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX);
105   next_index = node->cached_next_index;
106
107   while (wp->cursize)
108     {
109       /* Be aware: this is not the usual coding pattern */
110       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
111
112       while (n_left_to_next > 0 && ep->tx_time <= now)
113         {
114           /* Out of local buffer cache? */
115           if (PREDICT_FALSE (_vec_len (my_buffer_cache) == 0))
116             {
117               u32 n =
118                 vlib_buffer_alloc (vm, my_buffer_cache, VLIB_FRAME_SIZE);
119               _vec_len (my_buffer_cache) = n;
120
121               /* Ugh, drop the rest of the expired entries */
122               if (n == 0)
123                 {
124                   u32 drops = 0;
125                   while (ep->tx_time <= now && wp->cursize)
126                     {
127                       wp->head++;
128                       if (wp->head == wp->wheel_size)
129                         wp->head = 0;
130                       ep = wp->entries + wp->head;
131                       wp->cursize--;
132                       drops++;
133                     }
134                   /* Count the drops */
135                   vlib_node_increment_counter (vm, node->node_index,
136                                                NSIM_TX_ERROR_DROPPED, drops);
137                   /* Ship any pkts we already processed */
138                   vlib_put_next_frame (vm, node, next_index, n_left_to_next);
139                   return n_rx_packets + drops;
140                 }
141             }
142
143           /* Allocate a buffer */
144           bi0 = my_buffer_cache[_vec_len (my_buffer_cache) - 1];
145           _vec_len (my_buffer_cache) -= 1;
146
147           to_next[0] = bi0;
148           to_next += 1;
149           n_left_to_next -= 1;
150
151           b0 = vlib_get_buffer (vm, bi0);
152           /* Initialize the buffer */
153           vlib_buffer_init_for_free_list (b0, fl);
154
155           b0->current_data = 0;
156           b0->current_length = ep->current_length;
157
158           /* Copy data from the ring */
159           clib_memcpy (b0->data, ep->data, ep->current_length);
160           b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
161           vnet_buffer (b0)->sw_if_index[VLIB_TX] = ep->tx_sw_if_index;
162           vnet_buffer (b0)->sw_if_index[VLIB_RX] =
163             (ep->tx_sw_if_index == nsm->sw_if_index0) ? nsm->sw_if_index1 :
164             nsm->sw_if_index0;
165           next0 = (ep->tx_sw_if_index == nsm->sw_if_index0) ?
166             nsm->output_next_index0 : nsm->output_next_index1;
167
168           /* verify speculative enqueue, maybe switch current next frame */
169           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
170                                            to_next, n_left_to_next,
171                                            bi0, next0);
172           /* Advance to the next ring entry */
173           wp->head++;
174           if (wp->head == wp->wheel_size)
175             wp->head = 0;
176           wp->cursize--;
177           ep = wp->entries + wp->head;
178           n_rx_packets++;
179
180           /* Out of ring entries? */
181           if (PREDICT_FALSE (wp->cursize == 0))
182             break;
183         }
184
185       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
186
187       /* If the current entry hasn't expired, we're done */
188       if (ep->tx_time > now)
189         break;
190     }
191   return n_rx_packets;
192 }
193
194 VLIB_NODE_FN (nsim_input_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
195                                 vlib_frame_t * frame)
196 {
197   if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
198     return nsim_input_inline (vm, node, frame, 1 /* is_trace */ );
199   else
200     return nsim_input_inline (vm, node, frame, 0 /* is_trace */ );
201
202 }
203
204 /* *INDENT-OFF* */
205 #ifndef CLIB_MARCH_VARIANT
206 VLIB_REGISTER_NODE (nsim_input_node) =
207 {
208   .type = VLIB_NODE_TYPE_INPUT,
209   .name = "nsim-wheel",
210
211   /* Will be enabled if/when the feature is configured */
212   .state = VLIB_NODE_STATE_DISABLED,
213
214   .format_trace = format_nsim_tx_trace,
215
216   .n_errors = NSIM_N_ERROR,
217   .error_strings = nsim_tx_error_strings,
218 };
219 #endif /* CLIB_MARCH_VARIANT */
220 /* *INDENT-ON* */
221
222 /*
223  * fd.io coding-style-patch-verification: ON
224  *
225  * Local Variables:
226  * eval: (c-set-style "gnu")
227  * End:
228  */