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