FD.io VPP  v18.04-17-g3a0d853
Vector Packet Processing
flow_report.c
Go to the documentation of this file.
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 
22 
25 {
27  return index < vec_len (frm->streams) &&
28  frm->streams[index].domain_id != ~0;
29 }
30 
32 add_stream (void)
33 {
35  u32 i;
36  for (i = 0; i < vec_len (frm->streams); i++)
37  if (!stream_index_valid (i))
38  return &frm->streams[i];
39  u32 index = vec_len (frm->streams);
40  vec_validate (frm->streams, index);
41  return &frm->streams[index];
42 }
43 
46 {
48  ASSERT (index < vec_len (frm->streams));
49  ASSERT (frm->streams[index].domain_id != ~0);
50  frm->streams[index].domain_id = ~0;
51 }
52 
53 static i32
54 find_stream (u32 domain_id, u16 src_port)
55 {
57  flow_report_stream_t *stream;
58  u32 i;
59  for (i = 0; i < vec_len (frm->streams); i++)
60  if (stream_index_valid (i))
61  {
62  stream = &frm->streams[i];
63  if (domain_id == stream->domain_id)
64  {
65  if (src_port != stream->src_port)
66  return -2;
67  return i;
68  }
69  else if (src_port == stream->src_port)
70  {
71  return -2;
72  }
73  }
74  return -1;
75 }
76 
77 int
79  flow_report_t * fr, u32 * buffer_indexp)
80 {
81  u32 bi0;
82  vlib_buffer_t *b0;
85  ip4_header_t *ip;
86  udp_header_t *udp;
87  vlib_main_t *vm = frm->vlib_main;
88  flow_report_stream_t *stream;
90 
91  ASSERT (buffer_indexp);
92 
93  if (fr->update_rewrite || fr->rewrite == 0)
94  {
95  if (frm->ipfix_collector.as_u32 == 0 || frm->src_address.as_u32 == 0)
96  {
98  VLIB_NODE_STATE_DISABLED);
99  return -1;
100  }
101  vec_free (fr->rewrite);
102  fr->update_rewrite = 1;
103  }
104 
105  if (fr->update_rewrite)
106  {
107  fr->rewrite = fr->rewrite_callback (frm, fr,
108  &frm->ipfix_collector,
109  &frm->src_address,
110  frm->collector_port);
111  fr->update_rewrite = 0;
112  }
113 
114  if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
115  return -1;
116 
117  b0 = vlib_get_buffer (vm, bi0);
118 
119  /* Initialize the buffer */
123 
125 
126  clib_memcpy (b0->data, fr->rewrite, vec_len (fr->rewrite));
127  b0->current_data = 0;
128  b0->current_length = vec_len (fr->rewrite);
129  b0->flags |= (VLIB_BUFFER_TOTAL_LENGTH_VALID | VNET_BUFFER_F_FLOW_REPORT);
130  vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
131  vnet_buffer (b0)->sw_if_index[VLIB_TX] = frm->fib_index;
132 
133  tp = vlib_buffer_get_current (b0);
134  ip = (ip4_header_t *) & tp->ip4;
135  udp = (udp_header_t *) (ip + 1);
136  h = (ipfix_message_header_t *) (udp + 1);
137 
138  /* FIXUP: message header export_time */
139  h->export_time = (u32)
140  (((f64) frm->unix_time_0) +
141  (vlib_time_now (frm->vlib_main) - frm->vlib_time_0));
142  h->export_time = clib_host_to_net_u32 (h->export_time);
143 
144  stream = &frm->streams[fr->stream_index];
145 
146  /* FIXUP: message header sequence_number. Templates do not increase it */
147  h->sequence_number = clib_host_to_net_u32 (stream->sequence_number);
148 
149  /* FIXUP: udp length */
150  udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
151 
152  if (frm->udp_checksum)
153  {
154  /* RFC 7011 section 10.3.2. */
155  udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
156  if (udp->checksum == 0)
157  udp->checksum = 0xffff;
158  }
159 
160  *buffer_indexp = bi0;
161 
163 
164  return 0;
165 }
166 
167 static uword
170 {
172  flow_report_t *fr;
173  u32 ip4_lookup_node_index;
175  vlib_frame_t *nf = 0;
176  u32 template_bi;
177  u32 *to_next;
178  int send_template;
179  f64 now;
180  int rv;
181  uword event_type;
182  uword *event_data = 0;
183 
184  /* Wait for Godot... */
186  event_type = vlib_process_get_events (vm, &event_data);
187  if (event_type != 1)
188  clib_warning ("bogus kickoff event received, %d", event_type);
189  vec_reset_length (event_data);
190 
191  /* Enqueue pkts to ip4-lookup */
192  ip4_lookup_node = vlib_get_node_by_name (vm, (u8 *) "ip4-lookup");
193  ip4_lookup_node_index = ip4_lookup_node->index;
194 
195  while (1)
196  {
198  event_type = vlib_process_get_events (vm, &event_data);
199  vec_reset_length (event_data);
200 
201  vec_foreach (fr, frm->reports)
202  {
203  now = vlib_time_now (vm);
204 
205  /* Need to send a template packet? */
206  send_template =
207  now > (fr->last_template_sent + frm->template_interval);
208  send_template += fr->last_template_sent == 0;
209  template_bi = ~0;
210  rv = 0;
211 
212  if (send_template)
213  rv = send_template_packet (frm, fr, &template_bi);
214 
215  if (rv < 0)
216  continue;
217 
218  nf = vlib_get_frame_to_node (vm, ip4_lookup_node_index);
219  nf->n_vectors = 0;
220  to_next = vlib_frame_vector_args (nf);
221 
222  if (template_bi != ~0)
223  {
224  to_next[0] = template_bi;
225  to_next++;
226  nf->n_vectors++;
227  }
228 
229  nf = fr->flow_data_callback (frm, fr,
230  nf, to_next, ip4_lookup_node_index);
231  if (nf)
232  vlib_put_frame_to_node (vm, ip4_lookup_node_index, nf);
233  }
234  }
235 
236  return 0; /* not so much */
237 }
238 
239 /* *INDENT-OFF* */
241  .function = flow_report_process,
242  .type = VLIB_NODE_TYPE_PROCESS,
243  .name = "flow-report-process",
244 };
245 /* *INDENT-ON* */
246 
247 int
250  u16 * template_id)
251 {
252  int i;
253  int found_index = ~0;
254  flow_report_t *fr;
255  flow_report_stream_t *stream;
256  u32 si;
257 
258  si = find_stream (a->domain_id, a->src_port);
259  if (si == -2)
260  return VNET_API_ERROR_INVALID_VALUE;
261  if (si == -1 && a->is_add == 0)
262  return VNET_API_ERROR_NO_SUCH_ENTRY;
263 
264  for (i = 0; i < vec_len (frm->reports); i++)
265  {
266  fr = vec_elt_at_index (frm->reports, i);
267  if (fr->opaque.as_uword == a->opaque.as_uword
270  {
271  found_index = i;
272  if (template_id)
273  *template_id = fr->template_id;
274  break;
275  }
276  }
277 
278  if (a->is_add == 0)
279  {
280  if (found_index != ~0)
281  {
282  vec_delete (frm->reports, 1, found_index);
283  stream = &frm->streams[si];
284  stream->n_reports--;
285  if (stream->n_reports == 0)
286  delete_stream (si);
287  return 0;
288  }
289  return VNET_API_ERROR_NO_SUCH_ENTRY;
290  }
291 
292  if (found_index != ~0)
293  return VNET_API_ERROR_VALUE_EXIST;
294 
295  if (si == -1)
296  {
297  stream = add_stream ();
298  stream->domain_id = a->domain_id;
299  stream->src_port = a->src_port;
300  stream->sequence_number = 0;
301  stream->n_reports = 0;
302  si = stream - frm->streams;
303  }
304  else
305  stream = &frm->streams[si];
306 
307  stream->n_reports++;
308 
309  vec_add2 (frm->reports, fr, 1);
310 
311  fr->stream_index = si;
312  fr->template_id = 256 + stream->next_template_no;
313  stream->next_template_no = (stream->next_template_no + 1) % (65536 - 256);
314  fr->update_rewrite = 1;
315  fr->opaque = a->opaque;
318 
319  if (template_id)
320  *template_id = fr->template_id;
321 
322  return 0;
323 }
324 
325 clib_error_t *
327 {
328  switch (error)
329  {
330  case 0:
331  return 0;
332  case VNET_API_ERROR_NO_SUCH_ENTRY:
333  return clib_error_return (0, "Flow report not found");
334  case VNET_API_ERROR_VALUE_EXIST:
335  return clib_error_return (0, "Flow report already exists");
336  case VNET_API_ERROR_INVALID_VALUE:
337  return clib_error_return (0, "Expecting either still unused values "
338  "for both domain_id and src_port "
339  "or already used values for both fields");
340  default:
341  return clib_error_return (0, "vnet_flow_report_add_del returned %d",
342  error);
343  }
344 }
345 
346 void
348 {
349  flow_report_t *fr;
350  u32 i;
351 
352  for (i = 0; i < vec_len (frm->streams); i++)
353  if (stream_index_valid (i))
354  frm->streams[i].sequence_number = 0;
355 
356  vec_foreach (fr, frm->reports)
357  {
358  fr->update_rewrite = 1;
359  fr->last_template_sent = 0;
360  }
361 }
362 
363 void
365 {
366  flow_report_t *fr;
367 
368  frm->streams[stream_index].sequence_number = 0;
369 
370  vec_foreach (fr, frm->reports)
371  if (frm->reports->stream_index == stream_index)
372  {
373  fr->update_rewrite = 1;
374  fr->last_template_sent = 0;
375  }
376 }
377 
378 int
380  u32 old_domain_id, u16 old_src_port,
381  u32 new_domain_id, u16 new_src_port)
382 {
383  i32 stream_index = find_stream (old_domain_id, old_src_port);
384  if (stream_index < 0)
385  return 1;
386  flow_report_stream_t *stream = &frm->streams[stream_index];
387  stream->domain_id = new_domain_id;
388  stream->src_port = new_src_port;
389  if (old_domain_id != new_domain_id || old_src_port != new_src_port)
390  vnet_stream_reset (frm, stream_index);
391  return 0;
392 }
393 
394 static clib_error_t *
396  unformat_input_t * input,
397  vlib_cli_command_t * cmd)
398 {
400  ip4_address_t collector, src;
401  u16 collector_port = UDP_DST_PORT_ipfix;
402  u32 fib_id;
403  u32 fib_index = ~0;
404 
405  collector.as_u32 = 0;
406  src.as_u32 = 0;
407  u32 path_mtu = 512; // RFC 7011 section 10.3.3.
408  u32 template_interval = 20;
409  u8 udp_checksum = 0;
410 
412  {
413  if (unformat (input, "collector %U", unformat_ip4_address, &collector))
414  ;
415  else if (unformat (input, "port %u", &collector_port))
416  ;
417  else if (unformat (input, "src %U", unformat_ip4_address, &src))
418  ;
419  else if (unformat (input, "fib-id %u", &fib_id))
420  {
421  ip4_main_t *im = &ip4_main;
422  uword *p = hash_get (im->fib_index_by_table_id, fib_id);
423  if (!p)
424  return clib_error_return (0, "fib ID %d doesn't exist\n", fib_id);
425  fib_index = p[0];
426  }
427  else if (unformat (input, "path-mtu %u", &path_mtu))
428  ;
429  else if (unformat (input, "template-interval %u", &template_interval))
430  ;
431  else if (unformat (input, "udp-checksum"))
432  udp_checksum = 1;
433  else
434  break;
435  }
436 
437  if (collector.as_u32 != 0 && src.as_u32 == 0)
438  return clib_error_return (0, "src address required");
439 
440  if (path_mtu > 1450 /* vpp does not support fragmentation */ )
441  return clib_error_return (0, "too big path-mtu value, maximum is 1450");
442 
443  if (path_mtu < 68)
444  return clib_error_return (0, "too small path-mtu value, minimum is 68");
445 
446  /* Reset report streams if we are reconfiguring IP addresses */
447  if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
448  frm->src_address.as_u32 != src.as_u32 ||
449  frm->collector_port != collector_port)
451 
452  frm->ipfix_collector.as_u32 = collector.as_u32;
453  frm->collector_port = collector_port;
454  frm->src_address.as_u32 = src.as_u32;
455  frm->fib_index = fib_index;
456  frm->path_mtu = path_mtu;
457  frm->template_interval = template_interval;
458  frm->udp_checksum = udp_checksum;
459 
460  if (collector.as_u32)
461  vlib_cli_output (vm, "Collector %U, src address %U, "
462  "fib index %d, path MTU %u, "
463  "template resend interval %us, "
464  "udp checksum %s",
467  fib_index, path_mtu, template_interval,
468  udp_checksum ? "enabled" : "disabled");
469  else
470  vlib_cli_output (vm, "IPFIX Collector is disabled");
471 
472  /* Turn on the flow reporting process */
474  return 0;
475 }
476 
477 /* *INDENT-OFF* */
478 VLIB_CLI_COMMAND (set_ipfix_exporter_command, static) = {
479  .path = "set ipfix exporter",
480  .short_help = "set ipfix exporter "
481  "collector <ip4-address> [port <port>] "
482  "src <ip4-address> [fib-id <fib-id>] "
483  "[path-mtu <path-mtu>] "
484  "[template-interval <template-interval>]",
485  "[udp-checksum]",
486  .function = set_ipfix_exporter_command_fn,
487 };
488 /* *INDENT-ON* */
489 
490 
491 static clib_error_t *
493  unformat_input_t * input, vlib_cli_command_t * cmd)
494 {
495  /* poke the flow reporting process */
497  return 0;
498 }
499 
500 /* *INDENT-OFF* */
501 VLIB_CLI_COMMAND (ipfix_flush_command, static) = {
502  .path = "ipfix flush",
503  .short_help = "flush the current ipfix data [for make test]",
504  .function = ipfix_flush_command_fn,
505 };
506 /* *INDENT-ON* */
507 
508 static clib_error_t *
510 {
512 
513  frm->vlib_main = vm;
514  frm->vnet_main = vnet_get_main ();
515  frm->unix_time_0 = time (0);
516  frm->vlib_time_0 = vlib_time_now (frm->vlib_main);
517  frm->fib_index = ~0;
518 
519  return 0;
520 }
521 
523 /*
524  * fd.io coding-style-patch-verification: ON
525  *
526  * Local Variables:
527  * eval: (c-set-style "gnu")
528  * End:
529  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:434
clib_error_t * flow_report_add_del_error_to_clib_error(int error)
Definition: flow_report.c:326
static f64 vlib_process_wait_for_event_or_clock(vlib_main_t *vm, f64 dt)
Suspend a cooperative multi-tasking thread Waits for an event, or for the indicated number of seconds...
Definition: node_funcs.h:699
vlib_node_registration_t flow_report_process_node
(constructor) VLIB_REGISTER_NODE (flow_report_process_node)
Definition: flow_report.c:240
a
Definition: bitmap.h:516
u16 udp_checksum(udp_header_t *uh, u32 udp_len, void *ih, u8 version)
Definition: packets.c:114
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
uword as_uword
Definition: flow_report.h:58
u32 index
Definition: node.h:237
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:227
u32 stream_index
Definition: flow_report.h:75
static clib_error_t * ipfix_flush_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: flow_report.c:492
int vnet_flow_report_add_del(flow_report_main_t *frm, vnet_flow_report_add_del_args_t *a, u16 *template_id)
Definition: flow_report.c:248
opaque_t opaque
Definition: flow_report.h:83
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:559
int i
ip4_address_t src_address
Definition: flow_report.h:100
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
ip4_address_t ipfix_collector
Definition: flow_report.h:98
vlib_node_registration_t ip4_lookup_node
(constructor) VLIB_REGISTER_NODE (ip4_lookup_node)
Definition: ip4_forward.c:103
void vnet_stream_reset(flow_report_main_t *frm, u32 stream_index)
Definition: flow_report.c:364
flow_report_stream_t * streams
Definition: flow_report.h:95
format_function_t format_ip4_address
Definition: format.h:79
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:104
#define static_always_inline
Definition: clib.h:93
unformat_function_t unformat_ip4_address
Definition: format.h:76
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type...
Definition: node_funcs.h:542
vnet_flow_rewrite_callback_t * rewrite_callback
Definition: flow_report.h:130
int i32
Definition: types.h:81
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
vlib_frame_t * vlib_get_frame_to_node(vlib_main_t *vm, u32 to_node_index)
Definition: main.c:182
#define clib_error_return(e, args...)
Definition: error.h:99
#define VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES
Definition: buffer.h:447
static uword flow_report_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: flow_report.c:168
flow_report_t * reports
Definition: flow_report.h:94
#define fl(x, y)
flow_report_main_t flow_report_main
Definition: flow_report.c:21
#define hash_get(h, key)
Definition: hash.h:248
uword * fib_index_by_table_id
Hash table mapping table id to fib index.
Definition: ip4.h:121
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:108
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:952
u8 * rewrite
Definition: flow_report.h:73
struct _unformat_input_t unformat_input_t
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:191
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:209
#define VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX
Definition: buffer.h:446
vnet_main_t * vnet_main
Definition: flow_report.h:118
int send_template_packet(flow_report_main_t *frm, flow_report_t *fr, u32 *buffer_indexp)
Definition: flow_report.c:78
static_always_inline flow_report_stream_t * add_stream(void)
Definition: flow_report.c:32
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
u16 n_vectors
Definition: node.h:344
vlib_main_t * vm
Definition: buffer.c:294
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:336
#define clib_warning(format, args...)
Definition: error.h:59
#define clib_memcpy(a, b, c)
Definition: string.h:75
static clib_error_t * set_ipfix_exporter_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: flow_report.c:395
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:45
static_always_inline u8 stream_index_valid(u32 index)
Definition: flow_report.c:24
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:154
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
vlib_main_t * vlib_main
Definition: flow_report.h:117
u16 ip4_tcp_udp_compute_checksum(vlib_main_t *vm, vlib_buffer_t *p0, ip4_header_t *ip0)
Definition: ip4_forward.c:1041
#define vec_delete(V, N, M)
Delete N elements starting at element M.
Definition: vec.h:783
IPv4 main type.
Definition: ip4.h:95
static_always_inline void delete_stream(u32 index)
Definition: flow_report.c:45
void vnet_flow_reports_reset(flow_report_main_t *frm)
Definition: flow_report.c:347
u16 template_id
Definition: flow_report.h:74
static void vlib_node_set_state(vlib_main_t *vm, u32 node_index, vlib_node_state_t new_state)
Set node dispatch state.
Definition: node_funcs.h:147
vnet_flow_data_callback_t * flow_data_callback
Definition: flow_report.h:89
u64 uword
Definition: types.h:112
Definition: defs.h:47
unsigned short u16
Definition: types.h:57
int update_rewrite
Definition: flow_report.h:77
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:142
unsigned char u8
Definition: types.h:56
f64 last_template_sent
Definition: flow_report.h:76
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:553
int vnet_stream_change(flow_report_main_t *frm, u32 old_domain_id, u16 old_src_port, u32 new_domain_id, u16 new_src_port)
Definition: flow_report.c:379
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:267
vnet_flow_data_callback_t * flow_data_callback
Definition: flow_report.h:129
static i32 find_stream(u32 domain_id, u16 src_port)
Definition: flow_report.c:54
static void vlib_buffer_init_for_free_list(vlib_buffer_t *dst, vlib_buffer_free_list_t *fl)
Definition: buffer_funcs.h:915
#define vnet_buffer(b)
Definition: buffer.h:372
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:818
vnet_flow_rewrite_callback_t * rewrite_callback
Definition: flow_report.h:86
u8 data[0]
Packet data.
Definition: buffer.h:179
#define vec_foreach(var, vec)
Vector iterator.
static vlib_buffer_free_list_t * vlib_buffer_get_free_list(vlib_main_t *vm, vlib_buffer_free_list_index_t free_list_index)
Definition: buffer_funcs.h:519
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:111
static u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:347
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:680
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
static clib_error_t * flow_report_init(vlib_main_t *vm)
Definition: flow_report.c:509
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
Definition: defs.h:46
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169