blob: 0d88a2efff58532cccee6064fb0ab39cd1c8bebc [file] [log] [blame]
Dave Barachb852bfa2016-01-04 15:27:42 -05001;;; plugin-node-skel.el - vpp engine plug-in "node.c" skeleton
2;;;
3;;; Copyright (c) 2016 Cisco and/or its affiliates.
4;;; Licensed under the Apache License, Version 2.0 (the "License");
5;;; you may not use this file except in compliance with the License.
6;;; You may obtain a copy of the License at:
7;;;
8;;; http://www.apache.org/licenses/LICENSE-2.0
9;;;
10;;; Unless required by applicable law or agreed to in writing, software
11;;; distributed under the License is distributed on an "AS IS" BASIS,
12;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13;;; See the License for the specific language governing permissions and
14;;; limitations under the License.
15
16(require 'skeleton)
17
Keith Burns (alagalah)ca46d8c2016-03-18 07:22:15 -070018(define-skeleton skel-plugin-node
Dave Barachb852bfa2016-01-04 15:27:42 -050019"Insert a plug-in 'node.c' skeleton "
20nil
21'(if (not (boundp 'plugin-name))
22 (setq plugin-name (read-string "Plugin name: ")))
23'(setq PLUGIN-NAME (upcase plugin-name))
24"
25/*
26 * node.c - skeleton vpp engine plug-in dual-loop node skeleton
27 *
28 * Copyright (c) <current-year> <your-organization>
29 * Licensed under the Apache License, Version 2.0 (the \"License\");
30 * you may not use this file except in compliance with the License.
31 * You may obtain a copy of the License at:
32 *
33 * http://www.apache.org/licenses/LICENSE-2.0
34 *
35 * Unless required by applicable law or agreed to in writing, software
36 * distributed under the License is distributed on an \"AS IS\" BASIS,
37 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
38 * See the License for the specific language governing permissions and
39 * limitations under the License.
40 */
41#include <vlib/vlib.h>
42#include <vnet/vnet.h>
43#include <vnet/pg/pg.h>
44#include <vppinfra/error.h>
45#include <" plugin-name "/" plugin-name ".h>
46
47typedef struct {
48 u32 next_index;
49 u32 sw_if_index;
50} " plugin-name "_trace_t;
51
52/* packet trace format function */
53static u8 * format_" plugin-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 " plugin-name "_trace_t * t = va_arg (*args, " plugin-name "_trace_t *);
58
59 s = format (s, \"" PLUGIN-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 " plugin-name "_node;
65
66#define foreach_" plugin-name "_error \\
67_(SWAPPED, \"Mac swap packets processed\")
68
69typedef enum {
70#define _(sym,str) " PLUGIN-NAME "_ERROR_##sym,
71 foreach_" plugin-name "_error
72#undef _
73 " PLUGIN-NAME "_N_ERROR,
74} " plugin-name "_error_t;
75
76static char * " plugin-name "_error_strings[] = {
77#define _(sym,string) string,
78 foreach_" plugin-name "_error
79#undef _
80};
81
82typedef enum {
83 " PLUGIN-NAME "_NEXT_INTERFACE_OUTPUT,
84 " PLUGIN-NAME "_N_NEXT,
85} " plugin-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" plugin-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 " plugin-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 = " PLUGIN-NAME "_NEXT_INTERFACE_OUTPUT;
118 u32 next1 = " PLUGIN-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 ASSERT (b0->current_data == 0);
151 ASSERT (b1->current_data == 0);
152
153 en0 = vlib_buffer_get_current (b0);
154 en1 = vlib_buffer_get_current (b1);
155
156 /* This is not the fastest way to swap src + dst mac addresses */
157#define _(a) tmp0[a] = en0->src_address[a];
158 foreach_mac_address_offset;
159#undef _
160#define _(a) en0->src_address[a] = en0->dst_address[a];
161 foreach_mac_address_offset;
162#undef _
163#define _(a) en0->dst_address[a] = tmp0[a];
164 foreach_mac_address_offset;
165#undef _
166
167#define _(a) tmp1[a] = en1->src_address[a];
168 foreach_mac_address_offset;
169#undef _
170#define _(a) en1->src_address[a] = en1->dst_address[a];
171 foreach_mac_address_offset;
172#undef _
173#define _(a) en1->dst_address[a] = tmp1[a];
174 foreach_mac_address_offset;
175#undef _
176
177
178
179 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
180 sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
181
182 /* Send pkt back out the RX interface */
183 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0;
184 vnet_buffer(b1)->sw_if_index[VLIB_TX] = sw_if_index1;
185
186 pkts_swapped += 2;
187
188 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)))
189 {
190 if (b0->flags & VLIB_BUFFER_IS_TRACED)
191 {
192 " plugin-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 " plugin-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 = " PLUGIN-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 * Direct from the driver, we should be at offset 0
232 * aka at &b0->data[0]
233 */
234 ASSERT (b0->current_data == 0);
235
236 en0 = vlib_buffer_get_current (b0);
237
238 /* This is not the fastest way to swap src + dst mac addresses */
239#define _(a) tmp0[a] = en0->src_address[a];
240 foreach_mac_address_offset;
241#undef _
242#define _(a) en0->src_address[a] = en0->dst_address[a];
243 foreach_mac_address_offset;
244#undef _
245#define _(a) en0->dst_address[a] = tmp0[a];
246 foreach_mac_address_offset;
247#undef _
248
249 sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
250
251 /* Send pkt back out the RX interface */
252 vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0;
253
254 if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)
255 && (b0->flags & VLIB_BUFFER_IS_TRACED))) {
256 " plugin-name "_trace_t *t =
257 vlib_add_trace (vm, node, b0, sizeof (*t));
258 t->sw_if_index = sw_if_index0;
259 t->next_index = next0;
260 }
261
262 pkts_swapped += 1;
263
264 /* verify speculative enqueue, maybe switch current next frame */
265 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
266 to_next, n_left_to_next,
267 bi0, next0);
268 }
269
270 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
271 }
272
273 vlib_node_increment_counter (vm, " plugin-name "_node.index,
274 " PLUGIN-NAME "_ERROR_SWAPPED, pkts_swapped);
275 return frame->n_vectors;
276}
277
278VLIB_REGISTER_NODE (" plugin-name "_node) = {
279 .function = " plugin-name "_node_fn,
280 .name = \"" plugin-name "\",
281 .vector_size = sizeof (u32),
282 .format_trace = format_" plugin-name "_trace,
283 .type = VLIB_NODE_TYPE_INTERNAL,
284
285 .n_errors = ARRAY_LEN(" plugin-name "_error_strings),
286 .error_strings = " plugin-name "_error_strings,
287
288 .n_next_nodes = " PLUGIN-NAME "_N_NEXT,
289
290 /* edit / add dispositions here */
291 .next_nodes = {
292 [" PLUGIN-NAME "_NEXT_INTERFACE_OUTPUT] = \"interface-output\",
293 },
294};
295")