46ac3d4a582b12bea30a48523cb9258650036279
[vpp.git] / src / plugins / 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-common/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
31 /* define message IDs */
32 #include <ioam/export/ioam_export_msg_enum.h>
33
34 /* define message structures */
35 #define vl_typedefs
36 #include <ioam/export/ioam_export_all_api_h.h>
37 #undef vl_typedefs
38
39 /* define generated endian-swappers */
40 #define vl_endianfun
41 #include <ioam/export/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/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/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_ioam_export_plugin_api_msg                        \
60 _(IOAM_EXPORT_IP6_ENABLE_DISABLE, ioam_export_ip6_enable_disable)
61
62 ioam_export_main_t ioam_export_main;
63
64 extern vlib_node_registration_t export_node;
65
66 /* Action function shared between message handler and debug CLI */
67
68 int
69 ioam_export_ip6_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
76   if (is_disable == 0)
77     {
78       if (1 == ioam_export_header_create (em, collector_address, src_address))
79         {
80           ioam_export_thread_buffer_init (em, vm);
81           ip6_hbh_set_next_override (em->my_hbh_slot);
82           /* Turn on the export buffer check process */
83           vlib_process_signal_event (vm, em->export_process_node_index, 1, 0);
84
85         }
86       else
87         {
88           return (-2);
89         }
90     }
91   else
92     {
93       ip6_hbh_set_next_override (IP6_LOOKUP_NEXT_POP_HOP_BY_HOP);
94       ioam_export_header_cleanup (em, collector_address, src_address);
95       ioam_export_thread_buffer_free (em);
96       /* Turn off the export buffer check process */
97       vlib_process_signal_event (vm, em->export_process_node_index, 2, 0);
98
99     }
100
101   return 0;
102 }
103
104 /* API message handler */
105 static void vl_api_ioam_export_ip6_enable_disable_t_handler
106   (vl_api_ioam_export_ip6_enable_disable_t * mp)
107 {
108   vl_api_ioam_export_ip6_enable_disable_reply_t *rmp;
109   ioam_export_main_t *sm = &ioam_export_main;
110   int rv;
111
112   rv = ioam_export_ip6_enable_disable (sm, (int) (mp->is_disable),
113                                        (ip4_address_t *)
114                                        mp->collector_address,
115                                        (ip4_address_t *) mp->src_address);
116
117   REPLY_MACRO (VL_API_IOAM_EXPORT_IP6_ENABLE_DISABLE_REPLY);
118 }
119
120 /* Set up the API message handling tables */
121 static clib_error_t *
122 ioam_export_plugin_api_hookup (vlib_main_t * vm)
123 {
124   ioam_export_main_t *sm = &ioam_export_main;
125 #define _(N,n)                                                  \
126     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
127                            #n,                                  \
128                            vl_api_##n##_t_handler,              \
129                            vl_noop_handler,                     \
130                            vl_api_##n##_t_endian,               \
131                            vl_api_##n##_t_print,                \
132                            sizeof(vl_api_##n##_t), 1);
133   foreach_ioam_export_plugin_api_msg;
134 #undef _
135
136   return 0;
137 }
138
139 #define vl_msg_name_crc_list
140 #include <ioam/export/ioam_export_all_api_h.h>
141 #undef vl_msg_name_crc_list
142
143 static void
144 setup_message_id_table (ioam_export_main_t * sm, api_main_t * am)
145 {
146 #define _(id,n,crc) \
147   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + sm->msg_id_base);
148   foreach_vl_msg_name_crc_ioam_export;
149 #undef _
150 }
151
152 static clib_error_t *
153 set_ioam_export_ipfix_command_fn (vlib_main_t * vm,
154                                   unformat_input_t * input,
155                                   vlib_cli_command_t * cmd)
156 {
157   ioam_export_main_t *em = &ioam_export_main;
158   ip4_address_t collector, src;
159   u8 is_disable = 0;
160
161   collector.as_u32 = 0;
162   src.as_u32 = 0;
163
164   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
165     {
166       if (unformat (input, "collector %U", unformat_ip4_address, &collector))
167         ;
168       else if (unformat (input, "src %U", unformat_ip4_address, &src))
169         ;
170       else if (unformat (input, "disable"))
171         is_disable = 1;
172       else
173         break;
174     }
175
176   if (collector.as_u32 == 0)
177     return clib_error_return (0, "collector address required");
178
179   if (src.as_u32 == 0)
180     return clib_error_return (0, "src address required");
181
182   em->ipfix_collector.as_u32 = collector.as_u32;
183   em->src_address.as_u32 = src.as_u32;
184
185   vlib_cli_output (vm, "Collector %U, src address %U",
186                    format_ip4_address, &em->ipfix_collector,
187                    format_ip4_address, &em->src_address);
188
189   /* Turn on the export timer process */
190   // vlib_process_signal_event (vm, flow_report_process_node.index,
191   //1, 0);
192   ioam_export_ip6_enable_disable (em, is_disable, &collector, &src);
193
194   return 0;
195 }
196
197 /* *INDENT-OFF* */
198 VLIB_CLI_COMMAND (set_ipfix_command, static) =
199 {
200 .path = "set ioam export ipfix",.short_help =
201     "set ioam export ipfix collector <ip4-address> src <ip4-address>",.
202     function = set_ioam_export_ipfix_command_fn,};
203 /* *INDENT-ON* */
204
205
206 static clib_error_t *
207 ioam_export_init (vlib_main_t * vm)
208 {
209   ioam_export_main_t *em = &ioam_export_main;
210   clib_error_t *error = 0;
211   u8 *name;
212   u32 node_index = export_node.index;
213   vlib_node_t *ip6_hbyh_node = NULL;
214
215   em->vlib_main = vm;
216   em->vnet_main = vnet_get_main ();
217   em->set_id = IPFIX_IOAM_EXPORT_ID;
218   ioam_export_reset_next_node (em);
219
220   name = format (0, "ioam_export_%08x%c", api_version, 0);
221
222   /* Ask for a correctly-sized block of API message decode slots */
223   em->msg_id_base = vl_msg_api_get_msg_ids
224     ((char *) name, VL_MSG_FIRST_AVAILABLE);
225   em->unix_time_0 = (u32) time (0);     /* Store starting time */
226   em->vlib_time_0 = vlib_time_now (vm);
227
228   error = ioam_export_plugin_api_hookup (vm);
229
230   /* Add our API messages to the global name_crc hash table */
231   setup_message_id_table (em, &api_main);
232
233   /* Hook this export node to ip6-hop-by-hop */
234   ip6_hbyh_node = vlib_get_node_by_name (vm, (u8 *) "ip6-hop-by-hop");
235   em->my_hbh_slot = vlib_node_add_next (vm, ip6_hbyh_node->index, node_index);
236   vec_free (name);
237
238   return error;
239 }
240
241 VLIB_INIT_FUNCTION (ioam_export_init);
242
243 /*
244  * fd.io coding-style-patch-verification: ON
245  *
246  * Local Variables:
247  * eval: (c-set-style "gnu")
248  * End:
249  */