FD.io VPP  v16.06
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  flow_report_t *fr,
23  u32 * buffer_indexp)
24 {
25  u32 bi0;
26  vlib_buffer_t * b0;
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");
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  fr->update_rewrite = 0;
55  }
56 
57  if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
58  return -1;
59 
60  b0 = vlib_get_buffer (vm, bi0);
61 
63 
64  clib_memcpy (b0->data, fr->rewrite, vec_len (fr->rewrite));
65  b0->current_data = 0;
66  b0->current_length = vec_len (fr->rewrite);
68  vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
69  /* $$$ for now, look up in fib-0. Later: arbitrary TX fib */
70  vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
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  return 0;
91 }
92 
93 static uword
96  vlib_frame_t * f)
97 {
99  flow_report_t * fr;
100  u32 ip4_lookup_node_index;
102  vlib_frame_t * nf = 0;
103  u32 template_bi;
104  u32 * to_next;
105  int send_template;
106  f64 now;
107  int rv;
108  uword event_type;
109  uword *event_data = 0;
110 
111  /* Wait for Godot... */
113  event_type = vlib_process_get_events (vm, &event_data);
114  if (event_type != 1)
115  clib_warning ("bogus kickoff event received, %d", event_type);
116  vec_reset_length (event_data);
117 
118  /* Enqueue pkts to ip4-lookup */
119  ip4_lookup_node = vlib_get_node_by_name (vm, (u8 *) "ip4-lookup");
120  ip4_lookup_node_index = ip4_lookup_node->index;
121 
122  while (1)
123  {
124  vlib_process_suspend (vm, 5.0);
125 
126  vec_foreach (fr, frm->reports)
127  {
128  now = vlib_time_now (vm);
129 
130  /* Need to send a template packet? */
131  send_template = now > (fr->last_template_sent + 20.0);
132  send_template += fr->last_template_sent == 0;
133  template_bi = ~0;
134  rv = 0;
135 
136  if (send_template)
137  rv = send_template_packet (frm, fr, &template_bi);
138 
139  if (rv < 0)
140  continue;
141 
142  nf = vlib_get_frame_to_node (vm, ip4_lookup_node_index);
143  nf->n_vectors = 0;
144  to_next = vlib_frame_vector_args (nf);
145 
146  if (template_bi != ~0)
147  {
148  to_next[0] = template_bi;
149  to_next++;
150  nf->n_vectors++;
151  }
152 
153  nf = fr->flow_data_callback (frm, fr,
154  nf, to_next, ip4_lookup_node_index);
155  if (nf)
156  vlib_put_frame_to_node (vm, ip4_lookup_node_index, nf);
157  }
158  }
159 
160  return 0; /* not so much */
161 }
162 
164  .function = flow_report_process,
165  .type = VLIB_NODE_TYPE_PROCESS,
166  .name = "flow-report-process",
167 };
168 
171 {
172  int i;
173  int found_index = ~0;
174  flow_report_t *fr;
175 
176  for (i = 0; i < vec_len(frm->reports); i++)
177  {
178  fr = vec_elt_at_index (frm->reports, i);
179  if (fr->opaque == a->opaque
182  {
183  found_index = i;
184  break;
185  }
186  }
187 
188  if (a->is_add == 0)
189  {
190  if (found_index != ~0)
191  {
192  vec_delete (frm->reports, 1, found_index);
193  return 0;
194  }
195  return VNET_API_ERROR_NO_SUCH_ENTRY;
196  }
197 
198  vec_add2 (frm->reports, fr, 1);
199 
200  fr->sequence_number = 0;
201  fr->domain_id = a->domain_id;
202  fr->update_rewrite = 1;
203  fr->opaque = a->opaque;
206 
207  return 0;
208 }
209 
210 static clib_error_t *
212  unformat_input_t * input,
213  vlib_cli_command_t * cmd)
214 {
216  ip4_address_t collector, src;
217 
218  collector.as_u32 = 0;
219  src.as_u32 = 0;
220 
221  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
222  if (unformat (input, "collector %U", unformat_ip4_address, &collector))
223  ;
224  else if (unformat (input, "src %U", unformat_ip4_address, &src))
225  ;
226  else
227  break;
228  }
229 
230  if (collector.as_u32 == 0)
231  return clib_error_return (0, "collector address required");
232 
233  if (src.as_u32 == 0)
234  return clib_error_return (0, "src address required");
235 
236  frm->ipfix_collector.as_u32 = collector.as_u32;
237  frm->src_address.as_u32 = src.as_u32;
238 
239  vlib_cli_output (vm, "Collector %U, src address %U",
242 
243  /* Turn on the flow reporting process */
245  1, 0);
246  return 0;
247 }
248 
249 VLIB_CLI_COMMAND (set_ipfix_command, static) = {
250  .path = "set ipfix",
251  .short_help = "set ipfix collector <ip4-address> src <ip4-address>",
252  .function = set_ipfix_command_fn,
253 };
254 
255 static clib_error_t *
257 {
259 
260  frm->vlib_main = vm;
261  frm->vnet_main = vnet_get_main();
262 
263  return 0;
264 }
265 
flow_report_main_t flow_report_main
Definition: flow_report.h:90
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:942
always_inline uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Definition: node_funcs.h:410
vlib_node_registration_t flow_report_process_node
(constructor) VLIB_REGISTER_NODE (flow_report_process_node)
Definition: flow_report.c:163
a
Definition: bitmap.h:393
#define UNFORMAT_END_OF_INPUT
Definition: format.h:142
u32 index
Definition: node.h:203
void * opaque
Definition: flow_report.h:65
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:519
ip4_address_t src_address
Definition: flow_report.h:79
always_inline void vlib_process_signal_event(vlib_main_t *vm, uword node_index, uword type_opaque, uword data)
Definition: node_funcs.h:789
always_inline uword vlib_process_suspend(vlib_main_t *vm, f64 dt)
Definition: node_funcs.h:326
format_function_t format_ip4_address
Definition: format.h:71
always_inline void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:184
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
ip4_address_t ipfix_collector
Definition: flow_report.h:78
vlib_node_registration_t ip4_lookup_node
(constructor) VLIB_REGISTER_NODE (ip4_lookup_node)
Definition: ip4_forward.c:1360
always_inline uword unformat_check_input(unformat_input_t *i)
Definition: format.h:168
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:77
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:109
vnet_flow_rewrite_callback_t * rewrite_callback
Definition: flow_report.h:98
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
always_inline void vlib_node_set_state(vlib_main_t *vm, u32 node_index, vlib_node_state_t new_state)
Definition: node_funcs.h:100
#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:94
always_inline void * vlib_frame_vector_args(vlib_frame_t *f)
Definition: node_funcs.h:202
flow_report_t * reports
Definition: flow_report.h:75
unformat_function_t unformat_ip4_address
Definition: format.h:68
#define VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES
Definition: buffer.h:297
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:81
u8 * rewrite
Definition: flow_report.h:55
always_inline f64 vlib_process_wait_for_event_or_clock(vlib_main_t *vm, f64 dt)
Definition: node_funcs.h:551
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:191
vnet_main_t * vnet_main
Definition: flow_report.h:87
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:538
int send_template_packet(flow_report_main_t *frm, flow_report_t *fr, u32 *buffer_indexp)
Definition: flow_report.c:21
u32 sequence_number
Definition: flow_report.h:56
u16 n_vectors
Definition: node.h:307
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:298
#define clib_memcpy(a, b, c)
Definition: string.h:63
#define VLIB_BUFFER_TOTAL_LENGTH_VALID
Definition: buffer.h:95
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:150
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
vlib_main_t * vlib_main
Definition: flow_report.h:86
#define vnet_buffer(b)
Definition: buffer.h:300
#define vec_delete(V, N, M)
Delete N elements starting at element M.
Definition: vec.h:743
u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer.c:770
vnet_flow_data_callback_t * flow_data_callback
Definition: flow_report.h:71
u64 uword
Definition: types.h:112
vlib_node_t * vlib_get_node_by_name(vlib_main_t *vm, u8 *name)
Definition: node.c:44
Definition: defs.h:46
int update_rewrite
Definition: flow_report.h:59
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:140
unsigned char u8
Definition: types.h:56
f64 last_template_sent
Definition: flow_report.h:58
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:211
vnet_flow_data_callback_t * flow_data_callback
Definition: flow_report.h:97
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:140
vnet_flow_rewrite_callback_t * rewrite_callback
Definition: flow_report.h:68
u8 data[0]
Packet data.
Definition: buffer.h:150
#define vec_foreach(var, vec)
Vector iterator.
always_inline f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:182
#define clib_error_return(e, args...)
Definition: error.h:112
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:184
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:84
always_inline 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:256
Definition: defs.h:45
int vnet_flow_report_add_del(flow_report_main_t *frm, vnet_flow_report_add_del_args_t *a)
Definition: flow_report.c:169