blob: e8969c85488a14a4de72acc5467b426fadf6484f [file] [log] [blame]
Dave Barachb852bfa2016-01-04 15:27:42 -05001;;; 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
Keith Burns (alagalah)ca46d8c2016-03-18 07:22:15 -070018(define-skeleton skel-plugin-main
Dave Barachb852bfa2016-01-04 15:27:42 -050019"Insert a plug-in 'main.c' skeleton "
20nil
21'(if (not (boundp 'plugin-name))
22 (setq plugin-name (read-string "Plugin name: ")))
23'(setq PLUGIN-NAME (upcase plugin-name))
Dave Barachfed79e82017-02-10 11:57:46 -050024'(setq capital-oh-en "ON")
Dave Barach82842442018-10-31 09:54:34 -040025'(setq main-p (concat (substring plugin-name 0 1) "mp"))
Dave Barachfed79e82017-02-10 11:57:46 -050026"/*
Dave Barach557d1282016-11-10 14:22:49 -050027 * " plugin-name ".c - skeleton vpp engine plug-in
Dave Barachb852bfa2016-01-04 15:27:42 -050028 *
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>
Dave Barachfed79e82017-02-10 11:57:46 -050049#include <vpp/app/version.h>
Dave Barach913f4c92019-05-29 09:59:51 -040050#include <stdbool.h>
Dave Barachb852bfa2016-01-04 15:27:42 -050051
Dave Barach4ec6f6b2019-10-01 12:10:57 -040052#include <" plugin-name "/" plugin-name ".api_enum.h>
53#include <" plugin-name "/" plugin-name ".api_types.h>
Dave Barachb852bfa2016-01-04 15:27:42 -050054
Dave Barach82842442018-10-31 09:54:34 -040055#define REPLY_MSG_ID_BASE " main-p "->msg_id_base
Dave Barach10440432018-04-03 14:58:14 -040056#include <vlibapi/api_helper_macros.h>
Dave Barachb852bfa2016-01-04 15:27:42 -050057
Dave Barach3f2e7752018-10-03 16:10:04 -040058" plugin-name "_main_t " plugin-name "_main;
Dave Barachb852bfa2016-01-04 15:27:42 -050059
Dave Barachb852bfa2016-01-04 15:27:42 -050060/* Action function shared between message handler and debug CLI */
61
Dave Barach82842442018-10-31 09:54:34 -040062int " plugin-name "_enable_disable (" plugin-name "_main_t * " main-p ", u32 sw_if_index,
Dave Barachb852bfa2016-01-04 15:27:42 -050063 int enable_disable)
64{
65 vnet_sw_interface_t * sw;
Dave Barachb7e2f3d2016-11-08 16:47:34 -050066 int rv = 0;
Dave Barachb852bfa2016-01-04 15:27:42 -050067
68 /* Utterly wrong? */
Dave Barach82842442018-10-31 09:54:34 -040069 if (pool_is_free_index (" main-p "->vnet_main->interface_main.sw_interfaces,
Dave Barachb852bfa2016-01-04 15:27:42 -050070 sw_if_index))
71 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
72
73 /* Not a physical port? */
Dave Barach82842442018-10-31 09:54:34 -040074 sw = vnet_get_sw_interface (" main-p "->vnet_main, sw_if_index);
Dave Barachb852bfa2016-01-04 15:27:42 -050075 if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
76 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
Dave Barach557d1282016-11-10 14:22:49 -050077
Dave Barach11965c72019-05-28 16:31:05 -040078 " plugin-name "_create_periodic_process (" main-p ");
79
Dave Barachb7e2f3d2016-11-08 16:47:34 -050080 vnet_feature_enable_disable (\"device-input\", \"" plugin-name "\",
81 sw_if_index, enable_disable, 0, 0);
82
Dave Barachf4addbd2018-04-30 13:03:46 -040083 /* Send an event to enable/disable the periodic scanner process */
Dave Barach11965c72019-05-28 16:31:05 -040084 vlib_process_signal_event (" main-p "->vlib_main,
85 " main-p"->periodic_node_index,
86 " PLUGIN-NAME"_EVENT_PERIODIC_ENABLE_DISABLE,
Dave Barachf4addbd2018-04-30 13:03:46 -040087 (uword)enable_disable);
Dave Barachb852bfa2016-01-04 15:27:42 -050088 return rv;
89}
90
91static 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{
Dave Barach82842442018-10-31 09:54:34 -040096 " plugin-name "_main_t * " main-p " = &" plugin-name "_main;
Dave Barachb852bfa2016-01-04 15:27:42 -050097 u32 sw_if_index = ~0;
98 int enable_disable = 1;
Dave Barach557d1282016-11-10 14:22:49 -050099
Dave Barachb852bfa2016-01-04 15:27:42 -0500100 int rv;
101
Dave Barach35465dc2019-05-18 14:52:54 -0400102 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
Dave Barachfed79e82017-02-10 11:57:46 -0500103 {
104 if (unformat (input, \"disable\"))
105 enable_disable = 0;
106 else if (unformat (input, \"%U\", unformat_vnet_sw_interface,
Dave Barach82842442018-10-31 09:54:34 -0400107 " main-p "->vnet_main, &sw_if_index))
Dave Barachfed79e82017-02-10 11:57:46 -0500108 ;
109 else
110 break;
Dave Barachb852bfa2016-01-04 15:27:42 -0500111 }
112
113 if (sw_if_index == ~0)
114 return clib_error_return (0, \"Please specify an interface...\");
Dave Barach557d1282016-11-10 14:22:49 -0500115
Dave Barach82842442018-10-31 09:54:34 -0400116 rv = " plugin-name "_enable_disable (" main-p ", sw_if_index, enable_disable);
Dave Barachb852bfa2016-01-04 15:27:42 -0500117
Dave Barach35465dc2019-05-18 14:52:54 -0400118 switch(rv)
Dave Barachfed79e82017-02-10 11:57:46 -0500119 {
Dave Barachb852bfa2016-01-04 15:27:42 -0500120 case 0:
121 break;
122
123 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
Dave Barach557d1282016-11-10 14:22:49 -0500124 return clib_error_return
Dave Barachb852bfa2016-01-04 15:27:42 -0500125 (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);
Dave Barachfed79e82017-02-10 11:57:46 -0500135 }
Dave Barachb852bfa2016-01-04 15:27:42 -0500136 return 0;
137}
138
Dave Barachfed79e82017-02-10 11:57:46 -0500139/* *INDENT-OFF* */
Dave Barach35465dc2019-05-18 14:52:54 -0400140VLIB_CLI_COMMAND (" plugin-name "_enable_disable_command, static) =
Dave Barachfed79e82017-02-10 11:57:46 -0500141{
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,
Dave Barachb852bfa2016-01-04 15:27:42 -0500146};
Dave Barachfed79e82017-02-10 11:57:46 -0500147/* *INDENT-ON* */
Dave Barachb852bfa2016-01-04 15:27:42 -0500148
149/* API message handler */
150static 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;
Dave Barach82842442018-10-31 09:54:34 -0400154 " plugin-name "_main_t * " main-p " = &" plugin-name "_main;
Dave Barachb852bfa2016-01-04 15:27:42 -0500155 int rv;
156
Dave Barach82842442018-10-31 09:54:34 -0400157 rv = " plugin-name "_enable_disable (" main-p ", ntohl(mp->sw_if_index),
Dave Barachb852bfa2016-01-04 15:27:42 -0500158 (int) (mp->enable_disable));
Dave Barach557d1282016-11-10 14:22:49 -0500159
Dave Barachb852bfa2016-01-04 15:27:42 -0500160 REPLY_MACRO(VL_API_" PLUGIN-NAME "_ENABLE_DISABLE_REPLY);
161}
162
Dave Barach4ec6f6b2019-10-01 12:10:57 -0400163/* API definitions */
164#include <" plugin-name "/" plugin-name ".api.c>
Dave Barach557d1282016-11-10 14:22:49 -0500165
Dave Barachb852bfa2016-01-04 15:27:42 -0500166static clib_error_t * " plugin-name "_init (vlib_main_t * vm)
167{
Dave Barach82842442018-10-31 09:54:34 -0400168 " plugin-name "_main_t * " main-p " = &" plugin-name "_main;
Dave Barachb852bfa2016-01-04 15:27:42 -0500169 clib_error_t * error = 0;
Dave Barachb852bfa2016-01-04 15:27:42 -0500170
Dave Barach82842442018-10-31 09:54:34 -0400171 " main-p "->vlib_main = vm;
172 " main-p "->vnet_main = vnet_get_main();
Dave Barach10440432018-04-03 14:58:14 -0400173
Dave Barach605c6362017-01-02 12:22:48 -0500174 /* Add our API messages to the global name_crc hash table */
Dave Barach4ec6f6b2019-10-01 12:10:57 -0400175 " main-p "->msg_id_base = setup_message_id_table ();
Dave Barachb852bfa2016-01-04 15:27:42 -0500176
177 return error;
178}
179
180VLIB_INIT_FUNCTION (" plugin-name "_init);
Dave Barachb7e2f3d2016-11-08 16:47:34 -0500181
Dave Barachfed79e82017-02-10 11:57:46 -0500182/* *INDENT-OFF* */
Dave Barach557d1282016-11-10 14:22:49 -0500183VNET_FEATURE_INIT (" plugin-name ", static) =
Dave Barachb7e2f3d2016-11-08 16:47:34 -0500184{
185 .arc_name = \"device-input\",
186 .node_name = \"" plugin-name "\",
187 .runs_before = VNET_FEATURES (\"ethernet-input\"),
188};
Dave Barachfed79e82017-02-10 11:57:46 -0500189/* *INDENT-ON */
190
191/* *INDENT-OFF* */
Dave Barach35465dc2019-05-18 14:52:54 -0400192VLIB_PLUGIN_REGISTER () =
Dave Barachfed79e82017-02-10 11:57:46 -0500193{
194 .version = VPP_BUILD_VER,
Dave Barach10440432018-04-03 14:58:14 -0400195 .description = \"" plugin-name " plugin description goes here\",
Dave Barachfed79e82017-02-10 11:57:46 -0500196};
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 */
Dave Barachb852bfa2016-01-04 15:27:42 -0500206")