blob: ce15f70634c68d6e330c977155215b1092f4e53a [file] [log] [blame]
Dave Baracha638c182019-06-21 18:24:07 -04001/*
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 <handoffdemo/handoffdemo.h>
22
23typedef struct
24{
25 int current_thread;
26} handoffdemo_trace_t;
27
28/* packet trace format function */
29static u8 *
30format_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
41vlib_node_registration_t handoffdemo_node;
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
48typedef enum
49{
50#define _(sym,str) HANDOFFDEMO_ERROR_##sym,
51 foreach_handoffdemo_error
52#undef _
53 HANDOFFDEMO_N_ERROR,
54} handoffdemo_error_t;
55
56static char *handoffdemo_error_strings[] = {
57#define _(sym,string) string,
58 foreach_handoffdemo_error
59#undef _
60};
61
62typedef enum
63{
64 HANDOFFDEMO_NEXT_DROP,
65 HANDOFFDEMO_N_NEXT,
66} handoffdemo_next_t;
67
68always_inline uword
69handoffdemo_inline (vlib_main_t * vm,
70 vlib_node_runtime_t * node, vlib_frame_t * frame,
71 int which, int is_trace)
72{
73 handoffdemo_main_t *hmp = &handoffdemo_main;
74 u32 n_left_from, *from;
75 u32 error0 = node->errors[HANDOFFDEMO_ERROR_COMPLETE];
76 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
77 u16 thread_indices[VLIB_FRAME_SIZE];
78 u16 nexts[VLIB_FRAME_SIZE], *next;
79 u32 n_enq;
80 int i;
81
82 from = vlib_frame_vector_args (frame);
83 n_left_from = frame->n_vectors;
84
85 vlib_get_buffers (vm, from, bufs, n_left_from);
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 {
99 handoffdemo_trace_t *t = vlib_add_trace (vm, node, b[0],
100 sizeof (*t));
101 t->current_thread = vm->thread_index;
102 }
103
104 b += 1;
105 next += 1;
106 n_left_from -= 1;
107 }
108
109 /* Enqueue buffers to threads */
Sivaprasad Tummala7311f182021-08-31 12:23:23 +0530110 n_enq = vlib_buffer_enqueue_to_thread (
111 vm, node, hmp->frame_queue_index, from, thread_indices,
112 frame->n_vectors, 1 /* drop on congestion */);
Dave Baracha638c182019-06-21 18:24:07 -0400113 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
125 from = vlib_frame_vector_args (frame);
126 n_left_from = frame->n_vectors;
127
128 vlib_get_buffers (vm, from, bufs, n_left_from);
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 {
136 handoffdemo_trace_t *t = vlib_add_trace (vm, node, b[0],
137 sizeof (*t));
138 t->current_thread = vm->thread_index;
139 }
140
141 next[0] = HANDOFFDEMO_NEXT_DROP;
142 b[0]->error = error0;
143 next++;
144 b++;
145 n_left_from--;
146 }
147
148 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
149 frame->n_vectors);
150 }
151
152 return frame->n_vectors;
153}
154
155static uword
156handoffdemo_node_1_fn (vlib_main_t * vm,
157 vlib_node_runtime_t * node, vlib_frame_t * frame)
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* */
168VLIB_REGISTER_NODE (handoffdemo_node_1) =
169{
170 .name = "handoffdemo-1",
171 .function = handoffdemo_node_1_fn,
172 .vector_size = sizeof (u32),
173 .format_trace = format_handoffdemo_trace,
174 .type = VLIB_NODE_TYPE_INTERNAL,
175
176 .n_errors = ARRAY_LEN(handoffdemo_error_strings),
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
188uword
189handoffdemo_node_2_fn (vlib_main_t * vm,
190 vlib_node_runtime_t * node, vlib_frame_t * frame)
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* */
201VLIB_REGISTER_NODE (handoffdemo_node_2) =
202{
203 .name = "handoffdemo-2",
204 .function = handoffdemo_node_2_fn,
205 .vector_size = sizeof (u32),
206 .format_trace = format_handoffdemo_trace,
207 .type = VLIB_NODE_TYPE_INTERNAL,
208
209 .n_errors = ARRAY_LEN(handoffdemo_error_strings),
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
221static clib_error_t *
222handoffdemo_node_init (vlib_main_t * vm)
223{
224 handoffdemo_main_t *hmp = &handoffdemo_main;
225
226 hmp->frame_queue_index = vlib_frame_queue_main_init
227 (handoffdemo_node_2.index, 16);
228
229 return 0;
230}
231
232VLIB_INIT_FUNCTION (handoffdemo_node_init);
233
234/*
235 * fd.io coding-style-patch-verification: ON
236 *
237 * Local Variables:
238 * eval: (c-set-style "gnu")
239 * End:
240 */