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