vlib: prevent some signals from being executed on workers
[vpp.git] / extras / emacs / plugin-main-skel.el
1 ;;; plugin-main-skel.el - vpp engine plug-in "main.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-main
19 "Insert a plug-in 'main.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 '(setq main-p (concat (substring plugin-name 0 1) "mp"))
26 "/*
27  * " plugin-name ".c - skeleton vpp engine plug-in
28  *
29  * Copyright (c) <current-year> <your-organization>
30  * Licensed under the Apache License, Version 2.0 (the \"License\");
31  * you may not use this file except in compliance with the License.
32  * You may obtain a copy of the License at:
33  *
34  *     http://www.apache.org/licenses/LICENSE-2.0
35  *
36  * Unless required by applicable law or agreed to in writing, software
37  * distributed under the License is distributed on an \"AS IS\" BASIS,
38  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39  * See the License for the specific language governing permissions and
40  * limitations under the License.
41  */
42
43 #include <vnet/vnet.h>
44 #include <vnet/plugin/plugin.h>
45 #include <" plugin-name "/" plugin-name ".h>
46
47 #include <vlibapi/api.h>
48 #include <vlibmemory/api.h>
49 #include <vpp/app/version.h>
50 #include <stdbool.h>
51
52 #include <" plugin-name "/" plugin-name ".api_enum.h>
53 #include <" plugin-name "/" plugin-name ".api_types.h>
54
55 #define REPLY_MSG_ID_BASE " main-p "->msg_id_base
56 #include <vlibapi/api_helper_macros.h>
57
58 " plugin-name "_main_t " plugin-name "_main;
59
60 /* Action function shared between message handler and debug CLI */
61
62 int " plugin-name "_enable_disable (" plugin-name "_main_t * " main-p ", u32 sw_if_index,
63                                    int enable_disable)
64 {
65   vnet_sw_interface_t * sw;
66   int rv = 0;
67
68   /* Utterly wrong? */
69   if (pool_is_free_index (" main-p "->vnet_main->interface_main.sw_interfaces,
70                           sw_if_index))
71     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
72
73   /* Not a physical port? */
74   sw = vnet_get_sw_interface (" main-p "->vnet_main, sw_if_index);
75   if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
76     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
77
78   " plugin-name "_create_periodic_process (" main-p ");
79
80   vnet_feature_enable_disable (\"device-input\", \"" plugin-name "\",
81                                sw_if_index, enable_disable, 0, 0);
82
83   /* Send an event to enable/disable the periodic scanner process */
84   vlib_process_signal_event (" main-p "->vlib_main,
85                              " main-p"->periodic_node_index,
86                              " PLUGIN-NAME"_EVENT_PERIODIC_ENABLE_DISABLE,
87                             (uword)enable_disable);
88   return rv;
89 }
90
91 static clib_error_t *
92 " plugin-name "_enable_disable_command_fn (vlib_main_t * vm,
93                                    unformat_input_t * input,
94                                    vlib_cli_command_t * cmd)
95 {
96   " plugin-name "_main_t * " main-p " = &" plugin-name "_main;
97   u32 sw_if_index = ~0;
98   int enable_disable = 1;
99
100   int rv;
101
102   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
103     {
104       if (unformat (input, \"disable\"))
105         enable_disable = 0;
106       else if (unformat (input, \"%U\", unformat_vnet_sw_interface,
107                          " main-p "->vnet_main, &sw_if_index))
108         ;
109       else
110         break;
111   }
112
113   if (sw_if_index == ~0)
114     return clib_error_return (0, \"Please specify an interface...\");
115
116   rv = " plugin-name "_enable_disable (" main-p ", sw_if_index, enable_disable);
117
118   switch(rv)
119     {
120   case 0:
121     break;
122
123   case VNET_API_ERROR_INVALID_SW_IF_INDEX:
124     return clib_error_return
125       (0, \"Invalid interface, only works on physical ports\");
126     break;
127
128   case VNET_API_ERROR_UNIMPLEMENTED:
129     return clib_error_return (0, \"Device driver doesn't support redirection\");
130     break;
131
132   default:
133     return clib_error_return (0, \"" plugin-name "_enable_disable returned %d\",
134                               rv);
135     }
136   return 0;
137 }
138
139 /* *INDENT-OFF* */
140 VLIB_CLI_COMMAND (" plugin-name "_enable_disable_command, static) =
141 {
142   .path = \"" plugin-name " enable-disable\",
143   .short_help =
144   \"" plugin-name " enable-disable <interface-name> [disable]\",
145   .function = " plugin-name "_enable_disable_command_fn,
146 };
147 /* *INDENT-ON* */
148
149 /* API message handler */
150 static void vl_api_" plugin-name "_enable_disable_t_handler
151 (vl_api_" plugin-name "_enable_disable_t * mp)
152 {
153   vl_api_" plugin-name "_enable_disable_reply_t * rmp;
154   " plugin-name "_main_t * " main-p " = &" plugin-name "_main;
155   int rv;
156
157   rv = " plugin-name "_enable_disable (" main-p ", ntohl(mp->sw_if_index),
158                                       (int) (mp->enable_disable));
159
160   REPLY_MACRO(VL_API_" PLUGIN-NAME "_ENABLE_DISABLE_REPLY);
161 }
162
163 /* API definitions */
164 #include <" plugin-name "/" plugin-name ".api.c>
165
166 static clib_error_t * " plugin-name "_init (vlib_main_t * vm)
167 {
168   " plugin-name "_main_t * " main-p " = &" plugin-name "_main;
169   clib_error_t * error = 0;
170
171   " main-p "->vlib_main = vm;
172   " main-p "->vnet_main = vnet_get_main();
173
174   /* Add our API messages to the global name_crc hash table */
175   " main-p "->msg_id_base = setup_message_id_table ();
176
177   return error;
178 }
179
180 VLIB_INIT_FUNCTION (" plugin-name "_init);
181
182 /* *INDENT-OFF* */
183 VNET_FEATURE_INIT (" plugin-name ", static) =
184 {
185   .arc_name = \"device-input\",
186   .node_name = \"" plugin-name "\",
187   .runs_before = VNET_FEATURES (\"ethernet-input\"),
188 };
189 /* *INDENT-ON */
190
191 /* *INDENT-OFF* */
192 VLIB_PLUGIN_REGISTER () =
193 {
194   .version = VPP_BUILD_VER,
195   .description = \"" plugin-name " plugin description goes here\",
196 };
197 /* *INDENT-ON* */
198
199 /*
200  * fd.io coding-style-patch-verification: " capital-oh-en "
201  *
202  * Local Variables:
203  * eval: (c-set-style \"gnu\")
204  * End:
205  */
206 ")