VPP-470: Introduce VxLAN-GPE as transport for iOAM.
[vpp.git] / plugins / ioam-plugin / 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 /*
57  * A handy macro to set up a message reply.
58  * Assumes that the following variables are available:
59  * mp - pointer to request message
60  * rmp - pointer to reply message type
61  * rv - return value
62  */
63
64 #define REPLY_MACRO(t)                                          \
65 do {                                                            \
66     unix_shared_memory_queue_t * q =                            \
67     vl_api_client_index_to_input_queue (mp->client_index);      \
68     if (!q)                                                     \
69         return;                                                 \
70                                                                 \
71     rmp = vl_msg_api_alloc (sizeof (*rmp));                     \
72     rmp->_vl_msg_id = ntohs((t)+sm->msg_id_base);               \
73     rmp->context = mp->context;                                 \
74     rmp->retval = ntohl(rv);                                    \
75                                                                 \
76     vl_msg_api_send_shmem (q, (u8 *)&rmp);                      \
77 } while(0);
78
79
80 /* List of message types that this plugin understands */
81
82
83 #define foreach_vxlan_gpe_ioam_export_plugin_api_msg                        \
84 _(VXLAN_GPE_IOAM_EXPORT_ENABLE_DISABLE, vxlan_gpe_ioam_export_enable_disable)
85
86 /*
87  * This routine exists to convince the vlib plugin framework that
88  * we haven't accidentally copied a random .dll into the plugin directory.
89  *
90  * Also collects global variable pointers passed from the vpp engine
91  */
92
93 clib_error_t *
94 vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
95                       int from_early_init)
96 {
97   ioam_export_main_t *em = &vxlan_gpe_ioam_export_main;
98   clib_error_t *error = 0;
99
100   em->vlib_main = vm;
101   em->vnet_main = h->vnet_main;
102   em->ethernet_main = h->ethernet_main;
103
104   return error;
105 }
106
107 extern void vxlan_gpe_set_next_override (uword next);
108 /* Action function shared between message handler and debug CLI */
109 int
110 vxlan_gpe_ioam_export_enable_disable (ioam_export_main_t * em,
111                                       u8 is_disable,
112                                       ip4_address_t * collector_address,
113                                       ip4_address_t * src_address)
114 {
115   vlib_main_t *vm = em->vlib_main;
116
117   if (is_disable == 0)
118     {
119       if (1 == ioam_export_header_create (em, collector_address, src_address))
120         {
121           ioam_export_thread_buffer_init (em, vm);
122           vxlan_gpe_set_next_override (em->my_hbh_slot);
123           /* Turn on the export buffer check process */
124           vlib_process_signal_event (vm, em->export_process_node_index, 1, 0);
125
126         }
127       else
128         {
129           return (-2);
130         }
131     }
132   else
133     {
134       vxlan_gpe_set_next_override (VXLAN_GPE_DECAP_IOAM_V4_NEXT_POP);
135       ioam_export_header_cleanup (em, collector_address, src_address);
136       ioam_export_thread_buffer_free (em);
137       /* Turn off the export buffer check process */
138       vlib_process_signal_event (vm, em->export_process_node_index, 2, 0);
139
140     }
141
142   return 0;
143 }
144
145 /* API message handler */
146 static void vl_api_vxlan_gpe_ioam_export_enable_disable_t_handler
147   (vl_api_vxlan_gpe_ioam_export_enable_disable_t * mp)
148 {
149   vl_api_vxlan_gpe_ioam_export_enable_disable_reply_t *rmp;
150   ioam_export_main_t *sm = &vxlan_gpe_ioam_export_main;
151   int rv;
152
153   rv = vxlan_gpe_ioam_export_enable_disable (sm, (int) (mp->is_disable),
154                                              (ip4_address_t *)
155                                              mp->collector_address,
156                                              (ip4_address_t *)
157                                              mp->src_address);
158
159   REPLY_MACRO (VL_API_VXLAN_GPE_IOAM_EXPORT_ENABLE_DISABLE_REPLY);
160 }                               /* API message handler */
161
162
163
164 /* Set up the API message handling tables */
165 static clib_error_t *
166 vxlan_gpe_ioam_export_plugin_api_hookup (vlib_main_t * vm)
167 {
168   ioam_export_main_t *sm = &vxlan_gpe_ioam_export_main;
169 #define _(N,n)                                                  \
170     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
171                            #n,                                  \
172                            vl_api_##n##_t_handler,              \
173                            vl_noop_handler,                     \
174                            vl_api_##n##_t_endian,               \
175                            vl_api_##n##_t_print,                \
176                            sizeof(vl_api_##n##_t), 1);
177   foreach_vxlan_gpe_ioam_export_plugin_api_msg;
178 #undef _
179
180   return 0;
181 }
182
183
184 static clib_error_t *
185 set_vxlan_gpe_ioam_export_ipfix_command_fn (vlib_main_t * vm,
186                                             unformat_input_t * input,
187                                             vlib_cli_command_t * cmd)
188 {
189   ioam_export_main_t *em = &vxlan_gpe_ioam_export_main;
190   ip4_address_t collector, src;
191   u8 is_disable = 0;
192
193   collector.as_u32 = 0;
194   src.as_u32 = 0;
195
196   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
197     {
198       if (unformat (input, "collector %U", unformat_ip4_address, &collector))
199         ;
200       else if (unformat (input, "src %U", unformat_ip4_address, &src))
201         ;
202       else if (unformat (input, "disable"))
203         is_disable = 1;
204       else
205         break;
206     }
207
208   if (collector.as_u32 == 0)
209     return clib_error_return (0, "collector address required");
210
211   if (src.as_u32 == 0)
212     return clib_error_return (0, "src address required");
213
214   em->ipfix_collector.as_u32 = collector.as_u32;
215   em->src_address.as_u32 = src.as_u32;
216
217   vlib_cli_output (vm, "Collector %U, src address %U",
218                    format_ip4_address, &em->ipfix_collector,
219                    format_ip4_address, &em->src_address);
220
221   /* Turn on the export timer process */
222   // vlib_process_signal_event (vm, flow_report_process_node.index,
223   //1, 0);
224   vxlan_gpe_ioam_export_enable_disable (em, is_disable, &collector, &src);
225
226   return 0;
227 }
228
229 /* *INDENT-OFF* */
230 VLIB_CLI_COMMAND (set_vxlan_gpe_ioam_ipfix_command, static) =
231 {
232 .path = "set vxlan-gpe-ioam export ipfix",
233 .short_help = "set vxlan-gpe-ioam export ipfix collector <ip4-address> src <ip4-address>",
234 .function = set_vxlan_gpe_ioam_export_ipfix_command_fn,
235 };
236 /* *INDENT-ON* */
237
238
239 static clib_error_t *
240 vxlan_gpe_ioam_export_init (vlib_main_t * vm)
241 {
242   ioam_export_main_t *em = &vxlan_gpe_ioam_export_main;
243   clib_error_t *error = 0;
244   u8 *name;
245   u32 node_index = export_node.index;
246   vlib_node_t *vxlan_gpe_decap_ioam_node = NULL;
247
248   name = format (0, "vxlan_gpe_ioam_export_%08x%c", api_version, 0);
249
250   /* Ask for a correctly-sized block of API message decode slots */
251   em->msg_id_base = vl_msg_api_get_msg_ids
252     ((char *) name, VL_MSG_FIRST_AVAILABLE);
253   em->unix_time_0 = (u32) time (0);     /* Store starting time */
254   em->vlib_time_0 = vlib_time_now (vm);
255
256   error = vxlan_gpe_ioam_export_plugin_api_hookup (vm);
257
258   /* Hook this export node to vxlan-gpe-decap-ioam-v4 */
259   vxlan_gpe_decap_ioam_node =
260     vlib_get_node_by_name (vm, (u8 *) "vxlan-gpe-decap-ioam-v4");
261   em->my_hbh_slot =
262     vlib_node_add_next (vm, vxlan_gpe_decap_ioam_node->index, node_index);
263   vec_free (name);
264
265   return error;
266 }
267
268 VLIB_INIT_FUNCTION (vxlan_gpe_ioam_export_init);
269
270
271 /*
272  * fd.io coding-style-patch-verification: ON
273  *
274  * Local Variables:
275  * eval: (c-set-style "gnu")
276  * End:
277  */