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