FD.io VPP  v19.04.2-12-g66b1689
Vector Packet Processing
nsim_input.c
Go to the documentation of this file.
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 {
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 #define foreach_nsim_tx_error \
46 _(TX, "Packets transmitted") \
47 _(DROPPED, "No buffer drops")
48 
49 typedef enum
50 {
51 #define _(sym,str) NSIM_TX_ERROR_##sym,
53 #undef _
56 
57 #ifndef CLIB_MARCH_VARIANT
58 static char *nsim_tx_error_strings[] = {
59 #define _(sym,string) string,
61 #undef _
62 };
63 #endif /* CLIB_MARCH_VARIANT */
64 
65 typedef enum
66 {
69 } nsim_next_t;
70 
73  vlib_frame_t * f, int is_trace)
74 {
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  u32 n_trace = vlib_get_trace_count (vm, node);
80  f64 now = vlib_time_now (vm);
81  uword n_rx_packets = 0;
82  vlib_buffer_t *b0;
83  u32 bi0, next0;
84  u32 *to_next;
85  u32 next_index;
86  u32 n_left_to_next;
88 
89  /* Nothing on the scheduler wheel? */
90  if (wp->cursize == 0)
91  return 0;
92 
93  /* First entry on the wheel isn't expired? */
94  ep = wp->entries + wp->head;
95  if (ep->tx_time > now)
96  return n_rx_packets;
97 
98  /*
99  * We use per-thread buffer caches, so we need the freelist to
100  * initialize them...
101  */
102  next_index = node->cached_next_index;
103 
104  while (wp->cursize)
105  {
106  /* Be aware: this is not the usual coding pattern */
107  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
108 
109  while (n_left_to_next > 0 && ep->tx_time <= now)
110  {
111  /* Out of local buffer cache? */
112  if (PREDICT_FALSE (_vec_len (my_buffer_cache) == 0))
113  {
114  u32 n =
115  vlib_buffer_alloc (vm, my_buffer_cache, VLIB_FRAME_SIZE);
116  _vec_len (my_buffer_cache) = n;
117 
118  /* Ugh, drop the rest of the expired entries */
119  if (n == 0)
120  {
121  u32 drops = 0;
122  while (ep->tx_time <= now && wp->cursize)
123  {
124  wp->head++;
125  if (wp->head == wp->wheel_size)
126  wp->head = 0;
127  ep = wp->entries + wp->head;
128  wp->cursize--;
129  drops++;
130  }
131  /* Count the drops */
133  NSIM_TX_ERROR_DROPPED, drops);
134  /* Ship any pkts we already processed */
135  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
136  return n_rx_packets + drops;
137  }
138  }
139 
140  /* Allocate a buffer */
141  bi0 = my_buffer_cache[_vec_len (my_buffer_cache) - 1];
142  _vec_len (my_buffer_cache) -= 1;
143 
144  to_next[0] = bi0;
145  to_next += 1;
146  n_left_to_next -= 1;
147 
148  b0 = vlib_get_buffer (vm, bi0);
149  /* Initialize the buffer */
150 
151  b0->current_data = 0;
152  b0->current_length = ep->current_length;
153 
155 
156  if (PREDICT_FALSE (n_trace))
157  {
158  nsim_tx_trace_t *t0;
159  vlib_trace_buffer (vm, node, next_index, b0,
160  0 /* follow_chain */ );
161  t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
162  t0->expired = ep->tx_time;
163  t0->tx_sw_if_index = ep->tx_sw_if_index;
164  }
165 
166  /* Copy data from the ring */
167  clib_memcpy_fast (b0->data, ep->data, ep->current_length);
168  b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
169  vnet_buffer (b0)->sw_if_index[VLIB_TX] = ep->tx_sw_if_index;
170  vnet_buffer (b0)->sw_if_index[VLIB_RX] =
171  (ep->tx_sw_if_index == nsm->sw_if_index0) ? nsm->sw_if_index1 :
172  nsm->sw_if_index0;
173  next0 = (ep->tx_sw_if_index == nsm->sw_if_index0) ?
175 
176  /* verify speculative enqueue, maybe switch current next frame */
177  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
178  to_next, n_left_to_next,
179  bi0, next0);
180  /* Advance to the next ring entry */
181  wp->head++;
182  if (wp->head == wp->wheel_size)
183  wp->head = 0;
184  wp->cursize--;
185  ep = wp->entries + wp->head;
186  n_rx_packets++;
187 
188  /* Out of ring entries? */
189  if (PREDICT_FALSE (wp->cursize == 0))
190  break;
191  }
192 
193  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
194 
195  /* If the current entry hasn't expired, we're done */
196  if (ep->tx_time > now)
197  break;
198  }
199  return n_rx_packets;
200 }
201 
203  vlib_frame_t * frame)
204 {
205  if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
206  return nsim_input_inline (vm, node, frame, 1 /* is_trace */ );
207  else
208  return nsim_input_inline (vm, node, frame, 0 /* is_trace */ );
209 
210 }
211 
212 /* *INDENT-OFF* */
213 #ifndef CLIB_MARCH_VARIANT
215 {
216  .type = VLIB_NODE_TYPE_INPUT,
217  .name = "nsim-wheel",
218 
219  /* Will be enabled if/when the feature is configured */
220  .state = VLIB_NODE_STATE_DISABLED,
221 
222  .format_trace = format_nsim_tx_trace,
223 
224  .n_errors = NSIM_N_ERROR,
225  .error_strings = nsim_tx_error_strings,
226 };
227 #endif /* CLIB_MARCH_VARIANT */
228 /* *INDENT-ON* */
229 
230 /*
231  * fd.io coding-style-patch-verification: ON
232  *
233  * Local Variables:
234  * eval: (c-set-style "gnu")
235  * End:
236  */
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:124
static char * nsim_tx_error_strings[]
Definition: nsim_input.c:58
#define CLIB_UNUSED(x)
Definition: clib.h:82
static u32 vlib_get_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt)
Definition: trace_funcs.h:156
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:110
Definition: nsim.h:30
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:255
u32 thread_index
Definition: main.h:197
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
u8 data[0]
Packet data.
Definition: buffer.h:181
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
#define VLIB_NODE_FN(node)
Definition: node.h:201
u32 output_next_index0
Definition: nsim.h:56
unsigned char u8
Definition: types.h:56
u32 current_length
Definition: nsim.h:34
double f64
Definition: types.h:142
static u8 * format_nsim_tx_trace(u8 *s, va_list *args)
Definition: nsim_input.c:33
static void vlib_trace_buffer(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, vlib_buffer_t *b, int follow_chain)
Definition: trace_funcs.h:114
u32 tx_sw_if_index
Definition: nsim.h:33
u32 cursize
Definition: nsim.h:42
#define always_inline
Definition: clib.h:98
unsigned int u32
Definition: types.h:88
nsim_tx_error_t
Definition: nsim_input.c:49
#define VLIB_FRAME_SIZE
Definition: node.h:376
#define foreach_nsim_tx_error
Definition: nsim_input.c:45
u8 data[WHEEL_ENTRY_DATA_SIZE]
Definition: nsim.h:36
nsim_next_t
Definition: nsim_input.c:65
#define PREDICT_FALSE(x)
Definition: clib.h:111
u32 node_index
Node index.
Definition: node.h:495
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:218
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:368
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1180
u32 sw_if_index1
Definition: nsim.h:55
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
vlib_main_t * vm
Definition: buffer.c:312
static uword nsim_input_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *f, int is_trace)
Definition: nsim_input.c:72
nsim_wheel_entry_t * entries
Definition: nsim.h:45
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:465
u32 head
Definition: nsim.h:43
u16 cached_next_index
Next frame index that vector arguments were last enqueued to last time this node ran.
Definition: node.h:514
nsim_wheel_t ** wheel_by_thread
Definition: nsim.h:61
vlib_node_registration_t nsim_input_node
(constructor) VLIB_REGISTER_NODE (nsim_input_node)
Definition: nsim_input.c:214
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:57
Definition: defs.h:47
u32 sw_if_index0
Definition: nsim.h:55
f64 tx_time
Definition: nsim.h:32
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:451
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
u32 ** buffer_indices_by_thread
Definition: nsim.h:62
#define vnet_buffer(b)
Definition: buffer.h:369
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:301
static u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:612
u32 wheel_size
Definition: nsim.h:41
u32 output_next_index1
Definition: nsim.h:56
nsim_main_t nsim_main
Definition: nsim.c:59
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
Definition: defs.h:46