FD.io VPP  v21.10.1-2-g0a485f517
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>
22 
23 typedef struct
24 {
27 
28 /* packet trace format function */
29 static u8 *
30 format_handoffdemo_trace (u8 * s, va_list * args)
31 {
32  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
33  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
34  handoffdemo_trace_t *t = va_arg (*args, handoffdemo_trace_t *);
35 
36  s = format (s, "HANDOFFDEMO: current thread %d", t->current_thread);
37 
38  return s;
39 }
40 
42 
43 #define foreach_handoffdemo_error \
44 _(HANDED_OFF, "packets handed off processed") \
45 _(CONGESTION_DROP, "handoff queue congestion drops") \
46 _(COMPLETE, "completed packets")
47 
48 typedef enum
49 {
50 #define _(sym,str) HANDOFFDEMO_ERROR_##sym,
52 #undef _
55 
56 static char *handoffdemo_error_strings[] = {
57 #define _(sym,string) string,
59 #undef _
60 };
61 
62 typedef enum
63 {
67 
71  int which, int is_trace)
72 {
75  u32 error0 = node->errors[HANDOFFDEMO_ERROR_COMPLETE];
77  u16 thread_indices[VLIB_FRAME_SIZE];
79  u32 n_enq;
80  int i;
81 
83  n_left_from = frame->n_vectors;
84 
86  next = nexts;
87  b = bufs;
88 
89  /* First thread */
90  if (which == 1)
91  {
92  for (i = 0; i < frame->n_vectors; i++)
93  {
94  /* Pick a thread to handle this packet */
95  thread_indices[i] = 2;
96 
97  if (is_trace && (b[0]->flags & VLIB_BUFFER_IS_TRACED))
98  {
100  sizeof (*t));
102  }
103 
104  b += 1;
105  next += 1;
106  n_left_from -= 1;
107  }
108 
109  /* Enqueue buffers to threads */
111  vm, node, hmp->frame_queue_index, from, thread_indices,
112  frame->n_vectors, 1 /* drop on congestion */);
113  if (n_enq < frame->n_vectors)
114  vlib_node_increment_counter (vm, node->node_index,
115  HANDOFFDEMO_ERROR_CONGESTION_DROP,
116  frame->n_vectors - n_enq);
117  vlib_node_increment_counter (vm, node->node_index,
118  HANDOFFDEMO_ERROR_HANDED_OFF, n_enq);
119  return frame->n_vectors;
120  }
121  else /* Second thread */
122  {
123  u32 *from;
124 
126  n_left_from = frame->n_vectors;
127 
129  next = nexts;
130  b = bufs;
131 
132  while (n_left_from > 0)
133  {
134  if (is_trace && (b[0]->flags & VLIB_BUFFER_IS_TRACED))
135  {
137  sizeof (*t));
139  }
140 
142  b[0]->error = error0;
143  next++;
144  b++;
145  n_left_from--;
146  }
147 
149  frame->n_vectors);
150  }
151 
152  return frame->n_vectors;
153 }
154 
155 static uword
158 {
159  if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
160  return handoffdemo_inline (vm, node, frame, 1 /* which */ ,
161  1 /* is_trace */ );
162  else
163  return handoffdemo_inline (vm, node, frame, 1 /* which */ ,
164  0 /* is_trace */ );
165 }
166 
167 /* *INDENT-OFF* */
169 {
170  .name = "handoffdemo-1",
171  .function = handoffdemo_node_1_fn,
172  .vector_size = sizeof (u32),
173  .format_trace = format_handoffdemo_trace,
175 
177  .error_strings = handoffdemo_error_strings,
178 
179  .n_next_nodes = HANDOFFDEMO_N_NEXT,
180 
181  /* edit / add dispositions here */
182  .next_nodes = {
183  [HANDOFFDEMO_NEXT_DROP] = "error-drop",
184  },
185 };
186 /* *INDENT-ON* */
187 
188 uword
191 {
192  if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
193  return handoffdemo_inline (vm, node, frame, 2 /* which */ ,
194  1 /* is_trace */ );
195  else
196  return handoffdemo_inline (vm, node, frame, 2 /* which */ ,
197  0 /* is_trace */ );
198 }
199 
200 /* *INDENT-OFF* */
202 {
203  .name = "handoffdemo-2",
204  .function = handoffdemo_node_2_fn,
205  .vector_size = sizeof (u32),
206  .format_trace = format_handoffdemo_trace,
208 
210  .error_strings = handoffdemo_error_strings,
211 
212  .n_next_nodes = HANDOFFDEMO_N_NEXT,
213 
214  /* edit / add dispositions here */
215  .next_nodes = {
216  [HANDOFFDEMO_NEXT_DROP] = "error-drop",
217  },
218 };
219 /* *INDENT-ON* */
220 
221 static clib_error_t *
223 {
225 
227  (handoffdemo_node_2.index, 16);
228 
229  return 0;
230 }
231 
233 
234 /*
235  * fd.io coding-style-patch-verification: ON
236  *
237  * Local Variables:
238  * eval: (c-set-style "gnu")
239  * End:
240  */
vlib.h
HANDOFFDEMO_N_ERROR
@ HANDOFFDEMO_N_ERROR
Definition: node.c:53
handoffdemo_main_t::frame_queue_index
u32 frame_queue_index
Definition: handoffdemo.h:30
bufs
vlib_buffer_t * bufs[VLIB_FRAME_SIZE]
Definition: nat44_ei_out2in.c:717
vlib_buffer_enqueue_to_thread
static_always_inline u32 vlib_buffer_enqueue_to_thread(vlib_main_t *vm, vlib_node_runtime_t *node, u32 frame_queue_index, u32 *buffer_indices, u16 *thread_indices, u32 n_packets, int drop_on_congestion)
Definition: buffer_node.h:383
frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: nat44_ei.c:3048
handoffdemo_node_1_fn
static uword handoffdemo_node_1_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node.c:156
pg.h
handoffdemo_error_strings
static char * handoffdemo_error_strings[]
Definition: node.c:56
vlib_get_buffers
vlib_get_buffers(vm, from, b, n_left_from)
next
u16 * next
Definition: nat44_ei_out2in.c:718
VLIB_NODE_TYPE_INTERNAL
@ VLIB_NODE_TYPE_INTERNAL
Definition: node.h:72
VLIB_FRAME_SIZE
#define VLIB_FRAME_SIZE
Definition: node.h:368
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
handoffdemo_inline
static uword handoffdemo_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, int which, int is_trace)
Definition: node.c:69
u16
unsigned short u16
Definition: types.h:57
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
format_handoffdemo_trace
static u8 * format_handoffdemo_trace(u8 *s, va_list *args)
Definition: node.c:30
vlib_buffer_enqueue_to_next
vlib_buffer_enqueue_to_next(vm, node, from,(u16 *) nexts, frame->n_vectors)
vlib_frame_t
Definition: node.h:372
handoffdemo_node_2_fn
uword handoffdemo_node_2_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: node.c:189
vlib_frame_queue_main_init
u32 vlib_frame_queue_main_init(u32 node_index, u32 frame_queue_nelts)
Definition: threads.c:1561
foreach_handoffdemo_error
#define foreach_handoffdemo_error
Definition: node.c:43
which
int which
Definition: cJSON.h:234
vlib_buffer_t::error
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:145
error.h
handoffdemo_next_t
handoffdemo_next_t
Definition: node.c:62
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
VLIB_NODE_FLAG_TRACE
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:291
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
ARRAY_LEN
#define ARRAY_LEN(x)
Definition: clib.h:70
vlib_frame_vector_args
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:301
handoffdemo_node
vlib_node_registration_t handoffdemo_node
Definition: node.c:41
uword
u64 uword
Definition: types.h:112
vlib_main_t::thread_index
u32 thread_index
Definition: main.h:215
handoffdemo_trace_t
Definition: node.c:23
handoffdemo_node_1
vlib_node_registration_t handoffdemo_node_1
(constructor) VLIB_REGISTER_NODE (handoffdemo_node_1)
Definition: node.c:168
vlib_node_increment_counter
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1244
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
handoffdemo.h
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
handoffdemo_node_2
vlib_node_registration_t handoffdemo_node_2
(constructor) VLIB_REGISTER_NODE (handoffdemo_node_2)
Definition: node.c:201
format
description fragment has unexpected format
Definition: map.api:433
handoffdemo_main_t
Definition: handoffdemo.h:28
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
handoffdemo_error_t
handoffdemo_error_t
Definition: node.c:48
n_vectors
return frame n_vectors
Definition: nat44_ei_hairpinning.c:488
vlib_main_t
Definition: main.h:102
vlib_node_t
Definition: node.h:247
vlib_add_trace
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:628
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
handoffdemo_main
handoffdemo_main_t handoffdemo_main
Definition: handoffdemo.c:23
i
int i
Definition: flowhash_template.h:376
HANDOFFDEMO_NEXT_DROP
@ HANDOFFDEMO_NEXT_DROP
Definition: node.c:64
nexts
u16 nexts[VLIB_FRAME_SIZE]
Definition: nat44_ei_out2in.c:718
vnet.h
vlib_node_runtime_t
Definition: node.h:454
handoffdemo_trace_t::current_thread
int current_thread
Definition: node.c:25
HANDOFFDEMO_N_NEXT
@ HANDOFFDEMO_N_NEXT
Definition: node.c:65
from
from
Definition: nat44_ei_hairpinning.c:415
n_left_from
n_left_from
Definition: nat44_ei_hairpinning.c:416
handoffdemo_node_init
static clib_error_t * handoffdemo_node_init(vlib_main_t *vm)
Definition: node.c:222
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
flags
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105