FD.io VPP  v16.09
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 
24  flow_report_t *fr,
25  u32 * buffer_indexp)
26 {
27  u32 bi0;
28  vlib_buffer_t * b0;
31  ip4_header_t * ip;
32  udp_header_t * udp;
33  vlib_main_t * vm = frm->vlib_main;
34 
35  ASSERT (buffer_indexp);
36 
37  if (fr->update_rewrite || fr->rewrite == 0)
38  {
39  if (frm->ipfix_collector.as_u32 == 0
40  || frm->src_address.as_u32 == 0)
41  {
42  clib_warning ("no collector: disabling flow collector process");
44  VLIB_NODE_STATE_DISABLED);
45  return -1;
46  }
47  vec_free (fr->rewrite);
48  fr->update_rewrite = 1;
49  }
50 
51  if (fr->update_rewrite)
52  {
53  fr->rewrite = fr->rewrite_callback (frm, fr,
54  &frm->ipfix_collector,
55  &frm->src_address,
56  frm->collector_port);
57  fr->update_rewrite = 0;
58  }
59 
60  if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
61  return -1;
62 
63  b0 = vlib_get_buffer (vm, bi0);
64 
66 
67  clib_memcpy (b0->data, fr->rewrite, vec_len (fr->rewrite));
68  b0->current_data = 0;
69  b0->current_length = vec_len (fr->rewrite);
71  vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
72  vnet_buffer (b0)->sw_if_index[VLIB_TX] = frm->fib_index;
73 
74  tp = vlib_buffer_get_current (b0);
75  ip = (ip4_header_t *) &tp->ip4;
76  udp = (udp_header_t *) (ip+1);
77  h = (ipfix_message_header_t *)(udp+1);
78 
79  /* FIXUP: message header export_time */
80  h->export_time = (u32)
81  (((f64)frm->unix_time_0) +
82  (vlib_time_now(frm->vlib_main) - frm->vlib_time_0));
83  h->export_time = clib_host_to_net_u32(h->export_time);
84 
85  /* FIXUP: message header sequence_number. Templates do not increase it */
86  h->sequence_number = clib_host_to_net_u32(fr->sequence_number);
87 
88  /* FIXUP: udp length */
89  udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
90 
91  *buffer_indexp = bi0;
92 
94 
95  return 0;
96 }
97 
98 static uword
100  vlib_node_runtime_t * rt,
101  vlib_frame_t * f)
102 {
104  flow_report_t * fr;
105  u32 ip4_lookup_node_index;
107  vlib_frame_t * nf = 0;
108  u32 template_bi;
109  u32 * to_next;
110  int send_template;
111  f64 now;
112  int rv;
113  uword event_type;
114  uword *event_data = 0;
115 
116  /* Wait for Godot... */
118  event_type = vlib_process_get_events (vm, &event_data);
119  if (event_type != 1)
120  clib_warning ("bogus kickoff event received, %d", event_type);
121  vec_reset_length (event_data);
122 
123  /* Enqueue pkts to ip4-lookup */
124  ip4_lookup_node = vlib_get_node_by_name (vm, (u8 *) "ip4-lookup");
125  ip4_lookup_node_index = ip4_lookup_node->index;
126 
127  while (1)
128  {
129  vlib_process_suspend (vm, 5.0);
130 
131  vec_foreach (fr, frm->reports)
132  {
133  now = vlib_time_now (vm);
134 
135  /* Need to send a template packet? */
136  send_template =
137  now > (fr->last_template_sent + frm->template_interval);
138  send_template += fr->last_template_sent == 0;
139  template_bi = ~0;
140  rv = 0;
141 
142  if (send_template)
143  rv = send_template_packet (frm, fr, &template_bi);
144 
145  if (rv < 0)
146  continue;
147 
148  nf = vlib_get_frame_to_node (vm, ip4_lookup_node_index);
149  nf->n_vectors = 0;
150  to_next = vlib_frame_vector_args (nf);
151 
152  if (template_bi != ~0)
153  {
154  to_next[0] = template_bi;
155  to_next++;
156  nf->n_vectors++;
157  }
158 
159  nf = fr->flow_data_callback (frm, fr,
160  nf, to_next, ip4_lookup_node_index);
161  if (nf)
162  vlib_put_frame_to_node (vm, ip4_lookup_node_index, nf);
163  }
164  }
165 
166  return 0; /* not so much */
167 }
168 
170  .function = flow_report_process,
171  .type = VLIB_NODE_TYPE_PROCESS,
172  .name = "flow-report-process",
173 };
174 
177 {
178  int i;
179  int found_index = ~0;
180  flow_report_t *fr;
181 
182  for (i = 0; i < vec_len(frm->reports); i++)
183  {
184  fr = vec_elt_at_index (frm->reports, i);
185  if (fr->opaque == a->opaque
188  {
189  found_index = i;
190  break;
191  }
192  }
193 
194  if (a->is_add == 0)
195  {
196  if (found_index != ~0)
197  {
198  vec_delete (frm->reports, 1, found_index);
199  return 0;
200  }
201  return VNET_API_ERROR_NO_SUCH_ENTRY;
202  }
203 
204  vec_add2 (frm->reports, fr, 1);
205 
206  fr->sequence_number = 0;
207  fr->domain_id = a->domain_id;
208  fr->src_port = a->src_port;
209  fr->update_rewrite = 1;
210  fr->opaque = a->opaque;
213 
214  return 0;
215 }
216 
218 {
219  flow_report_t *fr;
220  vec_foreach (fr, frm->reports)
221  {
222  fr->sequence_number = 0;
223  fr->update_rewrite = 1;
224  fr->last_template_sent = 0;
225  }
226 }
227 
228 static clib_error_t *
230  unformat_input_t * input,
231  vlib_cli_command_t * cmd)
232 {
234  ip4_address_t collector, src;
235  u16 collector_port = UDP_DST_PORT_ipfix;
236  u32 fib_id;
237  u32 fib_index = ~0;
238 
239  collector.as_u32 = 0;
240  src.as_u32 = 0;
241  u32 path_mtu = 512; // RFC 7011 section 10.3.3.
242  u32 template_interval = 20;
243 
244  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
245  if (unformat (input, "collector %U", unformat_ip4_address, &collector))
246  ;
247  else if (unformat (input, "port %u", &collector_port))
248  ;
249  else if (unformat (input, "src %U", unformat_ip4_address, &src))
250  ;
251  else if (unformat (input, "fib-id %u", &fib_id))
252  {
253  ip4_main_t * im = &ip4_main;
254  uword * p = hash_get (im->fib_index_by_table_id, fib_id);
255  if (! p)
256  return clib_error_return (0, "fib ID %d doesn't exist\n",
257  fib_id);
258  fib_index = p[0];
259  }
260  else if (unformat (input, "path-mtu %u", &path_mtu))
261  ;
262  else if (unformat (input, "template-interval %u", &template_interval))
263  ;
264  else
265  break;
266  }
267 
268  if (collector.as_u32 == 0)
269  return clib_error_return (0, "collector address required");
270 
271  if (src.as_u32 == 0)
272  return clib_error_return (0, "src address required");
273 
274  if (path_mtu > 1450 /* vpp does not support fragmentation */)
275  return clib_error_return (0, "too big path-mtu value, maximum is 1450");
276 
277  if (path_mtu < 68)
278  return clib_error_return (0, "too small path-mtu value, minimum is 68");
279 
280  /* Reset report streams if we are reconfiguring IP addresses */
281  if (frm->ipfix_collector.as_u32 != collector.as_u32 ||
282  frm->src_address.as_u32 != src.as_u32 ||
283  frm->collector_port != collector_port)
285 
286  frm->ipfix_collector.as_u32 = collector.as_u32;
287  frm->collector_port = collector_port;
288  frm->src_address.as_u32 = src.as_u32;
289  frm->fib_index = fib_index;
290  frm->path_mtu = path_mtu;
291  frm->template_interval = template_interval;
292 
293  vlib_cli_output (vm, "Collector %U, src address %U, "
294  "fib index %d, path MTU %u, "
295  "template resend interval %us",
298  fib_index, path_mtu, template_interval);
299 
300  /* Turn on the flow reporting process */
302  1, 0);
303  return 0;
304 }
305 
306 VLIB_CLI_COMMAND (set_ipfix_command, static) = {
307  .path = "set ipfix",
308  .short_help = "set ipfix collector <ip4-address> "
309  "[port <port>] "
310  "src <ip4-address> [fib-id <fib-id>] "
311  "[path-mtu <path-mtu>] "
312  "[template-interval <template-interval>]",
313  .function = set_ipfix_command_fn,
314 };
315 
316 static clib_error_t *
318 {
320 
321  frm->vlib_main = vm;
322  frm->vnet_main = vnet_get_main();
323  frm->unix_time_0 = time(0);
324  frm->vlib_time_0 = vlib_time_now(frm->vlib_main);
325 
326  return 0;
327 }
328 
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:966
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:682
vlib_node_registration_t flow_report_process_node
(constructor) VLIB_REGISTER_NODE (flow_report_process_node)
Definition: flow_report.c:169
a
Definition: bitmap.h:516
#define UNFORMAT_END_OF_INPUT
Definition: format.h:143
u32 index
Definition: node.h:237
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:182
void * opaque
Definition: flow_report.h:67
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:521
ip4_address_t src_address
Definition: flow_report.h:82
format_function_t format_ip4_address
Definition: format.h:71
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
ip4_address_t ipfix_collector
Definition: flow_report.h:80
vlib_node_registration_t ip4_lookup_node
(constructor) VLIB_REGISTER_NODE (ip4_lookup_node)
Definition: ip4_forward.c:1541
static uword vlib_process_suspend(vlib_main_t *vm, f64 dt)
Suspend a vlib cooperative multi-tasking thread for a period of time.
Definition: node_funcs.h:430
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:78
#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:525
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:187
vnet_flow_rewrite_callback_t * rewrite_callback
Definition: flow_report.h:108
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define clib_warning(format, args...)
Definition: error.h:59
static uword flow_report_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: flow_report.c:99
flow_report_t * reports
Definition: flow_report.h:77
unformat_function_t unformat_ip4_address
Definition: format.h:68
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:127
#define VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES
Definition: buffer.h:304
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:82
static void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:929
u8 * rewrite
Definition: flow_report.h:56
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:194
vnet_main_t * vnet_main
Definition: flow_report.h:97
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:575
int send_template_packet(flow_report_main_t *frm, flow_report_t *fr, u32 *buffer_indexp)
Definition: flow_report.c:23
u32 sequence_number
Definition: flow_report.h:57
u16 n_vectors
Definition: node.h:344
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
#define clib_memcpy(a, b, c)
Definition: string.h:63
#define VLIB_BUFFER_TOTAL_LENGTH_VALID
Definition: buffer.h:97
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
vlib_main_t * vlib_main
Definition: flow_report.h:96
#define vnet_buffer(b)
Definition: buffer.h:335
#define vec_delete(V, N, M)
Delete N elements starting at element M.
Definition: vec.h:745
IPv4 main type.
Definition: ip4.h:114
void vnet_flow_reports_reset(flow_report_main_t *frm)
Definition: flow_report.c:217
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:144
u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: dpdk_buffer.c:643
vnet_flow_data_callback_t * flow_data_callback
Definition: flow_report.h:73
u64 uword
Definition: types.h:112
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:45
Definition: defs.h:47
unsigned short u16
Definition: types.h:57
int update_rewrite
Definition: flow_report.h:61
VLIB_CLI_COMMAND(set_interface_ip_source_and_port_range_check_command, static)
#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:60
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:251
static clib_error_t * set_ipfix_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: flow_report.c:229
vnet_flow_data_callback_t * flow_data_callback
Definition: flow_report.h:107
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:169
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
ip4_main_t ip4_main
Global ip4 main structure.
Definition: ip4_forward.c:1578
vnet_flow_rewrite_callback_t * rewrite_callback
Definition: flow_report.h:70
u8 data[0]
Packet data.
Definition: buffer.h:151
#define vec_foreach(var, vec)
Vector iterator.
#define clib_error_return(e, args...)
Definition: error.h:111
struct _unformat_input_t unformat_input_t
vlib_frame_t * vlib_get_frame_to_node(vlib_main_t *vm, u32 to_node_index)
Definition: main.c:185
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:85
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69
static clib_error_t * flow_report_init(vlib_main_t *vm)
Definition: flow_report.c:317
Definition: defs.h:46
int vnet_flow_report_add_del(flow_report_main_t *frm, vnet_flow_report_add_del_args_t *a)
Definition: flow_report.c:175