Remove local REPLY_MACRO so that socket transport works.
[vpp.git] / src / plugins / cdp / cdp.c
1 /*
2  * cdp.c - cdp protocol plug-in
3  *
4  * Copyright (c) 2011-2018 by Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <cdp/cdp.h>
21
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vpp/app/version.h>
25
26 /* define message IDs */
27 #include <cdp/cdp_msg_enum.h>
28
29 /* define message structures */
30 #define vl_typedefs
31 #include <cdp/cdp_all_api_h.h>
32 #undef vl_typedefs
33
34 /* define generated endian-swappers */
35 #define vl_endianfun
36 #include <cdp/cdp_all_api_h.h>
37 #undef vl_endianfun
38
39 /* instantiate all the print functions we know about */
40 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
41 #define vl_printfun
42 #include <cdp/cdp_all_api_h.h>
43 #undef vl_printfun
44
45 /* Get the API version number */
46 #define vl_api_version(n,v) static u32 api_version=(v);
47 #include <cdp/cdp_all_api_h.h>
48 #undef vl_api_version
49
50 #define REPLY_MSG_ID_BASE cm->msg_id_base
51 #include <vlibapi/api_helper_macros.h>
52
53 /* List of message types that this plugin understands */
54
55 #define foreach_cdp_plugin_api_msg                           \
56 _(CDP_ENABLE_DISABLE, cdp_enable_disable)
57
58 /* Action function shared between message handler and debug CLI */
59
60 int
61 cdp_enable_disable (cdp_main_t * cm, int enable_disable)
62 {
63   int rv = 0;
64
65   if (enable_disable)
66     vlib_process_signal_event (cm->vlib_main, cdp_process_node.index,
67                                CDP_EVENT_ENABLE, 0);
68   else
69     vlib_process_signal_event (cm->vlib_main, cdp_process_node.index,
70                                CDP_EVENT_DISABLE, 0);
71   cm->enabled = enable_disable;
72
73   return rv;
74 }
75
76 static clib_error_t *
77 cdp_command_fn (vlib_main_t * vm,
78                 unformat_input_t * input, vlib_cli_command_t * cmd)
79 {
80   cdp_main_t *cm = &cdp_main;
81   int enable_disable = 1;
82
83   int rv;
84
85   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
86     {
87       if (unformat (input, "disable"))
88         enable_disable = 0;
89       else if (unformat (input, "enable"))
90         enable_disable = 1;
91       else
92         break;
93     }
94
95   rv = cdp_enable_disable (cm, enable_disable);
96
97   switch (rv)
98     {
99     case 0:
100       break;
101
102     default:
103       return clib_error_return (0, "cdp_enable_disable returned %d", rv);
104     }
105   return 0;
106 }
107
108 /* *INDENT-OFF* */
109 VLIB_CLI_COMMAND (cdp_command, static) =
110 {
111   .path = "cdp",
112   .short_help = "cdp enable | disable",
113   .function = cdp_command_fn,
114 };
115 /* *INDENT-ON* */
116
117 /* API message handler */
118 static void vl_api_cdp_enable_disable_t_handler
119   (vl_api_cdp_enable_disable_t * mp)
120 {
121   vl_api_cdp_enable_disable_reply_t *rmp;
122   cdp_main_t *cm = &cdp_main;
123   int rv;
124
125   rv = cdp_enable_disable (cm, (int) (mp->enable_disable));
126
127   REPLY_MACRO (VL_API_CDP_ENABLE_DISABLE_REPLY);
128 }
129
130 /* Set up the API message handling tables */
131 static clib_error_t *
132 cdp_plugin_api_hookup (vlib_main_t * vm)
133 {
134   cdp_main_t *cm = &cdp_main;
135 #define _(N,n)                                                  \
136     vl_msg_api_set_handlers((VL_API_##N + cm->msg_id_base),     \
137                            #n,                                  \
138                            vl_api_##n##_t_handler,              \
139                            vl_noop_handler,                     \
140                            vl_api_##n##_t_endian,               \
141                            vl_api_##n##_t_print,                \
142                            sizeof(vl_api_##n##_t), 1);
143   foreach_cdp_plugin_api_msg;
144 #undef _
145
146   return 0;
147 }
148
149 #define vl_msg_name_crc_list
150 #include <cdp/cdp_all_api_h.h>
151 #undef vl_msg_name_crc_list
152
153 static void
154 setup_message_id_table (cdp_main_t * cm, api_main_t * am)
155 {
156 #define _(id,n,crc)   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + cm->msg_id_base);
157   foreach_vl_msg_name_crc_cdp;
158 #undef _
159 }
160
161 static clib_error_t *
162 cdp_init (vlib_main_t * vm)
163 {
164   cdp_main_t *cm = &cdp_main;
165   clib_error_t *error = 0;
166   u8 *name;
167
168   cm->vlib_main = vm;
169
170   name = format (0, "cdp_%08x%c", api_version, 0);
171
172   /* Ask for a correctly-sized block of API message decode slots */
173   cm->msg_id_base = vl_msg_api_get_msg_ids
174     ((char *) name, VL_MSG_FIRST_AVAILABLE);
175
176   error = cdp_plugin_api_hookup (vm);
177
178   /* Add our API messages to the global name_crc hash table */
179   setup_message_id_table (cm, &api_main);
180
181   vec_free (name);
182
183   return error;
184 }
185
186 VLIB_INIT_FUNCTION (cdp_init);
187
188 /* *INDENT-OFF* */
189 VLIB_PLUGIN_REGISTER () =
190 {
191   .version = VPP_BUILD_VER,
192 };
193 /* *INDENT-ON* */
194
195 /*
196  * fd.io coding-style-patch-verification: ON
197  *
198  * Local Variables:
199  * eval: (c-set-style "gnu")
200  * End:
201  */