FD.io VPP  v18.10-32-g1161dda
Vector Packet Processing
node.c
Go to the documentation of this file.
1 /*
2  * node.c - skeleton vpp engine plug-in dual-loop node skeleton
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 #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>
22 
23 typedef struct
24 {
27  int is_drop;
28 } nsim_trace_t;
29 
30 #ifndef CLIB_MARCH_VARIANT
31 
32 /* packet trace format function */
33 static u8 *
34 format_nsim_trace (u8 * s, va_list * args)
35 {
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 *);
39 
40  if (t->is_drop)
41  s = format (s, "NSIM: ring drop");
42  else
43  s = format (s, "NSIM: tx time %.6f sw_if_index %d",
44  t->expires, t->tx_sw_if_index);
45 
46  return s;
47 }
48 
50 #endif /* CLIB_MARCH_VARIANT */
51 
52 #define foreach_nsim_error \
53 _(BUFFERED, "Packets buffered") \
54 _(DROPPED, "Packets dropped due to lack of space")
55 
56 typedef enum
57 {
58 #define _(sym,str) NSIM_ERROR_##sym,
60 #undef _
62 } nsim_error_t;
63 
64 #ifndef CLIB_MARCH_VARIANT
65 static char *nsim_error_strings[] = {
66 #define _(sym,string) string,
68 #undef _
69 };
70 #endif /* CLIB_MARCH_VARIANT */
71 
72 typedef enum
73 {
76 } nsim_next_t;
77 
80  vlib_node_runtime_t * node, vlib_frame_t * frame, int is_trace)
81 {
82  nsim_main_t *nsm = &nsim_main;
83  u32 n_left_from, *from;
84  vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
85  u16 nexts[VLIB_FRAME_SIZE], *next;
86  u32 my_thread_index = vm->thread_index;
87  nsim_wheel_t *wp = nsm->wheel_by_thread[my_thread_index];
88  f64 now = vlib_time_now (vm);
89  f64 expires = now + nsm->delay;
90  int is_drop0;
91  u32 no_error = node->errors[NSIM_ERROR_BUFFERED];
92  u32 no_buffer_error = node->errors[NSIM_ERROR_DROPPED];
93  nsim_wheel_entry_t *ep = 0;
94 
95  ASSERT (wp);
96 
97  from = vlib_frame_vector_args (frame);
98  n_left_from = frame->n_vectors;
99 
100  vlib_get_buffers (vm, from, bufs, n_left_from);
101  b = bufs;
102  next = nexts;
103 
104  /* There is no point in trying to do more than 1 pkt here */
105  while (n_left_from > 0)
106  {
107  b[0]->error = no_error;
108  next[0] = NSIM_NEXT_DROP;
109  is_drop0 = 0;
110  if (PREDICT_TRUE (wp->cursize < wp->wheel_size))
111  {
112  ep = wp->entries + wp->tail;
113  wp->tail++;
114  if (wp->tail == wp->wheel_size)
115  wp->tail = 0;
116  wp->cursize++;
117 
118  ep->tx_time = expires;
119  ep->tx_sw_if_index =
120  (vnet_buffer (b[0])->sw_if_index[VLIB_RX] == nsm->sw_if_index0)
121  ? nsm->sw_if_index1 : nsm->sw_if_index0;
125  ep->current_length);
126  }
127  else /* out of wheel space, drop pkt */
128  {
129  b[0]->error = no_buffer_error;
130  is_drop0 = 1;
131  }
132 
133  if (is_trace)
134  {
135  if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
136  {
137  nsim_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t));
138  t->expires = expires;
139  t->is_drop = is_drop0;
140  t->tx_sw_if_index = (is_drop0 == 0) ? ep->tx_sw_if_index : 0;
141  }
142  }
143 
144  b += 1;
145  next += 1;
146  n_left_from -= 1;
147  }
148  vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
149  return frame->n_vectors;
150 }
151 
153  vlib_frame_t * frame)
154 {
155  if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
156  return nsim_inline (vm, node, frame, 1 /* is_trace */ );
157  else
158  return nsim_inline (vm, node, frame, 0 /* is_trace */ );
159 }
160 
161 /* *INDENT-OFF* */
162 #ifndef CLIB_MARCH_VARIANT
164 {
165  .name = "nsim",
166  .vector_size = sizeof (u32),
167  .format_trace = format_nsim_trace,
168  .type = VLIB_NODE_TYPE_INTERNAL,
169 
170  .n_errors = ARRAY_LEN(nsim_error_strings),
171  .error_strings = nsim_error_strings,
172 
173  .n_next_nodes = NSIM_N_NEXT,
174 
175  /* edit / add dispositions here */
176  .next_nodes = {
177  [NSIM_NEXT_DROP] = "error-drop",
178  },
179 };
180 #endif /* CLIB_MARCH_VARIANT */
181 /* *INDENT-ON* */
182 
183 /*
184  * fd.io coding-style-patch-verification: ON
185  *
186  * Local Variables:
187  * eval: (c-set-style "gnu")
188  * End:
189  */
#define CLIB_UNUSED(x)
Definition: clib.h:81
#define WHEEL_ENTRY_DATA_SIZE
Definition: nsim.h:28
#define PREDICT_TRUE(x)
Definition: clib.h:108
Definition: nsim.h:30
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:227
u32 thread_index
Definition: main.h:179
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
#define VLIB_NODE_FN(node)
Definition: node.h:187
vlib_node_registration_t nsim_node
(constructor) VLIB_REGISTER_NODE (nsim_node)
Definition: node.c:49
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:472
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:263
unsigned char u8
Definition: types.h:56
u32 current_length
Definition: nsim.h:34
u32 tx_sw_if_index
Definition: node.c:26
double f64
Definition: types.h:142
u32 tx_sw_if_index
Definition: nsim.h:33
u32 cursize
Definition: nsim.h:42
#define always_inline
Definition: clib.h:94
unsigned int u32
Definition: types.h:88
f64 expires
Definition: node.c:25
#define VLIB_FRAME_SIZE
Definition: node.h:382
static char * nsim_error_strings[]
Definition: node.c:65
int is_drop
Definition: node.c:27
nsim_next_t
Definition: node.c:72
#define foreach_nsim_error
Definition: node.c:52
u8 data[WHEEL_ENTRY_DATA_SIZE]
Definition: nsim.h:36
unsigned short u16
Definition: types.h:57
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:205
#define PREDICT_FALSE(x)
Definition: clib.h:107
u32 tail
Definition: nsim.h:44
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:138
u32 flags
Definition: vhost_user.h:115
u32 sw_if_index1
Definition: nsim.h:55
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:155
u16 n_vectors
Definition: node.h:401
vlib_main_t * vm
Definition: buffer.c:294
static_always_inline void vlib_buffer_enqueue_to_next(vlib_main_t *vm, vlib_node_runtime_t *node, u32 *buffers, u16 *nexts, uword count)
Definition: buffer_node.h:332
nsim_wheel_entry_t * entries
Definition: nsim.h:45
#define clib_memcpy(a, b, c)
Definition: string.h:75
#define ARRAY_LEN(x)
Definition: clib.h:61
static uword nsim_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, int is_trace)
Definition: node.c:79
#define ASSERT(truth)
nsim_wheel_t ** wheel_by_thread
Definition: nsim.h:59
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
struct _vlib_node_registration vlib_node_registration_t
nsim_error_t
Definition: node.c:56
u32 sw_if_index0
Definition: nsim.h:55
f64 tx_time
Definition: nsim.h:32
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:267
f64 delay
Definition: nsim.h:63
#define vnet_buffer(b)
Definition: buffer.h:344
static_always_inline void vlib_get_buffers(vlib_main_t *vm, u32 *bi, vlib_buffer_t **b, int count)
Translate array of buffer indices into buffer pointers.
Definition: buffer_funcs.h:141
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:310
static u8 * format_nsim_trace(u8 *s, va_list *args)
Definition: node.c:34
u32 wheel_size
Definition: nsim.h:41
nsim_main_t nsim_main
Definition: nsim.c:59
Definition: defs.h:46