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 <ioam/analyse/ioam_summary_export.h>
19 #include <ioam/analyse/ip6/ip6_ioam_analyse.h>
22 ioam_template_rewrite (flow_report_main_t * frm, flow_report_t * fr,
23 ip4_address_t * collector_address,
24 ip4_address_t * src_address, u16 collector_port)
28 ipfix_message_header_t *h;
29 ipfix_set_header_t *s;
30 ipfix_template_header_t *t;
31 ipfix_field_specifier_t *f;
32 ipfix_field_specifier_t *first_field;
34 ip4_ipfix_template_packet_t *tp;
37 flow_report_stream_t *stream;
39 stream = &frm->streams[fr->stream_index];
41 /* Determine field count */
42 #define _(field,mask,item,length) \
45 fr->fields_to_send = clib_bitmap_set (fr->fields_to_send, \
50 foreach_ioam_ipfix_field;
53 /* Add Src address, dest address, src port, dest port
54 * path map, number of paths manually */
57 /* allocate rewrite space */
58 vec_validate_aligned (rewrite,
59 sizeof (ip4_ipfix_template_packet_t)
60 + field_count * sizeof (ipfix_field_specifier_t) - 1,
61 CLIB_CACHE_LINE_BYTES);
63 tp = (ip4_ipfix_template_packet_t *) rewrite;
64 ip = (ip4_header_t *) & tp->ip4;
65 udp = (udp_header_t *) (ip + 1);
66 h = (ipfix_message_header_t *) (udp + 1);
67 s = (ipfix_set_header_t *) (h + 1);
68 t = (ipfix_template_header_t *) (s + 1);
69 first_field = f = (ipfix_field_specifier_t *) (t + 1);
71 ip->ip_version_and_header_length = 0x45;
73 ip->protocol = IP_PROTOCOL_UDP;
74 ip->src_address.as_u32 = src_address->as_u32;
75 ip->dst_address.as_u32 = collector_address->as_u32;
76 udp->src_port = clib_host_to_net_u16 (collector_port);
77 udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_ipfix);
78 udp->length = clib_host_to_net_u16 (vec_len (rewrite) - sizeof (*ip));
80 h->domain_id = clib_host_to_net_u32 (stream->domain_id); //fr->domain_id);
82 /* Add Src address, dest address, src port, dest port
83 * path map, number of paths manually */
84 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
86 sizeof (ip6_address_t));
89 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
90 destinationIPv6Address,
91 sizeof (ip6_address_t));
94 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
95 sourceTransportPort, 2);
98 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
99 destinationTransportPort, 2);
102 #define _(field,mask,item,length) \
104 f->e_id_length = ipfix_e_id_length (0 /* enterprise */, \
108 foreach_ioam_ipfix_field;
111 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
112 ioamNumberOfPaths, 2);
115 /* Add ioamPathMap manually */
116 f->e_id_length = ipfix_e_id_length (0 /* enterprise */ ,
117 ioamPathMap, (1 * sizeof (ioam_path)));
120 /* Back to the template packet... */
121 ip = (ip4_header_t *) & tp->ip4;
122 udp = (udp_header_t *) (ip + 1);
124 ASSERT (f - first_field);
125 /* Field count in this template */
126 t->id_count = ipfix_id_count (IOAM_FLOW_TEMPLATE_ID, f - first_field);
128 /* set length in octets */
130 ipfix_set_id_length (2 /* set_id */ , (u8 *) f - (u8 *) s);
132 /* message length in octets */
133 h->version_length = version_length ((u8 *) f - (u8 *) h);
135 ip->length = clib_host_to_net_u16 ((u8 *) f - (u8 *) ip);
136 ip->checksum = ip4_header_checksum (ip);
142 ioam_analyse_add_ipfix_record (flow_report_t * fr,
143 ioam_analyser_data_t * record,
144 vlib_buffer_t * b0, u16 offset,
145 ip6_address_t * src, ip6_address_t * dst,
146 u16 src_port, u16 dst_port)
148 while (__sync_lock_test_and_set (record->writer_lock, 1))
155 u16 num_paths_offset;
158 /* Add IPv6 source address manually */
159 memcpy (b0->data + offset, &src->as_u64[0], sizeof (u64));
160 offset += sizeof (u64);
161 memcpy (b0->data + offset, &src->as_u64[1], sizeof (u64));
162 offset += sizeof (u64);
164 /* Add IPv6 destination address manually */
165 memcpy (b0->data + offset, &dst->as_u64[0], sizeof (u64));
166 offset += sizeof (u64);
167 memcpy (b0->data + offset, &dst->as_u64[1], sizeof (u64));
168 offset += sizeof (u64);
170 /* Add source port manually */
171 tmp = clib_host_to_net_u16 (src_port);
172 memcpy (b0->data + offset, &tmp, sizeof (u16));
173 offset += sizeof (u16);
175 /* Add dest port manually */
176 tmp = clib_host_to_net_u16 (dst_port);
177 memcpy (b0->data + offset, &tmp, sizeof (u16));
178 offset += sizeof (u16);
180 #define _(field,mask,item,length) \
181 if (clib_bitmap_get (fr->fields_to_send, field_index)) \
183 /* Expect only 4 bytes */ \
185 tmp = clib_host_to_net_u32((u32)record->field - (u32)record->chached_data_list->field);\
186 memcpy (b0->data + offset, &tmp, length); \
190 foreach_ioam_ipfix_field;
193 /* Store num_paths_offset here and update later */
194 num_paths_offset = offset;
195 offset += sizeof (u16);
197 /* Add ioamPathMap manually */
198 for (i = 0; i < IOAM_MAX_PATHS_PER_FLOW; i++)
200 ioam_analyse_trace_record *trace = record->trace_data.path_data + i;
201 ioam_analyse_trace_record *trace_cached =
202 record->chached_data_list->trace_data.path_data + i;
203 ioam_path *path = (ioam_path *) (b0->data + offset);
209 path->num_nodes = trace->num_nodes;
211 path->trace_type = trace->trace_type;
212 if (0 < (trace->pkt_counter - trace_cached->pkt_counter))
214 u64 new_sum = trace->mean_delay * record->seqno_data.rx_packets;
216 trace_cached->mean_delay *
217 record->chached_data_list->seqno_data.rx_packets;
219 (u32) ((new_sum - old_sum) / (trace->pkt_counter -
220 trace_cached->pkt_counter));
221 path->mean_delay = clib_host_to_net_u32 (path->mean_delay);
224 path->mean_delay = 0;
226 path->bytes_counter =
227 trace->bytes_counter - trace_cached->bytes_counter;
228 path->bytes_counter = clib_host_to_net_u32 (path->bytes_counter);
230 path->pkt_counter = trace->pkt_counter - trace_cached->pkt_counter;
231 path->pkt_counter = clib_host_to_net_u32 (path->pkt_counter);
233 for (j = 0; j < IOAM_TRACE_MAX_NODES; j++)
235 path->path[j].node_id =
236 clib_host_to_net_u32 (trace->path[j].node_id);
237 path->path[j].ingress_if =
238 clib_host_to_net_u16 (trace->path[j].ingress_if);
239 path->path[j].egress_if =
240 clib_host_to_net_u16 (trace->path[j].egress_if);
243 offset += sizeof (ioam_path);
247 num_paths = clib_host_to_net_u16 (num_paths);
248 memcpy (b0->data + num_paths_offset, &num_paths, sizeof (u16));
251 *(record->chached_data_list) = *record;
252 record->chached_data_list->chached_data_list = NULL;
254 *(record->writer_lock) = 0;
259 ioam_send_flows (flow_report_main_t * frm, flow_report_t * fr,
260 vlib_frame_t * f, u32 * to_next, u32 node_index)
262 vlib_buffer_t *b0 = NULL;
266 ip4_ipfix_template_packet_t *tp;
267 ipfix_message_header_t *h;
268 ipfix_set_header_t *s = NULL;
271 u32 records_this_buffer;
274 vlib_main_t *vm = frm->vlib_main;
276 ioam_analyser_data_t *record = NULL;
277 flow_report_stream_t *stream;
278 ioam_analyser_data_t *aggregated_data;
281 stream = &frm->streams[fr->stream_index];
283 memset (&temp, 0, sizeof (ip6_address_t));
285 aggregated_data = ioam_analyser_main.aggregated_data;
286 data_len = vec_len (aggregated_data);
288 vec_foreach_index (i, aggregated_data)
291 record = aggregated_data + i;
293 /* Flush if last entry */
294 if (i == (data_len - 1))
297 if (!record->is_free)
300 if (PREDICT_FALSE (b0 == NULL))
302 if (vlib_buffer_alloc (vm, &bi0, 1) != 1)
305 b0 = vlib_get_buffer (vm, bi0);
306 memcpy (b0->data, fr->rewrite, vec_len (fr->rewrite));
307 b0->current_data = 0;
308 b0->current_length = vec_len (fr->rewrite);
309 b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
310 vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
311 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
313 tp = vlib_buffer_get_current (b0);
319 /* FIXUP: message header export_time */
320 h->export_time = clib_host_to_net_u32 (((u32) time (NULL)));
322 /* FIXUP: message header sequence_number */
323 h->sequence_number = stream->sequence_number++;
324 h->sequence_number = clib_host_to_net_u32 (h->sequence_number);
325 next_offset = (u32) (((u8 *) (s + 1)) - (u8 *) tp);
326 records_this_buffer = 0;
329 next_offset = ioam_analyse_add_ipfix_record (fr, record,
332 records_this_buffer++;
334 /* Flush data if packet len is about to reach path mtu */
335 if (next_offset > (frm->path_mtu - 250))
339 if (PREDICT_FALSE (flush && b0))
341 s->set_id_length = ipfix_set_id_length (IOAM_FLOW_TEMPLATE_ID,
342 next_offset - (sizeof (*ip) +
345 b0->current_length = next_offset;
346 b0->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
347 tp = vlib_buffer_get_current (b0);
348 ip = (ip4_header_t *) & tp->ip4;
349 udp = (udp_header_t *) (ip + 1);
353 new_l0 = clib_host_to_net_u16 ((u16) next_offset);
354 sum0 = ip_csum_update (sum0, old_l0, new_l0, ip4_header_t,
355 length /* changed member */ );
357 ip->checksum = ip_csum_fold (sum0);
360 clib_host_to_net_u16 (b0->current_length - sizeof (*ip));
362 if (frm->udp_checksum)
364 /* RFC 7011 section 10.3.2. */
365 udp->checksum = ip4_tcp_udp_compute_checksum (vm, b0, ip);
366 if (udp->checksum == 0)
367 udp->checksum = 0xffff;
374 if (f->n_vectors == VLIB_FRAME_SIZE)
376 vlib_put_frame_to_node (vm, node_index, f);
377 f = vlib_get_frame_to_node (vm, node_index);
379 to_next = vlib_frame_vector_args (f);
390 ioam_flow_create (u8 del)
392 vnet_flow_report_add_del_args_t args;
395 flow_report_main_t *frm = &flow_report_main;
397 args.rewrite_callback = ioam_template_rewrite;
398 args.flow_data_callback = ioam_send_flows;
399 del ? (args.is_add = 0) : (args.is_add = 1);
400 args.domain_id = domain_id;
402 rv = vnet_flow_report_add_del (frm, &args);
408 case VNET_API_ERROR_NO_SUCH_ENTRY:
409 return clib_error_return (0, "registration not found...");
411 return clib_error_return (0, "vnet_flow_report_add_del returned %d",
419 ioam_flow_report_init (vlib_main_t * vm)
423 if ((error = vlib_call_init_function (vm, flow_report_init)))
429 VLIB_INIT_FUNCTION (ioam_flow_report_init);
432 * fd.io coding-style-patch-verification: ON
435 * eval: (c-set-style "gnu")