In-band OAM active probe (VPP-471)
[vpp.git] / src / plugins / ioam / udp-ping / udp_ping_export.c
1 /*
2  * Copyright (c) 2016 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 #include <vnet/flow/flow_report.h>
17 #include <ioam/analyse/ioam_summary_export.h>
18 #include <vnet/api_errno.h>
19 #include <ioam/udp-ping/udp_ping.h>
20
21 #define UDP_PING_EXPORT_RECORD_SIZE 400
22
23 static u8 *
24 udp_ping_template_rewrite (flow_report_main_t * frm, flow_report_t * fr,
25                            ip4_address_t * collector_address,
26                            ip4_address_t * src_address, u16 collector_port)
27 {
28   return ioam_template_rewrite (frm, fr, collector_address,
29                                 src_address, collector_port);
30 }
31
32 static vlib_frame_t *
33 udp_ping_send_flows (flow_report_main_t * frm, flow_report_t * fr,
34                      vlib_frame_t * f, u32 * to_next, u32 node_index)
35 {
36   vlib_buffer_t *b0 = NULL;
37   u32 next_offset = 0;
38   u32 bi0 = ~0;
39   int i, j;
40   ip4_ipfix_template_packet_t *tp;
41   ipfix_message_header_t *h;
42   ipfix_set_header_t *s = NULL;
43   ip4_header_t *ip;
44   udp_header_t *udp;
45   u32 records_this_buffer;
46   u16 new_l0, old_l0;
47   ip_csum_t sum0;
48   vlib_main_t *vm = frm->vlib_main;
49   flow_report_stream_t *stream;
50   udp_ping_flow_data *stats;
51   ip46_udp_ping_flow *ip46_flow;
52   u16 src_port, dst_port;
53   u16 data_len;
54
55   stream = &frm->streams[fr->stream_index];
56   data_len = vec_len (udp_ping_main.ip46_flow);
57
58   for (i = 0; i < data_len; i++)
59     {
60       if (pool_is_free_index (udp_ping_main.ip46_flow, i))
61         continue;
62
63       ip46_flow = pool_elt_at_index (udp_ping_main.ip46_flow, i);
64       j = 0;
65       for (src_port = ip46_flow->udp_data.start_src_port;
66            src_port <= ip46_flow->udp_data.end_src_port; src_port++)
67         {
68           for (dst_port = ip46_flow->udp_data.start_dst_port;
69                dst_port <= ip46_flow->udp_data.end_dst_port; dst_port++, j++)
70             {
71               stats = ip46_flow->udp_data.stats + j;
72               if (PREDICT_FALSE (b0 == NULL))
73                 {
74                   if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
75                     break;
76
77
78                   b0 = vlib_get_buffer (vm, bi0);
79                   memcpy (b0->data, fr->rewrite, vec_len (fr->rewrite));
80                   b0->current_data = 0;
81                   b0->current_length = vec_len (fr->rewrite);
82                   b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
83                   vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
84                   vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
85
86                   tp = vlib_buffer_get_current (b0);
87                   ip = &tp->ip4;
88                   udp = &tp->udp;
89                   h = &tp->ipfix.h;
90                   s = &tp->ipfix.s;
91
92                   /* FIXUP: message header export_time */
93                   h->export_time = clib_host_to_net_u32 (((u32) time (NULL)));
94
95                   /* FIXUP: message header sequence_number */
96                   h->sequence_number = stream->sequence_number++;
97                   h->sequence_number =
98                     clib_host_to_net_u32 (h->sequence_number);
99                   next_offset = (u32) (((u8 *) (s + 1)) - (u8 *) tp);
100                   records_this_buffer = 0;
101                 }
102
103               next_offset = ioam_analyse_add_ipfix_record (fr,
104                                                            &stats->analyse_data,
105                                                            b0, next_offset,
106                                                            &ip46_flow->
107                                                            src.ip6,
108                                                            &ip46_flow->
109                                                            dst.ip6, src_port,
110                                                            dst_port);
111
112               //u32 pak_sent = clib_host_to_net_u32(stats->pak_sent);
113               //memcpy (b0->data + next_offset, &pak_sent, sizeof(u32));
114               //next_offset += sizeof(u32);
115
116               records_this_buffer++;
117
118               /* Flush data if packet len is about to reach path mtu */
119               if (next_offset > (frm->path_mtu - UDP_PING_EXPORT_RECORD_SIZE))
120                 {
121                   b0->current_length = next_offset;
122                   b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
123                   tp = vlib_buffer_get_current (b0);
124                   ip = (ip4_header_t *) & tp->ip4;
125                   udp = (udp_header_t *) (ip + 1);
126                   h = &tp->ipfix.h;
127                   s = &tp->ipfix.s;
128
129                   s->set_id_length =
130                     ipfix_set_id_length (IOAM_FLOW_TEMPLATE_ID,
131                                          next_offset - (sizeof (*ip) +
132                                                         sizeof (*udp) +
133                                                         sizeof (*h)));
134                   h->version_length =
135                     version_length (next_offset -
136                                     (sizeof (*ip) + sizeof (*udp)));
137
138                   sum0 = ip->checksum;
139                   old_l0 = ip->length;
140                   new_l0 = clib_host_to_net_u16 ((u16) next_offset);
141                   sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
142                                          length /* changed member */ );
143
144                   ip->checksum = ip_csum_fold (sum0);
145                   ip->length = new_l0;
146                   udp->length =
147                     clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
148
149                   udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
150                   if (udp->checksum == 0)
151                     udp->checksum = 0xffff;
152
153                   ASSERT (ip->checksum == ip4_header_checksum (ip));
154
155                   to_next[0] = bi0;
156                   f->n_vectors++;
157                   to_next++;
158
159                   if (f->n_vectors == VLIB_FRAME_SIZE)
160                     {
161                       vlib_put_frame_to_node (vm, node_index, f);
162                       f = vlib_get_frame_to_node (vm, node_index);
163                       f->n_vectors = 0;
164                       to_next = vlib_frame_vector_args (f);
165                     }
166                   b0 = 0;
167                   bi0 = ~0;
168                 }
169             }
170         }
171     }
172
173   if (b0)
174     {
175       b0->current_length = next_offset;
176       b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
177       tp = vlib_buffer_get_current (b0);
178       ip = (ip4_header_t *) & tp->ip4;
179       udp = (udp_header_t *) (ip + 1);
180       h = &tp->ipfix.h;
181       s = &tp->ipfix.s;
182
183       s->set_id_length = ipfix_set_id_length (IOAM_FLOW_TEMPLATE_ID,
184                                               next_offset - (sizeof (*ip) +
185                                                              sizeof (*udp) +
186                                                              sizeof (*h)));
187       h->version_length =
188         version_length (next_offset - (sizeof (*ip) + sizeof (*udp)));
189
190       sum0 = ip->checksum;
191       old_l0 = ip->length;
192       new_l0 = clib_host_to_net_u16 ((u16) next_offset);
193       sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
194                              length /* changed member */ );
195
196       ip->checksum = ip_csum_fold (sum0);
197       ip->length = new_l0;
198       udp->length = clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
199
200       udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
201       if (udp->checksum == 0)
202         udp->checksum = 0xffff;
203
204       ASSERT (ip->checksum == ip4_header_checksum (ip));
205
206       to_next[0] = bi0;
207       f->n_vectors++;
208       to_next++;
209
210       if (f->n_vectors == VLIB_FRAME_SIZE)
211         {
212           vlib_put_frame_to_node (vm, node_index, f);
213           f = vlib_get_frame_to_node (vm, node_index);
214           f->n_vectors = 0;
215           to_next = vlib_frame_vector_args (f);
216         }
217       b0 = 0;
218       bi0 = ~0;
219     }
220   return f;
221 }
222
223 clib_error_t *
224 udp_ping_flow_create (u8 del)
225 {
226   vnet_flow_report_add_del_args_t args;
227   int rv;
228   u32 domain_id = 0;
229   flow_report_main_t *frm = &flow_report_main;
230
231   args.rewrite_callback = udp_ping_template_rewrite;
232   args.flow_data_callback = udp_ping_send_flows;
233   del ? (args.is_add = 0) : (args.is_add = 1);
234   args.domain_id = domain_id;
235
236   rv = vnet_flow_report_add_del (frm, &args);
237
238   switch (rv)
239     {
240     case 0:
241       break;
242     case VNET_API_ERROR_NO_SUCH_ENTRY:
243       return clib_error_return (0, "registration not found...");
244     default:
245       return clib_error_return (0, "vnet_flow_report_add_del returned %d",
246                                 rv);
247     }
248
249   return 0;
250 }
251
252 static clib_error_t *
253 set_udp_ping_export_command_fn (vlib_main_t * vm, unformat_input_t * input,
254                                 vlib_cli_command_t * cmd)
255 {
256   //int rv;
257   int is_add = 1;
258
259   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
260     {
261       if (unformat (input, "export-ipfix"))
262         is_add = 1;
263       else if (unformat (input, "disable"))
264         is_add = 0;
265       else
266         break;
267     }
268
269   if (is_add)
270     (void) udp_ping_flow_create (0);
271   else
272     (void) udp_ping_flow_create (1);
273
274   return 0;
275 }
276
277 /* *INDENT-OFF* */
278 VLIB_CLI_COMMAND (set_udp_ping_export_command, static) = {
279     .path = "set udp-ping export-ipfix",
280     .short_help = "set udp-ping export-ipfix [disable]",
281     .function = set_udp_ping_export_command_fn,
282 };
283 /* *INDENT-ON* */
284
285 clib_error_t *
286 udp_ping_flow_report_init (vlib_main_t * vm)
287 {
288   clib_error_t *error;
289
290   if ((error = vlib_call_init_function (vm, flow_report_init)))
291     return error;
292
293   return 0;
294 }
295
296 VLIB_INIT_FUNCTION (udp_ping_flow_report_init);
297
298 /*
299  * fd.io coding-style-patch-verification: ON
300  *
301  * Local Variables:
302  * eval: (c-set-style "gnu")
303  * End:
304  */