blob: 583a2da52d8f112e29b86de9c6868a9b46f3cb32 [file] [log] [blame]
Dave Barach8d0f2f02018-03-12 09:31:36 -04001;;; Copyright (c) 2016 Cisco and/or its affiliates.
2;;; Licensed under the Apache License, Version 2.0 (the "License");
3;;; you may not use this file except in compliance with the License.
4;;; You may obtain a copy of the License at:
5;;;
6;;; http://www.apache.org/licenses/LICENSE-2.0
7;;;
8;;; Unless required by applicable law or agreed to in writing, software
9;;; distributed under the License is distributed on an "AS IS" BASIS,
10;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11;;; See the License for the specific language governing permissions and
12;;; limitations under the License.
13
Ed Warnickecb9cada2015-12-08 15:45:58 -070014;;; dual-loop-skel.el - Eliotic dual-loop node skeleton
15
16(require 'skeleton)
17
Keith Burns (alagalah)ca46d8c2016-03-18 07:22:15 -070018(define-skeleton skel-dual-loop
Ed Warnickecb9cada2015-12-08 15:45:58 -070019"Insert a skeleton dual-loop graph node"
20nil
21'(setq node-name (skeleton-read "Node Name: "))
22'(setq uc-node-name (upcase node-name))
Dave Barach82842442018-10-31 09:54:34 -040023'(setq main-p (concat (substring plugin-name 0 1) "mp"))
Ed Warnickecb9cada2015-12-08 15:45:58 -070024"
25#include <vlib/vlib.h>
26#include <vnet/vnet.h>
27#include <vnet/pg/pg.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070028
29#include <vnet/ip/ip.h>
30#include <vnet/ethernet/ethernet.h>
31
Dave Wallace526b5e82016-03-14 00:10:24 -040032#include <vppinfra/hash.h>
33#include <vppinfra/error.h>
34#include <vppinfra/elog.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070035
36typedef struct {
37 /* convenience */
38 vlib_main_t * vlib_main;
39 vnet_main_t * vnet_main;
40 ethernet_main_t * ethernet_main;
41} " node-name "_main_t;
42
43" node-name "_main_t " node-name "_main;
44
45vlib_node_registration_t " node-name "_node;
46
47typedef struct {
48 u32 next_index;
49 u32 sw_if_index;
50} " node-name "_trace_t;
51
52/* packet trace format function */
53static u8 * format_" node-name "_trace (u8 * s, va_list * args)
54{
55 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
56 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
57 " node-name "_trace_t * t = va_arg (*args, " node-name "_trace_t *);
58
59 s = format (s, \"" uc-node-name ": sw_if_index %d, next index %d\",
60 t->sw_if_index, t->next_index);
61 return s;
62}
63
64vlib_node_registration_t " node-name "_node;
65
66#define foreach_" node-name "_error \\
67_(SWAPPED, \"Mac swap packets processed\")
68
69typedef enum {
70#define _(sym,str) " uc-node-name "_ERROR_##sym,
71 foreach_" node-name "_error
72#undef _
73 " uc-node-name "_N_ERROR,
74} " node-name "_error_t;
75
76static char * " node-name "_error_strings[] = {
77#define _(sym,string) string,
78 foreach_" node-name "_error
79#undef _
80};
81
82typedef enum {
83 " uc-node-name "_NEXT_INTERFACE_OUTPUT,
84 " uc-node-name "_N_NEXT,
85} " node-name "_next_t;
86
87#define foreach_mac_address_offset \\
88_(0) \\
89_(1) \\
90_(2) \\
91_(3) \\
92_(4) \\
93_(5)
94
95static uword
96" node-name "_node_fn (vlib_main_t * vm,
97 vlib_node_runtime_t * node,
98 vlib_frame_t * frame)
99{
100 u32 n_left_from, * from, * to_next;
101 " node-name "_next_t next_index;
102 u32 pkts_swapped = 0;
103
104 from = vlib_frame_vector_args (frame);
105 n_left_from = frame->n_vectors;
106 next_index = node->cached_next_index;
107
108 while (n_left_from > 0)
109 {
110 u32 n_left_to_next;
111
112 vlib_get_next_frame (vm, node, next_index,
113 to_next, n_left_to_next);
114
115 while (n_left_from >= 4 && n_left_to_next >= 2)
116 {
117 u32 next0 = " uc-node-name "_NEXT_INTERFACE_OUTPUT;
118 u32 next1 = " uc-node-name "_NEXT_INTERFACE_OUTPUT;
119 u32 sw_if_index0, sw_if_index1;
120 u8 tmp0[6], tmp1[6];
121 ethernet_header_t *en0, *en1;
122 u32 bi0, bi1;
123 vlib_buffer_t * b0, * b1;
124
125 /* Prefetch next iteration. */
126 {
127 vlib_buffer_t * p2, * p3;
128
129 p2 = vlib_get_buffer (vm, from[2]);
130 p3 = vlib_get_buffer (vm, from[3]);
131
132 vlib_prefetch_buffer_header (p2, LOAD);
133 vlib_prefetch_buffer_header (p3, LOAD);
134
135 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
136 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
137 }
138
139 /* speculatively enqueue b0 and b1 to the current next frame */
140 to_next[0] = bi0 = from[0];
141 to_next[1] = bi1 = from[1];
142 from += 2;
143 to_next += 2;
144 n_left_from -= 2;
145 n_left_to_next -= 2;
146
147 b0 = vlib_get_buffer (vm, bi0);
148 b1 = vlib_get_buffer (vm, bi1);
149
150 /* $$$$$ Dual loop: process 2 x packets here $$$$$ */
151 ASSERT (b0->current_data == 0);
152 ASSERT (b1->current_data == 0);
153
154 en0 = vlib_buffer_get_current (b0);
155 en1 = vlib_buffer_get_current (b1);
156
157 /* This is not the fastest way to swap src + dst mac addresses */
158#define _(a) tmp0[a] = en0->src_address[a];
159 foreach_mac_address_offset;
160#undef _
161#define _(a) en0->src_address[a] = en0->dst_address[a];
162 foreach_mac_address_offset;
163#undef _
164#define _(a) en0->dst_address[a] = tmp0[a];
165 foreach_mac_address_offset;
166#undef _
167
168#define _(a) tmp1[a] = en1->src_address[a];
169 foreach_mac_address_offset;
170#undef _
171#define _(a) en1->src_address[a] = en1->dst_address[a];
172 foreach_mac_address_offset;
173#undef _
174#define _(a) en1->dst_address[a] = tmp1[a];
175 foreach_mac_address_offset;
176#undef _
177
178 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
179 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
180
181 /* Send pkt back out the RX interface */
182 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0;
183 vnet_buffer(b1)->sw_if_index[VLIB_TX] = sw_if_index1;
184
185 pkts_swapped += 2;
186 /* $$$$$ End of processing 2 x packets $$$$$ */
187
188 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)))
189 {
190 if (b0->flags & VLIB_BUFFER_IS_TRACED)
191 {
192 " node-name "_trace_t *t =
193 vlib_add_trace (vm, node, b0, sizeof (*t));
194 t->sw_if_index = sw_if_index0;
195 t->next_index = next0;
196 }
197 if (b1->flags & VLIB_BUFFER_IS_TRACED)
198 {
199 " node-name "_trace_t *t =
200 vlib_add_trace (vm, node, b1, sizeof (*t));
201 t->sw_if_index = sw_if_index1;
202 t->next_index = next1;
203 }
204 }
205
206 /* verify speculative enqueues, maybe switch current next frame */
207 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
208 to_next, n_left_to_next,
209 bi0, bi1, next0, next1);
210 }
211
212 while (n_left_from > 0 && n_left_to_next > 0)
213 {
214 u32 bi0;
215 vlib_buffer_t * b0;
216 u32 next0 = " uc-node-name "_NEXT_INTERFACE_OUTPUT;
217 u32 sw_if_index0;
218 u8 tmp0[6];
219 ethernet_header_t *en0;
220
221 /* speculatively enqueue b0 to the current next frame */
222 bi0 = from[0];
223 to_next[0] = bi0;
224 from += 1;
225 to_next += 1;
226 n_left_from -= 1;
227 n_left_to_next -= 1;
228
229 b0 = vlib_get_buffer (vm, bi0);
230
231 /* $$$$$ Single loop: process 1 packet here $$$$$ */
232
233 /*
234 * Direct from the driver, we should be at offset 0
235 * aka at &b0->data[0]
236 */
237 ASSERT (b0->current_data == 0);
238
239 en0 = vlib_buffer_get_current (b0);
240
241 /* This is not the fastest way to swap src + dst mac addresses */
242#define _(a) tmp0[a] = en0->src_address[a];
243 foreach_mac_address_offset;
244#undef _
245#define _(a) en0->src_address[a] = en0->dst_address[a];
246 foreach_mac_address_offset;
247#undef _
248#define _(a) en0->dst_address[a] = tmp0[a];
249 foreach_mac_address_offset;
250#undef _
251
252 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
253
254 /* Send pkt back out the RX interface */
255 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0;
256
257 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
258 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
259 {
260 " node-name "_trace_t *t =
261 vlib_add_trace (vm, node, b0, sizeof (*t));
262 t->sw_if_index = sw_if_index0;
263 t->next_index = next0;
264 }
265
266 pkts_swapped += 1;
267
268 /* $$$$$ Done processing 1 packet here $$$$$ */
269
270 /* verify speculative enqueue, maybe switch current next frame */
271 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
272 to_next, n_left_to_next,
273 bi0, next0);
274 }
275
276 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
277 }
278
279 vlib_node_increment_counter (vm, " node-name "_node.index,
280 " uc-node-name "_ERROR_SWAPPED, pkts_swapped);
281 return frame->n_vectors;
282}
283
284VLIB_REGISTER_NODE (" node-name "_node) = {
285 .function = " node-name "_node_fn,
286 .name = \"" node-name "\",
287 .vector_size = sizeof (u32),
288 .format_trace = format_" node-name "_trace,
289 .type = VLIB_NODE_TYPE_INTERNAL,
290
291 .n_errors = ARRAY_LEN(" node-name "_error_strings),
292 .error_strings = " node-name "_error_strings,
293
294 .n_next_nodes = " uc-node-name "_N_NEXT,
295
296 /* edit / add dispositions here */
297 .next_nodes = {
298 [" uc-node-name "_NEXT_INTERFACE_OUTPUT] = \"interface-output\",
299 },
300};
301
Dave Wallace334806c2016-03-15 01:28:58 -0400302clib_error_t *" node-name "_init (vlib_main_t *vm)
303{
Dave Barach82842442018-10-31 09:54:34 -0400304 " node-name "_main_t *" main-p " = &" node-name "_main;
Dave Wallace334806c2016-03-15 01:28:58 -0400305
306 /* $$$$$ Initialize " node-name "_main_t structure here. $$$$$ */
Dave Barach82842442018-10-31 09:54:34 -0400307 " main-p "->vlib_main = vm;
308 " main-p "->vnet_main = vnet_get_main();
309 " main-p "->ethernet_main = ethernet_get_main(vm);
Dave Wallace334806c2016-03-15 01:28:58 -0400310
311 return 0;
312}
313
314VLIB_INIT_FUNCTION(" node-name "_init);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315")
316