blob: fafd21aeb06fc795a68be6e17d8d5d1a22fb1381 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001;;; dual-loop-skel.el - Eliotic dual-loop node skeleton
2
3(require 'skeleton)
4
5(define-skeleton dual-loop-skel
6"Insert a skeleton dual-loop graph node"
7nil
8'(setq node-name (skeleton-read "Node Name: "))
9'(setq uc-node-name (upcase node-name))
10"
11#include <vlib/vlib.h>
12#include <vnet/vnet.h>
13#include <vnet/pg/pg.h>
14#include <clib/error.h>
15
16#include <vnet/ip/ip.h>
17#include <vnet/ethernet/ethernet.h>
18
19#include <clib/hash.h>
20#include <clib/error.h>
21#include <clib/elog.h>
22
23typedef struct {
24 /* convenience */
25 vlib_main_t * vlib_main;
26 vnet_main_t * vnet_main;
27 ethernet_main_t * ethernet_main;
28} " node-name "_main_t;
29
30" node-name "_main_t " node-name "_main;
31
32vlib_node_registration_t " node-name "_node;
33
34typedef struct {
35 u32 next_index;
36 u32 sw_if_index;
37} " node-name "_trace_t;
38
39/* packet trace format function */
40static u8 * format_" node-name "_trace (u8 * s, va_list * args)
41{
42 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
43 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
44 " node-name "_trace_t * t = va_arg (*args, " node-name "_trace_t *);
45
46 s = format (s, \"" uc-node-name ": sw_if_index %d, next index %d\",
47 t->sw_if_index, t->next_index);
48 return s;
49}
50
51vlib_node_registration_t " node-name "_node;
52
53#define foreach_" node-name "_error \\
54_(SWAPPED, \"Mac swap packets processed\")
55
56typedef enum {
57#define _(sym,str) " uc-node-name "_ERROR_##sym,
58 foreach_" node-name "_error
59#undef _
60 " uc-node-name "_N_ERROR,
61} " node-name "_error_t;
62
63static char * " node-name "_error_strings[] = {
64#define _(sym,string) string,
65 foreach_" node-name "_error
66#undef _
67};
68
69typedef enum {
70 " uc-node-name "_NEXT_INTERFACE_OUTPUT,
71 " uc-node-name "_N_NEXT,
72} " node-name "_next_t;
73
74#define foreach_mac_address_offset \\
75_(0) \\
76_(1) \\
77_(2) \\
78_(3) \\
79_(4) \\
80_(5)
81
82static uword
83" node-name "_node_fn (vlib_main_t * vm,
84 vlib_node_runtime_t * node,
85 vlib_frame_t * frame)
86{
87 u32 n_left_from, * from, * to_next;
88 " node-name "_next_t next_index;
89 u32 pkts_swapped = 0;
90
91 from = vlib_frame_vector_args (frame);
92 n_left_from = frame->n_vectors;
93 next_index = node->cached_next_index;
94
95 while (n_left_from > 0)
96 {
97 u32 n_left_to_next;
98
99 vlib_get_next_frame (vm, node, next_index,
100 to_next, n_left_to_next);
101
102 while (n_left_from >= 4 && n_left_to_next >= 2)
103 {
104 u32 next0 = " uc-node-name "_NEXT_INTERFACE_OUTPUT;
105 u32 next1 = " uc-node-name "_NEXT_INTERFACE_OUTPUT;
106 u32 sw_if_index0, sw_if_index1;
107 u8 tmp0[6], tmp1[6];
108 ethernet_header_t *en0, *en1;
109 u32 bi0, bi1;
110 vlib_buffer_t * b0, * b1;
111
112 /* Prefetch next iteration. */
113 {
114 vlib_buffer_t * p2, * p3;
115
116 p2 = vlib_get_buffer (vm, from[2]);
117 p3 = vlib_get_buffer (vm, from[3]);
118
119 vlib_prefetch_buffer_header (p2, LOAD);
120 vlib_prefetch_buffer_header (p3, LOAD);
121
122 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
123 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
124 }
125
126 /* speculatively enqueue b0 and b1 to the current next frame */
127 to_next[0] = bi0 = from[0];
128 to_next[1] = bi1 = from[1];
129 from += 2;
130 to_next += 2;
131 n_left_from -= 2;
132 n_left_to_next -= 2;
133
134 b0 = vlib_get_buffer (vm, bi0);
135 b1 = vlib_get_buffer (vm, bi1);
136
137 /* $$$$$ Dual loop: process 2 x packets here $$$$$ */
138 ASSERT (b0->current_data == 0);
139 ASSERT (b1->current_data == 0);
140
141 en0 = vlib_buffer_get_current (b0);
142 en1 = vlib_buffer_get_current (b1);
143
144 /* This is not the fastest way to swap src + dst mac addresses */
145#define _(a) tmp0[a] = en0->src_address[a];
146 foreach_mac_address_offset;
147#undef _
148#define _(a) en0->src_address[a] = en0->dst_address[a];
149 foreach_mac_address_offset;
150#undef _
151#define _(a) en0->dst_address[a] = tmp0[a];
152 foreach_mac_address_offset;
153#undef _
154
155#define _(a) tmp1[a] = en1->src_address[a];
156 foreach_mac_address_offset;
157#undef _
158#define _(a) en1->src_address[a] = en1->dst_address[a];
159 foreach_mac_address_offset;
160#undef _
161#define _(a) en1->dst_address[a] = tmp1[a];
162 foreach_mac_address_offset;
163#undef _
164
165 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
166 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
167
168 /* Send pkt back out the RX interface */
169 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0;
170 vnet_buffer(b1)->sw_if_index[VLIB_TX] = sw_if_index1;
171
172 pkts_swapped += 2;
173 /* $$$$$ End of processing 2 x packets $$$$$ */
174
175 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)))
176 {
177 if (b0->flags & VLIB_BUFFER_IS_TRACED)
178 {
179 " node-name "_trace_t *t =
180 vlib_add_trace (vm, node, b0, sizeof (*t));
181 t->sw_if_index = sw_if_index0;
182 t->next_index = next0;
183 }
184 if (b1->flags & VLIB_BUFFER_IS_TRACED)
185 {
186 " node-name "_trace_t *t =
187 vlib_add_trace (vm, node, b1, sizeof (*t));
188 t->sw_if_index = sw_if_index1;
189 t->next_index = next1;
190 }
191 }
192
193 /* verify speculative enqueues, maybe switch current next frame */
194 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
195 to_next, n_left_to_next,
196 bi0, bi1, next0, next1);
197 }
198
199 while (n_left_from > 0 && n_left_to_next > 0)
200 {
201 u32 bi0;
202 vlib_buffer_t * b0;
203 u32 next0 = " uc-node-name "_NEXT_INTERFACE_OUTPUT;
204 u32 sw_if_index0;
205 u8 tmp0[6];
206 ethernet_header_t *en0;
207
208 /* speculatively enqueue b0 to the current next frame */
209 bi0 = from[0];
210 to_next[0] = bi0;
211 from += 1;
212 to_next += 1;
213 n_left_from -= 1;
214 n_left_to_next -= 1;
215
216 b0 = vlib_get_buffer (vm, bi0);
217
218 /* $$$$$ Single loop: process 1 packet here $$$$$ */
219
220 /*
221 * Direct from the driver, we should be at offset 0
222 * aka at &b0->data[0]
223 */
224 ASSERT (b0->current_data == 0);
225
226 en0 = vlib_buffer_get_current (b0);
227
228 /* This is not the fastest way to swap src + dst mac addresses */
229#define _(a) tmp0[a] = en0->src_address[a];
230 foreach_mac_address_offset;
231#undef _
232#define _(a) en0->src_address[a] = en0->dst_address[a];
233 foreach_mac_address_offset;
234#undef _
235#define _(a) en0->dst_address[a] = tmp0[a];
236 foreach_mac_address_offset;
237#undef _
238
239 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
240
241 /* Send pkt back out the RX interface */
242 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0;
243
244 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
245 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
246 {
247 " node-name "_trace_t *t =
248 vlib_add_trace (vm, node, b0, sizeof (*t));
249 t->sw_if_index = sw_if_index0;
250 t->next_index = next0;
251 }
252
253 pkts_swapped += 1;
254
255 /* $$$$$ Done processing 1 packet here $$$$$ */
256
257 /* verify speculative enqueue, maybe switch current next frame */
258 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
259 to_next, n_left_to_next,
260 bi0, next0);
261 }
262
263 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
264 }
265
266 vlib_node_increment_counter (vm, " node-name "_node.index,
267 " uc-node-name "_ERROR_SWAPPED, pkts_swapped);
268 return frame->n_vectors;
269}
270
271VLIB_REGISTER_NODE (" node-name "_node) = {
272 .function = " node-name "_node_fn,
273 .name = \"" node-name "\",
274 .vector_size = sizeof (u32),
275 .format_trace = format_" node-name "_trace,
276 .type = VLIB_NODE_TYPE_INTERNAL,
277
278 .n_errors = ARRAY_LEN(" node-name "_error_strings),
279 .error_strings = " node-name "_error_strings,
280
281 .n_next_nodes = " uc-node-name "_N_NEXT,
282
283 /* edit / add dispositions here */
284 .next_nodes = {
285 [" uc-node-name "_NEXT_INTERFACE_OUTPUT] = \"interface-output\",
286 },
287};
288
289")
290