ipfix-export: refactor params to the callback fns
[vpp.git] / src / vnet / ipfix-export / flow_report.h
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 #ifndef __included_vnet_flow_report_h__
16 #define __included_vnet_flow_report_h__
17
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 #include <vnet/ethernet/ethernet.h>
21 #include <vnet/ethernet/packet.h>
22 #include <vnet/ip/ip_packet.h>
23 #include <vnet/ip/ip_types.h>
24 #include <vnet/ip/ip4_packet.h>
25 #include <vnet/ip/ip6_packet.h>
26 #include <vnet/udp/udp_packet.h>
27 #include <vlib/cli.h>
28 #include <vppinfra/error.h>
29 #include <vppinfra/hash.h>
30 #include <vppinfra/cache.h>
31
32 #include <vnet/ipfix-export/ipfix_packet.h>
33
34 /* ipfix field definitions for a particular report */
35 typedef struct
36 {
37   u32 info_element;
38   u32 size;
39 } ipfix_report_element_t;
40
41 /* Used to build the rewrite */
42 typedef struct
43 {
44   ip4_header_t ip4;
45   udp_header_t udp;
46   ipfix_template_packet_t ipfix;
47 } ip4_ipfix_template_packet_t;
48
49 struct flow_report_main;
50 struct flow_report;
51 struct ipfix_exporter;
52
53 typedef vlib_frame_t *(vnet_flow_data_callback_t) (
54   struct flow_report_main *frm, struct ipfix_exporter *exp,
55   struct flow_report *, vlib_frame_t *, u32 *, u32);
56
57 typedef u8 *(vnet_flow_rewrite_callback_t) (struct ipfix_exporter *exp,
58                                             struct flow_report *,
59                                             u16, ipfix_report_element_t *elts,
60                                             u32 n_elts, u32 *stream_index);
61
62 u8 *vnet_flow_rewrite_generic_callback (struct ipfix_exporter *exp,
63                                         struct flow_report *, u16,
64                                         ipfix_report_element_t *elts,
65                                         u32 n_elts, u32 *stream_index);
66
67 typedef union
68 {
69   void *as_ptr;
70   uword as_uword;
71 } opaque_t;
72
73 /*
74  * A stream represents an IPFIX session to a destination. We can have
75  * multiple streams to the same destination, but each one has its own
76  * domain and source port. A stream has a sequence number for that
77  * session. A stream may contain multiple templates (i.e multiple for
78  * reports) and each stream also has its own template space.
79  *
80  * A stream has per thread state so that data packets can be built
81  * and send on multiple threads at the same time.
82  */
83 typedef struct
84 {
85   u32 domain_id;
86   u32 sequence_number;
87   u16 src_port;
88   u16 n_reports;
89   u16 next_template_no;
90 } flow_report_stream_t;
91
92 /*
93  * For each flow_report we want to be able to build buffers/frames per thread.
94  */
95 typedef struct
96 {
97   vlib_buffer_t *buffer;
98   vlib_frame_t *frame;
99   u16 next_data_offset;
100   /*
101    * We need this per stream as the IPFIX sequence number is the count of
102    * data record sent, not the count of packets with data records sent.
103    * See RFC 7011, Sec 3.1
104    */
105   u8 n_data_records;
106 } flow_report_per_thread_t;
107
108 /*
109  * A flow report represents a group of fields that are to be exported.
110  * Each flow_report has an associated template that is generated when
111  * the flow_report is added. Each flow_report is associated with a
112  * stream, and multiple flow_reports can use the same stream. When
113  * adding a flow_report the keys for the stream are the domain_id
114  * and the source_port.
115  */
116 typedef struct flow_report
117 {
118   /* ipfix rewrite, set by callback */
119   u8 *rewrite;
120   u16 template_id;
121   int data_record_size;
122   flow_report_per_thread_t *per_thread_data;
123   u32 stream_index;
124   f64 last_template_sent;
125   int update_rewrite;
126
127   /* Bitmap of fields to send */
128   uword *fields_to_send;
129
130   /* Opaque data */
131   opaque_t opaque;
132
133   /* build-the-template-packet rewrite callback */
134   vnet_flow_rewrite_callback_t *rewrite_callback;
135   ipfix_report_element_t *report_elements;
136   u32 n_report_elements;
137   u32 *stream_indexp;
138
139   /* Send-flow-data callback */
140   vnet_flow_data_callback_t *flow_data_callback;
141 } flow_report_t;
142
143 /*
144  * The maximum number of ipfix exporters we can have at once
145  */
146 #define IPFIX_EXPORTERS_MAX 5
147
148 /*
149  * We support multiple exporters. Each one has its own configured
150  * destination, and its own set of reports and streams.
151  */
152 typedef struct ipfix_exporter
153 {
154   flow_report_t *reports;
155   flow_report_stream_t *streams;
156
157   /* ipfix collector ip address, port, our ip address, fib index */
158   ip4_address_t ipfix_collector;
159   u16 collector_port;
160   ip4_address_t src_address;
161   u32 fib_index;
162
163   /* Path MTU */
164   u32 path_mtu;
165
166   /* time interval in seconds after which to resend templates */
167   u32 template_interval;
168
169   /* UDP checksum calculation enable flag */
170   u8 udp_checksum;
171
172   /*
173    * The amount of data needed for all the headers, prior to the first
174    * flowset (template or data or ...) This is mostly dependent on the
175    * L3 and L4 protocols in use.
176    */
177   u32 all_headers_size;
178 } ipfix_exporter_t;
179
180 typedef struct flow_report_main
181 {
182   /*
183    * A pool of the exporters. Entry 0 is always there for backwards
184    * compatability reasons. Entries 1 and above have to be created by
185    * the users.
186    */
187   ipfix_exporter_t *exporters;
188
189   /* time scale transform. Joy. */
190   u32 unix_time_0;
191   f64 vlib_time_0;
192
193   /* convenience variables */
194   vlib_main_t *vlib_main;
195   vnet_main_t *vnet_main;
196
197   u16 msg_id_base;
198 } flow_report_main_t;
199
200 extern flow_report_main_t flow_report_main;
201
202 extern vlib_node_registration_t flow_report_process_node;
203
204 typedef struct
205 {
206   vnet_flow_data_callback_t *flow_data_callback;
207   vnet_flow_rewrite_callback_t *rewrite_callback;
208   ipfix_report_element_t *report_elements;
209   u32 n_report_elements;
210   opaque_t opaque;
211   int is_add;
212   u32 domain_id;
213   u16 src_port;
214   u32 *stream_indexp;
215   /*
216    * When adding a flow report, the index of the flow report is stored
217    * here on success.
218    */
219   u32 flow_report_index;
220 } vnet_flow_report_add_del_args_t;
221
222 int vnet_flow_report_add_del (ipfix_exporter_t *exp,
223                               vnet_flow_report_add_del_args_t *a,
224                               u16 *template_id);
225
226 clib_error_t *flow_report_add_del_error_to_clib_error (int error);
227
228 void vnet_flow_reports_reset (ipfix_exporter_t *exp);
229
230 void vnet_stream_reset (ipfix_exporter_t *exp, u32 stream_index);
231
232 int vnet_stream_change (ipfix_exporter_t *exp, u32 old_domain_id,
233                         u16 old_src_port, u32 new_domain_id, u16 new_src_port);
234
235 /*
236  * Search all the exporters for one that has a matching destination address.
237  */
238 ipfix_exporter_t *vnet_ipfix_exporter_lookup (ip4_address_t *ipfix_collector);
239
240 /*
241  * Get the currently in use buffer for the given stream on the given core.
242  * If there is no current buffer then allocate a new one and return that.
243  * This is the buffer that data records should be written into. The offset
244  * currently in use is stored in the per-thread data for the stream and
245  * should be updated as new records are written in.
246  */
247 vlib_buffer_t *vnet_ipfix_exp_get_buffer (vlib_main_t *vm,
248                                           ipfix_exporter_t *exp,
249                                           flow_report_t *fr, u32 thread_index);
250
251 /*
252  * Send the provided buffer. At this stage the buffer should be populated
253  * with data records, with the offset in use stored in the stream per thread
254  * data. This func will fix up all the headers and then send the buffer.
255  */
256 void vnet_ipfix_exp_send_buffer (vlib_main_t *vm, ipfix_exporter_t *exp,
257                                  flow_report_t *fr,
258                                  flow_report_stream_t *stream,
259                                  u32 thread_index, vlib_buffer_t *b0);
260
261 #endif /* __included_vnet_flow_report_h__ */
262
263 /*
264  * fd.io coding-style-patch-verification: ON
265  *
266  * Local Variables:
267  * eval: (c-set-style "gnu")
268  * End:
269  */