2 * Copyright (c) 2017 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vlib/vlib.h>
17 #include <vnet/ip/ip6_packet.h>
18 #include <vnet/udp/udp_local.h>
19 #include <ioam/analyse/ioam_summary_export.h>
20 #include <ioam/analyse/ip6/ip6_ioam_analyse.h>
23 ioam_template_rewrite (flow_report_main_t * frm, flow_report_t * fr,
24 ip4_address_t * collector_address,
25 ip4_address_t * src_address, u16 collector_port,
26 ipfix_report_element_t * elts,
27 u32 n_elts, u32 * stream_index)
31 ipfix_message_header_t *h;
32 ipfix_set_header_t *s;
33 ipfix_template_header_t *t;
34 ipfix_field_specifier_t *f;
35 ipfix_field_specifier_t *first_field;
37 ip4_ipfix_template_packet_t *tp;
40 flow_report_stream_t *stream;
42 stream = &frm->streams[fr->stream_index];
44 /* Determine field count */
45 #define _(field,mask,item,length) \
48 fr->fields_to_send = clib_bitmap_set (fr->fields_to_send, \
53 foreach_ioam_ipfix_field;
56 /* Add Src address, dest address, src port, dest port
57 * path map, number of paths manually */
60 /* allocate rewrite space */
61 vec_validate_aligned (rewrite,
62 sizeof (ip4_ipfix_template_packet_t)
63 + field_count * sizeof (ipfix_field_specifier_t) - 1,
64 CLIB_CACHE_LINE_BYTES);
66 tp = (ip4_ipfix_template_packet_t *) rewrite;
67 ip = (ip4_header_t *) & tp->ip4;
68 udp = (udp_header_t *) (ip + 1);
69 h = (ipfix_message_header_t *) (udp + 1);
70 s = (ipfix_set_header_t *) (h + 1);
71 t = (ipfix_template_header_t *) (s + 1);
72 first_field = f = (ipfix_field_specifier_t *) (t + 1);
74 ip->ip_version_and_header_length = 0x45;
76 ip->protocol = IP_PROTOCOL_UDP;
77 ip->src_address.as_u32 = src_address->as_u32;
78 ip->dst_address.as_u32 = collector_address->as_u32;
79 udp->src_port = clib_host_to_net_u16 (collector_port);
80 udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_ipfix);
81 udp->length = clib_host_to_net_u16 (vec_len (rewrite) - sizeof (*ip));
83 h->domain_id = clib_host_to_net_u32 (stream->domain_id); //fr->domain_id);
85 /* Add Src address, dest address, src port, dest port
86 * path map, number of paths manually */
87 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
89 sizeof (ip6_address_t));
92 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
93 destinationIPv6Address,
94 sizeof (ip6_address_t));
97 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
98 sourceTransportPort, 2);
101 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
102 destinationTransportPort, 2);
105 #define _(field,mask,item,length) \
107 f->e_id_length = ipfix_e_id_length (0 /* enterprise */, \
111 foreach_ioam_ipfix_field;
114 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
115 ioamNumberOfPaths, 2);
118 /* Add ioamPathMap manually */
119 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
121 (sizeof (ioam_path) +
122 (sizeof (ioam_path_map_t) *
123 IOAM_TRACE_MAX_NODES)));
126 /* Back to the template packet... */
127 ip = (ip4_header_t *) & tp->ip4;
128 udp = (udp_header_t *) (ip + 1);
130 ASSERT (f - first_field);
131 /* Field count in this template */
132 t->id_count = ipfix_id_count (IOAM_FLOW_TEMPLATE_ID, f - first_field);
134 /* set length in octets */
136 ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s);
138 /* message length in octets */
139 h->version_length = version_length ((u8 *) f - (u8 *) h);
141 ip->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip);
142 ip->checksum = ip4_header_checksum (ip);
148 ioam_analyse_add_ipfix_record (flow_report_t * fr,
149 ioam_analyser_data_t * record,
150 vlib_buffer_t * b0, u16 offset,
151 ip6_address_t * src, ip6_address_t * dst,
152 u16 src_port, u16 dst_port)
154 clib_spinlock_lock (&record->writer_lock);
160 u16 num_paths_offset;
163 /* Add IPv6 source address manually */
164 memcpy (b0->data + offset, &src->as_u64[0], sizeof (u64));
165 offset += sizeof (u64);
166 memcpy (b0->data + offset, &src->as_u64[1], sizeof (u64));
167 offset += sizeof (u64);
169 /* Add IPv6 destination address manually */
170 memcpy (b0->data + offset, &dst->as_u64[0], sizeof (u64));
171 offset += sizeof (u64);
172 memcpy (b0->data + offset, &dst->as_u64[1], sizeof (u64));
173 offset += sizeof (u64);
175 /* Add source port manually */
176 tmp = clib_host_to_net_u16 (src_port);
177 memcpy (b0->data + offset, &tmp, sizeof (u16));
178 offset += sizeof (u16);
180 /* Add dest port manually */
181 tmp = clib_host_to_net_u16 (dst_port);
182 memcpy (b0->data + offset, &tmp, sizeof (u16));
183 offset += sizeof (u16);
185 #define _(field,mask,item,length) \
186 if (clib_bitmap_get (fr->fields_to_send, field_index)) \
188 /* Expect only 4 bytes */ \
190 tmp = clib_host_to_net_u32((u32)record->field - (u32)record->chached_data_list->field);\
191 memcpy (b0->data + offset, &tmp, length); \
195 foreach_ioam_ipfix_field;
198 /* Store num_paths_offset here and update later */
199 num_paths_offset = offset;
200 offset += sizeof (u16);
202 /* Add ioamPathMap manually */
203 for (i = 0; i < IOAM_MAX_PATHS_PER_FLOW; i++)
205 ioam_analyse_trace_record *trace = record->trace_data.path_data + i;
206 ioam_analyse_trace_record *trace_cached =
207 record->chached_data_list->trace_data.path_data + i;
208 ioam_path *path = (ioam_path *) (b0->data + offset);
214 path->num_nodes = trace->num_nodes;
216 path->trace_type = trace->trace_type;
217 if (0 < (trace->pkt_counter - trace_cached->pkt_counter))
219 u64 new_sum = trace->mean_delay * record->seqno_data.rx_packets;
221 trace_cached->mean_delay *
222 record->chached_data_list->seqno_data.rx_packets;
224 (u32) ((new_sum - old_sum) / (trace->pkt_counter -
225 trace_cached->pkt_counter));
226 path->mean_delay = clib_host_to_net_u32 (path->mean_delay);
229 path->mean_delay = 0;
231 path->bytes_counter =
232 trace->bytes_counter - trace_cached->bytes_counter;
233 path->bytes_counter = clib_host_to_net_u32 (path->bytes_counter);
235 path->pkt_counter = trace->pkt_counter - trace_cached->pkt_counter;
236 path->pkt_counter = clib_host_to_net_u32 (path->pkt_counter);
237 offset += sizeof (ioam_path);
239 for (j = 0; j < trace->num_nodes; j++)
241 path->path[j].node_id =
242 clib_host_to_net_u32 (trace->path[j].node_id);
243 path->path[j].ingress_if =
244 clib_host_to_net_u16 (trace->path[j].ingress_if);
245 path->path[j].egress_if =
246 clib_host_to_net_u16 (trace->path[j].egress_if);
247 path->path[j].state_up = trace->path[j].state_up;
250 //offset += (sizeof(ioam_path_map_t) * trace->num_nodes);
251 offset += (sizeof (ioam_path_map_t) * IOAM_TRACE_MAX_NODES); //FIXME
255 num_paths = clib_host_to_net_u16 (num_paths);
256 memcpy (b0->data + num_paths_offset, &num_paths, sizeof (u16));
259 *(record->chached_data_list) = *record;
260 record->chached_data_list->chached_data_list = NULL;
262 clib_spinlock_unlock (&record->writer_lock);
267 ioam_send_flows (flow_report_main_t * frm, flow_report_t * fr,
268 vlib_frame_t * f, u32 * to_next, u32 node_index)
270 vlib_buffer_t *b0 = NULL;
274 ip4_ipfix_template_packet_t *tp;
275 ipfix_message_header_t *h;
276 ipfix_set_header_t *s = NULL;
279 u32 records_this_buffer;
282 vlib_main_t *vm = frm->vlib_main;
284 ioam_analyser_data_t *record = NULL;
285 flow_report_stream_t *stream;
286 ioam_analyser_data_t *aggregated_data;
289 stream = &frm->streams[fr->stream_index];
291 clib_memset (&temp, 0, sizeof (ip6_address_t));
293 aggregated_data = ioam_analyser_main.aggregated_data;
294 data_len = vec_len (aggregated_data);
296 vec_foreach_index (i, aggregated_data)
299 record = aggregated_data + i;
301 /* Flush if last entry */
302 if (i == (data_len - 1))
305 if (!record->is_free)
308 if (PREDICT_FALSE (b0 == NULL))
310 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
313 b0 = vlib_get_buffer (vm, bi0);
314 memcpy (b0->data, fr->rewrite, vec_len (fr->rewrite));
315 b0->current_data = 0;
316 b0->current_length = vec_len (fr->rewrite);
317 b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
318 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
319 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
321 tp = vlib_buffer_get_current (b0);
326 /* FIXUP: message header export_time */
327 h->export_time = clib_host_to_net_u32 (((u32) time (NULL)));
329 /* FIXUP: message header sequence_number */
330 h->sequence_number = stream->sequence_number++;
331 h->sequence_number = clib_host_to_net_u32 (h->sequence_number);
332 next_offset = (u32) (((u8 *) (s + 1)) - (u8 *) tp);
333 records_this_buffer = 0;
336 next_offset = ioam_analyse_add_ipfix_record (fr, record,
339 records_this_buffer++;
341 /* Flush data if packet len is about to reach path mtu */
342 if (next_offset > (frm->path_mtu - 250))
346 if (PREDICT_FALSE (flush && b0))
348 s->set_id_length = ipfix_set_id_length (IOAM_FLOW_TEMPLATE_ID,
349 next_offset - (sizeof (*ip) +
352 b0->current_length = next_offset;
353 b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
354 tp = vlib_buffer_get_current (b0);
355 ip = (ip4_header_t *) & tp->ip4;
356 udp = (udp_header_t *) (ip + 1);
360 new_l0 = clib_host_to_net_u16 ((u16) next_offset);
361 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
362 length /* changed member */ );
364 ip->checksum = ip_csum_fold (sum0);
367 clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
369 if (frm->udp_checksum)
371 /* RFC 7011 section 10.3.2. */
372 udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
373 if (udp->checksum == 0)
374 udp->checksum = 0xffff;
381 if (f->n_vectors == VLIB_FRAME_SIZE)
383 vlib_put_frame_to_node (vm, node_index, f);
384 f = vlib_get_frame_to_node (vm, node_index);
386 to_next = vlib_frame_vector_args (f);
397 ioam_flow_create (u8 del)
399 vnet_flow_report_add_del_args_t args;
402 flow_report_main_t *frm = &flow_report_main;
405 clib_memset (&args, 0, sizeof (args));
406 args.rewrite_callback = ioam_template_rewrite;
407 args.flow_data_callback = ioam_send_flows;
408 del ? (args.is_add = 0) : (args.is_add = 1);
409 args.domain_id = domain_id;
411 rv = vnet_flow_report_add_del (frm, &args, &template_id);
417 case VNET_API_ERROR_NO_SUCH_ENTRY:
418 return clib_error_return (0, "registration not found...");
420 return clib_error_return (0, "vnet_flow_report_add_del returned %d",
428 ioam_flow_report_init (vlib_main_t * vm)
434 VLIB_INIT_FUNCTION (ioam_flow_report_init) =
436 .runs_after = VLIB_INITS("flow_report_init"),
441 * fd.io coding-style-patch-verification: ON
444 * eval: (c-set-style "gnu")