misc: remove GNU Indent directives
[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 VLIB_PLUGIN_REGISTER () = {
34     .version = SAMPLE_PLUGIN_BUILD_VER,
35     .description = "Sample of VPP Plugin",
36 };
37
38 sample_main_t sample_main;
39
40 /**
41  * @brief Enable/disable the macswap plugin. 
42  *
43  * Action function shared between message handler and debug CLI.
44  */
45
46 int sample_macswap_enable_disable (sample_main_t * sm, u32 sw_if_index,
47                                    int enable_disable)
48 {
49   vnet_sw_interface_t * sw;
50   int rv = 0;
51
52   /* Utterly wrong? */
53   if (pool_is_free_index (sm->vnet_main->interface_main.sw_interfaces, 
54                           sw_if_index))
55     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
56
57   /* Not a physical port? */
58   sw = vnet_get_sw_interface (sm->vnet_main, sw_if_index);
59   if (sw->type != VNET_SW_INTERFACE_TYPE_HARDWARE)
60     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
61   
62   vnet_feature_enable_disable ("device-input", "sample",
63                                sw_if_index, enable_disable, 0, 0);
64
65   return rv;
66 }
67
68 static clib_error_t *
69 macswap_enable_disable_command_fn (vlib_main_t * vm,
70                                    unformat_input_t * input,
71                                    vlib_cli_command_t * cmd)
72 {
73   sample_main_t * sm = &sample_main;
74   u32 sw_if_index = ~0;
75   int enable_disable = 1;
76     
77   int rv;
78
79   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
80     if (unformat (input, "disable"))
81       enable_disable = 0;
82     else if (unformat (input, "%U", unformat_vnet_sw_interface,
83                        sm->vnet_main, &sw_if_index))
84       ;
85     else
86       break;
87   }
88
89   if (sw_if_index == ~0)
90     return clib_error_return (0, "Please specify an interface...");
91     
92   rv = sample_macswap_enable_disable (sm, sw_if_index, enable_disable);
93
94   switch(rv) {
95   case 0:
96     break;
97
98   case VNET_API_ERROR_INVALID_SW_IF_INDEX:
99     return clib_error_return 
100       (0, "Invalid interface, only works on physical ports");
101     break;
102
103   case VNET_API_ERROR_UNIMPLEMENTED:
104     return clib_error_return (0, "Device driver doesn't support redirection");
105     break;
106
107   default:
108     return clib_error_return (0, "sample_macswap_enable_disable returned %d",
109                               rv);
110   }
111   return 0;
112 }
113
114 /**
115  * @brief CLI command to enable/disable the sample macswap plugin.
116  */
117 VLIB_CLI_COMMAND (sr_content_command, static) = {
118     .path = "sample macswap",
119     .short_help = 
120     "sample macswap <interface-name> [disable]",
121     .function = macswap_enable_disable_command_fn,
122 };
123
124 /**
125  * @brief Plugin API message handler.
126  */
127 static void vl_api_sample_macswap_enable_disable_t_handler
128 (vl_api_sample_macswap_enable_disable_t * mp)
129 {
130   vl_api_sample_macswap_enable_disable_reply_t * rmp;
131   sample_main_t * sm = &sample_main;
132   int rv;
133
134   rv = sample_macswap_enable_disable (sm, ntohl(mp->sw_if_index), 
135                                       (int) (mp->enable_disable));
136   
137   REPLY_MACRO(VL_API_SAMPLE_MACSWAP_ENABLE_DISABLE_REPLY);
138 }
139
140 /* API definitions */
141 #include <sample/sample.api.c>
142
143 /**
144  * @brief Initialize the sample plugin.
145  */
146 static clib_error_t * sample_init (vlib_main_t * vm)
147 {
148   sample_main_t * sm = &sample_main;
149
150   sm->vnet_main =  vnet_get_main ();
151
152   /* Add our API messages to the global name_crc hash table */
153   sm->msg_id_base = setup_message_id_table ();
154
155   return 0;
156 }
157
158 VLIB_INIT_FUNCTION (sample_init);
159
160 /**
161  * @brief Hook the sample plugin into the VPP graph hierarchy.
162  */
163 VNET_FEATURE_INIT (sample, static) = 
164 {
165   .arc_name = "device-input",
166   .node_name = "sample",
167   .runs_before = VNET_FEATURES ("ethernet-input"),
168 };