Repair vlib API socket server
[vpp.git] / src / plugins / ioam / export-vxlan-gpe / vxlan_gpe_ioam_export.c
1 /*
2  * Copyright (c) 2016 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  *------------------------------------------------------------------
17  * vxlan_gpe_ioam_export.c - ioam export API / debug CLI handling
18  *------------------------------------------------------------------
19  */
20
21 #include <vnet/vnet.h>
22 #include <vnet/plugin/plugin.h>
23 #include <ioam/export-common/ioam_export.h>
24 #include <vnet/vxlan-gpe/vxlan_gpe.h>
25
26 #include <vlibapi/api.h>
27 #include <vlibmemory/api.h>
28
29 #include <ioam/lib-vxlan-gpe/vxlan_gpe_ioam.h>
30
31 /* define message IDs */
32 #include <ioam/export-vxlan-gpe/vxlan_gpe_ioam_export_msg_enum.h>
33
34 /* define message structures */
35 #define vl_typedefs
36 #include <ioam/export-vxlan-gpe/vxlan_gpe_ioam_export_all_api_h.h>
37 #undef vl_typedefs
38
39 /* define generated endian-swappers */
40 #define vl_endianfun
41 #include <ioam/export-vxlan-gpe/vxlan_gpe_ioam_export_all_api_h.h>
42 #undef vl_endianfun
43
44 /* instantiate all the print functions we know about */
45 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
46 #define vl_printfun
47 #include <ioam/export-vxlan-gpe/vxlan_gpe_ioam_export_all_api_h.h>
48 #undef vl_printfun
49
50 /* Get the API version number */
51 #define vl_api_version(n,v) static u32 api_version=(v);
52 #include <ioam/export-vxlan-gpe/vxlan_gpe_ioam_export_all_api_h.h>
53 #undef vl_api_version
54
55 #define REPLY_MSG_ID_BASE sm->msg_id_base
56 #include <vlibapi/api_helper_macros.h>
57
58 /* List of message types that this plugin understands */
59 #define foreach_vxlan_gpe_ioam_export_plugin_api_msg                        \
60 _(VXLAN_GPE_IOAM_EXPORT_ENABLE_DISABLE, vxlan_gpe_ioam_export_enable_disable)
61
62 ioam_export_main_t vxlan_gpe_ioam_export_main;
63 extern vlib_node_registration_t vxlan_export_node;
64
65 extern void vxlan_gpe_set_next_override (uword next);
66 /* Action function shared between message handler and debug CLI */
67 int
68 vxlan_gpe_ioam_export_enable_disable (ioam_export_main_t * em,
69                                       u8 is_disable,
70                                       ip4_address_t * collector_address,
71                                       ip4_address_t * src_address)
72 {
73   vlib_main_t *vm = em->vlib_main;
74   u32 node_index = vxlan_export_node.index;
75   vlib_node_t *vxlan_gpe_decap_ioam_node = NULL;
76
77   if (is_disable == 0)
78     {
79       if (em->my_hbh_slot == ~0)
80         {
81           /* Hook this export node to vxlan-gpe-decap-ioam-v4 */
82           vxlan_gpe_decap_ioam_node =
83             vlib_get_node_by_name (vm, (u8 *) "vxlan-gpe-decap-ioam-v4");
84           if (!vxlan_gpe_decap_ioam_node)
85             {
86               /* node does not exist give up */
87               return (-1);
88             }
89           em->my_hbh_slot =
90             vlib_node_add_next (vm, vxlan_gpe_decap_ioam_node->index,
91                                 node_index);
92         }
93       if (1 == ioam_export_header_create (em, collector_address, src_address))
94         {
95           ioam_export_thread_buffer_init (em, vm);
96           vxlan_gpe_set_next_override (em->my_hbh_slot);
97           /* Turn on the export buffer check process */
98           vlib_process_signal_event (vm, em->export_process_node_index, 1, 0);
99
100         }
101       else
102         {
103           return (-2);
104         }
105     }
106   else
107     {
108       vxlan_gpe_set_next_override (VXLAN_GPE_DECAP_IOAM_V4_NEXT_POP);
109       ioam_export_header_cleanup (em, collector_address, src_address);
110       ioam_export_thread_buffer_free (em);
111       /* Turn off the export buffer check process */
112       vlib_process_signal_event (vm, em->export_process_node_index, 2, 0);
113
114     }
115
116   return 0;
117 }
118
119 /* API message handler */
120 static void vl_api_vxlan_gpe_ioam_export_enable_disable_t_handler
121   (vl_api_vxlan_gpe_ioam_export_enable_disable_t * mp)
122 {
123   vl_api_vxlan_gpe_ioam_export_enable_disable_reply_t *rmp;
124   ioam_export_main_t *sm = &vxlan_gpe_ioam_export_main;
125   int rv;
126
127   rv = vxlan_gpe_ioam_export_enable_disable (sm, (int) (mp->is_disable),
128                                              (ip4_address_t *)
129                                              mp->collector_address,
130                                              (ip4_address_t *)
131                                              mp->src_address);
132
133   REPLY_MACRO (VL_API_VXLAN_GPE_IOAM_EXPORT_ENABLE_DISABLE_REPLY);
134 }                               /* API message handler */
135
136
137
138 /* Set up the API message handling tables */
139 static clib_error_t *
140 vxlan_gpe_ioam_export_plugin_api_hookup (vlib_main_t * vm)
141 {
142   ioam_export_main_t *sm = &vxlan_gpe_ioam_export_main;
143 #define _(N,n)                                                  \
144     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
145                            #n,                                  \
146                            vl_api_##n##_t_handler,              \
147                            vl_noop_handler,                     \
148                            vl_api_##n##_t_endian,               \
149                            vl_api_##n##_t_print,                \
150                            sizeof(vl_api_##n##_t), 1);
151   foreach_vxlan_gpe_ioam_export_plugin_api_msg;
152 #undef _
153
154   return 0;
155 }
156
157 #define vl_msg_name_crc_list
158 #include <ioam/export-vxlan-gpe/vxlan_gpe_ioam_export_all_api_h.h>
159 #undef vl_msg_name_crc_list
160
161 static void
162 setup_message_id_table (ioam_export_main_t * sm, api_main_t * am)
163 {
164 #define _(id,n,crc) \
165   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
166   foreach_vl_msg_name_crc_vxlan_gpe_ioam_export;
167 #undef _
168 }
169
170
171 static clib_error_t *
172 set_vxlan_gpe_ioam_export_ipfix_command_fn (vlib_main_t * vm,
173                                             unformat_input_t * input,
174                                             vlib_cli_command_t * cmd)
175 {
176   ioam_export_main_t *em = &vxlan_gpe_ioam_export_main;
177   ip4_address_t collector, src;
178   u8 is_disable = 0;
179
180   collector.as_u32 = 0;
181   src.as_u32 = 0;
182
183   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
184     {
185       if (unformat (input, "collector %U", unformat_ip4_address, &collector))
186         ;
187       else if (unformat (input, "src %U", unformat_ip4_address, &src))
188         ;
189       else if (unformat (input, "disable"))
190         is_disable = 1;
191       else
192         break;
193     }
194
195   if (collector.as_u32 == 0)
196     return clib_error_return (0, "collector address required");
197
198   if (src.as_u32 == 0)
199     return clib_error_return (0, "src address required");
200
201   em->ipfix_collector.as_u32 = collector.as_u32;
202   em->src_address.as_u32 = src.as_u32;
203
204   vlib_cli_output (vm, "Collector %U, src address %U",
205                    format_ip4_address, &em->ipfix_collector,
206                    format_ip4_address, &em->src_address);
207
208   /* Turn on the export timer process */
209   // vlib_process_signal_event (vm, flow_report_process_node.index,
210   //1, 0);
211   if (0 !=
212       vxlan_gpe_ioam_export_enable_disable (em, is_disable, &collector, &src))
213     {
214       return clib_error_return (0, "Unable to set ioam vxlan-gpe export");
215     }
216
217   return 0;
218 }
219
220 /* *INDENT-OFF* */
221 VLIB_CLI_COMMAND (set_vxlan_gpe_ioam_ipfix_command, static) =
222 {
223 .path = "set vxlan-gpe-ioam export ipfix",
224 .short_help = "set vxlan-gpe-ioam export ipfix collector <ip4-address> src <ip4-address>",
225 .function = set_vxlan_gpe_ioam_export_ipfix_command_fn,
226 };
227 /* *INDENT-ON* */
228
229
230 static clib_error_t *
231 vxlan_gpe_ioam_export_init (vlib_main_t * vm)
232 {
233   ioam_export_main_t *em = &vxlan_gpe_ioam_export_main;
234   clib_error_t *error = 0;
235   u8 *name;
236
237   em->set_id = IPFIX_VXLAN_IOAM_EXPORT_ID;
238
239   name = format (0, "vxlan_gpe_ioam_export_%08x%c", api_version, 0);
240
241   /* Ask for a correctly-sized block of API message decode slots */
242   em->msg_id_base = vl_msg_api_get_msg_ids
243     ((char *) name, VL_MSG_FIRST_AVAILABLE);
244   em->unix_time_0 = (u32) time (0);     /* Store starting time */
245   em->vlib_time_0 = vlib_time_now (vm);
246
247   error = vxlan_gpe_ioam_export_plugin_api_hookup (vm);
248
249   /* Add our API messages to the global name_crc hash table */
250   setup_message_id_table (em, &api_main);
251
252   em->my_hbh_slot = ~0;
253   em->vlib_main = vm;
254   em->vnet_main = vnet_get_main ();
255   ioam_export_reset_next_node (em);
256   vec_free (name);
257
258   return error;
259 }
260
261 VLIB_INIT_FUNCTION (vxlan_gpe_ioam_export_init);
262
263
264 /*
265  * fd.io coding-style-patch-verification: ON
266  *
267  * Local Variables:
268  * eval: (c-set-style "gnu")
269  * End:
270  */