LISP ONLY: maintain the emacs lisp plugin generator
[vpp.git] / extras / emacs / plugin-node-skel.el
1 ;;; 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
18 (define-skeleton skel-plugin-node
19 "Insert a plug-in 'node.c' skeleton "
20 nil
21 '(if (not (boundp 'plugin-name))
22      (setq plugin-name (read-string "Plugin name: ")))
23 '(setq PLUGIN-NAME (upcase plugin-name))
24 '(setq capital-oh-en "ON")
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
47 typedef struct 
48 {
49   u32 next_index;
50   u32 sw_if_index;
51   u8 new_src_mac[6];
52   u8 new_dst_mac[6];
53 } " plugin-name "_trace_t;
54
55 #ifndef CLIB_MARCH_VARIANT
56 static u8 *
57 format_mac_address (u8 * s, va_list * args)
58 {
59   u8 *a = va_arg (*args, u8 *);
60   return format (s, \"%02x:%02x:%02x:%02x:%02x:%02x\",
61                  a[0], a[1], a[2], a[3], a[4], a[5]);
62 }
63
64 /* packet trace format function */
65 static u8 * format_" plugin-name "_trace (u8 * s, va_list * args)
66 {
67   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
68   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
69   " plugin-name "_trace_t * t = va_arg (*args, " plugin-name "_trace_t *);
70   
71   s = format (s, \"" PLUGIN-NAME ": sw_if_index %d, next index %d\\n\",
72               t->sw_if_index, t->next_index);
73   s = format (s, \"  new src %U -> new dst %U\",
74               format_mac_address, t->new_src_mac, 
75               format_mac_address, t->new_dst_mac);
76   return s;
77 }
78
79 vlib_node_registration_t " plugin-name "_node;
80
81 #endif /* CLIB_MARCH_VARIANT */
82
83 #define foreach_" plugin-name "_error \\
84 _(SWAPPED, \"Mac swap packets processed\")
85
86 typedef enum {
87 #define _(sym,str) " PLUGIN-NAME "_ERROR_##sym,
88   foreach_" plugin-name "_error
89 #undef _
90   " PLUGIN-NAME "_N_ERROR,
91 } " plugin-name "_error_t;
92
93 #ifndef CLIB_MARCH_VARIANT
94 static char * " plugin-name "_error_strings[] = 
95 {
96 #define _(sym,string) string,
97   foreach_" plugin-name "_error
98 #undef _
99 };
100 #endif /* CLIB_MARCH_VARIANT */
101
102 typedef enum 
103 {
104   " PLUGIN-NAME "_NEXT_INTERFACE_OUTPUT,
105   " PLUGIN-NAME "_N_NEXT,
106 } " plugin-name "_next_t;
107
108 #define foreach_mac_address_offset              \\
109 _(0)                                            \\
110 _(1)                                            \\
111 _(2)                                            \\
112 _(3)                                            \\
113 _(4)                                            \\
114 _(5)
115
116
117 VLIB_NODE_FN (" plugin-name "_node) (vlib_main_t * vm,
118                   vlib_node_runtime_t * node,
119                   vlib_frame_t * frame)
120 {
121   u32 n_left_from, * from, * to_next;
122   " plugin-name "_next_t next_index;
123   u32 pkts_swapped = 0;
124
125   from = vlib_frame_vector_args (frame);
126   n_left_from = frame->n_vectors;
127   next_index = node->cached_next_index;
128
129   while (n_left_from > 0)
130     {
131       u32 n_left_to_next;
132
133       vlib_get_next_frame (vm, node, next_index,
134                            to_next, n_left_to_next);
135
136       while (n_left_from >= 4 && n_left_to_next >= 2)
137         {
138           u32 next0 = " PLUGIN-NAME "_NEXT_INTERFACE_OUTPUT;
139           u32 next1 = " PLUGIN-NAME "_NEXT_INTERFACE_OUTPUT;
140           u32 sw_if_index0, sw_if_index1;
141           u8 tmp0[6], tmp1[6];
142           ethernet_header_t *en0, *en1;
143           u32 bi0, bi1;
144           vlib_buffer_t * b0, * b1;
145           
146           /* Prefetch next iteration. */
147           {
148             vlib_buffer_t * p2, * p3;
149             
150             p2 = vlib_get_buffer (vm, from[2]);
151             p3 = vlib_get_buffer (vm, from[3]);
152             
153             vlib_prefetch_buffer_header (p2, LOAD);
154             vlib_prefetch_buffer_header (p3, LOAD);
155
156             CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
157             CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
158           }
159
160           /* speculatively enqueue b0 and b1 to the current next frame */
161           to_next[0] = bi0 = from[0];
162           to_next[1] = bi1 = from[1];
163           from += 2;
164           to_next += 2;
165           n_left_from -= 2;
166           n_left_to_next -= 2;
167
168           b0 = vlib_get_buffer (vm, bi0);
169           b1 = vlib_get_buffer (vm, bi1);
170
171           ASSERT (b0->current_data == 0);
172           ASSERT (b1->current_data == 0);
173           
174           en0 = vlib_buffer_get_current (b0);
175           en1 = vlib_buffer_get_current (b1);
176
177           /* This is not the fastest way to swap src + dst mac addresses */
178 #define _(a) tmp0[a] = en0->src_address[a];
179           foreach_mac_address_offset;
180 #undef _
181 #define _(a) en0->src_address[a] = en0->dst_address[a];
182           foreach_mac_address_offset;
183 #undef _
184 #define _(a) en0->dst_address[a] = tmp0[a];
185           foreach_mac_address_offset;
186 #undef _
187
188 #define _(a) tmp1[a] = en1->src_address[a];
189           foreach_mac_address_offset;
190 #undef _
191 #define _(a) en1->src_address[a] = en1->dst_address[a];
192           foreach_mac_address_offset;
193 #undef _
194 #define _(a) en1->dst_address[a] = tmp1[a];
195           foreach_mac_address_offset;
196 #undef _
197
198           sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
199           sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
200
201           /* Send pkt back out the RX interface */
202           vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0;
203           vnet_buffer(b1)->sw_if_index[VLIB_TX] = sw_if_index1;
204
205           pkts_swapped += 2;
206
207           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE)))
208             {
209               if (b0->flags & VLIB_BUFFER_IS_TRACED) 
210                 {
211                     " plugin-name "_trace_t *t = 
212                       vlib_add_trace (vm, node, b0, sizeof (*t));
213                     t->sw_if_index = sw_if_index0;
214                     t->next_index = next0;
215                     clib_memcpy (t->new_src_mac, en0->src_address,
216                                  sizeof (t->new_src_mac));
217                     clib_memcpy (t->new_dst_mac, en0->dst_address,
218                                  sizeof (t->new_dst_mac));
219                   }
220                 if (b1->flags & VLIB_BUFFER_IS_TRACED) 
221                   {
222                     " plugin-name "_trace_t *t = 
223                       vlib_add_trace (vm, node, b1, sizeof (*t));
224                     t->sw_if_index = sw_if_index1;
225                     t->next_index = next1;
226                     clib_memcpy (t->new_src_mac, en1->src_address,
227                                  sizeof (t->new_src_mac));
228                     clib_memcpy (t->new_dst_mac, en1->dst_address,
229                                  sizeof (t->new_dst_mac));
230                   }
231               }
232             
233             /* verify speculative enqueues, maybe switch current next frame */
234             vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
235                                              to_next, n_left_to_next,
236                                              bi0, bi1, next0, next1);
237         }
238
239       while (n_left_from > 0 && n_left_to_next > 0)
240         {
241           u32 bi0;
242           vlib_buffer_t * b0;
243           u32 next0 = " PLUGIN-NAME "_NEXT_INTERFACE_OUTPUT;
244           u32 sw_if_index0;
245           u8 tmp0[6];
246           ethernet_header_t *en0;
247
248           /* speculatively enqueue b0 to the current next frame */
249           bi0 = from[0];
250           to_next[0] = bi0;
251           from += 1;
252           to_next += 1;
253           n_left_from -= 1;
254           n_left_to_next -= 1;
255
256           b0 = vlib_get_buffer (vm, bi0);
257           /* 
258            * Direct from the driver, we should be at offset 0
259            * aka at &b0->data[0]
260            */
261           ASSERT (b0->current_data == 0);
262           
263           en0 = vlib_buffer_get_current (b0);
264
265           /* This is not the fastest way to swap src + dst mac addresses */
266 #define _(a) tmp0[a] = en0->src_address[a];
267           foreach_mac_address_offset;
268 #undef _
269 #define _(a) en0->src_address[a] = en0->dst_address[a];
270           foreach_mac_address_offset;
271 #undef _
272 #define _(a) en0->dst_address[a] = tmp0[a];
273           foreach_mac_address_offset;
274 #undef _
275
276           sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
277
278           /* Send pkt back out the RX interface */
279           vnet_buffer(b0)->sw_if_index[VLIB_TX] = sw_if_index0;
280
281           if (PREDICT_FALSE((node->flags & VLIB_NODE_FLAG_TRACE) 
282                             && (b0->flags & VLIB_BUFFER_IS_TRACED))) {
283             " plugin-name "_trace_t *t = 
284                vlib_add_trace (vm, node, b0, sizeof (*t));
285             t->sw_if_index = sw_if_index0;
286             t->next_index = next0;
287             clib_memcpy (t->new_src_mac, en0->src_address,
288                          sizeof (t->new_src_mac));
289             clib_memcpy (t->new_dst_mac, en0->dst_address,
290                          sizeof (t->new_dst_mac));
291             }
292             
293           pkts_swapped += 1;
294
295           /* verify speculative enqueue, maybe switch current next frame */
296           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
297                                            to_next, n_left_to_next,
298                                            bi0, next0);
299         }
300
301       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
302     }
303
304   vlib_node_increment_counter (vm, " plugin-name "_node.index, 
305                                " PLUGIN-NAME "_ERROR_SWAPPED, pkts_swapped);
306   return frame->n_vectors;
307 }
308
309 /* *INDENT-OFF* */
310 #ifndef CLIB_MARCH_VARIANT
311 VLIB_REGISTER_NODE (" plugin-name "_node) = 
312 {
313   .name = \"" plugin-name "\",
314   .vector_size = sizeof (u32),
315   .format_trace = format_" plugin-name "_trace,
316   .type = VLIB_NODE_TYPE_INTERNAL,
317   
318   .n_errors = ARRAY_LEN(" plugin-name "_error_strings),
319   .error_strings = " plugin-name "_error_strings,
320
321   .n_next_nodes = " PLUGIN-NAME "_N_NEXT,
322
323   /* edit / add dispositions here */
324   .next_nodes = {
325         [" PLUGIN-NAME "_NEXT_INTERFACE_OUTPUT] = \"interface-output\",
326   },
327 };
328 #endif /* CLIB_MARCH_VARIANT */
329 /* *INDENT-ON* */
330 /*
331  * fd.io coding-style-patch-verification: " capital-oh-en "
332  *
333  * Local Variables:
334  * eval: (c-set-style \"gnu\")
335  * End:
336  */
337 ")