8cfbd59a90045c069a71cb169a87be168bd93110
[vpp.git] / vnet / vnet / flow / flow_report.c
1 /*
2  * Copyright (c) 2015 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  * flow_report.c
17  */
18 #include <vnet/flow/flow_report.h>
19 #include <vnet/api_errno.h>
20
21 int send_template_packet (flow_report_main_t *frm, 
22                           flow_report_t *fr,
23                           u32 * buffer_indexp)
24 {
25   u32 bi0;
26   vlib_buffer_t * b0;
27   ip4_ipfix_template_packet_t * tp;
28   ipfix_message_header_t * h;
29   ip4_header_t * ip;
30   udp_header_t * udp;
31   vlib_main_t * vm = frm->vlib_main;
32
33   ASSERT (buffer_indexp);
34
35   if (fr->update_rewrite || fr->rewrite == 0)
36     {
37       if (frm->ipfix_collector.as_u32 == 0 
38           || frm->src_address.as_u32 == 0)
39         {
40           clib_warning ("no collector: disabling flow collector process");
41           vlib_node_set_state (frm->vlib_main, flow_report_process_node.index,
42                                VLIB_NODE_STATE_DISABLED);
43           return -1;
44         }
45       vec_free (fr->rewrite);
46       fr->update_rewrite = 1;
47     }
48
49   if (fr->update_rewrite)
50     {
51       fr->rewrite = fr->rewrite_callback (frm, fr,
52                                           &frm->ipfix_collector,
53                                           &frm->src_address,
54                                           frm->collector_port);
55       fr->update_rewrite = 0;
56     }
57
58   if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
59     return -1;
60   
61   b0 = vlib_get_buffer (vm, bi0);
62
63   ASSERT (vec_len (fr->rewrite) < VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES);
64     
65   clib_memcpy (b0->data, fr->rewrite, vec_len (fr->rewrite));
66   b0->current_data = 0;
67   b0->current_length = vec_len (fr->rewrite);
68   b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
69   vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
70   vnet_buffer (b0)->sw_if_index[VLIB_TX] = frm->fib_index;
71
72   tp = vlib_buffer_get_current (b0);
73   ip = (ip4_header_t *) &tp->ip4;
74   udp = (udp_header_t *) (ip+1);
75   h = (ipfix_message_header_t *)(udp+1);
76
77   /* FIXUP: message header export_time */ 
78   h->export_time = (u32) 
79     (((f64)frm->unix_time_0) + 
80      (vlib_time_now(frm->vlib_main) - frm->vlib_time_0));
81   h->export_time = clib_host_to_net_u32(h->export_time);
82
83   /* FIXUP: message header sequence_number. Templates do not increase it */
84   h->sequence_number = clib_host_to_net_u32(fr->sequence_number);
85
86   /* FIXUP: udp length */
87   udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
88
89   *buffer_indexp = bi0;
90
91   fr->last_template_sent = vlib_time_now (vm);
92
93   return 0;
94 }
95
96 static uword
97 flow_report_process (vlib_main_t * vm,
98                      vlib_node_runtime_t * rt,
99                      vlib_frame_t * f)
100 {
101   flow_report_main_t * frm = &flow_report_main;
102   flow_report_t * fr;
103   u32 ip4_lookup_node_index;
104   vlib_node_t * ip4_lookup_node;
105   vlib_frame_t * nf = 0;
106   u32 template_bi;
107   u32 * to_next;
108   int send_template;
109   f64 now;
110   int rv;
111   uword event_type;
112   uword *event_data = 0;
113
114   /* Wait for Godot... */
115   vlib_process_wait_for_event_or_clock (vm, 1e9);
116   event_type = vlib_process_get_events (vm, &event_data);
117   if (event_type != 1)
118       clib_warning ("bogus kickoff event received, %d", event_type);
119   vec_reset_length (event_data);
120
121   /* Enqueue pkts to ip4-lookup */
122   ip4_lookup_node = vlib_get_node_by_name (vm, (u8 *) "ip4-lookup");
123   ip4_lookup_node_index = ip4_lookup_node->index;
124
125   while (1) 
126     {
127       vlib_process_suspend (vm, 5.0);
128       
129       vec_foreach (fr, frm->reports)
130         {
131           now = vlib_time_now (vm);
132
133           /* Need to send a template packet? */
134           send_template =
135               now > (fr->last_template_sent + frm->template_interval);
136           send_template += fr->last_template_sent == 0;
137           template_bi = ~0;
138           rv = 0;
139
140           if (send_template)
141             rv = send_template_packet (frm, fr, &template_bi);
142
143           if (rv < 0)
144             continue;
145
146           nf = vlib_get_frame_to_node (vm, ip4_lookup_node_index);
147           nf->n_vectors = 0;
148           to_next = vlib_frame_vector_args (nf);
149
150           if (template_bi != ~0)
151             {
152               to_next[0] = template_bi;
153               to_next++;
154               nf->n_vectors++;
155             }
156       
157           nf = fr->flow_data_callback (frm, fr, 
158                                        nf, to_next, ip4_lookup_node_index);
159           if (nf)
160             vlib_put_frame_to_node (vm, ip4_lookup_node_index, nf);
161         }
162     }
163
164   return 0; /* not so much */
165 }
166
167 VLIB_REGISTER_NODE (flow_report_process_node) = {
168     .function = flow_report_process,
169     .type = VLIB_NODE_TYPE_PROCESS,
170     .name = "flow-report-process",
171 };
172
173 int vnet_flow_report_add_del (flow_report_main_t *frm, 
174                               vnet_flow_report_add_del_args_t *a)
175 {
176   int i;
177   int found_index = ~0;
178   flow_report_t *fr;
179   
180   for (i = 0; i < vec_len(frm->reports); i++)
181     {
182       fr = vec_elt_at_index (frm->reports, i);
183       if (fr->opaque == a->opaque
184           && fr->rewrite_callback == a->rewrite_callback
185           && fr->flow_data_callback == a->flow_data_callback)
186         {
187           found_index = i;
188           break;
189         }
190     }
191
192   if (a->is_add == 0)
193     {
194       if (found_index != ~0)
195         {
196           vec_delete (frm->reports, 1, found_index);
197           return 0;
198         }
199       return VNET_API_ERROR_NO_SUCH_ENTRY;
200     }
201
202   vec_add2 (frm->reports, fr, 1);
203
204   fr->sequence_number = 0;
205   fr->domain_id = a->domain_id;
206   fr->update_rewrite = 1;
207   fr->opaque = a->opaque;
208   fr->rewrite_callback = a->rewrite_callback;
209   fr->flow_data_callback = a->flow_data_callback;
210   
211   return 0;
212 }
213
214 void vnet_flow_reports_reset (flow_report_main_t * frm)
215 {
216   flow_report_t *fr;
217   vec_foreach (fr, frm->reports)
218     {
219           fr->sequence_number = 0;
220       fr->update_rewrite = 1;
221       fr->last_template_sent = 0;
222     }
223 }
224
225 static clib_error_t *
226 set_ipfix_command_fn (vlib_main_t * vm,
227                  unformat_input_t * input,
228                  vlib_cli_command_t * cmd)
229 {
230   flow_report_main_t * frm = &flow_report_main;
231   ip4_address_t collector, src;
232   u16 collector_port = UDP_DST_PORT_ipfix;
233   u32 fib_id;
234   u32 fib_index = ~0;
235   
236   collector.as_u32 = 0;
237   src.as_u32 = 0;
238   u32 path_mtu = 512; // RFC 7011 section 10.3.3.
239   u32 template_interval = 20;
240
241   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
242     if (unformat (input, "collector %U", unformat_ip4_address, &collector))
243       ;
244     else if (unformat (input, "port %u", &collector_port))
245       ;
246     else if (unformat (input, "src %U", unformat_ip4_address, &src))
247       ;
248     else if (unformat (input, "fib-id %u", &fib_id))
249       {
250         ip4_main_t * im = &ip4_main;
251         uword * p = hash_get (im->fib_index_by_table_id, fib_id);
252         if (! p)
253           return clib_error_return (0, "fib ID %d doesn't exist\n",
254                                     fib_id);
255         fib_index = p[0];
256       }
257     else if (unformat (input, "path-mtu %u", &path_mtu))
258       ;
259     else if (unformat (input, "template-interval %u", &template_interval))
260       ;
261     else
262       break;
263   }
264   
265   if (collector.as_u32 == 0)
266     return clib_error_return (0, "collector address required");
267
268   if (src.as_u32 == 0)
269     return clib_error_return (0, "src address required");
270
271   if (path_mtu > 1450 /* vpp does not support fragmentation */)
272         return clib_error_return (0, "too big path-mtu value, maximum is 1450");
273
274   if (path_mtu < 68)
275         return clib_error_return (0, "too small path-mtu value, minimum is 68");
276
277   /* Reset report streams if we are reconfiguring IP addresses */
278   if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
279       frm->src_address.as_u32 != src.as_u32 ||
280       frm->collector_port != collector_port)
281     vnet_flow_reports_reset(frm);
282
283   frm->ipfix_collector.as_u32 = collector.as_u32;
284   frm->collector_port = collector_port;
285   frm->src_address.as_u32 = src.as_u32;
286   frm->fib_index = fib_index;
287   frm->path_mtu = path_mtu;
288   frm->template_interval = template_interval;
289   
290   vlib_cli_output (vm, "Collector %U, src address %U, "
291                            "fib index %d, path MTU %u, "
292                            "template resend interval %us",
293                    format_ip4_address, &frm->ipfix_collector,
294                    format_ip4_address, &frm->src_address,
295                    fib_index, path_mtu, template_interval);
296
297   /* Turn on the flow reporting process */
298   vlib_process_signal_event (vm, flow_report_process_node.index,
299                              1, 0);
300   return 0;
301 }
302
303 VLIB_CLI_COMMAND (set_ipfix_command, static) = {
304     .path = "set ipfix",
305     .short_help = "set ipfix collector <ip4-address> "
306                   "[port <port>] "
307                   "src <ip4-address> [fib-id <fib-id>] "
308                   "[path-mtu <path-mtu>] "
309                   "[template-interval <template-interval>]",
310     .function = set_ipfix_command_fn,
311 };
312
313 static clib_error_t * 
314 flow_report_init (vlib_main_t *vm)
315 {
316   flow_report_main_t * frm = &flow_report_main;
317
318   frm->vlib_main = vm;
319   frm->vnet_main = vnet_get_main();
320   frm->unix_time_0 = time(0);
321   frm->vlib_time_0 = vlib_time_now(frm->vlib_main);
322
323   return 0;
324 }
325
326 VLIB_INIT_FUNCTION (flow_report_init)