LISP ONLY: maintain the emacs lisp plugin generator
[vpp.git] / extras / emacs / plugin-qsnode-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-qsnode
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 sw_if_index;
50   u32 next_index;
51 } " plugin-name "_trace_t;
52
53 #ifndef CLIB_MARCH_VARIANT
54
55 /* packet trace format function */
56 static u8 * format_" plugin-name "_trace (u8 * s, va_list * args)
57 {
58   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
59   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
60   " plugin-name "_trace_t * t = va_arg (*args, " plugin-name "_trace_t *);
61   
62   s = format (s, \"" PLUGIN-NAME ": sw_if_index %d, next index %d\\n\",
63               t->sw_if_index, t->next_index);
64   return s;
65 }
66
67 vlib_node_registration_t " plugin-name "_node;
68
69 #endif /* CLIB_MARCH_VARIANT */
70
71 #define foreach_" plugin-name "_error \\
72 _(SWAPPED, \"Mac swap packets processed\")
73
74 typedef enum 
75 {
76 #define _(sym,str) " PLUGIN-NAME "_ERROR_##sym,
77   foreach_" plugin-name "_error
78 #undef _
79   " PLUGIN-NAME "_N_ERROR,
80 } " plugin-name "_error_t;
81
82 #ifndef CLIB_MARCH_VARIANT
83 static char * " plugin-name "_error_strings[] = 
84 {
85 #define _(sym,string) string,
86   foreach_" plugin-name "_error
87 #undef _
88 };
89 #endif /* CLIB_MARCH_VARIANT */
90
91 typedef enum 
92 {
93   " PLUGIN-NAME "_NEXT_DROP,
94   " PLUGIN-NAME "_N_NEXT,
95 } " plugin-name "_next_t;
96
97 always_inline uword
98 "plugin-name"_inline (vlib_main_t * vm,
99                  vlib_node_runtime_t * node, vlib_frame_t * frame,
100                  int is_ip4, int is_trace)
101 {
102   u32 n_left_from, *from;
103   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
104   u16 nexts[VLIB_FRAME_SIZE], *next;
105
106   from = vlib_frame_vector_args (frame);
107   n_left_from = frame->n_vectors;
108
109   vlib_get_buffers (vm, from, bufs, n_left_from);
110   b = bufs;
111   next = nexts;
112
113   while (n_left_from >= 4)
114     {
115       /* Prefetch next iteration. */
116       if (PREDICT_TRUE (n_left_from >= 8))
117         {
118           vlib_prefetch_buffer_header (b[4], STORE);
119           vlib_prefetch_buffer_header (b[5], STORE);
120           vlib_prefetch_buffer_header (b[6], STORE);
121           vlib_prefetch_buffer_header (b[7], STORE);
122           CLIB_PREFETCH (b[4]->data, CLIB_CACHE_LINE_BYTES, STORE);
123           CLIB_PREFETCH (b[5]->data, CLIB_CACHE_LINE_BYTES, STORE);
124           CLIB_PREFETCH (b[6]->data, CLIB_CACHE_LINE_BYTES, STORE);
125           CLIB_PREFETCH (b[7]->data, CLIB_CACHE_LINE_BYTES, STORE);
126         }
127
128      /* $$$$ process 4x pkts right here */
129       next[0] = 0;
130       next[1] = 0;
131       next[2] = 0;
132       next[3] = 0;
133
134       if (is_trace)
135         {
136           if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
137             {
138               "plugin-name"_trace_t *t = 
139                    vlib_add_trace (vm, node, b[0], sizeof (*t));
140               t->next_index = next[0];
141               t->sw_if_index = vnet_buffer(b[0])->sw_if_index[VLIB_RX];
142             }
143           if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
144             {
145               "plugin-name"_trace_t *t = 
146                     vlib_add_trace (vm, node, b[1], sizeof (*t));
147               t->next_index = next[1];
148               t->sw_if_index = vnet_buffer(b[1])->sw_if_index[VLIB_RX];
149             }
150           if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
151             {
152               "plugin-name"_trace_t *t = 
153                     vlib_add_trace (vm, node, b[2], sizeof (*t));
154               t->next_index = next[2];
155               t->sw_if_index = vnet_buffer(b[2])->sw_if_index[VLIB_RX];
156             }
157           if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
158             {
159               "plugin-name"_trace_t *t = 
160                     vlib_add_trace (vm, node, b[3], sizeof (*t));
161               t->next_index = next[3];
162               t->sw_if_index = vnet_buffer(b[3])->sw_if_index[VLIB_RX];
163             }
164         }
165
166       b += 4;
167       next += 4;
168       n_left_from -= 4;
169     }
170
171   while (n_left_from > 0)
172     {
173
174      /* $$$$ process 1 pkt right here */
175       next[0] = 0;
176
177       if (is_trace)
178         {
179           if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
180             {
181               "plugin-name"_trace_t *t = 
182                     vlib_add_trace (vm, node, b[0], sizeof (*t));
183               t->next_index = next[0];
184               t->sw_if_index = vnet_buffer(b[0])->sw_if_index[VLIB_RX];
185             }
186         }
187
188       b += 1;
189       next += 1;
190       n_left_from -= 1;
191     }
192
193   vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
194
195   return frame->n_vectors;
196 }
197
198 VLIB_NODE_FN ("plugin-name"_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
199                              vlib_frame_t * frame)
200 {
201   if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
202     return " plugin-name "_inline (vm, node, frame, 1 /* is_ip4 */ ,
203                             1 /* is_trace */ );
204   else
205     return " plugin-name "_inline (vm, node, frame, 1 /* is_ip4 */ ,
206                             0 /* is_trace */ );
207 }
208
209 /* *INDENT-OFF* */
210 #ifndef CLIB_MARCH_VARIANT
211 VLIB_REGISTER_NODE (" plugin-name "_node) = 
212 {
213   .name = \"" plugin-name "\",
214   .vector_size = sizeof (u32),
215   .format_trace = format_" plugin-name "_trace,
216   .type = VLIB_NODE_TYPE_INTERNAL,
217   
218   .n_errors = ARRAY_LEN(" plugin-name "_error_strings),
219   .error_strings = " plugin-name "_error_strings,
220
221   .n_next_nodes = " PLUGIN-NAME "_N_NEXT,
222
223   /* edit / add dispositions here */
224   .next_nodes = {
225         [" PLUGIN-NAME "_NEXT_DROP] = \"error-drop\",
226   },
227 };
228 #endif /* CLIB_MARCH_VARIANT */
229 /* *INDENT-ON* */
230 /*
231  * fd.io coding-style-patch-verification: " capital-oh-en "
232  *
233  * Local Variables:
234  * eval: (c-set-style \"gnu\")
235  * End:
236  */
237 ")