ipfix-export: set msg id correctly for dump msg
[vpp.git] / src / vnet / ipfix-export / flow_api.c
1 /*
2  *------------------------------------------------------------------
3  * flow_api.c - flow api
4  *
5  * Copyright (c) 2016 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19
20 #include <vnet/vnet.h>
21 #include <vlibmemory/api.h>
22 #include <vnet/ip/ip_types_api.h>
23 #include <vnet/udp/udp_local.h>
24
25 #include <vnet/interface.h>
26 #include <vnet/api_errno.h>
27
28 #include <vnet/fib/fib_table.h>
29 #include <vnet/ipfix-export/flow_report.h>
30 #include <vnet/ipfix-export/flow_report_classify.h>
31
32 #include <vnet/format_fns.h>
33 #include <vnet/ipfix-export/ipfix_export.api_enum.h>
34 #include <vnet/ipfix-export/ipfix_export.api_types.h>
35
36 #define REPLY_MSG_ID_BASE frm->msg_id_base
37 #include <vlibapi/api_helper_macros.h>
38
39 static void
40 vl_api_set_ipfix_exporter_t_handler (vl_api_set_ipfix_exporter_t * mp)
41 {
42   vlib_main_t *vm = vlib_get_main ();
43   flow_report_main_t *frm = &flow_report_main;
44   vl_api_registration_t *reg;
45   vl_api_set_ipfix_exporter_reply_t *rmp;
46   ip4_address_t collector, src;
47   u16 collector_port = UDP_DST_PORT_ipfix;
48   u32 path_mtu;
49   u32 template_interval;
50   u8 udp_checksum;
51   u32 fib_id;
52   u32 fib_index = ~0;
53   int rv = 0;
54
55   reg = vl_api_client_index_to_registration (mp->client_index);
56   if (!reg)
57     return;
58
59   if (mp->src_address.af == ADDRESS_IP6
60       || mp->collector_address.af == ADDRESS_IP6)
61     {
62       rv = VNET_API_ERROR_UNIMPLEMENTED;
63       goto out;
64     }
65
66   ip4_address_decode (mp->collector_address.un.ip4, &collector);
67   collector_port = ntohs (mp->collector_port);
68   if (collector_port == (u16) ~ 0)
69     collector_port = UDP_DST_PORT_ipfix;
70   ip4_address_decode (mp->src_address.un.ip4, &src);
71   fib_id = ntohl (mp->vrf_id);
72
73   ip4_main_t *im = &ip4_main;
74   if (fib_id == ~0)
75     {
76       fib_index = ~0;
77     }
78   else
79     {
80       uword *p = hash_get (im->fib_index_by_table_id, fib_id);
81       if (!p)
82         {
83           rv = VNET_API_ERROR_NO_SUCH_FIB;
84           goto out;
85         }
86       fib_index = p[0];
87     }
88
89   path_mtu = ntohl (mp->path_mtu);
90   if (path_mtu == ~0)
91     path_mtu = 512;             // RFC 7011 section 10.3.3.
92   template_interval = ntohl (mp->template_interval);
93   if (template_interval == ~0)
94     template_interval = 20;
95   udp_checksum = mp->udp_checksum;
96
97   if (collector.as_u32 != 0 && src.as_u32 == 0)
98     {
99       rv = VNET_API_ERROR_INVALID_VALUE;
100       goto out;
101     }
102
103   if (path_mtu > 1450 /* vpp does not support fragmentation */ )
104     {
105       rv = VNET_API_ERROR_INVALID_VALUE;
106       goto out;
107     }
108
109   if (path_mtu < 68)
110     {
111       rv = VNET_API_ERROR_INVALID_VALUE;
112       goto out;
113     }
114
115   /* Reset report streams if we are reconfiguring IP addresses */
116   if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
117       frm->src_address.as_u32 != src.as_u32 ||
118       frm->collector_port != collector_port)
119     vnet_flow_reports_reset (frm);
120
121   frm->ipfix_collector.as_u32 = collector.as_u32;
122   frm->collector_port = collector_port;
123   frm->src_address.as_u32 = src.as_u32;
124   frm->fib_index = fib_index;
125   frm->path_mtu = path_mtu;
126   frm->template_interval = template_interval;
127   frm->udp_checksum = udp_checksum;
128
129   /* Turn on the flow reporting process */
130   vlib_process_signal_event (vm, flow_report_process_node.index, 1, 0);
131
132 out:
133   REPLY_MACRO (VL_API_SET_IPFIX_EXPORTER_REPLY);
134 }
135
136 static void
137 vl_api_ipfix_exporter_dump_t_handler (vl_api_ipfix_exporter_dump_t * mp)
138 {
139   flow_report_main_t *frm = &flow_report_main;
140   vl_api_registration_t *reg;
141   vl_api_ipfix_exporter_details_t *rmp;
142   ip4_main_t *im = &ip4_main;
143   ip46_address_t collector = {.as_u64[0] = 0,.as_u64[1] = 0 };
144   ip46_address_t src = {.as_u64[0] = 0,.as_u64[1] = 0 };
145   u32 vrf_id;
146
147   reg = vl_api_client_index_to_registration (mp->client_index);
148   if (!reg)
149     return;
150
151   rmp = vl_msg_api_alloc (sizeof (*rmp));
152   clib_memset (rmp, 0, sizeof (*rmp));
153   rmp->_vl_msg_id =
154     ntohs ((REPLY_MSG_ID_BASE) + VL_API_IPFIX_EXPORTER_DETAILS);
155   rmp->context = mp->context;
156
157   memcpy (&collector.ip4, &frm->ipfix_collector, sizeof (ip4_address_t));
158   ip_address_encode (&collector, IP46_TYPE_IP4, &rmp->collector_address);
159
160   rmp->collector_port = htons (frm->collector_port);
161
162   memcpy (&src.ip4, &frm->src_address, sizeof (ip4_address_t));
163   ip_address_encode (&src, IP46_TYPE_IP4, &rmp->src_address);
164
165   if (frm->fib_index == ~0)
166     vrf_id = ~0;
167   else
168     vrf_id = im->fibs[frm->fib_index].ft_table_id;
169   rmp->vrf_id = htonl (vrf_id);
170   rmp->path_mtu = htonl (frm->path_mtu);
171   rmp->template_interval = htonl (frm->template_interval);
172   rmp->udp_checksum = (frm->udp_checksum != 0);
173
174   vl_api_send_msg (reg, (u8 *) rmp);
175 }
176
177 static void
178   vl_api_set_ipfix_classify_stream_t_handler
179   (vl_api_set_ipfix_classify_stream_t * mp)
180 {
181   vl_api_set_ipfix_classify_stream_reply_t *rmp;
182   flow_report_classify_main_t *fcm = &flow_report_classify_main;
183   flow_report_main_t *frm = &flow_report_main;
184   u32 domain_id = 0;
185   u32 src_port = UDP_DST_PORT_ipfix;
186   int rv = 0;
187
188   domain_id = ntohl (mp->domain_id);
189   src_port = ntohs (mp->src_port);
190
191   if (fcm->src_port != 0 &&
192       (fcm->domain_id != domain_id || fcm->src_port != (u16) src_port))
193     {
194       int rv = vnet_stream_change (frm, fcm->domain_id, fcm->src_port,
195                                    domain_id, (u16) src_port);
196       ASSERT (rv == 0);
197     }
198
199   fcm->domain_id = domain_id;
200   fcm->src_port = (u16) src_port;
201
202   REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
203 }
204
205 static void
206   vl_api_ipfix_classify_stream_dump_t_handler
207   (vl_api_ipfix_classify_stream_dump_t * mp)
208 {
209   flow_report_classify_main_t *fcm = &flow_report_classify_main;
210   vl_api_registration_t *reg;
211   vl_api_ipfix_classify_stream_details_t *rmp;
212
213   reg = vl_api_client_index_to_registration (mp->client_index);
214   if (!reg)
215     return;
216
217   rmp = vl_msg_api_alloc (sizeof (*rmp));
218   clib_memset (rmp, 0, sizeof (*rmp));
219   rmp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_STREAM_DETAILS);
220   rmp->context = mp->context;
221   rmp->domain_id = htonl (fcm->domain_id);
222   rmp->src_port = htons (fcm->src_port);
223
224   vl_api_send_msg (reg, (u8 *) rmp);
225 }
226
227 static void
228   vl_api_ipfix_classify_table_add_del_t_handler
229   (vl_api_ipfix_classify_table_add_del_t * mp)
230 {
231   vl_api_ipfix_classify_table_add_del_reply_t *rmp;
232   vl_api_registration_t *reg;
233   flow_report_classify_main_t *fcm = &flow_report_classify_main;
234   flow_report_main_t *frm = &flow_report_main;
235   vnet_flow_report_add_del_args_t args;
236   ipfix_classify_table_t *table;
237   int is_add;
238   u32 classify_table_index;
239   u8 ip_version;
240   u8 transport_protocol;
241   int rv = 0;
242
243   reg = vl_api_client_index_to_registration (mp->client_index);
244   if (!reg)
245     return;
246
247   classify_table_index = ntohl (mp->table_id);
248   ip_version = (mp->ip_version == ADDRESS_IP4) ? 4 : 6;
249   transport_protocol = mp->transport_protocol;
250   is_add = mp->is_add;
251
252   if (fcm->src_port == 0)
253     {
254       /* call set_ipfix_classify_stream first */
255       rv = VNET_API_ERROR_UNSPECIFIED;
256       goto out;
257     }
258
259   clib_memset (&args, 0, sizeof (args));
260
261   table = 0;
262   int i;
263   for (i = 0; i < vec_len (fcm->tables); i++)
264     if (ipfix_classify_table_index_valid (i))
265       if (fcm->tables[i].classify_table_index == classify_table_index)
266         {
267           table = &fcm->tables[i];
268           break;
269         }
270
271   if (is_add)
272     {
273       if (table)
274         {
275           rv = VNET_API_ERROR_VALUE_EXIST;
276           goto out;
277         }
278       table = ipfix_classify_add_table ();
279       table->classify_table_index = classify_table_index;
280     }
281   else
282     {
283       if (!table)
284         {
285           rv = VNET_API_ERROR_NO_SUCH_ENTRY;
286           goto out;
287         }
288     }
289
290   table->ip_version = ip_version;
291   table->transport_protocol = transport_protocol;
292
293   args.opaque.as_uword = table - fcm->tables;
294   args.rewrite_callback = ipfix_classify_template_rewrite;
295   args.flow_data_callback = ipfix_classify_send_flows;
296   args.is_add = is_add;
297   args.domain_id = fcm->domain_id;
298   args.src_port = fcm->src_port;
299
300   rv = vnet_flow_report_add_del (frm, &args, NULL);
301
302   /* If deleting, or add failed */
303   if (is_add == 0 || (rv && is_add))
304     ipfix_classify_delete_table (table - fcm->tables);
305
306 out:
307   REPLY_MACRO (VL_API_SET_IPFIX_CLASSIFY_STREAM_REPLY);
308 }
309
310 static void
311 send_ipfix_classify_table_details (u32 table_index,
312                                    vl_api_registration_t * reg, u32 context)
313 {
314   flow_report_classify_main_t *fcm = &flow_report_classify_main;
315   vl_api_ipfix_classify_table_details_t *mp;
316
317   ipfix_classify_table_t *table = &fcm->tables[table_index];
318
319   mp = vl_msg_api_alloc (sizeof (*mp));
320   clib_memset (mp, 0, sizeof (*mp));
321   mp->_vl_msg_id = ntohs (VL_API_IPFIX_CLASSIFY_TABLE_DETAILS);
322   mp->context = context;
323   mp->table_id = htonl (table->classify_table_index);
324   mp->ip_version = (table->ip_version == 4) ? ADDRESS_IP4 : ADDRESS_IP6;
325   mp->transport_protocol = table->transport_protocol;
326
327   vl_api_send_msg (reg, (u8 *) mp);
328 }
329
330 static void
331   vl_api_ipfix_classify_table_dump_t_handler
332   (vl_api_ipfix_classify_table_dump_t * mp)
333 {
334   flow_report_classify_main_t *fcm = &flow_report_classify_main;
335   vl_api_registration_t *reg;
336   u32 i;
337
338   reg = vl_api_client_index_to_registration (mp->client_index);
339   if (!reg)
340     return;
341
342   for (i = 0; i < vec_len (fcm->tables); i++)
343     if (ipfix_classify_table_index_valid (i))
344       send_ipfix_classify_table_details (i, reg, mp->context);
345 }
346
347 static void
348 vl_api_ipfix_flush_t_handler (vl_api_ipfix_flush_t * mp)
349 {
350   flow_report_main_t *frm = &flow_report_main;
351   vl_api_ipfix_flush_reply_t *rmp;
352   vl_api_registration_t *reg;
353   vlib_main_t *vm = vlib_get_main ();
354   int rv = 0;
355
356   reg = vl_api_client_index_to_registration (mp->client_index);
357   if (!reg)
358     return;
359
360   /* poke the flow reporting process */
361   vlib_process_signal_event (vm, flow_report_process_node.index,
362                              1 /* type_opaque */ , 0 /* data */ );
363
364   REPLY_MACRO (VL_API_IPFIX_FLUSH_REPLY);
365 }
366
367 #include <vnet/ipfix-export/ipfix_export.api.c>
368 static clib_error_t *
369 flow_api_hookup (vlib_main_t * vm)
370 {
371   flow_report_main_t *frm = &flow_report_main;
372   /*
373    * Set up the (msg_name, crc, message-id) table
374    */
375   REPLY_MSG_ID_BASE = setup_message_id_table ();
376
377   return 0;
378 }
379
380 VLIB_API_INIT_FUNCTION (flow_api_hookup);
381
382 /*
383  * fd.io coding-style-patch-verification: ON
384  *
385  * Local Variables:
386  * eval: (c-set-style "gnu")
387  * End:
388  */