9efcab054875443ce86747ce7c0548c152e6e1dc
[vpp.git] / plugins / ioam-plugin / ioam / export / 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  * 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/ioam_export.h>
24
25 #include <vlibapi/api.h>
26 #include <vlibmemory/api.h>
27 #include <vlibsocket/api.h>
28 #include <vnet/ip/ip6_hop_by_hop.h>
29
30 #include "ioam_export.h"
31
32 /* define message IDs */
33 #include <ioam/export/ioam_export_msg_enum.h>
34
35 /* define message structures */
36 #define vl_typedefs
37 #include <ioam/export/ioam_export_all_api_h.h>
38 #undef vl_typedefs
39
40 /* define generated endian-swappers */
41 #define vl_endianfun
42 #include <ioam/export/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/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/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 #define foreach_ioam_export_plugin_api_msg                        \
83 _(IOAM_EXPORT_IP6_ENABLE_DISABLE, ioam_export_ip6_enable_disable)
84
85 /*
86  * This routine exists to convince the vlib plugin framework that
87  * we haven't accidentally copied a random .dll into the plugin directory.
88  *
89  * Also collects global variable pointers passed from the vpp engine
90  */
91
92 clib_error_t *
93 vlib_plugin_register (vlib_main_t * vm, vnet_plugin_handoff_t * h,
94                       int from_early_init)
95 {
96   ioam_export_main_t *em = &ioam_export_main;
97   clib_error_t *error = 0;
98
99   em->vlib_main = vm;
100   em->vnet_main = h->vnet_main;
101   em->ethernet_main = h->ethernet_main;
102
103   return error;
104 }
105
106 /* Action function shared between message handler and debug CLI */
107
108 int
109 ioam_export_ip6_enable_disable (ioam_export_main_t * em,
110                                 u8 is_disable,
111                                 ip4_address_t * collector_address,
112                                 ip4_address_t * src_address)
113 {
114   vlib_main_t *vm = em->vlib_main;
115
116   if (is_disable == 0)
117     {
118       if (1 == ioam_export_header_create (collector_address, src_address))
119         {
120           ioam_export_thread_buffer_init (vm);
121           ip6_hbh_set_next_override (em->my_hbh_slot);
122           /* Turn on the export buffer check process */
123           vlib_process_signal_event (vm, em->export_process_node_index, 1, 0);
124
125         }
126       else
127         {
128           return (-2);
129         }
130     }
131   else
132     {
133       ip6_hbh_set_next_override (IP6_LOOKUP_NEXT_POP_HOP_BY_HOP);
134       ioam_export_header_cleanup (collector_address, src_address);
135       ioam_export_thread_buffer_free ();
136       /* Turn off the export buffer check process */
137       vlib_process_signal_event (vm, em->export_process_node_index, 2, 0);
138
139     }
140
141   return 0;
142 }
143
144 /* API message handler */
145 static void vl_api_ioam_export_ip6_enable_disable_t_handler
146   (vl_api_ioam_export_ip6_enable_disable_t * mp)
147 {
148   vl_api_ioam_export_ip6_enable_disable_reply_t *rmp;
149   ioam_export_main_t *sm = &ioam_export_main;
150   int rv;
151
152   rv = ioam_export_ip6_enable_disable (sm, (int) (mp->is_disable),
153                                        (ip4_address_t *) mp->
154                                        collector_address,
155                                        (ip4_address_t *) mp->src_address);
156
157   REPLY_MACRO (VL_API_IOAM_EXPORT_IP6_ENABLE_DISABLE_REPLY);
158 }
159
160 /* Set up the API message handling tables */
161 static clib_error_t *
162 ioam_export_plugin_api_hookup (vlib_main_t * vm)
163 {
164   ioam_export_main_t *sm = &ioam_export_main;
165 #define _(N,n)                                                  \
166     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
167                            #n,                                  \
168                            vl_api_##n##_t_handler,              \
169                            vl_noop_handler,                     \
170                            vl_api_##n##_t_endian,               \
171                            vl_api_##n##_t_print,                \
172                            sizeof(vl_api_##n##_t), 1);
173   foreach_ioam_export_plugin_api_msg;
174 #undef _
175
176   return 0;
177 }
178
179 static clib_error_t *
180 set_ioam_export_ipfix_command_fn (vlib_main_t * vm,
181                                   unformat_input_t * input,
182                                   vlib_cli_command_t * cmd)
183 {
184   ioam_export_main_t *em = &ioam_export_main;
185   ip4_address_t collector, src;
186   u8 is_disable = 0;
187
188   collector.as_u32 = 0;
189   src.as_u32 = 0;
190
191   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
192     {
193       if (unformat (input, "collector %U", unformat_ip4_address, &collector))
194         ;
195       else if (unformat (input, "src %U", unformat_ip4_address, &src))
196         ;
197       else if (unformat (input, "disable"))
198         is_disable = 1;
199       else
200         break;
201     }
202
203   if (collector.as_u32 == 0)
204     return clib_error_return (0, "collector address required");
205
206   if (src.as_u32 == 0)
207     return clib_error_return (0, "src address required");
208
209   em->ipfix_collector.as_u32 = collector.as_u32;
210   em->src_address.as_u32 = src.as_u32;
211
212   vlib_cli_output (vm, "Collector %U, src address %U",
213                    format_ip4_address, &em->ipfix_collector,
214                    format_ip4_address, &em->src_address);
215
216   /* Turn on the export timer process */
217   // vlib_process_signal_event (vm, flow_report_process_node.index,
218   //1, 0);
219   ioam_export_ip6_enable_disable (em, is_disable, &collector, &src);
220
221   return 0;
222 }
223
224 VLIB_CLI_COMMAND (set_ipfix_command, static) =
225 {
226 .path = "set ioam export ipfix",.short_help =
227     "set ioam export ipfix collector <ip4-address> src <ip4-address>",.
228     function = set_ioam_export_ipfix_command_fn,};
229
230
231 static clib_error_t *
232 ioam_export_init (vlib_main_t * vm)
233 {
234   ioam_export_main_t *em = &ioam_export_main;
235   clib_error_t *error = 0;
236   u8 *name;
237   u32 node_index = export_node.index;
238   vlib_node_t *ip6_hbyh_node = NULL;
239
240   name = format (0, "ioam_export_%08x%c", api_version, 0);
241
242   /* Ask for a correctly-sized block of API message decode slots */
243   em->msg_id_base = vl_msg_api_get_msg_ids
244     ((char *) name, VL_MSG_FIRST_AVAILABLE);
245   em->unix_time_0 = (u32) time (0);     /* Store starting time */
246   em->vlib_time_0 = vlib_time_now (vm);
247
248   error = ioam_export_plugin_api_hookup (vm);
249
250   /* Hook this export node to ip6-hop-by-hop */
251   ip6_hbyh_node = vlib_get_node_by_name (vm, (u8 *) "ip6-hop-by-hop");
252   em->my_hbh_slot = vlib_node_add_next (vm, ip6_hbyh_node->index, node_index);
253   vec_free (name);
254
255   return error;
256 }
257
258 VLIB_INIT_FUNCTION (ioam_export_init);