api: split api generated files
[vpp.git] / src / examples / sample-plugin / sample / sample.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /**
16  * @file
17  * @brief Sample Plugin, plugin API / trace / CLI handling.
18  */
19
20 #include <vnet/vnet.h>
21 #include <vnet/plugin/plugin.h>
22 #include <sample/sample.h>
23
24 #include <vlibapi/api.h>
25 #include <vlibmemory/api.h>
26
27 #include <sample/sample.api_enum.h>
28 #include <sample/sample.api_types.h>
29
30 #define REPLY_MSG_ID_BASE sm->msg_id_base
31 #include <vlibapi/api_helper_macros.h>
32
33 /* *INDENT-OFF* */
34 VLIB_PLUGIN_REGISTER () = {
35     .version = SAMPLE_PLUGIN_BUILD_VER,
36     .description = "Sample of VPP Plugin",
37 };
38 /* *INDENT-ON* */
39
40 sample_main_t sample_main;
41
42 /**
43  * @brief Enable/disable the macswap plugin. 
44  *
45  * Action function shared between message handler and debug CLI.
46  */
47
48 int sample_macswap_enable_disable (sample_main_t * sm, u32 sw_if_index,
49                                    int enable_disable)
50 {
51   vnet_sw_interface_t * sw;
52   int rv = 0;
53
54   /* Utterly wrong? */
55   if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces, 
56                           sw_if_index))
57     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
58
59   /* Not a physical port? */
60   sw = vnet_get_sw_interface (sm->vnet_main, sw_if_index);
61   if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
62     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
63   
64   vnet_feature_enable_disable ("device-input", "sample",
65                                sw_if_index, enable_disable, 0, 0);
66
67   return rv;
68 }
69
70 static clib_error_t *
71 macswap_enable_disable_command_fn (vlib_main_t * vm,
72                                    unformat_input_t * input,
73                                    vlib_cli_command_t * cmd)
74 {
75   sample_main_t * sm = &sample_main;
76   u32 sw_if_index = ~0;
77   int enable_disable = 1;
78     
79   int rv;
80
81   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
82     if (unformat (input, "disable"))
83       enable_disable = 0;
84     else if (unformat (input, "%U", unformat_vnet_sw_interface,
85                        sm->vnet_main, &sw_if_index))
86       ;
87     else
88       break;
89   }
90
91   if (sw_if_index == ~0)
92     return clib_error_return (0, "Please specify an interface...");
93     
94   rv = sample_macswap_enable_disable (sm, sw_if_index, enable_disable);
95
96   switch(rv) {
97   case 0:
98     break;
99
100   case VNET_API_ERROR_INVALID_SW_IF_INDEX:
101     return clib_error_return 
102       (0, "Invalid interface, only works on physical ports");
103     break;
104
105   case VNET_API_ERROR_UNIMPLEMENTED:
106     return clib_error_return (0, "Device driver doesn't support redirection");
107     break;
108
109   default:
110     return clib_error_return (0, "sample_macswap_enable_disable returned %d",
111                               rv);
112   }
113   return 0;
114 }
115
116 /**
117  * @brief CLI command to enable/disable the sample macswap plugin.
118  */
119 VLIB_CLI_COMMAND (sr_content_command, static) = {
120     .path = "sample macswap",
121     .short_help = 
122     "sample macswap <interface-name> [disable]",
123     .function = macswap_enable_disable_command_fn,
124 };
125
126 /**
127  * @brief Plugin API message handler.
128  */
129 static void vl_api_sample_macswap_enable_disable_t_handler
130 (vl_api_sample_macswap_enable_disable_t * mp)
131 {
132   vl_api_sample_macswap_enable_disable_reply_t * rmp;
133   sample_main_t * sm = &sample_main;
134   int rv;
135
136   rv = sample_macswap_enable_disable (sm, ntohl(mp->sw_if_index), 
137                                       (int) (mp->enable_disable));
138   
139   REPLY_MACRO(VL_API_SAMPLE_MACSWAP_ENABLE_DISABLE_REPLY);
140 }
141
142 /* API definitions */
143 #include <sample/sample.api.c>
144
145 /**
146  * @brief Initialize the sample plugin.
147  */
148 static clib_error_t * sample_init (vlib_main_t * vm)
149 {
150   sample_main_t * sm = &sample_main;
151
152   sm->vnet_main =  vnet_get_main ();
153
154   /* Add our API messages to the global name_crc hash table */
155   sm->msg_id_base = setup_message_id_table ();
156
157   return 0;
158 }
159
160 VLIB_INIT_FUNCTION (sample_init);
161
162 /**
163  * @brief Hook the sample plugin into the VPP graph hierarchy.
164  */
165 VNET_FEATURE_INIT (sample, static) = 
166 {
167   .arc_name = "device-input",
168   .node_name = "sample",
169   .runs_before = VNET_FEATURES ("ethernet-input"),
170 };